From 65573d90d20c2bfb87d69e3a5a043e04e09559f1 Mon Sep 17 00:00:00 2001 From: Patrick Kofod Mogensen Date: Thu, 2 Jun 2016 19:02:40 +0200 Subject: [PATCH 1/2] Change concatenation involving sparse matrices, sparse vectors and dense vectors to return sparse arrays. --- base/array.jl | 8 ++++++++ base/sparse/sparsematrix.jl | 4 ++-- base/sparse/sparsevector.jl | 5 +++++ test/sparsedir/sparse.jl | 4 ---- test/sparsedir/sparsevector.jl | 12 ++++++++++++ 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/base/array.jl b/base/array.jl index 8cf7b097e37f4..bbe97c195a1bc 100644 --- a/base/array.jl +++ b/base/array.jl @@ -721,6 +721,14 @@ 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...) +hcat(A::Union{Matrix, Vector}...) = typed_hcat(promote_eltype(A...), A...) +hcat{T}(A::Union{Matrix{T}, Vector{T}}...) = typed_hcat(T, A...) + + +vcat(A::Union{Matrix, Vector}...) = typed_vcat(promote_eltype(A...), A...) +vcat{T}(A::Union{Matrix{T}, Vector{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...) diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 803f60b75836b..bdf70fd75b2ee 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -2901,12 +2901,12 @@ end # Sparse/dense concatenation -function hcat(Xin::Union{Matrix, SparseMatrixCSC}...) +function hcat(Xin::Union{Vector, Matrix, SparseMatrixCSC}...) X = SparseMatrixCSC[issparse(x) ? x : sparse(x) for x in Xin] hcat(X...) end -function vcat(Xin::Union{Matrix, SparseMatrixCSC}...) +function vcat(Xin::Union{Vector, Matrix, SparseMatrixCSC}...) X = SparseMatrixCSC[issparse(x) ? x : sparse(x) for x in Xin] vcat(X...) end diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index d1207f740ff2b..f7987a2b898da 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -755,6 +755,11 @@ function vcat{Tv,Ti}(X::AbstractSparseVector{Tv,Ti}...) SparseVector(len, rnzind, rnzval) end +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)...) + ### math functions ### Unary Map diff --git a/test/sparsedir/sparse.jl b/test/sparsedir/sparse.jl index cbfc014619bb0..5f4ae28caae14 100644 --- a/test/sparsedir/sparse.jl +++ b/test/sparsedir/sparse.jl @@ -1381,10 +1381,6 @@ end @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)]) diff --git a/test/sparsedir/sparsevector.jl b/test/sparsedir/sparsevector.jl index 29465824452a6..5f3a26481bb5e 100644 --- a/test/sparsedir/sparsevector.jl +++ b/test/sparsedir/sparsevector.jl @@ -988,3 +988,15 @@ for Tv in [Float32, Float64, Int64, Int32, Complex128] end end end + +# Matrix vector cat not supported for sparse #13130 and #16661 +@test issparse([sprand(10,10,.1) sprand(10,.1)]) +@test issparse([sprand(10,1,.1); sprand(10,.1)]) + +@test issparse([sprand(10,10,.1) rand(10)]) +@test issparse([sprand(10,1,.1) rand(10)]) +@test issparse([sprand(10,2,.1) sprand(10,1,.1) rand(10)]) +@test issparse([sprand(10,1,.1); rand(10)]) + +@test issparse([sprand(10,.1) rand(10)]) +@test issparse([sprand(10,.1); rand(10)]) From b4f82dde15ba8f7cb121c510b4dd8d71cc2f20fd Mon Sep 17 00:00:00 2001 From: Patrick Kofod Mogensen Date: Fri, 3 Jun 2016 11:17:49 +0200 Subject: [PATCH 2/2] Change working in sparsevec test, and add Vector to union in hvcat. --- base/sparse/sparsematrix.jl | 2 +- test/sparsedir/sparsevector.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index bdf70fd75b2ee..df2ef9a73da06 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -2911,7 +2911,7 @@ function vcat(Xin::Union{Vector, Matrix, SparseMatrixCSC}...) vcat(X...) end -function hvcat(rows::Tuple{Vararg{Int}}, X::Union{Matrix, SparseMatrixCSC}...) +function hvcat(rows::Tuple{Vararg{Int}}, X::Union{Vector, Matrix, SparseMatrixCSC}...) nbr = length(rows) # number of block rows tmp_rows = Array{SparseMatrixCSC}(nbr) diff --git a/test/sparsedir/sparsevector.jl b/test/sparsedir/sparsevector.jl index 5f3a26481bb5e..fde3585357dd9 100644 --- a/test/sparsedir/sparsevector.jl +++ b/test/sparsedir/sparsevector.jl @@ -989,7 +989,7 @@ for Tv in [Float32, Float64, Int64, Int32, Complex128] end end -# Matrix vector cat not supported for sparse #13130 and #16661 +# ref 13130 and 16661 @test issparse([sprand(10,10,.1) sprand(10,.1)]) @test issparse([sprand(10,1,.1); sprand(10,.1)])