예제 #1
0
def test_equals():
    assert Not(Or(A, B)).equals( And(Not(A), Not(B)) ) is True
    assert Equivalent(A, B).equals((A >> B) & (B >> A)) is True
    assert ((A | ~B) & (~A | B)).equals((~A & ~B) | (A & B)) is True
    assert (A >> B).equals(~A >> ~B) is False
    assert (A >> (B >> A)).equals(A >> (C >> A)) is False
    pytest.raises(NotImplementedError, lambda: And(A, A < B).equals(And(A, B > A)))
예제 #2
0
def test_bool_as_set():
    assert And(x <= 2, x >= -2).as_set() == Interval(-2, 2)
    assert Or(x >= 2, x <= -2).as_set() == (Interval(-oo, -2, True) +
                                            Interval(2, oo, False, True))
    assert Not(x > 2, evaluate=False).as_set() == Interval(-oo, 2, True)
    # issue sympy/sympy#10240
    assert Not(And(x > 2, x < 3)).as_set() == \
        Union(Interval(-oo, 2, True), Interval(3, oo, False, True))
    assert true.as_set() == S.UniversalSet
    assert false.as_set() == EmptySet()
예제 #3
0
def test_basic():
    assert lambdarepr(x * y) == "x*y"
    assert lambdarepr(x + y) in ["y + x", "x + y"]
    assert lambdarepr(x**y) == "x**y"
    assert lambdarepr(And(x, y)) == "((x) and (y))"
    assert lambdarepr(Or(x, y)) == "((x) or (y))"
    assert lambdarepr(Not(x)) == "(not (x))"
    assert lambdarepr(false) == "False"
예제 #4
0
def test_to_cnf():

    assert to_cnf(~(B | C)) == And(Not(B), Not(C))
    assert to_cnf((A & B) | C) == And(Or(A, C), Or(B, C))
    assert to_cnf(A >> B) == (~A) | B
    assert to_cnf(A >> (B & C)) == (~A | B) & (~A | C)
    assert to_cnf(A & (B | C) | ~A & (B | C), True) == B | C

    assert to_cnf(Equivalent(A, B)) == And(Or(A, Not(B)), Or(B, Not(A)))
    assert to_cnf(Equivalent(A, B & C)) == \
        (~A | B) & (~A | C) & (~B | ~C | A)
    assert to_cnf(Equivalent(A, B | C), True) == \
        And(Or(Not(B), A), Or(Not(C), A), Or(B, C, Not(A)))
    assert to_cnf(~(A | B) | C) == And(Or(Not(A), C), Or(Not(B), C))
예제 #5
0
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)
예제 #6
0
def test_piecewise_fold_piecewise_in_cond():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (p1 / Abs(p1), True))
    p3 = piecewise_fold(p2)
    assert (p2.subs(x, -pi / 2) == 0.0)
    assert (p2.subs(x, 1) == 0.0)
    assert (p2.subs(x, -pi / 4) == 1.0)
    p4 = Piecewise((0, Eq(p1, 0)), (1, True))
    assert (piecewise_fold(p4) == Piecewise(
        (0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))), (1, True)))

    r1 = 1 < Piecewise((1, x < 1), (3, True))
    assert (piecewise_fold(r1) == Not(x < 1))

    p5 = Piecewise((1, x < 0), (3, True))
    p6 = Piecewise((1, x < 1), (3, True))
    p7 = piecewise_fold(Piecewise((1, p5 < p6), (0, True)))
    assert (Piecewise((1, And(Not(x < 1), x < 0)), (0, True)))
예제 #7
0
def test_numpy_logical_ops():
    and_func = lambdify((x, y), And(x, y), modules="numpy")
    or_func = lambdify((x, y), Or(x, y), modules="numpy")
    not_func = lambdify((x), Not(x), modules="numpy")
    arr1 = numpy.array([True, True])
    arr2 = numpy.array([False, True])
    numpy.testing.assert_array_equal(and_func(arr1, arr2), numpy.array([False, True]))
    numpy.testing.assert_array_equal(or_func(arr1, arr2), numpy.array([True, True]))
    numpy.testing.assert_array_equal(not_func(arr2), numpy.array([True, False]))
