Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen Library assigning Matrix's elements?

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.

like image 386
CroCo Avatar asked May 24 '14 05:05

CroCo


Video Answer


1 Answers

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).

like image 68
vsoftco Avatar answered Sep 23 '22 15:09

vsoftco