diff --git a/include/eigenpy/numpy-map.hpp b/include/eigenpy/numpy-map.hpp index ed8d846297ab5781c361355aca2bb28382f461dd..8aba0944a9dae5b400c1a303dcaa97a1e536d708 100644 --- a/include/eigenpy/numpy-map.hpp +++ b/include/eigenpy/numpy-map.hpp @@ -82,16 +82,32 @@ namespace eigenpy rows = (int)PyArray_DIMS(pyArray)[0]; cols = 1; - inner_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize; - outer_stride = 0; + if(EquivalentInputMatrixType::IsRowMajor) + { + outer_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize; + inner_stride = 0; + } + else + { + inner_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize; + outer_stride = 0; + } } else { rows = 1; cols = (int)PyArray_DIMS(pyArray)[0]; - inner_stride = 0; - outer_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize; + if(EquivalentInputMatrixType::IsRowMajor) + { + inner_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize; + outer_stride = 0; + } + else + { + inner_stride = 0; + outer_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize; + } } } diff --git a/unittest/matrix.cpp b/unittest/matrix.cpp index fd4ebae04b87b10d864e27bd0a3cc5e86bdb2763..2982ee81fe132d90d592fe6f2fab1867d6b32a55 100644 --- a/unittest/matrix.cpp +++ b/unittest/matrix.cpp @@ -1,6 +1,5 @@ /* - * Copyright 2014-2019, CNRS - * Copyright 2018-2020, INRIA + * Copyright 2014-2022 CNRS INRIA */ #include "eigenpy/eigenpy.hpp" @@ -94,6 +93,61 @@ Eigen::Matrix<Scalar,6,6> matrix6(const Scalar & value) return ReturnType::Constant(value); } +template<typename Scalar> +Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> +generateRowMajorMatrix(const Eigen::DenseIndex rows, + const Eigen::DenseIndex cols) +{ + typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> RowMajorMatrix; + RowMajorMatrix A(rows, cols); + typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> Vector; + Eigen::Map<Vector>(A.data(),A.size()) = Vector::LinSpaced(A.size(),1,A.size()); + std::cout << "Matrix values:\n" << A << std::endl; + return A; +} + +template<typename Scalar> +Eigen::Matrix<Scalar,1,Eigen::Dynamic,Eigen::RowMajor> +generateRowMajorVector(const Eigen::DenseIndex size) +{ + typedef Eigen::Matrix<Scalar,1,Eigen::Dynamic,Eigen::RowMajor> RowMajorVector; + RowMajorVector A(size); + A.setLinSpaced(size,1,size); + std::cout << "Vector values: " << A.transpose() << std::endl; + return A; +} + +template<typename Scalar> +Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> +generateColMajorMatrix(const Eigen::DenseIndex rows, + const Eigen::DenseIndex cols) +{ + typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> ColMajorMatrix; + ColMajorMatrix A(rows, cols); + typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> Vector; + Eigen::Map<Vector>(A.data(),A.size()) = Vector::LinSpaced(A.size(),1,A.size()); + std::cout << "Matrix values:\n" << A << std::endl; + return A; +} + +template<typename Scalar> +Eigen::Matrix<Scalar,1,Eigen::Dynamic> +generateColMajorVector(const Eigen::DenseIndex size) +{ + typedef Eigen::Matrix<Scalar,1,Eigen::Dynamic> ColMajorVector; + ColMajorVector A(size); + A.setLinSpaced(size,1,size); + std::cout << "Vector values: " << A.transpose() << std::endl; + return A; +} + +template<typename Matrix, typename ReturnMatrix> +ReturnMatrix +copy(const Eigen::MatrixBase<Matrix> & mat) +{ + return mat; +} + BOOST_PYTHON_MODULE(matrix) { using namespace Eigen; @@ -134,4 +188,16 @@ BOOST_PYTHON_MODULE(matrix) bp::def("plain", plain<MatrixXd>); bp::def("matrix6", matrix6<double>); + + bp::def("generateRowMajorMatrix", generateRowMajorMatrix<double>); + bp::def("generateRowMajorVector", generateRowMajorVector<double>); + + bp::def("generateColMajorMatrix", generateColMajorMatrix<double>); + bp::def("generateColMajorVector", generateColMajorVector<double>); + + typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> RowMajorMatrixXd; + bp::def("asRowMajorFromColMajorMatrix", copy<Eigen::MatrixXd,RowMajorMatrixXd>); + bp::def("asRowMajorFromColMajorVector", copy<Eigen::VectorXd,Eigen::RowVectorXd>); + bp::def("asRowMajorFromRowMajorMatrix", copy<RowMajorMatrixXd,RowMajorMatrixXd>); + bp::def("asRowMajorFromRowMajorVector", copy<Eigen::RowVectorXd,Eigen::RowVectorXd>); } diff --git a/unittest/python/test_matrix.py b/unittest/python/test_matrix.py index 1b1ea6e739147df959056af916bee23827f11da3..33b4c022b07e5099c26cbf4a564ceaa6f1d18673 100644 --- a/unittest/python/test_matrix.py +++ b/unittest/python/test_matrix.py @@ -134,3 +134,15 @@ assert(vec1x1[0] == value) # test registration of matrix6 mat6 = eigenpy.matrix6(0.) assert(mat6.size == 36) + +# test RowMajor + +mat = np.arange(0,10).reshape(2,5) +assert((eigenpy.asRowMajorFromColMajorMatrix(mat) == mat).all()) +assert((eigenpy.asRowMajorFromRowMajorMatrix(mat) == mat).all()) + +vec = np.arange(0,10) +assert((eigenpy.asRowMajorFromColMajorMatrix(vec) == vec).all()) +assert((eigenpy.asRowMajorFromColMajorVector(vec) == vec).all()) +assert((eigenpy.asRowMajorFromRowMajorMatrix(vec) == vec).all()) +assert((eigenpy.asRowMajorFromRowMajorVector(vec) == vec).all())