swap.cpp
Go to the documentation of this file.
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>
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 {
23 };
24 
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 
31  Index rows = m.rows();
32  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 
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
94 }
s
RealScalar s
Definition: level1_cplx_impl.h:126
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
eigen_assert
#define eigen_assert(x)
Definition: Macros.h:1037
m1
Matrix3d m1
Definition: IOFormat.cpp:2
other_matrix_type< Matrix< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::type
Matrix< _Scalar, _Rows, _Cols, _Options^RowMajor, _MaxRows, _MaxCols > type
Definition: swap.cpp:22
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
other_matrix_type::type
int type
Definition: swap.cpp:16
m2
MatrixType m2(n_dims)
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
Eigen::Dynamic
const int Dynamic
Definition: Constants.h:22
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
swap
void swap(const MatrixType &m)
Definition: swap.cpp:25
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
test_docs.d2
d2
Definition: test_docs.py:29
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
other_matrix_type
Definition: swap.cpp:14
m3
static const DiscreteKey m3(M(3), 2)
main.h
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
test_callbacks.value
value
Definition: test_callbacks.py:160
TEST_SET_BUT_UNUSED_VARIABLE
#define TEST_SET_BUT_UNUSED_VARIABLE(X)
Definition: main.h:121
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(swap)
Definition: swap.cpp:86
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:05:15