从Eigen :: VectorXd获取矩阵视图/块,无需复制(共享内存) [英] Get matrix views/blocks from a Eigen::VectorXd without copying (shared memory)
问题描述
有谁知道一个很好的方式如何我可以从一个Eigen :: VectorXf提取块,可以解释为一个特定的Eigen :: MatrixXf,而不复制数据? (向量应该包含几个扁平矩阵)
例如。 (伪代码):
VectorXd W = VectorXd :: Zero
//使用来自W的数据并从前四个元素创建矩阵视图
块< 2,2> A = W.blockFromIndex(0,2,2);
//使用来自W的数据并从最后四个元素创建矩阵视图
Block< 2,2> B = W.blockFromIndex(4,2,2);
//也应更改W $ b中的数据$ b A(0,0)= 1.0
B(0,0)= 1.0
目的很简单,有多个表示指向内存中的相同数据。
这可以做例如在python / numpy通过提取子矩阵视图和重塑它们。
A = numpy.reshape(W [0:0 + 2 * 2],(2,2))
我不知道Eigen是否支持Eigen :: Block的重塑方法。
我猜,Eigen :: Map非常相似,除了它期望平原c数组/原始内存。
(链接: Eigen :: Map )。
Chris
解决方案如果要将子矢量重新解释为矩阵,使用地图:
地图< Matrix2d> A(W.data()); // using the前4个元素
Map< Matrix2d> B(W.tail(4).data()); // using the last 4 elements
Map< MatrixXd> C(W.data()+ 6,2,2); //使用第6到第10个元素
//在运行时定义大小。
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
解决方案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.
这篇关于从Eigen :: VectorXd获取矩阵视图/块,无需复制(共享内存)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!