Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 992f285

Browse files
committedOct 27, 2016
Provide specialized typed_[hv]cat(::Type{SparseVector}, ...)
And make hcat/vcat involving SparseVectors just call the corresponding typed_*cat function.
1 parent 74d2392 commit 992f285

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed
 

‎base/sparse/sparsevector.jl

+32-12
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,14 @@ complex(x::AbstractSparseVector) =
774774

775775
# Without the first of these methods, horizontal concatenations of SparseVectors fall
776776
# back to the horizontal concatenation method that ensures that combinations of
777-
# sparse/special/dense matrix/vector types concatenate to SparseMatrixCSCs, instead
778-
# of _absspvec_hcat below. The <:Integer qualifications are necessary for correct dispatch.
779-
hcat{Tv,Ti<:Integer}(X::SparseVector{Tv,Ti}...) = _absspvec_hcat(X...)
780-
hcat{Tv,Ti<:Integer}(X::AbstractSparseVector{Tv,Ti}...) = _absspvec_hcat(X...)
781-
function _absspvec_hcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...)
777+
# sparse/special/dense matrix/vector types concatenate to SparseMatrixCSCs.
778+
# The <:Integer qualifications are necessary for correct dispatch.
779+
hcat{Tv,Ti<:Integer}(X::SparseVector{Tv,Ti}...) = typed_hcat(SparseMatrixCSC, X...)
780+
hcat{Tv,Ti<:Integer}(X::AbstractSparseVector{Tv,Ti}...) = typed_hcat(SparseMatrixCSC, X...)
781+
782+
promote_indtype{Tv,Ti}(X::SparseVector{Tv,Ti}, Xs::SparseVector...) = promote_type(Ti, promote_indtype(Xs...))
783+
784+
function typed_hcat{Tv,Ti<:Integer}(::Type{SparseMatrixCSC{Tv,Ti}}, X::AbstractSparseVector...)
782785
# check sizes
783786
n = length(X)
784787
m = length(X[1])
@@ -806,14 +809,22 @@ function _absspvec_hcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...)
806809
colptr[n+1] = roff
807810
SparseMatrixCSC{Tv,Ti}(m, n, colptr, nzrow, nzval)
808811
end
812+
typed_hcat(::Type{SparseMatrixCSC}, X::AbstractSparseVector...) =
813+
typed_hcat(SparseMatrixCSC{promote_eltype(X...)}, X...)
814+
typed_hcat{Tv}(::Type{SparseMatrixCSC{Tv}}, X::AbstractSparseVector...) =
815+
typed_hcat(SparseMatrixCSC{Tv,promote_indtype(X...)}, X...)
816+
typed_hcat{T<:SparseMatrixCSC}(::Type{T}, Xin::AbstractVector...) =
817+
typed_hcat(T, map(sparse, Xin)...)
809818

810819
# Without the first of these methods, vertical concatenations of SparseVectors fall
811820
# back to the vertical concatenation method that ensures that combinations of
812-
# sparse/special/dense matrix/vector types concatenate to SparseMatrixCSCs, instead
813-
# of _absspvec_vcat below. The <:Integer qualifications are necessary for correct dispatch.
814-
vcat{Tv,Ti<:Integer}(X::SparseVector{Tv,Ti}...) = _absspvec_vcat(X...)
815-
vcat{Tv,Ti<:Integer}(X::AbstractSparseVector{Tv,Ti}...) = _absspvec_vcat(X...)
816-
function _absspvec_vcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...)
821+
# sparse/special/dense matrix/vector types concatenate to SparseMatrixCSCs.
822+
# The <:Integer qualifications are necessary for correct dispatch.
823+
vcat{Tv,Ti<:Integer}(X::SparseVector{Tv,Ti}...) = typed_vcat(SparseVector, X...)
824+
vcat{Tv,Ti<:Integer}(X::AbstractSparseVector{Tv,Ti}...) = typed_vcat(SparseVector, X...)
825+
826+
typed_vcat{T<:SparseVector}(::Type{T}) = T(0)
827+
function typed_vcat{Tv,Ti<:Integer}(::Type{SparseVector{Tv,Ti}}, X::AbstractSparseVector...)
817828
# check sizes
818829
n = length(X)
819830
tnnz = 0
@@ -840,9 +851,18 @@ function _absspvec_vcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...)
840851
end
841852
SparseVector(len, rnzind, rnzval)
842853
end
854+
typed_vcat(::Type{SparseVector}, X::AbstractSparseVector...) =
855+
typed_vcat(SparseVector{promote_eltype(X...)}, X...)
856+
typed_vcat{Tv}(::Type{SparseVector{Tv}}, X::AbstractSparseVector...) =
857+
typed_vcat(SparseVector{Tv,promote_indtype(X...)}, X...)
858+
typed_vcat{T<:SparseVector}(::Type{T}, Xin::AbstractVector...) =
859+
typed_vcat(T, map(sparse, Xin)...)
860+
843861

844-
hcat(Xin::Union{Vector, AbstractSparseVector}...) = hcat(map(sparse, Xin)...)
845-
vcat(Xin::Union{Vector, AbstractSparseVector}...) = vcat(map(sparse, Xin)...)
862+
hcat(Xin::Union{Vector, AbstractSparseVector, SparseMatrixCSC}...) = typed_hcat(SparseMatrixCSC, Xin...)
863+
vcat(Xin::Union{Vector, AbstractSparseVector}...) = typed_vcat(SparseVector, Xin...)
864+
vcat(Xin::AbstractSparseVector...) = typed_vcat(SparseVector, Xin...)
865+
vcat(Xin::Union{AbstractSparseVector, SparseMatrixCSC}...) = typed_vcat(SparseMatrixCSC, Xin...)
846866

847867

848868
### Concatenation of un/annotated sparse/special/dense vectors/matrices

0 commit comments

Comments
 (0)
Please sign in to comment.