]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
Hunted down some output bugs. Hope it can be safely piped into Maple now.
[ginac.git] / ginac / inifcns_gamma.cpp
1 /** @file inifcns_gamma.cpp
2  *
3  *  Implementation of Gamma function and some related stuff. */
4
5 #include <vector>
6 #include <stdexcept>
7
8 #include "ginac.h"
9
10 //////////
11 // gamma function
12 //////////
13
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...
17  *
18  *  @exception fail_numeric("complex_infinity") or something similar... */
19 ex gamma_eval(ex const & x)
20 {
21     if ( x.info(info_flags::numeric) ) {
22
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()));
28             } else {
29                 return numZERO();  // Infinity. Throw? What?
30             }
31         }
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()));
41             } else {
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()));
48             }
49         }
50     }
51     return gamma(x).hold();
52 }    
53     
54 ex gamma_evalf(ex const & x)
55 {
56     BEGIN_TYPECHECK
57         TYPECHECK(x,numeric)
58     END_TYPECHECK(gamma(x))
59     
60     return gamma(ex_to_numeric(x));
61 }
62
63 ex gamma_diff(ex const & x, unsigned diff_param)
64 {
65     ASSERT(diff_param==0);
66
67     return power(x, -1);        //!!
68 }
69
70 ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
71 {
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);
76         } else
77                 throw(std::logic_error("don't know the series expansion of this particular gamma function"));
78 }
79
80 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);