Changeset 1587 for trunk/yat/classifier


Ignore:
Timestamp:
Oct 17, 2008, 5:31:42 PM (13 years ago)
Author:
Peter
Message:

closes #396

Location:
trunk/yat/classifier
Files:
2 edited

Legend:

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

    r1581 r1587  
    4141                                             const utility::Index& rows,
    4242                                             const utility::Index& columns)
    43     : column_index_(columns), row_index_(rows)
    44   {
    45     utility::Matrix* data = new utility::Matrix(m.rows(), m.columns());
    46     utility::Matrix* weight = new utility::Matrix(m.rows(), m.columns());
    47     for (size_t i=0; i<m.rows(); ++i)
    48       for (size_t j=0; j<m.columns(); ++j) {
    49         (*data)(i,j) = m(i,j).data();
    50         (*weight)(i,j) = m(i,j).weight();
    51       }
    52     // smart pointers are taking ownership
    53     data_ = MatrixP(data);
    54     weights_ = MatrixP(weight);
     43    : column_index_(columns), data_(MatrixWP(&m,false)), row_index_(rows)
     44  {
     45    assert(validate());
    5546  }
    5647
     
    5950                                             bool owner)
    6051    : column_index_(utility::Index(m.columns())),
     52      data_(MatrixWP(&m,owner)),
    6153      row_index_(utility::Index(m.rows()))
    6254  {
    63     // Peter, remember to take care of ownership (but for now leave a leak)
    64 
    65     utility::Matrix* data = new utility::Matrix(m.rows(), m.columns());
    66     utility::Matrix* weight = new utility::Matrix(m.rows(), m.columns());
    67     for (size_t i=0; i<m.rows(); ++i)
    68       for (size_t j=0; j<m.columns(); ++j) {
    69         (*data)(i,j) = m(i,j).data();
    70         (*weight)(i,j) = m(i,j).weight();
    71       }
    72   // smart pointers are taking ownership
    73     data_ = MatrixP(data);
    74     weights_ = MatrixP(weight);
    75   }
    76 
    77 
    78   MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data,
    79                                              const utility::Matrix& weights,
    80                                              const bool own)
    81     : data_(MatrixP(&data, own)), weights_(MatrixP(&weights, own))
    82   {
    83     assert(data.rows()==weights.rows());
    84     assert(data.columns()==weights.columns());
    85     row_index_ = utility::Index(data.rows());
    86     column_index_ = utility::Index(data.columns());
    8755    assert(validate());
    8856  }
     
    9058
    9159  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& ml)
    92     : column_index_(ml.column_index_), data_(ml.data_),
    93       row_index_(ml.row_index_),
    94       weights_(MatrixP(new utility::Matrix(data_->rows(),data_->columns(),1.0)))
     60    : column_index_(ml.column_index_),
     61      row_index_(ml.row_index_)
    9562  {
    96     assert(validate());
    97   }
    98  
    99 
    100   MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data,
    101                                              const utility::Matrix& weights,
    102                                              const utility::Index& row,
    103                                              const utility::Index& col)
    104     : column_index_(col), data_(MatrixP(&data, false)),
    105       row_index_(row), weights_(MatrixP(&weights, false))
    106   {
    107     assert(validate());
    108   }
    109  
    110 
    111 
    112   /*
    113   MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data,
    114                                              const utility::Matrix& weights,
    115                                              const utility::Index& index,
    116                                              const bool row)
    117     : data_(MatrixP(new utility::Matrix(data), false)),
    118       weights_(MatrixP(new utility::Matrix(weights), false))
    119   {
    120     assert(data.rows()==weights.rows());
    121     assert(data.columns()==weights.columns());
    122     if (row){
    123       row_index_=index;
    124       column_index_ = utility::Index(data.columns());
    125     }
    126     else{
    127       column_index_=index;
    128       row_index_ = utility::Index(data.rows());
    129     }
    130     assert(validate());
    131   }
    132   */
    133 
    134   /*
    135   MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& dv,
    136                                              const MatrixLookup& wv)
    137     : DataLookup2D(dv), data_(dv.data_), weights_(dv.data_)
    138   {
    139   }
    140   */
    141 
     63    utility::MatrixWeighted* mw = new utility::MatrixWeighted(*ml.data_);
     64    data_ = MatrixWP(mw);
     65    assert(validate());
     66  }
     67 
    14268
    14369  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other)
    14470    : column_index_(other.column_index_), data_(other.data_),
    145       row_index_(other.row_index_), weights_(other.weights_)
     71      row_index_(other.row_index_)
    14672  {
    14773    assert(validate());
     
    15480                                             const utility::Index& col)
    15581    : column_index_(utility::Index(other.column_index_, col)),
    156       data_(other.data_), row_index_(utility::Index(other.row_index_, row)),
    157       weights_(other.weights_)
     82      data_(other.data_), row_index_(utility::Index(other.row_index_, row))
    15883  {
    15984    assert(validate());
     
    16590                                             const utility::Index& index,
    16691                                             bool row)
    167     : data_(other.data_),
    168       weights_(other.weights_)
     92    : data_(other.data_)
    16993  {
    17094    if (row){
     
    186110                                             const double weight)
    187111    : column_index_(utility::Index(std::vector<size_t>(rows, 0))),
    188       data_(MatrixP(new utility::Matrix(1,1,value))),
    189       row_index_(utility::Index(std::vector<size_t>(columns, 0))),
    190       weights_(MatrixP(new utility::Matrix(1,1,weight)))
     112      data_(MatrixWP(new utility::MatrixWeighted(1,1,value, weight))),
     113      row_index_(utility::Index(std::vector<size_t>(columns, 0)))
    191114  {
    192115    assert(validate());
     
    195118 
    196119  MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep)
    197     : data_(MatrixP(new utility::Matrix(is,sep)))
     120    : data_(MatrixWP(new utility::MatrixWeighted(is,sep)))
    198121  {
    199122    column_index_ = utility::Index(data_->columns());
    200123    row_index_ = utility::Index(data_->rows());
    201     utility::Matrix weights;
    202     utility::nan(*data_,weights);
    203     // Peter, should be possible to avoid this copying
    204     weights_= MatrixP(new utility::Matrix(weights));
    205124    assert(validate());
    206125  }
     
    247166    assert(row_index_[row]<data_->rows());
    248167    assert(column_index_[column]<data_->columns());
    249     return (*data_)(row_index_[row], column_index_[column]);
     168    return (*this)(row, column).data();
    250169  }
    251170
     
    290209      if (column_index_[i]>=data_->columns())
    291210        return false;
    292     for (size_t i=0; i<row_index_.size(); ++i)
    293       if (row_index_[i]>=weights_->rows())
    294         return false;
    295     for (size_t i=0; i<column_index_.size(); ++i)
    296       if (column_index_[i]>=weights_->columns())
    297         return false;
    298211    return true;
    299212  }
     
    304217    assert(row<rows());
    305218    assert(column<columns());
    306     assert(row_index_[row]<weights_->rows());
    307     assert(column_index_[column]<weights_->columns());
    308     return (*weights_)(row_index_[row], column_index_[column]);
     219    return (*this)(row, column).weight();
    309220  }
    310221
     
    321232  MatrixLookupWeighted::operator()(const size_t row, const size_t column) const
    322233  {
    323     return utility::DataWeight(data(row, column), weight(row,column));
     234    return (*data_)(row_index_[row], column_index_[column]);
    324235  }
    325236
     
    333244      row_index_=other.row_index_;
    334245      data_ = other.data_;
    335       weights_ = other.weights_;
    336246    }
    337247    assert(validate());
  • trunk/yat/classifier/MatrixLookupWeighted.h

    r1581 r1587  
    7171  class MatrixLookupWeighted
    7272  {
    73  
    7473  public:
    7574    /**
     
    119118                                  bool owner=false);
    120119
    121     ///
    122     /// Constructor creating a lookup into the entire \a matrix and \a
    123     /// weights.
    124     ///
    125     /// @note If @a matrix or @a weights goes out of scope or is
    126     /// deleted, the MatrixLookupWeighted becomes invalid and the
    127     /// result of further use is undefined.
    128     ///
    129     /// \deprecated
    130     MatrixLookupWeighted(const utility::Matrix& matrix,
    131                          const utility::Matrix& weights,
    132                          const bool owner=false);// YAT_DEPRECATE;
    133    
    134120    /**
    135121       Constructor creating a MatrixLookupWeighted from a MatrixLookup. A
     
    138124       \note no check for nan is performed.
    139125       
    140        @note If underlying utility::Matrix goes out of scope or
    141        is deleted, the MatrixLookupWeighted becomes invalid and the
    142        result of further use is undefined.
     126       @note from yat 0.5 data is copied and further modifications in
     127       \a matrix will not be reflected in MatrixLookupWeighted.
    143128    */
    144129    explicit MatrixLookupWeighted(const MatrixLookup& matrix);
    145130
    146 
    147     ///
    148     /// Constructor creating a lookup into a sub-matrix of @a matrix.
    149     /// The @a row and @a column define what sub-matrix to look into,
    150     /// in other words, the created MatrixLookupWeighted will fullfill
    151     /// the following:
    152     /// MatrixLookupWeighted(i,j)=matrix(row[i],column[j])
    153     /// weights(row[i],column[j]). This also means that number of
    154     /// rows in created MatrixLookupWeighted is equal to size of
    155     /// @a row, and number of columns is equal to size of @a column.
    156     ///
    157     /// @note If @a matrix or @a weights goes out of scope or is deleted, the
    158     /// MatrixLookupWeighted becomes invalid and the result of further use is
    159     /// undefined.
    160     ///
    161     /// \deprecated
    162     MatrixLookupWeighted(const utility::Matrix& matrix,
    163                          const utility::Matrix& weights,
    164                          const utility::Index& row,
    165                          const utility::Index& column);// YAT_DEPRECATE;
    166 
    167     ///
    168     /// Constructor creating a lookup into a sub-matrix of @a matrix.
    169     ///
    170     /// If @a row_vectors is true the new MatrixLookupWeighted will be
    171     /// consist of the row vectors defined by @a index. This means
    172     /// that the created MatrixLookupWeighted will fullfill:
    173     /// MatrixLookupWeighted(i,j)=matrix(i,index[j])*weights(i,index[j])
    174     ///
    175     ///
    176     /// If @a row_vectors is false the new MatrixLookupWeighted will be consist
    177     /// of the rolumn vectors defined by @a index. This means that the
    178     /// created MatrixLookupWeighted will fullfill:
    179     ///
    180     ///
    181     /// @note If @a matrix or @a weights goes out of scope or is
    182     /// deleted, the MatrixLookupWeighted becomes invalid and the
    183     /// result of further use is undefined.
    184     ///
    185     /*
    186     MatrixLookupWeighted(const utility::Matrix& matrix,
    187                          const utility::Matrix& weights,
    188                          const utility::Index& index,
    189                          const bool row_vectors);
    190     */
    191131
    192132    ///
     
    266206    /// matrix.
    267207    ///
    268     /// @see matrix(istream&) for details.
     208    /// @see utility::MatrixWeighted(istream&) for details.
    269209    ///
    270210    MatrixLookupWeighted(std::istream&, char sep='\0');
     
    356296   
    357297  private:
    358     typedef utility::SmartPtr<const utility::Matrix> MatrixP;
     298    typedef utility::SmartPtr<const utility::MatrixWeighted> MatrixWP;
    359299    utility::Index column_index_;
    360     MatrixP data_;
     300    MatrixWP data_;
    361301    utility::Index row_index_;
    362     MatrixP weights_;
    363302
    364303    // for assertions
Note: See TracChangeset for help on using the changeset viewer.