File indexing completed on 2025-01-18 09:40:42
0001
0002
0003
0004
0005
0006 #ifndef BOOST_MATH_TOOLS_STATS_INCLUDED
0007 #define BOOST_MATH_TOOLS_STATS_INCLUDED
0008
0009 #ifdef _MSC_VER
0010 #pragma once
0011 #endif
0012
0013 #include <cstdint>
0014 #include <cmath>
0015 #include <boost/math/tools/precision.hpp>
0016
0017 namespace boost{ namespace math{ namespace tools{
0018
0019 template <class T>
0020 class stats
0021 {
0022 public:
0023 stats()
0024 : m_min(tools::max_value<T>()),
0025 m_max(-tools::max_value<T>()),
0026 m_total(0),
0027 m_squared_total(0)
0028 {}
0029 void add(const T& val)
0030 {
0031 if(val < m_min)
0032 m_min = val;
0033 if(val > m_max)
0034 m_max = val;
0035 m_total += val;
0036 ++m_count;
0037 m_squared_total += val*val;
0038 }
0039 T min BOOST_PREVENT_MACRO_SUBSTITUTION()const{ return m_min; }
0040 T max BOOST_PREVENT_MACRO_SUBSTITUTION()const{ return m_max; }
0041 T total()const{ return m_total; }
0042 T mean()const{ return m_total / static_cast<T>(m_count); }
0043 std::uintmax_t count()const{ return m_count; }
0044 T variance()const
0045 {
0046 BOOST_MATH_STD_USING
0047
0048 T t = m_squared_total - m_total * m_total / m_count;
0049 t /= m_count;
0050 return t;
0051 }
0052 T variance1()const
0053 {
0054 BOOST_MATH_STD_USING
0055
0056 T t = m_squared_total - m_total * m_total / m_count;
0057 t /= (m_count-1);
0058 return t;
0059 }
0060 T rms()const
0061 {
0062 BOOST_MATH_STD_USING
0063
0064 return sqrt(m_squared_total / static_cast<T>(m_count));
0065 }
0066 stats& operator+=(const stats& s)
0067 {
0068 if(s.m_min < m_min)
0069 m_min = s.m_min;
0070 if(s.m_max > m_max)
0071 m_max = s.m_max;
0072 m_total += s.m_total;
0073 m_squared_total += s.m_squared_total;
0074 m_count += s.m_count;
0075 return *this;
0076 }
0077 private:
0078 T m_min, m_max, m_total, m_squared_total;
0079 std::uintmax_t m_count{0};
0080 };
0081
0082 }
0083 }
0084 }
0085
0086 #endif
0087