Home | | Python | | IPython | | Share This Page |

IPython: Math Processor

A tool that manipulates mathematics as a word processor manipulates words

Copyright © 2014, Paul Lutus — Message Page

(double-click any word to see its definition)

Getting Started

In this page we'll become familiar with IPython and the command-line and notebook interfaces. Let's start with a simple command-line session — open a shell session and type:$ ipythonWe should get something like this as a result:

Python 2.7.5+ (default, Feb 27 2014, 19:37:08) Type "copyright", "credits" or "license" for more information. IPython 2.0.0 — An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]:Notice about the result that it identifies both Python the language, and IPython the interactive environment. Be sure that your display identifies IPython version 2.0.0 or newer as shown above, in order for this article's examples to produce the expected results. If your IPython version is below 2.0.0, you may want to return to the installation page to get an update.

First, let's import a very useful math library, sympy (symbolic Python):

In [1]: from sympy import *

Defined Constants in sympyLet's see if sympy knows about $\pi$ (pi):

In [2]: pi Out[2]: piSo sympy knows about $\pi$ — well, that deserves an explanation. If IPython and the sympy library didn't know about $\pi$ and we typed "pi" hoping to get a response, the result would have been:

In [2]: pi NameError: name 'pi' is not definedBut sympy recognizes $\pi$ — it has a definition in the sympy library. But why didn't Python print $\pi$'s numerical value? The reason is that, because $\pi$ is irrational, any numerical representation of $\pi$ is an approximation, consequently a reasonable computer math environment will avoid replacing a symbolic value with a numerical one. But in a computer, isn't $\pi$ usually approximated to only a handful of decimal places? Yes, that's true, except in libraries like sympy, where accuracy is important.

So how many decimal places of $\pi$ does sympy support? Here's a hint:

In [2]: N(pi,1000) Out[10]: 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420198The N() function forces a numerical evaluation of its first argument, and an optional second argument specifies a number of decimal places. I will leave it to the reader to see how many of $\pi$'s decimal places we can acquire in a finite time interval.

Euler's IdentityThere is a famous equation by Euler, often quoted as an example of mathematical beauty, in which five important mathematical values appear. The equation (Euler's identity) is:

(1) $ \displaystyle e^{i\pi} + 1 = 0$Where:

- $e$ = the base of natural logarithms, equal to $ \displaystyle \lim_{n \to \infty} \left(1 + \frac{1}{n} \right)^n$.
- $i$, the imaginary unit, equal to $\sqrt{-1}$, $i$ = "imaginary".
- $\pi$, an irrational constant equal to the ratio of a circle's circumference and diameter.
- 1 = the multiplicative identity.
- 0 = the additive identity.
Let's see if sympy knowns about these constants and their relationship:

In[3]: N(E,60) Out[3]: 2.71828182845904523536028747135266249775724709369995957496697 In[4]: N(limit((1+1/n)**n,n,oo),60) Out[4]: 2.71828182845904523536028747135266249775724709369995957496697 In[5] I*I Out[5]: -1 In[6]: E**(I*pi)+1 Out[6]: 0It seems to be so. Readers should note that, in Python, the exponentiation operator is "**", not "^". The latter is reserved for the logical exclusive-or operation. Note also that in sympy, a convenient shorthand for infinity is "oo":

In[6]: N(oo) Out[6]: +infBut also note that "oo" and "+inf" aren't interchangeable (try it).

Halfway ThereThe Greeks, who didn't have the notion of infinity in their intellectual toolkit, argued that you could never really get anywhere, because to get there, you first had to go halfway. Then you had to cross the remaining distance, but to do so, you first had to cover half that distance too. And so forth. This idea, which appears as the Dichotomy Paradox in Aristotle's Physics, represented a philosophical obstacle until the development of calculus and the concept of infinite series.

The relevant infinite series is:

(2) $ \displaystyle \sum_{n=1}^\infty 2^{-n} = \frac{1}{2} + \frac{1}{4} + \frac{1}{8} \ldots = 1 $Let's acquire this result with sympy:

In[7]: summation(2**-n,(n,1,oo)) Out[7]: 1Notice that sympy's summation function is named "summation()", not "sum()". The latter function has a different purpose in Python. So what does this mean about Greek philosophy? It means you really can get to your goal, but you mustn't try to think about every step along the way.

Notebook Interface

Well-Rounded IntegralFor my next example I need to produce a graph, so let's use the IPython notebook interface. Enter this in your shell session:

In[8]: quit $ ipython notebookOn most systems, IPython will not only start its Web server, it will launch the system default browser showing the notebook interface:

This example involves a function that has a relationship to $\pi$. The function is:

(3) $ \displaystyle f(x) = \sqrt{1-x^2} $Here's my notebook cell that plots the above function — notice that the function inscribes a half-circle:

Apart from wanting to give my readers examples of how to use IPython's plotting features, It occurs to me that, if I integrate this function with respect to x on the interval between -1 and 1, I will have the area of half a unit circle (i.e. a circle with a radius of one), therefore the result should be equal to $\frac{\pi}{2}$. Here's a graph of the function with the area beneath the curve filled in:

Here's the desired integral for $\pi$:

(4) $ \displaystyle 2 \int_{-1}^1 \sqrt{1-x^2} dx = \pi $Here's my result from the IPython notebook:

Notice about this result that sympy didn't just produce a numerical result that approximates $\pi$ — it understood that the symbolic integral $ \displaystyle 2 \int_{-1}^1 \sqrt{1-x^2} dx $ equals $\pi$. Rather than compute a numerical approximation of $\pi$, sympy identified $\pi$.

Remember also about the notebook interface that it gives you more than the ability to show pretty-printed equations and graphics — it also saves your work in plain-text files for later improvement and archiving.

*To navigate this article set, use the arrows and drop-down lists at the top and bottom of each page.*

Home | | Python | | IPython | | Share This Page |