Numeric Python and Arrays

There is a built in Python array type, but it is generally considered slow, difficult to use, and "non-sexy". Numeric is a module developed by a group interested in high performance array computations in scientific applications to address these problems. Numeric Arrays have quite a few built in operations and some of the numerical tricks of Linpack and other number crunching libraries.


Numeric, Numpy, and Scipy.core

The Numeric community fractured when the future replacement for Numeric (NumPy) was initially released. For a while Numeric users were in 2 camps, some for Numeric, and some for Numpy. The reason for this schism was that Numeric was generally those who had to create a large number of small arrays, and NumPy was faster for those who wanted to use huge arrays.

Recently the Numeric community has supposedly reunited behind the SciPy Organization and a new array package has been released called Scipy.core.

Where Numeric used to mean a specific package, it is beginning to be used to refer to any of these Python array packages. At least that's my rationalization for calling this the "Numeric" page of my tutorial instead of the "Scipy.core" page.

Numeric and C

If you are interested in working with compiled code and Numeric arrays (i.e.: passing Numeric(Python) arrays to C / passing C arrays to Numeric(Python) ) you should take a look at my new page that deals with that topic. It shows a simple example of code that can take Numeric array from Python and access it in C.

Return to the top


Quick Numeric Examples

Numeric arrays are not terribly difficult to use, so I'm not going to spend a lot of time on them, but they are worth getting ahold of and using, especially in graphics or numerical programs. There is a significant difference(some might say a HUGE difference) in performance between Numeric Arrays and the built in Python lists. If you ever find yourself tempted to write multidimensional Python Lists I strongly encourage you to pick up and use Numeric instead.

I'm going to do some brief examples. They are very similar to Python lists, and it probably helps to think of them as just special cases of lists. They support many of the same operations, especially slicing, and are mutable(you can change size and shape on the fly).

Hello World!

So you can create an Numeric array from a list using the "Numeric.array" constructor. This is kind of an unrealistic usage. It is faster and more efficient to just create an array using the following methods:

Hello World!

Numeric arrays can also be sliced:

Hello World!

Here's a few additional operations that might be useful. There are quite a few more operations as you'll see in the next section.

Hello World!

Return to the top


Numeric Methods

You can access Numeric arrays just like standard Python lists (using [] brackets), but looping over these arrays using for is not going to be the most efficient. Like many math/array packages Numeric includes quite a few specialized routines to help you take full advantage of Numeric arrays. Depending on what you want to do Numeric may have a specialized method that works quickly and efficently over arrays.

Some simple examples are the Numeric.sum and Numeric.average functions. There is a large selection of Numeric operations that can be quickly accessed by typing "help(Numeric)" at the Python prompt. Here is a brief summary of those functions(shamelessly stolen from Numeric's documentation):


Functions:
            
    -   array                      - NumPy Array construction
    -   zeros                      - Return an array of all zeros
    -   shape                      - Return shape of sequence or array
    -   rank                       - Return number of dimensions
    -   size                       - Return number of elements in entire array or a
                                     certain dimension
    -   fromstring                 - Construct array from (byte) string
    -   take                       - Select sub-arrays using sequence of indices
    -   put                        - Set sub-arrays using sequence of 1-D indices
    -   putmask                    - Set portion of arrays using a mask
    -   reshape                    - Return array with new shape
    -   repeat                     - Repeat elements of array
    -   choose                     - Construct new array from indexed array tuple
    -   cross_correlate            - Correlate two 1-d arrays
    -   searchsorted               - Search for element in 1-d array
    -   sum                        - Total sum over a specified dimension
    -   average                    - Average, possibly weighted, over axis or array.
    -   cumsum                     - Cumulative sum over a specified dimension
    -   product                    - Total product over a specified dimension
    -   cumproduct                 - Cumulative product over a specified dimension
    -   alltrue                    - Logical and over an entire axis
    -   sometrue                   - Logical or over an entire axis
    -   allclose                   - Tests if sequences are essentially equal

    -   arrayrange (arange)        - Return regularly spaced array
    -   asarray                    - Guarantee NumPy array
    -   sarray                     - Guarantee a NumPy array that keeps precision
    -   convolve                   - Convolve two 1-d arrays
    -   swapaxes                   - Exchange axes
    -   concatenate                - Join arrays together
    -   transpose                  - Permute axes
    -   sort                       - Sort elements of array
    -   argsort                    - Indices of sorted array
    -   argmax                     - Index of largest value
    -   argmin                     - Index of smallest value
    -   innerproduct               - Innerproduct of two arrays
    -   dot                        - Dot product (matrix multiplication)
    -   outerproduct               - Outerproduct of two arrays
    -   resize                     - Return array with arbitrary new shape
    -   indices                    - Tuple of indices
    -   fromfunction               - Construct array from universal function
    -   diagonal                   - Return diagonal array
    -   trace                      - Trace of array
    -   dump                       - Dump array to file object (pickle)
    -   dumps                      - Return pickled string representing data
    -   load                       - Return array stored in file object
    -   loads                      - Return array from pickled string
    -   ravel                      - Return array as 1-D
    -   nonzero                    - Indices of nonzero elements for 1-D array
    -   shape                      - Shape of array
    -   where                      - Construct array from binary result
    -   compress                   - Elements of array where condition is true
    -   clip                       - Clip array between two values
    -   zeros                      - Array of all zeros
    -   ones                       - Array of all ones
    -   identity                   - 2-D identity array (matrix)

(Universal) Math Functions

           add                    logical_or             exp
           subtract               logical_xor            log
           multiply               logical_not            log10
           divide                 maximum                sin
           divide_safe            minimum                sinh
           conjugate              bitwise_and            sqrt
           power                  bitwise_or             tan
           absolute               bitwise_xor            tanh
           negative               invert                 ceil
           greater                left_shift             fabs
           greater_equal          right_shift            floor
           less                   arccos                 arctan2
           less_equal             arcsin                 fmod
           equal                  arctan                 hypot
           not_equal              cos                    around
           logical_and            cosh                   sign
           arccosh                arcsinh                arctanh
        

Return to the top


Indepth Examples

Steady State Heat (Simple 2D Partial Differential Equation problem)

I realize that this example is a little bit more computationally intensive then many in this tutorial. However, given the subject matter it seems natural to do something like this here.

None of the code below is something that you won't have already seen so far, so I'm going to just show it to you and let you experiment with it.

Hello World!

Download steady-state.py

Return to the top

Steady State with PyGame Visualization

Check it out: PyGame Examples

heat.png