Convert sympy expression to standard python

1,720 views
Skip to first unread message

Alan Bromborsky

unread,
May 7, 2012, 9:14:12 AM5/7/12
to sympy
What is the simplest way of converting a sympy expression (formula) to a
standard python expression for the purpose of evaluation using tools
such as numpy. That is convert all the sympy variables and functions to
python variables and functions similar to fcode and ccode, but for
python and doing it within the program as opposed to writing to a file
(printing).

krastano...@gmail.com

unread,
May 7, 2012, 9:39:14 AM5/7/12
to sy...@googlegroups.com
Check lambdify.

I have much to say *against* the way it is written and how it acts on
corner cases, but for simple stuff it works well.

It just prints the expressions to a string and uses eval() on that
string. It can "compile" to python.math, numpy and mpmath.
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.
>

krastano...@gmail.com

unread,
May 7, 2012, 9:41:00 AM5/7/12
to sy...@googlegroups.com
In [1]: f = lambdify([x,y], sin(x)*y)

In [2]: f
Out[2]: <function numpy.<lambda>>

In [3]: f(3.1415/2, 2)
Out[3]: 1.999999997853828
Reply all
Reply to author
Forward
0 new messages