Comment #3 on issue 3312 by laurence.gonsalves: "RuntimeError: maximum recursion depth exceeded" in sympy.cse
http://code.google.com/p/sympy/issues/detail?id=3312

Here's a workaround:

    def AppliedUndef__lt__(self, other):
      if type(self) != type(other):
        return type(self) < type(other)
      return self.args < other.args
    sympy.function.AppliedUndef.is_negative = False
    sympy.function.AppliedUndef.__lt__ = AppliedUndef__lt__

Actually, setting is_negative turns out to not be strictly necessary once __lt__ is defined, but without setting it this way cse will do things like:

    tmp0 = f(1)
    tmp1 = -tmp0

and then never use tmp0 again. For example:

    ...
    >>> sympy.function.AppliedUndef.__lt__ = AppliedUndef__lt__
    >>> a = sympy.Function('a')
    >>> b = sympy.Function('b')
    >>> ma = sympy.Matrix([[a(0),a(1)], [a(2),a(3)]])
    >>> mb = sympy.Matrix([[b(0),b(1)], [b(2),b(3)]])

    >>> # at this point __lt_ has been patched, but not is_negative
    >>> sympy.cse(ma*mb)
([(x0, a(0)), (x1, a(1)), (x2, b(2)), (x3, b(0)), (x4, a(2)), (x5, b(1)), (x6, a(3)), (x7, b(3)), (x8, -x7), (x9, -x2), (x10, -x0), (x11, -x4)], [-x1*x9 - x10*x3, -x1*x8 - x10*x5, -x11*x3 - x6*x9, -x11*x5 - x6*x8])

    >>> sympy.function.AppliedUndef.is_negative = False
    >>> sympy.cse(ma*mb)
([(x0, a(0)), (x1, a(1)), (x2, b(0)), (x3, b(2)), (x4, a(2)), (x5, b(1)), (x6, a(3)), (x7, b(3))], [x0*x2 + x1*x3, x0*x5 + x1*x7, x2*x4 + x3*x6, x4*x5 + x6*x7])

Note how the second call to cse eliminates the useless temporaries, and the end result also has less (negating) operations.

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

Reply via email to