MCPcopy Create free account
hub / github.com/PX4/eigen / comparisons

Function comparisons

test/array_for_matrix.cpp:83–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

81}
82
83template<typename MatrixType> void comparisons(const MatrixType& m)
84{
85 using std::abs;
86 typedef typename MatrixType::Index Index;
87 typedef typename MatrixType::Scalar Scalar;
88 typedef typename NumTraits<Scalar>::Real RealScalar;
89
90 Index rows = m.rows();
91 Index cols = m.cols();
92
93 Index r = internal::random<Index>(0, rows-1),
94 c = internal::random<Index>(0, cols-1);
95
96 MatrixType m1 = MatrixType::Random(rows, cols),
97 m2 = MatrixType::Random(rows, cols),
98 m3(rows, cols);
99
100 VERIFY(((m1.array() + Scalar(1)) > m1.array()).all());
101 VERIFY(((m1.array() - Scalar(1)) < m1.array()).all());
102 if (rows*cols>1)
103 {
104 m3 = m1;
105 m3(r,c) += 1;
106 VERIFY(! (m1.array() < m3.array()).all() );
107 VERIFY(! (m1.array() > m3.array()).all() );
108 }
109
110 // comparisons to scalar
111 VERIFY( (m1.array() != (m1(r,c)+1) ).any() );
112 VERIFY( (m1.array() > (m1(r,c)-1) ).any() );
113 VERIFY( (m1.array() < (m1(r,c)+1) ).any() );
114 VERIFY( (m1.array() == m1(r,c) ).any() );
115 VERIFY( m1.cwiseEqual(m1(r,c)).any() );
116
117 // test Select
118 VERIFY_IS_APPROX( (m1.array()<m2.array()).select(m1,m2), m1.cwiseMin(m2) );
119 VERIFY_IS_APPROX( (m1.array()>m2.array()).select(m1,m2), m1.cwiseMax(m2) );
120 Scalar mid = (m1.cwiseAbs().minCoeff() + m1.cwiseAbs().maxCoeff())/Scalar(2);
121 for (int j=0; j<cols; ++j)
122 for (int i=0; i<rows; ++i)
123 m3(i,j) = abs(m1(i,j))<mid ? 0 : m1(i,j);
124 VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array())
125 .select(MatrixType::Zero(rows,cols),m1), m3);
126 // shorter versions:
127 VERIFY_IS_APPROX( (m1.array().abs()<MatrixType::Constant(rows,cols,mid).array())
128 .select(0,m1), m3);
129 VERIFY_IS_APPROX( (m1.array().abs()>=MatrixType::Constant(rows,cols,mid).array())
130 .select(m1,0), m3);
131 // even shorter version:
132 VERIFY_IS_APPROX( (m1.array().abs()<mid).select(0,m1), m3);
133
134 // count
135 VERIFY(((m1.array().abs()+1)>RealScalar(0.1)).count() == rows*cols);
136
137 // and/or
138 VERIFY( ((m1.array()<RealScalar(0)).matrix() && (m1.array()>RealScalar(0)).matrix()).count() == 0);
139 VERIFY( ((m1.array()<RealScalar(0)).matrix() || (m1.array()>=RealScalar(0)).matrix()).count() == rows*cols);
140 RealScalar a = m1.cwiseAbs().mean();

Callers 1

test_array_for_matrixFunction · 0.70

Calls 15

selectMethod · 0.80
cwiseMinMethod · 0.80
cwiseMaxMethod · 0.80
absMethod · 0.80
colwiseMethod · 0.80
rowwiseMethod · 0.80
absFunction · 0.50
rowsMethod · 0.45
colsMethod · 0.45
allMethod · 0.45
arrayMethod · 0.45
anyMethod · 0.45

Tested by 1

test_array_for_matrixFunction · 0.56