Search code examples
matrixlinear-algebraeigeneigen3matrix-inverse

Eigen: inverting rectangular matrix (least norm solution)


I have very simple question but unfortunately i cannot find answer in Eigen documentation

I have a "fat" matrix A (number of rows is less than number of cols) and i want to find least norm pseudoinverse of this matrix.

Ideally i want to find it via least norm QR decomposition as specified in this slides.

According slides i can use straight-forward approach to do this by using this formula

A.transpose() * (A * A.transpose()).inverse()

But i hope that there is more elegant solution in Eigen

PS Sorry for my English


Solution

  • If A is full rank then your formula is correct and you can also obtain it from the HouseholderQR decomposition of A.transpose():

    MatrixXd A(3,6);
    A.setRandom();
    HouseholderQR<MatrixXd> qr(A.transpose());
    MatrixXd pinv;
    pinv.setIdentity(A.cols(), A.rows());
    pinv = qr.householderQ() * pinv;
    pinv = qr.matrixQR().topLeftCorner(A.rows(),A.rows()).triangularView<Upper>().transpose().solve<OnTheRight>(pinv);
    

    If not, then you'll have to use Eigen::CompleteOrthogonalDecomposition that is much simpler to use because it's main purpose is to solve minimal-norm problems:

    CompleteOrthogonalDecomposition<MatrixXd> cqr(A);
    pinv = cqr.pseudoInverse();