| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  | // This file is part of Eigen, a lightweight C++ template library
 | 
					
						
							|  |  |  | // for linear algebra.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											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/.
 | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "main.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							|  |  |  | struct other_matrix_type { | 
					
						
							|  |  |  |   typedef int type; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-05 06:41:52 +08:00
										 |  |  | template <typename Scalar_, int Rows_, int Cols_, int Options_, int MaxRows_, int MaxCols_> | 
					
						
							|  |  |  | struct other_matrix_type<Matrix<Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_> > { | 
					
						
							|  |  |  |   typedef Matrix<Scalar_, Rows_, Cols_, Options_ ^ RowMajor, MaxRows_, MaxCols_> type; | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +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::RowsAtCompileTime == 1 || MatrixType::RowsAtCompileTime == Dynamic), void> check_row_swap( | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  |     MatrixType& m1) { | 
					
						
							| 
									
										
										
										
											2022-03-10 23:05:58 +08:00
										 |  |  |   if (m1.rows() != 1) { | 
					
						
							|  |  |  |     // test assertion on mismatching size -- matrix case
 | 
					
						
							|  |  |  |     VERIFY_RAISES_ASSERT(m1.swap(m1.row(0))); | 
					
						
							|  |  |  |     // test assertion on mismatching size -- xpr case
 | 
					
						
							|  |  |  |     VERIFY_RAISES_ASSERT(m1.row(0).swap(m1)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <typename MatrixType> | 
					
						
							| 
									
										
										
										
											2022-03-17 00:43:40 +08:00
										 |  |  | std::enable_if_t<!(MatrixType::RowsAtCompileTime == 1 || MatrixType::RowsAtCompileTime == Dynamic), void> | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  | check_row_swap(MatrixType& /* unused */) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  | template <typename MatrixType> | 
					
						
							|  |  |  | void swap(const MatrixType& m) { | 
					
						
							|  |  |  |   typedef typename other_matrix_type<MatrixType>::type OtherMatrixType; | 
					
						
							|  |  |  |   typedef typename MatrixType::Scalar Scalar; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-26 04:13:49 +08:00
										 |  |  |   eigen_assert((!internal::is_same<MatrixType, OtherMatrixType>::value)); | 
					
						
							| 
									
										
										
										
											2018-07-12 23:16:40 +08:00
										 |  |  |   Index rows = m.rows(); | 
					
						
							|  |  |  |   Index cols = m.cols(); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  |   // construct 3 matrix guaranteed to be distinct
 | 
					
						
							|  |  |  |   MatrixType m1 = MatrixType::Random(rows, cols); | 
					
						
							|  |  |  |   MatrixType m2 = MatrixType::Random(rows, cols) + Scalar(100) * MatrixType::Identity(rows, cols); | 
					
						
							|  |  |  |   OtherMatrixType m3 = OtherMatrixType::Random(rows, cols) + Scalar(200) * OtherMatrixType::Identity(rows, cols); | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  |   MatrixType m1_copy = m1; | 
					
						
							|  |  |  |   MatrixType m2_copy = m2; | 
					
						
							|  |  |  |   OtherMatrixType m3_copy = m3; | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  |   // test swapping 2 matrices of same type
 | 
					
						
							| 
									
										
										
										
											2014-11-06 16:25:26 +08:00
										 |  |  |   Scalar *d1 = m1.data(), *d2 = m2.data(); | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  |   m1.swap(m2); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1, m2_copy); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m2, m1_copy); | 
					
						
							| 
									
										
										
										
											2014-11-06 16:25:26 +08:00
										 |  |  |   if (MatrixType::SizeAtCompileTime == Dynamic) { | 
					
						
							|  |  |  |     VERIFY(m1.data() == d2); | 
					
						
							|  |  |  |     VERIFY(m2.data() == d1); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  |   m1 = m1_copy; | 
					
						
							|  |  |  |   m2 = m2_copy; | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  |   // test swapping 2 matrices of different types
 | 
					
						
							|  |  |  |   m1.swap(m3); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1, m3_copy); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m3, m1_copy); | 
					
						
							|  |  |  |   m1 = m1_copy; | 
					
						
							|  |  |  |   m3 = m3_copy; | 
					
						
							| 
									
										
										
										
											2023-12-06 05:22:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  |   // test swapping matrix with expression
 | 
					
						
							|  |  |  |   m1.swap(m2.block(0, 0, rows, cols)); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1, m2_copy); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m2, m1_copy); | 
					
						
							|  |  |  |   m1 = m1_copy; | 
					
						
							|  |  |  |   m2 = m2_copy; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // test swapping two expressions of different types
 | 
					
						
							|  |  |  |   m1.transpose().swap(m3.transpose()); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m1, m3_copy); | 
					
						
							|  |  |  |   VERIFY_IS_APPROX(m3, m1_copy); | 
					
						
							|  |  |  |   m1 = m1_copy; | 
					
						
							|  |  |  |   m3 = m3_copy; | 
					
						
							| 
									
										
										
										
											2021-09-17 04:43:54 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   check_row_swap(m1); | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 20:46:15 +08:00
										 |  |  | EIGEN_DECLARE_TEST(swap) { | 
					
						
							| 
									
										
										
										
											2015-04-01 02:16:02 +08:00
										 |  |  |   int s = internal::random<int>(1, EIGEN_TEST_MAX_SIZE); | 
					
						
							| 
									
										
										
										
											2009-10-29 06:19:29 +08:00
										 |  |  |   CALL_SUBTEST_1(swap(Matrix3f()));      // fixed size, no vectorization
 | 
					
						
							|  |  |  |   CALL_SUBTEST_2(swap(Matrix4d()));      // fixed size, possible vectorization
 | 
					
						
							| 
									
										
										
										
											2015-04-01 02:16:02 +08:00
										 |  |  |   CALL_SUBTEST_3(swap(MatrixXd(s, s)));  // dyn size, no vectorization
 | 
					
						
							|  |  |  |   CALL_SUBTEST_4(swap(MatrixXf(s, s)));  // dyn size, possible vectorization
 | 
					
						
							|  |  |  |   TEST_SET_BUT_UNUSED_VARIABLE(s) | 
					
						
							| 
									
										
										
										
											2009-09-03 04:56:48 +08:00
										 |  |  | } |