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.

135 lines
4.0 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. eigen_maxc = (m.adjoint()*m).maxCoeff(&eigen_maxrow,&eigen_maxcol);
  54. eigen_maxc = (m.adjoint()*m).eval().maxCoeff(&maxrow,&maxcol);
  55. VERIFY(maxrow == eigen_maxrow);
  56. VERIFY(maxcol == eigen_maxcol);
  57. }
  58. template<typename VectorType> void vectorVisitor(const VectorType& w)
  59. {
  60. typedef typename VectorType::Scalar Scalar;
  61. typedef typename VectorType::Index Index;
  62. Index size = w.size();
  63. // construct a random vector where all coefficients are different
  64. VectorType v;
  65. v = VectorType::Random(size);
  66. for(Index i = 0; i < size; i++)
  67. for(Index i2 = 0; i2 < i; i2++)
  68. while(v(i) == v(i2)) // yes, ==
  69. v(i) = internal::random<Scalar>();
  70. Scalar minc = v(0), maxc = v(0);
  71. Index minidx=0, maxidx=0;
  72. for(Index i = 0; i < size; i++)
  73. {
  74. if(v(i) < minc)
  75. {
  76. minc = v(i);
  77. minidx = i;
  78. }
  79. if(v(i) > maxc)
  80. {
  81. maxc = v(i);
  82. maxidx = i;
  83. }
  84. }
  85. Index eigen_minidx, eigen_maxidx;
  86. Scalar eigen_minc, eigen_maxc;
  87. eigen_minc = v.minCoeff(&eigen_minidx);
  88. eigen_maxc = v.maxCoeff(&eigen_maxidx);
  89. VERIFY(minidx == eigen_minidx);
  90. VERIFY(maxidx == eigen_maxidx);
  91. VERIFY_IS_APPROX(minc, eigen_minc);
  92. VERIFY_IS_APPROX(maxc, eigen_maxc);
  93. VERIFY_IS_APPROX(minc, v.minCoeff());
  94. VERIFY_IS_APPROX(maxc, v.maxCoeff());
  95. Index idx0 = internal::random<Index>(0,size-1);
  96. Index idx1 = eigen_minidx;
  97. Index idx2 = eigen_maxidx;
  98. VectorType v1(v), v2(v);
  99. v1(idx0) = v1(idx1);
  100. v2(idx0) = v2(idx2);
  101. v1.minCoeff(&eigen_minidx);
  102. v2.maxCoeff(&eigen_maxidx);
  103. VERIFY(eigen_minidx == (std::min)(idx0,idx1));
  104. VERIFY(eigen_maxidx == (std::min)(idx0,idx2));
  105. }
  106. void test_visitor()
  107. {
  108. for(int i = 0; i < g_repeat; i++) {
  109. CALL_SUBTEST_1( matrixVisitor(Matrix<float, 1, 1>()) );
  110. CALL_SUBTEST_2( matrixVisitor(Matrix2f()) );
  111. CALL_SUBTEST_3( matrixVisitor(Matrix4d()) );
  112. CALL_SUBTEST_4( matrixVisitor(MatrixXd(8, 12)) );
  113. CALL_SUBTEST_5( matrixVisitor(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 20)) );
  114. CALL_SUBTEST_6( matrixVisitor(MatrixXi(8, 12)) );
  115. }
  116. for(int i = 0; i < g_repeat; i++) {
  117. CALL_SUBTEST_7( vectorVisitor(Vector4f()) );
  118. CALL_SUBTEST_7( vectorVisitor(Matrix<int,12,1>()) );
  119. CALL_SUBTEST_8( vectorVisitor(VectorXd(10)) );
  120. CALL_SUBTEST_9( vectorVisitor(RowVectorXd(10)) );
  121. CALL_SUBTEST_10( vectorVisitor(VectorXf(33)) );
  122. }
  123. }