From bcbda77e3f0c0e2e16fa0dae457145693bac3c91 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Wed, 16 Aug 2017 01:21:37 +0200 Subject: [PATCH 1/2] 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 97ec06ecb4484..5ec6023379708 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1690,6 +1690,9 @@ export hex2num # issue #17886 # deprecations for filter[!] with 2-arg functions are in associative.jl +# 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 From 4e7da40366c5a400424f8ee0bf5a827c9106a0bc Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Mon, 21 Aug 2017 00:21:24 +0200 Subject: [PATCH 2/2] use spdiagm instead --- NEWS.md | 3 +++ base/deprecated.jl | 2 +- base/sparse/sparsevector.jl | 27 --------------------------- test/sparse/sparse.jl | 7 +++++++ test/sparse/sparsevector.jl | 7 ------- 5 files changed, 11 insertions(+), 35 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1e3c1729e1659..5a996e7e71452 100644 --- a/NEWS.md +++ b/NEWS.md @@ -320,6 +320,9 @@ Deprecated or removed * `Base.SparseArrays.SpDiagIterator` has been removed ([#23261]). + * `diagm(A::SparseMatrixCSC)` has been deprecated in favor of + `spdiagm(sparsevec(A))` ([#23341]). + Command-line option changes --------------------------- diff --git a/base/deprecated.jl b/base/deprecated.jl index 5ec6023379708..0adc8eb68c8ff 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1691,7 +1691,7 @@ export hex2num # deprecations for filter[!] with 2-arg functions are in associative.jl # PR 23341 -@deprecate diagm(A::SparseMatrixCSC) diagm(sparsevec(A)) +@deprecate diagm(A::SparseMatrixCSC) spdiagm(sparsevec(A)) # END 0.7 deprecations diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 8bb0a975626b3..80225e4d328c8 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -2004,30 +2004,3 @@ 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 d4b559cbdf788..62e705c3cf323 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -1316,6 +1316,13 @@ end @test trace(speye(5)) == 5 end +@testset "spdiagm" begin + v = sprand(10, 0.4) + @test spdiagm(v)::SparseMatrixCSC == diagm(Vector(v)) + @test spdiagm(sparse(ones(5)))::SparseMatrixCSC == speye(5) + @test spdiagm(sparse(zeros(5)))::SparseMatrixCSC == spzeros(5,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 9506b232a9ed9..4a61940f53677 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -1150,10 +1150,3 @@ 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