Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion numba/np/arrayobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -5545,6 +5545,11 @@ def _take_along_axis_impl(
# With addition of pre-broadcasting:
# https://github.com/numpy/numpy/issues/19704

# Wrap axis, it's used in tuple_setitem so must be (axis >= 0) to ensure
# the GEP is in bounds.
if axis < 0:
axis = arr.ndim + axis

# Broadcast the two arrays to matching shapes:
arr_shape = list(arr.shape)
arr_shape[axis] = 1
Expand Down Expand Up @@ -5620,7 +5625,7 @@ def take_along_axis_impl(arr, indices, axis):
check_is_integer(axis, "axis")
if not isinstance(axis, types.IntegerLiteral):
raise ValueError(
"axis must be a literal value (i.e. not an argument) for now"
"axis must be a literal value (i.e. not an argument)"
)
axis = axis.literal_value
if axis < 0:
Expand Down
19 changes: 12 additions & 7 deletions numba/tests/test_np_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4461,14 +4461,19 @@ def test_take_along_axis_broadcasting(self):
arr = np.ones((3, 4, 1))
ai = np.ones((1, 2, 5), dtype=np.intp)

@njit
def check(a, i):
return np.take_along_axis(a, i, axis=1)
def gen(axis):
@njit
def impl(a, i):
return np.take_along_axis(a, i, axis)
return impl

expected = check.py_func(arr, ai)
actual = check(arr, ai)
self.assertPreciseEqual(expected, actual)
self.assertEqual(actual.shape, (3, 2, 5))
# Check same axis but expressed as positive/negative value
for i in (1, -2):
check = gen(i)
expected = check.py_func(arr, ai)
actual = check(arr, ai)
self.assertPreciseEqual(expected, actual)
self.assertEqual(actual.shape, (3, 2, 5))

def test_take_along_axis_exceptions(self):
arr2d = np.arange(8).reshape(2, 4)
Expand Down