1 /* boost random/exponential_distribution.hpp header file
3 * Copyright Jens Maurer 2000-2001
4 * Copyright Steven Watanabe 2011
5 * Distributed under the Boost Software License, Version 1.0. (See
6 * accompanying file LICENSE_1_0.txt or copy at
7 * http://www.boost.org/LICENSE_1_0.txt)
9 * See http://www.boost.org for most recent version including documentation.
11 * $Id: exponential_distribution.hpp 71018 2011-04-05 21:27:52Z steven_watanabe $
14 * 2001-02-18 moved to individual header files
17 #ifndef BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
18 #define BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
20 #include <boost/config/no_tr1/cmath.hpp>
22 #include <boost/assert.hpp>
23 #include <boost/limits.hpp>
24 #include <boost/random/detail/config.hpp>
25 #include <boost/random/detail/operators.hpp>
26 #include <boost/random/uniform_01.hpp>
32 * The exponential distribution is a model of \random_distribution with
33 * a single parameter lambda.
35 * It has \f$\displaystyle p(x) = \lambda e^{-\lambda x}\f$
37 template<class RealType = double>
38 class exponential_distribution
41 typedef RealType input_type;
42 typedef RealType result_type;
48 typedef exponential_distribution distribution_type;
51 * Constructs parameters with a given lambda.
53 * Requires: lambda > 0
55 param_type(RealType lambda_arg = RealType(1.0))
56 : _lambda(lambda_arg) { BOOST_ASSERT(_lambda > RealType(0)); }
58 /** Returns the lambda parameter of the distribution. */
59 RealType lambda() const { return _lambda; }
61 /** Writes the parameters to a @c std::ostream. */
62 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
68 /** Reads the parameters from a @c std::istream. */
69 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
75 /** Returns true if the two sets of parameters are equal. */
76 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
77 { return lhs._lambda == rhs._lambda; }
79 /** Returns true if the two sets of parameters are different. */
80 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
87 * Constructs an exponential_distribution with a given lambda.
89 * Requires: lambda > 0
91 explicit exponential_distribution(RealType lambda_arg = RealType(1.0))
92 : _lambda(lambda_arg) { BOOST_ASSERT(_lambda > RealType(0)); }
95 * Constructs an exponential_distribution from its parameters
97 explicit exponential_distribution(const param_type& parm)
98 : _lambda(parm.lambda()) {}
100 // compiler-generated copy ctor and assignment operator are fine
102 /** Returns the lambda parameter of the distribution. */
103 RealType lambda() const { return _lambda; }
105 /** Returns the smallest value that the distribution can produce. */
106 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
107 { return RealType(0); }
108 /** Returns the largest value that the distribution can produce. */
109 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
110 { return (std::numeric_limits<RealType>::infinity)(); }
112 /** Returns the parameters of the distribution. */
113 param_type param() const { return param_type(_lambda); }
114 /** Sets the parameters of the distribution. */
115 void param(const param_type& parm) { _lambda = parm.lambda(); }
118 * Effects: Subsequent uses of the distribution do not depend
119 * on values produced by any engine prior to invoking reset.
124 * Returns a random variate distributed according to the
125 * exponential distribution.
127 template<class Engine>
128 result_type operator()(Engine& eng) const
131 return -result_type(1) /
132 _lambda * log(result_type(1)-uniform_01<RealType>()(eng));
136 * Returns a random variate distributed according to the exponential
137 * distribution with parameters specified by param.
139 template<class Engine>
140 result_type operator()(Engine& eng, const param_type& parm) const
142 return exponential_distribution(parm)(eng);
145 /** Writes the distribution to a std::ostream. */
146 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, exponential_distribution, ed)
152 /** Reads the distribution from a std::istream. */
153 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, exponential_distribution, ed)
160 * Returns true iff the two distributions will produce identical
161 * sequences of values given equal generators.
163 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(exponential_distribution, lhs, rhs)
164 { return lhs._lambda == rhs._lambda; }
167 * Returns true iff the two distributions will produce different
168 * sequences of values given equal generators.
170 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(exponential_distribution)
176 } // namespace random
178 using random::exponential_distribution;
182 #endif // BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP