1
votes

In Eigen library, I know that there are visitors and reductions for dense Eigen::Matrix class which I can use efficiently to compute their 1-norm, inf-norm, etc. someway like this:

Eigen::MatrixXd A;
...
A.colwise().lpNorm<1>().maxCoeff();
A.rowwise().lpNorm<1>().maxCoeff();
// etc.

Now I have sparse Eigen::SparseMatrix class. How can I efficiently compute these norms in this case?

1

1 Answers

0
votes

You can compute the colwise/rowwise 1-norm using a product with a vector of ones:

(Eigen::RowVectorXd::Ones(A.rows()) * A.cwiseAbs()).maxCoeff();
(A.cwiseAbs() * Eigen::VectorXd::Ones(A.cols()).maxCoeff();

Check the generated assembly to see if this gets sufficiently optimized for your purpose. If not, or if you need other lpNorms, you may need to write two nested loops with sparse iterators.