Boost.Python - Vector to Numpy Array

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

I have the following class:

class PyWav {     public:       static inline boost::python::object sdVecToNumpyArray(std::vector<double> const& vec)     {         npy_intp size = vec.size();          double * data = size ? const_cast<double *>(&vec[0]) : static_cast<double *>(NULL);         PyObject * pyObj = PyArray_SimpleNewFromData(1, &size, NPY_DOUBLE, data);         boost::python::handle<> handle ( pyObj );         boost::python::numeric::array arr(handle);          return arr.copy();      }     // Access function     inline boost::python::numeric::array toNumPy()    {         std::vector<double> v = Signal(); // get the vector          boost::python::numeric::array arr = Lib::python::PyWav::sdVecToNumpyArray(                                                                                   v);         return arr;    }  };  

The problem is I do not know how to access the stdVecToNumpyArray and pass through the vector? I want the method "toNumPy()" to be open to the user, but, not the sdVecToNumpyArray

I get the following error:

error: conversion from ‘boost::python::api::object’ to non-scalar type ‘boost::python::numeric::array’ requested v);

Anyone have any ideas?

回答1:

You may need an explicit cast, like here: https://mail.python.org/pipermail/cplusplus-sig/2009-January/014194.html

boost::python::numeric::array arr(static_cast<boost::python::numeric::array>(handle)); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!