Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong results using auto with Eigen

Tags:

c++11

eigen

I got different results using auto and using Vector when summing two vectors.

My code:

#include "stdafx.h"
#include <iostream>
#include "D:\externals\eigen_3_1_2\include\Eigen\Geometry"

typedef Eigen::Matrix<double, 3, 1>       Vector3;

void foo(const Vector3& Ha, volatile int j) 
{
    const auto resAuto = Ha + Vector3(0.,0.,j * 2.567);
    const Vector3 resVector3 = Ha + Vector3(0.,0.,j * 2.567);

    std::cout << "resAuto = " << resAuto <<std::endl;
    std::cout << "resVector3 = " << resVector3 <<std::endl;
}

int main(int argc, _TCHAR* argv[])
{
    Vector3 Ha(-24.9536,-29.3876,65.801);
    Vector3 z(0.,0.,2.567);

    int j = 7;

    foo(Ha,j);
    return 0;
}

The results:

resAuto = -24.9536, -29.3876,65.801

resVector3 = -24.9536,-29.3876,83.77

Press any key to continue . . .

I understand that Eigen does internal optimization that generate different results. But it looks like a bug in Eigen and C++11.

like image 888
Rotem Hayut Avatar asked Jun 28 '15 11:06

Rotem Hayut


1 Answers

The auto keyword tells the compiler to "guess" the best object based on the right hand side of the =. You can check the results by adding

std::cout << typeid(resAuto).name() <<std::endl;
std::cout << typeid(resVector3).name() <<std::endl;

to foo (don't forget to include <typeinfo>).

In this case, after constructing the temporary Vector3, the operator+ method is called, which creates a CwiseBinaryOp object. This object is part of Eigens lazy evaluation (can increase performance). If you want to force eager evaluation (and therefore type determination), you could use

const auto resAuto = (Ha + Vector3(0.,0.,j * 2.567)).eval();

instead of your line in foo.

A few side notes:

  • Vector3 is identical to the Vector3d class defined in Eigen
  • You can use #include <Eigen/Core> instead of #include <Eigen/Geometry> to include most of the Eigen headers, plus certain things get defined there that should be.
like image 176
Avi Ginsburg Avatar answered Nov 03 '22 04:11

Avi Ginsburg