使用一个遵循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;
};
 
其实就是一个指向数组的指针+各个维度的信息就可以了. 然后我们就可以用指针+偏移来访问数字中的任意位置上的数字了.
 
下面是一个可以跑的例子:
 
 1 #include <pybind11/pybind11.h>
2 #include <pybind11/numpy.h>
3
4 namespace py = pybind11;
5
6 py::array_t<double> add_arrays(py::array_t<double> input1, py::array_t<double> input2) {
7 py::buffer_info buf1 = input1.request(), buf2 = input2.request();
8
9 if (buf1.ndim != 1 || buf2.ndim != 1)
10 throw std::runtime_error("Number of dimensions must be one");
11
12 if (buf1.size != buf2.size)
13 throw std::runtime_error("Input shapes must match");
14
15 /* No pointer is passed, so NumPy will allocate the buffer */
16 auto result = py::array_t<double>(buf1.size);
17
18 py::buffer_info buf3 = result.request();
19
20 double *ptr1 = (double *) buf1.ptr,
21 *ptr2 = (double *) buf2.ptr,
22 *ptr3 = (double *) buf3.ptr;
23
24 for (size_t idx = 0; idx < buf1.shape[0]; idx++)
25 ptr3[idx] = ptr1[idx] + ptr2[idx];
26
27 return result;
28 }
29
30 PYBIND11_MODULE(test, m) {
31 m.def("add_arrays", &add_arrays, "Add two NumPy arrays");
32 }
 
array_t里的buf就是一个兼容的接口.
buf中可以得到指针和对应数字的维度信息.
 
 
为了方便我们甚至可以使用Eigen当作我们兼容numpy的接口:
 
 
 1 #include <pybind11/pybind11.h>
2 #include <pybind11/eigen.h>
3
4 #include <Eigen/LU>
5
6 // N.B. this would equally work with Eigen-types that are not predefined. For example replacing
7 // all occurrences of "Eigen::MatrixXd" with "MatD", with the following definition:
8 //
9 // typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> MatD;
10
11 Eigen::MatrixXd inv(const Eigen::MatrixXd &xs)
12 {
13 return xs.inverse();
14 }
15
16 double det(const Eigen::MatrixXd &xs)
17 {
18 return xs.determinant();
19 }
20
21 namespace py = pybind11;
22
23 PYBIND11_MODULE(example,m)
24 {
25 m.doc() = "pybind11 example plugin";
26
27 m.def("inv", &inv);
28
29 m.def("det", &det);
30 }
 
 
更多参考:
 
 

pybind11和numpy进行交互的更多相关文章

  1. TensorFlow 学习(六) —— TensorFlow 与 numpy 的交互

    1. 将 numpy 下的多维数组(ndarray)转化为 tensor a = np.zeros((3, 3)) ta = tf.convert_to_tensor(a) with tf.Sessi ...

  2. 学机器学习,不会数据处理怎么行?—— 一、NumPy详解

    最近学习强化学习和机器学习,意识到数据分析的重要性,就开始补Python的几个科学计算库,并总结到博客中.本篇博客中用到的代码在这里下载. 什么是Numpy? NumPy是Python数值计算最重要的 ...

  3. python数据分析---第04章 NumPy基础:数组和矢量计算

    NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...

  4. python数据分析系列(2)--numpy

    NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...

  5. python数据分析 Numpy基础 数组和矢量计算

    NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...

  6. python3 通过 pybind11 使用Eigen加速代码

    python是很容易上手的编程语言,但是有些时候使用python编写的程序并不能保证其运行速度(例如:while 和 for),这个时候我们就需要借助c++等为我们的代码提速.下面是我使用pybind ...

  7. 混合编程[python+cpp+cuda]

    很多时候,我们是基于python进行模型的设计和运行,可是基于python本身的速度问题,使得原生态python代码无法满足生产需求,不过我们可以借助其他编程语言来缓解python开发的性能瓶颈.这里 ...

  8. python2迁移python3的问题

    ▌使用 pathlib 模块来更好地处理路径 pathlib 是 Python 3默认的用于处理数据路径的模块,它能够帮助我们避免使用大量的 os.path.joins语句: from pathlib ...

  9. numpy数组与python的list互转,然后用json写入文件与c交互

    1.对于numpy的tofile方法,一个一维数组可以直接写成二进制形式,用c语言或者numpy.fromfile()可以读出来内容.而如果数组超过一维,tofile并不区分,也就是arr1=[1,2 ...

随机推荐

  1. python中库引用与import

    在蟒蛇绘制函数中,多有turtle.   ,称它为<a>.<b>的编码风格 库引用 扩充python程序功能的方式 使用import保留字完成,采用<a>.< ...

  2. Linux系统编程—进程间同步

    我们知道,线程间同步有多种方式,比如:信号量.互斥量.读写锁,等等.那进程间如何实现同步呢?本文介绍两种方式:互斥量和文件锁. 互斥量mutex 我们已经知道了互斥量可以用于在线程间同步,但实际上,互 ...

  3. python-字符串,字典,列表

    0x01 字符串 python单双引号都可以 str = "hello world" str_test = "yicunyiye" print(str,str_ ...

  4. 浅谈 ArrayList 及其扩容机制

    浅谈ArrayList ArrayList类又称动态数组,同时实现了Collection和List接口,其内部数据结构由数组实现,因此可对容器内元素实现快速随机访问.但因为ArrayList中插入或删 ...

  5. zabbix关键字含义

    Zabbix server :zabbix控制中心,收集数据,写入数据库都是他的工作 Zabbix Agent:部署在被监控服务器上的一个进程,负责和zabbix server 交互,执行命令. Ho ...

  6. 百度地图四(Android百度地图Poi检索开发总结)

    https://blog.csdn.net/wenzhi20102321/article/details/54575999

  7. 一篇文章搞定 Nginx 反向代理与负载均衡

    代理 要想弄明白反向代理,首先要知道啥是正向代理,要搞懂正向代理只需要知道啥是代理即可.代理其实就是一个中介,在不同事物或同一事物内部起到居间联系作用的环节.比如买票黄牛,房屋中介等等. 在互联网中代 ...

  8. JQuery实现tab页

    用ul 和 div 配合实现tab 页 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="U ...

  9. Code Test(1)

    0922test 最小数Time Limit: 1 Sec Memory Limit: 128 MB文件名:a.cppDescription给定一个正整数n,请去掉其中的m个数字,使其剩下的数字按原先 ...

  10. C++中的In 和 Out用法

    参考:https://zhidao.baidu.com/question/541219383.html In 这是一个宏,它的实际意义就是告诉你,这个变量或参数是输入值,即你必须给这个变量填写好以后提 ...