]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
* Prepare for landing of version 0.7.3.
[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 "utils.h"
35
36 #ifndef NO_NAMESPACE_GINAC
37 namespace GiNaC {
38 #endif // ndef NO_NAMESPACE_GINAC
39
40 //////////
41 // Logarithm of Gamma function
42 //////////
43
44 static ex lgamma_evalf(const ex & x)
45 {
46         BEGIN_TYPECHECK
47                 TYPECHECK(x,numeric)
48         END_TYPECHECK(lgamma(x))
49         
50         return lgamma(ex_to_numeric(x));
51 }
52
53
54 /** Evaluation of lgamma(x), the natural logarithm of the Gamma function.
55  *  Knows about integer arguments and that's it.  Somebody ought to provide
56  *  some good numerical evaluation some day...
57  *
58  *  @exception GiNaC::pole_error("lgamma_eval(): logarithmic pole",0) */
59 static ex lgamma_eval(const ex & x)
60 {
61         if (x.info(info_flags::numeric)) {
62                 // trap integer arguments:
63                 if (x.info(info_flags::integer)) {
64                         // lgamma(n) -> log((n-1)!) for postitive n
65                         if (x.info(info_flags::posint))
66                                 return log(factorial(x.exadd(_ex_1())));
67                         else
68                                 throw (pole_error("lgamma_eval(): logarithmic pole",0));
69                 }
70                 //  lgamma_evalf should be called here once it becomes available
71         }
72         
73         return lgamma(x).hold();
74 }
75
76
77 static ex lgamma_deriv(const ex & x, unsigned deriv_param)
78 {
79         GINAC_ASSERT(deriv_param==0);
80         
81         // d/dx  lgamma(x) -> psi(x)
82         return psi(x);
83 }
84
85
86 static ex lgamma_series(const ex & arg,
87                         const relational & rel,
88                         int order,
89                         unsigned options)
90 {
91         // method:
92         // Taylor series where there is no pole falls back to psi function
93         // evaluation.
94         // On a pole at -m we could use the recurrence relation
95         //   lgamma(x) == lgamma(x+1)-log(x)
96         // from which follows
97         //   series(lgamma(x),x==-m,order) ==
98         //   series(lgamma(x+m+1)-log(x)...-log(x+m)),x==-m,order);
99         const ex arg_pt = arg.subs(rel);
100         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
101                 throw do_taylor();  // caught by function::series()
102         // if we got here we have to care for a simple pole of tgamma(-m):
103         numeric m = -ex_to_numeric(arg_pt);
104         ex recur;
105         for (numeric p; p<=m; ++p)
106                 recur += log(arg+p);
107         return (lgamma(arg+m+_ex1())-recur).series(rel, order, options);
108 }
109
110
111 REGISTER_FUNCTION(lgamma, eval_func(lgamma_eval).
112                           evalf_func(lgamma_evalf).
113                           derivative_func(lgamma_deriv).
114                           series_func(lgamma_series));
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
212
213 //////////
214 // beta-function
215 //////////
216
217 static ex beta_evalf(const ex & x, const ex & y)
218 {
219         BEGIN_TYPECHECK
220                 TYPECHECK(x,numeric)
221                 TYPECHECK(y,numeric)
222         END_TYPECHECK(beta(x,y))
223         
224         return tgamma(ex_to_numeric(x))*tgamma(ex_to_numeric(y))/tgamma(ex_to_numeric(x+y));
225 }
226
227
228 static ex beta_eval(const ex & x, const ex & y)
229 {
230         if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
231                 // treat all problematic x and y that may not be passed into tgamma,
232                 // because they would throw there although beta(x,y) is well-defined
233                 // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
234                 numeric nx(ex_to_numeric(x));
235                 numeric ny(ex_to_numeric(y));
236                 if (nx.is_real() && nx.is_integer() &&
237                         ny.is_real() && ny.is_integer()) {
238                         if (nx.is_negative()) {
239                                 if (nx<=-ny)
240                                         return pow(_num_1(), ny)*beta(1-x-y, y);
241                                 else
242                                         throw (pole_error("beta_eval(): simple pole",1));
243                         }
244                         if (ny.is_negative()) {
245                                 if (ny<=-nx)
246                                         return pow(_num_1(), nx)*beta(1-y-x, x);
247                                 else
248                                         throw (pole_error("beta_eval(): simple pole",1));
249                         }
250                         return tgamma(x)*tgamma(y)/tgamma(x+y);
251                 }
252                 // no problem in numerator, but denominator has pole:
253                 if ((nx+ny).is_real() &&
254                         (nx+ny).is_integer() &&
255                         !(nx+ny).is_positive())
256                          return _ex0();
257                 // everything is ok:
258                 return tgamma(x)*tgamma(y)/tgamma(x+y);
259         }
260         
261         return beta(x,y).hold();
262 }
263
264
265 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
266 {
267         GINAC_ASSERT(deriv_param<2);
268         ex retval;
269         
270         // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
271         if (deriv_param==0)
272                 retval = (psi(x)-psi(x+y))*beta(x,y);
273         // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
274         if (deriv_param==1)
275                 retval = (psi(y)-psi(x+y))*beta(x,y);
276         return retval;
277 }
278
279
280 static ex beta_series(const ex & arg1,
281                       const ex & arg2,
282                       const relational & rel,
283                       int order,
284                       unsigned options)
285 {
286         // method:
287         // Taylor series where there is no pole of one of the tgamma functions
288         // falls back to beta function evaluation.  Otherwise, fall back to
289         // tgamma series directly.
290         const ex arg1_pt = arg1.subs(rel);
291         const ex arg2_pt = arg2.subs(rel);
292         GINAC_ASSERT(is_ex_exactly_of_type(rel.lhs(),symbol));
293         const symbol *s = static_cast<symbol *>(rel.lhs().bp);
294         ex arg1_ser, arg2_ser, arg1arg2_ser;
295         if ((!arg1_pt.info(info_flags::integer) || arg1_pt.info(info_flags::positive)) &&
296             (!arg2_pt.info(info_flags::integer) || arg2_pt.info(info_flags::positive)))
297                 throw do_taylor();  // caught by function::series()
298         // trap the case where arg1 is on a pole:
299         if (arg1.info(info_flags::integer) && !arg1.info(info_flags::positive))
300                 arg1_ser = tgamma(arg1+*s).series(rel, order, options);
301         else
302                 arg1_ser = tgamma(arg1).series(rel,order);
303         // trap the case where arg2 is on a pole:
304         if (arg2.info(info_flags::integer) && !arg2.info(info_flags::positive))
305                 arg2_ser = tgamma(arg2+*s).series(rel, order, options);
306         else
307                 arg2_ser = tgamma(arg2).series(rel,order);
308         // trap the case where arg1+arg2 is on a pole:
309         if ((arg1+arg2).info(info_flags::integer) && !(arg1+arg2).info(info_flags::positive))
310                 arg1arg2_ser = tgamma(arg2+arg1+*s).series(rel, order, options);
311         else
312                 arg1arg2_ser = tgamma(arg2+arg1).series(rel,order);
313         // compose the result (expanding all the terms):
314         return (arg1_ser*arg2_ser/arg1arg2_ser).series(rel, order, options).expand();
315 }
316
317
318 REGISTER_FUNCTION(beta, eval_func(beta_eval).
319                         evalf_func(beta_evalf).
320                         derivative_func(beta_deriv).
321                         series_func(beta_series));
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                 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.is_positive(); --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.is_positive(); 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         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                                overloaded(2));
421
422 //////////
423 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
424 //////////
425
426 static ex psi2_evalf(const ex & n, const ex & x)
427 {
428         BEGIN_TYPECHECK
429                 TYPECHECK(n,numeric)
430                 TYPECHECK(x,numeric)
431         END_TYPECHECK(psi(n,x))
432         
433         return psi(ex_to_numeric(n), ex_to_numeric(x));
434 }
435
436 /** Evaluation of polygamma-function psi(n,x). 
437  *  Somebody ought to provide some good numerical evaluation some day... */
438 static ex psi2_eval(const ex & n, const ex & x)
439 {
440         // psi(0,x) -> psi(x)
441         if (n.is_zero())
442                 return psi(x);
443         // psi(-1,x) -> log(tgamma(x))
444         if (n.is_equal(_ex_1()))
445                 return log(tgamma(x));
446         if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
447                 x.info(info_flags::numeric)) {
448                 numeric nn = ex_to_numeric(n);
449                 numeric nx = ex_to_numeric(x);
450                 if (nx.is_integer()) {
451                         // integer case 
452                         if (nx.is_equal(_num1()))
453                                 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
454                                 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
455                         if (nx.is_positive()) {
456                                 // use the recurrence relation
457                                 //   psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
458                                 // to relate psi(n,m) to psi(n,1):
459                                 //   psi(n,m) == psi(n,1) + r
460                                 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
461                                 numeric recur(0);
462                                 for (numeric p(1); p<nx; ++p)
463                                         recur += pow(p, -nn+_num_1());
464                                 recur *= factorial(nn)*pow(_num_1(), nn);
465                                 return recur+psi(n,_ex1());
466                         } else {
467                                 // for non-positive integers there is a pole:
468                                 throw (pole_error("psi2_eval(): pole",1));
469                         }
470                 }
471                 if ((_num2()*nx).is_integer()) {
472                         // half integer case
473                         if (nx.is_equal(_num1_2()))
474                                 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
475                                 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
476                         if (nx.is_positive()) {
477                                 numeric m = nx - _num1_2();
478                                 // use the multiplication formula
479                                 //   psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
480                                 // to revert to positive integer case
481                                 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
482                         } else {
483                                 // use the recurrence relation
484                                 //   psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
485                                 // to relate psi(n,-m-1/2) to psi(n,1/2):
486                                 //   psi(n,-m-1/2) == psi(n,1/2) + r
487                                 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
488                                 numeric recur(0);
489                                 for (numeric p(nx); p<0; ++p)
490                                         recur += pow(p, -nn+_num_1());
491                                 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
492                                 return recur+psi(n,_ex1_2());
493                         }
494                 }
495                 //  psi2_evalf should be called here once it becomes available
496         }
497         
498         return psi(n, x).hold();
499 }    
500
501 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
502 {
503         GINAC_ASSERT(deriv_param<2);
504         
505         if (deriv_param==0) {
506                 // d/dn psi(n,x)
507                 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
508         }
509         // d/dx psi(n,x) -> psi(n+1,x)
510         return psi(n+_ex1(), x);
511 }
512
513 static ex psi2_series(const ex & n,
514                       const ex & arg,
515                       const relational & rel,
516                       int order,
517                       unsigned options)
518 {
519         // method:
520         // Taylor series where there is no pole falls back to polygamma function
521         // evaluation.
522         // On a pole at -m use the recurrence relation
523         //   psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
524         // from which follows
525         //   series(psi(x),x==-m,order) == 
526         //   series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
527         //                                      ... + (x+m)^(-n-1))),x==-m,order);
528         const ex arg_pt = arg.subs(rel);
529         if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
530                 throw do_taylor();  // caught by function::series()
531         // if we got here we have to care for a pole of order n+1 at -m:
532         numeric m = -ex_to_numeric(arg_pt);
533         ex recur;
534         for (numeric p; p<=m; ++p)
535                 recur += power(arg+p,-n+_ex_1());
536         recur *= factorial(n)*power(_ex_1(),n);
537         return (psi(n, arg+m+_ex1())-recur).series(rel, order, options);
538 }
539
540 const unsigned function_index_psi2 =
541         function::register_new(function_options("psi").
542                                eval_func(psi2_eval).
543                                evalf_func(psi2_evalf).
544                                derivative_func(psi2_deriv).
545                                series_func(psi2_series).
546                                overloaded(2));
547
548
549 #ifndef NO_NAMESPACE_GINAC
550 } // namespace GiNaC
551 #endif // ndef NO_NAMESPACE_GINAC