Mercurial > hg > segmenter-vamp-plugin
comparison armadillo-3.900.4/include/armadillo_bits/op_mean_meat.hpp @ 49:1ec0e2823891
Switch to using subrepo copies of qm-dsp, nnls-chroma, vamp-plugin-sdk; update Armadillo version; assume build without external BLAS/LAPACK
author | Chris Cannam |
---|---|
date | Thu, 13 Jun 2013 10:25:24 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
48:69251e11a913 | 49:1ec0e2823891 |
---|---|
1 // Copyright (C) 2009-2012 NICTA (www.nicta.com.au) | |
2 // Copyright (C) 2009-2012 Conrad Sanderson | |
3 // | |
4 // This Source Code Form is subject to the terms of the Mozilla Public | |
5 // License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 // file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 | |
8 | |
9 //! \addtogroup op_mean | |
10 //! @{ | |
11 | |
12 | |
13 | |
14 //! \brief | |
15 //! For each row or for each column, find the mean value. | |
16 //! The result is stored in a dense matrix that has either one column or one row. | |
17 //! The dimension, for which the means are found, is set via the mean() function. | |
18 template<typename T1> | |
19 inline | |
20 void | |
21 op_mean::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_mean>& in) | |
22 { | |
23 arma_extra_debug_sigprint(); | |
24 | |
25 typedef typename T1::elem_type eT; | |
26 | |
27 const unwrap_check<T1> tmp(in.m, out); | |
28 const Mat<eT>& X = tmp.M; | |
29 | |
30 const uword dim = in.aux_uword_a; | |
31 arma_debug_check( (dim > 1), "mean(): incorrect usage. dim must be 0 or 1"); | |
32 | |
33 const uword X_n_rows = X.n_rows; | |
34 const uword X_n_cols = X.n_cols; | |
35 | |
36 if(dim == 0) | |
37 { | |
38 arma_extra_debug_print("op_mean::apply(), dim = 0"); | |
39 | |
40 out.set_size( (X_n_rows > 0) ? 1 : 0, X_n_cols ); | |
41 | |
42 if(X_n_rows > 0) | |
43 { | |
44 eT* out_mem = out.memptr(); | |
45 | |
46 for(uword col=0; col < X_n_cols; ++col) | |
47 { | |
48 out_mem[col] = op_mean::direct_mean( X.colptr(col), X_n_rows ); | |
49 } | |
50 } | |
51 } | |
52 else | |
53 if(dim == 1) | |
54 { | |
55 arma_extra_debug_print("op_mean::apply(), dim = 1"); | |
56 | |
57 out.set_size(X_n_rows, (X_n_cols > 0) ? 1 : 0); | |
58 | |
59 if(X_n_cols > 0) | |
60 { | |
61 eT* out_mem = out.memptr(); | |
62 | |
63 for(uword row=0; row < X_n_rows; ++row) | |
64 { | |
65 out_mem[row] = op_mean::direct_mean( X, row ); | |
66 } | |
67 } | |
68 } | |
69 } | |
70 | |
71 | |
72 | |
73 template<typename eT> | |
74 arma_pure | |
75 inline | |
76 eT | |
77 op_mean::direct_mean(const eT* const X, const uword n_elem) | |
78 { | |
79 arma_extra_debug_sigprint(); | |
80 | |
81 typedef typename get_pod_type<eT>::result T; | |
82 | |
83 const eT result = arrayops::accumulate(X, n_elem) / T(n_elem); | |
84 | |
85 return arma_isfinite(result) ? result : op_mean::direct_mean_robust(X, n_elem); | |
86 } | |
87 | |
88 | |
89 | |
90 template<typename eT> | |
91 arma_pure | |
92 inline | |
93 eT | |
94 op_mean::direct_mean_robust(const eT* const X, const uword n_elem) | |
95 { | |
96 arma_extra_debug_sigprint(); | |
97 | |
98 // use an adapted form of the mean finding algorithm from the running_stat class | |
99 | |
100 typedef typename get_pod_type<eT>::result T; | |
101 | |
102 uword i,j; | |
103 | |
104 eT r_mean = eT(0); | |
105 | |
106 for(i=0, j=1; j<n_elem; i+=2, j+=2) | |
107 { | |
108 const eT Xi = X[i]; | |
109 const eT Xj = X[j]; | |
110 | |
111 r_mean = r_mean + (Xi - r_mean)/T(j); // we need i+1, and j is equivalent to i+1 here | |
112 r_mean = r_mean + (Xj - r_mean)/T(j+1); | |
113 } | |
114 | |
115 | |
116 if(i < n_elem) | |
117 { | |
118 const eT Xi = X[i]; | |
119 | |
120 r_mean = r_mean + (Xi - r_mean)/T(i+1); | |
121 } | |
122 | |
123 return r_mean; | |
124 } | |
125 | |
126 | |
127 | |
128 template<typename eT> | |
129 inline | |
130 eT | |
131 op_mean::direct_mean(const Mat<eT>& X, const uword row) | |
132 { | |
133 arma_extra_debug_sigprint(); | |
134 | |
135 typedef typename get_pod_type<eT>::result T; | |
136 | |
137 const uword X_n_cols = X.n_cols; | |
138 | |
139 eT val = eT(0); | |
140 | |
141 uword i,j; | |
142 for(i=0, j=1; j < X_n_cols; i+=2, j+=2) | |
143 { | |
144 val += X.at(row,i); | |
145 val += X.at(row,j); | |
146 } | |
147 | |
148 if(i < X_n_cols) | |
149 { | |
150 val += X.at(row,i); | |
151 } | |
152 | |
153 const eT result = val / T(X_n_cols); | |
154 | |
155 return arma_isfinite(result) ? result : op_mean::direct_mean_robust(X, row); | |
156 } | |
157 | |
158 | |
159 | |
160 template<typename eT> | |
161 inline | |
162 eT | |
163 op_mean::direct_mean_robust(const Mat<eT>& X, const uword row) | |
164 { | |
165 arma_extra_debug_sigprint(); | |
166 | |
167 typedef typename get_pod_type<eT>::result T; | |
168 | |
169 const uword X_n_cols = X.n_cols; | |
170 | |
171 eT r_mean = eT(0); | |
172 | |
173 for(uword col=0; col < X_n_cols; ++col) | |
174 { | |
175 r_mean = r_mean + (X.at(row,col) - r_mean)/T(col+1); | |
176 } | |
177 | |
178 return r_mean; | |
179 } | |
180 | |
181 | |
182 | |
183 template<typename eT> | |
184 inline | |
185 eT | |
186 op_mean::mean_all(const subview<eT>& X) | |
187 { | |
188 arma_extra_debug_sigprint(); | |
189 | |
190 typedef typename get_pod_type<eT>::result T; | |
191 | |
192 const uword X_n_rows = X.n_rows; | |
193 const uword X_n_cols = X.n_cols; | |
194 const uword X_n_elem = X.n_elem; | |
195 | |
196 arma_debug_check( (X_n_elem == 0), "mean(): given object has no elements" ); | |
197 | |
198 eT val = eT(0); | |
199 | |
200 if(X_n_rows == 1) | |
201 { | |
202 const Mat<eT>& A = X.m; | |
203 | |
204 const uword start_row = X.aux_row1; | |
205 const uword start_col = X.aux_col1; | |
206 | |
207 const uword end_col_p1 = start_col + X_n_cols; | |
208 | |
209 uword i,j; | |
210 for(i=start_col, j=start_col+1; j < end_col_p1; i+=2, j+=2) | |
211 { | |
212 val += A.at(start_row, i); | |
213 val += A.at(start_row, j); | |
214 } | |
215 | |
216 if(i < end_col_p1) | |
217 { | |
218 val += A.at(start_row, i); | |
219 } | |
220 } | |
221 else | |
222 { | |
223 for(uword col=0; col < X_n_cols; ++col) | |
224 { | |
225 val += arrayops::accumulate(X.colptr(col), X_n_rows); | |
226 } | |
227 } | |
228 | |
229 const eT result = val / T(X_n_elem); | |
230 | |
231 return arma_isfinite(result) ? result : op_mean::mean_all_robust(X); | |
232 } | |
233 | |
234 | |
235 | |
236 template<typename eT> | |
237 inline | |
238 eT | |
239 op_mean::mean_all_robust(const subview<eT>& X) | |
240 { | |
241 arma_extra_debug_sigprint(); | |
242 | |
243 typedef typename get_pod_type<eT>::result T; | |
244 | |
245 const uword X_n_rows = X.n_rows; | |
246 const uword X_n_cols = X.n_cols; | |
247 | |
248 const uword start_row = X.aux_row1; | |
249 const uword start_col = X.aux_col1; | |
250 | |
251 const uword end_row_p1 = start_row + X_n_rows; | |
252 const uword end_col_p1 = start_col + X_n_cols; | |
253 | |
254 const Mat<eT>& A = X.m; | |
255 | |
256 | |
257 eT r_mean = eT(0); | |
258 | |
259 if(X_n_rows == 1) | |
260 { | |
261 uword i=0; | |
262 | |
263 for(uword col = start_col; col < end_col_p1; ++col, ++i) | |
264 { | |
265 r_mean = r_mean + (A.at(start_row,col) - r_mean)/T(i+1); | |
266 } | |
267 } | |
268 else | |
269 { | |
270 uword i=0; | |
271 | |
272 for(uword col = start_col; col < end_col_p1; ++col) | |
273 for(uword row = start_row; row < end_row_p1; ++row, ++i) | |
274 { | |
275 r_mean = r_mean + (A.at(row,col) - r_mean)/T(i+1); | |
276 } | |
277 } | |
278 | |
279 return r_mean; | |
280 } | |
281 | |
282 | |
283 | |
284 template<typename eT> | |
285 inline | |
286 eT | |
287 op_mean::mean_all(const diagview<eT>& X) | |
288 { | |
289 arma_extra_debug_sigprint(); | |
290 | |
291 typedef typename get_pod_type<eT>::result T; | |
292 | |
293 const uword X_n_elem = X.n_elem; | |
294 | |
295 arma_debug_check( (X_n_elem == 0), "mean(): given object has no elements" ); | |
296 | |
297 eT val = eT(0); | |
298 | |
299 for(uword i=0; i<X_n_elem; ++i) | |
300 { | |
301 val += X[i]; | |
302 } | |
303 | |
304 const eT result = val / T(X_n_elem); | |
305 | |
306 return arma_isfinite(result) ? result : op_mean::mean_all_robust(X); | |
307 } | |
308 | |
309 | |
310 | |
311 template<typename eT> | |
312 inline | |
313 eT | |
314 op_mean::mean_all_robust(const diagview<eT>& X) | |
315 { | |
316 arma_extra_debug_sigprint(); | |
317 | |
318 typedef typename get_pod_type<eT>::result T; | |
319 | |
320 const uword X_n_elem = X.n_elem; | |
321 | |
322 eT r_mean = eT(0); | |
323 | |
324 for(uword i=0; i<X_n_elem; ++i) | |
325 { | |
326 r_mean = r_mean + (X[i] - r_mean)/T(i+1); | |
327 } | |
328 | |
329 return r_mean; | |
330 } | |
331 | |
332 | |
333 | |
334 template<typename T1> | |
335 inline | |
336 typename T1::elem_type | |
337 op_mean::mean_all(const Base<typename T1::elem_type, T1>& X) | |
338 { | |
339 arma_extra_debug_sigprint(); | |
340 | |
341 typedef typename T1::elem_type eT; | |
342 | |
343 const unwrap<T1> tmp(X.get_ref()); | |
344 const Mat<eT>& A = tmp.M; | |
345 | |
346 const uword A_n_elem = A.n_elem; | |
347 | |
348 arma_debug_check( (A_n_elem == 0), "mean(): given object has no elements" ); | |
349 | |
350 return op_mean::direct_mean(A.memptr(), A_n_elem); | |
351 } | |
352 | |
353 | |
354 | |
355 template<typename eT> | |
356 arma_inline | |
357 eT | |
358 op_mean::robust_mean(const eT A, const eT B) | |
359 { | |
360 return A + (B - A)/eT(2); | |
361 } | |
362 | |
363 | |
364 | |
365 template<typename T> | |
366 arma_inline | |
367 std::complex<T> | |
368 op_mean::robust_mean(const std::complex<T>& A, const std::complex<T>& B) | |
369 { | |
370 return A + (B - A)/T(2); | |
371 } | |
372 | |
373 | |
374 | |
375 //! @} | |
376 |