#!/usr/bin/env python # coding: utf-8 # > This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python. # # # 15.6. Finding a Boolean propositional formula from a truth table # In[ ]: from sympy import * init_printing() # Let's define a few variables. # In[ ]: var('x y z') # We can define propositional formulas with symbols and a few operators. # In[ ]: P = x & (y | ~z); P # In[ ]: P.subs({x: True, y: False, z: True}) # Now, we want to find a propositional formula depending on x, y, z, with the following truth table: # In[1]: get_ipython().run_cell_magic('HTML', '', '\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
xyz??
TTT*
TTF*
TFTT
TFFT
FTTF
FTFF
FFTF
FFFT
\n') # Let's write down all combinations that we want to evaluate to True, and those for which the outcome does not matter. # In[ ]: minterms = [[1,0,1], [1,0,0], [0,0,0]] dontcare = [[1,1,1], [1,1,0]] # Now, we use the SOPform function to derive an adequate proposition. # In[ ]: Q = SOPform(['x', 'y', 'z'], minterms, dontcare); Q # Let's test that this proposition works. # In[ ]: Q.subs({x: True, y: False, z: False}), Q.subs({x: False, y: True, z: True}) # > You'll find all the explanations, figures, references, and much more in the book (to be released later this summer). # # > [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages).