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