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:
Jason Rhinelander
2016-08-04 15:24:41 -04:00
parent 19637536ac
commit 9ffb3dda5f
5 changed files with 84 additions and 5 deletions

View File

@@ -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);
});