diff --git a/docs/advanced.rst b/docs/advanced.rst index d35192be3..eb111768b 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -863,26 +863,28 @@ objects (e.g. a NumPy matrix). py::class_(m, "MatrixXd") .def("__init__", [](Eigen::MatrixXd &m, py::buffer b) { + typedef Eigen::Stride Strides; + typedef Eigen::MatrixXd Matrix; + typedef Matrix::Scalar Scalar; + /* Request a buffer descriptor from Python */ py::buffer_info info = b.request(); /* Some sanity checks ... */ - if (info.format != py::format_descriptor::value) + if (info.format != py::format_descriptor::value) throw std::runtime_error("Incompatible format: expected a double array!"); if (info.ndim != 2) throw std::runtime_error("Incompatible buffer dimension!"); - if (info.strides[0] == sizeof(double)) { - /* Buffer has the right layout -- directly copy. */ - new (&m) Eigen::MatrixXd(info.shape[0], info.shape[1]); - memcpy(m.data(), info.ptr, sizeof(double) * m.size()); - } else { - /* Oops -- the buffer is transposed */ - new (&m) Eigen::MatrixXd(info.shape[1], info.shape[0]); - memcpy(m.data(), info.ptr, sizeof(double) * m.size()); - m.transposeInPlace(); - } + auto strides = Strides( + info.strides[Matrix::Flags & Eigen::RowMajorBit ? 0 : 1] / sizeof(Scalar), + info.strides[Matrix::Flags & Eigen::RowMajorBit ? 1 : 0] / sizeof(Scalar)); + + auto map = Eigen::Map( + (Scalar *) info.ptr, info.shape[0], info.shape[1], strides); + + new (&m) Matrix(map); }); .. seealso::