Differential equations

Mark Rowe bdash at gmx.net
Wed Dec 11 20:02:13 EST 2002


On Thursday, Dec 12, 2002, at 13:29 Pacific/Auckland, Greg Ewing wrote:
> If you're willing to write your expression in a form
> such as
>
>   Sum(Pow(Prod(2,"x"),3),Prod(3,"x"),-10)
>
> you could delegate the parsing problem to Python
> by defining suitable classes Sum, Prod, etc.
>
> Taking this a step further, with suitably clever
> __add__ etc. methods you could probably reduce this
> to something like
>
>   x = Var("x")
>   y = 2*x^3+3*x-10
>
>
> Then it's simply a matter of giving each class an
>
> appropriate differentiate() method. :-)

The Simpy package appears to have this functionality almost exactly as  
you describe it.

An example from Simpy.Expressions.Symbolic  
(http://www.imm.dtu.dk/~mzp/simpy/pydoc/ 
Simpy.Expressions.Symbolic.html):
 >>> x=Variable()
 >>> expr=x**2+sin(x+2)
 >>> print expr
x**2+sin(x+2)
 >>> print expr.diff(x)
2*x+cos(x+2)
 >>> v={x:4.5}
 >>> expr.diff(x).eval(v)
9.9765876257280226
 >>>

http://www.imm.dtu.dk/~mzp/simpy/simpy.html

Mark





More information about the Python-list mailing list