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__
eigenpy::getPyArrayType
PyTypeObject * getPyArrayType()
Definition: numpy.hpp:264
Eigen
Definition: complex.cpp:7
eigenpy::eigen_from_py_impl
Definition: eigen-from-python.hpp:266
boost::python::detail::referent_storage< Eigen::TensorRef< TensorType > & >::RefType
Eigen::TensorRef< TensorType > RefType
Definition: tensor/eigen-from-python.hpp:58
eigenpy::eigen_from_py_impl::registration
static void registration()
boost::python::converter::rvalue_from_python_data< Eigen::Tensor< Scalar, Rank, Options, IndexType > const & >::T
Eigen::Tensor< Scalar, Rank, Options, IndexType > T
Definition: tensor/eigen-from-python.hpp:39
EIGENPY_RVALUE_FROM_PYTHON_DATA_INIT
#define EIGENPY_RVALUE_FROM_PYTHON_DATA_INIT(type)
Definition: eigen-from-python.hpp:133
eigenpy::EigenFromPy
Definition: eigen-from-python.hpp:296
fwd.hpp
scalar-conversion.hpp
eigenpy::eigen_from_py_impl::convertible
static void * convertible(PyObject *pyObj)
Determine if pyObj can be converted into a MatType object.
boost
Definition: alignment.hpp:48
eigenpy::EigenFromPy< Eigen::TensorRef< TensorType > >::RefType
Eigen::TensorRef< TensorType > RefType
Definition: tensor/eigen-from-python.hpp:179
eigenpy::EigenFromPy< Eigen::TensorRef< TensorType > >::convertible
static void * convertible(PyObject *pyObj)
Determine if pyObj can be converted into a MatType object.
Definition: tensor/eigen-from-python.hpp:183
eigenpy::EigenFromPy< const Eigen::TensorRef< const TensorType > >::ConstRefType
const typedef Eigen::TensorRef< const TensorType > ConstRefType
Definition: tensor/eigen-from-python.hpp:204
eigenpy::EigenFromPy< const Eigen::TensorRef< const TensorType > >::registration
static void registration()
Definition: tensor/eigen-from-python.hpp:212
eigenpy::call_PyArray_Check
bool call_PyArray_Check(PyObject *py_obj)
Definition: numpy.hpp:237
boost::python::detail::referent_storage< Eigen::TensorRef< TensorType > & >::StorageType
::eigenpy::details::referent_storage_eigen_ref< RefType > StorageType
Definition: tensor/eigen-from-python.hpp:59
eigenpy
Definition: alignment.hpp:14
eigenpy::expected_pytype_for_arg
Definition: eigen-from-python.hpp:17
eigen-allocator.hpp
eigenpy::eigen_from_py_converter_impl
Definition: eigen-from-python.hpp:420
python
Definition: python.py:1
test_matrix.value
float value
Definition: test_matrix.py:161
eigenpy::EigenFromPy< Eigen::TensorBase< TensorType > >::registration
static void registration()
Definition: tensor/eigen-from-python.hpp:165
eigenpy::eigen_from_py_impl::construct
static void construct(PyObject *pyObj, bp::converter::rvalue_from_python_stage1_data *memory)
Allocate memory and copy pyObj in the new storage.
eigenpy::eigen_from_py_impl< TensorType, Eigen::TensorBase< TensorType > >::Scalar
TensorType::Scalar Scalar
Definition: tensor/eigen-from-python.hpp:79
eigenpy::EigenFromPy< Eigen::TensorBase< TensorType > >::Base
Eigen::TensorBase< TensorType > Base
Definition: tensor/eigen-from-python.hpp:163
eigenpy::EigenFromPy< Eigen::TensorRef< TensorType > >::Scalar
TensorType::Scalar Scalar
Definition: tensor/eigen-from-python.hpp:180
eigenpy::rvalue_from_python_data
Definition: alignment.hpp:130
EIGENPY_GET_PY_ARRAY_TYPE
#define EIGENPY_GET_PY_ARRAY_TYPE(array)
Definition: numpy.hpp:55
boost::python::detail::referent_storage< const Eigen::TensorRef< const TensorType > & >::RefType
Eigen::TensorRef< const TensorType > RefType
Definition: tensor/eigen-from-python.hpp:66
eigenpy::aligned_storage::type
Definition: alignment.hpp:18
eigenpy::eigen_from_py_converter_impl< TensorType, Eigen::TensorBase< TensorType > >::registration
static void registration()
Definition: tensor/eigen-from-python.hpp:143
eigenpy::EigenFromPy< const Eigen::TensorRef< const TensorType > >::convertible
static void * convertible(PyObject *pyObj)
Determine if pyObj can be converted into a MatType object.
Definition: tensor/eigen-from-python.hpp:208
numpy-type.hpp
boost::python::detail::referent_storage< const Eigen::TensorRef< const TensorType > & >::StorageType
::eigenpy::details::referent_storage_eigen_ref< RefType > StorageType
Definition: tensor/eigen-from-python.hpp:67
boost::python::detail::referent_storage< Eigen::TensorRef< TensorType > & >::type
::eigenpy::aligned_storage< referent_size< StorageType & >::value >::type type
Definition: tensor/eigen-from-python.hpp:61
eigenpy::expected_pytype_for_arg< TensorType, Eigen::TensorBase< TensorType > >::get_pytype
static const PyTypeObject * get_pytype()
Definition: tensor/eigen-from-python.hpp:17
eigenpy::EigenFromPy< Eigen::TensorRef< TensorType > >::registration
static void registration()
Definition: tensor/eigen-from-python.hpp:190
eigenpy::EigenFromPy< const Eigen::TensorRef< const TensorType > >::Scalar
TensorType::Scalar Scalar
Definition: tensor/eigen-from-python.hpp:205
boost::python::detail::referent_storage< const Eigen::TensorRef< const TensorType > & >::type
::eigenpy::aligned_storage< referent_size< StorageType & >::value >::type type
Definition: tensor/eigen-from-python.hpp:69
eigenpy::EigenFromPy< Eigen::TensorBase< TensorType > >::EigenFromPyDerived
EigenFromPy< TensorType > EigenFromPyDerived
Definition: tensor/eigen-from-python.hpp:162


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