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.

73 lines
2.1 KiB

  1. /*
  2. example/eigen.cpp -- automatic conversion of Eigen types
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "example.h"
  8. #include <pybind11/eigen.h>
  9. void init_eigen(py::module &m) {
  10. typedef Eigen::Matrix<float, 5, 6, Eigen::RowMajor> FixedMatrixR;
  11. typedef Eigen::Matrix<float, 5, 6> FixedMatrixC;
  12. typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> DenseMatrixR;
  13. typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> DenseMatrixC;
  14. typedef Eigen::SparseMatrix<float, Eigen::RowMajor> SparseMatrixR;
  15. typedef Eigen::SparseMatrix<float> SparseMatrixC;
  16. // Non-symmetric matrix with zero elements
  17. Eigen::MatrixXf mat(5, 6);
  18. mat << 0, 3, 0, 0, 0, 11, 22, 0, 0, 0, 17, 11, 7, 5, 0, 1, 0, 11, 0,
  19. 0, 0, 0, 0, 11, 0, 0, 14, 0, 8, 11;
  20. m.def("fixed_r", [mat]() -> FixedMatrixR {
  21. return FixedMatrixR(mat);
  22. });
  23. m.def("fixed_c", [mat]() -> FixedMatrixC {
  24. return FixedMatrixC(mat);
  25. });
  26. m.def("fixed_passthrough_r", [](const FixedMatrixR &m) -> FixedMatrixR {
  27. return m;
  28. });
  29. m.def("fixed_passthrough_c", [](const FixedMatrixC &m) -> FixedMatrixC {
  30. return m;
  31. });
  32. m.def("dense_r", [mat]() -> DenseMatrixR {
  33. return DenseMatrixR(mat);
  34. });
  35. m.def("dense_c", [mat]() -> DenseMatrixC {
  36. return DenseMatrixC(mat);
  37. });
  38. m.def("dense_passthrough_r", [](const DenseMatrixR &m) -> DenseMatrixR {
  39. return m;
  40. });
  41. m.def("dense_passthrough_c", [](const DenseMatrixC &m) -> DenseMatrixC {
  42. return m;
  43. });
  44. m.def("sparse_r", [mat]() -> SparseMatrixR {
  45. return Eigen::SparseView<Eigen::MatrixXf>(mat);
  46. });
  47. m.def("sparse_c", [mat]() -> SparseMatrixC {
  48. return Eigen::SparseView<Eigen::MatrixXf>(mat);
  49. });
  50. m.def("sparse_passthrough_r", [](const SparseMatrixR &m) -> SparseMatrixR {
  51. return m;
  52. });
  53. m.def("sparse_passthrough_c", [](const SparseMatrixC &m) -> SparseMatrixC {
  54. return m;
  55. });
  56. }