1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#include "main.h"
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType> void matrixRedux(const MatrixType& m)
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename MatrixType::Index Index;
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename MatrixType::Scalar Scalar;
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename MatrixType::RealScalar RealScalar;
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Index rows = m.rows();
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Index cols = m.cols();
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  MatrixType m1 = MatrixType::Random(rows, cols);
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // The entries of m1 are uniformly distributed in [0,1], so m1.prod() is very small. This may lead to test
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // failures if we underflow into denormals. Thus, we scale so that entires are close to 1.
257faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  MatrixType m1_for_prod = MatrixType::Ones(rows, cols) + RealScalar(0.2) * m1;
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows, cols).sum(), Scalar(1));
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  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
297faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  Scalar s(0), p(1), minc(numext::real(m1.coeff(0))), maxc(numext::real(m1.coeff(0)));
30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  for(int j = 0; j < cols; j++)
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  for(int i = 0; i < rows; i++)
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    s += m1(i,j);
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    p *= m1_for_prod(i,j);
357faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    minc = (std::min)(numext::real(minc), numext::real(m1(i,j)));
367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    maxc = (std::max)(numext::real(maxc), numext::real(m1(i,j)));
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  const Scalar mean = s/Scalar(RealScalar(rows*cols));
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m1.sum(), s);
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m1.mean(), mean);
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m1_for_prod.prod(), p);
437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_APPROX(m1.real().minCoeff(), numext::real(minc));
447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  VERIFY_IS_APPROX(m1.real().maxCoeff(), numext::real(maxc));
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // test slice vectorization assuming assign is ok
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Index r0 = internal::random<Index>(0,rows-1);
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Index c0 = internal::random<Index>(0,cols-1);
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Index r1 = internal::random<Index>(r0+1,rows)-r0;
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Index c1 = internal::random<Index>(c0+1,cols)-c0;
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).sum(), m1.block(r0,c0,r1,c1).eval().sum());
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).mean(), m1.block(r0,c0,r1,c1).eval().mean());
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m1_for_prod.block(r0,c0,r1,c1).prod(), m1_for_prod.block(r0,c0,r1,c1).eval().prod());
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).real().minCoeff(), m1.block(r0,c0,r1,c1).real().eval().minCoeff());
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).real().maxCoeff(), m1.block(r0,c0,r1,c1).real().eval().maxCoeff());
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // test empty objects
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m1.block(r0,c0,0,0).sum(),   Scalar(0));
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(m1.block(r0,c0,0,0).prod(),  Scalar(1));
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename VectorType> void vectorRedux(const VectorType& w)
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
647faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  using std::abs;
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename VectorType::Index Index;
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename VectorType::Scalar Scalar;
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename NumTraits<Scalar>::Real RealScalar;
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Index size = w.size();
69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VectorType v = VectorType::Random(size);
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VectorType v_for_prod = VectorType::Ones(size) + Scalar(0.2) * v; // see comment above declaration of m1_for_prod
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  for(int i = 1; i < size; i++)
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Scalar s(0), p(1);
767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    RealScalar minc(numext::real(v.coeff(0))), maxc(numext::real(v.coeff(0)));
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    for(int j = 0; j < i; j++)
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      s += v[j];
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      p *= v_for_prod[j];
817faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      minc = (std::min)(minc, numext::real(v[j]));
827faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      maxc = (std::max)(maxc, numext::real(v[j]));
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
847faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.head(i).sum()), Scalar(1));
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    VERIFY_IS_APPROX(p, v_for_prod.head(i).prod());
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    VERIFY_IS_APPROX(minc, v.real().head(i).minCoeff());
87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    VERIFY_IS_APPROX(maxc, v.real().head(i).maxCoeff());
88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  for(int i = 0; i < size-1; i++)
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Scalar s(0), p(1);
937faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    RealScalar minc(numext::real(v.coeff(i))), maxc(numext::real(v.coeff(i)));
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    for(int j = i; j < size; j++)
95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      s += v[j];
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      p *= v_for_prod[j];
987faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      minc = (std::min)(minc, numext::real(v[j]));
997faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      maxc = (std::max)(maxc, numext::real(v[j]));
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
1017faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.tail(size-i).sum()), Scalar(1));
102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    VERIFY_IS_APPROX(p, v_for_prod.tail(size-i).prod());
103c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    VERIFY_IS_APPROX(minc, v.real().tail(size-i).minCoeff());
104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    VERIFY_IS_APPROX(maxc, v.real().tail(size-i).maxCoeff());
105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
107c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  for(int i = 0; i < size/2; i++)
108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Scalar s(0), p(1);
1107faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    RealScalar minc(numext::real(v.coeff(i))), maxc(numext::real(v.coeff(i)));
111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    for(int j = i; j < size-i; j++)
112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      s += v[j];
114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      p *= v_for_prod[j];
1157faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      minc = (std::min)(minc, numext::real(v[j]));
1167faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      maxc = (std::max)(maxc, numext::real(v[j]));
117c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
1187faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.segment(i, size-2*i).sum()), Scalar(1));
119c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    VERIFY_IS_APPROX(p, v_for_prod.segment(i, size-2*i).prod());
120c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    VERIFY_IS_APPROX(minc, v.real().segment(i, size-2*i).minCoeff());
121c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    VERIFY_IS_APPROX(maxc, v.real().segment(i, size-2*i).maxCoeff());
122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
124c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // test empty objects
125c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(v.head(0).sum(),   Scalar(0));
126c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_IS_APPROX(v.tail(0).prod(),  Scalar(1));
127c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_RAISES_ASSERT(v.head(0).mean());
128c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_RAISES_ASSERT(v.head(0).minCoeff());
129c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  VERIFY_RAISES_ASSERT(v.head(0).maxCoeff());
130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
132c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathvoid test_redux()
133c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // the max size cannot be too large, otherwise reduxion operations obviously generate large errors.
135c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  int maxsize = (std::min)(100,EIGEN_TEST_MAX_SIZE);
1367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  TEST_SET_BUT_UNUSED_VARIABLE(maxsize);
137c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  for(int i = 0; i < g_repeat; i++) {
138c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_1( matrixRedux(Matrix<float, 1, 1>()) );
139c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_1( matrixRedux(Array<float, 1, 1>()) );
140c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_2( matrixRedux(Matrix2f()) );
141c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_2( matrixRedux(Array2f()) );
142c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_3( matrixRedux(Matrix4d()) );
143c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_3( matrixRedux(Array4d()) );
144c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_4( matrixRedux(MatrixXcf(internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
145c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_4( matrixRedux(ArrayXXcf(internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
146c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_5( matrixRedux(MatrixXd (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
147c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_5( matrixRedux(ArrayXXd (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
148c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_6( matrixRedux(MatrixXi (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
149c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_6( matrixRedux(ArrayXXi (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
150c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
151c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  for(int i = 0; i < g_repeat; i++) {
152c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_7( vectorRedux(Vector4f()) );
153c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_7( vectorRedux(Array4f()) );
154c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_5( vectorRedux(VectorXd(internal::random<int>(1,maxsize))) );
155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_5( vectorRedux(ArrayXd(internal::random<int>(1,maxsize))) );
156c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_8( vectorRedux(VectorXf(internal::random<int>(1,maxsize))) );
157c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    CALL_SUBTEST_8( vectorRedux(ArrayXf(internal::random<int>(1,maxsize))) );
158c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
159c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
160