Home > Net >  Python Sympy - Select monomials from ANF of Boolean expression
Python Sympy - Select monomials from ANF of Boolean expression

Time:01-28

I have some truth table in GF(2) and I compute the corresponding Algebraic Normal Form (ANF) or Zhegalkin polynomial. Below a dummy example with two variables.

from sympy.logic.boolalg import ANFform
from sympy.abc import x, y

truth_table = [0, 1, 1, 1]

expr = ANFform([x, y], truth_table)

which prints

x ^ y ^ (x & y)

I would then like to extract and select the individuals monomials of the resulting expression.

For example, I would have something like this:

expr_monoms[2] # (x & y)

How to achieve this?

CodePudding user response:

You can use .args:

In [50]: from sympy.logic.boolalg import ANFform
    ...: from sympy.abc import x, y
    ...: 
    ...: truth_table = [0, 1, 1, 1]
    ...: 
    ...: expr = ANFform([x, y], truth_table)

In [51]: expr
Out[51]: x ⊻ y ⊻ (x ∧ y)

In [52]: expr.args[2]
Out[52]: x ∧ y
  • Related