os/ossrv/ossrv_pub/boost_apis/boost/random/gamma_distribution.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* boost random/gamma_distribution.hpp header file
sl@0
     2
 *
sl@0
     3
 * Copyright Jens Maurer 2002
sl@0
     4
 * Distributed under the Boost Software License, Version 1.0. (See
sl@0
     5
 * accompanying file LICENSE_1_0.txt or copy at
sl@0
     6
 * http://www.boost.org/LICENSE_1_0.txt)
sl@0
     7
 *
sl@0
     8
 * See http://www.boost.org for most recent version including documentation.
sl@0
     9
 *
sl@0
    10
 * $Id: gamma_distribution.hpp,v 1.9 2004/07/27 03:43:32 dgregor Exp $
sl@0
    11
 *
sl@0
    12
 */
sl@0
    13
sl@0
    14
#ifndef BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
sl@0
    15
#define BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP
sl@0
    16
sl@0
    17
#include <cmath>
sl@0
    18
#include <cassert>
sl@0
    19
#include <boost/limits.hpp>
sl@0
    20
#include <boost/static_assert.hpp>
sl@0
    21
#include <boost/random/exponential_distribution.hpp>
sl@0
    22
sl@0
    23
namespace boost {
sl@0
    24
sl@0
    25
// Knuth
sl@0
    26
template<class RealType = double>
sl@0
    27
class gamma_distribution
sl@0
    28
{
sl@0
    29
public:
sl@0
    30
  typedef RealType input_type;
sl@0
    31
  typedef RealType result_type;
sl@0
    32
sl@0
    33
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
sl@0
    34
  BOOST_STATIC_ASSERT(!std::numeric_limits<RealType>::is_integer);
sl@0
    35
#endif
sl@0
    36
sl@0
    37
  explicit gamma_distribution(const result_type& alpha = result_type(1))
sl@0
    38
    : _exp(result_type(1)), _alpha(alpha)
sl@0
    39
  {
sl@0
    40
    assert(alpha > result_type(0));
sl@0
    41
    init();
sl@0
    42
  }
sl@0
    43
sl@0
    44
  // compiler-generated copy ctor and assignment operator are fine
sl@0
    45
sl@0
    46
  RealType alpha() const { return _alpha; }
sl@0
    47
sl@0
    48
  void reset() { _exp.reset(); }
sl@0
    49
sl@0
    50
  template<class Engine>
sl@0
    51
  result_type operator()(Engine& eng)
sl@0
    52
  {
sl@0
    53
#ifndef BOOST_NO_STDC_NAMESPACE
sl@0
    54
    // allow for Koenig lookup
sl@0
    55
    using std::tan; using std::sqrt; using std::exp; using std::log;
sl@0
    56
    using std::pow;
sl@0
    57
#endif
sl@0
    58
    if(_alpha == result_type(1)) {
sl@0
    59
      return _exp(eng);
sl@0
    60
    } else if(_alpha > result_type(1)) {
sl@0
    61
      // Can we have a boost::mathconst please?
sl@0
    62
      const result_type pi = result_type(3.14159265358979323846);
sl@0
    63
      for(;;) {
sl@0
    64
        result_type y = tan(pi * eng());
sl@0
    65
        result_type x = sqrt(result_type(2)*_alpha-result_type(1))*y
sl@0
    66
          + _alpha-result_type(1);
sl@0
    67
        if(x <= result_type(0))
sl@0
    68
          continue;
sl@0
    69
        if(eng() >
sl@0
    70
           (result_type(1)+y*y) * exp((_alpha-result_type(1))
sl@0
    71
                                        *log(x/(_alpha-result_type(1)))
sl@0
    72
                                        - sqrt(result_type(2)*_alpha
sl@0
    73
                                               -result_type(1))*y))
sl@0
    74
          continue;
sl@0
    75
        return x;
sl@0
    76
      }
sl@0
    77
    } else /* alpha < 1.0 */ {
sl@0
    78
      for(;;) {
sl@0
    79
        result_type u = eng();
sl@0
    80
        result_type y = _exp(eng);
sl@0
    81
        result_type x, q;
sl@0
    82
        if(u < _p) {
sl@0
    83
          x = exp(-y/_alpha);
sl@0
    84
          q = _p*exp(-x);
sl@0
    85
        } else {
sl@0
    86
          x = result_type(1)+y;
sl@0
    87
          q = _p + (result_type(1)-_p) * pow(x, _alpha-result_type(1));
sl@0
    88
        }
sl@0
    89
        if(u >= q)
sl@0
    90
          continue;
sl@0
    91
        return x;
sl@0
    92
      }
sl@0
    93
    }
sl@0
    94
  }
sl@0
    95
sl@0
    96
#if !defined(BOOST_NO_OPERATORS_IN_NAMESPACE) && !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
sl@0
    97
  template<class CharT, class Traits>
sl@0
    98
  friend std::basic_ostream<CharT,Traits>&
sl@0
    99
  operator<<(std::basic_ostream<CharT,Traits>& os, const gamma_distribution& gd)
sl@0
   100
  {
sl@0
   101
    os << gd._alpha;
sl@0
   102
    return os;
sl@0
   103
  }
sl@0
   104
sl@0
   105
  template<class CharT, class Traits>
sl@0
   106
  friend std::basic_istream<CharT,Traits>&
sl@0
   107
  operator>>(std::basic_istream<CharT,Traits>& is, gamma_distribution& gd)
sl@0
   108
  {
sl@0
   109
    is >> std::ws >> gd._alpha;
sl@0
   110
    gd.init();
sl@0
   111
    return is;
sl@0
   112
  }
sl@0
   113
#endif
sl@0
   114
sl@0
   115
private:
sl@0
   116
  void init()
sl@0
   117
  {
sl@0
   118
#ifndef BOOST_NO_STDC_NAMESPACE
sl@0
   119
    // allow for Koenig lookup
sl@0
   120
    using std::exp;
sl@0
   121
#endif
sl@0
   122
    _p = exp(result_type(1)) / (_alpha + exp(result_type(1)));
sl@0
   123
  }
sl@0
   124
sl@0
   125
  exponential_distribution<RealType> _exp;
sl@0
   126
  result_type _alpha;
sl@0
   127
  // some data precomputed from the parameters
sl@0
   128
  result_type _p;
sl@0
   129
};
sl@0
   130
sl@0
   131
} // namespace boost
sl@0
   132
sl@0
   133
#endif // BOOST_RANDOM_GAMMA_DISTRIBUTION_HPP