$search
00001 // Copyright (C) 2009-2011 NICTA (www.nicta.com.au) 00002 // Copyright (C) 2009-2011 Conrad Sanderson 00003 // 00004 // This file is part of the Armadillo C++ library. 00005 // It is provided without any warranty of fitness 00006 // for any purpose. You can redistribute this file 00007 // and/or modify it under the terms of the GNU 00008 // Lesser General Public License (LGPL) as published 00009 // by the Free Software Foundation, either version 3 00010 // of the License or (at your option) any later version. 00011 // (see http://www.opensource.org/licenses for more info) 00012 00013 00016 00017 00018 00019 template<typename eT> 00020 inline 00021 arma_counter<eT>::~arma_counter() 00022 { 00023 arma_extra_debug_sigprint_this(this); 00024 } 00025 00026 00027 00028 template<typename eT> 00029 inline 00030 arma_counter<eT>::arma_counter() 00031 : d_count( eT(0)) 00032 , i_count(uword(0)) 00033 { 00034 arma_extra_debug_sigprint_this(this); 00035 } 00036 00037 00038 00039 template<typename eT> 00040 inline 00041 const arma_counter<eT>& 00042 arma_counter<eT>::operator++() 00043 { 00044 if(i_count < ARMA_MAX_UWORD) 00045 { 00046 i_count++; 00047 } 00048 else 00049 { 00050 d_count += eT(ARMA_MAX_UWORD); 00051 i_count = 0; 00052 } 00053 00054 return *this; 00055 } 00056 00057 00058 00059 template<typename eT> 00060 inline 00061 void 00062 arma_counter<eT>::operator++(int) 00063 { 00064 operator++(); 00065 } 00066 00067 00068 00069 template<typename eT> 00070 inline 00071 void 00072 arma_counter<eT>::reset() 00073 { 00074 d_count = eT(0); 00075 i_count = uword(0); 00076 } 00077 00078 00079 00080 template<typename eT> 00081 inline 00082 eT 00083 arma_counter<eT>::value() const 00084 { 00085 return d_count + eT(i_count); 00086 } 00087 00088 00089 00090 template<typename eT> 00091 inline 00092 eT 00093 arma_counter<eT>::value_plus_1() const 00094 { 00095 if(i_count < ARMA_MAX_UWORD) 00096 { 00097 return d_count + eT(i_count + 1); 00098 } 00099 else 00100 { 00101 return d_count + eT(ARMA_MAX_UWORD) + eT(1); 00102 } 00103 } 00104 00105 00106 00107 template<typename eT> 00108 inline 00109 eT 00110 arma_counter<eT>::value_minus_1() const 00111 { 00112 if(i_count > 0) 00113 { 00114 return d_count + eT(i_count - 1); 00115 } 00116 else 00117 { 00118 return d_count - eT(1); 00119 } 00120 } 00121 00122 00123 00124 // 00125 00126 00127 00128 template<typename eT> 00129 running_stat<eT>::~running_stat() 00130 { 00131 arma_extra_debug_sigprint_this(this); 00132 } 00133 00134 00135 00136 template<typename eT> 00137 running_stat<eT>::running_stat() 00138 : r_mean ( eT(0)) 00139 , r_var (typename running_stat<eT>::T(0)) 00140 , min_val ( eT(0)) 00141 , max_val ( eT(0)) 00142 , min_val_norm(typename running_stat<eT>::T(0)) 00143 , max_val_norm(typename running_stat<eT>::T(0)) 00144 { 00145 arma_extra_debug_sigprint_this(this); 00146 } 00147 00148 00149 00151 template<typename eT> 00152 inline 00153 void 00154 running_stat<eT>::operator() (const typename running_stat<eT>::T sample) 00155 { 00156 arma_extra_debug_sigprint(); 00157 00158 if( arma_isfinite(sample) == false ) 00159 { 00160 arma_warn(true, "running_stat: sample ignored as it is non-finite" ); 00161 return; 00162 } 00163 00164 running_stat_aux::update_stats(*this, sample); 00165 } 00166 00167 00168 00170 template<typename eT> 00171 inline 00172 void 00173 running_stat<eT>::operator() (const std::complex< typename running_stat<eT>::T >& sample) 00174 { 00175 arma_extra_debug_sigprint(); 00176 00177 arma_type_check(( is_same_type<eT, std::complex< typename running_stat<eT>::T > >::value == false )); 00178 00179 if( arma_isfinite(sample) == false ) 00180 { 00181 arma_warn(true, "running_stat: sample ignored as it is non-finite" ); 00182 return; 00183 } 00184 00185 running_stat_aux::update_stats(*this, sample); 00186 } 00187 00188 00189 00191 template<typename eT> 00192 inline 00193 void 00194 running_stat<eT>::reset() 00195 { 00196 arma_extra_debug_sigprint(); 00197 00198 typedef typename running_stat<eT>::T T; 00199 00200 counter.reset(); 00201 00202 r_mean = eT(0); 00203 r_var = T(0); 00204 00205 min_val = eT(0); 00206 max_val = eT(0); 00207 00208 min_val_norm = T(0); 00209 max_val_norm = T(0); 00210 } 00211 00212 00213 00215 template<typename eT> 00216 inline 00217 eT 00218 running_stat<eT>::mean() const 00219 { 00220 arma_extra_debug_sigprint(); 00221 00222 return r_mean; 00223 } 00224 00225 00226 00228 template<typename eT> 00229 inline 00230 typename running_stat<eT>::T 00231 running_stat<eT>::var(const uword norm_type) const 00232 { 00233 arma_extra_debug_sigprint(); 00234 00235 const T N = counter.value(); 00236 00237 if(N > T(1)) 00238 { 00239 if(norm_type == 0) 00240 { 00241 return r_var; 00242 } 00243 else 00244 { 00245 const T N_minus_1 = counter.value_minus_1(); 00246 return (N_minus_1/N) * r_var; 00247 } 00248 } 00249 else 00250 { 00251 return T(0); 00252 } 00253 } 00254 00255 00256 00258 template<typename eT> 00259 inline 00260 typename running_stat<eT>::T 00261 running_stat<eT>::stddev(const uword norm_type) const 00262 { 00263 arma_extra_debug_sigprint(); 00264 00265 return std::sqrt( (*this).var(norm_type) ); 00266 } 00267 00268 00269 00271 template<typename eT> 00272 inline 00273 eT 00274 running_stat<eT>::min() const 00275 { 00276 arma_extra_debug_sigprint(); 00277 00278 return min_val; 00279 } 00280 00281 00282 00284 template<typename eT> 00285 inline 00286 eT 00287 running_stat<eT>::max() const 00288 { 00289 arma_extra_debug_sigprint(); 00290 00291 return max_val; 00292 } 00293 00294 00295 00297 template<typename eT> 00298 inline 00299 typename get_pod_type<eT>::result 00300 running_stat<eT>::count() const 00301 { 00302 arma_extra_debug_sigprint(); 00303 00304 return counter.value(); 00305 } 00306 00307 00308 00310 template<typename eT> 00311 inline 00312 void 00313 running_stat_aux::update_stats(running_stat<eT>& x, const eT sample) 00314 { 00315 arma_extra_debug_sigprint(); 00316 00317 typedef typename running_stat<eT>::T T; 00318 00319 const T N = x.counter.value(); 00320 00321 if(N > T(0)) 00322 { 00323 if(sample < x.min_val) 00324 { 00325 x.min_val = sample; 00326 } 00327 00328 if(sample > x.max_val) 00329 { 00330 x.max_val = sample; 00331 } 00332 00333 const T N_plus_1 = x.counter.value_plus_1(); 00334 const T N_minus_1 = x.counter.value_minus_1(); 00335 00336 // note: variance has to be updated before the mean 00337 00338 const eT tmp = sample - x.r_mean; 00339 00340 x.r_var = N_minus_1/N * x.r_var + (tmp*tmp)/N_plus_1; 00341 00342 x.r_mean = x.r_mean + (sample - x.r_mean)/N_plus_1; 00343 //x.r_mean = (N/N_plus_1)*x.r_mean + sample/N_plus_1; 00344 //x.r_mean = (x.r_mean + sample/N) * N/N_plus_1; 00345 } 00346 else 00347 { 00348 x.r_mean = sample; 00349 x.min_val = sample; 00350 x.max_val = sample; 00351 00352 // r_var is initialised to zero 00353 // in the constructor and reset() 00354 } 00355 00356 x.counter++; 00357 } 00358 00359 00360 00362 template<typename T> 00363 inline 00364 void 00365 running_stat_aux::update_stats(running_stat< std::complex<T> >& x, const T sample) 00366 { 00367 arma_extra_debug_sigprint(); 00368 00369 running_stat_aux::update_stats(x, std::complex<T>(sample)); 00370 } 00371 00372 00373 00375 template<typename T> 00376 inline 00377 void 00378 running_stat_aux::update_stats(running_stat< std::complex<T> >& x, const std::complex<T>& sample) 00379 { 00380 arma_extra_debug_sigprint(); 00381 00382 typedef typename std::complex<T> eT; 00383 00384 const T sample_norm = std::norm(sample); 00385 const T N = x.counter.value(); 00386 00387 if(N > T(0)) 00388 { 00389 if(sample_norm < x.min_val_norm) 00390 { 00391 x.min_val_norm = sample_norm; 00392 x.min_val = sample; 00393 } 00394 00395 if(sample_norm > x.max_val_norm) 00396 { 00397 x.max_val_norm = sample_norm; 00398 x.max_val = sample; 00399 } 00400 00401 const T N_plus_1 = x.counter.value_plus_1(); 00402 const T N_minus_1 = x.counter.value_minus_1(); 00403 00404 x.r_var = N_minus_1/N * x.r_var + std::norm(sample - x.r_mean)/N_plus_1; 00405 00406 x.r_mean = x.r_mean + (sample - x.r_mean)/N_plus_1; 00407 //x.r_mean = (N/N_plus_1)*x.r_mean + sample/N_plus_1; 00408 //x.r_mean = (x.r_mean + sample/N) * N/N_plus_1; 00409 } 00410 else 00411 { 00412 x.r_mean = sample; 00413 x.min_val = sample; 00414 x.max_val = sample; 00415 x.min_val_norm = sample_norm; 00416 x.max_val_norm = sample_norm; 00417 00418 // r_var is initialised to zero 00419 // in the constructor and reset() 00420 } 00421 00422 x.counter++; 00423 } 00424 00425 00426