• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 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 #define EIGEN_NO_STATIC_ASSERT
11 #include "main.h"
12 
13 template<typename T>
14 struct other_matrix_type
15 {
16   typedef int type;
17 };
18 
19 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
20 struct other_matrix_type<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
21 {
22   typedef Matrix<_Scalar, _Rows, _Cols, _Options^RowMajor, _MaxRows, _MaxCols> type;
23 };
24 
swap(const MatrixType & m)25 template<typename MatrixType> void swap(const MatrixType& m)
26 {
27   typedef typename other_matrix_type<MatrixType>::type OtherMatrixType;
28   typedef typename MatrixType::Scalar Scalar;
29 
30   eigen_assert((!internal::is_same<MatrixType,OtherMatrixType>::value));
31   typename MatrixType::Index rows = m.rows();
32   typename MatrixType::Index cols = m.cols();
33 
34   // construct 3 matrix guaranteed to be distinct
35   MatrixType m1 = MatrixType::Random(rows,cols);
36   MatrixType m2 = MatrixType::Random(rows,cols) + Scalar(100) * MatrixType::Identity(rows,cols);
37   OtherMatrixType m3 = OtherMatrixType::Random(rows,cols) + Scalar(200) * OtherMatrixType::Identity(rows,cols);
38 
39   MatrixType m1_copy = m1;
40   MatrixType m2_copy = m2;
41   OtherMatrixType m3_copy = m3;
42 
43   // test swapping 2 matrices of same type
44   Scalar *d1=m1.data(), *d2=m2.data();
45   m1.swap(m2);
46   VERIFY_IS_APPROX(m1,m2_copy);
47   VERIFY_IS_APPROX(m2,m1_copy);
48   if(MatrixType::SizeAtCompileTime==Dynamic)
49   {
50     VERIFY(m1.data()==d2);
51     VERIFY(m2.data()==d1);
52   }
53   m1 = m1_copy;
54   m2 = m2_copy;
55 
56   // test swapping 2 matrices of different types
57   m1.swap(m3);
58   VERIFY_IS_APPROX(m1,m3_copy);
59   VERIFY_IS_APPROX(m3,m1_copy);
60   m1 = m1_copy;
61   m3 = m3_copy;
62 
63   // test swapping matrix with expression
64   m1.swap(m2.block(0,0,rows,cols));
65   VERIFY_IS_APPROX(m1,m2_copy);
66   VERIFY_IS_APPROX(m2,m1_copy);
67   m1 = m1_copy;
68   m2 = m2_copy;
69 
70   // test swapping two expressions of different types
71   m1.transpose().swap(m3.transpose());
72   VERIFY_IS_APPROX(m1,m3_copy);
73   VERIFY_IS_APPROX(m3,m1_copy);
74   m1 = m1_copy;
75   m3 = m3_copy;
76 
77   if(m1.rows()>1)
78   {
79     // test assertion on mismatching size -- matrix case
80     VERIFY_RAISES_ASSERT(m1.swap(m1.row(0)));
81     // test assertion on mismatching size -- xpr case
82     VERIFY_RAISES_ASSERT(m1.row(0).swap(m1));
83   }
84 }
85 
test_swap()86 void test_swap()
87 {
88   int s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE);
89   CALL_SUBTEST_1( swap(Matrix3f()) ); // fixed size, no vectorization
90   CALL_SUBTEST_2( swap(Matrix4d()) ); // fixed size, possible vectorization
91   CALL_SUBTEST_3( swap(MatrixXd(s,s)) ); // dyn size, no vectorization
92   CALL_SUBTEST_4( swap(MatrixXf(s,s)) ); // dyn size, possible vectorization
93   TEST_SET_BUT_UNUSED_VARIABLE(s)
94 }
95