Search code examples
c++eigen

How do I construct a dynamic Array from an EigenBase?


I want to construct an Eigen::Array<double,Dynamic,Dynamic> type from an EigenBase type. According to the documentation I should be able to use the copy constructor using another array, for example:

http://eigen.tuxfamily.org/dox/classEigen_1_1Array.html#a0b2d2aba2e64b58c980399838f60205c

So I tried the following:

#include <iostream>
#include <Eigen/Dense>
int main()
{
    Eigen::Array<double,2,2> a(1,2,3,4);
    Eigen::Array<double,Eigen::Dynamic,Eigen::Dynamic> b(a);
    return 0;
}

But I got this error:

error: static assertion failed: THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE

I mean, the message is pretty clear, but my question is how can I achieve this kind of functionality, short of manually resizing the array and filling it piece by piece?


Solution

  • As shown by your previous question here: What do the initialized Array constructors do?

    You cannot initialize a 4 long array like this:

    Eigen::Array<double,2,2> a(1,2,3,4);
    

    your choices here are:

    Eigen::Array<double,1,4> a(1,2,3,4);
    Eigen::Array<double,4,1> a(1,2,3,4);
    

    Again you would be better off initializing the array like this:

    Eigen::Array<double,2,2> a;
    a << 1, 2, 3, 4;