1

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

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
dacian
  • 95
  • 1
  • 8
  • The `R` matrix will always have only positive (actually non-negative) numbers on the diagonal and therefore a non-negative determinant. To get the sign of the determinant, you need to check the determinant of `Q` -- this could be determined by keeping track of how many reflections happened to compute `Q` (but IIRC Eigen does not do that). – chtz Jul 05 '20 at 14:56
  • It looks to me that these routines return the determinant. However as the documentation notes determinants can get outlandishly big or small for even quite nice matrices, and you'd be better to use their logAbsDterminant method. – dmuir Jul 06 '20 at 15:27

0 Answers0