flint-sys 0.9.0

Bindings to the FLINT C library
Documentation
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
    Copyright (C) 2021 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/>.
*/

#include "fmpz_mod.h"
#include "mpoly.h"
#include "fmpz_mod_mpoly.h"

static int _fmpz_mod_mpoly_divrem_ideal_monagan_pearce(
    fmpz_mod_mpoly_struct ** Q,
    fmpz_mod_mpoly_t R,
    const fmpz * Acoeffs, const ulong * Aexps, slong Alen,
    fmpz_mod_mpoly_struct * const * Bs, ulong * const * Bexps, slong Blen,
    slong N,
    flint_bitcnt_t bits,
    const fmpz_mod_mpoly_ctx_t ctx,
    const ulong * cmpmask)
{
    int overflows, divides, success;
    slong i, j, p, w;
    slong next_loc;
    slong * store, * store_base;
    slong len3;
    slong heap_len = 2; /* heap zero index unused */
    mpoly_heap_s * heap;
    mpoly_nheap_t ** chains, * chains_ptr;
    slong ** hinds, * hinds_ptr;
    mpoly_nheap_t * x;
    fmpz * r_coeff = R->coeffs;
    ulong * r_exp = R->exps;
    slong r_len;
    ulong * exp, * exps, * texp;
    ulong ** exp_list;
    slong exp_next;
    ulong mask;
    slong * q_len, * s;
    fmpz * lc_inv;
    fmpz_t acc;
    TMP_INIT;

    TMP_START;

    fmpz_init(acc);

    chains = TMP_ARRAY_ALLOC(Blen, mpoly_nheap_t *);
    hinds = TMP_ARRAY_ALLOC(Blen, slong *);

    /* chains[w], hinds[w] will be arrays of length Bs[w]->length; combine the allocations*/
    len3 = 0;
    for (w = 0; w < Blen; w++)
        len3 += Bs[w]->length;
    chains_ptr = TMP_ARRAY_ALLOC(len3, mpoly_nheap_t);
    hinds_ptr = TMP_ARRAY_ALLOC(len3, slong);

    len3 = 0;
    for (w = 0; w < Blen; w++)
    {
        chains[w] = chains_ptr + len3;
        hinds[w] = hinds_ptr + len3;
        len3 += Bs[w]->length;
        for (i = 0; i < Bs[w]->length; i++)
            hinds[w][i] = 1;
    }

    next_loc = len3 + 4;   /* something bigger than heap can ever be */
    heap = (mpoly_heap_s *) TMP_ALLOC((len3 + 1)*sizeof(mpoly_heap_s));
    store = store_base = (slong *) TMP_ALLOC(3*len3*sizeof(slong *));

    exps = (ulong *) TMP_ALLOC(len3*N*sizeof(ulong));
    exp_list = (ulong **) TMP_ALLOC(len3*sizeof(ulong *));
    texp = (ulong *) TMP_ALLOC(N*sizeof(ulong));
    exp = (ulong *) TMP_ALLOC(N*sizeof(ulong));
    q_len = (slong *) TMP_ALLOC(Blen*sizeof(slong));
    s = (slong *) TMP_ALLOC(Blen*sizeof(slong));

    exp_next = 0;
    for (i = 0; i < len3; i++)
        exp_list[i] = exps + i*N;

    mask = bits <= FLINT_BITS ? mpoly_overflow_mask_sp(bits) : 0;

    for (w = 0; w < Blen; w++)
    {
        q_len[w] = WORD(0);
        s[w] = Bs[w]->length;
    }
    r_len = WORD(0);

    x = chains[0] + 0;
    x->i = -WORD(1);
    x->j = 0;
    x->p = -WORD(1);
    x->next = NULL;
    heap[1].next = x;
    heap[1].exp = exp_list[exp_next++];
    mpoly_monomial_set(heap[1].exp, Aexps, N);

    /* precompute leading coeff info */
    lc_inv = TMP_ARRAY_ALLOC(Blen, fmpz);
    for (w = 0; w < Blen; w++)
    {
        fmpz_init(lc_inv + w);
        fmpz_mod_inv(lc_inv + w, Bs[w]->coeffs + 0, ctx->ffinfo);
    }

    while (heap_len > 1)
    {
        mpoly_monomial_set(exp, heap[1].exp, N);

        overflows =  (bits <= FLINT_BITS) ?
                                    mpoly_monomial_overflows(exp, N, mask) :
                                    mpoly_monomial_overflows_mp(exp, N, bits);
        if (overflows)
            goto exp_overflow;

        fmpz_zero(acc);
        do {
            exp_list[--exp_next] = heap[1].exp;
            x = _mpoly_heap_pop(heap, &heap_len, N, cmpmask);
            do {
                *store++ = x->i;
                *store++ = x->j;
                *store++ = x->p;

                if (x->i == -UWORD(1))
                {
                    fmpz_add(acc, acc, Acoeffs + x->j);
                }
                else
                {
                    hinds[x->p][x->i] |= WORD(1);
                    fmpz_submul(acc, Bs[x->p]->coeffs + x->i, Q[x->p]->coeffs + x->j);
                }

            } while ((x = x->next) != NULL);
        } while (heap_len > 1 && mpoly_monomial_equal(heap[1].exp, exp, N));

        fmpz_mod_set_fmpz(acc, acc, ctx->ffinfo);

        while (store > store_base)
        {
            p = *--store;
            j = *--store;
            i = *--store;
            if (i == -WORD(1))
            {
                if (j + 1 < Alen)
                {
                    x = chains[0] + 0;
                    x->i = -WORD(1);
                    x->j = j + 1;
                    x->p = p;
                    x->next = NULL;
                    mpoly_monomial_set(exp_list[exp_next], Aexps + x->j*N, N);
                    exp_next += _mpoly_heap_insert(heap, exp_list[exp_next], x,
                                             &next_loc, &heap_len, N, cmpmask);
                }
            }
            else
            {
                /* should we go right? */
                if ((i + 1 < Bs[p]->length) &&
                    (hinds[p][i + 1] == 2*j + 1))
                {
                    x = chains[p] + i + 1;
                    x->i = i + 1;
                    x->j = j;
                    x->p = p;
                    x->next = NULL;
                    hinds[p][x->i] = 2*(x->j + 1) + 0;
                    mpoly_monomial_add_mp(exp_list[exp_next],
                              Bexps[x->p] + N*x->i, Q[x->p]->exps + N*x->j, N);
                    exp_next += _mpoly_heap_insert(heap, exp_list[exp_next], x,
                                             &next_loc, &heap_len, N, cmpmask);
                }

                /* should we go up? */
                if (j + 1 == q_len[p])
                {
                    s[p]++;
                }
                else if (((hinds[p][i] & 1) == 1) &&
                         ((i == 1) || (hinds[p][i - 1] >= 2*(j + 2) + 1)))
                {
                    x = chains[p] + i;
                    x->i = i;
                    x->j = j + 1;
                    x->p = p;
                    x->next = NULL;
                    hinds[p][x->i] = 2*(x->j + 1) + 0;
                    mpoly_monomial_add_mp(exp_list[exp_next],
                              Bexps[x->p] + N*x->i, Q[x->p]->exps + N*x->j, N);
                    exp_next += _mpoly_heap_insert(heap, exp_list[exp_next], x,
                                             &next_loc, &heap_len, N, cmpmask);
                }
            }
        }

        if (fmpz_is_zero(acc))
            continue;

        for (w = 0; w < Blen; w++)
        {
            divides = (bits <= FLINT_BITS) ?
                 mpoly_monomial_divides(texp, exp, Bexps[w] + N*0, N, mask) :
                 mpoly_monomial_divides_mp(texp, exp, Bexps[w] + N*0, N, bits);

            if (!divides)
                continue;

            fmpz_mod_mpoly_fit_length(Q[w], q_len[w] + 1, ctx);
            mpoly_monomial_set(Q[w]->exps + N*q_len[w], texp, N);
            fmpz_mod_mul(Q[w]->coeffs + q_len[w], acc, lc_inv + w, ctx->ffinfo);

            if (s[w] > 1)
            {
                i = 1;
                x = chains[w] + i;
                x->i = i;
                x->j = q_len[w];
                x->p = w;
                x->next = NULL;
                hinds[w][x->i] = 2*(x->j + 1) + 0;
                mpoly_monomial_add_mp(exp_list[exp_next], Bexps[w] + N*x->i,
                                                       Q[w]->exps + N*x->j, N);
                exp_next += _mpoly_heap_insert(heap, exp_list[exp_next], x,
                                             &next_loc, &heap_len, N, cmpmask);
            }
            s[w] = 1;
            q_len[w]++;
            goto break_continue; /* continue in heap loop */
        }

        /* if get here, no leading terms divided */
        _fmpz_mod_mpoly_fit_length(&r_coeff, &R->coeffs_alloc,
                                   &r_exp, &R->exps_alloc, N, r_len + 1);
        fmpz_set(r_coeff + r_len, acc);
        mpoly_monomial_set(r_exp + N*r_len, exp, N);
        r_len++;

break_continue:;
    }

    R->coeffs = r_coeff;
    R->exps = r_exp;
    R->length = r_len;

    for (i = 0; i < Blen; i++)
        Q[i]->length = q_len[i];

    success = 1;

cleanup:

    for (w = 0; w < Blen; w++)
        fmpz_clear(lc_inv + w);

    fmpz_clear(acc);

    TMP_END;

    return success;

exp_overflow:

    R->coeffs = r_coeff;
    R->exps = r_exp;
    R->length = 0;

    for (i = 0; i < Blen; i++)
        Q[i]->length = 0;

    success = 0;
    goto cleanup;
}

