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
/*
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 "fq_nmod.h"
#include "n_poly.h"
#include "mpoly.h"
#include "fq_nmod_mpoly.h"
/*
assuming that the exponents are valid and sorted,
put the polynomial in canonical form
i.e.
2*x^e + 3*x^e -> 5x^e
2*x^e - 2*x^e -> 0
*/
void fq_nmod_mpoly_combine_like_terms(
fq_nmod_mpoly_t A,
const fq_nmod_mpoly_ctx_t ctx)
{
slong d = fq_nmod_ctx_degree(ctx->fqctx);
slong N = mpoly_words_per_exp(A->bits, ctx->minfo);
slong in, out;
out = -1;
for (in = 0; in < A->length; in++)
{
FLINT_ASSERT(in > out);
if (out >= 0 &&
mpoly_monomial_equal(A->exps + N*out, A->exps + N*in, N))
{
n_fq_add(A->coeffs + d*out, A->coeffs + d*out, A->coeffs + d*in,
ctx-> fqctx);
}
else
{
if (out < 0 || !_n_fq_is_zero(A->coeffs + d*out, d))
out++;
if (out != in)
{
mpoly_monomial_set(A->exps + N*out, A->exps + N*in, N);
_n_fq_swap(A->coeffs + d*out, A->coeffs + d*in, d);
}
}
}
if (out < WORD(0) || !_n_fq_is_zero(A->coeffs + d*out, d))
out++;
A->length = out;
}