Introduction to Scientific Computing with Python, part two. M. Emmett Department of Mathematics University of North Carolina at Chapel Hill June 20 2012
The Zen of Python zen of python... fire up python and run: import this these slides are in the public domain
How I use Python Experimenting with complex algorithms: it s quick and relatively easy to try new things in Python vs C or Fortran 90. Debugging: interactively drawing things mid-algorithm is helpful for debugging. Analysis: loading and working with data, computing errors, drawing graphs, and making movies is relatively painless. Deployment: building and launching many jobs/experiments on KillDevil is very convenient (ie, control everything from your desktop).
Resources for scientific computing with Python NumPy and SciPy: http://scipy.org Large collection of packages for: low-level numerical arrays (C/Fortran), sparse and dense linear algebra, FFTs, statistics etc... PyPI: http://pypi.python.org The Python Package Index is a central place for authors to distribute Python packages Software Carpentry: http://software-carpentry.org Lectures/videos about scientific programming. Books by Prof. Langtangen: http://folk.uio.no/hpl Prof. Langtangen has two great books on scientific computing with Python.
Python Packages Python has a batteries included philosophy. A standard Python installation contains many helpful packages. If the standard libraries don t have what you are looking for, there is a rich and growing collection of Python packages for many applications: system interaction, graphics, user interfaces, web programming, scientific computing, etc etc. Installing Python packages is relatively easy with pip: http://www.pip-installer.org
ipython - Interactive Python Fire up ipython. It s an interactive Python prompt. I find it helpful for looking up documentation and experimenting with Python. A few helpful things: tab completion is your friend shell commands like cd and ls work?command shows help for a given command??command shows the source code for a given command run script.py runs the script script.py, and shows traces nicely
numpy - Numerical Python NumPy is the fundamental package for scientific computing with Python low-level (C/Fortran) N-dimensional arrays broadcasting, dot/matrix products linear algebra (LAPACK), FFTs etc Compare the following # using python lists and a for loop y = [] for k in range ( 10 ): x = k * 2* math. pi / 10 y. append ( math. sin (x)) # using numpy broadcasting x = np. arange (0.0, 2*np.pi, 2*np.pi/10) y = np.sin (x)
scipy - Scientific Python SciPy is a large collection of packages for more general scientific computing. ODE solvers and quadrature routines optimization and root finding signal processing sparse linear algebra statistics, distributions, random numbers masked arrays much more...
sympy - Symbolic Python SymPy is a package for performing symbolic calculations. import sympy x = sympy. var ( 'x ') p = x**2 + 5*x + 2 print p. diff (x) # 2* x + 5 SymPy also contains multi-precision math routines from sympy. mpmath import * print quad ( lambda x: 4* sqrt (1-x**2), [0, 1]) # 3. 1415926535897932384626433832795028841971693993751
matplotlib - Plotting (similar in style to MATLAB) Matplotlib is a 2D plotting library that will be familiar to MATLAB users. import numpy as np import matplotlib. pylab as plt x = np. arange (0.0, 2*np.pi, 2*np.pi/20) y = np.sin (x) plt. plot (x, y, '-ok ') plt. show ()
fabric - Remote execution/deployment Fabric is a tool for executing commands on remote hosts. from fabric. api import * @task def build (): with cd(env. work ): run ( 'make ') Then, on the command line I could run: $ fab -H killdevil build
h5py and pytables - HDF5 storage The HDF5 file format is a great way to store data and move it around (ie, platform independent storage). I like to think of it as a filesystem within a file. There are two Python packages that can read and write HDF5 files: h5py and pytables. For example import h5py output = h5py. File ( ' results.h5 ', 'w ') output. create_dataset ( ' pressure ', data = pressure ) output. create_dataset ( ' temperature ', data = temperature ) output. close () results = h5py. File ( ' results.h5 ', 'r ') pressure = results [ ' pressure '] temperatute = results [ ' temperature '] # do something with pressure, temperature results. close ()
mpi4py - Parallel computing (MPI) for Python There are many ways to do parallel computing. One of them is with a Message Passing Interface (MPI) library (of which there are several implementations). The mpi4py package provides a nice wrapper to the MPI routines: from mpi4py import MPI import numpy as np comm = MPI. COMM_ WORLD if comm. rank == 0: x = np. array ([1.0, 2.0, 3.0, 4.0]) else : x = np. zeros (4) comm. Bcast (x) print 'i am %d of %d ' % ( comm.rank, comm. size ), x
Other useful packages MMMMMOOORE! While creating this presentation I kept thinking of more cool packages that I have used... some of them include Mayavi - 3d graphics with OpenGL rendering Pandas - data analysis similar in style to R PyMC - Markov-Chain Monte-Carlo simulations many, many more...
Defining functions To define a function in Python, use the def keyword def f(x): """ Return cos (x) - x**3. This is an extended docstring. """ return np.cos (x) - x**3 Then, one can call the function by y = f(0.2) Note that documentation is, in some sense, part of the language.
Defining functions Default arguments def error (a, b, order = 'inf '): """ Compute the error between * a* and * b*. The * order * can be ' inf ' or 2. """ if order == 'inf ': return np.max ( abs (a-b)) elif order == 2: return np. sqrt (np.sum ((a-b )** 2)) raise ValueError ( ' invalid order ') Now you can call this as: err_def = error (a, b) err_inf = error (a, b, order = 'inf ') err_2 = error (a, b, order =2) err_ reorder = error ( b=x, a=y)
Defining functions Lambda forms Small throw-away functions can be created using the lambda keyword: square = lambda x: x** 2 print square ( 2) # 4 This is especially useful for functional programming x = [ 1, 2, 3, 4 ] y = map ( lambda x: x**2, x) print y # [ 1, 4, 9, 16 ]
List comprehensions Python has some nice language constructs to make programming look more mathematical. Consider the following statement Let Ω = { x 2 x N, x < 100, and x is prime } In Python, this is omega = [ x** 2 for x in range ( 100 ) if is_ prime ( x) ] A similar construct exists for creating dictionaries.
Map and reduce Functional programming The map routine applies a function to each element of a list. The following are equivalent y = [ math. sin ( x) for x in X ] y = map ( math.sin, X) The reduce routine applies a function in an aggregate manner to reduce lists. To compute the factorial of 10, one could f = reduce ( lambda x, y: x * y, range (1, 11 ))
NumPy: vector programming When working with NumPy arrays, one can # do scalar / vector multiplication a = 2.0 x = np. asarray ([1.0, 2.0, 3.0]) y = a * x # [ 2.0, 4.0, 6. 0 ] # do elementwise multiplication z = x * y # [ 2.0, 8.0, 18.0 ] # do matrix / vector multiplication A = np. asarray ([ [ 1.0, 2.0, 1.0 ], [ 2.0, -1.0, 1.0 ] ]) z = np.dot (A, x) Recall the broadcasting rules too (y = np.sin(x)).
Computing π There are many ways to numerically compute an approximate value of π. One way is to note that π 4 = arctan 1 2 + arctan 1 3 and use the Taylor expansion of arctan x about 0, which is arctan x x x 3 3 + x 5 5 + = k=0 ( 1) k x 1+2k 1 + 2k
Computing π Try it! Here is a start... x2, x3 = 1.0/2.0, 1.0/3.0 atan2, atan3 = 0.0, 0. 0 for k in range (5): atan2 +=... atan3 +=... print ' pi = ', 4. 0 * ( atan2 + atan3 ) Warning: Be careful when dividing integers in Python: sometimes 1/2 0.5 That s why I wrote 1.0/2.0 instead of 1/2. Challenge: can you use the sympy.mpmath module to go beyond 14 digits?
Spectral analysis Sometimes we want to know about the frequencies that exist in a signal. Consider the following signal (contain in y) import numpy as np x = np. arange (0.0, 2*np.pi, 2*np.pi/100 ) y = ( 1.0 + 0.5*np.sin (x) + 0.3*np.sin (5*x) + 0.1*np.sin (8*x) ) Can you plot the spectrum of this signal, with the zero frequency in the center?