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
64
65
/*
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"
void fmpq_mpoly_set_term_coeff_fmpq(fmpq_mpoly_t A,
slong i, const fmpq_t x, const fmpq_mpoly_ctx_t ctx)
{
if ((ulong) i >= (ulong) fmpq_mpoly_length(A, ctx))
{
flint_throw(FLINT_ERROR, "index out of range in fmpq_mpoly_set_term_coeff_fmpq");
}
if (fmpq_is_zero(x))
{
/* we can easily get a zero coeff by zeroing the coeff of the zpoly */
fmpz_mpoly_set_term_coeff_fmpz(A->zpoly, i, fmpq_numref(x), ctx->zctx);
}
else if (fmpq_is_zero(A->content))
{
/*
if the A->content is zero,
set the nth coeff to 1
set all other coeffs to 0
set the content to x
*/
fmpz_t t;
fmpz_init_set_ui(t, UWORD(1));
fmpq_set(A->content, x);
_fmpz_vec_zero(A->zpoly->coeffs, A->zpoly->length);
fmpz_mpoly_set_term_coeff_fmpz(A->zpoly, i, t, ctx->zctx);
fmpz_clear(t);
}
else
{
/*
if A->content != 0 and x != 0,
compute nun/den = x / A->content
scale zpoly by den
divide A->content by den
set the nth coeff of zpoly to num
*/
fmpq_t t;
fmpq_init(t);
fmpq_div(t, x, A->content);
if (!fmpz_is_one(fmpq_denref(t)))
{
fmpq_div_fmpz(A->content, A->content, fmpq_denref(t));
_fmpz_vec_scalar_mul_fmpz(A->zpoly->coeffs, A->zpoly->coeffs,
A->zpoly->length, fmpq_denref(t));
}
fmpz_mpoly_set_term_coeff_fmpz(A->zpoly, i, fmpq_numref(t), ctx->zctx);
fmpq_clear(t);
}
}