1

I have some big expressions which have their symbols with the attribute is_commutative=False.

Here is an example:

import sympy
from sympy import pi, sin, cos, exp
sympy.var('L, xPL, cosa, i, j, r2, sina, t, x', commutative=False)
sin.is_commutative = False
cos.is_commutative = False
exp.is_commutative = False


f = L*(r2 + sina*x)**(-1)*cosa*x*exp(10000.0*x*xPL*(2*i + 1 + i**2)**(-1)/L**2 \
    - 5000.0*xPL**2*(2*i + 1 + i**2)**(-1)/L**2 - 5000.0*x**2*(2*i + 1 \
    + i**2)**(-1)/L**2)*cos(pi*j*t - j*t**2/2 + pi*t - t**2/2) \
    - (r2 + sina*x)**(-1)*cosa*x**2*exp(10000.0*x*xPL*(2*i + 1 \
    + i**2)**(-1)/L**2 - 5000.0*xPL**2*(2*i + 1 + i**2)**(-1)/L**2 \
    - 5000.0*x**2*(2*i + 1 + i**2)**(-1)/L**2)*cos(pi*j*t - j*t**2/2 + pi*t - t**2/2)

If I try to do f.simplify() it raises an error:

RuntimeError: maximum recursion depth exceeded.

I already tried the "gotcha" sys.setrecursionlimit, but in this case it doesn't help.

What helps is to set commutative=True in sympy.var. (Without need to set it True to sin, cos and exp)

Since these expressions come from a previous process, I have the following workaroud:

def get_new_f(f):
    sin.is_commutative = True
    cos.is_commutative = True
    exp.is_commutative = True
    str_f = str(f)
    for s in f.free_symbols:
        sympy.var(str(x))
    return eval(str_f)r

Then get_new_f(f).simplify() works!

Is there another way to overcome this error?

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
  • I had a similar problem with recursion depth, [asked a question about recursion here once](http://stackoverflow.com/questions/17526625/baktracking-function-which-calculates-change-exceeds-maximum-recursion-depth)and the only answer I've got was "because of the semantics of Python as a language, recursion isn't a particularly efficient paradigm" and a reference to [this question](http://stackoverflow.com/questions/3323001/maximum-recursion-depth) – Pawel Miech Aug 15 '13 at 13:32
  • Thank you for the comment! the developers from SymPy says that recursion is a common practice for symbolic evaluation, but for this case I believe the problem is not the recursion, but the fact that the symbols are not `commutative`... – Saullo G. P. Castro Aug 15 '13 at 13:35
  • I didn't notice that you're setting is_commutative on sin, cos, and exp. That isn't supported. – asmeurer Aug 17 '13 at 06:10
  • This error also happens when I don't do `.is_commutative=False` for `sin, cos, exp`... – Saullo G. P. Castro Aug 17 '13 at 06:17
  • @asmeurer despite it is not supported, setting `.is_commutative=False` for `sin, cos, exp` works very well to keep the order prior to the differentiation... – Saullo G. P. Castro Aug 17 '13 at 06:20
  • It shouldn't even be needed. These functions automatically inherit the commutativity of their arguments. – asmeurer Aug 17 '13 at 22:54

1 Answers1

2

With such a small expression, a recursion error like this most likely indicates a bug in SymPy. You should report this as a bug at the SymPy issue tracker.

asmeurer
  • 86,894
  • 26
  • 169
  • 240