introduction to scientific computing with python, part...
TRANSCRIPT
Resources and useful packages Functions Programming mathematically Exercises
Introduction to Scientific Computing withPython, part two.
M. Emmett
Department of MathematicsUniversity of North Carolina at Chapel Hill
June 20 2012
Resources and useful packages Functions Programming mathematically Exercises
The Zen of Python
zen of python... fire up python and run: import this
these slides are in the public domain
Resources and useful packages Functions Programming mathematically Exercises
How I use Python
I Experimenting with complex algorithms: it’s quick and relativelyeasy to try new things in Python vs C or Fortran 90.
I Debugging: interactively drawing things mid-algorithm is helpful fordebugging.
I Analysis: loading and working with data, computing errors, drawinggraphs, and making movies is relatively painless.
I Deployment: building and launching many jobs/experiments onKillDevil is very convenient (ie, control everything from yourdesktop).
Resources and useful packages Functions Programming mathematically Exercises
Resources for scientific computing with Python
I 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...
I PyPI: http://pypi.python.org
The Python Package Index is a central place for authors todistribute Python packages
I Software Carpentry: http://software-carpentry.org
Lectures/videos about scientific programming.
I Books by Prof. Langtangen: http://folk.uio.no/hpl
Prof. Langtangen has two great books on scientific computing withPython.
Resources and useful packages Functions Programming mathematically Exercises
Python Packages
Python has a “batteries included” philosophy. A standard Pythoninstallation contains many helpful packages.
If the “standard libraries” don’t have what you are looking for, there is arich and growing collection of Python packages for many applications:system interaction, graphics, user interfaces, web programming, scientificcomputing, etc etc.
Installing Python packages is relatively easy with pip:
http://www.pip-installer.org
Resources and useful packages Functions Programming mathematically Exercises
ipython - Interactive Python
Fire up ipython. It’s an interactive Python prompt. I find it helpful forlooking up documentation and experimenting with Python.
A few helpful things:
I tab completion is your friend
I shell commands like cd and ls work
I ?command shows help for a given command
I ??command shows the source code for a given command
I run script.py runs the script script.py, and shows traces nicely
Resources and useful packages Functions Programming mathematically Exercises
numpy - Numerical Python
NumPy is the “fundamental” package for scientific computing withPython
I low-level (C/Fortran) N-dimensional arrays
I broadcasting, dot/matrix products
I 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)
Resources and useful packages Functions Programming mathematically Exercises
scipy - Scientific Python
SciPy is a large collection of packages for more general scientificcomputing.
I ODE solvers and quadrature routines
I optimization and root finding
I signal processing
I sparse linear algebra
I statistics, distributions, random numbers
I masked arrays
I much more...
Resources and useful packages Functions Programming mathematically Exercises
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
Resources and useful packages Functions Programming mathematically Exercises
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()
Resources and useful packages Functions Programming mathematically Exercises
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
Resources and useful packages Functions Programming mathematically Exercises
h5py and pytables - HDF5 storageThe 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 “filesystemwithin 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()
Resources and useful packages Functions Programming mathematically Exercises
mpi4py - Parallel computing (MPI) for Python
There are many ways to do parallel computing. One of them is with aMessage Passing Interface (MPI) library (of which there are severalimplementations).
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
Resources and useful packages Functions Programming mathematically Exercises
Other useful packagesMMMMMOOORE!
While creating this presentation I kept thinking of more cool packagesthat I have used... some of them include
I Mayavi - 3d graphics with OpenGL rendering
I Pandas - data analysis similar in style to R
I PyMC - Markov-Chain Monte-Carlo simulations
I many, many more...
Resources and useful packages Functions Programming mathematically Exercises
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.
Resources and useful packages Functions Programming mathematically Exercises
Defining functionsDefault 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)
Resources and useful packages Functions Programming mathematically Exercises
Defining functionsLambda 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 ]
Resources and useful packages Functions Programming mathematically Exercises
List comprehensions
Python has some nice language constructs to make programming lookmore “mathematical”. Consider the following statement
Let Ω =x2 | 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.
Resources and useful packages Functions Programming mathematically Exercises
Map and reduceFunctional programming
The map routine applies a function to each element of a list. Thefollowing 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 reducelists. To compute the factorial of 10, one could
f = reduce(lambda x, y: x * y, range(1, 11))
Resources and useful packages Functions Programming mathematically Exercises
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)).
Resources and useful packages Functions Programming mathematically Exercises
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 − x3
3+
x5
5+ · · · =
∞∑k=0
(−1)kx1+2k
1 + 2k
Resources and useful packages Functions Programming mathematically Exercises
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 6= 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 14digits?
Resources and useful packages Functions Programming mathematically Exercises
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 thecenter?