8000 Merge pull request #24183 from charris/backport-24150 · numpy/numpy@c9b63e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c9b63e8

Browse files
authored
Merge pull request #24183 from charris/backport-24150
BUG: properly handle negative indexes in ufunc_at fast path
2 parents b12a3ad + 11ea00a commit c9b63e8

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

numpy/core/src/umath/ufunc_object.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5955,14 +5955,19 @@ trivial_at_loop(PyArrayMethodObject *ufuncimpl, NPY_ARRAYMETHOD_FLAGS flags,
59555955
}
59565956
}
59575957

5958-
npy_intp *inner_size = NpyIter_GetInnerLoopSizePtr(iter->outer);
5959-
59605958
if (!(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) {
59615959
npy_clear_floatstatus_barrier((char *)context);
59625960
}
59635961

59645962
do {
5965-
args[1] = (char *) iter->o 8000 uter_ptrs[0];
5963+
npy_intp *inner_size = NpyIter_GetInnerLoopSizePtr(iter->outer);
5964+
npy_intp * indxP = (npy_intp *)iter->outer_ptrs[0];
5965+
for (npy_intp i=0; i < *inner_size; i++) {
5966+
if (indxP[i] < 0) {
5967+
indxP[i] += iter->fancy_dims[0];
5968+
}
5969+
}
5970+
args[1] = (char *)indxP;
59665971
steps[1] = iter->outer_strides[0];
59675972

59685973
res = ufuncimpl->contiguous_indexed_loop(

numpy/core/tests/test_ufunc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,14 @@ def test_ufunc_at_advanced(self):
22152215
np.maximum.at(a, [0], 0)
22162216
assert_equal(a, np.array([1, 2, 3]))
22172217

2218+
def test_at_negative_indexes(self):
2219+
a = np.arange(10)
2220+
indxs = np.array([-1, 1, -1, 2])
2221+
np.add.at(a, indxs, 1)
2222+
assert a[-1] == 11 # issue 24147
2223+
assert a[1] == 2
2224+
assert a[2] == 3
2225+
22182226
def test_at_not_none_signature(self):
22192227
# Test ufuncs with non-trivial signature raise a TypeError
22202228
a = np.ones((2, 2, 2))

0 commit comments

Comments
 (0)
0