redux.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 // Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
6 //
7 // This Source Code Form is subject to the terms of the Mozilla
8 // Public License v. 2.0. If a copy of the MPL was not distributed
9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 
11 #define TEST_ENABLE_TEMPORARY_TRACKING
12 #define EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD 8
13 // ^^ see bug 1449
14 
15 #include "main.h"
16 
17 template<typename MatrixType> void matrixRedux(const MatrixType& m)
18 {
19  typedef typename MatrixType::Scalar Scalar;
20  typedef typename MatrixType::RealScalar RealScalar;
21 
22  Index rows = m.rows();
23  Index cols = m.cols();
24 
25  MatrixType m1 = MatrixType::Random(rows, cols);
26 
27  // The entries of m1 are uniformly distributed in [0,1], so m1.prod() is very small. This may lead to test
28  // failures if we underflow into denormals. Thus, we scale so that entries are close to 1.
29  MatrixType m1_for_prod = MatrixType::Ones(rows, cols) + RealScalar(0.2) * m1;
30 
31  VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows, cols).sum(), Scalar(1));
32  VERIFY_IS_APPROX(MatrixType::Ones(rows, cols).sum(), Scalar(float(rows*cols))); // the float() here to shut up excessive MSVC warning about int->complex conversion being lossy
33  Scalar s(0), p(1), minc(numext::real(m1.coeff(0))), maxc(numext::real(m1.coeff(0)));
34  for(int j = 0; j < cols; j++)
35  for(int i = 0; i < rows; i++)
36  {
37  s += m1(i,j);
38  p *= m1_for_prod(i,j);
39  minc = (std::min)(numext::real(minc), numext::real(m1(i,j)));
40  maxc = (std::max)(numext::real(maxc), numext::real(m1(i,j)));
41  }
42  const Scalar mean = s/Scalar(RealScalar(rows*cols));
43 
44  VERIFY_IS_APPROX(m1.sum(), s);
45  VERIFY_IS_APPROX(m1.mean(), mean);
46  VERIFY_IS_APPROX(m1_for_prod.prod(), p);
47  VERIFY_IS_APPROX(m1.real().minCoeff(), numext::real(minc));
48  VERIFY_IS_APPROX(m1.real().maxCoeff(), numext::real(maxc));
49 
50  // test slice vectorization assuming assign is ok
51  Index r0 = internal::random<Index>(0,rows-1);
52  Index c0 = internal::random<Index>(0,cols-1);
53  Index r1 = internal::random<Index>(r0+1,rows)-r0;
54  Index c1 = internal::random<Index>(c0+1,cols)-c0;
55  VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).sum(), m1.block(r0,c0,r1,c1).eval().sum());
56  VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).mean(), m1.block(r0,c0,r1,c1).eval().mean());
57  VERIFY_IS_APPROX(m1_for_prod.block(r0,c0,r1,c1).prod(), m1_for_prod.block(r0,c0,r1,c1).eval().prod());
58  VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).real().minCoeff(), m1.block(r0,c0,r1,c1).real().eval().minCoeff());
59  VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).real().maxCoeff(), m1.block(r0,c0,r1,c1).real().eval().maxCoeff());
60 
61  // regression for bug 1090
62  const int R1 = MatrixType::RowsAtCompileTime>=2 ? MatrixType::RowsAtCompileTime/2 : 6;
63  const int C1 = MatrixType::ColsAtCompileTime>=2 ? MatrixType::ColsAtCompileTime/2 : 6;
64  if(R1<=rows-r0 && C1<=cols-c0)
65  {
66  VERIFY_IS_APPROX( (m1.template block<R1,C1>(r0,c0).sum()), m1.block(r0,c0,R1,C1).sum() );
67  }
68 
69  // test empty objects
70  VERIFY_IS_APPROX(m1.block(r0,c0,0,0).sum(), Scalar(0));
71  VERIFY_IS_APPROX(m1.block(r0,c0,0,0).prod(), Scalar(1));
72 
73  // test nesting complex expression
74  VERIFY_EVALUATION_COUNT( (m1.matrix()*m1.matrix().transpose()).sum(), (MatrixType::IsVectorAtCompileTime && MatrixType::SizeAtCompileTime!=1 ? 0 : 1) );
76  m2.setRandom();
77  VERIFY_EVALUATION_COUNT( ((m1.matrix()*m1.matrix().transpose())+m2).sum(),(MatrixType::IsVectorAtCompileTime && MatrixType::SizeAtCompileTime!=1 ? 0 : 1));
78 }
79 
80 template<typename VectorType> void vectorRedux(const VectorType& w)
81 {
82  using std::abs;
83  typedef typename VectorType::Scalar Scalar;
84  typedef typename NumTraits<Scalar>::Real RealScalar;
85  Index size = w.size();
86 
87  VectorType v = VectorType::Random(size);
88  VectorType v_for_prod = VectorType::Ones(size) + Scalar(0.2) * v; // see comment above declaration of m1_for_prod
89 
90  for(int i = 1; i < size; i++)
91  {
92  Scalar s(0), p(1);
93  RealScalar minc(numext::real(v.coeff(0))), maxc(numext::real(v.coeff(0)));
94  for(int j = 0; j < i; j++)
95  {
96  s += v[j];
97  p *= v_for_prod[j];
98  minc = (std::min)(minc, numext::real(v[j]));
99  maxc = (std::max)(maxc, numext::real(v[j]));
100  }
101  VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.head(i).sum()), Scalar(1));
102  VERIFY_IS_APPROX(p, v_for_prod.head(i).prod());
103  VERIFY_IS_APPROX(minc, v.real().head(i).minCoeff());
104  VERIFY_IS_APPROX(maxc, v.real().head(i).maxCoeff());
105  }
106 
107  for(int i = 0; i < size-1; i++)
108  {
109  Scalar s(0), p(1);
110  RealScalar minc(numext::real(v.coeff(i))), maxc(numext::real(v.coeff(i)));
111  for(int j = i; j < size; j++)
112  {
113  s += v[j];
114  p *= v_for_prod[j];
115  minc = (std::min)(minc, numext::real(v[j]));
116  maxc = (std::max)(maxc, numext::real(v[j]));
117  }
118  VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.tail(size-i).sum()), Scalar(1));
119  VERIFY_IS_APPROX(p, v_for_prod.tail(size-i).prod());
120  VERIFY_IS_APPROX(minc, v.real().tail(size-i).minCoeff());
121  VERIFY_IS_APPROX(maxc, v.real().tail(size-i).maxCoeff());
122  }
123 
124  for(int i = 0; i < size/2; i++)
125  {
126  Scalar s(0), p(1);
127  RealScalar minc(numext::real(v.coeff(i))), maxc(numext::real(v.coeff(i)));
128  for(int j = i; j < size-i; j++)
129  {
130  s += v[j];
131  p *= v_for_prod[j];
132  minc = (std::min)(minc, numext::real(v[j]));
133  maxc = (std::max)(maxc, numext::real(v[j]));
134  }
135  VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.segment(i, size-2*i).sum()), Scalar(1));
136  VERIFY_IS_APPROX(p, v_for_prod.segment(i, size-2*i).prod());
137  VERIFY_IS_APPROX(minc, v.real().segment(i, size-2*i).minCoeff());
138  VERIFY_IS_APPROX(maxc, v.real().segment(i, size-2*i).maxCoeff());
139  }
140 
141  // test empty objects
142  VERIFY_IS_APPROX(v.head(0).sum(), Scalar(0));
143  VERIFY_IS_APPROX(v.tail(0).prod(), Scalar(1));
144  VERIFY_RAISES_ASSERT(v.head(0).mean());
145  VERIFY_RAISES_ASSERT(v.head(0).minCoeff());
146  VERIFY_RAISES_ASSERT(v.head(0).maxCoeff());
147 }
148 
150 {
151  // the max size cannot be too large, otherwise reduxion operations obviously generate large errors.
152  int maxsize = (std::min)(100,EIGEN_TEST_MAX_SIZE);
154  for(int i = 0; i < g_repeat; i++) {
155  CALL_SUBTEST_1( matrixRedux(Matrix<float, 1, 1>()) );
156  CALL_SUBTEST_1( matrixRedux(Array<float, 1, 1>()) );
157  CALL_SUBTEST_2( matrixRedux(Matrix2f()) );
158  CALL_SUBTEST_2( matrixRedux(Array2f()) );
159  CALL_SUBTEST_2( matrixRedux(Array22f()) );
160  CALL_SUBTEST_3( matrixRedux(Matrix4d()) );
161  CALL_SUBTEST_3( matrixRedux(Array4d()) );
162  CALL_SUBTEST_3( matrixRedux(Array44d()) );
163  CALL_SUBTEST_4( matrixRedux(MatrixXcf(internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
164  CALL_SUBTEST_4( matrixRedux(ArrayXXcf(internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
165  CALL_SUBTEST_5( matrixRedux(MatrixXd (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
166  CALL_SUBTEST_5( matrixRedux(ArrayXXd (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
167  CALL_SUBTEST_6( matrixRedux(MatrixXi (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
168  CALL_SUBTEST_6( matrixRedux(ArrayXXi (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
169  }
170  for(int i = 0; i < g_repeat; i++) {
171  CALL_SUBTEST_7( vectorRedux(Vector4f()) );
172  CALL_SUBTEST_7( vectorRedux(Array4f()) );
173  CALL_SUBTEST_5( vectorRedux(VectorXd(internal::random<int>(1,maxsize))) );
174  CALL_SUBTEST_5( vectorRedux(ArrayXd(internal::random<int>(1,maxsize))) );
175  CALL_SUBTEST_8( vectorRedux(VectorXf(internal::random<int>(1,maxsize))) );
176  CALL_SUBTEST_8( vectorRedux(ArrayXf(internal::random<int>(1,maxsize))) );
177  }
178 }
Matrix3f m
SCALAR Scalar
Definition: bench_gemm.cpp:33
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:285
#define max(a, b)
Definition: datatypes.h:20
float real
Definition: datatypes.h:10
#define min(a, b)
Definition: datatypes.h:19
MatrixType m2(n_dims)
ArrayXcf v
Definition: Cwise_arg.cpp:1
MatrixXf MatrixType
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:150
const size_t maxsize
Point3 mean(const CONTAINER &points)
mean
Definition: Point3.h:66
void vectorRedux(const VectorType &w)
Definition: redux.cpp:80
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
#define VERIFY_EVALUATION_COUNT(XPR, N)
#define VERIFY_IS_APPROX(a, b)
Matrix3d m1
Definition: IOFormat.cpp:2
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
RealScalar s
const mpreal sum(const mpreal tab[], const unsigned long int n, int &status, mp_rnd_t mode=mpreal::get_default_rnd())
Definition: mpreal.h:2381
Q R1(Eigen::AngleAxisd(1, Q_z_axis))
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:34
RowVector3d w
#define VERIFY_IS_MUCH_SMALLER_THAN(a, b)
Definition: main.h:335
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:91
static const double r1
#define EIGEN_TEST_MAX_SIZE
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:45
float * p
The matrix class, also used for vectors and row-vectors.
#define abs(x)
Definition: datatypes.h:17
void matrixRedux(const MatrixType &m)
Definition: redux.cpp:17
Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > & setRandom(Index size)
std::ptrdiff_t j
void test_redux()
Definition: redux.cpp:149
static const Key c1


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:43:50