예제 #8
0
def test_basic():
    assert lambdarepr(x * y) == 'x*y'
    assert lambdarepr(x + y) in ['y + x', 'x + y']
    assert lambdarepr(x**y) == 'x**y'
    assert lambdarepr(And(x, y)) == '((x) and (y))'
    assert lambdarepr(Or(x, y)) == '((x) or (y))'
    assert lambdarepr(Not(x)) == '(not (x))'
    assert lambdarepr(false) == 'False'
    assert lambdarepr(Dummy('f(x)')) == '_f_lpar_x_rpar_'
예제 #9
0
def test_piecewise_fold_piecewise_in_cond():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (p1 / abs(p1), True))
    assert p2.subs({x: -pi / 2}) == 0.0
    assert p2.subs({x: 1}) == 0.0
    assert p2.subs({x: -pi / 4}) == 1.0
    p4 = Piecewise((0, Eq(p1, 0)), (1, True))
    assert (piecewise_fold(p4) == Piecewise(
        (0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))), (1, True)))

    r1 = 1 < Piecewise((1, x < 1), (3, True))
    assert (piecewise_fold(r1) == Not(x < 1))

    p5 = Piecewise((1, x < 0), (3, True))
    p6 = Piecewise((1, x < 1), (3, True))
    p7 = piecewise_fold(Piecewise((1, p5 < p6), (0, True)))
    assert p7
    assert Piecewise((1, And(Not(x < 1), x < 0)), (0, True))
예제 #10
0
def test_Not():
    pytest.raises(TypeError, lambda: Not(True, False))
    assert Not(True) is false
    assert Not(False) is true
    assert Not(0) is true
    assert Not(1) is false
    assert Not(2) is false
    assert Not(Unequality(a, b)) == Equality(a, b)
예제 #11
0
def test_is_nnf():
    assert is_nnf(true) is True
    assert is_nnf(A) is True
    assert is_nnf(~A) is True
    assert is_nnf(A & B) is True
    assert is_nnf((A & B) | (~A & A) | (~B & B) | (~A & ~B), False) is True
    assert is_nnf((A | B) & (~A | ~B)) is True
    assert is_nnf(Not(Or(A, B))) is False
    assert is_nnf(A ^ B) is False
    assert is_nnf((A & B) | (~A & A) | (~B & B) | (~A & ~B), True) is False
예제 #12
0
def test_to_dnf():
    assert to_dnf(true) == true
    assert to_dnf((~B) & (~C)) == (~B) & (~C)
    assert to_dnf(~(B | C)) == And(Not(B), Not(C))
    assert to_dnf(A & (B | C)) == Or(And(A, B), And(A, C))
    assert to_dnf(A >> B) == (~A) | B
    assert to_dnf(A >> (B & C)) == (~A) | (B & C)

    assert to_dnf(Equivalent(A, B), True) == \
        Or(And(A, B), And(Not(A), Not(B)))
    assert to_dnf(Equivalent(A, B & C), True) == \
        Or(And(A, B, C), And(Not(A), Not(B)), And(Not(A), Not(C)))
예제 #13
0
def test_Equivalent():

    assert Equivalent(A, B) == Equivalent(B, A) == Equivalent(A, B, A)
    assert Equivalent() is true
    assert Equivalent(A, A) == Equivalent(A) is true
    assert Equivalent(True, True) == Equivalent(False, False) is true
    assert Equivalent(True, False) == Equivalent(False, True) is false
    assert Equivalent(A, True) == A
    assert Equivalent(A, False) == Not(A)
    assert Equivalent(A, B, True) == A & B
    assert Equivalent(A, B, False) == ~A & ~B
    assert Equivalent(1, A) == A
    assert Equivalent(0, A) == Not(A)
    assert Equivalent(A, Equivalent(B, C)) != Equivalent(Equivalent(A, B), C)
    assert Equivalent(A < 1, A >= 1) is false
    assert Equivalent(A < 1, A >= 1, 0) is false
    assert Equivalent(A < 1, A >= 1, 1) is false
    assert Equivalent(A < 1, Integer(1) > A) == Equivalent(1, 1) == Equivalent(0, 0)
    assert Equivalent(A < 1, B >= 1) == Equivalent(B >= 1, A < 1, evaluate=False)
