def test_to_dnf(): 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(A | B) == A | B 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))) assert to_dnf(A + 1) == A + 1
def get_known_facts(): return And( Implies(Q.infinite, ~Q.finite), Implies(Q.real, Q.complex), Implies(Q.real, Q.hermitian), Equivalent(Q.extended_real, Q.real | Q.infinite), Equivalent(Q.even | Q.odd, Q.integer), Implies(Q.even, ~Q.odd), Equivalent(Q.prime, Q.integer & Q.positive & ~Q.composite), Implies(Q.integer, Q.rational), Implies(Q.rational, Q.algebraic), Implies(Q.irrational, Q.finite), Implies(Q.algebraic, Q.complex), Implies(Q.algebraic, Q.finite), Equivalent(Q.transcendental | Q.algebraic, Q.complex & Q.finite), Implies(Q.transcendental, ~Q.algebraic), Implies(Q.transcendental, Q.finite), Implies(Q.imaginary, Q.complex & ~Q.real), Implies(Q.imaginary, Q.antihermitian), Implies(Q.antihermitian, ~Q.hermitian), Equivalent(Q.irrational | Q.rational, Q.real & Q.finite), Implies(Q.irrational, ~Q.rational), Implies(Q.zero, Q.even), Equivalent(Q.real, Q.negative | Q.zero | Q.positive), Implies(Q.zero, ~Q.negative & ~Q.positive), Implies(Q.negative, ~Q.positive), Equivalent(Q.nonnegative, Q.zero | Q.positive), Equivalent(Q.nonpositive, Q.zero | Q.negative), Equivalent(Q.nonzero, Q.negative | Q.positive), Implies(Q.orthogonal, Q.positive_definite), Implies(Q.orthogonal, Q.unitary), Implies(Q.unitary & Q.real, Q.orthogonal), Implies(Q.unitary, Q.normal), Implies(Q.unitary, Q.invertible), Implies(Q.normal, Q.square), Implies(Q.diagonal, Q.normal), Implies(Q.positive_definite, Q.invertible), Implies(Q.diagonal, Q.upper_triangular), Implies(Q.diagonal, Q.lower_triangular), Implies(Q.lower_triangular, Q.triangular), Implies(Q.upper_triangular, Q.triangular), Implies(Q.triangular, Q.upper_triangular | Q.lower_triangular), Implies(Q.upper_triangular & Q.lower_triangular, Q.diagonal), Implies(Q.diagonal, Q.symmetric), Implies(Q.unit_triangular, Q.triangular), Implies(Q.invertible, Q.fullrank), Implies(Q.invertible, Q.square), Implies(Q.symmetric, Q.square), Implies(Q.fullrank & Q.square, Q.invertible), Equivalent(Q.invertible, ~Q.singular), Implies(Q.integer_elements, Q.real_elements), Implies(Q.real_elements, Q.complex_elements), )
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)))
def test_dpll_satisfiable(): A, B, C = symbols('A,B,C') assert dpll_satisfiable( A & ~A ) is False assert dpll_satisfiable( A & ~B ) == {A: True, B: False} assert dpll_satisfiable( A | B ) in ({A: True}, {B: True}, {A: True, B: True}) assert dpll_satisfiable( (~A | B) & (~B | A) ) in ({A: True, B: True}, {A: False, B: False}) assert dpll_satisfiable( (A | B) & (~B | C) ) in ({A: True, B: False}, {A: True, C: True}, {B: True, C: True}) assert dpll_satisfiable( A & B & C ) == {A: True, B: True, C: True} assert dpll_satisfiable( (A | B) & (A >> B) ) == {B: True} assert dpll_satisfiable( Equivalent(A, B) & A ) == {A: True, B: True} assert dpll_satisfiable( Equivalent(A, B) & ~A ) == {A: False, B: False}
def test_print_logic(): assert mpp.doprint(And( x, y)) == '<mrow><mi>x</mi><mo>∧</mo><mi>y</mi></mrow>' assert mpp.doprint(Or( x, y)) == '<mrow><mi>x</mi><mo>∨</mo><mi>y</mi></mrow>' assert mpp.doprint(Xor( x, y)) == '<mrow><mi>x</mi><mo>⊻</mo><mi>y</mi></mrow>' assert mpp.doprint(Implies( x, y)) == '<mrow><mi>x</mi><mo>⇒</mo><mi>y</mi></mrow>' assert mpp.doprint(Equivalent( x, y)) == '<mrow><mi>x</mi><mo>⇔</mo><mi>y</mi></mrow>' assert mpp.doprint( And(Eq(x, y), x > 4) ) == '<mrow><mrow><mi>x</mi><mo>=</mo><mi>y</mi></mrow><mo>∧</mo><mrow><mi>x</mi><mo>></mo><mn>4</mn></mrow></mrow>' assert mpp.doprint( And(Eq(x, 3), y < 3, x > y + 1) ) == '<mrow><mrow><mi>x</mi><mo>=</mo><mn>3</mn></mrow><mo>∧</mo><mrow><mi>x</mi><mo>></mo><mrow><mi>y</mi><mo>+</mo><mn>1</mn></mrow></mrow><mo>∧</mo><mrow><mi>y</mi><mo><</mo><mn>3</mn></mrow></mrow>' assert mpp.doprint( Or(Eq(x, y), x > 4) ) == '<mrow><mrow><mi>x</mi><mo>=</mo><mi>y</mi></mrow><mo>∨</mo><mrow><mi>x</mi><mo>></mo><mn>4</mn></mrow></mrow>' assert mpp.doprint( And(Eq(x, 3), Or(y < 3, x > y + 1)) ) == '<mrow><mrow><mi>x</mi><mo>=</mo><mn>3</mn></mrow><mo>∧</mo><mfenced><mrow><mrow><mi>x</mi><mo>></mo><mrow><mi>y</mi><mo>+</mo><mn>1</mn></mrow></mrow><mo>∨</mo><mrow><mi>y</mi><mo><</mo><mn>3</mn></mrow></mrow></mfenced></mrow>' assert mpp.doprint(Not(x)) == '<mrow><mo>¬</mo><mi>x</mi></mrow>' assert mpp.doprint( Not(And(x, y)) ) == '<mrow><mo>¬</mo><mfenced><mrow><mi>x</mi><mo>∧</mo><mi>y</mi></mrow></mfenced></mrow>'
def test_eliminate_implications(): from sympy.abc import A, B, C, D 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)
def test_composite_proposition(): from sympy.logic.boolalg import Equivalent, Implies x = symbols('x') assert ask(True) is True assert ask(~Q.negative(x), Q.positive(x)) is True assert ask(~Q.real(x), Q.commutative(x)) is None assert ask(Q.negative(x) & Q.integer(x), Q.positive(x)) is False assert ask(Q.negative(x) & Q.integer(x)) is None assert ask(Q.real(x) | Q.integer(x), Q.positive(x)) is True assert ask(Q.real(x) | Q.integer(x)) is None assert ask(Q.real(x) >> Q.positive(x), Q.negative(x)) is False assert ask(Implies(Q.real(x), Q.positive(x), evaluate=False), Q.negative(x)) is False assert ask(Implies(Q.real(x), Q.positive(x), evaluate=False)) is None assert ask(Equivalent(Q.integer(x), Q.even(x)), Q.even(x)) is True assert ask(Equivalent(Q.integer(x), Q.even(x))) is None assert ask(Equivalent(Q.positive(x), Q.integer(x)), Q.integer(x)) is None
def _(expr): # Including the integer qualification means we don't need to add any facts # for odd, since the assumptions already know that every integer is # exactly one of even or odd. allargs_integer = allargs(x, Q.integer(x), expr) anyarg_even = anyarg(x, Q.even(x), expr) return Implies(allargs_integer, Equivalent(anyarg_even, Q.even(expr)))
def test_PropKB(): A, B, C = symbols('A,B,C') kb = PropKB() kb.tell(A >> B) kb.tell(B >> C) assert kb.ask(A) is True assert kb.ask(B) is True assert kb.ask(C) is True assert kb.ask(~A) is True assert kb.ask(~B) is True assert kb.ask(~C) is True kb.tell(A) assert kb.ask(A) is True assert kb.ask(B) is True assert kb.ask(C) is True assert kb.ask(~C) is False kb.retract(A) assert kb.ask(~C) is True kb2 = PropKB(Equivalent(A, B)) assert kb2.ask(A) is True assert kb2.ask(B) is True kb2.tell(A) assert kb2.ask(A) is True kb3 = PropKB() kb3.tell(A)
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 raises(NotImplementedError, lambda: (A & B).equals(A > B))
def test_satisfiable_all_models(): from sympy.abc import A, B assert next(satisfiable(False, all_models=True)) is False assert list(satisfiable((A >> ~A) & A, all_models=True)) == [False] assert list(satisfiable(True, all_models=True)) == [{true: true}] models = [{A: True, B: False}, {A: False, B: True}] result = satisfiable(A ^ B, all_models=True) models.remove(next(result)) models.remove(next(result)) raises(StopIteration, lambda: next(result)) assert not models assert list(satisfiable(Equivalent(A, B), all_models=True)) == \ [{A: False, B: False}, {A: True, B: True}] models = [{A: False, B: False}, {A: False, B: True}, {A: True, B: True}] for model in satisfiable(A >> B, all_models=True): models.remove(model) assert not models # This is a santiy test to check that only the required number # of solutions are generated. The expr below has 2**100 - 1 models # which would time out the test if all are generated at once. from sympy import numbered_symbols from sympy.logic.boolalg import Or sym = numbered_symbols() X = [next(sym) for i in range(100)] result = satisfiable(Or(*X), all_models=True) for i in range(10): assert next(result)
def toEnc1(nodes): """ Creates the ENC 1 encoding of the given nodes """ cnf = [] for node in nodes: cnf += create_indicator_cnf(node) parents = node.getParents() cond_list = [node.getStates()] for parent in parents: cond_list.append(parent.getStates()) pairs = get_combinations(cond_list) nodes = [node] + parents # create parameter clauses for pair in pairs: par_var = create_conditional_var(node, pair[0], pair[1:], parents) rl = par_var[0] ll = And(*[create_var(nodes[i], s)[0] for i, s in enumerate(pair)]) cnf.append(Equivalent(ll, rl)) return And(*cnf)
def _(expr): arg = expr.args[0] return [Q.nonnegative(expr), Equivalent(~Q.zero(arg), ~Q.zero(expr)), Q.even(arg) >> Q.even(expr), Q.odd(arg) >> Q.odd(expr), Q.integer(arg) >> Q.integer(expr), ]
def _(expr): ret = [] for p, getter in _old_assump_getters.items(): pred = p(expr) prop = getter(expr) if prop is not None: ret.append(Equivalent(pred, prop)) return ret
def _(expr): base, exp = expr.base, expr.exp return [ (Q.real(base) & Q.even(exp) & Q.nonnegative(exp)) >> Q.nonnegative(expr), (Q.nonnegative(base) & Q.odd(exp) & Q.nonnegative(exp)) >> Q.nonnegative(expr), (Q.nonpositive(base) & Q.odd(exp) & Q.nonnegative(exp)) >> Q.nonpositive(expr), Equivalent(Q.zero(expr), Q.zero(base) & Q.positive(exp)) ]
def _(expr): return [Equivalent(Q.zero(expr), anyarg(x, Q.zero(x), expr)), allarg(x, Q.positive(x), expr) >> Q.positive(expr), allarg(x, Q.real(x), expr) >> Q.real(expr), allarg(x, Q.rational(x), expr) >> Q.rational(expr), allarg(x, Q.integer(x), expr) >> Q.integer(expr), exactlyonearg(x, ~Q.rational(x), expr) >> ~Q.integer(expr), allarg(x, Q.commutative(x), expr) >> Q.commutative(expr), ]
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]] from sympy.abc import w, x, y, z assert SOPform('xyz', set1) == Or(And(Not(x), z), And(Not(z), x)) assert Not(SOPform('xyz', set2)) == Not(Or(And(Not(x), Not(z)), And(x, z))) assert POSform('xyz', set1 + set2) is true assert SOPform('xyz', 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('wxyz', minterms, dontcares) == Or(And(Not(w), z), And(y, z))) assert POSform('wxyz', 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 # check input ans = SOPform('xy', [[1, 0]]) assert SOPform([x, y], [[1, 0]]) == ans assert POSform(['x', 'y'], [[1, 0]]) == ans 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 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)') == sympify('And(A, Or(B, C))') assert simplify(And(x, Not(x))) == False assert simplify(Or(x, Not(x))) == True
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)) is S.false 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)) b = (~x & ~y & ~z) | ( ~x & ~y & z) e = And(A, b) assert simplify_logic(e) == A & ~x & ~y # check input ans = SOPform([x, y], [[1, 0]]) assert SOPform([x, y], [[1, 0]]) == ans assert POSform([x, y], [[1, 0]]) == ans 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 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))) == False assert simplify(Or(x, Not(x))) == True
def test_fcode_precedence(): x, y = symbols("x y") 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_to_nnf(): assert to_nnf(true) is true assert to_nnf(false) is false assert to_nnf(A) == 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))
def match(h_task, curr, cand): """ match if the label of the second state equals :param h_task: :param curr: parent node :param cand: child node :return: yes if there exists the same subtask """ for edge in h_task.edges(): if (edge[0].x == curr.x and edge[1].x == cand.x) or (edge[1].x == curr.x and edge[0].x == cand.x) \ and Equivalent(edge[1].label, cand.label): return True return False
def match(subtask_lib, subtask_new): """ whether subtask_lib equals subtask_new :param subtask_lib: :param subtask_new: :return: """ if Equivalent(subtask_lib[0].label, subtask_new[0].label): if subtask_lib[0].x == subtask_new[0].x and subtask_lib[1].x == subtask_new[1].x: return 'forward' elif subtask_lib[0].x == subtask_new[1].x and subtask_lib[1].x == subtask_new[0].x: return 'backward' return ''
def get_gate_cnf(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) 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) 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) 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) 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) elif self.gate_type.find("inverter") != -1: gate_logic = Not(input_0) elif self.gate_type.find("buffer") != -1: gate_logic = Not(Not(input_0)) self.cnf = Implies(self.symbol, Equivalent(output, gate_logic)) # print(self.cnf) self.cnf = to_cnf(self.cnf)
def test_pl_true(): A, B, C = symbols('A,B,C') assert pl_true(True) is True assert pl_true(A & B, {A: True, B: True}) is True assert pl_true(A | B, {A: True}) is True assert pl_true(A | B, {B: True}) is True assert pl_true(A | B, {A: None, B: True}) is True assert pl_true(A >> B, {A: False}) is True assert pl_true(A | B | ~C, {A: False, B: True, C: True}) is True assert pl_true(Equivalent(A, B), {A: False, B: False}) is True # test for false assert pl_true(False) is False assert pl_true(A & B, {A: False, B: False}) is False assert pl_true(A & B, {A: False}) is False assert pl_true(A & B, {B: False}) is False assert pl_true(A | B, {A: False, B: False}) is False #test for None assert pl_true(B, {B: None}) is None assert pl_true(A & B, {A: True, B: None}) is None assert pl_true(A >> B, {A: True, B: None}) is None assert pl_true(Equivalent(A, B), {A: None}) is None assert pl_true(Equivalent(A, B), {A: True, B: None}) is None
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_count_ops_non_visual(): def count(val): return count_ops(val, visual=False) assert count(x) == 0 assert count(x) is not S.Zero assert count(x + y) == 1 assert count(x + y) is not S.One assert count(x + y * x + 2 * y) == 4 assert count({x + y: x}) == 1 assert count({x + y: S(2) + x}) is not S.One assert count(x < y) == 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 expand(self, formula, order, add_on_finish=True): """ Updates entrenchment ranking for belief base expansion. Params: - add_on_finish: If true, the formula will be added to the belief base after the ranking has been updated. """ x = to_cnf(formula) _validate_order(order) logger.debug(f'Expanding with {x} and order {order}') if not entails([], ~x): # If x is a contradiction, ignore if entails([], x): # If x is a tautology, assign order = 1 order = 1 else: for belief in self.beliefs: y = belief.formula if belief.order > order: # Don't change beliefs of higher order continue # Degree of implication x -> y d = self.degree(x >> y) if (entails([], Equivalent(x, y)) # if |- (x <-> y) or belief.order <= order < d): logger.debug(f'{belief} raised to order {order}') self._add_reorder_queue(belief, order) else: self._add_reorder_queue(belief, d) self._run_reorder_queue() if add_on_finish: self.add(x, order) logger.debug(f'New belief base:\n{self}')
'positive' : ['sympy.assumptions.handlers.order.AskPositiveHandler'], 'prime' : ['sympy.assumptions.handlers.ntheory.AskPrimeHandler'], 'real' : ['sympy.assumptions.handlers.sets.AskRealHandler'], 'odd' : ['sympy.assumptions.handlers.ntheory.AskOddHandler'], 'algebraic' : ['sympy.assumptions.handlers.sets.AskAlgebraicHandler'], 'is_true' : ['sympy.assumptions.handlers.TautologicalHandler'] } for name, value in _handlers_dict.iteritems(): register_handler(name, value[0]) known_facts_keys = [getattr(Q, attr) for attr in Q.__dict__ \ if not attr.startswith('__')] known_facts = And( Implies (Q.real, Q.complex), Equivalent(Q.even, Q.integer & ~Q.odd), Equivalent(Q.extended_real, Q.real | Q.infinity), Equivalent(Q.odd, Q.integer & ~Q.even), Equivalent(Q.prime, Q.integer & Q.positive & ~Q.composite), Implies (Q.integer, Q.rational), Implies (Q.imaginary, Q.complex & ~Q.real), Equivalent(Q.negative, Q.nonzero & ~Q.positive), Equivalent(Q.positive, Q.nonzero & ~Q.negative), Equivalent(Q.rational, Q.real & ~Q.irrational), Equivalent(Q.real, Q.rational | Q.irrational), Implies (Q.nonzero, Q.real), Equivalent(Q.nonzero, Q.positive | Q.negative) ) ################################################################################ # Note: The following facts are generated by the compute_known_facts function. #
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_equal(): 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