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.6 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<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  24. typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
  25. typedef typename std::complex<typename NumTraits<typename MatrixType::Scalar>::Real> Complex;
  26. MatrixType a = MatrixType::Random(rows,cols);
  27. MatrixType a1 = MatrixType::Random(rows,cols);
  28. MatrixType symmA = a.adjoint() * a + a1.adjoint() * a1;
  29. EigenSolver<MatrixType> ei0(symmA);
  30. VERIFY_IS_EQUAL(ei0.info(), Success);
  31. VERIFY_IS_APPROX(symmA * ei0.pseudoEigenvectors(), ei0.pseudoEigenvectors() * ei0.pseudoEigenvalueMatrix());
  32. VERIFY_IS_APPROX((symmA.template cast<Complex>()) * (ei0.pseudoEigenvectors().template cast<Complex>()),
  33. (ei0.pseudoEigenvectors().template cast<Complex>()) * (ei0.eigenvalues().asDiagonal()));
  34. EigenSolver<MatrixType> ei1(a);
  35. VERIFY_IS_EQUAL(ei1.info(), Success);
  36. VERIFY_IS_APPROX(a * ei1.pseudoEigenvectors(), ei1.pseudoEigenvectors() * ei1.pseudoEigenvalueMatrix());
  37. VERIFY_IS_APPROX(a.template cast<Complex>() * ei1.eigenvectors(),
  38. ei1.eigenvectors() * ei1.eigenvalues().asDiagonal());
  39. VERIFY_IS_APPROX(ei1.eigenvectors().colwise().norm(), RealVectorType::Ones(rows).transpose());
  40. VERIFY_IS_APPROX(a.eigenvalues(), ei1.eigenvalues());
  41. EigenSolver<MatrixType> ei2;
  42. ei2.setMaxIterations(RealSchur<MatrixType>::m_maxIterationsPerRow * rows).compute(a);
  43. VERIFY_IS_EQUAL(ei2.info(), Success);
  44. VERIFY_IS_EQUAL(ei2.eigenvectors(), ei1.eigenvectors());
  45. VERIFY_IS_EQUAL(ei2.eigenvalues(), ei1.eigenvalues());
  46. if (rows > 2) {
  47. ei2.setMaxIterations(1).compute(a);
  48. VERIFY_IS_EQUAL(ei2.info(), NoConvergence);
  49. VERIFY_IS_EQUAL(ei2.getMaxIterations(), 1);
  50. }
  51. EigenSolver<MatrixType> eiNoEivecs(a, false);
  52. VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
  53. VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
  54. VERIFY_IS_APPROX(ei1.pseudoEigenvalueMatrix(), eiNoEivecs.pseudoEigenvalueMatrix());
  55. MatrixType id = MatrixType::Identity(rows, cols);
  56. VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
  57. if (rows > 2)
  58. {
  59. // Test matrix with NaN
  60. a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
  61. EigenSolver<MatrixType> eiNaN(a);
  62. VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
  63. }
  64. }
  65. template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
  66. {
  67. EigenSolver<MatrixType> eig;
  68. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  69. VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
  70. VERIFY_RAISES_ASSERT(eig.pseudoEigenvalueMatrix());
  71. VERIFY_RAISES_ASSERT(eig.eigenvalues());
  72. MatrixType a = MatrixType::Random(m.rows(),m.cols());
  73. eig.compute(a, false);
  74. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  75. VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
  76. }
  77. void test_eigensolver_generic()
  78. {
  79. int s;
  80. for(int i = 0; i < g_repeat; i++) {
  81. CALL_SUBTEST_1( eigensolver(Matrix4f()) );
  82. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  83. CALL_SUBTEST_2( eigensolver(MatrixXd(s,s)) );
  84. // some trivial but implementation-wise tricky cases
  85. CALL_SUBTEST_2( eigensolver(MatrixXd(1,1)) );
  86. CALL_SUBTEST_2( eigensolver(MatrixXd(2,2)) );
  87. CALL_SUBTEST_3( eigensolver(Matrix<double,1,1>()) );
  88. CALL_SUBTEST_4( eigensolver(Matrix2d()) );
  89. }
  90. CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4f()) );
  91. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  92. CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXd(s,s)) );
  93. CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<double,1,1>()) );
  94. CALL_SUBTEST_4( eigensolver_verify_assert(Matrix2d()) );
  95. // Test problem size constructors
  96. CALL_SUBTEST_5(EigenSolver<MatrixXf>(s));
  97. // regression test for bug 410
  98. CALL_SUBTEST_2(
  99. {
  100. MatrixXd A(1,1);
  101. A(0,0) = std::sqrt(-1.);
  102. Eigen::EigenSolver<MatrixXd> solver(A);
  103. MatrixXd V(1, 1);
  104. V(0,0) = solver.eigenvectors()(0,0).real();
  105. }
  106. );
  107. EIGEN_UNUSED_VARIABLE(s)
  108. }