c++ - 将 Eigen 矩阵转换为 c 数组以便我可以使用 gsl 的性能有效方法是什么

标签 c++ pointers eigen arrays

我的大部分代码都使用 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/

相关文章:

c++ - 在 Eigen 中复制模板化函数参数

c++ - 类型转换重载

c++ - 在 C++ 中,表达式 "*pointer++"是如何工作的?

c++ - 特征行 vector 超出范围会产生 “Run-Time Check Failure #2 - Stack around the variable X was corrupted”

c - 指向自身的指针

javascript - Parse.com 云代码 : Fast Way of Creating Pointers than Object Queries/Manual

c++ - 使用特征向量中的权重从离散分布中采样

c++ - 放宽执行顺序规则

c++ - 不能使用 Qt Xlsx

c++ - vector<bool> 特化与 range base 不兼容