You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
2.0 KiB

  1. Eigen
  2. =====
  3. `Eigen <http://eigen.tuxfamily.org>`_ is C++ header-based library for dense and
  4. sparse linear algebra. Due to its popularity and widespread adoption, pybind11
  5. provides transparent conversion support between Eigen and Scientific Python linear
  6. algebra data types.
  7. Specifically, when including the optional header file :file:`pybind11/eigen.h`,
  8. pybind11 will automatically and transparently convert
  9. 1. Static and dynamic Eigen dense vectors and matrices to instances of
  10. ``numpy.ndarray`` (and vice versa).
  11. 2. Returned matrix expressions such as blocks (including columns or rows) and
  12. diagonals will be converted to ``numpy.ndarray`` of the expression
  13. values.
  14. 3. Returned matrix-like objects such as Eigen::DiagonalMatrix or
  15. Eigen::SelfAdjointView will be converted to ``numpy.ndarray`` containing the
  16. expressed value.
  17. 4. Eigen sparse vectors and matrices to instances of
  18. ``scipy.sparse.csr_matrix``/``scipy.sparse.csc_matrix`` (and vice versa).
  19. This makes it possible to bind most kinds of functions that rely on these types.
  20. One major caveat are functions that take Eigen matrices *by reference* and modify
  21. them somehow, in which case the information won't be propagated to the caller.
  22. .. code-block:: cpp
  23. /* The Python bindings of these functions won't replicate
  24. the intended effect of modifying the function arguments */
  25. void scale_by_2(Eigen::Vector3f &v) {
  26. v *= 2;
  27. }
  28. void scale_by_2(Eigen::Ref<Eigen::MatrixXd> &v) {
  29. v *= 2;
  30. }
  31. To see why this is, refer to the section on :ref:`opaque` (although that
  32. section specifically covers STL data types, the underlying issue is the same).
  33. The :ref:`numpy` sections discuss an efficient alternative for exposing the
  34. underlying native Eigen types as opaque objects in a way that still integrates
  35. with NumPy and SciPy.
  36. .. seealso::
  37. The file :file:`tests/test_eigen.cpp` contains a complete example that
  38. shows how to pass Eigen sparse and dense data types in more detail.