numpy-allocator.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 INRIA
3  */
4 
5 #ifndef __eigenpy_numpy_allocator_hpp__
6 #define __eigenpy_numpy_allocator_hpp__
7 
8 #include "eigenpy/fwd.hpp"
9 #include "eigenpy/numpy-type.hpp"
11 
12 #include "eigenpy/register.hpp"
13 
14 namespace eigenpy
15 {
16 
17  template<typename MatType>
19  {
20  template<typename SimilarMatrixType>
21  static PyArrayObject * allocate(const Eigen::MatrixBase<SimilarMatrixType> & mat,
22  npy_intp nd, npy_intp * shape)
23  {
24  typedef typename SimilarMatrixType::Scalar Scalar;
25 
26  const int code = Register::getTypeCode<Scalar>();
27  PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_SimpleNew(static_cast<int>(nd), shape, code);
28 
29  // Copy data
31 
32  return pyArray;
33  }
34  };
35 
36  template<typename MatType>
37  struct NumpyAllocator<MatType &>
38  {
39  template<typename SimilarMatrixType>
40  static PyArrayObject * allocate(Eigen::PlainObjectBase<SimilarMatrixType> & mat,
41  npy_intp nd, npy_intp * shape)
42  {
43  typedef typename SimilarMatrixType::Scalar Scalar;
44  enum { NPY_ARRAY_MEMORY_CONTIGUOUS = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY : NPY_ARRAY_FARRAY };
45 
47  {
48  const int Scalar_type_code = Register::getTypeCode<Scalar>();
49  PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(getPyArrayType(),
50  static_cast<int>(nd),
51  shape,
52  Scalar_type_code,
53  mat.data(),
54  NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED);
55 
56  return pyArray;
57  }
58  else
59  {
60  return NumpyAllocator<MatType>::allocate(mat,nd,shape);
61  }
62  }
63  };
64 
65 #if EIGEN_VERSION_AT_LEAST(3,2,0)
66 
67  template<typename MatType, int Options, typename Stride>
68  struct NumpyAllocator<Eigen::Ref<MatType,Options,Stride> >
69  {
70  typedef Eigen::Ref<MatType,Options,Stride> RefType;
71 
72  static PyArrayObject * allocate(RefType & mat,
73  npy_intp nd, npy_intp * shape)
74  {
75  typedef typename RefType::Scalar Scalar;
76  enum { NPY_ARRAY_MEMORY_CONTIGUOUS = RefType::IsRowMajor ? NPY_ARRAY_CARRAY : NPY_ARRAY_FARRAY };
77 
79  {
80  const int Scalar_type_code = Register::getTypeCode<Scalar>();
81  PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(getPyArrayType(),
82  static_cast<int>(nd),
83  shape,
84  Scalar_type_code,
85  mat.data(),
86  NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED);
87 
88  return pyArray;
89  }
90  else
91  {
92  return NumpyAllocator<MatType>::allocate(mat,nd,shape);
93  }
94  }
95  };
96 
97 #endif
98 
99  template<typename MatType>
100  struct NumpyAllocator<const MatType &>
101  {
102  template<typename SimilarMatrixType>
103  static PyArrayObject * allocate(const Eigen::PlainObjectBase<SimilarMatrixType> & mat,
104  npy_intp nd, npy_intp * shape)
105  {
106  typedef typename SimilarMatrixType::Scalar Scalar;
107  enum { NPY_ARRAY_MEMORY_CONTIGUOUS_RO = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY_RO : NPY_ARRAY_FARRAY_RO };
108 
110  {
111  const int Scalar_type_code = Register::getTypeCode<Scalar>();
112  PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(getPyArrayType(),
113  static_cast<int>(nd),
114  shape,
115  Scalar_type_code,
116  const_cast<Scalar *>(mat.data()),
117  NPY_ARRAY_MEMORY_CONTIGUOUS_RO | NPY_ARRAY_ALIGNED);
118 
119  return pyArray;
120  }
121  else
122  {
123  return NumpyAllocator<MatType>::allocate(mat,nd,shape);
124  }
125  }
126  };
127 
128 #if EIGEN_VERSION_AT_LEAST(3,2,0)
129 
130  template<typename MatType, int Options, typename Stride>
131  struct NumpyAllocator<const Eigen::Ref<const MatType,Options,Stride> >
132  {
133  typedef const Eigen::Ref<const MatType,Options,Stride> RefType;
134 
135  template<typename SimilarMatrixType>
136  static PyArrayObject * allocate(RefType & mat,
137  npy_intp nd, npy_intp * shape)
138  {
139  typedef typename SimilarMatrixType::Scalar Scalar;
140  enum { NPY_ARRAY_MEMORY_CONTIGUOUS_RO = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY_RO : NPY_ARRAY_FARRAY_RO };
141 
143  {
144  const int Scalar_type_code = Register::getTypeCode<Scalar>();
145  PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(getPyArrayType(),
146  static_cast<int>(nd),
147  shape,
148  Scalar_type_code,
149  const_cast<Scalar *>(mat.data()),
150  NPY_ARRAY_MEMORY_CONTIGUOUS_RO | NPY_ARRAY_ALIGNED);
151 
152  return pyArray;
153  }
154  else
155  {
156  return NumpyAllocator<MatType>::allocate(mat,nd,shape);
157  }
158  }
159  };
160 
161 #endif
162 }
163 
164 #endif // ifndef __eigenpy_numpy_allocator_hpp__
static bool sharedMemory()
Definition: numpy-type.cpp:49
#define getPyArrayType()
Definition: numpy.hpp:64
static PyArrayObject * allocate(Eigen::PlainObjectBase< SimilarMatrixType > &mat, npy_intp nd, npy_intp *shape)
#define call_PyArray_New(py_type_ptr, nd, shape, np_type, data_ptr, options)
Definition: numpy.hpp:62
#define call_PyArray_SimpleNew
Definition: numpy.hpp:61
static void copy(const Eigen::MatrixBase< MatrixDerived > &mat_, PyArrayObject *pyArray)
Copy mat into the Python array using Eigen::Map.
static PyArrayObject * allocate(const Eigen::PlainObjectBase< SimilarMatrixType > &mat, npy_intp nd, npy_intp *shape)
static PyArrayObject * allocate(const Eigen::MatrixBase< SimilarMatrixType > &mat, npy_intp nd, npy_intp *shape)


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Apr 17 2021 02:37:59