numpy  2.0.0
src/multiarray/ctors.h File Reference

Go to the source code of this file.

Functions

NPY_NO_EXPORT PyObject * PyArray_NewFromDescr (PyTypeObject *subtype, PyArray_Descr *descr, int nd, npy_intp *dims, npy_intp *strides, void *data, int flags, PyObject *obj)
NPY_NO_EXPORT PyObject * PyArray_NewFromDescr_int (PyTypeObject *subtype, PyArray_Descr *descr, int nd, npy_intp *dims, npy_intp *strides, void *data, int flags, PyObject *obj, int zeroed, int allow_emptystring)
NPY_NO_EXPORT PyObject * PyArray_New (PyTypeObject *, int nd, npy_intp *, int, npy_intp *, void *, int, int, PyObject *)
NPY_NO_EXPORT PyObject * PyArray_FromAny (PyObject *op, PyArray_Descr *newtype, int min_depth, int max_depth, int flags, PyObject *context)
NPY_NO_EXPORT PyObject * PyArray_CheckFromAny (PyObject *op, PyArray_Descr *descr, int min_depth, int max_depth, int requires, PyObject *context)
NPY_NO_EXPORT PyObject * PyArray_FromArray (PyArrayObject *arr, PyArray_Descr *newtype, int flags)
NPY_NO_EXPORT PyObject * PyArray_FromStructInterface (PyObject *input)
NPY_NO_EXPORT PyObject * PyArray_FromInterface (PyObject *input)
NPY_NO_EXPORT PyObject * PyArray_FromArrayAttr (PyObject *op, PyArray_Descr *typecode, PyObject *context)
NPY_NO_EXPORT PyObject * PyArray_EnsureArray (PyObject *op)
NPY_NO_EXPORT PyObject * PyArray_EnsureAnyArray (PyObject *op)
NPY_NO_EXPORT int PyArray_MoveInto (PyArrayObject *dest, PyArrayObject *src)
NPY_NO_EXPORT int PyArray_CopyAnyInto (PyArrayObject *dest, PyArrayObject *src)
NPY_NO_EXPORT PyObject * PyArray_CheckAxis (PyArrayObject *arr, int *axis, int flags)
NPY_NO_EXPORT int PyArray_CopyAsFlat (PyArrayObject *dst, PyArrayObject *src, NPY_ORDER order)
NPY_NO_EXPORT void _array_fill_strides (npy_intp *strides, npy_intp *dims, int nd, size_t itemsize, int inflag, int *objflags)
NPY_NO_EXPORT void _unaligned_strided_byte_copy (char *dst, npy_intp outstrides, char *src, npy_intp instrides, npy_intp N, int elsize)
NPY_NO_EXPORT void _strided_byte_swap (void *p, npy_intp stride, npy_intp n, int size)
NPY_NO_EXPORT void copy_and_swap (void *dst, void *src, int itemsize, npy_intp numitems, npy_intp srcstrides, int swap)
NPY_NO_EXPORT void byte_swap_vector (void *p, npy_intp n, int size)
NPY_NO_EXPORT int PyArray_AssignFromSequence (PyArrayObject *self, PyObject *v)
NPY_NO_EXPORT PyArrayObjectPyArray_SubclassWrap (PyArrayObject *arr_of_subclass, PyArrayObject *towrap)

Function Documentation

NPY_NO_EXPORT void _array_fill_strides ( npy_intp strides,
npy_intp dims,
int  nd,
size_t  itemsize,
int  inflag,
int *  objflags 
)
FIXME: remove those from here
This is the main array creation routine.
Flags argument has multiple related meanings depending on data and strides:
If data is given, then flags is flags associated with data. If strides is not given, then a contiguous strides array will be created and the NPY_ARRAY_C_CONTIGUOUS bit will be set. If the flags argument has the NPY_ARRAY_F_CONTIGUOUS bit set, then a FORTRAN-style strides array will be created (and of course the NPY_ARRAY_F_CONTIGUOUS flag bit will be set).
If data is not given but created here, then flags will be NPY_ARRAY_DEFAULT and a non-zero flags argument can be used to indicate a FORTRAN style array is desired.
Dimensions and itemsize must have been checked for validity.

