]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
b28c246253258e0980a1fbbdc7ad50eefdc769ef
[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-2000 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 "ex.h"
29 #include "constant.h"
30 #include "pseries.h"
31 #include "numeric.h"
32 #include "power.h"
33 #include "relational.h"
34 #include "symbol.h"
35 #include "utils.h"
36
37 #ifndef NO_NAMESPACE_GINAC
38 namespace GiNaC {
39 #endif // ndef NO_NAMESPACE_GINAC
40
41 //////////
42 // Logarithm of Gamma function
43 //////////
44
45 static ex lgamma_evalf(const ex & x)
46 {
47     BEGIN_TYPECHECK
48         TYPECHECK(x,numeric)
49     END_TYPECHECK(lgamma(x))
50     
51     return lgamma(ex_to_numeric(x));
52 }
53
54
55 /** Evaluation of lgamma(x), the natural logarithm of the Gamma function.
56  *  Knows about integer arguments and that's it.  Somebody ought to provide
57  *  some good numerical evaluation some day...
58  *
59  *  @exception std::domain_error("lgamma_eval(): simple pole") */
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.exadd(_ex_1())));
68             } else {
69                 throw (std::domain_error("lgamma_eval(): logarithmic pole"));
70             }
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 // need to implement lgamma_series.
88
89 REGISTER_FUNCTION(lgamma, eval_func(lgamma_eval).
90                           evalf_func(lgamma_evalf).
91                           derivative_func(lgamma_deriv));
92
93
94 //////////
95 // true Gamma function
96 //////////
97
98 static ex tgamma_evalf(const ex & x)
99 {
100     BEGIN_TYPECHECK
101         TYPECHECK(x,numeric)
102     END_TYPECHECK(tgamma(x))
103     
104     return tgamma(ex_to_numeric(x));
105 }
106
107
108 /** Evaluation of tgamma(x), the true Gamma function.  Knows about integer
109  *  arguments, half-integer arguments and that's it. Somebody ought to provide
110  *  some good numerical evaluation some day...
111  *
112  *  @exception std::domain_error("tgamma_eval(): simple pole") */
113 static ex tgamma_eval(const ex & x)
114 {
115     if (x.info(info_flags::numeric)) {
116         // trap integer arguments:
117         if (x.info(info_flags::integer)) {
118             // tgamma(n) -> (n-1)! for postitive n
119             if (x.info(info_flags::posint)) {
120                 return factorial(ex_to_numeric(x).sub(_num1()));
121             } else {
122                 throw (std::domain_error("tgamma_eval(): simple pole"));
123             }
124         }
125         // trap half integer arguments:
126         if ((x*2).info(info_flags::integer)) {
127             // trap positive x==(n+1/2)
128             // tgamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
129             if ((x*_ex2()).info(info_flags::posint)) {
130                 numeric n = ex_to_numeric(x).sub(_num1_2());
131                 numeric coefficient = doublefactorial(n.mul(_num2()).sub(_num1()));
132                 coefficient = coefficient.div(pow(_num2(),n));
133                 return coefficient * pow(Pi,_ex1_2());
134             } else {
135                 // trap negative x==(-n+1/2)
136                 // tgamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
137                 numeric n = abs(ex_to_numeric(x).sub(_num1_2()));
138                 numeric coefficient = pow(_num_2(), n);
139                 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
140                 return coefficient*power(Pi,_ex1_2());
141             }
142         }
143         //  tgamma_evalf should be called here once it becomes available
144     }
145     
146     return tgamma(x).hold();
147 }
148
149
150 static ex tgamma_deriv(const ex & x, unsigned deriv_param)
151 {
152     GINAC_ASSERT(deriv_param==0);
153     
154     // d/dx  tgamma(x) -> psi(x)*tgamma(x)
155     return psi(x)*tgamma(x);
156 }
157
158
159 static ex tgamma_series(const ex & x, const relational & r, int order)
160 {
161     // method:
162     // Taylor series where there is no pole falls back to psi function
163     // evaluation.
164     // On a pole at -m use the recurrence relation
165     //   tgamma(x) == tgamma(x+1) / x
166     // from which follows
167     //   series(tgamma(x),x,-m,order) ==
168     //   series(tgamma(x+m+1)/(x*(x+1)*...*(x+m)),x,-m,order+1);
169     const ex x_pt = x.subs(r);
170     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
171         throw do_taylor();  // caught by function::series()
172     // if we got here we have to care for a simple pole at -m:
173     numeric m = -ex_to_numeric(x_pt);
174     ex ser_denom = _ex1();
175     for (numeric p; p<=m; ++p)
176         ser_denom *= x+p;
177     return (tgamma(x+m+_ex1())/ser_denom).series(r, order+1);
178 }
179
180
181 REGISTER_FUNCTION(tgamma, eval_func(tgamma_eval).
182                           evalf_func(tgamma_evalf).
183                           derivative_func(tgamma_deriv).
184                           series_func(tgamma_series));
185
186
187 //////////
188 // beta-function
189 //////////
190
191 static ex beta_evalf(const ex & x, const ex & y)
192 {
193     BEGIN_TYPECHECK
194         TYPECHECK(x,numeric)
195         TYPECHECK(y,numeric)
196     END_TYPECHECK(beta(x,y))
197     
198     return tgamma(ex_to_numeric(x))*tgamma(ex_to_numeric(y))/tgamma(ex_to_numeric(x+y));
199 }
200
201
202 static ex beta_eval(const ex & x, const ex & y)
203 {
204     if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
205         // treat all problematic x and y that may not be passed into tgamma,
206         // because they would throw there although beta(x,y) is well-defined
207         // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
208         numeric nx(ex_to_numeric(x));
209         numeric ny(ex_to_numeric(y));
210         if (nx.is_real() && nx.is_integer() &&
211             ny.is_real() && ny.is_integer()) {
212             if (nx.is_negative()) {
213                 if (nx<=-ny)
214                     return pow(_num_1(), ny)*beta(1-x-y, y);
215                 else
216                     throw (std::domain_error("beta_eval(): simple pole"));
217             }
218             if (ny.is_negative()) {
219                 if (ny<=-nx)
220                     return pow(_num_1(), nx)*beta(1-y-x, x);
221                 else
222                     throw (std::domain_error("beta_eval(): simple pole"));
223             }
224             return tgamma(x)*tgamma(y)/tgamma(x+y);
225         }
226         // no problem in numerator, but denominator has pole:
227         if ((nx+ny).is_real() &&
228             (nx+ny).is_integer() &&
229             !(nx+ny).is_positive())
230              return _ex0();
231         // everything is ok:
232         return tgamma(x)*tgamma(y)/tgamma(x+y);
233     }
234     
235     return beta(x,y).hold();
236 }
237
238
239 static ex beta_deriv(const ex & x, const ex & y, unsigned deriv_param)
240 {
241     GINAC_ASSERT(deriv_param<2);
242     ex retval;
243     
244     // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
245     if (deriv_param==0)
246         retval = (psi(x)-psi(x+y))*beta(x,y);
247     // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
248     if (deriv_param==1)
249         retval = (psi(y)-psi(x+y))*beta(x,y);
250     return retval;
251 }
252
253
254 static ex beta_series(const ex & x, const ex & y, const relational & r, int order)
255 {
256     // method:
257     // Taylor series where there is no pole of one of the tgamma functions
258     // falls back to beta function evaluation.  Otherwise, fall back to
259     // tgamma series directly.
260     // FIXME: this could need some testing, maybe it's wrong in some cases?
261     const ex x_pt = x.subs(r);
262     const ex y_pt = y.subs(r);
263     GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
264     const symbol *s = static_cast<symbol *>(r.lhs().bp);
265     ex x_ser, y_ser, xy_ser;
266     if ((!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive)) &&
267         (!y_pt.info(info_flags::integer) || y_pt.info(info_flags::positive)))
268         throw do_taylor();  // caught by function::series()
269     // trap the case where x is on a pole directly:
270     if (x.info(info_flags::integer) && !x.info(info_flags::positive))
271         x_ser = tgamma(x+*s).series(r,order);
272     else
273         x_ser = tgamma(x).series(r,order);
274     // trap the case where y is on a pole directly:
275     if (y.info(info_flags::integer) && !y.info(info_flags::positive))
276         y_ser = tgamma(y+*s).series(r,order);
277     else
278         y_ser = tgamma(y).series(r,order);
279     // trap the case where y is on a pole directly:
280     if ((x+y).info(info_flags::integer) && !(x+y).info(info_flags::positive))
281         xy_ser = tgamma(y+x+*s).series(r,order);
282     else
283         xy_ser = tgamma(y+x).series(r,order);
284     // compose the result:
285     return (x_ser*y_ser/xy_ser).series(r,order);
286 }
287
288
289 REGISTER_FUNCTION(beta, eval_func(beta_eval).
290                         evalf_func(beta_evalf).
291                         derivative_func(beta_deriv).
292                         series_func(beta_series));
293
294
295 //////////
296 // Psi-function (aka digamma-function)
297 //////////
298
299 static ex psi1_evalf(const ex & x)
300 {
301     BEGIN_TYPECHECK
302         TYPECHECK(x,numeric)
303     END_TYPECHECK(psi(x))
304     
305     return psi(ex_to_numeric(x));
306 }
307
308 /** Evaluation of digamma-function psi(x).
309  *  Somebody ought to provide some good numerical evaluation some day... */
310 static ex psi1_eval(const ex & x)
311 {
312     if (x.info(info_flags::numeric)) {
313         numeric nx = ex_to_numeric(x);
314         if (nx.is_integer()) {
315             // integer case 
316             if (nx.is_positive()) {
317                 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
318                 numeric rat(0);
319                 for (numeric i(nx+_num_1()); i.is_positive(); --i)
320                     rat += i.inverse();
321                 return rat-Euler;
322             } else {
323                 // for non-positive integers there is a pole:
324                 throw (std::domain_error("psi_eval(): simple pole"));
325             }
326         }
327         if ((_num2()*nx).is_integer()) {
328             // half integer case
329             if (nx.is_positive()) {
330                 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
331                 numeric rat(0);
332                 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
333                                       rat += _num2()*i.inverse();
334                                       return rat-Euler-_ex2()*log(_ex2());
335             } else {
336                 // use the recurrence relation
337                 //   psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
338                 // to relate psi(-m-1/2) to psi(1/2):
339                 //   psi(-m-1/2) == psi(1/2) + r
340                 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
341                 numeric recur(0);
342                 for (numeric p(nx); p<0; ++p)
343                     recur -= pow(p, _num_1());
344                 return recur+psi(_ex1_2());
345             }
346         }
347         //  psi1_evalf should be called here once it becomes available
348     }
349     
350     return psi(x).hold();
351 }
352
353 static ex psi1_deriv(const ex & x, unsigned deriv_param)
354 {
355     GINAC_ASSERT(deriv_param==0);
356     
357     // d/dx psi(x) -> psi(1,x)
358     return psi(_ex1(), x);
359 }
360
361 static ex psi1_series(const ex & x, const relational & r, int order)
362 {
363     // method:
364     // Taylor series where there is no pole falls back to polygamma function
365     // evaluation.
366     // On a pole at -m use the recurrence relation
367     //   psi(x) == psi(x+1) - 1/z
368     // from which follows
369     //   series(psi(x),x,-m,order) ==
370     //   series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
371     const ex x_pt = x.subs(r);
372     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
373         throw do_taylor();  // caught by function::series()
374     // if we got here we have to care for a simple pole at -m:
375     numeric m = -ex_to_numeric(x_pt);
376     ex recur;
377     for (numeric p; p<=m; ++p)
378         recur += power(x+p,_ex_1());
379     return (psi(x+m+_ex1())-recur).series(r, order);
380 }
381
382 const unsigned function_index_psi1 =
383     function::register_new(function_options("psi").
384                            eval_func(psi1_eval).
385                            evalf_func(psi1_evalf).
386                            derivative_func(psi1_deriv).
387                            series_func(psi1_series).
388                            overloaded(2));
389
390 //////////
391 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
392 //////////
393
394 static ex psi2_evalf(const ex & n, const ex & x)
395 {
396     BEGIN_TYPECHECK
397         TYPECHECK(n,numeric)
398         TYPECHECK(x,numeric)
399     END_TYPECHECK(psi(n,x))
400     
401     return psi(ex_to_numeric(n), ex_to_numeric(x));
402 }
403
404 /** Evaluation of polygamma-function psi(n,x). 
405  *  Somebody ought to provide some good numerical evaluation some day... */
406 static ex psi2_eval(const ex & n, const ex & x)
407 {
408     // psi(0,x) -> psi(x)
409     if (n.is_zero())
410         return psi(x);
411     // psi(-1,x) -> log(tgamma(x))
412     if (n.is_equal(_ex_1()))
413         return log(tgamma(x));
414     if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
415         x.info(info_flags::numeric)) {
416         numeric nn = ex_to_numeric(n);
417         numeric nx = ex_to_numeric(x);
418         if (nx.is_integer()) {
419             // integer case 
420             if (nx.is_equal(_num1()))
421                 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
422                 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
423             if (nx.is_positive()) {
424                 // use the recurrence relation
425                 //   psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
426                 // to relate psi(n,m) to psi(n,1):
427                 //   psi(n,m) == psi(n,1) + r
428                 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
429                 numeric recur(0);
430                 for (numeric p(1); p<nx; ++p)
431                     recur += pow(p, -nn+_num_1());
432                 recur *= factorial(nn)*pow(_num_1(), nn);
433                 return recur+psi(n,_ex1());
434             } else {
435                 // for non-positive integers there is a pole:
436                 throw (std::domain_error("psi2_eval(): pole"));
437             }
438         }
439         if ((_num2()*nx).is_integer()) {
440             // half integer case
441             if (nx.is_equal(_num1_2()))
442                 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
443                 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
444             if (nx.is_positive()) {
445                 numeric m = nx - _num1_2();
446                 // use the multiplication formula
447                 //   psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
448                 // to revert to positive integer case
449                 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
450             } else {
451                 // use the recurrence relation
452                 //   psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
453                 // to relate psi(n,-m-1/2) to psi(n,1/2):
454                 //   psi(n,-m-1/2) == psi(n,1/2) + r
455                 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
456                 numeric recur(0);
457                 for (numeric p(nx); p<0; ++p)
458                     recur += pow(p, -nn+_num_1());
459                 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
460                 return recur+psi(n,_ex1_2());
461             }
462         }
463         //  psi2_evalf should be called here once it becomes available
464     }
465     
466     return psi(n, x).hold();
467 }    
468
469 static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
470 {
471     GINAC_ASSERT(deriv_param<2);
472     
473     if (deriv_param==0) {
474         // d/dn psi(n,x)
475         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
476     }
477     // d/dx psi(n,x) -> psi(n+1,x)
478     return psi(n+_ex1(), x);
479 }
480
481 static ex psi2_series(const ex & n, const ex & x, const relational & r, int order)
482 {
483     // method:
484     // Taylor series where there is no pole falls back to polygamma function
485     // evaluation.
486     // On a pole at -m use the recurrence relation
487     //   psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
488     // from which follows
489     //   series(psi(x),x,-m,order) == 
490     //   series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
491     //                                      ... + (x+m)^(-n-1))),x,-m,order);
492     const ex x_pt = x.subs(r);
493     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
494         throw do_taylor();  // caught by function::series()
495     // if we got here we have to care for a pole of order n+1 at -m:
496     numeric m = -ex_to_numeric(x_pt);
497     ex recur;
498     for (numeric p; p<=m; ++p)
499         recur += power(x+p,-n+_ex_1());
500     recur *= factorial(n)*power(_ex_1(),n);
501     return (psi(n, x+m+_ex1())-recur).series(r, order);
502 }
503
504 const unsigned function_index_psi2 =
505     function::register_new(function_options("psi").
506                            eval_func(psi2_eval).
507                            evalf_func(psi2_evalf).
508                derivative_func(psi2_deriv).
509                            series_func(psi2_series).
510                            overloaded(2));
511
512
513 #ifndef NO_NAMESPACE_GINAC
514 } // namespace GiNaC
515 #endif // ndef NO_NAMESPACE_GINAC