From b6bf8aefee5692c8da0a34a1cfc536a57fcfb2e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= <1671644+arrufat@users.noreply.github.com> Date: Sun, 22 Nov 2020 07:09:06 +0900 Subject: [PATCH] Add support for matrix serialization to python API (#2241) * Add support for matrix serialization to python API * add double to function names --- tools/python/src/matrix.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/python/src/matrix.cpp b/tools/python/src/matrix.cpp index a93544820..ceb0f20d7 100644 --- a/tools/python/src/matrix.cpp +++ b/tools/python/src/matrix.cpp @@ -108,6 +108,16 @@ long matrix_double__len__(matrix& c) return c.nr(); } +void matrix_double_serialize(const matrix& m, const std::string& file) +{ + serialize(file) << m; +} + +void matrix_double_deserialize(matrix& m, const std::string& file) +{ + deserialize(file) >> m; +} + struct mat_row { mat_row() : data(0),size(0) {} @@ -202,6 +212,8 @@ void bind_matrix(py::module& m) .def("__str__", &matrix_double__str__) .def("nr", &matrix::nr, "Return the number of rows in the matrix.") .def("nc", &matrix::nc, "Return the number of columns in the matrix.") + .def("serialize", &matrix_double_serialize, py::arg("file"), "Serialize the matrix to a file") + .def("deserialize", &matrix_double_deserialize, py::arg("file"), "Deserialize the matrix from a file") .def("__len__", &matrix_double__len__) .def("__getitem__", &matrix_double__getitem__, py::keep_alive<0,1>()) .def_property_readonly("shape", &get_matrix_size)