Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ CHANGELOG
Next release (0.9.0)...
-----------------------

Contributors (0.9.0):

- Rémy Oudompheng (RO)

Changes (0.9.0):

- [gh-312](https://github.com/flintlib/python-flint/pull/312),
Add `discriminant` method to `fmpz_poly`, `fmpq_poly` and
`nmod_poly`. (RO)

0.8.0
-----

Expand Down
3 changes: 3 additions & 0 deletions src/flint/test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -3045,6 +3045,9 @@ def setbad(obj, i, val):
assert x.resultant(x**10 - x**5 + 1) == S(1)
assert (x - 1).resultant(x**5 + 1) == S(2)

assert (x**5 + 1).discriminant() == S(3125)
assert (x**5 + 1).resultant(5 * x**4) == S(3125)

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

Expand Down
1 change: 1 addition & 0 deletions src/flint/types/fmpq_poly.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class fmpq_poly(flint_poly[fmpq]):
def truncate(self, n: int, /) -> fmpq_poly: ...

def gcd(self, other: ifmpq_poly, /) -> fmpq_poly: ...
def discriminant(self) -> fmpq: ...
def resultant(self, other: ifmpq_poly, /) -> fmpq: ...
def xgcd(self, other: ifmpq_poly, /) -> tuple[fmpq_poly, fmpq_poly, fmpq_poly]: ...

Expand Down
31 changes: 30 additions & 1 deletion src/flint/types/fmpq_poly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ from flint.types.fmpz cimport any_as_fmpz

from flint.flintlib.functions.fmpz cimport fmpz_is_zero
from flint.flintlib.functions.fmpz cimport fmpz_set
from flint.flintlib.functions.fmpq cimport fmpq_is_zero
from flint.flintlib.functions.fmpz_poly cimport fmpz_poly_discriminant
from flint.flintlib.functions.fmpq cimport fmpq_is_zero, fmpq_set_fmpz_frac
from flint.flintlib.functions.fmpq_poly cimport *
from flint.flintlib.functions.arith cimport arith_bernoulli_polynomial
from flint.flintlib.functions.arith cimport arith_euler_polynomial
Expand Down Expand Up @@ -518,6 +519,34 @@ cdef class fmpq_poly(flint_poly):
fmpq_poly_gcd(res.val, self.val, (<fmpq_poly>other).val)
return res

def discriminant(self):
"""
Return the discriminant of ``self``.

>>> f = fmpq_poly([1, 2, 3, 4, 5, 6])
>>> f.discriminant()
1037232
>>> f = fmpq_poly([1, 3, 5, 7, 9, 11, 13])
>>> f.discriminant()
-2238305839
>>> f = fmpq_poly([1, 3, 5, 7, 9, 11, 13], 10)
>>> f.discriminant()
-2238305839/10000000000

"""
# There is no FLINT function for the discriminant of a fmpq_poly,
# we use the fact that disc(f/q) = disc(f)/q^(2d-2)
cdef fmpq res = fmpq.__new__(fmpq)
cdef fmpz rnum = fmpz.__new__(fmpz)
cdef fmpz rden = self.denom()**(2 * self.degree() - 2)

cdef fmpz_poly x = fmpz_poly.__new__(fmpz_poly)
fmpq_poly_get_numerator(x.val, self.val)
fmpz_poly_discriminant(rnum.val, x.val)
fmpq_set_fmpz_frac(res.val, rnum.val, rden.val)

return res

def resultant(self, other):
"""
Returns the resultant of *self* and *other*.
Expand Down
1 change: 1 addition & 0 deletions src/flint/types/fmpz_poly.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class fmpz_poly(flint_poly[fmpz]):

def gcd(self, other: ifmpz_poly, /) -> fmpz_poly: ...
def content(self) -> fmpz: ...
def discriminant(self) -> fmpz: ...
def resultant(self, other: ifmpz_poly, /) -> fmpz: ...
def factor(self) -> tuple[fmpz, list[tuple[fmpz_poly, int]]]: ...
def factor_squarefree(self) -> tuple[fmpz, list[tuple[fmpz_poly, int]]]: ...
Expand Down
16 changes: 16 additions & 0 deletions src/flint/types/fmpz_poly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,22 @@ cdef class fmpz_poly(flint_poly):
fmpz_poly_gcd(res.val, self.val, (<fmpz_poly>other).val)
return res

def discriminant(self):
"""
Return the discriminant of ``self``.

>>> f = fmpz_poly([1, 2, 3, 4, 5, 6])
>>> f.discriminant()
1037232
>>> f = fmpz_poly([1, 3, 5, 7, 9, 11, 13])
>>> f.discriminant()
-2238305839

"""
cdef fmpz res = fmpz.__new__(fmpz)
fmpz_poly_discriminant(res.val, self.val)
return res

def resultant(self, other):
"""
Returns the resultant of *self* and *other*.
Expand Down
1 change: 1 addition & 0 deletions src/flint/types/nmod_poly.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class nmod_poly(flint_poly[nmod]):
self, e: int, modulus: inmod_poly, mod_rev_inv: inmod_poly | None = None
) -> nmod_poly: ...
def gcd(self, other: inmod_poly) -> nmod_poly: ...
def discriminant(self) -> nmod: ...
def resultant(self, other: inmod_poly) -> nmod: ...
def xgcd(self, other: inmod_poly) -> tuple[nmod_poly, nmod_poly, nmod_poly]: ...
def factor(
Expand Down
16 changes: 16 additions & 0 deletions src/flint/types/nmod_poly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,22 @@ cdef class nmod_poly(flint_poly):
nmod_poly_gcd(res.val, self.val, (<nmod_poly>other).val)
return res

def discriminant(self):
"""
Return the discriminant of ``self``.

>>> f = nmod_poly([1, 2, 3, 4, 5, 6], 65537)
>>> f.discriminant()
54177
>>> f = nmod_poly([1, 3, 5, 7, 9, 11, 13], 65537)
>>> f.discriminant()
44859

"""
cdef nmod res = nmod(0, self.modulus())
res.val = nmod_poly_discriminant(self.val)
return res

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