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

Last change on this file since 1487 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

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