Ask Your Question

Revision history [back]

Univariate polynomial division implementation

I'm trying to implement the polynomial division given by the pseudocode in Wikipedia but I'm getting some weird error that says

AttributeError: 'sage.rings.fraction_field_element.FractionFieldElement_1poly_field' object has no attribute 'degree'

I think it's some coercion error but I'm new to sage so I wouldn't know.

The code:

x=var('x')
P.<x>=PolynomialRing(QQ)

def div(p,q):
    if q==0:
        return("NaN")
    elif q!=0:
        l=0
        r=p
        while r!=0 and q.degree()<=r.degree():
            t=r.leading_coefficient()/q.leading_coefficient()
            m=x^r.degree()/x^q.degree()
            l=l+t*m
            r=r-(t*m*q)
            print(l,r) #I was seeing when the code failed
        return(l,r)

When I call div(x^2-1,x-1) it prints x x-1 but then doesn't do the next step and shows the error.