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.

168 lines
5.8 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. template<typename MatrixType> bool find_pivot(typename MatrixType::Scalar tol, MatrixType &diffs, Index col=0)
  15. {
  16. bool match = diffs.diagonal().sum() <= tol;
  17. if(match || col==diffs.cols())
  18. {
  19. return match;
  20. }
  21. else
  22. {
  23. Index n = diffs.cols();
  24. std::vector<std::pair<Index,Index> > transpositions;
  25. for(Index i=col; i<n; ++i)
  26. {
  27. Index best_index(0);
  28. if(diffs.col(col).segment(col,n-i).minCoeff(&best_index) > tol)
  29. break;
  30. best_index += col;
  31. diffs.row(col).swap(diffs.row(best_index));
  32. if(find_pivot(tol,diffs,col+1)) return true;
  33. diffs.row(col).swap(diffs.row(best_index));
  34. // move current pivot to the end
  35. diffs.row(n-(i-col)-1).swap(diffs.row(best_index));
  36. transpositions.push_back(std::pair<Index,Index>(n-(i-col)-1,best_index));
  37. }
  38. // restore
  39. for(Index k=transpositions.size()-1; k>=0; --k)
  40. diffs.row(transpositions[k].first).swap(diffs.row(transpositions[k].second));
  41. }
  42. return false;
  43. }
  44. /* Check that two column vectors are approximately equal upto permutations.
  45. * Initially, this method checked that the k-th power sums are equal for all k = 1, ..., vec1.rows(),
  46. * however this strategy is numerically inacurate because of numerical cancellation issues.
  47. */
  48. template<typename VectorType>
  49. void verify_is_approx_upto_permutation(const VectorType& vec1, const VectorType& vec2)
  50. {
  51. typedef typename VectorType::Scalar Scalar;
  52. typedef typename NumTraits<Scalar>::Real RealScalar;
  53. VERIFY(vec1.cols() == 1);
  54. VERIFY(vec2.cols() == 1);
  55. VERIFY(vec1.rows() == vec2.rows());
  56. Index n = vec1.rows();
  57. RealScalar tol = test_precision<RealScalar>()*test_precision<RealScalar>()*numext::maxi(vec1.squaredNorm(),vec2.squaredNorm());
  58. Matrix<RealScalar,Dynamic,Dynamic> diffs = (vec1.rowwise().replicate(n) - vec2.rowwise().replicate(n).transpose()).cwiseAbs2();
  59. VERIFY( find_pivot(tol, diffs) );
  60. }
  61. template<typename MatrixType> void eigensolver(const MatrixType& m)
  62. {
  63. typedef typename MatrixType::Index Index;
  64. /* this test covers the following files:
  65. ComplexEigenSolver.h, and indirectly ComplexSchur.h
  66. */
  67. Index rows = m.rows();
  68. Index cols = m.cols();
  69. typedef typename MatrixType::Scalar Scalar;
  70. typedef typename NumTraits<Scalar>::Real RealScalar;
  71. MatrixType a = MatrixType::Random(rows,cols);
  72. MatrixType symmA = a.adjoint() * a;
  73. ComplexEigenSolver<MatrixType> ei0(symmA);
  74. VERIFY_IS_EQUAL(ei0.info(), Success);
  75. VERIFY_IS_APPROX(symmA * ei0.eigenvectors(), ei0.eigenvectors() * ei0.eigenvalues().asDiagonal());
  76. ComplexEigenSolver<MatrixType> ei1(a);
  77. VERIFY_IS_EQUAL(ei1.info(), Success);
  78. VERIFY_IS_APPROX(a * ei1.eigenvectors(), ei1.eigenvectors() * ei1.eigenvalues().asDiagonal());
  79. // Note: If MatrixType is real then a.eigenvalues() uses EigenSolver and thus
  80. // another algorithm so results may differ slightly
  81. verify_is_approx_upto_permutation(a.eigenvalues(), ei1.eigenvalues());
  82. ComplexEigenSolver<MatrixType> ei2;
  83. ei2.setMaxIterations(ComplexSchur<MatrixType>::m_maxIterationsPerRow * rows).compute(a);
  84. VERIFY_IS_EQUAL(ei2.info(), Success);
  85. VERIFY_IS_EQUAL(ei2.eigenvectors(), ei1.eigenvectors());
  86. VERIFY_IS_EQUAL(ei2.eigenvalues(), ei1.eigenvalues());
  87. if (rows > 2) {
  88. ei2.setMaxIterations(1).compute(a);
  89. VERIFY_IS_EQUAL(ei2.info(), NoConvergence);
  90. VERIFY_IS_EQUAL(ei2.getMaxIterations(), 1);
  91. }
  92. ComplexEigenSolver<MatrixType> eiNoEivecs(a, false);
  93. VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
  94. VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
  95. // Regression test for issue #66
  96. MatrixType z = MatrixType::Zero(rows,cols);
  97. ComplexEigenSolver<MatrixType> eiz(z);
  98. VERIFY((eiz.eigenvalues().cwiseEqual(0)).all());
  99. MatrixType id = MatrixType::Identity(rows, cols);
  100. VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
  101. if (rows > 1 && rows < 20)
  102. {
  103. // Test matrix with NaN
  104. a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
  105. ComplexEigenSolver<MatrixType> eiNaN(a);
  106. VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
  107. }
  108. // regression test for bug 1098
  109. {
  110. ComplexEigenSolver<MatrixType> eig(a.adjoint() * a);
  111. eig.compute(a.adjoint() * a);
  112. }
  113. }
  114. template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
  115. {
  116. ComplexEigenSolver<MatrixType> eig;
  117. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  118. VERIFY_RAISES_ASSERT(eig.eigenvalues());
  119. MatrixType a = MatrixType::Random(m.rows(),m.cols());
  120. eig.compute(a, false);
  121. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  122. }
  123. void test_eigensolver_complex()
  124. {
  125. int s = 0;
  126. for(int i = 0; i < g_repeat; i++) {
  127. CALL_SUBTEST_1( eigensolver(Matrix4cf()) );
  128. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  129. CALL_SUBTEST_2( eigensolver(MatrixXcd(s,s)) );
  130. CALL_SUBTEST_3( eigensolver(Matrix<std::complex<float>, 1, 1>()) );
  131. CALL_SUBTEST_4( eigensolver(Matrix3f()) );
  132. TEST_SET_BUT_UNUSED_VARIABLE(s)
  133. }
  134. CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4cf()) );
  135. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  136. CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXcd(s,s)) );
  137. CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<std::complex<float>, 1, 1>()) );
  138. CALL_SUBTEST_4( eigensolver_verify_assert(Matrix3f()) );
  139. // Test problem size constructors
  140. CALL_SUBTEST_5(ComplexEigenSolver<MatrixXf> tmp(s));
  141. TEST_SET_BUT_UNUSED_VARIABLE(s)
  142. }