Beruflich Dokumente
Kultur Dokumente
org/SciPy_T utorial
SciPy Tutorial
Contents
Before reading this tutorial you should know a bit of Python. If this is not the case, or if
you want to refresh your memory, take a look at the Python tutorial. In particular, you
may wish to read up to section 6 (Modules).
You also need to have some software installed on your computer. You need at least
Python
and NumPy (It is useful to read the Tentative NumPy Tutorial)
and SciPy,
but there are some other tools that may be useful to you:
1 of 9 10/14/2008 03:27 PM
SciPy T utorial - http://www.scipy.org/SciPy_T utorial
For the basic matrix operations you can make a matrix like an array in the NumPy (see
the Tentative NumPy Tutorial):
The scipy.sparse module provides data structures for 2D sparse matrices. There are
five available sparse matrix types:
2 of 9 10/14/2008 03:27 PM
SciPy T utorial - http://www.scipy.org/SciPy_T utorial
scipy.sparse currently only handles float numbers (both 32 and 64bits, both
real and complex).
CSC format is also called Harwell-Boeing format, and used in other libraries or
software (Matlab for example).
This is a very simple example illustrating basic usage of linsolve, sparse and some
other useful things (use the latest version of linsolve.py). If you don't know how to get
hold of the latest modules, spend some time in the Developer Zone and see the
SOURCE CODE section.
We're going to solve a trivial case of Ax = b, where A is a matrix, b a vector (the RHS)
and x the unknowns. In this example A refers to the 'normal' matrix, and Asp to the
sparse representation of A, x for the 'normal' solution and xsp for the solution arising
from using the sparse method. (You'll most probably find it useful to use IPython when
going through this example, especially if you're used to Matlab®. The normal Python
prompt looks like this >>>, IPython's default prompt (one can change it) looks like this
In [x]:, where the x is a number).
Import the necessary commands from the numpy and scipy modules:
It is of course also possible to import all the commands from the two modules, but in
that case it might not be a bad idea to do it as follows (note that this is just one way of
doing it):
import numpy as N
import scipy as S
If you're working in an IPython session, type S. and press the <TAB> button, you'll see
all the available commands (attributes). The same goes for N.
Construct an identity 1000x1000 lil_matrix. There is a couple of ways of doing this,
one method is slow, the other not. A slow method:
A = eye(1000)
Asp = sparse.lil_matrix(A)
A faster method:
3 of 9 10/14/2008 03:27 PM
SciPy T utorial - http://www.scipy.org/SciPy_T utorial
Asp = sparse.lil_matrix((1000,1000))
Asp.setdiag(ones(1000))
Note that Asp is still in LInked List (lil_matrix) format - leave it like that for now.
Now create vector b; we choose the entries so that we can easily check the (trivial)
solution:
b = arange(1,1001)
x = linalg.solve(A,b)
xsp = linsolve.spsolve(Asp,b)
Check the result: x and xsp should both be equal to b, as one expects. A convenient way
of checking this is to make use of allclose(). In IPython you can type allclose? to
get help on it, and at the Python prompt one will type help(allclose) (this is the case
for almost all commands, but some might not have a Docstring). The input and resulting
output from the command is shown here:
>>> allclose(x,b)
True
>>> allclose(b,xsp)
True
Let's have a look at the difference in solution time between the two methods. In an
IPython session, simply type (or scroll back with your arrow keys and just insert the
time in front):
time x = linalg.solve(A,b)
Itime xsp1 = linsolve.spsolve(Asp,b)
4 of 9 10/14/2008 03:27 PM
SciPy T utorial - http://www.scipy.org/SciPy_T utorial
We saw that we can get the solution to Ax = b even if A is in LIL format. We'll now
inspect the difference in execution speed for the following (assuming that you're using
IPython, Out [x]:'s not shown, and in a new session):
Note: the line that returns xsp3 fails if you don't have a recent enough version of scipy.
The time for the last solution (xsp3) should be the fastest by a factor of roughly 1,5 to 2
times compared to the other two solutions (xsp1 and xsp2). The benefit in speed when
compared to using 'normal' methods for solving sparse systems is obvious.
A = A.tocsr()
b = random.rand(1000)
x = linsolve.spsolve(A * A.T, b)
The .T bit of A.T above computes the transpose of A, i.e., it's a shortcut for
A.transpose(). It will also work for 'normal' dense (numpy) matrices and
5 of 9 10/14/2008 03:27 PM
SciPy T utorial - http://www.scipy.org/SciPy_T utorial
currently works for arrays in the latest SVN versions of NumPy. Also take note
of the differences between matrices and arrays when working with them. If B is a
matrix and C an array, both with the same size and entries, then B*B is not the
same as C*C. See NumPy for Matlab Users for more details.
Convert it to a dense matrix and solve, and check that the result is the same:
A_ = A.todense()
x_ = linalg.solve(A_ * A_.T, b)
err = linalg.norm(x-x_)
It should be small
The correct answer is of course 1/3. quad returns a tuple consisting of its result and its
estimate of the error in its result. In this case we can see that its estimate is pretty good.
For a slightly more difficult example, let's integrate a function (related to) the blackbody
spectrum:
6 of 9 10/14/2008 03:27 PM
SciPy T utorial - http://www.scipy.org/SciPy_T utorial
The fact that the integrand can't be evaluated at zero doesn't seem to be a problem either:
Under the hood, quad uses a sophisticated method ("Clenshaw-Curtis with Chebyshev
moments") from the FORTRAN package QUADPACK which attempts to efficiently
compute which regions contain complicated behaviour and spends more effort evaluating
the function there. It can be tricked:
Here all I have done is a change of variable t=x*1e-5, so the value of the integral should
not have changed at all, but unfortunately quad fails to find the peak of the function, so it
gives a wildly wrong answer with a wildly wrong error estimate.
Scipy includes several robust ODE solvers. Let's take the simplest possible example and
integrate the equation y'=y:
Here we provided odeint with a function that computes y' given y and t - in this case we
used an anonymous python function, but any function will do. We also provided it with
an initial value of y (1) and the initial and final values of t (0 and 1). We then obtained
the result evaluated at those times (1 and e, as we would expect). To get more values:
7 of 9 10/14/2008 03:27 PM
SciPy T utorial - http://www.scipy.org/SciPy_T utorial
Here we see that the solution is behaving like cos(t), as we would have hoped. There is a
certain amount of error; the error tolerance is one of the many things that can be adjusted
with extra parameters to odeint.
Seems like people are planning to put a new online tutorial here.
Until they do, you can use this link, which is a direct conversion of Travis Oliphant's
original LyX document (usually available in pdf format) to html.
This html tutorial was created using a simple htlatex command in the tutorial source
directory of the "old scipy" branch - available here
The resulting files are available as one archived file: scipy_tutorial.tar.gz.
The original document was written by TravisOliphant. I think it's copyrighted to
Enthought under the following license.
If you think it's useful, and wish to host it elsewhere, please do, because the current
place is temporary and I don't intend to keep it forever.
-- AmitAronovitch 2006-02-17 22:58:57
8 of 9 10/14/2008 03:27 PM
SciPy T utorial - http://www.scipy.org/SciPy_T utorial
9 of 9 10/14/2008 03:27 PM