MatrixBaseEigenvalues.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #ifndef EIGEN_MATRIXBASEEIGENVALUES_H
  11. #define EIGEN_MATRIXBASEEIGENVALUES_H
  12. namespace Eigen {
  13. namespace internal {
  14. template<typename Derived, bool IsComplex>
  15. struct eigenvalues_selector
  16. {
  17. // this is the implementation for the case IsComplex = true
  18. static inline typename MatrixBase<Derived>::EigenvaluesReturnType const
  19. run(const MatrixBase<Derived>& m)
  20. {
  21. typedef typename Derived::PlainObject PlainObject;
  22. PlainObject m_eval(m);
  23. return ComplexEigenSolver<PlainObject>(m_eval, false).eigenvalues();
  24. }
  25. };
  26. template<typename Derived>
  27. struct eigenvalues_selector<Derived, false>
  28. {
  29. static inline typename MatrixBase<Derived>::EigenvaluesReturnType const
  30. run(const MatrixBase<Derived>& m)
  31. {
  32. typedef typename Derived::PlainObject PlainObject;
  33. PlainObject m_eval(m);
  34. return EigenSolver<PlainObject>(m_eval, false).eigenvalues();
  35. }
  36. };
  37. } // end namespace internal
  38. /** \brief Computes the eigenvalues of a matrix
  39. * \returns Column vector containing the eigenvalues.
  40. *
  41. * \eigenvalues_module
  42. * This function computes the eigenvalues with the help of the EigenSolver
  43. * class (for real matrices) or the ComplexEigenSolver class (for complex
  44. * matrices).
  45. *
  46. * The eigenvalues are repeated according to their algebraic multiplicity,
  47. * so there are as many eigenvalues as rows in the matrix.
  48. *
  49. * The SelfAdjointView class provides a better algorithm for selfadjoint
  50. * matrices.
  51. *
  52. * Example: \include MatrixBase_eigenvalues.cpp
  53. * Output: \verbinclude MatrixBase_eigenvalues.out
  54. *
  55. * \sa EigenSolver::eigenvalues(), ComplexEigenSolver::eigenvalues(),
  56. * SelfAdjointView::eigenvalues()
  57. */
  58. template<typename Derived>
  59. inline typename MatrixBase<Derived>::EigenvaluesReturnType
  60. MatrixBase<Derived>::eigenvalues() const
  61. {
  62. return internal::eigenvalues_selector<Derived, NumTraits<Scalar>::IsComplex>::run(derived());
  63. }
  64. /** \brief Computes the eigenvalues of a matrix
  65. * \returns Column vector containing the eigenvalues.
  66. *
  67. * \eigenvalues_module
  68. * This function computes the eigenvalues with the help of the
  69. * SelfAdjointEigenSolver class. The eigenvalues are repeated according to
  70. * their algebraic multiplicity, so there are as many eigenvalues as rows in
  71. * the matrix.
  72. *
  73. * Example: \include SelfAdjointView_eigenvalues.cpp
  74. * Output: \verbinclude SelfAdjointView_eigenvalues.out
  75. *
  76. * \sa SelfAdjointEigenSolver::eigenvalues(), MatrixBase::eigenvalues()
  77. */
  78. template<typename MatrixType, unsigned int UpLo>
  79. inline typename SelfAdjointView<MatrixType, UpLo>::EigenvaluesReturnType
  80. SelfAdjointView<MatrixType, UpLo>::eigenvalues() const
  81. {
  82. PlainObject thisAsMatrix(*this);
  83. return SelfAdjointEigenSolver<PlainObject>(thisAsMatrix, false).eigenvalues();
  84. }
  85. /** \brief Computes the L2 operator norm
  86. * \returns Operator norm of the matrix.
  87. *
  88. * \eigenvalues_module
  89. * This function computes the L2 operator norm of a matrix, which is also
  90. * known as the spectral norm. The norm of a matrix \f$ A \f$ is defined to be
  91. * \f[ \|A\|_2 = \max_x \frac{\|Ax\|_2}{\|x\|_2} \f]
  92. * where the maximum is over all vectors and the norm on the right is the
  93. * Euclidean vector norm. The norm equals the largest singular value, which is
  94. * the square root of the largest eigenvalue of the positive semi-definite
  95. * matrix \f$ A^*A \f$.
  96. *
  97. * The current implementation uses the eigenvalues of \f$ A^*A \f$, as computed
  98. * by SelfAdjointView::eigenvalues(), to compute the operator norm of a
  99. * matrix. The SelfAdjointView class provides a better algorithm for
  100. * selfadjoint matrices.
  101. *
  102. * Example: \include MatrixBase_operatorNorm.cpp
  103. * Output: \verbinclude MatrixBase_operatorNorm.out
  104. *
  105. * \sa SelfAdjointView::eigenvalues(), SelfAdjointView::operatorNorm()
  106. */
  107. template<typename Derived>
  108. inline typename MatrixBase<Derived>::RealScalar
  109. MatrixBase<Derived>::operatorNorm() const
  110. {
  111. using std::sqrt;
  112. typename Derived::PlainObject m_eval(derived());
  113. // FIXME if it is really guaranteed that the eigenvalues are already sorted,
  114. // then we don't need to compute a maxCoeff() here, comparing the 1st and last ones is enough.
  115. return sqrt((m_eval*m_eval.adjoint())
  116. .eval()
  117. .template selfadjointView<Lower>()
  118. .eigenvalues()
  119. .maxCoeff()
  120. );
  121. }
  122. /** \brief Computes the L2 operator norm
  123. * \returns Operator norm of the matrix.
  124. *
  125. * \eigenvalues_module
  126. * This function computes the L2 operator norm of a self-adjoint matrix. For a
  127. * self-adjoint matrix, the operator norm is the largest eigenvalue.
  128. *
  129. * The current implementation uses the eigenvalues of the matrix, as computed
  130. * by eigenvalues(), to compute the operator norm of the matrix.
  131. *
  132. * Example: \include SelfAdjointView_operatorNorm.cpp
  133. * Output: \verbinclude SelfAdjointView_operatorNorm.out
  134. *
  135. * \sa eigenvalues(), MatrixBase::operatorNorm()
  136. */
  137. template<typename MatrixType, unsigned int UpLo>
  138. inline typename SelfAdjointView<MatrixType, UpLo>::RealScalar
  139. SelfAdjointView<MatrixType, UpLo>::operatorNorm() const
  140. {
  141. return eigenvalues().cwiseAbs().maxCoeff();
  142. }
  143. } // end namespace Eigen
  144. #endif