return_by_ref.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 INRIA
3  */
4 
5 #include <iostream>
6 
7 #include "eigenpy/eigenpy.hpp"
8 
9 template <typename Matrix>
10 struct Base {
11  Base(const Eigen::DenseIndex rows, const Eigen::DenseIndex cols)
12  : mat(rows, cols) {}
13 
14  void show() { std::cout << mat << std::endl; }
15 
16  Matrix& ref() { return mat; }
17  const Matrix& const_ref() { return mat; }
18  Matrix copy() { return mat; }
19 
20  protected:
21  Matrix mat;
22 };
23 
24 template <typename MatrixType>
25 void expose_matrix_class(const std::string& name) {
26  using namespace Eigen;
27  namespace bp = boost::python;
28 
29  bp::class_<Base<MatrixType> >(name.c_str(),
30  bp::init<DenseIndex, DenseIndex>())
31  .def("show", &Base<MatrixType>::show)
32  .def("ref", &Base<MatrixType>::ref, bp::return_internal_reference<>())
33  .def("const_ref", &Base<MatrixType>::const_ref,
34  bp::return_internal_reference<>())
35  .def("copy", &Base<MatrixType>::copy);
36 }
37 
38 BOOST_PYTHON_MODULE(return_by_ref) {
39  using namespace Eigen;
41 
42  typedef Eigen::Matrix<double, Eigen::Dynamic, 1> VectorType;
43  typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
44  typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
45  RowMatrixType;
46 
47  expose_matrix_class<VectorType>("Vector");
48  expose_matrix_class<MatrixType>("Matrix");
49  expose_matrix_class<RowMatrixType>("RowMatrix");
50 }
boost::python
Definition: alignment.hpp:49
Eigen
Definition: complex.cpp:7
Base::Base
Base(const Eigen::DenseIndex rows, const Eigen::DenseIndex cols)
Definition: return_by_ref.cpp:11
eigenpy::enableEigenPy
void EIGENPY_DLLAPI enableEigenPy()
Definition: eigenpy.cpp:43
test_complex.rows
int rows
Definition: test_complex.py:4
Base::mat
Matrix mat
Definition: return_by_ref.cpp:21
BOOST_PYTHON_MODULE
BOOST_PYTHON_MODULE(return_by_ref)
Definition: return_by_ref.cpp:38
Base::copy
Matrix copy()
Definition: return_by_ref.cpp:18
expose_matrix_class
void expose_matrix_class(const std::string &name)
Definition: return_by_ref.cpp:25
setup.name
name
Definition: setup.in.py:179
eigenpy.hpp
Base::const_ref
const Matrix & const_ref()
Definition: return_by_ref.cpp:17
test_complex.cols
int cols
Definition: test_complex.py:5
Base
Definition: return_by_ref.cpp:10
Base::show
void show()
Definition: return_by_ref.cpp:14
Base::ref
Matrix & ref()
Definition: return_by_ref.cpp:16


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Nov 2 2024 02:14:45