8000 Merge pull request #24150 from mattip/issue-24147 · numpy/numpy@c19ce9c · GitHub
[go: up one dir, main page]

Skip to content

Commit c19ce9c

Browse files
authored
Merge pull request #24150 from mattip/issue-24147
BUG: properly handle negative indexes in ufunc_at fast path
2 parents 8b6024e + b774b3a commit c19ce9c

File tree

2 files changed

+16
-3
lines changed
  • numpy/core
    • src/umath
    • < 8000 svg aria-hidden="true" focusable="false" class="octicon octicon-chevron-down" viewBox="0 0 12 12" width="12" height="12" fill="currentColor" display="inline-block" overflow="visible" style="vertical-align:text-bottom">
      tests

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
@@ -5812,14 +5812,19 @@ trivial_at_loop(PyArrayMethodObject *ufuncimpl, NPY_ARRAYMETHOD_FLAGS flags,
58125812
}
58135813
}
58145814

5815-
npy_intp *inner_size = NpyIter_GetInnerLoopSizePtr(iter->outer);
5816-
58175815
if (!(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) {
58185816
npy_clear_floatstatus_barrier((char *)context);
58195817
}
58205818

58215819
do {
5822-
args[1] = (char *) iter->oute 8000 r_ptrs[0];
5820+
npy_intp *inner_size = NpyIter_GetInnerLoopSizePtr(iter->outer);
5821+
npy_intp * indxP = (npy_intp *)iter->outer_ptrs[0];
5822+
for (npy_intp i=0; i < *inner_size; i++) {
5823+
if (indxP[i] < 0) {
5824+
indxP[i] += iter->fancy_dims[0];
5825+
}
5826+
}
5827+
args[1] = (char *)indxP;
58235828
steps[1] = iter->outer_strides[0];
58245829

58255830
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
@@ -2224,6 +2224,14 @@ def test_ufunc_at_advanced(self):
22242224
np.maximum.at(a, [0], 0)
22252225
assert_equal(a, np.array([1, 2, 3]))
22262226

2227+
def test_at_negative_indexes(self):
2228+
a = np.arange(10)
2229+
indxs = np.array([-1, 1, -1, 2])
2230+
np.add.at(a, indxs, 1)
2231+
assert a[-1] == 11 # issue 24147
2232+
assert a[1] == 2
2233+
assert a[2] == 3
2234+
22272235
def test_at_not_none_signature(self):
22282236
# Test ufuncs with non-trivial signature raise a TypeError
22292237
a = np.ones((2, 2, 2))

0 commit comments

Comments
 (0)
0