MCPcopy Create free account
hub / github.com/numpy/numpy / array_assign_boolean_subscript

Function array_assign_boolean_subscript

numpy/core/src/multiarray/mapping.c:1077–1244  ·  view source on GitHub ↗

* Implements boolean indexing assignment. This takes the one-dimensional * array 'v' and assigns its values to all of the elements of 'self' for which * the corresponding element of 'op' is True. * * This operation is somewhat unfortunate, because to match up with * a one-dimensional output array, it has to choose a particular * iteration order, in the case of NumPy that is always C order ev

Source from the content-addressed store, hash-verified

1075 * Returns 0 on success, -1 on failure.
1076 */
1077NPY_NO_EXPORT int
1078array_assign_boolean_subscript(PyArrayObject *self,
1079 PyArrayObject *bmask, PyArrayObject *v, NPY_ORDER order)
1080{
1081 npy_intp size, v_stride;
1082 char *v_data;
1083 npy_intp bmask_size;
1084
1085 if (PyArray_DESCR(bmask)->type_num != NPY_BOOL) {
1086 PyErr_SetString(PyExc_TypeError,
1087 "NumPy boolean array indexing assignment "
1088 "requires a boolean index");
1089 return -1;
1090 }
1091
1092 if (PyArray_NDIM(v) > 1) {
1093 PyErr_Format(PyExc_TypeError,
1094 "NumPy boolean array indexing assignment "
1095 "requires a 0 or 1-dimensional input, input "
1096 "has %d dimensions", PyArray_NDIM(v));
1097 return -1;
1098 }
1099
1100 if (PyArray_NDIM(bmask) != PyArray_NDIM(self)) {
1101 PyErr_SetString(PyExc_ValueError,
1102 "The boolean mask assignment indexing array "
1103 "must have the same number of dimensions as "
1104 "the array being indexed");
1105 return -1;
1106 }
1107
1108 size = count_boolean_trues(PyArray_NDIM(bmask), PyArray_DATA(bmask),
1109 PyArray_DIMS(bmask), PyArray_STRIDES(bmask));
1110 /* Correction factor for broadcasting 'bmask' to 'self' */
1111 bmask_size = PyArray_SIZE(bmask);
1112 if (bmask_size > 0) {
1113 size *= PyArray_SIZE(self) / bmask_size;
1114 }
1115
1116 /* Tweak the strides for 0-dim and broadcasting cases */
1117 if (PyArray_NDIM(v) > 0 && PyArray_DIMS(v)[0] != 1) {
1118 if (size != PyArray_DIMS(v)[0]) {
1119 PyErr_Format(PyExc_ValueError,
1120 "NumPy boolean array indexing assignment "
1121 "cannot assign %" NPY_INTP_FMT " input values to "
1122 "the %" NPY_INTP_FMT " output values where the mask is true",
1123 PyArray_DIMS(v)[0], size);
1124 return -1;
1125 }
1126 v_stride = PyArray_STRIDES(v)[0];
1127 }
1128 else {
1129 v_stride = 0;
1130 }
1131
1132 v_data = PyArray_DATA(v);
1133
1134 /* Create an iterator for the data */

Callers 1

array_assign_subscriptFunction · 0.85

Calls 15

PyArray_DESCRFunction · 0.85
PyArray_NDIMFunction · 0.85
count_boolean_truesFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_STRIDESFunction · 0.85
NpyIter_MultiNewFunction · 0.85
NpyIter_DeallocateFunction · 0.85
NpyIter_GetDataPtrArrayFunction · 0.85

Tested by

no test coverage detected