Added a min() and max() to the running_stats object.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402348
This commit is contained in:
Davis King 2008-06-22 22:14:13 +00:00
parent 4129ef3ca5
commit a0db2a60d0
2 changed files with 55 additions and 2 deletions

View File

@ -37,6 +37,8 @@ namespace dlib
sum_sqr = 0;
n = 0;
maximum_n = std::numeric_limits<T>::max();
min_value = std::numeric_limits<T>::max();
max_value = std::numeric_limits<T>::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;
};
// ----------------------------------------------------------------------------------------

View File

@ -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;