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.

115 lines
4.1 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> eiNoEivecs(a, false);
  53. VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
  54. VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
  55. // Regression test for issue #66
  56. MatrixType z = MatrixType::Zero(rows,cols);
  57. ComplexEigenSolver<MatrixType> eiz(z);
  58. VERIFY((eiz.eigenvalues().cwiseEqual(0)).all());
  59. MatrixType id = MatrixType::Identity(rows, cols);
  60. VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
  61. if (rows > 1)
  62. {
  63. // Test matrix with NaN
  64. a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
  65. ComplexEigenSolver<MatrixType> eiNaN(a);
  66. VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
  67. }
  68. }
  69. template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
  70. {
  71. ComplexEigenSolver<MatrixType> eig;
  72. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  73. VERIFY_RAISES_ASSERT(eig.eigenvalues());
  74. MatrixType a = MatrixType::Random(m.rows(),m.cols());
  75. eig.compute(a, false);
  76. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  77. }
  78. void test_eigensolver_complex()
  79. {
  80. int s;
  81. for(int i = 0; i < g_repeat; i++) {
  82. CALL_SUBTEST_1( eigensolver(Matrix4cf()) );
  83. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  84. CALL_SUBTEST_2( eigensolver(MatrixXcd(s,s)) );
  85. CALL_SUBTEST_3( eigensolver(Matrix<std::complex<float>, 1, 1>()) );
  86. CALL_SUBTEST_4( eigensolver(Matrix3f()) );
  87. }
  88. CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4cf()) );
  89. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  90. CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXcd(s,s)) );
  91. CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<std::complex<float>, 1, 1>()) );
  92. CALL_SUBTEST_4( eigensolver_verify_assert(Matrix3f()) );
  93. // Test problem size constructors
  94. CALL_SUBTEST_5(ComplexEigenSolver<MatrixXf>(s));
  95. EIGEN_UNUSED_VARIABLE(s)
  96. }