]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
- building GiNaC in a separate directory now works
[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_GINAC_NAMESPACE
38 namespace GiNaC {
39 #endif // ndef NO_GINAC_NAMESPACE
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_diff(const ex & x, unsigned diff_param)
98 {
99     GINAC_ASSERT(diff_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 symbol & s, const ex & pt, 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(s==pt);
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(s, pt, order+1);
126 }
127
128
129 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
130
131
132 //////////
133 // Beta-function
134 //////////
135
136 static ex beta_evalf(const ex & x, const ex & y)
137 {
138     BEGIN_TYPECHECK
139         TYPECHECK(x,numeric)
140         TYPECHECK(y,numeric)
141     END_TYPECHECK(beta(x,y))
142     
143     return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))/gamma(ex_to_numeric(x+y));
144 }
145
146
147 static ex beta_eval(const ex & x, const ex & y)
148 {
149     if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
150         // treat all problematic x and y that may not be passed into gamma,
151         // because they would throw there although beta(x,y) is well-defined
152         // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
153         numeric nx(ex_to_numeric(x));
154         numeric ny(ex_to_numeric(y));
155         if (nx.is_real() && nx.is_integer() &&
156             ny.is_real() && ny.is_integer()) {
157             if (nx.is_negative()) {
158                 if (nx<=-ny)
159                     return pow(_num_1(), ny)*beta(1-x-y, y);
160                 else
161                     throw (std::domain_error("beta_eval(): simple pole"));
162             }
163             if (ny.is_negative()) {
164                 if (ny<=-nx)
165                     return pow(_num_1(), nx)*beta(1-y-x, x);
166                 else
167                     throw (std::domain_error("beta_eval(): simple pole"));
168             }
169             return gamma(x)*gamma(y)/gamma(x+y);
170         }
171         // no problem in numerator, but denominator has pole:
172         if ((nx+ny).is_real() &&
173             (nx+ny).is_integer() &&
174             !(nx+ny).is_positive())
175              return _ex0();
176         // everything is ok:
177         return gamma(x)*gamma(y)/gamma(x+y);
178     }
179     
180     return beta(x,y).hold();
181 }
182
183
184 static ex beta_diff(const ex & x, const ex & y, unsigned diff_param)
185 {
186     GINAC_ASSERT(diff_param<2);
187     ex retval;
188     
189     // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
190     if (diff_param==0)
191         retval = (psi(x)-psi(x+y))*beta(x,y);
192     // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
193     if (diff_param==1)
194         retval = (psi(y)-psi(x+y))*beta(x,y);
195     return retval;
196 }
197
198
199 static ex beta_series(const ex & x, const ex & y, const symbol & s, const ex & pt, int order)
200 {
201     // method:
202     // Taylor series where there is no pole of one of the gamma functions
203     // falls back to beta function evaluation.  Otherwise, fall back to
204     // gamma series directly.
205     // FIXME: this could need some testing, maybe it's wrong in some cases?
206     const ex x_pt = x.subs(s==pt);
207     const ex y_pt = y.subs(s==pt);
208     ex x_ser, y_ser, xy_ser;
209     if ((!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive)) &&
210         (!y_pt.info(info_flags::integer) || y_pt.info(info_flags::positive)))
211         throw do_taylor();  // caught by function::series()
212     // trap the case where x is on a pole directly:
213     if (x.info(info_flags::integer) && !x.info(info_flags::positive))
214         x_ser = gamma(x+s).series(s,pt,order);
215     else
216         x_ser = gamma(x).series(s,pt,order);
217     // trap the case where y is on a pole directly:
218     if (y.info(info_flags::integer) && !y.info(info_flags::positive))
219         y_ser = gamma(y+s).series(s,pt,order);
220     else
221         y_ser = gamma(y).series(s,pt,order);
222     // trap the case where y is on a pole directly:
223     if ((x+y).info(info_flags::integer) && !(x+y).info(info_flags::positive))
224         xy_ser = gamma(y+x+s).series(s,pt,order);
225     else
226         xy_ser = gamma(y+x).series(s,pt,order);
227     // compose the result:
228     return (x_ser*y_ser/xy_ser).series(s,pt,order);
229 }
230
231
232 REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, beta_series);
233
234
235 //////////
236 // Psi-function (aka digamma-function)
237 //////////
238
239 static ex psi1_evalf(const ex & x)
240 {
241     BEGIN_TYPECHECK
242         TYPECHECK(x,numeric)
243     END_TYPECHECK(psi(x))
244     
245     return psi(ex_to_numeric(x));
246 }
247
248 /** Evaluation of digamma-function psi(x).
249  *  Somebody ought to provide some good numerical evaluation some day... */
250 static ex psi1_eval(const ex & x)
251 {
252     if (x.info(info_flags::numeric)) {
253         numeric nx = ex_to_numeric(x);
254         if (nx.is_integer()) {
255             // integer case 
256             if (nx.is_positive()) {
257                 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
258                 numeric rat(0);
259                 for (numeric i(nx+_num_1()); i.is_positive(); --i)
260                     rat += i.inverse();
261                 return rat-EulerGamma;
262             } else {
263                 // for non-positive integers there is a pole:
264                 throw (std::domain_error("psi_eval(): simple pole"));
265             }
266         }
267         if ((_num2()*nx).is_integer()) {
268             // half integer case
269             if (nx.is_positive()) {
270                 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
271                 numeric rat(0);
272                 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
273                                       rat += _num2()*i.inverse();
274                                       return rat-EulerGamma-_ex2()*log(_ex2());
275             } else {
276                 // use the recurrence relation
277                 //   psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
278                 // to relate psi(-m-1/2) to psi(1/2):
279                 //   psi(-m-1/2) == psi(1/2) + r
280                 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
281                 numeric recur(0);
282                 for (numeric p(nx); p<0; ++p)
283                     recur -= pow(p, _num_1());
284                 return recur+psi(_ex1_2());
285             }
286         }
287         //  psi1_evalf should be called here once it becomes available
288     }
289     
290     return psi(x).hold();
291 }
292
293 static ex psi1_diff(const ex & x, unsigned diff_param)
294 {
295     GINAC_ASSERT(diff_param==0);
296     
297     // d/dx psi(x) -> psi(1,x)
298     return psi(_ex1(), x);
299 }
300
301 static ex psi1_series(const ex & x, const symbol & s, const ex & pt, int order)
302 {
303     // method:
304     // Taylor series where there is no pole falls back to polygamma function
305     // evaluation.
306     // On a pole at -m use the recurrence relation
307     //   psi(x) == psi(x+1) - 1/z
308     // from which follows
309     //   series(psi(x),x,-m,order) ==
310     //   series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
311     const ex x_pt = x.subs(s==pt);
312     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
313         throw do_taylor();  // caught by function::series()
314     // if we got here we have to care for a simple pole at -m:
315     numeric m = -ex_to_numeric(x_pt);
316     ex recur;
317     for (numeric p; p<=m; ++p)
318         recur += power(x+p,_ex_1());
319     return (psi(x+m+_ex1())-recur).series(s, pt, order);
320 }
321
322 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, psi1_series);
323
324 //////////
325 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
326 //////////
327
328 static ex psi2_evalf(const ex & n, const ex & x)
329 {
330     BEGIN_TYPECHECK
331         TYPECHECK(n,numeric)
332         TYPECHECK(x,numeric)
333     END_TYPECHECK(psi(n,x))
334     
335     return psi(ex_to_numeric(n), ex_to_numeric(x));
336 }
337
338 /** Evaluation of polygamma-function psi(n,x). 
339  *  Somebody ought to provide some good numerical evaluation some day... */
340 static ex psi2_eval(const ex & n, const ex & x)
341 {
342     // psi(0,x) -> psi(x)
343     if (n.is_zero())
344         return psi(x);
345     // psi(-1,x) -> log(gamma(x))
346     if (n.is_equal(_ex_1()))
347         return log(gamma(x));
348     if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
349         x.info(info_flags::numeric)) {
350         numeric nn = ex_to_numeric(n);
351         numeric nx = ex_to_numeric(x);
352         if (nx.is_integer()) {
353             // integer case 
354             if (nx.is_equal(_num1()))
355                 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
356                 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
357             if (nx.is_positive()) {
358                 // use the recurrence relation
359                 //   psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
360                 // to relate psi(n,m) to psi(n,1):
361                 //   psi(n,m) == psi(n,1) + r
362                 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
363                 numeric recur(0);
364                 for (numeric p(1); p<nx; ++p)
365                     recur += pow(p, -nn+_num_1());
366                 recur *= factorial(nn)*pow(_num_1(), nn);
367                 return recur+psi(n,_ex1());
368             } else {
369                 // for non-positive integers there is a pole:
370                 throw (std::domain_error("psi2_eval(): pole"));
371             }
372         }
373         if ((_num2()*nx).is_integer()) {
374             // half integer case
375             if (nx.is_equal(_num1_2()))
376                 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
377                 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
378             if (nx.is_positive()) {
379                 numeric m = nx - _num1_2();
380                 // use the multiplication formula
381                 //   psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
382                 // to revert to positive integer case
383                 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
384             } else {
385                 // use the recurrence relation
386                 //   psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
387                 // to relate psi(n,-m-1/2) to psi(n,1/2):
388                 //   psi(n,-m-1/2) == psi(n,1/2) + r
389                 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
390                 numeric recur(0);
391                 for (numeric p(nx); p<0; ++p)
392                     recur += pow(p, -nn+_num_1());
393                 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
394                 return recur+psi(n,_ex1_2());
395             }
396         }
397         //  psi2_evalf should be called here once it becomes available
398     }
399     
400     return psi(n, x).hold();
401 }    
402
403 static ex psi2_diff(const ex & n, const ex & x, unsigned diff_param)
404 {
405     GINAC_ASSERT(diff_param<2);
406     
407     if (diff_param==0) {
408         // d/dn psi(n,x)
409         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
410     }
411     // d/dx psi(n,x) -> psi(n+1,x)
412     return psi(n+_ex1(), x);
413 }
414
415 static ex psi2_series(const ex & n, const ex & x, const symbol & s, const ex & pt, int order)
416 {
417     // method:
418     // Taylor series where there is no pole falls back to polygamma function
419     // evaluation.
420     // On a pole at -m use the recurrence relation
421     //   psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
422     // from which follows
423     //   series(psi(x),x,-m,order) == 
424     //   series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
425     //                                      ... + (x+m)^(-n-1))),x,-m,order);
426     const ex x_pt = x.subs(s==pt);
427     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
428         throw do_taylor();  // caught by function::series()
429     // if we got here we have to care for a pole of order n+1 at -m:
430     numeric m = -ex_to_numeric(x_pt);
431     ex recur;
432     for (numeric p; p<=m; ++p)
433         recur += power(x+p,-n+_ex_1());
434     recur *= factorial(n)*power(_ex_1(),n);
435     return (psi(n, x+m+_ex1())-recur).series(s, pt, order);
436 }
437
438 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, psi2_series);
439
440 #ifndef NO_GINAC_NAMESPACE
441 } // namespace GiNaC
442 #endif // ndef NO_GINAC_NAMESPACE