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) 2024 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 "qqbar.h"
#include "gr.h"
#include "gr_poly.h"
TEST_FUNCTION_START(qqbar_roots_poly_squarefree, state)
{
slong iter;
slong count_success = 0;
slong deg_limit = 200;
slong bits_limit = 1000;
for (iter = 0; iter < 100 * flint_test_multiplier(); iter++)
{
gr_poly_t poly, fac;
gr_ctx_t ctx;
qqbar_ptr roots, computed_roots;
int success, status;
qqbar_t c;
slong deg, i, j;
deg = n_randint(state, 4);
roots = _qqbar_vec_init(deg);
computed_roots = _qqbar_vec_init(deg);
qqbar_init(c);
gr_ctx_init_complex_qqbar(ctx);
_gr_ctx_qqbar_set_limits(ctx, deg_limit, bits_limit);
gr_poly_init(poly, ctx);
gr_poly_init(fac, ctx);
for (i = 0; i < deg; i++)
{
if (n_randint(state, 10) == 0)
qqbar_randtest(roots + i, state, 4, 4);
else
qqbar_randtest(roots + i, state, 2, 4);
for (j = 0; j < i; j++)
{
if (qqbar_equal(roots + i, roots + j))
{
i--;
break;
}
}
}
if (n_randint(state, 5))
{
qqbar_one(c);
}
else
{
do {
qqbar_randtest(c, state, 2, 4);
} while (qqbar_is_zero(c));
}
status = GR_SUCCESS;
status |= gr_poly_set_scalar(poly, c, ctx);
for (i = 0; i < deg; i++)
{
status |= gr_poly_set_scalar(fac, roots + i, ctx);
status |= gr_poly_neg(fac, fac, ctx);
status |= gr_poly_set_coeff_si(fac, 1, 1, ctx);
status |= gr_poly_mul(poly, poly, fac, ctx);
}
if (status == GR_SUCCESS)
{
success = _qqbar_roots_poly_squarefree(computed_roots, poly->coeffs, poly->length, deg_limit, bits_limit);
count_success += success;
if (success)
{
for (i = 0; i < deg; i++)
{
int found = 0;
for (j = 0; j < deg; j++)
{
if (qqbar_equal(roots + i, computed_roots + j))
{
found = 1;
break;
}
}
if (!found)
{
flint_printf("FAIL\n\n");
flint_printf("deg = %wd\n", deg);
flint_printf("roots:\n");
for (j = 0; j < deg; j++)
{
qqbar_print(roots + i);
flint_printf("\n");
}
flint_printf("computed roots:\n");
for (j = 0; j < deg; j++)
{
qqbar_print(roots + i);
flint_printf("\n");
}
flint_abort();
}
}
}
/* flint_printf("total: %wd / %wd\n", count_success, iter); */
}
gr_poly_clear(poly, ctx);
gr_poly_clear(fac, ctx);
gr_ctx_clear(ctx);
_qqbar_vec_clear(roots, deg);
_qqbar_vec_clear(computed_roots, deg);
qqbar_clear(c);
}
if (iter > 100 && count_success < 0.1 * iter)
{
flint_printf("FAIL: only %wd / %wd succeeded\n", count_success, iter);
flint_abort();
}
TEST_FUNCTION_END(state);
}