Skip to content

Commit

Permalink
Fix indexing in _mapreducedim for OffsetArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Aug 16, 2024
1 parent 5230d27 commit bf66e74
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,10 @@ function _mapreducedim!(f, op, R::AbstractArray, A::AbstractArrayOrBroadcasted)
# use mapreduce_impl, which is probably better tuned to achieve higher performance
nslices = div(length(A), lsiz)
ibase = first(LinearIndices(A))-1
for i = 1:nslices
@inbounds R[i] = op(R[i], mapreduce_impl(f, op, A, ibase+1, ibase+lsiz))
for i = range(firstindex(R), length=nslices)
v = mapreduce_impl(f, op, A, ibase+1, ibase+lsiz)
r = op(@inbounds(R[i]), v)
@inbounds R[i] = r
ibase += lsiz
end
return R
Expand Down
7 changes: 7 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -907,3 +907,10 @@ end
v = view([1,2,3,4], :)
@test v[Base.IdentityUnitRange(2:3)] == OffsetArray(2:3, 2:3)
end

@testset "mapreduce with OffsetRanges" begin
r = 5:100
a = OffsetArray(r, 2)
b = sum(a, dims=1)
@test b[begin] == sum(r)
end

0 comments on commit bf66e74

Please sign in to comment.