Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > Python科学计算

pybind11和numpy进行交互的方法

来源:中文源码网    浏览:176 次    日期:2024-04-30 09:35:02
【下载文档:  pybind11和numpy进行交互的方法.txt 】


pybind11和numpy进行交互的方法
使用一个遵循buffer protocol的对象就可以和numpy交互了.
这个buffer_protocol要有哪些东西呢? 要有如下接口:
struct buffer_info {
void *ptr;
ssize_t itemsize;
std::string format;
ssize_t ndim;
std::vector shape;
std::vector strides;
};
其实就是一个指向数组的指针+各个维度的信息就可以了. 然后我们就可以用指针+偏移来访问数字中的任意位置上的数字了.
下面是一个可以跑的例子:
#include
#include
namespace py = pybind11;
py::array_t add_arrays(py::array_t input1, py::array_t input2) {
py::buffer_info buf1 = input1.request(), buf2 = input2.request();
if (buf1.ndim != 1 || buf2.ndim != 1)
throw std::runtime_error("Number of dimensions must be one");
if (buf1.size != buf2.size)
throw std::runtime_error("Input shapes must match");
/* No pointer is passed, so NumPy will allocate the buffer */
auto result = py::array_t(buf1.size);
py::buffer_info buf3 = result.request();
double *ptr1 = (double *) buf1.ptr,
*ptr2 = (double *) buf2.ptr,
*ptr3 = (double *) buf3.ptr;
for (size_t idx = 0; idx < buf1.shape[0]; idx++)
ptr3[idx] = ptr1[idx] + ptr2[idx];
return result;
}
PYBIND11_MODULE(test, m) {
m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
}
array_t里的buf就是一个兼容的接口.
buf中可以得到指针和对应数字的维度信息.
为了方便我们甚至可以使用Eigen当作我们兼容numpy的接口:
#include
#include
#include
// N.B. this would equally work with Eigen-types that are not predefined. For example replacing
// all occurrences of "Eigen::MatrixXd" with "MatD", with the following definition:
//
// typedef Eigen::Matrix MatD;
Eigen::MatrixXd inv(const Eigen::MatrixXd &xs)
{
return xs.inverse();
}
double det(const Eigen::MatrixXd &xs)
{
return xs.determinant();
}
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
m.doc() = "pybind11 example plugin";
m.def("inv", &inv);
m.def("det", &det);
}
更多参考:
http://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html
http://github.com/tdegeus/pybind11_examples
总结
以上所述是小编给大家介绍的pybind11和numpy进行交互的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对中文源码网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关内容