Search code examples
c++sparse-matrixlinear-algebraeigen

How to efficiently update Eigen sparse matrix entries


I have a fixed sparse matrix that I set up before my computations through:

Eigen::SparseMatrix<double, Eigen::ColMajor> A;
std::vector<Eigen::Triplet<double>> coefficients{
    {0, 0}//, {,},{,},{,}
};
A.setFromTriplets(coefficients.begin(), coefficients.end());
A.makeCompressed();

Now, I need to update it with my actual entries. Unfortunately, going through the entries with:

for(...) A.coeffRef(i,j) = new_val(i,j);

I get a binary search instead of an efficient constant time access.

I could use valuePtr(), but it seems a little bit "hacky".


Solution

  • One way is to go with iterators and update the entries with them:

    for (int k=0; k<A.outerSize(); ++k)
      for (SparseMatrix<double>::InnerIterator it(A,k); it; ++it)
        it.valueRef() = new_val(it.row(), it.col());
    

    In my case, I can unroll the loops and populate manually each entry in code:

    A.coeffs()[0] = new_val_0;
    A.coeffs()[1] = new_val_1;
    A.coeffs()[2] = new_val_2;
    ...
    

    Be aware that this is a column-major sparse matrix, so it's fast j and slow i. Populating the coefficients by hand requires to keep this fact in mind.