summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--numpy/core/src/multiarray/arraytypes.c.src16
-rw-r--r--numpy/core/src/multiarray/item_selection.c3
-rw-r--r--numpy/core/tests/test_regression.py6
3 files changed, 18 insertions, 7 deletions
diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src
index a05289fb0..fb48704ba 100644
--- a/numpy/core/src/multiarray/arraytypes.c.src
+++ b/numpy/core/src/multiarray/arraytypes.c.src
@@ -2231,6 +2231,7 @@ UNICODE_copyswap (char *dst, char *src, int swap, PyArrayObject *arr)
* #type = Bool, byte, ubyte, short, ushort, int, uint, long, ulong,
* longlong, ulonglong, float, double, longdouble,
* datetime, timedelta#
+ * #isfloat = 0*11, 1*3, 0*2#
*/
static Bool
@fname@_nonzero (char *ip, PyArrayObject *ap)
@@ -2241,11 +2242,16 @@ static Bool
}
else {
/*
- * don't worry about swap, since we are just testing
- * whether or not equal to 0
+ * Don't worry about swapping for integer types,
+ * since we are just testing for equality with 0.
+ * For float types, the signed zeros require us to swap.
*/
@type@ tmp;
+#if @isfloat@
+ ap->descr->f->copyswap(&tmp, ip, !PyArray_ISNOTSWAPPED(ap), ap);
+#else
memcpy(&tmp, ip, sizeof(@type@));
+#endif
return (Bool) (tmp != 0);
}
}
@@ -2264,12 +2270,8 @@ static Bool
return (Bool) ((ptmp->real != 0) || (ptmp->imag != 0));
}
else {
- /*
- * don't worry about swap, since we are just testing
- * whether or not equal to 0
- */
@type@ tmp;
- memcpy(&tmp, ip, sizeof(@type@));
+ ap->descr->f->copyswap(&tmp, ip, !PyArray_ISNOTSWAPPED(ap), ap);
return (Bool) ((tmp.real != 0) || (tmp.imag != 0));
}
}
diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c
index 224e11e3b..660c6d9fd 100644
--- a/numpy/core/src/multiarray/item_selection.c
+++ b/numpy/core/src/multiarray/item_selection.c
@@ -1697,6 +1697,7 @@ PyArray_Nonzero(PyArrayObject *self)
if (it == NULL) {
return NULL;
}
+ /* One pass through 'self', counting the non-zero elements */
size = it->size;
for (i = 0; i < size; i++) {
if (self->descr->f->nonzero(it->dataptr, self)) {
@@ -1706,6 +1707,7 @@ PyArray_Nonzero(PyArrayObject *self)
}
PyArray_ITER_RESET(it);
+ /* Allocate the tuple of coordinates */
ret = PyTuple_New(n);
if (ret == NULL) {
goto fail;
@@ -1720,6 +1722,7 @@ PyArray_Nonzero(PyArrayObject *self)
PyTuple_SET_ITEM(ret, j, item);
dptr[j] = (intp *)PyArray_DATA(item);
}
+ /* A second pass through 'self', recording the indices */
if (n == 1) {
for (i = 0; i < size; i++) {
if (self->descr->f->nonzero(it->dataptr, self)) {
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index 3a013f351..06055b2cd 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -1434,6 +1434,12 @@ class TestRegression(TestCase):
assert_array_equal(a1 == a2, [True, False])
assert_array_equal(a2 == a1, [True, False])
+ def test_nonzero_byteswap(self):
+ a = np.array([0x80000000, 0x00000080, 0], dtype=np.uint32)
+ a.dtype = np.float32
+ assert_equal(a.nonzero()[0], [1])
+ a = a.byteswap().newbyteorder()
+ assert_equal(a.nonzero()[0], [1]) # [0] if nonzero() ignores swap
if __name__ == "__main__":
run_module_suite()