New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RecursionError with Piecewise.xreplace containing Indexed objects #14933

Open
richardotis opened this Issue Jul 16, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@richardotis
Copy link
Contributor

richardotis commented Jul 16, 2018

Minimal example to reproduce the issue:

from sympy import Piecewise, Symbol, ITE, MatrixSymbol

x = Symbol('x')
y = Symbol('y')

inp = MatrixSymbol('inp', 1, 1)
rep_dict = {y: inp[0, 0], x: inp[0, 0]}

p = Piecewise((1, ITE(y > 0, x < 0, True)))
z = p.xreplace(rep_dict)
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-44-46c498a48320> in <module>()
----> 1 z = p.xreplace(rep_dict)
      2 print(z)

/home/rotis/anaconda/envs/calphadpy3/lib/python3.5/site-packages/sympy/core/basic.py in xreplace(self, rule)
   1113 
   1114         """
-> 1115         value, _ = self._xreplace(rule)
   1116         return value
   1117 

/home/rotis/anaconda/envs/calphadpy3/lib/python3.5/site-packages/sympy/core/basic.py in _xreplace(self, rule)
   1134             args = tuple(args)
   1135             if changed:
-> 1136                 return self.func(*args), True
   1137         return self, False
   1138 

/home/rotis/anaconda/envs/calphadpy3/lib/python3.5/site-packages/sympy/functions/elementary/piecewise.py in __new__(cls, *args, **options)
    134 
    135         if options.pop('evaluate', True):
--> 136             r = cls.eval(*newargs)
    137         else:
    138             r = None

/home/rotis/anaconda/envs/calphadpy3/lib/python3.5/site-packages/sympy/functions/elementary/piecewise.py in eval(cls, *_args)
    183                     x = free.pop()
    184                     try:
--> 185                         c = c.as_set().as_relational(x)
    186                     except NotImplementedError:
    187                         pass

/home/rotis/anaconda/envs/calphadpy3/lib/python3.5/site-packages/sympy/logic/boolalg.py in as_set(self)
    144             reps = {}
    145             for r in self.atoms(Relational):
--> 146                 if periodicity(r, x) not in (0, None):
    147                     s = r._eval_as_set()
    148                     if s in (S.EmptySet, S.UniversalSet, S.Reals):

/home/rotis/anaconda/envs/calphadpy3/lib/python3.5/site-packages/sympy/calculus/util.py in periodicity(f, symbol, check)
    471     elif period is None:
    472         from sympy.solvers.decompogen import compogen
--> 473         g_s = decompogen(f, symbol)
    474         num_of_gs = len(g_s)
    475         if num_of_gs > 1:

/home/rotis/anaconda/envs/calphadpy3/lib/python3.5/site-packages/sympy/solvers/decompogen.py in decompogen(f, symbol)
     55         f1 = f.subs(gens[0], symbol)
     56         f2 = gens[0]
---> 57         result += [f1] + decompogen(f2, symbol)
     58         return result
     59 

/home/rotis/anaconda/envs/calphadpy3/lib/python3.5/site-packages/sympy/solvers/decompogen.py in decompogen(f, symbol)
     55         f1 = f.subs(gens[0], symbol)
     56         f2 = gens[0]
---> 57         result += [f1] + decompogen(f2, symbol)
     58         return result
     59 

/home/rotis/anaconda/envs/calphadpy3/lib/python3.5/site-packages/sympy/solvers/decompogen.py in decompogen(f, symbol)
     55         f1 = f.subs(gens[0], symbol)
     56         f2 = gens[0]
---> 57         result += [f1] + decompogen(f2, symbol)
     58         return result
     59 

... last 3 frames repeated, from the frame below ...

/home/rotis/anaconda/envs/calphadpy3/lib/python3.5/site-packages/sympy/solvers/decompogen.py in decompogen(f, symbol)
     55         f1 = f.subs(gens[0], symbol)
     56         f2 = gens[0]
---> 57         result += [f1] + decompogen(f2, symbol)
     58         return result
     59 

RecursionError: maximum recursion depth exceeded

Note that this will occur even if you pass evaluate=False to Piecewise.
This may be related to #14899

@richardotis

This comment has been minimized.

Copy link
Contributor

richardotis commented Jul 16, 2018

In sympy/functions/elementary/piecewise.py:173, changing c.free_symbols to c.expr_free_symbols seems to fix the issue. Still analyzing.

@asmeurer

This comment has been minimized.

Copy link
Member

asmeurer commented Jul 17, 2018

CC @smichr

@asmeurer

This comment has been minimized.

Copy link
Member

asmeurer commented Jul 19, 2018

It also would help to bisect the issue.

bocklund added a commit to pycalphad/pycalphad that referenced this issue Aug 17, 2018

FIX: SymPy 1.2 compatibility (#180)
Provides compatibility fixes for codegen changes, as well as a workaround for sympy/sympy#14933
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment