1 /*
2     Copyright (C) 2012, 2013 Fredrik Johansson
3 
4     This file is part of Arb.
5 
6     Arb 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 "acb_poly.h"
13 
14 void
_acb_poly_div_series(acb_ptr Q,acb_srcptr A,slong Alen,acb_srcptr B,slong Blen,slong n,slong prec)15 _acb_poly_div_series(acb_ptr Q, acb_srcptr A, slong Alen,
16     acb_srcptr B, slong Blen, slong n, slong prec)
17 {
18     Alen = FLINT_MIN(Alen, n);
19     Blen = FLINT_MIN(Blen, n);
20 
21     if (Blen == 1)
22     {
23         _acb_vec_scalar_div(Q, A, Alen, B, prec);
24         _acb_vec_zero(Q + Alen, n - Alen);
25     }
26     else if (n == 2)
27     {
28         if (Alen == 1)
29         {
30             acb_div(Q, A, B, prec);
31             acb_div(Q + 1, Q, B, prec);
32             acb_mul(Q + 1, Q + 1, B + 1, prec);
33             acb_neg(Q + 1, Q + 1);
34         }
35         else
36         {
37             acb_div(Q, A, B, prec);
38             acb_mul(Q + 1, Q, B + 1, prec);
39             acb_sub(Q + 1, A + 1, Q + 1, prec);
40             acb_div(Q + 1, Q + 1, B, prec);
41         }
42     }
43     else if (Blen == 2 || n <= 10)
44     {
45         /* The basecase algorithm is faster for much larger Blen and n than
46            this, but unfortunately has worse numerical stability. */
47         slong i;
48         acb_t q;
49 
50         acb_init(q);
51 
52         acb_inv(q, B, prec);
53         acb_div(Q, A, B, prec);
54 
55         for (i = 1; i < n; i++)
56         {
57             acb_dot(Q + i, (i < Alen) ? A + i : NULL, 1,
58                 B + 1, 1, Q + i - 1, -1, FLINT_MIN(i, Blen - 1), prec);
59             if (!acb_is_one(q))
60                 acb_mul(Q + i, Q + i, q, prec);
61         }
62 
63         acb_clear(q);
64     }
65     else
66     {
67         acb_ptr Binv;
68         Binv = _acb_vec_init(n);
69         _acb_poly_inv_series(Binv, B, Blen, n, prec);
70         _acb_poly_mullow(Q, Binv, n, A, Alen, n, prec);
71         _acb_vec_clear(Binv, n);
72     }
73 }
74 
75 void
acb_poly_div_series(acb_poly_t Q,const acb_poly_t A,const acb_poly_t B,slong n,slong prec)76 acb_poly_div_series(acb_poly_t Q, const acb_poly_t A, const acb_poly_t B, slong n, slong prec)
77 {
78     if (n == 0)
79     {
80         acb_poly_zero(Q);
81         return;
82     }
83 
84     if (B->length == 0)
85     {
86         acb_poly_fit_length(Q, n);
87         _acb_vec_indeterminate(Q->coeffs, n);
88         _acb_poly_set_length(Q, n);
89         return;
90     }
91 
92     if (A->length == 0)
93     {
94         acb_poly_zero(Q);
95         return;
96     }
97 
98     if (Q == A || Q == B)
99     {
100         acb_poly_t t;
101         acb_poly_init(t);
102         acb_poly_div_series(t, A, B, n, prec);
103         acb_poly_swap(Q, t);
104         acb_poly_clear(t);
105         return;
106     }
107 
108     acb_poly_fit_length(Q, n);
109     _acb_poly_div_series(Q->coeffs, A->coeffs, A->length, B->coeffs, B->length, n, prec);
110     _acb_poly_set_length(Q, n);
111     _acb_poly_normalise(Q);
112 }
113 
114