Skip to content

Commit b4edc25

Browse files
timholyKristofferC
authored andcommitted
Convert range type in reduced_index
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. (cherry picked from commit eda4131)
1 parent e0d0bb1 commit b4edc25

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

base/reducedim.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# for reductions that expand 0 dims to 1
66
reduced_index(i::OneTo) = OneTo(1)
7-
reduced_index(i::Union{Slice, IdentityUnitRange}) = first(i):first(i)
7+
reduced_index(i::Union{Slice, IdentityUnitRange}) = oftype(i, first(i):first(i))
88
reduced_index(i::AbstractUnitRange) =
99
throw(ArgumentError(
1010
"""

test/offsetarray.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,14 @@ I = findall(!iszero, z)
440440
@test std(A_3_3, dims=2) == OffsetArray(reshape([3,3,3], (3,1)), A_3_3.offsets)
441441
@test sum(OffsetArray(fill(1,3000), -1000)) == 3000
442442

443+
# https://github.com/JuliaArrays/OffsetArrays.jl/issues/92
444+
A92 = OffsetArray(reshape(1:27, 3, 3, 3), -2, -2, -2)
445+
B92 = view(A92, :, :, -1:0)
446+
@test axes(B92) == (-1:1, -1:1, 1:2)
447+
@test sum(B92, dims=(2,3)) == OffsetArray(reshape([51,57,63], Val(3)), -2, -2, 0)
448+
B92 = view(A92, :, :, Base.IdentityUnitRange(-1:0))
449+
@test sum(B92, dims=(2,3)) == OffsetArray(reshape([51,57,63], Val(3)), -2, -2, -2)
450+
443451
@test norm(v) norm(parent(v))
444452
@test norm(A) norm(parent(A))
445453
@test dot(v, v) dot(v0, v0)

0 commit comments

Comments
 (0)