QR decomposition in Eigen C++ - sign problem

776 Views Asked by At

I need to compute some determinants for a project: I use c++ 14 and Eigen.

So, MatrixXd A is a Eigen matrix with X rows and X cols and contains double values. To compute determinant I use A.determinant(). Let's pretend that A.determinant() is equal to d. Then, the problem apper when I use QR decomposition because R.determinant() is equal to -d, should be equal to d. This happened only for large matrices (with size greater than 5 - I observed this). Only the sign is problem, why?

My code:

#include <iostream>
#include <Eigen>
#include <fstream>
#include <chrono>
using namespace Eigen;
using namespace std;
using namespace std::chrono;
ifstream fin("input.txt");

int main()
{
    double aux;
    int n = 10;
    MatrixXd A;
    A.resize(n,n);

    // Read A
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
            fin>>aux;
            A(i,j) = aux;
        }
    cout<<"Start!"<<endl;
    cout<<A.determinant()<<endl;


    //Use QR decomposition, get R matrix
    HouseholderQR<MatrixXd> qr(A);
    qr.compute(A);
    MatrixXd R = qr.matrixQR().template  triangularView<Upper>();

    // R is a triangular matrix, det(A) should be equal to det(R)
    cout<<R.determinant()<<endl;

    return 0;
}

How can I solve this? img

0

There are 0 best solutions below