From eda4131859229c7d9de4461e154a73bb89a6b783 Mon Sep 17 00:00:00 2001 From: Tim Holy <tim.holy@gmail.com> Date: Sat, 15 Feb 2020 06:20:38 -0600 Subject: [PATCH] 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 https://github.com/JuliaArrays/OffsetArrays.jl/issues/92. I'll put a workaround in OffsetArrays.jl too. --- base/reducedim.jl | 2 +- test/offsetarray.jl | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/base/reducedim.jl b/base/reducedim.jl index a448931499fc5..a66cb09cfe5c5 100644 --- a/base/reducedim.jl +++ b/base/reducedim.jl @@ -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( """ diff --git a/test/offsetarray.jl b/test/offsetarray.jl index bb25424d6855e..e7b4fb9410018 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -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)