Changeset 1027


Ignore:
Timestamp:
Feb 2, 2008, 10:29:29 PM (16 years ago)
Author:
Peter
Message:

going back to previous design in which view and const_view are in different classes. Having them in same didnt work as expected. There is a problem converting vector::iterator to vector::const_iterator. I'll open a separate ticket for this issue.

Location:
trunk
Files:
15 edited
4 copied

Legend:

Unmodified
Added
Removed
  • trunk/test/iterator_test.cc

    r1000 r1027  
    5353  utility::vector::iterator begin=vec.begin();
    5454  // test iterator to const_iterator conversion
     55  ///////////////////////////////////////////////////////
     56  /* const conversion doesn't work for new vector structure
    5557  utility::vector::const_iterator ci = vec.begin();
    5658  ci = begin;
    5759  if (begin!=ci)
    5860    ok = false;
    59  
     61  */ 
     62  ///////////////////////////////////////////////////////
    6063  utility::vector::iterator end=vec.end();
    6164  std::sort(begin, end);
  • trunk/test/vector_test.cc

    r1015 r1027  
    2929#include "yat/utility/utility.h"
    3030#include "yat/utility/vector.h"
     31#include "yat/utility/VectorConstView.h"
    3132#include "yat/utility/VectorView.h"
    3233
     
    6768  shuffle(vec);
    6869  double sum_after = utility::sum(vec);
    69   ok &= (sum_after==sum_before);
     70  if (sum_after!=sum_before){
     71    *message << "shuffle failed" << std::endl;
     72    ok = false;
     73  }
    7074
    7175  // checking that view works
     
    8892    *message << "const view implementation" << std::endl;
    8993    const utility::vector vv(10,3.0);
    90     utility::VectorView vview(vv,0,5,1);
     94    utility::VectorConstView vview(vv,0,5,1);
    9195    // const utility::vector vview(vv,0,5,1); // this is the proper line
    9296    utility::vector vv2(5,2.0);
     
    117121  }
    118122
    119   /* different sizes are allowed in new design
    120123  // checking that assignment operator throws an exception if vectors
    121124  // differ in size
     
    130133    try {
    131134      utility::vector v(vec_view.size()+1,0.0);
    132       v=vec_view;
     135      vec_view=v;
    133136    } catch (utility::GSL_error& err) {
    134137      exception_happens=true;
     
    141144    gsl_set_error_handler(err_handler);
    142145  }
    143   */
    144146
    145147  // checking that assignment operator changes the underlying object when
  • trunk/yat/classifier/KNN.h

    r1009 r1027  
    261261    for(size_t sample=0;sample<distances->columns();sample++) {
    262262      std::vector<size_t> k_index;
    263       utility::sort_smallest_index(k_index,k_,distances->column_vec(sample));
     263      utility::sort_smallest_index(k_index,k_,
     264                                   distances->column_const_vec(sample));
    264265      for(size_t j=0;j<k_index.size();j++) {
    265266        prediction(target_(k_index[j]),sample)++;
  • trunk/yat/classifier/NBC.cc

    r1009 r1027  
    207207    for (size_t i=0; i<prediction.rows(); ++i){
    208208      prediction.row_vec(i) *=
    209         1.0/sum(
    210                 prediction.row_vec(i)
    211                 );
     209        1.0/sum(prediction.row_const_vec(i));
    212210    }
    213211  }
  • trunk/yat/utility/Iterator.h

    r1000 r1027  
    3030#include <stddef.h>
    3131#include <stdexcept>
     32#include <utility>
    3233
    3334namespace theplu {
     
    3536namespace utility {
    3637
     38  class VectorBase;
     39
    3740  /**
    3841     @brief Iterator
    3942  */
    40   template<typename return_type, typename Container>
     43  template<typename T, typename Container>
    4144  class Iterator
    4245  {
     
    4649    typedef size_t difference_type;
    4750    typedef double* pointer;
    48     typedef double& reference;
    49 
     51    typedef T reference;
     52
     53  private:
     54    typedef Iterator<reference, Container> self;
     55
     56  public:
    5057    /**
    5158       \brief Default Constructor
     
    6269      : container_(&container), index_(index) {}
    6370
     71    operator Iterator<const reference, const Container>()
     72    { return Iterator<const reference, const Container>(*container_, index_); }
     73
    6474    /**
    6575       \return element
    6676     */
    67     return_type operator*(void) const
     77    reference operator*(void) const
    6878    { yat_assert<std::out_of_range>(index_<container_->size());
    6979      return container_->operator()(index_); }
     
    7282       \return element \a n steps forward
    7383     */
    74     return_type operator[](difference_type n) const
     84    reference operator[](difference_type n) const
    7585    { yat_assert<std::out_of_range>(index_+n < container_->size());
    7686      return container_->operator()(index_+n); }
     
    8999     */
    90100    Iterator operator++(int)
    91     { Iterator<return_type, Container> tmp(*this); ++index_; return tmp;}
     101    { self tmp(*this); ++index_; return tmp;}
    92102
    93103    /**
     
    104114     */
    105115    Iterator operator--(int)
    106     { Iterator<return_type, Container> tmp(*this); --index_; return tmp;}
     116    { self tmp(*this); --index_; return tmp;}
    107117
    108118    /**
     
    126136     */
    127137    friend Iterator operator+(const Iterator& lhs, size_t n)
    128     { return Iterator<return_type, Container>(*lhs.container_, lhs.index_+n); }
     138    { return self(*lhs.container_, lhs.index_+n); }
    129139
    130140    /**
     
    134144     */
    135145    friend Iterator operator-(const Iterator& lhs, size_t n)
    136     { return Iterator<return_type, Container>(*lhs.container_, lhs.index_-n); }
     146    { return self(*lhs.container_, lhs.index_-n); }
    137147
    138148    /**
     
    146156   
    147157    /**
    148        \brief Conversion operator
    149 
    150        This operator allows automatic conversion from iterator to
    151        const_iterator
    152      */
    153     operator Iterator<const double, const Container> ()
    154     { return Iterator<const double, const Container>(*container_, index_); }
    155 
    156     /**
    157158       \brief Equality operator
    158159
    159160       \return True if \a lhs and \a rhs are pointing to same element
    160161     */
    161     friend bool operator==(const Iterator<return_type, Container>& lhs,
    162                            const Iterator<return_type, Container>& rhs)
     162    friend bool operator==(const self& lhs, const self& rhs)
    163163    { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; }
    164164   
     
    168168       \return False if \a lhs and \a rhs are pointing to same element
    169169     */
    170     friend bool operator!=(const Iterator<return_type, Container>& lhs,
    171                            const Iterator<return_type, Container>& rhs)
     170    friend bool operator!=(const Iterator& lhs,
     171                           const Iterator& rhs)
    172172    { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); }
    173173   
     
    175175       \brief Less operator
    176176     */
    177     friend bool operator<(const Iterator<return_type, Container>& lhs,
    178                            const Iterator<return_type, Container>& rhs)
     177    friend bool operator<(const self& lhs, const self& rhs)
    179178    { return lhs.index_<rhs.index_; }
    180179   
     
    182181       \brief Less equal operator
    183182     */
    184     friend bool operator<=(const Iterator<return_type, Container>& lhs,
    185                            const Iterator<return_type, Container>& rhs)
     183    friend bool operator<=(const self& lhs, const self& rhs)
    186184    { return lhs.index_<=rhs.index_; }
    187185   
     
    189187       \brief Larger operator
    190188     */
    191     friend bool operator>(const Iterator<return_type, Container>& lhs,
    192                            const Iterator<return_type, Container>& rhs)
     189    friend bool operator>(const self& lhs,
     190                           const self& rhs)
    193191    { return lhs.index_>rhs.index_; }
    194192   
     
    196194       \brief Larger equal operator
    197195     */
    198     friend bool operator>=(const Iterator<return_type, Container>& lhs,
    199                            const Iterator<return_type, Container>& rhs)
     196    friend bool operator>=(const self& lhs, const self& rhs)
    200197    { return lhs.index_>=rhs.index_; }
    201198   
     
    208205    //Iterator& operator=(const Iterator&);
    209206  };
    210 
    211207}}} // of namespace utility, yat, and theplu
    212208
  • trunk/yat/utility/Makefile.am

    r1015 r1027  
    3030  OptionHelp.cc OptionSwitch.cc \
    3131  PCA.cc stl_utility.cc SVD.cc TypeInfo.cc utility.cc vector.cc \
    32   VectorBase.cc VectorView.cc WeNNI.cc
     32  VectorBase.cc VectorConstView.cc VectorMutable.cc VectorView.cc WeNNI.cc
    3333
    3434include_utilitydir = $(includedir)/yat/utility
     
    4141  OptionHelp.h OptionSwitch.h \
    4242  PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h \
    43   VectorBase.h VectorView.h \
     43  VectorBase.h VectorConstView.h VectorMutable.h VectorView.h \
    4444  WeNNI.h yat_assert.h
    4545
  • trunk/yat/utility/PCA.cc

    r1015 r1027  
    193193    utility::vector A_row_sum(A_.rows());
    194194    for (size_t i=0; i<A_row_sum.size(); ++i){
    195       const VectorView tmp(A_.row_vec(i));
     195      const VectorConstView tmp(A_.row_const_vec(i));
    196196      A_row_sum(i) = sum(tmp);
    197197    }
  • trunk/yat/utility/VectorBase.cc

    r1015 r1027  
    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 
    5745  VectorBase::VectorBase(const gsl_vector* v)
    58     : vec_(NULL), const_vec_(v)
     46    : const_vec_(v)
    5947  {
    6048  }
     
    6654
    6755
    68   void VectorBase::all(double value)
    69   {
    70     assert(vec_);
    71     gsl_vector_set_all(vec_,value);
    72   }
    73 
    74 
    7556  VectorBase::const_iterator VectorBase::begin(void) const
    7657  {
     
    7960
    8061
    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));
    93   }
    94 
    95 
    9662  VectorBase::const_iterator VectorBase::end(void) const
    9763  {
    9864    return const_iterator(*this, size());
    99   }
    100 
    101 
    102   VectorBase::iterator VectorBase::end(void)
    103   {
    104     return iterator(*this, size());
    10565  }
    10666
     
    12888
    12989
    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 
    14590  size_t VectorBase::size(void) const
    14691  {
     
    160105
    161106
    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 
    177107  const double& VectorBase::operator[](size_t i) const
    178108  {
     
    199129      res += other(i) * (*this)(i);
    200130    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;
    235131  }
    236132
     
    280176
    281177
    282   void shuffle(VectorBase& invec)
    283   {
    284     random::DiscreteUniform rnd;
    285     std::random_shuffle(invec.begin(), invec.end(), rnd);
    286   }
    287 
    288 
    289178  void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec)
    290179  {
  • trunk/yat/utility/VectorBase.h

    r1015 r1027  
    8989  {
    9090  public:
    91 
    92     /// \brief VectorBase::iterator
    93     typedef Iterator<double&, VectorBase> iterator;
    94 
    9591    /// \brief VectorBase::const_iterator
    96     typedef Iterator<const double, const VectorBase> const_iterator;
    97 
    98     /**
    99        \brief default constructor
    100     */
    101     VectorBase(void);
     92    typedef Iterator<const double&, const VectorBase> const_iterator;
    10293
    10394    /**
    10495       \brief Constructor.
    10596    */
    106     VectorBase(gsl_vector*);
    107 
    108     /**
    109        \brief Constructor.
    110     */
    111     VectorBase(const gsl_vector*);
     97    VectorBase(const gsl_vector* v=NULL);
    11298
    11399    ///
     
    115101    ///
    116102    virtual ~VectorBase(void);
    117 
    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);
    127103
    128104    /**
     
    132108
    133109    /**
    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);
    145 
    146     /**
    147110       \return read-only iterator to end of VectorBase
    148111     */
     
    161124
    162125    ///
    163     /// @return A pointer to the internal GSL vector,
    164     ///
    165     gsl_vector* gsl_vector_p(void);
    166 
    167     ///
    168126    /// @return A const pointer to the internal GSL vector,
    169127    ///
     
    171129
    172130    /**
    173    
    174131       Check if the vector object is a view (sub-vector) to another
    175132       vector.
    176133   
    177134       \return True if the object is a view, false othwerwise.
    178    
    179135     */
    180136    virtual bool isview(void) const=0;
    181137
    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 
    195138    ///
    196139    /// @return the number of elements in the VectorBase.
     
    199142
    200143    /**
    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     /**
    208144       \brief Element access operator.
    209145
    210        \return Reference to element \a i.
     146       \return Const reference to element \a i.
    211147
    212148       \throw If GSL range checks are enabled in the underlying GSL
     
    214150       of range.
    215151    */
    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     */
    229152    const double& operator()(size_t i) const;
    230153    // Peter, remove this one
     
    262185    double operator*(const VectorBase&) const;
    263186
    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 
    324187  protected:
    325     gsl_vector* vec_;
    326188    const gsl_vector* const_vec_;
    327 
    328     /*
    329     struct proxy
    330     {
    331       gsl_vector* vec_;
    332     };
    333     */
    334189
    335190  private:
    336191    // copy assignment no allowed
    337192    const VectorBase& operator=(const VectorBase&);
    338   public:
    339     /**
    340      */
    341     //operator proxy();
    342 
    343193  };
    344194
     
    397247  */
    398248  bool nan(const VectorBase& templat, vector& flag);
    399 
    400   /**
    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&);
    409249
    410250  /**
     
    442282
    443283  /**
    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   /**
    453284     \brief The output operator for the VectorBase class.
    454285  */
  • trunk/yat/utility/VectorConstView.cc

    r1026 r1027  
    2525*/
    2626
    27 #include "VectorView.h"
     27#include "VectorConstView.h"
    2828#include "matrix.h"
    2929#include "utility.h"
     
    4343
    4444
    45   VectorView::VectorView(void)
    46     : VectorBase(), view_(NULL), const_view_(NULL)
     45  VectorConstView::VectorConstView(const VectorConstView& other)
     46    : VectorBase(other.gsl_vector_p()), const_view_(NULL)
    4747  {
    4848  }
    4949
    5050
    51   VectorView::VectorView(VectorView& other)
    52     : VectorBase(), const_view_(NULL)
     51  VectorConstView::VectorConstView(const VectorBase& other)
     52    : VectorBase(other.gsl_vector_p()), const_view_(NULL)
    5353  {
    54     view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(),
    55                                                      0, other.size()));
    56     const_vec_ = vec_ = &(view_->vector);
    57    
    5854  }
    5955
    6056
    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()), const_view_(NULL)
    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(), const_view_(NULL)
    89   {
    90     view_ =
    91       new gsl_vector_view(gsl_vector_subvector_with_stride(v.gsl_vector_p(),
    92                                                            offset, stride,n));
    93     const_vec_ = vec_ = &(view_->vector);
    94   }
    95 
    96 
    97   VectorView::VectorView(const VectorBase& v, size_t offset,size_t n,
    98                          size_t stride)
    99     : VectorBase(), view_(NULL)
     57  VectorConstView::VectorConstView(const VectorBase& v, size_t offset,size_t n,
     58                                   size_t stride)
     59    : VectorBase()
    10060  {
    10161    const_view_ = new gsl_vector_const_view(
     
    10666
    10767
    108   VectorView::VectorView(matrix& m, size_t i, bool row)
     68  VectorConstView::VectorConstView(const matrix& m, size_t i, bool row)
    10969    : VectorBase(), const_view_(NULL)
    110   {
    111     view_=new gsl_vector_view(row ?
    112                               gsl_matrix_row   (m.gsl_matrix_p(),i) :
    113                               gsl_matrix_column(m.gsl_matrix_p(),i)  );
    114     const_vec_ = vec_ = &(view_->vector);
    115   }
    116 
    117 
    118   VectorView::VectorView(const matrix& m, size_t i, bool row)
    119     : VectorBase(), view_(NULL)
    12070  {
    12171    const_view_ =
     
    12777
    12878
    129   /*
    130   VectorView::VectorView(proxy p)
    131     : VectorBase()
    132   {
    133     view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_, 0, p.vec_->size));
    134     const_vec_ = vec_ = &(view_->vector);
    135   }
    136   */
    137 
    138   VectorView::~VectorView(void)
     79  VectorConstView::~VectorConstView(void)
    13980  {
    14081    delete_allocated_memory();
     
    14283
    14384
    144   const VectorView& VectorView::assign(const VectorBase& other )
    145   {
    146     if (size()!=other.size())
    147       throw utility::GSL_error("VectorView::dimension mis-match");
    148     if (!other.size())
    149       return *this;
    150     gsl_vector_memcpy(vec_, other.gsl_vector_p());
    151     const_vec_ = vec_;
    152     return *this;
    153   }
    154 
    155 
    156   bool VectorView::isview(void) const
     85  bool VectorConstView::isview(void) const
    15786  {
    15887    return true;
     
    16190
    16291
    163   const VectorView& VectorView::operator=(const VectorView& other )
     92  void VectorConstView::delete_allocated_memory(void)
    16493  {
    165     return assign(other);
    166   }
    167 
    168  
    169   const VectorView& VectorView::operator=(const VectorBase& other )
    170   {
    171     return assign(other);
    172   }
    173 
    174  
    175   /*
    176   const VectorView& VectorView::operator=(proxy p)
    177   {
    178     if (size()!=p.vec_->size)
    179       throw utility::GSL_error("VectorView::dimension mis-match");
    180     if (!size())
    181       return *this;
    182     clean_up();
    183     view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_,0, p.vec_->size));
    184     const_vec_=vec_ = &view_->vector;
    185     return *this;
    186   }
    187   */
    188 
    189   void VectorView::delete_allocated_memory(void)
    190   {
    191     if (view_){
    192       delete view_;
    193       view_=NULL;
    194     }
    195     else if (const_view_){
     94    if (const_view_){
    19695      delete const_view_;
    19796      const_view_=NULL;
    19897    }
    199     const_vec_ = vec_ = NULL;
     98    const_vec_ = NULL;
    20099  }
    201100
  • trunk/yat/utility/VectorConstView.h

    r1026 r1027  
    1 #ifndef _theplu_yat_utility_vector_view_
    2 #define _theplu_yat_utility_vector_view_
     1#ifndef _theplu_yat_utility_vector_const_view_
     2#define _theplu_yat_utility_vector_const_view_
    33
    44// $Id$
     
    8585  */
    8686
    87   class VectorView : public VectorBase
     87  class VectorConstView : public VectorBase
    8888  {
    8989  public:
    9090    /**
    91        \brief Default constructor.
    92     */
    93     VectorView(void);
     91       \brief The copy constructor.
     92    */
     93    VectorConstView(const VectorBase& other);
     94    VectorConstView(const VectorConstView& other);
    9495
    9596    /**
    96        \brief The copy constructor.
    97     */
    98     VectorView(VectorBase& other);
    99     VectorView(VectorView& other);
    100     // Peter, privatize
    101     VectorView(const VectorView& other);
     97       \brief VectorConstView constructor.
    10298
    103     /**
    104        \brief VectorView constructor.
    105 
    106        Create a view of VectorView \a v, with starting index \a offset,
     99       Create a view of VectorConstView \a v, with starting index \a offset,
    107100       size \a n, and an optional \a stride.
    108101
    109        A VectorView view can be used as any VectorView with the difference
     102       A VectorConstView view can be used as any VectorConstView with the difference
    110103       that changes made to the view will also change the object that
    111104       is viewed. Also, using the copy constructor will create a new
    112        VectorView object that is a copy of whatever is viewed. If a copy
     105       VectorConstView object that is a copy of whatever is viewed. If a copy
    113106       of the view is needed then you should use this constructor to
    114107       obtain a copy.
     
    120113       \throw GSL_error if a view cannot be set up.
    121114    */
    122     VectorView(VectorBase& v, size_t offset, size_t n, size_t stride=1);
    123     // Peter, privatize
    124     VectorView(const VectorBase& v, size_t offset, size_t n, size_t stride=1);
     115    VectorConstView(const VectorBase& v,size_t offset,size_t n,size_t stride=1);
    125116
    126117    ///
    127118    /// Matrix row/column view constructor.
    128119    ///
    129     /// Create a row/column VectorView view of matrix \a m, pointing at
     120    /// Create a row/column VectorConstView view of matrix \a m, pointing at
    130121    /// row/column \a i. The parameter \a row is used to set whether
    131122    /// the view should be a row or column view. If \a row is set to
     
    133124    /// naturally, a column view otherwise.
    134125    ///
    135     /// A VectorView view can be used as any VectorView with the difference
     126    /// A VectorConstView view can be used as any VectorConstView with the difference
    136127    /// that changes made to the view will also change the object that
    137128    /// is viewed. Also, using the copy constructor will create a new
    138     /// VectorView object that is a copy of whatever is viewed. If a copy
    139     /// of the view is needed then you should use the VectorView view
     129    /// VectorConstView object that is a copy of whatever is viewed. If a copy
     130    /// of the view is needed then you should use the VectorConstView view
    140131    /// constructor to obtain a copy.
    141132    ///
     
    144135    /// use is undefined.
    145136    ///
    146     VectorView(matrix& m, size_t i, bool row=true);
    147 
    148     // Peter should be private
    149     VectorView(const matrix& m, size_t i, bool row=true);
    150 
    151     /**
    152      */
    153     //VectorView(proxy p);
     137    VectorConstView(const matrix& m, size_t i, bool row=true);
    154138
    155139    ///
    156140    /// The destructor.
    157141    ///
    158     ~VectorView(void);
     142    ~VectorConstView(void);
    159143
    160144    /**
     
    170154       \throw GSL_error if dimensions mis-match.
    171155    */
    172     const VectorView& operator=(const VectorBase&);
    173     const VectorView& operator=(const VectorView&);
    174 
    175     //const VectorBase& operator=(proxy);
     156    const VectorConstView& operator=(const VectorBase&);
     157    const VectorConstView& operator=(const VectorConstView&);
    176158
    177159  private:
    178     const VectorView& assign(const VectorBase& other);
     160    const VectorConstView& assign(const VectorBase& other);
    179161    void delete_allocated_memory(void);
    180162
    181     gsl_vector_view* view_;
    182163    gsl_vector_const_view* const_view_;
    183164   
  • trunk/yat/utility/VectorMutable.cc

    r1026 r1027  
    2525*/
    2626
    27 #include "VectorBase.h"
     27#include "VectorMutable.h"
    2828#include "matrix.h"
    2929#include "utility.h"
     
    4343
    4444
    45   VectorBase::VectorBase(void)
    46     : vec_(NULL), const_vec_(NULL)
     45  VectorMutable::VectorMutable(void)
     46    : VectorBase(NULL), vec_(NULL)
    4747  {
    4848  }
    4949
    5050
    51   VectorBase::VectorBase(gsl_vector* v)
    52     : vec_(v), const_vec_(v)
     51  VectorMutable::VectorMutable(gsl_vector* v)
     52    : VectorBase(v), vec_(v)
    5353  {
    5454  }
    5555
    5656
    57   VectorBase::VectorBase(const gsl_vector* v)
    58     : vec_(NULL), const_vec_(v)
     57  VectorMutable::VectorMutable(const gsl_vector* v)
     58    : VectorBase(v), vec_(NULL)
    5959  {
    6060  }
    6161
    6262
    63   VectorBase::~VectorBase(void)
     63  VectorMutable::~VectorMutable(void)
    6464  {
    6565  }
    6666
    6767
    68   void VectorBase::all(double value)
     68  void VectorMutable::all(double value)
    6969  {
    7070    assert(vec_);
     
    7373
    7474
    75   VectorBase::const_iterator VectorBase::begin(void) const
    76   {
    77     return const_iterator(*this, 0);
    78   }
    79 
    80 
    81   VectorBase::iterator VectorBase::begin(void)
     75  VectorMutable::iterator VectorMutable::begin(void)
    8276  {
    8377    return iterator(*this, 0);
     
    8579
    8680
    87   void VectorBase::div(const VectorBase& other)
     81  void VectorMutable::div(const VectorBase& other)
    8882  {
    8983    assert(vec_);
    9084    int status=gsl_vector_div(vec_,other.gsl_vector_p());
    9185    if (status)
    92       throw utility::GSL_error(std::string("VectorBase::div",status));
     86      throw utility::GSL_error(std::string("VectorMutable::div",status));
    9387  }
    9488
    9589
    96   VectorBase::const_iterator VectorBase::end(void) const
    97   {
    98     return const_iterator(*this, size());
    99   }
    100 
    101 
    102   VectorBase::iterator VectorBase::end(void)
     90  VectorMutable::iterator VectorMutable::end(void)
    10391  {
    10492    return iterator(*this, size());
     
    10694
    10795
    108   bool VectorBase::equal(const VectorBase& other, const double d) const
    109   {
    110     if (this==&other)
    111       return true;
    112     if (size()!=other.size())
    113       return false;
    114     // if gsl error handler disabled, out of bounds index will not
    115     // abort the program.
    116     for (size_t i=0; i<size(); ++i)
    117       if (fabs( (*this)(i)-other(i) ) > d ||
    118           std::isnan((*this)(i)) || std::isnan(other(i)) )
    119         return false;
    120     return true;
    121   }
    122 
    123 
    124   const gsl_vector* VectorBase::gsl_vector_p(void) const
    125   {
    126     return const_vec_;
    127   }
    128 
    129 
    130   gsl_vector* VectorBase::gsl_vector_p(void)
     96  gsl_vector* VectorMutable::gsl_vector_p(void)
    13197  {
    13298    return vec_;
     
    134100
    135101
    136   void VectorBase::mul(const VectorBase& other)
     102  void VectorMutable::mul(const VectorBase& other)
    137103  {
    138104    assert(vec_);
    139105    int status=gsl_vector_mul(vec_,other.gsl_vector_p());
    140106    if (status)
    141       throw utility::GSL_error(std::string("VectorBase::div",status));
     107      throw utility::GSL_error(std::string("VectorMutable::div",status));
    142108  }
    143109
    144110
    145   size_t VectorBase::size(void) const
     111  double& VectorMutable::operator()(size_t i)
    146112  {
    147     if (!const_vec_)
    148       return 0;
    149     return const_vec_->size;
    150   }
    151 
    152 
    153   const double& VectorBase::operator()(size_t i) const
    154   {
    155     const double* d=gsl_vector_const_ptr(const_vec_, i);
     113    double* d=gsl_vector_ptr(vec_, i);
    156114    if (!d)
    157       throw utility::GSL_error("VectorBase::operator()",GSL_EINVAL);
     115      throw utility::GSL_error("VectorMutable::operator()",GSL_EINVAL);
    158116    return *d;
    159117  }
    160118
    161119
    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)
     120  double& VectorMutable::operator[](size_t i)
    172121  {
    173122    return this->operator()(i);
     
    175124
    176125
    177   const double& VectorBase::operator[](size_t i) const
    178   {
    179     return this->operator()(i);
    180   }
    181 
    182 
    183   bool VectorBase::operator==(const VectorBase& other) const
    184   {
    185     return equal(other);
    186   }
    187 
    188 
    189   bool VectorBase::operator!=(const VectorBase& other) const
    190   {
    191     return !equal(other);
    192   }
    193 
    194 
    195   double VectorBase::operator*( const VectorBase &other ) const
    196   {
    197     double res = 0.0;;
    198     for ( size_t i = 0; i < size(); ++i )
    199       res += other(i) * (*this)(i);
    200     return res;
    201   }
    202 
    203 
    204   const VectorBase& VectorBase::operator+=(double d)
     126  const VectorMutable& VectorMutable::operator+=(double d)
    205127  {
    206128    assert(vec_);
     
    210132
    211133
    212   const VectorBase& VectorBase::operator-=(const VectorBase& other)
     134  const VectorMutable& VectorMutable::operator-=(const VectorBase& other)
    213135  {
    214136    assert(vec_);
    215137    int status=gsl_vector_sub(vec_, other.gsl_vector_p());
    216138    if (status)
    217       throw utility::GSL_error(std::string("VectorBase::sub", status));
     139      throw utility::GSL_error(std::string("VectorMutable::sub", status));
    218140    return *this;
    219141  }
    220142
    221143
    222   const VectorBase& VectorBase::operator-=(const double d)
     144  const VectorMutable& VectorMutable::operator-=(const double d)
    223145  {
    224146    assert(vec_);
     
    228150
    229151
    230   const VectorBase& VectorBase::operator*=(double d)
     152  const VectorMutable& VectorMutable::operator*=(double d)
    231153  {
    232154    assert(vec_);
     
    236158
    237159
    238   bool isnull(const VectorBase& v)
     160  void shuffle(VectorMutable& invec)
    239161  {
    240     return gsl_vector_isnull(v.gsl_vector_p());
     162    random::random_shuffle(invec.begin(), invec.end());
    241163  }
    242164
    243165
    244   double max(const VectorBase& v)
     166  void sort(VectorMutable& invec)
    245167  {
    246     return gsl_vector_max(v.gsl_vector_p());
     168    std::sort(invec.begin(), invec.end());
    247169  }
    248170
    249171
    250   size_t max_index(const VectorBase& v)
     172  VectorMutable::operator proxy()
    251173  {
    252     return gsl_vector_max_index(v.gsl_vector_p());
    253   }
    254 
    255 
    256   double min(const VectorBase& v)
    257   {
    258     return gsl_vector_min(v.gsl_vector_p());
    259   }
    260 
    261 
    262   size_t min_index(const VectorBase& v)
    263   {
    264     return gsl_vector_min_index(v.gsl_vector_p());
    265   }
    266 
    267 
    268   bool nan(const VectorBase& templat, vector& flag)
    269   {
    270     size_t vsize(templat.size());
    271     flag = vector(vsize, 1.0);
    272     bool nan=false;
    273     for (size_t i=0; i<vsize; i++)
    274       if (std::isnan(templat(i))) {
    275         flag(i)=0;
    276         nan=true;
    277       }
    278     return nan;
    279   }
    280 
    281 
    282   void shuffle(VectorBase& invec)
    283   {
    284     random::DiscreteUniform rnd;
    285     std::random_shuffle(invec.begin(), invec.end(), rnd);
    286   }
    287 
    288 
    289   void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec)
    290   {
    291     assert(invec.gsl_vector_p());
    292     gsl_permutation* p = gsl_permutation_alloc(invec.size());
    293     int status=gsl_sort_vector_index(p,invec.gsl_vector_p());
    294     if (status) {
    295       gsl_permutation_free(p);
    296       throw utility::GSL_error(std::string("sort_index(vector&,const VectorBase&)",status));     
    297     }
    298     sort_index=std::vector<size_t>(p->data,p->data+p->size);
    299     gsl_permutation_free(p);
    300   }
    301 
    302 
    303   void sort_smallest_index(std::vector<size_t>& sort_index, size_t k,
    304                             const VectorBase& invec)
    305   {
    306     assert(invec.gsl_vector_p());
    307     assert(k<=invec.size());
    308     sort_index.resize(k);
    309     gsl_sort_vector_smallest_index(&sort_index[0],k,invec.gsl_vector_p());
    310   }
    311  
    312   void sort_largest_index(std::vector<size_t>& sort_index, size_t k,
    313                             const VectorBase& invec)
    314   {
    315     assert(invec.gsl_vector_p());
    316     assert(k<=invec.size());
    317     sort_index.resize(k);
    318     gsl_sort_vector_largest_index(&sort_index[0],k,invec.gsl_vector_p());
    319   }
    320 
    321 
    322   double sum(const VectorBase& v)
    323   {
    324     double sum = 0;
    325     size_t vsize=v.size();
    326     for (size_t i=0; i<vsize; ++i)
    327       sum += v(i);
    328     return sum;
    329   }
    330 
    331 
    332   std::ostream& operator<<(std::ostream& s, const VectorBase& a)
    333   {
    334     s.setf(std::ios::dec);
    335     s.precision(12);
    336     for (size_t j = 0; j < a.size(); ++j) {
    337       s << a(j);
    338       if ( (j+1)<a.size() )
    339         s << s.fill();
    340     }
    341     return s;
     174    proxy p;
     175    p.vec_ = vec_;
     176    vec_ = NULL; // proxy takes ownership and delivers to its receiver
     177    return p;
    342178  }
    343179
  • trunk/yat/utility/VectorMutable.h

    r1026 r1027  
    1 #ifndef _theplu_yat_utility_vector_base_
    2 #define _theplu_yat_utility_vector_base_
     1#ifndef _theplu_yat_utility_vector_mutable_
     2#define _theplu_yat_utility_vector_mutable_
    33
    44// $Id$
     
    3030*/
    3131
     32#include "VectorBase.h"
    3233#include "Exception.h"
    3334#include "Iterator.h"
     
    8687  */
    8788
    88   class VectorBase
     89  class VectorMutable : public VectorBase
    8990  {
    9091  public:
    91 
    92     /// \brief VectorBase::iterator
    93     typedef Iterator<double&, VectorBase> iterator;
    94 
    95     /// \brief VectorBase::const_iterator
    96     typedef Iterator<const double, const VectorBase> const_iterator;
     92    /**
     93       \brief mutable iterator
     94    */
     95    typedef Iterator<double&, VectorMutable> iterator;
    9796
    9897    /**
    9998       \brief default constructor
    10099    */
    101     VectorBase(void);
     100    VectorMutable(void);
    102101
    103102    /**
    104103       \brief Constructor.
    105104    */
    106     VectorBase(gsl_vector*);
     105    VectorMutable(gsl_vector*);
    107106
    108107    /**
    109108       \brief Constructor.
    110109    */
    111     VectorBase(const gsl_vector*);
     110    VectorMutable(const gsl_vector*);
    112111
    113112    ///
    114113    /// The destructor.
    115114    ///
    116     virtual ~VectorBase(void);
     115    virtual ~VectorMutable(void);
    117116
    118117    ///
     
    126125    iterator begin(void);
    127126
    128     /**
    129        \return read-only iterator to start of VectorBase
    130      */
    131     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$.
     127    // to allow overload from base class
     128    using VectorBase::begin;
     129
     130    /**
     131       \brief This function performs element-wise division, \f$
     132       this_i = this_i / other_i \; \forall i \f$.
    136133
    137134       \throw GSL_error if dimensions mis-match.
     
    140137
    141138    /**
    142        \return mutable iterator to end of VectorBase
     139       \return mutable iterator to end of VectorMutable
    143140     */
    144141    iterator end(void);
    145142
    146     /**
    147        \return read-only iterator to end of VectorBase
    148      */
    149     const_iterator end(void) const;
    150 
    151     /**
    152        \brief Check whether VectorBases are equal within a user defined
    153        precision, set by \a precision.
    154 
    155        \return True if each element deviates less or equal than \a
    156        d. If any VectorBase contain a NaN, false is always returned.
    157 
    158        \see operator== and operator!=
    159     */
    160     bool equal(const VectorBase&, const double precision=0) const;
    161 
    162     ///
    163     /// @return A pointer to the internal GSL vector,
    164     ///
     143    // to allow overload from base class
     144    using VectorBase::end;
     145
     146    /**
     147       @return A pointer to the internal GSL vector,
     148    */
    165149    gsl_vector* gsl_vector_p(void);
    166150
    167     ///
    168     /// @return A const pointer to the internal GSL vector,
    169     ///
    170     const gsl_vector* gsl_vector_p(void) const;
    171 
    172     /**
    173    
     151    using VectorBase::gsl_vector_p;
     152
     153    /**
    174154       Check if the vector object is a view (sub-vector) to another
    175155       vector.
     
    189169
    190170    /**
    191        \brief Reverse the order of elements in the VectorBase.
     171       \brief Reverse the order of elements in the VectorMutable.
    192172    */
    193173    void reverse(void);
    194174
    195     ///
    196     /// @return the number of elements in the VectorBase.
    197     ///
    198     size_t size(void) const;
    199 
    200175    /**
    201176       \brief Exchange elements \a i and \a j.
    202 
    203        \throw GSL_error if VectorBase lengths differs.
    204177    */
    205178    void swap(size_t i, size_t j);
     
    217190    // Peter, remove this one
    218191    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     */
    229     const double& operator()(size_t i) const;
    230     // Peter, remove this one
    231     const double& operator[](size_t i) const;
    232 
    233     /**
    234        \brief Comparison operator. Takes linear time.
    235 
    236        Checks are performed with exact matching, i.e., rounding off
    237        effects may destroy comparison. Use the equal function for
    238        comparing elements within a user defined precision.
    239 
    240        \return True if all elements are equal otherwise false.
    241 
    242        \see equal(const VectorBase&, const double precision=0)
    243     */
    244     bool operator==(const VectorBase&) const;
    245 
    246     /**
    247        \brief Comparison operator. Takes linear time.
    248 
    249        Checks are performed with exact matching, i.e., rounding off
    250        effects may destroy comparison. Use the equal function for
    251        comparing elements within a user defined precision.
    252 
    253        \return False if all elements are equal otherwise true.
    254 
    255        \see equal(const VectorBase&, const double precision=0)
    256     */
    257     bool operator!=(const VectorBase&) const;
    258 
    259     ///
    260     /// @return The dot product.
    261     ///
    262     double operator*(const VectorBase&) const;
    263 
    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&);
     192    // to allow overload from base class
     193    using VectorBase::operator();
     194    using VectorBase::operator[];
    277195
    278196    /**
     
    284202       \throw GSL_error if dimensions mis-match.
    285203    */
    286     const VectorBase& operator+=(const VectorBase&);
     204    const VectorMutable& operator+=(const VectorBase&);
    287205
    288206    /**
     
    292210       \return A const reference to the resulting VectorBase.
    293211    */
    294     const VectorBase& operator+=(double d);
     212    const VectorMutable& operator+=(double d);
    295213
    296214    /**
     
    302220       \throw GSL_error if dimensions mis-match.
    303221    */
    304     const VectorBase& operator-=(const VectorBase&);
     222    const VectorMutable& operator-=(const VectorBase&);
    305223
    306224    /**
     
    310228       \return A const reference to the resulting VectorBase.
    311229    */
    312     const VectorBase& operator-=(double d);
     230    const VectorMutable& operator-=(double d);
    313231
    314232    /**
     
    318236       \return A const reference to the resulting VectorBase.
    319237    */
    320     const VectorBase& operator*=(double d);
    321 
    322 
     238    const VectorMutable& operator*=(double d);
    323239
    324240  protected:
    325241    gsl_vector* vec_;
    326     const gsl_vector* const_vec_;
    327 
    328     /*
     242
     243    // Peter document
    329244    struct proxy
    330245    {
    331246      gsl_vector* vec_;
    332247    };
    333     */
    334248
    335249  private:
    336250    // copy assignment no allowed
    337     const VectorBase& operator=(const VectorBase&);
     251    const VectorMutable& operator=(const VectorMutable&);
    338252  public:
    339253    /**
    340254     */
    341     //operator proxy();
     255    operator proxy();
    342256
    343257  };
    344 
    345   /**
    346      \brief Check if all elements of the VectorBase are zero.
    347 
    348      \return True if all elements in the VectorBase is zero, false
    349      othwerwise.
    350   */
    351   bool isnull(const VectorBase&);
    352 
    353   /**
    354      \brief Get the maximum value of the VectorBase.
    355 
    356      \return The maximum value of the VectorBase.
    357   */
    358   double max(const VectorBase&);
    359 
    360   /**
    361      \brief Locate the maximum value in the VectorBase.
    362 
    363      \return The index to the maximum value of the VectorBase.
    364 
    365      \note Lower index has precedence.
    366   */
    367   size_t max_index(const VectorBase&);
    368 
    369   /**
    370      \brief Get the minimum value of the VectorBase.
    371 
    372      \return The minimum value of the VectorBase.
    373   */
    374   double min(const VectorBase&);
    375 
    376   /**
    377      \brief Locate the minimum value in the VectorBase.
    378 
    379      \return The index to the minimum value of the VectorBase.
    380 
    381      \note Lower index has precedence.
    382   */
    383   size_t min_index(const VectorBase&);
    384 
    385   /**
    386      \brief Create a VectorBase \a flag indicating NaN's in another VectorBase
    387      \a templat.
    388 
    389      The \a flag VectorBase is changed to contain 1's and 0's only. A 1
    390      means that the corresponding element in the \a templat VectorBase is
    391      valid and a zero means that the corresponding element is a NaN.
    392 
    393      \note Space for VectorBase \a flag is reallocated to fit the size of
    394      VectorBase \a templat if sizes mismatch.
    395 
    396      \return True if the \a templat VectorBase contains at least one NaN.
    397   */
    398   bool nan(const VectorBase& templat, vector& flag);
    399258
    400259  /**
    401260     Randomly shuffles the elements in VectorBase \a invec
    402261  */
    403   void shuffle(VectorBase& invec);
     262  void shuffle(VectorMutable& invec);
    404263
    405264  /**
    406265     Sort the elements in the VectorBase.
    407266  */
    408   void sort(VectorBase&);
    409 
    410   /**
    411      Create a vector \a sort_index containing the indeces of
    412      elements in a another VectorBase \a invec.  The elements of \a
    413      sort_index give the index of the VectorBase element which would
    414      have been stored in that position if the VectorBase had been sorted
    415      in place. The first element of \a sort_index gives the index of the least
    416      element in \a invec, and the last element of \a sort_index gives the
    417      index of the greatest element in \a invec . The VectorBase \a invec
    418      is not changed.
    419   */
    420   void sort_index(std::vector<size_t>& sort_index, const VectorBase& invec);
    421 
    422   /**
    423       Similar to sort_index but creates a VectorBase with indices to the \a k
    424   smallest elements in \a invec. 
    425   */
    426   void sort_smallest_index(std::vector<size_t>& sort_index, size_t k,
    427                            const VectorBase& invec);
    428 
    429   /**
    430       Similar to sort_index but creates a VectorBase with indices to the \a k
    431   largest elements in \a invec. 
    432   */
    433   void sort_largest_index(std::vector<size_t>& sort_index, size_t k,
    434                           const VectorBase& invec);
    435 
    436   /**
    437      \brief Calculate the sum of all VectorBase elements.
    438 
    439      \return The sum.
    440   */
    441   double sum(const VectorBase&);
    442 
    443   /**
    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   /**
    453      \brief The output operator for the VectorBase class.
    454   */
    455   std::ostream& operator<<(std::ostream&, const VectorBase&);
     267  void sort(VectorMutable&);
    456268
    457269}}} // of namespace utility, yat, and theplu
  • trunk/yat/utility/VectorView.cc

    r1026 r1027  
    2626
    2727#include "VectorView.h"
     28#include "VectorMutable.h"
    2829#include "matrix.h"
    2930#include "utility.h"
     
    4445
    4546  VectorView::VectorView(void)
    46     : VectorBase(), view_(NULL), const_view_(NULL)
     47    : VectorMutable(), view_(NULL)
    4748  {
    4849  }
     
    5051
    5152  VectorView::VectorView(VectorView& other)
    52     : VectorBase(), const_view_(NULL)
     53    : VectorMutable()
    5354  {
    5455    view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(),
     
    5960
    6061
    61   VectorView::VectorView(const VectorView& other)
    62     : VectorBase(), view_(NULL)
     62  VectorView::VectorView(VectorMutable& other)
     63    : VectorMutable()
    6364  {
    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()), const_view_(NULL)
    72   {
    73     view_ =
    74       new gsl_vector_view(gsl_vector_subvector_with_stride(other.gsl_vector_p(),
    75                                                            0, 1,other.size()));
     65    view_ = new gsl_vector_view(gsl_vector_subvector(other.gsl_vector_p(),
     66                                                     0,other.size()));
    7667    const_vec_ = vec_ = &(view_->vector);
    7768  }
    7869
    7970
    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(), const_view_(NULL)
     71  VectorView::VectorView(VectorMutable& v,size_t offset,size_t n,size_t stride)
     72    : VectorMutable()
    8973  {
    9074    view_ =
     
    9579
    9680
    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 
    10881  VectorView::VectorView(matrix& m, size_t i, bool row)
    109     : VectorBase(), const_view_(NULL)
     82    : VectorMutable()
    11083  {
    11184    view_=new gsl_vector_view(row ?
     
    11689
    11790
    118   VectorView::VectorView(const matrix& m, size_t i, bool row)
    119     : VectorBase(), view_(NULL)
     91  VectorView::VectorView(proxy p)
     92    : VectorMutable(p.vec_), view_(NULL)
    12093  {
    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);
    12694  }
    12795
    128 
    129   /*
    130   VectorView::VectorView(proxy p)
    131     : VectorBase()
    132   {
    133     view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_, 0, p.vec_->size));
    134     const_vec_ = vec_ = &(view_->vector);
    135   }
    136   */
    13796
    13897  VectorView::~VectorView(void)
     
    173132
    174133 
    175   /*
    176   const VectorView& VectorView::operator=(proxy p)
    177   {
    178     if (size()!=p.vec_->size)
    179       throw utility::GSL_error("VectorView::dimension mis-match");
    180     if (!size())
    181       return *this;
    182     clean_up();
    183     view_ = new gsl_vector_view(gsl_vector_subvector(p.vec_,0, p.vec_->size));
    184     const_vec_=vec_ = &view_->vector;
    185     return *this;
    186   }
    187   */
    188 
    189134  void VectorView::delete_allocated_memory(void)
    190135  {
     
    193138      view_=NULL;
    194139    }
    195     else if (const_view_){
    196       delete const_view_;
    197       const_view_=NULL;
    198     }
    199140    const_vec_ = vec_ = NULL;
    200141  }
  • trunk/yat/utility/VectorView.h

    r1026 r1027  
    2929*/
    3030
    31 #include "VectorBase.h"
     31#include "VectorMutable.h"
    3232#include "Exception.h"
    3333
     
    8585  */
    8686
    87   class VectorView : public VectorBase
     87  class VectorView : public VectorMutable
    8888  {
    8989  public:
     
    9696       \brief The copy constructor.
    9797    */
    98     VectorView(VectorBase& other);
     98    VectorView(VectorMutable& other);
    9999    VectorView(VectorView& other);
    100     // Peter, privatize
    101     VectorView(const VectorView& other);
    102100
    103101    /**
     
    120118       \throw GSL_error if a view cannot be set up.
    121119    */
    122     VectorView(VectorBase& v, size_t offset, size_t n, size_t stride=1);
    123     // Peter, privatize
    124     VectorView(const VectorBase& v, size_t offset, size_t n, size_t stride=1);
     120    VectorView(VectorMutable& v, size_t offset, size_t n, size_t stride=1);
    125121
    126122    ///
     
    146142    VectorView(matrix& m, size_t i, bool row=true);
    147143
    148     // Peter should be private
    149     VectorView(const matrix& m, size_t i, bool row=true);
    150 
    151144    /**
    152145     */
    153     //VectorView(proxy p);
     146    VectorView(proxy p);
    154147
    155148    ///
     
    173166    const VectorView& operator=(const VectorView&);
    174167
    175     //const VectorBase& operator=(proxy);
    176 
    177168  private:
    178169    const VectorView& assign(const VectorBase& other);
  • trunk/yat/utility/matrix.cc

    r1017 r1027  
    2828#include "vector.h"
    2929#include "VectorBase.h"
     30#include "VectorConstView.h"
    3031#include "VectorView.h"
    3132#include "utility.h"
     
    311312
    312313
    313   const VectorView matrix::column_vec(size_t col) const
    314   {
    315     return VectorView(*this, col, false);
    316   }
    317 
    318 
    319   const VectorView matrix::row_vec(size_t col) const
    320   {
    321     return VectorView(*this, col, true);
     314  const VectorConstView matrix::column_const_vec(size_t col) const
     315  {
     316    return VectorConstView(*this, col, false);
     317  }
     318
     319
     320  const VectorConstView matrix::row_const_vec(size_t col) const
     321  {
     322    return VectorConstView(*this, col, true);
    322323  }
    323324
     
    563564    utility::vector res(m.rows());
    564565    for (size_t i=0; i<res.size(); ++i)
    565       res(i) = VectorView(m,i) * v;
     566      res(i) = VectorConstView(m,i) * v;
    566567    return res;
    567568  }
     
    572573    utility::vector res(m.columns());
    573574    for (size_t i=0; i<res.size(); ++i)
    574       res(i) = v * VectorView(m,i,false);
     575      res(i) = v * VectorConstView(m,i,false);
    575576    return res;
    576577  }
  • trunk/yat/utility/matrix.h

    r1017 r1027  
    2929
    3030#include "vector.h"
     31#include "VectorConstView.h"
    3132#include "VectorView.h"
    3233#include "Exception.h"
     
    161162    /**
    162163     */
    163     const VectorView column_vec(size_t) const;
     164    const VectorConstView column_const_vec(size_t) const;
    164165
    165166    ///
     
    236237    /**
    237238     */
    238     const VectorView row_vec(size_t) const;
     239    const VectorConstView row_const_vec(size_t) const;
    239240
    240241    /**
  • trunk/yat/utility/vector.cc

    r1026 r1027  
    4444
    4545  vector::vector(void)
    46     : VectorBase()
     46    : VectorMutable()
    4747  {
    4848  }
     
    5050
    5151  vector::vector(size_t n, double init_value)
    52     : VectorBase(gsl_vector_alloc(n))
     52    : VectorMutable(gsl_vector_alloc(n))
    5353  {
    5454    if (!vec_)
     
    6060
    6161  vector::vector(const vector& other)
    62     : VectorBase(create_gsl_vector_copy(other))
     62    : VectorMutable(create_gsl_vector_copy(other))
    6363  {
    6464  }
     
    6666
    6767  vector::vector(const VectorBase& other)
    68     : VectorBase(create_gsl_vector_copy(other))
     68    : VectorMutable(create_gsl_vector_copy(other))
    6969  {
    7070  }
     
    7373  vector::vector(std::istream& is, char sep)
    7474    throw (utility::IO_error, std::exception)
    75     : VectorBase()
     75    : VectorMutable()
    7676  {
    7777    // read the data file and store in stl vectors (dynamically
  • trunk/yat/utility/vector.h

    r1026 r1027  
    2929*/
    3030
    31 #include "VectorBase.h"
     31#include "VectorMutable.h"
    3232#include "Exception.h"
    33 #include "Iterator.h"
    3433
    3534#include <iostream>
     
    8584  */
    8685
    87   class vector : public VectorBase
     86  class vector : public VectorMutable
    8887  {
    8988  public:
Note: See TracChangeset for help on using the changeset viewer.