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

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

Addresses #436.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 11.6 KB
RevLine 
[680]1#ifndef _theplu_yat_classifier_matrix_lookup_weighted_
2#define _theplu_yat_classifier_matrix_lookup_weighted_
[595]3
[616]4// $Id$
5
[675]6/*
[1275]7  Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
[831]8  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
[1275]9  Copyright (C) 2008 Peter Johansson
[595]10
[1437]11  This file is part of the yat library, http://dev.thep.lu.se/yat
[675]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
[1486]15  published by the Free Software Foundation; either version 3 of the
[675]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
[1110]29#include "yat/utility/Container2DIterator.h"
[1170]30#include "yat/utility/Index.h"
[1090]31#include "yat/utility/IteratorPolicy.h"
[1169]32#include "yat/utility/SmartPtr.h"
[1090]33#include "yat/utility/StrideIterator.h"
[675]34
[757]35#include <iostream>
[1090]36#include <utility>
[757]37#include <vector>
[595]38
39namespace theplu {
[680]40namespace yat {
[757]41
42namespace utility {
[1121]43  class Matrix;
[1482]44  class MatrixWeighted;
[757]45}
46
[595]47namespace classifier { 
48
[1035]49  class MatrixLookup;
50
[595]51  ///
[767]52  /// @brief Class viewing into data and weight matrix.
53  ///
[596]54  /// A MatrixLookupWeighted is very similar to a MatrixLookup, but
[597]55  /// contains a pointer to a weight matrix as well meaning each data
[826]56  /// element is associated to a weight.
[595]57  ///
[1192]58  /// A MatrixLookupWeighted can be created directly from a utility::Matrix or
59  /// from another MatrixLookupWeighted. In the latter case, the
[597]60  /// resulting MatrixLookupWeighted is looking directly into the
61  /// underlying matrix to avoid multiple lookups.
[595]62  ///
[596]63  /// There is a possibility to set the MatrixLookupWeighted as owner
[826]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.
[595]70  ///
[1170]71  class MatrixLookupWeighted
[595]72  {
73 
74  public:
[1090]75    /// 'Read Only' iterator
76    typedef utility::StrideIterator<
[1110]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> > >
[1090]83    const_iterator;
[595]84
[1125]85    /**
86       'Read only' iterator used to iterate over a column
87     */
[1105]88    typedef const_iterator const_column_iterator;
[1125]89
90    /**
91       'Read only' iterator used to iterate over a row
92     */
[1105]93    typedef const_iterator const_row_iterator;
94
[1482]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     */
[1483]108    explicit MatrixLookupWeighted(const utility::MatrixWeighted& matrix);
[1482]109
[595]110    ///
[826]111    /// Constructor creating a lookup into the entire \a matrix and \a
112    /// weights.
[595]113    ///
[596]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.
[595]117    ///
[1121]118    MatrixLookupWeighted(const utility::Matrix& matrix, 
119                         const utility::Matrix& weights,
[658]120                         const bool owner=false);
[638]121   
[1035]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       
[1192]128       @note If underlying utility::Matrix goes out of scope or
[1035]129       is deleted, the MatrixLookupWeighted becomes invalid and the
130       result of further use is undefined.
131    */
[1484]132    explicit MatrixLookupWeighted(const MatrixLookup& matrix);
[1035]133
134
[638]135    ///
[595]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,
[596]138    /// in other words, the created MatrixLookupWeighted will fullfill
[826]139    /// the following:
[596]140    /// MatrixLookupWeighted(i,j)=matrix(row[i],column[j])
[826]141    /// weights(row[i],column[j]). This also means that number of
[596]142    /// rows in created MatrixLookupWeighted is equal to size of
[1192]143    /// @a row, and number of columns is equal to size of @a column.
[595]144    ///
[596]145    /// @note If @a matrix or @a weights goes out of scope or is deleted, the
[595]146    /// MatrixLookupWeighted becomes invalid and the result of further use is
147    /// undefined.
148    ///
[1121]149    MatrixLookupWeighted(const utility::Matrix& matrix, 
150                         const utility::Matrix& weights, 
[1134]151                         const utility::Index& row, 
152                         const utility::Index& column);
[595]153
154    ///
[648]155    /// Constructor creating a lookup into a sub-matrix of @a matrix.
[595]156    ///
[596]157    /// If @a row_vectors is true the new MatrixLookupWeighted will be
158    /// consist of the row vectors defined by @a index. This means
[826]159    /// that the created MatrixLookupWeighted will fullfill:
[596]160    /// MatrixLookupWeighted(i,j)=matrix(i,index[j])*weights(i,index[j])
[826]161    ///
[595]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:
[826]166    ///
[595]167    ///
[596]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.
[595]171    ///
[1484]172    /*
[1121]173    MatrixLookupWeighted(const utility::Matrix& matrix,
174                         const utility::Matrix& weights,
[1134]175                         const utility::Index& index,
[595]176                         const bool row_vectors);
[1484]177    */
[595]178
179    ///
[826]180    /// @brief Copy constructor.
[595]181    ///
[826]182    /// If \a other is owner of underlying data, constructed
183    /// MatrixLookup will also be set as owner of underlying data.
[595]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    ///
[826]189    MatrixLookupWeighted(const MatrixLookupWeighted& other);
[595]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    ///
[826]203    /// If \a ml is owner of underlying data, constructed
204    /// MatrixLookup will also be set as owner of underlying data.
205    ///
[595]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, 
[1134]211                         const utility::Index& row, 
212                         const utility::Index& column);
[595]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    ///
[746]219    /// If @a row_vectors is true the new MatrixLookupWeighted will consist
[595]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    ///
[746]224    /// If @a row_vectors is false the new MatrixLookupWeighted will consist
[595]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    ///
[826]229    /// If \a ml is owner of underlying data, constructed
230    /// MatrixLookup will also be set as owner of underlying data.
231    ///
[595]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, 
[1134]237                         const utility::Index&, 
[595]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, 
[624]246                         const double value=0, const double weight=1);
[595]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    ///
[826]260    /// Destructor. If MatrixLookup is owner (and the only owner) of
261    /// underlying matrix, the matrices are destroyed.
[595]262    ///
263    virtual ~MatrixLookupWeighted();
264
[1091]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     */
[1105]278    const_column_iterator begin_column(size_t) const;
[1091]279
280    /**
281       Iterator iterates along a column.
282
283       \return const_iterator pointing to first element of column \a i.
284     */
[1105]285    const_row_iterator begin_row(size_t) const;
[1091]286
[1170]287    /**
288       \return number of columns
289    */
290    size_t columns(void) const;
291
[595]292    ///
[624]293    /// @return data value of element (@a row, @a column)
294    ///
[720]295    double data(size_t row, size_t column) const;
[624]296
[826]297    /**
[1091]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     */
[1105]305    const_column_iterator end_column(size_t) const;
[1091]306
307    /**
308       \return const_iterator pointing to end of row \a i
309     */
[1105]310    const_row_iterator end_row(size_t) const;
[1091]311
[1170]312    /**
313       \return number of rows
314    */
315    size_t rows(void) const;
316
[604]317    ///
[624]318    /// @return weight value of element (@a row, @a column)
319    ///
[720]320    double weight(size_t row, size_t column) const;
[624]321
322    ///
[631]323    /// @return true
324    ///
325    bool weighted(void) const;
326
327    ///
[595]328    /// Access operator
329    ///
[1193]330    /// @return data-weight pair (@a row, @a column)
[595]331    ///
[1193]332    std::pair<double, double> 
333    operator()(const size_t row, const size_t column) const;
[595]334
335    ///
336    /// @brief assigment operator
337    ///
[826]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.
[595]342    ///
343    const MatrixLookupWeighted& operator=(const MatrixLookupWeighted&);
344   
345  private:
[1169]346    typedef utility::SmartPtr<const utility::Matrix> MatrixP;
[1170]347    utility::Index column_index_;
[1169]348    MatrixP data_;
[1170]349    utility::Index row_index_;
[1169]350    MatrixP weights_;
[1170]351
352    // for assertions
353    bool validate(void) const;
[595]354  }; 
355 
356  ///
[597]357  /// The output operator MatrixLookupWeighted
[595]358  ///
[826]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  ///
[595]362  std::ostream& operator<< (std::ostream& s, const MatrixLookupWeighted&);
363
[680]364}}} // of namespace classifier, yat, and theplu
[595]365
366#endif
Note: See TracBrowser for help on using the repository browser.