Changeset 1008


Ignore:
Timestamp:
Feb 1, 2008, 4:49:13 AM (13 years ago)
Author:
Peter
Message:

fixing tests for new vector design

Location:
branches/peter-dev
Files:
4 deleted
17 edited

Legend:

Unmodified
Added
Removed
  • branches/peter-dev/test/matrix_test.cc

    r865 r1008  
    3636    : m_(i,j,value) {}
    3737
    38   inline theplu::yat::utility::vector
    39   row(const size_t& i) { return theplu::yat::utility::vector(m_,i); }
     38  inline theplu::yat::utility::vectorView
     39  row(const size_t& i) { return m_.row_vec(i); }
    4040
    4141  inline const theplu::yat::utility::matrix& matrix(void) const { return m_; }
     
    258258    for (size_t j=0; j<m5.columns(); ++j)
    259259      m5_sum2+=m5(i,j);
    260   utility::vector v5subrow(m5,3);
     260  utility::vectorView v5subrow(m5,3);
    261261  double v5subrow_sum=0;
    262262  for (size_t i=0; i<v5subrow.size(); ++i) {
     
    280280    for (size_t j=0; j<m5.columns(); ++j)
    281281      m5_sum3+=m5(i,j);
    282   utility::vector v5subcolumn(m5,0,false);
     282  utility::vectorView v5subcolumn = m5.column_vec(0);
    283283  double v5subcolumn_sum=0;
    284284  for (size_t i=0; i<v5subcolumn.size(); ++i) {
     
    332332  *error << "\tthat class member returns a view" << std::endl;
    333333  matrixwrapper mw(5,2);
    334   utility::vector mwrow=mw.row(2);
     334  utility::vectorView mwrow=mw.row(2);
    335335  if (mwrow.gsl_vector_p()->data != &(mw.matrix()(2,0))) {
    336336    ok=false;
  • branches/peter-dev/test/pca_test.cc

    r865 r1008  
    3333#include <vector>
    3434
    35 
    3635using namespace theplu::yat;
    3736int main()
  • branches/peter-dev/test/score_test.cc

    r865 r1008  
    3232#include "yat/utility/matrix.h"
    3333#include "yat/utility/vector.h"
     34#include "yat/utility/vectorView.h"
    3435
    3536#include <cmath>
     
    9293  const double tol = 0.001;
    9394  for (size_t i=0; i<data.rows(); i++){
    94     utility::vector vec(data,i);
     95    utility::vectorView vec(data,i);
    9596    if (vec.size()!=target2.size()){
    9697      *error << "vec.size() is " << vec.size() << " and target2.size() is "
     
    108109  utility::vector weight(target2.size(),1);
    109110  for (size_t i=0; i<data.rows(); i++){
    110     utility::vector vec(data,i);
     111    utility::vectorView vec = data.row_vec(i);
    111112    area = auc.score(target2, vec, weight);
    112113    if (area<correct_area(i)-tol || area>correct_area(i)+tol){
  • branches/peter-dev/test/vector_test.cc

    r995 r1008  
    2929#include "yat/utility/utility.h"
    3030#include "yat/utility/vector.h"
     31#include "yat/utility/vectorView.h"
    3132
    3233#include <fstream>
     
    7475  for (size_t i=0; i<vec.size(); i+=2)
    7576    sum_before+=vec[i];
    76   utility::vector vec_view(vec,0,6,2);
     77  utility::vectorView vec_view(vec,0,6,2);
    7778  sum_after=utility::sum(vec_view);
    7879  ok &= (sum_after==sum_before);
     
    8889    *message << "const view implementation" << std::endl;
    8990    const utility::vector vv(10,3.0);
    90     utility::vector vview(vv,0,5,1);
     91    utility::vectorView vview(vv,0,5,1);
    9192    // const utility::vector vview(vv,0,5,1); // this is the proper line
    9293    utility::vector vv2(5,2.0);
    9394    vv2.mul(vview); // should work even without const since const arg passing
    9495    vv2.div(vview); // should work even without const since const arg passing
     96    *message << "end view" << std::endl;
    9597    ok &= (vview*vview == 3.0*3.0*vview.size());
    96   }
    97 
     98    *message << "end view" << std::endl;
     99  }
     100  return 0;
     101
     102  *message << "end view" << std::endl;
    98103  // checking that copy constructor creates an independent object when
    99104  // a non-view vector is copied
     
    114119    ok &= (vec_view.size()==vec3.size());
    115120    ok &= (vec3 == vec_view);
    116     ok &= (&vec3 != &vec_view);
    117121    ok &= !vec3.isview();
    118122  }
    119123
     124  /* different sizes are allowed in new design
    120125  // checking that assignment operator throws an exception if vectors
    121126  // differ in size
     
    141146    gsl_set_error_handler(err_handler);
    142147  }
     148  */
    143149
    144150  // checking that assignment operator changes the underlying object when
     
    147153    *message << "assignment operator on view" << std::endl;
    148154    vec[3]=vec[4]=vec[5]=13;
    149     utility::vector vec_view(vec,3,3,1);
     155    utility::vectorView vec_view(vec,3,3,1);
    150156    utility::vector vec2(3,123.0);
    151157    vec_view=vec2;
    152     if (vec[3]!=vec_view[0] || vec[4]!=vec_view[1] || vec[5]!=vec_view[2])
    153       ok=false;
     158    if (vec[3]!=vec_view[0] || vec[4]!=vec_view[1] || vec[5]!=vec_view[2]){
     159      *message << " failed\n";
     160      ok=false;
     161    }
    154162  }
    155163
     
    160168    *message << "\tcloning normal vector" << std::endl;
    161169    utility::vector vec2(3,123.0);
    162     vec2.clone(vec);
     170    vec2 = vec;
    163171    if (vec.size()!=vec2.size())
    164172      this_ok=false;
     
    170178      this_ok=false;
    171179    *message << "\tcloning vector view" << std::endl;
    172     utility::vector* vec_view=new utility::vector(vec,3,3,1);
    173     utility::vector vec_view2;
    174     vec_view2.clone(*vec_view);
     180    utility::vectorView* vec_view=new utility::vectorView(vec,3,3,1);
     181    utility::vectorView vec_view2(*vec_view);
    175182    if (!vec_view2.isview())
    176183      this_ok=false;
  • branches/peter-dev/yat/classifier/IGP.h

    r936 r1008  
    8686   
    8787    // Calculate IGP for each class
    88     igp_.clone(utility::vector(target_.nof_classes()));
     88    igp_ = utility::vector(target_.nof_classes());
    8989   
    9090    for(u_int i=0; i<target_.size(); i++) {
  • branches/peter-dev/yat/classifier/KNN.h

    r948 r1008  
    240240    for(size_t sample=0;sample<distances->columns();sample++) {
    241241      std::vector<size_t> k_index;
    242       utility::sort_smallest_index(k_index,k_,utility::vector(*distances,sample,false));
     242      utility::sort_smallest_index(k_index,k_,distances->column_vec(sample));
    243243      for(size_t j=0;j<k_index.size();j++) {
    244244        prediction(target_(k_index[j]),sample)++;
  • branches/peter-dev/yat/classifier/NBC.cc

    r995 r1008  
    207207    for (size_t i=0; i<prediction.rows(); ++i){
    208208      prediction.row_vec(i) *=
    209         1.0/sum(prediction.row_vec(i));
     209        1.0/sum(
     210                prediction.row_vec(i)
     211                );
    210212    }
    211213  }
  • branches/peter-dev/yat/utility/Makefile.am

    r995 r1008  
    2929  matrix.cc NNI.cc Option.cc OptionFile.cc OptionHelp.cc OptionSwitch.cc \
    3030  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  vectorBase.cc vectorView.cc WeNNI.cc
    3232
    3333include_utilitydir = $(includedir)/yat/utility
     
    3838  IteratorWeighted.h kNNI.h matrix.h NNI.h \
    3939  Option.h OptionArg.h OptionFile.h OptionHelp.h OptionSwitch.h \
    40   PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h
    41   vectorBase.h vectorMutable.h vectorView.h vectorConstView.h
     40  PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h \
     41  vectorBase.h vectorView.h \
    4242  WeNNI.h yat_assert.h
    4343
  • branches/peter-dev/yat/utility/PCA.cc

    r995 r1008  
    2929#include "SVD.h"
    3030#include "utility.h"
    31 #include "vectorConstView.h"
     31#include "vectorView.h"
    3232
    3333#include <iostream>
     
    193193    utility::vector A_row_sum(A_.rows());
    194194    for (size_t i=0; i<A_row_sum.size(); ++i){
    195       vectorConstView tmp;
    196       tmp = A_.const_row(i);
     195      const vectorView tmp(A_.row_vec(i));
    197196      A_row_sum(i) = sum(tmp);
    198197    }
  • branches/peter-dev/yat/utility/matrix.cc

    r995 r1008  
    2828#include "vector.h"
    2929#include "vectorView.h"
    30 #include "vectorConstView.h"
    3130#include "utility.h"
    3231
     
    311310
    312311
    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);
     312  const vectorView matrix::column_vec(size_t col) const
     313  {
     314    return vectorView(*this, col, false);
     315  }
     316
     317
     318  const vectorView matrix::row_vec(size_t col) const
     319  {
     320    return vectorView(*this, col, true);
    322321  }
    323322
     
    563562    utility::vector res(m.rows());
    564563    for (size_t i=0; i<res.size(); ++i)
    565       res(i) = vectorConstView(m,i) * v;
     564      res(i) = vectorView(m,i) * v;
    566565    return res;
    567566  }
     
    572571    utility::vector res(m.columns());
    573572    for (size_t i=0; i<res.size(); ++i)
    574       res(i) = v * vectorConstView(m,i,false);
     573      res(i) = v * vectorView(m,i,false);
    575574    return res;
    576575  }
  • branches/peter-dev/yat/utility/matrix.h

    r995 r1008  
    3030#include "vector.h"
    3131#include "vectorView.h"
    32 #include "vectorConstView.h"
    3332#include "Exception.h"
    3433
     
    160159    vectorView column_vec(size_t);
    161160
     161    /**
     162     */
     163    const vectorView column_vec(size_t) const;
     164
    162165    ///
    163166    /// @return The number of columns in the matrix.
    164167    ///
    165168    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;
    174169
    175170    /**
     
    238233     */
    239234    vectorView row_vec(size_t);
     235
     236    /**
     237     */
     238    const vectorView row_vec(size_t) const;
    240239
    241240    /**
  • branches/peter-dev/yat/utility/vector.cc

    r995 r1008  
    4444
    4545  vector::vector(void)
    46     : vectorMutable(NULL)
     46    : vectorBase()
    4747  {
    4848  }
     
    5050
    5151  vector::vector(size_t n, double init_value)
    52     : vectorMutable(gsl_vector_alloc(n))
     52    : vectorBase(gsl_vector_alloc(n))
    5353  {
    5454    if (!vec_)
    5555      throw utility::GSL_error("vector::vector failed to allocate memory");
    56 
     56    assert(const_vec_);
    5757    all(init_value);
    5858  }
     
    6060
    6161  vector::vector(const vector& other)
    62     : vectorMutable(create_gsl_vector_copy(other))
     62    : vectorBase(create_gsl_vector_copy(other))
    6363  {
    6464  }
     
    6666
    6767  vector::vector(const vectorBase& other)
    68     : vectorMutable(create_gsl_vector_copy(other))
     68    : vectorBase(create_gsl_vector_copy(other))
    6969  {
    7070  }
     
    7373  vector::vector(std::istream& is, char sep)
    7474    throw (utility::IO_error, std::exception)
    75     : vectorMutable(NULL)
     75    : vectorBase()
    7676  {
    7777    // read the data file and store in stl vectors (dynamically
     
    145145      for (size_t j=0; j<nof_columns; j++)
    146146        gsl_vector_set( vec_, n++, data_matrix[i][j] );
     147    const_vec_ = vec_;
    147148  }
    148149
     
    150151  vector::~vector(void)
    151152  {
    152     gsl_vector_free(vec_);
     153    delete_allocated_memory();
    153154  }
    154155
     
    165166
    166167
     168  void vector::delete_allocated_memory(void)
     169  {
     170    if (vec_)
     171      gsl_vector_free(vec_);
     172    const_vec_ = vec_ = NULL;
     173  }
     174
     175
     176  bool vector::isview(void) const
     177  {
     178    return false;
     179  }
     180
     181
    167182  void vector::resize(size_t n, double init_value)
    168183  {
    169     gsl_vector_free(vec_);
    170     vec_ = gsl_vector_alloc(n);
     184    delete_allocated_memory();
     185    const_vec_ = vec_ = gsl_vector_alloc(n);
    171186    if (!vec_)
    172187      throw utility::GSL_error("vector::vector failed to allocate memory");
    173188    all(init_value);
     189   
    174190  }
    175191
    176192
     193  /*
    177194  //Peter use swap idiom
    178   const vector& vector::operator=( const vectorBase& other )
     195  const vectorBase& vector::operator=( const vectorBase& other )
    179196  {
    180197    if (!other.size())
     
    187204    return *this;
    188205  }
    189 
    190 
    191   //Peter use swap idiom
    192   const vectorMutable& vector::operator=( vectorMutable& other )
     206  */
     207
     208  const vectorBase& vector::operator=( const vector& other )
     209  {
     210    return assign(other);
     211  }
     212
     213  /* 
     214  const vectorBase& vector::assign(vectorBase& other)
     215  {
     216    assign(other);
     217  }
     218  */
     219
     220  const vectorBase& vector::assign(const vectorBase& other)
    193221  {
    194222    if (!other.size())
     
    199227      gsl_vector_memcpy(vec_,other.gsl_vector_p());
    200228    }
     229    const_vec_ = vec_;
    201230    return *this;
    202231  }
  • branches/peter-dev/yat/utility/vector.h

    r995 r1008  
    2929*/
    3030
    31 #include "vectorMutable.h"
     31#include "vectorBase.h"
    3232#include "Exception.h"
    3333#include "Iterator.h"
     
    8585  */
    8686
    87   class vector : public vectorMutable
     87  class vector : public vectorBase
    8888  {
    8989  public:
     
    145145
    146146    /**
     147       \return false
     148    */
     149    bool isview(void) const;
     150
     151    /**
    147152       @brief Resize vector
    148153       
     
    154159    void resize(size_t n, double init_value);
    155160
     161    //    using vectorBase::operator=;
     162
    156163    /**
    157164       \brief The assignment operator.
     
    159166       \return A const reference to the resulting vector.
    160167    */
    161     const vectorMutable& operator=(vectorMutable&);
    162     const vector& operator=(const vectorBase&);
     168    //const vectorBase& operator=(const vectorBase&);
     169    const vectorBase& operator=(const vector&);
    163170
    164171  private:
     172    const vectorBase& assign(const vectorBase& other);
     173    //const vectorBase& assign(vectorBase& other);
    165174
    166175    /**
     
    177186    gsl_vector* create_gsl_vector_copy(const vectorBase&) const;
    178187
     188    void delete_allocated_memory(void);
    179189  };
    180190
  • branches/peter-dev/yat/utility/vectorBase.cc

    r995 r1008  
    4343
    4444
     45  vectorBase::vectorBase(void)
     46    : vec_(NULL), const_vec_(NULL)
     47  {
     48  }
     49
     50
     51  vectorBase::vectorBase(gsl_vector* v)
     52    : vec_(v), const_vec_(v)
     53  {
     54  }
     55
     56
    4557  vectorBase::vectorBase(const gsl_vector* v)
    46     : const_vec_(v)
     58    : vec_(NULL), const_vec_(v)
    4759  {
    4860  }
     
    5466
    5567
     68  void vectorBase::all(double value)
     69  {
     70    assert(vec_);
     71    gsl_vector_set_all(vec_,value);
     72  }
     73
     74
    5675  vectorBase::const_iterator vectorBase::begin(void) const
    5776  {
    58     return vectorBase::const_iterator(*this, 0);
     77    return const_iterator(*this, 0);
     78  }
     79
     80
     81  vectorBase::iterator vectorBase::begin(void)
     82  {
     83    return iterator(*this, 0);
     84  }
     85
     86
     87  void vectorBase::div(const vectorBase& other)
     88  {
     89    assert(vec_);
     90    int status=gsl_vector_div(vec_,other.gsl_vector_p());
     91    if (status)
     92      throw utility::GSL_error(std::string("vectorBase::div",status));
    5993  }
    6094
     
    6296  vectorBase::const_iterator vectorBase::end(void) const
    6397  {
    64     return vectorBase::const_iterator(*this, size());
     98    return const_iterator(*this, size());
     99  }
     100
     101
     102  vectorBase::iterator vectorBase::end(void)
     103  {
     104    return iterator(*this, size());
    65105  }
    66106
     
    88128
    89129
     130  gsl_vector* vectorBase::gsl_vector_p(void)
     131  {
     132    return vec_;
     133  }
     134
     135
     136  void vectorBase::mul(const vectorBase& other)
     137  {
     138    assert(vec_);
     139    int status=gsl_vector_mul(vec_,other.gsl_vector_p());
     140    if (status)
     141      throw utility::GSL_error(std::string("vectorBase::div",status));
     142  }
     143
     144
    90145  size_t vectorBase::size(void) const
    91146  {
    92     if (const_vec_)
     147    if (!const_vec_)
    93148      return 0;
    94149    return const_vec_->size;
     
    105160
    106161
     162  double& vectorBase::operator()(size_t i)
     163  {
     164    double* d=gsl_vector_ptr(vec_, i);
     165    if (!d)
     166      throw utility::GSL_error("vectorBase::operator()",GSL_EINVAL);
     167    return *d;
     168  }
     169
     170
     171  double& vectorBase::operator[](size_t i)
     172  {
     173    return this->operator()(i);
     174  }
     175
     176
    107177  const double& vectorBase::operator[](size_t i) const
    108178  {
    109     const double* d=gsl_vector_const_ptr(const_vec_, i);
    110     if (!d)
    111       throw utility::GSL_error("vectorBase::operator[]",GSL_EINVAL);
    112     return *d;
     179    return this->operator()(i);
    113180  }
    114181
     
    132199      res += other(i) * (*this)(i);
    133200    return res;
     201  }
     202
     203
     204  const vectorBase& vectorBase::operator+=(double d)
     205  {
     206    assert(vec_);
     207    gsl_vector_add_constant(vec_, d);
     208    return *this;
     209  }
     210
     211
     212  const vectorBase& vectorBase::operator-=(const vectorBase& other)
     213  {
     214    assert(vec_);
     215    int status=gsl_vector_sub(vec_, other.gsl_vector_p());
     216    if (status)
     217      throw utility::GSL_error(std::string("vectorBase::sub", status));
     218    return *this;
     219  }
     220
     221
     222  const vectorBase& vectorBase::operator-=(const double d)
     223  {
     224    assert(vec_);
     225    gsl_vector_add_constant(vec_, -d);
     226    return *this;
     227  }
     228
     229
     230  const vectorBase& vectorBase::operator*=(double d)
     231  {
     232    assert(vec_);
     233    gsl_vector_scale(vec_, d);
     234    return *this;
    134235  }
    135236
     
    179280
    180281
     282  void shuffle(vectorBase& invec)
     283  {
     284    random::DiscreteUniform rnd;
     285    std::random_shuffle(invec.begin(), invec.end(), rnd);
     286  }
     287
     288
    181289  void sort_index(std::vector<size_t>& sort_index, const vectorBase& invec)
    182290  {
  • branches/peter-dev/yat/utility/vectorBase.h

    r995 r1008  
    1010  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér
    1111  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
     12  Copyright (C) 2008 Peter Johansson
    1213
    1314  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
     
    8990  public:
    9091
     92    /// \brief vectorBase::iterator
     93    typedef Iterator<double&, vectorBase> iterator;
     94
    9195    /// \brief vectorBase::const_iterator
    9296    typedef Iterator<const double, const vectorBase> const_iterator;
    9397
    9498    /**
     99       \brief default constructor
     100    */
     101    vectorBase(void);
     102
     103    /**
    95104       \brief Constructor.
    96105    */
     106    vectorBase(gsl_vector*);
     107
     108    /**
     109       \brief Constructor.
     110    */
    97111    vectorBase(const gsl_vector*);
    98112
     
    102116    virtual ~vectorBase(void);
    103117
     118    ///
     119    /// Set all elements to \a value.
     120    ///
     121    void all(double value);
     122
     123    /**
     124       \return mutable iterator to start of vectorBase
     125     */
     126    iterator begin(void);
     127
    104128    /**
    105129       \return read-only iterator to start of vectorBase
    106130     */
    107131    const_iterator begin(void) const;
     132
     133    /**
     134       \brief This function performs element-wise division, \f$ this_i =
     135       this_i/other_i \; \forall i \f$.
     136
     137       \throw GSL_error if dimensions mis-match.
     138    */
     139    void div(const vectorBase& other);
     140
     141    /**
     142       \return mutable iterator to end of vectorBase
     143     */
     144    iterator end(void);
    108145
    109146    /**
     
    124161
    125162    ///
     163    /// @return A pointer to the internal GSL vector,
     164    ///
     165    gsl_vector* gsl_vector_p(void);
     166
     167    ///
    126168    /// @return A const pointer to the internal GSL vector,
    127169    ///
    128170    const gsl_vector* gsl_vector_p(void) const;
    129171
     172    /**
     173   
     174       Check if the vector object is a view (sub-vector) to another
     175       vector.
     176   
     177       \return True if the object is a view, false othwerwise.
     178   
     179     */
     180    virtual bool isview(void) const=0;
     181
     182    /**
     183       \brief This function performs element-wise multiplication, \f$
     184       this_i = this_i * other_i \; \forall i \f$.
     185
     186       \throw GSL_error if dimensions mis-match.
     187    */
     188    void mul(const vectorBase& other);
     189
     190    /**
     191       \brief Reverse the order of elements in the vectorBase.
     192    */
     193    void reverse(void);
     194
    130195    ///
    131196    /// @return the number of elements in the vectorBase.
     
    134199
    135200    /**
     201       \brief Exchange elements \a i and \a j.
     202
     203       \throw GSL_error if vectorBase lengths differs.
     204    */
     205    void swap(size_t i, size_t j);
     206
     207    /**
    136208       \brief Element access operator.
    137209
    138        \return Const reference to element \a i.
     210       \return Reference to element \a i.
    139211
    140212       \throw If GSL range checks are enabled in the underlying GSL
     
    142214       of range.
    143215    */
     216    double& operator()(size_t i);
     217    // Peter, remove this one
     218    double& operator[](size_t i);
     219
     220    /**
     221       \brief Element access operator.
     222
     223       \return Const reference to element \a i.
     224
     225       \throw If GSL range checks are enabled in the underlying GSL
     226       library a GSL_error exception is thrown if either index is out
     227       of range.
     228    */
    144229    const double& operator()(size_t i) const;
     230    // Peter, remove this one
    145231    const double& operator[](size_t i) const;
    146232
     
    176262    double operator*(const vectorBase&) const;
    177263
     264    /**
     265       \brief The assignment operator.
     266
     267       Dimensions of the vectorBases must match. If the LHS vectorBase is a
     268       view, the underlying data will be changed.
     269
     270       \return A const reference to the resulting vectorBase.
     271
     272       \see void set(const vectorBase&).
     273
     274       \throw GSL_error if dimensions mis-match.
     275    */
     276    //const vectorBase& operator=(vectorBase&);
     277
     278    /**
     279       \brief Addition and assign operator. VectorBase addition, \f$
     280       this_i = this_i + other_i \; \forall i \f$.
     281
     282       \return A const reference to the resulting vectorBase.
     283
     284       \throw GSL_error if dimensions mis-match.
     285    */
     286    const vectorBase& operator+=(const vectorBase&);
     287
     288    /**
     289       \brief Add a constant to a vectorBase, \f$ this_i = this_i + d \;
     290       \forall i \f$.
     291
     292       \return A const reference to the resulting vectorBase.
     293    */
     294    const vectorBase& operator+=(double d);
     295
     296    /**
     297       \brief Subtract and assign operator. VectorBase subtraction, \f$
     298       this_i = this_i - other_i \; \forall i \f$.
     299
     300       \return A const reference to the resulting vectorBase.
     301
     302       \throw GSL_error if dimensions mis-match.
     303    */
     304    const vectorBase& operator-=(const vectorBase&);
     305
     306    /**
     307       \brief Subtract a constant to a vectorBase, \f$ this_i = this_i - d
     308       \; \forall i \f$.
     309
     310       \return A const reference to the resulting vectorBase.
     311    */
     312    const vectorBase& operator-=(double d);
     313
     314    /**
     315       \brief Multiply with scalar and assign operator, \f$ this_i =
     316       this_i * d \; \forall i \f$.
     317
     318       \return A const reference to the resulting vectorBase.
     319    */
     320    const vectorBase& operator*=(double d);
     321
     322
     323
    178324  protected:
     325    gsl_vector* vec_;
    179326    const gsl_vector* const_vec_;
     327
     328    /*
     329    struct proxy
     330    {
     331      gsl_vector* vec_;
     332    };
     333    */
     334
     335  private:
     336    // copy assignment no allowed
     337    const vectorBase& operator=(const vectorBase&);
     338  public:
     339    /**
     340     */
     341    //operator proxy();
     342
    180343  };
    181344
     
    236399
    237400  /**
    238      Create a vectorBase \a sort_index containing the indeces of
     401     Randomly shuffles the elements in vectorBase \a invec
     402  */
     403  void shuffle(vectorBase& invec);
     404
     405  /**
     406     Sort the elements in the vectorBase.
     407  */
     408  void sort(vectorBase&);
     409
     410  /**
     411     Create a vector \a sort_index containing the indeces of
    239412     elements in a another vectorBase \a invec.  The elements of \a
    240413     sort_index give the index of the vectorBase element which would
     
    251424  smallest elements in \a invec. 
    252425  */
    253   void sort_smallest_index(std::vector<size_t>& sort_index, size_t k, const
    254   vectorBase& invec);
     426  void sort_smallest_index(std::vector<size_t>& sort_index, size_t k,
     427                           const vectorBase& invec);
    255428
    256429  /**
     
    258431  largest elements in \a invec. 
    259432  */
    260   void sort_largest_index(std::vector<size_t>& sort_index, size_t k, const
    261   vectorBase& invec);
    262 
    263  
     433  void sort_largest_index(std::vector<size_t>& sort_index, size_t k,
     434                          const vectorBase& invec);
    264435
    265436  /**
     
    271442
    272443  /**
     444     \brief Swap vector elements by copying.
     445
     446     The two vectorMutables must have the same length.
     447
     448     \throw GSL_error if vectorMutable lengths differs.
     449  */
     450  //void swap(vectorMutable&, vectorMutable&);
     451
     452  /**
    273453     \brief The output operator for the vectorBase class.
    274454  */
  • branches/peter-dev/yat/utility/vectorView.cc

    r995 r1008  
    4444
    4545  vectorView::vectorView(void)
    46     : vectorMutable(NULL)
    47   {
    48   }
    49 
    50 
    51   vectorView::vectorView(vectorMutable& other)
    52     : vectorMutable(NULL), view_(NULL)
    53   {
    54     view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(),0,
    55                                                      other.size()));
    56     const_vec_ = vec_ = &(view_->vector);
    57   }
    58 
    59 
    60   vectorView::vectorView(vectorMutable& v, size_t offset,size_t n,size_t stride)
    61     : vectorMutable(NULL)
     46    : vectorBase()
     47  {
     48  }
     49
     50
     51  vectorView::vectorView(vectorView& other)
     52    : vectorBase()
     53  {
     54    view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(),
     55                                                     0, other.size()));
     56    const_vec_ = vec_ = &(view_->vector);
     57   
     58  }
     59
     60
     61  vectorView::vectorView(const vectorView& other)
     62    : vectorBase(), view_(NULL)
     63  {
     64    const_view_ = new gsl_vector_const_view(
     65        gsl_vector_const_subvector(other.gsl_vector_p(), 0, other.size()));
     66    const_vec_ = &(const_view_->vector);
     67  }
     68
     69
     70  vectorView::vectorView(vectorBase& other)
     71    : vectorBase(other.gsl_vector_p())
     72  {
     73    view_ =
     74      new gsl_vector_view(gsl_vector_subvector_with_stride(other.gsl_vector_p(),
     75                                                           0, 1,other.size()));
     76    const_vec_ = vec_ = &(view_->vector);
     77  }
     78
     79
     80  /*
     81  vectorView::vectorView(const vectorBase& other)
     82    : vectorBase(other.const_vec_), view_(NULL)
     83  {
     84  }
     85  */
     86
     87  vectorView::vectorView(vectorBase& v, size_t offset,size_t n,size_t stride)
     88    : vectorBase()
    6289  {
    6390    view_ =
     
    6895
    6996
     97  vectorView::vectorView(const vectorBase& v, size_t offset,size_t n,
     98                         size_t stride)
     99    : vectorBase(), view_(NULL)
     100  {
     101    const_view_ = new gsl_vector_const_view(
     102                   gsl_vector_const_subvector_with_stride(v.gsl_vector_p(),
     103                                                          offset, stride,n));
     104    const_vec_ = &(const_view_->vector);
     105  }
     106
     107
    70108  vectorView::vectorView(matrix& m, size_t i, bool row)
    71     : vectorMutable(NULL)
     109    : vectorBase()
    72110  {
    73111    view_=new gsl_vector_view(row ?
     
    78116
    79117
     118  vectorView::vectorView(const matrix& m, size_t i, bool row)
     119    : vectorBase(), view_(NULL)
     120  {
     121    const_view_ =
     122      new gsl_vector_const_view(row ?
     123                                gsl_matrix_const_row(m.gsl_matrix_p(),i) :
     124                                gsl_matrix_const_column(m.gsl_matrix_p(),i));
     125    const_vec_ = &(const_view_->vector);
     126  }
     127
     128
     129  /*
    80130  vectorView::vectorView(proxy p)
    81     : vectorMutable(NULL)
     131    : vectorBase()
    82132  {
    83133    view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_, 0, p.vec_->size));
    84134    const_vec_ = vec_ = &(view_->vector);
    85135  }
    86 
     136  */
    87137
    88138  vectorView::~vectorView(void)
    89139  {
    90     clean_up();
    91   }
    92 
    93 
    94   const vectorMutable& vectorView::operator=(vectorMutable& other )
     140    delete_allocated_memory();
     141  }
     142
     143
     144  const vectorBase& vectorView::assign(const vectorBase& other )
    95145  {
    96146    if (size()!=other.size())
     
    98148    if (!other.size())
    99149      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;
     150    gsl_vector_memcpy(vec_, other.gsl_vector_p());
     151    const_vec_ = vec_;
    104152    return *this;
    105153  }
    106154
    107155
    108   const vectorMutable& vectorView::operator=(proxy p)
     156  bool vectorView::isview(void) const
     157  {
     158    return true;
     159  }
     160
     161
     162
     163  const vectorBase& vectorView::operator=(const vectorView& other )
     164  {
     165    return assign(other);
     166  }
     167
     168 
     169  const vectorBase& vectorView::operator=(const vectorBase& other )
     170  {
     171    return assign(other);
     172  }
     173
     174 
     175  /*
     176  const vectorBase& vectorView::operator=(proxy p)
    109177  {
    110178    if (size()!=p.vec_->size)
     
    114182    clean_up();
    115183    view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_,0, p.vec_->size));
    116     const_vec_ = vec_ = &view_->vector;
     184    const_vec_=vec_ = &view_->vector;
    117185    return *this;
    118186  }
    119 
    120 
    121   void vectorView::clean_up(void)
    122   {
    123     if (view_)
     187  */
     188
     189  void vectorView::delete_allocated_memory(void)
     190  {
     191    if (view_){
    124192      delete view_;
     193      view_=NULL;
     194    }
     195    else if (const_view_){
     196      delete const_view_;
     197      const_view_=NULL;
     198    }
     199    const_vec_ = vec_ = NULL;
    125200  }
    126201
  • branches/peter-dev/yat/utility/vectorView.h

    r995 r1008  
    2929*/
    3030
    31 #include "vectorMutable.h"
     31#include "vectorBase.h"
    3232
    3333#include "Exception.h"
     
    8787  */
    8888
    89   class vectorView : public vectorMutable
     89  class vectorView : public vectorBase
    9090  {
    9191  public:
     
    101101       \brief The copy constructor.
    102102    */
    103     vectorView(vectorMutable& other);
     103    vectorView(vectorBase& other);
     104    vectorView(vectorView& other);
     105    // Peter, privatize
     106    vectorView(const vectorView& other);
    104107
    105108    /**
     
    122125       \throw GSL_error if a view cannot be set up.
    123126    */
    124     vectorView(vectorMutable& v, size_t offset, size_t n, size_t stride=1);
     127    vectorView(vectorBase& v, size_t offset, size_t n, size_t stride=1);
     128    // Peter, privatize
     129    vectorView(const vectorBase& v, size_t offset, size_t n, size_t stride=1);
    125130
    126131    ///
     
    146151    vectorView(matrix& m, size_t i, bool row=true);
    147152
     153    // Peter should be private
     154    vectorView(const matrix& m, size_t i, bool row=true);
     155
    148156    /**
    149157     */
    150     vectorView(proxy p);
     158    //vectorView(proxy p);
    151159
    152160    ///
     
    154162    ///
    155163    ~vectorView(void);
     164
     165    /**
     166       \return true
     167    */
     168    bool isview(void) const;
    156169
    157170    /**
     
    162175       \throw GSL_error if dimensions mis-match.
    163176    */
    164     const vectorMutable& operator=(vectorMutable&);
     177    const vectorBase& operator=(const vectorBase&);
     178    const vectorBase& operator=(const vectorView&);
    165179
    166     const vectorMutable& operator=(proxy);
     180    //const vectorBase& operator=(proxy);
    167181
    168182  private:
    169     void clean_up(void);
     183    const vectorBase& assign(const vectorBase& other);
     184    void delete_allocated_memory(void);
    170185
    171186    gsl_vector_view* view_;
     187    gsl_vector_const_view* const_view_;
    172188   
    173189  };
Note: See TracChangeset for help on using the changeset viewer.