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  eigen_maxc = (m.adjoint()*m).eval().maxCoeff(&maxrow,&maxcol);
60  VERIFY(maxrow == eigen_maxrow);
61  VERIFY(maxcol == eigen_maxcol);
62 }
63 
64 template<typename VectorType> void vectorVisitor(const VectorType& w)
65 {
66  typedef typename VectorType::Scalar Scalar;
67 
68  Index size = w.size();
69 
70  // construct a random vector where all coefficients are different
71  VectorType v;
72  v = VectorType::Random(size);
73  for(Index i = 0; i < size; i++)
74  for(Index i2 = 0; i2 < i; i2++)
75  while(v(i) == v(i2)) // yes, ==
76  v(i) = internal::random<Scalar>();
77 
78  Scalar minc = v(0), maxc = v(0);
79  Index minidx=0, maxidx=0;
80  for(Index i = 0; i < size; i++)
81  {
82  if(v(i) < minc)
83  {
84  minc = v(i);
85  minidx = i;
86  }
87  if(v(i) > maxc)
88  {
89  maxc = v(i);
90  maxidx = i;
91  }
92  }
93  Index eigen_minidx, eigen_maxidx;
94  Scalar eigen_minc, eigen_maxc;
95  eigen_minc = v.minCoeff(&eigen_minidx);
96  eigen_maxc = v.maxCoeff(&eigen_maxidx);
97  VERIFY(minidx == eigen_minidx);
98  VERIFY(maxidx == eigen_maxidx);
99  VERIFY_IS_APPROX(minc, eigen_minc);
100  VERIFY_IS_APPROX(maxc, eigen_maxc);
101  VERIFY_IS_APPROX(minc, v.minCoeff());
102  VERIFY_IS_APPROX(maxc, v.maxCoeff());
103 
104  Index idx0 = internal::random<Index>(0,size-1);
105  Index idx1 = eigen_minidx;
106  Index idx2 = eigen_maxidx;
107  VectorType v1(v), v2(v);
108  v1(idx0) = v1(idx1);
109  v2(idx0) = v2(idx2);
110  v1.minCoeff(&eigen_minidx);
111  v2.maxCoeff(&eigen_maxidx);
112  VERIFY(eigen_minidx == (std::min)(idx0,idx1));
113  VERIFY(eigen_maxidx == (std::min)(idx0,idx2));
114 }
115 
117 {
118  for(int i = 0; i < g_repeat; i++) {
119  CALL_SUBTEST_1( matrixVisitor(Matrix<float, 1, 1>()) );
120  CALL_SUBTEST_2( matrixVisitor(Matrix2f()) );
121  CALL_SUBTEST_3( matrixVisitor(Matrix4d()) );
122  CALL_SUBTEST_4( matrixVisitor(MatrixXd(8, 12)) );
123  CALL_SUBTEST_5( matrixVisitor(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 20)) );
124  CALL_SUBTEST_6( matrixVisitor(MatrixXi(8, 12)) );
125  }
126  for(int i = 0; i < g_repeat; i++) {
127  CALL_SUBTEST_7( vectorVisitor(Vector4f()) );
128  CALL_SUBTEST_7( vectorVisitor(Matrix<int,12,1>()) );
129  CALL_SUBTEST_8( vectorVisitor(VectorXd(10)) );
130  CALL_SUBTEST_9( vectorVisitor(RowVectorXd(10)) );
131  CALL_SUBTEST_10( vectorVisitor(VectorXf(33)) );
132  }
133 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:33
Vector v2
Vector v1
#define min(a, b)
Definition: datatypes.h:19
void test_visitor()
Definition: visitor.cpp:116
ArrayXcf v
Definition: Cwise_arg.cpp:1
MatrixXf MatrixType
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
#define VERIFY_IS_APPROX(a, b)
void matrixVisitor(const MatrixType &p)
Definition: visitor.cpp:12
static int g_repeat
Definition: main.h:144
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
RowVector3d w
void vectorVisitor(const VectorType &w)
Definition: visitor.cpp:64
#define VERIFY(a)
Definition: main.h:325
float * p
internal::nested_eval< T, 1 >::type eval(const T &xpr)
The matrix class, also used for vectors and row-vectors.
std::ptrdiff_t j


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:25