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.

130 lines
3.8 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. //
  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. #include "main.h"
  10. template<typename MatrixType> void matrixVisitor(const MatrixType& p)
  11. {
  12. typedef typename MatrixType::Scalar Scalar;
  13. typedef typename MatrixType::Index Index;
  14. Index rows = p.rows();
  15. Index cols = p.cols();
  16. // construct a random matrix where all coefficients are different
  17. MatrixType m;
  18. m = MatrixType::Random(rows, cols);
  19. for(Index i = 0; i < m.size(); i++)
  20. for(Index i2 = 0; i2 < i; i2++)
  21. while(m(i) == m(i2)) // yes, ==
  22. m(i) = internal::random<Scalar>();
  23. Scalar minc = Scalar(1000), maxc = Scalar(-1000);
  24. Index minrow=0,mincol=0,maxrow=0,maxcol=0;
  25. for(Index j = 0; j < cols; j++)
  26. for(Index i = 0; i < rows; i++)
  27. {
  28. if(m(i,j) < minc)
  29. {
  30. minc = m(i,j);
  31. minrow = i;
  32. mincol = j;
  33. }
  34. if(m(i,j) > maxc)
  35. {
  36. maxc = m(i,j);
  37. maxrow = i;
  38. maxcol = j;
  39. }
  40. }
  41. Index eigen_minrow, eigen_mincol, eigen_maxrow, eigen_maxcol;
  42. Scalar eigen_minc, eigen_maxc;
  43. eigen_minc = m.minCoeff(&eigen_minrow,&eigen_mincol);
  44. eigen_maxc = m.maxCoeff(&eigen_maxrow,&eigen_maxcol);
  45. VERIFY(minrow == eigen_minrow);
  46. VERIFY(maxrow == eigen_maxrow);
  47. VERIFY(mincol == eigen_mincol);
  48. VERIFY(maxcol == eigen_maxcol);
  49. VERIFY_IS_APPROX(minc, eigen_minc);
  50. VERIFY_IS_APPROX(maxc, eigen_maxc);
  51. VERIFY_IS_APPROX(minc, m.minCoeff());
  52. VERIFY_IS_APPROX(maxc, m.maxCoeff());
  53. }
  54. template<typename VectorType> void vectorVisitor(const VectorType& w)
  55. {
  56. typedef typename VectorType::Scalar Scalar;
  57. typedef typename VectorType::Index Index;
  58. Index size = w.size();
  59. // construct a random vector where all coefficients are different
  60. VectorType v;
  61. v = VectorType::Random(size);
  62. for(Index i = 0; i < size; i++)
  63. for(Index i2 = 0; i2 < i; i2++)
  64. while(v(i) == v(i2)) // yes, ==
  65. v(i) = internal::random<Scalar>();
  66. Scalar minc = v(0), maxc = v(0);
  67. Index minidx=0, maxidx=0;
  68. for(Index i = 0; i < size; i++)
  69. {
  70. if(v(i) < minc)
  71. {
  72. minc = v(i);
  73. minidx = i;
  74. }
  75. if(v(i) > maxc)
  76. {
  77. maxc = v(i);
  78. maxidx = i;
  79. }
  80. }
  81. Index eigen_minidx, eigen_maxidx;
  82. Scalar eigen_minc, eigen_maxc;
  83. eigen_minc = v.minCoeff(&eigen_minidx);
  84. eigen_maxc = v.maxCoeff(&eigen_maxidx);
  85. VERIFY(minidx == eigen_minidx);
  86. VERIFY(maxidx == eigen_maxidx);
  87. VERIFY_IS_APPROX(minc, eigen_minc);
  88. VERIFY_IS_APPROX(maxc, eigen_maxc);
  89. VERIFY_IS_APPROX(minc, v.minCoeff());
  90. VERIFY_IS_APPROX(maxc, v.maxCoeff());
  91. Index idx0 = internal::random<Index>(0,size-1);
  92. Index idx1 = eigen_minidx;
  93. Index idx2 = eigen_maxidx;
  94. VectorType v1(v), v2(v);
  95. v1(idx0) = v1(idx1);
  96. v2(idx0) = v2(idx2);
  97. v1.minCoeff(&eigen_minidx);
  98. v2.maxCoeff(&eigen_maxidx);
  99. VERIFY(eigen_minidx == (std::min)(idx0,idx1));
  100. VERIFY(eigen_maxidx == (std::min)(idx0,idx2));
  101. }
  102. void test_visitor()
  103. {
  104. for(int i = 0; i < g_repeat; i++) {
  105. CALL_SUBTEST_1( matrixVisitor(Matrix<float, 1, 1>()) );
  106. CALL_SUBTEST_2( matrixVisitor(Matrix2f()) );
  107. CALL_SUBTEST_3( matrixVisitor(Matrix4d()) );
  108. CALL_SUBTEST_4( matrixVisitor(MatrixXd(8, 12)) );
  109. CALL_SUBTEST_5( matrixVisitor(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 20)) );
  110. CALL_SUBTEST_6( matrixVisitor(MatrixXi(8, 12)) );
  111. }
  112. for(int i = 0; i < g_repeat; i++) {
  113. CALL_SUBTEST_7( vectorVisitor(Vector4f()) );
  114. CALL_SUBTEST_7( vectorVisitor(Matrix<int,12,1>()) );
  115. CALL_SUBTEST_8( vectorVisitor(VectorXd(10)) );
  116. CALL_SUBTEST_9( vectorVisitor(RowVectorXd(10)) );
  117. CALL_SUBTEST_10( vectorVisitor(VectorXf(33)) );
  118. }
  119. }