1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma-function, Beta-function, Polygamma-functions, and
4 * some related stuff. */
7 * GiNaC Copyright (C) 1999-2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "relational.h"
30 #include "operators.h"
41 // Logarithm of Gamma function
44 static ex lgamma_evalf(const ex & x)
46 if (is_exactly_a<numeric>(x)) {
48 return lgamma(ex_to<numeric>(x));
49 } catch (const dunno &e) { }
52 return lgamma(x).hold();
56 /** Evaluation of lgamma(x), the natural logarithm of the Gamma function.
57 * Handles integer arguments as a special case.
59 * @exception GiNaC::pole_error("lgamma_eval(): logarithmic pole",0) */
60 static ex lgamma_eval(const ex & x)
62 if (x.info(info_flags::numeric)) {
63 // trap integer arguments:
64 if (x.info(info_flags::integer)) {
65 // lgamma(n) -> log((n-1)!) for postitive n
66 if (x.info(info_flags::posint))
67 return log(factorial(x + _ex_1));
69 throw (pole_error("lgamma_eval(): logarithmic pole",0));
71 if (!ex_to<numeric>(x).is_rational())
72 return lgamma(ex_to<numeric>(x));
75 return lgamma(x).hold();
79 static ex lgamma_deriv(const ex & x, unsigned deriv_param)
81 GINAC_ASSERT(deriv_param==0);
83 // d/dx lgamma(x) -> psi(x)
88 static ex lgamma_series(const ex & arg,
89 const relational & rel,
94 // Taylor series where there is no pole falls back to psi function
96 // On a pole at -m we could use the recurrence relation
97 // lgamma(x) == lgamma(x+1)-log(x)
99 // series(lgamma(x),x==-m,order) ==
100 // series(lgamma(x+m+1)-log(x)...-log(x+m)),x==-m,order);
101 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
102 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
103 throw do_taylor(); // caught by function::series()
104 // if we got here we have to care for a simple pole of tgamma(-m):
105 numeric m = -ex_to<numeric>(arg_pt);
107 for (numeric p = 0; p<=m; ++p)
109 return (lgamma(arg+m+_ex1)-recur).series(rel, order, options);
113 REGISTER_FUNCTION(lgamma, eval_func(lgamma_eval).
114 evalf_func(lgamma_evalf).
115 derivative_func(lgamma_deriv).
116 series_func(lgamma_series).
117 latex_name("\\log \\Gamma"));
121 // true Gamma function
124 static ex tgamma_evalf(const ex & x)
126 if (is_exactly_a<numeric>(x)) {
128 return tgamma(ex_to<numeric>(x));
129 } catch (const dunno &e) { }
132 return tgamma(x).hold();
136 /** Evaluation of tgamma(x), the true Gamma function. Knows about integer
137 * arguments, half-integer arguments and that's it. Somebody ought to provide
138 * some good numerical evaluation some day...
140 * @exception pole_error("tgamma_eval(): simple pole",0) */
141 static ex tgamma_eval(const ex & x)
143 if (x.info(info_flags::numeric)) {
144 // trap integer arguments:
145 const numeric two_x = (*_num2_p)*ex_to<numeric>(x);
146 if (two_x.is_even()) {
147 // tgamma(n) -> (n-1)! for postitive n
148 if (two_x.is_positive()) {
149 return factorial(ex_to<numeric>(x).sub(*_num1_p));
151 throw (pole_error("tgamma_eval(): simple pole",1));
154 // trap half integer arguments:
155 if (two_x.is_integer()) {
156 // trap positive x==(n+1/2)
157 // tgamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
158 if (two_x.is_positive()) {
159 const numeric n = ex_to<numeric>(x).sub(*_num1_2_p);
160 return (doublefactorial(n.mul(*_num2_p).sub(*_num1_p)).div(pow(*_num2_p,n))) * sqrt(Pi);
162 // trap negative x==(-n+1/2)
163 // tgamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
164 const numeric n = abs(ex_to<numeric>(x).sub(*_num1_2_p));
165 return (pow(*_num_2_p, n).div(doublefactorial(n.mul(*_num2_p).sub(*_num1_p))))*sqrt(Pi);
168 if (!ex_to<numeric>(x).is_rational())
169 return tgamma(ex_to<numeric>(x));
172 return tgamma(x).hold();
176 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
178 GINAC_ASSERT(deriv_param==0);
180 // d/dx tgamma(x) -> psi(x)*tgamma(x)
181 return psi(x)*tgamma(x);
185 static ex tgamma_series(const ex & arg,
186 const relational & rel,
191 // Taylor series where there is no pole falls back to psi function
193 // On a pole at -m use the recurrence relation
194 // tgamma(x) == tgamma(x+1) / x
195 // from which follows
196 // series(tgamma(x),x==-m,order) ==
197 // series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x==-m,order);
198 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
199 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
200 throw do_taylor(); // caught by function::series()
201 // if we got here we have to care for a simple pole at -m:
202 const numeric m = -ex_to<numeric>(arg_pt);
204 for (numeric p; p<=m; ++p)
206 return (tgamma(arg+m+_ex1)/ser_denom).series(rel, order, options);
210 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
211 evalf_func(tgamma_evalf).
212 derivative_func(tgamma_deriv).
213 series_func(tgamma_series).
214 latex_name("\\Gamma"));
221 static ex beta_evalf(const ex & x, const ex & y)
223 if (is_exactly_a<numeric>(x) && is_exactly_a<numeric>(y)) {
225 return exp(lgamma(ex_to<numeric>(x))+lgamma(ex_to<numeric>(y))-lgamma(ex_to<numeric>(x+y)));
226 } catch (const dunno &e) { }
229 return beta(x,y).hold();
233 static ex beta_eval(const ex & x, const ex & y)
235 if (x.is_equal(_ex1))
237 if (y.is_equal(_ex1))
239 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
240 // treat all problematic x and y that may not be passed into tgamma,
241 // because they would throw there although beta(x,y) is well-defined
242 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
243 const numeric &nx = ex_to<numeric>(x);
244 const numeric &ny = ex_to<numeric>(y);
245 if (nx.is_real() && nx.is_integer() &&
246 ny.is_real() && ny.is_integer()) {
247 if (nx.is_negative()) {
249 return pow(*_num_1_p, ny)*beta(1-x-y, y);
251 throw (pole_error("beta_eval(): simple pole",1));
253 if (ny.is_negative()) {
255 return pow(*_num_1_p, nx)*beta(1-y-x, x);
257 throw (pole_error("beta_eval(): simple pole",1));
259 return tgamma(x)*tgamma(y)/tgamma(x+y);
261 // no problem in numerator, but denominator has pole:
262 if ((nx+ny).is_real() &&
263 (nx+ny).is_integer() &&
264 !(nx+ny).is_positive())
266 if (!ex_to<numeric>(x).is_rational() || !ex_to<numeric>(x).is_rational())
267 return evalf(beta(x, y).hold());
270 return beta(x,y).hold();
274 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
276 GINAC_ASSERT(deriv_param<2);
279 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
281 retval = (psi(x)-psi(x+y))*beta(x,y);
282 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
284 retval = (psi(y)-psi(x+y))*beta(x,y);
289 static ex beta_series(const ex & arg1,
291 const relational & rel,
296 // Taylor series where there is no pole of one of the tgamma functions
297 // falls back to beta function evaluation. Otherwise, fall back to
298 // tgamma series directly.
299 const ex arg1_pt = arg1.subs(rel, subs_options::no_pattern);
300 const ex arg2_pt = arg2.subs(rel, subs_options::no_pattern);
301 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
302 const symbol &s = ex_to<symbol>(rel.lhs());
303 ex arg1_ser, arg2_ser, arg1arg2_ser;
304 if ((!arg1_pt.info(info_flags::integer) || arg1_pt.info(info_flags::positive)) &&
305 (!arg2_pt.info(info_flags::integer) || arg2_pt.info(info_flags::positive)))
306 throw do_taylor(); // caught by function::series()
307 // trap the case where arg1 is on a pole:
308 if (arg1.info(info_flags::integer) && !arg1.info(info_flags::positive))
309 arg1_ser = tgamma(arg1+s);
311 arg1_ser = tgamma(arg1);
312 // trap the case where arg2 is on a pole:
313 if (arg2.info(info_flags::integer) && !arg2.info(info_flags::positive))
314 arg2_ser = tgamma(arg2+s);
316 arg2_ser = tgamma(arg2);
317 // trap the case where arg1+arg2 is on a pole:
318 if ((arg1+arg2).info(info_flags::integer) && !(arg1+arg2).info(info_flags::positive))
319 arg1arg2_ser = tgamma(arg2+arg1+s);
321 arg1arg2_ser = tgamma(arg2+arg1);
322 // compose the result (expanding all the terms):
323 return (arg1_ser*arg2_ser/arg1arg2_ser).series(rel, order, options).expand();
327 REGISTER_FUNCTION(beta, eval_func(beta_eval).
328 evalf_func(beta_evalf).
329 derivative_func(beta_deriv).
330 series_func(beta_series).
331 latex_name("\\mathrm{B}").
332 set_symmetry(sy_symm(0, 1)));
336 // Psi-function (aka digamma-function)
339 static ex psi1_evalf(const ex & x)
341 if (is_exactly_a<numeric>(x)) {
343 return psi(ex_to<numeric>(x));
344 } catch (const dunno &e) { }
347 return psi(x).hold();
350 /** Evaluation of digamma-function psi(x).
351 * Somebody ought to provide some good numerical evaluation some day... */
352 static ex psi1_eval(const ex & x)
354 if (x.info(info_flags::numeric)) {
355 const numeric &nx = ex_to<numeric>(x);
356 if (nx.is_integer()) {
358 if (nx.is_positive()) {
359 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
361 for (numeric i(nx+(*_num_1_p)); i>0; --i)
365 // for non-positive integers there is a pole:
366 throw (pole_error("psi_eval(): simple pole",1));
369 if (((*_num2_p)*nx).is_integer()) {
371 if (nx.is_positive()) {
372 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
374 for (numeric i = (nx+(*_num_1_p))*(*_num2_p); i>0; i-=(*_num2_p))
375 rat += (*_num2_p)*i.inverse();
376 return rat-Euler-_ex2*log(_ex2);
378 // use the recurrence relation
379 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
380 // to relate psi(-m-1/2) to psi(1/2):
381 // psi(-m-1/2) == psi(1/2) + r
382 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
384 for (numeric p = nx; p<0; ++p)
385 recur -= pow(p, *_num_1_p);
386 return recur+psi(_ex1_2);
389 // psi1_evalf should be called here once it becomes available
392 return psi(x).hold();
395 static ex psi1_deriv(const ex & x, unsigned deriv_param)
397 GINAC_ASSERT(deriv_param==0);
399 // d/dx psi(x) -> psi(1,x)
403 static ex psi1_series(const ex & arg,
404 const relational & rel,
409 // Taylor series where there is no pole falls back to polygamma function
411 // On a pole at -m use the recurrence relation
412 // psi(x) == psi(x+1) - 1/z
413 // from which follows
414 // series(psi(x),x==-m,order) ==
415 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
416 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
417 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
418 throw do_taylor(); // caught by function::series()
419 // if we got here we have to care for a simple pole at -m:
420 const numeric m = -ex_to<numeric>(arg_pt);
422 for (numeric p; p<=m; ++p)
423 recur += power(arg+p,_ex_1);
424 return (psi(arg+m+_ex1)-recur).series(rel, order, options);
427 unsigned psi1_SERIAL::serial =
428 function::register_new(function_options("psi", 1).
429 eval_func(psi1_eval).
430 evalf_func(psi1_evalf).
431 derivative_func(psi1_deriv).
432 series_func(psi1_series).
437 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
440 static ex psi2_evalf(const ex & n, const ex & x)
442 if (is_exactly_a<numeric>(n) && is_exactly_a<numeric>(x)) {
444 return psi(ex_to<numeric>(n),ex_to<numeric>(x));
445 } catch (const dunno &e) { }
448 return psi(n,x).hold();
451 /** Evaluation of polygamma-function psi(n,x).
452 * Somebody ought to provide some good numerical evaluation some day... */
453 static ex psi2_eval(const ex & n, const ex & x)
455 // psi(0,x) -> psi(x)
458 // psi(-1,x) -> log(tgamma(x))
459 if (n.is_equal(_ex_1))
460 return log(tgamma(x));
461 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
462 x.info(info_flags::numeric)) {
463 const numeric &nn = ex_to<numeric>(n);
464 const numeric &nx = ex_to<numeric>(x);
465 if (nx.is_integer()) {
467 if (nx.is_equal(*_num1_p))
468 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
469 return pow(*_num_1_p,nn+(*_num1_p))*factorial(nn)*zeta(ex(nn+(*_num1_p)));
470 if (nx.is_positive()) {
471 // use the recurrence relation
472 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
473 // to relate psi(n,m) to psi(n,1):
474 // psi(n,m) == psi(n,1) + r
475 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
477 for (numeric p = 1; p<nx; ++p)
478 recur += pow(p, -nn+(*_num_1_p));
479 recur *= factorial(nn)*pow((*_num_1_p), nn);
480 return recur+psi(n,_ex1);
482 // for non-positive integers there is a pole:
483 throw (pole_error("psi2_eval(): pole",1));
486 if (((*_num2_p)*nx).is_integer()) {
488 if (nx.is_equal(*_num1_2_p))
489 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
490 return pow(*_num_1_p,nn+(*_num1_p))*factorial(nn)*(pow(*_num2_p,nn+(*_num1_p)) + (*_num_1_p))*zeta(ex(nn+(*_num1_p)));
491 if (nx.is_positive()) {
492 const numeric m = nx - (*_num1_2_p);
493 // use the multiplication formula
494 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
495 // to revert to positive integer case
496 return psi(n,(*_num2_p)*m)*pow((*_num2_p),nn+(*_num1_p))-psi(n,m);
498 // use the recurrence relation
499 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
500 // to relate psi(n,-m-1/2) to psi(n,1/2):
501 // psi(n,-m-1/2) == psi(n,1/2) + r
502 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
504 for (numeric p = nx; p<0; ++p)
505 recur += pow(p, -nn+(*_num_1_p));
506 recur *= factorial(nn)*pow(*_num_1_p, nn+(*_num_1_p));
507 return recur+psi(n,_ex1_2);
510 // psi2_evalf should be called here once it becomes available
513 return psi(n, x).hold();
516 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
518 GINAC_ASSERT(deriv_param<2);
520 if (deriv_param==0) {
522 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
524 // d/dx psi(n,x) -> psi(n+1,x)
525 return psi(n+_ex1, x);
528 static ex psi2_series(const ex & n,
530 const relational & rel,
535 // Taylor series where there is no pole falls back to polygamma function
537 // On a pole at -m use the recurrence relation
538 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
539 // from which follows
540 // series(psi(x),x==-m,order) ==
541 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
542 // ... + (x+m)^(-n-1))),x==-m,order);
543 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
544 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
545 throw do_taylor(); // caught by function::series()
546 // if we got here we have to care for a pole of order n+1 at -m:
547 const numeric m = -ex_to<numeric>(arg_pt);
549 for (numeric p; p<=m; ++p)
550 recur += power(arg+p,-n+_ex_1);
551 recur *= factorial(n)*power(_ex_1,n);
552 return (psi(n, arg+m+_ex1)-recur).series(rel, order, options);
555 unsigned psi2_SERIAL::serial =
556 function::register_new(function_options("psi", 2).
557 eval_func(psi2_eval).
558 evalf_func(psi2_evalf).
559 derivative_func(psi2_deriv).
560 series_func(psi2_series).