File: pseudo_divrem_divconquer.c

package info (click to toggle)
flint 2.5.2-19
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 30,308 kB
  • sloc: ansic: 289,367; cpp: 11,210; python: 1,280; sh: 649; makefile: 283
file content (363 lines) | stat: -rw-r--r-- 11,094 bytes parent folder | download | duplicates (4)
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
/*=============================================================================

    This file is part of FLINT.

    FLINT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    FLINT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with FLINT; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

=============================================================================*/
/******************************************************************************

    Copyright (C) 2008, 2009 William Hart
    Copyright (C) 2010 Sebastian Pancratz

******************************************************************************/

#include <stdlib.h>
#include "fmpz_poly.h"

static void 
__fmpz_poly_pseudo_divrem_divconquer(fmpz * Q, fmpz * R, 
                  ulong * d, const fmpz * A, slong lenA, 
                          const fmpz * B, slong lenB, const fmpz_preinvn_t inv)
{
    if (lenB <= 16 || (lenA > 2 * lenB - 1 && lenA < 128))
    {
        _fmpz_poly_pseudo_divrem_basecase(Q, R, d, A, lenA, B, lenB, inv);
    }
    else
    {
        const slong n2 = lenB / 2;
        const slong n1 = lenB - n2;

        const fmpz * d1 = B + n2;
        const fmpz * d2 = B;
        const fmpz * d3 = B + n1;
        const fmpz * d4 = B;
    
        if (lenA <= lenB + n2 - 1)
        {
            fmpz *p1, *r1, *d2q1;
            fmpz *f;

            /*
               Shift A right by n1, zero the bottom n2 - 1 coeffs; call this p1
             */

            p1 = (fmpz *) flint_malloc((lenA - n1) * sizeof(fmpz));
            {
                slong i;
                flint_mpn_zero((mp_ptr) p1, n2 - 1);
                for (i = n2 - 1; i < lenA - n1; i++)
                    p1[i] = (A + n1)[i];
            }

            /*
               Compute p1 div d3, at most a 2 n2 - 1 by n2 division, leaving 
               lenA - lenB + 1 <= n2 terms in the quotient
             */

            r1 = R + n1;
            _fmpz_poly_pseudo_divrem_divconquer(Q, r1, d, p1, lenA - n1, d3, n2, inv);

            flint_free(p1);

            /*
               Push the relevant {n2 - 1} terms of the remainder to the 
               top of {R, lenA}
             */

            {
                slong i;
                for (i = n2 - 2; i >= 0; i--)
                    fmpz_swap(R + lenA - (n2 - 1) + i, r1 + i);
                r1 = R + lenA - (n2 - 1);
            }

            /*
               Compute d2q1 = Q d4 of length lenA - n2, which is 
               at most n1 + n2 - 1 terms
             */

            d2q1 = R;
            _fmpz_poly_mul(d2q1, d4, n1, Q, lenA - lenB + 1);

            /*
               Compute R = L^d R', where R' is the terms of A we have not dealt, 
               of which there are at most n1 + n2 - 1; that is, 

               Set R to {A, n1 + n2 - 1} * f + r1 x^n1 - d2q1
             */

            _fmpz_vec_neg(R, R, lenA - n2);
            _fmpz_vec_add(R + n1, R + n1, R + lenA - n2 + 1, lenA - lenB);
            _fmpz_vec_swap(R + lenA - n2, R + 2 * lenA - lenB + 1 - n2, n2 - (lenA - lenB + 1));

            f = R + lenB - 1;
            fmpz_pow_ui(f, B + (lenB - 1), *d);
            _fmpz_vec_scalar_addmul_fmpz(R, A, n1 + n2 - 1, f);
        }
        else if (lenA > 2 * lenB - 1)
        {
            /*
               XXX:  In this case, we expect A to be modifiable
             */

            ulong s1, s2;
            const slong shift = lenA - 2 * lenB + 1;

            fmpz * q1 = Q + shift;
            fmpz * q2 = Q;
            fmpz * r1 = R;

            fmpz *p1, *t;
            fmpz_t f;

            fmpz_init(f);

            /*
               Shift A right until it is of length 2 lenB - 1, call this p1; 
               zero the bottom lenB - 1 coeffs
             */

            p1 = (fmpz *) flint_malloc((2 * lenB - 1) * sizeof(fmpz));
            {
                slong i;
                flint_mpn_zero((mp_ptr) p1, lenB - 1);
                for (i = lenB - 1; i < 2*lenB - 1; i++)
                    p1[i] = (A + shift)[i];
            }

            /*
               Set q1 to p1 div B, a 2 lenB - 1 by lenB division, so q1 ends up 
               being at most length lenB; r1 is of length at most lenB - 1
             */

            _fmpz_poly_pseudo_divrem_divconquer(q1, r1, &s1, p1, 2 * lenB - 1, B, lenB, inv);

            flint_free(p1);

            /*
               Compute t = L^s1 a2 + r1 x^shift, of length at most lenA - lenB 
               since r1 is of length at most lenB - 1.  Here a2 is what remains
               of A after the first lenR coefficients are removed
             */

            t = (fmpz *) A;

            fmpz_pow_ui(f, B + (lenB - 1), s1);

            _fmpz_vec_scalar_mul_fmpz(t, A, lenA - lenB, f);
            _fmpz_vec_add(t + shift, t + shift, r1, lenB - 1);

            /*
               Compute q2 = t div B; it is a smaller division than the original 
               since len(t) <= lenA - lenB, and r2 has length at most lenB - 1
             */

            _fmpz_poly_pseudo_divrem_divconquer(q2, R, &s2, t, lenA - lenB, B, lenB, inv);

            /*
               Write out Q = L^s2 q1 x^shift + q2, of length at most 
               lenB + shift.  Note q2 has length at most shift since it is at 
               most an lenA - lenB by lenB division; q1 cannot have length zero
               since we are doing pseudo division
             */

            fmpz_pow_ui(f, B + (lenB - 1), s2);

            _fmpz_vec_scalar_mul_fmpz(q1, q1, lenB, f);

            *d = s1 + s2;

            fmpz_clear(f);
        }
        else  /* n1 + 2 n2 - 1 < lenA <= 2 lenB - 1 */
        {
            fmpz * q1   = Q + n2;
            fmpz * q2   = Q;
            fmpz * r1   = R;
            fmpz * d2q1 = R + (n1 - 1);
            fmpz *p1, *t;
            fmpz_t f;
            ulong s1, s2;

            fmpz_init(f);

            /*
               Set p1 to the top lenA - 2 n2 coeffs of A, clearing the bottom 
               n1 - 1 coeffs
             */

            p1 = (fmpz *) flint_malloc((lenA - 2 * n2) * sizeof(fmpz));
            {
                slong i;
                flint_mpn_zero((mp_ptr) p1, n1 - 1);
                for (i = n1 - 1; i < lenA - 2 * n2; i++)
                    p1[i] = (A + 2 * n2)[i];
            }

            /*
               Set q1 to p1 div d1, at most a 2 n1 - 1 by n1 division, so q1 ends 
               up being of length at most n1; r1 is of length n1 - 1
             */

            _fmpz_poly_pseudo_divrem_divconquer(q1, r1, &s1, p1, lenA - 2 * n2, d1, n1, inv);

            flint_free(p1);

            /*
               Compute d2q1 = d2q1, of length lenA - lenB

               Note lenA - lenB <= lenB - 1 <= 2 n2 and lenA - (n1 - 1) > 2 n2, 
               so we can store d2q1 in the top 2 n2 coeffs of R
             */

            if (n2 >= lenA - n1 - 2 * n2 + 1)
                _fmpz_poly_mul(d2q1, d2, n2, q1, lenA - (n1 + 2 * n2 - 1));
            else
                _fmpz_poly_mul(d2q1, q1, lenA - (n1 + 2 * n2 - 1), d2, n2);

            /*
               Compute
                   t = L^s1 * (a2 x^{n1 + n2 - 1} + a3) 
                       + r1 x^{2 n2} - d2q1 x^n2
               of length at most lenB + n2 - 1, since r1 is of length at most 
               n1 - 1 and d2q1 is of length at most n1 + n2 - 1
             */

            t = _fmpz_vec_init(n1 + 2 * n2 - 1);

            fmpz_pow_ui(f, B + (lenB - 1), s1);

            _fmpz_vec_scalar_mul_fmpz(t, A, n1 + 2 * n2 - 1, f);
            _fmpz_vec_add(t + 2 * n2, t + 2 * n2, r1, n1 - 1);
            _fmpz_vec_sub(t + n2, t + n2, d2q1, lenA - lenB);

            /*
               Compute q2 = t div B and set R to the remainder, at most a 
               lenB + n2 - 1 by lenB division, so q2 is of length at most n2
             */

            _fmpz_poly_pseudo_divrem_divconquer(q2, R, &s2, t, lenB + n2 - 1, B, lenB, inv);

            _fmpz_vec_clear(t, n1 + 2 * n2 - 1);

            /*
               Write Q = L^s2 q1 x^n2 + q2; note len(q1) is non-zero since 
               we are performing pseudo division
             */

            fmpz_pow_ui(f, B + (lenB - 1), s2);

            _fmpz_vec_scalar_mul_fmpz(q1, q1, lenA - lenB + 1 - n2, f);

            *d = s1 + s2;

            fmpz_clear(f);
        }
    }
}

