Skip to content

Commit 1afbcfd

Browse files
committed
all: remove support of numpy.matrix
1 parent 14787a6 commit 1afbcfd

File tree

11 files changed

+8
-154
lines changed

11 files changed

+8
-154
lines changed

benchmarks/bench-switch.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,6 @@
2424
ipython.magic(cmd2)
2525
print("\n")
2626

27-
eigenpy.switchToNumpyMatrix()
28-
print("----------------------")
29-
print("switch to numpy matrix")
30-
print("----------------------")
31-
print("\n")
32-
33-
cmd3 = "timeit quat.coeffs()"
34-
print(cmd3)
35-
ipython.magic(cmd3)
36-
print("\n")
37-
38-
eigenpy.switchToNumpyArray()
39-
print("---------------------")
40-
print("switch to numpy array")
41-
print("---------------------")
42-
print("\n")
43-
4427
cmd4 = "timeit quat.coeffs()"
4528
print(cmd4)
4629
ipython.magic(cmd4)

include/eigenpy/eigen-to-python.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ struct eigen_to_py_impl_matrix {
101101
PyArrayObject* pyArray;
102102
// Allocate Python memory
103103
if ((((!(C == 1) != !(R == 1)) && !MatrixDerived::IsVectorAtCompileTime) ||
104-
MatrixDerived::IsVectorAtCompileTime) &&
105-
NumpyType::getType() ==
106-
ARRAY_TYPE) // Handle array with a single dimension
104+
MatrixDerived::IsVectorAtCompileTime)) // Handle array with a single
105+
// dimension
107106
{
108107
npy_intp shape[1] = {C == 1 ? R : C};
109108
pyArray = NumpyAllocator<MatType>::allocate(

include/eigenpy/numpy-type.hpp

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 INRIA
2+
* Copyright 2018-2023 INRIA
33
*/
44

55
#ifndef __eigenpy_numpy_type_hpp__
@@ -45,54 +45,30 @@ bool np_type_is_convertible_into_scalar(const int np_type) {
4545
}
4646
}
4747

48-
enum NP_TYPE { MATRIX_TYPE, ARRAY_TYPE };
49-
5048
struct EIGENPY_DLLAPI NumpyType {
5149
static NumpyType& getInstance();
5250

53-
operator bp::object() { return getInstance().CurrentNumpyType; }
54-
5551
static bp::object make(PyArrayObject* pyArray, bool copy = false);
5652

5753
static bp::object make(PyObject* pyObj, bool copy = false);
5854

59-
static void setNumpyType(bp::object& obj);
60-
6155
static void sharedMemory(const bool value);
6256

6357
static bool sharedMemory();
6458

65-
static void switchToNumpyArray();
66-
67-
static void switchToNumpyMatrix();
68-
69-
static NP_TYPE& getType();
70-
7159
static bp::object getNumpyType();
7260

73-
static const PyTypeObject* getNumpyMatrixType();
74-
7561
static const PyTypeObject* getNumpyArrayType();
7662

77-
static bool isMatrix();
78-
79-
static bool isArray();
80-
8163
protected:
8264
NumpyType();
8365

84-
bp::object CurrentNumpyType;
8566
bp::object pyModule;
8667

8768
// Numpy types
88-
bp::object NumpyMatrixObject;
89-
PyTypeObject* NumpyMatrixType;
90-
// bp::object NumpyAsMatrixObject; PyTypeObject * NumpyAsMatrixType;
9169
bp::object NumpyArrayObject;
9270
PyTypeObject* NumpyArrayType;
9371

94-
NP_TYPE np_type;
95-
9672
bool shared_memory;
9773
};
9874
} // namespace eigenpy

src/eigenpy.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright 2014-2019, CNRS
3-
* Copyright 2018-2021, INRIA
3+
* Copyright 2018-2023, INRIA
44
*/
55

