# generate_plot_points fails to evaluate points, while plot imaginary part of expression Hello,

I solve cubic equation and try to plot imaginary part of its solutions (to find out, at which intervals roots are purely real).

The code is something like this

var('a, b, P, E')

P_sols = solve(a*P + b*P**3 - E, P)

P1 = lambda E_: P_sols.rhs().subs({a:-5, b:5, E:E_}).n().imag()
plot(P1, [-10, 10])


Plot function (actually, generate_plot_command) fails to evaluate at points, where image part is nearly zero (range approximately between -2 and 2).

verbose 0 (4190: plot.py, generate_plot_points) WARNING: When plotting,
failed to evaluate function at 39 points.
verbose 0 (4190: plot.py, generate_plot_points) Last error message:
'negative number cannot be raised to a fractional power'


But if I try to evaluate this function directly

P1(0)  # OK
P1(float(0))  # OK


it works OK.

I've found on ask-sage (here), that to create callables it is better to use fast_callable, but I still wonder, why I can call function directly, but plot() function cannot. I've looked into code of generate_plot_points and find there only call line (x, f(x)), so I cannot understand, why it do not work.

Thanks.

edit retag close merge delete

Sort by » oldest newest most voted While it's true that P1(float(0)) works, P1(float(-1)) doesn't:

sage: P1(float(-1))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ValueError: negative number cannot be raised to a fractional power


I'm not sure what the best way to avoid this problem would be. Probably we could get away with coercing the function argument to RDF instead of float before it's called-- making that one change in generate_plot_points got the function to plot without difficulty. It's already done in lots of places in plot.py, so maybe a few more wouldn't be a bad idea.

more

It would be worth opening a ticket to at least try timings. If it doesn't slow anything down... so did it not get turned into a fast_callable because it was a lambda?

Interesting:

sage: float(-1)^(1/3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/Users/grout/projects/HLA2/<ipython console> in <module>()

/Users/grout/sage-trees/sage-5.0.beta1/local/lib/python2.7/site-packages/sage/rings/rational.so in sage.rings.rational.Rational.__pow__ (sage/rings/rational.c:17026)()

ValueError: negative number cannot be raised to a fractional power
sage: RDF(-1)^(1/3)
NaN


In other words, having an RDF just doesn't give the error.

more