这是一个非 Eigen 用户可以回答的问题...
我想使用 Eigen API 在头文件中初始化常量矩阵,但 Eigen 似乎没有提供构造函数来实现这一点,以下是我尝试的方法:
// tried the following first, but Eigen does not provide such a constructor
//const Eigen::Matrix3f M<<1,2,3,4,5,6,7,8,9;
// then I tried the following, but this is not allowed in header file
//const Eigen::Matrix3f M;
//M <<1,2,3,4,5,6,7,8,9; // not allowed in header file
在头文件中实现此目的的替代方法是什么?
最佳答案
至少有两种可能性。第一个是使用 Eigen 的逗号初始化器功能:
Eigen::Matrix3d A((Eigen::Matrix3d() << 1, 2, 3, 4, 5, 6, 7, 8, 9).finished());
第二种是使用 Matrix3d(const double*)
构造函数从原始指针复制数据。在这种情况下,必须以与目标存储顺序相同的顺序提供值,因此在大多数情况下按列排列:
const double B_data[] = {1, 4, 7, 2, 5, 8, 3, 6, 9};
Eigen::Matrix3d B(B_data);
关于c++ - 在头文件中初始化常量特征矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25999407/