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.

146 lines
5.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. #include <Eigen/QR>
  11. #ifdef HAS_GSL
  12. #include "gsl_helper.h"
  13. #endif
  14. template<typename MatrixType> void selfadjointeigensolver(const MatrixType& m)
  15. {
  16. /* this test covers the following files:
  17. EigenSolver.h, SelfAdjointEigenSolver.h (and indirectly: Tridiagonalization.h)
  18. */
  19. int rows = m.rows();
  20. int 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. RealScalar largerEps = 10*test_precision<RealScalar>();
  27. MatrixType a = MatrixType::Random(rows,cols);
  28. MatrixType a1 = MatrixType::Random(rows,cols);
  29. MatrixType symmA = a.adjoint() * a + a1.adjoint() * a1;
  30. MatrixType b = MatrixType::Random(rows,cols);
  31. MatrixType b1 = MatrixType::Random(rows,cols);
  32. MatrixType symmB = b.adjoint() * b + b1.adjoint() * b1;
  33. SelfAdjointEigenSolver<MatrixType> eiSymm(symmA);
  34. // generalized eigen pb
  35. SelfAdjointEigenSolver<MatrixType> eiSymmGen(symmA, symmB);
  36. #ifdef HAS_GSL
  37. if (ei_is_same_type<RealScalar,double>::ret)
  38. {
  39. typedef GslTraits<Scalar> Gsl;
  40. typename Gsl::Matrix gEvec=0, gSymmA=0, gSymmB=0;
  41. typename GslTraits<RealScalar>::Vector gEval=0;
  42. RealVectorType _eval;
  43. MatrixType _evec;
  44. convert<MatrixType>(symmA, gSymmA);
  45. convert<MatrixType>(symmB, gSymmB);
  46. convert<MatrixType>(symmA, gEvec);
  47. gEval = GslTraits<RealScalar>::createVector(rows);
  48. Gsl::eigen_symm(gSymmA, gEval, gEvec);
  49. convert(gEval, _eval);
  50. convert(gEvec, _evec);
  51. // test gsl itself !
  52. VERIFY((symmA * _evec).isApprox(_evec * _eval.asDiagonal(), largerEps));
  53. // compare with eigen
  54. VERIFY_IS_APPROX(_eval, eiSymm.eigenvalues());
  55. VERIFY_IS_APPROX(_evec.cwise().abs(), eiSymm.eigenvectors().cwise().abs());
  56. // generalized pb
  57. Gsl::eigen_symm_gen(gSymmA, gSymmB, gEval, gEvec);
  58. convert(gEval, _eval);
  59. convert(gEvec, _evec);
  60. // test GSL itself:
  61. VERIFY((symmA * _evec).isApprox(symmB * (_evec * _eval.asDiagonal()), largerEps));
  62. // compare with eigen
  63. MatrixType normalized_eivec = eiSymmGen.eigenvectors()*eiSymmGen.eigenvectors().colwise().norm().asDiagonal().inverse();
  64. VERIFY_IS_APPROX(_eval, eiSymmGen.eigenvalues());
  65. VERIFY_IS_APPROX(_evec.cwiseAbs(), normalized_eivec.cwiseAbs());
  66. Gsl::free(gSymmA);
  67. Gsl::free(gSymmB);
  68. GslTraits<RealScalar>::free(gEval);
  69. Gsl::free(gEvec);
  70. }
  71. #endif
  72. VERIFY((symmA * eiSymm.eigenvectors()).isApprox(
  73. eiSymm.eigenvectors() * eiSymm.eigenvalues().asDiagonal(), largerEps));
  74. // generalized eigen problem Ax = lBx
  75. VERIFY((symmA * eiSymmGen.eigenvectors()).isApprox(
  76. symmB * (eiSymmGen.eigenvectors() * eiSymmGen.eigenvalues().asDiagonal()), largerEps));
  77. MatrixType sqrtSymmA = eiSymm.operatorSqrt();
  78. VERIFY_IS_APPROX(symmA, sqrtSymmA*sqrtSymmA);
  79. VERIFY_IS_APPROX(sqrtSymmA, symmA*eiSymm.operatorInverseSqrt());
  80. }
  81. template<typename MatrixType> void eigensolver(const MatrixType& m)
  82. {
  83. /* this test covers the following files:
  84. EigenSolver.h
  85. */
  86. int rows = m.rows();
  87. int cols = m.cols();
  88. typedef typename MatrixType::Scalar Scalar;
  89. typedef typename NumTraits<Scalar>::Real RealScalar;
  90. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  91. typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
  92. typedef typename std::complex<typename NumTraits<typename MatrixType::Scalar>::Real> Complex;
  93. // RealScalar largerEps = 10*test_precision<RealScalar>();
  94. MatrixType a = MatrixType::Random(rows,cols);
  95. MatrixType a1 = MatrixType::Random(rows,cols);
  96. MatrixType symmA = a.adjoint() * a + a1.adjoint() * a1;
  97. EigenSolver<MatrixType> ei0(symmA);
  98. VERIFY_IS_APPROX(symmA * ei0.pseudoEigenvectors(), ei0.pseudoEigenvectors() * ei0.pseudoEigenvalueMatrix());
  99. VERIFY_IS_APPROX((symmA.template cast<Complex>()) * (ei0.pseudoEigenvectors().template cast<Complex>()),
  100. (ei0.pseudoEigenvectors().template cast<Complex>()) * (ei0.eigenvalues().asDiagonal()));
  101. EigenSolver<MatrixType> ei1(a);
  102. VERIFY_IS_APPROX(a * ei1.pseudoEigenvectors(), ei1.pseudoEigenvectors() * ei1.pseudoEigenvalueMatrix());
  103. VERIFY_IS_APPROX(a.template cast<Complex>() * ei1.eigenvectors(),
  104. ei1.eigenvectors() * ei1.eigenvalues().asDiagonal());
  105. }
  106. void test_eigen2_eigensolver()
  107. {
  108. for(int i = 0; i < g_repeat; i++) {
  109. // very important to test a 3x3 matrix since we provide a special path for it
  110. CALL_SUBTEST_1( selfadjointeigensolver(Matrix3f()) );
  111. CALL_SUBTEST_2( selfadjointeigensolver(Matrix4d()) );
  112. CALL_SUBTEST_3( selfadjointeigensolver(MatrixXf(7,7)) );
  113. CALL_SUBTEST_4( selfadjointeigensolver(MatrixXcd(5,5)) );
  114. CALL_SUBTEST_5( selfadjointeigensolver(MatrixXd(19,19)) );
  115. CALL_SUBTEST_6( eigensolver(Matrix4f()) );
  116. CALL_SUBTEST_5( eigensolver(MatrixXd(17,17)) );
  117. }
  118. }