Debian Codegraph @

third-party/boost-math/include/boost/math/tools/stats.hpp

raw
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 <cstdint>
14 #include <cmath>
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:
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 {}
29 void add(const T& val)
30 {
31 if(val < m_min)
32 m_min = val;
33 if(val > m_max)
34 m_max = val;
35 m_total += val;
36 ++m_count;
37 m_squared_total += val*val;
38 }
39 T min BOOST_MATH_PREVENT_MACRO_SUBSTITUTION()const{ return m_min; }
40 T max BOOST_MATH_PREVENT_MACRO_SUBSTITUTION()const{ return m_max; }
41 T total()const{ return m_total; }
42 T mean()const{ return m_total / static_cast<T>(m_count); }
43 std::uintmax_t count()const{ return m_count; }
44 T variance()const
45 {
46 BOOST_MATH_STD_USING
47
48 T t = m_squared_total - m_total * m_total / m_count;
49 t /= m_count;
50 return t;
51 }
52 T variance1()const
53 {
54 BOOST_MATH_STD_USING
55
56 T t = m_squared_total - m_total * m_total / m_count;
57 t /= (m_count-1);
58 return t;
59 }
60 T rms()const
61 {
62 BOOST_MATH_STD_USING
63
64 return sqrt(m_squared_total / static_cast<T>(m_count));
65 }
66 stats& operator+=(const stats& s)
67 {
68 if(s.m_min < m_min)
69 m_min = s.m_min;
70 if(s.m_max > m_max)
71 m_max = s.m_max;
72 m_total += s.m_total;
73 m_squared_total += s.m_squared_total;
74 m_count += s.m_count;
75 return *this;
76 }
77 private:
78 T m_min, m_max, m_total, m_squared_total;
79 std::uintmax_t m_count{0};
80 };
81
82 } // namespace tools
83 } // namespace math
84 } // namespace boost
85
86 #endif
87