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(ex const & 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(ex const & 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*2).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(_num2().power(n));
79 return coefficient * pow(Pi,_num1_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 = numeric(-2).power(n);
85 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
86 return coefficient*sqrt(Pi);
90 return gamma(x).hold();
93 static ex gamma_diff(ex const & x, unsigned diff_param)
95 GINAC_ASSERT(diff_param==0);
97 // d/dx log(gamma(x)) -> psi(x)
98 // d/dx gamma(x) -> psi(x)*gamma(x)
99 return psi(x)*gamma(x);
102 static ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
105 // Taylor series where there is no pole falls back to psi function evaluation.
106 // On a pole at -m use the recurrence relation
107 // gamma(x) == gamma(x+1) / x
108 // from which follows
109 // series(gamma(x),x,-m,order) ==
110 // series(gamma(x+m+1)/(x*(x+1)...*(x+m)),x,-m,order+1);
111 ex xpoint = x.subs(s==point);
112 if (!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive))
113 throw do_taylor(); // caught by function::series()
114 // if we got here we have to care for a simple pole at -m:
115 numeric m = -ex_to_numeric(xpoint);
116 ex ser_numer = gamma(x+m+_ex1());
117 ex ser_denom = _ex1();
118 for (numeric p; p<=m; ++p)
120 return (ser_numer/ser_denom).series(s, point, order+1);
123 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
129 static ex beta_evalf(ex const & x, ex const & y)
134 END_TYPECHECK(beta(x,y))
136 return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
137 / gamma(ex_to_numeric(x+y));
140 static ex beta_eval(ex const & x, ex const & y)
142 if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
143 numeric nx(ex_to_numeric(x));
144 numeric ny(ex_to_numeric(y));
145 // treat all problematic x and y that may not be passed into gamma,
146 // because they would throw there although beta(x,y) is well-defined:
147 if (nx.is_real() && nx.is_integer() &&
148 ny.is_real() && ny.is_integer()) {
149 if (nx.is_negative()) {
151 return _num_1().power(ny)*beta(1-x-y, y);
153 throw (std::domain_error("beta_eval(): simple pole"));
155 if (ny.is_negative()) {
157 return _num_1().power(nx)*beta(1-y-x, x);
159 throw (std::domain_error("beta_eval(): simple pole"));
161 return gamma(x)*gamma(y)/gamma(x+y);
163 // no problem in numerator, but denominator has pole:
164 if ((nx+ny).is_real() &&
165 (nx+ny).is_integer() &&
166 !(nx+ny).is_positive())
168 return gamma(x)*gamma(y)/gamma(x+y);
170 return beta(x,y).hold();
173 static ex beta_diff(ex const & x, ex const & y, unsigned diff_param)
175 GINAC_ASSERT(diff_param<2);
178 // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
180 retval = (psi(x)-psi(x+y))*beta(x,y);
181 // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
183 retval = (psi(y)-psi(x+y))*beta(x,y);
187 REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, NULL);
190 // Psi-function (aka digamma-function)
193 static ex psi1_evalf(ex const & x)
197 END_TYPECHECK(psi(x))
199 return psi(ex_to_numeric(x));
202 /** Evaluation of digamma-function psi(x).
203 * Somebody ought to provide some good numerical evaluation some day... */
204 static ex psi1_eval(ex const & x)
206 if (x.info(info_flags::numeric)) {
207 if (x.info(info_flags::integer) && !x.info(info_flags::positive))
208 throw (std::domain_error("psi_eval(): simple pole"));
209 if (x.info(info_flags::positive)) {
210 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
211 if (x.info(info_flags::integer)) {
213 for (numeric i(ex_to_numeric(x)-_num1()); i.is_positive(); --i)
215 return rat-EulerGamma;
217 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
218 if ((_ex2()*x).info(info_flags::integer)) {
220 for (numeric i((ex_to_numeric(x)-_num1())*_num2()); i.is_positive(); i-=_num2())
221 rat += _num2()*i.inverse();
222 return rat-EulerGamma-_ex2()*log(_ex2());
224 if (x.compare(_ex1())==1) {
225 // should call numeric, since >1
229 return psi(x).hold();
232 static ex psi1_diff(ex const & x, unsigned diff_param)
234 GINAC_ASSERT(diff_param==0);
236 // d/dx psi(x) -> psi(1,x)
237 return psi(_ex1(), x);
240 static ex psi1_series(ex const & x, symbol const & s, ex const & point, int order)
243 // Taylor series where there is no pole falls back to polygamma function
245 // On a pole at -m use the recurrence relation
246 // psi(x) == psi(x+1) - 1/z
247 // from which follows
248 // series(psi(x),x,-m,order) ==
249 // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
250 ex xpoint = x.subs(s==point);
251 if (!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive))
252 throw do_taylor(); // caught by function::series()
253 // if we got here we have to care for a simple pole at -m:
254 numeric m = -ex_to_numeric(xpoint);
256 for (numeric p; p<=m; ++p)
257 recur += power(x+p,_ex_1());
258 return (psi(x+m+_ex1())-recur).series(s, point, order);
261 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, psi1_series);
264 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
267 static ex psi2_evalf(ex const & n, ex const & x)
272 END_TYPECHECK(psi(n,x))
274 return psi(ex_to_numeric(n), ex_to_numeric(x));
277 /** Evaluation of polygamma-function psi(n,x).
278 * Somebody ought to provide some good numerical evaluation some day... */
279 static ex psi2_eval(ex const & n, ex const & x)
281 // psi(0,x) -> psi(x)
284 // psi(-1,x) -> log(gamma(x))
285 if (n.is_equal(_ex_1()))
286 return log(gamma(x));
287 if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
288 x.info(info_flags::numeric)) {
289 numeric nn = ex_to_numeric(n);
290 numeric nx = ex_to_numeric(x);
291 if (x.is_equal(_ex1()))
292 return _num_1().power(nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
294 return psi(n, x).hold();
297 static ex psi2_diff(ex const & n, ex const & x, unsigned diff_param)
299 GINAC_ASSERT(diff_param<2);
303 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
305 // d/dx psi(n,x) -> psi(n+1,x)
309 static ex psi2_series(ex const & n, ex const & x, symbol const & s, ex const & point, 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(n,x) == psi(n,x+1) - (-)^n * n! / z^(n+1)
316 // from which follows
317 // series(psi(x),x,-m,order) ==
318 // series(psi(x+m+1) - (-1)^n * n!
319 // * ((x)^(-n-1) + (x+1)^(-n-1) + (x+m)^(-n-1))),x,-m,order);
320 ex xpoint = x.subs(s==point);
321 if (!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive))
322 throw do_taylor(); // caught by function::series()
323 // if we got here we have to care for a pole of order n+1 at -m:
324 numeric m = -ex_to_numeric(xpoint);
326 for (numeric p; p<=m; ++p)
327 recur += power(x+p,-n+_ex_1());
328 recur *= factorial(n)*power(_ex_1(),n);
329 return (psi(n, x+m+_ex1())-recur).series(s, point, order);
332 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, psi2_series);
334 #ifndef NO_GINAC_NAMESPACE
336 #endif // ndef NO_GINAC_NAMESPACE