Line data Source code
1 : /* Copyright (C) 2019 The PARI group.
2 :
3 : This file is part of the PARI/GP package.
4 :
5 : PARI/GP is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 2 of the License, or (at your option) any later
8 : version. It is distributed in the hope that it will be useful, but WITHOUT
9 : ANY WARRANTY WHATSOEVER.
10 :
11 : Check the License for details. You should have received a copy of it, along
12 : with the package; see the file 'COPYING'. If not, write to the Free Software
13 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14 :
15 : #include "pari.h"
16 : #include "paripriv.h"
17 :
18 : /***********************************************************************/
19 : /** **/
20 : /** FlxX **/
21 : /** **/
22 : /***********************************************************************/
23 :
24 : /* FlxX are t_POL with Flx coefficients.
25 : * Normally the variable ordering should be respected.*/
26 :
27 : /*Similar to normalizepol, in place*/
28 : /*FlxX_renormalize=zxX_renormalize */
29 : GEN
30 15028578 : FlxX_renormalize(GEN /*in place*/ x, long lx)
31 : {
32 : long i;
33 19836203 : for (i = lx-1; i>1; i--)
34 19058302 : if (lgpol(gel(x,i))) break;
35 15028708 : stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
36 15029244 : setlg(x, i+1); setsigne(x, i!=1); return x;
37 : }
38 :
39 : GEN
40 760586 : pol1_FlxX(long v, long sv)
41 : {
42 760586 : GEN z = cgetg(3, t_POL);
43 760585 : z[1] = evalsigne(1) | evalvarn(v);
44 760585 : gel(z,2) = pol1_Flx(sv); return z;
45 : }
46 :
47 : GEN
48 690442 : polx_FlxX(long v, long sv)
49 : {
50 690442 : GEN z = cgetg(4, t_POL);
51 690442 : z[1] = evalsigne(1) | evalvarn(v);
52 690442 : gel(z,2) = pol0_Flx(sv);
53 690442 : gel(z,3) = pol1_Flx(sv); return z;
54 : }
55 :
56 : long
57 2892639 : FlxY_degreex(GEN b)
58 : {
59 2892639 : long deg = 0, i;
60 2892639 : if (!signe(b)) return -1;
61 10420000 : for (i = 2; i < lg(b); ++i)
62 7527192 : deg = maxss(deg, degpol(gel(b, i)));
63 2892808 : return deg;
64 : }
65 :
66 : /*Lift coefficient of B to constant Flx, to give a FlxY*/
67 : GEN
68 2533 : Fly_to_FlxY(GEN B, long sv)
69 : {
70 2533 : long lb=lg(B);
71 : long i;
72 2533 : GEN b=cgetg(lb,t_POL);
73 2533 : b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
74 66003 : for (i=2; i<lb; i++)
75 63483 : gel(b,i) = Fl_to_Flx(B[i], sv);
76 2520 : return FlxX_renormalize(b, lb);
77 : }
78 :
79 : GEN
80 2688342 : zxX_to_FlxX(GEN B, ulong p)
81 : {
82 2688342 : long i, lb = lg(B);
83 2688342 : GEN b = cgetg(lb,t_POL);
84 8766536 : for (i=2; i<lb; i++)
85 6083445 : gel(b,i) = zx_to_Flx(gel(B,i), p);
86 2683091 : b[1] = B[1]; return FlxX_renormalize(b, lb);
87 : }
88 :
89 : GEN
90 2768099 : FlxX_to_ZXX(GEN B)
91 : {
92 2768099 : long i, lb = lg(B);
93 2768099 : GEN b = cgetg(lb,t_POL);
94 8882857 : for (i=2; i<lb; i++)
95 : {
96 6115713 : GEN c = gel(B,i);
97 6115713 : switch(lgpol(c))
98 : {
99 268083 : case 0: c = gen_0; break;
100 796223 : case 1: c = utoi(c[2]); break;
101 5051475 : default: c = Flx_to_ZX(c); break;
102 : }
103 6114544 : gel(b,i) = c;
104 : }
105 2767144 : b[1] = B[1]; return b;
106 : }
107 :
108 : /* Note: v is used _only_ for the t_INT. It must match
109 : * the variable of any t_POL coefficients. */
110 : GEN
111 2809014 : ZXX_to_FlxX(GEN B, ulong p, long v)
112 : {
113 2809014 : long lb=lg(B);
114 : long i;
115 2809014 : GEN b=cgetg(lb,t_POL);
116 2809141 : b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
117 13750855 : for (i=2; i<lb; i++)
118 10943061 : switch (typ(gel(B,i)))
119 : {
120 2136009 : case t_INT:
121 2136009 : gel(b,i) = Z_to_Flx(gel(B,i), p, evalvarn(v));
122 2135927 : break;
123 8808671 : case t_POL:
124 8808671 : gel(b,i) = ZX_to_Flx(gel(B,i), p);
125 8807406 : break;
126 : }
127 2807794 : return FlxX_renormalize(b, lb);
128 : }
129 :
130 : GEN
131 0 : ZXXV_to_FlxXV(GEN x, ulong p, long v)
132 0 : { pari_APPLY_type(t_VEC, ZXX_to_FlxX(gel(x,i), p, v)) }
133 :
134 : GEN
135 512 : ZXXT_to_FlxXT(GEN x, ulong p, long v)
136 : {
137 512 : if (typ(x) == t_POL)
138 491 : return ZXX_to_FlxX(x, p, v);
139 : else
140 63 : pari_APPLY_type(t_VEC, ZXXT_to_FlxXT(gel(x,i), p, v))
141 : }
142 :
143 : GEN
144 323888 : FlxX_to_FlxC(GEN x, long N, long sv)
145 : {
146 : long i, l;
147 : GEN z;
148 323888 : l = lg(x)-1; x++;
149 323888 : if (l > N+1) l = N+1; /* truncate higher degree terms */
150 323888 : z = cgetg(N+1,t_COL);
151 2436104 : for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
152 3478001 : for ( ; i<=N; i++) gel(z,i) = pol0_Flx(sv);
153 323888 : return z;
154 : }
155 :
156 : /* matrix whose entries are given by the coeffs of the polynomial v in
157 : * two variables (considered as degree n polynomials) */
158 : GEN
159 54655 : FlxX_to_Flm(GEN v, long n)
160 : {
161 54655 : long j, N = lg(v)-1;
162 54655 : GEN y = cgetg(N, t_MAT);
163 54655 : v++;
164 188322 : for (j=1; j<N; j++) gel(y,j) = Flx_to_Flv(gel(v,j), n);
165 54653 : return y;
166 : }
167 :
168 : GEN
169 363373 : FlxX_to_Flx(GEN f)
170 : {
171 363373 : long i, l = lg(f);
172 363373 : GEN V = cgetg(l, t_VECSMALL);
173 363374 : V[1] = ((ulong)f[1])&VARNBITS;
174 3495020 : for(i=2; i<l; i++)
175 3131646 : V[i] = lgpol(gel(f,i)) ? mael(f,i,2): 0L;
176 363374 : return V;
177 : }
178 :
179 : GEN
180 219820 : Flm_to_FlxX(GEN x, long v,long w)
181 : {
182 219820 : long j, lx = lg(x);
183 219820 : GEN y = cgetg(lx+1, t_POL);
184 219823 : y[1]=evalsigne(1) | v;
185 219823 : y++;
186 809205 : for (j=1; j<lx; j++) gel(y,j) = Flv_to_Flx(gel(x,j), w);
187 219791 : return FlxX_renormalize(--y, lx+1);
188 : }
189 :
190 : /* P(X,Y) --> P(Y,X), n is the degree in Y */
191 : GEN
192 181424 : FlxX_swap(GEN x, long n, long ws)
193 : {
194 181424 : long j, lx = lg(x), ly = n+3;
195 181424 : GEN y = cgetg(ly, t_POL);
196 181426 : y[1] = x[1];
197 1165492 : for (j=2; j<ly; j++)
198 : {
199 : long k;
200 984072 : GEN p1 = cgetg(lx, t_VECSMALL);
201 984064 : p1[1] = ws;
202 11195191 : for (k=2; k<lx; k++)
203 10211127 : if (j<lg(gel(x,k)))
204 7184357 : p1[k] = mael(x,k,j);
205 : else
206 3026770 : p1[k] = 0;
207 984064 : gel(y,j) = Flx_renormalize(p1,lx);
208 : }
209 181420 : return FlxX_renormalize(y,ly);
210 : }
211 :
212 : static GEN
213 4353745 : zxX_to_Kronecker_spec(GEN P, long lp, long n)
214 : { /* P(X) = sum Pi(Y) * X^i, return P( Y^(2n-1) ) */
215 4353745 : long i, j, k, l, N = (n<<1) + 1;
216 4353745 : GEN y = cgetg((N-2)*lp + 2, t_VECSMALL) + 2;
217 28724751 : for (k=i=0; i<lp; i++)
218 : {
219 28688540 : GEN c = gel(P,i);
220 28688540 : l = lg(c);
221 28688540 : if (l-3 >= n)
222 0 : pari_err_BUG("zxX_to_Kronecker, P is not reduced mod Q");
223 167372563 : for (j=2; j < l; j++) y[k++] = c[j];
224 28688560 : if (i == lp-1) break;
225 217354354 : for ( ; j < N; j++) y[k++] = 0;
226 : }
227 4353758 : y -= 2;
228 4353758 : y[1] = 0; setlg(y, k+2); return y;
229 : }
230 :
231 : GEN
232 3773059 : zxX_to_Kronecker(GEN P, GEN Q)
233 : {
234 3773059 : GEN z = zxX_to_Kronecker_spec(P+2, lg(P)-2, degpol(Q));
235 3773078 : z[1] = P[1]; return z;
236 : }
237 :
238 : GEN
239 346162 : FlxX_add(GEN x, GEN y, ulong p)
240 : {
241 : long i,lz;
242 : GEN z;
243 346162 : long lx=lg(x);
244 346162 : long ly=lg(y);
245 346162 : if (ly>lx) swapspec(x,y, lx,ly);
246 346162 : lz = lx; z = cgetg(lz, t_POL); z[1]=x[1];
247 3902449 : for (i=2; i<ly; i++) gel(z,i) = Flx_add(gel(x,i), gel(y,i), p);
248 2136102 : for ( ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
249 346157 : return FlxX_renormalize(z, lz);
250 : }
251 :
252 : GEN
253 245 : FlxX_Flx_add(GEN y, GEN x, ulong p)
254 : {
255 245 : long i, lz = lg(y);
256 : GEN z;
257 245 : if (signe(y) == 0) return scalarpol(x, varn(y));
258 245 : z = cgetg(lz,t_POL); z[1] = y[1];
259 245 : gel(z,2) = Flx_add(gel(y,2), x, p);
260 245 : if (lz == 3) z = FlxX_renormalize(z,lz);
261 : else
262 1071 : for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
263 245 : return z;
264 : }
265 :
266 : GEN
267 11734 : FlxX_Flx_sub(GEN y, GEN x, ulong p)
268 : {
269 11734 : long i, lz = lg(y);
270 : GEN z;
271 11734 : if (signe(y) == 0) return scalarpol(x, varn(y));
272 11734 : z = cgetg(lz,t_POL); z[1] = y[1];
273 11734 : gel(z,2) = Flx_sub(gel(y,2), x, p);
274 11734 : if (lz == 3) z = FlxX_renormalize(z,lz);
275 : else
276 84319 : for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
277 11734 : return z;
278 : }
279 :
280 : GEN
281 2636 : FlxX_neg(GEN x, ulong p)
282 : {
283 2636 : long i, lx=lg(x);
284 2636 : GEN z = cgetg(lx, t_POL);
285 2636 : z[1]=x[1];
286 45407 : for (i=2; i<lx; i++) gel(z,i) = Flx_neg(gel(x,i), p);
287 2636 : return z;
288 : }
289 :
290 : GEN
291 761 : FlxX_Fl_mul(GEN x, ulong y, ulong p)
292 : {
293 761 : long i, lx=lg(x);
294 761 : GEN z = cgetg(lx, t_POL);
295 761 : z[1]=x[1];
296 3667 : for (i=2; i<lx; i++) gel(z,i) = Flx_Fl_mul(gel(x,i), y, p);
297 761 : return FlxX_renormalize(z, lx);
298 : }
299 :
300 : GEN
301 0 : FlxX_triple(GEN x, ulong p)
302 : {
303 0 : long i, lx=lg(x);
304 0 : GEN z = cgetg(lx, t_POL);
305 0 : z[1]=x[1];
306 0 : for (i=2; i<lx; i++) gel(z,i) = Flx_triple(gel(x,i), p);
307 0 : return FlxX_renormalize(z, lx);
308 : }
309 :
310 : GEN
311 761 : FlxX_double(GEN x, ulong p)
312 : {
313 761 : long i, lx=lg(x);
314 761 : GEN z = cgetg(lx, t_POL);
315 761 : z[1]=x[1];
316 6088 : for (i=2; i<lx; i++) gel(z,i) = Flx_double(gel(x,i), p);
317 761 : return FlxX_renormalize(z, lx);
318 : }
319 :
320 : GEN
321 82241 : FlxX_deriv(GEN z, ulong p)
322 : {
323 82241 : long i,l = lg(z)-1;
324 : GEN x;
325 82241 : if (l < 2) l = 2;
326 82241 : x = cgetg(l, t_POL); x[1] = z[1];
327 892672 : for (i=2; i<l; i++) gel(x,i) = Flx_mulu(gel(z,i+1), (ulong) i-1, p);
328 82240 : return FlxX_renormalize(x,l);
329 : }
330 :
331 : GEN
332 0 : FlxX_translate1(GEN P, long p, long n)
333 : {
334 : GEN Q;
335 0 : long i, l, ws, lP = lgpol(P);
336 0 : if (!lP) return gcopy(P);
337 0 : ws = mael(P,2,1);
338 0 : Q = FlxX_swap(P, n, ws);
339 0 : l = lg(Q);
340 0 : for (i=2; i<l; i++) gel(Q, i) = Flx_translate1(gel(Q, i), p);
341 0 : return FlxX_swap(Q, lP, ws);
342 : }
343 :
344 : GEN
345 0 : zlxX_translate1(GEN P, long p, long e, long n)
346 : {
347 : GEN Q;
348 0 : long i, l, ws, lP = lgpol(P);
349 0 : if (!lP) return gcopy(P);
350 0 : ws = mael(P,2,1);
351 0 : Q = FlxX_swap(P, n, ws);
352 0 : l = lg(Q);
353 0 : for (i=2; i<l; i++) gel(Q, i) = zlx_translate1(gel(Q, i), p, e);
354 0 : return FlxX_swap(Q, lP, ws);
355 : }
356 :
357 : static GEN
358 109012 : FlxX_subspec(GEN x, GEN y, ulong p, long lx, long ly)
359 : {
360 : long i,lz;
361 : GEN z;
362 :
363 109012 : if (ly <= lx)
364 : {
365 109012 : lz = lx+2; z = cgetg(lz, t_POL);
366 3324116 : for (i=0; i<ly; i++) gel(z,i+2) = Flx_sub(gel(x,i),gel(y,i),p);
367 110789 : for ( ; i<lx; i++) gel(z,i+2) = Flx_copy(gel(x,i));
368 : }
369 : else
370 : {
371 0 : lz = ly+2; z = cgetg(lz, t_POL);
372 0 : for (i=0; i<lx; i++) gel(z,i+2) = Flx_sub(gel(x,i),gel(y,i),p);
373 0 : for ( ; i<ly; i++) gel(z,i+2) = Flx_neg(gel(y,i),p);
374 : }
375 109012 : z[1] = 0; return FlxX_renormalize(z, lz);
376 : }
377 :
378 : GEN
379 624051 : FlxX_sub(GEN x, GEN y, ulong p)
380 : {
381 : long lx,ly,i,lz;
382 : GEN z;
383 624051 : lx = lg(x); ly = lg(y);
384 624051 : lz=maxss(lx,ly);
385 624052 : z = cgetg(lz,t_POL);
386 624052 : if (lx >= ly)
387 : {
388 110103 : z[1] = x[1];
389 638399 : for (i=2; i<ly; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
390 388081 : for ( ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
391 110103 : if (lx==ly) z = FlxX_renormalize(z, lz);
392 : }
393 : else
394 : {
395 513949 : z[1] = y[1];
396 1300529 : for (i=2; i<lx; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
397 1381231 : for ( ; i<ly; i++) gel(z,i) = Flx_neg(gel(y,i),p);
398 : }
399 624002 : if (!lgpol(z)) { set_avma((pari_sp)(z + lz)); z = pol_0(varn(x)); }
400 624029 : return z;
401 : }
402 :
403 : GEN
404 0 : FlxX_Flx_mul(GEN P, GEN U, ulong p)
405 : {
406 0 : long i, lP = lg(P);
407 0 : GEN res = cgetg(lP,t_POL);
408 0 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
409 0 : res[1] = P[1];
410 0 : for(i=2; i<lP; i++) gel(res,i) = Flx_mul_pre(U,gel(P,i), p, pi);
411 0 : return FlxX_renormalize(res, lP);
412 : }
413 :
414 : GEN
415 2310146 : FlxY_evalx_pre(GEN Q, ulong x, ulong p, ulong pi)
416 : {
417 2310146 : long i, lb = lg(Q);
418 : GEN z;
419 2310146 : z = cgetg(lb,t_VECSMALL); z[1] = evalvarn(varn(Q));
420 19998295 : for (i=2; i<lb; i++) z[i] = Flx_eval_pre(gel(Q,i), x, p, pi);
421 2310940 : return Flx_renormalize(z, lb);
422 : }
423 : GEN
424 231 : FlxY_evalx(GEN Q, ulong x, ulong p)
425 231 : { return FlxY_evalx_pre(Q, x, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
426 :
427 : GEN
428 0 : FlxY_Flx_translate(GEN P, GEN c, ulong p)
429 : {
430 0 : pari_sp av = avma;
431 0 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
432 : GEN Q;
433 : long i, k, n;
434 :
435 0 : if (!signe(P) || gequal0(c)) return RgX_copy(P);
436 0 : Q = leafcopy(P); n = degpol(P);
437 0 : for (i=1; i<=n; i++)
438 : {
439 0 : for (k=n-i; k<n; k++)
440 0 : gel(Q,2+k) = Flx_add(gel(Q,2+k), Flx_mul_pre(gel(Q,2+k+1), c, p, pi), p);
441 0 : if (gc_needed(av,2))
442 : {
443 0 : if(DEBUGMEM>1)
444 0 : pari_warn(warnmem,"FlxY_Flx_translate, i = %ld/%ld", i,n);
445 0 : Q = gerepilecopy(av, Q);
446 : }
447 : }
448 0 : return gerepilecopy(av, Q);
449 : }
450 :
451 : /* allow pi = 0 */
452 : GEN
453 10185418 : FlxY_evalx_powers_pre(GEN pol, GEN ypowers, ulong p, ulong pi)
454 : {
455 10185418 : long i, len = lg(pol);
456 10185418 : GEN res = cgetg(len, t_VECSMALL);
457 10182366 : res[1] = pol[1] & VARNBITS;
458 34537850 : for (i = 2; i < len; ++i)
459 24350914 : res[i] = Flx_eval_powers_pre(gel(pol, i), ypowers, p, pi);
460 10186936 : return Flx_renormalize(res, len);
461 : }
462 :
463 : /* allow pi = 0 */
464 : ulong
465 6833638 : FlxY_eval_powers_pre(GEN pol, GEN ypowers, GEN xpowers, ulong p, ulong pi)
466 : {
467 6833638 : pari_sp av = avma;
468 6833638 : GEN t = FlxY_evalx_powers_pre(pol, ypowers, p, pi);
469 6829136 : return gc_ulong(av, Flx_eval_powers_pre(t, xpowers, p, pi));
470 : }
471 :
472 : GEN
473 137260 : FlxY_FlxqV_evalx_pre(GEN P, GEN x, GEN T, ulong p, ulong pi)
474 : {
475 137260 : long i, lP = lg(P);
476 137260 : GEN res = cgetg(lP,t_POL);
477 137260 : res[1] = P[1];
478 931751 : for(i=2; i<lP; i++)
479 794491 : gel(res,i) = Flx_FlxqV_eval_pre(gel(P,i), x, T, p, pi);
480 137260 : return FlxX_renormalize(res, lP);
481 : }
482 : GEN
483 0 : FlxY_FlxqV_evalx(GEN P, GEN x, GEN T, ulong p)
484 0 : { return FlxY_FlxqV_evalx_pre(P, x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
485 :
486 : GEN
487 0 : FlxY_Flxq_evalx_pre(GEN P, GEN x, GEN T, ulong p, ulong pi)
488 : {
489 0 : pari_sp av = avma;
490 0 : long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(P),1);
491 0 : GEN xp = Flxq_powers_pre(x, n, T, p, pi);
492 0 : return gerepileupto(av, FlxY_FlxqV_evalx_pre(P, xp, T, p, pi));
493 : }
494 : GEN
495 0 : FlxY_Flxq_evalx(GEN P, GEN x, GEN T, ulong p)
496 0 : { return FlxY_Flxq_evalx_pre(P, x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
497 :
498 : GEN
499 7575 : FlxY_Flx_div(GEN x, GEN y, ulong p)
500 : {
501 : long i, l;
502 : GEN z;
503 7575 : if (degpol(y) == 0)
504 : {
505 5610 : ulong t = uel(y,2);
506 5610 : if (t == 1) return x;
507 0 : t = Fl_inv(t, p);
508 0 : z = cgetg_copy(x, &l); z[1] = x[1];
509 0 : for (i=2; i<l; i++) gel(z,i) = Flx_Fl_mul(gel(x,i),t,p);
510 : }
511 : else
512 : {
513 1965 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
514 1965 : z = cgetg_copy(x, &l); z[1] = x[1];
515 3930 : for (i=2; i<l; i++) gel(z,i) = Flx_div_pre(gel(x,i),y,p,pi);
516 : }
517 1965 : return z;
518 : }
519 :
520 : GEN
521 71515 : FlxX_shift(GEN a, long n, long vs)
522 : {
523 71515 : long i, l = lg(a);
524 : GEN b;
525 71515 : if (l == 2 || !n) return a;
526 64660 : l += n;
527 64660 : if (n < 0)
528 : {
529 35761 : if (l <= 2) return pol_0(varn(a));
530 29529 : b = cgetg(l, t_POL); b[1] = a[1];
531 29529 : a -= n;
532 611185 : for (i=2; i<l; i++) gel(b,i) = gel(a,i);
533 : } else {
534 28899 : b = cgetg(l, t_POL); b[1] = a[1];
535 28900 : a -= n; n += 2;
536 503140 : for (i=2; i<n; i++) gel(b,i) = pol0_Flx(vs);
537 363864 : for ( ; i<l; i++) gel(b,i) = gel(a,i);
538 : }
539 58428 : return b;
540 : }
541 :
542 : GEN
543 13028 : FlxX_blocks(GEN P, long n, long m, long vs)
544 : {
545 13028 : GEN z = cgetg(m+1,t_VEC);
546 13028 : long i,j, k=2, l = lg(P);
547 39082 : for(i=1; i<=m; i++)
548 : {
549 26053 : GEN zi = cgetg(n+2,t_POL);
550 26053 : zi[1] = P[1];
551 26053 : gel(z,i) = zi;
552 159483 : for(j=2; j<n+2; j++)
553 133459 : gel(zi, j) = k==l ? pol0_Flx(vs) : gel(P,k++);
554 26024 : zi = FlxX_renormalize(zi, n+2);
555 : }
556 13029 : return z;
557 : }
558 :
559 : static GEN
560 233048 : FlxX_recipspec(GEN x, long l, long n, long vs)
561 : {
562 : long i;
563 233048 : GEN z = cgetg(n+2,t_POL);
564 233048 : z[1] = 0; z += 2;
565 5242723 : for(i=0; i<l; i++)
566 5009675 : gel(z,n-i-1) = Flx_copy(gel(x,i));
567 240342 : for( ; i<n; i++)
568 7294 : gel(z,n-i-1) = pol0_Flx(vs);
569 233048 : return FlxX_renormalize(z-2,n+2);
570 : }
571 :
572 : GEN
573 2390 : FlxX_invLaplace(GEN x, ulong p)
574 : {
575 2390 : long i, d = degpol(x);
576 : GEN y;
577 : ulong t;
578 2390 : if (d <= 1) return gcopy(x);
579 2383 : t = Fl_inv(factorial_Fl(d, p), p);
580 2383 : y = cgetg(d+3, t_POL);
581 2383 : y[1] = x[1];
582 47416 : for (i=d; i>=2; i--)
583 : {
584 45033 : gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
585 45035 : t = Fl_mul(t, i, p);
586 : }
587 2383 : gel(y,3) = Flx_copy(gel(x,3));
588 2383 : gel(y,2) = Flx_copy(gel(x,2));
589 2383 : return FlxX_renormalize(y, d+3);
590 : }
591 :
592 : GEN
593 1195 : FlxX_Laplace(GEN x, ulong p)
594 : {
595 1195 : long i, d = degpol(x);
596 1195 : ulong t = 1;
597 : GEN y;
598 1195 : if (d <= 1) return gcopy(x);
599 1195 : y = cgetg(d+3, t_POL);
600 1195 : y[1] = x[1];
601 1195 : gel(y,2) = Flx_copy(gel(x,2));
602 1195 : gel(y,3) = Flx_copy(gel(x,3));
603 23724 : for (i=2; i<=d; i++)
604 : {
605 22529 : t = Fl_mul(t, i%p, p);
606 22529 : gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
607 : }
608 1195 : return FlxX_renormalize(y, d+3);
609 : }
610 :
611 : /***********************************************************************/
612 : /** **/
613 : /** FlxXV **/
614 : /** **/
615 : /***********************************************************************/
616 :
617 : GEN
618 0 : FlxXC_sub(GEN x, GEN y, ulong p)
619 0 : { pari_APPLY_same(FlxX_sub(gel(x, i), gel(y,i), p)) }
620 :
621 : static GEN
622 139268 : FlxXV_to_FlxM_lg(GEN x, long m, long n, long sv)
623 : {
624 : long i;
625 139268 : GEN y = cgetg(n+1, t_MAT);
626 463156 : for (i=1; i<=n; i++) gel(y,i) = FlxX_to_FlxC(gel(x,i), m, sv);
627 139268 : return y;
628 : }
629 :
630 : GEN
631 0 : FlxXV_to_FlxM(GEN v, long n, long sv)
632 0 : { return FlxXV_to_FlxM_lg(v, n, lg(v)-1, sv); }
633 :
634 : GEN
635 12460 : FlxXC_to_ZXXC(GEN x)
636 62464 : { pari_APPLY_type(t_COL, FlxX_to_ZXX(gel(x,i))) }
637 :
638 : GEN
639 0 : FlxXM_to_ZXXM(GEN x)
640 0 : { pari_APPLY_same(FlxXC_to_ZXXC(gel(x,i))) }
641 :
642 : /***********************************************************************/
643 : /** **/
644 : /** FlxqX **/
645 : /** **/
646 : /***********************************************************************/
647 :
648 : static GEN
649 3588415 : get_FlxqX_red(GEN T, GEN *B)
650 : {
651 3588415 : if (typ(T)!=t_VEC) { *B=NULL; return T; }
652 139590 : *B = gel(T,1); return gel(T,2);
653 : }
654 :
655 : GEN
656 151335 : RgX_to_FlxqX(GEN x, GEN T, ulong p)
657 : {
658 151335 : long i, l = lg(x);
659 151335 : GEN z = cgetg(l, t_POL); z[1] = x[1];
660 2326544 : for (i = 2; i < l; i++)
661 2175214 : gel(z,i) = Rg_to_Flxq(gel(x,i), T, p);
662 151330 : return FlxX_renormalize(z, l);
663 : }
664 :
665 : /* FlxqX are t_POL with Flxq coefficients.
666 : * Normally the variable ordering should be respected.*/
667 :
668 : GEN
669 634 : random_FlxqX(long d1, long v, GEN T, ulong p)
670 : {
671 634 : long dT = get_Flx_degree(T), vT = get_Flx_var(T);
672 634 : long i, d = d1+2;
673 634 : GEN y = cgetg(d,t_POL); y[1] = evalsigne(1) | evalvarn(v);
674 3376 : for (i=2; i<d; i++) gel(y,i) = random_Flx(dT, vT, p);
675 634 : return FlxX_renormalize(y,d);
676 : }
677 :
678 : /*Not stack clean*/
679 : GEN
680 2354754 : Kronecker_to_FlxqX_pre(GEN z, GEN T, ulong p, ulong pi)
681 : {
682 2354754 : long i,j,lx,l, N = (get_Flx_degree(T)<<1) + 1;
683 2354741 : GEN x, t = cgetg(N,t_VECSMALL);
684 2354740 : t[1] = get_Flx_var(T);
685 2354737 : l = lg(z); lx = (l-2) / (N-2);
686 2354737 : x = cgetg(lx+3,t_POL);
687 2354740 : x[1] = z[1];
688 28775147 : for (i=2; i<lx+2; i++)
689 : {
690 367918896 : for (j=2; j<N; j++) t[j] = z[j];
691 26420429 : z += (N-2);
692 26420429 : gel(x,i) = Flx_rem_pre(Flx_renormalize(t,N), T,p,pi);
693 : }
694 2354718 : N = (l-2) % (N-2) + 2;
695 7475727 : for (j=2; j<N; j++) t[j] = z[j];
696 2354718 : gel(x,i) = Flx_rem_pre(Flx_renormalize(t,N), T,p,pi);
697 2354686 : return FlxX_renormalize(x, i+1);
698 : }
699 : GEN
700 0 : Kronecker_to_FlxqX(GEN z, GEN T, ulong p)
701 0 : { return Kronecker_to_FlxqX_pre(z, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
702 :
703 : GEN
704 1259357 : FlxqX_red_pre(GEN z, GEN T, ulong p, ulong pi)
705 : {
706 : GEN res;
707 1259357 : long i, l = lg(z);
708 1259357 : res = cgetg(l,t_POL); res[1] = z[1];
709 13000897 : for(i=2;i<l;i++) gel(res,i) = Flx_rem_pre(gel(z,i),T,p,pi);
710 1259306 : return FlxX_renormalize(res,l);
711 : }
712 : GEN
713 0 : FlxqX_red(GEN z, GEN T, ulong p)
714 0 : { return FlxqX_red_pre(z, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
715 :
716 : static GEN
717 290346 : FlxqX_mulspec(GEN x, GEN y, GEN T, ulong p, ulong pi, long lx, long ly)
718 : {
719 290346 : pari_sp av = avma;
720 : GEN z,kx,ky;
721 290346 : long dT = get_Flx_degree(T);
722 290346 : kx= zxX_to_Kronecker_spec(x,lx,dT);
723 290346 : ky= zxX_to_Kronecker_spec(y,ly,dT);
724 290346 : z = Flx_mul_pre(ky, kx, p, pi);
725 290346 : z = Kronecker_to_FlxqX_pre(z,T,p,pi);
726 290346 : return gerepileupto(av, z);
727 : }
728 :
729 : GEN
730 1708651 : FlxqX_mul_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
731 : {
732 1708651 : pari_sp av = avma;
733 1708651 : GEN z, kx, ky, Tm = get_Flx_mod(T);
734 1708649 : kx= zxX_to_Kronecker(x, Tm);
735 1708672 : ky= zxX_to_Kronecker(y, Tm);
736 1708670 : z = Flx_mul_pre(ky, kx, p, pi);
737 1708650 : z = Kronecker_to_FlxqX_pre(z, T, p, pi);
738 1708588 : return gerepileupto(av, z);
739 : }
740 : GEN
741 63691 : FlxqX_mul(GEN x, GEN y, GEN T, ulong p)
742 63691 : { return FlxqX_mul_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
743 :
744 : GEN
745 355777 : FlxqX_sqr_pre(GEN x, GEN T, ulong p, ulong pi)
746 : {
747 355777 : pari_sp av = avma;
748 : GEN z,kx;
749 355777 : kx= zxX_to_Kronecker(x,get_Flx_mod(T));
750 355778 : z = Flx_sqr_pre(kx, p, pi);
751 355774 : z = Kronecker_to_FlxqX_pre(z,T,p,pi);
752 355775 : return gerepileupto(av, z);
753 : }
754 : GEN
755 1260 : FlxqX_sqr(GEN x, GEN T, ulong p)
756 1260 : { return FlxqX_sqr_pre(x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
757 :
758 : GEN
759 154907 : FlxqX_Flxq_mul_pre(GEN P, GEN U, GEN T, ulong p, ulong pi)
760 : {
761 154907 : long i, lP = lg(P);
762 154907 : GEN res = cgetg(lP,t_POL);
763 154907 : res[1] = P[1];
764 553477 : for(i=2; i<lP; i++) gel(res,i) = Flxq_mul_pre(U,gel(P,i), T,p,pi);
765 154897 : return FlxX_renormalize(res, lP);
766 : }
767 : GEN
768 0 : FlxqX_Flxq_mul(GEN P, GEN U, GEN T, ulong p)
769 0 : { return FlxqX_Flxq_mul_pre(P, U, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
770 :
771 : GEN
772 632407 : FlxqX_Flxq_mul_to_monic_pre(GEN P, GEN U, GEN T, ulong p, ulong pi)
773 : {
774 632407 : long i, lP = lg(P);
775 632407 : GEN res = cgetg(lP,t_POL);
776 632403 : res[1] = P[1];
777 3444687 : for(i=2; i<lP-1; i++) gel(res,i) = Flxq_mul_pre(U,gel(P,i), T,p,pi);
778 632027 : gel(res,lP-1) = pol1_Flx(get_Flx_var(T));
779 632407 : return FlxX_renormalize(res, lP);
780 : }
781 : GEN
782 0 : FlxqX_Flxq_mul_to_monic(GEN P, GEN U, GEN T, ulong p)
783 : {
784 0 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
785 0 : return FlxqX_Flxq_mul_to_monic_pre(P, U, T, p, pi);
786 : }
787 :
788 : GEN
789 189659 : FlxqX_normalize_pre(GEN z, GEN T, ulong p, ulong pi)
790 : {
791 189659 : GEN p1 = leading_coeff(z);
792 189659 : if (!lgpol(z) || (!degpol(p1) && p1[1] == 1)) return z;
793 189638 : return FlxqX_Flxq_mul_to_monic_pre(z, Flxq_inv_pre(p1,T,p,pi), T,p,pi);
794 : }
795 : GEN
796 133 : FlxqX_normalize(GEN z, GEN T, ulong p)
797 133 : { return FlxqX_normalize_pre(z, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
798 :
799 : struct _FlxqX {ulong p, pi; GEN T;};
800 538491 : static GEN _FlxqX_mul(void *data,GEN a,GEN b)
801 : {
802 538491 : struct _FlxqX *d=(struct _FlxqX*)data;
803 538491 : return FlxqX_mul_pre(a,b,d->T,d->p,d->pi);
804 : }
805 10283 : static GEN _FlxqX_sqr(void *data,GEN a)
806 : {
807 10283 : struct _FlxqX *d=(struct _FlxqX*)data;
808 10283 : return FlxqX_sqr_pre(a,d->T,d->p,d->pi);
809 : }
810 :
811 : GEN
812 10248 : FlxqX_powu_pre(GEN V, ulong n, GEN T, ulong p, ulong pi)
813 : {
814 10248 : struct _FlxqX d; d.p = p; d.pi = pi; d.T = T;
815 10248 : return gen_powu(V, n, (void*)&d, &_FlxqX_sqr, &_FlxqX_mul);
816 : }
817 : GEN
818 0 : FlxqX_powu(GEN V, ulong n, GEN T, ulong p)
819 0 : { return FlxqX_powu_pre(V, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
820 :
821 : /* x and y in Z[Y][X]. Assume T irreducible mod p */
822 : static GEN
823 3140603 : FlxqX_divrem_basecase(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *pr)
824 : {
825 : long vx, dx, dy, dz, i, j, sx, lr;
826 :
827 : pari_sp av0, av, tetpil;
828 : GEN z,p1,rem,lead;
829 :
830 3140603 : if (!signe(y)) pari_err_INV("FlxqX_divrem",y);
831 3140603 : vx=varn(x); dy=degpol(y); dx=degpol(x);
832 3140614 : if (dx < dy)
833 : {
834 21666 : if (pr)
835 : {
836 21362 : av0 = avma; x = FlxqX_red_pre(x, T, p, pi);
837 21362 : if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: pol_0(vx); }
838 21362 : if (pr == ONLY_REM) return x;
839 21362 : *pr = x;
840 : }
841 21666 : return pol_0(vx);
842 : }
843 3118948 : lead = leading_coeff(y);
844 3118962 : if (!dy) /* y is constant */
845 : {
846 325138 : if (pr && pr != ONLY_DIVIDES)
847 : {
848 320479 : if (pr == ONLY_REM) return pol_0(vx);
849 133630 : *pr = pol_0(vx);
850 : }
851 138291 : if (Flx_equal1(lead)) return gcopy(x);
852 134108 : av0 = avma; x = FlxqX_Flxq_mul_pre(x,Flxq_inv(lead,T,p),T,p,pi);
853 134105 : return gerepileupto(av0,x);
854 : }
855 2793824 : av0 = avma; dz = dx-dy;
856 2793824 : lead = Flx_equal1(lead)? NULL: gclone(Flxq_inv_pre(lead,T,p,pi));
857 2793874 : set_avma(av0);
858 2793873 : z = cgetg(dz+3,t_POL); z[1] = x[1];
859 2793882 : x += 2; y += 2; z += 2;
860 :
861 2793882 : p1 = gel(x,dx); av = avma;
862 2793882 : gel(z,dz) = lead? gerepileupto(av, Flxq_mul_pre(p1,lead, T,p,pi)): gcopy(p1);
863 6446779 : for (i=dx-1; i>=dy; i--)
864 : {
865 3652898 : av=avma; p1=gel(x,i);
866 11931437 : for (j=i-dy+1; j<=i && j<=dz; j++)
867 8278729 : p1 = Flx_sub(p1, Flx_mul_pre(gel(z,j),gel(y,i-j),p,pi),p);
868 3652708 : if (lead) p1 = Flx_mul_pre(p1, lead,p,pi);
869 3652707 : tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil,Flx_rem_pre(p1,T,p,pi));
870 : }
871 2793881 : if (!pr) { guncloneNULL(lead); return z-2; }
872 :
873 2599826 : rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
874 2909011 : for (sx=0; ; i--)
875 : {
876 2909011 : p1 = gel(x,i);
877 8583596 : for (j=0; j<=i && j<=dz; j++)
878 5674933 : p1 = Flx_sub(p1, Flx_mul_pre(gel(z,j),gel(y,i-j),p,pi),p);
879 2908663 : tetpil=avma; p1 = Flx_rem_pre(p1, T,p,pi); if (lgpol(p1)) { sx = 1; break; }
880 381104 : if (!i) break;
881 309153 : set_avma(av);
882 : }
883 2599552 : if (pr == ONLY_DIVIDES)
884 : {
885 0 : guncloneNULL(lead);
886 0 : if (sx) return gc_NULL(av0);
887 0 : return gc_const((pari_sp)rem, z-2);
888 : }
889 2599552 : lr=i+3; rem -= lr;
890 2599552 : rem[0] = evaltyp(t_POL) | _evallg(lr);
891 2599552 : rem[1] = z[-1];
892 2599552 : p1 = gerepile((pari_sp)rem,tetpil,p1);
893 2599783 : rem += 2; gel(rem,i) = p1;
894 16284401 : for (i--; i>=0; i--)
895 : {
896 13684618 : av=avma; p1 = gel(x,i);
897 44713172 : for (j=0; j<=i && j<=dz; j++)
898 31033818 : p1 = Flx_sub(p1, Flx_mul_pre(gel(z,j),gel(y,i-j),p,pi), p);
899 13679354 : tetpil=avma; gel(rem,i) = gerepile(av,tetpil, Flx_rem_pre(p1, T,p,pi));
900 : }
901 2599783 : rem -= 2;
902 2599783 : guncloneNULL(lead);
903 2599792 : if (!sx) (void)FlxX_renormalize(rem, lr);
904 2599793 : if (pr == ONLY_REM) return gerepileupto(av0,rem);
905 1219273 : *pr = rem; return z-2;
906 : }
907 :
908 : static GEN
909 1340 : FlxqX_invBarrett_basecase(GEN T, GEN Q, ulong p, ulong pi)
910 : {
911 1340 : long i, l=lg(T)-1, lr = l-1, k;
912 1340 : long sv=Q[1];
913 1340 : GEN r=cgetg(lr,t_POL); r[1]=T[1];
914 1340 : gel(r,2) = pol1_Flx(sv);
915 15134 : for (i=3;i<lr;i++)
916 : {
917 13794 : pari_sp ltop=avma;
918 13794 : GEN u = Flx_neg(gel(T,l-i+2),p);
919 99301 : for (k=3;k<i;k++)
920 85507 : u = Flx_sub(u, Flxq_mul_pre(gel(T,l-i+k), gel(r,k), Q, p, pi), p);
921 13794 : gel(r,i) = gerepileupto(ltop, u);
922 : }
923 1340 : r = FlxX_renormalize(r,lr);
924 1340 : return r;
925 : }
926 :
927 : /* Return new lgpol */
928 : static long
929 325174 : FlxX_lgrenormalizespec(GEN x, long lx)
930 : {
931 : long i;
932 356282 : for (i = lx-1; i>=0; i--)
933 356282 : if (lgpol(gel(x,i))) break;
934 325174 : return i+1;
935 : }
936 :
937 : static GEN
938 6697 : FlxqX_invBarrett_Newton(GEN S, GEN T, ulong p, ulong pi)
939 : {
940 6697 : pari_sp av = avma;
941 6697 : long nold, lx, lz, lq, l = degpol(S), i, lQ;
942 6697 : GEN q, y, z, x = cgetg(l+2, t_POL) + 2;
943 6697 : long dT = get_Flx_degree(T), vT = get_Flx_var(T);
944 6697 : ulong mask = quadratic_prec_mask(l-2); /* assume l > 2 */
945 273192 : for (i=0;i<l;i++) gel(x,i) = pol0_Flx(vT);
946 6697 : q = FlxX_recipspec(S+2,l+1,l+1,dT);
947 6697 : lQ = lgpol(q); q+=2;
948 : /* We work on _spec_ FlxX's, all the l[xzq] below are lgpol's */
949 :
950 : /* initialize */
951 6697 : gel(x,0) = Flxq_inv_pre(gel(q,0),T, p, pi);
952 6697 : if (lQ>1 && degpol(gel(q,1)) >= dT)
953 0 : gel(q,1) = Flx_rem_pre(gel(q,1), T, p, pi);
954 6697 : if (lQ>1 && lgpol(gel(q,1)))
955 5686 : {
956 5686 : GEN u = gel(q, 1);
957 5686 : if (!Flx_equal1(gel(x,0)))
958 0 : u = Flxq_mul_pre(u, Flxq_sqr_pre(gel(x,0), T,p,pi), T,p,pi);
959 5686 : gel(x,1) = Flx_neg(u, p); lx = 2;
960 : }
961 : else
962 1011 : lx = 1;
963 6697 : nold = 1;
964 43173 : for (; mask > 1; )
965 : { /* set x -= x(x*q - 1) + O(t^(nnew + 1)), knowing x*q = 1 + O(t^(nold+1)) */
966 36476 : long i, lnew, nnew = nold << 1;
967 :
968 36476 : if (mask & 1) nnew--;
969 36476 : mask >>= 1;
970 :
971 36476 : lnew = nnew + 1;
972 36476 : lq = FlxX_lgrenormalizespec(q, minss(lQ,lnew));
973 36476 : z = FlxqX_mulspec(x, q, T,p,pi, lx, lq); /* FIXME: high product */
974 36476 : lz = lgpol(z); if (lz > lnew) lz = lnew;
975 36476 : z += 2;
976 : /* subtract 1 [=>first nold words are 0]: renormalize so that z(0) != 0 */
977 74564 : for (i = nold; i < lz; i++) if (lgpol(gel(z,i))) break;
978 36476 : nold = nnew;
979 36476 : if (i >= lz) continue; /* z-1 = 0(t^(nnew + 1)) */
980 :
981 : /* z + i represents (x*q - 1) / t^i */
982 35573 : lz = FlxX_lgrenormalizespec (z+i, lz-i);
983 35573 : z = FlxqX_mulspec(x, z+i, T,p,pi, lx, lz); /* FIXME: low product */
984 35573 : lz = lgpol(z); z += 2;
985 35573 : if (lz > lnew-i) lz = FlxX_lgrenormalizespec(z, lnew-i);
986 :
987 35573 : lx = lz+ i;
988 35573 : y = x + i; /* x -= z * t^i, in place */
989 278745 : for (i = 0; i < lz; i++) gel(y,i) = Flx_neg(gel(z,i), p);
990 : }
991 6697 : x -= 2; setlg(x, lx + 2); x[1] = S[1];
992 6697 : return gerepilecopy(av, x);
993 : }
994 :
995 : GEN
996 8037 : FlxqX_invBarrett_pre(GEN T, GEN Q, ulong p, ulong pi)
997 : {
998 8037 : pari_sp ltop=avma;
999 8037 : long l=lg(T), v = varn(T);
1000 : GEN r;
1001 8037 : GEN c = gel(T,l-1);
1002 8037 : if (l<5) return pol_0(v);
1003 8037 : if (l<=FlxqX_INVBARRETT_LIMIT)
1004 : {
1005 1340 : if (!Flx_equal1(c))
1006 : {
1007 0 : GEN ci = Flxq_inv_pre(c,Q,p,pi);
1008 0 : T = FlxqX_Flxq_mul_pre(T, ci, Q, p, pi);
1009 0 : r = FlxqX_invBarrett_basecase(T,Q,p,pi);
1010 0 : r = FlxqX_Flxq_mul_pre(r,ci,Q,p,pi);
1011 : } else
1012 1340 : r = FlxqX_invBarrett_basecase(T,Q,p,pi);
1013 : } else
1014 6697 : r = FlxqX_invBarrett_Newton(T,Q,p,pi);
1015 8037 : return gerepileupto(ltop, r);
1016 : }
1017 : GEN
1018 76 : FlxqX_invBarrett(GEN T, GEN Q, ulong p)
1019 76 : { return FlxqX_invBarrett_pre(T, Q, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1020 :
1021 : GEN
1022 493265 : FlxqX_get_red_pre(GEN S, GEN T, ulong p, ulong pi)
1023 : {
1024 493265 : if (typ(S)==t_POL && lg(S)>FlxqX_BARRETT_LIMIT)
1025 6588 : retmkvec2(FlxqX_invBarrett_pre(S, T, p, pi), S);
1026 486677 : return S;
1027 : }
1028 : GEN
1029 371 : FlxqX_get_red(GEN S, GEN T, ulong p)
1030 : {
1031 371 : if (typ(S)==t_POL && lg(S)>FlxqX_BARRETT_LIMIT)
1032 76 : retmkvec2(FlxqX_invBarrett(S, T, p), S);
1033 295 : return S;
1034 : }
1035 :
1036 : /* Compute x mod S where 2 <= degpol(S) <= l+1 <= 2*(degpol(S)-1)
1037 : * * and mg is the Barrett inverse of S. */
1038 : static GEN
1039 109285 : FlxqX_divrem_Barrettspec(GEN x, long l, GEN mg, GEN S, GEN T, ulong p,
1040 : ulong pi, GEN *pr)
1041 : {
1042 : GEN q, r;
1043 109285 : long lt = degpol(S); /*We discard the leading term*/
1044 : long ld, lm, lT, lmg;
1045 109285 : ld = l-lt;
1046 109285 : lm = minss(ld, lgpol(mg));
1047 109285 : lT = FlxX_lgrenormalizespec(S+2,lt);
1048 109285 : lmg = FlxX_lgrenormalizespec(mg+2,lm);
1049 109285 : q = FlxX_recipspec(x+lt,ld,ld,0); /* = rec(x) lq<=ld*/
1050 109285 : q = FlxqX_mulspec(q+2,mg+2,T,p,pi,lgpol(q),lmg); /* = rec(x)*mg lq<=ld+lm*/
1051 109285 : q = FlxX_recipspec(q+2,minss(ld,lgpol(q)),ld,0); /* = rec(rec(x)*mg) lq<=ld*/
1052 109285 : if (!pr) return q;
1053 109012 : r = FlxqX_mulspec(q+2,S+2,T,p,pi,lgpol(q),lT); /* = q*pol lr<=ld+lt*/
1054 109012 : r = FlxX_subspec(x,r+2,p,lt,minss(lt,lgpol(r)));/* = x - r lr<=lt */
1055 109012 : if (pr == ONLY_REM) return r;
1056 7667 : *pr = r; return q;
1057 : }
1058 :
1059 : static GEN
1060 102410 : FlxqX_divrem_Barrett(GEN x, GEN mg, GEN S, GEN T, ulong p, ulong pi, GEN *pr)
1061 : {
1062 102410 : GEN q = NULL, r = FlxqX_red_pre(x, T, p, pi);
1063 102410 : long l = lgpol(r), lt = degpol(S), lm = 2*lt-1, v = varn(S);
1064 : long i;
1065 102410 : if (l <= lt)
1066 : {
1067 0 : if (pr == ONLY_REM) return r;
1068 0 : if (pr == ONLY_DIVIDES) return signe(r)? NULL: pol_0(v);
1069 0 : if (pr) *pr = r;
1070 0 : return pol_0(v);
1071 : }
1072 102410 : if (lt <= 1)
1073 0 : return FlxqX_divrem_basecase(x,S,T,p,pi,pr);
1074 102410 : if (pr != ONLY_REM && l>lm)
1075 : {
1076 750 : long vT = get_Flx_var(T);
1077 750 : q = cgetg(l-lt+2, t_POL); q[1] = S[1];
1078 53441 : for (i=0;i<l-lt;i++) gel(q+2,i) = pol0_Flx(vT);
1079 : }
1080 109367 : while (l>lm)
1081 : {
1082 6957 : GEN zr, zq = FlxqX_divrem_Barrettspec(r+2+l-lm,lm,mg,S,T,p,pi,&zr);
1083 6957 : long lz = lgpol(zr);
1084 6957 : if (pr != ONLY_REM)
1085 : {
1086 2568 : long lq = lgpol(zq);
1087 48051 : for(i=0; i<lq; i++) gel(q+2+l-lm,i) = gel(zq,2+i);
1088 : }
1089 69735 : for(i=0; i<lz; i++) gel(r+2+l-lm,i) = gel(zr,2+i);
1090 6957 : l = l-lm+lz;
1091 : }
1092 102410 : if (pr == ONLY_REM)
1093 : {
1094 101345 : if (l > lt)
1095 101345 : r = FlxqX_divrem_Barrettspec(r+2,l,mg,S,T,p,pi,ONLY_REM);
1096 : else
1097 0 : r = FlxX_renormalize(r, l+2);
1098 101345 : setvarn(r, v); return r;
1099 : }
1100 1065 : if (l > lt)
1101 : {
1102 983 : GEN zq = FlxqX_divrem_Barrettspec(r+2,l,mg,S,T,p,pi,pr? &r: NULL);
1103 983 : if (!q) q = zq;
1104 : else
1105 : {
1106 668 : long lq = lgpol(zq);
1107 5292 : for(i=0; i<lq; i++) gel(q+2,i) = gel(zq,2+i);
1108 : }
1109 : }
1110 82 : else if (pr)
1111 82 : r = FlxX_renormalize(r, l+2);
1112 1065 : setvarn(q, v); q = FlxX_renormalize(q, lg(q));
1113 1065 : if (pr == ONLY_DIVIDES) return signe(r)? NULL: q;
1114 1065 : if (pr) { setvarn(r, v); *pr = r; }
1115 1065 : return q;
1116 : }
1117 :
1118 : GEN
1119 1574224 : FlxqX_divrem_pre(GEN x, GEN S, GEN T, ulong p, long pi, GEN *pr)
1120 : {
1121 : GEN B, y;
1122 : long dy, dx, d;
1123 1574224 : if (pr==ONLY_REM) return FlxqX_rem_pre(x, S, T, p, pi);
1124 1574224 : y = get_FlxqX_red(S, &B);
1125 1574227 : dy = degpol(y); dx = degpol(x); d = dx-dy;
1126 1574184 : if (!B && d+3 < FlxqX_DIVREM_BARRETT_LIMIT)
1127 1573119 : return FlxqX_divrem_basecase(x,y,T,p,pi,pr);
1128 : else
1129 : {
1130 1065 : pari_sp av = avma;
1131 1065 : GEN mg = B? B: FlxqX_invBarrett_pre(y, T, p, pi);
1132 1065 : GEN q = FlxqX_divrem_Barrett(x,mg,y,T,p,pi,pr);
1133 1065 : if (!q) return gc_NULL(av);
1134 1065 : if (!pr || pr==ONLY_DIVIDES) return gerepilecopy(av, q);
1135 792 : return gc_all(av, 2, &q, pr);
1136 : }
1137 : }
1138 : GEN
1139 1072190 : FlxqX_divrem(GEN x, GEN S, GEN T, ulong p, GEN *pr)
1140 : {
1141 1072190 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1142 1072190 : return FlxqX_divrem_pre(x, S, T, p, pi, pr);
1143 : }
1144 :
1145 : GEN
1146 2013583 : FlxqX_rem_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
1147 : {
1148 2013583 : GEN B, y = get_FlxqX_red(S, &B);
1149 2013582 : long dy = degpol(y), dx = degpol(x), d = dx-dy;
1150 2013581 : if (d < 0) return FlxqX_red_pre(x, T, p, pi);
1151 1668752 : if (!B && d+3 < FlxqX_REM_BARRETT_LIMIT)
1152 1567407 : return FlxqX_divrem_basecase(x,y, T, p, pi, ONLY_REM);
1153 : else
1154 : {
1155 101345 : pari_sp av=avma;
1156 101345 : GEN mg = B? B: FlxqX_invBarrett_pre(y, T, p, pi);
1157 101345 : GEN r = FlxqX_divrem_Barrett(x, mg, y, T, p, pi, ONLY_REM);
1158 101345 : return gerepileupto(av, r);
1159 : }
1160 : }
1161 : GEN
1162 5949 : FlxqX_rem(GEN x, GEN S, GEN T, ulong p)
1163 5949 : { return FlxqX_rem_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1164 :
1165 : /* x + y*z mod p */
1166 : INLINE GEN
1167 107400 : Flxq_addmul_pre(GEN x, GEN y, GEN z, GEN T, ulong p, ulong pi)
1168 : {
1169 : pari_sp av;
1170 107400 : if (!lgpol(y) || !lgpol(z)) return Flx_rem_pre(x, T, p, pi);
1171 105935 : if (!lgpol(x)) return Flxq_mul_pre(z, y, T, p, pi);
1172 105849 : av = avma;
1173 105849 : return gerepileupto(av, Flx_add(x, Flxq_mul_pre(y, z, T, p, pi), p));
1174 : }
1175 :
1176 : GEN
1177 53700 : FlxqX_div_by_X_x_pre(GEN a, GEN x, GEN T, ulong p, ulong pi, GEN *r)
1178 : {
1179 53700 : long l = lg(a), i;
1180 : GEN z;
1181 53700 : if (l <= 3)
1182 : {
1183 0 : if (r) *r = l == 2? pol0_Flx(get_Flx_var(T)): Flx_copy(gel(a,2));
1184 0 : return pol_0(varn(a));
1185 : }
1186 53700 : l--; z = cgetg(l, t_POL); z[1] = a[1];
1187 53700 : gel(z, l-1) = gel(a,l);
1188 161100 : for (i=l-2; i>1; i--) /* z[i] = a[i+1] + x*z[i+1] */
1189 107400 : gel(z, i) = Flxq_addmul_pre(gel(a,i+1), x, gel(z,i+1), T, p, pi);
1190 53700 : if (r) *r = Flxq_addmul_pre(gel(a,2), x, gel(z,2), T, p, pi);
1191 53700 : return z;
1192 : }
1193 :
1194 : GEN
1195 53700 : FlxqX_div_by_X_x(GEN a, GEN x, GEN T, ulong p, GEN *r)
1196 53700 : { return FlxqX_div_by_X_x_pre(a, x, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p), r); }
1197 :
1198 : static GEN
1199 13580 : FlxqX_addmulmul(GEN u, GEN v, GEN x, GEN y, GEN T, ulong p, ulong pi)
1200 : {
1201 13580 : return FlxX_add(FlxqX_mul_pre(u, x, T, p, pi),
1202 : FlxqX_mul_pre(v, y, T, p, pi), p);
1203 : }
1204 :
1205 : static GEN
1206 6651 : FlxqXM_FlxqX_mul2(GEN M, GEN x, GEN y, GEN T, ulong p, ulong pi)
1207 : {
1208 6651 : GEN res = cgetg(3, t_COL);
1209 6651 : gel(res, 1) = FlxqX_addmulmul(gcoeff(M,1,1), gcoeff(M,1,2), x, y, T, p, pi);
1210 6651 : gel(res, 2) = FlxqX_addmulmul(gcoeff(M,2,1), gcoeff(M,2,2), x, y, T, p, pi);
1211 6651 : return res;
1212 : }
1213 :
1214 : static GEN
1215 3289 : FlxqXM_mul2(GEN A, GEN B, GEN T, ulong p, ulong pi)
1216 : {
1217 3289 : GEN A11=gcoeff(A,1,1),A12=gcoeff(A,1,2), B11=gcoeff(B,1,1),B12=gcoeff(B,1,2);
1218 3289 : GEN A21=gcoeff(A,2,1),A22=gcoeff(A,2,2), B21=gcoeff(B,2,1),B22=gcoeff(B,2,2);
1219 3289 : GEN M1 = FlxqX_mul_pre(FlxX_add(A11,A22, p), FlxX_add(B11,B22, p), T, p, pi);
1220 3289 : GEN M2 = FlxqX_mul_pre(FlxX_add(A21,A22, p), B11, T, p, pi);
1221 3289 : GEN M3 = FlxqX_mul_pre(A11, FlxX_sub(B12,B22, p), T, p, pi);
1222 3289 : GEN M4 = FlxqX_mul_pre(A22, FlxX_sub(B21,B11, p), T, p, pi);
1223 3289 : GEN M5 = FlxqX_mul_pre(FlxX_add(A11,A12, p), B22, T, p, pi);
1224 3289 : GEN M6 = FlxqX_mul_pre(FlxX_sub(A21,A11, p), FlxX_add(B11,B12, p), T, p, pi);
1225 3289 : GEN M7 = FlxqX_mul_pre(FlxX_sub(A12,A22, p), FlxX_add(B21,B22, p), T, p, pi);
1226 3289 : GEN T1 = FlxX_add(M1,M4, p), T2 = FlxX_sub(M7,M5, p);
1227 3289 : GEN T3 = FlxX_sub(M1,M2, p), T4 = FlxX_add(M3,M6, p);
1228 3289 : retmkmat22(FlxX_add(T1,T2, p), FlxX_add(M3,M5, p),
1229 : FlxX_add(M2,M4, p), FlxX_add(T3,T4, p));
1230 : }
1231 :
1232 : /* Return [0,1;1,-q]*M */
1233 : static GEN
1234 3289 : FlxqX_FlxqXM_qmul(GEN q, GEN M, GEN T, ulong p, ulong pi)
1235 : {
1236 3289 : GEN u = FlxqX_mul_pre(gcoeff(M,2,1), q, T,p,pi);
1237 3289 : GEN v = FlxqX_mul_pre(gcoeff(M,2,2), q, T,p,pi);
1238 3289 : retmkmat22(gcoeff(M,2,1), gcoeff(M,2,2),
1239 : FlxX_sub(gcoeff(M,1,1), u, p), FlxX_sub(gcoeff(M,1,2), v, p));
1240 : }
1241 :
1242 : static GEN
1243 0 : matid2_FlxXM(long v, long sv)
1244 0 : { retmkmat22(pol1_FlxX(v, sv),pol_0(v),pol_0(v),pol1_FlxX(v, sv)); }
1245 :
1246 : static GEN
1247 0 : matJ2_FlxXM(long v, long sv)
1248 0 : { retmkmat22(pol_0(v),pol1_FlxX(v, sv),pol1_FlxX(v, sv),pol_0(v)); }
1249 :
1250 : struct FlxqX_res
1251 : {
1252 : GEN res, lc;
1253 : long deg0, deg1, off;
1254 : };
1255 :
1256 : INLINE void
1257 0 : FlxqX_halfres_update(long da, long db, long dr, GEN T, ulong p, ulong pi, struct FlxqX_res *res)
1258 : {
1259 0 : if (dr >= 0)
1260 : {
1261 0 : if (!Flx_equal1(res->lc))
1262 : {
1263 0 : res->lc = Flxq_powu_pre(res->lc, da - dr, T, p, pi);
1264 0 : res->res = Flxq_mul_pre(res->res, res->lc, T, p, pi);
1265 : }
1266 0 : if (both_odd(da + res->off, db + res->off))
1267 0 : res->res = Flx_neg(res->res, p);
1268 : } else
1269 : {
1270 0 : if (db == 0)
1271 : {
1272 0 : if (!Flx_equal1(res->lc))
1273 : {
1274 0 : res->lc = Flxq_powu_pre(res->lc, da, T, p, pi);
1275 0 : res->res = Flxq_mul_pre(res->res, res->lc, T, p, pi);
1276 : }
1277 : } else
1278 0 : res->res = pol0_Flx(get_Flx_var(T));
1279 : }
1280 0 : }
1281 :
1282 : static GEN
1283 4114 : FlxqX_halfres_basecase(GEN a, GEN b, GEN T, ulong p, ulong pi, GEN *pa, GEN *pb, struct FlxqX_res *res)
1284 : {
1285 4114 : pari_sp av=avma;
1286 : GEN u,u1,v,v1, M;
1287 4114 : long vx = varn(a), vT = get_Flx_var(T), n = lgpol(a)>>1;
1288 4114 : u1 = v = pol_0(vx);
1289 4114 : u = v1 = pol1_FlxX(vx, vT);
1290 22801 : while (lgpol(b)>n)
1291 : {
1292 : GEN r, q;
1293 18687 : q = FlxqX_divrem(a,b, T, p, &r);
1294 18687 : if (res)
1295 : {
1296 0 : long da = degpol(a), db = degpol(b), dr = degpol(r);
1297 0 : res->lc = gel(b,db+2);
1298 0 : if (dr >= n)
1299 0 : FlxqX_halfres_update(da, db, dr, T, p, pi, res);
1300 : else
1301 : {
1302 0 : res->deg0 = da;
1303 0 : res->deg1 = db;
1304 : }
1305 : }
1306 18687 : a = b; b = r; swap(u,u1); swap(v,v1);
1307 18687 : u1 = FlxX_sub(u1, FlxqX_mul_pre(u, q, T, p, pi), p);
1308 18687 : v1 = FlxX_sub(v1, FlxqX_mul_pre(v, q, T, p, pi), p);
1309 18687 : if (gc_needed(av,2))
1310 : {
1311 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_halfgcd (d = %ld)",degpol(b));
1312 0 : gerepileall(av,res ? 8: 6, &a,&b,&u1,&v1,&u,&v,&res->res,&res->lc);
1313 : }
1314 : }
1315 4114 : M = mkmat22(u,v,u1,v1); *pa = a; *pb = b;
1316 4114 : return gc_all(av, res ? 5: 3, &M, pa, pb, &res->res, &res->lc);
1317 : }
1318 :
1319 : static GEN FlxqX_halfres_i(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, struct FlxqX_res *res);
1320 :
1321 : static GEN
1322 3362 : FlxqX_halfres_split(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, struct FlxqX_res *res)
1323 : {
1324 3362 : pari_sp av = avma;
1325 : GEN Q, R, S, V1, V2;
1326 : GEN x1, y1, r, q;
1327 3362 : long l = lgpol(x), n = l>>1, k, vT = get_Flx_var(T);
1328 3362 : if (lgpol(y) <= n)
1329 0 : { *a = RgX_copy(x); *b = RgX_copy(y); return matid2_FlxXM(varn(x), vT); }
1330 3362 : if (res)
1331 : {
1332 0 : res->lc = leading_coeff(y);
1333 0 : res->deg0 -= n;
1334 0 : res->deg1 -= n;
1335 0 : res->off += n;
1336 : }
1337 3362 : R = FlxqX_halfres_i(FlxX_shift(x,-n, vT),FlxX_shift(y,-n, vT), T, p, pi, a, b, res);
1338 3362 : if (res)
1339 : {
1340 0 : res->off -= n;
1341 0 : res->deg0 += n;
1342 0 : res->deg1 += n;
1343 : }
1344 3362 : V1 = FlxqXM_FlxqX_mul2(R, Flxn_red(x,n), Flxn_red(y,n), T, p, pi);
1345 3362 : x1 = FlxX_add(FlxX_shift(*a,n,vT), gel(V1,1), p);
1346 3362 : y1 = FlxX_add(FlxX_shift(*b,n,vT), gel(V1,2), p);
1347 3362 : if (lgpol(y1) <= n)
1348 : {
1349 73 : *a = x1; *b = y1;
1350 73 : return gc_all(av, res ? 5: 3, &R, a, b, &res->res, &res->lc);
1351 : }
1352 3289 : k = 2*n-degpol(y1);
1353 3289 : q = FlxqX_divrem(x1, y1, T, p, &r);
1354 3289 : if (res)
1355 : {
1356 0 : long dx1 = degpol(x1), dy1 = degpol(y1), dr = degpol(r);
1357 0 : if (dy1 < degpol(y))
1358 0 : FlxqX_halfres_update(res->deg0, res->deg1, dy1, T, p, pi, res);
1359 0 : res->lc = leading_coeff(y1);
1360 0 : res->deg0 = dx1;
1361 0 : res->deg1 = dy1;
1362 0 : if (dr >= n)
1363 : {
1364 0 : FlxqX_halfres_update(dx1, dy1, dr, T, p, pi, res);
1365 0 : res->deg0 = dy1;
1366 0 : res->deg1 = dr;
1367 : }
1368 0 : res->deg0 -= k;
1369 0 : res->deg1 -= k;
1370 0 : res->off += k;
1371 : }
1372 3289 : S = FlxqX_halfres_i(FlxX_shift(y1,-k, vT), FlxX_shift(r,-k, vT), T, p, pi, a, b, res);
1373 3289 : if (res)
1374 : {
1375 0 : res->deg0 += k;
1376 0 : res->deg1 += k;
1377 0 : res->off -= k;
1378 : }
1379 3289 : Q = FlxqXM_mul2(S, FlxqX_FlxqXM_qmul(q, R, T, p, pi), T, p, pi);
1380 3289 : V2 = FlxqXM_FlxqX_mul2(S, FlxXn_red(y1,k), FlxXn_red(r,k), T, p, pi);
1381 3289 : *a = FlxX_add(FlxX_shift(*a,k,vT), gel(V2,1), p);
1382 3289 : *b = FlxX_add(FlxX_shift(*b,k,vT), gel(V2,2), p);
1383 3289 : return gc_all(av, res ? 5: 3, &Q, a, b, &res->res, &res->lc);
1384 : }
1385 :
1386 : static GEN
1387 7476 : FlxqX_halfres_i(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, struct FlxqX_res *res)
1388 : {
1389 7476 : if (lgpol(x) < FlxqX_HALFGCD_LIMIT)
1390 4114 : return FlxqX_halfres_basecase(x, y, T, p, pi, a, b, res);
1391 3362 : return FlxqX_halfres_split(x, y, T, p, pi, a, b, res);
1392 : }
1393 :
1394 : static GEN
1395 825 : FlxqX_halfgcd_all_i(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *pa, GEN *pb)
1396 : {
1397 : GEN a, b;
1398 825 : GEN R = FlxqX_halfres_i(x, y, T, p, pi, &a, &b, NULL);
1399 825 : if (pa) *pa = a;
1400 825 : if (pb) *pb = b;
1401 825 : return R;
1402 : }
1403 :
1404 : /* Return M in GL_2(Fp[X]/(T)[Y]) such that:
1405 : if [a',b']~=M*[a,b]~ then degpol(a')>= (lgpol(a)>>1) >degpol(b')
1406 : */
1407 :
1408 : GEN
1409 825 : FlxqX_halfgcd_all_pre(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b)
1410 : {
1411 825 : pari_sp av = avma;
1412 : GEN R,q,r;
1413 825 : if (!signe(x))
1414 : {
1415 0 : if (a) *a = RgX_copy(y);
1416 0 : if (b) *b = RgX_copy(x);
1417 0 : return matJ2_FlxXM(varn(x),get_Flx_var(T));
1418 : }
1419 825 : if (degpol(y)<degpol(x)) return FlxqX_halfgcd_all_i(x, y, T, p, pi, a, b);
1420 186 : q = FlxqX_divrem_pre(y, x, T, p, pi, &r);
1421 186 : R = FlxqX_halfgcd_all_i(x, r, T, p, pi, a, b);
1422 186 : gcoeff(R,1,1) = FlxX_sub(gcoeff(R,1,1),
1423 186 : FlxqX_mul_pre(q, gcoeff(R,1,2), T, p, pi), p);
1424 186 : gcoeff(R,2,1) = FlxX_sub(gcoeff(R,2,1),
1425 186 : FlxqX_mul_pre(q, gcoeff(R,2,2), T, p, pi), p);
1426 186 : return !a && b ? gc_all(av, 2, &R, b): gc_all(av, 1+!!a+!!b, &R, a, b);
1427 : }
1428 : GEN
1429 7 : FlxqX_halfgcd_all(GEN x, GEN y, GEN T, ulong p, GEN *a, GEN *b)
1430 7 : { return FlxqX_halfgcd_all_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p), a, b); }
1431 :
1432 : GEN
1433 301 : FlxqX_halfgcd_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
1434 301 : { return FlxqX_halfgcd_all_pre(x, y, T, p, pi, NULL, NULL); }
1435 :
1436 : GEN
1437 0 : FlxqX_halfgcd(GEN x, GEN y, GEN T, ulong p)
1438 0 : { return FlxqX_halfgcd_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1439 :
1440 : static GEN
1441 166767 : FlxqX_gcd_basecase(GEN a, GEN b, GEN T, ulong p, ulong pi)
1442 : {
1443 166767 : pari_sp av = avma, av0=avma;
1444 1075699 : while (signe(b))
1445 : {
1446 : GEN c;
1447 908932 : if (gc_needed(av0,2))
1448 : {
1449 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_gcd (d = %ld)",degpol(b));
1450 0 : gerepileall(av0,2, &a,&b);
1451 : }
1452 908932 : av = avma; c = FlxqX_rem_pre(a, b, T, p, pi); a=b; b=c;
1453 : }
1454 166767 : return gc_const(av, a);
1455 : }
1456 :
1457 : GEN
1458 175909 : FlxqX_gcd_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
1459 : {
1460 175909 : pari_sp av = avma;
1461 175909 : x = FlxqX_red_pre(x, T, p, pi);
1462 175909 : y = FlxqX_red_pre(y, T, p, pi);
1463 175909 : if (!signe(x)) return gerepileupto(av, y);
1464 167145 : while (lgpol(y)>=FlxqX_GCD_LIMIT)
1465 : {
1466 378 : if (lgpol(y)<=(lgpol(x)>>1))
1467 : {
1468 0 : GEN r = FlxqX_rem_pre(x, y, T, p, pi);
1469 0 : x = y; y = r;
1470 : }
1471 378 : (void) FlxqX_halfgcd_all_pre(x,y, T, p, pi, &x, &y);
1472 378 : if (gc_needed(av,2))
1473 : {
1474 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_gcd (y = %ld)",degpol(y));
1475 0 : gerepileall(av,2,&x,&y);
1476 : }
1477 : }
1478 166767 : return gerepileupto(av, FlxqX_gcd_basecase(x, y, T, p, pi));
1479 : }
1480 : GEN
1481 23436 : FlxqX_gcd(GEN x, GEN y, GEN T, ulong p)
1482 23436 : { return FlxqX_gcd_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1483 :
1484 : static GEN
1485 133639 : FlxqX_extgcd_basecase(GEN a, GEN b, GEN T, ulong p,ulong pi, GEN *ptu, GEN *ptv)
1486 : {
1487 133639 : pari_sp av=avma;
1488 : GEN u,v,d,d1,v1;
1489 133639 : long vx = varn(a);
1490 133639 : d = a; d1 = b;
1491 133639 : v = pol_0(vx); v1 = pol1_FlxX(vx, get_Flx_var(T));
1492 470286 : while (signe(d1))
1493 : {
1494 336648 : GEN r, q = FlxqX_divrem_pre(d, d1, T, p, pi, &r);
1495 336648 : v = FlxX_sub(v, FlxqX_mul_pre(q,v1,T, p, pi),p);
1496 336627 : u=v; v=v1; v1=u;
1497 336627 : u=r; d=d1; d1=u;
1498 336627 : if (gc_needed(av,2))
1499 : {
1500 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_extgcd (d = %ld)",degpol(d));
1501 0 : gerepileall(av,5, &d,&d1,&u,&v,&v1);
1502 : }
1503 : }
1504 133638 : if (ptu)
1505 133624 : *ptu = FlxqX_div_pre(FlxX_sub(d,FlxqX_mul_pre(b,v, T,p,pi), p), a, T,p,pi);
1506 133638 : *ptv = v; return d;
1507 : }
1508 :
1509 : static GEN
1510 130 : FlxqX_extgcd_halfgcd(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *ptu, GEN *ptv)
1511 : {
1512 : GEN u,v;
1513 130 : GEN V = cgetg(expu(lgpol(y))+2,t_VEC);
1514 130 : long i, n = 0, vs = varn(x), vT = get_Flx_var(T);
1515 269 : while (lgpol(y) >= FlxqX_EXTGCD_LIMIT)
1516 : {
1517 139 : if (lgpol(y)<=(lgpol(x)>>1))
1518 : {
1519 0 : GEN r, q = FlxqX_divrem_pre(x, y, T, p, pi, &r);
1520 0 : x = y; y = r;
1521 0 : gel(V,++n) = mkmat22(pol_0(vs),pol1_FlxX(vs,vT),pol1_FlxX(vs,vT),FlxX_neg(q,p));
1522 : } else
1523 139 : gel(V,++n) = FlxqX_halfgcd_all_pre(x, y, T, p, pi, &x, &y);
1524 : }
1525 130 : y = FlxqX_extgcd_basecase(x,y, T, p, pi, &u,&v);
1526 139 : for (i = n; i>1; i--)
1527 : {
1528 9 : GEN R = gel(V,i);
1529 9 : GEN u1 = FlxqX_addmulmul(u, v, gcoeff(R,1,1), gcoeff(R,2,1), T, p, pi);
1530 9 : GEN v1 = FlxqX_addmulmul(u, v, gcoeff(R,1,2), gcoeff(R,2,2), T, p, pi);
1531 9 : u = u1; v = v1;
1532 : }
1533 : {
1534 130 : GEN R = gel(V,1);
1535 130 : if (ptu)
1536 130 : *ptu = FlxqX_addmulmul(u, v, gcoeff(R,1,1), gcoeff(R,2,1), T, p, pi);
1537 130 : *ptv = FlxqX_addmulmul(u, v, gcoeff(R,1,2), gcoeff(R,2,2), T, p, pi);
1538 : }
1539 130 : return y;
1540 : }
1541 :
1542 : /* x and y in Z[Y][X], return lift(gcd(x mod T,p, y mod T,p)). Set u and v st
1543 : * ux + vy = gcd (mod T,p) */
1544 : GEN
1545 133639 : FlxqX_extgcd_pre(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *ptu, GEN *ptv)
1546 : {
1547 133639 : pari_sp av = avma;
1548 : GEN d;
1549 133639 : x = FlxqX_red_pre(x, T, p, pi);
1550 133639 : y = FlxqX_red_pre(y, T, p, pi);
1551 133639 : if (lgpol(y)>=FlxqX_EXTGCD_LIMIT)
1552 130 : d = FlxqX_extgcd_halfgcd(x, y, T, p, pi, ptu, ptv);
1553 : else
1554 133509 : d = FlxqX_extgcd_basecase(x, y, T, p, pi, ptu, ptv);
1555 133638 : return gc_all(av, ptu?3:2, &d, ptv, ptu);
1556 : }
1557 : GEN
1558 133624 : FlxqX_extgcd(GEN x, GEN y, GEN T, ulong p, GEN *ptu, GEN *ptv)
1559 : {
1560 133624 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1561 133624 : return FlxqX_extgcd_pre(x, y, T, p, pi, ptu, ptv);
1562 : }
1563 :
1564 : static GEN
1565 354309 : FlxqX_saferem(GEN P, GEN Q, GEN T, ulong p, ulong pi)
1566 : {
1567 354309 : GEN U = Flxq_invsafe_pre(leading_coeff(Q), T, p, pi);
1568 354343 : if (!U) return NULL;
1569 354343 : Q = FlxqX_Flxq_mul_to_monic_pre(Q,U,T,p,pi);
1570 354349 : return FlxqX_rem_pre(P,Q,T,p,pi);
1571 : }
1572 :
1573 : GEN
1574 0 : FlxqX_eval(GEN x, GEN y, GEN T, ulong p)
1575 : {
1576 : pari_sp av;
1577 : GEN p1, r;
1578 0 : long j, i=lg(x)-1;
1579 0 : if (i<=2)
1580 0 : return (i==2)? gcopy(gel(x,2)): pol0_Flx(get_Flx_var(T));
1581 0 : av=avma; p1=gel(x,i);
1582 : /* specific attention to sparse polynomials (see poleval)*/
1583 : /*You've guessed it! It's a copy-paste(tm)*/
1584 0 : for (i--; i>=2; i=j-1)
1585 : {
1586 0 : for (j=i; lg(gel(x,j))==1; j--)
1587 0 : if (j==2)
1588 : {
1589 0 : if (i!=j) y = Flxq_powu(y, i-j+1, T, p);
1590 0 : return gerepileupto(av, Flxq_mul(p1,y, T, p));
1591 : }
1592 0 : r = (i==j)? y: Flxq_powu(y, i-j+1, T, p);
1593 0 : p1 = Flx_add(Flxq_mul(p1,r,T,p), gel(x,j), p);
1594 : }
1595 0 : return gerepileupto(av, p1);
1596 : }
1597 :
1598 : GEN
1599 88430 : FlxqX_safegcd(GEN P, GEN Q, GEN T, ulong p)
1600 : {
1601 88430 : pari_sp av = avma;
1602 : ulong pi;
1603 : GEN U;
1604 88430 : if (!signe(P)) return gcopy(Q);
1605 88430 : if (!signe(Q)) return gcopy(P);
1606 88430 : pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1607 88430 : T = Flx_get_red_pre(T,p,pi);
1608 : for(;;)
1609 : {
1610 308158 : P = FlxqX_saferem(P,Q,T,p,pi);
1611 308158 : if (!P) return gc_NULL(av);
1612 308159 : if (!signe(P)) break;
1613 219728 : if (gc_needed(av, 1))
1614 : {
1615 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_safegcd");
1616 0 : gerepileall(av, 2, &P,&Q);
1617 : }
1618 219728 : swap(P, Q);
1619 : }
1620 88431 : U = Flxq_invsafe_pre(leading_coeff(Q), T, p, pi);
1621 88431 : if (!U) return gc_NULL(av);
1622 88431 : Q = FlxqX_Flxq_mul_to_monic_pre(Q,U,T,p,pi);
1623 88431 : return gerepileupto(av, Q);
1624 : }
1625 :
1626 : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
1627 : GEN
1628 8616 : FlxqX_saferesultant(GEN a, GEN b, GEN T, ulong p)
1629 : {
1630 8616 : long vT = get_Flx_var(T), da,db,dc;
1631 : ulong pi;
1632 : pari_sp av;
1633 8616 : GEN c,lb, res = pol1_Flx(vT);
1634 :
1635 8616 : if (!signe(a) || !signe(b)) return pol0_Flx(vT);
1636 :
1637 8616 : da = degpol(a);
1638 8616 : db = degpol(b);
1639 8616 : if (db > da)
1640 : {
1641 0 : swapspec(a,b, da,db);
1642 0 : if (both_odd(da,db)) res = Flx_neg(res, p);
1643 : }
1644 8616 : if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
1645 8616 : pi = SMALL_ULONG(p)? 0: get_Fl_red(p); av = avma;
1646 54740 : while (db)
1647 : {
1648 46150 : lb = gel(b,db+2);
1649 46150 : c = FlxqX_saferem(a,b, T,p,pi);
1650 46218 : if (!c) return gc_NULL(av);
1651 46218 : a = b; b = c; dc = degpol(c);
1652 46218 : if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
1653 :
1654 46204 : if (both_odd(da,db)) res = Flx_neg(res, p);
1655 46201 : if (!Flx_equal1(lb))
1656 46140 : res = Flxq_mul_pre(res, Flxq_powu_pre(lb, da - dc, T, p, pi), T, p, pi);
1657 46124 : if (gc_needed(av,2))
1658 : {
1659 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
1660 0 : gerepileall(av,3, &a,&b,&res);
1661 : }
1662 46124 : da = db; /* = degpol(a) */
1663 46124 : db = dc; /* = degpol(b) */
1664 : }
1665 8590 : res = Flxq_mul_pre(res, Flxq_powu_pre(gel(b,2), da, T, p, pi), T, p, pi);
1666 8602 : return gerepileupto(av, res);
1667 : }
1668 :
1669 : static GEN
1670 0 : FlxqX_halfres(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, GEN *r)
1671 : {
1672 : struct FlxqX_res res;
1673 : GEN V;
1674 : long dB;
1675 :
1676 0 : res.res = *r;
1677 0 : res.lc = leading_coeff(y);
1678 0 : res.deg0 = degpol(x);
1679 0 : res.deg1 = degpol(y);
1680 0 : res.off = 0;
1681 0 : V = FlxqX_halfres_i(x, y, T, p, pi, a, b, &res);
1682 0 : dB = degpol(*b);
1683 0 : if (dB < degpol(y))
1684 0 : FlxqX_halfres_update(res.deg0, res.deg1, dB, T, p, pi, &res);
1685 0 : *r = res.res;
1686 0 : return V;
1687 : }
1688 :
1689 : static GEN
1690 56 : FlxqX_resultant_basecase(GEN a, GEN b, GEN T, ulong p, ulong pi)
1691 : {
1692 56 : pari_sp av = avma;
1693 56 : long vT = get_Flx_var(T), da,db,dc;
1694 56 : GEN c,lb, res = pol1_Flx(vT);
1695 :
1696 56 : if (!signe(a) || !signe(b)) return pol0_Flx(vT);
1697 :
1698 56 : da = degpol(a);
1699 56 : db = degpol(b);
1700 56 : if (db > da)
1701 : {
1702 0 : swapspec(a,b, da,db);
1703 0 : if (both_odd(da,db)) res = Flx_neg(res, p);
1704 : }
1705 56 : if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
1706 147 : while (db)
1707 : {
1708 91 : lb = gel(b,db+2);
1709 91 : c = FlxqX_rem_pre(a,b, T,p,pi);
1710 91 : a = b; b = c; dc = degpol(c);
1711 91 : if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
1712 :
1713 91 : if (both_odd(da,db)) res = Flx_neg(res, p);
1714 91 : if (!Flx_equal1(lb))
1715 63 : res = Flxq_mul_pre(res, Flxq_powu_pre(lb, da - dc, T,p,pi), T,p,pi);
1716 91 : if (gc_needed(av,2))
1717 : {
1718 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
1719 0 : gerepileall(av,3, &a,&b,&res);
1720 : }
1721 91 : da = db; /* = degpol(a) */
1722 91 : db = dc; /* = degpol(b) */
1723 : }
1724 56 : res = Flxq_mul_pre(res, Flxq_powu_pre(gel(b,2), da, T,p,pi), T,p,pi);
1725 56 : return gerepileupto(av, res);
1726 : }
1727 :
1728 : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
1729 : GEN
1730 56 : FlxqX_resultant_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
1731 : {
1732 56 : pari_sp av = avma;
1733 56 : long dx, dy, vT = get_Flx_var(T);
1734 56 : GEN res = pol1_Flx(vT);
1735 56 : if (!signe(x) || !signe(y)) return pol0_Flx(vT);
1736 56 : dx = degpol(x); dy = degpol(y);
1737 56 : if (dx < dy)
1738 : {
1739 21 : swap(x,y);
1740 21 : if (both_odd(dx, dy))
1741 0 : res = Flx_neg(res, p);
1742 : }
1743 56 : while (lgpol(y) >= FlxqX_GCD_LIMIT)
1744 : {
1745 0 : if (lgpol(y)<=(lgpol(x)>>1))
1746 : {
1747 0 : GEN r = FlxqX_rem_pre(x, y, T, p, pi);
1748 0 : long dx = degpol(x), dy = degpol(y), dr = degpol(r);
1749 0 : GEN ly = gel(y,dy+2);
1750 0 : if (!Flx_equal1(ly))
1751 0 : res = Flxq_mul_pre(res, Flxq_powu_pre(ly, dx - dr, T, p, pi), T, p, pi);
1752 0 : if (both_odd(dx, dy))
1753 0 : res = Flx_neg(res, p);
1754 0 : x = y; y = r;
1755 : }
1756 0 : (void) FlxqX_halfres(x, y, T, p, pi, &x, &y, &res);
1757 0 : if (gc_needed(av,2))
1758 : {
1759 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (y = %ld)",degpol(y));
1760 0 : gerepileall(av,3,&x,&y,&res);
1761 : }
1762 : }
1763 56 : res = Flxq_mul_pre(res, FlxqX_resultant_basecase(x, y, T, p, pi), T, p, pi);
1764 56 : return gerepileupto(av, res);
1765 : }
1766 : GEN
1767 56 : FlxqX_resultant(GEN x, GEN y, GEN T, ulong p)
1768 56 : { return FlxqX_resultant_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1769 :
1770 : /* disc P = (-1)^(n(n-1)/2) lc(P)^(n - deg P' - 2) Res(P,P'), n = deg P */
1771 : GEN
1772 14 : FlxqX_disc(GEN P, GEN T, ulong p)
1773 : {
1774 14 : pari_sp av = avma;
1775 14 : GEN L, dP = FlxX_deriv(P, p), D = FlxqX_resultant(P, dP, T, p);
1776 : long dd;
1777 14 : if (!lgpol(D)) return pol0_Flx(get_Flx_var(T));
1778 14 : dd = degpol(P) - 2 - degpol(dP); /* >= -1; > -1 iff p | deg(P) */
1779 14 : L = leading_coeff(P);
1780 14 : if (dd && !Flx_equal1(L))
1781 : {
1782 0 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1783 0 : D = (dd == -1)? Flxq_div_pre(D,L,T,p,pi)
1784 0 : : Flxq_mul_pre(D, Flxq_powu_pre(L, dd, T,p,pi), T,p,pi); }
1785 14 : if (degpol(P) & 2) D = Flx_neg(D, p);
1786 14 : return gerepileupto(av, D);
1787 : }
1788 :
1789 : INLINE GEN
1790 5975 : FlxXn_recip(GEN x, long n, long v)
1791 5975 : { return FlxX_recipspec(x+2, minss(lgpol(x), n), n, v); }
1792 :
1793 : GEN
1794 2390 : FlxqX_Newton_pre(GEN P, long n, GEN T, ulong p, ulong pi)
1795 : {
1796 2390 : pari_sp av = avma;
1797 2390 : long d = degpol(P), vT = get_Flx_var(T);
1798 2390 : GEN dP = FlxXn_recip(FlxX_deriv(P, p), d, vT);
1799 2390 : GEN Q = FlxqXn_mul_pre(FlxqXn_inv_pre(FlxXn_recip(P, d+1, vT), n, T,p,pi),
1800 : dP, n, T, p, pi);
1801 2390 : return gerepilecopy(av, Q);
1802 : }
1803 : GEN
1804 0 : FlxqX_Newton(GEN P, long n, GEN T, ulong p)
1805 0 : { return FlxqX_Newton_pre(P, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1806 :
1807 : GEN
1808 1195 : FlxqX_fromNewton_pre(GEN P, GEN T, ulong p, ulong pi)
1809 : {
1810 1195 : pari_sp av = avma;
1811 1195 : long vT = get_Flx_var(T);
1812 1195 : long n = Flx_constant(constant_coeff(P))+1;
1813 1195 : GEN z = FlxX_neg(FlxX_shift(P, -1, vT), p);
1814 1195 : GEN Q = FlxXn_recip(FlxqXn_expint_pre(z, n, T, p, pi), n, vT);
1815 1195 : return gerepilecopy(av, Q);
1816 : }
1817 : GEN
1818 0 : FlxqX_fromNewton(GEN P, GEN T, ulong p)
1819 0 : { return FlxqX_fromNewton_pre(P, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1820 :
1821 : GEN
1822 1195 : FlxqX_composedsum(GEN P, GEN Q, GEN T, ulong p)
1823 : {
1824 1195 : pari_sp av = avma;
1825 1195 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1826 1195 : long n = 1+ degpol(P)*degpol(Q);
1827 1195 : GEN Pl = FlxX_invLaplace(FlxqX_Newton_pre(P,n, T,p,pi), p);
1828 1195 : GEN Ql = FlxX_invLaplace(FlxqX_Newton_pre(Q,n, T,p,pi), p);
1829 1195 : GEN L = FlxX_Laplace(FlxqXn_mul_pre(Pl, Ql, n, T,p,pi), p);
1830 1195 : GEN R = FlxqX_fromNewton_pre(L, T, p, pi);
1831 1195 : GEN lead = Flxq_mul_pre(Flxq_powu_pre(leading_coeff(P),degpol(Q), T,p,pi),
1832 1195 : Flxq_powu_pre(leading_coeff(Q),degpol(P), T,p,pi),
1833 : T, p, pi);
1834 1195 : return gerepileupto(av, FlxqX_Flxq_mul_pre(R, lead, T, p, pi));
1835 : }
1836 :
1837 : GEN
1838 223087 : FlxqXV_prod(GEN V, GEN T, ulong p)
1839 : {
1840 223087 : struct _FlxqX d; d.p=p; d.T=T; d.pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1841 223087 : return gen_product(V, (void*)&d, &_FlxqX_mul);
1842 : }
1843 :
1844 : static GEN
1845 32145 : FlxqV_roots_to_deg1(GEN x, GEN T, ulong p, long v)
1846 : {
1847 32145 : long sv = get_Flx_var(T);
1848 124198 : pari_APPLY_same(deg1pol_shallow(pol1_Flx(sv),Flx_neg(gel(x,i),p),v))
1849 : }
1850 :
1851 : GEN
1852 32145 : FlxqV_roots_to_pol(GEN V, GEN T, ulong p, long v)
1853 : {
1854 32145 : pari_sp ltop = avma;
1855 32145 : GEN W = FlxqV_roots_to_deg1(V, T, p, v);
1856 32145 : return gerepileupto(ltop, FlxqXV_prod(W, T, p));
1857 : }
1858 :
1859 : /*******************************************************************/
1860 : /* */
1861 : /* (Fl[X]/T(X))[Y] / S(Y) */
1862 : /* */
1863 : /*******************************************************************/
1864 :
1865 : GEN
1866 470016 : FlxqXQ_mul_pre(GEN x, GEN y, GEN S, GEN T, ulong p, ulong pi)
1867 470016 : { return FlxqX_rem_pre(FlxqX_mul_pre(x,y,T,p,pi),S,T,p,pi); }
1868 : GEN
1869 1947 : FlxqXQ_mul(GEN x, GEN y, GEN S, GEN T, ulong p)
1870 1947 : { return FlxqXQ_mul_pre(x, y, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1871 :
1872 : GEN
1873 263821 : FlxqXQ_sqr_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
1874 263821 : { return FlxqX_rem_pre(FlxqX_sqr_pre(x,T,p,pi),S,T,p,pi); }
1875 : GEN
1876 0 : FlxqXQ_sqr(GEN x, GEN S, GEN T, ulong p)
1877 0 : { return FlxqXQ_sqr_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1878 :
1879 : GEN
1880 14 : FlxqXQ_invsafe_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
1881 : {
1882 14 : GEN V, z = FlxqX_extgcd_pre(get_FlxqX_mod(S), x, T, p, pi, NULL, &V);
1883 14 : if (degpol(z)) return NULL;
1884 14 : z = Flxq_invsafe_pre(gel(z,2),T,p,pi);
1885 14 : if (!z) return NULL;
1886 14 : return FlxqX_Flxq_mul_pre(V, z, T, p, pi);
1887 : }
1888 : GEN
1889 0 : FlxqXQ_invsafe(GEN x, GEN S, GEN T, ulong p)
1890 0 : { return FlxqXQ_invsafe_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1891 :
1892 : GEN
1893 14 : FlxqXQ_inv_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
1894 : {
1895 14 : pari_sp av = avma;
1896 14 : GEN U = FlxqXQ_invsafe_pre(x, S, T, p, pi);
1897 14 : if (!U) pari_err_INV("FlxqXQ_inv",x);
1898 14 : return gerepileupto(av, U);
1899 : }
1900 : GEN
1901 14 : FlxqXQ_inv(GEN x, GEN S, GEN T,ulong p)
1902 14 : { return FlxqXQ_inv_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1903 :
1904 : GEN
1905 0 : FlxqXQ_div_pre(GEN x, GEN y, GEN S, GEN T, ulong p, ulong pi)
1906 0 : { return FlxqXQ_mul_pre(x, FlxqXQ_inv_pre(y,S,T,p,pi),S,T,p,pi); }
1907 : GEN
1908 0 : FlxqXQ_div(GEN x, GEN y, GEN S, GEN T, ulong p)
1909 0 : { return FlxqXQ_div_pre(x, y, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1910 :
1911 : struct _FlxqXQ {
1912 : GEN T, S;
1913 : ulong p, pi;
1914 : };
1915 : static GEN
1916 0 : _FlxqXQ_add(void *data, GEN x, GEN y) {
1917 0 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1918 0 : return FlxX_add(x,y, d->p);
1919 : }
1920 : static GEN
1921 5526 : _FlxqXQ_sub(void *data, GEN x, GEN y) {
1922 5526 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1923 5526 : return FlxX_sub(x,y, d->p);
1924 : }
1925 : #if 0
1926 : static GEN
1927 : _FlxqXQ_cmul(void *data, GEN P, long a, GEN x) {
1928 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1929 : return FlxX_Flx_mul(x,gel(P,a+2), d->p);
1930 : }
1931 : #endif
1932 : static GEN
1933 6044 : _FlxqXQ_red(void *data, GEN x) {
1934 6044 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1935 6044 : return FlxqX_red_pre(x, d->T, d->p, d->pi);
1936 : }
1937 : static GEN
1938 180030 : _FlxqXQ_mul(void *data, GEN x, GEN y) {
1939 180030 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1940 180030 : return FlxqXQ_mul_pre(x,y, d->S,d->T, d->p, d->pi);
1941 : }
1942 : static GEN
1943 263380 : _FlxqXQ_sqr(void *data, GEN x) {
1944 263380 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1945 263380 : return FlxqXQ_sqr_pre(x, d->S,d->T, d->p, d->pi);
1946 : }
1947 :
1948 : static GEN
1949 111734 : _FlxqXQ_one(void *data) {
1950 111734 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1951 111734 : return pol1_FlxX(get_FlxqX_var(d->S),get_Flx_var(d->T));
1952 : }
1953 :
1954 : static GEN
1955 316 : _FlxqXQ_zero(void *data) {
1956 316 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1957 316 : return pol_0(get_FlxqX_var(d->S));
1958 : }
1959 :
1960 : static struct bb_algebra FlxqXQ_algebra = { _FlxqXQ_red, _FlxqXQ_add,
1961 : _FlxqXQ_sub, _FlxqXQ_mul, _FlxqXQ_sqr, _FlxqXQ_one, _FlxqXQ_zero };
1962 :
1963 : const struct bb_algebra *
1964 761 : get_FlxqXQ_algebra(void **E, GEN S, GEN T, ulong p)
1965 : {
1966 761 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1967 761 : GEN z = new_chunk(sizeof(struct _FlxqXQ));
1968 761 : struct _FlxqXQ *e = (struct _FlxqXQ *) z;
1969 761 : e->T = Flx_get_red_pre(T, p, pi);
1970 761 : e->S = FlxqX_get_red_pre(S, e->T, p, pi);
1971 761 : e->p = p;
1972 761 : e->pi= pi; *E = (void*)e;
1973 761 : return &FlxqXQ_algebra;
1974 : }
1975 :
1976 : /* x over Fq, return lift(x^n) mod S */
1977 : GEN
1978 91 : FlxqXQ_pow_pre(GEN x, GEN n, GEN S, GEN T, ulong p, ulong pi)
1979 : {
1980 91 : pari_sp av = avma;
1981 : struct _FlxqXQ D;
1982 91 : long s = signe(n);
1983 91 : if (!s) return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
1984 91 : if (s < 0) x = FlxqXQ_inv_pre(x,S,T,p,pi);
1985 91 : if (is_pm1(n)) return s < 0 ? x : gcopy(x);
1986 91 : if (degpol(x) >= get_FlxqX_degree(S)) x = FlxqX_rem_pre(x,S,T,p,pi);
1987 91 : T = Flx_get_red_pre(T, p, pi);
1988 91 : S = FlxqX_get_red_pre(S, T, p, pi);
1989 91 : D.S = S; D.T = T; D.p = p; D.pi = pi;
1990 91 : x = gen_pow_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
1991 91 : return gerepilecopy(av, x);
1992 : }
1993 : GEN
1994 35 : FlxqXQ_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
1995 35 : { return FlxqXQ_pow_pre(x, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1996 :
1997 : /* x over Fq, return lift(x^n) mod S */
1998 : GEN
1999 82151 : FlxqXQ_powu_pre(GEN x, ulong n, GEN S, GEN T, ulong p, ulong pi)
2000 : {
2001 82151 : pari_sp av = avma;
2002 : struct _FlxqXQ D;
2003 82151 : switch(n)
2004 : {
2005 0 : case 0: return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
2006 7683 : case 1: return gcopy(x);
2007 441 : case 2: return FlxqXQ_sqr_pre(x, S, T, p, pi);
2008 : }
2009 74027 : T = Flx_get_red_pre(T, p, pi);
2010 74027 : S = FlxqX_get_red_pre(S, T, p, pi);
2011 74027 : D.S = S; D.T = T; D.p = p; D.pi = pi;
2012 74027 : x = gen_powu_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
2013 74027 : return gerepilecopy(av, x);
2014 : }
2015 : GEN
2016 0 : FlxqXQ_powu(GEN x, ulong n, GEN S, GEN T, ulong p)
2017 0 : { return FlxqXQ_powu_pre(x, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2018 :
2019 : GEN
2020 108699 : FlxqXQ_powers_pre(GEN x, long l, GEN S, GEN T, ulong p, ulong pi)
2021 : {
2022 : struct _FlxqXQ D;
2023 108699 : int use_sqr = 2*degpol(x) >= get_FlxqX_degree(S);
2024 108699 : T = Flx_get_red_pre(T, p, pi);
2025 108699 : S = FlxqX_get_red_pre(S, T, p, pi);
2026 108699 : D.S = S; D.T = T; D.p = p; D.pi = pi;
2027 108699 : return gen_powers(x, l, use_sqr, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul,&_FlxqXQ_one);
2028 : }
2029 : GEN
2030 0 : FlxqXQ_powers(GEN x, long l, GEN S, GEN T, ulong p)
2031 0 : { return FlxqXQ_powers_pre(x, l, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2032 :
2033 : /* Let v a linear form, return the linear form z->v(tau*z)
2034 : that is, v*(M_tau) */
2035 : static GEN
2036 602 : FlxqXQ_transmul_init(GEN tau, GEN S, GEN T, ulong p, ulong pi)
2037 : {
2038 : GEN bht;
2039 602 : GEN h, Sp = get_FlxqX_red(S, &h);
2040 602 : long n = degpol(Sp), vS = varn(Sp), vT = get_Flx_var(T);
2041 602 : GEN ft = FlxX_recipspec(Sp+2, n+1, n+1, vT);
2042 602 : GEN bt = FlxX_recipspec(tau+2, lgpol(tau), n, vT);
2043 602 : setvarn(ft, vS); setvarn(bt, vS);
2044 602 : if (h)
2045 0 : bht = FlxqXn_mul_pre(bt, h, n-1, T, p, pi);
2046 : else
2047 : {
2048 602 : GEN bh = FlxqX_div_pre(FlxX_shift(tau, n-1, vT), S, T, p, pi);
2049 602 : bht = FlxX_recipspec(bh+2, lgpol(bh), n-1, vT);
2050 602 : setvarn(bht, vS);
2051 : }
2052 602 : return mkvec3(bt, bht, ft);
2053 : }
2054 :
2055 : static GEN
2056 1265 : FlxqXQ_transmul(GEN tau, GEN a, long n, GEN T, ulong p, ulong pi)
2057 : {
2058 1265 : pari_sp ltop = avma;
2059 : GEN t1, t2, t3, vec;
2060 1265 : GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
2061 1265 : long vT = get_Flx_var(T);
2062 1265 : if (signe(a)==0) return pol_0(varn(a));
2063 1210 : t2 = FlxX_shift(FlxqX_mul_pre(bt, a, T, p, pi),1-n,vT);
2064 1210 : if (signe(bht)==0) return gerepilecopy(ltop, t2);
2065 815 : t1 = FlxX_shift(FlxqX_mul_pre(ft, a, T, p, pi),-n,vT);
2066 815 : t3 = FlxqXn_mul_pre(t1, bht, n-1, T, p, pi);
2067 815 : vec = FlxX_sub(t2, FlxX_shift(t3, 1, vT), p);
2068 815 : return gerepileupto(ltop, vec);
2069 : }
2070 :
2071 : static GEN
2072 301 : polxn_FlxX(long n, long v, long vT)
2073 : {
2074 301 : long i, a = n+2;
2075 301 : GEN p = cgetg(a+1, t_POL);
2076 301 : p[1] = evalsigne(1)|evalvarn(v);
2077 2551 : for (i = 2; i < a; i++) gel(p,i) = pol0_Flx(vT);
2078 301 : gel(p,a) = pol1_Flx(vT); return p;
2079 : }
2080 :
2081 : GEN
2082 220 : FlxqXQ_minpoly_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
2083 : {
2084 220 : pari_sp ltop = avma;
2085 : long vS, vT, n;
2086 : GEN v_x, g, tau;
2087 220 : vS = get_FlxqX_var(S);
2088 220 : vT = get_Flx_var(T);
2089 220 : n = get_FlxqX_degree(S);
2090 220 : g = pol1_FlxX(vS,vT);
2091 220 : tau = pol1_FlxX(vS,vT);
2092 220 : S = FlxqX_get_red_pre(S, T, p, pi);
2093 220 : v_x = FlxqXQ_powers_pre(x, usqrt(2*n), S, T, p, pi);
2094 521 : while(signe(tau) != 0)
2095 : {
2096 : long i, j, m, k1;
2097 : GEN M, v, tr;
2098 : GEN g_prime, c;
2099 301 : if (degpol(g) == n) { tau = pol1_FlxX(vS, vT); g = pol1_FlxX(vS, vT); }
2100 301 : v = random_FlxqX(n, vS, T, p);
2101 301 : tr = FlxqXQ_transmul_init(tau, S, T, p, pi);
2102 301 : v = FlxqXQ_transmul(tr, v, n, T, p, pi);
2103 301 : m = 2*(n-degpol(g));
2104 301 : k1 = usqrt(m);
2105 301 : tr = FlxqXQ_transmul_init(gel(v_x,k1+1), S, T, p, pi);
2106 301 : c = cgetg(m+2,t_POL);
2107 301 : c[1] = evalsigne(1)|evalvarn(vS);
2108 1265 : for (i=0; i<m; i+=k1)
2109 : {
2110 964 : long mj = minss(m-i, k1);
2111 3214 : for (j=0; j<mj; j++)
2112 2250 : gel(c,m+1-(i+j)) = FlxqX_dotproduct(v, gel(v_x,j+1), T, p);
2113 964 : v = FlxqXQ_transmul(tr, v, n, T, p, pi);
2114 : }
2115 301 : c = FlxX_renormalize(c, m+2);
2116 : /* now c contains <v,x^i> , i = 0..m-1 */
2117 301 : M = FlxqX_halfgcd_pre(polxn_FlxX(m, vS, vT), c, T, p, pi);
2118 301 : g_prime = gmael(M, 2, 2);
2119 301 : if (degpol(g_prime) < 1) continue;
2120 260 : g = FlxqX_mul_pre(g, g_prime, T, p, pi);
2121 260 : tau = FlxqXQ_mul_pre(tau, FlxqX_FlxqXQV_eval_pre(g_prime, v_x, S, T,p,pi),
2122 : S, T, p,pi);
2123 : }
2124 220 : g = FlxqX_normalize_pre(g,T,p,pi);
2125 220 : return gerepilecopy(ltop,g);
2126 : }
2127 : GEN
2128 62 : FlxqXQ_minpoly(GEN x, GEN S, GEN T, ulong p)
2129 62 : { return FlxqXQ_minpoly_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2130 :
2131 : GEN
2132 0 : FlxqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, ulong p)
2133 0 : { return FlxXV_to_FlxM(FlxqXQ_powers(y,m-1,S,T,p), n, get_Flx_var(T)); }
2134 :
2135 : static GEN
2136 139268 : FlxX_blocks_FlxM(GEN P, long n, long m, long v)
2137 : {
2138 139268 : GEN z = cgetg(m+1,t_MAT);
2139 139268 : long i,j, k=2, l = lg(P);
2140 535812 : for(i=1; i<=m; i++)
2141 : {
2142 396544 : GEN zi = cgetg(n+1,t_COL);
2143 396544 : gel(z,i) = zi;
2144 1270224 : for(j=1; j<=n; j++)
2145 873680 : gel(zi, j) = k==l ? pol0_Flx(v) : gel(P,k++);
2146 : }
2147 139268 : return z;
2148 : }
2149 :
2150 : GEN
2151 139268 : FlxqX_FlxqXQV_eval_pre(GEN Q, GEN x, GEN S, GEN T, ulong p, ulong pi)
2152 : {
2153 139268 : pari_sp btop, av = avma;
2154 139268 : long v = get_FlxqX_var(S), m = get_FlxqX_degree(S);
2155 139268 : long vT = get_Flx_var(T);
2156 139268 : long i, l = lg(x)-1, lQ = lgpol(Q), n, d;
2157 : GEN A, B, C, R, g;
2158 139268 : if (lQ == 0) return pol_0(v);
2159 139268 : if (lQ <= l)
2160 : {
2161 45287 : n = l;
2162 45287 : d = 1;
2163 : }
2164 : else
2165 : {
2166 93981 : n = l-1;
2167 93981 : d = (lQ+n-1)/n;
2168 : }
2169 139268 : A = FlxXV_to_FlxM_lg(x, m, n, vT);
2170 139268 : B = FlxX_blocks_FlxM(Q, n, d, vT);
2171 139268 : C = gerepileupto(av, FlxqM_mul(A, B, T, p));
2172 139268 : g = gel(x, l);
2173 139268 : T = Flx_get_red_pre(T, p, pi);
2174 139268 : S = FlxqX_get_red_pre(S, T, p, pi);
2175 139268 : btop = avma;
2176 139268 : R = FlxV_to_FlxX(gel(C, d), v);
2177 396544 : for (i = d-1; i>0; i--)
2178 : {
2179 257276 : R = FlxX_add(FlxqXQ_mul_pre(R, g, S, T,p,pi), FlxV_to_FlxX(gel(C,i), v), p);
2180 257276 : if (gc_needed(btop,1))
2181 6 : R = gerepileupto(btop, R);
2182 : }
2183 139268 : return gerepilecopy(av, R);
2184 : }
2185 : GEN
2186 0 : FlxqX_FlxqXQV_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
2187 0 : { return FlxqX_FlxqXQV_eval_pre(Q,x,S,T,p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2188 :
2189 : GEN
2190 76756 : FlxqX_FlxqXQ_eval_pre(GEN Q, GEN x, GEN S, GEN T, ulong p, ulong pi)
2191 : {
2192 76756 : pari_sp av = avma;
2193 : GEN z, V;
2194 76756 : long d = degpol(Q), rtd;
2195 76756 : if (d < 0) return pol_0(get_FlxqX_var(S));
2196 76756 : rtd = (long) sqrt((double)d);
2197 76756 : T = Flx_get_red_pre(T, p, pi);
2198 76756 : S = FlxqX_get_red_pre(S, T, p, pi);
2199 76756 : V = FlxqXQ_powers_pre(x, rtd, S, T, p, pi);
2200 76756 : z = FlxqX_FlxqXQV_eval_pre(Q, V, S, T, p, pi);
2201 76756 : return gerepileupto(av, z);
2202 : }
2203 : GEN
2204 0 : FlxqX_FlxqXQ_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
2205 0 : { return FlxqX_FlxqXQ_eval_pre(Q, x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2206 :
2207 : GEN
2208 0 : FlxqXC_FlxqXQV_eval_pre(GEN x, GEN v, GEN S, GEN T, ulong p, ulong pi)
2209 0 : { pari_APPLY_type(t_COL, FlxqX_FlxqXQV_eval_pre(gel(x,i), v, S, T, p, pi)) }
2210 : GEN
2211 0 : FlxqXC_FlxqXQV_eval(GEN x, GEN v, GEN S, GEN T, ulong p)
2212 0 : { return FlxqXC_FlxqXQV_eval_pre(x, v, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2213 :
2214 : GEN
2215 0 : FlxqXC_FlxqXQ_eval(GEN x, GEN F, GEN S, GEN T, ulong p)
2216 : {
2217 0 : long d = brent_kung_optpow(get_FlxqX_degree(S)-1,lg(x)-1,1);
2218 0 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
2219 0 : GEN Fp = FlxqXQ_powers_pre(F, d, S, T, p, pi);
2220 0 : return FlxqXC_FlxqXQV_eval_pre(x, Fp, S, T, p, pi);
2221 : }
2222 :
2223 : static GEN
2224 72659 : FlxqXQ_autpow_sqr(void * E, GEN x)
2225 : {
2226 72659 : struct _FlxqXQ *D = (struct _FlxqXQ *)E;
2227 72659 : GEN S = D->S, T = D->T;
2228 72659 : ulong p = D->p, pi = D->pi;
2229 72659 : GEN phi = gel(x,1), S1 = gel(x,2);
2230 72659 : long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
2231 72659 : GEN V = Flxq_powers_pre(phi, n, T, p, pi);
2232 72659 : GEN phi2 = Flx_FlxqV_eval_pre(phi, V, T, p, pi);
2233 72659 : GEN Sphi = FlxY_FlxqV_evalx_pre(S1, V, T, p, pi);
2234 72659 : GEN S2 = FlxqX_FlxqXQ_eval_pre(Sphi, S1, S, T, p, pi);
2235 72659 : return mkvec2(phi2, S2);
2236 : }
2237 :
2238 : static GEN
2239 3735 : FlxqXQ_autpow_mul(void * E, GEN x, GEN y)
2240 : {
2241 3735 : struct _FlxqXQ *D = (struct _FlxqXQ *)E;
2242 3735 : GEN S = D->S, T = D->T;
2243 3735 : ulong p = D->p, pi = D->pi;
2244 3735 : GEN phi1 = gel(x,1), S1 = gel(x,2);
2245 3735 : GEN phi2 = gel(y,1), S2 = gel(y,2);
2246 3735 : long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
2247 3735 : GEN V = Flxq_powers_pre(phi2, n, T, p, pi);
2248 3735 : GEN phi3 = Flx_FlxqV_eval_pre(phi1, V, T, p, pi);
2249 3735 : GEN Sphi = FlxY_FlxqV_evalx_pre(S1, V, T, p, pi);
2250 3735 : GEN S3 = FlxqX_FlxqXQ_eval_pre(Sphi, S2, S, T, p, pi);
2251 3735 : return mkvec2(phi3, S3);
2252 : }
2253 :
2254 : GEN
2255 69296 : FlxqXQ_autpow_pre(GEN aut, long n, GEN S, GEN T, ulong p, ulong pi)
2256 : {
2257 69296 : pari_sp av = avma;
2258 : struct _FlxqXQ D;
2259 69296 : T = Flx_get_red_pre(T, p, pi);
2260 69296 : S = FlxqX_get_red_pre(S, T, p, pi);
2261 69296 : D.S = S; D.T = T; D.p = p; D.pi = pi;
2262 69296 : aut = gen_powu_i(aut,n,&D,FlxqXQ_autpow_sqr,FlxqXQ_autpow_mul);
2263 69296 : return gerepilecopy(av, aut);
2264 : }
2265 : GEN
2266 0 : FlxqXQ_autpow(GEN aut, long n, GEN S, GEN T, ulong p)
2267 0 : { return FlxqXQ_autpow_pre(aut, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2268 :
2269 : static GEN
2270 30433 : FlxqXQ_autsum_mul(void *E, GEN x, GEN y)
2271 : {
2272 30433 : struct _FlxqXQ *D = (struct _FlxqXQ *)E;
2273 30433 : GEN S = D->S, T = D->T;
2274 30433 : ulong p = D->p, pi = D->pi;
2275 30433 : GEN phi1 = gel(x,1), S1 = gel(x,2), a1 = gel(x,3);
2276 30433 : GEN phi2 = gel(y,1), S2 = gel(y,2), a2 = gel(y,3);
2277 30433 : long n2 = brent_kung_optpow(get_Flx_degree(T)-1, lgpol(S1)+lgpol(a1)+1,1);
2278 30433 : GEN V2 = Flxq_powers_pre(phi2, n2, T, p, pi);
2279 30433 : GEN phi3 = Flx_FlxqV_eval_pre(phi1, V2, T, p, pi);
2280 30433 : GEN Sphi = FlxY_FlxqV_evalx_pre(S1, V2, T, p, pi);
2281 30433 : GEN aphi = FlxY_FlxqV_evalx_pre(a1, V2, T, p, pi);
2282 30433 : long n = brent_kung_optpow(maxss(degpol(Sphi),degpol(aphi)),2,1);
2283 30433 : GEN V = FlxqXQ_powers_pre(S2, n, S, T, p, pi);
2284 30433 : GEN S3 = FlxqX_FlxqXQV_eval_pre(Sphi, V, S, T, p, pi);
2285 30433 : GEN aS = FlxqX_FlxqXQV_eval_pre(aphi, V, S, T, p, pi);
2286 30433 : GEN a3 = FlxqXQ_mul_pre(aS, a2, S, T, p, pi);
2287 30433 : return mkvec3(phi3, S3, a3);
2288 : }
2289 :
2290 : static GEN
2291 18846 : FlxqXQ_autsum_sqr(void * T, GEN x)
2292 18846 : { return FlxqXQ_autsum_mul(T, x, x); }
2293 :
2294 : GEN
2295 12197 : FlxqXQ_autsum_pre(GEN aut, long n, GEN S, GEN T, ulong p, ulong pi)
2296 : {
2297 12197 : pari_sp av = avma;
2298 : struct _FlxqXQ D;
2299 12197 : T = Flx_get_red_pre(T, p, pi);
2300 12197 : S = FlxqX_get_red_pre(S, T, p, pi);
2301 12197 : D.S=S; D.T=T; D.p=p; D.pi=pi;
2302 12197 : aut = gen_powu_i(aut,n,&D,FlxqXQ_autsum_sqr,FlxqXQ_autsum_mul);
2303 12197 : return gerepilecopy(av, aut);
2304 : }
2305 : GEN
2306 0 : FlxqXQ_autsum(GEN aut, long n, GEN S, GEN T, ulong p)
2307 0 : { return FlxqXQ_autsum_pre(aut, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2308 :
2309 : static GEN
2310 27 : FlxqXQ_auttrace_mul(void *E, GEN x, GEN y)
2311 : {
2312 27 : struct _FlxqXQ *D = (struct _FlxqXQ *)E;
2313 27 : GEN S = D->S, T = D->T;
2314 27 : ulong p = D->p, pi = D->pi;
2315 27 : GEN S1 = gel(x,1), a1 = gel(x,2);
2316 27 : GEN S2 = gel(y,1), a2 = gel(y,2);
2317 27 : long n = brent_kung_optpow(maxss(degpol(S1),degpol(a1)),2,1);
2318 27 : GEN V = FlxqXQ_powers_pre(S2, n, S, T, p, pi);
2319 27 : GEN S3 = FlxqX_FlxqXQV_eval_pre(S1, V, S, T, p, pi);
2320 27 : GEN aS = FlxqX_FlxqXQV_eval_pre(a1, V, S, T, p, pi);
2321 27 : GEN a3 = FlxX_add(aS, a2, p);
2322 27 : return mkvec2(S3, a3);
2323 : }
2324 :
2325 : static GEN
2326 20 : FlxqXQ_auttrace_sqr(void *E, GEN x)
2327 20 : { return FlxqXQ_auttrace_mul(E, x, x); }
2328 :
2329 : GEN
2330 333 : FlxqXQ_auttrace_pre(GEN x, ulong n, GEN S, GEN T, ulong p, ulong pi)
2331 : {
2332 333 : pari_sp av = avma;
2333 : struct _FlxqXQ D;
2334 333 : T = Flx_get_red_pre(T, p, pi);
2335 333 : S = FlxqX_get_red_pre(S, T, p, pi);
2336 333 : D.S=S; D.T=T; D.p=p; D.pi = pi;
2337 333 : x = gen_powu_i(x,n,(void*)&D,FlxqXQ_auttrace_sqr,FlxqXQ_auttrace_mul);
2338 333 : return gerepilecopy(av, x);
2339 : }
2340 : GEN
2341 0 : FlxqXQ_auttrace(GEN x, ulong n, GEN S, GEN T, ulong p)
2342 0 : { return FlxqXQ_auttrace_pre(x, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2343 :
2344 : /*******************************************************************/
2345 : /* */
2346 : /* FlxYqQ */
2347 : /* */
2348 : /*******************************************************************/
2349 :
2350 : /*Preliminary implementation to speed up FpX_ffisom*/
2351 : typedef struct {
2352 : GEN S, T;
2353 : ulong p, pi;
2354 : } FlxYqq_muldata;
2355 :
2356 : /* reduce x in Fl[X, Y] in the algebra Fl[X, Y]/ (P(X),Q(Y)) */
2357 : static GEN
2358 90716 : FlxYqq_redswap(GEN x, GEN S, GEN T, ulong p, ulong pi)
2359 : {
2360 90716 : pari_sp ltop=avma;
2361 90716 : long n = get_Flx_degree(S);
2362 90716 : long m = get_Flx_degree(T);
2363 90716 : long w = get_Flx_var(T);
2364 90716 : GEN V = FlxX_swap(x,m,w);
2365 90716 : V = FlxqX_red_pre(V,S,p,pi);
2366 90710 : V = FlxX_swap(V,n,w);
2367 90713 : return gerepilecopy(ltop,V);
2368 : }
2369 : static GEN
2370 80413 : FlxYqq_sqr(void *data, GEN x)
2371 : {
2372 80413 : FlxYqq_muldata *D = (FlxYqq_muldata*)data;
2373 80413 : return FlxYqq_redswap(FlxqX_sqr_pre(x,D->T,D->p,D->pi),D->S,D->T,D->p,D->pi);
2374 : }
2375 :
2376 : static GEN
2377 10302 : FlxYqq_mul(void *data, GEN x, GEN y)
2378 : {
2379 10302 : FlxYqq_muldata *D = (FlxYqq_muldata*)data;
2380 10302 : return FlxYqq_redswap(FlxqX_mul_pre(x,y, D->T,D->p,D->pi),D->S,D->T,D->p,D->pi);
2381 : }
2382 :
2383 : /* x in Z[X,Y], S in Z[X] over Fq = Z[Y]/(p,T); compute lift(x^n mod (S,T,p)) */
2384 : GEN
2385 39184 : FlxYqq_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
2386 : {
2387 : FlxYqq_muldata D;
2388 39184 : D.S = S; D.T = T; D.p = p; D.pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
2389 39184 : return gen_pow(x, n, (void*)&D, &FlxYqq_sqr, &FlxYqq_mul);
2390 : }
2391 :
2392 : /*******************************************************************/
2393 : /* */
2394 : /* FlxqXn */
2395 : /* */
2396 : /*******************************************************************/
2397 :
2398 : GEN
2399 65473 : FlxXn_red(GEN a, long n)
2400 : {
2401 65473 : long i, L = n+2, l = lg(a);
2402 : GEN b;
2403 65473 : if (L >= l) return a; /* deg(x) < n */
2404 36545 : b = cgetg(L, t_POL); b[1] = a[1];
2405 391502 : for (i=2; i<L; i++) gel(b,i) = gel(a,i);
2406 36545 : return FlxX_renormalize(b,L);
2407 : }
2408 :
2409 : GEN
2410 39933 : FlxqXn_mul_pre(GEN a, GEN b, long n, GEN T, ulong p, ulong pi)
2411 39933 : { return FlxXn_red(FlxqX_mul_pre(a, b, T, p, pi), n); }
2412 : GEN
2413 0 : FlxqXn_mul(GEN a, GEN b, long n, GEN T, ulong p)
2414 0 : { return FlxqXn_mul_pre(a, b, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2415 :
2416 : GEN
2417 0 : FlxqXn_sqr_pre(GEN a, long n, GEN T, ulong p, ulong pi)
2418 0 : { return FlxXn_red(FlxqX_sqr_pre(a, T, p, pi), n); }
2419 : GEN
2420 0 : FlxqXn_sqr(GEN a, long n, GEN T, ulong p)
2421 0 : { return FlxqXn_sqr_pre(a, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2422 :
2423 : /* (f*g) \/ x^n */
2424 : static GEN
2425 17769 : FlxqX_mulhigh_i(GEN f, GEN g, long n, GEN T, ulong p, ulong pi)
2426 17769 : { return FlxX_shift(FlxqX_mul_pre(f, g, T, p, pi), -n , get_Flx_var(T)); }
2427 :
2428 : static GEN
2429 13027 : FlxqXn_mulhigh(GEN f, GEN g, long n2, long n, GEN T, ulong p, ulong pi)
2430 : {
2431 13027 : long vT = get_Flx_var(T);
2432 13027 : GEN F = FlxX_blocks(f, n2, 2, vT), fl = gel(F,1), fh = gel(F,2);
2433 13026 : return FlxX_add(FlxqX_mulhigh_i(fl, g, n2, T, p, pi),
2434 : FlxqXn_mul_pre(fh, g, n - n2, T, p, pi), p);
2435 : }
2436 :
2437 : GEN
2438 2390 : FlxqXn_inv_pre(GEN f, long e, GEN T, ulong p, ulong pi)
2439 : {
2440 2390 : pari_sp av = avma, av2;
2441 : ulong mask;
2442 : GEN W, a;
2443 2390 : long v = varn(f), n = 1, vT = get_Flx_var(T);
2444 :
2445 2390 : if (!signe(f)) pari_err_INV("FlxqXn_inv",f);
2446 2390 : a = Flxq_inv_pre(gel(f,2), T, p, pi);
2447 2390 : if (e == 1) return scalarpol(a, v);
2448 2390 : else if (e == 2)
2449 : {
2450 : GEN b;
2451 0 : if (degpol(f) <= 0) return scalarpol(a, v);
2452 0 : b = Flx_neg(gel(f,3), p);
2453 0 : if (lgpol(b)==0) return scalarpol(a, v);
2454 0 : b = Flxq_mul_pre(b, Flxq_sqr_pre(a, T, p, pi), T, p, pi);
2455 0 : W = deg1pol_shallow(b, a, v);
2456 0 : return gerepilecopy(av, W);
2457 : }
2458 2390 : W = scalarpol_shallow(Flxq_inv_pre(gel(f,2), T, p, pi), v);
2459 2390 : mask = quadratic_prec_mask(e);
2460 2390 : av2 = avma;
2461 11872 : for (;mask>1;)
2462 : {
2463 : GEN u, fr;
2464 9482 : long n2 = n;
2465 9482 : n<<=1; if (mask & 1) n--;
2466 9482 : mask >>= 1;
2467 9482 : fr = FlxXn_red(f, n);
2468 9482 : u = FlxqXn_mul_pre(W, FlxqXn_mulhigh(fr, W, n2, n, T,p,pi), n-n2, T,p,pi);
2469 9482 : W = FlxX_sub(W, FlxX_shift(u, n2, vT), p);
2470 9482 : if (gc_needed(av2,2))
2471 : {
2472 0 : if(DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_inv, e = %ld", n);
2473 0 : W = gerepileupto(av2, W);
2474 : }
2475 : }
2476 2390 : return gerepileupto(av, W);
2477 : }
2478 : GEN
2479 0 : FlxqXn_inv(GEN f, long e, GEN T, ulong p)
2480 0 : { return FlxqXn_inv_pre(f, e, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2481 :
2482 : /* Compute intformal(x^n*S)/x^(n+1) */
2483 : static GEN
2484 4741 : FlxX_integXn(GEN x, long n, ulong p)
2485 : {
2486 4741 : long i, lx = lg(x);
2487 : GEN y;
2488 4741 : if (lx == 2) return gcopy(x);
2489 3949 : y = cgetg(lx, t_POL); y[1] = x[1];
2490 26585 : for (i=2; i<lx; i++)
2491 : {
2492 22636 : GEN xi = gel(x,i);
2493 22636 : gel(y,i) = Flx_Fl_mul(xi, Fl_inv((n+i-1)%p, p), p);
2494 : }
2495 3949 : return FlxX_renormalize(y, lx);;
2496 : }
2497 :
2498 : GEN
2499 1195 : FlxqXn_expint_pre(GEN h, long e, GEN T, ulong p, ulong pi)
2500 : {
2501 1195 : pari_sp av = avma, av2;
2502 1195 : long v = varn(h), n = 1, vT = get_Flx_var(T);
2503 1195 : GEN f = pol1_FlxX(v, vT), g = pol1_FlxX(v, vT);
2504 1195 : ulong mask = quadratic_prec_mask(e);
2505 1195 : av2 = avma;
2506 4741 : for (;mask>1;)
2507 : {
2508 : GEN u, w;
2509 4741 : long n2 = n;
2510 4741 : n<<=1; if (mask & 1) n--;
2511 4741 : mask >>= 1;
2512 4741 : u = FlxqXn_mul_pre(g, FlxqX_mulhigh_i(f, FlxXn_red(h, n2-1), n2-1, T,p,pi), n-n2, T,p,pi);
2513 4741 : u = FlxX_add(u, FlxX_shift(FlxXn_red(h, n-1), 1-n2, vT), p);
2514 4741 : w = FlxqXn_mul_pre(f, FlxX_integXn(u, n2-1, p), n-n2, T, p, pi);
2515 4741 : f = FlxX_add(f, FlxX_shift(w, n2, vT), p);
2516 4741 : if (mask<=1) break;
2517 3546 : u = FlxqXn_mul_pre(g, FlxqXn_mulhigh(f, g, n2, n, T,p,pi), n-n2, T,p,pi);
2518 3546 : g = FlxX_sub(g, FlxX_shift(u, n2, vT), p);
2519 3546 : if (gc_needed(av2,2))
2520 : {
2521 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_exp, e = %ld", n);
2522 0 : gerepileall(av2, 2, &f, &g);
2523 : }
2524 : }
2525 1195 : return gerepileupto(av, f);
2526 : }
2527 : GEN
2528 0 : FlxqXn_expint(GEN h, long e, GEN T, ulong p)
2529 0 : { return FlxqXn_expint_pre(h, e, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
|