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.

137 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. // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #include "main.h"
  11. #include <Eigen/QR>
  12. template<typename MatrixType> void qr()
  13. {
  14. typedef typename MatrixType::Index Index;
  15. Index rows = internal::random<Index>(20,200), cols = internal::random<int>(20,200), cols2 = internal::random<int>(20,200);
  16. Index rank = internal::random<Index>(1, (std::min)(rows, cols)-1);
  17. typedef typename MatrixType::Scalar Scalar;
  18. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, MatrixType::RowsAtCompileTime> MatrixQType;
  19. MatrixType m1;
  20. createRandomPIMatrixOfRank(rank,rows,cols,m1);
  21. FullPivHouseholderQR<MatrixType> qr(m1);
  22. VERIFY(rank == qr.rank());
  23. VERIFY(cols - qr.rank() == qr.dimensionOfKernel());
  24. VERIFY(!qr.isInjective());
  25. VERIFY(!qr.isInvertible());
  26. VERIFY(!qr.isSurjective());
  27. MatrixType r = qr.matrixQR();
  28. MatrixQType q = qr.matrixQ();
  29. VERIFY_IS_UNITARY(q);
  30. // FIXME need better way to construct trapezoid
  31. for(int i = 0; i < rows; i++) for(int j = 0; j < cols; j++) if(i>j) r(i,j) = Scalar(0);
  32. MatrixType c = qr.matrixQ() * r * qr.colsPermutation().inverse();
  33. VERIFY_IS_APPROX(m1, c);
  34. MatrixType m2 = MatrixType::Random(cols,cols2);
  35. MatrixType m3 = m1*m2;
  36. m2 = MatrixType::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. 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. FullPivHouseholderQR<MatrixType> qr(m1);
  56. VERIFY(qr.isInjective());
  57. VERIFY(qr.isInvertible());
  58. VERIFY(qr.isSurjective());
  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.matrixQ(); // get a unitary
  67. m1 = m3 * m1 * m3;
  68. qr.compute(m1);
  69. VERIFY_IS_APPROX(absdet, qr.absDeterminant());
  70. VERIFY_IS_APPROX(log(absdet), qr.logAbsDeterminant());
  71. }
  72. template<typename MatrixType> void qr_verify_assert()
  73. {
  74. MatrixType tmp;
  75. FullPivHouseholderQR<MatrixType> qr;
  76. VERIFY_RAISES_ASSERT(qr.matrixQR())
  77. VERIFY_RAISES_ASSERT(qr.solve(tmp))
  78. VERIFY_RAISES_ASSERT(qr.matrixQ())
  79. VERIFY_RAISES_ASSERT(qr.dimensionOfKernel())
  80. VERIFY_RAISES_ASSERT(qr.isInjective())
  81. VERIFY_RAISES_ASSERT(qr.isSurjective())
  82. VERIFY_RAISES_ASSERT(qr.isInvertible())
  83. VERIFY_RAISES_ASSERT(qr.inverse())
  84. VERIFY_RAISES_ASSERT(qr.absDeterminant())
  85. VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
  86. }
  87. void test_qr_fullpivoting()
  88. {
  89. for(int i = 0; i < 1; i++) {
  90. // FIXME : very weird bug here
  91. // CALL_SUBTEST(qr(Matrix2f()) );
  92. CALL_SUBTEST_1( qr<MatrixXf>() );
  93. CALL_SUBTEST_2( qr<MatrixXd>() );
  94. CALL_SUBTEST_3( qr<MatrixXcd>() );
  95. }
  96. for(int i = 0; i < g_repeat; i++) {
  97. CALL_SUBTEST_1( qr_invertible<MatrixXf>() );
  98. CALL_SUBTEST_2( qr_invertible<MatrixXd>() );
  99. CALL_SUBTEST_4( qr_invertible<MatrixXcf>() );
  100. CALL_SUBTEST_3( qr_invertible<MatrixXcd>() );
  101. }
  102. CALL_SUBTEST_5(qr_verify_assert<Matrix3f>());
  103. CALL_SUBTEST_6(qr_verify_assert<Matrix3d>());
  104. CALL_SUBTEST_1(qr_verify_assert<MatrixXf>());
  105. CALL_SUBTEST_2(qr_verify_assert<MatrixXd>());
  106. CALL_SUBTEST_4(qr_verify_assert<MatrixXcf>());
  107. CALL_SUBTEST_3(qr_verify_assert<MatrixXcd>());
  108. // Test problem size constructors
  109. CALL_SUBTEST_7(FullPivHouseholderQR<MatrixXf>(10, 20));
  110. CALL_SUBTEST_7((FullPivHouseholderQR<Matrix<float,10,20> >(10,20)));
  111. CALL_SUBTEST_7((FullPivHouseholderQR<Matrix<float,10,20> >(Matrix<float,10,20>::Random())));
  112. CALL_SUBTEST_7((FullPivHouseholderQR<Matrix<float,20,10> >(20,10)));
  113. CALL_SUBTEST_7((FullPivHouseholderQR<Matrix<float,20,10> >(Matrix<float,20,10>::Random())));
  114. }