/*
* This file is part of the GROMACS molecular simulation package.
*
- * Copyright (c) 2015,2016, by the GROMACS development team, led by
+ * Copyright (c) 2015,2016,2019, by the GROMACS development team, led by
* Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
* and including many others, as listed in the AUTHORS file in the
* top-level source directory and at http://www.gromacs.org.
template<class RealType = real>
class NormalDistribution
{
- public:
- /*! \brief Type of values returned */
- typedef RealType result_type;
-
- /*! \brief Normal distribution parameters */
- class param_type
- {
- /*! \brief Mean of normal distribution */
- result_type mean_;
- /*! \brief Standard deviation of distribution */
- result_type stddev_;
-
- public:
- /*! \brief Reference back to the distribution class */
- typedef NormalDistribution distribution_type;
-
- /*! \brief Construct parameter block
- *
- * \param mean Mean of normal distribution
- * \param stddev Standard deviation of normal distribution
- */
- explicit param_type(result_type mean = 0.0, result_type stddev = 1.0)
- : mean_(mean), stddev_(stddev) {}
-
- /*! \brief Return first parameter */
- result_type mean() const { return mean_; }
- /*! \brief Return second parameter */
- result_type stddev() const { return stddev_; }
-
- /*! \brief True if two parameter sets will return the same normal distribution.
- *
- * \param x Instance to compare with.
- */
- bool
- operator==(const param_type &x) const
- {
- return mean_ == x.mean_ && stddev_ == x.stddev_;
- }
-
- /*! \brief True if two parameter sets will return different normal distributions
- *
- * \param x Instance to compare with.
- */
- bool
- operator!=(const param_type &x) const { return !operator==(x); }
- };
+public:
+ /*! \brief Type of values returned */
+ typedef RealType result_type;
+
+ /*! \brief Normal distribution parameters */
+ class param_type
+ {
+ /*! \brief Mean of normal distribution */
+ result_type mean_;
+ /*! \brief Standard deviation of distribution */
+ result_type stddev_;
public:
+ /*! \brief Reference back to the distribution class */
+ typedef NormalDistribution distribution_type;
- /*! \brief Construct new distribution with given floating-point parameters.
+ /*! \brief Construct parameter block
*
* \param mean Mean of normal distribution
* \param stddev Standard deviation of normal distribution
*/
- explicit NormalDistribution(result_type mean = 0.0, result_type stddev = 1.0)
- : param_(param_type(mean, stddev)), hot_(false), saved_(0) {}
-
- /*! \brief Construct new distribution from parameter class
- *
- * \param param Parameter class as defined inside gmx::NormalDistribution.
- */
- explicit NormalDistribution(const param_type ¶m)
- : param_(param), hot_(false), saved_(0) {}
+ explicit param_type(result_type mean = 0.0, result_type stddev = 1.0) :
+ mean_(mean),
+ stddev_(stddev)
+ {
+ }
- /*! \brief Flush all internal saved values */
- void
- reset() { hot_ = false; }
+ /*! \brief Return first parameter */
+ result_type mean() const { return mean_; }
+ /*! \brief Return second parameter */
+ result_type stddev() const { return stddev_; }
- /*! \brief Return values from normal distribution with internal parameters
- *
- * \tparam Rng Random engine class
+ /*! \brief True if two parameter sets will return the same normal distribution.
*
- * \param g Random engine
+ * \param x Instance to compare with.
*/
- template<class Rng>
- result_type
- operator()(Rng &g) { return (*this)(g, param_); }
+ bool operator==(const param_type& x) const
+ {
+ return mean_ == x.mean_ && stddev_ == x.stddev_;
+ }
- /*! \brief Return value from normal distribution with given parameters
+ /*! \brief True if two parameter sets will return different normal distributions
*
- * \tparam Rng Random engine class
- *
- * \param g Random engine
- * \param param Parameters to use
+ * \param x Instance to compare with.
*/
- template<class Rng>
- result_type
- operator()(Rng &g, const param_type ¶m)
+ bool operator!=(const param_type& x) const { return !operator==(x); }
+ };
+
+public:
+ /*! \brief Construct new distribution with given floating-point parameters.
+ *
+ * \param mean Mean of normal distribution
+ * \param stddev Standard deviation of normal distribution
+ */
+ explicit NormalDistribution(result_type mean = 0.0, result_type stddev = 1.0) :
+ param_(param_type(mean, stddev)),
+ hot_(false),
+ saved_(0)
+ {
+ }
+
+ /*! \brief Construct new distribution from parameter class
+ *
+ * \param param Parameter class as defined inside gmx::NormalDistribution.
+ */
+ explicit NormalDistribution(const param_type& param) : param_(param), hot_(false), saved_(0) {}
+
+ /*! \brief Flush all internal saved values */
+ void reset() { hot_ = false; }
+
+ /*! \brief Return values from normal distribution with internal parameters
+ *
+ * \tparam Rng Random engine class
+ *
+ * \param g Random engine
+ */
+ template<class Rng>
+ result_type operator()(Rng& g)
+ {
+ return (*this)(g, param_);
+ }
+
+ /*! \brief Return value from normal distribution with given parameters
+ *
+ * \tparam Rng Random engine class
+ *
+ * \param g Random engine
+ * \param param Parameters to use
+ */
+ template<class Rng>
+ result_type operator()(Rng& g, const param_type& param)
+ {
+ result_type result;
+
+ if (hot_)
+ {
+ hot_ = false;
+ result = saved_;
+ }
+ else
{
- result_type result;
+ UniformRealDistribution<result_type> uniformDist(-1.0, 1.0);
+ result_type u;
+ result_type v;
+ result_type s;
- if (hot_)
+ do
{
- hot_ = false;
- result = saved_;
- }
- else
- {
- UniformRealDistribution<result_type> uniformDist(-1.0, 1.0);
- result_type u;
- result_type v;
- result_type s;
-
- do
- {
- u = uniformDist(g);
- v = uniformDist(g);
- s = u * u + v * v;
- }
- while (s > 1.0 || s == 0.0);
-
- s = std::sqrt(-2.0 * std::log(s) / s);
- saved_ = v * s;
- hot_ = true;
- result = u * s;
- }
- return result * param.stddev() + param.mean();
+ u = uniformDist(g);
+ v = uniformDist(g);
+ s = u * u + v * v;
+ } while (s > 1.0 || s == 0.0);
+
+ s = std::sqrt(-2.0 * std::log(s) / s);
+ saved_ = v * s;
+ hot_ = true;
+ result = u * s;
}
+ return result * param.stddev() + param.mean();
+ }
- /*! \brief Return the mean of the normal distribution */
- result_type
- mean() const { return param_.mean(); }
-
- /*! \brief Return the standard deviation of the normal distribution */
- result_type
- stddev() const { return param_.stddev(); }
+ /*! \brief Return the mean of the normal distribution */
+ result_type mean() const { return param_.mean(); }
- /*! \brief Return the full parameter class of the normal distribution */
- param_type param() const { return param_; }
+ /*! \brief Return the standard deviation of the normal distribution */
+ result_type stddev() const { return param_.stddev(); }
- /*! \brief Smallest value that can be returned from normal distribution */
- result_type
- min() const { return -std::numeric_limits<result_type>::infinity(); }
+ /*! \brief Return the full parameter class of the normal distribution */
+ param_type param() const { return param_; }
- /*! \brief Largest value that can be returned from normal distribution */
- result_type
- max() const { return std::numeric_limits<result_type>::infinity(); }
+ /*! \brief Smallest value that can be returned from normal distribution */
+ result_type min() const { return -std::numeric_limits<result_type>::infinity(); }
- /*! \brief True if two normal distributions will produce the same values.
- *
- * \param x Instance to compare with.
- */
- bool
- operator==(const NormalDistribution &x) const
- {
- /* Equal if: Params are identical, and saved-state is identical,
- * and if we have something saved, it must be identical.
- */
- return param_ == x.param_ && hot_ == x.hot_ && (!hot_ || saved_ == x.saved_);
- }
+ /*! \brief Largest value that can be returned from normal distribution */
+ result_type max() const { return std::numeric_limits<result_type>::infinity(); }
- /*! \brief True if two normal distributions will produce different values.
- *
- * \param x Instance to compare with.
+ /*! \brief True if two normal distributions will produce the same values.
+ *
+ * \param x Instance to compare with.
+ */
+ bool operator==(const NormalDistribution& x) const
+ {
+ /* Equal if: Params are identical, and saved-state is identical,
+ * and if we have something saved, it must be identical.
*/
- bool
- operator!=(const NormalDistribution &x) const
- { return !operator==(x); }
-
- private:
- /*! \brief Internal value for parameters, can be overridden at generation time. */
- param_type param_;
- /*! \brief True if there is a saved result to return */
- bool hot_;
- /*! \brief The saved result to return - only valid if hot_ is true */
- result_type saved_;
-
- GMX_DISALLOW_COPY_AND_ASSIGN(NormalDistribution);
+ return param_ == x.param_ && hot_ == x.hot_ && (!hot_ || saved_ == x.saved_);
+ }
+
+ /*! \brief True if two normal distributions will produce different values.
+ *
+ * \param x Instance to compare with.
+ */
+ bool operator!=(const NormalDistribution& x) const { return !operator==(x); }
+
+private:
+ /*! \brief Internal value for parameters, can be overridden at generation time. */
+ param_type param_;
+ /*! \brief True if there is a saved result to return */
+ bool hot_;
+ /*! \brief The saved result to return - only valid if hot_ is true */
+ result_type saved_;
+
+ GMX_DISALLOW_COPY_AND_ASSIGN(NormalDistribution);
};
-} // namespace gmx
+} // namespace gmx
#endif // GMX_RANDOM_NORMALDISTRIBUTION_H