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.

150 lines
5.8 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 TEST_ENABLE_TEMPORARY_TRACKING
  10. #include "main.h"
  11. using namespace std;
  12. template<typename MatrixType> void permutationmatrices(const MatrixType& m)
  13. {
  14. typedef typename MatrixType::Index Index;
  15. typedef typename MatrixType::Scalar Scalar;
  16. enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime,
  17. Options = MatrixType::Options };
  18. typedef PermutationMatrix<Rows> LeftPermutationType;
  19. typedef Matrix<int, Rows, 1> LeftPermutationVectorType;
  20. typedef Map<LeftPermutationType> MapLeftPerm;
  21. typedef PermutationMatrix<Cols> RightPermutationType;
  22. typedef Matrix<int, Cols, 1> RightPermutationVectorType;
  23. typedef Map<RightPermutationType> MapRightPerm;
  24. Index rows = m.rows();
  25. Index cols = m.cols();
  26. MatrixType m_original = MatrixType::Random(rows,cols);
  27. LeftPermutationVectorType lv;
  28. randomPermutationVector(lv, rows);
  29. LeftPermutationType lp(lv);
  30. RightPermutationVectorType rv;
  31. randomPermutationVector(rv, cols);
  32. RightPermutationType rp(rv);
  33. MatrixType m_permuted = MatrixType::Random(rows,cols);
  34. const int one_if_dynamic = MatrixType::SizeAtCompileTime==Dynamic ? 1 : 0;
  35. VERIFY_EVALUATION_COUNT(m_permuted = lp * m_original * rp, one_if_dynamic); // 1 temp for sub expression "lp * m_original"
  36. for (int i=0; i<rows; i++)
  37. for (int j=0; j<cols; j++)
  38. VERIFY_IS_APPROX(m_permuted(lv(i),j), m_original(i,rv(j)));
  39. Matrix<Scalar,Rows,Rows> lm(lp);
  40. Matrix<Scalar,Cols,Cols> rm(rp);
  41. VERIFY_IS_APPROX(m_permuted, lm*m_original*rm);
  42. m_permuted = m_original;
  43. VERIFY_EVALUATION_COUNT(m_permuted = lp * m_permuted * rp, one_if_dynamic);
  44. VERIFY_IS_APPROX(m_permuted, lm*m_original*rm);
  45. VERIFY_IS_APPROX(lp.inverse()*m_permuted*rp.inverse(), m_original);
  46. VERIFY_IS_APPROX(lv.asPermutation().inverse()*m_permuted*rv.asPermutation().inverse(), m_original);
  47. VERIFY_IS_APPROX(MapLeftPerm(lv.data(),lv.size()).inverse()*m_permuted*MapRightPerm(rv.data(),rv.size()).inverse(), m_original);
  48. VERIFY((lp*lp.inverse()).toDenseMatrix().isIdentity());
  49. VERIFY((lv.asPermutation()*lv.asPermutation().inverse()).toDenseMatrix().isIdentity());
  50. VERIFY((MapLeftPerm(lv.data(),lv.size())*MapLeftPerm(lv.data(),lv.size()).inverse()).toDenseMatrix().isIdentity());
  51. LeftPermutationVectorType lv2;
  52. randomPermutationVector(lv2, rows);
  53. LeftPermutationType lp2(lv2);
  54. Matrix<Scalar,Rows,Rows> lm2(lp2);
  55. VERIFY_IS_APPROX((lp*lp2).toDenseMatrix().template cast<Scalar>(), lm*lm2);
  56. VERIFY_IS_APPROX((lv.asPermutation()*lv2.asPermutation()).toDenseMatrix().template cast<Scalar>(), lm*lm2);
  57. VERIFY_IS_APPROX((MapLeftPerm(lv.data(),lv.size())*MapLeftPerm(lv2.data(),lv2.size())).toDenseMatrix().template cast<Scalar>(), lm*lm2);
  58. LeftPermutationType identityp;
  59. identityp.setIdentity(rows);
  60. VERIFY_IS_APPROX(m_original, identityp*m_original);
  61. // check inplace permutations
  62. m_permuted = m_original;
  63. VERIFY_EVALUATION_COUNT(m_permuted.noalias()= lp.inverse() * m_permuted, one_if_dynamic); // 1 temp to allocate the mask
  64. VERIFY_IS_APPROX(m_permuted, lp.inverse()*m_original);
  65. m_permuted = m_original;
  66. VERIFY_EVALUATION_COUNT(m_permuted.noalias() = m_permuted * rp.inverse(), one_if_dynamic); // 1 temp to allocate the mask
  67. VERIFY_IS_APPROX(m_permuted, m_original*rp.inverse());
  68. m_permuted = m_original;
  69. VERIFY_EVALUATION_COUNT(m_permuted.noalias() = lp * m_permuted, one_if_dynamic); // 1 temp to allocate the mask
  70. VERIFY_IS_APPROX(m_permuted, lp*m_original);
  71. m_permuted = m_original;
  72. VERIFY_EVALUATION_COUNT(m_permuted.noalias() = m_permuted * rp, one_if_dynamic); // 1 temp to allocate the mask
  73. VERIFY_IS_APPROX(m_permuted, m_original*rp);
  74. if(rows>1 && cols>1)
  75. {
  76. lp2 = lp;
  77. Index i = internal::random<Index>(0, rows-1);
  78. Index j;
  79. do j = internal::random<Index>(0, rows-1); while(j==i);
  80. lp2.applyTranspositionOnTheLeft(i, j);
  81. lm = lp;
  82. lm.row(i).swap(lm.row(j));
  83. VERIFY_IS_APPROX(lm, lp2.toDenseMatrix().template cast<Scalar>());
  84. RightPermutationType rp2 = rp;
  85. i = internal::random<Index>(0, cols-1);
  86. do j = internal::random<Index>(0, cols-1); while(j==i);
  87. rp2.applyTranspositionOnTheRight(i, j);
  88. rm = rp;
  89. rm.col(i).swap(rm.col(j));
  90. VERIFY_IS_APPROX(rm, rp2.toDenseMatrix().template cast<Scalar>());
  91. }
  92. }
  93. template<typename T>
  94. void bug890()
  95. {
  96. typedef Matrix<T, Dynamic, Dynamic> MatrixType;
  97. typedef Matrix<T, Dynamic, 1> VectorType;
  98. typedef Stride<Dynamic,Dynamic> S;
  99. typedef Map<MatrixType, Aligned, S> MapType;
  100. typedef PermutationMatrix<Dynamic> Perm;
  101. VectorType v1(2), v2(2), op(4), rhs(2);
  102. v1 << 666,667;
  103. op << 1,0,0,1;
  104. rhs << 42,42;
  105. Perm P(2);
  106. P.indices() << 1, 0;
  107. MapType(v1.data(),2,1,S(1,1)) = P * MapType(rhs.data(),2,1,S(1,1));
  108. VERIFY_IS_APPROX(v1, (P * rhs).eval());
  109. MapType(v1.data(),2,1,S(1,1)) = P.inverse() * MapType(rhs.data(),2,1,S(1,1));
  110. VERIFY_IS_APPROX(v1, (P.inverse() * rhs).eval());
  111. }
  112. void test_permutationmatrices()
  113. {
  114. for(int i = 0; i < g_repeat; i++) {
  115. CALL_SUBTEST_1( permutationmatrices(Matrix<float, 1, 1>()) );
  116. CALL_SUBTEST_2( permutationmatrices(Matrix3f()) );
  117. CALL_SUBTEST_3( permutationmatrices(Matrix<double,3,3,RowMajor>()) );
  118. CALL_SUBTEST_4( permutationmatrices(Matrix4d()) );
  119. CALL_SUBTEST_5( permutationmatrices(Matrix<double,40,60>()) );
  120. CALL_SUBTEST_6( permutationmatrices(Matrix<double,Dynamic,Dynamic,RowMajor>(20, 30)) );
  121. CALL_SUBTEST_7( permutationmatrices(MatrixXcf(15, 10)) );
  122. }
  123. CALL_SUBTEST_5( bug890<double>() );
  124. }