ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 07 Sep 2020 05:51:37 +0200Division algorithm in one variablehttps://ask.sagemath.org/question/53339/division-algorithm-in-one-variable/ 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 g.lt().divides(r.lt()):
q==q+r.lt()/g.lt()
r==r-(r.lt()/g.lt())*g
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?Sun, 06 Sep 2020 19:59:38 +0200https://ask.sagemath.org/question/53339/division-algorithm-in-one-variable/Answer by slelievre for <p>Hi! I'm trying to implement the division algorithm 'by hand' (yes, I know the existence of <code>f.maxima_methods().divide(g)</code>)</p>
<p>So far I've came up with this:</p>
<pre><code>def div(f,g):
q==0; r==f;
while r != 0 and g.lt().divides(r.lt()):
q==q+r.lt()/g.lt()
r==r-(r.lt()/g.lt())*g
return q,r
</code></pre>
<p>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).</p>
<p>I've gotten this "far" reading the book + documentation. Any ideas on how to proceed here?</p>
https://ask.sagemath.org/question/53339/division-algorithm-in-one-variable/?answer=53344#post-id-53344In 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 g.lt().divides(r.lt()):
q = q + r.lt() // g.lt()
r = r - (r.lt() // g.lt()) * 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)Mon, 07 Sep 2020 02:33:14 +0200https://ask.sagemath.org/question/53339/division-algorithm-in-one-variable/?answer=53344#post-id-53344Comment by Hit for <p>In Python (the programming language Sage uses),</p>
<ul>
<li>assignment uses <code>=</code>, while <code>==</code> is to test equality</li>
<li>indentation is syntax, and in particular</li>
<li>the content of a <code>while</code> loop is everything that is indented after <code>:</code>.</li>
</ul>
<p>So your code might want to look something like that instead:</p>
<pre><code>def div(f, g):
q = 0
r = f
while r != 0 and g.lt().divides(r.lt()):
q = q + r.lt() // g.lt()
r = r - (r.lt() // g.lt()) * g
return q, r
</code></pre>
<p>Then you can use the function as follows.</p>
<p>Define a polynomial ring and two polynomials:</p>
<pre><code>sage: R.<x> = QQ[]
sage: f = x^5 + x^3 + 2
sage: g = x^2 + 1
</code></pre>
<p>Apply the function:</p>
<pre><code>sage: div(f, g)
(x^3, 2)
</code></pre>
https://ask.sagemath.org/question/53339/division-algorithm-in-one-variable/?comment=53347#post-id-53347Hi! 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!!Mon, 07 Sep 2020 05:51:37 +0200https://ask.sagemath.org/question/53339/division-algorithm-in-one-variable/?comment=53347#post-id-53347