source: trunk/yat/statistics/Averager.h @ 2817

Last change on this file since 2817 was 2817, checked in by Peter, 10 years ago

update copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.7 KB
Line 
1#ifndef _theplu_yat_statistics_averager_
2#define _theplu_yat_statistics_averager_
3
4// $Id: Averager.h 2817 2012-08-29 00:38:17Z peter $
5
6/*
7  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
9  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
10  Copyright (C) 2009, 2010, 2011, 2012 Peter Johansson
11
12  This file is part of the yat library, http://dev.thep.lu.se/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 3 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with yat. If not, see <http://www.gnu.org/licenses/>.
26*/
27
28#include "averager_base.h"
29
30#include "yat/utility/iterator_traits.h"
31
32#include <boost/concept_check.hpp>
33
34#include <cmath>
35
36namespace theplu{
37namespace yat{
38namespace statistics{
39
40  ///
41  /// @brief Class to calculate simple (first and second moments) averages.
42  ///
43  /// @see AveragerWeighted AveragerPair AveragerPairWeighted
44  ///
45  class Averager : public averager_base2<Averager>
46  {
47  public:
48
49    ///
50    /// Default constructor
51    ///
52    Averager(void);
53
54    ///
55    /// Constructor taking sum of \a x, sum of squared x, \a xx, and
56    /// number of samples \a n.
57    ///
58    Averager(double x, double xx, long n);
59
60    ///
61    /// Copy constructor
62    ///
63    Averager(const Averager& a);
64
65    ///
66    /// @brief The assignment operator
67    ///
68    const Averager& operator=(const Averager&);
69
70    /**
71       \brief plus assignment operator
72
73       Add another Averager
74     */
75    template<class Derived>
76    const Averager& operator+=(const averager_base2<Derived>& other);
77
78  private:
79    friend class averager_base<Averager>;
80    void add_impl(double, long int);
81    void rescale_impl(double);
82  };
83
84
85  /**
86     \brief adding a range of values to Averager \a a
87
88     \relates Averager
89   */
90  template <typename InputIterator>
91  void add(Averager& a, InputIterator first, InputIterator last)
92  {
93    BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
94    utility::check_iterator_is_unweighted(first);
95    for ( ; first != last; ++first)
96      a.add(*first);
97  }
98
99  // template implementation
100  template<class Derived>
101  const Averager& Averager::operator+=(const averager_base2<Derived>& other)
102  {
103    if (other.n())
104      add2(other.mean(), other.sum_xx_centered(), other.n());
105    return *this;
106  }
107
108
109}}} // of namespace statistics, yat, and theplu
110
111#endif
Note: See TracBrowser for help on using the repository browser.