Skip to content

Commit c8e505c

Browse files
committed
deprecate diagm(A::SparseMatrixCSC) in favor of diagm(sparsevec(A))
1 parent feaa2f6 commit c8e505c

File tree

5 files changed

+37
-50
lines changed

5 files changed

+37
-50
lines changed

base/deprecated.jl

+3
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,9 @@ export hex2num
16871687
# issue #5148, PR #23259
16881688
# warning for `const` on locals should be changed to an error in julia-syntax.scm
16891689

1690+
# PR 23341
1691+
@deprecate diagm(A::SparseMatrixCSC) diagm(sparsevec(A))
1692+
16901693
# END 0.7 deprecations
16911694

16921695
# BEGIN 1.0 deprecations

base/sparse/sparsematrix.jl

-43
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()

base/sparse/sparsevector.jl

+27
Original file line numberDiff line numberDiff line change
@@ -2004,3 +2004,30 @@ 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,13 +1316,6 @@ 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)
1324-
end
1325-
13261319
@testset "diag" begin
13271320
for T in (Float64, Complex128)
13281321
S1 = sprand(T, 5, 5, 0.5)

test/sparse/sparsevector.jl

+7
Original file line numberDiff line numberDiff line change
@@ -1150,3 +1150,10 @@ 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)