SymPy is a symbolic mathematics Python package. Its goal is to develop into a completely featured computer algebra system while keeping the code as basic as possible to make it understandable and extendable. The package is entirely written in python language. Logical expressions in sympy are expressed by using boolean functions. sympy.basic.booleanarg module of sympy contains boolean functions.
The common Python operators & (And), | (Or), and ~ (Not) can be used to create Boolean expressions. >> and can also be used to create implications. other boolean operations or gates are NAND, NOR, XOR, etc.
Boolean True:
sympy.logic.boolalg.BooleanTrue
Boolean True in SymPy is a singleton that can be accessed with S.true, or by directly importing True or by importing simplify from the sympy package.
# import packages
from sympy import true
from sympy import S, sympify
# prints True
print(true)
# prints True
print(S.true)
# prints true
print(sympify(true))
Output:
True True True
Boolean False:
sympy.logic.boolalg.BooleanFalse
Boolean False in SymPy is a singleton that can be accessed with S.false, or by directly importing false or by importing simplify from the sympy package.
# import packages
from sympy import false
from sympy import S, sympify
# prints False
print(false)
# prints False
print(S.false)
# prints False
print(sympify(false))
Output:
False False False
negation of true is false, and negation of false is true.
# import packages
from sympy import false, true
print(~true,~false)
Output:
False True
Boolean And:
sympy.logic.boolalg.And
It analyzes each of its arguments in sequence, it returns true if all of the arguments are true. if at least one argument is false, false is returned.
- true &true = true
- true&false = false
- false & true = false
- false&false = false
# import packages
from sympy.abc import x, y
from sympy.logic.boolalg import And
res = x & y
print(res)
# 1&y ==y
print(And(x, y).subs(x, 1))
# 0&y ==0 or False
print(And(x, y).subs(x, 0))
# True&False == False
print(And(True, False))
# True & True == True
print(And(True, True))
# False & False == False
print(And(False, False))
# False & True == False
print(And(False, True))
Output:
x & y y False False True False False
Boolean Or:
sympy.logic.boolalg.Or
If any of the arguments is true, True is returned or else false is returned.
- true |true = true
- true|false = true
- false | true = true
- false|false = false
# import packages
from sympy.abc import x, y
from sympy.logic.boolalg import Or
res = x | y
print(res)
# 1&y == 1 or True
print(Or(x, y).subs(x, 1))
# 0|y ==y
print(Or(x, y).subs(x, 0))
# True | False == True
print(Or(True, False))
# True | True == True
print(Or(True, True))
# False | False == False
print(Or(False, False))
# False | True == True
print(Or(False, True))
Output:
x | y True y True True False True
Boolean Not:
sympy.logic.boolalg.Not(arg)
Not represents negation. If the statement is False, this method returns True. If the assertion is true, it returns False.
# import packages
from sympy.abc import x
from sympy.logic.boolalg import Not
# nor formula
print(Not(x))
# ~True == True
print(Not(True))
# ~False == False
print(Not(False))
Output:
~x False True
Boolean Nor:
sympy.logic.boolalg.Nor(*args)
Nor is a conjunction of Not and Or. Nor = Not+Or. It examines each argument in turn, returning False if any of them are True and True if all of them are False. If any argument is True, returns False. If all arguments are False, this function returns True.
# import packages
from sympy.abc import x, y
from sympy.logic.boolalg import Nor
nor_formula = ~(x | y)
print(nor_formula)
print(Nor(x, y))
# ~( True | False) == False
print(Nor(True, False))
# ~(True | True) == False
print(Nor(True, True))
# ~(False | False) == True
print(Nor(False, False))
# ~(False | True) == False
print(Nor(False, True))
Output:
~(x | y) ~(x | y) False False True False
Boolean Nand:
sympy.logic.boolalg.Nand(*args)
Nand is a conjunction of Not and. Nor = Not+And. It analyses each of its inputs in succession, returning True if any of them are False and False if all of them are True. If any of the inputs are False, this function returns True. If all arguments are True, returns False.
# import packages
from sympy.abc import x, y
from sympy.logic.boolalg import Nand
# not + nand == nand
nor_formula = ~(x & y)
print(nor_formula)
print(Nand(x, y))
# ~( True & False) == True
print(Nand(True, False))
# ~(True & True) == False
print(Nand(True, True))
# ~(False & False) == True
print(Nand(False, False))
# ~(False & True) == True
print(Nand(False, True))
Output:
~(x & y) ~(x & y) True False True True
Boolean Xor:
sympy.logic.boolalg.Xor(*args)
Xor represents Logical XOR or Exclusive Or function. If an odd number of the arguments are True and the others are False, this function returns True. If an even number of the arguments are True and the others are False, the result is False.
# import packages
from sympy.abc import x, y
from sympy.logic.boolalg import Xor
xor_formula = x ^ y
print(xor_formula)
print(Xor(x, y))
# True ^ False == True
print(Xor(True, False))
# True ^ True == False
print(Xor(True, True))
# False ^ False == False
print(Xor(False, False))
# False ^ True == True
print(Xor(False, True))
Output:
x ^ y x ^ y True False False True
Boolean Xnor:
sympy.logic.boolalg.Xnor(*args)
Exclusive-NOR gate or XNOR gate is formed by combining the Exclusive-OR gate (XOR gate) and the NOT gate.
Returns False if an odd number of the arguments are True and the rest are False. Returns True if an even number of the arguments are True and the rest are False.
# import packages
from sympy.abc import x, y
from sympy.logic.boolalg import Xnor
xnor_formula = ~(x ^ y)
print(xnor_formula)
print(Xnor(x,y))
# ~(True ^ False) == False
print(Xnor(True, False))
# ~(True ^ True) == True
print(Xnor(True, True))
# ~(False ^ True) == False
print(Xnor(False, True))
# ~(False ^ False) == True
print(Xnor(False, False))
Output:
~(x ^ y) ~(x ^ y) False True False True
Boolean Implies:
sympy.logic.boolalg.Implies(*args)
Implies refer to Logical implications. x implies y is equivalent to !x v y. Accepts x and y as Boolean inputs. If x is True and y is False, returns False. Otherwise, True is returned.
from sympy.abc import x, y
from sympy.logic.boolalg import Implies
# !A v B == implies formula
# returns false when A is true and B is
# false, rest all cases returns True
print(Implies(x, y))
# false
print(Implies(True, False))
# true
print(Implies(True, True))
# true
print(Implies(False, False))
# true
print(Implies(False, True))
print(x << y)
print(x >> y)
Output:
Implies(x, y) False True True True Implies(y, x) Implies(x, y)
Boolean Equivalent
sympy.logic.boolalg.Equivalent(*args)
Refers to an Equivalence relation. If x and y are both True or False, Equivalent(x, y) is True. If all of the arguments are logically equivalent, True is returned. Otherwise, False is returned.
from sympy.abc import x, y, z
from sympy.logic.boolalg import Equivalent, And, Or
print(Equivalent(x, y, z))
# true != false so it returns false
print(Equivalent(True, False))
# True == True so it returns true
print(Equivalent(True, True))
# False == False so it returns true
print(Equivalent(False, False))
# False !=True so it returns false
print(Equivalent(False, True))
# true ==true == true so it returns true
print(Equivalent(True, Or(True, False), And(True, True)))
Output:
Equivalent(x, y, z) False True True False True
Boolean ITE:
sympy.logic.boolalg.ITE(*args)
ITE refers to the If then else clause. If A is true, ITE(x, y, z) evaluates and returns the result of y; otherwise, ITE(x, y, z) evaluates and returns the result of z. All of the arguments must be Booleans.
from sympy.abc import x, y, z
from sympy.logic.boolalg import ITE, Nor, Nand, Xor, Or, And
# ITE == if then else
print(ITE(x, y, z))
# x is true so y is returned
print(ITE(True, Or(True, False), And(True, True)))
# x is false so z is returned
print(ITE(Nor(True, False), Xor(True, False), Nand(True, True)))
Output:
ITE(x, y, z) True False