The Eigen documentation is filled with examples illustrating how one should write a general function accepting a matrix:
template <typename Derived>
void print_cond(const MatrixBase<Derived>& a)
The reason to use MatrixBase
as opposed to Matrix
is that all dense Eigen matrix expressions derive from MatrixBase
. So, for instance, if I pass a block of a matrix
print_cond ( A.block(...));
Then using the signature const MatrixBase<Derived>& a
avoids creating a temporary. Conversely, if we had declared the function with the signature
template <typename T, int rows, int cols>
void print_cond(const Matrix<T,rows,cols>& a)
then Eigen would have to convert the block type to a matrix before passing it to the function, meaning that an unnecessary temporary would have to be created.
Please correct me if this understanding is incorrect...
With that in mind, one of the benefits of the second approach is that we can get compile time checks on the dimensions of the matrix (assuming they are fixed, not dynamic).
What I can't find in the documentation is an example with the generality of the first approach (which helps avoid temporary creation), but which has complie time checks on the type and dimensions of the matrix. Could somebody please tell me how to do that?
Derived
in the first example deduced toMatrix<T,rows,cols>
? – superA.block (...)
, it will be a different type. In Eigen the types can get very very complicated, so I am asking how to ensure the size and other matrix properties despite these complicated types. – bremen_mattRef
orMap
– bremen_mattT
,rows
, andcols
? And doesconst Ref<const ...>&
work for you? – chtz