File: gamma.c

package info (click to toggle)
flint-arb 1%3A2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: ansic: 204,753; sh: 570; makefile: 287; python: 268
file content (237 lines) | stat: -rw-r--r-- 5,702 bytes parent folder | download
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
    Copyright (C) 2014, 2015, 2021 Fredrik Johansson

    This file is part of Arb.

    Arb 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 2.1 of the License, or
    (at your option) any later version.  See <http://www.gnu.org/licenses/>.
*/

#include "acb_hypgeom.h"
#include "arb_hypgeom.h"

void
acb_hypgeom_gamma_stirling_choose_param(int * reflect, slong * r, slong * n,
    const acb_t z, int use_reflect, int digamma, slong prec);

void acb_gamma_stirling_bound(mag_ptr err, const acb_t x, slong k0, slong knum, slong n);

void
acb_hypgeom_gamma_stirling_inner(acb_t s, const acb_t z, slong N, slong prec)
{
    acb_t logz, t;
    mag_t err;

    mag_init(err);
    acb_init(t);
    acb_init(logz);

    acb_gamma_stirling_bound(err, z, 0, 1, N);

    /* t = (z-0.5)*log(z) - z + log(2*pi)/2 */
    acb_log(logz, z, prec);
    arb_one(acb_realref(t));
    arb_mul_2exp_si(acb_realref(t), acb_realref(t), -1);
    acb_sub(t, z, t, prec);
    acb_mul(t, logz, t, prec);
    acb_sub(t, t, z, prec);
    arb_const_log_sqrt2pi(acb_realref(logz), prec);
    arb_add(acb_realref(t), acb_realref(t), acb_realref(logz), prec);

    /* sum part */
    if (prec <= 128 || (prec <= 1024 && N <= 40) || (prec <= 2048 && N <= 16))
        acb_hypgeom_gamma_stirling_sum_horner(s, z, N, prec);
    else
        acb_hypgeom_gamma_stirling_sum_improved(s, z, N, 0, prec);

    acb_add(s, s, t, prec);
    acb_add_error_mag(s, err);

    acb_clear(t);
    acb_clear(logz);
    mag_clear(err);
}


void
acb_hypgeom_gamma_stirling(acb_t y, const acb_t x, int reciprocal, slong prec)
{
    int reflect;
    slong r, n, wp;
    acb_t t, u, v;
    double acc;

    wp = prec + FLINT_BIT_COUNT(prec);

    /* todo: for large x (if exact or accurate enough), increase precision */
    acc = acb_rel_accuracy_bits(x);
    acc = FLINT_MAX(acc, 0);
    wp = FLINT_MIN(prec, acc + 20);
    wp = FLINT_MAX(wp, 2);
    wp = wp + FLINT_BIT_COUNT(wp);

    if (acc < 3)  /* try to avoid divisions blowing up */
    {
        if (arf_cmp_d(arb_midref(acb_realref(x)), -0.5) < 0)
        {
            reflect = 1;
            r = 0;
        }
        else if (arf_cmp_si(arb_midref(acb_realref(x)), 1) < 0)
        {
            reflect = 0;
            r = 1;
        }
        else
        {
            reflect = 0;
            r = 0;
        }

        n = 1;
    }
    else
    {
        acb_hypgeom_gamma_stirling_choose_param(&reflect, &r, &n, x, 1, 0, wp);
    }

    acb_init(t);
    acb_init(u);
    acb_init(v);

    if (reflect)
    {
        acb_sub_ui(t, x, 1, wp);
        acb_neg(t, t);
        acb_hypgeom_rising_ui_rec(u, t, r, wp);
        arb_const_pi(acb_realref(v), wp);
        acb_mul_arb(u, u, acb_realref(v), wp);
        acb_add_ui(t, t, r, wp);
        acb_hypgeom_gamma_stirling_inner(v, t, n, wp);

        if (reciprocal)
        {
            /* rgamma(x) = gamma(1-x+r) sin(pi x) / ((rf(1-x, r) * pi) */
            acb_exp(v, v, wp);
            acb_sin_pi(t, x, wp);
            acb_mul(v, v, t, wp);
            acb_mul(y, u, v, wp);
            acb_div(y, v, u, prec);
        }
        else
        {
            /* gamma(x) = (rf(1-x, r) * pi) rgamma(1-x+r) csc(pi x) */
            acb_neg(v, v);
            acb_exp(v, v, wp);
            acb_csc_pi(t, x, wp);
            acb_mul(v, v, t, wp);
            acb_mul(y, v, u, prec);
        }
    }
    else
    {
        acb_add_ui(t, x, r, wp);
        acb_hypgeom_gamma_stirling_inner(u, t, n, wp);

        if (reciprocal)
        {
            /* rgamma(x) = rf(x,r) rgamma(x+r) */
            acb_neg(u, u);
            acb_exp(u, u, prec);
            acb_hypgeom_rising_ui_rec(v, x, r, wp);
            acb_mul(y, v, u, prec);
        }
        else
        {
            /* gamma(x) = gamma(x+r) / rf(x,r) */
            acb_exp(u, u, prec);
            acb_hypgeom_rising_ui_rec(v, x, r, wp);
            acb_div(y, u, v, prec);
        }
    }

    acb_clear(t);
    acb_clear(u);
    acb_clear(v);
}

void
acb_hypgeom_gamma(acb_t y, const acb_t x, slong prec)
{
    if (acb_is_real(x))
    {
        arb_hypgeom_gamma(acb_realref(y), acb_realref(x), prec);
        arb_zero(acb_imagref(y));
        return;
    }

    if (acb_hypgeom_gamma_taylor(y, x, 0, prec))
        return;

    acb_hypgeom_gamma_stirling(y, x, 0, prec);
}

void
acb_hypgeom_rgamma(acb_t y, const acb_t x, slong prec)
{
    mag_t magz;

    if (acb_is_real(x))
    {
        arb_hypgeom_rgamma(acb_realref(y), acb_realref(x), prec);
        arb_zero(acb_imagref(y));
        return;
    }

    if (acb_hypgeom_gamma_taylor(y, x, 1, prec))
        return;

    mag_init(magz);
    acb_get_mag(magz, x);

    if (mag_is_inf(magz))
    {
        acb_indeterminate(y);
    }
    else
    {
        acb_hypgeom_gamma_stirling(y, x, 1, prec);

        /* Todo: improved bounds computation */
        if (!acb_is_finite(y))
        {
            arb_t t, u, R;

            arb_init(R);
            arb_init(t);
            arb_init(u);

            arf_set_mag(arb_midref(R), magz);

            arb_set_d(u, 0.5);
            arb_add(u, u, R, MAG_BITS);
            arb_pow(u, R, u, MAG_BITS);

            arb_const_pi(t, MAG_BITS);
            arb_mul(t, t, R, MAG_BITS);
            arb_mul_2exp_si(t, t, -1);
            arb_exp(t, t, MAG_BITS);

            arb_mul(t, t, u, MAG_BITS);

            arb_get_mag(magz, t);

            acb_zero(y);
            acb_add_error_mag(y, magz);

            arb_clear(R);
            arb_clear(t);
            arb_clear(u);
        }
    }

    mag_clear(magz);
}