These macros access the PyArrayObject structure members and are defined in ndarraytypes.h. The input argument, arr, can be any PyObject * that is directly interpretable as a PyArrayObject * (any instance of the PyArray_Type and its sub-types).
Sets the memory location item of dtype descr to value.
The function is equivalent to setting a single array element with a Python assignment. Returns 0 on success and -1 with an error set on failure.
Convert obj and place it in the ndarray, arr, at the place pointed to by itemptr. Return -1 if an error occurs or 0 on success.
Data access
These functions and macros provide easy access to elements of the ndarray from C. These work for all arrays. You may need to take care when accessing the data in the array, however, if it is not in machine byte-order, misaligned, or not writeable. In other words, be sure to respect the state of the flags unless you know what you are doing, or have previously guaranteed an array that is writeable, aligned, and in machine byte-order using PyArray_FromAny. If you wish to handle all types of arrays, the copyswap function for each type is useful for handling misbehaved arrays. Some platforms (e.g. Solaris) do not like misaligned data and will crash if you de-reference a misaligned pointer. Other platforms (e.g. x86 Linux) will just work more slowly with misaligned data.
Quick, inline access to the element at the given coordinates in the ndarray, obj, which must have respectively 1, 2, 3, or 4 dimensions (this is not checked). The corresponding i, j, k, and l coordinates can be any integer but will be interpreted as npy_intp. You may want to typecast the returned pointer to the data type of the ndarray.
Creating arrays
From scratch
This function steals a reference to descr. The easiest way to get one is using PyArray_DescrFromType.
This is the main array creation function. Most new arrays are created with this flexible function.
The returned object is an object of Python-type subtype, which must be a subtype of PyArray_Type. The array has nd dimensions, described by dims. The data-type descriptor of the new array is descr.
If subtype is of an array subclass instead of the base &PyArray_Type<PyArray_Type>, then obj is the object to pass to the ~numpy.class.__array_finalize__ method of the subclass.
If data is NULL, then new unitinialized memory will be allocated and flags can be non-zero to indicate a Fortran-style contiguous array. Use PyArray_FILLWBYTE to initialize the memory.
If data is not NULL, then it is assumed to point to the memory to be used for the array and the flags argument is used as the new flags for the array (except the state of NPY_ARRAY_OWNDATA, NPY_ARRAY_WRITEBACKIFCOPY flag of the new array will be reset).
In addition, if data is non-NULL, then strides can also be provided. If strides is NULL, then the array strides are computed as C-style contiguous (default) or Fortran-style contiguous (flags is nonzero for data = NULL or flags & NPY_ARRAY_F_CONTIGUOUS is nonzero non-NULL data). Any provided dims and strides are copied into newly allocated dimension and strides arrays for the new array object.
PyArray_CheckStrides can help verify non- NULL stride information.
If data is provided, it must stay alive for the life of the array. One way to manage this is through PyArray_SetBaseObject
This function steals a reference to descr if it is not NULL. This array creation routine allows for the convenient creation of a new array matching an existing array's shapes and memory layout, possibly changing the layout and/or data type.
When order is NPY_ANYORDER, the result order is NPY_FORTRANORDER if prototype is a fortran array, NPY_CORDER otherwise. When order is NPY_KEEPORDER, the result order matches that of prototype, even when the axes of prototype aren't in C or Fortran order.
If descr is NULL, the data type of prototype is used.
If subok is 1, the newly created array will use the sub-type of prototype to create the new array, otherwise it will create a base-class array.
This is similar to PyArray_NewFromDescr (...) except you specify the data-type descriptor with type_num and itemsize, where type_num corresponds to a builtin (or user-defined) type. If the type always has the same number of bytes, then itemsize is ignored. Otherwise, itemsize specifies the particular size of this array.
Create an array wrapper around data pointed to by the given pointer. The array flags will have a default that the data area is well-behaved and C-style contiguous. The shape of the array is given by the dims c-array of length nd. The data-type of the array is indicated by typenum. If data comes from another reference-counted Python object, the reference count on this object should be increased after the pointer is passed in, and the base member of the returned ndarray should point to the Python object that owns the data. This will ensure that the provided memory is not freed while the returned array is in existence.
This function steals a reference to descr.
Create a new array with the provided data-type descriptor, descr, of the shape determined by nd and dims.
Construct a new nd -dimensional array with shape given by dims and data type given by dtype. If fortran is non-zero, then a Fortran-order array is created, otherwise a C-order array is created. Fill the memory with zeros (or the 0 object if dtype corresponds to NPY_OBJECT ).
Macro form of PyArray_Zeros which takes a type-number instead of a data-type object.
Construct a new nd -dimensional array with shape given by dims and data type given by dtype. If fortran is non-zero, then a Fortran-order array is created, otherwise a C-order array is created. The array is uninitialized unless the data type corresponds to NPY_OBJECT in which case the array is filled with Py_None.
Macro form of PyArray_Empty which takes a type-number, typenum, instead of a data-type object.
Construct a new 1-dimensional array of data-type, typenum, that ranges from start to stop (exclusive) in increments of step . Equivalent to arange (start, stop, step, dtype).
Construct a new 1-dimensional array of data-type determined by descr, that ranges from start to stop (exclusive) in increments of step. Equivalent to arange( start, stop, step, typenum ).
From other objects
This is the main function used to obtain an array from any nested sequence, or object that exposes the array interface, op. The parameters allow specification of the required dtype, the minimum (min_depth) and maximum (max_depth) number of dimensions acceptable, and other requirements for the array. This function steals a reference to the dtype argument, which needs to be a PyArray_Descr structure indicating the desired data-type (including required byteorder). The dtype argument may be NULL, indicating that any data-type (and byteorder) is acceptable. Unless NPY_ARRAY_FORCECAST is present in flags, this call will generate an error if the data type cannot be safely obtained from the object. If you want to use NULL for the dtype and ensure the array is not swapped then use PyArray_CheckFromAny. A value of 0 for either of the depth parameters causes the parameter to be ignored. Any of the following array flags can be added (e.g. using |) to get the requirements argument. If your code can handle general (e.g. strided, byte-swapped, or unaligned arrays) then requirements may be 0. Also, if op is not already an array (or does not expose the array interface), then a new array will be created (and filled from op using the sequence protocol). The new array will have NPY_ARRAY_DEFAULT as its flags member. The context argument is unused.
NPY_ARRAY_C_CONTIGUOUS
Make sure the returned array is C-style contiguous
NPY_ARRAY_F_CONTIGUOUS
Make sure the returned array is Fortran-style contiguous.
NPY_ARRAY_ALIGNED
Make sure the returned array is aligned on proper boundaries for its data type. An aligned array has the data pointer and every strides factor as a multiple of the alignment factor for the data-type- descriptor.
NPY_ARRAY_WRITEABLE
Make sure the returned array can be written to.
NPY_ARRAY_ENSURECOPY
Make sure a copy is made of op. If this flag is not present, data is not copied if it can be avoided.
NPY_ARRAY_ENSUREARRAY
Make sure the result is a base-class ndarray. By default, if op is an instance of a subclass of ndarray, an instance of that same subclass is returned. If this flag is set, an ndarray object will be returned instead.
NPY_ARRAY_FORCECAST
Force a cast to the output type even if it cannot be done safely. Without this flag, a data cast will occur only if it can be done safely, otherwise an error is raised.
NPY_ARRAY_WRITEBACKIFCOPY
If op is already an array, but does not satisfy the requirements, then a copy is made (which will satisfy the requirements). If this flag is present and a copy (of an object that is already an array) must be made, then the corresponding NPY_ARRAY_WRITEBACKIFCOPY flag is set in the returned copy and op is made to be read-only. You must be sure to call PyArray_ResolveWritebackIfCopy to copy the contents back into op and the op array will be made writeable again. If op is not writeable to begin with, or if it is not already an array, then an error is raised.
Combinations of array flags can also be added.
Nearly identical to PyArray_FromAny (...) except requirements can contain NPY_ARRAY_NOTSWAPPED (over-riding the specification in dtype) and NPY_ARRAY_ELEMENTSTRIDES which indicates that the array should be aligned in the sense that the strides are multiples of the element size.
Special case of PyArray_FromAny for when op is already an array but it needs to be of a specific newtype (including byte-order) or has certain requirements.
Return an ndarray object from a Python object that exposes the ~numpy.class.__array__ method. The third-party implementations of ~numpy.class.__array__ must take dtype and copy keyword arguments. context is unused.
This function returns a (C-style) contiguous and behaved function array from any nested sequence or array interface exporting object, op, of (non-flexible) type given by the enumerated typenum, of minimum depth min_depth, and of maximum depth max_depth. Equivalent to a call to PyArray_FromAny with requirements set to NPY_ARRAY_DEFAULT and the type_num member of the type argument set to typenum.
This function returns a well-behaved C-style contiguous array from any nested sequence or array-interface exporting object. The minimum number of dimensions the array can have is given by min_depth while the maximum is max_depth. This is equivalent to call PyArray_FromAny with requirements NPY_ARRAY_DEFAULT and NPY_ARRAY_ENSUREARRAY.
Return an aligned and in native-byteorder array from any nested sequence or array-interface exporting object, op, of a type given by the enumerated typenum. The minimum number of dimensions the array can have is given by min_depth while the maximum is max_depth. This is equivalent to a call to PyArray_FromAny with requirements set to BEHAVED.
Construct a one-dimensional ndarray of a single type from a binary or (ASCII) text string of length slen. The data-type of the array to-be-created is given by dtype. If num is -1, then copy the entire string and return an appropriately sized array, otherwise, num is the number of items to copy from the string. If sep is NULL (or ""), then interpret the string as bytes of binary data, otherwise convert the sub-strings separated by sep to items of data-type dtype. Some data-types may not be readable in text mode and an error will be raised if that occurs. All errors return NULL.
Construct a one-dimensional ndarray of a single type from a binary or text file. The open file pointer is fp, the data-type of the array to be created is given by dtype. This must match the data in the file. If num is -1, then read until the end of the file and return an appropriately sized array, otherwise, num is the number of items to read. If sep is NULL (or ""), then read from the file in binary mode, otherwise read from the file in text mode with sep providing the item separator. Some array types cannot be read in text mode in which case an error is raised.
Construct a one-dimensional ndarray of a single type from an object, buf, that exports the (single-segment) buffer protocol (or has an attribute __buffer\__ that returns an object that exports the buffer protocol). A writeable buffer will be tried first followed by a read- only buffer. The NPY_ARRAY_WRITEABLE flag of the returned array will reflect which one was successful. The data is assumed to start at offset bytes from the start of the memory location for the object. The type of the data in the buffer will be interpreted depending on the data- type descriptor, dtype. If count is negative then it will be determined from the size of the buffer and the requested itemsize, otherwise, count represents how many elements should be converted from the buffer.
Combination of PyArray_FROM_OF and PyArray_FROM_OT allowing both a typenum and a flags argument to be provided.
Similar to PyArray_FromAny except the data-type is specified using a typenumber. PyArray_DescrFromType (typenum) is passed directly to PyArray_FromAny. This macro also adds NPY_ARRAY_DEFAULT to requirements if NPY_ARRAY_ENSURECOPY is passed in as requirements.
Encapsulate the functionality of functions and methods that take the axis= keyword and work properly with None as the axis argument. The input array is obj, while *axis is a converted integer (so that *axis == NPY_RAVEL_AXIS is the None value), and requirements gives the needed properties of obj. The output is a converted version of the input so that requirements are met and if needed a flattening has occurred. On output negative values of *axis are converted and the new value is checked to ensure consistency with the shape of obj.
Dealing with types
General check of Python Type
If op implements any part of the array interface, then out will contain a new reference to the newly created ndarray using the interface or out will contain NULL if an error during conversion occurs. Otherwise, out will contain a borrowed reference to Py_NotImplemented and no error condition is set. This version allows setting of the dtype in the part of the array interface that looks for the ~numpy.class.__array__ attribute. context is unused.
Data-type accessors
Some of the descriptor attributes may not always be defined and should or cannot not be accessed directly.
Data-type checking
For the typenum macros, the argument is an integer representing an enumerated array data type. For the array type checking macros the argument must be a PyObject * that can be directly interpreted as a PyArrayObject *.
Return NPY_TRUE if type1 and type2 actually represent equivalent types for this platform (the fortran member of each type is ignored). For example, on 32-bit platforms, NPY_LONG and NPY_INT are equivalent. Otherwise return NPY_FALSE.
Return NPY_TRUE if a1 and a2 are arrays with equivalent types for this platform.
Converting data types
Return a new array of the type specified, casting the elements of arr as appropriate. The fortran argument specifies the ordering of the output array.
PyArray_CanCastTypeTo supersedes this function in NumPy 1.6 and later.
Equivalent to PyArray_CanCastTypeTo(fromtype, totype, NPY_SAFE_CASTING).
Returns non-zero if an array of data type fromtype (which can include flexible types) can be cast safely to an array of data type totype (which can include flexible types) according to the casting rule casting. For simple types with NPY_SAFE_CASTING, this is basically a wrapper around PyArray_CanCastSafely, but for flexible types such as strings or unicode, it produces results taking into account their sizes. Integer and float types can only be cast to a string or unicode type using NPY_SAFE_CASTING if the string or unicode type is big enough to hold the max value of the integer/float type being cast from.
Returns non-zero if arr can be cast to totype according to the casting rule given in casting. If arr is an array scalar, its value is taken into account, and non-zero is also returned when the value will not overflow or be truncated to an integer when converting to a smaller type.
Finds the data type of smallest size and kind to which type1 and type2 may be safely converted. This function is symmetric and associative. A string or unicode result will be the proper size for storing the max value of the input types converted to a string or unicode.
This applies type promotion to all the input arrays and dtype objects, using the NumPy rules for combining scalars and arrays, to determine the output type for an operation with the given set of operands. This is the same result type that ufuncs produce.
See the documentation of numpy.result_type for more detail about the type promotion algorithm.
The functionality this provides is largely superseded by iterator NpyIter introduced in 1.6, with flag NPY_ITER_COMMON_DTYPE or with the same dtype parameter for all operands.
Convert a sequence of Python objects contained in op to an array of ndarrays each having the same data type. The type is selected in the same way as PyArray_ResultType. The length of the sequence is returned in n, and an n -length array of PyArrayObject pointers is the return value (or NULL if an error occurs). The returned array must be freed by the caller of this routine (using PyDataMem_FREE ) and all the array objects in it DECREF 'd or a memory-leak will occur. The example template-code below shows a typical usage:
mps = PyArray_ConvertToCommonType(obj, &n); if (mps==NULL) return NULL; {code} <before return> for (i=0; i<n; i++) Py_DECREF(mps[i]); PyDataMem_FREE(mps); {return}
User-defined data types
Register a low-level casting function, castfunc, to convert from the data-type, descr, to the given data-type number, totype. Any old casting function is over-written. A 0 is returned on success or a -1 on failure.
Register the data-type number, totype, as castable from data-type object, descr, of the given scalar kind. Use scalar = NPY_NOSCALAR to register that an array of data-type descr can be cast safely to a data-type whose type_number is totype. The return value is 0 on success or -1 on failure.
Special functions for NPY_OBJECT
Array flags
The flags attribute of the PyArrayObject structure contains important information about the memory used by the array (pointed to by the data member) This flag information must be kept accurate or strange results and even segfaults may result.
There are 6 (binary) flags that describe the memory area used by the data buffer. These constants are defined in arrayobject.h and determine the bit-position of the flag. Python exposes a nice attribute- based interface as well as a dictionary-like interface for getting (and, if appropriate, setting) these flags.
Memory areas of all kinds can be pointed to by an ndarray, necessitating these flags. If you get an arbitrary PyArrayObject in C-code, you need to be aware of the flags that are set. If you need to guarantee a certain kind of array (like NPY_ARRAY_C_CONTIGUOUS and NPY_ARRAY_BEHAVED), then pass these requirements into the PyArray_FromAny function.
In versions 1.6 and earlier of NumPy, the following flags did not have the _ARRAY_ macro namespace in them. That form of the constant names is deprecated in 1.7.
Basic Array Flags
An ndarray can have a data segment that is not a simple contiguous chunk of well-behaved memory you can manipulate. It may not be aligned with word boundaries (very important on some platforms). It might have its data in a different byte-order than the machine recognizes. It might not be writeable. It might be in Fortran-contiguous order. The array flags are used to indicate what can be said about data associated with an array.
PyArray_UpdateFlags (obj, flags) will update the obj->flags for flags which can be any of NPY_ARRAY_C_CONTIGUOUS, NPY_ARRAY_F_CONTIGUOUS, NPY_ARRAY_ALIGNED, or NPY_ARRAY_WRITEABLE.
Combinations of array flags
Flag-like constants
These constants are used in PyArray_FromAny (and its macro forms) to specify desired properties of the new array.
These constants are used in PyArray_CheckFromAny (and its macro forms) to specify desired properties of the new array.
Flag checking
For all of these macros arr must be an instance of a (subclass of) PyArray_Type.
ArrayMethod API
ArrayMethod loops are intended as a generic mechanism for writing loops over arrays, including ufunc loops and casts. The public API is defined in the numpy/dtype_api.h header. See arraymethod-structs for documentation on the C structs exposed in the ArrayMethod API.
Slots and Typedefs
These are used to identify which kind of function an ArrayMethod slot implements. See arraymethod-typedefs below for documentation on the functions that must be implemented for each slot.
The function used to set the descriptors for an operation based on the descriptors of the operands. For example, a ufunc operation with two input operands and one output operand that is called without out being set in the python API, resolve_descriptors will be passed the descriptors for the two operands and determine the correct descriptor to use for the output based on the output DType set for the ArrayMethod. If out is set, then the output descriptor would be passed in as well and should not be overridden.
The method is a pointer to the underlying cast or ufunc loop. In the future we may expose this struct publicly but for now this is an opaque pointer and the method cannot be inspected. The dtypes is an nargs length array of PyArray_DTypeMeta pointers, given_descrs is an nargs length array of input descriptor instances (output descriptors may be NULL if no output was provided by the user), and loop_descrs is an nargs length array of descriptors that must be filled in by the resolve descriptors implementation. view_offset is currently only interesting for casts and can normally be ignored. When a cast does not require any operation, this can be signalled by setting view_offset to 0. On error, you must return (NPY_CASTING)-1 with an error set.
An implementation of an ArrayMethod loop. All of the loop slot IDs listed above must provide a PyArrayMethod_StridedLoop implementation. The context is a struct containing context for the loop operation - in particular the input descriptors. The data are an array of pointers to the beginning of the input and output array buffers. The dimensions are the loop dimensions for the operation. The strides are an nargs length array of strides for each input. The auxdata is an optional set of auxiliary data that can be passed in to the loop - helpful to turn on and off optional behavior or reduce boilerplate by allowing similar ufuncs to share loop implementations or to allocate space that is persistent over multiple strided loop calls.
Sets the loop to use for an operation at runtime. The context is the runtime context for the operation. aligned indicates whether the data access for the loop is aligned (1) or unaligned (0). move_references indicates whether embedded references in the data should be copied. strides are the strides for the input array, out_loop is a pointer that must be filled in with a pointer to the loop implementation. out_transferdata can be optionally filled in to allow passing in extra user-defined context to an operation. flags must be filled in with ArrayMethod flags relevant for the operation. This is for example necessary to indicate if the inner loop requires the Python GIL to be held.
Query an ArrayMethod for the initial value for use in reduction. The context is the ArrayMethod context, mainly to access the input descriptors. reduction_is_empty indicates whether the reduction is empty. When it is, the value returned may differ. In this case it is a "default" value that may differ from the "identity" value normally used. For example:
0.0 is the default for sum([]). But -0.0 is the correct identity otherwise as it preserves the sign for sum([-0.0]).
We use no identity for object, but return the default of 0 and 1 for the empty sum([], dtype=object) and prod([], dtype=object). This allows np.sum(np.array(["a", "b"], dtype=object)) to work.
-inf or INT_MIN for max is an identity, but at least INT_MIN not a good default when there are no items.
initial is a pointer to the data for the initial value, which should be filled in. Returns -1, 0, or 1 indicating error, no initial value, and the initial value being successfully filled. Errors must not be given when no initial value is correct, since NumPy may call this even when it is not strictly necessary to do so.
Flags
Typedefs
Typedefs for functions that users of the ArrayMethod API can implement are described below.
A traverse loop working on a single array. This is similar to the general strided-loop function. This is designed for loops that need to visit every element of a single array.
Currently this is used for array clearing, via the NPY_DT_get_clear_loop DType API hook, and zero-filling, via the NPY_DT_get_fill_zero_loop DType API hook. These are most useful for handling arrays storing embedded references to python objects or heap-allocated data.
The descr is the descriptor for the array, data is a pointer to the array buffer, size is the 1D size of the array buffer, stride is the stride, and auxdata is optional extra data for the loop.
The traverse_context is passed in because we may need to pass in Interpreter state or similar in the future, but we don't want to pass in a full context (with pointers to dtypes, method, caller which all make no sense for a traverse function). We assume for now that this context can be just passed through in the future (for structured dtypes).
Simplified get_loop function specific to dtype traversal
It should set the flags needed for the traversal loop and set out_loop to the loop function, which must be a valid PyArrayMethod_TraverseLoop pointer. Currently this is used for zero-filling and clearing arrays storing embedded references.
API Functions and Typedefs
These functions are part of the main numpy array API and were added along with the rest of the ArrayMethod API.
Add loop directly to a ufunc from a given ArrayMethod spec. the main ufunc registration function. This adds a new implementation/loop to a ufunc. It replaces PyUFunc_RegisterLoopForType.
Add multiple loops to ufuncs from ArrayMethod specs. This also handles the registration of methods for the ufunc-like functions sort and argsort. See array-methods-sorting for details.
The slots argument must be a NULL-terminated array of PyUFunc_LoopSlot (see above), which give the name of the ufunc and spec needed to create the loop.
Note that currently the output dtypes are always NULL unless they are also part of the signature. This is an implementation detail and could change in the future. However, in general promoters should not have a need for output dtypes. Register a new promoter for a ufunc. The first argument is the ufunc to register the promoter with. The second argument is a Python tuple containing DTypes or None matching the number of inputs and outputs for the ufuncs. The last argument is a promoter is a function stored in a PyCapsule. It is passed the operation and requested DType signatures and can mutate it to attempt a new search for a matching loop/promoter.
Type of the promoter function, which must be wrapped into a PyCapsule with name "numpy._ufunc_promoter". It is passed the operation and requested DType signatures and can mutate the signatures to attempt a search for a new loop or promoter that can accomplish the operation by casting the inputs to the "promoted" DTypes.
Checks for a floating point error after performing a floating point operation in a manner that takes into account the error signaling configured via numpy.errstate. Takes the name of the operation to use in the error message and an integer flag that is one of NPY_FPE_DIVIDEBYZERO, NPY_FPE_OVERFLOW, NPY_FPE_UNDERFLOW, NPY_FPE_INVALID to indicate which error to check for.
Returns -1 on failure (an error was raised) and 0 on success.
Allows creating of a fairly lightweight wrapper around an existing ufunc loop. The idea is mainly for units, as this is currently slightly limited in that it enforces that you cannot use a loop from another ufunc.
The function to convert the given descriptors (passed in to resolve_descriptors) and translates them for the wrapped loop. The new descriptors MUST be viewable with the old ones, NULL must be supported (for output arguments) and should normally be forwarded.
The output of of this function will be used to construct views of the arguments as if they were the translated dtypes and does not use a cast. This means this mechanism is mostly useful for DTypes that "wrap" another DType implementation. For example, a unit DType could use this to wrap an existing floating point DType without needing to re-implement low-level ufunc logic. In the unit example, resolve_descriptors would handle computing the output unit from the input unit.
The function to convert the actual loop descriptors (as returned by the original resolve_descriptors function) to the ones the output array should use. This function must return "viewable" types, it must not mutate them in any form that would break the inner-loop logic. Does not need to support NULL.
Wrapping Loop Example
Suppose you want to wrap the float64 multiply implementation for a WrappedDoubleDType. You would add a wrapping loop like so:
Note that this also requires two functions to be defined above this code:
static inttranslate_given_descrs(int nin, int nout, PyArray_DTypeMeta *NPY_UNUSED(wrapped_dtypes[]), PyArray_Descr *given_descrs[], PyArray_Descr *new_descrs[]){ for (int i = 0; i < nin + nout; i++) { if (given_descrs[i] == NULL) { new_descrs[i] = NULL; } else { new_descrs[i] = PyArray_DescrFromType(NPY_DOUBLE); } } return 0;}static inttranslate_loop_descrs(int nin, int NPY_UNUSED(nout), PyArray_DTypeMeta *NPY_UNUSED(new_dtypes[]), PyArray_Descr *given_descrs[], PyArray_Descr *original_descrs[], PyArray_Descr *loop_descrs[]){ // more complicated parametric DTypes may need to // to do additional checking, but we know the wrapped // DTypes *have* to be float64 for this example. loop_descrs[0] = PyArray_DescrFromType(NPY_FLOAT64); Py_INCREF(loop_descrs[0]); loop_descrs[1] = PyArray_DescrFromType(NPY_FLOAT64); Py_INCREF(loop_descrs[1]); loop_descrs[2] = PyArray_DescrFromType(NPY_FLOAT64); Py_INCREF(loop_descrs[2]);}
Sorting and Argsorting
Sorting and argsorting methods for dtypes can be registered using the ArrayMethod API. This is done by adding an ArrayMethod spec with the name "sort" or "argsort" respectively. The spec must have nin=1 and nout=1 for both sort and argsort. Sorting is inplace, hence we enforce that data[0] == data[1]. Argsorting returns a new array of indices, so the output must be of NPY_INTP type.
The context passed to the loop contains the parameters field which for these operations is a PyArrayMethod_SortParameters * struct. This struct contains a flags field which is a bitwise OR of NPY_SORTKIND values indicating the kind of sort to perform (that is, whether it is a stable and/or descending sort). If the strided loop depends on the flags, a good way to deal with this is to define NPY_METH_get_loop, and not set any of the other loop slots.
These specs can be registered using PyUFunc_AddLoopsFromSpecs along with other ufunc loops.
API for calling array methods
Conversion
Equivalent to ndarray.getfield<numpy.ndarray.getfield> (self, dtype, offset). This function steals a reference to PyArray_Descr and returns a new array of the given dtype using the data in the current array at a specified offset in bytes. The offset plus the itemsize of the new array type must be less than self->descr->elsize or an error is raised. The same shape and strides as the original array are used. Therefore, this function has the effect of returning a field from a structured array. But, it can also be used to select specific bytes or groups of bytes from any array type.
Equivalent to ndarray.setfield<numpy.ndarray.setfield> (self, val, dtype, offset ). Set the field starting at offset in bytes and of the given dtype to val. The offset plus dtype ->elsize must be less than self ->descr->elsize or an error is raised. Otherwise, the val argument is converted to an array and copied into the field pointed to. If necessary, the elements of val are repeated to fill the destination array, But, the number of elements in the destination must be an integer multiple of the number of elements in val.
Write the contents of self to the file pointer fp in C-style contiguous fashion. Write the data as binary bytes if sep is the string ""or NULL. Otherwise, write the contents of self as text using the sep string as the item separator. Each item will be printed to the file. If the format string is not NULL or "", then it is a Python print statement format string showing how the items are to be written.
Equivalent to ndarray.view<numpy.ndarray.view> (self, dtype). Return a new view of the array self as possibly a different data-type, dtype, and different array subclass ptype.
If dtype is NULL, then the returned array will have the same data type as self. The new data-type must be consistent with the size of self. Either the itemsizes must be identical, or self must be single-segment and the total number of bytes must be the same. In the latter case the dimensions of the returned array will be altered in the last (or first for Fortran-style contiguous arrays) dimension. The data area of the returned array and self is exactly the same.
Shape Manipulation
Result will be a new array (pointing to the same memory location as self if possible), but having a shape given by newshape. If the new shape is not compatible with the strides of self, then a copy of the array with the new specified shape will be returned.
Equivalent to ndarray.resize<numpy.ndarray.resize> (self, newshape, refcheck). This function only works on single-segment arrays. It changes the shape of self inplace and will reallocate the memory for self if newshape has a different total number of elements then the old shape. If reallocation is necessary, then self must own its data, have self - >base==NULL, have self - >weakrefs==NULL, and (unless refcheck is 0) not be referenced by any other array. The fortran argument can be NPY_ANYORDER, NPY_CORDER, or NPY_FORTRANORDER. It currently has no effect. Eventually it could be used to determine how the resize operation should view the data when constructing a differently-dimensioned array. Returns None on success and NULL on error.
Equivalent to ndarray.transpose<numpy.ndarray.transpose> (self, permute). Permute the axes of the ndarray object self according to the data structure permute and return the result. If permute is NULL, then the resulting array has its axes reversed. For example if self has shape 10×20×30, and permute.ptr is (0,2,1) the shape of the result is 10×30×20. If permute is NULL, the shape of the result is 30×20×10.
Item selection and manipulation
Equivalent to ndarray.take<numpy.ndarray.take> (self, indices, axis, ret, clipmode) except axis =None in Python is obtained by setting axis = NPY_MAXDIMS in C. Extract the items from self indicated by the integer-valued indices along the given axis. The clipmode argument can be NPY_RAISE, NPY_WRAP, or NPY_CLIP to indicate what to do with out-of-bound indices. The ret argument can specify an output array rather than having one created internally.
Equivalent to self.put(values, indices, clipmode ). Put values into self at the corresponding (flattened) indices. If values is too small it will be repeated as necessary.
Place the values in self wherever corresponding positions (using a flattened context) in mask are true. The mask and self arrays must have the same total number of elements. If values is too small, it will be repeated as necessary.
Equivalent to ndarray.repeat<numpy.ndarray.repeat> (self, op, axis). Copy the elements of self, op times along the given axis. Either op is a scalar integer or a sequence of length self ->dimensions[ axis ] indicating how many times to repeat each item along the axis.
Equivalent to ndarray.choose<numpy.ndarray.choose> (self, op, ret, clipmode). Create a new array by selecting elements from the sequence of arrays in op based on the integer values in self. The arrays must all be broadcastable to the same shape and the entries in self should be between 0 and len(op). The output is placed in ret unless it is NULL in which case a new output is created. The clipmode argument determines behavior for when entries in self are not between 0 and len(op).
Equivalent to ndarray.searchsorted<numpy.ndarray.searchsorted> (self, values, side, perm). Assuming self is a 1-d array in ascending order, then the output is an array of indices the same shape as values such that, if the elements in values were inserted before the indices, the order of self would be preserved. No checking is done on whether or not self is in ascending order.
The side argument indicates whether the index returned should be that of the first suitable location (if NPY_SEARCHLEFT) or of the last (if NPY_SEARCHRIGHT).
The sorter argument, if not NULL, must be a 1D array of integer indices the same length as self, that sorts it into ascending order. This is typically the result of a call to PyArray_ArgSort (...) Binary search is used to find the required insertion points.
Equivalent to ndarray.partition<numpy.ndarray.partition> (self, ktharray, axis, kind). Partitions the array so that the values of the element indexed by ktharray are in the positions they would be if the array is fully sorted and places all elements smaller than the kth before and all elements equal or greater after the kth element. The ordering of all elements within the partitions is undefined. If self->descr is a data-type with fields defined, then self->descr->names is used to determine the sort order. A comparison where the first field is equal will use the second field and so on. To alter the sort order of a structured array, create a new data-type with a different order of names and construct a view of the array with that new data-type. Returns zero on success and -1 on failure.
Equivalent to ndarray.argpartition<numpy.ndarray.argpartition> (self, ktharray, axis, kind). Return an array of indices such that selection of these indices along the given axis would return a partitioned version of self.
Equivalent to ndarray.diagonal<numpy.ndarray.diagonal> (self, offset, axis1, axis2 ). Return the offset diagonals of the 2-d arrays defined by axis1 and axis2.
Equivalent to ndarray.compress<numpy.ndarray.compress> (self, condition, axis ). Return the elements along axis corresponding to elements of condition that are true.
Calculation
Equivalent to ndarray.argmax<numpy.ndarray.argmax> (self, axis). Return the index of the largest element of self along axis.
Equivalent to ndarray.argmin<numpy.ndarray.argmin> (self, axis). Return the index of the smallest element of self along axis.
Equivalent to ndarray.max<numpy.ndarray.max> (self, axis). Returns the largest element of self along the given axis. When the result is a single element, returns a numpy scalar instead of an ndarray.
Equivalent to ndarray.min<numpy.ndarray.min> (self, axis). Return the smallest element of self along the given axis. When the result is a single element, returns a numpy scalar instead of an ndarray.
Return the difference between the largest element of self along axis and the smallest element of self along axis. When the result is a single element, returns a numpy scalar instead of an ndarray.
Equivalent to ndarray.mean<numpy.ndarray.mean> (self, axis, rtype). Returns the mean of the elements along the given axis, using the enumerated type rtype as the data type to sum in. Default sum behavior is obtained using NPY_NOTYPE for rtype.
Equivalent to ndarray.trace<numpy.ndarray.trace> (self, offset, axis1, axis2, rtype). Return the sum (using rtype as the data type of summation) over the offset diagonal elements of the 2-d arrays defined by axis1 and axis2 variables. A positive offset chooses diagonals above the main diagonal. A negative offset selects diagonals below the main diagonal.
Equivalent to ndarray.clip<numpy.ndarray.clip> (self, min, max). Clip an array, self, so that values larger than max are fixed to max and values less than min are fixed to min.
Equivalent to ndarray.round<numpy.ndarray.round> (self, decimals, out). Returns the array with elements rounded to the nearest decimal place. The decimal place is defined as the 10−decimals digit so that negative decimals cause rounding to the nearest 10's, 100's, etc. If out is NULL, then the output array is created, otherwise the output is placed in out which must be the correct size and type.
Equivalent to ndarray.std<numpy.ndarray.std> (self, axis, rtype). Return the standard deviation using data along axis converted to data type rtype.
Equivalent to ndarray.sum<numpy.ndarray.sum> (self, axis, rtype). Return 1-d vector sums of elements in self along axis. Perform the sum after converting data to data type rtype.
Equivalent to ndarray.cumsum<numpy.ndarray.cumsum> (self, axis, rtype). Return cumulative 1-d sums of elements in self along axis. Perform the sum after converting data to data type rtype.
Equivalent to ndarray.prod<numpy.ndarray.prod> (self, axis, rtype). Return 1-d products of elements in self along axis. Perform the product after converting data to data type rtype.
Equivalent to ndarray.cumprod<numpy.ndarray.cumprod> (self, axis, rtype). Return 1-d cumulative products of elements in self along axis. Perform the product after converting data to data type rtype.
Equivalent to ndarray.all<numpy.ndarray.all> (self, axis). Return an array with True elements for every 1-d sub-array of self defined by axis in which all the elements are True.
Equivalent to ndarray.any<numpy.ndarray.any> (self, axis). Return an array with True elements for every 1-d sub-array of self defined by axis in which any of the elements are True.
Functions
Array Functions
Sometimes it is useful to access a multidimensional array as a C-style multi-dimensional array so that algorithms can be implemented using C's a[i][j][k] syntax. This routine returns a pointer, ptr, that simulates this kind of C-style array, for 1-, 2-, and 3-d ndarrays.
paramop
The address to any Python object. This Python object will be replaced with an equivalent well-behaved, C-style contiguous, ndarray of the given data type specified by the last two arguments. Be sure that stealing a reference in this way to the input object is justified.
paramptr
The address to a (ctype* for 1-d, ctype** for 2-d or ctype*** for 3-d) variable where ctype is the equivalent C-type for the data type. On return, ptr will be addressable as a 1-d, 2-d, or 3-d array.
paramdims
An output array that contains the shape of the array object. This array gives boundaries on any looping that will take place.
paramnd
The dimensionality of the array (1, 2, or 3).
paramtypedescr
A PyArray_Descr structure indicating the desired data-type (including required byteorder). The call will steal a reference to the parameter.
Same as PyArray_MatrixProduct, but store the result in out. The output array must have the correct shape, type, and be C-contiguous, or an exception is raised.
Applies the Einstein summation convention to the array operands provided, returning a new array or placing the result in out. The string in subscripts is a comma separated list of index letters. The number of operands is in nop, and op_in is an array containing those operands. The data type of the output can be forced with dtype, the output order can be forced with order (NPY_KEEPORDER is recommended), and when dtype is specified, casting indicates how permissive the data conversion should be.
Compute the 1-d correlation of the 1-d arrays op1 and op2 . The correlation is computed at each output point by multiplying op1 by a shifted version of op2 and summing the result. As a result of the shift, needed values outside of the defined range of op1 and op2 are interpreted as zero. The mode determines how many shifts to return: 0 - return only shifts that did not need to assume zero- values; 1 - return an object that is the same size as op1, 2 - return all possible shifts (any overlap at all is accepted).
This does not compute the usual correlation: if op2 is larger than op1, the arguments are swapped, and the conjugate is never taken for complex arrays. See PyArray_Correlate2 for the usual signal processing correlation.
Updated version of PyArray_Correlate, which uses the usual definition of correlation for 1d arrays. The correlation is computed at each output point by multiplying op1 by a shifted version of op2 and summing the result. As a result of the shift, needed values outside of the defined range of op1 and op2 are interpreted as zero. The mode determines how many shifts to return: 0 - return only shifts that did not need to assume zero- values; 1 - return an object that is the same size as op1, 2 - return all possible shifts (any overlap at all is accepted).
Compute z as follows
z[k] = sum_n op1[n] * conj(op2[n+k])
If both x and y are NULL, then return PyArray_Nonzero (condition). Otherwise, both x and y must be given and the object returned is shaped like condition and has elements of x and y where condition is respectively True or False.
Other functions
Determine if newstrides is a strides array consistent with the memory of an nd -dimensional array with shape dims and element-size, elsize. The newstrides array is checked to see if jumping by the provided number of bytes in each direction will ever mean jumping more than numbytes which is the assumed size of the available memory segment. If numbytes is 0, then an equivalent numbytes is computed assuming nd, dims, and elsize refer to a single-segment array. Return NPY_TRUE if newstrides is acceptable, otherwise return NPY_FALSE.
Auxiliary data with object semantics
When working with more complex dtypes which are composed of other dtypes, such as the struct dtype, creating inner loops that manipulate the dtypes requires carrying along additional data. NumPy supports this idea through a struct NpyAuxData, mandating a few conventions so that it is possible to do this.
Defining an NpyAuxData is similar to defining a class in C++, but the object semantics have to be tracked manually since the API is in C. Here's an example for a function which doubles up an element using an element copier function as a primitive.
typedef struct { NpyAuxData base; ElementCopier_Func *func; NpyAuxData *funcdata;} eldoubler_aux_data;void free_element_doubler_aux_data(NpyAuxData *data){ eldoubler_aux_data *d = (eldoubler_aux_data *)data; /* Free the memory owned by this auxdata */ NPY_AUXDATA_FREE(d->funcdata); PyArray_free(d);}NpyAuxData *clone_element_doubler_aux_data(NpyAuxData *data){ eldoubler_aux_data *ret = PyArray_malloc(sizeof(eldoubler_aux_data)); if (ret == NULL) { return NULL; } /* Raw copy of all data */ memcpy(ret, data, sizeof(eldoubler_aux_data)); /* Fix up the owned auxdata so we have our own copy */ ret->funcdata = NPY_AUXDATA_CLONE(ret->funcdata); if (ret->funcdata == NULL) { PyArray_free(ret); return NULL; } return (NpyAuxData *)ret;}NpyAuxData *create_element_doubler_aux_data( ElementCopier_Func *func, NpyAuxData *funcdata){ eldoubler_aux_data *ret = PyArray_malloc(sizeof(eldoubler_aux_data)); if (ret == NULL) { PyErr_NoMemory(); return NULL; } memset(&ret, 0, sizeof(eldoubler_aux_data)); ret->base->free = &free_element_doubler_aux_data; ret->base->clone = &clone_element_doubler_aux_data; ret->func = func; ret->funcdata = funcdata; return (NpyAuxData *)ret;}
Array iterators
As of NumPy 1.6.0, these array iterators are superseded by the new array iterator, NpyIter.
An array iterator is a simple way to access the elements of an N-dimensional array quickly and efficiently, as seen in the example which provides more description of this useful approach to looping over an array from C.
Return an array iterator that is broadcast to iterate as an array of the shape provided by dimensions and nd.
Set the iterator index, dataptr, and coordinates members to the location in the array indicated by the N-dimensional c-array, destination, which must have size at least iterator ->nd_m1+1.
Broadcasting (multi-iterators)
Advance each iterator in a multi-iterator object, multi, to the given N -dimensional destination where N is the number of dimensions in the broadcasted array.
Neighborhood iterator
Neighborhood iterators are subclasses of the iterator object, and can be used to iter over a neighborhood of a point. For example, you may want to iterate over every voxel of a 3d image, and for every such voxel, iterate over an hypercube. Neighborhood iterator automatically handle boundaries, thus making this kind of code much easier to write than manual boundaries handling, at the cost of a slight overhead.
This function creates a new neighborhood iterator from an existing iterator. The neighborhood will be computed relatively to the position currently pointed by iter, the bounds define the shape of the neighborhood iterator, and the mode argument the boundaries handling mode.
The bounds argument is expected to be a (2 * iter->ao->nd) arrays, such as the range bound[2*i]->bounds[2*i+1] defines the range where to walk for dimension i (both bounds are included in the walked coordinates). The bounds should be ordered for each dimension (bounds[2*i] <= bounds[2*i+1]).
The mode should be one of:
If the mode is constant filling (NPY_NEIGHBORHOOD_ITER_CONSTANT_PADDING), fill_value should point to an array object which holds the filling value (the first item will be the filling value if the array contains more than one item). For other cases, fill_value may be NULL.
The iterator holds a reference to iter
Return NULL on failure (in which case the reference count of iter is not changed)
iter itself can be a Neighborhood iterator: this can be useful for .e.g automatic boundaries handling
the object returned by this function should be safe to use as a normal iterator
If the position of iter is changed, any subsequent call to PyArrayNeighborhoodIter_Next is undefined behavior, and PyArrayNeighborhoodIter_Reset must be called.
If the position of iter is not the beginning of the data and the underlying data for iter is contiguous, the iterator will point to the start of the data instead of position pointed by iter. To avoid this situation, iter should be moved to the required position only after the creation of iterator, and PyArrayNeighborhoodIter_Reset must be called.
PyArrayIterObject *iter; PyArrayNeighborhoodIterObject *neigh_iter; iter = PyArray_IterNew(x); /*For a 3x3 kernel */ bounds = {-1, 1, -1, 1}; neigh_iter = (PyArrayNeighborhoodIterObject*)PyArray_NeighborhoodIterNew( iter, bounds, NPY_NEIGHBORHOOD_ITER_ZERO_PADDING, NULL); for(i = 0; i < iter->size; ++i) { for (j = 0; j < neigh_iter->size; ++j) { /* Walk around the item currently pointed by iter->dataptr */ PyArrayNeighborhoodIter_Next(neigh_iter); } /* Move to the next point of iter */ PyArrayIter_Next(iter); PyArrayNeighborhoodIter_Reset(neigh_iter); }
Reset the iterator position to the first point of the neighborhood. This should be called whenever the iter argument given at PyArray_NeighborhoodIterObject is changed (see example)
After this call, iter->dataptr points to the next point of the neighborhood. Calling this function after every point of the neighborhood has been visited is undefined.
Array scalars
Return an array scalar object of the given dtype by copying from memory pointed to by data. base is expected to be the array object that is the owner of the data. base is required if dtype is a void scalar, or if the NPY_USE_GETITEM flag is set and it is known that the getitem method uses the arr argument without checking if it is NULL. Otherwise base may be NULL.
If the data is not in native byte order (as indicated by dtype->byteorder) then this function will byteswap the data, because array scalars are always in correct machine-byte order.
Return a 0-dimensional array of type determined by outcode from scalar which should be an array-scalar object. If outcode is NULL, then the type is determined from scalar.
Return the data (cast to the data type indicated by outcode) from the array-scalar, scalar, into the memory pointed to by ctypeptr (which must be large enough to handle the incoming memory).
Returns -1 on failure, and 0 on success.
Legacy way to query special promotion for scalar values. This is not used in NumPy itself anymore and is expected to be deprecated eventually.
New DTypes can define promotion rules specific to Python scalars.
Legacy way to query special promotion for scalar values. This is not used in NumPy itself anymore and is expected to be deprecated eventually.
Use PyArray_ResultType for similar purposes.
Data-type descriptors
Create a new data-type object with the byteorder set according to newendian. All referenced data-type objects (in subdescr and fields members of the data-type object) are also changed (recursively).
The value of newendian is one of these macros:
If a byteorder of NPY_IGNORE is encountered it is left alone. If newendian is NPY_SWAP, then all byte-orders are swapped. Other valid newendian values are NPY_NATIVE, NPY_LITTLE, and NPY_BIG which all cause the returned data-typed descriptor (and all it's referenced data-type descriptors) to have the corresponding byte- order.
Determine an appropriate data-type object from the object op (which should be a "nested" sequence object) and the minimum data-type descriptor mintype (which can be NULL ). Similar in behavior to array(op).dtype. Don't confuse this function with PyArray_DescrConverter. This function essentially looks at all the objects in the (nested) sequence and determines the data-type from the elements it finds.
Convert any compatible Python object, obj, to a data-type object in dtype. This version of the converter converts None objects so that the returned data-type is NULL. This function can also be used with the "O&" character in PyArg_ParseTuple processing.
Like PyArray_DescrConverter except it aligns C-struct-like objects on word-boundaries as the compiler would.
Like PyArray_DescrConverter2 except it aligns C-struct-like objects on word-boundaries as the compiler would.
Data Type Promotion and Inspection
This function defines the common DType operator. Note that the common DType will not be object (unless one of the DTypes is object). Similar to numpy.result_type, but works on the classes and not instances.
Promotes a list of DTypes with each other in a way that should guarantee stable results even when changing the order. This function is smarter and can often return successful and unambiguous results when common_dtype(common_dtype(dt1, dt2), dt3) would depend on the operation order or fail. Nevertheless, DTypes should aim to ensure that their common-dtype implementation is associative and commutative! (Mainly, unsigned and signed integers are not.)
For guaranteed consistent results DTypes must implement common-Dtype "transitively". If A promotes B and B promotes C, than A must generally also promote C; where "promotes" means implements the promotion. (There are some exceptions for abstract DTypes)
In general this approach always works as long as the most generic dtype is either strictly larger, or compatible with all other dtypes. For example promoting float16 with any other float, integer, or unsigned integer again gives a floating point number.
Custom Data Types
These functions allow defining custom flexible data types outside of NumPy. See NEP 42 <NEP42> for more details about the rationale and design of the new DType system. See the numpy-user-dtypes repository for a number of example DTypes. Also see dtypemeta for documentation on PyArray_DTypeMeta and PyArrayDTypeMeta_Spec.
Initialize a new DType. It must currently be a static Python C type that is declared as PyArray_DTypeMeta and not PyTypeObject. Further, it must subclass np.dtype and set its type to PyArrayDTypeMeta_Type (before calling PyType_Ready()), which has additional fields compared to a normal PyTypeObject. See the examples in the numpy-user-dtypes repository for usage with both parametric and non-parametric data types.
Flags
Flags that can be set on the PyArrayDTypeMeta_Spec to initialize the DType.
Slot IDs and API Function Typedefs
These IDs correspond to slots in the DType API and are used to identify implementations of each slot from the items of the slots array member of PyArrayDTypeMeta_Spec struct.
Used during DType inference to find the correct DType for a given PyObject. Must return a descriptor instance appropriate to store the data in the python object that is passed in. obj is the python object to inspect and cls is the DType class to create a descriptor for.
Returns the default descriptor instance for the DType. Must be defined for parametric data types. Non-parametric data types return the singleton by default.
Given two input DTypes, determines the appropriate "common" DType that can store values for both types. Returns Py_NotImplemented if no such type exists.
Given two input descriptors, determines the appropriate "common" descriptor that can store values for both instances. Returns NULL on error.
Returns the "canonical" representation for a descriptor instance. The notion of a canonical descriptor generalizes the concept of byte order, in that a canonical descriptor always has native byte order. If the descriptor is already canonical, this function returns a new reference to the input descriptor.
If defined, a function that is called to "finalize" a descriptor instance after an array is created. One use of this function is to force newly created arrays to have a newly created descriptor instance, no matter what input descriptor is provided by a user.
If defined, allows the DType to expose constant values such as machine limits, special values (infinity, NaN), and floating-point characteristics. The descr is the descriptor instance, constant_id is one of the NPY_CONSTANT_* macros, and out is a pointer to uninitialized memory where the constant value should be written. The memory pointed to by out may be unaligned and is uninitialized. Returns 1 on success, 0 if the constant is not available, or -1 with an error set.
Constant IDs:
The following constant IDs are defined for retrieving dtype-specific values:
Basic constants (available for all numeric types):
Floating-point special values:
Floating-point characteristics (values of the dtype's native type):
Floating-point characteristics (integer values, type npy_intp):
These constants return integer metadata about the floating-point representation. They are marked with the 1 << 16 bit to indicate they return npy_intp values rather than the dtype's native type.
PyArray_ArrFuncs slots
In addition the above slots, the following slots are exposed to allow filling the arrfuncs-type struct attached to descriptor instances. Note that in the future these will be replaced by proper DType API slots but for now we have exposed the legacy PyArray_ArrFuncs slots.
Macros and Static Inline Functions
These macros and static inline functions are provided to allow more understandable and idiomatic code when working with PyArray_DTypeMeta instances.
Returns a PyArray_DTypeMeta * pointer to a new reference to a DType.
Conversion utilities
For use with PyArg_ParseTuple
All of these functions can be used in PyArg_ParseTuple (...) with the "O&" format specifier to automatically convert any Python object to the required C-object. All of these functions return NPY_SUCCEED if successful and NPY_FAIL if not. The first argument to all of these function is a Python object. The second argument is the address of the C-type to convert the Python object to.
This is a default converter for output arrays given to functions. If obj is Py_None or NULL, then \*address will be NULL but the call will succeed. If PyArray_Check ( obj) is TRUE then it is returned in \*address without incrementing its reference count.
Convert Python strings into one of NPY_SEARCHLEFT (starts with 'l' or 'L'), or NPY_SEARCHRIGHT (starts with 'r' or 'R').
Convert the Python strings 'no', 'equiv', 'safe', 'same_kind', and 'unsafe' into the NPY_CASTING enumeration NPY_NO_CASTING, NPY_EQUIV_CASTING, NPY_SAFE_CASTING, NPY_SAME_KIND_CASTING, and NPY_UNSAFE_CASTING.
Convert the Python strings 'clip', 'wrap', and 'raise' into the NPY_CLIPMODE enumeration NPY_CLIP, NPY_WRAP, and NPY_RAISE.
Converts either a sequence of clipmodes or a single clipmode into a C array of NPY_CLIPMODE values. The number of clipmodes n must be known before calling this function. This function is provided to help functions allow a different clipmode for each dimension.
Other conversions
Convert any Python sequence (or single Python number) passed in as seq to (up to) maxvals pointer-sized integers and place them in the vals array. The sequence can be smaller then maxvals as the number of converted objects is returned.
Including and importing the C API
To use the NumPy C-API you typically need to include the numpy/ndarrayobject.h header and numpy/ufuncobject.h for some ufunc related functionality (arrayobject.h is an alias for ndarrayobject.h).
These two headers export most relevant functionality. In general any project which uses the NumPy API must import NumPy using one of the functions PyArray_ImportNumPyAPI() or import_array(). In some places, functionality which requires import_array() is not needed, because you only need type definitions. In this case, it is sufficient to include numpy/ndarratypes.h.
For the typical Python project, multiple C or C++ files will be compiled into a single shared object (the Python C-module) and PyArray_ImportNumPyAPI() should be called inside it's module initialization.
When you have a single C-file, this will consist of:
#include "numpy/ndarrayobject.h"PyMODINIT_FUNC PyInit_my_module(void){ if (PyArray_ImportNumPyAPI() < 0) { return NULL; } /* Other initialization code. */}
However, most projects will have additional C files which are all linked together into a single Python module. In this case, the helper C files typically do not have a canonical place where PyArray_ImportNumPyAPI should be called (although it is OK and fast to call it often).
To solve this, NumPy provides the following pattern that the main file is modified to define PY_ARRAY_UNIQUE_SYMBOL before the include:
/* Main module file */#define PY_ARRAY_UNIQUE_SYMBOL MyModule#include "numpy/ndarrayobject.h"PyMODINIT_FUNC PyInit_my_module(void){ if (PyArray_ImportNumPyAPI() < 0) { return NULL; } /* Other initialization code. */}
while the other files use:
/* Second file without any import */#define NO_IMPORT_ARRAY#define PY_ARRAY_UNIQUE_SYMBOL MyModule#include "numpy/ndarrayobject.h"
You can of course add the defines to a local header used throughout. You just have to make sure that the main file does _not_ define NO_IMPORT_ARRAY.
For numpy/ufuncobject.h the same logic applies, but the unique symbol mechanism is #define PY_UFUNC_UNIQUE_SYMBOL (both can match).
Additionally, you will probably wish to add a #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION to avoid warnings about possible use of old API.
Mechanism details and dynamic linking
The main part of the mechanism is that without NumPy needs to define a void **PyArray_API table for you to look up all functions. Depending on your macro setup, this takes different routes depending on whether NO_IMPORT_ARRAY and PY_ARRAY_UNIQUE_SYMBOL are defined:
If neither is defined, the C-API is declared to static void **PyArray_API, so it is only visible within the compilation unit/file using #include <numpy/arrayobject.h>.
If only PY_ARRAY_UNIQUE_SYMBOL is defined (it could be empty) then the it is declared to a non-static void ** allowing it to be used by other files which are linked.
If NO_IMPORT_ARRAY is defined, the table is declared as extern void **, meaning that it must be linked to a file which does not use NO_IMPORT_ARRAY.
The PY_ARRAY_UNIQUE_SYMBOL mechanism additionally mangles the names to avoid conflicts.
In order to make use of the C-API from another extension module, the import_array function must be called. If the extension module is self-contained in a single .c file, then that is all that needs to be done. If, however, the extension module involves multiple files where the C-API is needed then some additional steps must be taken.
Checking the API Version
Because python extensions are not used in the same way as usual libraries on most platforms, some errors cannot be automatically detected at build time or even runtime. For example, if you build an extension using a function available only for numpy >= 1.3.0, and you import the extension later with numpy 1.2, you will not get an import error (but almost certainly a segmentation fault when calling the function). That's why several functions are provided to check for numpy versions. The macros NPY_VERSION and NPY_FEATURE_VERSION corresponds to the numpy version used to build the extension, whereas the versions returned by the functions PyArray_GetNDArrayCVersion and PyArray_GetNDArrayCFeatureVersion corresponds to the runtime numpy's version.
The rules for ABI and API compatibilities can be summarized as follows:
Whenever NPY_VERSION != PyArray_GetNDArrayCVersion(), the extension has to be recompiled (ABI incompatibility).
NPY_VERSION == PyArray_GetNDArrayCVersion() and NPY_FEATURE_VERSION <= PyArray_GetNDArrayCFeatureVersion() means backward compatible changes.
ABI incompatibility is automatically detected in every numpy's version. API incompatibility detection was added in numpy 1.4.0. If you want to supported many different numpy versions with one extension binary, you have to build your extension with the lowest NPY_FEATURE_VERSION as possible.
Memory management
Threading support
These macros are only meaningful if NPY_ALLOW_THREADS evaluates True during compilation of the extension module. Otherwise, these macros are equivalent to whitespace. Python uses a single Global Interpreter Lock (GIL) for each Python process so that only a single thread may execute at a time (even on multi-cpu machines). When calling out to a compiled function that may take time to compute (and does not have side-effects for other threads like updated global variables), the GIL should be released so that other Python threads can run while the time-consuming calculations are performed. This can be accomplished using two groups of macros. Typically, if one macro in a group is used in a code block, all of them must be used in the same code block. NPY_ALLOW_THREADS is true (defined as 1) unless the build option -Ddisable-threading is set to true - in which case NPY_ALLOW_THREADS is false (0).
Group 1
This group is used to call code that may take some time but does not use any Python C-API calls. Thus, the GIL should be released during its calculation.
Group 2
This group is used to re-acquire the Python GIL after it has been released. For example, suppose the GIL has been released (using the previous calls), and then some path in the code (perhaps in a different subroutine) requires use of the Python C-API, then these macros are useful to acquire the GIL. These macros accomplish essentially a reverse of the previous three (acquire the LOCK saving what state it had) and then re-release it with the saved state.