You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
7.6 KiB

  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. #define TEST_ENABLE_TEMPORARY_TRACKING
  11. #include "main.h"
  12. template<typename MatrixType> void matrixRedux(const MatrixType& m)
  13. {
  14. typedef typename MatrixType::Index Index;
  15. typedef typename MatrixType::Scalar Scalar;
  16. typedef typename MatrixType::RealScalar RealScalar;
  17. Index rows = m.rows();
  18. Index cols = m.cols();
  19. MatrixType m1 = MatrixType::Random(rows, cols);
  20. // The entries of m1 are uniformly distributed in [0,1], so m1.prod() is very small. This may lead to test
  21. // failures if we underflow into denormals. Thus, we scale so that entries are close to 1.
  22. MatrixType m1_for_prod = MatrixType::Ones(rows, cols) + RealScalar(0.2) * m1;
  23. VERIFY_IS_MUCH_SMALLER_THAN(MatrixType::Zero(rows, cols).sum(), Scalar(1));
  24. 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
  25. Scalar s(0), p(1), minc(numext::real(m1.coeff(0))), maxc(numext::real(m1.coeff(0)));
  26. for(int j = 0; j < cols; j++)
  27. for(int i = 0; i < rows; i++)
  28. {
  29. s += m1(i,j);
  30. p *= m1_for_prod(i,j);
  31. minc = (std::min)(numext::real(minc), numext::real(m1(i,j)));
  32. maxc = (std::max)(numext::real(maxc), numext::real(m1(i,j)));
  33. }
  34. const Scalar mean = s/Scalar(RealScalar(rows*cols));
  35. VERIFY_IS_APPROX(m1.sum(), s);
  36. VERIFY_IS_APPROX(m1.mean(), mean);
  37. VERIFY_IS_APPROX(m1_for_prod.prod(), p);
  38. VERIFY_IS_APPROX(m1.real().minCoeff(), numext::real(minc));
  39. VERIFY_IS_APPROX(m1.real().maxCoeff(), numext::real(maxc));
  40. // test slice vectorization assuming assign is ok
  41. Index r0 = internal::random<Index>(0,rows-1);
  42. Index c0 = internal::random<Index>(0,cols-1);
  43. Index r1 = internal::random<Index>(r0+1,rows)-r0;
  44. Index c1 = internal::random<Index>(c0+1,cols)-c0;
  45. VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).sum(), m1.block(r0,c0,r1,c1).eval().sum());
  46. VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).mean(), m1.block(r0,c0,r1,c1).eval().mean());
  47. VERIFY_IS_APPROX(m1_for_prod.block(r0,c0,r1,c1).prod(), m1_for_prod.block(r0,c0,r1,c1).eval().prod());
  48. VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).real().minCoeff(), m1.block(r0,c0,r1,c1).real().eval().minCoeff());
  49. VERIFY_IS_APPROX(m1.block(r0,c0,r1,c1).real().maxCoeff(), m1.block(r0,c0,r1,c1).real().eval().maxCoeff());
  50. // regression for bug 1090
  51. const int R1 = MatrixType::RowsAtCompileTime>=2 ? MatrixType::RowsAtCompileTime/2 : 6;
  52. const int C1 = MatrixType::ColsAtCompileTime>=2 ? MatrixType::ColsAtCompileTime/2 : 6;
  53. if(R1<=rows-r0 && C1<=cols-c0)
  54. {
  55. VERIFY_IS_APPROX( (m1.template block<R1,C1>(r0,c0).sum()), m1.block(r0,c0,R1,C1).sum() );
  56. }
  57. // test empty objects
  58. VERIFY_IS_APPROX(m1.block(r0,c0,0,0).sum(), Scalar(0));
  59. VERIFY_IS_APPROX(m1.block(r0,c0,0,0).prod(), Scalar(1));
  60. // test nesting complex expression
  61. VERIFY_EVALUATION_COUNT( (m1.matrix()*m1.matrix().transpose()).sum(), (MatrixType::SizeAtCompileTime==Dynamic ? 1 : 0) );
  62. Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> m2(rows,rows);
  63. m2.setRandom();
  64. VERIFY_EVALUATION_COUNT( ((m1.matrix()*m1.matrix().transpose())+m2).sum(), (MatrixType::SizeAtCompileTime==Dynamic ? 1 : 0) );
  65. }
  66. template<typename VectorType> void vectorRedux(const VectorType& w)
  67. {
  68. using std::abs;
  69. typedef typename VectorType::Index Index;
  70. typedef typename VectorType::Scalar Scalar;
  71. typedef typename NumTraits<Scalar>::Real RealScalar;
  72. Index size = w.size();
  73. VectorType v = VectorType::Random(size);
  74. VectorType v_for_prod = VectorType::Ones(size) + Scalar(0.2) * v; // see comment above declaration of m1_for_prod
  75. for(int i = 1; i < size; i++)
  76. {
  77. Scalar s(0), p(1);
  78. RealScalar minc(numext::real(v.coeff(0))), maxc(numext::real(v.coeff(0)));
  79. for(int j = 0; j < i; j++)
  80. {
  81. s += v[j];
  82. p *= v_for_prod[j];
  83. minc = (std::min)(minc, numext::real(v[j]));
  84. maxc = (std::max)(maxc, numext::real(v[j]));
  85. }
  86. VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.head(i).sum()), Scalar(1));
  87. VERIFY_IS_APPROX(p, v_for_prod.head(i).prod());
  88. VERIFY_IS_APPROX(minc, v.real().head(i).minCoeff());
  89. VERIFY_IS_APPROX(maxc, v.real().head(i).maxCoeff());
  90. }
  91. for(int i = 0; i < size-1; i++)
  92. {
  93. Scalar s(0), p(1);
  94. RealScalar minc(numext::real(v.coeff(i))), maxc(numext::real(v.coeff(i)));
  95. for(int j = i; j < size; j++)
  96. {
  97. s += v[j];
  98. p *= v_for_prod[j];
  99. minc = (std::min)(minc, numext::real(v[j]));
  100. maxc = (std::max)(maxc, numext::real(v[j]));
  101. }
  102. VERIFY_IS_MUCH_SMALLER_THAN(abs(s - v.tail(size-i).sum()), Scalar(1));
  103. VERIFY_IS_APPROX(p, v_for_prod.tail(size-i).prod());
  104. VERIFY_IS_APPROX(minc, v.real().tail(size-i).minCoeff());
  105. VERIFY_IS_APPROX(maxc, v.real().tail(size-i).maxCoeff());
  106. }
  107. for(int i = 0; i < size/2; 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-i; 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.segment(i, size-2*i).sum()), Scalar(1));
  119. VERIFY_IS_APPROX(p, v_for_prod.segment(i, size-2*i).prod());
  120. VERIFY_IS_APPROX(minc, v.real().segment(i, size-2*i).minCoeff());
  121. VERIFY_IS_APPROX(maxc, v.real().segment(i, size-2*i).maxCoeff());
  122. }
  123. // test empty objects
  124. VERIFY_IS_APPROX(v.head(0).sum(), Scalar(0));
  125. VERIFY_IS_APPROX(v.tail(0).prod(), Scalar(1));
  126. VERIFY_RAISES_ASSERT(v.head(0).mean());
  127. VERIFY_RAISES_ASSERT(v.head(0).minCoeff());
  128. VERIFY_RAISES_ASSERT(v.head(0).maxCoeff());
  129. }
  130. void test_redux()
  131. {
  132. // the max size cannot be too large, otherwise reduxion operations obviously generate large errors.
  133. int maxsize = (std::min)(100,EIGEN_TEST_MAX_SIZE);
  134. TEST_SET_BUT_UNUSED_VARIABLE(maxsize);
  135. for(int i = 0; i < g_repeat; i++) {
  136. CALL_SUBTEST_1( matrixRedux(Matrix<float, 1, 1>()) );
  137. CALL_SUBTEST_1( matrixRedux(Array<float, 1, 1>()) );
  138. CALL_SUBTEST_2( matrixRedux(Matrix2f()) );
  139. CALL_SUBTEST_2( matrixRedux(Array2f()) );
  140. CALL_SUBTEST_3( matrixRedux(Matrix4d()) );
  141. CALL_SUBTEST_3( matrixRedux(Array4d()) );
  142. CALL_SUBTEST_4( matrixRedux(MatrixXcf(internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
  143. CALL_SUBTEST_4( matrixRedux(ArrayXXcf(internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
  144. CALL_SUBTEST_5( matrixRedux(MatrixXd (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
  145. CALL_SUBTEST_5( matrixRedux(ArrayXXd (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
  146. CALL_SUBTEST_6( matrixRedux(MatrixXi (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
  147. CALL_SUBTEST_6( matrixRedux(ArrayXXi (internal::random<int>(1,maxsize), internal::random<int>(1,maxsize))) );
  148. }
  149. for(int i = 0; i < g_repeat; i++) {
  150. CALL_SUBTEST_7( vectorRedux(Vector4f()) );
  151. CALL_SUBTEST_7( vectorRedux(Array4f()) );
  152. CALL_SUBTEST_5( vectorRedux(VectorXd(internal::random<int>(1,maxsize))) );
  153. CALL_SUBTEST_5( vectorRedux(ArrayXd(internal::random<int>(1,maxsize))) );
  154. CALL_SUBTEST_8( vectorRedux(VectorXf(internal::random<int>(1,maxsize))) );
  155. CALL_SUBTEST_8( vectorRedux(ArrayXf(internal::random<int>(1,maxsize))) );
  156. }
  157. }