pybind以数组作为类属性

时间:2018-09-04 16:02:29

标签: pybind11

我想使用pybind将以下C ++代码包装到python中

class Galaxy {

public:
    double x[3];
    double v[3];

};

class GalaxyCatalogue {

public:
    long n_tracers;
    Galaxy *object;

    GalaxyCatalogue(long n_tracers);

    ~GalaxyCatalogue();

};

GalaxyCatalogue::GalaxyCatalogue(long n_tracers) : n_tracers(n_tracers) {

    std::cout << "from galaxies " << n_tracers << std::endl;
    object = new Galaxy[n_tracers];
    std::cout << "has been allocated " << std::endl;

}

GalaxyCatalogue::~GalaxyCatalogue() {

    delete[] object;

}

我遇到的第一个问题是Galaxy没有构造函数,所以我不确定该怎么做。即使我声明一个空的构造函数,我也不知道如何以一种在编译时不会出错的方式对待数组。这是我尝试过的:

#include <pybind11/pybind11.h>
#include <iostream>
namespace py = pybind11;

class Galaxy {
   public:
  Galaxy();
 double x[3];
};

PYBIND11_MODULE(example, m){
  py::class_<Galaxy>(m, "Galaxy")
 .def(py::init<>())
 .def_readwrite("x", &Galaxy::x);
 }

这是我的编译方式:

c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` gal.cpp -o example`python3-config --extension-suffix`

这是我得到的错误:

In file included from gal.cpp:1:
/home/florpi/.conda/envs/virtualito/include/python3.5m/pybind11/pybind11.h: In instantiation of ‘pybind11::class_<type_, options>& pybind11::class_<type_, options>::def_readwrite(const char*, D C::*, const Extra& ...) [with C = Galaxy; D = double [3]; Extra = {}; type_ = Galaxy; options = {}]’:
gal.cpp:19:33:   required from here
/home/florpi/.conda/envs/virtualito/include/python3.5m/pybind11/pybind11.h:1163:65: error: invalid array assignment
                      fset([pm](type &c, const D &value) { c.*pm = value; }, is_method(*this));
                                                           ~~~~~~^~~~~~~
In file included from gal.cpp:1:

/home/florpi/.conda/envs/virtualito/include/python3.5m/pybind11/pybind11.h:64:5: error: ‘pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = pybind11::class_<type_, options>::def_readwrite(const char*, D C::*, const Extra& ...) [with C = Galaxy; D = double [3]; Extra = {}; type_ = Galaxy; options = {}]::<lambda(pybind11::class_<Galaxy>::type&, const double (&)[3])>; Extra = {pybind11::is_method}; <template-parameter-1-3> = void]’, declared using local type ‘pybind11::class_<type_, options>::def_readwrite(const char*, D C::*, const Extra& ...) [with C = Galaxy; D = double [3]; Extra = {}; type_ = Galaxy; options = {}]::<lambda(pybind11::class_<Galaxy>::type&, const double (&)[3])>’, is used but never defined [-fpermissive]
     cpp_function(Func &&f, const Extra&... extra) {
     ^~~~~~~~~~~~

谢谢。

1 个答案:

答案 0 :(得分:0)

在C ++中,您不能直接分配给数组,这是pybind11试图在其包装魔术中执行的操作。通常,C ++数组不是数字数组的出色抽象。正如您所注意到的,您甚至不能说galaxy.x = other_galaxy.x

您最好的选择是对矩阵和向量使用更高级别的库, a)给您更好的编写C ++的经验 b)表现更好 c)更清晰地映射到Python

Eigen是一个不错的选择。 pybind11自动知道如何将本征矩阵和向量映射到numpy数组。您的银河将变成:

class Galaxy {
  public:
    Eigen::Vector3d x;
    Eigen::Vector3d v;
};

如果您绝对无法做到这一点,则必须为该属性提供手动的getter / setter函数,在该属性中您可以自己进行python类型的转换:   https://pybind11.readthedocs.io/en/master/classes.html?highlight=def_property#instance-and-static-fields