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.

138 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-2010 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. #include "main.h"
  10. #include <Eigen/QR>
  11. template<typename MatrixType> void householder(const MatrixType& m)
  12. {
  13. typedef typename MatrixType::Index Index;
  14. static bool even = true;
  15. even = !even;
  16. /* this test covers the following files:
  17. Householder.h
  18. */
  19. Index rows = m.rows();
  20. Index cols = m.cols();
  21. typedef typename MatrixType::Scalar Scalar;
  22. typedef typename NumTraits<Scalar>::Real RealScalar;
  23. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  24. typedef Matrix<Scalar, internal::decrement_size<MatrixType::RowsAtCompileTime>::ret, 1> EssentialVectorType;
  25. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> SquareMatrixType;
  26. typedef Matrix<Scalar, Dynamic, MatrixType::ColsAtCompileTime> HBlockMatrixType;
  27. typedef Matrix<Scalar, Dynamic, 1> HCoeffsVectorType;
  28. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::RowsAtCompileTime> TMatrixType;
  29. Matrix<Scalar, EIGEN_SIZE_MAX(MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime), 1> _tmp((std::max)(rows,cols));
  30. Scalar* tmp = &_tmp.coeffRef(0,0);
  31. Scalar beta;
  32. RealScalar alpha;
  33. EssentialVectorType essential;
  34. VectorType v1 = VectorType::Random(rows), v2;
  35. v2 = v1;
  36. v1.makeHouseholder(essential, beta, alpha);
  37. v1.applyHouseholderOnTheLeft(essential,beta,tmp);
  38. VERIFY_IS_APPROX(v1.norm(), v2.norm());
  39. if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(v1.tail(rows-1).norm(), v1.norm());
  40. v1 = VectorType::Random(rows);
  41. v2 = v1;
  42. v1.applyHouseholderOnTheLeft(essential,beta,tmp);
  43. VERIFY_IS_APPROX(v1.norm(), v2.norm());
  44. MatrixType m1(rows, cols),
  45. m2(rows, cols);
  46. v1 = VectorType::Random(rows);
  47. if(even) v1.tail(rows-1).setZero();
  48. m1.colwise() = v1;
  49. m2 = m1;
  50. m1.col(0).makeHouseholder(essential, beta, alpha);
  51. m1.applyHouseholderOnTheLeft(essential,beta,tmp);
  52. VERIFY_IS_APPROX(m1.norm(), m2.norm());
  53. if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m1.block(1,0,rows-1,cols).norm(), m1.norm());
  54. VERIFY_IS_MUCH_SMALLER_THAN(numext::imag(m1(0,0)), numext::real(m1(0,0)));
  55. VERIFY_IS_APPROX(numext::real(m1(0,0)), alpha);
  56. v1 = VectorType::Random(rows);
  57. if(even) v1.tail(rows-1).setZero();
  58. SquareMatrixType m3(rows,rows), m4(rows,rows);
  59. m3.rowwise() = v1.transpose();
  60. m4 = m3;
  61. m3.row(0).makeHouseholder(essential, beta, alpha);
  62. m3.applyHouseholderOnTheRight(essential,beta,tmp);
  63. VERIFY_IS_APPROX(m3.norm(), m4.norm());
  64. if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m3.block(0,1,rows,rows-1).norm(), m3.norm());
  65. VERIFY_IS_MUCH_SMALLER_THAN(numext::imag(m3(0,0)), numext::real(m3(0,0)));
  66. VERIFY_IS_APPROX(numext::real(m3(0,0)), alpha);
  67. // test householder sequence on the left with a shift
  68. Index shift = internal::random<Index>(0, std::max<Index>(rows-2,0));
  69. Index brows = rows - shift;
  70. m1.setRandom(rows, cols);
  71. HBlockMatrixType hbm = m1.block(shift,0,brows,cols);
  72. HouseholderQR<HBlockMatrixType> qr(hbm);
  73. m2 = m1;
  74. m2.block(shift,0,brows,cols) = qr.matrixQR();
  75. HCoeffsVectorType hc = qr.hCoeffs().conjugate();
  76. HouseholderSequence<MatrixType, HCoeffsVectorType> hseq(m2, hc);
  77. hseq.setLength(hc.size()).setShift(shift);
  78. VERIFY(hseq.length() == hc.size());
  79. VERIFY(hseq.shift() == shift);
  80. MatrixType m5 = m2;
  81. m5.block(shift,0,brows,cols).template triangularView<StrictlyLower>().setZero();
  82. VERIFY_IS_APPROX(hseq * m5, m1); // test applying hseq directly
  83. m3 = hseq;
  84. VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating hseq to a dense matrix, then applying
  85. SquareMatrixType hseq_mat = hseq;
  86. SquareMatrixType hseq_mat_conj = hseq.conjugate();
  87. SquareMatrixType hseq_mat_adj = hseq.adjoint();
  88. SquareMatrixType hseq_mat_trans = hseq.transpose();
  89. SquareMatrixType m6 = SquareMatrixType::Random(rows, rows);
  90. VERIFY_IS_APPROX(hseq_mat.adjoint(), hseq_mat_adj);
  91. VERIFY_IS_APPROX(hseq_mat.conjugate(), hseq_mat_conj);
  92. VERIFY_IS_APPROX(hseq_mat.transpose(), hseq_mat_trans);
  93. VERIFY_IS_APPROX(hseq_mat * m6, hseq_mat * m6);
  94. VERIFY_IS_APPROX(hseq_mat.adjoint() * m6, hseq_mat_adj * m6);
  95. VERIFY_IS_APPROX(hseq_mat.conjugate() * m6, hseq_mat_conj * m6);
  96. VERIFY_IS_APPROX(hseq_mat.transpose() * m6, hseq_mat_trans * m6);
  97. VERIFY_IS_APPROX(m6 * hseq_mat, m6 * hseq_mat);
  98. VERIFY_IS_APPROX(m6 * hseq_mat.adjoint(), m6 * hseq_mat_adj);
  99. VERIFY_IS_APPROX(m6 * hseq_mat.conjugate(), m6 * hseq_mat_conj);
  100. VERIFY_IS_APPROX(m6 * hseq_mat.transpose(), m6 * hseq_mat_trans);
  101. // test householder sequence on the right with a shift
  102. TMatrixType tm2 = m2.transpose();
  103. HouseholderSequence<TMatrixType, HCoeffsVectorType, OnTheRight> rhseq(tm2, hc);
  104. rhseq.setLength(hc.size()).setShift(shift);
  105. VERIFY_IS_APPROX(rhseq * m5, m1); // test applying rhseq directly
  106. m3 = rhseq;
  107. VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating rhseq to a dense matrix, then applying
  108. }
  109. void test_householder()
  110. {
  111. for(int i = 0; i < g_repeat; i++) {
  112. CALL_SUBTEST_1( householder(Matrix<double,2,2>()) );
  113. CALL_SUBTEST_2( householder(Matrix<float,2,3>()) );
  114. CALL_SUBTEST_3( householder(Matrix<double,3,5>()) );
  115. CALL_SUBTEST_4( householder(Matrix<float,4,4>()) );
  116. CALL_SUBTEST_5( householder(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  117. CALL_SUBTEST_6( householder(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  118. CALL_SUBTEST_7( householder(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  119. CALL_SUBTEST_8( householder(Matrix<double,1,1>()) );
  120. }
  121. }