将pybind numpy数组从float转换为双精度

时间:2019-08-30 23:23:47

标签: python c++ numpy pybind11

对于一个项目,我必须将dtype float的numpy传递给pybind函数。然后,该float pybind数组传递给一个函数,该函数接受一个指向double的指针,如下所示:

// this is the pybind signature
m.def("example", [](py::array_t<float, py::array::c_style> T) { ... }

// this is the cpp signature
void example(double * T) { ... }

cpp函数对传递的numpy数组进行操作。因此,应该更改从Python传递给函数的numpy变量。

我的问题是,我不知道如何将传递的numpy数组转换为浮点数组,再转换为双精度数组,同时又保留原始引用。或者至少现在有原始的numpy float数组引用了新的double数组。

当我使用std::move时,原始的float pybind数组仍保持其大小,而新的double数组具有一个新地址。这是否意味着std::move实际上正在执行复制?

我当前的解决方案未遵循指导原则,并且有望翻倍:

// this is the pybind signature
m.def("example", [](py::array_t<double, py::array::c_style> T) { 
    namespace::example(T.mutable_data(0));
}

让我知道这是否不是最佳实践。

0 个答案:

没有答案