Honeycomb  0.1
Component-Model Framework
Gamma.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 
6 namespace honey
7 {
8 
9 template<class Real> class GammaFunc_;
10 template<class Real> class GammaInc;
11 
13 
28 template<class Real>
29 class Gamma_ : public RandomDist<Real>
30 {
31  typedef RandomDist<Real> Super;
32  RandomDist_imports();
33  typedef Trig_<Real> Trig;
36  typedef GammaInc<Double> GammaInc;
37 
38 public:
39  Gamma_(optional<RandomGen&> gen, Real a, Real b) : Super(gen), a(a), b(b) { assert(a > 0 && b > 0); }
40  Gamma_(Real a, Real b) : Gamma_(optnull, a, b) {}
41 
42  virtual Real next() const;
43  virtual Real pdf(Real x) const;
44  virtual Real cdf(Real x) const;
45  virtual Real cdfComp(Real x) const;
46  virtual Real cdfInv(Real P) const;
47  virtual Real mean() const { return a*b; }
48  virtual Real variance() const { return a*Alge::sqr(b); }
49 
52 };
53 
57 
58 extern template class Gamma_<Float>;
59 extern template class Gamma_<Double>;
60 
61 
63 template<class Real>
64 class GammaFunc_
65 {
66  typedef typename Numeral<Real>::Real_ Real_;
67  typedef typename Real_::DoubleType Double_;
68  typedef typename Double_::Real Double;
69  typedef Alge_<Real> Alge;
70 public:
72  static Real gamma(Real z) { return Alge::exp(gammaLn(z)); }
74  static Real gammaLn(Real z);
76 
79  static Real factorial(Real n);
81  static Real factorialLn(Real n);
83  static Real choose(Real n, Real m) { return Alge::exp(chooseLn(n,m)); }
85  static Real chooseLn(Real n, Real m);
86 private:
87  static Real series(Real z);
88  static Real asymp(Real z);
89  static Real gValue() { return 10.0; }
90  static Real lanczos(Real z);
91  static Real near1(Real z);
92  static Real near2(Real z);
93  static Real gt1(Real z);
94  static Real gammaLn(Real z, int& sign);
95 
96  static const int factorialTableSize;
97  static const Double factorialTable[];
98 };
99 
103 
104 extern template class GammaFunc_<Float>;
105 extern template class GammaFunc_<Double>;
106 
107 }
Double_::Real Double
double type
Definition: Double.h:60
Algebra.
Definition: Alge.h:13
Gamma_< Real > Gamma
Definition: Gamma.h:54
static Real choose(Real n, Real m)
Number of ways of choosing m objects from n distinct objects. "N choose M". Both N and M can be fract...
Definition: Gamma.h:83
Real a
Definition: Gamma.h:50
static optnull_t optnull
Null optional, use to reset an optional to an uninitialized state or test for initialization.
Definition: Optional.h:12
Defines 64-bit floating point operations and constants.
Definition: Double.h:10
Generate a random variate from a gamma distribution .
Definition: Gamma.h:29
GammaFunc_< Double > GammaFunc_d
Definition: Gamma.h:102
Gamma_(Real a, Real b)
Definition: Gamma.h:40
Gamma_< Double > Gamma_d
Definition: Gamma.h:56
Class to evaluate Gamma and related functions.
Definition: Gamma.h:9
static Real exp(Real x)
Euler's number e raised to exponent x (e^x)
Definition: Alge.h:68
Real b
Definition: Gamma.h:51
GammaFunc_< Real > GammaFunc
Definition: Gamma.h:100
Alge_< Real > Alge
Definition: Alge.h:130
virtual Real cdfComp(Real x) const
Complement of the CDF.
Definition: Gamma.cpp:752
static Real gamma(Real z)
Gamma function.
Definition: Gamma.h:72
Gamma_(optional< RandomGen & > gen, Real a, Real b)
Definition: Gamma.h:39
Value< int64,(val< 0)?-1:1 > sign
Get the sign of a number.
Definition: Meta.h:309
virtual Real cdfInv(Real P) const
Inverse of the CDF.
Definition: Gamma.cpp:758
virtual Real variance() const
Calc variance.
Definition: Gamma.h:48
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
virtual Real cdf(Real x) const
Cumulative Distribution Function.
Definition: Gamma.cpp:746
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
Enables any type to be optional so it can exist in an uninitialized null state.
Definition: Optional.h:52
Gamma_< Float > Gamma_f
Definition: Gamma.h:55
double Real
Definition: Real.h:14
Numeral< Real >::Real_ Real_
Operations and constants for Real type. See Float_, Double_.
Definition: Real.h:25
static Num sqr(Num x)
Square.
Definition: Alge.h:60
virtual Real next() const
Get next randomly distributed variate. Requires a random generator (see ctor or setGen()) ...
Definition: Gamma.cpp:11
virtual Real pdf(Real x) const
Probability Density Function.
Definition: Gamma.cpp:48
virtual Real mean() const
Calc mean.
Definition: Gamma.h:47
Generate a normally (Gaussian) distributed random variate.
Definition: Gaussian.h:26
Global Honeycomb namespace.
GammaFunc_< Float > GammaFunc_f
Definition: Gamma.h:101
Trigonometry.
Definition: Trig.h:52
Base class for all random distributions.
Definition: Dist.h:15