test_eigen.cpp (12037:d28054ac6ec9) | test_eigen.cpp (12391:ceeca8b41e4b) |
---|---|
1/* 2 tests/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 "pybind11_tests.h" 11#include "constructor_stats.h" 12#include <pybind11/eigen.h> | 1/* 2 tests/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 "pybind11_tests.h" 11#include "constructor_stats.h" 12#include <pybind11/eigen.h> |
13#include <pybind11/stl.h> |
|
13#include <Eigen/Cholesky> 14 15using MatrixXdR = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>; 16 17 18 19// Sets/resets a testing reference matrix to have values of 10*r + c, where r and c are the 20// (1-based) row/column number. --- 43 unchanged lines hidden (view full) --- 64 CustomOperatorNew() = default; 65 66 Eigen::Matrix4d a = Eigen::Matrix4d::Zero(); 67 Eigen::Matrix4d b = Eigen::Matrix4d::Identity(); 68 69 EIGEN_MAKE_ALIGNED_OPERATOR_NEW; 70}; 71 | 14#include <Eigen/Cholesky> 15 16using MatrixXdR = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>; 17 18 19 20// Sets/resets a testing reference matrix to have values of 10*r + c, where r and c are the 21// (1-based) row/column number. --- 43 unchanged lines hidden (view full) --- 65 CustomOperatorNew() = default; 66 67 Eigen::Matrix4d a = Eigen::Matrix4d::Zero(); 68 Eigen::Matrix4d b = Eigen::Matrix4d::Identity(); 69 70 EIGEN_MAKE_ALIGNED_OPERATOR_NEW; 71}; 72 |
72test_initializer eigen([](py::module &m) { 73 typedef Eigen::Matrix<float, 5, 6, Eigen::RowMajor> FixedMatrixR; 74 typedef Eigen::Matrix<float, 5, 6> FixedMatrixC; 75 typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> DenseMatrixR; 76 typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> DenseMatrixC; 77 typedef Eigen::Matrix<float, 4, Eigen::Dynamic> FourRowMatrixC; 78 typedef Eigen::Matrix<float, Eigen::Dynamic, 4> FourColMatrixC; 79 typedef Eigen::Matrix<float, 4, Eigen::Dynamic> FourRowMatrixR; 80 typedef Eigen::Matrix<float, Eigen::Dynamic, 4> FourColMatrixR; 81 typedef Eigen::SparseMatrix<float, Eigen::RowMajor> SparseMatrixR; 82 typedef Eigen::SparseMatrix<float> SparseMatrixC; | 73TEST_SUBMODULE(eigen, m) { 74 using FixedMatrixR = Eigen::Matrix<float, 5, 6, Eigen::RowMajor>; 75 using FixedMatrixC = Eigen::Matrix<float, 5, 6>; 76 using DenseMatrixR = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>; 77 using DenseMatrixC = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic>; 78 using FourRowMatrixC = Eigen::Matrix<float, 4, Eigen::Dynamic>; 79 using FourColMatrixC = Eigen::Matrix<float, Eigen::Dynamic, 4>; 80 using FourRowMatrixR = Eigen::Matrix<float, 4, Eigen::Dynamic>; 81 using FourColMatrixR = Eigen::Matrix<float, Eigen::Dynamic, 4>; 82 using SparseMatrixR = Eigen::SparseMatrix<float, Eigen::RowMajor>; 83 using SparseMatrixC = Eigen::SparseMatrix<float>; |
83 84 m.attr("have_eigen") = true; 85 | 84 85 m.attr("have_eigen") = true; 86 |
87 // various tests |
|
86 m.def("double_col", [](const Eigen::VectorXf &x) -> Eigen::VectorXf { return 2.0f * x; }); 87 m.def("double_row", [](const Eigen::RowVectorXf &x) -> Eigen::RowVectorXf { return 2.0f * x; }); 88 m.def("double_complex", [](const Eigen::VectorXcf &x) -> Eigen::VectorXcf { return 2.0f * x; }); 89 m.def("double_threec", [](py::EigenDRef<Eigen::Vector3f> x) { x *= 2; }); 90 m.def("double_threer", [](py::EigenDRef<Eigen::RowVector3f> x) { x *= 2; }); 91 m.def("double_mat_cm", [](Eigen::MatrixXf x) -> Eigen::MatrixXf { return 2.0f * x; }); 92 m.def("double_mat_rm", [](DenseMatrixR x) -> DenseMatrixR { return 2.0f * x; }); 93 | 88 m.def("double_col", [](const Eigen::VectorXf &x) -> Eigen::VectorXf { return 2.0f * x; }); 89 m.def("double_row", [](const Eigen::RowVectorXf &x) -> Eigen::RowVectorXf { return 2.0f * x; }); 90 m.def("double_complex", [](const Eigen::VectorXcf &x) -> Eigen::VectorXcf { return 2.0f * x; }); 91 m.def("double_threec", [](py::EigenDRef<Eigen::Vector3f> x) { x *= 2; }); 92 m.def("double_threer", [](py::EigenDRef<Eigen::RowVector3f> x) { x *= 2; }); 93 m.def("double_mat_cm", [](Eigen::MatrixXf x) -> Eigen::MatrixXf { return 2.0f * x; }); 94 m.def("double_mat_rm", [](DenseMatrixR x) -> DenseMatrixR { return 2.0f * x; }); 95 |
96 // test_eigen_ref_to_python |
|
94 // Different ways of passing via Eigen::Ref; the first and second are the Eigen-recommended 95 m.def("cholesky1", [](Eigen::Ref<MatrixXdR> x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); 96 m.def("cholesky2", [](const Eigen::Ref<const MatrixXdR> &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); 97 m.def("cholesky3", [](const Eigen::Ref<MatrixXdR> &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); 98 m.def("cholesky4", [](Eigen::Ref<const MatrixXdR> x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); 99 | 97 // Different ways of passing via Eigen::Ref; the first and second are the Eigen-recommended 98 m.def("cholesky1", [](Eigen::Ref<MatrixXdR> x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); 99 m.def("cholesky2", [](const Eigen::Ref<const MatrixXdR> &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); 100 m.def("cholesky3", [](const Eigen::Ref<MatrixXdR> &x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); 101 m.def("cholesky4", [](Eigen::Ref<const MatrixXdR> x) -> Eigen::MatrixXd { return x.llt().matrixL(); }); 102 |
103 // test_eigen_ref_mutators |
|
100 // Mutators: these add some value to the given element using Eigen, but Eigen should be mapping into 101 // the numpy array data and so the result should show up there. There are three versions: one that 102 // works on a contiguous-row matrix (numpy's default), one for a contiguous-column matrix, and one 103 // for any matrix. 104 auto add_rm = [](Eigen::Ref<MatrixXdR> x, int r, int c, double v) { x(r,c) += v; }; 105 auto add_cm = [](Eigen::Ref<Eigen::MatrixXd> x, int r, int c, double v) { x(r,c) += v; }; 106 107 // Mutators (Eigen maps into numpy variables): --- 8 unchanged lines hidden (view full) --- 116 m.def("add_any", [](py::EigenDRef<Eigen::MatrixXd> x, int r, int c, double v) { x(r,c) += v; }); 117 118 // Return mutable references (numpy maps into eigen varibles) 119 m.def("get_cm_ref", []() { return Eigen::Ref<Eigen::MatrixXd>(get_cm()); }); 120 m.def("get_rm_ref", []() { return Eigen::Ref<MatrixXdR>(get_rm()); }); 121 // The same references, but non-mutable (numpy maps into eigen variables, but is !writeable) 122 m.def("get_cm_const_ref", []() { return Eigen::Ref<const Eigen::MatrixXd>(get_cm()); }); 123 m.def("get_rm_const_ref", []() { return Eigen::Ref<const MatrixXdR>(get_rm()); }); | 104 // Mutators: these add some value to the given element using Eigen, but Eigen should be mapping into 105 // the numpy array data and so the result should show up there. There are three versions: one that 106 // works on a contiguous-row matrix (numpy's default), one for a contiguous-column matrix, and one 107 // for any matrix. 108 auto add_rm = [](Eigen::Ref<MatrixXdR> x, int r, int c, double v) { x(r,c) += v; }; 109 auto add_cm = [](Eigen::Ref<Eigen::MatrixXd> x, int r, int c, double v) { x(r,c) += v; }; 110 111 // Mutators (Eigen maps into numpy variables): --- 8 unchanged lines hidden (view full) --- 120 m.def("add_any", [](py::EigenDRef<Eigen::MatrixXd> x, int r, int c, double v) { x(r,c) += v; }); 121 122 // Return mutable references (numpy maps into eigen varibles) 123 m.def("get_cm_ref", []() { return Eigen::Ref<Eigen::MatrixXd>(get_cm()); }); 124 m.def("get_rm_ref", []() { return Eigen::Ref<MatrixXdR>(get_rm()); }); 125 // The same references, but non-mutable (numpy maps into eigen variables, but is !writeable) 126 m.def("get_cm_const_ref", []() { return Eigen::Ref<const Eigen::MatrixXd>(get_cm()); }); 127 m.def("get_rm_const_ref", []() { return Eigen::Ref<const MatrixXdR>(get_rm()); }); |
124 // Just the corners (via a Map instead of a Ref): 125 m.def("get_cm_corners", []() { 126 auto &x = get_cm(); 127 return py::EigenDMap<Eigen::Matrix2d>( 128 x.data(), 129 py::EigenDStride(x.outerStride() * (x.rows() - 1), x.innerStride() * (x.cols() - 1))); 130 }); 131 m.def("get_cm_corners_const", []() { 132 const auto &x = get_cm(); 133 return py::EigenDMap<const Eigen::Matrix2d>( 134 x.data(), 135 py::EigenDStride(x.outerStride() * (x.rows() - 1), x.innerStride() * (x.cols() - 1))); 136 }); | |
137 138 m.def("reset_refs", reset_refs); // Restores get_{cm,rm}_ref to original values 139 140 // Increments and returns ref to (same) matrix 141 m.def("incr_matrix", [](Eigen::Ref<Eigen::MatrixXd> m, double v) { 142 m += Eigen::MatrixXd::Constant(m.rows(), m.cols(), v); 143 return m; 144 }, py::return_value_policy::reference); --- 23 unchanged lines hidden (view full) --- 168 m.def("diagonal_1", [](const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.diagonal<1>(); }); 169 m.def("diagonal_n", [](const Eigen::Ref<const Eigen::MatrixXd> &x, int index) { return x.diagonal(index); }); 170 171 // Return a block of a matrix (gives non-standard strides) 172 m.def("block", [](const Eigen::Ref<const Eigen::MatrixXd> &x, int start_row, int start_col, int block_rows, int block_cols) { 173 return x.block(start_row, start_col, block_rows, block_cols); 174 }); 175 | 128 129 m.def("reset_refs", reset_refs); // Restores get_{cm,rm}_ref to original values 130 131 // Increments and returns ref to (same) matrix 132 m.def("incr_matrix", [](Eigen::Ref<Eigen::MatrixXd> m, double v) { 133 m += Eigen::MatrixXd::Constant(m.rows(), m.cols(), v); 134 return m; 135 }, py::return_value_policy::reference); --- 23 unchanged lines hidden (view full) --- 159 m.def("diagonal_1", [](const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.diagonal<1>(); }); 160 m.def("diagonal_n", [](const Eigen::Ref<const Eigen::MatrixXd> &x, int index) { return x.diagonal(index); }); 161 162 // Return a block of a matrix (gives non-standard strides) 163 m.def("block", [](const Eigen::Ref<const Eigen::MatrixXd> &x, int start_row, int start_col, int block_rows, int block_cols) { 164 return x.block(start_row, start_col, block_rows, block_cols); 165 }); 166 |
167 // test_eigen_return_references, test_eigen_keepalive |
|
176 // return value referencing/copying tests: 177 class ReturnTester { 178 Eigen::MatrixXd mat = create(); 179 public: 180 ReturnTester() { print_created(this); } 181 ~ReturnTester() { print_destroyed(this); } 182 static Eigen::MatrixXd create() { return Eigen::MatrixXd::Ones(10, 10); } 183 static const Eigen::MatrixXd createConst() { return Eigen::MatrixXd::Ones(10, 10); } --- 30 unchanged lines hidden (view full) --- 214 .def("block", &ReturnTester::block) 215 .def("block_safe", &ReturnTester::block, rvp::reference_internal) 216 .def("block_const", &ReturnTester::blockConst, rvp::reference_internal) 217 .def("copy_block", &ReturnTester::block, rvp::copy) 218 .def("corners", &ReturnTester::corners, rvp::reference_internal) 219 .def("corners_const", &ReturnTester::cornersConst, rvp::reference_internal) 220 ; 221 | 168 // return value referencing/copying tests: 169 class ReturnTester { 170 Eigen::MatrixXd mat = create(); 171 public: 172 ReturnTester() { print_created(this); } 173 ~ReturnTester() { print_destroyed(this); } 174 static Eigen::MatrixXd create() { return Eigen::MatrixXd::Ones(10, 10); } 175 static const Eigen::MatrixXd createConst() { return Eigen::MatrixXd::Ones(10, 10); } --- 30 unchanged lines hidden (view full) --- 206 .def("block", &ReturnTester::block) 207 .def("block_safe", &ReturnTester::block, rvp::reference_internal) 208 .def("block_const", &ReturnTester::blockConst, rvp::reference_internal) 209 .def("copy_block", &ReturnTester::block, rvp::copy) 210 .def("corners", &ReturnTester::corners, rvp::reference_internal) 211 .def("corners_const", &ReturnTester::cornersConst, rvp::reference_internal) 212 ; 213 |
214 // test_special_matrix_objects |
|
222 // Returns a DiagonalMatrix with diagonal (1,2,3,...) 223 m.def("incr_diag", [](int k) { 224 Eigen::DiagonalMatrix<int, Eigen::Dynamic> m(k); 225 for (int i = 0; i < k; i++) m.diagonal()[i] = i+1; 226 return m; 227 }); 228 229 // Returns a SelfAdjointView referencing the lower triangle of m --- 8 unchanged lines hidden (view full) --- 238 // Test matrix for various functions below. 239 Eigen::MatrixXf mat(5, 6); 240 mat << 0, 3, 0, 0, 0, 11, 241 22, 0, 0, 0, 17, 11, 242 7, 5, 0, 1, 0, 11, 243 0, 0, 0, 0, 0, 11, 244 0, 0, 14, 0, 8, 11; 245 | 215 // Returns a DiagonalMatrix with diagonal (1,2,3,...) 216 m.def("incr_diag", [](int k) { 217 Eigen::DiagonalMatrix<int, Eigen::Dynamic> m(k); 218 for (int i = 0; i < k; i++) m.diagonal()[i] = i+1; 219 return m; 220 }); 221 222 // Returns a SelfAdjointView referencing the lower triangle of m --- 8 unchanged lines hidden (view full) --- 231 // Test matrix for various functions below. 232 Eigen::MatrixXf mat(5, 6); 233 mat << 0, 3, 0, 0, 0, 11, 234 22, 0, 0, 0, 17, 11, 235 7, 5, 0, 1, 0, 11, 236 0, 0, 0, 0, 0, 11, 237 0, 0, 14, 0, 8, 11; 238 |
239 // test_fixed, and various other tests |
|
246 m.def("fixed_r", [mat]() -> FixedMatrixR { return FixedMatrixR(mat); }); 247 m.def("fixed_r_const", [mat]() -> const FixedMatrixR { return FixedMatrixR(mat); }); 248 m.def("fixed_c", [mat]() -> FixedMatrixC { return FixedMatrixC(mat); }); 249 m.def("fixed_copy_r", [](const FixedMatrixR &m) -> FixedMatrixR { return m; }); 250 m.def("fixed_copy_c", [](const FixedMatrixC &m) -> FixedMatrixC { return m; }); | 240 m.def("fixed_r", [mat]() -> FixedMatrixR { return FixedMatrixR(mat); }); 241 m.def("fixed_r_const", [mat]() -> const FixedMatrixR { return FixedMatrixR(mat); }); 242 m.def("fixed_c", [mat]() -> FixedMatrixC { return FixedMatrixC(mat); }); 243 m.def("fixed_copy_r", [](const FixedMatrixR &m) -> FixedMatrixR { return m; }); 244 m.def("fixed_copy_c", [](const FixedMatrixC &m) -> FixedMatrixC { return m; }); |
245 // test_mutator_descriptors |
|
251 m.def("fixed_mutator_r", [](Eigen::Ref<FixedMatrixR>) {}); 252 m.def("fixed_mutator_c", [](Eigen::Ref<FixedMatrixC>) {}); 253 m.def("fixed_mutator_a", [](py::EigenDRef<FixedMatrixC>) {}); | 246 m.def("fixed_mutator_r", [](Eigen::Ref<FixedMatrixR>) {}); 247 m.def("fixed_mutator_c", [](Eigen::Ref<FixedMatrixC>) {}); 248 m.def("fixed_mutator_a", [](py::EigenDRef<FixedMatrixC>) {}); |
249 // test_dense |
|
254 m.def("dense_r", [mat]() -> DenseMatrixR { return DenseMatrixR(mat); }); 255 m.def("dense_c", [mat]() -> DenseMatrixC { return DenseMatrixC(mat); }); 256 m.def("dense_copy_r", [](const DenseMatrixR &m) -> DenseMatrixR { return m; }); 257 m.def("dense_copy_c", [](const DenseMatrixC &m) -> DenseMatrixC { return m; }); | 250 m.def("dense_r", [mat]() -> DenseMatrixR { return DenseMatrixR(mat); }); 251 m.def("dense_c", [mat]() -> DenseMatrixC { return DenseMatrixC(mat); }); 252 m.def("dense_copy_r", [](const DenseMatrixR &m) -> DenseMatrixR { return m; }); 253 m.def("dense_copy_c", [](const DenseMatrixC &m) -> DenseMatrixC { return m; }); |
254 // test_sparse, test_sparse_signature |
|
258 m.def("sparse_r", [mat]() -> SparseMatrixR { return Eigen::SparseView<Eigen::MatrixXf>(mat); }); 259 m.def("sparse_c", [mat]() -> SparseMatrixC { return Eigen::SparseView<Eigen::MatrixXf>(mat); }); 260 m.def("sparse_copy_r", [](const SparseMatrixR &m) -> SparseMatrixR { return m; }); 261 m.def("sparse_copy_c", [](const SparseMatrixC &m) -> SparseMatrixC { return m; }); | 255 m.def("sparse_r", [mat]() -> SparseMatrixR { return Eigen::SparseView<Eigen::MatrixXf>(mat); }); 256 m.def("sparse_c", [mat]() -> SparseMatrixC { return Eigen::SparseView<Eigen::MatrixXf>(mat); }); 257 m.def("sparse_copy_r", [](const SparseMatrixR &m) -> SparseMatrixR { return m; }); 258 m.def("sparse_copy_c", [](const SparseMatrixC &m) -> SparseMatrixC { return m; }); |
259 // test_partially_fixed |
|
262 m.def("partial_copy_four_rm_r", [](const FourRowMatrixR &m) -> FourRowMatrixR { return m; }); 263 m.def("partial_copy_four_rm_c", [](const FourColMatrixR &m) -> FourColMatrixR { return m; }); 264 m.def("partial_copy_four_cm_r", [](const FourRowMatrixC &m) -> FourRowMatrixC { return m; }); 265 m.def("partial_copy_four_cm_c", [](const FourColMatrixC &m) -> FourColMatrixC { return m; }); 266 | 260 m.def("partial_copy_four_rm_r", [](const FourRowMatrixR &m) -> FourRowMatrixR { return m; }); 261 m.def("partial_copy_four_rm_c", [](const FourColMatrixR &m) -> FourColMatrixR { return m; }); 262 m.def("partial_copy_four_cm_r", [](const FourRowMatrixC &m) -> FourRowMatrixC { return m; }); 263 m.def("partial_copy_four_cm_c", [](const FourColMatrixC &m) -> FourColMatrixC { return m; }); 264 |
265 // test_cpp_casting |
|
267 // Test that we can cast a numpy object to a Eigen::MatrixXd explicitly 268 m.def("cpp_copy", [](py::handle m) { return m.cast<Eigen::MatrixXd>()(1, 0); }); 269 m.def("cpp_ref_c", [](py::handle m) { return m.cast<Eigen::Ref<Eigen::MatrixXd>>()(1, 0); }); 270 m.def("cpp_ref_r", [](py::handle m) { return m.cast<Eigen::Ref<MatrixXdR>>()(1, 0); }); 271 m.def("cpp_ref_any", [](py::handle m) { return m.cast<py::EigenDRef<Eigen::MatrixXd>>()(1, 0); }); 272 273 | 266 // Test that we can cast a numpy object to a Eigen::MatrixXd explicitly 267 m.def("cpp_copy", [](py::handle m) { return m.cast<Eigen::MatrixXd>()(1, 0); }); 268 m.def("cpp_ref_c", [](py::handle m) { return m.cast<Eigen::Ref<Eigen::MatrixXd>>()(1, 0); }); 269 m.def("cpp_ref_r", [](py::handle m) { return m.cast<Eigen::Ref<MatrixXdR>>()(1, 0); }); 270 m.def("cpp_ref_any", [](py::handle m) { return m.cast<py::EigenDRef<Eigen::MatrixXd>>()(1, 0); }); 271 272 |
273 // test_nocopy_wrapper |
|
274 // Test that we can prevent copying into an argument that would normally copy: First a version 275 // that would allow copying (if types or strides don't match) for comparison: 276 m.def("get_elem", &get_elem); 277 // Now this alternative that calls the tells pybind to fail rather than copy: 278 m.def("get_elem_nocopy", [](Eigen::Ref<const Eigen::MatrixXd> m) -> double { return get_elem(m); }, 279 py::arg().noconvert()); 280 // Also test a row-major-only no-copy const ref: 281 m.def("get_elem_rm_nocopy", [](Eigen::Ref<const Eigen::Matrix<long, -1, -1, Eigen::RowMajor>> &m) -> long { return m(2, 1); }, 282 py::arg().noconvert()); 283 | 274 // Test that we can prevent copying into an argument that would normally copy: First a version 275 // that would allow copying (if types or strides don't match) for comparison: 276 m.def("get_elem", &get_elem); 277 // Now this alternative that calls the tells pybind to fail rather than copy: 278 m.def("get_elem_nocopy", [](Eigen::Ref<const Eigen::MatrixXd> m) -> double { return get_elem(m); }, 279 py::arg().noconvert()); 280 // Also test a row-major-only no-copy const ref: 281 m.def("get_elem_rm_nocopy", [](Eigen::Ref<const Eigen::Matrix<long, -1, -1, Eigen::RowMajor>> &m) -> long { return m(2, 1); }, 282 py::arg().noconvert()); 283 |
284 // test_issue738 |
|
284 // Issue #738: 1xN or Nx1 2D matrices were neither accepted nor properly copied with an 285 // incompatible stride value on the length-1 dimension--but that should be allowed (without 286 // requiring a copy!) because the stride value can be safely ignored on a size-1 dimension. 287 m.def("iss738_f1", &adjust_matrix<const Eigen::Ref<const Eigen::MatrixXd> &>, py::arg().noconvert()); 288 m.def("iss738_f2", &adjust_matrix<const Eigen::Ref<const Eigen::Matrix<double, -1, -1, Eigen::RowMajor>> &>, py::arg().noconvert()); 289 | 285 // Issue #738: 1xN or Nx1 2D matrices were neither accepted nor properly copied with an 286 // incompatible stride value on the length-1 dimension--but that should be allowed (without 287 // requiring a copy!) because the stride value can be safely ignored on a size-1 dimension. 288 m.def("iss738_f1", &adjust_matrix<const Eigen::Ref<const Eigen::MatrixXd> &>, py::arg().noconvert()); 289 m.def("iss738_f2", &adjust_matrix<const Eigen::Ref<const Eigen::Matrix<double, -1, -1, Eigen::RowMajor>> &>, py::arg().noconvert()); 290 |
291 // test_named_arguments 292 // Make sure named arguments are working properly: 293 m.def("matrix_multiply", [](const py::EigenDRef<const Eigen::MatrixXd> A, const py::EigenDRef<const Eigen::MatrixXd> B) 294 -> Eigen::MatrixXd { 295 if (A.cols() != B.rows()) throw std::domain_error("Nonconformable matrices!"); 296 return A * B; 297 }, py::arg("A"), py::arg("B")); 298 299 // test_custom_operator_new |
|
290 py::class_<CustomOperatorNew>(m, "CustomOperatorNew") 291 .def(py::init<>()) 292 .def_readonly("a", &CustomOperatorNew::a) 293 .def_readonly("b", &CustomOperatorNew::b); | 300 py::class_<CustomOperatorNew>(m, "CustomOperatorNew") 301 .def(py::init<>()) 302 .def_readonly("a", &CustomOperatorNew::a) 303 .def_readonly("b", &CustomOperatorNew::b); |
294}); | 304 305 // test_eigen_ref_life_support 306 // In case of a failure (the caster's temp array does not live long enough), creating 307 // a new array (np.ones(10)) increases the chances that the temp array will be garbage 308 // collected and/or that its memory will be overridden with different values. 309 m.def("get_elem_direct", [](Eigen::Ref<const Eigen::VectorXd> v) { 310 py::module::import("numpy").attr("ones")(10); 311 return v(5); 312 }); 313 m.def("get_elem_indirect", [](std::vector<Eigen::Ref<const Eigen::VectorXd>> v) { 314 py::module::import("numpy").attr("ones")(10); 315 return v[0](5); 316 }); 317} |