]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
- killed old add::print
[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_GINAC_NAMESPACE
38 namespace GiNaC {
39 #endif // ndef NO_GINAC_NAMESPACE
40
41 //////////
42 // Gamma-function
43 //////////
44
45 static ex gamma_evalf(const ex & x)
46 {
47     BEGIN_TYPECHECK
48         TYPECHECK(x,numeric)
49     END_TYPECHECK(gamma(x))
50     
51     return gamma(ex_to_numeric(x));
52 }
53
54 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
55  *  arguments and that's it. Somebody ought to provide some good numerical
56  *  evaluation some day...
57  *
58  *  @exception std::domain_error("gamma_eval(): simple pole") */
59 static ex gamma_eval(const ex & x)
60 {
61     if (x.info(info_flags::numeric)) {
62         // trap integer arguments:
63         if (x.info(info_flags::integer)) {
64             // gamma(n+1) -> n! for postitive n
65             if (x.info(info_flags::posint)) {
66                 return factorial(ex_to_numeric(x).sub(_num1()));
67             } else {
68                 throw (std::domain_error("gamma_eval(): simple pole"));
69             }
70         }
71         // trap half integer arguments:
72         if ((x*2).info(info_flags::integer)) {
73             // trap positive x==(n+1/2)
74             // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
75             if ((x*_ex2()).info(info_flags::posint)) {
76                 numeric n = ex_to_numeric(x).sub(_num1_2());
77                 numeric coefficient = doublefactorial(n.mul(_num2()).sub(_num1()));
78                 coefficient = coefficient.div(pow(_num2(),n));
79                 return coefficient * pow(Pi,_ex1_2());
80             } else {
81                 // trap negative x==(-n+1/2)
82                 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
83                 numeric n = abs(ex_to_numeric(x).sub(_num1_2()));
84                 numeric coefficient = pow(_num_2(), n);
85                 coefficient = coefficient.div(doublefactorial(n.mul(_num2()).sub(_num1())));;
86                 return coefficient*power(Pi,_ex1_2());
87             }
88         }
89         //  gamma_evalf should be called here once it becomes available
90     }
91     
92     return gamma(x).hold();
93 }    
94
95 static ex gamma_diff(const ex & x, unsigned diff_param)
96 {
97     GINAC_ASSERT(diff_param==0);
98     
99     // d/dx  log(gamma(x)) -> psi(x)
100     // d/dx  gamma(x) -> psi(x)*gamma(x)
101     return psi(x)*gamma(x);
102 }
103
104 static ex gamma_series(const ex & x, const symbol & s, const ex & pt, int order)
105 {
106     // method:
107     // Taylor series where there is no pole falls back to psi function
108     // evaluation.
109     // On a pole at -m use the recurrence relation
110     //   gamma(x) == gamma(x+1) / x
111     // from which follows
112     //   series(gamma(x),x,-m,order) ==
113     //   series(gamma(x+m+1)/(x*(x+1)*...*(x+m)),x,-m,order+1);
114     const ex x_pt = x.subs(s==pt);
115     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
116         throw do_taylor();  // caught by function::series()
117     // if we got here we have to care for a simple pole at -m:
118     numeric m = -ex_to_numeric(x_pt);
119     ex ser_numer = gamma(x+m+_ex1());
120     ex ser_denom = _ex1();
121     for (numeric p; p<=m; ++p)
122         ser_denom *= x+p;
123     return (ser_numer/ser_denom).series(s, pt, order+1);
124 }
125
126 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
127
128 //////////
129 // Beta-function
130 //////////
131
132 static ex beta_evalf(const ex & x, const ex & y)
133 {
134     BEGIN_TYPECHECK
135         TYPECHECK(x,numeric)
136         TYPECHECK(y,numeric)
137     END_TYPECHECK(beta(x,y))
138     
139     return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
140         / gamma(ex_to_numeric(x+y));
141 }
142
143 static ex beta_eval(const ex & x, const ex & y)
144 {
145     if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
146         // treat all problematic x and y that may not be passed into gamma,
147         // because they would throw there although beta(x,y) is well-defined
148         // using the formula beta(x,y) == (-1)^y * beta(1-x-y, y)
149         numeric nx(ex_to_numeric(x));
150         numeric ny(ex_to_numeric(y));
151         if (nx.is_real() && nx.is_integer() &&
152             ny.is_real() && ny.is_integer()) {
153             if (nx.is_negative()) {
154                 if (nx<=-ny)
155                     return pow(_num_1(), ny)*beta(1-x-y, y);
156                 else
157                     throw (std::domain_error("beta_eval(): simple pole"));
158             }
159             if (ny.is_negative()) {
160                 if (ny<=-nx)
161                     return pow(_num_1(), nx)*beta(1-y-x, x);
162                 else
163                     throw (std::domain_error("beta_eval(): simple pole"));
164             }
165             return gamma(x)*gamma(y)/gamma(x+y);
166         }
167         // no problem in numerator, but denominator has pole:
168         if ((nx+ny).is_real() &&
169             (nx+ny).is_integer() &&
170             !(nx+ny).is_positive())
171              return _ex0();
172         // everything is ok:
173         return gamma(x)*gamma(y)/gamma(x+y);
174     }
175     
176     return beta(x,y).hold();
177 }
178
179 static ex beta_diff(const ex & x, const ex & y, unsigned diff_param)
180 {
181     GINAC_ASSERT(diff_param<2);
182     ex retval;
183     
184     // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
185     if (diff_param==0)
186         retval = (psi(x)-psi(x+y))*beta(x,y);
187     // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
188     if (diff_param==1)
189         retval = (psi(y)-psi(x+y))*beta(x,y);
190     return retval;
191 }
192
193 static ex beta_series(const ex & x, const ex & y, const symbol & s, const ex & pt, int order)
194 {
195     // method:
196     // Taylor series where there is no pole of one of the gamma functions
197     // falls back to beta function evaluation.  Otherwise, fall back to
198     // gamma series directly.
199     // FIXME: this could need some testing, maybe it's wrong in some cases?
200     const ex x_pt = x.subs(s==pt);
201     const ex y_pt = y.subs(s==pt);
202     ex x_ser, y_ser, xy_ser;
203     if ((!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive)) &&
204         (!y_pt.info(info_flags::integer) || y_pt.info(info_flags::positive)))
205         throw do_taylor();  // caught by function::series()
206     // trap the case where x is on a pole directly:
207     if (x.info(info_flags::integer) && !x.info(info_flags::positive))
208         x_ser = gamma(x+s).series(s,pt,order);
209     else
210         x_ser = gamma(x).series(s,pt,order);
211     // trap the case where y is on a pole directly:
212     if (y.info(info_flags::integer) && !y.info(info_flags::positive))
213         y_ser = gamma(y+s).series(s,pt,order);
214     else
215         y_ser = gamma(y).series(s,pt,order);
216     // trap the case where y is on a pole directly:
217     if ((x+y).info(info_flags::integer) && !(x+y).info(info_flags::positive))
218         xy_ser = gamma(y+x+s).series(s,pt,order);
219     else
220         xy_ser = gamma(y+x).series(s,pt,order);
221     // compose the result:
222     return (x_ser*y_ser/xy_ser).series(s,pt,order);
223 }
224
225 REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, beta_series);
226
227 //////////
228 // Psi-function (aka digamma-function)
229 //////////
230
231 static ex psi1_evalf(const ex & x)
232 {
233     BEGIN_TYPECHECK
234         TYPECHECK(x,numeric)
235     END_TYPECHECK(psi(x))
236     
237     return psi(ex_to_numeric(x));
238 }
239
240 /** Evaluation of digamma-function psi(x).
241  *  Somebody ought to provide some good numerical evaluation some day... */
242 static ex psi1_eval(const ex & x)
243 {
244     if (x.info(info_flags::numeric)) {
245         numeric nx = ex_to_numeric(x);
246         if (nx.is_integer()) {
247             // integer case 
248             if (nx.is_positive()) {
249                 // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
250                 numeric rat(0);
251                 for (numeric i(nx+_num_1()); i.is_positive(); --i)
252                     rat += i.inverse();
253                 return rat-EulerGamma;
254             } else {
255                 // for non-positive integers there is a pole:
256                 throw (std::domain_error("psi_eval(): simple pole"));
257             }
258         }
259         if ((_num2()*nx).is_integer()) {
260             // half integer case
261             if (nx.is_positive()) {
262                 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
263                 numeric rat(0);
264                 for (numeric i((nx+_num_1())*_num2()); i.is_positive(); i-=_num2())
265                                       rat += _num2()*i.inverse();
266                                       return rat-EulerGamma-_ex2()*log(_ex2());
267             } else {
268                 // use the recurrence relation
269                 //   psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
270                 // to relate psi(-m-1/2) to psi(1/2):
271                 //   psi(-m-1/2) == psi(1/2) + r
272                 // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
273                 numeric recur(0);
274                 for (numeric p(nx); p<0; ++p)
275                     recur -= pow(p, _num_1());
276                 return recur+psi(_ex1_2());
277             }
278         }
279         //  psi1_evalf should be called here once it becomes available
280     }
281     
282     return psi(x).hold();
283 }
284
285 static ex psi1_diff(const ex & x, unsigned diff_param)
286 {
287     GINAC_ASSERT(diff_param==0);
288     
289     // d/dx psi(x) -> psi(1,x)
290     return psi(_ex1(), x);
291 }
292
293 static ex psi1_series(const ex & x, const symbol & s, const ex & pt, int order)
294 {
295     // method:
296     // Taylor series where there is no pole falls back to polygamma function
297     // evaluation.
298     // On a pole at -m use the recurrence relation
299     //   psi(x) == psi(x+1) - 1/z
300     // from which follows
301     //   series(psi(x),x,-m,order) ==
302     //   series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
303     const ex x_pt = x.subs(s==pt);
304     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
305         throw do_taylor();  // caught by function::series()
306     // if we got here we have to care for a simple pole at -m:
307     numeric m = -ex_to_numeric(x_pt);
308     ex recur;
309     for (numeric p; p<=m; ++p)
310         recur += power(x+p,_ex_1());
311     return (psi(x+m+_ex1())-recur).series(s, pt, order);
312 }
313
314 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, psi1_series);
315
316 //////////
317 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
318 //////////
319
320 static ex psi2_evalf(const ex & n, const ex & x)
321 {
322     BEGIN_TYPECHECK
323         TYPECHECK(n,numeric)
324         TYPECHECK(x,numeric)
325     END_TYPECHECK(psi(n,x))
326     
327     return psi(ex_to_numeric(n), ex_to_numeric(x));
328 }
329
330 /** Evaluation of polygamma-function psi(n,x). 
331  *  Somebody ought to provide some good numerical evaluation some day... */
332 static ex psi2_eval(const ex & n, const ex & x)
333 {
334     // psi(0,x) -> psi(x)
335     if (n.is_zero())
336         return psi(x);
337     // psi(-1,x) -> log(gamma(x))
338     if (n.is_equal(_ex_1()))
339         return log(gamma(x));
340     if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
341         x.info(info_flags::numeric)) {
342         numeric nn = ex_to_numeric(n);
343         numeric nx = ex_to_numeric(x);
344         if (nx.is_integer()) {
345             // integer case 
346             if (nx.is_equal(_num1()))
347                 // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
348                 return pow(_num_1(),nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
349             if (nx.is_positive()) {
350                 // use the recurrence relation
351                 //   psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
352                 // to relate psi(n,m) to psi(n,1):
353                 //   psi(n,m) == psi(n,1) + r
354                 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
355                 numeric recur(0);
356                 for (numeric p(1); p<nx; ++p)
357                     recur += pow(p, -nn+_num_1());
358                 recur *= factorial(nn)*pow(_num_1(), nn);
359                 return recur+psi(n,_ex1());
360             } else {
361                 // for non-positive integers there is a pole:
362                 throw (std::domain_error("psi2_eval(): pole"));
363             }
364         }
365         if ((_num2()*nx).is_integer()) {
366             // half integer case
367             if (nx.is_equal(_num1_2()))
368                 // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
369                 return pow(_num_1(),nn+_num1())*factorial(nn)*(pow(_num2(),nn+_num1()) + _num_1())*zeta(ex(nn+_num1()));
370             if (nx.is_positive()) {
371                 numeric m = nx - _num1_2();
372                 // use the multiplication formula
373                 //   psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
374                 // to revert to positive integer case
375                 return psi(n,_num2()*m)*pow(_num2(),nn+_num1())-psi(n,m);
376             } else {
377                 // use the recurrence relation
378                 //   psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
379                 // to relate psi(n,-m-1/2) to psi(n,1/2):
380                 //   psi(n,-m-1/2) == psi(n,1/2) + r
381                 // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
382                 numeric recur(0);
383                 for (numeric p(nx); p<0; ++p)
384                     recur += pow(p, -nn+_num_1());
385                 recur *= factorial(nn)*pow(_num_1(), nn+_num_1());
386                 return recur+psi(n,_ex1_2());
387             }
388         }
389         //  psi2_evalf should be called here once it becomes available
390     }
391     
392     return psi(n, x).hold();
393 }    
394
395 static ex psi2_diff(const ex & n, const ex & x, unsigned diff_param)
396 {
397     GINAC_ASSERT(diff_param<2);
398     
399     if (diff_param==0) {
400         // d/dn psi(n,x)
401         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
402     }
403     // d/dx psi(n,x) -> psi(n+1,x)
404     return psi(n+_ex1(), x);
405 }
406
407 static ex psi2_series(const ex & n, const ex & x, const symbol & s, const ex & pt, int order)
408 {
409     // method:
410     // Taylor series where there is no pole falls back to polygamma function
411     // evaluation.
412     // On a pole at -m use the recurrence relation
413     //   psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
414     // from which follows
415     //   series(psi(x),x,-m,order) == 
416     //   series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
417     //                                      ... + (x+m)^(-n-1))),x,-m,order);
418     const ex x_pt = x.subs(s==pt);
419     if (!x_pt.info(info_flags::integer) || x_pt.info(info_flags::positive))
420         throw do_taylor();  // caught by function::series()
421     // if we got here we have to care for a pole of order n+1 at -m:
422     numeric m = -ex_to_numeric(x_pt);
423     ex recur;
424     for (numeric p; p<=m; ++p)
425         recur += power(x+p,-n+_ex_1());
426     recur *= factorial(n)*power(_ex_1(),n);
427     return (psi(n, x+m+_ex1())-recur).series(s, pt, order);
428 }
429
430 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, psi2_series);
431
432 #ifndef NO_GINAC_NAMESPACE
433 } // namespace GiNaC
434 #endif // ndef NO_GINAC_NAMESPACE