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 | |
---|
11 | namespace theplu { |
---|
12 | namespace 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 MatrixLookupWeighted can be created directly from a matrix or from an |
---|
27 | /// other MatrixLookupWeighted. In the latter case, the resulting |
---|
28 | /// MatrixLookupWeighted is looking directly into the underlying matrix to |
---|
29 | /// avoid multiple lookups. |
---|
30 | /// |
---|
31 | /// There is a possibility to set the MatrixLookupWeighted as owner of the |
---|
32 | /// underlying matrix. In that case the underlying matrix will be |
---|
33 | /// destroyed in the destructor. Consequently, the underlying matrix |
---|
34 | /// must have been dynamically allocated and no other MatrixLookupWeighted |
---|
35 | /// can own the Kernel. |
---|
36 | /// |
---|
37 | /// @todo add on weight part |
---|
38 | class MatrixLookupWeighted : public DataLookup2D |
---|
39 | { |
---|
40 | |
---|
41 | public: |
---|
42 | |
---|
43 | |
---|
44 | /// |
---|
45 | /// Constructor creating a lookup into the entire @a matrix. |
---|
46 | /// |
---|
47 | /// @note If @a matrix goes out of scope or is deleted, the |
---|
48 | /// MatrixLookupWeighted becomes invalid and the result of further use is |
---|
49 | /// undefined. |
---|
50 | /// |
---|
51 | MatrixLookupWeighted(const gslapi::matrix& matrix, |
---|
52 | const gslapi::matrix& weights); |
---|
53 | |
---|
54 | /// |
---|
55 | /// Constructor creating a lookup into a sub-matrix of @a matrix. |
---|
56 | /// The @a row and @a column define what sub-matrix to look into, |
---|
57 | /// in other words, the created MatrixLookupWeighted will fullfill the |
---|
58 | /// following: \f$ MatrixLookupWeighted(i,j)=matrix(row[i],column[j]) |
---|
59 | /// \f$. This also means that number of rows in created |
---|
60 | /// MatrixLookupWeighted is equal to size of vector @a row, and number of |
---|
61 | /// columns is equal to size of vector @a column. |
---|
62 | /// |
---|
63 | /// @note If @a matrix goes out of scope or is deleted, the |
---|
64 | /// MatrixLookupWeighted becomes invalid and the result of further use is |
---|
65 | /// undefined. |
---|
66 | /// |
---|
67 | MatrixLookupWeighted(const gslapi::matrix& matrix, const gslapi::matrix& weights, |
---|
68 | const std::vector<size_t>& row, |
---|
69 | const std::vector<size_t>& column); |
---|
70 | |
---|
71 | /// |
---|
72 | /// Constructor creating a lookup into a sub-matrix of @matrix. |
---|
73 | /// |
---|
74 | /// If @a row_vectors is true the new MatrixLookupWeighted will be consist |
---|
75 | /// of the row vectors defined by @a index. This means that the |
---|
76 | /// created MatrixLookupWeighted will fullfill: |
---|
77 | /// \f$ MatrixLookupWeighted(i,j)=matrix(i,index[j]) \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 goes out of scope or is deleted, the |
---|
85 | /// MatrixLookupWeighted becomes invalid and the result of further use is |
---|
86 | /// 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 | /// |
---|
181 | /// The created MatrixLookupWeighted corresponds to all rows and the |
---|
182 | /// columns defined by @a index in the original MatrixLookupWeighted. The |
---|
183 | /// created MatrixLookupWeighted will fullfill: |
---|
184 | /// \f$ novel_ml(i,j)=original(i,index[j]) \f$. |
---|
185 | /// |
---|
186 | /// @return pointer to sub-Lookup of the MatrixLookupWeighted |
---|
187 | /// |
---|
188 | /// @note If underlying matrix goes out of scope or is deleted, the |
---|
189 | /// returned pointer becomes invalid and the result of further use is |
---|
190 | /// undefined. |
---|
191 | /// |
---|
192 | const MatrixLookupWeighted* training_data(const std::vector<size_t>& index) const; |
---|
193 | |
---|
194 | /// |
---|
195 | /// The created MatrixLookupWeighted corresponds to all rows and the |
---|
196 | /// columns defined by @a index in the original MatrixLookupWeighted. The |
---|
197 | /// created MatrixLookupWeighted will fullfill: |
---|
198 | /// \f$ novel_ml(i,j)=original(i,index[j]) \f$. |
---|
199 | /// |
---|
200 | /// @return pointer to sub-Lookup of the MatrixLookupWeighted |
---|
201 | /// |
---|
202 | /// @note If underlying matrix goes out of scope or is deleted, the |
---|
203 | /// returned pointer becomes invalid and the result of further use is |
---|
204 | /// undefined. |
---|
205 | /// |
---|
206 | const MatrixLookupWeighted* validation_data(const std::vector<size_t>&, |
---|
207 | const std::vector<size_t>&) const; |
---|
208 | /// |
---|
209 | /// Access operator |
---|
210 | /// |
---|
211 | /// @return weight * data for element \f$i j\f$ |
---|
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 | (*weights_)(row_index_[row], column_index_[column]); |
---|
219 | } |
---|
220 | |
---|
221 | /// |
---|
222 | /// @brief assigment operator |
---|
223 | /// |
---|
224 | /// Does only change MatrixLookupWeighted not the underlying matrix |
---|
225 | /// object. However if the MatrixLookupWeighted is owner of its underlying |
---|
226 | /// matrix, that matrix will be deleted here. |
---|
227 | /// |
---|
228 | const MatrixLookupWeighted& operator=(const MatrixLookupWeighted&); |
---|
229 | |
---|
230 | private: |
---|
231 | const gslapi::matrix* data_; |
---|
232 | const gslapi::matrix* weights_; |
---|
233 | }; |
---|
234 | |
---|
235 | /// |
---|
236 | /// The output operator MatrixLookupWeighted |
---|
237 | /// |
---|
238 | std::ostream& operator<< (std::ostream& s, const MatrixLookupWeighted&); |
---|
239 | |
---|
240 | }} // of namespace classifier and namespace theplu |
---|
241 | |
---|
242 | #endif |
---|