# Pseudo Inverse of singular matrix

**URL:** https://community.freefem.org/t/pseudo-inverse-of-singular-matrix/230
**Category:** General Discussion
**Created:** [January 29, 2020, 5:19pm UTC](https://community.freefem.org/t/pseudo-inverse-of-singular-matrix/230 "2020-01-29T17:19:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hraf5](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hraf5](https://community.freefem.org/u/hraf5)
#### Post date: [January 29, 2020, 5:19pm UTC](https://community.freefem.org/t/pseudo-inverse-of-singular-matrix/230/1 "2020-01-29T17:19:04Z")

</div>

Hello every one,

How to calculate the pseudoInverse of a singular matrix with FreeFem++?

Best regards.

---

<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: [January 29, 2020, 5:29pm UTC](https://community.freefem.org/t/pseudo-inverse-of-singular-matrix/230/2 "2020-01-29T17:29:54Z")

</div>

Uset PETSc, with the following options: `-pc_type lu -pc_factor_mat_solver_type mumps -mat_mumps_icntl_24 1`. The pseudo-inverse is not unique, this will compute _a_ pseudo-inverse inverse. You can lookup the option ICNTL(24) from the MUMPS [guide](http://mumps.enseeiht.fr/doc/userguide_5.2.1.pdf).

---

<div class="post-metadata">

### Author: ![hraf5](https://avatars.discourse-cdn.com/v4/letter/h/8baadc/32.png) [@hraf5](https://community.freefem.org/u/hraf5)
#### Post date: [February 6, 2020, 11:15am UTC](https://community.freefem.org/t/pseudo-inverse-of-singular-matrix/230/3 "2020-02-06T11:15:15Z")

</div>

Hello prj,

First of all, I would like to thank you for your response.

I tried to work with the PETSc package. I wrote the code below, but I can’t upload the PETSc package. Could you please tell me how to upload the PETSc package and if possible tell me your opinion about the code?

Thank you very much.

_Code :_  
load “PETSc”

// Matrix  
int n = 16;  
real[int, int] MM(n, n), A(n, n);

cout \<\< "read the matrix MM "\<\< endl;   
{  
ifstream file1(“MM.dat”);  
for(int i=0;i\<16 ; i++){  
for(int j=0;j\<16 ; j++){  
file1 \>\> MM(i,j) ;  
}  
}  
}

//Compute the Pseudo inverse of MM  
set(MM, sparams = “-pc\_type lu -pc\_factor\_mat\_solver\_type mumps -mat\_mumps\_icntl\_24 1”);

---

<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: [February 6, 2020, 11:51am UTC](https://community.freefem.org/t/pseudo-inverse-of-singular-matrix/230/4 "2020-02-06T11:51:11Z")

</div>

The code looks good. You probably don’t have the `PETSc` package installed with FreeFEM. You need to fix that of you want to compute pseudo-inverses.

---

<div class="post-metadata">

### Author: ![tea](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@tea](https://community.freefem.org/u/tea)
#### Post date: [November 8, 2024, 1:00pm UTC](https://community.freefem.org/t/pseudo-inverse-of-singular-matrix/230/6 "2024-11-08T13:00:04Z")

</div>

Hello prj,

is there a way to compute the pseudo-inverse which gives the solution with the smallest norm?

---

<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: [November 10, 2024, 8:56am UTC](https://community.freefem.org/t/pseudo-inverse-of-singular-matrix/230/7 "2024-11-10T08:56:43Z")

</div>

With `-pc_type svd`, but that will be extremely slow.
