Gael Guennebaud | 6fbca94 | 2009-02-06 09:01:50 +0000 | [diff] [blame] | 1 | // 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 | |
| 29 | using namespace std; |
| 30 | |
| 31 | template<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 Guennebaud | 6fbca94 | 2009-02-06 09:01:50 +0000 | [diff] [blame] | 106 | /* |
| 107 | cout << "m1:" << endl << m1 << endl; |
| 108 | cout << "m1c_reversed:" << endl << m1c_reversed << endl; |
| 109 | |
| 110 | cout << "----------------" << endl; |
| 111 | |
| 112 | for ( int i=0; i< rows*cols; i++){ |
| 113 | cout << m1c_reversed.coeff(i) << endl; |
| 114 | } |
| 115 | |
| 116 | cout << "----------------" << endl; |
| 117 | |
| 118 | for ( int i=0; i< rows*cols; i++){ |
| 119 | cout << m1c_reversed.colwise().reverse().coeff(i) << endl; |
| 120 | } |
| 121 | |
| 122 | cout << "================" << endl; |
| 123 | |
| 124 | cout << "m1.coeff( ind ): " << m1.coeff( ind ) << endl; |
| 125 | cout << "m1c_reversed.colwise().reverse().coeff( ind ): " << m1c_reversed.colwise().reverse().coeff( ind ) << endl; |
| 126 | */ |
| 127 | |
| 128 | //MatrixType m1r_reversed = m1.rowwise().reverse(); |
| 129 | //VERIFY_IS_APPROX( m1r_reversed.rowwise().reverse().coeff( ind ), m1.coeff( ind ) ); |
| 130 | |
| 131 | /* |
| 132 | cout << "m1" << endl << m1 << endl; |
| 133 | cout << "m1 using coeff(int index)" << endl; |
| 134 | for ( int i = 0; i < rows*cols; i++) { |
| 135 | cout << m1.coeff(i) << " "; |
| 136 | } |
| 137 | cout << endl; |
| 138 | |
| 139 | cout << "m1.transpose()" << endl << m1.transpose() << endl; |
| 140 | cout << "m1.transpose() using coeff(int index)" << endl; |
| 141 | for ( int i = 0; i < rows*cols; i++) { |
| 142 | cout << m1.transpose().coeff(i) << " "; |
| 143 | } |
| 144 | cout << endl; |
| 145 | */ |
| 146 | /* |
| 147 | Scalar x = ei_random<Scalar>(); |
| 148 | |
| 149 | int r = ei_random<int>(0, rows-1), |
| 150 | c = ei_random<int>(0, cols-1); |
| 151 | |
| 152 | m1.reverse()(r, c) = x; |
| 153 | VERIFY_IS_APPROX(x, m1(rows - 1 - r, cols - 1 - c)); |
| 154 | |
| 155 | m1.colwise().reverse()(r, c) = x; |
| 156 | VERIFY_IS_APPROX(x, m1(rows - 1 - r, c)); |
| 157 | |
| 158 | m1.rowwise().reverse()(r, c) = x; |
| 159 | VERIFY_IS_APPROX(x, m1(r, cols - 1 - c)); |
| 160 | */ |
| 161 | } |
| 162 | |
Gael Guennebaud | 0be89a4 | 2009-03-05 10:25:22 +0000 | [diff] [blame] | 163 | void test_array_reverse() |
Gael Guennebaud | 6fbca94 | 2009-02-06 09:01:50 +0000 | [diff] [blame] | 164 | { |
| 165 | for(int i = 0; i < g_repeat; i++) { |
| 166 | CALL_SUBTEST( reverse(Matrix<float, 1, 1>()) ); |
Gael Guennebaud | f5d96df | 2009-02-06 12:40:38 +0000 | [diff] [blame] | 167 | CALL_SUBTEST( reverse(Matrix2f()) ); |
| 168 | CALL_SUBTEST( reverse(Matrix4f()) ); |
Gael Guennebaud | 6fbca94 | 2009-02-06 09:01:50 +0000 | [diff] [blame] | 169 | CALL_SUBTEST( reverse(Matrix4d()) ); |
| 170 | CALL_SUBTEST( reverse(MatrixXcf(3, 3)) ); |
Gael Guennebaud | f5d96df | 2009-02-06 12:40:38 +0000 | [diff] [blame] | 171 | CALL_SUBTEST( reverse(MatrixXi(6, 3)) ); |
Gael Guennebaud | 6fbca94 | 2009-02-06 09:01:50 +0000 | [diff] [blame] | 172 | CALL_SUBTEST( reverse(MatrixXcd(20, 20)) ); |
| 173 | CALL_SUBTEST( reverse(Matrix<float, 100, 100>()) ); |
Gael Guennebaud | f5d96df | 2009-02-06 12:40:38 +0000 | [diff] [blame] | 174 | CALL_SUBTEST( reverse(Matrix<float,Dynamic,Dynamic,RowMajor>(6,3)) ); |
Gael Guennebaud | 6fbca94 | 2009-02-06 09:01:50 +0000 | [diff] [blame] | 175 | } |
Gael Guennebaud | 4dc4ab3 | 2009-02-06 09:13:04 +0000 | [diff] [blame] | 176 | Vector4f x; x << 1, 2, 3, 4; |
| 177 | Vector4f y; y << 4, 3, 2, 1; |
| 178 | VERIFY(x.reverse()[1] == 3); |
| 179 | VERIFY(x.reverse() == y); |
| 180 | |
Gael Guennebaud | 6fbca94 | 2009-02-06 09:01:50 +0000 | [diff] [blame] | 181 | } |