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.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. // 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. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
  20. MatrixType m1;
  21. createRandomPIMatrixOfRank(rank,rows,cols,m1);
  22. FullPivHouseholderQR<MatrixType> qr(m1);
  23. VERIFY(rank == qr.rank());
  24. VERIFY(cols - qr.rank() == qr.dimensionOfKernel());
  25. VERIFY(!qr.isInjective());
  26. VERIFY(!qr.isInvertible());
  27. VERIFY(!qr.isSurjective());
  28. MatrixType r = qr.matrixQR();
  29. MatrixQType q = qr.matrixQ();
  30. VERIFY_IS_UNITARY(q);
  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. MatrixType c = qr.matrixQ() * r * qr.colsPermutation().inverse();
  34. VERIFY_IS_APPROX(m1, c);
  35. MatrixType m2 = MatrixType::Random(cols,cols2);
  36. MatrixType m3 = m1*m2;
  37. m2 = MatrixType::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. typedef typename NumTraits<typename MatrixType::Scalar>::Real RealScalar;
  44. typedef typename MatrixType::Scalar Scalar;
  45. int size = internal::random<int>(10,50);
  46. MatrixType m1(size, size), m2(size, size), m3(size, size);
  47. m1 = MatrixType::Random(size,size);
  48. if (internal::is_same<RealScalar,float>::value)
  49. {
  50. // let's build a matrix more stable to inverse
  51. MatrixType a = MatrixType::Random(size,size*2);
  52. m1 += a * a.adjoint();
  53. }
  54. FullPivHouseholderQR<MatrixType> qr(m1);
  55. VERIFY(qr.isInjective());
  56. VERIFY(qr.isInvertible());
  57. VERIFY(qr.isSurjective());
  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 = internal::abs(m1.diagonal().prod());
  65. m3 = qr.matrixQ(); // get a unitary
  66. m1 = m3 * m1 * m3;
  67. qr.compute(m1);
  68. VERIFY_IS_APPROX(absdet, qr.absDeterminant());
  69. VERIFY_IS_APPROX(internal::log(absdet), qr.logAbsDeterminant());
  70. }
  71. template<typename MatrixType> void qr_verify_assert()
  72. {
  73. MatrixType tmp;
  74. FullPivHouseholderQR<MatrixType> qr;
  75. VERIFY_RAISES_ASSERT(qr.matrixQR())
  76. VERIFY_RAISES_ASSERT(qr.solve(tmp))
  77. VERIFY_RAISES_ASSERT(qr.matrixQ())
  78. VERIFY_RAISES_ASSERT(qr.dimensionOfKernel())
  79. VERIFY_RAISES_ASSERT(qr.isInjective())
  80. VERIFY_RAISES_ASSERT(qr.isSurjective())
  81. VERIFY_RAISES_ASSERT(qr.isInvertible())
  82. VERIFY_RAISES_ASSERT(qr.inverse())
  83. VERIFY_RAISES_ASSERT(qr.absDeterminant())
  84. VERIFY_RAISES_ASSERT(qr.logAbsDeterminant())
  85. }
  86. void test_qr_fullpivoting()
  87. {
  88. for(int i = 0; i < 1; i++) {
  89. // FIXME : very weird bug here
  90. // CALL_SUBTEST(qr(Matrix2f()) );
  91. CALL_SUBTEST_1( qr<MatrixXf>() );
  92. CALL_SUBTEST_2( qr<MatrixXd>() );
  93. CALL_SUBTEST_3( qr<MatrixXcd>() );
  94. }
  95. for(int i = 0; i < g_repeat; i++) {
  96. CALL_SUBTEST_1( qr_invertible<MatrixXf>() );
  97. CALL_SUBTEST_2( qr_invertible<MatrixXd>() );
  98. CALL_SUBTEST_4( qr_invertible<MatrixXcf>() );
  99. CALL_SUBTEST_3( qr_invertible<MatrixXcd>() );
  100. }
  101. CALL_SUBTEST_5(qr_verify_assert<Matrix3f>());
  102. CALL_SUBTEST_6(qr_verify_assert<Matrix3d>());
  103. CALL_SUBTEST_1(qr_verify_assert<MatrixXf>());
  104. CALL_SUBTEST_2(qr_verify_assert<MatrixXd>());
  105. CALL_SUBTEST_4(qr_verify_assert<MatrixXcf>());
  106. CALL_SUBTEST_3(qr_verify_assert<MatrixXcd>());
  107. // Test problem size constructors
  108. CALL_SUBTEST_7(FullPivHouseholderQR<MatrixXf>(10, 20));
  109. }