传递Eigen :: Map< ArrayXd>期待ArrayXd&的函数 [英] Passing Eigen::Map<ArrayXd> to a function expecting ArrayXd&

查看:155
本文介绍了传递Eigen :: Map< ArrayXd>期待ArrayXd&的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的某些Eigen C ++方法需要可以从普通C ++调用,因此我想提供接受c数组并使用Eigen :: Map将它们映射到ArrayXd的重载函数。

Some of my Eigen C++ methods need to be callable from plain C++, therefore I want to provide overloaded functions that accept c arrays and map them to ArrayXd using Eigen::Map.

我当前拥有的代码如下:

The code I currently have looks like this:

bool Dmp::executeStep(double* position, double* velocity,
                  double* acceleration, const int len)
{
  Map<ArrayXd> posMap(position, len);
  Map<ArrayXd> velMap(velocity, len);
  Map<ArrayXd> accMap(acceleration, len);
  return executeStep(posMap, velMap, accMap);
}


bool Dmp::executeStep(ArrayXd& position, ArrayXd& velocity, ArrayXd& acceleration)
{
//Code that modifies position, velocity and acceleration
}

此操作无效,因为 Map< ArrayXd> ArrayXd&
这样做的正确方法是什么?

This does not work because there is no known conversation from Map<ArrayXd> to ArrayXd&. What is the correct way of doing this?

编辑:
下面的luk32指出的答案会起作用,但是这涉及将我的代码移动到头文件中,这是我尽可能避免的事情。

edit: The answer that luk32 pointed out below would work, however it involves moving my code to the header file which is something I would like to avoid if at all possible.

推荐答案

您应该将 executeStep 用作模板函数。或使用其他工具,例如参考以本征类型为参数的编写函数

You should make the executeStep a template function. Or use their other facilities like Ref There is a whole comprehensive tutorial in docs on Writing Functions Taking Eigen Types as Parameters.

我不确定 Map 是否具有比 EigenBase 更直接的父级(也许Dense确实不确定),但是

I am not sure if Map has a more direct parent than EigenBase (maybe Dense really not sure), but as it is most general it should work:

template <typename Derived>
void Dmp::executeStep(EigenBase<Derived>& pos,EigenBase<Derived>& vel, EigenBase<Derived>& acc )
{
// fun ...
}

当然,您也需要将其声明为模板成员。

Of course you need to declare it as a template member too.

我强烈建议阅读整个教程。

I really highly suggest reading the whole tutorial.

使用 Ref 实施。但是,我不确定是否从 Map 复制到 MyMatrix Ref 不接受 Map 对象,因为它似乎将它们转换为 DenseMatrix

Using Ref implementation. I am not sure however if there is a copy from Map to MyMatrix made. Ref do not accept Map objects as it seems to cast them to DenseMatrix.

#include <Eigen/Core>
#include <iostream>

using namespace Eigen;

typedef Matrix<int,2,4>  MyMatrix ;

void print_size(const Ref<const MyMatrix>& b)
{
  std::cout << "size (rows, cols): " << b.size() << " (" << b.rows()
            << ", " << b.cols() << ")" << "\n";
}

int main()
{
  int array[8];
  for(int i = 0; i < 8; ++i) array[i] = i;
  Map<MyMatrix> map(array);
  std::cout << "Column-major:\n" <<  map << "\n";
  print_size(map);
}

这篇关于传递Eigen :: Map&lt; ArrayXd&gt;期待ArrayXd&amp;的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