<

NPY_RELAXED_STRIDES_CHECKING
Only make Fortran strides if not contiguous as well

<

not NPY_RELAXED_STRIDES_CHECKING

<

not NPY_RELAXED_STRIDES_CHECKING

Referenced by array_setstate().

NPY_NO_EXPORT void _strided_byte_swap ( void *  p,
npy_intp  stride,
npy_intp  n,
int  size 
)

<

no byteswap necessary

Referenced by to().

NPY_NO_EXPORT void _unaligned_strided_byte_copy ( char *  dst,
npy_intp  outstrides,
char *  src,
npy_intp  instrides,
npy_intp  N,
int  elsize 
)

References npy_bswap4().

Referenced by _scan(), LONGDOUBLE_scan(), and to().

NPY_NO_EXPORT void byte_swap_vector ( void *  p,
npy_intp  n,
int  size 
)
NPY_NO_EXPORT void copy_and_swap ( void *  dst,
void *  src,
int  itemsize,
npy_intp  numitems,
npy_intp  srcstrides,
int  swap 
)
If numitems > 1, then dst must be contiguous
NPY_NO_EXPORT int PyArray_AssignFromSequence ( PyArrayObject self,
PyObject *  v 
)
NPY_NO_EXPORT PyObject* PyArray_CheckAxis ( PyArrayObject arr,
int *  axis,
int  flags 
)
PyArray_CheckAxis <blockquote> check that axis is valid convert 0-d arrays to 1-d arrays</blockquote>

Referenced by PyArray_ArgMax(), PyArray_Max(), PyArray_Mean(), PyArray_Min(), PyArray_Prod(), PyArray_Sum(), and PyArray_TakeFrom().

NPY_NO_EXPORT PyObject* PyArray_CheckFromAny ( PyObject *  op,
PyArray_Descr descr,
int  min_depth,
int  max_depth,
int  requires,
PyObject *  context 
)
flags is any of NPY_ARRAY_C_CONTIGUOUS (formerly CONTIGUOUS), NPY_ARRAY_F_CONTIGUOUS (formerly FORTRAN), NPY_ARRAY_ALIGNED, NPY_ARRAY_WRITEABLE, NPY_ARRAY_NOTSWAPPED, NPY_ARRAY_ENSURECOPY, NPY_ARRAY_UPDATEIFCOPY, NPY_ARRAY_FORCECAST, NPY_ARRAY_ENSUREARRAY, NPY_ARRAY_ELEMENTSTRIDES
or'd (|) together
Any of these flags present means that the returned array should guarantee that aspect of the array. Otherwise the returned array won't guarantee it -- it will depend on the object as to whether or not it has such features.
Note that NPY_ARRAY_ENSURECOPY is enough to guarantee NPY_ARRAY_C_CONTIGUOUS, NPY_ARRAY_ALIGNED and NPY_ARRAY_WRITEABLE and therefore it is redundant to include those as well.
NPY_ARRAY_BEHAVED == NPY_ARRAY_ALIGNED | NPY_ARRAY_WRITEABLE NPY_ARRAY_CARRAY = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_BEHAVED NPY_ARRAY_FARRAY = NPY_ARRAY_F_CONTIGUOUS | NPY_ARRAY_BEHAVED
NPY_ARRAY_F_CONTIGUOUS can be set in the FLAGS to request a FORTRAN array. Fortran arrays are always behaved (aligned, notswapped, and writeable) and not (C) CONTIGUOUS (if > 1d).
NPY_ARRAY_UPDATEIFCOPY flag sets this flag in the returned array if a copy is made and the base argument points to the (possibly) misbehaved array. When the new array is deallocated, the original array held in base is updated with the contents of the new array.
NPY_ARRAY_FORCECAST will cause a cast to occur regardless of whether or not it is safe.
steals a reference to descr -- accepts NULL
Copy an Array into another array -- memory must not overlap

