1 /*
2     Copyright (C) 2010 Sebastian Pancratz
3     Copyright (C) 2014 Fredrik Johansson
4     Copyright (C) 2019 William Hart
5 
6     This file is part of FLINT.
7 
8     FLINT is free software: you can redistribute it and/or modify it under
9     the terms of the GNU Lesser General Public License (LGPL) as published
10     by the Free Software Foundation; either version 2.1 of the License, or
11     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
12 */
13 
14 #include "fmpz_poly.h"
15 
16 void
_fmpz_poly_div_series(fmpz * Q,const fmpz * A,slong Alen,const fmpz * B,slong Blen,slong n)17 _fmpz_poly_div_series(fmpz * Q, const fmpz * A, slong Alen,
18     const fmpz * B, slong Blen, slong n)
19 {
20     Alen = FLINT_MIN(Alen, n);
21     Blen = FLINT_MIN(Blen, n);
22 
23     if (n < 32 || Blen < 20)
24        _fmpz_poly_div_series_basecase(Q, A, Alen, B, Blen, n);
25     else if (fmpz_is_pm1(B + 0))
26     {
27         fmpz * Binv = _fmpz_vec_init(n);
28 
29         _fmpz_poly_inv_series(Binv, B, Blen, n);
30         _fmpz_poly_mullow(Q, Binv, n, A, Alen, n);
31 
32         _fmpz_vec_clear(Binv, n);
33     } else
34         _fmpz_poly_div_series_divconquer(Q, A, Alen, B, Blen, n);
35 }
36 
fmpz_poly_div_series(fmpz_poly_t Q,const fmpz_poly_t A,const fmpz_poly_t B,slong n)37 void fmpz_poly_div_series(fmpz_poly_t Q, const fmpz_poly_t A,
38                                          const fmpz_poly_t B, slong n)
39 {
40     slong Alen = FLINT_MIN(A->length, n);
41     slong Blen = FLINT_MIN(B->length, n);
42 
43     if (Blen == 0)
44     {
45         flint_printf("Exception (fmpz_poly_div_series). Division by zero.\n");
46         flint_abort();
47     }
48 
49     if (Alen == 0)
50     {
51         fmpz_poly_zero(Q);
52         return;
53     }
54 
55     if (Q == A || Q == B)
56     {
57         fmpz_poly_t t;
58         fmpz_poly_init2(t, n);
59         _fmpz_poly_div_series(t->coeffs, A->coeffs, Alen, B->coeffs, Blen, n);
60         fmpz_poly_swap(Q, t);
61         fmpz_poly_clear(t);
62     }
63     else
64     {
65         fmpz_poly_fit_length(Q, n);
66         _fmpz_poly_div_series(Q->coeffs, A->coeffs, Alen, B->coeffs, Blen, n);
67     }
68 
69     _fmpz_poly_set_length(Q, n);
70     _fmpz_poly_normalise(Q);
71 }
72