source: trunk/c++_tools/classifier/MatrixLookup.h @ 658

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

added function in KernelLookup? to create a KernelLookup? from inner data and outer (passed) data.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date ID
File size: 8.1 KB
Line 
1#ifndef _theplu_classifier_matrix_lookup_
2#define _theplu_classifier_matrix_lookup_
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  ///
17  /// MatrixLookups can be used to create lookups/views into matrices
18  /// in a more general way than the views supported in the matrix
19  /// class. The object does not contain any data, but only a pointer
20  /// to a matrix and row and columns incices defining which
21  /// sub-matrix to look into.  Each row and each column corresponds
22  /// to a row and a column in the Kernel, respectively. This design
23  /// allow for fast creation of sub-matrices, which is a common
24  /// operation in most traning/validation procedures.
25  ///
26  /// A MatrixLookup can be created directly from a matrix or from an
27  /// other MatrixLookup. In the latter case, the resulting
28  /// MatrixLookup is looking directly into the underlying matrix to
29  /// avoid multiple lookups.
30  ///
31  /// There is a possibility to set the MatrixLookup as owner of the
32  /// underlying matrix.
33  ///
34  class MatrixLookup : public DataLookup2D
35  {
36 
37  public:
38
39
40    ///
41    /// Constructor creating a lookup into the entire @a matrix.
42    /// @param own if true MatrixLookup owns its underlying @a matrix
43    ///
44    /// @note If @a matrix goes out of scope or is deleted, the
45    /// MatrixLookup becomes invalid and the result of further use is
46    /// undefined.
47    ///
48    MatrixLookup(const utility::matrix& matrix, const bool own=false);
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 MatrixLookup will fullfill the
54    /// following: \f$ MatrixLookup(i,j)=matrix(row[i],column[j])
55    /// \f$. This also means that number of rows in created
56    /// MatrixLookup is equal to size of vector @a row, and number of
57    /// columns is equal to size of vector @a column.
58    ///
59    /// @note If @a matrix goes out of scope or is deleted, the
60    /// MatrixLookup becomes invalid and the result of further use is
61    /// undefined.
62    ///
63    MatrixLookup(const utility::matrix& matrix, const std::vector<size_t>& row, 
64                 const std::vector<size_t>& column);
65
66    ///
67    /// Constructor creating a lookup into a sub-matrix of @a matrix.
68    ///
69    /// If @a row_vectors is true the new MatrixLookup will consist
70    /// of the row vectors defined by @a index. This means that the
71    /// created MatrixLookup will fullfill:
72    /// \f$ MatrixLookup(i,j)=matrix(i,index[j]) \f$
73    ///
74    /// If @a row_vectors is false the new MatrixLookup will be consist
75    /// of the rolumn vectors defined by @a index. This means that the
76    /// created MatrixLookup will fullfill:
77    /// \f$ MatrixLookup(i,j)=matrix(index[i],j) \f$
78    ///
79    /// @note If @a matrix goes out of scope or is deleted, the
80    /// MatrixLookup becomes invalid and the result of further use is
81    /// undefined.
82    ///
83    MatrixLookup(const utility::matrix& matrix, 
84                 const std::vector<size_t>& index, 
85                 const bool row_vectors);
86
87    ///
88    /// @brief Copy constructor.
89    ///
90    /// @note If underlying matrix goes out of scope or is deleted, the
91    /// MatrixLookup becomes invalid and the result of further use is
92    /// undefined.
93    ///
94    MatrixLookup(const MatrixLookup&);
95
96    ///
97    /// Creates a sub-MatrixLookup. The Lookup is independent of
98    /// MatrixLookup @a ml. The MatrixLookup is created to look
99    /// directly into the underlying matrix to avoid multiple lookups.
100    ///
101    /// The @a row and @a column define what sub-matrix to look into,
102    /// in other words, the created MatrixLookup will fullfill the
103    /// following: \f$ MatrixLookup(i,j)=ml(row[i],column[j]) \f$. This
104    /// also means that number of rows in created MatrixLookup is
105    /// equal to size of vector @a row, and number of columns is equal
106    /// to size of vector @a column.
107    ///
108    /// @note If underlying matrix goes out of scope or is deleted, the
109    /// MatrixLookup becomes invalid and the result of further use is
110    /// undefined.
111    ///
112    MatrixLookup(const MatrixLookup& ml, const std::vector<size_t>& row, 
113                 const std::vector<size_t>& column);
114
115    ///
116    /// Constructor creating a lookup into a sub-matrix of
117    /// @a ml. The MatrixLookup is created to look directly into the
118    /// underlying matrix to avoid multiple lookups.
119    ///
120    /// If @a row_vectors is true the new MatrixLookup will consist
121    /// of the row vectors defined by @a index. This means that the
122    /// created MatrixLookup will fullfill:
123    /// \f$ MatrixLookup(i,j)=ml(i,index[j])\f$
124    ///
125    /// If @a row_vectors is false the new MatrixLookup will consist
126    /// of the rolumn vectors defined by @a index. This means that the
127    /// created MatrixLookup will fullfill:
128    /// \f$ MatrixLookup(i,j) = ml(index[i],j) \f$
129    ///
130    /// @note If underlying matrix goes out of scope or is deleted, the
131    /// MatrixLookup becomes invalid and the result of further use is
132    /// undefined.
133    ///
134    MatrixLookup(const MatrixLookup& ml, const std::vector<size_t>&, 
135                 const bool row_vectors);
136
137    ///
138    /// Constructor creating a MatrixLookup with @a rows rows, @a
139    /// columns columns, and all values are set to @a value. Created
140    /// MatrixLookup owns its underlying matrix.
141    ///
142    MatrixLookup(const size_t rows, const size_t columns, const double value=0);
143
144    ///
145    /// @brief The istream constructor.
146    ///
147    /// In construction the underlying matrix is created from
148    /// stream. The MatrixLookup will be owner of the underlying
149    /// matrix.
150    ///
151    /// @see matrix(istream&) for details.
152    ///
153    MatrixLookup(std::istream&, char sep='\0');
154
155    ///
156    /// Destructor.
157    ///
158    virtual ~MatrixLookup();
159
160    /**
161       the new MatrixLookup will consist of the rolumn vectors
162       defined by @a index. This means that the returned MatrixLookup
163       will fullfill: \f$ returned(i,j) = original(index[i],j) \f$
164
165       @note If underlying matrix goes out of scope or is deleted, the
166       returned pointer becomes invalid and the result of further use is
167       undefined.
168   
169    */
170    const MatrixLookup* selected(const std::vector<size_t>&) const;
171
172    ///
173    /// The created MatrixLookup corresponds to all rows and the
174    /// columns defined by @a index in the original MatrixLookup. The
175    /// created MatrixLookup will fullfill:
176    /// \f$ novel_ml(i,j)=original(i,index[j]) \f$.
177    ///
178    /// @return pointer to sub-Lookup of the MatrixLookup
179    ///
180    /// @note If underlying matrix goes out of scope or is deleted, the
181    /// returned pointer becomes invalid and the result of further use is
182    /// undefined.
183    ///
184    /// @Note Returns a dynamically allocated DataLookup2D, which has
185    /// to be deleted by the caller to avoid memory leaks.
186    ///
187    const MatrixLookup* training_data(const std::vector<size_t>& index) const;
188   
189    ///
190    /// The created MatrixLookup corresponds to all rows and the
191    /// columns defined by @a index in the original MatrixLookup. The
192    /// created MatrixLookup will fullfill:
193    /// \f$ novel_ml(i,j)=original(i,index[j]) \f$.
194    ///
195    /// @return pointer to sub-Lookup of the MatrixLookup
196    ///
197    /// @note If underlying matrix goes out of scope or is deleted, the
198    /// returned pointer becomes invalid and the result of further use is
199    /// undefined.
200    ///
201    const MatrixLookup* validation_data(const std::vector<size_t>&,
202                                        const std::vector<size_t>&) const;
203    ///
204    /// @return false
205    ///
206    bool weighted(void) const;
207
208    ///
209    /// Access operator
210    ///
211    /// @return element
212    ///
213    inline double operator()(const size_t row, const size_t column) const
214    { 
215      assert(row<rows());
216      assert(column<columns());
217      return (*data_)(row_index_[row], column_index_[column]); 
218    }
219
220    ///
221    /// @brief assigment operator
222    ///
223    /// Does only change MatrixLookup not the underlying matrix
224    /// object. However if the MatrixLookup is owner of its underlying
225    /// matrix, that matrix will be deleted here.
226    ///
227    const MatrixLookup& operator=(const MatrixLookup&);
228   
229  private:
230    const utility::matrix* data_;
231  }; 
232 
233  ///
234  /// The output operator MatrixLookup
235  ///
236  std::ostream& operator<< (std::ostream& s, const MatrixLookup&);
237
238}} // of namespace classifier and namespace theplu
239
240#endif
Note: See TracBrowser for help on using the repository browser.