pybind提供了较方便的数据交互方式,有很多c++类型可以直接在Python中使用,但是为了使程序更加高效,往往需要手动转换
?1
void print_vector(const std::vector<int> &v) {
for (auto item : v)
std::cout << item << "\n";
}
>>> print_vector([1, 2, 3])
#这里python可以直接调用c++中的函数,但是传入参数后,参数会默认copy
为了避免复制,可以手动将python中的类型传入到C++中。
void print_list(py::list my_list) {
for (auto item : my_list)
std::cout << item << " ";
}
?2
与上面类似,Eigen::MatrixXd
与numpy.ndarray
也可以通过copy的形式直接在c++或python中使用。
为了避免传值copy,也可以使用传引用的方式,此时c++中要使用Eigen::Ref<const MatrixType>
并且,应该满足1.数据类型相同double与float64;2.Storage orders要相同。 注意如果MatrixType前面没有const此时要求numpy array是writeable才可传引用。为了避免复制,可以bind参数时设置py::arg().noconvert()
c++传到python中时也可以通过设置return value policy等保证传引用
class MyClass {
Eigen::MatrixXd big_mat = Eigen::MatrixXd::Zero(10000, 10000);
public:
Eigen::MatrixXd &getMatrix() { return big_mat; }
const Eigen::MatrixXd &viewMatrix() { return big_mat; }
};
// Later, in binding code:
py::class_<MyClass>(m, "MyClass")
.def(py::init<>())
.def("copy_matrix", &MyClass::getMatrix) // Makes a copy!
.def("get_matrix", &MyClass::getMatrix, py::return_value_policy::reference_internal)
.def("view_matrix", &MyClass::viewMatrix, py::return_value_policy::reference_internal)
;
a = MyClass()
m = a.get_matrix() # flags.writeable = True, flags.owndata = False
v = a.view_matrix() # flags.writeable = False, flags.owndata = False
c = a.copy_matrix() # flags.writeable = True, flags.owndata = True
# m[5,6] and v[5,6] refer to the same element, c[5,6] does not.