blob: 8bbb3f854311d9952fd6061e3c58360fb18899cd [file] [log] [blame]
Googler45874d82019-08-21 12:06:47 -07001// 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
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080012#define COMPARE_CORNER(A, B) \
Googler45874d82019-08-21 12:06:47 -070013 VERIFY_IS_EQUAL(matrix.A, matrix.B); \
14 VERIFY_IS_EQUAL(const_matrix.A, const_matrix.B);
15
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080016template <typename MatrixType>
17void corners(const MatrixType& m) {
Googler45874d82019-08-21 12:06:47 -070018 Index rows = m.rows();
19 Index cols = m.cols();
20
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080021 Index r = internal::random<Index>(1, rows);
22 Index c = internal::random<Index>(1, cols);
Googler45874d82019-08-21 12:06:47 -070023
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080024 MatrixType matrix = MatrixType::Random(rows, cols);
25 const MatrixType const_matrix = MatrixType::Random(rows, cols);
Googler45874d82019-08-21 12:06:47 -070026
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080027 COMPARE_CORNER(topLeftCorner(r, c), block(0, 0, r, c));
28 COMPARE_CORNER(topRightCorner(r, c), block(0, cols - c, r, c));
29 COMPARE_CORNER(bottomLeftCorner(r, c), block(rows - r, 0, r, c));
30 COMPARE_CORNER(bottomRightCorner(r, c), block(rows - r, cols - c, r, c));
Googler45874d82019-08-21 12:06:47 -070031
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080032 Index sr = internal::random<Index>(1, rows) - 1;
33 Index nr = internal::random<Index>(1, rows - sr);
34 Index sc = internal::random<Index>(1, cols) - 1;
35 Index nc = internal::random<Index>(1, cols - sc);
Googler45874d82019-08-21 12:06:47 -070036
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080037 COMPARE_CORNER(topRows(r), block(0, 0, r, cols));
38 COMPARE_CORNER(middleRows(sr, nr), block(sr, 0, nr, cols));
39 COMPARE_CORNER(bottomRows(r), block(rows - r, 0, r, cols));
40 COMPARE_CORNER(leftCols(c), block(0, 0, rows, c));
41 COMPARE_CORNER(middleCols(sc, nc), block(0, sc, rows, nc));
42 COMPARE_CORNER(rightCols(c), block(0, cols - c, rows, c));
Googler45874d82019-08-21 12:06:47 -070043}
44
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080045template <typename MatrixType, int CRows, int CCols, int SRows, int SCols>
46void corners_fixedsize() {
Googler45874d82019-08-21 12:06:47 -070047 MatrixType matrix = MatrixType::Random();
48 const MatrixType const_matrix = MatrixType::Random();
49
50 enum {
51 rows = MatrixType::RowsAtCompileTime,
52 cols = MatrixType::ColsAtCompileTime,
53 r = CRows,
54 c = CCols,
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080055 sr = SRows,
56 sc = SCols
Googler45874d82019-08-21 12:06:47 -070057 };
58
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080059 VERIFY_IS_EQUAL((matrix.template topLeftCorner<r, c>()), (matrix.template block<r, c>(0, 0)));
60 VERIFY_IS_EQUAL((matrix.template topRightCorner<r, c>()), (matrix.template block<r, c>(0, cols - c)));
61 VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r, c>()), (matrix.template block<r, c>(rows - r, 0)));
62 VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r, c>()), (matrix.template block<r, c>(rows - r, cols - c)));
Googler45874d82019-08-21 12:06:47 -070063
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080064 VERIFY_IS_EQUAL((matrix.template topLeftCorner<r, c>()), (matrix.template topLeftCorner<r, Dynamic>(r, c)));
65 VERIFY_IS_EQUAL((matrix.template topRightCorner<r, c>()), (matrix.template topRightCorner<r, Dynamic>(r, c)));
66 VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r, c>()), (matrix.template bottomLeftCorner<r, Dynamic>(r, c)));
67 VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r, c>()), (matrix.template bottomRightCorner<r, Dynamic>(r, c)));
Googler45874d82019-08-21 12:06:47 -070068
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080069 VERIFY_IS_EQUAL((matrix.template topLeftCorner<r, c>()), (matrix.template topLeftCorner<Dynamic, c>(r, c)));
70 VERIFY_IS_EQUAL((matrix.template topRightCorner<r, c>()), (matrix.template topRightCorner<Dynamic, c>(r, c)));
71 VERIFY_IS_EQUAL((matrix.template bottomLeftCorner<r, c>()), (matrix.template bottomLeftCorner<Dynamic, c>(r, c)));
72 VERIFY_IS_EQUAL((matrix.template bottomRightCorner<r, c>()), (matrix.template bottomRightCorner<Dynamic, c>(r, c)));
Googler45874d82019-08-21 12:06:47 -070073
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080074 VERIFY_IS_EQUAL((matrix.template topRows<r>()), (matrix.template block<r, cols>(0, 0)));
75 VERIFY_IS_EQUAL((matrix.template middleRows<r>(sr)), (matrix.template block<r, cols>(sr, 0)));
76 VERIFY_IS_EQUAL((matrix.template bottomRows<r>()), (matrix.template block<r, cols>(rows - r, 0)));
77 VERIFY_IS_EQUAL((matrix.template leftCols<c>()), (matrix.template block<rows, c>(0, 0)));
78 VERIFY_IS_EQUAL((matrix.template middleCols<c>(sc)), (matrix.template block<rows, c>(0, sc)));
79 VERIFY_IS_EQUAL((matrix.template rightCols<c>()), (matrix.template block<rows, c>(0, cols - c)));
Googler45874d82019-08-21 12:06:47 -070080
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080081 VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r, c>()), (const_matrix.template block<r, c>(0, 0)));
82 VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r, c>()), (const_matrix.template block<r, c>(0, cols - c)));
83 VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r, c>()), (const_matrix.template block<r, c>(rows - r, 0)));
84 VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r, c>()),
85 (const_matrix.template block<r, c>(rows - r, cols - c)));
Googler45874d82019-08-21 12:06:47 -070086
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080087 VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r, c>()),
88 (const_matrix.template topLeftCorner<r, Dynamic>(r, c)));
89 VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r, c>()),
90 (const_matrix.template topRightCorner<r, Dynamic>(r, c)));
91 VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r, c>()),
92 (const_matrix.template bottomLeftCorner<r, Dynamic>(r, c)));
93 VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r, c>()),
94 (const_matrix.template bottomRightCorner<r, Dynamic>(r, c)));
Googler45874d82019-08-21 12:06:47 -070095
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -080096 VERIFY_IS_EQUAL((const_matrix.template topLeftCorner<r, c>()),
97 (const_matrix.template topLeftCorner<Dynamic, c>(r, c)));
98 VERIFY_IS_EQUAL((const_matrix.template topRightCorner<r, c>()),
99 (const_matrix.template topRightCorner<Dynamic, c>(r, c)));
100 VERIFY_IS_EQUAL((const_matrix.template bottomLeftCorner<r, c>()),
101 (const_matrix.template bottomLeftCorner<Dynamic, c>(r, c)));
102 VERIFY_IS_EQUAL((const_matrix.template bottomRightCorner<r, c>()),
103 (const_matrix.template bottomRightCorner<Dynamic, c>(r, c)));
Googler45874d82019-08-21 12:06:47 -0700104
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -0800105 VERIFY_IS_EQUAL((const_matrix.template topRows<r>()), (const_matrix.template block<r, cols>(0, 0)));
106 VERIFY_IS_EQUAL((const_matrix.template middleRows<r>(sr)), (const_matrix.template block<r, cols>(sr, 0)));
107 VERIFY_IS_EQUAL((const_matrix.template bottomRows<r>()), (const_matrix.template block<r, cols>(rows - r, 0)));
108 VERIFY_IS_EQUAL((const_matrix.template leftCols<c>()), (const_matrix.template block<rows, c>(0, 0)));
109 VERIFY_IS_EQUAL((const_matrix.template middleCols<c>(sc)), (const_matrix.template block<rows, c>(0, sc)));
110 VERIFY_IS_EQUAL((const_matrix.template rightCols<c>()), (const_matrix.template block<rows, c>(0, cols - c)));
Googler45874d82019-08-21 12:06:47 -0700111}
112
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -0800113EIGEN_DECLARE_TEST(corners) {
114 for (int i = 0; i < g_repeat; i++) {
115 CALL_SUBTEST_1(corners(Matrix<float, 1, 1>()));
116 CALL_SUBTEST_2(corners(Matrix4d()));
117 CALL_SUBTEST_3(corners(Matrix<int, 10, 12>()));
118 CALL_SUBTEST_4(corners(MatrixXcf(5, 7)));
119 CALL_SUBTEST_5(corners(MatrixXf(21, 20)));
Googler45874d82019-08-21 12:06:47 -0700120
Rasmus Munk Larsen2434cfd2023-12-06 12:02:41 -0800121 CALL_SUBTEST_1((corners_fixedsize<Matrix<float, 1, 1>, 1, 1, 0, 0>()));
122 CALL_SUBTEST_2((corners_fixedsize<Matrix4d, 2, 2, 1, 1>()));
123 CALL_SUBTEST_3((corners_fixedsize<Matrix<int, 10, 12>, 4, 7, 5, 2>()));
Googler45874d82019-08-21 12:06:47 -0700124 }
125}