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 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #include "main.h"
11 
12 // check minor separately in order to avoid the possible creation of a zero-sized
13 // array. Comes from a compilation error with gcc-3.4 or gcc-4 with -ansi -pedantic.
14 // Another solution would be to declare the array like this: T m_data[Size==0?1:Size]; in ei_matrix_storage
15 // but this is probably not bad to raise such an error at compile time...
16 template<typename Scalar, int _Rows, int _Cols> struct CheckMinor
17 {
18     typedef Matrix<Scalar, _Rows, _Cols> MatrixType;
CheckMinorCheckMinor19     CheckMinor(MatrixType& m1, int r1, int c1)
20     {
21         int rows = m1.rows();
22         int cols = m1.cols();
23 
24         Matrix<Scalar, Dynamic, Dynamic> mi = m1.minor(0,0).eval();
25         VERIFY_IS_APPROX(mi, m1.block(1,1,rows-1,cols-1));
26         mi = m1.minor(r1,c1);
27         VERIFY_IS_APPROX(mi.transpose(), m1.transpose().minor(c1,r1));
28         //check operator(), both constant and non-constant, on minor()
29         m1.minor(r1,c1)(0,0) = m1.minor(0,0)(0,0);
30     }
31 };
32 
33 template<typename Scalar> struct CheckMinor<Scalar,1,1>
34 {
35     typedef Matrix<Scalar, 1, 1> MatrixType;
CheckMinorCheckMinor36     CheckMinor(MatrixType&, int, int) {}
37 };
38 
submatrices(const MatrixType & m)39 template<typename MatrixType> void submatrices(const MatrixType& m)
40 {
41   /* this test covers the following files:
42      Row.h Column.h Block.h Minor.h DiagonalCoeffs.h
43   */
44   typedef typename MatrixType::Scalar Scalar;
45   typedef typename MatrixType::RealScalar RealScalar;
46   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
47   typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType;
48   int rows = m.rows();
49   int cols = m.cols();
50 
51   MatrixType m1 = MatrixType::Random(rows, cols),
52              m2 = MatrixType::Random(rows, cols),
53              m3(rows, cols),
54              ones = MatrixType::Ones(rows, cols),
55              square = Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime>
56                               ::Random(rows, rows);
57   VectorType v1 = VectorType::Random(rows);
58 
59   Scalar s1 = ei_random<Scalar>();
60 
61   int r1 = ei_random<int>(0,rows-1);
62   int r2 = ei_random<int>(r1,rows-1);
63   int c1 = ei_random<int>(0,cols-1);
64   int c2 = ei_random<int>(c1,cols-1);
65 
66   //check row() and col()
67   VERIFY_IS_APPROX(m1.col(c1).transpose(), m1.transpose().row(c1));
68   VERIFY_IS_APPROX(square.row(r1).eigen2_dot(m1.col(c1)), (square.lazy() * m1.conjugate())(r1,c1));
69   //check operator(), both constant and non-constant, on row() and col()
70   m1.row(r1) += s1 * m1.row(r2);
71   m1.col(c1) += s1 * m1.col(c2);
72 
73   //check block()
74   Matrix<Scalar,Dynamic,Dynamic> b1(1,1); b1(0,0) = m1(r1,c1);
75   RowVectorType br1(m1.block(r1,0,1,cols));
76   VectorType bc1(m1.block(0,c1,rows,1));
77   VERIFY_IS_APPROX(b1, m1.block(r1,c1,1,1));
78   VERIFY_IS_APPROX(m1.row(r1), br1);
79   VERIFY_IS_APPROX(m1.col(c1), bc1);
80   //check operator(), both constant and non-constant, on block()
81   m1.block(r1,c1,r2-r1+1,c2-c1+1) = s1 * m2.block(0, 0, r2-r1+1,c2-c1+1);
82   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);
83 
84   //check minor()
85   CheckMinor<Scalar, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> checkminor(m1,r1,c1);
86 
87   //check diagonal()
88   VERIFY_IS_APPROX(m1.diagonal(), m1.transpose().diagonal());
89   m2.diagonal() = 2 * m1.diagonal();
90   m2.diagonal()[0] *= 3;
91   VERIFY_IS_APPROX(m2.diagonal()[0], static_cast<Scalar>(6) * m1.diagonal()[0]);
92 
93   enum {
94     BlockRows = EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::RowsAtCompileTime,2),
95     BlockCols = EIGEN_SIZE_MIN_PREFER_FIXED(MatrixType::ColsAtCompileTime,5)
96   };
97   if (rows>=5 && cols>=8)
98   {
99     // test fixed block() as lvalue
100     m1.template block<BlockRows,BlockCols>(1,1) *= s1;
101     // test operator() on fixed block() both as constant and non-constant
102     m1.template block<BlockRows,BlockCols>(1,1)(0, 3) = m1.template block<2,5>(1,1)(1,2);
103     // check that fixed block() and block() agree
104     Matrix<Scalar,Dynamic,Dynamic> b = m1.template block<BlockRows,BlockCols>(3,3);
105     VERIFY_IS_APPROX(b, m1.block(3,3,BlockRows,BlockCols));
106   }
107 
108   if (rows>2)
109   {
110     // test sub vectors
111     VERIFY_IS_APPROX(v1.template start<2>(), v1.block(0,0,2,1));
112     VERIFY_IS_APPROX(v1.template start<2>(), v1.start(2));
113     VERIFY_IS_APPROX(v1.template start<2>(), v1.segment(0,2));
114     VERIFY_IS_APPROX(v1.template start<2>(), v1.template segment<2>(0));
115     int i = rows-2;
116     VERIFY_IS_APPROX(v1.template end<2>(), v1.block(i,0,2,1));
117     VERIFY_IS_APPROX(v1.template end<2>(), v1.end(2));
118     VERIFY_IS_APPROX(v1.template end<2>(), v1.segment(i,2));
119     VERIFY_IS_APPROX(v1.template end<2>(), v1.template segment<2>(i));
120     i = ei_random(0,rows-2);
121     VERIFY_IS_APPROX(v1.segment(i,2), v1.template segment<2>(i));
122   }
123 
124   // stress some basic stuffs with block matrices
125   VERIFY(ei_real(ones.col(c1).sum()) == RealScalar(rows));
126   VERIFY(ei_real(ones.row(r1).sum()) == RealScalar(cols));
127 
128   VERIFY(ei_real(ones.col(c1).eigen2_dot(ones.col(c2))) == RealScalar(rows));
129   VERIFY(ei_real(ones.row(r1).eigen2_dot(ones.row(r2))) == RealScalar(cols));
130 }
131 
test_eigen2_submatrices()132 void test_eigen2_submatrices()
133 {
134   for(int i = 0; i < g_repeat; i++) {
135     CALL_SUBTEST_1( submatrices(Matrix<float, 1, 1>()) );
136     CALL_SUBTEST_2( submatrices(Matrix4d()) );
137     CALL_SUBTEST_3( submatrices(MatrixXcf(3, 3)) );
138     CALL_SUBTEST_4( submatrices(MatrixXi(8, 12)) );
139     CALL_SUBTEST_5( submatrices(MatrixXcd(20, 20)) );
140     CALL_SUBTEST_6( submatrices(MatrixXf(20, 20)) );
141   }
142 }
143