1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma-function, Beta-function, Polygamma-functions, and
4 * some related stuff. */
7 * GiNaC Copyright (C) 1999-2018 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 static ex lgamma_conjugate(const ex & x)
115 // conjugate(lgamma(x))==lgamma(conjugate(x)) unless on the branch cut
116 // which runs along the negative real axis.
117 if (x.info(info_flags::positive)) {
120 if (is_exactly_a<numeric>(x) &&
121 !x.imag_part().is_zero()) {
122 return lgamma(x.conjugate());
124 return conjugate_function(lgamma(x)).hold();
128 REGISTER_FUNCTION(lgamma, eval_func(lgamma_eval).
129 evalf_func(lgamma_evalf).
130 derivative_func(lgamma_deriv).
131 series_func(lgamma_series).
132 conjugate_func(lgamma_conjugate).
133 latex_name("\\log \\Gamma"));
137 // true Gamma function
140 static ex tgamma_evalf(const ex & x)
142 if (is_exactly_a<numeric>(x)) {
144 return tgamma(ex_to<numeric>(x));
145 } catch (const dunno &e) { }
148 return tgamma(x).hold();
152 /** Evaluation of tgamma(x), the true Gamma function. Knows about integer
153 * arguments, half-integer arguments and that's it. Somebody ought to provide
154 * some good numerical evaluation some day...
156 * @exception pole_error("tgamma_eval(): simple pole",0) */
157 static ex tgamma_eval(const ex & x)
159 if (x.info(info_flags::numeric)) {
160 // trap integer arguments:
161 const numeric two_x = (*_num2_p)*ex_to<numeric>(x);
162 if (two_x.is_even()) {
163 // tgamma(n) -> (n-1)! for postitive n
164 if (two_x.is_positive()) {
165 return factorial(ex_to<numeric>(x).sub(*_num1_p));
167 throw (pole_error("tgamma_eval(): simple pole",1));
170 // trap half integer arguments:
171 if (two_x.is_integer()) {
172 // trap positive x==(n+1/2)
173 // tgamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
174 if (two_x.is_positive()) {
175 const numeric n = ex_to<numeric>(x).sub(*_num1_2_p);
176 return (doublefactorial(n.mul(*_num2_p).sub(*_num1_p)).div(pow(*_num2_p,n))) * sqrt(Pi);
178 // trap negative x==(-n+1/2)
179 // tgamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
180 const numeric n = abs(ex_to<numeric>(x).sub(*_num1_2_p));
181 return (pow(*_num_2_p, n).div(doublefactorial(n.mul(*_num2_p).sub(*_num1_p))))*sqrt(Pi);
184 if (!ex_to<numeric>(x).is_rational())
185 return tgamma(ex_to<numeric>(x));
188 return tgamma(x).hold();
192 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
194 GINAC_ASSERT(deriv_param==0);
196 // d/dx tgamma(x) -> psi(x)*tgamma(x)
197 return psi(x)*tgamma(x);
201 static ex tgamma_series(const ex & arg,
202 const relational & rel,
207 // Taylor series where there is no pole falls back to psi function
209 // On a pole at -m use the recurrence relation
210 // tgamma(x) == tgamma(x+1) / x
211 // from which follows
212 // series(tgamma(x),x==-m,order) ==
213 // series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x==-m,order);
214 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
215 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
216 throw do_taylor(); // caught by function::series()
217 // if we got here we have to care for a simple pole at -m:
218 const numeric m = -ex_to<numeric>(arg_pt);
220 for (numeric p; p<=m; ++p)
222 return (tgamma(arg+m+_ex1)/ser_denom).series(rel, order, options);
226 static ex tgamma_conjugate(const ex & x)
228 // conjugate(tgamma(x))==tgamma(conjugate(x))
229 return tgamma(x.conjugate());
233 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
234 evalf_func(tgamma_evalf).
235 derivative_func(tgamma_deriv).
236 series_func(tgamma_series).
237 conjugate_func(tgamma_conjugate).
238 latex_name("\\Gamma"));
245 static ex beta_evalf(const ex & x, const ex & y)
247 if (is_exactly_a<numeric>(x) && is_exactly_a<numeric>(y)) {
249 return exp(lgamma(ex_to<numeric>(x))+lgamma(ex_to<numeric>(y))-lgamma(ex_to<numeric>(x+y)));
250 } catch (const dunno &e) { }
253 return beta(x,y).hold();
257 static ex beta_eval(const ex & x, const ex & y)
259 if (x.is_equal(_ex1))
261 if (y.is_equal(_ex1))
263 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
264 // treat all problematic x and y that may not be passed into tgamma,
265 // because they would throw there although beta(x,y) is well-defined
266 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
267 const numeric &nx = ex_to<numeric>(x);
268 const numeric &ny = ex_to<numeric>(y);
269 if (nx.is_real() && nx.is_integer() &&
270 ny.is_real() && ny.is_integer()) {
271 if (nx.is_negative()) {
273 return pow(*_num_1_p, ny)*beta(1-x-y, y);
275 throw (pole_error("beta_eval(): simple pole",1));
277 if (ny.is_negative()) {
279 return pow(*_num_1_p, nx)*beta(1-y-x, x);
281 throw (pole_error("beta_eval(): simple pole",1));
283 return tgamma(x)*tgamma(y)/tgamma(x+y);
285 // no problem in numerator, but denominator has pole:
286 if ((nx+ny).is_real() &&
287 (nx+ny).is_integer() &&
288 !(nx+ny).is_positive())
290 if (!ex_to<numeric>(x).is_rational() || !ex_to<numeric>(x).is_rational())
291 return evalf(beta(x, y).hold());
294 return beta(x,y).hold();
298 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
300 GINAC_ASSERT(deriv_param<2);
303 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
305 retval = (psi(x)-psi(x+y))*beta(x,y);
306 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
308 retval = (psi(y)-psi(x+y))*beta(x,y);
313 static ex beta_series(const ex & arg1,
315 const relational & rel,
320 // Taylor series where there is no pole of one of the tgamma functions
321 // falls back to beta function evaluation. Otherwise, fall back to
322 // tgamma series directly.
323 const ex arg1_pt = arg1.subs(rel, subs_options::no_pattern);
324 const ex arg2_pt = arg2.subs(rel, subs_options::no_pattern);
325 GINAC_ASSERT(is_a<symbol>(rel.lhs()));
326 const symbol &s = ex_to<symbol>(rel.lhs());
327 ex arg1_ser, arg2_ser, arg1arg2_ser;
328 if ((!arg1_pt.info(info_flags::integer) || arg1_pt.info(info_flags::positive)) &&
329 (!arg2_pt.info(info_flags::integer) || arg2_pt.info(info_flags::positive)))
330 throw do_taylor(); // caught by function::series()
331 // trap the case where arg1 is on a pole:
332 if (arg1.info(info_flags::integer) && !arg1.info(info_flags::positive))
333 arg1_ser = tgamma(arg1+s);
335 arg1_ser = tgamma(arg1);
336 // trap the case where arg2 is on a pole:
337 if (arg2.info(info_flags::integer) && !arg2.info(info_flags::positive))
338 arg2_ser = tgamma(arg2+s);
340 arg2_ser = tgamma(arg2);
341 // trap the case where arg1+arg2 is on a pole:
342 if ((arg1+arg2).info(info_flags::integer) && !(arg1+arg2).info(info_flags::positive))
343 arg1arg2_ser = tgamma(arg2+arg1+s);
345 arg1arg2_ser = tgamma(arg2+arg1);
346 // compose the result (expanding all the terms):
347 return (arg1_ser*arg2_ser/arg1arg2_ser).series(rel, order, options).expand();
351 REGISTER_FUNCTION(beta, eval_func(beta_eval).
352 evalf_func(beta_evalf).
353 derivative_func(beta_deriv).
354 series_func(beta_series).
355 latex_name("\\mathrm{B}").
356 set_symmetry(sy_symm(0, 1)));
360 // Psi-function (aka digamma-function)
363 static ex psi1_evalf(const ex & x)
365 if (is_exactly_a<numeric>(x)) {
367 return psi(ex_to<numeric>(x));
368 } catch (const dunno &e) { }
371 return psi(x).hold();
374 /** Evaluation of digamma-function psi(x).
375 * Somebody ought to provide some good numerical evaluation some day... */
376 static ex psi1_eval(const ex & x)
378 if (x.info(info_flags::numeric)) {
379 const numeric &nx = ex_to<numeric>(x);
380 if (nx.is_integer()) {
382 if (nx.is_positive()) {
383 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
385 for (numeric i(nx+(*_num_1_p)); i>0; --i)
389 // for non-positive integers there is a pole:
390 throw (pole_error("psi_eval(): simple pole",1));
393 if (((*_num2_p)*nx).is_integer()) {
395 if (nx.is_positive()) {
396 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
398 for (numeric i = (nx+(*_num_1_p))*(*_num2_p); i>0; i-=(*_num2_p))
399 rat += (*_num2_p)*i.inverse();
400 return rat-Euler-_ex2*log(_ex2);
402 // use the recurrence relation
403 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
404 // to relate psi(-m-1/2) to psi(1/2):
405 // psi(-m-1/2) == psi(1/2) + r
406 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
408 for (numeric p = nx; p<0; ++p)
409 recur -= pow(p, *_num_1_p);
410 return recur+psi(_ex1_2);
413 // psi1_evalf should be called here once it becomes available
416 return psi(x).hold();
419 static ex psi1_deriv(const ex & x, unsigned deriv_param)
421 GINAC_ASSERT(deriv_param==0);
423 // d/dx psi(x) -> psi(1,x)
427 static ex psi1_series(const ex & arg,
428 const relational & rel,
433 // Taylor series where there is no pole falls back to polygamma function
435 // On a pole at -m use the recurrence relation
436 // psi(x) == psi(x+1) - 1/z
437 // from which follows
438 // series(psi(x),x==-m,order) ==
439 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
440 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
441 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
442 throw do_taylor(); // caught by function::series()
443 // if we got here we have to care for a simple pole at -m:
444 const numeric m = -ex_to<numeric>(arg_pt);
446 for (numeric p; p<=m; ++p)
447 recur += power(arg+p,_ex_1);
448 return (psi(arg+m+_ex1)-recur).series(rel, order, options);
451 unsigned psi1_SERIAL::serial =
452 function::register_new(function_options("psi", 1).
453 eval_func(psi1_eval).
454 evalf_func(psi1_evalf).
455 derivative_func(psi1_deriv).
456 series_func(psi1_series).
461 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
464 static ex psi2_evalf(const ex & n, const ex & x)
466 if (is_exactly_a<numeric>(n) && is_exactly_a<numeric>(x)) {
468 return psi(ex_to<numeric>(n),ex_to<numeric>(x));
469 } catch (const dunno &e) { }
472 return psi(n,x).hold();
475 /** Evaluation of polygamma-function psi(n,x).
476 * Somebody ought to provide some good numerical evaluation some day... */
477 static ex psi2_eval(const ex & n, const ex & x)
479 // psi(0,x) -> psi(x)
482 // psi(-1,x) -> log(tgamma(x))
483 if (n.is_equal(_ex_1))
484 return log(tgamma(x));
485 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
486 x.info(info_flags::numeric)) {
487 const numeric &nn = ex_to<numeric>(n);
488 const numeric &nx = ex_to<numeric>(x);
489 if (nx.is_integer()) {
491 if (nx.is_equal(*_num1_p))
492 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
493 return pow(*_num_1_p,nn+(*_num1_p))*factorial(nn)*zeta(ex(nn+(*_num1_p)));
494 if (nx.is_positive()) {
495 // use the recurrence relation
496 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
497 // to relate psi(n,m) to psi(n,1):
498 // psi(n,m) == psi(n,1) + r
499 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
501 for (numeric p = 1; p<nx; ++p)
502 recur += pow(p, -nn+(*_num_1_p));
503 recur *= factorial(nn)*pow((*_num_1_p), nn);
504 return recur+psi(n,_ex1);
506 // for non-positive integers there is a pole:
507 throw (pole_error("psi2_eval(): pole",1));
510 if (((*_num2_p)*nx).is_integer()) {
512 if (nx.is_equal(*_num1_2_p))
513 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
514 return pow(*_num_1_p,nn+(*_num1_p))*factorial(nn)*(pow(*_num2_p,nn+(*_num1_p)) + (*_num_1_p))*zeta(ex(nn+(*_num1_p)));
515 if (nx.is_positive()) {
516 const numeric m = nx - (*_num1_2_p);
517 // use the multiplication formula
518 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
519 // to revert to positive integer case
520 return psi(n,(*_num2_p)*m)*pow((*_num2_p),nn+(*_num1_p))-psi(n,m);
522 // use the recurrence relation
523 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
524 // to relate psi(n,-m-1/2) to psi(n,1/2):
525 // psi(n,-m-1/2) == psi(n,1/2) + r
526 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
528 for (numeric p = nx; p<0; ++p)
529 recur += pow(p, -nn+(*_num_1_p));
530 recur *= factorial(nn)*pow(*_num_1_p, nn+(*_num_1_p));
531 return recur+psi(n,_ex1_2);
534 // psi2_evalf should be called here once it becomes available
537 return psi(n, x).hold();
540 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
542 GINAC_ASSERT(deriv_param<2);
544 if (deriv_param==0) {
546 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
548 // d/dx psi(n,x) -> psi(n+1,x)
549 return psi(n+_ex1, x);
552 static ex psi2_series(const ex & n,
554 const relational & rel,
559 // Taylor series where there is no pole falls back to polygamma function
561 // On a pole at -m use the recurrence relation
562 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
563 // from which follows
564 // series(psi(x),x==-m,order) ==
565 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
566 // ... + (x+m)^(-n-1))),x==-m,order);
567 const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
568 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
569 throw do_taylor(); // caught by function::series()
570 // if we got here we have to care for a pole of order n+1 at -m:
571 const numeric m = -ex_to<numeric>(arg_pt);
573 for (numeric p; p<=m; ++p)
574 recur += power(arg+p,-n+_ex_1);
575 recur *= factorial(n)*power(_ex_1,n);
576 return (psi(n, arg+m+_ex1)-recur).series(rel, order, options);
579 unsigned psi2_SERIAL::serial =
580 function::register_new(function_options("psi", 2).
581 eval_func(psi2_eval).
582 evalf_func(psi2_evalf).
583 derivative_func(psi2_deriv).
584 series_func(psi2_series).