Skip to content

Commit

Permalink
Provide specialized typed_[hv]cat(::Type{SparseVector}, ...)
Browse files Browse the repository at this point in the history
And make hcat/vcat involving SparseVectors just call the corresponding
typed_*cat function.
  • Loading branch information
martinholters committed Jul 6, 2016
1 parent 5140b57 commit 2d76236
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 2d76236

Please sign in to comment.