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.

62 lines
2.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
  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/Eigenvalues>
  12. template<typename Scalar,int Size> void hessenberg(int size = Size)
  13. {
  14. typedef Matrix<Scalar,Size,Size> MatrixType;
  15. // Test basic functionality: A = U H U* and H is Hessenberg
  16. for(int counter = 0; counter < g_repeat; ++counter) {
  17. MatrixType m = MatrixType::Random(size,size);
  18. HessenbergDecomposition<MatrixType> hess(m);
  19. MatrixType Q = hess.matrixQ();
  20. MatrixType H = hess.matrixH();
  21. VERIFY_IS_APPROX(m, Q * H * Q.adjoint());
  22. for(int row = 2; row < size; ++row) {
  23. for(int col = 0; col < row-1; ++col) {
  24. VERIFY(H(row,col) == (typename MatrixType::Scalar)0);
  25. }
  26. }
  27. }
  28. // Test whether compute() and constructor returns same result
  29. MatrixType A = MatrixType::Random(size, size);
  30. HessenbergDecomposition<MatrixType> cs1;
  31. cs1.compute(A);
  32. HessenbergDecomposition<MatrixType> cs2(A);
  33. VERIFY_IS_EQUAL(cs1.matrixH().eval(), cs2.matrixH().eval());
  34. MatrixType cs1Q = cs1.matrixQ();
  35. MatrixType cs2Q = cs2.matrixQ();
  36. VERIFY_IS_EQUAL(cs1Q, cs2Q);
  37. // Test assertions for when used uninitialized
  38. HessenbergDecomposition<MatrixType> hessUninitialized;
  39. VERIFY_RAISES_ASSERT( hessUninitialized.matrixH() );
  40. VERIFY_RAISES_ASSERT( hessUninitialized.matrixQ() );
  41. VERIFY_RAISES_ASSERT( hessUninitialized.householderCoefficients() );
  42. VERIFY_RAISES_ASSERT( hessUninitialized.packedMatrix() );
  43. // TODO: Add tests for packedMatrix() and householderCoefficients()
  44. }
  45. void test_hessenberg()
  46. {
  47. CALL_SUBTEST_1(( hessenberg<std::complex<double>,1>() ));
  48. CALL_SUBTEST_2(( hessenberg<std::complex<double>,2>() ));
  49. CALL_SUBTEST_3(( hessenberg<std::complex<float>,4>() ));
  50. CALL_SUBTEST_4(( hessenberg<float,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE)) ));
  51. CALL_SUBTEST_5(( hessenberg<std::complex<double>,Dynamic>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE)) ));
  52. // Test problem size constructors
  53. CALL_SUBTEST_6(HessenbergDecomposition<MatrixXf>(10));
  54. }