code generation and simplification

46 views
Skip to first unread message

Matthew Emmett

unread,
Jul 26, 2012, 6:43:05 PM7/26/12
to sy...@googlegroups.com
Hi everyone,

I have some sympy code that generates Fortran code, and was wondering
if there a good way of simplifying the sympy expressions in order to
minimize the number of floating point operations that are required to
evaluate them numerically. Any suggestions?

Thanks,
Matt

Aaron Meurer

unread,
Jul 26, 2012, 7:19:39 PM7/26/12
to sy...@googlegroups.com
One thing you can try is to use the cse() function to group common
subexpressions so that they are only evaluated once.

Aaron Meurer
> --
> 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.
>

Matthew Rocklin

unread,
Jul 26, 2012, 7:32:14 PM7/26/12
to sy...@googlegroups.com
SymPy may be able to help by counting operations in a given expression. I don't think you want it controlling the optimization search though.

I'm not very familiar with the relevant modules but I don't think it will be easy to perform this optimization within SymPy. To the best of my knowledge It is not easy to separate out the simplification rules and choose whether or not to apply each of them.

If you haven't already I recommend asking a non-sympy-specific version of this question on scicomp.stackexchange.com 

Matthew Emmett

unread,
Jul 27, 2012, 7:32:19 AM7/27/12
to sy...@googlegroups.com
Thanks Aaron and Matthew, I will take a look at the cse routine and
consider asking a general version of this on the scicomp site.

Matt

Vinzent Steinberg

unread,
Jul 28, 2012, 9:31:10 AM7/28/12
to sy...@googlegroups.com
There is Ignition [1], which uses Sympy for optimizing numerical expressions. See for example Andy's paper [2].

Vinzent


Matthew Rocklin

unread,
Jul 28, 2012, 9:39:42 AM7/28/12
to sy...@googlegroups.com
There is also the Theano project which is all about code generation on array expressions. They've optimized a lot of this stuff much more than SymPy has. It's a bit harder to get out a single code snippet with them though. They generate working C and CUDA code and then wrap it into python functions under the hood. 

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sympy/-/k_YvrA8-OVAJ.

Julien Rioux

unread,
Aug 9, 2012, 2:57:49 PM8/9/12
to sy...@googlegroups.com

If you're working with polynomials, there is a function that returns the Horner form in the polys module, reducing the number of operations.

Cheers,
Julien
Reply all
Reply to author
Forward
0 new messages