1
votes

Using the Eigen library in C++, given a sparse matrix A, what is the most efficient way (row-wise operations? how to?) to compute a sparse matrix B such that B(i, j) = A(i, j) / A(i, i) ? That is, divide each row i by the corresponding diagonal element A(i, i). It would be helpful to know how to do it both in-place (replacing entries in A) and out-of-place (creating a new sparse matrix B).

My sparse matrix is defined as:

typedef double Real;
typedef Eigen::SparseMatrix<Real> SparseMatrixR;

Thank you,
m.

1

1 Answers

3
votes

In other words you want to extract the diagonal of A, view it as a diagonal matrix, and apply its inverse to A:

A = A.diagonal().asDiagonal().inverse() * A;

This operation should be slightly more efficient if A is rowmajor.