diff --git a/unittest/eigen_ref.cpp b/unittest/eigen_ref.cpp index 78896b9a376ee15228beca98bc7e36ef776d6959..72b62ce3fa8d360c0670f0ce274f440746775d88 100644 --- a/unittest/eigen_ref.cpp +++ b/unittest/eigen_ref.cpp @@ -15,7 +15,7 @@ using namespace eigenpy; template <typename MatType> void printMatrix(const Eigen::Ref<const MatType> mat) { if (MatType::IsVectorAtCompileTime) std::cout << "isVector" << std::endl; - std::cout << "size: cols " << mat.cols() << " rows " << mat.rows() + std::cout << "input size: cols " << mat.cols() << " rows " << mat.rows() << std::endl; std::cout << mat << std::endl; } @@ -61,17 +61,19 @@ void fill(Eigen::Ref<MatType> mat, const typename MatType::Scalar& 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; + std::cout << "create ref to matrix of size (" << rows << "," << cols << ")\n"; return mat; } template <typename MatType> Eigen::Ref<MatType> asRef(Eigen::Ref<MatType> mat) { + std::cout << "create Ref to input mutable Ref\n"; return Eigen::Ref<MatType>(mat); } template <typename MatType> -const Eigen::Ref<const MatType> asConstRef(Eigen::Ref<MatType> mat) { +const Eigen::Ref<const MatType> asConstRef(Eigen::Ref<const MatType> mat) { + std::cout << "create const Ref to input\n"; return Eigen::Ref<const MatType>(mat); } @@ -82,8 +84,8 @@ struct modify_block { virtual void call(Eigen::Ref<MatrixXd> mat) = 0; }; -struct modify_wrap : modify_block, bp::wrapper<modify_block> { - modify_wrap() : modify_block() {} +struct modify_block_wrap : modify_block, bp::wrapper<modify_block> { + modify_block_wrap() : modify_block() {} void call(Eigen::Ref<MatrixXd> mat) { this->get_override("call")(mat); } }; @@ -112,20 +114,20 @@ BOOST_PYTHON_MODULE(eigen_ref) { bp::def("fillVec", fill<VectorXd>); bp::def("fill", fill<MatrixXd>); - bp::def("asRef", - (Eigen::Ref<MatrixXd>(*)(const int, const int))asRef<MatrixXd>); - bp::def("asRef", - (Eigen::Ref<MatrixXd>(*)(Eigen::Ref<MatrixXd>))asRef<MatrixXd>); - bp::def("asConstRef", (const Eigen::Ref<const MatrixXd> (*)( - Eigen::Ref<MatrixXd>))asConstRef<MatrixXd>); + bp::def<Eigen::Ref<MatrixXd> (*)(const int, const int)>("asRef", + asRef<MatrixXd>); + bp::def<Eigen::Ref<MatrixXd> (*)(Eigen::Ref<MatrixXd>)>("asRef", + asRef<MatrixXd>); + bp::def("asConstRef", asConstRef<MatrixXd>); bp::def("getBlock", &getBlock<MatrixXd>); bp::def("editBlock", &editBlock<MatrixXd>); - bp::class_<modify_wrap, boost::noncopyable>("modify_block", bp::init<>()) + bp::class_<modify_block_wrap, boost::noncopyable>("modify_block", + bp::init<>()) .def_readonly("J", &modify_block::J) .def("modify", &modify_block::modify) - .def("call", bp::pure_virtual(&modify_wrap::call)); + .def("call", bp::pure_virtual(&modify_block_wrap::call)); bp::class_<has_ref_member, boost::noncopyable>("has_ref_member", bp::init<>()) .def_readonly("J", &has_ref_member::J) diff --git a/unittest/python/test_eigen_ref.py b/unittest/python/test_eigen_ref.py index 0802643624c8bec22d5da501a0056146206fca8a..30ef286b7c3310d0346102b54faa0ab17ec506f8 100644 --- a/unittest/python/test_eigen_ref.py +++ b/unittest/python/test_eigen_ref.py @@ -11,48 +11,68 @@ from eigen_ref import ( ) -def test(mat): +def test_fill_print(mat): + print("print matrix:") printMatrix(mat) + print("calling fill():") fill(mat, 1.0) + print("print again:") printMatrix(mat) assert np.array_equal(mat, np.full(mat.shape, 1.0)) + +def test_create_ref_to_static(mat): + # create ref to static: + print() + print("[asRef(int, int)]") A_ref = asRef(mat.shape[0], mat.shape[1]) A_ref.fill(1.0) + A_ref[0, 1] = -1.0 + print("make second reference:") A_ref2 = asRef(mat.shape[0], mat.shape[1]) + print(A_ref2) assert np.array_equal(A_ref, A_ref2) A_ref2.fill(0) assert np.array_equal(A_ref, A_ref2) + +def test_create_ref(mat): + # create ref to input: + + print("[asRef(mat)]") ref = asRef(mat) - assert np.all(ref == mat) + assert np.array_equal(ref, mat) + assert not (ref.flags.owndata) + assert ref.flags.writeable + print("[asConstRef]") const_ref = asConstRef(mat) - assert np.all(const_ref == mat) + print(const_ref.flags) + assert np.array_equal(const_ref, mat) + assert not (const_ref.flags.writeable) + assert not (const_ref.flags.owndata) - mat.fill(0.0) + print("fill a slice") + mat[:, :] = 0.0 fill(mat[:3, :2], 1.0) + assert np.array_equal(mat[:3, :2], np.ones((3, 2))) - assert np.all(mat[:3, :2] == np.ones((3, 2))) - - mat.fill(0.0) + mat[:, :] = 0.0 fill(mat[:2, :3], 1.0) + assert np.array_equal(mat[:2, :3], np.ones((2, 3))) - assert np.all(mat[:2, :3] == np.ones((2, 3))) - + print("set mat data to arange()") mat.fill(0.0) mat[:, :] = np.arange(rows * cols).reshape(rows, cols) - printMatrix(mat) mat0 = mat.copy() mat_as_C_order = np.array(mat, order="F") for i, rowsize, colsize in ([0, 3, 2], [1, 1, 2], [0, 3, 1]): print("taking block [{}:{}, {}:{}]".format(i, rowsize + i, 0, colsize)) B = getBlock(mat_as_C_order, i, 0, rowsize, colsize) lhs = mat_as_C_order[i : rowsize + i, :colsize] - print("should be:\n{}\ngot:\n{}".format(lhs, B)) assert np.array_equal(lhs, B.reshape(rowsize, colsize)) B[:] = 1.0 @@ -62,6 +82,7 @@ def test(mat): mat_as_C_order[:, :] = mat0 mat_copy = mat_as_C_order.copy() + print("[editBlock]") editBlock(mat_as_C_order, 0, 0, 3, 2) mat_copy[:3, :2] = np.arange(6).reshape(3, 2) @@ -91,9 +112,23 @@ def test(mat): assert np.array_equal(hasref.J, J_true) -rows = 10 -cols = 30 +def _do_test(mat): + test_fill_print(mat) + test_create_ref_to_static(mat) + test_create_ref(mat) + + +if __name__ == "__main__": + rows = 8 + cols = 10 -mat = np.ones((rows, cols), order="F") + mat = np.ones((rows, cols), order="F") + mat[0, 0] = 0 + mat[1:5, 1:5] = 6 + _do_test(mat) -test(mat) + mat = np.ones((rows, cols)) + mat[2:4, 1:4] = 2 + _do_test(mat) + mat_f = np.asfortranarray(mat) + _do_test(mat_f) diff --git a/unittest/vector.cpp b/unittest/vector.cpp index a0eca638cf2611a9dcb3a72adf0bf38abe01075a..7b2523a4ed88a136cd38aa008debf830f21aedb9 100644 --- a/unittest/vector.cpp +++ b/unittest/vector.cpp @@ -2,6 +2,8 @@ #include <type_traits> #include "eigenpy/eigenpy.hpp" +// include main first +#include "eigenpy/eigen-from-python.hpp" #include "eigenpy/std-vector.hpp" template <typename MatType>