Repository: sympy/sympy

Issue:
Piecewise simplification raises error
An expression given [here](https://stackoverflow.com/questions/53287120/sympy-generating-broken-piecewise-conditions-when-simplifying?rq=1) fails to simplify and gives, instead, the following traceback;
```python
>>> S('''Piecewise(
...     (0, (n > 3) | (n < 0) | ((n > 0) & (n < 3))),
...     (-1, Ne(n, 0)),
...     (0, Ne(n, 3)),
...     (-1/2, True))''')
Piecewise((0, (n > 3) | (n < 0) | ((n > 0) & (n < 3))), (-1, Ne(n, 0)), (0, Ne(n, 3)), (-1/2, True))
>>> simplify(_)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "*simplify\simplify.py", line 520, in simplify

    return _eval_simplify(ratio=ratio, measure=measure, rational=rational, inverse=inverse)
  File "*functions\elementary\piecewise.py", line 326
, in _eval_simplify
    for a in self.args]
  File "*functions\elementary\piecewise.py", line 326
, in <listcomp>
    for a in self.args]
  File "*functions\elementary\piecewise.py", line 65,
 in _eval_simplify
    inverse=inverse) for a in self.args])
  File "*functions\elementary\piecewise.py", line 65,
 in <listcomp>
    inverse=inverse) for a in self.args])
  File "*logic\boolalg.py", line 432, in simplify
    return self._eval_simplify(ratio, measure, rational, inverse)
  File "*logic\boolalg.py", line 800, in _eval_simplify
    measure, S.true)
  File "*logic\boolalg.py", line 554, in _apply_patternbased_simplification
    Rel = [i.canonical for i in Rel]
  File "*logic\boolalg.py", line 554, in <listcomp>
    Rel = [i.canonical for i in Rel]
AttributeError: 'And' object has no attribute 'canonical'
```

There is an error because a sifted group was intended to be sifted again but the original group was sifted the second time.

Discussion:

Patch:
diff --git a/sympy/logic/boolalg.py b/sympy/logic/boolalg.py
index c19b95da65..bc1da809e4 100644
--- a/sympy/logic/boolalg.py
+++ b/sympy/logic/boolalg.py
@@ -555,7 +555,7 @@ def _apply_patternbased_simplification(self, rv, patterns, measure,
                            binary=True)
         if len(Rel) <= 1:
             return rv
-        Rel, nonRealRel = sift(rv.args, lambda i: all(s.is_real is not False
+        Rel, nonRealRel = sift(Rel, lambda i: all(s.is_real is not False
                                                       for s in i.free_symbols),
                                binary=True)
         Rel = [i.canonical for i in Rel]


Test patch:
diff --git a/sympy/logic/tests/test_boolalg.py b/sympy/logic/tests/test_boolalg.py
index b07d5eda49..8ced2bfaec 100644
--- a/sympy/logic/tests/test_boolalg.py
+++ b/sympy/logic/tests/test_boolalg.py
@@ -1072,3 +1072,10 @@ def test_relational_simplification_patterns_numerically():
                 assert originalvalue == simplifiedvalue, "Original: {}\nand"\
                     " simplified: {}\ndo not evaluate to the same value for"\
                     "{}".format(original, simplified, sublist)
+
+
+def test_issue_16803():
+    n = symbols('n')
+    # No simplification done, but should not raise an exception
+    assert ((n > 3) | (n < 0) | ((n > 0) & (n < 3))).simplify() == \
+        ((n > 3) | (n < 0) | ((n > 0) & (n < 3)))