将自动与Eigen结合使用会导致错误的结果 [英] Wrong results using auto with Eigen
问题描述
当使用两个向量求和时,使用auto和 Vector
会得到不同的结果。
我的代码:
#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);
返回0;
}
结果:
< blockquote>
resAuto = -24.9536,-29.3876,65.801
resVector3 = -24.9536,-29.3876,83.77
按任意键继续。 。
我知道Eigen会进行内部优化,从而产生不同的结果。但这看起来像是Eigen和C ++ 11中的错误。
自动
关键字告诉编译器根据 =
的右侧猜测最佳对象。您可以通过添加
std :: cout<< typeid(resAuto).name()<< std :: endl;
std :: cout<< typeid(resVector3).name()<< std :: endl;
到 foo
(不要忘记包括< typeinfo>
)。
在这种情况下,构造临时 Vector3之后
,将调用 operator +
方法,该方法将创建 CwiseBinaryOp
对象。此对象是Eigens 惰性评估的一部分(可以提高性能)。如果要强制进行急切的评估(并因此确定类型),则可以使用
const auto resAuto =(Ha + Vector3( 0.,0.,j * 2.567))。eval();
代替您在 foo
中的行。 / p>
一些注意事项:
-
Vector3
与Eigen中定义的Vector3d
类相同 - 您可以使用
#include< ; Eigen / Core>
而不是#include< Eigen / Geometry>
来包含大多数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.
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 theVector3d
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.
这篇关于将自动与Eigen结合使用会导致错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!