1 | // $Id$ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Peter Johansson, Markus Ringnér |
---|
5 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
6 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
7 | Copyright (C) 2008 Peter Johansson |
---|
8 | |
---|
9 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
10 | |
---|
11 | The yat library is free software; you can redistribute it and/or |
---|
12 | modify it under the terms of the GNU General Public License as |
---|
13 | published by the Free Software Foundation; either version 3 of the |
---|
14 | License, or (at your option) any later version. |
---|
15 | |
---|
16 | The yat library is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
23 | */ |
---|
24 | |
---|
25 | #include "MatrixLookup.h" |
---|
26 | #include "yat/utility/Matrix.h" |
---|
27 | |
---|
28 | #include <algorithm> |
---|
29 | #include <cassert> |
---|
30 | #include <fstream> |
---|
31 | #include <vector> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace classifier { |
---|
36 | |
---|
37 | MatrixLookup::MatrixLookup(const utility::Matrix& data, const bool own) |
---|
38 | : data_(MatrixP(&data, own)) |
---|
39 | { |
---|
40 | column_index_ = utility::Index(data.columns()); |
---|
41 | row_index_ = utility::Index(data.rows()); |
---|
42 | assert(rows()==data.rows()); |
---|
43 | assert(columns()==data.columns()); |
---|
44 | assert(validate()); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | MatrixLookup::MatrixLookup(const utility::Matrix& data, |
---|
50 | const utility::Index& row, |
---|
51 | const utility::Index& col) |
---|
52 | : column_index_(col), |
---|
53 | data_(MatrixP(&data, false)), |
---|
54 | row_index_(row) |
---|
55 | |
---|
56 | { |
---|
57 | assert(rows()==row.size()); |
---|
58 | assert(columns()==col.size()); |
---|
59 | assert(validate()); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | MatrixLookup::MatrixLookup(const utility::Matrix& data, |
---|
65 | const utility::Index& index, |
---|
66 | const bool row) |
---|
67 | : data_(MatrixP(&data, false)) |
---|
68 | { |
---|
69 | if (row){ |
---|
70 | row_index_=index; |
---|
71 | column_index_ = utility::Index(data.columns()); |
---|
72 | } |
---|
73 | else{ |
---|
74 | column_index_=index; |
---|
75 | row_index_ = utility::Index(data.rows()); |
---|
76 | } |
---|
77 | assert(row || rows()==data.rows()); |
---|
78 | assert(row || columns()==index.size()); |
---|
79 | assert(!row || rows()==index.size()); |
---|
80 | assert(!row || columns()==data.columns()); |
---|
81 | assert(validate()); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | MatrixLookup::MatrixLookup(const MatrixLookup& other) |
---|
87 | : column_index_(other.column_index_), |
---|
88 | data_(other.data_), |
---|
89 | row_index_(other.row_index_) |
---|
90 | { |
---|
91 | assert(validate()); |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | MatrixLookup::MatrixLookup(const MatrixLookup& other, |
---|
97 | const utility::Index& row, |
---|
98 | const utility::Index& col) |
---|
99 | : column_index_(utility::Index(other.column_index_, col)), |
---|
100 | data_(other.data_), row_index_(utility::Index(other.row_index_, row)) |
---|
101 | { |
---|
102 | assert(rows()==row.size()); |
---|
103 | assert(columns()==col.size()); |
---|
104 | assert(validate()); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | MatrixLookup::MatrixLookup(const MatrixLookup& other, |
---|
110 | const utility::Index& index, bool row) |
---|
111 | : data_(other.data_) |
---|
112 | { |
---|
113 | assert(other.validate()); |
---|
114 | if (row){ |
---|
115 | row_index_ = utility::Index(other.row_index_, index); |
---|
116 | column_index_= other.column_index_; |
---|
117 | } |
---|
118 | else{ |
---|
119 | column_index_ = utility::Index(other.column_index_, index); |
---|
120 | row_index_= other.row_index_; |
---|
121 | } |
---|
122 | assert(validate()); |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | MatrixLookup::MatrixLookup(const size_t rows, const size_t columns, |
---|
127 | const double value) |
---|
128 | : data_(MatrixP(new utility::Matrix(1,1,value))) |
---|
129 | { |
---|
130 | column_index_ = utility::Index(std::vector<size_t>(columns, 0)); |
---|
131 | row_index_ = utility::Index(std::vector<size_t>(rows,0)); |
---|
132 | assert(validate()); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | MatrixLookup::MatrixLookup(std::istream& is, char sep) |
---|
137 | : data_(MatrixP(new utility::Matrix(is,sep))) |
---|
138 | { |
---|
139 | column_index_ = utility::Index(data_->columns()); |
---|
140 | row_index_ = utility::Index(data_->rows()); |
---|
141 | assert(validate()); |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | MatrixLookup::~MatrixLookup(void) |
---|
146 | { |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | MatrixLookup::const_iterator MatrixLookup::begin(void) const |
---|
151 | { |
---|
152 | return const_iterator(*this, 0, 0); |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | MatrixLookup::const_column_iterator MatrixLookup::begin_column(size_t i) const |
---|
157 | { |
---|
158 | return const_column_iterator(data_->begin_column(column_index_[i]), |
---|
159 | row_index_.begin()); |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | MatrixLookup::const_row_iterator MatrixLookup::begin_row(size_t i) const |
---|
164 | { |
---|
165 | return const_row_iterator(data_->begin_row(row_index_[i]), |
---|
166 | column_index_.begin()); |
---|
167 | } |
---|
168 | |
---|
169 | |
---|
170 | size_t MatrixLookup::columns(void) const |
---|
171 | { |
---|
172 | return column_index_.size(); |
---|
173 | } |
---|
174 | |
---|
175 | |
---|
176 | MatrixLookup::const_iterator MatrixLookup::end(void) const |
---|
177 | { |
---|
178 | return const_iterator(*this, rows(), 0); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | MatrixLookup::const_column_iterator MatrixLookup::end_column(size_t i) const |
---|
183 | { |
---|
184 | return const_column_iterator(data_->end_column(column_index_[i]), |
---|
185 | row_index_.end()); |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | MatrixLookup::const_row_iterator MatrixLookup::end_row(size_t i) const |
---|
190 | { |
---|
191 | return const_row_iterator(data_->end_row(row_index_[i]), |
---|
192 | column_index_.end()); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | size_t MatrixLookup::rows(void) const |
---|
197 | { |
---|
198 | return row_index_.size(); |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | bool MatrixLookup::validate(void) const |
---|
203 | { |
---|
204 | for (size_t i=0; i<row_index_.size(); ++i) |
---|
205 | if (row_index_[i]>=data_->rows()) |
---|
206 | return false; |
---|
207 | for (size_t i=0; i<column_index_.size(); ++i) |
---|
208 | if (column_index_[i]>=data_->columns()) |
---|
209 | return false; |
---|
210 | return true; |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | bool MatrixLookup::weighted(void) const |
---|
215 | { |
---|
216 | return false; |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | double MatrixLookup::operator()(const size_t row, const size_t column) const |
---|
222 | { |
---|
223 | assert(row<rows()); |
---|
224 | assert(column<columns()); |
---|
225 | assert(row_index_[row]<data_->rows()); |
---|
226 | assert(column_index_[column]<data_->columns()); |
---|
227 | return (*data_)(row_index_[row], column_index_[column]); |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | const MatrixLookup& MatrixLookup::operator=(const MatrixLookup& other) |
---|
233 | { |
---|
234 | if (this!=&other){ |
---|
235 | row_index_ = other.row_index_; |
---|
236 | column_index_ = other.column_index_; |
---|
237 | data_ = other.data_; |
---|
238 | } |
---|
239 | assert(validate()); |
---|
240 | return *this; |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | std::ostream& operator<<(std::ostream& s, const MatrixLookup& m) |
---|
245 | { |
---|
246 | s.setf(std::ios::dec); |
---|
247 | s.precision(12); |
---|
248 | for(size_t i=0, j=0; i<m.rows(); i++) |
---|
249 | for (j=0; j<m.columns(); j++) { |
---|
250 | s << m(i,j); |
---|
251 | if (j<m.columns()-1) |
---|
252 | s << s.fill(); |
---|
253 | else if (i<m.rows()-1) |
---|
254 | s << "\n"; |
---|
255 | } |
---|
256 | return s; |
---|
257 | } |
---|
258 | |
---|
259 | |
---|
260 | |
---|
261 | }}} // of namespace classifier, yat, and theplu |
---|