Home | Python | IPython | * IPython: Math Processor 2: Installation 3: Getting Started 4: Equation Processing 5: Equation Solving 6: Normal Distribution 7: Differential Equations 8: Fourier Transforms 9: Tips & Tricks  Share This Page
IPython: Math Processor
A tool that manipulates mathematics as a word processor manipulates words

Tips & Tricks

(double-click any word to see its definition)

Tips & Tricks

This page is a miscellany of suggestions to make IPython activities easier and more productive.

Painless Graphics

Using IPython to make beautiful, three-dimensional graphics isn't difficult with plenty of leisure time on one's hands, but there are times when a quick plot is the point, not a perfectly formed graph. So with that in mind, here's the absolute minimum of code required to go from function to plot:

In this simple example there's little control over the result, but it requires minimal effort. It relies on symbolic Python's plotting library, which depends on matplotlib's libraries as a backend, but without the usual effort required to set things up for a beautiful, publication-quality graph.

On the topic of simple plotting methods, I can't resist including an example of a console text-based plot using sympy.textplot:

In : from sympy import *
In : var('x')
In : textplot(E**-x**2,-3,3)

0.99702 |                           .  .
|                          .    .
|                         .      .
|                        .        .
|
|                       .          .
|
|                      .            .
0.49857 | --------------------.--------------.-------------------
|
|                    .                .
|                   .                  .
|
|                  .                    .
|                 .                      .
|               ..                        ..
|             ..                            ..
0.00012 | ............                                ...........
-3                     0                          3


Moving toward the exotic but not difficult, here's a simple surface plot example:

The above example is much better when plotted in a separate window (remove "%matplotlib inline" and restart the notebook), because when placed in a separate window, the drawing scale can be enlarged and the mouse can be used to rotate the plot in three dimensions.

Mixing Sympy and Numpy

Symbolic Python (sympy) has the ability to produce interesting results and optimize existing methods. Numpy, by contrast, is able to process numerical data very efficiently. There are times when sympy and numpy work at cross-purposes, and this is particularly evident when one tries to plot sympy's results. The reason is that a typical sympy result is a symbolic function, but matplotlib needs a list of numerical values to plot and won't directly accept a sympy function as a plotting source.

Since I use sympy extensively, and since I need to plot symbolic function results with a minimum of effort, I have figured out ways to make these conflicting libraries get along. The simplest way to plot a symbolic function using matplotlib is to create a list comprehension as a go-between:

In the above workbook, this line creates the bridge between sympy and numpy/matplotlib:

y = np.array([f(v) for v in x],dtype='float')


The function f(v) is symbolic, quite unsuitable for use with numpy, but the result of the list comprehension/array declaration (y) is a numpy array of float values with a set of results from f(x) over the numerical range provided by index list (x).

Notice about the above example that it also shows an easy way to partition a plot into colored segments using list indexing and two control indices (a and b).

Exporting Presentation Workbooks

IPython normally saves workbooks as plain-text files containing JSON, a concise, portable way to archive one's work. But IPython also exports workbooks in a number of other formats for presentation purposes (not to preserve workbook content for later revision). At the time of writing the only interesting export format is HTML, and the HTML result is quite large for its content (because the required CSS is embedded). But a little Python scripting can bring the size of the export down — by linking to an external CSS file instead of embedding the style data in each page, and by linking to local resources instead of Web resources for (as one example) MathJax. This is how the workbook sections in these articles were created.

The Python Universe

This article set only touches a few key points about IPython and Python in general — there are hundreds of libraries and specialized environments available for engineering, academic and scientific work. Python is coming to be accepted as the default development environment for many mathematical tasks, because of its rich ecosystem, its ubiquity, and the fact that it's free.

 Home | Python | IPython | * IPython: Math Processor 2: Installation 3: Getting Started 4: Equation Processing 5: Equation Solving 6: Normal Distribution 7: Differential Equations 8: Fourier Transforms 9: Tips & Tricks  Share This Page