Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resultant methods to fmpz_poly and fmpq_poly #274

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/flint/test/test_all.py
Original file line number Diff line number Diff line change
@@ -2834,6 +2834,44 @@ def setbad(obj, i, val):
if type(p) == flint.fq_default_poly:
assert raises(lambda: p.integral(), NotImplementedError)

# resultant checks.

if is_field and characteristic == 0:
# Check that the resultant of two cyclotomic polynomials is right.
# See Dresden's 2012 "Resultants of Cyclotomic Polynomials"
for m in range(1, 50):
for n in range(m + 1, 50):
a = flint.fmpz_poly.cyclotomic(m)
b = flint.fmpz_poly.cyclotomic(n)
Comment on lines +2839 to +2845
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are running in a loop over all polynomial types but don't depend on the type.

I think it would be better just to move all of this out to a separate test_poly_resultants test function but still use _all_polys for the part that loops over the types.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it would be better not to have such large test functions as this one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I'll split the resultant result checking off into a separate test.

In general it would be better not to have such large test functions as this one.

Do you mean test_all_polys is too long, or that this check with cyclotomic polynomials is too long?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that test_all_polys is too long.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally the test suite would have reasonably short test functions like test_resultants or something but where each test loops over all possible polynomial types. When a new polynomial type is added it should be possible to just add it to to the list in _all_polys and then update each test or add the methods that are needed for consistency with other types.

q, r = divmod(flint.fmpz(n), flint.fmpz(m))
fs = q.factor()
if r != 0 or len(fs) > 1:
assert a.resultant(b) == 1
else:
prime = fs[0][0]
tot = flint.fmpz(m).euler_phi()
assert a.resultant(b) == prime**tot

x = P([0, 1])

if composite_characteristic and type(x) == flint.fmpz_mod_poly:
# Flint crashes in this case, even though the resultant could be
# computed.
divisor = characteristic.factor()[0][0]
if type(x) == flint.fmpz_mod_poly:
assert raises(lambda: x.resultant(x + divisor), ValueError)
elif type(x) == flint.fq_default_poly:
# Flint does not implement resultants over GF(q) for nonprime q, so
# there's nothing for us to check.
pass
else:
assert x.resultant(x) == 0
assert x.resultant(x**2 + x - x) == 0
assert x.resultant(x**10 - x**5 + 1) == S(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised that nmod_poly doesn't crash here. I guess it sometimes does and sometimes doesn't:

In [3]: a = flint.nmod_poly([1, 2, 3], 12)

In [4]: b = flint.nmod_poly([1, 2, 3], 12)

In [5]: a.resultant(b)
Out[5]: 0

In [6]: b = flint.nmod_poly([1, 2, 3, 4], 12)

In [7]: a.resultant(b)
Flint exception (Impossible inverse):
    Cannot invert modulo 3*4
Aborted (core dumped)

assert (x - 1).resultant(x**5 + 1) == S(2)

for k in range(-10, 10):
assert x.resultant(x + S(k)) == S(k)

def _all_mpolys():
return [
@@ -2869,7 +2907,6 @@ def _all_mpolys():
),
]


def test_mpolys():
for P, get_context, S, is_field, characteristic in _all_mpolys():

26 changes: 26 additions & 0 deletions src/flint/types/fmpq_poly.pyx
Original file line number Diff line number Diff line change
@@ -415,6 +415,32 @@ cdef class fmpq_poly(flint_poly):
fmpq_poly_gcd(res.val, self.val, (<fmpq_poly>other).val)
return res

def resultant(self, other):
"""
Returns the resultant of *self* and *other*.

>>> A = fmpq_poly([1, 0, -1]); B = fmpq_poly([1, -1])
>>> A.resultant(B)
0
>>> C = fmpq_poly([1, 0, 0, 0, 0, -1, 1])
>>> D = fmpq_poly([1, 0, 0, -1, 0, 0, 1])
>>> C.resultant(D)
3
>>> f = fmpq_poly([1, -1] + [0] * 98 + [1])
>>> g = fmpq_poly([1] + [0] * 50 + [-1] + [0] * 48 + [1])
>>> f.resultant(g)
1125899906842623

"""
cdef fmpq res
other = any_as_fmpq_poly(other)
if other is NotImplemented:
raise TypeError("cannot convert input to fmpq_poly")

