Repository: sympy/sympy

Issue:
Substitution with E fails
Same fuzzer.

expr1 = E 
expr0 = expr1 \* expr1 
expr1 = expr0.subs(expr1,expr0) 

fails with:
[expfail.zip](https://github.com/sympy/sympy/files/280484/expfail.zip)

Traceback (most recent call last):
  File "expfail.py", line 11, in <module>
    expr1 = expr0.subs(expr1,expr0) 
  File "/usr/local/lib/python2.7/site-packages/sympy/core/basic.py", line 902, in subs
    rv = rv._subs(old, new, *_kwargs)
  File "/usr/local/lib/python2.7/site-packages/sympy/core/cache.py", line 95, in wrapper
    retval = func(_args, *_kwargs)
  File "/usr/local/lib/python2.7/site-packages/sympy/core/basic.py", line 1014, in _subs
    rv = self._eval_subs(old, new)
  File "/usr/local/lib/python2.7/site-packages/sympy/functions/elementary/exponential.py", line 374, in _eval_subs
    return Function._eval_subs(self, old, new)
  File "/usr/local/lib/python2.7/site-packages/sympy/core/function.py", line 272, in _eval_subs
    return new(_self.args)
TypeError: 'exp' object is not callable

Works fine with anything but E, depends on \* (+ works fine)


Discussion:

Patch:
diff --git a/.travis.yml b/.travis.yml
index 4a4e1ddbe2..3189289d47 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -286,7 +286,7 @@ before_install:
       pip install "gmpy==1.16";
     fi
   - if [[ "${TEST_SPHINX}" == "true" ]]; then
-      pip install "sphinx==1.3.1" "docutils==0.12" doctr;
+      pip install "sphinx==1.3.1" doctr;
     fi
   - |
     if [[ "${TEST_MATPLOTLIB}" == "true" || "${TEST_SYMENGINE}" == "true" ]]; then
@@ -349,11 +349,9 @@ install:
       python setup.py install;
     fi
 script:
-  # Don't run doctr of the build fails
-  - set -e
   - bin/test_travis.sh
   - if [[ "${TEST_SPHINX}" == "true" ]]; then
-        doctr deploy --deploy-repo sympy/sympy_doc --gh-pages-docs dev --command './generate_indexes.py';
+      doctr deploy --deploy-repo sympy/sympy_doc --gh-pages-docs dev --command './generate_indexes.py';
     fi
 notifications:
   email: false
diff --git a/sympy/core/function.py b/sympy/core/function.py
index eec86a8540..2d33d96b94 100644
--- a/sympy/core/function.py
+++ b/sympy/core/function.py
@@ -308,8 +308,9 @@ def func(self):
         return self.__class__
 
     def _eval_subs(self, old, new):
-        if (old.is_Function and new.is_Function and old == self.func and
-            len(self.args) in new.nargs):
+        if (old.is_Function and new.is_Function and
+            callable(old) and callable(new) and
+            old == self.func and len(self.args) in new.nargs):
             return new(*self.args)
 
 
diff --git a/sympy/printing/pretty/pretty.py b/sympy/printing/pretty/pretty.py
index ef64d8172c..99c30dc5c3 100644
--- a/sympy/printing/pretty/pretty.py
+++ b/sympy/printing/pretty/pretty.py
@@ -1193,14 +1193,10 @@ def _print_SingularityFunction(self, e):
             return base**n
 
     def _print_gamma(self, e):
-        from sympy.functions import gamma
         if self._use_unicode:
             pform = self._print(e.args[0])
             pform = prettyForm(*pform.parens())
-            if e.func == gamma:
-                pform = prettyForm(*pform.left(greek_unicode['Gamma']))
-            else:
-                pform = prettyForm(*pform.left(greek_unicode['gamma']))
+            pform = prettyForm(*pform.left(greek_unicode['Gamma']))
             return pform
         else:
             return self._print_Function(e)
diff --git a/sympy/sets/fancysets.py b/sympy/sets/fancysets.py
index cae6eb7d42..a009f3f7ab 100644
--- a/sympy/sets/fancysets.py
+++ b/sympy/sets/fancysets.py
@@ -1353,25 +1353,6 @@ def _measure(self):
         """
         return self.sets._measure
 
-    @classmethod
-    def from_real(cls, sets):
-        """
-        Converts given subset of real numbers to a complex region.
-
-        Examples
-        ========
-
-        >>> from sympy import Interval, ComplexRegion
-        >>> unit = Interval(0,1)
-        >>> ComplexRegion.from_real(unit)
-        ComplexRegion([0, 1] x {0}, False)
-
-        """
-        if not sets.is_subset(S.Reals):
-            raise ValueError("sets must be a subset of the real line")
-
-        return cls(sets * FiniteSet(0))
-
     def _contains(self, other):
         from sympy.functions import arg, Abs
         from sympy.core.containers import Tuple
@@ -1428,6 +1409,8 @@ def _intersect(self, other):
                 return ComplexRegion(new_r_interval*new_theta_interval,
                                     polar=True)
 
+        if other is S.Reals:
+            return other
 
         if other.is_subset(S.Reals):
             new_interval = []
@@ -1450,10 +1433,6 @@ def _intersect(self, other):
 
     def _union(self, other):
 
-        if other.is_subset(S.Reals):
-            # treat a subset of reals as a complex region
-            other = ComplexRegion.from_real(other)
-
         if other.is_ComplexRegion:
 
             # self in rectangular form
@@ -1464,6 +1443,9 @@ def _union(self, other):
             elif self.polar and other.polar:
                 return ComplexRegion(Union(self.sets, other.sets), polar=True)
 
+        if self == S.Complexes:
+            return self
+
         return None
 
 
diff --git a/sympy/sets/sets.py b/sympy/sets/sets.py
index 0321862557..bb9d9b32e6 100644
--- a/sympy/sets/sets.py
+++ b/sympy/sets/sets.py
@@ -633,8 +633,6 @@ def _intersect(self, other):
                 for a, b in zip(self.sets, other.sets))
 
     def _union(self, other):
-        if other.is_subset(self):
-            return self
         if not other.is_ProductSet:
             return None
         if len(other.args) != len(self.args):


Test patch:
diff --git a/sympy/core/tests/test_function.py b/sympy/core/tests/test_function.py
index 4dd523718e..d1fdd97ef8 100644
--- a/sympy/core/tests/test_function.py
+++ b/sympy/core/tests/test_function.py
@@ -804,3 +804,10 @@ def test_Derivative_as_finite_difference():
     xm, xp, ym, yp = x-half, x+half, y-half, y+half
     ref2 = f(xm, ym) + f(xp, yp) - f(xp, ym) - f(xm, yp)
     assert (d2fdxdy.as_finite_difference() - ref2).simplify() == 0
+
+def test_issue_11159():
+    # Tests Application._eval_subs
+    expr1 = E
+    expr0 = expr1 * expr1
+    expr1 = expr0.subs(expr1,expr0)
+    assert expr0 == expr1
diff --git a/sympy/printing/pretty/tests/test_pretty.py b/sympy/printing/pretty/tests/test_pretty.py
index 466e45cd40..5243ab8c82 100644
--- a/sympy/printing/pretty/tests/test_pretty.py
+++ b/sympy/printing/pretty/tests/test_pretty.py
@@ -12,7 +12,7 @@
 
 from sympy.functions import (Abs, Chi, Ci, Ei, KroneckerDelta,
     Piecewise, Shi, Si, atan2, binomial, catalan, ceiling, cos,
-    euler, exp, expint, factorial, factorial2, floor, hyper, log,
+    euler, exp, expint, factorial, factorial2, floor, gamma, hyper, log,
     lowergamma, meijerg, sin, sqrt, subfactorial, tan, uppergamma,
     elliptic_k, elliptic_f, elliptic_e, elliptic_pi, DiracDelta)
 
@@ -4623,11 +4623,9 @@ def test_pretty_Subs():
 
 
 def test_gammas():
-    from sympy import gamma
     assert upretty(lowergamma(x, y)) == u"γ(x, y)"
     assert upretty(uppergamma(x, y)) == u"Γ(x, y)"
     assert xpretty(gamma(x), use_unicode=True) == u'Γ(x)'
-    assert xpretty(symbols('gamma', cls=Function)(x), use_unicode=True) == u'γ(x)'
 
 
 def test_SingularityFunction():
diff --git a/sympy/sets/tests/test_fancysets.py b/sympy/sets/tests/test_fancysets.py
index 165bfe2530..0d26c0b650 100644
--- a/sympy/sets/tests/test_fancysets.py
+++ b/sympy/sets/tests/test_fancysets.py
@@ -5,7 +5,7 @@
                              Intersection)
 from sympy.simplify.simplify import simplify
 from sympy import (S, Symbol, Lambda, symbols, cos, sin, pi, oo, Basic,
-                   Rational, sqrt, tan, log, exp, Abs, I, Tuple, eye)
+                   Rational, sqrt, tan, log, exp, Abs, I, Tuple)
 from sympy.utilities.iterables import cartes
 from sympy.utilities.pytest import XFAIL, raises
 from sympy.abc import x, y, z, t
@@ -594,7 +594,7 @@ def test_ComplexRegion_union():
     assert c7.union(c8) == ComplexRegion(p4)
 
     assert c1.union(Interval(2, 4)) == Union(c1, Interval(2, 4), evaluate=False)
-    assert c5.union(Interval(2, 4)) == Union(c5, ComplexRegion.from_real(Interval(2, 4)))
+    assert c5.union(Interval(2, 4)) == Union(c5, Interval(2, 4), evaluate=False)
 
 
 def test_ComplexRegion_measure():
@@ -698,13 +698,3 @@ def test_issue_11732():
     assert (pointComplex in S.Naturals0) == False
     assert (pointComplex in S.Integers) == False
     assert (pointComplex in S.Complexes) == True
-
-
-def test_issue_11730():
-    unit = Interval(0, 1)
-    square = ComplexRegion(unit ** 2)
-
-    assert Union(S.Complexes, FiniteSet(oo)) != S.Complexes
-    assert Union(S.Complexes, FiniteSet(eye(4))) != S.Complexes
-    assert Union(unit, square) == square
-    assert Intersection(S.Reals, square) == unit