Skip to content

Commit 1db7b8f

Browse files
fredrikekreandreasnoack
authored andcommitted
deprecate diagm(A::SparseMatrixCSC) in favor of spdiagm(sparsevec(A)) (#23341)
* deprecate diagm(A::SparseMatrixCSC) in favor of diagm(sparsevec(A)) * use spdiagm instead
1 parent 5fa91e2 commit 1db7b8f

File tree

4 files changed

+11
-48
lines changed

4 files changed

+11
-48
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ Deprecated or removed
326326

327327
* `Base.SparseArrays.SpDiagIterator` has been removed ([#23261]).
328328

329+
* `diagm(A::SparseMatrixCSC)` has been deprecated in favor of
330+
`spdiagm(sparsevec(A))` ([#23341]).
331+
329332
Command-line option changes
330333
---------------------------
331334

base/deprecated.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,9 @@ export hex2num
17051705
# issue #17886
17061706
# deprecations for filter[!] with 2-arg functions are in associative.jl
17071707

1708+
# PR 23341
1709+
@deprecate diagm(A::SparseMatrixCSC) spdiagm(sparsevec(A))
1710+
17081711
# END 0.7 deprecations
17091712

17101713
# BEGIN 1.0 deprecations

base/sparse/sparsematrix.jl

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3413,49 +3413,6 @@ function trace(A::SparseMatrixCSC{Tv}) where Tv
34133413
return s
34143414
end
34153415

3416-
function diagm(v::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti}
3417-
if size(v,1) != 1 && size(v,2) != 1
3418-
throw(DimensionMismatch("input should be nx1 or 1xn"))
3419-
end
3420-
3421-
n = length(v)
3422-
numnz = nnz(v)
3423-
colptr = Vector{Ti}(n+1)
3424-
rowval = Vector{Ti}(numnz)
3425-
nzval = Vector{Tv}(numnz)
3426-
3427-
if size(v,1) == 1
3428-
copy!(colptr, 1, v.colptr, 1, n+1)
3429-
ptr = 1
3430-
for col = 1:n
3431-
if colptr[col] != colptr[col+1]
3432-
rowval[ptr] = col
3433-
nzval[ptr] = v.nzval[ptr]
3434-
ptr += 1
3435-
end
3436-
end
3437-
else
3438-
copy!(rowval, 1, v.rowval, 1, numnz)
3439-
copy!(nzval, 1, v.nzval, 1, numnz)
3440-
colptr[1] = 1
3441-
ptr = 1
3442-
col = 1
3443-
while col <= n && ptr <= numnz
3444-
while rowval[ptr] > col
3445-
colptr[col+1] = colptr[col]
3446-
col += 1
3447-
end
3448-
colptr[col+1] = colptr[col] + 1
3449-
ptr += 1
3450-
col += 1
3451-
end
3452-
if col <= n
3453-
colptr[(col+1):(n+1)] = colptr[col]
3454-
end
3455-
end
3456-
3457-
return SparseMatrixCSC(n, n, colptr, rowval, nzval)
3458-
end
34593416

34603417
# Sort all the indices in each column of a CSC sparse matrix
34613418
# sortSparseMatrixCSC!(A, sortindices = :sortcols) # Sort each column with sort()

test/sparse/sparse.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,11 +1316,11 @@ end
13161316
@test trace(speye(5)) == 5
13171317
end
13181318

1319-
@testset "diagm on a matrix" begin
1320-
@test_throws DimensionMismatch diagm(sparse(ones(5,2)))
1321-
@test_throws DimensionMismatch diagm(sparse(ones(2,5)))
1322-
@test diagm(sparse(ones(1,5))) == speye(5)
1323-
@test diagm(sparse(ones(5,1))) == speye(5)
1319+
@testset "spdiagm" begin
1320+
v = sprand(10, 0.4)
1321+
@test spdiagm(v)::SparseMatrixCSC == diagm(Vector(v))
1322+
@test spdiagm(sparse(ones(5)))::SparseMatrixCSC == speye(5)
1323+
@test spdiagm(sparse(zeros(5)))::SparseMatrixCSC == spzeros(5,5)
13241324
end
13251325

13261326
@testset "diag" begin

0 commit comments

Comments
 (0)