Skip to content

Preserve axes in permutedims for AbstractVectors #243

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 4 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,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
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,15 @@ end
@test first(A) == 5
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)
end
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add 2d array tests to make sure the behavior is consistent?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, but those should be unaffected as the method is only added for OffsetVectors


@testset "Indexing with OffsetArray axes" begin
A0 = [1 3; 2 4]

Expand Down