# Fast numerical plot command that always works?

I typically encounter expressions of the type

 C = -I*sqrt(pi)*x*e^(-1/4*x^2)
Abs = abs(C)


Sage's built-in plot commands fails:

verbose 0 (4101: plot.py, generate_plot_points) WARNING: When plotting,
failed to evaluate function at 200 points.
verbose 0 (4101: plot.py, generate_plot_points) Last error message:
'unable to simplify to float approximation'


We can evaluate the expression numerically:

X = map(lambda x: x/10, range(-100,100))
Y = map(lambda xx: Abs.subs(x=xx).n(), X)
Yreal = map(real, Y)
Yimag = map(imag, Y)
list_plot(zip(X,Yreal)) + list_plot(zip(X,Yimag),color='red')


This works, but looks like a dirty workaround. Should I use numpy arrays and fast_callable? Or use fast_callable() without numpy?

Here is an example worksheet.

edit retag close merge delete

Can you give a complete example (e.g., give us a C function) illustrating the problem? I'm sure we'll be able to help you better then. LIkely, using fast_callable will solve your issue.

@Jason Grout, I update the question with an example and a link to a public worksheet. In this simple example, simplify() would solve the issue, but on more sophisticated expressions simplify will fail.

Sort by » oldest newest most voted

I would probably do it like this:

C = -I*sqrt(pi)*x*e^(-1/4*x^2)
f=fast_callable(abs(C),vars=[x],domain=CC)
plot(f, (-10,10))

more

Thank You @Jason Grout. This methods works, but only if domain=CC is specified explicitly. Mind to explain?

domain=CC means that the problem is evaluated and at each step of the evaluation, the number is converted to the complex numbers (CC). That lets you do the calculation using I. You can specify other domains like RR (real numbers), RDF (machine real numbers), etc. I think the original problem is that the default is to do fast_callable with domain RDF, which means that the intermediate steps have to be real numbers.