小编diz*_*q22的帖子

从包装 eigen 的 pybind11 返回数组的列表或元组

我有一个使用 eigen 的 C++ 函数,它使用 pybind11 包装,以便我可以从 python 调用它。预期函数的简单版本返回一个Eigen::MatrixXd类型,pybind 成功地将其转换为 2D numpy 数组。

我希望这个函数能够返回此类矩阵的列表或元组,或者 3D numpy 数组。

我在某种程度上是 C++ 的新手,并且 pybind 的文档没有(据我所知)提供任何方向。一个模拟示例如下:

测试.cpp

#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <Eigen/Dense>

Eigen::MatrixXd test(Eigen::Ref<const Eigen::MatrixXd> x, double a)
{
  Eigen::MatrixXd y;
  y = x * a;
  return y;
}

Eigen::MatrixXd *test2(Eigen::Ref<const Eigen::MatrixXd> x, Eigen::Ref<const Eigen::VectorXd> as)
{
  Eigen::MatrixXd *ys = new Eigen::MatrixXd[as.size()];
  for(unsigned int k = 0; k < as.size(); k++){
    Eigen::MatrixXd& y = ys[k];
    y = x * as[k];
  } …
Run Code Online (Sandbox Code Playgroud)

python numpy eigen pybind11

5
推荐指数
1
解决办法
8844
查看次数

使用C ++中的switch语句将不同的函数分配为类属性

我正在使用pybind11为python编写C ++ Eigen扩展。

我希望其中一个扩展类采用一个参数,该参数指定要使用属性的几个函数中的哪个。我的部分问题是,由于我有限的C ++经验,因此我很难阐明这个问题,因此,我希望您能对术语提出任何建议。

Python中,我想做的简化版本如下所示:

class My_class:

    def __init__(self, arg1, option):
        self.arg1 = arg1

        if option == 'option1'
            self.operation = operation1
        else:
            self.operation = operation2

    def my_method(self, arg):
        return self.operation(arg, self.arg1)

Run Code Online (Sandbox Code Playgroud)

我尝试遵循此问题的可接受答案,并且具有一个具有函数作为属性的类,尽管它不能按预期工作(如下所述)。

我已尽力构建了一个最小的示例:

#define _USE_MATH_DEFINES

#include "kernels.h"
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <pybind11/stl.h>
#include <Eigen/LU>
#include <Eigen/Dense>
#include <math.h>
#include <unsupported/Eigen/SpecialFunctions>

// ================ Functions

Eigen::MatrixXd operation1(Eigen::Ref<const Eigen::MatrixXd> A, Eigen::Ref<const Eigen::MatrixXd> B){
  return A.array() + B.array();
}

Eigen::MatrixXd operation2(Eigen::Ref<const Eigen::MatrixXd> A, Eigen::Ref<const Eigen::MatrixXd> B){
  return A.array() …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
60
查看次数

标签 统计

c++ ×1

eigen ×1

numpy ×1

pybind11 ×1

python ×1