numpy  2.0.0
src/multiarray/array_assign.h File Reference

Go to the source code of this file.

Defines

#define NPY_ARRAY_ASSIGN_BUFFERSIZE   8192

Functions

NPY_NO_EXPORT int PyArray_AssignArray (PyArrayObject *dst, PyArrayObject *src, PyArrayObject *wheremask, NPY_CASTING casting)
NPY_NO_EXPORT int PyArray_AssignRawScalar (PyArrayObject *dst, PyArray_Descr *src_dtype, char *src_data, PyArrayObject *wheremask, NPY_CASTING casting)
NPY_NO_EXPORT int raw_array_assign_scalar (int ndim, npy_intp *shape, PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, PyArray_Descr *src_dtype, char *src_data)
NPY_NO_EXPORT int raw_array_wheremasked_assign_scalar (int ndim, npy_intp *shape, PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, PyArray_Descr *src_dtype, char *src_data, PyArray_Descr *wheremask_dtype, char *wheremask_data, npy_intp *wheremask_strides)
NPY_NO_EXPORT int broadcast_strides (int ndim, npy_intp *shape, int strides_ndim, npy_intp *strides_shape, npy_intp *strides, char *strides_name, npy_intp *out_strides)
NPY_NO_EXPORT int raw_array_is_aligned (int ndim, char *data, npy_intp *strides, int alignment)
NPY_NO_EXPORT int arrays_overlap (PyArrayObject *arr1, PyArrayObject *arr2)

Define Documentation

#define NPY_ARRAY_ASSIGN_BUFFERSIZE   8192
** LOW-LEVEL ARRAY MANIPULATION HELPERS ***
Internal detail of how much to buffer during array assignments which need it. This is for more complex NA masking operations where masks need to be inverted or combined together.

Function Documentation

Returns 1 if the arrays have overlapping data, 0 otherwise
NPY_NO_EXPORT int broadcast_strides ( int  ndim,
npy_intp shape,
int  strides_ndim,
npy_intp strides_shape,
npy_intp strides,
char *  strides_name,
npy_intp out_strides 
)
Broadcasts strides to match the given dimensions. Can be used, for instance, to set up a raw iteration.
'strides_name' is used to produce an error message if the strides cannot be broadcast.
Returns 0 on success, -1 on failure.
See array_assign.h for parameter documentation
Can't broadcast to fewer dimensions
Process from the end to the start, so that 'strides' and 'out_strides' can point to the same memory.
If it doesn't have dimension one, it must match
New dimensions get a zero stride

References build_shape_string(), PyUString_ConcatAndDel, PyUString_FromFormat, and PyUString_FromString.

NPY_NO_EXPORT int PyArray_AssignArray ( PyArrayObject dst,
PyArrayObject src,
PyArrayObject wheremask,
NPY_CASTING  casting 
)
An array assignment function for copying arrays, treating the arrays as flat according to their respective ordering rules. This function makes a temporary copy of 'src' if 'src' and 'dst' overlap, to be able to handle views of the same data with different strides.
dst: The destination array. dst_order: The rule for how 'dst' is to be made flat. src: The source array. src_order: The rule for how 'src' is to be made flat. casting: An exception is raised if the copy violates this

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

<blockquote> casting rule.</blockquote>

Returns 0 on success, -1 on failure.
<blockquote> Not yet implemented</blockquote>

System Message: WARNING/2 (<string>, line 2) Block quote ends without a blank line; unexpected unindent.
NPY_NO_EXPORT int PyArray_AssignArrayAsFlat(PyArrayObject *dst, NPY_ORDER dst_order,

System Message: WARNING/2 (<string>, line 2); backlink Inline emphasis start-string without end-string.
System Message: ERROR/3 (<string>, line 4) Unexpected indentation.

<blockquote>

PyArrayObject *src, NPY_ORDER src_order, NPY_CASTING casting, npy_bool preservena, npy_bool *preservewhichna);

System Message: WARNING/2 (<string>, line 4); backlink Inline emphasis start-string without end-string.
System Message: WARNING/2 (<string>, line 4); backlink Inline emphasis start-string without end-string.

</blockquote>

An array assignment function for copying arrays, broadcasting 'src' into 'dst'. This function makes a temporary copy of 'src' if 'src' and 'dst' overlap, to be able to handle views of the same data with different strides.
dst: The destination array. src: The source array. wheremask: If non-NULL, a boolean mask specifying where to copy. casting: An exception is raised if the copy violates this

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

<blockquote> casting rule.</blockquote>

