numpy-type.hpp
Go to the documentation of this file.
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"
15 
16 namespace eigenpy {
17 
18 template <typename Scalar>
19 bool 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:
72  case NPY_DOUBLE:
74  case NPY_CDOUBLE:
76  case NPY_LONGDOUBLE:
78  case NPY_CLONGDOUBLE:
80  default:
81  return false;
82  }
83 }
84 
85 struct 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 
108 };
109 } // namespace eigenpy
110 
111 #endif // ifndef __eigenpy_numpy_type_hpp__
register.hpp
fwd.hpp
scalar-conversion.hpp
eigenpy::NumpyType
Definition: numpy-type.hpp:85
eigenpy::NumpyType::shared_memory
bool shared_memory
Definition: numpy-type.hpp:107
eigenpy::NumpyType::NumpyArrayType
PyTypeObject * NumpyArrayType
Definition: numpy-type.hpp:105
eigenpy
Definition: alignment.hpp:14
copy
ReturnMatrix copy(const Eigen::MatrixBase< Matrix > &mat)
Definition: matrix.cpp:131
test_matrix.value
float value
Definition: test_matrix.py:161
eigenpy::NumpyEquivalentType
Definition: numpy.hpp:66
eigenpy::NumpyType::NumpyArrayObject
bp::object NumpyArrayObject
Definition: numpy-type.hpp:104
eigenpy::FromTypeToType
Definition: scalar-conversion.hpp:15
eigenpy::np_type_is_convertible_into_scalar
bool np_type_is_convertible_into_scalar(const int np_type)
Definition: numpy-type.hpp:19
eigenpy::NumpyType::pyModule
bp::object pyModule
Definition: numpy-type.hpp:101


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