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
/*
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 "test_helpers.h"
#include "ulong_extras.h"
#include "fmpz.h"
#include "fmpz_poly.h"
static void cyclotomic_naive(fmpz_poly_t poly, ulong n)
{
fmpz_poly_t t;
slong d;
fmpz_poly_init(t);
fmpz_poly_set_ui(poly, UWORD(1));
for (d = 1; d <= n; d++)
{
if (n % d == 0)
{
if (n_moebius_mu(n / d) == 1)
{
fmpz_poly_zero(t);
fmpz_poly_set_coeff_si(t, d, 1);
fmpz_poly_set_coeff_si(t, 0, -1);
fmpz_poly_mul(poly, poly, t);
}
}
}
for (d = 1; d <= n; d++)
{
if (n % d == 0)
{
if (n_moebius_mu(n / d) == -1)
{
fmpz_poly_zero(t);
fmpz_poly_set_coeff_si(t, d, 1);
fmpz_poly_set_coeff_si(t, 0, -1);
fmpz_poly_div(poly, poly, t);
}
}
}
fmpz_poly_clear(t);
}
TEST_FUNCTION_START(fmpz_poly_cyclotomic, state)
{
fmpz_poly_t A, B;
slong n;
for (n = 0; n <= FLINT_MAX(200.0, 100 * flint_test_multiplier()); n++)
{
fmpz_poly_init(A);
fmpz_poly_init(B);
fmpz_poly_cyclotomic(A, n);
cyclotomic_naive(B, n);
if (!fmpz_poly_equal(A, B))
{
flint_printf("FAIL: wrong value of Phi_%wd(x)\n", n);
flint_printf("Computed:\n");
fmpz_poly_print_pretty(A, "x");
flint_printf("\n\nExpected:\n");
fmpz_poly_print_pretty(B, "x");
flint_printf("\n\n");
fflush(stdout);
flint_abort();
}
fmpz_poly_clear(A);
fmpz_poly_clear(B);
}
/* We verify the first value that does not fit on 32 bits.
This exercises the slow path at least on a 32 bit system.
Testing the 64 bit value is a bit too much to do by default
as it requires ~2 GB of memory and takes a few minutes. */
if (flint_test_multiplier() >= 2.0)
{
fmpz_t h, ref;
const ulong nn = UWORD(10163195);
/* const ulong nn = UWORD(169828113); 64-bit case */
fmpz_init(h);
fmpz_init(ref);
fmpz_set_str(ref, "1376877780831", 10);
/* fmpz_set_str(ref, "31484567640915734941", 10); 64-bit case */
fmpz_poly_init(A);
fmpz_poly_cyclotomic(A, UWORD(10163195));
fmpz_poly_height(h, A);
if (!fmpz_equal(h, ref))
{
flint_printf("Bad computation of Phi_%wd(x)\n", nn);
flint_printf("Computed height:\n");
fmpz_print(h);
flint_printf("\nExpected height:\n");
fmpz_print(ref);
flint_printf("\n\n");
fflush(stdout);
flint_abort();
}
fmpz_poly_clear(A);
fmpz_clear(h);
fmpz_clear(ref);
}
TEST_FUNCTION_END(state);
}