From 45dc18d0bc75c9b74d7934cbbb3311b4d0a54ca2 Mon Sep 17 00:00:00 2001 From: Napoleon Oikonomou Date: Mon, 1 May 2017 04:35:55 +0300 Subject: [PATCH] fixed zero insertion when concatenate sparse matrices with 0*I --- base/sparse/sparsematrix.jl | 3 +++ test/sparse/sparse.jl | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index f21a2411f17d6..cee5b35516408 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -1430,6 +1430,9 @@ speye_scaled(diag, m::Integer, n::Integer) = speye_scaled(typeof(diag), diag, m, function speye_scaled(T, diag, m::Integer, n::Integer) ((m < 0) || (n < 0)) && throw(ArgumentError("invalid array dimensions")) + if iszero(diag) + return SparseMatrixCSC(m, n, ones(Int, n+1), Vector{Int}(0), Vector{T}(0)) + end nnz = min(m,n) colptr = Vector{Int}(1+n) colptr[1:nnz+1] = 1:nnz+1 diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index f0ae30b96c99c..43a0c4c346577 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -45,14 +45,18 @@ do33 = ones(3) end @testset "concatenation tests" begin + sp33 = speye(3, 3) + @testset "horizontal concatenation" begin @test all([se33 se33] == sparse([1, 2, 3, 1, 2, 3], [1, 2, 3, 4, 5, 6], ones(6))) + @test length(([sp33 0I]).nzval) == 3 end @testset "vertical concatenation" begin @test all([se33; se33] == sparse([1, 4, 2, 5, 3, 6], [1, 1, 2, 2, 3, 3], ones(6))) se33_32bit = convert(SparseMatrixCSC{Float32,Int32}, se33) @test all([se33; se33_32bit] == sparse([1, 4, 2, 5, 3, 6], [1, 1, 2, 2, 3, 3], ones(6))) + @test length(([sp33; 0I]).nzval) == 3 end se44 = speye(4) @@ -62,6 +66,7 @@ end se77 = speye(7) @testset "h+v concatenation" begin @test all([se44 sz42 sz41; sz34 se33] == se77) + @test length(([sp33 0I; 1I 0I]).nzval) == 6 end @testset "blkdiag concatenation" begin