{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "> 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.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 15.6. Finding a Boolean propositional formula from a truth table" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from sympy import *\n", "init_printing()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's define a few variables." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "var('x y z')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can define propositional formulas with symbols and a few operators." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "P = x & (y | ~z); P" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "P.subs({x: True, y: False, z: True})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we want to find a propositional formula depending on x, y, z, with the following truth table:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/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
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%%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", "\n", "
xyz??
TTT*
TTF*
TFTT
TFFT
FTTF
FTFF
FFTF
FFFT
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's write down all combinations that we want to evaluate to True, and those for which the outcome does not matter." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "minterms = [[1,0,1], [1,0,0], [0,0,0]]\n", "dontcare = [[1,1,1], [1,1,0]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we use the SOPform function to derive an adequate proposition." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Q = SOPform(['x', 'y', 'z'], minterms, dontcare); Q" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's test that this proposition works." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Q.subs({x: True, y: False, z: False}), Q.subs({x: False, y: True, z: True})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }