Skip to content

Commit 7483bb9

Browse files
committed
Fix #13130 such that concatenation of sparse and dense is sparse.
1 parent d341677 commit 7483bb9

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ Library improvements
138138

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

141+
* Concatenating dense and sparse matrices now returns a sparse matrix ([#15172]).
142+
141143
Deprecated or removed
142144
---------------------
143145

@@ -198,3 +200,4 @@ Deprecated or removed
198200
[#15550]: https://github.com/JuliaLang/julia/issues/15550
199201
[#15609]: https://github.com/JuliaLang/julia/issues/15609
200202
[#15763]: https://github.com/JuliaLang/julia/issues/15763
203+
[#15172]: https://github.com/JuliaLang/julia/issues/15172

base/array.jl

+8
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,14 @@ function hcat{T}(V::Vector{T}...)
714714
[ V[j][i]::T for i=1:length(V[1]), j=1:length(V) ]
715715
end
716716

717+
hcat(A::Matrix...) = typed_hcat(promote_eltype(A...), A...)
718+
hcat{T}(A::Matrix{T}...) = typed_hcat(T, A...)
719+
720+
vcat(A::Matrix...) = typed_vcat(promote_eltype(A...), A...)
721+
vcat{T}(A::Matrix{T}...) = typed_vcat(T, A...)
722+
723+
hvcat(rows::Tuple{Vararg{Int}}, xs::Matrix...) = typed_hvcat(promote_eltype(xs...), rows, xs...)
724+
hvcat{T}(rows::Tuple{Vararg{Int}}, xs::Matrix{T}...) = typed_hvcat(T, rows, xs...)
717725

718726
## find ##
719727

base/sparse/sparsematrix.jl

+15-2
Original file line numberDiff line numberDiff line change
@@ -2867,13 +2867,26 @@ function hcat(X::SparseMatrixCSC...)
28672867
SparseMatrixCSC(m, n, colptr, rowval, nzval)
28682868
end
28692869

2870-
function hvcat(rows::Tuple{Vararg{Int}}, X::SparseMatrixCSC...)
2870+
2871+
# Sparse/dense concatenation
2872+
2873+
function hcat(Xin::Union{Matrix, SparseMatrixCSC}...)
2874+
X = SparseMatrixCSC[issparse(x) ? x : sparse(x) for x in Xin]
2875+
hcat(X...)
2876+
end
2877+
2878+
function vcat(Xin::Union{Matrix, SparseMatrixCSC}...)
2879+
X = SparseMatrixCSC[issparse(x) ? x : sparse(x) for x in Xin]
2880+
vcat(X...)
2881+
end
2882+
2883+
function hvcat(rows::Tuple{Vararg{Int}}, X::Union{Matrix, SparseMatrixCSC}...)
28712884
nbr = length(rows) # number of block rows
28722885

28732886
tmp_rows = Array(SparseMatrixCSC, nbr)
28742887
k = 0
28752888
@inbounds for i = 1 : nbr
2876-
tmp_rows[i] = hcat(X[(1 : rows[i]) + k]...)
2889+
tmp_rows[i] = sparse(hcat(X[(1 : rows[i]) + k]...))
28772890
k += rows[i]
28782891
end
28792892
vcat(tmp_rows...)

test/sparsedir/sparse.jl

+13
Original file line numberDiff line numberDiff line change
@@ -1319,3 +1319,16 @@ let
13191319
@test issparse(UpperTriangular(full(m))) == false
13201320
@test issparse(LinAlg.UnitUpperTriangular(full(m))) == false
13211321
end
1322+
1323+
# dense sparse concatenation -> sparse return type
1324+
@test issparse([sprand(10,10,.1) rand(10,10)])
1325+
@test issparse([sprand(10,10,.1); rand(10,10)])
1326+
@test issparse([sprand(10,10,.1) rand(10,10); rand(10,10) rand(10,10)])
1327+
#---
1328+
# Matrix vector cat not supported for sparse #13130
1329+
#@test issparse([sprand(10,10,.1) rand(10)])
1330+
#@test issparse([sprand(10,10,.1) sprand(10,.1)])
1331+
# ---
1332+
@test !issparse([rand(10,10) rand(10,10)])
1333+
@test !issparse([rand(10,10); rand(10,10)])
1334+
@test !issparse([rand(10,10) rand(10,10); rand(10,10) rand(10,10)])

0 commit comments

Comments
 (0)