Ask Your Question

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

asked 2012-02-01 17:09:50 +0200

Dmitry Semikin gravatar image

updated 2012-02-01 17:12:58 +0200


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[0].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:, generate_plot_points) WARNING: When plotting,
failed to evaluate function at 39 points.
verbose 0 (4190:, 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.

I've already solved my practical issue with fast_callable(), but I'm still curious about this behavior.


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2012-02-01 18:43:30 +0200

DSM gravatar image

updated 2012-02-01 18:44:39 +0200

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, so maybe a few more wouldn't be a bad idea.

edit flag offensive delete link 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?

kcrisman gravatar imagekcrisman ( 2012-02-01 23:10:02 +0200 )edit

answered 2012-02-01 22:45:15 +0200

Jason Grout gravatar image


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/ 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)

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

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: 2012-02-01 17:09:50 +0200

Seen: 582 times

Last updated: Feb 01 '12