예제 #14
0
def test_to_nnf():
    assert to_nnf(true) is true
    assert to_nnf(false) is false
    assert to_nnf(A) == A
    assert (~A).to_nnf() == ~A

    class Boo(BooleanFunction):
        pass

    pytest.raises(ValueError, lambda: to_nnf(~Boo(A)))

    assert to_nnf(A | ~A | B) is true
    assert to_nnf(A & ~A & B) is false
    assert to_nnf(A >> B) == ~A | B
    assert to_nnf(Equivalent(A, B, C)) == (~A | B) & (~B | C) & (~C | A)
    assert to_nnf(A ^ B ^ C) == \
        (A | B | C) & (~A | ~B | C) & (A | ~B | ~C) & (~A | B | ~C)
    assert to_nnf(ITE(A, B, C)) == (~A | B) & (A | C)
    assert to_nnf(Not(A | B | C)) == ~A & ~B & ~C
    assert to_nnf(Not(A & B & C)) == ~A | ~B | ~C
    assert to_nnf(Not(A >> B)) == A & ~B
    assert to_nnf(Not(Equivalent(A, B, C))) == And(Or(A, B, C), Or(~A, ~B, ~C))
    assert to_nnf(Not(A ^ B ^ C)) == \
        (~A | B | C) & (A | ~B | C) & (A | B | ~C) & (~A | ~B | ~C)
    assert to_nnf(Not(ITE(A, B, C))) == (~A | ~B) & (A | ~C)
    assert to_nnf((A >> B) ^ (B >> A)) == (A & ~B) | (~A & B)
    assert to_nnf((A >> B) ^ (B >> A), False) == \
        (~A | ~B | A | B) & ((A & ~B) | (~A & B))
예제 #15
0
def test_Not():
    assert Not(Equality(x, y)) == Unequality(x, y)
    assert Not(Unequality(x, y)) == Equality(x, y)
    assert Not(StrictGreaterThan(x, y)) == LessThan(x, y)
    assert Not(StrictLessThan(x, y)) == GreaterThan(x, y)
    assert Not(GreaterThan(x, y)) == StrictLessThan(x, y)
    assert Not(LessThan(x, y)) == StrictGreaterThan(x, y)
예제 #16
0
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))
예제 #17
0
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)
예제 #18
0
def test_ITE():
    A, B, C = map(Boolean, symbols('A,B,C'))

    pytest.raises(ValueError, lambda: ITE(A, B))

    assert ITE(True, False, True) is false
    assert ITE(True, True, False) is true
    assert ITE(False, True, False) is false
    assert ITE(False, False, True) is true
    assert isinstance(ITE(A, B, C), ITE)

    assert ITE(True, B, C) == B
    assert ITE(False, B, C) == C

    assert ITE(A, B, B) == B

    assert ITE(C, False, True) == Not(C)
    assert ITE(C, True, False) == C
예제 #19
0
def test_Equivalent():
    assert Equivalent(a, b) == Equivalent(b, a) == Equivalent(a, b, a)
    assert Equivalent() is true
    assert Equivalent(a, a) == Equivalent(a) is true
    assert Equivalent(True, True) == Equivalent(False, False) is true
    assert Equivalent(True, False) == Equivalent(False, True) is false
    assert Equivalent(a, True) == a
    assert Equivalent(a, False) == ~a
    assert Equivalent(a, b, True) == a & b
    assert Equivalent(a, b, False) == ~a & ~b
    assert Equivalent(1, a) == a
    assert Equivalent(0, a) == Not(a)
    assert Equivalent(a, Equivalent(b, c)) != Equivalent(Equivalent(a, b), c)
    assert Equivalent(a < 1, a >= 1) is false
    assert Equivalent(a < 1, a >= 1, 0) is false
    assert Equivalent(a < 1, a >= 1, 1) is false
    assert Equivalent(a < 1, 1 > a) == Equivalent(1, 1) == Equivalent(0, 0)
    assert Equivalent(a < 1, Integer(1) > a) == Equivalent(1, 1) == Equivalent(0, 0)
    assert Equivalent(a < 1, b >= 1) == Equivalent(b >= 1, a < 1, evaluate=False)
