Thin QR decomposition in c++

5.2k views Asked by At

Is there an easy to use c++ library for "thin" QR decomposition of a rectangular matrix?
Eigen seems to only support full Q matrices. I can take a full Q and discard some columns, but would it be more efficient to not compute them to begin with?

2

There are 2 answers

0
George Skoptsov On BEST ANSWER

Newmat does exactly what you want.

To decompose A into QR, you can do:

Matrix Q = A;
UpperTriangularMatrix R;
QRZ(Q, R)

If A is a 3x5 matrix, R will be 3x3 and Q will be 3x5 as well.

2
chtz On

Even though this question is a bit old, for the record: Eigen does not explicitly compute the Q matrix, but a sequence of Householder vectors, which can directly be multiplied with any matrix (with the correct number of rows).

If you actually explicitly want the thin Q matrix, just multiply by an identity-matrix of the desired size:

#include <Eigen/QR>
#include <iostream>

int main()
{
    using namespace Eigen;
    MatrixXf A(MatrixXf::Random(5,3));
    HouseholderQR<MatrixXf> qr(A);
    MatrixXf thinQ = qr.householderQ() * MatrixXf::Identity(5,3);
    std::cout << thinQ << '\n';
}