我正在使用boost.numpy並試圖製作ndarray包裝器。我試圖發送ndarray從Python到C++和C++到Python。當我將ndarray從python發送到C++時,問題就出現了。什麼是不正確的?如何在boost-numpy庫中使用boost :: numpy :: from_data
+++mod2.cpp+++
1 #include"boost/numpy.hpp"
2 #include<iostream>
3
4 boost::numpy::ndarray test(boost::numpy::ndarray a){
5 boost::numpy::ndarray b = a;
6 int arr[]={5,4,3,2,1};
7 b = boost::numpy::from_data(arr,boost::numpy::dtype::get_builtin<int>(),
8 boost::python::make_tuple(5),boost::python::make_tuple(sizeof(int)),boost::python::object());
9 std::cout << "In mymod2.cpp:" <<
10 boost::python::extract < char const * > (boost::python::str(b)) << std::endl ;
11 return b;
12 }
13
14 BOOST_PYTHON_MODULE(mymod2){
15 boost::numpy::initialize();
16 boost::python::def("test",test);
17 }
+++demo.py+++
1 import numpy
2 import mymod2
3
4 Mat1 = numpy.array([1,2,3,4,5], dtype=numpy.int32)
5 print("In demo.py:",Mat1)
6 Mat2=mymod2.test(Mat1)
7 print("In demo.py:",Mat2)
+++Output+++
In demo.py: [1 2 3 4 5]
In mymod2.cpp:[5 4 3 2 1]
In demo.py: [ 0 48 430934944 32509 3]