66
#include "eigenpy/eigenpy.hpp"
@@ -30,20 +30,6 @@ void enableEigenPy() {
3030

3131
Exception::registerException();
3232

33-
bp::def(
34-
"setNumpyType", &NumpyType::setNumpyType, bp::arg("numpy_type"),
35-
"Change the Numpy type returned by the converters from an Eigen object.");
36-
37-
bp::def(
38-
"getNumpyType", &NumpyType::getNumpyType,
39-
"Get the Numpy type returned by the converters from an Eigen object.");
40-
41-
bp::def("switchToNumpyArray", &NumpyType::switchToNumpyArray,
42-
"Set the conversion from Eigen::Matrix to numpy.ndarray.");
43-
44-
bp::def("switchToNumpyMatrix", &NumpyType::switchToNumpyMatrix,
45-
"Set the conversion from Eigen::Matrix to numpy.matrix.");
46-
4733
bp::def("sharedMemory", (void (*)(const bool))NumpyType::sharedMemory,
4834
bp::arg("value"),
4935
"Share the memory when converting from Eigen to Numpy.");

src/numpy-type.cpp

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2020 INRIA
2+
* Copyright 2018-2023 INRIA
33
*/
44

55
#include "eigenpy/numpy-type.hpp"
@@ -17,70 +17,24 @@ bp::object NumpyType::make(PyArrayObject* pyArray, bool copy) {
1717
return make((PyObject*)pyArray, copy);
1818
}
1919

20-
bp::object NumpyType::make(PyObject* pyObj, bool copy) {
20+
bp::object NumpyType::make(PyObject* pyObj, bool /*copy*/) {
2121
bp::object m;
22-
if (isMatrix())
23-
m = getInstance().NumpyMatrixObject(bp::object(bp::handle<>(pyObj)),
24-
bp::object(), copy);
25-
// m = NumpyAsMatrixObject(bp::object(bp::handle<>(pyObj)));
26-
else if (isArray())
27-
m = bp::object(bp::handle<>(pyObj)); // nothing to do here
22+
m = bp::object(bp::handle<>(pyObj)); // nothing to do here
2823

2924
Py_INCREF(m.ptr());
3025
return m;
3126
}
3227

33-
void NumpyType::setNumpyType(bp::object& obj) {
34-
PyTypeObject* obj_type = PyType_Check(obj.ptr())
35-
? reinterpret_cast<PyTypeObject*>(obj.ptr())
36-
: obj.ptr()->ob_type;
37-
if (PyType_IsSubtype(obj_type, getInstance().NumpyMatrixType))
38-
switchToNumpyMatrix();
39-
else if (PyType_IsSubtype(obj_type, getInstance().NumpyArrayType))
40-
switchToNumpyArray();
41-
}
42-
4328
void NumpyType::sharedMemory(const bool value) {
4429
getInstance().shared_memory = value;
4530
}
4631

4732
bool NumpyType::sharedMemory() { return getInstance().shared_memory; }
4833

49-
void NumpyType::switchToNumpyArray() {
50-
getInstance().CurrentNumpyType = getInstance().NumpyArrayObject;
51-
getInstance().getType() = ARRAY_TYPE;
52-
}
53-
54-
void NumpyType::switchToNumpyMatrix() {
55-
getInstance().CurrentNumpyType = getInstance().NumpyMatrixObject;
56-
getInstance().getType() = MATRIX_TYPE;
57-
}
58-
59-
NP_TYPE& NumpyType::getType() { return getInstance().np_type; }
60-
61-
bp::object NumpyType::getNumpyType() { return getInstance().CurrentNumpyType; }
62-
63-
const PyTypeObject* NumpyType::getNumpyMatrixType() {
64-
return getInstance().NumpyMatrixType;
65-
}
66-
6734
const PyTypeObject* NumpyType::getNumpyArrayType() {
6835
return getInstance().NumpyArrayType;
6936
}
7037

71-
bool NumpyType::isMatrix() {
72-
return PyType_IsSubtype(
73-
reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()),
74-
getInstance().NumpyMatrixType);
75-
}
76-
77-
bool NumpyType::isArray() {
78-
if (getInstance().isMatrix()) return false;
79-
return PyType_IsSubtype(
80-
reinterpret_cast<PyTypeObject*>(getInstance().CurrentNumpyType.ptr()),
81-
getInstance().NumpyArrayType);
82-
}
83-
8438
NumpyType::NumpyType() {
8539
pyModule = bp::import("numpy");
8640

@@ -90,16 +44,8 @@ NumpyType::NumpyType() {
9044
Py_INCREF(pyModule.ptr());
9145
#endif
9246

93-
NumpyMatrixObject = pyModule.attr("matrix");
94-
NumpyMatrixType = reinterpret_cast<PyTypeObject*>(NumpyMatrixObject.ptr());
9547
NumpyArrayObject = pyModule.attr("ndarray");
9648
NumpyArrayType = reinterpret_cast<PyTypeObject*>(NumpyArrayObject.ptr());
97-
// NumpyAsMatrixObject = pyModule.attr("asmatrix");
98-
// NumpyAsMatrixType =
99-
// reinterpret_cast<PyTypeObject*>(NumpyAsMatrixObject.ptr());
100-
101-
CurrentNumpyType = NumpyArrayObject; // default conversion
102-
np_type = ARRAY_TYPE;
10349

10450
shared_memory = true;
10551
}

unittest/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ if(NOT NUMPY_WITH_BROKEN_UFUNC_SUPPORT)
8080
"unittest")
8181
endif()
8282

83-
add_python_unit_test("py-switch" "unittest/python/test_switch.py"
84-
"python;unittest")
85-
set_tests_properties("py-switch" PROPERTIES DEPENDS ${PYWRAP})
86-
8783
add_python_unit_test("py-dimensions" "unittest/python/test_dimensions.py"
8884
"python;unittest")
8985
set_tests_properties("py-dimensions" PROPERTIES DEPENDS ${PYWRAP})

unittest/python/test_complex.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from __future__ import print_function
22

33
import numpy as np
4-
from complex import switchToNumpyArray, real, imag, ascomplex
5-
6-
switchToNumpyArray()
4+
from complex import real, imag, ascomplex
75

86
rows = 10
97
cols = 20

unittest/python/test_dimensions.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
import eigenpy
44

55
quat = eigenpy.Quaternion()
6-
# By default, we convert as numpy.matrix
7-
eigenpy.switchToNumpyMatrix()
8-
coeffs_vector = quat.coeffs()
9-
assert len(coeffs_vector.shape) == 2
106

117
# Switch to numpy.array
12-
eigenpy.switchToNumpyArray()
138
coeffs_vector = quat.coeffs()
149
assert len(coeffs_vector.shape) == 1

unittest/python/test_eigen_solver.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import numpy as np
22
import eigenpy
33

4-
eigenpy.switchToNumpyArray()
5-
64
dim = 100
75
A = np.random.rand(dim, dim)
86

unittest/python/test_self_adjoint_eigen_solver.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import numpy as np
44

5-
eigenpy.switchToNumpyArray()
6-
75
dim = 100
86
A = np.random.rand(dim, dim)
97
A = (A + A.T) * 0.5

0 commit comments

Comments
 (0)