using expression Ok in two places but raised error in third [closed]

I am new to Sagemath. I am writing code to divide univariate polynomials (I know Sagemath has quo_rem and other ways but want the steps) 

def div1(f,f1): # returns qoutient and remainder of dividing f by f1
R.<x>=QQ['x']
g=R(f)
f1=R(f1)
q=R(0)
n=g.degree()
l=g.lt()
print(g,g.parent()," degree =",g.degree()," leading term =",g.lt())
while g.degree()>0:
if g.degree()>f1.degree():
tt=(g.lt()/f1.lt())
g=g-tt*f1
q=q+tt
print(g,"\t<<>>\t",q)

return [q,g]


The first two uses of g.degree() were OK but the one in the while loop raised an error AttributeError: 'sage.rings.fraction_field_element.FractionFieldElement_1poly_field' object has no attribute 'degree' I don't understand why. I would greatly appreciate any guidance Thanks

edit retag reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Max Alekseyev close date 2024-08-21 18:33:30.725421

use exact division // to remain in the ring of polynomial, instead of division /` that land in the fraction field

( 2024-08-11 13:49:20 +0200 )edit

Sort by » oldest newest most voted

OK. The problem was that the polynomial g might be a constant in which case the degree might not work When I changed the line g = g- ttf1 to q= R(g- ttf1) the code worked. I still would appreciate any commenOf course the logic of the code was incorrect. The loop condition should have been d.degree()>f1.degree() and no neeed for the if statement. Still the error i encountered had nothing to do with this

more