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
/*
Copyright (C) 2012 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 "fmpz_poly.h"
int
_fmpz_poly_sqrt(fmpz * res, const fmpz * poly, slong len)
{
/* KS is heuristic but fast */
int result = _fmpz_poly_sqrt_KS(res, poly, len);
/* KS failed, so use fallback */
if (result == -1)
result = _fmpz_poly_sqrt_divconquer(res, poly, len, 1);
return result;
}
int
fmpz_poly_sqrt(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(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(b->coeffs, a->coeffs, len);
if (!result)
_fmpz_poly_set_length(b, 0);
return result;
}