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.

126 lines
4.3 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. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
  19. MatrixType a = MatrixType::Random(rows,cols);
  20. HouseholderQR<MatrixType> qrOfA(a);
  21. MatrixQType q = qrOfA.householderQ();
  22. VERIFY_IS_UNITARY(q);
  23. MatrixType r = qrOfA.matrixQR().template triangularView<Upper>();
  24. VERIFY_IS_APPROX(a, qrOfA.householderQ() * r);
  25. }
  26. template<typename MatrixType, int Cols2> void qr_fixedsize()
  27. {
  28. enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime };
  29. typedef typename MatrixType::Scalar Scalar;
  30. Matrix<Scalar,Rows,Cols> m1 = Matrix<Scalar,Rows,Cols>::Random();
  31. HouseholderQR<Matrix<Scalar,Rows,Cols> > qr(m1);
  32. Matrix<Scalar,Rows,Cols> r = qr.matrixQR();
  33. // FIXME need better way to construct trapezoid
  34. for(int i = 0; i < Rows; i++) for(int j = 0; j < Cols; j++) if(i>j) r(i,j) = Scalar(0);
  35. VERIFY_IS_APPROX(m1, qr.householderQ() * r);
  36. Matrix<Scalar,Cols,Cols2> m2 = Matrix<Scalar,Cols,Cols2>::Random(Cols,Cols2);
  37. Matrix<Scalar,Rows,Cols2> m3 = m1*m2;
  38. m2 = Matrix<Scalar,Cols,Cols2>::Random(Cols,Cols2);
  39. m2 = qr.solve(m3);
  40. VERIFY_IS_APPROX(m3, m1*m2);
  41. }
  42. template<typename MatrixType> void qr_invertible()
  43. {
  44. typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
  45. typedef typename MatrixType::Scalar Scalar;
  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*2);
  53. m1 += a * a.adjoint();
  54. }
  55. HouseholderQR<MatrixType> qr(m1);
  56. m3 = MatrixType::Random(size,size);
  57. m2 = qr.solve(m3);
  58. VERIFY_IS_APPROX(m3, m1*m2);
  59. // now construct a matrix with prescribed determinant
  60. m1.setZero();
  61. for(int i = 0; i < size; i++) m1(i,i) = internal::random<Scalar>();
  62. RealScalar absdet = internal::abs(m1.diagonal().prod());
  63. m3 = qr.householderQ(); // get a unitary
  64. m1 = m3 * m1 * m3;
  65. qr.compute(m1);
  66. VERIFY_IS_APPROX(absdet, qr.absDeterminant());
  67. VERIFY_IS_APPROX(internal::log(absdet), qr.logAbsDeterminant());
  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.householderQ())
  76. VERIFY_RAISES_ASSERT(qr.absDeterminant())
  77. VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
  78. }
  79. void test_qr()
  80. {
  81. for(int i = 0; i < g_repeat; i++) {
  82. CALL_SUBTEST_1( qr(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  83. CALL_SUBTEST_2( qr(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2),internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  84. CALL_SUBTEST_3(( qr_fixedsize<Matrix<float,3,4>, 2 >() ));
  85. CALL_SUBTEST_4(( qr_fixedsize<Matrix<double,6,2>, 4 >() ));
  86. CALL_SUBTEST_5(( qr_fixedsize<Matrix<double,2,5>, 7 >() ));
  87. CALL_SUBTEST_11( qr(Matrix<float,1,1>()) );
  88. }
  89. for(int i = 0; i < g_repeat; i++) {
  90. CALL_SUBTEST_1( qr_invertible<MatrixXf>() );
  91. CALL_SUBTEST_6( qr_invertible<MatrixXd>() );
  92. CALL_SUBTEST_7( qr_invertible<MatrixXcf>() );
  93. CALL_SUBTEST_8( qr_invertible<MatrixXcd>() );
  94. }
  95. CALL_SUBTEST_9(qr_verify_assert<Matrix3f>());
  96. CALL_SUBTEST_10(qr_verify_assert<Matrix3d>());
  97. CALL_SUBTEST_1(qr_verify_assert<MatrixXf>());
  98. CALL_SUBTEST_6(qr_verify_assert<MatrixXd>());
  99. CALL_SUBTEST_7(qr_verify_assert<MatrixXcf>());
  100. CALL_SUBTEST_8(qr_verify_assert<MatrixXcd>());
  101. // Test problem size constructors
  102. CALL_SUBTEST_12(HouseholderQR<MatrixXf>(10, 20));
  103. }