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