| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  | // This file is part of Eigen, a lightweight C++ template library
 | 
					
						
							| 
									
										
										
										
											2009-05-23 02:25:33 +08:00
										 |  |  | // for linear algebra.
 | 
					
						
							| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  | // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
 | 
					
						
							| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2012-07-14 02:42:47 +08:00
										 |  |  | // This Source Code Form is subject to the terms of the Mozilla
 | 
					
						
							|  |  |  | // Public License v. 2.0. If a copy of the MPL was not distributed
 | 
					
						
							|  |  |  | // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 | 
					
						
							| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "main.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 19:57:24 +08:00
										 |  |  | template <typename MatrixType, typename Index, typename Scalar> | 
					
						
							| 
									
										
										
										
											2022-03-17 00:43:40 +08:00
										 |  |  | std::enable_if_t<!NumTraits<typename MatrixType::Scalar>::IsComplex, typename MatrixType::Scalar> block_real_only( | 
					
						
							| 
									
										
										
										
											2014-02-27 19:57:24 +08:00
										 |  |  |     const MatrixType& m1, Index r1, Index r2, Index c1, Index c2, const Scalar& s1) { | 
					
						
							|  |  |  |   // check cwise-Functions:
 | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1.row(r1).cwiseMax(s1), m1.cwiseMax(s1).row(r1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1.col(c1).cwiseMin(s1), m1.cwiseMin(s1).col(c1)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1.block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).cwiseMin(s1), | 
					
						
							|  |  |  |                    m1.cwiseMin(s1).block(r1, c1, r2 - r1 + 1, c2 - c1 + 1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1.block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).cwiseMax(s1), | 
					
						
							|  |  |  |                    m1.cwiseMax(s1).block(r1, c1, r2 - r1 + 1, c2 - c1 + 1)); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 19:57:24 +08:00
										 |  |  |   return Scalar(0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename MatrixType, typename Index, typename Scalar> | 
					
						
							| 
									
										
										
										
											2022-03-17 00:43:40 +08:00
										 |  |  | std::enable_if_t<NumTraits<typename MatrixType::Scalar>::IsComplex, typename MatrixType::Scalar> block_real_only( | 
					
						
							| 
									
										
										
										
											2014-02-27 19:57:24 +08:00
										 |  |  |     const MatrixType&, Index, Index, Index, Index, const Scalar&) { | 
					
						
							|  |  |  |   return Scalar(0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 18:34:28 +08:00
										 |  |  | // Check at compile-time that T1==T2, and at runtime-time that a==b
 | 
					
						
							|  |  |  | template <typename T1, typename T2> | 
					
						
							| 
									
										
										
										
											2022-03-17 00:43:40 +08:00
										 |  |  | std::enable_if_t<internal::is_same<T1, T2>::value, bool> is_same_block(const T1& a, const T2& b) { | 
					
						
							| 
									
										
										
										
											2017-01-17 18:34:28 +08:00
										 |  |  |   return a.isApprox(b); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-02-27 19:57:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  | template <typename MatrixType> | 
					
						
							| 
									
										
										
										
											2022-03-17 00:43:40 +08:00
										 |  |  | std::enable_if_t<((MatrixType::Flags & RowMajorBit) == 0), void> check_left_top(const MatrixType& m, Index r, Index c, | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  |                                                                                 Index rows, Index /*unused*/) { | 
					
						
							| 
									
										
										
										
											2023-07-15 05:12:45 +08:00
										 |  |  |   if (c > 0) VERIFY_IS_EQUAL(m.leftCols(c).coeff(r + c * rows), m(r, c)); | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename MatrixType> | 
					
						
							| 
									
										
										
										
											2022-03-17 00:43:40 +08:00
										 |  |  | std::enable_if_t<((MatrixType::Flags & RowMajorBit) != 0), void> check_left_top(const MatrixType& m, Index r, Index c, | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  |                                                                                 Index /*unused*/, Index cols) { | 
					
						
							| 
									
										
										
										
											2023-07-15 05:12:45 +08:00
										 |  |  |   if (r > 0) VERIFY_IS_EQUAL(m.topRows(r).coeff(c + r * cols), m(r, c)); | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  | template <typename MatrixType> | 
					
						
							|  |  |  | void block(const MatrixType& m) { | 
					
						
							| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  |   typedef typename MatrixType::Scalar Scalar; | 
					
						
							| 
									
										
										
										
											2009-01-23 20:26:32 +08:00
										 |  |  |   typedef typename MatrixType::RealScalar RealScalar; | 
					
						
							| 
									
										
										
										
											2008-03-13 01:17:36 +08:00
										 |  |  |   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; | 
					
						
							|  |  |  |   typedef Matrix<Scalar, 1, MatrixType::ColsAtCompileTime> RowVectorType; | 
					
						
							| 
									
										
										
										
											2018-02-09 23:45:49 +08:00
										 |  |  |   typedef Matrix<Scalar, Dynamic, Dynamic, MatrixType::IsRowMajor ? RowMajor : ColMajor> DynamicMatrixType; | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |   typedef Matrix<Scalar, Dynamic, 1> DynamicVectorType; | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-20 23:37:56 +08:00
										 |  |  |   Index rows = m.rows(); | 
					
						
							|  |  |  |   Index cols = m.cols(); | 
					
						
							| 
									
										
										
										
											2008-05-22 20:18:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-21 08:34:46 +08:00
										 |  |  |   MatrixType m1 = MatrixType::Random(rows, cols), m1_copy = m1, m2 = MatrixType::Random(rows, cols), m3(rows, cols), | 
					
						
							| 
									
										
										
										
											2010-01-28 00:42:04 +08:00
										 |  |  |              ones = MatrixType::Ones(rows, cols); | 
					
						
							| 
									
										
										
										
											2011-10-31 12:51:36 +08:00
										 |  |  |   VectorType v1 = VectorType::Random(rows); | 
					
						
							| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-25 22:15:22 +08:00
										 |  |  |   Scalar s1 = internal::random<Scalar>(); | 
					
						
							| 
									
										
										
										
											2008-03-03 19:02:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-25 22:15:22 +08:00
										 |  |  |   Index r1 = internal::random<Index>(0, rows - 1); | 
					
						
							|  |  |  |   Index r2 = internal::random<Index>(r1, rows - 1); | 
					
						
							|  |  |  |   Index c1 = internal::random<Index>(0, cols - 1); | 
					
						
							|  |  |  |   Index c2 = internal::random<Index>(c1, cols - 1); | 
					
						
							| 
									
										
										
										
											2008-03-03 19:02:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-27 19:57:24 +08:00
										 |  |  |   block_real_only(m1, r1, r2, c1, c1, s1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  |   // check row() and col()
 | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |   VERIFY_IS_EQUAL(m1.col(c1).transpose(), m1.transpose().row(c1)); | 
					
						
							| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  |   // check operator(), both constant and non-constant, on row() and col()
 | 
					
						
							| 
									
										
										
										
											2010-12-23 06:45:37 +08:00
										 |  |  |   m1 = m1_copy; | 
					
						
							|  |  |  |   m1.row(r1) += s1 * m1_copy.row(r2); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + s1 * m1_copy.row(r2)); | 
					
						
							|  |  |  |   // check nested block xpr on lhs
 | 
					
						
							|  |  |  |   m1.row(r1).row(0) += s1 * m1_copy.row(r2); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1.row(r1), m1_copy.row(r1) + Scalar(2) * s1 * m1_copy.row(r2)); | 
					
						
							|  |  |  |   m1 = m1_copy; | 
					
						
							|  |  |  |   m1.col(c1) += s1 * m1_copy.col(c2); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + s1 * m1_copy.col(c2)); | 
					
						
							|  |  |  |   m1.col(c1).col(0) += s1 * m1_copy.col(c2); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1.col(c1), m1_copy.col(c1) + Scalar(2) * s1 * m1_copy.col(c2)); | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   check_left_top(m1, r1, c1, rows, cols); | 
					
						
							| 
									
										
										
										
											2010-02-19 09:42:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-29 21:56:40 +08:00
										 |  |  |   // check block()
 | 
					
						
							| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  |   Matrix<Scalar, Dynamic, Dynamic> b1(1, 1); | 
					
						
							|  |  |  |   b1(0, 0) = m1(r1, c1); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-14 04:19:14 +08:00
										 |  |  |   RowVectorType br1(m1.block(r1, 0, 1, cols)); | 
					
						
							|  |  |  |   VectorType bc1(m1.block(0, c1, rows, 1)); | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |   VERIFY_IS_EQUAL(b1, m1.block(r1, c1, 1, 1)); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(m1.row(r1), br1); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(m1.col(c1), bc1); | 
					
						
							| 
									
										
										
										
											2008-01-14 04:19:14 +08:00
										 |  |  |   // check operator(), both constant and non-constant, on block()
 | 
					
						
							|  |  |  |   m1.block(r1, c1, r2 - r1 + 1, c2 - c1 + 1) = s1 * m2.block(0, 0, r2 - r1 + 1, c2 - c1 + 1); | 
					
						
							|  |  |  |   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); | 
					
						
							| 
									
										
										
										
											2008-05-22 20:18:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-27 18:57:52 +08:00
										 |  |  |   const Index BlockRows = 2; | 
					
						
							|  |  |  |   const Index BlockCols = 5; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-22 20:18:55 +08:00
										 |  |  |   if (rows >= 5 && cols >= 8) { | 
					
						
							| 
									
										
										
										
											2008-02-29 21:56:40 +08:00
										 |  |  |     // test fixed block() as lvalue
 | 
					
						
							| 
									
										
										
										
											2008-05-22 20:18:55 +08:00
										 |  |  |     m1.template block<BlockRows, BlockCols>(1, 1) *= s1; | 
					
						
							| 
									
										
										
										
											2008-02-29 21:56:40 +08:00
										 |  |  |     // test operator() on fixed block() both as constant and non-constant
 | 
					
						
							| 
									
										
										
										
											2008-05-22 20:18:55 +08:00
										 |  |  |     m1.template block<BlockRows, BlockCols>(1, 1)(0, 3) = m1.template block<2, 5>(1, 1)(1, 2); | 
					
						
							| 
									
										
										
										
											2008-02-29 21:56:40 +08:00
										 |  |  |     // check that fixed block() and block() agree
 | 
					
						
							| 
									
										
										
										
											2008-05-22 20:18:55 +08:00
										 |  |  |     Matrix<Scalar, Dynamic, Dynamic> b = m1.template block<BlockRows, BlockCols>(3, 3); | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |     VERIFY_IS_EQUAL(b, m1.block(3, 3, BlockRows, BlockCols)); | 
					
						
							| 
									
										
										
										
											2013-06-18 21:29:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // same tests with mixed fixed/dynamic size
 | 
					
						
							|  |  |  |     m1.template block<BlockRows, Dynamic>(1, 1, BlockRows, BlockCols) *= s1; | 
					
						
							|  |  |  |     m1.template block<BlockRows, Dynamic>(1, 1, BlockRows, BlockCols)(0, 3) = m1.template block<2, 5>(1, 1)(1, 2); | 
					
						
							|  |  |  |     Matrix<Scalar, Dynamic, Dynamic> b2 = m1.template block<Dynamic, BlockCols>(3, 3, 2, 5); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(b2, m1.block(3, 3, BlockRows, BlockCols)); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 18:34:28 +08:00
										 |  |  |     VERIFY(is_same_block(m1.block(3, 3, BlockRows, BlockCols), | 
					
						
							|  |  |  |                          m1.block(3, 3, fix<Dynamic>(BlockRows), fix<Dynamic>(BlockCols)))); | 
					
						
							|  |  |  |     VERIFY(is_same_block(m1.template block<BlockRows, Dynamic>(1, 1, BlockRows, BlockCols), | 
					
						
							|  |  |  |                          m1.block(1, 1, fix<BlockRows>, BlockCols))); | 
					
						
							|  |  |  |     VERIFY(is_same_block(m1.template block<BlockRows, BlockCols>(1, 1, BlockRows, BlockCols), | 
					
						
							|  |  |  |                          m1.block(1, 1, fix<BlockRows>(), fix<BlockCols>))); | 
					
						
							|  |  |  |     VERIFY(is_same_block(m1.template block<BlockRows, BlockCols>(1, 1, BlockRows, BlockCols), | 
					
						
							|  |  |  |                          m1.block(1, 1, fix<BlockRows>, fix<BlockCols>(BlockCols)))); | 
					
						
							| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2008-08-10 04:06:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-09 17:30:23 +08:00
										 |  |  |   if (rows > 2) { | 
					
						
							|  |  |  |     // test sub vectors
 | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |     VERIFY_IS_EQUAL(v1.template head<2>(), v1.block(0, 0, 2, 1)); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(v1.template head<2>(), v1.head(2)); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(v1.template head<2>(), v1.segment(0, 2)); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(v1.template head<2>(), v1.template segment<2>(0)); | 
					
						
							| 
									
										
										
										
											2010-06-21 00:59:15 +08:00
										 |  |  |     Index i = rows - 2; | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |     VERIFY_IS_EQUAL(v1.template tail<2>(), v1.block(i, 0, 2, 1)); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(v1.template tail<2>(), v1.tail(2)); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(v1.template tail<2>(), v1.segment(i, 2)); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(v1.template tail<2>(), v1.template segment<2>(i)); | 
					
						
							| 
									
										
										
										
											2010-10-25 22:15:22 +08:00
										 |  |  |     i = internal::random<Index>(0, rows - 2); | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |     VERIFY_IS_EQUAL(v1.segment(i, 2), v1.template segment<2>(i)); | 
					
						
							| 
									
										
										
										
											2008-09-09 17:30:23 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-10 04:06:25 +08:00
										 |  |  |   // stress some basic stuffs with block matrices
 | 
					
						
							| 
									
										
										
										
											2022-01-27 02:16:19 +08:00
										 |  |  |   VERIFY_IS_EQUAL(numext::real(ones.col(c1).sum()), RealScalar(rows)); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(numext::real(ones.row(r1).sum()), RealScalar(cols)); | 
					
						
							| 
									
										
										
										
											2008-08-10 04:06:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-27 02:16:19 +08:00
										 |  |  |   VERIFY_IS_EQUAL(numext::real(ones.col(c1).dot(ones.col(c2))), RealScalar(rows)); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(numext::real(ones.row(r1).dot(ones.row(r2))), RealScalar(cols)); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-09 23:45:49 +08:00
										 |  |  |   // check that linear acccessors works on blocks
 | 
					
						
							| 
									
										
										
										
											2014-09-08 15:50:03 +08:00
										 |  |  |   m1 = m1_copy; | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // now test some block-inside-of-block.
 | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |   // expressions with direct access
 | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL((m1.block(r1, c1, rows - r1, cols - c1).block(r2 - r1, c2 - c1, rows - r2, cols - c2)), | 
					
						
							|  |  |  |                   (m1.block(r2, c2, rows - r2, cols - c2))); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL((m1.block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).row(0)), (m1.row(r1).segment(c1, c2 - c1 + 1))); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL((m1.block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).col(0)), (m1.col(c1).segment(r1, r2 - r1 + 1))); | 
					
						
							| 
									
										
										
										
											2010-05-31 04:00:09 +08:00
										 |  |  |   VERIFY_IS_EQUAL((m1.block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).transpose().col(0)), | 
					
						
							|  |  |  |                   (m1.row(r1).segment(c1, c2 - c1 + 1)).transpose()); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL((m1.transpose().block(c1, r1, c2 - c1 + 1, r2 - r1 + 1).col(0)), | 
					
						
							|  |  |  |                   (m1.row(r1).segment(c1, c2 - c1 + 1)).transpose()); | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // expressions without direct access
 | 
					
						
							| 
									
										
										
										
											2014-07-09 23:21:16 +08:00
										 |  |  |   VERIFY_IS_APPROX(((m1 + m2).block(r1, c1, rows - r1, cols - c1).block(r2 - r1, c2 - c1, rows - r2, cols - c2)), | 
					
						
							|  |  |  |                    ((m1 + m2).block(r2, c2, rows - r2, cols - c2))); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(((m1 + m2).block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).row(0)), | 
					
						
							| 
									
										
										
										
											2018-07-10 15:10:15 +08:00
										 |  |  |                    ((m1 + m2).row(r1).segment(c1, c2 - c1 + 1))); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(((m1 + m2).block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).row(0)), | 
					
						
							| 
									
										
										
										
											2014-07-09 23:21:16 +08:00
										 |  |  |                    ((m1 + m2).eval().row(r1).segment(c1, c2 - c1 + 1))); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(((m1 + m2).block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).col(0)), | 
					
						
							|  |  |  |                    ((m1 + m2).col(c1).segment(r1, r2 - r1 + 1))); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(((m1 + m2).block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).transpose().col(0)), | 
					
						
							|  |  |  |                    ((m1 + m2).row(r1).segment(c1, c2 - c1 + 1)).transpose()); | 
					
						
							| 
									
										
										
										
											2018-07-11 23:09:23 +08:00
										 |  |  |   VERIFY_IS_APPROX(((m1 + m2).transpose().block(c1, r1, c2 - c1 + 1, r2 - r1 + 1).col(0)), | 
					
						
							|  |  |  |                    ((m1 + m2).row(r1).segment(c1, c2 - c1 + 1)).transpose()); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(((m1 + m2).template block<Dynamic, 1>(r1, c1, r2 - r1 + 1, 1)), | 
					
						
							|  |  |  |                    ((m1 + m2).eval().col(c1).eval().segment(r1, r2 - r1 + 1))); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(((m1 + m2).template block<1, Dynamic>(r1, c1, 1, c2 - c1 + 1)), | 
					
						
							|  |  |  |                    ((m1 + m2).eval().row(r1).eval().segment(c1, c2 - c1 + 1))); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(((m1 + m2).transpose().template block<1, Dynamic>(c1, r1, 1, r2 - r1 + 1)), | 
					
						
							|  |  |  |                    ((m1 + m2).eval().col(c1).eval().segment(r1, r2 - r1 + 1)).transpose()); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 + m2).row(r1).eval(), (m1 + m2).eval().row(r1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 + m2).adjoint().col(r1).eval(), (m1 + m2).adjoint().eval().col(r1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 + m2).adjoint().row(c1).eval(), (m1 + m2).adjoint().eval().row(c1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 * 1).row(r1).segment(c1, c2 - c1 + 1).eval(), m1.row(r1).eval().segment(c1, c2 - c1 + 1).eval()); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1.col(c1).reverse().segment(r1, r2 - r1 + 1).eval(), | 
					
						
							|  |  |  |                    m1.col(c1).reverse().eval().segment(r1, r2 - r1 + 1).eval()); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-09 23:45:49 +08:00
										 |  |  |   VERIFY_IS_APPROX((m1 * 1).topRows(r1), m1.topRows(r1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 * 1).leftCols(c1), m1.leftCols(c1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 * 1).transpose().topRows(c1), m1.transpose().topRows(c1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 * 1).transpose().leftCols(r1), m1.transpose().leftCols(r1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 * 1).transpose().middleRows(c1, c2 - c1 + 1), m1.transpose().middleRows(c1, c2 - c1 + 1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 * 1).transpose().middleCols(r1, r2 - r1 + 1), m1.transpose().middleCols(r1, r2 - r1 + 1)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |   // evaluation into plain matrices from expressions with direct access (stress MapBase)
 | 
					
						
							|  |  |  |   DynamicMatrixType dm; | 
					
						
							|  |  |  |   DynamicVectorType dv; | 
					
						
							|  |  |  |   dm.setZero(); | 
					
						
							|  |  |  |   dm = m1.block(r1, c1, rows - r1, cols - c1).block(r2 - r1, c2 - c1, rows - r2, cols - c2); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(dm, (m1.block(r2, c2, rows - r2, cols - c2))); | 
					
						
							|  |  |  |   dm.setZero(); | 
					
						
							|  |  |  |   dv.setZero(); | 
					
						
							|  |  |  |   dm = m1.block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).row(0).transpose(); | 
					
						
							|  |  |  |   dv = m1.row(r1).segment(c1, c2 - c1 + 1); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(dv, dm); | 
					
						
							|  |  |  |   dm.setZero(); | 
					
						
							|  |  |  |   dv.setZero(); | 
					
						
							|  |  |  |   dm = m1.col(c1).segment(r1, r2 - r1 + 1); | 
					
						
							|  |  |  |   dv = m1.block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).col(0); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(dv, dm); | 
					
						
							|  |  |  |   dm.setZero(); | 
					
						
							|  |  |  |   dv.setZero(); | 
					
						
							|  |  |  |   dm = m1.block(r1, c1, r2 - r1 + 1, c2 - c1 + 1).transpose().col(0); | 
					
						
							|  |  |  |   dv = m1.row(r1).segment(c1, c2 - c1 + 1); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(dv, dm); | 
					
						
							|  |  |  |   dm.setZero(); | 
					
						
							|  |  |  |   dv.setZero(); | 
					
						
							|  |  |  |   dm = m1.row(r1).segment(c1, c2 - c1 + 1).transpose(); | 
					
						
							|  |  |  |   dv = m1.transpose().block(c1, r1, c2 - c1 + 1, r2 - r1 + 1).col(0); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(dv, dm); | 
					
						
							| 
									
										
										
										
											2016-02-29 17:31:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL((m1.template block<Dynamic, 1>(1, 0, 0, 1)), m1.block(1, 0, 0, 1)); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL((m1.template block<1, Dynamic>(0, 1, 1, 0)), m1.block(0, 1, 1, 0)); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(((m1 * 1).template block<Dynamic, 1>(1, 0, 0, 1)), m1.block(1, 0, 0, 1)); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(((m1 * 1).template block<1, Dynamic>(0, 1, 1, 0)), m1.block(0, 1, 1, 0)); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-02 20:02:34 +08:00
										 |  |  |   VERIFY_IS_EQUAL(m1.template subVector<Horizontal>(r1), m1.row(r1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 + m1).template subVector<Horizontal>(r1), (m1 + m1).row(r1)); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(m1.template subVector<Vertical>(c1), m1.col(c1)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX((m1 + m1).template subVector<Vertical>(c1), (m1 + m1).col(c1)); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(m1.template subVectors<Horizontal>(), m1.rows()); | 
					
						
							|  |  |  |   VERIFY_IS_EQUAL(m1.template subVectors<Vertical>(), m1.cols()); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 22:09:49 +08:00
										 |  |  |   if (rows >= 2 || cols >= 2) { | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(int(m1.middleCols(0, 0).IsRowMajor), int(m1.IsRowMajor)); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(m1.middleCols(0, 0).outerSize(), m1.IsRowMajor ? rows : 0); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(m1.middleCols(0, 0).innerSize(), m1.IsRowMajor ? 0 : rows); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-15 22:09:49 +08:00
										 |  |  |     VERIFY_IS_EQUAL(int(m1.middleRows(0, 0).IsRowMajor), int(m1.IsRowMajor)); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(m1.middleRows(0, 0).outerSize(), m1.IsRowMajor ? 0 : cols); | 
					
						
							|  |  |  |     VERIFY_IS_EQUAL(m1.middleRows(0, 0).innerSize(), m1.IsRowMajor ? cols : 0); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2007-12-11 18:07:18 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-31 23:39:56 +08:00
										 |  |  | template <typename MatrixType> | 
					
						
							| 
									
										
										
										
											2022-03-17 00:43:40 +08:00
										 |  |  | std::enable_if_t<MatrixType::IsVectorAtCompileTime, void> compare_using_data_and_stride(const MatrixType& m) { | 
					
						
							| 
									
										
										
										
											2010-06-20 23:37:56 +08:00
										 |  |  |   Index rows = m.rows(); | 
					
						
							|  |  |  |   Index cols = m.cols(); | 
					
						
							|  |  |  |   Index size = m.size(); | 
					
						
							|  |  |  |   Index innerStride = m.innerStride(); | 
					
						
							|  |  |  |   Index rowStride = m.rowStride(); | 
					
						
							|  |  |  |   Index colStride = m.colStride(); | 
					
						
							| 
									
										
										
										
											2009-08-31 23:39:56 +08:00
										 |  |  |   const typename MatrixType::Scalar* data = m.data(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (int j = 0; j < cols; ++j) | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |     for (int i = 0; i < rows; ++i) VERIFY(m.coeff(i, j) == data[i * rowStride + j * colStride]); | 
					
						
							| 
									
										
										
										
											2010-02-19 09:42:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  |   VERIFY(innerStride == int((&m.coeff(1)) - (&m.coeff(0)))); | 
					
						
							|  |  |  |   for (int i = 0; i < size; ++i) VERIFY(m.coeff(i) == data[i * innerStride]); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-08-31 23:39:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  | template <typename MatrixType> | 
					
						
							| 
									
										
										
										
											2022-03-17 00:43:40 +08:00
										 |  |  | std::enable_if_t<!MatrixType::IsVectorAtCompileTime, void> compare_using_data_and_stride(const MatrixType& m) { | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  |   Index rows = m.rows(); | 
					
						
							|  |  |  |   Index cols = m.cols(); | 
					
						
							|  |  |  |   Index innerStride = m.innerStride(); | 
					
						
							|  |  |  |   Index outerStride = m.outerStride(); | 
					
						
							|  |  |  |   Index rowStride = m.rowStride(); | 
					
						
							|  |  |  |   Index colStride = m.colStride(); | 
					
						
							|  |  |  |   const typename MatrixType::Scalar* data = m.data(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (int j = 0; j < cols; ++j) | 
					
						
							|  |  |  |     for (int i = 0; i < rows; ++i) VERIFY(m.coeff(i, j) == data[i * rowStride + j * colStride]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (int j = 0; j < cols; ++j) | 
					
						
							|  |  |  |     for (int i = 0; i < rows; ++i) | 
					
						
							|  |  |  |       VERIFY(m.coeff(i, j) == data[(MatrixType::Flags & RowMajorBit) ? i * outerStride + j * innerStride | 
					
						
							|  |  |  |                                                                      : j * outerStride + i * innerStride]); | 
					
						
							| 
									
										
										
										
											2009-08-31 23:39:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename MatrixType> | 
					
						
							|  |  |  | void data_and_stride(const MatrixType& m) { | 
					
						
							| 
									
										
										
										
											2010-06-20 23:37:56 +08:00
										 |  |  |   Index rows = m.rows(); | 
					
						
							|  |  |  |   Index cols = m.cols(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-25 22:15:22 +08:00
										 |  |  |   Index r1 = internal::random<Index>(0, rows - 1); | 
					
						
							|  |  |  |   Index r2 = internal::random<Index>(r1, rows - 1); | 
					
						
							|  |  |  |   Index c1 = internal::random<Index>(0, cols - 1); | 
					
						
							|  |  |  |   Index c2 = internal::random<Index>(c1, cols - 1); | 
					
						
							| 
									
										
										
										
											2009-08-31 23:39:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   MatrixType m1 = MatrixType::Random(rows, cols); | 
					
						
							|  |  |  |   compare_using_data_and_stride(m1.block(r1, c1, r2 - r1 + 1, c2 - c1 + 1)); | 
					
						
							| 
									
										
										
										
											2010-02-26 10:01:52 +08:00
										 |  |  |   compare_using_data_and_stride(m1.transpose().block(c1, r1, c2 - c1 + 1, r2 - r1 + 1)); | 
					
						
							| 
									
										
										
										
											2009-08-31 23:39:56 +08:00
										 |  |  |   compare_using_data_and_stride(m1.row(r1)); | 
					
						
							|  |  |  |   compare_using_data_and_stride(m1.col(c1)); | 
					
						
							| 
									
										
										
										
											2010-02-26 10:01:52 +08:00
										 |  |  |   compare_using_data_and_stride(m1.row(r1).transpose()); | 
					
						
							|  |  |  |   compare_using_data_and_stride(m1.col(c1).transpose()); | 
					
						
							| 
									
										
										
										
											2009-08-31 23:39:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-30 01:21:41 +08:00
										 |  |  | template <typename BaseXpr, typename Xpr = BaseXpr, int Depth = 0> | 
					
						
							|  |  |  | struct unwind_test_impl { | 
					
						
							|  |  |  |   static void run(Xpr& xpr) { | 
					
						
							|  |  |  |     Index startRow = internal::random<Index>(0, xpr.rows() / 5); | 
					
						
							|  |  |  |     Index startCol = internal::random<Index>(0, xpr.cols() / 6); | 
					
						
							|  |  |  |     Index rows = xpr.rows() / 3; | 
					
						
							|  |  |  |     Index cols = xpr.cols() / 2; | 
					
						
							|  |  |  |     // test equivalence of const expressions
 | 
					
						
							|  |  |  |     const Block<const Xpr> constNestedBlock(xpr, startRow, startCol, rows, cols); | 
					
						
							|  |  |  |     const Block<const BaseXpr> constUnwoundBlock = constNestedBlock.unwind(); | 
					
						
							|  |  |  |     VERIFY_IS_CWISE_EQUAL(constNestedBlock, constUnwoundBlock); | 
					
						
							|  |  |  |     // modify a random element in each representation and test equivalence of non-const expressions
 | 
					
						
							|  |  |  |     Block<Xpr> nestedBlock(xpr, startRow, startCol, rows, cols); | 
					
						
							|  |  |  |     Block<BaseXpr> unwoundBlock = nestedBlock.unwind(); | 
					
						
							|  |  |  |     Index r1 = internal::random<Index>(0, rows - 1); | 
					
						
							|  |  |  |     Index c1 = internal::random<Index>(0, cols - 1); | 
					
						
							|  |  |  |     Index r2 = internal::random<Index>(0, rows - 1); | 
					
						
							|  |  |  |     Index c2 = internal::random<Index>(0, cols - 1); | 
					
						
							|  |  |  |     nestedBlock.coeffRef(r1, c1) = internal::random<typename DenseBase<Xpr>::Scalar>(); | 
					
						
							|  |  |  |     unwoundBlock.coeffRef(r2, c2) = internal::random<typename DenseBase<Xpr>::Scalar>(); | 
					
						
							|  |  |  |     VERIFY_IS_CWISE_EQUAL(nestedBlock, unwoundBlock); | 
					
						
							|  |  |  |     unwind_test_impl<BaseXpr, Block<Xpr>, Depth + 1>::run(nestedBlock); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename BaseXpr, typename Xpr> | 
					
						
							|  |  |  | struct unwind_test_impl<BaseXpr, Xpr, 4> { | 
					
						
							|  |  |  |   static void run(const Xpr&) {} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename BaseXpr> | 
					
						
							|  |  |  | void unwind_test(const BaseXpr&) { | 
					
						
							|  |  |  |   BaseXpr xpr = BaseXpr::Random(100, 100); | 
					
						
							|  |  |  |   unwind_test_impl<BaseXpr>::run(xpr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 20:46:15 +08:00
										 |  |  | EIGEN_DECLARE_TEST(block) { | 
					
						
							| 
									
										
										
										
											2008-05-22 20:18:55 +08:00
										 |  |  |   for (int i = 0; i < g_repeat; i++) { | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |     CALL_SUBTEST_1(block(Matrix<float, 1, 1>())); | 
					
						
							| 
									
										
										
										
											2019-01-15 22:09:49 +08:00
										 |  |  |     CALL_SUBTEST_1(block(Matrix<float, 1, Dynamic>(internal::random(2, 50)))); | 
					
						
							|  |  |  |     CALL_SUBTEST_1(block(Matrix<float, Dynamic, 1>(internal::random(2, 50)))); | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |     CALL_SUBTEST_2(block(Matrix4d())); | 
					
						
							| 
									
										
										
										
											2019-01-15 22:09:49 +08:00
										 |  |  |     CALL_SUBTEST_3(block(MatrixXcf(internal::random(2, 50), internal::random(2, 50)))); | 
					
						
							|  |  |  |     CALL_SUBTEST_4(block(MatrixXi(internal::random(2, 50), internal::random(2, 50)))); | 
					
						
							|  |  |  |     CALL_SUBTEST_5(block(MatrixXcd(internal::random(2, 50), internal::random(2, 50)))); | 
					
						
							|  |  |  |     CALL_SUBTEST_6(block(MatrixXf(internal::random(2, 50), internal::random(2, 50)))); | 
					
						
							|  |  |  |     CALL_SUBTEST_7(block(Matrix<int, Dynamic, Dynamic, RowMajor>(internal::random(2, 50), internal::random(2, 50)))); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-26 22:03:13 +08:00
										 |  |  |     CALL_SUBTEST_8(block(Matrix<float, Dynamic, 4>(3, 4))); | 
					
						
							| 
									
										
										
										
											2023-08-30 01:21:41 +08:00
										 |  |  |     CALL_SUBTEST_9(unwind_test(MatrixXf())); | 
					
						
							| 
									
										
										
										
											2010-01-28 00:42:04 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-19 09:42:38 +08:00
										 |  |  | #ifndef EIGEN_DEFAULT_TO_ROW_MAJOR
 | 
					
						
							| 
									
										
										
										
											2010-10-25 22:15:22 +08:00
										 |  |  |     CALL_SUBTEST_6(data_and_stride(MatrixXf(internal::random(5, 50), internal::random(5, 50)))); | 
					
						
							|  |  |  |     CALL_SUBTEST_7( | 
					
						
							|  |  |  |         data_and_stride(Matrix<int, Dynamic, Dynamic, RowMajor>(internal::random(5, 50), internal::random(5, 50)))); | 
					
						
							| 
									
										
										
										
											2010-02-19 09:42:38 +08:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-05-22 20:18:55 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } |