Forming logical expressions
>>> from sympy import * >>> x, y = symbols('x,y') >>> y | (x & y) Or(And(x, y), y) >>> x | y Or(x, y) >>> ~x Not(x) >>> x >> y Implies(x, y) >>> x << y Implies(y, x) >>> (y & x).subs({x: True, y: True}) True >>> (x | y).atoms() set([x, y])
sympy.logic.boolalg.SOPform(variables, minterms, dontcares=None)
#sum of products >>> from sympy.logic import SOPform >>> from sympy import symbols >>> w, x, y, z = symbols('w x y z') >>> 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]] >>> SOPform([w, x, y, z], minterms, dontcares) Or(And(Not(w), z), And(y, z))
sympy.logic.boolalg.POSform(variables, minterms, dontcares=None)
#products of sum >>> from sympy.logic import POSform >>> from sympy import symbols >>> w, x, y, z = symbols('w x y z') >>> 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]] >>> POSform([w, x, y, z], minterms, dontcares) And(Or(Not(w), y), z)
Boolean functions
#python 的True 与 sympy的 true 1 >>> from sympy import sympify, true, Or >>> sympify(True) True >>> ~true False >>> ~True -2 >>> Or(True, False) True #python 的False与 sympy的false 0 >>> from sympy import sympify, false, Or, true >>> sympify(False) False >>> false >> false True >>> False >> False 0 >>> Or(True, False) True
Simplification and equivalence-testing
>>> from sympy.logic import simplify_logic >>> from sympy.abc import x, y, z >>> from sympy import S >>> b = (~x & ~y & ~z) | ( ~x & ~y & z) >>> simplify_logic(b) And(Not(x), Not(y)) >>> S(b) Or(And(Not(x), Not(y), Not(z)), And(Not(x), Not(y), z)) >>> simplify_logic(_) And(Not(x), Not(y))