pybind传输list

举报
风吹稻花香 发表于 2021/06/05 00:32:46 2021/06/05
【摘要】   https://github.com/tdegeus/pybind11_examples/blob/master/01_py-list_cpp-vector/test.py   import example A = [1.,2.,3.,4.] B = example.modify(A) print(B) pybind传输list 感觉数据类型...

 

https://github.com/tdegeus/pybind11_examples/blob/master/01_py-list_cpp-vector/test.py

 


  
  1. import example
  2. A = [1.,2.,3.,4.]
  3. B = example.modify(A)
  4. print(B)

pybind传输list

感觉数据类型必须是单一的某个类型:


  
  1. #include <pybind11/pybind11.h>
  2. #include <pybind11/stl.h>
  3. #include <vector>
  4. // ----------------
  5. // Regular C++ code
  6. // ----------------
  7. // multiply all entries by 2.0
  8. // input: std::vector ([...]) (read only)
  9. // output: std::vector ([...]) (new copy)
  10. std::vector<double> modify(const std::vector<double>& input)
  11. {
  12. std::vector<double> output;
  13. std::transform(
  14. input.begin(),
  15. input.end(),
  16. std::back_inserter(output),
  17. [](double x) -> double { return 2.*x; }
  18. );
  19. // N.B. this is equivalent to (but there are also other ways to do the same)
  20. //
  21. // std::vector<double> output(input.size());
  22. //
  23. // for ( size_t i = 0 ; i < input.size() ; ++i )
  24. // output[i] = 2. * input[i];
  25. return output;
  26. }
  27. // ----------------
  28. // Python interface
  29. // ----------------
  30. namespace py = pybind11;
  31. PYBIND11_MODULE(example,m)
  32. {
  33. m.doc() = "pybind11 example plugin";
  34. m.def("modify", &modify, "Multiply all entries of a list by 2.0");
  35. }

 

例子2:


  
  1. import numpy as np
  2. import example
  3. A = [0,1,2,3,4,5]
  4. B = example.multiply(A)
  5. print('input list = ',A)
  6. print('output = ',B)
  7. A = np.arange(10)
  8. B = example.multiply(A)
  9. print('input list = ',A)
  10. print('output = ',B)

  
  1. #include <pybind11/pybind11.h>
  2. #include <pybind11/stl.h>
  3. #include <pybind11/numpy.h>
  4. #include <vector>
  5. // -------------
  6. // pure C++ code
  7. // -------------
  8. std::vector<int> multiply(const std::vector<double>& input)
  9. {
  10. std::vector<int> output(input.size());
  11. for ( size_t i = 0 ; i < input.size() ; ++i )
  12. output[i] = 10*static_cast<int>(input[i]);
  13. return output;
  14. }
  15. // ----------------
  16. // Python interface
  17. // ----------------
  18. namespace py = pybind11;
  19. // wrap C++ function with NumPy array IO
  20. py::array_t<int> py_multiply(py::array_t<double, py::array::c_style | py::array::forcecast> array)
  21. {
  22. // allocate std::vector (to pass to the C++ function)
  23. std::vector<double> array_vec(array.size());
  24. // copy py::array -> std::vector
  25. std::memcpy(array_vec.data(),array.data(),array.size()*sizeof(double));
  26. // call pure C++ function
  27. std::vector<int> result_vec = multiply(array_vec);
  28. // allocate py::array (to pass the result of the C++ function to Python)
  29. auto result = py::array_t<int>(array.size());
  30. auto result_buffer = result.request();
  31. int *result_ptr = (int *) result_buffer.ptr;
  32. // copy std::vector -> py::array
  33. std::memcpy(result_ptr,result_vec.data(),result_vec.size()*sizeof(int));
  34. return result;
  35. }
  36. // wrap as Python module
  37. PYBIND11_MODULE(example,m)
  38. {
  39. m.doc() = "pybind11 example plugin";
  40. m.def("multiply", &py_multiply, "Convert all entries of an 1-D NumPy-array to int and multiply by 10");
  41. }

 

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/110952354

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。