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.

93 lines
3.1 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2010 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 computation of only T, not U
  61. RealSchur<MatrixType> rsOnlyT(A, false);
  62. VERIFY_IS_EQUAL(rsOnlyT.info(), Success);
  63. VERIFY_IS_EQUAL(rs1.matrixT(), rsOnlyT.matrixT());
  64. VERIFY_RAISES_ASSERT(rsOnlyT.matrixU());
  65. if (size > 2)
  66. {
  67. // Test matrix with NaN
  68. A(0,0) = std::numeric_limits<typename MatrixType::Scalar>::quiet_NaN();
  69. RealSchur<MatrixType> rsNaN(A);
  70. VERIFY_IS_EQUAL(rsNaN.info(), NoConvergence);
  71. }
  72. }
  73. void test_schur_real()
  74. {
  75. CALL_SUBTEST_1(( schur<Matrix4f>() ));
  76. CALL_SUBTEST_2(( schur<MatrixXd>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4)) ));
  77. CALL_SUBTEST_3(( schur<Matrix<float, 1, 1> >() ));
  78. CALL_SUBTEST_4(( schur<Matrix<double, 3, 3, Eigen::RowMajor> >() ));
  79. // Test problem size constructors
  80. CALL_SUBTEST_5(RealSchur<MatrixXf>(10));
  81. }