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.

122 lines
4.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008-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 <limits>
  12. #include <Eigen/Eigenvalues>
  13. #include <Eigen/LU>
  14. /* Check that two column vectors are approximately equal upto permutations,
  15. by checking that the k-th power sums are equal for k = 1, ..., vec1.rows() */
  16. template<typename VectorType>
  17. void verify_is_approx_upto_permutation(const VectorType& vec1, const VectorType& vec2)
  18. {
  19. typedef typename NumTraits<typename VectorType::Scalar>::Real RealScalar;
  20. VERIFY(vec1.cols() == 1);
  21. VERIFY(vec2.cols() == 1);
  22. VERIFY(vec1.rows() == vec2.rows());
  23. for (int k = 1; k <= vec1.rows(); ++k)
  24. {
  25. VERIFY_IS_APPROX(vec1.array().pow(RealScalar(k)).sum(), vec2.array().pow(RealScalar(k)).sum());
  26. }
  27. }
  28. template<typename MatrixType> void eigensolver(const MatrixType& m)
  29. {
  30. typedef typename MatrixType::Index Index;
  31. /* this test covers the following files:
  32. ComplexEigenSolver.h, and indirectly ComplexSchur.h
  33. */
  34. Index rows = m.rows();
  35. Index cols = m.cols();
  36. typedef typename MatrixType::Scalar Scalar;
  37. typedef typename NumTraits<Scalar>::Real RealScalar;
  38. MatrixType a = MatrixType::Random(rows,cols);
  39. MatrixType symmA = a.adjoint() * a;
  40. ComplexEigenSolver<MatrixType> ei0(symmA);
  41. VERIFY_IS_EQUAL(ei0.info(), Success);
  42. VERIFY_IS_APPROX(symmA * ei0.eigenvectors(), ei0.eigenvectors() * ei0.eigenvalues().asDiagonal());
  43. ComplexEigenSolver<MatrixType> ei1(a);
  44. VERIFY_IS_EQUAL(ei1.info(), Success);
  45. VERIFY_IS_APPROX(a * ei1.eigenvectors(), ei1.eigenvectors() * ei1.eigenvalues().asDiagonal());
  46. // Note: If MatrixType is real then a.eigenvalues() uses EigenSolver and thus
  47. // another algorithm so results may differ slightly
  48. verify_is_approx_upto_permutation(a.eigenvalues(), ei1.eigenvalues());
  49. ComplexEigenSolver<MatrixType> ei2;
  50. ei2.setMaxIterations(ComplexSchur<MatrixType>::m_maxIterationsPerRow * rows).compute(a);
  51. VERIFY_IS_EQUAL(ei2.info(), Success);
  52. VERIFY_IS_EQUAL(ei2.eigenvectors(), ei1.eigenvectors());
  53. VERIFY_IS_EQUAL(ei2.eigenvalues(), ei1.eigenvalues());
  54. if (rows > 2) {
  55. ei2.setMaxIterations(1).compute(a);
  56. VERIFY_IS_EQUAL(ei2.info(), NoConvergence);
  57. VERIFY_IS_EQUAL(ei2.getMaxIterations(), 1);
  58. }
  59. ComplexEigenSolver<MatrixType> eiNoEivecs(a, false);
  60. VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
  61. VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
  62. // Regression test for issue #66
  63. MatrixType z = MatrixType::Zero(rows,cols);
  64. ComplexEigenSolver<MatrixType> eiz(z);
  65. VERIFY((eiz.eigenvalues().cwiseEqual(0)).all());
  66. MatrixType id = MatrixType::Identity(rows, cols);
  67. VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
  68. if (rows > 1)
  69. {
  70. // Test matrix with NaN
  71. a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
  72. ComplexEigenSolver<MatrixType> eiNaN(a);
  73. VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
  74. }
  75. }
  76. template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
  77. {
  78. ComplexEigenSolver<MatrixType> eig;
  79. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  80. VERIFY_RAISES_ASSERT(eig.eigenvalues());
  81. MatrixType a = MatrixType::Random(m.rows(),m.cols());
  82. eig.compute(a, false);
  83. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  84. }
  85. void test_eigensolver_complex()
  86. {
  87. int s = 0;
  88. for(int i = 0; i < g_repeat; i++) {
  89. CALL_SUBTEST_1( eigensolver(Matrix4cf()) );
  90. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  91. CALL_SUBTEST_2( eigensolver(MatrixXcd(s,s)) );
  92. CALL_SUBTEST_3( eigensolver(Matrix<std::complex<float>, 1, 1>()) );
  93. CALL_SUBTEST_4( eigensolver(Matrix3f()) );
  94. }
  95. CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4cf()) );
  96. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  97. CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXcd(s,s)) );
  98. CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<std::complex<float>, 1, 1>()) );
  99. CALL_SUBTEST_4( eigensolver_verify_assert(Matrix3f()) );
  100. // Test problem size constructors
  101. CALL_SUBTEST_5(ComplexEigenSolver<MatrixXf> tmp(s));
  102. TEST_SET_BUT_UNUSED_VARIABLE(s)
  103. }