householder.cpp 6.1 KB

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