Returns 0 on success, -1 on failure.
Use array_assign_scalar if 'src' NDIM is 0
Performance fix for expresions like "a[1000:6000] += x". In this case, first an in-place add is done, followed by an assignment, equivalently expressed like this: <blockquote> tmp = a[1000:6000] # Calls array_subscript_nice in mapping.c np.add(tmp, x, tmp) a[1000:6000] = tmp # Calls array_ass_sub in mapping.c</blockquote>
In the assignment the underlying data type, shape, strides, and data pointers are identical, but src != dst because they are separately generated slices. By detecting this and skipping the redundant copy of values to themselves, we potentially give a big speed boost.
Note that we don't call EquivTypes, because usually the exact same dtype object will appear, and we don't want to slow things down with a complicated comparison. The comparisons are ordered to try and reject this with as little work as possible.
printf("Redundant copy operation detectedn");
Check the casting rule
When ndim is 1 and the strides point in the same direction, the lower-level inner loop handles copying of overlapping data. For bigger ndim and opposite-strided 1D data, we make a temporary copy of 'src' if 'src' and 'dst' overlap.'
Allocate a temporary copy array.
Broadcast 'src' to 'dst' for raw iteration
As a special case for backwards compatibility, strip away unit dimensions from the left of 'src'
A straightforward value assignment
Do the assignment with raw array iteration
Broadcast the wheremask to 'dst' for raw iteration
A straightforward where-masked assignment
Do the masked assignment with raw array iteration

Referenced by _prepend_ones(), PyArray_AssignZero(), and PyArray_FromArrayAttr().

NPY_NO_EXPORT int PyArray_AssignRawScalar ( PyArrayObject dst,
PyArray_Descr src_dtype,
char *  src_data,
PyArrayObject wheremask,
NPY_CASTING  casting 
)
Assigns a scalar value specified by 'src_dtype' and 'src_data' to elements of 'dst'.
dst: The destination array. src_dtype: The data type of the source scalar. src_data: The memory element of the source scalar. wheremask: If non-NULL, a boolean mask specifying where to copy. casting: An exception is raised if the assignment violates this

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

<blockquote> casting rule.</blockquote>

This function is implemented in array_assign_scalar.c.
Returns 0 on success, -1 on failure.
Check the casting rule
Make a copy of the src data if it's a different dtype than 'dst' or isn't aligned, and the destination we're copying to has more than one element. To avoid having to manage object lifetimes, we also skip this if 'dst' has an object dtype.
Use a static buffer to store the aligned/cast version, or allocate some memory if more space is needed.
Replace src_data/src_dtype
A straightforward value assignment
Do the assignment with raw array iteration
Broadcast the wheremask to 'dst' for raw iteration
Do the masked assignment with raw array iteration
NPY_NO_EXPORT int raw_array_assign_scalar ( int  ndim,
npy_intp shape,
PyArray_Descr dst_dtype,
char *  dst_data,
npy_intp dst_strides,
PyArray_Descr src_dtype,
char *  src_data 
)
** LOW-LEVEL SCALAR TO ARRAY ASSIGNMENT ***
Assigns the scalar value to every element of the destination raw array.
Returns 0 on success, -1 on failure.
Assigns the scalar value to every element of the destination raw array.
Returns 0 on success, -1 on failure.
Check alignment
Use raw iteration with no heap allocation
Get the function to do the casting
Process the innermost dimension

References _PyArray_Descr::alignment, _PyArray_Descr::elsize, NPY_AUXDATA_FREE, NPY_BEGIN_THREADS, NPY_BEGIN_THREADS_DEF, NPY_END_THREADS, NPY_MAXDIMS, NPY_RAW_ITER_ONE_NEXT, NPY_RAW_ITER_START, NPY_SUCCEED, PyArray_GetDTypeTransferFunction(), PyArray_PrepareOneRawArrayIter(), and raw_array_is_aligned().

NPY_NO_EXPORT int raw_array_is_aligned ( int  ndim,
char *  data,
npy_intp strides,
int  alignment 
)
Checks whether a data pointer + set of strides refers to a raw array which is fully aligned data.
See array_assign.h for parameter documentation

References PyArray_DATA, PyArray_DIMS, PyArray_NDIM, and PyArray_STRIDES.

Referenced by raw_array_assign_array(), and raw_array_assign_scalar().

NPY_NO_EXPORT int raw_array_wheremasked_assign_scalar ( int  ndim,
npy_intp shape,
PyArray_Descr dst_dtype,
char *  dst_data,
npy_intp dst_strides,
PyArray_Descr src_dtype,
char *  src_data,
PyArray_Descr wheremask_dtype,
char *  wheremask_data,
npy_intp wheremask_strides 
)
Assigns the scalar value to every element of the destination raw array where the 'wheremask' value is True.
Returns 0 on success, -1 on failure.
Check alignment
Use raw iteration with no heap allocation
Get the function to do the casting
Process the innermost dimension