tensor/eigen-from-python.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2023 INRIA
3 //
4 
5 #ifndef __eigenpy_tensor_eigen_from_python_hpp__
6 #define __eigenpy_tensor_eigen_from_python_hpp__
7 
8 #include "eigenpy/fwd.hpp"
10 #include "eigenpy/numpy-type.hpp"
12 
13 namespace eigenpy {
14 
15 template <typename TensorType>
16 struct expected_pytype_for_arg<TensorType, Eigen::TensorBase<TensorType> > {
17  static PyTypeObject const *get_pytype() {
18  PyTypeObject const *py_type = eigenpy::getPyArrayType();
19  return py_type;
20  }
21 };
22 
23 } // namespace eigenpy
24 
25 namespace boost {
26 namespace python {
27 namespace converter {
28 
29 template <typename Scalar, int Rank, int Options, typename IndexType>
30 struct expected_pytype_for_arg<Eigen::Tensor<Scalar, Rank, Options, IndexType> >
32  Eigen::Tensor<Scalar, Rank, Options, IndexType> > {};
33 
34 template <typename Scalar, int Rank, int Options, typename IndexType>
35 struct rvalue_from_python_data<
36  Eigen::Tensor<Scalar, Rank, Options, IndexType> const &>
38  Eigen::Tensor<Scalar, Rank, Options, IndexType> const &> {
39  typedef Eigen::Tensor<Scalar, Rank, Options, IndexType> T;
41 };
42 
43 template <typename Derived>
44 struct rvalue_from_python_data<Eigen::TensorBase<Derived> const &>
45  : ::eigenpy::rvalue_from_python_data<Derived const &> {
47 };
48 
49 } // namespace converter
50 } // namespace python
51 } // namespace boost
52 
53 namespace boost {
54 namespace python {
55 namespace detail {
56 template <typename TensorType>
57 struct referent_storage<Eigen::TensorRef<TensorType> &> {
58  typedef Eigen::TensorRef<TensorType> RefType;
59  typedef ::eigenpy::details::referent_storage_eigen_ref<RefType> StorageType;
60  typedef typename ::eigenpy::aligned_storage<
62 };
63 
64 template <typename TensorType>
65 struct referent_storage<const Eigen::TensorRef<const TensorType> &> {
66  typedef Eigen::TensorRef<const TensorType> RefType;
67  typedef ::eigenpy::details::referent_storage_eigen_ref<RefType> StorageType;
68  typedef typename ::eigenpy::aligned_storage<
70 };
71 } // namespace detail
72 } // namespace python
73 } // namespace boost
74 
75 namespace eigenpy {
76 
77 template <typename TensorType>
78 struct eigen_from_py_impl<TensorType, Eigen::TensorBase<TensorType> > {
79  typedef typename TensorType::Scalar Scalar;
80 
82  static void *convertible(PyObject *pyObj);
83 
85  static void construct(PyObject *pyObj,
86  bp::converter::rvalue_from_python_stage1_data *memory);
87 
88  static void registration();
89 };
90 
91 template <typename TensorType>
92 void *
94  PyObject *pyObj) {
95  if (!call_PyArray_Check(reinterpret_cast<PyObject *>(pyObj))) return 0;
96 
97  typedef typename Eigen::internal::traits<TensorType>::Index Index;
98  static const Index NumIndices = TensorType::NumIndices;
99 
100  PyArrayObject *pyArray = reinterpret_cast<PyArrayObject *>(pyObj);
101 
102  if (!np_type_is_convertible_into_scalar<Scalar>(
103  EIGENPY_GET_PY_ARRAY_TYPE(pyArray)))
104  return 0;
105 
106  if (!(PyArray_NDIM(pyArray) == NumIndices || NumIndices == Eigen::Dynamic))
107  return 0;
108 
109 #ifdef NPY_1_8_API_VERSION
110  if (!(PyArray_FLAGS(pyArray)))
111 #else
112  if (!(PyArray_FLAGS(pyArray) & NPY_ALIGNED))
113 #endif
114  {
115  return 0;
116  }
117 
118  return pyArray;
119 }
120 
121 template <typename TensorType>
123  PyObject *pyObj, bp::converter::rvalue_from_python_stage1_data *memory) {
124  eigen_from_py_construct<TensorType>(pyObj, memory);
125 }
126 
127 template <typename TensorType>
128 void eigen_from_py_impl<TensorType,
129  Eigen::TensorBase<TensorType> >::registration() {
130  bp::converter::registry::push_back(
131  reinterpret_cast<void *(*)(_object *)>(&eigen_from_py_impl::convertible),
132  &eigen_from_py_impl::construct, bp::type_id<TensorType>()
133 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
134  ,
136 #endif
137  );
138 }
139 
140 template <typename TensorType>
142  Eigen::TensorBase<TensorType> > {
143  static void registration() {
145 
146  // Add conversion to Eigen::TensorBase<TensorType>
147  typedef Eigen::TensorBase<TensorType> TensorBase;
149 
150  // Add conversion to Eigen::TensorRef<TensorType>
151  typedef Eigen::TensorRef<TensorType> RefType;
153 
154  // Add conversion to Eigen::TensorRef<const TensorType>
155  typedef const Eigen::TensorRef<const TensorType> ConstRefType;
157  }
158 };
159 
160 template <typename TensorType>
161 struct EigenFromPy<Eigen::TensorBase<TensorType> > : EigenFromPy<TensorType> {
163  typedef Eigen::TensorBase<TensorType> Base;
164 
165  static void registration() {
166  bp::converter::registry::push_back(
167  reinterpret_cast<void *(*)(_object *)>(&EigenFromPy::convertible),
168  &EigenFromPy::construct, bp::type_id<Base>()
169 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
170  ,
172 #endif
173  );
174  }
175 };
176 
177 template <typename TensorType>
178 struct EigenFromPy<Eigen::TensorRef<TensorType> > {
179  typedef Eigen::TensorRef<TensorType> RefType;
180  typedef typename TensorType::Scalar Scalar;
181 
183  static void *convertible(PyObject *pyObj) {
184  if (!call_PyArray_Check(pyObj)) return 0;
185  PyArrayObject *pyArray = reinterpret_cast<PyArrayObject *>(pyObj);
186  if (!PyArray_ISWRITEABLE(pyArray)) return 0;
188  }
189 
190  static void registration() {
191  bp::converter::registry::push_back(
192  reinterpret_cast<void *(*)(_object *)>(&EigenFromPy::convertible),
193  &eigen_from_py_construct<RefType>, bp::type_id<RefType>()
194 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
195  ,
197 #endif
198  );
199  }
200 };
201 
202 template <typename TensorType>
203 struct EigenFromPy<const Eigen::TensorRef<const TensorType> > {
204  typedef const Eigen::TensorRef<const TensorType> ConstRefType;
205  typedef typename TensorType::Scalar Scalar;
206 
208  static void *convertible(PyObject *pyObj) {
210  }
211 
212  static void registration() {
213  bp::converter::registry::push_back(
214  reinterpret_cast<void *(*)(_object *)>(&EigenFromPy::convertible),
215  &eigen_from_py_construct<ConstRefType>, bp::type_id<ConstRefType>()
216 #ifndef BOOST_PYTHON_NO_PY_SIGNATURES
217  ,
219 #endif
220  );
221  }
222 };
223 
224 } // namespace eigenpy
225 
226 #endif // __eigenpy_tensor_eigen_from_python_hpp__
#define EIGENPY_RVALUE_FROM_PYTHON_DATA_INIT(type)
::eigenpy::aligned_storage< referent_size< StorageType & >::value >::type type
bool call_PyArray_Check(PyObject *py_obj)
Definition: numpy.hpp:136
Definition: complex.cpp:7
static void * convertible(PyObject *pyObj)
Determine if pyObj can be converted into a MatType object.
static void * convertible(PyObject *pyObj)
Determine if pyObj can be converted into a MatType object.
::eigenpy::details::referent_storage_eigen_ref< RefType > StorageType
::eigenpy::aligned_storage< referent_size< StorageType & >::value >::type type
#define EIGENPY_GET_PY_ARRAY_TYPE(array)
Definition: numpy.hpp:26
static void construct(PyObject *pyObj, bp::converter::rvalue_from_python_stage1_data *memory)
Allocate memory and copy pyObj in the new storage.
static void * convertible(PyObject *pyObj)
Determine if pyObj can be converted into a MatType object.
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