]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
- enforced GiNaC coding standards :-)
[ginac.git] / ginac / inifcns_gamma.cpp
1 /** @file inifcns_gamma.cpp
2  *
3  *  Implementation of Gamma function and some related stuff.
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <vector>
23 #include <stdexcept>
24
25 #include "ginac.h"
26
27 //////////
28 // gamma function
29 //////////
30
31 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
32  *  arguments and that's it. Somebody ought to provide some good numerical
33  *  evaluation some day...
34  *
35  *  @exception fail_numeric("complex_infinity") or something similar... */
36 ex gamma_eval(ex const & x)
37 {
38     if ( x.info(info_flags::numeric) ) {
39
40         // trap integer arguments:
41         if ( x.info(info_flags::integer) ) {
42             // gamma(n+1) -> n! for postitive n
43             if ( x.info(info_flags::posint) ) {
44                 return factorial(ex_to_numeric(x).sub(numONE()));
45             } else {
46                 return numZERO();  // Infinity. Throw? What?
47             }
48         }
49         // trap half integer arguments:
50         if ( (x*2).info(info_flags::integer) ) {
51             // trap positive x=(n+1/2)
52             // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
53             if ( (x*2).info(info_flags::posint) ) {
54                 numeric n = ex_to_numeric(x).sub(numHALF());
55                 numeric coefficient = doublefactorial(n.mul(numTWO()).sub(numONE()));
56                 coefficient = coefficient.div(numTWO().power(n));
57                 return mul(coefficient,power(Pi,numHALF()));
58             } else {
59                 // trap negative x=(-n+1/2)
60                 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
61                 numeric n = abs(ex_to_numeric(x).sub(numHALF()));
62                 numeric coefficient = numeric(-2).power(n);
63                 coefficient = coefficient.div(doublefactorial(n.mul(numTWO()).sub(numONE())));;
64                 return mul(coefficient,power(Pi,numHALF()));
65             }
66         }
67     }
68     return gamma(x).hold();
69 }    
70     
71 ex gamma_evalf(ex const & x)
72 {
73     BEGIN_TYPECHECK
74         TYPECHECK(x,numeric)
75     END_TYPECHECK(gamma(x))
76     
77     return gamma(ex_to_numeric(x));
78 }
79
80 ex gamma_diff(ex const & x, unsigned diff_param)
81 {
82     ASSERT(diff_param==0);
83
84     return power(x, -1);        //!!
85 }
86
87 ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
88 {
89         //!! Only handle one special case for now...
90         if (x.is_equal(s) && point.is_zero()) {
91                 ex e = 1 / s - EulerGamma + s * (power(Pi, 2) / 12 + power(EulerGamma, 2) / 2) + Order(power(s, 2));
92                 return e.series(s, point, order);
93         } else
94                 throw(std::logic_error("don't know the series expansion of this particular gamma function"));
95 }
96
97 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);