def test_bool_map(): """ Test working of bool_map function. """ minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 0, 1, 1], [1, 1, 1, 1]] assert bool_map(Not(Not(a)), a) == (a, {a: a}) assert bool_map(SOPform([w, x, y, z], minterms), POSform([w, x, y, z], minterms)) == \ (And(Or(Not(w), y), Or(Not(x), y), z), {x: x, w: w, z: z, y: y}) assert bool_map(SOPform([x, z, y], [[1, 0, 1]]), SOPform([a, b, c], [[1, 0, 1]])) != False function1 = SOPform([x, z, y], [[1, 0, 1], [0, 0, 1]]) function2 = SOPform([a, b, c], [[1, 0, 1], [1, 0, 0]]) assert bool_map(function1, function2) == \ (function1, {y: a, z: b}) assert bool_map(Xor(x, y), ~Xor(x, y)) == False assert bool_map(And(x, y), Or(x, y)) is None assert bool_map(And(x, y), And(x, y, z)) is None # issue 16179 assert bool_map(Xor(x, y, z), ~Xor(x, y, z)) == False assert bool_map(Xor(a, x, y, z), ~Xor(a, x, y, z)) == False
def test_fcode_Xlogical(): x, y, z = symbols("x y z") # binary Xor assert fcode(Xor(x, y, evaluate=False)) == "x .neqv. y" assert fcode(Xor(x, Not(y), evaluate=False)) == "x .neqv. .not. y" assert fcode(Xor(Not(x), y, evaluate=False)) == "y .neqv. .not. x" assert fcode(Xor(Not(x), Not(y), evaluate=False)) == ".not. x .neqv. .not. y" assert fcode(Not(Xor(x, y, evaluate=False), evaluate=False)) == ".not. (x .neqv. y)" # binary Equivalent assert fcode(Equivalent(x, y)) == "x .eqv. y" assert fcode(Equivalent(x, Not(y))) == "x .eqv. .not. y" assert fcode(Equivalent(Not(x), y)) == "y .eqv. .not. x" assert fcode(Equivalent(Not(x), Not(y))) == ".not. x .eqv. .not. y" assert fcode(Not(Equivalent(x, y), evaluate=False)) == ".not. (x .eqv. y)" # mixed And/Equivalent assert fcode(Equivalent(And(y, z), x)) == "x .eqv. y .and. z" assert fcode(Equivalent(And(z, x), y)) == "y .eqv. x .and. z" assert fcode(Equivalent(And(x, y), z)) == "z .eqv. x .and. y" assert fcode(And(Equivalent(y, z), x)) == "x .and. (y .eqv. z)" assert fcode(And(Equivalent(z, x), y)) == "y .and. (x .eqv. z)" assert fcode(And(Equivalent(x, y), z)) == "z .and. (x .eqv. y)" # mixed Or/Equivalent assert fcode(Equivalent(Or(y, z), x)) == "x .eqv. y .or. z" assert fcode(Equivalent(Or(z, x), y)) == "y .eqv. x .or. z" assert fcode(Equivalent(Or(x, y), z)) == "z .eqv. x .or. y" assert fcode(Or(Equivalent(y, z), x)) == "x .or. (y .eqv. z)" assert fcode(Or(Equivalent(z, x), y)) == "y .or. (x .eqv. z)" assert fcode(Or(Equivalent(x, y), z)) == "z .or. (x .eqv. y)" # mixed Xor/Equivalent assert fcode(Equivalent(Xor(y, z, evaluate=False), x)) == "x .eqv. (y .neqv. z)" assert fcode(Equivalent(Xor(z, x, evaluate=False), y)) == "y .eqv. (x .neqv. z)" assert fcode(Equivalent(Xor(x, y, evaluate=False), z)) == "z .eqv. (x .neqv. y)" assert fcode(Xor(Equivalent(y, z), x, evaluate=False)) == "x .neqv. (y .eqv. z)" assert fcode(Xor(Equivalent(z, x), y, evaluate=False)) == "y .neqv. (x .eqv. z)" assert fcode(Xor(Equivalent(x, y), z, evaluate=False)) == "z .neqv. (x .eqv. y)" # mixed And/Xor assert fcode(Xor(And(y, z), x, evaluate=False)) == "x .neqv. y .and. z" assert fcode(Xor(And(z, x), y, evaluate=False)) == "y .neqv. x .and. z" assert fcode(Xor(And(x, y), z, evaluate=False)) == "z .neqv. x .and. y" assert fcode(And(Xor(y, z, evaluate=False), x)) == "x .and. (y .neqv. z)" assert fcode(And(Xor(z, x, evaluate=False), y)) == "y .and. (x .neqv. z)" assert fcode(And(Xor(x, y, evaluate=False), z)) == "z .and. (x .neqv. y)" # mixed Or/Xor assert fcode(Xor(Or(y, z), x, evaluate=False)) == "x .neqv. y .or. z" assert fcode(Xor(Or(z, x), y, evaluate=False)) == "y .neqv. x .or. z" assert fcode(Xor(Or(x, y), z, evaluate=False)) == "z .neqv. x .or. y" assert fcode(Or(Xor(y, z, evaluate=False), x)) == "x .or. (y .neqv. z)" assert fcode(Or(Xor(z, x, evaluate=False), y)) == "y .or. (x .neqv. z)" assert fcode(Or(Xor(x, y, evaluate=False), z)) == "z .or. (x .neqv. y)" # ternary Xor assert fcode(Xor(x, y, z, evaluate=False)) == "x .neqv. y .neqv. z" assert fcode(Xor(x, y, Not(z), evaluate=False)) == "x .neqv. y .neqv. .not. z" assert fcode(Xor(x, Not(y), z, evaluate=False)) == "x .neqv. z .neqv. .not. y" assert fcode(Xor(Not(x), y, z, evaluate=False)) == "y .neqv. z .neqv. .not. x"
def test_Xor(): assert Xor() is False assert Xor(A) == A assert Xor(True) is True assert Xor(False) is False assert Xor(True, True ) is False assert Xor(True, False) is True assert Xor(False, False) is False assert Xor(True, A) == ~A assert Xor(False, A) == A assert Xor(True, False, False) is True assert Xor(True, False, A) == ~A assert Xor(False, False, A) == A
def test_Xor(self): assert Xor(BooleanFunction(Application(), true)).to_nnf() is not true assert isinstance(Xor(A, B), Xor) assert Xor(A, B, Xor(C, D)) == Xor(A, B, C, D) assert Xor(A, B, Xor(B, C)) == Xor(A, C) assert Xor(A < 1, A >= 1, B) == Xor(0, 1, B) == Xor(1, 0, B) e = A > 1 assert Xor(e, e.canonical) == Xor(0, 0) == Xor(1, 1)
from sympy.logic.boolalg import Xor from sympy import symbols Xor(True, False) Xor(True, True) Xor(True, False, True) Xor(True, False, True, False) Xor(True, False, True, False, True) a, b = symbols('a b') a ^ b from sympy.logic.boolalg import Nand Nand(True, False) Nand(True, True) Nand(a, b) from sympy.logic.boolalg import Nor Nor(True, False) Nor(True, True) Nor(False, True) Nor(False, False) Nor(a, b) from sympy.logic.boolalg import Equivalent, And Equivalent(False, False, False) Equivalent(True, False, False) Equivalent(a, And(a, True)) from sympy.logic.boolalg import Implies Implies(False, True) Implies(True, False) Implies(False, False)
def test_true_false(): x = symbols('x') assert true is S.true assert false is S.false assert true is not True assert false is not False assert true assert not false assert true == True assert false == False assert not (true == False) assert not (false == True) assert not (true == false) assert hash(true) == hash(True) assert hash(false) == hash(False) assert len(set([true, True])) == len(set([false, False])) == 1 assert isinstance(true, BooleanAtom) assert isinstance(false, BooleanAtom) # We don't want to subclass from bool, because bool subclasses from # int. But operators like &, |, ^, <<, >>, and ~ act differently on 0 and # 1 then we want them to on true and false. See the docstrings of the # various And, Or, etc. functions for examples. assert not isinstance(true, bool) assert not isinstance(false, bool) # Note: using 'is' comparison is important here. We want these to return # true and false, not True and False assert Not(true) is false assert Not(True) is false assert Not(false) is true assert Not(False) is true assert ~true is false assert ~false is true for T, F in cartes([True, true], [False, false]): assert And(T, F) is false assert And(F, T) is false assert And(F, F) is false assert And(T, T) is true assert And(T, x) == x assert And(F, x) is false if not (T is True and F is False): assert T & F is false assert F & T is false if not F is False: assert F & F is false if not T is True: assert T & T is true assert Or(T, F) is true assert Or(F, T) is true assert Or(F, F) is false assert Or(T, T) is true assert Or(T, x) is true assert Or(F, x) == x if not (T is True and F is False): assert T | F is true assert F | T is true if not F is False: assert F | F is false if not T is True: assert T | T is true assert Xor(T, F) is true assert Xor(F, T) is true assert Xor(F, F) is false assert Xor(T, T) is false assert Xor(T, x) == ~x assert Xor(F, x) == x if not (T is True and F is False): assert T ^ F is true assert F ^ T is true if not F is False: assert F ^ F is false if not T is True: assert T ^ T is false assert Nand(T, F) is true assert Nand(F, T) is true assert Nand(F, F) is true assert Nand(T, T) is false assert Nand(T, x) == ~x assert Nand(F, x) is true assert Nor(T, F) is false assert Nor(F, T) is false assert Nor(F, F) is true assert Nor(T, T) is false assert Nor(T, x) is false assert Nor(F, x) == ~x assert Implies(T, F) is false assert Implies(F, T) is true assert Implies(F, F) is true assert Implies(T, T) is true assert Implies(T, x) == x assert Implies(F, x) is true assert Implies(x, T) is true assert Implies(x, F) == ~x if not (T is True and F is False): assert T >> F is false assert F << T is false assert F >> T is true assert T << F is true if not F is False: assert F >> F is true assert F << F is true if not T is True: assert T >> T is true assert T << T is true assert Equivalent(T, F) is false assert Equivalent(F, T) is false assert Equivalent(F, F) is true assert Equivalent(T, T) is true assert Equivalent(T, x) == x assert Equivalent(F, x) == ~x assert Equivalent(x, T) == x assert Equivalent(x, F) == ~x assert ITE(T, T, T) is true assert ITE(T, T, F) is true assert ITE(T, F, T) is false assert ITE(T, F, F) is false assert ITE(F, T, T) is true assert ITE(F, T, F) is false assert ITE(F, F, T) is true assert ITE(F, F, F) is false
def test_Xor(): assert Xor() is false assert Xor(A) == A assert Xor(A, A) is false assert Xor(True, A, A) is true assert Xor(A, A, A, A, A) == A assert Xor(True, False, False, A, B) == And(Or(A, Not(B)), Or(B, Not(A))) assert Xor(True) is true assert Xor(False) is false assert Xor(True, True) is false assert Xor(True, False) is true assert Xor(False, False) is false assert Xor(True, A) == ~A assert Xor(False, A) == A assert Xor(True, False, False) is true assert Xor(True, False, A) == ~A assert Xor(False, False, A) == A
def test_to_anf(): x, y, z = symbols('x,y,z') assert to_anf(And(x, y)) == And(x, y) assert to_anf(Or(x, y)) == Xor(x, y, And(x, y)) assert to_anf(Or(Implies(x, y), And(x, y), y)) == \ Xor(x, True, x & y, remove_true=False) assert to_anf(Or(Nand(x, y), Nor(x, y), Xnor(x, y), Implies(x, y))) == True assert to_anf(Or(x, Not(y), Nor(x,z), And(x, y), Nand(y, z))) == \ Xor(True, And(y, z), And(x, y, z), remove_true=False) assert to_anf(Xor(x, y)) == Xor(x, y) assert to_anf(Not(x)) == Xor(x, True, remove_true=False) assert to_anf(Nand(x, y)) == Xor(True, And(x, y), remove_true=False) assert to_anf(Nor(x, y)) == Xor(x, y, True, And(x, y), remove_true=False) assert to_anf(Implies(x, y)) == Xor(x, True, And(x, y), remove_true=False) assert to_anf(Equivalent(x, y)) == Xor(x, y, True, remove_true=False) assert to_anf(Nand(x | y, x >> y), deep=False) == \ Xor(True, And(Or(x, y), Implies(x, y)), remove_true=False) assert to_anf(Nor(x ^ y, x & y), deep=False) == \ Xor(True, Or(Xor(x, y), And(x, y)), remove_true=False)
def test_relational_simplification(): w, x, y, z = symbols('w x y z', real=True) d, e = symbols('d e', real=False) # Test all combinations or sign and order assert Or(x >= y, x < y).simplify() == S.true assert Or(x >= y, y > x).simplify() == S.true assert Or(x >= y, -x > -y).simplify() == S.true assert Or(x >= y, -y < -x).simplify() == S.true assert Or(-x <= -y, x < y).simplify() == S.true assert Or(-x <= -y, -x > -y).simplify() == S.true assert Or(-x <= -y, y > x).simplify() == S.true assert Or(-x <= -y, -y < -x).simplify() == S.true assert Or(y <= x, x < y).simplify() == S.true assert Or(y <= x, y > x).simplify() == S.true assert Or(y <= x, -x > -y).simplify() == S.true assert Or(y <= x, -y < -x).simplify() == S.true assert Or(-y >= -x, x < y).simplify() == S.true assert Or(-y >= -x, y > x).simplify() == S.true assert Or(-y >= -x, -x > -y).simplify() == S.true assert Or(-y >= -x, -y < -x).simplify() == S.true assert Or(x < y, x >= y).simplify() == S.true assert Or(y > x, x >= y).simplify() == S.true assert Or(-x > -y, x >= y).simplify() == S.true assert Or(-y < -x, x >= y).simplify() == S.true assert Or(x < y, -x <= -y).simplify() == S.true assert Or(-x > -y, -x <= -y).simplify() == S.true assert Or(y > x, -x <= -y).simplify() == S.true assert Or(-y < -x, -x <= -y).simplify() == S.true assert Or(x < y, y <= x).simplify() == S.true assert Or(y > x, y <= x).simplify() == S.true assert Or(-x > -y, y <= x).simplify() == S.true assert Or(-y < -x, y <= x).simplify() == S.true assert Or(x < y, -y >= -x).simplify() == S.true assert Or(y > x, -y >= -x).simplify() == S.true assert Or(-x > -y, -y >= -x).simplify() == S.true assert Or(-y < -x, -y >= -x).simplify() == S.true # Some other tests assert Or(x >= y, w < z, x <= y).simplify() == S.true assert And(x >= y, x < y).simplify() == S.false assert Or(x >= y, Eq(y, x)).simplify() == (x >= y) assert And(x >= y, Eq(y, x)).simplify() == Eq(x, y) assert And(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y).simplify() == \ (Eq(x, y) & (x >= 1) & (y >= 5) & (y > z)) assert Or(Eq(x, y), x >= y, w < y, z < y).simplify() == \ (x >= y) | (y > z) | (w < y) assert And(Eq(x, y), x >= y, w < y, y >= z, z < y).simplify() == \ Eq(x, y) & (y > z) & (w < y) # assert And(Eq(x, y), x >= y, w < y, y >= z, z < y).simplify(relational_minmax=True) == \ # And(Eq(x, y), y > Max(w, z)) # assert Or(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y).simplify(relational_minmax=True) == \ # (Eq(x, y) | (x >= 1) | (y > Min(2, z))) assert And(Eq(x, y), x >= 1, 2 < y, y >= 5, z < y).simplify() == \ (Eq(x, y) & (x >= 1) & (y >= 5) & (y > z)) assert (Eq(x, y) & Eq(d, e) & (x >= y) & (d >= e)).simplify() == \ (Eq(x, y) & Eq(d, e) & (d >= e)) assert And(Eq(x, y), Eq(x, -y)).simplify() == And(Eq(x, 0), Eq(y, 0)) assert Xor(x >= y, x <= y).simplify() == Ne(x, y) assert And(x > 1, x < -1, Eq(x, y)).simplify() == S.false # From #16690 assert And(x >= y, Eq(y, 0)).simplify() == And(x >= 0, Eq(y, 0))
print(And(x, y).subs(x, 1)) from sympy.logic.boolalg import Not, And, Or from sympy.abc import x, A, B print(Not(True)) print(Not(And(True, False))) print(Not(And(And(True, x), Or(x, False)))) print(Not(And(Or(A, B), Or(~A, ~B)))) #xor ops from sympy.logic.boolalg import Xor from sympy import symbols x, y = symbols('x y') print(Xor(True, False)) print(Xor(True, True)) print(Xor(True, False, True, True, False)) print(x ^ y) #NAND ops from sympy.logic.boolalg import Nand from sympy import symbols x, y = symbols('x y') print(Nand(False, True)) print(Nand(True, True)) print(Nand(x, y)) #Nor ops
def test_count_ops_visual(): ADD, MUL, POW, SIN, COS, EXP, AND, D, G, M = symbols( 'Add Mul Pow sin cos exp And Derivative Integral Sum'.upper()) DIV, SUB, NEG = symbols('DIV SUB NEG') LT, LE, GT, GE, EQ, NE = symbols('LT LE GT GE EQ NE') NOT, OR, AND, XOR, IMPLIES, EQUIVALENT, _ITE, BASIC, TUPLE = symbols( 'Not Or And Xor Implies Equivalent ITE Basic Tuple'.upper()) def count(val): return count_ops(val, visual=True) assert count(7) is S.Zero assert count(S(7)) is S.Zero assert count(-1) == NEG assert count(-2) == NEG assert count(S(2) / 3) == DIV assert count(Rational(2, 3)) == DIV assert count(pi / 3) == DIV assert count(-pi / 3) == DIV + NEG assert count(I - 1) == SUB assert count(1 - I) == SUB assert count(1 - 2 * I) == SUB + MUL assert count(x) is S.Zero assert count(-x) == NEG assert count(-2 * x / 3) == NEG + DIV + MUL assert count(Rational(-2, 3) * x) == NEG + DIV + MUL assert count(1 / x) == DIV assert count(1 / (x * y)) == DIV + MUL assert count(-1 / x) == NEG + DIV assert count(-2 / x) == NEG + DIV assert count(x / y) == DIV assert count(-x / y) == NEG + DIV assert count(x**2) == POW assert count(-x**2) == POW + NEG assert count(-2 * x**2) == POW + MUL + NEG assert count(x + pi / 3) == ADD + DIV assert count(x + S.One / 3) == ADD + DIV assert count(x + Rational(1, 3)) == ADD + DIV assert count(x + y) == ADD assert count(x - y) == SUB assert count(y - x) == SUB assert count(-1 / (x - y)) == DIV + NEG + SUB assert count(-1 / (y - x)) == DIV + NEG + SUB assert count(1 + x**y) == ADD + POW assert count(1 + x + y) == 2 * ADD assert count(1 + x + y + z) == 3 * ADD assert count(1 + x**y + 2 * x * y + y**2) == 3 * ADD + 2 * POW + 2 * MUL assert count(2 * z + y + x + 1) == 3 * ADD + MUL assert count(2 * z + y**17 + x + 1) == 3 * ADD + MUL + POW assert count(2 * z + y**17 + x + sin(x)) == 3 * ADD + POW + MUL + SIN assert count(2 * z + y**17 + x + sin(x**2)) == 3 * ADD + MUL + 2 * POW + SIN assert count(2 * z + y**17 + x + sin(x**2) + exp(cos(x))) == 4 * ADD + MUL + 2 * POW + EXP + COS + SIN assert count(Derivative(x, x)) == D assert count(Integral(x, x) + 2 * x / (1 + x)) == G + DIV + MUL + 2 * ADD assert count(Sum(x, (x, 1, x + 1)) + 2 * x / (1 + x)) == M + DIV + MUL + 3 * ADD assert count(Basic()) is S.Zero assert count({x + 1: sin(x)}) == ADD + SIN assert count([x + 1, sin(x) + y, None]) == ADD + SIN + ADD assert count({x + 1: sin(x), y: cos(x) + 1}) == SIN + COS + 2 * ADD assert count({}) is S.Zero assert count([x + 1, sin(x) * y, None]) == SIN + ADD + MUL assert count([]) is S.Zero assert count(Basic()) == 0 assert count(Basic(Basic(), Basic(x, x + y))) == ADD + 2 * BASIC assert count(Basic(x, x + y)) == ADD + BASIC assert [count(Rel(x, y, op)) for op in '< <= > >= == <> !='.split() ] == [LT, LE, GT, GE, EQ, NE, NE] assert count(Or(x, y)) == OR assert count(And(x, y)) == AND assert count(Or(x, Or(y, And(z, a)))) == AND + OR assert count(Nor(x, y)) == NOT + OR assert count(Nand(x, y)) == NOT + AND assert count(Xor(x, y)) == XOR assert count(Implies(x, y)) == IMPLIES assert count(Equivalent(x, y)) == EQUIVALENT assert count(ITE(x, y, z)) == _ITE assert count([Or(x, y), And(x, y), Basic(x + y)]) == ADD + AND + BASIC + OR assert count(Basic(Tuple(x))) == BASIC + TUPLE #It checks that TUPLE is counted as an operation. assert count(Eq(x + y, S(2))) == ADD + EQ
from sympy.logic.boolalg import ITE, And, Xor, Or from sympy.logic.boolalg import to_cnf, to_dnf from sympy.logic.boolalg import is_cnf, is_dnf from sympy.abc import A, B, C from sympy.abc import X, Y, Z from sympy.abc import a, b, c ITE(True, False, True) ITE(Or(True, False), And(True, True), Xor(True, True)) ITE(a, b, c) ITE(True, a, b) ITE(False, a, b) ITE(a, b, c) to_cnf(~(A | B) | C) to_cnf((A | B) & (A | ~A), True) to_dnf(Y & (X | Z)) to_dnf((X & Y) | (X & ~Y) | (Y & Z) | (~Y & Z), True) is_cnf(X | Y | Z) is_cnf(X & Y & Z) is_cnf((X & Y) | Z) is_cnf(X & (Y | Z)) is_dnf(X | Y | Z) is_dnf(X & Y & Z) is_dnf((X & Y) | Z) is_dnf(X & (Y | Z))
def get_gate_cnf_leq(self): gate_logic = None input_0 = self.gate_inputs[0].symbol if len(self.gate_inputs) > 1: input_1 = self.gate_inputs[1].symbol output = self.gate_output.symbol if re.match("^and", self.gate_type): gate_logic = And(input_0, input_1) if len(self.gate_inputs) > 2: for idx in range(2, len(self.gate_inputs)): gate_logic = And(gate_logic, self.gate_inputs[idx].symbol) # gate_logic = Xor(gate_logic,Not(self.gate_name)) elif re.match("^or", self.gate_type): gate_logic = Or(input_0, input_1) if len(self.gate_inputs) > 2: for idx in range(2, len(self.gate_inputs)): gate_logic = Or(gate_logic, self.gate_inputs[idx].symbol) # gate_logic = Xor(gate_logic,self.gate_name) elif re.match("^xor", self.gate_type): gate_logic = Xor(input_0, input_1) if len(self.gate_inputs) > 2: for idx in range(2, len(self.gate_inputs)): gate_logic = Xor(gate_logic, self.gate_inputs[idx].symbol) # gate_logic = Xor(gate_logic,self.gate_name) elif re.match("^nor", self.gate_type): gate_logic = Or(input_0, input_1) if len(self.gate_inputs) > 2: for idx in range(2, len(self.gate_inputs)): gate_logic = Or(gate_logic, self.gate_inputs[idx].symbol) gate_logic = Not(gate_logic) # gate_logic = Xor(gate_logic,self.gate_name) elif re.match("^nand", self.gate_type): gate_logic = And(input_0, input_1) if len(self.gate_inputs) > 2: for idx in range(2, len(self.gate_inputs)): gate_logic = And(gate_logic, self.gate_inputs[idx].symbol) gate_logic = Not(gate_logic) # gate_logic = Xor(gate_logic,self.gate_name) elif self.gate_type.find("inverter") != -1: gate_logic = Not(input_0) # gate_logic = Xor(gate_logic,self.gate_name) elif self.gate_type.find("buffer") != -1: gate_logic = Not(Not(input_0)) # gate_logic = Xor(gate_logic,self.gate_name) gate_logic = Xor(gate_logic, Not(self.gate_name)) self.cnf_leq = Equivalent(output, gate_logic) # print(self.cnf) self.cnf_leq = to_cnf(self.cnf_leq)
def test_Xor(): A, B, C = symbols('ABC') assert Xor() == False assert Xor(A) == A assert Xor(True) == True assert Xor(False) == False assert Xor(True, True ) == False assert Xor(True, False) == True assert Xor(False, False) == False assert Xor(True, A) == ~A assert Xor(False, A) == A assert Xor(True, False, False) == True assert Xor(True, False, A) == ~A assert Xor(False, False, A) == A
assert not os.path.exists(args.cnf_file_path), ('File already exists:', args.cnf_file_path) from sympy.logic.boolalg import ITE, And, Xor, Or, Not import sympy from sympy.logic.boolalg import to_cnf num_xors = int(args.num_variables * args.xor_density) variables = sympy.symbols(','.join(map(str, range(1, args.num_variables+1)))) xors = [] for _ in range(num_xors): xor_vars = random.sample(variables, args.xor_num_vars) for i in range(len(xor_vars)): if np.random.random() < 0.5: xor_vars[i] = Not(xor_vars[i]) xor = Xor(*xor_vars) if np.random.random() < 0.5: xor = Not(xor) xors.append(xor) formula = And(*xors) cnf_formula = to_cnf(formula) with open(args.cnf_file_path, 'w') as f: print >>f, 'p cnf %d %d' % (args.num_variables, len(cnf_formula.args)) for clause in cnf_formula.args: for variable in clause.args: if isinstance(variable, sympy.Symbol): print >>f, variable.name, else: print >>f, '-%s' % (variable.args[0].name),
def test_fcode_precedence(): assert fcode(And(x < y, y < x + 1)) == "x < y .and. y < x + 1" assert fcode(Or(x < y, y < x + 1)) == "x < y .or. y < x + 1" assert fcode(Xor(x < y, y < x + 1, evaluate=False)) == "x < y .neqv. y < x + 1" assert fcode(Equivalent(x < y, y < x + 1)) == "x < y .eqv. y < x + 1"
def test_distribute(): assert distribute_and_over_or(Or(And(A, B), C)) == And(Or(A, C), Or(B, C)) assert distribute_or_over_and(And(A, Or(B, C))) == Or(And(A, B), And(A, C)) assert distribute_xor_over_and(And(A, Xor(B, C))) == Xor(And(A, B), And(A, C))
def test_functions_in_assumptions(): from sympy.logic.boolalg import Equivalent, Xor x = symbols('x') assert ask(x, Q.negative, Q.real(x) >> Q.positive(x)) is False assert ask(x, Q.negative, Equivalent(Q.real(x), Q.positive(x))) is False assert ask(x, Q.negative, Xor(Q.real(x), Q.negative(x))) is False
def test_sympy__logic__boolalg__Xor(): from sympy.logic.boolalg import Xor assert _test_args(Xor(x, y, 2))
def xor(): from sympy.logic.boolalg import Xor from sympy import symbols x, y = symbols('x y') expr = input(("Enter a logic expression to perform XOR operation")) print(Xor(expr))
def test_Xor(): assert Xor() is false assert Xor(A) == A assert Xor(A, A) is false assert Xor(True, A, A) is true assert Xor(A, A, A, A, A) == A assert Xor(True, False, False, A, B) == ~Xor(A, B) assert Xor(True) is true assert Xor(False) is false assert Xor(True, True) is false assert Xor(True, False) is true assert Xor(False, False) is false assert Xor(True, A) == ~A assert Xor(False, A) == A assert Xor(True, False, False) is true assert Xor(True, False, A) == ~A assert Xor(False, False, A) == A assert isinstance(Xor(A, B), Xor) assert Xor(A, B, Xor(C, D)) == Xor(A, B, C, D) assert Xor(A, B, Xor(B, C)) == Xor(A, C) assert Xor(A < 1, A >= 1, B) == Xor(0, 1, B) == Xor(1, 0, B) e = A > 1 assert Xor(e, e.canonical) == Xor(0, 0) == Xor(1, 1)
def test_Xor(): assert str(Xor(y, x, evaluate=False)) == "x ^ y"
def test_Xor(): assert Xor() is false assert Xor(A) == A assert Xor(A, A) is false assert Xor(True, A, A) is true assert Xor(A, A, A, A, A) == A assert Xor(True, False, False, A, B) == ~Xor(A, B) assert Xor(True) is true assert Xor(False) is false assert Xor(True, True) is false assert Xor(True, False) is true assert Xor(False, False) is false assert Xor(True, A) == ~A assert Xor(False, A) == A assert Xor(True, False, False) is true assert Xor(True, False, A) == ~A assert Xor(False, False, A) == A assert isinstance(Xor(A, B), Xor) assert Xor(A, B, Xor(C, D)) == Xor(A, B, C, D) assert Xor(A, B, Xor(B, C)) == Xor(A, C)
def test_xor(self): A, B = self.alpha_symbols[:2] expr = Xor(A, B) model = mental_model_builder(expr, Insight.INTUITIVE) npt.assert_allclose( model.model, np.array([[POS_VAL, IMPL_NEG], [IMPL_NEG, POS_VAL]]))