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 "fq_nmod_mpoly.h"
15 
16 int
main(void)17 main(void)
18 {
19     slong i, j, v;
20     int tmul = 5;
21 #ifdef _WIN32
22     tmul = 1;
23 #endif
24     FLINT_TEST_INIT(state);
25 
26     flint_printf("evaluate....");
27     fflush(stdout);
28 
29     /* Check repeated evalone matches evalall */
30     for (i = 0; i < tmul * flint_test_multiplier(); i++)
31     {
32         fq_nmod_mpoly_ctx_t ctx;
33         fq_nmod_mpoly_t f;
34         fq_nmod_t fe;
35         fq_nmod_struct ** vals;
36         slong * perm;
37         slong nvars, len;
38         flint_bitcnt_t exp_bits;
39 
40         fq_nmod_mpoly_ctx_init_rand(ctx, state, 20, FLINT_BITS, 10);
41         nvars = ctx->minfo->nvars;
42 
43         fq_nmod_mpoly_init(f, ctx);
44         fq_nmod_init(fe, ctx->fqctx);
45 
46         len = n_randint(state, 50);
47         exp_bits = n_randint(state, 80) + 1;
48 
49         perm = (slong *) flint_malloc(nvars*sizeof(slong));
50         vals = (fq_nmod_struct **) flint_malloc(nvars*sizeof(fq_nmod_struct *));
51         for (v = 0; v < nvars; v++)
52         {
53             vals[v] = (fq_nmod_struct *) flint_malloc(sizeof(fq_nmod_struct));
54             fq_nmod_init(vals[v], ctx->fqctx);
55             fq_nmod_randtest(vals[v], state, ctx->fqctx);
56             perm[v] = v;
57         }
58 
59         for (j = 0; j < 2*nvars; j++)
60         {
61             slong a, b, c;
62             a = n_randint(state, nvars);
63             b = n_randint(state, nvars);
64             c = perm[a];
65             perm[a] = perm[b];
66             perm[b] = c;
67         }
68 
69         for (j = 0; j < 4; j++)
70         {
71             fq_nmod_mpoly_randtest_bits(f, state, len, exp_bits, ctx);
72             fq_nmod_mpoly_evaluate_all_fq_nmod(fe, f, vals, ctx);
73 
74             for (v = 0; v < nvars; v++)
75             {
76                 fq_nmod_mpoly_evaluate_one_fq_nmod(f, f, perm[v], vals[perm[v]], ctx);
77                 fq_nmod_mpoly_assert_canonical(f, ctx);
78             }
79             if (!fq_nmod_mpoly_equal_fq_nmod(f, fe, ctx))
80             {
81                 printf("FAIL\n");
82                 flint_printf("Check repeated evalone matches evalall\ni: %wd  j: %wd\n", i, j);
83                 flint_abort();
84             }
85         }
86 
87         for (v = 0; v < ctx->minfo->nvars; v++)
88         {
89             fq_nmod_clear(vals[v], ctx->fqctx);
90             flint_free(vals[v]);
91         }
92         flint_free(vals);
93 
94         fq_nmod_clear(fe, ctx->fqctx);
95         fq_nmod_mpoly_clear(f, ctx);
96         fq_nmod_mpoly_ctx_clear(ctx);
97 
98         flint_free(perm);
99     }
100 
101     /* Check add commutes with evalall */
102     for (i = 0; i < tmul * flint_test_multiplier(); i++)
103     {
104         fq_nmod_mpoly_ctx_t ctx;
105         fq_nmod_mpoly_t f, g, fg;
106         fq_nmod_t fe, ge, te, fge;
107         fq_nmod_struct ** vals;
108         slong nvars, len1, len2;
109         flint_bitcnt_t exp_bits1, exp_bits2;
110 
111         fq_nmod_mpoly_ctx_init_rand(ctx, state, 20, FLINT_BITS, 10);
112         nvars = ctx->minfo->nvars;
113 
114         fq_nmod_mpoly_init(f, ctx);
115         fq_nmod_mpoly_init(g, ctx);
116         fq_nmod_mpoly_init(fg, ctx);
117         fq_nmod_init(fe, ctx->fqctx);
118         fq_nmod_init(ge, ctx->fqctx);
119         fq_nmod_init(te, ctx->fqctx);
120         fq_nmod_init(fge, ctx->fqctx);
121 
122         len1 = n_randint(state, 100);
123         len2 = n_randint(state, 100);
124         exp_bits1 = n_randint(state, 80) + 1;
125         exp_bits2 = n_randint(state, 80) + 1;
126 
127         vals = (fq_nmod_struct **) flint_malloc(nvars*sizeof(fq_nmod_struct *));
128         for (v = 0; v < nvars; v++)
129         {
130             vals[v] = (fq_nmod_struct *) flint_malloc(sizeof(fq_nmod_struct));
131             fq_nmod_init(vals[v], ctx->fqctx);
132             fq_nmod_randtest(vals[v], state, ctx->fqctx);
133         }
134 
135         for (j = 0; j < 4; j++)
136         {
137             fq_nmod_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx);
138             fq_nmod_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx);
139             fq_nmod_mpoly_add(fg, f, g, ctx);
140 
141             fq_nmod_mpoly_evaluate_all_fq_nmod(fe, f, vals, ctx);
142             fq_nmod_mpoly_evaluate_all_fq_nmod(ge, g, vals, ctx);
143             fq_nmod_mpoly_evaluate_all_fq_nmod(fge, fg, vals, ctx);
144 
145             fq_nmod_add(te, fe, ge, ctx->fqctx);
146             if (!fq_nmod_equal(fge, te, ctx->fqctx))
147             {
148                 printf("FAIL\n");
149                 flint_printf("Check add commutes with evalall\ni: %wd  j: %wd\n", i, j);
150                 flint_abort();
151             }
152         }
153 
154         for (v = 0; v < nvars; v++)
155         {
156             fq_nmod_clear(vals[v], ctx->fqctx);
157             flint_free(vals[v]);
158         }
159         flint_free(vals);
160 
161         fq_nmod_clear(fe, ctx->fqctx);
162         fq_nmod_clear(ge, ctx->fqctx);
163         fq_nmod_clear(te, ctx->fqctx);
164         fq_nmod_clear(fge, ctx->fqctx);
165         fq_nmod_mpoly_clear(f, ctx);
166         fq_nmod_mpoly_clear(g, ctx);
167         fq_nmod_mpoly_clear(fg, ctx);
168         fq_nmod_mpoly_ctx_clear(ctx);
169     }
170 
171     /* Check mul commutes with evalall */
172     for (i = 0; i < tmul * flint_test_multiplier(); i++)
173     {
174         fq_nmod_mpoly_ctx_t ctx;
175         fq_nmod_mpoly_t f, g, fg;
176         fq_nmod_t fe, ge, te, fge;
177         fq_nmod_struct ** vals;
178         slong nvars, len1, len2;
179         flint_bitcnt_t exp_bits1, exp_bits2;
180 
181         fq_nmod_mpoly_ctx_init_rand(ctx, state, 20, FLINT_BITS, 10);
182         nvars = ctx->minfo->nvars;
183 
184         fq_nmod_mpoly_init(f, ctx);
185         fq_nmod_mpoly_init(g, ctx);
186         fq_nmod_mpoly_init(fg, ctx);
187         fq_nmod_init(fe, ctx->fqctx);
188         fq_nmod_init(ge, ctx->fqctx);
189         fq_nmod_init(te, ctx->fqctx);
190         fq_nmod_init(fge, ctx->fqctx);
191 
192         len1 = n_randint(state, 20);
193         len2 = n_randint(state, 20);
194         exp_bits1 = n_randint(state, 70) + 1;
195         exp_bits2 = n_randint(state, 70) + 1;
196 
197         vals = (fq_nmod_struct **) flint_malloc(nvars*sizeof(fq_nmod_struct *));
198         for (v = 0; v < nvars; v++)
199         {
200             vals[v] = (fq_nmod_struct *) flint_malloc(sizeof(fq_nmod_struct));
201             fq_nmod_init(vals[v], ctx->fqctx);
202             fq_nmod_randtest(vals[v], state, ctx->fqctx);
203         }
204 
205         for (j = 0; j < 4; j++)
206         {
207             fq_nmod_mpoly_randtest_bits(f, state, len1, exp_bits1, ctx);
208             fq_nmod_mpoly_randtest_bits(g, state, len2, exp_bits2, ctx);
209             fq_nmod_mpoly_mul(fg, f, g, ctx);
210 
211             fq_nmod_mpoly_evaluate_all_fq_nmod(fe, f, vals, ctx);
212             fq_nmod_mpoly_evaluate_all_fq_nmod(ge, g, vals, ctx);
213             fq_nmod_mpoly_evaluate_all_fq_nmod(fge, fg, vals, ctx);
214 
215             fq_nmod_mul(te, fe, ge, ctx->fqctx);
216             if (!fq_nmod_equal(fge, te, ctx->fqctx))
217             {
218                 printf("FAIL\n");
219                 flint_printf("Check add commutes with evalall\ni: %wd  j: %wd\n", i, j);
220                 flint_abort();
221             }
222         }
223 
224         for (v = 0; v < nvars; v++)
225         {
226             fq_nmod_clear(vals[v], ctx->fqctx);
227             flint_free(vals[v]);
228         }
229         flint_free(vals);
230 
231         fq_nmod_clear(fe, ctx->fqctx);
232         fq_nmod_clear(ge, ctx->fqctx);
233         fq_nmod_clear(te, ctx->fqctx);
234         fq_nmod_clear(fge, ctx->fqctx);
235         fq_nmod_mpoly_clear(f, ctx);
236         fq_nmod_mpoly_clear(g, ctx);
237         fq_nmod_mpoly_clear(fg, ctx);
238         fq_nmod_mpoly_ctx_clear(ctx);
239     }
240 
241     printf("PASS\n");
242     FLINT_TEST_CLEANUP(state);
243 
244     return 0;
245 }
246