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 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
57 #include "gromacs/random/uniformrealdistribution.h"
58 #include "gromacs/utility/classhelpers.h"
61 * The portable version of the exponential distribution (to make sure we get the
62 * same values on all platforms) has been modified from the LLVM libcxx headers,
63 * distributed under the MIT license:
65 * Copyright (c) The LLVM compiler infrastructure
67 * Permission is hereby granted, free of charge, to any person obtaining a copy
68 * of this software and associated documentation files (the "Software"), to deal
69 * in the Software without restriction, including without limitation the rights
70 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
71 * copies of the Software, and to permit persons to whom the Software is
72 * furnished to do so, subject to the following conditions:
74 * The above copyright notice and this permission notice shall be included in
75 * all copies or substantial portions of the Software.
77 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
80 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
81 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
82 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
89 /*! \brief Exponential distribution
91 * The C++ standard library does provide an exponential distribution, but even
92 * though they all sample from a correct distribution, different standard
93 * library implementations appear to return different sequences of numbers
94 * for the same random number generator. To make it easier to use GROMACS
95 * unit tests that depend on random numbers we have our own implementation.
97 * \tparam RealType Floating-point type, real by default in GROMACS.
99 template<class RealType = real>
100 class ExponentialDistribution
103 /*! \brief Type of values returned */
104 typedef RealType result_type;
106 /*! \brief Exponential distribution parameters */
109 /*! \brief The lambda/decay parameter */
113 /*! \brief Reference back to the distribution class */
114 typedef ExponentialDistribution distribution_type;
116 /*! \brief Construct parameter block
118 * \param lambda lambda/decay parameter
120 explicit param_type(result_type lambda = 1.0) : lambda_(lambda) {}
122 /*! \brief Return lambda parameter */
123 result_type lambda() const { return lambda_; }
125 /*! \brief True if two parameter sets will return the same exponential distribution.
127 * \param x Instance to compare with.
129 bool operator==(const param_type& x) const { return lambda_ == x.lambda_; }
131 /*! \brief True if two parameter sets will return different exponential distributions
133 * \param x Instance to compare with.
135 bool operator!=(const param_type& x) const { return !operator==(x); }
139 /*! \brief Construct new distribution with given floating-point parameter.
141 * \param lambda lambda/decay parameter
144 explicit ExponentialDistribution(result_type lambda = 1.0) : param_(param_type(lambda)) {}
146 /*! \brief Construct new distribution from parameter class
148 * \param param Parameter class as defined inside gmx::ExponentialDistribution.
150 explicit ExponentialDistribution(const param_type& param) : param_(param) {}
152 /*! \brief Flush all internal saved values */
155 /*! \brief Return values from exponential distribution with internal parameters
157 * \tparam Rng Random engine class
159 * \param g Random engine
162 result_type operator()(Rng& g)
164 return (*this)(g, param_);
167 /*! \brief Return value from exponential distribution with given parameters
169 * \tparam Rng Random engine class
171 * \param g Random engine
172 * \param param Parameters to use
175 result_type operator()(Rng& g, const param_type& param)
177 return -std::log(result_type(1)
178 - generateCanonical<result_type, std::numeric_limits<result_type>::digits>(g))
182 /*! \brief Return the lambda parameter of the exponential distribution */
183 result_type lambda() const { return param_.lambda(); }
185 /*! \brief Return the full parameter class of exponential distribution */
186 param_type param() const { return param_; }
188 /*! \brief Smallest value that can be returned from exponential distribution */
189 result_type min() const { return 0; }
191 /*! \brief Largest value that can be returned from exponential distribution */
192 result_type max() const { return std::numeric_limits<result_type>::infinity(); }
194 /*! \brief True if two exponential distributions will produce the same values.
196 * \param x Instance to compare with.
198 bool operator==(const ExponentialDistribution& x) const { return param_ == x.param_; }
200 /*! \brief True if two exponential distributions will produce different values.
202 * \param x Instance to compare with.
204 bool operator!=(const ExponentialDistribution& x) const { return !operator==(x); }
207 /*! \brief Internal value for parameters, can be overridden at generation time. */
210 GMX_DISALLOW_COPY_AND_ASSIGN(ExponentialDistribution);
215 #endif // GMX_MATH_RANDOM_H