Boost.Python - Vector to Numpy Array

2019-05-04 20:08发布

问题:

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));


标签: c++ boost numpy