I'm trying to write a c++ extension to replace the following python function in order to speed up my program
The python function looks like the following
def calc_dist(fea1, fea2):
    #fea1 and fea2 are two lists with same length
    ...
I wrote the function using c++ and boost python like follows:
#include <vector>
#include <boost/python.hpp>
double calc_dist(vector<double>& fea1, vector<double>& fea2)
{
    int len = fea1.size();
    double s=0;
    for(int i=0; i<len;i++){
        double p=fea1[i];
        double q=fea2[i];
        ...//calculating..
    }
    return s;
}
BOOST_PYTHON_MODULE(calc_dist)
{
    using namespace boost::python;
    def("calc_dist",calc_dist);
}
and compile the above cpp code into a .so file like
g++ calc_dist.cpp -shared -fPIC -o calc_dist.so -I /usr/include/python2.6 -lboost_python
and trying to use the .so in a python program, the import works fine, indicating the module can successfully imported.
However, whenever I pass two lists to the parameter to the function, python will give errors like
ArgumentError: Python argument types in
    calc_dist.calc_dist(list, list)
did not match C++ signature:
    calc_dist.calc_dist(std::vector<float, std::allocator<float> >,
                        std::vector<float, std::allocator<float> >)
can any one help me how to solve this problem? i.e pass a python list to c++ extension using boost?
Thanks a lot!
Why did you write a function accepting std::vector if you want it to operate on a Python list? They're different things.
Boost.Python exposes python lists as the list class.
So, your function should look something like
double calc_dist(boost::python::list fea1, boost::python::list fea2)
{
    boost::python::ssize_t len = boost::python::len(fea1);
    double s=0;
    for(int i=0; i<len;i++){
        double p = boost::python::extract<double>(fea1[i]);
        double q = boost::python::extract<double>(fea2[i]);
        ...//calculating..
    }
    return s;
}
it's not tested, but hopefully is close enough to get you started ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With