I'm thinking about 3 different ways:
1 - the easiest:
for(...) A.coeffRef(i,j) = new_val(i,j);
but it costs 1 binary search per element
2 - via an iterator over the non-zeros:
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());
3 - if you don't need the row/col indices:
for(...) A.coeffs()[k] = new_val;
In terms of speed, the options 2 and 3 are equivalent. Then the most appropriate solution depends on how and in which order you can provide the new values.
I want to reuse a sparse matrix structure and a solver for faster linear solve:
Eigen::SparseMatrix<double, Eigen::ColMajor> A;
Eigen::SparseLU<Eigen::SparseMatrix<double, Eigen::ColMajor>, Eigen::COLAMDOrdering<Eigen::Index> > solver;
I set A at the beginning with 0 entries and ask the solver to analyze the pattern.
Now,, I'd like to reuse the matrix instead of creating a new one, as I'm doing real-time computations and don't want any allocation there.
Can I use the comma operator? Or is there another operator that I can use to update the matrix existing entries? I tried to find information online but couldn't find the best practice for this.