Skip to content

Commit 8e24f51

Browse files
authored
Fix reductions over dimensions (#108)
Fixes #92 Fixes #107
1 parent 61823a9 commit 8e24f51

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/axes.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ offset_coerce(::Type{I}, r::AbstractUnitRange) where I<:AbstractUnitRange{T} whe
132132
# issue 100: IdOffsetRange as another index-preserving case shouldn't comtribute offsets
133133
@inline Base.compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{IdOffsetRange}, I::Tuple) =
134134
Base.compute_linindex(parent, I) - stride1*first(axes(parent, dims[1]))
135+
Base.reduced_index(i::IdOffsetRange) = typeof(i)(first(i):first(i))
136+
# Workaround for #92 on Julia < 1.4
137+
Base.reduced_index(i::IdentityUnitRange{<:IdOffsetRange}) = typeof(i)(first(i):first(i))
135138

136139
@inline function Base.iterate(r::IdOffsetRange)
137140
ret = iterate(r.parent)

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ end
542542
@test maximum(A) == maximum(parent(A))
543543
@test minimum(A) == minimum(parent(A))
544544
@test extrema(A) == extrema(parent(A))
545+
@test sum(A) == sum(parent(A))
546+
@test sum(A, dims=1) == OffsetArray(sum(parent(A), dims=1), A.offsets)
547+
@test sum(A, dims=2) == OffsetArray(sum(parent(A), dims=2), A.offsets)
548+
@test sum(A, dims=(1,2)) == OffsetArray(sum(parent(A), dims=(1,2)), A.offsets)
549+
@test sum(view(OffsetArray(reshape(1:27, 3, 3, 3), 0, 0, 0), :, :, 1:2), dims=(2,3)) == reshape([51,57,63], 3, 1, 1)
545550
C = similar(A)
546551
cumsum!(C, A, dims = 1)
547552
@test parent(C) == cumsum(parent(A), dims = 1)

0 commit comments

Comments
 (0)