5#ifndef __eigenpy_numpy_hpp__ 6#define __eigenpy_numpy_hpp__ 8#include "eigenpy/config.hpp" 10#ifndef PY_ARRAY_UNIQUE_SYMBOL 11#define PY_ARRAY_UNIQUE_SYMBOL EIGENPY_ARRAY_API 16#define NPY_API_SYMBOL_ATTRIBUTE EIGENPY_DLLAPI 27#include <boost/python/detail/wrap_python.hpp> 29#include <numpy/numpyconfig.h> 30#ifdef NPY_1_8_API_VERSION 31#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 36#if NPY_ABI_VERSION < 0x02000000 37#define PyArray_DescrProto PyArray_Descr 40#include <numpy/ndarrayobject.h> 41#include <numpy/ufuncobject.h> 43#if NPY_ABI_VERSION < 0x02000000 44static inline PyArray_ArrFuncs* PyDataType_GetArrFuncs(PyArray_Descr* descr) {
52#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE) 53static inline void _Py_SET_TYPE(PyObject* o, PyTypeObject* type) {
56#define Py_SET_TYPE(o, type) _Py_SET_TYPE((PyObject*)(o), type) 59#if defined _WIN32 || defined __CYGWIN__ 60#define EIGENPY_GET_PY_ARRAY_TYPE(array) \ 61 call_PyArray_MinScalarType(array)->type_num 63#define EIGENPY_GET_PY_ARRAY_TYPE(array) PyArray_MinScalarType(array)->type_num 69void EIGENPY_DLLAPI import_numpy();
70int EIGENPY_DLLAPI PyArray_TypeNum(PyTypeObject*
type);
73template <
typename Scalar,
typename Enable =
void>
75 enum { type_code = NPY_USERDEF };
80 enum { type_code = NPY_BOOL };
85 enum { type_code = NPY_INT8 };
89 enum { type_code = NPY_UINT8 };
93 enum { type_code = NPY_INT8 };
98 enum { type_code = NPY_INT16 };
102 enum { type_code = NPY_UINT16 };
107 enum { type_code = NPY_INT32 };
111 enum { type_code = NPY_UINT32 };
116#if defined _WIN32 || defined __CYGWIN__ 120 enum { type_code = NPY_INT32 };
124 enum { type_code = NPY_UINT32 };
131 enum { type_code = NPY_INT64 };
135 enum { type_code = NPY_UINT64 };
144 enum { type_code = NPY_INT64 };
148 enum { type_code = NPY_UINT64 };
157#include <type_traits> 159template <
typename Scalar>
162 typename std::enable_if<!std::is_same<int64_t, long long>::value &&
163 std::is_same<Scalar, long long>::value>::type> {
164 enum { type_code = NPY_LONGLONG };
166template <
typename Scalar>
168 Scalar, typename std::enable_if<
169 !std::is_same<uint64_t, unsigned long long>::value &&
170 std::is_same<Scalar, unsigned long long>::value>::type> {
171 enum { type_code = NPY_ULONGLONG };
178 enum { type_code = NPY_FLOAT };
182 enum { type_code = NPY_DOUBLE };
186 enum { type_code = NPY_LONGDOUBLE };
191 enum { type_code = NPY_CFLOAT };
195 enum { type_code = NPY_CDOUBLE };
199 enum { type_code = NPY_CLONGDOUBLE };
202template <
typename Scalar>
203bool isNumpyNativeType() {
204 if ((
int)NumpyEquivalentType<Scalar>::type_code == NPY_USERDEF)
return false;
211#if defined _WIN32 || defined __CYGWIN__ 212EIGENPY_DLLAPI
bool call_PyArray_Check(PyObject*);
214EIGENPY_DLLAPI PyObject* call_PyArray_SimpleNew(
int nd, npy_intp* shape,
217EIGENPY_DLLAPI PyObject* call_PyArray_New(PyTypeObject* py_type_ptr,
int nd,
218 npy_intp* shape,
int np_type,
219 void* data_ptr,
int options);
221EIGENPY_DLLAPI PyObject* call_PyArray_New(PyTypeObject* py_type_ptr,
int nd,
222 npy_intp* shape,
int np_type,
223 npy_intp* strides,
void* data_ptr,
226EIGENPY_DLLAPI
int call_PyArray_ObjectType(PyObject*,
int);
228EIGENPY_DLLAPI PyTypeObject* getPyArrayType();
230EIGENPY_DLLAPI PyArray_Descr* call_PyArray_DescrFromType(
int typenum);
232EIGENPY_DLLAPI
void call_PyArray_InitArrFuncs(PyArray_ArrFuncs* funcs);
234EIGENPY_DLLAPI
int call_PyArray_RegisterDataType(PyArray_DescrProto* dtype);
236EIGENPY_DLLAPI
int call_PyArray_RegisterCanCast(PyArray_Descr* descr,
238 NPY_SCALARKIND scalar);
240EIGENPY_DLLAPI PyArray_Descr* call_PyArray_MinScalarType(PyArrayObject* arr);
242EIGENPY_DLLAPI
int call_PyArray_RegisterCastFunc(
243 PyArray_Descr* descr,
int totype, PyArray_VectorUnaryFunc* castfunc);
245inline bool call_PyArray_Check(PyObject* py_obj) {
246 return PyArray_Check(py_obj);
249inline PyObject* call_PyArray_SimpleNew(
int nd, npy_intp* shape,
int np_type) {
250 return PyArray_SimpleNew(nd, shape, np_type);
253inline PyObject* call_PyArray_New(PyTypeObject* py_type_ptr,
int nd,
254 npy_intp* shape,
int np_type,
void* data_ptr,
256 return PyArray_New(py_type_ptr, nd, shape, np_type, NULL, data_ptr, 0,
260inline PyObject* call_PyArray_New(PyTypeObject* py_type_ptr,
int nd,
261 npy_intp* shape,
int np_type,
262 npy_intp* strides,
void* data_ptr,
264 return PyArray_New(py_type_ptr, nd, shape, np_type, strides, data_ptr, 0,
268inline int call_PyArray_ObjectType(PyObject* obj,
int val) {
269 return PyArray_ObjectType(obj, val);
272inline PyTypeObject* getPyArrayType() {
return &PyArray_Type; }
274inline PyArray_Descr* call_PyArray_DescrFromType(
int typenum) {
275 return PyArray_DescrFromType(typenum);
278inline void call_PyArray_InitArrFuncs(PyArray_ArrFuncs* funcs) {
279 PyArray_InitArrFuncs(funcs);
282inline int call_PyArray_RegisterDataType(PyArray_DescrProto* dtype) {
283 return PyArray_RegisterDataType(dtype);
286inline PyArray_Descr* call_PyArray_MinScalarType(PyArrayObject* arr) {
287 return PyArray_MinScalarType(arr);
290inline int call_PyArray_RegisterCanCast(PyArray_Descr* descr,
int totype,
291 NPY_SCALARKIND scalar) {
292 return PyArray_RegisterCanCast(descr, totype, scalar);
295inline int call_PyArray_RegisterCastFunc(PyArray_Descr* descr,
int totype,
296 PyArray_VectorUnaryFunc* castfunc) {
297 return PyArray_RegisterCastFunc(descr, totype, castfunc);