cosine_tree.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_COSINE_TREE_H
00046 #define QUICSVD_COSINE_TREE_H
00047 #include <fastlib/fastlib.h>
00048 
00049 class CosineNode {
00051   Matrix A_;
00052 
00054   ArrayList<int> origIndices_;
00055 
00057   ArrayList<double> norms_;
00058 
00060   ArrayList<double> cum_norms_;
00061 
00063   Vector mean_;
00064 
00066   bool isLeft_;
00067 
00072   double L2Err_;
00073 
00075   CosineNode *parent_, *left_, *right_;
00076 
00077  public:
00079   CosineNode(const Matrix& A);
00080 
00082   CosineNode(CosineNode& parent, const ArrayList<int>& indices,
00083              bool isLeft);
00084 
00088   void GetColumn(int i_col, Vector* col) {
00089     A_.MakeColumnVector(origIndices_[i_col], col);
00090   }
00091 
00093   index_t n_cols() const {
00094     return origIndices_.size();
00095   }
00096 
00102   void Split();
00103   
00105   double getSumL2() const {
00106     return cum_norms_[n_cols()-1];
00107   }
00108 
00110   const Vector& getMean() const {
00111     return mean_;
00112   }
00113 
00115   index_t getOrigIndex(index_t i_col) const {
00116     return origIndices_[i_col];
00117   }
00118 
00120   void setL2Err(double L2Err) {
00121     L2Err_ = L2Err;
00122   }
00123 
00125   bool hasLeft() const {
00126     return left_ != NULL;
00127   }
00128 
00130   bool hasRight() const {
00131     return right_ != NULL;
00132   }
00133 
00135   CosineNode* getLeft() {
00136     return left_;
00137   }
00138 
00140   CosineNode* getRight() {
00141     return right_;
00142   }
00143 
00144  private:
00145   // Helper functions
00146 
00148   void CalStats();
00149 
00153   void ChooseCenter(Vector* center);
00154 
00156   void CalCosines(const Vector& center, ArrayList<double>* cosines);
00157 
00159   void CreateIndices(ArrayList<int>* indices);
00160 
00162   friend class CosineNodeTest;
00163 
00165   friend class CompareCosineNode;
00166 };
00167 
00168 class CompareCosineNode {
00169  public:
00170   bool operator ()(CosineNode* a, CosineNode* b) {
00171     return a->L2Err_ < b->L2Err_;
00172   }
00173 };
00174 
00175 class CosineNodeTest {
00176   FILE * logfile;
00177 
00178   void test_CosineTreeNode() {
00179     Matrix A;
00180     data::Load("input.txt", &A);
00181     CosineNode root(A);
00182     //ot::Print(root, "cosine root", logfile);
00183 
00184     root.Split();
00185 
00186     //ot::Print(*root.left_, "left node", logfile);
00187     //ot::Print(*root.right_, "right node", logfile);
00188   }
00189 
00190 public:
00191   CosineNodeTest() {
00192     logfile = fopen("LOG", "w");
00193   }
00194   ~CosineNodeTest() {
00195     fclose(logfile);
00196   }
00197 
00198   void run_tests() {
00199     test_CosineTreeNode();
00200   }
00201 };
00202 
00203 
00204 #endif
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3