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

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

refs #396
stupid implementation of constructors - copying should be avoided when
internal representation in MatrixLookupWeighted? has changed

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