blob: 3933ff5230e8f840011c8d7147a1e3c206842385 [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//
7// Eigen is free software; you can redistribute it and/or
8// modify it under the terms of the GNU Lesser General Public
9// License as published by the Free Software Foundation; either
10// version 3 of the License, or (at your option) any later version.
11//
12// Alternatively, you can redistribute it and/or
13// modify it under the terms of the GNU General Public License as
14// published by the Free Software Foundation; either version 2 of
15// the License, or (at your option) any later version.
16//
17// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public
23// License and a copy of the GNU General Public License along with
24// Eigen. If not, see <http://www.gnu.org/licenses/>.
25
26#include "main.h"
27#include <iostream>
28
29using namespace std;
30
31template<typename MatrixType> void reverse(const MatrixType& m)
32{
33 typedef typename MatrixType::Scalar Scalar;
34 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
35
36 int rows = m.rows();
37 int cols = m.cols();
38
39 // this test relies a lot on Random.h, and there's not much more that we can do
40 // to test it, hence I consider that we will have tested Random.h
41 MatrixType m1 = MatrixType::Random(rows, cols);
42 VectorType v1 = VectorType::Random(rows);
43
44 MatrixType m1_r = m1.reverse();
45 // Verify that MatrixBase::reverse() works
46 for ( int i = 0; i < rows; i++ ) {
47 for ( int j = 0; j < cols; j++ ) {
48 VERIFY_IS_APPROX(m1_r(i, j), m1(rows - 1 - i, cols - 1 - j));
49 }
50 }
51
52 Reverse<MatrixType> m1_rd(m1);
53 // Verify that a Reverse default (in both 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_rd(i, j), m1(rows - 1 - i, cols - 1 - j));
57 }
58 }
59
60 Reverse<MatrixType, BothDirections> m1_rb(m1);
61 // Verify that a Reverse in both directions of an expression works
62 for ( int i = 0; i < rows; i++ ) {
63 for ( int j = 0; j < cols; j++ ) {
64 VERIFY_IS_APPROX(m1_rb(i, j), m1(rows - 1 - i, cols - 1 - j));
65 }
66 }
67
68 Reverse<MatrixType, Vertical> m1_rv(m1);
69 // Verify that a Reverse in the vertical directions of an expression works
70 for ( int i = 0; i < rows; i++ ) {
71 for ( int j = 0; j < cols; j++ ) {
72 VERIFY_IS_APPROX(m1_rv(i, j), m1(rows - 1 - i, j));
73 }
74 }
75
76 Reverse<MatrixType, Horizontal> m1_rh(m1);
77 // Verify that a Reverse in the horizontal directions of an expression works
78 for ( int i = 0; i < rows; i++ ) {
79 for ( int j = 0; j < cols; j++ ) {
80 VERIFY_IS_APPROX(m1_rh(i, j), m1(i, cols - 1 - j));
81 }
82 }
83
84 VectorType v1_r = v1.reverse();
85 // Verify that a VectorType::reverse() of an expression works
86 for ( int i = 0; i < rows; i++ ) {
87 VERIFY_IS_APPROX(v1_r(i), v1(rows - 1 - i));
88 }
89
90 MatrixType m1_cr = m1.colwise().reverse();
91 // Verify that PartialRedux::reverse() works (for colwise())
92 for ( int i = 0; i < rows; i++ ) {
93 for ( int j = 0; j < cols; j++ ) {
94 VERIFY_IS_APPROX(m1_cr(i, j), m1(rows - 1 - i, j));
95 }
96 }
97
98 MatrixType m1_rr = m1.rowwise().reverse();
99 // Verify that PartialRedux::reverse() works (for rowwise())
100 for ( int i = 0; i < rows; i++ ) {
101 for ( int j = 0; j < cols; j++ ) {
102 VERIFY_IS_APPROX(m1_rr(i, j), m1(i, cols - 1 - j));
103 }
104 }
105
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000106 Scalar x = ei_random<Scalar>();
107
108 int r = ei_random<int>(0, rows-1),
109 c = ei_random<int>(0, cols-1);
110
111 m1.reverse()(r, c) = x;
112 VERIFY_IS_APPROX(x, m1(rows - 1 - r, cols - 1 - c));
113
Jitse Niesenc21390a2010-05-31 14:42:04 +0100114 /*
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000115 m1.colwise().reverse()(r, c) = x;
116 VERIFY_IS_APPROX(x, m1(rows - 1 - r, c));
117
118 m1.rowwise().reverse()(r, c) = x;
119 VERIFY_IS_APPROX(x, m1(r, cols - 1 - c));
120 */
121}
122
Gael Guennebaud0be89a42009-03-05 10:25:22 +0000123void test_array_reverse()
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000124{
125 for(int i = 0; i < g_repeat; i++) {
Benoit Jacob2840ac72009-10-28 18:19:29 -0400126 CALL_SUBTEST_1( reverse(Matrix<float, 1, 1>()) );
127 CALL_SUBTEST_2( reverse(Matrix2f()) );
128 CALL_SUBTEST_3( reverse(Matrix4f()) );
129 CALL_SUBTEST_4( reverse(Matrix4d()) );
130 CALL_SUBTEST_5( reverse(MatrixXcf(3, 3)) );
131 CALL_SUBTEST_6( reverse(MatrixXi(6, 3)) );
132 CALL_SUBTEST_7( reverse(MatrixXcd(20, 20)) );
133 CALL_SUBTEST_8( reverse(Matrix<float, 100, 100>()) );
134 CALL_SUBTEST_9( reverse(Matrix<float,Dynamic,Dynamic,RowMajor>(6,3)) );
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000135 }
Benoit Jacob2840ac72009-10-28 18:19:29 -0400136#ifdef EIGEN_TEST_PART_3
Gael Guennebaud4dc4ab32009-02-06 09:13:04 +0000137 Vector4f x; x << 1, 2, 3, 4;
138 Vector4f y; y << 4, 3, 2, 1;
139 VERIFY(x.reverse()[1] == 3);
140 VERIFY(x.reverse() == y);
Benoit Jacob2840ac72009-10-28 18:19:29 -0400141#endif
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000142}