import numpy as np print("x1, x2, x3, F(x1,x2,x3)") a = np.array([[0,0,0,1], [0,0,1,1], [0,1,0,0], [0,1,1,0], [1,0,0,1], [1,0,1,1], [1,1,0,0], [1,1,1,1]]) print(a) print(" ") print("Воспользуемся правилом построения СДНФ:") print("x1, x2, x3, F(x1,x2,x3)") res = a[a[:, 3] == 1] print(res) 

enter image description here

  • What do you expect to get as a result (in terms of Python)? - MaxU
  • What is in the photo. At least approximately similar. Or in any form to build the PDNF. - Ashley King
  • The photo formula from discrete mathematics. How do you imagine it in terms of Python? - MaxU
  • To be honest, I generally thought that you could somehow write this formula on a python. But it seems that it is impossible. So, I can not even imagine. - Ashley King
  • What is your purpose for doing this? Maybe understand your goal, it will be easier to suggest a solution? - MaxU

2 answers 2

Use the Pandas module:

 import pandas as pd 

source truth table in the form of a Pandas DataFrame:

 In [112]: df = pd.DataFrame( ...: [[0,0,0,1], ...: [0,0,1,1], ...: [0,1,0,0], ...: [0,1,1,0], ...: [1,0,0,1], ...: [1,0,1,1], ...: [1,1,0,0], ...: [1,1,1,1]], ...: columns = ["x1", "x2", "x3", "F(x1,x2,x3)"] ...: ) In [113]: df Out[113]: x1 x2 x3 F(x1,x2,x3) 0 0 0 0 1 1 0 0 1 1 2 0 1 0 0 3 0 1 1 0 4 1 0 0 1 5 1 0 1 1 6 1 1 0 0 7 1 1 1 1 

Decision:

first we get an inverted truth table where the result is 1 :

 In [114]: r = (~df.loc[df['F(x1,x2,x3)']==1, ['x1','x2','x3']].astype(bool)).astype('int8') In [115]: r Out[115]: x1 x2 x3 0 1 1 1 1 1 1 0 4 0 1 1 5 0 1 0 7 0 0 0 

Now, on its basis, you can get the PDNF:

 In [116]: res = (r.apply(lambda r: '({}{} ^ {}{} ^ {}{})'.format('!'*r['x1'], 'x1', ...: '!'*r['x2'], 'x2', ...: '!'*r['x3'], 'x3'), ...: axis=1) ...: .str.cat(sep = ' v ')) ...: In [117]: print(res) (!x1 ^ !x2 ^ !x3) v (!x1 ^ !x2 ^ x3) v (x1 ^ !x2 ^ !x3) v (x1 ^ !x2 ^ x3) v (x1 ^ x2 ^ x3) 

UPDATE: Auxiliary inverted matrix is ​​needed for convenience.

Rule: If the value of a variable is 0, then it is written with inversion. If the value of the variable is 1, then without inversion.

The answer uses the following trick:

 In [29]: '!' * 0 + 'x1' Out[29]: 'x1' In [30]: '!' * 1 + 'x1' Out[30]: '!x1' 

those. in order to put a negation (exclamation mark) it is necessary that the corresponding value in the matrix is ​​equal to one, and according to the rule of construction of the PDNF, the negation is put if the element is zero. Therefore, it is more convenient to use the inverted table in order to use the trick calculated above.

  • And why, when we got an inverted table, are the rows data different from the original table? And why PDNF does not match the table? - Ashley King
  • @AshleyKing, inverted and must be different - this is like a negative film (where there were zeroes in the original matrix and vice versa). PDNF, in my opinion, corresponds to the original matrix and coincides with your version ... - MaxU
  • Yes, the PDNF is the same, I looked at something. Is it possible to somehow make it so that the values ​​of the SDNF table were the same as in the original one? And another such question, how can one make up the SKNF (this is also the same as the PDNF, only with 0, and not with 1)? And then my answer is SKNF with exactly the opposite. - Ashley King
  • @AshleyKing, an inverted table is an auxiliary table, it's just easier to form a MDF. It is not necessary to show it. Now I will answer you in your new SO question about SKNF ... - MaxU
  • @AshleyKing, added an explanation - why an auxiliary table is needed - MaxU

You can use the pyeda library.

 from pyeda.inter import * import numpy as np x = exprvars('x', 3) a = np.array([[0,0,0,1], [0,0,1,1], [0,1,0,0], [0,1,1,0], [1,0,0,1], [1,0,1,1], [1,1,0,0], [1,1,1,1]]) vector = [i[3] for i in a] f = truthtable(x, vector) print(f) print(truthtable2expr(f)) 

Conclusion:

 x[2] x[1] x[0] 0 0 0 : 1 0 0 1 : 1 0 1 0 : 0 0 1 1 : 0 1 0 0 : 1 1 0 1 : 1 1 1 0 : 0 1 1 1 : 1 Or(And(~x[0], ~x[1], ~x[2]), And(x[0], ~x[1], ~x[2]), And(~x[0], ~x[1], x[2]), And(x[0], ~x[1], x[2]), And(x[0], x[1], x[2]))