Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen vector constructor initialization vs comma initialization

For Eigen vectors of a fixed size (eg Eigen::Vector3d, Eigen::Vector4f) there is the option to initialize the vector using the constructor as follows:

Eigen::Vector3d a(0.0, 1.0, 2.0);

However, Eigen also offers a way to use comma initialization of a general Eigen matrix that can be used in this case:

Eigen::Vector3d b;
b << 0.0, 1.0, 2.0;

Is one of the two methods preferable for speed or some other reasons? Or are they equal?

like image 328
Manumerous Avatar asked Dec 09 '25 18:12

Manumerous


1 Answers

One advantage of the first version is that it will fail at compile time if you pass the wrong number of arguments, e.g. because you misstyped Vector2d as Vector3d.

Performance-wise, the compiler is able to optimize both the same. Checked it with GCC.

like image 65
Homer512 Avatar answered Dec 12 '25 13:12

Homer512



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!