I am writing a wrapper to Eigen for my personal use and I encountered the following weird behavior:
void get_QR(MatrixXd A, MatrixXd& Q, MatrixXd& R) {
HouseholderQR<MatrixXd> qr(A);
Q = qr.householderQ()*(MatrixXd::Identity(A.rows(),A.cols()));
R = qr.matrixQR().block(0,0,A.cols(),A.cols()).triangularView<Upper>();
}
void get_QR(double* A, int m, int n, double*& Q, double*& R) {
// Maps the double to MatrixXd.
Map<MatrixXd> A_E(A, m, n);
// Obtains the QR of A_E.
MatrixXd Q_E, R_E;
get_QR(A_E, Q_E, R_E);
// Maps the MatrixXd to double.
Q = Q_E.data();
R = R_E.data();
}
Below is the test:
int main(int argc, char* argv[]) {
srand(time(NULL));
int m = atoi(argv[1]);
int n = atoi(argv[2]);
// Check the double version.
double* A = new double[m*n];
double* Q;
double* R;
double RANDMAX = double(RAND_MAX);
// Initialize A as a random matrix.
for (int index=0; index<m*n; ++index) {
A[index] = rand()/RANDMAX;
}
get_QR(A, m, n, Q, R);
std::cout << Q[0] << std::endl;
// Check the MatrixXd version.
Map<MatrixXd> A_E(A, m, n);
MatrixXd Q_E(m,n), R_E(n,n);
get_QR(A_E, Q_E, R_E);
std::cout << Q[0] << std::endl;
}
I get different values of Q[0]. For instance, I get "-0.421857" and "-1.49563".
Thanks