Miguel Nuno
Miguel Nuno

Reputation: 59

How to get parts of an boolean logical expression/ math expression in sympy?

I would like to get all the parts of a boolean logic expression like this (x & y) | (a | b). I would like to get (x & y) and (a | b) as separated functions. I tried to use args, but it delivered me (x & y, a,b). I tried use atoms() but it just delivers me {a, x, y, b}. Even iteration through the args as a list didnt give me the wanting result. I want to get an expression such as (x & y) | (a | b), separate it into parts to create a truth table showing step by step what (x & y) and (a | b) evaluates in between before showing the final results of the expression.

Thank you

Upvotes: 0

Views: 106

Answers (1)

smichr
smichr

Reputation: 19115

I am not aware of any way to create a nested Or, so your expression automatically becomes (x & y | a | b) and that is why you get the args you do. I would just create those two expressions in addition to the desired Or having them as arguments:

o1 = x & y
o2 = a | b
eq = o1 | o2
from sympy.utilities.iterables import *
for i in subsets((True, False), 4, repetition=True):
    for p in multiset_permutations(i):
        reps = dict(zip((x,y,a,b),p))
        print(reps, o1.xreplace(reps), o2.xreplace(reps), eq.xreplace(reps))

Upvotes: 1

Related Questions