Skip to content

Fix a few offset indexing bugs #28100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/arrayshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
21 changes: 21 additions & 0 deletions stdlib/LinearAlgebra/test/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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",
Expand Down
12 changes: 11 additions & 1 deletion test/reduce.jl
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -414,4 +416,12 @@ test18695(r) = sum( t^2 for t in r )
@test X == Y
@test typeof(X) === typeof(Y)
end
end
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