python - How to convert a vector to numpy array with templates and boost? -


so found how return numpy.array boost::python?. however, have write function int, float, double separately. possible avoid using templates? somehow need convert t entry enumeration of npy data types. or there other options?

template<typename t> boost::python::object getnumpyarray(std::vector<t>& vector) {     npy_intp shape[1] = { vector.size() };     //here need replace npy_double conversion of t     pyobject* obj = pyarray_simplenewfromdata(1, shape, npy_double, vector.data());     boost::python::handle<> handle(obj);     boost::python::numeric::array arr(handle);      return arr.copy(); } 

you can write own trait select numpy type based on c++ type, example:

template <typename t> struct select_npy_type {};  template <> struct select_npy_type<double> {     const static npy_types type = npy_double; };  template <> struct select_npy_type<float> {     const static npy_types type = npy_float; };  template <> struct select_npy_type<int> {     const static npy_types type = npy_int; }; 

and then:

pyobject* obj = pyarray_simplenewfromdata(1, shape, select_npy_type<t>::type, vector.data()); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -