How do you change a Block in Eigen?

◇◆丶佛笑我妖孽 提交于 2020-01-07 07:40:20

问题


I'm having trouble reassigning a Block. In the code below I store the matrix A in two different ways:

  1. as 3 ArrayXds, one for each row
  2. as an ArrayXXd

.

// data

ArrayXXd A (3, 3);
A << 0, 1, 2, 3, 4, 5, 6, 7, 8;

std::vector<ArrayXd> A_rows = {A.row(0), A.row(1), A.row(2)};

// std::vector<ArrayXd> solution

// first row
ArrayXd & current_row = A_rows[0];
// read it, write it, do stuff
// start working with the second row
current_row = std::ref(A_rows[1]);
cout << current_row << endl << endl; // prints 3 4 5
cout << A << endl; // A is unchanged

// Eigen solution

// first row
Block<ArrayXXd, 1, -1> && current_row_block = A.row(0);
// read it, write it, do stuff
// start working with the second row
current_row_block = std::ref(A.row(1)); // doesn't compile
cout << current_row_block << endl;
cout << A << endl;

The error message is:

error: use of deleted function 'void std::ref(const _Tp&&) [with _Tp = Eigen::Block<Eigen::Array<double, -1, -1>, 1, -1, false>]'
 current_row_block = std::ref(A.row(1));
                                      ^

Is it possible to fix the second approach or should I move to storing the matrix as std::vector<ArrayXd>?

Related question: Passing a reference of a vector element to a threaded function


回答1:


You don't need a Block<...> to reference a row. You only need an index.

int current_row_id = 0;
std::out << A.row(current_row_id) << std::end;
current_row_id = 1;
std::out << A.row(current_row_id) << std::end;

For your std::vector<ArrayXd> approach, as you are make copies of the rows, you can not change the original A.



来源:https://stackoverflow.com/questions/38705842/how-do-you-change-a-block-in-eigen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!