Changeset 616 for trunk/c++_tools/utility
- Timestamp:
- Aug 31, 2006, 10:52:02 AM (16 years ago)
- Location:
- trunk/c++_tools/utility
- Files:
-
- 15 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/c++_tools/utility/Alignment.cc
r301 r616 2 2 3 3 #include <c++_tools/utility/Alignment.h> 4 #include <c++_tools/ gslapi/matrix.h>4 #include <c++_tools/utility/matrix.h> 5 5 6 6 #include <utility> … … 11 11 namespace alignment { 12 12 13 double NeedlemanWunsch(const gslapi::matrix& s,13 double NeedlemanWunsch(const utility::matrix& s, 14 14 std::vector<std::pair<size_t, size_t> >& path, 15 15 const double gap) 16 16 { 17 gslapi::matrix m(s.rows()+1,s.columns()+1);17 utility::matrix m(s.rows()+1,s.columns()+1); 18 18 // Init upper and left border of matrix 19 19 for (size_t i=1; i<m.rows(); i++) … … 23 23 // choice(i,j) tells us how we came to s(i,j). 1 is diagonal, 2 24 24 // vertical, and 3 horizontal, 25 gslapi::matrix choice(m.rows(),m.columns());25 utility::matrix choice(m.rows(),m.columns()); 26 26 27 27 // Calculating NeedlemanWunsch matrix -
trunk/c++_tools/utility/Alignment.h
r304 r616 1 // $Id$2 3 1 #ifndef _theplu_utility_alignment_ 4 2 #define _theplu_utility_alignment_ 3 4 // $Id$ 5 5 6 6 #include <utility> … … 9 9 namespace theplu { 10 10 11 namespace gslapi{11 namespace utility { 12 12 class matrix; 13 13 } … … 36 36 /// @return the global maximum alignment score. 37 37 /// 38 double NeedlemanWunsch(const gslapi::matrix& s,38 double NeedlemanWunsch(const utility::matrix& s, 39 39 std::vector<std::pair<size_t, size_t> >& path, 40 40 const double gap); -
trunk/c++_tools/utility/Makefile.am
r594 r616 26 26 noinst_LTLIBRARIES = libutility.la 27 27 libutility_la_SOURCES = \ 28 Alignment.cc CommandLine.cc FileIO.cc kNNI.cc NNI.cc Option.cc PCA.cc \ 29 stl_utility.cc SVD.cc utility.cc WeNNI.cc 28 Alignment.cc CommandLine.cc FileIO.cc kNNI.cc matrix.cc NNI.cc \ 29 Option.cc PCA.cc stl_utility.cc SVD.cc utility.cc vector.cc WeNNI.cc 30 30 31 31 32 include_utilitydir = $(includedir)/c++_tools/utility 32 33 33 34 include_utility_HEADERS = \ 34 Alignment.h CommandLine.h Exception.h FileIO.h kNNI.h NNI.h Option.h PCA.h\35 stl_utility.h SVD.h utility.h WeNNI.h35 Alignment.h CommandLine.h Exception.h FileIO.h kNNI.h matrix.h NNI.h \ 36 Option.h PCA.h stl_utility.h SVD.h utility.h vector.h WeNNI.h -
trunk/c++_tools/utility/NNI.cc
r570 r616 37 37 namespace utility { 38 38 39 using namespace std;40 41 39 // For a discussion and motivation for various algorithm 42 40 // implementations here see the paper cited in the class definition 43 41 // documentation. 44 NNI::NNI(const gslapi::matrix& matrix,const gslapi::matrix& weight,42 NNI::NNI(const utility::matrix& matrix,const utility::matrix& weight, 45 43 const u_int neighbours) 46 44 : data_(matrix), imputed_data_(matrix), neighbours_(neighbours), … … 53 51 // {\sum_{k=l,C} w_{ik} w_{jk} } 54 52 // where C is the number of columns 55 vector<pair<u_int,double> > NNI::calculate_distances(const u_int row) const 53 std::vector<std::pair<u_int,double> > 54 NNI::calculate_distances(const u_int row) const 56 55 { 57 vector<pair<u_int,double> > distance;56 std::vector<std::pair<u_int,double> > distance; 58 57 for (size_t i=0; i<data_.rows(); i++) 59 58 if (i!=row) { 60 59 double contribs=0; 61 pair<u_int,double> this_distance(i,0.0);60 std::pair<u_int,double> this_distance(i,0.0); 62 61 for (size_t j=0; j<data_.columns(); j++) 63 62 // 0 contribution for missing values … … 82 81 // number, and neighbours are disqualified if their element (column) 83 82 // weight is zero 84 vector<u_int>83 std::vector<u_int> 85 84 NNI::nearest_neighbours(const u_int column, 86 const vector<pair<u_int,double> >& d) const85 const std::vector<std::pair<u_int,double> >& d) const 87 86 { 88 vector<u_int> index;87 std::vector<u_int> index; 89 88 double contribs=0; 90 89 for (u_int i=0; ((i<d.size()) && -
trunk/c++_tools/utility/NNI.h
r570 r616 1 #ifndef _theplu_utility_nni_ 2 #define _theplu_utility_nni_ 3 1 4 // $Id$ 2 5 … … 25 28 */ 26 29 27 #ifndef _theplu_utility_nni_28 #define _theplu_utility_nni_29 30 30 #include <iostream> 31 31 #include <utility> 32 32 #include <vector> 33 33 34 #include <c++_tools/ gslapi/matrix.h>34 #include <c++_tools/utility/matrix.h> 35 35 36 36 namespace theplu { … … 88 88 /// algorithms. 89 89 /// 90 NNI(const gslapi::matrix& matrix,const gslapi::matrix& weight,90 NNI(const utility::matrix& matrix,const utility::matrix& weight, 91 91 const u_int neighbours); 92 92 … … 103 103 /// @return A const reference to the modified data. 104 104 /// 105 const gslapi::matrix& imputed_data(void) const { return imputed_data_; }105 const utility::matrix& imputed_data(void) const { return imputed_data_; } 106 106 107 107 /// … … 115 115 const std::vector<std::pair<u_int,double> >&) const; 116 116 117 const gslapi::matrix& data_;118 gslapi::matrix imputed_data_;117 const utility::matrix& data_; 118 utility::matrix imputed_data_; 119 119 u_int neighbours_; 120 120 std::vector<size_t> not_imputed_; 121 const gslapi::matrix& weight_;121 const utility::matrix& weight_; 122 122 }; 123 123 -
trunk/c++_tools/utility/PCA.cc
r420 r616 10 10 11 11 namespace theplu { 12 namespace utility { 12 namespace utility { 13 13 14 14 15 void PCA::process() 16 { 17 process_ = true; 18 // Row-center the data matrix 19 gslapi::matrix A_center( A_.rows(), A_.columns() ); 20 this->row_center( A_center ); 21 15 void PCA::process(void) 16 { 17 process_ = true; 18 // Row-center the data matrix 19 utility::matrix A_center( A_.rows(), A_.columns() ); 20 this->row_center( A_center ); 22 21 23 24 25 26 gslapi::matrix U = pSVD->U();27 gslapi::matrix V = pSVD->V();22 // Single value decompose the data matrix 23 std::auto_ptr<SVD> pSVD( new SVD( A_center ) ); 24 pSVD->decompose(); 25 utility::matrix U = pSVD->U(); 26 utility::matrix V = pSVD->V(); 28 27 29 30 eigenvectors_ = U;31 eigenvectors_ .transpose();32 eigenvalues_ = pSVD->s();28 // Read the eigenvectors and eigenvalues 29 eigenvectors_ = U; 30 eigenvectors_ .transpose(); 31 eigenvalues_ = pSVD->s(); 33 32 34 // T 35 for( size_t i = 0; i < eigenvalues_.size(); ++i ) 36 { 37 eigenvalues_[ i ] = eigenvalues_[ i ]*eigenvalues_[ i ]; 38 } 33 // T 34 for( size_t i = 0; i < eigenvalues_.size(); ++i ) 35 eigenvalues_[ i ] = eigenvalues_[ i ]*eigenvalues_[ i ]; 36 eigenvalues_ *= 1.0/A_center.rows(); 39 37 40 eigenvalues_ *= 1.0/A_center.rows(); 41 42 // Sort the eigenvectors in order of eigenvalues 43 // Simple (not efficient) algorithm that always 44 // make sure that the i:th element is in its correct 45 // position (N element --> Ordo( N*N )) 46 for ( size_t i = 0; i < eigenvalues_.size(); ++i ) 47 for ( size_t j = i + 1; j < eigenvalues_.size(); ++j ) 48 if ( eigenvalues_[ j ] > eigenvalues_[ i ] ) { 49 std::swap( eigenvalues_[ i ], eigenvalues_[ j ] ); 50 eigenvectors_.swap_rows(i,j); 51 } 52 } 38 // Sort the eigenvectors in order of eigenvalues 39 // Simple (not efficient) algorithm that always 40 // make sure that the i:th element is in its correct 41 // position (N element --> Ordo( N*N )) 42 for ( size_t i = 0; i < eigenvalues_.size(); ++i ) 43 for ( size_t j = i + 1; j < eigenvalues_.size(); ++j ) 44 if ( eigenvalues_[ j ] > eigenvalues_[ i ] ) { 45 std::swap( eigenvalues_[ i ], eigenvalues_[ j ] ); 46 eigenvectors_.swap_rows(i,j); 47 } 48 } 53 49 54 50 51 void PCA::process_transposed_problem(void) 52 { 53 process_ = true; 54 // Row-center the data matrix 55 utility::matrix A_center( A_.rows(), A_.columns() ); 56 this->row_center( A_center ); 55 57 56 void PCA::process_transposed_problem() 57 { 58 process_ = true; 59 // Row-center the data matrix 60 gslapi::matrix A_center( A_.rows(), A_.columns() ); 61 this->row_center( A_center ); 58 // Transform into SVD friendly dimension 59 A_.transpose(); 60 A_center.transpose(); 62 61 63 // Transform into SVD friendly dimension 64 //////////////////////////////////////// 65 A_.transpose(); 66 A_center.transpose(); 62 // Single value decompose the data matrix 63 std::auto_ptr<SVD> pSVD( new SVD( A_center ) ); 64 pSVD->decompose(); 65 utility::matrix U = pSVD->U(); 66 utility::matrix V = pSVD->V(); 67 67 68 // Single value decompose the data matrix 69 std::auto_ptr<SVD> pSVD( new SVD( A_center ) ); 70 pSVD->decompose(); 71 gslapi::matrix U = pSVD->U(); 72 gslapi::matrix V = pSVD->V(); 68 // Read the eigenvectors and eigenvalues 69 eigenvectors_=V; 70 eigenvectors_.transpose(); 71 eigenvalues_ = pSVD->s(); 73 72 74 // Read the eigenvectors and eigenvalues 75 eigenvectors_=V; 76 eigenvectors_.transpose();77 eigenvalues_ = pSVD->s();73 // Transform back when done with SVD! 74 // (used V insted of U now for eigenvectors) 75 A_.transpose(); 76 A_center.transpose(); 78 77 79 // Transform back when done with SVD! 80 // (used V insted of U now for eigenvectors) 81 //////////////////////////////////////////// 82 A_.transpose(); 83 A_center.transpose(); 78 // T 79 for( size_t i = 0; i < eigenvalues_.size(); ++i ) 80 eigenvalues_[ i ] = eigenvalues_[ i ]*eigenvalues_[ i ]; 81 eigenvalues_ *= 1.0/A_center.rows(); 84 82 85 // T 86 for( size_t i = 0; i < eigenvalues_.size(); ++i ) 87 { 88 eigenvalues_[ i ] = eigenvalues_[ i ]*eigenvalues_[ i ]; 89 } 90 91 eigenvalues_ *= 1.0/A_center.rows(); 92 93 // Sort the eigenvectors in order of eigenvalues 94 // Simple (not efficient) algorithm that always 95 // make sure that the i:th element is in its correct 96 // position (N element --> Ordo( N*N )) 97 for ( size_t i = 0; i < eigenvalues_.size(); ++i ) 98 for ( size_t j = i + 1; j < eigenvalues_.size(); ++j ) 99 if ( eigenvalues_[ j ] > eigenvalues_[ i ] ) { 100 std::swap( eigenvalues_[ i ], eigenvalues_[ j ] ); 101 eigenvectors_.swap_rows(i,j); 102 } 103 } 83 // Sort the eigenvectors in order of eigenvalues 84 // Simple (not efficient) algorithm that always 85 // make sure that the i:th element is in its correct 86 // position (N element --> Ordo( N*N )) 87 for ( size_t i = 0; i < eigenvalues_.size(); ++i ) 88 for ( size_t j = i + 1; j < eigenvalues_.size(); ++j ) 89 if ( eigenvalues_[ j ] > eigenvalues_[ i ] ) { 90 std::swap( eigenvalues_[ i ], eigenvalues_[ j ] ); 91 eigenvectors_.swap_rows(i,j); 92 } 93 } 104 94 105 95 106 107 // This function will row-center the matrix A_, 108 // that is, A_ = A_ - M, where M is a matrix 109 // with the meanvalues of each row 110 void PCA::row_center( gslapi::matrix& A_center ) 111 { 112 meanvalues_ = gslapi::vector( A_.rows() ); 113 gslapi::vector A_row_sum(A_.rows()); 114 for (size_t i=0; i<A_row_sum.size(); ++i) 115 A_row_sum(i)=gslapi::vector(A_,i).sum(); 116 for( size_t i = 0; i < A_center.rows(); ++i ) 117 { 118 meanvalues_[i] = A_row_sum(i) / static_cast<double>(A_.columns()); 119 for( size_t j = 0; j < A_center.columns(); ++j ) 120 A_center(i,j) = A_(i,j) - meanvalues_(i); 121 } 122 } 96 // This function will row-center the matrix A_, 97 // that is, A_ = A_ - M, where M is a matrix 98 // with the meanvalues of each row 99 void PCA::row_center(utility::matrix& A_center) 100 { 101 meanvalues_ = utility::vector( A_.rows() ); 102 utility::vector A_row_sum(A_.rows()); 103 for (size_t i=0; i<A_row_sum.size(); ++i) 104 A_row_sum(i)=utility::vector(A_,i).sum(); 105 for( size_t i = 0; i < A_center.rows(); ++i ) { 106 meanvalues_[i] = A_row_sum(i) / static_cast<double>(A_.columns()); 107 for( size_t j = 0; j < A_center.columns(); ++j ) 108 A_center(i,j) = A_(i,j) - meanvalues_(i); 109 } 110 } 123 111 124 112 113 utility::matrix PCA::projection(const utility::matrix& samples ) const 114 { 115 const size_t Ncol = samples.columns(); 116 const size_t Nrow = samples.rows(); 117 utility::matrix projs( Ncol, Ncol ); 125 118 126 gslapi::matrix PCA::projection( const gslapi::matrix& 127 samples ) const 128 { 129 const size_t Ncol = samples.columns(); 130 const size_t Nrow = samples.rows(); 131 gslapi::matrix projs( Ncol, Ncol ); 132 133 gslapi::vector temp(samples.rows()); 134 for( size_t j = 0; j < Ncol; ++j ) 135 { 119 utility::vector temp(samples.rows()); 120 for( size_t j = 0; j < Ncol; ++j ) { 136 121 for (size_t i=0; i<Ncol; ++i ) 137 temp(i) = samples(i,j); 138 gslapi::vector centered( Nrow ); 139 140 for( size_t i = 0; i < Nrow; ++i ) 141 centered(i) = temp(i) - meanvalues_(i); 142 143 gslapi::vector proj( eigenvectors_ * centered ); 144 for( size_t i = 0; i < Ncol; ++i ) 122 temp(i) = samples(i,j); 123 utility::vector centered( Nrow ); 124 for( size_t i = 0; i < Nrow; ++i ) 125 centered(i) = temp(i) - meanvalues_(i); 126 utility::vector proj( eigenvectors_ * centered ); 127 for( size_t i = 0; i < Ncol; ++i ) 145 128 projs(i,j)=proj(i); 146 } 147 148 }129 } 130 return projs; 131 } 149 132 150 133 134 utility::matrix 135 PCA::projection_transposed(const utility::matrix& samples) const 136 { 137 const size_t Ncol = samples.columns(); 138 const size_t Nrow = samples.rows(); 139 utility::matrix projs( Nrow, Ncol ); 151 140 152 gslapi::matrix PCA::projection_transposed( const gslapi::matrix& 153 samples ) const 154 { 155 const size_t Ncol = samples.columns(); 156 const size_t Nrow = samples.rows(); 157 gslapi::matrix projs( Nrow, Ncol ); 158 159 gslapi::vector temp(samples.rows()); 160 for( size_t j = 0; j < Ncol; ++j ) 161 { 141 utility::vector temp(samples.rows()); 142 for( size_t j = 0; j < Ncol; ++j ) { 162 143 for (size_t i=0; i<Ncol; ++i ) 163 temp(i) = samples(i,j); 164 gslapi::vector centered( Nrow ); 165 166 for( size_t i = 0; i < Nrow; ++i ) 167 centered(i)=temp(i)-meanvalues_(i); 168 169 gslapi::vector proj( eigenvectors_ * centered ); 170 for( size_t i = 0; i < Nrow; ++i ) 171 projs(i,j)=proj(i); 172 } 173 return projs; 174 } 144 temp(i) = samples(i,j); 145 utility::vector centered( Nrow ); 146 for( size_t i = 0; i < Nrow; ++i ) 147 centered(i)=temp(i)-meanvalues_(i); 148 utility::vector proj( eigenvectors_ * centered ); 149 for( size_t i = 0; i < Nrow; ++i ) 150 projs(i,j)=proj(i); 151 } 152 return projs; 153 } 175 154 176 155 177 178 void PCA::calculate_explained_intensity() 179 { 180 size_t DIM = eigenvalues_.size(); 181 explained_intensity_ = gslapi::vector( DIM ); 182 double sum = 0; 183 for( size_t i = 0; i < DIM; ++i ) 184 { 185 sum += eigenvalues_[ i ]; 186 } 187 double exp_sum = 0; 188 for( size_t i = 0; i < DIM; ++i ) 189 { 190 exp_sum += eigenvalues_[ i ]; 191 explained_intensity_[ i ] = exp_sum / sum ; 192 } 193 } 156 void PCA::calculate_explained_intensity(void) 157 { 158 size_t DIM = eigenvalues_.size(); 159 explained_intensity_ = utility::vector( DIM ); 160 double sum = 0; 161 for( size_t i = 0; i < DIM; ++i ) 162 sum += eigenvalues_[ i ]; 163 double exp_sum = 0; 164 for( size_t i = 0; i < DIM; ++i ) { 165 exp_sum += eigenvalues_[ i ]; 166 explained_intensity_[ i ] = exp_sum / sum ; 167 } 168 } 194 169 195 170 196 197 double PCA::get_explained_intensity( const size_t& k ) 198 { 199 if( !explained_calc_ ) 200 { 201 this->calculate_explained_intensity(); 202 explained_calc_ = true; 203 } 204 return explained_intensity_[ k ]; 205 } 171 double PCA::get_explained_intensity( const size_t& k ) 172 { 173 if( !explained_calc_ ) { 174 this->calculate_explained_intensity(); 175 explained_calc_ = true; 176 } 177 return explained_intensity_[ k ]; 178 } 206 179 207 180 -
trunk/c++_tools/utility/PCA.h
r420 r616 1 // $Id$2 3 1 #ifndef _theplu_utility_pca_ 4 2 #define _theplu_utility_pca_ 5 3 6 #include <c++_tools/gslapi/matrix.h> 7 #include <c++_tools/gslapi/vector.h> 4 // $Id$ 5 6 #include <c++_tools/utility/matrix.h> 7 #include <c++_tools/utility/vector.h> 8 8 9 9 … … 34 34 should have been performed and no products. 35 35 */ 36 inline explicit PCA(const gslapi::matrix& A)36 inline explicit PCA(const utility::matrix& A) 37 37 : A_(A), process_(false), explained_calc_(false) {} 38 38 … … 59 59 @return Eigenvector \a i. 60 60 */ 61 inline gslapi::vector get_eigenvector(const size_t& i) const62 { return gslapi::vector(eigenvectors_,i); }61 inline utility::vector get_eigenvector(const size_t& i) const 62 { return utility::vector(eigenvectors_,i); } 63 63 64 64 /** … … 83 83 spanned by the eigenvectors. 84 84 */ 85 gslapi::matrix projection( const gslapi::matrix& ) const;85 utility::matrix projection( const utility::matrix& ) const; 86 86 87 87 /** … … 89 89 process_transposed_problem(). 90 90 */ 91 gslapi::matrix projection_transposed( const gslapi::matrix& ) const;91 utility::matrix projection_transposed( const utility::matrix& ) const; 92 92 93 93 94 94 private: 95 gslapi::matrix A_;96 gslapi::matrix eigenvectors_;97 gslapi::vector eigenvalues_;98 gslapi::vector explained_intensity_;99 gslapi::vector meanvalues_;95 utility::matrix A_; 96 utility::matrix eigenvectors_; 97 utility::vector eigenvalues_; 98 utility::vector explained_intensity_; 99 utility::vector meanvalues_; 100 100 bool process_, explained_calc_; 101 101 … … 105 105 with the meanvalues of each row 106 106 */ 107 void row_center( gslapi::matrix& A_center );107 void row_center( utility::matrix& A_center ); 108 108 109 109 /** -
trunk/c++_tools/utility/SVD.cc
r420 r616 17 17 case Jacobi: 18 18 return jacobi(); 19 19 } 20 20 // the program should never end up here, return values should be 21 21 // something different from normal GSL return values, or maybe … … 28 28 int SVD::golub_reinsch(void) 29 29 { 30 gslapi::vector w(U_.columns());30 utility::vector w(U_.columns()); 31 31 return gsl_linalg_SV_decomp(U_.gsl_matrix_p(), V_.gsl_matrix_p(), 32 32 s_.gsl_vector_p(), w.gsl_vector_p()); … … 37 37 int SVD::modified_golub_reinsch(void) 38 38 { 39 gslapi::vector w(U_.columns());40 gslapi::matrix X(U_.columns(),U_.columns());39 utility::vector w(U_.columns()); 40 utility::matrix X(U_.columns(),U_.columns()); 41 41 return gsl_linalg_SV_decomp_mod(U_.gsl_matrix_p(), X.gsl_matrix_p(), 42 42 V_.gsl_matrix_p(), s_.gsl_vector_p(), -
trunk/c++_tools/utility/SVD.h
r597 r616 1 // $Id$2 3 1 #ifndef _theplu_utility_svd_ 4 2 #define _theplu_utility_svd_ 5 3 6 #include <c++_tools/gslapi/matrix.h> 7 #include <c++_tools/gslapi/vector.h> 4 // $Id$ 5 6 #include <c++_tools/utility/matrix.h> 7 #include <c++_tools/utility/vector.h> 8 8 9 9 #include <gsl/gsl_linalg.h> … … 52 52 /// input matrix is copied for further use in the object. 53 53 /// 54 inline SVD(const gslapi::matrix& Ain)54 inline SVD(const utility::matrix& Ain) 55 55 : U_(Ain), V_(Ain.columns(),Ain.columns()), s_(Ain.columns()) {} 56 56 … … 73 73 /// is undefined. 74 74 /// 75 inline const gslapi::vector& s(void) const { return s_; }75 inline const utility::vector& s(void) const { return s_; } 76 76 77 77 /// … … 83 83 /// @return Whatever GSL returns. 84 84 /// 85 inline int solve( gslapi::vector b, gslapi::vector x)85 inline int solve(utility::vector b, utility::vector x) 86 86 { return gsl_linalg_SV_solve(U_.gsl_matrix_p(), V_.gsl_matrix_p(), 87 87 s_.gsl_vector_p(), b.gsl_vector_p(), … … 96 96 /// is undefined. 97 97 /// 98 inline const gslapi::matrix& U(void) const { return U_; }98 inline const utility::matrix& U(void) const { return U_; } 99 99 100 100 /// … … 106 106 /// is undefined. 107 107 /// 108 inline const gslapi::matrix& V(void) const { return V_; }108 inline const utility::matrix& V(void) const { return V_; } 109 109 110 110 private: … … 115 115 int modified_golub_reinsch(void); 116 116 117 gslapi::matrix U_, V_;118 gslapi::vector s_;117 utility::matrix U_, V_; 118 utility::vector s_; 119 119 }; 120 120 -
trunk/c++_tools/utility/WeNNI.cc
r570 r616 27 27 #include <c++_tools/utility/WeNNI.h> 28 28 29 #include <c++_tools/utility/matrix.h> 29 30 #include <c++_tools/utility/stl_utility.h> 30 31 … … 37 38 38 39 39 WeNNI::WeNNI(const gslapi::matrix& matrix,const gslapi::matrix& flag,40 WeNNI::WeNNI(const utility::matrix& matrix,const utility::matrix& flag, 40 41 const u_int neighbours) 41 42 : NNI(matrix,flag,neighbours), imputed_data_raw_(matrix) … … 52 53 u_int WeNNI::estimate(void) 53 54 { 54 using namespace std;55 55 for (unsigned int i=0; i<data_.rows(); i++) { 56 56 // Jari, avoid copying in next line 57 vector<pair<u_int,double> > distance=calculate_distances(i);58 57 std::vector<std::pair<u_int,double> > distance=calculate_distances(i); 58 std::sort(distance.begin(),distance.end(), 59 59 pair_value_compare<u_int,double>()); 60 60 bool row_imputed=true; 61 61 for (unsigned int j=0; j<data_.columns(); j++) { 62 vector<u_int> knn=nearest_neighbours(j,distance);62 std::vector<u_int> knn=nearest_neighbours(j,distance); 63 63 double new_value=0.0; 64 64 double norm=0.0; 65 for (vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); k++) { 65 for (std::vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); 66 ++k) { 66 67 // Jari, a small number needed here, use something standardized. 67 68 // Avoid division with zero (perfect match vectors) -
trunk/c++_tools/utility/WeNNI.h
r570 r616 1 #ifndef _theplu_utility_wenni_ 2 #define _theplu_utility_wenni_ 3 1 4 // $Id$ 2 5 … … 25 28 */ 26 29 27 #ifndef _theplu_utility_wenni_28 #define _theplu_utility_wenni_29 30 30 #include <c++_tools/utility/NNI.h> 31 31 32 #include <c++_tools/ gslapi/matrix.h>32 #include <c++_tools/utility/matrix.h> 33 33 34 34 #include <iostream> … … 54 54 /// Constructor 55 55 /// 56 WeNNI(const gslapi::matrix& matrix,const gslapi::matrix& weight,56 WeNNI(const utility::matrix& matrix,const utility::matrix& weight, 57 57 const u_int neighbours); 58 58 … … 67 67 /// @return A const reference to imputed_data_raw. 68 68 /// 69 const gslapi::matrix& imputed_data_raw(void) const69 const utility::matrix& imputed_data_raw(void) const 70 70 { return imputed_data_raw_; } 71 71 … … 74 74 private: 75 75 76 gslapi::matrix imputed_data_raw_;76 utility::matrix imputed_data_raw_; 77 77 }; 78 78 -
trunk/c++_tools/utility/kNNI.cc
r570 r616 37 37 namespace utility { 38 38 39 kNNI::kNNI(const gslapi::matrix& matrix,const gslapi::matrix& flag,39 kNNI::kNNI(const utility::matrix& matrix,const utility::matrix& flag, 40 40 const u_int neighbours) 41 41 : NNI(matrix,flag,neighbours) … … 58 58 u_int kNNI::estimate(void) 59 59 { 60 using namespace std;61 60 for (unsigned int i=0; i<mv_rows_.size(); i++) { 62 61 // Jari, avoid copying in next line 63 vector<pair<u_int,double> > distance=calculate_distances(mv_rows_[i]); 64 sort(distance.begin(),distance.end(), 62 std::vector<std::pair<u_int,double> > distance= 63 calculate_distances(mv_rows_[i]); 64 std::sort(distance.begin(),distance.end(), 65 65 pair_value_compare<u_int,double>()); 66 66 for (unsigned int j=0; j<data_.columns(); j++) 67 67 if (!weight_(mv_rows_[i],j)) { 68 vector<u_int> knn=nearest_neighbours(j,distance);68 std::vector<u_int> knn=nearest_neighbours(j,distance); 69 69 double new_value=0.0; 70 70 double norm=0.0; 71 for (vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); k++){ 71 for (std::vector<u_int>::const_iterator k=knn.begin(); k!=knn.end(); 72 ++k) { 72 73 // Jari, a small number needed here, use something standardized. 73 74 // Avoid division with zero (perfect match vectors) -
trunk/c++_tools/utility/kNNI.h
r570 r616 1 #ifndef _theplu_utility_knni_ 2 #define _theplu_utility_knni_ 3 1 4 // $Id$ 2 5 … … 25 28 */ 26 29 27 #ifndef _theplu_utility_knni_28 #define _theplu_utility_knni_29 30 30 #include <c++_tools/utility/NNI.h> 31 31 … … 52 52 /// Constructor 53 53 /// 54 kNNI(const gslapi::matrix& matrix,const gslapi::matrix& weight,54 kNNI(const utility::matrix& matrix,const utility::matrix& weight, 55 55 const u_int neighbours); 56 56 -
trunk/c++_tools/utility/matrix.cc
r613 r616 25 25 */ 26 26 27 #include <c++_tools/ gslapi/matrix.h>28 29 #include <c++_tools/ gslapi/vector.h>27 #include <c++_tools/utility/matrix.h> 28 29 #include <c++_tools/utility/vector.h> 30 30 #include <c++_tools/utility/stl_utility.h> 31 31 #include <c++_tools/utility/utility.h> … … 39 39 40 40 namespace theplu { 41 namespace gslapi{41 namespace utility { 42 42 43 43 … … 242 242 243 243 244 }} // of namespace gslapiand namespace thep244 }} // of namespace utility and namespace thep -
trunk/c++_tools/utility/matrix.h
r614 r616 1 #ifndef _theplu_utility_matrix_ 2 #define _theplu_utility_matrix_ 3 1 4 // $Id$ 2 5 … … 26 29 */ 27 30 28 #ifndef _theplu_gslapi_matrix_ 29 #define _theplu_gslapi_matrix_ 30 31 #include <c++_tools/gslapi/vector.h> 31 #include <c++_tools/utility/vector.h> 32 32 #include <c++_tools/utility/Exception.h> 33 33 … … 36 36 37 37 namespace theplu { 38 namespace gslapi{38 namespace utility { 39 39 40 40 class vector; … … 51 51 /// 52 52 /// \par[Matrix views] GSL matrix views are supported and these are 53 /// disguised as ordinary gslapi::matrix'es. A support function is54 /// added, gslapi::matrix::isview(), that can be used to check if a53 /// disguised as ordinary utility::matrix'es. A support function is 54 /// added, utility::matrix::isview(), that can be used to check if a 55 55 /// matrix object is a view. Note that view matricess do not own the 56 56 /// undelying data, and a view is not valid if the matrix owning the … … 61 61 /// this interface class. Most notable GSL functionality not 62 62 /// supported are no binary file support and views on arrays, 63 /// gslapi::vectors, gsl_vectors, diagonals, subdiagonals, and63 /// utility::vectors, gsl_vectors, diagonals, subdiagonals, and 64 64 /// superdiagonals. If there is an interest from the user community, 65 65 /// the omitted functionality could be included. … … 464 464 465 465 466 }} // of namespace gslapiand namespace theplu466 }} // of namespace utility and namespace theplu 467 467 468 468 #endif -
trunk/c++_tools/utility/utility.cc
r609 r616 26 26 #include <c++_tools/utility/utility.h> 27 27 28 #include <c++_tools/random/random.h> 28 29 #include <c++_tools/utility/stl_utility.h> 30 #include <c++_tools/utility/vector.h> 29 31 30 32 #include <sstream> … … 88 90 } 89 91 92 93 std::pair<size_t, size_t> minmax_index(const vector& vec, 94 const std::vector<size_t>& subset) 95 { 96 size_t min_index = subset[0]; 97 size_t max_index = subset[0]; 98 for (unsigned int i=1; i<subset.size(); i++) 99 if (vec[subset[i]] < vec[min_index]) 100 min_index = subset[i]; 101 else if (vec[subset[i]] > vec[max_index]) 102 max_index = subset[i]; 103 return std::pair<size_t,size_t>(min_index, max_index); 104 } 105 106 107 void shuffle(vector& invec) 108 { 109 vector vec(invec); 110 random::DiscreteUniform rnd; 111 for (size_t i=0; i<vec.size(); i++){ 112 size_t index = rnd(vec.size()-i); 113 invec[i]=vec(index); 114 vec(index)=vec(vec.size()-i-1); 115 } 116 } 117 118 90 119 }} // end of namespace utility and namespace thep -
trunk/c++_tools/utility/utility.h
r570 r616 1 #ifndef _theplu_utility_utility_ 2 #define _theplu_utility_utility_ 3 1 4 // $Id$ 2 5 … … 24 27 */ 25 28 26 #ifndef _theplu_utility_utility_27 #define _theplu_utility_utility_28 29 29 /// 30 30 /// @file utility/utility.h … … 34 34 35 35 #include <string> 36 #include <utility> 37 #include <vector> 36 38 37 39 namespace theplu { 38 40 namespace utility { 39 41 42 class vector; 40 43 41 44 /// … … 60 63 bool is_nan(const std::string& s); 61 64 65 /** 66 This function returns the indices of the minimum and maximum 67 values in the sub-vector (defined by \a subset), storing them in 68 imin and imax. When there are several equal minimum or maximum 69 elements then the lowest indices are returned. The returned index 70 is the index from the complete vector (not the sub-vector) 71 72 @return Index corresponding to the smallest and largest value. 73 74 @note If \a subset is emtpy, the result is undefined. 75 */ 76 std::pair<size_t,size_t> minmax_index(const vector& vec, 77 const std::vector<size_t>& subset); 78 79 /** 80 Randomly shuffles the elements in vector \a invec 81 */ 82 void shuffle(vector& invec); 83 84 62 85 }} // of namespace utility and namespace theplu 63 86 -
trunk/c++_tools/utility/vector.cc
r613 r616 26 26 */ 27 27 28 #include <c++_tools/ gslapi/vector.h>29 #include <c++_tools/ gslapi/matrix.h>28 #include <c++_tools/utility/vector.h> 29 #include <c++_tools/utility/matrix.h> 30 30 #include <c++_tools/utility/stl_utility.h> 31 31 #include <c++_tools/utility/utility.h> … … 39 39 40 40 namespace theplu { 41 namespace gslapi{41 namespace utility { 42 42 43 43 … … 265 265 266 266 267 }} // of namespace gslapiand namespace thep267 }} // of namespace utility and namespace thep -
trunk/c++_tools/utility/vector.h
r613 r616 1 #ifndef _theplu_utility_vector_ 2 #define _theplu_utility_vector_ 3 1 4 // $Id$ 2 5 … … 26 29 */ 27 30 28 #ifndef _theplu_gslapi_vector_29 #define _theplu_gslapi_vector_30 31 31 #include <c++_tools/utility/Exception.h> 32 32 … … 39 39 40 40 namespace theplu { 41 namespace gslapi{41 namespace utility { 42 42 43 43 class matrix; … … 54 54 /// 55 55 /// \par[Vector views] GSL vector views are supported and these are 56 /// disguised as ordinary gslapi::vectors. A support function is57 /// added, gslapi::vector::isview(), that can be used to check if a56 /// disguised as ordinary utility::vectors. A support function is 57 /// added, utility::vector::isview(), that can be used to check if a 58 58 /// vector object is a view. Note that view vectors do not own the 59 59 /// undelying data, and a view is not valid if the vector owning the … … 491 491 492 492 493 }} // of namespace gslapiand namespace theplu493 }} // of namespace utility and namespace theplu 494 494 495 495 #endif
Note: See TracChangeset
for help on using the changeset viewer.