[SciPy-user] symbolic strings
Robert Clewley
rclewley at cam.cornell.edu
Fri Feb 3 13:19:10 EST 2006
Hi Ryan,
Recently I threw together some fairly elementary symbolic expression
classes in python, mainly for my own research use, from which I think you
might be able to glean something useful. These classes do not permit
assignments in the way you mention, although that would be a simple
extension. Also, my code does not enable symbolic matrices, although
symbolically defined functions are OK. Otherwise, I think they might help
you do what you need.
Constructing terms is easy. You can use the constructor method on my
symbolic classes, but there are also capitalized symbolic objects in-built
for the elementary math functions ('Sin', 'Exp', etc.) which can be called
with a float, a string, or another symbolic object. These calls return
another symbolic object. I've overridden all of the necessary arithmetic
methods on these objects (I think!). There are several other useful
goodies associated with these objects...
There are no "simplification" tools beyond what the python parser provides
for braces using eval(), etc, but it is easy to substitute
sub-expressions, floats for symbols, etc. My code does not involve a real
parser of its own, and is far from being optimized for efficiency when
dealing with massive expressions.
With these caveats out of the way, please feel free to check out the
classes QuantSpec and Quantity in ModelSpec.py. This file is part of our
PyDSTool package at http://pydstool.sourceforge.net although it can be
used standalone. (FYI, ModelSpec.py provides other tools for building
dynamical systems models from modular "components" made up of symbolic
expressions.) The package is provided with a BSD license. The URL takes
you to our wiki: if you click on Project Overview you'll see links to the
page Symbolic, if you want to read some documentation. If you
click on Code Topics on the side-bar you'll be able to download the code.
ModelSpec.py can also be run as a script, which will show off some of its
features.
Let me know if this is of any use or you have any suggestions.
I'd like to make this prototype code more useful for other people (and
eventually, efficient :).
-Rob
On Fri, 3 Feb 2006, Ryan Krauss wrote:
> I may be getting in over my head, but I think this can work. I do a
> lot of analysis with matrices using the transfer matrix method.
> Sometimes I want to do numeric work with them and sometimes I use
> python to generate an input script for Maxima. Right now I have two
> methods for my transfer matrix element, one that outputs a numeric
> matrix and one that outputs a string for Maxima. I am working on a
> new element and would like to combined these two expressions by
> creating a symbolic string method so that expressions like
> a=-(L0*m1*r1*s**2-c1*s-k1)/(c1*s+k1)
>
> make sense whether L0, m1, r1, s, etc are floats or symstrs (my new
> class). I overrode the __mul__, __add__, __div__,__sub__, and __pow__
> methods but don't know if there are methods to override to deal with
> the parenthesis and the unary - out in front. Can this be done with
> only a little bit more work? Basically I want the output of this
> expression to be 'a=-(L0*m1*r1*s**2-c1*s-k1)/(c1*s+k1)' if it is a
> string and a float or complex if the variables are numbers.
>
> Thanks,
>
> Ryan
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.net
> http://www.scipy.net/mailman/listinfo/scipy-user
>
-----------------------------------
Rob Clewley
Research Associate
Department of Mathematics
and Center for Applied Mathematics
Cornell University
Ithaca, NY 14853
www.cam.cornell.edu/~rclewley
Tel: 607-255-7760
Fax: 607-255-9860
More information about the SciPy-User
mailing list