예제 #20
0
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
예제 #21
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'
예제 #22
0
def test_fcode_Logical():
    # unary Not
    assert fcode(Not(x), source_format='free') == '.not. x'
    # binary And
    assert fcode(And(x, y), source_format='free') == 'x .and. y'
    assert fcode(And(x, Not(y)), source_format='free') == 'x .and. .not. y'
    assert fcode(And(Not(x), y), source_format='free') == 'y .and. .not. x'
    assert fcode(And(Not(x), Not(y)), source_format='free') == \
        '.not. x .and. .not. y'
    assert fcode(Not(And(x, y), evaluate=False), source_format='free') == \
        '.not. (x .and. y)'
    # binary Or
    assert fcode(Or(x, y), source_format='free') == 'x .or. y'
    assert fcode(Or(x, Not(y)), source_format='free') == 'x .or. .not. y'
    assert fcode(Or(Not(x), y), source_format='free') == 'y .or. .not. x'
    assert fcode(Or(Not(x), Not(y)), source_format='free') == \
        '.not. x .or. .not. y'
    assert fcode(Not(Or(x, y), evaluate=False), source_format='free') == \
        '.not. (x .or. y)'
    # mixed And/Or
    assert fcode(And(Or(y, z), x),
                 source_format='free') == 'x .and. (y .or. z)'
    assert fcode(And(Or(z, x), y),
                 source_format='free') == 'y .and. (x .or. z)'
    assert fcode(And(Or(x, y), z),
                 source_format='free') == 'z .and. (x .or. y)'
    assert fcode(Or(And(y, z), x), source_format='free') == 'x .or. y .and. z'
    assert fcode(Or(And(z, x), y), source_format='free') == 'y .or. x .and. z'
    assert fcode(Or(And(x, y), z), source_format='free') == 'z .or. x .and. y'
    # trinary And
    assert fcode(And(x, y, z), source_format='free') == 'x .and. y .and. z'
    assert fcode(And(x, y, Not(z)), source_format='free') == \
        'x .and. y .and. .not. z'
    assert fcode(And(x, Not(y), z), source_format='free') == \
        'x .and. z .and. .not. y'
    assert fcode(And(Not(x), y, z), source_format='free') == \
        'y .and. z .and. .not. x'
    assert fcode(Not(And(x, y, z), evaluate=False), source_format='free') == \
        '.not. (x .and. y .and. z)'
    # trinary Or
    assert fcode(Or(x, y, z), source_format='free') == 'x .or. y .or. z'
    assert fcode(Or(x, y, Not(z)), source_format='free') == \
        'x .or. y .or. .not. z'
    assert fcode(Or(x, Not(y), z), source_format='free') == \
        'x .or. z .or. .not. y'
    assert fcode(Or(Not(x), y, z), source_format='free') == \
        'y .or. z .or. .not. x'
    assert fcode(Not(Or(x, y, z), evaluate=False), source_format='free') == \
        '.not. (x .or. y .or. z)'
