How can I calculate inverse of sparse matrix in Eigen library
Asked Answered
L

5

10

I have a question about Eigen library in C++. Actually, I want to calculate inverse matrix of sparse matrix. When I used Dense matrix in Eigen, I can use .inverse() operation to calculate inverse of dense matrix. But in Sparse matrix, I cannot find inverse operation anywhere. Does anyone who know to calculate inverse of sparse matrix? help me.

Latterly answered 19/9, 2014 at 8:14 Comment(3)
Put in a couple of more tags to attract possibly more comprehensive answers.Sixgun
But my terse answer does stand!Sixgun
Why do you need the inverse in an explicit form? This is extremely expensive, both in terms of computation and memory, and this is numerally instable (unless your matrix is nearly unitary). If you want to compute A^1 * B, then use the solve method of SparseLU.Mccammon
L
10

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);
Lamellibranch answered 19/9, 2014 at 8:45 Comment(0)
S
2

It's not mathematically meaningful.

A sparse matrix does not necessarily have a sparse inverse.

That's why the method is not available.

Sixgun answered 19/9, 2014 at 8:22 Comment(1)
This holds also true for dense matrices, mathematically there is no difference. It's more that it's costly to do so, especially as sparse matrices tend to be big.Lamellibranch
H
1

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

Hf answered 6/12, 2016 at 17:58 Comment(0)
P
1

Be warned that the inverse of a sparse matrix is not necessarily sparse, so if you're working with large matrices (which is likely, if you're using sparse representations) then this is going to be expensive. Think carefully about whether you really need the actual matrix inverse. If you're going to use the matrix inverse to solve a system of equations, then you don't need to actually compute the matrix inverse and multiply it out (use the method typically named solve and supply the right-hand-side of the equation). If you need the inverse of the Fisher matrix for covariances, try to approximate.

Passionate answered 11/5, 2021 at 17:56 Comment(0)
E
0

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;
 }
Evulsion answered 17/2, 2015 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.