]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
pgcd(), chinrem_gcd(): use appropriate definition of the degree.
[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-2010 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 "inifcns.h"
25 #include "constant.h"
26 #include "pseries.h"
27 #include "numeric.h"
28 #include "power.h"
29 #include "relational.h"
30 #include "operators.h"
31 #include "symbol.h"
32 #include "symmetry.h"
33 #include "utils.h"
34
35 #include <stdexcept>
36 #include <vector>
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  *  Handles integer arguments as a special case.
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 + _ex_1));
68                         else
69                                 throw (pole_error("lgamma_eval(): logarithmic pole",0));
70                 }
71                 if (!ex_to<numeric>(x).is_rational())
72                         return lgamma(ex_to<numeric>(x));
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_p)*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_p));
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_p);
160                                 return (doublefactorial(n.mul(*_num2_p).sub(*_num1_p)).div(pow(*_num2_p,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_p));
165                                 return (pow(*_num_2_p, n).div(doublefactorial(n.mul(*_num2_p).sub(*_num1_p))))*sqrt(Pi);
166                         }
167                 }
168                 if (!ex_to<numeric>(x).is_rational())
169                         return tgamma(ex_to<numeric>(x));
170         }
171         
172         return tgamma(x).hold();
173 }
174
175
176 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
177 {
178         GINAC_ASSERT(deriv_param==0);
179         
180         // d/dx  tgamma(x) -> psi(x)*tgamma(x)
181         return psi(x)*tgamma(x);
182 }
183
184
185 static ex tgamma_series(const ex & arg,
186                         const relational & rel,
187                         int order,
188                         unsigned options)
189 {
190         // method:
191         // Taylor series where there is no pole falls back to psi function
192         // evaluation.
193         // On a pole at -m use the recurrence relation
194         //   tgamma(x) == tgamma(x+1) / x
195         // from which follows
196         //   series(tgamma(x),x==-m,order) ==
197         //   series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x==-m,order);
198         const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
199         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
200                 throw do_taylor();  // caught by function::series()
201         // if we got here we have to care for a simple pole at -m:
202         const numeric m = -ex_to<numeric>(arg_pt);
203         ex ser_denom = _ex1;
204         for (numeric p; p<=m; ++p)
205                 ser_denom *= arg+p;
206         return (tgamma(arg+m+_ex1)/ser_denom).series(rel, order, options);
207 }
208
209
210 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
211                           evalf_func(tgamma_evalf).
212                           derivative_func(tgamma_deriv).
213                           series_func(tgamma_series).
214                           latex_name("\\Gamma"));
215
216
217 //////////
218 // beta-function
219 //////////
220
221 static ex beta_evalf(const ex & x, const ex & y)
222 {
223         if (is_exactly_a<numeric>(x) && is_exactly_a<numeric>(y)) {
224                 try {
225                         return exp(lgamma(ex_to<numeric>(x))+lgamma(ex_to<numeric>(y))-lgamma(ex_to<numeric>(x+y)));
226                 } catch (const dunno &e) { }
227         }
228         
229         return beta(x,y).hold();
230 }
231
232
233 static ex beta_eval(const ex & x, const ex & y)
234 {
235         if (x.is_equal(_ex1))
236                 return 1/y;
237         if (y.is_equal(_ex1))
238                 return 1/x;
239         if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
240                 // treat all problematic x and y that may not be passed into tgamma,
241                 // because they would throw there although beta(x,y) is well-defined
242                 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
243                 const numeric &nx = ex_to<numeric>(x);
244                 const numeric &ny = ex_to<numeric>(y);
245                 if (nx.is_real() && nx.is_integer() &&
246                         ny.is_real() && ny.is_integer()) {
247                         if (nx.is_negative()) {
248                                 if (nx<=-ny)
249                                         return pow(*_num_1_p, ny)*beta(1-x-y, y);
250                                 else
251                                         throw (pole_error("beta_eval(): simple pole",1));
252                         }
253                         if (ny.is_negative()) {
254                                 if (ny<=-nx)
255                                         return pow(*_num_1_p, nx)*beta(1-y-x, x);
256                                 else
257                                         throw (pole_error("beta_eval(): simple pole",1));
258                         }
259                         return tgamma(x)*tgamma(y)/tgamma(x+y);
260                 }
261                 // no problem in numerator, but denominator has pole:
262                 if ((nx+ny).is_real() &&
263                     (nx+ny).is_integer() &&
264                    !(nx+ny).is_positive())
265                          return _ex0;
266                 if (!ex_to<numeric>(x).is_rational() || !ex_to<numeric>(x).is_rational())
267                         return evalf(beta(x, y).hold());
268         }
269         
270         return beta(x,y).hold();
271 }
272
273
274 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
275 {
276         GINAC_ASSERT(deriv_param<2);
277         ex retval;
278         
279         // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
280         if (deriv_param==0)
281                 retval = (psi(x)-psi(x+y))*beta(x,y);
282         // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
283         if (deriv_param==1)
284                 retval = (psi(y)-psi(x+y))*beta(x,y);
285         return retval;
286 }
287
288
289 static ex beta_series(const ex & arg1,
290                       const ex & arg2,
291                       const relational & rel,
292                       int order,
293                       unsigned options)
294 {
295         // method:
296         // Taylor series where there is no pole of one of the tgamma functions
297         // falls back to beta function evaluation.  Otherwise, fall back to
298         // tgamma series directly.
299         const ex arg1_pt = arg1.subs(rel, subs_options::no_pattern);
300         const ex arg2_pt = arg2.subs(rel, subs_options::no_pattern);
301         GINAC_ASSERT(is_a<symbol>(rel.lhs()));
302         const symbol &s = ex_to<symbol>(rel.lhs());
303         ex arg1_ser, arg2_ser, arg1arg2_ser;
304         if ((!arg1_pt.info(info_flags::integer) || arg1_pt.info(info_flags::positive)) &&
305             (!arg2_pt.info(info_flags::integer) || arg2_pt.info(info_flags::positive)))
306                 throw do_taylor();  // caught by function::series()
307         // trap the case where arg1 is on a pole:
308         if (arg1.info(info_flags::integer) && !arg1.info(info_flags::positive))
309                 arg1_ser = tgamma(arg1+s);
310         else
311                 arg1_ser = tgamma(arg1);
312         // trap the case where arg2 is on a pole:
313         if (arg2.info(info_flags::integer) && !arg2.info(info_flags::positive))
314                 arg2_ser = tgamma(arg2+s);
315         else
316                 arg2_ser = tgamma(arg2);
317         // trap the case where arg1+arg2 is on a pole:
318         if ((arg1+arg2).info(info_flags::integer) && !(arg1+arg2).info(info_flags::positive))
319                 arg1arg2_ser = tgamma(arg2+arg1+s);
320         else
321                 arg1arg2_ser = tgamma(arg2+arg1);
322         // compose the result (expanding all the terms):
323         return (arg1_ser*arg2_ser/arg1arg2_ser).series(rel, order, options).expand();
324 }
325
326
327 REGISTER_FUNCTION(beta, eval_func(beta_eval).
328                         evalf_func(beta_evalf).
329                         derivative_func(beta_deriv).
330                         series_func(beta_series).
331                         latex_name("\\mathrm{B}").
332                                                 set_symmetry(sy_symm(0, 1)));
333
334
335 //////////
336 // Psi-function (aka digamma-function)
337 //////////
338
339 static ex psi1_evalf(const ex & x)
340 {
341         if (is_exactly_a<numeric>(x)) {
342                 try {
343                         return psi(ex_to<numeric>(x));
344                 } catch (const dunno &e) { }
345         }
346         
347         return psi(x).hold();
348 }
349
350 /** Evaluation of digamma-function psi(x).
351  *  Somebody ought to provide some good numerical evaluation some day... */
352 static ex psi1_eval(const ex & x)
353 {
354         if (x.info(info_flags::numeric)) {
355                 const numeric &nx = ex_to<numeric>(x);
356                 if (nx.is_integer()) {
357                         // integer case 
358                         if (nx.is_positive()) {
359                                 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
360                                 numeric rat = 0;
361                                 for (numeric i(nx+(*_num_1_p)); i>0; --i)
362                                         rat += i.inverse();
363                                 return rat-Euler;
364                         } else {
365                                 // for non-positive integers there is a pole:
366                                 throw (pole_error("psi_eval(): simple pole",1));
367                         }
368                 }
369                 if (((*_num2_p)*nx).is_integer()) {
370                         // half integer case
371                         if (nx.is_positive()) {
372                                 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
373                                 numeric rat = 0;
374                                 for (numeric i = (nx+(*_num_1_p))*(*_num2_p); i>0; i-=(*_num2_p))
375                                         rat += (*_num2_p)*i.inverse();
376                                 return rat-Euler-_ex2*log(_ex2);
377                         } else {
378                                 // use the recurrence relation
379                                 //   psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
380                                 // to relate psi(-m-1/2) to psi(1/2):
381                                 //   psi(-m-1/2) == psi(1/2) + r
382                                 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
383                                 numeric recur = 0;
384                                 for (numeric p = nx; p<0; ++p)
385                                         recur -= pow(p, *_num_1_p);
386                                 return recur+psi(_ex1_2);
387                         }
388                 }
389                 //  psi1_evalf should be called here once it becomes available
390         }
391         
392         return psi(x).hold();
393 }
394
395 static ex psi1_deriv(const ex & x, unsigned deriv_param)
396 {
397         GINAC_ASSERT(deriv_param==0);
398         
399         // d/dx psi(x) -> psi(1,x)
400         return psi(_ex1, x);
401 }
402
403 static ex psi1_series(const ex & arg,
404                       const relational & rel,
405                       int order,
406                       unsigned options)
407 {
408         // method:
409         // Taylor series where there is no pole falls back to polygamma function
410         // evaluation.
411         // On a pole at -m use the recurrence relation
412         //   psi(x) == psi(x+1) - 1/z
413         // from which follows
414         //   series(psi(x),x==-m,order) ==
415         //   series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
416         const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
417         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
418                 throw do_taylor();  // caught by function::series()
419         // if we got here we have to care for a simple pole at -m:
420         const numeric m = -ex_to<numeric>(arg_pt);
421         ex recur;
422         for (numeric p; p<=m; ++p)
423                 recur += power(arg+p,_ex_1);
424         return (psi(arg+m+_ex1)-recur).series(rel, order, options);
425 }
426
427 unsigned psi1_SERIAL::serial =
428         function::register_new(function_options("psi", 1).
429                                eval_func(psi1_eval).
430                                evalf_func(psi1_evalf).
431                                derivative_func(psi1_deriv).
432                                series_func(psi1_series).
433                                latex_name("\\psi").
434                                overloaded(2));
435
436 //////////
437 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
438 //////////
439
440 static ex psi2_evalf(const ex & n, const ex & x)
441 {
442         if (is_exactly_a<numeric>(n) && is_exactly_a<numeric>(x)) {
443                 try {
444                         return psi(ex_to<numeric>(n),ex_to<numeric>(x));
445                 } catch (const dunno &e) { }
446         }
447         
448         return psi(n,x).hold();
449 }
450
451 /** Evaluation of polygamma-function psi(n,x). 
452  *  Somebody ought to provide some good numerical evaluation some day... */
453 static ex psi2_eval(const ex & n, const ex & x)
454 {
455         // psi(0,x) -> psi(x)
456         if (n.is_zero())
457                 return psi(x);
458         // psi(-1,x) -> log(tgamma(x))
459         if (n.is_equal(_ex_1))
460                 return log(tgamma(x));
461         if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
462                 x.info(info_flags::numeric)) {
463                 const numeric &nn = ex_to<numeric>(n);
464                 const numeric &nx = ex_to<numeric>(x);
465                 if (nx.is_integer()) {
466                         // integer case 
467                         if (nx.is_equal(*_num1_p))
468                                 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
469                                 return pow(*_num_1_p,nn+(*_num1_p))*factorial(nn)*zeta(ex(nn+(*_num1_p)));
470                         if (nx.is_positive()) {
471                                 // use the recurrence relation
472                                 //   psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
473                                 // to relate psi(n,m) to psi(n,1):
474                                 //   psi(n,m) == psi(n,1) + r
475                                 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
476                                 numeric recur = 0;
477                                 for (numeric p = 1; p<nx; ++p)
478                                         recur += pow(p, -nn+(*_num_1_p));
479                                 recur *= factorial(nn)*pow((*_num_1_p), nn);
480                                 return recur+psi(n,_ex1);
481                         } else {
482                                 // for non-positive integers there is a pole:
483                                 throw (pole_error("psi2_eval(): pole",1));
484                         }
485                 }
486                 if (((*_num2_p)*nx).is_integer()) {
487                         // half integer case
488                         if (nx.is_equal(*_num1_2_p))
489                                 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
490                                 return pow(*_num_1_p,nn+(*_num1_p))*factorial(nn)*(pow(*_num2_p,nn+(*_num1_p)) + (*_num_1_p))*zeta(ex(nn+(*_num1_p)));
491                         if (nx.is_positive()) {
492                                 const numeric m = nx - (*_num1_2_p);
493                                 // use the multiplication formula
494                                 //   psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
495                                 // to revert to positive integer case
496                                 return psi(n,(*_num2_p)*m)*pow((*_num2_p),nn+(*_num1_p))-psi(n,m);
497                         } else {
498                                 // use the recurrence relation
499                                 //   psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
500                                 // to relate psi(n,-m-1/2) to psi(n,1/2):
501                                 //   psi(n,-m-1/2) == psi(n,1/2) + r
502                                 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
503                                 numeric recur = 0;
504                                 for (numeric p = nx; p<0; ++p)
505                                         recur += pow(p, -nn+(*_num_1_p));
506                                 recur *= factorial(nn)*pow(*_num_1_p, nn+(*_num_1_p));
507                                 return recur+psi(n,_ex1_2);
508                         }
509                 }
510                 //  psi2_evalf should be called here once it becomes available
511         }
512         
513         return psi(n, x).hold();
514 }    
515
516 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
517 {
518         GINAC_ASSERT(deriv_param<2);
519         
520         if (deriv_param==0) {
521                 // d/dn psi(n,x)
522                 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
523         }
524         // d/dx psi(n,x) -> psi(n+1,x)
525         return psi(n+_ex1, x);
526 }
527
528 static ex psi2_series(const ex & n,
529                       const ex & arg,
530                       const relational & rel,
531                       int order,
532                       unsigned options)
533 {
534         // method:
535         // Taylor series where there is no pole falls back to polygamma function
536         // evaluation.
537         // On a pole at -m use the recurrence relation
538         //   psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
539         // from which follows
540         //   series(psi(x),x==-m,order) == 
541         //   series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
542         //                                      ... + (x+m)^(-n-1))),x==-m,order);
543         const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
544         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
545                 throw do_taylor();  // caught by function::series()
546         // if we got here we have to care for a pole of order n+1 at -m:
547         const numeric m = -ex_to<numeric>(arg_pt);
548         ex recur;
549         for (numeric p; p<=m; ++p)
550                 recur += power(arg+p,-n+_ex_1);
551         recur *= factorial(n)*power(_ex_1,n);
552         return (psi(n, arg+m+_ex1)-recur).series(rel, order, options);
553 }
554
555 unsigned psi2_SERIAL::serial =
556         function::register_new(function_options("psi", 2).
557                                eval_func(psi2_eval).
558                                evalf_func(psi2_evalf).
559                                derivative_func(psi2_deriv).
560                                series_func(psi2_series).
561                                latex_name("\\psi").
562                                overloaded(2));
563
564
565 } // namespace GiNaC