File tree 3 files changed +39
-5
lines changed 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():
3193
3193
3194
3194
def factor (p ):
3195
3195
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 ()
3201
3197
assert type (coeff ) is type (lc )
3202
3198
assert isinstance (factors , list )
3203
3199
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):
176
176
def is_one (self ):
177
177
return < bint> fmpq_poly_is_one(self .val)
178
178
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
+
179
197
def __call__ (self , other ):
180
198
t = any_as_fmpz(other)
181
199
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):
144
144
def is_one (self ):
145
145
return < bint> fmpz_poly_is_one(self .val)
146
146
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
+
147
167
def __call__ (self , other ):
148
168
t = any_as_fmpz(other)
149
169
if t is not NotImplemented :
You can’t perform that action at this time.
0 commit comments