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
6.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2010 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. #define COMPARE_CORNER(A,B) \
  11. VERIFY_IS_EQUAL(matrix.A, matrix.B); \
  12. VERIFY_IS_EQUAL(const_matrix.A, const_matrix.B);
  13. template<typename MatrixType> void corners(const MatrixType& m)
  14. {
  15. typedef typename MatrixType::Index Index;
  16. Index rows = m.rows();
  17. Index cols = m.cols();
  18. Index r = internal::random<Index>(1,rows);
  19. Index c = internal::random<Index>(1,cols);
  20. MatrixType matrix = MatrixType::Random(rows,cols);
  21. const MatrixType const_matrix = MatrixType::Random(rows,cols);
  22. COMPARE_CORNER(topLeftCorner(r,c), block(0,0,r,c));
  23. COMPARE_CORNER(topRightCorner(r,c), block(0,cols-c,r,c));
  24. COMPARE_CORNER(bottomLeftCorner(r,c), block(rows-r,0,r,c));
  25. COMPARE_CORNER(bottomRightCorner(r,c), block(rows-r,cols-c,r,c));
  26. Index sr = internal::random<Index>(1,rows) - 1;
  27. Index nr = internal::random<Index>(1,rows-sr);
  28. Index sc = internal::random<Index>(1,cols) - 1;
  29. Index nc = internal::random<Index>(1,cols-sc);
  30. COMPARE_CORNER(topRows(r), block(0,0,r,cols));
  31. COMPARE_CORNER(middleRows(sr,nr), block(sr,0,nr,cols));
  32. COMPARE_CORNER(bottomRows(r), block(rows-r,0,r,cols));
  33. COMPARE_CORNER(leftCols(c), block(0,0,rows,c));
  34. COMPARE_CORNER(middleCols(sc,nc), block(0,sc,rows,nc));
  35. COMPARE_CORNER(rightCols(c), block(0,cols-c,rows,c));
  36. }
  37. template<typename MatrixType, int CRows, int CCols, int SRows, int SCols> void corners_fixedsize()
  38. {
  39. MatrixType matrix = MatrixType::Random();
  40. const MatrixType const_matrix = MatrixType::Random();
  41. enum {
  42. rows = MatrixType::RowsAtCompileTime,
  43. cols = MatrixType::ColsAtCompileTime,
  44. r = CRows,
  45. c = CCols,
  46. sr = SRows,
  47. sc = SCols
  48. };
  49. VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template block<r,c>(0,0)));
  50. VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template block<r,c>(0,cols-c)));
  51. VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template block<r,c>(rows-r,0)));
  52. VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template block<r,c>(rows-r,cols-c)));
  53. VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template topLeftCorner<r,Dynamic>(r,c)));
  54. VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template topRightCorner<r,Dynamic>(r,c)));
  55. VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template bottomLeftCorner<r,Dynamic>(r,c)));
  56. VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template bottomRightCorner<r,Dynamic>(r,c)));
  57. VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template topLeftCorner<Dynamic,c>(r,c)));
  58. VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template topRightCorner<Dynamic,c>(r,c)));
  59. VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template bottomLeftCorner<Dynamic,c>(r,c)));
  60. VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template bottomRightCorner<Dynamic,c>(r,c)));
  61. VERIFY_IS_EQUAL((matrix.template topRows<r>()), (matrix.template block<r,cols>(0,0)));
  62. VERIFY_IS_EQUAL((matrix.template middleRows<r>(sr)), (matrix.template block<r,cols>(sr,0)));
  63. VERIFY_IS_EQUAL((matrix.template bottomRows<r>()), (matrix.template block<r,cols>(rows-r,0)));
  64. VERIFY_IS_EQUAL((matrix.template leftCols<c>()), (matrix.template block<rows,c>(0,0)));
  65. VERIFY_IS_EQUAL((matrix.template middleCols<c>(sc)), (matrix.template block<rows,c>(0,sc)));
  66. VERIFY_IS_EQUAL((matrix.template rightCols<c>()), (matrix.template block<rows,c>(0,cols-c)));
  67. VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template block<r,c>(0,0)));
  68. VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template block<r,c>(0,cols-c)));
  69. VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template block<r,c>(rows-r,0)));
  70. VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template block<r,c>(rows-r,cols-c)));
  71. VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template topLeftCorner<r,Dynamic>(r,c)));
  72. VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template topRightCorner<r,Dynamic>(r,c)));
  73. VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template bottomLeftCorner<r,Dynamic>(r,c)));
  74. VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template bottomRightCorner<r,Dynamic>(r,c)));
  75. VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template topLeftCorner<Dynamic,c>(r,c)));
  76. VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template topRightCorner<Dynamic,c>(r,c)));
  77. VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template bottomLeftCorner<Dynamic,c>(r,c)));
  78. VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template bottomRightCorner<Dynamic,c>(r,c)));
  79. VERIFY_IS_EQUAL((const_matrix.template topRows<r>()), (const_matrix.template block<r,cols>(0,0)));
  80. VERIFY_IS_EQUAL((const_matrix.template middleRows<r>(sr)), (const_matrix.template block<r,cols>(sr,0)));
  81. VERIFY_IS_EQUAL((const_matrix.template bottomRows<r>()), (const_matrix.template block<r,cols>(rows-r,0)));
  82. VERIFY_IS_EQUAL((const_matrix.template leftCols<c>()), (const_matrix.template block<rows,c>(0,0)));
  83. VERIFY_IS_EQUAL((const_matrix.template middleCols<c>(sc)), (const_matrix.template block<rows,c>(0,sc)));
  84. VERIFY_IS_EQUAL((const_matrix.template rightCols<c>()), (const_matrix.template block<rows,c>(0,cols-c)));
  85. }
  86. void test_corners()
  87. {
  88. for(int i = 0; i < g_repeat; i++) {
  89. CALL_SUBTEST_1( corners(Matrix<float, 1, 1>()) );
  90. CALL_SUBTEST_2( corners(Matrix4d()) );
  91. CALL_SUBTEST_3( corners(Matrix<int,10,12>()) );
  92. CALL_SUBTEST_4( corners(MatrixXcf(5, 7)) );
  93. CALL_SUBTEST_5( corners(MatrixXf(21, 20)) );
  94. CALL_SUBTEST_1(( corners_fixedsize<Matrix<float, 1, 1>, 1, 1, 0, 0>() ));
  95. CALL_SUBTEST_2(( corners_fixedsize<Matrix4d,2,2,1,1>() ));
  96. CALL_SUBTEST_3(( corners_fixedsize<Matrix<int,10,12>,4,7,5,2>() ));
  97. }
  98. }