What are the Logical Expressions in Sympy?
Last Updated :
28 Mar, 2023
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
from sympy import true
from sympy import S, sympify
print (true)
print (S.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.
Python3
from sympy import false
from sympy import S, sympify
print (false)
print (S.false)
print (sympify(false))
|
Output:
False
False
False
negation of true is false, and negation of false is true.
Python3
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
Python3
from sympy.abc import x, y
from sympy.logic.boolalg import And
res = x & y
print (res)
print (And(x, y).subs(x, 1 ))
print (And(x, y).subs(x, 0 ))
print (And( True , False ))
print (And( True , True ))
print (And( False , 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
Python3
from sympy.abc import x, y
from sympy.logic.boolalg import Or
res = x | y
print (res)
print (Or(x, y).subs(x, 1 ))
print (Or(x, y).subs(x, 0 ))
print (Or( True , False ))
print (Or( True , True ))
print (Or( False , False ))
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.
Python3
from sympy.abc import x
from sympy.logic.boolalg import Not
print (Not(x))
print (Not( True ))
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.
Python3
from sympy.abc import x, y
from sympy.logic.boolalg import Nor
nor_formula = ~(x | y)
print (nor_formula)
print (Nor(x, y))
print (Nor( True , False ))
print (Nor( True , True ))
print (Nor( False , 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.
Python3
from sympy.abc import x, y
from sympy.logic.boolalg import Nand
nor_formula = ~(x & y)
print (nor_formula)
print (Nand(x, y))
print (Nand( True , False ))
print (Nand( True , True ))
print (Nand( False , False ))
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.
Python3
from sympy.abc import x, y
from sympy.logic.boolalg import Xor
xor_formula = x ^ y
print (xor_formula)
print (Xor(x, y))
print (Xor( True , False ))
print (Xor( True , True ))
print (Xor( False , False ))
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.
Python3
from sympy.abc import x, y
from sympy.logic.boolalg import Xnor
xnor_formula = ~(x ^ y)
print (xnor_formula)
print (Xnor(x,y))
print (Xnor( True , False ))
print (Xnor( True , True ))
print (Xnor( 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.
Python3
from sympy.abc import x, y
from sympy.logic.boolalg import Implies
print (Implies(x, y))
print (Implies( True , False ))
print (Implies( True , True ))
print (Implies( False , False ))
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.
Python3
from sympy.abc import x, y, z
from sympy.logic.boolalg import Equivalent, And, Or
print (Equivalent(x, y, z))
print (Equivalent( True , False ))
print (Equivalent( True , True ))
print (Equivalent( False , False ))
print (Equivalent( False , 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.
Python3
from sympy.abc import x, y, z
from sympy.logic.boolalg import ITE, Nor, Nand, Xor, Or, And
print (ITE(x, y, z))
print (ITE( True , Or( True , False ), And( True , True )))
print (ITE(Nor( True , False ), Xor( True , False ), Nand( True , True )))
|
Output:
ITE(x, y, z)
True
False
Share your thoughts in the comments
Please Login to comment...