ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkSymmetricEigenAnalysis.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkSymmetricEigenAnalysis_h
00019 #define __itkSymmetricEigenAnalysis_h
00020 
00021 #include "itkMacro.h"
00022 
00023 namespace itk
00024 {
00060 template< typename TMatrix, typename TVector, typename TEigenMatrix = TMatrix >
00061 class SymmetricEigenAnalysis
00062 {
00063 public:
00064   typedef enum
00065   {
00066     OrderByValue = 1,
00067     OrderByMagnitude,
00068     DoNotOrder
00069   }
00070   EigenValueOrderType;
00071 
00072   SymmetricEigenAnalysis():
00073   m_Dimension(0),
00074   m_Order(0),
00075   m_OrderEigenValues(OrderByValue) {}
00076 
00077   SymmetricEigenAnalysis(const unsigned int dimension):
00078   m_Dimension(dimension),
00079   m_Order(dimension),
00080   m_OrderEigenValues(OrderByValue) {}
00081 
00082   ~SymmetricEigenAnalysis() {}
00083 
00084   typedef TMatrix      MatrixType;
00085   typedef TEigenMatrix EigenMatrixType;
00086   typedef TVector      VectorType;
00087 
00101   unsigned int ComputeEigenValues(
00102     const TMatrix  & A,
00103     TVector        & EigenValues) const;
00104 
00125   unsigned int ComputeEigenValuesAndVectors(
00126     const TMatrix  & A,
00127     TVector        & EigenValues,
00128     TEigenMatrix   & EigenVectors) const;
00129 
00131   void SetOrder(const unsigned int n)
00132   {
00133     m_Order = n;
00134   }
00135 
00139   unsigned int GetOrder() const { return m_Order; }
00140 
00144   void SetOrderEigenValues(const bool b)
00145   {
00146     if ( b ) { m_OrderEigenValues = OrderByValue;     }
00147     else   { m_OrderEigenValues = DoNotOrder;       }
00148   }
00150 
00151   bool GetOrderEigenValues() const { return ( m_OrderEigenValues == OrderByValue ); }
00152 
00156   void SetOrderEigenMagnitudes(const bool b)
00157   {
00158     if ( b ) { m_OrderEigenValues = OrderByMagnitude; }
00159     else   { m_OrderEigenValues = DoNotOrder;       }
00160   }
00162 
00163   bool GetOrderEigenMagnitudes() const { return ( m_OrderEigenValues == OrderByMagnitude ); }
00164 
00167   void SetDimension(const unsigned int n)
00168   {
00169     m_Dimension = n;
00170     if ( m_Order == 0 )
00171       {
00172       m_Order = m_Dimension;
00173       }
00174   }
00176 
00179   unsigned int GetDimension() const { return m_Dimension; }
00180 private:
00181   unsigned int        m_Dimension;
00182   unsigned int        m_Order;
00183   EigenValueOrderType m_OrderEigenValues;
00184 
00204   void ReduceToTridiagonalMatrix(double *inputMatrix, VectorType & d,
00205                                  double *e, double *e2) const;
00206 
00228   void ReduceToTridiagonalMatrixAndGetTransformation(
00229     double *inputMatrix, VectorType & diagonalElements,
00230     double *subDiagonalElements, double *transformMatrix) const;
00231 
00261   unsigned int ComputeEigenValuesUsingQL(
00262     VectorType & d, double *e) const;
00263 
00301   unsigned int ComputeEigenValuesAndVectorsUsingQL(
00302     VectorType & d, double *e, double *z) const;
00303 };
00304 
00305 template< typename TMatrix, typename TVector, typename TEigenMatrix >
00306 std::ostream & operator<<(std::ostream & os,
00307                           const SymmetricEigenAnalysis< TMatrix, TVector, TEigenMatrix > & s)
00308 {
00309   os << "[ClassType: SymmetricEigenAnalysis]" << std::endl;
00310   os << "  Dimension : " << s.GetDimension() << std::endl;
00311   os << "  Order : " << s.GetOrder() << std::endl;
00312   os << "  OrderEigenValues: " << s.GetOrderEigenValues() << std::endl;
00313   os << "  OrderEigenMagnitudes: " << s.GetOrderEigenMagnitudes() << std::endl;
00314   return os;
00315 }
00316 } // end namespace itk
00317 
00318 #ifndef ITK_MANUAL_INSTANTIATION
00319 #include "itkSymmetricEigenAnalysis.hxx"
00320 #endif
00321 
00322 #endif
00323