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 Eigen { 
00030 
00031 namespace internal {
00032 
00033 template<typename Derived, bool IsComplex>
00034 struct eigenvalues_selector
00035 {
00036   // this is the implementation for the case IsComplex = true
00037   static inline typename MatrixBase<Derived>::EigenvaluesReturnType const
00038   run(const MatrixBase<Derived>& m)
00039   {
00040     typedef typename Derived::PlainObject PlainObject;
00041     PlainObject m_eval(m);
00042     return ComplexEigenSolver<PlainObject>(m_eval, false).eigenvalues();
00043   }
00044 };
00045 
00046 template<typename Derived>
00047 struct eigenvalues_selector<Derived, false>
00048 {
00049   static inline typename MatrixBase<Derived>::EigenvaluesReturnType const
00050   run(const MatrixBase<Derived>& m)
00051   {
00052     typedef typename Derived::PlainObject PlainObject;
00053     PlainObject m_eval(m);
00054     return EigenSolver<PlainObject>(m_eval, false).eigenvalues();
00055   }
00056 };
00057 
00058 } // end namespace internal
00059 
00080 template<typename Derived>
00081 inline typename MatrixBase<Derived>::EigenvaluesReturnType
00082 MatrixBase<Derived>::eigenvalues() const
00083 {
00084   typedef typename internal::traits<Derived>::Scalar Scalar;
00085   return internal::eigenvalues_selector<Derived, NumTraits<Scalar>::IsComplex>::run(derived());
00086 }
00087 
00102 template<typename MatrixType, unsigned int UpLo> 
00103 inline typename SelfAdjointView<MatrixType, UpLo>::EigenvaluesReturnType
00104 SelfAdjointView<MatrixType, UpLo>::eigenvalues() const
00105 {
00106   typedef typename SelfAdjointView<MatrixType, UpLo>::PlainObject PlainObject;
00107   PlainObject thisAsMatrix(*this);
00108   return SelfAdjointEigenSolver<PlainObject>(thisAsMatrix, false).eigenvalues();
00109 }
00110 
00111 
00112 
00135 template<typename Derived>
00136 inline typename MatrixBase<Derived>::RealScalar
00137 MatrixBase<Derived>::operatorNorm() const
00138 {
00139   typename Derived::PlainObject m_eval(derived());
00140   // FIXME if it is really guaranteed that the eigenvalues are already sorted,
00141   // then we don't need to compute a maxCoeff() here, comparing the 1st and last ones is enough.
00142   return internal::sqrt((m_eval*m_eval.adjoint())
00143                  .eval()
00144                  .template selfadjointView<Lower>()
00145                  .eigenvalues()
00146                  .maxCoeff()
00147                  );
00148 }
00149 
00165 template<typename MatrixType, unsigned int UpLo>
00166 inline typename SelfAdjointView<MatrixType, UpLo>::RealScalar
00167 SelfAdjointView<MatrixType, UpLo>::operatorNorm() const
00168 {
00169   return eigenvalues().cwiseAbs().maxCoeff();
00170 }
00171 
00172 } // end namespace Eigen
00173 
00174 #endif