quicsvd.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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
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