88 lines · plain
1// (C) Copyright John Maddock 2005-2006.2// Use, modification and distribution are subject to the3// Boost Software License, Version 1.0. (See accompanying file4// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)5 6#ifndef BOOST_MATH_TOOLS_STATS_INCLUDED7#define BOOST_MATH_TOOLS_STATS_INCLUDED8 9#ifdef _MSC_VER10#pragma once11#endif12 13#include <cstdint>14#include <cmath>15#include <boost/math/tools/precision.hpp>16 17namespace boost{ namespace math{ namespace tools{18 19template <class T>20class stats21{22public: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()const45 {46 BOOST_MATH_STD_USING47 48 T t = m_squared_total - m_total * m_total / m_count;49 t /= m_count;50 return t;51 }52 T variance1()const53 {54 BOOST_MATH_STD_USING55 56 T t = m_squared_total - m_total * m_total / m_count;57 t /= (m_count-1);58 return t;59 }60 T rms()const61 {62 BOOST_MATH_STD_USING63 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 }77private:78 T m_min, m_max, m_total, m_squared_total;79 std::uintmax_t m_count{0};80};81 82} // namespace tools83} // namespace math84} // namespace boost85 86#endif87 88