IdentifiantMot de passe
Mot de passe oublié ?
3.1 Numarray Objects

# 3.1 Numarray Objects

The array objects are generally homogeneous collections of potentially large numbers of numbers. All numbers in a numarray are the same kind (i.e. number representation, such as double-precision floating point). Array objects must be full (no empty cells are allowed), and their size is immutable. The specific numbers within them can change throughout the life of the array, however. There is a "mask array" package ("MA") for Numeric, which has been ported to numarray as ``numarray.ma''.

Mathematical operations on arrays return new arrays containing the results of these operations performed element-wise on the arguments of the operation.

The size of an array is the total number of elements therein (it can be 0 or more). It does not change throughout the life of the array, unless the array is explicitly resized using the resize function.

The shape of an array is the number of dimensions of the array and its extent in each of these dimensions (it can be 0, 1 or more). It can change throughout the life of the array. In Python terms, the shape of an array is a tuple of integers, one integer for each dimension that represents the extent in that dimension. The rank of an array is the number of dimensions along which it is defined. It can change throughout the life of the array. Thus, the rank is the length of the shape (except for rank 0). Note: This is not the same meaning of rank as in linear algebra.

Use more familiar mathematicial examples: A vector is a rank-1 array (it has only one dimension along which it can be indexed). A matrix as used in linear algebra is a rank-2 array (it has two dimensions along which it can be indexed). It is possible to create a rank-0 array which is just a scalar of one single value -- it has no dimension along which it can be indexed.

The type of an array is a description of the kind of element it contains. It determines the itemsize of the array. In contrast to Numeric, an array type in numarray is an instance of a NumericType class, rather than a single character code. However, it has been implemented in such a way that one may use aliases, such as `u1', `i1', `i2', `i4', `f4', `f8', etc., as well as the original character codes, to set array types. The itemsize of an array is the number of 8-bit bytes used to store a single element in the array. The total memory used by an array tends to be its size times its itemsize, when the size is large (there is a fixed overhead per array, as well as a fixed overhead per dimension).

Here is an example of Python code using the array objects:

```>>> vector1 = array([1,2,3,4,5])
>>> print vector1
[1 2 3 4 5]
>>> matrix1 = array([[0,1],[1,3]])
>>> print matrix1
[[0 1]
[1 3]]
>>> print vector1.shape, matrix1.shape
(5,) (2,2)
>>> print vector1 + vector1
[ 2  4  6  8  10]
>>> print matrix1 * matrix1
[[0 1]                                  # note that this is not the matrix
[1 9]]                                 # multiplication of linear algebra
```
If this example complains of an unknown name "array", you forgot to begin your session with
```>>> from numarray import *
```
See section 2.3.1.

Send comments to the NumArray community.