2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2019,2021, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * \brief The normal distribution
39 * Portable version of the normal distribution that generates the same sequence
40 * on all platforms. Since stdlibc++ and libc++ provide different sequences
41 * we prefer this one so unit tests produce the same values on all platforms.
43 * \author Erik Lindahl <erik.lindahl@gmail.com>
45 * \ingroup module_random
48 #ifndef GMX_RANDOM_NORMALDISTRIBUTION_H
49 #define GMX_RANDOM_NORMALDISTRIBUTION_H
56 #include "gromacs/random/uniformrealdistribution.h"
57 #include "gromacs/utility/classhelpers.h"
60 * The portable version of the normal distribution (to make sure we get the same
61 * values on all platforms) has been modified from the LLVM libcxx headers,
62 * distributed under the MIT license:
64 * Copyright (c) The LLVM compiler infrastructure
66 * Permission is hereby granted, free of charge, to any person obtaining a copy
67 * of this software and associated documentation files (the "Software"), to deal
68 * in the Software without restriction, including without limitation the rights
69 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
70 * copies of the Software, and to permit persons to whom the Software is
71 * furnished to do so, subject to the following conditions:
73 * The above copyright notice and this permission notice shall be included in
74 * all copies or substantial portions of the Software.
76 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
77 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
78 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
79 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
80 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
81 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
88 /*! \brief Normal distribution
90 * The C++ standard library does provide a normal distribution, but even
91 * though they all sample from the normal distribution different standard
92 * library implementations appear to return different sequences of numbers
93 * for the same random number generator. To make it easier to use GROMACS
94 * unit tests that depend on random numbers we have our own implementation.
96 * Be warned that the normal distribution draws values from the random engine
97 * in a loop, so you want to make sure you use a random stream with a
98 * very large margin to make sure you do not run out of random numbers
99 * in an unlucky case (which will lead to an exception with the GROMACS
100 * default random engine).
102 * \tparam RealType Floating-point type, real by default in GROMACS.
104 template<class RealType = real>
105 class NormalDistribution
108 /*! \brief Type of values returned */
109 typedef RealType result_type;
111 /*! \brief Normal distribution parameters */
114 /*! \brief Mean of normal distribution */
116 /*! \brief Standard deviation of distribution */
120 /*! \brief Reference back to the distribution class */
121 typedef NormalDistribution distribution_type;
123 /*! \brief Construct parameter block
125 * \param mean Mean of normal distribution
126 * \param stddev Standard deviation of normal distribution
128 explicit param_type(result_type mean = 0.0, result_type stddev = 1.0) :
129 mean_(mean), stddev_(stddev)
133 /*! \brief Return first parameter */
134 result_type mean() const { return mean_; }
135 /*! \brief Return second parameter */
136 result_type stddev() const { return stddev_; }
138 /*! \brief True if two parameter sets will return the same normal distribution.
140 * \param x Instance to compare with.
142 bool operator==(const param_type& x) const
144 return mean_ == x.mean_ && stddev_ == x.stddev_;
147 /*! \brief True if two parameter sets will return different normal distributions
149 * \param x Instance to compare with.
151 bool operator!=(const param_type& x) const { return !operator==(x); }
154 /*! \brief Construct new distribution with given floating-point parameters.
156 * \param mean Mean of normal distribution
157 * \param stddev Standard deviation of normal distribution
159 explicit NormalDistribution(result_type mean = 0.0, result_type stddev = 1.0) :
160 param_(param_type(mean, stddev)), hot_(false), saved_(0)
164 /*! \brief Construct new distribution from parameter class
166 * \param param Parameter class as defined inside gmx::NormalDistribution.
168 explicit NormalDistribution(const param_type& param) : param_(param), hot_(false), saved_(0) {}
170 /*! \brief Flush all internal saved values */
171 void reset() { hot_ = false; }
173 /*! \brief Return values from normal distribution with internal parameters
175 * \tparam Rng Random engine class
177 * \param g Random engine
180 result_type operator()(Rng& g)
182 return (*this)(g, param_);
185 /*! \brief Return value from normal distribution with given parameters
187 * \tparam Rng Random engine class
189 * \param g Random engine
190 * \param param Parameters to use
193 result_type operator()(Rng& g, const param_type& param)
204 UniformRealDistribution<result_type> uniformDist(-1.0, 1.0);
214 } while (s > 1.0 || s == 0.0);
216 s = std::sqrt(-2.0 * std::log(s) / s);
221 return result * param.stddev() + param.mean();
224 /*! \brief Return the mean of the normal distribution */
225 result_type mean() const { return param_.mean(); }
227 /*! \brief Return the standard deviation of the normal distribution */
228 result_type stddev() const { return param_.stddev(); }
230 /*! \brief Return the full parameter class of the normal distribution */
231 param_type param() const { return param_; }
233 /*! \brief Smallest value that can be returned from normal distribution */
234 result_type min() const { return -std::numeric_limits<result_type>::infinity(); }
236 /*! \brief Largest value that can be returned from normal distribution */
237 result_type max() const { return std::numeric_limits<result_type>::infinity(); }
239 /*! \brief True if two normal distributions will produce the same values.
241 * \param x Instance to compare with.
243 bool operator==(const NormalDistribution& x) const
245 /* Equal if: Params are identical, and saved-state is identical,
246 * and if we have something saved, it must be identical.
248 return param_ == x.param_ && hot_ == x.hot_ && (!hot_ || saved_ == x.saved_);
251 /*! \brief True if two normal distributions will produce different values.
253 * \param x Instance to compare with.
255 bool operator!=(const NormalDistribution& x) const { return !operator==(x); }
258 /*! \brief Internal value for parameters, can be overridden at generation time. */
260 /*! \brief True if there is a saved result to return */
262 /*! \brief The saved result to return - only valid if hot_ is true */
265 GMX_DISALLOW_COPY_AND_ASSIGN(NormalDistribution);
271 #endif // GMX_RANDOM_NORMALDISTRIBUTION_H