Skip to content

specialize axes(S, dim) to return SOneTo #916

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Base.axes(s::StaticArray) = _axes(Size(s))
@pure function _axes(::Size{sizes}) where {sizes}
map(SOneTo, sizes)
end
Base.axes(s::StaticArray, d) = d <= ndims(s) ? _axes(Size(s))[d] : SOneTo{1}()
Base.axes(rv::Adjoint{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)
Base.axes(rv::Transpose{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)

Expand Down
12 changes: 8 additions & 4 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ using StaticArrays, Test, LinearAlgebra
msr = reshape(ms, SOneTo(4))
msr[2] = 10
@test ms == SA[1 2; 10 4]

s = SA[1,2];
s2 = @inferred reshape(s, axes(s,1), axes(s,2))
@test s2 isa StaticArray
end

@testset "copy" begin
Expand Down Expand Up @@ -200,7 +204,7 @@ using StaticArrays, Test, LinearAlgebra
@test @inferred(convert(AbstractArray{Float64}, diag)) isa Diagonal{Float64,SVector{2,Float64}}
@test convert(AbstractArray{Float64}, diag) == diag
# The following cases currently convert the SMatrix into an MMatrix, because
# the constructor in Base invokes `similar`, rather than `convert`, on the static
# the constructor in Base invokes `similar`, rather than `convert`, on the static
# array. This was fixed in https://github.com/JuliaLang/julia/pull/40831; so should
# work from Julia v1.8.0-DEV.55
trans = Transpose(SVector(1,2))
Expand Down Expand Up @@ -297,7 +301,7 @@ end
@test Base.rest(x) == x
a, b... = x
@test b == SA[2, 3]

x = SA[1 2; 3 4]
@test Base.rest(x) == vec(x)
a, b... = x
Expand All @@ -306,14 +310,14 @@ end
a, b... = SA[1]
@test b == []
@test b isa SVector{0}

for (Vec, Mat) in [(MVector, MMatrix), (SizedVector, SizedMatrix)]
x = Vec(1, 2, 3)
@test Base.rest(x) == x
@test pointer(Base.rest(x)) != pointer(x)
a, b... = x
@test b == Vec(2, 3)

x = Mat{2,2}(1, 2, 3, 4)
@test Base.rest(x) == vec(x)
@test pointer(Base.rest(x)) != pointer(x)
Expand Down