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 3 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 yat. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "MatrixLookupWeighted.h" |
---|
25 | #include "MatrixLookup.h" |
---|
26 | #include "yat/utility/DataIterator.h" |
---|
27 | #include "yat/utility/Matrix.h" |
---|
28 | #include "yat/utility/MatrixWeighted.h" |
---|
29 | #include "yat/utility/WeightIterator.h" |
---|
30 | |
---|
31 | #include <algorithm> |
---|
32 | #include <cassert> |
---|
33 | #include <fstream> |
---|
34 | #include <vector> |
---|
35 | |
---|
36 | namespace theplu { |
---|
37 | namespace yat { |
---|
38 | namespace classifier { |
---|
39 | |
---|
40 | MatrixLookupWeighted::MatrixLookupWeighted(const utility::MatrixWeighted& m, |
---|
41 | const utility::Index& rows, |
---|
42 | const utility::Index& columns) |
---|
43 | : column_index_(columns), data_(MatrixWP(&m,false)), row_index_(rows) |
---|
44 | { |
---|
45 | assert(validate()); |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | MatrixLookupWeighted::MatrixLookupWeighted(const utility::MatrixWeighted& m, |
---|
50 | bool owner) |
---|
51 | : column_index_(utility::Index(m.columns())), |
---|
52 | data_(MatrixWP(&m,owner)), |
---|
53 | row_index_(utility::Index(m.rows())) |
---|
54 | { |
---|
55 | assert(validate()); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& ml) |
---|
60 | : column_index_(ml.column_index_), |
---|
61 | row_index_(ml.row_index_) |
---|
62 | { |
---|
63 | utility::MatrixWeighted* mw = new utility::MatrixWeighted(*ml.data_); |
---|
64 | data_ = MatrixWP(mw); |
---|
65 | assert(validate()); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other) |
---|
70 | : column_index_(other.column_index_), data_(other.data_), |
---|
71 | row_index_(other.row_index_) |
---|
72 | { |
---|
73 | assert(validate()); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other, |
---|
79 | const utility::Index& row, |
---|
80 | const utility::Index& col) |
---|
81 | : column_index_(utility::Index(other.column_index_, col)), |
---|
82 | data_(other.data_), row_index_(utility::Index(other.row_index_, row)) |
---|
83 | { |
---|
84 | assert(validate()); |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other, |
---|
90 | const utility::Index& index, |
---|
91 | bool row) |
---|
92 | : data_(other.data_) |
---|
93 | { |
---|
94 | if (row){ |
---|
95 | row_index_ = utility::Index(other.row_index_, index); |
---|
96 | column_index_= other.column_index_; |
---|
97 | } |
---|
98 | else{ |
---|
99 | column_index_ = utility::Index(other.column_index_, index); |
---|
100 | row_index_= other.row_index_; |
---|
101 | } |
---|
102 | assert(validate()); |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | MatrixLookupWeighted::MatrixLookupWeighted(const size_t rows, |
---|
108 | const size_t columns, |
---|
109 | const double value, |
---|
110 | const double weight) |
---|
111 | : column_index_(utility::Index(std::vector<size_t>(rows, 0))), |
---|
112 | data_(MatrixWP(new utility::MatrixWeighted(1,1,value, weight))), |
---|
113 | row_index_(utility::Index(std::vector<size_t>(columns, 0))) |
---|
114 | { |
---|
115 | assert(validate()); |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep) |
---|
120 | : data_(MatrixWP(new utility::MatrixWeighted(is,sep))) |
---|
121 | { |
---|
122 | column_index_ = utility::Index(data_->columns()); |
---|
123 | row_index_ = utility::Index(data_->rows()); |
---|
124 | assert(validate()); |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | MatrixLookupWeighted::~MatrixLookupWeighted(void) |
---|
129 | { |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | MatrixLookupWeighted::const_iterator MatrixLookupWeighted::begin(void) const |
---|
135 | { |
---|
136 | return const_iterator(const_iterator::iterator_type(*this, 0, 0), 1); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | MatrixLookupWeighted::const_column_iterator |
---|
141 | MatrixLookupWeighted::begin_column(size_t i) const |
---|
142 | { |
---|
143 | return const_column_iterator(const_column_iterator::iterator_type(*this,0, |
---|
144 | i), |
---|
145 | columns()); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | MatrixLookupWeighted::const_iterator |
---|
150 | MatrixLookupWeighted::begin_row(size_t i) const |
---|
151 | { |
---|
152 | return const_row_iterator(const_row_iterator::iterator_type(*this,i, 0), 1); |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | size_t MatrixLookupWeighted::columns(void) const |
---|
157 | { |
---|
158 | return column_index_.size(); |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | double MatrixLookupWeighted::data(size_t row, size_t column) const |
---|
163 | { |
---|
164 | assert(row<rows()); |
---|
165 | assert(column<columns()); |
---|
166 | assert(row_index_[row]<data_->rows()); |
---|
167 | assert(column_index_[column]<data_->columns()); |
---|
168 | return (*this)(row, column).data(); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | |
---|
173 | MatrixLookupWeighted::const_iterator MatrixLookupWeighted::end(void) const |
---|
174 | { |
---|
175 | return const_iterator(const_iterator::iterator_type(*this, rows(), 0), 1); |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | MatrixLookupWeighted::const_iterator |
---|
180 | MatrixLookupWeighted::end_column(size_t i) const |
---|
181 | { |
---|
182 | return const_column_iterator(const_column_iterator::iterator_type(*this, |
---|
183 | rows(),i), |
---|
184 | columns()); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | MatrixLookupWeighted::const_row_iterator |
---|
189 | MatrixLookupWeighted::end_row(size_t i) const |
---|
190 | { |
---|
191 | return const_row_iterator(const_row_iterator::iterator_type(*this,i+1,0),1); |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | size_t MatrixLookupWeighted::rows(void) const |
---|
196 | { |
---|
197 | return row_index_.size(); |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | bool MatrixLookupWeighted::validate(void) const |
---|
202 | { |
---|
203 | for (size_t i=0; i<row_index_.size(); ++i) |
---|
204 | if (row_index_[i]>=data_->rows()) { |
---|
205 | std::cerr << i << " " << row_index_[i] << std::endl; |
---|
206 | return false; |
---|
207 | } |
---|
208 | for (size_t i=0; i<column_index_.size(); ++i) |
---|
209 | if (column_index_[i]>=data_->columns()) |
---|
210 | return false; |
---|
211 | return true; |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | double MatrixLookupWeighted::weight(size_t row, size_t column) const |
---|
216 | { |
---|
217 | assert(row<rows()); |
---|
218 | assert(column<columns()); |
---|
219 | return (*this)(row, column).weight(); |
---|
220 | } |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | bool MatrixLookupWeighted::weighted(void) const |
---|
225 | { |
---|
226 | return true; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | |
---|
231 | MatrixLookupWeighted::const_reference |
---|
232 | MatrixLookupWeighted::operator()(const size_t row, const size_t column) const |
---|
233 | { |
---|
234 | return (*data_)(row_index_[row], column_index_[column]); |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | const MatrixLookupWeighted& MatrixLookupWeighted::operator= |
---|
240 | (const MatrixLookupWeighted& other) |
---|
241 | { |
---|
242 | if (this!=&other){ |
---|
243 | column_index_=other.column_index_; |
---|
244 | row_index_=other.row_index_; |
---|
245 | data_ = other.data_; |
---|
246 | } |
---|
247 | assert(validate()); |
---|
248 | return *this; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | }}} // of namespace classifier, yat, and theplu |
---|