Armadillo element-wise operations only on diagonal

Viewed 22

Does armadillo offer functions that apply element-wise operations only on the diagonal elements of a matrix? Say I want to run an inverse on it then I get infs on off-diagonals. I know that I can get it with arma::inv(m), but for diagonal matrices it seems faster to run it element-wise.

Minimal example:

#include <iostream>
#include <armadillo>

int main() {
  arma::Col<double> v = {1.2, 3.14, 2.71, 0.1};
  arma::Mat<double> m = arma::diagmat(v);
  std::cout << " A diagonal matrix:\n" << m << "\n\n";
  std::cout << " It's elemenet-wise inverse:\n" << 1.0/m << std::endl;
  return 0;
}
0 Answers
Related