You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.6 KiB

  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. #define EIGEN_NO_STATIC_ASSERT
  10. #include "main.h"
  11. template<typename T>
  12. struct other_matrix_type
  13. {
  14. typedef int type;
  15. };
  16. template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
  17. struct other_matrix_type<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
  18. {
  19. typedef Matrix<_Scalar, _Rows, _Cols, _Options^RowMajor, _MaxRows, _MaxCols> type;
  20. };
  21. template<typename MatrixType> void swap(const MatrixType& m)
  22. {
  23. typedef typename other_matrix_type<MatrixType>::type OtherMatrixType;
  24. typedef typename MatrixType::Scalar Scalar;
  25. ei_assert((!ei_is_same_type<MatrixType,OtherMatrixType>::ret));
  26. int rows = m.rows();
  27. int cols = m.cols();
  28. // construct 3 matrix guaranteed to be distinct
  29. MatrixType m1 = MatrixType::Random(rows,cols);
  30. MatrixType m2 = MatrixType::Random(rows,cols) + Scalar(100) * MatrixType::Identity(rows,cols);
  31. OtherMatrixType m3 = OtherMatrixType::Random(rows,cols) + Scalar(200) * OtherMatrixType::Identity(rows,cols);
  32. MatrixType m1_copy = m1;
  33. MatrixType m2_copy = m2;
  34. OtherMatrixType m3_copy = m3;
  35. // test swapping 2 matrices of same type
  36. m1.swap(m2);
  37. VERIFY_IS_APPROX(m1,m2_copy);
  38. VERIFY_IS_APPROX(m2,m1_copy);
  39. m1 = m1_copy;
  40. m2 = m2_copy;
  41. // test swapping 2 matrices of different types
  42. m1.swap(m3);
  43. VERIFY_IS_APPROX(m1,m3_copy);
  44. VERIFY_IS_APPROX(m3,m1_copy);
  45. m1 = m1_copy;
  46. m3 = m3_copy;
  47. // test swapping matrix with expression
  48. m1.swap(m2.block(0,0,rows,cols));
  49. VERIFY_IS_APPROX(m1,m2_copy);
  50. VERIFY_IS_APPROX(m2,m1_copy);
  51. m1 = m1_copy;
  52. m2 = m2_copy;
  53. // test swapping two expressions of different types
  54. m1.transpose().swap(m3.transpose());
  55. VERIFY_IS_APPROX(m1,m3_copy);
  56. VERIFY_IS_APPROX(m3,m1_copy);
  57. m1 = m1_copy;
  58. m3 = m3_copy;
  59. // test assertion on mismatching size -- matrix case
  60. VERIFY_RAISES_ASSERT(m1.swap(m1.row(0)));
  61. // test assertion on mismatching size -- xpr case
  62. VERIFY_RAISES_ASSERT(m1.row(0).swap(m1));
  63. }
  64. void test_eigen2_swap()
  65. {
  66. CALL_SUBTEST_1( swap(Matrix3f()) ); // fixed size, no vectorization
  67. CALL_SUBTEST_1( swap(Matrix4d()) ); // fixed size, possible vectorization
  68. CALL_SUBTEST_1( swap(MatrixXd(3,3)) ); // dyn size, no vectorization
  69. CALL_SUBTEST_1( swap(MatrixXf(30,30)) ); // dyn size, possible vectorization
  70. }