Ask Your Question

plotting prime_pi

asked 2010-09-07 11:32:48 +0200

finotti gravatar image

Maybe I am missing something really simple, but shouldn't one be able to plot prime_pi?

| Sage Version 4.5.1, Release Date: 2010-07-19                       |
| Type notebook() for the GUI, and license() for information.        |
sage: plot(prime_pi(x),3,10)
TypeError                                 Traceback (most recent call last)

/home/dept/finotti/<ipython console=""> in <module>()

/scratch/local/sage-4.5.1/local/lib/python2.6/site-packages/sage/functions/ in sage.functions.prime_pi.PrimePi.__call__ (sage/functions/prime_pi.c:1064)()

/scratch/local/sage-4.5.1/local/lib/python2.6/site-packages/sage/symbolic/ in sage.symbolic.expression.Expression.__int__ (sage/symbolic/expression.cpp:4171)()

/scratch/local/sage-4.5.1/local/lib/python2.6/site-packages/sage/symbolic/ in sage.symbolic.expression.Expression.n (sage/symbolic/expression.cpp:17042)()

TypeError: cannot evaluate symbolic expression numerically

It seems wot work fine with real input

sage: prime_pi(pi)
_3 = 2
sage: prime_pi(1020.312788)
_4 = 171



edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2010-09-07 11:41:24 +0200

kcrisman gravatar image

Essentially, prime_pi has its own plotting method, so you need to do

sage: plot(prime_pi, 50,100)

You can see this at

sage: prime_pi?

However, you are right that it should play nicely with other plotting syntax, which is why we have the ticket here tracking this issue.

edit flag offensive delete link more

answered 2010-09-07 11:42:18 +0200

ccanonc gravatar image

updated 2010-09-07 11:46:21 +0200

list_plot([(x,prime_pi(x)) for x in xrange(3,10+1)])

edit flag offensive delete link more

answered 2010-09-07 11:40:29 +0200

niles gravatar image

updated 2010-09-07 11:42:39 +0200

Yes, you should be able to plot prime_pi, using just the code you tried! Unfortunately it seems that plot can not always figure out what to evaluate, so you have to help it a little, using a lambda function:

sage: plot(lambda x: prime_pi(x),3,10)

or, equivalently,

sage: f = lambda x: prime_pi(x)
sage: plot(f,3,10)

There have been a number of similar problems with similar solutions mentioned on this site . . . maybe eventually someone will extend plot so that the code you tried first will work. At least in the meantime someone could change the error message to suggest trying lambda functions . . .

Also, could you tell us where you read about the plot function? It could be very useful to include an example like this somewhere that people will find it :)

edit flag offensive delete link more


In this case, no lambda needed - see below. Niles, maybe you want to fix this ticket? :)

kcrisman gravatar imagekcrisman ( 2010-09-07 11:42:03 +0200 )edit

ha! I certainly *want* to . . .

niles gravatar imageniles ( 2010-09-07 11:45:25 +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

1 follower


Asked: 2010-09-07 11:32:48 +0200

Seen: 673 times

Last updated: Sep 07 '10