8000 Merge pull request #13632 from eric-wieser/tidy-nonzero · numpy/numpy@40ada70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 40ada70

Browse files
authored
Merge pull request #13632 from eric-wieser/tidy-nonzero
MAINT: Collect together the special-casing of 0d non-zero into one place
2 parents a9d0916 + d3c1c22 commit 40ada70

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

numpy/core/src/multiarray/item_selection.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,21 @@ PyArray_Nonzero(PyArrayObject *self)
22072207
NpyIter_IterNextFunc *iternext;
22082208
NpyIter_GetMultiIndexFunc *get_multi_index;
22092209
char **dataptr;
2210-
int is_empty = 0;
2210+
2211+
/* Special case - nonzero(zero_d) is nonzero(atleast1d(zero_d)) */
2212+
if (ndim == 0) {
2213+
static npy_intp const zero_dim_shape[1] = {1};
2214+
static npy_intp const zero_dim_strides[1] = {0};
2215+
2216+
PyArrayObject *self_1d = (PyArrayObject *)PyArray_NewFromDescrAndBase(
2217+
Py_TYPE(self), PyArray_DESCR(self),
2218+
1, zero_dim_shape, zero_dim_strides, PyArray_BYTES(self),
2219+
PyArray_FLAGS(self), (PyObject *)self, (PyObject *)self);
2220+
if (self_1d == NULL) {
2221+
return NULL;
2222+
}
2223+
return PyArray_Nonzero(self_1d);
2224+
}
22112225

22122226
/*
22132227
* First count the number of non-zeros in 'self'.
@@ -2219,7 +2233,7 @@ PyArray_Nonzero(PyArrayObject *self)
22192233

22202234
/* Allocate the result as a 2D array */
22212235
ret_dims[0] = nonzero_count;
2222-
ret_dims[1] = (ndim == 0) ? 1 : ndim;
2236+
ret_dims[1] = ndim;
22232237
ret = (PyArrayObject *)PyArray_NewFromDescr(
22242238
&PyArray_Type, PyArray_DescrFromType(NPY_INTP),
22252239
2, ret_dims, NULL, NULL,
@@ -2229,11 +2243,11 @@ PyArray_Nonzero(PyArrayObject *self)
22292243
}
22302244

22312245
/* If it's a one-dimensional result, don't use an iterator */
2232-
if (ndim <= 1) {
2246+
if (ndim == 1) {
22332247
npy_intp * multi_index = (npy_intp *)PyArray_DATA(ret);
22342248
char * data = PyArray_BYTES(self);
2235-
npy_intp stride = (ndim == 0) ? 0 : PyArray_STRIDE(self, 0);
2236-
npy_intp count = (ndim == 0) ? 1 : PyArray_DIM(self, 0);
2249+
npy_intp stride = PyArray_STRIDE(self, 0);
2250+
npy_intp count = PyArray_DIM(self, 0);
22372251
NPY_BEGIN_THREADS_DEF;
22382252

22392253
/* nothing to do */
@@ -2351,29 +2365,17 @@ PyArray_Nonzero(PyArrayObject *self)
23512365
NpyIter_Deallocate(iter);
23522366

23532367
finish:
2354-
/* Treat zero-dimensional as shape (1,) */
2355-
if (ndim == 0) {
2356-
ndim = 1;
2357-
}
2358-
23592368
ret_tuple = PyTuple_New(ndim);
23602369
if (ret_tuple == NULL) {
23612370
Py_DECREF(ret);
23622371
return NULL;
23632372
}
23642373

2365-
for (i = 0; i < PyArray_NDIM(ret); ++i) {
2366-
if (PyArray_DIMS(ret)[i] == 0) {
2367-
is_empty = 1;
2368-
break;
2369-
}
2370-
}
2371-
23722374
/* Create views into ret, one for each dimension */
23732375
for (i = 0; i < ndim; ++i) {
23742376
npy_intp stride = ndim * NPY_SIZEOF_INTP;
23752377
/* the result is an empty array, the view must point to valid memory */
2376-
npy_intp data_offset = is_empty ? 0 : i * NPY_SIZEOF_INTP;
2378+
npy_intp data_offset = nonzero_count == 0 ? 0 : i * NPY_SIZEOF_INTP;
23772379

23782380
PyArrayObject *view = (PyArrayObject *)PyArray_NewFromDescrAndBase(
23792381
Py_TYPE(ret), PyArray_DescrFromType(NPY_INTP),

0 commit comments

Comments
 (0)
0