This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Fixed size matrix in templated function call

Tags: None
(comma "," separated)
damien_d
Registered Member
Posts
15
Karma
0
Dear all,

I have been reading the documentation at http://eigen.tuxfamily.org/dox-devel/To ... Types.html but there is a topic that is missing. I would like to create a temporay matrix inside a templated function of a size different to any of which that are passed in. For example:
Code: Select all
template <typename Derived 1, typename Derived2>
void Foo(MatrixBase<Derived1>& y, const MatrixBase<Derived1>& x, const MatrixBase<Derived2>& A)
{
    // How do I declare B in terms of x.rows() and A.cols()?
    Matrix<rows of x, cols of A> B = SomeVeryExpensiveFunctionCall();
    y = x + B * A * B.transpose();
}


Previously, I have been passing in matricies of specific sizes, but I would like to use the generic functionality.

Thanks in advance,
Damien.
jitseniesen
Registered Member
Posts
204
Karma
2
Every Eigen type has constants RowsAtCompileTime and ColsAtCompileTime, which are set to the number of rows and columns (or to Dynamic for types with a dynamic size). Similarly, there is a typedef Scalar which is the type of the entries that make up the matrix. Thus, if B should have the same entries as A, you can use you can use
Code: Select all
 Matrix<Derived1::RowsAtCompileTime, Derived2::ColsAtCompileTime, typename Derived2::Scalar> B = ...
Alternatively, you can just use a dynamic size matrix for B. If your expensive function call is really expensive, the extra overhead of dynamic size is negligible, and getting the template parameters right can sometimes be tricky.
damien_d
Registered Member
Posts
15
Karma
0
This works, very nicely, but one comment: change

Code: Select all
 Matrix<Derived1::RowsAtCompileTime, Derived2::ColsAtCompileTime, typename Derived2::Scalar>


To:

Code: Select all
 Matrix<typename Derived2::Scalar, Derived1::RowsAtCompileTime, Derived2::ColsAtCompileTime>


Thanks!


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]