]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
- print 0 without parens.
[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 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, 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 symbol & s, const ex & pt, 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(s==pt);
210     const ex y_pt = y.subs(s==pt);
211     ex x_ser, y_ser, xy_ser;
212     if ((!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive)) &&
213         (!y_pt.info(info_flags::integer) || y_pt.info(info_flags::positive)))
214         throw do_taylor();  // caught by function::series()
215     // trap the case where x is on a pole directly:
216     if (x.info(info_flags::integer) && !x.info(info_flags::positive))
217         x_ser = gamma(x+s).series(s,pt,order);
218     else
219         x_ser = gamma(x).series(s,pt,order);
220     // trap the case where y is on a pole directly:
221     if (y.info(info_flags::integer) && !y.info(info_flags::positive))
222         y_ser = gamma(y+s).series(s,pt,order);
223     else
224         y_ser = gamma(y).series(s,pt,order);
225     // trap the case where y is on a pole directly:
226     if ((x+y).info(info_flags::integer) && !(x+y).info(info_flags::positive))
227         xy_ser = gamma(y+x+s).series(s,pt,order);
228     else
229         xy_ser = gamma(y+x).series(s,pt,order);
230     // compose the result:
231     return (x_ser*y_ser/xy_ser).series(s,pt,order);
232 }
233
234
235 REGISTER_FUNCTION(beta, eval_func(beta_eval).
236                         evalf_func(beta_evalf).
237                         derivative_func(beta_deriv).
238                         series_func(beta_series));
239
240
241 //////////
242 // Psi-function (aka digamma-function)
243 //////////
244
245 static ex psi1_evalf(const ex & x)
246 {
247     BEGIN_TYPECHECK
248         TYPECHECK(x,numeric)
249     END_TYPECHECK(psi(x))
250     
251     return psi(ex_to_numeric(x));
252 }
253
254 /** Evaluation of digamma-function psi(x).
255  *  Somebody ought to provide some good numerical evaluation some day... */
256 static ex psi1_eval(const ex & x)
257 {
258     if (x.info(info_flags::numeric)) {
259         numeric nx = ex_to_numeric(x);
260         if (nx.is_integer()) {
261             // integer case 
262             if (nx.is_positive()) {
263                 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
264                 numeric rat(0);
265                 for (numeric i(nx+_num_1()); i.is_positive(); --i)
266                     rat += i.inverse();
267                 return rat-EulerGamma;
268             } else {
269                 // for non-positive integers there is a pole:
270                 throw (std::domain_error("psi_eval(): simple pole"));
271             }
272         }
273         if ((_num2()*nx).is_integer()) {
274             // half integer case
275             if (nx.is_positive()) {
276                 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
277                 numeric rat(0);
278                 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
279                                       rat += _num2()*i.inverse();
280                                       return rat-EulerGamma-_ex2()*log(_ex2());
281             } else {
282                 // use the recurrence relation
283                 //   psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
284                 // to relate psi(-m-1/2) to psi(1/2):
285                 //   psi(-m-1/2) == psi(1/2) + r
286                 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
287                 numeric recur(0);
288                 for (numeric p(nx); p<0; ++p)
289                     recur -= pow(p, _num_1());
290                 return recur+psi(_ex1_2());
291             }
292         }
293         //  psi1_evalf should be called here once it becomes available
294     }
295     
296     return psi(x).hold();
297 }
298
299 static ex psi1_deriv(const ex & x, unsigned deriv_param)
300 {
301     GINAC_ASSERT(deriv_param==0);
302     
303     // d/dx psi(x) -> psi(1,x)
304     return psi(_ex1(), x);
305 }
306
307 static ex psi1_series(const ex & x, const symbol & s, const ex & pt, int order)
308 {
309     // method:
310     // Taylor series where there is no pole falls back to polygamma function
311     // evaluation.
312     // On a pole at -m use the recurrence relation
313     //   psi(x) == psi(x+1) - 1/z
314     // from which follows
315     //   series(psi(x),x,-m,order) ==
316     //   series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
317     const ex x_pt = x.subs(s==pt);
318     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
319         throw do_taylor();  // caught by function::series()
320     // if we got here we have to care for a simple pole at -m:
321     numeric m = -ex_to_numeric(x_pt);
322     ex recur;
323     for (numeric p; p<=m; ++p)
324         recur += power(x+p,_ex_1());
325     return (psi(x+m+_ex1())-recur).series(s, pt, order);
326 }
327
328 const unsigned function_index_psi1 =
329     function::register_new(function_options("psi").
330                            eval_func(psi1_eval).
331                            evalf_func(psi1_evalf).
332                            derivative_func(psi1_deriv).
333                            series_func(psi1_series).
334                            overloaded(2));
335
336 //////////
337 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
338 //////////
339
340 static ex psi2_evalf(const ex & n, const ex & x)
341 {
342     BEGIN_TYPECHECK
343         TYPECHECK(n,numeric)
344         TYPECHECK(x,numeric)
345     END_TYPECHECK(psi(n,x))
346     
347     return psi(ex_to_numeric(n), ex_to_numeric(x));
348 }
349
350 /** Evaluation of polygamma-function psi(n,x). 
351  *  Somebody ought to provide some good numerical evaluation some day... */
352 static ex psi2_eval(const ex & n, const ex & x)
353 {
354     // psi(0,x) -> psi(x)
355     if (n.is_zero())
356         return psi(x);
357     // psi(-1,x) -> log(gamma(x))
358     if (n.is_equal(_ex_1()))
359         return log(gamma(x));
360     if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
361         x.info(info_flags::numeric)) {
362         numeric nn = ex_to_numeric(n);
363         numeric nx = ex_to_numeric(x);
364         if (nx.is_integer()) {
365             // integer case 
366             if (nx.is_equal(_num1()))
367                 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
368                 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
369             if (nx.is_positive()) {
370                 // use the recurrence relation
371                 //   psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
372                 // to relate psi(n,m) to psi(n,1):
373                 //   psi(n,m) == psi(n,1) + r
374                 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
375                 numeric recur(0);
376                 for (numeric p(1); p<nx; ++p)
377                     recur += pow(p, -nn+_num_1());
378                 recur *= factorial(nn)*pow(_num_1(), nn);
379                 return recur+psi(n,_ex1());
380             } else {
381                 // for non-positive integers there is a pole:
382                 throw (std::domain_error("psi2_eval(): pole"));
383             }
384         }
385         if ((_num2()*nx).is_integer()) {
386             // half integer case
387             if (nx.is_equal(_num1_2()))
388                 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
389                 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
390             if (nx.is_positive()) {
391                 numeric m = nx - _num1_2();
392                 // use the multiplication formula
393                 //   psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
394                 // to revert to positive integer case
395                 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
396             } else {
397                 // use the recurrence relation
398                 //   psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
399                 // to relate psi(n,-m-1/2) to psi(n,1/2):
400                 //   psi(n,-m-1/2) == psi(n,1/2) + r
401                 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
402                 numeric recur(0);
403                 for (numeric p(nx); p<0; ++p)
404                     recur += pow(p, -nn+_num_1());
405                 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
406                 return recur+psi(n,_ex1_2());
407             }
408         }
409         //  psi2_evalf should be called here once it becomes available
410     }
411     
412     return psi(n, x).hold();
413 }    
414
415 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
416 {
417     GINAC_ASSERT(deriv_param<2);
418     
419     if (deriv_param==0) {
420         // d/dn psi(n,x)
421         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
422     }
423     // d/dx psi(n,x) -> psi(n+1,x)
424     return psi(n+_ex1(), x);
425 }
426
427 static ex psi2_series(const ex & n, const ex & x, const symbol & s, const ex & pt, int order)
428 {
429     // method:
430     // Taylor series where there is no pole falls back to polygamma function
431     // evaluation.
432     // On a pole at -m use the recurrence relation
433     //   psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
434     // from which follows
435     //   series(psi(x),x,-m,order) == 
436     //   series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
437     //                                      ... + (x+m)^(-n-1))),x,-m,order);
438     const ex x_pt = x.subs(s==pt);
439     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
440         throw do_taylor();  // caught by function::series()
441     // if we got here we have to care for a pole of order n+1 at -m:
442     numeric m = -ex_to_numeric(x_pt);
443     ex recur;
444     for (numeric p; p<=m; ++p)
445         recur += power(x+p,-n+_ex_1());
446     recur *= factorial(n)*power(_ex_1(),n);
447     return (psi(n, x+m+_ex1())-recur).series(s, pt, order);
448 }
449
450 const unsigned function_index_psi2 =
451     function::register_new(function_options("psi").
452                            eval_func(psi2_eval).
453                            evalf_func(psi2_evalf).
454                            derivative_func(psi2_deriv).
455                            series_func(psi2_series).
456                            overloaded(2));
457
458
459 #ifndef NO_NAMESPACE_GINAC
460 } // namespace GiNaC
461 #endif // ndef NO_NAMESPACE_GINAC