1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma function and some related stuff.
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
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...
40 * @exception fail_numeric("complex_infinity") or something similar... */
41 ex gamma_eval(ex const & x)
43 if ( x.info(info_flags::numeric) ) {
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()));
51 return numZERO(); // Infinity. Throw? What?
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());
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());
73 return gamma(x).hold();
76 ex gamma_evalf(ex const & x)
80 END_TYPECHECK(gamma(x))
82 return gamma(ex_to_numeric(x));
85 ex gamma_diff(ex const & x, unsigned diff_param)
87 ASSERT(diff_param==0);
89 return power(x, -1); //!!
92 ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
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);
99 throw(std::logic_error("don't know the series expansion of this particular gamma function"));
102 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);