1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma-function, Beta-function, Polygamma-functions, and
4 * some related stuff. */
7 * GiNaC Copyright (C) 1999 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_GINAC_NAMESPACE
39 #endif // ndef NO_GINAC_NAMESPACE
45 static ex gamma_evalf(const ex & x)
49 END_TYPECHECK(gamma(x))
51 return gamma(ex_to_numeric(x));
54 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
55 * arguments and that's it. Somebody ought to provide some good numerical
56 * evaluation some day...
58 * @exception std::domain_error("gamma_eval(): simple pole") */
59 static ex gamma_eval(const ex & x)
61 if (x.info(info_flags::numeric)) {
62 // trap integer arguments:
63 if (x.info(info_flags::integer)) {
64 // gamma(n+1) -> n! for postitive n
65 if (x.info(info_flags::posint)) {
66 return factorial(ex_to_numeric(x).sub(_num1()));
68 throw (std::domain_error("gamma_eval(): simple pole"));
71 // trap half integer arguments:
72 if ((x*2).info(info_flags::integer)) {
73 // trap positive x==(n+1/2)
74 // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
75 if ((x*_ex2()).info(info_flags::posint)) {
76 numeric n = ex_to_numeric(x).sub(_num1_2());
77 numeric coefficient = doublefactorial(n.mul(_num2()).sub(_num1()));
78 coefficient = coefficient.div(pow(_num2(),n));
79 return coefficient * pow(Pi,_ex1_2());
81 // trap negative x==(-n+1/2)
82 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
83 numeric n = abs(ex_to_numeric(x).sub(_num1_2()));
84 numeric coefficient = pow(_num_2(), n);
85 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
86 return coefficient*power(Pi,_ex1_2());
89 // gamma_evalf should be called here once it becomes available
92 return gamma(x).hold();
95 static ex gamma_diff(const ex & x, unsigned diff_param)
97 GINAC_ASSERT(diff_param==0);
99 // d/dx log(gamma(x)) -> psi(x)
100 // d/dx gamma(x) -> psi(x)*gamma(x)
101 return psi(x)*gamma(x);
104 static ex gamma_series(const ex & x, const symbol & s, const ex & point, int order)
107 // Taylor series where there is no pole falls back to psi function
109 // On a pole at -m use the recurrence relation
110 // gamma(x) == gamma(x+1) / x
111 // from which follows
112 // series(gamma(x),x,-m,order) ==
113 // series(gamma(x+m+1)/(x*(x+1)...*(x+m)),x,-m,order+1);
114 ex xpoint = x.subs(s==point);
115 if (!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive))
116 throw do_taylor(); // caught by function::series()
117 // if we got here we have to care for a simple pole at -m:
118 numeric m = -ex_to_numeric(xpoint);
119 ex ser_numer = gamma(x+m+_ex1());
120 ex ser_denom = _ex1();
121 for (numeric p; p<=m; ++p)
123 return (ser_numer/ser_denom).series(s, point, order+1);
126 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
132 static ex beta_evalf(const ex & x, const ex & y)
137 END_TYPECHECK(beta(x,y))
139 return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
140 / gamma(ex_to_numeric(x+y));
143 static ex beta_eval(const ex & x, const ex & y)
145 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
146 numeric nx(ex_to_numeric(x));
147 numeric ny(ex_to_numeric(y));
148 // treat all problematic x and y that may not be passed into gamma,
149 // because they would throw there although beta(x,y) is well-defined
150 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
151 if (nx.is_real() && nx.is_integer() &&
152 ny.is_real() && ny.is_integer()) {
153 if (nx.is_negative()) {
155 return pow(_num_1(), ny)*beta(1-x-y, y);
157 throw (std::domain_error("beta_eval(): simple pole"));
159 if (ny.is_negative()) {
161 return pow(_num_1(), nx)*beta(1-y-x, x);
163 throw (std::domain_error("beta_eval(): simple pole"));
165 return gamma(x)*gamma(y)/gamma(x+y);
167 // no problem in numerator, but denominator has pole:
168 if ((nx+ny).is_real() &&
169 (nx+ny).is_integer() &&
170 !(nx+ny).is_positive())
173 return gamma(x)*gamma(y)/gamma(x+y);
176 return beta(x,y).hold();
179 static ex beta_diff(const ex & x, const ex & y, unsigned diff_param)
181 GINAC_ASSERT(diff_param<2);
184 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
186 retval = (psi(x)-psi(x+y))*beta(x,y);
187 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
189 retval = (psi(y)-psi(x+y))*beta(x,y);
193 static ex beta_series(const ex & x, const ex & y, const symbol & s, const ex & point, int order)
196 // Taylor series where there is no pole falls back to beta function
198 // On a pole at -m use the recurrence relation
199 // gamma(x) == gamma(x+1) / x
200 // from which follows
201 // series(gamma(x),x,-m,order) ==
202 // series(gamma(x+m+1)/(x*(x+1)...*(x+m)),x,-m,order+1);
203 ex xpoint = x.subs(s==point);
204 ex ypoint = y.subs(s==point);
205 if ((!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive)) &&
206 (!ypoint.info(info_flags::integer) || ypoint.info(info_flags::positive)))
207 throw do_taylor(); // caught by function::series()
208 // if we got here we have to care for a simple pole at -m:
209 throw (std::domain_error("beta_series(): Mama, please code me!"));
212 REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, beta_series);
215 // Psi-function (aka digamma-function)
218 static ex psi1_evalf(const ex & x)
222 END_TYPECHECK(psi(x))
224 return psi(ex_to_numeric(x));
227 /** Evaluation of digamma-function psi(x).
228 * Somebody ought to provide some good numerical evaluation some day... */
229 static ex psi1_eval(const ex & x)
231 if (x.info(info_flags::numeric)) {
232 numeric nx = ex_to_numeric(x);
233 if (nx.is_integer()) {
235 if (nx.is_positive()) {
236 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
238 for (numeric i(nx+_num_1()); i.is_positive(); --i)
240 return rat-EulerGamma;
242 // for non-positive integers there is a pole:
243 throw (std::domain_error("psi_eval(): simple pole"));
246 if ((_num2()*nx).is_integer()) {
248 if (nx.is_positive()) {
249 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
251 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
252 rat += _num2()*i.inverse();
253 return rat-EulerGamma-_ex2()*log(_ex2());
255 // use the recurrence relation
256 // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
257 // to relate psi(-m-1/2) to psi(1/2):
258 // psi(-m-1/2) == psi(1/2) + r
259 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
261 for (numeric p(nx); p<0; ++p)
262 recur -= pow(p, _num_1());
263 return recur+psi(_ex1_2());
266 // psi1_evalf should be called here once it becomes available
269 return psi(x).hold();
272 static ex psi1_diff(const ex & x, unsigned diff_param)
274 GINAC_ASSERT(diff_param==0);
276 // d/dx psi(x) -> psi(1,x)
277 return psi(_ex1(), x);
280 static ex psi1_series(const ex & x, const symbol & s, const ex & point, int order)
283 // Taylor series where there is no pole falls back to polygamma function
285 // On a pole at -m use the recurrence relation
286 // psi(x) == psi(x+1) - 1/z
287 // from which follows
288 // series(psi(x),x,-m,order) ==
289 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
290 ex xpoint = x.subs(s==point);
291 if (!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive))
292 throw do_taylor(); // caught by function::series()
293 // if we got here we have to care for a simple pole at -m:
294 numeric m = -ex_to_numeric(xpoint);
296 for (numeric p; p<=m; ++p)
297 recur += power(x+p,_ex_1());
298 return (psi(x+m+_ex1())-recur).series(s, point, order);
301 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, psi1_series);
304 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
307 static ex psi2_evalf(const ex & n, const ex & x)
312 END_TYPECHECK(psi(n,x))
314 return psi(ex_to_numeric(n), ex_to_numeric(x));
317 /** Evaluation of polygamma-function psi(n,x).
318 * Somebody ought to provide some good numerical evaluation some day... */
319 static ex psi2_eval(const ex & n, const ex & x)
321 // psi(0,x) -> psi(x)
324 // psi(-1,x) -> log(gamma(x))
325 if (n.is_equal(_ex_1()))
326 return log(gamma(x));
327 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
328 x.info(info_flags::numeric)) {
329 numeric nn = ex_to_numeric(n);
330 numeric nx = ex_to_numeric(x);
331 if (nx.is_integer()) {
333 if (nx.is_equal(_num1()))
334 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
335 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
336 if (nx.is_positive()) {
337 // use the recurrence relation
338 // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
339 // to relate psi(n,m) to psi(n,1):
340 // psi(n,m) == psi(n,1) + r
341 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
343 for (numeric p(1); p<nx; ++p)
344 recur += pow(p, -nn+_num_1());
345 recur *= factorial(nn)*pow(_num_1(), nn);
346 return recur+psi(n,_ex1());
348 // for non-positive integers there is a pole:
349 throw (std::domain_error("psi2_eval(): pole"));
352 if ((_num2()*nx).is_integer()) {
354 if (nx.is_equal(_num1_2()))
355 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
356 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
357 if (nx.is_positive()) {
358 numeric m = nx - _num1_2();
359 // use the multiplication formula
360 // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
361 // to revert to positive integer case
362 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
364 // use the recurrence relation
365 // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
366 // to relate psi(n,-m-1/2) to psi(n,1/2):
367 // psi(n,-m-1/2) == psi(n,1/2) + r
368 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
370 for (numeric p(nx); p<0; ++p)
371 recur += pow(p, -nn+_num_1());
372 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
373 return recur+psi(n,_ex1_2());
376 // psi2_evalf should be called here once it becomes available
379 return psi(n, x).hold();
382 static ex psi2_diff(const ex & n, const ex & x, unsigned diff_param)
384 GINAC_ASSERT(diff_param<2);
388 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
390 // d/dx psi(n,x) -> psi(n+1,x)
391 return psi(n+_ex1(), x);
394 static ex psi2_series(const ex & n, const ex & x, const symbol & s, const ex & point, int order)
397 // Taylor series where there is no pole falls back to polygamma function
399 // On a pole at -m use the recurrence relation
400 // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
401 // from which follows
402 // series(psi(x),x,-m,order) ==
403 // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
404 // ... + (x+m)^(-n-1))),x,-m,order);
405 ex xpoint = x.subs(s==point);
406 if (!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive))
407 throw do_taylor(); // caught by function::series()
408 // if we got here we have to care for a pole of order n+1 at -m:
409 numeric m = -ex_to_numeric(xpoint);
411 for (numeric p; p<=m; ++p)
412 recur += power(x+p,-n+_ex_1());
413 recur *= factorial(n)*power(_ex_1(),n);
414 return (psi(n, x+m+_ex1())-recur).series(s, point, order);
417 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, psi2_series);
419 #ifndef NO_GINAC_NAMESPACE
421 #endif // ndef NO_GINAC_NAMESPACE