【发布时间】:2016-12-06 18:34:55
【问题描述】:
我想在一些C风格的代码中使用Eigen做一些计算,函数接口有一个如下的原始指针,
#include <iostream>
#include <memory>
#include <Eigen/Dense>
using namespace Eigen;
typedef Eigen::Matrix<double, -1, -1, Eigen::RowMajor> Mrow;
void compute_with_Eigen(double * p_data, int row, int col)
{
// Q1: is there any data copy here?
Eigen::MatrixXd Mc = Eigen::Map<Mrow>(p_data, row, col);
// do computations with Mc, for example
auto M_temp = Mc.inverse();
Mc = M_temp;
// Q2: why is this assign-back necessary?
Eigen::Map<Mrow>( p_data, row, col ) = Mc;
}
int main()
{
std::unique_ptr<double[]> p(new double[10]);
for (int i = 0; i < 9; ++i)
{
p[i]=i+1.0;
std::cout<<p[i]<<std::endl;
}
compute_with_Eigen(p.get(),3,3);
std::cout<<"after inverse\n";
for (int i = 0; i < 10; ++i)
std::cout<<p[i]<<std::endl;
}
我有问题 1,因为在此 thread 中接受的答案表明存在一些副本,但是,原则上“视图”不应复制任何内容。
我有问题 2,因为否则结果不符合预期,但是如果我真的必须分配回来,这不像“视图”(另见 answer)
【问题讨论】:
标签: c++ smart-pointers eigen eigen3