blob: 03584b8127ab0c009de7642b8d54803c639b215b [file] [log] [blame]
Gael Guennebaud6fbca942009-02-06 09:01:50 +00001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra. Eigen itself is part of the KDE project.
3//
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
106 int ind = ei_random<int>(0, (rows*cols) - 1);
107
108 /* Reverse::coeff(int) is for vector only */
109 /*
110 MatrixType m1_reversed(m1.reverse());
111 VERIFY_IS_APPROX( m1_reversed.reverse().coeff( ind ), m1.coeff( ind ) );
112
113 MatrixType m1c_reversed = m1.colwise().reverse();
114 VERIFY_IS_APPROX( m1c_reversed.colwise().reverse().coeff( ind ), m1.coeff( ind ) );
115
116 MatrixType m1r_reversed = m1.rowwise().reverse();
117 VERIFY_IS_APPROX( m1r_reversed.rowwise().reverse().coeff( ind ), m1.coeff( ind ) );
118 */
119
120 /*
121 cout << "m1:" << endl << m1 << endl;
122 cout << "m1c_reversed:" << endl << m1c_reversed << endl;
123
124 cout << "----------------" << endl;
125
126 for ( int i=0; i< rows*cols; i++){
127 cout << m1c_reversed.coeff(i) << endl;
128 }
129
130 cout << "----------------" << endl;
131
132 for ( int i=0; i< rows*cols; i++){
133 cout << m1c_reversed.colwise().reverse().coeff(i) << endl;
134 }
135
136 cout << "================" << endl;
137
138 cout << "m1.coeff( ind ): " << m1.coeff( ind ) << endl;
139 cout << "m1c_reversed.colwise().reverse().coeff( ind ): " << m1c_reversed.colwise().reverse().coeff( ind ) << endl;
140 */
141
142 //MatrixType m1r_reversed = m1.rowwise().reverse();
143 //VERIFY_IS_APPROX( m1r_reversed.rowwise().reverse().coeff( ind ), m1.coeff( ind ) );
144
145 /*
146 cout << "m1" << endl << m1 << endl;
147 cout << "m1 using coeff(int index)" << endl;
148 for ( int i = 0; i < rows*cols; i++) {
149 cout << m1.coeff(i) << " ";
150 }
151 cout << endl;
152
153 cout << "m1.transpose()" << endl << m1.transpose() << endl;
154 cout << "m1.transpose() using coeff(int index)" << endl;
155 for ( int i = 0; i < rows*cols; i++) {
156 cout << m1.transpose().coeff(i) << " ";
157 }
158 cout << endl;
159 */
160 /*
161 Scalar x = ei_random<Scalar>();
162
163 int r = ei_random<int>(0, rows-1),
164 c = ei_random<int>(0, cols-1);
165
166 m1.reverse()(r, c) = x;
167 VERIFY_IS_APPROX(x, m1(rows - 1 - r, cols - 1 - c));
168
169 m1.colwise().reverse()(r, c) = x;
170 VERIFY_IS_APPROX(x, m1(rows - 1 - r, c));
171
172 m1.rowwise().reverse()(r, c) = x;
173 VERIFY_IS_APPROX(x, m1(r, cols - 1 - c));
174 */
175}
176
177void test_reverse()
178{
179 for(int i = 0; i < g_repeat; i++) {
180 CALL_SUBTEST( reverse(Matrix<float, 1, 1>()) );
181 CALL_SUBTEST( reverse(Matrix4d()) );
182 CALL_SUBTEST( reverse(MatrixXcf(3, 3)) );
183 CALL_SUBTEST( reverse(MatrixXi(8, 12)) );
184 CALL_SUBTEST( reverse(MatrixXcd(20, 20)) );
185 CALL_SUBTEST( reverse(Matrix<float, 100, 100>()) );
186 CALL_SUBTEST( reverse(Matrix<long double,Dynamic,Dynamic>(10,10)) );
187 }
188}