source: trunk/c++_tools/classifier/MatrixLookupWeighted.h @ 624

Last change on this file since 624 was 624, checked in by Peter, 17 years ago

fixes #109 and #110

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