pybind11和numpy进行交互的方法

作者:酱油 时间:2021-08-18 23:24:14 

使用一个遵循buffer protocol的对象就可以和numpy交互了.

这个buffer_protocol要有哪些东西呢? 要有如下接口:


struct buffer_info {
 void *ptr;
 ssize_t itemsize;
 std::string format;
 ssize_t ndim;
 std::vector<ssize_t> shape;
 std::vector<ssize_t> strides;
};

其实就是一个指向数组的指针+各个维度的信息就可以了. 然后我们就可以用指针+偏移来访问数字中的任意位置上的数字了.

下面是一个可以跑的例子:


#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> 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<double>(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 <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <Eigen/LU>
// 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<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> 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);
}

更多参考:

https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html

https://github.com/tdegeus/pybind11_examples

总结

以上所述是小编给大家介绍的pybind11和numpy进行交互的方法,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

来源:https://www.cnblogs.com/JiangOil/p/11130670.html

标签:pybind11,numpy
0
投稿

猜你喜欢

  • 监测站点使用多少session和application的asp程序

    2007-09-12 19:40:00
  • asp解决fso.ReadAll提示输入超出了文件尾的错误

    2008-01-30 21:40:00
  • 超酷的js图片轮播渐变效果

    2007-10-10 20:45:00
  • 纯CSS3文字渐变内发光投影效果

    2011-08-24 20:15:10
  • oracle的一些tips技巧

    2009-03-02 11:06:00
  • php5.2 Json不能正确处理中文、GB编码的解决方法

    2023-10-26 13:49:28
  • 服务端XMLHTTP(ServerXMLHTTP in ASP)基本应用(上)

    2008-11-11 12:49:00
  • jquery 使用点滴函数代码

    2011-05-21 16:12:00
  • Dhtml网页实例教程

    2007-10-09 13:39:00
  • php实现pdo数据库操作类过程详解

    2023-05-25 11:15:05
  • 正视WEB标准,一本全面的标准参考书

    2009-05-30 16:36:00
  • 简化ADO数据库操作的控件(带分页功能)

    2008-05-20 13:15:00
  • 用 AjaxTags 简化 Ajax 开发

    2007-11-27 00:00:00
  • Python连接Hadoop数据中遇到的各种坑(汇总)

    2023-09-13 20:16:34
  • 在ASP中通过oo4o连接Oracle数据库的例子

    2008-10-12 12:55:00
  • php抓取页面的几种方法详解

    2023-11-14 10:53:42
  • 如何正确处理数据库中的Null

    2007-09-30 19:41:00
  • Codeigniter控制器controller继承问题实例分析

    2023-11-23 04:15:31
  • 简单的文本内容处理工具

    2010-01-28 12:31:00
  • 简单聊聊Golang中defer预计算参数

    2023-07-22 03:55:09
  • asp之家 网络编程 m.aspxhome.com