Numerical python (commonly referred to as NumPy) is a widely used Python extension library for fast processing of fixed-type arrays of any number of dimensions. Because the underlying code is a fully optimized C language code, the primary operation of the array is no longer limited by the Python interpreter when it executes in the NumPy call. Because NumPy has achieved such success, NumPy developers will replace NumPy with a new module called Numarray, which is largely (but not entirely) compatible with NumPy. In this article, David describes the general features of NumPy and some of the special improvements that Numarray will bring.
The first thing to understand about the numerical Python package is that numerical python will not let you do any work that standard Python cannot do. It just allows you to do the same tasks that standard Python can accomplish with much faster speed. More than that, many array operations are much more elegant to express in Numeric or Numarray than by using standard Python data types and syntax. However, the astonishing speed is the main reason to attract users to use numerical Python.
In fact, numerical Python just implements a new data type: an array. Unlike lists, tuples, and dictionaries that can contain different types of elements, a Numarray array can contain only the same type of data. Another advantage of the Numarray array is that it can be multidimensional-but the dimensions of the array are slightly different from the simple nesting of the lists. Numerical Python draws on the programmer's hands-on experience (especially those with a scientific computing background that abstracts the best features of the array in APL, FORTRAN, MATLAB, and S), creating arrays that can flexibly change shapes and dimensions. We'll be back soon to continue the subject.
The operations of an array in numerical Python are based on elements. Although two-dimensional arrays are similar to matrices in linear algebra, their operations (such as multiplication) and operations in linear algebra (such as matrix multiplication) are completely different.
Let's take a look at a concrete example of the above question. In pure Python, you can create a two-dimensional list like this:
Listing 1. Nested Arrays of Python
>>> pyarr = [[1,2,3],
... [4,5,6],
... [7,8,9]]
>>> print pyarr
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> pyarr[1][1] = 0
>>> print pyarr
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]
Good, but what you can do with this structure is simply to set and retrieve the elements through a separate (or multidimensional) index. Numarray arrays are more flexible than this:
Listing 2. Numerical Python Array
>>> from numarray import *
>>> numarr = array(pyarr)
>>> print numarr
[[1 2 3]
[4 0 6]
[7 8 9]]
It's not a big change, but what about the operation with Numarray? Here is an example:
Listing 3. Element actions
>>> numarr2 = numarr * 2
>>> print numarr2
[[ 2 4 6]
[ 8 0 12]
[14 16 18]]
>>> print numarr2 + numarr
[[ 3 6 9]
[12 0 18]
[21 24 27]]