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 static ex gamma_evalf(ex const & x)
49 END_TYPECHECK(gamma(x))
51 return gamma(ex_to_numeric(x));
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...
58 * @exception std::domain_error("gamma_eval(): simple pole") */
59 static ex gamma_eval(ex const & x)
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()));
68 throw (std::domain_error("gamma_eval(): simple pole"));
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());
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);
90 return gamma(x).hold();
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_evalf(ex const & x, ex const & y)
130 END_TYPECHECK(beta(x,y))
132 return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
133 / gamma(ex_to_numeric(x+y));
136 static ex beta_eval(ex const & x, ex const & y)
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()) {
147 return numMINUSONE().power(ny)*beta(1-x-y, y);
149 throw (std::domain_error("beta_eval(): simple pole"));
151 if (ny.is_negative()) {
153 return numMINUSONE().power(nx)*beta(1-y-x, x);
155 throw (std::domain_error("beta_eval(): simple pole"));
157 return gamma(x)*gamma(y)/gamma(x+y);
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())
164 return gamma(x)*gamma(y)/gamma(x+y);
166 return beta(x,y).hold();
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 static ex psi1_evalf(ex const & x)
191 END_TYPECHECK(psi(x))
193 return psi(ex_to_numeric(x));
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)
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)) {
207 for (numeric i(ex_to_numeric(x)-numONE()); i.is_positive(); --i)
209 return rat-EulerGamma;
211 // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - EulerGamma - 2log(2)
212 if ((exTWO()*x).info(info_flags::integer)) {
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());
218 if (x.compare(exONE())==1) {
219 // should call numeric, since >1
223 return psi(x).hold();
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 static ex psi2_evalf(ex const & n, ex const & x)
244 END_TYPECHECK(psi(n,x))
246 return psi(ex_to_numeric(n), ex_to_numeric(x));
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)
253 // psi(0,x) -> 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()));
266 return psi(n, x).hold();
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