Skip to content

Commit

Permalink
use spdiagm instead
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Aug 20, 2017
1 parent bcbda77 commit 4e7da40
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 35 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------------

Expand Down
2 changes: 1 addition & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 0 additions & 27 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions test/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4e7da40

Please sign in to comment.