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

Function _nonzero_indices

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

* This function takes a Boolean array and constructs index objects and * iterators as if nonzero(Bool) had been called * * Must not be called on a 0-d array. */

Source from the content-addressed store, hash-verified

2163 * Must not be called on a 0-d array.
2164 */
2165static int
2166_nonzero_indices(PyObject *myBool, PyArrayObject **arrays)
2167{
2168 PyArray_Descr *typecode;
2169 PyArrayObject *ba = NULL, *new = NULL;
2170 int nd, j;
2171 npy_intp size, i, count;
2172 npy_bool *ptr;
2173 npy_intp coords[NPY_MAXDIMS], dims_m1[NPY_MAXDIMS];
2174 npy_intp *dptr[NPY_MAXDIMS];
2175 static npy_intp one = 1;
2176 NPY_BEGIN_THREADS_DEF;
2177
2178 typecode=PyArray_DescrFromType(NPY_BOOL);
2179 ba = (PyArrayObject *)PyArray_FromAny(myBool, typecode, 0, 0,
2180 NPY_ARRAY_CARRAY, NULL);
2181 if (ba == NULL) {
2182 return -1;
2183 }
2184 nd = PyArray_NDIM(ba);
2185
2186 for (j = 0; j < nd; j++) {
2187 arrays[j] = NULL;
2188 }
2189 size = PyArray_SIZE(ba);
2190 ptr = (npy_bool *)PyArray_DATA(ba);
2191
2192 /*
2193 * pre-determine how many nonzero entries there are,
2194 * ignore dimensionality of input as its a CARRAY
2195 */
2196 count = count_boolean_trues(1, (char*)ptr, &size, &one);
2197
2198 /* create count-sized index arrays for each dimension */
2199 for (j = 0; j < nd; j++) {
2200 new = (PyArrayObject *)PyArray_NewFromDescr(
2201 &PyArray_Type, PyArray_DescrFromType(NPY_INTP),
2202 1, &count, NULL, NULL,
2203 0, NULL);
2204 if (new == NULL) {
2205 goto fail;
2206 }
2207 arrays[j] = new;
2208
2209 dptr[j] = (npy_intp *)PyArray_DATA(new);
2210 coords[j] = 0;
2211 dims_m1[j] = PyArray_DIMS(ba)[j]-1;
2212 }
2213 if (count == 0) {
2214 goto finish;
2215 }
2216
2217 /*
2218 * Loop through the Boolean array and copy coordinates
2219 * for non-zero entries
2220 */
2221 NPY_BEGIN_THREADS_THRESHOLDED(size);
2222 for (i = 0; i < size; i++) {

Callers 1

prepare_indexFunction · 0.85

Calls 6

PyArray_FromAnyFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_DATAFunction · 0.85
count_boolean_truesFunction · 0.85
PyArray_NewFromDescrFunction · 0.85
PyArray_DIMSFunction · 0.85

Tested by

no test coverage detected