10 #ifndef OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
11 #define OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
29 template <
typename ValueType,
typename Less = std::less<ValueType> >
32 using Limits = std::numeric_limits<ValueType>;
40 static_assert(std::numeric_limits<ValueType>::is_specialized,
41 "openvdb::math::MinMax default constructor requires a std::numeric_limits specialization");
73 std::ostringstream os;
74 os << std::setprecision(
precision) << std::setiosflags(std::ios::fixed);
76 if (!
name.empty()) os <<
"for \"" <<
name <<
"\" ";
77 os <<
" Min=" <<
mMin <<
", Max=" <<
mMax << std::endl;
96 ,
mMin(std::numeric_limits<double>::
max())
110 void add(
double val, uint64_t
n)
140 std::ostringstream os;
141 os << std::setprecision(
precision) << std::setiosflags(std::ios::fixed);
143 if (!
name.empty()) os <<
"for \"" <<
name <<
"\" ";
145 os <<
"with " <<
mSize <<
" samples:\n"
148 <<
", Range="<< this->
range() << std::endl;
150 os <<
": no samples were added." << std::endl;
159 assert(other.
mSize > 0);
192 const double delta = val -
mAvg;
193 mAvg += delta/double(
mSize);
198 void add(
double val, uint64_t
n)
200 const double denom = 1.0/double(
mSize + n);
201 const double delta = val -
mAvg;
202 mAvg += denom * delta * double(n);
203 mAux += denom * delta * delta * double(
mSize) * double(n);
210 if (other.
mSize > 0) {
211 const double denom = 1.0/double(
mSize + other.
mSize);
212 const double delta = other.
mAvg -
mAvg;
213 mAvg += denom * delta * double(other.
mSize);
245 std::ostringstream os;
246 os << std::setprecision(
precision) << std::setiosflags(std::ios::fixed);
248 if (!
name.empty()) os <<
"for \"" <<
name <<
"\" ";
250 os <<
"with " <<
mSize <<
" samples:\n"
254 <<
", Std=" << this->
stdDev()
255 <<
", Var=" << this->
variance() << std::endl;
257 os <<
": no samples were added." << std::endl;
280 : mSize(0), mMin(min), mMax(max + 1e-10),
283 if ( mMax <= mMin ) {
286 OPENVDB_THROW(ValueError,
"Histogram: expected at least one bin");
288 for (
size_t i=0; i<
numBins; ++i) mBins[i]=0;
294 mSize(0), mMin(s.
min()), mMax(s.
max()+1e-10),
297 if ( mMax <= mMin ) {
300 OPENVDB_THROW(ValueError,
"Histogram: expected at least one bin");
302 for (
size_t i=0; i<
numBins; ++i) mBins[i]=0;
308 inline bool add(
double val, uint64_t
n = 1)
311 mBins[size_t(mDelta*(val-mMin))] +=
n;
321 mBins.size() != other.mBins.size())
return false;
322 for (
size_t i=0, e=mBins.size(); i!=e; ++i) mBins[i] += other.mBins[i];
323 mSize += other.mSize;
328 inline size_t numBins()
const {
return mBins.size(); }
330 inline double min()
const {
return mMin; }
332 inline double max()
const {
return mMax; }
334 inline double min(
int n)
const {
return mMin+n/mDelta; }
336 inline double max(
int n)
const {
return mMin+(n+1)/mDelta; }
338 inline uint64_t
count(
int n)
const {
return mBins[
n]; }
340 inline uint64_t
size()
const {
return mSize; }
347 std::ostringstream os;
348 os << std::setprecision(6) << std::setiosflags(std::ios::fixed) << std::endl;
350 if (!
name.empty()) os <<
"for \"" <<
name <<
"\" ";
352 os <<
"with " << mSize <<
" samples:\n";
353 os <<
"==============================================================\n";
354 os <<
"|| # | Min | Max | Frequency | % ||\n";
355 os <<
"==============================================================\n";
356 for (
int i = 0, e =
int(mBins.size()); i != e; ++i) {
357 os <<
"|| " << std::setw(4) << i <<
" | " << std::setw(14) << this->
min(i) <<
" | "
358 << std::setw(14) << this->
max(i) <<
" | " << std::setw(9) << mBins[i] <<
" | "
359 << std::setw(3) << (100*mBins[i]/mSize) <<
" ||\n";
361 os <<
"==============================================================\n";
363 os <<
": no samples were added." << std::endl;
370 double mMin, mMax, mDelta;
371 std::vector<uint64_t> mBins;
378 #endif // OPENVDB_MATH_STATS_HAS_BEEN_INCLUDED
GLsizei const GLchar *const * string
double stdDev() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
vfloat4 sqrt(const vfloat4 &a)
bool add(const Histogram &other)
Add all the contributions from the other histogram, provided that it has the same configuration as th...
#define OPENVDB_USE_VERSION_NAMESPACE
void add(const Extrema &other)
Add the samples from the other Stats instance.
void add(const ValueType &val, const Less &less=Less())
Add a single sample.
void print(const std::string &name="", std::ostream &strm=std::cout) const
Print the histogram to the specified output stream.
uint64_t size() const
Return the population size, i.e., the total number of samples.
void add(const MinMax &other, const Less &less=Less())
Add the samples from the other Stats instance.
uint64_t count(int n) const
Return the number of samples in the nth bin.
double mean() const
Return the arithmetic mean, i.e. average, value.
bool isApproxEqual(const Type &a, const Type &b, const Type &tolerance)
Return true if a is equal to b to within the given tolerance.
double min(int n) const
Return the minimum value in the nth bin.
General-purpose arithmetic and comparison routines, most of which accept arbitrary value types (or at...
size_t numBins() const
Return the number of bins in this histogram.
double variance() const
Return the population variance.
double max(int n) const
Return the maximum value in the nth bin.
void join(const Extrema &other)
const ValueType & max() const
Return the maximum value.
double range() const
Return the range defined as the maximum value minus the minimum value.
This class computes statistics (minimum value, maximum value, mean, variance and standard deviation) ...
void add(double val)
Add a single sample.
double avg() const
Return the arithmetic mean, i.e. average, value.
GLuint const GLchar * name
uint64_t size() const
Return the size of the population, i.e., the total number of samples.
Templated class to compute the minimum and maximum values.
void print(const std::string &name="", std::ostream &strm=std::cout, int precision=3) const
Print statistics to the specified output stream.
MinMax(const ValueType &min, const ValueType &max)
Constructor.
void add(double val, uint64_t n)
Add n samples with constant value val.
double min() const
Return the minimum value.
double min() const
Return the lower bound of this histogram's value range.
MinMax()
Empty constructor.
GLenum GLint GLint * precision
This class computes a histogram, with a fixed interval width, of a population of floating-point value...
Library and file format version numbers.
void add(double val, uint64_t n)
Add n samples with constant value val.
void add(const Stats &other)
Add the samples from the other Stats instance.
double var() const
Return the population variance.
const ValueType & min() const
Return the minimum value.
void print(const std::string &name="", std::ostream &strm=std::cout, int precision=3) const
Print MinMax to the specified output stream.
void print(const std::string &name="", std::ostream &strm=std::cout, int precision=3) const
Print extrema to the specified output stream.
double max() const
Return the upper bound of this histogram's value range.
Histogram(const Stats &s, size_t numBins=10)
Construct with the given bin count and with minimum and maximum values taken from a Stats object...
Histogram(double min, double max, size_t numBins=10)
Construct with given minimum and maximum values and the given bin count.
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
bool add(double val, uint64_t n=1)
Add n samples with constant value val, provided that the val falls within this histogram's value rang...
double max() const
Return the maximum value.
double std() const
Return the standard deviation (=Sqrt(variance)) as defined from the (biased) population variance...
void add(double val)
Add a single sample.
#define OPENVDB_THROW(exception, message)
This class computes the minimum and maximum values of a population of floating-point values...