Changeset 1587 for trunk/yat/classifier
- Timestamp:
- Oct 17, 2008, 5:31:42 PM (14 years ago)
- Location:
- trunk/yat/classifier
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/yat/classifier/MatrixLookupWeighted.cc
r1581 r1587 41 41 const utility::Index& rows, 42 42 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()); 55 46 } 56 47 … … 59 50 bool owner) 60 51 : column_index_(utility::Index(m.columns())), 52 data_(MatrixWP(&m,owner)), 61 53 row_index_(utility::Index(m.rows())) 62 54 { 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 ownership73 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());87 55 assert(validate()); 88 56 } … … 90 58 91 59 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_) 95 62 { 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 142 68 143 69 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other) 144 70 : column_index_(other.column_index_), data_(other.data_), 145 row_index_(other.row_index_) , weights_(other.weights_)71 row_index_(other.row_index_) 146 72 { 147 73 assert(validate()); … … 154 80 const utility::Index& col) 155 81 : 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)) 158 83 { 159 84 assert(validate()); … … 165 90 const utility::Index& index, 166 91 bool row) 167 : data_(other.data_), 168 weights_(other.weights_) 92 : data_(other.data_) 169 93 { 170 94 if (row){ … … 186 110 const double weight) 187 111 : 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))) 191 114 { 192 115 assert(validate()); … … 195 118 196 119 MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep) 197 : data_(Matrix P(new utility::Matrix(is,sep)))120 : data_(MatrixWP(new utility::MatrixWeighted(is,sep))) 198 121 { 199 122 column_index_ = utility::Index(data_->columns()); 200 123 row_index_ = utility::Index(data_->rows()); 201 utility::Matrix weights;202 utility::nan(*data_,weights);203 // Peter, should be possible to avoid this copying204 weights_= MatrixP(new utility::Matrix(weights));205 124 assert(validate()); 206 125 } … … 247 166 assert(row_index_[row]<data_->rows()); 248 167 assert(column_index_[column]<data_->columns()); 249 return (* data_)(row_index_[row], column_index_[column]);168 return (*this)(row, column).data(); 250 169 } 251 170 … … 290 209 if (column_index_[i]>=data_->columns()) 291 210 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;298 211 return true; 299 212 } … … 304 217 assert(row<rows()); 305 218 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(); 309 220 } 310 221 … … 321 232 MatrixLookupWeighted::operator()(const size_t row, const size_t column) const 322 233 { 323 return utility::DataWeight(data(row, column), weight(row,column));234 return (*data_)(row_index_[row], column_index_[column]); 324 235 } 325 236 … … 333 244 row_index_=other.row_index_; 334 245 data_ = other.data_; 335 weights_ = other.weights_;336 246 } 337 247 assert(validate()); -
trunk/yat/classifier/MatrixLookupWeighted.h
r1581 r1587 71 71 class MatrixLookupWeighted 72 72 { 73 74 73 public: 75 74 /** … … 119 118 bool owner=false); 120 119 121 ///122 /// Constructor creating a lookup into the entire \a matrix and \a123 /// weights.124 ///125 /// @note If @a matrix or @a weights goes out of scope or is126 /// deleted, the MatrixLookupWeighted becomes invalid and the127 /// result of further use is undefined.128 ///129 /// \deprecated130 MatrixLookupWeighted(const utility::Matrix& matrix,131 const utility::Matrix& weights,132 const bool owner=false);// YAT_DEPRECATE;133 134 120 /** 135 121 Constructor creating a MatrixLookupWeighted from a MatrixLookup. A … … 138 124 \note no check for nan is performed. 139 125 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. 143 128 */ 144 129 explicit MatrixLookupWeighted(const MatrixLookup& matrix); 145 130 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 fullfill151 /// the following:152 /// MatrixLookupWeighted(i,j)=matrix(row[i],column[j])153 /// weights(row[i],column[j]). This also means that number of154 /// rows in created MatrixLookupWeighted is equal to size of155 /// @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, the158 /// MatrixLookupWeighted becomes invalid and the result of further use is159 /// undefined.160 ///161 /// \deprecated162 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 be171 /// consist of the row vectors defined by @a index. This means172 /// 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 consist177 /// of the rolumn vectors defined by @a index. This means that the178 /// created MatrixLookupWeighted will fullfill:179 ///180 ///181 /// @note If @a matrix or @a weights goes out of scope or is182 /// deleted, the MatrixLookupWeighted becomes invalid and the183 /// 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 */191 131 192 132 /// … … 266 206 /// matrix. 267 207 /// 268 /// @see matrix(istream&) for details.208 /// @see utility::MatrixWeighted(istream&) for details. 269 209 /// 270 210 MatrixLookupWeighted(std::istream&, char sep='\0'); … … 356 296 357 297 private: 358 typedef utility::SmartPtr<const utility::Matrix > MatrixP;298 typedef utility::SmartPtr<const utility::MatrixWeighted> MatrixWP; 359 299 utility::Index column_index_; 360 Matrix P data_;300 MatrixWP data_; 361 301 utility::Index row_index_; 362 MatrixP weights_;363 302 364 303 // for assertions
Note: See TracChangeset
for help on using the changeset viewer.