Skip to content

Commit

Permalink
powmod to pow_mod
Browse files Browse the repository at this point in the history
  • Loading branch information
GiacomoPope committed Aug 6, 2024
1 parent ae31437 commit a2f80ba
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/flint/types/nmod_poly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,26 @@ cdef class nmod_poly(flint_poly):
nmod_poly_reverse(res.val, self.val, length)
return res

def leading_coefficient(self):
"""
Return the leading coefficient of this polynomial.
>>> f = nmod_poly([123, 129, 63, 14, 51, 76, 133], 163)
>>> f.leading_coefficient()
133
"""
# XXX: This is a workaround for a Cython/PyPy bug:
# https://github.com/flintlib/python-flint/issues/74
# https://github.com/cython/cython/issues/5776
d = self.degree()
if d < 0:
return 0
return nmod_poly_get_coeff_ui(self.val, d)

def inverse_series_trunc(self, slong n):
"""
Returns the inverse of ``self`` modulo `x^n`.
Returns the inverse of ``self`` modulo `x^n`. Assumes the leading
coefficient of the polynomial is invertible.
>>> f = nmod_poly([123, 129, 63, 14, 51, 76, 133], 163)
>>> f.inverse_series_trunc(3)
Expand All @@ -245,6 +262,9 @@ cdef class nmod_poly(flint_poly):
>>> f.inverse_series_trunc(5)
45*x^4 + 23*x^3 + 159*x^2 + 151*x + 110
"""
if n <= 0:
raise ValueError("n must be positive")

cdef nmod_poly res
res = nmod_poly.__new__(nmod_poly)
nmod_poly_init_preinv(res.val, self.val.mod.n, self.val.mod.ninv)
Expand Down

0 comments on commit a2f80ba

Please sign in to comment.