~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/random/gamma_distribution.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* boost random/gamma_distribution.hpp header file
 
2
 *
 
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,
 
8
 *
 
9
 * Jens Maurer makes no representations about the suitability of this
 
10
 * software for any purpose. It is provided "as is" without express or
 
11
 * implied warranty.
 
12
 *
 
13
 * See http://www.boost.org for most recent version including documentation.
 
14
 *
 
15
 * $Id: gamma_distribution.hpp,v 1.1 2004/02/27 03:16:46 pseudonym Exp $
 
16
 *
 
17
 */
 
18
 
 
19
#ifndef BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
 
20
#define BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
 
21
 
 
22
#include <cmath>
 
23
#include <cassert>
 
24
#include <boost/random/uniform_01.hpp>
 
25
 
 
26
namespace boost {
 
27
 
 
28
// Knuth
 
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
 
33
{
 
34
public:
 
35
  typedef Adaptor adaptor_type;
 
36
  typedef UniformRandomNumberGenerator base_type;
 
37
  typedef RealType result_type;
 
38
 
 
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)
 
42
  {
 
43
    assert(alpha > result_type(0));
 
44
    init();
 
45
  }
 
46
 
 
47
  // compiler-generated copy ctor and assignment operator are fine
 
48
 
 
49
  adaptor_type& adaptor() { return _rng; }
 
50
  base_type& base() const { return _rng.base(); }
 
51
  RealType alpha() const { return _alpha; }
 
52
 
 
53
  void reset() { _rng.reset(); _exp.reset(); }
 
54
 
 
55
  result_type operator()()
 
56
  {
 
57
#ifndef BOOST_NO_STDC_NAMESPACE
 
58
    // allow for Koenig lookup
 
59
    using std::tan; using std::sqrt; using std::exp; using std::log;
 
60
    using std::pow;
 
61
#endif
 
62
    if(_alpha == result_type(1)) {
 
63
      return _exp();
 
64
    } else if(_alpha > result_type(1)) {
 
65
      // Can we have a boost::mathconst please?
 
66
      const result_type pi = result_type(3.14159265358979323846);
 
67
      for(;;) {
 
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))
 
72
          continue;
 
73
        if(_rng() >
 
74
           (result_type(1)+y*y) * exp((_alpha-result_type(1))
 
75
                                        *log(x/(_alpha-result_type(1)))
 
76
                                        - sqrt(result_type(2)*_alpha
 
77
                                               -result_type(1))*y))
 
78
          continue;
 
79
        return x;
 
80
      }
 
81
    } else /* alpha < 1.0 */ {
 
82
      for(;;) {
 
83
        result_type u = _rng();
 
84
        result_type y = _exp();
 
85
        result_type x, q;
 
86
        if(u < _p) {
 
87
          x = exp(-y/_alpha);
 
88
          q = _p*exp(-x);
 
89
        } else {
 
90
          x = result_type(1)+y;
 
91
          q = _p + (result_type(1)-_p) * pow(x, _alpha-result_type(1));
 
92
        }
 
93
        if(u >= q)
 
94
          continue;
 
95
        return x;
 
96
      }
 
97
    }
 
98
  }
 
99
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
 
100
  friend bool operator==(const gamma_distribution& x, 
 
101
                         const gamma_distribution& y)
 
102
  {
 
103
    return x._alpha == y._alpha && x._rng == y._rng;
 
104
  }
 
105
 
 
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)
 
110
  {
 
111
    os << gd._alpha;
 
112
    return os;
 
113
  }
 
114
 
 
115
  template<class CharT, class Traits>
 
116
  friend std::basic_istream<CharT,Traits>&
 
117
  operator>>(std::basic_istream<CharT,Traits>& is, gamma_distribution& gd)
 
118
  {
 
119
    is >> std::ws >> gd._alpha;
 
120
    gd.init();
 
121
    return is;
 
122
  }
 
123
#endif
 
124
 
 
125
#else
 
126
  // Use a member function
 
127
  bool operator==(const gamma_distribution& rhs) const
 
128
  {
 
129
    return _alpha == rhs._alpha && _rng == rhs._rng;
 
130
  }
 
131
#endif
 
132
 
 
133
private:
 
134
  void init()
 
135
  {
 
136
#ifndef BOOST_NO_STDC_NAMESPACE
 
137
    // allow for Koenig lookup
 
138
    using std::exp;
 
139
#endif
 
140
    _p = exp(result_type(1)) / (_alpha + exp(result_type(1)));
 
141
  }
 
142
 
 
143
  adaptor_type _rng;
 
144
  exponential_distribution<base_type, RealType, Adaptor> _exp;
 
145
  result_type _alpha;
 
146
  // some data precomputed from the parameters
 
147
  result_type _p;
 
148
};
 
149
 
 
150
} // namespace boost
 
151
 
 
152
#endif // BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP