Skip to content

Commit 95e906b

Browse files
authored
Merge pull request #147 from jcarpent/devel
Add test for matrix 1x1
2 parents ed34f01 + 9c4d4ca commit 95e906b

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

include/eigenpy/details.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ namespace eigenpy
461461

462462
PyArrayObject* pyArray;
463463
// Allocate Python memory
464-
if(C == 1 && NumpyType::getType() == ARRAY_TYPE) // Handle array with a single dimension
464+
if(C == 1 && NumpyType::getType() == ARRAY_TYPE && MatType::IsVectorAtCompileTime) // Handle array with a single dimension
465465
{
466466
npy_intp shape[1] = { R };
467467
pyArray = (PyArrayObject*) PyArray_SimpleNew(1, shape,

include/eigenpy/map.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright 2014-2019, CNRS
3-
* Copyright 2018-2019, INRIA
3+
* Copyright 2018-2020, INRIA
44
*/
55

66
#include "eigenpy/fwd.hpp"
@@ -10,7 +10,7 @@
1010

1111
namespace eigenpy
1212
{
13-
template<typename MatType, typename InputScalar, int IsVector>
13+
template<typename MatType, typename InputScalar, bool IsVector>
1414
struct MapNumpyTraits {};
1515

1616
/* Wrap a numpy::array with an Eigen::Map. No memory copy. */
@@ -33,7 +33,7 @@ namespace eigenpy
3333
namespace eigenpy
3434
{
3535
template<typename MatType, typename InputScalar>
36-
struct MapNumpyTraits<MatType,InputScalar,0>
36+
struct MapNumpyTraits<MatType,InputScalar,false>
3737
{
3838
typedef typename StrideType<MatType>::type Stride;
3939
typedef Eigen::Matrix<InputScalar,MatType::RowsAtCompileTime,MatType::ColsAtCompileTime> EquivalentInputMatrixType;
@@ -43,10 +43,10 @@ namespace eigenpy
4343
{
4444
assert( PyArray_NDIM(pyArray) == 2 );
4545

46-
assert( (PyArray_DIMS(pyArray)[0]<INT_MAX)
47-
&& (PyArray_DIMS(pyArray)[1]<INT_MAX)
48-
&& (PyArray_STRIDE(pyArray, 0)<INT_MAX)
49-
&& (PyArray_STRIDE(pyArray, 1)<INT_MAX) );
46+
assert( (PyArray_DIMS(pyArray)[0] < INT_MAX)
47+
&& (PyArray_DIMS(pyArray)[1] < INT_MAX)
48+
&& (PyArray_STRIDE(pyArray,0) < INT_MAX)
49+
&& (PyArray_STRIDE(pyArray,1) < INT_MAX) );
5050

5151
const int R = (int)PyArray_DIMS(pyArray)[0];
5252
const int C = (int)PyArray_DIMS(pyArray)[1];
@@ -69,7 +69,7 @@ namespace eigenpy
6969
};
7070

7171
template<typename MatType, typename InputScalar>
72-
struct MapNumpyTraits<MatType,InputScalar,1>
72+
struct MapNumpyTraits<MatType,InputScalar,true>
7373
{
7474
typedef typename StrideType<MatType>::type Stride;
7575
typedef Eigen::Matrix<InputScalar,MatType::RowsAtCompileTime,MatType::ColsAtCompileTime> EquivalentInputMatrixType;

unittest/matrix.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
/*
22
* Copyright 2014-2019, CNRS
3-
* Copyright 2018-2019, INRIA
3+
* Copyright 2018-2020, INRIA
44
*/
55

66
#include "eigenpy/eigenpy.hpp"
77
#include <iostream>
88

9+
template<typename Scalar>
10+
Eigen::Matrix<Scalar,Eigen::Dynamic,1> vector1x1(const Scalar & value)
11+
{
12+
typedef Eigen::Matrix<Scalar,Eigen::Dynamic,1> ReturnType;
13+
return ReturnType::Constant(1,value);
14+
}
15+
16+
template<typename Scalar>
17+
Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> matrix1x1(const Scalar & value)
18+
{
19+
typedef Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic> ReturnType;
20+
return ReturnType::Constant(1,1,value);
21+
}
22+
923
Eigen::VectorXd emptyVector()
1024
{
1125
Eigen::VectorXd vec;
@@ -77,6 +91,9 @@ BOOST_PYTHON_MODULE(matrix)
7791
Eigen::VectorXd (*naturalsX)(int,bool) = naturals;
7892
Eigen::Matrix3d (*naturals33)(bool) = naturals;
7993

94+
bp::def("vector1x1", vector1x1<double>);
95+
bp::def("matrix1x1", matrix1x1<double>);
96+
8097
bp::def("naturals", naturalsXX);
8198
bp::def("naturalsX", naturalsX);
8299
bp::def("naturals33", naturals33);

unittest/python/test_matrix.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,12 @@
117117
# TODO
118118
# M = Mref[0:3,1:2]
119119
# assert( np.array_equal(M,eigenpy.reflex3(M,verbose)) );
120+
121+
value = 2.
122+
mat1x1 = eigenpy.matrix1x1(value)
123+
assert(mat1x1.size == 1)
124+
assert(mat1x1[0,0] == value)
125+
126+
vec1x1 = eigenpy.vector1x1(value)
127+
assert(vec1x1.size == 1)
128+
assert(vec1x1[0,0] == value)

0 commit comments

Comments
 (0)