pybind11和numpy进行交互
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 }
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进行交互的更多相关文章
- TensorFlow 学习(六) —— TensorFlow 与 numpy 的交互
1. 将 numpy 下的多维数组(ndarray)转化为 tensor a = np.zeros((3, 3)) ta = tf.convert_to_tensor(a) with tf.Sessi ...
- 学机器学习,不会数据处理怎么行?—— 一、NumPy详解
最近学习强化学习和机器学习,意识到数据分析的重要性,就开始补Python的几个科学计算库,并总结到博客中.本篇博客中用到的代码在这里下载. 什么是Numpy? NumPy是Python数值计算最重要的 ...
- python数据分析---第04章 NumPy基础:数组和矢量计算
NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...
- python数据分析系列(2)--numpy
NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...
- python数据分析 Numpy基础 数组和矢量计算
NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...
- python3 通过 pybind11 使用Eigen加速代码
python是很容易上手的编程语言,但是有些时候使用python编写的程序并不能保证其运行速度(例如:while 和 for),这个时候我们就需要借助c++等为我们的代码提速.下面是我使用pybind ...
- 混合编程[python+cpp+cuda]
很多时候,我们是基于python进行模型的设计和运行,可是基于python本身的速度问题,使得原生态python代码无法满足生产需求,不过我们可以借助其他编程语言来缓解python开发的性能瓶颈.这里 ...
- python2迁移python3的问题
▌使用 pathlib 模块来更好地处理路径 pathlib 是 Python 3默认的用于处理数据路径的模块,它能够帮助我们避免使用大量的 os.path.joins语句: from pathlib ...
- numpy数组与python的list互转,然后用json写入文件与c交互
1.对于numpy的tofile方法,一个一维数组可以直接写成二进制形式,用c语言或者numpy.fromfile()可以读出来内容.而如果数组超过一维,tofile并不区分,也就是arr1=[1,2 ...
随机推荐
- 修改python包pip下载国内源
第一种方式- pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/ 附国内常用镜像源:阿里云:https:// ...
- Redis学习(四)redis发布订阅
文章更新时间:2020/04/22 一.简介 Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. Redis 客户端可以订阅任意数量的频道. ...
- IDEA配置jQuery,$符号不再显示黄色波浪线
在使用IDEA搭建Maven的Web环境时,编写的JQuery入口函数时,遇到了未知符号的提示,并且在前端页面js的console里报错. 以下是错误信息: 解决方案: 继续看图: 配置成功生效: ...
- Windows控件的属性与事件
Treeview控件重要属性和事件 属性 说明 Nodes Treeview控件中所有树节点 SelectdNode 当前Treeview控件中选定的树节点,如果当前没有选定树节点,返回值为null ...
- pytorch和tensorflow的爱恨情仇之定义可训练的参数
pytorch和tensorflow的爱恨情仇之基本数据类型 pytorch和tensorflow的爱恨情仇之张量 pytorch版本:1.6.0 tensorflow版本:1.15.0 之前我们就已 ...
- 注册表“CLSID”下面的“InprocServer32”子键是什么?
这个键值有什么用?每个CLSID下基本都有,即使没有其它项也会有此项.谁能详细说下,"InprocServer32"子键起什么作用?谢了. 刚好遇到这问题了,这是我找到的:Inpr ...
- JDK1.8前_HashMap的扩容机制原理
最近在研究hashmap的扩容机制,作为一个小白,相信我的理解,对于一些同样是刚刚接触hashmap的白白是有很很大的帮助,毕竟你去看一些已经对数据结构了解透彻的大神谈hashmap的原理等,人家说的 ...
- Arduino control Eeprom by IIC method of using device address in Arduino
参考: 1.https://www.arduino.cc/ 2.https://www.arduino.cc/reference/en/ 3.https://www.arduino.cc/en/Ref ...
- SPI应用 用SPI控制一个数字电位器
Controlling a Digital Potentiometer Using SPI In this tutorial you will learn how to control the AD5 ...
- Java (四)APACHE Commons IO 复制文件
上一篇:Java (三)APACHE Commons IO 常规操作 例1:复制文件 1 import java.io.File; 2 import java.io.IOException; 3 4 ...