]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
7f3b9f5c924fcb7700e8dc4a8fcad4f4f2bd6c04
[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 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
46  *  arguments and that's it. Somebody ought to provide some good numerical
47  *  evaluation some day...
48  *
49  *  @exception std::domain_error("gamma_eval(): simple pole") */
50 static ex gamma_eval(ex const & x)
51 {
52     if (x.info(info_flags::numeric)) {
53         // trap integer arguments:
54         if (x.info(info_flags::integer)) {
55             // gamma(n+1) -> n! for postitive n
56             if (x.info(info_flags::posint)) {
57                 return factorial(ex_to_numeric(x).sub(numONE()));
58             } else {
59                 throw (std::domain_error("gamma_eval(): simple pole"));
60             }
61         }
62         // trap half integer arguments:
63         if ((x*2).info(info_flags::integer)) {
64             // trap positive x==(n+1/2)
65             // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
66             if ((x*2).info(info_flags::posint)) {
67                 numeric n = ex_to_numeric(x).sub(numHALF());
68                 numeric coefficient = doublefactorial(n.mul(numTWO()).sub(numONE()));
69                 coefficient = coefficient.div(numTWO().power(n));
70                 return coefficient * pow(Pi,numHALF());
71             } else {
72                 // trap negative x==(-n+1/2)
73                 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
74                 numeric n = abs(ex_to_numeric(x).sub(numHALF()));
75                 numeric coefficient = numeric(-2).power(n);
76                 coefficient = coefficient.div(doublefactorial(n.mul(numTWO()).sub(numONE())));;
77                 return coefficient*sqrt(Pi);
78             }
79         }
80     }
81     return gamma(x).hold();
82 }    
83
84 static ex gamma_evalf(ex const & x)
85 {
86     BEGIN_TYPECHECK
87         TYPECHECK(x,numeric)
88     END_TYPECHECK(gamma(x))
89     
90     return gamma(ex_to_numeric(x));
91 }
92
93 static ex gamma_diff(ex const & x, unsigned diff_param)
94 {
95     GINAC_ASSERT(diff_param==0);
96     
97     return psi(x)*gamma(x);  // diff(log(gamma(x)),x)==psi(x)
98 }
99
100 static ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
101 {
102     // method:
103     // Taylor series where there is no pole falls back to psi functions.
104     // On a pole at -n use the identity
105     //   series(GAMMA(x),x=-n,order) ==
106     //   series(GAMMA(x+n+1)/(x*(x+1)...*(x+n)),x=-n,order+1);
107     ex xpoint = x.subs(s==point);
108     if (!xpoint.info(info_flags::integer) || xpoint.info(info_flags::positive))
109         throw do_taylor();
110     // if we got here we have to care for a simple pole at -n:
111     numeric n = -ex_to_numeric(xpoint);
112     ex ser_numer = gamma(x+n+exONE());
113     ex ser_denom = exONE();
114     for (numeric p; p<=n; ++p)
115         ser_denom *= x+p;
116     return (ser_numer/ser_denom).series(s, point, order+1);
117 }
118
119 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
120
121 //////////
122 // Beta-function
123 //////////
124
125 static ex beta_eval(ex const & x, ex const & y)
126 {
127     if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
128         numeric nx(ex_to_numeric(x));
129         numeric ny(ex_to_numeric(y));
130         // treat all problematic x and y that may not be passed into gamma,
131         // because they would throw there although beta(x,y) is well-defined:
132         if (nx.is_real() && nx.is_integer() &&
133             ny.is_real() && ny.is_integer()) {
134             if (nx.is_negative()) {
135                 if (nx<=-ny)
136                     return numMINUSONE().power(ny)*beta(1-x-y, y);
137                 else
138                     throw (std::domain_error("beta_eval(): simple pole"));
139             }
140             if (ny.is_negative()) {
141                 if (ny<=-nx)
142                     return numMINUSONE().power(nx)*beta(1-y-x, x);
143                 else
144                     throw (std::domain_error("beta_eval(): simple pole"));
145             }
146             return gamma(x)*gamma(y)/gamma(x+y);
147         }
148         // no problem in numerator, but denominator has pole:
149         if ((nx+ny).is_real() &&
150             (nx+ny).is_integer() &&
151             !(nx+ny).is_positive())
152             return exZERO();
153         return gamma(x)*gamma(y)/gamma(x+y);
154     }
155     return beta(x,y).hold();
156 }
157
158 static ex beta_evalf(ex const & x, ex const & y)
159 {
160     BEGIN_TYPECHECK
161         TYPECHECK(x,numeric)
162         TYPECHECK(y,numeric)
163     END_TYPECHECK(beta(x,y))
164     
165     return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
166         / gamma(ex_to_numeric(x+y));
167 }
168
169 static ex beta_diff(ex const & x, ex const & y, unsigned diff_param)
170 {
171     GINAC_ASSERT(diff_param<2);
172     ex retval;
173     
174     if (diff_param==0)  // d/dx beta(x,y)
175         retval = (psi(x)-psi(x+y))*beta(x,y);
176     if (diff_param==1)  // d/dy beta(x,y)
177         retval = (psi(y)-psi(x+y))*beta(x,y);
178     return retval;
179 }
180
181 REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, NULL);
182
183 //////////
184 // Psi-function (aka polygamma-function)
185 //////////
186
187 /** Evaluation of polygamma-function psi(x). 
188  *  Somebody ought to provide some good numerical evaluation some day... */
189 static ex psi1_eval(ex const & x)
190 {
191     if (x.info(info_flags::numeric)) {
192         if (x.info(info_flags::integer) && !x.info(info_flags::positive))
193             throw (std::domain_error("psi_eval(): simple pole"));
194         if (x.info(info_flags::positive)) {
195             // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
196             if (x.info(info_flags::integer)) {
197                 numeric rat(0);
198                 for (numeric i(ex_to_numeric(x)-numONE()); i.is_positive(); --i)
199                     rat += i.inverse();
200                 return rat-EulerGamma;
201             }
202             // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
203             if ((exTWO()*x).info(info_flags::integer)) {
204                 numeric rat(0);
205                 for (numeric i((ex_to_numeric(x)-numONE())*numTWO()); i.is_positive(); i-=numTWO())
206                     rat += numTWO()*i.inverse();
207                 return rat-EulerGamma-exTWO()*log(exTWO());
208             }
209             if (x.compare(exONE())==1) {
210                 // should call numeric, since >1
211             }
212         }
213     }
214     return psi(x).hold();
215 }
216
217 static ex psi1_evalf(ex const & x)
218 {
219     BEGIN_TYPECHECK
220         TYPECHECK(x,numeric)
221     END_TYPECHECK(psi(x))
222     
223     return psi(ex_to_numeric(x));
224 }
225
226 static ex psi1_diff(ex const & x, unsigned diff_param)
227 {
228     GINAC_ASSERT(diff_param==0);
229     
230     return psi(exONE(), x);
231 }
232
233 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, NULL);
234
235 //////////
236 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
237 //////////
238
239 /** Evaluation of polygamma-function psi(n,x). 
240  *  Somebody ought to provide some good numerical evaluation some day... */
241 static ex psi2_eval(ex const & n, ex const & x)
242 {
243     // psi(0,x) -> psi(x)
244     if (n.is_zero())
245         return psi(x);
246     // psi(-1,x) -> log(gamma(x))
247     if (n.is_equal(exMINUSONE()))
248         return log(gamma(x));
249     if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
250         x.info(info_flags::numeric)) {
251         numeric nn = ex_to_numeric(n);
252         numeric nx = ex_to_numeric(x);
253         if (x.is_equal(exONE()))
254             return numMINUSONE().power(nn+numONE())*factorial(nn)*zeta(ex(nn+numONE()));
255     }
256     return psi(n, x).hold();
257 }    
258
259 static ex psi2_evalf(ex const & n, ex const & x)
260 {
261     BEGIN_TYPECHECK
262         TYPECHECK(n,numeric)
263         TYPECHECK(x,numeric)
264     END_TYPECHECK(psi(n,x))
265     
266     return psi(ex_to_numeric(n), ex_to_numeric(x));
267 }
268
269 static ex psi2_diff(ex const & n, ex const & x, unsigned diff_param)
270 {
271     GINAC_ASSERT(diff_param<2);
272     
273     if (diff_param==0) {
274         // d/dn psi(n,x)
275         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
276     }
277     // d/dx psi(n,x)
278     return psi(n+1, x);
279 }
280
281 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, NULL);
282
283 #ifndef NO_GINAC_NAMESPACE
284 } // namespace GiNaC
285 #endif // ndef NO_GINAC_NAMESPACE