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

Last change on this file since 767 was 767, checked in by Peter, 16 years ago

Fixes #65

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 10.1 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) The authors contributing to this file.
8
9  This file is part of the yat library, http://lev.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "DataLookup2D.h"
28
29#include <iostream>
30#include <vector>
31
32namespace theplu {
33namespace yat {
34
35namespace utility {
36  class matrix;
37}
38
39namespace classifier { 
40
41  ///
42  /// @brief Class viewing into data and weight matrix.
43  ///
44  /// A MatrixLookupWeighted is very similar to a MatrixLookup, but
45  /// contains a pointer to a weight matrix as well meaning each data
46  /// element is associated to weight.
47  ///
48  /// A MatrixLookupWeighted can be created directly from a matrix or
49  /// from an other MatrixLookupWeighted. In the latter case, the
50  /// resulting MatrixLookupWeighted is looking directly into the
51  /// underlying matrix to avoid multiple lookups.
52  ///
53  /// There is a possibility to set the MatrixLookupWeighted as owner
54  /// of the underlying matrices (data and weight). In that case the
55  /// underlying matrix will be destroyed in the
56  /// destructor. Consequently, the underlying matricis must have been
57  /// dynamically allocated and no other MatrixLookupWeighted can own
58  /// the matrices.
59  ///
60  class MatrixLookupWeighted : public DataLookup2D
61  {
62 
63  public:
64
65
66    ///
67    /// Constructor creating a lookup into the entire @a matrix.
68    ///
69    /// @note If @a matrix or @a weights goes out of scope or is
70    /// deleted, the MatrixLookupWeighted becomes invalid and the
71    /// result of further use is undefined.
72    ///
73    MatrixLookupWeighted(const utility::matrix& matrix, 
74                         const utility::matrix& weights,
75                         const bool owner=false);
76
77   
78    ///
79    /// Constructor creating a lookup into the entire @a matrix.  A
80    /// weight matrix is constructed with weights 1 for values and 0
81    /// for nan's in @a matrix. @note If @a matrix goes out of scope or
82    /// is deleted, the MatrixLookupWeighted becomes invalid and the
83    /// result of further use is undefined.
84    ///
85    MatrixLookupWeighted(const utility::matrix& matrix);
86
87
88    ///
89    /// Constructor creating a lookup into a sub-matrix of @a matrix.
90    /// The @a row and @a column define what sub-matrix to look into,
91    /// in other words, the created MatrixLookupWeighted will fullfill
92    /// the following: \f$
93    /// MatrixLookupWeighted(i,j)=matrix(row[i],column[j])
94    /// weights(row[i],column[j])\f$. This also means that number of
95    /// rows in created MatrixLookupWeighted is equal to size of
96    /// vector @a row, and number of columns is equal to size of
97    /// vector @a column.
98    ///
99    /// @note If @a matrix or @a weights goes out of scope or is deleted, the
100    /// MatrixLookupWeighted becomes invalid and the result of further use is
101    /// undefined.
102    ///
103    MatrixLookupWeighted(const utility::matrix& matrix, 
104                         const utility::matrix& weights, 
105                         const std::vector<size_t>& row, 
106                         const std::vector<size_t>& column);
107
108    ///
109    /// Constructor creating a lookup into a sub-matrix of @a matrix.
110    ///
111    /// If @a row_vectors is true the new MatrixLookupWeighted will be
112    /// consist of the row vectors defined by @a index. This means
113    /// that the created MatrixLookupWeighted will fullfill: \f$
114    /// MatrixLookupWeighted(i,j)=matrix(i,index[j])*weights(i,index[j])
115    /// \f$
116    ///
117    /// If @a row_vectors is false the new MatrixLookupWeighted will be consist
118    /// of the rolumn vectors defined by @a index. This means that the
119    /// created MatrixLookupWeighted will fullfill:
120    /// \f$ MatrixLookupWeighted(i,j)=matrix(index[i],j) \f$
121    ///
122    /// @note If @a matrix or @a weights goes out of scope or is
123    /// deleted, the MatrixLookupWeighted becomes invalid and the
124    /// result of further use is undefined.
125    ///
126    MatrixLookupWeighted(const utility::matrix& matrix, 
127                         const utility::matrix& weights, 
128                         const std::vector<size_t>& index, 
129                         const bool row_vectors);
130
131    ///
132    /// @brief
133    ///
134    /// @note If underlying matrix goes out of scope or is deleted, the
135    /// MatrixLookupWeighted becomes invalid and the result of further use is
136    /// undefined.
137    ///
138    //MatrixLookupWeighted(const MatrixLookup&, const MatrixLookup);
139
140    ///
141    /// @brief Copy constructor.
142    ///
143    /// @note If underlying matrix 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 MatrixLookupWeighted&);
148
149    ///
150    /// Creates a sub-MatrixLookupWeighted. The Lookup is independent of
151    /// MatrixLookupWeighted @a ml. The MatrixLookupWeighted is created to look
152    /// directly into the underlying matrix to avoid multiple lookups.
153    ///
154    /// The @a row and @a column define what sub-matrix to look into,
155    /// in other words, the created MatrixLookupWeighted will fullfill the
156    /// following: \f$ MatrixLookupWeighted(i,j)=ml(row[i],column[j]) \f$. This
157    /// also means that number of rows in created MatrixLookupWeighted is
158    /// equal to size of vector @a row, and number of columns is equal
159    /// to size of vector @a column.
160    ///
161    /// @note If underlying matrix 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 MatrixLookupWeighted& ml, 
166                         const std::vector<size_t>& row, 
167                         const std::vector<size_t>& column);
168
169    ///
170    /// Constructor creating a lookup into a sub-matrix of
171    /// @a ml. The MatrixLookupWeighted is created to look directly into the
172    /// underlying matrix to avoid multiple lookups.
173    ///
174    /// If @a row_vectors is true the new MatrixLookupWeighted will consist
175    /// of the row vectors defined by @a index. This means that the
176    /// created MatrixLookupWeighted will fullfill:
177    /// \f$ MatrixLookupWeighted(i,j)=ml(i,index[j])\f$
178    ///
179    /// If @a row_vectors is false the new MatrixLookupWeighted will consist
180    /// of the rolumn vectors defined by @a index. This means that the
181    /// created MatrixLookupWeighted will fullfill:
182    /// \f$ MatrixLookupWeighted(i,j) = ml(index[i],j) \f$
183    ///
184    /// @note If underlying matrix goes out of scope or is deleted, the
185    /// MatrixLookupWeighted becomes invalid and the result of further use is
186    /// undefined.
187    ///
188    MatrixLookupWeighted(const MatrixLookupWeighted& ml, 
189                         const std::vector<size_t>&, 
190                         const bool row_vectors);
191
192    ///
193    /// Constructor creating a MatrixLookupWeighted with @a rows rows, @a
194    /// columns columns, and all values are set to @a value. Created
195    /// MatrixLookupWeighted owns its underlying matrix.
196    ///
197    MatrixLookupWeighted(const size_t rows, const size_t columns, 
198                         const double value=0, const double weight=1);
199
200    ///
201    /// @brief The istream constructor.
202    ///
203    /// In construction the underlying matrix is created from
204    /// stream. The MatrixLookupWeighted will be owner of the underlying
205    /// matrix.
206    ///
207    /// @see matrix(istream&) for details.
208    ///
209    MatrixLookupWeighted(std::istream&, char sep='\0');
210
211    ///
212    /// Destructor. If MatrixLookup is owner of underlying matrix, the
213    /// matrix is destroyed.
214    ///
215    virtual ~MatrixLookupWeighted();
216
217    ///
218    /// @return data value of element (@a row, @a column)
219    ///
220    double data(size_t row, size_t column) const;
221
222    ///
223    /// @todo doc
224    ///
225    const MatrixLookupWeighted* selected(const std::vector<size_t>& i) const;
226
227    ///
228    /// The created MatrixLookupWeighted corresponds to all rows and the
229    /// columns defined by @a index in the original MatrixLookupWeighted. The
230    /// created MatrixLookupWeighted will fullfill:
231    /// \f$ novel_ml(i,j)=original(i,index[j]) \f$.
232    ///
233    /// @return pointer to sub-Lookup of the MatrixLookupWeighted
234    ///
235    /// @note If underlying matrix goes out of scope or is deleted, the
236    /// returned pointer becomes invalid and the result of further use is
237    /// undefined.
238    ///
239    const MatrixLookupWeighted* 
240    training_data(const std::vector<size_t>& index) const;
241   
242    ///
243    /// The created MatrixLookupWeighted corresponds to all rows and the
244    /// columns defined by @a index in the original MatrixLookupWeighted. The
245    /// created MatrixLookupWeighted will fullfill:
246    /// \f$ novel_ml(i,j)=original(i,index[j]) \f$.
247    ///
248    /// @return pointer to sub-Lookup of the MatrixLookupWeighted
249    ///
250    /// @note If underlying matrix goes out of scope or is deleted, the
251    /// returned pointer becomes invalid and the result of further use is
252    /// undefined.
253    ///
254    const MatrixLookupWeighted* validation_data(const std::vector<size_t>&,
255                                        const std::vector<size_t>&) const;
256
257    ///
258    /// @return weight value of element (@a row, @a column)
259    ///
260    double weight(size_t row, size_t column) const;
261
262    ///
263    /// @return true
264    ///
265    bool weighted(void) const;
266
267    ///
268    /// Access operator
269    ///
270    /// @return weight * data for element \f$ i j\f$
271    ///
272    double operator()(const size_t row, const size_t column) const;
273
274    ///
275    /// @brief assigment operator
276    ///
277    /// Does only change MatrixLookupWeighted not the underlying matrix
278    /// object. However if the MatrixLookupWeighted is owner of its underlying
279    /// matrix, that matrix will be deleted here.
280    ///
281    const MatrixLookupWeighted& operator=(const MatrixLookupWeighted&);
282   
283  private:
284    const utility::matrix* data_;
285    const utility::matrix* weights_;
286    u_int* ref_count_weights_;
287  }; 
288 
289  ///
290  /// The output operator MatrixLookupWeighted
291  ///
292  std::ostream& operator<< (std::ostream& s, const MatrixLookupWeighted&);
293
294}}} // of namespace classifier, yat, and theplu
295
296#endif
Note: See TracBrowser for help on using the repository browser.