@@ -233,9 +233,26 @@ cdef class nmod_poly(flint_poly):
233233 nmod_poly_reverse(res.val, self .val, length)
234234 return res
235235
236+ def leading_coefficient (self ):
237+ """
238+ Return the leading coefficient of this polynomial.
239+
240+ >>> f = nmod_poly([123, 129, 63, 14, 51, 76, 133], 163)
241+ >>> f.leading_coefficient()
242+ 133
243+ """
244+ # XXX: This is a workaround for a Cython/PyPy bug:
245+ # https://github.com/flintlib/python-flint/issues/74
246+ # https://github.com/cython/cython/issues/5776
247+ d = self .degree()
248+ if d < 0 :
249+ return 0
250+ return nmod_poly_get_coeff_ui(self .val, d)
251+
236252 def inverse_series_trunc (self , slong n ):
237253 """
238- Returns the inverse of ``self`` modulo `x^n`.
254+ Returns the inverse of ``self`` modulo `x^n`. Assumes the leading
255+ coefficient of the polynomial is invertible.
239256
240257 >>> f = nmod_poly([123, 129, 63, 14, 51, 76, 133], 163)
241258 >>> f.inverse_series_trunc(3)
@@ -245,6 +262,9 @@ cdef class nmod_poly(flint_poly):
245262 >>> f.inverse_series_trunc(5)
246263 45*x^4 + 23*x^3 + 159*x^2 + 151*x + 110
247264 """
265+ if n <= 0 :
266+ raise ValueError (" n must be positive" )
267+
248268 cdef nmod_poly res
249269 res = nmod_poly.__new__ (nmod_poly)
250270 nmod_poly_init_preinv(res.val, self .val.mod.n, self .val.mod.ninv)
0 commit comments