]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
2fd58cce517fe04d66c8bf158aa95878de6635ea
[ginac.git] / ginac / inifcns_gamma.cpp
1 /** @file inifcns_gamma.cpp
2  *
3  *  Implementation of Gamma-function, Polygamma-functions, and some related
4  *  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                 return numZERO();  // Infinity. Throw? What?
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 // Psi-function (aka polygamma-function)
111 //////////
112
113 /** Evaluation of polygamma-function psi(x). 
114  *  Somebody ought to provide some good numerical evaluation some day... */
115 static ex psi1_eval(ex const & x)
116 {
117     if (x.info(info_flags::numeric)) {
118         if (x.info(info_flags::integer) && !x.info(info_flags::positive))
119             throw (std::domain_error("psi_eval(): simple pole"));
120         if (x.info(info_flags::positive)) {
121             // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
122             if (x.info(info_flags::integer)) {
123                 numeric rat(0);
124                 for (numeric i(ex_to_numeric(x)-numONE()); i.is_positive(); --i)
125                     rat += i.inverse();
126                 return rat-EulerGamma;
127             }
128             // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
129             if ((exTWO()*x).info(info_flags::integer)) {
130                 numeric rat(0);
131                 for (numeric i((ex_to_numeric(x)-numONE())*numTWO()); i.is_positive(); i-=numTWO())
132                     rat += numTWO()*i.inverse();
133                 return rat-EulerGamma-exTWO()*log(exTWO());
134             }
135             if (x.compare(exONE())==1) {
136                 // should call numeric, since >1
137             }
138         }
139     }
140     return psi(x).hold();
141 }
142
143 static ex psi1_evalf(ex const & x)
144 {
145     BEGIN_TYPECHECK
146         TYPECHECK(x,numeric)
147     END_TYPECHECK(psi(x))
148     
149     return psi(ex_to_numeric(x));
150 }
151
152 static ex psi1_diff(ex const & x, unsigned diff_param)
153 {
154     GINAC_ASSERT(diff_param==0);
155     
156     return psi(exONE(), x);
157 }
158
159 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, NULL);
160
161 //////////
162 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
163 //////////
164
165 /** Evaluation of polygamma-function psi(n,x). 
166  *  Somebody ought to provide some good numerical evaluation some day... */
167 static ex psi2_eval(ex const & n, ex const & x)
168 {
169     // psi(0,x) -> psi(x)
170     if (n.is_zero())
171         return psi(x);
172     if (n.info(info_flags::numeric) && x.info(info_flags::numeric)) {
173         // do some stuff...
174     }
175     return psi(n, x).hold();
176 }    
177
178 static ex psi2_evalf(ex const & n, ex const & x)
179 {
180     BEGIN_TYPECHECK
181         TYPECHECK(n,numeric)
182         TYPECHECK(x,numeric)
183     END_TYPECHECK(psi(n,x))
184     
185     return psi(ex_to_numeric(n), ex_to_numeric(x));
186 }
187
188 static ex psi2_diff(ex const & n, ex const & x, unsigned diff_param)
189 {
190     GINAC_ASSERT(diff_param<2);
191     
192     if (diff_param==0) {
193         // d/dn psi(n,x)
194         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
195     }
196     // d/dx psi(n,x)
197     return psi(n+1, x);
198 }
199
200 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, NULL);
201
202 #ifndef NO_GINAC_NAMESPACE
203 } // namespace GiNaC
204 #endif // ndef NO_GINAC_NAMESPACE