visitor.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 
12 template<typename MatrixType> void matrixVisitor(const MatrixType& p)
13 {
14  typedef typename MatrixType::Scalar Scalar;
15 
16  Index rows = p.rows();
17  Index cols = p.cols();
18 
19  // construct a random matrix where all coefficients are different
20  MatrixType m;
21  m = MatrixType::Random(rows, cols);
22  for(Index i = 0; i < m.size(); i++)
23  for(Index i2 = 0; i2 < i; i2++)
24  while(m(i) == m(i2)) // yes, ==
25  m(i) = internal::random<Scalar>();
26 
27  Scalar minc = Scalar(1000), maxc = Scalar(-1000);
28  Index minrow=0,mincol=0,maxrow=0,maxcol=0;
29  for(Index j = 0; j < cols; j++)
30  for(Index i = 0; i < rows; i++)
31  {
32  if(m(i,j) < minc)
33  {
34  minc = m(i,j);
35  minrow = i;
36  mincol = j;
37  }
38  if(m(i,j) > maxc)
39  {
40  maxc = m(i,j);
41  maxrow = i;
42  maxcol = j;
43  }
44  }
45  Index eigen_minrow, eigen_mincol, eigen_maxrow, eigen_maxcol;
46  Scalar eigen_minc, eigen_maxc;
47  eigen_minc = m.minCoeff(&eigen_minrow,&eigen_mincol);
48  eigen_maxc = m.maxCoeff(&eigen_maxrow,&eigen_maxcol);
49  VERIFY(minrow == eigen_minrow);
50  VERIFY(maxrow == eigen_maxrow);
51  VERIFY(mincol == eigen_mincol);
52  VERIFY(maxcol == eigen_maxcol);
53  VERIFY_IS_APPROX(minc, eigen_minc);
54  VERIFY_IS_APPROX(maxc, eigen_maxc);
55  VERIFY_IS_APPROX(minc, m.minCoeff());
56  VERIFY_IS_APPROX(maxc, m.maxCoeff());
57 
58  eigen_maxc = (m.adjoint()*m).maxCoeff(&eigen_maxrow,&eigen_maxcol);
59  Index maxrow2=0,maxcol2=0;
60  eigen_maxc = (m.adjoint()*m).eval().maxCoeff(&maxrow2,&maxcol2);
61  VERIFY(maxrow2 == eigen_maxrow);
62  VERIFY(maxcol2 == eigen_maxcol);
63 
64  if (!NumTraits<Scalar>::IsInteger && m.size() > 2) {
65  // Test NaN propagation by replacing an element with NaN.
66  bool stop = false;
67  for (Index j = 0; j < cols && !stop; ++j) {
68  for (Index i = 0; i < rows && !stop; ++i) {
69  if (!(j == mincol && i == minrow) &&
70  !(j == maxcol && i == maxrow)) {
72  stop = true;
73  break;
74  }
75  }
76  }
77 
78  eigen_minc = m.template minCoeff<PropagateNumbers>(&eigen_minrow, &eigen_mincol);
79  eigen_maxc = m.template maxCoeff<PropagateNumbers>(&eigen_maxrow, &eigen_maxcol);
80  VERIFY(minrow == eigen_minrow);
81  VERIFY(maxrow == eigen_maxrow);
82  VERIFY(mincol == eigen_mincol);
83  VERIFY(maxcol == eigen_maxcol);
84  VERIFY_IS_APPROX(minc, eigen_minc);
85  VERIFY_IS_APPROX(maxc, eigen_maxc);
86  VERIFY_IS_APPROX(minc, m.template minCoeff<PropagateNumbers>());
87  VERIFY_IS_APPROX(maxc, m.template maxCoeff<PropagateNumbers>());
88 
89  eigen_minc = m.template minCoeff<PropagateNaN>(&eigen_minrow, &eigen_mincol);
90  eigen_maxc = m.template maxCoeff<PropagateNaN>(&eigen_maxrow, &eigen_maxcol);
91  VERIFY(minrow != eigen_minrow || mincol != eigen_mincol);
92  VERIFY(maxrow != eigen_maxrow || maxcol != eigen_maxcol);
93  VERIFY((numext::isnan)(eigen_minc));
94  VERIFY((numext::isnan)(eigen_maxc));
95  }
96 
97 }
98 
99 template<typename VectorType> void vectorVisitor(const VectorType& w)
100 {
101  typedef typename VectorType::Scalar Scalar;
102 
103  Index size = w.size();
104 
105  // construct a random vector where all coefficients are different
106  VectorType v;
107  v = VectorType::Random(size);
108  for(Index i = 0; i < size; i++)
109  for(Index i2 = 0; i2 < i; i2++)
110  while(v(i) == v(i2)) // yes, ==
111  v(i) = internal::random<Scalar>();
112 
113  Scalar minc = v(0), maxc = v(0);
114  Index minidx=0, maxidx=0;
115  for(Index i = 0; i < size; i++)
116  {
117  if(v(i) < minc)
118  {
119  minc = v(i);
120  minidx = i;
121  }
122  if(v(i) > maxc)
123  {
124  maxc = v(i);
125  maxidx = i;
126  }
127  }
128  Index eigen_minidx, eigen_maxidx;
129  Scalar eigen_minc, eigen_maxc;
130  eigen_minc = v.minCoeff(&eigen_minidx);
131  eigen_maxc = v.maxCoeff(&eigen_maxidx);
132  VERIFY(minidx == eigen_minidx);
133  VERIFY(maxidx == eigen_maxidx);
134  VERIFY_IS_APPROX(minc, eigen_minc);
135  VERIFY_IS_APPROX(maxc, eigen_maxc);
136  VERIFY_IS_APPROX(minc, v.minCoeff());
137  VERIFY_IS_APPROX(maxc, v.maxCoeff());
138 
139  Index idx0 = internal::random<Index>(0,size-1);
140  Index idx1 = eigen_minidx;
141  Index idx2 = eigen_maxidx;
142  VectorType v1(v), v2(v);
143  v1(idx0) = v1(idx1);
144  v2(idx0) = v2(idx2);
145  v1.minCoeff(&eigen_minidx);
146  v2.maxCoeff(&eigen_maxidx);
147  VERIFY(eigen_minidx == (std::min)(idx0,idx1));
148  VERIFY(eigen_maxidx == (std::min)(idx0,idx2));
149 
150  if (!NumTraits<Scalar>::IsInteger && size > 2) {
151  // Test NaN propagation by replacing an element with NaN.
152  for (Index i = 0; i < size; ++i) {
153  if (i != minidx && i != maxidx) {
155  break;
156  }
157  }
158  eigen_minc = v.template minCoeff<PropagateNumbers>(&eigen_minidx);
159  eigen_maxc = v.template maxCoeff<PropagateNumbers>(&eigen_maxidx);
160  VERIFY(minidx == eigen_minidx);
161  VERIFY(maxidx == eigen_maxidx);
162  VERIFY_IS_APPROX(minc, eigen_minc);
163  VERIFY_IS_APPROX(maxc, eigen_maxc);
164  VERIFY_IS_APPROX(minc, v.template minCoeff<PropagateNumbers>());
165  VERIFY_IS_APPROX(maxc, v.template maxCoeff<PropagateNumbers>());
166 
167  eigen_minc = v.template minCoeff<PropagateNaN>(&eigen_minidx);
168  eigen_maxc = v.template maxCoeff<PropagateNaN>(&eigen_maxidx);
169  VERIFY(minidx != eigen_minidx);
170  VERIFY(maxidx != eigen_maxidx);
171  VERIFY((numext::isnan)(eigen_minc));
172  VERIFY((numext::isnan)(eigen_maxc));
173  }
174 }
175 
177 {
178  for(int i = 0; i < g_repeat; i++) {
180  CALL_SUBTEST_2( matrixVisitor(Matrix2f()) );
181  CALL_SUBTEST_3( matrixVisitor(Matrix4d()) );
182  CALL_SUBTEST_4( matrixVisitor(MatrixXd(8, 12)) );
184  CALL_SUBTEST_6( matrixVisitor(MatrixXi(8, 12)) );
185  }
186  for(int i = 0; i < g_repeat; i++) {
187  CALL_SUBTEST_7( vectorVisitor(Vector4f()) );
189  CALL_SUBTEST_8( vectorVisitor(VectorXd(10)) );
190  CALL_SUBTEST_9( vectorVisitor(RowVectorXd(10)) );
191  CALL_SUBTEST_10( vectorVisitor(VectorXf(33)) );
192  }
193 }
w
RowVector3d w
Definition: Matrix_resize_int.cpp:3
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
isnan
#define isnan(X)
Definition: main.h:93
CALL_SUBTEST_9
#define CALL_SUBTEST_9(FUNC)
Definition: split_test_helper.h:52
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
matrixVisitor
void matrixVisitor(const MatrixType &p)
Definition: visitor.cpp:12
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
vectorVisitor
void vectorVisitor(const VectorType &w)
Definition: visitor.cpp:99
CALL_SUBTEST_10
#define CALL_SUBTEST_10(FUNC)
Definition: split_test_helper.h:58
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
CALL_SUBTEST_6
#define CALL_SUBTEST_6(FUNC)
Definition: split_test_helper.h:34
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(visitor)
Definition: visitor.cpp:176
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
main.h
p
float * p
Definition: Tutorial_Map_using.cpp:9
v2
Vector v2
Definition: testSerializationBase.cpp:39
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
min
#define min(a, b)
Definition: datatypes.h:19
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
VectorType
Definition: FFTW.cpp:65
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
CALL_SUBTEST_7
#define CALL_SUBTEST_7(FUNC)
Definition: split_test_helper.h:40
CALL_SUBTEST_8
#define CALL_SUBTEST_8(FUNC)
Definition: split_test_helper.h:46
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
eval
internal::nested_eval< T, 1 >::type eval(const T &xpr)
Definition: sparse_permutations.cpp:38
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
v1
Vector v1
Definition: testSerializationBase.cpp:38
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:18:25