简体   繁体   中英

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".

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM