pybind11和numpy進(jìn)行交互的方法
使用一個(gè)遵循buffer protocol的對(duì)象就可以和numpy交互了.
這個(gè)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; };
其實(shí)就是一個(gè)指向數(shù)組的指針+各個(gè)維度的信息就可以了. 然后我們就可以用指針+偏移來(lái)訪問(wèn)數(shù)字中的任意位置上的數(shù)字了.
下面是一個(gè)可以跑的例子:
#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就是一個(gè)兼容的接口.
buf中可以得到指針和對(duì)應(yīng)數(shù)字的維度信息.
為了方便我們甚至可以使用Eigen當(dāng)作我們兼容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
總結(jié)
以上所述是小編給大家介紹的pybind11和numpy進(jìn)行交互的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
python編程學(xué)習(xí)np.float 被刪除的問(wèn)題解析
這篇文章主要為大家介紹了python編程學(xué)習(xí)np.float 被刪除的問(wèn)題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02淺談tensorflow與pytorch的相互轉(zhuǎn)換
本文主要介紹了簡(jiǎn)單介紹一下tensorflow與pytorch的相互轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06詳解用Python練習(xí)畫個(gè)美隊(duì)盾牌
這篇文章主要介紹了用Python練習(xí)畫個(gè)美隊(duì)盾牌,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03python查找目錄下指定擴(kuò)展名的文件實(shí)例
這篇文章主要介紹了python查找目錄下指定擴(kuò)展名的文件,實(shí)例分析了Python文件查詢的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04解決pycharm安裝后代碼區(qū)不能編輯的問(wèn)題
今天小編就為大家分享一篇解決pycharm安裝后代碼區(qū)不能編輯的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10基于Python實(shí)現(xiàn)新年倒計(jì)時(shí)
眼看馬上春節(jié)就要來(lái)臨了,所以滿懷期待的寫了一個(gè)Python新年倒計(jì)時(shí)的小工具!文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-01-01python實(shí)現(xiàn)冒泡排序算法的兩種方法
本篇文章主要介紹了python實(shí)現(xiàn)冒泡排序的兩種方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03