**Numerical Python Modules**

Pages: 1, **2**

### Modules

With an understanding of basic manipulation of the multiarray in hand, let's take a quick tour of four modules that are included with the default NumPy package.

**Linear algebra**

The LinearAlgebra module provides a workhorse package bolstered by the LAPACK library, a freely available library of fast matrix routines written in Fortran77. This gives the linear algebra package its speed. We used this package in Numerically Python I to perform both matrix multiplications as well as find the multiplicative inverse of a matrix.

This module also includes find functions for computing the eigen and singular value decompositions, functions that break matrices down into fundamental components and give insight into their structure. The structure of a matrix (the relationships between numbers stored within the matrix) is as important as the actual values the matrix contains. In this module you will also find functions for computing the determinant of a matrix as well as solving the least squares problem. The functions in this module are used to perform most, if not all, of the math associated with solving matrix problems found in science, business, and engineering.

**Fast Fourier**

The Fourier transform is a mathematical operation used by scientists and engineers to convert between frequency and time domain representations of a numerical sequence. If a sequence of samples represents measurements made in time, the Fourier transform can be used to compute the frequency (or cycles) of the measured events.

The Fast Fourier Transform (FFT) is an optimized way to compute the Fourier transform that was realized by Cooley and Tukey in the mid-1960s. Some will argue that the realization of this algorithm was the single most important event in making processing of signal waveforms by computers possible. Digital Signal Processing (DSP) is the heart of many industrial and consumer devices we rely on today -- most importantly, the modem inside your computer that allows you to read this article!

The FFT module provides access to an optimized library for efficiently computing such transforms. To gain access to the module, it needs to be imported:

`>>>from FFT import *`

Functions supported include the forward and inverse FFT for real and complex number sequences and forward FFTs for two-dimensional data sets (useful for image processing).

In general, the Fourier transform can be calculated on a data sequence of any length. However, to take advantage of the FFT algorithm, the data needs to be a power of 2 in length (2,4,8,16,32…..).

The workhorse functions are

`fft(data,n=None,axis=-1)`

, which performs the forward FFT and

`inverse_fft(data,n=None,axis=-1)`

, which performs the inverse FFT.

Remember back to Numerically Python II where we computed the sum of two sinusoids to generate an interesting example for the plot() function using `DISLIN`

? Let's use similar code to see an example of what the FFT routines can do for us.

```
>>> x=arange(128.0)
>>> y=sin(x/3)+cos(x/5)
>>> plot(x,y)
```

generates the plot as before. If we consider the *x* axis to represent time, then taking the FFT will let us measure the frequencies involved.

Resulting plot. |

The following snippet generated the next plot. Notice that there are two peaks shown. Each peak represents the amplitude of each of the two sinusoids added together. In this case the answer is known, but the same general technique can be used to see the amplitudes and frequencies present in measured (not created) data. This forms a particularly powerful tool for analysis.

`>>> plot(x[0:64],abs(fft(y)[0:64]))`

Resulting frequency plot. |

Take another look at the last line of code. Slicing was used to extract and plot only the first 64 data samples. Because the FFT is symmetric for data represented by real (as opposed to complex) samples, the last half of the FFT is a mirror to the first. We only need to plot the first half.

**Random numbers**

The `RandomArray`

module provides support for generating multiarrays of random numbers. Random numbers find their way into many simulation algorithms representing the element of chance or noisy measurement data. Since a computer is generating the numbers, they are not truly "random" but rather have statistical properties simulating truly random numbers.

Like most random number packages, the `RandomArray`

module allows the user to pick a seed value. The computer initializes the random number generator with the seed value. Picking a different seed each time an algorithm uses a random number increases the random nature of things. If you are looking for an identical operation between each run simply fix the seed value to the same value each run.

To use the `RandomArray`

module, import it as follows:

`>>> from RandomArray import *.`

Then set the seed value like this:

`>>>seed(x=0,y=0)`

If no arguments are specified, values will be substituted based on the system clock (not a bad choice, by the way). Typically `seed()`

is only called at the start of a session (or when you want to restart the generated sequence).

Random numbers are specified by their distribution. Different distributions are used to simulate different natural processes. The two most common distributions are the uniform distribution, which can be used to model equally random choices between two boundary conditions (pick any number between 1 and 10) and the normal or Gaussian distribution, which is used to model noise in natural processes (also the so-called "bell curve" from those olden days of high school).

The `RandomArray`

module will compute sequences with either of these distributions as well as others (binomial, Poisson, chi-square, F, gamma, beta, and exponential).

All of the distribution functions accept a shape tuple specifying the desired output matrix size. In addition, some functions also require information about the desired distribution. The following code computes a 3-by-3 matrix of normally distributed random numbers with a mean of zero and a variance of 1.0.

```
>>> a = normal(0.0,1.0,(3,3))
>>> a
array([[ 2.27323961, -0.67850655, 0.39735967],
[ 1.22253323, 1.09771109, 1.43763125],
[ 0.53184462, 0.10846746, 1.87741685]])
>>>
```

**Mlab**

The `Mlab`

(or `MATLAB`

) module provides convenience routines for users familiar with the commercial matrix manipulation package from MathWorks -- MATLAB). The module includes routines for array initializations (eye, zeros, ones), shorthand for the eigen decomposition and singular value functions from the LinearAlgebra module (eig and svd) as well as many functions used in signal processing (sinc, cov, hamming and barlett).

### End game

While the muscle of multiarray manipulation is provided by the fundamental NumPy routines, it is the add-ons that put the shiny chrome face on the distribution. The modules introduced here are only the standard package. In addition to these, users of NumPy have contributed even more modules that you can find at python.org and Travis Oliphant's Python Pages.

As you dig deeper into NumPy, its power and usefulness becomes more evident. Go ahead and try some of the examples! The NumPy documentation contains additional examples for these modules and full definitions of their functions.