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

writing a function that takes MatrixBase or Block

Tags: None
(comma "," separated)
hazelnusse
Registered Member
Posts
5
Karma
0
OS
Following the guidance here [0], I have:
Code: Select all
template <typename Derived>
void test(const Eigen::MatrixBase<Derived>& a)
{
  // Use case 1:
  Derived aa;
  // do stuff with aa

  // Use case 2:
  Derived bb = a;
  // do stuff with bb
}


If I create an Eigen::Matrix and call test like so, it works fine:
Code: Select all
Eigen::Matrix<double, 2, 4> A;
A << 1, 2, 3, 4,
        5, 6, 7, 8;
// Works
test(A);

However, the following doesn't work:
Code: Select all
test(A.block<2, 1>(0, 1));

However, the following does work:
Code: Select all
test(A.block<2, 1>(0, 1).eval());

Is .eval() the right approach here? Should I being doing something else at the call site, or in my function definition to better handle this? Does the return value of `.block<2, 1>(0, 1)` constitute a "plain matrix or vector (not an expression)" [1] or not, so that I can be certain this isn't creating a useless copy?

Sincerely,
Luke

[0] https://eigen.tuxfamily.org/dox/TopicFu ... Types.html
[1] https://eigen.tuxfamily.org/dox/classEi ... ml#title39
hazelnusse
Registered Member
Posts
5
Karma
0
OS
The solution I landed upon was to change to this:
Code: Select all
  // Use case 1:
  Eigen::Matrix<typename Derived::Scalar, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime>  aa;
  // do stuff with aa

  // Use case 2:
  Eigen::Matrix<typename Derived::Scalar, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime> bb = a;
  // do stuff with bb


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell