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.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 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> eiNoEivecs(a, false);
  42. VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
  43. VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
  44. VERIFY_IS_APPROX(ei1.pseudoEigenvalueMatrix(), eiNoEivecs.pseudoEigenvalueMatrix());
  45. MatrixType id = MatrixType::Identity(rows, cols);
  46. VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
  47. if (rows > 2)
  48. {
  49. // Test matrix with NaN
  50. a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
  51. EigenSolver<MatrixType> eiNaN(a);
  52. VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
  53. }
  54. }
  55. template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
  56. {
  57. EigenSolver<MatrixType> eig;
  58. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  59. VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
  60. VERIFY_RAISES_ASSERT(eig.pseudoEigenvalueMatrix());
  61. VERIFY_RAISES_ASSERT(eig.eigenvalues());
  62. MatrixType a = MatrixType::Random(m.rows(),m.cols());
  63. eig.compute(a, false);
  64. VERIFY_RAISES_ASSERT(eig.eigenvectors());
  65. VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
  66. }
  67. void test_eigensolver_generic()
  68. {
  69. int s;
  70. for(int i = 0; i < g_repeat; i++) {
  71. CALL_SUBTEST_1( eigensolver(Matrix4f()) );
  72. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  73. CALL_SUBTEST_2( eigensolver(MatrixXd(s,s)) );
  74. // some trivial but implementation-wise tricky cases
  75. CALL_SUBTEST_2( eigensolver(MatrixXd(1,1)) );
  76. CALL_SUBTEST_2( eigensolver(MatrixXd(2,2)) );
  77. CALL_SUBTEST_3( eigensolver(Matrix<double,1,1>()) );
  78. CALL_SUBTEST_4( eigensolver(Matrix2d()) );
  79. }
  80. CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4f()) );
  81. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  82. CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXd(s,s)) );
  83. CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<double,1,1>()) );
  84. CALL_SUBTEST_4( eigensolver_verify_assert(Matrix2d()) );
  85. // Test problem size constructors
  86. CALL_SUBTEST_5(EigenSolver<MatrixXf>(s));
  87. // regression test for bug 410
  88. CALL_SUBTEST_2(
  89. {
  90. MatrixXd A(1,1);
  91. A(0,0) = std::sqrt(-1.);
  92. Eigen::EigenSolver<MatrixXd> solver(A);
  93. MatrixXd V(1, 1);
  94. V(0,0) = solver.eigenvectors()(0,0).real();
  95. }
  96. );
  97. EIGEN_UNUSED_VARIABLE(s)
  98. }