Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 #ifndef __ALGORITHMS_LOCALIZATION_DISTANCE_BASED_MATH_STATISTIC_H 00020 #define __ALGORITHMS_LOCALIZATION_DISTANCE_BASED_MATH_STATISTIC_H 00021 00022 #include "util/pstl/list_static.h" 00023 #include "util/pstl/algorithm.h" 00024 #include <math.h> 00025 #include <float.h> 00026 #include <limits.h> 00027 00028 00029 namespace wiselib 00030 { 00031 00033 00036 template<typename OsModel_P, 00037 int MAX_ENTRIES = 20> 00038 class LocalizationStatistic 00039 { 00040 00041 public: 00042 typedef OsModel_P OsModel; 00043 00044 typedef list_static<OsModel, double, MAX_ENTRIES> List; 00045 typedef typename List::iterator ListIterator; 00046 00047 typedef LocalizationStatistic<OsModel, MAX_ENTRIES> self_type; 00048 00052 LocalizationStatistic(); 00053 LocalizationStatistic( const self_type& ); 00054 ~LocalizationStatistic(); 00056 00057 00060 00063 void add( double ); 00067 LocalizationStatistic& operator+=( double ); 00069 00070 00073 00079 double mean( void ); 00086 double variance( void ); 00093 double std_dev( void ); 00096 double min( void ); 00099 double max( void ); 00101 00102 00105 00107 size_t size( void ); 00110 List as_list( void ); 00112 void clear( void ); 00113 00114 private: 00115 00116 List values_; 00117 00118 }; 00119 // ----------------------------------------------------------------------- 00120 // ----------------------------------------------------------------------- 00121 // ----------------------------------------------------------------------- 00122 template<typename OsModel_P, int MAX_ENTRIES> 00123 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00124 LocalizationStatistic() 00125 {} 00126 // ---------------------------------------------------------------------- 00127 template<typename OsModel_P, int MAX_ENTRIES> 00128 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00129 LocalizationStatistic( const self_type& ls ) 00130 : values_( ls.as_list() ) 00131 {} 00132 // ---------------------------------------------------------------------- 00133 template<typename OsModel_P, int MAX_ENTRIES> 00134 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00135 ~LocalizationStatistic() 00136 {} 00137 // ---------------------------------------------------------------------- 00138 template<typename OsModel_P, int MAX_ENTRIES> 00139 void 00140 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00141 add( double value ) 00142 { 00143 values_.push_back( value ); 00144 } 00145 // ---------------------------------------------------------------------- 00146 template<typename OsModel_P, int MAX_ENTRIES> 00147 typename LocalizationStatistic<OsModel_P, MAX_ENTRIES>::self_type& 00148 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00149 operator+=( double value ) 00150 { 00151 this->add( value ); 00152 00153 return *this; 00154 } 00155 // ---------------------------------------------------------------------- 00156 template<typename OsModel_P, int MAX_ENTRIES> 00157 double 00158 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00159 mean( void ) 00160 { 00161 if ( values_.size() == 0 ) return 0; 00162 00163 double sum = 0; 00164 00165 for ( ListIterator 00166 it = values_.begin(); 00167 it != values_.end(); 00168 ++it ) 00169 sum += *it; 00170 00171 return ( sum / values_.size() ); 00172 } 00173 // ---------------------------------------------------------------------- 00174 template<typename OsModel_P, int MAX_ENTRIES> 00175 double 00176 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00177 variance( void ) 00178 { 00179 if ( values_.size() == 0 ) return 0; 00180 00181 double var = 0; 00182 double avg = mean(); 00183 00184 for ( ListIterator 00185 it = values_.begin(); 00186 it != values_.end(); 00187 ++it ) 00188 { 00189 double tmp = *it - avg; 00190 var += tmp * tmp; 00191 } 00192 00193 return ( var / values_.size() ); 00194 } 00195 // ---------------------------------------------------------------------- 00196 template<typename OsModel_P, int MAX_ENTRIES> 00197 double 00198 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00199 std_dev( void ) 00200 { 00201 return sqrt( variance() ); 00202 } 00203 // ---------------------------------------------------------------------- 00204 template<typename OsModel_P, int MAX_ENTRIES> 00205 double 00206 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00207 min( void ) 00208 { 00209 if ( size() == 0 ) 00210 return DBL_MAX; 00211 00212 return *min_element( values_.begin(), values_.end() ); 00213 } 00214 // ---------------------------------------------------------------------- 00215 template<typename OsModel_P, int MAX_ENTRIES> 00216 double 00217 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00218 max( void ) 00219 { 00220 if ( size() == 0 ) 00221 return DBL_MIN; 00222 00223 return *max_element( values_.begin(), values_.end() ); 00224 } 00225 // ---------------------------------------------------------------------- 00226 template<typename OsModel_P, int MAX_ENTRIES> 00227 size_t 00228 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00229 size( void ) 00230 { 00231 return values_.size(); 00232 } 00233 // ---------------------------------------------------------------------- 00234 template<typename OsModel_P, int MAX_ENTRIES> 00235 typename LocalizationStatistic<OsModel_P, MAX_ENTRIES>::List 00236 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00237 as_list( void ) 00238 { 00239 return values_; 00240 } 00241 // ---------------------------------------------------------------------- 00242 template<typename OsModel_P, int MAX_ENTRIES> 00243 void 00244 LocalizationStatistic<OsModel_P, MAX_ENTRIES>:: 00245 clear( void ) 00246 { 00247 values_.clear(); 00248 } 00249 00250 }// namespace wiselib 00251 #endif