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.

91 lines
3.5 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 schur(int size = MatrixType::ColsAtCompileTime)
  13. {
  14. typedef typename ComplexSchur<MatrixType>::ComplexScalar ComplexScalar;
  15. typedef typename ComplexSchur<MatrixType>::ComplexMatrixType ComplexMatrixType;
  16. // Test basic functionality: T is triangular and A = U T U*
  17. for(int counter = 0; counter < g_repeat; ++counter) {
  18. MatrixType A = MatrixType::Random(size, size);
  19. ComplexSchur<MatrixType> schurOfA(A);
  20. VERIFY_IS_EQUAL(schurOfA.info(), Success);
  21. ComplexMatrixType U = schurOfA.matrixU();
  22. ComplexMatrixType T = schurOfA.matrixT();
  23. for(int row = 1; row < size; ++row) {
  24. for(int col = 0; col < row; ++col) {
  25. VERIFY(T(row,col) == (typename MatrixType::Scalar)0);
  26. }
  27. }
  28. VERIFY_IS_APPROX(A.template cast<ComplexScalar>(), U * T * U.adjoint());
  29. }
  30. // Test asserts when not initialized
  31. ComplexSchur<MatrixType> csUninitialized;
  32. VERIFY_RAISES_ASSERT(csUninitialized.matrixT());
  33. VERIFY_RAISES_ASSERT(csUninitialized.matrixU());
  34. VERIFY_RAISES_ASSERT(csUninitialized.info());
  35. // Test whether compute() and constructor returns same result
  36. MatrixType A = MatrixType::Random(size, size);
  37. ComplexSchur<MatrixType> cs1;
  38. cs1.compute(A);
  39. ComplexSchur<MatrixType> cs2(A);
  40. VERIFY_IS_EQUAL(cs1.info(), Success);
  41. VERIFY_IS_EQUAL(cs2.info(), Success);
  42. VERIFY_IS_EQUAL(cs1.matrixT(), cs2.matrixT());
  43. VERIFY_IS_EQUAL(cs1.matrixU(), cs2.matrixU());
  44. // Test maximum number of iterations
  45. ComplexSchur<MatrixType> cs3;
  46. cs3.setMaxIterations(ComplexSchur<MatrixType>::m_maxIterationsPerRow * size).compute(A);
  47. VERIFY_IS_EQUAL(cs3.info(), Success);
  48. VERIFY_IS_EQUAL(cs3.matrixT(), cs1.matrixT());
  49. VERIFY_IS_EQUAL(cs3.matrixU(), cs1.matrixU());
  50. cs3.setMaxIterations(1).compute(A);
  51. VERIFY_IS_EQUAL(cs3.info(), size > 1 ? NoConvergence : Success);
  52. VERIFY_IS_EQUAL(cs3.getMaxIterations(), 1);
  53. MatrixType Atriangular = A;
  54. Atriangular.template triangularView<StrictlyLower>().setZero();
  55. cs3.setMaxIterations(1).compute(Atriangular); // triangular matrices do not need any iterations
  56. VERIFY_IS_EQUAL(cs3.info(), Success);
  57. VERIFY_IS_EQUAL(cs3.matrixT(), Atriangular.template cast<ComplexScalar>());
  58. VERIFY_IS_EQUAL(cs3.matrixU(), ComplexMatrixType::Identity(size, size));
  59. // Test computation of only T, not U
  60. ComplexSchur<MatrixType> csOnlyT(A, false);
  61. VERIFY_IS_EQUAL(csOnlyT.info(), Success);
  62. VERIFY_IS_EQUAL(cs1.matrixT(), csOnlyT.matrixT());
  63. VERIFY_RAISES_ASSERT(csOnlyT.matrixU());
  64. if (size > 1 && size < 20)
  65. {
  66. // Test matrix with NaN
  67. A(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
  68. ComplexSchur<MatrixType> csNaN(A);
  69. VERIFY_IS_EQUAL(csNaN.info(), NoConvergence);
  70. }
  71. }
  72. void test_schur_complex()
  73. {
  74. CALL_SUBTEST_1(( schur<Matrix4cd>() ));
  75. CALL_SUBTEST_2(( schur<MatrixXcf>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4)) ));
  76. CALL_SUBTEST_3(( schur<Matrix<std::complex<float>, 1, 1> >() ));
  77. CALL_SUBTEST_4(( schur<Matrix<float, 3, 3, StormEigen::RowMajor> >() ));
  78. // Test problem size constructors
  79. CALL_SUBTEST_5(ComplexSchur<MatrixXf>(10));
  80. }