Skip to content

Commit e0312f8

Browse files
committed
add leading coefficent
1 parent ae31437 commit e0312f8

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/flint/types/nmod_poly.pyx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,23 @@ 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+
d = self.degree()
245+
if d < 0:
246+
return 0
247+
return nmod_poly_get_coeff_ui(self.val, d)
248+
236249
def inverse_series_trunc(self, slong n):
237250
"""
238-
Returns the inverse of ``self`` modulo `x^n`.
251+
Returns the inverse of ``self`` modulo `x^n`. Assumes the leading
252+
coefficient of the polynomial is invertible.
239253
240254
>>> f = nmod_poly([123, 129, 63, 14, 51, 76, 133], 163)
241255
>>> f.inverse_series_trunc(3)
@@ -245,6 +259,9 @@ cdef class nmod_poly(flint_poly):
245259
>>> f.inverse_series_trunc(5)
246260
45*x^4 + 23*x^3 + 159*x^2 + 151*x + 110
247261
"""
262+
if n <= 0:
263+
raise ValueError("n must be positive")
264+
248265
cdef nmod_poly res
249266
res = nmod_poly.__new__(nmod_poly)
250267
nmod_poly_init_preinv(res.val, self.val.mod.n, self.val.mod.ninv)

0 commit comments

Comments
 (0)