From f15dab935a86f82f423c7619c1977273ff50b3bb Mon Sep 17 00:00:00 2001 From: Justin Carpentier <justin.carpentier@inria.fr> Date: Mon, 24 Aug 2020 19:52:32 +0200 Subject: [PATCH] test: test to-python for Eigen::Ref --- unittest/eigen_ref.cpp | 10 ++++++++++ unittest/python/test_eigen_ref.py | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/unittest/eigen_ref.cpp b/unittest/eigen_ref.cpp index 1e58cfee..267f4a00 100644 --- a/unittest/eigen_ref.cpp +++ b/unittest/eigen_ref.cpp @@ -37,6 +37,14 @@ void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar & value) mat.fill(value); } +template<typename MatType> +Eigen::Ref<MatType> asRef(const int rows, const int cols) +{ + static MatType mat(rows,cols); + std::cout << "mat:\n" << mat << std::endl; + return mat; +} + BOOST_PYTHON_MODULE(eigen_ref) { namespace bp = boost::python; @@ -56,4 +64,6 @@ BOOST_PYTHON_MODULE(eigen_ref) bp::def("fillVec3", fill<Vector3d>); bp::def("fillVec", fill<VectorXd>); bp::def("fill", fill<MatrixXd>); + + bp::def("asRef", asRef<MatrixXd>); } diff --git a/unittest/python/test_eigen_ref.py b/unittest/python/test_eigen_ref.py index fe8cd29e..76721a3f 100644 --- a/unittest/python/test_eigen_ref.py +++ b/unittest/python/test_eigen_ref.py @@ -8,6 +8,16 @@ def test(mat): printMatrix(mat) assert np.array_equal(mat,np.full(mat.shape,1.)) + A_ref = asRef(mat.shape[0],mat.shape[1]) + A_ref.fill(1.) + A_ref2 = asRef(mat.shape[0],mat.shape[1]) + + assert np.array_equal(A_ref,A_ref2) + + A_ref2.fill(0) + assert np.array_equal(A_ref,A_ref2) + + rows = 10 cols = 30 -- GitLab