+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));
+
+
+} // namespace GiNaC