blob: e23159defa0f7897ace6fdc3cc42441de7c264fe [file] [log] [blame]
Gael Guennebaud6fbca942009-02-06 09:01:50 +00001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacob6347b1d2009-05-22 20:25:33 +02002// for linear algebra.
Gael Guennebaud6fbca942009-02-06 09:01:50 +00003//
4// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5// Copyright (C) 2009 Ricard Marxer <email@ricardmarxer.com>
6//
Benoit Jacob69124cf2012-07-13 14:42:47 -04007// 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/.
Gael Guennebaud6fbca942009-02-06 09:01:50 +000010
11#include "main.h"
12#include <iostream>
13
14using namespace std;
15
16template<typename MatrixType> void reverse(const MatrixType& m)
17{
18 typedef typename MatrixType::Scalar Scalar;
19 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
20
Hauke Heibelf1679c72010-06-20 17:37:56 +020021 Index rows = m.rows();
22 Index cols = m.cols();
Gael Guennebaud6fbca942009-02-06 09:01:50 +000023
24 // this test relies a lot on Random.h, and there's not much more that we can do
25 // to test it, hence I consider that we will have tested Random.h
Gael Guennebauddfb674a2015-03-31 20:17:10 +020026 MatrixType m1 = MatrixType::Random(rows, cols), m2;
Gael Guennebaud6fbca942009-02-06 09:01:50 +000027 VectorType v1 = VectorType::Random(rows);
28
29 MatrixType m1_r = m1.reverse();
30 // Verify that MatrixBase::reverse() works
31 for ( int i = 0; i < rows; i++ ) {
32 for ( int j = 0; j < cols; j++ ) {
33 VERIFY_IS_APPROX(m1_r(i, j), m1(rows - 1 - i, cols - 1 - j));
34 }
35 }
36
37 Reverse<MatrixType> m1_rd(m1);
38 // Verify that a Reverse default (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_rd(i, j), m1(rows - 1 - i, cols - 1 - j));
42 }
43 }
44
45 Reverse<MatrixType, BothDirections> m1_rb(m1);
46 // Verify that a Reverse in both 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_rb(i, j), m1(rows - 1 - i, cols - 1 - j));
50 }
51 }
52
53 Reverse<MatrixType, Vertical> m1_rv(m1);
54 // Verify that a Reverse in the vertical directions of an expression works
55 for ( int i = 0; i < rows; i++ ) {
56 for ( int j = 0; j < cols; j++ ) {
57 VERIFY_IS_APPROX(m1_rv(i, j), m1(rows - 1 - i, j));
58 }
59 }
60
61 Reverse<MatrixType, Horizontal> m1_rh(m1);
62 // Verify that a Reverse in the horizontal directions of an expression works
63 for ( int i = 0; i < rows; i++ ) {
64 for ( int j = 0; j < cols; j++ ) {
65 VERIFY_IS_APPROX(m1_rh(i, j), m1(i, cols - 1 - j));
66 }
67 }
68
69 VectorType v1_r = v1.reverse();
70 // Verify that a VectorType::reverse() of an expression works
71 for ( int i = 0; i < rows; i++ ) {
72 VERIFY_IS_APPROX(v1_r(i), v1(rows - 1 - i));
73 }
74
75 MatrixType m1_cr = m1.colwise().reverse();
76 // Verify that PartialRedux::reverse() works (for colwise())
77 for ( int i = 0; i < rows; i++ ) {
78 for ( int j = 0; j < cols; j++ ) {
79 VERIFY_IS_APPROX(m1_cr(i, j), m1(rows - 1 - i, j));
80 }
81 }
82
83 MatrixType m1_rr = m1.rowwise().reverse();
84 // Verify that PartialRedux::reverse() works (for rowwise())
85 for ( int i = 0; i < rows; i++ ) {
86 for ( int j = 0; j < cols; j++ ) {
87 VERIFY_IS_APPROX(m1_rr(i, j), m1(i, cols - 1 - j));
88 }
89 }
90
Benoit Jacob47160402010-10-25 10:15:22 -040091 Scalar x = internal::random<Scalar>();
Gael Guennebaud6fbca942009-02-06 09:01:50 +000092
Benoit Jacob47160402010-10-25 10:15:22 -040093 Index r = internal::random<Index>(0, rows-1),
94 c = internal::random<Index>(0, cols-1);
Gael Guennebaud6fbca942009-02-06 09:01:50 +000095
96 m1.reverse()(r, c) = x;
97 VERIFY_IS_APPROX(x, m1(rows - 1 - r, cols - 1 - c));
Gael Guennebauddfb674a2015-03-31 20:17:10 +020098
99 m2 = m1;
100 m2.reverseInPlace();
101 VERIFY_IS_APPROX(m2,m1.reverse().eval());
102
103 m2 = m1;
104 m2.col(0).reverseInPlace();
105 VERIFY_IS_APPROX(m2.col(0),m1.col(0).reverse().eval());
106
107 m2 = m1;
108 m2.row(0).reverseInPlace();
109 VERIFY_IS_APPROX(m2.row(0),m1.row(0).reverse().eval());
Gael Guennebaud8313fb72015-03-31 21:35:53 +0200110
111 m2 = m1;
112 m2.rowwise().reverseInPlace();
113 VERIFY_IS_APPROX(m2,m1.rowwise().reverse().eval());
114
115 m2 = m1;
116 m2.colwise().reverseInPlace();
117 VERIFY_IS_APPROX(m2,m1.colwise().reverse().eval());
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000118
119 m1.colwise().reverse()(r, c) = x;
120 VERIFY_IS_APPROX(x, m1(rows - 1 - r, c));
121
122 m1.rowwise().reverse()(r, c) = x;
123 VERIFY_IS_APPROX(x, m1(r, cols - 1 - c));
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000124}
125
Gael Guennebauddff3a922018-07-17 15:52:58 +0200126template<int>
127void array_reverse_extra()
128{
129 Vector4f x; x << 1, 2, 3, 4;
130 Vector4f y; y << 4, 3, 2, 1;
131 VERIFY(x.reverse()[1] == 3);
132 VERIFY(x.reverse() == y);
133}
134
Gael Guennebaud82f0ce22018-07-17 14:46:15 +0200135EIGEN_DECLARE_TEST(array_reverse)
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000136{
137 for(int i = 0; i < g_repeat; i++) {
Benoit Jacob2840ac72009-10-28 18:19:29 -0400138 CALL_SUBTEST_1( reverse(Matrix<float, 1, 1>()) );
139 CALL_SUBTEST_2( reverse(Matrix2f()) );
140 CALL_SUBTEST_3( reverse(Matrix4f()) );
141 CALL_SUBTEST_4( reverse(Matrix4d()) );
Gael Guennebauddfb674a2015-03-31 20:17:10 +0200142 CALL_SUBTEST_5( reverse(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
143 CALL_SUBTEST_6( reverse(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
144 CALL_SUBTEST_7( reverse(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
Benoit Jacob2840ac72009-10-28 18:19:29 -0400145 CALL_SUBTEST_8( reverse(Matrix<float, 100, 100>()) );
Gael Guennebauddfb674a2015-03-31 20:17:10 +0200146 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))) );
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000147 }
Gael Guennebauddff3a922018-07-17 15:52:58 +0200148 CALL_SUBTEST_3( array_reverse_extra<0>() );
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000149}