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, 06 Jul 2019 17:27:55 +0200Numerical integration and plot failinghttps://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/I have a function which can be efficiently calculated on some interval on the real line
and if I feed Sage with concrete arguments, Sage gives concrete numerical values.
Nevertheless, if I try to integrate it numerically (or plot it), something goes terribly wrong
and Sage is not able to calculate the values.
In my case I get the error message
negative number cannot be raised to a fractional power
The function itself is a solution to the cubic equation so it involves square root and a cubic root.
is it some bug in sage and if there is some workaround?
MINIMAL NON_WORKING EXAMPLE
y = var('y')
z = var('z');
cauchy = y.substitute(solve(z*y^3 +y^2 - 2*z*y+2, y)[0])
def F(u):
return arg(cauchy).substitute(z=u).n()
numerical_integral(F, 1, 2)Thu, 06 Jun 2019 09:30:11 +0200https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/Comment by Emmanuel Charpentier for <p>I have a function which can be efficiently calculated on some interval on the real line
and if I feed Sage with concrete arguments, Sage gives concrete numerical values.
Nevertheless, if I try to integrate it numerically (or plot it), something goes terribly wrong
and Sage is not able to calculate the values.</p>
<p>In my case I get the error message </p>
<pre><code>negative number cannot be raised to a fractional power
</code></pre>
<p>The function itself is a solution to the cubic equation so it involves square root and a cubic root.</p>
<p>is it some bug in sage and if there is some workaround?</p>
<p>MINIMAL NON_WORKING EXAMPLE</p>
<pre><code>y = var('y')
z = var('z');
cauchy = y.substitute(solve(z*y^3 +y^2 - 2*z*y+2, y)[0])
def F(u):
return arg(cauchy).substitute(z=u).n()
numerical_integral(F, 1, 2)
</code></pre>
https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/?comment=46825#post-id-46825Indeed :
sage: var("y,z")
(y, z)
sage: cauchy(z)=solve(z*y^3 +y^2 - 2*z*y+2 ,y)[0].rhs()
sage: def foo(u):return(arg(cauchy(u)).n())
sage: [foo(t) for t in (1,1.1..2)]
[0.828222717321238,
## Snip...
0.377665318352514]
Therefore, this function *can* be evaluated. But :
sage: plot(foo,(1,2))
verbose 0 (3635: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 200 points.
verbose 0 (3635: plot.py, generate_plot_points) Last error message: 'negative number cannot be raised to a fractional power'
Launched png viewer for Graphics object consisting of 0 graphics primitives
... not in `plot`. Trying to plot symbolically yelds :
TypeError: Cannot evaluate symbolic expression to a numeric value.
A bug, IMHOThu, 06 Jun 2019 10:44:05 +0200https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/?comment=46825#post-id-46825Answer by Emmanuel Charpentier for <p>I have a function which can be efficiently calculated on some interval on the real line
and if I feed Sage with concrete arguments, Sage gives concrete numerical values.
Nevertheless, if I try to integrate it numerically (or plot it), something goes terribly wrong
and Sage is not able to calculate the values.</p>
<p>In my case I get the error message </p>
<pre><code>negative number cannot be raised to a fractional power
</code></pre>
<p>The function itself is a solution to the cubic equation so it involves square root and a cubic root.</p>
<p>is it some bug in sage and if there is some workaround?</p>
<p>MINIMAL NON_WORKING EXAMPLE</p>
<pre><code>y = var('y')
z = var('z');
cauchy = y.substitute(solve(z*y^3 +y^2 - 2*z*y+2, y)[0])
def F(u):
return arg(cauchy).substitute(z=u).n()
numerical_integral(F, 1, 2)
</code></pre>
https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/?answer=47086#post-id-47086**NOTE :** This has *replaced*, (not complemented) my original answer concluding that this was a (Python) bug. Apologies for the confusion...
As pointed out by Serge Lelièvre, this seems a limitation of Python 2 floats, which does not exist in Python 3, where one can do :
sage: def F(u): return arg((u-3)^(1/3))
sage: F(1)
arg((-2)^(1/3))
sage: F(1).n()
1.04719755119660
sage: F(2).n()
1.04719755119660
sage: plot(F,(1,2))
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: integrate(F(x),x,1,2)
1/3*pi
See this sage-devel [thread](https://groups.google.com/forum/#!topic/sage-devel/94QnlUyALwU) for further illumination...
In the original example:
sage: cauchy(z)=y.substitute(solve(z*y^3 +y^2 - 2*z*y+2, y)[0])
sage: def G(u): return arg(cauchy(u))
sage: numerical_integral(lambda u:G(u), 1, 2)
(0.6116545934235456, 6.790730127365591e-15)Sat, 06 Jul 2019 17:27:55 +0200https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/?answer=47086#post-id-47086Answer by slelievre for <p>I have a function which can be efficiently calculated on some interval on the real line
and if I feed Sage with concrete arguments, Sage gives concrete numerical values.
Nevertheless, if I try to integrate it numerically (or plot it), something goes terribly wrong
and Sage is not able to calculate the values.</p>
<p>In my case I get the error message </p>
<pre><code>negative number cannot be raised to a fractional power
</code></pre>
<p>The function itself is a solution to the cubic equation so it involves square root and a cubic root.</p>
<p>is it some bug in sage and if there is some workaround?</p>
<p>MINIMAL NON_WORKING EXAMPLE</p>
<pre><code>y = var('y')
z = var('z');
cauchy = y.substitute(solve(z*y^3 +y^2 - 2*z*y+2, y)[0])
def F(u):
return arg(cauchy).substitute(z=u).n()
numerical_integral(F, 1, 2)
</code></pre>
https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/?answer=46827#post-id-46827This works for me in (Python 3 based) Sage 8.8.beta7:
sage: y, z = var('y z')
sage: cauchy = y.substitute(solve(z*y^3 + y^2 - 2*z*y + 2, y)[0])
sage: def F(u):
....: return arg(cauchy).substitute(z=u).n()
....:
sage: numerical_integral(F, 1, 2)
(0.6116545934235456, 6.790730127365591e-15)
Here are links to Jupyter notebooks on CoCalc showing
- [failure to integrate F with Py2-based SageMath 8.7](https://cocalc.com/share/fc94de41-6af4-4ee1-a684-c33a9e56b2b0/real-cube-root.ipynb?viewer=share)
- [success integrating F with Py3-based SageMath 8.8.beta7](https://cocalc.com/share/fc94de41-6af4-4ee1-a684-c33a9e56b2b0/real-cube-root.sage3.ipynb?viewer=share)
This also fails with Py2-based SageMath 8.8.beta7.
So the "easy" solution for you is: switch to Python3-based Sage!
For this, follow the [instructions to install from source](https://github.com/sagemath/sage/blob/master/README.md#more-detailed-instructions-to-build-from-source),
paying attention to the discussion about Python 3.
Or if you want to turn your existing SageMath into a Python3-based one,
you could open a terminal, cd to your Sage directory, and type
make distclean
make configure
./configure --with-python=3
make
which will work if you have all the prerequisites.Thu, 06 Jun 2019 11:18:50 +0200https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/?answer=46827#post-id-46827Answer by paulmasson for <p>I have a function which can be efficiently calculated on some interval on the real line
and if I feed Sage with concrete arguments, Sage gives concrete numerical values.
Nevertheless, if I try to integrate it numerically (or plot it), something goes terribly wrong
and Sage is not able to calculate the values.</p>
<p>In my case I get the error message </p>
<pre><code>negative number cannot be raised to a fractional power
</code></pre>
<p>The function itself is a solution to the cubic equation so it involves square root and a cubic root.</p>
<p>is it some bug in sage and if there is some workaround?</p>
<p>MINIMAL NON_WORKING EXAMPLE</p>
<pre><code>y = var('y')
z = var('z');
cauchy = y.substitute(solve(z*y^3 +y^2 - 2*z*y+2, y)[0])
def F(u):
return arg(cauchy).substitute(z=u).n()
numerical_integral(F, 1, 2)
</code></pre>
https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/?answer=47023#post-id-47023Here’s a workaround for plotting:
list_plot( [[t, F(t)] for t in (1,1.1..2)], plotjoined=True )
with a live [example](https://sagecell.sagemath.org/?z=eJxNjEEOgjAURPdNeoe_ox9qI3WnYesJ3BEwCEUxhJryS9KeXtCYOLs3mXkBClgaJ5KQIGfxRzHBE2ectY1vH2Ftg5r9baaBPBkx23ExIqahPkAWag070OmKmZYQsNxXuH0708NZeDxyBmucIe8maNxdfK34r4yFRzWJz3EcZrq-RksCypLkKiGsoLcOCIYJRC5zlSulsZKwzZ52mExXXJw3gG-PfT0U&lang=sage&interacts=eJyLjgUAARUAuQ==). For integration you could likewise evaluate the function at discrete points, add all interior evaluations plus half of the values at the endpoints and multiply by the step size (i.e., a discrete integral).Mon, 01 Jul 2019 03:06:30 +0200https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/?answer=47023#post-id-47023Answer by Juanjo for <p>I have a function which can be efficiently calculated on some interval on the real line
and if I feed Sage with concrete arguments, Sage gives concrete numerical values.
Nevertheless, if I try to integrate it numerically (or plot it), something goes terribly wrong
and Sage is not able to calculate the values.</p>
<p>In my case I get the error message </p>
<pre><code>negative number cannot be raised to a fractional power
</code></pre>
<p>The function itself is a solution to the cubic equation so it involves square root and a cubic root.</p>
<p>is it some bug in sage and if there is some workaround?</p>
<p>MINIMAL NON_WORKING EXAMPLE</p>
<pre><code>y = var('y')
z = var('z');
cauchy = y.substitute(solve(z*y^3 +y^2 - 2*z*y+2, y)[0])
def F(u):
return arg(cauchy).substitute(z=u).n()
numerical_integral(F, 1, 2)
</code></pre>
https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/?answer=47027#post-id-47027A different approach that works even in a Python 2 based Sage:
sage: var("y,z");
sage: cauchy(z) = y.subs(solve(z*y^3 + y^2 - 2*z*y + 2, y)[0])
sage: F(z) = arctan2(imag(cauchy(z)),real(cauchy(z)))
sage: numerical_integral(F, 1, 2)
(0.6116545934235456, 6.790730127365591e-15)
sage: plot(F(z),(z,1,2))
![image description](/upfiles/15620240288320518.png)
The plot is really slow. It may take more than 20 seconds. See this [SageMath cell](https://sagecell.sagemath.org/?z=eJxNjEEKwjAQAO-Cf1h62tRVbDx77SfEyhpCDaSpbJpC8npTBPE4MDMrCzaZSqP2O8PJvDIWBVfIp5ieEePsV4ulzcMFDpAHDUfQbeVKmiCr2_ley_4bsZiFg0Y38Yi_myKx7P-4Bm9xYYGQJivOsH9UsqNUqyfoCPSm-HnBbUxYqCOt1AdJOzLx&lang=sage&interacts=eJyLjgUAARUAuQ==).Tue, 02 Jul 2019 01:36:27 +0200https://ask.sagemath.org/question/46819/numerical-integration-and-plot-failing/?answer=47027#post-id-47027