Skip to content

Commit c2aba98

Browse files
authored
Merge pull request #275 from jcarpent/devel
Expose const Eigen::Ref<const ... + update pre-commit + correctly export the project version
2 parents 80b1fd7 + e368506 commit c2aba98

20 files changed

+359
-284
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ repos:
33
rev: v13.0.1
44
hooks:
55
- id: clang-format
6-
args: [-i, --style=Google]
6+
args: ['-i', '--style={BasedOnStyle: Google, SortIncludes: false}']
77
- repo: https://github.com/pre-commit/pre-commit-hooks
88
rev: v4.1.0
99
hooks:
1010
- id: trailing-whitespace
11+
- repo: https://github.com/psf/black
12+
rev: 22.3.0
13+
hooks:
14+
- id: black

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
22
# Copyright (c) 2014-2019 CNRS
3-
# Copyright (c) 2018-2021 INRIA
3+
# Copyright (c) 2018-2022 INRIA
44
#
55

66
CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
@@ -11,6 +11,7 @@ SET(PROJECT_URL "http://github.com/stack-of-tasks/eigenpy")
1111
SET(PROJECT_USE_CMAKE_EXPORT TRUE)
1212
SET(PROJECT_USE_KEYWORD_LINK_LIBRARIES TRUE)
1313
SET(PROJECT_CUSTOM_HEADER_EXTENSION "hpp")
14+
SET(PROJECT_COMPATIBILITY_VERSION AnyNewerVersion)
1415

1516
# Check if the submodule cmake have been initialized
1617
set(JRL_CMAKE_MODULES "${CMAKE_CURRENT_LIST_DIR}/cmake")

benchmarks/bench-switch.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
import timeit
88

99
from IPython import get_ipython
10+
1011
ipython = get_ipython()
1112

1213
quat = eigenpy.Quaternion()
13-
a = [0., 0., 0.]
14+
a = [0.0, 0.0, 0.0]
1415

1516
cmd1 = "timeit np.array(a)"
1617
print("\n")
@@ -50,7 +51,7 @@
5051
ipython.magic(cmd5)
5152
print("\n")
5253

53-
a_matrix = np.matrix(a);
54+
a_matrix = np.matrix(a)
5455
cmd6 = "timeit np.asarray(a_matrix)"
5556
print(cmd6)
5657
ipython.magic(cmd6)

