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

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

fixes #444

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 9.3 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/MatrixWeighted.h"
31#include "yat/utility/SmartPtr.h"
32#include "yat/utility/StrideIterator.h"
33
34#include <boost/iterator/permutation_iterator.hpp>
35
36#include <iostream>
37#include <utility>
38#include <vector>
39
40namespace theplu {
41namespace yat {
42namespace classifier { 
43
44  class MatrixLookup;
45
46  ///
47  /// @brief General view into utility::MatrixWeighted
48  ///
49  /// A MatrixLookupWeighted can be created directly from a
50  /// utility::MatrixWeighted or from another MatrixLookupWeighted. In
51  /// the latter case, the resulting MatrixLookupWeighted is looking
52  /// directly into the underlying matrix to avoid multiple lookups.
53  ///
54  /// There is a possibility to set the MatrixLookupWeighted as owner
55  /// of the underlying utility::MatrixWeighted.  This implies that
56  /// underlying data is deleted in destructor of
57  /// MatrixLookupWeighted, but only if there is no other owner of the
58  /// underlying data.
59  ///
60  /// \see MatrixLookup
61  ///
62  class MatrixLookupWeighted
63  {
64  public:
65    /**
66       value_type is DataWeight
67
68       \since New in yat 0.5
69     */
70    typedef utility::DataWeight value_type;
71
72    /**
73       const_reference type is const DataWeight
74
75       \since New in yat 0.5
76     */
77    typedef const utility::DataWeight& const_reference;
78
79    /// 'Read Only' iterator
80    typedef utility::StrideIterator<
81    utility::Container2DIterator<const MatrixLookupWeighted, 
82                                 value_type, const_reference> >
83    const_iterator;
84
85    /**
86       'Read only' iterator used to iterate over a column
87     */
88    typedef boost::permutation_iterator<
89      utility::MatrixWeighted::const_column_iterator,
90      utility::Index::const_iterator> const_column_iterator;
91
92    /**
93       'Read only' iterator used to iterate over a row
94     */
95    typedef boost::permutation_iterator<
96      utility::MatrixWeighted::const_row_iterator,
97      utility::Index::const_iterator> const_row_iterator;
98
99    /**
100       \brief Create a lookup into \a matrix.
101
102       The created lookup, mlw, will fullfil: mlw(i,j) =
103       matrix(rows(i), columns(j))
104     */
105    MatrixLookupWeighted(const utility::MatrixWeighted& matrix,
106                         const utility::Index& rows,
107                         const utility::Index& columns);
108
109    /**
110       \brief Create a lookup into entire \a matrix.
111     */
112    explicit MatrixLookupWeighted(const utility::MatrixWeighted& matrix,
113                                  bool owner=false);
114
115    /**
116       Constructor creating a MatrixLookupWeighted from a MatrixLookup. A
117       weight matrix with unitary weights are created internally.
118
119       \note no check for nan is performed.
120       
121       @note from yat 0.5 data is copied and further modifications in
122       \a matrix will not be reflected in MatrixLookupWeighted.
123    */
124    explicit MatrixLookupWeighted(const MatrixLookup& matrix);
125
126
127    ///
128    /// @brief Copy constructor.
129    ///
130    /// If \a other is owner of underlying data, constructed
131    /// MatrixLookup will also be set as owner of underlying data.
132    ///
133    /// @note If underlying matrix goes out of scope or is deleted, the
134    /// MatrixLookupWeighted becomes invalid and the result of further use is
135    /// undefined.
136    ///
137    MatrixLookupWeighted(const MatrixLookupWeighted& other);
138
139    ///
140    /// Creates a sub-MatrixLookupWeighted. The Lookup is independent of
141    /// MatrixLookupWeighted @a ml. The MatrixLookupWeighted is created to look
142    /// directly into the underlying matrix to avoid multiple lookups.
143    ///
144    /// The @a row and @a column define what sub-matrix to look into,
145    /// in other words, the created MatrixLookupWeighted will fullfill the
146    /// following: \f$ MatrixLookupWeighted(i,j)=ml(row[i],column[j]) \f$. This
147    /// also means that number of rows in created MatrixLookupWeighted is
148    /// equal to size of vector @a row, and number of columns is equal
149    /// to size of vector @a column.
150    ///
151    /// If \a ml is owner of underlying data, constructed
152    /// MatrixLookup will also be set as owner of underlying data.
153    ///
154    /// @note If underlying matrix goes out of scope or is deleted, the
155    /// MatrixLookupWeighted becomes invalid and the result of further use is
156    /// undefined.
157    ///
158    MatrixLookupWeighted(const MatrixLookupWeighted& ml, 
159                         const utility::Index& row, 
160                         const utility::Index& column);
161
162    ///
163    /// Constructor creating a lookup into a sub-matrix of
164    /// @a ml. The MatrixLookupWeighted is created to look directly into the
165    /// underlying matrix to avoid multiple lookups.
166    ///
167    /// If @a row_vectors is true the new MatrixLookupWeighted will consist
168    /// of the row vectors defined by @a index. This means that the
169    /// created MatrixLookupWeighted will fullfill:
170    /// \f$ MatrixLookupWeighted(i,j)=ml(i,index[j])\f$
171    ///
172    /// If @a row_vectors is false the new MatrixLookupWeighted will consist
173    /// of the rolumn vectors defined by @a index. This means that the
174    /// created MatrixLookupWeighted will fullfill:
175    /// \f$ MatrixLookupWeighted(i,j) = ml(index[i],j) \f$
176    ///
177    /// If \a ml is owner of underlying data, constructed
178    /// MatrixLookup will also be set as owner of underlying data.
179    ///
180    /// @note If underlying matrix goes out of scope or is deleted, the
181    /// MatrixLookupWeighted becomes invalid and the result of further use is
182    /// undefined.
183    ///
184    MatrixLookupWeighted(const MatrixLookupWeighted& ml, 
185                         const utility::Index&, 
186                         const bool row_vectors);
187
188    ///
189    /// Constructor creating a MatrixLookupWeighted with @a rows rows, @a
190    /// columns columns, and all values are set to @a value. Created
191    /// MatrixLookupWeighted owns its underlying matrix.
192    ///
193    MatrixLookupWeighted(const size_t rows, const size_t columns, 
194                         const double value=0, const double weight=1);
195
196    ///
197    /// @brief The istream constructor.
198    ///
199    /// In construction the underlying matrix is created from
200    /// stream. The MatrixLookupWeighted will be owner of the underlying
201    /// matrix.
202    ///
203    /// @see utility::MatrixWeighted(istream&) for details.
204    ///
205    MatrixLookupWeighted(std::istream&, char sep='\0');
206
207    ///
208    /// Destructor. If MatrixLookup is owner (and the only owner) of
209    /// underlying matrix, the matrices are destroyed.
210    ///
211    virtual ~MatrixLookupWeighted();
212
213    /**
214       Iterator iterates along a row. When end of row is reached it
215       jumps to beginning of next row.
216
217       \return const_iterator pointing to upper-left element.
218     */
219    const_iterator begin(void) const;
220
221    /**
222       Iterator iterates along a column.
223
224       \return iterator pointing to first element of column \a i.
225     */
226    const_column_iterator begin_column(size_t) const;
227
228    /**
229       Iterator iterates along a column.
230
231       \return const_iterator pointing to first element of column \a i.
232     */
233    const_row_iterator begin_row(size_t) const;
234
235    /**
236       \return number of columns
237    */
238    size_t columns(void) const;
239
240    ///
241    /// @return data value of element (@a row, @a column)
242    ///
243    double data(size_t row, size_t column) const;
244
245    /**
246       \return const_iterator pointing to end of matrix
247     */
248    const_iterator end(void) const;
249
250    /**
251       \return const_iterator pointing to end of column \a i
252     */
253    const_column_iterator end_column(size_t) const;
254
255    /**
256       \return const_iterator pointing to end of row \a i
257     */
258    const_row_iterator end_row(size_t) const;
259
260    /**
261       \return number of rows
262    */
263    size_t rows(void) const;
264
265    ///
266    /// @return weight value of element (@a row, @a column)
267    ///
268    double weight(size_t row, size_t column) const;
269
270    ///
271    /// @return true
272    ///
273    bool weighted(void) const;
274
275    ///
276    /// Access operator
277    ///
278    /// @return data-weight pair (@a row, @a column)
279    ///
280    const_reference operator()(const size_t row, const size_t column) const;
281
282    ///
283    /// @brief assigment operator
284    ///
285    /// Does only change MatrixLookupWeighted not the underlying
286    /// matrix object. However if the MatrixLookupWeighted is owner
287    /// (and the only owner) of its underlying data, those data will
288    /// be deleted here.
289    ///
290    const MatrixLookupWeighted& operator=(const MatrixLookupWeighted&);
291   
292  private:
293    typedef utility::SmartPtr<const utility::MatrixWeighted> MatrixWP;
294    utility::Index column_index_;
295    MatrixWP data_;
296    utility::Index row_index_;
297
298    // for assertions
299    bool validate(void) const;
300  }; 
301 
302  ///
303  /// The output operator MatrixLookupWeighted
304  ///
305  /// For eacd data element data(i,j) is printed except those being
306  /// associated with a zero weight for which nothing is printed.
307  ///
308  std::ostream& operator<< (std::ostream& s, const MatrixLookupWeighted&);
309
310}}} // of namespace classifier, yat, and theplu
311
312#endif
Note: See TracBrowser for help on using the repository browser.