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.
diff --git a/docs/advanced.rst b/docs/advanced.rst
index 3ebf83e..27df2d8 100644
--- a/docs/advanced.rst
+++ b/docs/advanced.rst
@@ -1098,6 +1098,14 @@
1. Static and dynamic Eigen dense vectors and matrices to instances of
``numpy.ndarray`` (and vice versa).
+1. Returned matrix expressions such as blocks (including columns or rows) and
+ diagonals will be converted to ``numpy.ndarray`` of the expression
+ values.
+
+1. Returned matrix-like objects such as Eigen::DiagonalMatrix or
+ Eigen::SelfAdjointView will be converted to ``numpy.ndarray`` containing the
+ expressed value.
+
1. Eigen sparse vectors and matrices to instances of
``scipy.sparse.csr_matrix``/``scipy.sparse.csc_matrix`` (and vice versa).
@@ -1107,10 +1115,13 @@
.. code-block:: cpp
- /* The Python bindings of this function won't replicate
- the intended effect of modifying the function argument */
+ /* The Python bindings of these functions won't replicate
+ the intended effect of modifying the function arguments */
void scale_by_2(Eigen::Vector3f &v) {
- v *= 2;
+ v *= 2;
+ }
+ void scale_by_2(Eigen::Ref<Eigen::MatrixXd> &v) {
+ v *= 2;
}
To see why this is, refer to the section on :ref:`opaque` (although that