#StackBounty: #c++ #eigen #eigen3 Eigen: function signature which accepts general matrix expression of fixed size and type

Bounty: 50

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?

Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.