Code generation has been coming up in the last few days and I have been
dealing with it myself as well.  For this purpose, I created a function for
PyDy called 'generate_function'.  You can see it if you go to my github, in
the bicycle_work branch:

git://github.com/hazelnusse/pydy.git

And go to pydy.py:2936 to see the function.

It is a little bit custom to PyDy because I rely on my customized
StrPrinter, but maybe some of the ideas in it could be useful to others.  It
is also geared towards lists of equations which have trigonometric terms
(sin,cos, tan) in them, so I do some parsing to identify all the trig terms
and compute them only once.  The basic idea is to specify:
1)  a string name --- this will become the name of the Python function
2)  a python list of Sympy Relational (lhs==rhs) objects
3)  the function arguments  (quantities needed to evaluate the rhs of the
equations
4)  optional function parameters
5)  optional docstring
6)  optional nested terms -- repeated things that show up in numerous places
throughout the equations
7)  optional time boolean argument to create a function signature of f(x, t,
params)
8)  optional triples boolean argument to return a list of length 3 lists
rather than one big list, useful for 3d vector stuff

It returns a string which is valid python code and defines a function which
can be either used with exec() or written to file and then used later as a
library.  The function returns a list of the left hand sides of the items in
the list of Relational objects.

I have been using it to create functions that work well with scipy's odeint
and fsolve and it has worked very well.

To see it in action, run the rollingdisc.py script in the examples
directory.  It will print out the generated string expression.

~Luke

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com
To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to