Skip to content

Commit

Permalink
Convert range type in reduced_index
Browse files Browse the repository at this point in the history
The `reduced_indices` and `reduced_indices0` methods sometimes assert
that the return axes type is the same as the input.
Consequently, the implementation of `reduced_index` had better
return a range of the same type as the input.

This corrects the error in
JuliaArrays/OffsetArrays.jl#92.
I'll put a workaround in OffsetArrays.jl too.
  • Loading branch information
timholy committed Feb 15, 2020
1 parent 48f942b commit eda4131
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# for reductions that expand 0 dims to 1
reduced_index(i::OneTo) = OneTo(1)
reduced_index(i::Union{Slice, IdentityUnitRange}) = first(i):first(i)
reduced_index(i::Union{Slice, IdentityUnitRange}) = oftype(i, first(i):first(i))
reduced_index(i::AbstractUnitRange) =
throw(ArgumentError(
"""
Expand Down
8 changes: 8 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,14 @@ I = findall(!iszero, z)
@test std(A_3_3, dims=2) == OffsetArray(reshape([3,3,3], (3,1)), A_3_3.offsets)
@test sum(OffsetArray(fill(1,3000), -1000)) == 3000

# https://github.com/JuliaArrays/OffsetArrays.jl/issues/92
A92 = OffsetArray(reshape(1:27, 3, 3, 3), -2, -2, -2)
B92 = view(A92, :, :, -1:0)
@test axes(B92) == (-1:1, -1:1, 1:2)
@test sum(B92, dims=(2,3)) == OffsetArray(reshape([51,57,63], Val(3)), -2, -2, 0)
B92 = view(A92, :, :, Base.IdentityUnitRange(-1:0))
@test sum(B92, dims=(2,3)) == OffsetArray(reshape([51,57,63], Val(3)), -2, -2, -2)

@test norm(v) norm(parent(v))
@test norm(A) norm(parent(A))
@test dot(v, v) dot(v0, v0)
Expand Down

0 comments on commit eda4131

Please sign in to comment.