2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016, 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
55 #include "gromacs/random/uniformrealdistribution.h"
56 #include "gromacs/utility/classhelpers.h"
59 * The portable version of the normal distribution (to make sure we get the same
60 * values on all platforms) has been modified from the LLVM libcxx headers,
61 * distributed under the MIT license:
63 * Copyright (c) The LLVM compiler infrastructure
65 * Permission is hereby granted, free of charge, to any person obtaining a copy
66 * of this software and associated documentation files (the "Software"), to deal
67 * in the Software without restriction, including without limitation the rights
68 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
69 * copies of the Software, and to permit persons to whom the Software is
70 * furnished to do so, subject to the following conditions:
72 * The above copyright notice and this permission notice shall be included in
73 * all copies or substantial portions of the Software.
75 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
76 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
77 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
78 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
79 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
80 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
87 /*! \brief Normal distribution
89 * The C++ standard library does provide a normal distribution, but even
90 * though they all sample from the normal distribution different standard
91 * library implementations appear to return different sequences of numbers
92 * for the same random number generator. To make it easier to use GROMACS
93 * unit tests that depend on random numbers we have our own implementation.
95 * Be warned that the normal distribution draws values from the random engine
96 * in a loop, so you want to make sure you use a random stream with a
97 * very large margin to make sure you do not run out of random numbers
98 * in an unlucky case (which will lead to an exception with the GROMACS
99 * default random engine).
101 * \tparam RealType Floating-point type, real by default in GROMACS.
103 template<class RealType
= real
>
104 class NormalDistribution
107 /*! \brief Type of values returned */
108 typedef RealType result_type
;
110 /*! \brief Normal distribution parameters */
113 /*! \brief Mean of normal distribution */
115 /*! \brief Standard deviation of distribution */
119 /*! \brief Reference back to the distribution class */
120 typedef NormalDistribution distribution_type
;
122 /*! \brief Construct parameter block
124 * \param mean Mean of normal distribution
125 * \param stddev Standard deviation of normal distribution
127 explicit param_type(result_type mean
= 0.0, result_type stddev
= 1.0)
128 : mean_(mean
), stddev_(stddev
) {}
130 /*! \brief Return first parameter */
131 result_type
mean() const { return mean_
; }
132 /*! \brief Return second parameter */
133 result_type
stddev() const { return stddev_
; }
135 /*! \brief True if two parameter sets will return the same normal distribution.
137 * \param x Instance to compare with.
140 operator==(const param_type
&x
) const
142 return mean_
== x
.mean_
&& stddev_
== x
.stddev_
;
145 /*! \brief True if two parameter sets will return different normal distributions
147 * \param x Instance to compare with.
150 operator!=(const param_type
&x
) const { return !operator==(x
); }
155 /*! \brief Construct new distribution with given floating-point parameters.
157 * \param mean Mean of normal distribution
158 * \param stddev Standard deviation of normal distribution
160 explicit NormalDistribution(result_type mean
= 0.0, result_type stddev
= 1.0)
161 : param_(param_type(mean
, stddev
)), hot_(false), saved_(0) {}
163 /*! \brief Construct new distribution from parameter class
165 * \param param Parameter class as defined inside gmx::NormalDistribution.
167 explicit NormalDistribution(const param_type
¶m
)
168 : param_(param
), hot_(false), saved_(0) {}
170 /*! \brief Flush all internal saved values */
172 reset() { hot_
= false; }
174 /*! \brief Return values from normal distribution with internal parameters
176 * \tparam Rng Random engine class
178 * \param g Random engine
182 operator()(Rng
&g
) { return (*this)(g
, param_
); }
184 /*! \brief Return value from normal distribution with given parameters
186 * \tparam Rng Random engine class
188 * \param g Random engine
189 * \param param Parameters to use
193 operator()(Rng
&g
, const param_type
¶m
)
204 UniformRealDistribution
<result_type
> uniformDist(-1.0, 1.0);
215 while (s
> 1.0 || s
== 0.0);
217 s
= std::sqrt(-2.0 * std::log(s
) / s
);
222 return result
* param
.stddev() + param
.mean();
225 /*! \brief Return the mean of the normal distribution */
227 mean() const { return param_
.mean(); }
229 /*! \brief Return the standard deviation of the normal distribution */
231 stddev() const { return param_
.stddev(); }
233 /*! \brief Return the full parameter class of the normal distribution */
234 param_type
param() const { return param_
; }
236 /*! \brief Smallest value that can be returned from normal distribution */
238 min() const { return -std::numeric_limits
<result_type
>::infinity(); }
240 /*! \brief Largest value that can be returned from normal distribution */
242 max() const { return std::numeric_limits
<result_type
>::infinity(); }
244 /*! \brief True if two normal distributions will produce the same values.
246 * \param x Instance to compare with.
249 operator==(const NormalDistribution
&x
) const
251 /* Equal if: Params are identical, and saved-state is identical,
252 * and if we have something saved, it must be identical.
254 return param_
== x
.param_
&& hot_
== x
.hot_
&& (!hot_
|| saved_
== x
.saved_
);
257 /*! \brief True if two normal distributions will produce different values.
259 * \param x Instance to compare with.
262 operator!=(const NormalDistribution
&x
) const
263 { return !operator==(x
); }
266 /*! \brief Internal value for parameters, can be overridden at generation time. */
268 /*! \brief True if there is a saved result to return */
270 /*! \brief The saved result to return - only valid if hot_ is true */
273 GMX_DISALLOW_COPY_AND_ASSIGN(NormalDistribution
);
279 #endif // GMX_RANDOM_NORMALDISTRIBUTION_H