1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma-function, Beta-function, Polygamma-functions, and
4 * some related stuff. */
7 * GiNaC Copyright (C) 1999-2000 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
33 #include "relational.h"
37 #ifndef NO_NAMESPACE_GINAC
39 #endif // ndef NO_NAMESPACE_GINAC
42 // Logarithm of Gamma function
45 static ex lgamma_evalf(const ex & x)
49 END_TYPECHECK(lgamma(x))
51 return lgamma(ex_to_numeric(x));
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 std::domain_error("lgamma_eval(): logarithmic pole") */
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.exadd(_ex_1())));
69 throw (std::domain_error("lgamma_eval(): logarithmic pole"));
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 & x, const relational & rel, int order)
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 // This, however, seems to fail utterly because you run into branch-cut
99 // problems. Somebody ought to implement it some day using an asymptotic
100 // series for tgamma:
101 const ex x_pt = x.subs(rel);
102 if (!x_pt.info(info_flags::integer) || x_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 throw (std::domain_error("lgamma_series: please implemnt my at the poles"));
106 return _ex0(); // not reached
110 REGISTER_FUNCTION(lgamma, eval_func(lgamma_eval).
111 evalf_func(lgamma_evalf).
112 derivative_func(lgamma_deriv).
113 series_func(lgamma_series));
117 // true Gamma function
120 static ex tgamma_evalf(const ex & x)
124 END_TYPECHECK(tgamma(x))
126 return tgamma(ex_to_numeric(x));
130 /** Evaluation of tgamma(x), the true Gamma function. Knows about integer
131 * arguments, half-integer arguments and that's it. Somebody ought to provide
132 * some good numerical evaluation some day...
134 * @exception std::domain_error("tgamma_eval(): simple pole") */
135 static ex tgamma_eval(const ex & x)
137 if (x.info(info_flags::numeric)) {
138 // trap integer arguments:
139 if (x.info(info_flags::integer)) {
140 // tgamma(n) -> (n-1)! for postitive n
141 if (x.info(info_flags::posint)) {
142 return factorial(ex_to_numeric(x).sub(_num1()));
144 throw (std::domain_error("tgamma_eval(): simple pole"));
147 // trap half integer arguments:
148 if ((x*2).info(info_flags::integer)) {
149 // trap positive x==(n+1/2)
150 // tgamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
151 if ((x*_ex2()).info(info_flags::posint)) {
152 numeric n = ex_to_numeric(x).sub(_num1_2());
153 numeric coefficient = doublefactorial(n.mul(_num2()).sub(_num1()));
154 coefficient = coefficient.div(pow(_num2(),n));
155 return coefficient * pow(Pi,_ex1_2());
157 // trap negative x==(-n+1/2)
158 // tgamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
159 numeric n = abs(ex_to_numeric(x).sub(_num1_2()));
160 numeric coefficient = pow(_num_2(), n);
161 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
162 return coefficient*power(Pi,_ex1_2());
165 // tgamma_evalf should be called here once it becomes available
168 return tgamma(x).hold();
172 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
174 GINAC_ASSERT(deriv_param==0);
176 // d/dx tgamma(x) -> psi(x)*tgamma(x)
177 return psi(x)*tgamma(x);
181 static ex tgamma_series(const ex & x, const relational & rel, int order)
184 // Taylor series where there is no pole falls back to psi function
186 // On a pole at -m use the recurrence relation
187 // tgamma(x) == tgamma(x+1) / x
188 // from which follows
189 // series(tgamma(x),x==-m,order) ==
190 // series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x==-m,order+1);
191 const ex x_pt = x.subs(rel);
192 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
193 throw do_taylor(); // caught by function::series()
194 // if we got here we have to care for a simple pole at -m:
195 numeric m = -ex_to_numeric(x_pt);
196 ex ser_denom = _ex1();
197 for (numeric p; p<=m; ++p)
199 return (tgamma(x+m+_ex1())/ser_denom).series(rel, order+1);
203 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
204 evalf_func(tgamma_evalf).
205 derivative_func(tgamma_deriv).
206 series_func(tgamma_series));
213 static ex beta_evalf(const ex & x, const ex & y)
218 END_TYPECHECK(beta(x,y))
220 return tgamma(ex_to_numeric(x))*tgamma(ex_to_numeric(y))/tgamma(ex_to_numeric(x+y));
224 static ex beta_eval(const ex & x, const ex & y)
226 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
227 // treat all problematic x and y that may not be passed into tgamma,
228 // because they would throw there although beta(x,y) is well-defined
229 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
230 numeric nx(ex_to_numeric(x));
231 numeric ny(ex_to_numeric(y));
232 if (nx.is_real() && nx.is_integer() &&
233 ny.is_real() && ny.is_integer()) {
234 if (nx.is_negative()) {
236 return pow(_num_1(), ny)*beta(1-x-y, y);
238 throw (std::domain_error("beta_eval(): simple pole"));
240 if (ny.is_negative()) {
242 return pow(_num_1(), nx)*beta(1-y-x, x);
244 throw (std::domain_error("beta_eval(): simple pole"));
246 return tgamma(x)*tgamma(y)/tgamma(x+y);
248 // no problem in numerator, but denominator has pole:
249 if ((nx+ny).is_real() &&
250 (nx+ny).is_integer() &&
251 !(nx+ny).is_positive())
254 return tgamma(x)*tgamma(y)/tgamma(x+y);
257 return beta(x,y).hold();
261 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
263 GINAC_ASSERT(deriv_param<2);
266 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
268 retval = (psi(x)-psi(x+y))*beta(x,y);
269 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
271 retval = (psi(y)-psi(x+y))*beta(x,y);
276 static ex beta_series(const ex & x, const ex & y, const relational & rel, int order)
279 // Taylor series where there is no pole of one of the tgamma functions
280 // falls back to beta function evaluation. Otherwise, fall back to
281 // tgamma series directly.
282 const ex x_pt = x.subs(rel);
283 const ex y_pt = y.subs(rel);
284 GINAC_ASSERT(is_ex_exactly_of_type(rel.lhs(),symbol));
285 const symbol *s = static_cast<symbol *>(rel.lhs().bp);
286 ex x_ser, y_ser, xy_ser;
287 if ((!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive)) &&
288 (!y_pt.info(info_flags::integer) || y_pt.info(info_flags::positive)))
289 throw do_taylor(); // caught by function::series()
290 // trap the case where x is on a pole directly:
291 if (x.info(info_flags::integer) && !x.info(info_flags::positive))
292 x_ser = tgamma(x+*s).series(rel,order);
294 x_ser = tgamma(x).series(rel,order);
295 // trap the case where y is on a pole directly:
296 if (y.info(info_flags::integer) && !y.info(info_flags::positive))
297 y_ser = tgamma(y+*s).series(rel,order);
299 y_ser = tgamma(y).series(rel,order);
300 // trap the case where y is on a pole directly:
301 if ((x+y).info(info_flags::integer) && !(x+y).info(info_flags::positive))
302 xy_ser = tgamma(y+x+*s).series(rel,order);
304 xy_ser = tgamma(y+x).series(rel,order);
305 // compose the result:
306 return (x_ser*y_ser/xy_ser).series(rel,order);
310 REGISTER_FUNCTION(beta, eval_func(beta_eval).
311 evalf_func(beta_evalf).
312 derivative_func(beta_deriv).
313 series_func(beta_series));
317 // Psi-function (aka digamma-function)
320 static ex psi1_evalf(const ex & x)
324 END_TYPECHECK(psi(x))
326 return psi(ex_to_numeric(x));
329 /** Evaluation of digamma-function psi(x).
330 * Somebody ought to provide some good numerical evaluation some day... */
331 static ex psi1_eval(const ex & x)
333 if (x.info(info_flags::numeric)) {
334 numeric nx = ex_to_numeric(x);
335 if (nx.is_integer()) {
337 if (nx.is_positive()) {
338 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
340 for (numeric i(nx+_num_1()); i.is_positive(); --i)
344 // for non-positive integers there is a pole:
345 throw (std::domain_error("psi_eval(): simple pole"));
348 if ((_num2()*nx).is_integer()) {
350 if (nx.is_positive()) {
351 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
353 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
354 rat += _num2()*i.inverse();
355 return rat-Euler-_ex2()*log(_ex2());
357 // use the recurrence relation
358 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
359 // to relate psi(-m-1/2) to psi(1/2):
360 // psi(-m-1/2) == psi(1/2) + r
361 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
363 for (numeric p(nx); p<0; ++p)
364 recur -= pow(p, _num_1());
365 return recur+psi(_ex1_2());
368 // psi1_evalf should be called here once it becomes available
371 return psi(x).hold();
374 static ex psi1_deriv(const ex & x, unsigned deriv_param)
376 GINAC_ASSERT(deriv_param==0);
378 // d/dx psi(x) -> psi(1,x)
379 return psi(_ex1(), x);
382 static ex psi1_series(const ex & x, const relational & rel, int order)
385 // Taylor series where there is no pole falls back to polygamma function
387 // On a pole at -m use the recurrence relation
388 // psi(x) == psi(x+1) - 1/z
389 // from which follows
390 // series(psi(x),x==-m,order) ==
391 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
392 const ex x_pt = x.subs(rel);
393 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
394 throw do_taylor(); // caught by function::series()
395 // if we got here we have to care for a simple pole at -m:
396 numeric m = -ex_to_numeric(x_pt);
398 for (numeric p; p<=m; ++p)
399 recur += power(x+p,_ex_1());
400 return (psi(x+m+_ex1())-recur).series(rel, order);
403 const unsigned function_index_psi1 =
404 function::register_new(function_options("psi").
405 eval_func(psi1_eval).
406 evalf_func(psi1_evalf).
407 derivative_func(psi1_deriv).
408 series_func(psi1_series).
412 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
415 static ex psi2_evalf(const ex & n, const ex & x)
420 END_TYPECHECK(psi(n,x))
422 return psi(ex_to_numeric(n), ex_to_numeric(x));
425 /** Evaluation of polygamma-function psi(n,x).
426 * Somebody ought to provide some good numerical evaluation some day... */
427 static ex psi2_eval(const ex & n, const ex & x)
429 // psi(0,x) -> psi(x)
432 // psi(-1,x) -> log(tgamma(x))
433 if (n.is_equal(_ex_1()))
434 return log(tgamma(x));
435 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
436 x.info(info_flags::numeric)) {
437 numeric nn = ex_to_numeric(n);
438 numeric nx = ex_to_numeric(x);
439 if (nx.is_integer()) {
441 if (nx.is_equal(_num1()))
442 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
443 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
444 if (nx.is_positive()) {
445 // use the recurrence relation
446 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
447 // to relate psi(n,m) to psi(n,1):
448 // psi(n,m) == psi(n,1) + r
449 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
451 for (numeric p(1); p<nx; ++p)
452 recur += pow(p, -nn+_num_1());
453 recur *= factorial(nn)*pow(_num_1(), nn);
454 return recur+psi(n,_ex1());
456 // for non-positive integers there is a pole:
457 throw (std::domain_error("psi2_eval(): pole"));
460 if ((_num2()*nx).is_integer()) {
462 if (nx.is_equal(_num1_2()))
463 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
464 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
465 if (nx.is_positive()) {
466 numeric m = nx - _num1_2();
467 // use the multiplication formula
468 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
469 // to revert to positive integer case
470 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
472 // use the recurrence relation
473 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
474 // to relate psi(n,-m-1/2) to psi(n,1/2):
475 // psi(n,-m-1/2) == psi(n,1/2) + r
476 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
478 for (numeric p(nx); p<0; ++p)
479 recur += pow(p, -nn+_num_1());
480 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
481 return recur+psi(n,_ex1_2());
484 // psi2_evalf should be called here once it becomes available
487 return psi(n, x).hold();
490 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
492 GINAC_ASSERT(deriv_param<2);
494 if (deriv_param==0) {
496 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
498 // d/dx psi(n,x) -> psi(n+1,x)
499 return psi(n+_ex1(), x);
502 static ex psi2_series(const ex & n, const ex & x, const relational & rel, int order)
505 // Taylor series where there is no pole falls back to polygamma function
507 // On a pole at -m use the recurrence relation
508 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
509 // from which follows
510 // series(psi(x),x==-m,order) ==
511 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
512 // ... + (x+m)^(-n-1))),x==-m,order);
513 const ex x_pt = x.subs(rel);
514 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
515 throw do_taylor(); // caught by function::series()
516 // if we got here we have to care for a pole of order n+1 at -m:
517 numeric m = -ex_to_numeric(x_pt);
519 for (numeric p; p<=m; ++p)
520 recur += power(x+p,-n+_ex_1());
521 recur *= factorial(n)*power(_ex_1(),n);
522 return (psi(n, x+m+_ex1())-recur).series(rel, order);
525 const unsigned function_index_psi2 =
526 function::register_new(function_options("psi").
527 eval_func(psi2_eval).
528 evalf_func(psi2_evalf).
529 derivative_func(psi2_deriv).
530 series_func(psi2_series).
534 #ifndef NO_NAMESPACE_GINAC
536 #endif // ndef NO_NAMESPACE_GINAC