1 | #ifndef _theplu_yat_classifier_matrix_lookup_ |
---|
2 | #define _theplu_yat_classifier_matrix_lookup_ |
---|
3 | |
---|
4 | // $Id$ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2005 Peter Johansson, Markus Ringnér |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
9 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
10 | Copyright (C) 2008 Peter Johansson |
---|
11 | |
---|
12 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
13 | |
---|
14 | The yat library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU General Public License as |
---|
16 | published by the Free Software Foundation; either version 3 of the |
---|
17 | License, or (at your option) any later version. |
---|
18 | |
---|
19 | The yat library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU General Public License |
---|
25 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
26 | */ |
---|
27 | |
---|
28 | #include "yat/utility/Container2DIterator.h" |
---|
29 | #include "yat/utility/Index.h" |
---|
30 | #include "yat/utility/iterator_traits.h" |
---|
31 | #include "yat/utility/Matrix.h" |
---|
32 | #include "yat/utility/SmartPtr.h" |
---|
33 | |
---|
34 | #include <boost/iterator/permutation_iterator.hpp> |
---|
35 | |
---|
36 | #include <iostream> |
---|
37 | #include <vector> |
---|
38 | |
---|
39 | namespace theplu { |
---|
40 | namespace yat { |
---|
41 | namespace classifier { |
---|
42 | |
---|
43 | /// |
---|
44 | /// @brief General view into utility::Matrix |
---|
45 | /// |
---|
46 | /// MatrixLookups can be used to create lookups/views into matrices |
---|
47 | /// in a more general way than the views supported in the matrix |
---|
48 | /// class. The object does not contain any data, but only a pointer |
---|
49 | /// to a utility::Matrix and row and columns incices defining which |
---|
50 | /// sub-matrix to look into. Each row and each column corresponds |
---|
51 | /// to a row and a column in the utility::Matrix, respectively. This design |
---|
52 | /// allow for fast creation of sub-matrices, which is a common |
---|
53 | /// operation in most traning/validation procedures. The views are |
---|
54 | /// const views in the sense that they cannot modify underlying |
---|
55 | /// utility::Matrix. |
---|
56 | /// |
---|
57 | /// A MatrixLookup can be created directly from a utility::Matrix or from |
---|
58 | /// another MatrixLookup. In the latter case, the resulting |
---|
59 | /// MatrixLookup is looking directly into the underlying utility::Matrix to |
---|
60 | /// avoid multiple lookups. |
---|
61 | /// |
---|
62 | /// There is a possibility to set the MatrixLookup as owner of the |
---|
63 | /// underlying utility::Matrix. This implies that underlying data is deleted |
---|
64 | /// in destructor of MatrixLookup, but only if there is no other |
---|
65 | /// owner of the underlying data. A reference counter is used to |
---|
66 | /// keep track of number of owners. Ownership is copied in copy |
---|
67 | /// constructors and assignments. |
---|
68 | /// |
---|
69 | /// \see MatrixLookupWeighted |
---|
70 | /// |
---|
71 | class MatrixLookup |
---|
72 | { |
---|
73 | public: |
---|
74 | /// 'Read Only' iterator |
---|
75 | typedef utility::Container2DIterator<const MatrixLookup, const double, |
---|
76 | void, const double> |
---|
77 | const_iterator; |
---|
78 | |
---|
79 | /** |
---|
80 | 'Read only' iterator used to iterate over a column |
---|
81 | */ |
---|
82 | typedef boost::permutation_iterator<utility::Matrix::const_column_iterator, |
---|
83 | utility::Index::const_iterator> |
---|
84 | const_column_iterator; |
---|
85 | |
---|
86 | /** |
---|
87 | 'Read only' iterator used to iterate over a row |
---|
88 | */ |
---|
89 | typedef const_column_iterator const_row_iterator; |
---|
90 | |
---|
91 | /// |
---|
92 | /// Constructor creating a lookup into the entire @a matrix. |
---|
93 | /// \param matrix underlying matrix |
---|
94 | /// @param own if true MatrixLookup owns its underlying @a matrix |
---|
95 | /// |
---|
96 | /// @note If \a own is true and \a matrix is already owned by some |
---|
97 | /// other object, this will lead to \a matrix having multiple |
---|
98 | /// owners without the owners being aware of each |
---|
99 | /// other. Consequently multiple deletion will occur. |
---|
100 | /// |
---|
101 | /// @note If @a matrix goes out of scope or is deleted, the |
---|
102 | /// MatrixLookup becomes invalid and the result of further use is |
---|
103 | /// undefined. |
---|
104 | /// |
---|
105 | MatrixLookup(const utility::Matrix& matrix, const bool own=false); |
---|
106 | |
---|
107 | /// |
---|
108 | /// Constructor creating a lookup into a sub-matrix of @a matrix. |
---|
109 | /// The @a row and @a column define what sub-matrix to look into, |
---|
110 | /// in other words, the created MatrixLookup will fullfill the |
---|
111 | /// following: \f$ MatrixLookup(i,j)=matrix(row[i],column[j]) |
---|
112 | /// \f$. This also means that number of rows in created |
---|
113 | /// MatrixLookup is equal to size of vector @a row, and number of |
---|
114 | /// columns is equal to size of vector @a column. |
---|
115 | /// |
---|
116 | /// @note If @a matrix goes out of scope or is deleted, the |
---|
117 | /// MatrixLookup becomes invalid and the result of further use is |
---|
118 | /// undefined. |
---|
119 | /// |
---|
120 | MatrixLookup(const utility::Matrix& matrix, const utility::Index& row, |
---|
121 | const utility::Index& column); |
---|
122 | |
---|
123 | /// |
---|
124 | /// Constructor creating a lookup into a sub-matrix of @a matrix. |
---|
125 | /// |
---|
126 | /// If @a row_vectors is true the new MatrixLookup will consist |
---|
127 | /// of the row vectors defined by @a index. This means that the |
---|
128 | /// created MatrixLookup will fullfill: |
---|
129 | /// \f$ MatrixLookup(i,j)=matrix(i,index[j]) \f$ |
---|
130 | /// |
---|
131 | /// If @a row_vectors is false the new MatrixLookup will be consist |
---|
132 | /// of the rolumn vectors defined by @a index. This means that the |
---|
133 | /// created MatrixLookup will fullfill: |
---|
134 | /// \f$ MatrixLookup(i,j)=matrix(index[i],j) \f$ |
---|
135 | /// |
---|
136 | /// @note If @a matrix goes out of scope or is deleted, the |
---|
137 | /// MatrixLookup becomes invalid and the result of further use is |
---|
138 | /// undefined. |
---|
139 | /// |
---|
140 | MatrixLookup(const utility::Matrix& matrix, |
---|
141 | const utility::Index& index, |
---|
142 | const bool row_vectors); |
---|
143 | |
---|
144 | /// |
---|
145 | /// @brief Copy constructor. |
---|
146 | /// |
---|
147 | /// If \a other is owner of underlying data, constructed |
---|
148 | /// MatrixLookup will also be set as owner of underlying data. |
---|
149 | /// |
---|
150 | /// @note If underlying matrix goes out of scope or is deleted, the |
---|
151 | /// MatrixLookup becomes invalid and the result of further use is |
---|
152 | /// undefined. |
---|
153 | /// |
---|
154 | MatrixLookup(const MatrixLookup& other); |
---|
155 | |
---|
156 | /// |
---|
157 | /// @brief Create a sub-MatrixLookup. |
---|
158 | /// |
---|
159 | /// The Lookup is independent of |
---|
160 | /// MatrixLookup @a ml. The MatrixLookup is created to look |
---|
161 | /// directly into the underlying matrix to avoid multiple lookups. |
---|
162 | /// |
---|
163 | /// If \a ml is owner of underlying data, constructed |
---|
164 | /// MatrixLookup will also be set as owner of underlying data. |
---|
165 | /// |
---|
166 | /// The @a row and @a column define what sub-matrix to look into, |
---|
167 | /// in other words, the created MatrixLookup will fullfill the |
---|
168 | /// following: MatrixLookup(i,j)=ml(row[i],column[j]). This |
---|
169 | /// also means that number of rows in created MatrixLookup is |
---|
170 | /// equal to size of vector @a row, and number of columns is equal |
---|
171 | /// to size of vector @a column. |
---|
172 | /// |
---|
173 | /// If \a ml is owner of underlying data, constructed |
---|
174 | /// MatrixLookup will also be set as owner of underlying data. |
---|
175 | /// |
---|
176 | /// @note If underlying matrix goes out of scope or is deleted, the |
---|
177 | /// MatrixLookup becomes invalid and the result of further use is |
---|
178 | /// undefined. |
---|
179 | /// |
---|
180 | MatrixLookup(const MatrixLookup& ml, const utility::Index& row, |
---|
181 | const utility::Index& column); |
---|
182 | |
---|
183 | /// |
---|
184 | /// Constructor creating a lookup into a sub-matrix of |
---|
185 | /// @a ml. The MatrixLookup is created to look directly into the |
---|
186 | /// underlying matrix to avoid multiple lookups. |
---|
187 | /// |
---|
188 | /// If @a row_vectors is true the new MatrixLookup will consist |
---|
189 | /// of the row vectors defined by @a index. This means that the |
---|
190 | /// created MatrixLookup will fullfill: |
---|
191 | /// MatrixLookup(i,j)=ml(i,index[j]) |
---|
192 | /// |
---|
193 | /// If @a row_vectors is false the new MatrixLookup will consist |
---|
194 | /// of the rolumn vectors defined by @a index. This means that the |
---|
195 | /// created MatrixLookup will fullfill: |
---|
196 | /// \f$ MatrixLookup(i,j) = ml(index[i],j) \f$ |
---|
197 | /// |
---|
198 | /// If \a ml is owner of underlying data, constructed |
---|
199 | /// MatrixLookup will also be set as owner of underlying data. |
---|
200 | /// |
---|
201 | /// @note If underlying matrix goes out of scope or is deleted, the |
---|
202 | /// MatrixLookup becomes invalid and the result of further use is |
---|
203 | /// undefined. |
---|
204 | /// |
---|
205 | MatrixLookup(const MatrixLookup& ml, const utility::Index&, |
---|
206 | const bool row_vectors); |
---|
207 | |
---|
208 | /// |
---|
209 | /// Constructor creating a MatrixLookup with @a rows rows, @a |
---|
210 | /// columns columns, and all values are set to @a value. Created |
---|
211 | /// MatrixLookup owns its underlying matrix. |
---|
212 | /// |
---|
213 | MatrixLookup(const size_t rows, const size_t columns, const double value=0); |
---|
214 | |
---|
215 | /// |
---|
216 | /// @brief The istream constructor. |
---|
217 | /// |
---|
218 | /// In construction the underlying matrix is created from |
---|
219 | /// stream. The MatrixLookup will be owner of the underlying |
---|
220 | /// matrix. |
---|
221 | /// |
---|
222 | /// @see matrix(istream&) for details. |
---|
223 | /// |
---|
224 | MatrixLookup(std::istream&, char sep='\0'); |
---|
225 | |
---|
226 | /// |
---|
227 | /// @brief Destructor. |
---|
228 | /// |
---|
229 | /// If ownership is set true and there is no other owner of |
---|
230 | /// underlying data, underlying data is deleted. |
---|
231 | /// |
---|
232 | virtual ~MatrixLookup(); |
---|
233 | |
---|
234 | /** |
---|
235 | Iterator iterates along a row. When end of row is reached it |
---|
236 | jumps to beginning of next row. |
---|
237 | |
---|
238 | \return const_iterator pointing to upper-left element. |
---|
239 | */ |
---|
240 | const_iterator begin(void) const; |
---|
241 | |
---|
242 | /** |
---|
243 | Iterator iterates along a column. |
---|
244 | |
---|
245 | \return iterator pointing to first element of column \a i. |
---|
246 | */ |
---|
247 | const_column_iterator begin_column(size_t) const; |
---|
248 | |
---|
249 | /** |
---|
250 | Iterator iterates along a column. |
---|
251 | |
---|
252 | \return const_iterator pointing to first element of column \a i. |
---|
253 | */ |
---|
254 | const_row_iterator begin_row(size_t) const; |
---|
255 | |
---|
256 | /** |
---|
257 | \return number of columns |
---|
258 | */ |
---|
259 | size_t columns(void) const; |
---|
260 | |
---|
261 | /** |
---|
262 | \return const_iterator pointing to end of matrix |
---|
263 | */ |
---|
264 | const_iterator end(void) const; |
---|
265 | |
---|
266 | /** |
---|
267 | \return const_iterator pointing to end of column \a i |
---|
268 | */ |
---|
269 | const_column_iterator end_column(size_t) const; |
---|
270 | |
---|
271 | /** |
---|
272 | \return const_iterator pointing to end of row \a i |
---|
273 | */ |
---|
274 | const_row_iterator end_row(size_t) const; |
---|
275 | |
---|
276 | /** |
---|
277 | \return number of rows |
---|
278 | */ |
---|
279 | size_t rows(void) const; |
---|
280 | |
---|
281 | /// |
---|
282 | /// @return false |
---|
283 | /// |
---|
284 | bool weighted(void) const; |
---|
285 | |
---|
286 | /// |
---|
287 | /// Access operator |
---|
288 | /// |
---|
289 | /// @return element |
---|
290 | /// |
---|
291 | double operator()(const size_t row, const size_t column) const; |
---|
292 | |
---|
293 | /// |
---|
294 | /// @brief assigment operator |
---|
295 | /// |
---|
296 | /// Does only change MatrixLookup not the underlying matrix |
---|
297 | /// object. However if the MatrixLookup is owner (and the only owner) |
---|
298 | /// of its underlying |
---|
299 | /// matrix, that matrix will be deleted here. |
---|
300 | /// |
---|
301 | const MatrixLookup& operator=(const MatrixLookup&); |
---|
302 | |
---|
303 | private: |
---|
304 | friend class MatrixLookupWeighted; |
---|
305 | |
---|
306 | utility::Index column_index_; |
---|
307 | typedef utility::SmartPtr<const utility::Matrix> MatrixP; |
---|
308 | MatrixP data_; |
---|
309 | utility::Index row_index_; |
---|
310 | |
---|
311 | // for assertions |
---|
312 | bool validate(void) const; |
---|
313 | }; |
---|
314 | |
---|
315 | /// |
---|
316 | /// The output operator MatrixLookup |
---|
317 | /// |
---|
318 | std::ostream& operator<< (std::ostream& s, const MatrixLookup&); |
---|
319 | |
---|
320 | }}} // of namespace classifier, yat, and theplu |
---|
321 | |
---|
322 | #endif |
---|