root/core/ave_deviation_meter_impl.hpp

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. estimate_average_deviation
  2. compute_average_deviation
  3. invariant
  4. ave_deviation
  5. ave_deviation
  6. ave_deviation
  7. ave_deviation
  8. update
  9. min
  10. arithmetic_mean
  11. max
  12. total
  13. deviation_percentage
  14. percent
  15. to_string
  16. to_csv

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/core/ave_deviation_meter_impl.hpp 2251 2018-02-20 23:30:57Z jmmcg $
   3 **
   4 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 
  21 namespace jmmcg {
  22 
  23         template<typename MeteredObjType>
  24         inline tostream & __fastcall
  25         operator<<(tostream &os, ave_deviation_meter<MeteredObjType> const &p) noexcept(false) {
  26                 os<<p.to_string();
  27                 return os;
  28         }
  29 
  30         template<class MeteredObjType, class Fn>
  31         inline ave_deviation_meter<MeteredObjType>
  32         estimate_average_deviation(typename ave_deviation_meter<MeteredObjType>::value_type const computations, Fn fn) {
  33                 ave_deviation_meter<MeteredObjType> meter(fn.operator()());
  34                 for (auto i=typename ave_deviation_meter<MeteredObjType>::value_type();i<computations;++i) {
  35                         meter.update(fn.operator()());
  36                 }
  37                 return meter;
  38         }
  39 
  40         template<class MeteredObjType, class Fn>
  41         inline std::pair<ave_deviation_meter<MeteredObjType>, bool>
  42         compute_average_deviation(double const target_deviation, typename ave_deviation_meter<MeteredObjType>::value_type const computations, Fn fn) {
  43                 std::pair<ave_deviation_meter<MeteredObjType>, bool> meter(ave_deviation_meter<MeteredObjType>(fn.operator()()), true);
  44                 for (auto i=typename ave_deviation_meter<MeteredObjType>::value_type();i<computations;++i) {
  45                         meter.first.update(fn.operator()());
  46                         if (meter.first.deviation_percentage()<=target_deviation) {
  47                                 meter.second=false;
  48                                 break;
  49                         }
  50                 }
  51                 return meter;
  52         }
  53 
  54         template<typename MeteredObjType> inline void
  55         ave_deviation_meter<MeteredObjType>::invariant() const noexcept(true) {
  56 #ifndef _REENTRANT
  57                 assert(num_samples_>0 || ((minimum_>=std::numeric_limits<value_type>::max()) && (maximum_<=std::numeric_limits<value_type>::min()) && num_samples_==0));
  58                 assert((minimum_>=std::numeric_limits<value_type>::max()) || (minimum_<=arithmetic_mean_));
  59                 assert((maximum_<=std::numeric_limits<value_type>::min()) || (arithmetic_mean_<=maximum_));
  60                 assert(maximum_<=total_);
  61 //                      assert(((minimum_>=std::numeric_limits<value_type>::max()) || (minimum_<=arithmetic_mean_)) || (fprintf(stdout, "minimum_=%lu, arithmetic_mean_=%g, num_samples_=%lu, cond=%i\n", minimum_, arithmetic_mean_, num_samples_, ((minimum_>=std::numeric_limits<value_type>::max()) || (minimum_<=arithmetic_mean_))) && 0));
  62 //                      assert(((maximum_<=std::numeric_limits<value_type>::min()) || (arithmetic_mean_<=maximum_)) || (fprintf(stdout, "maximum_=%lu\n", maximum_) && 0));
  63 //                      assert((maximum_<=total_) || (fprintf(stdout, "total_=%lu\n", total_) && 0));
  64 #endif
  65         }
  66 
  67         template<typename MeteredObjType> inline constexpr
  68         ave_deviation_meter<MeteredObjType>::ave_deviation_meter() noexcept(true)
  69         : num_samples_(),
  70                 minimum_(std::numeric_limits<value_type>::max()),
  71                 maximum_(std::numeric_limits<value_type>::min()),
  72                 total_(),
  73                 arithmetic_mean_(),
  74                 ave_deviation() {
  75         }
  76 
  77         template<typename MeteredObjType> inline constexpr
  78         ave_deviation_meter<MeteredObjType>::ave_deviation_meter(const value_type val) noexcept(true)
  79         : num_samples_(1),
  80                 minimum_(val), maximum_(val),
  81                 total_(val),
  82                 arithmetic_mean_(val),
  83                 ave_deviation(std::abs(arithmetic_mean_)) {
  84         }
  85 
  86         template<typename MeteredObjType> inline constexpr
  87         ave_deviation_meter<MeteredObjType>::ave_deviation_meter(value_type const val, double const dev) noexcept(true)
  88         : num_samples_(1),
  89                 minimum_(val), maximum_(val),
  90                 total_(val),
  91                 arithmetic_mean_(val),
  92                 ave_deviation(dev) {
  93         }
  94 
  95         template<typename MeteredObjType> inline
  96         ave_deviation_meter<MeteredObjType>::ave_deviation_meter(ave_deviation_meter const &rm) noexcept(true)
  97         : num_samples_(rm.num_samples_),
  98                 minimum_(rm.minimum_), maximum_(rm.maximum_),
  99                 total_(rm.total_),
 100                 arithmetic_mean_(rm.arithmetic_mean_),
 101                 ave_deviation(rm.ave_deviation) {
 102                 invariant();
 103         }
 104 
 105         template<typename MeteredObjType> inline
 106         ave_deviation_meter<MeteredObjType>::~ave_deviation_meter() noexcept(true) {
 107         }
 108 
 109         template<typename MeteredObjType> inline ave_deviation_meter<MeteredObjType> &
 110         ave_deviation_meter<MeteredObjType>::operator=(ave_deviation_meter const &rm) noexcept(true) {
 111                 num_samples_=rm.num_samples_;
 112                 minimum_=rm.minimum_;
 113                 maximum_=rm.maximum_;
 114                 total_=rm.total_;
 115                 arithmetic_mean_=rm.arithmetic_mean_;
 116                 ave_deviation=rm.ave_deviation;
 117                 invariant();
 118                 return *this;
 119         }
 120 
 121         template<typename MeteredObjType> inline ave_deviation_meter<MeteredObjType> &
 122         ave_deviation_meter<MeteredObjType>::operator+=(ave_deviation_meter const &rm) noexcept(true) {
 123                 invariant();
 124                 minimum_=std::min(minimum_, rm.minimum_);
 125                 maximum_=std::max(maximum_, rm.maximum_);
 126                 total_+=rm.total_;
 127                 if (num_samples_+rm.num_samples_) {
 128                         arithmetic_mean_=(arithmetic_mean_*num_samples_+rm.arithmetic_mean_*rm.num_samples_)/(num_samples_+rm.num_samples_);
 129                 }
 130                 ave_deviation=(ave_deviation+rm.ave_deviation)/2;
 131                 num_samples_+=rm.num_samples_;
 132                 minimum_=std::min(minimum_, rm.minimum_);
 133                 invariant();
 134                 return *this;
 135         }
 136 
 137         template<typename MeteredObjType> inline ave_deviation_meter<MeteredObjType> &
 138         ave_deviation_meter<MeteredObjType>::update(value_type const new_val) noexcept(true) {
 139                 invariant();
 140                 minimum_=std::min(minimum_, new_val);
 141                 maximum_=std::max(maximum_, new_val);
 142                 total_+=new_val;
 143                 unsigned long const old_num_samples_=num_samples_++;
 144                 value_type const old_arith_mean=arithmetic_mean_;
 145                 arithmetic_mean_=(arithmetic_mean_*old_num_samples_+new_val)/num_samples_;
 146                 if (old_num_samples_) {
 147                         ave_deviation=(std::abs(ave_deviation-(arithmetic_mean_-old_arith_mean))*old_num_samples_+std::abs(new_val-arithmetic_mean_))/num_samples_;
 148                 }
 149                 invariant();
 150                 return *this;
 151         }
 152 
 153         template<typename MeteredObjType> inline constexpr typename ave_deviation_meter<MeteredObjType>::value_type
 154         ave_deviation_meter<MeteredObjType>::min() const noexcept(true) {
 155                 return minimum_;
 156         }
 157 
 158         template<typename MeteredObjType> inline constexpr typename ave_deviation_meter<MeteredObjType>::value_type
 159         ave_deviation_meter<MeteredObjType>::arithmetic_mean() const noexcept(true) {
 160                 return static_cast<value_type>(arithmetic_mean_);
 161         }
 162 
 163         template<typename MeteredObjType> inline constexpr typename ave_deviation_meter<MeteredObjType>::value_type
 164         ave_deviation_meter<MeteredObjType>::max() const noexcept(true) {
 165                 return maximum_;
 166         }
 167 
 168         template<typename MeteredObjType> inline constexpr typename ave_deviation_meter<MeteredObjType>::value_type
 169         ave_deviation_meter<MeteredObjType>::total() const noexcept(true) {
 170                 return total_;
 171         }
 172 
 173         template<typename MeteredObjType> inline constexpr double
 174         ave_deviation_meter<MeteredObjType>::deviation() const noexcept(true) {
 175                 return ave_deviation;
 176         }
 177 
 178         template<typename MeteredObjType> inline constexpr double
 179         ave_deviation_meter<MeteredObjType>::deviation_percentage() const noexcept(true) {
 180                 return ave_deviation*100/arithmetic_mean_;
 181         }
 182 
 183         template<typename MeteredObjType> inline constexpr unsigned short
 184         ave_deviation_meter<MeteredObjType>::percent() const noexcept(true) {
 185                 return static_cast<unsigned short>(deviation_percentage());
 186         }
 187 
 188         template<typename MeteredObjType> inline tstring
 189         ave_deviation_meter<MeteredObjType>::to_string() const noexcept(false) {
 190                 invariant();
 191                 tostringstream os;
 192                 os
 193                         <<_T("[")<<minimum_<<_T(", ")<<value_type(arithmetic_mean_)<<_T(" ~(+/-")<<std::setprecision(2)<<value_type(deviation_percentage())<<_T("%), ")<<maximum_<<_T("]")
 194                         _T(", samples=")<<num_samples_
 195                         <<_T(", total=")<<total_;
 196                 return os.str();
 197         }
 198 
 199         template<typename MeteredObjType> inline tstring
 200         ave_deviation_meter<MeteredObjType>::to_csv() const noexcept(false) {
 201                 invariant();
 202                 tostringstream os;
 203                 os
 204                         <<_T("|")<<value_type(arithmetic_mean_)<<_T("|")<<std::setprecision(2)<<deviation_percentage()/100;
 205                 return os.str();
 206         }
 207 
 208 }

/* [<][>][^][v][top][bottom][index][help] */