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
45 static ex gamma_evalf(const ex & x)
49 END_TYPECHECK(gamma(x))
51 return gamma(ex_to_numeric(x));
55 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
56 * arguments and that's it. Somebody ought to provide some good numerical
57 * evaluation some day...
59 * @exception std::domain_error("gamma_eval(): simple pole") */
60 static ex gamma_eval(const ex & x)
62 if (x.info(info_flags::numeric)) {
63 // trap integer arguments:
64 if (x.info(info_flags::integer)) {
65 // gamma(n+1) -> n! for postitive n
66 if (x.info(info_flags::posint)) {
67 return factorial(ex_to_numeric(x).sub(_num1()));
69 throw (std::domain_error("gamma_eval(): simple pole"));
72 // trap half integer arguments:
73 if ((x*2).info(info_flags::integer)) {
74 // trap positive x==(n+1/2)
75 // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
76 if ((x*_ex2()).info(info_flags::posint)) {
77 numeric n = ex_to_numeric(x).sub(_num1_2());
78 numeric coefficient = doublefactorial(n.mul(_num2()).sub(_num1()));
79 coefficient = coefficient.div(pow(_num2(),n));
80 return coefficient * pow(Pi,_ex1_2());
82 // trap negative x==(-n+1/2)
83 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
84 numeric n = abs(ex_to_numeric(x).sub(_num1_2()));
85 numeric coefficient = pow(_num_2(), n);
86 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
87 return coefficient*power(Pi,_ex1_2());
90 // gamma_evalf should be called here once it becomes available
93 return gamma(x).hold();
97 static ex gamma_deriv(const ex & x, unsigned deriv_param)
99 GINAC_ASSERT(deriv_param==0);
101 // d/dx log(gamma(x)) -> psi(x)
102 // d/dx gamma(x) -> psi(x)*gamma(x)
103 return psi(x)*gamma(x);
107 static ex gamma_series(const ex & x, const symbol & s, const ex & pt, int order)
110 // Taylor series where there is no pole falls back to psi function
112 // On a pole at -m use the recurrence relation
113 // gamma(x) == gamma(x+1) / x
114 // from which follows
115 // series(gamma(x),x,-m,order) ==
116 // series(gamma(x+m+1)/(x*(x+1)*...*(x+m)),x,-m,order+1);
117 const ex x_pt = x.subs(s==pt);
118 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
119 throw do_taylor(); // caught by function::series()
120 // if we got here we have to care for a simple pole at -m:
121 numeric m = -ex_to_numeric(x_pt);
122 ex ser_denom = _ex1();
123 for (numeric p; p<=m; ++p)
125 return (gamma(x+m+_ex1())/ser_denom).series(s, pt, order+1);
129 REGISTER_FUNCTION(gamma, eval_func(gamma_eval).
130 evalf_func(gamma_evalf).
131 derivative_func(gamma_deriv).
132 series_func(gamma_series));
139 static ex beta_evalf(const ex & x, const ex & y)
144 END_TYPECHECK(beta(x,y))
146 return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))/gamma(ex_to_numeric(x+y));
150 static ex beta_eval(const ex & x, const ex & y)
152 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
153 // treat all problematic x and y that may not be passed into gamma,
154 // because they would throw there although beta(x,y) is well-defined
155 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
156 numeric nx(ex_to_numeric(x));
157 numeric ny(ex_to_numeric(y));
158 if (nx.is_real() && nx.is_integer() &&
159 ny.is_real() && ny.is_integer()) {
160 if (nx.is_negative()) {
162 return pow(_num_1(), ny)*beta(1-x-y, y);
164 throw (std::domain_error("beta_eval(): simple pole"));
166 if (ny.is_negative()) {
168 return pow(_num_1(), nx)*beta(1-y-x, x);
170 throw (std::domain_error("beta_eval(): simple pole"));
172 return gamma(x)*gamma(y)/gamma(x+y);
174 // no problem in numerator, but denominator has pole:
175 if ((nx+ny).is_real() &&
176 (nx+ny).is_integer() &&
177 !(nx+ny).is_positive())
180 return gamma(x)*gamma(y)/gamma(x+y);
183 return beta(x,y).hold();
187 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
189 GINAC_ASSERT(deriv_param<2);
192 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
194 retval = (psi(x)-psi(x+y))*beta(x,y);
195 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
197 retval = (psi(y)-psi(x+y))*beta(x,y);
202 static ex beta_series(const ex & x, const ex & y, const symbol & s, const ex & pt, int order)
205 // Taylor series where there is no pole of one of the gamma functions
206 // falls back to beta function evaluation. Otherwise, fall back to
207 // gamma series directly.
208 // FIXME: this could need some testing, maybe it's wrong in some cases?
209 const ex x_pt = x.subs(s==pt);
210 const ex y_pt = y.subs(s==pt);
211 ex x_ser, y_ser, xy_ser;
212 if ((!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive)) &&
213 (!y_pt.info(info_flags::integer) || y_pt.info(info_flags::positive)))
214 throw do_taylor(); // caught by function::series()
215 // trap the case where x is on a pole directly:
216 if (x.info(info_flags::integer) && !x.info(info_flags::positive))
217 x_ser = gamma(x+s).series(s,pt,order);
219 x_ser = gamma(x).series(s,pt,order);
220 // trap the case where y is on a pole directly:
221 if (y.info(info_flags::integer) && !y.info(info_flags::positive))
222 y_ser = gamma(y+s).series(s,pt,order);
224 y_ser = gamma(y).series(s,pt,order);
225 // trap the case where y is on a pole directly:
226 if ((x+y).info(info_flags::integer) && !(x+y).info(info_flags::positive))
227 xy_ser = gamma(y+x+s).series(s,pt,order);
229 xy_ser = gamma(y+x).series(s,pt,order);
230 // compose the result:
231 return (x_ser*y_ser/xy_ser).series(s,pt,order);
235 REGISTER_FUNCTION(beta, eval_func(beta_eval).
236 evalf_func(beta_evalf).
237 derivative_func(beta_deriv).
238 series_func(beta_series));
242 // Psi-function (aka digamma-function)
245 static ex psi1_evalf(const ex & x)
249 END_TYPECHECK(psi(x))
251 return psi(ex_to_numeric(x));
254 /** Evaluation of digamma-function psi(x).
255 * Somebody ought to provide some good numerical evaluation some day... */
256 static ex psi1_eval(const ex & x)
258 if (x.info(info_flags::numeric)) {
259 numeric nx = ex_to_numeric(x);
260 if (nx.is_integer()) {
262 if (nx.is_positive()) {
263 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
265 for (numeric i(nx+_num_1()); i.is_positive(); --i)
267 return rat-EulerGamma;
269 // for non-positive integers there is a pole:
270 throw (std::domain_error("psi_eval(): simple pole"));
273 if ((_num2()*nx).is_integer()) {
275 if (nx.is_positive()) {
276 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
278 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
279 rat += _num2()*i.inverse();
280 return rat-EulerGamma-_ex2()*log(_ex2());
282 // use the recurrence relation
283 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
284 // to relate psi(-m-1/2) to psi(1/2):
285 // psi(-m-1/2) == psi(1/2) + r
286 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
288 for (numeric p(nx); p<0; ++p)
289 recur -= pow(p, _num_1());
290 return recur+psi(_ex1_2());
293 // psi1_evalf should be called here once it becomes available
296 return psi(x).hold();
299 static ex psi1_deriv(const ex & x, unsigned deriv_param)
301 GINAC_ASSERT(deriv_param==0);
303 // d/dx psi(x) -> psi(1,x)
304 return psi(_ex1(), x);
307 static ex psi1_series(const ex & x, const symbol & s, const ex & pt, int order)
310 // Taylor series where there is no pole falls back to polygamma function
312 // On a pole at -m use the recurrence relation
313 // psi(x) == psi(x+1) - 1/z
314 // from which follows
315 // series(psi(x),x,-m,order) ==
316 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
317 const ex x_pt = x.subs(s==pt);
318 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
319 throw do_taylor(); // caught by function::series()
320 // if we got here we have to care for a simple pole at -m:
321 numeric m = -ex_to_numeric(x_pt);
323 for (numeric p; p<=m; ++p)
324 recur += power(x+p,_ex_1());
325 return (psi(x+m+_ex1())-recur).series(s, pt, order);
328 const unsigned function_index_psi1 =
329 function::register_new(function_options("psi").
330 eval_func(psi1_eval).
331 evalf_func(psi1_evalf).
332 derivative_func(psi1_deriv).
333 series_func(psi1_series).
337 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
340 static ex psi2_evalf(const ex & n, const ex & x)
345 END_TYPECHECK(psi(n,x))
347 return psi(ex_to_numeric(n), ex_to_numeric(x));
350 /** Evaluation of polygamma-function psi(n,x).
351 * Somebody ought to provide some good numerical evaluation some day... */
352 static ex psi2_eval(const ex & n, const ex & x)
354 // psi(0,x) -> psi(x)
357 // psi(-1,x) -> log(gamma(x))
358 if (n.is_equal(_ex_1()))
359 return log(gamma(x));
360 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
361 x.info(info_flags::numeric)) {
362 numeric nn = ex_to_numeric(n);
363 numeric nx = ex_to_numeric(x);
364 if (nx.is_integer()) {
366 if (nx.is_equal(_num1()))
367 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
368 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
369 if (nx.is_positive()) {
370 // use the recurrence relation
371 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
372 // to relate psi(n,m) to psi(n,1):
373 // psi(n,m) == psi(n,1) + r
374 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
376 for (numeric p(1); p<nx; ++p)
377 recur += pow(p, -nn+_num_1());
378 recur *= factorial(nn)*pow(_num_1(), nn);
379 return recur+psi(n,_ex1());
381 // for non-positive integers there is a pole:
382 throw (std::domain_error("psi2_eval(): pole"));
385 if ((_num2()*nx).is_integer()) {
387 if (nx.is_equal(_num1_2()))
388 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
389 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
390 if (nx.is_positive()) {
391 numeric m = nx - _num1_2();
392 // use the multiplication formula
393 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
394 // to revert to positive integer case
395 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
397 // use the recurrence relation
398 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
399 // to relate psi(n,-m-1/2) to psi(n,1/2):
400 // psi(n,-m-1/2) == psi(n,1/2) + r
401 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
403 for (numeric p(nx); p<0; ++p)
404 recur += pow(p, -nn+_num_1());
405 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
406 return recur+psi(n,_ex1_2());
409 // psi2_evalf should be called here once it becomes available
412 return psi(n, x).hold();
415 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
417 GINAC_ASSERT(deriv_param<2);
419 if (deriv_param==0) {
421 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
423 // d/dx psi(n,x) -> psi(n+1,x)
424 return psi(n+_ex1(), x);
427 static ex psi2_series(const ex & n, const ex & x, const symbol & s, const ex & pt, int order)
430 // Taylor series where there is no pole falls back to polygamma function
432 // On a pole at -m use the recurrence relation
433 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
434 // from which follows
435 // series(psi(x),x,-m,order) ==
436 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
437 // ... + (x+m)^(-n-1))),x,-m,order);
438 const ex x_pt = x.subs(s==pt);
439 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
440 throw do_taylor(); // caught by function::series()
441 // if we got here we have to care for a pole of order n+1 at -m:
442 numeric m = -ex_to_numeric(x_pt);
444 for (numeric p; p<=m; ++p)
445 recur += power(x+p,-n+_ex_1());
446 recur *= factorial(n)*power(_ex_1(),n);
447 return (psi(n, x+m+_ex1())-recur).series(s, pt, order);
450 const unsigned function_index_psi2 =
451 function::register_new(function_options("psi").
452 eval_func(psi2_eval).
453 evalf_func(psi2_evalf).
454 derivative_func(psi2_deriv).
455 series_func(psi2_series).
459 #ifndef NO_NAMESPACE_GINAC
461 #endif // ndef NO_NAMESPACE_GINAC