3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_MATH_STATS_HISTOGRAMS_H
7#define ELEMENTA_MATH_STATS_HISTOGRAMS_H
71 {
return(
static_cast<size_t>(
72 std::round( 2.0 * std::pow(
static_cast<double>(ndata),
90 bool iswidthorright =
true,
91 bool addleftinf =
false,
bool addrightinf =
false);
100 BinDef(
long leftcenter,
long rightcenter,
101 bool addleftinf =
false,
bool addrightinf =
false);
105 size_t numbins(
void)
const noexcept {
return(numbins_); }
110 bool entireReals(
void)
const noexcept {
return(binlefts_.empty()); }
115 void binBounds(
size_t ind,
double & left,
double & right)
const;
122 bool whichBin(
double v,
size_t & ind)
const noexcept;
129 static void checkFiniteness(
double v,
const std::string & name,
130 const std::string & place);
131 static void checkPositiveness(
double v,
const std::string & name,
132 const std::string & place,
133 bool orzero =
false);
134 static void checkNumbins(
size_t numbins,
const std::string & place);
136 std::vector<double> binlefts_;
138 bool tominf_,topinf_;
141 size_t calcnumbins(
void)
const noexcept
142 {
if (binlefts_.empty())
return(1);
143 return(binlefts_.size() + (tominf_ ? 1:0) + (topinf_ ? 1:0)); }
157 Histogram(
const DIt & first,
const DIt & pastend,
171 size_t numData(
void)
const noexcept {
return(ndata_); }
176 size_t freq(
size_t indbin)
const
178 return(frequencies_[indbin]); }
188 std::vector<size_t> frequencies_;
216 ndata_ = std::distance(first,pastend);
217 frequencies_.push_back(ndata_);
225 while (it != pastend)
229 ++(frequencies_[ind]);
Definition of the support (bins) of a histogram.
BinDef(void)
Default constructor: one bin from -inf to +inf.
BinDef(long leftcenter, long rightcenter, bool addleftinf=false, bool addrightinf=false)
Constructor for bins that cover discrete supports.
std::string to_string(void) const
Return an informative text with the summary of the bin definition.
void binBounds(size_t ind, double &left, double &right) const
Fill LEFT and RIGHT with the boundaries of the IND-th bin.
bool entireReals(void) const noexcept
Return TRUE if there is only one bin covering from -inf to +inf.
static size_t eq_freq_nbins(size_t ndata)
Return a reasonable number of bins for a sample with NDATA data.
bool whichBin(double v, size_t &ind) const noexcept
Fill IND with the index of the bin where V lies.
size_t numbins(void) const noexcept
Return the total number of bins, including infinite ones.
BinDef(double leftmost, double binwidth_or_rightmost, size_t numbins, bool iswidthorright=true, bool addleftinf=false, bool addrightinf=false)
Constructor for periodic bins.
#define ELE_CODE_OUTOFRANGE(expl)
To throw an out-of-range exception with an explanation.
#define ELE_CODE_INVARG(expl)
To throw an invalid-argument exception with an explanation.
const BinDef & bindef(void) const
Return a reference to the internal ref kept of the bin definition.
size_t freq(size_t indbin) const
Return the frequency calculated for the INDBIN-th bin.
std::string to_string(void) const
Return a string with the information of the histogram.
size_t numData(void) const noexcept
Return the number of data effectively used (within bins) for the hist.
A one-dimensional histogram of data.
Histogram(const DIt &first, const DIt &pastend, const BinDef &bindef, const GetStaDatumFun< DIt > &getdatum=getstadatum_direct< DIt >)
Fill the histogram with the data got by getdatum according to bindef.
std::function< StaDatum(const DatumIterator &) > GetStaDatumFun
A function type that gets a datum from an iterator.