source: trunk/yat/classifier/MatrixLookupWeighted.h @ 1549

Last change on this file since 1549 was 1549, checked in by Peter, 13 years ago

adding some typedefs in Container2Ds - refs #448

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 11.8 KB
Line 
1#ifndef _theplu_yat_classifier_matrix_lookup_weighted_
2#define _theplu_yat_classifier_matrix_lookup_weighted_
3
4// $Id$
5
6/*
7  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
8  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
9  Copyright (C) 2008 Peter Johansson
10
11  This file is part of the yat library, http://dev.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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
25*/
26
27#include "yat/utility/Container2DIterator.h"
28#include "yat/utility/DataWeight.h"
29#include "yat/utility/Index.h"
30#include "yat/utility/IteratorPolicy.h"
31#include "yat/utility/SmartPtr.h"
32#include "yat/utility/StrideIterator.h"
33
34#include <iostream>
35#include <utility>
36#include <vector>
37
38namespace theplu {
39namespace yat {
40
41namespace utility {
42  class Matrix;
43  class MatrixWeighted;
44}
45
46namespace classifier { 
47
48  class MatrixLookup;
49
50  ///
51  /// @brief Class viewing into data and weight matrix.
52  ///
53  /// A MatrixLookupWeighted is very similar to a MatrixLookup, but
54  /// contains a pointer to a weight matrix as well meaning each data
55  /// element is associated to a weight.
56  ///
57  /// A MatrixLookupWeighted can be created directly from a utility::Matrix or
58  /// from another MatrixLookupWeighted. In the latter case, the
59  /// resulting MatrixLookupWeighted is looking directly into the
60  /// underlying matrix to avoid multiple lookups.
61  ///
62  /// There is a possibility to set the MatrixLookupWeighted as owner
63  /// of the underlying matrices (data and weight).
64  /// This implies that underlying data is deleted
65  /// in destructor of MatrixLookupWeighted, but only if there is no other
66  /// owner of the underlying data. A reference counter is used to
67  /// keep track of number of owners. Ownership is copied in copy
68  /// constructors and assignments.
69  ///
70  class MatrixLookupWeighted
71  {
72 
73  public:
74    /**
75       value_type is DataWeight
76
77       \since New in yat 0.5
78     */
79    typedef utility::DataWeight value_type;
80
81    /**
82       const_reference type is const DataWeight
83
84       \since New in yat 0.5
85     */
86    typedef const utility::DataWeight const_reference;
87
88    /// 'Read Only' iterator
89    typedef utility::StrideIterator<
90    utility::Container2DIterator<const MatrixLookupWeighted, 
91                                 const std::pair<double, double>, void, 
92                                 const std::pair<double, double>,
93             utility::IteratorPolicyWeighted<const MatrixLookupWeighted,
94                                             const double,
95                                             const double> > >
96    const_iterator;
97
98    /**
99       'Read only' iterator used to iterate over a column
100     */
101    typedef const_iterator const_column_iterator;
102
103    /**
104       'Read only' iterator used to iterate over a row
105     */
106    typedef const_iterator const_row_iterator;
107
108    /**
109       \brief Create a lookup into \a matrix.
110
111       The created lookup, mlw, will fullfil: mlw(i,j) =
112       matrix(rows(i), columns(j))
113     */
114    MatrixLookupWeighted(const utility::MatrixWeighted& matrix,
115                         const utility::Index& rows,
116                         const utility::Index& columns);
117
118    /**
119       \brief Create a lookup into entire \a matrix.
120     */
121    explicit MatrixLookupWeighted(const utility::MatrixWeighted& matrix);
122
123    ///
124    /// Constructor creating a lookup into the entire \a matrix and \a
125    /// weights.
126    ///
127    /// @note If @a matrix or @a weights goes out of scope or is
128    /// deleted, the MatrixLookupWeighted becomes invalid and the
129    /// result of further use is undefined.
130    ///
131    MatrixLookupWeighted(const utility::Matrix& matrix, 
132                         const utility::Matrix& weights,
133                         const bool owner=false);
134   
135    /**
136       Constructor creating a MatrixLookupWeighted from a MatrixLookup. A
137       weight matrix with unitary weights are created internally.
138
139       \note no check for nan is performed.
140       
141       @note If underlying utility::Matrix goes out of scope or
142       is deleted, the MatrixLookupWeighted becomes invalid and the
143       result of further use is undefined.
144    */
145    explicit MatrixLookupWeighted(const MatrixLookup& matrix);
146
147
148    ///
149    /// Constructor creating a lookup into a sub-matrix of @a matrix.
150    /// The @a row and @a column define what sub-matrix to look into,
151    /// in other words, the created MatrixLookupWeighted will fullfill
152    /// the following:
153    /// MatrixLookupWeighted(i,j)=matrix(row[i],column[j])
154    /// weights(row[i],column[j]). This also means that number of
155    /// rows in created MatrixLookupWeighted is equal to size of
156    /// @a row, and number of columns is equal to size of @a column.
157    ///
158    /// @note If @a matrix or @a weights goes out of scope or is deleted, the
159    /// MatrixLookupWeighted becomes invalid and the result of further use is
160    /// undefined.
161    ///
162    MatrixLookupWeighted(const utility::Matrix& matrix, 
163                         const utility::Matrix& weights, 
164                         const utility::Index& row, 
165                         const utility::Index& column);
166
167    ///
168    /// Constructor creating a lookup into a sub-matrix of @a matrix.
169    ///
170    /// If @a row_vectors is true the new MatrixLookupWeighted will be
171    /// consist of the row vectors defined by @a index. This means
172    /// that the created MatrixLookupWeighted will fullfill:
173    /// MatrixLookupWeighted(i,j)=matrix(i,index[j])*weights(i,index[j])
174    ///
175    ///
176    /// If @a row_vectors is false the new MatrixLookupWeighted will be consist
177    /// of the rolumn vectors defined by @a index. This means that the
178    /// created MatrixLookupWeighted will fullfill:
179    ///
180    ///
181    /// @note If @a matrix or @a weights goes out of scope or is
182    /// deleted, the MatrixLookupWeighted becomes invalid and the
183    /// result of further use is undefined.
184    ///
185    /*
186    MatrixLookupWeighted(const utility::Matrix& matrix,
187                         const utility::Matrix& weights,
188                         const utility::Index& index,
189                         const bool row_vectors);
190    */
191
192    ///
193    /// @brief Copy constructor.
194    ///
195    /// If \a other is owner of underlying data, constructed
196    /// MatrixLookup will also be set as owner of underlying data.
197    ///
198    /// @note If underlying matrix goes out of scope or is deleted, the
199    /// MatrixLookupWeighted becomes invalid and the result of further use is
200    /// undefined.
201    ///
202    MatrixLookupWeighted(const MatrixLookupWeighted& other);
203
204    ///
205    /// Creates a sub-MatrixLookupWeighted. The Lookup is independent of
206    /// MatrixLookupWeighted @a ml. The MatrixLookupWeighted is created to look
207    /// directly into the underlying matrix to avoid multiple lookups.
208    ///
209    /// The @a row and @a column define what sub-matrix to look into,
210    /// in other words, the created MatrixLookupWeighted will fullfill the
211    /// following: \f$ MatrixLookupWeighted(i,j)=ml(row[i],column[j]) \f$. This
212    /// also means that number of rows in created MatrixLookupWeighted is
213    /// equal to size of vector @a row, and number of columns is equal
214    /// to size of vector @a column.
215    ///
216    /// If \a ml is owner of underlying data, constructed
217    /// MatrixLookup will also be set as owner of underlying data.
218    ///
219    /// @note If underlying matrix goes out of scope or is deleted, the
220    /// MatrixLookupWeighted becomes invalid and the result of further use is
221    /// undefined.
222    ///
223    MatrixLookupWeighted(const MatrixLookupWeighted& ml, 
224                         const utility::Index& row, 
225                         const utility::Index& column);
226
227    ///
228    /// Constructor creating a lookup into a sub-matrix of
229    /// @a ml. The MatrixLookupWeighted is created to look directly into the
230    /// underlying matrix to avoid multiple lookups.
231    ///
232    /// If @a row_vectors is true the new MatrixLookupWeighted will consist
233    /// of the row vectors defined by @a index. This means that the
234    /// created MatrixLookupWeighted will fullfill:
235    /// \f$ MatrixLookupWeighted(i,j)=ml(i,index[j])\f$
236    ///
237    /// If @a row_vectors is false the new MatrixLookupWeighted will consist
238    /// of the rolumn vectors defined by @a index. This means that the
239    /// created MatrixLookupWeighted will fullfill:
240    /// \f$ MatrixLookupWeighted(i,j) = ml(index[i],j) \f$
241    ///
242    /// If \a ml is owner of underlying data, constructed
243    /// MatrixLookup will also be set as owner of underlying data.
244    ///
245    /// @note If underlying matrix goes out of scope or is deleted, the
246    /// MatrixLookupWeighted becomes invalid and the result of further use is
247    /// undefined.
248    ///
249    MatrixLookupWeighted(const MatrixLookupWeighted& ml, 
250                         const utility::Index&, 
251                         const bool row_vectors);
252
253    ///
254    /// Constructor creating a MatrixLookupWeighted with @a rows rows, @a
255    /// columns columns, and all values are set to @a value. Created
256    /// MatrixLookupWeighted owns its underlying matrix.
257    ///
258    MatrixLookupWeighted(const size_t rows, const size_t columns, 
259                         const double value=0, const double weight=1);
260
261    ///
262    /// @brief The istream constructor.
263    ///
264    /// In construction the underlying matrix is created from
265    /// stream. The MatrixLookupWeighted will be owner of the underlying
266    /// matrix.
267    ///
268    /// @see matrix(istream&) for details.
269    ///
270    MatrixLookupWeighted(std::istream&, char sep='\0');
271
272    ///
273    /// Destructor. If MatrixLookup is owner (and the only owner) of
274    /// underlying matrix, the matrices are destroyed.
275    ///
276    virtual ~MatrixLookupWeighted();
277
278    /**
279       Iterator iterates along a row. When end of row is reached it
280       jumps to beginning of next row.
281
282       \return const_iterator pointing to upper-left element.
283     */
284    const_iterator begin(void) const;
285
286    /**
287       Iterator iterates along a column.
288
289       \return iterator pointing to first element of column \a i.
290     */
291    const_column_iterator begin_column(size_t) const;
292
293    /**
294       Iterator iterates along a column.
295
296       \return const_iterator pointing to first element of column \a i.
297     */
298    const_row_iterator begin_row(size_t) const;
299
300    /**
301       \return number of columns
302    */
303    size_t columns(void) const;
304
305    ///
306    /// @return data value of element (@a row, @a column)
307    ///
308    double data(size_t row, size_t column) const;
309
310    /**
311       \return const_iterator pointing to end of matrix
312     */
313    const_iterator end(void) const;
314
315    /**
316       \return const_iterator pointing to end of column \a i
317     */
318    const_column_iterator end_column(size_t) const;
319
320    /**
321       \return const_iterator pointing to end of row \a i
322     */
323    const_row_iterator end_row(size_t) const;
324
325    /**
326       \return number of rows
327    */
328    size_t rows(void) const;
329
330    ///
331    /// @return weight value of element (@a row, @a column)
332    ///
333    double weight(size_t row, size_t column) const;
334
335    ///
336    /// @return true
337    ///
338    bool weighted(void) const;
339
340    ///
341    /// Access operator
342    ///
343    /// @return data-weight pair (@a row, @a column)
344    ///
345    std::pair<double, double> 
346    operator()(const size_t row, const size_t column) const;
347
348    ///
349    /// @brief assigment operator
350    ///
351    /// Does only change MatrixLookupWeighted not the underlying
352    /// matrix object. However if the MatrixLookupWeighted is owner
353    /// (and the only owner) of its underlying data, those data will
354    /// be deleted here.
355    ///
356    const MatrixLookupWeighted& operator=(const MatrixLookupWeighted&);
357   
358  private:
359    typedef utility::SmartPtr<const utility::Matrix> MatrixP;
360    utility::Index column_index_;
361    MatrixP data_;
362    utility::Index row_index_;
363    MatrixP weights_;
364
365    // for assertions
366    bool validate(void) const;
367  }; 
368 
369  ///
370  /// The output operator MatrixLookupWeighted
371  ///
372  /// For eacd data element data(i,j) is printed except those being
373  /// associated with a zero weight for which nothing is printed.
374  ///
375  std::ostream& operator<< (std::ostream& s, const MatrixLookupWeighted&);
376
377}}} // of namespace classifier, yat, and theplu
378
379#endif
Note: See TracBrowser for help on using the repository browser.