File tree Expand file tree Collapse file tree 3 files changed +39
-5
lines changed Expand file tree Collapse file tree 3 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -3193,11 +3193,7 @@ def test_factor_poly_mpoly():
31933193
31943194 def factor (p ):
31953195 coeff , factors = p .factor ()
3196- try :
3197- lc = p .leading_coefficient ()
3198- except AttributeError :
3199- # XXX: Not all univariate types have a leading_coefficient method.
3200- lc = p [0 ]
3196+ lc = p .leading_coefficient ()
32013197 assert type (coeff ) is type (lc )
32023198 assert isinstance (factors , list )
32033199 assert all (isinstance (f , tuple ) for f in factors )
Original file line number Diff line number Diff line change @@ -176,6 +176,24 @@ cdef class fmpq_poly(flint_poly):
176176 def is_one (self ):
177177 return < bint> fmpq_poly_is_one(self .val)
178178
179+ def leading_coefficient (self ):
180+ """
181+ Returns the leading coefficient of the polynomial.
182+
183+ >>> f = fmpq_poly([1, 2, 3])
184+ >>> f
185+ 3*x^2 + 2*x + 1
186+ >>> f.leading_coefficient()
187+ 3
188+ """
189+ cdef fmpq x
190+ cdef slong d
191+ d = fmpq_poly_degree(self .val)
192+ x = fmpq.__new__ (fmpq)
193+ if d >= 0 :
194+ fmpq_poly_get_coeff_fmpq(x.val, self .val, d)
195+ return x
196+
179197 def __call__ (self , other ):
180198 t = any_as_fmpz(other)
181199 if t is not NotImplemented :
Original file line number Diff line number Diff line change @@ -144,6 +144,26 @@ cdef class fmpz_poly(flint_poly):
144144 def is_one (self ):
145145 return < bint> fmpz_poly_is_one(self .val)
146146
147+ def leading_coefficient (self ):
148+ """
149+ Returns the leading coefficient of the polynomial.
150+
151+ >>> f = fmpz_poly([1, 2, 3])
152+ >>> f
153+ 3*x^2 + 2*x + 1
154+ >>> f.leading_coefficient()
155+ 3
156+ """
157+ cdef fmpz x
158+ cdef slong d
159+ d = fmpz_poly_degree(self .val)
160+ if d < 0 :
161+ x = fmpz.__new__ (fmpz)
162+ else :
163+ x = fmpz.__new__ (fmpz)
164+ fmpz_poly_get_coeff_fmpz(x.val, self .val, d)
165+ return x
166+
147167 def __call__ (self , other ):
148168 t = any_as_fmpz(other)
149169 if t is not NotImplemented :
You can’t perform that action at this time.
0 commit comments