res = fmpq.__new__(fmpq)
fmpq_poly_resultant(res.val, self.val, (<fmpq_poly>other).val)
return res

def xgcd(self, other):
cdef fmpq_poly res1, res2, res3
other = any_as_fmpq_poly(other)
3 changes: 3 additions & 0 deletions src/flint/types/fmpz_mod_poly.pyx
Original file line number Diff line number Diff line change
@@ -1465,6 +1465,9 @@ cdef class fmpz_mod_poly(flint_poly):
"""
cdef fmpz_mod res

if not self.ctx.mod.is_prime():
raise ValueError("cannot compute fmpz_mod_poly resultants with composite moduli")

other = self.ctx.any_as_fmpz_mod_poly(other)
if other is NotImplemented:
raise TypeError(f"Cannot interpret {other} as a polynomial")
53 changes: 53 additions & 0 deletions src/flint/types/fmpz_poly.pyx
Original file line number Diff line number Diff line change
@@ -397,6 +397,59 @@ cdef class fmpz_poly(flint_poly):
fmpz_poly_gcd(res.val, self.val, (<fmpz_poly>other).val)
return res

def resultant(self, other):
"""
Returns the resultant of *self* and *other*.

>>> A = fmpz_poly([1, 0, -1]); B = fmpz_poly([1, -1])
>>> A.resultant(B)
0
>>> C = fmpz_poly([1, 0, 0, 0, 0, -1, 1])
>>> D = fmpz_poly([1, 0, 0, -1, 0, 0, 1])
>>> C.resultant(D)
3
>>> f = fmpz_poly([1, -1] + [0] * 98 + [1])
>>> g = fmpz_poly([1] + [0] * 50 + [-1] + [0] * 48 + [1])
>>> f.resultant(g)
1125899906842623

"""
cdef fmpz res
other = any_as_fmpz_poly(other)
if other is NotImplemented:
raise TypeError("cannot convert input to fmpz_poly")

res = fmpz.__new__(fmpz)
fmpz_poly_resultant(res.val, self.val, (<fmpz_poly>other).val)
return res

def resultant_modular(self, other):
"""
Returns the resultant of *self* and *other* using Collins' 1971 modular
algorithm.

>>> A = fmpz_poly([1, 0, -1]); B = fmpz_poly([1, -1])
>>> A.resultant_modular(B)
0
>>> C = fmpz_poly([1, 0, 0, 0, 0, -1, 1])
>>> D = fmpz_poly([1, 0, 0, -1, 0, 0, 1])
>>> C.resultant_modular(D)
3
>>> f = fmpz_poly([1, -1] + [0] * 98 + [1])
>>> g = fmpz_poly([1] + [0] * 50 + [-1] + [0] * 48 + [1])
>>> f.resultant_modular(g)
1125899906842623

"""
cdef fmpz res
other = any_as_fmpz_poly(other)
if other is NotImplemented:
raise TypeError("cannot convert input to fmpz_poly")

res = fmpz.__new__(fmpz)
fmpz_poly_resultant_modular(res.val, self.val, (<fmpz_poly>other).val)
return res

def factor(self):
"""
Factors self into irreducible factors, returning a tuple
20 changes: 20 additions & 0 deletions src/flint/types/nmod_poly.pyx
Original file line number Diff line number Diff line change
@@ -621,6 +621,26 @@ cdef class nmod_poly(flint_poly):
nmod_poly_gcd(res.val, self.val, (<nmod_poly>other).val)
return res

def resultant(self, other):
"""
Returns the resultant of *self* and *other*.

>>> f = nmod_poly([1, 2, 3], 3)
>>> g = nmod_poly([1, 0, 1], 3)
>>> f.resultant(f)
0
>>> f.resultant(g)
2

"""
cdef ulong res
other = any_as_nmod_poly(other, (<nmod_poly>self).val.mod)
if other is NotImplemented:
raise TypeError("cannot convert input to nmod_poly")

res = nmod_poly_resultant(self.val, (<nmod_poly>other).val)
return res

def xgcd(self, other):
cdef nmod_poly res1, res2, res3
other = any_as_nmod_poly(other, (<nmod_poly>self).val.mod)