Changeset 1170 for trunk/yat/classifier/MatrixLookupWeighted.cc
 Timestamp:
 Feb 27, 2008, 1:06:55 AM (15 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/yat/classifier/MatrixLookupWeighted.cc
r1169 r1170 27 27 #include "yat/utility/Matrix.h" 28 28 29 #include <algorithm>30 29 #include <cassert> 31 30 #include <fstream> 31 #include <vector> 32 32 33 33 namespace theplu { … … 44 44 row_index_ = utility::Index(data.rows()); 45 45 column_index_ = utility::Index(data.columns()); 46 assert(validate()); 46 47 } 47 48 48 49 49 50 MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data) 50 : DataLookup2D(),data_(MatrixP(&data, false))51 : data_(MatrixP(&data, false)) 51 52 { 52 53 utility::Matrix weights; … … 55 56 row_index_ = utility::Index(data.rows()); 56 57 column_index_ = utility::Index(data.columns()); 58 assert(validate()); 57 59 } 58 60 59 61 60 62 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& ml) 61 : DataLookup2D(ml), data_(ml.data_) 62 { 63 weights_= MatrixP(new utility::Matrix(data_>rows(), data_>columns(),1.0)); 63 : column_index_(ml.column_index_), data_(ml.data_), 64 row_index_(ml.row_index_), 65 weights_(MatrixP(new utility::Matrix(data_>rows(),data_>columns(),1.0))) 66 { 67 assert(validate()); 64 68 } 65 69 … … 69 73 const utility::Index& row, 70 74 const utility::Index& col) 71 : DataLookup2D(row,col), data_(MatrixP(new utility::Matrix(data), false)), 72 weights_(MatrixP(new utility::Matrix(weights), false)) 73 { 75 : column_index_(col), data_(MatrixP(new utility::Matrix(data), false)), 76 row_index_(row), weights_(MatrixP(new utility::Matrix(weights), false)) 77 { 78 assert(validate()); 74 79 } 75 80 … … 80 85 const utility::Index& index, 81 86 const bool row) 82 : DataLookup2D(),data_(MatrixP(new utility::Matrix(data), false)),87 : data_(MatrixP(new utility::Matrix(data), false)), 83 88 weights_(MatrixP(new utility::Matrix(weights), false)) 84 89 { … … 93 98 row_index_ = utility::Index(data.rows()); 94 99 } 100 assert(validate()); 95 101 } 96 102 … … 106 112 107 113 MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other) 108 : DataLookup2D(other), data_(other.data_), weights_(other.weights_) 109 { 114 : column_index_(other.column_index_), data_(other.data_), 115 row_index_(other.row_index_), weights_(other.weights_) 116 { 117 assert(validate()); 110 118 } 111 119 … … 115 123 const utility::Index& row, 116 124 const utility::Index& col) 117 : DataLookup2D(other,row,col), data_(other.data_), weights_(other.weights_) 118 { 125 : column_index_(utility::Index(other.column_index_, col)), 126 data_(other.data_), row_index_(utility::Index(other.row_index_, row)), 127 weights_(other.weights_) 128 { 129 assert(validate()); 119 130 } 120 131 … … 124 135 const utility::Index& index, 125 136 bool row) 126 : DataLookup2D(other,index,row),data_(other.data_),137 : data_(other.data_), 127 138 weights_(other.weights_) 128 139 { 140 if (row){ 141 row_index_ = utility::Index(other.row_index_, index); 142 column_index_= other.column_index_; 143 } 144 else{ 145 column_index_ = utility::Index(other.column_index_, index); 146 row_index_= other.row_index_; 147 } 148 assert(validate()); 129 149 } 130 150 … … 135 155 const double value, 136 156 const double weight) 137 : DataLookup2D(rows,columns),157 : column_index_(utility::Index(std::vector<size_t>(rows, 0))), 138 158 data_(MatrixP(new utility::Matrix(1,1,value))), 159 row_index_(utility::Index(std::vector<size_t>(columns, 0))), 139 160 weights_(MatrixP(new utility::Matrix(1,1,weight))) 140 161 { 162 assert(validate()); 141 163 } 142 164 143 165 144 166 MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep) 145 : DataLookup2D()146 { 147 data_ = MatrixP(new utility::Matrix(is,sep));167 : data_(MatrixP(new utility::Matrix(is,sep))) 168 { 169 column_index_ = utility::Index(data_>columns()); 148 170 row_index_ = utility::Index(data_>rows()); 149 column_index_ = utility::Index(data_>columns());150 171 utility::Matrix weights; 151 172 utility::nan(*data_,weights); 152 173 // Peter, should be possible to avoid this copying 153 174 weights_= MatrixP(new utility::Matrix(weights)); 175 assert(validate()); 154 176 } 155 177 … … 183 205 184 206 207 size_t MatrixLookupWeighted::columns(void) const 208 { 209 return column_index_.size(); 210 } 211 212 185 213 double MatrixLookupWeighted::data(size_t row, size_t column) const 186 214 { 215 assert(row<rows()); 216 assert(column<columns()); 217 assert(row_index_[row]<data_>rows()); 218 assert(column_index_[column]<data_>columns()); 187 219 return (*data_)(row_index_[row], column_index_[column]); 188 220 } … … 212 244 213 245 246 size_t MatrixLookupWeighted::rows(void) const 247 { 248 return row_index_.size(); 249 } 250 251 252 bool MatrixLookupWeighted::validate(void) const 253 { 254 for (size_t i=0; i<row_index_.size(); ++i) 255 if (row_index_[i]>=data_>rows()) 256 return false; 257 for (size_t i=0; i<column_index_.size(); ++i) 258 if (column_index_[i]>=data_>columns()) 259 return false; 260 for (size_t i=0; i<row_index_.size(); ++i) 261 if (row_index_[i]>=weights_>rows()) 262 return false; 263 for (size_t i=0; i<column_index_.size(); ++i) 264 if (column_index_[i]>=weights_>columns()) 265 return false; 266 return true; 267 } 268 269 214 270 double MatrixLookupWeighted::weight(size_t row, size_t column) const 215 271 { 272 assert(row<rows()); 273 assert(column<columns()); 274 assert(row_index_[row]<weights_>rows()); 275 assert(column_index_[column]<weights_>columns()); 216 276 return (*weights_)(row_index_[row], column_index_[column]); 217 277 } … … 238 298 { 239 299 if (this!=&other){ 240 DataLookup2D::operator=(other); 300 column_index_=other.column_index_; 301 row_index_=other.row_index_; 241 302 data_ = other.data_; 242 303 weights_ = other.weights_; 243 304 } 305 assert(validate()); 244 306 return *this; 245 307 }
Note: See TracChangeset
for help on using the changeset viewer.