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.

148 lines
4.8 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (C) 2009 Ricard Marxer <email@ricardmarxer.com>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #include "main.h"
  11. #include <iostream>
  12. using namespace std;
  13. template<typename MatrixType> void reverse(const MatrixType& m)
  14. {
  15. typedef typename MatrixType::Index Index;
  16. typedef typename MatrixType::Scalar Scalar;
  17. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  18. Index rows = m.rows();
  19. Index cols = m.cols();
  20. // this test relies a lot on Random.h, and there's not much more that we can do
  21. // to test it, hence I consider that we will have tested Random.h
  22. MatrixType m1 = MatrixType::Random(rows, cols), m2;
  23. VectorType v1 = VectorType::Random(rows);
  24. MatrixType m1_r = m1.reverse();
  25. // Verify that MatrixBase::reverse() works
  26. for ( int i = 0; i < rows; i++ ) {
  27. for ( int j = 0; j < cols; j++ ) {
  28. VERIFY_IS_APPROX(m1_r(i, j), m1(rows - 1 - i, cols - 1 - j));
  29. }
  30. }
  31. Reverse<MatrixType> m1_rd(m1);
  32. // Verify that a Reverse default (in both directions) of an expression works
  33. for ( int i = 0; i < rows; i++ ) {
  34. for ( int j = 0; j < cols; j++ ) {
  35. VERIFY_IS_APPROX(m1_rd(i, j), m1(rows - 1 - i, cols - 1 - j));
  36. }
  37. }
  38. Reverse<MatrixType, BothDirections> m1_rb(m1);
  39. // Verify that a Reverse in both directions of an expression works
  40. for ( int i = 0; i < rows; i++ ) {
  41. for ( int j = 0; j < cols; j++ ) {
  42. VERIFY_IS_APPROX(m1_rb(i, j), m1(rows - 1 - i, cols - 1 - j));
  43. }
  44. }
  45. Reverse<MatrixType, Vertical> m1_rv(m1);
  46. // Verify that a Reverse in the vertical directions of an expression works
  47. for ( int i = 0; i < rows; i++ ) {
  48. for ( int j = 0; j < cols; j++ ) {
  49. VERIFY_IS_APPROX(m1_rv(i, j), m1(rows - 1 - i, j));
  50. }
  51. }
  52. Reverse<MatrixType, Horizontal> m1_rh(m1);
  53. // Verify that a Reverse in the horizontal directions of an expression works
  54. for ( int i = 0; i < rows; i++ ) {
  55. for ( int j = 0; j < cols; j++ ) {
  56. VERIFY_IS_APPROX(m1_rh(i, j), m1(i, cols - 1 - j));
  57. }
  58. }
  59. VectorType v1_r = v1.reverse();
  60. // Verify that a VectorType::reverse() of an expression works
  61. for ( int i = 0; i < rows; i++ ) {
  62. VERIFY_IS_APPROX(v1_r(i), v1(rows - 1 - i));
  63. }
  64. MatrixType m1_cr = m1.colwise().reverse();
  65. // Verify that PartialRedux::reverse() works (for colwise())
  66. for ( int i = 0; i < rows; i++ ) {
  67. for ( int j = 0; j < cols; j++ ) {
  68. VERIFY_IS_APPROX(m1_cr(i, j), m1(rows - 1 - i, j));
  69. }
  70. }
  71. MatrixType m1_rr = m1.rowwise().reverse();
  72. // Verify that PartialRedux::reverse() works (for rowwise())
  73. for ( int i = 0; i < rows; i++ ) {
  74. for ( int j = 0; j < cols; j++ ) {
  75. VERIFY_IS_APPROX(m1_rr(i, j), m1(i, cols - 1 - j));
  76. }
  77. }
  78. Scalar x = internal::random<Scalar>();
  79. Index r = internal::random<Index>(0, rows-1),
  80. c = internal::random<Index>(0, cols-1);
  81. m1.reverse()(r, c) = x;
  82. VERIFY_IS_APPROX(x, m1(rows - 1 - r, cols - 1 - c));
  83. m2 = m1;
  84. m2.reverseInPlace();
  85. VERIFY_IS_APPROX(m2,m1.reverse().eval());
  86. m2 = m1;
  87. m2.col(0).reverseInPlace();
  88. VERIFY_IS_APPROX(m2.col(0),m1.col(0).reverse().eval());
  89. m2 = m1;
  90. m2.row(0).reverseInPlace();
  91. VERIFY_IS_APPROX(m2.row(0),m1.row(0).reverse().eval());
  92. m2 = m1;
  93. m2.rowwise().reverseInPlace();
  94. VERIFY_IS_APPROX(m2,m1.rowwise().reverse().eval());
  95. m2 = m1;
  96. m2.colwise().reverseInPlace();
  97. VERIFY_IS_APPROX(m2,m1.colwise().reverse().eval());
  98. /*
  99. m1.colwise().reverse()(r, c) = x;
  100. VERIFY_IS_APPROX(x, m1(rows - 1 - r, c));
  101. m1.rowwise().reverse()(r, c) = x;
  102. VERIFY_IS_APPROX(x, m1(r, cols - 1 - c));
  103. */
  104. }
  105. void test_array_reverse()
  106. {
  107. for(int i = 0; i < g_repeat; i++) {
  108. CALL_SUBTEST_1( reverse(Matrix<float, 1, 1>()) );
  109. CALL_SUBTEST_2( reverse(Matrix2f()) );
  110. CALL_SUBTEST_3( reverse(Matrix4f()) );
  111. CALL_SUBTEST_4( reverse(Matrix4d()) );
  112. CALL_SUBTEST_5( reverse(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  113. CALL_SUBTEST_6( reverse(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  114. CALL_SUBTEST_7( reverse(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  115. CALL_SUBTEST_8( reverse(Matrix<float, 100, 100>()) );
  116. CALL_SUBTEST_9( reverse(Matrix<float,Dynamic,Dynamic,RowMajor>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  117. }
  118. #ifdef EIGEN_TEST_PART_3
  119. Vector4f x; x << 1, 2, 3, 4;
  120. Vector4f y; y << 4, 3, 2, 1;
  121. VERIFY(x.reverse()[1] == 3);
  122. VERIFY(x.reverse() == y);
  123. #endif
  124. }