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.

112 lines
3.9 KiB

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