Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 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 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 12 | #define COMPARE_CORNER(A, B) \ |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 13 | VERIFY_IS_EQUAL(matrix.A, matrix.B); \ |
| 14 | VERIFY_IS_EQUAL(const_matrix.A, const_matrix.B); |
| 15 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 16 | template <typename MatrixType> |
| 17 | void corners(const MatrixType& m) { |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 18 | Index rows = m.rows(); |
| 19 | Index cols = m.cols(); |
| 20 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 21 | Index r = internal::random<Index>(1, rows); |
| 22 | Index c = internal::random<Index>(1, cols); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 23 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 24 | MatrixType matrix = MatrixType::Random(rows, cols); |
| 25 | const MatrixType const_matrix = MatrixType::Random(rows, cols); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 26 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 27 | 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)); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 31 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 32 | 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); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 36 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 37 | 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)); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 45 | template <typename MatrixType, int CRows, int CCols, int SRows, int SCols> |
| 46 | void corners_fixedsize() { |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 47 | 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 Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 55 | sr = SRows, |
| 56 | sc = SCols |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 59 | 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))); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 63 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 64 | 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))); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 68 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 69 | 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))); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 73 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 74 | 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))); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 80 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 81 | 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))); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 86 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 87 | 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))); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 95 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 96 | 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))); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 104 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 105 | 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))); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 113 | EIGEN_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))); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 120 | |
Rasmus Munk Larsen | 2434cfd | 2023-12-06 12:02:41 -0800 | [diff] [blame] | 121 | 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>())); |
Googler | 45874d8 | 2019-08-21 12:06:47 -0700 | [diff] [blame] | 124 | } |
| 125 | } |