Skip to content

Commit 20e7ab8

Browse files
authored
Merge pull request #266 from jcarpent/devel
Fix RowMajor issues
2 parents fa69988 + abea6ab commit 20e7ab8

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

include/eigenpy/numpy-map.hpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,32 @@ namespace eigenpy
8282
rows = (int)PyArray_DIMS(pyArray)[0];
8383
cols = 1;
8484

85-
inner_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
86-
outer_stride = 0;
85+
if(EquivalentInputMatrixType::IsRowMajor)
86+
{
87+
outer_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
88+
inner_stride = 0;
89+
}
90+
else
91+
{
92+
inner_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
93+
outer_stride = 0;
94+
}
8795
}
8896
else
8997
{
9098
rows = 1;
9199
cols = (int)PyArray_DIMS(pyArray)[0];
92100

93-
inner_stride = 0;
94-
outer_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
101+
if(EquivalentInputMatrixType::IsRowMajor)
102+
{
103+
inner_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
104+
outer_stride = 0;
105+
}
106+
else
107+
{
108+
inner_stride = 0;
109+
outer_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
110+
}
95111
}
96112
}
97113

unittest/matrix.cpp

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
* Copyright 2014-2019, CNRS
3-
* Copyright 2018-2020, INRIA
2+
* Copyright 2014-2022 CNRS INRIA
43
*/
54

65
#include "eigenpy/eigenpy.hpp"
@@ -94,6 +93,61 @@ Eigen::Matrix<Scalar,6,6> matrix6(const Scalar & value)
9493
return ReturnType::Constant(value);
9594
}
9695

96+
template<typename Scalar>
97+
Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor>
98+
generateRowMajorMatrix(const Eigen::DenseIndex rows,
99+
const Eigen::DenseIndex cols)
100+
{
101+
typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> RowMajorMatrix;
102+
RowMajorMatrix A(rows, cols);
103+
typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> Vector;
104+
Eigen::Map<Vector>(A.data(),A.size()) = Vector::LinSpaced(A.size(),1,A.size());
105+
std::cout << "Matrix values:\n" << A << std::endl;
106+
return A;
107+
}
108+
109+
template<typename Scalar>
110+
Eigen::Matrix<Scalar,1,Eigen::Dynamic,Eigen::RowMajor>
111+
generateRowMajorVector(const Eigen::DenseIndex size)
112+
{
113+
typedef Eigen::Matrix<Scalar,1,Eigen::Dynamic,Eigen::RowMajor> RowMajorVector;
114+
RowMajorVector A(size);
115+
A.setLinSpaced(size,1,size);
116+
std::cout << "Vector values: " << A.transpose() << std::endl;
117+
return A;
118+
}
119+
120+
template<typename Scalar>
121+
Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>
122+
generateColMajorMatrix(const Eigen::DenseIndex rows,
123+
const Eigen::DenseIndex cols)
124+
{
125+
typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> ColMajorMatrix;
126+
ColMajorMatrix A(rows, cols);
127+
typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> Vector;
128+
Eigen::Map<Vector>(A.data(),A.size()) = Vector::LinSpaced(A.size(),1,A.size());
129+
std::cout << "Matrix values:\n" << A << std::endl;
130+
return A;
131+
}
132+
133+
template<typename Scalar>
134+
Eigen::Matrix<Scalar,1,Eigen::Dynamic>
135+
generateColMajorVector(const Eigen::DenseIndex size)
136+
{
137+
typedef Eigen::Matrix<Scalar,1,Eigen::Dynamic> ColMajorVector;
138+
ColMajorVector A(size);
139+
A.setLinSpaced(size,1,size);
140+
std::cout << "Vector values: " << A.transpose() << std::endl;
141+
return A;
142+
}
143+
144+
template<typename Matrix, typename ReturnMatrix>
145+
ReturnMatrix
146+
copy(const Eigen::MatrixBase<Matrix> & mat)
147+
{
148+
return mat;
149+
}
150+
97151
BOOST_PYTHON_MODULE(matrix)
98152
{
99153
using namespace Eigen;
@@ -134,4 +188,16 @@ BOOST_PYTHON_MODULE(matrix)
134188
bp::def("plain", plain<MatrixXd>);
135189

136190
bp::def("matrix6", matrix6<double>);
191+
192+
bp::def("generateRowMajorMatrix", generateRowMajorMatrix<double>);
193+
bp::def("generateRowMajorVector", generateRowMajorVector<double>);
194+
195+
bp::def("generateColMajorMatrix", generateColMajorMatrix<double>);
196+
bp::def("generateColMajorVector", generateColMajorVector<double>);
197+
198+
typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> RowMajorMatrixXd;
199+
bp::def("asRowMajorFromColMajorMatrix", copy<Eigen::MatrixXd,RowMajorMatrixXd>);
200+
bp::def("asRowMajorFromColMajorVector", copy<Eigen::VectorXd,Eigen::RowVectorXd>);
201+
bp::def("asRowMajorFromRowMajorMatrix", copy<RowMajorMatrixXd,RowMajorMatrixXd>);
202+
bp::def("asRowMajorFromRowMajorVector", copy<Eigen::RowVectorXd,Eigen::RowVectorXd>);
137203
}

unittest/python/test_matrix.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,15 @@
134134
# test registration of matrix6
135135
mat6 = eigenpy.matrix6(0.)
136136
assert(mat6.size == 36)
137+
138+
# test RowMajor
139+
140+
mat = np.arange(0,10).reshape(2,5)
141+
assert((eigenpy.asRowMajorFromColMajorMatrix(mat) == mat).all())
142+
assert((eigenpy.asRowMajorFromRowMajorMatrix(mat) == mat).all())
143+
144+
vec = np.arange(0,10)
145+
assert((eigenpy.asRowMajorFromColMajorMatrix(vec) == vec).all())
146+
assert((eigenpy.asRowMajorFromColMajorVector(vec) == vec).all())
147+
assert((eigenpy.asRowMajorFromRowMajorMatrix(vec) == vec).all())
148+
assert((eigenpy.asRowMajorFromRowMajorVector(vec) == vec).all())

0 commit comments

Comments
 (0)