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 #include "main.h"
00026
00027
00028
00029
00030
00031 template<typename Scalar, int _Rows, int _Cols> struct CheckMinor
00032 {
00033 typedef Matrix<Scalar, _Rows, _Cols> MatrixType;
00034 CheckMinor(MatrixType& m1, int r1, int c1)
00035 {
00036 int rows = m1.rows();
00037 int cols = m1.cols();
00038
00039 Matrix<Scalar, Dynamic, Dynamic> mi = m1.minor(0,0).eval();
00040 VERIFY_IS_APPROX(mi, m1.block(1,1,rows-1,cols-1));
00041 mi = m1.minor(r1,c1);
00042 VERIFY_IS_APPROX(mi.transpose(), m1.transpose().minor(c1,r1));
00043
00044 m1.minor(r1,c1)(0,0) = m1.minor(0,0)(0,0);
00045 }
00046 };
00047
00048 template<typename Scalar> struct CheckMinor<Scalar,1,1>
00049 {
00050 typedef Matrix<Scalar, 1, 1> MatrixType;
00051 CheckMinor(MatrixType&, int, int) {}
00052 };
00053
00054 template<typename MatrixType> void submatrices(const MatrixType& m)
00055 {
00056
00057
00058
00059 typedef typename MatrixType::Scalar Scalar;
00060 typedef typename MatrixType::RealScalar RealScalar;
00061 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00062 typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
00063 int rows = m.rows();
00064 int cols = m.cols();
00065
00066 MatrixType m1 = MatrixType::Random(rows, cols),
00067 m2 = MatrixType::Random(rows, cols),
00068 m3(rows, cols),
00069 mzero = MatrixType::Zero(rows, cols),
00070 ones = MatrixType::Ones(rows, cols),
00071 identity = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00072 ::Identity(rows, rows),
00073 square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
00074 ::Random(rows, rows);
00075 VectorType v1 = VectorType::Random(rows),
00076 v2 = VectorType::Random(rows),
00077 v3 = VectorType::Random(rows),
00078 vzero = VectorType::Zero(rows);
00079
00080 Scalar s1 = ei_random<Scalar>();
00081
00082 int r1 = ei_random<int>(0,rows-1);
00083 int r2 = ei_random<int>(r1,rows-1);
00084 int c1 = ei_random<int>(0,cols-1);
00085 int c2 = ei_random<int>(c1,cols-1);
00086
00087
00088 VERIFY_IS_APPROX(m1.col(c1).transpose(), m1.transpose().row(c1));
00089 VERIFY_IS_APPROX(square.row(r1).eigen2_dot(m1.col(c1)), (square.lazy() * m1.conjugate())(r1,c1));
00090
00091 m1.row(r1) += s1 * m1.row(r2);
00092 m1.col(c1) += s1 * m1.col(c2);
00093
00094
00095 Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1);
00096 RowVectorType br1(m1.block(r1,0,1,cols));
00097 VectorType bc1(m1.block(0,c1,rows,1));
00098 VERIFY_IS_APPROX(b1, m1.block(r1,c1,1,1));
00099 VERIFY_IS_APPROX(m1.row(r1), br1);
00100 VERIFY_IS_APPROX(m1.col(c1), bc1);
00101
00102 m1.block(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.block(0, 0, r2-r1+1,c2-c1+1);
00103 m1.block(r1,c1,r2-r1+1,c2-c1+1)(r2-r1,c2-c1) = m2.block(0, 0, r2-r1+1,c2-c1+1)(0,0);
00104
00105
00106 CheckMinor<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> checkminor(m1,r1,c1);
00107
00108
00109 VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal());
00110 m2.diagonal() = 2 * m1.diagonal();
00111 m2.diagonal()[0] *= 3;
00112 VERIFY_IS_APPROX(m2.diagonal()[0], static_cast<Scalar>(6) * m1.diagonal()[0]);
00113
00114 enum {
00115 BlockRows = EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::RowsAtCompileTime,2),
00116 BlockCols = EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::ColsAtCompileTime,5)
00117 };
00118 if (rows>=5 && cols>=8)
00119 {
00120
00121 m1.template block<BlockRows,BlockCols>(1,1) *= s1;
00122
00123 m1.template block<BlockRows,BlockCols>(1,1)(0, 3) = m1.template block<2,5>(1,1)(1,2);
00124
00125 Matrix<Scalar,Dynamic,Dynamic> b = m1.template block<BlockRows,BlockCols>(3,3);
00126 VERIFY_IS_APPROX(b, m1.block(3,3,BlockRows,BlockCols));
00127 }
00128
00129 if (rows>2)
00130 {
00131
00132 VERIFY_IS_APPROX(v1.template start<2>(), v1.block(0,0,2,1));
00133 VERIFY_IS_APPROX(v1.template start<2>(), v1.start(2));
00134 VERIFY_IS_APPROX(v1.template start<2>(), v1.segment(0,2));
00135 VERIFY_IS_APPROX(v1.template start<2>(), v1.template segment<2>(0));
00136 int i = rows-2;
00137 VERIFY_IS_APPROX(v1.template end<2>(), v1.block(i,0,2,1));
00138 VERIFY_IS_APPROX(v1.template end<2>(), v1.end(2));
00139 VERIFY_IS_APPROX(v1.template end<2>(), v1.segment(i,2));
00140 VERIFY_IS_APPROX(v1.template end<2>(), v1.template segment<2>(i));
00141 i = ei_random(0,rows-2);
00142 VERIFY_IS_APPROX(v1.segment(i,2), v1.template segment<2>(i));
00143 }
00144
00145
00146 VERIFY(ei_real(ones.col(c1).sum()) == RealScalar(rows));
00147 VERIFY(ei_real(ones.row(r1).sum()) == RealScalar(cols));
00148
00149 VERIFY(ei_real(ones.col(c1).eigen2_dot(ones.col(c2))) == RealScalar(rows));
00150 VERIFY(ei_real(ones.row(r1).eigen2_dot(ones.row(r2))) == RealScalar(cols));
00151 }
00152
00153 void test_eigen2_submatrices()
00154 {
00155 for(int i = 0; i < g_repeat; i++) {
00156 CALL_SUBTEST_1( submatrices(Matrix<float, 1, 1>()) );
00157 CALL_SUBTEST_2( submatrices(Matrix4d()) );
00158 CALL_SUBTEST_3( submatrices(MatrixXcf(3, 3)) );
00159 CALL_SUBTEST_4( submatrices(MatrixXi(8, 12)) );
00160 CALL_SUBTEST_5( submatrices(MatrixXcd(20, 20)) );
00161 CALL_SUBTEST_6( submatrices(MatrixXf(20, 20)) );
00162 }
00163 }