On 9/21/07, James wrote:
>    Is the behavior below legit?  The code is just to illustrate
> something for a class.

Jim,

All you have to do to fix the code is replace lt(r)/lt(g)
by lt(r)//lt(g) below.  The expression lt(r)/lt(g)
creates an element of Frac(R), so it is not iterable.
Another option would be to replace lt(r)/lt(g) by
R(lt(r)/lt(g)), which is slower, but safer in that if there
were a bug and lt(g) doesn't divide lt(r) you'll get
an error.   In general f//g means "floor division in the
same ring", so when g divides f you get back f/g as
an element of R.

 -- William

>
> -----
> /Users/tmp/sage/poly1.py in div(f, g)
>       30 def div(f,g):
>       31    q,r = Integer(0),f
> ---> 32    while not zero(r) and divides(lt(g),lt(r)):
>       33       q = q + lt(r)/lt(g)
>       34       r = r - (lt(r)/lt(g))*g
>
> /Users/tmp/sage/poly1.py in lt(f)
>        9
>       10 def lt(f):
> ---> 11    coeffs = list(f)
>       12    d = len(coeffs) - Integer(1)
>       13    return coeffs[d]*x**d
>
> <type 'exceptions.TypeError'>: 'FractionFieldElement' object is not
> iterable
> ---------
>
>
> K = QQ
> R.<x> = PolynomialRing(K)
> f = 1 + 3*x + 7*x^5
> g = 1 + 2*x^2
>
> def lt(f):
>     coeffs = list(f)
>     d = len(coeffs) - 1
>     return coeffs[d]*x^d
>
> def deg(f):
>     return len(list(f)) - 1
>
> def divides(f,g):
>     if deg(g) - deg(f) >= 0:
>        return True
>     else:
>        return False
>
> def zero(f):
>     if f == 0:
>        return True
>     else:
>        return False
>
> def div(f,g):
>     q,r = 0,f
>     while not zero(r) and divides(lt(g),lt(r)):
>        q = q + lt(r)/lt(g)
>        r = r - (lt(r)/lt(g))*g
>        print "hi"
>     return [q,r]
>


-- 
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/
-~----------~----~----~----~------~----~------~--~---

Reply via email to