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 | |
---|
11 | namespace theplu { |
---|
12 | namespace 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 | class MatrixLookupWeighted : public DataLookup2D |
---|
33 | { |
---|
34 | |
---|
35 | public: |
---|
36 | |
---|
37 | |
---|
38 | /// |
---|
39 | /// Constructor creating a lookup into the entire @a matrix. |
---|
40 | /// |
---|
41 | /// @note If @a matrix or @a weights goes out of scope or is |
---|
42 | /// deleted, the MatrixLookupWeighted becomes invalid and the |
---|
43 | /// result of further use is undefined. |
---|
44 | /// |
---|
45 | MatrixLookupWeighted(const utility::matrix& matrix, |
---|
46 | const utility::matrix& weights, |
---|
47 | const bool owner=false); |
---|
48 | |
---|
49 | |
---|
50 | /// |
---|
51 | /// Constructor creating a lookup into the entire @a matrix. A |
---|
52 | /// weight matrix is constructed with weights 1 for values and 0 |
---|
53 | /// for nan's in @a matrix. @note If @a matrix goes out of scope or |
---|
54 | /// is deleted, the MatrixLookupWeighted becomes invalid and the |
---|
55 | /// result of further use is undefined. |
---|
56 | /// |
---|
57 | MatrixLookupWeighted(const utility::matrix& matrix); |
---|
58 | |
---|
59 | |
---|
60 | /// |
---|
61 | /// Constructor creating a lookup into a sub-matrix of @a matrix. |
---|
62 | /// The @a row and @a column define what sub-matrix to look into, |
---|
63 | /// in other words, the created MatrixLookupWeighted will fullfill |
---|
64 | /// the following: \f$ |
---|
65 | /// MatrixLookupWeighted(i,j)=matrix(row[i],column[j]) |
---|
66 | /// weights(row[i],column[j])\f$. This also means that number of |
---|
67 | /// rows in created MatrixLookupWeighted is equal to size of |
---|
68 | /// vector @a row, and number of columns is equal to size of |
---|
69 | /// vector @a column. |
---|
70 | /// |
---|
71 | /// @note If @a matrix or @a weights goes out of scope or is deleted, the |
---|
72 | /// MatrixLookupWeighted becomes invalid and the result of further use is |
---|
73 | /// undefined. |
---|
74 | /// |
---|
75 | MatrixLookupWeighted(const utility::matrix& matrix, |
---|
76 | const utility::matrix& weights, |
---|
77 | const std::vector<size_t>& row, |
---|
78 | const std::vector<size_t>& column); |
---|
79 | |
---|
80 | /// |
---|
81 | /// Constructor creating a lookup into a sub-matrix of @a matrix. |
---|
82 | /// |
---|
83 | /// If @a row_vectors is true the new MatrixLookupWeighted will be |
---|
84 | /// consist of the row vectors defined by @a index. This means |
---|
85 | /// that the created MatrixLookupWeighted will fullfill: \f$ |
---|
86 | /// MatrixLookupWeighted(i,j)=matrix(i,index[j])*weights(i,index[j]) |
---|
87 | /// \f$ |
---|
88 | /// |
---|
89 | /// If @a row_vectors is false the new MatrixLookupWeighted will be consist |
---|
90 | /// of the rolumn vectors defined by @a index. This means that the |
---|
91 | /// created MatrixLookupWeighted will fullfill: |
---|
92 | /// \f$ MatrixLookupWeighted(i,j)=matrix(index[i],j) \f$ |
---|
93 | /// |
---|
94 | /// @note If @a matrix or @a weights goes out of scope or is |
---|
95 | /// deleted, the MatrixLookupWeighted becomes invalid and the |
---|
96 | /// result of further use is undefined. |
---|
97 | /// |
---|
98 | MatrixLookupWeighted(const utility::matrix& matrix, |
---|
99 | const utility::matrix& weights, |
---|
100 | const std::vector<size_t>& index, |
---|
101 | const bool row_vectors); |
---|
102 | |
---|
103 | /// |
---|
104 | /// @brief |
---|
105 | /// |
---|
106 | /// @note If underlying matrix goes out of scope or is deleted, the |
---|
107 | /// MatrixLookupWeighted becomes invalid and the result of further use is |
---|
108 | /// undefined. |
---|
109 | /// |
---|
110 | //MatrixLookupWeighted(const MatrixLookup&, const MatrixLookup); |
---|
111 | |
---|
112 | /// |
---|
113 | /// @brief Copy constructor. |
---|
114 | /// |
---|
115 | /// @note If underlying matrix goes out of scope or is deleted, the |
---|
116 | /// MatrixLookupWeighted becomes invalid and the result of further use is |
---|
117 | /// undefined. |
---|
118 | /// |
---|
119 | MatrixLookupWeighted(const MatrixLookupWeighted&); |
---|
120 | |
---|
121 | /// |
---|
122 | /// Creates a sub-MatrixLookupWeighted. The Lookup is independent of |
---|
123 | /// MatrixLookupWeighted @a ml. The MatrixLookupWeighted is created to look |
---|
124 | /// directly into the underlying matrix to avoid multiple lookups. |
---|
125 | /// |
---|
126 | /// The @a row and @a column define what sub-matrix to look into, |
---|
127 | /// in other words, the created MatrixLookupWeighted will fullfill the |
---|
128 | /// following: \f$ MatrixLookupWeighted(i,j)=ml(row[i],column[j]) \f$. This |
---|
129 | /// also means that number of rows in created MatrixLookupWeighted is |
---|
130 | /// equal to size of vector @a row, and number of columns is equal |
---|
131 | /// to size of vector @a column. |
---|
132 | /// |
---|
133 | /// @note If underlying matrix goes out of scope or is deleted, the |
---|
134 | /// MatrixLookupWeighted becomes invalid and the result of further use is |
---|
135 | /// undefined. |
---|
136 | /// |
---|
137 | MatrixLookupWeighted(const MatrixLookupWeighted& ml, |
---|
138 | const std::vector<size_t>& row, |
---|
139 | const std::vector<size_t>& column); |
---|
140 | |
---|
141 | /// |
---|
142 | /// Constructor creating a lookup into a sub-matrix of |
---|
143 | /// @a ml. The MatrixLookupWeighted is created to look directly into the |
---|
144 | /// underlying matrix to avoid multiple lookups. |
---|
145 | /// |
---|
146 | /// If @a row_vectors is true the new MatrixLookupWeighted will be consist |
---|
147 | /// of the row vectors defined by @a index. This means that the |
---|
148 | /// created MatrixLookupWeighted will fullfill: |
---|
149 | /// \f$ MatrixLookupWeighted(i,j)=ml(i,index[j])\f$ |
---|
150 | /// |
---|
151 | /// If @a row_vectors is false the new MatrixLookupWeighted will be consist |
---|
152 | /// of the rolumn vectors defined by @a index. This means that the |
---|
153 | /// created MatrixLookupWeighted will fullfill: |
---|
154 | /// \f$ MatrixLookupWeighted(i,j) = ml(index[i],j) \f$ |
---|
155 | /// |
---|
156 | /// @note If underlying matrix goes out of scope or is deleted, the |
---|
157 | /// MatrixLookupWeighted becomes invalid and the result of further use is |
---|
158 | /// undefined. |
---|
159 | /// |
---|
160 | MatrixLookupWeighted(const MatrixLookupWeighted& ml, |
---|
161 | const std::vector<size_t>&, |
---|
162 | const bool row_vectors); |
---|
163 | |
---|
164 | /// |
---|
165 | /// Constructor creating a MatrixLookupWeighted with @a rows rows, @a |
---|
166 | /// columns columns, and all values are set to @a value. Created |
---|
167 | /// MatrixLookupWeighted owns its underlying matrix. |
---|
168 | /// |
---|
169 | MatrixLookupWeighted(const size_t rows, const size_t columns, |
---|
170 | const double value=0, const double weight=1); |
---|
171 | |
---|
172 | /// |
---|
173 | /// @brief The istream constructor. |
---|
174 | /// |
---|
175 | /// In construction the underlying matrix is created from |
---|
176 | /// stream. The MatrixLookupWeighted will be owner of the underlying |
---|
177 | /// matrix. |
---|
178 | /// |
---|
179 | /// @see matrix(istream&) for details. |
---|
180 | /// |
---|
181 | MatrixLookupWeighted(std::istream&, char sep='\0'); |
---|
182 | |
---|
183 | /// |
---|
184 | /// Destructor. If MatrixLookup is owner of underlying matrix, the |
---|
185 | /// matrix is destroyed. |
---|
186 | /// |
---|
187 | virtual ~MatrixLookupWeighted(); |
---|
188 | |
---|
189 | /// |
---|
190 | /// @return data value of element (@a row, @a column) |
---|
191 | /// |
---|
192 | inline double data(size_t row, size_t column) const |
---|
193 | { return (*data_)(row_index_[row], column_index_[column]); } |
---|
194 | |
---|
195 | /// |
---|
196 | /// @todo doc |
---|
197 | /// |
---|
198 | const MatrixLookupWeighted* selected(const std::vector<size_t>& i) 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* |
---|
213 | training_data(const std::vector<size_t>& index) const; |
---|
214 | |
---|
215 | /// |
---|
216 | /// The created MatrixLookupWeighted corresponds to all rows and the |
---|
217 | /// columns defined by @a index in the original MatrixLookupWeighted. The |
---|
218 | /// created MatrixLookupWeighted will fullfill: |
---|
219 | /// \f$ novel_ml(i,j)=original(i,index[j]) \f$. |
---|
220 | /// |
---|
221 | /// @return pointer to sub-Lookup of the MatrixLookupWeighted |
---|
222 | /// |
---|
223 | /// @note If underlying matrix goes out of scope or is deleted, the |
---|
224 | /// returned pointer becomes invalid and the result of further use is |
---|
225 | /// undefined. |
---|
226 | /// |
---|
227 | const MatrixLookupWeighted* validation_data(const std::vector<size_t>&, |
---|
228 | const std::vector<size_t>&) const; |
---|
229 | |
---|
230 | /// |
---|
231 | /// @return weight value of element (@a row, @a column) |
---|
232 | /// |
---|
233 | inline double weight(size_t row, size_t column) const |
---|
234 | { return (*weights_)(row_index_[row], column_index_[column]); } |
---|
235 | |
---|
236 | /// |
---|
237 | /// @return true |
---|
238 | /// |
---|
239 | bool weighted(void) const; |
---|
240 | |
---|
241 | /// |
---|
242 | /// Access operator |
---|
243 | /// |
---|
244 | /// @return weight * data for element \f$ i j\f$ |
---|
245 | /// |
---|
246 | inline double operator()(const size_t row, const size_t column) const |
---|
247 | { |
---|
248 | return (weight(row,column) ? data(row,column)*weight(row,column) : 0); |
---|
249 | } |
---|
250 | |
---|
251 | /// |
---|
252 | /// @brief assigment operator |
---|
253 | /// |
---|
254 | /// Does only change MatrixLookupWeighted not the underlying matrix |
---|
255 | /// object. However if the MatrixLookupWeighted is owner of its underlying |
---|
256 | /// matrix, that matrix will be deleted here. |
---|
257 | /// |
---|
258 | const MatrixLookupWeighted& operator=(const MatrixLookupWeighted&); |
---|
259 | |
---|
260 | private: |
---|
261 | const utility::matrix* data_; |
---|
262 | const utility::matrix* weights_; |
---|
263 | u_int* ref_count_weights_; |
---|
264 | }; |
---|
265 | |
---|
266 | /// |
---|
267 | /// The output operator MatrixLookupWeighted |
---|
268 | /// |
---|
269 | std::ostream& operator<< (std::ostream& s, const MatrixLookupWeighted&); |
---|
270 | |
---|
271 | }} // of namespace classifier and namespace theplu |
---|
272 | |
---|
273 | #endif |
---|