595 |
28 Aug 06 |
peter |
// $Id$ |
595 |
28 Aug 06 |
peter |
2 |
|
675 |
10 Oct 06 |
jari |
3 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson |
2862 |
03 Oct 12 |
peter |
Copyright (C) 2009, 2012 Peter Johansson |
595 |
28 Aug 06 |
peter |
7 |
|
1437 |
25 Aug 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
616 |
31 Aug 06 |
jari |
9 |
|
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 |
14 |
|
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 |
19 |
|
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 |
22 |
*/ |
675 |
10 Oct 06 |
jari |
23 |
|
2881 |
18 Nov 12 |
peter |
24 |
#include <config.h> |
2881 |
18 Nov 12 |
peter |
25 |
|
680 |
11 Oct 06 |
jari |
26 |
#include "MatrixLookupWeighted.h" |
1035 |
05 Feb 08 |
peter |
27 |
#include "MatrixLookup.h" |
1482 |
09 Sep 08 |
peter |
28 |
#include "yat/utility/DataIterator.h" |
2862 |
03 Oct 12 |
peter |
29 |
#include "yat/utility/Deleter.h" |
1121 |
22 Feb 08 |
peter |
30 |
#include "yat/utility/Matrix.h" |
1482 |
09 Sep 08 |
peter |
31 |
#include "yat/utility/MatrixWeighted.h" |
1482 |
09 Sep 08 |
peter |
32 |
#include "yat/utility/WeightIterator.h" |
675 |
10 Oct 06 |
jari |
33 |
|
1482 |
09 Sep 08 |
peter |
34 |
#include <algorithm> |
781 |
05 Mar 07 |
peter |
35 |
#include <cassert> |
595 |
28 Aug 06 |
peter |
36 |
#include <fstream> |
2076 |
06 Oct 09 |
peter |
37 |
#include <iostream> |
1170 |
27 Feb 08 |
peter |
38 |
#include <vector> |
595 |
28 Aug 06 |
peter |
39 |
|
595 |
28 Aug 06 |
peter |
40 |
namespace theplu { |
680 |
11 Oct 06 |
jari |
41 |
namespace yat { |
595 |
28 Aug 06 |
peter |
42 |
namespace classifier { |
595 |
28 Aug 06 |
peter |
43 |
|
1482 |
09 Sep 08 |
peter |
44 |
MatrixLookupWeighted::MatrixLookupWeighted(const utility::MatrixWeighted& m, |
1482 |
09 Sep 08 |
peter |
45 |
const utility::Index& rows, |
1482 |
09 Sep 08 |
peter |
46 |
const utility::Index& columns) |
2862 |
03 Oct 12 |
peter |
47 |
: column_index_(columns), data_(&m, utility::Deleter(false)), |
2862 |
03 Oct 12 |
peter |
48 |
row_index_(rows) |
1482 |
09 Sep 08 |
peter |
49 |
{ |
1587 |
17 Oct 08 |
peter |
50 |
assert(validate()); |
1482 |
09 Sep 08 |
peter |
51 |
} |
1482 |
09 Sep 08 |
peter |
52 |
|
1482 |
09 Sep 08 |
peter |
53 |
|
1581 |
15 Oct 08 |
peter |
54 |
MatrixLookupWeighted::MatrixLookupWeighted(const utility::MatrixWeighted& m, |
1581 |
15 Oct 08 |
peter |
55 |
bool owner) |
2862 |
03 Oct 12 |
peter |
56 |
: column_index_(utility::Index(m.columns())), |
2862 |
03 Oct 12 |
peter |
57 |
data_(&m,utility::Deleter(owner)), |
1482 |
09 Sep 08 |
peter |
58 |
row_index_(utility::Index(m.rows())) |
1482 |
09 Sep 08 |
peter |
59 |
{ |
1170 |
27 Feb 08 |
peter |
60 |
assert(validate()); |
595 |
28 Aug 06 |
peter |
61 |
} |
638 |
06 Sep 06 |
markus |
62 |
|
1035 |
05 Feb 08 |
peter |
63 |
|
1035 |
05 Feb 08 |
peter |
64 |
MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookup& ml) |
2862 |
03 Oct 12 |
peter |
65 |
: column_index_(ml.column_index_), |
1587 |
17 Oct 08 |
peter |
66 |
row_index_(ml.row_index_) |
2862 |
03 Oct 12 |
peter |
67 |
{ |
1587 |
17 Oct 08 |
peter |
68 |
utility::MatrixWeighted* mw = new utility::MatrixWeighted(*ml.data_); |
1587 |
17 Oct 08 |
peter |
69 |
data_ = MatrixWP(mw); |
1170 |
27 Feb 08 |
peter |
70 |
assert(validate()); |
1035 |
05 Feb 08 |
peter |
71 |
} |
595 |
28 Aug 06 |
peter |
72 |
|
2862 |
03 Oct 12 |
peter |
73 |
|
640 |
07 Sep 06 |
peter |
74 |
MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other) |
2862 |
03 Oct 12 |
peter |
75 |
: column_index_(other.column_index_), data_(other.data_), |
1587 |
17 Oct 08 |
peter |
76 |
row_index_(other.row_index_) |
595 |
28 Aug 06 |
peter |
77 |
{ |
1170 |
27 Feb 08 |
peter |
78 |
assert(validate()); |
595 |
28 Aug 06 |
peter |
79 |
} |
595 |
28 Aug 06 |
peter |
80 |
|
595 |
28 Aug 06 |
peter |
81 |
|
595 |
28 Aug 06 |
peter |
82 |
|
640 |
07 Sep 06 |
peter |
83 |
MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other, |
2862 |
03 Oct 12 |
peter |
84 |
const utility::Index& row, |
1134 |
23 Feb 08 |
peter |
85 |
const utility::Index& col) |
2862 |
03 Oct 12 |
peter |
86 |
: column_index_(utility::Index(other.column_index_, col)), |
1587 |
17 Oct 08 |
peter |
87 |
data_(other.data_), row_index_(utility::Index(other.row_index_, row)) |
595 |
28 Aug 06 |
peter |
88 |
{ |
1170 |
27 Feb 08 |
peter |
89 |
assert(validate()); |
595 |
28 Aug 06 |
peter |
90 |
} |
595 |
28 Aug 06 |
peter |
91 |
|
595 |
28 Aug 06 |
peter |
92 |
|
2862 |
03 Oct 12 |
peter |
93 |
|
2862 |
03 Oct 12 |
peter |
94 |
MatrixLookupWeighted::MatrixLookupWeighted(const MatrixLookupWeighted& other, |
2862 |
03 Oct 12 |
peter |
95 |
const utility::Index& index, |
595 |
28 Aug 06 |
peter |
96 |
bool row) |
1587 |
17 Oct 08 |
peter |
97 |
: data_(other.data_) |
595 |
28 Aug 06 |
peter |
98 |
{ |
1170 |
27 Feb 08 |
peter |
99 |
if (row){ |
1170 |
27 Feb 08 |
peter |
100 |
row_index_ = utility::Index(other.row_index_, index); |
1170 |
27 Feb 08 |
peter |
101 |
column_index_= other.column_index_; |
1170 |
27 Feb 08 |
peter |
102 |
} |
1170 |
27 Feb 08 |
peter |
103 |
else{ |
1170 |
27 Feb 08 |
peter |
104 |
column_index_ = utility::Index(other.column_index_, index); |
1170 |
27 Feb 08 |
peter |
105 |
row_index_= other.row_index_; |
1170 |
27 Feb 08 |
peter |
106 |
} |
1170 |
27 Feb 08 |
peter |
107 |
assert(validate()); |
595 |
28 Aug 06 |
peter |
108 |
} |
595 |
28 Aug 06 |
peter |
109 |
|
595 |
28 Aug 06 |
peter |
110 |
|
2862 |
03 Oct 12 |
peter |
111 |
|
2862 |
03 Oct 12 |
peter |
112 |
MatrixLookupWeighted::MatrixLookupWeighted(const size_t rows, |
2862 |
03 Oct 12 |
peter |
113 |
const size_t columns, |
624 |
05 Sep 06 |
peter |
114 |
const double value, |
624 |
05 Sep 06 |
peter |
115 |
const double weight) |
1170 |
27 Feb 08 |
peter |
116 |
: column_index_(utility::Index(std::vector<size_t>(rows, 0))), |
1587 |
17 Oct 08 |
peter |
117 |
data_(MatrixWP(new utility::MatrixWeighted(1,1,value, weight))), |
1587 |
17 Oct 08 |
peter |
118 |
row_index_(utility::Index(std::vector<size_t>(columns, 0))) |
595 |
28 Aug 06 |
peter |
119 |
{ |
1170 |
27 Feb 08 |
peter |
120 |
assert(validate()); |
595 |
28 Aug 06 |
peter |
121 |
} |
595 |
28 Aug 06 |
peter |
122 |
|
2862 |
03 Oct 12 |
peter |
123 |
|
638 |
06 Sep 06 |
markus |
124 |
MatrixLookupWeighted::MatrixLookupWeighted(std::istream& is, char sep) |
1587 |
17 Oct 08 |
peter |
125 |
: data_(MatrixWP(new utility::MatrixWeighted(is,sep))) |
595 |
28 Aug 06 |
peter |
126 |
{ |
1170 |
27 Feb 08 |
peter |
127 |
column_index_ = utility::Index(data_->columns()); |
1134 |
23 Feb 08 |
peter |
128 |
row_index_ = utility::Index(data_->rows()); |
1170 |
27 Feb 08 |
peter |
129 |
assert(validate()); |
595 |
28 Aug 06 |
peter |
130 |
} |
595 |
28 Aug 06 |
peter |
131 |
|
2862 |
03 Oct 12 |
peter |
132 |
|
595 |
28 Aug 06 |
peter |
133 |
MatrixLookupWeighted::~MatrixLookupWeighted(void) |
595 |
28 Aug 06 |
peter |
134 |
{ |
595 |
28 Aug 06 |
peter |
135 |
} |
595 |
28 Aug 06 |
peter |
136 |
|
595 |
28 Aug 06 |
peter |
137 |
|
720 |
26 Dec 06 |
jari |
138 |
|
1091 |
14 Feb 08 |
peter |
139 |
MatrixLookupWeighted::const_iterator MatrixLookupWeighted::begin(void) const |
1091 |
14 Feb 08 |
peter |
140 |
{ |
1091 |
14 Feb 08 |
peter |
141 |
return const_iterator(const_iterator::iterator_type(*this, 0, 0), 1); |
1091 |
14 Feb 08 |
peter |
142 |
} |
1091 |
14 Feb 08 |
peter |
143 |
|
1091 |
14 Feb 08 |
peter |
144 |
|
2862 |
03 Oct 12 |
peter |
145 |
MatrixLookupWeighted::const_column_iterator |
1091 |
14 Feb 08 |
peter |
146 |
MatrixLookupWeighted::begin_column(size_t i) const |
1091 |
14 Feb 08 |
peter |
147 |
{ |
1589 |
17 Oct 08 |
peter |
148 |
return const_column_iterator(data_->begin_column(column_index_[i]), |
1589 |
17 Oct 08 |
peter |
149 |
row_index_.begin()); |
1091 |
14 Feb 08 |
peter |
150 |
} |
1091 |
14 Feb 08 |
peter |
151 |
|
1091 |
14 Feb 08 |
peter |
152 |
|
2862 |
03 Oct 12 |
peter |
153 |
MatrixLookupWeighted::const_row_iterator |
1091 |
14 Feb 08 |
peter |
154 |
MatrixLookupWeighted::begin_row(size_t i) const |
1091 |
14 Feb 08 |
peter |
155 |
{ |
1589 |
17 Oct 08 |
peter |
156 |
return const_row_iterator(data_->begin_row(row_index_[i]), |
1589 |
17 Oct 08 |
peter |
157 |
column_index_.begin()); |
1091 |
14 Feb 08 |
peter |
158 |
} |
1091 |
14 Feb 08 |
peter |
159 |
|
1091 |
14 Feb 08 |
peter |
160 |
|
1170 |
27 Feb 08 |
peter |
161 |
size_t MatrixLookupWeighted::columns(void) const |
1170 |
27 Feb 08 |
peter |
162 |
{ |
1170 |
27 Feb 08 |
peter |
163 |
return column_index_.size(); |
1170 |
27 Feb 08 |
peter |
164 |
} |
1170 |
27 Feb 08 |
peter |
165 |
|
1170 |
27 Feb 08 |
peter |
166 |
|
720 |
26 Dec 06 |
jari |
167 |
double MatrixLookupWeighted::data(size_t row, size_t column) const |
720 |
26 Dec 06 |
jari |
168 |
{ |
1170 |
27 Feb 08 |
peter |
169 |
assert(row<rows()); |
1170 |
27 Feb 08 |
peter |
170 |
assert(column<columns()); |
1170 |
27 Feb 08 |
peter |
171 |
assert(row_index_[row]<data_->rows()); |
1170 |
27 Feb 08 |
peter |
172 |
assert(column_index_[column]<data_->columns()); |
1587 |
17 Oct 08 |
peter |
173 |
return (*this)(row, column).data(); |
720 |
26 Dec 06 |
jari |
174 |
} |
720 |
26 Dec 06 |
jari |
175 |
|
720 |
26 Dec 06 |
jari |
176 |
|
720 |
26 Dec 06 |
jari |
177 |
|
1091 |
14 Feb 08 |
peter |
178 |
MatrixLookupWeighted::const_iterator MatrixLookupWeighted::end(void) const |
1091 |
14 Feb 08 |
peter |
179 |
{ |
1091 |
14 Feb 08 |
peter |
180 |
return const_iterator(const_iterator::iterator_type(*this, rows(), 0), 1); |
1091 |
14 Feb 08 |
peter |
181 |
} |
1091 |
14 Feb 08 |
peter |
182 |
|
1091 |
14 Feb 08 |
peter |
183 |
|
2862 |
03 Oct 12 |
peter |
184 |
MatrixLookupWeighted::const_column_iterator |
1091 |
14 Feb 08 |
peter |
185 |
MatrixLookupWeighted::end_column(size_t i) const |
1091 |
14 Feb 08 |
peter |
186 |
{ |
2862 |
03 Oct 12 |
peter |
187 |
return const_column_iterator(data_->end_column(column_index_[i]), |
1589 |
17 Oct 08 |
peter |
188 |
row_index_.end()); |
1091 |
14 Feb 08 |
peter |
189 |
} |
1091 |
14 Feb 08 |
peter |
190 |
|
1091 |
14 Feb 08 |
peter |
191 |
|
2862 |
03 Oct 12 |
peter |
192 |
MatrixLookupWeighted::const_row_iterator |
1091 |
14 Feb 08 |
peter |
193 |
MatrixLookupWeighted::end_row(size_t i) const |
1091 |
14 Feb 08 |
peter |
194 |
{ |
2862 |
03 Oct 12 |
peter |
195 |
return const_row_iterator(data_->end_row(row_index_[i]), |
1589 |
17 Oct 08 |
peter |
196 |
column_index_.end()); |
1091 |
14 Feb 08 |
peter |
197 |
} |
1091 |
14 Feb 08 |
peter |
198 |
|
1091 |
14 Feb 08 |
peter |
199 |
|
1170 |
27 Feb 08 |
peter |
200 |
size_t MatrixLookupWeighted::rows(void) const |
1170 |
27 Feb 08 |
peter |
201 |
{ |
1170 |
27 Feb 08 |
peter |
202 |
return row_index_.size(); |
1170 |
27 Feb 08 |
peter |
203 |
} |
1170 |
27 Feb 08 |
peter |
204 |
|
1170 |
27 Feb 08 |
peter |
205 |
|
1170 |
27 Feb 08 |
peter |
206 |
bool MatrixLookupWeighted::validate(void) const |
1170 |
27 Feb 08 |
peter |
207 |
{ |
1170 |
27 Feb 08 |
peter |
208 |
for (size_t i=0; i<row_index_.size(); ++i) |
1484 |
09 Sep 08 |
peter |
209 |
if (row_index_[i]>=data_->rows()) { |
1484 |
09 Sep 08 |
peter |
210 |
std::cerr << i << " " << row_index_[i] << std::endl; |
1170 |
27 Feb 08 |
peter |
211 |
return false; |
1484 |
09 Sep 08 |
peter |
212 |
} |
1170 |
27 Feb 08 |
peter |
213 |
for (size_t i=0; i<column_index_.size(); ++i) |
1170 |
27 Feb 08 |
peter |
214 |
if (column_index_[i]>=data_->columns()) |
1170 |
27 Feb 08 |
peter |
215 |
return false; |
1170 |
27 Feb 08 |
peter |
216 |
return true; |
1170 |
27 Feb 08 |
peter |
217 |
} |
1170 |
27 Feb 08 |
peter |
218 |
|
1170 |
27 Feb 08 |
peter |
219 |
|
720 |
26 Dec 06 |
jari |
220 |
double MatrixLookupWeighted::weight(size_t row, size_t column) const |
720 |
26 Dec 06 |
jari |
221 |
{ |
1170 |
27 Feb 08 |
peter |
222 |
assert(row<rows()); |
1170 |
27 Feb 08 |
peter |
223 |
assert(column<columns()); |
1587 |
17 Oct 08 |
peter |
224 |
return (*this)(row, column).weight(); |
720 |
26 Dec 06 |
jari |
225 |
} |
720 |
26 Dec 06 |
jari |
226 |
|
720 |
26 Dec 06 |
jari |
227 |
|
720 |
26 Dec 06 |
jari |
228 |
|
2862 |
03 Oct 12 |
peter |
229 |
bool MatrixLookupWeighted::weighted(void) const |
631 |
05 Sep 06 |
peter |
230 |
{ |
631 |
05 Sep 06 |
peter |
231 |
return true; |
631 |
05 Sep 06 |
peter |
232 |
} |
631 |
05 Sep 06 |
peter |
233 |
|
631 |
05 Sep 06 |
peter |
234 |
|
631 |
05 Sep 06 |
peter |
235 |
|
1552 |
06 Oct 08 |
peter |
236 |
MatrixLookupWeighted::const_reference |
1193 |
29 Feb 08 |
peter |
237 |
MatrixLookupWeighted::operator()(const size_t row, const size_t column) const |
2862 |
03 Oct 12 |
peter |
238 |
{ |
1587 |
17 Oct 08 |
peter |
239 |
return (*data_)(row_index_[row], column_index_[column]); |
720 |
26 Dec 06 |
jari |
240 |
} |
720 |
26 Dec 06 |
jari |
241 |
|
720 |
26 Dec 06 |
jari |
242 |
|
720 |
26 Dec 06 |
jari |
243 |
|
595 |
28 Aug 06 |
peter |
244 |
const MatrixLookupWeighted& MatrixLookupWeighted::operator= |
595 |
28 Aug 06 |
peter |
245 |
(const MatrixLookupWeighted& other) |
595 |
28 Aug 06 |
peter |
246 |
{ |
595 |
28 Aug 06 |
peter |
247 |
if (this!=&other){ |
1170 |
27 Feb 08 |
peter |
248 |
column_index_=other.column_index_; |
1170 |
27 Feb 08 |
peter |
249 |
row_index_=other.row_index_; |
595 |
28 Aug 06 |
peter |
250 |
data_ = other.data_; |
595 |
28 Aug 06 |
peter |
251 |
} |
1170 |
27 Feb 08 |
peter |
252 |
assert(validate()); |
595 |
28 Aug 06 |
peter |
253 |
return *this; |
595 |
28 Aug 06 |
peter |
254 |
} |
595 |
28 Aug 06 |
peter |
255 |
|
595 |
28 Aug 06 |
peter |
256 |
|
680 |
11 Oct 06 |
jari |
257 |
}}} // of namespace classifier, yat, and theplu |