Does not require src and dest to have "broadcastable" shapes (only the same number of elements).

TODO: For NumPy 2.0, this could accept an order parameter which
only allows NPY_CORDER and NPY_FORDER. Could also rename this to CopyAsFlat to make the name more intuitive.

Returns 0 on success, -1 on error.

TODO: Put the order parameter in PyArray_CopyAnyInto and remove this
If the shapes match and a particular order is forced for both, use the more efficient CopyInto
Zero-sized arrays require nothing be done
This copy is based on matching C-order traversals of src and dst. By using two iterators, we can find maximal sub-chunks that can be processed at once.
Get all the values needed for the inner loop
Since buffering is disabled, we can cache the stride
Since buffering is disabled, we can cache the stride
Because buffering is disabled in the iterator, the inner loop strides will be the same throughout the iteration loop. Thus, we can pass them to this function to take advantage of contiguous strides, etc.
Transfer the biggest amount that fits both
If we exhausted the dst block, refresh it
If we exhausted the src block, refresh it
NPY_NO_EXPORT PyObject* PyArray_EnsureAnyArray ( PyObject *  op)
NPY_NO_EXPORT PyObject* PyArray_EnsureArray ( PyObject *  op)
end old calls
This is a quick wrapper around PyArray_FromAny(op, NULL, 0, 0, ENSUREARRAY)
that special cases Arrays and PyArray_Scalars up front It steals a reference to the object It also guarantees that the result is PyArray_Type Because it decrefs op if any conversion needs to take place so it can be used like PyArray_EnsureArray(some_function(...))
NPY_NO_EXPORT PyObject* PyArray_FromAny ( PyObject *  op,
PyArray_Descr newtype,
int  min_depth,
int  max_depth,
int  flags,
PyObject *  context 
)
Does not check for NPY_ARRAY_ENSURECOPY and NPY_ARRAY_NOTSWAPPED in flags
Steals a reference to newtype --- which can be NULL
This is the main code to make a NumPy array from a Python Object. It is called from many different places.
Get either the array or its parameters if it isn't an array
If the requested dtype is flexible, adapt it
If we got dimensions and dtype instead of an array
TODO: would be nice to do this too, but it's
a behavior change. It's also a bit tricky for downcasting to small integer and float types, and might be better to modify PyArray_AssignFromSequence and descr->f->setitem to have a 'casting' parameter and to check each value with scalar rules like in PyArray_MinScalarType.
if (!(flags&NPY_ARRAY_FORCECAST) && ndim > 0 &&

<blockquote class="first"> !PyArray_CanCastTo(dtype, newtype)) {</blockquote>

System Message: WARNING/2 (<string>, line 4) Block quote ends without a blank line; unexpected unindent.

Py_DECREF(dtype); Py_XDECREF(newtype); PyErr_SetString(PyExc_TypeError,

System Message: ERROR/3 (<string>, line 7) Unexpected indentation.

<blockquote> "object cannot be safely cast to array " "of required type");</blockquote>

System Message: WARNING/2 (<string>, line 9) Block quote ends without a blank line; unexpected unindent.

return NULL;

System Message: WARNING/2 (<string>, line 10) Definition list ends without a blank line; unexpected unindent.
}
Create an array and copy the data

Referenced by _array_fromobject(), _pyarray_correlate(), array_imag_get(), PyArray_CanCoerceScalar(), PyArray_CompareLists(), and PyUFunc_FromFuncAndDataAndSignature().

NPY_NO_EXPORT PyObject* PyArray_FromArray ( PyArrayObject arr,
PyArray_Descr newtype,
int  flags 
)
steals reference to newtype --- acc. NULL
Check if object is of array with Null newtype. If so return it directly instead of checking for casting.
If the casting if forced, use the 'unsafe' casting rule
Raise an error if the casting rule isn't followed
If a guaranteed copy was requested
If C contiguous was requested, and arr is not
If an aligned array was requested, and arr is not
If a Fortran contiguous array was requested, and arr is not
If a writeable array was requested, and arr is not
Set the order for the copy being made based on the flags
If no copy then take an appropriate view if necessary, or just return a reference to ret itself.

