I'm using pybind to wrap C++ code.
Given the following function, which is originally part of a library and should not be modified:
void manipulate(Eigen::MatrixXd& data) {
data = data*2;
}
Using pybind, I can wrap it as:
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include <Eigen/LU>
namespace py = pybind11;
void wrap1(Eigen::Ref<Eigen::MatrixXd> data){
Eigen::MatrixXd mData(data);
manipulate(mData);
data = mData;
}
PYBIND11_PLUGIN(cod) {
pybind11::module m("cod", "auto-compiled c++ extension");
m.def("wrap1", &wrap1);
return m.ptr();
}
But this creates an unnecessary copy mData
.
This fails to modify the data:
void wrap2(Eigen::MatrixXd& data){
manipulate(data);
}
And this one fails fails to compile:
void wrap3(Eigen::Ref<Eigen::MatrixXd> data){
manipulate(data);
}
How can I avoid creating a copy of data
likewise wrap2
, wrap3
or an equivalent of std::swap
?