Skip to content

Commit

Permalink
Fix reductions over dimensions (#108)
Browse files Browse the repository at this point in the history
Fixes #92
Fixes #107
  • Loading branch information
timholy authored Mar 29, 2020
1 parent 61823a9 commit 8e24f51
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ offset_coerce(::Type{I}, r::AbstractUnitRange) where I<:AbstractUnitRange{T} whe
# issue 100: IdOffsetRange as another index-preserving case shouldn't comtribute offsets
@inline Base.compute_offset1(parent, stride1::Integer, dims::Tuple{Int}, inds::Tuple{IdOffsetRange}, I::Tuple) =
Base.compute_linindex(parent, I) - stride1*first(axes(parent, dims[1]))
Base.reduced_index(i::IdOffsetRange) = typeof(i)(first(i):first(i))
# Workaround for #92 on Julia < 1.4
Base.reduced_index(i::IdentityUnitRange{<:IdOffsetRange}) = typeof(i)(first(i):first(i))

@inline function Base.iterate(r::IdOffsetRange)
ret = iterate(r.parent)
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ end
@test maximum(A) == maximum(parent(A))
@test minimum(A) == minimum(parent(A))
@test extrema(A) == extrema(parent(A))
@test sum(A) == sum(parent(A))
@test sum(A, dims=1) == OffsetArray(sum(parent(A), dims=1), A.offsets)
@test sum(A, dims=2) == OffsetArray(sum(parent(A), dims=2), A.offsets)
@test sum(A, dims=(1,2)) == OffsetArray(sum(parent(A), dims=(1,2)), A.offsets)
@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)
C = similar(A)
cumsum!(C, A, dims = 1)
@test parent(C) == cumsum(parent(A), dims = 1)
Expand Down

0 comments on commit 8e24f51

Please sign in to comment.