我的大部分代码都使用 Eigen,但我想使用 GSL 的 Miser 或 Vegas monte-carlo 集成。我需要将 Eigen 的 vector 转换为 double
的 c 数组
最好的方法是什么?
Matrix<double,3,1> --> c_array []
最佳答案
我以前和 Eigen 一起工作过...
通常,用于简单访问内部数组数据,如mentioned by user janneb in this thread ,你只想invoke data() :
Vector3d v;
// Operations to add values to the vector.
double *c_ptr = v.data();
如果您希望迭代单个值以执行某些操作,则需要迭代每一行(.row(index))和列(.col(index)),取决于您要在目标 vector 中放置的矩阵顺序。
在您的特定示例中,您只需要迭代行:
Matrix<double,3,1> --> c_array []
你会想调用 .col(0)。如果出现类似需求,the specific documentation is always helpful!
所以你最终会得到类似的东西(假设你想使用三行单列矩阵):
Vector3d v;
// Operations to add values to the vector.
for (int i=0; i<v.rows(); ++i)
c_array[i] = v(i,0);
希望对您有所帮助。
关于c++ - 将 Eigen 矩阵转换为 c 数组以便我可以使用 gsl 的性能有效方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12849327/