Ask Your Question
1

running sage from a Python script

asked 2013-01-22 11:36:49 +0100

anonymous user

Anonymous

I'm using sage 5.5 on ubuntu 12.04. I'm trying to run sage via the following script

#!/usr/bin/env sage -python

import sys 
from sage.all import *

sigma = 10
z = var('z')
m0 = 128 
P = exp(-(1/2)*z**2/sigma**2)/(sqrt(2*pi)*sigma)
m = m0*exp(-(1/2)*z**2) 
mb = m0/sqrt(1+sigma**2)


f = lambda u: 1-2*(numerical_integral(u*P*m**2/(mb*(u*mb+(1-u)*m)), 0, Infinity))[0] 
print find_root(f, 0.1, 1, rtol=0.01)

I run this from the command line with

sage -python runDispK1.py

and I get the following error

Warning: invalid value encountered in absolute
Warning: invalid value encountered in absolute
Warning: invalid value encountered in absolute
Warning: invalid value encountered in absolute
Warning: invalid value encountered in absolute
Traceback (most recent call last):
  File "runDispK1.py", line 16, in <module>
    print find_root(f, 0.1, 1, rtol=0.01)                          
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/numerical/optimize.py", line 108, in find_root
    raise RuntimeError, "f appears to have no zero on the interval"
RuntimeError: f appears to have no zero on the interval

Whereas if I run the exact same code within sage, e.g. I have a file called run.sage with this text in it:

    sigma = 10
    z = var('z')
    m0 = 128 
    P = exp(-(1/2)*z**2/sigma**2)/(sqrt(2*pi)*sigma)
    m = m0*exp(-(1/2)*z**2) 
    mb = m0/sqrt(1+sigma**2)


    f = lambda u: 1-2*(numerical_integral(u*P*m**2/(mb*(u*mb+(1-u)*m)), 0, Infinity))[0] 
    print find_root(f, 0.1, 1, rtol=0.01)

then sage gives me the correct answer

sage: load("run.sage")
0.550279773862

Is there something I'm missing here? I'm new to sage and python scripting but it seems the numerical_integral function is returning nothing useful, so I are these functions created incorrectly?

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-01-22 13:08:49 +0100

ppurka gravatar image

Why don't you run it as sage instead of trying to run it as a python file? Simply use

#!/usr/bin/env sage

Also, note that the #! you used would try to run a command called sage -python instead of running the command sage with the argument -python.

edit flag offensive delete link more

Comments

That is, depending on the OS, having a command with a space in it after `#!/usr/bin/env` may not work as expected, so use `#!/usr/bin/env sage` instead of `#!/usr/bin/env sage --python`. Of course, if you're explicitly saying `sage --python runDispK1.py`, then it doesn't matter what the first line is. What if you do `sage runDispK1.py`?

John Palmieri gravatar imageJohn Palmieri ( 2013-01-24 10:39:21 +0100 )edit

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: 2013-01-22 11:36:49 +0100

Seen: 2,523 times

Last updated: Jan 22 '13