Niklas
Niklas

Reputation: 190

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. 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. This doesn't work.. Is this even possible?

Upvotes: 4

Views: 1324

Answers (2)

ggael
ggael

Reputation: 29265

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;

Upvotes: 4

mahesh
mahesh

Reputation: 1098

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):

The three mandatory template parameters of Matrix are:

Matrix<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>

Scalar is the scalar type, i.e. the type of the coefficients. That is, if you want a matrix of floats, choose float here. 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:

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.

Upvotes: 2

Related Questions