1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma function and some related stuff. */
14 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
15 * arguments and that's it. Somebody ought to provide some good numerical
16 * evaluation some day...
18 * @exception fail_numeric("complex_infinity") or something similar... */
19 ex gamma_eval(ex const & x)
21 if ( x.info(info_flags::numeric) ) {
23 // trap integer arguments:
24 if ( x.info(info_flags::integer) ) {
25 // gamma(n+1) -> n! for postitive n
26 if ( x.info(info_flags::posint) ) {
27 return factorial(ex_to_numeric(x).sub(numONE()));
29 return numZERO(); // Infinity. Throw? What?
32 // trap half integer arguments:
33 if ( (x*2).info(info_flags::integer) ) {
34 // trap positive x=(n+1/2)
35 // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
36 if ( (x*2).info(info_flags::posint) ) {
37 numeric n = ex_to_numeric(x).sub(numHALF());
38 numeric coefficient = doublefactorial(n.mul(numTWO()).sub(numONE()));
39 coefficient = coefficient.div(numTWO().power(n));
40 return mul(coefficient,power(Pi,numHALF()));
42 // trap negative x=(-n+1/2)
43 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
44 numeric n = abs(ex_to_numeric(x).sub(numHALF()));
45 numeric coefficient = numeric(-2).power(n);
46 coefficient = coefficient.div(doublefactorial(n.mul(numTWO()).sub(numONE())));;
47 return mul(coefficient,power(Pi,numHALF()));
51 return gamma(x).hold();
54 ex gamma_evalf(ex const & x)
58 END_TYPECHECK(gamma(x))
60 return gamma(ex_to_numeric(x));
63 ex gamma_diff(ex const & x, unsigned diff_param)
65 ASSERT(diff_param==0);
67 return power(x, -1); //!!
70 ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
72 //!! Only handle one special case for now...
73 if (x.is_equal(s) && point.is_zero()) {
74 ex e = 1 / s - EulerGamma + s * (power(Pi, 2) / 12 + power(EulerGamma, 2) / 2) + Order(power(s, 2));
75 return e.series(s, point, order);
77 throw(std::logic_error("don't know the series expansion of this particular gamma function"));
80 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);