-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathexample.cpp
More file actions
74 lines (54 loc) · 1.41 KB
/
Copy pathexample.cpp
File metadata and controls
74 lines (54 loc) · 1.41 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <Eigen/LU>
// ------------------
// regular C++ header
// ------------------
// a custom vector-class, with one function "mul"
class CustomVectorXd
{
private:
Eigen::VectorXd m_data;
public:
CustomVectorXd(const Eigen::VectorXd &data);
Eigen::VectorXd mul(double factor=1.);
};
// ----------------
// regular C++ code
// ----------------
// class-constructor: store the input "Eigen::VectorXd"
CustomVectorXd::CustomVectorXd(const Eigen::VectorXd &data) : m_data(data)
{
}
// return the custom vector, multiplied by a factor
Eigen::VectorXd CustomVectorXd::mul(double factor)
{
return factor*m_data;
}
// a function that has nothing to do with the class
// the point is to show how one can return a copy "Eigen::VectorXd"
Eigen::VectorXi trans(const Eigen::VectorXi& array)
{
auto N = array.size();
Eigen::VectorXi out(N);
for ( auto i = 0 ; i < N ; ++i )
out[array.size()-i-1] = array[i];
return out;
}
// ----------------
// Python interface
// ----------------
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
m.doc() = "pybind11 example plugin";
m.def("trans", &trans);
py::class_<CustomVectorXd>(m, "CustomVectorXd")
.def(py::init<Eigen::VectorXd>())
.def("mul", &CustomVectorXd::mul,pybind11::arg("factor")=1.)
.def("__repr__",
[](const CustomVectorXd &a) {
return "<example.CustomVectorXd>";
}
);
}