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
/*
Copyright (C) 2011 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 "ulong_extras.h"
#include "fmpz.h"
#include "fmpz_poly.h"
void
_fmpz_poly_cyclotomic(fmpz * a, ulong n, nn_ptr factors,
slong num_factors, ulong phi)
{
ulong i;
slong j, k;
int small;
ulong D;
D = phi / 2;
/* Phi_p(x) = 1 + x + x^2 + ... + x^{p-1} */
if (num_factors == 1)
{
for (i = 0; i <= D; i++)
fmpz_one(a + i);
return;
}
/* Phi_{2n}(x) = Phi_n(-x)*/
if (factors[0] == 2)
{
_fmpz_poly_cyclotomic(a, n / 2, factors + 1, num_factors - 1, phi);
for (i = 1; i <= D; i += 2)
fmpz_neg(a + i, a + i);
return;
}
fmpz_one(a);
for (i = 1; i <= D; i++)
fmpz_zero(a + i);
/* Coefficients are guaranteed not to overflow an fmpz */
small = (num_factors == 2) || /* Always +1/0/-1*/
(n < 10163195) || /* At most 27 bits */
(FLINT_BITS == 64 && n < 169828113); /* At most 60 bits */
/* Iterate over all divisors of n */
for (k = 0; k < (WORD(1) << num_factors); k++)
{
int mu;
ulong d;
mu = (num_factors & 1) ? -1 : 1;
d = WORD(1);
for (j = 0; j < num_factors; j++)
{
if ((k >> j) & 1)
{
d *= factors[j];
mu = -mu;
}
}
/* Multiply by (x^d - 1)^{\mu(n/d)} */
if (small)
{
if (mu == 1)
for (i = D; i >= d; i--) a[i] -= a[i - d];
else
for (i = d; i <= D; i++) a[i] += a[i - d];
}
else
{
if (mu == 1)
for (i = D; i >= d; i--) fmpz_sub(a + i, a + i, a + i - d);
else
for (i = d; i <= D; i++) fmpz_add(a + i, a + i, a + i - d);
}
}
}
void
fmpz_poly_cyclotomic(fmpz_poly_t poly, ulong n)
{
n_factor_t factors;
slong i;
ulong k;
ulong s, phi;
if (n <= 2)
{
if (n == 0)
{
fmpz_poly_one(poly);
}
else
{
fmpz_poly_fit_length(poly, 2);
fmpz_set_si(poly->coeffs, (n == 1) ? -1 : 1);
fmpz_set_si(poly->coeffs + 1, 1);
_fmpz_poly_set_length(poly, 2);
}
return;
}
/* Write n = q * s where q is squarefree, compute the factors of q,
and compute phi(s) which determines the degree of the polynomial. */
n_factor_init(&factors);
n_factor(&factors, n, 1);
s = phi = 1;
for (i = 0; i < factors.num; i++)
{
phi *= factors.p[i] - 1;
while (factors.exp[i] > 1)
{
s *= factors.p[i];
factors.exp[i]--;
}
}
fmpz_poly_fit_length(poly, phi * s + 1);
/* Evaluate lower half of Phi_s(x) */
_fmpz_poly_cyclotomic(poly->coeffs, n / s, factors.p, factors.num, phi);
/* Palindromic extension */
for (k = 0; k < (phi + 1) / 2; k++)
fmpz_set(poly->coeffs + phi - k, poly->coeffs + k);
/* Stretch */
if (s != 1)
{
for (i = phi; i > 0; i--)
{
fmpz_set(poly->coeffs + i*s, poly->coeffs + i);
for (k = 1; k < s; k++)
fmpz_zero(poly->coeffs + i*s - k);
}
}
_fmpz_poly_set_length(poly, phi * s + 1);
}