Skip to content

Commit

Permalink
Fix #13130 such that concatenation of sparse and dense is sparse.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkofod committed May 19, 2016
1 parent 1b39fc7 commit e280a27
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ Library improvements

* There is now a default no-op `flush(io)` function for all `IO` types ([#16403]).

* Concatenating dense and sparse matrices now returns a sparse matrix ([#15172]).

Deprecated or removed
---------------------

Expand Down Expand Up @@ -231,3 +233,4 @@ Deprecated or removed
[#15609]: https://github.com/JuliaLang/julia/issues/15609
[#15763]: https://github.com/JuliaLang/julia/issues/15763
[#16403]: https://github.com/JuliaLang/julia/issues/16403
[#15172]: https://github.com/JuliaLang/julia/issues/15172
8 changes: 8 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,14 @@ function hcat{T}(V::Vector{T}...)
[ V[j][i]::T for i=1:length(V[1]), j=1:length(V) ]
end

hcat(A::Matrix...) = typed_hcat(promote_eltype(A...), A...)
hcat{T}(A::Matrix{T}...) = typed_hcat(T, A...)

vcat(A::Matrix...) = typed_vcat(promote_eltype(A...), A...)
vcat{T}(A::Matrix{T}...) = typed_vcat(T, A...)

hvcat(rows::Tuple{Vararg{Int}}, xs::Matrix...) = typed_hvcat(promote_eltype(xs...), rows, xs...)
hvcat{T}(rows::Tuple{Vararg{Int}}, xs::Matrix{T}...) = typed_hvcat(T, rows, xs...)

## find ##

Expand Down
17 changes: 15 additions & 2 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ function convert{Tv,Ti}(::Type{SparseMatrixCSC{Tv,Ti}}, M::AbstractMatrix)
end
convert{T}(::Type{AbstractMatrix{T}}, A::SparseMatrixCSC) = convert(SparseMatrixCSC{T}, A)
convert(::Type{Matrix}, S::SparseMatrixCSC) = full(S)

convert(::Type{SparseMatrixCSC}, M::Matrix) = sparse(M)

"""
full(S)
Expand Down Expand Up @@ -2896,7 +2896,20 @@ function hcat(X::SparseMatrixCSC...)
SparseMatrixCSC(m, n, colptr, rowval, nzval)
end

function hvcat(rows::Tuple{Vararg{Int}}, X::SparseMatrixCSC...)

# Sparse/dense concatenation

function hcat(Xin::Union{Matrix, SparseMatrixCSC}...)
X = SparseMatrixCSC[issparse(x) ? x : sparse(x) for x in Xin]
hcat(X...)
end

function vcat(Xin::Union{Matrix, SparseMatrixCSC}...)
X = SparseMatrixCSC[issparse(x) ? x : sparse(x) for x in Xin]
vcat(X...)
end

function hvcat(rows::Tuple{Vararg{Int}}, X::Union{Matrix, SparseMatrixCSC}...)
nbr = length(rows) # number of block rows

tmp_rows = Array(SparseMatrixCSC, nbr)
Expand Down
13 changes: 13 additions & 0 deletions test/sparsedir/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1367,3 +1367,16 @@ end
@inferred sprand(1, 1, 1.0)
@inferred sprand(1, 1, 1.0, rand, Float64)
@inferred sprand(1, 1, 1.0, x->round(Int,rand(x)*100))

# dense sparse concatenation -> sparse return type
@test issparse([sprand(10,10,.1) rand(10,10)])
@test issparse([sprand(10,10,.1); rand(10,10)])
@test issparse([sprand(10,10,.1) rand(10,10); rand(10,10) rand(10,10)])
#---
# Matrix vector cat not supported for sparse #13130
#@test issparse([sprand(10,10,.1) rand(10)])
#@test issparse([sprand(10,10,.1) sprand(10,.1)])
# ---
@test !issparse([rand(10,10) rand(10,10)])
@test !issparse([rand(10,10); rand(10,10)])
@test !issparse([rand(10,10) rand(10,10); rand(10,10) rand(10,10)])

0 comments on commit e280a27

Please sign in to comment.