简体   繁体   English

用本征初始化向量C ++的矩阵

[英]Initializing matrix of vectors C++ with eigen

My final goal is to have a matrix where each element is a vector using the eigen module with c++, so that I can do a summation of matrices. 我的最终目标是使用带有c ++的特征模块得到一个矩阵,其中每个元素都是一个向量,这样我就可以对矩阵求和。 The data type I came up with is: 我想出的数据类型是:

Matrix<Vector3d,256,256> Matrix_A;

for a 256x256 matrix where each element is of data type Vector3D. 对于256x256矩阵,其中每个元素都是数据类型Vector3D。 This doesn't work.. Is this even possible? 这不起作用..这甚至可能吗?

If you read your compiler's error message you would have found something like: 如果您阅读编译器的错误消息,您会发现类似于:

error: static_assert failed "OBJECT_ALLOCATED_ON_STACK_IS_TOO_BIG"

Meaning that for such large objects you should move to a dynamically allocated matrix type: 这意味着对于这样大的对象,您应该移动到动态分配的矩阵类型:

int N = 256;
using Mat = Matrix<Vector3d,Dynamic,Dynamic>;
Mat A(N,N), B(N,N);
Mat C = A+B;

Eigen's Matrix template takes only a scalar type for the first template parameter (whereas the doc hints that it might be possible to extend supported types, it is not clear how): Eigen的Matrix模板仅为第一个模板参数采用标量类型(而doc提示可能扩展支持的类型,但不清楚如何):

The three mandatory template parameters of Matrix are: Matrix的三个必需模板参数是:

Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>

Scalar is the scalar type, ie the type of the coefficients. 标量是标量类型,即系数的类型。 That is, if you want a matrix of floats, choose float here. 也就是说,如果你想要一个浮点矩阵,请在这里选择float。 See Scalar types for a list of all supported scalar types and for how to extend support to new types. 有关所有支持的标量类型的列表以及如何扩展对新类型的支持,请参阅标量类型。

This means it is not possible to define a matrix of vectors. 这意味着不可能定义向量矩阵。 The only possibility I see is to use a std::vector of Eigen's Matrix objects: 我看到的唯一可能是使用Eigen的Matrix对象的std::vector

typedef Matrix<float,256,256> my_2dfmat;

std::vector<my_2dfmat> Matrix_A(3);

This does have some drawbacks, such as the indexing order being non-intuitive etc. 这确实有一些缺点,例如索引顺序不直观等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM