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 relational & r, 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(r);
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(r, 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 relational & r, 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(r);
210 const ex y_pt = y.subs(r);
211 GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
212 const symbol *s = static_cast<symbol *>(r.lhs().bp);
213 ex x_ser, y_ser, xy_ser;
214 if ((!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive)) &&
215 (!y_pt.info(info_flags::integer) || y_pt.info(info_flags::positive)))
216 throw do_taylor(); // caught by function::series()
217 // trap the case where x is on a pole directly:
218 if (x.info(info_flags::integer) && !x.info(info_flags::positive))
219 x_ser = Gamma(x+*s).series(r,order);
221 x_ser = Gamma(x).series(r,order);
222 // trap the case where y is on a pole directly:
223 if (y.info(info_flags::integer) && !y.info(info_flags::positive))
224 y_ser = Gamma(y+*s).series(r,order);
226 y_ser = Gamma(y).series(r,order);
227 // trap the case where y is on a pole directly:
228 if ((x+y).info(info_flags::integer) && !(x+y).info(info_flags::positive))
229 xy_ser = Gamma(y+x+*s).series(r,order);
231 xy_ser = Gamma(y+x).series(r,order);
232 // compose the result:
233 return (x_ser*y_ser/xy_ser).series(r,order);
237 REGISTER_FUNCTION(Beta, eval_func(Beta_eval).
238 evalf_func(Beta_evalf).
239 derivative_func(Beta_deriv).
240 series_func(Beta_series));
244 // Psi-function (aka digamma-function)
247 static ex psi1_evalf(const ex & x)
251 END_TYPECHECK(psi(x))
253 return psi(ex_to_numeric(x));
256 /** Evaluation of digamma-function psi(x).
257 * Somebody ought to provide some good numerical evaluation some day... */
258 static ex psi1_eval(const ex & x)
260 if (x.info(info_flags::numeric)) {
261 numeric nx = ex_to_numeric(x);
262 if (nx.is_integer()) {
264 if (nx.is_positive()) {
265 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - gamma
267 for (numeric i(nx+_num_1()); i.is_positive(); --i)
271 // for non-positive integers there is a pole:
272 throw (std::domain_error("psi_eval(): simple pole"));
275 if ((_num2()*nx).is_integer()) {
277 if (nx.is_positive()) {
278 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - gamma - 2log(2)
280 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
281 rat += _num2()*i.inverse();
282 return rat-gamma-_ex2()*log(_ex2());
284 // use the recurrence relation
285 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
286 // to relate psi(-m-1/2) to psi(1/2):
287 // psi(-m-1/2) == psi(1/2) + r
288 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
290 for (numeric p(nx); p<0; ++p)
291 recur -= pow(p, _num_1());
292 return recur+psi(_ex1_2());
295 // psi1_evalf should be called here once it becomes available
298 return psi(x).hold();
301 static ex psi1_deriv(const ex & x, unsigned deriv_param)
303 GINAC_ASSERT(deriv_param==0);
305 // d/dx psi(x) -> psi(1,x)
306 return psi(_ex1(), x);
309 static ex psi1_series(const ex & x, const relational & r, int order)
312 // Taylor series where there is no pole falls back to polygamma function
314 // On a pole at -m use the recurrence relation
315 // psi(x) == psi(x+1) - 1/z
316 // from which follows
317 // series(psi(x),x,-m,order) ==
318 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
319 const ex x_pt = x.subs(r);
320 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
321 throw do_taylor(); // caught by function::series()
322 // if we got here we have to care for a simple pole at -m:
323 numeric m = -ex_to_numeric(x_pt);
325 for (numeric p; p<=m; ++p)
326 recur += power(x+p,_ex_1());
327 return (psi(x+m+_ex1())-recur).series(r, order);
330 const unsigned function_index_psi1 =
331 function::register_new(function_options("psi").
332 eval_func(psi1_eval).
333 evalf_func(psi1_evalf).
334 derivative_func(psi1_deriv).
335 series_func(psi1_series).
339 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
342 static ex psi2_evalf(const ex & n, const ex & x)
347 END_TYPECHECK(psi(n,x))
349 return psi(ex_to_numeric(n), ex_to_numeric(x));
352 /** Evaluation of polygamma-function psi(n,x).
353 * Somebody ought to provide some good numerical evaluation some day... */
354 static ex psi2_eval(const ex & n, const ex & x)
356 // psi(0,x) -> psi(x)
359 // psi(-1,x) -> log(Gamma(x))
360 if (n.is_equal(_ex_1()))
361 return log(Gamma(x));
362 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
363 x.info(info_flags::numeric)) {
364 numeric nn = ex_to_numeric(n);
365 numeric nx = ex_to_numeric(x);
366 if (nx.is_integer()) {
368 if (nx.is_equal(_num1()))
369 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
370 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
371 if (nx.is_positive()) {
372 // use the recurrence relation
373 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
374 // to relate psi(n,m) to psi(n,1):
375 // psi(n,m) == psi(n,1) + r
376 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
378 for (numeric p(1); p<nx; ++p)
379 recur += pow(p, -nn+_num_1());
380 recur *= factorial(nn)*pow(_num_1(), nn);
381 return recur+psi(n,_ex1());
383 // for non-positive integers there is a pole:
384 throw (std::domain_error("psi2_eval(): pole"));
387 if ((_num2()*nx).is_integer()) {
389 if (nx.is_equal(_num1_2()))
390 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
391 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
392 if (nx.is_positive()) {
393 numeric m = nx - _num1_2();
394 // use the multiplication formula
395 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
396 // to revert to positive integer case
397 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
399 // use the recurrence relation
400 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
401 // to relate psi(n,-m-1/2) to psi(n,1/2):
402 // psi(n,-m-1/2) == psi(n,1/2) + r
403 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
405 for (numeric p(nx); p<0; ++p)
406 recur += pow(p, -nn+_num_1());
407 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
408 return recur+psi(n,_ex1_2());
411 // psi2_evalf should be called here once it becomes available
414 return psi(n, x).hold();
417 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
419 GINAC_ASSERT(deriv_param<2);
421 if (deriv_param==0) {
423 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
425 // d/dx psi(n,x) -> psi(n+1,x)
426 return psi(n+_ex1(), x);
429 static ex psi2_series(const ex & n, const ex & x, const relational & r, int order)
432 // Taylor series where there is no pole falls back to polygamma function
434 // On a pole at -m use the recurrence relation
435 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
436 // from which follows
437 // series(psi(x),x,-m,order) ==
438 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
439 // ... + (x+m)^(-n-1))),x,-m,order);
440 const ex x_pt = x.subs(r);
441 if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
442 throw do_taylor(); // caught by function::series()
443 // if we got here we have to care for a pole of order n+1 at -m:
444 numeric m = -ex_to_numeric(x_pt);
446 for (numeric p; p<=m; ++p)
447 recur += power(x+p,-n+_ex_1());
448 recur *= factorial(n)*power(_ex_1(),n);
449 return (psi(n, x+m+_ex1())-recur).series(r, order);
452 const unsigned function_index_psi2 =
453 function::register_new(function_options("psi").
454 eval_func(psi2_eval).
455 evalf_func(psi2_evalf).
456 derivative_func(psi2_deriv).
457 series_func(psi2_series).
461 #ifndef NO_NAMESPACE_GINAC
463 #endif // ndef NO_NAMESPACE_GINAC