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
/*
Copyright (C) 2010 Sebastian Pancratz
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 "fmpq_poly.h"
void _fmpq_poly_canonicalise(fmpz * poly, fmpz_t den, slong len)
{
if (*den == WORD(1))
return;
if (*den == WORD(-1))
{
_fmpz_vec_neg(poly, poly, len);
fmpz_one(den);
}
else if (len == 0)
{
fmpz_one(den);
}
else
{
fmpz_t gcd;
fmpz_init(gcd);
_fmpz_vec_content_chained(gcd, poly, len, den);
if (fmpz_sgn(den) < 0)
fmpz_neg(gcd, gcd);
if (!fmpz_is_one(gcd))
{
_fmpz_vec_scalar_divexact_fmpz(poly, poly, len, gcd);
fmpz_divexact(den, den, gcd);
}
fmpz_clear(gcd);
}
}
void fmpq_poly_canonicalise(fmpq_poly_t poly)
{
_fmpq_poly_normalise(poly);
_fmpq_poly_canonicalise(poly->coeffs, poly->den, poly->length);
}