Reputation: 558
Suppose I have a Matrix <float, Dynamic, Dynamic, RowMajor> A
in Eigen
. When i write following code:
cout << "Number of Columns of A is: "<< A.cols() << endl;
cout << "Number of Rows of A is: "<< A.rows() << endl;
I get following result:
Number of Columns of A is: 129
Number of Rows of A is: 600
According to the above results, i expect when i write following code, i get Exception Error
but that does not happen and it print a value!!! WHY??!!
cout << A(500,140);
Upvotes: 0
Views: 1328
Reputation: 5624
As explained in Eigen docs, matrix coefficients can be accessed either via m(i,j)
or m.coeff(i,j)
/m.coeffRef(i,j)
(plus m[i]
and m.x(),...
for the special vector case).
Now, the 'm(i,j)' variant is range-checked unless the NDEBUG
or EIGEN_NO_DEBUG
macros are defined. Typically, the former macro is defined for 'release' builds, so no range checks will be performed in that case. The rationale is that Eigen is a performance oriented library, checks have a cost, so it makes sense enabling them for debugging purposes only.
The m.coeff(i,j)
form is never checked.
When no check is performed, any attempt to invoke a coefficient accessor out of range is a precondition violation, this means the behaviour is undefined.
Generally speaking, you should minimize the use of indexed access in favor of higher level, block/linear algebra operations (Eigen has plenty of them); your code will result more compact, more readable (well, at least, to an algebra-aware reader), more correct (less risk of out of range accesses) and (possibly much) faster.
Upvotes: 5