3

我想对来自另一个库的数据使用 Eigen3。ggael较早的回答指出了一种Eigen::Matrix通过关键字采用预先存在的数据的方法new。但是,这对我来说还不够,因为结果Matrix似乎仍然获得了数据的所有权,这意味着它会在超出范围时释放数据。也就是说,如果data最终被它来自的库删除,这将是一个崩溃:

void crasher(double* data, size_t dim)
{
  MatrixXd m;

  new (&m) Map<MatrixXd>(data,dim,dim); // matrix adopts the passed data
  m.setRandom(); cout<<m<<endl; // manipulation of the passed data via the Matrix interface
} // data deleted by m => potential problem in scope of function call

我想出了两个解决方法:

void nonCrasher1(double* data, size_t dim)
{
  MatrixXd m; // semantically, a non-owning matrix
  const Map<const MatrixXd> cache(m.data(),0,0); // cache the original „data” (in a const-correct way)

  new (&m) Map<MatrixXd>(data,dim,dim); // matrix adopts the passed data
  m.setRandom(); cout<<m<<endl; // manipulation of the passed data via the Matrix interface

  new (&m) Map<const MatrixXd>(cache); // re-adopting the original data
} // original data deleted by m

这是相当不方便的,因为存在cache. 另一个没有这个问题:

void nonCrasher2(double* data, size_t dim) // no need for caching
{
  MatrixXd m; // semantically, a non-owning matrix

  new (&m) Map<MatrixXd>(data,dim,dim); // matrix adopts the passed data
  m.setRandom(); cout<<m<<endl; // manipulation of the passed data via the Matrix interface

  new (&m) Map<MatrixXd>(nullptr,0,0); // adopting nullptr for subsequent deletion
} // nullptr „deleted” by m (what happens with original data [if any]?)

然而,这里不清楚原始数据会发生什么m(如果有的话——所有这些都不是从 Eigen3 的文档中完全清楚的)。

我的问题是是否有一种规范的方式Eigen::Matrix来释放其数据的所有权(自行分配或采用)。

4

1 回答 1

1

数据没有发生任何事情,nonCrasher2只是故意填充了随机值。

但是,这仍然看起来很老套,干净的方法是使用Map对象而不是MatrixXd

Map<MatrixXd> m(data, dim, dim);
m.setRandom();

如果因为需要调用获取对象的函数而需要m成为 a ,并且此类函数无法模板化,那么您可以考虑将这些函数泛化为获取对象。默认情况下,a可以接受任何将存储重新组装为具有任意前导维度的表达式。它已在 Eigen 3.2 中引入,请查看文档以获取更多详细信息。MatrixXdMatrixXdRef<MatrixXd>Ref<MatrixXd>MatrixXd

于 2013-11-27T13:13:51.900 回答