cmtkQRDecomposition.txx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2676 $
00026 //
00027 //  $LastChangedDate: 2010-12-15 14:50:13 -0800 (Wed, 15 Dec 2010) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "Numerics/sevd.h"
00034 #include "Numerics/qr.h"
00035 
00036 namespace
00037 cmtk
00038 {
00039 
00042 
00043 template<class TFloat>
00044 QRDecomposition<TFloat>
00045 ::QRDecomposition( const Matrix2D<TFloat>& matrix )
00046 {
00047   m = matrix.GetNumberOfRows();
00048   n = matrix.GetNumberOfColumns();
00049 
00050   /* Copy matrix into compactQR
00051    */
00052 
00053   compactQR.setbounds(0, (int)matrix.GetNumberOfRows(), 0, (int)matrix.GetNumberOfColumns());
00054   for ( int j = 0; j < m; j++ )
00055     for ( int i = 0; i < n; i++ )
00056       compactQR(i,j) = (double)(1.0 * matrix[i][j]);
00057   
00058   /* Run AlgLib QR decomposition
00059    */
00060   rmatrixqr( compactQR, m, n, tau );
00061 
00062   /* Initialized Q and R objects
00063    */
00064   Q = matrixPtr ( new matrix2D( m, n ) );
00065   R = matrixPtr ( new matrix2D( m, n ) );
00066 
00067   /* Set these to true once the Q or R 
00068    * matrix has been extracted from the compact
00069    * alglib representation
00070    */ 
00071   extractedQ = extractedR = false;
00072 
00073 }
00074 
00076 template<class TFloat>
00077 Matrix2D<TFloat>&
00078 QRDecomposition<TFloat>
00079 ::GetQ() 
00080 {
00081   if ( ! extractedQ ) 
00082     {
00083     /* Extract Q from compactQR
00084       */
00085     ap::real_2d_array tmp_ap_matrix;
00086     rmatrixqrunpackq( compactQR, m, n, tau, n, tmp_ap_matrix );
00087 
00088     for ( int j = 0; j < m; j++ )
00089       for ( int i = 0; i < n; i++ )
00090         (*Q)[i][j] = tmp_ap_matrix(i,j);
00091 
00092     extractedQ = true;
00093     }
00094   return *(this->Q);
00095 }
00096 
00098 template<class TFloat>
00099 Matrix2D<TFloat>&
00100 QRDecomposition<TFloat>
00101 ::GetR()
00102 {
00103   if ( ! extractedR ) 
00104     {
00105     /* Extract R from compactQR
00106       */ 
00107     ap::real_2d_array tmp_ap_matrix;
00108     rmatrixqrunpackr( compactQR, m, n, tmp_ap_matrix );
00109     for ( int j = 0; j < m; j++ )
00110       for ( int i = 0; i < n; i++ )
00111         (*R)[i][j] = tmp_ap_matrix(i,j);
00112 
00113     extractedR = true;
00114     }
00115 
00116   return *(this->R);
00117 }
00118 
00119 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines