Repository: sympy/sympy

Issue:
(Integration) regressions?
Hello, I am trying to integrate the following expressions (`f`) which work under SymPy v1.4 but fail on both the master version and 1.7.1.

Behavior in v1.4 is:

```
>>> from sympy import *
>>> x = Symbol('x', real=True, nonzero=True)

>>> f1 = x*(-x**4/asin(5)**4 - x*sinh(x + log(asin(5))) + 5)
>>> integrate(f1, x)
-x**6/(6*asin(5)**4) - x**2*cosh(x + log(asin(5))) + 5*x**2/2 + 2*x*sinh(x + log(asin(5))) - 2*cosh(x + log(asin(5)))

>>> f2 = (x + cosh(cos(4)))/(x*(x + 1/(12*x)))
>>> integrate(f2, x)
log(x**2 + 1/12)/2 + 2*sqrt(3)*cosh(cos(4))*atan(2*sqrt(3)*x)
```

For `f1`: v1.7.1 and master raise the following error: `RecursionError: maximum recursion depth exceeded`
For `f2`: v1.7.1 raises CoercionFailed error (issue possibly related to #21024), while master fails with `RecursionError: maximum recursion depth exceeded`

Discussion:

Patch:
diff --git a/sympy/solvers/solvers.py b/sympy/solvers/solvers.py
index 06d30cbdea..e1df176891 100644
--- a/sympy/solvers/solvers.py
+++ b/sympy/solvers/solvers.py
@@ -912,8 +912,8 @@ def _sympified_list(w):
             f[i] = fi.as_expr()
 
         # rewrite hyperbolics in terms of exp
-        f[i] = f[i].replace(lambda w: isinstance(w, HyperbolicFunction),
-                lambda w: w.rewrite(exp))
+        f[i] = f[i].replace(lambda w: isinstance(w, HyperbolicFunction) and \
+            (len(w.free_symbols & set(symbols)) > 0), lambda w: w.rewrite(exp))
 
         # if we have a Matrix, we need to iterate over its elements again
         if f[i].is_Matrix:


Test patch:
diff --git a/sympy/integrals/tests/test_integrals.py b/sympy/integrals/tests/test_integrals.py
index 3de1d543f3..06537bdee5 100644
--- a/sympy/integrals/tests/test_integrals.py
+++ b/sympy/integrals/tests/test_integrals.py
@@ -1708,6 +1708,11 @@ def test_issue_17841():
 
 def test_issue_21034():
     x = Symbol('x', real=True, nonzero=True)
-    f = x*(-x**4/asin(5)**4 - x*sinh(x + log(asin(5))) + 5)
-    assert integrate(f, x) == \
+    f1 = x*(-x**4/asin(5)**4 - x*sinh(x + log(asin(5))) + 5)
+    f2 = (x + cosh(cos(4)))/(x*(x + 1/(12*x)))
+
+    assert integrate(f1, x) == \
         -x**6/(6*asin(5)**4) - x**2*cosh(x + log(asin(5))) + 5*x**2/2 + 2*x*sinh(x + log(asin(5))) - 2*cosh(x + log(asin(5)))
+
+    assert integrate(f2, x) == \
+        log(x**2 + S(1)/12)/2 + 2*sqrt(3)*cosh(cos(4))*atan(2*sqrt(3)*x)
diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py
index ead1d47b7c..7ee9d2ecce 100644
--- a/sympy/solvers/tests/test_solvers.py
+++ b/sympy/solvers/tests/test_solvers.py
@@ -2316,6 +2316,18 @@ def test_issue_20902():
     assert solve(f.subs({t: 3 * x + 2}).diff(x) > 0, x) == (S(-1) < x) & (x < S(-1)/3)
 
 
+def test_issue_21034():
+    a = symbols('a', real=True)
+    system = [x - cosh(cos(4)), y - sinh(cos(a)), z - tanh(x)]
+    assert solve(system, x, y, z) == {x: cosh(cos(4)), z: tanh(cosh(cos(4))),
+        y: sinh(cos(a))}
+    #Constants inside hyperbolic functions should not be rewritten in terms of exp
+    newsystem = [(exp(x) - exp(-x)) - tanh(x)*(exp(x) + exp(-x)) + x - 5]
+    assert solve(newsystem, x) == {x: 5}
+    #If the variable of interest is present in hyperbolic function, only then
+    # it shouuld be rewritten in terms of exp and solved further
+
+
 def test_issue_4886():
     z = a*sqrt(R**2*a**2 + R**2*b**2 - c**2)/(a**2 + b**2)
     t = b*c/(a**2 + b**2)