Skip to content

Commit b6f58ad

Browse files
c42fmateuszbaran
andauthored
Some vcat / hcat overloads for Number + StaticArray (JuliaArrays#768)
These extra overloads should be helpful for users though it's not really possible to fix this in generality until the dispatch mechanism in Base is improved. Co-authored-by: Mateusz Baran <mateuszbaran89@gmail.com>
1 parent b95d07e commit b6f58ad

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/abstractarray.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,15 @@ end
230230

231231
#--------------------------------------------------
232232
# Concatenation
233-
@inline vcat(a::StaticVecOrMatLike) = a
233+
@inline vcat(a::StaticMatrixLike) = a
234234
@inline vcat(a::StaticVecOrMatLike, b::StaticVecOrMatLike) = _vcat(Size(a), Size(b), a, b)
235235
@inline vcat(a::StaticVecOrMatLike, b::StaticVecOrMatLike, c::StaticVecOrMatLike...) = vcat(vcat(a,b), vcat(c...))
236+
# A couple of hacky overloads to avoid some vcat surprises.
237+
# We can't really make this work a lot better in general without Base providing
238+
# a dispatch mechanism for output container type.
239+
@inline vcat(a::StaticVector) = a
240+
@inline vcat(a::StaticVector, bs::Number...) = vcat(a, SVector(bs))
241+
@inline vcat(a::Number, b::StaticVector) = vcat(similar_type(b, typeof(a), Size(1))((a,)), b)
236242

237243
@generated function _vcat(::Size{Sa}, ::Size{Sb}, a::StaticVecOrMatLike, b::StaticVecOrMatLike) where {Sa, Sb}
238244
if Size(Sa)[2] != Size(Sb)[2]
@@ -261,6 +267,9 @@ end
261267
@inline hcat(a::StaticMatrixLike) = a
262268
@inline hcat(a::StaticVecOrMatLike, b::StaticVecOrMatLike) = _hcat(Size(a), Size(b), a, b)
263269
@inline hcat(a::StaticVecOrMatLike, b::StaticVecOrMatLike, c::StaticVecOrMatLike...) = hcat(hcat(a,b), hcat(c...))
270+
@inline hcat(a::StaticMatrix{1}) = a # disambiguation
271+
@inline hcat(a::StaticMatrix{1}, bs::Number...) = hcat(a, SMatrix{1,length(bs)}(bs))
272+
@inline hcat(a::Number, b::StaticMatrix{1}) = hcat(similar_type(b, typeof(a), Size(1))((a,)), b)
264273

265274
@generated function _hcat(::Size{Sa}, ::Size{Sb}, a::StaticVecOrMatLike, b::StaticVecOrMatLike) where {Sa, Sb}
266275
if Sa[1] != Sb[1]

test/abstractarray.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,16 @@ end
260260
let A = Transpose(@SMatrix [1 2; 3 4]), B = Adjoint(@SMatrix [5 6; 7 8])
261261
@test @inferred(vcat(A, B)) === SMatrix{4, 2}([Matrix(A); Matrix(B)])
262262
end
263+
264+
# hcat/vcat + mixtures of Number and SVector / SMatrix
265+
@test @inferred(vcat(SA[1,2,3], 4, 5, 6)) === SVector{6}((1,2,3,4,5,6))
266+
@test @inferred(vcat(0, SA[1,2,3])) === SVector{4}((0,1,2,3))
267+
@test @inferred(hcat(SMatrix{1,3}((1,2,3)), 4, 5, 6)) === SMatrix{1,6}((1,2,3,4,5,6))
268+
@test @inferred(hcat(0, SMatrix{1,3}((1,2,3)))) === SMatrix{1,4}((0,1,2,3))
269+
@test @inferred(vcat(MVector((1,2,3)), 4, 5, 6))::MVector == [1,2,3,4,5,6]
270+
271+
@test @inferred(vcat(SA[1,2,3])) === SA[1,2,3]
272+
@test @inferred(vcat(SA[1 2 3])) === SA[1 2 3]
273+
@test @inferred(hcat(SA[1,2,3])) === SMatrix{3,1}(1,2,3)
274+
@test @inferred(hcat(SA[1 2 3])) === SA[1 2 3]
263275
end

0 commit comments

Comments
 (0)