Returning an Array to Python
pybind11
allows us to construct a NumPy array in C++ and have it
returned to python. To do this, we need to use the pybind11
classes that create a python array.
In pybind11
, the array class is pybind11::array_t<>
. The constructor
Here’s an example that computes a Hilbert matrix in C++ and returns it to python.
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
pybind11::array_t<double> hilbert(const int N) {
// construct the numpy array we will return
// we need to specify the strides manually
constexpr size_t elsize = sizeof(double);
size_t shape[2]{N, N};
size_t strides[2]{N * elsize, elsize};
auto H = pybind11::array_t<double>(shape, strides);
auto H_view = H.mutable_unchecked<2>();
for (int i = 0; i < H.shape(0); ++i) {
for (int j = 0; j < H.shape(1); ++j) {
H_view(i, j) = 1.0 / (static_cast<double>(i + j) + 1.0);
}
}
return H;
}
PYBIND11_MODULE(array, m) {
m.doc() = "simple example of creating a NumPy array (Hilbert matrix) in C++";
m.def("hilbert", &hilbert);
}
The GNUmakefile
is essentially the same, but we just need to
give the name of the library (BASE
in the GNUmakefile
):
GNUmakefile
.