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.

132 lines
4.5 KiB

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