1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma-function, Beta-function, Polygamma-functions, and
4 * some related stuff. */
7 * GiNaC Copyright (C) 1999-2001 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"
40 // Logarithm of Gamma function
43 static ex lgamma_evalf(const ex & x)
47 END_TYPECHECK(lgamma(x))
49 return lgamma(ex_to<numeric>(x));
53 /** Evaluation of lgamma(x), the natural logarithm of the Gamma function.
54 * Knows about integer arguments and that's it. Somebody ought to provide
55 * some good numerical evaluation some day...
57 * @exception GiNaC::pole_error("lgamma_eval(): logarithmic pole",0) */
58 static ex lgamma_eval(const ex & x)
60 if (x.info(info_flags::numeric)) {
61 // trap integer arguments:
62 if (x.info(info_flags::integer)) {
63 // lgamma(n) -> log((n-1)!) for postitive n
64 if (x.info(info_flags::posint))
65 return log(factorial(x + _ex_1()));
67 throw (pole_error("lgamma_eval(): logarithmic pole",0));
69 // lgamma_evalf should be called here once it becomes available
72 return lgamma(x).hold();
76 static ex lgamma_deriv(const ex & x, unsigned deriv_param)
78 GINAC_ASSERT(deriv_param==0);
80 // d/dx lgamma(x) -> psi(x)
85 static ex lgamma_series(const ex & arg,
86 const relational & rel,
91 // Taylor series where there is no pole falls back to psi function
93 // On a pole at -m we could use the recurrence relation
94 // lgamma(x) == lgamma(x+1)-log(x)
96 // series(lgamma(x),x==-m,order) ==
97 // series(lgamma(x+m+1)-log(x)...-log(x+m)),x==-m,order);
98 const ex arg_pt = arg.subs(rel);
99 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
100 throw do_taylor(); // caught by function::series()
101 // if we got here we have to care for a simple pole of tgamma(-m):
102 numeric m = -ex_to<numeric>(arg_pt);
104 for (numeric p; p<=m; ++p)
106 return (lgamma(arg+m+_ex1())-recur).series(rel, order, options);
110 REGISTER_FUNCTION(lgamma, eval_func(lgamma_eval).
111 evalf_func(lgamma_evalf).
112 derivative_func(lgamma_deriv).
113 series_func(lgamma_series).
114 latex_name("\\log \\Gamma"));
118 // true Gamma function
121 static ex tgamma_evalf(const ex & x)
125 END_TYPECHECK(tgamma(x))
127 return tgamma(ex_to<numeric>(x));
131 /** Evaluation of tgamma(x), the true Gamma function. Knows about integer
132 * arguments, half-integer arguments and that's it. Somebody ought to provide
133 * some good numerical evaluation some day...
135 * @exception pole_error("tgamma_eval(): simple pole",0) */
136 static ex tgamma_eval(const ex & x)
138 if (x.info(info_flags::numeric)) {
139 // trap integer arguments:
140 if (x.info(info_flags::integer)) {
141 // tgamma(n) -> (n-1)! for postitive n
142 if (x.info(info_flags::posint)) {
143 return factorial(ex_to<numeric>(x).sub(_num1()));
145 throw (pole_error("tgamma_eval(): simple pole",1));
148 // trap half integer arguments:
149 if ((x*2).info(info_flags::integer)) {
150 // trap positive x==(n+1/2)
151 // tgamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
152 if ((x*_ex2()).info(info_flags::posint)) {
153 numeric n = ex_to<numeric>(x).sub(_num1_2());
154 numeric coefficient = doublefactorial(n.mul(_num2()).sub(_num1()));
155 coefficient = coefficient.div(pow(_num2(),n));
156 return coefficient * pow(Pi,_ex1_2());
158 // trap negative x==(-n+1/2)
159 // tgamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
160 numeric n = abs(ex_to<numeric>(x).sub(_num1_2()));
161 numeric coefficient = pow(_num_2(), n);
162 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
163 return coefficient*power(Pi,_ex1_2());
166 // tgamma_evalf should be called here once it becomes available
169 return tgamma(x).hold();
173 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
175 GINAC_ASSERT(deriv_param==0);
177 // d/dx tgamma(x) -> psi(x)*tgamma(x)
178 return psi(x)*tgamma(x);
182 static ex tgamma_series(const ex & arg,
183 const relational & rel,
188 // Taylor series where there is no pole falls back to psi function
190 // On a pole at -m use the recurrence relation
191 // tgamma(x) == tgamma(x+1) / x
192 // from which follows
193 // series(tgamma(x),x==-m,order) ==
194 // series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x==-m,order+1);
195 const ex arg_pt = arg.subs(rel);
196 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
197 throw do_taylor(); // caught by function::series()
198 // if we got here we have to care for a simple pole at -m:
199 numeric m = -ex_to<numeric>(arg_pt);
200 ex ser_denom = _ex1();
201 for (numeric p; p<=m; ++p)
203 return (tgamma(arg+m+_ex1())/ser_denom).series(rel, order+1, options);
207 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
208 evalf_func(tgamma_evalf).
209 derivative_func(tgamma_deriv).
210 series_func(tgamma_series).
211 latex_name("\\Gamma"));
218 static ex beta_evalf(const ex & x, const ex & y)
223 END_TYPECHECK(beta(x,y))
225 return tgamma(ex_to<numeric>(x))*tgamma(ex_to<numeric>(y))/tgamma(ex_to<numeric>(x+y));
229 static ex beta_eval(const ex & x, const ex & y)
231 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
232 // treat all problematic x and y that may not be passed into tgamma,
233 // because they would throw there although beta(x,y) is well-defined
234 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
235 numeric nx(ex_to<numeric>(x));
236 numeric ny(ex_to<numeric>(y));
237 if (nx.is_real() && nx.is_integer() &&
238 ny.is_real() && ny.is_integer()) {
239 if (nx.is_negative()) {
241 return pow(_num_1(), ny)*beta(1-x-y, y);
243 throw (pole_error("beta_eval(): simple pole",1));
245 if (ny.is_negative()) {
247 return pow(_num_1(), nx)*beta(1-y-x, x);
249 throw (pole_error("beta_eval(): simple pole",1));
251 return tgamma(x)*tgamma(y)/tgamma(x+y);
253 // no problem in numerator, but denominator has pole:
254 if ((nx+ny).is_real() &&
255 (nx+ny).is_integer() &&
256 !(nx+ny).is_positive())
259 return tgamma(x)*tgamma(y)/tgamma(x+y);
262 return beta(x,y).hold();
266 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
268 GINAC_ASSERT(deriv_param<2);
271 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
273 retval = (psi(x)-psi(x+y))*beta(x,y);
274 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
276 retval = (psi(y)-psi(x+y))*beta(x,y);
281 static ex beta_series(const ex & arg1,
283 const relational & rel,
288 // Taylor series where there is no pole of one of the tgamma functions
289 // falls back to beta function evaluation. Otherwise, fall back to
290 // tgamma series directly.
291 const ex arg1_pt = arg1.subs(rel);
292 const ex arg2_pt = arg2.subs(rel);
293 GINAC_ASSERT(is_ex_exactly_of_type(rel.lhs(),symbol));
294 const symbol *s = static_cast<symbol *>(rel.lhs().bp);
295 ex arg1_ser, arg2_ser, arg1arg2_ser;
296 if ((!arg1_pt.info(info_flags::integer) || arg1_pt.info(info_flags::positive)) &&
297 (!arg2_pt.info(info_flags::integer) || arg2_pt.info(info_flags::positive)))
298 throw do_taylor(); // caught by function::series()
299 // trap the case where arg1 is on a pole:
300 if (arg1.info(info_flags::integer) && !arg1.info(info_flags::positive))
301 arg1_ser = tgamma(arg1+*s).series(rel, order, options);
303 arg1_ser = tgamma(arg1).series(rel,order);
304 // trap the case where arg2 is on a pole:
305 if (arg2.info(info_flags::integer) && !arg2.info(info_flags::positive))
306 arg2_ser = tgamma(arg2+*s).series(rel, order, options);
308 arg2_ser = tgamma(arg2).series(rel,order);
309 // trap the case where arg1+arg2 is on a pole:
310 if ((arg1+arg2).info(info_flags::integer) && !(arg1+arg2).info(info_flags::positive))
311 arg1arg2_ser = tgamma(arg2+arg1+*s).series(rel, order, options);
313 arg1arg2_ser = tgamma(arg2+arg1).series(rel,order);
314 // compose the result (expanding all the terms):
315 return (arg1_ser*arg2_ser/arg1arg2_ser).series(rel, order, options).expand();
319 REGISTER_FUNCTION(beta, eval_func(beta_eval).
320 evalf_func(beta_evalf).
321 derivative_func(beta_deriv).
322 series_func(beta_series).
323 latex_name("\\mbox{B}").
324 set_symmetry(sy_symm(0, 1)));
328 // Psi-function (aka digamma-function)
331 static ex psi1_evalf(const ex & x)
335 END_TYPECHECK(psi(x))
337 return psi(ex_to<numeric>(x));
340 /** Evaluation of digamma-function psi(x).
341 * Somebody ought to provide some good numerical evaluation some day... */
342 static ex psi1_eval(const ex & x)
344 if (x.info(info_flags::numeric)) {
345 numeric nx = ex_to<numeric>(x);
346 if (nx.is_integer()) {
348 if (nx.is_positive()) {
349 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
351 for (numeric i(nx+_num_1()); i.is_positive(); --i)
355 // for non-positive integers there is a pole:
356 throw (pole_error("psi_eval(): simple pole",1));
359 if ((_num2()*nx).is_integer()) {
361 if (nx.is_positive()) {
362 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
364 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
365 rat += _num2()*i.inverse();
366 return rat-Euler-_ex2()*log(_ex2());
368 // use the recurrence relation
369 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
370 // to relate psi(-m-1/2) to psi(1/2):
371 // psi(-m-1/2) == psi(1/2) + r
372 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
374 for (numeric p(nx); p<0; ++p)
375 recur -= pow(p, _num_1());
376 return recur+psi(_ex1_2());
379 // psi1_evalf should be called here once it becomes available
382 return psi(x).hold();
385 static ex psi1_deriv(const ex & x, unsigned deriv_param)
387 GINAC_ASSERT(deriv_param==0);
389 // d/dx psi(x) -> psi(1,x)
390 return psi(_ex1(), x);
393 static ex psi1_series(const ex & arg,
394 const relational & rel,
399 // Taylor series where there is no pole falls back to polygamma function
401 // On a pole at -m use the recurrence relation
402 // psi(x) == psi(x+1) - 1/z
403 // from which follows
404 // series(psi(x),x==-m,order) ==
405 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
406 const ex arg_pt = arg.subs(rel);
407 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
408 throw do_taylor(); // caught by function::series()
409 // if we got here we have to care for a simple pole at -m:
410 numeric m = -ex_to<numeric>(arg_pt);
412 for (numeric p; p<=m; ++p)
413 recur += power(arg+p,_ex_1());
414 return (psi(arg+m+_ex1())-recur).series(rel, order, options);
417 const unsigned function_index_psi1 =
418 function::register_new(function_options("psi").
419 eval_func(psi1_eval).
420 evalf_func(psi1_evalf).
421 derivative_func(psi1_deriv).
422 series_func(psi1_series).
427 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
430 static ex psi2_evalf(const ex & n, const ex & x)
435 END_TYPECHECK(psi(n,x))
437 return psi(ex_to<numeric>(n), ex_to<numeric>(x));
440 /** Evaluation of polygamma-function psi(n,x).
441 * Somebody ought to provide some good numerical evaluation some day... */
442 static ex psi2_eval(const ex & n, const ex & x)
444 // psi(0,x) -> psi(x)
447 // psi(-1,x) -> log(tgamma(x))
448 if (n.is_equal(_ex_1()))
449 return log(tgamma(x));
450 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
451 x.info(info_flags::numeric)) {
452 numeric nn = ex_to<numeric>(n);
453 numeric nx = ex_to<numeric>(x);
454 if (nx.is_integer()) {
456 if (nx.is_equal(_num1()))
457 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
458 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
459 if (nx.is_positive()) {
460 // use the recurrence relation
461 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
462 // to relate psi(n,m) to psi(n,1):
463 // psi(n,m) == psi(n,1) + r
464 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
466 for (numeric p(1); p<nx; ++p)
467 recur += pow(p, -nn+_num_1());
468 recur *= factorial(nn)*pow(_num_1(), nn);
469 return recur+psi(n,_ex1());
471 // for non-positive integers there is a pole:
472 throw (pole_error("psi2_eval(): pole",1));
475 if ((_num2()*nx).is_integer()) {
477 if (nx.is_equal(_num1_2()))
478 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
479 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
480 if (nx.is_positive()) {
481 numeric m = nx - _num1_2();
482 // use the multiplication formula
483 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
484 // to revert to positive integer case
485 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
487 // use the recurrence relation
488 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
489 // to relate psi(n,-m-1/2) to psi(n,1/2):
490 // psi(n,-m-1/2) == psi(n,1/2) + r
491 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
493 for (numeric p(nx); p<0; ++p)
494 recur += pow(p, -nn+_num_1());
495 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
496 return recur+psi(n,_ex1_2());
499 // psi2_evalf should be called here once it becomes available
502 return psi(n, x).hold();
505 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
507 GINAC_ASSERT(deriv_param<2);
509 if (deriv_param==0) {
511 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
513 // d/dx psi(n,x) -> psi(n+1,x)
514 return psi(n+_ex1(), x);
517 static ex psi2_series(const ex & n,
519 const relational & rel,
524 // Taylor series where there is no pole falls back to polygamma function
526 // On a pole at -m use the recurrence relation
527 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
528 // from which follows
529 // series(psi(x),x==-m,order) ==
530 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
531 // ... + (x+m)^(-n-1))),x==-m,order);
532 const ex arg_pt = arg.subs(rel);
533 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
534 throw do_taylor(); // caught by function::series()
535 // if we got here we have to care for a pole of order n+1 at -m:
536 numeric m = -ex_to<numeric>(arg_pt);
538 for (numeric p; p<=m; ++p)
539 recur += power(arg+p,-n+_ex_1());
540 recur *= factorial(n)*power(_ex_1(),n);
541 return (psi(n, arg+m+_ex1())-recur).series(rel, order, options);
544 const unsigned function_index_psi2 =
545 function::register_new(function_options("psi").
546 eval_func(psi2_eval).
547 evalf_func(psi2_evalf).
548 derivative_func(psi2_deriv).
549 series_func(psi2_series).