4.2 Arrays in Python

In lower level languages common mathematical operations on arrays must be done ``manually''. For example, we might have a three element array that represents a vector. To double the length of the vector we simply multiply it by two:


\begin{displaymath}
2 \times \left(
\begin{array}{c}
2 \\
4 \\
-11
\end{...
...ft(
\begin{array}{c}
4 \\
8 \\
-22
\end{array} \right)
\end{displaymath}

In many languages (C for example) the programming equivalent is more complicated. You step through the array an element at a time, multiplying each element by two. In Python this might be done like this:

    >>> xx = array([2, 4, -11])
    >>> yy = zeros(3, Int)        # Create empty array ready to receive result
    >>> for i in range(0, 3):
            yy[i] = xx[i] * 2
    >>> print yy
    [  4   8 -22]

However, this is not required. Python's arrays ``understand'' common mathematical operations and will generally Do The Right Thing. Examples follow.

If you add a number to an array, it gets added to each element...

    >>> xx = array([2, 4, -11])
    >>> yy = xx + 0.1
    >>> print yy
    [  2.1   4.1  -10.9]

...and if you multiply an array by a number, each element gets individually multiplied (like vectors)...

    >>> print xx * 2
    [  4    8  -22 ]

If you add two arrays together the corresponding element in each array gets added together (again, like vectors)...

    >>> zz = array([5, 5, 5])
    >>> print xx + zz
    [ 7   9   -6]

...but of course the arrays must be of the same dimensions (see Figure 4.1):

    >>> zz = array([5, 5, 5, 5])
    >>> print xx + zz
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: frames are not aligned

Figure 4.1: Arrays must be of the same dimensions in order to add them together

These kind of statements save a lot of time. The array library contains many other useful functions. For example, one can compute the dot product of two arrays using the dot() function. This provides a much quicker way of doing the exercise in Section 3.10.

Arrays can be of more than one dimension. A two-dimensional array is similar to a matrix in mathematics. Consider the following matrix:



We create such an array in Python as follows:

    >>> xx = zeros([2,2], Float)  # Recall 2nd parameter to zeros is the type
    >>> print xx
    [[0 0]
     [0 0]]
    >>> xx[0][0] = 5.3
    >>> print xx
    [[ 5.3  0. ]
     [ 0.   0. ]]
    >>> xx[1][0] = 4
    >>> xx[0][1] = -10
    >>> xx[1][1] = 16
    >>> print xx
    [[  5.3 -10. ]
     [  4.   16. ]]
    >>> print xx[1][0]
    4.0

The first parameter that zeros() is passed maybe a list rather than a single number. This then gives the dimensions of the array (number of rows first, then columns). Elements of the array are then indexed using two numbers enlcosed in square brackets. First the row offset from the top left, then the column offset.

Two-dimensional arrays are used widely in Physics, and also in the manipulation of graphical images, although you will not find much need for them in the first year problems you will be attempting in this trial.