Ask Your Question

Fast numerical plot command that always works?

asked 2011-10-17 14:56:26 +0200

jk gravatar image

updated 2011-10-18 19:40:24 +0200

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:, generate_plot_points) WARNING: When plotting,
failed to evaluate function at 200 points.
verbose 0 (4101:, 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 flag offensive 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 gravatar imageJason Grout ( 2011-10-17 23:13:12 +0200 )edit

@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.

jk gravatar imagejk ( 2011-10-18 19:44:36 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2011-10-19 05:38:13 +0200

Jason Grout gravatar image

I would probably do it like this:

C = -I*sqrt(pi)*x*e^(-1/4*x^2)
plot(f, (-10,10))
edit flag offensive delete link more


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

jk gravatar imagejk ( 2011-10-20 12:12:07 +0200 )edit

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.

Jason Grout gravatar imageJason Grout ( 2011-10-22 04:18:47 +0200 )edit

Your Answer

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

Add Answer

Question Tools


Asked: 2011-10-17 14:56:26 +0200

Seen: 1,094 times

Last updated: Oct 19 '11