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 | |
Ben North | b063e64 | 2016-07-05 20:01:11 +0100 | [diff] [blame] | 13 | Eigen::VectorXf double_col(const Eigen::VectorXf& x) |
| 14 | { return 2.0f * x; } |
| 15 | |
| 16 | Eigen::RowVectorXf double_row(const Eigen::RowVectorXf& x) |
| 17 | { return 2.0f * x; } |
| 18 | |
Ben North | 3e0e779 | 2016-07-05 21:00:05 +0100 | [diff] [blame] | 19 | Eigen::MatrixXf double_mat_cm(const Eigen::MatrixXf& x) |
| 20 | { return 2.0f * x; } |
| 21 | |
| 22 | typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> MatrixXfRowMajor; |
| 23 | MatrixXfRowMajor double_mat_rm(const MatrixXfRowMajor& x) |
| 24 | { return 2.0f * x; } |
| 25 | |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 26 | void init_eigen(py::module &m) { |
| 27 | typedef Eigen::Matrix<float, 5, 6, Eigen::RowMajor> FixedMatrixR; |
| 28 | typedef Eigen::Matrix<float, 5, 6> FixedMatrixC; |
| 29 | typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> DenseMatrixR; |
| 30 | typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> DenseMatrixC; |
| 31 | typedef Eigen::SparseMatrix<float, Eigen::RowMajor> SparseMatrixR; |
| 32 | typedef Eigen::SparseMatrix<float> SparseMatrixC; |
| 33 | |
| 34 | // Non-symmetric matrix with zero elements |
| 35 | Eigen::MatrixXf mat(5, 6); |
| 36 | mat << 0, 3, 0, 0, 0, 11, 22, 0, 0, 0, 17, 11, 7, 5, 0, 1, 0, 11, 0, |
| 37 | 0, 0, 0, 0, 11, 0, 0, 14, 0, 8, 11; |
| 38 | |
Ben North | b063e64 | 2016-07-05 20:01:11 +0100 | [diff] [blame] | 39 | m.def("double_col", &double_col); |
| 40 | m.def("double_row", &double_row); |
Ben North | 3e0e779 | 2016-07-05 21:00:05 +0100 | [diff] [blame] | 41 | m.def("double_mat_cm", &double_mat_cm); |
| 42 | m.def("double_mat_rm", &double_mat_rm); |
Ben North | b063e64 | 2016-07-05 20:01:11 +0100 | [diff] [blame] | 43 | |
Wenzel Jakob | 9e0a056 | 2016-05-05 20:33:54 +0200 | [diff] [blame] | 44 | m.def("fixed_r", [mat]() -> FixedMatrixR { |
| 45 | return FixedMatrixR(mat); |
| 46 | }); |
| 47 | |
| 48 | m.def("fixed_c", [mat]() -> FixedMatrixC { |
| 49 | return FixedMatrixC(mat); |
| 50 | }); |
| 51 | |
| 52 | m.def("fixed_passthrough_r", [](const FixedMatrixR &m) -> FixedMatrixR { |
| 53 | return m; |
| 54 | }); |
| 55 | |
| 56 | m.def("fixed_passthrough_c", [](const FixedMatrixC &m) -> FixedMatrixC { |
| 57 | return m; |
| 58 | }); |
| 59 | |
| 60 | m.def("dense_r", [mat]() -> DenseMatrixR { |
| 61 | return DenseMatrixR(mat); |
| 62 | }); |
| 63 | |
| 64 | m.def("dense_c", [mat]() -> DenseMatrixC { |
| 65 | return DenseMatrixC(mat); |
| 66 | }); |
| 67 | |
| 68 | m.def("dense_passthrough_r", [](const DenseMatrixR &m) -> DenseMatrixR { |
| 69 | return m; |
| 70 | }); |
| 71 | |
| 72 | m.def("dense_passthrough_c", [](const DenseMatrixC &m) -> DenseMatrixC { |
| 73 | return m; |
| 74 | }); |
| 75 | |
| 76 | m.def("sparse_r", [mat]() -> SparseMatrixR { |
| 77 | return Eigen::SparseView<Eigen::MatrixXf>(mat); |
| 78 | }); |
| 79 | |
| 80 | m.def("sparse_c", [mat]() -> SparseMatrixC { |
| 81 | return Eigen::SparseView<Eigen::MatrixXf>(mat); |
| 82 | }); |
| 83 | |
| 84 | m.def("sparse_passthrough_r", [](const SparseMatrixR &m) -> SparseMatrixR { |
| 85 | return m; |
| 86 | }); |
| 87 | |
| 88 | m.def("sparse_passthrough_c", [](const SparseMatrixC &m) -> SparseMatrixC { |
| 89 | return m; |
| 90 | }); |
| 91 | } |