# What is the best pseudo inverse (Moore-Penrose inverse) subroutine?

**URL:** <https://fortran-lang.discourse.group/t/what-is-the-best-pseudo-inverse-moore-penrose-inverse-subroutine/3936>\
**Category:** Uncategorized\
**Created:** [July 6, 2022, 7:57am UTC](https://fortran-lang.discourse.group/t/what-is-the-best-pseudo-inverse-moore-penrose-inverse-subroutine/3936 "2022-07-06T07:57:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [July 6, 2022, 7:57am UTC](https://fortran-lang.discourse.group/t/what-is-the-best-pseudo-inverse-moore-penrose-inverse-subroutine/3936/1 "2022-07-06T07:57:03Z")

</div>

Dear all,

Pseudo inverse (Moore-Penrose inverse), usually is called `pinv` in Matlab or some Python library, etc.  
In Fortran, what is the best pseudo inverse (Moore-Penrose inverse) subroutine?

Thanks much in advance!

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [July 6, 2022, 8:14am UTC](https://fortran-lang.discourse.group/t/what-is-the-best-pseudo-inverse-moore-penrose-inverse-subroutine/3936/2 "2022-07-06T08:14:42Z")

</div>

[DGELSD](https://www.netlib.org/lapack/explore-html/d7/d3b/group__double_g_esolve_ga94bd4a63a6dacf523e25ff617719f752.html) computes the minimum-norm solution to a linear least squares problem for GE matrices.

If you really do need the inverse (see my reply in [Why doesn't Fortran have a built-in matrix inverse function? - #4 by ivanpribec](https://fortran-lang.discourse.group/t/why-doesnt-fortran-have-a-built-in-matrix-inverse-function/2705/4)), then do your [SVD](https://www.intel.com/content/www/us/en/develop/documentation/onemkl-developer-reference-fortran/top/lapack-routines/lapack-least-squares-and-eigenvalue-problem/lapack-least-square-eigenvalue-problem-computation/singular-value-decomposition-lapack-computation.html) / [QR / LQ](https://www.intel.com/content/www/us/en/develop/documentation/onemkl-developer-reference-fortran/top/lapack-routines/lapack-least-squares-and-eigenvalue-problem/lapack-least-square-eigenvalue-problem-computation/orthogonal-lapack-computational-routines.html) factorization first and then solve A X = I.

Note for example the Eigen C++ library has a [complete orthogonal decomposition](https://eigen.tuxfamily.org/dox/classEigen_1_1CompleteOrthogonalDecomposition.html#ab5e8b3f2c7b602772e1f1d7ce63d446e) routine, which also provides a pseudo inverse method, however the documentation states explicitly:

> Do not compute `this->pseudoInverse()*rhs` to solve a linear systems. It is more efficient and numerically stable to call `this->solve(rhs)` .

---

<div class="post-metadata">

**Author:** ![DavidB](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@DavidB](https://fortran-lang.discourse.group/u/DavidB)\
**Post date:** [July 6, 2022, 10:45am UTC](https://fortran-lang.discourse.group/t/what-is-the-best-pseudo-inverse-moore-penrose-inverse-subroutine/3936/3 "2022-07-06T10:45:48Z")

</div>

> [@ivanpribec](#):
>
> [DGELSD](https://www.netlib.org/lapack/explore-html/d7/d3b/group__double_g_esolve_ga94bd4a63a6dacf523e25ff617719f752.html) computes the minimum-norm solution to a linear least squares problem for GE matrices.

Yes. Use one of the LAPACK suite of solvers.

If performance is an issue then make sure that you link to a [BLAS](https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms) implementation that is optimized for your platform and compiler.

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [November 27, 2024, 5:40pm UTC](https://fortran-lang.discourse.group/t/what-is-the-best-pseudo-inverse-moore-penrose-inverse-subroutine/3936/4 "2024-11-27T17:40:07Z")

</div>

Dear all, sorry to revive this old thread -

I thought it’d be useful to link to my proposed implementation of the pseudo-inverse  
[at stdlib](https://github.com/fortran-lang/stdlib/pull/899) here for those interested.

Looking forward to your comments!

---

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [November 27, 2024, 11:57pm UTC](https://fortran-lang.discourse.group/t/what-is-the-best-pseudo-inverse-moore-penrose-inverse-subroutine/3936/5 "2024-11-27T23:57:57Z")

</div>

Thanks! Exciting!

> <https://github.com/fortran-lang/stdlib/pull/899/commits/f5de00f5b5337e6af7080c167ae43c6e78e26596>

I have a very basic and irrelevant question.  
It looks like you write the code in fypp file. What IDE or code editor do you use to write code in fypp file?  
I mean if I write f90 file I use visual studio, and it can easily compile and build the code and show errors messages if any, and I can easily locate to the lines which contain errors. For fypp, is there a IDE like visual studio? Thanks!

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [November 28, 2024, 7:21am UTC](https://fortran-lang.discourse.group/t/what-is-the-best-pseudo-inverse-moore-penrose-inverse-subroutine/3936/6 "2024-11-28T07:21:06Z")

</div>

Thank you @CRquantum, I use the Code::Blocks IDE. I have no experience with VS, but I think this trick applies to basically any editors: just set the syntax to Fortran for files with extension `.fypp`, that does the job for me (including linting, etc.). For preprocessing `fypp` files into `.f90`, you’ll have to script your IDE to call `fypp` behind the scenes though.