예제 #23
0
def test_simplification():
    """Test working of simplification methods."""
    set1 = [[0, 0, 1], [0, 1, 1], [1, 0, 0], [1, 1, 0]]
    set2 = [[0, 0, 0], [0, 1, 0], [1, 0, 1], [1, 1, 1]]
    assert SOPform([x, y, z], set1) == Or(And(Not(x), z), And(Not(z), x))
    assert Not(SOPform([x, y, z],
                       set2)) == Not(Or(And(Not(x), Not(z)), And(x, z)))
    assert POSform([x, y, z], set1 + set2) is true
    assert SOPform([x, y, z], set1 + set2) is true
    assert SOPform([Dummy(), Dummy(), Dummy()], set1 + set2) is true

    minterms = [[0, 0, 0, 1], [0, 0, 1, 1], [0, 1, 1, 1], [1, 0, 1, 1],
                [1, 1, 1, 1]]
    dontcares = [[0, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 1]]
    assert (SOPform([w, x, y, z], minterms,
                    dontcares) == Or(And(Not(w), z), And(y, z)))
    assert POSform([w, x, y, z], minterms, dontcares) == And(Or(Not(w), y), z)

    # test simplification
    ans = And(A, Or(B, C))
    assert simplify_logic(A & (B | C)) == ans
    assert simplify_logic((A & B) | (A & C)) == ans
    assert simplify_logic(Implies(A, B)) == Or(Not(A), B)
    assert simplify_logic(Equivalent(A, B)) == \
        Or(And(A, B), And(Not(A), Not(B)))
    assert simplify_logic(And(Equality(A, 2), C)) == And(Equality(A, 2), C)
    assert simplify_logic(And(Equality(A, 2), A)) == And(Equality(A, 2), A)
    assert simplify_logic(And(Equality(A, B), C)) == And(Equality(A, B), C)
    assert simplify_logic(Or(And(Equality(A, 3), B), And(Equality(A, 3), C))) \
        == And(Equality(A, 3), Or(B, C))
    e = And(A, x**2 - x)
    assert simplify_logic(e) == And(A, x * (x - 1))
    assert simplify_logic(e, deep=False) == e
    pytest.raises(ValueError, lambda: simplify_logic(A & (B | C), form='spam'))

    e = x & y ^ z | (z ^ x)
    res = [(x & ~z) | (z & ~x) | (z & ~y), (x & ~y) | (x & ~z) | (z & ~x)]
    assert simplify_logic(e) in res
    assert SOPform(
        [z, y, x],
        [[0, 0, 1], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0]]) == res[1]

    # check input
    ans = SOPform([x, y], [[1, 0]])
    assert SOPform([x, y], [[1, 0]]) == ans
    assert POSform([x, y], [[1, 0]]) == ans

    pytest.raises(ValueError, lambda: SOPform([x], [[1]], [[1]]))
    assert SOPform([x], [[1]], [[0]]) is true
    assert SOPform([x], [[0]], [[1]]) is true
    assert SOPform([x], [], []) is false

    pytest.raises(ValueError, lambda: POSform([x], [[1]], [[1]]))
    assert POSform([x], [[1]], [[0]]) is true
    assert POSform([x], [[0]], [[1]]) is true
    assert POSform([x], [], []) is false

    # check working of simplify
    assert simplify((A & B) | (A & C)) == And(A, Or(B, C))
    assert simplify(And(x, Not(x))) is false
    assert simplify(Or(x, Not(x))) is true
예제 #24
0
def test_bool_as_set():
    assert ((x <= 2) & (x >= -2)).as_set() == Interval(-2, 2)
    assert ((x >= 2) | (x <= -2)).as_set() == (Interval(-oo, -2) + Interval(2, oo, False))
    assert Not(x > 2, evaluate=False).as_set() == Interval(-oo, 2, True)
    assert true.as_set() == S.UniversalSet
    assert false.as_set() == EmptySet()
예제 #25
0
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
예제 #26
0
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)
예제 #27
0
def test_eliminate_implications():
    assert eliminate_implications(Implies(A, B, evaluate=False)) == (~A) | B
    assert eliminate_implications(A >> (C >> Not(B))) == Or(
        Or(Not(B), Not(C)), Not(A))
    assert eliminate_implications(Equivalent(A, B, C, D)) == \
        (~A | B) & (~B | C) & (~C | D) & (~D | A)
예제 #28
0
def test_piecewise_fold_piecewise_in_cond_2():
    p1 = Piecewise((cos(x), x < 0), (0, True))
    p2 = Piecewise((0, Eq(p1, 0)), (1 / p1, True))
    p3 = Piecewise((0, Or(And(Eq(cos(x), 0), x < 0), Not(x < 0))),
                   (1 / cos(x), True))
    assert (piecewise_fold(p2) == p3)
예제 #29
0
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
예제 #30
0
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]])) is not 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(And(x, Not(y)), Or(y, Not(x))) is False
    assert bool_map(And(x, Not(y)), And(y, Not(x), z)) is False
    assert bool_map(And(x, Not(y)), And(Or(y, z), Not(x))) is False
    assert bool_map(Or(And(Not(y), a), And(Not(y), b), And(x, y)), Or(
        x, y, a)) is False