Lambdify performance with repeated subexpressions

40 views
Skip to first unread message

Albert Pető

unread,
Oct 14, 2016, 9:09:25 AM10/14/16
to sympy
Hi,

I would like to evaluate certain functions created with lambdify. Those functions would come from rotations and would have a lot of trigonometric function invocations in them with the same parameters. For example, lets suppose that it would contain cos(a) many times. I plan to heavily use those generated functions, but I am concerned about the performance loss of evaluating cos(a) multiple times in one function invocation, when it would suffice to evaluate it only once.

I am not sure how lambdify treats these cases, i.e. if it would call cos(a) multiple times or not, or if I have to specify it explicitly. Can somebody tell me how it works?

Thanks for your help! :)

Albert Pető

unread,
Oct 14, 2016, 9:09:46 AM10/14/16
to sympy
Hi, I plan to heavily use a function generated with lambdify from a sympy expression which has repeated occurences of some subexpressions. Specifically it will have a lot of cosine and sine expressions with the same arguments. I don't know if lambdify can notice this pattern and evaluate that subexpression only once, or if it will call it (the numeric sine and cosine functions) as many times as it appears, or if I have to specify it somehow. I am afraid that it would mean a considerable performance loss if, for example, it will have to call 2-3 times as many trigonometric functions as a hand-written code, since I plan to use it a lot. Can somebody provide me some information?

Thanks in advance :)

Björn Dahlgren

unread,
Oct 14, 2016, 11:06:17 AM10/14/16
to sympy


On Friday, 14 October 2016 15:09:46 UTC+2, Albert Pető wrote:
Hi, I plan to heavily use a function generated with lambdify from a sympy expression which has repeated occurences of some subexpressions.

This functionality is available in symengine: https://github.com/symengine/symengine.py/blob/master/symengine/lib/symengine_wrapper.pyx#L2827
(which you might want to consider if you are concerned about speed). If you want to stay pure python and only use sympy you can look at the
source code there for how to achieve this (we are creating a closure).

Best,
Björn

Aaron Meurer

unread,
Oct 14, 2016, 11:29:23 AM10/14/16
to sy...@googlegroups.com
To achieve this with lambdify you should call cse() first, then
lambdify each expression separately. We ought to build a wrapper to
make this easier (or a cse=True flag to lambdify).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/45c5d1d6-b5ea-458d-92f4-096cd4b51b8d%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages