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 ...
随机推荐
- Centos 7 redis、tomcat、Spring Boot添加开机自启服务
一.redis添加开机自启 1.添加服务配置文件 [root@test system]# vim /etc/systemd/system/redis-server.service 2.服务配置文件内容 ...
- python身体指数BMI
问题需求 既要输出国际标准也要输出国内标准
- php反序列化到getshell
0x01 扫描存活,端口 C:\Users\Administrator>nmap -sn -PR -T 4 192.168.18.0/24 Starting Nmap 7.70 ( https: ...
- JVM 的参数类型
标配参数 -version -help X 参数 -Xint:解释执行 -Xcomp:第一次使用就编译成本地代码 -Xmixed:混合模式 XX 参数 Boolean 类型:-XX:+ 或者 - 某个 ...
- JVM学习(三)JVM垃圾回收
一.引用的分类 在了解JVM垃圾回收机制之前,了解一下对象的引用类型是非常必要的. 强引用:GC时不会被回收 软引用:描述有用但不是必须的对象,在发生内存溢出异常之前被回收 弱引用:描述有用但不是必须 ...
- redis集群简介
1.1 集群的概念 所谓的集群,就是通过添加服务器的数量,提供相同的服务,从而让服务器达到一个稳定.高效的状态. 1.1.1 使用redis集群的必要性 问题:我们已经部署好 ...
- O、Θ、Ω
1.这些是时间复杂度的.(e.g. O(n).Θ(n).Ω(n)) 主要为主定理(坏东西) 2.本质 O <= Θ = Ω >=
- Processing 高效控制管理图形方法(一)
之前在CSDN上发表过: https://blog.csdn.net/fddxsyf123/article/details/62456299
- mysql-9-limit
#进阶9:分页查询 /* 当要显示的数据,一页显示不全,需要分页提交sql请求 SELECT FROM JOIN ON WHERE GROUP BY HAVING ORDER BY LIMIT off ...
- 再解决不了前端加密我就吃shi
参考文章 快速定位前端加密方法 渗透测试-前端加密测试 前言 最近学习挖洞以来,碰到数据做了加密基本上也就放弃了.但是发现越来越多的网站都开始做前端加密了,不论是金融行业还是其他.所以趁此机会来捣鼓一 ...