Get matrix views/blocks from a Eigen::VectorXd without copying (shared memory)

会有一股神秘感。 提交于 2019-11-29 22:53:04

问题


Does anyone know a good way how i can extract blocks from an Eigen::VectorXf that can be interpreted as a specific Eigen::MatrixXf without copying data? (the vector should contains several flatten matrices)

e.g. something like that (pseudocode):

VectorXd W = VectorXd::Zero(8);

// Use data from W and create a matrix view from first four elements
Block<2,2> A = W.blockFromIndex(0, 2, 2);
// Use data from W and create a matrix view from last four elements
Block<2,2> B = W.blockFromIndex(4, 2, 2);

// Should also change data in W
A(0,0) = 1.0
B(0,0) = 1.0

The purpose is simple to have several representations that point to the same data in memory.

This can be done e.g. in python/numpy by extracting submatrix views and reshape them.

A = numpy.reshape(W[0:0 + 2 * 2], (2,2))

I Don't know whether Eigen supports reshape methods for Eigen::Block.

I guess, Eigen::Map is very similar except it expects plain c-arrays / raw memory. (Link: Eigen::Map).

Chris


回答1:


If you want to reinterpret a subvector as a matrix then yes, you have to use Map:

Map<Matrix2d> A(W.data());          // using the first 4 elements
Map<Matrix2d> B(W.tail(4).data());  // using the last 4 elements
Map<MatrixXd> C(W.data()+6, 2,2);   // using the 6th to 10th elements
                                    // with sizes defined at runtime.


来源:https://stackoverflow.com/questions/21556965/get-matrix-views-blocks-from-a-eigenvectorxd-without-copying-shared-memory

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