Changeset 1169 for trunk/yat


Ignore:
Timestamp:
Feb 26, 2008, 11:09:04 PM (16 years ago)
Author:
Peter
Message:

refs #343 moving data to inherited classes and using SmartPtr?.

Location:
trunk/yat/classifier
Files:
7 edited

Legend:

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

    r1134 r1169  
    3838
    3939  DataLookup2D::DataLookup2D(const bool own)
    40     : ref_count_(NULL)
    4140  {
    42     if (own)
    43       ref_count_ = new u_int(1);
    4441  }
    4542
     
    4845                             const utility::Index& row,
    4946                             const utility::Index& col)
    50     : ref_count_(NULL)
    5147  {
    5248    row_index_ = utility::Index(m.row_index_, row);
     
    5955                             const utility::Index& index,
    6056                             const bool row)
    61     : ref_count_(NULL)
    6257  {
    6358    if (row){
     
    7570                             const utility::Index& col,
    7671                             const bool own)
    77     : row_index_(row),column_index_(col), ref_count_(NULL)
     72    : row_index_(row),column_index_(col)
    7873  {
    79     if (own)
    80       ref_count_ = new u_int(1);
    8174  }
    8275
     
    8477
    8578  DataLookup2D::DataLookup2D(const DataLookup2D& mv)
    86     : row_index_(mv.row_index_),column_index_(mv.column_index_),
    87       ref_count_(NULL)
     79    : row_index_(mv.row_index_),column_index_(mv.column_index_)
    8880  {
    8981  }
     
    9284  DataLookup2D::DataLookup2D(const size_t rows, const size_t columns)
    9385    : row_index_(std::vector<size_t>(rows,0)),
    94       column_index_(std::vector<size_t>(columns,0)), ref_count_(NULL)
    95 
     86      column_index_(std::vector<size_t>(columns,0))
    9687  {
    9788  }
     
    116107
    117108
    118   double DataLookup2D::weight(size_t i, size_t j) const
    119   {
    120     return 1.0;
    121   }
    122 
    123 
    124109  const DataLookup2D& DataLookup2D::operator=(const DataLookup2D& other)
    125110  {
     
    132117  }
    133118
    134   std::ostream& operator<<(std::ostream& s, const DataLookup2D& m)
    135   {
    136     s.setf(std::ios::dec);
    137     s.precision(12);
    138     for(size_t i=0, j=0; i<m.rows(); i++)
    139       for (j=0; j<m.columns(); j++) {
    140         s << m(i,j);
    141         if (j<m.columns()-1)
    142           s << s.fill();
    143         else if (i<m.rows()-1)
    144           s << "\n";
    145       }
    146     return s;
    147   }
    148 
    149119}}} // of namespace classifier, yat, and theplu
  • trunk/yat/classifier/DataLookup2D.h

    r1167 r1169  
    123123    size_t rows(void) const;
    124124
    125     /**
    126        \return data
    127     */
    128     virtual double weight(size_t i, size_t j) const;
    129    
    130125    ///
    131126    /// Is lookup weighted?
     
    156151    utility::Index column_index_;
    157152
    158     ///
    159     /// poiter telling how many owners to underlying data. NULL if
    160     /// this is not an owner.
    161     ///
    162     u_int* ref_count_;
    163    
    164153  }; 
    165154 
  • trunk/yat/classifier/MatrixLookup.cc

    r1168 r1169  
    3636
    3737  MatrixLookup::MatrixLookup(const utility::Matrix& data, const bool own)
    38     : DataLookup2D(own), data_(&data)
     38    : DataLookup2D(own), data_(MatrixP(&data, own))
    3939  {
    4040    column_index_ = utility::Index(data.columns());
     
    4747                             const utility::Index& row,
    4848                             const utility::Index& col)
    49     : DataLookup2D(row,col), data_(&data)
     49    : DataLookup2D(row,col), data_(MatrixP(&data, false))
    5050  {
    5151  }
     
    5656                             const utility::Index& index,
    5757                             const bool row)
    58     : DataLookup2D(), data_(&data)
     58    : DataLookup2D(), data_(MatrixP(&data, false))
    5959  {
    6060    if (row){
     
    7373    : DataLookup2D(other), data_(other.data_)
    7474  {
    75     ref_count_=other.ref_count_;
    76     if (ref_count_)
    77       ++(*ref_count_);
    7875  }
    7976
     
    8582    : DataLookup2D(other,row,col), data_(other.data_)
    8683  {
    87     ref_count_=other.ref_count_;
    88     if (ref_count_)
    89       ++(*ref_count_);
    9084  }
    9185 
     
    9690    : DataLookup2D(other,index,row), data_(other.data_)
    9791  {
    98     ref_count_=other.ref_count_;
    99     if (ref_count_)
    100       ++(*ref_count_);
    101 
    10292  }
    10393 
     
    10898    : DataLookup2D(rows,columns)
    10999  {
    110     data_ = new utility::Matrix(1,1,value);
    111     ref_count_= new u_int(1);
     100    data_ = MatrixP(new utility::Matrix(1,1,value));
    112101  }
    113102
     
    116105    : DataLookup2D()
    117106  {
    118     data_ = new utility::Matrix(is,sep);
    119     ref_count_= new u_int(1);
     107    data_ = MatrixP(new utility::Matrix(is,sep));
    120108    row_index_ = utility::Index(data_->rows());
    121109    column_index_ = utility::Index(data_->columns());
     
    125113  MatrixLookup::~MatrixLookup(void)
    126114  {
    127     if (ref_count_)
    128       if (!--(*ref_count_))
    129         delete data_;
    130115  }
    131116
     
    191176  {
    192177    if (this!=&other){
    193       if (ref_count_ && !--(*ref_count_))
    194         delete data_;
    195178      DataLookup2D::operator=(other);
    196179      data_ = other.data_;
    197       ref_count_=other.ref_count_;
    198       if (ref_count_)
    199         ++(*ref_count_);
    200180    }
    201181    return *this;
     
    203183
    204184
     185  std::ostream& operator<<(std::ostream& s, const MatrixLookup& m)
     186  {
     187    s.setf(std::ios::dec);
     188    s.precision(12);
     189    for(size_t i=0, j=0; i<m.rows(); i++)
     190      for (j=0; j<m.columns(); j++) {
     191        s << m(i,j);
     192        if (j<m.columns()-1)
     193          s << s.fill();
     194        else if (i<m.rows()-1)
     195          s << "\n";
     196      }
     197    return s;
     198  }
     199
    205200
    206201
  • trunk/yat/classifier/MatrixLookup.h

    r1168 r1169  
    3131#include "yat/utility/Index.h"
    3232#include "yat/utility/iterator_traits.h"
     33#include "yat/utility/SmartPtr.h"
    3334#include "yat/utility/StrideIterator.h"
    3435
     
    295296    friend class MatrixLookupWeighted;
    296297
    297     const utility::Matrix* data_;
     298    typedef utility::SmartPtr<const utility::Matrix> MatrixP;
     299    MatrixP data_;
    298300  }; 
    299301 
     302  ///
     303  /// The output operator DataLookup2D
     304  ///
     305  std::ostream& operator<< (std::ostream& s, const MatrixLookup&);
     306
    300307}}} // of namespace classifier, yat, and theplu
    301308
  • trunk/yat/classifier/MatrixLookupWeighted.cc

    r1168 r1169  
    3838                                             const utility::Matrix& weights,
    3939                                             const bool own)
    40     : DataLookup2D(own), data_(&data), weights_(&weights),
    41       ref_count_weights_(NULL)
     40    : data_(MatrixP(&data, own)), weights_(MatrixP(&weights, own))
    4241  {
    4342    assert(data.rows()==weights.rows());
     
    4948
    5049  MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data)
    51     : DataLookup2D(), data_(&data)
     50    : DataLookup2D(), data_(MatrixP(&data, false))
    5251  {
    5352    utility::Matrix weights;
    5453    utility::nan(*data_,weights);
    55     weights_= new utility::Matrix(weights);
    56     ref_count_weights_=new u_int(1);
     54    weights_= MatrixP(new utility::Matrix(weights));
    5755    row_index_ = utility::Index(data.rows());
    5856    column_index_ = utility::Index(data.columns());
     
    6361    : DataLookup2D(ml), data_(ml.data_)
    6462  {
    65     weights_= new utility::Matrix(data_->rows(), data_->columns(), 1.0);
    66     ref_count_weights_=new u_int(1);
    67     ref_count_=ml.ref_count_;
    68     if (ref_count_)
    69       ++(*ref_count_);
    70 
     63    weights_= MatrixP(new utility::Matrix(data_->rows(), data_->columns(),1.0));
    7164  }
    7265 
     
    7669                                             const utility::Index& row,
    7770                                             const utility::Index& col)
    78     : DataLookup2D(row,col), data_(&data), weights_(&weights),
    79       ref_count_weights_(NULL)
     71    : DataLookup2D(row,col), data_(MatrixP(new utility::Matrix(data), false)),
     72      weights_(MatrixP(new utility::Matrix(weights), false))
    8073  {
    8174  }
     
    8780                                             const utility::Index& index,
    8881                                             const bool row)
    89     : DataLookup2D(), data_(&data), weights_(&weights),
    90       ref_count_weights_(NULL)
     82    : DataLookup2D(), data_(MatrixP(new utility::Matrix(data), false)),
     83      weights_(MatrixP(new utility::Matrix(weights), false))
    9184  {
    9285    assert(data.rows()==weights.rows());
     
    115108    : DataLookup2D(other), data_(other.data_), weights_(other.weights_)
    116109  {
    117     ref_count_ = other.ref_count_;
    118     if (ref_count_)
    119       ++(*ref_count_);
    120     ref_count_weights_ = other.ref_count_weights_;
    121     if (ref_count_weights_)
    122       ++(*ref_count_weights_);
    123 
    124110  }
    125111
     
    131117    : DataLookup2D(other,row,col), data_(other.data_), weights_(other.weights_)
    132118  {
    133     ref_count_ = other.ref_count_;
    134     if (ref_count_)
    135       ++(*ref_count_);
    136     ref_count_weights_ = other.ref_count_weights_;
    137     if (ref_count_weights_)
    138       ++(*ref_count_weights_);
    139119  }
    140120 
     
    147127      weights_(other.weights_)
    148128  {
    149     ref_count_ = other.ref_count_;
    150     if (ref_count_)
    151       ++(*ref_count_);
    152     ref_count_weights_ = other.ref_count_weights_;
    153     if (ref_count_weights_)
    154       ++(*ref_count_weights_);
    155 
    156129  }
    157130 
     
    162135                                             const double value,
    163136                                             const double weight)
    164     : DataLookup2D(rows,columns)
    165   {
    166     data_ = new utility::Matrix(1,1,value);
    167     ref_count_=new u_int(1);
    168     weights_ = new utility::Matrix(1,1,weight);
    169     ref_count_weights_=new u_int(1);
     137    : DataLookup2D(rows,columns),
     138      data_(MatrixP(new utility::Matrix(1,1,value))),
     139      weights_(MatrixP(new utility::Matrix(1,1,weight)))
     140  {
    170141  }
    171142
     
    174145    : DataLookup2D()
    175146  {
    176     data_ = new utility::Matrix(is,sep);
    177     ref_count_=new u_int(1);
     147    data_ = MatrixP(new utility::Matrix(is,sep));
    178148    row_index_ = utility::Index(data_->rows());
    179149    column_index_ = utility::Index(data_->columns());
    180150    utility::Matrix weights;
    181151    utility::nan(*data_,weights);
    182     weights_= new utility::Matrix(weights);
    183     ref_count_weights_=new u_int(1);
     152    // Peter, should be possible to avoid this copying
     153    weights_= MatrixP(new utility::Matrix(weights));
    184154  }
    185155 
     
    187157  MatrixLookupWeighted::~MatrixLookupWeighted(void)
    188158  {
    189     if (ref_count_)
    190       if (!--(*ref_count_))
    191         delete data_;
    192     if (ref_count_weights_)
    193       if (!--(*ref_count_weights_))
    194         delete weights_;
    195159  }
    196160
     
    274238  {
    275239    if (this!=&other){
    276       if (ref_count_ && !--(*ref_count_))
    277         delete data_;
    278       if (ref_count_weights_ && !--(*ref_count_weights_))
    279         delete weights_;
    280240      DataLookup2D::operator=(other);
    281241      data_ = other.data_;
    282       ref_count_=other.ref_count_;
    283       if (ref_count_)
    284         ++(*ref_count_);
    285242      weights_ = other.weights_;
    286       ref_count_weights_ = other.ref_count_weights_;
    287       if (ref_count_weights_)
    288         ++(*ref_count_weights_);
    289243    }
    290244    return *this;
     
    292246
    293247
    294   std::ostream& operator<<(std::ostream& s, const MatrixLookupWeighted& m)
    295   {
    296     s.setf(std::ios::dec);
    297     s.precision(12);
    298     for(size_t i=0, j=0; i<m.rows(); i++)
    299       for (j=0; j<m.columns(); j++) {
    300         if (m.weight(i,j))
    301           s << m.data(i,j);
    302         if (j<m.columns()-1)
    303           s << s.fill();
    304         else if (i<m.rows()-1)
    305           s << "\n";
    306       }
    307     return s;
    308   }
    309 
    310 
    311 
    312248}}} // of namespace classifier, yat, and theplu
  • trunk/yat/classifier/MatrixLookupWeighted.h

    r1168 r1169  
    2929#include "yat/utility/Container2DIterator.h"
    3030#include "yat/utility/IteratorPolicy.h"
     31#include "yat/utility/SmartPtr.h"
    3132#include "yat/utility/StrideIterator.h"
    3233
     
    331332   
    332333  private:
    333     const utility::Matrix* data_;
    334     const utility::Matrix* weights_;
    335     u_int* ref_count_weights_;
     334    typedef utility::SmartPtr<const utility::Matrix> MatrixP;
     335    MatrixP data_;
     336    MatrixP weights_;
    336337  }; 
    337338 
  • trunk/yat/classifier/NBC.h

    r1160 r1169  
    8989
    9090       \f$ P_j = \frac{1}{Z}\prod_i{\frac{1}{\sqrt{2\pi\sigma_i^2}}}
    91        \exp(\frac{w_i(x_i-\mu_i)^2}{\sigma_i^2})\f$, where \f$ \mu_i
     91       \exp(\frac{(x_i-\mu_i)^2}{\sigma_i^2})\f$, where \f$ \mu_i
    9292       \f$ and \f$ \sigma_i^2 \f$ are the estimated mean and variance,
    9393       respectively. If a \f$ \sigma_i \f$ could not be estimated
     
    9595       words, that feature is ignored for the prediction of that
    9696       particular class. Z is chosen such that total probability, \f$
    97        \sum P_j \f$, equals unity. If \a data is a MatrixLookup is
    98        equivalent to using all weight equal to unity.
     97       \sum P_j \f$, equals unity.
    9998    */
    10099    void predict(const MatrixLookup& data, utility::Matrix& res) const;
    101100
    102101    /**
    103        @see above
     102       Each sample (column) in \a data is predicted and predictions
     103       are returned in the corresponding column in passed \a res. Each
     104       row in \a res corresponds to a class. The prediction is the
     105       estimated probability that sample belong to class \f$ j \f$
    104106     */
    105107    void predict(const MatrixLookupWeighted& data, utility::Matrix& res) const;
Note: See TracChangeset for help on using the changeset viewer.