Changeset 782 for trunk/yat/utility


Ignore:
Timestamp:
Mar 5, 2007, 10:17:31 PM (17 years ago)
Author:
Jari Häkkinen
Message:

Fixes #195.

Location:
trunk/yat/utility
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/PCA.cc

    r735 r782  
    142142    utility::vector A_row_sum(A_.rows());
    143143    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));
    145145    for( size_t i = 0; i < A_center.rows(); ++i ) {
    146146      meanvalues_[i] = A_row_sum(i) / static_cast<double>(A_.columns());
  • trunk/yat/utility/vector.cc

    r775 r782  
    229229
    230230
    231   bool vector::isnull(void) const
    232   {
    233     return gsl_vector_isnull(proxy_v_);
    234   }
    235 
    236 
    237231  bool vector::isview(void) const
    238232  {
    239233    return view_ || view_const_;
    240   }
    241 
    242 
    243   double vector::max(void) const
    244   {
    245     return gsl_vector_max(proxy_v_);
    246   }
    247 
    248 
    249   size_t vector::max_index(void) const
    250   {
    251     return gsl_vector_max_index(proxy_v_);
    252   }
    253 
    254 
    255   double vector::min(void) const
    256   {
    257     return gsl_vector_min(proxy_v_);
    258   }
    259 
    260 
    261   size_t vector::min_index(void) const
    262   {
    263     return gsl_vector_min_index(proxy_v_);
    264   }
    265 
    266 
    267   std::pair<double,double> vector::minmax(void) const
    268   {
    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) const
    276   {
    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);
    280234  }
    281235
     
    305259  {
    306260    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_);
    319261  }
    320262
     
    328270
    329271
    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)
    354273  {
    355274    int status=gsl_vector_swap_elements(v_, i, j);
     
    459378
    460379
     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
    461456  std::ostream& operator<<(std::ostream& s, const vector& a)
    462457  {
  • trunk/yat/utility/vector.h

    r775 r782  
    6262
    6363     \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
     64     Currently there is no restriction on how a vector is used when
     65     the vector is a const view into another vector or
    6666     matrix. To avoid unexpected runtime errors, the programmer must
    6767     declare const view vectors as const in order to get compile time
     
    234234
    235235    ///
    236     /// @return True if all elements in the vector is zero, false
    237     /// othwerwise;
    238     ///
    239     bool isnull(void) const;
    240 
    241     ///
    242236    /// Check if the vector object is a view (sub-vector) to another
    243237    /// vector.
     
    247241    bool isview(void) const;
    248242
    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 the
    256     /// 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 the
    267     /// 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 the
    273     /// \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 the
    280     /// vector, as the \a first and \a second member of the returned
    281     /// \a pair, respectively. The lowest index has precedence.
    282     ///
    283     std::pair<size_t,size_t> minmax_index(void) const;
    284 
    285243    /**
    286244       \brief This function performs element-wise multiplication, \f$
     
    313271
    314272    ///
    315     /// Makes a basis vector by setting all elements to
    316     /// zero except the \a i-th element which is set to
    317     /// 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     ///
    327273    /// @return the number of elements in the vector.
    328274    ///
    329275    size_t size(void) const;
    330276
    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.
    349279
    350280       \throw GSL_error if vector lengths differs.
    351281    */
    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);
    360283
    361284    /**
     
    496419  };
    497420
    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&);
    503515
    504516}}} // of namespace utility, yat, and theplu
Note: See TracChangeset for help on using the changeset viewer.