source: trunk/yat/classifier/MatrixLookup.h @ 1125

Last change on this file since 1125 was 1125, checked in by Peter, 14 years ago

fixing Doxygen parsing

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 11.5 KB
Line 
1#ifndef _theplu_yat_classifier_matrix_lookup_
2#define _theplu_yat_classifier_matrix_lookup_
3
4// $Id$
5
6/*
7  Copyright (C) 2005 Markus Ringnér, Peter Johansson
8  Copyright (C) 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
9  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
10
11  This file is part of the yat library, http://trac.thep.lu.se/yat
12
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 2 of the
16  License, or (at your option) any later version.
17
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26  02111-1307, USA.
27*/
28
29#include "DataLookup2D.h"
30#include "yat/utility/Container2DIterator.h"
31#include "yat/utility/iterator_traits.h"
32#include "yat/utility/StrideIterator.h"
33
34#include <iostream>
35#include <vector>
36
37namespace theplu {
38namespace yat {
39
40namespace utility {
41  class Matrix;
42}
43
44namespace classifier { 
45
46  ///
47  /// @brief General view into utility::matrix
48  ///
49  /// MatrixLookups can be used to create lookups/views into matrices
50  /// in a more general way than the views supported in the matrix
51  /// class. The object does not contain any data, but only a pointer
52  /// to a matrix and row and columns incices defining which
53  /// sub-matrix to look into.  Each row and each column corresponds
54  /// to a row and a column in the Kernel, respectively. This design
55  /// allow for fast creation of sub-matrices, which is a common
56  /// operation in most traning/validation procedures. The views are
57  /// const views in the sense that they cannot modify underlying
58  /// matrix.
59  ///
60  /// A MatrixLookup can be created directly from a matrix or from an
61  /// other MatrixLookup. In the latter case, the resulting
62  /// MatrixLookup is looking directly into the underlying matrix to
63  /// avoid multiple lookups.
64  ///
65  /// There is a possibility to set the MatrixLookup as owner of the
66  /// underlying matrix. This implies that underlying data is deleted
67  /// in destructor of MatrixLookup, but only if there is no other
68  /// owner of the underlying data. A reference counter is used to
69  /// keep track of number of owners. Ownership is copied in copy
70  /// constructors and assignments.
71  ///
72  class MatrixLookup : public DataLookup2D
73  {
74  public:
75    /// 'Read Only' iterator
76    typedef utility::StrideIterator<
77    utility::Container2DIterator<const MatrixLookup, const double, void, 
78                                 const double> >
79    const_iterator;
80
81    /**
82       'Read only' iterator used to iterate over a column
83     */
84    typedef const_iterator const_column_iterator;
85
86    /**
87       'Read only' iterator used to iterate over a row
88     */
89    typedef const_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 std::vector<size_t>& row, 
121                 const std::vector<size_t>& 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 std::vector<size_t>& 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 std::vector<size_t>& row, 
181                 const std::vector<size_t>& 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 std::vector<size_t>&, 
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 const_iterator pointing to end of matrix
258     */
259    const_iterator end(void) const;
260
261    /**
262       \return const_iterator pointing to end of column \a i
263     */
264    const_column_iterator end_column(size_t) const;
265
266    /**
267       \return const_iterator pointing to end of row \a i
268     */
269    const_row_iterator end_row(size_t) const;
270
271    /**
272       the new MatrixLookup will consist of the rolumn vectors
273       defined by @a index. This means that the returned MatrixLookup
274       will fullfill: \f$ returned(i,j) = original(index[i],j) \f$
275
276       @note If underlying matrix goes out of scope or is deleted, the
277       returned pointer becomes invalid and the result of further use is
278       undefined.
279   
280    */
281    const MatrixLookup* selected(const std::vector<size_t>&) const;
282
283    ///
284    /// The created MatrixLookup corresponds to all rows and the
285    /// columns defined by @a index in the original MatrixLookup. The
286    /// created MatrixLookup will fullfill:
287    /// \f$ novel_ml(i,j)=original(i,index[j]) \f$.
288    ///
289    /// @return pointer to sub-Lookup of the MatrixLookup
290    ///
291    /// @note If underlying matrix goes out of scope or is deleted, the
292    /// returned pointer becomes invalid and the result of further use is
293    /// undefined.
294    ///
295    /// @note Returns a dynamically allocated DataLookup2D, which has
296    /// to be deleted by the caller to avoid memory leaks.
297    ///
298    const MatrixLookup* training_data(const std::vector<size_t>& index) const;
299   
300    ///
301    /// The created MatrixLookup corresponds to all rows and the
302    /// columns defined by @a index in the original MatrixLookup. The
303    /// created MatrixLookup will fullfill:
304    /// \f$ novel_ml(i,j)=original(i,index[j]) \f$.
305    ///
306    /// @return pointer to sub-Lookup of the MatrixLookup
307    ///
308    /// @note If underlying matrix goes out of scope or is deleted, the
309    /// returned pointer becomes invalid and the result of further use is
310    /// undefined.
311    ///
312    const MatrixLookup* validation_data(const std::vector<size_t>&,
313                                        const std::vector<size_t>&) const;
314    ///
315    /// @return false
316    ///
317    bool weighted(void) const;
318
319    ///
320    /// Access operator
321    ///
322    /// @return element
323    ///
324    double operator()(const size_t row, const size_t column) const;
325
326    ///
327    /// @brief assigment operator
328    ///
329    /// Does only change MatrixLookup not the underlying matrix
330    /// object. However if the MatrixLookup is owner (and the only owner)
331    /// of its underlying
332    /// matrix, that matrix will be deleted here.
333    ///
334    const MatrixLookup& operator=(const MatrixLookup&);
335   
336  private:
337    friend class MatrixLookupWeighted;
338
339    const utility::Matrix* data_;
340  }; 
341 
342}}} // of namespace classifier, yat, and theplu
343
344#endif
Note: See TracBrowser for help on using the repository browser.