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"
40 // Logarithm of Gamma function
43 static ex lgamma_evalf(const ex & x)
45 if (is_exactly_a<numeric>(x)) {
47 return lgamma(ex_to<numeric>(x));
48 } catch (const dunno &e) { }
51 return lgamma(x).hold();
55 /** Evaluation of lgamma(x), the natural logarithm of the Gamma function.
56 * Knows about integer arguments and that's it. Somebody ought to provide
57 * some good numerical evaluation some day...
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 // lgamma_evalf should be called here once it becomes available
74 return lgamma(x).hold();
78 static ex lgamma_deriv(const ex & x, unsigned deriv_param)
80 GINAC_ASSERT(deriv_param==0);
82 // d/dx lgamma(x) -> psi(x)
87 static ex lgamma_series(const ex & arg,
88 const relational & rel,
93 // Taylor series where there is no pole falls back to psi function
95 // On a pole at -m we could use the recurrence relation
96 // lgamma(x) == lgamma(x+1)-log(x)
98 // series(lgamma(x),x==-m,order) ==
99 // series(lgamma(x+m+1)-log(x)...-log(x+m)),x==-m,order);
100 const ex arg_pt = arg.subs(rel);
101 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
102 throw do_taylor(); // caught by function::series()
103 // if we got here we have to care for a simple pole of tgamma(-m):
104 numeric m = -ex_to<numeric>(arg_pt);
106 for (numeric p = 0; p<=m; ++p)
108 return (lgamma(arg+m+_ex1)-recur).series(rel, order, options);
112 REGISTER_FUNCTION(lgamma, eval_func(lgamma_eval).
113 evalf_func(lgamma_evalf).
114 derivative_func(lgamma_deriv).
115 series_func(lgamma_series).
116 latex_name("\\log \\Gamma"));
120 // true Gamma function
123 static ex tgamma_evalf(const ex & x)
125 if (is_exactly_a<numeric>(x)) {
127 return tgamma(ex_to<numeric>(x));
128 } catch (const dunno &e) { }
131 return tgamma(x).hold();
135 /** Evaluation of tgamma(x), the true Gamma function. Knows about integer
136 * arguments, half-integer arguments and that's it. Somebody ought to provide
137 * some good numerical evaluation some day...
139 * @exception pole_error("tgamma_eval(): simple pole",0) */
140 static ex tgamma_eval(const ex & x)
142 if (x.info(info_flags::numeric)) {
143 // trap integer arguments:
144 const numeric two_x = _num2*ex_to<numeric>(x);
145 if (two_x.is_even()) {
146 // tgamma(n) -> (n-1)! for postitive n
147 if (two_x.is_positive()) {
148 return factorial(ex_to<numeric>(x).sub(_num1));
150 throw (pole_error("tgamma_eval(): simple pole",1));
153 // trap half integer arguments:
154 if (two_x.is_integer()) {
155 // trap positive x==(n+1/2)
156 // tgamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
157 if (two_x.is_positive()) {
158 const numeric n = ex_to<numeric>(x).sub(_num1_2);
159 return (doublefactorial(n.mul(_num2).sub(_num1)).div(pow(_num2,n))) * sqrt(Pi);
161 // trap negative x==(-n+1/2)
162 // tgamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
163 const numeric n = abs(ex_to<numeric>(x).sub(_num1_2));
164 return (pow(_num_2, n).div(doublefactorial(n.mul(_num2).sub(_num1))))*sqrt(Pi);
167 // tgamma_evalf should be called here once it becomes available
170 return tgamma(x).hold();
174 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
176 GINAC_ASSERT(deriv_param==0);
178 // d/dx tgamma(x) -> psi(x)*tgamma(x)
179 return psi(x)*tgamma(x);
183 static ex tgamma_series(const ex & arg,
184 const relational & rel,
189 // Taylor series where there is no pole falls back to psi function
191 // On a pole at -m use the recurrence relation
192 // tgamma(x) == tgamma(x+1) / x
193 // from which follows
194 // series(tgamma(x),x==-m,order) ==
195 // series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x==-m,order+1);
196 const ex arg_pt = arg.subs(rel);
197 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
198 throw do_taylor(); // caught by function::series()
199 // if we got here we have to care for a simple pole at -m:
200 const numeric m = -ex_to<numeric>(arg_pt);
202 for (numeric p; p<=m; ++p)
204 return (tgamma(arg+m+_ex1)/ser_denom).series(rel, order+1, options);
208 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
209 evalf_func(tgamma_evalf).
210 derivative_func(tgamma_deriv).
211 series_func(tgamma_series).
212 latex_name("\\Gamma"));
219 static ex beta_evalf(const ex & x, const ex & y)
221 if (is_exactly_a<numeric>(x) && is_exactly_a<numeric>(y)) {
223 return tgamma(ex_to<numeric>(x))*tgamma(ex_to<numeric>(y))/tgamma(ex_to<numeric>(x+y));
224 } catch (const dunno &e) { }
227 return beta(x,y).hold();
231 static ex beta_eval(const ex & x, const ex & y)
233 if (x.is_equal(_ex1))
235 if (y.is_equal(_ex1))
237 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
238 // treat all problematic x and y that may not be passed into tgamma,
239 // because they would throw there although beta(x,y) is well-defined
240 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
241 const numeric &nx = ex_to<numeric>(x);
242 const numeric &ny = ex_to<numeric>(y);
243 if (nx.is_real() && nx.is_integer() &&
244 ny.is_real() && ny.is_integer()) {
245 if (nx.is_negative()) {
247 return pow(_num_1, ny)*beta(1-x-y, y);
249 throw (pole_error("beta_eval(): simple pole",1));
251 if (ny.is_negative()) {
253 return pow(_num_1, nx)*beta(1-y-x, x);
255 throw (pole_error("beta_eval(): simple pole",1));
257 return tgamma(x)*tgamma(y)/tgamma(x+y);
259 // no problem in numerator, but denominator has pole:
260 if ((nx+ny).is_real() &&
261 (nx+ny).is_integer() &&
262 !(nx+ny).is_positive())
264 // beta_evalf should be called here once it becomes available
267 return beta(x,y).hold();
271 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
273 GINAC_ASSERT(deriv_param<2);
276 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
278 retval = (psi(x)-psi(x+y))*beta(x,y);
279 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
281 retval = (psi(y)-psi(x+y))*beta(x,y);
286 static ex beta_series(const ex & arg1,
288 const relational & rel,
293 // Taylor series where there is no pole of one of the tgamma functions
294 // falls back to beta function evaluation. Otherwise, fall back to
295 // tgamma series directly.
296 const ex arg1_pt = arg1.subs(rel);
297 const ex arg2_pt = arg2.subs(rel);
298 GINAC_ASSERT(is_exactly_a<symbol>(rel.lhs()));
299 const symbol &s = ex_to<symbol>(rel.lhs());
300 ex arg1_ser, arg2_ser, arg1arg2_ser;
301 if ((!arg1_pt.info(info_flags::integer) || arg1_pt.info(info_flags::positive)) &&
302 (!arg2_pt.info(info_flags::integer) || arg2_pt.info(info_flags::positive)))
303 throw do_taylor(); // caught by function::series()
304 // trap the case where arg1 is on a pole:
305 if (arg1.info(info_flags::integer) && !arg1.info(info_flags::positive))
306 arg1_ser = tgamma(arg1+s).series(rel, order, options);
308 arg1_ser = tgamma(arg1).series(rel,order);
309 // trap the case where arg2 is on a pole:
310 if (arg2.info(info_flags::integer) && !arg2.info(info_flags::positive))
311 arg2_ser = tgamma(arg2+s).series(rel, order, options);
313 arg2_ser = tgamma(arg2).series(rel,order);
314 // trap the case where arg1+arg2 is on a pole:
315 if ((arg1+arg2).info(info_flags::integer) && !(arg1+arg2).info(info_flags::positive))
316 arg1arg2_ser = tgamma(arg2+arg1+s).series(rel, order, options);
318 arg1arg2_ser = tgamma(arg2+arg1).series(rel,order);
319 // compose the result (expanding all the terms):
320 return (arg1_ser*arg2_ser/arg1arg2_ser).series(rel, order, options).expand();
324 REGISTER_FUNCTION(beta, eval_func(beta_eval).
325 evalf_func(beta_evalf).
326 derivative_func(beta_deriv).
327 series_func(beta_series).
328 latex_name("\\mbox{B}").
329 set_symmetry(sy_symm(0, 1)));
333 // Psi-function (aka digamma-function)
336 static ex psi1_evalf(const ex & x)
338 if (is_exactly_a<numeric>(x)) {
340 return psi(ex_to<numeric>(x));
341 } catch (const dunno &e) { }
344 return psi(x).hold();
347 /** Evaluation of digamma-function psi(x).
348 * Somebody ought to provide some good numerical evaluation some day... */
349 static ex psi1_eval(const ex & x)
351 if (x.info(info_flags::numeric)) {
352 const numeric &nx = ex_to<numeric>(x);
353 if (nx.is_integer()) {
355 if (nx.is_positive()) {
356 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
358 for (numeric i(nx+_num_1); i>0; --i)
362 // for non-positive integers there is a pole:
363 throw (pole_error("psi_eval(): simple pole",1));
366 if ((_num2*nx).is_integer()) {
368 if (nx.is_positive()) {
369 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
371 for (numeric i = (nx+_num_1)*_num2; i>0; i-=_num2)
372 rat += _num2*i.inverse();
373 return rat-Euler-_ex2*log(_ex2);
375 // use the recurrence relation
376 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
377 // to relate psi(-m-1/2) to psi(1/2):
378 // psi(-m-1/2) == psi(1/2) + r
379 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
381 for (numeric p = nx; p<0; ++p)
382 recur -= pow(p, _num_1);
383 return recur+psi(_ex1_2);
386 // psi1_evalf should be called here once it becomes available
389 return psi(x).hold();
392 static ex psi1_deriv(const ex & x, unsigned deriv_param)
394 GINAC_ASSERT(deriv_param==0);
396 // d/dx psi(x) -> psi(1,x)
400 static ex psi1_series(const ex & arg,
401 const relational & rel,
406 // Taylor series where there is no pole falls back to polygamma function
408 // On a pole at -m use the recurrence relation
409 // psi(x) == psi(x+1) - 1/z
410 // from which follows
411 // series(psi(x),x==-m,order) ==
412 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
413 const ex arg_pt = arg.subs(rel);
414 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
415 throw do_taylor(); // caught by function::series()
416 // if we got here we have to care for a simple pole at -m:
417 const numeric m = -ex_to<numeric>(arg_pt);
419 for (numeric p; p<=m; ++p)
420 recur += power(arg+p,_ex_1);
421 return (psi(arg+m+_ex1)-recur).series(rel, order, options);
424 const unsigned function_index_psi1 =
425 function::register_new(function_options("psi").
426 eval_func(psi1_eval).
427 evalf_func(psi1_evalf).
428 derivative_func(psi1_deriv).
429 series_func(psi1_series).
434 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
437 static ex psi2_evalf(const ex & n, const ex & x)
439 if (is_exactly_a<numeric>(n) && is_exactly_a<numeric>(x)) {
441 return psi(ex_to<numeric>(n),ex_to<numeric>(x));
442 } catch (const dunno &e) { }
445 return psi(n,x).hold();
448 /** Evaluation of polygamma-function psi(n,x).
449 * Somebody ought to provide some good numerical evaluation some day... */
450 static ex psi2_eval(const ex & n, const ex & x)
452 // psi(0,x) -> psi(x)
455 // psi(-1,x) -> log(tgamma(x))
456 if (n.is_equal(_ex_1))
457 return log(tgamma(x));
458 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
459 x.info(info_flags::numeric)) {
460 const numeric &nn = ex_to<numeric>(n);
461 const numeric &nx = ex_to<numeric>(x);
462 if (nx.is_integer()) {
464 if (nx.is_equal(_num1))
465 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
466 return pow(_num_1,nn+_num1)*factorial(nn)*zeta(ex(nn+_num1));
467 if (nx.is_positive()) {
468 // use the recurrence relation
469 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
470 // to relate psi(n,m) to psi(n,1):
471 // psi(n,m) == psi(n,1) + r
472 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
474 for (numeric p = 1; p<nx; ++p)
475 recur += pow(p, -nn+_num_1);
476 recur *= factorial(nn)*pow(_num_1, nn);
477 return recur+psi(n,_ex1);
479 // for non-positive integers there is a pole:
480 throw (pole_error("psi2_eval(): pole",1));
483 if ((_num2*nx).is_integer()) {
485 if (nx.is_equal(_num1_2))
486 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
487 return pow(_num_1,nn+_num1)*factorial(nn)*(pow(_num2,nn+_num1) + _num_1)*zeta(ex(nn+_num1));
488 if (nx.is_positive()) {
489 const numeric m = nx - _num1_2;
490 // use the multiplication formula
491 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
492 // to revert to positive integer case
493 return psi(n,_num2*m)*pow(_num2,nn+_num1)-psi(n,m);
495 // use the recurrence relation
496 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
497 // to relate psi(n,-m-1/2) to psi(n,1/2):
498 // psi(n,-m-1/2) == psi(n,1/2) + r
499 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
501 for (numeric p = nx; p<0; ++p)
502 recur += pow(p, -nn+_num_1);
503 recur *= factorial(nn)*pow(_num_1, nn+_num_1);
504 return recur+psi(n,_ex1_2);
507 // psi2_evalf should be called here once it becomes available
510 return psi(n, x).hold();
513 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
515 GINAC_ASSERT(deriv_param<2);
517 if (deriv_param==0) {
519 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
521 // d/dx psi(n,x) -> psi(n+1,x)
522 return psi(n+_ex1, x);
525 static ex psi2_series(const ex & n,
527 const relational & rel,
532 // Taylor series where there is no pole falls back to polygamma function
534 // On a pole at -m use the recurrence relation
535 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
536 // from which follows
537 // series(psi(x),x==-m,order) ==
538 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
539 // ... + (x+m)^(-n-1))),x==-m,order);
540 const ex arg_pt = arg.subs(rel);
541 if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
542 throw do_taylor(); // caught by function::series()
543 // if we got here we have to care for a pole of order n+1 at -m:
544 const numeric m = -ex_to<numeric>(arg_pt);
546 for (numeric p; p<=m; ++p)
547 recur += power(arg+p,-n+_ex_1);
548 recur *= factorial(n)*power(_ex_1,n);
549 return (psi(n, arg+m+_ex1)-recur).series(rel, order, options);
552 const unsigned function_index_psi2 =
553 function::register_new(function_options("psi").
554 eval_func(psi2_eval).
555 evalf_func(psi2_evalf).
556 derivative_func(psi2_deriv).
557 series_func(psi2_series).