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.

144 lines
5.2 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2010,2012 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. template<typename MatrixType> void eigensolver(const MatrixType& m)
  14. {
  15. typedef typename MatrixType::Index Index;
  16. /* this test covers the following files:
  17. EigenSolver.h
  18. */
  19. Index rows = m.rows();
  20. Index cols = m.cols();
  21. typedef typename MatrixType::Scalar Scalar;
  22. typedef typename NumTraits<Scalar>::Real RealScalar;
  23. typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
  24. typedef typename std::complex<typename NumTraits<typename MatrixType::Scalar>::Real> Complex;
  25. MatrixType a = MatrixType::Random(rows,cols);
  26. MatrixType a1 = MatrixType::Random(rows,cols);
  27. MatrixType symmA = a.adjoint() * a + a1.adjoint() * a1;
  28. EigenSolver<MatrixType> ei0(symmA);
  29. VERIFY_IS_EQUAL(ei0.info(), Success);
  30. VERIFY_IS_APPROX(symmA * ei0.pseudoEigenvectors(), ei0.pseudoEigenvectors() * ei0.pseudoEigenvalueMatrix());
  31. VERIFY_IS_APPROX((symmA.template cast<Complex>()) * (ei0.pseudoEigenvectors().template cast<Complex>()),
  32. (ei0.pseudoEigenvectors().template cast<Complex>()) * (ei0.eigenvalues().asDiagonal()));
  33. EigenSolver<MatrixType> ei1(a);
  34. VERIFY_IS_EQUAL(ei1.info(), Success);
  35. VERIFY_IS_APPROX(a * ei1.pseudoEigenvectors(), ei1.pseudoEigenvectors() * ei1.pseudoEigenvalueMatrix());
  36. VERIFY_IS_APPROX(a.template cast<Complex>() * ei1.eigenvectors(),
  37. ei1.eigenvectors() * ei1.eigenvalues().asDiagonal());
  38. VERIFY_IS_APPROX(ei1.eigenvectors().colwise().norm(), RealVectorType::Ones(rows).transpose());
  39. VERIFY_IS_APPROX(a.eigenvalues(), ei1.eigenvalues());
  40. EigenSolver<MatrixType> ei2;
  41. ei2.setMaxIterations(RealSchur<MatrixType>::m_maxIterationsPerRow * rows).compute(a);
  42. VERIFY_IS_EQUAL(ei2.info(), Success);
  43. VERIFY_IS_EQUAL(ei2.eigenvectors(), ei1.eigenvectors());
  44. VERIFY_IS_EQUAL(ei2.eigenvalues(), ei1.eigenvalues());
  45. if (rows > 2) {
  46. ei2.setMaxIterations(1).compute(a);
  47. VERIFY_IS_EQUAL(ei2.info(), NoConvergence);
  48. VERIFY_IS_EQUAL(ei2.getMaxIterations(), 1);
  49. }
  50. EigenSolver<MatrixType> eiNoEivecs(a, false);
  51. VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
  52. VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
  53. VERIFY_IS_APPROX(ei1.pseudoEigenvalueMatrix(), eiNoEivecs.pseudoEigenvalueMatrix());
  54. MatrixType id = MatrixType::Identity(rows, cols);
  55. VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
  56. if (rows > 2 && rows < 20)
  57. {
  58. // Test matrix with NaN
  59. a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
  60. EigenSolver<MatrixType> eiNaN(a);
  61. VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
  62. }
  63. // regression test for bug 1098
  64. {
  65. EigenSolver<MatrixType> eig(a.adjoint() * a);
  66. eig.compute(a.adjoint() * a);
  67. }
  68. }
  69. template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
  70. {
  71. EigenSolver<MatrixType> eig;
  72. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  73. VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
  74. VERIFY_RAISES_ASSERT(eig.pseudoEigenvalueMatrix());
  75. VERIFY_RAISES_ASSERT(eig.eigenvalues());
  76. MatrixType a = MatrixType::Random(m.rows(),m.cols());
  77. eig.compute(a, false);
  78. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  79. VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
  80. }
  81. void test_eigensolver_generic()
  82. {
  83. int s = 0;
  84. for(int i = 0; i < g_repeat; i++) {
  85. CALL_SUBTEST_1( eigensolver(Matrix4f()) );
  86. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  87. CALL_SUBTEST_2( eigensolver(MatrixXd(s,s)) );
  88. TEST_SET_BUT_UNUSED_VARIABLE(s)
  89. // some trivial but implementation-wise tricky cases
  90. CALL_SUBTEST_2( eigensolver(MatrixXd(1,1)) );
  91. CALL_SUBTEST_2( eigensolver(MatrixXd(2,2)) );
  92. CALL_SUBTEST_3( eigensolver(Matrix<double,1,1>()) );
  93. CALL_SUBTEST_4( eigensolver(Matrix2d()) );
  94. }
  95. CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4f()) );
  96. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  97. CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXd(s,s)) );
  98. CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<double,1,1>()) );
  99. CALL_SUBTEST_4( eigensolver_verify_assert(Matrix2d()) );
  100. // Test problem size constructors
  101. CALL_SUBTEST_5(EigenSolver<MatrixXf> tmp(s));
  102. // regression test for bug 410
  103. CALL_SUBTEST_2(
  104. {
  105. MatrixXd A(1,1);
  106. A(0,0) = std::sqrt(-1.); // is Not-a-Number
  107. Eigen::EigenSolver<MatrixXd> solver(A);
  108. VERIFY_IS_EQUAL(solver.info(), NumericalIssue);
  109. }
  110. );
  111. // regression test for bug 793
  112. #ifdef EIGEN_TEST_PART_2
  113. {
  114. MatrixXd a(3,3);
  115. a << 0, 0, 1,
  116. 1, 1, 1,
  117. 1, 1e+200, 1;
  118. Eigen::EigenSolver<MatrixXd> eig(a);
  119. VERIFY_IS_APPROX(a * eig.pseudoEigenvectors(), eig.pseudoEigenvectors() * eig.pseudoEigenvalueMatrix());
  120. VERIFY_IS_APPROX(a * eig.eigenvectors(), eig.eigenvectors() * eig.eigenvalues().asDiagonal());
  121. }
  122. #endif
  123. TEST_SET_BUT_UNUSED_VARIABLE(s)
  124. }