1
/* boost random/gamma_distribution.hpp header file
3
* Copyright Jens Maurer 2002
4
* Permission to use, copy, modify, sell, and distribute this software
5
* is hereby granted without fee provided that the above copyright notice
6
* appears in all copies and that both that copyright notice and this
7
* permission notice appear in supporting documentation,
9
* Jens Maurer makes no representations about the suitability of this
10
* software for any purpose. It is provided "as is" without express or
13
* See http://www.boost.org for most recent version including documentation.
15
* $Id: gamma_distribution.hpp,v 1.1 2004/02/27 03:16:46 pseudonym Exp $
19
#ifndef BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
20
#define BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
24
#include <boost/random/uniform_01.hpp>
29
// deterministic polar method, uses trigonometric functions
30
template<class UniformRandomNumberGenerator, class RealType = double,
31
class Adaptor = uniform_01<UniformRandomNumberGenerator, RealType> >
32
class gamma_distribution
35
typedef Adaptor adaptor_type;
36
typedef UniformRandomNumberGenerator base_type;
37
typedef RealType result_type;
39
explicit gamma_distribution(base_type & rng,
40
const result_type& alpha = result_type(1))
41
: _rng(rng), _exp(rng, result_type(1)), _alpha(alpha)
43
assert(alpha > result_type(0));
47
// compiler-generated copy ctor and assignment operator are fine
49
adaptor_type& adaptor() { return _rng; }
50
base_type& base() const { return _rng.base(); }
51
RealType alpha() const { return _alpha; }
53
void reset() { _rng.reset(); _exp.reset(); }
55
result_type operator()()
57
#ifndef BOOST_NO_STDC_NAMESPACE
58
// allow for Koenig lookup
59
using std::tan; using std::sqrt; using std::exp; using std::log;
62
if(_alpha == result_type(1)) {
64
} else if(_alpha > result_type(1)) {
65
// Can we have a boost::mathconst please?
66
const result_type pi = result_type(3.14159265358979323846);
68
result_type y = tan(pi * _rng());
69
result_type x = sqrt(result_type(2)*_alpha-result_type(1))*y
70
+ _alpha-result_type(1);
71
if(x <= result_type(0))
74
(result_type(1)+y*y) * exp((_alpha-result_type(1))
75
*log(x/(_alpha-result_type(1)))
76
- sqrt(result_type(2)*_alpha
81
} else /* alpha < 1.0 */ {
83
result_type u = _rng();
84
result_type y = _exp();
91
q = _p + (result_type(1)-_p) * pow(x, _alpha-result_type(1));
99
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
100
friend bool operator==(const gamma_distribution& x,
101
const gamma_distribution& y)
103
return x._alpha == y._alpha && x._rng == y._rng;
106
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
107
template<class CharT, class Traits>
108
friend std::basic_ostream<CharT,Traits>&
109
operator<<(std::basic_ostream<CharT,Traits>& os, const gamma_distribution& gd)
115
template<class CharT, class Traits>
116
friend std::basic_istream<CharT,Traits>&
117
operator>>(std::basic_istream<CharT,Traits>& is, gamma_distribution& gd)
119
is >> std::ws >> gd._alpha;
126
// Use a member function
127
bool operator==(const gamma_distribution& rhs) const
129
return _alpha == rhs._alpha && _rng == rhs._rng;
136
#ifndef BOOST_NO_STDC_NAMESPACE
137
// allow for Koenig lookup
140
_p = exp(result_type(1)) / (_alpha + exp(result_type(1)));
144
exponential_distribution<base_type, RealType, Adaptor> _exp;
146
// some data precomputed from the parameters
152
#endif // BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP