Код: Выделить всё
// C++ code
#include "Vector.hpp"
#include
namespace py = pybind11;
void solve(const std::function& evaluate_constraints) {
const Vector x = ...;
Vector constraints = ...;
evaluate_constraints(x, constraints);
}
PYBIND11_MODULE(myCppModule, module) {
py::class_(module, "Vector")
.def(py::init(), "Constructor")
.def("__getitem__", [](const Vector& vector, size_t index) {
return vector[index];
})
.def("__setitem__", [](Vector& vector, size_t index, double value) {
vector[index] = value;
});
module.def("solve", &solve);
}
< /code>
# Python code
import myCppModule
def evaluate_constraints(x, constraints):
constraints[0] = function of x
constraints[1] = function of x
...
myCppModule.solve(evaluate_constraints)
Подробнее здесь: https://stackoverflow.com/questions/796 ... dification