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

Last change on this file since 604 was 604, checked in by Peter, 15 years ago

ref #96 Changed InputRanker? to return vector of index rather than element. Also added draft to FeatureSelection? class.

File size: 8.7 KB
Line 
1// $Id$
2
3#ifndef _theplu_classifier_matrix_lookup_weighted_
4#define _theplu_classifier_matrix_lookup_weighted_
5
6#include <c++_tools/classifier/DataLookup2D.h>
7#include <c++_tools/gslapi/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 gslapi::matrix& matrix, 
48                         const gslapi::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 gslapi::matrix& matrix, 
66                         const gslapi::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 gslapi::matrix& matrix, 
89                         const gslapi::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);
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    /// @todo doc
181    ///   
182    const MatrixLookupWeighted* 
183    selected(const std::vector<size_t>& index) const;
184
185    ///
186    /// The created MatrixLookupWeighted corresponds to all rows and the
187    /// columns defined by @a index in the original MatrixLookupWeighted. The
188    /// created MatrixLookupWeighted will fullfill:
189    /// \f$ novel_ml(i,j)=original(i,index[j]) \f$.
190    ///
191    /// @return pointer to sub-Lookup of the MatrixLookupWeighted
192    ///
193    /// @note If underlying matrix goes out of scope or is deleted, the
194    /// returned pointer becomes invalid and the result of further use is
195    /// undefined.
196    ///
197    const MatrixLookupWeighted* 
198    training_data(const std::vector<size_t>& index) const;
199   
200    ///
201    /// The created MatrixLookupWeighted corresponds to all rows and the
202    /// columns defined by @a index in the original MatrixLookupWeighted. The
203    /// created MatrixLookupWeighted will fullfill:
204    /// \f$ novel_ml(i,j)=original(i,index[j]) \f$.
205    ///
206    /// @return pointer to sub-Lookup of the MatrixLookupWeighted
207    ///
208    /// @note If underlying matrix goes out of scope or is deleted, the
209    /// returned pointer becomes invalid and the result of further use is
210    /// undefined.
211    ///
212    const MatrixLookupWeighted* validation_data(const std::vector<size_t>&,
213                                        const std::vector<size_t>&) const;
214
215    ///
216    /// Access operator
217    ///
218    /// @return weight * data for element \f$ i j\f$
219    ///
220    inline double operator()(const size_t row, const size_t column) const
221    { 
222      assert(row<rows());
223      assert(column<columns());
224      return (*data_)(row_index_[row], column_index_[column])*
225        (*weights_)(row_index_[row], column_index_[column]); 
226    }
227
228    ///
229    /// @brief assigment operator
230    ///
231    /// Does only change MatrixLookupWeighted not the underlying matrix
232    /// object. However if the MatrixLookupWeighted is owner of its underlying
233    /// matrix, that matrix will be deleted here.
234    ///
235    const MatrixLookupWeighted& operator=(const MatrixLookupWeighted&);
236   
237  private:
238    const gslapi::matrix* data_;
239    const gslapi::matrix* weights_;
240  }; 
241 
242  ///
243  /// The output operator MatrixLookupWeighted
244  ///
245  std::ostream& operator<< (std::ostream& s, const MatrixLookupWeighted&);
246
247}} // of namespace classifier and namespace theplu
248
249#endif
Note: See TracBrowser for help on using the repository browser.