Theoretica
A C++ numerical and automatic mathematical library
special.h
Go to the documentation of this file.
1 
5 
6 #ifndef THEORETICA_SPECIAL_H
7 #define THEORETICA_SPECIAL_H
8 
9 #include "constants.h"
10 #include "real_analysis.h"
11 
12 
13 namespace theoretica {
14 
16  namespace special {
17 
18 
23  inline real gamma(unsigned int k) {
24 
25  if(k == 0) {
26  TH_MATH_ERROR("gamma", k, OUT_OF_DOMAIN);
27  return nan();
28  }
29 
30  return fact(k - 1);
31  }
32 
33 
41  inline real half_gamma(unsigned int k) {
42 
43  if(k == 0) {
44  TH_MATH_ERROR("half_gamma", k, OUT_OF_DOMAIN);
45  return nan();
46  }
47 
48  return (k % 2 == 0)
49  ? fact<uint64_t>(k / 2 - 1)
50  : double_fact<uint64_t>(k - 2) * SQRTPI / (1 << ((k - 1) / 2));
51  }
52 
53 
59  inline real lngamma(real x) {
60 
61  // Reflection formula for negative values
62  if(x < 0) {
63 
64  // Check for negative values of Gamma(x)
65  if(floor(-x) % 2 == 0) {
66  TH_MATH_ERROR("lngamma", x, OUT_OF_DOMAIN);
67  return nan();
68  }
69 
70  return ln(PI / sin(PI * x)) - lngamma(1 - x);
71  }
72 
73  // Lanczos' coefficients
74  const real c[7] = {
75  1.000000000178,
76  76.180091729400,
77  -86.505320327112,
78  24.014098222230,
79  -1.231739516140,
80  0.001208580030,
81  -0.000005363820
82  };
83 
84  // Simplified logarithmic formula
85  // for Lanczos' approximation
86  real A5 = c[0];
87 
88  for (int i = 1; i < 7; ++i)
89  A5 += c[i] / (x + i - 1);
90 
91  return (x - 0.5) * (ln(x + 4.5) - 1)
92  - 5 + ln(SQRTPI * SQRT2 * A5);
93  }
94 
95 
101  inline real gamma(real x) {
102 
103  const real x_fract = fract(x);
104 
105  // Check if x is a pole or an integer number
106  if(x_fract < MACH_EPSILON) {
107 
108  if(x <= 0) {
109  TH_MATH_ERROR("gamma", x, OUT_OF_DOMAIN);
110  return inf();
111  } else
112  return gamma((unsigned int) x);
113  }
114 
115  // Check for negative values of Gamma(x)
116  // and use the translation identity
117  if(x < 0 && floor(-x) % 2 == 0)
118  return exp(lngamma(x + 1)) / x;
119 
120  // Compute the Gamma function as the exponential
121  // of the log Gamma function which uses Lanczos'
122  // approximation
123  return exp(lngamma(x));
124  }
125 
126 
131  inline real pi(real x) {
132  return gamma(x + 1);
133  }
134 
135 
141  inline real beta(real x1, real x2) {
142 
143  return exp(lngamma(x1) + lngamma(x2) - lngamma(x1 + x2));
144  }
145 
146  }
147 
148 }
149 
150 #endif
Mathematical constants and default algorithm parameters.
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition: error.h:219
real beta(real x1, real x2)
Beta special function of real argument.
Definition: special.h:141
real pi(real x)
Pi special function of real argument.
Definition: special.h:131
real half_gamma(unsigned int k)
Half Gamma special function, defined as HG(n) = Gamma(n / 2) for any positive integer n.
Definition: special.h:41
real lngamma(real x)
Log Gamma special function of real argument.
Definition: special.h:59
real gamma(unsigned int k)
Gamma special function of positive integer argument.
Definition: special.h:23
Main namespace of the library which contains all functions and objects.
Definition: algebra.h:27
double real
A real number, defined as a floating point type.
Definition: constants.h:188
dual2 ln(dual2 x)
Compute the natural logarithm of a second order dual number.
Definition: dual2_functions.h:139
real inf()
Return positive infinity in floating point representation.
Definition: error.h:76
constexpr real SQRTPI
The square root of Pi.
Definition: constants.h:224
dual2 exp(dual2 x)
Compute the exponential of a second order dual number.
Definition: dual2_functions.h:130
real fract(real x)
Compute the fractional part of a real number.
Definition: real_analysis.h:288
TH_CONSTEXPR IntType fact(unsigned int n)
Compute the factorial of n.
Definition: real_analysis.h:671
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition: constants.h:197
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition: dual2_functions.h:70
constexpr real SQRT2
The square root of 2.
Definition: constants.h:251
constexpr real PI
The Pi mathematical constant.
Definition: constants.h:206
real nan()
Return a quiet NaN number in floating point representation.
Definition: error.h:54
TH_CONSTEXPR int floor(real x)
Compute the floor of x Computes the maximum integer number that is smaller than x.
Definition: real_analysis.h:271
Real functions.