Weird Behavior when using Eigen

2019-08-30 19:16发布

问题:

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

回答1:

The answer of George is correct but suffers from unnecessary copies. A better solution consists in mapping Q and R:

void get_QR(const double* A, int m, int n, double*& Q, double*& R) {
    Map<const MatrixXd> A_E(A, m, n);
    Map<MatrixXd> Q_E(Q, m, n);
    Map<MatrixXd> R_E(Q, n, n);

    HouseholderQR<MatrixXd> qr(A_E);
    Q_E = qr.householderQ()*(MatrixXd::Identity(m,n));
    R_E = qr.matrixQR().block(0,0,n,n).triangularView<Upper>();
}

In order to be able to reuse the get_QR function taking Eigen's object, then use Ref<MatrixXd> instead of MatrixXd:

void get_QR(Ref<const MatrixXd> A, Ref<MatrixXd> Q, Ref<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(const double* A, int m, int n, double* Q, double* R) {
    Map<const MatrixXd> A_E(A, m, n);
    Map<MatrixXd> Q_E(Q, m, n);
    Map<MatrixXd> R_E(R, n, n);
    get_QR(A_E, Q_E, R_E);
}

The Ref<MatrixXd> can wrap any Eigen's object that is similar to a MatrixXd without any copy. This include MatrixXd itself, as well as Map and Block expressions. This requires Eigen 3.2.



回答2:

I don't think it has anything to do with Eigen.

It looks like you are assigning pointer Q to a memory location belonging to a local variable Q_E

        Q       =       Q_E.data();

which leaks the previous memory allocation

double* Q       =       new double[m*n];

and is meaningless or undefined outside of the get_QR() function.

You should use memcpy instead:

memcpy(Q, Q_E.data(), m*n*sizeof(double));


标签: c++ eigen