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 Apr 27, 2016
1 parent d341677 commit f68ac31
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 @@ -138,6 +138,8 @@ Library improvements

* The new `Base.StackTraces` module makes stack traces easier to use programmatically. ([#14469])

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

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

Expand Down Expand Up @@ -198,3 +200,4 @@ Deprecated or removed
[#15550]: https://github.com/JuliaLang/julia/issues/15550
[#15609]: https://github.com/JuliaLang/julia/issues/15609
[#15763]: https://github.com/JuliaLang/julia/issues/15763
[#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 @@ -714,6 +714,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 @@ -2867,13 +2867,26 @@ 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)
k = 0
@inbounds for i = 1 : nbr
tmp_rows[i] = hcat(X[(1 : rows[i]) + k]...)
tmp_rows[i] = sparse(hcat(X[(1 : rows[i]) + k]...))
k += rows[i]
end
vcat(tmp_rows...)
Expand Down
13 changes: 13 additions & 0 deletions test/sparsedir/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1319,3 +1319,16 @@ let
@test issparse(UpperTriangular(full(m))) == false
@test issparse(LinAlg.UnitUpperTriangular(full(m))) == false
end

# 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 f68ac31

Please sign in to comment.