test_eigen.cpp
Go to the documentation of this file.
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/eigen.h>
11 #include <pybind11/stl.h>
12 
13 #include "constructor_stats.h"
14 #include "pybind11_tests.h"
15 
16 #if defined(_MSC_VER)
17 # pragma warning(disable : 4996) // C4996: std::unary_negation is deprecated
18 #endif
19 
20 #include <Eigen/Cholesky>
21 
23 
24 // Sets/resets a testing reference matrix to have values of 10*r + c, where r and c are the
25 // (1-based) row/column number.
26 template <typename M>
27 void reset_ref(M &x) {
28  for (int i = 0; i < x.rows(); i++) {
29  for (int j = 0; j < x.cols(); j++) {
30  x(i, j) = 11 + 10 * i + j;
31  }
32  }
33 }
34 
35 // Returns a static, column-major matrix
36 Eigen::MatrixXd &get_cm() {
37  static Eigen::MatrixXd *x;
38  if (!x) {
39  x = new Eigen::MatrixXd(3, 3);
40  reset_ref(*x);
41  }
42  return *x;
43 }
44 // Likewise, but row-major
46  static MatrixXdR *x;
47  if (!x) {
48  x = new MatrixXdR(3, 3);
49  reset_ref(*x);
50  }
51  return *x;
52 }
53 // Resets the values of the static matrices returned by get_cm()/get_rm()
54 void reset_refs() {
55  reset_ref(get_cm());
56  reset_ref(get_rm());
57 }
58 
59 // Returns element 2,1 from a matrix (used to test copy/nocopy)
60 double get_elem(const Eigen::Ref<const Eigen::MatrixXd> &m) { return m(2, 1); };
61 
62 // Returns a matrix with 10*r + 100*c added to each matrix element (to help test that the matrix
63 // reference is referencing rows/columns correctly).
64 template <typename MatrixArgType>
65 Eigen::MatrixXd adjust_matrix(MatrixArgType m) {
66  Eigen::MatrixXd ret(m);
67  for (int c = 0; c < m.cols(); c++) {
68  for (int r = 0; r < m.rows(); r++) {
69  ret(r, c) += 10 * r + 100 * c; // NOLINT(clang-analyzer-core.uninitialized.Assign)
70  }
71  }
72  return ret;
73 }
74 
76  CustomOperatorNew() = default;
77 
78  Eigen::Matrix4d a = Eigen::Matrix4d::Zero();
79  Eigen::Matrix4d b = Eigen::Matrix4d::Identity();
80 
82 };
83 
84 TEST_SUBMODULE(eigen, m) {
86  using FixedMatrixC = Eigen::Matrix<float, 5, 6>;
89  using FourRowMatrixC = Eigen::Matrix<float, 4, Eigen::Dynamic>;
90  using FourColMatrixC = Eigen::Matrix<float, Eigen::Dynamic, 4>;
91  using FourRowMatrixR = Eigen::Matrix<float, 4, Eigen::Dynamic>;
92  using FourColMatrixR = Eigen::Matrix<float, Eigen::Dynamic, 4>;
93  using SparseMatrixR = Eigen::SparseMatrix<float, Eigen::RowMajor>;
94  using SparseMatrixC = Eigen::SparseMatrix<float>;
95 
96  // various tests
97  m.def("double_col", [](const Eigen::VectorXf &x) -> Eigen::VectorXf { return 2.0f * x; });
98  m.def("double_row",
99  [](const Eigen::RowVectorXf &x) -> Eigen::RowVectorXf { return 2.0f * x; });
100  m.def("double_complex",
101  [](const Eigen::VectorXcf &x) -> Eigen::VectorXcf { return 2.0f * x; });
102  m.def("double_threec", [](py::EigenDRef<Eigen::Vector3f> x) { x *= 2; });
103  m.def("double_threer", [](py::EigenDRef<Eigen::RowVector3f> x) { x *= 2; });
104  m.def("double_mat_cm", [](const Eigen::MatrixXf &x) -> Eigen::MatrixXf { return 2.0f * x; });
105  m.def("double_mat_rm", [](const DenseMatrixR &x) -> DenseMatrixR { return 2.0f * x; });
106 
107  // test_eigen_ref_to_python
108  // Different ways of passing via Eigen::Ref; the first and second are the Eigen-recommended
109  m.def("cholesky1",
110  [](const Eigen::Ref<MatrixXdR> &x) -> Eigen::MatrixXd { return x.llt().matrixL(); });
111  m.def("cholesky2", [](const Eigen::Ref<const MatrixXdR> &x) -> Eigen::MatrixXd {
112  return x.llt().matrixL();
113  });
114  m.def("cholesky3",
115  [](const Eigen::Ref<MatrixXdR> &x) -> Eigen::MatrixXd { return x.llt().matrixL(); });
116  m.def("cholesky4", [](const Eigen::Ref<const MatrixXdR> &x) -> Eigen::MatrixXd {
117  return x.llt().matrixL();
118  });
119 
120  // test_eigen_ref_mutators
121  // Mutators: these add some value to the given element using Eigen, but Eigen should be mapping
122  // into the numpy array data and so the result should show up there. There are three versions:
123  // one that works on a contiguous-row matrix (numpy's default), one for a contiguous-column
124  // matrix, and one for any matrix.
125  auto add_rm = [](Eigen::Ref<MatrixXdR> x, int r, int c, double v) { x(r, c) += v; };
126  auto add_cm = [](Eigen::Ref<Eigen::MatrixXd> x, int r, int c, double v) { x(r, c) += v; };
127 
128  // Mutators (Eigen maps into numpy variables):
129  m.def("add_rm", add_rm); // Only takes row-contiguous
130  m.def("add_cm", add_cm); // Only takes column-contiguous
131  // Overloaded versions that will accept either row or column contiguous:
132  m.def("add1", add_rm);
133  m.def("add1", add_cm);
134  m.def("add2", add_cm);
135  m.def("add2", add_rm);
136  // This one accepts a matrix of any stride:
137  m.def("add_any",
138  [](py::EigenDRef<Eigen::MatrixXd> x, int r, int c, double v) { x(r, c) += v; });
139 
140  // Return mutable references (numpy maps into eigen variables)
141  m.def("get_cm_ref", []() { return Eigen::Ref<Eigen::MatrixXd>(get_cm()); });
142  m.def("get_rm_ref", []() { return Eigen::Ref<MatrixXdR>(get_rm()); });
143  // The same references, but non-mutable (numpy maps into eigen variables, but is !writeable)
144  m.def("get_cm_const_ref", []() { return Eigen::Ref<const Eigen::MatrixXd>(get_cm()); });
145  m.def("get_rm_const_ref", []() { return Eigen::Ref<const MatrixXdR>(get_rm()); });
146 
147  m.def("reset_refs", reset_refs); // Restores get_{cm,rm}_ref to original values
148 
149  // Increments and returns ref to (same) matrix
150  m.def(
151  "incr_matrix",
152  [](Eigen::Ref<Eigen::MatrixXd> m, double v) {
153  m += Eigen::MatrixXd::Constant(m.rows(), m.cols(), v);
154  return m;
155  },
156  py::return_value_policy::reference);
157 
158  // Same, but accepts a matrix of any strides
159  m.def(
160  "incr_matrix_any",
161  [](py::EigenDRef<Eigen::MatrixXd> m, double v) {
162  m += Eigen::MatrixXd::Constant(m.rows(), m.cols(), v);
163  return m;
164  },
165  py::return_value_policy::reference);
166 
167  // Returns an eigen slice of even rows
168  m.def(
169  "even_rows",
170  [](py::EigenDRef<Eigen::MatrixXd> m) {
171  return py::EigenDMap<Eigen::MatrixXd>(
172  m.data(),
173  (m.rows() + 1) / 2,
174  m.cols(),
175  py::EigenDStride(m.outerStride(), 2 * m.innerStride()));
176  },
177  py::return_value_policy::reference);
178 
179  // Returns an eigen slice of even columns
180  m.def(
181  "even_cols",
182  [](py::EigenDRef<Eigen::MatrixXd> m) {
183  return py::EigenDMap<Eigen::MatrixXd>(
184  m.data(),
185  m.rows(),
186  (m.cols() + 1) / 2,
187  py::EigenDStride(2 * m.outerStride(), m.innerStride()));
188  },
189  py::return_value_policy::reference);
190 
191  // Returns diagonals: a vector-like object with an inner stride != 1
192  m.def("diagonal", [](const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.diagonal(); });
193  m.def("diagonal_1",
194  [](const Eigen::Ref<const Eigen::MatrixXd> &x) { return x.diagonal<1>(); });
195  m.def("diagonal_n",
196  [](const Eigen::Ref<const Eigen::MatrixXd> &x, int index) { return x.diagonal(index); });
197 
198  // Return a block of a matrix (gives non-standard strides)
199  m.def("block",
201  int start_row,
202  int start_col,
203  int block_rows,
204  int block_cols) { return x.block(start_row, start_col, block_rows, block_cols); });
205 
206  // test_eigen_return_references, test_eigen_keepalive
207  // return value referencing/copying tests:
208  class ReturnTester {
209  Eigen::MatrixXd mat = create();
210 
211  public:
212  ReturnTester() { print_created(this); }
213  ~ReturnTester() { print_destroyed(this); }
214  static Eigen::MatrixXd create() { return Eigen::MatrixXd::Ones(10, 10); }
215  // NOLINTNEXTLINE(readability-const-return-type)
216  static const Eigen::MatrixXd createConst() { return Eigen::MatrixXd::Ones(10, 10); }
217  Eigen::MatrixXd &get() { return mat; }
218  Eigen::MatrixXd *getPtr() { return &mat; }
219  const Eigen::MatrixXd &view() { return mat; }
220  const Eigen::MatrixXd *viewPtr() { return &mat; }
221  Eigen::Ref<Eigen::MatrixXd> ref() { return mat; }
222  Eigen::Ref<const Eigen::MatrixXd> refConst() { return mat; }
223  Eigen::Block<Eigen::MatrixXd> block(int r, int c, int nrow, int ncol) {
224  return mat.block(r, c, nrow, ncol);
225  }
226  Eigen::Block<const Eigen::MatrixXd> blockConst(int r, int c, int nrow, int ncol) const {
227  return mat.block(r, c, nrow, ncol);
228  }
229  py::EigenDMap<Eigen::Matrix2d> corners() {
230  return py::EigenDMap<Eigen::Matrix2d>(
231  mat.data(),
232  py::EigenDStride(mat.outerStride() * (mat.outerSize() - 1),
233  mat.innerStride() * (mat.innerSize() - 1)));
234  }
235  py::EigenDMap<const Eigen::Matrix2d> cornersConst() const {
236  return py::EigenDMap<const Eigen::Matrix2d>(
237  mat.data(),
238  py::EigenDStride(mat.outerStride() * (mat.outerSize() - 1),
239  mat.innerStride() * (mat.innerSize() - 1)));
240  }
241  };
242  using rvp = py::return_value_policy;
243  py::class_<ReturnTester>(m, "ReturnTester")
244  .def(py::init<>())
245  .def_static("create", &ReturnTester::create)
246  .def_static("create_const", &ReturnTester::createConst)
247  .def("get", &ReturnTester::get, rvp::reference_internal)
248  .def("get_ptr", &ReturnTester::getPtr, rvp::reference_internal)
249  .def("view", &ReturnTester::view, rvp::reference_internal)
250  .def("view_ptr", &ReturnTester::view, rvp::reference_internal)
251  .def("copy_get", &ReturnTester::get) // Default rvp: copy
252  .def("copy_view", &ReturnTester::view) // "
253  .def("ref", &ReturnTester::ref) // Default for Ref is to reference
254  .def("ref_const", &ReturnTester::refConst) // Likewise, but const
255  .def("ref_safe", &ReturnTester::ref, rvp::reference_internal)
256  .def("ref_const_safe", &ReturnTester::refConst, rvp::reference_internal)
257  .def("copy_ref", &ReturnTester::ref, rvp::copy)
258  .def("copy_ref_const", &ReturnTester::refConst, rvp::copy)
259  .def("block", &ReturnTester::block)
260  .def("block_safe", &ReturnTester::block, rvp::reference_internal)
261  .def("block_const", &ReturnTester::blockConst, rvp::reference_internal)
262  .def("copy_block", &ReturnTester::block, rvp::copy)
263  .def("corners", &ReturnTester::corners, rvp::reference_internal)
264  .def("corners_const", &ReturnTester::cornersConst, rvp::reference_internal);
265 
266  // test_special_matrix_objects
267  // Returns a DiagonalMatrix with diagonal (1,2,3,...)
268  m.def("incr_diag", [](int k) {
270  for (int i = 0; i < k; i++) {
271  m.diagonal()[i] = i + 1;
272  }
273  return m;
274  });
275 
276  // Returns a SelfAdjointView referencing the lower triangle of m
277  m.def("symmetric_lower",
278  [](const Eigen::MatrixXi &m) { return m.selfadjointView<Eigen::Lower>(); });
279  // Returns a SelfAdjointView referencing the lower triangle of m
280  m.def("symmetric_upper",
281  [](const Eigen::MatrixXi &m) { return m.selfadjointView<Eigen::Upper>(); });
282 
283  // Test matrix for various functions below.
284  Eigen::MatrixXf mat(5, 6);
285  mat << 0, 3, 0, 0, 0, 11, 22, 0, 0, 0, 17, 11, 7, 5, 0, 1, 0, 11, 0, 0, 0, 0, 0, 11, 0, 0, 14,
286  0, 8, 11;
287 
288  // test_fixed, and various other tests
289  m.def("fixed_r", [mat]() -> FixedMatrixR { return FixedMatrixR(mat); });
290  // Our Eigen does a hack which respects constness through the numpy writeable flag.
291  // Therefore, the const return actually affects this type despite being an rvalue.
292  // NOLINTNEXTLINE(readability-const-return-type)
293  m.def("fixed_r_const", [mat]() -> const FixedMatrixR { return FixedMatrixR(mat); });
294  m.def("fixed_c", [mat]() -> FixedMatrixC { return FixedMatrixC(mat); });
295  m.def("fixed_copy_r", [](const FixedMatrixR &m) -> FixedMatrixR { return m; });
296  m.def("fixed_copy_c", [](const FixedMatrixC &m) -> FixedMatrixC { return m; });
297  // test_mutator_descriptors
298  m.def("fixed_mutator_r", [](const Eigen::Ref<FixedMatrixR> &) {});
299  m.def("fixed_mutator_c", [](const Eigen::Ref<FixedMatrixC> &) {});
300  m.def("fixed_mutator_a", [](const py::EigenDRef<FixedMatrixC> &) {});
301  // test_dense
302  m.def("dense_r", [mat]() -> DenseMatrixR { return DenseMatrixR(mat); });
303  m.def("dense_c", [mat]() -> DenseMatrixC { return DenseMatrixC(mat); });
304  m.def("dense_copy_r", [](const DenseMatrixR &m) -> DenseMatrixR { return m; });
305  m.def("dense_copy_c", [](const DenseMatrixC &m) -> DenseMatrixC { return m; });
306  // test_sparse, test_sparse_signature
307  m.def("sparse_r", [mat]() -> SparseMatrixR {
308  // NOLINTNEXTLINE(clang-analyzer-core.uninitialized.UndefReturn)
310  });
311  m.def("sparse_c",
312  [mat]() -> SparseMatrixC { return Eigen::SparseView<Eigen::MatrixXf>(mat); });
313  m.def("sparse_copy_r", [](const SparseMatrixR &m) -> SparseMatrixR { return m; });
314  m.def("sparse_copy_c", [](const SparseMatrixC &m) -> SparseMatrixC { return m; });
315  // test_partially_fixed
316  m.def("partial_copy_four_rm_r", [](const FourRowMatrixR &m) -> FourRowMatrixR { return m; });
317  m.def("partial_copy_four_rm_c", [](const FourColMatrixR &m) -> FourColMatrixR { return m; });
318  m.def("partial_copy_four_cm_r", [](const FourRowMatrixC &m) -> FourRowMatrixC { return m; });
319  m.def("partial_copy_four_cm_c", [](const FourColMatrixC &m) -> FourColMatrixC { return m; });
320 
321  // test_cpp_casting
322  // Test that we can cast a numpy object to a Eigen::MatrixXd explicitly
323  m.def("cpp_copy", [](py::handle m) { return m.cast<Eigen::MatrixXd>()(1, 0); });
324  m.def("cpp_ref_c", [](py::handle m) { return m.cast<Eigen::Ref<Eigen::MatrixXd>>()(1, 0); });
325  m.def("cpp_ref_r", [](py::handle m) { return m.cast<Eigen::Ref<MatrixXdR>>()(1, 0); });
326  m.def("cpp_ref_any",
327  [](py::handle m) { return m.cast<py::EigenDRef<Eigen::MatrixXd>>()(1, 0); });
328 
329  // [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works.
330 
331  // test_nocopy_wrapper
332  // Test that we can prevent copying into an argument that would normally copy: First a version
333  // that would allow copying (if types or strides don't match) for comparison:
334  m.def("get_elem", &get_elem);
335  // Now this alternative that calls the tells pybind to fail rather than copy:
336  m.def(
337  "get_elem_nocopy",
338  [](const Eigen::Ref<const Eigen::MatrixXd> &m) -> double { return get_elem(m); },
339  py::arg{}.noconvert());
340  // Also test a row-major-only no-copy const ref:
341  m.def(
342  "get_elem_rm_nocopy",
344  return m(2, 1);
345  },
346  py::arg{}.noconvert());
347 
348  // test_issue738, test_zero_length
349  // Issue #738: 1×N or N×1 2D matrices were neither accepted nor properly copied with an
350  // incompatible stride value on the length-1 dimension--but that should be allowed (without
351  // requiring a copy!) because the stride value can be safely ignored on a size-1 dimension.
352  // Similarly, 0×N or N×0 matrices were not accepted--again, these should be allowed since
353  // they contain no data. This particularly affects numpy ≥ 1.23, which sets the strides to
354  // 0 if any dimension size is 0.
355  m.def("iss738_f1",
357  py::arg{}.noconvert());
358  m.def("iss738_f2",
360  py::arg{}.noconvert());
361 
362  // test_issue1105
363  // Issue #1105: when converting from a numpy two-dimensional (Nx1) or (1xN) value into a dense
364  // eigen Vector or RowVector, the argument would fail to load because the numpy copy would
365  // fail: numpy won't broadcast a Nx1 into a 1-dimensional vector.
366  m.def("iss1105_col", [](const Eigen::VectorXd &) { return true; });
367  m.def("iss1105_row", [](const Eigen::RowVectorXd &) { return true; });
368 
369  // test_named_arguments
370  // Make sure named arguments are working properly:
371  m.def(
372  "matrix_multiply",
373  [](const py::EigenDRef<const Eigen::MatrixXd> &A,
374  const py::EigenDRef<const Eigen::MatrixXd> &B) -> Eigen::MatrixXd {
375  if (A.cols() != B.rows()) {
376  throw std::domain_error("Nonconformable matrices!");
377  }
378  return A * B;
379  },
380  py::arg("A"),
381  py::arg("B"));
382 
383  // test_custom_operator_new
384  py::class_<CustomOperatorNew>(m, "CustomOperatorNew")
385  .def(py::init<>())
386  .def_readonly("a", &CustomOperatorNew::a)
387  .def_readonly("b", &CustomOperatorNew::b);
388 
389  // test_eigen_ref_life_support
390  // In case of a failure (the caster's temp array does not live long enough), creating
391  // a new array (np.ones(10)) increases the chances that the temp array will be garbage
392  // collected and/or that its memory will be overridden with different values.
393  m.def("get_elem_direct", [](const Eigen::Ref<const Eigen::VectorXd> &v) {
394  py::module_::import("numpy").attr("ones")(10);
395  return v(5);
396  });
397  m.def("get_elem_indirect", [](std::vector<Eigen::Ref<const Eigen::VectorXd>> v) {
398  py::module_::import("numpy").attr("ones")(10);
399  return v[0](5);
400  });
401 }
Matrix3f m
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51
MatrixXdR & get_rm()
Definition: test_eigen.cpp:45
m m block(1, 0, 2, 2)<< 4
Eigen::Stride< Eigen::Dynamic, Eigen::Dynamic > EigenDStride
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
EIGEN_DEVICE_FUNC const DiagonalVectorType & diagonal() const
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixXdR
Definition: test_eigen.cpp:22
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
void print_destroyed(T *inst, Values &&...values)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set view
Represents a diagonal matrix with its storage.
double get_elem(const Eigen::Ref< const Eigen::MatrixXd > &m)
Definition: test_eigen.cpp:60
Eigen::MatrixXd & get_cm()
Definition: test_eigen.cpp:36
CustomOperatorNew()=default
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const ArgReturnType arg() const
Array< int, Dynamic, 1 > v
Expression of a dense or sparse matrix with zero or too small values removed.
DenseIndex ret
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:281
TEST_SUBMODULE(eigen, m)
Definition: test_eigen.cpp:84
ADT create(const Signature &signature)
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
void print_created(T *inst, Values &&...values)
void corners(const MatrixType &m)
Definition: corners.cpp:16
void reset_ref(M &x)
Definition: test_eigen.cpp:27
Eigen::MatrixXd adjust_matrix(MatrixArgType m)
Definition: test_eigen.cpp:65
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:29
Eigen::Matrix4d a
Definition: test_eigen.cpp:78
Eigen::Matrix4d b
Definition: test_eigen.cpp:79
Container::iterator get(Container &c, Position position)
The matrix class, also used for vectors and row-vectors.
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
void reset_refs()
Definition: test_eigen.cpp:54
std::ptrdiff_t j


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:45