array_reverse.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2009 Ricard Marxer <email@ricardmarxer.com>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #include "main.h"
00027 #include <iostream>
00028 
00029 using namespace std;
00030 
00031 template<typename MatrixType> void reverse(const MatrixType& m)
00032 {
00033   typedef typename MatrixType::Index Index;
00034   typedef typename MatrixType::Scalar Scalar;
00035   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00036 
00037   Index rows = m.rows();
00038   Index cols = m.cols();
00039 
00040   // this test relies a lot on Random.h, and there's not much more that we can do
00041   // to test it, hence I consider that we will have tested Random.h
00042   MatrixType m1 = MatrixType::Random(rows, cols);
00043   VectorType v1 = VectorType::Random(rows);
00044 
00045   MatrixType m1_r = m1.reverse();
00046   // Verify that MatrixBase::reverse() works
00047   for ( int i = 0; i < rows; i++ ) {
00048     for ( int j = 0; j < cols; j++ ) {
00049       VERIFY_IS_APPROX(m1_r(i, j), m1(rows - 1 - i, cols - 1 - j));
00050     }
00051   }
00052 
00053   Reverse<MatrixType> m1_rd(m1);
00054   // Verify that a Reverse default (in both directions) of an expression works
00055   for ( int i = 0; i < rows; i++ ) {
00056     for ( int j = 0; j < cols; j++ ) {
00057       VERIFY_IS_APPROX(m1_rd(i, j), m1(rows - 1 - i, cols - 1 - j));
00058     }
00059   }
00060 
00061   Reverse<MatrixType, BothDirections> m1_rb(m1);
00062   // Verify that a Reverse in both directions of an expression works
00063   for ( int i = 0; i < rows; i++ ) {
00064     for ( int j = 0; j < cols; j++ ) {
00065       VERIFY_IS_APPROX(m1_rb(i, j), m1(rows - 1 - i, cols - 1 - j));
00066     }
00067   }
00068 
00069   Reverse<MatrixType, Vertical> m1_rv(m1);
00070   // Verify that a Reverse in the vertical directions of an expression works
00071   for ( int i = 0; i < rows; i++ ) {
00072     for ( int j = 0; j < cols; j++ ) {
00073       VERIFY_IS_APPROX(m1_rv(i, j), m1(rows - 1 - i, j));
00074     }
00075   }
00076 
00077   Reverse<MatrixType, Horizontal> m1_rh(m1);
00078   // Verify that a Reverse in the horizontal directions of an expression works
00079   for ( int i = 0; i < rows; i++ ) {
00080     for ( int j = 0; j < cols; j++ ) {
00081       VERIFY_IS_APPROX(m1_rh(i, j), m1(i, cols - 1 - j));
00082     }
00083   }
00084 
00085   VectorType v1_r = v1.reverse();
00086   // Verify that a VectorType::reverse() of an expression works
00087   for ( int i = 0; i < rows; i++ ) {
00088     VERIFY_IS_APPROX(v1_r(i), v1(rows - 1 - i));
00089   }
00090 
00091   MatrixType m1_cr = m1.colwise().reverse();
00092   // Verify that PartialRedux::reverse() works (for colwise())
00093   for ( int i = 0; i < rows; i++ ) {
00094     for ( int j = 0; j < cols; j++ ) {
00095       VERIFY_IS_APPROX(m1_cr(i, j), m1(rows - 1 - i, j));
00096     }
00097   }
00098 
00099   MatrixType m1_rr = m1.rowwise().reverse();
00100   // Verify that PartialRedux::reverse() works (for rowwise())
00101   for ( int i = 0; i < rows; i++ ) {
00102     for ( int j = 0; j < cols; j++ ) {
00103       VERIFY_IS_APPROX(m1_rr(i, j), m1(i, cols - 1 - j));
00104     }
00105   }
00106 
00107   Scalar x = internal::random<Scalar>();
00108 
00109   Index r = internal::random<Index>(0, rows-1),
00110         c = internal::random<Index>(0, cols-1);
00111 
00112   m1.reverse()(r, c) = x;
00113   VERIFY_IS_APPROX(x, m1(rows - 1 - r, cols - 1 - c));
00114 
00115   /*
00116   m1.colwise().reverse()(r, c) = x;
00117   VERIFY_IS_APPROX(x, m1(rows - 1 - r, c));
00118 
00119   m1.rowwise().reverse()(r, c) = x;
00120   VERIFY_IS_APPROX(x, m1(r, cols - 1 - c));
00121   */
00122 }
00123 
00124 void test_array_reverse()
00125 {
00126   for(int i = 0; i < g_repeat; i++) {
00127     CALL_SUBTEST_1( reverse(Matrix<float, 1, 1>()) );
00128     CALL_SUBTEST_2( reverse(Matrix2f()) );
00129     CALL_SUBTEST_3( reverse(Matrix4f()) );
00130     CALL_SUBTEST_4( reverse(Matrix4d()) );
00131     CALL_SUBTEST_5( reverse(MatrixXcf(3, 3)) );
00132     CALL_SUBTEST_6( reverse(MatrixXi(6, 3)) );
00133     CALL_SUBTEST_7( reverse(MatrixXcd(20, 20)) );
00134     CALL_SUBTEST_8( reverse(Matrix<float, 100, 100>()) );
00135     CALL_SUBTEST_9( reverse(Matrix<float,Dynamic,Dynamic,RowMajor>(6,3)) );
00136   }
00137 #ifdef EIGEN_TEST_PART_3
00138   Vector4f x; x << 1, 2, 3, 4;
00139   Vector4f y; y << 4, 3, 2, 1;
00140   VERIFY(x.reverse()[1] == 3);
00141   VERIFY(x.reverse() == y);
00142 #endif
00143 }


libicr
Author(s): Robert Krug
autogenerated on Mon Jan 6 2014 11:32:28