]> www.ginac.de Git - ginac.git/blob - ginac/inifcns_gamma.cpp
- added prefix and postfix increment and decrement operators for class numeric
[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 namespace GiNaC {
35
36 //////////
37 // Gamma-function
38 //////////
39
40 /** Evaluation of gamma(x). Knows about integer arguments, half-integer
41  *  arguments and that's it. Somebody ought to provide some good numerical
42  *  evaluation some day...
43  *
44  *  @exception fail_numeric("complex_infinity") or something similar... */
45 static ex gamma_eval(ex const & x)
46 {
47     if (x.info(info_flags::numeric)) {
48         // trap integer arguments:
49         if (x.info(info_flags::integer)) {
50             // gamma(n+1) -> n! for postitive n
51             if (x.info(info_flags::posint)) {
52                 return factorial(ex_to_numeric(x).sub(numONE()));
53             } else {
54                 return numZERO();  // Infinity. Throw? What?
55             }
56         }
57         // trap half integer arguments:
58         if ((x*2).info(info_flags::integer)) {
59             // trap positive x=(n+1/2)
60             // gamma(n+1/2) -> Pi^(1/2)*(1*3*..*(2*n-1))/(2^n)
61             if ((x*2).info(info_flags::posint)) {
62                 numeric n = ex_to_numeric(x).sub(numHALF());
63                 numeric coefficient = doublefactorial(n.mul(numTWO()).sub(numONE()));
64                 coefficient = coefficient.div(numTWO().power(n));
65                 return coefficient * pow(Pi,numHALF());
66             } else {
67                 // trap negative x=(-n+1/2)
68                 // gamma(-n+1/2) -> Pi^(1/2)*(-2)^n/(1*3*..*(2*n-1))
69                 numeric n = abs(ex_to_numeric(x).sub(numHALF()));
70                 numeric coefficient = numeric(-2).power(n);
71                 coefficient = coefficient.div(doublefactorial(n.mul(numTWO()).sub(numONE())));;
72                 return coefficient*sqrt(Pi);
73             }
74         }
75     }
76     return gamma(x).hold();
77 }    
78
79 static ex gamma_evalf(ex const & x)
80 {
81     BEGIN_TYPECHECK
82         TYPECHECK(x,numeric)
83     END_TYPECHECK(gamma(x))
84     
85     return gamma(ex_to_numeric(x));
86 }
87
88 static ex gamma_diff(ex const & x, unsigned diff_param)
89 {
90     GINAC_ASSERT(diff_param==0);
91     
92     return psi(x)*gamma(x);  // diff(log(gamma(x)),x)==psi(x)
93 }
94
95 static ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
96 {
97         // FIXME: Only handle one special case for now...
98         if (x.is_equal(s) && point.is_zero()) {
99                 ex e = 1 / s - EulerGamma + s * (pow(Pi, 2) / 12 + pow(EulerGamma, 2) / 2) + Order(pow(s, 2));
100                 return e.series(s, point, order);
101         } else
102                 throw(std::logic_error("don't know the series expansion of this particular gamma function"));
103 }
104
105 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
106
107 //////////
108 // Psi-function (aka polygamma-function)
109 //////////
110
111 /** Evaluation of polygamma-function psi(x). 
112  *  Somebody ought to provide some good numerical evaluation some day... */
113 static ex psi1_eval(ex const & x)
114 {
115     if (x.info(info_flags::numeric)) {
116         if (x.info(info_flags::integer) && !x.info(info_flags::positive))
117             throw (std::domain_error("psi_eval(): simple pole"));
118         if (x.info(info_flags::positive)) {
119             // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - EulerGamma
120             if (x.info(info_flags::integer)) {
121                 numeric rat(0);
122                 for (numeric i(ex_to_numeric(x)-numONE()); i.is_positive(); --i)
123                     rat += i.inverse();
124                 return rat-EulerGamma;
125             }
126             // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
127             if ((exTWO()*x).info(info_flags::integer)) {
128                 numeric rat(0);
129                 for (numeric i((ex_to_numeric(x)-numONE())*numTWO()); i.is_positive(); i-=numTWO())
130                     rat += numTWO()*i.inverse();
131                 return rat-EulerGamma-exTWO()*log(exTWO());
132             }
133             if (x.compare(exONE())==1) {
134                 // should call numeric, since >1
135             }
136         }
137     }
138     return psi(x).hold();
139 }
140
141 static ex psi1_evalf(ex const & x)
142 {
143     BEGIN_TYPECHECK
144         TYPECHECK(x,numeric)
145     END_TYPECHECK(psi(x))
146     
147     return psi(ex_to_numeric(x));
148 }
149
150 static ex psi1_diff(ex const & x, unsigned diff_param)
151 {
152     GINAC_ASSERT(diff_param==0);
153     
154     return psi(exONE(), x);
155 }
156
157 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, NULL);
158
159 //////////
160 // Psi-functions (aka polygamma-functions)  psi(0,x)==psi(x)
161 //////////
162
163 /** Evaluation of polygamma-function psi(n,x). 
164  *  Somebody ought to provide some good numerical evaluation some day... */
165 static ex psi2_eval(ex const & n, ex const & x)
166 {
167     // psi(0,x) -> psi(x)
168     if (n.is_zero())
169         return psi(x);
170     if (n.info(info_flags::numeric) && x.info(info_flags::numeric)) {
171         // do some stuff...
172     }
173     return psi(n, x).hold();
174 }    
175
176 static ex psi2_evalf(ex const & n, ex const & x)
177 {
178     BEGIN_TYPECHECK
179         TYPECHECK(n,numeric)
180         TYPECHECK(x,numeric)
181     END_TYPECHECK(psi(n,x))
182     
183     return psi(ex_to_numeric(n), ex_to_numeric(x));
184 }
185
186 static ex psi2_diff(ex const & n, ex const & x, unsigned diff_param)
187 {
188     GINAC_ASSERT(diff_param<2);
189     
190     if (diff_param==0) {
191         // d/dn psi(n,x)
192         throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
193     }
194     // d/dx psi(n,x)
195     return psi(n+1, x);
196 }
197
198 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, NULL);
199
200 } // namespace GiNaC