00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00041
00042 MatrixType m1 = MatrixType::Random(rows, cols);
00043 VectorType v1 = VectorType::Random(rows);
00044
00045 MatrixType m1_r = m1.reverse();
00046
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
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
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
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
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
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
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
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
00117
00118
00119
00120
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 }