4377efdefa73c0c2d17c87746e0a57c3220854a5
[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 = 0; 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                 const numeric two_x = _num2()*ex_to<numeric>(x);
141                 if (two_x.is_even()) {
142                         // tgamma(n) -> (n-1)! for postitive n
143                         if (two_x.is_positive()) {
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 (two_x.is_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 (two_x.is_positive()) {
154                                 const numeric n = ex_to<numeric>(x).sub(_num1_2());
155                                 return (doublefactorial(n.mul(_num2()).sub(_num1())).div(pow(_num2(),n))) * 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                                 const numeric n = abs(ex_to<numeric>(x).sub(_num1_2()));
160                                 return (pow(_num_2(), n).div(doublefactorial(n.mul(_num2()).sub(_num1()))))*power(Pi,_ex1_2());
161                         }
162                 }
163                 //  tgamma_evalf should be called here once it becomes available
164         }
165         
166         return tgamma(x).hold();
167 }
168
169
170 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
171 {
172         GINAC_ASSERT(deriv_param==0);
173         
174         // d/dx  tgamma(x) -> psi(x)*tgamma(x)
175         return psi(x)*tgamma(x);
176 }
177
178
179 static ex tgamma_series(const ex & arg,
180                         const relational & rel,
181                         int order,
182                         unsigned options)
183 {
184         // method:
185         // Taylor series where there is no pole falls back to psi function
186         // evaluation.
187         // On a pole at -m use the recurrence relation
188         //   tgamma(x) == tgamma(x+1) / x
189         // from which follows
190         //   series(tgamma(x),x==-m,order) ==
191         //   series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x==-m,order+1);
192         const ex arg_pt = arg.subs(rel);
193         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
194                 throw do_taylor();  // caught by function::series()
195         // if we got here we have to care for a simple pole at -m:
196         const numeric m = -ex_to<numeric>(arg_pt);
197         ex ser_denom = _ex1();
198         for (numeric p; p<=m; ++p)
199                 ser_denom *= arg+p;
200         return (tgamma(arg+m+_ex1())/ser_denom).series(rel, order+1, options);
201 }
202
203
204 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
205                           evalf_func(tgamma_evalf).
206                           derivative_func(tgamma_deriv).
207                           series_func(tgamma_series).
208                           latex_name("\\Gamma"));
209
210
211 //////////
212 // beta-function
213 //////////
214
215 static ex beta_evalf(const ex & x, const ex & y)
216 {
217         BEGIN_TYPECHECK
218                 TYPECHECK(x,numeric)
219                 TYPECHECK(y,numeric)
220         END_TYPECHECK(beta(x,y))
221         
222         return tgamma(ex_to<numeric>(x))*tgamma(ex_to<numeric>(y))/tgamma(ex_to<numeric>(x+y));
223 }
224
225
226 static ex beta_eval(const ex & x, const ex & y)
227 {
228         if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
229                 // treat all problematic x and y that may not be passed into tgamma,
230                 // because they would throw there although beta(x,y) is well-defined
231                 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
232                 const numeric nx = ex_to<numeric>(x);
233                 const numeric ny = ex_to<numeric>(y);
234                 if (nx.is_real() && nx.is_integer() &&
235                         ny.is_real() && ny.is_integer()) {
236                         if (nx.is_negative()) {
237                                 if (nx<=-ny)
238                                         return pow(_num_1(), ny)*beta(1-x-y, y);
239                                 else
240                                         throw (pole_error("beta_eval(): simple pole",1));
241                         }
242                         if (ny.is_negative()) {
243                                 if (ny<=-nx)
244                                         return pow(_num_1(), nx)*beta(1-y-x, x);
245                                 else
246                                         throw (pole_error("beta_eval(): simple pole",1));
247                         }
248                         return tgamma(x)*tgamma(y)/tgamma(x+y);
249                 }
250                 // no problem in numerator, but denominator has pole:
251                 if ((nx+ny).is_real() &&
252                         (nx+ny).is_integer() &&
253                         !(nx+ny).is_positive())
254                          return _ex0();
255                 // everything is ok:
256                 return tgamma(x)*tgamma(y)/tgamma(x+y);
257         }
258         
259         return beta(x,y).hold();
260 }
261
262
263 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
264 {
265         GINAC_ASSERT(deriv_param<2);
266         ex retval;
267         
268         // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
269         if (deriv_param==0)
270                 retval = (psi(x)-psi(x+y))*beta(x,y);
271         // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
272         if (deriv_param==1)
273                 retval = (psi(y)-psi(x+y))*beta(x,y);
274         return retval;
275 }
276
277
278 static ex beta_series(const ex & arg1,
279                       const ex & arg2,
280                       const relational & rel,
281                       int order,
282                       unsigned options)
283 {
284         // method:
285         // Taylor series where there is no pole of one of the tgamma functions
286         // falls back to beta function evaluation.  Otherwise, fall back to
287         // tgamma series directly.
288         const ex arg1_pt = arg1.subs(rel);
289         const ex arg2_pt = arg2.subs(rel);
290         GINAC_ASSERT(is_ex_exactly_of_type(rel.lhs(),symbol));
291         const symbol *s = static_cast<symbol *>(rel.lhs().bp);
292         ex arg1_ser, arg2_ser, arg1arg2_ser;
293         if ((!arg1_pt.info(info_flags::integer) || arg1_pt.info(info_flags::positive)) &&
294             (!arg2_pt.info(info_flags::integer) || arg2_pt.info(info_flags::positive)))
295                 throw do_taylor();  // caught by function::series()
296         // trap the case where arg1 is on a pole:
297         if (arg1.info(info_flags::integer) && !arg1.info(info_flags::positive))
298                 arg1_ser = tgamma(arg1+*s).series(rel, order, options);
299         else
300                 arg1_ser = tgamma(arg1).series(rel,order);
301         // trap the case where arg2 is on a pole:
302         if (arg2.info(info_flags::integer) && !arg2.info(info_flags::positive))
303                 arg2_ser = tgamma(arg2+*s).series(rel, order, options);
304         else
305                 arg2_ser = tgamma(arg2).series(rel,order);
306         // trap the case where arg1+arg2 is on a pole:
307         if ((arg1+arg2).info(info_flags::integer) && !(arg1+arg2).info(info_flags::positive))
308                 arg1arg2_ser = tgamma(arg2+arg1+*s).series(rel, order, options);
309         else
310                 arg1arg2_ser = tgamma(arg2+arg1).series(rel,order);
311         // compose the result (expanding all the terms):
312         return (arg1_ser*arg2_ser/arg1arg2_ser).series(rel, order, options).expand();
313 }
314
315
316 REGISTER_FUNCTION(beta, eval_func(beta_eval).
317                         evalf_func(beta_evalf).
318                         derivative_func(beta_deriv).
319                         series_func(beta_series).
320                         latex_name("\\mbox{B}").
321                                                 set_symmetry(sy_symm(0, 1)));
322
323
324 //////////
325 // Psi-function (aka digamma-function)
326 //////////
327
328 static ex psi1_evalf(const ex & x)
329 {
330         BEGIN_TYPECHECK
331                 TYPECHECK(x,numeric)
332         END_TYPECHECK(psi(x))
333         
334         return psi(ex_to<numeric>(x));
335 }
336
337 /** Evaluation of digamma-function psi(x).
338  *  Somebody ought to provide some good numerical evaluation some day... */
339 static ex psi1_eval(const ex & x)
340 {
341         if (x.info(info_flags::numeric)) {
342                 const numeric nx = ex_to<numeric>(x);
343                 if (nx.is_integer()) {
344                         // integer case 
345                         if (nx.is_positive()) {
346                                 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
347                                 numeric rat = 0;
348                                 for (numeric i(nx+_num_1()); i>0; --i)
349                                         rat += i.inverse();
350                                 return rat-Euler;
351                         } else {
352                                 // for non-positive integers there is a pole:
353                                 throw (pole_error("psi_eval(): simple pole",1));
354                         }
355                 }
356                 if ((_num2()*nx).is_integer()) {
357                         // half integer case
358                         if (nx.is_positive()) {
359                                 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
360                                 numeric rat = 0;
361                                 for (numeric i = (nx+_num_1())*_num2(); i>0; i-=_num2())
362                                         rat += _num2()*i.inverse();
363                                 return rat-Euler-_ex2()*log(_ex2());
364                         } else {
365                                 // use the recurrence relation
366                                 //   psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
367                                 // to relate psi(-m-1/2) to psi(1/2):
368                                 //   psi(-m-1/2) == psi(1/2) + r
369                                 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
370                                 numeric recur = 0;
371                                 for (numeric p = nx; p<0; ++p)
372                                         recur -= pow(p, _num_1());
373                                 return recur+psi(_ex1_2());
374                         }
375                 }
376                 //  psi1_evalf should be called here once it becomes available
377         }
378         
379         return psi(x).hold();
380 }
381
382 static ex psi1_deriv(const ex & x, unsigned deriv_param)
383 {
384         GINAC_ASSERT(deriv_param==0);
385         
386         // d/dx psi(x) -> psi(1,x)
387         return psi(_ex1(), x);
388 }
389
390 static ex psi1_series(const ex & arg,
391                       const relational & rel,
392                       int order,
393                       unsigned options)
394 {
395         // method:
396         // Taylor series where there is no pole falls back to polygamma function
397         // evaluation.
398         // On a pole at -m use the recurrence relation
399         //   psi(x) == psi(x+1) - 1/z
400         // from which follows
401         //   series(psi(x),x==-m,order) ==
402         //   series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
403         const ex arg_pt = arg.subs(rel);
404         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
405                 throw do_taylor();  // caught by function::series()
406         // if we got here we have to care for a simple pole at -m:
407         const numeric m = -ex_to<numeric>(arg_pt);
408         ex recur;
409         for (numeric p; p<=m; ++p)
410                 recur += power(arg+p,_ex_1());
411         return (psi(arg+m+_ex1())-recur).series(rel, order, options);
412 }
413
414 const unsigned function_index_psi1 =
415         function::register_new(function_options("psi").
416                                eval_func(psi1_eval).
417                                evalf_func(psi1_evalf).
418                                derivative_func(psi1_deriv).
419                                series_func(psi1_series).
420                                latex_name("\\psi").
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                 const numeric nn = ex_to<numeric>(n);
450                 const 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                                 const 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         const 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                                latex_name("\\psi").
548                                overloaded(2));
549
550
551 } // namespace GiNaC