From e80e4f0f32459355491bdd4da6affb4402191416 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 12 Jul 2018 21:41:34 -0500 Subject: [PATCH 1/3] Fix reduction of small arrays with offset axes --- base/reduce.jl | 4 ++-- test/reduce.jl | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/base/reduce.jl b/base/reduce.jl index f56fd341fc61b..caeadc55b4017 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -302,10 +302,10 @@ function _mapreduce(f, op, ::IndexLinear, A::AbstractArray{T}) where T if n == 0 return mapreduce_empty(f, op, T) elseif n == 1 - @inbounds a1 = A[inds[1]] + @inbounds a1 = A[first(inds)] return mapreduce_first(f, op, a1) elseif n < 16 # process short array here, avoid mapreduce_impl() compilation - @inbounds i = inds[1] + @inbounds i = first(inds) @inbounds a1 = A[i] @inbounds a2 = A[i+=1] s = op(f(a1), f(a2)) diff --git a/test/reduce.jl b/test/reduce.jl index 33a238c1c6e43..1632d7a72568a 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -1,6 +1,8 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license using Random +isdefined(Main, :TestHelpers) || @eval Main include(joinpath(dirname(@__FILE__), "TestHelpers.jl")) +using .Main.TestHelpers.OAs # fold(l|r) & mapfold(l|r) @test foldl(+, Int64[]) === Int64(0) # In reference to issues #7465/#20144 (PR #20160) @@ -414,4 +416,12 @@ test18695(r) = sum( t^2 for t in r ) @test X == Y @test typeof(X) === typeof(Y) end -end \ No newline at end of file +end + +# offset axes +i = Base.Slice(-3:3) +x = [j^2 for j in i] +@test sum(x) == sum(x.parent) == 28 +i = Base.Slice(0:0) +x = [j+7 for j in i] +@test sum(x) == 7 From 5b9fcfefeafcb96ed65af4db5bd4b7ca864ea5f8 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 12 Jul 2018 22:46:27 -0500 Subject: [PATCH 2/3] Fix indexing of r' where r is an identity-range (Base.Slice) --- stdlib/LinearAlgebra/src/adjtrans.jl | 4 ++-- stdlib/LinearAlgebra/test/adjtrans.jl | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/stdlib/LinearAlgebra/src/adjtrans.jl b/stdlib/LinearAlgebra/src/adjtrans.jl index c5ec51132e9c2..f75c42117ec8e 100644 --- a/stdlib/LinearAlgebra/src/adjtrans.jl +++ b/stdlib/LinearAlgebra/src/adjtrans.jl @@ -126,9 +126,9 @@ axes(v::AdjOrTransAbsVec) = (Base.OneTo(1), axes(v.parent)...) axes(A::AdjOrTransAbsMat) = reverse(axes(A.parent)) IndexStyle(::Type{<:AdjOrTransAbsVec}) = IndexLinear() IndexStyle(::Type{<:AdjOrTransAbsMat}) = IndexCartesian() -@propagate_inbounds getindex(v::AdjOrTransAbsVec, i::Int) = wrapperop(v)(v.parent[i]) +@propagate_inbounds getindex(v::AdjOrTransAbsVec, i::Int) = wrapperop(v)(v.parent[i-1+first(axes(v.parent)[1])]) @propagate_inbounds getindex(A::AdjOrTransAbsMat, i::Int, j::Int) = wrapperop(A)(A.parent[j, i]) -@propagate_inbounds setindex!(v::AdjOrTransAbsVec, x, i::Int) = (setindex!(v.parent, wrapperop(v)(x), i); v) +@propagate_inbounds setindex!(v::AdjOrTransAbsVec, x, i::Int) = (setindex!(v.parent, wrapperop(v)(x), i-1+first(axes(v.parent)[1])); v) @propagate_inbounds setindex!(A::AdjOrTransAbsMat, x, i::Int, j::Int) = (setindex!(A.parent, wrapperop(A)(x), j, i); A) # AbstractArray interface, additional definitions to retain wrapper over vectors where appropriate @propagate_inbounds getindex(v::AdjOrTransAbsVec, ::Colon, is::AbstractArray{Int}) = wrapperop(v)(v.parent[is]) diff --git a/stdlib/LinearAlgebra/test/adjtrans.jl b/stdlib/LinearAlgebra/test/adjtrans.jl index ae140d5f7a8c8..f6ecdcdb55c02 100644 --- a/stdlib/LinearAlgebra/test/adjtrans.jl +++ b/stdlib/LinearAlgebra/test/adjtrans.jl @@ -481,4 +481,25 @@ end "$t of "*sprint((io, t) -> show(io, MIME"text/plain"(), t), parent(Fop)) end +const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test") +isdefined(Main, :TestHelpers) || @eval Main include(joinpath($(BASE_TEST_PATH), "TestHelpers.jl")) +using .Main.TestHelpers.OAs + +@testset "offset axes" begin + s = Base.Slice(-3:3)' + @test axes(s) === (Base.OneTo(1), Base.Slice(-3:3)) + @test collect(LinearIndices(s)) == reshape(1:7, 1, 7) + @test collect(CartesianIndices(s)) == reshape([CartesianIndex(1,i) for i = -3:3], 1, 7) + @test s[1] == -3 + @test s[7] == 3 + @test s[4] == 0 + @test_throws BoundsError s[0] + @test_throws BoundsError s[8] + @test s[1,-3] == -3 + @test s[1, 3] == 3 + @test s[1, 0] == 0 + @test_throws BoundsError s[1,-4] + @test_throws BoundsError s[1, 4] +end + end # module TestAdjointTranspose From 1a53b11f3e9840a4c4f3db520bd200c199bb7b42 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Fri, 13 Jul 2018 08:01:32 -0500 Subject: [PATCH 3/3] Fix an offset array display bug --- base/arrayshow.jl | 2 +- test/offsetarray.jl | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/base/arrayshow.jl b/base/arrayshow.jl index 39b582150c1d4..d78d53ccb9be9 100644 --- a/base/arrayshow.jl +++ b/base/arrayshow.jl @@ -167,7 +167,7 @@ function print_matrix(io::IO, X::AbstractVecOrMat, postsp = "" @assert textwidth(hdots) == textwidth(ddots) sepsize = length(sep) - rowsA, colsA = axes(X,1), axes(X,2) + rowsA, colsA = UnitRange(axes(X,1)), UnitRange(axes(X,2)) m, n = length(rowsA), length(colsA) # To figure out alignments, only need to look at as many rows as could # fit down screen. If screen has at least as many rows as A, look at A. diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 2f370007d21ab..33f56b8b53015 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -190,8 +190,8 @@ show(io, parent(v)) smry = summary(v) @test occursin("OffsetArray{Float64,1", smry) @test occursin("with indices -1:1", smry) -function cmp_showf(printfunc, io, A) - ioc = IOContext(io, :limit => true, :compact => true) +function cmp_showf(printfunc, io, A; options = ()) + ioc = IOContext(io, :limit => true, :compact => true, options...) printfunc(ioc, A) str1 = String(take!(io)) printfunc(ioc, parent(A)) @@ -202,7 +202,7 @@ cmp_showf(Base.print_matrix, io, OffsetArray(rand(5,5), (10,-9))) # rows&c cmp_showf(Base.print_matrix, io, OffsetArray(rand(10^3,5), (10,-9))) # columns fit cmp_showf(Base.print_matrix, io, OffsetArray(rand(5,10^3), (10,-9))) # rows fit cmp_showf(Base.print_matrix, io, OffsetArray(rand(10^3,10^3), (10,-9))) # neither fits -cmp_showf(Base.show, io, OffsetArray(rand(1,1,10^3,1), (1,2,3,4))) # issue in #24393 +cmp_showf(Base.print_matrix, io, OffsetArray(reshape(range(-0.212121212121, stop=2/11, length=3*29), 3, 29), (-2, -15)); options=(:displaysize=>(53,210),)) targets1 = ["0-dimensional $OAs_name.OffsetArray{Float64,0,Array{Float64,0}}:\n1.0", "$OAs_name.OffsetArray{Float64,1,Array{Float64,1}} with indices 2:2:\n 1.0", "$OAs_name.OffsetArray{Float64,2,Array{Float64,2}} with indices 2:2×3:3:\n 1.0",