Changeset 789 for trunk/yat


Ignore:
Timestamp:
Mar 10, 2007, 9:07:13 PM (15 years ago)
Author:
Jari Häkkinen
Message:

Addresses #193. vector now works as outlined here. Added some
functionality. Added a clone function that facilitates resizing of
vectors. clone is needed since assignement operator functionality is
changed.

Location:
trunk/yat
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/classifier/IGP.cc

    r680 r789  
    4545   
    4646    // Calculate IGP for each class
    47     igp_=utility::vector(target_.nof_classes());
     47    igp_.clone(utility::vector(target_.nof_classes()));
    4848    for(u_int i=0; i<target_.size(); i++) {
    4949      u_int neighbor=i;
  • trunk/yat/classifier/NCC.cc

    r722 r789  
    110110                    utility::vector& prediction) const
    111111  {
    112     prediction=utility::vector(centroids_.columns());   
     112    prediction.clone(utility::vector(centroids_.columns()));
    113113
    114114    utility::vector value(input.size(),0);
  • trunk/yat/classifier/utility.cc

    r779 r789  
    3434  void convert(const DataLookup1D& lookup, utility::vector& vector)
    3535  {
    36     vector=utility::vector(lookup.size());
     36    vector.clone(utility::vector(lookup.size()));
    3737    for(u_int i=0; i<lookup.size(); i++)
    3838      vector(i)=lookup(i);
     
    4343  {
    4444   
    45     value=utility::vector(lookup.size());
    46     weight=utility::vector(lookup.size());
     45    value.clone(utility::vector(lookup.size()));
     46    weight.clone(utility::vector(lookup.size()));
    4747    for(u_int i=0; i<lookup.size(); i++){
    4848      value(i)=lookup.data(i);
  • trunk/yat/regression/Local.cc

    r767 r789  
    5858
    5959    size_t nof_fits=data_.size()/step_size;
    60     x_= utility::vector(nof_fits);
    61     y_predicted_ = utility::vector(x_.size());
    62     y_err_ = utility::vector(x_.size());
     60    x_.clone(utility::vector(nof_fits));
     61    y_predicted_.clone(utility::vector(x_.size()));
     62    y_err_.clone(utility::vector(x_.size()));
    6363    sort(data_.begin(), data_.end());
    6464
  • trunk/yat/regression/MultiDimensional.cc

    r750 r789  
    5757    assert(x.rows()==y.size());
    5858    covariance_=utility::matrix(x.columns(),x.columns());
    59     fit_parameters_=utility::vector(x.columns());
     59    fit_parameters_.clone(utility::vector(x.columns()));
    6060    if (work_)
    6161      gsl_multifit_linear_free(work_);
  • trunk/yat/regression/MultiDimensionalWeighted.cc

    r750 r789  
    5959
    6060    covariance_=utility::matrix(x.columns(),x.columns());
    61     fit_parameters_=utility::vector(x.columns());
     61    fit_parameters_.clone(utility::vector(x.columns()));
    6262    if (work_)
    6363      gsl_multifit_linear_free(work_);
     
    8585  }
    8686
     87
    8788  double MultiDimensionalWeighted::predict(const utility::vector& x) const
    8889  {
     
    9091    return fit_parameters_ * x;
    9192  }
     93
    9294
    9395  double MultiDimensionalWeighted::prediction_error2(const utility::vector& x,
  • trunk/yat/statistics/Score.cc

    r779 r789  
    5454  {
    5555    assert(target.size()==value.size());
    56     utility::vector a;
     56    utility::vector a(value.size());
    5757    classifier::convert(value,a);
    5858    return score(target,a);
     
    6464  {
    6565    assert(target.size()==value.size());
    66     utility::vector a;
    67     utility::vector b;
     66    utility::vector a(value.size());
     67    utility::vector b(value.size());
    6868    classifier::convert(value,a,b);
    6969    return score(target,a,b);
     
    7575                      const classifier::DataLookup1D& weight) const
    7676  {
    77     utility::vector a;
     77    utility::vector a(value.size());
    7878    classifier::convert(value,a);
    79     utility::vector b;
     79    utility::vector b(value.size());
    8080    classifier::convert(weight,a);
    8181    return score(target,a,b);
  • trunk/yat/utility/PCA.cc

    r782 r789  
    6969    eigenvectors_ = U;
    7070    eigenvectors_ .transpose();
    71     eigenvalues_ = pSVD->s();
     71    eigenvalues_.clone(pSVD->s());
    7272
    7373    // T
     
    109109    eigenvectors_=V;
    110110    eigenvectors_.transpose();
    111     eigenvalues_ = pSVD->s();
     111    eigenvalues_.clone(pSVD->s());
    112112
    113113    // Transform back when done with SVD!
     
    139139  void PCA::row_center(utility::matrix& A_center)
    140140  {
    141     meanvalues_ = utility::vector( A_.rows() );
     141    meanvalues_.clone(utility::vector(A_.rows()));
    142142    utility::vector A_row_sum(A_.rows());
    143143    for (size_t i=0; i<A_row_sum.size(); ++i)
  • trunk/yat/utility/vector.cc

    r787 r789  
    3030
    3131#include <cassert>
     32#include <cmath>
    3233#include <iostream>
    3334#include <sstream>
     
    8384      throw utility::GSL_error("vector::vector failed to setup view");
    8485    proxy_v_ = &(view_const_->vector);
    85     const_cast<const gsl_vector*>(proxy_v_);
    8686  }
    8787
     
    210210
    211211
     212  const vector& vector::clone(const vector& other)
     213  {
     214    if (this!=&other) {
     215
     216      if (view_)
     217        delete view_;
     218      else if (view_const_)
     219        delete view_const_;
     220      else if (v_)
     221        gsl_vector_free(v_);
     222
     223      if (other.view_) {
     224        view_ = new gsl_vector_view(*other.view_);
     225        proxy_v_ = v_ = &(view_->vector);
     226      }
     227      else if (other.view_const_) {
     228        view_const_ = new gsl_vector_const_view(*other.view_const_);
     229        proxy_v_ = &(view_const_->vector);
     230        v_=NULL;
     231      }
     232      else if (other.v_)
     233        proxy_v_ = v_ = other.create_gsl_vector_copy();
     234
     235    }
     236    return *this;
     237  }
     238
     239
    212240  void vector::div(const vector& other)
    213241  {
     
    216244    if (status)
    217245      throw utility::GSL_error(std::string("vector::div",status));
     246  }
     247
     248
     249  bool vector::equal(const vector& other, const double d) const
     250  {
     251    if (this==&other)
     252      return true;
     253    if (size()!=other.size())
     254      return false;
     255    // if gsl error handler disabled, out of bounds index will not
     256    // abort the program.
     257    for (size_t i=0; i<size(); ++i)
     258      // The two last condition checks are needed for NaN detection
     259      if (fabs( (*this)(i)-other(i) ) > d ||
     260          (*this)(i)!=(*this)(i) || other(i)!=other(i))
     261        return false;
     262    return true;
    218263  }
    219264
     
    316361
    317362
    318   bool vector::operator==(const vector& a) const
    319   {
    320     if (size()!=a.size())
    321       return false;
    322     // if gsl error handler disabled, out of bounds index will not
    323     // abort the program.
    324     for (size_t i=0; i<size(); ++i)
    325       if (gsl_vector_get(proxy_v_,i)!=a(i))
    326         return false;
    327     return true;
     363  bool vector::operator==(const vector& other) const
     364  {
     365    return equal(other);
     366  }
     367
     368
     369  bool vector::operator!=(const vector& other) const
     370  {
     371    return !equal(other);
    328372  }
    329373
     
    341385  {
    342386    if( this != &other ) {
    343       if (view_)
    344         delete view_;
    345       else if (view_const_)
    346         delete view_const_;
    347       else if ( v_ )
    348         gsl_vector_free( v_ );
    349       proxy_v_ = v_ = other.create_gsl_vector_copy();
     387      if (size()!=other.size())
     388        throw utility::GSL_error("vector::operator= vector sizes differ");
     389      for (size_t i=0; i<size(); ++i)
     390        gsl_vector_set(v_, i, other(i));
    350391    }
    351392    return *this;
  • trunk/yat/utility/vector.h

    r787 r789  
    219219
    220220    /**
     221       \brief Make a copy of \a other.
     222
     223       This function will make a deep copy of \a other. Memory is
     224       resized and view state is changed if needed.
     225    */
     226    const vector& clone(const vector& other);
     227
     228    /**
    221229       \brief This function performs element-wise division, \f$ this_i =
    222230       this_i/other_i \; \forall i \f$.
     
    225233    */
    226234    void div(const vector& other);
     235
     236    /**
     237       \brief Check whether vectors are equal within a user defined
     238       precision, set by \a precision.
     239
     240       \return True if each element deviates less or equal than \a
     241       d. If any vector contain a NaN, false is always returned.
     242
     243       \see operator== and operator!=
     244    */
     245    bool equal(const vector&, const double precision=0) const;
    227246
    228247    ///
     
    321340    const double& operator[](size_t i) const;
    322341
    323     ///
    324     /// Comparison operator. Takes linear time.
    325     ///
    326     /// @return True if the sequence of the elements in the vectors
    327     /// are element/wise equal.
    328     ///
     342    /**
     343       \brief Comparison operator. Takes linear time.
     344
     345       Checks are performed with exact matching, i.e., rounding off
     346       effects may destroy comparison. Use the equal function for
     347       comparing elements within a user defined precision.
     348
     349       \return True if all elements are equal otherwise false.
     350
     351       \see equal
     352    */
    329353    bool operator==(const vector&) const;
     354
     355    /**
     356       \brief Comparison operator. Takes linear time.
     357
     358       Checks are performed with exact matching, i.e., rounding off
     359       effects may destroy comparison. Use the equal function for
     360       comparing elements within a user defined precision.
     361
     362       \return False if all elements are equal otherwise true.
     363
     364       \see equal
     365    */
     366    bool operator!=(const vector&) const;
    330367
    331368    ///
Note: See TracChangeset for help on using the changeset viewer.