Skip to content

Commit fec8a10

Browse files
committed
Add resultant methods to fmpz_poly and fmpq_poly
1 parent b87c681 commit fec8a10

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/flint/types/fmpq_poly.pyx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,32 @@ cdef class fmpq_poly(flint_poly):
415415
fmpq_poly_gcd(res.val, self.val, (<fmpq_poly>other).val)
416416
return res
417417

418+
def resultant(self, other):
419+
"""
420+
Returns the resultant of *self* and *other*.
421+
422+
>>> A = fmpq_poly([1, 0, -1]); B = fmpq_poly([1, -1])
423+
>>> A.resultant(B)
424+
0
425+
>>> C = fmpq_poly([1, 0, 0, 0, 0, -1, 1])
426+
>>> D = fmpq_poly([1, 0, 0, -1, 0, 0, 1])
427+
>>> C.resultant(D)
428+
3
429+
>>> f = fmpq_poly([1, -1] + [0] * 98 + [1])
430+
>>> g = fmpq_poly([1] + [0] * 50 + [-1] + [0] * 48 + [1])
431+
>>> f.resultant(g)
432+
1125899906842623
433+
434+
"""
435+
cdef fmpq res
436+
other = any_as_fmpq_poly(other)
437+
if other is NotImplemented:
438+
raise TypeError("cannot convert input to fmpq_poly")
439+
440+
res = fmpq.__new__(fmpq)
441+
fmpq_poly_resultant(res.val, self.val, (<fmpq_poly>other).val)
442+
return res
443+
418444
def xgcd(self, other):
419445
cdef fmpq_poly res1, res2, res3
420446
other = any_as_fmpq_poly(other)

src/flint/types/fmpz_poly.pyx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,60 @@ cdef class fmpz_poly(flint_poly):
397397
fmpz_poly_gcd(res.val, self.val, (<fmpz_poly>other).val)
398398
return res
399399

400+
def resultant(self, other):
401+
"""
402+
Returns the resultant of *self* and *other*.
403+
404+
>>> A = fmpz_poly([1, 0, -1]); B = fmpz_poly([1, -1])
405+
>>> A.resultant(B)
406+
0
407+
>>> C = fmpz_poly([1, 0, 0, 0, 0, -1, 1])
408+
>>> D = fmpz_poly([1, 0, 0, -1, 0, 0, 1])
409+
>>> C.resultant(D)
410+
3
411+
>>> f = fmpz_poly([1, -1] + [0] * 98 + [1])
412+
>>> g = fmpz_poly([1] + [0] * 50 + [-1] + [0] * 48 + [1])
413+
>>> f.resultant(g)
414+
1125899906842623
415+
416+
"""
417+
cdef fmpz res
418+
other = any_as_fmpz_poly(other)
419+
if other is NotImplemented:
420+
raise TypeError("cannot convert input to fmpz_poly")
421+
422+
res = fmpz.__new__(fmpz)
423+
fmpz_poly_resultant(res.val, self.val, (<fmpz_poly>other).val)
424+
return res
425+
426+
def resultant_modular(self, other):
427+
"""
428+
Returns the resultant of *self* and *other* using Collins' 1971 modular
429+
algorithm.
430+
431+
>>> A = fmpz_poly([1, 0, -1]); B = fmpz_poly([1, -1])
432+
>>> A.resultant_modular(B)
433+
0
434+
>>> C = fmpz_poly([1, 0, 0, 0, 0, -1, 1])
435+
>>> D = fmpz_poly([1, 0, 0, -1, 0, 0, 1])
436+
>>> C.resultant_modular(D)
437+
3
438+
>>> f = fmpz_poly([1, -1] + [0] * 98 + [1])
439+
>>> g = fmpz_poly([1] + [0] * 50 + [-1] + [0] * 48 + [1])
440+
>>> f.resultant_modular(g)
441+
1125899906842623
442+
443+
"""
444+
cdef fmpz res
445+
other = any_as_fmpz_poly(other)
446+
if other is NotImplemented:
447+
raise TypeError("cannot convert input to fmpz_poly")
448+
449+
res = fmpz.__new__(fmpz)
450+
fmpz_poly_resultant_modular(res.val, self.val, (<fmpz_poly>other).val)
451+
return res
452+
453+
400454
def factor(self):
401455
"""
402456
Factors self into irreducible factors, returning a tuple

0 commit comments

Comments
 (0)