File: integrals_double_exp.c

package info (click to toggle)
flint 3.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 68,996 kB
  • sloc: ansic: 915,350; asm: 14,605; python: 5,340; sh: 4,512; lisp: 2,621; makefile: 787; cpp: 341
file content (480 lines) | stat: -rw-r--r-- 10,305 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/* 
    Examples for double exponential integration demonstrating integration 
    with log, log^2 and sqrt singularities. In all cases close to
    quadratic convergence with the number of function evaluations is 
    observed. 

    This file is part of FLINT.

    Copyright (C) 2024 Hartmut Monien
*/

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>

#include <flint/profiler.h>
#include <flint/arb_hypgeom.h>
#include <flint/acb_hypgeom.h>
#include <flint/acb_dirichlet.h>
#include <flint/acb_modular.h>
#include <flint/arb_calc.h>

enum QUAD_RULE
{
    QUAD_INTERVAL,
    QUAD_SEMI_INF,
    QUAD_INF
};

void
quad_rule(arb_t w, arb_t t, arb_t x, arb_t pi, int rule, slong prec)
{

    arb_t c, s;

    arb_init(s);
    arb_init(c);

    arb_sinh_cosh(s, c, x, prec);
    arb_mul(c, c, pi, prec);
    arb_mul(s, s, pi, prec);
    arb_div_ui(s, s, 2, prec);
    arb_div_ui(c, c, 2, prec);

    switch (rule)
    {
        case QUAD_INTERVAL:
            arb_sinh_cosh(t, w, s, prec);
            arb_div(t, t, w, prec);
            arb_sqr(w, w, prec);
            arb_div(w, c, w, prec);
            break;
        case QUAD_SEMI_INF:
            arb_exp(t, s, prec);
            arb_mul(w, c, t, prec);
            break;
        case QUAD_INF:
            arb_set(w, c);
            arb_sinh_cosh(t, c, s, prec);
            arb_mul(w, w, c, prec);
            break;
        default:
            flint_printf("no integration rule %d.\n", rule);
            exit(1);
    }

    arb_clear(s);
    arb_clear(c);
}

void
quad(arb_t result,
     arb_calc_func_t f, void *param, int rule, int verbose, slong prec)
{

    arb_t x, y, h, x0, pi, w, t;
    slong order = 0, max_iter = 16, n_eval = 0, FLINT_SET_BUT_UNUSED(flag), j;

    arb_ptr r = _arb_vec_init(max_iter);
    arb_ptr d = _arb_vec_init(max_iter);

    arb_init(x);
    arb_init(y);
    arb_init(h);
    arb_init(x0);

    arb_init(pi);

    arb_init(w);
    arb_init(t);

    arb_const_pi(pi, prec);

    arb_zero(t);
    quad_rule(w, t, x, pi, rule, prec);

    flag = f(y, t, param, order, prec);
    n_eval++;
    arb_mul(result, y, w, prec);
    arb_set(r, result);

    if (verbose > 0)
    {
        flint_printf("\n\t%8s %8s %10s\n\n", "log(d)", "#evals", "rate");
    }

    arb_one(x0);
    arb_one(h);

    for (j = 0; j + 1 < max_iter; j++)
    {

        for (int pm = -1; pm <= 1; pm += 2)
        {

            arb_set(x, x0);

            if (pm < 0)
            {
                arb_neg(x, x);
            }

            for (;;)
            {

                quad_rule(w, t, x, pi, rule, prec);

                flag = f(y, t, param, order, prec);
                n_eval++;

                arb_mul(y, y, w, prec);
                arb_mul(y, y, h, prec);
                arb_add(w, result, y, prec);

                if (!arb_contains(w, result))
                {
                    arb_set(result, w);
                }
                else
                {
                    break;
                }

                if (pm > 0)
                {
                    arb_add(x, x, h, prec);
                }
                else
                {
                    arb_sub(x, x, h, prec);
                }

            }
        }

        arb_div_ui(x0, x0, 2, prec);

        if (j > 0)
        {
            arb_div_ui(h, h, 2, prec);
            arb_div_ui(result, result, 2, prec);
        }

        arb_sub(d + j, result, r + j, prec);
        arb_abs(d + j, d + j);

        if (arb_contains_zero(d + j))
            break;

        if (verbose > 0)
        {
            arb_log_base_ui(w, d + j, 10, prec);
            double d_1 = arf_get_d(arb_midref(w), ARF_RND_NEAR);
            if (j > 1)
            {
                arb_log_base_ui(w, d + j - 1, 10, prec);
                double d_0 = arf_get_d(arb_midref(w), ARF_RND_NEAR);
                flint_printf("\t%8.2f %8d %10.4f\n", d_1, n_eval, d_1 / d_0);
            }
            else
            {
                flint_printf("\t%8.2f %8d\n", d_1, n_eval);
            }
        }

        arb_set(r + j + 1, result);

    }

    arb_clear(x);
    arb_clear(y);
    arb_clear(h);
    arb_clear(x0);

    arb_clear(pi);

    arb_clear(w);
    arb_clear(t);

    _arb_vec_clear(r, max_iter);
    _arb_vec_clear(d, max_iter);
}

