numpy-type.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-2020 INRIA
3 */
4 
5 #include "eigenpy/numpy-type.hpp"
6 
7 #include <patchlevel.h> // For PY_MAJOR_VERSION
8 
9 namespace eigenpy
10 {
11  namespace bp = boost::python;
12 
14  {
15  static NumpyType instance;
16  return instance;
17  }
18 
19  bp::object NumpyType::make(PyArrayObject* pyArray, bool copy)
20  { return make((PyObject*)pyArray,copy); }
21 
22  bp::object NumpyType::make(PyObject* pyObj, bool copy)
23  {
24  bp::object m;
25  if(isMatrix())
26  m = getInstance().NumpyMatrixObject(bp::object(bp::handle<>(pyObj)), bp::object(), copy);
27 // m = NumpyAsMatrixObject(bp::object(bp::handle<>(pyObj)));
28  else if(isArray())
29  m = bp::object(bp::handle<>(pyObj)); // nothing to do here
30 
31  Py_INCREF(m.ptr());
32  return m;
33  }
34 
35  void NumpyType::setNumpyType(bp::object & obj)
36  {
37  PyTypeObject * obj_type = PyType_Check(obj.ptr()) ? reinterpret_cast<PyTypeObject*>(obj.ptr()) : obj.ptr()->ob_type;
38  if(PyType_IsSubtype(obj_type,getInstance().NumpyMatrixType))
40  else if(PyType_IsSubtype(obj_type,getInstance().NumpyArrayType))
42  }
43 
44  void NumpyType::sharedMemory(const bool value)
45  {
47  }
48 
50  {
51  return getInstance().shared_memory;
52  }
53 
55  {
58  }
59 
61  {
64  }
65 
67  {
68  return getInstance().np_type;
69  }
70 
72  {
74  }
75 
76  const PyTypeObject * NumpyType::getNumpyMatrixType()
77  {
79  }
80 
81  const PyTypeObject * NumpyType::getNumpyArrayType()
82  {
83  return getInstance().NumpyArrayType;
84  }
85 
87  {
88  return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()),
90  }
91 
93  {
94  if(getInstance().isMatrix()) return false;
95  return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()),
97  }
98 
100  {
101  pyModule = bp::import("numpy");
102 
103 #if PY_MAJOR_VERSION >= 3
104  // TODO I don't know why this Py_INCREF is necessary.
105  // Without it, the destructor of NumpyType SEGV sometimes.
106  Py_INCREF(pyModule.ptr());
107 #endif
108 
109  NumpyMatrixObject = pyModule.attr("matrix");
110  NumpyMatrixType = reinterpret_cast<PyTypeObject*>(NumpyMatrixObject.ptr());
111  NumpyArrayObject = pyModule.attr("ndarray");
112  NumpyArrayType = reinterpret_cast<PyTypeObject*>(NumpyArrayObject.ptr());
113  //NumpyAsMatrixObject = pyModule.attr("asmatrix");
114  //NumpyAsMatrixType = reinterpret_cast<PyTypeObject*>(NumpyAsMatrixObject.ptr());
115 
116  CurrentNumpyType = NumpyArrayObject; // default conversion
118 
119  shared_memory = true;
120  }
121 }
static bool sharedMemory()
Definition: numpy-type.cpp:49
bp::object NumpyArrayObject
Definition: numpy-type.hpp:118
static void switchToNumpyMatrix()
Definition: numpy-type.cpp:60
static bool isMatrix()
Definition: numpy-type.cpp:86
static bp::object make(PyArrayObject *pyArray, bool copy=false)
Definition: numpy-type.cpp:19
static const PyTypeObject * getNumpyArrayType()
Definition: numpy-type.cpp:81
bp::object CurrentNumpyType
Definition: numpy-type.hpp:112
static bp::object getNumpyType()
Definition: numpy-type.cpp:71
static void setNumpyType(bp::object &obj)
Definition: numpy-type.cpp:35
static NP_TYPE & getType()
Definition: numpy-type.cpp:66
PyTypeObject * NumpyMatrixType
Definition: numpy-type.hpp:116
PyTypeObject * NumpyArrayType
Definition: numpy-type.hpp:118
static const PyTypeObject * getNumpyMatrixType()
Definition: numpy-type.cpp:76
static NumpyType & getInstance()
Definition: numpy-type.cpp:13
bp::object pyModule
Definition: numpy-type.hpp:113
static void switchToNumpyArray()
Definition: numpy-type.cpp:54
static bool isArray()
Definition: numpy-type.cpp:92
bp::object NumpyMatrixObject
Definition: numpy-type.hpp:116


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