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 
_acb_poly_tree_alloc(slong len)14 acb_ptr * _acb_poly_tree_alloc(slong len)
15 {
16     acb_ptr * tree = NULL;
17 
18     if (len)
19     {
20         slong i, height = FLINT_CLOG2(len);
21 
22         tree = flint_malloc(sizeof(acb_ptr) * (height + 1));
23         for (i = 0; i <= height; i++)
24             tree[i] = _acb_vec_init(len + (len >> i) + 1);
25     }
26 
27     return tree;
28 }
29 
_acb_poly_tree_free(acb_ptr * tree,slong len)30 void _acb_poly_tree_free(acb_ptr * tree, slong len)
31 {
32     if (len)
33     {
34         slong i, height = FLINT_CLOG2(len);
35 
36         for (i = 0; i <= height; i++)
37             _acb_vec_clear(tree[i], len + (len >> i) + 1);
38 
39         flint_free(tree);
40     }
41 }
42 
43 void
_acb_poly_tree_build(acb_ptr * tree,acb_srcptr roots,slong len,slong prec)44 _acb_poly_tree_build(acb_ptr * tree, acb_srcptr roots, slong len, slong prec)
45 {
46     slong height, pow, left, i;
47     acb_ptr pa, pb;
48     acb_srcptr a, b;
49 
50     if (len == 0)
51         return;
52 
53     height = FLINT_CLOG2(len);
54 
55     /* zeroth level, (x-a) */
56     for (i = 0; i < len; i++)
57     {
58         acb_one(tree[0] + (2 * i + 1));
59         acb_neg(tree[0] + (2 * i), roots + i);
60     }
61 
62     /* first level, (x-a)(x-b) = x^2 + (-a-b)*x + a*b */
63     if (height > 1)
64     {
65         pa = tree[1];
66 
67         for (i = 0; i < len / 2; i++)
68         {
69             a = (acb_srcptr) (roots + (2 * i));
70             b = (acb_srcptr) (roots + (2 * i + 1));
71 
72             acb_mul(pa + (3 * i), a, b, prec);
73             acb_add(pa + (3 * i + 1), a, b, prec);
74             acb_neg(pa + (3 * i + 1), pa + (3 * i + 1));
75             acb_one(pa + (3 * i + 2));
76         }
77 
78         if (len & 1)
79         {
80             acb_neg(pa + (3 * (len / 2)), roots + len - 1);
81             acb_one(pa + (3 * (len / 2) + 1));
82         }
83     }
84 
85     for (i = 1; i < height - 1; i++)
86     {
87         left = len;
88         pow = WORD(1) << i;
89         pa = tree[i];
90         pb = tree[i + 1];
91 
92         while (left >= 2 * pow)
93         {
94             _acb_poly_mul_monic(pb, pa, pow + 1, pa + pow + 1, pow + 1, prec);
95             left -= 2 * pow;
96             pa += 2 * pow + 2;
97             pb += 2 * pow + 1;
98         }
99 
100         if (left > pow)
101         {
102             _acb_poly_mul_monic(pb, pa, pow + 1, pa + pow + 1, left - pow + 1, prec);
103         }
104         else if (left > 0)
105             _acb_vec_set(pb, pa, left + 1);
106     }
107 }
108