1 /* boost random/gamma_distribution.hpp header file
3 * Copyright Jens Maurer 2002
4 * Distributed under the Boost Software License, Version 1.0. (See
5 * accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org for most recent version including documentation.
14 #ifndef BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
15 #define BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
17 #include <boost/config/no_tr1/cmath.hpp>
19 #include <boost/limits.hpp>
20 #include <boost/static_assert.hpp>
21 #include <boost/random/detail/config.hpp>
22 #include <boost/random/exponential_distribution.hpp>
27 template<class RealType
= double>
28 class gamma_distribution
31 typedef RealType input_type
;
32 typedef RealType result_type
;
34 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
35 BOOST_STATIC_ASSERT(!std::numeric_limits
<RealType
>::is_integer
);
38 explicit gamma_distribution(const result_type
& alpha_arg
= result_type(1))
39 : _exp(result_type(1)), _alpha(alpha_arg
)
41 assert(_alpha
> result_type(0));
45 // compiler-generated copy ctor and assignment operator are fine
47 RealType
alpha() const { return _alpha
; }
49 void reset() { _exp
.reset(); }
51 template<class Engine
>
52 result_type
operator()(Engine
& eng
)
54 #ifndef BOOST_NO_STDC_NAMESPACE
55 // allow for Koenig lookup
56 using std::tan
; using std::sqrt
; using std::exp
; using std::log
;
59 if(_alpha
== result_type(1)) {
61 } else if(_alpha
> result_type(1)) {
62 // Can we have a boost::mathconst please?
63 const result_type pi
= result_type(3.14159265358979323846);
65 result_type y
= tan(pi
* eng());
66 result_type x
= sqrt(result_type(2)*_alpha
-result_type(1))*y
67 + _alpha
-result_type(1);
68 if(x
<= result_type(0))
71 (result_type(1)+y
*y
) * exp((_alpha
-result_type(1))
72 *log(x
/(_alpha
-result_type(1)))
73 - sqrt(result_type(2)*_alpha
78 } else /* alpha < 1.0 */ {
80 result_type u
= eng();
81 result_type y
= _exp(eng
);
88 q
= _p
+ (result_type(1)-_p
) * pow(x
, _alpha
-result_type(1));
97 #ifndef BOOST_RANDOM_NO_STREAM_OPERATORS
98 template<class CharT
, class Traits
>
99 friend std::basic_ostream
<CharT
,Traits
>&
100 operator<<(std::basic_ostream
<CharT
,Traits
>& os
, const gamma_distribution
& gd
)
106 template<class CharT
, class Traits
>
107 friend std::basic_istream
<CharT
,Traits
>&
108 operator>>(std::basic_istream
<CharT
,Traits
>& is
, gamma_distribution
& gd
)
110 is
>> std::ws
>> gd
._alpha
;
119 #ifndef BOOST_NO_STDC_NAMESPACE
120 // allow for Koenig lookup
123 _p
= exp(result_type(1)) / (_alpha
+ exp(result_type(1)));
126 exponential_distribution
<RealType
> _exp
;
128 // some data precomputed from the parameters
134 #endif // BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP