Skip to content

Commit

Permalink
Make concatenations involving combinations of special matrices with s…
Browse files Browse the repository at this point in the history
…pecial matrices, sparse matrices, or dense matrices/vectors yield sparse arrays.
  • Loading branch information
Sacha0 committed Jul 28, 2016
1 parent e18ae9e commit fea7504
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
10 changes: 7 additions & 3 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3231,17 +3231,21 @@ end

# Sparse/dense concatenation

function hcat(Xin::Union{Vector, Matrix, SparseMatrixCSC}...)
# TODO: A similar definition also exists in base/linalg/bidiag.jl. These definitions should
# be consolidated in a more appropriate location, for example base/linalg/special.jl.
SpecialArrays = Union{Diagonal, Bidiagonal, Tridiagonal, SymTridiagonal}

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

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

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

tmp_rows = Array{SparseMatrixCSC}(nbr)
Expand Down
35 changes: 35 additions & 0 deletions test/linalg/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,38 @@ for typ in [UpperTriangular,LowerTriangular,Base.LinAlg.UnitUpperTriangular,Base
@test Base.LinAlg.A_mul_Bc(atri,qrb[:Q]) full(atri) * qrb[:Q]'
@test Base.LinAlg.A_mul_Bc!(copy(atri),qrb[:Q]) full(atri) * qrb[:Q]'
end

# Test that concatenations of combinations of special and other matrix types yield sparse arrays
let
N = 4
# Test concatenating pairwise combinations of special matrices
diagmat = Diagonal(ones(N))
bidiagmat = Bidiagonal(ones(N), ones(N-1), true)
tridiagmat = Tridiagonal(ones(N-1), ones(N), ones(N-1))
symtridiagmat = SymTridiagonal(ones(N), ones(N-1))
specialmats = (diagmat, bidiagmat, tridiagmat, symtridiagmat)
for specialmata in specialmats, specialmatb in specialmats
@test issubtype(typeof(hcat(specialmata, specialmatb)), AbstractSparseArray)
@test issubtype(typeof(vcat(specialmata, specialmatb)), AbstractSparseArray)
@test issubtype(typeof(hvcat((1,1), specialmata, specialmatb)), AbstractSparseArray)
end
# Test concatenating pairwise combinations of special matrices with sparse or dense matrices
spmat = spdiagm(ones(N))
densemat = diagm(ones(N))
for specialmat in specialmats, othermat in (spmat, densemat)
@test issubtype(typeof(hcat(specialmat, spmat)), AbstractSparseArray)
@test issubtype(typeof(hcat(spmat, specialmat)), AbstractSparseArray)
@test issubtype(typeof(vcat(specialmat, spmat)), AbstractSparseArray)
@test issubtype(typeof(vcat(spmat, specialmat)), AbstractSparseArray)
@test issubtype(typeof(hvcat((1,1), specialmat, spmat)), AbstractSparseArray)
@test issubtype(typeof(hvcat((1,1), spmat, specialmat)), AbstractSparseArray)
end
# Test concatenating combinations of special matrices and dense vectors
densevec = ones(N)
for specialmat in specialmats
@test issubtype(typeof(hcat(specialmat, densevec)), AbstractSparseArray)
@test issubtype(typeof(hcat(densevec, specialmat)), AbstractSparseArray)
@test issubtype(typeof(hvcat((2,), specialmat, densevec)), AbstractSparseArray)
@test issubtype(typeof(hvcat((2,), densevec, specialmat)), AbstractSparseArray)
end
end

0 comments on commit fea7504

Please sign in to comment.