comparison armadillo-2.4.4/include/armadillo_bits/op_mean_meat.hpp @ 0:8b6102e2a9b0

Armadillo Library
author maxzanoni76 <max.zanoni@eecs.qmul.ac.uk>
date Wed, 11 Apr 2012 09:27:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:8b6102e2a9b0
1 // Copyright (C) 2009-2011 NICTA (www.nicta.com.au)
2 // Copyright (C) 2009-2011 Conrad Sanderson
3 //
4 // This file is part of the Armadillo C++ library.
5 // It is provided without any warranty of fitness
6 // for any purpose. You can redistribute this file
7 // and/or modify it under the terms of the GNU
8 // Lesser General Public License (LGPL) as published
9 // by the Free Software Foundation, either version 3
10 // of the License or (at your option) any later version.
11 // (see http://www.opensource.org/licenses for more info)
12
13
14 //! \addtogroup op_mean
15 //! @{
16
17
18
19 template<typename eT>
20 arma_pure
21 inline
22 eT
23 op_mean::direct_mean(const eT* const X, const uword n_elem)
24 {
25 arma_extra_debug_sigprint();
26
27 typedef typename get_pod_type<eT>::result T;
28
29 const eT result = arrayops::accumulate(X, n_elem) / T(n_elem);
30
31 return arma_isfinite(result) ? result : op_mean::direct_mean_robust(X, n_elem);
32 }
33
34
35
36 template<typename eT>
37 inline
38 eT
39 op_mean::direct_mean(const Mat<eT>& X, const uword row)
40 {
41 arma_extra_debug_sigprint();
42
43 typedef typename get_pod_type<eT>::result T;
44
45 const uword X_n_cols = X.n_cols;
46
47 eT val = eT(0);
48
49 for(uword col=0; col<X_n_cols; ++col)
50 {
51 val += X.at(row,col);
52 }
53
54 const eT result = val / T(X_n_cols);
55
56 return arma_isfinite(result) ? result : direct_mean_robust(X, row);
57 }
58
59
60
61 template<typename eT>
62 inline
63 eT
64 op_mean::direct_mean(const subview<eT>& X)
65 {
66 arma_extra_debug_sigprint();
67
68 typedef typename get_pod_type<eT>::result T;
69
70 const uword X_n_elem = X.n_elem;
71
72 eT val = eT(0);
73
74 for(uword i=0; i<X_n_elem; ++i)
75 {
76 val += X[i];
77 }
78
79 const eT result = val / T(X_n_elem);
80
81 return arma_isfinite(result) ? result : direct_mean_robust(X);
82 }
83
84
85
86 template<typename eT>
87 inline
88 eT
89 op_mean::direct_mean(const diagview<eT>& X)
90 {
91 arma_extra_debug_sigprint();
92
93 typedef typename get_pod_type<eT>::result T;
94
95 const uword X_n_elem = X.n_elem;
96
97 eT val = eT(0);
98
99 for(uword i=0; i<X_n_elem; ++i)
100 {
101 val += X[i];
102 }
103
104 const eT result = val / T(X_n_elem);
105
106 return arma_isfinite(result) ? result : direct_mean_robust(X);
107 }
108
109
110
111 //! \brief
112 //! For each row or for each column, find the mean value.
113 //! The result is stored in a dense matrix that has either one column or one row.
114 //! The dimension, for which the means are found, is set via the mean() function.
115 template<typename T1>
116 inline
117 void
118 op_mean::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_mean>& in)
119 {
120 arma_extra_debug_sigprint();
121
122 typedef typename T1::elem_type eT;
123 typedef typename get_pod_type<eT>::result T;
124
125 const unwrap_check<T1> tmp(in.m, out);
126 const Mat<eT>& X = tmp.M;
127
128 const uword dim = in.aux_uword_a;
129 arma_debug_check( (dim > 1), "mean(): incorrect usage. dim must be 0 or 1");
130
131 const uword X_n_rows = X.n_rows;
132 const uword X_n_cols = X.n_cols;
133
134 if(dim == 0)
135 {
136 arma_extra_debug_print("op_mean::apply(), dim = 0");
137
138 out.set_size( (X_n_rows > 0) ? 1 : 0, X_n_cols );
139
140 if(X_n_rows > 0)
141 {
142 eT* out_mem = out.memptr();
143
144 for(uword col=0; col<X_n_cols; ++col)
145 {
146 out_mem[col] = op_mean::direct_mean( X.colptr(col), X_n_rows );
147 }
148 }
149 }
150 else
151 if(dim == 1)
152 {
153 arma_extra_debug_print("op_mean::apply(), dim = 1");
154
155 out.set_size(X_n_rows, (X_n_cols > 0) ? 1 : 0);
156
157 if(X_n_cols > 0)
158 {
159 eT* out_mem = out.memptr();
160
161 for(uword row=0; row<X_n_rows; ++row)
162 {
163 out_mem[row] = op_mean::direct_mean( X, row );
164 }
165 }
166 }
167 }
168
169
170
171 template<typename eT>
172 arma_pure
173 inline
174 eT
175 op_mean::direct_mean_robust(const eT* const X, const uword n_elem)
176 {
177 arma_extra_debug_sigprint();
178
179 // use an adapted form of the mean finding algorithm from the running_stat class
180
181 typedef typename get_pod_type<eT>::result T;
182
183 uword i,j;
184
185 eT r_mean = eT(0);
186
187 for(i=0, j=1; j<n_elem; i+=2, j+=2)
188 {
189 const eT Xi = X[i];
190 const eT Xj = X[j];
191
192 r_mean = r_mean + (Xi - r_mean)/T(j); // we need i+1, and j is equivalent to i+1 here
193 r_mean = r_mean + (Xj - r_mean)/T(j+1);
194 }
195
196
197 if(i < n_elem)
198 {
199 const eT Xi = X[i];
200
201 r_mean = r_mean + (Xi - r_mean)/T(i+1);
202 }
203
204 return r_mean;
205 }
206
207
208
209 template<typename eT>
210 inline
211 eT
212 op_mean::direct_mean_robust(const Mat<eT>& X, const uword row)
213 {
214 arma_extra_debug_sigprint();
215
216 typedef typename get_pod_type<eT>::result T;
217
218 const uword X_n_cols = X.n_cols;
219
220 eT r_mean = eT(0);
221
222 for(uword col=0; col<X_n_cols; ++col)
223 {
224 r_mean = r_mean + (X.at(row,col) - r_mean)/T(col+1);
225 }
226
227 return r_mean;
228 }
229
230
231
232 template<typename eT>
233 inline
234 eT
235 op_mean::direct_mean_robust(const subview<eT>& X)
236 {
237 arma_extra_debug_sigprint();
238
239 typedef typename get_pod_type<eT>::result T;
240
241 const uword X_n_elem = X.n_elem;
242
243 eT r_mean = eT(0);
244
245 for(uword i=0; i<X_n_elem; ++i)
246 {
247 r_mean = r_mean + (X[i] - r_mean)/T(i+1);
248 }
249
250 return r_mean;
251 }
252
253
254
255 template<typename eT>
256 inline
257 eT
258 op_mean::direct_mean_robust(const diagview<eT>& X)
259 {
260 arma_extra_debug_sigprint();
261
262 typedef typename get_pod_type<eT>::result T;
263
264 const uword X_n_elem = X.n_elem;
265
266 eT r_mean = eT(0);
267
268 for(uword i=0; i<X_n_elem; ++i)
269 {
270 r_mean = r_mean + (X[i] - r_mean)/T(i+1);
271 }
272
273 return r_mean;
274 }
275
276
277
278 //! @}
279