python-3.xboolean-logicpyeda

PYEDA truthtable of functions


Hope there is anybody who feels good with PYEDA.

I want to add fictious variables to function Let me have f=x1, but how can I get truthtable for this function , which will have x2 too

Like truthtable for f(x1)=x1 is:

x1 f
0 0
1 1

But for f(x1,x2)=x1 is:

x1   x2   f
0    0    0
0    1    0
1    0    1
1    1    1

But I will get first table, pyeda will simplify x1&(x2|~x2) to x1 automatically. How can I add this x2?

def calcFunction(function, i):
    #here is is point with dimension-size 4
    function=function.restrict({x4:i[3]})
    function = function.restrict({x3:i[2]})
    function = function.restrict({x2:i[1]})
    function = function.restrict({x1:i[0]})
    if function.satisfy_one() is not None:
        return 1
    return 0

Here is my algo to fix it, I am calculating func in each point manually, where function can containt 1-4 variables and I am calculating for all point and combinations of x1...x4.


Solution

  • I'm not sure I understand the question as asked, but you might want to try the expression simplify method.

    For example:

    In [1]: f = (X[1] & X[2]) | (X[3] | X[4] | ~X[3])                                                                  
    
    In [2]: expr2truthtable(f)                                                                                         
    Out[2]: 
    x[4] x[3] x[2] x[1]
       0    0    0    0 : 1
       0    0    0    1 : 1
       0    0    1    0 : 1
       0    0    1    1 : 1
       0    1    0    0 : 1
       0    1    0    1 : 1
       0    1    1    0 : 1
       0    1    1    1 : 1
       1    0    0    0 : 1
       1    0    0    1 : 1
       1    0    1    0 : 1
       1    0    1    1 : 1
       1    1    0    0 : 1
       1    1    0    1 : 1
       1    1    1    0 : 1
       1    1    1    1 : 1
    
    In [3]: f = f.simplify()                                                                                           
    
    In [4]: f                                                                                                          
    Out[4]: 1
    
    In [5]: expr2truthtable(f)                                                                                         
    Out[5]: 1