Chris@16
|
1 /* boost random/exponential_distribution.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Jens Maurer 2000-2001
|
Chris@16
|
4 * Copyright Steven Watanabe 2011
|
Chris@16
|
5 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
6 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 *
|
Chris@16
|
9 * See http://www.boost.org for most recent version including documentation.
|
Chris@16
|
10 *
|
Chris@101
|
11 * $Id$
|
Chris@16
|
12 *
|
Chris@16
|
13 * Revision history
|
Chris@16
|
14 * 2001-02-18 moved to individual header files
|
Chris@16
|
15 */
|
Chris@16
|
16
|
Chris@16
|
17 #ifndef BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
|
Chris@16
|
18 #define BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
|
Chris@16
|
19
|
Chris@16
|
20 #include <boost/config/no_tr1/cmath.hpp>
|
Chris@16
|
21 #include <iosfwd>
|
Chris@16
|
22 #include <boost/assert.hpp>
|
Chris@16
|
23 #include <boost/limits.hpp>
|
Chris@16
|
24 #include <boost/random/detail/config.hpp>
|
Chris@16
|
25 #include <boost/random/detail/operators.hpp>
|
Chris@16
|
26 #include <boost/random/uniform_01.hpp>
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost {
|
Chris@16
|
29 namespace random {
|
Chris@16
|
30
|
Chris@16
|
31 /**
|
Chris@16
|
32 * The exponential distribution is a model of \random_distribution with
|
Chris@16
|
33 * a single parameter lambda.
|
Chris@16
|
34 *
|
Chris@16
|
35 * It has \f$\displaystyle p(x) = \lambda e^{-\lambda x}\f$
|
Chris@16
|
36 */
|
Chris@16
|
37 template<class RealType = double>
|
Chris@16
|
38 class exponential_distribution
|
Chris@16
|
39 {
|
Chris@16
|
40 public:
|
Chris@16
|
41 typedef RealType input_type;
|
Chris@16
|
42 typedef RealType result_type;
|
Chris@16
|
43
|
Chris@16
|
44 class param_type
|
Chris@16
|
45 {
|
Chris@16
|
46 public:
|
Chris@16
|
47
|
Chris@16
|
48 typedef exponential_distribution distribution_type;
|
Chris@16
|
49
|
Chris@16
|
50 /**
|
Chris@16
|
51 * Constructs parameters with a given lambda.
|
Chris@16
|
52 *
|
Chris@16
|
53 * Requires: lambda > 0
|
Chris@16
|
54 */
|
Chris@16
|
55 param_type(RealType lambda_arg = RealType(1.0))
|
Chris@16
|
56 : _lambda(lambda_arg) { BOOST_ASSERT(_lambda > RealType(0)); }
|
Chris@16
|
57
|
Chris@16
|
58 /** Returns the lambda parameter of the distribution. */
|
Chris@16
|
59 RealType lambda() const { return _lambda; }
|
Chris@16
|
60
|
Chris@16
|
61 /** Writes the parameters to a @c std::ostream. */
|
Chris@16
|
62 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, param_type, parm)
|
Chris@16
|
63 {
|
Chris@16
|
64 os << parm._lambda;
|
Chris@16
|
65 return os;
|
Chris@16
|
66 }
|
Chris@16
|
67
|
Chris@16
|
68 /** Reads the parameters from a @c std::istream. */
|
Chris@16
|
69 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, param_type, parm)
|
Chris@16
|
70 {
|
Chris@16
|
71 is >> parm._lambda;
|
Chris@16
|
72 return is;
|
Chris@16
|
73 }
|
Chris@16
|
74
|
Chris@16
|
75 /** Returns true if the two sets of parameters are equal. */
|
Chris@16
|
76 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(param_type, lhs, rhs)
|
Chris@16
|
77 { return lhs._lambda == rhs._lambda; }
|
Chris@16
|
78
|
Chris@16
|
79 /** Returns true if the two sets of parameters are different. */
|
Chris@16
|
80 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(param_type)
|
Chris@16
|
81
|
Chris@16
|
82 private:
|
Chris@16
|
83 RealType _lambda;
|
Chris@16
|
84 };
|
Chris@16
|
85
|
Chris@16
|
86 /**
|
Chris@16
|
87 * Constructs an exponential_distribution with a given lambda.
|
Chris@16
|
88 *
|
Chris@16
|
89 * Requires: lambda > 0
|
Chris@16
|
90 */
|
Chris@16
|
91 explicit exponential_distribution(RealType lambda_arg = RealType(1.0))
|
Chris@16
|
92 : _lambda(lambda_arg) { BOOST_ASSERT(_lambda > RealType(0)); }
|
Chris@16
|
93
|
Chris@16
|
94 /**
|
Chris@16
|
95 * Constructs an exponential_distribution from its parameters
|
Chris@16
|
96 */
|
Chris@16
|
97 explicit exponential_distribution(const param_type& parm)
|
Chris@16
|
98 : _lambda(parm.lambda()) {}
|
Chris@16
|
99
|
Chris@16
|
100 // compiler-generated copy ctor and assignment operator are fine
|
Chris@16
|
101
|
Chris@16
|
102 /** Returns the lambda parameter of the distribution. */
|
Chris@16
|
103 RealType lambda() const { return _lambda; }
|
Chris@16
|
104
|
Chris@16
|
105 /** Returns the smallest value that the distribution can produce. */
|
Chris@16
|
106 RealType min BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
Chris@16
|
107 { return RealType(0); }
|
Chris@16
|
108 /** Returns the largest value that the distribution can produce. */
|
Chris@16
|
109 RealType max BOOST_PREVENT_MACRO_SUBSTITUTION () const
|
Chris@16
|
110 { return (std::numeric_limits<RealType>::infinity)(); }
|
Chris@16
|
111
|
Chris@16
|
112 /** Returns the parameters of the distribution. */
|
Chris@16
|
113 param_type param() const { return param_type(_lambda); }
|
Chris@16
|
114 /** Sets the parameters of the distribution. */
|
Chris@16
|
115 void param(const param_type& parm) { _lambda = parm.lambda(); }
|
Chris@16
|
116
|
Chris@16
|
117 /**
|
Chris@16
|
118 * Effects: Subsequent uses of the distribution do not depend
|
Chris@16
|
119 * on values produced by any engine prior to invoking reset.
|
Chris@16
|
120 */
|
Chris@16
|
121 void reset() { }
|
Chris@16
|
122
|
Chris@16
|
123 /**
|
Chris@16
|
124 * Returns a random variate distributed according to the
|
Chris@16
|
125 * exponential distribution.
|
Chris@16
|
126 */
|
Chris@16
|
127 template<class Engine>
|
Chris@16
|
128 result_type operator()(Engine& eng) const
|
Chris@16
|
129 {
|
Chris@16
|
130 using std::log;
|
Chris@16
|
131 return -result_type(1) /
|
Chris@16
|
132 _lambda * log(result_type(1)-uniform_01<RealType>()(eng));
|
Chris@16
|
133 }
|
Chris@16
|
134
|
Chris@16
|
135 /**
|
Chris@16
|
136 * Returns a random variate distributed according to the exponential
|
Chris@16
|
137 * distribution with parameters specified by param.
|
Chris@16
|
138 */
|
Chris@16
|
139 template<class Engine>
|
Chris@16
|
140 result_type operator()(Engine& eng, const param_type& parm) const
|
Chris@16
|
141 {
|
Chris@16
|
142 return exponential_distribution(parm)(eng);
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 /** Writes the distribution to a std::ostream. */
|
Chris@16
|
146 BOOST_RANDOM_DETAIL_OSTREAM_OPERATOR(os, exponential_distribution, ed)
|
Chris@16
|
147 {
|
Chris@16
|
148 os << ed._lambda;
|
Chris@16
|
149 return os;
|
Chris@16
|
150 }
|
Chris@16
|
151
|
Chris@16
|
152 /** Reads the distribution from a std::istream. */
|
Chris@16
|
153 BOOST_RANDOM_DETAIL_ISTREAM_OPERATOR(is, exponential_distribution, ed)
|
Chris@16
|
154 {
|
Chris@16
|
155 is >> ed._lambda;
|
Chris@16
|
156 return is;
|
Chris@16
|
157 }
|
Chris@16
|
158
|
Chris@16
|
159 /**
|
Chris@16
|
160 * Returns true iff the two distributions will produce identical
|
Chris@16
|
161 * sequences of values given equal generators.
|
Chris@16
|
162 */
|
Chris@16
|
163 BOOST_RANDOM_DETAIL_EQUALITY_OPERATOR(exponential_distribution, lhs, rhs)
|
Chris@16
|
164 { return lhs._lambda == rhs._lambda; }
|
Chris@16
|
165
|
Chris@16
|
166 /**
|
Chris@16
|
167 * Returns true iff the two distributions will produce different
|
Chris@16
|
168 * sequences of values given equal generators.
|
Chris@16
|
169 */
|
Chris@16
|
170 BOOST_RANDOM_DETAIL_INEQUALITY_OPERATOR(exponential_distribution)
|
Chris@16
|
171
|
Chris@16
|
172 private:
|
Chris@16
|
173 result_type _lambda;
|
Chris@16
|
174 };
|
Chris@16
|
175
|
Chris@16
|
176 } // namespace random
|
Chris@16
|
177
|
Chris@16
|
178 using random::exponential_distribution;
|
Chris@16
|
179
|
Chris@16
|
180 } // namespace boost
|
Chris@16
|
181
|
Chris@16
|
182 #endif // BOOST_RANDOM_EXPONENTIAL_DISTRIBUTION_HPP
|