Skip to content

Commit

Permalink
Avoid exp rewrites that evaluate back to Pow in solve()
Browse files Browse the repository at this point in the history
This can lead to infinite recursion.

Fixes #24368
  • Loading branch information
asmeurer committed Apr 1, 2023
1 parent 1b64adc commit a3042a0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sympy/solvers/solvers.py
Expand Up @@ -42,6 +42,7 @@
separatevars)
from sympy.simplify.sqrtdenest import sqrt_depth
from sympy.simplify.fu import TR1, TR2i
from sympy.strategies.rl import rebuild
from sympy.matrices.common import NonInvertibleMatrixError
from sympy.matrices import Matrix, zeros
from sympy.polys import roots, cancel, factor, Poly
Expand Down Expand Up @@ -2770,6 +2771,7 @@ def equal(expr1, expr2):
return _vsolve(lhs.args[0] - rhs*exp(rhs), sym, **flags)

rewrite = lhs.rewrite(exp)
rewrite = rebuild(rewrite) # avoid rewrites involving evaluate=False
if rewrite != lhs:
return _vsolve(rewrite - rhs, sym, **flags)
except NotImplementedError:
Expand Down
10 changes: 10 additions & 0 deletions sympy/solvers/tests/test_solvers.py
Expand Up @@ -2,6 +2,7 @@
from sympy.core.add import Add
from sympy.core.containers import Tuple
from sympy.core.function import (Derivative, Function, diff)
from sympy.core.mod import Mod
from sympy.core.mul import Mul
from sympy.core import (GoldenRatio, TribonacciConstant)
from sympy.core.numbers import (E, Float, I, Rational, oo, pi)
Expand All @@ -13,6 +14,7 @@
from sympy.functions.elementary.complexes import (Abs, arg, conjugate, im, re)
from sympy.functions.elementary.exponential import (LambertW, exp, log)
from sympy.functions.elementary.hyperbolic import (atanh, cosh, sinh, tanh)
from sympy.functions.elementary.integers import floor
from sympy.functions.elementary.miscellaneous import (cbrt, root, sqrt)
from sympy.functions.elementary.piecewise import Piecewise
from sympy.functions.elementary.trigonometric import (acos, asin, atan, atan2, cos, sec, sin, tan)
Expand Down Expand Up @@ -2646,3 +2648,11 @@ def test_solve_undetermined_coeffs_issue_23927():
phi: 2*atan((A + sqrt(A**2 + B**2))/B),
r: (A**2 + A*sqrt(A**2 + B**2) + B**2)/(A + sqrt(A**2 + B**2))/-1
}]

def test_issue_24368():
# Ideally these would produce a solution, but for now just check that they
# don't fail with a RuntimeError
raises(NotImplementedError, lambda: solve(Mod(x**2, 49), x))
s2 = Symbol('s2', integer=True, positive=True)
f = floor(s2/2 - S(1)/2)
raises(NotImplementedError, lambda: solve((Mod(f**2/(f + 1) + 2*f/(f + 1) + 1/(f + 1), 1))*f + Mod(f**2/(f + 1) + 2*f/(f + 1) + 1/(f + 1), 1), s2))

0 comments on commit a3042a0

Please sign in to comment.