eigen.rst revision 11986
111986Sandreas.sandberg@arm.comEigen 211986Sandreas.sandberg@arm.com===== 311986Sandreas.sandberg@arm.com 411986Sandreas.sandberg@arm.com`Eigen <http://eigen.tuxfamily.org>`_ is C++ header-based library for dense and 511986Sandreas.sandberg@arm.comsparse linear algebra. Due to its popularity and widespread adoption, pybind11 611986Sandreas.sandberg@arm.comprovides transparent conversion support between Eigen and Scientific Python linear 711986Sandreas.sandberg@arm.comalgebra data types. 811986Sandreas.sandberg@arm.com 911986Sandreas.sandberg@arm.comSpecifically, when including the optional header file :file:`pybind11/eigen.h`, 1011986Sandreas.sandberg@arm.compybind11 will automatically and transparently convert 1111986Sandreas.sandberg@arm.com 1211986Sandreas.sandberg@arm.com1. Static and dynamic Eigen dense vectors and matrices to instances of 1311986Sandreas.sandberg@arm.com ``numpy.ndarray`` (and vice versa). 1411986Sandreas.sandberg@arm.com 1511986Sandreas.sandberg@arm.com2. Returned matrix expressions such as blocks (including columns or rows) and 1611986Sandreas.sandberg@arm.com diagonals will be converted to ``numpy.ndarray`` of the expression 1711986Sandreas.sandberg@arm.com values. 1811986Sandreas.sandberg@arm.com 1911986Sandreas.sandberg@arm.com3. Returned matrix-like objects such as Eigen::DiagonalMatrix or 2011986Sandreas.sandberg@arm.com Eigen::SelfAdjointView will be converted to ``numpy.ndarray`` containing the 2111986Sandreas.sandberg@arm.com expressed value. 2211986Sandreas.sandberg@arm.com 2311986Sandreas.sandberg@arm.com4. Eigen sparse vectors and matrices to instances of 2411986Sandreas.sandberg@arm.com ``scipy.sparse.csr_matrix``/``scipy.sparse.csc_matrix`` (and vice versa). 2511986Sandreas.sandberg@arm.com 2611986Sandreas.sandberg@arm.comThis makes it possible to bind most kinds of functions that rely on these types. 2711986Sandreas.sandberg@arm.comOne major caveat are functions that take Eigen matrices *by reference* and modify 2811986Sandreas.sandberg@arm.comthem somehow, in which case the information won't be propagated to the caller. 2911986Sandreas.sandberg@arm.com 3011986Sandreas.sandberg@arm.com.. code-block:: cpp 3111986Sandreas.sandberg@arm.com 3211986Sandreas.sandberg@arm.com /* The Python bindings of these functions won't replicate 3311986Sandreas.sandberg@arm.com the intended effect of modifying the function arguments */ 3411986Sandreas.sandberg@arm.com void scale_by_2(Eigen::Vector3f &v) { 3511986Sandreas.sandberg@arm.com v *= 2; 3611986Sandreas.sandberg@arm.com } 3711986Sandreas.sandberg@arm.com void scale_by_2(Eigen::Ref<Eigen::MatrixXd> &v) { 3811986Sandreas.sandberg@arm.com v *= 2; 3911986Sandreas.sandberg@arm.com } 4011986Sandreas.sandberg@arm.com 4111986Sandreas.sandberg@arm.comTo see why this is, refer to the section on :ref:`opaque` (although that 4211986Sandreas.sandberg@arm.comsection specifically covers STL data types, the underlying issue is the same). 4311986Sandreas.sandberg@arm.comThe :ref:`numpy` sections discuss an efficient alternative for exposing the 4411986Sandreas.sandberg@arm.comunderlying native Eigen types as opaque objects in a way that still integrates 4511986Sandreas.sandberg@arm.comwith NumPy and SciPy. 4611986Sandreas.sandberg@arm.com 4711986Sandreas.sandberg@arm.com.. seealso:: 4811986Sandreas.sandberg@arm.com 4911986Sandreas.sandberg@arm.com The file :file:`tests/test_eigen.cpp` contains a complete example that 5011986Sandreas.sandberg@arm.com shows how to pass Eigen sparse and dense data types in more detail. 51