1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Copyright (C) 2018 Daniel Schultz
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
void fmpz_mpolyd_swap(fmpz_mpolyd_t A, fmpz_mpolyd_t B)
{
fmpz_mpolyd_struct t = *A;
*A = *B;
*B = t;
}
void fmpz_mpolyd_ctx_init(fmpz_mpolyd_ctx_t dctx, slong nvars)
{
slong i;
dctx->nvars = nvars;
dctx->perm = (slong *) flint_malloc(nvars*sizeof(slong));
for (i = 0; i < nvars; i++)
{
dctx->perm[i] = i;
}
}
int fmpz_mpolyd_ctx_init_version1(fmpz_mpolyd_ctx_t dctx,
const fmpz_mpoly_t A, const fmpz_mpoly_t B,
const fmpz_mpoly_ctx_t ctx)
{
int success = 0;
slong i, j, degb_prod;
slong * Aexps, * Bexps, * deg_bounds;
slong nvars = ctx->minfo->nvars;
slong * perm = dctx->perm;
TMP_INIT;
TMP_START;
Aexps = (slong *) TMP_ALLOC(nvars*sizeof(slong));
Bexps = (slong *) TMP_ALLOC(nvars*sizeof(slong));
if (A->bits > FLINT_BITS || B->bits > FLINT_BITS)
goto cleanup;
fmpz_mpoly_degrees_si(Aexps, A, ctx);
fmpz_mpoly_degrees_si(Bexps, B, ctx);
deg_bounds = (slong *) TMP_ALLOC(nvars*sizeof(slong));
for (i = 0; i < nvars; i++)
{
dctx->perm[i] = i;
}
degb_prod = 1;
for (i = 0; i < nvars; i++)
{
ulong hi;
deg_bounds[i] = FLINT_MAX(Aexps[i] + 1, Bexps[i] + 1);
umul_ppmm(hi, degb_prod, degb_prod, deg_bounds[i]);
if (hi != WORD(0) || degb_prod < 0)
goto cleanup;
}
success = 1;
for (i = 1; i < nvars; i++)
{
for (j = i; (j > 0) && (deg_bounds[j-1] < deg_bounds[j-0]); j--)
{
slong t1, t2;
t1 = deg_bounds[j-1];
t2 = deg_bounds[j-0];
deg_bounds[j-0] = t1;
deg_bounds[j-1] = t2;
t1 = perm[j-1];
t2 = perm[j-0];
perm[j-0] = t1;
perm[j-1] = t2;
}
}
cleanup:
TMP_END;
return success;
}
void fmpz_mpolyd_ctx_clear(fmpz_mpolyd_ctx_t dctx)
{
flint_free(dctx->perm);
}
void fmpz_mpolyd_zero(fmpz_mpolyd_t poly)
{
slong i;
for (i = 0; i < poly->nvars; i++)
{
poly->deg_bounds[i] = WORD(1);
}
poly->coeffs[0] = UWORD(0);
}
void fmpz_mpolyd_set_fmpz(fmpz_mpolyd_t poly, fmpz_t num)
{
slong i;
for (i = 0; i < poly->nvars; i++)
{
poly->deg_bounds[i] = WORD(1);
}
fmpz_set(poly->coeffs + 0, num);
}
void fmpz_mpolyd_set_nvars(fmpz_mpolyd_t poly, slong nvars)
{
poly->nvars = nvars;
if (poly->degb_alloc < nvars) {
poly->deg_bounds = (slong *) flint_realloc(poly->deg_bounds, nvars*sizeof(slong));
poly->degb_alloc = nvars;
}
}
void fmpz_mpoly_convert_to_fmpz_mpolyd(
fmpz_mpolyd_t poly1, const fmpz_mpolyd_ctx_t dctx,
const fmpz_mpoly_t poly2, const fmpz_mpoly_ctx_t ctx)
{
slong degb_prod;
slong i, j, N;
slong * exps;
const slong * perm = dctx->perm;
slong nvars = ctx->minfo->nvars;
TMP_INIT;
fmpz_mpolyd_set_nvars(poly1, ctx->minfo->nvars);
FLINT_ASSERT(poly2->bits <= FLINT_BITS);
if (poly2->length == 0)
{
fmpz_mpolyd_zero(poly1);
return;
}
TMP_START;
exps = (slong *) TMP_ALLOC(ctx->minfo->nvars*sizeof(slong));
fmpz_mpoly_degrees_si(exps, poly2, ctx);
degb_prod = WORD(1);
for (i = 0; i < nvars; i++)
{
poly1->deg_bounds[i] = exps[perm[i]] + 1;
degb_prod *= poly1->deg_bounds[i];
}
fmpz_mpolyd_fit_length(poly1, degb_prod);
for (i = 0; i < degb_prod; i++)
{
fmpz_zero(poly1->coeffs + i);
}
N = mpoly_words_per_exp(poly2->bits, ctx->minfo);
for (i = 0; i < poly2->length; i++)
{
slong off = 0;
mpoly_get_monomial_ui((ulong *)exps, poly2->exps + N*i, poly2->bits, ctx->minfo);
for (j = 0; j < nvars; j++)
{
off = exps[perm[j]] + poly1->deg_bounds[j]*off;
}
fmpz_set(poly1->coeffs + off, poly2->coeffs + i);
}
TMP_END;
}
void fmpz_mpoly_convert_from_fmpz_mpolyd(
fmpz_mpoly_t A, const fmpz_mpoly_ctx_t ctx,
const fmpz_mpolyd_t B, const fmpz_mpolyd_ctx_t dctx)
{
slong i, j;
slong degb_prod;
slong * perm = dctx->perm;
ulong * exps;
TMP_INIT;
FLINT_ASSERT(ctx->minfo->nvars == B->nvars);
degb_prod = WORD(1);
for (j = 0; j < B->nvars; j++) {
degb_prod *= B->deg_bounds[j];
}
TMP_START;
exps = (ulong *) TMP_ALLOC(B->nvars*sizeof(ulong));
fmpz_mpoly_zero(A, ctx);
for (i = 0; i < degb_prod; i++) {
ulong k = i;
if (fmpz_is_zero(B->coeffs + i))
continue;
for (j = B->nvars - 1; j >= 0; j--)
{
ulong m = B->deg_bounds[j];
ulong e = k % m;
k = k / m;
exps[perm[j]] = e;
}
FLINT_ASSERT(k == 0);
fmpz_mpoly_set_coeff_fmpz_ui(A, B->coeffs + i, exps, ctx);
}
TMP_END;
}
void
void
void