This can be done in two ways:
- With the interface with lapack if it has been installed correctly on your computer.
You have to invokeload "lapack", then the inverse is called with^-1. It works on an array with two indices (not on a sparse matrix), thus if you start from a sparse matrix you have to convert it to an array.
Example (from Getting Problem to get Superconvergence results in Hybrid Higher Order (HHO) Method in Poisson Equation - #6 by fb77 HHO-reduced.edp line 91). IfAis a square sparse matrix of sizens
real[int,int] Arr(ns,ns);
real[int,int] Arrinv(ns,ns);
//Arr=A;//not possible in this form
for (int i=0;i<ns;i++){
for (int j=0;j<ns;j++){
Arr(i,j)=A(i,j);
}
}
Arrinv=Arr^-1;//inverse with lapack
matrix Ainv=Arrinv;//recover a sparse matrix
- You can solve successively the system
Ax=bforbbase element. IfAis a square sparse matrix of sizens
matrix Ainv(ns,ns);
for (int j=0;j<ns;j++){
real[int] vec(ns);
vec=0.;
vec(j)=1.;
real[int] solvec=A^-1*vec;
for (int i=0;i<ns;i++){
Ainv(i,j)=solvec(i);
}
}
In any case, this will be possible only if the matrix is small.