numpy-type.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-2020 INRIA
3 */
4 
5 #ifndef __eigenpy_numpy_type_hpp__
6 #define __eigenpy_numpy_type_hpp__
7 
8 #include "eigenpy/fwd.hpp"
10 
11 #include <stdexcept>
12 #include <typeinfo>
13 #include <sstream>
14 
15 namespace eigenpy
16 {
17  namespace bp = boost::python;
18 
19  // By default, the Scalar is considered as a Python object
20  template <typename Scalar> struct NumpyEquivalentType { enum { type_code = NPY_USERDEF };};
21 
22  template <> struct NumpyEquivalentType<float> { enum { type_code = NPY_FLOAT };};
23  template <> struct NumpyEquivalentType< std::complex<float> > { enum { type_code = NPY_CFLOAT };};
24  template <> struct NumpyEquivalentType<double> { enum { type_code = NPY_DOUBLE };};
25  template <> struct NumpyEquivalentType< std::complex<double> > { enum { type_code = NPY_CDOUBLE };};
26  template <> struct NumpyEquivalentType<long double> { enum { type_code = NPY_LONGDOUBLE };};
27  template <> struct NumpyEquivalentType< std::complex<long double> > { enum { type_code = NPY_CLONGDOUBLE };};
28  template <> struct NumpyEquivalentType<bool> { enum { type_code = NPY_BOOL };};
29  template <> struct NumpyEquivalentType<int> { enum { type_code = NPY_INT };};
30  template <> struct NumpyEquivalentType<long> { enum { type_code = NPY_LONG };};
31 
32  template<typename Scalar>
34  {
35  if((int)NumpyEquivalentType<Scalar>::type_code == NPY_USERDEF)
36  return false;
37  return true;
38  }
39 
40  template<typename Scalar>
41  bool np_type_is_convertible_into_scalar(const int np_type)
42  {
44  return true;
45 
46  switch(np_type)
47  {
48  case NPY_INT:
50  case NPY_LONG:
52  case NPY_FLOAT:
54  case NPY_CFLOAT:
56  case NPY_DOUBLE:
58  case NPY_CDOUBLE:
60  case NPY_LONGDOUBLE:
62  case NPY_CLONGDOUBLE:
64  default:
65  return false;
66  }
67  }
68 
69  enum NP_TYPE
70  {
73  };
74 
75  struct EIGENPY_DLLAPI NumpyType
76  {
77 
78  static NumpyType & getInstance();
79 
80  operator bp::object () { return getInstance().CurrentNumpyType; }
81 
82  static bp::object make(PyArrayObject* pyArray, bool copy = false);
83 
84  static bp::object make(PyObject* pyObj, bool copy = false);
85 
86  static void setNumpyType(bp::object & obj);
87 
88  static void sharedMemory(const bool value);
89 
90  static bool sharedMemory();
91 
92  static void switchToNumpyArray();
93 
94  static void switchToNumpyMatrix();
95 
96  static NP_TYPE & getType();
97 
98  static bp::object getNumpyType();
99 
100  static const PyTypeObject * getNumpyMatrixType();
101 
102  static const PyTypeObject * getNumpyArrayType();
103 
104  static bool isMatrix();
105 
106  static bool isArray();
107 
108  protected:
109 
110  NumpyType();
111 
112  bp::object CurrentNumpyType;
113  bp::object pyModule;
114 
115  // Numpy types
116  bp::object NumpyMatrixObject; PyTypeObject * NumpyMatrixType;
117  //bp::object NumpyAsMatrixObject; PyTypeObject * NumpyAsMatrixType;
118  bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType;
119 
121 
123  };
124 }
125 
126 #endif // ifndef __eigenpy_numpy_type_hpp__
bool isNumpyNativeType()
Definition: numpy-type.hpp:33
bp::object CurrentNumpyType
Definition: numpy-type.hpp:112
PyTypeObject * NumpyMatrixType
Definition: numpy-type.hpp:116
bool np_type_is_convertible_into_scalar(const int np_type)
Definition: numpy-type.hpp:41
PyTypeObject * NumpyArrayType
Definition: numpy-type.hpp:118
bp::object pyModule
Definition: numpy-type.hpp:113


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