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.Thu, 24 Jan 2013 10:39:21 +0100running sage from a Python scripthttps://ask.sagemath.org/question/9733/running-sage-from-a-python-script/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
Tue, 22 Jan 2013 11:36:49 +0100https://ask.sagemath.org/question/9733/running-sage-from-a-python-script/Answer by ppurka for <p>I'm using sage 5.5 on ubuntu 12.04. I'm trying to run sage via the following script</p>
<pre><code>#!/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)
</code></pre>
<p>I run this from the command line with</p>
<blockquote>
<p>sage -python runDispK1.py</p>
</blockquote>
<p>and I get the following error</p>
<pre><code>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
</code></pre>
<p>Whereas if I run the exact same code within sage, e.g. I have a file called run.sage with this text in it:</p>
<pre><code> 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)
</code></pre>
<p>then sage gives me the correct answer</p>
<pre><code>sage: load("run.sage")
0.550279773862
</code></pre>
<p>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?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/9733/running-sage-from-a-python-script/?answer=14488#post-id-14488Why 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`.Tue, 22 Jan 2013 13:08:49 +0100https://ask.sagemath.org/question/9733/running-sage-from-a-python-script/?answer=14488#post-id-14488Comment by John Palmieri for <p>Why don't you run it as <code>sage</code> instead of trying to run it as a python file? Simply use</p>
<pre><code>#!/usr/bin/env sage
</code></pre>
<p>Also, note that the <code>#!</code> you used would try to run a command called <code>sage -python</code> instead of running the command <code>sage</code> with the argument <code>-python</code>.</p>
https://ask.sagemath.org/question/9733/running-sage-from-a-python-script/?comment=18348#post-id-18348That 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`?Thu, 24 Jan 2013 10:39:21 +0100https://ask.sagemath.org/question/9733/running-sage-from-a-python-script/?comment=18348#post-id-18348