Changeset 782 for trunk/yat/utility
- Timestamp:
- Mar 5, 2007, 10:17:31 PM (17 years ago)
- Location:
- trunk/yat/utility
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/utility/PCA.cc
r735 r782 142 142 utility::vector A_row_sum(A_.rows()); 143 143 for (size_t i=0; i<A_row_sum.size(); ++i) 144 A_row_sum(i)=utility:: vector(A_,i).sum();144 A_row_sum(i)=utility::sum(utility::vector(A_,i)); 145 145 for( size_t i = 0; i < A_center.rows(); ++i ) { 146 146 meanvalues_[i] = A_row_sum(i) / static_cast<double>(A_.columns()); -
trunk/yat/utility/vector.cc
r775 r782 229 229 230 230 231 bool vector::isnull(void) const232 {233 return gsl_vector_isnull(proxy_v_);234 }235 236 237 231 bool vector::isview(void) const 238 232 { 239 233 return view_ || view_const_; 240 }241 242 243 double vector::max(void) const244 {245 return gsl_vector_max(proxy_v_);246 }247 248 249 size_t vector::max_index(void) const250 {251 return gsl_vector_max_index(proxy_v_);252 }253 254 255 double vector::min(void) const256 {257 return gsl_vector_min(proxy_v_);258 }259 260 261 size_t vector::min_index(void) const262 {263 return gsl_vector_min_index(proxy_v_);264 }265 266 267 std::pair<double,double> vector::minmax(void) const268 {269 double min, max;270 gsl_vector_minmax(proxy_v_, &min, &max);271 return std::pair<double,double>(min,max);272 }273 274 275 std::pair<size_t,size_t> vector::minmax_index(void) const276 {277 size_t min_index, max_index;278 gsl_vector_minmax_index(proxy_v_, &min_index, &max_index);279 return std::pair<size_t,size_t>(min_index,max_index);280 234 } 281 235 … … 305 259 { 306 260 gsl_vector_set_all(v_,value); 307 }308 309 310 void vector::set_basis(const size_t i)311 {312 gsl_vector_set_basis(v_,i);313 }314 315 316 void vector::set_zero(void)317 {318 gsl_vector_set_zero(v_);319 261 } 320 262 … … 328 270 329 271 330 void vector::sort(void) 331 { 332 gsl_sort_vector(v_); 333 } 334 335 336 double vector::sum(void) const 337 { 338 double sum = 0; 339 for (size_t i=0; i<size(); i++) 340 sum += (*this)(i); 341 return( sum ); 342 } 343 344 345 void vector::swap(vector& other) 346 { 347 int status=gsl_vector_swap(v_,other.v_); 348 if (status) 349 throw utility::GSL_error(std::string("vector::swap",status)); 350 } 351 352 353 void vector::swap_elements(size_t i, size_t j) 272 void vector::swap(size_t i, size_t j) 354 273 { 355 274 int status=gsl_vector_swap_elements(v_, i, j); … … 459 378 460 379 380 bool isnull(const vector& v) 381 { 382 return gsl_vector_isnull(v.gsl_vector_p()); 383 } 384 385 386 double max(const vector& v) 387 { 388 return gsl_vector_max(v.gsl_vector_p()); 389 } 390 391 392 size_t max_index(const vector& v) 393 { 394 return gsl_vector_max_index(v.gsl_vector_p()); 395 } 396 397 398 double min(const vector& v) 399 { 400 return gsl_vector_min(v.gsl_vector_p()); 401 } 402 403 404 size_t min_index(const vector& v) 405 { 406 return gsl_vector_min_index(v.gsl_vector_p()); 407 } 408 409 410 std::pair<double,double> minmax(const vector& v) 411 { 412 std::pair<double,double> minmax; 413 gsl_vector_minmax(v.gsl_vector_p(), &minmax.first, &minmax.second); 414 return minmax; 415 } 416 417 418 std::pair<size_t,size_t> minmax_index(const vector& v) 419 { 420 std::pair<size_t,size_t> minmax; 421 gsl_vector_minmax_index(v.gsl_vector_p(), &minmax.first, &minmax.second); 422 return minmax; 423 } 424 425 426 void set_basis(vector& v, size_t i) 427 { 428 gsl_vector_set_basis(v.gsl_vector_p(),i); 429 } 430 431 432 void sort(vector& v) 433 { 434 gsl_sort_vector(v.gsl_vector_p()); 435 } 436 437 438 double sum(const vector& v) 439 { 440 double sum = 0; 441 size_t vsize=v.size(); 442 for (size_t i=0; i<vsize; ++i) 443 sum += v[i]; 444 return sum; 445 } 446 447 448 void swap(vector& v, vector& w) 449 { 450 int status=gsl_vector_swap(v.gsl_vector_p(),w.gsl_vector_p()); 451 if (status) 452 throw utility::GSL_error(std::string("swap(vector&,vector&)",status)); 453 } 454 455 461 456 std::ostream& operator<<(std::ostream& s, const vector& a) 462 457 { -
trunk/yat/utility/vector.h
r775 r782 62 62 63 63 \par 64 Currently there is no restriction on how a vector is used in65 cases whenthe vector is a const view into another vector or64 Currently there is no restriction on how a vector is used when 65 the vector is a const view into another vector or 66 66 matrix. To avoid unexpected runtime errors, the programmer must 67 67 declare const view vectors as const in order to get compile time … … 234 234 235 235 /// 236 /// @return True if all elements in the vector is zero, false237 /// othwerwise;238 ///239 bool isnull(void) const;240 241 ///242 236 /// Check if the vector object is a view (sub-vector) to another 243 237 /// vector. … … 247 241 bool isview(void) const; 248 242 249 ///250 /// @return The maximum value of the vector.251 ///252 double max(void) const;253 254 ///255 /// @return The element index to the maximum value of the256 /// vector. The lowest index has precedence.257 ///258 size_t max_index(void) const;259 260 ///261 /// @return The minimum value of the vector.262 ///263 double min(void) const;264 265 ///266 /// @return The element index to the minimum value of the267 /// vector. The lowest index has precedence.268 ///269 size_t min_index(void) const;270 271 ///272 /// @return The minimum and maximum values of the vector, as the273 /// \a first and \a second member of the returned \a pair,274 /// respectively.275 ///276 std::pair<double,double> minmax(void) const;277 278 ///279 /// @return The indecies to the minimum and maximum values of the280 /// vector, as the \a first and \a second member of the returned281 /// \a pair, respectively. The lowest index has precedence.282 ///283 std::pair<size_t,size_t> minmax_index(void) const;284 285 243 /** 286 244 \brief This function performs element-wise multiplication, \f$ … … 313 271 314 272 /// 315 /// Makes a basis vector by setting all elements to316 /// zero except the \a i-th element which is set to317 /// one.318 ///319 void set_basis(const size_t i);320 321 ///322 /// Set all elements to zero.323 ///324 void set_zero(void);325 326 ///327 273 /// @return the number of elements in the vector. 328 274 /// 329 275 size_t size(void) const; 330 276 331 /// 332 /// Sort the elements in the vector. 333 /// 334 /// Bug in gsl: if vector contains NaN an infinite loop is entered. 335 /// 336 void sort(void); 337 338 /// 339 /// Calculate the sum of all vector elements. 340 /// 341 /// @return The sum. 342 /// 343 double sum(void) const; 344 345 /** 346 \brief Swap vector elements by copying. 347 348 The two vectors must have the same length. 277 /** 278 \brief Exchange elements \a i and \a j. 349 279 350 280 \throw GSL_error if vector lengths differs. 351 281 */ 352 void swap(vector& other); 353 354 /** 355 \brief Exchange elements \a i and \a j. 356 357 \throw GSL_error if vector lengths differs. 358 */ 359 void swap_elements(size_t i, size_t j); 282 void swap(size_t i, size_t j); 360 283 361 284 /** … … 496 419 }; 497 420 498 /// 499 /// The output operator for the vector class. 500 /// 501 std::ostream& operator<<(std::ostream&, const vector& ); 502 421 /** 422 \brief Check if all elements of the vector are zero. 423 424 \return True if all elements in the vector is zero, false 425 othwerwise. 426 */ 427 bool isnull(const vector&); 428 429 /** 430 \brief Get the maximum value of the vector. 431 432 \return The maximum value of the vector. 433 */ 434 double max(const vector&); 435 436 /** 437 \brief Locate the maximum value in the vector. 438 439 \return The index to the maximum value of the vector. 440 441 \note Lower index has precedence. 442 */ 443 size_t max_index(const vector&); 444 445 /** 446 \brief Get the minimum value of the vector. 447 448 \return The minimum value of the vector. 449 */ 450 double min(const vector&); 451 452 /** 453 \brief Locate the minimum value in the vector. 454 455 \return The index to the minimum value of the vector. 456 457 \note Lower index has precedence. 458 */ 459 size_t min_index(const vector&); 460 461 /** 462 \brief Get the minimum and maximim values of the vector. 463 464 \return The minimum and maximum values of the vector, 465 respectively. 466 */ 467 std::pair<double,double> minmax(const vector&); 468 469 /** 470 \brief Locate the maximum and minumum element in the vector. 471 472 \return The indecies to the element with the minimum and maximum 473 values of the vector, respectively. 474 475 \note Lower index has precedence. 476 */ 477 std::pair<size_t,size_t> minmax_index(const vector&); 478 479 /** 480 \brief Transforms a vector to a basis vector. 481 482 All elements are set to zero except the \a i-th element which is 483 set to one. 484 */ 485 void set_basis(vector&, size_t i); 486 487 /** 488 Sort the elements in the vector. 489 490 \note Bug in GSL: if the vector contains one ore more NaNs the 491 call never returns. 492 */ 493 void sort(vector&); 494 495 /** 496 \brief Calculate the sum of all vector elements. 497 498 \return The sum. 499 */ 500 double sum(const vector&); 501 502 /** 503 \brief Swap vector elements by copying. 504 505 The two vectors must have the same length. 506 507 \throw GSL_error if vector lengths differs. 508 */ 509 void swap(vector&, vector&); 510 511 /** 512 \brief The output operator for the vector class. 513 */ 514 std::ostream& operator<<(std::ostream&, const vector&); 503 515 504 516 }}} // of namespace utility, yat, and theplu
Note: See TracChangeset
for help on using the changeset viewer.