Search code examples
pythonbooleansympyboolean-logicboolean-expression

Remove some variables from Boolean expression in SymPy


I have a Boolean expression in SymPy. If I know the values of some of symbols, how can I find the relation between the rest of the symbols? For example:

from sympy.core.symbol import Symbol
from sympy.logic.boolalg import Equivalent
from sympy import simplify_logic
a=Symbol("a")
b=Symbol("b")
c=Symbol("c")
d=Symbol("d")
e=Symbol("e")

expr=a&b>>c|a&d|~a&Equivalent(c,(d|a&e))
print("if a=True and d=False, then relation between a,c and e must be")#c|~b

alternatively: I have a Boolean function f(a,b,c,k,l,j). How to solve for k,l and j if a,b and c are known? I know that I have to form a new expression that is true on any values of a, b and c, but how to do it?

simplify_logic((eeldus&expr))                    #leaves a and d in.

Solution

  • If I understand your question correctly, subs would work:

    >>> from sympy import *
    >>> a, b, c, d, e = symbols('a b c d e')
    >>> expr = a & b >> c | a & d | ~a & Equivalent(c,(d | a & e))
    >>> expr.subs([(a,true),(d,false)])
    Implies(b, c)