mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-12 09:17:42 +00:00
Eigen support for special matrix objects
Functions returning specialized Eigen matrices like Eigen::DiagonalMatrix and Eigen::SelfAdjointView--which inherit from EigenBase but not DenseBase--isn't currently allowed; such classes are explicitly copyable into a Matrix (by definition), and so we can support functions that return them by copying the value into a Matrix then casting that resulting dense Matrix into a numpy.ndarray. This commit does exactly that.
This commit is contained in:
@@ -66,6 +66,22 @@ void init_eigen(py::module &m) {
|
||||
return x.block(start_row, start_col, block_rows, block_cols);
|
||||
});
|
||||
|
||||
// Returns a DiagonalMatrix with diagonal (1,2,3,...)
|
||||
m.def("incr_diag", [](int k) {
|
||||
Eigen::DiagonalMatrix<int, Eigen::Dynamic> m(k);
|
||||
for (int i = 0; i < k; i++) m.diagonal()[i] = i+1;
|
||||
return m;
|
||||
});
|
||||
|
||||
// Returns a SelfAdjointView referencing the lower triangle of m
|
||||
m.def("symmetric_lower", [](const Eigen::MatrixXi &m) {
|
||||
return m.selfadjointView<Eigen::Lower>();
|
||||
});
|
||||
// Returns a SelfAdjointView referencing the lower triangle of m
|
||||
m.def("symmetric_upper", [](const Eigen::MatrixXi &m) {
|
||||
return m.selfadjointView<Eigen::Upper>();
|
||||
});
|
||||
|
||||
m.def("fixed_r", [mat]() -> FixedMatrixR {
|
||||
return FixedMatrixR(mat);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user