1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma-function, Polygamma-functions, and some related
7 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #ifndef NO_GINAC_NAMESPACE
36 #endif // ndef NO_GINAC_NAMESPACE
42 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
43 * arguments and that's it. Somebody ought to provide some good numerical
44 * evaluation some day...
46 * @exception fail_numeric("complex_infinity") or something similar... */
47 static ex gamma_eval(ex const & x)
49 if (x.info(info_flags::numeric)) {
50 // trap integer arguments:
51 if (x.info(info_flags::integer)) {
52 // gamma(n+1) -> n! for postitive n
53 if (x.info(info_flags::posint)) {
54 return factorial(ex_to_numeric(x).sub(numONE()));
56 return numZERO(); // Infinity. Throw? What?
59 // trap half integer arguments:
60 if ((x*2).info(info_flags::integer)) {
61 // trap positive x=(n+1/2)
62 // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
63 if ((x*2).info(info_flags::posint)) {
64 numeric n = ex_to_numeric(x).sub(numHALF());
65 numeric coefficient = doublefactorial(n.mul(numTWO()).sub(numONE()));
66 coefficient = coefficient.div(numTWO().power(n));
67 return coefficient * pow(Pi,numHALF());
69 // trap negative x=(-n+1/2)
70 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
71 numeric n = abs(ex_to_numeric(x).sub(numHALF()));
72 numeric coefficient = numeric(-2).power(n);
73 coefficient = coefficient.div(doublefactorial(n.mul(numTWO()).sub(numONE())));;
74 return coefficient*sqrt(Pi);
78 return gamma(x).hold();
81 static ex gamma_evalf(ex const & x)
85 END_TYPECHECK(gamma(x))
87 return gamma(ex_to_numeric(x));
90 static ex gamma_diff(ex const & x, unsigned diff_param)
92 GINAC_ASSERT(diff_param==0);
94 return psi(x)*gamma(x); // diff(log(gamma(x)),x)==psi(x)
97 static ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
99 // FIXME: Only handle one special case for now...
100 if (x.is_equal(s) && point.is_zero()) {
101 ex e = 1 / s - EulerGamma + s * (pow(Pi, 2) / 12 + pow(EulerGamma, 2) / 2) + Order(pow(s, 2));
102 return e.series(s, point, order);
104 throw(std::logic_error("don't know the series expansion of this particular gamma function"));
107 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
110 // Psi-function (aka polygamma-function)
113 /** Evaluation of polygamma-function psi(x).
114 * Somebody ought to provide some good numerical evaluation some day... */
115 static ex psi1_eval(ex const & x)
117 if (x.info(info_flags::numeric)) {
118 if (x.info(info_flags::integer) && !x.info(info_flags::positive))
119 throw (std::domain_error("psi_eval(): simple pole"));
120 if (x.info(info_flags::positive)) {
121 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
122 if (x.info(info_flags::integer)) {
124 for (numeric i(ex_to_numeric(x)-numONE()); i.is_positive(); --i)
126 return rat-EulerGamma;
128 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
129 if ((exTWO()*x).info(info_flags::integer)) {
131 for (numeric i((ex_to_numeric(x)-numONE())*numTWO()); i.is_positive(); i-=numTWO())
132 rat += numTWO()*i.inverse();
133 return rat-EulerGamma-exTWO()*log(exTWO());
135 if (x.compare(exONE())==1) {
136 // should call numeric, since >1
140 return psi(x).hold();
143 static ex psi1_evalf(ex const & x)
147 END_TYPECHECK(psi(x))
149 return psi(ex_to_numeric(x));
152 static ex psi1_diff(ex const & x, unsigned diff_param)
154 GINAC_ASSERT(diff_param==0);
156 return psi(exONE(), x);
159 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, NULL);
162 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
165 /** Evaluation of polygamma-function psi(n,x).
166 * Somebody ought to provide some good numerical evaluation some day... */
167 static ex psi2_eval(ex const & n, ex const & x)
169 // psi(0,x) -> psi(x)
172 if (n.info(info_flags::numeric) && x.info(info_flags::numeric)) {
175 return psi(n, x).hold();
178 static ex psi2_evalf(ex const & n, ex const & x)
183 END_TYPECHECK(psi(n,x))
185 return psi(ex_to_numeric(n), ex_to_numeric(x));
188 static ex psi2_diff(ex const & n, ex const & x, unsigned diff_param)
190 GINAC_ASSERT(diff_param<2);
194 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
200 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, NULL);
202 #ifndef NO_GINAC_NAMESPACE
204 #endif // ndef NO_GINAC_NAMESPACE