/* Assumes divisor polys don't alias any output polys */
void fmpz_mod_mpoly_divrem_ideal_monagan_pearce(
    fmpz_mod_mpoly_struct ** Q,
    fmpz_mod_mpoly_t R,
    const fmpz_mod_mpoly_t A,
    fmpz_mod_mpoly_struct * const * B,
    slong Blen,
    const fmpz_mod_mpoly_ctx_t ctx)
{
    slong i, N;
    flint_bitcnt_t QRbits;
    slong len3 = 0;
    ulong * cmpmask;
    ulong * Aexps;
    ulong ** Bexps;
    int freeAexps, * freeBexps;
    fmpz_mod_mpoly_t TR;
    fmpz_mod_mpoly_struct * r;
    TMP_INIT;

    for (i = 0; i < Blen; i++)
    {
        len3 = FLINT_MAX(len3, B[i]->length);
        if (fmpz_mod_mpoly_is_zero(B[i], ctx))
            flint_throw(FLINT_DIVZERO,
                 "fmpz_mod_mpoly_divrem_ideal_monagan_pearce: divide by zero");
    }

    /* dividend is zero, write out quotients and remainder */
    if (fmpz_mod_mpoly_is_zero(A, ctx))
    {
        fmpz_mod_mpoly_zero(R, ctx);
        for (i = 0; i < Blen; i++)
            fmpz_mod_mpoly_zero(Q[i], ctx);
        return;
    }

    TMP_START;

    fmpz_mod_mpoly_init(TR, ctx);

    freeBexps = TMP_ARRAY_ALLOC(Blen, int);
    Bexps = TMP_ARRAY_ALLOC(Blen, ulong *);

    /* compute maximum degrees that can occur in any input or output polys */
    QRbits = A->bits;
    for (i = 0; i < Blen; i++)
        QRbits = FLINT_MAX(QRbits, B[i]->bits);
    QRbits = mpoly_fix_bits(QRbits, ctx->minfo);

    N = mpoly_words_per_exp(QRbits, ctx->minfo);
    cmpmask = FLINT_ARRAY_ALLOC(N, ulong);
    mpoly_get_cmpmask(cmpmask, N, QRbits, ctx->minfo);

    /* ensure input exponents packed to same size as output exponents */
    Aexps = A->exps;
    freeAexps = 0;
    if (QRbits > A->bits)
    {
        freeAexps = 1;
        Aexps = FLINT_ARRAY_ALLOC(N*A->length, ulong);
        mpoly_repack_monomials(Aexps, QRbits, A->exps, A->bits,
                                                        A->length, ctx->minfo);
    }

    for (i = 0; i < Blen; i++)
    {
        Bexps[i] = B[i]->exps;
        freeBexps[i] = 0;
        if (QRbits > B[i]->bits)
        {
            freeBexps[i] = 1;
            Bexps[i] = FLINT_ARRAY_ALLOC(N*B[i]->length, ulong);
            mpoly_repack_monomials(Bexps[i], QRbits, B[i]->exps, B[i]->bits,
                                                     B[i]->length, ctx->minfo);
        }
    }

    /* check leading mon. of at least one divisor is at most that of dividend */
    for (i = 0; i < Blen; i++)
    {
        if (!mpoly_monomial_lt(Aexps + N*0, Bexps[i] + N*0, N, cmpmask))
            break;
    }

    if (i == Blen)
    {
        fmpz_mod_mpoly_set(R, A, ctx);
        for (i = 0; i < Blen; i++)
            fmpz_mod_mpoly_zero(Q[i], ctx);
        goto cleanup;
    }

    /* take care of aliasing */
    if (R == A)
        r = TR;
    else
        r = R;

    /* do division with remainder */
    while (1)
    {
        fmpz_mod_mpoly_fit_length_reset_bits(r, len3, QRbits, ctx);
        for (i = 0; i < Blen; i++)
            fmpz_mod_mpoly_fit_length_reset_bits(Q[i], 1, QRbits, ctx);

        if (_fmpz_mod_mpoly_divrem_ideal_monagan_pearce(Q, r, A->coeffs, Aexps,
                           A->length, B, Bexps, Blen, N, QRbits, ctx, cmpmask))
        {
            break;
        }

        QRbits = mpoly_fix_bits(QRbits + 1, ctx->minfo);
        N = mpoly_words_per_exp(QRbits, ctx->minfo);
        cmpmask = FLINT_ARRAY_REALLOC(cmpmask, N, ulong);
        mpoly_get_cmpmask(cmpmask, N, QRbits, ctx->minfo);

        if (freeAexps)
            flint_free(Aexps);
        Aexps = FLINT_ARRAY_ALLOC(N*A->length, ulong);
        mpoly_repack_monomials(Aexps, QRbits, A->exps, A->bits,
                                                        A->length, ctx->minfo);
        freeAexps = 1;

        for (i = 0; i < Blen; i++)
        {
            if (freeBexps[i])
                flint_free(Bexps[i]);

            Bexps[i] = FLINT_ARRAY_ALLOC(N*B[i]->length, ulong);
            mpoly_repack_monomials(Bexps[i], QRbits, B[i]->exps, B[i]->bits,
                                                     B[i]->length, ctx->minfo);
            freeBexps[i] = 1;
        }
    }

    /* take care of aliasing */
    if (R == A)
        fmpz_mod_mpoly_swap(R, TR, ctx);

cleanup:

    fmpz_mod_mpoly_clear(TR, ctx);

    if (freeAexps)
        flint_free(Aexps);

    for (i = 0; i < Blen; i++)
    {
        if (freeBexps[i])
            flint_free(Bexps[i]);
    }

    flint_free(cmpmask);

    TMP_END;
}