numpy.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020-2022 INRIA
3  */
4 
5 #ifndef __eigenpy_numpy_hpp__
6 #define __eigenpy_numpy_hpp__
7 
8 #include "eigenpy/fwd.hpp"
9 
10 #ifndef PY_ARRAY_UNIQUE_SYMBOL
11 #define PY_ARRAY_UNIQUE_SYMBOL EIGENPY_ARRAY_API
12 #endif
13 
14 #include <numpy/numpyconfig.h>
15 #ifdef NPY_1_8_API_VERSION
16 #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
17 #endif
18 
19 /* Allow compiling against NumPy 1.x and 2.x
20  see:
21  https://github.com/numpy/numpy/blob/afea8fd66f6bdbde855f5aff0b4e73eb0213c646/doc/source/reference/c-api/array.rst#L1224
22 */
23 #if NPY_ABI_VERSION < 0x02000000
24 #define PyArray_DescrProto PyArray_Descr
25 #endif
26 
27 #include <numpy/ndarrayobject.h>
28 #include <numpy/ufuncobject.h>
29 
30 #if NPY_ABI_VERSION < 0x02000000
31 static inline PyArray_ArrFuncs* PyDataType_GetArrFuncs(PyArray_Descr* descr) {
32  return descr->f;
33 }
34 #endif
35 
36 /* PEP 674 disallow using macros as l-values
37  see : https://peps.python.org/pep-0674/
38 */
39 #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
40 static inline void _Py_SET_TYPE(PyObject* o, PyTypeObject* type) {
41  Py_TYPE(o) = type;
42 }
43 #define Py_SET_TYPE(o, type) _Py_SET_TYPE((PyObject*)(o), type)
44 #endif
45 
46 #if defined _WIN32 || defined __CYGWIN__
47 #define EIGENPY_GET_PY_ARRAY_TYPE(array) \
48  call_PyArray_MinScalarType(array)->type_num
49 #else
50 #define EIGENPY_GET_PY_ARRAY_TYPE(array) PyArray_MinScalarType(array)->type_num
51 #endif
52 
53 namespace eigenpy {
54 void EIGENPY_DLLAPI import_numpy();
55 int EIGENPY_DLLAPI PyArray_TypeNum(PyTypeObject* type);
56 
57 // By default, the Scalar is considered as a Python object
58 template <typename Scalar>
60  enum { type_code = NPY_USERDEF };
61 };
62 
63 template <>
64 struct NumpyEquivalentType<bool> {
65  enum { type_code = NPY_BOOL };
66 };
67 
68 template <>
69 struct NumpyEquivalentType<char> {
70  enum { type_code = NPY_INT8 };
71 };
72 template <>
73 struct NumpyEquivalentType<unsigned char> {
74  enum { type_code = NPY_UINT8 };
75 };
76 template <>
78  enum { type_code = NPY_INT8 };
79 };
80 
81 template <>
83  enum { type_code = NPY_INT16 };
84 };
85 template <>
87  enum { type_code = NPY_UINT16 };
88 };
89 
90 template <>
92  enum { type_code = NPY_INT32 };
93 };
94 template <>
96  enum { type_code = NPY_UINT32 };
97 };
98 
99 // On Windows, long is a 32 bytes type but it's a different type than int
100 // See https://github.com/stack-of-tasks/eigenpy/pull/455
101 #if defined _WIN32 || defined __CYGWIN__
102 
103 template <>
104 struct NumpyEquivalentType<long> {
105  enum { type_code = NPY_INT32 };
106 };
107 template <>
108 struct NumpyEquivalentType<unsigned long> {
109  enum { type_code = NPY_UINT32 };
110 };
111 
112 #endif // WIN32
113 
114 template <>
116  enum { type_code = NPY_INT64 };
117 };
118 template <>
120  enum { type_code = NPY_UINT64 };
121 };
122 
123 // On Mac, long is a 64 bytes type but it's a different type than int64_t
124 // See https://github.com/stack-of-tasks/eigenpy/pull/455
125 #if defined __APPLE__
126 
127 template <>
128 struct NumpyEquivalentType<long> {
129  enum { type_code = NPY_INT64 };
130 };
131 template <>
132 struct NumpyEquivalentType<unsigned long> {
133  enum { type_code = NPY_UINT64 };
134 };
135 
136 #endif // MAC
137 
138 // On Linux, long long is a 64 bytes type but it's a different type than int64_t
139 // See https://github.com/stack-of-tasks/eigenpy/pull/455
140 #if defined __linux__
141 
142 template <>
143 struct NumpyEquivalentType<long long> {
144  enum { type_code = NPY_LONGLONG };
145 };
146 template <>
147 struct NumpyEquivalentType<unsigned long long> {
148  enum { type_code = NPY_ULONGLONG };
149 };
150 
151 #endif // Linux
152 
153 template <>
154 struct NumpyEquivalentType<float> {
155  enum { type_code = NPY_FLOAT };
156 };
157 template <>
158 struct NumpyEquivalentType<double> {
159  enum { type_code = NPY_DOUBLE };
160 };
161 template <>
162 struct NumpyEquivalentType<long double> {
163  enum { type_code = NPY_LONGDOUBLE };
164 };
165 
166 template <>
167 struct NumpyEquivalentType<std::complex<float> > {
168  enum { type_code = NPY_CFLOAT };
169 };
170 template <>
171 struct NumpyEquivalentType<std::complex<double> > {
172  enum { type_code = NPY_CDOUBLE };
173 };
174 template <>
175 struct NumpyEquivalentType<std::complex<long double> > {
176  enum { type_code = NPY_CLONGDOUBLE };
177 };
178 
179 template <typename Scalar>
181  if ((int)NumpyEquivalentType<Scalar>::type_code == NPY_USERDEF) return false;
182  return true;
183 }
184 
185 } // namespace eigenpy
186 
187 namespace eigenpy {
188 #if defined _WIN32 || defined __CYGWIN__
189 EIGENPY_DLLAPI bool call_PyArray_Check(PyObject*);
190 
191 EIGENPY_DLLAPI PyObject* call_PyArray_SimpleNew(int nd, npy_intp* shape,
192  int np_type);
193 
194 EIGENPY_DLLAPI PyObject* call_PyArray_New(PyTypeObject* py_type_ptr, int nd,
195  npy_intp* shape, int np_type,
196  void* data_ptr, int options);
197 
198 EIGENPY_DLLAPI PyObject* call_PyArray_New(PyTypeObject* py_type_ptr, int nd,
199  npy_intp* shape, int np_type,
200  npy_intp* strides, void* data_ptr,
201  int options);
202 
203 EIGENPY_DLLAPI int call_PyArray_ObjectType(PyObject*, int);
204 
205 EIGENPY_DLLAPI PyTypeObject* getPyArrayType();
206 
207 EIGENPY_DLLAPI PyArray_Descr* call_PyArray_DescrFromType(int typenum);
208 
209 EIGENPY_DLLAPI void call_PyArray_InitArrFuncs(PyArray_ArrFuncs* funcs);
210 
211 EIGENPY_DLLAPI int call_PyArray_RegisterDataType(PyArray_Descr* dtype);
212 
213 EIGENPY_DLLAPI int call_PyArray_RegisterCanCast(PyArray_Descr* descr,
214  int totype,
215  NPY_SCALARKIND scalar);
216 
217 EIGENPY_DLLAPI PyArray_Descr* call_PyArray_MinScalarType(PyArrayObject* arr);
218 
219 EIGENPY_DLLAPI int call_PyArray_RegisterCastFunc(
220  PyArray_Descr* descr, int totype, PyArray_VectorUnaryFunc* castfunc);
221 #else
222 inline bool call_PyArray_Check(PyObject* py_obj) {
223  return PyArray_Check(py_obj);
224 }
225 
226 inline PyObject* call_PyArray_SimpleNew(int nd, npy_intp* shape, int np_type) {
227  return PyArray_SimpleNew(nd, shape, np_type);
228 }
229 
230 inline PyObject* call_PyArray_New(PyTypeObject* py_type_ptr, int nd,
231  npy_intp* shape, int np_type, void* data_ptr,
232  int options) {
233  return PyArray_New(py_type_ptr, nd, shape, np_type, NULL, data_ptr, 0,
234  options, NULL);
235 }
236 
237 inline PyObject* call_PyArray_New(PyTypeObject* py_type_ptr, int nd,
238  npy_intp* shape, int np_type,
239  npy_intp* strides, void* data_ptr,
240  int options) {
241  return PyArray_New(py_type_ptr, nd, shape, np_type, strides, data_ptr, 0,
242  options, NULL);
243 }
244 
245 inline int call_PyArray_ObjectType(PyObject* obj, int val) {
246  return PyArray_ObjectType(obj, val);
247 }
248 
249 inline PyTypeObject* getPyArrayType() { return &PyArray_Type; }
250 
251 inline PyArray_Descr* call_PyArray_DescrFromType(int typenum) {
252  return PyArray_DescrFromType(typenum);
253 }
254 
255 inline void call_PyArray_InitArrFuncs(PyArray_ArrFuncs* funcs) {
256  PyArray_InitArrFuncs(funcs);
257 }
258 
260  return PyArray_RegisterDataType(dtype);
261 }
262 
263 inline PyArray_Descr* call_PyArray_MinScalarType(PyArrayObject* arr) {
264  return PyArray_MinScalarType(arr);
265 }
266 
267 inline int call_PyArray_RegisterCanCast(PyArray_Descr* descr, int totype,
268  NPY_SCALARKIND scalar) {
269  return PyArray_RegisterCanCast(descr, totype, scalar);
270 }
271 
272 inline int call_PyArray_RegisterCastFunc(PyArray_Descr* descr, int totype,
273  PyArray_VectorUnaryFunc* castfunc) {
274  return PyArray_RegisterCastFunc(descr, totype, castfunc);
275 }
276 #endif
277 } // namespace eigenpy
278 
279 #endif // ifndef __eigenpy_numpy_hpp__
test_matrix.int32_t
int32_t
Definition: test_matrix.py:202
eigenpy::getPyArrayType
PyTypeObject * getPyArrayType()
Definition: numpy.hpp:249
eigenpy::PyArray_TypeNum
int EIGENPY_DLLAPI PyArray_TypeNum(PyTypeObject *type)
Definition: numpy.cpp:16
PyDataType_GetArrFuncs
static PyArray_ArrFuncs * PyDataType_GetArrFuncs(PyArray_Descr *descr)
Definition: numpy.hpp:31
eigenpy::call_PyArray_InitArrFuncs
void call_PyArray_InitArrFuncs(PyArray_ArrFuncs *funcs)
Definition: numpy.hpp:255
test_matrix.uint16_t
uint16_t
Definition: test_matrix.py:201
eigenpy::call_PyArray_DescrFromType
PyArray_Descr * call_PyArray_DescrFromType(int typenum)
Definition: numpy.hpp:251
eigenpy::isNumpyNativeType
bool isNumpyNativeType()
Definition: numpy.hpp:180
fwd.hpp
eigenpy::call_PyArray_RegisterDataType
int call_PyArray_RegisterDataType(PyArray_DescrProto *dtype)
Definition: numpy.hpp:259
test_matrix.uint32_t
uint32_t
Definition: test_matrix.py:203
eigenpy::import_numpy
void EIGENPY_DLLAPI import_numpy()
Definition: numpy.cpp:8
test_matrix.uint64_t
uint64_t
Definition: test_matrix.py:205
eigenpy::call_PyArray_Check
bool call_PyArray_Check(PyObject *py_obj)
Definition: numpy.hpp:222
eigenpy
Definition: alignment.hpp:14
eigenpy::call_PyArray_SimpleNew
PyObject * call_PyArray_SimpleNew(int nd, npy_intp *shape, int np_type)
Definition: numpy.hpp:226
eigenpy::NumpyEquivalentType::type_code
@ type_code
Definition: numpy.hpp:60
eigenpy::call_PyArray_ObjectType
int call_PyArray_ObjectType(PyObject *obj, int val)
Definition: numpy.hpp:245
eigenpy::NumpyEquivalentType
Definition: numpy.hpp:59
test_matrix.int64_t
int64_t
Definition: test_matrix.py:204
eigenpy::call_PyArray_RegisterCanCast
int call_PyArray_RegisterCanCast(PyArray_Descr *descr, int totype, NPY_SCALARKIND scalar)
Definition: numpy.hpp:267
eigenpy::call_PyArray_MinScalarType
PyArray_Descr * call_PyArray_MinScalarType(PyArrayObject *arr)
Definition: numpy.hpp:263
eigenpy::call_PyArray_New
PyObject * call_PyArray_New(PyTypeObject *py_type_ptr, int nd, npy_intp *shape, int np_type, void *data_ptr, int options)
Definition: numpy.hpp:230
eigenpy::call_PyArray_RegisterCastFunc
int call_PyArray_RegisterCastFunc(PyArray_Descr *descr, int totype, PyArray_VectorUnaryFunc *castfunc)
Definition: numpy.hpp:272
test_matrix.int16_t
int16_t
Definition: test_matrix.py:200
_Py_SET_TYPE
static void _Py_SET_TYPE(PyObject *o, PyTypeObject *type)
Definition: numpy.hpp:40
PyArray_DescrProto
#define PyArray_DescrProto
Definition: numpy.hpp:24
test_matrix.int8_t
int8_t
Definition: test_matrix.py:198


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Fri Apr 26 2024 02:17:35