]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
ecec37a603dbc208f12aeacb790a2daac18b6309
[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(ex const & 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(ex const & 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(numONE()));
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*2).info(info_flags::posint)) {
76                 numeric n = ex_to_numeric(x).sub(numHALF());
77                 numeric coefficient = doublefactorial(n.mul(numTWO()).sub(numONE()));
78                 coefficient = coefficient.div(numTWO().power(n));
79                 return coefficient * pow(Pi,numHALF());
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(numHALF()));
84                 numeric coefficient = numeric(-2).power(n);
85                 coefficient = coefficient.div(doublefactorial(n.mul(numTWO()).sub(numONE())));;
86                 return coefficient*sqrt(Pi);
87             }
88         }
89     }
90     return gamma(x).hold();
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_evalf(ex const & x, ex const & y)
126 {
127     BEGIN_TYPECHECK
128         TYPECHECK(x,numeric)
129         TYPECHECK(y,numeric)
130     END_TYPECHECK(beta(x,y))
131     
132     return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
133         / gamma(ex_to_numeric(x+y));
134 }
135
136 static ex beta_eval(ex const & x, ex const & y)
137 {
138     if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
139         numeric nx(ex_to_numeric(x));
140         numeric ny(ex_to_numeric(y));
141         // treat all problematic x and y that may not be passed into gamma,
142         // because they would throw there although beta(x,y) is well-defined:
143         if (nx.is_real() && nx.is_integer() &&
144             ny.is_real() && ny.is_integer()) {
145             if (nx.is_negative()) {
146                 if (nx<=-ny)
147                     return numMINUSONE().power(ny)*beta(1-x-y, y);
148                 else
149                     throw (std::domain_error("beta_eval(): simple pole"));
150             }
151             if (ny.is_negative()) {
152                 if (ny<=-nx)
153                     return numMINUSONE().power(nx)*beta(1-y-x, x);
154                 else
155                     throw (std::domain_error("beta_eval(): simple pole"));
156             }
157             return gamma(x)*gamma(y)/gamma(x+y);
158         }
159         // no problem in numerator, but denominator has pole:
160         if ((nx+ny).is_real() &&
161             (nx+ny).is_integer() &&
162             !(nx+ny).is_positive())
163             return exZERO();
164         return gamma(x)*gamma(y)/gamma(x+y);
165     }
166     return beta(x,y).hold();
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 static ex psi1_evalf(ex const & x)
188 {
189     BEGIN_TYPECHECK
190         TYPECHECK(x,numeric)
191     END_TYPECHECK(psi(x))
192     
193     return psi(ex_to_numeric(x));
194 }
195
196 /** Evaluation of polygamma-function psi(x). 
197  *  Somebody ought to provide some good numerical evaluation some day... */
198 static ex psi1_eval(ex const & x)
199 {
200     if (x.info(info_flags::numeric)) {
201         if (x.info(info_flags::integer) && !x.info(info_flags::positive))
202             throw (std::domain_error("psi_eval(): simple pole"));
203         if (x.info(info_flags::positive)) {
204             // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
205             if (x.info(info_flags::integer)) {
206                 numeric rat(0);
207                 for (numeric i(ex_to_numeric(x)-numONE()); i.is_positive(); --i)
208                     rat += i.inverse();
209                 return rat-EulerGamma;
210             }
211             // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
212             if ((exTWO()*x).info(info_flags::integer)) {
213                 numeric rat(0);
214                 for (numeric i((ex_to_numeric(x)-numONE())*numTWO()); i.is_positive(); i-=numTWO())
215                     rat += numTWO()*i.inverse();
216                 return rat-EulerGamma-exTWO()*log(exTWO());
217             }
218             if (x.compare(exONE())==1) {
219                 // should call numeric, since >1
220             }
221         }
222     }
223     return psi(x).hold();
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 static ex psi2_evalf(ex const & n, ex const & x)
240 {
241     BEGIN_TYPECHECK
242         TYPECHECK(n,numeric)
243         TYPECHECK(x,numeric)
244     END_TYPECHECK(psi(n,x))
245     
246     return psi(ex_to_numeric(n), ex_to_numeric(x));
247 }
248
249 /** Evaluation of polygamma-function psi(n,x). 
250  *  Somebody ought to provide some good numerical evaluation some day... */
251 static ex psi2_eval(ex const & n, ex const & x)
252 {
253     // psi(0,x) -> psi(x)
254     if (n.is_zero())
255         return psi(x);
256     // psi(-1,x) -> log(gamma(x))
257     if (n.is_equal(exMINUSONE()))
258         return log(gamma(x));
259     if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
260         x.info(info_flags::numeric)) {
261         numeric nn = ex_to_numeric(n);
262         numeric nx = ex_to_numeric(x);
263         if (x.is_equal(exONE()))
264             return numMINUSONE().power(nn+numONE())*factorial(nn)*zeta(ex(nn+numONE()));
265     }
266     return psi(n, x).hold();
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