1 /*
2     Copyright (C) 2012 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_compose_series(acb_ptr res,acb_srcptr poly1,slong len1,acb_srcptr poly2,slong len2,slong n,slong prec)15 _acb_poly_compose_series(acb_ptr res, acb_srcptr poly1, slong len1,
16                             acb_srcptr poly2, slong len2, slong n, slong prec)
17 {
18     if (len2 == 1)
19     {
20         acb_set_round(res, poly1, prec);
21         _acb_vec_zero(res + 1, n - 1);
22     }
23     else if (_acb_vec_is_zero(poly2 + 1, len2 - 2))  /* poly2 is a monomial */
24     {
25         slong i, j;
26         acb_t t;
27 
28         acb_init(t);
29         acb_set(t, poly2 + len2 - 1);
30         acb_set_round(res, poly1, prec);
31 
32         for (i = 1, j = len2 - 1; i < len1 && j < n; i++, j += len2 - 1)
33         {
34             acb_mul(res + j, poly1 + i, t, prec);
35 
36             if (i + 1 < len1 && j + len2 - 1 < n)
37                 acb_mul(t, t, poly2 + len2 - 1, prec);
38         }
39 
40         if (len2 != 2)
41             for (i = 1; i < n; i++)
42                 if (i % (len2 - 1) != 0)
43                     acb_zero(res + i);
44 
45         acb_clear(t);
46     }
47     else if (len1 < 6 || n < 6)
48     {
49         _acb_poly_compose_series_horner(res, poly1, len1, poly2, len2, n, prec);
50     }
51     else
52     {
53         _acb_poly_compose_series_brent_kung(res, poly1, len1, poly2, len2, n, prec);
54     }
55 }
56 
57 void
acb_poly_compose_series(acb_poly_t res,const acb_poly_t poly1,const acb_poly_t poly2,slong n,slong prec)58 acb_poly_compose_series(acb_poly_t res,
59                     const acb_poly_t poly1,
60                     const acb_poly_t poly2, slong n, slong prec)
61 {
62     slong len1 = poly1->length;
63     slong len2 = poly2->length;
64     slong lenr;
65 
66     if (len2 != 0 && !acb_is_zero(poly2->coeffs))
67     {
68         flint_printf("exception: compose_series: inner "
69                 "polynomial must have zero constant term\n");
70         flint_abort();
71     }
72 
73     if (len1 == 0 || n == 0)
74     {
75         acb_poly_zero(res);
76         return;
77     }
78 
79     if (len2 == 0 || len1 == 1)
80     {
81         acb_poly_set_acb(res, poly1->coeffs);
82         return;
83     }
84 
85     lenr = FLINT_MIN((len1 - 1) * (len2 - 1) + 1, n);
86     len1 = FLINT_MIN(len1, lenr);
87     len2 = FLINT_MIN(len2, lenr);
88 
89     if ((res != poly1) && (res != poly2))
90     {
91         acb_poly_fit_length(res, lenr);
92         _acb_poly_compose_series(res->coeffs, poly1->coeffs, len1,
93                                         poly2->coeffs, len2, lenr, prec);
94         _acb_poly_set_length(res, lenr);
95         _acb_poly_normalise(res);
96     }
97     else
98     {
99         acb_poly_t t;
100         acb_poly_init2(t, lenr);
101         _acb_poly_compose_series(t->coeffs, poly1->coeffs, len1,
102                                         poly2->coeffs, len2, lenr, prec);
103         _acb_poly_set_length(t, lenr);
104         _acb_poly_normalise(t);
105         acb_poly_swap(res, t);
106         acb_poly_clear(t);
107     }
108 }
109