Ask Your Question
1

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

asked 2024-08-11 05:20:30 +0100

fahad gravatar image

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 flag offensive 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

Comments

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

FrédéricC gravatar imageFrédéricC ( 2024-08-11 13:49:20 +0100 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2024-08-11 09:58:47 +0100

fahad gravatar image

updated 2024-08-12 11:47:00 +0100

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

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2024-08-11 05:20:30 +0100

Seen: 189 times

Last updated: Aug 12