5e1b1eb97fb59d14efe2ab36680aafb0f244ae4e
[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-2001 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 "constant.h"
29 #include "pseries.h"
30 #include "numeric.h"
31 #include "power.h"
32 #include "relational.h"
33 #include "symbol.h"
34 #include "symmetry.h"
35 #include "utils.h"
36
37 namespace GiNaC {
38
39 //////////
40 // Logarithm of Gamma function
41 //////////
42
43 static ex lgamma_evalf(const ex & x)
44 {
45         BEGIN_TYPECHECK
46                 TYPECHECK(x,numeric)
47         END_TYPECHECK(lgamma(x))
48         
49         return lgamma(ex_to<numeric>(x));
50 }
51
52
53 /** Evaluation of lgamma(x), the natural logarithm of the Gamma function.
54  *  Knows about integer arguments and that's it.  Somebody ought to provide
55  *  some good numerical evaluation some day...
56  *
57  *  @exception GiNaC::pole_error("lgamma_eval(): logarithmic pole",0) */
58 static ex lgamma_eval(const ex & x)
59 {
60         if (x.info(info_flags::numeric)) {
61                 // trap integer arguments:
62                 if (x.info(info_flags::integer)) {
63                         // lgamma(n) -> log((n-1)!) for postitive n
64                         if (x.info(info_flags::posint))
65                                 return log(factorial(x + _ex_1()));
66                         else
67                                 throw (pole_error("lgamma_eval(): logarithmic pole",0));
68                 }
69                 //  lgamma_evalf should be called here once it becomes available
70         }
71         
72         return lgamma(x).hold();
73 }
74
75
76 static ex lgamma_deriv(const ex & x, unsigned deriv_param)
77 {
78         GINAC_ASSERT(deriv_param==0);
79         
80         // d/dx  lgamma(x) -> psi(x)
81         return psi(x);
82 }
83
84
85 static ex lgamma_series(const ex & arg,
86                         const relational & rel,
87                         int order,
88                         unsigned options)
89 {
90         // method:
91         // Taylor series where there is no pole falls back to psi function
92         // evaluation.
93         // On a pole at -m we could 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);
98         const ex arg_pt = arg.subs(rel);
99         if (!arg_pt.info(info_flags::integer) || arg_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 of tgamma(-m):
102         numeric m = -ex_to<numeric>(arg_pt);
103         ex recur;
104         for (numeric p; p<=m; ++p)
105                 recur += log(arg+p);
106         return (lgamma(arg+m+_ex1())-recur).series(rel, order, options);
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                           latex_name("\\log \\Gamma"));
115
116
117 //////////
118 // true Gamma function
119 //////////
120
121 static ex tgamma_evalf(const ex & x)
122 {
123         BEGIN_TYPECHECK
124                 TYPECHECK(x,numeric)
125         END_TYPECHECK(tgamma(x))
126         
127         return tgamma(ex_to<numeric>(x));
128 }
129
130
131 /** Evaluation of tgamma(x), the true Gamma function.  Knows about integer
132  *  arguments, half-integer arguments and that's it. Somebody ought to provide
133  *  some good numerical evaluation some day...
134  *
135  *  @exception pole_error("tgamma_eval(): simple pole",0) */
136 static ex tgamma_eval(const ex & x)
137 {
138         if (x.info(info_flags::numeric)) {
139                 // trap integer arguments:
140                 if (x.info(info_flags::integer)) {
141                         // tgamma(n) -> (n-1)! for postitive n
142                         if (x.info(info_flags::posint)) {
143                                 return factorial(ex_to<numeric>(x).sub(_num1()));
144                         } else {
145                                 throw (pole_error("tgamma_eval(): simple pole",1));
146                         }
147                 }
148                 // trap half integer arguments:
149                 if ((x*2).info(info_flags::integer)) {
150                         // trap positive x==(n+1/2)
151                         // tgamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
152                         if ((x*_ex2()).info(info_flags::posint)) {
153                                 numeric n = ex_to<numeric>(x).sub(_num1_2());
154                                 numeric coefficient = doublefactorial(n.mul(_num2()).sub(_num1()));
155                                 coefficient = coefficient.div(pow(_num2(),n));
156                                 return coefficient * pow(Pi,_ex1_2());
157                         } else {
158                                 // trap negative x==(-n+1/2)
159                                 // tgamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
160                                 numeric n = abs(ex_to<numeric>(x).sub(_num1_2()));
161                                 numeric coefficient = pow(_num_2(), n);
162                                 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
163                                 return coefficient*power(Pi,_ex1_2());
164                         }
165                 }
166                 //  tgamma_evalf should be called here once it becomes available
167         }
168         
169         return tgamma(x).hold();
170 }
171
172
173 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
174 {
175         GINAC_ASSERT(deriv_param==0);
176         
177         // d/dx  tgamma(x) -> psi(x)*tgamma(x)
178         return psi(x)*tgamma(x);
179 }
180
181
182 static ex tgamma_series(const ex & arg,
183                         const relational & rel,
184                         int order,
185                         unsigned options)
186 {
187         // method:
188         // Taylor series where there is no pole falls back to psi function
189         // evaluation.
190         // On a pole at -m use the recurrence relation
191         //   tgamma(x) == tgamma(x+1) / x
192         // from which follows
193         //   series(tgamma(x),x==-m,order) ==
194         //   series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x==-m,order+1);
195         const ex arg_pt = arg.subs(rel);
196         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
197                 throw do_taylor();  // caught by function::series()
198         // if we got here we have to care for a simple pole at -m:
199         numeric m = -ex_to<numeric>(arg_pt);
200         ex ser_denom = _ex1();
201         for (numeric p; p<=m; ++p)
202                 ser_denom *= arg+p;
203         return (tgamma(arg+m+_ex1())/ser_denom).series(rel, order+1, options);
204 }
205
206
207 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
208                           evalf_func(tgamma_evalf).
209                           derivative_func(tgamma_deriv).
210                           series_func(tgamma_series).
211                           latex_name("\\Gamma"));
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                         latex_name("\\mbox{B}").
324                                                 set_symmetry(sy_symm(0, 1)));
325
326
327 //////////
328 // Psi-function (aka digamma-function)
329 //////////
330
331 static ex psi1_evalf(const ex & x)
332 {
333         BEGIN_TYPECHECK
334                 TYPECHECK(x,numeric)
335         END_TYPECHECK(psi(x))
336         
337         return psi(ex_to<numeric>(x));
338 }
339
340 /** Evaluation of digamma-function psi(x).
341  *  Somebody ought to provide some good numerical evaluation some day... */
342 static ex psi1_eval(const ex & x)
343 {
344         if (x.info(info_flags::numeric)) {
345                 numeric nx = ex_to<numeric>(x);
346                 if (nx.is_integer()) {
347                         // integer case 
348                         if (nx.is_positive()) {
349                                 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
350                                 numeric rat(0);
351                                 for (numeric i(nx+_num_1()); i.is_positive(); --i)
352                                         rat += i.inverse();
353                                 return rat-Euler;
354                         } else {
355                                 // for non-positive integers there is a pole:
356                                 throw (pole_error("psi_eval(): simple pole",1));
357                         }
358                 }
359                 if ((_num2()*nx).is_integer()) {
360                         // half integer case
361                         if (nx.is_positive()) {
362                                 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
363                                 numeric rat(0);
364                                 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
365                                         rat += _num2()*i.inverse();
366                                 return rat-Euler-_ex2()*log(_ex2());
367                         } else {
368                                 // use the recurrence relation
369                                 //   psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
370                                 // to relate psi(-m-1/2) to psi(1/2):
371                                 //   psi(-m-1/2) == psi(1/2) + r
372                                 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
373                                 numeric recur(0);
374                                 for (numeric p(nx); p<0; ++p)
375                                         recur -= pow(p, _num_1());
376                                 return recur+psi(_ex1_2());
377                         }
378                 }
379                 //  psi1_evalf should be called here once it becomes available
380         }
381         
382         return psi(x).hold();
383 }
384
385 static ex psi1_deriv(const ex & x, unsigned deriv_param)
386 {
387         GINAC_ASSERT(deriv_param==0);
388         
389         // d/dx psi(x) -> psi(1,x)
390         return psi(_ex1(), x);
391 }
392
393 static ex psi1_series(const ex & arg,
394                       const relational & rel,
395                       int order,
396                       unsigned options)
397 {
398         // method:
399         // Taylor series where there is no pole falls back to polygamma function
400         // evaluation.
401         // On a pole at -m use the recurrence relation
402         //   psi(x) == psi(x+1) - 1/z
403         // from which follows
404         //   series(psi(x),x==-m,order) ==
405         //   series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
406         const ex arg_pt = arg.subs(rel);
407         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
408                 throw do_taylor();  // caught by function::series()
409         // if we got here we have to care for a simple pole at -m:
410         numeric m = -ex_to<numeric>(arg_pt);
411         ex recur;
412         for (numeric p; p<=m; ++p)
413                 recur += power(arg+p,_ex_1());
414         return (psi(arg+m+_ex1())-recur).series(rel, order, options);
415 }
416
417 const unsigned function_index_psi1 =
418         function::register_new(function_options("psi").
419                                eval_func(psi1_eval).
420                                evalf_func(psi1_evalf).
421                                derivative_func(psi1_deriv).
422                                series_func(psi1_series).
423                                latex_name("\\psi").
424                                overloaded(2));
425
426 //////////
427 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
428 //////////
429
430 static ex psi2_evalf(const ex & n, const ex & x)
431 {
432         BEGIN_TYPECHECK
433                 TYPECHECK(n,numeric)
434                 TYPECHECK(x,numeric)
435         END_TYPECHECK(psi(n,x))
436         
437         return psi(ex_to<numeric>(n), ex_to<numeric>(x));
438 }
439
440 /** Evaluation of polygamma-function psi(n,x). 
441  *  Somebody ought to provide some good numerical evaluation some day... */
442 static ex psi2_eval(const ex & n, const ex & x)
443 {
444         // psi(0,x) -> psi(x)
445         if (n.is_zero())
446                 return psi(x);
447         // psi(-1,x) -> log(tgamma(x))
448         if (n.is_equal(_ex_1()))
449                 return log(tgamma(x));
450         if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
451                 x.info(info_flags::numeric)) {
452                 numeric nn = ex_to<numeric>(n);
453                 numeric nx = ex_to<numeric>(x);
454                 if (nx.is_integer()) {
455                         // integer case 
456                         if (nx.is_equal(_num1()))
457                                 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
458                                 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
459                         if (nx.is_positive()) {
460                                 // use the recurrence relation
461                                 //   psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
462                                 // to relate psi(n,m) to psi(n,1):
463                                 //   psi(n,m) == psi(n,1) + r
464                                 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
465                                 numeric recur(0);
466                                 for (numeric p(1); p<nx; ++p)
467                                         recur += pow(p, -nn+_num_1());
468                                 recur *= factorial(nn)*pow(_num_1(), nn);
469                                 return recur+psi(n,_ex1());
470                         } else {
471                                 // for non-positive integers there is a pole:
472                                 throw (pole_error("psi2_eval(): pole",1));
473                         }
474                 }
475                 if ((_num2()*nx).is_integer()) {
476                         // half integer case
477                         if (nx.is_equal(_num1_2()))
478                                 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
479                                 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
480                         if (nx.is_positive()) {
481                                 numeric m = nx - _num1_2();
482                                 // use the multiplication formula
483                                 //   psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
484                                 // to revert to positive integer case
485                                 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
486                         } else {
487                                 // use the recurrence relation
488                                 //   psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
489                                 // to relate psi(n,-m-1/2) to psi(n,1/2):
490                                 //   psi(n,-m-1/2) == psi(n,1/2) + r
491                                 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
492                                 numeric recur(0);
493                                 for (numeric p(nx); p<0; ++p)
494                                         recur += pow(p, -nn+_num_1());
495                                 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
496                                 return recur+psi(n,_ex1_2());
497                         }
498                 }
499                 //  psi2_evalf should be called here once it becomes available
500         }
501         
502         return psi(n, x).hold();
503 }    
504
505 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
506 {
507         GINAC_ASSERT(deriv_param<2);
508         
509         if (deriv_param==0) {
510                 // d/dn psi(n,x)
511                 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
512         }
513         // d/dx psi(n,x) -> psi(n+1,x)
514         return psi(n+_ex1(), x);
515 }
516
517 static ex psi2_series(const ex & n,
518                       const ex & arg,
519                       const relational & rel,
520                       int order,
521                       unsigned options)
522 {
523         // method:
524         // Taylor series where there is no pole falls back to polygamma function
525         // evaluation.
526         // On a pole at -m use the recurrence relation
527         //   psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
528         // from which follows
529         //   series(psi(x),x==-m,order) == 
530         //   series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
531         //                                      ... + (x+m)^(-n-1))),x==-m,order);
532         const ex arg_pt = arg.subs(rel);
533         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
534                 throw do_taylor();  // caught by function::series()
535         // if we got here we have to care for a pole of order n+1 at -m:
536         numeric m = -ex_to<numeric>(arg_pt);
537         ex recur;
538         for (numeric p; p<=m; ++p)
539                 recur += power(arg+p,-n+_ex_1());
540         recur *= factorial(n)*power(_ex_1(),n);
541         return (psi(n, arg+m+_ex1())-recur).series(rel, order, options);
542 }
543
544 const unsigned function_index_psi2 =
545         function::register_new(function_options("psi").
546                                eval_func(psi2_eval).
547                                evalf_func(psi2_evalf).
548                                derivative_func(psi2_deriv).
549                                series_func(psi2_series).
550                                latex_name("\\psi").
551                                overloaded(2));
552
553
554 } // namespace GiNaC