# Matrix Inversion and Lapack

**URL:** <https://community.freefem.org/t/matrix-inversion-and-lapack/1262>\
**Category:** General Discussion\
**Created:** [October 18, 2021, 11:08am UTC](https://community.freefem.org/t/matrix-inversion-and-lapack/1262 "2021-10-18T11:08:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![salah](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@salah](https://community.freefem.org/u/salah)\
**Post date:** [October 18, 2021, 11:08am UTC](https://community.freefem.org/t/matrix-inversion-and-lapack/1262/1 "2021-10-18T11:08:47Z")

</div>

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](https://github.com/FreeFem/FreeFem-sources/blob/897a6ed0606d497b043b593e0d7db86448731889/plugin/seq/lapack.cpp)) 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."

* * *

load “lapack”

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

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [October 18, 2021, 11:27am UTC](https://community.freefem.org/t/matrix-inversion-and-lapack/1262/2 "2021-10-18T11:27:39Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![salah](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@salah](https://community.freefem.org/u/salah)\
**Post date:** [October 18, 2021, 5:22pm UTC](https://community.freefem.org/t/matrix-inversion-and-lapack/1262/3 "2021-10-18T17:22:19Z")

</div>

Thank you so much for your quick reply,

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,

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [October 18, 2021, 6:05pm UTC](https://community.freefem.org/t/matrix-inversion-and-lapack/1262/4 "2021-10-18T18:05:04Z")

</div>

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.

```auto
$ 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.

---

<div class="post-metadata">

**Author:** ![salah](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@salah](https://community.freefem.org/u/salah)\
**Post date:** [October 19, 2021, 8:57am UTC](https://community.freefem.org/t/matrix-inversion-and-lapack/1262/5 "2021-10-19T08:57:25Z")

</div>

Thanks again! Have a good day.

Salah
