Thin QR decomposition in c++

倖福魔咒の 提交于 2019-12-04 14:43:20

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.

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';
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!