]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
- check program is not built until you say "make check"
[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 "inifcns.h"
26 #include "ex.h"
27 #include "constant.h"
28 #include "numeric.h"
29 #include "power.h"
30 #include "symbol.h"
31
32 //////////
33 // gamma function
34 //////////
35
36 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
37  *  arguments and that's it. Somebody ought to provide some good numerical
38  *  evaluation some day...
39  *
40  *  @exception fail_numeric("complex_infinity") or something similar... */
41 ex gamma_eval(ex const & x)
42 {
43     if ( x.info(info_flags::numeric) ) {
44
45         // trap integer arguments:
46         if ( x.info(info_flags::integer) ) {
47             // gamma(n+1) -> n! for postitive n
48             if ( x.info(info_flags::posint) ) {
49                 return factorial(ex_to_numeric(x).sub(numONE()));
50             } else {
51                 return numZERO();  // Infinity. Throw? What?
52             }
53         }
54         // trap half integer arguments:
55         if ( (x*2).info(info_flags::integer) ) {
56             // trap positive x=(n+1/2)
57             // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
58             if ( (x*2).info(info_flags::posint) ) {
59                 numeric n = ex_to_numeric(x).sub(numHALF());
60                 numeric coefficient = doublefactorial(n.mul(numTWO()).sub(numONE()));
61                 coefficient = coefficient.div(numTWO().power(n));
62                 return coefficient * power(Pi,numHALF());
63             } else {
64                 // trap negative x=(-n+1/2)
65                 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
66                 numeric n = abs(ex_to_numeric(x).sub(numHALF()));
67                 numeric coefficient = numeric(-2).power(n);
68                 coefficient = coefficient.div(doublefactorial(n.mul(numTWO()).sub(numONE())));;
69                 return coefficient * power(Pi,numHALF());
70             }
71         }
72     }
73     return gamma(x).hold();
74 }    
75     
76 ex gamma_evalf(ex const & x)
77 {
78     BEGIN_TYPECHECK
79         TYPECHECK(x,numeric)
80     END_TYPECHECK(gamma(x))
81     
82     return gamma(ex_to_numeric(x));
83 }
84
85 ex gamma_diff(ex const & x, unsigned diff_param)
86 {
87     ASSERT(diff_param==0);
88
89     return power(x, -1);        //!!
90 }
91
92 ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
93 {
94         //!! Only handle one special case for now...
95         if (x.is_equal(s) && point.is_zero()) {
96                 ex e = 1 / s - EulerGamma + s * (power(Pi, 2) / 12 + power(EulerGamma, 2) / 2) + Order(power(s, 2));
97                 return e.series(s, point, order);
98         } else
99                 throw(std::logic_error("don't know the series expansion of this particular gamma function"));
100 }
101
102 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);