numpy  2.0.0
src/multiarray/array_assign_array.c File Reference
#include <Python.h>
#include <numpy/ndarraytypes.h>
#include "npy_config.h"
#include "npy_pycompat.h"
#include "convert_datatype.h"
#include "methods.h"
#include "shape.h"
#include "lowlevel_strided_loops.h"
#include "array_assign.h"

Defines

#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API   NPY_API_VERSION
#define _MULTIARRAYMODULE

Functions

NPY_NO_EXPORT int raw_array_assign_array (int ndim, npy_intp *shape, PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, PyArray_Descr *src_dtype, char *src_data, npy_intp *src_strides)
NPY_NO_EXPORT int raw_array_wheremasked_assign_array (int ndim, npy_intp *shape, PyArray_Descr *dst_dtype, char *dst_data, npy_intp *dst_strides, PyArray_Descr *src_dtype, char *src_data, npy_intp *src_strides, PyArray_Descr *wheremask_dtype, char *wheremask_data, npy_intp *wheremask_strides)
NPY_NO_EXPORT int PyArray_AssignArray (PyArrayObject *dst, PyArrayObject *src, PyArrayObject *wheremask, NPY_CASTING casting)

Define Documentation

#define NPY_NO_DEPRECATED_API   NPY_API_VERSION

Function Documentation

NPY_NO_EXPORT int PyArray_AssignArray ( PyArrayObject dst,
PyArrayObject src,
PyArrayObject wheremask,
NPY_CASTING  casting 
)
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 expressions 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 in mapping.c np.add(tmp, x, tmp) a[1000:6000] = tmp # Calls array_assign_subscript 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 _array_fromobject().

NPY_NO_EXPORT int raw_array_assign_array ( int  ndim,
npy_intp shape,
PyArray_Descr dst_dtype,
char *  dst_data,
npy_intp dst_strides,
PyArray_Descr src_dtype,
char *  src_data,
npy_intp src_strides 
)
Assigns the array from 'src' to 'dst'. The strides must already have been broadcast.
Returns 0 on success, -1 on failure.
Check alignment
Use raw iteration with no heap allocation
Overlap check for the 1D case. Higher dimensional arrays and opposite strides cause a temporary copy before getting here.
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_START, NPY_RAW_ITER_TWO_NEXT, NPY_SUCCEED, PyArray_GetDTypeTransferFunction(), PyArray_PrepareTwoRawArrayIter(), and raw_array_is_aligned().

NPY_NO_EXPORT int raw_array_wheremasked_assign_array ( int  ndim,
npy_intp shape,
PyArray_Descr dst_dtype,
char *  dst_data,
npy_intp dst_strides,
PyArray_Descr src_dtype,
char *  src_data,
npy_intp src_strides,
PyArray_Descr wheremask_dtype,
char *  wheremask_data,
npy_intp wheremask_strides 
)
Assigns the array from 'src' to 'dst, wherever the 'wheremask' value is True. The strides must already have been broadcast.
Returns 0 on success, -1 on failure.
Check alignment
Use raw iteration with no heap allocation
Overlap check for the 1D case. Higher dimensional arrays cause a temporary copy before getting here.
Get the function to do the casting
Process the innermost dimension