First time here? Check out the FAQ!

Ask Your Question
1

Compiling .spyx-files

asked 14 years ago

Mustafa gravatar image

updated 13 years ago

Kelvin Li gravatar image

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.

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 14 years ago

Mike Hansen gravatar image

I 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.

Preview: (hide)
link

Comments

Yes, 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?

Mustafa gravatar imageMustafa ( 14 years ago )
1

answered 14 years ago

DSM gravatar image

updated 14 years ago

It seems to work for me:

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

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".

Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 14 years ago

Seen: 1,077 times

Last updated: Jan 08 '11