1 /** @file inifcns_gamma.cpp
3 * Implementation of Gamma-function, Beta-function, Polygamma-functions, and
4 * some related stuff. */
7 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
33 #include "relational.h"
37 #ifndef NO_GINAC_NAMESPACE
39 #endif // ndef NO_GINAC_NAMESPACE
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...
49 * @exception std::domain_error("gamma_eval(): simple pole") */
50 static ex gamma_eval(ex const & x)
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()));
59 throw (std::domain_error("gamma_eval(): simple pole"));
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());
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);
81 return gamma(x).hold();
84 static ex gamma_evalf(ex const & x)
88 END_TYPECHECK(gamma(x))
90 return gamma(ex_to_numeric(x));
93 static ex gamma_diff(ex const & x, unsigned diff_param)
95 GINAC_ASSERT(diff_param==0);
97 return psi(x)*gamma(x); // diff(log(gamma(x)),x)==psi(x)
100 static ex gamma_series(ex const & x, symbol const & s, ex const & point, int order)
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))
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)
116 return (ser_numer/ser_denom).series(s, point, order+1);
119 REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
125 static ex beta_eval(ex const & x, ex const & y)
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()) {
136 return numMINUSONE().power(ny)*beta(1-x-y, y);
138 throw (std::domain_error("beta_eval(): simple pole"));
140 if (ny.is_negative()) {
142 return numMINUSONE().power(nx)*beta(1-y-x, x);
144 throw (std::domain_error("beta_eval(): simple pole"));
146 return gamma(x)*gamma(y)/gamma(x+y);
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())
153 return gamma(x)*gamma(y)/gamma(x+y);
155 return beta(x,y).hold();
158 static ex beta_evalf(ex const & x, ex const & y)
163 END_TYPECHECK(beta(x,y))
165 return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
166 / gamma(ex_to_numeric(x+y));
169 static ex beta_diff(ex const & x, ex const & y, unsigned diff_param)
171 GINAC_ASSERT(diff_param<2);
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);
181 REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, NULL);
184 // Psi-function (aka polygamma-function)
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)
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)) {
198 for (numeric i(ex_to_numeric(x)-numONE()); i.is_positive(); --i)
200 return rat-EulerGamma;
202 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
203 if ((exTWO()*x).info(info_flags::integer)) {
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());
209 if (x.compare(exONE())==1) {
210 // should call numeric, since >1
214 return psi(x).hold();
217 static ex psi1_evalf(ex const & x)
221 END_TYPECHECK(psi(x))
223 return psi(ex_to_numeric(x));
226 static ex psi1_diff(ex const & x, unsigned diff_param)
228 GINAC_ASSERT(diff_param==0);
230 return psi(exONE(), x);
233 const unsigned function_index_psi1 = function::register_new("psi", psi1_eval, psi1_evalf, psi1_diff, NULL);
236 // Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
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)
243 // psi(0,x) -> 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()));
256 return psi(n, x).hold();
259 static ex psi2_evalf(ex const & n, ex const & x)
264 END_TYPECHECK(psi(n,x))
266 return psi(ex_to_numeric(n), ex_to_numeric(x));
269 static ex psi2_diff(ex const & n, ex const & x, unsigned diff_param)
271 GINAC_ASSERT(diff_param<2);
275 throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
281 const unsigned function_index_psi2 = function::register_new("psi", psi2_eval, psi2_evalf, psi2_diff, NULL);
283 #ifndef NO_GINAC_NAMESPACE
285 #endif // ndef NO_GINAC_NAMESPACE