Changeset 95


Ignore:
Timestamp:
Jun 9, 2004, 6:01:43 PM (18 years ago)
Author:
Peter
Message:

some functions added to WeightedAverager?

Location:
trunk/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Averager.cc

    r87 r95  
    1616  }
    1717
    18   Averager::Averager(long n,double x,double xx)
     18  Averager::Averager(double x)
     19    : n_(1), x_(x), xx_(x*x)
     20  {
     21  }
     22
     23  Averager::Averager(double x,double xx,long n)
    1924    : n_(n), x_(x), xx_(xx)
    2025  {
  • trunk/src/Averager.h

    r87 r95  
    88namespace theplu{
    99namespace cpptools{
    10 
     10  class ostream;
    1111  ///
    1212  /// Modified version of the Averager class written by Patrik Eden,
    1313  /// Dept. Theoretical Physic, Lund University, Sweden
    1414  ///
    15 
    16   class ostream;
    1715  class Averager
    1816  {
     
    2523   
    2624    ///
    27     /// Constructor taking number of samples, first moment(mean) and
    28     /// second moment.
     25    /// Constructor taking the first value.
    2926    ///
    30     Averager(long, double, double);
     27    Averager(double);
     28   
     29    ///
     30    /// Constructor taking sum of x, sum of squared x and number of
     31    /// samples.
     32    ///
     33    Averager(double, double, long);
    3134   
    3235    ///
  • trunk/src/WeightedAverager.cc

    r94 r95  
    1313
    1414  WeightedAverager::WeightedAverager(void)
    15     : w_(Averager()), wx_(Averager()), wwx_(Averager())
     15    : w_(Averager()), wx_(Averager()), wwx_(0)
     16  {
     17  }
     18
     19  WeightedAverager::WeightedAverager(const double d, const double w)
     20    : w_(Averager(w,w*w,1)), wx_(Averager(w*d,w*w*d*d,1)), wwx_(w*w*d)
     21  {
     22  }
     23
     24  WeightedAverager::WeightedAverager(const WeightedAverager& a)
     25    : w_(Averager(a.sum_w(), a.sum_ww(),1)),
     26      wx_(Averager(a.sum_wx(),a.sum_wwxx(),1)), wwx_(a.sum_wwx())
    1627  {
    1728  }
  • trunk/src/WeightedAverager.h

    r94 r95  
    1010namespace cpptools{
    1111  ///
    12   /// Averager class for weights.
     12  /// Averager class with weights.
    1313  ///
    1414  class WeightedAverager
     
    2020    ///
    2121    WeightedAverager(void);
    22    
     22
     23    ///
     24    /// Constructor taking the data point, i.e. the value and its
     25    /// weight (default = 1)
     26    ///
     27    WeightedAverager(const double, const double=1);
     28
     29    ///
     30    /// Copy constructor
     31    ///
     32    WeightedAverager(const WeightedAverager&);
     33
    2334    ///
    2435    /// adding a data point d, with weight w (default is 1)
    2536    ///
    2637    inline void add(const double d,const double w=1)
    27     {w_.add(w); wx_.add(w*d); wwx_.add(w*w*d);}
     38    {w_.add(w); wx_.add(w*d); wwx_+=w*w*d;}
    2839
    2940    ///
    30     /// @return average
     41    /// Calculating the average according to: \f$ \frac{\sum
     42    /// w_ix_i}{\sum w_i}\f$ @return average
    3143    ///
    3244    inline double average(void) const {return mean();}
    3345
    3446    ///
    35     /// @return average
     47    ///Calculating the mean according to: \f$ \frac{\sum
     48    ///w_ix_i}{\sum w_i} \f$ @return average
    3649    ///
    3750    inline double mean(void) const { return w_.sum_x() ?
     
    3952 
    4053    ///
    41     /// @return squarred error
     54    /// Calculating the squared error according to: \f$ \frac{\sum
     55    /// w_i^2(x_i-m)^2}{(\sum w_i)^2} \f$ @return squared error
    4256    ///
    43     inline double squarred_error(void)  const
    44     { return (wx_.sum_xsqr()-2*mean()*wwx_.sum_x()+
     57    inline double squared_error(void) const
     58    { return (wx_.sum_xsqr()-2*mean()*wwx_+
    4559              mean()*mean()*w_.sum_xsqr()) / (w_.sum_x()*w_.sum_x()); }
     60
     61    ///
     62    /// Calculating the sum of weights: \f$ \sum
     63    /// w_i \f$ @return sum of weights
     64    ///
     65    inline double sum_w(void) const
     66    { return w_.sum_x(); }
     67
     68    ///
     69    /// \f$ \sum w_i^2 \f$ @return sum of squared weights
     70    ///
     71    inline double sum_ww(void)  const
     72    { return w_.sum_xsqr(); }
     73   
     74    ///
     75    /// \f$ \sum w_ix_i \f$ @return weighted sum of x
     76    ///
     77    inline double sum_wx(void)  const
     78    { return w_.sum_xsqr(); }
     79   
     80    ///
     81    ///  @return \f$ \sum w_i^2x_i^2 \f$
     82    ///
     83    inline double sum_wwxx(void)  const
     84    { return wx_.sum_xsqr(); }
     85   
     86    ///
     87    ///  @return \f$ \sum w_i^2x_i \f$
     88    ///
     89    inline double sum_wwx(void) const
     90    { return wwx_; }
     91
     92    ///
     93    /// operator to add a double (with weight 1) to the object
     94    ///
     95    inline WeightedAverager operator+=(double d)
     96    { add(d); return *this; }
     97
     98    ///
     99    /// operator to add a WeightedAverager
     100    ///
     101    inline WeightedAverager operator+=(WeightedAverager a)
     102    { wx_+=a.wx(); w_+=a.w(); return *this; }
     103
    46104
    47105  private:
    48106    Averager w_;
    49107    Averager wx_;
    50     Averager wwx_;
     108    double wwx_;
    51109   
     110    inline Averager wx(void) const {return wx_;}
     111    inline Averager w(void) const {return w_;}
     112
    52113
    53114  };
Note: See TracChangeset for help on using the changeset viewer.