SciPy - Library of scientific algorithms for Python
J.R. Johansson (jrjohansson at gmail.com)
The latest version of this IPython notebook lecture is available at http://github.com/jrjohansson/scientific-python-lectures.
The other notebooks in this lecture series are indexed at http://jrjohansson.github.io.
Introduction
The SciPy framework builds on top of the low-level NumPy framework for multidimensional arrays, and provides a large number of higher-level scientific algorithms. Some of the topics that SciPy covers are:
Special functions (scipy.special)
Integration (scipy.integrate)
Optimization (scipy.optimize)
Interpolation (scipy.interpolate)
Fourier Transforms (scipy.fftpack)
Signal Processing (scipy.signal)
Linear Algebra (scipy.linalg)
Sparse Eigenvalue Problems (scipy.sparse)
Statistics (scipy.stats)
Multi-dimensional image processing (scipy.ndimage)
File IO (scipy.io)
Each of these submodules provides a number of functions and classes that can be used to solve problems in their respective topics.
In this lecture we will look at how to use some of these subpackages.
To access the SciPy package in a Python program, we start by importing everything from the scipy
module.
If we only need to use part of the SciPy framework we can selectively include only those modules we are interested in. For example, to include the linear algebra package under the name la
, we can do:
Special functions
A large number of mathematical special functions are important for many computional physics problems. SciPy provides implementations of a very extensive set of special functions. For details, see the list of functions in the reference documention at http://docs.scipy.org/doc/scipy/reference/special.html#module-scipy.special.
To demonstrate the typical usage of special functions we will look in more detail at the Bessel functions:
Integration
Numerical integration: quadrature
Numerical evaluation of a function of the type
is called numerical quadrature, or simply quadature. SciPy provides a series of functions for different kind of quadrature, for example the quad
, dblquad
and tplquad
for single, double and triple integrals, respectively.
The quad
function takes a large number of optional arguments, which can be used to fine-tune the behaviour of the function (try help(quad)
for details).
The basic usage is as follows:
If we need to pass extra arguments to integrand function we can use the args
keyword argument:
For simple functions we can use a lambda function (name-less function) instead of explicitly defining a function for the integrand:
As show in the example above, we can also use 'Inf' or '-Inf' as integral limits.
Higher-dimensional integration works in the same way:
Note how we had to pass lambda functions for the limits for the y integration, since these in general can be functions of x.
Ordinary differential equations (ODEs)
SciPy provides two different ways to solve ODEs: An API based on the function odeint
, and object-oriented API based on the class ode
. Usually odeint
is easier to get started with, but the ode
class offers some finer level of control.
Here we will use the odeint
functions. For more information about the class ode
, try help(ode)
. It does pretty much the same thing as odeint
, but in an object-oriented fashion.
To use odeint
, first import it from the scipy.integrate
module
A system of ODEs are usually formulated on standard form before it is attacked numerically. The standard form is:
where
and is some function that gives the derivatives of the function . To solve an ODE we need to know the function and an initial condition, .
Note that higher-order ODEs can always be written in this form by introducing new variables for the intermediate derivatives.
Once we have defined the Python function f
and array y_0
(that is and in the mathematical formulation), we can use the odeint
function as:
where t
is and array with time-coordinates for which to solve the ODE problem. y_t
is an array with one row for each point in time in t
, where each column corresponds to a solution y_i(t)
at that point in time.
We will see how we can implement f
and y_0
in Python code in the examples below.
Example: double pendulum
Let's consider a physical example: The double compound pendulum, described in some detail here: http://en.wikipedia.org/wiki/Double_pendulum
The equations of motion of the pendulum are given on the wiki page:
To make the Python code simpler to follow, let's introduce new variable names and the vector notation:
Simple annimation of the pendulum motion. We will see how to make better animation in Lecture 4.
Example: Damped harmonic oscillator
ODE problems are important in computational physics, so we will look at one more example: the damped harmonic oscillation. This problem is well described on the wiki page: http://en.wikipedia.org/wiki/Damping
The equation of motion for the damped oscillator is:
where is the position of the oscillator, is the frequency, and is the damping ratio. To write this second-order ODE on standard form we introduce :
In the implementation of this example we will add extra arguments to the RHS function for the ODE, rather than using global variables as we did in the previous example. As a consequence of the extra arguments to the RHS, we need to pass an keyword argument args
to the odeint
function:
Fourier transform
Fourier transforms are one of the universal tools in computational physics, which appear over and over again in different contexts. SciPy provides functions for accessing the classic FFTPACK library from NetLib, which is an efficient and well tested FFT library written in FORTRAN. The SciPy API has a few additional convenience functions, but overall the API is closely related to the original FORTRAN library.
To use the fftpack
module in a python program, include it using:
To demonstrate how to do a fast Fourier transform with SciPy, let's look at the FFT of the solution to the damped oscillator from the previous section:
Since the signal is real, the spectrum is symmetric. We therefore only need to plot the part that corresponds to the postive frequencies. To extract that part of the w
and F
we can use some of the indexing tricks for NumPy arrays that we saw in Lecture 2:
As expected, we now see a peak in the spectrum that is centered around 1, which is the frequency we used in the damped oscillator example.
Linear algebra
The linear algebra module contains a lot of matrix related functions, including linear equation solving, eigenvalue solvers, matrix functions (for example matrix-exponentiation), a number of different decompositions (SVD, LU, cholesky), etc.
Detailed documetation is available at: http://docs.scipy.org/doc/scipy/reference/linalg.html
Here we will look at how to use some of these functions:
Linear equation systems
Linear equation systems on the matrix form
where is a matrix and are vectors can be solved like:
We can also do the same with
where are matrices:
Eigenvalues and eigenvectors
The eigenvalue problem for a matrix :
where is the th eigenvector and is the th eigenvalue.
To calculate eigenvalues of a matrix, use the eigvals
and for calculating both eigenvalues and eigenvectors, use the function eig
:
The eigenvectors corresponding to the th eigenvalue (stored in evals[n]
) is the th column in evecs
, i.e., evecs[:,n]
. To verify this, let's try mutiplying eigenvectors with the matrix and compare to the product of the eigenvector and the eigenvalue:
There are also more specialized eigensolvers, like the eigh
for Hermitian matrices.
Matrix operations
Sparse matrices
Sparse matrices are often useful in numerical simulations dealing with large systems, if the problem can be described in matrix form where the matrices or vectors mostly contains zeros. Scipy has a good support for sparse matrices, with basic linear algebra operations (such as equation solving, eigenvalue calculations, etc).
There are many possible strategies for storing sparse matrices in an efficient way. Some of the most common are the so-called coordinate form (COO), list of list (LIL) form, and compressed-sparse column CSC (and row, CSR). Each format has some advantanges and disadvantages. Most computational algorithms (equation solving, matrix-matrix multiplication, etc) can be efficiently implemented using CSR or CSC formats, but they are not so intuitive and not so easy to initialize. So often a sparse matrix is initially created in COO or LIL format (where we can efficiently add elements to the sparse matrix data), and then converted to CSC or CSR before used in real calcalations.
For more information about these sparse formats, see e.g. http://en.wikipedia.org/wiki/Sparse_matrix
When we create a sparse matrix we have to choose which format it should be stored in. For example,
More efficient way to create sparse matrices: create an empty matrix and populate with using matrix indexing (avoids creating a potentially large dense matrix)
Converting between different sparse matrix formats:
We can compute with sparse matrices like with dense matrices:
Optimization
Optimization (finding minima or maxima of a function) is a large field in mathematics, and optimization of complicated functions or in many variables can be rather involved. Here we will only look at a few very simple cases. For a more detailed introduction to optimization with SciPy see: http://scipy-lectures.github.com/advanced/mathematical_optimization/index.html
To use the optimization module in scipy first include the optimize
module:
Finding a minima
Let's first look at how to find the minima of a simple function of a single variable:
We can use the fmin_bfgs
function to find the minima of a function:
We can also use the brent
or fminbound
functions. They have a bit different syntax and use different algorithms.
Finding a solution to a function
To find the root for a function of the form we can use the fsolve
function. It requires an initial guess:
Interpolation
Interpolation is simple and convenient in scipy: The interp1d
function, when given arrays describing X and Y data, returns and object that behaves like a function that can be called for an arbitrary value of x (in the range covered by X), and it returns the corresponding interpolated y value:
Statistics
The scipy.stats
module contains a large number of statistical distributions, statistical functions and tests. For a complete documentation of its features, see http://docs.scipy.org/doc/scipy/reference/stats.html.
There is also a very powerful python package for statistical modelling called statsmodels. See http://statsmodels.sourceforge.net for more details.
Statistics:
Statistical tests
Test if two sets of (independent) random data comes from the same distribution:
Since the p value is very large we cannot reject the hypothesis that the two sets of random data have different means.
To test if the mean of a single sample of data has mean 0.1 (the true mean is 0.0):
Low p-value means that we can reject the hypothesis that the mean of Y is 0.1.
Further reading
http://www.scipy.org - The official web page for the SciPy project.
http://docs.scipy.org/doc/scipy/reference/tutorial/index.html - A tutorial on how to get started using SciPy.
https://github.com/scipy/scipy/ - The SciPy source code.
Versions
Software | Version |
---|---|
Python | 2.7.10 64bit [GCC 4.2.1 (Apple Inc. build 5577)] |
IPython | 3.2.1 |
OS | Darwin 14.1.0 x86_64 i386 64bit |
numpy | 1.9.2 |
matplotlib | 1.4.3 |
scipy | 0.16.0 |
Sat Aug 15 11:13:18 2015 JST |