1 /*
2     Copyright (C) 2018 Daniel Schultz
3 
4     This file is part of FLINT.
5 
6     FLINT 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 <stdio.h>
13 #include <stdlib.h>
14 #include "fmpq_mpoly.h"
15 
16 int
main(void)17 main(void)
18 {
19     int i, j, result;
20     FLINT_TEST_INIT(state);
21 
22     flint_printf("add/sub....");
23     fflush(stdout);
24 
25     /* Check (f + g) - g = f */
26     for (i = 0; i < 10 * flint_test_multiplier(); i++)
27     {
28         fmpq_mpoly_ctx_t ctx;
29         fmpq_mpoly_t f, g, h, k;
30         slong len, len1, len2;
31         flint_bitcnt_t coeff_bits, exp_bits, exp_bits1, exp_bits2;
32 
33         fmpq_mpoly_ctx_init_rand(ctx, state, 20);
34 
35         fmpq_mpoly_init(f, ctx);
36         fmpq_mpoly_init(g, ctx);
37         fmpq_mpoly_init(h, ctx);
38         fmpq_mpoly_init(k, ctx);
39 
40         len = n_randint(state, 100);
41         len1 = n_randint(state, 100);
42         len2 = n_randint(state, 100);
43 
44         exp_bits = n_randint(state, 200) + 2;
45         exp_bits1 = n_randint(state, 200) + 2;
46         exp_bits2 = n_randint(state, 200) + 2;
47 
48         coeff_bits = n_randint(state, 200);
49 
50         for (j = 0; j < 4; j++)
51         {
52             fmpq_mpoly_randtest_bits(f, state, len1, coeff_bits, exp_bits1, ctx);
53             fmpq_mpoly_randtest_bits(g, state, len2, coeff_bits, exp_bits2, ctx);
54             fmpq_mpoly_randtest_bits(h, state, len, coeff_bits, exp_bits, ctx);
55             fmpq_mpoly_randtest_bits(k, state, len, coeff_bits, exp_bits, ctx);
56 
57             fmpq_mpoly_add(h, g, f, ctx);
58             fmpq_mpoly_sub(k, h, g, ctx);
59             result = fmpq_mpoly_equal(f, k, ctx);
60 
61             if (!result)
62             {
63                 printf("FAIL\n");
64                 flint_printf("Check (f + g) - g = f\ni = %wd, j = %wd\n", i ,j);
65                 flint_abort();
66             }
67         }
68 
69         fmpq_mpoly_clear(f, ctx);
70         fmpq_mpoly_clear(g, ctx);
71         fmpq_mpoly_clear(h, ctx);
72         fmpq_mpoly_clear(k, ctx);
73         fmpq_mpoly_ctx_clear(ctx);
74     }
75 
76     /* Check f + g = g + f */
77     for (i = 0; i < 10 * flint_test_multiplier(); i++)
78     {
79         fmpq_mpoly_ctx_t ctx;
80         fmpq_mpoly_t f, g, h, k;
81         slong len, len1, len2;
82         flint_bitcnt_t coeff_bits, exp_bits, exp_bits1, exp_bits2;
83 
84         fmpq_mpoly_ctx_init_rand(ctx, state, 20);
85 
86         fmpq_mpoly_init(f, ctx);
87         fmpq_mpoly_init(g, ctx);
88         fmpq_mpoly_init(h, ctx);
89         fmpq_mpoly_init(k, ctx);
90 
91         len = n_randint(state, 100);
92         len1 = n_randint(state, 100);
93         len2 = n_randint(state, 100);
94 
95         exp_bits = n_randint(state, 200) + 2;
96         exp_bits1 = n_randint(state, 200) + 2;
97         exp_bits2 = n_randint(state, 200) + 2;
98 
99         coeff_bits = n_randint(state, 200);
100 
101         for (j = 0; j < 4; j++)
102         {
103             fmpq_mpoly_randtest_bits(f, state, len1, coeff_bits, exp_bits1, ctx);
104             fmpq_mpoly_randtest_bits(g, state, len2, coeff_bits, exp_bits2, ctx);
105             fmpq_mpoly_randtest_bits(h, state, len, coeff_bits, exp_bits, ctx);
106             fmpq_mpoly_randtest_bits(k, state, len, coeff_bits, exp_bits, ctx);
107 
108             fmpq_mpoly_add(h, f, g, ctx);
109             fmpq_mpoly_add(k, g, f, ctx);
110             result = fmpq_mpoly_equal(h, k, ctx);
111 
112             if (!result)
113             {
114                 printf("FAIL\n");
115                 flint_printf("Check f + g = g + f\ni = %wd, j = %wd\n", i ,j);
116                 flint_abort();
117             }
118         }
119 
120         fmpq_mpoly_clear(f, ctx);
121         fmpq_mpoly_clear(g, ctx);
122         fmpq_mpoly_clear(h, ctx);
123         fmpq_mpoly_clear(k, ctx);
124         fmpq_mpoly_ctx_clear(ctx);
125     }
126 
127     /* Check f - g = -g + f */
128     for (i = 0; i < 10 * flint_test_multiplier(); i++)
129     {
130         fmpq_mpoly_ctx_t ctx;
131         fmpq_mpoly_t f, g, h, k;
132         slong len, len1, len2;
133         flint_bitcnt_t coeff_bits, exp_bits, exp_bits1, exp_bits2;
134 
135         fmpq_mpoly_ctx_init_rand(ctx, state, 20);
136 
137         fmpq_mpoly_init(f, ctx);
138         fmpq_mpoly_init(g, ctx);
139         fmpq_mpoly_init(h, ctx);
140         fmpq_mpoly_init(k, ctx);
141 
142         len = n_randint(state, 100);
143         len1 = n_randint(state, 100);
144         len2 = n_randint(state, 100);
145 
146         exp_bits = n_randint(state, 200) + 2;
147         exp_bits1 = n_randint(state, 200) + 2;
148         exp_bits2 = n_randint(state, 200) + 2;
149 
150         coeff_bits = n_randint(state, 200);
151 
152         for (j = 0; j < 10; j++)
153         {
154             fmpq_mpoly_randtest_bits(f, state, len1, coeff_bits, exp_bits1, ctx);
155             fmpq_mpoly_randtest_bits(g, state, len2, coeff_bits, exp_bits2, ctx);
156             fmpq_mpoly_randtest_bits(h, state, len, coeff_bits, exp_bits, ctx);
157             fmpq_mpoly_randtest_bits(k, state, len, coeff_bits, exp_bits, ctx);
158 
159             fmpq_mpoly_sub(h, f, g, ctx);
160             fmpq_mpoly_neg(k, g, ctx);
161             fmpq_mpoly_add(k, k, f, ctx);
162             result = fmpq_mpoly_equal(h, k, ctx);
163 
164             if (!result)
165             {
166                 printf("FAIL\n");
167                 flint_printf("Check f + g = g + f\ni = %wd, j = %wd\n", i ,j);
168                 flint_abort();
169             }
170         }
171 
172         fmpq_mpoly_clear(f, ctx);
173         fmpq_mpoly_clear(g, ctx);
174         fmpq_mpoly_clear(h, ctx);
175         fmpq_mpoly_clear(k, ctx);
176         fmpq_mpoly_ctx_clear(ctx);
177     }
178 
179     /* Check f + (g + h) = (f + g) + h */
180     for (i = 0; i < 10 * flint_test_multiplier(); i++)
181     {
182         fmpq_mpoly_ctx_t ctx;
183         fmpq_mpoly_t f, g, h, k1, k2;
184         slong len, len1, len2;
185         flint_bitcnt_t coeff_bits, exp_bits, exp_bits1, exp_bits2;
186 
187         fmpq_mpoly_ctx_init_rand(ctx, state, 20);
188 
189         fmpq_mpoly_init(f, ctx);
190         fmpq_mpoly_init(g, ctx);
191         fmpq_mpoly_init(h, ctx);
192         fmpq_mpoly_init(k1, ctx);
193         fmpq_mpoly_init(k2, ctx);
194 
195         len = n_randint(state, 100);
196         len1 = n_randint(state, 100);
197         len2 = n_randint(state, 100);
198 
199         exp_bits = n_randint(state, 200) + 2;
200         exp_bits1 = n_randint(state, 200) + 2;
201         exp_bits2 = n_randint(state, 200) + 2;
202 
203         coeff_bits = n_randint(state, 200);
204 
205         for (j = 0; j < 4; j++)
206         {
207             fmpq_mpoly_randtest_bits(f, state, len1, coeff_bits, exp_bits1, ctx);
208             fmpq_mpoly_randtest_bits(g, state, len2, coeff_bits, exp_bits2, ctx);
209             fmpq_mpoly_randtest_bits(h, state, len, coeff_bits, exp_bits, ctx);
210             fmpq_mpoly_randtest_bits(k1, state, len, coeff_bits, exp_bits, ctx);
211             fmpq_mpoly_randtest_bits(k2, state, len, coeff_bits, exp_bits, ctx);
212 
213             fmpq_mpoly_add(k1, f, g, ctx);
214             fmpq_mpoly_add(k1, k1, h, ctx);
215             fmpq_mpoly_add(k2, g, h, ctx);
216             fmpq_mpoly_add(k2, k2, f, ctx);
217             result = fmpq_mpoly_equal(k1, k2, ctx);
218 
219             if (!result)
220             {
221                 printf("FAIL\n");
222                 flint_printf("Check f + (g + h) = (f + g) + h\ni = %wd, j = %wd\n", i ,j);
223                 flint_abort();
224             }
225         }
226 
227         fmpq_mpoly_clear(f, ctx);
228         fmpq_mpoly_clear(g, ctx);
229         fmpq_mpoly_clear(h, ctx);
230         fmpq_mpoly_clear(k1, ctx);
231         fmpq_mpoly_clear(k2, ctx);
232         fmpq_mpoly_ctx_clear(ctx);
233     }
234 
235     /* Check f - (g + h) = (f - g) - h */
236     for (i = 0; i < 10 * flint_test_multiplier(); i++)
237     {
238         fmpq_mpoly_ctx_t ctx;
239         fmpq_mpoly_t f, g, h, k1, k2;
240         slong len, len1, len2;
241         flint_bitcnt_t coeff_bits, exp_bits, exp_bits1, exp_bits2;
242 
243         fmpq_mpoly_ctx_init_rand(ctx, state, 20);
244 
245         fmpq_mpoly_init(f, ctx);
246         fmpq_mpoly_init(g, ctx);
247         fmpq_mpoly_init(h, ctx);
248         fmpq_mpoly_init(k1, ctx);
249         fmpq_mpoly_init(k2, ctx);
250 
251         len = n_randint(state, 100);
252         len1 = n_randint(state, 100);
253         len2 = n_randint(state, 100);
254 
255         exp_bits = n_randint(state, 200) + 2;
256         exp_bits1 = n_randint(state, 200) + 2;
257         exp_bits2 = n_randint(state, 200) + 2;
258 
259         coeff_bits = n_randint(state, 200);
260 
261         for (j = 0; j < 4; j++)
262         {
263             fmpq_mpoly_randtest_bits(f, state, len1, coeff_bits, exp_bits1, ctx);
264             fmpq_mpoly_randtest_bits(g, state, len2, coeff_bits, exp_bits2, ctx);
265             fmpq_mpoly_randtest_bits(h, state, len, coeff_bits, exp_bits, ctx);
266             fmpq_mpoly_randtest_bits(k1, state, len, coeff_bits, exp_bits, ctx);
267             fmpq_mpoly_randtest_bits(k2, state, len, coeff_bits, exp_bits, ctx);
268 
269             fmpq_mpoly_add(k1, g, h, ctx);
270             fmpq_mpoly_sub(k1, f, k1, ctx);
271             fmpq_mpoly_sub(k2, f, g, ctx);
272             fmpq_mpoly_sub(k2, k2, h, ctx);
273             result = fmpq_mpoly_equal(k1, k2, ctx);
274 
275             if (!result)
276             {
277                 printf("FAIL\n");
278                 flint_printf("Check f - (g + h) = (f - g) - h\ni = %wd, j = %wd\n", i ,j);
279                 flint_abort();
280             }
281         }
282 
283         fmpq_mpoly_clear(f, ctx);
284         fmpq_mpoly_clear(g, ctx);
285         fmpq_mpoly_clear(h, ctx);
286         fmpq_mpoly_clear(k1, ctx);
287         fmpq_mpoly_clear(k2, ctx);
288         fmpq_mpoly_ctx_clear(ctx);
289     }
290 
291     /* Check aliasing first arg */
292     for (i = 0; i < 10 * flint_test_multiplier(); i++)
293     {
294         fmpq_mpoly_ctx_t ctx;
295         fmpq_mpoly_t f, g, h;
296         slong len1, len2;
297         flint_bitcnt_t coeff_bits, exp_bits1, exp_bits2;
298 
299         fmpq_mpoly_ctx_init_rand(ctx, state, 20);
300 
301         fmpq_mpoly_init(f, ctx);
302         fmpq_mpoly_init(g, ctx);
303         fmpq_mpoly_init(h, ctx);
304 
305         len1 = n_randint(state, 100);
306         len2 = n_randint(state, 100);
307 
308         exp_bits1 = n_randint(state, 200) + 2;
309         exp_bits2 = n_randint(state, 200) + 2;
310 
311         coeff_bits = n_randint(state, 200);
312 
313         for (j = 0; j < 4; j++)
314         {
315             fmpq_mpoly_randtest_bits(f, state, len1, coeff_bits, exp_bits1, ctx);
316             fmpq_mpoly_randtest_bits(g, state, len2, coeff_bits, exp_bits2, ctx);
317             fmpq_mpoly_set(h, f, ctx);
318 
319             fmpq_mpoly_add(f, f, g, ctx);
320             fmpq_mpoly_assert_canonical(f, ctx);
321             fmpq_mpoly_sub(f, f, g, ctx);
322             fmpq_mpoly_assert_canonical(f, ctx);
323             result = fmpq_mpoly_equal(f, h, ctx);
324 
325             if (!result)
326             {
327                 printf("FAIL\n");
328                 flint_printf("Check aliasing first arg\ni = %wd, j = %wd\n", i ,j);
329                 flint_abort();
330             }
331         }
332 
333         fmpq_mpoly_clear(f, ctx);
334         fmpq_mpoly_clear(g, ctx);
335         fmpq_mpoly_clear(h, ctx);
336         fmpq_mpoly_ctx_clear(ctx);
337     }
338 
339     /* Check aliasing second arg */
340     for (i = 0; i < 10 * flint_test_multiplier(); i++)
341     {
342         fmpq_mpoly_ctx_t ctx;
343         fmpq_mpoly_t f, g, h;
344         slong len, len1, len2;
345         flint_bitcnt_t coeff_bits, exp_bits, exp_bits1, exp_bits2;
346 
347         fmpq_mpoly_ctx_init_rand(ctx, state, 20);
348 
349         fmpq_mpoly_init(f, ctx);
350         fmpq_mpoly_init(g, ctx);
351         fmpq_mpoly_init(h, ctx);
352 
353         len = n_randint(state, 100);
354         len1 = n_randint(state, 100);
355         len2 = n_randint(state, 100);
356 
357         exp_bits = n_randint(state, 200) + 2;
358         exp_bits1 = n_randint(state, 200) + 2;
359         exp_bits2 = n_randint(state, 200) + 2;
360 
361         coeff_bits = n_randint(state, 200);
362 
363         for (j = 0; j < 4; j++)
364         {
365             fmpq_mpoly_randtest_bits(f, state, len1, coeff_bits, exp_bits1, ctx);
366             fmpq_mpoly_randtest_bits(g, state, len2, coeff_bits, exp_bits2, ctx);
367             fmpq_mpoly_randtest_bits(h, state, len, coeff_bits, exp_bits, ctx);
368 
369             if ((j % 2) == 0)
370             {
371                 fmpq_mpoly_add(h, g, f, ctx);
372                 fmpq_mpoly_assert_canonical(h, ctx);
373                 fmpq_mpoly_add(f, g, f, ctx);
374                 fmpq_mpoly_assert_canonical(f, ctx);
375             } else
376             {
377                 fmpq_mpoly_sub(h, g, f, ctx);
378                 fmpq_mpoly_assert_canonical(h, ctx);
379                 fmpq_mpoly_sub(f, g, f, ctx);
380                 fmpq_mpoly_assert_canonical(f, ctx);
381             }
382             result = fmpq_mpoly_equal(f, h, ctx);
383 
384             if (!result)
385             {
386                 printf("FAIL\n");
387                 flint_printf("Check aliasing second arg\ni = %wd, j = %wd\n", i ,j);
388                 flint_abort();
389             }
390         }
391 
392         fmpq_mpoly_clear(f, ctx);
393         fmpq_mpoly_clear(g, ctx);
394         fmpq_mpoly_clear(h, ctx);
395         fmpq_mpoly_ctx_clear(ctx);
396     }
397 
398     FLINT_TEST_CLEANUP(state);
399 
400     flint_printf("PASS\n");
401     return 0;
402 }
403 
404