Repository: sympy/sympy

Issue:
Regression in Piecewise when xreplacing involing a Relational (sympy-1.9 -- sympy-1.10)
Here's a script to find the regression:
```bash
#!/bin/bash                                                                                                                                                                                                                                                                   
cat <<EOF >bisect-regression.py                                                                                                                                                                                                                                               
from sympy import symbols, Piecewise, Equality, true                                                                                                                                                                                                                          
x, y = symbols('x, y')                                                                                                                                                                                                                                                        
p1 = Piecewise(                                                                                                                                                                                                                                                               
    (0, Equality(x, true)),                                                                                                                                                                                                                                                   
    (1, true),                                                                                                                                                                                                                                                                
)                                                                                                                                                                                                                                                                             
p2 = p1.xreplace({x: y < 1})                                                                                                                                                                                                                                                  
assert p2.subs(y, 0) == 0                                                                                                                                                                                                                                                     
assert p2.subs(y, 1) == 1                                                                                                                                                                                                                                                     
EOF                                                                                                                                                                                                                                                                           
git bisect start --first-parent sympy-1.10 sympy-1.9                                                                                                                                                                                                                          
git bisect run python bisect-regression.py
```

<details>
<summary>$ ./bisect-regression.sh</summary>


```
[930efd2907a9d6c15500e0aa3d5507691dbb297b] Merge pull request #22358 from PGijsbers/18521_limits
running  'python' 'bisect-regression.py'
4282433901d3159a485866ce0d93aefe531ffbc7 is the first bad commit
commit 4282433901d3159a485866ce0d93aefe531ffbc7
Merge: 930efd2907 d02946955f
Author: Christopher Smith <smichr@gmail.com>
Date:   Sun Oct 24 04:20:27 2021 -0500

    Merge pull request #22286 from smichr/pwmeij
    
    modify _condsimp to handle missed cases; remove expensive steps from Piecewise.eval
```

</details>
It identifies gh-22286 as the PR introducing the regression (I don't blame anyone for that of course, our test coverage isn't always the best).

One way to resolve this would be to patch `_canonical_coeff` in `.core.relational` (introduced in the referenced PR), so that it handles `Relational`s with `BooleanTrue`/`BooleanFalse` in either `lhs` or `rhs`. But if someone knows a better way, let me know, otherwise I'll try to whip up a PR doing just that.


Discussion:

Patch:
diff --git a/sympy/core/relational.py b/sympy/core/relational.py
index d09ca82388..28bf039c9b 100644
--- a/sympy/core/relational.py
+++ b/sympy/core/relational.py
@@ -52,6 +52,8 @@ def _canonical_coeff(rel):
     rel = rel.canonical
     if not rel.is_Relational or rel.rhs.is_Boolean:
         return rel  # Eq(x, True)
+    if not isinstance(rel.lhs, Expr):
+        return rel.reversed  # e.g.: Eq(True, x) -> Eq(x, True)
     b, l = rel.lhs.as_coeff_Add(rational=True)
     m, lhs = l.as_coeff_Mul(rational=True)
     rhs = (rel.rhs - b)/m


Test patch:
diff --git a/sympy/functions/elementary/tests/test_piecewise.py b/sympy/functions/elementary/tests/test_piecewise.py
index d06dc832e1..7d07280955 100644
--- a/sympy/functions/elementary/tests/test_piecewise.py
+++ b/sympy/functions/elementary/tests/test_piecewise.py
@@ -1335,6 +1335,17 @@ def _ITE(*args):
     raises(ValueError, lambda: _ITE((a, x < 2), (b, x > 3)))
 
 
+def test_Piecewise_replace_relational_27538():
+    x, y = symbols('x, y')
+    p1 = Piecewise(
+        (0, Eq(x, True)),
+        (1, True),
+    )
+    p2 = p1.xreplace({x: y < 1})
+    assert p2.subs(y, 0) == 0
+    assert p2.subs(y, 1) == 1
+
+
 def test_issue_14052():
     assert integrate(abs(sin(x)), (x, 0, 2*pi)) == 4