quicsvd.h

Go to the documentation of this file.
00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00045 #ifndef QUICSVD_QUICSVD_H
00046 #define QUICSVD_QUICSVD_H
00047 #include <fastlib/fastlib.h>
00048 #include "cosine_tree.h"
00049 #include <vector>
00050 #include <queue>
00051 
00052 class QuicSVD {
00053 
00054   // An alias for the priority queue of CosineNode
00055   typedef std::priority_queue<CosineNode*, std::vector<CosineNode*>,
00056     CompareCosineNode> CosineNodeQueue;
00057 
00059   Matrix A_;
00060 
00062   double dataNorm2_;
00063 
00065   CosineNode root_;
00066 
00071   CosineNodeQueue leaves_;
00072 
00073 
00075   ArrayList<Vector> basis_;
00076 
00078   ArrayList<Vector> UTA_;
00079   
00081   ArrayList<double> projMagSq_;
00082 
00084   double sumProjMagSq_;
00085 
00087   double targetRelErr_;
00088 
00089  public:
00091   QuicSVD(const Matrix& A, double targetRelErr);
00092   
00097   index_t n_cols() {
00098     return A_.n_cols();
00099   }
00100 
00105   void ComputeSVD(Vector* s, Matrix* U, Matrix* VT);
00106 
00113   static double SVDInit(const Matrix& A, double targetRelErr,
00114                       Vector* s, Matrix* U, Matrix* VT);
00115 
00116  private:
00117   // Helper private functions
00118 
00123   void addBasisFrom(const CosineNode& node);
00124 
00126   double curRelErr();
00127 
00129   void addToQueue(CosineNode* node);
00130 
00134   double calL2Err(const CosineNode& node);
00135 
00138   void extractSVD(Vector* s, Matrix* U, Matrix* VT);
00139 
00141   friend class QuicSVDTest;
00142 };
00143 
00144 class QuicSVDTest {
00145   FILE * logfile;
00146 
00147   void test_QuicSVD() {
00148     Matrix A;
00149     printf("Load data from input1.txt.\n");
00150     data::Load("input1.txt", &A);
00151     QuicSVD svd(A, 0.1);
00152     Vector s;
00153     Matrix U, VT, S;
00154     svd.ComputeSVD(&s, &U, &VT);
00155     data::Save("U.txt", U);
00156     S.InitDiagonal(s);
00157     data::Save("S.txt", S);
00158     data::Save("VT.txt", VT);
00159     printf("U,s,VT are saved to U.txt, S.txt, VT.txt, respectively.\n");
00160   }
00161 public:
00162   QuicSVDTest() {
00163     logfile = fopen("LOG", "w");
00164   }
00165   ~QuicSVDTest() {
00166     fclose(logfile);
00167   }
00168 
00169   void run_tests() {
00170     test_QuicSVD();
00171   }
00172 
00173 };
00174 
00175 #endif
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3