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
6.5 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 selfadjointeigensolver(const MatrixType& m)
  14. {
  15. typedef typename MatrixType::Index Index;
  16. /* this test covers the following files:
  17. EigenSolver.h, SelfAdjointEigenSolver.h (and indirectly: Tridiagonalization.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. 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. symmA.template triangularView<StrictlyUpper>().setZero();
  31. MatrixType b = MatrixType::Random(rows,cols);
  32. MatrixType b1 = MatrixType::Random(rows,cols);
  33. MatrixType symmB = b.adjoint() * b + b1.adjoint() * b1;
  34. symmB.template triangularView<StrictlyUpper>().setZero();
  35. SelfAdjointEigenSolver<MatrixType> eiSymm(symmA);
  36. SelfAdjointEigenSolver<MatrixType> eiDirect;
  37. eiDirect.computeDirect(symmA);
  38. // generalized eigen pb
  39. GeneralizedSelfAdjointEigenSolver<MatrixType> eiSymmGen(symmA, symmB);
  40. VERIFY_IS_EQUAL(eiSymm.info(), Success);
  41. VERIFY((symmA.template selfadjointView<Lower>() * eiSymm.eigenvectors()).isApprox(
  42. eiSymm.eigenvectors() * eiSymm.eigenvalues().asDiagonal(), largerEps));
  43. VERIFY_IS_APPROX(symmA.template selfadjointView<Lower>().eigenvalues(), eiSymm.eigenvalues());
  44. VERIFY_IS_EQUAL(eiDirect.info(), Success);
  45. VERIFY((symmA.template selfadjointView<Lower>() * eiDirect.eigenvectors()).isApprox(
  46. eiDirect.eigenvectors() * eiDirect.eigenvalues().asDiagonal(), largerEps));
  47. VERIFY_IS_APPROX(symmA.template selfadjointView<Lower>().eigenvalues(), eiDirect.eigenvalues());
  48. SelfAdjointEigenSolver<MatrixType> eiSymmNoEivecs(symmA, false);
  49. VERIFY_IS_EQUAL(eiSymmNoEivecs.info(), Success);
  50. VERIFY_IS_APPROX(eiSymm.eigenvalues(), eiSymmNoEivecs.eigenvalues());
  51. // generalized eigen problem Ax = lBx
  52. eiSymmGen.compute(symmA, symmB,Ax_lBx);
  53. VERIFY_IS_EQUAL(eiSymmGen.info(), Success);
  54. VERIFY((symmA.template selfadjointView<Lower>() * eiSymmGen.eigenvectors()).isApprox(
  55. symmB.template selfadjointView<Lower>() * (eiSymmGen.eigenvectors() * eiSymmGen.eigenvalues().asDiagonal()), largerEps));
  56. // generalized eigen problem BAx = lx
  57. eiSymmGen.compute(symmA, symmB,BAx_lx);
  58. VERIFY_IS_EQUAL(eiSymmGen.info(), Success);
  59. VERIFY((symmB.template selfadjointView<Lower>() * (symmA.template selfadjointView<Lower>() * eiSymmGen.eigenvectors())).isApprox(
  60. (eiSymmGen.eigenvectors() * eiSymmGen.eigenvalues().asDiagonal()), largerEps));
  61. // generalized eigen problem ABx = lx
  62. eiSymmGen.compute(symmA, symmB,ABx_lx);
  63. VERIFY_IS_EQUAL(eiSymmGen.info(), Success);
  64. VERIFY((symmA.template selfadjointView<Lower>() * (symmB.template selfadjointView<Lower>() * eiSymmGen.eigenvectors())).isApprox(
  65. (eiSymmGen.eigenvectors() * eiSymmGen.eigenvalues().asDiagonal()), largerEps));
  66. MatrixType sqrtSymmA = eiSymm.operatorSqrt();
  67. VERIFY_IS_APPROX(MatrixType(symmA.template selfadjointView<Lower>()), sqrtSymmA*sqrtSymmA);
  68. VERIFY_IS_APPROX(sqrtSymmA, symmA.template selfadjointView<Lower>()*eiSymm.operatorInverseSqrt());
  69. MatrixType id = MatrixType::Identity(rows, cols);
  70. VERIFY_IS_APPROX(id.template selfadjointView<Lower>().operatorNorm(), RealScalar(1));
  71. SelfAdjointEigenSolver<MatrixType> eiSymmUninitialized;
  72. VERIFY_RAISES_ASSERT(eiSymmUninitialized.info());
  73. VERIFY_RAISES_ASSERT(eiSymmUninitialized.eigenvalues());
  74. VERIFY_RAISES_ASSERT(eiSymmUninitialized.eigenvectors());
  75. VERIFY_RAISES_ASSERT(eiSymmUninitialized.operatorSqrt());
  76. VERIFY_RAISES_ASSERT(eiSymmUninitialized.operatorInverseSqrt());
  77. eiSymmUninitialized.compute(symmA, false);
  78. VERIFY_RAISES_ASSERT(eiSymmUninitialized.eigenvectors());
  79. VERIFY_RAISES_ASSERT(eiSymmUninitialized.operatorSqrt());
  80. VERIFY_RAISES_ASSERT(eiSymmUninitialized.operatorInverseSqrt());
  81. // test Tridiagonalization's methods
  82. Tridiagonalization<MatrixType> tridiag(symmA);
  83. // FIXME tridiag.matrixQ().adjoint() does not work
  84. VERIFY_IS_APPROX(MatrixType(symmA.template selfadjointView<Lower>()), tridiag.matrixQ() * tridiag.matrixT().eval() * MatrixType(tridiag.matrixQ()).adjoint());
  85. if (rows > 1)
  86. {
  87. // Test matrix with NaN
  88. symmA(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
  89. SelfAdjointEigenSolver<MatrixType> eiSymmNaN(symmA);
  90. VERIFY_IS_EQUAL(eiSymmNaN.info(), NoConvergence);
  91. }
  92. }
  93. void test_eigensolver_selfadjoint()
  94. {
  95. int s;
  96. for(int i = 0; i < g_repeat; i++) {
  97. // very important to test 3x3 and 2x2 matrices since we provide special paths for them
  98. CALL_SUBTEST_1( selfadjointeigensolver(Matrix2d()) );
  99. CALL_SUBTEST_1( selfadjointeigensolver(Matrix3f()) );
  100. CALL_SUBTEST_2( selfadjointeigensolver(Matrix4d()) );
  101. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  102. CALL_SUBTEST_3( selfadjointeigensolver(MatrixXf(s,s)) );
  103. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  104. CALL_SUBTEST_4( selfadjointeigensolver(MatrixXd(s,s)) );
  105. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  106. CALL_SUBTEST_5( selfadjointeigensolver(MatrixXcd(s,s)) );
  107. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  108. CALL_SUBTEST_9( selfadjointeigensolver(Matrix<std::complex<double>,Dynamic,Dynamic,RowMajor>(s,s)) );
  109. // some trivial but implementation-wise tricky cases
  110. CALL_SUBTEST_4( selfadjointeigensolver(MatrixXd(1,1)) );
  111. CALL_SUBTEST_4( selfadjointeigensolver(MatrixXd(2,2)) );
  112. CALL_SUBTEST_6( selfadjointeigensolver(Matrix<double,1,1>()) );
  113. CALL_SUBTEST_7( selfadjointeigensolver(Matrix<double,2,2>()) );
  114. }
  115. // Test problem size constructors
  116. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  117. CALL_SUBTEST_8(SelfAdjointEigenSolver<MatrixXf>(s));
  118. CALL_SUBTEST_8(Tridiagonalization<MatrixXf>(s));
  119. EIGEN_UNUSED_VARIABLE(s)
  120. }