2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015,2016,2019, 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 exponential distribution
39 * Portable version of the exponential distribution that generates the same
40 * sequence on all platforms. Since stdlibc++ and libc++ provide different
41 * sequences we prefer this one so unit tests produce the same values on all
44 * \author Erik Lindahl <erik.lindahl@gmail.com>
46 * \ingroup module_random
49 #ifndef GMX_RANDOM_EXPONENTIALDISTRIBUTION_H
50 #define GMX_RANDOM_EXPONENTIALDISTRIBUTION_H
56 #include "gromacs/random/uniformrealdistribution.h"
57 #include "gromacs/utility/classhelpers.h"
60 * The portable version of the exponential distribution (to make sure we get the
61 * same 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 Exponential distribution
90 * The C++ standard library does provide an exponential distribution, but even
91 * though they all sample from a correct 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 * \tparam RealType Floating-point type, real by default in GROMACS.
98 template<class RealType = real>
99 class ExponentialDistribution
102 /*! \brief Type of values returned */
103 typedef RealType result_type;
105 /*! \brief Exponential distribution parameters */
108 /*! \brief The lambda/decay parameter */
112 /*! \brief Reference back to the distribution class */
113 typedef ExponentialDistribution distribution_type;
115 /*! \brief Construct parameter block
117 * \param lambda lambda/decay parameter
119 explicit param_type(result_type lambda = 1.0) : lambda_(lambda) {}
121 /*! \brief Return lambda parameter */
122 result_type lambda() const { return lambda_; }
124 /*! \brief True if two parameter sets will return the same exponential distribution.
126 * \param x Instance to compare with.
128 bool operator==(const param_type& x) const { return lambda_ == x.lambda_; }
130 /*! \brief True if two parameter sets will return different exponential distributions
132 * \param x Instance to compare with.
134 bool operator!=(const param_type& x) const { return !operator==(x); }
138 /*! \brief Construct new distribution with given floating-point parameter.
140 * \param lambda lambda/decay parameter
143 explicit ExponentialDistribution(result_type lambda = 1.0) : param_(param_type(lambda)) {}
145 /*! \brief Construct new distribution from parameter class
147 * \param param Parameter class as defined inside gmx::ExponentialDistribution.
149 explicit ExponentialDistribution(const param_type& param) : param_(param) {}
151 /*! \brief Flush all internal saved values */
154 /*! \brief Return values from exponential distribution with internal parameters
156 * \tparam Rng Random engine class
158 * \param g Random engine
161 result_type operator()(Rng& g)
163 return (*this)(g, param_);
166 /*! \brief Return value from exponential distribution with given parameters
168 * \tparam Rng Random engine class
170 * \param g Random engine
171 * \param param Parameters to use
174 result_type operator()(Rng& g, const param_type& param)
176 return -std::log(result_type(1)
177 - generateCanonical<result_type, std::numeric_limits<result_type>::digits>(g))
181 /*! \brief Return the lambda parameter of the exponential distribution */
182 result_type lambda() const { return param_.lambda(); }
184 /*! \brief Return the full parameter class of exponential distribution */
185 param_type param() const { return param_; }
187 /*! \brief Smallest value that can be returned from exponential distribution */
188 result_type min() const { return 0; }
190 /*! \brief Largest value that can be returned from exponential distribution */
191 result_type max() const { return std::numeric_limits<result_type>::infinity(); }
193 /*! \brief True if two exponential distributions will produce the same values.
195 * \param x Instance to compare with.
197 bool operator==(const ExponentialDistribution& x) const { return param_ == x.param_; }
199 /*! \brief True if two exponential distributions will produce different values.
201 * \param x Instance to compare with.
203 bool operator!=(const ExponentialDistribution& x) const { return !operator==(x); }
206 /*! \brief Internal value for parameters, can be overridden at generation time. */
209 GMX_DISALLOW_COPY_AND_ASSIGN(ExponentialDistribution);
214 #endif // GMX_MATH_RANDOM_H