File: logistic.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 (86 lines) | stat: -rw-r--r-- 1,895 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
/* This file is public domain. Author: Fredrik Johansson. */

#include <stdlib.h>
#include <flint/arb.h>
#include <flint/profiler.h>

int main(int argc, char *argv[])
{
    slong prec, goal, digits;
    const char * xs;
    const char * rs;
    arb_t x, r, s, t;
    slong i, n;

    if (argc < 2)
    {
        flint_printf("nth iterate of the logistic map x_{n+1} = r x_n (1-x_n)\n");
        flint_printf("usage: build/examples/logistic n [x_0=0.5] [r=3.75] [digits=10]\n");
        return 1;
    }

    n = atol(argv[1]);
    n = FLINT_MAX(n, 0);
    xs = (argc >= 3) ? argv[2] : "0.5";
    rs = (argc >= 4) ? argv[3] : "3.75";
    digits = (argc >= 5) ? atol(argv[4]) : 10;
    if (digits < 1)
        digits = 1;

    goal = digits * 3.3219280948873623 + 3;

    arb_init(x);
    arb_init(r);
    arb_init(s);
    arb_init(t);

    TIMEIT_ONCE_START;
    for (prec = 64; ; prec *= 2)
    {
        flint_printf("Trying prec=%wd bits...", prec);
        fflush(stdout);

        arb_set_str(x, xs, prec);
        arb_set_str(r, rs, prec);

        if (!arb_is_finite(x) || !arb_is_finite(r))
        {
            flint_printf("unable to parse input string!\n");
            flint_abort();
        }

        for (i = 0; i < n; i++)
        {
            arb_sub_ui(t, x, 1, prec);
            arb_neg(t, t);
            arb_mul(x, x, t, prec);
            arb_mul(x, x, r, prec);

            if (arb_rel_accuracy_bits(x) < goal)
            {
                flint_printf("ran out of accuracy at step %wd\n", i);
                break;
            }
        }

        if (i == n)
        {
            flint_printf("success!\n");
            break;
        }
    }
    TIMEIT_ONCE_STOP;

    flint_printf("x_%wd = ", n);
    arb_printn(x, digits, 0);
    flint_printf("\n");

    arb_clear(x);
    arb_clear(r);
    arb_clear(s);
    arb_clear(t);

    flint_cleanup();
    return 0;
}