Skip to content
Snippets Groups Projects
Verified Commit 1afbcfd1 authored by Justin Carpentier's avatar Justin Carpentier
Browse files

all: remove support of numpy.matrix

parent 14787a69
No related branches found
No related tags found
No related merge requests found
......@@ -24,23 +24,6 @@ print(cmd2)
ipython.magic(cmd2)
print("\n")
eigenpy.switchToNumpyMatrix()
print("----------------------")
print("switch to numpy matrix")
print("----------------------")
print("\n")
cmd3 = "timeit quat.coeffs()"
print(cmd3)
ipython.magic(cmd3)
print("\n")
eigenpy.switchToNumpyArray()
print("---------------------")
print("switch to numpy array")
print("---------------------")
print("\n")
cmd4 = "timeit quat.coeffs()"
print(cmd4)
ipython.magic(cmd4)
......
......@@ -101,9 +101,8 @@ struct eigen_to_py_impl_matrix {
PyArrayObject* pyArray;
// Allocate Python memory
if ((((!(C == 1) != !(R == 1)) && !MatrixDerived::IsVectorAtCompileTime) ||
MatrixDerived::IsVectorAtCompileTime) &&
NumpyType::getType() ==
ARRAY_TYPE) // Handle array with a single dimension
MatrixDerived::IsVectorAtCompileTime)) // Handle array with a single
// dimension
{
npy_intp shape[1] = {C == 1 ? R : C};
pyArray = NumpyAllocator<MatType>::allocate(
......
/*
* Copyright 2018-2020 INRIA
* Copyright 2018-2023 INRIA
*/
#ifndef __eigenpy_numpy_type_hpp__
......@@ -45,54 +45,30 @@ bool np_type_is_convertible_into_scalar(const int np_type) {
}
}
enum NP_TYPE { MATRIX_TYPE, ARRAY_TYPE };
struct EIGENPY_DLLAPI NumpyType {
static NumpyType& getInstance();
operator bp::object() { return getInstance().CurrentNumpyType; }
static bp::object make(PyArrayObject* pyArray, bool copy = false);
static bp::object make(PyObject* pyObj, bool copy = false);
static void setNumpyType(bp::object& obj);
static void sharedMemory(const bool value);
static bool sharedMemory();
static void switchToNumpyArray();
static void switchToNumpyMatrix();
static NP_TYPE& getType();
static bp::object getNumpyType();
static const PyTypeObject* getNumpyMatrixType();
static const PyTypeObject* getNumpyArrayType();
static bool isMatrix();
static bool isArray();
protected:
NumpyType();
bp::object CurrentNumpyType;
bp::object pyModule;
// Numpy types
bp::object NumpyMatrixObject;
PyTypeObject* NumpyMatrixType;
// bp::object NumpyAsMatrixObject; PyTypeObject * NumpyAsMatrixType;
bp::object NumpyArrayObject;
PyTypeObject* NumpyArrayType;
NP_TYPE np_type;
bool shared_memory;
};
} // namespace eigenpy
......
/*
* Copyright 2014-2019, CNRS
* Copyright 2018-2021, INRIA
* Copyright 2018-2023, INRIA
*/
#include "eigenpy/eigenpy.hpp"
......@@ -30,20 +30,6 @@ void enableEigenPy() {
Exception::registerException();
bp::def(
"setNumpyType", &NumpyType::setNumpyType, bp::arg("numpy_type"),
"Change the Numpy type returned by the converters from an Eigen object.");
bp::def(
"getNumpyType", &NumpyType::getNumpyType,
"Get the Numpy type returned by the converters from an Eigen object.");
bp::def("switchToNumpyArray", &NumpyType::switchToNumpyArray,
"Set the conversion from Eigen::Matrix to numpy.ndarray.");
bp::def("switchToNumpyMatrix", &NumpyType::switchToNumpyMatrix,
"Set the conversion from Eigen::Matrix to numpy.matrix.");
bp::def("sharedMemory", (void (*)(const bool))NumpyType::sharedMemory,
bp::arg("value"),
"Share the memory when converting from Eigen to Numpy.");
......
/*
* Copyright 2018-2020 INRIA
* Copyright 2018-2023 INRIA
*/
#include "eigenpy/numpy-type.hpp"
......@@ -17,70 +17,24 @@ bp::object NumpyType::make(PyArrayObject* pyArray, bool copy) {
return make((PyObject*)pyArray, copy);
}
bp::object NumpyType::make(PyObject* pyObj, bool 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
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");
......@@ -90,16 +44,8 @@ NumpyType::NumpyType() {
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;
}
......
......@@ -80,10 +80,6 @@ if(NOT NUMPY_WITH_BROKEN_UFUNC_SUPPORT)
"unittest")
endif()
add_python_unit_test("py-switch" "unittest/python/test_switch.py"
"python;unittest")
set_tests_properties("py-switch" PROPERTIES DEPENDS ${PYWRAP})
add_python_unit_test("py-dimensions" "unittest/python/test_dimensions.py"
"python;unittest")
set_tests_properties("py-dimensions" PROPERTIES DEPENDS ${PYWRAP})
......
from __future__ import print_function
import numpy as np
from complex import switchToNumpyArray, real, imag, ascomplex
switchToNumpyArray()
from complex import real, imag, ascomplex
rows = 10
cols = 20
......
......@@ -3,12 +3,7 @@ from __future__ import print_function
import eigenpy
quat = eigenpy.Quaternion()
# By default, we convert as numpy.matrix
eigenpy.switchToNumpyMatrix()
coeffs_vector = quat.coeffs()
assert len(coeffs_vector.shape) == 2
# Switch to numpy.array
eigenpy.switchToNumpyArray()
coeffs_vector = quat.coeffs()
assert len(coeffs_vector.shape) == 1
import numpy as np
import eigenpy
eigenpy.switchToNumpyArray()
dim = 100
A = np.random.rand(dim, dim)
......
......@@ -2,8 +2,6 @@ import eigenpy
import numpy as np
eigenpy.switchToNumpyArray()
dim = 100
A = np.random.rand(dim, dim)
A = (A + A.T) * 0.5
......
from __future__ import print_function
import eigenpy
import numpy as np
eigenpy.switchToNumpyMatrix()
quat = eigenpy.Quaternion()
# By default, we convert as numpy.matrix
coeffs_vector = quat.coeffs()
print(type(coeffs_vector))
assert isinstance(coeffs_vector, np.matrixlib.defmatrix.matrix)
assert eigenpy.getNumpyType() == np.matrix
# Switch to numpy.array
eigenpy.switchToNumpyArray()
coeffs_array = quat.coeffs()
print(type(coeffs_array))
assert isinstance(coeffs_vector, np.ndarray)
assert eigenpy.getNumpyType() == np.ndarray
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment