* Now two different simplification paths in clifford::contract_with().
[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-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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 "operators.h"
34 #include "symbol.h"
35 #include "symmetry.h"
36 #include "utils.h"
37
38 namespace GiNaC {
39
40 //////////
41 // Logarithm of Gamma function
42 //////////
43
44 static ex lgamma_evalf(const ex & x)
45 {
46         if (is_exactly_a<numeric>(x)) {
47                 try {
48                         return lgamma(ex_to<numeric>(x));
49                 } catch (const dunno &e) { }
50         }
51         
52         return lgamma(x).hold();
53 }
54
55
56 /** Evaluation of lgamma(x), the natural logarithm of the Gamma function.
57  *  Knows about integer arguments and that's it.  Somebody ought to provide
58  *  some good numerical evaluation some day...
59  *
60  *  @exception GiNaC::pole_error("lgamma_eval(): logarithmic pole",0) */
61 static ex lgamma_eval(const ex & x)
62 {
63         if (x.info(info_flags::numeric)) {
64                 // trap integer arguments:
65                 if (x.info(info_flags::integer)) {
66                         // lgamma(n) -> log((n-1)!) for postitive n
67                         if (x.info(info_flags::posint))
68                                 return log(factorial(x + _ex_1));
69                         else
70                                 throw (pole_error("lgamma_eval(): logarithmic pole",0));
71                 }
72                 //  lgamma_evalf should be called here once it becomes available
73         }
74         
75         return lgamma(x).hold();
76 }
77
78
79 static ex lgamma_deriv(const ex & x, unsigned deriv_param)
80 {
81         GINAC_ASSERT(deriv_param==0);
82         
83         // d/dx  lgamma(x) -> psi(x)
84         return psi(x);
85 }
86
87
88 static ex lgamma_series(const ex & arg,
89                         const relational & rel,
90                         int order,
91                         unsigned options)
92 {
93         // method:
94         // Taylor series where there is no pole falls back to psi function
95         // evaluation.
96         // On a pole at -m we could use the recurrence relation
97         //   lgamma(x) == lgamma(x+1)-log(x)
98         // from which follows
99         //   series(lgamma(x),x==-m,order) ==
100         //   series(lgamma(x+m+1)-log(x)...-log(x+m)),x==-m,order);
101         const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
102         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
103                 throw do_taylor();  // caught by function::series()
104         // if we got here we have to care for a simple pole of tgamma(-m):
105         numeric m = -ex_to<numeric>(arg_pt);
106         ex recur;
107         for (numeric p = 0; p<=m; ++p)
108                 recur += log(arg+p);
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                           latex_name("\\log \\Gamma"));
118
119
120 //////////
121 // true Gamma function
122 //////////
123
124 static ex tgamma_evalf(const ex & x)
125 {
126         if (is_exactly_a<numeric>(x)) {
127                 try {
128                         return tgamma(ex_to<numeric>(x));
129                 } catch (const dunno &e) { }
130         }
131         
132         return tgamma(x).hold();
133 }
134
135
136 /** Evaluation of tgamma(x), the true Gamma function.  Knows about integer
137  *  arguments, half-integer arguments and that's it. Somebody ought to provide
138  *  some good numerical evaluation some day...
139  *
140  *  @exception pole_error("tgamma_eval(): simple pole",0) */
141 static ex tgamma_eval(const ex & x)
142 {
143         if (x.info(info_flags::numeric)) {
144                 // trap integer arguments:
145                 const numeric two_x = _num2*ex_to<numeric>(x);
146                 if (two_x.is_even()) {
147                         // tgamma(n) -> (n-1)! for postitive n
148                         if (two_x.is_positive()) {
149                                 return factorial(ex_to<numeric>(x).sub(_num1));
150                         } else {
151                                 throw (pole_error("tgamma_eval(): simple pole",1));
152                         }
153                 }
154                 // trap half integer arguments:
155                 if (two_x.is_integer()) {
156                         // trap positive x==(n+1/2)
157                         // tgamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
158                         if (two_x.is_positive()) {
159                                 const numeric n = ex_to<numeric>(x).sub(_num1_2);
160                                 return (doublefactorial(n.mul(_num2).sub(_num1)).div(pow(_num2,n))) * sqrt(Pi);
161                         } else {
162                                 // trap negative x==(-n+1/2)
163                                 // tgamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
164                                 const numeric n = abs(ex_to<numeric>(x).sub(_num1_2));
165                                 return (pow(_num_2, n).div(doublefactorial(n.mul(_num2).sub(_num1))))*sqrt(Pi);
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);
197         const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
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         const 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, 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                           latex_name("\\Gamma"));
214
215
216 //////////
217 // beta-function
218 //////////
219
220 static ex beta_evalf(const ex & x, const ex & y)
221 {
222         if (is_exactly_a<numeric>(x) && is_exactly_a<numeric>(y)) {
223                 try {
224                         return tgamma(ex_to<numeric>(x))*tgamma(ex_to<numeric>(y))/tgamma(ex_to<numeric>(x+y));
225                 } catch (const dunno &e) { }
226         }
227         
228         return beta(x,y).hold();
229 }
230
231
232 static ex beta_eval(const ex & x, const ex & y)
233 {
234         if (x.is_equal(_ex1))
235                 return 1/y;
236         if (y.is_equal(_ex1))
237                 return 1/x;
238         if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
239                 // treat all problematic x and y that may not be passed into tgamma,
240                 // because they would throw there although beta(x,y) is well-defined
241                 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
242                 const numeric &nx = ex_to<numeric>(x);
243                 const numeric &ny = ex_to<numeric>(y);
244                 if (nx.is_real() && nx.is_integer() &&
245                         ny.is_real() && ny.is_integer()) {
246                         if (nx.is_negative()) {
247                                 if (nx<=-ny)
248                                         return pow(_num_1, ny)*beta(1-x-y, y);
249                                 else
250                                         throw (pole_error("beta_eval(): simple pole",1));
251                         }
252                         if (ny.is_negative()) {
253                                 if (ny<=-nx)
254                                         return pow(_num_1, nx)*beta(1-y-x, x);
255                                 else
256                                         throw (pole_error("beta_eval(): simple pole",1));
257                         }
258                         return tgamma(x)*tgamma(y)/tgamma(x+y);
259                 }
260                 // no problem in numerator, but denominator has pole:
261                 if ((nx+ny).is_real() &&
262                     (nx+ny).is_integer() &&
263                    !(nx+ny).is_positive())
264                          return _ex0;
265                 // beta_evalf should be called here once it becomes available
266         }
267         
268         return beta(x,y).hold();
269 }
270
271
272 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
273 {
274         GINAC_ASSERT(deriv_param<2);
275         ex retval;
276         
277         // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
278         if (deriv_param==0)
279                 retval = (psi(x)-psi(x+y))*beta(x,y);
280         // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
281         if (deriv_param==1)
282                 retval = (psi(y)-psi(x+y))*beta(x,y);
283         return retval;
284 }
285
286
287 static ex beta_series(const ex & arg1,
288                       const ex & arg2,
289                       const relational & rel,
290                       int order,
291                       unsigned options)
292 {
293         // method:
294         // Taylor series where there is no pole of one of the tgamma functions
295         // falls back to beta function evaluation.  Otherwise, fall back to
296         // tgamma series directly.
297         const ex arg1_pt = arg1.subs(rel, subs_options::no_pattern);
298         const ex arg2_pt = arg2.subs(rel, subs_options::no_pattern);
299         GINAC_ASSERT(is_a<symbol>(rel.lhs()));
300         const symbol &s = ex_to<symbol>(rel.lhs());
301         ex arg1_ser, arg2_ser, arg1arg2_ser;
302         if ((!arg1_pt.info(info_flags::integer) || arg1_pt.info(info_flags::positive)) &&
303             (!arg2_pt.info(info_flags::integer) || arg2_pt.info(info_flags::positive)))
304                 throw do_taylor();  // caught by function::series()
305         // trap the case where arg1 is on a pole:
306         if (arg1.info(info_flags::integer) && !arg1.info(info_flags::positive))
307                 arg1_ser = tgamma(arg1+s);
308         else
309                 arg1_ser = tgamma(arg1);
310         // trap the case where arg2 is on a pole:
311         if (arg2.info(info_flags::integer) && !arg2.info(info_flags::positive))
312                 arg2_ser = tgamma(arg2+s);
313         else
314                 arg2_ser = tgamma(arg2);
315         // trap the case where arg1+arg2 is on a pole:
316         if ((arg1+arg2).info(info_flags::integer) && !(arg1+arg2).info(info_flags::positive))
317                 arg1arg2_ser = tgamma(arg2+arg1+s);
318         else
319                 arg1arg2_ser = tgamma(arg2+arg1);
320         // compose the result (expanding all the terms):
321         return (arg1_ser*arg2_ser/arg1arg2_ser).series(rel, order, options).expand();
322 }
323
324
325 REGISTER_FUNCTION(beta, eval_func(beta_eval).
326                         evalf_func(beta_evalf).
327                         derivative_func(beta_deriv).
328                         series_func(beta_series).
329                         latex_name("\\mbox{B}").
330                                                 set_symmetry(sy_symm(0, 1)));
331
332
333 //////////
334 // Psi-function (aka digamma-function)
335 //////////
336
337 static ex psi1_evalf(const ex & x)
338 {
339         if (is_exactly_a<numeric>(x)) {
340                 try {
341                         return psi(ex_to<numeric>(x));
342                 } catch (const dunno &e) { }
343         }
344         
345         return psi(x).hold();
346 }
347
348 /** Evaluation of digamma-function psi(x).
349  *  Somebody ought to provide some good numerical evaluation some day... */
350 static ex psi1_eval(const ex & x)
351 {
352         if (x.info(info_flags::numeric)) {
353                 const numeric &nx = ex_to<numeric>(x);
354                 if (nx.is_integer()) {
355                         // integer case 
356                         if (nx.is_positive()) {
357                                 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
358                                 numeric rat = 0;
359                                 for (numeric i(nx+_num_1); i>0; --i)
360                                         rat += i.inverse();
361                                 return rat-Euler;
362                         } else {
363                                 // for non-positive integers there is a pole:
364                                 throw (pole_error("psi_eval(): simple pole",1));
365                         }
366                 }
367                 if ((_num2*nx).is_integer()) {
368                         // half integer case
369                         if (nx.is_positive()) {
370                                 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
371                                 numeric rat = 0;
372                                 for (numeric i = (nx+_num_1)*_num2; i>0; i-=_num2)
373                                         rat += _num2*i.inverse();
374                                 return rat-Euler-_ex2*log(_ex2);
375                         } else {
376                                 // use the recurrence relation
377                                 //   psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
378                                 // to relate psi(-m-1/2) to psi(1/2):
379                                 //   psi(-m-1/2) == psi(1/2) + r
380                                 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
381                                 numeric recur = 0;
382                                 for (numeric p = nx; p<0; ++p)
383                                         recur -= pow(p, _num_1);
384                                 return recur+psi(_ex1_2);
385                         }
386                 }
387                 //  psi1_evalf should be called here once it becomes available
388         }
389         
390         return psi(x).hold();
391 }
392
393 static ex psi1_deriv(const ex & x, unsigned deriv_param)
394 {
395         GINAC_ASSERT(deriv_param==0);
396         
397         // d/dx psi(x) -> psi(1,x)
398         return psi(_ex1, x);
399 }
400
401 static ex psi1_series(const ex & arg,
402                       const relational & rel,
403                       int order,
404                       unsigned options)
405 {
406         // method:
407         // Taylor series where there is no pole falls back to polygamma function
408         // evaluation.
409         // On a pole at -m use the recurrence relation
410         //   psi(x) == psi(x+1) - 1/z
411         // from which follows
412         //   series(psi(x),x==-m,order) ==
413         //   series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
414         const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
415         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
416                 throw do_taylor();  // caught by function::series()
417         // if we got here we have to care for a simple pole at -m:
418         const numeric m = -ex_to<numeric>(arg_pt);
419         ex recur;
420         for (numeric p; p<=m; ++p)
421                 recur += power(arg+p,_ex_1);
422         return (psi(arg+m+_ex1)-recur).series(rel, order, options);
423 }
424
425 unsigned psi1_SERIAL::serial =
426         function::register_new(function_options("psi", 1).
427                                eval_func(psi1_eval).
428                                evalf_func(psi1_evalf).
429                                derivative_func(psi1_deriv).
430                                series_func(psi1_series).
431                                latex_name("\\psi").
432                                overloaded(2));
433
434 //////////
435 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
436 //////////
437
438 static ex psi2_evalf(const ex & n, const ex & x)
439 {
440         if (is_exactly_a<numeric>(n) && is_exactly_a<numeric>(x)) {
441                 try {
442                         return psi(ex_to<numeric>(n),ex_to<numeric>(x));
443                 } catch (const dunno &e) { }
444         }
445         
446         return psi(n,x).hold();
447 }
448
449 /** Evaluation of polygamma-function psi(n,x). 
450  *  Somebody ought to provide some good numerical evaluation some day... */
451 static ex psi2_eval(const ex & n, const ex & x)
452 {
453         // psi(0,x) -> psi(x)
454         if (n.is_zero())
455                 return psi(x);
456         // psi(-1,x) -> log(tgamma(x))
457         if (n.is_equal(_ex_1))
458                 return log(tgamma(x));
459         if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
460                 x.info(info_flags::numeric)) {
461                 const numeric &nn = ex_to<numeric>(n);
462                 const numeric &nx = ex_to<numeric>(x);
463                 if (nx.is_integer()) {
464                         // integer case 
465                         if (nx.is_equal(_num1))
466                                 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
467                                 return pow(_num_1,nn+_num1)*factorial(nn)*zeta(ex(nn+_num1));
468                         if (nx.is_positive()) {
469                                 // use the recurrence relation
470                                 //   psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
471                                 // to relate psi(n,m) to psi(n,1):
472                                 //   psi(n,m) == psi(n,1) + r
473                                 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
474                                 numeric recur = 0;
475                                 for (numeric p = 1; p<nx; ++p)
476                                         recur += pow(p, -nn+_num_1);
477                                 recur *= factorial(nn)*pow(_num_1, nn);
478                                 return recur+psi(n,_ex1);
479                         } else {
480                                 // for non-positive integers there is a pole:
481                                 throw (pole_error("psi2_eval(): pole",1));
482                         }
483                 }
484                 if ((_num2*nx).is_integer()) {
485                         // half integer case
486                         if (nx.is_equal(_num1_2))
487                                 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
488                                 return pow(_num_1,nn+_num1)*factorial(nn)*(pow(_num2,nn+_num1) + _num_1)*zeta(ex(nn+_num1));
489                         if (nx.is_positive()) {
490                                 const numeric m = nx - _num1_2;
491                                 // use the multiplication formula
492                                 //   psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
493                                 // to revert to positive integer case
494                                 return psi(n,_num2*m)*pow(_num2,nn+_num1)-psi(n,m);
495                         } else {
496                                 // use the recurrence relation
497                                 //   psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
498                                 // to relate psi(n,-m-1/2) to psi(n,1/2):
499                                 //   psi(n,-m-1/2) == psi(n,1/2) + r
500                                 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
501                                 numeric recur = 0;
502                                 for (numeric p = nx; p<0; ++p)
503                                         recur += pow(p, -nn+_num_1);
504                                 recur *= factorial(nn)*pow(_num_1, nn+_num_1);
505                                 return recur+psi(n,_ex1_2);
506                         }
507                 }
508                 //  psi2_evalf should be called here once it becomes available
509         }
510         
511         return psi(n, x).hold();
512 }    
513
514 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
515 {
516         GINAC_ASSERT(deriv_param<2);
517         
518         if (deriv_param==0) {
519                 // d/dn psi(n,x)
520                 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
521         }
522         // d/dx psi(n,x) -> psi(n+1,x)
523         return psi(n+_ex1, x);
524 }
525
526 static ex psi2_series(const ex & n,
527                       const ex & arg,
528                       const relational & rel,
529                       int order,
530                       unsigned options)
531 {
532         // method:
533         // Taylor series where there is no pole falls back to polygamma function
534         // evaluation.
535         // On a pole at -m use the recurrence relation
536         //   psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
537         // from which follows
538         //   series(psi(x),x==-m,order) == 
539         //   series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
540         //                                      ... + (x+m)^(-n-1))),x==-m,order);
541         const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
542         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
543                 throw do_taylor();  // caught by function::series()
544         // if we got here we have to care for a pole of order n+1 at -m:
545         const numeric m = -ex_to<numeric>(arg_pt);
546         ex recur;
547         for (numeric p; p<=m; ++p)
548                 recur += power(arg+p,-n+_ex_1);
549         recur *= factorial(n)*power(_ex_1,n);
550         return (psi(n, arg+m+_ex1)-recur).series(rel, order, options);
551 }
552
553 unsigned psi2_SERIAL::serial =
554         function::register_new(function_options("psi", 2).
555                                eval_func(psi2_eval).
556                                evalf_func(psi2_evalf).
557                                derivative_func(psi2_deriv).
558                                series_func(psi2_series).
559                                latex_name("\\psi").
560                                overloaded(2));
561
562
563 } // namespace GiNaC