What are the Logical Expressions in Sympy?

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.
Python3
# import packagesfrom sympy import truefrom sympy import S, sympify# prints Trueprint(true)# prints Trueprint(S.true)# prints trueprint(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.
Python3
# import packagesfrom sympy import falsefrom sympy import S, sympify# prints Falseprint(false)# prints Falseprint(S.false)# prints Falseprint(sympify(false)) |
Output:
False False False
negation of true is false, and negation of false is true.
Python3
# import packagesfrom sympy import false, trueprint(~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
Python3
# import packagesfrom sympy.abc import x, yfrom sympy.logic.boolalg import Andres = x & yprint(res)# 1&y ==yprint(And(x, y).subs(x, 1))# 0&y ==0 or Falseprint(And(x, y).subs(x, 0))# True&False == Falseprint(And(True, False))# True & True == Trueprint(And(True, True))# False & False == Falseprint(And(False, False))# False & True == Falseprint(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
Python3
# import packagesfrom sympy.abc import x, yfrom sympy.logic.boolalg import Orres = x | yprint(res)# 1&y == 1 or Trueprint(Or(x, y).subs(x, 1))# 0|y ==yprint(Or(x, y).subs(x, 0))# True | False == Trueprint(Or(True, False))# True | True == Trueprint(Or(True, True))# False | False == Falseprint(Or(False, False))# False | True == Trueprint(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.
Python3
# import packagesfrom sympy.abc import xfrom sympy.logic.boolalg import Not# nor formulaprint(Not(x))# ~True == Trueprint(Not(True))# ~False == Falseprint(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.
Python3
# import packagesfrom sympy.abc import x, yfrom sympy.logic.boolalg import Nornor_formula = ~(x | y)print(nor_formula)print(Nor(x, y))# ~( True | False) == Falseprint(Nor(True, False))# ~(True | True) == Falseprint(Nor(True, True))# ~(False | False) == Trueprint(Nor(False, False))# ~(False | True) == Falseprint(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.
Python3
# import packagesfrom sympy.abc import x, yfrom sympy.logic.boolalg import Nand# not + nand == nandnor_formula = ~(x & y)print(nor_formula)print(Nand(x, y))# ~( True & False) == Trueprint(Nand(True, False))# ~(True & True) == Falseprint(Nand(True, True))# ~(False & False) == Trueprint(Nand(False, False))# ~(False & True) == Trueprint(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.
Python3
# import packagesfrom sympy.abc import x, yfrom sympy.logic.boolalg import Xorxor_formula = x ^ yprint(xor_formula)print(Xor(x, y))# True ^ False == Trueprint(Xor(True, False))# True ^ True == Falseprint(Xor(True, True))# False ^ False == Falseprint(Xor(False, False))# False ^ True == Trueprint(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.
Python3
# import packagesfrom sympy.abc import x, yfrom sympy.logic.boolalg import Xnorxnor_formula = ~(x ^ y)print(xnor_formula)print(Xnor(x,y))# ~(True ^ False) == Falseprint(Xnor(True, False))# ~(True ^ True) == Trueprint(Xnor(True, True))# ~(False ^ True) == False print(Xnor(False, True))# ~(False ^ False) == Trueprint(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.
Python3
from sympy.abc import x, yfrom sympy.logic.boolalg import Implies# !A v B == implies formula# returns false when A is true and B is # false, rest all cases returns Trueprint(Implies(x, y))# falseprint(Implies(True, False))# trueprint(Implies(True, True))# trueprint(Implies(False, False))# trueprint(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.
Python3
from sympy.abc import x, y, zfrom sympy.logic.boolalg import Equivalent, And, Orprint(Equivalent(x, y, z))# true != false so it returns falseprint(Equivalent(True, False))# True == True so it returns trueprint(Equivalent(True, True))# False == False so it returns trueprint(Equivalent(False, False))# False !=True so it returns falseprint(Equivalent(False, True))# true ==true == true so it returns trueprint(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.
Python3
from sympy.abc import x, y, zfrom sympy.logic.boolalg import ITE, Nor, Nand, Xor, Or, And# ITE == if then elseprint(ITE(x, y, z))# x is true so y is returnedprint(ITE(True, Or(True, False), And(True, True)))# x is false so z is returnedprint(ITE(Nor(True, False), Xor(True, False), Nand(True, True))) |
Output:
ITE(x, y, z) True False



