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