Changeset 1009
- Timestamp:
- Feb 1, 2008, 5:15:44 AM (16 years ago)
- Location:
- trunk
- Files:
-
- 18 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/matrix_test.cc
r1000 r1009 36 36 : m_(i,j,value) {} 37 37 38 inline theplu::yat::utility::vector 39 row(const size_t& i) { return theplu::yat::utility::vector(m_,i); }38 inline theplu::yat::utility::vectorView 39 row(const size_t& i) { return m_.row_vec(i); } 40 40 41 41 inline const theplu::yat::utility::matrix& matrix(void) const { return m_; } … … 258 258 for (size_t j=0; j<m5.columns(); ++j) 259 259 m5_sum2+=m5(i,j); 260 utility::vector v5subrow(m5,3);260 utility::vectorView v5subrow(m5,3); 261 261 double v5subrow_sum=0; 262 262 for (size_t i=0; i<v5subrow.size(); ++i) { … … 280 280 for (size_t j=0; j<m5.columns(); ++j) 281 281 m5_sum3+=m5(i,j); 282 utility::vector v5subcolumn(m5,0,false);282 utility::vectorView v5subcolumn = m5.column_vec(0); 283 283 double v5subcolumn_sum=0; 284 284 for (size_t i=0; i<v5subcolumn.size(); ++i) { … … 332 332 *error << "\tthat class member returns a view" << std::endl; 333 333 matrixwrapper mw(5,2); 334 utility::vector mwrow=mw.row(2);334 utility::vectorView mwrow=mw.row(2); 335 335 if (mwrow.gsl_vector_p()->data != &(mw.matrix()(2,0))) { 336 336 ok=false; -
trunk/test/score_test.cc
r1000 r1009 32 32 #include "yat/utility/matrix.h" 33 33 #include "yat/utility/vector.h" 34 #include "yat/utility/vectorView.h" 34 35 35 36 #include <cmath> … … 92 93 const double tol = 0.001; 93 94 for (size_t i=0; i<data.rows(); i++){ 94 utility::vector vec(data,i);95 utility::vectorView vec(data,i); 95 96 if (vec.size()!=target2.size()){ 96 97 *error << "vec.size() is " << vec.size() << " and target2.size() is " … … 108 109 utility::vector weight(target2.size(),1); 109 110 for (size_t i=0; i<data.rows(); i++){ 110 utility::vector vec(data,i);111 utility::vectorView vec = data.row_vec(i); 111 112 area = auc.score(target2, vec, weight); 112 113 if (area<correct_area(i)-tol || area>correct_area(i)+tol){ -
trunk/test/vector_test.cc
r1000 r1009 29 29 #include "yat/utility/utility.h" 30 30 #include "yat/utility/vector.h" 31 #include "yat/utility/vectorView.h" 31 32 32 33 #include <fstream> … … 73 74 for (size_t i=0; i<vec.size(); i+=2) 74 75 sum_before+=vec[i]; 75 utility::vector vec_view(vec,0,6,2);76 utility::vectorView vec_view(vec,0,6,2); 76 77 sum_after=utility::sum(vec_view); 77 78 ok &= (sum_after==sum_before); … … 87 88 *message << "const view implementation" << std::endl; 88 89 const utility::vector vv(10,3.0); 89 utility::vector vview(vv,0,5,1);90 utility::vectorView vview(vv,0,5,1); 90 91 // const utility::vector vview(vv,0,5,1); // this is the proper line 91 92 utility::vector vv2(5,2.0); … … 113 114 ok &= (vec_view.size()==vec3.size()); 114 115 ok &= (vec3 == vec_view); 115 ok &= (&vec3 != &vec_view);116 116 ok &= !vec3.isview(); 117 117 } 118 118 119 /* different sizes are allowed in new design 119 120 // checking that assignment operator throws an exception if vectors 120 121 // differ in size … … 140 141 gsl_set_error_handler(err_handler); 141 142 } 143 */ 142 144 143 145 // checking that assignment operator changes the underlying object when … … 146 148 *message << "assignment operator on view" << std::endl; 147 149 vec[3]=vec[4]=vec[5]=13; 148 utility::vector vec_view(vec,3,3,1);150 utility::vectorView vec_view(vec,3,3,1); 149 151 utility::vector vec2(3,123.0); 150 152 vec_view=vec2; 151 if (vec[3]!=vec_view[0] || vec[4]!=vec_view[1] || vec[5]!=vec_view[2]) 152 ok=false; 153 if (vec[3]!=vec_view[0] || vec[4]!=vec_view[1] || vec[5]!=vec_view[2]){ 154 *message << " failed\n"; 155 ok=false; 156 } 153 157 } 154 158 … … 159 163 *message << "\tcloning normal vector" << std::endl; 160 164 utility::vector vec2(3,123.0); 161 vec2 .clone(vec);165 vec2 = vec; 162 166 if (vec.size()!=vec2.size()) 163 167 this_ok=false; … … 169 173 this_ok=false; 170 174 *message << "\tcloning vector view" << std::endl; 171 utility::vector* vec_view=new utility::vector(vec,3,3,1); 172 utility::vector vec_view2; 173 vec_view2.clone(*vec_view); 175 utility::vectorView* vec_view=new utility::vectorView(vec,3,3,1); 176 utility::vectorView vec_view2(*vec_view); 174 177 if (!vec_view2.isview()) 175 178 this_ok=false; -
trunk/yat/classifier/IGP.h
r1000 r1009 86 86 87 87 // Calculate IGP for each class 88 igp_ .clone(utility::vector(target_.nof_classes()));88 igp_ = utility::vector(target_.nof_classes()); 89 89 90 90 for(u_int i=0; i<target_.size(); i++) { -
trunk/yat/classifier/KNN.h
r999 r1009 261 261 for(size_t sample=0;sample<distances->columns();sample++) { 262 262 std::vector<size_t> k_index; 263 utility::sort_smallest_index(k_index,k_, utility::vector(*distances,sample,false));263 utility::sort_smallest_index(k_index,k_,distances->column_vec(sample)); 264 264 for(size_t j=0;j<k_index.size();j++) { 265 265 prediction(target_(k_index[j]),sample)++; -
trunk/yat/classifier/NBC.cc
r1000 r1009 205 205 206 206 // normalize each row (label) to sum up to unity (probability) 207 for (size_t i=0; i<prediction.rows(); ++i) 208 utility::vector(prediction,i) *= 209 1.0/utility::sum(utility::vector(prediction,i)); 210 207 for (size_t i=0; i<prediction.rows(); ++i){ 208 prediction.row_vec(i) *= 209 1.0/sum( 210 prediction.row_vec(i) 211 ); 212 } 211 213 } 212 214 -
trunk/yat/classifier/SVM.cc
r1000 r1009 198 198 { 199 199 trained_=false; 200 alpha_ .clone(utility::vector(target_.size(), 0));200 alpha_ = utility::vector(target_.size(), 0); 201 201 } 202 202 -
trunk/yat/classifier/utility.cc
r1000 r1009 35 35 void convert(const DataLookup1D& lookup, utility::vector& vector) 36 36 { 37 vector .clone(utility::vector(lookup.size()));37 vector = utility::vector(lookup.size()); 38 38 for(u_int i=0; i<lookup.size(); i++) 39 39 vector(i)=lookup(i); … … 44 44 { 45 45 46 value .clone(utility::vector(lookup.size()));47 weight .clone(utility::vector(lookup.size()));46 value = utility::vector(lookup.size()); 47 weight = utility::vector(lookup.size()); 48 48 for(u_int i=0; i<lookup.size(); i++){ 49 49 value(i)=lookup.data(i); -
trunk/yat/regression/Local.cc
r1000 r1009 27 27 #include "OneDimensionalWeighted.h" 28 28 #include "yat/utility/vector.h" 29 #include "yat/utility/vectorView.h" 29 30 30 31 #include <algorithm> … … 59 60 60 61 size_t nof_fits=data_.size()/step_size; 61 x_ .clone(utility::vector(nof_fits));62 y_predicted_ .clone(utility::vector(x_.size()));63 y_err_ .clone(utility::vector(x_.size()));62 x_ = utility::vector(nof_fits); 63 y_predicted_ = utility::vector(x_.size()); 64 y_err_ = utility::vector(x_.size()); 64 65 sort(data_.begin(), data_.end()); 65 66 … … 102 103 assert(max_index<data_.size()); 103 104 104 utility::vector x_local(x, min_index, max_index-min_index+1);105 utility::vector y_local(y, min_index, max_index-min_index+1);105 utility::vectorView x_local(x, min_index, max_index-min_index+1); 106 utility::vectorView y_local(y, min_index, max_index-min_index+1); 106 107 107 108 // calculating weights -
trunk/yat/regression/MultiDimensional.cc
r1000 r1009 58 58 assert(x.rows()==y.size()); 59 59 covariance_.clone(utility::matrix(x.columns(),x.columns())); 60 fit_parameters_ .clone(utility::vector(x.columns()));60 fit_parameters_ = utility::vector(x.columns()); 61 61 if (work_) 62 62 gsl_multifit_linear_free(work_); -
trunk/yat/regression/MultiDimensionalWeighted.cc
r1000 r1009 59 59 60 60 covariance_.clone(utility::matrix(x.columns(),x.columns())); 61 fit_parameters_ .clone(utility::vector(x.columns()));61 fit_parameters_ = utility::vector(x.columns()); 62 62 if (work_) 63 63 gsl_multifit_linear_free(work_); -
trunk/yat/statistics/AveragerPairWeighted.h
r1000 r1009 201 201 { 202 202 for (size_t i=0; i<x.size(); ++i){ 203 utility::yat_assert<std::runtime_error>(!std::isnan(x [i]));203 utility::yat_assert<std::runtime_error>(!std::isnan(x(i))); 204 204 add(x[i],y[i],wx[i],wy[i]); 205 205 } -
trunk/yat/utility/Makefile.am
r1000 r1009 28 28 Alignment.cc ColumnStream.cc CommandLine.cc FileUtil.cc kNNI.cc \ 29 29 matrix.cc NNI.cc Option.cc OptionFile.cc OptionHelp.cc OptionSwitch.cc \ 30 PCA.cc stl_utility.cc SVD.cc TypeInfo.cc utility.cc vector.cc WeNNI.cc 30 PCA.cc stl_utility.cc SVD.cc TypeInfo.cc utility.cc vector.cc \ 31 vectorBase.cc vectorView.cc WeNNI.cc 31 32 32 33 include_utilitydir = $(includedir)/yat/utility … … 37 38 IteratorWeighted.h kNNI.h matrix.h NNI.h \ 38 39 Option.h OptionArg.h OptionFile.h OptionHelp.h OptionSwitch.h \ 39 PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h WeNNI.h \ 40 yat_assert.h 40 PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h \ 41 vectorBase.h vectorView.h \ 42 WeNNI.h yat_assert.h 41 43 -
trunk/yat/utility/PCA.cc
r1000 r1009 29 29 #include "SVD.h" 30 30 #include "utility.h" 31 #include "vectorView.h" 31 32 32 33 #include <iostream> … … 73 74 eigenvectors_.clone(U); 74 75 eigenvectors_ .transpose(); 76 eigenvalues_ = pSVD->s(); 77 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(); 82 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 } 94 95 96 /* 97 void PCA::process_transposed_problem(void) 98 { 99 // Row-center the data matrix 100 utility::matrix A_center( A_.rows(), A_.columns() ); 101 this->row_center( A_center ); 102 103 // Transform into SVD friendly dimension 104 A_.transpose(); 105 A_center.transpose(); 106 107 // Single value decompose the data matrix 108 std::auto_ptr<SVD> pSVD( new SVD( A_center ) ); 109 pSVD->decompose(); 110 utility::matrix U(pSVD->U()); 111 utility::matrix V(pSVD->V()); 112 113 // Read the eigenvectors and eigenvalues 114 eigenvectors_.clone(V); 115 eigenvectors_.transpose(); 75 116 eigenvalues_.clone(pSVD->s()); 117 118 // Transform back when done with SVD! 119 // (used V insted of U now for eigenvectors) 120 A_.transpose(); 121 A_center.transpose(); 76 122 77 123 // T … … 91 137 } 92 138 } 93 94 95 /*96 void PCA::process_transposed_problem(void)97 {98 // Row-center the data matrix99 utility::matrix A_center( A_.rows(), A_.columns() );100 this->row_center( A_center );101 102 // Transform into SVD friendly dimension103 A_.transpose();104 A_center.transpose();105 106 // Single value decompose the data matrix107 std::auto_ptr<SVD> pSVD( new SVD( A_center ) );108 pSVD->decompose();109 utility::matrix U(pSVD->U());110 utility::matrix V(pSVD->V());111 112 // Read the eigenvectors and eigenvalues113 eigenvectors_.clone(V);114 eigenvectors_.transpose();115 eigenvalues_.clone(pSVD->s());116 117 // Transform back when done with SVD!118 // (used V insted of U now for eigenvectors)119 A_.transpose();120 A_center.transpose();121 122 // T123 for( size_t i = 0; i < eigenvalues_.size(); ++i )124 eigenvalues_[ i ] = eigenvalues_[ i ]*eigenvalues_[ i ];125 eigenvalues_ *= 1.0/A_center.rows();126 127 // Sort the eigenvectors in order of eigenvalues128 // Simple (not efficient) algorithm that always129 // make sure that the i:th element is in its correct130 // position (N element --> Ordo( N*N ))131 for ( size_t i = 0; i < eigenvalues_.size(); ++i )132 for ( size_t j = i + 1; j < eigenvalues_.size(); ++j )133 if ( eigenvalues_[ j ] > eigenvalues_[ i ] ) {134 std::swap( eigenvalues_[ i ], eigenvalues_[ j ] );135 eigenvectors_.swap_rows(i,j);136 }137 }138 139 */ 139 140 … … 189 190 void PCA::row_center(utility::matrix& A_center) 190 191 { 191 meanvalues_ .clone(utility::vector(A_.rows()));192 meanvalues_ = vector(A_.rows()); 192 193 utility::vector A_row_sum(A_.rows()); 193 for (size_t i=0; i<A_row_sum.size(); ++i) 194 A_row_sum(i)=utility::sum(utility::vector(A_,i)); 194 for (size_t i=0; i<A_row_sum.size(); ++i){ 195 const vectorView tmp(A_.row_vec(i)); 196 A_row_sum(i) = sum(tmp); 197 } 195 198 for( size_t i = 0; i < A_center.rows(); ++i ) { 196 meanvalues_ [i]= A_row_sum(i) / static_cast<double>(A_.columns());199 meanvalues_(i) = A_row_sum(i) / static_cast<double>(A_.columns()); 197 200 for( size_t j = 0; j < A_center.columns(); ++j ) 198 201 A_center(i,j) = A_(i,j) - meanvalues_(i); -
trunk/yat/utility/matrix.cc
r1000 r1009 27 27 #include "matrix.h" 28 28 #include "vector.h" 29 #include "vectorView.h" 29 30 #include "utility.h" 30 31 … … 302 303 303 304 304 void matrix::column(const size_t column, const vector& vec) 305 { 306 assert(m_); 307 int status=gsl_matrix_set_col(m_, column, vec.gsl_vector_p()); 308 if (status) 309 throw utility::GSL_error(std::string("matrix::set_column",status)); 310 } 311 312 313 void matrix::row(const size_t row, const vector& vec) 314 { 315 assert(m_); 316 int status=gsl_matrix_set_row(m_, row, vec.gsl_vector_p()); 317 if (status) 318 throw utility::GSL_error(std::string("matrix::set_row",status)); 305 vectorView matrix::column_vec(size_t col) 306 { 307 vectorView res(*this, col, false); 308 return res; 309 } 310 311 312 const vectorView matrix::column_vec(size_t col) const 313 { 314 return vectorView(*this, col, false); 315 } 316 317 318 const vectorView matrix::row_vec(size_t col) const 319 { 320 return vectorView(*this, col, true); 321 } 322 323 324 vectorView matrix::row_vec(size_t row) 325 { 326 vectorView res(*this, row, true); 327 return res; 319 328 } 320 329 … … 549 558 550 559 551 vector operator*(const matrix& m, const vector & v)560 vector operator*(const matrix& m, const vectorBase& v) 552 561 { 553 562 utility::vector res(m.rows()); 554 563 for (size_t i=0; i<res.size(); ++i) 555 res(i) = vector (m,i) * v;564 res(i) = vectorView(m,i) * v; 556 565 return res; 557 566 } … … 562 571 utility::vector res(m.columns()); 563 572 for (size_t i=0; i<res.size(); ++i) 564 res(i) = v * vector (m,i,false);573 res(i) = v * vectorView(m,i,false); 565 574 return res; 566 575 } -
trunk/yat/utility/matrix.h
r1000 r1009 29 29 30 30 #include "vector.h" 31 #include "vectorView.h" 31 32 #include "Exception.h" 32 33 … … 141 142 ~matrix(void); 142 143 144 /// 145 /// Set all elements to \a value. 146 /// 147 void all(const double value); 148 143 149 /** 144 150 \brief Make a copy of \a other. … … 148 154 */ 149 155 const matrix& clone(const matrix& other); 156 157 /** 158 */ 159 vectorView column_vec(size_t); 160 161 /** 162 */ 163 const vectorView column_vec(size_t) const; 150 164 151 165 /// … … 216 230 size_t rows(void) const; 217 231 218 /// 219 /// Set all elements to \a value. 220 /// 221 void all(const double value); 222 223 /** 224 \brief Set \a column values to values in \a vec. 225 226 \note No check on size is done. 227 228 \throw GSL_error if index is out of range or mis-match in 229 sizes. 230 */ 231 void column(const size_t column, const vector& vec); 232 233 /** 234 \brief Set \a row values to values in \a vec. 235 236 \note No check on size is done. 237 238 \throw GSL_error if index is out of range or mis-match in 239 sizes. 240 */ 241 void row(const size_t row, const vector& vec); 232 /** 233 */ 234 vectorView row_vec(size_t); 235 236 /** 237 */ 238 const vectorView row_vec(size_t) const; 242 239 243 240 /** -
trunk/yat/utility/vector.cc
r1004 r1009 44 44 45 45 vector::vector(void) 46 : v _(NULL), view_(NULL), view_const_(NULL), proxy_v_(NULL)46 : vectorBase() 47 47 { 48 48 } … … 50 50 51 51 vector::vector(size_t n, double init_value) 52 : v_(gsl_vector_alloc(n)), view_(NULL), view_const_(NULL), 53 proxy_v_(v_) 54 { 55 if (!v_) 52 : vectorBase(gsl_vector_alloc(n)) 53 { 54 if (!vec_) 56 55 throw utility::GSL_error("vector::vector failed to allocate memory"); 57 56 assert(const_vec_); 58 57 all(init_value); 59 58 } … … 61 60 62 61 vector::vector(const vector& other) 63 : v_(other.create_gsl_vector_copy()), view_(NULL), 64 view_const_(NULL), proxy_v_(v_) 65 { 66 } 67 68 69 vector::vector(vector& v, size_t offset, size_t n, size_t stride) 70 : view_const_(NULL) 71 { 72 view_ = new gsl_vector_view(gsl_vector_subvector_with_stride(v.v_,offset, 73 stride,n)); 74 if (!view_) 75 throw utility::GSL_error("vector::vector failed to setup view"); 76 proxy_v_ = v_ = &(view_->vector); 77 } 78 79 80 vector::vector(const vector& v, size_t offset, size_t n, size_t stride) 81 : v_(NULL), view_(NULL) 82 { 83 view_const_ = new gsl_vector_const_view( 84 gsl_vector_const_subvector_with_stride(v.v_,offset,stride,n)); 85 if (!view_const_) 86 throw utility::GSL_error("vector::vector failed to setup view"); 87 proxy_v_ = &(view_const_->vector); 88 } 89 90 91 vector::vector(matrix& m, size_t i, bool row) 92 : view_const_(NULL) 93 { 94 view_=new gsl_vector_view(row ? 95 gsl_matrix_row (m.gsl_matrix_p(),i) : 96 gsl_matrix_column(m.gsl_matrix_p(),i) ); 97 if (!view_) 98 throw utility::GSL_error("vector::vector failed to setup view"); 99 proxy_v_ = v_ = &(view_->vector); 100 } 101 102 103 vector::vector(const matrix& m, size_t i, bool row) 104 : v_(NULL), view_(NULL) 105 { 106 view_const_ = new gsl_vector_const_view(row ? 107 gsl_matrix_const_row (m.gsl_matrix_p(),i) : 108 gsl_matrix_const_column(m.gsl_matrix_p(),i) ); 109 if (!view_const_) 110 throw utility::GSL_error("vector::vector failed to setup view"); 111 proxy_v_ = &(view_const_->vector); 62 : vectorBase(create_gsl_vector_copy(other)) 63 { 64 } 65 66 67 vector::vector(const vectorBase& other) 68 : vectorBase(create_gsl_vector_copy(other)) 69 { 112 70 } 113 71 … … 115 73 vector::vector(std::istream& is, char sep) 116 74 throw (utility::IO_error, std::exception) 117 : v iew_(NULL), view_const_(NULL)75 : vectorBase() 118 76 { 119 77 // read the data file and store in stl vectors (dynamically … … 178 136 is.clear(std::ios::goodbit); 179 137 // convert the data to a gsl vector 180 proxy_v_ = v_ = gsl_vector_alloc(nof_rows*nof_columns);181 if (!v _)138 vec_ = gsl_vector_alloc(nof_rows*nof_columns); 139 if (!vec_) 182 140 throw utility::GSL_error("vector::vector failed to allocate memory"); 183 141 size_t n=0; … … 186 144 for (size_t i=0; i<nof_rows; i++) 187 145 for (size_t j=0; j<nof_columns; j++) 188 gsl_vector_set( v_, n++, data_matrix[i][j] ); 146 gsl_vector_set( vec_, n++, data_matrix[i][j] ); 147 const_vec_ = vec_; 189 148 } 190 149 … … 196 155 197 156 198 vector::iterator vector::begin(void) 199 { 200 return vector::iterator(*this, 0); 201 } 202 203 204 vector::const_iterator vector::begin(void) const 205 { 206 return vector::const_iterator(*this, 0); 207 } 208 209 210 const vector& vector::clone(const vector& other) 211 { 212 if (this!=&other) { 213 214 delete_allocated_memory(); 215 216 if (other.view_) { 217 view_ = new gsl_vector_view(*other.view_); 218 proxy_v_ = v_ = &(view_->vector); 219 } 220 else if (other.view_const_) { 221 view_const_ = new gsl_vector_const_view(*other.view_const_); 222 proxy_v_ = &(view_const_->vector); 223 v_=NULL; 224 } 225 else if (other.v_) 226 proxy_v_ = v_ = other.create_gsl_vector_copy(); 227 157 gsl_vector* vector::create_gsl_vector_copy(const vectorBase& other) const 158 { 159 gsl_vector* vec = gsl_vector_alloc(other.size()); 160 if (!vec) 161 throw utility::GSL_error("vector::create_gsl_vector_copy failed to allocate memory"); 162 if (gsl_vector_memcpy(vec, other.gsl_vector_p())) 163 throw utility::GSL_error("vector::create_gsl_matrix_copy dimension mis-match"); 164 return vec; 165 } 166 167 168 void vector::delete_allocated_memory(void) 169 { 170 if (vec_) 171 gsl_vector_free(vec_); 172 const_vec_ = vec_ = NULL; 173 } 174 175 176 bool vector::isview(void) const 177 { 178 return false; 179 } 180 181 182 void vector::resize(size_t n, double init_value) 183 { 184 delete_allocated_memory(); 185 const_vec_ = vec_ = gsl_vector_alloc(n); 186 if (!vec_) 187 throw utility::GSL_error("vector::vector failed to allocate memory"); 188 all(init_value); 189 190 } 191 192 193 /* 194 //Peter use swap idiom 195 const vectorBase& vector::operator=( const vectorBase& other ) 196 { 197 if (!other.size()) 198 vec_=NULL; 199 else { 200 if (size()!=other.size()) 201 resize(other.size(),0.0); 202 gsl_vector_memcpy(vec_,other.gsl_vector_p()); 228 203 } 229 204 return *this; 230 205 } 231 232 233 gsl_vector* vector::create_gsl_vector_copy(void) const 234 { 235 gsl_vector* vec = gsl_vector_alloc(size()); 236 if (!vec) 237 throw utility::GSL_error("vector::create_gsl_vector_copy failed to allocate memory"); 238 if (gsl_vector_memcpy(vec, proxy_v_)) 239 throw utility::GSL_error("vector::create_gsl_matrix_copy dimension mis-match"); 240 return vec; 241 } 242 243 244 void vector::delete_allocated_memory(void) 245 { 246 if (view_) 247 delete view_; 248 else if (view_const_) 249 delete view_const_; 250 else if (v_) 251 gsl_vector_free(v_); 252 proxy_v_=v_=NULL; 253 } 254 255 256 void vector::div(const vector& other) 257 { 258 assert(v_); 259 int status=gsl_vector_div(v_,other.gsl_vector_p()); 260 if (status) 261 throw utility::GSL_error(std::string("vector::div",status)); 262 } 263 264 265 vector::iterator vector::end(void) 266 { 267 return vector::iterator(*this, size()); 268 } 269 270 271 vector::const_iterator vector::end(void) const 272 { 273 return vector::const_iterator(*this, size()); 274 } 275 276 277 bool vector::equal(const vector& other, const double d) const 278 { 279 if (this==&other) 280 return true; 281 if (size()!=other.size()) 282 return false; 283 // if gsl error handler disabled, out of bounds index will not 284 // abort the program. 285 for (size_t i=0; i<size(); ++i) 286 // The two last condition checks are needed for NaN detection 287 if (fabs( (*this)(i)-other(i) ) > d || 288 (*this)(i)!=(*this)(i) || other(i)!=other(i)) 289 return false; 290 return true; 291 } 292 293 294 const gsl_vector* vector::gsl_vector_p(void) const 295 { 296 return proxy_v_; 297 } 298 299 300 gsl_vector* vector::gsl_vector_p(void) 301 { 302 return v_; 303 } 304 305 306 bool vector::isview(void) const 307 { 308 return view_ || view_const_; 309 } 310 311 312 void vector::mul(const vector& other) 313 { 314 assert(v_); 315 int status=gsl_vector_mul(v_,other.gsl_vector_p()); 316 if (status) 317 throw utility::GSL_error(std::string("vector::mul",status)); 318 } 319 320 321 void vector::resize(size_t n, double init_value) 322 { 323 delete_allocated_memory(); 324 proxy_v_ = v_ = gsl_vector_alloc(n); 325 if (!v_) 326 throw utility::GSL_error("vector::vector failed to allocate memory"); 327 all(init_value); 328 } 329 330 331 void vector::reverse(void) 332 { 333 assert(v_); 334 gsl_vector_reverse(v_); 335 } 336 337 338 void vector::all(const double& value) 339 { 340 assert(v_); 341 gsl_vector_set_all(v_,value); 342 } 343 344 345 size_t vector::size(void) const 346 { 347 if (!proxy_v_) 348 return 0; 349 return proxy_v_->size; 350 } 351 352 353 void vector::swap(size_t i, size_t j) 354 { 355 assert(v_); 356 int status=gsl_vector_swap_elements(v_, i, j); 357 if (status) 358 throw utility::GSL_error(std::string("vector::swap_elements",status)); 359 } 360 361 362 double& vector::operator()(size_t i) 363 { 364 assert(v_); 365 double* d=gsl_vector_ptr(v_, i); 366 if (!d) 367 throw utility::GSL_error("vector::operator()",GSL_EINVAL); 368 return *d; 369 } 370 371 372 const double& vector::operator()(size_t i) const 373 { 374 const double* d=gsl_vector_const_ptr(proxy_v_, i); 375 if (!d) 376 throw utility::GSL_error("vector::operator()",GSL_EINVAL); 377 return *d; 378 } 379 380 381 double& vector::operator[](size_t i) 382 { 383 return this->operator()(i); 384 } 385 386 387 const double& vector::operator[](size_t i) const 388 { 389 return this->operator()(i); 390 } 391 392 393 bool vector::operator==(const vector& other) const 394 { 395 return equal(other); 396 } 397 398 399 bool vector::operator!=(const vector& other) const 400 { 401 return !equal(other); 402 } 403 404 405 double vector::operator*( const vector &other ) const 406 { 407 double res = 0.0;; 408 for ( size_t i = 0; i < size(); ++i ) 409 res += other(i) * (*this)(i); 410 return res; 411 } 412 413 414 const vector& vector::operator=( const vector& other ) 415 { 416 assert(v_); 417 if (gsl_vector_memcpy(v_,other.gsl_vector_p())) 418 throw utility::GSL_error("vector::set dimension mis-match"); 206 */ 207 208 const vectorBase& vector::operator=( const vector& other ) 209 { 210 return assign(other); 211 } 212 213 /* 214 const vectorBase& vector::assign(vectorBase& other) 215 { 216 assign(other); 217 } 218 */ 219 220 const vectorBase& vector::assign(const vectorBase& other) 221 { 222 if (!other.size()) 223 vec_=NULL; 224 else { 225 if (size()!=other.size()) 226 resize(other.size(),0.0); 227 gsl_vector_memcpy(vec_,other.gsl_vector_p()); 228 } 229 const_vec_ = vec_; 419 230 return *this; 420 231 } 421 232 422 233 423 const vector& vector::operator+=(const vector& other) 424 { 425 assert(v_); 426 int status=gsl_vector_add(v_, other.gsl_vector_p()); 427 if (status) 428 throw utility::GSL_error(std::string("vector::add", status)); 429 return *this; 430 } 431 432 433 const vector& vector::operator+=(double d) 434 { 435 assert(v_); 436 gsl_vector_add_constant(v_, d); 437 return *this; 438 } 439 440 441 const vector& vector::operator-=(const vector& other) 442 { 443 assert(v_); 444 int status=gsl_vector_sub(v_, other.gsl_vector_p()); 445 if (status) 446 throw utility::GSL_error(std::string("vector::sub", status)); 447 return *this; 448 } 449 450 451 const vector& vector::operator-=(const double d) 452 { 453 assert(v_); 454 gsl_vector_add_constant(v_, -d); 455 return *this; 456 } 457 458 459 const vector& vector::operator*=(const double d) 460 { 461 assert(v_); 462 gsl_vector_scale(v_, d); 463 return *this; 464 } 465 466 467 bool isnull(const vector& v) 468 { 469 return gsl_vector_isnull(v.gsl_vector_p()); 470 } 471 472 473 double max(const vector& v) 474 { 475 return gsl_vector_max(v.gsl_vector_p()); 476 } 477 478 479 size_t max_index(const vector& v) 480 { 481 return gsl_vector_max_index(v.gsl_vector_p()); 482 } 483 484 485 double min(const vector& v) 486 { 487 return gsl_vector_min(v.gsl_vector_p()); 488 } 489 490 491 size_t min_index(const vector& v) 492 { 493 return gsl_vector_min_index(v.gsl_vector_p()); 494 } 495 496 497 bool nan(const vector& templat, vector& flag) 498 { 499 size_t vsize=templat.size(); 500 if (vsize!=flag.size()) 501 flag.clone(vector(vsize,1.0)); 502 else 503 flag.all(1.0); 504 bool nan=false; 505 for (size_t i=0; i<vsize; i++) 506 if (std::isnan(templat(i))) { 507 flag(i)=0; 508 nan=true; 509 } 510 return nan; 511 } 512 513 234 <<<<<<< .working 514 235 void set_basis(vector& v, size_t i) 515 236 { … … 519 240 520 241 521 void shuffle(vector& invec)522 {523 random::random_shuffle(invec.begin(), invec.end());524 }525 526 527 242 void sort(vector& v) 528 243 { 529 244 assert(v.gsl_vector_p()); 530 245 std::sort(v.begin(), v.end()); 531 }532 533 534 void sort_index(std::vector<size_t>& sort_index, const vector& invec)535 {536 assert(invec.gsl_vector_p());537 gsl_permutation* p = gsl_permutation_alloc(invec.size());538 int status=gsl_sort_vector_index(p,invec.gsl_vector_p());539 if (status) {540 gsl_permutation_free(p);541 throw utility::GSL_error(std::string("sort_index(vector&,const vector&)",status));542 }543 sort_index=std::vector<size_t>(p->data,p->data+p->size);544 gsl_permutation_free(p);545 }546 547 548 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k,549 const vector& invec)550 {551 assert(invec.gsl_vector_p());552 assert(k<=invec.size());553 sort_index.resize(k);554 gsl_sort_vector_smallest_index(&sort_index[0],k,invec.gsl_vector_p());555 }556 557 void sort_largest_index(std::vector<size_t>& sort_index, size_t k,558 const vector& invec)559 {560 assert(invec.gsl_vector_p());561 assert(k<=invec.size());562 sort_index.resize(k);563 gsl_sort_vector_largest_index(&sort_index[0],k,invec.gsl_vector_p());564 }565 566 567 double sum(const vector& v)568 {569 double sum = 0;570 size_t vsize=v.size();571 for (size_t i=0; i<vsize; ++i)572 sum += v[i];573 return sum;574 246 } 575 247 … … 584 256 585 257 586 std::ostream& operator<<(std::ostream& s, const vector& a)587 {588 s.setf(std::ios::dec);589 s.precision(12);590 for (size_t j = 0; j < a.size(); ++j) {591 s << a[j];592 if ( (j+1)<a.size() )593 s << s.fill();594 }595 return s;596 }597 598 258 }}} // of namespace utility, yat, and thep -
trunk/yat/utility/vector.h
r1000 r1009 29 29 */ 30 30 31 #include "vectorBase.h" 31 32 #include "Exception.h" 32 33 #include "Iterator.h" … … 84 85 */ 85 86 86 class vector 87 class vector : public vectorBase 87 88 { 88 89 public: 89 90 /// \brief vector::iterator91 typedef Iterator<double&, vector> iterator;92 /// \brief vector::const_iterator93 typedef Iterator<const double, const vector> const_iterator;94 90 95 91 /** … … 118 114 119 115 /** 120 \brief Vector view constructor. 121 122 Create a view of vector \a v, with starting index \a offset, 123 size \a n, and an optional \a stride. 124 125 A vector view can be used as any vector with the difference 126 that changes made to the view will also change the object that 127 is viewed. Also, using the copy constructor will create a new 128 vector object that is a copy of whatever is viewed. If a copy 129 of the view is needed then you should use this constructor to 130 obtain a copy. 131 132 \note If the object viewed by the view goes out of scope or is 133 deleted, the view becomes invalid and the result of further use 134 is undefined. 135 136 \throw GSL_error if a view cannot be set up. 137 */ 138 vector(vector& v, size_t offset, size_t n, size_t stride=1); 139 140 /** 141 \brief Vector const view constructor. 142 143 Create a view of vector \a v, with starting index \a offset, 144 size \a n, and an optional \a stride. 145 146 A vector view can be used as any const vector. Using the copy 147 constructor will create a new vector object that is a copy of 148 whatever is viewed. If a copy of the view is needed then you 149 should use this constructor to obtain a copy. 150 151 \note If the object viewed by the view goes out of scope or is 152 deleted, the view becomes invalid and the result of further use 153 is undefined. 154 155 \throw GSL_error if a view cannot be set up. 156 */ 157 vector(const vector& v, size_t offset, size_t n, size_t stride=1); 158 159 /// 160 /// Matrix row/column view constructor. 161 /// 162 /// Create a row/column vector view of matrix \a m, pointing at 163 /// row/column \a i. The parameter \a row is used to set whether 164 /// the view should be a row or column view. If \a row is set to 165 /// true, the view will be a row view (default behaviour), and, 166 /// naturally, a column view otherwise. 167 /// 168 /// A vector view can be used as any vector with the difference 169 /// that changes made to the view will also change the object that 170 /// is viewed. Also, using the copy constructor will create a new 171 /// vector object that is a copy of whatever is viewed. If a copy 172 /// of the view is needed then you should use the vector view 173 /// constructor to obtain a copy. 174 /// 175 /// @note If the object viewed by the view goes out of scope or is 176 /// deleted, the view becomes invalid and the result of further 177 /// use is undefined. 178 /// 179 vector(matrix& m, size_t i, bool row=true); 180 181 /// 182 /// Matrix row/column const view constructor. 183 /// 184 /// Create a row/column vector view of matrix \a m, pointing at 185 /// row/column \a i. The parameter \a row is used to set whether 186 /// the view should be a row or column view. If \a row is set to 187 /// true, the view will be a row view (default behaviour), and, 188 /// naturally, a column view otherwise. 189 /// 190 /// A const vector view can be used as any const vector. Using the 191 /// copy constructor will create a new vector object that is a 192 /// copy of whatever is viewed. If a copy of the view is needed 193 /// then you should use the vector view constructor to obtain a 194 /// copy. 195 /// 196 /// @note If the object viewed by the view goes out of scope or is 197 /// deleted, the view becomes invalid and the result of further 198 /// use is undefined. 199 /// 200 vector(const matrix& m, size_t i, bool row=true); 116 \brief The copy constructor. 117 118 \note If the object to be copied is a vector view, the values 119 of the view will be copied, i.e. the view is not copied. 120 121 \throw A GSL_error is indirectly thrown if memory allocation 122 fails. 123 */ 124 vector(const vectorBase& other); 201 125 202 126 /** … … 221 145 222 146 /** 223 \return mutable iterator to start of vector 224 */ 225 iterator begin(void); 226 227 /** 228 \return read-only iterator to start of vector 229 */ 230 const_iterator begin(void) const; 231 232 /** 233 \brief Make a copy of \a other. 234 235 This function will make a deep copy of \a other. Memory is 236 resized and view state is changed if needed. 237 */ 238 const vector& clone(const vector& other); 239 240 /** 241 \brief This function performs element-wise division, \f$ this_i = 242 this_i/other_i \; \forall i \f$. 243 244 \throw GSL_error if dimensions mis-match. 245 */ 246 void div(const vector& other); 247 248 /** 249 \return mutable iterator to end of vector 250 */ 251 iterator end(void); 252 253 /** 254 \return read-only iterator to end of vector 255 */ 256 const_iterator end(void) const; 257 258 /** 259 \brief Check whether vectors are equal within a user defined 260 precision, set by \a precision. 261 262 \return True if each element deviates less or equal than \a 263 d. If any vector contain a NaN, false is always returned. 264 265 \see operator== and operator!= 266 */ 267 bool equal(const vector&, const double precision=0) const; 268 269 /// 270 /// @return A const pointer to the internal GSL vector, 271 /// 272 const gsl_vector* gsl_vector_p(void) const; 273 274 /// 275 /// @return A pointer to the internal GSL vector, 276 /// 277 gsl_vector* gsl_vector_p(void); 278 279 /// 280 /// Check if the vector object is a view (sub-vector) to another 281 /// vector. 282 /// 283 /// @return True if the object is a view, false othwerwise. 284 /// 285 bool isview(void) const; 286 287 /** 288 \brief This function performs element-wise multiplication, \f$ 289 this_i = this_i * other_i \; \forall i \f$. 290 291 \throw GSL_error if dimensions mis-match. 292 */ 293 void mul(const vector& other); 147 \return false 148 */ 149 bool isview(void) const; 294 150 295 151 /** … … 301 157 vector becomes invalid. 302 158 */ 303 void resize(size_t, double init_value=0); 304 305 /** 306 \brief Reverse the order of elements in the vector. 307 */ 308 void reverse(void); 309 310 /// 311 /// Set all elements to \a value. 312 /// 313 void all(const double& value); 314 315 /// 316 /// @return the number of elements in the vector. 317 /// 318 size_t size(void) const; 319 320 /** 321 \brief Exchange elements \a i and \a j. 322 323 \throw GSL_error if vector lengths differs. 324 */ 325 void swap(size_t i, size_t j); 326 327 /** 328 \brief Element access operator. 329 330 \return Reference to element \a i. 331 332 \throw If GSL range checks are enabled in the underlying GSL 333 library a GSL_error exception is thrown if either index is out 334 of range. 335 */ 336 double& operator()(size_t i); 337 338 /** 339 \brief Element access operator. 340 341 \return Const reference to element \a i. 342 343 \throw If GSL range checks are enabled in the underlying GSL 344 library a GSL_error exception is thrown if either index is out 345 of range. 346 */ 347 const double& operator()(size_t i) const; 348 349 /// 350 /// Element access operator. 351 /// 352 /// @return Reference to element \a i. 353 /// 354 double& operator[](size_t i); 355 356 /// 357 /// Const element access operator. 358 /// 359 /// @return The value of element \a i. 360 /// 361 const double& operator[](size_t i) const; 362 363 /** 364 \brief Comparison operator. Takes linear time. 365 366 Checks are performed with exact matching, i.e., rounding off 367 effects may destroy comparison. Use the equal function for 368 comparing elements within a user defined precision. 369 370 \return True if all elements are equal otherwise false. 371 372 \see equal(const vector&, const double precision=0) 373 */ 374 bool operator==(const vector&) const; 375 376 /** 377 \brief Comparison operator. Takes linear time. 378 379 Checks are performed with exact matching, i.e., rounding off 380 effects may destroy comparison. Use the equal function for 381 comparing elements within a user defined precision. 382 383 \return False if all elements are equal otherwise true. 384 385 \see equal(const vector&, const double precision=0) 386 */ 387 bool operator!=(const vector&) const; 388 389 /// 390 /// @return The dot product. 391 /// 392 double operator*(const vector&) const; 159 void resize(size_t n, double init_value); 160 161 // using vectorBase::operator=; 393 162 394 163 /** 395 164 \brief The assignment operator. 396 165 397 Dimensions of the vectors must match. If the LHS vector is a398 view, the underlying data will be changed.399 400 166 \return A const reference to the resulting vector. 401 402 \see void set(const vector&). 403 404 \throw GSL_error if dimensions mis-match. 405 */ 406 const vector& operator=(const vector&); 407 408 /** 409 \brief Addition and assign operator. Vector addition, \f$ 410 this_i = this_i + other_i \; \forall i \f$. 411 412 \return A const reference to the resulting vector. 413 414 \throw GSL_error if dimensions mis-match. 415 */ 416 const vector& operator+=(const vector&); 417 418 /** 419 \brief Add a constant to a vector, \f$ this_i = this_i + d \; 420 \forall i \f$. 421 422 \return A const reference to the resulting vector. 423 */ 424 const vector& operator+=(double d); 425 426 /** 427 \brief Subtract and assign operator. Vector subtraction, \f$ 428 this_i = this_i - other_i \; \forall i \f$. 429 430 \return A const reference to the resulting vector. 431 432 \throw GSL_error if dimensions mis-match. 433 */ 434 const vector& operator-=(const vector&); 435 436 /** 437 \brief Subtract a constant to a vector, \f$ this_i = this_i - d 438 \; \forall i \f$. 439 440 \return A const reference to the resulting vector. 441 */ 442 const vector& operator-=(double d); 443 444 /** 445 \brief Multiply with scalar and assign operator, \f$ this_i = 446 this_i * d \; \forall i \f$. 447 448 \return A const reference to the resulting vector. 449 */ 450 const vector& operator*=(double d); 451 167 */ 168 //const vectorBase& operator=(const vectorBase&); 169 const vectorBase& operator=(const vector&); 452 170 453 171 private: 172 const vectorBase& assign(const vectorBase& other); 173 //const vectorBase& assign(vectorBase& other); 454 174 455 175 /** … … 464 184 copy, or if dimensions mis-match. 465 185 */ 466 gsl_vector* create_gsl_vector_copy(void) const; 467 468 /** 469 \brief Clear all dynamically allocated memory. 470 471 Internal utility function. 472 */ 186 gsl_vector* create_gsl_vector_copy(const vectorBase&) const; 187 473 188 void delete_allocated_memory(void); 474 475 gsl_vector* v_;476 gsl_vector_view* view_;477 const gsl_vector_const_view* view_const_;478 // proxy_v_ is used to access the proper underlying gsl_vector479 // in all const member functions. It is not used by design for480 // non-const vector functions and operators. This is to make sure481 // that runtime errors occur if a const vector is used in an482 // inappropriate manner such as on left hand side in assignment483 // (remember, v_ is null for const vector views).484 const gsl_vector* proxy_v_;485 189 }; 486 487 /**488 \brief Check if all elements of the vector are zero.489 490 \return True if all elements in the vector is zero, false491 othwerwise.492 */493 bool isnull(const vector&);494 495 /**496 \brief Get the maximum value of the vector.497 498 \return The maximum value of the vector.499 */500 double max(const vector&);501 502 /**503 \brief Locate the maximum value in the vector.504 505 \return The index to the maximum value of the vector.506 507 \note Lower index has precedence.508 */509 size_t max_index(const vector&);510 511 /**512 \brief Get the minimum value of the vector.513 514 \return The minimum value of the vector.515 */516 double min(const vector&);517 518 /**519 \brief Locate the minimum value in the vector.520 521 \return The index to the minimum value of the vector.522 523 \note Lower index has precedence.524 */525 size_t min_index(const vector&);526 527 /**528 \brief Create a vector \a flag indicating NaN's in another vector529 \a templat.530 531 The \a flag vector is changed to contain 1's and 0's only. A 1532 means that the corresponding element in the \a templat vector is533 valid and a zero means that the corresponding element is a NaN.534 535 \note Space for vector \a flag is reallocated to fit the size of536 vector \a templat if sizes mismatch.537 538 \return True if the \a templat vector contains at least one NaN.539 */540 bool nan(const vector& templat, vector& flag);541 542 /**543 \brief Transforms a vector to a basis vector.544 545 All elements are set to zero except the \a i-th element which is546 set to one.547 */548 void set_basis(vector&, size_t i);549 550 /**551 Randomly shuffles the elements in vector \a invec552 */553 void shuffle(vector& invec);554 555 /**556 Sort the elements in the vector.557 */558 void sort(vector&);559 560 /**561 Create a vector \a sort_index containing the indeces of562 elements in a another vector \a invec. The elements of \a563 sort_index give the index of the vector element which would564 have been stored in that position if the vector had been sorted565 in place. The first element of \a sort_index gives the index of the least566 element in \a invec, and the last element of \a sort_index gives the index of the567 greatest element in \a invec . The vector \a invec is not changed.568 569 */570 void sort_index(std::vector<size_t>& sort_index, const vector& invec);571 572 /** Similar to sort_index but creates a vector with indices to the \a k573 smallest elements in \a invec.574 */575 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, const576 vector& invec);577 578 /** Similar to sort_index but creates a vector with indices to the \a k579 largest elements in \a invec.580 */581 void sort_largest_index(std::vector<size_t>& sort_index, size_t k, const582 vector& invec);583 584 585 586 /**587 \brief Calculate the sum of all vector elements.588 589 \return The sum.590 */591 double sum(const vector&);592 190 593 191 /** … … 600 198 void swap(vector&, vector&); 601 199 602 /**603 \brief The output operator for the vector class.604 */605 std::ostream& operator<<(std::ostream&, const vector&);606 607 200 }}} // of namespace utility, yat, and theplu 608 201
Note: See TracChangeset
for help on using the changeset viewer.