1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma function and some related stuff. */
6 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
39 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
40 * arguments and that's it. Somebody ought to provide some good numerical
41 * evaluation some day...
43 * @exception fail_numeric("complex_infinity") or something similar... */
44 static ex gamma_eval(ex const & x)
46 if ( x.info(info_flags::numeric) ) {
48 // trap integer arguments:
49 if ( x.info(info_flags::integer) ) {
50 // gamma(n+1) -> n! for postitive n
51 if ( x.info(info_flags::posint) ) {
52 return factorial(ex_to_numeric(x).sub(numONE()));
54 return numZERO(); // Infinity. Throw? What?
57 // trap half integer arguments:
58 if ( (x*2).info(info_flags::integer) ) {
59 // trap positive x=(n+1/2)
60 // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
61 if ( (x*2).info(info_flags::posint) ) {
62 numeric n = ex_to_numeric(x).sub(numHALF());
63 numeric coefficient = doublefactorial(n.mul(numTWO()).sub(numONE()));
64 coefficient = coefficient.div(numTWO().power(n));
65 return coefficient * power(Pi,numHALF());
67 // trap negative x=(-n+1/2)
68 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
69 numeric n = abs(ex_to_numeric(x).sub(numHALF()));
70 numeric coefficient = numeric(-2).power(n);
71 coefficient = coefficient.div(doublefactorial(n.mul(numTWO()).sub(numONE())));;
72 return coefficient * power(Pi,numHALF());
76 return gamma(x).hold();
79 static ex gamma_evalf(ex const & x)
83 END_TYPECHECK(gamma(x))
85 return gamma(ex_to_numeric(x));
88 static ex gamma_diff(ex const & x, unsigned diff_param)
90 ASSERT(diff_param==0);
92 return power(x, -1); // FIXME
95 static ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
97 // FIXME: Only handle one special case for now...
98 if (x.is_equal(s) && point.is_zero()) {
99 ex e = 1 / s - EulerGamma + s * (power(Pi, 2) / 12 + power(EulerGamma, 2) / 2) + Order(power(s, 2));
100 return e.series(s, point, order);
102 throw(std::logic_error("don't know the series expansion of this particular gamma function"));
105 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);