]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
- ginac.h includes version.h (created by configure script), which contains
[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-2000 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 "pseries.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_NAMESPACE_GINAC
38 namespace GiNaC {
39 #endif // ndef NO_NAMESPACE_GINAC
40
41 //////////
42 // Gamma-function
43 //////////
44
45 static ex Gamma_evalf(const ex & 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
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...
58  *
59  *  @exception std::domain_error("Gamma_eval(): simple pole") */
60 static ex Gamma_eval(const ex & x)
61 {
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()));
68             } else {
69                 throw (std::domain_error("Gamma_eval(): simple pole"));
70             }
71         }
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());
81             } else {
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());
88             }
89         }
90         //  Gamma_evalf should be called here once it becomes available
91     }
92     
93     return Gamma(x).hold();
94 }    
95
96
97 static ex Gamma_deriv(const ex & x, unsigned deriv_param)
98 {
99     GINAC_ASSERT(deriv_param==0);
100     
101     // d/dx  log(Gamma(x)) -> psi(x)
102     // d/dx  Gamma(x) -> psi(x)*Gamma(x)
103     return psi(x)*Gamma(x);
104 }
105
106
107 static ex Gamma_series(const ex & x, const relational & r, int order)
108 {
109     // method:
110     // Taylor series where there is no pole falls back to psi function
111     // evaluation.
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)
124         ser_denom *= x+p;
125     return (Gamma(x+m+_ex1())/ser_denom).series(r, order+1);
126 }
127
128
129 REGISTER_FUNCTION(Gamma, eval_func(Gamma_eval).
130                          evalf_func(Gamma_evalf).
131                          derivative_func(Gamma_deriv).
132                          series_func(Gamma_series));
133
134
135 //////////
136 // Beta-function
137 //////////
138
139 static ex Beta_evalf(const ex & x, const ex & y)
140 {
141     BEGIN_TYPECHECK
142         TYPECHECK(x,numeric)
143         TYPECHECK(y,numeric)
144     END_TYPECHECK(Beta(x,y))
145     
146     return Gamma(ex_to_numeric(x))*Gamma(ex_to_numeric(y))/Gamma(ex_to_numeric(x+y));
147 }
148
149
150 static ex Beta_eval(const ex & x, const ex & y)
151 {
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()) {
161                 if (nx<=-ny)
162                     return pow(_num_1(), ny)*Beta(1-x-y, y);
163                 else
164                     throw (std::domain_error("Beta_eval(): simple pole"));
165             }
166             if (ny.is_negative()) {
167                 if (ny<=-nx)
168                     return pow(_num_1(), nx)*Beta(1-y-x, x);
169                 else
170                     throw (std::domain_error("Beta_eval(): simple pole"));
171             }
172             return Gamma(x)*Gamma(y)/Gamma(x+y);
173         }
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())
178              return _ex0();
179         // everything is ok:
180         return Gamma(x)*Gamma(y)/Gamma(x+y);
181     }
182     
183     return Beta(x,y).hold();
184 }
185
186
187 static ex Beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
188 {
189     GINAC_ASSERT(deriv_param<2);
190     ex retval;
191     
192     // d/dx Beta(x,y) -> (psi(x)-psi(x+y)) * Beta(x,y)
193     if (deriv_param==0)
194         retval = (psi(x)-psi(x+y))*Beta(x,y);
195     // d/dy Beta(x,y) -> (psi(y)-psi(x+y)) * Beta(x,y)
196     if (deriv_param==1)
197         retval = (psi(y)-psi(x+y))*Beta(x,y);
198     return retval;
199 }
200
201
202 static ex Beta_series(const ex & x, const ex & y, const relational & r, int order)
203 {
204     // method:
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);
220     else
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);
225     else
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);
230     else
231         xy_ser = Gamma(y+x).series(r,order);
232     // compose the result:
233     return (x_ser*y_ser/xy_ser).series(r,order);
234 }
235
236
237 REGISTER_FUNCTION(Beta, eval_func(Beta_eval).
238                         evalf_func(Beta_evalf).
239                         derivative_func(Beta_deriv).
240                         series_func(Beta_series));
241
242
243 //////////
244 // Psi-function (aka digamma-function)
245 //////////
246
247 static ex psi1_evalf(const ex & x)
248 {
249     BEGIN_TYPECHECK
250         TYPECHECK(x,numeric)
251     END_TYPECHECK(psi(x))
252     
253     return psi(ex_to_numeric(x));
254 }
255
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)
259 {
260     if (x.info(info_flags::numeric)) {
261         numeric nx = ex_to_numeric(x);
262         if (nx.is_integer()) {
263             // integer case 
264             if (nx.is_positive()) {
265                 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - gamma
266                 numeric rat(0);
267                 for (numeric i(nx+_num_1()); i.is_positive(); --i)
268                     rat += i.inverse();
269                 return rat-gamma;
270             } else {
271                 // for non-positive integers there is a pole:
272                 throw (std::domain_error("psi_eval(): simple pole"));
273             }
274         }
275         if ((_num2()*nx).is_integer()) {
276             // half integer case
277             if (nx.is_positive()) {
278                 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - gamma - 2log(2)
279                 numeric rat(0);
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());
283             } else {
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))
289                 numeric recur(0);
290                 for (numeric p(nx); p<0; ++p)
291                     recur -= pow(p, _num_1());
292                 return recur+psi(_ex1_2());
293             }
294         }
295         //  psi1_evalf should be called here once it becomes available
296     }
297     
298     return psi(x).hold();
299 }
300
301 static ex psi1_deriv(const ex & x, unsigned deriv_param)
302 {
303     GINAC_ASSERT(deriv_param==0);
304     
305     // d/dx psi(x) -> psi(1,x)
306     return psi(_ex1(), x);
307 }
308
309 static ex psi1_series(const ex & x, const relational & r, 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(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);
324     ex recur;
325     for (numeric p; p<=m; ++p)
326         recur += power(x+p,_ex_1());
327     return (psi(x+m+_ex1())-recur).series(r, order);
328 }
329
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).
336                            overloaded(2));
337
338 //////////
339 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
340 //////////
341
342 static ex psi2_evalf(const ex & n, const ex & x)
343 {
344     BEGIN_TYPECHECK
345         TYPECHECK(n,numeric)
346         TYPECHECK(x,numeric)
347     END_TYPECHECK(psi(n,x))
348     
349     return psi(ex_to_numeric(n), ex_to_numeric(x));
350 }
351
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)
355 {
356     // psi(0,x) -> psi(x)
357     if (n.is_zero())
358         return 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()) {
367             // integer case 
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))
377                 numeric recur(0);
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());
382             } else {
383                 // for non-positive integers there is a pole:
384                 throw (std::domain_error("psi2_eval(): pole"));
385             }
386         }
387         if ((_num2()*nx).is_integer()) {
388             // half integer case
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);
398             } else {
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))
404                 numeric recur(0);
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());
409             }
410         }
411         //  psi2_evalf should be called here once it becomes available
412     }
413     
414     return psi(n, x).hold();
415 }    
416
417 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
418 {
419     GINAC_ASSERT(deriv_param<2);
420     
421     if (deriv_param==0) {
422         // d/dn psi(n,x)
423         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
424     }
425     // d/dx psi(n,x) -> psi(n+1,x)
426     return psi(n+_ex1(), x);
427 }
428
429 static ex psi2_series(const ex & n, const ex & x, const relational & r, int order)
430 {
431     // method:
432     // Taylor series where there is no pole falls back to polygamma function
433     // evaluation.
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);
445     ex recur;
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);
450 }
451
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).
458                            overloaded(2));
459
460
461 #ifndef NO_NAMESPACE_GINAC
462 } // namespace GiNaC
463 #endif // ndef NO_NAMESPACE_GINAC