blob: 144a0b088c77df734b1428991150c4d2a57a8357 [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{
Hauke Heibelf1679c72010-06-20 17:37:56 +020033 typedef typename MatrixType::Index Index;
Gael Guennebaud6fbca942009-02-06 09:01:50 +000034 typedef typename MatrixType::Scalar Scalar;
35 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
36
Hauke Heibelf1679c72010-06-20 17:37:56 +020037 Index rows = m.rows();
38 Index cols = m.cols();
Gael Guennebaud6fbca942009-02-06 09:01:50 +000039
40 // this test relies a lot on Random.h, and there's not much more that we can do
41 // to test it, hence I consider that we will have tested Random.h
42 MatrixType m1 = MatrixType::Random(rows, cols);
43 VectorType v1 = VectorType::Random(rows);
44
45 MatrixType m1_r = m1.reverse();
46 // Verify that MatrixBase::reverse() works
47 for ( int i = 0; i < rows; i++ ) {
48 for ( int j = 0; j < cols; j++ ) {
49 VERIFY_IS_APPROX(m1_r(i, j), m1(rows - 1 - i, cols - 1 - j));
50 }
51 }
52
53 Reverse<MatrixType> m1_rd(m1);
54 // Verify that a Reverse default (in both 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_rd(i, j), m1(rows - 1 - i, cols - 1 - j));
58 }
59 }
60
61 Reverse<MatrixType, BothDirections> m1_rb(m1);
62 // Verify that a Reverse in both 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_rb(i, j), m1(rows - 1 - i, cols - 1 - j));
66 }
67 }
68
69 Reverse<MatrixType, Vertical> m1_rv(m1);
70 // Verify that a Reverse in the vertical directions of an expression works
71 for ( int i = 0; i < rows; i++ ) {
72 for ( int j = 0; j < cols; j++ ) {
73 VERIFY_IS_APPROX(m1_rv(i, j), m1(rows - 1 - i, j));
74 }
75 }
76
77 Reverse<MatrixType, Horizontal> m1_rh(m1);
78 // Verify that a Reverse in the horizontal directions of an expression works
79 for ( int i = 0; i < rows; i++ ) {
80 for ( int j = 0; j < cols; j++ ) {
81 VERIFY_IS_APPROX(m1_rh(i, j), m1(i, cols - 1 - j));
82 }
83 }
84
85 VectorType v1_r = v1.reverse();
86 // Verify that a VectorType::reverse() of an expression works
87 for ( int i = 0; i < rows; i++ ) {
88 VERIFY_IS_APPROX(v1_r(i), v1(rows - 1 - i));
89 }
90
91 MatrixType m1_cr = m1.colwise().reverse();
92 // Verify that PartialRedux::reverse() works (for colwise())
93 for ( int i = 0; i < rows; i++ ) {
94 for ( int j = 0; j < cols; j++ ) {
95 VERIFY_IS_APPROX(m1_cr(i, j), m1(rows - 1 - i, j));
96 }
97 }
98
99 MatrixType m1_rr = m1.rowwise().reverse();
100 // Verify that PartialRedux::reverse() works (for rowwise())
101 for ( int i = 0; i < rows; i++ ) {
102 for ( int j = 0; j < cols; j++ ) {
103 VERIFY_IS_APPROX(m1_rr(i, j), m1(i, cols - 1 - j));
104 }
105 }
106
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000107 Scalar x = ei_random<Scalar>();
108
109 int r = ei_random<int>(0, rows-1),
110 c = ei_random<int>(0, cols-1);
111
112 m1.reverse()(r, c) = x;
113 VERIFY_IS_APPROX(x, m1(rows - 1 - r, cols - 1 - c));
114
Jitse Niesenc21390a2010-05-31 14:42:04 +0100115 /*
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000116 m1.colwise().reverse()(r, c) = x;
117 VERIFY_IS_APPROX(x, m1(rows - 1 - r, c));
118
119 m1.rowwise().reverse()(r, c) = x;
120 VERIFY_IS_APPROX(x, m1(r, cols - 1 - c));
121 */
122}
123
Gael Guennebaud0be89a42009-03-05 10:25:22 +0000124void test_array_reverse()
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000125{
126 for(int i = 0; i < g_repeat; i++) {
Benoit Jacob2840ac72009-10-28 18:19:29 -0400127 CALL_SUBTEST_1( reverse(Matrix<float, 1, 1>()) );
128 CALL_SUBTEST_2( reverse(Matrix2f()) );
129 CALL_SUBTEST_3( reverse(Matrix4f()) );
130 CALL_SUBTEST_4( reverse(Matrix4d()) );
131 CALL_SUBTEST_5( reverse(MatrixXcf(3, 3)) );
132 CALL_SUBTEST_6( reverse(MatrixXi(6, 3)) );
133 CALL_SUBTEST_7( reverse(MatrixXcd(20, 20)) );
134 CALL_SUBTEST_8( reverse(Matrix<float, 100, 100>()) );
135 CALL_SUBTEST_9( reverse(Matrix<float,Dynamic,Dynamic,RowMajor>(6,3)) );
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000136 }
Benoit Jacob2840ac72009-10-28 18:19:29 -0400137#ifdef EIGEN_TEST_PART_3
Gael Guennebaud4dc4ab32009-02-06 09:13:04 +0000138 Vector4f x; x << 1, 2, 3, 4;
139 Vector4f y; y << 4, 3, 2, 1;
140 VERIFY(x.reverse()[1] == 3);
141 VERIFY(x.reverse() == y);
Benoit Jacob2840ac72009-10-28 18:19:29 -0400142#endif
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000143}