schur_real.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2010,2012 Jitse Niesen <jitse@maths.leeds.ac.uk>
  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 <limits>
  11. #include <Eigen/Eigenvalues>
  12. template<typename MatrixType> void verifyIsQuasiTriangular(const MatrixType& T)
  13. {
  14. const Index size = T.cols();
  15. typedef typename MatrixType::Scalar Scalar;
  16. // Check T is lower Hessenberg
  17. for(int row = 2; row < size; ++row) {
  18. for(int col = 0; col < row - 1; ++col) {
  19. VERIFY(T(row,col) == Scalar(0));
  20. }
  21. }
  22. // Check that any non-zero on the subdiagonal is followed by a zero and is
  23. // part of a 2x2 diagonal block with imaginary eigenvalues.
  24. for(int row = 1; row < size; ++row) {
  25. if (T(row,row-1) != Scalar(0)) {
  26. VERIFY(row == size-1 || T(row+1,row) == 0);
  27. Scalar tr = T(row-1,row-1) + T(row,row);
  28. Scalar det = T(row-1,row-1) * T(row,row) - T(row-1,row) * T(row,row-1);
  29. VERIFY(4 * det > tr * tr);
  30. }
  31. }
  32. }
  33. template<typename MatrixType> void schur(int size = MatrixType::ColsAtCompileTime)
  34. {
  35. // Test basic functionality: T is quasi-triangular and A = U T U*
  36. for(int counter = 0; counter < g_repeat; ++counter) {
  37. MatrixType A = MatrixType::Random(size, size);
  38. RealSchur<MatrixType> schurOfA(A);
  39. VERIFY_IS_EQUAL(schurOfA.info(), Success);
  40. MatrixType U = schurOfA.matrixU();
  41. MatrixType T = schurOfA.matrixT();
  42. verifyIsQuasiTriangular(T);
  43. VERIFY_IS_APPROX(A, U * T * U.transpose());
  44. }
  45. // Test asserts when not initialized
  46. RealSchur<MatrixType> rsUninitialized;
  47. VERIFY_RAISES_ASSERT(rsUninitialized.matrixT());
  48. VERIFY_RAISES_ASSERT(rsUninitialized.matrixU());
  49. VERIFY_RAISES_ASSERT(rsUninitialized.info());
  50. // Test whether compute() and constructor returns same result
  51. MatrixType A = MatrixType::Random(size, size);
  52. RealSchur<MatrixType> rs1;
  53. rs1.compute(A);
  54. RealSchur<MatrixType> rs2(A);
  55. VERIFY_IS_EQUAL(rs1.info(), Success);
  56. VERIFY_IS_EQUAL(rs2.info(), Success);
  57. VERIFY_IS_EQUAL(rs1.matrixT(), rs2.matrixT());
  58. VERIFY_IS_EQUAL(rs1.matrixU(), rs2.matrixU());
  59. // Test maximum number of iterations
  60. RealSchur<MatrixType> rs3;
  61. rs3.setMaxIterations(RealSchur<MatrixType>::m_maxIterationsPerRow * size).compute(A);
  62. VERIFY_IS_EQUAL(rs3.info(), Success);
  63. VERIFY_IS_EQUAL(rs3.matrixT(), rs1.matrixT());
  64. VERIFY_IS_EQUAL(rs3.matrixU(), rs1.matrixU());
  65. if (size > 2) {
  66. rs3.setMaxIterations(1).compute(A);
  67. VERIFY_IS_EQUAL(rs3.info(), NoConvergence);
  68. VERIFY_IS_EQUAL(rs3.getMaxIterations(), 1);
  69. }
  70. MatrixType Atriangular = A;
  71. Atriangular.template triangularView<StrictlyLower>().setZero();
  72. rs3.setMaxIterations(1).compute(Atriangular); // triangular matrices do not need any iterations
  73. VERIFY_IS_EQUAL(rs3.info(), Success);
  74. VERIFY_IS_APPROX(rs3.matrixT(), Atriangular); // approx because of scaling...
  75. VERIFY_IS_EQUAL(rs3.matrixU(), MatrixType::Identity(size, size));
  76. // Test computation of only T, not U
  77. RealSchur<MatrixType> rsOnlyT(A, false);
  78. VERIFY_IS_EQUAL(rsOnlyT.info(), Success);
  79. VERIFY_IS_EQUAL(rs1.matrixT(), rsOnlyT.matrixT());
  80. VERIFY_RAISES_ASSERT(rsOnlyT.matrixU());
  81. if (size > 2 && size < 20)
  82. {
  83. // Test matrix with NaN
  84. A(0,0) = std::numeric_limits<typename MatrixType::Scalar>::quiet_NaN();
  85. RealSchur<MatrixType> rsNaN(A);
  86. VERIFY_IS_EQUAL(rsNaN.info(), NoConvergence);
  87. }
  88. }
  89. EIGEN_DECLARE_TEST(schur_real)
  90. {
  91. CALL_SUBTEST_1(( schur<Matrix4f>() ));
  92. CALL_SUBTEST_2(( schur<MatrixXd>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4)) ));
  93. CALL_SUBTEST_3(( schur<Matrix<float, 1, 1> >() ));
  94. CALL_SUBTEST_4(( schur<Matrix<double, 3, 3, Eigen::RowMajor> >() ));
  95. // Test problem size constructors
  96. CALL_SUBTEST_5(RealSchur<MatrixXf>(10));
  97. }