1 /*
2     Copyright (C) 2019 Daniel Schultz
3 
4     This file is part of FLINT.
5 
6     FLINT is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <https://www.gnu.org/licenses/>.
10 */
11 
12 #include "fq_nmod_mpoly.h"
13 
fq_nmod_mpoly_sub_fq_nmod(fq_nmod_mpoly_t A,const fq_nmod_mpoly_t B,const fq_nmod_t c,const fq_nmod_mpoly_ctx_t ctx)14 void fq_nmod_mpoly_sub_fq_nmod(
15     fq_nmod_mpoly_t A,
16     const fq_nmod_mpoly_t B,
17     const fq_nmod_t c,
18     const fq_nmod_mpoly_ctx_t ctx)
19 {
20     slong d = fq_nmod_ctx_degree(ctx->fqctx);
21     slong N = mpoly_words_per_exp(B->bits, ctx->minfo);
22     slong Blen = B->length;
23 
24 fq_nmod_mpoly_assert_canonical(B, ctx);
25 
26     if (fq_nmod_is_zero(c, ctx->fqctx))
27     {
28         fq_nmod_mpoly_set(A, B, ctx);
29         return;
30     }
31 
32     if (Blen < 1)
33     {
34         fq_nmod_mpoly_set_fq_nmod(A, c, ctx);
35         fq_nmod_mpoly_neg(A, A, ctx);
36         return;
37     }
38 
39     if (mpoly_monomial_is_zero(B->exps + (Blen - 1)*N, N))
40     {
41         if (A != B)
42         {
43             fq_nmod_mpoly_fit_length_reset_bits(A, Blen, B->bits, ctx);
44             _nmod_vec_set(A->coeffs, B->coeffs, d*(Blen - 1));
45             mpoly_copy_monomials(A->exps, B->exps, Blen, N);
46             _fq_nmod_mpoly_set_length(A, Blen, ctx);
47         }
48 
49         n_fq_sub_fq_nmod(A->coeffs + d*(Blen - 1), B->coeffs + d*(Blen - 1), c, ctx->fqctx);
50         if (_n_fq_is_zero(A->coeffs + d*(Blen - 1), d))
51             _fq_nmod_mpoly_set_length(A, Blen - 1, ctx);
52     }
53     else
54     {
55         if (A != B)
56         {
57             fq_nmod_mpoly_fit_length_reset_bits(A, Blen + 1, B->bits, ctx);
58             _nmod_vec_set(A->coeffs, B->coeffs, d*Blen);
59             mpoly_copy_monomials(A->exps, B->exps, Blen, N);
60         }
61         else
62         {
63             fq_nmod_mpoly_fit_length(A, Blen + 1, ctx);
64         }
65 
66         mpoly_monomial_zero(A->exps + N*Blen, N);
67         n_fq_set_fq_nmod(A->coeffs + d*Blen, c, ctx->fqctx);
68         _n_fq_neg(A->coeffs + d*Blen, A->coeffs + d*Blen, d, fq_nmod_ctx_mod(ctx->fqctx));
69         _fq_nmod_mpoly_set_length(A, Blen + 1, ctx);
70     }
71 }
72