From c8e505c7b26c4ac460b2c4f4fbed68bcecd595c9 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Wed, 16 Aug 2017 01:21:37 +0200 Subject: [PATCH] deprecate diagm(A::SparseMatrixCSC) in favor of diagm(sparsevec(A)) --- base/deprecated.jl | 3 +++ base/sparse/sparsematrix.jl | 43 ------------------------------------- base/sparse/sparsevector.jl | 27 +++++++++++++++++++++++ test/sparse/sparse.jl | 7 ------ test/sparse/sparsevector.jl | 7 ++++++ 5 files changed, 37 insertions(+), 50 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 0671d1db0fbe3..d05ed6199896a 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1687,6 +1687,9 @@ export hex2num # issue #5148, PR #23259 # warning for `const` on locals should be changed to an error in julia-syntax.scm +# PR 23341 +@deprecate diagm(A::SparseMatrixCSC) diagm(sparsevec(A)) + # END 0.7 deprecations # BEGIN 1.0 deprecations diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 0c501c8c72bfa..ef58c290f24ba 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -3413,49 +3413,6 @@ function trace(A::SparseMatrixCSC{Tv}) where Tv return s end -function diagm(v::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} - if size(v,1) != 1 && size(v,2) != 1 - throw(DimensionMismatch("input should be nx1 or 1xn")) - end - - n = length(v) - numnz = nnz(v) - colptr = Vector{Ti}(n+1) - rowval = Vector{Ti}(numnz) - nzval = Vector{Tv}(numnz) - - if size(v,1) == 1 - copy!(colptr, 1, v.colptr, 1, n+1) - ptr = 1 - for col = 1:n - if colptr[col] != colptr[col+1] - rowval[ptr] = col - nzval[ptr] = v.nzval[ptr] - ptr += 1 - end - end - else - copy!(rowval, 1, v.rowval, 1, numnz) - copy!(nzval, 1, v.nzval, 1, numnz) - colptr[1] = 1 - ptr = 1 - col = 1 - while col <= n && ptr <= numnz - while rowval[ptr] > col - colptr[col+1] = colptr[col] - col += 1 - end - colptr[col+1] = colptr[col] + 1 - ptr += 1 - col += 1 - end - if col <= n - colptr[(col+1):(n+1)] = colptr[col] - end - end - - return SparseMatrixCSC(n, n, colptr, rowval, nzval) -end # Sort all the indices in each column of a CSC sparse matrix # sortSparseMatrixCSC!(A, sortindices = :sortcols) # Sort each column with sort() diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 80225e4d328c8..8bb0a975626b3 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -2004,3 +2004,30 @@ function fill!(A::Union{SparseVector, SparseMatrixCSC}, x) end return A end + +function diagm(v::SparseVector{Tv,Ti}) where {Tv,Ti} + n = length(v) + numnz = nnz(v) + colptr = Vector{Ti}(n+1) + rowval = Vector{Ti}(numnz) + nzval = Vector{Tv}(numnz) + + copy!(rowval, 1, v.nzind, 1, numnz) + copy!(nzval, 1, v.nzval, 1, numnz) + colptr[1] = 1 + ptr = 1 + col = 1 + while col <= n && ptr <= numnz + while rowval[ptr] > col + colptr[col+1] = colptr[col] + col += 1 + end + colptr[col+1] = colptr[col] + 1 + ptr += 1 + col += 1 + end + if col <= n + colptr[(col+1):(n+1)] = colptr[col] + end + return SparseMatrixCSC(n, n, colptr, rowval, nzval) +end diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index 38e25d0a3a5a8..d4b559cbdf788 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -1316,13 +1316,6 @@ end @test trace(speye(5)) == 5 end -@testset "diagm on a matrix" begin - @test_throws DimensionMismatch diagm(sparse(ones(5,2))) - @test_throws DimensionMismatch diagm(sparse(ones(2,5))) - @test diagm(sparse(ones(1,5))) == speye(5) - @test diagm(sparse(ones(5,1))) == speye(5) -end - @testset "diag" begin for T in (Float64, Complex128) S1 = sprand(T, 5, 5, 0.5) diff --git a/test/sparse/sparsevector.jl b/test/sparse/sparsevector.jl index 4a61940f53677..9506b232a9ed9 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -1150,3 +1150,10 @@ end @testset "spzeros with index type" begin @test typeof(spzeros(Float32, Int16, 3)) == SparseVector{Float32,Int16} end + +@testset "diagm" begin + v = sprand(10, 0.4) + @test diagm(v)::SparseMatrixCSC == diagm(Vector(v)) + @test diagm(sparse(ones(5)))::SparseMatrixCSC == speye(5) + @test diagm(sparse(zeros(5)))::SparseMatrixCSC == spzeros(5,5) +end