# Matrix Inversion and Lapack

Hello FreeFem Community,

I’m looking to invert a square matrix A in order to solve the problem Ax=b. The problem is, this matrix A, has no reason to be invertible, so when it’s the case, I would like to compute the more-penrose pseudoinverse solution to the problem : min_{Y\in R^{n\times n} ||AY-I_n||.
I was going through the lapack FreeFem sources on Github (FreeFem-sources/lapack.cpp at 897a6ed0606d497b043b593e0d7db86448731889 · FreeFem/FreeFem-sources · GitHub) and found that we can call on the lapack function dgelsy from Freefem, which would take care of this.
I can’t seem to make it work though, as the following example returns an error message :
" Intel MKL ERROR: Parameter 5 was incorrect on entry to DGELSY."

real[int,int] A=[[1,1,1],[0,2,0],[1,1,1]];
real[int,int] B=[[1,0,0],[0,1,0],[0,0,1]];

dgelsy(A,B);

Can anyone please confirm if my calling of the function is correct? and in case dgelsy is not the way to go, are there other ways to compute pseudo-inverses?

Thank you so much for your time and help,

Salah

This is a bug, thank you for your report. It is fixed here: Wrong LDA in call to dgelsy. · FreeFem/FreeFem-sources@c508630 · GitHub. Now, you’ll get the proper B:

3 3
0.25 -0.25 0.25
4.394057034e-17 0.5 1.259510683e-16
0.25 -0.25 0.25


I am not familiar with how Github works, but my feeling is that I would have to rebuild FreeFem (installed initially through the dmg image) to get the changes, and inform the university to rebuild it on the cluster. I wanted to ask before I mess with my installation files if just correcting my source file would do the trick?

Salah,

If you are using the .dmg, then yes, you’d need to rebuild FreeFEM from the sources (there is probably a release coming up soon though if you are not in a rush).
For the installation on the cluster, the admin can just fetch the file on GitHub, and then recompile only the plugin.

$cd FreeFem-sources/plugin/seq$ ./ff-c++ -auto lapack.cpp


Assuming lapack.cpp as been overwritten by the latest version from GitHub. Then, the corresponding library (lapack.so) must be copied in the installation directory.

Thanks again! Have a good day.

Salah