return result;
}
-/* Simple tests on the Gamma combinatorial function. We stuff in arguments
- * where the result exists in closed form and check if it's ok. */
+/* Simple tests on the Gamma function. We stuff in arguments where the results
+ * exists in closed form and check if it's ok. */
static unsigned inifcns_consist_gamma(void)
{
unsigned result = 0;
return result;
}
+/* Simple tests on the Psi-function (aka polygamma-function). We stuff in
+ arguments where the result exists in closed form and check if it's ok. */
+static unsigned inifcns_consist_psi(void)
+{
+ unsigned result = 0;
+ symbol x;
+ ex e;
+
+ // We check psi(1) and psi(1/2) implicitly by calculating the curious
+ // little identity gamma(1)'/gamma(1) - gamma(1/2)'/gamma(1/2) == 2*log(2).
+ e += (gamma(x).diff(x)/gamma(x)).subs(x==numeric(1));
+ e -= (gamma(x).diff(x)/gamma(x)).subs(x==numeric(1,2));
+ if (e!=2*log(2)) {
+ clog << "gamma(1)'/gamma(1) - gamma(1/2)'/gamma(1/2) erroneously returned "
+ << e << " instead of 2*log(2)" << endl;
+ ++result;
+ }
+
+ return result;
+}
+
/* Simple tests on the Riemann Zeta function. We stuff in arguments where the
* result exists in closed form and check if it's ok. Of course, this checks
* the Bernoulli numbers as a side effect. */
result += inifcns_consist_cos();
result += inifcns_consist_trans();
result += inifcns_consist_gamma();
+ result += inifcns_consist_psi();
result += inifcns_consist_zeta();
if ( !result ) {
/** @file inifcns_gamma.cpp
*
- * Implementation of Gamma-function, Polygamma-functions, and some related
- * stuff. */
+ * Implementation of Gamma-function, Beta-function, Polygamma-functions, and
+ * some related stuff. */
/*
* GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
if (x.info(info_flags::posint)) {
return factorial(ex_to_numeric(x).sub(numONE()));
} else {
- return numZERO(); // Infinity. Throw? What?
+ throw (std::domain_error("gamma_eval(): simple pole"));
}
}
// trap half integer arguments:
REGISTER_FUNCTION(gamma, gamma_eval, gamma_evalf, gamma_diff, gamma_series);
+//////////
+// Beta-function
+//////////
+
+static ex beta_eval(ex const & x, ex const & y)
+{
+ if (x.info(info_flags::numeric) && y.info(info_flags::numeric)) {
+ numeric nx(ex_to_numeric(x));
+ numeric ny(ex_to_numeric(y));
+ // treat all problematic x and y that may not be passed into gamma,
+ // because they would throw there although beta(x,y) is well-defined:
+ if (nx.is_real() && nx.is_integer() &&
+ ny.is_real() && ny.is_integer()) {
+ if (nx.is_negative()) {
+ if (nx<=-ny)
+ return numMINUSONE().power(ny)*beta(1-x-y, y);
+ else
+ throw (std::domain_error("beta_eval(): simple pole"));
+ }
+ if (ny.is_negative()) {
+ if (ny<=-nx)
+ return numMINUSONE().power(nx)*beta(1-y-x, x);
+ else
+ throw (std::domain_error("beta_eval(): simple pole"));
+ }
+ return gamma(x)*gamma(y)/gamma(x+y);
+ }
+ // no problem in numerator, but denominator has pole:
+ if ((nx+ny).is_real() &&
+ (nx+ny).is_integer() &&
+ !(nx+ny).is_positive())
+ return exZERO();
+ return gamma(x)*gamma(y)/gamma(x+y);
+ }
+ return beta(x,y).hold();
+}
+
+static ex beta_evalf(ex const & x, ex const & y)
+{
+ BEGIN_TYPECHECK
+ TYPECHECK(x,numeric)
+ TYPECHECK(y,numeric)
+ END_TYPECHECK(beta(x,y))
+
+ return gamma(ex_to_numeric(x))*gamma(ex_to_numeric(y))
+ / gamma(ex_to_numeric(x+y));
+}
+
+static ex beta_diff(ex const & x, ex const & y, unsigned diff_param)
+{
+ GINAC_ASSERT(diff_param<2);
+ ex retval;
+
+ if (diff_param==0) // d/dx beta(x,y)
+ retval = (psi(x)-psi(x+y))*beta(x,y);
+ if (diff_param==1) // d/dy beta(x,y)
+ retval = (psi(y)-psi(x+y))*beta(x,y);
+ return retval;
+}
+
+static ex beta_series(ex const & x, ex const & y, symbol const & s, ex const & point, int order)
+{
+ if (x.is_equal(s) && point.is_zero()) {
+ ex e = 1 / s - EulerGamma + s * (pow(Pi, 2) / 12 + pow(EulerGamma, 2) / 2) + Order(pow(s, 2));
+ return e.series(s, point, order);
+ } else
+ throw(std::logic_error("don't know the series expansion of this particular beta function"));
+}
+
+REGISTER_FUNCTION(beta, beta_eval, beta_evalf, beta_diff, beta_series);
+
//////////
// Psi-function (aka polygamma-function)
//////////
* Built-in functions
*/
-static ex f_beta(const exprseq &e) {return gamma(e[0])*gamma(e[1])/gamma(e[0]+e[1]);}
static ex f_denom(const exprseq &e) {return e[0].denom();}
static ex f_eval1(const exprseq &e) {return e[0].eval();}
static ex f_evalf1(const exprseq &e) {return e[0].evalf();}
};
static const fcn_init builtin_fcns[] = {
- {"beta", fcn_desc(f_beta, 2)},
{"charpoly", fcn_desc(f_charpoly, 2)},
{"coeff", fcn_desc(f_coeff, 3)},
{"collect", fcn_desc(f_collect, 2)},
insert_fcn_help("atan", "inverse tangent function");
insert_fcn_help("atan2", "inverse tangent function with two arguments");
insert_fcn_help("atanh", "inverse hyperbolic tangent function");
+ insert_fcn_help("beta", "beta function");
insert_fcn_help("cos", "cosine function");
insert_fcn_help("cosh", "hyperbolic cosine function");
+ insert_fcn_help("psi", "polygamma function");
insert_fcn_help("sin", "sine function");
insert_fcn_help("sinh", "hyperbolic sine function");
insert_fcn_help("tan", "tangent function");