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 ...
随机推荐
- java 集合删除数据
public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asL ...
- Jakartase_IO流_ — Commons IO_(IO流终极篇)
一.前言 Apache Commons IO是Apache基金会创建并维护的Java函数库. 它提供了许多类使得开发者的常见任务变得简单,同时减少重复代码 二.Commons IO 类库 2.1 Fi ...
- kubeadm单集群部署k8s1.15.1&flannel网络
说明 本次实验在Windows下的VMware进行 系统配置及初始化配置在所有的主机执行 容器镜像全部替换为国内可拉取的 pod网络采用flannel 实验环境 主机名 IP地址 角色 OS CPU/ ...
- uni-app开发注意事项
关于vue 1.注意 如果使用老版的非自定义组件模式,即manifest中"usingComponents":false,部分模版语法不支持,但此模式已于2019年11月起下线. ...
- 渗透测试方法论(qf总结)
渗透测试(penetration testing , pentest)是实施安全评估(即审计)的具体手段.方法论是在指定.实施信息安全审计方案时,需要遵循的规则.惯例和过程.人们在评估网路.应用.系统 ...
- 将SublimeText3打造成简易Java IDE
简介与优点 使用该教程,你能使你的Sublime Text3可以作为一个精简版的JAVA IDE工具 既可以独立在cmd控制台运行也可以在Sublime自带的控制台运行 运行后不会有乱码 报错后可以在 ...
- GAN的理论 Theory behind GAN
任务:想要找到一个高维空间中的分布 P_data(x),要在目标类别的区域,采样的概率是高的:在那个区域之外,probability是低的.但这个P_data(x)分布的具体形式(pdf)是不知道的, ...
- 嵌入式arm-linux mips-linux 交叉编译GDB,结合vscode图形化调试使用,coredump定位段错误
第一部分:使用GDB GDB源码下载路径:http://ftp.gnu.org/gnu/gdb/ 遇到的主要难点: 选择合适的GDB源码版本 我的mips-linux交叉编译器不支持C++11特性,所 ...
- Arduino 寻找I2C地址address
参考:http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-quick-tip-fin ...
- MATLAB textread函数
实际应用中经常要读取txt文件,这个时候就需要用到强大的textread函数.它的基本语法是:[A,B,C,...] = textread(filename,format)[A,B,C,...] = ...