Repository: sympy/sympy

Issue:
Incorrect result in intersection of ComplexRegions and Reals
Hi,

the following bug was found by @jksuom [here](https://github.com/sympy/sympy/pull/11818#issuecomment-266730240)
```python
In [5]: ival = Interval(1, 2)

In [6]: sq = ComplexRegion(ival*unit)

In [7]: Intersection(S.Reals, sq)
Out[7]: ∅
```
Expected answer would be `[1,2]`
Related bug for intersection with a real subset in case of polar coordinates:
```python
In [3]: rad, arg = Interval(0,1), FiniteSet(pi)

In [4]: cr = ComplexRegion(rad * arg, polar=True)

In [5]: Intersection(cr, S.Reals)
Out[5]: [0, 1]

```
Here, we would expect the answer to be `[-1,0]`

Discussion:

Patch:
diff --git a/.travis.yml b/.travis.yml
index 4a4e1ddbe2..7b77c8a747 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -348,11 +348,9 @@ install:
     elif [[ "${TEST_SAGE}" != "true" ]]; then
       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
+script: |
+    bin/test_travis.sh
+    if [[ "${TEST_SPHINX}" == "true" ]]; then
         doctr deploy --deploy-repo sympy/sympy_doc --gh-pages-docs dev --command './generate_indexes.py';
     fi
 notifications:
diff --git a/sympy/sets/fancysets.py b/sympy/sets/fancysets.py
index cae6eb7d42..959cfce626 100644
--- a/sympy/sets/fancysets.py
+++ b/sympy/sets/fancysets.py
@@ -466,9 +466,14 @@ def _intersect(self, other):
                 if any(i is None for i in (new_sup, new_inf)):
                     return
 
+
                 range_set = S.EmptySet
 
                 if all(i.is_real for i in (new_sup, new_inf)):
+                    # this assumes continuity of underlying function
+                    # however fixes the case when it is decreasing
+                    if new_inf > new_sup:
+                        new_inf, new_sup = new_sup, new_inf
                     new_interval = Interval(new_inf, new_sup, new_lopen, new_ropen)
                     range_set = base_set._intersect(new_interval)
                 else:
@@ -1431,11 +1436,12 @@ def _intersect(self, other):
 
         if other.is_subset(S.Reals):
             new_interval = []
+            x = symbols("x", cls=Dummy, real=True)
 
             # self in rectangular form
             if not self.polar:
                 for element in self.psets:
-                    if S.Zero in element.args[0]:
+                    if S.Zero in element.args[1]:
                         new_interval.append(element.args[0])
                 new_interval = Union(*new_interval)
                 return Intersection(new_interval, other)
@@ -1443,8 +1449,12 @@ def _intersect(self, other):
             # self in polar form
             elif self.polar:
                 for element in self.psets:
-                    if (0 in element.args[1]) or (S.Pi in element.args[1]):
+                    if S.Zero in element.args[1]:
                         new_interval.append(element.args[0])
+                    if S.Pi in element.args[1]:
+                        new_interval.append(ImageSet(Lambda(x, -x), element.args[0]))
+                    if S.Zero in element.args[0]:
+                        new_interval.append(FiniteSet(0))
                 new_interval = Union(*new_interval)
                 return Intersection(new_interval, other)
 
diff --git a/sympy/sets/sets.py b/sympy/sets/sets.py
index 0321862557..9620389287 100644
--- a/sympy/sets/sets.py
+++ b/sympy/sets/sets.py
@@ -860,6 +860,8 @@ def _intersect(self, other):
 
         See Set._intersect for docstring
         """
+        if other.is_EmptySet:
+            return other
         # We only know how to intersect with other intervals
         if not other.is_Interval:
             return None


Test patch:
diff --git a/sympy/sets/tests/test_fancysets.py b/sympy/sets/tests/test_fancysets.py
index 165bfe2530..2ab0542dd0 100644
--- a/sympy/sets/tests/test_fancysets.py
+++ b/sympy/sets/tests/test_fancysets.py
@@ -708,3 +708,23 @@ def test_issue_11730():
     assert Union(S.Complexes, FiniteSet(eye(4))) != S.Complexes
     assert Union(unit, square) == square
     assert Intersection(S.Reals, square) == unit
+
+
+def test_issue_11938():
+    unit = Interval(0, 1)
+    ival = Interval(1, 2)
+    cr1 = ComplexRegion(ival * unit)
+
+    assert Intersection(cr1, S.Reals) == ival
+    assert Intersection(cr1, unit) == FiniteSet(1)
+
+    arg1 = Interval(0, S.Pi)
+    arg2 = FiniteSet(S.Pi)
+    arg3 = Interval(S.Pi / 4, 3 * S.Pi / 4)
+    cp1 = ComplexRegion(unit * arg1, polar=True)
+    cp2 = ComplexRegion(unit * arg2, polar=True)
+    cp3 = ComplexRegion(unit * arg3, polar=True)
+
+    assert Intersection(cp1, S.Reals) == Interval(-1, 1)
+    assert Intersection(cp2, S.Reals) == Interval(-1, 0)
+    assert Intersection(cp3, S.Reals) == FiniteSet(0)