How can I calculate inverse of sparse matrix in Eigen library

10,322

Solution 1

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);

Solution 2

It's not mathematically meaningful.

A sparse matrix does not necessarily have a sparse inverse.

That's why the method is not available.

Share:
10,322
kujungmul
Author by

kujungmul

Updated on June 11, 2022

Comments

  • kujungmul
    kujungmul almost 2 years

    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.

  • MatthiasB
    MatthiasB over 9 years
    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.