I'm looking for a way to retrieve the inverse of a SparseMatrix using RowMajor storage with EIGEN 3.3.4. I found an answer to this question there, but I'm experiencing issues adapting it to my case.
The given way to do it is by using a Solver, and resolve Ax=I
(I is identity), to retrieve the inverse of A.
What I do not understand is the following :
auto A_inv = solver.solve(I);
Looks like, in the author case, the result of solver.solve()
is a SparseMatrix
object. In mine, it is a Solve
object and this is why I'm in trouble right now.
I'm using SimplicialLDLT
as follow :
SimplicialLDLT<SparseMatrix<short, RowMajor> solver;
solver.compute(A);
SparseMatrix<short,RowMajor> identity(A.rows(), A.cols()); identity.setIdentity();
auto result = solver.solve(identity);
std::cout<<"INVERSE MATRIX : "<<result<<std::endl;
Which does not work since result
is a Solve
(I guess there's no <<
overload). What I'm looking for is a way to do :
SparseMatrix<short,RowMajor> matrix_result = result;
For now, the result of this conversion is an error : THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES
.
The question is : How do you setup a RowMajor solver, and how do you retrieve the result of this in a usable RowMajor matrix?
Thanks in advance.
With ColMajor
Using ColMajor for both the solver and rhs (lhs is still rowmajor, I can't change it), the result is wrong. The matrix A is :
A = 1 -1 0
0 1 0
0 0 1
The code is :
SparseMatrix<short,RowMajor> A;
//set A values
SparseMatrix<short,ColMajor> I(A.rows(),A.cols());I.setIdentity();
SimplicialLDLT<SparseMatrix<short,ColMajor>> solver;
solver.compute(A);
SparseMatrix<short,ColMajor> result = solver.solve(I);
std::cout<<result<<std::endl;
Displaying the wrong following :
1 0 0
0 1 0
0 0 1
Where the expected result was :
1 1 0
0 1 0
0 0 1
I'm probably doing something wrong there but I can't figure it out.
Solution : Change the solver to SparseLU, SimplicialLDLT is only working for symmetric matrices.