From a0db2a60d0b1350e3df7ba2bb041cec74e4bed07 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sun, 22 Jun 2008 22:14:13 +0000 Subject: [PATCH] Added a min() and max() to the running_stats object. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402348 --- dlib/statistics/statistics.h | 39 +++++++++++++++++++++++++-- dlib/statistics/statistics_abstract.h | 18 +++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/dlib/statistics/statistics.h b/dlib/statistics/statistics.h index 28df37cbf..98571a08b 100644 --- a/dlib/statistics/statistics.h +++ b/dlib/statistics/statistics.h @@ -37,6 +37,8 @@ namespace dlib sum_sqr = 0; n = 0; maximum_n = std::numeric_limits::max(); + min_value = std::numeric_limits::max(); + max_value = std::numeric_limits::min(); } void set_max_n ( @@ -56,6 +58,11 @@ namespace dlib sum = n_div_n*sum + val*div_n; sum_sqr = n_div_n*sum_sqr + val*div_n*val; + if (val < min_value) + min_value = val; + if (val > max_value) + max_value = val; + if (n < maximum_n) ++n; } @@ -78,13 +85,39 @@ namespace dlib return sum; } + T max ( + ) const + { + // make sure requires clause is not broken + DLIB_ASSERT(current_n() > 1, + "\tT running_stats::max" + << "\n\tyou have to add some numbers to this object first" + << "\n\tthis: " << this + ); + + return max_value; + } + + T min ( + ) const + { + // make sure requires clause is not broken + DLIB_ASSERT(current_n() > 1, + "\tT running_stats::min" + << "\n\tyou have to add some numbers to this object first" + << "\n\tthis: " << this + ); + + return min_value; + } + T variance ( ) const { // make sure requires clause is not broken DLIB_ASSERT(current_n() > 1, "\tT running_stats::variance" - << "\n\tsize of queue should not be zero" + << "\n\tyou have to add some numbers to this object first" << "\n\tthis: " << this ); @@ -99,7 +132,7 @@ namespace dlib // make sure requires clause is not broken DLIB_ASSERT(current_n() > 1, "\tT running_stats::variance" - << "\n\tsize of queue should not be zero" + << "\n\tyou have to add some numbers to this object first" << "\n\tthis: " << this ); return (val-mean())/std::sqrt(variance()); @@ -110,6 +143,8 @@ namespace dlib T sum_sqr; T n; T maximum_n; + T min_value; + T max_value; }; // ---------------------------------------------------------------------------------------- diff --git a/dlib/statistics/statistics_abstract.h b/dlib/statistics/statistics_abstract.h index c9e8fc467..c63a68557 100644 --- a/dlib/statistics/statistics_abstract.h +++ b/dlib/statistics/statistics_abstract.h @@ -115,6 +115,24 @@ namespace dlib object so far. !*/ + T max ( + ) const; + /*! + requires + - current_n() > 1 + ensures + - returns the largest value presented to this object so far. + !*/ + + T min ( + ) const; + /*! + requires + - current_n() > 1 + ensures + - returns the smallest value presented to this object so far. + !*/ + T scale ( const T& val ) const;