1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma-function, Beta-function, Polygamma-functions, and
4 * some related stuff. */
7 * GiNaC Copyright (C) 1999-2000 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
33 #include "relational.h"
37 #ifndef NO_NAMESPACE_GINAC
39 #endif // ndef NO_NAMESPACE_GINAC
45 static ex gamma_evalf(const ex & x)
49 END_TYPECHECK(gamma(x))
51 return gamma(ex_to_numeric(x));
55 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
56 * arguments and that's it. Somebody ought to provide some good numerical
57 * evaluation some day...
59 * @exception std::domain_error("gamma_eval(): simple pole") */
60 static ex gamma_eval(const ex & x)
62 if (x.info(info_flags::numeric)) {
63 // trap integer arguments:
64 if (x.info(info_flags::integer)) {
65 // gamma(n+1) -> n! for postitive n
66 if (x.info(info_flags::posint)) {
67 return factorial(ex_to_numeric(x).sub(_num1()));
69 throw (std::domain_error("gamma_eval(): simple pole"));
72 // trap half integer arguments:
73 if ((x*2).info(info_flags::integer)) {
74 // trap positive x==(n+1/2)
75 // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
76 if ((x*_ex2()).info(info_flags::posint)) {
77 numeric n = ex_to_numeric(x).sub(_num1_2());
78 numeric coefficient = doublefactorial(n.mul(_num2()).sub(_num1()));
79 coefficient = coefficient.div(pow(_num2(),n));
80 return coefficient * pow(Pi,_ex1_2());
82 // trap negative x==(-n+1/2)
83 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
84 numeric n = abs(ex_to_numeric(x).sub(_num1_2()));
85 numeric coefficient = pow(_num_2(), n);
86 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
87 return coefficient*power(Pi,_ex1_2());
90 // gamma_evalf should be called here once it becomes available
93 return gamma(x).hold();
97 static ex gamma_deriv(const ex & x, unsigned deriv_param)
99 GINAC_ASSERT(deriv_param==0);
101 // d/dx log(gamma(x)) -> psi(x)
102 // d/dx gamma(x) -> psi(x)*gamma(x)
103 return psi(x)*gamma(x);
107 static ex gamma_series(const ex & x, const symbol & s, const ex & pt, int order)
110 // Taylor series where there is no pole falls back to psi function
112 // On a pole at -m use the recurrence relation
113 // gamma(x) == gamma(x+1) / x
114 // from which follows
115 // series(gamma(x),x,-m,order) ==
116 // series(gamma(x+m+1)/(x*(x+1)*...*(x+m)),x,-m,order+1);
117 const ex x_pt = x.subs(s==pt);
118 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
119 throw do_taylor(); // caught by function::series()
120 // if we got here we have to care for a simple pole at -m:
121 numeric m = -ex_to_numeric(x_pt);
122 ex ser_denom = _ex1();
123 for (numeric p; p<=m; ++p)
125 return (gamma(x+m+_ex1())/ser_denom).series(s, pt, order+1);
129 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_deriv, gamma_series);
136 static ex beta_evalf(const ex & x, const ex & y)
141 END_TYPECHECK(beta(x,y))
143 return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))/gamma(ex_to_numeric(x+y));
147 static ex beta_eval(const ex & x, const ex & y)
149 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
150 // treat all problematic x and y that may not be passed into gamma,
151 // because they would throw there although beta(x,y) is well-defined
152 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
153 numeric nx(ex_to_numeric(x));
154 numeric ny(ex_to_numeric(y));
155 if (nx.is_real() && nx.is_integer() &&
156 ny.is_real() && ny.is_integer()) {
157 if (nx.is_negative()) {
159 return pow(_num_1(), ny)*beta(1-x-y, y);
161 throw (std::domain_error("beta_eval(): simple pole"));
163 if (ny.is_negative()) {
165 return pow(_num_1(), nx)*beta(1-y-x, x);
167 throw (std::domain_error("beta_eval(): simple pole"));
169 return gamma(x)*gamma(y)/gamma(x+y);
171 // no problem in numerator, but denominator has pole:
172 if ((nx+ny).is_real() &&
173 (nx+ny).is_integer() &&
174 !(nx+ny).is_positive())
177 return gamma(x)*gamma(y)/gamma(x+y);
180 return beta(x,y).hold();
184 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
186 GINAC_ASSERT(deriv_param<2);
189 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
191 retval = (psi(x)-psi(x+y))*beta(x,y);
192 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
194 retval = (psi(y)-psi(x+y))*beta(x,y);
199 static ex beta_series(const ex & x, const ex & y, const symbol & s, const ex & pt, int order)
202 // Taylor series where there is no pole of one of the gamma functions
203 // falls back to beta function evaluation. Otherwise, fall back to
204 // gamma series directly.
205 // FIXME: this could need some testing, maybe it's wrong in some cases?
206 const ex x_pt = x.subs(s==pt);
207 const ex y_pt = y.subs(s==pt);
208 ex x_ser, y_ser, xy_ser;
209 if ((!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive)) &&
210 (!y_pt.info(info_flags::integer) || y_pt.info(info_flags::positive)))
211 throw do_taylor(); // caught by function::series()
212 // trap the case where x is on a pole directly:
213 if (x.info(info_flags::integer) && !x.info(info_flags::positive))
214 x_ser = gamma(x+s).series(s,pt,order);
216 x_ser = gamma(x).series(s,pt,order);
217 // trap the case where y is on a pole directly:
218 if (y.info(info_flags::integer) && !y.info(info_flags::positive))
219 y_ser = gamma(y+s).series(s,pt,order);
221 y_ser = gamma(y).series(s,pt,order);
222 // trap the case where y is on a pole directly:
223 if ((x+y).info(info_flags::integer) && !(x+y).info(info_flags::positive))
224 xy_ser = gamma(y+x+s).series(s,pt,order);
226 xy_ser = gamma(y+x).series(s,pt,order);
227 // compose the result:
228 return (x_ser*y_ser/xy_ser).series(s,pt,order);
232 REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_deriv, beta_series);
236 // Psi-function (aka digamma-function)
239 static ex psi1_evalf(const ex & x)
243 END_TYPECHECK(psi(x))
245 return psi(ex_to_numeric(x));
248 /** Evaluation of digamma-function psi(x).
249 * Somebody ought to provide some good numerical evaluation some day... */
250 static ex psi1_eval(const ex & x)
252 if (x.info(info_flags::numeric)) {
253 numeric nx = ex_to_numeric(x);
254 if (nx.is_integer()) {
256 if (nx.is_positive()) {
257 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
259 for (numeric i(nx+_num_1()); i.is_positive(); --i)
261 return rat-EulerGamma;
263 // for non-positive integers there is a pole:
264 throw (std::domain_error("psi_eval(): simple pole"));
267 if ((_num2()*nx).is_integer()) {
269 if (nx.is_positive()) {
270 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
272 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
273 rat += _num2()*i.inverse();
274 return rat-EulerGamma-_ex2()*log(_ex2());
276 // use the recurrence relation
277 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
278 // to relate psi(-m-1/2) to psi(1/2):
279 // psi(-m-1/2) == psi(1/2) + r
280 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
282 for (numeric p(nx); p<0; ++p)
283 recur -= pow(p, _num_1());
284 return recur+psi(_ex1_2());
287 // psi1_evalf should be called here once it becomes available
290 return psi(x).hold();
293 static ex psi1_deriv(const ex & x, unsigned deriv_param)
295 GINAC_ASSERT(deriv_param==0);
297 // d/dx psi(x) -> psi(1,x)
298 return psi(_ex1(), x);
301 static ex psi1_series(const ex & x, const symbol & s, const ex & pt, int order)
304 // Taylor series where there is no pole falls back to polygamma function
306 // On a pole at -m use the recurrence relation
307 // psi(x) == psi(x+1) - 1/z
308 // from which follows
309 // series(psi(x),x,-m,order) ==
310 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
311 const ex x_pt = x.subs(s==pt);
312 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
313 throw do_taylor(); // caught by function::series()
314 // if we got here we have to care for a simple pole at -m:
315 numeric m = -ex_to_numeric(x_pt);
317 for (numeric p; p<=m; ++p)
318 recur += power(x+p,_ex_1());
319 return (psi(x+m+_ex1())-recur).series(s, pt, order);
322 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_deriv, psi1_series);
325 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
328 static ex psi2_evalf(const ex & n, const ex & x)
333 END_TYPECHECK(psi(n,x))
335 return psi(ex_to_numeric(n), ex_to_numeric(x));
338 /** Evaluation of polygamma-function psi(n,x).
339 * Somebody ought to provide some good numerical evaluation some day... */
340 static ex psi2_eval(const ex & n, const ex & x)
342 // psi(0,x) -> psi(x)
345 // psi(-1,x) -> log(gamma(x))
346 if (n.is_equal(_ex_1()))
347 return log(gamma(x));
348 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
349 x.info(info_flags::numeric)) {
350 numeric nn = ex_to_numeric(n);
351 numeric nx = ex_to_numeric(x);
352 if (nx.is_integer()) {
354 if (nx.is_equal(_num1()))
355 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
356 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
357 if (nx.is_positive()) {
358 // use the recurrence relation
359 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
360 // to relate psi(n,m) to psi(n,1):
361 // psi(n,m) == psi(n,1) + r
362 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
364 for (numeric p(1); p<nx; ++p)
365 recur += pow(p, -nn+_num_1());
366 recur *= factorial(nn)*pow(_num_1(), nn);
367 return recur+psi(n,_ex1());
369 // for non-positive integers there is a pole:
370 throw (std::domain_error("psi2_eval(): pole"));
373 if ((_num2()*nx).is_integer()) {
375 if (nx.is_equal(_num1_2()))
376 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
377 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
378 if (nx.is_positive()) {
379 numeric m = nx - _num1_2();
380 // use the multiplication formula
381 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
382 // to revert to positive integer case
383 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
385 // use the recurrence relation
386 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
387 // to relate psi(n,-m-1/2) to psi(n,1/2):
388 // psi(n,-m-1/2) == psi(n,1/2) + r
389 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
391 for (numeric p(nx); p<0; ++p)
392 recur += pow(p, -nn+_num_1());
393 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
394 return recur+psi(n,_ex1_2());
397 // psi2_evalf should be called here once it becomes available
400 return psi(n, x).hold();
403 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
405 GINAC_ASSERT(deriv_param<2);
407 if (deriv_param==0) {
409 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
411 // d/dx psi(n,x) -> psi(n+1,x)
412 return psi(n+_ex1(), x);
415 static ex psi2_series(const ex & n, const ex & x, const symbol & s, const ex & pt, int order)
418 // Taylor series where there is no pole falls back to polygamma function
420 // On a pole at -m use the recurrence relation
421 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
422 // from which follows
423 // series(psi(x),x,-m,order) ==
424 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
425 // ... + (x+m)^(-n-1))),x,-m,order);
426 const ex x_pt = x.subs(s==pt);
427 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
428 throw do_taylor(); // caught by function::series()
429 // if we got here we have to care for a pole of order n+1 at -m:
430 numeric m = -ex_to_numeric(x_pt);
432 for (numeric p; p<=m; ++p)
433 recur += power(x+p,-n+_ex_1());
434 recur *= factorial(n)*power(_ex_1(),n);
435 return (psi(n, x+m+_ex1())-recur).series(s, pt, order);
438 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_deriv, psi2_series);
440 #ifndef NO_NAMESPACE_GINAC
442 #endif // ndef NO_NAMESPACE_GINAC