/*-- Example Integrands ------------------------------------------------------*/

int
f_rat(arb_ptr y, const arb_t x, void *param, slong order, slong prec)
{
    arb_t t;
    arb_init(t);
    arb_pow_ui(y, x, 3, prec);
    arb_add_ui(y, y, 1, prec);
    arb_inv(y, y, prec);
    arb_clear(t);
    return 0;
}

void
f_rat_exc(arb_ptr exc, slong prec)
{
    arb_t t;
    arb_init(t);
    arb_const_pi(exc, prec);
    arb_mul_ui(exc, exc, 2, prec);
    arb_set_ui(t, 27);
    arb_sqrt(t, t, prec);
    arb_div(exc, exc, t, prec);
    arb_clear(t);
}

int
g_rat(arb_ptr y, const arb_t x, void *param, slong order, slong prec)
{
    arb_pow_ui(y, x, 2, prec);
    arb_add_ui(y, y, 1, prec);
    arb_inv(y, y, prec);
    return 0;
}

void
g_rat_exc(arb_ptr exc, slong prec)
{
    arb_t t, w;
    arb_init(t);
    arb_init(w);
    arb_const_pi(exc, prec);
    arb_div_ui(exc, exc, 2, prec);
    arb_clear(t);
    arb_clear(w);
}

void
g_rat_exc_2(arb_ptr exc, slong prec)
{
    arb_const_pi(exc, prec);
}

int
f_log_exp(arb_ptr y, const arb_t x, void *param, slong order, slong prec)
{
    arb_t t;
    arb_init(t);
    arb_neg(y, x);
    arb_exp(y, y, prec);
    arb_log(t, x, prec);
    arb_mul(y, y, t, prec);
    arb_clear(t);
    return 0;
}

void
f_log_exp_exc(arb_ptr exc, slong prec)
{
    arb_const_euler(exc, prec);
    arb_neg(exc, exc);
}

int
f_log_rat(arb_ptr y, const arb_t x, void *param, slong order, slong prec)
{
    arb_t t;
    arb_init(t);
    arb_log(y, x, prec);
    arb_sqr(y, y, prec);
    arb_sqr(t, x, prec);
    arb_add_ui(t, t, 1, prec);
    arb_div(y, y, t, prec);
    arb_clear(t);
    return 0;
}

void
f_log_rat_exc(arb_ptr exc, slong prec)
{
    arb_const_pi(exc, prec);
    arb_pow_ui(exc, exc, 3, prec);
    arb_div_ui(exc, exc, 8, prec);
}

int
f_polylog(arb_ptr y, const arb_t x, void *param, slong order, slong prec)
{
    arb_t t;
    arb_init(t);
    arb_sqr(y, x, prec);
    arb_exp(t, x, prec);
    arb_sub_ui(t, t, 1, prec);
    arb_div(y, y, t, prec);
    arb_clear(t);
    return 0;
}

void
f_polylog_exc(arb_ptr exc, slong prec)
{
    arb_zeta_ui(exc, 3, prec);
    arb_mul_ui(exc, exc, 2, prec);
}

int
f_exp_sqrt(arb_ptr y, const arb_t x, void *param, slong order, slong prec)
{
    arb_t t;
    arb_init(t);
    arb_neg(y, x);
    arb_exp(y, y, prec);
    arb_sqrt(t, x, prec);
    arb_div(y, y, t, prec);
    arb_clear(t);
    return 0;
}

void
f_exp_sqrt_exc(arb_ptr exc, slong prec)
{
    arb_const_pi(exc, prec);
    arb_sqrt(exc, exc, prec);
}

