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(): simple 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)
87 // need to implement lgamma_series.
89 REGISTER_FUNCTION(lgamma, eval_func(lgamma_eval).
90 evalf_func(lgamma_evalf).
91 derivative_func(lgamma_deriv));
95 // true Gamma function
98 static ex tgamma_evalf(const ex & x)
102 END_TYPECHECK(tgamma(x))
104 return tgamma(ex_to_numeric(x));
108 /** Evaluation of tgamma(x), the true Gamma function. Knows about integer
109 * arguments, half-integer arguments and that's it. Somebody ought to provide
110 * some good numerical evaluation some day...
112 * @exception std::domain_error("tgamma_eval(): simple pole") */
113 static ex tgamma_eval(const ex & x)
115 if (x.info(info_flags::numeric)) {
116 // trap integer arguments:
117 if (x.info(info_flags::integer)) {
118 // tgamma(n) -> (n-1)! for postitive n
119 if (x.info(info_flags::posint)) {
120 return factorial(ex_to_numeric(x).sub(_num1()));
122 throw (std::domain_error("tgamma_eval(): simple pole"));
125 // trap half integer arguments:
126 if ((x*2).info(info_flags::integer)) {
127 // trap positive x==(n+1/2)
128 // tgamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
129 if ((x*_ex2()).info(info_flags::posint)) {
130 numeric n = ex_to_numeric(x).sub(_num1_2());
131 numeric coefficient = doublefactorial(n.mul(_num2()).sub(_num1()));
132 coefficient = coefficient.div(pow(_num2(),n));
133 return coefficient * pow(Pi,_ex1_2());
135 // trap negative x==(-n+1/2)
136 // tgamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
137 numeric n = abs(ex_to_numeric(x).sub(_num1_2()));
138 numeric coefficient = pow(_num_2(), n);
139 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
140 return coefficient*power(Pi,_ex1_2());
143 // tgamma_evalf should be called here once it becomes available
146 return tgamma(x).hold();
150 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
152 GINAC_ASSERT(deriv_param==0);
154 // d/dx tgamma(x) -> psi(x)*tgamma(x)
155 return psi(x)*tgamma(x);
159 static ex tgamma_series(const ex & x, const relational & r, int order)
162 // Taylor series where there is no pole falls back to psi function
164 // On a pole at -m use the recurrence relation
165 // tgamma(x) == tgamma(x+1) / x
166 // from which follows
167 // series(tgamma(x),x,-m,order) ==
168 // series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x,-m,order+1);
169 const ex x_pt = x.subs(r);
170 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
171 throw do_taylor(); // caught by function::series()
172 // if we got here we have to care for a simple pole at -m:
173 numeric m = -ex_to_numeric(x_pt);
174 ex ser_denom = _ex1();
175 for (numeric p; p<=m; ++p)
177 return (tgamma(x+m+_ex1())/ser_denom).series(r, order+1);
181 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
182 evalf_func(tgamma_evalf).
183 derivative_func(tgamma_deriv).
184 series_func(tgamma_series));
191 static ex beta_evalf(const ex & x, const ex & y)
196 END_TYPECHECK(beta(x,y))
198 return tgamma(ex_to_numeric(x))*tgamma(ex_to_numeric(y))/tgamma(ex_to_numeric(x+y));
202 static ex beta_eval(const ex & x, const ex & y)
204 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
205 // treat all problematic x and y that may not be passed into tgamma,
206 // because they would throw there although beta(x,y) is well-defined
207 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
208 numeric nx(ex_to_numeric(x));
209 numeric ny(ex_to_numeric(y));
210 if (nx.is_real() && nx.is_integer() &&
211 ny.is_real() && ny.is_integer()) {
212 if (nx.is_negative()) {
214 return pow(_num_1(), ny)*beta(1-x-y, y);
216 throw (std::domain_error("beta_eval(): simple pole"));
218 if (ny.is_negative()) {
220 return pow(_num_1(), nx)*beta(1-y-x, x);
222 throw (std::domain_error("beta_eval(): simple pole"));
224 return tgamma(x)*tgamma(y)/tgamma(x+y);
226 // no problem in numerator, but denominator has pole:
227 if ((nx+ny).is_real() &&
228 (nx+ny).is_integer() &&
229 !(nx+ny).is_positive())
232 return tgamma(x)*tgamma(y)/tgamma(x+y);
235 return beta(x,y).hold();
239 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
241 GINAC_ASSERT(deriv_param<2);
244 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
246 retval = (psi(x)-psi(x+y))*beta(x,y);
247 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
249 retval = (psi(y)-psi(x+y))*beta(x,y);
254 static ex beta_series(const ex & x, const ex & y, const relational & r, int order)
257 // Taylor series where there is no pole of one of the tgamma functions
258 // falls back to beta function evaluation. Otherwise, fall back to
259 // tgamma series directly.
260 // FIXME: this could need some testing, maybe it's wrong in some cases?
261 const ex x_pt = x.subs(r);
262 const ex y_pt = y.subs(r);
263 GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
264 const symbol *s = static_cast<symbol *>(r.lhs().bp);
265 ex x_ser, y_ser, xy_ser;
266 if ((!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive)) &&
267 (!y_pt.info(info_flags::integer) || y_pt.info(info_flags::positive)))
268 throw do_taylor(); // caught by function::series()
269 // trap the case where x is on a pole directly:
270 if (x.info(info_flags::integer) && !x.info(info_flags::positive))
271 x_ser = tgamma(x+*s).series(r,order);
273 x_ser = tgamma(x).series(r,order);
274 // trap the case where y is on a pole directly:
275 if (y.info(info_flags::integer) && !y.info(info_flags::positive))
276 y_ser = tgamma(y+*s).series(r,order);
278 y_ser = tgamma(y).series(r,order);
279 // trap the case where y is on a pole directly:
280 if ((x+y).info(info_flags::integer) && !(x+y).info(info_flags::positive))
281 xy_ser = tgamma(y+x+*s).series(r,order);
283 xy_ser = tgamma(y+x).series(r,order);
284 // compose the result:
285 return (x_ser*y_ser/xy_ser).series(r,order);
289 REGISTER_FUNCTION(beta, eval_func(beta_eval).
290 evalf_func(beta_evalf).
291 derivative_func(beta_deriv).
292 series_func(beta_series));
296 // Psi-function (aka digamma-function)
299 static ex psi1_evalf(const ex & x)
303 END_TYPECHECK(psi(x))
305 return psi(ex_to_numeric(x));
308 /** Evaluation of digamma-function psi(x).
309 * Somebody ought to provide some good numerical evaluation some day... */
310 static ex psi1_eval(const ex & x)
312 if (x.info(info_flags::numeric)) {
313 numeric nx = ex_to_numeric(x);
314 if (nx.is_integer()) {
316 if (nx.is_positive()) {
317 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - gamma
319 for (numeric i(nx+_num_1()); i.is_positive(); --i)
323 // for non-positive integers there is a pole:
324 throw (std::domain_error("psi_eval(): simple pole"));
327 if ((_num2()*nx).is_integer()) {
329 if (nx.is_positive()) {
330 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - gamma - 2log(2)
332 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
333 rat += _num2()*i.inverse();
334 return rat-gamma-_ex2()*log(_ex2());
336 // use the recurrence relation
337 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
338 // to relate psi(-m-1/2) to psi(1/2):
339 // psi(-m-1/2) == psi(1/2) + r
340 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
342 for (numeric p(nx); p<0; ++p)
343 recur -= pow(p, _num_1());
344 return recur+psi(_ex1_2());
347 // psi1_evalf should be called here once it becomes available
350 return psi(x).hold();
353 static ex psi1_deriv(const ex & x, unsigned deriv_param)
355 GINAC_ASSERT(deriv_param==0);
357 // d/dx psi(x) -> psi(1,x)
358 return psi(_ex1(), x);
361 static ex psi1_series(const ex & x, const relational & r, int order)
364 // Taylor series where there is no pole falls back to polygamma function
366 // On a pole at -m use the recurrence relation
367 // psi(x) == psi(x+1) - 1/z
368 // from which follows
369 // series(psi(x),x,-m,order) ==
370 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
371 const ex x_pt = x.subs(r);
372 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
373 throw do_taylor(); // caught by function::series()
374 // if we got here we have to care for a simple pole at -m:
375 numeric m = -ex_to_numeric(x_pt);
377 for (numeric p; p<=m; ++p)
378 recur += power(x+p,_ex_1());
379 return (psi(x+m+_ex1())-recur).series(r, order);
382 const unsigned function_index_psi1 =
383 function::register_new(function_options("psi").
384 eval_func(psi1_eval).
385 evalf_func(psi1_evalf).
386 derivative_func(psi1_deriv).
387 series_func(psi1_series).
391 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
394 static ex psi2_evalf(const ex & n, const ex & x)
399 END_TYPECHECK(psi(n,x))
401 return psi(ex_to_numeric(n), ex_to_numeric(x));
404 /** Evaluation of polygamma-function psi(n,x).
405 * Somebody ought to provide some good numerical evaluation some day... */
406 static ex psi2_eval(const ex & n, const ex & x)
408 // psi(0,x) -> psi(x)
411 // psi(-1,x) -> log(tgamma(x))
412 if (n.is_equal(_ex_1()))
413 return log(tgamma(x));
414 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
415 x.info(info_flags::numeric)) {
416 numeric nn = ex_to_numeric(n);
417 numeric nx = ex_to_numeric(x);
418 if (nx.is_integer()) {
420 if (nx.is_equal(_num1()))
421 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
422 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
423 if (nx.is_positive()) {
424 // use the recurrence relation
425 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
426 // to relate psi(n,m) to psi(n,1):
427 // psi(n,m) == psi(n,1) + r
428 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
430 for (numeric p(1); p<nx; ++p)
431 recur += pow(p, -nn+_num_1());
432 recur *= factorial(nn)*pow(_num_1(), nn);
433 return recur+psi(n,_ex1());
435 // for non-positive integers there is a pole:
436 throw (std::domain_error("psi2_eval(): pole"));
439 if ((_num2()*nx).is_integer()) {
441 if (nx.is_equal(_num1_2()))
442 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
443 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
444 if (nx.is_positive()) {
445 numeric m = nx - _num1_2();
446 // use the multiplication formula
447 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
448 // to revert to positive integer case
449 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
451 // use the recurrence relation
452 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
453 // to relate psi(n,-m-1/2) to psi(n,1/2):
454 // psi(n,-m-1/2) == psi(n,1/2) + r
455 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
457 for (numeric p(nx); p<0; ++p)
458 recur += pow(p, -nn+_num_1());
459 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
460 return recur+psi(n,_ex1_2());
463 // psi2_evalf should be called here once it becomes available
466 return psi(n, x).hold();
469 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
471 GINAC_ASSERT(deriv_param<2);
473 if (deriv_param==0) {
475 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
477 // d/dx psi(n,x) -> psi(n+1,x)
478 return psi(n+_ex1(), x);
481 static ex psi2_series(const ex & n, const ex & x, const relational & r, int order)
484 // Taylor series where there is no pole falls back to polygamma function
486 // On a pole at -m use the recurrence relation
487 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
488 // from which follows
489 // series(psi(x),x,-m,order) ==
490 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
491 // ... + (x+m)^(-n-1))),x,-m,order);
492 const ex x_pt = x.subs(r);
493 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
494 throw do_taylor(); // caught by function::series()
495 // if we got here we have to care for a pole of order n+1 at -m:
496 numeric m = -ex_to_numeric(x_pt);
498 for (numeric p; p<=m; ++p)
499 recur += power(x+p,-n+_ex_1());
500 recur *= factorial(n)*power(_ex_1(),n);
501 return (psi(n, x+m+_ex1())-recur).series(r, order);
504 const unsigned function_index_psi2 =
505 function::register_new(function_options("psi").
506 eval_func(psi2_eval).
507 evalf_func(psi2_evalf).
508 derivative_func(psi2_deriv).
509 series_func(psi2_series).
513 #ifndef NO_NAMESPACE_GINAC
515 #endif // ndef NO_NAMESPACE_GINAC