ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 17 Jun 2013 05:18:48 -0500why this algorithm does not work?http://ask.sagemath.org/question/10234/why-this-algorithm-does-not-work/Hello
I'm trying to write this algorithm in sage:
def roots(f, q):
# return list of roots of f in finite field of q elements
K.<T> = GF(q)
r = [ ]
g = qq2zz(f).change_ring(K)
for a in K:
if g(a) == 0:
r.append(a)
return r
but i get error, it says that:
Traceback (click to the left of this block for traceback ) ....
how should I correct it?
Thu, 13 Jun 2013 09:28:39 -0500http://ask.sagemath.org/question/10234/why-this-algorithm-does-not-work/Answer by tmonteil for <p>Hello
I'm trying to write this algorithm in sage:</p>
<pre><code>def roots(f, q):
# return list of roots of f in finite field of q elements
K.<T> = GF(q)
r = [ ]
g = qq2zz(f).change_ring(K)
for a in K:
if g(a) == 0:
r.append(a)
return r
</code></pre>
<p>but i get error, it says that:</p>
<pre><code>Traceback (click to the left of this block for traceback ) ....
</code></pre>
<p>how should I correct it?</p>
http://ask.sagemath.org/question/10234/why-this-algorithm-does-not-work/?answer=15079#post-id-15079What is interesting is the line that comes after the message
`Traceback (click to the left of this block for traceback)`
You are using the notebook, to see more information about the error, just click on the left side of the word `Traceback`, and then you will get more details.
In your case, it seems that the function `qq2zz()` does not exist (maybe you defined it before, at least it does not exist in sage).
Thu, 13 Jun 2013 10:00:10 -0500http://ask.sagemath.org/question/10234/why-this-algorithm-does-not-work/?answer=15079#post-id-15079Answer by barbules for <p>Hello
I'm trying to write this algorithm in sage:</p>
<pre><code>def roots(f, q):
# return list of roots of f in finite field of q elements
K.<T> = GF(q)
r = [ ]
g = qq2zz(f).change_ring(K)
for a in K:
if g(a) == 0:
r.append(a)
return r
</code></pre>
<p>but i get error, it says that:</p>
<pre><code>Traceback (click to the left of this block for traceback ) ....
</code></pre>
<p>how should I correct it?</p>
http://ask.sagemath.org/question/10234/why-this-algorithm-does-not-work/?answer=15098#post-id-15098The problem is change_ring(). You must give the new ring of the coefficients instead of the new ring of f. The code below works.
def roots(f, q):
# return list of roots of f in finite field of q elements
K = GF(q)
r = [ ]
g = f.change_ring(K)
for a in K:
if g(a) == 0:
r.append(a)
return r
"""EXAMPLE
R.<x>=QQ['x']
roots(2*x+1,7)
"""
Mon, 17 Jun 2013 05:18:48 -0500http://ask.sagemath.org/question/10234/why-this-algorithm-does-not-work/?answer=15098#post-id-15098