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 "arb_poly.h"
13
14 void
_arb_poly_div_series(arb_ptr Q,arb_srcptr A,slong Alen,arb_srcptr B,slong Blen,slong n,slong prec)15 _arb_poly_div_series(arb_ptr Q, arb_srcptr A, slong Alen,
16 arb_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 _arb_vec_scalar_div(Q, A, Alen, B, prec);
24 _arb_vec_zero(Q + Alen, n - Alen);
25 }
26 else if (n == 2)
27 {
28 if (Alen == 1)
29 {
30 arb_div(Q, A, B, prec);
31 arb_div(Q + 1, Q, B, prec);
32 arb_mul(Q + 1, Q + 1, B + 1, prec);
33 arb_neg(Q + 1, Q + 1);
34 }
35 else
36 {
37 arb_div(Q, A, B, prec);
38 arb_mul(Q + 1, Q, B + 1, prec);
39 arb_sub(Q + 1, A + 1, Q + 1, prec);
40 arb_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 arb_t q;
49
50 arb_init(q);
51
52 arb_inv(q, B, prec);
53 arb_div(Q, A, B, prec);
54
55 for (i = 1; i < n; i++)
56 {
57 arb_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 (!arb_is_one(q))
60 arb_mul(Q + i, Q + i, q, prec);
61 }
62
63 arb_clear(q);
64 }
65 else
66 {
67 arb_ptr Binv;
68 Binv = _arb_vec_init(n);
69 _arb_poly_inv_series(Binv, B, Blen, n, prec);
70 _arb_poly_mullow(Q, Binv, n, A, Alen, n, prec);
71 _arb_vec_clear(Binv, n);
72 }
73 }
74
75 void
arb_poly_div_series(arb_poly_t Q,const arb_poly_t A,const arb_poly_t B,slong n,slong prec)76 arb_poly_div_series(arb_poly_t Q, const arb_poly_t A, const arb_poly_t B, slong n, slong prec)
77 {
78 if (n == 0)
79 {
80 arb_poly_zero(Q);
81 return;
82 }
83
84 if (B->length == 0)
85 {
86 arb_poly_fit_length(Q, n);
87 _arb_vec_indeterminate(Q->coeffs, n);
88 _arb_poly_set_length(Q, n);
89 return;
90 }
91
92 if (A->length == 0)
93 {
94 arb_poly_zero(Q);
95 return;
96 }
97
98 if (Q == A || Q == B)
99 {
100 arb_poly_t t;
101 arb_poly_init(t);
102 arb_poly_div_series(t, A, B, n, prec);
103 arb_poly_swap(Q, t);
104 arb_poly_clear(t);
105 return;
106 }
107
108 arb_poly_fit_length(Q, n);
109 _arb_poly_div_series(Q->coeffs, A->coeffs, A->length, B->coeffs, B->length, n, prec);
110 _arb_poly_set_length(Q, n);
111 _arb_poly_normalise(Q);
112 }
113
114