def test_sympify3(): assert sympify("x**3") == x**3 assert sympify("x^3") == x**3 assert sympify("x^3", convert_xor=False) == Xor(x, 3) assert sympify("1/2") == Rational(1, 2) pytest.raises(SympifyError, lambda: sympify('x**3', strict=True)) pytest.raises(SympifyError, lambda: sympify('1/2', strict=True))
def test_fcode_precedence(): assert fcode(And(x < y, y < x + 1), source_format='free') == \ 'x < y .and. y < x + 1' assert fcode(Or(x < y, y < x + 1), source_format='free') == \ 'x < y .or. y < x + 1' assert fcode(Xor(x < y, y < x + 1, evaluate=False), source_format='free') == 'x < y .neqv. y < x + 1' assert fcode(Equivalent(x < y, y < x + 1), source_format='free') == \ 'x < y .eqv. y < x + 1'
def test_overloading(): """Test that |, & are overloaded as expected.""" assert A & B == And(A, B) assert A | B == Or(A, B) assert (A & B) | C == Or(And(A, B), C) assert A >> B == Implies(A, B) assert A << B == Implies(B, A) assert ~A == Not(A) assert A ^ B == Xor(A, B)
def test_relational_logic_symbols(): # See issue sympy/sympy#6204 assert (x < y) & (z < t) == And(x < y, z < t) assert (x < y) | (z < t) == Or(x < y, z < t) assert ~(x < y) == Not(x < y) assert (x < y) >> (z < t) == Implies(x < y, z < t) assert (x < y) << (z < t) == Implies(z < t, x < y) assert (x < y) ^ (z < t) == Xor(x < y, z < t) assert isinstance((x < y) & (z < t), And) assert isinstance((x < y) | (z < t), Or) assert isinstance(~(x < y), GreaterThan) assert isinstance((x < y) >> (z < t), Implies) assert isinstance((x < y) << (z < t), Implies) assert isinstance((x < y) ^ (z < t), (Or, Xor))
def test_operators(): # Mostly test __and__, __rand__, and so on assert True & A == (A & True) == A assert False & A == (A & False) == false assert A & B == And(A, B) assert True | A == (A | True) == true assert False | A == (A | False) == A assert A | B == Or(A, B) assert ~A == Not(A) assert True >> A == (A << True) == A assert False >> A == (A << False) == true assert (A >> True) == (True << A) == true assert (A >> False) == (False << A) == ~A assert A >> B == B << A == Implies(A, B) assert True ^ A == A ^ True == ~A assert False ^ A == (A ^ False) == A assert A ^ B == Xor(A, B)
def test_count_ops_non_visual(): def count(val): return count_ops(val, visual=False) assert count(x) == 0 assert count(x) is not Integer(0) assert count(x + y) == 1 assert count(x + y) is not Integer(1) assert count(x + y*x + 2*y) == 4 assert count({x + y: x}) == 1 assert count({x + y: 2 + x}) is not Integer(1) assert count(Or(x, y)) == 1 assert count(And(x, y)) == 1 assert count(Not(x)) == 1 assert count(Nor(x, y)) == 2 assert count(Nand(x, y)) == 2 assert count(Xor(x, y)) == 1 assert count(Implies(x, y)) == 1 assert count(Equivalent(x, y)) == 1 assert count(ITE(x, y, z)) == 1 assert count(ITE(True, x, y)) == 0
def test_fcode_Xlogical(): # binary Xor assert fcode(Xor(x, y, evaluate=False), source_format='free') == \ 'x .neqv. y' assert fcode(Xor(x, Not(y), evaluate=False), source_format='free') == \ 'x .neqv. .not. y' assert fcode(Xor(Not(x), y, evaluate=False), source_format='free') == \ 'y .neqv. .not. x' assert fcode(Xor(Not(x), Not(y), evaluate=False), source_format='free') == '.not. x .neqv. .not. y' assert fcode(Not(Xor(x, y, evaluate=False), evaluate=False), source_format='free') == '.not. (x .neqv. y)' # binary Equivalent assert fcode(Equivalent(x, y), source_format='free') == 'x .eqv. y' assert fcode(Equivalent(x, Not(y)), source_format='free') == \ 'x .eqv. .not. y' assert fcode(Equivalent(Not(x), y), source_format='free') == \ 'y .eqv. .not. x' assert fcode(Equivalent(Not(x), Not(y)), source_format='free') == \ '.not. x .eqv. .not. y' assert fcode(Not(Equivalent(x, y), evaluate=False), source_format='free') == '.not. (x .eqv. y)' # mixed And/Equivalent assert fcode(Equivalent(And(y, z), x), source_format='free') == \ 'x .eqv. y .and. z' assert fcode(Equivalent(And(z, x), y), source_format='free') == \ 'y .eqv. x .and. z' assert fcode(Equivalent(And(x, y), z), source_format='free') == \ 'z .eqv. x .and. y' assert fcode(And(Equivalent(y, z), x), source_format='free') == \ 'x .and. (y .eqv. z)' assert fcode(And(Equivalent(z, x), y), source_format='free') == \ 'y .and. (x .eqv. z)' assert fcode(And(Equivalent(x, y), z), source_format='free') == \ 'z .and. (x .eqv. y)' # mixed Or/Equivalent assert fcode(Equivalent(Or(y, z), x), source_format='free') == \ 'x .eqv. y .or. z' assert fcode(Equivalent(Or(z, x), y), source_format='free') == \ 'y .eqv. x .or. z' assert fcode(Equivalent(Or(x, y), z), source_format='free') == \ 'z .eqv. x .or. y' assert fcode(Or(Equivalent(y, z), x), source_format='free') == \ 'x .or. (y .eqv. z)' assert fcode(Or(Equivalent(z, x), y), source_format='free') == \ 'y .or. (x .eqv. z)' assert fcode(Or(Equivalent(x, y), z), source_format='free') == \ 'z .or. (x .eqv. y)' # mixed Xor/Equivalent assert fcode(Equivalent(Xor(y, z, evaluate=False), x), source_format='free') == 'x .eqv. (y .neqv. z)' assert fcode(Equivalent(Xor(z, x, evaluate=False), y), source_format='free') == 'y .eqv. (x .neqv. z)' assert fcode(Equivalent(Xor(x, y, evaluate=False), z), source_format='free') == 'z .eqv. (x .neqv. y)' assert fcode(Xor(Equivalent(y, z), x, evaluate=False), source_format='free') == 'x .neqv. (y .eqv. z)' assert fcode(Xor(Equivalent(z, x), y, evaluate=False), source_format='free') == 'y .neqv. (x .eqv. z)' assert fcode(Xor(Equivalent(x, y), z, evaluate=False), source_format='free') == 'z .neqv. (x .eqv. y)' # mixed And/Xor assert fcode(Xor(And(y, z), x, evaluate=False), source_format='free') == \ 'x .neqv. y .and. z' assert fcode(Xor(And(z, x), y, evaluate=False), source_format='free') == \ 'y .neqv. x .and. z' assert fcode(Xor(And(x, y), z, evaluate=False), source_format='free') == \ 'z .neqv. x .and. y' assert fcode(And(Xor(y, z, evaluate=False), x), source_format='free') == \ 'x .and. (y .neqv. z)' assert fcode(And(Xor(z, x, evaluate=False), y), source_format='free') == \ 'y .and. (x .neqv. z)' assert fcode(And(Xor(x, y, evaluate=False), z), source_format='free') == \ 'z .and. (x .neqv. y)' # mixed Or/Xor assert fcode(Xor(Or(y, z), x, evaluate=False), source_format='free') == \ 'x .neqv. y .or. z' assert fcode(Xor(Or(z, x), y, evaluate=False), source_format='free') == \ 'y .neqv. x .or. z' assert fcode(Xor(Or(x, y), z, evaluate=False), source_format='free') == \ 'z .neqv. x .or. y' assert fcode(Or(Xor(y, z, evaluate=False), x), source_format='free') == \ 'x .or. (y .neqv. z)' assert fcode(Or(Xor(z, x, evaluate=False), y), source_format='free') == \ 'y .or. (x .neqv. z)' assert fcode(Or(Xor(x, y, evaluate=False), z), source_format='free') == \ 'z .or. (x .neqv. y)' # trinary Xor assert fcode(Xor(x, y, z, evaluate=False), source_format='free') == \ 'x .neqv. y .neqv. z' assert fcode(Xor(x, y, Not(z), evaluate=False), source_format='free') == \ 'x .neqv. y .neqv. .not. z' assert fcode(Xor(x, Not(y), z, evaluate=False), source_format='free') == \ 'x .neqv. z .neqv. .not. y' assert fcode(Xor(Not(x), y, z, evaluate=False), source_format='free') == \ 'y .neqv. z .neqv. .not. x'
def test_Xor(): assert str(Xor(y, x, evaluate=False)) == "Xor(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) 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) e = Integer(1) < a assert e != e.canonical and Xor(e, e.canonical) is false assert Xor(a > 1, b > c) == Xor(a > 1, b > c, evaluate=False)
def test_true_false(): # pylint: disable=singleton-comparison,comparison-with-itself assert true is true assert false is false assert true is not True assert false is not False assert true assert not false assert true == True # noqa: E712 assert false == False # noqa: E712 assert not true == False # noqa: E712 assert not false == True # noqa: E712 assert not true == false assert hash(true) == hash(True) assert hash(false) == hash(False) assert len({true, True}) == len({false, False}) == 1 assert int(true) == 1 assert int(false) == 0 assert isinstance(true, BooleanAtom) assert isinstance(false, BooleanAtom) assert not isinstance(true, bool) assert not isinstance(false, bool) assert ~true is false assert Not(True) is false assert ~false is true assert Not(False) is true for T, F in itertools.product([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 F is not False: assert F & F is false if T is not 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 F is not False: assert F | F is false if T is not 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 F is not False: assert F ^ F is false if T is not 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 F is not False: assert F >> F is true assert F << F is true if T is not 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_overloading(): assert a & b == And(a, b) assert a | b == Or(a, b) assert a >> b == Implies(a, b) assert ~a == Not(a) assert a ^ b == Xor(a, b)
def test_count_ops_visual(): ADD, MUL, POW, SIN, COS, AND, D, G = symbols( 'Add Mul Pow sin cos And Derivative Integral'.upper()) DIV, SUB, NEG = symbols('DIV SUB NEG') 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 Integer(0) assert count(-1) == NEG assert count(-2) == NEG 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 Integer(0) assert count(-x) == NEG assert count(-2 * x / 3) == 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 + 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 + 3 * POW + COS + SIN assert count(Derivative(x, x)) == D assert count(Integral(x, x) + 2 * x / (1 + x)) == G + DIV + MUL + 2 * ADD assert count(Basic()) is Integer(0) 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 Integer(0) assert count([x + 1, sin(x) * y, None]) == SIN + ADD + MUL assert count([]) is Integer(0) 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(Or(x, y)) == OR assert count(And(x, y)) == AND assert count(And(x**y, z)) == AND + POW 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, 2)) == ADD
def test_logic(): p = NumPyPrinter() assert p.doprint(And(a, b, c)) == 'logical_and(logical_and(a, b), c)' assert p.doprint(Or(a, b, c)) == 'logical_or(logical_or(a, b), c)' assert p.doprint(Xor(a, b, c)) == 'logical_xor(logical_xor(a, b), c)'
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) e = Integer(1) < A assert e != e.canonical and Xor(e, e.canonical) is false assert Xor(A > 1, B > C) == Xor(A > 1, B > C, evaluate=False)
def test_true_false(): assert true is true assert false is false assert true is not True assert false is not False assert true assert not false assert true == True # noqa: E712 assert false == False # noqa: E712 assert not (true == False) # noqa: E712 assert not (false == True) # noqa: E712 assert not (true == false) assert hash(true) == hash(True) assert hash(false) == hash(False) assert len({true, True}) == len({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 itertools.product([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 F is not False: assert F & F is false if T is not 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 F is not False: assert F | F is false if T is not 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 F is not False: assert F ^ F is false if T is not 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 F is not False: assert F >> F is true assert F << F is true if T is not 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