1 /*
2     Copyright (C) 2011 Fredrik Johansson
3     Copyright (C) 2012 Lina Kulakova
4     Copyright (C) 2013 Mike Hansen
5 
6     This file is part of FLINT.
7 
8     FLINT is free software: you can redistribute it and/or modify it under
9     the terms of the GNU Lesser General Public License (LGPL) as published
10     by the Free Software Foundation; either version 2.1 of the License, or
11     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
12 */
13 
14 #ifdef T
15 
16 #include "templates.h"
17 
18 #include <gmp.h>
19 #include "flint.h"
20 #include "ulong_extras.h"
21 void
_TEMPLATE(T,poly_compose_mod_horner_preinv)22 _TEMPLATE(T, poly_compose_mod_horner_preinv) (
23     TEMPLATE(T, struct) * res,
24     const TEMPLATE(T, struct) * f, slong lenf,
25     const TEMPLATE(T, struct) * g,
26     const TEMPLATE(T, struct) * h, slong lenh,
27     const TEMPLATE(T, struct) * hinv, slong lenhinv,
28     const TEMPLATE(T, ctx_t) ctx)
29 {
30     slong i, len;
31     TEMPLATE(T, struct) * t;
32 
33     if (lenh == 1)
34         return;
35 
36     if (lenf == 1)
37     {
38         TEMPLATE(T, set) (res, f, ctx);
39         return;
40     }
41 
42     if (lenh == 2)
43     {
44         _TEMPLATE(T, TEMPLATE(poly_evaluate, T)) (res, f, lenf, g, ctx);
45         return;
46     }
47 
48     len = lenh - 1;
49     i = lenf - 1;
50     t = _TEMPLATE(T, vec_init) (2 * lenh - 3, ctx);
51 
52     _TEMPLATE(T, TEMPLATE(poly_scalar_mul, T)) (res, g, len, f + i, ctx);
53     i--;
54     if (i >= 0)
55     {
56         TEMPLATE(T, add) (res, res, f + i, ctx);
57     }
58 
59     while (i > 0)
60     {
61         i--;
62         _TEMPLATE(T, poly_mulmod_preinv) (t, res, len, g, len, h, lenh, hinv,
63                                           lenhinv, ctx);
64         _TEMPLATE(T, poly_add) (res, t, len, f + i, 1, ctx);
65     }
66 
67     _TEMPLATE(T, vec_clear) (t, 2 * lenh - 3, ctx);
68 }
69 
70 void
TEMPLATE(T,poly_compose_mod_horner_preinv)71 TEMPLATE(T, poly_compose_mod_horner_preinv) (
72     TEMPLATE(T, poly_t) res,
73     const TEMPLATE(T, poly_t) poly1,
74     const TEMPLATE(T, poly_t) poly2,
75     const TEMPLATE(T, poly_t) poly3,
76     const TEMPLATE(T, poly_t) poly3inv,
77     const TEMPLATE(T, ctx_t) ctx)
78 {
79     TEMPLATE(T, t) inv3;
80     slong len1 = poly1->length;
81     slong len2 = poly2->length;
82     slong len3 = poly3->length;
83     slong len3inv = poly3inv->length;
84     slong len = len3 - 1;
85     slong vec_len = FLINT_MAX(len3 - 1, len2);
86 
87     TEMPLATE(T, struct) * ptr2;
88 
89     if (len3 == 0)
90     {
91         TEMPLATE_PRINTF
92             ("Exception: division by zero in %s_poly_compose_mod_horner\n", T);
93         flint_abort();
94     }
95 
96     if (len1 == 0 || len3 == 1)
97     {
98         TEMPLATE(T, poly_zero) (res, ctx);
99         return;
100     }
101 
102     if (len1 == 1)
103     {
104         TEMPLATE(T, poly_set) (res, poly1, ctx);
105         return;
106     }
107 
108     if (res == poly3 || res == poly1)
109     {
110         TEMPLATE(T, poly_t) tmp;
111         TEMPLATE(T, poly_init) (tmp, ctx);
112         TEMPLATE(T, poly_compose_mod_horner_preinv) (tmp, poly1, poly2, poly3,
113                                                      poly3inv, ctx);
114         TEMPLATE(T, poly_swap) (tmp, res, ctx);
115         TEMPLATE(T, poly_clear) (tmp, ctx);
116         return;
117     }
118 
119     ptr2 = _TEMPLATE(T, vec_init) (vec_len, ctx);
120 
121     if (len2 <= len3 - 1)
122     {
123         _TEMPLATE(T, vec_set) (ptr2, poly2->coeffs, len2, ctx);
124         _TEMPLATE(T, vec_zero) (ptr2 + len2, vec_len - len2, ctx);
125     }
126     else
127     {
128         TEMPLATE(T, init) (inv3, ctx);
129         TEMPLATE(T, inv) (inv3, poly3->coeffs + len, ctx);
130         _TEMPLATE(T, poly_rem) (ptr2, poly2->coeffs, len2,
131                                 poly3->coeffs, len3, inv3, ctx);
132         TEMPLATE(T, clear) (inv3, ctx);
133     }
134 
135     TEMPLATE(T, poly_fit_length) (res, len, ctx);
136     _TEMPLATE(T, poly_compose_mod_horner_preinv) (res->coeffs,
137                                                   poly1->coeffs, len1,
138                                                   ptr2,
139                                                   poly3->coeffs, len3,
140                                                   poly3inv->coeffs, len3inv,
141                                                   ctx);
142     _TEMPLATE(T, poly_set_length) (res, len, ctx);
143     _TEMPLATE(T, poly_normalise) (res, ctx);
144 
145     _TEMPLATE(T, vec_clear) (ptr2, vec_len, ctx);
146 }
147 
148 
149 #endif
150