From 9b3700efc316b7cc735c5213e516bb77ffcdc05f Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Thu, 8 Sep 2022 10:52:10 +0200 Subject: [PATCH] Fix `vcat` of sparse vectors with numbers --- src/sparsevector.jl | 3 ++- test/sparsevector.jl | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/sparsevector.jl b/src/sparsevector.jl index a9568455..d412ab97 100644 --- a/src/sparsevector.jl +++ b/src/sparsevector.jl @@ -1144,7 +1144,8 @@ const _SparseConcatGroup = Union{_DenseConcatGroup, _SparseConcatArrays, _Annota # Concatenations involving un/annotated sparse/special matrices/vectors should yield sparse arrays _makesparse(x::Number) = x -_makesparse(x::AbstractArray) = SparseMatrixCSC(issparse(x) ? x : sparse(x)) +_makesparse(x::AbstractVector) = convert(SparseVector, issparse(x) ? x : sparse(x))::SparseVector +_makesparse(x::AbstractMatrix) = convert(SparseMatrixCSC, issparse(x) ? x : sparse(x))::SparseMatrixCSC # `@constprop :aggressive` allows `dims` to be propagated as constant improving return type inference Base.@constprop :aggressive function Base._cat(dims, Xin::_SparseConcatGroup...) diff --git a/test/sparsevector.jl b/test/sparsevector.jl index 76d7446b..408e6bc2 100644 --- a/test/sparsevector.jl +++ b/test/sparsevector.jl @@ -544,6 +544,10 @@ end @test length(V) == m * n Vr = vec(Hr) @test Array(V) == Vr + Vnum = vcat(zero(eltype(A)), A...) + @test Vnum isa SparseVector{Float64,Int} + @test length(Vnum) == m*n + 1 + @test Array(Vnum) == [0; Vr] end @testset "concatenation of sparse vectors with other types" begin