PSCF v1.5.0
Distribution.h
1#ifndef UTIL_DISTRIBUTION_H
2#define UTIL_DISTRIBUTION_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu)
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/param/ParamComposite.h> // base class
12#include <util/containers/DArray.h> // member template
13#include <util/math/feq.h> // Used in serialize template
14
15namespace Util
16{
17
24 {
25
26 public:
27
32
38 Distribution(const Distribution& other);
39
46
50 virtual ~Distribution();
51
60 virtual void readParameters(std::istream& in);
61
69 void setParam(double min, double max, int nBin);
70
76 virtual void loadParameters(Serializable::IArchive &ar);
77
83 virtual void save(Serializable::OArchive &ar);
84
91 template <class Archive>
92 void serialize(Archive& ar, const unsigned int version);
93
99 void sample(double value);
100
104 virtual void clear();
105
111 void output(std::ostream& out);
112
118 int binIndex(double value) const;
119
123 double min() const;
124
128 double max() const;
129
133 double binWidth() const;
134
138 int nBin() const;
139
140 #ifdef UTIL_MPI
147 void reduce(MPI::Intracomm& communicator, int root);
148 #endif
149
150 protected:
151
153 double min_;
154 double max_;
155 double binWidth_;
156 int nBin_;
159
160 };
161
162 // inline method definitions
163
164 /*
165 * Return the index of the bin for a value.
166 */
167 inline int Distribution::binIndex(double value) const
168 {
169 // Rounding can give nBin_ for a value just below max_
170 int i = int( (value - min_)/binWidth_ );
171 return (i < nBin_) ? i : nBin_ - 1;
172 }
173
174 /*
175 * Get minimum value in range of histogram.
176 */
177 inline double Distribution::min() const
178 { return min_; }
179
180 /*
181 * Get maximum value in range of histogram.
182 */
183 inline double Distribution::max() const
184 { return max_; }
185
186 /*
187 * Get binWidth, the width of each bin.
188 */
189 inline double Distribution::binWidth() const
190 { return binWidth_; }
191
192 /*
193 * Get the number of bins
194 */
195 inline int Distribution::nBin() const
196 { return nBin_; }
197
198 /*
199 * Serialize this Distribution.
200 */
201 template <class Archive>
202 void Distribution::serialize(Archive& ar, const unsigned int version)
203 {
204 ar & min_;
205 ar & max_;
206 ar & nBin_;
207 ar & nSample_;
208 ar & nReject_;
209 ar & binWidth_;
210 ar & histogram_;
211
212 // Validate
213 if (histogram_.capacity() != nBin_) {
214 UTIL_THROW("Inconsistent histogram capacity");
215 }
216 if (!feq(binWidth_, (max_ - min_)/double(nBin_))) {
217 UTIL_THROW("Inconsistent binWidth");
218 }
219 }
220
221}
222#endif
Dynamically allocatable contiguous array template.
Definition DArray.h:32
int nBin() const
Get the number of bins.
void sample(double value)
Sample a value.
double binWidth_
width of bin = (max_-min_)/nBin_ .
double min_
minimum value.
double binWidth() const
Get binWidth, the width of each bin.
DArray< long > histogram_
Histogram of occurences, one element per bin.
double min() const
Get minimum value in range of histogram.
void setParam(double min, double max, int nBin)
Set parameters and initialize.
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
double max_
maximum value.
int nBin_
number of bins.
virtual void readParameters(std::istream &in)
Read parameters from file and initialize.
void output(std::ostream &out)
Output the distribution to file.
int binIndex(double value) const
Return the index of the bin for a value.
Distribution & operator=(const Distribution &other)
Assignment operator.
void serialize(Archive &ar, const unsigned int version)
Serialize this Distribution to/from an archive.
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from an archive.
Distribution()
Default constructor.
int nSample_
Number of sampled values in Histogram.
int nReject_
Number of sampled values that were out of range.
virtual void clear()
Clear (i.e., zero) previously allocated histogram.
double max() const
Get maximum value in range of histogram.
ParamComposite()
Constructor.
BinaryFileIArchive IArchive
Type of input archive used by load method.
BinaryFileOArchive OArchive
Type of output archive used by save method.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
bool feq(double x, double y, double eps=1.0E-10)
Are two floating point numbers equal to within round-off error?
Definition feq.h:27
Utility classes for scientific computation.