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.

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