0

I have a C++ function that takes in std::vector<std::vector<double> > X and does some operations on X and outputs std::vector<std::vector<double> > X_mod.

I want to be able to quickly make an interface such that I can pass a Python numpy array into this C++ function, and then have the C++ function return X_mod into Python.

I have briefly looked at Boost, and it seems too complicated for this simple purpose?

Any other suggestions on how to write a quick interface for this?

24n8
  • 1,898
  • 1
  • 12
  • 25
  • Haven't used it myself yet, but using [CFFI](https://cffi.readthedocs.io/en/latest/overview.html) this should be pretty simple I think. See [this answer](https://stackoverflow.com/a/16290289/1599111) for the numpy array specifics. – Lukas Graf Dec 11 '18 at 21:24
  • Thanks. I'll check this out. But I think I'm actually going to use Boost. The problem is I can't really find a good tutorial on it, and I have no experience with wrapping. – 24n8 Dec 11 '18 at 21:25
  • try pybind11, [link](https://pybind11.readthedocs.io/en/master/basics.html#creating-bindings-for-a-simple-function) – dddd4 Dec 11 '18 at 21:29
  • You also have a data type problem. A numpy array is nothing like a `std::vector>`, so you will have to copy all the data. I'd use [pybind11](https://github.com/pybind/pybind11) that has [buffer protocol](https://pybind11.readthedocs.io/en/master/advanced/pycpp/numpy.html) support and support for [stl container](https://pybind11.readthedocs.io/en/master/advanced/cast/stl.html) – Thomas Dec 11 '18 at 21:33

1 Answers1

1

As suggested in the comments Pybind11 can be used to write c++ bindings for python Pybind Documentation, Pybind Github Repo, Example how to use it.

The Reason why everyone is proposing Pybind over Boost can be found in their readme:

The main issue with Boost.Python—and the reason for creating such a similar project—is Boost. Boost is an enormously large and complex suite of utility libraries that works with almost every C++ compiler in existence. This compatibility has its cost: arcane template tricks and workarounds are necessary to support the oldest and buggiest of compiler specimens. Now that C++11-compatible compilers are widely available, this heavy machinery has become an excessively large and unnecessary dependency.

ohlr
  • 1,839
  • 1
  • 13
  • 29