1 /*
2     Copyright (C) 2010 William Hart
3     Copyright (C) 2013 Martin Lee
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 int
main(void)19 main(void)
20 {
21     int i, result;
22     FLINT_TEST_INIT(state);
23 
24     flint_printf("divrem_newton_n_preinv....");
25     fflush(stdout);
26 
27     /* Check result of divrem */
28     for (i = 0; i < 50 * flint_test_multiplier(); i++)
29     {
30         TEMPLATE(T, poly_t) a, b, binv, q, r, test;
31 
32         TEMPLATE(T, ctx_t) ctx;
33 
34         TEMPLATE(T, ctx_randtest) (ctx, state);
35 
36         TEMPLATE(T, poly_init) (a, ctx);
37         TEMPLATE(T, poly_init) (b, ctx);
38         TEMPLATE(T, poly_init) (binv, ctx);
39         TEMPLATE(T, poly_init) (q, ctx);
40         TEMPLATE(T, poly_init) (r, ctx);
41         TEMPLATE(T, poly_init) (test, ctx);
42 
43         do
44             TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 50), ctx);
45         while (b->length <= 2);
46         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 50), ctx);
47         if (a->length > 2 * (b->length) - 3)
48             TEMPLATE(T, poly_truncate) (a, 2 * (b->length) - 3, ctx);
49 
50         TEMPLATE(T, poly_reverse) (binv, b, b->length, ctx);
51         TEMPLATE(T, poly_inv_series_newton) (binv, binv, b->length, ctx);
52         TEMPLATE(T, poly_divrem_newton_n_preinv) (q, r, a, b, binv, ctx);
53         TEMPLATE(T, poly_mul) (test, q, b, ctx);
54         TEMPLATE(T, poly_add) (test, test, r, ctx);
55 
56         result = (TEMPLATE(T, poly_equal) (a, test, ctx));
57         if (!result)
58         {
59             flint_printf("FAIL:\n");
60             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
61             TEMPLATE(T, poly_print) (test, ctx), flint_printf("\n\n");
62             TEMPLATE(T, poly_print) (q, ctx), flint_printf("\n\n");
63             TEMPLATE(T, poly_print) (r, ctx), flint_printf("\n\n");
64             TEMPLATE(T, poly_print) (b, ctx), flint_printf("\n\n");
65             abort();
66         }
67 
68         TEMPLATE(T, poly_clear) (a, ctx);
69         TEMPLATE(T, poly_clear) (b, ctx);
70         TEMPLATE(T, poly_clear) (binv, ctx);
71         TEMPLATE(T, poly_clear) (q, ctx);
72         TEMPLATE(T, poly_clear) (r, ctx);
73         TEMPLATE(T, poly_clear) (test, ctx);
74         TEMPLATE(T, ctx_clear) (ctx);
75     }
76 
77     /* Check aliasing of a and q */
78     for (i = 0; i < 50 * flint_test_multiplier(); i++)
79     {
80         TEMPLATE(T, poly_t) a, b, binv, q, r;
81 
82         TEMPLATE(T, ctx_t) ctx;
83 
84         TEMPLATE(T, ctx_randtest) (ctx, state);
85 
86         TEMPLATE(T, poly_init) (a, ctx);
87         TEMPLATE(T, poly_init) (b, ctx);
88         TEMPLATE(T, poly_init) (binv, ctx);
89         TEMPLATE(T, poly_init) (q, ctx);
90         TEMPLATE(T, poly_init) (r, ctx);
91 
92         do
93             TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 50), ctx);
94         while (b->length <= 2);
95         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 50), ctx);
96         if (a->length > 2 * (b->length) - 3)
97             TEMPLATE(T, poly_truncate) (a, 2 * (b->length) - 3, ctx);
98 
99         TEMPLATE(T, poly_reverse) (binv, b, b->length, ctx);
100         TEMPLATE(T, poly_inv_series_newton) (binv, binv, b->length, ctx);
101 
102         TEMPLATE(T, poly_divrem_newton_n_preinv) (q, r, a, b, binv, ctx);
103         TEMPLATE(T, poly_divrem_newton_n_preinv) (a, r, a, b, binv, ctx);
104 
105         result = (TEMPLATE(T, poly_equal) (a, q, ctx));
106         if (!result)
107         {
108             flint_printf("FAIL:\n");
109             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
110             TEMPLATE(T, poly_print) (b, ctx), flint_printf("\n\n");
111             TEMPLATE(T, poly_print) (q, ctx), flint_printf("\n\n");
112             TEMPLATE(T, poly_print) (r, ctx), flint_printf("\n\n");
113             abort();
114         }
115 
116         TEMPLATE(T, poly_clear) (a, ctx);
117         TEMPLATE(T, poly_clear) (b, ctx);
118         TEMPLATE(T, poly_clear) (binv, ctx);
119         TEMPLATE(T, poly_clear) (q, ctx);
120         TEMPLATE(T, poly_clear) (r, ctx);
121         TEMPLATE(T, ctx_clear) (ctx);
122     }
123 
124     /* Check aliasing of b and q */
125     for (i = 0; i < 50 * flint_test_multiplier(); i++)
126     {
127         TEMPLATE(T, poly_t) a, b, binv, q, r;
128 
129         TEMPLATE(T, ctx_t) ctx;
130 
131         TEMPLATE(T, ctx_randtest) (ctx, state);
132 
133         TEMPLATE(T, poly_init) (a, ctx);
134         TEMPLATE(T, poly_init) (b, ctx);
135         TEMPLATE(T, poly_init) (binv, ctx);
136         TEMPLATE(T, poly_init) (q, ctx);
137         TEMPLATE(T, poly_init) (r, ctx);
138 
139         do
140             TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 50), ctx);
141         while (b->length <= 2);
142         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 50), ctx);
143         if (a->length > 2 * (b->length) - 3)
144             TEMPLATE(T, poly_truncate) (a, 2 * (b->length) - 3, ctx);
145 
146         TEMPLATE(T, poly_reverse) (binv, b, b->length, ctx);
147         TEMPLATE(T, poly_inv_series_newton) (binv, binv, b->length, ctx);
148 
149         TEMPLATE(T, poly_divrem_newton_n_preinv) (q, r, a, b, binv, ctx);
150         TEMPLATE(T, poly_divrem_newton_n_preinv) (b, r, a, b, binv, ctx);
151 
152         result = (TEMPLATE(T, poly_equal) (b, q, ctx));
153         if (!result)
154         {
155             flint_printf("FAIL:\n");
156             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
157             TEMPLATE(T, poly_print) (b, ctx), flint_printf("\n\n");
158             TEMPLATE(T, poly_print) (q, ctx), flint_printf("\n\n");
159             TEMPLATE(T, poly_print) (r, ctx), flint_printf("\n\n");
160             abort();
161         }
162 
163         TEMPLATE(T, poly_clear) (a, ctx);
164         TEMPLATE(T, poly_clear) (b, ctx);
165         TEMPLATE(T, poly_clear) (binv, ctx);
166         TEMPLATE(T, poly_clear) (q, ctx);
167         TEMPLATE(T, poly_clear) (r, ctx);
168         TEMPLATE(T, ctx_clear) (ctx);
169     }
170 
171     /* Check aliasing of binv and q */
172     for (i = 0; i < 50 * flint_test_multiplier(); i++)
173     {
174         TEMPLATE(T, poly_t) a, b, binv, q, r;
175 
176         TEMPLATE(T, ctx_t) ctx;
177 
178         TEMPLATE(T, ctx_randtest) (ctx, state);
179 
180         TEMPLATE(T, poly_init) (a, ctx);
181         TEMPLATE(T, poly_init) (b, ctx);
182         TEMPLATE(T, poly_init) (binv, ctx);
183         TEMPLATE(T, poly_init) (q, ctx);
184         TEMPLATE(T, poly_init) (r, ctx);
185 
186         do
187             TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 50), ctx);
188         while (b->length <= 2);
189         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 50), ctx);
190         if (a->length > 2 * (b->length) - 3)
191             TEMPLATE(T, poly_truncate) (a, 2 * (b->length) - 3, ctx);
192 
193         TEMPLATE(T, poly_reverse) (binv, b, b->length, ctx);
194         TEMPLATE(T, poly_inv_series_newton) (binv, binv, b->length, ctx);
195 
196         TEMPLATE(T, poly_divrem_newton_n_preinv) (q, r, a, b, binv, ctx);
197         TEMPLATE(T, poly_divrem_newton_n_preinv) (binv, r, a, b, binv, ctx);
198 
199         result = (TEMPLATE(T, poly_equal) (binv, q, ctx));
200         if (!result)
201         {
202             flint_printf("FAIL:\n");
203             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
204             TEMPLATE(T, poly_print) (b, ctx), flint_printf("\n\n");
205             TEMPLATE(T, poly_print) (binv, ctx), flint_printf("\n\n");
206             TEMPLATE(T, poly_print) (q, ctx), flint_printf("\n\n");
207             TEMPLATE(T, poly_print) (r, ctx), flint_printf("\n\n");
208             abort();
209         }
210 
211         TEMPLATE(T, poly_clear) (a, ctx);
212         TEMPLATE(T, poly_clear) (b, ctx);
213         TEMPLATE(T, poly_clear) (binv, ctx);
214         TEMPLATE(T, poly_clear) (q, ctx);
215         TEMPLATE(T, poly_clear) (r, ctx);
216         TEMPLATE(T, ctx_clear) (ctx);
217     }
218 
219     /* Check aliasing of a and r */
220     for (i = 0; i < 50 * flint_test_multiplier(); i++)
221     {
222         TEMPLATE(T, poly_t) a, b, binv, q, r;
223 
224         TEMPLATE(T, ctx_t) ctx;
225 
226         TEMPLATE(T, ctx_randtest) (ctx, state);
227 
228         TEMPLATE(T, poly_init) (a, ctx);
229         TEMPLATE(T, poly_init) (b, ctx);
230         TEMPLATE(T, poly_init) (binv, ctx);
231         TEMPLATE(T, poly_init) (q, ctx);
232         TEMPLATE(T, poly_init) (r, ctx);
233         do
234             TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 50), ctx);
235         while (b->length <= 2);
236         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 50), ctx);
237         if (a->length > 2 * (b->length) - 3)
238             TEMPLATE(T, poly_truncate) (a, 2 * (b->length) - 3, ctx);
239 
240         TEMPLATE(T, poly_reverse) (binv, b, b->length, ctx);
241         TEMPLATE(T, poly_inv_series_newton) (binv, binv, b->length, ctx);
242 
243         TEMPLATE(T, poly_divrem_newton_n_preinv) (q, r, a, b, binv, ctx);
244         TEMPLATE(T, poly_divrem_newton_n_preinv) (q, a, a, b, binv, ctx);
245 
246         result = (TEMPLATE(T, poly_equal) (a, r, ctx));
247         if (!result)
248         {
249             flint_printf("FAIL:\n");
250             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
251             TEMPLATE(T, poly_print) (b, ctx), flint_printf("\n\n");
252             TEMPLATE(T, poly_print) (q, ctx), flint_printf("\n\n");
253             TEMPLATE(T, poly_print) (r, ctx), flint_printf("\n\n");
254             abort();
255         }
256 
257         TEMPLATE(T, poly_clear) (a, ctx);
258         TEMPLATE(T, poly_clear) (b, ctx);
259         TEMPLATE(T, poly_clear) (binv, ctx);
260         TEMPLATE(T, poly_clear) (q, ctx);
261         TEMPLATE(T, poly_clear) (r, ctx);
262         TEMPLATE(T, ctx_clear) (ctx);
263     }
264 
265     /* Check aliasing of b and r */
266     for (i = 0; i < 50 * flint_test_multiplier(); i++)
267     {
268         TEMPLATE(T, poly_t) a, b, binv, q, r;
269 
270         TEMPLATE(T, ctx_t) ctx;
271 
272         TEMPLATE(T, ctx_randtest) (ctx, state);
273 
274         TEMPLATE(T, poly_init) (a, ctx);
275         TEMPLATE(T, poly_init) (b, ctx);
276         TEMPLATE(T, poly_init) (binv, ctx);
277         TEMPLATE(T, poly_init) (q, ctx);
278         TEMPLATE(T, poly_init) (r, ctx);
279 
280         do
281             TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 50), ctx);
282         while (b->length <= 2);
283         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 50), ctx);
284         if (a->length > 2 * (b->length) - 3)
285             TEMPLATE(T, poly_truncate) (a, 2 * (b->length) - 3, ctx);
286 
287         TEMPLATE(T, poly_reverse) (binv, b, b->length, ctx);
288         TEMPLATE(T, poly_inv_series_newton) (binv, binv, b->length, ctx);
289 
290         TEMPLATE(T, poly_divrem_newton_n_preinv) (q, r, a, b, binv, ctx);
291         TEMPLATE(T, poly_divrem_newton_n_preinv) (q, b, a, b, binv, ctx);
292 
293         result = (TEMPLATE(T, poly_equal) (b, r, ctx));
294         if (!result)
295         {
296             flint_printf("FAIL:\n");
297             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
298             TEMPLATE(T, poly_print) (b, ctx), flint_printf("\n\n");
299             TEMPLATE(T, poly_print) (q, ctx), flint_printf("\n\n");
300             TEMPLATE(T, poly_print) (r, ctx), flint_printf("\n\n");
301             abort();
302         }
303 
304         TEMPLATE(T, poly_clear) (a, ctx);
305         TEMPLATE(T, poly_clear) (b, ctx);
306         TEMPLATE(T, poly_clear) (binv, ctx);
307         TEMPLATE(T, poly_clear) (q, ctx);
308         TEMPLATE(T, poly_clear) (r, ctx);
309         TEMPLATE(T, ctx_clear) (ctx);
310     }
311 
312     /* Check aliasing of binv and r */
313     for (i = 0; i < 50 * flint_test_multiplier(); i++)
314     {
315         TEMPLATE(T, poly_t) a, b, binv, q, r;
316 
317         TEMPLATE(T, ctx_t) ctx;
318 
319         TEMPLATE(T, ctx_randtest) (ctx, state);
320 
321         TEMPLATE(T, poly_init) (a, ctx);
322         TEMPLATE(T, poly_init) (b, ctx);
323         TEMPLATE(T, poly_init) (binv, ctx);
324         TEMPLATE(T, poly_init) (q, ctx);
325         TEMPLATE(T, poly_init) (r, ctx);
326 
327         do
328             TEMPLATE(T, poly_randtest) (b, state, n_randint(state, 50), ctx);
329         while (b->length <= 2);
330         TEMPLATE(T, poly_randtest) (a, state, n_randint(state, 50), ctx);
331         if (a->length > 2 * (b->length) - 3)
332             TEMPLATE(T, poly_truncate) (a, 2 * (b->length) - 3, ctx);
333 
334         TEMPLATE(T, poly_reverse) (binv, b, b->length, ctx);
335         TEMPLATE(T, poly_inv_series_newton) (binv, binv, b->length, ctx);
336 
337         TEMPLATE(T, poly_divrem_newton_n_preinv) (q, r, a, b, binv, ctx);
338         TEMPLATE(T, poly_divrem_newton_n_preinv) (q, binv, a, b, binv, ctx);
339 
340         result = (TEMPLATE(T, poly_equal) (binv, r, ctx));
341         if (!result)
342         {
343             flint_printf("FAIL:\n");
344             TEMPLATE(T, poly_print) (a, ctx), flint_printf("\n\n");
345             TEMPLATE(T, poly_print) (b, ctx), flint_printf("\n\n");
346             TEMPLATE(T, poly_print) (binv, ctx), flint_printf("\n\n");
347             TEMPLATE(T, poly_print) (q, ctx), flint_printf("\n\n");
348             TEMPLATE(T, poly_print) (r, ctx), flint_printf("\n\n");
349             abort();
350         }
351 
352         TEMPLATE(T, poly_clear) (a, ctx);
353         TEMPLATE(T, poly_clear) (b, ctx);
354         TEMPLATE(T, poly_clear) (binv, ctx);
355         TEMPLATE(T, poly_clear) (q, ctx);
356         TEMPLATE(T, poly_clear) (r, ctx);
357         TEMPLATE(T, ctx_clear) (ctx);
358     }
359 
360     FLINT_TEST_CLEANUP(state);
361     flint_printf("PASS\n");
362     return 0;
363 }
364 
365 
366 #endif
367