File: bindings.cpp

package info (click to toggle)
python-cmake-build-extension 0.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 484 kB
  • sloc: python: 504; cpp: 70; sh: 13; makefile: 5
file content (29 lines) | stat: -rw-r--r-- 888 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "mymath.h"

#include <Eigen/Dense>
#include <pybind11/eigen.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include <string>

// Create the Python module
PYBIND11_MODULE(bindings, module)
{
    namespace py = ::pybind11;
    module.doc() = "mymath bindings";

    // These methods accept numpy array but return float / list
    module.def("dot", &mymath::dot, py::arg("vector1"), py::arg("vector2"));
    module.def("normalize", &mymath::normalize, py::arg("data"));

    // In order to return directly a numpy object, we can pass through Eigen
    module.def(
        "normalize_numpy",
        [](const std::vector<double>& data) -> Eigen::VectorXd {
            const auto output = mymath::normalize(data);
            return Eigen::Map<Eigen::VectorXd>(
                const_cast<double*>(output.data()), output.size());
        },
        py::arg("data"));
}