Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

BUG: Fix complex to bool conversion in lowlevel_strided_loops #477

Merged
merged 2 commits into from

3 participants

@seberg
Owner

This is a fix for Ticket 2218. I am not sure if this is the best way to fix it, but I hunted it down to that place and just implemented the first fix that seemed reasonable.

On a side note, the arraytypes.c.src code seems to handle complex to bool conversion fine, but when does it get called at all?

@charris charris commented on the diff
numpy/core/tests/test_regression.py
@@ -1459,6 +1459,14 @@ def test_complex_scalar_complex_cast(self):
x = tp(1+2j)
assert_equal(complex(x), 1+2j)
+ def test_complex_boolean_cast(self):
@charris Owner
charris added a note

Might as well test the astype method too.

@charris Owner
charris added a note

And maybe logical_or.reduce and logical_and.reduce as well.

In [1]: a = ones(3)*1j

In [2]: logical_or.reduce(a)
Out[2]: True

In [3]: logical_and.reduce(a)
Out[3]: True

You could maybe put those tests in numpy/core/test_ufunc. If you want to go for completeness, you could do it for all numeric types. The easy way to do that is to use the typecodes in np.typecodes

In [4]: np.typecodes
Out[4]: 
{'All': '?bhilqpBHILQPefdgFDGSUVOMm',
 'AllFloat': 'efdgFDG',
 'AllInteger': 'bBhHiIlLqQpP',
 'Character': 'c',
 'Complex': 'FDG',
 'Datetime': 'Mm',
 'Float': 'efdg',
 'Integer': 'bhilqp',
 'UnsignedInteger': 'BHILQP'}
@charris Owner
charris added a note

And if you do that and are working in the test directory you can run those tests by doing

charris@f16 [tests (pull-477)]$ python test_ufunc.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@charris
Owner

This looks good and works for me. If you make the tests more thorough that would be helpful in building up the numpy test coverage.

@charris
Owner

I'm going to put this in.. If you put together another PR with more extensive tests, I'll be glad to put that in also.

@certik I think this should go into 1.7.

@charris
Owner

I don't see a separate compilation problem on my machine.

@charris charris merged commit 3f10c36 into numpy:master
@certik
Owner

@charris, I just sent the PR #2741 backporting this.

@seberg seberg deleted the seberg:ticket2218 branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
12 numpy/core/src/multiarray/lowlevel_strided_loops.c.src
@@ -811,9 +811,17 @@ static void
dst_value[0] = _CONVERT_FN(src_value[0]);
dst_value[1] = _CONVERT_FN(src_value[1]);
# elif !@aligned@
- dst_value = _CONVERT_FN(src_value[0]);
+# if @is_bool2@
+ dst_value = _CONVERT_FN(src_value[0]) || _CONVERT_FN(src_value[1]);
+# else
+ dst_value = _CONVERT_FN(src_value[0]);
+# endif
# else
- *(_TYPE2 *)dst = _CONVERT_FN(src_value[0]);
+# if @is_bool2@
+ *(_TYPE2 *)dst = _CONVERT_FN(src_value[0]) || _CONVERT_FN(src_value[1]);
+# else
+ *(_TYPE2 *)dst = _CONVERT_FN(src_value[0]);
+# endif
# endif
#else
# if @is_complex2@
View
8 numpy/core/tests/test_regression.py
@@ -1459,6 +1459,14 @@ def test_complex_scalar_complex_cast(self):
x = tp(1+2j)
assert_equal(complex(x), 1+2j)
+ def test_complex_boolean_cast(self):
@charris Owner
charris added a note

Might as well test the astype method too.

@charris Owner
charris added a note

And maybe logical_or.reduce and logical_and.reduce as well.

In [1]: a = ones(3)*1j

In [2]: logical_or.reduce(a)
Out[2]: True

In [3]: logical_and.reduce(a)
Out[3]: True

You could maybe put those tests in numpy/core/test_ufunc. If you want to go for completeness, you could do it for all numeric types. The easy way to do that is to use the typecodes in np.typecodes

In [4]: np.typecodes
Out[4]: 
{'All': '?bhilqpBHILQPefdgFDGSUVOMm',
 'AllFloat': 'efdgFDG',
 'AllInteger': 'bBhHiIlLqQpP',
 'Character': 'c',
 'Complex': 'FDG',
 'Datetime': 'Mm',
 'Float': 'efdg',
 'Integer': 'bhilqp',
 'UnsignedInteger': 'BHILQP'}
@charris Owner
charris added a note

And if you do that and are working in the test directory you can run those tests by doing

charris@f16 [tests (pull-477)]$ python test_ufunc.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ """Ticket #2218"""
+ for tp in [np.csingle, np.cdouble, np.clongdouble]:
+ x = np.array([0, 0+0.5j, 0.5+0j], dtype=tp)
+ assert_equal(x.astype(bool), np.array([0, 1, 1], dtype=bool))
+ assert_(np.any(x))
+ assert_(np.all(x[1:]))
+
def test_uint_int_conversion(self):
x = 2**64 - 1
assert_equal(int(np.uint64(x)), x)
Something went wrong with that request. Please try again.