Ask Your Question

Numerical integration and plot failing

asked 2019-06-06 02:30:11 -0500

psniady gravatar image

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?


y = var('y'); z = var('z');

cauchy= y.substitute(solve(zy^3 +y^2 - 2z*y+2 ,y)[0])

def F(u): return arg(cauchy).substitute(z=u).n()


edit retag flag offensive close merge delete


Indeed :

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)]
## Snip...

Therefore, this function can be evaluated. But :

sage: plot(foo,(1,2))
verbose 0 (3635:, generate_plot_points) WARNING: When plotting, failed to evaluate function at 200 points.
verbose 0 (3635:, 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, IMHO

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2019-06-06 03:44:05 -0500 )edit

To display blocks of code or error messages, skip a line above and below, and do one of the following (all give the same result):

  • indent all code lines with 4 spaces
  • select all code lines and click the "code" button (the icon with '101 010')
  • select all code lines and hit ctrl-K

For instance, typing

If we define `f` by

    def f(x, y, z):
        return x * y * z

then `f(2, 3, 5)` returns `30` but `f(2*3*5)` gives:

    TypeError: f() takes exactly 3 arguments (1 given)


If we define f by

def f(x, y, z):
    return x * y * z

then f(2, 3, 5) returns 30 but f(2*3*5) gives:

TypeError: f() takes exactly 3 arguments (1 given)

Please edit your question to do that.

slelievre gravatar imageslelievre ( 2019-06-06 04:19:23 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-06-06 04:18:50 -0500

slelievre gravatar image

updated 2019-06-06 14:04:11 -0500

This 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

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

which will work if you have all the prerequisites.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2019-06-06 02:30:11 -0500

Seen: 57 times

Last updated: Jun 06