Skip to content
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

Preserve axes in permutedims for AbstractVectors #243

Merged
merged 4 commits into from
May 30, 2021
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
7 changes: 6 additions & 1 deletion src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ _similar_axes_or_length(AT, ax::I, ::I) where {I} = similar(AT, map(_indexlength
Base.reshape(A::AbstractArray, inds::OffsetAxis...) = reshape(A, inds)
function Base.reshape(A::AbstractArray, inds::Tuple{OffsetAxis,Vararg{OffsetAxis}})
AR = reshape(A, map(_indexlength, inds))
return OffsetArray(AR, map(_offset, axes(AR), inds))
O = OffsetArray(AR, map(_offset, axes(AR), inds))
return _popreshape(O, axes(AR), _filterreshapeinds(inds))
end

# Reshaping OffsetArrays can "pop" the original OffsetArray wrapper and return
Expand All @@ -364,6 +365,10 @@ Base.reshape(A::OffsetVector, ::Colon) = A
Base.reshape(A::OffsetArray, inds::Union{Int,Colon}...) = reshape(parent(A), inds)
Base.reshape(A::OffsetArray, inds::Tuple{Vararg{Union{Int,Colon}}}) = reshape(parent(A), inds)

# permutedims in Base does not preserve axes, and can not be fixed in a non-breaking way
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you explain it a bit why this can't be fixed in a non-breaking way? The following version looks good to me.

julia> using OffsetArrays

julia> function Base.permutedims(v::AbstractVector)
           out = similar(v, (1, axes(v, 1)))
           copyto!(out, v)
       end

julia> permutedims(rand(4))
1×4 Matrix{Float64}:
 0.062106  0.864693  0.0235159  0.929236

julia> x = OffsetArray(rand(4,), -1);

julia> permutedims(x)
1×4 OffsetArray(::Matrix{Float64}, 1:1, 0:3) with eltype Float64 with indices 1:1×0:3:
 0.449456  0.0518755  0.689773  0.00949295

Copy link
Member Author

@jishnub jishnub May 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring for permutedims states that it must be a reshape for AbstractVectors (that is the underlying data is shared), however reshape does not currently accept custom axis types as arguments.

Although I might be misinterpreting what the docstring tries to say. Perhaps it uses reshape loosely.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JuliaLang/julia#41003 is a good but controversial call; I don't think there will be a generic fix until #87 (comment) is solved; there will be many similar issues as identified by @mcabbott.

This specialization itself makes sense without being involved in type piracy so there's no objection in adding this to OffsetArrays.

# This is a stopgap solution
Base.permutedims(v::OffsetVector) = reshape(v, (1, axes(v, 1)))

Base.fill(v, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {N} =
fill!(similar(Array{typeof(v)}, inds), v)
Base.zeros(::Type{T}, inds::NTuple{N, Union{Integer, AbstractUnitRange}}) where {T, N} =
Expand Down
10 changes: 9 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _indexoffset(r::AbstractRange) = first(r) - 1
_indexoffset(i::Integer) = 0
_indexoffset(i::Colon) = 0
_indexlength(r::AbstractRange) = length(r)
_indexlength(i::Integer) = i
_indexlength(i::Integer) = Int(i)
_indexlength(i::Colon) = Colon()

_strip_IdOffsetRange(r::IdOffsetRange) = parent(r)
Expand Down Expand Up @@ -102,3 +102,11 @@ end

_of_eltype(::Type{T}, M::AbstractArray{T}) where {T} = M
_of_eltype(T, M::AbstractArray) = map(T, M)

# filter the arguments to reshape to check if there are any ranges
# If not, we may pop the parent array
_filterreshapeinds(t::Tuple{AbstractUnitRange, Vararg{Any}}) = t
_filterreshapeinds(t::Tuple) = _filterreshapeinds(tail(t))
_filterreshapeinds(t::Tuple{}) = t
_popreshape(A::AbstractArray, ax::Tuple{Vararg{Base.OneTo}}, inds::Tuple{}) = no_offset_view(A)
_popreshape(A::AbstractArray, ax, inds) = A
33 changes: 33 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1767,6 +1767,39 @@ end
Arsc = reshape(A, :, 1)
Arsc[1,1] = 5
@test first(A) == 5

@testset "issue #235" begin
Vec64 = zeros(6)
ind_a_64 = 3
ind_a_32 =Int32.(ind_a_64)
@test reshape(Vec64, ind_a_32, :) == reshape(Vec64, ind_a_64, :)
end

R = reshape(zeros(6), 2, :)
@test R isa Matrix
@test axes(R) == (1:2, 1:3)
R = reshape(zeros(6,1), 2, :)
@test R isa Matrix
@test axes(R) == (1:2, 1:3)

R = reshape(zeros(6), 1:2, :)
@test axes(R) == (1:2, 1:3)
R = reshape(zeros(6,1), 1:2, :)
@test axes(R) == (1:2, 1:3)
end

@testset "permutedims" begin
a = OffsetArray(1:2, 2:3)
@test permutedims(a) == reshape(1:2, 1, 2:3)
a = OffsetArray([10,11], Base.OneTo(2))
@test permutedims(a) == reshape(10:11, 1, 1:2)
a = OffsetArray(SVector{2}(1,2), 3:4)
@test permutedims(a) == reshape(1:2, 1, 3:4)

# check that the 2D case is unaffected
a = OffsetArray(reshape(1:2, 1, 2), 2:2, 4:5)
b = permutedims(a)
@test a[2,:] == b[:,2]
end

@testset "Indexing with OffsetArray axes" begin
Expand Down