Referenced by PyArray_ArgMax(), and PyArray_TakeFrom().

NPY_NO_EXPORT PyObject* PyArray_FromArrayAttr ( PyObject *  op,
PyArray_Descr typecode,
PyObject *  context 
)
NPY_NO_EXPORT PyObject* PyArray_FromInterface ( PyObject *  origin)
Get type string from interface specification
Get dtype from type string
If the dtype is NPY_VOID, see if there is extra information in the 'descr' attribute.
Get shape tuple from interface specification
Shape must be specified when 'data' is specified
Assume shape as scalar otherwise
NOTE: pointers to data and base should be NULL
Make sure 'shape' is a tuple
Get dimensions from shape tuple
Get data buffer from interface specification
Case for data access through pointer
Case for data access through buffer
Get offset number from interface specification
NPY_NO_EXPORT PyObject* PyArray_FromStructInterface ( PyObject *  input)
Move the memory of one array into another, allowing for overlapping data. <blockquote> Returns 0 on success, negative on failure.</blockquote>

References PyArray_Byteswap(), and PyArray_DESCR.

NPY_NO_EXPORT PyObject* PyArray_New ( PyTypeObject *  subtype,
int  nd,
npy_intp dims,
int  type_num,
npy_intp strides,
void *  data,
int  itemsize,
int  flags,
PyObject *  obj 
)
Generic new array creation routine.

Referenced by PyArray_ArgMax().

NPY_NO_EXPORT PyObject* PyArray_NewFromDescr ( PyTypeObject *  subtype,
PyArray_Descr descr,
int  nd,
npy_intp dims,
npy_intp strides,
void *  data,
int  flags,
PyObject *  obj 
)
Generic new array creation routine. <blockquote> steals a reference to descr. On failure or when dtype->subarray is true, dtype will be decrefed.</blockquote>

Referenced by allocate_reduce_result(), NpyIter_GetShape(), prepare_ufunc_output(), PyArray_CastScalarDirect(), PyArray_TakeFrom(), and PyArray_Zeros().

NPY_NO_EXPORT PyObject* PyArray_NewFromDescr_int ( PyTypeObject *  subtype,
PyArray_Descr descr,
int  nd,
npy_intp dims,
npy_intp strides,
void *  data,
int  flags,
PyObject *  obj,
int  zeroed,
int  allow_emptystring 
)
Generic new array creation routine. Internal variant with calloc argument for PyArray_Zeros.
steals a reference to descr. On failure or descr->subarray, descr will be decrefed.
Check datatype element size
Check dimensions and multiply them to nbytes
Compare to PyArray_OverflowMultiplyList that returns 0 in this case.
Care needs to be taken to avoid integer overflow when multiplying the dimensions together to get the total size of the array.

<

fill it in
we allow strides even when we create the memory, but be careful with this...
Allocate something even for zero-space arrays e.g. shape=(0,) -- otherwise buffer exposure (a.data) doesn't work as it should. Could probably just allocate a few bytes here. -- Chuck
It is bad to have uninitialized OBJECT pointers which could also be sub-fields of a VOID array
If data is passed in, this object won't own it by default. Caller must arrange for this to be reset if truly desired
always update the flags to get the right CONTIGUOUS, ALIGN properties not owned data and input strides may not be aligned and on some platforms (debian sparc) malloc does not provide enough alignment for long double types
call the __array_finalize__ method if a subtype. If obj is NULL, then call method with Py_None
A C-function is stored here

Referenced by PyArray_Arange().

Calls arr_of_subclass.__array_wrap__(towrap), in order to make 'towrap' have the same ndarray subclass as 'arr_of_subclass'.