mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-12 17:26:13 +00:00
Fix eigen copying of non-standard stride values
Some Eigen objects, such as those returned by matrix.diagonal() and matrix.block() have non-standard stride values because they are basically just maps onto the underlying matrix without copying it (for example, the primary diagonal of a 3x3 matrix is a vector-like object with .src equal to the full matrix data, but with stride 4). Returning such an object from a pybind11 method breaks, however, because pybind11 assumes vectors have stride 1, and that matrices have strides equal to the number of rows/columns or 1 (depending on whether the matrix is stored column-major or row-major). This commit fixes the issue by making pybind11 use Eigen's stride methods when copying the data.
This commit is contained in:
@@ -56,6 +56,16 @@ void init_eigen(py::module &m) {
|
||||
m.def("cholesky5", &cholesky5);
|
||||
m.def("cholesky6", &cholesky6);
|
||||
|
||||
// Returns diagonals: a vector-like object with an inner stride != 1
|
||||
m.def("diagonal", [](const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.diagonal(); });
|
||||
m.def("diagonal_1", [](const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.diagonal<1>(); });
|
||||
m.def("diagonal_n", [](const Eigen::Ref<const Eigen::MatrixXd> &x, int index) { return x.diagonal(index); });
|
||||
|
||||
// Return a block of a matrix (gives non-standard strides)
|
||||
m.def("block", [](const Eigen::Ref<const Eigen::MatrixXd> &x, int start_row, int start_col, int block_rows, int block_cols) {
|
||||
return x.block(start_row, start_col, block_rows, block_cols);
|
||||
});
|
||||
|
||||
m.def("fixed_r", [mat]() -> FixedMatrixR {
|
||||
return FixedMatrixR(mat);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user