int
f_gauss(arb_ptr y, const arb_t x, void *param, slong order, slong prec)
{
    arb_sqr(y, x, prec);
    arb_neg(y, y);
    arb_exp(y, y, prec);
    return 0;
}

void
f_gauss_exc(arb_ptr exc, slong prec)
{
    arb_const_pi(exc, prec);
    arb_sqrt(exc, exc, prec);
    arb_div_ui(exc, exc, 2, prec);
}

int
f_sqrt(arb_ptr y, const arb_t x, void *param, slong order, slong prec)
{
    arb_sqr(y, x, prec);
    arb_neg(y, y);
    arb_add_ui(y, y, 1, prec);
    arb_sqrt(y, y, prec);
    return 0;
}

void
f_sqrt_exc(arb_ptr exc, slong prec)
{
    arb_const_pi(exc, prec);
    arb_div_ui(exc, exc, 2, prec);
}

/*-- structure for integration examples --------------------------------------*/

typedef struct
{
    arb_calc_func_t fun;
    void (*exc)(arb_ptr out, slong prec);
    int rule;
    char *desc;
    char *result;
} example;

char *rule_desc[] = { "-1^+1", "0^oo", "-oo^+oo" };

example ex[] = {
    {f_rat, f_rat_exc, QUAD_SEMI_INF, "1/(1+x^3)", "2*pi/(3*sqrt(3))"},
    {f_log_exp, f_log_exp_exc, QUAD_SEMI_INF, "exp(-x)*log(x)", "-gamma"},
    {f_log_rat, f_log_rat_exc, QUAD_SEMI_INF, "log(x)^2/(1+x^2)", "pi^3/8"},
    {g_rat, g_rat_exc, QUAD_INTERVAL, "1/(1+x^2)", "pi/2"},
    {g_rat, g_rat_exc_2, QUAD_INF, "1/(1+x^2)", "pi"},
    {f_polylog, f_polylog_exc, QUAD_SEMI_INF, "x^2/(exp(x) - 1)", "2*zeta(3)"},
    {f_exp_sqrt, f_exp_sqrt_exc, QUAD_SEMI_INF, "exp(-x)/sqrt(x)", "sqrt(pi)"},
    {f_gauss, f_gauss_exc, QUAD_SEMI_INF, "exp(-x^2)", "sqrt(pi)/2"},
    {f_sqrt, f_sqrt_exc, QUAD_INTERVAL, "sqrt(1-x^2)", "pi/2"}
};

void
show_desc(int argc, char *argv[])
{
    flint_printf("Examples for double exponential integration.\n\n", argv[0]);
    flint_printf("usage: %s [-v] [-p precision] [-h]\n");
    flint_printf("\n\tcalculates a numerical approximation to:\n\n");
    for (slong j = 0; j < sizeof(ex) / sizeof(ex[0]); j++)
    {
        flint_printf("\tintegral_%s %s dx = %s\n",
                     rule_desc[ex[j].rule], ex[j].desc, ex[j].result);
    }
}

/*-- main program ------------------------------------------------------------*/

int
main(int argc, char *argv[])
{

    slong num_threads = 1;
    slong prec = 1024;

    int c, verbose = 0;

    opterr = 0;

    while ((c = getopt(argc, argv, "t:hvp:")) != -1)
    {
        switch (c)
        {
            case 'p':
                prec =
                    lround(strtol(optarg, (char **) NULL, 10) * log(10.) /
                           log(2.)) + 23;
                break;
            case 't':
                num_threads = atol(optarg);
                break;
            case 'v':
                verbose = 1;
                break;
            case 'h':
            default:
                show_desc(argc, argv);
                exit(0);
        }
    }

    if (num_threads > 1)
    {
        flint_set_num_threads(num_threads);
    }

    arb_t result, exc, d;

    arb_init(result);
    arb_init(exc);
    arb_init(d);

    for (slong j = 0; j < sizeof(ex) / sizeof(ex[0]); j++)
    {

        flint_printf("example: integral_%s %s dx = %s\n",
                     rule_desc[ex[j].rule], ex[j].desc, ex[j].result);

        quad(result, ex[j].fun, NULL, ex[j].rule, verbose, prec);

        ex[j].exc(exc, prec);

        arb_sub(d, result, exc, prec);
        arb_abs(d, d);

        flint_printf("\n\tresult = ");
        arb_printd(result, 16);
        flint_printf(", |result - exact| = ");
        arb_printn(d, 12, ARB_STR_NO_RADIUS);
        flint_printf("\n\n");

    }

}