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

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

refs #396

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