diff --git a/CMakeLists.txt b/CMakeLists.txt index 6925e0a8c9aacee8139da345c43023a9f09847b9..77d90c82455d05119b97d69ca758f508995d8d6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,6 +150,7 @@ SET(${PROJECT_NAME}_SOURCES src/exception.cpp src/eigenpy.cpp src/numpy.cpp + src/numpy-type.cpp src/matrix-float.cpp src/matrix-complex-float.cpp src/matrix-complex-double.cpp diff --git a/include/eigenpy/numpy-type.hpp b/include/eigenpy/numpy-type.hpp index 1da922b2b378b30172f71bac452abb78ef15847c..cf262c2f58b9decbff768edd22a9c6235747f810 100644 --- a/include/eigenpy/numpy-type.hpp +++ b/include/eigenpy/numpy-type.hpp @@ -8,7 +8,6 @@ #include "eigenpy/fwd.hpp" #include "eigenpy/scalar-conversion.hpp" -#include <patchlevel.h> // For PY_MAJOR_VERSION #include <stdexcept> #include <typeinfo> #include <sstream> @@ -76,118 +75,39 @@ namespace eigenpy struct NumpyType { - static NumpyType & getInstance() - { - static NumpyType instance; - return instance; - } + static NumpyType & getInstance(); operator bp::object () { return getInstance().CurrentNumpyType; } - static bp::object make(PyArrayObject* pyArray, bool copy = false) - { return make((PyObject*)pyArray,copy); } + static bp::object make(PyArrayObject* pyArray, bool copy = false); - static bp::object make(PyObject* pyObj, bool copy = false) - { - bp::object m; - if(isMatrix()) - m = getInstance().NumpyMatrixObject(bp::object(bp::handle<>(pyObj)), bp::object(), copy); -// m = NumpyAsMatrixObject(bp::object(bp::handle<>(pyObj))); - else if(isArray()) - m = bp::object(bp::handle<>(pyObj)); // nothing to do here - - Py_INCREF(m.ptr()); - return m; - } + static bp::object make(PyObject* pyObj, bool copy = false); - static void setNumpyType(bp::object & obj) - { - PyTypeObject * obj_type = PyType_Check(obj.ptr()) ? reinterpret_cast<PyTypeObject*>(obj.ptr()) : obj.ptr()->ob_type; - if(PyType_IsSubtype(obj_type,getInstance().NumpyMatrixType)) - switchToNumpyMatrix(); - else if(PyType_IsSubtype(obj_type,getInstance().NumpyArrayType)) - switchToNumpyArray(); - } + static void setNumpyType(bp::object & obj); - static void sharedMemory(const bool value) - { - getInstance().shared_memory = value; - } + static void sharedMemory(const bool value); - static bool sharedMemory() - { - return getInstance().shared_memory; - } + static bool sharedMemory(); - static void switchToNumpyArray() - { - getInstance().CurrentNumpyType = getInstance().NumpyArrayObject; - getInstance().getType() = ARRAY_TYPE; - } + static void switchToNumpyArray(); - static void switchToNumpyMatrix() - { - getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject; - getInstance().getType() = MATRIX_TYPE; - } + static void switchToNumpyMatrix(); - static NP_TYPE & getType() - { - return getInstance().np_type; - } + static NP_TYPE & getType(); - static bp::object getNumpyType() - { - return getInstance().CurrentNumpyType; - } + static bp::object getNumpyType(); - static const PyTypeObject * getNumpyMatrixType() - { - return getInstance().NumpyMatrixType; - } + static const PyTypeObject * getNumpyMatrixType(); - static const PyTypeObject * getNumpyArrayType() - { - return getInstance().NumpyArrayType; - } + static const PyTypeObject * getNumpyArrayType(); - static bool isMatrix() - { - return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()), - getInstance().NumpyMatrixType); - } + static bool isMatrix(); - static bool isArray() - { - if(getInstance().isMatrix()) return false; - return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()), - getInstance().NumpyArrayType); - } + static bool isArray(); protected: - NumpyType() - { - pyModule = bp::import("numpy"); - -#if PY_MAJOR_VERSION >= 3 - // TODO I don't know why this Py_INCREF is necessary. - // Without it, the destructor of NumpyType SEGV sometimes. - Py_INCREF(pyModule.ptr()); -#endif - - NumpyMatrixObject = pyModule.attr("matrix"); - NumpyMatrixType = reinterpret_cast<PyTypeObject*>(NumpyMatrixObject.ptr()); - NumpyArrayObject = pyModule.attr("ndarray"); - NumpyArrayType = reinterpret_cast<PyTypeObject*>(NumpyArrayObject.ptr()); - //NumpyAsMatrixObject = pyModule.attr("asmatrix"); - //NumpyAsMatrixType = reinterpret_cast<PyTypeObject*>(NumpyAsMatrixObject.ptr()); - - CurrentNumpyType = NumpyArrayObject; // default conversion - np_type = ARRAY_TYPE; - - shared_memory = true; - } + NumpyType(); bp::object CurrentNumpyType; bp::object pyModule; diff --git a/src/numpy-type.cpp b/src/numpy-type.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd235f01217b628e4bc337bafc17e33fe9b35d42 --- /dev/null +++ b/src/numpy-type.cpp @@ -0,0 +1,121 @@ +/* + * Copyright 2018-2020 INRIA +*/ + +#include "eigenpy/numpy-type.hpp" + +#include <patchlevel.h> // For PY_MAJOR_VERSION + +namespace eigenpy +{ + namespace bp = boost::python; + + NumpyType & NumpyType::getInstance() + { + static NumpyType instance; + return instance; + } + + bp::object NumpyType::make(PyArrayObject* pyArray, bool copy) + { return make((PyObject*)pyArray,copy); } + + bp::object NumpyType::make(PyObject* pyObj, bool copy) + { + bp::object m; + if(isMatrix()) + m = getInstance().NumpyMatrixObject(bp::object(bp::handle<>(pyObj)), bp::object(), copy); +// m = NumpyAsMatrixObject(bp::object(bp::handle<>(pyObj))); + else if(isArray()) + m = bp::object(bp::handle<>(pyObj)); // nothing to do here + + Py_INCREF(m.ptr()); + return m; + } + + void NumpyType::setNumpyType(bp::object & obj) + { + PyTypeObject * obj_type = PyType_Check(obj.ptr()) ? reinterpret_cast<PyTypeObject*>(obj.ptr()) : obj.ptr()->ob_type; + if(PyType_IsSubtype(obj_type,getInstance().NumpyMatrixType)) + switchToNumpyMatrix(); + else if(PyType_IsSubtype(obj_type,getInstance().NumpyArrayType)) + switchToNumpyArray(); + } + + void NumpyType::sharedMemory(const bool value) + { + getInstance().shared_memory = value; + } + + bool NumpyType::sharedMemory() + { + return getInstance().shared_memory; + } + + void NumpyType::switchToNumpyArray() + { + getInstance().CurrentNumpyType = getInstance().NumpyArrayObject; + getInstance().getType() = ARRAY_TYPE; + } + + void NumpyType::switchToNumpyMatrix() + { + getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject; + getInstance().getType() = MATRIX_TYPE; + } + + NP_TYPE & NumpyType::getType() + { + return getInstance().np_type; + } + + bp::object NumpyType::getNumpyType() + { + return getInstance().CurrentNumpyType; + } + + const PyTypeObject * NumpyType::getNumpyMatrixType() + { + return getInstance().NumpyMatrixType; + } + + const PyTypeObject * NumpyType::getNumpyArrayType() + { + return getInstance().NumpyArrayType; + } + + bool NumpyType::isMatrix() + { + return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()), + getInstance().NumpyMatrixType); + } + + bool NumpyType::isArray() + { + if(getInstance().isMatrix()) return false; + return PyType_IsSubtype(reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()), + getInstance().NumpyArrayType); + } + + NumpyType::NumpyType() + { + pyModule = bp::import("numpy"); + +#if PY_MAJOR_VERSION >= 3 + // TODO I don't know why this Py_INCREF is necessary. + // Without it, the destructor of NumpyType SEGV sometimes. + Py_INCREF(pyModule.ptr()); +#endif + + NumpyMatrixObject = pyModule.attr("matrix"); + NumpyMatrixType = reinterpret_cast<PyTypeObject*>(NumpyMatrixObject.ptr()); + NumpyArrayObject = pyModule.attr("ndarray"); + NumpyArrayType = reinterpret_cast<PyTypeObject*>(NumpyArrayObject.ptr()); + //NumpyAsMatrixObject = pyModule.attr("asmatrix"); + //NumpyAsMatrixType = reinterpret_cast<PyTypeObject*>(NumpyAsMatrixObject.ptr()); + + CurrentNumpyType = NumpyArrayObject; // default conversion + np_type = ARRAY_TYPE; + + shared_memory = true; + } +}