SymPy - Logical Expressions



Boolean functions are defined in sympy.basic.booleanarg module. It is possible to build Boolean expressions with the standard python operators & (And), | (Or), ~ (Not) as well as with >> and <<. Boolean expressions inherit from Basic class defined in SymPy's core module.

BooleanTrue function

This function is equivalent of True as in core Python. It returns a singleton that can be retrieved by S.true.

>>> from sympy import * 
>>> x=sympify(true) 
>>> x, S.true

The above code snippet gives the following output −

(True, True)

BooleanFalse function

Similarly, this function is equivalent to Boolean False in Python and can be accessed by S.false

>>> from sympy import * 
>>> x=sympify(false) 
>>> x, S.false

The above code snippet gives the following output −

(False, False)

And function

A logical AND function evaluates its two arguments and returns False if either of them is False. The function emulates & operator.

>>> from sympy import * 
>>> from sympy.logic.boolalg import And 
>>> x,y=symbols('x y') 
>>> x=True 
>>> y=True 
>>> And(x,y), x"&"y

The above code snippet gives the following output −

(True, True)

>>> y=False 
>>> And(x,y), x"&"y

The above code snippet gives the following output −

(False, False)

Or function

This function evaluates two Boolean arguments and returns True if either of them is True. The | operator conveniently emulates its behaviour.

>>> from sympy import * 
>>> from sympy.logic.boolalg import Or 
>>> x,y=symbols('x y') 
>>> x=True 
>>> y=False 
>>> Or(x,y), x|y

The above code snippet gives the following output −

(True, True)

>>> x=False 
>>> y=False 
>>> Or(x,y), x|y

The above code snippet gives the following output −

(False, False)

Not Function

A Logical Not function results in negation of the Boolean argument. It returns True if its argument is False and returns False if True. The ~ operator performs the operation similar to Not function. It is shown in the example below −

>>> from sympy import * 
>>> from sympy.logic.boolalg import Or, And, Not 
>>> x,y=symbols('x y') 
>>> x=True 
>>> y=False 
>>> Not(x), Not(y)

The above code snippet gives the following output −

(False, True)

>>> Not(And(x,y)), Not(Or(x,y))

The above code snippet gives the following output −

(True, False)

Xor Function

The Logical XOR (exclusive OR) function returns True if an odd number of the arguments are True and the rest are False and returns False if an even number of the arguments are True and the rest are False. Similar operation is performed by ^ operator.

>>> from sympy import * 
>>> from sympy.logic.boolalg import Xor 
>>> x,y=symbols('x y') 
>>> x=True 
>>> y=False

>>> Xor(x,y), x^y

The above code snippet gives the following output −

(True, True)

>>> a,b,c,d,e=symbols('a b c d e') 
>>> a,b,c,d,e=(True, False, True, True, False) 
>>> Xor(a,b,c,d,e)

The above code snippet gives the following output −

True

In above case, three (odd number) arguments are True, hence Xor returns true. However, if number of True arguments is even, it results in False, as shown below −

>>> a,b,c,d,e=(True, False, False, True, False) 
>>> Xor(a,b,c,d,e)

The above code snippet gives the following output −

False

Nand Function

This function performs Logical NAND operation. It evaluates its arguments and returns True if any of them are False, and False if they are all True.

>>> from sympy import * 
>>> from sympy.logic.boolalg import Nand 
>>> a,b,c=symbols('a b c') 
>>> a,b,c=(True, False, True) 
>>> Nand(a,b,c), Nand(a,c)

The above code snippet gives the following output −

(True, False)

Nor Function

This function performs Logical NOR operation. It evaluates its arguments and returns False if any of them are True, and True if they are all False.

>>> from sympy import * 
>>> from sympy.logic.boolalg import Nor 
>>> a,b,c=symbols('a b c') 
>>> a,b,c=(True, False, True) 
>>> Nor(a,b,c), Nor(a,c)

The above code snippet gives the following output −

(False, False)

Note that even though SymPy provides ^ operator for Xor, ~ for Not, | for Or and & for And functions as convenience, their normal use in Python is as bitwise operators. Hence, if operands are integers, results would be different.

Equivalent function

This function returns equivalence relation. Equivalent(A, B) is True if and only if A and B are both True or both False. The function returns True if all of the arguments are logically equivalent. Returns False otherwise.

>>> from sympy import * 
>>> from sympy.logic.boolalg import Equivalent 
>>> a,b,c=symbols('a b c') 
>>> a,b,c=(True, False, True) 
>>> Equivalent(a,b), Equivalent(a,c)

The above code snippet gives the following output −

(False, True)

ITE function

This function acts as If then else clause in a programming language.ITE(A, B, C) evaluates and returns the result of B if A is true else it returns the result of C. All args must be Booleans.

>>> from sympy import * >>> from sympy.logic.boolalg import ITE >>> a,b,c=symbols('a b c') >>> a,b,c=(True, False, True) 
>>> ITE(a,b,c), ITE(a,c,b)

The above code snippet gives the following output −

(False, True)

Advertisements