1 /*
2     Copyright (C) 2020 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 <https://www.gnu.org/licenses/>.
10 */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include "fmpz_mod_mpoly.h"
15 
16 int
main(void)17 main(void)
18 {
19     slong i, j, max_threads = 5;
20     slong tmul = 10;
21 
22     FLINT_TEST_INIT(state);
23 
24     flint_printf("mul_johnson....");
25     fflush(stdout);
26 
27     for (i = 0; i < tmul * flint_test_multiplier(); i++)
28     {
29         fmpz_mod_mpoly_ctx_t ctx;
30         fmpz_mod_mpoly_t f, g, h, k1, k2, t1, t2;
31         slong len, len1, len2;
32         flint_bitcnt_t exp_bits, exp_bits1, exp_bits2;
33 
34         fmpz_mod_mpoly_ctx_init_rand_bits(ctx, state, 2, 200);
35 
36         fmpz_mod_mpoly_init(f, ctx);
37         fmpz_mod_mpoly_init(g, ctx);
38         fmpz_mod_mpoly_init(h, ctx);
39         fmpz_mod_mpoly_init(k1, ctx);
40         fmpz_mod_mpoly_init(k2, ctx);
41         fmpz_mod_mpoly_init(t1, ctx);
42         fmpz_mod_mpoly_init(t2, ctx);
43 
44         len = n_randint(state, 100);
45         len1 = n_randint(state, 100);
46         len2 = n_randint(state, 100);
47 
48         exp_bits = n_randint(state, 200) + 2;
49         exp_bits1 = n_randint(state, 200) + 2;
50         exp_bits2 = n_randint(state, 200) + 2;
51 
52         fmpz_mod_mpoly_randtest_bits(k1, state, len, exp_bits, ctx);
53         fmpz_mod_mpoly_randtest_bits(k2, state, len, exp_bits, ctx);
54 
55         for (j = 0; j < 4; j++)
56         {
57             fmpz_mod_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx);
58             fmpz_mod_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx);
59             fmpz_mod_mpoly_randtest_bits(h, state, len2, exp_bits, ctx);
60 
61             fmpz_mod_mpoly_add(t1, g, h, ctx);
62             fmpz_mod_mpoly_assert_canonical(t1, ctx);
63             fmpz_mod_mpoly_mul_johnson(k1, f, t1, ctx);
64             fmpz_mod_mpoly_assert_canonical(k1, ctx);
65             fmpz_mod_mpoly_mul_johnson(t1, f, g, ctx);
66             fmpz_mod_mpoly_assert_canonical(t1, ctx);
67             fmpz_mod_mpoly_mul_johnson(t2, f, h, ctx);
68             fmpz_mod_mpoly_assert_canonical(t2, ctx);
69             fmpz_mod_mpoly_add(k2, t1, t2, ctx);
70             fmpz_mod_mpoly_assert_canonical(k2, ctx);
71 
72             if (!fmpz_mod_mpoly_equal(k1, k2, ctx))
73             {
74                 flint_printf("FAIL: Check f*(g + h) = f*g + f*h\n");
75                 flint_printf("i = %wd, j = %wd\n", i ,j);
76                 flint_abort();
77             }
78         }
79 
80         fmpz_mod_mpoly_clear(f, ctx);
81         fmpz_mod_mpoly_clear(g, ctx);
82         fmpz_mod_mpoly_clear(h, ctx);
83         fmpz_mod_mpoly_clear(k1, ctx);
84         fmpz_mod_mpoly_clear(k2, ctx);
85         fmpz_mod_mpoly_clear(t1, ctx);
86         fmpz_mod_mpoly_clear(t2, ctx);
87         fmpz_mod_mpoly_ctx_clear(ctx);
88     }
89 
90     for (i = 0; i < tmul * flint_test_multiplier(); i++)
91     {
92         fmpz_mod_mpoly_ctx_t ctx;
93         fmpz_mod_mpoly_t f, g, h;
94         slong len, len1, len2, nvars;
95         flint_bitcnt_t exp_bound, exp_bound1, exp_bound2;
96 
97         fmpz_mod_mpoly_ctx_init_rand_bits(ctx, state, 4, 200);
98         nvars = ctx->minfo->nvars;
99 
100         fmpz_mod_mpoly_init(f, ctx);
101         fmpz_mod_mpoly_init(g, ctx);
102         fmpz_mod_mpoly_init(h, ctx);
103 
104         exp_bound = 3 + n_randint(state, 1 + 100/nvars/nvars);
105         exp_bound1 = 3 + n_randint(state, 1 + 100/nvars/nvars);
106         exp_bound2 = 3 + n_randint(state, 1 + 100/nvars/nvars);
107 
108         len = exp_bound + 1;
109         len1 = exp_bound1 + 1;
110         len2 = exp_bound2 + 1;
111         for (j = n_randint(state, nvars) + 2; j >= 0; j--)
112         {
113             len *= exp_bound + 1;
114             len1 *= exp_bound1 + 1;
115             len2 *= exp_bound2 + 1;
116             len = FLINT_MIN(len, WORD(100));
117             len1 = FLINT_MIN(len, WORD(100));
118             len2 = FLINT_MIN(len, WORD(100));
119         }
120 
121         for (j = 0; j < 4; j++)
122         {
123             fmpz_mod_mpoly_randtest_bound(f, state, len1, exp_bound1, ctx);
124             fmpz_mod_mpoly_randtest_bound(g, state, len2, exp_bound2, ctx);
125             fmpz_mod_mpoly_randtest_bound(h, state, len, exp_bound, ctx);
126 
127             flint_set_num_threads(n_randint(state, max_threads) + 1);
128 
129             fmpz_mod_mpoly_mul_johnson(h, f, g, ctx);
130             fmpz_mod_mpoly_assert_canonical(h, ctx);
131             fmpz_mod_mpoly_mul_johnson(f, f, g, ctx);
132             fmpz_mod_mpoly_assert_canonical(f, ctx);
133             if (!fmpz_mod_mpoly_equal(h, f, ctx))
134             {
135                 flint_printf("FAIL: Check aliasing first arg\n");
136                 flint_printf("i = %wd, j = %wd\n", i, j);
137                 flint_abort();
138             }
139 
140             fmpz_mod_mpoly_randtest_bound(f, state, len1, exp_bound1, ctx);
141             fmpz_mod_mpoly_randtest_bound(g, state, len2, exp_bound2, ctx);
142             fmpz_mod_mpoly_randtest_bound(h, state, len, exp_bound, ctx);
143 
144             flint_set_num_threads(n_randint(state, max_threads) + 1);
145 
146             fmpz_mod_mpoly_mul_johnson(h, f, g, ctx);
147             fmpz_mod_mpoly_assert_canonical(h, ctx);
148             fmpz_mod_mpoly_mul_johnson(g, f, g, ctx);
149             fmpz_mod_mpoly_assert_canonical(g, ctx);
150             if (!fmpz_mod_mpoly_equal(h, g, ctx))
151             {
152                 flint_printf("FAIL: Check aliasing second arg\n");
153                 flint_printf("i = %wd, j = %wd\n", i, j);
154                 flint_abort();
155             }
156         }
157 
158         fmpz_mod_mpoly_clear(f, ctx);
159         fmpz_mod_mpoly_clear(g, ctx);
160         fmpz_mod_mpoly_clear(h, ctx);
161         fmpz_mod_mpoly_ctx_clear(ctx);
162     }
163 
164     FLINT_TEST_CLEANUP(state);
165 
166     flint_printf("PASS\n");
167     return 0;
168 }
169 
170