Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
enable boolean array indexing
  • Loading branch information
guilhermeleobas committed Nov 14, 2023
commit 2db339ecb2e355587e41defc8e349d0da05cd787
19 changes: 13 additions & 6 deletions numba/np/ufunc/dufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,13 @@ def ol_at(ufunc, a, indices, b=None):
msg = 'The first argument "a" must be array-like'
raise errors.NumbaTypeError(msg)

indices_int_arr = isinstance(indices, (types.Array, types.List)) \
and isinstance(indices.dtype, types.Integer)
indices_arr = isinstance(indices, (types.Array, types.List))
indices_bool_arr = indices_arr and \
isinstance(indices.dtype, types.Boolean)
indices_tuple = isinstance(indices, types.Tuple)
indices_slice = isinstance(indices, types.SliceType)
indices_scalar = not (indices_int_arr or indices_int_arr or
indices_slice or indices_tuple)
indices_empty_tuple = isinstance(indices, types.Tuple) and \
len(indices) == 0
indices_scalar = not (indices_arr or indices_slice or indices_tuple)
indices_empty_tuple = indices_tuple and len(indices) == 0
b_array = isinstance(b, (types.Array, types.Sequence, types.List,
types.Tuple))
b_scalar = not b_array
Expand Down Expand Up @@ -351,6 +350,12 @@ def impl_generic(ufunc, a, indices, b=None):
j += 1
return a

def impl_indices_bool_arr(ufunc, a, indices, b=None):
a_ = a[indices]
idx = np.indices(a_.shape)
a[indices] = ufunc.at(a_, idx, b)
return a

def impl_indices_empty_b_scalar(ufunc, a, indices, b=None):
a[()] = ufunc(a[()], b)
return a
Expand All @@ -366,6 +371,8 @@ def impl_indices_slice(ufunc, a, indices, b=None):
return impl_b_none
elif indices_slice:
return impl_indices_slice
elif indices_bool_arr:
return impl_indices_bool_arr
elif indices_empty_tuple and b_scalar:
return impl_indices_empty_b_scalar
elif indices_scalar and b_scalar:
Expand Down
3 changes: 1 addition & 2 deletions numba/tests/npyufunc/test_dufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,14 @@ def test_ufunc_at_dtypes(self):
power_at(a, [1, 2, 3, 2], 3.5)
self.assertPreciseEqual(a, np.array([0, 1, 4414, 46, 4, 5, 6, 7, 8, 9]))

@unittest.expectedFailure
def test_ufunc_at_boolean(self):
# Test boolean indexing and boolean ufuncs
a = np.arange(10)
index = a % 2 == 0
equal_at = self._generate_jit(np.equal)
# boolean indexing not supported
equal_at(a, index, [0, 2, 4, 6, 8])
self.assertPreciseEqual(a, [1, 1, 1, 3, 1, 5, 1, 7, 1, 9])
self.assertPreciseEqual(a, np.array([1, 1, 1, 3, 1, 5, 1, 7, 1, 9]))

def test_ufunc_at_boolean2(self):
# Test unary operator
Expand Down