Chris@16: // (C) Copyright John Maddock 2005-2006. Chris@16: // Use, modification and distribution are subject to the Chris@16: // Boost Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_MATH_TOOLS_STATS_INCLUDED Chris@16: #define BOOST_MATH_TOOLS_STATS_INCLUDED Chris@16: Chris@16: #ifdef _MSC_VER Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost{ namespace math{ namespace tools{ Chris@16: Chris@16: template Chris@16: class stats Chris@16: { Chris@16: public: Chris@16: stats() Chris@16: : m_min(tools::max_value()), Chris@16: m_max(-tools::max_value()), Chris@16: m_total(0), Chris@16: m_squared_total(0), Chris@16: m_count(0) Chris@16: {} Chris@16: void add(const T& val) Chris@16: { Chris@16: if(val < m_min) Chris@16: m_min = val; Chris@16: if(val > m_max) Chris@16: m_max = val; Chris@16: m_total += val; Chris@16: ++m_count; Chris@16: m_squared_total += val*val; Chris@16: } Chris@16: T min BOOST_PREVENT_MACRO_SUBSTITUTION()const{ return m_min; } Chris@16: T max BOOST_PREVENT_MACRO_SUBSTITUTION()const{ return m_max; } Chris@16: T total()const{ return m_total; } Chris@16: T mean()const{ return m_total / static_cast(m_count); } Chris@16: boost::uintmax_t count()const{ return m_count; } Chris@16: T variance()const Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: Chris@16: T t = m_squared_total - m_total * m_total / m_count; Chris@16: t /= m_count; Chris@16: return t; Chris@16: } Chris@16: T variance1()const Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: Chris@16: T t = m_squared_total - m_total * m_total / m_count; Chris@16: t /= (m_count-1); Chris@16: return t; Chris@16: } Chris@16: T rms()const Chris@16: { Chris@16: BOOST_MATH_STD_USING Chris@16: Chris@16: return sqrt(m_squared_total / static_cast(m_count)); Chris@16: } Chris@16: stats& operator+=(const stats& s) Chris@16: { Chris@16: if(s.m_min < m_min) Chris@16: m_min = s.m_min; Chris@16: if(s.m_max > m_max) Chris@16: m_max = s.m_max; Chris@16: m_total += s.m_total; Chris@16: m_squared_total += s.m_squared_total; Chris@16: m_count += s.m_count; Chris@16: return *this; Chris@16: } Chris@16: private: Chris@16: T m_min, m_max, m_total, m_squared_total; Chris@16: boost::uintmax_t m_count; Chris@16: }; Chris@16: Chris@16: } // namespace tools Chris@16: } // namespace math Chris@16: } // namespace boost Chris@16: Chris@16: #endif Chris@16: