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