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.

187 lines
7.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
  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. #include "sparse.h"
  10. template<int OtherStorage, typename SparseMatrixType> void sparse_permutations(const SparseMatrixType& ref)
  11. {
  12. typedef typename SparseMatrixType::Index Index;
  13. const Index rows = ref.rows();
  14. const Index cols = ref.cols();
  15. typedef typename SparseMatrixType::Scalar Scalar;
  16. typedef typename SparseMatrixType::Index Index;
  17. typedef SparseMatrix<Scalar, OtherStorage, Index> OtherSparseMatrixType;
  18. typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix;
  19. typedef Matrix<Index,Dynamic,1> VectorI;
  20. double density = (std::max)(8./(rows*cols), 0.01);
  21. SparseMatrixType mat(rows, cols), up(rows,cols), lo(rows,cols);
  22. OtherSparseMatrixType res;
  23. DenseMatrix mat_d = DenseMatrix::Zero(rows, cols), up_sym_d, lo_sym_d, res_d;
  24. initSparse<Scalar>(density, mat_d, mat, 0);
  25. up = mat.template triangularView<Upper>();
  26. lo = mat.template triangularView<Lower>();
  27. up_sym_d = mat_d.template selfadjointView<Upper>();
  28. lo_sym_d = mat_d.template selfadjointView<Lower>();
  29. VERIFY_IS_APPROX(mat, mat_d);
  30. VERIFY_IS_APPROX(up, DenseMatrix(mat_d.template triangularView<Upper>()));
  31. VERIFY_IS_APPROX(lo, DenseMatrix(mat_d.template triangularView<Lower>()));
  32. PermutationMatrix<Dynamic> p, p_null;
  33. VectorI pi;
  34. randomPermutationVector(pi, cols);
  35. p.indices() = pi;
  36. res = mat*p;
  37. res_d = mat_d*p;
  38. VERIFY(res.isApprox(res_d) && "mat*p");
  39. res = p*mat;
  40. res_d = p*mat_d;
  41. VERIFY(res.isApprox(res_d) && "p*mat");
  42. res = mat*p.inverse();
  43. res_d = mat*p.inverse();
  44. VERIFY(res.isApprox(res_d) && "mat*inv(p)");
  45. res = p.inverse()*mat;
  46. res_d = p.inverse()*mat_d;
  47. VERIFY(res.isApprox(res_d) && "inv(p)*mat");
  48. res = mat.twistedBy(p);
  49. res_d = (p * mat_d) * p.inverse();
  50. VERIFY(res.isApprox(res_d) && "p*mat*inv(p)");
  51. res = mat.template selfadjointView<Upper>().twistedBy(p_null);
  52. res_d = up_sym_d;
  53. VERIFY(res.isApprox(res_d) && "full selfadjoint upper to full");
  54. res = mat.template selfadjointView<Lower>().twistedBy(p_null);
  55. res_d = lo_sym_d;
  56. VERIFY(res.isApprox(res_d) && "full selfadjoint lower to full");
  57. res = up.template selfadjointView<Upper>().twistedBy(p_null);
  58. res_d = up_sym_d;
  59. VERIFY(res.isApprox(res_d) && "upper selfadjoint to full");
  60. res = lo.template selfadjointView<Lower>().twistedBy(p_null);
  61. res_d = lo_sym_d;
  62. VERIFY(res.isApprox(res_d) && "lower selfadjoint full");
  63. res = mat.template selfadjointView<Upper>();
  64. res_d = up_sym_d;
  65. VERIFY(res.isApprox(res_d) && "full selfadjoint upper to full");
  66. res = mat.template selfadjointView<Lower>();
  67. res_d = lo_sym_d;
  68. VERIFY(res.isApprox(res_d) && "full selfadjoint lower to full");
  69. res = up.template selfadjointView<Upper>();
  70. res_d = up_sym_d;
  71. VERIFY(res.isApprox(res_d) && "upper selfadjoint to full");
  72. res = lo.template selfadjointView<Lower>();
  73. res_d = lo_sym_d;
  74. VERIFY(res.isApprox(res_d) && "lower selfadjoint full");
  75. res.template selfadjointView<Upper>() = mat.template selfadjointView<Upper>();
  76. res_d = up_sym_d.template triangularView<Upper>();
  77. VERIFY(res.isApprox(res_d) && "full selfadjoint upper to upper");
  78. res.template selfadjointView<Lower>() = mat.template selfadjointView<Upper>();
  79. res_d = up_sym_d.template triangularView<Lower>();
  80. VERIFY(res.isApprox(res_d) && "full selfadjoint upper to lower");
  81. res.template selfadjointView<Upper>() = mat.template selfadjointView<Lower>();
  82. res_d = lo_sym_d.template triangularView<Upper>();
  83. VERIFY(res.isApprox(res_d) && "full selfadjoint lower to upper");
  84. res.template selfadjointView<Lower>() = mat.template selfadjointView<Lower>();
  85. res_d = lo_sym_d.template triangularView<Lower>();
  86. VERIFY(res.isApprox(res_d) && "full selfadjoint lower to lower");
  87. res.template selfadjointView<Upper>() = mat.template selfadjointView<Upper>().twistedBy(p);
  88. res_d = ((p * up_sym_d) * p.inverse()).eval().template triangularView<Upper>();
  89. VERIFY(res.isApprox(res_d) && "full selfadjoint upper twisted to upper");
  90. res.template selfadjointView<Upper>() = mat.template selfadjointView<Lower>().twistedBy(p);
  91. res_d = ((p * lo_sym_d) * p.inverse()).eval().template triangularView<Upper>();
  92. VERIFY(res.isApprox(res_d) && "full selfadjoint lower twisted to upper");
  93. res.template selfadjointView<Lower>() = mat.template selfadjointView<Lower>().twistedBy(p);
  94. res_d = ((p * lo_sym_d) * p.inverse()).eval().template triangularView<Lower>();
  95. VERIFY(res.isApprox(res_d) && "full selfadjoint lower twisted to lower");
  96. res.template selfadjointView<Lower>() = mat.template selfadjointView<Upper>().twistedBy(p);
  97. res_d = ((p * up_sym_d) * p.inverse()).eval().template triangularView<Lower>();
  98. VERIFY(res.isApprox(res_d) && "full selfadjoint upper twisted to lower");
  99. res.template selfadjointView<Upper>() = up.template selfadjointView<Upper>().twistedBy(p);
  100. res_d = ((p * up_sym_d) * p.inverse()).eval().template triangularView<Upper>();
  101. VERIFY(res.isApprox(res_d) && "upper selfadjoint twisted to upper");
  102. res.template selfadjointView<Upper>() = lo.template selfadjointView<Lower>().twistedBy(p);
  103. res_d = ((p * lo_sym_d) * p.inverse()).eval().template triangularView<Upper>();
  104. VERIFY(res.isApprox(res_d) && "lower selfadjoint twisted to upper");
  105. res.template selfadjointView<Lower>() = lo.template selfadjointView<Lower>().twistedBy(p);
  106. res_d = ((p * lo_sym_d) * p.inverse()).eval().template triangularView<Lower>();
  107. VERIFY(res.isApprox(res_d) && "lower selfadjoint twisted to lower");
  108. res.template selfadjointView<Lower>() = up.template selfadjointView<Upper>().twistedBy(p);
  109. res_d = ((p * up_sym_d) * p.inverse()).eval().template triangularView<Lower>();
  110. VERIFY(res.isApprox(res_d) && "upper selfadjoint twisted to lower");
  111. res = mat.template selfadjointView<Upper>().twistedBy(p);
  112. res_d = (p * up_sym_d) * p.inverse();
  113. VERIFY(res.isApprox(res_d) && "full selfadjoint upper twisted to full");
  114. res = mat.template selfadjointView<Lower>().twistedBy(p);
  115. res_d = (p * lo_sym_d) * p.inverse();
  116. VERIFY(res.isApprox(res_d) && "full selfadjoint lower twisted to full");
  117. res = up.template selfadjointView<Upper>().twistedBy(p);
  118. res_d = (p * up_sym_d) * p.inverse();
  119. VERIFY(res.isApprox(res_d) && "upper selfadjoint twisted to full");
  120. res = lo.template selfadjointView<Lower>().twistedBy(p);
  121. res_d = (p * lo_sym_d) * p.inverse();
  122. VERIFY(res.isApprox(res_d) && "lower selfadjoint twisted to full");
  123. }
  124. template<typename Scalar> void sparse_permutations_all(int size)
  125. {
  126. CALL_SUBTEST(( sparse_permutations<ColMajor>(SparseMatrix<Scalar, ColMajor>(size,size)) ));
  127. CALL_SUBTEST(( sparse_permutations<ColMajor>(SparseMatrix<Scalar, RowMajor>(size,size)) ));
  128. CALL_SUBTEST(( sparse_permutations<RowMajor>(SparseMatrix<Scalar, ColMajor>(size,size)) ));
  129. CALL_SUBTEST(( sparse_permutations<RowMajor>(SparseMatrix<Scalar, RowMajor>(size,size)) ));
  130. }
  131. void test_sparse_permutations()
  132. {
  133. for(int i = 0; i < g_repeat; i++) {
  134. int s = Eigen::internal::random<int>(1,50);
  135. CALL_SUBTEST_1(( sparse_permutations_all<double>(s) ));
  136. CALL_SUBTEST_2(( sparse_permutations_all<std::complex<double> >(s) ));
  137. }
  138. }