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

returning the matrix block from a function

Tags: None
(comma "," separated)
bkot
Registered Member
Posts
11
Karma
0
What happens under the hood if I returned the result matrix from a method, e.g.
Code: Select all
struct C {
  Eigen::MatrixXd data;
  Eigen::MatrixXd f();
  void compute();
  int current_row;
};

void C::compute() {
  // do something
}

Eigen::MatrixXd C::f() {
  return data.block(0, 0, current_row+1, data.cols() );
}

int main(void) {
  C c;
  c.compute();
  Eigen::MatrixXd m = C.f();
  return 0;
}

What performance hit (memory allocation, etc.) do I get? Is this the right way of returning a matrix? Thanks.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
In this case the block will be copied into a temporary matrix. To avoid that, your function should return a Block<MatrixXd,Dynamic,Dynamic> object which is what data.block(...) returns.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], rblackwell