11#include <pybind11/pybind11.h>
12#include <pybind11/numpy.h>
14#include <boost/range/counting_range.hpp>
15#include <boost/range/adaptor/transformed.hpp>
17namespace py = pybind11;
18typedef py::array_t<double> Dgm;
21static auto pairify(
void* p, py::ssize_t h, py::ssize_t w) {
22 return [=](py::ssize_t i){
23 char* birth = (
char*)p + i * h;
24 char* death = birth + w;
25 return std::make_pair(*(
double*)birth, *(
double*)death);
29inline auto numpy_to_range_of_pairs(py::array_t<double> dgm) {
30 py::buffer_info buf = dgm.request();
32 if((buf.ndim!=2 || buf.shape[1]!=2) && (buf.ndim!=1 || buf.shape[0]!=0))
33 throw std::runtime_error(
"Diagram must be an array of size n x 2");
35 py::ssize_t stride1 = buf.ndim == 2 ? buf.strides[1] : 0;
36 auto cnt = boost::counting_range<py::ssize_t>(0, buf.shape[0]);
37 return boost::adaptors::transform(cnt, pairify(buf.ptr, buf.strides[0], stride1));