eigenpy 3.12.0
Bindings between Numpy and Eigen using Boost.Python
Loading...
Searching...
No Matches
numpy-type.hpp
1/*
2 * Copyright 2018-2023 INRIA
3 */
4
5#ifndef __eigenpy_numpy_type_hpp__
6#define __eigenpy_numpy_type_hpp__
7
8#include <sstream>
9#include <stdexcept>
10#include <typeinfo>
11
12#include "eigenpy/fwd.hpp"
13#include "eigenpy/register.hpp"
14#include "eigenpy/scalar-conversion.hpp"
15
16namespace eigenpy {
17
18template <typename Scalar>
19bool np_type_is_convertible_into_scalar(const int np_type) {
20 const auto scalar_np_code =
21 static_cast<NPY_TYPES>(NumpyEquivalentType<Scalar>::type_code);
22
23 if (scalar_np_code >= NPY_USERDEF)
24 return np_type == Register::getTypeCode<Scalar>();
25
26 if (scalar_np_code == np_type) return true;
27
28 // Manage type promotion
29 switch (np_type) {
30 case NPY_BOOL:
32 case NPY_INT8:
34 case NPY_INT16:
36 case NPY_INT32:
38 case NPY_INT64:
40 case NPY_UINT8:
42 case NPY_UINT16:
44 case NPY_UINT32:
46 case NPY_UINT64:
48
49#if defined _WIN32 || defined __CYGWIN__
50 // Manage NPY_INT on Windows (NPY_INT32 is NPY_LONG).
51 // See https://github.com/stack-of-tasks/eigenpy/pull/455
52 case NPY_INT:
54 case NPY_UINT:
56#endif // WIN32
57
58#if defined __APPLE__
59 // Manage NPY_LONGLONG on Mac (NPY_INT64 is NPY_LONG)..
60 // long long and long are both the same type
61 // but NPY_LONGLONG and NPY_LONGĀ are different dtype.
62 // See https://github.com/stack-of-tasks/eigenpy/pull/455
63 case NPY_LONGLONG:
65 case NPY_ULONGLONG:
67#endif // MAC
68 case NPY_FLOAT:
70 case NPY_CFLOAT:
71 return FromTypeToType<std::complex<float>, Scalar>::value;
72 case NPY_DOUBLE:
74 case NPY_CDOUBLE:
75 return FromTypeToType<std::complex<double>, Scalar>::value;
76 case NPY_LONGDOUBLE:
78 case NPY_CLONGDOUBLE:
79 return FromTypeToType<std::complex<long double>, Scalar>::value;
80 default:
81 return false;
82 }
83}
84
85struct EIGENPY_DLLAPI NumpyType {
86 static NumpyType& getInstance();
87
88 static bp::object make(PyArrayObject* pyArray, bool copy = false);
89
90 static bp::object make(PyObject* pyObj, bool copy = false);
91
92 static void sharedMemory(const bool value);
93
94 static bool sharedMemory();
95
96 static const PyTypeObject* getNumpyArrayType();
97
98 protected:
99 NumpyType();
100
101 bp::object pyModule;
102
103 // Numpy types
104 bp::object NumpyArrayObject;
105 PyTypeObject* NumpyArrayType;
106
107 bool shared_memory;
108};
109} // namespace eigenpy
110
111#endif // ifndef __eigenpy_numpy_type_hpp__