File: root_of_unity.c

package info (click to toggle)
calcium 0.4.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,756 kB
  • sloc: ansic: 62,836; python: 2,827; sh: 518; makefile: 163
file content (147 lines) | stat: -rw-r--r-- 3,168 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
/*
    Copyright (C) 2020 Fredrik Johansson

    This file is part of Calcium.

    Calcium 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 "qqbar.h"

int
qqbar_is_root_of_unity(slong * p, ulong * q, const qqbar_t x)
{
    ulong n;

    n = fmpz_poly_is_cyclotomic(QQBAR_POLY(x));

    if (n == 0)
        return 0;

    if (q != NULL)
        *q = n;

    if (n == 1)
    {
        if (p != NULL) *p = 0;
    }
    else if (n == 2)
    {
        if (p != NULL) *p = 1;
    }
    else if (n == 3)
    {
        if (p != NULL) *p = (qqbar_sgn_im(x) > 0) ? 1 : 2;
    }
    else if (n == 4)
    {
        if (p != NULL) *p = (qqbar_sgn_im(x) > 0) ? 1 : 3;
    }
    else
    {
        if (p != NULL)
        {
            arb_t t, u;
            acb_t z;
            fmpz_t k;
            slong prec;

            acb_init(z);
            arb_init(t);
            arb_init(u);
            fmpz_init(k);

            prec = 64;  /* more than enough */

            qqbar_get_acb(z, x, prec);
            acb_arg(t, z, prec);
            arb_const_pi(u, prec);
            arb_div(t, t, u, prec);
            arb_mul_2exp_si(t, t, -1);
            arb_mul_ui(t, t, n, prec);

            if (!arb_get_unique_fmpz(k, t))
            {
                flint_printf("qqbar_is_root_of_unity: unexpected precision issue\n");
                flint_abort();
            }

            if (fmpz_sgn(k) < 0)
                fmpz_add_ui(k, k, n);

            *p = fmpz_get_si(k);

            acb_clear(z);
            arb_clear(t);
            arb_clear(u);
            fmpz_clear(k);
        }
    }

    return 1;
}

void
qqbar_root_of_unity(qqbar_t res, slong p, ulong q)
{
    fmpq_t t;
    ulong a, b;
    slong prec;

    fmpq_init(t);

    if (q == 0)
    {
        flint_printf("qqbar_root_of_unity: q = 0\n");
        flint_abort();
    }

    fmpq_set_si(t, p, q);
    fmpz_fdiv_r(fmpq_numref(t), fmpq_numref(t), fmpq_denref(t));

    a = fmpz_get_ui(fmpq_numref(t));
    b = fmpz_get_ui(fmpq_denref(t));

    if (a == 0)
    {
        qqbar_one(res);
    }
    else if (a == 1 && b == 2)
    {
        qqbar_set_si(res, -1);
    }
    else if (a == 1 && b == 4)
    {
        qqbar_i(res);
    }
    else if (a == 3 && b == 4)
    {
        qqbar_i(res);
        qqbar_conj(res, res);
    }
    else
    {
        fmpz_poly_cyclotomic(QQBAR_POLY(res), b);
        fmpq_mul_2exp(t, t, 1);

        for (prec = QQBAR_DEFAULT_PREC / 2; ; prec *= 2)
        {
            arb_sin_cos_pi_fmpq(acb_imagref(QQBAR_ENCLOSURE(res)),
                                acb_realref(QQBAR_ENCLOSURE(res)),
                                t, prec);

            /* todo: this is really unnecessary... */
            if (_qqbar_validate_uniqueness(QQBAR_ENCLOSURE(res),
                    QQBAR_POLY(res), QQBAR_ENCLOSURE(res), prec * 2))
            {
                break;
            }
        }
    }

    fmpq_clear(t);
}