Skip to content

Commit 4e7da40

Browse files
committed
use spdiagm instead
1 parent bcbda77 commit 4e7da40

File tree

5 files changed

+11
-35
lines changed

5 files changed

+11
-35
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@ Deprecated or removed
320320

321321
* `Base.SparseArrays.SpDiagIterator` has been removed ([#23261]).
322322

323+
* `diagm(A::SparseMatrixCSC)` has been deprecated in favor of
324+
`spdiagm(sparsevec(A))` ([#23341]).
325+
323326
Command-line option changes
324327
---------------------------
325328

base/deprecated.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,7 @@ export hex2num
16911691
# deprecations for filter[!] with 2-arg functions are in associative.jl
16921692

16931693
# PR 23341
1694-
@deprecate diagm(A::SparseMatrixCSC) diagm(sparsevec(A))
1694+
@deprecate diagm(A::SparseMatrixCSC) spdiagm(sparsevec(A))
16951695

16961696
# END 0.7 deprecations
16971697

base/sparse/sparsevector.jl

-27
Original file line numberDiff line numberDiff line change
@@ -2004,30 +2004,3 @@ function fill!(A::Union{SparseVector, SparseMatrixCSC}, x)
20042004
end
20052005
return A
20062006
end
2007-
2008-
function diagm(v::SparseVector{Tv,Ti}) where {Tv,Ti}
2009-
n = length(v)
2010-
numnz = nnz(v)
2011-
colptr = Vector{Ti}(n+1)
2012-
rowval = Vector{Ti}(numnz)
2013-
nzval = Vector{Tv}(numnz)
2014-
2015-
copy!(rowval, 1, v.nzind, 1, numnz)
2016-
copy!(nzval, 1, v.nzval, 1, numnz)
2017-
colptr[1] = 1
2018-
ptr = 1
2019-
col = 1
2020-
while col <= n && ptr <= numnz
2021-
while rowval[ptr] > col
2022-
colptr[col+1] = colptr[col]
2023-
col += 1
2024-
end
2025-
colptr[col+1] = colptr[col] + 1
2026-
ptr += 1
2027-
col += 1
2028-
end
2029-
if col <= n
2030-
colptr[(col+1):(n+1)] = colptr[col]
2031-
end
2032-
return SparseMatrixCSC(n, n, colptr, rowval, nzval)
2033-
end

test/sparse/sparse.jl

+7
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,13 @@ end
13161316
@test trace(speye(5)) == 5
13171317
end
13181318

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)
1324+
end
1325+
13191326
@testset "diag" begin
13201327
for T in (Float64, Complex128)
13211328
S1 = sprand(T, 5, 5, 0.5)

test/sparse/sparsevector.jl

-7
Original file line numberDiff line numberDiff line change
@@ -1150,10 +1150,3 @@ end
11501150
@testset "spzeros with index type" begin
11511151
@test typeof(spzeros(Float32, Int16, 3)) == SparseVector{Float32,Int16}
11521152
end
1153-
1154-
@testset "diagm" begin
1155-
v = sprand(10, 0.4)
1156-
@test diagm(v)::SparseMatrixCSC == diagm(Vector(v))
1157-
@test diagm(sparse(ones(5)))::SparseMatrixCSC == speye(5)
1158-
@test diagm(sparse(zeros(5)))::SparseMatrixCSC == spzeros(5,5)
1159-
end

0 commit comments

Comments
 (0)