0
votes

For example I have a 10x10 SparseMatrix A, and I want to add a 3x3 identity matrix to the upper left corner of A.

A is known to be already non-zero in those 3 entries.

If I have to add the values one by one it is ok too, but I didn't find the method to manipulate on elements of a Sparse Matrix in Eigen.

Did I miss something?

1
See this post. You really can't use all block operations on sparse matrices.Avi Ginsburg
How about element operations? I saw there were insert methods but can I add values to certain elements?CathIAS
I'm not following. What do you mean by element operations and add values to certain elements? Something like m(i,j) += k;?Avi Ginsburg
Yes. Because I only want to add an identity matrix that isn't very large, so maybe doing operations like this can be more efficient than using column operations on the sparse matrix.CathIAS

1 Answers

0
votes

If all you want is to apply an operation to a specific element at a time, you can use coeffRef like so:

typedef Eigen::Triplet<double> T;
std::vector<T> coefficients;
for (int i = 0; i < 9; i++) coefficients.push_back(T(i, i, (i % 3) + 1));

Eigen::SparseMatrix<double> A(10, 10);
A.setFromTriplets(coefficients.begin(), coefficients.end());

std::cout << A << "\n\n";

for (int i = 0; i < 3; i++) A.coeffRef(i,i) += 1;
std::cout << A << "\n\n";