1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma-function, Beta-function, Polygamma-functions, and
4 * some related stuff. */
7 * GiNaC Copyright (C) 1999-2002 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
32 #include "relational.h"
33 #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 * Knows about integer arguments and that's it. Somebody ought to provide
58 * some good numerical evaluation some day...
60 * @exception GiNaC::pole_error("lgamma_eval(): logarithmic pole",0) */
61 static ex lgamma_eval(const ex & x)
63 if (x.info(info_flags::numeric)) {
64 // trap integer arguments:
65 if (x.info(info_flags::integer)) {
66 // lgamma(n) -> log((n-1)!) for postitive n
67 if (x.info(info_flags::posint))
68 return log(factorial(x + _ex_1));
70 throw (pole_error("lgamma_eval(): logarithmic pole",0));
72 // lgamma_evalf should be called here once it becomes available
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);
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*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));
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);
160 return (doublefactorial(n.mul(_num2).sub(_num1)).div(pow(_num2,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));
165 return (pow(_num_2, n).div(doublefactorial(n.mul(_num2).sub(_num1))))*sqrt(Pi);
168 // tgamma_evalf should be called here once it becomes available
171 return tgamma(x).hold();
175 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
177 GINAC_ASSERT(deriv_param==0);
179 // d/dx tgamma(x) -> psi(x)*tgamma(x)
180 return psi(x)*tgamma(x);
184 static ex tgamma_series(const ex & arg,
185 const relational & rel,
190 // Taylor series where there is no pole falls back to psi function
192 // On a pole at -m use the recurrence relation
193 // tgamma(x) == tgamma(x+1) / x
194 // from which follows
195 // series(tgamma(x),x==-m,order) ==
196 // series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x==-m,order+1);
197 const ex arg_pt = arg.subs(rel);
198 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
199 throw do_taylor(); // caught by function::series()
200 // if we got here we have to care for a simple pole at -m:
201 const numeric m = -ex_to<numeric>(arg_pt);
203 for (numeric p; p<=m; ++p)
205 return (tgamma(arg+m+_ex1)/ser_denom).series(rel, order+1, options);
209 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
210 evalf_func(tgamma_evalf).
211 derivative_func(tgamma_deriv).
212 series_func(tgamma_series).
213 latex_name("\\Gamma"));
220 static ex beta_evalf(const ex & x, const ex & y)
222 if (is_exactly_a<numeric>(x) && is_exactly_a<numeric>(y)) {
224 return tgamma(ex_to<numeric>(x))*tgamma(ex_to<numeric>(y))/tgamma(ex_to<numeric>(x+y));
225 } catch (const dunno &e) { }
228 return beta(x,y).hold();
232 static ex beta_eval(const ex & x, const ex & y)
234 if (x.is_equal(_ex1))
236 if (y.is_equal(_ex1))
238 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
239 // treat all problematic x and y that may not be passed into tgamma,
240 // because they would throw there although beta(x,y) is well-defined
241 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
242 const numeric &nx = ex_to<numeric>(x);
243 const numeric &ny = ex_to<numeric>(y);
244 if (nx.is_real() && nx.is_integer() &&
245 ny.is_real() && ny.is_integer()) {
246 if (nx.is_negative()) {
248 return pow(_num_1, ny)*beta(1-x-y, y);
250 throw (pole_error("beta_eval(): simple pole",1));
252 if (ny.is_negative()) {
254 return pow(_num_1, nx)*beta(1-y-x, x);
256 throw (pole_error("beta_eval(): simple pole",1));
258 return tgamma(x)*tgamma(y)/tgamma(x+y);
260 // no problem in numerator, but denominator has pole:
261 if ((nx+ny).is_real() &&
262 (nx+ny).is_integer() &&
263 !(nx+ny).is_positive())
265 // beta_evalf should be called here once it becomes available
268 return beta(x,y).hold();
272 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
274 GINAC_ASSERT(deriv_param<2);
277 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
279 retval = (psi(x)-psi(x+y))*beta(x,y);
280 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
282 retval = (psi(y)-psi(x+y))*beta(x,y);
287 static ex beta_series(const ex & arg1,
289 const relational & rel,
294 // Taylor series where there is no pole of one of the tgamma functions
295 // falls back to beta function evaluation. Otherwise, fall back to
296 // tgamma series directly.
297 const ex arg1_pt = arg1.subs(rel);
298 const ex arg2_pt = arg2.subs(rel);
299 GINAC_ASSERT(is_exactly_a<symbol>(rel.lhs()));
300 const symbol &s = ex_to<symbol>(rel.lhs());
301 ex arg1_ser, arg2_ser, arg1arg2_ser;
302 if ((!arg1_pt.info(info_flags::integer) || arg1_pt.info(info_flags::positive)) &&
303 (!arg2_pt.info(info_flags::integer) || arg2_pt.info(info_flags::positive)))
304 throw do_taylor(); // caught by function::series()
305 // trap the case where arg1 is on a pole:
306 if (arg1.info(info_flags::integer) && !arg1.info(info_flags::positive))
307 arg1_ser = tgamma(arg1+s).series(rel, order, options);
309 arg1_ser = tgamma(arg1).series(rel,order);
310 // trap the case where arg2 is on a pole:
311 if (arg2.info(info_flags::integer) && !arg2.info(info_flags::positive))
312 arg2_ser = tgamma(arg2+s).series(rel, order, options);
314 arg2_ser = tgamma(arg2).series(rel,order);
315 // trap the case where arg1+arg2 is on a pole:
316 if ((arg1+arg2).info(info_flags::integer) && !(arg1+arg2).info(info_flags::positive))
317 arg1arg2_ser = tgamma(arg2+arg1+s).series(rel, order, options);
319 arg1arg2_ser = tgamma(arg2+arg1).series(rel,order);
320 // compose the result (expanding all the terms):
321 return (arg1_ser*arg2_ser/arg1arg2_ser).series(rel, order, options).expand();
325 REGISTER_FUNCTION(beta, eval_func(beta_eval).
326 evalf_func(beta_evalf).
327 derivative_func(beta_deriv).
328 series_func(beta_series).
329 latex_name("\\mbox{B}").
330 set_symmetry(sy_symm(0, 1)));
334 // Psi-function (aka digamma-function)
337 static ex psi1_evalf(const ex & x)
339 if (is_exactly_a<numeric>(x)) {
341 return psi(ex_to<numeric>(x));
342 } catch (const dunno &e) { }
345 return psi(x).hold();
348 /** Evaluation of digamma-function psi(x).
349 * Somebody ought to provide some good numerical evaluation some day... */
350 static ex psi1_eval(const ex & x)
352 if (x.info(info_flags::numeric)) {
353 const numeric &nx = ex_to<numeric>(x);
354 if (nx.is_integer()) {
356 if (nx.is_positive()) {
357 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
359 for (numeric i(nx+_num_1); i>0; --i)
363 // for non-positive integers there is a pole:
364 throw (pole_error("psi_eval(): simple pole",1));
367 if ((_num2*nx).is_integer()) {
369 if (nx.is_positive()) {
370 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
372 for (numeric i = (nx+_num_1)*_num2; i>0; i-=_num2)
373 rat += _num2*i.inverse();
374 return rat-Euler-_ex2*log(_ex2);
376 // use the recurrence relation
377 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
378 // to relate psi(-m-1/2) to psi(1/2):
379 // psi(-m-1/2) == psi(1/2) + r
380 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
382 for (numeric p = nx; p<0; ++p)
383 recur -= pow(p, _num_1);
384 return recur+psi(_ex1_2);
387 // psi1_evalf should be called here once it becomes available
390 return psi(x).hold();
393 static ex psi1_deriv(const ex & x, unsigned deriv_param)
395 GINAC_ASSERT(deriv_param==0);
397 // d/dx psi(x) -> psi(1,x)
401 static ex psi1_series(const ex & arg,
402 const relational & rel,
407 // Taylor series where there is no pole falls back to polygamma function
409 // On a pole at -m use the recurrence relation
410 // psi(x) == psi(x+1) - 1/z
411 // from which follows
412 // series(psi(x),x==-m,order) ==
413 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
414 const ex arg_pt = arg.subs(rel);
415 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
416 throw do_taylor(); // caught by function::series()
417 // if we got here we have to care for a simple pole at -m:
418 const numeric m = -ex_to<numeric>(arg_pt);
420 for (numeric p; p<=m; ++p)
421 recur += power(arg+p,_ex_1);
422 return (psi(arg+m+_ex1)-recur).series(rel, order, options);
425 const unsigned function_index_psi1 =
426 function::register_new(function_options("psi").
427 eval_func(psi1_eval).
428 evalf_func(psi1_evalf).
429 derivative_func(psi1_deriv).
430 series_func(psi1_series).
435 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
438 static ex psi2_evalf(const ex & n, const ex & x)
440 if (is_exactly_a<numeric>(n) && is_exactly_a<numeric>(x)) {
442 return psi(ex_to<numeric>(n),ex_to<numeric>(x));
443 } catch (const dunno &e) { }
446 return psi(n,x).hold();
449 /** Evaluation of polygamma-function psi(n,x).
450 * Somebody ought to provide some good numerical evaluation some day... */
451 static ex psi2_eval(const ex & n, const ex & x)
453 // psi(0,x) -> psi(x)
456 // psi(-1,x) -> log(tgamma(x))
457 if (n.is_equal(_ex_1))
458 return log(tgamma(x));
459 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
460 x.info(info_flags::numeric)) {
461 const numeric &nn = ex_to<numeric>(n);
462 const numeric &nx = ex_to<numeric>(x);
463 if (nx.is_integer()) {
465 if (nx.is_equal(_num1))
466 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
467 return pow(_num_1,nn+_num1)*factorial(nn)*zeta(ex(nn+_num1));
468 if (nx.is_positive()) {
469 // use the recurrence relation
470 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
471 // to relate psi(n,m) to psi(n,1):
472 // psi(n,m) == psi(n,1) + r
473 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
475 for (numeric p = 1; p<nx; ++p)
476 recur += pow(p, -nn+_num_1);
477 recur *= factorial(nn)*pow(_num_1, nn);
478 return recur+psi(n,_ex1);
480 // for non-positive integers there is a pole:
481 throw (pole_error("psi2_eval(): pole",1));
484 if ((_num2*nx).is_integer()) {
486 if (nx.is_equal(_num1_2))
487 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
488 return pow(_num_1,nn+_num1)*factorial(nn)*(pow(_num2,nn+_num1) + _num_1)*zeta(ex(nn+_num1));
489 if (nx.is_positive()) {
490 const numeric m = nx - _num1_2;
491 // use the multiplication formula
492 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
493 // to revert to positive integer case
494 return psi(n,_num2*m)*pow(_num2,nn+_num1)-psi(n,m);
496 // use the recurrence relation
497 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
498 // to relate psi(n,-m-1/2) to psi(n,1/2):
499 // psi(n,-m-1/2) == psi(n,1/2) + r
500 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
502 for (numeric p = nx; p<0; ++p)
503 recur += pow(p, -nn+_num_1);
504 recur *= factorial(nn)*pow(_num_1, nn+_num_1);
505 return recur+psi(n,_ex1_2);
508 // psi2_evalf should be called here once it becomes available
511 return psi(n, x).hold();
514 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
516 GINAC_ASSERT(deriv_param<2);
518 if (deriv_param==0) {
520 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
522 // d/dx psi(n,x) -> psi(n+1,x)
523 return psi(n+_ex1, x);
526 static ex psi2_series(const ex & n,
528 const relational & rel,
533 // Taylor series where there is no pole falls back to polygamma function
535 // On a pole at -m use the recurrence relation
536 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
537 // from which follows
538 // series(psi(x),x==-m,order) ==
539 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
540 // ... + (x+m)^(-n-1))),x==-m,order);
541 const ex arg_pt = arg.subs(rel);
542 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
543 throw do_taylor(); // caught by function::series()
544 // if we got here we have to care for a pole of order n+1 at -m:
545 const numeric m = -ex_to<numeric>(arg_pt);
547 for (numeric p; p<=m; ++p)
548 recur += power(arg+p,-n+_ex_1);
549 recur *= factorial(n)*power(_ex_1,n);
550 return (psi(n, arg+m+_ex1)-recur).series(rel, order, options);
553 const unsigned function_index_psi2 =
554 function::register_new(function_options("psi").
555 eval_func(psi2_eval).
556 evalf_func(psi2_evalf).
557 derivative_func(psi2_deriv).
558 series_func(psi2_series).