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.

118 lines
3.4 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 = Scalar(1000), maxc = Scalar(-1000);
  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. }
  92. void test_visitor()
  93. {
  94. for(int i = 0; i < g_repeat; i++) {
  95. CALL_SUBTEST_1( matrixVisitor(Matrix<float, 1, 1>()) );
  96. CALL_SUBTEST_2( matrixVisitor(Matrix2f()) );
  97. CALL_SUBTEST_3( matrixVisitor(Matrix4d()) );
  98. CALL_SUBTEST_4( matrixVisitor(MatrixXd(8, 12)) );
  99. CALL_SUBTEST_5( matrixVisitor(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 20)) );
  100. CALL_SUBTEST_6( matrixVisitor(MatrixXi(8, 12)) );
  101. }
  102. for(int i = 0; i < g_repeat; i++) {
  103. CALL_SUBTEST_7( vectorVisitor(Vector4f()) );
  104. CALL_SUBTEST_8( vectorVisitor(VectorXd(10)) );
  105. CALL_SUBTEST_9( vectorVisitor(RowVectorXd(10)) );
  106. CALL_SUBTEST_10( vectorVisitor(VectorXf(33)) );
  107. }
  108. }