swap.cpp File Reference
#include "main.h"

Classes

struct  other_matrix_type< T >
 
struct  other_matrix_type< Matrix< Scalar_, Rows_, Cols_, Options_, MaxRows_, MaxCols_ > >
 

Functions

template<typename MatrixType >
std::enable_if_t<(MatrixType::RowsAtCompileTime==1||MatrixType::RowsAtCompileTime==Dynamic), void > check_row_swap (MatrixType &m1)
 
template<typename MatrixType >
std::enable_if_t<!(MatrixType::RowsAtCompileTime==1||MatrixType::RowsAtCompileTime==Dynamic), void > check_row_swap (MatrixType &)
 
template<typename MatrixType >
void swap (const MatrixType &m)
 
 EIGEN_DECLARE_TEST (swap)
 

Function Documentation

◆ check_row_swap() [1/2]

template<typename MatrixType >
std::enable_if_t<!(MatrixType::RowsAtCompileTime == 1 || MatrixType::RowsAtCompileTime == Dynamic), void> check_row_swap ( MatrixType )
35 {}

◆ check_row_swap() [2/2]

template<typename MatrixType >
std::enable_if_t<(MatrixType::RowsAtCompileTime == 1 || MatrixType::RowsAtCompileTime == Dynamic), void> check_row_swap ( MatrixType m1)
24  {
25  if (m1.rows() != 1) {
26  // test assertion on mismatching size -- matrix case
27  VERIFY_RAISES_ASSERT(m1.swap(m1.row(0)));
28  // test assertion on mismatching size -- xpr case
29  VERIFY_RAISES_ASSERT(m1.row(0).swap(m1));
30  }
31 }
Matrix3d m1
Definition: IOFormat.cpp:2
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:329

References m1, and VERIFY_RAISES_ASSERT.

Referenced by swap().

◆ EIGEN_DECLARE_TEST()

EIGEN_DECLARE_TEST ( swap  )
116  {
117  int s = internal::random<int>(1, EIGEN_TEST_MAX_SIZE);
118  CALL_SUBTEST_1(swap(Matrix3f())); // fixed size, no vectorization
119  CALL_SUBTEST_2(swap(Matrix4d())); // fixed size, possible vectorization
120  CALL_SUBTEST_3(swap(MatrixXd(s, s))); // dyn size, no vectorization
121  CALL_SUBTEST_4(swap(MatrixXf(s, s))); // dyn size, possible vectorization
123 }
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
RealScalar s
Definition: level1_cplx_impl.h:130
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:139
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
void swap(const MatrixType &m)
Definition: swap.cpp:38

References CALL_SUBTEST_1, CALL_SUBTEST_2, CALL_SUBTEST_3, CALL_SUBTEST_4, EIGEN_TEST_MAX_SIZE, s, swap(), and TEST_SET_BUT_UNUSED_VARIABLE.

◆ swap()

template<typename MatrixType >
void swap ( const MatrixType m)
38  {
39  typedef typename other_matrix_type<MatrixType>::type OtherMatrixType;
40  typedef typename MatrixType::Scalar Scalar;
41 
43  Index rows = m.rows();
44  Index cols = m.cols();
45 
46  // construct 3 matrix guaranteed to be distinct
47  MatrixType m1 = MatrixType::Random(rows, cols);
48  MatrixType m2 = MatrixType::Random(rows, cols) + Scalar(100) * MatrixType::Identity(rows, cols);
49  OtherMatrixType m3 = OtherMatrixType::Random(rows, cols) + Scalar(200) * OtherMatrixType::Identity(rows, cols);
50 
51  MatrixType m1_copy = m1;
52  MatrixType m2_copy = m2;
53  OtherMatrixType m3_copy = m3;
54 
55  // test swapping 2 matrices of same type
56  Scalar *d1 = m1.data(), *d2 = m2.data();
57  m1.swap(m2);
58  VERIFY_IS_APPROX(m1, m2_copy);
59  VERIFY_IS_APPROX(m2, m1_copy);
60  if (MatrixType::SizeAtCompileTime == Dynamic) {
61  VERIFY(m1.data() == d2);
62  VERIFY(m2.data() == d1);
63  }
64  m1 = m1_copy;
65  m2 = m2_copy;
66  d1 = m1.data(), d2 = m2.data();
67  swap(m1, m2);
68  VERIFY_IS_APPROX(m1, m2_copy);
69  VERIFY_IS_APPROX(m2, m1_copy);
70  if (MatrixType::SizeAtCompileTime == Dynamic) {
71  VERIFY(m1.data() == d2);
72  VERIFY(m2.data() == d1);
73  }
74  m1 = m1_copy;
75  m2 = m2_copy;
76 
77  // test swapping 2 matrices of different types
78  m1.swap(m3);
79  VERIFY_IS_APPROX(m1, m3_copy);
80  VERIFY_IS_APPROX(m3, m1_copy);
81  m1 = m1_copy;
82  m3 = m3_copy;
83  swap(m1, m3);
84  VERIFY_IS_APPROX(m1, m3_copy);
85  VERIFY_IS_APPROX(m3, m1_copy);
86  m1 = m1_copy;
87  m3 = m3_copy;
88 
89  // test swapping matrix with expression
90  m1.swap(m2.block(0, 0, rows, cols));
91  VERIFY_IS_APPROX(m1, m2_copy);
92  VERIFY_IS_APPROX(m2, m1_copy);
93  m1 = m1_copy;
94  m2 = m2_copy;
95  swap(m1, m2.block(0, 0, rows, cols));
96  VERIFY_IS_APPROX(m1, m2_copy);
97  VERIFY_IS_APPROX(m2, m1_copy);
98  m1 = m1_copy;
99  m2 = m2_copy;
100 
101  // test swapping two expressions of different types
102  m1.transpose().swap(m3.transpose());
103  VERIFY_IS_APPROX(m1, m3_copy);
104  VERIFY_IS_APPROX(m3, m1_copy);
105  m1 = m1_copy;
106  m3 = m3_copy;
107  swap(m1.transpose(), m3.transpose());
108  VERIFY_IS_APPROX(m1, m3_copy);
109  VERIFY_IS_APPROX(m3, m1_copy);
110  m1 = m1_copy;
111  m3 = m3_copy;
112 
114 }
#define eigen_assert(x)
Definition: Macros.h:910
MatrixType m2(n_dims)
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:13
int * m
Definition: level2_cplx_impl.h:294
#define VERIFY(a)
Definition: main.h:362
squared absolute value
Definition: GlobalFunctions.h:87
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
int type
Definition: swap.cpp:14
std::enable_if_t<(MatrixType::RowsAtCompileTime==1||MatrixType::RowsAtCompileTime==Dynamic), void > check_row_swap(MatrixType &m1)
Definition: swap.cpp:23

References check_row_swap(), cols, Eigen::Dynamic, eigen_assert, m, m1, m2(), rows, Eigen::value, VERIFY, and VERIFY_IS_APPROX.

Referenced by EIGEN_DECLARE_TEST().