0

I try to use QR decomposition using Eigen, but the results get from the following tow methods is different, please help me to find out the error! Thanks.

// Initialize the sparse matrix
A.setFromTriplets(triplets.begin(), triplets.end());
A.makeCompressed();

//Dense matrix method
MatrixXd MatrixA = A;
HouseholderQR<MatrixXd> qr(MatrixA);
MatrixXd Rr = qr.matrixQR().triangularView<Upper>();

//Sparse matrix method
SparseQR < SparseMatrix < double >, COLAMDOrdering< int > > qr;
qr.compute(A);
SparseMatrix<double, RowMajor> Rr = qr.matrixR();
zhaolewen
  • 1
  • 1

1 Answers1

1

This is because SparseQR performs column reordering to both reduce fill-in and achieve a nearly rank-revealing decomposition, similar to ColPivHouseholderQR. More precisely, HouseholderQR computes: A = Q*R, whereas SparseQR computes: A*P = Q*R. So it is expected that the two R triangular factors are different.

ggael
  • 28,425
  • 2
  • 65
  • 71
  • Thanks for your explanation, then how shall I get the matrixR? I try to use: Rr = qr.matrixR()*qr.colsPermutation(); But it seems not correct – zhaolewen Jan 14 '17 at 01:34
  • You could compute `qr.matrixR()*qr.colsPermutation().inverse()` but the result won't be triangular anymore. I don't known what's your use case, but in most cases you adapt it to explicitly deal with the additional permutation. – ggael Jan 15 '17 at 21:04