Eigen's function of the day: pseudoInverse().
07 Dec 2021The pseudoinverse, sometimes more formally called the Moore-Penrose inverse ( Wikipedia ) is a general inverse for non-square matrices. When I encountered the pseudoinverse (denoted \(A^{\dagger}\) or \(A^{+}\)), we computed it this way in optimization class:
\[A^{\dagger} = (A^{T}A)^{-1}A^{T}\]Now, even for smaller matrices I am starting to avoid computing inverses for numerical robustness reasons, and while the pseudoinverse is not used for many things, in computer vision when you backproject an image pixel to create a ray into 3D space, I use the pseudoinverse of the camera calibration matrix.
Anyway, I ran into some numerical issues with the above and did some reading – Eigen provides a function for computing the pseudoinverse via a orthogonal decomposition, pseudoInverse().
Use it like so (reproduced from SO):
#include <Eigen/QR>
Eigen::MatrixXd A = .....
Eigen::MatrixXd pinv = A.completeOrthogonalDecomposition().pseudoInverse();
Additionally, at the Wikipedia page the subheading ‘Construction’ has good reading on various ways to compute the pseudoinverse.