1 /*
2 Copyright (C) 2011 Fredrik Johansson
3 Copyright (C) 2012 Lina Kulakova
4
5 This file is part of FLINT.
6
7 FLINT is free software: you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License (LGPL) as published
9 by the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version. See <https://www.gnu.org/licenses/>.
11 */
12
13 #include <gmp.h>
14 #include "flint.h"
15 #include "fmpz_vec.h"
16 #include "fmpz_mod_poly.h"
17 #include "ulong_extras.h"
18
19 void
_fmpz_mod_poly_compose_mod_horner(fmpz * res,const fmpz * f,slong lenf,const fmpz * g,const fmpz * h,slong lenh,const fmpz_t p)20 _fmpz_mod_poly_compose_mod_horner(fmpz * res, const fmpz * f, slong lenf, const fmpz * g,
21 const fmpz * h, slong lenh, const fmpz_t p)
22 {
23 slong i, len;
24 fmpz * t;
25
26 if (lenh == 1)
27 return;
28
29 if (lenf == 1)
30 {
31 fmpz_set(res, f);
32 return;
33 }
34
35 if (lenh == 2)
36 {
37 _fmpz_mod_poly_evaluate_fmpz(res, f, lenf, g, p);
38 return;
39 }
40
41 len = lenh - 1;
42 i = lenf - 1;
43 t = _fmpz_vec_init(2 * lenh - 3);
44
45 _fmpz_mod_poly_scalar_mul_fmpz(res, g, len, f + i, p);
46 i--;
47 if (i >= 0)
48 {
49 fmpz_add(res, res, f + i);
50 fmpz_mod(res, res, p);
51 }
52
53 while (i > 0)
54 {
55 i--;
56 _fmpz_mod_poly_mulmod(t, res, len, g, len, h, lenh, p);
57 _fmpz_mod_poly_add(res, t, len, f + i, 1, p);
58 }
59
60 _fmpz_vec_clear(t, 2 * lenh - 3);
61 }
62
fmpz_mod_poly_compose_mod_horner(fmpz_mod_poly_t res,const fmpz_mod_poly_t poly1,const fmpz_mod_poly_t poly2,const fmpz_mod_poly_t poly3,const fmpz_mod_ctx_t ctx)63 void fmpz_mod_poly_compose_mod_horner(fmpz_mod_poly_t res,
64 const fmpz_mod_poly_t poly1, const fmpz_mod_poly_t poly2,
65 const fmpz_mod_poly_t poly3, const fmpz_mod_ctx_t ctx)
66 {
67 fmpz_t inv3;
68 slong len1 = poly1->length;
69 slong len2 = poly2->length;
70 slong len3 = poly3->length;
71 slong len = len3 - 1;
72 slong vec_len = FLINT_MAX(len3 - 1, len2);
73
74 fmpz * ptr2;
75
76 if (len3 == 0)
77 {
78 flint_printf("Exception (fmpz_mod_poly_compose_mod_horner). Division by zero \n");
79 flint_abort();
80 }
81
82 if (len1 == 0 || len3 == 1)
83 {
84 fmpz_mod_poly_zero(res, ctx);
85 return;
86 }
87
88 if (len1 == 1)
89 {
90 fmpz_mod_poly_set(res, poly1, ctx);
91 return;
92 }
93
94 if (res == poly3 || res == poly1)
95 {
96 fmpz_mod_poly_t tmp;
97 fmpz_mod_poly_init(tmp, ctx);
98 fmpz_mod_poly_compose_mod_horner(tmp, poly1, poly2, poly3, ctx);
99 fmpz_mod_poly_swap(tmp, res, ctx);
100 fmpz_mod_poly_clear(tmp, ctx);
101 return;
102 }
103
104 ptr2 = _fmpz_vec_init(vec_len);
105
106 if (len2 <= len3 - 1)
107 {
108 _fmpz_vec_set(ptr2, poly2->coeffs, len2);
109 _fmpz_vec_zero(ptr2 + len2, vec_len - len2);
110 }
111 else
112 {
113 fmpz_init(inv3);
114 fmpz_invmod(inv3, poly3->coeffs + len, fmpz_mod_ctx_modulus(ctx));
115 _fmpz_mod_poly_rem(ptr2, poly2->coeffs, len2,
116 poly3->coeffs, len3, inv3, fmpz_mod_ctx_modulus(ctx));
117 fmpz_clear(inv3);
118 }
119
120 fmpz_mod_poly_fit_length(res, len, ctx);
121 _fmpz_mod_poly_compose_mod_horner(res->coeffs, poly1->coeffs, len1,
122 ptr2, poly3->coeffs, len3, fmpz_mod_ctx_modulus(ctx));
123 _fmpz_mod_poly_set_length(res, len);
124 _fmpz_mod_poly_normalise(res);
125
126 _fmpz_vec_clear(ptr2, vec_len);
127 }
128