Skip to content

Commit 8bac286

Browse files
committed
Refactor resultant tests
1 parent 658e502 commit 8bac286

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

src/flint/test/test_all.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,31 +2835,13 @@ def setbad(obj, i, val):
28352835
assert raises(lambda: p.integral(), NotImplementedError)
28362836

28372837
# resultant checks.
2838-
2839-
if is_field and characteristic == 0:
2840-
# Check that the resultant of two cyclotomic polynomials is right.
2841-
# See Dresden's 2012 "Resultants of Cyclotomic Polynomials"
2842-
for m in range(1, 50):
2843-
for n in range(m + 1, 50):
2844-
a = flint.fmpz_poly.cyclotomic(m)
2845-
b = flint.fmpz_poly.cyclotomic(n)
2846-
q, r = divmod(flint.fmpz(n), flint.fmpz(m))
2847-
fs = q.factor()
2848-
if r != 0 or len(fs) > 1:
2849-
assert a.resultant(b) == 1
2850-
else:
2851-
prime = fs[0][0]
2852-
tot = flint.fmpz(m).euler_phi()
2853-
assert a.resultant(b) == prime**tot
2854-
28552838
x = P([0, 1])
28562839

2857-
if composite_characteristic and type(x) == flint.fmpz_mod_poly:
2858-
# Flint crashes in this case, even though the resultant could be
2859-
# computed.
2840+
if composite_characteristic and type(x) in [flint.fmpz_mod_poly, flint.nmod_poly]:
2841+
# Flint sometimes crashes in this case, even though the resultant
2842+
# could be computed.
28602843
divisor = characteristic.factor()[0][0]
2861-
if type(x) == flint.fmpz_mod_poly:
2862-
assert raises(lambda: x.resultant(x + divisor), ValueError)
2844+
assert raises(lambda: x.resultant(x + divisor), ValueError)
28632845
elif type(x) == flint.fq_default_poly:
28642846
# Flint does not implement resultants over GF(q) for nonprime q, so
28652847
# there's nothing for us to check.
@@ -2873,6 +2855,22 @@ def setbad(obj, i, val):
28732855
for k in range(-10, 10):
28742856
assert x.resultant(x + S(k)) == S(k)
28752857

2858+
def test_poly_resultants():
2859+
# Check that the resultant of two cyclotomic polynomials is right.
2860+
# See Dresden's 2012 "Resultants of Cyclotomic Polynomials"
2861+
for m in range(1, 50):
2862+
for n in range(m + 1, 50):
2863+
a = flint.fmpz_poly.cyclotomic(m)
2864+
b = flint.fmpz_poly.cyclotomic(n)
2865+
q, r = divmod(flint.fmpz(n), flint.fmpz(m))
2866+
fs = q.factor()
2867+
if r != 0 or len(fs) > 1:
2868+
assert a.resultant(b) == 1
2869+
else:
2870+
prime = fs[0][0]
2871+
tot = flint.fmpz(m).euler_phi()
2872+
assert a.resultant(b) == prime**tot
2873+
28762874
def _all_mpolys():
28772875
return [
28782876
(flint.fmpz_mpoly, flint.fmpz_mpoly_ctx.get, flint.fmpz, False, flint.fmpz(0)),
@@ -4869,6 +4867,8 @@ def test_all_tests():
48694867
test_polys,
48704868
test_mpolys,
48714869

4870+
test_poly_resultants,
4871+
48724872
test_fmpz_mpoly_vec,
48734873

48744874
test_matrices_eq,

src/flint/types/nmod_poly.pyx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,11 @@ cdef class nmod_poly(flint_poly):
634634
635635
"""
636636
cdef ulong res
637+
638+
mod = any_as_fmpz(self.val.mod.n)
639+
if not mod.is_prime():
640+
raise ValueError("cannot compute nmod_poly resultants with composite moduli")
641+
637642
other = any_as_nmod_poly(other, (<nmod_poly>self).val.mod)
638643
if other is NotImplemented:
639644
raise TypeError("cannot convert input to nmod_poly")

0 commit comments

Comments
 (0)