00001 // This is core/vnl/algo/vnl_symmetric_eigensystem.h 00002 #ifndef vnl_symmetric_eigensystem_h_ 00003 #define vnl_symmetric_eigensystem_h_ 00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE 00005 #pragma interface 00006 #endif 00007 //: 00008 // \file 00009 // \brief Find eigenvalues of a symmetric matrix 00010 // 00011 // vnl_symmetric_eigensystem_compute() 00012 // solves the eigenproblem $A x = \lambda x$, with $A$ symmetric. 00013 // The resulting eigenvectors and values are sorted in increasing order 00014 // so <CODE> V.column(0) </CODE> is the eigenvector corresponding to the 00015 // smallest eigenvalue. 00016 // 00017 // As a matrix decomposition, this is $A = V D V^t$ 00018 // 00019 // Uses the EISPACK routine RS, which in turn calls TRED2 to reduce A 00020 // to tridiagonal form, followed by TQL2, to find the eigensystem. 00021 // This is summarized in Golub and van Loan, \S8.2. The following are 00022 // the original subroutine headers: 00023 // 00024 // \remark TRED2 is a translation of the Algol procedure tred2, 00025 // Num. Math. 11, 181-195(1968) by Martin, Reinsch, and Wilkinson. 00026 // Handbook for Auto. Comp., Vol.ii-Linear Algebra, 212-226(1971). 00027 // 00028 // \remark This subroutine reduces a real symmetric matrix to a 00029 // symmetric tridiagonal matrix using and accumulating 00030 // orthogonal similarity transformations. 00031 // 00032 // \remark TQL2 is a translation of the Algol procedure tql2, 00033 // Num. Math. 11, 293-306(1968) by Bowdler, Martin, Reinsch, and Wilkinson. 00034 // Handbook for Auto. Comp., Vol.ii-Linear Algebra, 227-240(1971). 00035 // 00036 // \remark This subroutine finds the eigenvalues and eigenvectors 00037 // of a symmetric tridiagonal matrix by the QL method. 00038 // the eigenvectors of a full symmetric matrix can also 00039 // be found if tred2 has been used to reduce this 00040 // full matrix to tridiagonal form. 00041 // 00042 // \author Andrew W. Fitzgibbon, Oxford RRG 00043 // \date 29 Aug 96 00044 // 00045 // \verbatim 00046 // Modifications 00047 // fsm, 5 March 2000: templated 00048 // dac (Manchester) 28/03/2001: tidied up documentation 00049 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line 00050 // Jan.2003 - Peter Vanroose - added missing implementation for solve(b,x) 00051 // \endverbatim 00052 00053 #include <vnl/vnl_matrix.h> 00054 #include <vnl/vnl_diag_matrix.h> 00055 00056 //: Find eigenvalues of a symmetric 3x3 matrix 00057 // Eigenvalues will be returned so that l1 <= l2 <= l3. 00058 // \verbatim 00059 // Matrix is M11 M12 M13 00060 // M12 M22 M23 00061 // M13 M23 M33 00062 // \endverbatim 00063 void vnl_symmetric_eigensystem_compute_eigenvals( 00064 double M11, double M12, double M13, 00065 double M22, double M23, 00066 double M33, 00067 double &l1, double &l2, double &l3); 00068 00069 //: Find eigenvalues of a symmetric matrix 00070 bool vnl_symmetric_eigensystem_compute(vnl_matrix<float> const & A, 00071 vnl_matrix<float> & V, 00072 vnl_vector<float> & D); 00073 00074 //: Find eigenvalues of a symmetric matrix 00075 00076 bool vnl_symmetric_eigensystem_compute(vnl_matrix<double> const & A, 00077 vnl_matrix<double> & V, 00078 vnl_vector<double> & D); 00079 00080 //: Computes and stores the eigensystem decomposition of a symmetric matrix. 00081 00082 export template <class T> 00083 class vnl_symmetric_eigensystem 00084 { 00085 public: 00086 //: Solve real symmetric eigensystem $A x = \lambda x$ 00087 vnl_symmetric_eigensystem(vnl_matrix<T> const & M); 00088 00089 protected: 00090 // need this here to get inits in correct order, but still keep gentex 00091 // in the right order. 00092 int n_; 00093 00094 public: 00095 //: Public eigenvectors. 00096 // After construction, the columns of V are the eigenvectors, sorted by 00097 // increasing eigenvalue, from most negative to most positive. 00098 vnl_matrix<T> V; 00099 00100 //: Public eigenvalues. 00101 // After construction, D contains the eigenvalues, sorted as described above. 00102 // Note that D is a vnl_diag_matrix, and is therefore stored as a vcl_vector while behaving as a matrix. 00103 vnl_diag_matrix<T> D; 00104 00105 //: Recover specified eigenvector after computation. 00106 vnl_vector<T> get_eigenvector(int i) const; 00107 00108 //: Recover specified eigenvalue after computation. 00109 T get_eigenvalue(int i) const; 00110 00111 //: Convenience method to get least-squares nullvector. 00112 // It is deliberate that the signature is the same as on vnl_svd<T>. 00113 vnl_vector<T> nullvector() const { return get_eigenvector(0); } 00114 00115 //: Return the matrix $V D V^\top$. 00116 // This can be useful if you've modified $D$. So an inverse is obtained using 00117 // \code 00118 // vnl_symmetric_eigensystem} eig(A); 00119 // eig.D.invert_in_place}(); 00120 // vnl_matrix<double> Ainverse = eig.recompose(); 00121 // \endcode 00122 00123 vnl_matrix<T> recompose() const { return V * D * V.transpose(); } 00124 00125 //: return product of eigenvalues. 00126 T determinant() const; 00127 00128 //: return the pseudoinverse. 00129 vnl_matrix<T> pinverse() const; 00130 00131 //: return the square root, if positive semi-definite. 00132 vnl_matrix<T> square_root() const; 00133 00134 //: return the inverse of the square root, if positive semi-definite. 00135 vnl_matrix<T> inverse_square_root() const; 00136 00137 //: Solve LS problem M x = b 00138 vnl_vector<T> solve(vnl_vector<T> const & b); 00139 00140 //: Solve LS problem M x = b 00141 void solve(vnl_vector<T> const & b, vnl_vector<T> * x) { *x = solve(b); } 00142 }; 00143 00144 #endif // vnl_symmetric_eigensystem_h_
1.5.1