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
/*
Copyright (C) 2018 Daniel Schultz
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_vec.h"
#include "fmpq_mpoly.h"
/*
We are supposed to produce a canonical A assuming that A->zpoly is itself
canonical. The code should not break if A->zpoly is not canonical.
*/
void fmpq_mpoly_reduce(fmpq_mpoly_t A, const fmpq_mpoly_ctx_t ctx)
{
fmpz_t g;
if (A->zpoly->length < 1)
{
fmpq_zero(A->content);
return;
}
else if (fmpq_is_zero(A->content))
{
fmpz_mpoly_zero(A->zpoly, ctx->zctx);
return;
}
/* A is nonzero at this point (assuming A->zpoly is canonical) */
fmpz_init(g);
_fmpz_vec_content(g, A->zpoly->coeffs, A->zpoly->length);
if (fmpz_sgn(A->zpoly->coeffs + 0) < 0)
fmpz_neg(g, g);
if (fmpz_is_zero(g))
{
/* bail if A->zpoly has only zeros stored (A->zpoly not canonical) */
fmpq_one(A->content);
}
else if (fmpz_is_pm1(g))
{
if (!fmpz_is_one(g))
{
fmpq_neg(A->content, A->content);
_fmpz_vec_neg(A->zpoly->coeffs, A->zpoly->coeffs, A->zpoly->length);
}
}
else
{
fmpq_mul_fmpz(A->content, A->content, g);
_fmpz_vec_scalar_divexact_fmpz(A->zpoly->coeffs, A->zpoly->coeffs,
A->zpoly->length, g);
}
fmpz_clear(g);
}