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
/*
Copyright (C) 2012 Fredrik Johansson
Copyright (C) 2018, 2019 William Hart
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 "fmpz.h"
#include "fmpz_vec.h"
#include "fmpz_poly.h"
/*
Implements the square root with remainder function of "speeding up the
division and square root of power series", by Hanrot, Quercia and
Zimmermann (see https://hal.inria.fr/inria-00072675/document), but
omitting the remainder in the power series case.
TODO: implement middle product so that we can implement their Sqrt function
(their specialised square root Divide function requires it).
*/
int
_fmpz_poly_sqrt_divconquer(fmpz * res, const fmpz * poly, slong len, int exact)
{
slong i, n, n2;
fmpz * r, * temp;
int result;
if (len < FMPZ_POLY_SQRT_DIVCONQUER_CUTOFF)
return _fmpz_poly_sqrt_classical(res, poly, len, exact);
/* the degree must be even */
if (len % 2 == 0)
return 0;
n = (len + 1)/2;
/* check whether a square root exists modulo 2 */
n2 = (n + 1)/2;
/* only check coeffs that won't be checked recursively */
for (i = ((n - 1) | 1); i < len - n2; i += 2)
if (!fmpz_is_even(poly + i))
return 0;
if (exact)
{
for (i = 1; i < ((n - 1) | 1); i += 2)
if (!fmpz_is_even(poly + i))
return 0;
}
/* check endpoints */
if (exact && !fmpz_is_square(poly))
return 0;
r = _fmpz_vec_init(len);
temp = _fmpz_vec_init(len);
_fmpz_vec_set(r, poly, len);
result = _fmpz_poly_sqrtrem_divconquer(res + n - n2, r + len - 2*n2 + 1,
r + len - 2*n2 + 1, 2*n2 - 1, temp);
if (result)
{
_fmpz_vec_scalar_mul_ui(temp, res + n - n2, n2, 2);
_fmpz_vec_set(temp + n, r + n2, 2*n - 2*n2 - 1);
if (!_fmpz_poly_divrem(res, r + n2, temp + n, 2*n - 2*n2 - 1, temp + 2*n2 - n, n - n2, 1))
result = 0;
if (exact && result)
{
_fmpz_poly_mul(temp + 2*n2 - n, res, n - n2, res, n - n2);
_fmpz_vec_sub(r, r, temp + 2*n2 - n, 2*n - 2*n2 - 1);
if (2*n2 > n)
_fmpz_vec_scalar_submul_fmpz(r + n - n2, res, n2 - 1, temp);
for (i = n; i < len && result; i++)
{
if (!fmpz_is_zero(r + len - 1 - i))
{
result = 0;
break;
}
}
}
}
_fmpz_vec_clear(r, len);
_fmpz_vec_clear(temp, len);
return result;
}
int
fmpz_poly_sqrt_divconquer(fmpz_poly_t b, const fmpz_poly_t a)
{
slong blen, len = a->length;
int result;
if (len % 2 == 0)
{
fmpz_poly_zero(b);
return len == 0;
}
if (b == a)
{
fmpz_poly_t tmp;
fmpz_poly_init(tmp);
result = fmpz_poly_sqrt_divconquer(tmp, a);
fmpz_poly_swap(b, tmp);
fmpz_poly_clear(tmp);
return result;
}
blen = len / 2 + 1;
fmpz_poly_fit_length(b, blen);
_fmpz_poly_set_length(b, blen);
result = _fmpz_poly_sqrt_divconquer(b->coeffs, a->coeffs, len, 1);
if (!result)
_fmpz_poly_set_length(b, 0);
return result;
}