IdentifiantMot de passe
Mot de passe oublié ?Je m'inscris ! (gratuit)
14.2 Fundamental data structures

14.2 Fundamental data structures

14.2.1 Numarray Numerical Data Types

Numarray hides the C implementation of its basic array elements behind a set of C typedefs which specify the absolute size of the type in bits. This approach enables a programmer to specify data items of arrays and extension functions in an explicit yet portable manner. In contrast, basic C types are platform relative, and so less useful for describing real physical data. Here are the names of the concrete Numarray element types:

  • Bool
  • Int8, UInt8
  • Int16, UInt16
  • Int32, UInt32
  • Int64, UInt64
  • Float32, Float64
  • Complex32, Complex64

14.2.2 NumarrayType

The type of a numarray is communicated in C via one of the following enumeration constants. Type codes which are backwards compatible with Numeric are defined in terms of these constants, but use these if you're not already using the Numeric codes. These constants communicate type requirements between one function and another, since in C, you cannot pass a typedef as a value. tAny is used to specify both ``no type requirement'' and ``no known type'' depending on context.

typedef enum 

  tInt8,      tUInt8,
  tInt16,     tUInt16,
  tInt32,     tUInt32, 
  tInt64,     tUInt64,
  tFloat32,   tFloat64,
  tComplex32, tComplex64,

  tDefault = tFloat64,

#if LP64
  tLong = tInt64
  tLong = tInt32

} NumarrayType;

14.2.3 PyArray_Descr

PyArray_Descr is used to hold a few parameters related to the type of an array and exists mostly for backwards compatability with Numeric. type_num is a NumarrayType value. elsize indicates the number of bytes in one element of an array of that type. type is a Numeric compatible character code.

Numarray's PyArray_Descr is currently missing the type-casting, ones, and zeroes functions. Extensions which use these missing Numeric features will not yet compile. Arrays of type Object are not yet supported.

typedef struct {
        int  type_num;  /* PyArray_TYPES */
        int  elsize;    /* bytes for 1 element */
        char type;      /* One of "cb1silfdFD "  Object arrays not supported. */
} PyArray_Descr;

14.2.4 PyArrayObject

The fundamental data structure of numarray is the PyArrayObject, which is named and layed out to provide source compatibility with Numeric. It is compatible with most but not all Numeric code. The constant MAXDIM, the maximum number of dimensions in an array, is defined as 40. It should be noted that unlike earlier versions of numarray, the present PyArrayObject structure is a first class python object, with full support for the number protocols in C. Well-behaved arrays have mutable fields which will reflect modifications back into Python``for free''.

typedef int maybelong;          /* towards 64-bit without breaking extensions. */

typedef struct {
        /* Numeric compatible stuff */

        char *data;              /* points to the actual C data for the array */
        int nd;                  /* number of array shape elements */
        maybelong *dimensions;   /* values of shape elements */
        maybelong *strides;      /* values of stride elements */
        PyObject *base;          /* unused, but don't touch! */
        PyArray_Descr *descr;    /* pointer to descriptor for this array's type */
        int flags;               /* bitmask defining various array properties */

        /* numarray extras */

        maybelong _dimensions[MAXDIM];  /* values of shape elements */
        maybelong _strides[MAXDIM];     /* values of stride elements */
        PyObject *_data;       /* object must meet buffer API */
        PyObject *_shadows;    /* ill-behaved original array. */
        int      nstrides;     /* elements in strides array */
        long     byteoffset;   /* offset into buffer where array data begins */
        long     bytestride;   /* basic seperation of elements in bytes */
        long     itemsize;     /* length of 1 element in bytes */

        char      byteorder;   /* NUM_BIG_ENDIAN, NUM_LITTLE_ENDIAN */

        char      _unused0; 
        char      _unused1; 
        /* Don't expect the following vars to stay around.  Never use them.
        They're an implementation detail of the get/set macros. */

        Complex64      temp;   /* temporary for get/set macros */
        char *         wptr;   /* working pointer for get/set macros */
} PyArrayObject;

14.2.5 Flag Bits

The following are the definitions for the bit values in the flags field of each numarray. Low order bits are Numeric compatible, higher order bits were added by numarray.

/* Array flags */
#define CONTIGUOUS        1       /* compatible, depends */
#define OWN_DIMENSIONS    2       /* always false */
#define OWN_STRIDES       4       /* always false */
#define OWN_DATA          8       /* always false */
#define SAVESPACE      0x10       /* not used */

#define ALIGNED       0x100       /* roughly: data % itemsize == 0 */
#define NOTSWAPPED    0x200       /* byteorder == sys.byteorder    */
#define WRITABLE      0x400       /* data buffer is writable       */


Send comments to the NumArray community.