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

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

closes #396

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