How can I calculate the inverse of a sparse matrix in the Eigen library

I have a question about the Eigen library in C ++. In fact, I want to calculate the inverse matrix of a sparse matrix. When I used the Dense matrix in Eigen, I can use the .inverse () operation to calculate the inverse of the dense matrix. But in the sparse matrix, I cannot find the inverse operation anywhere. Anyone who knows to calculate the inverse of a sparse matrix? Help me.

+5
source share
4 answers

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 am the identity matrix. If there is a solution, X will be your inverse matrix. In our own documentation there is a page about rare solvers and how to use them, but the main steps are as follows:

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

This is not mathematically significant.

A rare matrix does not necessarily have a sparse inverse.

This is why the method is not available.

+2
source

You can find an example about the inversion of the Sparse Complex Matrix.

I used the SimplicialLLT class,

You can find another class below.

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

This page can help you with the correct class name for your work (spead, precision 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; } 
0
source

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

0
source

Source: https://habr.com/ru/post/1202947/


All Articles