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