Skip to content

Commit

Permalink
deprecate diagm(A::SparseMatrixCSC) in favor of spdiagm(sparsevec(A)) (
Browse files Browse the repository at this point in the history
…JuliaLang#23341)

* deprecate diagm(A::SparseMatrixCSC) in favor of diagm(sparsevec(A))

* use spdiagm instead
  • Loading branch information
fredrikekre authored and andreasnoack committed Aug 22, 2017
1 parent 5fa91e2 commit 1db7b8f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 48 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,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
---------------------------

Expand Down
3 changes: 3 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,9 @@ export hex2num
# issue #17886
# deprecations for filter[!] with 2-arg functions are in associative.jl

# PR 23341
@deprecate diagm(A::SparseMatrixCSC) spdiagm(sparsevec(A))

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
43 changes: 0 additions & 43 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1316,11 +1316,11 @@ 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)
@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
Expand Down

0 comments on commit 1db7b8f

Please sign in to comment.