1 /*
2     Copyright (C) 2012 Sebastian Pancratz
3     Copyright (C) 2012 Andres Goens
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 <https://www.gnu.org/licenses/>.
12 */
13 
14 #ifdef T
15 
16 #include "templates.h"
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include "ulong_extras.h"
22 #include "long_extras.h"
23 
24 int
main(void)25 main(void)
26 {
27     int i, result;
28     FLINT_TEST_INIT(state);
29 
30     flint_printf("mul_reorder... ");
31     fflush(stdout);
32 
33     /* Check aliasing: a = a * b */
34     for (i = 0; i < 200 * flint_test_multiplier(); i++)
35     {
36         slong len;
37         TEMPLATE(T, ctx_t) ctx;
38 
39         TEMPLATE(T, poly_t) a, b, c;
40 
41         len = n_randint(state, 15) + 1;
42         TEMPLATE(T, ctx_randtest) (ctx, state);
43         TEMPLATE(T, poly_init) (a, ctx);
44         TEMPLATE(T, poly_init) (b, ctx);
45         TEMPLATE(T, poly_init) (c, ctx);
46 
47         TEMPLATE(T, poly_randtest) (a, state, len, ctx);
48         TEMPLATE(T, poly_randtest) (b, state, len, ctx);
49 
50         TEMPLATE(T, poly_mul_reorder) (c, a, b, ctx);
51         TEMPLATE(T, poly_mul_reorder) (a, a, b, ctx);
52 
53         result = (TEMPLATE(T, poly_equal) (a, c, ctx));
54         if (!result)
55         {
56             flint_printf("FAIL (alias a = a * b):\n\n");
57             flint_printf("a = "), TEMPLATE(T, poly_print_pretty) (a, "X", ctx),
58                 flint_printf("\n");
59             flint_printf("b = "), TEMPLATE(T, poly_print_pretty) (b, "X", ctx),
60                 flint_printf("\n");
61             flint_printf("c = "), TEMPLATE(T, poly_print_pretty) (c, "X", ctx),
62                 flint_printf("\n");
63             abort();
64         }
65 
66         TEMPLATE(T, poly_clear) (a, ctx);
67         TEMPLATE(T, poly_clear) (b, ctx);
68         TEMPLATE(T, poly_clear) (c, ctx);
69 
70         TEMPLATE(T, ctx_clear) (ctx);
71     }
72 
73     /* Check aliasing: b = a * b */
74     for (i = 0; i < 200 * flint_test_multiplier(); i++)
75     {
76         slong len;
77         TEMPLATE(T, ctx_t) ctx;
78 
79         TEMPLATE(T, poly_t) a, b, c;
80 
81         len = n_randint(state, 15) + 1;
82         TEMPLATE(T, ctx_randtest) (ctx, state);
83         TEMPLATE(T, poly_init) (a, ctx);
84         TEMPLATE(T, poly_init) (b, ctx);
85         TEMPLATE(T, poly_init) (c, ctx);
86 
87         TEMPLATE(T, poly_randtest) (a, state, len, ctx);
88         TEMPLATE(T, poly_randtest) (b, state, len, ctx);
89 
90         TEMPLATE(T, poly_mul_reorder) (c, a, b, ctx);
91         TEMPLATE(T, poly_mul_reorder) (b, a, b, ctx);
92 
93         result = (TEMPLATE(T, poly_equal) (b, c, ctx));
94         if (!result)
95         {
96             flint_printf("FAIL (b = a * b):\n\n");
97             flint_printf("a = "), TEMPLATE(T, poly_print_pretty) (a, "X", ctx),
98                 flint_printf("\n");
99             flint_printf("b = "), TEMPLATE(T, poly_print_pretty) (b, "X", ctx),
100                 flint_printf("\n");
101             flint_printf("c = "), TEMPLATE(T, poly_print_pretty) (c, "X", ctx),
102                 flint_printf("\n");
103             abort();
104         }
105 
106         TEMPLATE(T, poly_clear) (a, ctx);
107         TEMPLATE(T, poly_clear) (b, ctx);
108         TEMPLATE(T, poly_clear) (c, ctx);
109 
110         TEMPLATE(T, ctx_clear) (ctx);
111     }
112 
113     /* Check aliasing: a = a * a */
114     for (i = 0; i < 200 * flint_test_multiplier(); i++)
115     {
116         slong len;
117         TEMPLATE(T, ctx_t) ctx;
118 
119         TEMPLATE(T, poly_t) a, c;
120 
121         len = n_randint(state, 15) + 1;
122         TEMPLATE(T, ctx_randtest) (ctx, state);
123         TEMPLATE(T, poly_init) (a, ctx);
124         TEMPLATE(T, poly_init) (c, ctx);
125 
126         TEMPLATE(T, poly_randtest) (a, state, len, ctx);
127 
128         TEMPLATE(T, poly_mul_reorder) (c, a, a, ctx);
129         TEMPLATE(T, poly_mul_reorder) (a, a, a, ctx);
130 
131         result = (TEMPLATE(T, poly_equal) (a, c, ctx));
132         if (!result)
133         {
134             flint_printf("FAIL (alias a = a * a):\n\n");
135             flint_printf("a = "), TEMPLATE(T, poly_print_pretty) (a, "X", ctx),
136                 flint_printf("\n");
137             flint_printf("c = "), TEMPLATE(T, poly_print_pretty) (c, "X", ctx),
138                 flint_printf("\n");
139             abort();
140         }
141 
142         TEMPLATE(T, poly_clear) (a, ctx);
143         TEMPLATE(T, poly_clear) (c, ctx);
144 
145         TEMPLATE(T, ctx_clear) (ctx);
146     }
147 
148     /* Check that a * b == b * a */
149     for (i = 0; i < 200 * flint_test_multiplier(); i++)
150     {
151         slong len;
152         TEMPLATE(T, ctx_t) ctx;
153 
154         TEMPLATE(T, poly_t) a, b, c, e;
155 
156         len = n_randint(state, 15) + 1;
157         TEMPLATE(T, ctx_randtest) (ctx, state);
158         TEMPLATE(T, poly_init) (a, ctx);
159         TEMPLATE(T, poly_init) (b, ctx);
160         TEMPLATE(T, poly_init) (c, ctx);
161         TEMPLATE(T, poly_init) (e, ctx);
162 
163         TEMPLATE(T, poly_randtest) (a, state, len, ctx);
164         TEMPLATE(T, poly_randtest) (b, state, len, ctx);
165 
166         TEMPLATE(T, poly_mul_reorder) (c, a, b, ctx);
167         TEMPLATE(T, poly_mul_reorder) (e, b, a, ctx);
168 
169         result = (TEMPLATE(T, poly_equal) (e, c, ctx));
170         if (!result)
171         {
172             flint_printf("FAIL (a * b == b * a):\n\n");
173             flint_printf("a = "), TEMPLATE(T, poly_print_pretty) (a, "X", ctx),
174                 flint_printf("\n");
175             flint_printf("b = "), TEMPLATE(T, poly_print_pretty) (b, "X", ctx),
176                 flint_printf("\n");
177             flint_printf("c = "), TEMPLATE(T, poly_print_pretty) (c, "X", ctx),
178                 flint_printf("\n");
179             flint_printf("e = "), TEMPLATE(T, poly_print_pretty) (e, "X", ctx),
180                 flint_printf("\n");
181             abort();
182         }
183 
184         TEMPLATE(T, poly_clear) (a, ctx);
185         TEMPLATE(T, poly_clear) (b, ctx);
186         TEMPLATE(T, poly_clear) (c, ctx);
187         TEMPLATE(T, poly_clear) (e, ctx);
188 
189         TEMPLATE(T, ctx_clear) (ctx);
190     }
191 
192     /* Check that (b*c)+(b*d) = b*(c+d) */
193     for (i = 0; i < 200 * flint_test_multiplier(); i++)
194     {
195         slong len;
196         TEMPLATE(T, ctx_t) ctx;
197 
198         TEMPLATE(T, poly_t) a1, a2, b, c, d;
199 
200         len = n_randint(state, 15) + 1;
201         TEMPLATE(T, ctx_randtest) (ctx, state);
202         TEMPLATE(T, poly_init) (a1, ctx);
203         TEMPLATE(T, poly_init) (a2, ctx);
204         TEMPLATE(T, poly_init) (b, ctx);
205         TEMPLATE(T, poly_init) (c, ctx);
206         TEMPLATE(T, poly_init) (d, ctx);
207 
208         TEMPLATE(T, poly_randtest) (b, state, len, ctx);
209         TEMPLATE(T, poly_randtest) (c, state, len, ctx);
210         TEMPLATE(T, poly_randtest) (d, state, len, ctx);
211 
212         TEMPLATE(T, poly_mul_reorder) (a1, b, c, ctx);
213         TEMPLATE(T, poly_mul_reorder) (a2, b, d, ctx);
214         TEMPLATE(T, poly_add) (a1, a1, a2, ctx);
215 
216         TEMPLATE(T, poly_add) (c, c, d, ctx);
217         TEMPLATE(T, poly_mul_reorder) (a2, b, c, ctx);
218 
219         result = (TEMPLATE(T, poly_equal) (a1, a2, ctx));
220         if (!result)
221         {
222             flint_printf("FAIL ((b*c)+(b*d) == b*(c+d)):\n\n");
223             flint_printf("a1 = "), TEMPLATE(T, poly_print_pretty) (a1, "X",
224                                                                    ctx),
225                 flint_printf("\n");
226             flint_printf("a2 = "), TEMPLATE(T, poly_print_pretty) (a2, "X",
227                                                                    ctx),
228                 flint_printf("\n");
229             flint_printf("b  = "), TEMPLATE(T, poly_print_pretty) (b, "X",
230                                                                    ctx),
231                 flint_printf("\n");
232             flint_printf("c  = "), TEMPLATE(T, poly_print_pretty) (c, "X",
233                                                                    ctx),
234                 flint_printf("\n");
235             flint_printf("d  = "), TEMPLATE(T, poly_print_pretty) (d, "X",
236                                                                    ctx),
237                 flint_printf("\n");
238             abort();
239         }
240 
241         TEMPLATE(T, poly_clear) (a1, ctx);
242         TEMPLATE(T, poly_clear) (a2, ctx);
243         TEMPLATE(T, poly_clear) (b, ctx);
244         TEMPLATE(T, poly_clear) (c, ctx);
245         TEMPLATE(T, poly_clear) (d, ctx);
246 
247         TEMPLATE(T, ctx_clear) (ctx);
248     }
249 
250     FLINT_TEST_CLEANUP(state);
251     flint_printf("PASS\n");
252     return EXIT_SUCCESS;
253 }
254 
255 
256 
257 #endif
258