eigen-to-python.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2014-2024 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 
15 #include "eigenpy/numpy-type.hpp"
16 #include "eigenpy/scipy-type.hpp"
17 #include "eigenpy/registration.hpp"
18 
19 namespace eigenpy {
20 
22 
23 template <typename EigenType,
24  typename BaseType = typename get_eigen_base_type<EigenType>::type>
26 
27 template <typename MatType>
29 
30 template <typename MatType>
31 struct eigen_to_py_impl<MatType, Eigen::MatrixBase<MatType> >
32  : eigen_to_py_impl_matrix<MatType> {};
33 
34 template <typename MatType>
35 struct eigen_to_py_impl<MatType&, Eigen::MatrixBase<MatType> >
37 
38 template <typename MatType>
39 struct eigen_to_py_impl<const MatType, const Eigen::MatrixBase<MatType> >
40  : eigen_to_py_impl_matrix<const MatType> {};
41 
42 template <typename MatType>
43 struct eigen_to_py_impl<const MatType&, const Eigen::MatrixBase<MatType> >
45 
46 template <typename MatType>
48  static PyObject* convert(
49  typename boost::add_reference<
50  typename boost::add_const<MatType>::type>::type mat) {
51  typedef typename boost::remove_const<
52  typename boost::remove_reference<MatType>::type>::type MatrixDerived;
53 
54  assert((mat.rows() < INT_MAX) && (mat.cols() < INT_MAX) &&
55  "Matrix range larger than int ... should never happen.");
56  const npy_intp R = (npy_intp)mat.rows(), C = (npy_intp)mat.cols();
57 
58  PyArrayObject* pyArray;
59  // Allocate Python memory
60  if ((((!(C == 1) != !(R == 1)) && !MatrixDerived::IsVectorAtCompileTime) ||
61  MatrixDerived::IsVectorAtCompileTime)) // Handle array with a single
62  // dimension
63  {
64  npy_intp shape[1] = {C == 1 ? R : C};
66  const_cast<MatrixDerived&>(mat.derived()), 1, shape);
67  } else {
68  npy_intp shape[2] = {R, C};
70  const_cast<MatrixDerived&>(mat.derived()), 2, shape);
71  }
72 
73  // Create an instance (either np.array or np.matrix)
74  return NumpyType::make(pyArray).ptr();
75  }
76 
77  static PyTypeObject const* get_pytype() { return getPyArrayType(); }
78 };
79 
80 template <typename MatType>
82 
83 template <typename MatType>
84 struct eigen_to_py_impl<MatType, Eigen::SparseMatrixBase<MatType> >
85  : eigen_to_py_impl_sparse_matrix<MatType> {};
86 
87 template <typename MatType>
88 struct eigen_to_py_impl<MatType&, Eigen::SparseMatrixBase<MatType> >
90 
91 template <typename MatType>
92 struct eigen_to_py_impl<const MatType, const Eigen::SparseMatrixBase<MatType> >
93  : eigen_to_py_impl_sparse_matrix<const MatType> {};
94 
95 template <typename MatType>
96 struct eigen_to_py_impl<const MatType&, const Eigen::SparseMatrixBase<MatType> >
98 
99 template <typename MatType>
101  enum { IsRowMajor = MatType::IsRowMajor };
102 
103  static PyObject* convert(
104  typename boost::add_reference<
105  typename boost::add_const<MatType>::type>::type mat) {
106  typedef typename boost::remove_const<
107  typename boost::remove_reference<MatType>::type>::type MatrixDerived;
108 
109  // Allocate and perform the copy
110  PyObject* pyArray =
111  ScipyAllocator<MatType>::allocate(const_cast<MatrixDerived&>(mat));
112 
113  return pyArray;
114  }
115 
116  static PyTypeObject const* get_pytype() {
119  }
120 };
121 
122 #ifdef EIGENPY_WITH_TENSOR_SUPPORT
123 template <typename TensorType>
124 struct eigen_to_py_impl_tensor;
125 
126 template <typename TensorType>
127 struct eigen_to_py_impl<TensorType, Eigen::TensorBase<TensorType> >
128  : eigen_to_py_impl_tensor<TensorType> {};
129 
130 template <typename TensorType>
131 struct eigen_to_py_impl<const TensorType, const Eigen::TensorBase<TensorType> >
132  : eigen_to_py_impl_tensor<const TensorType> {};
133 
134 template <typename TensorType>
135 struct eigen_to_py_impl_tensor {
136  static PyObject* convert(
137  typename boost::add_reference<
138  typename boost::add_const<TensorType>::type>::type tensor) {
139  // typedef typename boost::remove_const<
140  // typename boost::remove_reference<Tensor>::type>::type
141  // TensorDerived;
142 
143  static const int NumIndices = TensorType::NumIndices;
144  npy_intp shape[NumIndices];
145  for (int k = 0; k < NumIndices; ++k) shape[k] = tensor.dimension(k);
146 
147  PyArrayObject* pyArray = NumpyAllocator<TensorType>::allocate(
148  const_cast<TensorType&>(tensor), NumIndices, shape);
149 
150  // Create an instance (either np.array or np.matrix)
151  return NumpyType::make(pyArray).ptr();
152  }
153 
154  static PyTypeObject const* get_pytype() { return getPyArrayType(); }
155 };
156 #endif
157 
158 EIGENPY_DOCUMENTATION_END_IGNORE
159 
160 template <typename EigenType,
161  typename Scalar =
162  typename boost::remove_reference<EigenType>::type::Scalar>
163 struct EigenToPy : eigen_to_py_impl<EigenType> {};
164 
165 template <typename MatType>
167  static void registration() {
168  bp::to_python_converter<MatType, EigenToPy<MatType>, true>();
169  }
170 };
171 
172 } // namespace eigenpy
173 
174 namespace boost {
175 namespace python {
176 
177 template <typename MatrixRef, class MakeHolder>
179  template <class U>
180  inline PyObject* operator()(U const& mat) const {
181  return eigenpy::EigenToPy<MatrixRef>::convert(const_cast<U&>(mat));
182  }
183 
184 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
185  inline PyTypeObject const* get_pytype() const {
186  return converter::registered_pytype<MatrixRef>::get_pytype();
187  }
188 #endif
189 };
190 
191 template <typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime,
192  int Options, int MaxRowsAtCompileTime, int MaxColsAtCompileTime,
193  class MakeHolder>
194 struct to_python_indirect<
195  Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options,
196  MaxRowsAtCompileTime, MaxColsAtCompileTime>&,
197  MakeHolder>
199  Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options,
200  MaxRowsAtCompileTime, MaxColsAtCompileTime>&,
201  MakeHolder> {};
202 
203 template <typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime,
204  int Options, int MaxRowsAtCompileTime, int MaxColsAtCompileTime,
205  class MakeHolder>
206 struct to_python_indirect<
207  const Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options,
208  MaxRowsAtCompileTime, MaxColsAtCompileTime>&,
209  MakeHolder>
211  const Eigen::Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime,
212  Options, MaxRowsAtCompileTime,
213  MaxColsAtCompileTime>&,
214  MakeHolder> {};
215 
216 } // namespace python
217 } // namespace boost
218 
219 #endif // __eigenpy_eigen_to_python_hpp__
boost::python::to_python_indirect_eigen
Definition: eigen-to-python.hpp:178
test_SimplicialLDLT.U
U
Definition: test_SimplicialLDLT.py:19
eigenpy::getPyArrayType
PyTypeObject * getPyArrayType()
Definition: numpy.hpp:264
Eigen
Definition: complex.cpp:7
registration.hpp
EIGENPY_DOCUMENTATION_START_IGNORE
#define EIGENPY_DOCUMENTATION_START_IGNORE
Definition: fwd.hpp:63
eigenpy::eigen_to_py_impl
Definition: eigen-to-python.hpp:25
fwd.hpp
eigenpy::EigenToPy
Definition: eigen-to-python.hpp:163
boost
Definition: alignment.hpp:48
eigenpy::eigen_to_py_impl_sparse_matrix::get_pytype
static PyTypeObject const * get_pytype()
Definition: eigen-to-python.hpp:116
test_geometry.R
R
Definition: test_geometry.py:81
eigenpy::ScipyType::getScipyCSCMatrixType
static const PyTypeObject * getScipyCSCMatrixType()
Definition: scipy-type.cpp:26
eigenpy::get_eigen_base_type::type
boost::mpl::if_< boost::is_const< typename boost::remove_reference< EigenType >::type >, const _type, _type >::type type
Definition: fwd.hpp:165
eigenpy::eigen_to_py_impl_sparse_matrix::IsRowMajor
@ IsRowMajor
Definition: eigen-to-python.hpp:101
eigenpy
Definition: alignment.hpp:14
scipy-allocator.hpp
eigenpy::NumpyType::make
static bp::object make(PyArrayObject *pyArray, bool copy=false)
Definition: numpy-type.cpp:16
eigen-allocator.hpp
boost::python::to_python_indirect_eigen::operator()
PyObject * operator()(U const &mat) const
Definition: eigen-to-python.hpp:180
eigenpy::EigenToPyConverter::registration
static void registration()
Definition: eigen-to-python.hpp:167
eigenpy::NumpyAllocator
Definition: numpy-allocator.hpp:43
python
Definition: python.py:1
test_eigen_ref.mat
mat
Definition: test_eigen_ref.py:137
eigenpy::eigen_to_py_impl_matrix::convert
static PyObject * convert(typename boost::add_reference< typename boost::add_const< MatType >::type >::type mat)
Definition: eigen-to-python.hpp:48
eigenpy::eigen_to_py_impl_sparse_matrix::convert
static PyObject * convert(typename boost::add_reference< typename boost::add_const< MatType >::type >::type mat)
Definition: eigen-to-python.hpp:103
boost::python::to_python_indirect_eigen::get_pytype
PyTypeObject const * get_pytype() const
Definition: eigen-to-python.hpp:185
eigenpy::ScipyAllocator
Definition: scipy-allocator.hpp:45
eigenpy::ScipyType::getScipyCSRMatrixType
static const PyTypeObject * getScipyCSRMatrixType()
Definition: scipy-type.cpp:22
eigenpy::eigen_to_py_impl_sparse_matrix
Definition: eigen-to-python.hpp:81
numpy-allocator.hpp
scipy-type.hpp
numpy-type.hpp
eigenpy::EigenToPyConverter
Definition: eigen-to-python.hpp:166
eigenpy::eigen_to_py_impl_matrix
Definition: eigen-to-python.hpp:28
eigenpy::eigen_to_py_impl_matrix::get_pytype
static PyTypeObject const * get_pytype()
Definition: eigen-to-python.hpp:77


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