Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let spdiagm preserve sparsity #37254

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0236a98
let spdiagm preserve sparsity
dkarrasch Aug 28, 2020
415b7e7
add test
dkarrasch Aug 28, 2020
deb08a5
rm whitespace
dkarrasch Aug 28, 2020
ec88ee0
introduce _numel, fix test
dkarrasch Aug 28, 2020
7f75f82
fix bootstrap
dkarrasch Aug 28, 2020
f9410e1
let spdiagm preserve sparsity
dkarrasch Aug 28, 2020
39c4e65
add test
dkarrasch Aug 28, 2020
eb49fce
rm whitespace
dkarrasch Aug 28, 2020
d625f4b
introduce _numel, fix test
dkarrasch Aug 28, 2020
086e049
fix bootstrap
dkarrasch Aug 28, 2020
e781e93
use more dispatch
dkarrasch Sep 16, 2020
024e93e
fix whitespace
dkarrasch Sep 16, 2020
2cd3fb7
add convenience constructor and tests
dkarrasch Sep 17, 2020
78bb2c8
Merge branch 'dk/spdiagm' of https://github.com/JuliaLang/julia into …
dkarrasch Sep 17, 2020
e20eba8
add compat annotation
dkarrasch Sep 17, 2020
46917ae
fix doctests
dkarrasch Sep 17, 2020
2c006c9
add NEWS entry
dkarrasch Sep 20, 2020
d121ee1
Merge branch 'master' into dk/spdiagm
dkarrasch Sep 20, 2020
5295af2
better names
dkarrasch Sep 20, 2020
6f85c0a
Update logo in README
ViralBShah Sep 21, 2020
9738c14
Remove logo in doc/build/README.md
ViralBShah Sep 21, 2020
9ffc703
Fix debug build (#37674)
yuyichao Sep 21, 2020
10e2455
dump: ensure Array eltype layout is initialized early (#37594)
vtjnash Sep 21, 2020
5a86131
Add in_sysimage(pkgid::PkgId) to check if a package is in the sysimag…
IanButterworth Sep 21, 2020
5f55b97
increase `RELOC_TAG_OFFSET` to handle larger system images (#37613)
JeffBezanson Sep 21, 2020
404a3f3
parent 5f55b977e439ab7b0d09dd826732ff5ea3f1098f
dkarrasch Aug 28, 2020
c52b481
Merge branch 'dk/spdiagm' of https://github.com/JuliaLang/julia into …
dkarrasch Sep 21, 2020
8e95825
use more dispatch
dkarrasch Sep 16, 2020
e600275
fix whitespace
dkarrasch Sep 16, 2020
e08db2a
better names
dkarrasch Sep 20, 2020
f6e6d6e
Merge branch 'dk/spdiagm' of https://github.com/JuliaLang/julia into …
dkarrasch Sep 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3491,20 +3491,25 @@ function istril(A::AbstractSparseMatrixCSC)
return true
end

_numel(vect::SparseVector) = nnz(vect)
dkarrasch marked this conversation as resolved.
Show resolved Hide resolved
_numel(vect::AbstractArray) = length(vect)

function spdiagm_internal(kv::Pair{<:Integer,<:AbstractVector}...)
ncoeffs = 0
for p in kv
ncoeffs += length(p.second)
ncoeffs += _numel(p.second)
end
I = Vector{Int}(undef, ncoeffs)
J = Vector{Int}(undef, ncoeffs)
V = Vector{promote_type(map(x -> eltype(x.second), kv)...)}(undef, ncoeffs)
i = 0
m = 0
n = 0
for p in kv
dia = p.first
vect = p.second
numel = length(vect)
numinds = length(vect)
numel = _numel(vect)
if dia < 0
row = -dia
col = 0
Expand All @@ -3516,12 +3521,20 @@ function spdiagm_internal(kv::Pair{<:Integer,<:AbstractVector}...)
col = 0
end
r = 1+i:numel+i
I[r] = row+1:row+numel
J[r] = col+1:col+numel
copyto!(view(V, r), vect)
if vect isa AbstractSparseVector
dkarrasch marked this conversation as resolved.
Show resolved Hide resolved
I[r] = row .+ nonzeroinds(vect)
J[r] = col .+ nonzeroinds(vect)
copyto!(view(V, r), nonzeros(vect))
else
I[r] = row+1:row+numinds
J[r] = col+1:col+numinds
copyto!(view(V, r), vect)
end
m = max(m, row + numinds)
n = max(n, col + numinds)
i += numel
end
return I, J, V
return I, J, V, m, n
end

"""
Expand All @@ -3548,8 +3561,7 @@ julia> spdiagm(-1 => [1,2,3,4], 1 => [4,3,2,1])
spdiagm(kv::Pair{<:Integer,<:AbstractVector}...) = _spdiagm(nothing, kv...)
spdiagm(m::Integer, n::Integer, kv::Pair{<:Integer,<:AbstractVector}...) = _spdiagm((Int(m),Int(n)), kv...)
function _spdiagm(size, kv::Pair{<:Integer,<:AbstractVector}...)
I, J, V = spdiagm_internal(kv...)
mmax, nmax = dimlub(I), dimlub(J)
I, J, V, mmax, nmax = spdiagm_internal(kv...)
mnmax = max(mmax, nmax)
m, n = something(size, (mnmax,mnmax))
(m ≥ mmax && n ≥ nmax) || throw(DimensionMismatch("invalid size=$size"))
Expand Down
5 changes: 5 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,11 @@ end
@test spdiagm(m,n, 0 => x, 1 => x) == M
end
end

# sparsity-preservation
x = sprand(10, 0.2); y = ones(9)
@test spdiagm(0 => x, 1 => y) == diagm(0 => x, 1 => y)
@test nnz(spdiagm(0 => x, 1 => y)) == length(y) + nnz(x)
end

@testset "diag" begin
Expand Down