blob: 9ba19246bd207a8336c49f8f2e32bee44daba7c1 [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{
Hauke Heibelf1679c72010-06-20 17:37:56 +020018 typedef typename MatrixType::Index Index;
Gael Guennebaud6fbca942009-02-06 09:01:50 +000019 typedef typename MatrixType::Scalar Scalar;
20 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
21
Hauke Heibelf1679c72010-06-20 17:37:56 +020022 Index rows = m.rows();
23 Index cols = m.cols();
Gael Guennebaud6fbca942009-02-06 09:01:50 +000024
25 // this test relies a lot on Random.h, and there's not much more that we can do
26 // to test it, hence I consider that we will have tested Random.h
Gael Guennebauddfb674a2015-03-31 20:17:10 +020027 MatrixType m1 = MatrixType::Random(rows, cols), m2;
Gael Guennebaud6fbca942009-02-06 09:01:50 +000028 VectorType v1 = VectorType::Random(rows);
29
30 MatrixType m1_r = m1.reverse();
31 // Verify that MatrixBase::reverse() works
32 for ( int i = 0; i < rows; i++ ) {
33 for ( int j = 0; j < cols; j++ ) {
34 VERIFY_IS_APPROX(m1_r(i, j), m1(rows - 1 - i, cols - 1 - j));
35 }
36 }
37
38 Reverse<MatrixType> m1_rd(m1);
39 // Verify that a Reverse default (in both directions) of an expression works
40 for ( int i = 0; i < rows; i++ ) {
41 for ( int j = 0; j < cols; j++ ) {
42 VERIFY_IS_APPROX(m1_rd(i, j), m1(rows - 1 - i, cols - 1 - j));
43 }
44 }
45
46 Reverse<MatrixType, BothDirections> m1_rb(m1);
47 // Verify that a Reverse in both directions of an expression works
48 for ( int i = 0; i < rows; i++ ) {
49 for ( int j = 0; j < cols; j++ ) {
50 VERIFY_IS_APPROX(m1_rb(i, j), m1(rows - 1 - i, cols - 1 - j));
51 }
52 }
53
54 Reverse<MatrixType, Vertical> m1_rv(m1);
55 // Verify that a Reverse in the vertical directions of an expression works
56 for ( int i = 0; i < rows; i++ ) {
57 for ( int j = 0; j < cols; j++ ) {
58 VERIFY_IS_APPROX(m1_rv(i, j), m1(rows - 1 - i, j));
59 }
60 }
61
62 Reverse<MatrixType, Horizontal> m1_rh(m1);
63 // Verify that a Reverse in the horizontal directions of an expression works
64 for ( int i = 0; i < rows; i++ ) {
65 for ( int j = 0; j < cols; j++ ) {
66 VERIFY_IS_APPROX(m1_rh(i, j), m1(i, cols - 1 - j));
67 }
68 }
69
70 VectorType v1_r = v1.reverse();
71 // Verify that a VectorType::reverse() of an expression works
72 for ( int i = 0; i < rows; i++ ) {
73 VERIFY_IS_APPROX(v1_r(i), v1(rows - 1 - i));
74 }
75
76 MatrixType m1_cr = m1.colwise().reverse();
77 // Verify that PartialRedux::reverse() works (for colwise())
78 for ( int i = 0; i < rows; i++ ) {
79 for ( int j = 0; j < cols; j++ ) {
80 VERIFY_IS_APPROX(m1_cr(i, j), m1(rows - 1 - i, j));
81 }
82 }
83
84 MatrixType m1_rr = m1.rowwise().reverse();
85 // Verify that PartialRedux::reverse() works (for rowwise())
86 for ( int i = 0; i < rows; i++ ) {
87 for ( int j = 0; j < cols; j++ ) {
88 VERIFY_IS_APPROX(m1_rr(i, j), m1(i, cols - 1 - j));
89 }
90 }
91
Benoit Jacob47160402010-10-25 10:15:22 -040092 Scalar x = internal::random<Scalar>();
Gael Guennebaud6fbca942009-02-06 09:01:50 +000093
Benoit Jacob47160402010-10-25 10:15:22 -040094 Index r = internal::random<Index>(0, rows-1),
95 c = internal::random<Index>(0, cols-1);
Gael Guennebaud6fbca942009-02-06 09:01:50 +000096
97 m1.reverse()(r, c) = x;
98 VERIFY_IS_APPROX(x, m1(rows - 1 - r, cols - 1 - c));
Gael Guennebauddfb674a2015-03-31 20:17:10 +020099
100 m2 = m1;
101 m2.reverseInPlace();
102 VERIFY_IS_APPROX(m2,m1.reverse().eval());
103
104 m2 = m1;
105 m2.col(0).reverseInPlace();
106 VERIFY_IS_APPROX(m2.col(0),m1.col(0).reverse().eval());
107
108 m2 = m1;
109 m2.row(0).reverseInPlace();
110 VERIFY_IS_APPROX(m2.row(0),m1.row(0).reverse().eval());
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000111
Jitse Niesenc21390a2010-05-31 14:42:04 +0100112 /*
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000113 m1.colwise().reverse()(r, c) = x;
114 VERIFY_IS_APPROX(x, m1(rows - 1 - r, c));
115
116 m1.rowwise().reverse()(r, c) = x;
117 VERIFY_IS_APPROX(x, m1(r, cols - 1 - c));
118 */
119}
120
Gael Guennebaud0be89a42009-03-05 10:25:22 +0000121void test_array_reverse()
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000122{
123 for(int i = 0; i < g_repeat; i++) {
Benoit Jacob2840ac72009-10-28 18:19:29 -0400124 CALL_SUBTEST_1( reverse(Matrix<float, 1, 1>()) );
125 CALL_SUBTEST_2( reverse(Matrix2f()) );
126 CALL_SUBTEST_3( reverse(Matrix4f()) );
127 CALL_SUBTEST_4( reverse(Matrix4d()) );
Gael Guennebauddfb674a2015-03-31 20:17:10 +0200128 CALL_SUBTEST_5( reverse(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
129 CALL_SUBTEST_6( reverse(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
130 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 -0400131 CALL_SUBTEST_8( reverse(Matrix<float, 100, 100>()) );
Gael Guennebauddfb674a2015-03-31 20:17:10 +0200132 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 +0000133 }
Benoit Jacob2840ac72009-10-28 18:19:29 -0400134#ifdef EIGEN_TEST_PART_3
Gael Guennebaud4dc4ab32009-02-06 09:13:04 +0000135 Vector4f x; x << 1, 2, 3, 4;
136 Vector4f y; y << 4, 3, 2, 1;
137 VERIFY(x.reverse()[1] == 3);
138 VERIFY(x.reverse() == y);
Benoit Jacob2840ac72009-10-28 18:19:29 -0400139#endif
Gael Guennebaud6fbca942009-02-06 09:01:50 +0000140}