diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 4cd9509c1367e..203d4d3d42085 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -703,7 +703,9 @@ complex(x::AbstractSparseVector) = ### Concatenation -function hcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...) +promote_indtype{Tv,Ti}(X::SparseVector{Tv,Ti}, Xs::SparseVector...) = promote_type(Ti, promote_indtype(Xs...)) + +function typed_hcat{Tv,Ti<:Integer}(::Type{SparseMatrixCSC{Tv,Ti}}, X::AbstractSparseVector...) # check sizes n = length(X) m = length(X[1]) @@ -731,8 +733,15 @@ function hcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...) colptr[n+1] = roff SparseMatrixCSC{Tv,Ti}(m, n, colptr, nzrow, nzval) end +typed_hcat(::Type{SparseMatrixCSC}, X::AbstractSparseVector...) = + typed_hcat(SparseMatrixCSC{promote_eltype(X...)}, X...) +typed_hcat{Tv}(::Type{SparseMatrixCSC{Tv}}, X::AbstractSparseVector...) = + typed_hcat(SparseMatrixCSC{Tv,promote_indtype(X...)}, X...) +typed_hcat{T<:SparseMatrixCSC}(::Type{T}, Xin::AbstractVector...) = + typed_hcat(T, map(sparse, Xin)...) -function vcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...) +typed_vcat{T<:SparseVector}(::Type{T}) = T(0) +function typed_vcat{Tv,Ti<:Integer}(::Type{SparseVector{Tv,Ti}}, X::AbstractSparseVector...) # check sizes n = length(X) tnnz = 0 @@ -759,11 +768,18 @@ function vcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...) end SparseVector(len, rnzind, rnzval) end +typed_vcat(::Type{SparseVector}, X::AbstractSparseVector...) = + typed_vcat(SparseVector{promote_eltype(X...)}, X...) +typed_vcat{Tv}(::Type{SparseVector{Tv}}, X::AbstractSparseVector...) = + typed_vcat(SparseVector{Tv,promote_indtype(X...)}, X...) +typed_vcat{T<:SparseVector}(::Type{T}, Xin::AbstractVector...) = + typed_vcat(T, map(sparse, Xin)...) + +hcat(Xin::Union{Vector, AbstractSparseVector, SparseMatrixCSC}...) = typed_hcat(SparseMatrixCSC, Xin...) -hcat(Xin::Union{AbstractSparseVector, SparseMatrixCSC}...) = hcat(map(SparseMatrixCSC, Xin)...) -vcat(Xin::Union{AbstractSparseVector, SparseMatrixCSC}...) = vcat(map(SparseMatrixCSC, Xin)...) -hcat(Xin::Union{Vector, AbstractSparseVector}...) = hcat(map(sparse, Xin)...) -vcat(Xin::Union{Vector, AbstractSparseVector}...) = vcat(map(sparse, Xin)...) +vcat(Xin::Union{AbstractSparseVector, SparseMatrixCSC}...) = typed_vcat(SparseMatrixCSC, Xin...) +vcat(Xin::Union{Vector, AbstractSparseVector}...) = typed_vcat(SparseVector, Xin...) +vcat(Xin::AbstractSparseVector...) = typed_vcat(SparseVector, Xin...) ### math functions