]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
85275f62aabec258d301d794bcc764a36f92ce21
[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 // Logarithm of Gamma function
43 //////////
44
45 static ex lgamma_evalf(const ex & x)
46 {
47     BEGIN_TYPECHECK
48         TYPECHECK(x,numeric)
49     END_TYPECHECK(lgamma(x))
50     
51     return lgamma(ex_to_numeric(x));
52 }
53
54
55 /** Evaluation of lgamma(x), the natural logarithm of the Gamma function.
56  *  Knows about integer arguments and that's it.  Somebody ought to provide
57  *  some good numerical evaluation some day...
58  *
59  *  @exception std::domain_error("lgamma_eval(): simple pole") */
60 static ex lgamma_eval(const ex & x)
61 {
62     if (x.info(info_flags::numeric)) {
63         // trap integer arguments:
64         if (x.info(info_flags::integer)) {
65             // lgamma(n) -> log((n-1)!) for postitive n
66             if (x.info(info_flags::posint)) {
67                 return log(factorial(x.exadd(_ex_1())));
68             } else {
69                 throw (std::domain_error("lgamma_eval(): logarithmic pole"));
70             }
71         }
72         //  lgamma_evalf should be called here once it becomes available
73     }
74     
75     return lgamma(x).hold();
76 }
77
78
79 static ex lgamma_deriv(const ex & x, unsigned deriv_param)
80 {
81     GINAC_ASSERT(deriv_param==0);
82     
83     // d/dx  lgamma(x) -> psi(x)
84     return psi(x);
85 }
86
87
88 static ex lgamma_series(const ex & x, const relational & rel, int order)
89 {
90     // method:
91     // Taylor series where there is no pole falls back to psi function
92     // evaluation.
93     // On a pole at -m use the recurrence relation
94     //   lgamma(x) == lgamma(x+1)-log(x)
95     // from which follows
96     //   series(lgamma(x),x==-m,order) ==
97     //   series(lgamma(x+m+1)-log(x)...-log(x+m)),x==-m,order+1);
98     const ex x_pt = x.subs(rel);
99     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
100         throw do_taylor();  // caught by function::series()
101     // if we got here we have to care for a simple pole at -m:
102     numeric m = -ex_to_numeric(x_pt);
103     ex ser_sub = _ex0();
104     for (numeric p; p<=m; ++p)
105         ser_sub += log(x+p);
106     return (lgamma(x+m+_ex1())-ser_sub).series(rel, order);
107 }
108
109
110 REGISTER_FUNCTION(lgamma, eval_func(lgamma_eval).
111                           evalf_func(lgamma_evalf).
112                           derivative_func(lgamma_deriv).
113                           series_func(lgamma_series));
114
115
116 //////////
117 // true Gamma function
118 //////////
119
120 static ex tgamma_evalf(const ex & x)
121 {
122     BEGIN_TYPECHECK
123         TYPECHECK(x,numeric)
124     END_TYPECHECK(tgamma(x))
125     
126     return tgamma(ex_to_numeric(x));
127 }
128
129
130 /** Evaluation of tgamma(x), the true Gamma function.  Knows about integer
131  *  arguments, half-integer arguments and that's it. Somebody ought to provide
132  *  some good numerical evaluation some day...
133  *
134  *  @exception std::domain_error("tgamma_eval(): simple pole") */
135 static ex tgamma_eval(const ex & x)
136 {
137     if (x.info(info_flags::numeric)) {
138         // trap integer arguments:
139         if (x.info(info_flags::integer)) {
140             // tgamma(n) -> (n-1)! for postitive n
141             if (x.info(info_flags::posint)) {
142                 return factorial(ex_to_numeric(x).sub(_num1()));
143             } else {
144                 throw (std::domain_error("tgamma_eval(): simple pole"));
145             }
146         }
147         // trap half integer arguments:
148         if ((x*2).info(info_flags::integer)) {
149             // trap positive x==(n+1/2)
150             // tgamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
151             if ((x*_ex2()).info(info_flags::posint)) {
152                 numeric n = ex_to_numeric(x).sub(_num1_2());
153                 numeric coefficient = doublefactorial(n.mul(_num2()).sub(_num1()));
154                 coefficient = coefficient.div(pow(_num2(),n));
155                 return coefficient * pow(Pi,_ex1_2());
156             } else {
157                 // trap negative x==(-n+1/2)
158                 // tgamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
159                 numeric n = abs(ex_to_numeric(x).sub(_num1_2()));
160                 numeric coefficient = pow(_num_2(), n);
161                 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
162                 return coefficient*power(Pi,_ex1_2());
163             }
164         }
165         //  tgamma_evalf should be called here once it becomes available
166     }
167     
168     return tgamma(x).hold();
169 }
170
171
172 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
173 {
174     GINAC_ASSERT(deriv_param==0);
175     
176     // d/dx  tgamma(x) -> psi(x)*tgamma(x)
177     return psi(x)*tgamma(x);
178 }
179
180
181 static ex tgamma_series(const ex & x, const relational & rel, int order)
182 {
183     // method:
184     // Taylor series where there is no pole falls back to psi function
185     // evaluation.
186     // On a pole at -m use the recurrence relation
187     //   tgamma(x) == tgamma(x+1) / x
188     // from which follows
189     //   series(tgamma(x),x==-m,order) ==
190     //   series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x==-m,order+1);
191     const ex x_pt = x.subs(rel);
192     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
193         throw do_taylor();  // caught by function::series()
194     // if we got here we have to care for a simple pole at -m:
195     numeric m = -ex_to_numeric(x_pt);
196     ex ser_denom = _ex1();
197     for (numeric p; p<=m; ++p)
198         ser_denom *= x+p;
199     return (tgamma(x+m+_ex1())/ser_denom).series(rel, order+1);
200 }
201
202
203 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
204                           evalf_func(tgamma_evalf).
205                           derivative_func(tgamma_deriv).
206                           series_func(tgamma_series));
207
208
209 //////////
210 // beta-function
211 //////////
212
213 static ex beta_evalf(const ex & x, const ex & y)
214 {
215     BEGIN_TYPECHECK
216         TYPECHECK(x,numeric)
217         TYPECHECK(y,numeric)
218     END_TYPECHECK(beta(x,y))
219     
220     return tgamma(ex_to_numeric(x))*tgamma(ex_to_numeric(y))/tgamma(ex_to_numeric(x+y));
221 }
222
223
224 static ex beta_eval(const ex & x, const ex & y)
225 {
226     if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
227         // treat all problematic x and y that may not be passed into tgamma,
228         // because they would throw there although beta(x,y) is well-defined
229         // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
230         numeric nx(ex_to_numeric(x));
231         numeric ny(ex_to_numeric(y));
232         if (nx.is_real() && nx.is_integer() &&
233             ny.is_real() && ny.is_integer()) {
234             if (nx.is_negative()) {
235                 if (nx<=-ny)
236                     return pow(_num_1(), ny)*beta(1-x-y, y);
237                 else
238                     throw (std::domain_error("beta_eval(): simple pole"));
239             }
240             if (ny.is_negative()) {
241                 if (ny<=-nx)
242                     return pow(_num_1(), nx)*beta(1-y-x, x);
243                 else
244                     throw (std::domain_error("beta_eval(): simple pole"));
245             }
246             return tgamma(x)*tgamma(y)/tgamma(x+y);
247         }
248         // no problem in numerator, but denominator has pole:
249         if ((nx+ny).is_real() &&
250             (nx+ny).is_integer() &&
251             !(nx+ny).is_positive())
252              return _ex0();
253         // everything is ok:
254         return tgamma(x)*tgamma(y)/tgamma(x+y);
255     }
256     
257     return beta(x,y).hold();
258 }
259
260
261 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
262 {
263     GINAC_ASSERT(deriv_param<2);
264     ex retval;
265     
266     // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
267     if (deriv_param==0)
268         retval = (psi(x)-psi(x+y))*beta(x,y);
269     // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
270     if (deriv_param==1)
271         retval = (psi(y)-psi(x+y))*beta(x,y);
272     return retval;
273 }
274
275
276 static ex beta_series(const ex & x, const ex & y, const relational & rel, int order)
277 {
278     // method:
279     // Taylor series where there is no pole of one of the tgamma functions
280     // falls back to beta function evaluation.  Otherwise, fall back to
281     // tgamma series directly.
282     const ex x_pt = x.subs(rel);
283     const ex y_pt = y.subs(rel);
284     GINAC_ASSERT(is_ex_exactly_of_type(rel.lhs(),symbol));
285     const symbol *s = static_cast<symbol *>(rel.lhs().bp);
286     ex x_ser, y_ser, xy_ser;
287     if ((!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive)) &&
288         (!y_pt.info(info_flags::integer) || y_pt.info(info_flags::positive)))
289         throw do_taylor();  // caught by function::series()
290     // trap the case where x is on a pole directly:
291     if (x.info(info_flags::integer) && !x.info(info_flags::positive))
292         x_ser = tgamma(x+*s).series(rel,order);
293     else
294         x_ser = tgamma(x).series(rel,order);
295     // trap the case where y is on a pole directly:
296     if (y.info(info_flags::integer) && !y.info(info_flags::positive))
297         y_ser = tgamma(y+*s).series(rel,order);
298     else
299         y_ser = tgamma(y).series(rel,order);
300     // trap the case where y is on a pole directly:
301     if ((x+y).info(info_flags::integer) && !(x+y).info(info_flags::positive))
302         xy_ser = tgamma(y+x+*s).series(rel,order);
303     else
304         xy_ser = tgamma(y+x).series(rel,order);
305     // compose the result:
306     return (x_ser*y_ser/xy_ser).series(rel,order);
307 }
308
309
310 REGISTER_FUNCTION(beta, eval_func(beta_eval).
311                         evalf_func(beta_evalf).
312                         derivative_func(beta_deriv).
313                         series_func(beta_series));
314
315
316 //////////
317 // Psi-function (aka digamma-function)
318 //////////
319
320 static ex psi1_evalf(const ex & x)
321 {
322     BEGIN_TYPECHECK
323         TYPECHECK(x,numeric)
324     END_TYPECHECK(psi(x))
325     
326     return psi(ex_to_numeric(x));
327 }
328
329 /** Evaluation of digamma-function psi(x).
330  *  Somebody ought to provide some good numerical evaluation some day... */
331 static ex psi1_eval(const ex & x)
332 {
333     if (x.info(info_flags::numeric)) {
334         numeric nx = ex_to_numeric(x);
335         if (nx.is_integer()) {
336             // integer case 
337             if (nx.is_positive()) {
338                 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
339                 numeric rat(0);
340                 for (numeric i(nx+_num_1()); i.is_positive(); --i)
341                     rat += i.inverse();
342                 return rat-Euler;
343             } else {
344                 // for non-positive integers there is a pole:
345                 throw (std::domain_error("psi_eval(): simple pole"));
346             }
347         }
348         if ((_num2()*nx).is_integer()) {
349             // half integer case
350             if (nx.is_positive()) {
351                 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
352                 numeric rat(0);
353                 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
354                                       rat += _num2()*i.inverse();
355                                       return rat-Euler-_ex2()*log(_ex2());
356             } else {
357                 // use the recurrence relation
358                 //   psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
359                 // to relate psi(-m-1/2) to psi(1/2):
360                 //   psi(-m-1/2) == psi(1/2) + r
361                 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
362                 numeric recur(0);
363                 for (numeric p(nx); p<0; ++p)
364                     recur -= pow(p, _num_1());
365                 return recur+psi(_ex1_2());
366             }
367         }
368         //  psi1_evalf should be called here once it becomes available
369     }
370     
371     return psi(x).hold();
372 }
373
374 static ex psi1_deriv(const ex & x, unsigned deriv_param)
375 {
376     GINAC_ASSERT(deriv_param==0);
377     
378     // d/dx psi(x) -> psi(1,x)
379     return psi(_ex1(), x);
380 }
381
382 static ex psi1_series(const ex & x, const relational & rel, int order)
383 {
384     // method:
385     // Taylor series where there is no pole falls back to polygamma function
386     // evaluation.
387     // On a pole at -m use the recurrence relation
388     //   psi(x) == psi(x+1) - 1/z
389     // from which follows
390     //   series(psi(x),x==-m,order) ==
391     //   series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
392     const ex x_pt = x.subs(rel);
393     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
394         throw do_taylor();  // caught by function::series()
395     // if we got here we have to care for a simple pole at -m:
396     numeric m = -ex_to_numeric(x_pt);
397     ex recur;
398     for (numeric p; p<=m; ++p)
399         recur += power(x+p,_ex_1());
400     return (psi(x+m+_ex1())-recur).series(rel, order);
401 }
402
403 const unsigned function_index_psi1 =
404     function::register_new(function_options("psi").
405                            eval_func(psi1_eval).
406                            evalf_func(psi1_evalf).
407                            derivative_func(psi1_deriv).
408                            series_func(psi1_series).
409                            overloaded(2));
410
411 //////////
412 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
413 //////////
414
415 static ex psi2_evalf(const ex & n, const ex & x)
416 {
417     BEGIN_TYPECHECK
418         TYPECHECK(n,numeric)
419         TYPECHECK(x,numeric)
420     END_TYPECHECK(psi(n,x))
421     
422     return psi(ex_to_numeric(n), ex_to_numeric(x));
423 }
424
425 /** Evaluation of polygamma-function psi(n,x). 
426  *  Somebody ought to provide some good numerical evaluation some day... */
427 static ex psi2_eval(const ex & n, const ex & x)
428 {
429     // psi(0,x) -> psi(x)
430     if (n.is_zero())
431         return psi(x);
432     // psi(-1,x) -> log(tgamma(x))
433     if (n.is_equal(_ex_1()))
434         return log(tgamma(x));
435     if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
436         x.info(info_flags::numeric)) {
437         numeric nn = ex_to_numeric(n);
438         numeric nx = ex_to_numeric(x);
439         if (nx.is_integer()) {
440             // integer case 
441             if (nx.is_equal(_num1()))
442                 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
443                 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
444             if (nx.is_positive()) {
445                 // use the recurrence relation
446                 //   psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
447                 // to relate psi(n,m) to psi(n,1):
448                 //   psi(n,m) == psi(n,1) + r
449                 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
450                 numeric recur(0);
451                 for (numeric p(1); p<nx; ++p)
452                     recur += pow(p, -nn+_num_1());
453                 recur *= factorial(nn)*pow(_num_1(), nn);
454                 return recur+psi(n,_ex1());
455             } else {
456                 // for non-positive integers there is a pole:
457                 throw (std::domain_error("psi2_eval(): pole"));
458             }
459         }
460         if ((_num2()*nx).is_integer()) {
461             // half integer case
462             if (nx.is_equal(_num1_2()))
463                 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
464                 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
465             if (nx.is_positive()) {
466                 numeric m = nx - _num1_2();
467                 // use the multiplication formula
468                 //   psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
469                 // to revert to positive integer case
470                 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
471             } else {
472                 // use the recurrence relation
473                 //   psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
474                 // to relate psi(n,-m-1/2) to psi(n,1/2):
475                 //   psi(n,-m-1/2) == psi(n,1/2) + r
476                 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
477                 numeric recur(0);
478                 for (numeric p(nx); p<0; ++p)
479                     recur += pow(p, -nn+_num_1());
480                 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
481                 return recur+psi(n,_ex1_2());
482             }
483         }
484         //  psi2_evalf should be called here once it becomes available
485     }
486     
487     return psi(n, x).hold();
488 }    
489
490 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
491 {
492     GINAC_ASSERT(deriv_param<2);
493     
494     if (deriv_param==0) {
495         // d/dn psi(n,x)
496         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
497     }
498     // d/dx psi(n,x) -> psi(n+1,x)
499     return psi(n+_ex1(), x);
500 }
501
502 static ex psi2_series(const ex & n, const ex & x, const relational & rel, int order)
503 {
504     // method:
505     // Taylor series where there is no pole falls back to polygamma function
506     // evaluation.
507     // On a pole at -m use the recurrence relation
508     //   psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
509     // from which follows
510     //   series(psi(x),x==-m,order) == 
511     //   series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
512     //                                      ... + (x+m)^(-n-1))),x==-m,order);
513     const ex x_pt = x.subs(rel);
514     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
515         throw do_taylor();  // caught by function::series()
516     // if we got here we have to care for a pole of order n+1 at -m:
517     numeric m = -ex_to_numeric(x_pt);
518     ex recur;
519     for (numeric p; p<=m; ++p)
520         recur += power(x+p,-n+_ex_1());
521     recur *= factorial(n)*power(_ex_1(),n);
522     return (psi(n, x+m+_ex1())-recur).series(rel, order);
523 }
524
525 const unsigned function_index_psi2 =
526     function::register_new(function_options("psi").
527                            eval_func(psi2_eval).
528                            evalf_func(psi2_evalf).
529                derivative_func(psi2_deriv).
530                            series_func(psi2_series).
531                            overloaded(2));
532
533
534 #ifndef NO_NAMESPACE_GINAC
535 } // namespace GiNaC
536 #endif // ndef NO_NAMESPACE_GINAC