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.

123 lines
5.1 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::ColsAtCompileTime> RightSquareMatrixType;
  29. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, Dynamic> VBlockMatrixType;
  30. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::RowsAtCompileTime> TMatrixType;
  31. Matrix<Scalar, EIGEN_SIZE_MAX(MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime), 1> _tmp((std::max)(rows,cols));
  32. Scalar* tmp = &_tmp.coeffRef(0,0);
  33. Scalar beta;
  34. RealScalar alpha;
  35. EssentialVectorType essential;
  36. VectorType v1 = VectorType::Random(rows), v2;
  37. v2 = v1;
  38. v1.makeHouseholder(essential, beta, alpha);
  39. v1.applyHouseholderOnTheLeft(essential,beta,tmp);
  40. VERIFY_IS_APPROX(v1.norm(), v2.norm());
  41. if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(v1.tail(rows-1).norm(), v1.norm());
  42. v1 = VectorType::Random(rows);
  43. v2 = v1;
  44. v1.applyHouseholderOnTheLeft(essential,beta,tmp);
  45. VERIFY_IS_APPROX(v1.norm(), v2.norm());
  46. MatrixType m1(rows, cols),
  47. m2(rows, cols);
  48. v1 = VectorType::Random(rows);
  49. if(even) v1.tail(rows-1).setZero();
  50. m1.colwise() = v1;
  51. m2 = m1;
  52. m1.col(0).makeHouseholder(essential, beta, alpha);
  53. m1.applyHouseholderOnTheLeft(essential,beta,tmp);
  54. VERIFY_IS_APPROX(m1.norm(), m2.norm());
  55. if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m1.block(1,0,rows-1,cols).norm(), m1.norm());
  56. VERIFY_IS_MUCH_SMALLER_THAN(internal::imag(m1(0,0)), internal::real(m1(0,0)));
  57. VERIFY_IS_APPROX(internal::real(m1(0,0)), alpha);
  58. v1 = VectorType::Random(rows);
  59. if(even) v1.tail(rows-1).setZero();
  60. SquareMatrixType m3(rows,rows), m4(rows,rows);
  61. m3.rowwise() = v1.transpose();
  62. m4 = m3;
  63. m3.row(0).makeHouseholder(essential, beta, alpha);
  64. m3.applyHouseholderOnTheRight(essential,beta,tmp);
  65. VERIFY_IS_APPROX(m3.norm(), m4.norm());
  66. if(rows>=2) VERIFY_IS_MUCH_SMALLER_THAN(m3.block(0,1,rows,rows-1).norm(), m3.norm());
  67. VERIFY_IS_MUCH_SMALLER_THAN(internal::imag(m3(0,0)), internal::real(m3(0,0)));
  68. VERIFY_IS_APPROX(internal::real(m3(0,0)), alpha);
  69. // test householder sequence on the left with a shift
  70. Index shift = internal::random<Index>(0, std::max<Index>(rows-2,0));
  71. Index brows = rows - shift;
  72. m1.setRandom(rows, cols);
  73. HBlockMatrixType hbm = m1.block(shift,0,brows,cols);
  74. HouseholderQR<HBlockMatrixType> qr(hbm);
  75. m2 = m1;
  76. m2.block(shift,0,brows,cols) = qr.matrixQR();
  77. HCoeffsVectorType hc = qr.hCoeffs().conjugate();
  78. HouseholderSequence<MatrixType, HCoeffsVectorType> hseq(m2, hc);
  79. hseq.setLength(hc.size()).setShift(shift);
  80. VERIFY(hseq.length() == hc.size());
  81. VERIFY(hseq.shift() == shift);
  82. MatrixType m5 = m2;
  83. m5.block(shift,0,brows,cols).template triangularView<StrictlyLower>().setZero();
  84. VERIFY_IS_APPROX(hseq * m5, m1); // test applying hseq directly
  85. m3 = hseq;
  86. VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating hseq to a dense matrix, then applying
  87. // test householder sequence on the right with a shift
  88. TMatrixType tm2 = m2.transpose();
  89. HouseholderSequence<TMatrixType, HCoeffsVectorType, OnTheRight> rhseq(tm2, hc);
  90. rhseq.setLength(hc.size()).setShift(shift);
  91. VERIFY_IS_APPROX(rhseq * m5, m1); // test applying rhseq directly
  92. m3 = rhseq;
  93. VERIFY_IS_APPROX(m3 * m5, m1); // test evaluating rhseq to a dense matrix, then applying
  94. }
  95. void test_householder()
  96. {
  97. for(int i = 0; i < g_repeat; i++) {
  98. CALL_SUBTEST_1( householder(Matrix<double,2,2>()) );
  99. CALL_SUBTEST_2( householder(Matrix<float,2,3>()) );
  100. CALL_SUBTEST_3( householder(Matrix<double,3,5>()) );
  101. CALL_SUBTEST_4( householder(Matrix<float,4,4>()) );
  102. CALL_SUBTEST_5( householder(MatrixXd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  103. CALL_SUBTEST_6( householder(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  104. CALL_SUBTEST_7( householder(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  105. CALL_SUBTEST_8( householder(Matrix<double,1,1>()) );
  106. }
  107. }