Book Image

IPython Interactive Computing and Visualization Cookbook - Second Edition

By : Cyrille Rossant
Book Image

IPython Interactive Computing and Visualization Cookbook - Second Edition

By: Cyrille Rossant

Overview of this book

Python is one of the leading open source platforms for data science and numerical computing. IPython and the associated Jupyter Notebook offer efficient interfaces to Python for data analysis and interactive visualization, and they constitute an ideal gateway to the platform. IPython Interactive Computing and Visualization Cookbook, Second Edition contains many ready-to-use, focused recipes for high-performance scientific computing and data analysis, from the latest IPython/Jupyter features to the most advanced tricks, to help you write better and faster code. You will apply these state-of-the-art methods to various real-world examples, illustrating topics in applied mathematics, scientific modeling, and machine learning. The first part of the book covers programming techniques: code quality and reproducibility, code optimization, high-performance computing through just-in-time compilation, parallel computing, and graphics card programming. The second part tackles data science, statistics, machine learning, signal and image processing, dynamical systems, and pure and applied mathematics.
Table of Contents (19 chapters)
IPython Interactive Computing and Visualization CookbookSecond Edition
Contributors
Preface
Index

Finding a Boolean propositional formula from a truth table


The logic module in SymPy lets us manipulate complex Boolean expressions, also known as propositional formulas.

This recipe will show an example where this module can be useful. Let's suppose that, in a program, we need to write a complex if statement depending on three Boolean variables. We can think about each of the eight possible cases (true, true and false, and so on) and evaluate what the outcome should be. SymPy offers a function to generate a compact logic expression that satisfies our truth table.

How to do it...

  1. Let's import SymPy:

    >>> from sympy import *
        init_printing()
  2. Let's define a few symbols:

    >>> var('x y z')
  3. We can define propositional formulas with symbols and a few operators:

    >>> P = x & (y | ~z)
        P
  4. We can use subs() to evaluate a formula on actual Boolean values:

    >>> P.subs({x: True, y: False, z: True})
  5. Now, we want to find a propositional formula depending on x, y, and z, with...