source: branches/0.4-stable/yat/classifier/MatrixLookupWeighted.cc @ 1392

Last change on this file since 1392 was 1392, checked in by Peter, 15 years ago

trac has moved

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 8.2 KB
Line 
1// $Id$
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
5  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2008 Peter Johansson
7
8  This file is part of the yat library, http://dev.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "MatrixLookupWeighted.h"
27#include "MatrixLookup.h"
28#include "yat/utility/Matrix.h"
29
30#include <cassert>
31#include <fstream>
32#include <vector>
33
34namespace theplu {
35namespace yat {
36namespace classifier {
37
38  MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data, 
39                                             const utility::Matrix& weights,
40                                             const bool own)
41    : data_(MatrixP(&data, own)), weights_(MatrixP(&weights, own)) 
42  {
43    assert(data.rows()==weights.rows());
44    assert(data.columns()==weights.columns());
45    row_index_ = utility::Index(data.rows());
46    column_index_ = utility::Index(data.columns());
47    assert(validate());
48  }
49
50
51  MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data)
52    : data_(MatrixP(&data, false))
53  {
54    utility::Matrix weights;
55    utility::nan(*data_,weights);
56    weights_= MatrixP(new utility::Matrix(weights));
57    row_index_ = utility::Index(data.rows());
58    column_index_ = utility::Index(data.columns());
59    assert(validate());
60  }
61
62
63  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& ml)
64    : column_index_(ml.column_index_), data_(ml.data_),
65      row_index_(ml.row_index_), 
66      weights_(MatrixP(new utility::Matrix(data_->rows(),data_->columns(),1.0)))
67  { 
68    assert(validate());
69  }
70 
71
72  MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data, 
73                                             const utility::Matrix& weights, 
74                                             const utility::Index& row, 
75                                             const utility::Index& col)
76    : column_index_(col), data_(MatrixP(new utility::Matrix(data), false)), 
77      row_index_(row), weights_(MatrixP(new utility::Matrix(weights), false))
78  {
79    assert(validate());
80  }
81 
82
83
84  MatrixLookupWeighted::MatrixLookupWeighted(const utility::Matrix& data, 
85                                             const utility::Matrix& weights, 
86                                             const utility::Index& index, 
87                                             const bool row)
88    : data_(MatrixP(new utility::Matrix(data), false)), 
89      weights_(MatrixP(new utility::Matrix(weights), false))
90  {
91    assert(data.rows()==weights.rows());
92    assert(data.columns()==weights.columns());
93    if (row){
94      row_index_=index;
95      column_index_ = utility::Index(data.columns());
96    }
97    else{
98      column_index_=index;
99      row_index_ = utility::Index(data.rows());
100    }
101    assert(validate());
102  }
103 
104
105  /*
106  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& dv,
107                                             const MatrixLookup& wv)
108    : DataLookup2D(dv), data_(dv.data_), weights_(dv.data_)
109  {
110  }
111  */
112
113
114  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other)
115    : column_index_(other.column_index_), data_(other.data_), 
116      row_index_(other.row_index_), weights_(other.weights_)
117  {
118    assert(validate());
119  }
120
121
122
123  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other,
124                                             const utility::Index& row, 
125                                             const utility::Index& col)
126    : column_index_(utility::Index(other.column_index_, col)), 
127      data_(other.data_), row_index_(utility::Index(other.row_index_, row)), 
128      weights_(other.weights_)
129  {
130    assert(validate());
131  }
132 
133
134
135  MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other, 
136                                             const utility::Index& index, 
137                                             bool row)
138    : data_(other.data_), 
139      weights_(other.weights_)
140  {
141    if (row){
142      row_index_ = utility::Index(other.row_index_, index);
143      column_index_= other.column_index_;
144    }
145    else{
146      column_index_ = utility::Index(other.column_index_, index);
147      row_index_= other.row_index_;
148    }
149    assert(validate());
150  }
151 
152
153
154  MatrixLookupWeighted::MatrixLookupWeighted(const size_t rows, 
155                                             const size_t columns, 
156                                             const double value,
157                                             const double weight)
158    : column_index_(utility::Index(std::vector<size_t>(rows, 0))),
159      data_(MatrixP(new utility::Matrix(1,1,value))),
160      row_index_(utility::Index(std::vector<size_t>(columns, 0))), 
161      weights_(MatrixP(new utility::Matrix(1,1,weight)))
162  {
163    assert(validate());
164  }
165
166 
167  MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep)
168    : data_(MatrixP(new utility::Matrix(is,sep)))
169  {
170    column_index_ = utility::Index(data_->columns());
171    row_index_ = utility::Index(data_->rows());
172    utility::Matrix weights;
173    utility::nan(*data_,weights);
174    // Peter, should be possible to avoid this copying
175    weights_= MatrixP(new utility::Matrix(weights));
176    assert(validate());
177  }
178 
179
180  MatrixLookupWeighted::~MatrixLookupWeighted(void)
181  {
182  }
183
184
185
186  MatrixLookupWeighted::const_iterator MatrixLookupWeighted::begin(void) const
187  {
188    return const_iterator(const_iterator::iterator_type(*this, 0, 0), 1);
189  }
190
191
192  MatrixLookupWeighted::const_column_iterator
193  MatrixLookupWeighted::begin_column(size_t i) const
194  {
195    return const_column_iterator(const_column_iterator::iterator_type(*this,0, 
196                                                                      i),
197                                 columns());
198  }
199
200
201  MatrixLookupWeighted::const_iterator
202  MatrixLookupWeighted::begin_row(size_t i) const
203  {
204    return const_row_iterator(const_row_iterator::iterator_type(*this,i, 0), 1);
205  }
206
207
208  size_t MatrixLookupWeighted::columns(void) const
209  {
210    return column_index_.size();
211  }
212
213
214  double MatrixLookupWeighted::data(size_t row, size_t column) const
215  {
216    assert(row<rows());
217    assert(column<columns());
218    assert(row_index_[row]<data_->rows());
219    assert(column_index_[column]<data_->columns());
220    return (*data_)(row_index_[row], column_index_[column]);
221  }
222
223
224
225  MatrixLookupWeighted::const_iterator MatrixLookupWeighted::end(void) const
226  {
227    return const_iterator(const_iterator::iterator_type(*this, rows(), 0), 1);
228  }
229
230
231  MatrixLookupWeighted::const_iterator
232  MatrixLookupWeighted::end_column(size_t i) const
233  {
234    return const_column_iterator(const_column_iterator::iterator_type(*this, 
235                                                                      rows(),i),
236                                 columns());
237  }
238
239
240  MatrixLookupWeighted::const_row_iterator
241  MatrixLookupWeighted::end_row(size_t i) const
242  {
243    return const_row_iterator(const_row_iterator::iterator_type(*this,i+1,0),1);
244  }
245
246
247  size_t MatrixLookupWeighted::rows(void) const
248  {
249    return row_index_.size();
250  }
251
252
253  bool MatrixLookupWeighted::validate(void) const
254  {
255    for (size_t i=0; i<row_index_.size(); ++i)
256      if (row_index_[i]>=data_->rows())
257        return false;
258    for (size_t i=0; i<column_index_.size(); ++i)
259      if (column_index_[i]>=data_->columns())
260        return false;
261    for (size_t i=0; i<row_index_.size(); ++i)
262      if (row_index_[i]>=weights_->rows())
263        return false;
264    for (size_t i=0; i<column_index_.size(); ++i)
265      if (column_index_[i]>=weights_->columns())
266        return false;
267    return true;
268  }
269
270
271  double MatrixLookupWeighted::weight(size_t row, size_t column) const
272  {
273    assert(row<rows());
274    assert(column<columns());
275    assert(row_index_[row]<weights_->rows());
276    assert(column_index_[column]<weights_->columns());
277    return (*weights_)(row_index_[row], column_index_[column]);
278  }
279
280
281
282  bool MatrixLookupWeighted::weighted(void) const 
283  {
284    return true;
285  }
286
287
288
289  std::pair<double, double> 
290  MatrixLookupWeighted::operator()(const size_t row, const size_t column) const
291  { 
292    return std::make_pair(data(row, column), weight(row,column));
293  }
294
295
296
297  const MatrixLookupWeighted& MatrixLookupWeighted::operator=
298  (const MatrixLookupWeighted& other)
299  {
300    if (this!=&other){
301      column_index_=other.column_index_;
302      row_index_=other.row_index_;
303      data_ = other.data_;
304      weights_ = other.weights_;
305    }
306    assert(validate());
307    return *this;
308  }
309
310
311}}} // of namespace classifier, yat, and theplu
Note: See TracBrowser for help on using the repository browser.