1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2006-2010 Benoit Jacob <[email protected]>
5*bf2c3715SXin Li //
6*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
7*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
8*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9*bf2c3715SXin Li
10*bf2c3715SXin Li #include "main.h"
11*bf2c3715SXin Li
12*bf2c3715SXin Li #define COMPARE_CORNER(A,B) \
13*bf2c3715SXin Li VERIFY_IS_EQUAL(matrix.A, matrix.B); \
14*bf2c3715SXin Li VERIFY_IS_EQUAL(const_matrix.A, const_matrix.B);
15*bf2c3715SXin Li
corners(const MatrixType & m)16*bf2c3715SXin Li template<typename MatrixType> void corners(const MatrixType& m)
17*bf2c3715SXin Li {
18*bf2c3715SXin Li Index rows = m.rows();
19*bf2c3715SXin Li Index cols = m.cols();
20*bf2c3715SXin Li
21*bf2c3715SXin Li Index r = internal::random<Index>(1,rows);
22*bf2c3715SXin Li Index c = internal::random<Index>(1,cols);
23*bf2c3715SXin Li
24*bf2c3715SXin Li MatrixType matrix = MatrixType::Random(rows,cols);
25*bf2c3715SXin Li const MatrixType const_matrix = MatrixType::Random(rows,cols);
26*bf2c3715SXin Li
27*bf2c3715SXin Li COMPARE_CORNER(topLeftCorner(r,c), block(0,0,r,c));
28*bf2c3715SXin Li COMPARE_CORNER(topRightCorner(r,c), block(0,cols-c,r,c));
29*bf2c3715SXin Li COMPARE_CORNER(bottomLeftCorner(r,c), block(rows-r,0,r,c));
30*bf2c3715SXin Li COMPARE_CORNER(bottomRightCorner(r,c), block(rows-r,cols-c,r,c));
31*bf2c3715SXin Li
32*bf2c3715SXin Li Index sr = internal::random<Index>(1,rows) - 1;
33*bf2c3715SXin Li Index nr = internal::random<Index>(1,rows-sr);
34*bf2c3715SXin Li Index sc = internal::random<Index>(1,cols) - 1;
35*bf2c3715SXin Li Index nc = internal::random<Index>(1,cols-sc);
36*bf2c3715SXin Li
37*bf2c3715SXin Li COMPARE_CORNER(topRows(r), block(0,0,r,cols));
38*bf2c3715SXin Li COMPARE_CORNER(middleRows(sr,nr), block(sr,0,nr,cols));
39*bf2c3715SXin Li COMPARE_CORNER(bottomRows(r), block(rows-r,0,r,cols));
40*bf2c3715SXin Li COMPARE_CORNER(leftCols(c), block(0,0,rows,c));
41*bf2c3715SXin Li COMPARE_CORNER(middleCols(sc,nc), block(0,sc,rows,nc));
42*bf2c3715SXin Li COMPARE_CORNER(rightCols(c), block(0,cols-c,rows,c));
43*bf2c3715SXin Li }
44*bf2c3715SXin Li
corners_fixedsize()45*bf2c3715SXin Li template<typename MatrixType, int CRows, int CCols, int SRows, int SCols> void corners_fixedsize()
46*bf2c3715SXin Li {
47*bf2c3715SXin Li MatrixType matrix = MatrixType::Random();
48*bf2c3715SXin Li const MatrixType const_matrix = MatrixType::Random();
49*bf2c3715SXin Li
50*bf2c3715SXin Li enum {
51*bf2c3715SXin Li rows = MatrixType::RowsAtCompileTime,
52*bf2c3715SXin Li cols = MatrixType::ColsAtCompileTime,
53*bf2c3715SXin Li r = CRows,
54*bf2c3715SXin Li c = CCols,
55*bf2c3715SXin Li sr = SRows,
56*bf2c3715SXin Li sc = SCols
57*bf2c3715SXin Li };
58*bf2c3715SXin Li
59*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template block<r,c>(0,0)));
60*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template block<r,c>(0,cols-c)));
61*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template block<r,c>(rows-r,0)));
62*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template block<r,c>(rows-r,cols-c)));
63*bf2c3715SXin Li
64*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template topLeftCorner<r,Dynamic>(r,c)));
65*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template topRightCorner<r,Dynamic>(r,c)));
66*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template bottomLeftCorner<r,Dynamic>(r,c)));
67*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template bottomRightCorner<r,Dynamic>(r,c)));
68*bf2c3715SXin Li
69*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template topLeftCorner<r,c>()), (matrix.template topLeftCorner<Dynamic,c>(r,c)));
70*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template topRightCorner<r,c>()), (matrix.template topRightCorner<Dynamic,c>(r,c)));
71*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r,c>()), (matrix.template bottomLeftCorner<Dynamic,c>(r,c)));
72*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r,c>()), (matrix.template bottomRightCorner<Dynamic,c>(r,c)));
73*bf2c3715SXin Li
74*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template topRows<r>()), (matrix.template block<r,cols>(0,0)));
75*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template middleRows<r>(sr)), (matrix.template block<r,cols>(sr,0)));
76*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template bottomRows<r>()), (matrix.template block<r,cols>(rows-r,0)));
77*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template leftCols<c>()), (matrix.template block<rows,c>(0,0)));
78*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template middleCols<c>(sc)), (matrix.template block<rows,c>(0,sc)));
79*bf2c3715SXin Li VERIFY_IS_EQUAL((matrix.template rightCols<c>()), (matrix.template block<rows,c>(0,cols-c)));
80*bf2c3715SXin Li
81*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template block<r,c>(0,0)));
82*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template block<r,c>(0,cols-c)));
83*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template block<r,c>(rows-r,0)));
84*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template block<r,c>(rows-r,cols-c)));
85*bf2c3715SXin Li
86*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template topLeftCorner<r,Dynamic>(r,c)));
87*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template topRightCorner<r,Dynamic>(r,c)));
88*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template bottomLeftCorner<r,Dynamic>(r,c)));
89*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template bottomRightCorner<r,Dynamic>(r,c)));
90*bf2c3715SXin Li
91*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r,c>()), (const_matrix.template topLeftCorner<Dynamic,c>(r,c)));
92*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r,c>()), (const_matrix.template topRightCorner<Dynamic,c>(r,c)));
93*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r,c>()), (const_matrix.template bottomLeftCorner<Dynamic,c>(r,c)));
94*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r,c>()), (const_matrix.template bottomRightCorner<Dynamic,c>(r,c)));
95*bf2c3715SXin Li
96*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template topRows<r>()), (const_matrix.template block<r,cols>(0,0)));
97*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template middleRows<r>(sr)), (const_matrix.template block<r,cols>(sr,0)));
98*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template bottomRows<r>()), (const_matrix.template block<r,cols>(rows-r,0)));
99*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template leftCols<c>()), (const_matrix.template block<rows,c>(0,0)));
100*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template middleCols<c>(sc)), (const_matrix.template block<rows,c>(0,sc)));
101*bf2c3715SXin Li VERIFY_IS_EQUAL((const_matrix.template rightCols<c>()), (const_matrix.template block<rows,c>(0,cols-c)));
102*bf2c3715SXin Li }
103*bf2c3715SXin Li
EIGEN_DECLARE_TEST(corners)104*bf2c3715SXin Li EIGEN_DECLARE_TEST(corners)
105*bf2c3715SXin Li {
106*bf2c3715SXin Li for(int i = 0; i < g_repeat; i++) {
107*bf2c3715SXin Li CALL_SUBTEST_1( corners(Matrix<float, 1, 1>()) );
108*bf2c3715SXin Li CALL_SUBTEST_2( corners(Matrix4d()) );
109*bf2c3715SXin Li CALL_SUBTEST_3( corners(Matrix<int,10,12>()) );
110*bf2c3715SXin Li CALL_SUBTEST_4( corners(MatrixXcf(5, 7)) );
111*bf2c3715SXin Li CALL_SUBTEST_5( corners(MatrixXf(21, 20)) );
112*bf2c3715SXin Li
113*bf2c3715SXin Li CALL_SUBTEST_1(( corners_fixedsize<Matrix<float, 1, 1>, 1, 1, 0, 0>() ));
114*bf2c3715SXin Li CALL_SUBTEST_2(( corners_fixedsize<Matrix4d,2,2,1,1>() ));
115*bf2c3715SXin Li CALL_SUBTEST_3(( corners_fixedsize<Matrix<int,10,12>,4,7,5,2>() ));
116*bf2c3715SXin Li }
117*bf2c3715SXin Li }
118