Ask Your Question

Division algorithm in one variable

asked 2020-09-06 19:59:38 +0100

Hit gravatar image

Hi! I'm trying to implement the division algorithm 'by hand' (yes, I know the existence of f.maxima_methods().divide(g))

So far I've came up with this:

def div(f,g):
q==0; r==f;
while r != 0 and
return q,r

but I'm getting an error on the "while" line. I'm following Ideals, Varieties and Algorithms by Cox (page 39 of the book), and they insert a "do" at the end of the while line, but I'm getting an "invalid syntax" error there. I'm pretty inexperienced with coding, and even more with Python/Sage (I've only taken a numerical methods course with Fortran).

I've gotten this "far" reading the book + documentation. Any ideas on how to proceed here?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-09-07 02:33:14 +0100

slelievre gravatar image

In Python (the programming language Sage uses),

  • assignment uses =, while == is to test equality
  • indentation is syntax, and in particular
  • the content of a while loop is everything that is indented after :.

So your code might want to look something like that instead:

def div(f, g):
    q = 0
    r = f
    while r != 0 and
        q = q + //
        r = r - ( // * g
    return q, r

Then you can use the function as follows.

Define a polynomial ring and two polynomials:

sage: R.<x> = QQ[]
sage: f = x^5 + x^3 + 2
sage: g = x^2 + 1

Apply the function:

sage: div(f, g)
(x^3, 2)
edit flag offensive delete link more


Hi! After playing and reading for a while after posting the question I managed to make it look similar to yours, but still wasn't compiling correctly. Now I see why

Thanks a ton, mate!!

Hit gravatar imageHit ( 2020-09-07 05:51:37 +0100 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2020-09-06 19:59:38 +0100

Seen: 281 times

Last updated: Sep 07 '20