User guide
NumPy User Guide, Release 1.9.0
NPY_DOUBLE, NPY_LONGDOUBLE, NPY_CFLOAT, NPY_CDOUBLE,
NPY_CLONGDOUBLE, NPY_OBJECT.
Alternatively, the bit-width names can be used as supported on the platform. For example:
NPY_INT8, NPY_INT16, NPY_INT32, NPY_INT64, NPY_UINT8, NPY_UINT16,
NPY_UINT32, NPY_UINT64, NPY_FLOAT32, NPY_FLOAT64, NPY_COMPLEX64,
NPY_COMPLEX128.
The object will be converted to the desired type only if it can be done without losing precision.
Otherwise NULL will be returned and an error raised. Use NPY_FORCECAST in the requirements
flag to override this behavior.
requirements
The memory model for an ndarray admits arbitrary strides in each dimension to advance to the next
element of the array. Often, however, you need to interface with code that expects a C-contiguous
or a Fortran-contiguous memory layout. In addition, an ndarray can be misaligned (the address of
an element is not at an integral multiple of the size of the element) which can cause your program
to crash (or at least work more slowly) if you try and dereference a pointer into the array data. Both
of these problems can be solved by converting the Python object into an array that is more “well-
behaved” for your specific usage.
The requirements flag allows specification of what kind of array is acceptable. If the object passed
in does not satisfy this requirements then a copy is made so that thre returned object will satisfy the
requirements. these ndarray can use a very generic pointer to memory. This flag allows specification
of the desired properties of the returned array object. All of the flags are explained in the detailed
API chapter. The flags most commonly needed are NPY_ARRAY_IN_ARRAY, NPY_OUT_ARRAY,
and NPY_ARRAY_INOUT_ARRAY:
NPY_ARRAY_IN_ARRAY
Equivalent to NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED. This combination of
flags is useful for arrays that must be in C-contiguous order and aligned. These kinds of arrays
are usually input arrays for some algorithm.
NPY_ARRAY_OUT_ARRAY
Equivalent to NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED |
NPY_ARRAY_WRITEABLE. This combination of flags is useful to specify an array that
is in C-contiguous order, is aligned, and can be written to as well. Such an array is usually
returned as output (although normally such output arrays are created from scratch).
NPY_ARRAY_INOUT_ARRAY
Equivalent to NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_ALIGNED |
NPY_ARRAY_WRITEABLE | NPY_ARRAY_UPDATEIFCOPY. This combination of flags
is useful to specify an array that will be used for both input and output. If a copy is needed,
then when the temporary is deleted (by your use of Py_DECREF at the end of the interface
routine), the temporary array will be copied back into the original array passed in. Use of
the NPY_ARRAY_UPDATEIFCOPY flag requires that the input object is already an array
(because other objects cannot be automatically updated in this fashion). If an error occurs use
PyArray_DECREF_ERR (obj) on an array with the NPY_ARRAY_UPDATEIFCOPY flag set.
This will delete the array without causing the contents to be copied back into the original array.
Other useful flags that can be OR’d as additional requirements are:
NPY_ARRAY_FORCECAST
Cast to the desired type, even if it can’t be done without losing information.
NPY_ARRAY_ENSURECOPY
Make sure the resulting array is a copy of the original.
58 Chapter 5. Using Numpy C-API