Boost.python Numpy example
The following C++ source code exposes a C++ function to Python. The function takes a Numpy array as an argument and extracts a C++ integer type.
[sourcecode language="c++"]
#include
#include “boost/python/extract.hpp”
#include “boost/python/numeric.hpp”
#include
using namespace boost::python;
// Functions to demonstrate extraction
void setArray(boost::python::numeric::array data) {
// Access a built-in type (an array)
boost::python::numeric::array a = data;
// Need to
std::cout << "First array item: " << extract
}
// Expose classes and methods to Python
BOOST_PYTHON_MODULE(TestNumPy) {
boost::python::numeric::array::set_module_and_type(“numpy”, “ndarray”);
def(“setArray”, &setArray);
}[/sourcecode]