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.

127 lines
4.2 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. typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
  46. typedef typename MatrixType::Scalar Scalar;
  47. int size = internal::random<int>(10,50);
  48. MatrixType m1(size, size), m2(size, size), m3(size, size);
  49. m1 = MatrixType::Random(size,size);
  50. if (internal::is_same<RealScalar,float>::value)
  51. {
  52. // let's build a matrix more stable to inverse
  53. MatrixType a = MatrixType::Random(size,size*2);
  54. m1 += a * a.adjoint();
  55. }
  56. HouseholderQR<MatrixType> qr(m1);
  57. m3 = MatrixType::Random(size,size);
  58. m2 = qr.solve(m3);
  59. VERIFY_IS_APPROX(m3, m1*m2);
  60. // now construct a matrix with prescribed determinant
  61. m1.setZero();
  62. for(int i = 0; i < size; i++) m1(i,i) = internal::random<Scalar>();
  63. RealScalar absdet = abs(m1.diagonal().prod());
  64. m3 = qr.householderQ(); // get a unitary
  65. m1 = m3 * m1 * m3;
  66. qr.compute(m1);
  67. VERIFY_IS_APPROX(absdet, qr.absDeterminant());
  68. VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant());
  69. }
  70. template<typename MatrixType> void qr_verify_assert()
  71. {
  72. MatrixType tmp;
  73. HouseholderQR<MatrixType> qr;
  74. VERIFY_RAISES_ASSERT(qr.matrixQR())
  75. VERIFY_RAISES_ASSERT(qr.solve(tmp))
  76. VERIFY_RAISES_ASSERT(qr.householderQ())
  77. VERIFY_RAISES_ASSERT(qr.absDeterminant())
  78. VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
  79. }
  80. void test_qr()
  81. {
  82. for(int i = 0; i < g_repeat; i++) {
  83. CALL_SUBTEST_1( qr(MatrixXf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE),internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  84. CALL_SUBTEST_2( qr(MatrixXcd(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2),internal::random<int>(1,EIGEN_TEST_MAX_SIZE/2))) );
  85. CALL_SUBTEST_3(( qr_fixedsize<Matrix<float,3,4>, 2 >() ));
  86. CALL_SUBTEST_4(( qr_fixedsize<Matrix<double,6,2>, 4 >() ));
  87. CALL_SUBTEST_5(( qr_fixedsize<Matrix<double,2,5>, 7 >() ));
  88. CALL_SUBTEST_11( qr(Matrix<float,1,1>()) );
  89. }
  90. for(int i = 0; i < g_repeat; i++) {
  91. CALL_SUBTEST_1( qr_invertible<MatrixXf>() );
  92. CALL_SUBTEST_6( qr_invertible<MatrixXd>() );
  93. CALL_SUBTEST_7( qr_invertible<MatrixXcf>() );
  94. CALL_SUBTEST_8( qr_invertible<MatrixXcd>() );
  95. }
  96. CALL_SUBTEST_9(qr_verify_assert<Matrix3f>());
  97. CALL_SUBTEST_10(qr_verify_assert<Matrix3d>());
  98. CALL_SUBTEST_1(qr_verify_assert<MatrixXf>());
  99. CALL_SUBTEST_6(qr_verify_assert<MatrixXd>());
  100. CALL_SUBTEST_7(qr_verify_assert<MatrixXcf>());
  101. CALL_SUBTEST_8(qr_verify_assert<MatrixXcd>());
  102. // Test problem size constructors
  103. CALL_SUBTEST_12(HouseholderQR<MatrixXf>(10, 20));
  104. }