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.

126 lines
4.5 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. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  39. typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
  40. typedef typename std::complex<typename NumTraits<typename MatrixType::Scalar>::Real> Complex;
  41. MatrixType a = MatrixType::Random(rows,cols);
  42. MatrixType symmA = a.adjoint() * a;
  43. ComplexEigenSolver<MatrixType> ei0(symmA);
  44. VERIFY_IS_EQUAL(ei0.info(), Success);
  45. VERIFY_IS_APPROX(symmA * ei0.eigenvectors(), ei0.eigenvectors() * ei0.eigenvalues().asDiagonal());
  46. ComplexEigenSolver<MatrixType> ei1(a);
  47. VERIFY_IS_EQUAL(ei1.info(), Success);
  48. VERIFY_IS_APPROX(a * ei1.eigenvectors(), ei1.eigenvectors() * ei1.eigenvalues().asDiagonal());
  49. // Note: If MatrixType is real then a.eigenvalues() uses EigenSolver and thus
  50. // another algorithm so results may differ slightly
  51. verify_is_approx_upto_permutation(a.eigenvalues(), ei1.eigenvalues());
  52. ComplexEigenSolver<MatrixType> ei2;
  53. ei2.setMaxIterations(ComplexSchur<MatrixType>::m_maxIterationsPerRow * rows).compute(a);
  54. VERIFY_IS_EQUAL(ei2.info(), Success);
  55. VERIFY_IS_EQUAL(ei2.eigenvectors(), ei1.eigenvectors());
  56. VERIFY_IS_EQUAL(ei2.eigenvalues(), ei1.eigenvalues());
  57. if (rows > 2) {
  58. ei2.setMaxIterations(1).compute(a);
  59. VERIFY_IS_EQUAL(ei2.info(), NoConvergence);
  60. VERIFY_IS_EQUAL(ei2.getMaxIterations(), 1);
  61. }
  62. ComplexEigenSolver<MatrixType> eiNoEivecs(a, false);
  63. VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
  64. VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
  65. // Regression test for issue #66
  66. MatrixType z = MatrixType::Zero(rows,cols);
  67. ComplexEigenSolver<MatrixType> eiz(z);
  68. VERIFY((eiz.eigenvalues().cwiseEqual(0)).all());
  69. MatrixType id = MatrixType::Identity(rows, cols);
  70. VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
  71. if (rows > 1)
  72. {
  73. // Test matrix with NaN
  74. a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
  75. ComplexEigenSolver<MatrixType> eiNaN(a);
  76. VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
  77. }
  78. }
  79. template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
  80. {
  81. ComplexEigenSolver<MatrixType> eig;
  82. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  83. VERIFY_RAISES_ASSERT(eig.eigenvalues());
  84. MatrixType a = MatrixType::Random(m.rows(),m.cols());
  85. eig.compute(a, false);
  86. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  87. }
  88. void test_eigensolver_complex()
  89. {
  90. int s;
  91. for(int i = 0; i < g_repeat; i++) {
  92. CALL_SUBTEST_1( eigensolver(Matrix4cf()) );
  93. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  94. CALL_SUBTEST_2( eigensolver(MatrixXcd(s,s)) );
  95. CALL_SUBTEST_3( eigensolver(Matrix<std::complex<float>, 1, 1>()) );
  96. CALL_SUBTEST_4( eigensolver(Matrix3f()) );
  97. }
  98. CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4cf()) );
  99. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  100. CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXcd(s,s)) );
  101. CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<std::complex<float>, 1, 1>()) );
  102. CALL_SUBTEST_4( eigensolver_verify_assert(Matrix3f()) );
  103. // Test problem size constructors
  104. CALL_SUBTEST_5(ComplexEigenSolver<MatrixXf>(s));
  105. EIGEN_UNUSED_VARIABLE(s)
  106. }