From b1de9e5cbe69c777b72ff52afdcc915487d9ab9e Mon Sep 17 00:00:00 2001 From: Sacha Verweij Date: Sat, 10 Jun 2017 14:18:17 -0700 Subject: [PATCH] Make vcat of SparseVectors with different el- and/or ind-type yield SparseVector (#22225). (#22301) --- base/sparse/sparsevector.jl | 5 +++++ test/sparse/sparsevector.jl | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 65372a911e20a..0a540f21d4252 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -876,6 +876,11 @@ end # of _absspvec_vcat below. The <:Integer qualifications are necessary for correct dispatch. vcat(X::SparseVector{Tv,Ti}...) where {Tv,Ti<:Integer} = _absspvec_vcat(X...) vcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti<:Integer} = _absspvec_vcat(X...) +function vcat(X::SparseVector...) + commeltype = promote_type(map(eltype, X)...) + commindtype = promote_type(map(indtype, X)...) + vcat(map(x -> SparseVector{commeltype,commindtype}(x), X)...) +end function _absspvec_vcat(X::AbstractSparseVector{Tv,Ti}...) where {Tv,Ti} # check sizes n = length(X) diff --git a/test/sparse/sparsevector.jl b/test/sparse/sparsevector.jl index 77024341a19ba..4ea50dc01a367 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -474,7 +474,12 @@ let N = 4 @test issparse(cat((1,2), densemat, diagmat, spmat, densevec, spvec)) @test issparse(cat((1,2), spvec, diagmat, densevec, spmat, densemat)) end - +@testset "vertical concatenation of SparseVectors with different el- and ind-type (#22225)" begin + spv6464 = SparseVector(0, Int64[], Int64[]) + @test isa(vcat(spv6464, SparseVector(0, Int64[], Int32[])), SparseVector{Int64,Int64}) + @test isa(vcat(spv6464, SparseVector(0, Int32[], Int64[])), SparseVector{Int64,Int64}) + @test isa(vcat(spv6464, SparseVector(0, Int32[], Int32[])), SparseVector{Int64,Int64}) +end ## sparsemat: combinations with sparse matrix