diff options
author | Mark Wiebe <mwwiebe@gmail.com> | 2010-11-04 22:40:04 -0700 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2010-11-07 11:15:16 -0700 |
commit | 6e5590ff95219c5323f33159034930de2d248448 (patch) | |
tree | 2178a4d172c603771ed8b137ef7c6c11044feb26 | |
parent | 12e936a7f793df5b76b639382c7b302c3153c2b3 (diff) | |
download | numpy-6e5590ff95219c5323f33159034930de2d248448.tar.gz |
BUG: core: nonzero() requires swap for float types
-rw-r--r-- | numpy/core/src/multiarray/arraytypes.c.src | 16 | ||||
-rw-r--r-- | numpy/core/src/multiarray/item_selection.c | 3 | ||||
-rw-r--r-- | numpy/core/tests/test_regression.py | 6 |
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() |