eigen-to-python.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2014-2023 CNRS INRIA
3 //
4 
5 #ifndef __eigenpy_eigen_to_python_hpp__
6 #define __eigenpy_eigen_to_python_hpp__
7 
8 #include <boost/type_traits.hpp>
9 
10 #include "eigenpy/fwd.hpp"
11 
14 #include "eigenpy/numpy-type.hpp"
15 #include "eigenpy/registration.hpp"
16 
17 namespace boost {
18 namespace python {
19 
20 template <typename MatrixRef, class MakeHolder>
22  template <class U>
23  inline PyObject* operator()(U const& mat) const {
24  return eigenpy::EigenToPy<MatrixRef>::convert(const_cast<U&>(mat));
25  }
26 
27 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
28  inline PyTypeObject const* get_pytype() const {
29  return converter::registered_pytype<MatrixRef>::get_pytype();
30  }
31 #endif
32 };
33 
34 template <typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime,
35  int Options, int MaxRowsAtCompileTime, int MaxColsAtCompileTime,
36  class MakeHolder>
37 struct to_python_indirect<
38  Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options,
39  MaxRowsAtCompileTime, MaxColsAtCompileTime>&,
40  MakeHolder>
42  Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options,
43  MaxRowsAtCompileTime, MaxColsAtCompileTime>&,
44  MakeHolder> {};
45 
46 template <typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime,
47  int Options, int MaxRowsAtCompileTime, int MaxColsAtCompileTime,
48  class MakeHolder>
49 struct to_python_indirect<
50  const Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options,
51  MaxRowsAtCompileTime, MaxColsAtCompileTime>&,
52  MakeHolder>
54  const Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime,
55  Options, MaxRowsAtCompileTime,
56  MaxColsAtCompileTime>&,
57  MakeHolder> {};
58 
59 } // namespace python
60 } // namespace boost
61 
62 namespace eigenpy {
63 
65 
66 template <typename EigenType,
67  typename BaseType = typename get_eigen_base_type<EigenType>::type>
69 
70 template <typename MatType>
72 
73 template <typename MatType>
74 struct eigen_to_py_impl<MatType, Eigen::MatrixBase<MatType> >
75  : eigen_to_py_impl_matrix<MatType> {};
76 
77 template <typename MatType>
78 struct eigen_to_py_impl<MatType&, Eigen::MatrixBase<MatType> >
80 
81 template <typename MatType>
82 struct eigen_to_py_impl<const MatType, const Eigen::MatrixBase<MatType> >
83  : eigen_to_py_impl_matrix<const MatType> {};
84 
85 template <typename MatType>
86 struct eigen_to_py_impl<const MatType&, const Eigen::MatrixBase<MatType> >
88 
89 template <typename MatType>
91  static PyObject* convert(
92  typename boost::add_reference<
93  typename boost::add_const<MatType>::type>::type mat) {
94  typedef typename boost::remove_const<
95  typename boost::remove_reference<MatType>::type>::type MatrixDerived;
96 
97  assert((mat.rows() < INT_MAX) && (mat.cols() < INT_MAX) &&
98  "Matrix range larger than int ... should never happen.");
99  const npy_intp R = (npy_intp)mat.rows(), C = (npy_intp)mat.cols();
100 
101  PyArrayObject* pyArray;
102  // Allocate Python memory
103  if ((((!(C == 1) != !(R == 1)) && !MatrixDerived::IsVectorAtCompileTime) ||
104  MatrixDerived::IsVectorAtCompileTime)) // Handle array with a single
105  // dimension
106  {
107  npy_intp shape[1] = {C == 1 ? R : C};
109  const_cast<MatrixDerived&>(mat.derived()), 1, shape);
110  } else {
111  npy_intp shape[2] = {R, C};
113  const_cast<MatrixDerived&>(mat.derived()), 2, shape);
114  }
115 
116  // Create an instance (either np.array or np.matrix)
117  return NumpyType::make(pyArray).ptr();
118  }
119 };
120 
121 #ifdef EIGENPY_WITH_TENSOR_SUPPORT
122 template <typename TensorType>
123 struct eigen_to_py_impl_tensor;
124 
125 template <typename TensorType>
126 struct eigen_to_py_impl<TensorType, Eigen::TensorBase<TensorType> >
127  : eigen_to_py_impl_tensor<TensorType> {};
128 
129 template <typename TensorType>
130 struct eigen_to_py_impl<const TensorType, const Eigen::TensorBase<TensorType> >
131  : eigen_to_py_impl_tensor<const TensorType> {};
132 
133 template <typename TensorType>
134 struct eigen_to_py_impl_tensor {
135  static PyObject* convert(
136  typename boost::add_reference<
137  typename boost::add_const<TensorType>::type>::type tensor) {
138  // typedef typename boost::remove_const<
139  // typename boost::remove_reference<Tensor>::type>::type
140  // TensorDerived;
141 
142  static const int NumIndices = TensorType::NumIndices;
143  npy_intp shape[NumIndices];
144  for (int k = 0; k < NumIndices; ++k) shape[k] = tensor.dimension(k);
145 
146  PyArrayObject* pyArray = NumpyAllocator<TensorType>::allocate(
147  const_cast<TensorType&>(tensor), NumIndices, shape);
148 
149  // Create an instance (either np.array or np.matrix)
150  return NumpyType::make(pyArray).ptr();
151  }
152 };
153 #endif
154 
155 EIGENPY_DOCUMENTATION_END_IGNORE
156 
157 #ifdef EIGENPY_MSVC_COMPILER
158 template <typename EigenType>
159 struct EigenToPy<EigenType,
160  typename boost::remove_reference<EigenType>::type::Scalar>
161 #else
162 template <typename EigenType, typename _Scalar>
163 struct EigenToPy
164 #endif
165  : eigen_to_py_impl<EigenType> {
166  static PyTypeObject const* get_pytype() { return getPyArrayType(); }
167 };
168 
169 template <typename MatType>
171  static void registration() {
172  bp::to_python_converter<MatType, EigenToPy<MatType>, true>();
173  }
174 };
175 } // namespace eigenpy
176 
177 #endif // __eigenpy_eigen_to_python_hpp__
Definition: complex.cpp:7
PyTypeObject const * get_pytype() const
static PyObject * convert(typename boost::add_reference< typename boost::add_const< MatType >::type >::type mat)
static PyTypeObject const * get_pytype()
PyObject * operator()(U const &mat) const
#define EIGENPY_DOCUMENTATION_START_IGNORE
Definition: fwd.hpp:64
PyTypeObject * getPyArrayType()
Definition: numpy.hpp:163
Definition: python.py:1


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Fri Jun 2 2023 02:10:26