Skip to content

Commit 542bbf8

Browse files
committed
specialize axes(S, dim) to return SOneTo
1 parent 920ff58 commit 542bbf8

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/abstractarray.jl

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Base.axes(s::StaticArray) = _axes(Size(s))
1212
@pure function _axes(::Size{sizes}) where {sizes}
1313
map(SOneTo, sizes)
1414
end
15+
Base.axes(s::StaticArray, d) = d <= ndims(s) ? _axes(Size(s))[d] : SOneTo{1}()
1516
Base.axes(rv::Adjoint{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)
1617
Base.axes(rv::Transpose{<:Any,<:StaticVector}) = (SOneTo(1), axes(rv.parent)...)
1718

test/abstractarray.jl

+8-4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ using StaticArrays, Test, LinearAlgebra
134134
msr = reshape(ms, SOneTo(4))
135135
msr[2] = 10
136136
@test ms == SA[1 2; 10 4]
137+
138+
s = SA[1,2];
139+
s2 = @inferred reshape(s, axes(s,1), axes(s,2))
140+
@test s2 isa StaticArray
137141
end
138142

139143
@testset "copy" begin
@@ -200,7 +204,7 @@ using StaticArrays, Test, LinearAlgebra
200204
@test @inferred(convert(AbstractArray{Float64}, diag)) isa Diagonal{Float64,SVector{2,Float64}}
201205
@test convert(AbstractArray{Float64}, diag) == diag
202206
# The following cases currently convert the SMatrix into an MMatrix, because
203-
# the constructor in Base invokes `similar`, rather than `convert`, on the static
207+
# the constructor in Base invokes `similar`, rather than `convert`, on the static
204208
# array. This was fixed in https://github.com/JuliaLang/julia/pull/40831; so should
205209
# work from Julia v1.8.0-DEV.55
206210
trans = Transpose(SVector(1,2))
@@ -297,7 +301,7 @@ end
297301
@test Base.rest(x) == x
298302
a, b... = x
299303
@test b == SA[2, 3]
300-
304+
301305
x = SA[1 2; 3 4]
302306
@test Base.rest(x) == vec(x)
303307
a, b... = x
@@ -306,14 +310,14 @@ end
306310
a, b... = SA[1]
307311
@test b == []
308312
@test b isa SVector{0}
309-
313+
310314
for (Vec, Mat) in [(MVector, MMatrix), (SizedVector, SizedMatrix)]
311315
x = Vec(1, 2, 3)
312316
@test Base.rest(x) == x
313317
@test pointer(Base.rest(x)) != pointer(x)
314318
a, b... = x
315319
@test b == Vec(2, 3)
316-
320+
317321
x = Mat{2,2}(1, 2, 3, 4)
318322
@test Base.rest(x) == vec(x)
319323
@test pointer(Base.rest(x)) != pointer(x)

0 commit comments

Comments
 (0)