Initialise Eigen :: vector with std :: vector [英] Initialise Eigen::vector with std::vector
问题描述
我已经看过它之前,但我不记得如何有效地初始化 Eigen :: Vector
已知长度的 std :: vector
的长度相同。这是一个很好的例子:
I have seen it done before but I cannot remember how to efficiently initialize an Eigen::Vector
of known length with a std::vector
of the same length. Here is a good example:
std::vector<double> v1 = {1.0, 2.0, 3.0};
Eigen::Vector3d v2; // Do I put it like this in here: v2(v1) ?
v2 << v1[0], v1[1], v1[2]; // I know you can do it like this but
// I am sure i have seen a one liner.
我已经阅读了 此 页面,但没有关于执行此操作的方法的清楚说明。
I have perused this page about advanced matrix initialization but there is not a clear explanation of the method to perform this action.
推荐答案
根据Eigen Doc,向量是Matrix的typedef,而且Matrix具有构造函数< a>具有以下签名:
According to Eigen Doc, Vector is a typedef for Matrix, and the Matrix has a constructor with the following signature:
Matrix (const Scalar *data)
构造一个用数据开始的系数初始化的固定大小的矩阵。
Constructs a fixed-sized matrix initialized with coefficients starting at data.
并且向量引用将 std :: vector :: data
定义为:
std::vector::data
T* data();
const T* data() const;
返回指向作为元素存储的底层数组的指针。
指针是这样的,范围 [data();
Returns pointer to the underlying array serving as element storage.
The pointer is such that range [data(); data() + size())
is always a
valid range, even if the container is empty.
$ b($ data()+ size())总是一个
有效范围。 $ b
所以,你可以传递向量的数据作为 Vector3d
构造函数参数:
So, you could just pass the vector's data as a Vector3d
constructor parameter:
Eigen::Vector3d v2(v1.data());
此外,从Eigen 3.2.8开始,上面提到的构造函数定义为:
Also, as of Eigen 3.2.8, the constructor mentioned above defined as:
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
inline Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>
::Matrix(const Scalar *data)
{
this->_set_noalias(Eigen::Map<const Matrix>(data));
}
如你所见,它还使用 Eigen: :Map
,如@ggael和@gongzhitaao所述。
As you can see, it also uses Eigen::Map
, as noted by @ggael and @gongzhitaao.
这篇关于Initialise Eigen :: vector with std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!