Repository: sympy/sympy

Issue:
relationals created with symbols can be made canonical
``` python
>>> a=1<x
>>> b=S(1)<x
>>> c=x>S(1)
>>> a==b
False
>>> b==c
False
```

The `a==b` failure is expected since Python flips the args around. But it and the `b==c` case could all be the same if Relationals created from symbols (<, >, etc...) would sort args canonically. This, like sorting arguments of an evaluated Add (so that `x + 2` == `2 + x`), would make working with inequalities a little friendlier.


Discussion:

Patch:
diff --git a/sympy/core/relational.py b/sympy/core/relational.py
index 3d95a9102b..7a4be766fd 100644
--- a/sympy/core/relational.py
+++ b/sympy/core/relational.py
@@ -265,9 +265,20 @@ def canonical(self):
         x < -y
         >>> (-y < -x).canonical
         x < y
+
+        The canonicalization is recursively applied:
+
+        >>> from sympy import Eq
+        >>> Eq(x < y, y > x).canonical
+        True
         """
-        args = self.args
-        r = self
+        args = tuple([i.canonical if isinstance(i, Relational) else i for i in self.args])
+        if args != self.args:
+            r = self.func(*args)
+            if not isinstance(r, Relational):
+                return r
+        else:
+            r = self
         if r.rhs.is_number:
             if r.rhs.is_Number and r.lhs.is_Number and r.lhs > r.rhs:
                 r = r.reversed


Test patch:
diff --git a/sympy/core/tests/test_relational.py b/sympy/core/tests/test_relational.py
index eeb5f9afec..e98f5f5df7 100644
--- a/sympy/core/tests/test_relational.py
+++ b/sympy/core/tests/test_relational.py
@@ -854,6 +854,7 @@ def test_canonical():
     assert [i.canonical for i in c] == c
     assert [i.reversed.canonical for i in c] == c
     assert not any(i.lhs.is_Number and not i.rhs.is_Number for i in c)
+    assert Eq(y < x, x > y).canonical is S.true
 
 
 @XFAIL