Numeric Programming in Python (Brief Overview, 2011)

 

 

A large Python application domain

·        Scientific: Lawrence Livermore, Los Alamos, Fermi, …

·        Engineering: NASA, JPL, …

·        Financial: UBS, JP Morgan Chase, Getco, DRW, …

·        Movie animation (ILM), weather forecasts (NOAA), hydrology models (SFWMD), GIS (ESRI), NSA, …

 

 

Based on third-party open source extensions

Three primary packages

·        NumPy:          efficient matrix processing and numeric libraries

·        SciPy:              adds additional numeric libs, statistics, tools

·        matplotlib:   2D plotting tools for rendering NumPy/SciPy results

Other popular packages

·        Parallel computation: pypar (MPI, NumPy, openmpi), pp (SMP), pypvm (PVM), pyMPI

·        Graphics: Maya, Blender, OpenGL

·        Visualization: VPython, VTK

·        Compiled code linkage: SWIG, F2PY, Cython (PyRex), Weave

·        IPython shell (MATLAB-like), BioPython, ScientificPython, cluster computing, …

 

 

NumPy

·        http://numpy.scipy.org/

·        Extends Python with high-performance vector/matrix tools

·        Python + NumPy = free, open source alternative to MATLAB

·        Effieciency of NumPy arrays  + flexibility, power, and libs of Python

·        Numeric processing, + GUIs, networking, databases, sys admin, text, Web, …

·        Also being used as an alternative to FORTRAN (no, really)

 

From NumPy’s example pages…

>>> import numpy as np

 

# many ways to create arrays

>>> x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)

>>> type(x)

<type 'numpy.ndarray'>

>>> x.shape

(2, 3)

>>> x.dtype

dtype('int32')

 

>>> np.array([1, 2, 3], dtype='f')

array([ 1.,  2.,  3.], dtype=float32)

>>> np.indices((3,3))

array([[[0, 0, 0], [1, 1, 1], [2, 2, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]])

 

# plus pairwise, vector-wide operations and expressions

>>> a = np.array((1, 2))

>>> a * 3.

array([3., 6.])

 

>>> a = np.array([1, 2, 3, 4])

>>> b = np.array([2, 3, 4, 5])

>>> a + b

array([3, 5, 7, 9])

 

See also:

Matrix code in the core Python language (after you’ve learned about loops and comprehensions!):

 

        Extras\Code\Misc\matrix-code.py

 

As a rule, NumPy supports arrays which are much more efficient in space and speed for larger data sets

 

 

SciPy

·        http://www.scipy.org/

·        Builds on NumPy base to add additional libraries and tools

·        Numerical integration, optimization, statistics, interpolation, …

·        Includes Weave, a tool for including C/C++ code in Python scripts

·        A central “clearinghouse” for other scientific tools: IPython shell, …

 

From SciPy’s example pages…

# Bessel functions, circular drum head vibrational modes

>>> from scipy import *

>>> from scipy.special import jn, jn_zeros

>>> 

>>> def drumhead_height(n, k, distance, angle, t):

...    nth_zero = jn_zeros(n, k)

...    return cos(t)*cos(n*angle)*jn(n, distance*nth_zero)

...

>>> theta = r_[0:2*pi:50j]

>>> radius = r_[0:1:50j]

>>> 

>>> x = array([r*cos(theta) for r in radius])

>>> y = array([r*sin(theta) for r in radius])

>>> z = array([drumhead_height(1, 1, r, theta, 0.5) for r in radius])

 

 

 

matplotlib

·        http://matplotlib.sourceforge.net/

·        A popular Python 2D plotting library

·        Can be used in python scripts, the python and IPython shell (like MATLAB, Mathematica)

·        Also supports web servers and 6 GUI toolkits for rendering

 

From matplotlib’s example pages…

# histogram plotting demo, one of many at the web site!

import numpy as np

import matplotlib.mlab as mlab

import matplotlib.pyplot as plt

 

mu, sigma = 100, 15

x = mu + sigma*np.random.randn(10000)

 

# the histogram of the data

n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75)

 

# add a 'best fit' line

y = mlab.normpdf( bins, mu, sigma)

l = plt.plot(bins, y, 'r--', linewidth=1)

 

plt.xlabel('Smarts')

plt.ylabel('Probability')

plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')

plt.axis([40, 160, 0, 0.03])

plt.grid(True)

 

plt.show()

 

 

 

For More Details

Google is your friend…

search the web for related and up-to-date links, examples, documentation, downloads