1 /*
2     Copyright (C) 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_evaluate2_rectangular(acb_t y,acb_t z,acb_srcptr poly,slong len,const acb_t x,slong prec)15 _acb_poly_evaluate2_rectangular(acb_t y, acb_t z, acb_srcptr poly,
16     slong len, const acb_t x, slong prec)
17 {
18     slong i, j, m, r;
19     acb_ptr xs;
20     acb_t s, t, c;
21 
22     if (len < 3)
23     {
24         if (len == 0)
25         {
26             acb_zero(y);
27             acb_zero(z);
28         }
29         else if (len == 1)
30         {
31             acb_set_round(y, poly + 0, prec);
32             acb_zero(z);
33         }
34         else if (len == 2)
35         {
36             acb_mul(y, x, poly + 1, prec);
37             acb_add(y, y, poly + 0, prec);
38             acb_set_round(z, poly + 1, prec);
39         }
40         return;
41     }
42 
43     m = n_sqrt(len) + 1;
44     m *= 1;
45 
46     r = (len + m - 1) / m;
47 
48     xs = _acb_vec_init(m + 1);
49     acb_init(s);
50     acb_init(t);
51     acb_init(c);
52 
53     _acb_vec_set_powers(xs, x, m + 1, prec);
54     acb_dot(y, poly + (r - 1) * m, 0, xs + 1, 1,
55         poly + (r - 1) * m + 1, 1, len - (r - 1) * m - 1, prec);
56 
57     for (i = r - 2; i >= 0; i--)
58     {
59         acb_dot(s, poly + i * m, 0, xs + 1, 1,
60             poly + i * m + 1, 1, m - 1, prec);
61         acb_mul(y, y, xs + m, prec);
62         acb_add(y, y, s, prec);
63     }
64 
65     /* todo: rewrite using acb_dot */
66     len -= 1;
67     r = (len + m - 1) / m;
68     acb_mul_ui(z, poly + (r - 1) * m + 1, (r - 1) * m + 1, ARF_PREC_EXACT);
69     for (j = 1; (r - 1) * m + j < len; j++)
70     {
71         acb_mul_ui(c, poly + (r - 1) * m + j + 1, (r - 1) * m + j + 1, ARF_PREC_EXACT);
72         acb_addmul(z, xs + j, c, prec);
73     }
74 
75     for (i = r - 2; i >= 0; i--)
76     {
77         acb_mul_ui(s, poly + i * m + 1, i * m + 1, ARF_PREC_EXACT);
78 
79         for (j = 1; j < m; j++)
80         {
81             acb_mul_ui(c, poly + i * m + j + 1, i * m + j + 1, ARF_PREC_EXACT);
82             acb_addmul(s, xs + j, c, prec);
83         }
84 
85         acb_mul(z, z, xs + m, prec);
86         acb_add(z, z, s, prec);
87     }
88 
89     _acb_vec_clear(xs, m + 1);
90     acb_clear(s);
91     acb_clear(t);
92     acb_clear(c);
93 }
94 
95 void
acb_poly_evaluate2_rectangular(acb_t r,acb_t s,const acb_poly_t f,const acb_t a,slong prec)96 acb_poly_evaluate2_rectangular(acb_t r, acb_t s, const acb_poly_t f, const acb_t a, slong prec)
97 {
98     _acb_poly_evaluate2_rectangular(r, s, f->coeffs, f->length, a, prec);
99 }
100 
101