1 /*
2     Copyright (C) 2018 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 <http://www.gnu.org/licenses/>.
10 */
11 
12 #include "fmpq_mpoly.h"
13 
14 
fmpq_mpoly_divrem(fmpq_mpoly_t Q,fmpq_mpoly_t R,const fmpq_mpoly_t A,const fmpq_mpoly_t B,const fmpq_mpoly_ctx_t ctx)15 void fmpq_mpoly_divrem(fmpq_mpoly_t Q, fmpq_mpoly_t R,
16                           const fmpq_mpoly_t A, const fmpq_mpoly_t B,
17                                                     const fmpq_mpoly_ctx_t ctx)
18 {
19     fmpz_t scale;
20     fmpq_t t;
21 
22     if (fmpq_mpoly_is_zero(B, ctx))
23     {
24         flint_throw(FLINT_DIVZERO, "Divide by zero in fmpq_mpoly_divrem");
25     }
26 
27     if (fmpq_mpoly_is_zero(A, ctx))
28     {
29         fmpq_mpoly_zero(Q, ctx);
30         fmpq_mpoly_zero(R, ctx);
31         return;
32     }
33 
34     fmpz_init(scale);
35     fmpz_mpoly_quasidivrem(scale, Q->zpoly, R->zpoly,
36                                                 A->zpoly, B->zpoly, ctx->zctx);
37 
38     fmpq_init(t);
39     fmpq_div_fmpz(t, A->content, scale);
40     fmpq_div(Q->content, t, B->content);
41     fmpq_swap(t, R->content);
42     fmpq_clear(t);
43     fmpz_clear(scale);
44 
45     fmpq_mpoly_reduce(Q, ctx);
46     fmpq_mpoly_reduce(R, ctx);
47 }
48