1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
Copyright (C) 2013 Fredrik Johansson
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "fmpq.h"
#include "arith.h"
#include "arb.h"
#include "arb/impl.h"
#include "arb_hypgeom/impl.h"
void
arb_digamma(arb_t y, const arb_t x, slong prec)
{
int reflect;
slong r, n, wp;
arb_t t, u, v;
if (arb_is_exact(x))
{
const arf_struct * mid = arb_midref(x);
if (arf_is_special(mid))
{
arb_indeterminate(y);
return;
}
else if (arf_is_int(mid))
{
if (arf_sgn(mid) < 0)
{
arb_indeterminate(y);
return;
}
else if (arf_cmpabs_ui(mid, 30 + prec / 2) < 0)
{
fmpq_t h;
arb_init(t);
fmpq_init(h);
/* should change to fmpq_harmonic_ui when flint upgrades */
arith_harmonic_number(h, arf_get_si(mid, ARF_RND_DOWN) - 1);
arb_set_fmpq(y, h, prec + 2);
arb_const_euler(t, prec + 2);
arb_sub(y, y, t, prec);
arb_clear(t);
fmpq_clear(h);
return;
}
}
}
wp = prec + FLINT_BIT_COUNT(prec);
arb_hypgeom_gamma_stirling_choose_param(&reflect, &r, &n, x, 1, 1, wp);
arb_init(t);
arb_init(u);
arb_init(v);
/* psi(x) = psi((1-x)+r) - h(1-x,r) - pi*cot(pi*x) */
if (reflect)
{
arb_sub_ui(t, x, 1, wp);
arb_neg(t, t);
arb_cot_pi(v, x, wp);
arb_const_pi(u, wp);
arb_mul(v, v, u, wp);
arb_rising2_ui(y, u, t, r, wp);
arb_div(u, u, y, wp);
arb_add(v, v, u, wp);
arb_add_ui(t, t, r, wp);
arb_gamma_stirling_eval(u, t, n, 1, wp);
arb_sub(y, u, v, wp);
}
else
{
arb_add_ui(t, x, r, wp);
arb_gamma_stirling_eval(u, t, n, 1, wp);
arb_rising2_ui(y, t, x, r, wp);
arb_div(t, t, y, wp);
arb_sub(y, u, t, prec);
}
arb_clear(t);
arb_clear(u);
arb_clear(v);
}