+
+static ex beta_series(const ex & arg1,
+ const ex & arg2,
+ const relational & rel,
+ int order,
+ unsigned options)
+{
+ // method:
+ // Taylor series where there is no pole of one of the tgamma functions
+ // falls back to beta function evaluation. Otherwise, fall back to
+ // tgamma series directly.
+ const ex arg1_pt = arg1.subs(rel, subs_options::no_pattern);
+ const ex arg2_pt = arg2.subs(rel, subs_options::no_pattern);
+ GINAC_ASSERT(is_a<symbol>(rel.lhs()));
+ const symbol &s = ex_to<symbol>(rel.lhs());
+ ex arg1_ser, arg2_ser, arg1arg2_ser;
+ if ((!arg1_pt.info(info_flags::integer) || arg1_pt.info(info_flags::positive)) &&
+ (!arg2_pt.info(info_flags::integer) || arg2_pt.info(info_flags::positive)))
+ throw do_taylor(); // caught by function::series()
+ // trap the case where arg1 is on a pole:
+ if (arg1.info(info_flags::integer) && !arg1.info(info_flags::positive))
+ arg1_ser = tgamma(arg1+s);
+ else
+ arg1_ser = tgamma(arg1);
+ // trap the case where arg2 is on a pole:
+ if (arg2.info(info_flags::integer) && !arg2.info(info_flags::positive))
+ arg2_ser = tgamma(arg2+s);
+ else
+ arg2_ser = tgamma(arg2);
+ // trap the case where arg1+arg2 is on a pole:
+ if ((arg1+arg2).info(info_flags::integer) && !(arg1+arg2).info(info_flags::positive))
+ arg1arg2_ser = tgamma(arg2+arg1+s);
+ else
+ arg1arg2_ser = tgamma(arg2+arg1);
+ // compose the result (expanding all the terms):
+ return (arg1_ser*arg2_ser/arg1arg2_ser).series(rel, order, options).expand();
+}
+
+
+REGISTER_FUNCTION(beta, eval_func(beta_eval).
+ evalf_func(beta_evalf).
+ derivative_func(beta_deriv).
+ series_func(beta_series).
+ latex_name("\\mathrm{B}").
+ set_symmetry(sy_symm(0, 1)));
+
+
+//////////
+// Psi-function (aka digamma-function)
+//////////
+
+static ex psi1_evalf(const ex & x)
+{
+ if (is_exactly_a<numeric>(x)) {
+ try {
+ return psi(ex_to<numeric>(x));
+ } catch (const dunno &e) { }
+ }
+
+ return psi(x).hold();
+}
+
+/** Evaluation of digamma-function psi(x).
+ * Somebody ought to provide some good numerical evaluation some day... */
+static ex psi1_eval(const ex & x)
+{
+ if (x.info(info_flags::numeric)) {
+ const numeric &nx = ex_to<numeric>(x);
+ if (nx.is_integer()) {
+ // integer case
+ if (nx.is_positive()) {
+ // psi(n) -> 1 + 1/2 +...+ 1/(n-1) - Euler
+ numeric rat = 0;
+ for (numeric i(nx+(*_num_1_p)); i>0; --i)
+ rat += i.inverse();
+ return rat-Euler;
+ } else {
+ // for non-positive integers there is a pole:
+ throw (pole_error("psi_eval(): simple pole",1));
+ }
+ }
+ if (((*_num2_p)*nx).is_integer()) {
+ // half integer case
+ if (nx.is_positive()) {
+ // psi((2m+1)/2) -> 2/(2m+1) + 2/2m +...+ 2/1 - Euler - 2log(2)
+ numeric rat = 0;
+ for (numeric i = (nx+(*_num_1_p))*(*_num2_p); i>0; i-=(*_num2_p))
+ rat += (*_num2_p)*i.inverse();
+ return rat-Euler-_ex2*log(_ex2);
+ } else {
+ // use the recurrence relation
+ // psi(-m-1/2) == psi(-m-1/2+1) - 1 / (-m-1/2)
+ // to relate psi(-m-1/2) to psi(1/2):
+ // psi(-m-1/2) == psi(1/2) + r
+ // where r == ((-1/2)^(-1) + ... + (-m-1/2)^(-1))
+ numeric recur = 0;
+ for (numeric p = nx; p<0; ++p)
+ recur -= pow(p, *_num_1_p);
+ return recur+psi(_ex1_2);
+ }
+ }
+ // psi1_evalf should be called here once it becomes available
+ }
+
+ return psi(x).hold();
+}
+
+static ex psi1_deriv(const ex & x, unsigned deriv_param)
+{
+ GINAC_ASSERT(deriv_param==0);
+
+ // d/dx psi(x) -> psi(1,x)
+ return psi(_ex1, x);
+}
+
+static ex psi1_series(const ex & arg,
+ const relational & rel,
+ int order,
+ unsigned options)
+{
+ // method:
+ // Taylor series where there is no pole falls back to polygamma function
+ // evaluation.
+ // On a pole at -m use the recurrence relation
+ // psi(x) == psi(x+1) - 1/z
+ // from which follows
+ // series(psi(x),x==-m,order) ==
+ // series(psi(x+m+1) - 1/x - 1/(x+1) - 1/(x+m)),x==-m,order);
+ const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
+ if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
+ throw do_taylor(); // caught by function::series()
+ // if we got here we have to care for a simple pole at -m:
+ const numeric m = -ex_to<numeric>(arg_pt);
+ ex recur;
+ for (numeric p; p<=m; ++p)
+ recur += power(arg+p,_ex_1);
+ return (psi(arg+m+_ex1)-recur).series(rel, order, options);
+}
+
+unsigned psi1_SERIAL::serial =
+ function::register_new(function_options("psi", 1).
+ eval_func(psi1_eval).
+ evalf_func(psi1_evalf).
+ derivative_func(psi1_deriv).
+ series_func(psi1_series).
+ latex_name("\\psi").
+ overloaded(2));
+
+//////////
+// Psi-functions (aka polygamma-functions) psi(0,x)==psi(x)
+//////////
+
+static ex psi2_evalf(const ex & n, const ex & x)
+{
+ if (is_exactly_a<numeric>(n) && is_exactly_a<numeric>(x)) {
+ try {
+ return psi(ex_to<numeric>(n),ex_to<numeric>(x));
+ } catch (const dunno &e) { }
+ }
+
+ return psi(n,x).hold();
+}
+
+/** Evaluation of polygamma-function psi(n,x).
+ * Somebody ought to provide some good numerical evaluation some day... */
+static ex psi2_eval(const ex & n, const ex & x)
+{
+ // psi(0,x) -> psi(x)
+ if (n.is_zero())
+ return psi(x);
+ // psi(-1,x) -> log(tgamma(x))
+ if (n.is_equal(_ex_1))
+ return log(tgamma(x));
+ if (n.info(info_flags::numeric) && n.info(info_flags::posint) &&
+ x.info(info_flags::numeric)) {
+ const numeric &nn = ex_to<numeric>(n);
+ const numeric &nx = ex_to<numeric>(x);
+ if (nx.is_integer()) {
+ // integer case
+ if (nx.is_equal(*_num1_p))
+ // use psi(n,1) == (-)^(n+1) * n! * zeta(n+1)
+ return pow(*_num_1_p,nn+(*_num1_p))*factorial(nn)*zeta(ex(nn+(*_num1_p)));
+ if (nx.is_positive()) {
+ // use the recurrence relation
+ // psi(n,m) == psi(n,m+1) - (-)^n * n! / m^(n+1)
+ // to relate psi(n,m) to psi(n,1):
+ // psi(n,m) == psi(n,1) + r
+ // where r == (-)^n * n! * (1^(-n-1) + ... + (m-1)^(-n-1))
+ numeric recur = 0;
+ for (numeric p = 1; p<nx; ++p)
+ recur += pow(p, -nn+(*_num_1_p));
+ recur *= factorial(nn)*pow((*_num_1_p), nn);
+ return recur+psi(n,_ex1);
+ } else {
+ // for non-positive integers there is a pole:
+ throw (pole_error("psi2_eval(): pole",1));
+ }
+ }
+ if (((*_num2_p)*nx).is_integer()) {
+ // half integer case
+ if (nx.is_equal(*_num1_2_p))
+ // use psi(n,1/2) == (-)^(n+1) * n! * (2^(n+1)-1) * zeta(n+1)
+ return pow(*_num_1_p,nn+(*_num1_p))*factorial(nn)*(pow(*_num2_p,nn+(*_num1_p)) + (*_num_1_p))*zeta(ex(nn+(*_num1_p)));
+ if (nx.is_positive()) {
+ const numeric m = nx - (*_num1_2_p);
+ // use the multiplication formula
+ // psi(n,2*m) == (psi(n,m) + psi(n,m+1/2)) / 2^(n+1)
+ // to revert to positive integer case
+ return psi(n,(*_num2_p)*m)*pow((*_num2_p),nn+(*_num1_p))-psi(n,m);
+ } else {
+ // use the recurrence relation
+ // psi(n,-m-1/2) == psi(n,-m-1/2+1) - (-)^n * n! / (-m-1/2)^(n+1)
+ // to relate psi(n,-m-1/2) to psi(n,1/2):
+ // psi(n,-m-1/2) == psi(n,1/2) + r
+ // where r == (-)^(n+1) * n! * ((-1/2)^(-n-1) + ... + (-m-1/2)^(-n-1))
+ numeric recur = 0;
+ for (numeric p = nx; p<0; ++p)
+ recur += pow(p, -nn+(*_num_1_p));
+ recur *= factorial(nn)*pow(*_num_1_p, nn+(*_num_1_p));
+ return recur+psi(n,_ex1_2);
+ }
+ }
+ // psi2_evalf should be called here once it becomes available
+ }
+
+ return psi(n, x).hold();
+}
+
+static ex psi2_deriv(const ex & n, const ex & x, unsigned deriv_param)
+{
+ GINAC_ASSERT(deriv_param<2);
+
+ if (deriv_param==0) {
+ // d/dn psi(n,x)
+ throw(std::logic_error("cannot diff psi(n,x) with respect to n"));
+ }
+ // d/dx psi(n,x) -> psi(n+1,x)
+ return psi(n+_ex1, x);
+}
+
+static ex psi2_series(const ex & n,
+ const ex & arg,
+ const relational & rel,
+ int order,
+ unsigned options)
+{
+ // method:
+ // Taylor series where there is no pole falls back to polygamma function
+ // evaluation.
+ // On a pole at -m use the recurrence relation
+ // psi(n,x) == psi(n,x+1) - (-)^n * n! / x^(n+1)
+ // from which follows
+ // series(psi(x),x==-m,order) ==
+ // series(psi(x+m+1) - (-1)^n * n! * ((x)^(-n-1) + (x+1)^(-n-1) + ...
+ // ... + (x+m)^(-n-1))),x==-m,order);
+ const ex arg_pt = arg.subs(rel, subs_options::no_pattern);
+ if (!arg_pt.info(info_flags::integer) || arg_pt.info(info_flags::positive))
+ throw do_taylor(); // caught by function::series()
+ // if we got here we have to care for a pole of order n+1 at -m:
+ const numeric m = -ex_to<numeric>(arg_pt);
+ ex recur;
+ for (numeric p; p<=m; ++p)
+ recur += power(arg+p,-n+_ex_1);
+ recur *= factorial(n)*power(_ex_1,n);
+ return (psi(n, arg+m+_ex1)-recur).series(rel, order, options);
+}
+
+unsigned psi2_SERIAL::serial =
+ function::register_new(function_options("psi", 2).
+ eval_func(psi2_eval).
+ evalf_func(psi2_evalf).
+ derivative_func(psi2_deriv).
+ series_func(psi2_series).
+ latex_name("\\psi").
+ overloaded(2));
+