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