Changeset 995 for branches/peter-dev
- Timestamp:
- Nov 30, 2007, 11:55:30 PM (16 years ago)
- Location:
- branches/peter-dev
- Files:
-
- 14 edited
- 8 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/peter-dev/test/vector_test.cc
r988 r995 34 34 35 35 using namespace theplu::yat; 36 36 37 37 38 void check_file_access(std::string& str) -
branches/peter-dev/yat/classifier/NBC.cc
r963 r995 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(prediction.row_vec(i)); 210 } 211 211 } 212 212 -
branches/peter-dev/yat/classifier/SVM.cc
r964 r995 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 -
branches/peter-dev/yat/classifier/utility.cc
r865 r995 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); -
branches/peter-dev/yat/regression/Local.cc
r865 r995 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 -
branches/peter-dev/yat/regression/MultiDimensional.cc
r865 r995 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_); -
branches/peter-dev/yat/regression/MultiDimensionalWeighted.cc
r916 r995 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_); -
branches/peter-dev/yat/statistics/AveragerPairWeighted.h
r937 r995 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 } -
branches/peter-dev/yat/utility/Makefile.am
r981 r995 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 vectorConstView.cc vectorMutable.cc vectorView.cc WeNNI.cc Peter.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 vectorMutable.h vectorView.h vectorConstView.h 42 WeNNI.h yat_assert.h 41 43 -
branches/peter-dev/yat/utility/PCA.cc
r865 r995 29 29 #include "SVD.h" 30 30 #include "utility.h" 31 #include "vectorConstView.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 vectorConstView tmp; 196 tmp = A_.const_row(i); 197 A_row_sum(i) = sum(tmp); 198 } 195 199 for( size_t i = 0; i < A_center.rows(); ++i ) { 196 meanvalues_ [i]= A_row_sum(i) / static_cast<double>(A_.columns());200 meanvalues_(i) = A_row_sum(i) / static_cast<double>(A_.columns()); 197 201 for( size_t j = 0; j < A_center.columns(); ++j ) 198 202 A_center(i,j) = A_(i,j) - meanvalues_(i); -
branches/peter-dev/yat/utility/matrix.cc
r916 r995 27 27 #include "matrix.h" 28 28 #include "vector.h" 29 #include "vectorView.h" 30 #include "vectorConstView.h" 29 31 #include "utility.h" 30 32 … … 302 304 303 305 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)); 306 vectorView matrix::column_vec(size_t col) 307 { 308 vectorView res(*this, col, false); 309 return res; 310 } 311 312 313 vectorConstView matrix::const_column(size_t col) const 314 { 315 return vectorConstView(*this, col, false); 316 } 317 318 319 vectorConstView matrix::const_row(size_t col) const 320 { 321 return vectorConstView(*this, col, true); 322 } 323 324 325 vectorView matrix::row_vec(size_t row) 326 { 327 vectorView res(*this, row, true); 328 return res; 319 329 } 320 330 … … 549 559 550 560 551 vector operator*(const matrix& m, const vector & v)561 vector operator*(const matrix& m, const vectorBase& v) 552 562 { 553 563 utility::vector res(m.rows()); 554 564 for (size_t i=0; i<res.size(); ++i) 555 res(i) = vector (m,i) * v;565 res(i) = vectorConstView(m,i) * v; 556 566 return res; 557 567 } … … 562 572 utility::vector res(m.columns()); 563 573 for (size_t i=0; i<res.size(); ++i) 564 res(i) = v * vector (m,i,false);574 res(i) = v * vectorConstView(m,i,false); 565 575 return res; 566 576 } -
branches/peter-dev/yat/utility/matrix.h
r865 r995 29 29 30 30 #include "vector.h" 31 #include "vectorView.h" 32 #include "vectorConstView.h" 31 33 #include "Exception.h" 32 34 … … 141 143 ~matrix(void); 142 144 145 /// 146 /// Set all elements to \a value. 147 /// 148 void all(const double value); 149 143 150 /** 144 151 \brief Make a copy of \a other. … … 149 156 const matrix& clone(const matrix& other); 150 157 158 /** 159 */ 160 vectorView column_vec(size_t); 161 151 162 /// 152 163 /// @return The number of columns in the matrix. 153 164 /// 154 165 size_t columns(void) const; 166 167 /** 168 */ 169 vectorConstView const_column(size_t) const; 170 171 /** 172 */ 173 vectorConstView const_row(size_t) const; 155 174 156 175 /** … … 216 235 size_t rows(void) const; 217 236 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); 237 /** 238 */ 239 vectorView row_vec(size_t); 242 240 243 241 /** -
branches/peter-dev/yat/utility/vector.cc
r978 r995 44 44 45 45 vector::vector(void) 46 : v _(NULL), view_(NULL), view_const_(NULL), proxy_v_(NULL)46 : vectorMutable(NULL) 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 : vectorMutable(gsl_vector_alloc(n)) 53 { 54 if (!vec_) 56 55 throw utility::GSL_error("vector::vector failed to allocate memory"); 57 56 … … 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 : vectorMutable(create_gsl_vector_copy(other)) 63 { 64 } 65 66 67 vector::vector(const vectorBase& other) 68 : vectorMutable(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 : vectorMutable(NULL) 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] ); 189 147 } 190 148 … … 192 150 vector::~vector(void) 193 151 { 194 delete_allocated_memory();195 } 196 197 198 vector::iterator vector::begin(void)199 { 200 return vector::iterator(*this, 0);201 }202 203 204 vector::const_iterator vector::begin(void) const205 {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 152 gsl_vector_free(vec_); 153 } 154 155 156 gsl_vector* vector::create_gsl_vector_copy(const vectorBase& other) const 157 { 158 gsl_vector* vec = gsl_vector_alloc(other.size()); 159 if (!vec) 160 throw utility::GSL_error("vector::create_gsl_vector_copy failed to allocate memory"); 161 if (gsl_vector_memcpy(vec, other.gsl_vector_p())) 162 throw utility::GSL_error("vector::create_gsl_matrix_copy dimension mis-match"); 163 return vec; 164 } 165 166 167 void vector::resize(size_t n, double init_value) 168 { 169 gsl_vector_free(vec_); 170 vec_ = gsl_vector_alloc(n); 171 if (!vec_) 172 throw utility::GSL_error("vector::vector failed to allocate memory"); 173 all(init_value); 174 } 175 176 177 //Peter use swap idiom 178 const vector& vector::operator=( const vectorBase& other ) 179 { 180 if (!other.size()) 181 vec_=NULL; 182 else { 183 if (size()!=other.size()) 184 resize(other.size(),0.0); 185 gsl_vector_memcpy(vec_,other.gsl_vector_p()); 228 186 } 229 187 return *this; … … 231 189 232 190 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"); 191 //Peter use swap idiom 192 const vectorMutable& vector::operator=( vectorMutable& other ) 193 { 194 if (!other.size()) 195 vec_=NULL; 196 else { 197 if (size()!=other.size()) 198 resize(other.size(),0.0); 199 gsl_vector_memcpy(vec_,other.gsl_vector_p()); 200 } 419 201 return *this; 420 202 } 421 422 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 else503 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 514 void set_basis(vector& v, size_t i)515 {516 assert(v.gsl_vector_p());517 gsl_vector_set_basis(v.gsl_vector_p(),i);518 }519 520 521 void shuffle(vector& invec)522 {523 random::DiscreteUniform rnd;524 std::random_shuffle(invec.begin(), invec.end(), rnd);525 }526 527 528 void sort(vector& v)529 {530 assert(v.gsl_vector_p());531 std::sort(v.begin(), v.end());532 }533 534 535 void sort_index(std::vector<size_t>& sort_index, const vector& invec)536 {537 assert(invec.gsl_vector_p());538 gsl_permutation* p = gsl_permutation_alloc(invec.size());539 int status=gsl_sort_vector_index(p,invec.gsl_vector_p());540 if (status) {541 gsl_permutation_free(p);542 throw utility::GSL_error(std::string("sort_index(vector&,const vector&)",status));543 }544 sort_index=std::vector<size_t>(p->data,p->data+p->size);545 gsl_permutation_free(p);546 }547 548 549 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k,550 const vector& invec)551 {552 assert(invec.gsl_vector_p());553 assert(k<=invec.size());554 sort_index.resize(k);555 gsl_sort_vector_smallest_index(&sort_index[0],k,invec.gsl_vector_p());556 }557 558 void sort_largest_index(std::vector<size_t>& sort_index, size_t k,559 const vector& invec)560 {561 assert(invec.gsl_vector_p());562 assert(k<=invec.size());563 sort_index.resize(k);564 gsl_sort_vector_largest_index(&sort_index[0],k,invec.gsl_vector_p());565 }566 567 568 double sum(const vector& v)569 {570 double sum = 0;571 size_t vsize=v.size();572 for (size_t i=0; i<vsize; ++i)573 sum += v[i];574 return sum;575 }576 203 577 204 … … 585 212 586 213 587 std::ostream& operator<<(std::ostream& s, const vector& a)588 {589 s.setf(std::ios::dec);590 s.precision(12);591 for (size_t j = 0; j < a.size(); ++j) {592 s << a[j];593 if ( (j+1)<a.size() )594 s << s.fill();595 }596 return s;597 }598 599 214 }}} // of namespace utility, yat, and thep -
branches/peter-dev/yat/utility/vector.h
r966 r995 29 29 */ 30 30 31 #include "vectorMutable.h" 31 32 #include "Exception.h" 32 33 #include "Iterator.h" … … 84 85 */ 85 86 86 class vector 87 class vector : public vectorMutable 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 viewconstructor.116 \brief The copy constructor. 121 117 122 Create a view of vector \a v, with starting index \a offset,123 size \a n, and an optional \a stride.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. 124 120 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. 121 \throw A GSL_error is indirectly thrown if memory allocation 122 fails. 137 123 */ 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); 124 vector(const vectorBase& other); 201 125 202 126 /** … … 221 145 222 146 /** 223 \return mutable iterator to start of vector224 */225 iterator begin(void);226 227 /**228 \return read-only iterator to start of vector229 */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 is236 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 vector250 */251 iterator end(void);252 253 /**254 \return read-only iterator to end of vector255 */256 const_iterator end(void) const;257 258 /**259 \brief Check whether vectors are equal within a user defined260 precision, set by \a precision.261 262 \return True if each element deviates less or equal than \a263 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 another281 /// 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);294 295 /**296 147 @brief Resize vector 297 148 … … 301 152 vector becomes invalid. 302 153 */ 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; 154 void resize(size_t n, double init_value); 393 155 394 156 /** 395 157 \brief The assignment operator. 396 158 397 Dimensions of the vectors must match. If the LHS vector is a398 view, the underlying data will be changed.399 400 \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 159 \return A const reference to the resulting vector. 423 160 */ 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 161 const vectorMutable& operator=(vectorMutable&); 162 const vector& operator=(const vectorBase&); 452 163 453 164 private: … … 464 175 copy, or if dimensions mis-match. 465 176 */ 466 gsl_vector* create_gsl_vector_copy( void) const;177 gsl_vector* create_gsl_vector_copy(const vectorBase&) const; 467 178 468 /**469 \brief Clear all dynamically allocated memory.470 471 Internal utility function.472 */473 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 179 }; 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 180 593 181 /** … … 600 188 void swap(vector&, vector&); 601 189 602 /**603 \brief The output operator for the vector class.604 */605 std::ostream& operator<<(std::ostream&, const vector&);606 607 190 }}} // of namespace utility, yat, and theplu 608 191 -
branches/peter-dev/yat/utility/vectorBase.cc
r994 r995 25 25 */ 26 26 27 #include "vector .h"27 #include "vectorBase.h" 28 28 #include "matrix.h" 29 29 #include "utility.h" … … 43 43 44 44 45 vector::vector(void) 46 : v_(NULL), view_(NULL), view_const_(NULL), proxy_v_(NULL) 47 { 48 } 49 50 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_) 56 throw utility::GSL_error("vector::vector failed to allocate memory"); 57 58 all(init_value); 59 } 60 61 62 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); 112 } 113 114 115 vector::vector(std::istream& is, char sep) 116 throw (utility::IO_error, std::exception) 117 : view_(NULL), view_const_(NULL) 118 { 119 // read the data file and store in stl vectors (dynamically 120 // expandable) 121 std::vector<std::vector<double> > data_matrix; 122 u_int nof_columns=0; 123 u_int nof_rows=0; 124 std::string line; 125 while(getline(is, line, '\n')) { 126 // Empty lines 127 if (!line.size()) 128 continue; 129 nof_rows++; 130 131 std::vector<double> v; 132 std::string element; 133 std::stringstream ss(line); 134 bool ok=true; 135 while(ok) { 136 if(sep=='\0') 137 ok=(ss>>element); 138 else 139 ok=getline(ss, element, sep); 140 if(!ok) 141 break; 142 143 if(utility::is_double(element)) { 144 v.push_back(atof(element.c_str())); 145 } 146 else if (!element.size() || utility::is_nan(element)) { 147 v.push_back(std::numeric_limits<double>::quiet_NaN()); 148 } 149 else { 150 std::stringstream ss("Warning: '"); 151 ss << element << "' is not a double."; 152 throw IO_error(ss.str()); 153 } 154 } 155 if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator 156 v.push_back(std::numeric_limits<double>::quiet_NaN()); 157 if (!nof_columns) 158 nof_columns=v.size(); 159 else if (nof_rows && (nof_columns>1)) { 160 std::ostringstream s; 161 s << "vector::vector(std::istream&) data file error:\n" 162 << " File has inconsistent number of rows (" << nof_rows 163 << ") and columns (" << nof_columns 164 << ").\n Expected a row or a column vector."; 165 throw utility::IO_error(s.str()); 166 } 167 else if (v.size()!=nof_columns) { 168 std::ostringstream s; 169 s << "vector::vector(std::istream&) data file error:\n" 170 << " Line " << nof_rows << " has " << v.size() 171 << " columns; expected " << nof_columns << " column."; 172 throw utility::IO_error(s.str()); 173 } 174 data_matrix.push_back(v); 175 } 176 177 // manipulate the state of the stream to be good 178 is.clear(std::ios::goodbit); 179 // convert the data to a gsl vector 180 proxy_v_ = v_ = gsl_vector_alloc(nof_rows*nof_columns); 181 if (!v_) 182 throw utility::GSL_error("vector::vector failed to allocate memory"); 183 size_t n=0; 184 // if gsl error handler disabled, out of bounds index will not 185 // abort the program. 186 for (size_t i=0; i<nof_rows; i++) 187 for (size_t j=0; j<nof_columns; j++) 188 gsl_vector_set( v_, n++, data_matrix[i][j] ); 189 } 190 191 192 vector::~vector(void) 193 { 194 delete_allocated_memory(); 195 } 196 197 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 228 } 229 return *this; 230 } 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 45 vectorBase::vectorBase(const gsl_vector* v) 46 : const_vec_(v) 47 { 48 } 49 50 51 vectorBase::~vectorBase(void) 52 { 53 } 54 55 56 vectorBase::const_iterator vectorBase::begin(void) const 57 { 58 return vectorBase::const_iterator(*this, 0); 59 } 60 61 62 vectorBase::const_iterator vectorBase::end(void) const 63 { 64 return vectorBase::const_iterator(*this, size()); 65 } 66 67 68 bool vectorBase::equal(const vectorBase& other, const double d) const 278 69 { 279 70 if (this==&other) … … 284 75 // abort the program. 285 76 for (size_t i=0; i<size(); ++i) 286 // The two last condition checks are needed for NaN detection287 77 if (fabs( (*this)(i)-other(i) ) > d || 288 (*this)(i)!=(*this)(i) || other(i)!=other(i))78 std::isnan((*this)(i)) || std::isnan(other(i)) ) 289 79 return false; 290 80 return true; … … 292 82 293 83 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_) 84 const gsl_vector* vectorBase::gsl_vector_p(void) const 85 { 86 return const_vec_; 87 } 88 89 90 size_t vectorBase::size(void) const 91 { 92 if (const_vec_) 348 93 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); 94 return const_vec_->size; 95 } 96 97 98 const double& vectorBase::operator()(size_t i) const 99 { 100 const double* d=gsl_vector_const_ptr(const_vec_, i); 366 101 if (!d) 367 throw utility::GSL_error("vector ::operator()",GSL_EINVAL);102 throw utility::GSL_error("vectorBase::operator()",GSL_EINVAL); 368 103 return *d; 369 104 } 370 105 371 106 372 const double& vector ::operator()(size_t i) const373 { 374 const double* d=gsl_vector_const_ptr( proxy_v_, i);107 const double& vectorBase::operator[](size_t i) const 108 { 109 const double* d=gsl_vector_const_ptr(const_vec_, i); 375 110 if (!d) 376 throw utility::GSL_error("vector ::operator()",GSL_EINVAL);111 throw utility::GSL_error("vectorBase::operator[]",GSL_EINVAL); 377 112 return *d; 378 113 } 379 114 380 115 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 116 bool vectorBase::operator==(const vectorBase& other) const 394 117 { 395 118 return equal(other); … … 397 120 398 121 399 bool vector ::operator!=(const vector& other) const122 bool vectorBase::operator!=(const vectorBase& other) const 400 123 { 401 124 return !equal(other); … … 403 126 404 127 405 double vector ::operator*( const vector&other ) const128 double vectorBase::operator*( const vectorBase &other ) const 406 129 { 407 130 double res = 0.0;; … … 412 135 413 136 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"); 419 return *this; 420 } 421 422 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) 137 bool isnull(const vectorBase& v) 468 138 { 469 139 return gsl_vector_isnull(v.gsl_vector_p()); … … 471 141 472 142 473 double max(const vector & v)143 double max(const vectorBase& v) 474 144 { 475 145 return gsl_vector_max(v.gsl_vector_p()); … … 477 147 478 148 479 size_t max_index(const vector & v)149 size_t max_index(const vectorBase& v) 480 150 { 481 151 return gsl_vector_max_index(v.gsl_vector_p()); … … 483 153 484 154 485 double min(const vector & v)155 double min(const vectorBase& v) 486 156 { 487 157 return gsl_vector_min(v.gsl_vector_p()); … … 489 159 490 160 491 size_t min_index(const vector & v)161 size_t min_index(const vectorBase& v) 492 162 { 493 163 return gsl_vector_min_index(v.gsl_vector_p()); … … 495 165 496 166 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); 167 bool nan(const vectorBase& templat, vector& flag) 168 { 169 size_t vsize(templat.size()); 170 flag = vector(vsize, 1.0); 504 171 bool nan=false; 505 172 for (size_t i=0; i<vsize; i++) … … 512 179 513 180 514 void set_basis(vector& v, size_t i) 515 { 516 assert(v.gsl_vector_p()); 517 gsl_vector_set_basis(v.gsl_vector_p(),i); 518 } 519 520 521 void shuffle(vector& invec) 522 { 523 random::DiscreteUniform rnd; 524 std::random_shuffle(invec.begin(), invec.end(), rnd); 525 } 526 527 528 void sort(vector& v) 529 { 530 assert(v.gsl_vector_p()); 531 std::sort(v.begin(), v.end()); 532 } 533 534 535 void sort_index(std::vector<size_t>& sort_index, const vector& invec) 181 void sort_index(std::vector<size_t>& sort_index, const vectorBase& invec) 536 182 { 537 183 assert(invec.gsl_vector_p()); … … 540 186 if (status) { 541 187 gsl_permutation_free(p); 542 throw utility::GSL_error(std::string("sort_index(vector&,const vector &)",status));188 throw utility::GSL_error(std::string("sort_index(vector&,const vectorBase&)",status)); 543 189 } 544 190 sort_index=std::vector<size_t>(p->data,p->data+p->size); … … 548 194 549 195 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, 550 const vector & invec)196 const vectorBase& invec) 551 197 { 552 198 assert(invec.gsl_vector_p()); … … 557 203 558 204 void sort_largest_index(std::vector<size_t>& sort_index, size_t k, 559 const vector & invec)205 const vectorBase& invec) 560 206 { 561 207 assert(invec.gsl_vector_p()); … … 566 212 567 213 568 double sum(const vector & v)214 double sum(const vectorBase& v) 569 215 { 570 216 double sum = 0; 571 217 size_t vsize=v.size(); 572 218 for (size_t i=0; i<vsize; ++i) 573 sum += v [i];219 sum += v(i); 574 220 return sum; 575 221 } 576 222 577 223 578 void swap(vector& v, vector& w) 579 { 580 assert(v.gsl_vector_p()); assert(w.gsl_vector_p()); 581 int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p()); 582 if (status) 583 throw utility::GSL_error(std::string("swap(vector&,vector&)",status)); 584 } 585 586 587 std::ostream& operator<<(std::ostream& s, const vector& a) 224 std::ostream& operator<<(std::ostream& s, const vectorBase& a) 588 225 { 589 226 s.setf(std::ios::dec); 590 227 s.precision(12); 591 228 for (size_t j = 0; j < a.size(); ++j) { 592 s << a [j];229 s << a(j); 593 230 if ( (j+1)<a.size() ) 594 231 s << s.fill(); -
branches/peter-dev/yat/utility/vectorBase.h
r994 r995 1 #ifndef _theplu_yat_utility_vector_ 2 #define _theplu_yat_utility_vector_ 1 #ifndef _theplu_yat_utility_vector_base_ 2 #define _theplu_yat_utility_vector_base_ 3 3 4 4 // $Id$ … … 44 44 45 45 class matrix; 46 class vector; 46 47 47 48 /** … … 84 85 */ 85 86 86 class vector 87 class vectorBase 87 88 { 88 89 public: 89 90 90 /// \brief vector::iterator 91 typedef Iterator<double&, vector> iterator; 92 /// \brief vector::const_iterator 93 typedef Iterator<const double, const vector> const_iterator; 94 95 /** 96 \brief The default constructor. 97 */ 98 vector(void); 99 100 /** 101 \brief Allocates memory space for \a n elements, and sets all 102 elements to \a init_value. 103 104 \throw GSL_error if memory allocation fails. 105 */ 106 vector(size_t n, double init_value=0); 107 108 /** 109 \brief The copy constructor. 110 111 \note If the object to be copied is a vector view, the values 112 of the view will be copied, i.e. the view is not copied. 113 114 \throw A GSL_error is indirectly thrown if memory allocation 115 fails. 116 */ 117 vector(const vector& other); 118 119 /** 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); 201 202 /** 203 \brief The istream constructor. 204 205 Either elements should be separated with white space characters 206 (default), or elements should be separated by the delimiter \a 207 sep. When delimiter \a sep is used empty elements are stored as 208 NaN's (except that empty lines are ignored). The end of input 209 to the vector is at end of file marker. 210 211 \throw GSL_error if memory allocation fails, IO_error if 212 unexpected input is found in the input stream. 213 */ 214 explicit vector(std::istream &, char sep='\0') 215 throw(utility::IO_error, std::exception); 91 /// \brief vectorBase::const_iterator 92 typedef Iterator<const double, const vectorBase> const_iterator; 93 94 /** 95 \brief Constructor. 96 */ 97 vectorBase(const gsl_vector*); 216 98 217 99 /// 218 100 /// The destructor. 219 101 /// 220 ~vector(void); 221 222 /** 223 \return mutable iterator to start of vector 224 */ 225 iterator begin(void); 226 227 /** 228 \return read-only iterator to start of vector 102 virtual ~vectorBase(void); 103 104 /** 105 \return read-only iterator to start of vectorBase 229 106 */ 230 107 const_iterator begin(void) const; 231 108 232 109 /** 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 110 \return read-only iterator to end of vectorBase 255 111 */ 256 112 const_iterator end(void) const; 257 113 258 114 /** 259 \brief Check whether vector s are equal within a user defined115 \brief Check whether vectorBases are equal within a user defined 260 116 precision, set by \a precision. 261 117 262 118 \return True if each element deviates less or equal than \a 263 d. If any vector contain a NaN, false is always returned.119 d. If any vectorBase contain a NaN, false is always returned. 264 120 265 121 \see operator== and operator!= 266 122 */ 267 bool equal(const vector &, const double precision=0) const;123 bool equal(const vectorBase&, const double precision=0) const; 268 124 269 125 /// … … 273 129 274 130 /// 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); 294 295 /** 296 @brief Resize vector 297 298 All elements are set to @a init_value. 299 300 \note Underlying GSL vector is destroyed and a view into this 301 vector becomes invalid. 302 */ 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. 131 /// @return the number of elements in the vectorBase. 317 132 /// 318 133 size_t size(void) const; 319 134 320 135 /** 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 136 \brief Element access operator. 329 137 330 \return Reference to element \a i.138 \return Const reference to element \a i. 331 139 332 140 \throw If GSL range checks are enabled in the underlying GSL … … 334 142 of range. 335 143 */ 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 GSL344 library a GSL_error exception is thrown if either index is out345 of range.346 */347 144 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 145 const double& operator[](size_t i) const; 362 146 … … 370 154 \return True if all elements are equal otherwise false. 371 155 372 \see equal(const vector &, const double precision=0)373 */ 374 bool operator==(const vector &) const;156 \see equal(const vectorBase&, const double precision=0) 157 */ 158 bool operator==(const vectorBase&) const; 375 159 376 160 /** … … 383 167 \return False if all elements are equal otherwise true. 384 168 385 \see equal(const vector &, const double precision=0)386 */ 387 bool operator!=(const vector &) const;169 \see equal(const vectorBase&, const double precision=0) 170 */ 171 bool operator!=(const vectorBase&) const; 388 172 389 173 /// 390 174 /// @return The dot product. 391 175 /// 392 double operator*(const vector&) const; 393 394 /** 395 \brief The assignment operator. 396 397 Dimensions of the vectors must match. If the LHS vector is a 398 view, the underlying data will be changed. 399 400 \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 452 453 private: 454 455 /** 456 \brief Create a new copy of the internal GSL vector. 457 458 Necessary memory for the new GSL vector is allocated and the 459 caller is responsible for freeing the allocated memory. 460 461 \return A pointer to a copy of the internal GSL vector. 462 463 \throw GSL_error if memory cannot be allocated for the new 464 copy, or if dimensions mis-match. 465 */ 466 gsl_vector* create_gsl_vector_copy(void) const; 467 468 /** 469 \brief Clear all dynamically allocated memory. 470 471 Internal utility function. 472 */ 473 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_vector 479 // in all const member functions. It is not used by design for 480 // non-const vector functions and operators. This is to make sure 481 // that runtime errors occur if a const vector is used in an 482 // inappropriate manner such as on left hand side in assignment 483 // (remember, v_ is null for const vector views). 484 const gsl_vector* proxy_v_; 176 double operator*(const vectorBase&) const; 177 178 protected: 179 const gsl_vector* const_vec_; 485 180 }; 486 181 487 182 /** 488 \brief Check if all elements of the vector are zero.489 490 \return True if all elements in the vector is zero, false183 \brief Check if all elements of the vectorBase are zero. 184 185 \return True if all elements in the vectorBase is zero, false 491 186 othwerwise. 492 187 */ 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 .188 bool isnull(const vectorBase&); 189 190 /** 191 \brief Get the maximum value of the vectorBase. 192 193 \return The maximum value of the vectorBase. 194 */ 195 double max(const vectorBase&); 196 197 /** 198 \brief Locate the maximum value in the vectorBase. 199 200 \return The index to the maximum value of the vectorBase. 506 201 507 202 \note Lower index has precedence. 508 203 */ 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 .204 size_t max_index(const vectorBase&); 205 206 /** 207 \brief Get the minimum value of the vectorBase. 208 209 \return The minimum value of the vectorBase. 210 */ 211 double min(const vectorBase&); 212 213 /** 214 \brief Locate the minimum value in the vectorBase. 215 216 \return The index to the minimum value of the vectorBase. 522 217 523 218 \note Lower index has precedence. 524 219 */ 525 size_t min_index(const vector &);526 527 /** 528 \brief Create a vector \a flag indicating NaN's in another vector220 size_t min_index(const vectorBase&); 221 222 /** 223 \brief Create a vectorBase \a flag indicating NaN's in another vectorBase 529 224 \a templat. 530 225 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 is226 The \a flag vectorBase is changed to contain 1's and 0's only. A 1 227 means that the corresponding element in the \a templat vectorBase is 533 228 valid and a zero means that the corresponding element is a NaN. 534 229 535 \note Space for vector \a flag is reallocated to fit the size of 536 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 is 546 set to one. 547 */ 548 void set_basis(vector&, size_t i); 549 550 /** 551 Randomly shuffles the elements in vector \a invec 552 */ 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 of 562 elements in a another vector \a invec. The elements of \a 563 sort_index give the index of the vector element which would 564 have been stored in that position if the vector had been sorted 230 \note Space for vectorBase \a flag is reallocated to fit the size of 231 vectorBase \a templat if sizes mismatch. 232 233 \return True if the \a templat vectorBase contains at least one NaN. 234 */ 235 bool nan(const vectorBase& templat, vector& flag); 236 237 /** 238 Create a vectorBase \a sort_index containing the indeces of 239 elements in a another vectorBase \a invec. The elements of \a 240 sort_index give the index of the vectorBase element which would 241 have been stored in that position if the vectorBase had been sorted 565 242 in place. The first element of \a sort_index gives the index of the least 566 element in \a invec, and the last element of \a sort_index gives the index of the 567 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 k 243 element in \a invec, and the last element of \a sort_index gives the 244 index of the greatest element in \a invec . The vectorBase \a invec 245 is not changed. 246 */ 247 void sort_index(std::vector<size_t>& sort_index, const vectorBase& invec); 248 249 /** 250 Similar to sort_index but creates a vectorBase with indices to the \a k 573 251 smallest elements in \a invec. 574 252 */ 575 253 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, const 576 vector& invec); 577 578 /** Similar to sort_index but creates a vector with indices to the \a k 254 vectorBase& invec); 255 256 /** 257 Similar to sort_index but creates a vectorBase with indices to the \a k 579 258 largest elements in \a invec. 580 259 */ 581 260 void sort_largest_index(std::vector<size_t>& sort_index, size_t k, const 582 vector & invec);261 vectorBase& invec); 583 262 584 263 585 264 586 265 /** 587 \brief Calculate the sum of all vector elements.266 \brief Calculate the sum of all vectorBase elements. 588 267 589 268 \return The sum. 590 269 */ 591 double sum(const vector&); 592 593 /** 594 \brief Swap vector elements by copying. 595 596 The two vectors must have the same length. 597 598 \throw GSL_error if vector lengths differs. 599 */ 600 void swap(vector&, vector&); 601 602 /** 603 \brief The output operator for the vector class. 604 */ 605 std::ostream& operator<<(std::ostream&, const vector&); 270 double sum(const vectorBase&); 271 272 /** 273 \brief The output operator for the vectorBase class. 274 */ 275 std::ostream& operator<<(std::ostream&, const vectorBase&); 606 276 607 277 }}} // of namespace utility, yat, and theplu -
branches/peter-dev/yat/utility/vectorConstView.cc
r994 r995 25 25 */ 26 26 27 #include "vector .h"27 #include "vectorConstView.h" 28 28 #include "matrix.h" 29 29 #include "utility.h" … … 43 43 44 44 45 vector ::vector(void)46 : v _(NULL), view_(NULL), view_const_(NULL), proxy_v_(NULL)45 vectorConstView::vectorConstView(void) 46 : vectorBase(NULL) 47 47 { 48 48 } 49 49 50 50 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_) 56 throw utility::GSL_error("vector::vector failed to allocate memory"); 57 58 all(init_value); 59 } 60 61 62 vector::vector(const vector& other) 63 : v_(other.create_gsl_vector_copy()), view_(NULL), 64 view_const_(NULL), proxy_v_(v_) 51 vectorConstView::vectorConstView(const vectorBase& other) 52 : vectorBase(other.gsl_vector_p()) 65 53 { 66 54 } 67 55 68 56 69 vector::vector(vector& v, size_t offset, size_t n, size_t stride) 70 : view_const_(NULL) 57 vectorConstView::vectorConstView(const vectorBase& v, size_t offset, 58 size_t n, size_t stride) 59 : vectorBase(NULL) 71 60 { 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); 61 const_view_ = new gsl_vector_const_view( 62 gsl_vector_const_subvector_with_stride(v.gsl_vector_p(), 63 offset, stride, n)); 64 const_vec_ = &(const_view_->vector); 77 65 } 78 66 79 67 80 vector ::vector(const vector& v, size_t offset, size_t n, size_t stride)81 : v _(NULL), view_(NULL)68 vectorConstView::vectorConstView(const matrix& m, size_t i, bool row) 69 : vectorBase(NULL) 82 70 { 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);71 const_view_ = 72 new gsl_vector_const_view(row ? 73 gsl_matrix_const_row(m.gsl_matrix_p(),i) : 74 gsl_matrix_const_column(m.gsl_matrix_p(),i)); 75 const_vec_ = &(const_view_->vector); 88 76 } 89 77 90 78 91 vector::vector(matrix& m, size_t i, bool row) 92 : view_const_(NULL) 79 vectorConstView::~vectorConstView(void) 93 80 { 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 81 } 101 82 102 83 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);112 }113 114 115 vector::vector(std::istream& is, char sep)116 throw (utility::IO_error, std::exception)117 : view_(NULL), view_const_(NULL)118 {119 // read the data file and store in stl vectors (dynamically120 // expandable)121 std::vector<std::vector<double> > data_matrix;122 u_int nof_columns=0;123 u_int nof_rows=0;124 std::string line;125 while(getline(is, line, '\n')) {126 // Empty lines127 if (!line.size())128 continue;129 nof_rows++;130 131 std::vector<double> v;132 std::string element;133 std::stringstream ss(line);134 bool ok=true;135 while(ok) {136 if(sep=='\0')137 ok=(ss>>element);138 else139 ok=getline(ss, element, sep);140 if(!ok)141 break;142 143 if(utility::is_double(element)) {144 v.push_back(atof(element.c_str()));145 }146 else if (!element.size() || utility::is_nan(element)) {147 v.push_back(std::numeric_limits<double>::quiet_NaN());148 }149 else {150 std::stringstream ss("Warning: '");151 ss << element << "' is not a double.";152 throw IO_error(ss.str());153 }154 }155 if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator156 v.push_back(std::numeric_limits<double>::quiet_NaN());157 if (!nof_columns)158 nof_columns=v.size();159 else if (nof_rows && (nof_columns>1)) {160 std::ostringstream s;161 s << "vector::vector(std::istream&) data file error:\n"162 << " File has inconsistent number of rows (" << nof_rows163 << ") and columns (" << nof_columns164 << ").\n Expected a row or a column vector.";165 throw utility::IO_error(s.str());166 }167 else if (v.size()!=nof_columns) {168 std::ostringstream s;169 s << "vector::vector(std::istream&) data file error:\n"170 << " Line " << nof_rows << " has " << v.size()171 << " columns; expected " << nof_columns << " column.";172 throw utility::IO_error(s.str());173 }174 data_matrix.push_back(v);175 }176 177 // manipulate the state of the stream to be good178 is.clear(std::ios::goodbit);179 // convert the data to a gsl vector180 proxy_v_ = v_ = gsl_vector_alloc(nof_rows*nof_columns);181 if (!v_)182 throw utility::GSL_error("vector::vector failed to allocate memory");183 size_t n=0;184 // if gsl error handler disabled, out of bounds index will not185 // abort the program.186 for (size_t i=0; i<nof_rows; i++)187 for (size_t j=0; j<nof_columns; j++)188 gsl_vector_set( v_, n++, data_matrix[i][j] );189 }190 191 192 vector::~vector(void)193 {194 delete_allocated_memory();195 }196 197 198 vector::iterator vector::begin(void)199 {200 return vector::iterator(*this, 0);201 }202 203 204 vector::const_iterator vector::begin(void) const205 {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 228 }229 return *this;230 }231 232 233 gsl_vector* vector::create_gsl_vector_copy(void) const234 {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) const272 {273 return vector::const_iterator(*this, size());274 }275 276 277 bool vector::equal(const vector& other, const double d) const278 {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 not284 // abort the program.285 for (size_t i=0; i<size(); ++i)286 // The two last condition checks are needed for NaN detection287 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) const295 {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) const307 {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) const346 {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) const373 {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) const388 {389 return this->operator()(i);390 }391 392 393 bool vector::operator==(const vector& other) const394 {395 return equal(other);396 }397 398 399 bool vector::operator!=(const vector& other) const400 {401 return !equal(other);402 }403 404 405 double vector::operator*( const vector &other ) const406 {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");419 return *this;420 }421 422 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 else503 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 514 void set_basis(vector& v, size_t i)515 {516 assert(v.gsl_vector_p());517 gsl_vector_set_basis(v.gsl_vector_p(),i);518 }519 520 521 void shuffle(vector& invec)522 {523 random::DiscreteUniform rnd;524 std::random_shuffle(invec.begin(), invec.end(), rnd);525 }526 527 528 void sort(vector& v)529 {530 assert(v.gsl_vector_p());531 std::sort(v.begin(), v.end());532 }533 534 535 void sort_index(std::vector<size_t>& sort_index, const vector& invec)536 {537 assert(invec.gsl_vector_p());538 gsl_permutation* p = gsl_permutation_alloc(invec.size());539 int status=gsl_sort_vector_index(p,invec.gsl_vector_p());540 if (status) {541 gsl_permutation_free(p);542 throw utility::GSL_error(std::string("sort_index(vector&,const vector&)",status));543 }544 sort_index=std::vector<size_t>(p->data,p->data+p->size);545 gsl_permutation_free(p);546 }547 548 549 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k,550 const vector& invec)551 {552 assert(invec.gsl_vector_p());553 assert(k<=invec.size());554 sort_index.resize(k);555 gsl_sort_vector_smallest_index(&sort_index[0],k,invec.gsl_vector_p());556 }557 558 void sort_largest_index(std::vector<size_t>& sort_index, size_t k,559 const vector& invec)560 {561 assert(invec.gsl_vector_p());562 assert(k<=invec.size());563 sort_index.resize(k);564 gsl_sort_vector_largest_index(&sort_index[0],k,invec.gsl_vector_p());565 }566 567 568 double sum(const vector& v)569 {570 double sum = 0;571 size_t vsize=v.size();572 for (size_t i=0; i<vsize; ++i)573 sum += v[i];574 return sum;575 }576 577 578 void swap(vector& v, vector& w)579 {580 assert(v.gsl_vector_p()); assert(w.gsl_vector_p());581 int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p());582 if (status)583 throw utility::GSL_error(std::string("swap(vector&,vector&)",status));584 }585 586 587 std::ostream& operator<<(std::ostream& s, const vector& a)588 {589 s.setf(std::ios::dec);590 s.precision(12);591 for (size_t j = 0; j < a.size(); ++j) {592 s << a[j];593 if ( (j+1)<a.size() )594 s << s.fill();595 }596 return s;597 }598 599 84 }}} // of namespace utility, yat, and thep -
branches/peter-dev/yat/utility/vectorConstView.h
r994 r995 1 #ifndef _theplu_yat_utility_vector_ 2 #define _theplu_yat_utility_vector_ 1 #ifndef _theplu_yat_utility_vector_const_view_ 2 #define _theplu_yat_utility_vector_const_view_ 3 3 4 4 // $Id$ … … 28 28 02111-1307, USA. 29 29 */ 30 31 #include "vectorBase.h" 30 32 31 33 #include "Exception.h" … … 84 86 */ 85 87 86 class vector 88 class vectorConstView : public vectorBase 87 89 { 88 90 public: 89 91 90 /// \brief vector::iterator91 typedef Iterator<double&, vector> iterator;92 /// \brief vector::const_iterator93 typedef Iterator<const double, const vector> const_iterator;94 95 92 /** 96 \brief The default constructor.93 \brief Default Constructor 97 94 */ 98 vector(void); 99 100 /** 101 \brief Allocates memory space for \a n elements, and sets all 102 elements to \a init_value. 103 104 \throw GSL_error if memory allocation fails. 105 */ 106 vector(size_t n, double init_value=0); 95 vectorConstView(void); 107 96 108 97 /** 109 98 \brief The copy constructor. 110 99 111 \note If the object to be copied is a vector view, the values100 \note If the object to be copied is a vectorConstView view, the values 112 101 of the view will be copied, i.e. the view is not copied. 113 102 … … 115 104 fails. 116 105 */ 117 vector (const vector& other);106 vectorConstView(const vectorBase& other); 118 107 119 108 /** 120 \brief Vector view constructor.109 \brief VectorConstView constructor. 121 110 122 Create a view of vector\a v, with starting index \a offset,111 Create a ConstView of vectorBase \a v, with starting index \a offset, 123 112 size \a n, and an optional \a stride. 124 125 A vector view can be used as any vector with the difference126 that changes made to the view will also change the object that127 is viewed. Also, using the copy constructor will create a new128 vector object that is a copy of whatever is viewed. If a copy129 of the view is needed then you should use this constructor to130 obtain a copy.131 113 132 114 \note If the object viewed by the view goes out of scope or is … … 136 118 \throw GSL_error if a view cannot be set up. 137 119 */ 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); 120 vectorConstView(const vectorBase& v, size_t offset, size_t n, 121 size_t stride=1); 158 122 159 123 /// 160 /// Matrix row/column view constructor.124 /// Matrix row/column const view constructor. 161 125 /// 162 /// Create a row/column vector view of matrix \a m, pointing at126 /// Create a row/column vectorConstView view of matrix \a m, pointing at 163 127 /// row/column \a i. The parameter \a row is used to set whether 164 128 /// the view should be a row or column view. If \a row is set to … … 166 130 /// naturally, a column view otherwise. 167 131 /// 168 /// A vector view can be used as any vector with the difference169 /// that changes made to the view will also change the object that170 /// is viewed. Also, using the copy constructor will create a new171 /// vector object that is a copy of whatever is viewed. If a copy172 /// of the view is needed then you should use the vector view173 /// constructor to obtain a copy.174 ///175 132 /// @note If the object viewed by the view goes out of scope or is 176 133 /// deleted, the view becomes invalid and the result of further 177 134 /// use is undefined. 178 135 /// 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); 201 202 /** 203 \brief The istream constructor. 204 205 Either elements should be separated with white space characters 206 (default), or elements should be separated by the delimiter \a 207 sep. When delimiter \a sep is used empty elements are stored as 208 NaN's (except that empty lines are ignored). The end of input 209 to the vector is at end of file marker. 210 211 \throw GSL_error if memory allocation fails, IO_error if 212 unexpected input is found in the input stream. 213 */ 214 explicit vector(std::istream &, char sep='\0') 215 throw(utility::IO_error, std::exception); 136 vectorConstView(const matrix& m, size_t i, bool row=true); 216 137 217 138 /// 218 139 /// The destructor. 219 140 /// 220 ~vector(void); 221 222 /** 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); 294 295 /** 296 @brief Resize vector 297 298 All elements are set to @a init_value. 299 300 \note Underlying GSL vector is destroyed and a view into this 301 vector becomes invalid. 302 */ 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; 393 394 /** 395 \brief The assignment operator. 396 397 Dimensions of the vectors must match. If the LHS vector is a 398 view, the underlying data will be changed. 399 400 \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 141 ~vectorConstView(void); 452 142 453 143 private: 454 455 /** 456 \brief Create a new copy of the internal GSL vector. 457 458 Necessary memory for the new GSL vector is allocated and the 459 caller is responsible for freeing the allocated memory. 460 461 \return A pointer to a copy of the internal GSL vector. 462 463 \throw GSL_error if memory cannot be allocated for the new 464 copy, or if dimensions mis-match. 465 */ 466 gsl_vector* create_gsl_vector_copy(void) const; 467 468 /** 469 \brief Clear all dynamically allocated memory. 470 471 Internal utility function. 472 */ 473 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_vector 479 // in all const member functions. It is not used by design for 480 // non-const vector functions and operators. This is to make sure 481 // that runtime errors occur if a const vector is used in an 482 // inappropriate manner such as on left hand side in assignment 483 // (remember, v_ is null for const vector views). 484 const gsl_vector* proxy_v_; 144 const gsl_vector_const_view* const_view_; 485 145 }; 486 146 487 /** 488 \brief Check if all elements of the vector are zero. 489 490 \return True if all elements in the vector is zero, false 491 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 vector 529 \a templat. 530 531 The \a flag vector is changed to contain 1's and 0's only. A 1 532 means that the corresponding element in the \a templat vector is 533 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 of 536 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 is 546 set to one. 547 */ 548 void set_basis(vector&, size_t i); 549 550 /** 551 Randomly shuffles the elements in vector \a invec 552 */ 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 of 562 elements in a another vector \a invec. The elements of \a 563 sort_index give the index of the vector element which would 564 have been stored in that position if the vector had been sorted 565 in place. The first element of \a sort_index gives the index of the least 566 element in \a invec, and the last element of \a sort_index gives the index of the 567 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 k 573 smallest elements in \a invec. 574 */ 575 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, const 576 vector& invec); 577 578 /** Similar to sort_index but creates a vector with indices to the \a k 579 largest elements in \a invec. 580 */ 581 void sort_largest_index(std::vector<size_t>& sort_index, size_t k, const 582 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 593 /** 594 \brief Swap vector elements by copying. 595 596 The two vectors must have the same length. 597 598 \throw GSL_error if vector lengths differs. 599 */ 600 void swap(vector&, vector&); 601 602 /** 603 \brief The output operator for the vector class. 604 */ 605 std::ostream& operator<<(std::ostream&, const vector&); 606 607 }}} // of namespace utility, yat, and theplu 147 }}} // of namespace utility, yat, and theplu 608 148 609 149 #endif -
branches/peter-dev/yat/utility/vectorMutable.cc
r994 r995 25 25 */ 26 26 27 #include "vector .h"27 #include "vectorMutable.h" 28 28 #include "matrix.h" 29 29 #include "utility.h" … … 43 43 44 44 45 vector ::vector(void)46 : v _(NULL), view_(NULL), view_const_(NULL), proxy_v_(NULL)45 vectorMutable::vectorMutable(gsl_vector* v) 46 : vectorBase(v) 47 47 { 48 48 } 49 49 50 50 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_) 56 throw utility::GSL_error("vector::vector failed to allocate memory"); 57 58 all(init_value); 59 } 60 61 62 vector::vector(const vector& other) 63 : v_(other.create_gsl_vector_copy()), view_(NULL), 64 view_const_(NULL), proxy_v_(v_) 51 vectorMutable::~vectorMutable(void) 65 52 { 66 53 } 67 54 68 55 69 vector::vector(vector& v, size_t offset, size_t n, size_t stride) 70 : view_const_(NULL) 56 vectorMutable::iterator vectorMutable::begin(void) 71 57 { 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); 58 return vectorMutable::iterator(*this, 0); 77 59 } 78 60 79 61 80 vector::vector(const vector& v, size_t offset, size_t n, size_t stride) 81 : v_(NULL), view_(NULL) 62 void vectorMutable::div(const vectorBase& other) 82 63 { 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); 64 assert(vec_); 65 int status=gsl_vector_div(vec_,other.gsl_vector_p()); 66 if (status) 67 throw utility::GSL_error(std::string("vectorMutable::div",status)); 88 68 } 89 69 90 70 91 vector::vector(matrix& m, size_t i, bool row) 92 : view_const_(NULL) 71 vectorMutable::iterator vectorMutable::end(void) 93 72 { 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); 73 return vectorMutable::iterator(*this, size()); 100 74 } 101 75 102 76 103 vector::vector(const matrix& m, size_t i, bool row) 104 : v_(NULL), view_(NULL) 77 gsl_vector* vectorMutable::gsl_vector_p(void) 105 78 { 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); 79 return vec_; 112 80 } 113 81 114 82 115 vector::vector(std::istream& is, char sep) 116 throw (utility::IO_error, std::exception) 117 : view_(NULL), view_const_(NULL) 83 void vectorMutable::mul(const vectorBase& other) 118 84 { 119 // read the data file and store in stl vectors (dynamically 120 // expandable) 121 std::vector<std::vector<double> > data_matrix; 122 u_int nof_columns=0; 123 u_int nof_rows=0; 124 std::string line; 125 while(getline(is, line, '\n')) { 126 // Empty lines 127 if (!line.size()) 128 continue; 129 nof_rows++; 130 131 std::vector<double> v; 132 std::string element; 133 std::stringstream ss(line); 134 bool ok=true; 135 while(ok) { 136 if(sep=='\0') 137 ok=(ss>>element); 138 else 139 ok=getline(ss, element, sep); 140 if(!ok) 141 break; 142 143 if(utility::is_double(element)) { 144 v.push_back(atof(element.c_str())); 145 } 146 else if (!element.size() || utility::is_nan(element)) { 147 v.push_back(std::numeric_limits<double>::quiet_NaN()); 148 } 149 else { 150 std::stringstream ss("Warning: '"); 151 ss << element << "' is not a double."; 152 throw IO_error(ss.str()); 153 } 154 } 155 if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator 156 v.push_back(std::numeric_limits<double>::quiet_NaN()); 157 if (!nof_columns) 158 nof_columns=v.size(); 159 else if (nof_rows && (nof_columns>1)) { 160 std::ostringstream s; 161 s << "vector::vector(std::istream&) data file error:\n" 162 << " File has inconsistent number of rows (" << nof_rows 163 << ") and columns (" << nof_columns 164 << ").\n Expected a row or a column vector."; 165 throw utility::IO_error(s.str()); 166 } 167 else if (v.size()!=nof_columns) { 168 std::ostringstream s; 169 s << "vector::vector(std::istream&) data file error:\n" 170 << " Line " << nof_rows << " has " << v.size() 171 << " columns; expected " << nof_columns << " column."; 172 throw utility::IO_error(s.str()); 173 } 174 data_matrix.push_back(v); 175 } 176 177 // manipulate the state of the stream to be good 178 is.clear(std::ios::goodbit); 179 // convert the data to a gsl vector 180 proxy_v_ = v_ = gsl_vector_alloc(nof_rows*nof_columns); 181 if (!v_) 182 throw utility::GSL_error("vector::vector failed to allocate memory"); 183 size_t n=0; 184 // if gsl error handler disabled, out of bounds index will not 185 // abort the program. 186 for (size_t i=0; i<nof_rows; i++) 187 for (size_t j=0; j<nof_columns; j++) 188 gsl_vector_set( v_, n++, data_matrix[i][j] ); 85 assert(vec_); 86 int status=gsl_vector_mul(vec_,other.gsl_vector_p()); 87 if (status) 88 throw utility::GSL_error(std::string("vectorMutable::mul",status)); 189 89 } 190 90 191 91 192 v ector::~vector(void)92 void vectorMutable::reverse(void) 193 93 { 194 delete_allocated_memory(); 94 assert(vec_); 95 gsl_vector_reverse(vec_); 195 96 } 196 97 197 98 198 v ector::iterator vector::begin(void)99 void vectorMutable::all(const double& value) 199 100 { 200 return vector::iterator(*this, 0); 101 assert(vec_); 102 gsl_vector_set_all(vec_,value); 201 103 } 202 104 203 105 204 v ector::const_iterator vector::begin(void) const106 void vectorMutable::swap(size_t i, size_t j) 205 107 { 206 return vector::const_iterator(*this, 0); 108 assert(vec_); 109 int status=gsl_vector_swap_elements(vec_, i, j); 110 if (status) 111 throw utility::GSL_error(std::string("vectorMutable::swap_elements",status)); 207 112 } 208 113 209 114 210 const vector& vector::clone(const vector& other)115 double& vectorMutable::operator()(size_t i) 211 116 { 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 228 } 229 return *this; 230 } 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); 117 assert(vec_); 118 double* d=gsl_vector_ptr(vec_, i); 366 119 if (!d) 367 throw utility::GSL_error("vector ::operator()",GSL_EINVAL);120 throw utility::GSL_error("vectorMutable::operator()",GSL_EINVAL); 368 121 return *d; 369 122 } 370 123 371 124 372 const double& vector::operator()(size_t i) const125 const vectorMutable& vectorMutable::operator+=(const vectorBase& other) 373 126 { 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"); 419 return *this; 420 } 421 422 423 const vector& vector::operator+=(const vector& other) 424 { 425 assert(v_); 426 int status=gsl_vector_add(v_, other.gsl_vector_p()); 127 assert(vec_); 128 int status=gsl_vector_add(vec_, other.gsl_vector_p()); 427 129 if (status) 428 throw utility::GSL_error(std::string("vector ::add", status));130 throw utility::GSL_error(std::string("vectorMutable::add", status)); 429 131 return *this; 430 132 } 431 133 432 134 433 const vector & vector::operator+=(double d)135 const vectorMutable& vectorMutable::operator+=(double d) 434 136 { 435 assert(v _);436 gsl_vector_add_constant(v _, d);137 assert(vec_); 138 gsl_vector_add_constant(vec_, d); 437 139 return *this; 438 140 } 439 141 440 142 441 const vector & vector::operator-=(const vector& other)143 const vectorMutable& vectorMutable::operator-=(const vectorBase& other) 442 144 { 443 assert(v _);444 int status=gsl_vector_sub(v _, other.gsl_vector_p());145 assert(vec_); 146 int status=gsl_vector_sub(vec_, other.gsl_vector_p()); 445 147 if (status) 446 throw utility::GSL_error(std::string("vector ::sub", status));148 throw utility::GSL_error(std::string("vectorMutable::sub", status)); 447 149 return *this; 448 150 } 449 151 450 152 451 const vector & vector::operator-=(const double d)153 const vectorMutable& vectorMutable::operator-=(const double d) 452 154 { 453 assert(v _);454 gsl_vector_add_constant(v _, -d);155 assert(vec_); 156 gsl_vector_add_constant(vec_, -d); 455 157 return *this; 456 158 } 457 159 458 160 459 const vector & vector::operator*=(const double d)161 const vectorMutable& vectorMutable::operator*=(const double d) 460 162 { 461 assert(v _);462 gsl_vector_scale(v _, d);163 assert(vec_); 164 gsl_vector_scale(vec_, d); 463 165 return *this; 464 166 } 465 167 466 168 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 514 void set_basis(vector& v, size_t i) 169 void set_basis(vectorMutable& v, size_t i) 515 170 { 516 171 assert(v.gsl_vector_p()); … … 519 174 520 175 521 void shuffle(vector & invec)176 void shuffle(vectorMutable& invec) 522 177 { 523 178 random::DiscreteUniform rnd; … … 526 181 527 182 528 void sort(vector & v)183 void sort(vectorMutable& v) 529 184 { 530 185 assert(v.gsl_vector_p()); … … 532 187 } 533 188 534 535 void sort_index(std::vector<size_t>& sort_index, const vector& invec) 189 vectorMutable::operator proxy () 536 190 { 537 assert(invec.gsl_vector_p()); 538 gsl_permutation* p = gsl_permutation_alloc(invec.size()); 539 int status=gsl_sort_vector_index(p,invec.gsl_vector_p()); 540 if (status) { 541 gsl_permutation_free(p); 542 throw utility::GSL_error(std::string("sort_index(vector&,const vector&)",status)); 543 } 544 sort_index=std::vector<size_t>(p->data,p->data+p->size); 545 gsl_permutation_free(p); 191 proxy p; 192 p.vec_ = this->vec_; 193 return p; 546 194 } 547 195 548 196 549 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k,550 const vector& invec)551 {552 assert(invec.gsl_vector_p());553 assert(k<=invec.size());554 sort_index.resize(k);555 gsl_sort_vector_smallest_index(&sort_index[0],k,invec.gsl_vector_p());556 }557 558 void sort_largest_index(std::vector<size_t>& sort_index, size_t k,559 const vector& invec)560 {561 assert(invec.gsl_vector_p());562 assert(k<=invec.size());563 sort_index.resize(k);564 gsl_sort_vector_largest_index(&sort_index[0],k,invec.gsl_vector_p());565 }566 567 568 double sum(const vector& v)569 {570 double sum = 0;571 size_t vsize=v.size();572 for (size_t i=0; i<vsize; ++i)573 sum += v[i];574 return sum;575 }576 577 578 void swap(vector& v, vector& w)579 {580 assert(v.gsl_vector_p()); assert(w.gsl_vector_p());581 int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p());582 if (status)583 throw utility::GSL_error(std::string("swap(vector&,vector&)",status));584 }585 586 587 std::ostream& operator<<(std::ostream& s, const vector& a)588 {589 s.setf(std::ios::dec);590 s.precision(12);591 for (size_t j = 0; j < a.size(); ++j) {592 s << a[j];593 if ( (j+1)<a.size() )594 s << s.fill();595 }596 return s;597 }598 197 599 198 }}} // of namespace utility, yat, and thep -
branches/peter-dev/yat/utility/vectorMutable.h
r994 r995 1 #ifndef _theplu_yat_utility_vector_ 2 #define _theplu_yat_utility_vector_ 1 #ifndef _theplu_yat_utility_vector_mutable_ 2 #define _theplu_yat_utility_vector_mutable_ 3 3 4 4 // $Id$ … … 29 29 */ 30 30 31 #include "vectorBase.h" 32 31 33 #include "Exception.h" 32 34 #include "Iterator.h" … … 44 46 45 47 class matrix; 46 48 47 49 /** 48 50 @brief This is the yat interface to GSL vector. … … 84 86 */ 85 87 86 class vector 88 class vectorMutable : public vectorBase 87 89 { 88 90 public: 89 91 90 /// \brief vector::iterator 91 typedef Iterator<double&, vector> iterator; 92 /// \brief vector::const_iterator 93 typedef Iterator<const double, const vector> const_iterator; 92 /// \brief vectorMutable::iterator 93 typedef Iterator<double&, vectorMutable> iterator; 94 94 95 95 /** 96 96 \brief The default constructor. 97 97 */ 98 vector(void); 99 100 /** 101 \brief Allocates memory space for \a n elements, and sets all 102 elements to \a init_value. 103 104 \throw GSL_error if memory allocation fails. 105 */ 106 vector(size_t n, double init_value=0); 107 108 /** 109 \brief The copy constructor. 110 111 \note If the object to be copied is a vector view, the values 112 of the view will be copied, i.e. the view is not copied. 113 114 \throw A GSL_error is indirectly thrown if memory allocation 115 fails. 116 */ 117 vector(const vector& other); 118 119 /** 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); 201 202 /** 203 \brief The istream constructor. 204 205 Either elements should be separated with white space characters 206 (default), or elements should be separated by the delimiter \a 207 sep. When delimiter \a sep is used empty elements are stored as 208 NaN's (except that empty lines are ignored). The end of input 209 to the vector is at end of file marker. 210 211 \throw GSL_error if memory allocation fails, IO_error if 212 unexpected input is found in the input stream. 213 */ 214 explicit vector(std::istream &, char sep='\0') 215 throw(utility::IO_error, std::exception); 98 vectorMutable(gsl_vector*); 216 99 217 100 /// 218 101 /// The destructor. 219 102 /// 220 ~vector(void); 221 222 /** 223 \return mutable iterator to start of vector 103 virtual ~vectorMutable(void); 104 105 // to enable overloading from base class 106 using vectorBase::begin; 107 108 /** 109 \return mutable iterator to start of vectorMutable 224 110 */ 225 111 iterator begin(void); 226 112 227 113 /** 228 \return read-only iterator to start of vector229 */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 is236 resized and view state is changed if needed.237 */238 const vector& clone(const vector& other);239 240 /**241 114 \brief This function performs element-wise division, \f$ this_i = 242 115 this_i/other_i \; \forall i \f$. … … 244 117 \throw GSL_error if dimensions mis-match. 245 118 */ 246 void div(const vector& other); 247 248 /** 249 \return mutable iterator to end of vector 119 void div(const vectorBase& other); 120 121 // to enable overloading from base class 122 using vectorBase::end; 123 124 /** 125 \return mutable iterator to end of vectorMutable 250 126 */ 251 127 iterator end(void); 252 128 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; 129 // to enable overloading from base class 130 using vectorBase::gsl_vector_p; 273 131 274 132 /// … … 276 134 /// 277 135 gsl_vector* gsl_vector_p(void); 278 279 ///280 /// Check if the vector object is a view (sub-vector) to another281 /// vector.282 ///283 /// @return True if the object is a view, false othwerwise.284 ///285 bool isview(void) const;286 136 287 137 /** … … 291 141 \throw GSL_error if dimensions mis-match. 292 142 */ 293 void mul(const vector& other); 294 295 /** 296 @brief Resize vector 297 298 All elements are set to @a init_value. 299 300 \note Underlying GSL vector is destroyed and a view into this 301 vector becomes invalid. 302 */ 303 void resize(size_t, double init_value=0); 304 305 /** 306 \brief Reverse the order of elements in the vector. 143 void mul(const vectorBase& other); 144 145 /** 146 \brief Reverse the order of elements in the vectorMutable. 307 147 */ 308 148 void reverse(void); … … 313 153 void all(const double& value); 314 154 315 ///316 /// @return the number of elements in the vector.317 ///318 size_t size(void) const;319 320 155 /** 321 156 \brief Exchange elements \a i and \a j. 322 157 323 \throw GSL_error if vector lengths differs.158 \throw GSL_error if vectorMutable lengths differs. 324 159 */ 325 160 void swap(size_t i, size_t j); 161 162 // to allow overloading from base class 163 using vectorBase::operator(); 326 164 327 165 /** … … 337 175 338 176 /** 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 GSL344 library a GSL_error exception is thrown if either index is out345 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 off367 effects may destroy comparison. Use the equal function for368 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 off380 effects may destroy comparison. Use the equal function for381 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;393 394 /**395 177 \brief The assignment operator. 396 178 397 Dimensions of the vector s must match. If the LHS vectoris a179 Dimensions of the vectorMutables must match. If the LHS vectorMutable is a 398 180 view, the underlying data will be changed. 399 181 400 \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$182 \return A const reference to the resulting vectorMutable. 183 184 \see void set(const vectorMutable&). 185 186 \throw GSL_error if dimensions mis-match. 187 */ 188 virtual const vectorMutable& operator=(vectorMutable&)=0; 189 190 /** 191 \brief Addition and assign operator. VectorMutable addition, \f$ 410 192 this_i = this_i + other_i \; \forall i \f$. 411 193 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 \;194 \return A const reference to the resulting vectorMutable. 195 196 \throw GSL_error if dimensions mis-match. 197 */ 198 const vectorMutable& operator+=(const vectorBase&); 199 200 /** 201 \brief Add a constant to a vectorMutable, \f$ this_i = this_i + d \; 420 202 \forall i \f$. 421 203 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$204 \return A const reference to the resulting vectorMutable. 205 */ 206 const vectorMutable& operator+=(double d); 207 208 /** 209 \brief Subtract and assign operator. VectorMutable subtraction, \f$ 428 210 this_i = this_i - other_i \; \forall i \f$. 429 211 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 - d212 \return A const reference to the resulting vectorMutable. 213 214 \throw GSL_error if dimensions mis-match. 215 */ 216 const vectorMutable& operator-=(const vectorBase&); 217 218 /** 219 \brief Subtract a constant to a vectorMutable, \f$ this_i = this_i - d 438 220 \; \forall i \f$. 439 221 440 \return A const reference to the resulting vector .441 */ 442 const vector & operator-=(double d);222 \return A const reference to the resulting vectorMutable. 223 */ 224 const vectorMutable& operator-=(double d); 443 225 444 226 /** … … 446 228 this_i * d \; \forall i \f$. 447 229 448 \return A const reference to the resulting vector. 449 */ 450 const vector& operator*=(double d); 451 452 453 private: 454 455 /** 456 \brief Create a new copy of the internal GSL vector. 457 458 Necessary memory for the new GSL vector is allocated and the 459 caller is responsible for freeing the allocated memory. 460 461 \return A pointer to a copy of the internal GSL vector. 462 463 \throw GSL_error if memory cannot be allocated for the new 464 copy, or if dimensions mis-match. 465 */ 466 gsl_vector* create_gsl_vector_copy(void) const; 467 468 /** 469 \brief Clear all dynamically allocated memory. 470 471 Internal utility function. 472 */ 473 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_vector 479 // in all const member functions. It is not used by design for 480 // non-const vector functions and operators. This is to make sure 481 // that runtime errors occur if a const vector is used in an 482 // inappropriate manner such as on left hand side in assignment 483 // (remember, v_ is null for const vector views). 484 const gsl_vector* proxy_v_; 230 \return A const reference to the resulting vectorMutable. 231 */ 232 const vectorMutable& operator*=(double d); 233 234 protected: 235 gsl_vector* vec_; 236 237 struct proxy 238 { 239 gsl_vector* vec_; 240 }; 241 242 public: 243 /** 244 */ 245 operator proxy(); 246 485 247 }; 486 248 487 249 /** 488 \brief Check if all elements of the vector are zero. 489 490 \return True if all elements in the vector is zero, false 491 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 vector 529 \a templat. 530 531 The \a flag vector is changed to contain 1's and 0's only. A 1 532 means that the corresponding element in the \a templat vector is 533 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 of 536 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. 250 \brief Transforms a vectorMutable to a basis vector. 544 251 545 252 All elements are set to zero except the \a i-th element which is 546 253 set to one. 547 254 */ 548 void set_basis(vector&, size_t i); 549 550 /** 551 Randomly shuffles the elements in vector \a invec 552 */ 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 of 562 elements in a another vector \a invec. The elements of \a 563 sort_index give the index of the vector element which would 564 have been stored in that position if the vector had been sorted 565 in place. The first element of \a sort_index gives the index of the least 566 element in \a invec, and the last element of \a sort_index gives the index of the 567 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 k 573 smallest elements in \a invec. 574 */ 575 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, const 576 vector& invec); 577 578 /** Similar to sort_index but creates a vector with indices to the \a k 579 largest elements in \a invec. 580 */ 581 void sort_largest_index(std::vector<size_t>& sort_index, size_t k, const 582 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&); 255 void set_basis(vectorMutable&, size_t i); 256 257 /** 258 Randomly shuffles the elements in vectorMutable \a invec 259 */ 260 void shuffle(vectorMutable& invec); 261 262 /** 263 Sort the elements in the vectorMutable. 264 */ 265 void sort(vectorMutable&); 592 266 593 267 /** 594 268 \brief Swap vector elements by copying. 595 269 596 The two vectors must have the same length. 597 598 \throw GSL_error if vector lengths differs. 599 */ 600 void swap(vector&, vector&); 601 602 /** 603 \brief The output operator for the vector class. 604 */ 605 std::ostream& operator<<(std::ostream&, const vector&); 270 The two vectorMutables must have the same length. 271 272 \throw GSL_error if vectorMutable lengths differs. 273 */ 274 //void swap(vectorMutable&, vectorMutable&); 606 275 607 276 }}} // of namespace utility, yat, and theplu -
branches/peter-dev/yat/utility/vectorView.cc
r994 r995 25 25 */ 26 26 27 #include "vector .h"27 #include "vectorView.h" 28 28 #include "matrix.h" 29 29 #include "utility.h" … … 43 43 44 44 45 vector ::vector(void)46 : v _(NULL), view_(NULL), view_const_(NULL), proxy_v_(NULL)45 vectorView::vectorView(void) 46 : vectorMutable(NULL) 47 47 { 48 48 } 49 49 50 50 51 vector::vector(size_t n, double init_value) 52 : v_(gsl_vector_alloc(n)), view_(NULL), view_const_(NULL), 53 proxy_v_(v_) 51 vectorView::vectorView(vectorMutable& other) 52 : vectorMutable(NULL), view_(NULL) 54 53 { 55 if (!v_) 56 throw utility::GSL_error("vector::vector failed to allocate memory"); 57 58 all(init_value); 54 view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(),0, 55 other.size())); 56 const_vec_ = vec_ = &(view_->vector); 59 57 } 60 58 61 59 62 vector::vector(const vector& other) 63 : v_(other.create_gsl_vector_copy()), view_(NULL), 64 view_const_(NULL), proxy_v_(v_) 60 vectorView::vectorView(vectorMutable& v, size_t offset,size_t n,size_t stride) 61 : vectorMutable(NULL) 65 62 { 63 view_ = 64 new gsl_vector_view(gsl_vector_subvector_with_stride(v.gsl_vector_p(), 65 offset, stride,n)); 66 const_vec_ = vec_ = &(view_->vector); 66 67 } 67 68 68 69 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) 70 vectorView::vectorView(matrix& m, size_t i, bool row) 71 : vectorMutable(NULL) 93 72 { 94 73 view_=new gsl_vector_view(row ? 95 74 gsl_matrix_row (m.gsl_matrix_p(),i) : 96 75 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); 76 const_vec_ = vec_ = &(view_->vector); 100 77 } 101 78 102 79 103 vector ::vector(const matrix& m, size_t i, bool row)104 : v _(NULL), view_(NULL)80 vectorView::vectorView(proxy p) 81 : vectorMutable(NULL) 105 82 { 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); 83 view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_, 0, p.vec_->size)); 84 const_vec_ = vec_ = &(view_->vector); 112 85 } 113 86 114 87 115 vector::vector(std::istream& is, char sep) 116 throw (utility::IO_error, std::exception) 117 : view_(NULL), view_const_(NULL) 88 vectorView::~vectorView(void) 118 89 { 119 // read the data file and store in stl vectors (dynamically 120 // expandable) 121 std::vector<std::vector<double> > data_matrix; 122 u_int nof_columns=0; 123 u_int nof_rows=0; 124 std::string line; 125 while(getline(is, line, '\n')) { 126 // Empty lines 127 if (!line.size()) 128 continue; 129 nof_rows++; 130 131 std::vector<double> v; 132 std::string element; 133 std::stringstream ss(line); 134 bool ok=true; 135 while(ok) { 136 if(sep=='\0') 137 ok=(ss>>element); 138 else 139 ok=getline(ss, element, sep); 140 if(!ok) 141 break; 142 143 if(utility::is_double(element)) { 144 v.push_back(atof(element.c_str())); 145 } 146 else if (!element.size() || utility::is_nan(element)) { 147 v.push_back(std::numeric_limits<double>::quiet_NaN()); 148 } 149 else { 150 std::stringstream ss("Warning: '"); 151 ss << element << "' is not a double."; 152 throw IO_error(ss.str()); 153 } 154 } 155 if(sep!='\0' && line[line.size()-1]==sep) // add NaN for final separator 156 v.push_back(std::numeric_limits<double>::quiet_NaN()); 157 if (!nof_columns) 158 nof_columns=v.size(); 159 else if (nof_rows && (nof_columns>1)) { 160 std::ostringstream s; 161 s << "vector::vector(std::istream&) data file error:\n" 162 << " File has inconsistent number of rows (" << nof_rows 163 << ") and columns (" << nof_columns 164 << ").\n Expected a row or a column vector."; 165 throw utility::IO_error(s.str()); 166 } 167 else if (v.size()!=nof_columns) { 168 std::ostringstream s; 169 s << "vector::vector(std::istream&) data file error:\n" 170 << " Line " << nof_rows << " has " << v.size() 171 << " columns; expected " << nof_columns << " column."; 172 throw utility::IO_error(s.str()); 173 } 174 data_matrix.push_back(v); 175 } 176 177 // manipulate the state of the stream to be good 178 is.clear(std::ios::goodbit); 179 // convert the data to a gsl vector 180 proxy_v_ = v_ = gsl_vector_alloc(nof_rows*nof_columns); 181 if (!v_) 182 throw utility::GSL_error("vector::vector failed to allocate memory"); 183 size_t n=0; 184 // if gsl error handler disabled, out of bounds index will not 185 // abort the program. 186 for (size_t i=0; i<nof_rows; i++) 187 for (size_t j=0; j<nof_columns; j++) 188 gsl_vector_set( v_, n++, data_matrix[i][j] ); 90 clean_up(); 189 91 } 190 92 191 93 192 vector::~vector(void)94 const vectorMutable& vectorView::operator=(vectorMutable& other ) 193 95 { 194 delete_allocated_memory(); 195 } 196 197 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 228 } 96 if (size()!=other.size()) 97 throw utility::GSL_error("vectorView::dimension mis-match"); 98 if (!other.size()) 99 return *this; 100 clean_up(); 101 view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(),0, 102 other.size())); 103 const_vec_ = vec_ = &view_->vector; 229 104 return *this; 230 105 } 231 106 232 107 233 gsl_vector* vector::create_gsl_vector_copy(void) const108 const vectorMutable& vectorView::operator=(proxy p) 234 109 { 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"); 110 if (size()!=p.vec_->size) 111 throw utility::GSL_error("vectorView::dimension mis-match"); 112 if (!size()) 113 return *this; 114 clean_up(); 115 view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_,0, p.vec_->size)); 116 const_vec_ = vec_ = &view_->vector; 419 117 return *this; 420 118 } 421 119 422 120 423 const vector& vector::operator+=(const vector& other)121 void vectorView::clean_up(void) 424 122 { 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; 123 if (view_) 124 delete view_; 430 125 } 431 126 432 127 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 else503 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 514 void set_basis(vector& v, size_t i)515 {516 assert(v.gsl_vector_p());517 gsl_vector_set_basis(v.gsl_vector_p(),i);518 }519 520 521 void shuffle(vector& invec)522 {523 random::DiscreteUniform rnd;524 std::random_shuffle(invec.begin(), invec.end(), rnd);525 }526 527 528 void sort(vector& v)529 {530 assert(v.gsl_vector_p());531 std::sort(v.begin(), v.end());532 }533 534 535 void sort_index(std::vector<size_t>& sort_index, const vector& invec)536 {537 assert(invec.gsl_vector_p());538 gsl_permutation* p = gsl_permutation_alloc(invec.size());539 int status=gsl_sort_vector_index(p,invec.gsl_vector_p());540 if (status) {541 gsl_permutation_free(p);542 throw utility::GSL_error(std::string("sort_index(vector&,const vector&)",status));543 }544 sort_index=std::vector<size_t>(p->data,p->data+p->size);545 gsl_permutation_free(p);546 }547 548 549 void sort_smallest_index(std::vector<size_t>& sort_index, size_t k,550 const vector& invec)551 {552 assert(invec.gsl_vector_p());553 assert(k<=invec.size());554 sort_index.resize(k);555 gsl_sort_vector_smallest_index(&sort_index[0],k,invec.gsl_vector_p());556 }557 558 void sort_largest_index(std::vector<size_t>& sort_index, size_t k,559 const vector& invec)560 {561 assert(invec.gsl_vector_p());562 assert(k<=invec.size());563 sort_index.resize(k);564 gsl_sort_vector_largest_index(&sort_index[0],k,invec.gsl_vector_p());565 }566 567 568 double sum(const vector& v)569 {570 double sum = 0;571 size_t vsize=v.size();572 for (size_t i=0; i<vsize; ++i)573 sum += v[i];574 return sum;575 }576 577 578 void swap(vector& v, vector& w)579 {580 assert(v.gsl_vector_p()); assert(w.gsl_vector_p());581 int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p());582 if (status)583 throw utility::GSL_error(std::string("swap(vector&,vector&)",status));584 }585 586 587 std::ostream& operator<<(std::ostream& s, const vector& a)588 {589 s.setf(std::ios::dec);590 s.precision(12);591 for (size_t j = 0; j < a.size(); ++j) {592 s << a[j];593 if ( (j+1)<a.size() )594 s << s.fill();595 }596 return s;597 }598 599 128 }}} // of namespace utility, yat, and thep -
branches/peter-dev/yat/utility/vectorView.h
r994 r995 1 #ifndef _theplu_yat_utility_vector_ 2 #define _theplu_yat_utility_vector_ 1 #ifndef _theplu_yat_utility_vector_view_ 2 #define _theplu_yat_utility_vector_view_ 3 3 4 4 // $Id$ … … 29 29 */ 30 30 31 #include "vectorMutable.h" 32 31 33 #include "Exception.h" 32 34 #include "Iterator.h" … … 44 46 45 47 class matrix; 48 class vector; 46 49 47 50 /** … … 84 87 */ 85 88 86 class vector 89 class vectorView : public vectorMutable 87 90 { 88 91 public: 89 90 /// \brief vector::iterator 91 typedef Iterator<double&, vector> iterator; 92 /// \brief vector::const_iterator 93 typedef Iterator<const double, const vector> const_iterator; 92 /// \brief vectorView::iterator 93 typedef Iterator<double&, vectorView> iterator; 94 94 95 95 /** 96 \brief The default constructor.96 \brief Default constructor. 97 97 */ 98 vector(void); 99 100 /** 101 \brief Allocates memory space for \a n elements, and sets all 102 elements to \a init_value. 103 104 \throw GSL_error if memory allocation fails. 105 */ 106 vector(size_t n, double init_value=0); 98 vectorView(void); 107 99 108 100 /** 109 101 \brief The copy constructor. 110 111 \note If the object to be copied is a vector view, the values112 of the view will be copied, i.e. the view is not copied.113 114 \throw A GSL_error is indirectly thrown if memory allocation115 fails.116 102 */ 117 vector (const vector& other);103 vectorView(vectorMutable& other); 118 104 119 105 /** 120 \brief Vector view constructor.106 \brief VectorView constructor. 121 107 122 Create a view of vector \a v, with starting index \a offset,108 Create a view of vectorView \a v, with starting index \a offset, 123 109 size \a n, and an optional \a stride. 124 110 125 A vector view can be used as any vectorwith the difference111 A vectorView view can be used as any vectorView with the difference 126 112 that changes made to the view will also change the object that 127 113 is viewed. Also, using the copy constructor will create a new 128 vector object that is a copy of whatever is viewed. If a copy114 vectorView object that is a copy of whatever is viewed. If a copy 129 115 of the view is needed then you should use this constructor to 130 116 obtain a copy. … … 136 122 \throw GSL_error if a view cannot be set up. 137 123 */ 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); 124 vectorView(vectorMutable& v, size_t offset, size_t n, size_t stride=1); 158 125 159 126 /// 160 127 /// Matrix row/column view constructor. 161 128 /// 162 /// Create a row/column vector view of matrix \a m, pointing at129 /// Create a row/column vectorView view of matrix \a m, pointing at 163 130 /// row/column \a i. The parameter \a row is used to set whether 164 131 /// the view should be a row or column view. If \a row is set to … … 166 133 /// naturally, a column view otherwise. 167 134 /// 168 /// A vector view can be used as any vectorwith the difference135 /// A vectorView view can be used as any vectorView with the difference 169 136 /// that changes made to the view will also change the object that 170 137 /// is viewed. Also, using the copy constructor will create a new 171 /// vector object that is a copy of whatever is viewed. If a copy172 /// of the view is needed then you should use the vector view138 /// vectorView object that is a copy of whatever is viewed. If a copy 139 /// of the view is needed then you should use the vectorView view 173 140 /// constructor to obtain a copy. 174 141 /// … … 177 144 /// use is undefined. 178 145 /// 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); 146 vectorView(matrix& m, size_t i, bool row=true); 201 147 202 148 /** 203 \brief The istream constructor. 204 205 Either elements should be separated with white space characters 206 (default), or elements should be separated by the delimiter \a 207 sep. When delimiter \a sep is used empty elements are stored as 208 NaN's (except that empty lines are ignored). The end of input 209 to the vector is at end of file marker. 210 211 \throw GSL_error if memory allocation fails, IO_error if 212 unexpected input is found in the input stream. 213 */ 214 explicit vector(std::istream &, char sep='\0') 215 throw(utility::IO_error, std::exception); 149 */ 150 vectorView(proxy p); 216 151 217 152 /// 218 153 /// The destructor. 219 154 /// 220 ~vector(void); 221 222 /** 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); 294 295 /** 296 @brief Resize vector 297 298 All elements are set to @a init_value. 299 300 \note Underlying GSL vector is destroyed and a view into this 301 vector becomes invalid. 302 */ 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; 155 ~vectorView(void); 393 156 394 157 /** 395 158 \brief The assignment operator. 396 397 Dimensions of the vectors must match. If the LHS vector is a398 view, the underlying data will be changed.399 400 \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 159 412 160 \return A const reference to the resulting vector. … … 414 162 \throw GSL_error if dimensions mis-match. 415 163 */ 416 const vector & operator+=(const vector&);164 const vectorMutable& operator=(vectorMutable&); 417 165 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 166 const vectorMutable& operator=(proxy); 452 167 453 168 private: 169 void clean_up(void); 454 170 455 /**456 \brief Create a new copy of the internal GSL vector.457 458 Necessary memory for the new GSL vector is allocated and the459 caller is responsible for freeing the allocated memory.460 461 \return A pointer to a copy of the internal GSL vector.462 463 \throw GSL_error if memory cannot be allocated for the new464 copy, or if dimensions mis-match.465 */466 gsl_vector* create_gsl_vector_copy(void) const;467 468 /**469 \brief Clear all dynamically allocated memory.470 471 Internal utility function.472 */473 void delete_allocated_memory(void);474 475 gsl_vector* v_;476 171 gsl_vector_view* view_; 477 const gsl_vector_const_view* view_const_; 478 // proxy_v_ is used to access the proper underlying gsl_vector 479 // in all const member functions. It is not used by design for 480 // non-const vector functions and operators. This is to make sure 481 // that runtime errors occur if a const vector is used in an 482 // inappropriate manner such as on left hand side in assignment 483 // (remember, v_ is null for const vector views). 484 const gsl_vector* proxy_v_; 172 485 173 }; 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 593 /**594 \brief Swap vector elements by copying.595 596 The two vectors must have the same length.597 598 \throw GSL_error if vector lengths differs.599 */600 void swap(vector&, vector&);601 602 /**603 \brief The output operator for the vector class.604 */605 std::ostream& operator<<(std::ostream&, const vector&);606 174 607 175 }}} // of namespace utility, yat, and theplu
Note: See TracChangeset
for help on using the changeset viewer.