]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
- more logic on the trigonometric function stuff.
[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 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 "series.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     }
90     return gamma(x).hold();
91 }    
92
93 static ex gamma_diff(const ex & x, unsigned diff_param)
94 {
95     GINAC_ASSERT(diff_param==0);
96     
97     // d/dx  log(gamma(x)) -> psi(x)
98     // d/dx  gamma(x) -> psi(x)*gamma(x)
99     return psi(x)*gamma(x);
100 }
101
102 static ex gamma_series(const ex & x, const symbol & s, const ex & point, int order)
103 {
104     // method:
105     // Taylor series where there is no pole falls back to psi function
106     // evaluation.
107     // On a pole at -m use the recurrence relation
108     //   gamma(x) == gamma(x+1) / x
109     // from which follows
110     //   series(gamma(x),x,-m,order) ==
111     //   series(gamma(x+m+1)/(x*(x+1)...*(x+m)),x,-m,order+1);
112     ex xpoint = x.subs(s==point);
113     if (!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive))
114         throw do_taylor();  // caught by function::series()
115     // if we got here we have to care for a simple pole at -m:
116     numeric m = -ex_to_numeric(xpoint);
117     ex ser_numer = gamma(x+m+_ex1());
118     ex ser_denom = _ex1();
119     for (numeric p; p<=m; ++p)
120         ser_denom *= x+p;
121     return (ser_numer/ser_denom).series(s, point, order+1);
122 }
123
124 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
125
126 //////////
127 // Beta-function
128 //////////
129
130 static ex beta_evalf(const ex & x, const ex & y)
131 {
132     BEGIN_TYPECHECK
133         TYPECHECK(x,numeric)
134         TYPECHECK(y,numeric)
135     END_TYPECHECK(beta(x,y))
136     
137     return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
138         / gamma(ex_to_numeric(x+y));
139 }
140
141 static ex beta_eval(const ex & x, const ex & y)
142 {
143     if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
144         numeric nx(ex_to_numeric(x));
145         numeric ny(ex_to_numeric(y));
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         if (nx.is_real() && nx.is_integer() &&
150             ny.is_real() && ny.is_integer()) {
151             if (nx.is_negative()) {
152                 if (nx<=-ny)
153                     return pow(_num_1(), ny)*beta(1-x-y, y);
154                 else
155                     throw (std::domain_error("beta_eval(): simple pole"));
156             }
157             if (ny.is_negative()) {
158                 if (ny<=-nx)
159                     return pow(_num_1(), nx)*beta(1-y-x, x);
160                 else
161                     throw (std::domain_error("beta_eval(): simple pole"));
162             }
163             return gamma(x)*gamma(y)/gamma(x+y);
164         }
165         // no problem in numerator, but denominator has pole:
166         if ((nx+ny).is_real() &&
167             (nx+ny).is_integer() &&
168             !(nx+ny).is_positive())
169              return _ex0();
170         // everything is ok:
171         return gamma(x)*gamma(y)/gamma(x+y);
172     }
173     
174     return beta(x,y).hold();
175 }
176
177 static ex beta_diff(const ex & x, const ex & y, unsigned diff_param)
178 {
179     GINAC_ASSERT(diff_param<2);
180     ex retval;
181     
182     // d/dx beta(x,y) -> (psi(x)-psi(x+y)) * beta(x,y)
183     if (diff_param==0)
184         retval = (psi(x)-psi(x+y))*beta(x,y);
185     // d/dy beta(x,y) -> (psi(y)-psi(x+y)) * beta(x,y)
186     if (diff_param==1)
187         retval = (psi(y)-psi(x+y))*beta(x,y);
188     return retval;
189 }
190
191 static ex beta_series(const ex & x, const ex & y, const symbol & s, const ex & point, int order)
192 {
193     // method:
194     // Taylor series where there is no pole falls back to beta function
195     // evaluation.
196     // On a pole at -m use the recurrence relation
197     //   gamma(x) == gamma(x+1) / x
198     // from which follows
199     //   series(gamma(x),x,-m,order) ==
200     //   series(gamma(x+m+1)/(x*(x+1)...*(x+m)),x,-m,order+1);
201     ex xpoint = x.subs(s==point);
202     ex ypoint = y.subs(s==point);
203     if ((!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive)) &&
204         (!ypoint.info(info_flags::integer) || ypoint.info(info_flags::positive)))
205         throw do_taylor();  // caught by function::series()
206     // if we got here we have to care for a simple pole at -m:
207     throw (std::domain_error("beta_series(): please code me"));
208     /*numeric m = -ex_to_numeric(xpoint);
209      *ex ser_numer = gamma(x+m+_ex1());
210      *ex ser_denom = _ex1();
211      *for (numeric p; p<=m; ++p)
212      *    ser_denom *= x+p;
213      *return (ser_numer/ser_denom).series(s, point, order+1);*/
214 }
215
216 REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, beta_series);
217
218 //////////
219 // Psi-function (aka digamma-function)
220 //////////
221
222 static ex psi1_evalf(const ex & x)
223 {
224     BEGIN_TYPECHECK
225         TYPECHECK(x,numeric)
226     END_TYPECHECK(psi(x))
227     
228     return psi(ex_to_numeric(x));
229 }
230
231 /** Evaluation of digamma-function psi(x).
232  *  Somebody ought to provide some good numerical evaluation some day... */
233 static ex psi1_eval(const ex & x)
234 {
235     if (x.info(info_flags::numeric)) {
236         if (x.info(info_flags::integer) && !x.info(info_flags::positive))
237             throw (std::domain_error("psi_eval(): simple pole"));
238         if (x.info(info_flags::positive)) {
239             // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
240             if (x.info(info_flags::integer)) {
241                 numeric rat(0);
242                 for (numeric i(ex_to_numeric(x)-_num1()); i.is_positive(); --i)
243                     rat += i.inverse();
244                 return rat-EulerGamma;
245             }
246             // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
247             if ((_ex2()*x).info(info_flags::integer)) {
248                 numeric rat(0);
249                 for (numeric i((ex_to_numeric(x)-_num1())*_num2()); i.is_positive(); i-=_num2())
250                     rat += _num2()*i.inverse();
251                 return rat-EulerGamma-_ex2()*log(_ex2());
252             }
253             if (x.compare(_ex1())==1) {
254                 // should call numeric, since >1
255             }
256         }
257     }
258     return psi(x).hold();
259 }
260
261 static ex psi1_diff(const ex & x, unsigned diff_param)
262 {
263     GINAC_ASSERT(diff_param==0);
264     
265     // d/dx psi(x) -> psi(1,x)
266     return psi(_ex1(), x);
267 }
268
269 static ex psi1_series(const ex & x, const symbol & s, const ex & point, int order)
270 {
271     // method:
272     // Taylor series where there is no pole falls back to polygamma function
273     // evaluation.
274     // On a pole at -m use the recurrence relation
275     //   psi(x) == psi(x+1) - 1/z
276     // from which follows
277     //   series(psi(x),x,-m,order) ==
278     //   series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x,-m,order);
279     ex xpoint = x.subs(s==point);
280     if (!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive))
281         throw do_taylor();  // caught by function::series()
282     // if we got here we have to care for a simple pole at -m:
283     numeric m = -ex_to_numeric(xpoint);
284     ex recur;
285     for (numeric p; p<=m; ++p)
286         recur += power(x+p,_ex_1());
287     return (psi(x+m+_ex1())-recur).series(s, point, order);
288 }
289
290 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, psi1_series);
291
292 //////////
293 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
294 //////////
295
296 static ex psi2_evalf(const ex & n, const ex & x)
297 {
298     BEGIN_TYPECHECK
299         TYPECHECK(n,numeric)
300         TYPECHECK(x,numeric)
301     END_TYPECHECK(psi(n,x))
302     
303     return psi(ex_to_numeric(n), ex_to_numeric(x));
304 }
305
306 /** Evaluation of polygamma-function psi(n,x). 
307  *  Somebody ought to provide some good numerical evaluation some day... */
308 static ex psi2_eval(const ex & n, const ex & x)
309 {
310     // psi(0,x) -> psi(x)
311     if (n.is_zero())
312         return psi(x);
313     // psi(-1,x) -> log(gamma(x))
314     if (n.is_equal(_ex_1()))
315         return log(gamma(x));
316     if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
317         x.info(info_flags::numeric)) {
318         numeric nn = ex_to_numeric(n);
319         numeric nx = ex_to_numeric(x);
320         if (nx.is_integer()) {
321             if (nx.is_equal(_num1()))
322                 return pow(_num_1(), nn+_num1())*factorial(nn)*zeta(ex(nn+_num1()));
323             if (nx.is_positive()) {
324                 // use the recurrence relation
325                 //   psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
326                 // to relate psi(n,m) to psi(n,1):
327                 //   psi(n,m) == psi(n,1) + r
328                 // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
329                 numeric recur;
330                 for (numeric p(1); p<nx; ++p)
331                     recur += pow(p, -nn+_num_1());
332                 recur *= factorial(nn)*pow(_num_1(), nn);
333                 return recur+psi(n,_ex1());
334             }
335             // for non-positive integers there is a pole:
336             throw (std::domain_error("psi2_eval(): pole"));
337         }
338     }
339     return psi(n, x).hold();
340 }    
341
342 static ex psi2_diff(const ex & n, const ex & x, unsigned diff_param)
343 {
344     GINAC_ASSERT(diff_param<2);
345     
346     if (diff_param==0) {
347         // d/dn psi(n,x)
348         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
349     }
350     // d/dx psi(n,x) -> psi(n+1,x)
351     return psi(n+_ex1(), x);
352 }
353
354 static ex psi2_series(const ex & n, const ex & x, const symbol & s, const ex & point, int order)
355 {
356     // method:
357     // Taylor series where there is no pole falls back to polygamma function
358     // evaluation.
359     // On a pole at -m use the recurrence relation
360     //   psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
361     // from which follows
362     //   series(psi(x),x,-m,order) == 
363     //   series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
364     //                                      ... + (x+m)^(-n-1))),x,-m,order);
365     ex xpoint = x.subs(s==point);
366     if (!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive))
367         throw do_taylor();  // caught by function::series()
368     // if we got here we have to care for a pole of order n+1 at -m:
369     numeric m = -ex_to_numeric(xpoint);
370     ex recur;
371     for (numeric p; p<=m; ++p)
372         recur += power(x+p,-n+_ex_1());
373     recur *= factorial(n)*power(_ex_1(),n);
374     return (psi(n, x+m+_ex1())-recur).series(s, point, order);
375 }
376
377 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, psi2_series);
378
379 #ifndef NO_GINAC_NAMESPACE
380 } // namespace GiNaC
381 #endif // ndef NO_GINAC_NAMESPACE