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