What do the initialized Array constructors do?

耗尽温柔 提交于 2019-12-11 17:40:04

问题


In the Eigen documentation I see a lot of these:

Array (const Scalar &val0, const Scalar &val1)
Array (const Scalar &val0, const Scalar &val1, const Scalar &val2)
Array (const Scalar &val0, const Scalar &val1, const Scalar &val2, const Scalar &val3)

According to the documentation (http://eigen.tuxfamily.org/dox/classEigen_1_1Array.html) these constructors "constructs an initialized ND vector with given coefficients".

What does that mean? If I do Array(1,2,3), what is the result?

Specifically, what coefficients does it place in each dimension, and how large is the array in each of those respective dimensions? The constructor Array(1,2,3), according to the docs, should construct an 3D array, and initialise its contents with "the given coefficients". How should the result look?


回答1:


The constructors are for fixed sized Arrays. Assuming the declaration is Eigen::Array3i then the constructor you mentioned initializes a 1D int array with three elements initialized to the stated values.




回答2:


Those constructos allow you to create arrays of sizes up to 4:

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

Eigen will throw a compile error if you attempt to initialize like this with anything but an N(1-4) x 1 array. For example:

Eigen::Array<int, 1, 3> a(1, 2, 3) //Fine
Eigen::Array<int, 3, 1> a(1, 2, 3) //Fine
Eigen::Array<int, 3, 3> a(1, 2, 3) //Compile error

error: ‘THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE

I would not be initializing them like this though. There is a tutorial on Eigen initialization here which provides good suggestions for how to initialize arrays for eg:

Eigen::Array33 a;
a << 1, 2, 3, 4 ...

Which has documentation here.

Or you can use special initializations like:

Eigen::ArrayXXf a = Eigen::ArrayXXf::Zero(1, 4) //0, 0, 0, 0


来源:https://stackoverflow.com/questions/24502837/what-do-the-initialized-array-constructors-do

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