def test_is_dnf(): x, y, z = symbols('x,y,z') assert is_dnf(x) is True assert is_dnf(x | y | z) is True assert is_dnf(x & y & z) is True assert is_dnf((x & y) | z) is True assert is_dnf((x | y) & z) is False
def test_is_dnf(): assert is_dnf(x) is True assert is_dnf(x | y | z) is True assert is_dnf(x & y & z) is True assert is_dnf((x & y) | z) is True assert is_dnf((x | y) & z) is False assert is_dnf(~(x | y) & z) is False
def parse_gpr_rule(rule, prefix=None): if not rule: return None rule = rule.replace('(', '( ').replace(')', ' )') def replacement(token): if token.lower() == 'and': return '&' elif token.lower() == 'or': return '|' elif token == '(' or token == ')': return token elif prefix is not None and not token.startswith(prefix): return prefix + sanitize_id(token) else: return sanitize_id(token) rule = ' '.join(map(replacement, rule.split())) expr = parse_expr(rule) if not is_dnf(expr): expr = to_dnf(expr) gpr = GPRAssociation() if type(expr) is Or: for sub_expr in expr.args: protein = Protein() if type(sub_expr) is And: protein.genes = [str(gene) for gene in sub_expr.args] else: protein.genes = [str(sub_expr)] gpr.proteins.append(protein) elif type(expr) is And: protein = Protein() protein.genes = [str(gene) for gene in expr.args] gpr.proteins = [protein] else: protein = Protein() protein.genes = [str(expr)] gpr.proteins = [protein] return gpr
def _process_expr(self): self._num_vars = len(self._expr.binary_symbols) self._lit_to_var = [None] + sorted(self._expr.binary_symbols, key=str) self._var_to_lit = dict( zip(self._lit_to_var[1:], range(1, self._num_vars + 1))) if self._optimization or (not is_cnf(self._expr) and not is_dnf(self._expr)): expr = simplify_logic(self._expr) else: expr = self._expr if isinstance(expr, BooleanTrue): ast = 'const', 1 elif isinstance(expr, BooleanFalse): ast = 'const', 0 else: ast = get_ast(self._var_to_lit, expr) if ast[0] == 'or': self._nf = DNF(ast, num_vars=self._num_vars) else: self._nf = CNF(ast, num_vars=self._num_vars)
def checkdnf(): from sympy.logic.boolalg import is_dnf from sympy.abc import A, B, C expr = input(("Enter a logic expression to check for DNF")) print(is_dnf(expr))
from sympy.abc import A, B, D print(to_cnf(~(A | B) | D)) print(to_cnf((A | B) & (A | ~A), True)) #dnf from sympy.logic.boolalg import to_dnf from sympy.abc import A, B, C print(to_dnf(B & (A | C))) print(to_dnf((A & B) | (A & ~B) | (B & C) | (~B & C), True)) #to check cnf and dnf from sympy.logic.boolalg import is_cnf from sympy.abc import A, B, C from sympy.logic.boolalg import is_dnf from sympy.abc import A, B, C print(is_cnf(A | B | C)) print(is_cnf((A & B) | C)) print(is_dnf(A | B | C)) print(is_dnf(A & (B | C))) #simplify logic from sympy.logic import simplify_logic from sympy.abc import x, y, z from sympy import S b = (~x & ~y & ~z) | (~x & ~y & z) print(simplify_logic(b))
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))