array_reverse.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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::Scalar Scalar;
  16. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  17. Index rows = m.rows();
  18. Index cols = m.cols();
  19. // this test relies a lot on Random.h, and there's not much more that we can do
  20. // to test it, hence I consider that we will have tested Random.h
  21. MatrixType m1 = MatrixType::Random(rows, cols), m2;
  22. VectorType v1 = VectorType::Random(rows);
  23. MatrixType m1_r = m1.reverse();
  24. // Verify that MatrixBase::reverse() works
  25. for ( int i = 0; i < rows; i++ ) {
  26. for ( int j = 0; j < cols; j++ ) {
  27. VERIFY_IS_APPROX(m1_r(i, j), m1(rows - 1 - i, cols - 1 - j));
  28. }
  29. }
  30. Reverse<MatrixType> m1_rd(m1);
  31. // Verify that a Reverse default (in both directions) of an expression works
  32. for ( int i = 0; i < rows; i++ ) {
  33. for ( int j = 0; j < cols; j++ ) {
  34. VERIFY_IS_APPROX(m1_rd(i, j), m1(rows - 1 - i, cols - 1 - j));
  35. }
  36. }
  37. Reverse<MatrixType, BothDirections> m1_rb(m1);
  38. // Verify that a Reverse in both directions of an expression works
  39. for ( int i = 0; i < rows; i++ ) {
  40. for ( int j = 0; j < cols; j++ ) {
  41. VERIFY_IS_APPROX(m1_rb(i, j), m1(rows - 1 - i, cols - 1 - j));
  42. }
  43. }
  44. Reverse<MatrixType, Vertical> m1_rv(m1);
  45. // Verify that a Reverse in the vertical directions of an expression works
  46. for ( int i = 0; i < rows; i++ ) {
  47. for ( int j = 0; j < cols; j++ ) {
  48. VERIFY_IS_APPROX(m1_rv(i, j), m1(rows - 1 - i, j));
  49. }
  50. }
  51. Reverse<MatrixType, Horizontal> m1_rh(m1);
  52. // Verify that a Reverse in the horizontal directions of an expression works
  53. for ( int i = 0; i < rows; i++ ) {
  54. for ( int j = 0; j < cols; j++ ) {
  55. VERIFY_IS_APPROX(m1_rh(i, j), m1(i, cols - 1 - j));
  56. }
  57. }
  58. VectorType v1_r = v1.reverse();
  59. // Verify that a VectorType::reverse() of an expression works
  60. for ( int i = 0; i < rows; i++ ) {
  61. VERIFY_IS_APPROX(v1_r(i), v1(rows - 1 - i));
  62. }
  63. MatrixType m1_cr = m1.colwise().reverse();
  64. // Verify that PartialRedux::reverse() works (for colwise())
  65. for ( int i = 0; i < rows; i++ ) {
  66. for ( int j = 0; j < cols; j++ ) {
  67. VERIFY_IS_APPROX(m1_cr(i, j), m1(rows - 1 - i, j));
  68. }
  69. }
  70. MatrixType m1_rr = m1.rowwise().reverse();
  71. // Verify that PartialRedux::reverse() works (for rowwise())
  72. for ( int i = 0; i < rows; i++ ) {
  73. for ( int j = 0; j < cols; j++ ) {
  74. VERIFY_IS_APPROX(m1_rr(i, j), m1(i, cols - 1 - j));
  75. }
  76. }
  77. Scalar x = internal::random<Scalar>();
  78. Index r = internal::random<Index>(0, rows-1),
  79. c = internal::random<Index>(0, cols-1);
  80. m1.reverse()(r, c) = x;
  81. VERIFY_IS_APPROX(x, m1(rows - 1 - r, cols - 1 - c));
  82. m2 = m1;
  83. m2.reverseInPlace();
  84. VERIFY_IS_APPROX(m2,m1.reverse().eval());
  85. m2 = m1;
  86. m2.col(0).reverseInPlace();
  87. VERIFY_IS_APPROX(m2.col(0),m1.col(0).reverse().eval());
  88. m2 = m1;
  89. m2.row(0).reverseInPlace();
  90. VERIFY_IS_APPROX(m2.row(0),m1.row(0).reverse().eval());
  91. m2 = m1;
  92. m2.rowwise().reverseInPlace();
  93. VERIFY_IS_APPROX(m2,m1.rowwise().reverse().eval());
  94. m2 = m1;
  95. m2.colwise().reverseInPlace();
  96. VERIFY_IS_APPROX(m2,m1.colwise().reverse().eval());
  97. m1.colwise().reverse()(r, c) = x;
  98. VERIFY_IS_APPROX(x, m1(rows - 1 - r, c));
  99. m1.rowwise().reverse()(r, c) = x;
  100. VERIFY_IS_APPROX(x, m1(r, cols - 1 - c));
  101. }
  102. template<int>
  103. void array_reverse_extra()
  104. {
  105. Vector4f x; x << 1, 2, 3, 4;
  106. Vector4f y; y << 4, 3, 2, 1;
  107. VERIFY(x.reverse()[1] == 3);
  108. VERIFY(x.reverse() == y);
  109. }
  110. // Simpler version of reverseInPlace leveraging a bug
  111. // in clang 6/7 with -O2 and AVX or AVX512 enabled.
  112. // This simpler version ensure that the clang bug is not simply hidden
  113. // through mis-inlining of reverseInPlace or other minor changes.
  114. template<typename MatrixType>
  115. EIGEN_DONT_INLINE
  116. void bug1684_job1(MatrixType& m1, MatrixType& m2)
  117. {
  118. m2 = m1;
  119. m2.col(0).swap(m2.col(3));
  120. m2.col(1).swap(m2.col(2));
  121. }
  122. template<typename MatrixType>
  123. EIGEN_DONT_INLINE
  124. void bug1684_job2(MatrixType& m1, MatrixType& m2)
  125. {
  126. m2 = m1; // load m1/m2 in AVX registers
  127. m1.col(0) = m2.col(3); // perform 128 bits moves
  128. m1.col(1) = m2.col(2);
  129. m1.col(2) = m2.col(1);
  130. m1.col(3) = m2.col(0);
  131. }
  132. template<typename MatrixType>
  133. EIGEN_DONT_INLINE
  134. void bug1684_job3(MatrixType& m1, MatrixType& m2)
  135. {
  136. m2 = m1;
  137. Vector4f tmp;
  138. tmp = m2.col(0);
  139. m2.col(0) = m2.col(3);
  140. m2.col(3) = tmp;
  141. tmp = m2.col(1);
  142. m2.col(1) = m2.col(2);
  143. m2.col(2) = tmp;
  144. }
  145. template<int>
  146. void bug1684()
  147. {
  148. Matrix4f m1 = Matrix4f::Random();
  149. Matrix4f m2 = Matrix4f::Random();
  150. bug1684_job1(m1,m2);
  151. VERIFY_IS_APPROX(m2, m1.rowwise().reverse().eval());
  152. bug1684_job2(m1,m2);
  153. VERIFY_IS_APPROX(m2, m1.rowwise().reverse().eval());
  154. // This one still fail after our swap's workaround,
  155. // but I expect users not to implement their own swap.
  156. // bug1684_job3(m1,m2);
  157. // VERIFY_IS_APPROX(m2, m1.rowwise().reverse().eval());
  158. }
  159. EIGEN_DECLARE_TEST(array_reverse)
  160. {
  161. for(int i = 0; i < g_repeat; i++) {
  162. CALL_SUBTEST_1( reverse(Matrix<float, 1, 1>()) );
  163. CALL_SUBTEST_2( reverse(Matrix2f()) );
  164. CALL_SUBTEST_3( reverse(Matrix4f()) );
  165. CALL_SUBTEST_4( reverse(Matrix4d()) );
  166. CALL_SUBTEST_5( reverse(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  167. CALL_SUBTEST_6( reverse(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  168. CALL_SUBTEST_7( reverse(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  169. CALL_SUBTEST_8( reverse(Matrix<float, 100, 100>()) );
  170. 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))) );
  171. CALL_SUBTEST_3( bug1684<0>() );
  172. }
  173. CALL_SUBTEST_3( array_reverse_extra<0>() );
  174. }