How can I calculate inverse of sparse matrix in Eigen library

被刻印的时光 ゝ 提交于 2019-12-03 14:58:12

You cannot do it directly, but you can always calculate it, using one of the sparse solvers. The idea is to solve A*X=I, where I is the identity matrix. If there is a solution, X will be your inverse matrix. The eigen documentation has a page about sparse solvers and how to use them, but the basic steps are as follows:

SolverClassName<SparseMatrix<double> > solver;
solver.compute(A);
SparseMatrix<double> I(n,n);
I.setIdentity();
auto A_inv = solver.solve(I);

It's not mathematically meaningful.

A sparse matrix does not necessarily have a sparse inverse.

That's why the method is not available.

You can find a example about inverse of Sparse Complex Matrix

I used of SimplicialLLT class,

you can find other class from bellow

http://eigen.tuxfamily.org/dox-devel/group__TopicSparseSystems.html

This page can help you with proper class name for your work (spead, accuracy and dimmenssion of your Matrix)

//////////////////////   In His Name  \\\\\\\\\\\\\\\\\\\\\\\\\\\
#include <iostream>
#include <vector>
#include <Eigen/Dense>
#include <Eigen/Sparse>

using namespace std;
using namespace Eigen;

int main()
 {
    SparseMatrix< complex<float> > A(4,4);

    for (int i=0; i<4; i++) {
      for (int j=0; j<4; j++) {
         A.coeffRef(i, i) = i+j;
      }
   }
  A.insert(2,1) = {2,1};
  A.insert(3,0) = {0,0};
  A.insert(3,1) = {2.5,1};
  A.insert(1,3) = {2.5,1};

   SimplicialLLT<SparseMatrix<complex<float> > > solverA;
   A.makeCompressed();
   solverA.compute(A);

   if(solverA.info()!=Success) {
     cout << "Oh: Very bad" << endl;
   }

   SparseMatrix<float> eye(4,4);
   eye.setIdentity();

   SparseMatrix<complex<float> > inv_A = solverA.solve(eye);

   cout << "A:\n" << A << endl;
   cout << "inv_A\n" << inv_A << endl;
 }

A small extension on @Soheib and @MatthiasB's answers, if you're using Eigen::SparseMatrix<float> it's better to use SparseLU rather than SimplicialLLT or SimplicialLDLT, they produced wrong answers with me on float matrices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!