1 /*
2     Copyright (C) 2013 Fredrik Johansson
3 
4     This file is part of Arb.
5 
6     Arb is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
10 */
11 
12 #include "acb.h"
13 
14 void acb_hypgeom_gamma_stirling_choose_param(int * reflect, slong * r, slong * n,
15     const acb_t x, int use_reflect, int digamma, slong prec);
16 
17 void acb_gamma_stirling_eval(acb_t s, const acb_t z, slong nterms, int digamma, slong prec);
18 
19 void
acb_digamma(acb_t y,const acb_t x,slong prec)20 acb_digamma(acb_t y, const acb_t x, slong prec)
21 {
22     int reflect;
23     slong r, n, wp;
24     acb_t t, u, v;
25 
26     if (acb_is_real(x))
27     {
28         arb_digamma(acb_realref(y), acb_realref(x), prec);
29         arb_zero(acb_imagref(y));
30         return;
31     }
32 
33     wp = prec + FLINT_BIT_COUNT(prec);
34 
35     acb_hypgeom_gamma_stirling_choose_param(&reflect, &r, &n, x, 1, 1, wp);
36 
37     acb_init(t);
38     acb_init(u);
39     acb_init(v);
40 
41     /* psi(x) = psi((1-x)+r) - h(1-x,r) - pi*cot(pi*x) */
42     if (reflect)
43     {
44         acb_sub_ui(t, x, 1, wp);
45         acb_neg(t, t);
46         acb_cot_pi(v, x, wp);
47         arb_const_pi(acb_realref(u), wp);
48         acb_mul_arb(v, v, acb_realref(u), wp);
49         acb_rising2_ui(y, u, t, r, wp);
50         acb_div(u, u, y, wp);
51         acb_add(v, v, u, wp);
52         acb_add_ui(t, t, r, wp);
53         acb_gamma_stirling_eval(u, t, n, 1, wp);
54         acb_sub(y, u, v, wp);
55     }
56     else
57     {
58         acb_add_ui(t, x, r, wp);
59         acb_gamma_stirling_eval(u, t, n, 1, wp);
60         acb_rising2_ui(y, t, x, r, wp);
61         acb_div(t, t, y, wp);
62         acb_sub(y, u, t, prec);
63     }
64 
65     acb_clear(t);
66     acb_clear(u);
67     acb_clear(v);
68 }
69 
70