void 
_fmpz_poly_pseudo_divrem_divconquer(fmpz * Q, fmpz * R, 
                     ulong * d, const fmpz * A, slong lenA, 
                          const fmpz * B, slong lenB, const fmpz_preinvn_t inv)
{
    if (lenA <= 2 * lenB - 1)
    {
        __fmpz_poly_pseudo_divrem_divconquer(Q, R, d, A, lenA, B, lenB, inv);
    }
    else  /* lenA > 2 * lenB - 1 */
    {
        fmpz *S = _fmpz_vec_init(lenA);

        _fmpz_vec_set(S, A, lenA);

        __fmpz_poly_pseudo_divrem_divconquer(Q, R, d, S, lenA, B, lenB, inv);

        _fmpz_vec_clear(S, lenA);
    }
}

void
fmpz_poly_pseudo_divrem_divconquer(fmpz_poly_t Q, fmpz_poly_t R,
                                   ulong * d, const fmpz_poly_t A,
                                   const fmpz_poly_t B)
{
    slong lenq, lenr;
    fmpz *q, *r;

    if (B->length == 0)
    {
        flint_printf("Exception (fmpz_poly_pseudo_divrem_divconquer). Division by zero.\n");
        abort();
    }
    if (Q == R)
    {
        flint_printf("Exception (fmpz_poly_pseudo_divrem_divconquer). \n"
               "Output arguments Q and R may not be aliased.\n");
        abort();
    }
    if (A->length < B->length)
    {
        fmpz_poly_zero(Q);
        fmpz_poly_set(R, A);
        *d = 0;
        return;
    }

    lenq = A->length - B->length + 1;
    lenr = A->length;
    if (Q == A || Q == B)
        q = _fmpz_vec_init(lenq);
    else
    {
        fmpz_poly_fit_length(Q, lenq);
        q = Q->coeffs;
    }
    if (R == A || R == B)
        r = _fmpz_vec_init(lenr);
    else
    {
        fmpz_poly_fit_length(R, lenr);
        r = R->coeffs;
    }
    
    _fmpz_poly_pseudo_divrem_divconquer(q, r, d, A->coeffs, A->length, 
                                                 B->coeffs, B->length, NULL);

    lenr = B->length - 1;
    FMPZ_VEC_NORM(r, lenr);

    if (Q == A || Q == B)
    {
        _fmpz_vec_clear(Q->coeffs, Q->alloc);
        Q->coeffs = q;
        Q->alloc = lenq;
        Q->length = lenq;
    }
    else
        _fmpz_poly_set_length(Q, lenq);
    if (R == A || R == B)
    {
        _fmpz_vec_clear(R->coeffs, R->alloc);
        R->coeffs = r;
        R->alloc = A->length;
        R->length = lenr;
    }
    else
        _fmpz_poly_set_length(R, lenr);
}