When you need more advanced mathematical functions, where do you look?
Understand that SciPy exists and what kinds of things it has.
Non-objective: know details of everything (or anything) in SciPy.
Main article: SciPy documentation
SciPy is a library that builds on top of NumPy. It contains a lot of interfaces to battle-tested numerical routines written in Fortran or C, as well as python implementations of many common algorithms.
What’s in SciPy?¶
Briefly, it contains functionality for
Special functions (Bessel, Gamma, etc.)
Fast Fourier Transform (FFT)
Linear algebra (more complete than in NumPy)
More I/O routine, e.g. Matrix Market format for sparse matrices, MATLAB files (.mat), etc.
Many (most?) of these are not written specifically for SciPy, but use the best available open source C or Fortran libraries. Thus, you get the best of Python and the best of compiled languages.
Most functions are documented ridiculously well from a scientific standpoint: you aren’t just using some unknown function, but have a full scientific description and citation to the method and implementation.
Example: Numerical integration¶
Define a function of one variable and using
calculate the integral of your function in the
[0.0, 4.0]. Then vary the interval and also modify the function and check
whether scipy can integrate it.
from scipy import integrate def myfunction(x): # you need to define result return result integral = integrate.quad(myfunction, 0.0, 4.0) print(integral)
quad uses the Fortran library QUADPACK, which one can assume is pretty good. You can also see a whole lot of scientific information about the function on the docs page - including the scientific names of the methods used.
Use the SciPy sparse matrix functionality to create a random sparse
matrix with a probability of non-zero elements of 0.05 and size 10000
x 10000. The use the SciPy sparse linear algebra support to calculate
the matrix-vector product of the sparse matrix you just created and a
random vector. Use the %timeit macro to measure how long it
takes. Does the optional
format argument when you create the
sparse matrix make a difference?
Then, compare to how long it takes if you’d instead first convert the
sparse matrix to a normal NumPy dense array, and use the NumPy
method to calculate the matrix-vector product.
Can you figure out a quick rule of thumb when it’s worth using a sparse matrix representation vs. a dense representation?