Skip to content

Commit 06ca384

Browse files
committed
test/ref: test for const Eigen::Ref<const ...
1 parent 3435c37 commit 06ca384

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

unittest/eigen_ref.cpp

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

66
#include <iostream>
77

8+
#include "eigenpy/eigen-from-python.hpp"
89
#include "eigenpy/eigenpy.hpp"
910

1011
using namespace Eigen;
@@ -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_eigen_ref.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@ def test(mat):
1717
A_ref2.fill(0)
1818
assert np.array_equal(A_ref,A_ref2)
1919

20+
ref = asRef(mat)
21+
assert np.all(ref == mat)
22+
23+
const_ref = asConstRef(mat)
24+
assert np.all(const_ref == mat)
2025

2126
rows = 10
2227
cols = 30
2328

24-
mat = np.array(np.zeros((rows,cols)))
29+
mat = np.ones((rows,cols),order='F')
2530

2631
test(mat)

0 commit comments

Comments
 (0)