我需要为项目计算一些决定因素:我使用c ++ 14和Eigen。
因此,MatrixXd A是具有X行和X col的本征矩阵,并且包含双精度值。要计算行列式,我使用A.determinant()。假设A.determinant()等于d。然后,当我使用QR分解(因为R.determinant()等于-d)时,问题产生者应该等于d。这仅在大型矩阵(大小大于5的矩阵-我观察到了)中才发生。只有标志是问题,为什么?
我的代码:
#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;
}
我该如何解决? img