You can do this in-place if you're careful about it. Do a scan through the CSC structure first to check the total number of diagonals that are currently nonzero for which you need to insert elements, then do a sizehint! and copy starting from the end of the nzval and rowval arrays, keeping track of an offset that shrinks as you pass each newly inserted diagonal element.
On Thursday, April 2, 2015 at 2:22:57 PM UTC-7, Patrick O'Leary wrote: > > I suppose a diagonal matrix is the worst-case for compressed storage. > > Convert to coordinate format, insert the diagonal elements, and then > convert back to CSC? > > On Thursday, April 2, 2015 at 3:55:38 PM UTC-5, Seth wrote: >> >> The creation of the new sparse matrix (via spdiagm) doubles the amount of >> memory, unfortunately. I had to kill it past 26 GB. >> >> >> On Thursday, April 2, 2015 at 1:36:48 PM UTC-7, Patrick O'Leary wrote: >>> >>> Perhaps try: >>> >>> z = e + spdiagm(fill(Inf, 50000), 0, 50000, 50000) >>> >>> I can't try a matrix that large, I don't have the memory on this >>> system--I tested with 5000, though, and it is massively faster. >>> >>> On Thursday, April 2, 2015 at 3:21:37 PM UTC-5, Seth wrote: >>>> >>>> >>>> >>>> Consider the following: >>>> >>>> julia> @time e = sprandn(50000,50000,0.3); >>>> elapsed time: 95.837541848 seconds (17178 MB allocated, 0.08% gc time >>>> in 4 pauses with 1 full sweep) >>>> >>>> then: >>>> >>>> julia> function doinf!(e::SparseMatrixCSC) >>>> for i = 1:size(e)[1] >>>> e[i,i] = Inf >>>> end >>>> end >>>> doinf! (generic function with 1 method) >>>> >>>> julia> @time z = doinf!(e); >>>> >>>> >>>> ...still going after about 20 minutes. I know sparse matrix insertion >>>> is expensive, but is there a better way of doing this? (I don't have the >>>> storage to make this a dense matrix.) I'm thinking it might be faster to >>>> write my own function to assign random number or Inf within two loops, but >>>> I'm surprised that the doinf! is taking so long. >>>> >>>
