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.Sat, 08 Jan 2011 11:07:17 +0100Compiling .spyx-fileshttps://ask.sagemath.org/question/7854/compiling-spyx-files/This is an implementation of the pollard-rho factorization-algorithm in sage (filename: exp.sage)
def pollard_rho(n):
x = 2
y = x
t = 1
while t == 1:
x = mod(x,n)^2+1
y = mod(mod(y,n)^2+1,n)^2+1
t = gcd(x-y,n)
if t<n:
return(t)
else:
return 0
I load it with "`load exp.sage`" and it works.
But trying to compile
import sage.all
def pollard_rho(n):
x = 2
y = x
t = 1
while t == 1:
x = sage.all.mod(x,n)**2+1
y = sage.all.mod(sage.all.mod(y,n)**2+1,n)**2+1
t = sage.all.gcd(x-y,n)
if t<n:
return(t)
else:
return 0
named as "`exp.spyx`" brings several errors, one of them (f.e.):
13 while t == 1:
14 x = sage.all.mod(x,n)**2+1
---> 15 y = sage.all.mod(sage.mod(y,n)**2+1,n)**2+1
16 t = sage.all.gcd(x-y,n)
17
AttributeError: 'module' object has no attribute 'mod'
What is wrong here? Probably many things - im totally new to Sage, coming from Pari-gp and work in console-mode - no experiences with C or Python. Thank you.
Sat, 08 Jan 2011 08:11:29 +0100https://ask.sagemath.org/question/7854/compiling-spyx-files/Answer by DSM for <p>This is an implementation of the pollard-rho factorization-algorithm in sage (filename: exp.sage)</p>
<pre><code>def pollard_rho(n):
x = 2
y = x
t = 1
while t == 1:
x = mod(x,n)^2+1
y = mod(mod(y,n)^2+1,n)^2+1
t = gcd(x-y,n)
if t<n:
return(t)
else:
return 0
</code></pre>
<p>I load it with "<code>load exp.sage</code>" and it works.</p>
<p>But trying to compile </p>
<pre><code>import sage.all
def pollard_rho(n):
x = 2
y = x
t = 1
while t == 1:
x = sage.all.mod(x,n)**2+1
y = sage.all.mod(sage.all.mod(y,n)**2+1,n)**2+1
t = sage.all.gcd(x-y,n)
if t<n:
return(t)
else:
return 0
</code></pre>
<p>named as "<code>exp.spyx</code>" brings several errors, one of them (f.e.):</p>
<pre><code> 13 while t == 1:
14 x = sage.all.mod(x,n)**2+1
---> 15 y = sage.all.mod(sage.mod(y,n)**2+1,n)**2+1
16 t = sage.all.gcd(x-y,n)
17
AttributeError: 'module' object has no attribute 'mod'
</code></pre>
<p>What is wrong here? Probably many things - im totally new to Sage, coming from Pari-gp and work in console-mode - no experiences with C or Python. Thank you.</p>
https://ask.sagemath.org/question/7854/compiling-spyx-files/?answer=11943#post-id-11943It seems to work for me:
<pre><code>sage: load "exp.spyx"
Compiling exp.spyx...
sage: time pollard_rho(next_prime(10**6)*previous_prime(10**7))
CPU times: user 0.23 s, sys: 0.01 s, total: 0.23 s
Wall time: 0.24 s
1000003
</code></pre>
I'm a little puzzled, though, because the error message you show doesn't seem to correspond to the line in your source, namely that in the code you put up it says "`sage.all.mod(sage.all.mod(y,n)**2+1,n)**2+1"` but the error message points to a line of code reading "`y = sage.all.mod(sage.mod(y,n)**2+1,n)**2+1`", without an ".all." between sage and mod, so it seems to be looking in the module "sage" for the function mod.
For what it's worth, that's exactly the error message I get if I remove the ".all".Sat, 08 Jan 2011 10:44:13 +0100https://ask.sagemath.org/question/7854/compiling-spyx-files/?answer=11943#post-id-11943Answer by Mike Hansen for <p>This is an implementation of the pollard-rho factorization-algorithm in sage (filename: exp.sage)</p>
<pre><code>def pollard_rho(n):
x = 2
y = x
t = 1
while t == 1:
x = mod(x,n)^2+1
y = mod(mod(y,n)^2+1,n)^2+1
t = gcd(x-y,n)
if t<n:
return(t)
else:
return 0
</code></pre>
<p>I load it with "<code>load exp.sage</code>" and it works.</p>
<p>But trying to compile </p>
<pre><code>import sage.all
def pollard_rho(n):
x = 2
y = x
t = 1
while t == 1:
x = sage.all.mod(x,n)**2+1
y = sage.all.mod(sage.all.mod(y,n)**2+1,n)**2+1
t = sage.all.gcd(x-y,n)
if t<n:
return(t)
else:
return 0
</code></pre>
<p>named as "<code>exp.spyx</code>" brings several errors, one of them (f.e.):</p>
<pre><code> 13 while t == 1:
14 x = sage.all.mod(x,n)**2+1
---> 15 y = sage.all.mod(sage.mod(y,n)**2+1,n)**2+1
16 t = sage.all.gcd(x-y,n)
17
AttributeError: 'module' object has no attribute 'mod'
</code></pre>
<p>What is wrong here? Probably many things - im totally new to Sage, coming from Pari-gp and work in console-mode - no experiences with C or Python. Thank you.</p>
https://ask.sagemath.org/question/7854/compiling-spyx-files/?answer=11942#post-id-11942I think the version you tried to compile is different than the one posted above. In the error message, you can see the line ` sage.all.mod(sage.mod(y,n)**2+1,n)**2+1` which includes `sage.mod` which doesn't appear anywhere in the version you posted.
When I try to compile the `exp.spyx` file posted above, then everything works fine for me. Try that and let me know how it goes.Sat, 08 Jan 2011 10:43:52 +0100https://ask.sagemath.org/question/7854/compiling-spyx-files/?answer=11942#post-id-11942Comment by Mustafa for <p>I think the version you tried to compile is different than the one posted above. In the error message, you can see the line <code>sage.all.mod(sage.mod(y,n)**2+1,n)**2+1</code> which includes <code>sage.mod</code> which doesn't appear anywhere in the version you posted.</p>
<p>When I try to compile the <code>exp.spyx</code> file posted above, then everything works fine for me. Try that and let me know how it goes.</p>
https://ask.sagemath.org/question/7854/compiling-spyx-files/?comment=22316#post-id-22316Yes, sorry, thank you Mike and DSM - it seems as if I intended to annoy people here with this. I did correct that in my posting, but didn't realize that this could be the mistake ... To be sure: On my machine theres absolutely no speed-up of the compiled version (its even a bit slower), whys that?Sat, 08 Jan 2011 11:07:17 +0100https://ask.sagemath.org/question/7854/compiling-spyx-files/?comment=22316#post-id-22316