- Timestamp:
- Oct 17, 2006, 1:33:46 AM (17 years ago)
- Location:
- trunk/yat/utility
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/utility/vector.cc
r680 r686 40 40 41 41 42 vector::vector(void) 43 : v_(NULL), v_const_(NULL), view_(NULL), view_const_(NULL), proxy_v_(NULL) 44 { 45 } 46 47 48 vector::vector(size_t n, double init_value) 49 : v_(gsl_vector_alloc(n)), v_const_(NULL), view_(NULL), view_const_(NULL), 50 proxy_v_(v_) 51 { 52 set_all(init_value); 53 } 54 55 56 vector::vector(const vector& other) 57 : v_(other.create_gsl_vector_copy()), v_const_(NULL), view_(NULL), 58 view_const_(NULL), proxy_v_(v_) 59 { 60 } 61 42 62 43 63 vector::vector(vector& v, size_t offset, size_t n, size_t stride) 44 : const_view_(NULL)64 : v_const_(NULL), view_const_(NULL) 45 65 { 46 66 // Jari, exception handling needed here. Failure in setting up a … … 49 69 view_ = new gsl_vector_view(gsl_vector_subvector_with_stride(v.v_,offset, 50 70 stride,n)); 51 v_ = &(view_->vector); 52 } 53 71 proxy_v_ = v_ = &(view_->vector); 72 } 54 73 55 74 56 75 vector::vector(const vector& v, size_t offset, size_t n, size_t stride) 57 : v iew_(NULL)76 : v_(NULL), view_(NULL) 58 77 { 59 78 // Jari, exception handling needed here. Failure in setting up a 60 79 // proper gsl_vector_view is communicated by NULL pointer in the 61 80 // view structure (cf. GSL manual). How about GSL error state? 62 const_view_ = new gsl_vector_const_view(81 view_const_ = new gsl_vector_const_view( 63 82 gsl_vector_const_subvector_with_stride(v.v_,offset,stride,n)); 64 v_ = const_cast<gsl_vector*>(&(const_view_->vector)); 65 } 66 83 proxy_v_ = v_const_ = &(view_const_->vector); 84 } 67 85 68 86 69 87 vector::vector(matrix& m, size_t i, bool row) 70 : const_view_(NULL)88 : v_const_(NULL), view_const_(NULL) 71 89 { 72 90 view_=new gsl_vector_view(row ? 73 91 gsl_matrix_row (m.gsl_matrix_p(),i) : 74 92 gsl_matrix_column(m.gsl_matrix_p(),i) ); 75 v_ = &(view_->vector); 76 } 77 93 proxy_v_ = v_ = &(view_->vector); 94 } 78 95 79 96 80 97 vector::vector(const matrix& m, size_t i, bool row) 81 : v iew_(NULL)82 { 83 const_view_ = new gsl_vector_const_view(row ?98 : v_(NULL), view_(NULL) 99 { 100 view_const_ = new gsl_vector_const_view(row ? 84 101 gsl_matrix_const_row (m.gsl_matrix_p(),i) : 85 102 gsl_matrix_const_column(m.gsl_matrix_p(),i) ); 86 v_ = const_cast<gsl_vector*>(&(const_view_->vector)); 87 } 88 89 90 91 vector::vector(std::istream& is, char sep) 92 throw (utility::IO_error,std::exception) 93 : view_(NULL), const_view_(NULL) 103 proxy_v_ = v_const_ = &(view_const_->vector); 104 } 105 106 107 vector::vector(std::istream& is, char sep) 108 throw (utility::IO_error, std::exception) 109 : v_const_(NULL), view_(NULL), view_const_(NULL) 94 110 { 95 111 // read the data file and store in stl vectors (dynamically … … 104 120 continue; 105 121 nof_rows++; 106 122 107 123 std::vector<double> v; 108 124 std::string element; … … 154 170 is.clear(std::ios::goodbit); 155 171 // convert the data to a gsl vector 156 v_ = gsl_vector_alloc(nof_rows*nof_columns);172 proxy_v_ = v_ = gsl_vector_alloc(nof_rows*nof_columns); 157 173 size_t n=0; 158 174 for (size_t i=0; i<nof_rows; i++) … … 162 178 163 179 164 165 180 vector::~vector(void) 166 181 { 167 182 if (view_) 168 183 delete view_; 169 else if ( const_view_)170 delete const_view_;184 else if (view_const_) 185 delete view_const_; 171 186 else if (v_) 172 187 gsl_vector_free(v_); 173 v_=NULL; 174 } 175 188 } 176 189 177 190 … … 179 192 { 180 193 gsl_vector* vec = gsl_vector_alloc(size()); 181 gsl_vector_memcpy(vec, v_);194 gsl_vector_memcpy(vec, proxy_v_); 182 195 return vec; 183 196 } 184 197 185 198 186 187 199 std::pair<double,double> vector::minmax(void) const 188 200 { 189 201 double min, max; 190 gsl_vector_minmax( v_,&min,&max);202 gsl_vector_minmax(proxy_v_, &min, &max); 191 203 return std::pair<double,double>(min,max); 192 204 } 193 205 194 206 195 196 207 std::pair<size_t,size_t> vector::minmax_index(void) const 197 208 { 198 209 size_t min_index, max_index; 199 gsl_vector_minmax_index( v_,&min_index,&max_index);210 gsl_vector_minmax_index(proxy_v_, &min_index, &max_index); 200 211 return std::pair<size_t,size_t>(min_index,max_index); 201 212 } 202 203 213 204 214 … … 209 219 sum += (*this)(i); 210 220 return( sum ); 211 } 221 } 222 212 223 213 224 bool vector::operator==(const vector& a) const … … 216 227 return false; 217 228 for (size_t i=0; i<size(); ++i) 218 if (gsl_vector_get( v_,i)!=a(i))229 if (gsl_vector_get(proxy_v_,i)!=a(i)) 219 230 return false; 220 231 return true; 221 232 } 222 223 233 224 234 … … 232 242 233 243 234 235 const vector& vector::operator=( const vector& other ) 244 const vector& vector::operator=( const vector& other ) 236 245 { 237 246 if( this != &other ) { 238 247 if (view_) 239 248 delete view_; 240 else if ( const_view_)241 delete const_view_;249 else if (view_const_) 250 delete view_const_; 242 251 else if ( v_ ) 243 252 gsl_vector_free( v_ ); 244 v_ = other.create_gsl_vector_copy(); 245 } 253 v_const_=NULL; 254 proxy_v_ = v_ = other.create_gsl_vector_copy(); 255 } 246 256 return *this; 247 257 } 248 249 258 250 259 … … 258 267 s << s.fill(); 259 268 } 260 261 269 return s; 262 270 } 263 271 264 265 }}} // of namespace utility, yat and thep 272 }}} // of namespace utility, yat, and thep -
trunk/yat/utility/vector.h
r680 r686 43 43 class matrix; 44 44 45 /// 46 /// This is the yat interface to GSL vector. 'double' is the 47 /// only type supported, maybe we should add a 'complex' type as 48 /// well in the future. 49 /// 50 /// \par[File streams] Reading and writing vectors to file streams 51 /// are of course supported. These are implemented without using GSL 52 /// functionality, and thus binary read and write to streams are not 53 /// supported. 54 /// 55 /// \par[Vector views] GSL vector views are supported and these are 56 /// disguised as ordinary utility::vectors. A support function is 57 /// added, utility::vector::isview(), that can be used to check if a 58 /// vector object is a view. Note that view vectors do not own the 59 /// undelying data, and a view is not valid if the vector owning the 60 /// data is deallocated. 61 /// 62 /// @note Missing support to underlying GSL vector features can in 63 /// principle be included to this class if requested by the user 64 /// community. 65 /// 45 /** 46 This is the yat interface to GSL vector. 'double' is the only 47 type supported, maybe we should add a 'complex' type as well in 48 the future. 49 50 \par File streams: 51 Reading and writing vectors to file streams are of course 52 supported. These are implemented without using GSL functionality, 53 and thus binary read and write to streams are not supported. 54 55 \par Vector views: 56 GSL vector views are supported and these are disguised as 57 ordinary utility::vectors. A support function is added, 58 utility::vector::isview(), that can be used to check if a vector 59 object is a view. Note that view vectors do not own the 60 underlying data, and a view is not valid if the vector owing the 61 data is deallocated. 62 63 \par 64 Currently there is no restriction on how a vector is used in 65 cases when the vector is a const view into another vector or 66 matrix. To avoid unexpected runtime errors, the programmer must 67 declare const view vectors as const in order to get compile time 68 diagnostics about improper use of a const view vector object. An 69 example of improper use of a const view vector is assignment of 70 values to an element in the object. If the const view object was 71 declared const the assigment will be caught by the compiler 72 whereas it will cause a (un-catchable) runtime error. Example: 73 @code 74 const vector vm(13,1.0); 75 vector v1(vm,2,4); // bad code! 76 v1(0)=-123; // accepted by compiler, runtime failure will occur 77 const vector v2(vm,2,4); 78 v2(0)=-123; // not acceptable for the compiler 79 @endcode 80 81 @note Missing support to underlying GSL vector features can in 82 principle be included to this class if requested by the user 83 community. 84 */ 85 66 86 class vector 67 87 { … … 71 91 /// The default constructor. 72 92 /// 73 inline vector(void) : v_(NULL), view_(NULL), const_view_(NULL) {}93 vector(void); 74 94 75 95 /// … … 77 97 /// sets all elements to \a init_value. 78 98 /// 79 inline vector(const size_t n,const double init_value=0) 80 : view_(NULL), const_view_(NULL) 81 { v_ = gsl_vector_alloc(n); set_all(init_value); } 99 vector(size_t n, double init_value=0); 82 100 83 101 /// … … 87 105 /// of the view will be copied, i.e. the view is not copied. 88 106 /// 89 inline vector(const vector& other) : view_(NULL), const_view_(NULL) 90 { v_ = other.create_gsl_vector_copy(); } 107 vector(const vector& other); 91 108 92 109 /// … … 178 195 /// end of input to the vector is at end of file marker. 179 196 /// 180 explicit vector(std::istream &, char sep='\0') throw (utility::IO_error,std::exception);181 197 explicit vector(std::istream &, char sep='\0') 198 throw(utility::IO_error, std::exception); 182 199 183 200 /// … … 215 232 /// @return A const pointer to the internal GSL vector, 216 233 /// 217 inline const gsl_vector* gsl_vector_p(void) const { return v_; }234 inline const gsl_vector* gsl_vector_p(void) const { return proxy_v_; } 218 235 219 236 /// … … 226 243 /// othwerwise; 227 244 /// 228 inline bool isnull(void) const { return gsl_vector_isnull( v_); }245 inline bool isnull(void) const { return gsl_vector_isnull(proxy_v_); } 229 246 230 247 /// … … 234 251 /// @return True if the object is a view, false othwerwise. 235 252 /// 236 inline bool isview(void) const { return view_ || const_view_; }253 inline bool isview(void) const { return view_ || view_const_; } 237 254 238 255 /// … … 240 257 /// 241 258 // Jari, doxygen group as Finding maximum and minimum elements 242 inline double max(void) const { return gsl_vector_max( v_); }259 inline double max(void) const { return gsl_vector_max(proxy_v_); } 243 260 244 261 /// … … 247 264 /// 248 265 // Jari, doxygen group as Finding maximum and minimum elements 249 inline size_t max_index(void) const { return gsl_vector_max_index(v_); } 266 inline size_t 267 max_index(void) const { return gsl_vector_max_index(proxy_v_); } 250 268 251 269 /// … … 253 271 /// 254 272 // Jari, doxygen group as Finding maximum and minimum elements 255 inline double min(void) const { return gsl_vector_min( v_); }273 inline double min(void) const { return gsl_vector_min(proxy_v_); } 256 274 257 275 /// … … 260 278 /// 261 279 // Jari, doxygen group as Finding maximum and minimum elements 262 inline size_t min_index(void) const { return gsl_vector_min_index(v_); } 280 inline size_t 281 min_index(void) const { return gsl_vector_min_index(proxy_v_); } 263 282 264 283 /// … … 338 357 /// @return the number of elements in the vector. 339 358 /// 340 inline size_t size(void) const { return v_->size; }359 inline size_t size(void) const { return proxy_v_->size; } 341 360 342 361 /// … … 346 365 /// 347 366 // Markus to Jari, doxygen group as Exchanging elements ???? 348 inline void sort(void) { gsl_sort_vector(v_);} 349 367 inline void sort(void) { gsl_sort_vector(v_); } 350 368 351 369 /// … … 396 414 // Jari, doxygen group as Accessing vector elements 397 415 inline const double& 398 operator()(size_t i) const { return *gsl_vector_const_ptr( v_,i); }416 operator()(size_t i) const { return *gsl_vector_const_ptr(proxy_v_,i); } 399 417 400 418 /// … … 413 431 // Jari, doxygen group as Accessing vector elements 414 432 inline const double& 415 operator[](size_t i) const { return *gsl_vector_const_ptr( v_,i); }433 operator[](size_t i) const { return *gsl_vector_const_ptr(proxy_v_,i); } 416 434 417 435 /// … … 481 499 482 500 gsl_vector* v_; 501 const gsl_vector* v_const_; 483 502 gsl_vector_view* view_; 484 gsl_vector_const_view* const_view_; 503 gsl_vector_const_view* view_const_; 504 const gsl_vector* proxy_v_; 485 505 }; 486 506 … … 491 511 492 512 493 }}} // of namespace utility, yat and theplu513 }}} // of namespace utility, yat, and theplu 494 514 495 515 #endif
Note: See TracChangeset
for help on using the changeset viewer.