]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
3f7fc677adda5d3725418a02be8351bc72f4001c
[ginac.git] / ginac / inifcns_gamma.cpp
1 /** @file inifcns_gamma.cpp
2  *
3  *  Implementation of Gamma-function, Beta-function, Polygamma-functions, and
4  *  some related stuff. */
5
6 /*
7  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <vector>
25 #include <stdexcept>
26
27 #include "inifcns.h"
28 #include "ex.h"
29 #include "constant.h"
30 #include "series.h"
31 #include "numeric.h"
32 #include "power.h"
33 #include "relational.h"
34 #include "symbol.h"
35 #include "utils.h"
36
37 #ifndef NO_GINAC_NAMESPACE
38 namespace GiNaC {
39 #endif // ndef NO_GINAC_NAMESPACE
40
41 //////////
42 // Gamma-function
43 //////////
44
45 static ex gamma_evalf(ex const & x)
46 {
47     BEGIN_TYPECHECK
48         TYPECHECK(x,numeric)
49     END_TYPECHECK(gamma(x))
50     
51     return gamma(ex_to_numeric(x));
52 }
53
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...
57  *
58  *  @exception std::domain_error("gamma_eval(): simple pole") */
59 static ex gamma_eval(ex const & x)
60 {
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()));
67             } else {
68                 throw (std::domain_error("gamma_eval(): simple pole"));
69             }
70         }
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());
80             } else {
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);
87             }
88         }
89     }
90     return gamma(x).hold();
91 }    
92
93 static ex gamma_diff(ex const & x, unsigned diff_param)
94 {
95     GINAC_ASSERT(diff_param==0);
96     
97     // d/dx  log(gamma(x)) -> psi(x)
98     // d/dx  gamma(x) -> psi(x)*gamma(x)
99     return psi(x)*gamma(x);
100 }
101
102 static ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
103 {
104     // method:
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)
119         ser_denom *= x+p;
120     return (ser_numer/ser_denom).series(s, point, order+1);
121 }
122
123 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
124
125 //////////
126 // Beta-function
127 //////////
128
129 static ex beta_evalf(ex const & x, ex const & y)
130 {
131     BEGIN_TYPECHECK
132         TYPECHECK(x,numeric)
133         TYPECHECK(y,numeric)
134     END_TYPECHECK(beta(x,y))
135     
136     return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
137         / gamma(ex_to_numeric(x+y));
138 }
139
140 static ex beta_eval(ex const & x, ex const & y)
141 {
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()) {
150                 if (nx<=-ny)
151                     return _num_1().power(ny)*beta(1-x-y, y);
152                 else
153                     throw (std::domain_error("beta_eval(): simple pole"));
154             }
155             if (ny.is_negative()) {
156                 if (ny<=-nx)
157                     return _num_1().power(nx)*beta(1-y-x, x);
158                 else
159                     throw (std::domain_error("beta_eval(): simple pole"));
160             }
161             return gamma(x)*gamma(y)/gamma(x+y);
162         }
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())
167             return _ex0();
168         return gamma(x)*gamma(y)/gamma(x+y);
169     }
170     return beta(x,y).hold();
171 }
172
173 static ex beta_diff(ex const & x, ex const & y, unsigned diff_param)
174 {
175     GINAC_ASSERT(diff_param<2);
176     ex retval;
177     
178     // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
179     if (diff_param==0)
180         retval = (psi(x)-psi(x+y))*beta(x,y);
181     // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
182     if (diff_param==1)  
183         retval = (psi(y)-psi(x+y))*beta(x,y);
184     return retval;
185 }
186
187 REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, NULL);
188
189 //////////
190 // Psi-function (aka digamma-function)
191 //////////
192
193 static ex psi1_evalf(ex const & x)
194 {
195     BEGIN_TYPECHECK
196         TYPECHECK(x,numeric)
197     END_TYPECHECK(psi(x))
198     
199     return psi(ex_to_numeric(x));
200 }
201
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)
205 {
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)) {
212                 numeric rat(0);
213                 for (numeric i(ex_to_numeric(x)-_num1()); i.is_positive(); --i)
214                     rat += i.inverse();
215                 return rat-EulerGamma;
216             }
217             // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
218             if ((_ex2()*x).info(info_flags::integer)) {
219                 numeric rat(0);
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());
223             }
224             if (x.compare(_ex1())==1) {
225                 // should call numeric, since >1
226             }
227         }
228     }
229     return psi(x).hold();
230 }
231
232 static ex psi1_diff(ex const & x, unsigned diff_param)
233 {
234     GINAC_ASSERT(diff_param==0);
235     
236     // d/dx psi(x) -> psi(1,x)
237     return psi(_ex1(), x);
238 }
239
240 static ex psi1_series(ex const & x, symbol const & s, ex const & point, int order)
241 {
242     // method:
243     // Taylor series where there is no pole falls back to polygamma function
244     // evaluation.
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);
255     ex recur;
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);
259 }
260
261 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, psi1_series);
262
263 //////////
264 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
265 //////////
266
267 static ex psi2_evalf(ex const & n, ex const & x)
268 {
269     BEGIN_TYPECHECK
270         TYPECHECK(n,numeric)
271         TYPECHECK(x,numeric)
272     END_TYPECHECK(psi(n,x))
273     
274     return psi(ex_to_numeric(n), ex_to_numeric(x));
275 }
276
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)
280 {
281     // psi(0,x) -> psi(x)
282     if (n.is_zero())
283         return 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()));
293     }
294     return psi(n, x).hold();
295 }    
296
297 static ex psi2_diff(ex const & n, ex const & x, unsigned diff_param)
298 {
299     GINAC_ASSERT(diff_param<2);
300     
301     if (diff_param==0) {
302         // d/dn psi(n,x)
303         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
304     }
305     // d/dx psi(n,x) -> psi(n+1,x)
306     return psi(n+1, x);
307 }
308
309 static ex psi2_series(ex const & n, ex const & x, symbol const & s, ex const & point, int order)
310 {
311     // method:
312     // Taylor series where there is no pole falls back to polygamma function
313     // evaluation.
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);
325     ex recur;
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);
330 }
331
332 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, psi2_series);
333
334 #ifndef NO_GINAC_NAMESPACE
335 } // namespace GiNaC
336 #endif // ndef NO_GINAC_NAMESPACE