[Edu-sig] More Pythonic Precalculus (Python 2.0)
David Scherer
dscherer@cmu.edu
Mon, 25 Sep 2000 15:18:31 -0400
> In any case, the challenge before me was to accept algebraic
> expressions as strings, for whatever reasons.
How about just
>>> f = "2 * x**2 - 10*x + 2"
>>> eval(f, {'x' : 5})
2
or
def graph(f):
for x in range(5):
print x, eval(f)
or, if you have Numerical Python
from Numeric import *
x = arange(0,10,1.0)
y = eval("2 * x**2 - 10*x + 2")
or, if you happen to have VPython handy
from visual import *
x = arange(-10,10,1.0)
f = "2 * x**2 - 10*x + 2"
gcurve( x=x, y=eval(f) )
Dave