• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  (C) Copyright John Maddock 2005-2006.
2 //  Use, modification and distribution are subject to the
3 //  Boost Software License, Version 1.0. (See accompanying file
4 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef BOOST_MATH_TOOLS_STATS_INCLUDED
7 #define BOOST_MATH_TOOLS_STATS_INCLUDED
8 
9 #ifdef _MSC_VER
10 #pragma once
11 #endif
12 
13 #include <boost/config/no_tr1/cmath.hpp>
14 #include <boost/cstdint.hpp>
15 #include <boost/math/tools/precision.hpp>
16 
17 namespace boost{ namespace math{ namespace tools{
18 
19 template <class T>
20 class stats
21 {
22 public:
stats()23    stats()
24       : m_min(tools::max_value<T>()),
25         m_max(-tools::max_value<T>()),
26         m_total(0),
27         m_squared_total(0),
28         m_count(0)
29    {}
add(const T & val)30    void add(const T& val)
31    {
32       if(val < m_min)
33          m_min = val;
34       if(val > m_max)
35          m_max = val;
36       m_total += val;
37       ++m_count;
38       m_squared_total += val*val;
39    }
BOOST_PREVENT_MACRO_SUBSTITUTION() const40    T min BOOST_PREVENT_MACRO_SUBSTITUTION()const{ return m_min; }
BOOST_PREVENT_MACRO_SUBSTITUTION() const41    T max BOOST_PREVENT_MACRO_SUBSTITUTION()const{ return m_max; }
total() const42    T total()const{ return m_total; }
mean() const43    T mean()const{ return m_total / static_cast<T>(m_count); }
count() const44    boost::uintmax_t count()const{ return m_count; }
variance() const45    T variance()const
46    {
47       BOOST_MATH_STD_USING
48 
49       T t = m_squared_total - m_total * m_total / m_count;
50       t /= m_count;
51       return t;
52    }
variance1() const53    T variance1()const
54    {
55       BOOST_MATH_STD_USING
56 
57       T t = m_squared_total - m_total * m_total / m_count;
58       t /= (m_count-1);
59       return t;
60    }
rms() const61    T rms()const
62    {
63       BOOST_MATH_STD_USING
64 
65       return sqrt(m_squared_total / static_cast<T>(m_count));
66    }
operator +=(const stats & s)67    stats& operator+=(const stats& s)
68    {
69       if(s.m_min < m_min)
70          m_min = s.m_min;
71       if(s.m_max > m_max)
72          m_max = s.m_max;
73       m_total += s.m_total;
74       m_squared_total += s.m_squared_total;
75       m_count += s.m_count;
76       return *this;
77    }
78 private:
79    T m_min, m_max, m_total, m_squared_total;
80    boost::uintmax_t m_count;
81 };
82 
83 } // namespace tools
84 } // namespace math
85 } // namespace boost
86 
87 #endif
88 
89