NumPy provides most of the numerical methods on ndarrays like:
In addition to this, there are also some optimized modules, all written in C, which provides it some number-crunching capabilities. This is why a C-compiler is needed for NumPy building.
In [1]:
%pylab inline
In [2]:
s = numpy.random.random(10000)
hist(s, 10, normed=True)
Out[2]:
In [3]:
i = numpy.random.random_integers(0, 65000, size=128*128).reshape((128,128))
imshow(i)
Out[3]:
In [4]:
hist(i.flat, 10, normed=True)
Out[4]:
Many univariate or multivariate distribution are available ... The best is just to refer to the numpy.random documentation.
Here is an example of Poisson distribution
In [5]:
p = numpy.random.poisson(10, 10000)
hist(p, 10, normed=True)
Out[5]:
Linear algebra function are available in the numpy.linalg submodule which is basically a Python wrapping of the BLAS and LAPACK Fortran packages which have been translated to C using f2c. Once again no Fortran compiler is needed but Fortran memory alignment may be needed.
The version shipped by default with NumPy is based on the BLAS and LAPACK from netlib and rely on the cleverness of your compiler to get good performances. As the API of BLAS and LAPACK is fixed, it is possible to link against highly optimized libraries like the MKL from Intel ...
Linalg contains tools to calculate:
In [6]:
import numpy
numpy.linalg?
Fourier analysis is fundamentally a method for expressing a function as a sum of periodic components, and for recovering the signal from those components. When both the function and its Fourier transform are replaced with discretized counterparts, it is called the discrete Fourier transform (DFT). The DFT has become a mainstay of numerical computing in part because of a very fast algorithm for computing it, called the Fast Fourier Transform (FFT), which was known to Gauss (1805) and was brought to light in its current form by Cooley and Tukey (1965).
NumPy's FFT is based on the FFTPACK ForTran code, translated to C using f2c and wrapped in Pythonic way. It provides 1d, 2d and nd, direct and inverse functions.
In [7]:
numpy.fft?
Load an image and calculate its power spectrum:
In [8]:
import scipy.misc
img = scipy.misc.lena()
imshow(img, cmap="gray")
Out[8]:
In [9]:
f2 = numpy.fft.fft2(img)
psd = numpy.fft.fftshift(10 * numpy.log10(abs(f2) ** 2))
imshow(psd)
Out[9]:
Note that the FFTw library is not (no more) used is NumPy for licensing issues. While faster and multi-threaded, FFTw is also harder to use as it requires the generation of a plan ...
My personnal experience with FFT in high perfomance computer is that it is worth going to GPU using cuFFT when performances matters.
What can we do if we need to access to other optimized, compiled libraries? f2py
In [ ]: