2

I want to gradually add sparse columns to a sparse matrix. Something like:

for ( int ieig_insert= 0; ieig_insert < 5000 ; ieig_insert++ ) {
   arma::sp_vec  tempVec( nSources_Total ) ;  // nSources_Total ~= 60,000  
   createColValues( ieig_insert,  tempVec );  //  ~95% sparse
   sp_mat.col( ieig_insert )= tempVec ; 
}

Yes, I can create a temporary vector<arma::sp_vec> to collect all the columns and then use a giant batch insertion constructor at the end, but that is inelegant. I tried the simpler approach above but it is maddeningly slow. It seems that this column insert should be efficient for CSC representation?

  • Unlike dense matrices, inserting data into an existing sparse matrix (by way of submatrix operations) is likely to require recreating the entire sparse matrix each time. Batch insertion at the end is probably more efficient. There is another way though -- manually set the elements in the matrix using `X(row,col) = value`. According to [this paper](http://arma.sourceforge.net/armadillo_lncs_2018.pdf), Armadillo appears to use a separate caching mechanism for setting individual elements. – mtall Nov 26 '20 at 05:57
  • Thanks for the paper. I also tried inserting individual elements and that was slower. But, since Armadillo uses CSC representation for sparse matrices, each column is compressed largely independently from each others. It should be possible to deal with compressed columns as a unit. – Christof Stork Nov 27 '20 at 15:56

0 Answers0