SparseArrays
automatically runs dropzeros!
after certain operations: for example,
using SparseArrays
A = spdiagm(ones(3))
println(nnz(A)) # result: 3
A .+= spdiagm(-1 .* ones(3))
println(nnz(A)) # result: 0
How can I prevent this from happening? Basically I want a version of .+=
that does not change the sparsity structure of the left side.
The matrix H
that I’m looking at is of the form J' * J + c * I
, where J
has fixed sparsity structure. I could work entry-by-entry: iterate through the columns of J
as sparse vectors, compute pairwise dot products, and set the appropriate entries. But it seems like there should be a better way. (I care about performance: J' * J
is almost certainly faster than explicitly computing dots products of pairs of columns.)