1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma-function, Beta-function, Polygamma-functions, and
4 * some related stuff. */
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 throw (std::domain_error("gamma_eval(): simple pole"));
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);
113 static ex beta_eval(ex const & x, ex const & y)
115 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
116 numeric nx(ex_to_numeric(x));
117 numeric ny(ex_to_numeric(y));
118 // treat all problematic x and y that may not be passed into gamma,
119 // because they would throw there although beta(x,y) is well-defined:
120 if (nx.is_real() && nx.is_integer() &&
121 ny.is_real() && ny.is_integer()) {
122 if (nx.is_negative()) {
124 return numMINUSONE().power(ny)*beta(1-x-y, y);
126 throw (std::domain_error("beta_eval(): simple pole"));
128 if (ny.is_negative()) {
130 return numMINUSONE().power(nx)*beta(1-y-x, x);
132 throw (std::domain_error("beta_eval(): simple pole"));
134 return gamma(x)*gamma(y)/gamma(x+y);
136 // no problem in numerator, but denominator has pole:
137 if ((nx+ny).is_real() &&
138 (nx+ny).is_integer() &&
139 !(nx+ny).is_positive())
141 return gamma(x)*gamma(y)/gamma(x+y);
143 return beta(x,y).hold();
146 static ex beta_evalf(ex const & x, ex const & y)
151 END_TYPECHECK(beta(x,y))
153 return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
154 / gamma(ex_to_numeric(x+y));
157 static ex beta_diff(ex const & x, ex const & y, unsigned diff_param)
159 GINAC_ASSERT(diff_param<2);
162 if (diff_param==0) // d/dx beta(x,y)
163 retval = (psi(x)-psi(x+y))*beta(x,y);
164 if (diff_param==1) // d/dy beta(x,y)
165 retval = (psi(y)-psi(x+y))*beta(x,y);
169 static ex beta_series(ex const & x, ex const & y, symbol const & s, ex const & point, int order)
171 if (x.is_equal(s) && point.is_zero()) {
172 ex e = 1 / s - EulerGamma + s * (pow(Pi, 2) / 12 + pow(EulerGamma, 2) / 2) + Order(pow(s, 2));
173 return e.series(s, point, order);
175 throw(std::logic_error("don't know the series expansion of this particular beta function"));
178 REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, beta_series);
181 // Psi-function (aka polygamma-function)
184 /** Evaluation of polygamma-function psi(x).
185 * Somebody ought to provide some good numerical evaluation some day... */
186 static ex psi1_eval(ex const & x)
188 if (x.info(info_flags::numeric)) {
189 if (x.info(info_flags::integer) && !x.info(info_flags::positive))
190 throw (std::domain_error("psi_eval(): simple pole"));
191 if (x.info(info_flags::positive)) {
192 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
193 if (x.info(info_flags::integer)) {
195 for (numeric i(ex_to_numeric(x)-numONE()); i.is_positive(); --i)
197 return rat-EulerGamma;
199 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
200 if ((exTWO()*x).info(info_flags::integer)) {
202 for (numeric i((ex_to_numeric(x)-numONE())*numTWO()); i.is_positive(); i-=numTWO())
203 rat += numTWO()*i.inverse();
204 return rat-EulerGamma-exTWO()*log(exTWO());
206 if (x.compare(exONE())==1) {
207 // should call numeric, since >1
211 return psi(x).hold();
214 static ex psi1_evalf(ex const & x)
218 END_TYPECHECK(psi(x))
220 return psi(ex_to_numeric(x));
223 static ex psi1_diff(ex const & x, unsigned diff_param)
225 GINAC_ASSERT(diff_param==0);
227 return psi(exONE(), x);
230 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, NULL);
233 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
236 /** Evaluation of polygamma-function psi(n,x).
237 * Somebody ought to provide some good numerical evaluation some day... */
238 static ex psi2_eval(ex const & n, ex const & x)
240 // psi(0,x) -> psi(x)
243 if (n.info(info_flags::numeric) && x.info(info_flags::numeric)) {
246 return psi(n, x).hold();
249 static ex psi2_evalf(ex const & n, ex const & x)
254 END_TYPECHECK(psi(n,x))
256 return psi(ex_to_numeric(n), ex_to_numeric(x));
259 static ex psi2_diff(ex const & n, ex const & x, unsigned diff_param)
261 GINAC_ASSERT(diff_param<2);
265 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
271 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, NULL);
273 #ifndef NO_GINAC_NAMESPACE
275 #endif // ndef NO_GINAC_NAMESPACE