C++ functional with Eigen pass-by-reference issue #4081
-
I am trying to create a C++ method that accepts a functional, which will then get passed a python function, e.g. using CustomErrorFunction = std::function<Eigen::VectorXd(const Eigen::MatrixXd& values, std::vector<Eigen::MatrixXd> *)>;
class Model {
CustomErrorFunction error_function_;
public:
Model(const CustomErrorFunction& func): error_function_(func) {}
/// This method is not wrapped to python since it is only ever called by C++ classes by design
Eigen::VectorXd error(const Eigen::MatrixXd& values, boost::optional<std::vector<Eigen::MatrixXd>&> Jacobians) {
if (Jacobians)
Vector error = this->error_function_(values, Jacobians.get_ptr());
else
Vector error = this->error_function_(values, nullptr);
return error;
}
}; The idea behind the functional is to compute an error and return the jacobians as an output argument, with the error and jacobians being computed on the Python side of things: def custom_error(x: np.ndarray, J: List[np.ndarray]):
if J is not None:
# some random jacobian(s) for illustration
J[0] = np.eye(3)
J[1] = -np.eye(3)
return np.linalg.norm(x) # return the L2 norm
model = Model(custom_error)
e = model.error(np.arange(3)) Then binding is done as follows: py::class_<Model, boost::shared_ptr<Model>>(m_, "Model")
.def(py::init<>())
.def(py::init<const CustomErrorFunction&>(), py::arg("errorFunction")); Note that we do not wrap The issue we're facing is that the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Quick follow up: This seems to be a known issue #1200 |
Beta Was this translation helpful? Give feedback.
Quick follow up: This seems to be a known issue #1200
I'll simply follow up there.