qr.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 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 "main.h"
  10. #include <Eigen/QR>
  11. #include "solverbase.h"
  12. template<typename MatrixType> void qr(const MatrixType& m)
  13. {
  14. Index rows = m.rows();
  15. Index cols = m.cols();
  16. typedef typename MatrixType::Scalar Scalar;
  17. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> MatrixQType;
  18. MatrixType a = MatrixType::Random(rows,cols);
  19. HouseholderQR<MatrixType> qrOfA(a);
  20. MatrixQType q = qrOfA.householderQ();
  21. VERIFY_IS_UNITARY(q);
  22. MatrixType r = qrOfA.matrixQR().template triangularView<Upper>();
  23. VERIFY_IS_APPROX(a, qrOfA.householderQ() * r);
  24. }
  25. template<typename MatrixType, int Cols2> void qr_fixedsize()
  26. {
  27. enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
  28. typedef typename MatrixType::Scalar Scalar;
  29. Matrix<Scalar,Rows,Cols> m1 = Matrix<Scalar,Rows,Cols>::Random();
  30. HouseholderQR<Matrix<Scalar,Rows,Cols> > qr(m1);
  31. Matrix<Scalar,Rows,Cols> r = qr.matrixQR();
  32. // FIXME need better way to construct trapezoid
  33. for(int i = 0; i < Rows; i++) for(int j = 0; j < Cols; j++) if(i>j) r(i,j) = Scalar(0);
  34. VERIFY_IS_APPROX(m1, qr.householderQ() * r);
  35. check_solverbase<Matrix<Scalar,Cols,Cols2>, Matrix<Scalar,Rows,Cols2> >(m1, qr, Rows, Cols, Cols2);
  36. }
  37. template<typename MatrixType> void qr_invertible()
  38. {
  39. using std::log;
  40. using std::abs;
  41. using std::pow;
  42. using std::max;
  43. typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
  44. typedef typename MatrixType::Scalar Scalar;
  45. STATIC_CHECK(( internal::is_same<typename HouseholderQR<MatrixType>::StorageIndex,int>::value ));
  46. int size = internal::random<int>(10,50);
  47. MatrixType m1(size, size), m2(size, size), m3(size, size);
  48. m1 = MatrixType::Random(size,size);
  49. if (internal::is_same<RealScalar,float>::value)
  50. {
  51. // let's build a matrix more stable to inverse
  52. MatrixType a = MatrixType::Random(size,size*4);
  53. m1 += a * a.adjoint();
  54. }
  55. HouseholderQR<MatrixType> qr(m1);
  56. check_solverbase<MatrixType, MatrixType>(m1, qr, size, size, size);
  57. // now construct a matrix with prescribed determinant
  58. m1.setZero();
  59. for(int i = 0; i < size; i++) m1(i,i) = internal::random<Scalar>();
  60. RealScalar absdet = abs(m1.diagonal().prod());
  61. m3 = qr.householderQ(); // get a unitary
  62. m1 = m3 * m1 * m3;
  63. qr.compute(m1);
  64. VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant());
  65. // This test is tricky if the determinant becomes too small.
  66. // Since we generate random numbers with magnitude range [0,1], the average determinant is 0.5^size
  67. VERIFY_IS_MUCH_SMALLER_THAN( abs(absdet-qr.absDeterminant()), numext::maxi(RealScalar(pow(0.5,size)),numext::maxi<RealScalar>(abs(absdet),abs(qr.absDeterminant()))) );
  68. }
  69. template<typename MatrixType> void qr_verify_assert()
  70. {
  71. MatrixType tmp;
  72. HouseholderQR<MatrixType> qr;
  73. VERIFY_RAISES_ASSERT(qr.matrixQR())
  74. VERIFY_RAISES_ASSERT(qr.solve(tmp))
  75. VERIFY_RAISES_ASSERT(qr.transpose().solve(tmp))
  76. VERIFY_RAISES_ASSERT(qr.adjoint().solve(tmp))
  77. VERIFY_RAISES_ASSERT(qr.householderQ())
  78. VERIFY_RAISES_ASSERT(qr.absDeterminant())
  79. VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
  80. }
  81. EIGEN_DECLARE_TEST(qr)
  82. {
  83. for(int i = 0; i < g_repeat; i++) {
  84. CALL_SUBTEST_1( qr(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  85. CALL_SUBTEST_2( qr(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2),internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  86. CALL_SUBTEST_3(( qr_fixedsize<Matrix<float,3,4>, 2 >() ));
  87. CALL_SUBTEST_4(( qr_fixedsize<Matrix<double,6,2>, 4 >() ));
  88. CALL_SUBTEST_5(( qr_fixedsize<Matrix<double,2,5>, 7 >() ));
  89. CALL_SUBTEST_11( qr(Matrix<float,1,1>()) );
  90. }
  91. for(int i = 0; i < g_repeat; i++) {
  92. CALL_SUBTEST_1( qr_invertible<MatrixXf>() );
  93. CALL_SUBTEST_6( qr_invertible<MatrixXd>() );
  94. CALL_SUBTEST_7( qr_invertible<MatrixXcf>() );
  95. CALL_SUBTEST_8( qr_invertible<MatrixXcd>() );
  96. }
  97. CALL_SUBTEST_9(qr_verify_assert<Matrix3f>());
  98. CALL_SUBTEST_10(qr_verify_assert<Matrix3d>());
  99. CALL_SUBTEST_1(qr_verify_assert<MatrixXf>());
  100. CALL_SUBTEST_6(qr_verify_assert<MatrixXd>());
  101. CALL_SUBTEST_7(qr_verify_assert<MatrixXcf>());
  102. CALL_SUBTEST_8(qr_verify_assert<MatrixXcd>());
  103. // Test problem size constructors
  104. CALL_SUBTEST_12(HouseholderQR<MatrixXf>(10, 20));
  105. }