I've been trying to determine boolean expression equivalence with Sympy, but it seems it doesn't detect equivalence of more complex expressions
from sympy.abc import x, y
from sympy.logic.boolalg import *
print(Equivalent(x, x))
print(Equivalent(x, x & True))
print(Equivalent(x | y, y | x))
print(Equivalent(x | (x & y), x | y))
print(Equivalent(~x & ~y, ~(x | y)))
Results:
>>>True
>>>True
>>>True
>>>Equivalent(Or(x, y), Or(And(x, y), x))
>>>Equivalent(Not(Or(x, y)), And(Not(x), Not(y)))
Is there a way to determine whether or not two symbolic boolean expressions are equal in Python?
equals
works fine for me:
( x|(x&y) ).equals( x|y )
# False
( ~x&~y ).equals( ~(x|y) )
# True
In general, equals
tries to transform the two expressions until they are equal to each other and only returns False
if it fails. It is therefore more accurate (but also slower) than ==
.
sympy.simplify_logic
perhaps?
>>> sympy.simplify_logic(Equivalent(Or(x, y), Or(And(x, y), x)))
Or(Not(y), x)
>>> sympy.simplify_logic(Equivalent(Not(Or(x, y)), And(Not(x), Not(y))))
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With