简体   繁体   中英

Boost Python importing a C++ function with std::vectors as arguments

I am facing a problem, yesterday I made a post asking how to import a C++ funtion in Python: Post . In this post they suggested me to use Boost Python. So I started to learn it. However all the tutorials are really complicated for me. I know Python language but I am learing C++ so I found it difficult to understand. The other point is that in all the posts that I have found here they talk about 1D vectors in C++, but my function takes 2D vectors.

Indeed all the posts usually employ a C++ class instead of a function. And I don't know anything about classes in C++. But I found it useless in my case since I only whant to evaluate a function and return the result (double) to python. So the first question is. Is It completaley necessary to employ classes instead of functions for Boost python?

As you can see in the other post my function have the following structure:

double many_body_pot(
std::vector< std::vector<double> > &par,
std::vector< std::vector<double> > &geometry,
double x, double y, double z)
{
   // ...
}

So it takes 2 2D vectors and 3 doubles as parameters. So what I have learned until now is I have to use #include <boost/python.hpp> in my C++ script and I have to include something like this:

BOOST_PYTHON_MODULE(many_body_pot) {
    using namespace boost::python;
    def("many_body_pot", many_body_pot);
}

I Python have to send rather 2D ndarrays or 2D lists to the function to be converted to 2D vectors. And if I use 2D ndarrays I will have to use numpy Boost. In my case I don't mind to use one or the other. But I don't understand how to do the conversion to 2D vectors. Could you please give me an easy-to-understand solution for this? It would be really appreciated.

Thank you

C++ has a complicated learning curve for people who only know scripting. C++ has much more freedom than you could ever imagine. This freedom can be a curse for new learners. So unless you dedicate some time to understand how C++ works, you're not only gonna do the job wrong, but you may also do it inefficiently.

I know this might not directly be the answer to your question, but if you want to avoid using classes, then consider using Python's ctypes . You can create a shared library, and then import it in Python.

I, personally, am a big opponent of adding unnecessary libraries unless you have to. And I think the freedom provided by ctypes and the backward/forward compatibility with almost all Python versions and the possibility to decouple your C++ work from Python is priceless. So, consider using ctypes , and then all you have to learn is how to create functions and how to compile them to shared libraries. And since you're a Python expert, you won't have a problem importing that to Python and utilizing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM