I came across the following assigning for a matrix in Eigen Library here
Matrix3f m;
m << 1, 2, 3,
4, 5, 6,
7, 8, 9;
as an alternative way of the boring one (m(0,0) = 1;
... etc). My question is any considerations should I pay at attention to for using the first method? because I know any simplifications come at cost.
In the first case, m(0,0)=1
calls operator(Index, Index)
and operator=(const Scalar& s)
, which is probably pretty fast. Whereas m << 1,2, ...
calls the overloaded operator<<
and a chain of overloaded comma operator,(const Scalar& s)
, see the code here: http://eigen.tuxfamily.org/dox/CommaInitializer_8h_source.html
I would guess the second initialization is a bit slower, but unless you are initializing huge matrices by hand, it shouldn't make a difference. In any case, you cannot use the comma initialization to initialize in a loop, so the comma form is used only for small matrices (where you can really write the elements by hand).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With