-
Notifications
You must be signed in to change notification settings - Fork 88
/
example.cpp
44 lines (35 loc) · 952 Bytes
/
example.cpp
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
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
// ----------------
// Regular C++ code
// ----------------
// multiply all entries by 2.0
// input: std::vector ([...]) (read only)
// output: std::vector ([...]) (new copy)
std::vector<double> modify(const std::vector<double>& input)
{
std::vector<double> output;
std::transform(
input.begin(),
input.end(),
std::back_inserter(output),
[](double x) -> double { return 2.*x; }
);
// N.B. this is equivalent to (but there are also other ways to do the same)
//
// std::vector<double> output(input.size());
//
// for ( size_t i = 0 ; i < input.size() ; ++i )
// output[i] = 2. * input[i];
return output;
}
// ----------------
// Python interface
// ----------------
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
m.doc() = "pybind11 example plugin";
m.def("modify", &modify, "Multiply all entries of a list by 2.0");
}