MatrixBaseEigenvalues.h revision 7faaa9f3f0df9d23790277834d426c3d992ac3ba
1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef EIGEN_MATRIXBASEEIGENVALUES_H
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_MATRIXBASEEIGENVALUES_H
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace Eigen {
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace internal {
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived, bool IsComplex>
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct eigenvalues_selector
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // this is the implementation for the case IsComplex = true
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline typename MatrixBase<Derived>::EigenvaluesReturnType const
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  run(const MatrixBase<Derived>& m)
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename Derived::PlainObject PlainObject;
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    PlainObject m_eval(m);
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return ComplexEigenSolver<PlainObject>(m_eval, false).eigenvalues();
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct eigenvalues_selector<Derived, false>
33c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline typename MatrixBase<Derived>::EigenvaluesReturnType const
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  run(const MatrixBase<Derived>& m)
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename Derived::PlainObject PlainObject;
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    PlainObject m_eval(m);
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    return EigenSolver<PlainObject>(m_eval, false).eigenvalues();
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace internal
44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** \brief Computes the eigenvalues of a matrix
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \returns Column vector containing the eigenvalues.
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \eigenvalues_module
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * This function computes the eigenvalues with the help of the EigenSolver
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * class (for real matrices) or the ComplexEigenSolver class (for complex
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * matrices).
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * The eigenvalues are repeated according to their algebraic multiplicity,
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * so there are as many eigenvalues as rows in the matrix.
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * The SelfAdjointView class provides a better algorithm for selfadjoint
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * matrices.
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * Example: \include MatrixBase_eigenvalues.cpp
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * Output: \verbinclude MatrixBase_eigenvalues.out
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \sa EigenSolver::eigenvalues(), ComplexEigenSolver::eigenvalues(),
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *     SelfAdjointView::eigenvalues()
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline typename MatrixBase<Derived>::EigenvaluesReturnType
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathMatrixBase<Derived>::eigenvalues() const
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename internal::traits<Derived>::Scalar Scalar;
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return internal::eigenvalues_selector<Derived, NumTraits<Scalar>::IsComplex>::run(derived());
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** \brief Computes the eigenvalues of a matrix
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \returns Column vector containing the eigenvalues.
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \eigenvalues_module
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * This function computes the eigenvalues with the help of the
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * SelfAdjointEigenSolver class.  The eigenvalues are repeated according to
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * their algebraic multiplicity, so there are as many eigenvalues as rows in
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * the matrix.
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * Example: \include SelfAdjointView_eigenvalues.cpp
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * Output: \verbinclude SelfAdjointView_eigenvalues.out
84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \sa SelfAdjointEigenSolver::eigenvalues(), MatrixBase::eigenvalues()
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType, unsigned int UpLo>
88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline typename SelfAdjointView<MatrixType, UpLo>::EigenvaluesReturnType
89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathSelfAdjointView<MatrixType, UpLo>::eigenvalues() const
90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename SelfAdjointView<MatrixType, UpLo>::PlainObject PlainObject;
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  PlainObject thisAsMatrix(*this);
93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return SelfAdjointEigenSolver<PlainObject>(thisAsMatrix, false).eigenvalues();
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** \brief Computes the L2 operator norm
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \returns Operator norm of the matrix.
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \eigenvalues_module
102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * This function computes the L2 operator norm of a matrix, which is also
103c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * known as the spectral norm. The norm of a matrix \f$ A \f$ is defined to be
104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \f[ \|A\|_2 = \max_x \frac{\|Ax\|_2}{\|x\|_2} \f]
105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * where the maximum is over all vectors and the norm on the right is the
106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * Euclidean vector norm. The norm equals the largest singular value, which is
107c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * the square root of the largest eigenvalue of the positive semi-definite
108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * matrix \f$ A^*A \f$.
109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
110c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * The current implementation uses the eigenvalues of \f$ A^*A \f$, as computed
111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * by SelfAdjointView::eigenvalues(), to compute the operator norm of a
112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * matrix.  The SelfAdjointView class provides a better algorithm for
113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * selfadjoint matrices.
114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
115c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * Example: \include MatrixBase_operatorNorm.cpp
116c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * Output: \verbinclude MatrixBase_operatorNorm.out
117c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
118c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \sa SelfAdjointView::eigenvalues(), SelfAdjointView::operatorNorm()
119c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
120c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
121c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline typename MatrixBase<Derived>::RealScalar
122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathMatrixBase<Derived>::operatorNorm() const
123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
1247faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  using std::sqrt;
125c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typename Derived::PlainObject m_eval(derived());
126c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // FIXME if it is really guaranteed that the eigenvalues are already sorted,
127c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  // then we don't need to compute a maxCoeff() here, comparing the 1st and last ones is enough.
1287faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  return sqrt((m_eval*m_eval.adjoint())
129c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                 .eval()
130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath		 .template selfadjointView<Lower>()
131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath		 .eigenvalues()
132c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath		 .maxCoeff()
133c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath		 );
134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
135c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
136c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** \brief Computes the L2 operator norm
137c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \returns Operator norm of the matrix.
138c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
139c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \eigenvalues_module
140c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * This function computes the L2 operator norm of a self-adjoint matrix. For a
141c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * self-adjoint matrix, the operator norm is the largest eigenvalue.
142c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
143c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * The current implementation uses the eigenvalues of the matrix, as computed
144c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * by eigenvalues(), to compute the operator norm of the matrix.
145c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
146c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * Example: \include SelfAdjointView_operatorNorm.cpp
147c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * Output: \verbinclude SelfAdjointView_operatorNorm.out
148c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
149c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \sa eigenvalues(), MatrixBase::operatorNorm()
150c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
151c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixType, unsigned int UpLo>
152c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline typename SelfAdjointView<MatrixType, UpLo>::RealScalar
153c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathSelfAdjointView<MatrixType, UpLo>::operatorNorm() const
154c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return eigenvalues().cwiseAbs().maxCoeff();
156c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
157c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
158c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace Eigen
159c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
160c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif
161