To compute the inverse of a matrix using Eigen, you can use the inverse() function, which is defined for both dense and sparse matrices. Here are a few examples:

  1. Dense matrix:
#include <Eigen/Dense>
#include <iostream>

using namespace Eigen;
using namespace std;

int main() {
  Matrix3d A;
  A << 1, 2, 3,
       4, 5, 6,
       7, 8, 9;

  // Compute the inverse of A
  Matrix3d Ainv = A.inverse();

  cout << "Ainv = " << endl << Ainv << endl;
  // Output: Ainv = (-3.541, 2.122, -0.470)
  //                (-4.902, 3.536, -0.471)
  //                (4.290, -2.664, 0.235)

  return 0;
}
  1. Sparse matrix:
#include <Eigen/Sparse>
#include <iostream>

using namespace Eigen;
using namespace std;

int main() {
  SparseMatrix<double> A(3, 3);
  A.insert(0, 0) = 1;
  A.insert(1, 1) = 2;
  A.insert(2, 2) = 3;
  A.makeCompressed();  // Optimize the storage of the non-zero elements

  // Compute the inverse of A
  SparseMatrix<double> Ainv = A.inverse();

  cout << "Ainv = " << endl << Ainv << end
  return 0; 
}

Posted

in

, , ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *