Skip to content

Commit

Permalink
Sort the args of Xor.
Browse files Browse the repository at this point in the history
Complement to issue 4081.
  • Loading branch information
jrioux committed Nov 12, 2013
1 parent ccd216a commit 7f822c5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
25 changes: 25 additions & 0 deletions sympy/logic/boolalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,31 @@ class Xor(BooleanFunction):
x
"""
def __new__(cls, *args, **options):
args = [_sympify(arg) for arg in args]

argset = set(args)
truecount = 0
for x in args:
if isinstance(x, Number) or x in [True, False]: # Includes 0, 1
argset.discard(x)
if x:
truecount += 1
if len(argset) < 1:
return true if truecount % 2 != 0 else false
if truecount % 2 != 0:
return Not(Xor(*argset))
_args = frozenset(argset)
obj = super(Xor, cls).__new__(cls, *_args, **options)
if isinstance(obj, Xor):
obj._argset = _args
return obj

@property
@cacheit
def args(self):
return tuple(ordered(self._argset))

@classmethod
def eval(cls, *args):
if not args:
Expand Down
7 changes: 5 additions & 2 deletions sympy/printing/tests/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Interval, Lambda, Limit, Matrix, nan, O, oo, pi, Rational, Float, Rel,
S, sin, SparseMatrix, sqrt, summation, Sum, Symbol, symbols, Wild,
WildFunction, zeta, zoo, Dummy, Dict, Tuple, FiniteSet, factor,
MatrixSymbol, subfactorial, true, false, Equivalent)
MatrixSymbol, subfactorial, true, false, Equivalent, Xor)
from sympy.core import Expr
from sympy.physics.units import second, joule
from sympy.polys import Poly, RootOf, RootSum, groebner, ring, field, ZZ, QQ, lex, grlex
Expand Down Expand Up @@ -698,4 +698,7 @@ def test_true_false():
assert str(false) == repr(false) == sstr(false) == "False"

def test_Equivalent():
assert str(Equivalent(x, y)) == "Equivalent(x, y)"
assert str(Equivalent(y, x)) == "Equivalent(x, y)"

def test_Xor():
assert str(Xor(y, x, evaluate=False)) == "Xor(x, y)"

0 comments on commit 7f822c5

Please sign in to comment.