From abc9fae9d1391c89444a20fa2c17e583b9081666 Mon Sep 17 00:00:00 2001 From: ManifoldFR <wilson.jallet@polytechnique.org> Date: Wed, 5 Oct 2022 17:22:30 +0200 Subject: [PATCH] std-vector: try setting elements to zero * does not work properly --- unittest/python/test_std_vector.py | 26 +++++++++++++++++++++++--- unittest/vector.cpp | 17 +++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/unittest/python/test_std_vector.py b/unittest/python/test_std_vector.py index ddc900f8..13c0edc8 100644 --- a/unittest/python/test_std_vector.py +++ b/unittest/python/test_std_vector.py @@ -8,7 +8,7 @@ np.random.seed(0) l1 = [np.random.randn(3), np.random.randn(2)] l2 = eigenpy.StdVec_VectorXd(l1) -l3 = [np.random.randn(2, 2), np.random.randn(1, 2), np.random.randn(2, 6)] +l3 = [np.random.randn(2, 2), np.random.randn(3, 1), np.random.randn(4, 2)] def checkAllValues(li1, li2): @@ -33,11 +33,31 @@ print() l4 = [np.random.randn(3, 3) for _ in range(4)] assert "StdVec_Mat3d" in printVectorOf3x3.__doc__ printVectorOf3x3(l4) +print() l4_copy = copyStdVector(l4) assert isinstance(l4_copy, eigenpy.StdVec_MatrixXd) -print(l4_copy) l4_copy2 = vector.copyStdVec_3x3(l4) assert isinstance(l4_copy2, vector.StdVec_Mat3d) -print(l4_copy2) + + +def checkZero(l): + for x in l: + assert np.allclose(x, 0.), "x = {}".format(x) + + +print("l1:") +vector.setZero(l1) +print(l1) +checkZero(l1) + +print("l2:") +l2_py = l2.tolist() +vector.setZero(l2_py) +print(l2_py) +checkZero(l2_py) + +print("l3:") +vector.setZero(l3) +checkZero(l3) diff --git a/unittest/vector.cpp b/unittest/vector.cpp index 6941b384..97201c68 100644 --- a/unittest/vector.cpp +++ b/unittest/vector.cpp @@ -18,10 +18,19 @@ std::vector<MatType> copy(const std::vector<MatType> &Ms) { return out; } +template<typename MatType> +void setZero(std::vector<MatType> Ms) { + for (std::size_t i = 0; i < Ms.size(); i++) { + Ms[i].setZero(); + } +} + + BOOST_PYTHON_MODULE(vector) { namespace bp = boost::python; + using namespace eigenpy; - eigenpy::enableEigenPy(); + enableEigenPy(); bp::def("printVectorOfMatrix", printVectorOfMatrix<Eigen::VectorXd>); bp::def("printVectorOfMatrix", printVectorOfMatrix<Eigen::MatrixXd>); @@ -29,8 +38,12 @@ BOOST_PYTHON_MODULE(vector) { bp::def("copyStdVector", copy<Eigen::MatrixXd>); bp::def("copyStdVector", copy<Eigen::VectorXd>); - eigenpy::StdVectorPythonVisitor<std::vector<Eigen::Matrix3d>>::expose( + StdVectorPythonVisitor<std::vector<Eigen::Matrix3d>>::expose( "StdVec_Mat3d", "3D matrices."); bp::def("printVectorOf3x3", printVectorOfMatrix<Eigen::Matrix3d>); bp::def("copyStdVec_3x3", copy<Eigen::Matrix3d>, bp::args("mats")); + + typedef Eigen::Ref<Eigen::MatrixXd> MatRef; + StdVectorPythonVisitor<std::vector<MatRef>, true>::expose("StdVec_MatRef"); + bp::def("setZero", setZero<MatRef>, "Sets the coeff in [0,0] to 0."); } -- GitLab