Main MRPT website > C++ reference
MRPT logo

MatrixBaseEigenvalues.h

Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_MATRIXBASEEIGENVALUES_H
00027 #define EIGEN_MATRIXBASEEIGENVALUES_H
00028 
00029 namespace internal {
00030 
00031 template<typename Derived, bool IsComplex>
00032 struct eigenvalues_selector
00033 {
00034   // this is the implementation for the case IsComplex = true
00035   static inline typename MatrixBase<Derived>::EigenvaluesReturnType const
00036   run(const MatrixBase<Derived>& m)
00037   {
00038     typedef typename Derived::PlainObject PlainObject;
00039     PlainObject m_eval(m);
00040     return ComplexEigenSolver<PlainObject>(m_eval, false).eigenvalues();
00041   }
00042 };
00043 
00044 template<typename Derived>
00045 struct eigenvalues_selector<Derived, false>
00046 {
00047   static inline typename MatrixBase<Derived>::EigenvaluesReturnType const
00048   run(const MatrixBase<Derived>& m)
00049   {
00050     typedef typename Derived::PlainObject PlainObject;
00051     PlainObject m_eval(m);
00052     return EigenSolver<PlainObject>(m_eval, false).eigenvalues();
00053   }
00054 };
00055 
00056 } // end namespace internal
00057 
00058 /** \brief Computes the eigenvalues of a matrix 
00059   * \returns Column vector containing the eigenvalues.
00060   *
00061   * \eigenvalues_module
00062   * This function computes the eigenvalues with the help of the EigenSolver
00063   * class (for real matrices) or the ComplexEigenSolver class (for complex
00064   * matrices). 
00065   *
00066   * The eigenvalues are repeated according to their algebraic multiplicity,
00067   * so there are as many eigenvalues as rows in the matrix.
00068   *
00069   * The SelfAdjointView class provides a better algorithm for selfadjoint
00070   * matrices.
00071   *
00072   * Example: \include MatrixBase_eigenvalues.cpp
00073   * Output: \verbinclude MatrixBase_eigenvalues.out
00074   *
00075   * \sa EigenSolver::eigenvalues(), ComplexEigenSolver::eigenvalues(),
00076   *     SelfAdjointView::eigenvalues()
00077   */
00078 template<typename Derived>
00079 inline typename MatrixBase<Derived>::EigenvaluesReturnType
00080 MatrixBase<Derived>::eigenvalues() const
00081 {
00082   typedef typename internal::traits<Derived>::Scalar Scalar;
00083   return internal::eigenvalues_selector<Derived, NumTraits<Scalar>::IsComplex>::run(derived());
00084 }
00085 
00086 /** \brief Computes the eigenvalues of a matrix
00087   * \returns Column vector containing the eigenvalues.
00088   *
00089   * \eigenvalues_module
00090   * This function computes the eigenvalues with the help of the
00091   * SelfAdjointEigenSolver class.  The eigenvalues are repeated according to
00092   * their algebraic multiplicity, so there are as many eigenvalues as rows in
00093   * the matrix.
00094   *
00095   * Example: \include SelfAdjointView_eigenvalues.cpp
00096   * Output: \verbinclude SelfAdjointView_eigenvalues.out
00097   *
00098   * \sa SelfAdjointEigenSolver::eigenvalues(), MatrixBase::eigenvalues()
00099   */
00100 template<typename MatrixType, unsigned int UpLo> 
00101 inline typename SelfAdjointView<MatrixType, UpLo>::EigenvaluesReturnType
00102 SelfAdjointView<MatrixType, UpLo>::eigenvalues() const
00103 {
00104   typedef typename SelfAdjointView<MatrixType, UpLo>::PlainObject PlainObject;
00105   PlainObject thisAsMatrix(*this);
00106   return SelfAdjointEigenSolver<PlainObject>(thisAsMatrix, false).eigenvalues();
00107 }
00108 
00109 
00110 
00111 /** \brief Computes the L2 operator norm
00112   * \returns Operator norm of the matrix.
00113   *
00114   * \eigenvalues_module
00115   * This function computes the L2 operator norm of a matrix, which is also
00116   * known as the spectral norm. The norm of a matrix \f$ A \f$ is defined to be
00117   * \f[ \|A\|_2 = \max_x \frac{\|Ax\|_2}{\|x\|_2} \f]
00118   * where the maximum is over all vectors and the norm on the right is the
00119   * Euclidean vector norm. The norm equals the largest singular value, which is
00120   * the square root of the largest eigenvalue of the positive semi-definite
00121   * matrix \f$ A^*A \f$.
00122   *
00123   * The current implementation uses the eigenvalues of \f$ A^*A \f$, as computed
00124   * by SelfAdjointView::eigenvalues(), to compute the operator norm of a
00125   * matrix.  The SelfAdjointView class provides a better algorithm for
00126   * selfadjoint matrices.
00127   *
00128   * Example: \include MatrixBase_operatorNorm.cpp
00129   * Output: \verbinclude MatrixBase_operatorNorm.out
00130   *
00131   * \sa SelfAdjointView::eigenvalues(), SelfAdjointView::operatorNorm()
00132   */
00133 template<typename Derived>
00134 inline typename MatrixBase<Derived>::RealScalar
00135 MatrixBase<Derived>::operatorNorm() const
00136 {
00137   typename Derived::PlainObject m_eval(derived());
00138   // FIXME if it is really guaranteed that the eigenvalues are already sorted,
00139   // then we don't need to compute a maxCoeff() here, comparing the 1st and last ones is enough.
00140   return internal::sqrt((m_eval*m_eval.adjoint())
00141                  .eval()
00142                  .template selfadjointView<Lower>()
00143                  .eigenvalues()
00144                  .maxCoeff()
00145                  );
00146 }
00147 
00148 /** \brief Computes the L2 operator norm
00149   * \returns Operator norm of the matrix.
00150   *
00151   * \eigenvalues_module
00152   * This function computes the L2 operator norm of a self-adjoint matrix. For a
00153   * self-adjoint matrix, the operator norm is the largest eigenvalue.
00154   *
00155   * The current implementation uses the eigenvalues of the matrix, as computed
00156   * by eigenvalues(), to compute the operator norm of the matrix.
00157   *
00158   * Example: \include SelfAdjointView_operatorNorm.cpp
00159   * Output: \verbinclude SelfAdjointView_operatorNorm.out
00160   *
00161   * \sa eigenvalues(), MatrixBase::operatorNorm()
00162   */
00163 template<typename MatrixType, unsigned int UpLo>
00164 inline typename SelfAdjointView<MatrixType, UpLo>::RealScalar
00165 SelfAdjointView<MatrixType, UpLo>::operatorNorm() const
00166 {
00167   return eigenvalues().cwiseAbs().maxCoeff();
00168 }
00169 
00170 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011