1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2010 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#define COMPARE_CORNER(A,B) \
13  VERIFY_IS_EQUAL(matrix.A, matrix.B); \
14  VERIFY_IS_EQUAL(const_matrix.A, const_matrix.B);
15
16template<typename MatrixType> void corners(const MatrixType& m)
17{
18  typedef typename MatrixType::Index Index;
19  Index rows = m.rows();
20  Index cols = m.cols();
21
22  Index r = internal::random<Index>(1,rows);
23  Index c = internal::random<Index>(1,cols);
24
25  MatrixType matrix = MatrixType::Random(rows,cols);
26  const MatrixType const_matrix = MatrixType::Random(rows,cols);
27
28  COMPARE_CORNER(topLeftCorner(r,c), block(0,0,r,c));
29  COMPARE_CORNER(topRightCorner(r,c), block(0,cols-c,r,c));
30  COMPARE_CORNER(bottomLeftCorner(r,c), block(rows-r,0,r,c));
31  COMPARE_CORNER(bottomRightCorner(r,c), block(rows-r,cols-c,r,c));
32
33  Index sr = internal::random<Index>(1,rows) - 1;
34  Index nr = internal::random<Index>(1,rows-sr);
35  Index sc = internal::random<Index>(1,cols) - 1;
36  Index nc = internal::random<Index>(1,cols-sc);
37
38  COMPARE_CORNER(topRows(r), block(0,0,r,cols));
39  COMPARE_CORNER(middleRows(sr,nr), block(sr,0,nr,cols));
40  COMPARE_CORNER(bottomRows(r), block(rows-r,0,r,cols));
41  COMPARE_CORNER(leftCols(c), block(0,0,rows,c));
42  COMPARE_CORNER(middleCols(sc,nc), block(0,sc,rows,nc));
43  COMPARE_CORNER(rightCols(c), block(0,cols-c,rows,c));
44}
45
46template<typename MatrixType, int CRows, int CCols, int SRows, int SCols> void corners_fixedsize()
47{
48  MatrixType matrix = MatrixType::Random();
49  const MatrixType const_matrix = MatrixType::Random();
50
51  enum {
52    rows = MatrixType::RowsAtCompileTime,
53    cols = MatrixType::ColsAtCompileTime,
54    r = CRows,
55    c = CCols,
56	sr = SRows,
57	sc = SCols
58  };
59
60  VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template block<r,c>(0,0)));
61  VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template block<r,c>(0,cols-c)));
62  VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template block<r,c>(rows-r,0)));
63  VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template block<r,c>(rows-r,cols-c)));
64
65  VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template topLeftCorner<r,Dynamic>(r,c)));
66  VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template topRightCorner<r,Dynamic>(r,c)));
67  VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template bottomLeftCorner<r,Dynamic>(r,c)));
68  VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template bottomRightCorner<r,Dynamic>(r,c)));
69
70  VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template topLeftCorner<Dynamic,c>(r,c)));
71  VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template topRightCorner<Dynamic,c>(r,c)));
72  VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template bottomLeftCorner<Dynamic,c>(r,c)));
73  VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template bottomRightCorner<Dynamic,c>(r,c)));
74
75  VERIFY_IS_EQUAL((matrix.template topRows<r>()), (matrix.template block<r,cols>(0,0)));
76  VERIFY_IS_EQUAL((matrix.template middleRows<r>(sr)), (matrix.template block<r,cols>(sr,0)));
77  VERIFY_IS_EQUAL((matrix.template bottomRows<r>()), (matrix.template block<r,cols>(rows-r,0)));
78  VERIFY_IS_EQUAL((matrix.template leftCols<c>()), (matrix.template block<rows,c>(0,0)));
79  VERIFY_IS_EQUAL((matrix.template middleCols<c>(sc)), (matrix.template block<rows,c>(0,sc)));
80  VERIFY_IS_EQUAL((matrix.template rightCols<c>()), (matrix.template block<rows,c>(0,cols-c)));
81
82  VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template block<r,c>(0,0)));
83  VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template block<r,c>(0,cols-c)));
84  VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template block<r,c>(rows-r,0)));
85  VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template block<r,c>(rows-r,cols-c)));
86
87  VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template topLeftCorner<r,Dynamic>(r,c)));
88  VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template topRightCorner<r,Dynamic>(r,c)));
89  VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template bottomLeftCorner<r,Dynamic>(r,c)));
90  VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template bottomRightCorner<r,Dynamic>(r,c)));
91
92  VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template topLeftCorner<Dynamic,c>(r,c)));
93  VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template topRightCorner<Dynamic,c>(r,c)));
94  VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template bottomLeftCorner<Dynamic,c>(r,c)));
95  VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template bottomRightCorner<Dynamic,c>(r,c)));
96
97  VERIFY_IS_EQUAL((const_matrix.template topRows<r>()), (const_matrix.template block<r,cols>(0,0)));
98  VERIFY_IS_EQUAL((const_matrix.template middleRows<r>(sr)), (const_matrix.template block<r,cols>(sr,0)));
99  VERIFY_IS_EQUAL((const_matrix.template bottomRows<r>()), (const_matrix.template block<r,cols>(rows-r,0)));
100  VERIFY_IS_EQUAL((const_matrix.template leftCols<c>()), (const_matrix.template block<rows,c>(0,0)));
101  VERIFY_IS_EQUAL((const_matrix.template middleCols<c>(sc)), (const_matrix.template block<rows,c>(0,sc)));
102  VERIFY_IS_EQUAL((const_matrix.template rightCols<c>()), (const_matrix.template block<rows,c>(0,cols-c)));
103}
104
105void test_corners()
106{
107  for(int i = 0; i < g_repeat; i++) {
108    CALL_SUBTEST_1( corners(Matrix<float, 1, 1>()) );
109    CALL_SUBTEST_2( corners(Matrix4d()) );
110    CALL_SUBTEST_3( corners(Matrix<int,10,12>()) );
111    CALL_SUBTEST_4( corners(MatrixXcf(5, 7)) );
112    CALL_SUBTEST_5( corners(MatrixXf(21, 20)) );
113
114    CALL_SUBTEST_1(( corners_fixedsize<Matrix<float, 1, 1>, 1, 1, 0, 0>() ));
115    CALL_SUBTEST_2(( corners_fixedsize<Matrix4d,2,2,1,1>() ));
116    CALL_SUBTEST_3(( corners_fixedsize<Matrix<int,10,12>,4,7,5,2>() ));
117  }
118}
119