blob: a9cb9f21c704931e67a6e4e8c96883e7619805ec [file] [log] [blame]
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/eigen.cpp -- automatic conversion of Eigen types
Wenzel Jakob9e0a0562016-05-05 20:33:54 +02003
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
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010#include "pybind11_tests.h"
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020011#include <pybind11/eigen.h>
Jason Rhinelander5fd50742016-08-03 16:50:22 -040012#include <Eigen/Cholesky>
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020013
Ben Northb063e642016-07-05 20:01:11 +010014Eigen::VectorXf double_col(const Eigen::VectorXf& x)
15{ return 2.0f * x; }
16
17Eigen::RowVectorXf double_row(const Eigen::RowVectorXf& x)
18{ return 2.0f * x; }
19
Ben North3e0e7792016-07-05 21:00:05 +010020Eigen::MatrixXf double_mat_cm(const Eigen::MatrixXf& x)
21{ return 2.0f * x; }
22
Jason Rhinelander5fd50742016-08-03 16:50:22 -040023// Different ways of passing via Eigen::Ref; the first and second are the Eigen-recommended
24Eigen::MatrixXd cholesky1(Eigen::Ref<Eigen::MatrixXd> &x) { return x.llt().matrixL(); }
25Eigen::MatrixXd cholesky2(const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.llt().matrixL(); }
26Eigen::MatrixXd cholesky3(const Eigen::Ref<Eigen::MatrixXd> &x) { return x.llt().matrixL(); }
27Eigen::MatrixXd cholesky4(Eigen::Ref<const Eigen::MatrixXd> &x) { return x.llt().matrixL(); }
28Eigen::MatrixXd cholesky5(Eigen::Ref<Eigen::MatrixXd> x) { return x.llt().matrixL(); }
29Eigen::MatrixXd cholesky6(Eigen::Ref<const Eigen::MatrixXd> x) { return x.llt().matrixL(); }
30
Ben North3e0e7792016-07-05 21:00:05 +010031typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> MatrixXfRowMajor;
32MatrixXfRowMajor double_mat_rm(const MatrixXfRowMajor& x)
33{ return 2.0f * x; }
34
Jason Rhinelander52f4be82016-09-03 14:54:22 -040035test_initializer eigen([](py::module &m) {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020036 typedef Eigen::Matrix<float, 5, 6, Eigen::RowMajor> FixedMatrixR;
37 typedef Eigen::Matrix<float, 5, 6> FixedMatrixC;
38 typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> DenseMatrixR;
39 typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> DenseMatrixC;
40 typedef Eigen::SparseMatrix<float, Eigen::RowMajor> SparseMatrixR;
41 typedef Eigen::SparseMatrix<float> SparseMatrixC;
42
Jason Rhinelander52f4be82016-09-03 14:54:22 -040043 m.attr("have_eigen") = py::cast(true);
44
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020045 // Non-symmetric matrix with zero elements
46 Eigen::MatrixXf mat(5, 6);
47 mat << 0, 3, 0, 0, 0, 11, 22, 0, 0, 0, 17, 11, 7, 5, 0, 1, 0, 11, 0,
48 0, 0, 0, 0, 11, 0, 0, 14, 0, 8, 11;
49
Ben Northb063e642016-07-05 20:01:11 +010050 m.def("double_col", &double_col);
51 m.def("double_row", &double_row);
Ben North3e0e7792016-07-05 21:00:05 +010052 m.def("double_mat_cm", &double_mat_cm);
53 m.def("double_mat_rm", &double_mat_rm);
Jason Rhinelander5fd50742016-08-03 16:50:22 -040054 m.def("cholesky1", &cholesky1);
55 m.def("cholesky2", &cholesky2);
56 m.def("cholesky3", &cholesky3);
57 m.def("cholesky4", &cholesky4);
58 m.def("cholesky5", &cholesky5);
59 m.def("cholesky6", &cholesky6);
Ben Northb063e642016-07-05 20:01:11 +010060
Jason Rhinelander8657f302016-08-04 13:21:39 -040061 // Returns diagonals: a vector-like object with an inner stride != 1
62 m.def("diagonal", [](const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.diagonal(); });
63 m.def("diagonal_1", [](const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.diagonal<1>(); });
64 m.def("diagonal_n", [](const Eigen::Ref<const Eigen::MatrixXd> &x, int index) { return x.diagonal(index); });
65
66 // Return a block of a matrix (gives non-standard strides)
67 m.def("block", [](const Eigen::Ref<const Eigen::MatrixXd> &x, int start_row, int start_col, int block_rows, int block_cols) {
68 return x.block(start_row, start_col, block_rows, block_cols);
69 });
70
Jason Rhinelander9ffb3dd2016-08-04 15:24:41 -040071 // Returns a DiagonalMatrix with diagonal (1,2,3,...)
72 m.def("incr_diag", [](int k) {
73 Eigen::DiagonalMatrix<int, Eigen::Dynamic> m(k);
74 for (int i = 0; i < k; i++) m.diagonal()[i] = i+1;
75 return m;
76 });
77
78 // Returns a SelfAdjointView referencing the lower triangle of m
79 m.def("symmetric_lower", [](const Eigen::MatrixXi &m) {
80 return m.selfadjointView<Eigen::Lower>();
81 });
82 // Returns a SelfAdjointView referencing the lower triangle of m
83 m.def("symmetric_upper", [](const Eigen::MatrixXi &m) {
84 return m.selfadjointView<Eigen::Upper>();
85 });
86
Wenzel Jakobfe342412016-09-06 13:02:29 +090087 m.def("fixed_r", [mat]() -> FixedMatrixR {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020088 return FixedMatrixR(mat);
89 });
90
Wenzel Jakobfe342412016-09-06 13:02:29 +090091 m.def("fixed_c", [mat]() -> FixedMatrixC {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020092 return FixedMatrixC(mat);
93 });
94
Wenzel Jakobfe342412016-09-06 13:02:29 +090095 m.def("fixed_passthrough_r", [](const FixedMatrixR &m) -> FixedMatrixR {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +020096 return m;
97 });
98
Wenzel Jakobfe342412016-09-06 13:02:29 +090099 m.def("fixed_passthrough_c", [](const FixedMatrixC &m) -> FixedMatrixC {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200100 return m;
101 });
102
Wenzel Jakobfe342412016-09-06 13:02:29 +0900103 m.def("dense_r", [mat]() -> DenseMatrixR {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200104 return DenseMatrixR(mat);
105 });
106
Wenzel Jakobfe342412016-09-06 13:02:29 +0900107 m.def("dense_c", [mat]() -> DenseMatrixC {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200108 return DenseMatrixC(mat);
109 });
110
Wenzel Jakobfe342412016-09-06 13:02:29 +0900111 m.def("dense_passthrough_r", [](const DenseMatrixR &m) -> DenseMatrixR {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200112 return m;
113 });
114
Wenzel Jakobfe342412016-09-06 13:02:29 +0900115 m.def("dense_passthrough_c", [](const DenseMatrixC &m) -> DenseMatrixC {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200116 return m;
117 });
118
Wenzel Jakobfe342412016-09-06 13:02:29 +0900119 m.def("sparse_r", [mat]() -> SparseMatrixR {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200120 return Eigen::SparseView<Eigen::MatrixXf>(mat);
121 });
122
Wenzel Jakobfe342412016-09-06 13:02:29 +0900123 m.def("sparse_c", [mat]() -> SparseMatrixC {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200124 return Eigen::SparseView<Eigen::MatrixXf>(mat);
125 });
126
Wenzel Jakobfe342412016-09-06 13:02:29 +0900127 m.def("sparse_passthrough_r", [](const SparseMatrixR &m) -> SparseMatrixR {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200128 return m;
129 });
130
Wenzel Jakobfe342412016-09-06 13:02:29 +0900131 m.def("sparse_passthrough_c", [](const SparseMatrixC &m) -> SparseMatrixC {
Wenzel Jakob9e0a0562016-05-05 20:33:54 +0200132 return m;
133 });
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400134});