Ask Your Question
0

Univariate polynomial division implementation

asked 2020-09-04 01:38:03 +0200

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-09-04 03:15:22 +0200

Fixed by casting m into the base ring, m=R(m) I guess this is some sloppy patch I would like to know if there is some smarter way to make this algorithm.

The code:

x=var('x')
R.<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()
            m=R(m)
            l=l+t*m
            r=r-(t*m*q)
            print(l,r)
        return(l,r)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-09-04 01:38:03 +0200

Seen: 333 times

Last updated: Sep 04 '20