include/eigenpy/details.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ void enableEigenPySpecific() {
6868
EigenToPyConverter<MatType>::registration();
6969
#if EIGEN_VERSION_AT_LEAST(3, 2, 0)
7070
EigenToPyConverter<Eigen::Ref<MatType> >::registration();
71+
EigenToPyConverter<const Eigen::Ref<const MatType> >::registration();
7172
#endif
7273

7374
// from-python

include/eigenpy/numpy-allocator.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 INRIA
2+
* Copyright 2020-2022 INRIA
33
*/
44

55
#ifndef __eigenpy_numpy_allocator_hpp__
@@ -116,13 +116,11 @@ template <typename MatType, int Options, typename Stride>
116116
struct NumpyAllocator<const Eigen::Ref<const MatType, Options, Stride> > {
117117
typedef const Eigen::Ref<const MatType, Options, Stride> RefType;
118118

119-
template <typename SimilarMatrixType>
120119
static PyArrayObject *allocate(RefType &mat, npy_intp nd, npy_intp *shape) {
121-
typedef typename SimilarMatrixType::Scalar Scalar;
120+
typedef typename RefType::Scalar Scalar;
122121
enum {
123-
NPY_ARRAY_MEMORY_CONTIGUOUS_RO = SimilarMatrixType::IsRowMajor
124-
? NPY_ARRAY_CARRAY_RO
125-
: NPY_ARRAY_FARRAY_RO
122+
NPY_ARRAY_MEMORY_CONTIGUOUS_RO =
123+
RefType::IsRowMajor ? NPY_ARRAY_CARRAY_RO : NPY_ARRAY_FARRAY_RO
126124
};
127125

128126
if (NumpyType::sharedMemory()) {

unittest/eigen_ref.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
22
* Copyright 2014-2019, CNRS
3-
* Copyright 2018-2020, INRIA
3+
* Copyright 2018-2022, INRIA
44
*/
55

66
#include <iostream>
77

88
#include "eigenpy/eigenpy.hpp"
9+
#include "eigenpy/eigen-from-python.hpp"
910

1011
using namespace Eigen;
1112
using namespace eigenpy;
@@ -41,6 +42,16 @@ Eigen::Ref<MatType> asRef(const int rows, const int cols) {
4142
return mat;
4243
}
4344

45+
template <typename MatType>
46+
Eigen::Ref<MatType> asRef(Eigen::Ref<MatType> mat) {
47+
return Eigen::Ref<MatType>(mat);
48+
}
49+
50+
template <typename MatType>
51+
const Eigen::Ref<const MatType> asConstRef(Eigen::Ref<MatType> mat) {
52+
return Eigen::Ref<const MatType>(mat);
53+
}
54+
4455
BOOST_PYTHON_MODULE(eigen_ref) {
4556
namespace bp = boost::python;
4657
eigenpy::enableEigenPy();
@@ -60,5 +71,10 @@ BOOST_PYTHON_MODULE(eigen_ref) {
6071
bp::def("fillVec", fill<VectorXd>);
6172
bp::def("fill", fill<MatrixXd>);
6273

63-
bp::def("asRef", asRef<MatrixXd>);
74+
bp::def("asRef",
75+
(Eigen::Ref<MatrixXd>(*)(const int, const int))asRef<MatrixXd>);
76+
bp::def("asRef",
77+
(Eigen::Ref<MatrixXd>(*)(Eigen::Ref<MatrixXd>))asRef<MatrixXd>);
78+
bp::def("asConstRef", (const Eigen::Ref<const MatrixXd> (*)(
79+
Eigen::Ref<MatrixXd>))asConstRef<MatrixXd>);
6480
}

unittest/python/test_LDLT.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44
import numpy.linalg as la
55

66
dim = 100
7-
A = np.random.rand(dim,dim)
7+
A = np.random.rand(dim, dim)
88

9-
A = (A + A.T)*0.5 + np.diag(10. + np.random.rand(dim))
9+
A = (A + A.T) * 0.5 + np.diag(10.0 + np.random.rand(dim))
1010

1111
ldlt = eigenpy.LDLT(A)
1212

1313
L = ldlt.matrixL()
1414
D = ldlt.vectorD()
1515
P = ldlt.transpositionsP()
1616

17-
assert eigenpy.is_approx(np.transpose(P).dot(L.dot(np.diag(D).dot(np.transpose(L).dot(P)))),A)
17+
assert eigenpy.is_approx(
18+
np.transpose(P).dot(L.dot(np.diag(D).dot(np.transpose(L).dot(P)))), A
19+
)
1820

19-
X = np.random.rand(dim,20)
21+
X = np.random.rand(dim, 20)
2022
B = A.dot(X)
2123
X_est = ldlt.solve(B)
22-
assert eigenpy.is_approx(X,X_est)
23-
assert eigenpy.is_approx(A.dot(X_est),B)
24+
assert eigenpy.is_approx(X, X_est)
25+
assert eigenpy.is_approx(A.dot(X_est), B)

unittest/python/test_LLT.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
import numpy.linalg as la
55

66
dim = 100
7-
A = np.random.rand(dim,dim)
7+
A = np.random.rand(dim, dim)
88

9-
A = (A + A.T)*0.5 + np.diag(10. + np.random.rand(dim))
9+
A = (A + A.T) * 0.5 + np.diag(10.0 + np.random.rand(dim))
1010

1111
llt = eigenpy.LLT(A)
1212

1313
L = llt.matrixL()
14-
assert eigenpy.is_approx(L.dot(np.transpose(L)),A)
14+
assert eigenpy.is_approx(L.dot(np.transpose(L)), A)
1515

16-
X = np.random.rand(dim,20)
16+
X = np.random.rand(dim, 20)
1717
B = A.dot(X)
1818
X_est = llt.solve(B)
19-
assert eigenpy.is_approx(X,X_est)
20-
assert eigenpy.is_approx(A.dot(X_est),B)
19+
assert eigenpy.is_approx(X, X_est)
20+
assert eigenpy.is_approx(A.dot(X_est), B)

unittest/python/test_MINRES.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
minres = eigenpy.MINRES(A)
1010

11-
X = np.random.rand(dim,20)
11+
X = np.random.rand(dim, 20)
1212
B = A.dot(X)
1313
X_est = minres.solve(B)
14-
print("A.dot(X_est):",A.dot(X_est))
15-
print("B:",B)
16-
assert eigenpy.is_approx(A.dot(X_est),B,1e-6)
14+
print("A.dot(X_est):", A.dot(X_est))
15+
print("B:", B)
16+
assert eigenpy.is_approx(A.dot(X_est), B, 1e-6)

0 commit comments

Comments
 (0)