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 15236240 : FlxX_renormalize(GEN /*in place*/ x, long lx)
31 : {
32 : long i;
33 20044436 : for (i = lx-1; i>1; i--)
34 19266282 : if (lgpol(gel(x,i))) break;
35 15236210 : stackdummy((pari_sp)(x + lg(x)), (pari_sp)(x + i+1));
36 15236781 : setlg(x, i+1); setsigne(x, i!=1); return x;
37 : }
38 :
39 : GEN
40 760924 : pol1_FlxX(long v, long sv)
41 : {
42 760924 : GEN z = cgetg(3, t_POL);
43 760924 : z[1] = evalsigne(1) | evalvarn(v);
44 760924 : gel(z,2) = pol1_Flx(sv); return z;
45 : }
46 :
47 : GEN
48 690449 : polx_FlxX(long v, long sv)
49 : {
50 690449 : GEN z = cgetg(4, t_POL);
51 690449 : z[1] = evalsigne(1) | evalvarn(v);
52 690449 : gel(z,2) = pol0_Flx(sv);
53 690449 : gel(z,3) = pol1_Flx(sv); return z;
54 : }
55 :
56 : long
57 2892062 : FlxY_degreex(GEN b)
58 : {
59 2892062 : long deg = 0, i;
60 2892062 : if (!signe(b)) return -1;
61 10419383 : for (i = 2; i < lg(b); ++i)
62 7527112 : deg = maxss(deg, degpol(gel(b, i)));
63 2892271 : return deg;
64 : }
65 :
66 : /*Lift coefficient of B to constant Flx, to give a FlxY*/
67 : GEN
68 2529 : Fly_to_FlxY(GEN B, long sv)
69 : {
70 2529 : long lb=lg(B);
71 : long i;
72 2529 : GEN b=cgetg(lb,t_POL);
73 2530 : b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
74 65773 : for (i=2; i<lb; i++)
75 63246 : gel(b,i) = Fl_to_Flx(B[i], sv);
76 2527 : return FlxX_renormalize(b, lb);
77 : }
78 :
79 : GEN
80 2687667 : zxX_to_FlxX(GEN B, ulong p)
81 : {
82 2687667 : long i, lb = lg(B);
83 2687667 : GEN b = cgetg(lb,t_POL);
84 8764647 : for (i=2; i<lb; i++)
85 6082148 : gel(b,i) = zx_to_Flx(gel(B,i), p);
86 2682499 : b[1] = B[1]; return FlxX_renormalize(b, lb);
87 : }
88 :
89 : GEN
90 2772679 : FlxX_to_ZXX(GEN B)
91 : {
92 2772679 : long i, lb = lg(B);
93 2772679 : GEN b = cgetg(lb,t_POL);
94 8894476 : for (i=2; i<lb; i++)
95 : {
96 6123027 : GEN c = gel(B,i);
97 6123027 : switch(lgpol(c))
98 : {
99 268222 : case 0: c = gen_0; break;
100 797548 : case 1: c = utoi(c[2]); break;
101 5057310 : default: c = Flx_to_ZX(c); break;
102 : }
103 6121544 : gel(b,i) = c;
104 : }
105 2771449 : 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 2816122 : ZXX_to_FlxX(GEN B, ulong p, long v)
112 : {
113 2816122 : long lb=lg(B);
114 : long i;
115 2816122 : GEN b=cgetg(lb,t_POL);
116 2816174 : b[1]=evalsigne(1)|(((ulong)B[1])&VARNBITS);
117 13776399 : for (i=2; i<lb; i++)
118 10961907 : switch (typ(gel(B,i)))
119 : {
120 2145461 : case t_INT:
121 2145461 : gel(b,i) = Z_to_Flx(gel(B,i), p, evalvarn(v));
122 2145371 : break;
123 8818107 : case t_POL:
124 8818107 : gel(b,i) = ZX_to_Flx(gel(B,i), p);
125 8816515 : break;
126 : }
127 2814492 : 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 323942 : FlxX_to_FlxC(GEN x, long N, long sv)
145 : {
146 : long i, l;
147 : GEN z;
148 323942 : l = lg(x)-1; x++;
149 323942 : if (l > N+1) l = N+1; /* truncate higher degree terms */
150 323942 : z = cgetg(N+1,t_COL);
151 2436342 : for (i=1; i<l ; i++) gel(z,i) = gel(x,i);
152 3478055 : for ( ; i<=N; i++) gel(z,i) = pol0_Flx(sv);
153 323942 : 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 55428 : FlxX_to_Flm(GEN v, long n)
160 : {
161 55428 : long j, N = lg(v)-1;
162 55428 : GEN y = cgetg(N, t_MAT);
163 55428 : v++;
164 190547 : for (j=1; j<N; j++) gel(y,j) = Flx_to_Flv(gel(v,j), n);
165 55426 : return y;
166 : }
167 :
168 : GEN
169 365351 : FlxX_to_Flx(GEN f)
170 : {
171 365351 : long i, l = lg(f);
172 365351 : GEN V = cgetg(l, t_VECSMALL);
173 365351 : V[1] = ((ulong)f[1])&VARNBITS;
174 3504190 : for(i=2; i<l; i++)
175 3138839 : V[i] = lgpol(gel(f,i)) ? mael(f,i,2): 0L;
176 365351 : return V;
177 : }
178 :
179 : GEN
180 221464 : Flm_to_FlxX(GEN x, long v,long w)
181 : {
182 221464 : long j, lx = lg(x);
183 221464 : GEN y = cgetg(lx+1, t_POL);
184 221467 : y[1]=evalsigne(1) | v;
185 221467 : y++;
186 814146 : for (j=1; j<lx; j++) gel(y,j) = Flv_to_Flx(gel(x,j), w);
187 221440 : return FlxX_renormalize(--y, lx+1);
188 : }
189 :
190 : /* P(X,Y) --> P(Y,X), n is the degree in Y */
191 : GEN
192 181477 : FlxX_swap(GEN x, long n, long ws)
193 : {
194 181477 : long j, lx = lg(x), ly = n+3;
195 181477 : GEN y = cgetg(ly, t_POL);
196 181480 : y[1] = x[1];
197 1165738 : for (j=2; j<ly; j++)
198 : {
199 : long k;
200 984272 : GEN p1 = cgetg(lx, t_VECSMALL);
201 984255 : p1[1] = ws;
202 11196017 : for (k=2; k<lx; k++)
203 10211762 : if (j<lg(gel(x,k)))
204 7184558 : p1[k] = mael(x,k,j);
205 : else
206 3027204 : p1[k] = 0;
207 984255 : gel(y,j) = Flx_renormalize(p1,lx);
208 : }
209 181466 : return FlxX_renormalize(y,ly);
210 : }
211 :
212 : static GEN
213 4355807 : zxX_to_Kronecker_spec(GEN P, long lp, long n)
214 : { /* P(X) = sum Pi(Y) * X^i, return P( Y^(2n-1) ) */
215 4355807 : long i, j, k, l, N = (n<<1) + 1;
216 4355807 : GEN y = cgetg((N-2)*lp + 2, t_VECSMALL) + 2;
217 28728572 : for (k=i=0; i<lp; i++)
218 : {
219 28692271 : GEN c = gel(P,i);
220 28692271 : l = lg(c);
221 28692271 : if (l-3 >= n)
222 0 : pari_err_BUG("zxX_to_Kronecker, P is not reduced mod Q");
223 167383425 : for (j=2; j < l; j++) y[k++] = c[j];
224 28692268 : if (i == lp-1) break;
225 217360032 : for ( ; j < N; j++) y[k++] = 0;
226 : }
227 4355798 : y -= 2;
228 4355798 : y[1] = 0; setlg(y, k+2); return y;
229 : }
230 :
231 : GEN
232 3775127 : zxX_to_Kronecker(GEN P, GEN Q)
233 : {
234 3775127 : GEN z = zxX_to_Kronecker_spec(P+2, lg(P)-2, degpol(Q));
235 3775105 : z[1] = P[1]; return z;
236 : }
237 :
238 : GEN
239 346159 : FlxX_add(GEN x, GEN y, ulong p)
240 : {
241 : long i,lz;
242 : GEN z;
243 346159 : long lx=lg(x);
244 346159 : long ly=lg(y);
245 346159 : if (ly>lx) swapspec(x,y, lx,ly);
246 346159 : lz = lx; z = cgetg(lz, t_POL); z[1]=x[1];
247 3902480 : for (i=2; i<ly; i++) gel(z,i) = Flx_add(gel(x,i), gel(y,i), p);
248 2135997 : for ( ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
249 346156 : 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 11729 : FlxX_Flx_sub(GEN y, GEN x, ulong p)
268 : {
269 11729 : long i, lz = lg(y);
270 : GEN z;
271 11729 : if (signe(y) == 0) return scalarpol(x, varn(y));
272 11729 : z = cgetg(lz,t_POL); z[1] = y[1];
273 11729 : gel(z,2) = Flx_sub(gel(y,2), x, p);
274 11729 : if (lz == 3) z = FlxX_renormalize(z,lz);
275 : else
276 84283 : for(i=3;i<lz;i++) gel(z,i) = Flx_copy(gel(y,i));
277 11729 : 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 45331 : for (i=2; i<lx; i++) gel(z,i) = Flx_neg(gel(x,i), p);
287 2635 : 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 82648 : FlxX_deriv(GEN z, ulong p)
322 : {
323 82648 : long i,l = lg(z)-1;
324 : GEN x;
325 82648 : if (l < 2) l = 2;
326 82648 : x = cgetg(l, t_POL); x[1] = z[1];
327 894125 : for (i=2; i<l; i++) gel(x,i) = Flx_mulu(gel(z,i+1), (ulong) i-1, p);
328 82650 : 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 624863 : FlxX_sub(GEN x, GEN y, ulong p)
380 : {
381 : long lx,ly,i,lz;
382 : GEN z;
383 624863 : lx = lg(x); ly = lg(y);
384 624863 : lz=maxss(lx,ly);
385 624866 : z = cgetg(lz,t_POL);
386 624865 : if (lx >= ly)
387 : {
388 110176 : z[1] = x[1];
389 638614 : for (i=2; i<ly; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
390 388184 : for ( ; i<lx; i++) gel(z,i) = Flx_copy(gel(x,i));
391 110176 : if (lx==ly) z = FlxX_renormalize(z, lz);
392 : }
393 : else
394 : {
395 514689 : z[1] = y[1];
396 1301850 : for (i=2; i<lx; i++) gel(z,i) = Flx_sub(gel(x,i),gel(y,i),p);
397 1383124 : for ( ; i<ly; i++) gel(z,i) = Flx_neg(gel(y,i),p);
398 : }
399 624824 : if (!lgpol(z)) { set_avma((pari_sp)(z + lz)); z = pol_0(varn(x)); }
400 624838 : 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 5574221 : FlxY_evalx_pre(GEN Q, ulong x, ulong p, ulong pi)
416 : {
417 5574221 : long i, lb = lg(Q);
418 : GEN z;
419 5574221 : z = cgetg(lb,t_VECSMALL); z[1] = evalvarn(varn(Q));
420 75804127 : for (i=2; i<lb; i++) z[i] = Flx_eval_pre(gel(Q,i), x, p, pi);
421 5576834 : 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 10171419 : FlxY_evalx_powers_pre(GEN pol, GEN ypowers, ulong p, ulong pi)
454 : {
455 10171419 : long i, len = lg(pol);
456 10171419 : GEN res = cgetg(len, t_VECSMALL);
457 10169134 : res[1] = pol[1] & VARNBITS;
458 34503896 : for (i = 2; i < len; ++i)
459 24325402 : res[i] = Flx_eval_powers_pre(gel(pol, i), ypowers, p, pi);
460 10178494 : return Flx_renormalize(res, len);
461 : }
462 :
463 : /* allow pi = 0 */
464 : ulong
465 6826740 : FlxY_eval_powers_pre(GEN pol, GEN ypowers, GEN xpowers, ulong p, ulong pi)
466 : {
467 6826740 : pari_sp av = avma;
468 6826740 : GEN t = FlxY_evalx_powers_pre(pol, ypowers, p, pi);
469 6823147 : return gc_ulong(av, Flx_eval_powers_pre(t, xpowers, p, pi));
470 : }
471 :
472 : GEN
473 137257 : FlxY_FlxqV_evalx_pre(GEN P, GEN x, GEN T, ulong p, ulong pi)
474 : {
475 137257 : long i, lP = lg(P);
476 137257 : GEN res = cgetg(lP,t_POL);
477 137257 : res[1] = P[1];
478 931747 : for(i=2; i<lP; i++)
479 794490 : gel(res,i) = Flx_FlxqV_eval_pre(gel(P,i), x, T, p, pi);
480 137257 : 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 7559 : FlxY_Flx_div(GEN x, GEN y, ulong p)
500 : {
501 : long i, l;
502 : GEN z;
503 7559 : if (degpol(y) == 0)
504 : {
505 5598 : ulong t = uel(y,2);
506 5598 : 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 1961 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
514 1961 : z = cgetg_copy(x, &l); z[1] = x[1];
515 3922 : for (i=2; i<l; i++) gel(z,i) = Flx_div_pre(gel(x,i),y,p,pi);
516 : }
517 1961 : return z;
518 : }
519 :
520 : GEN
521 71728 : FlxX_shift(GEN a, long n, long vs)
522 : {
523 71728 : long i, l = lg(a);
524 : GEN b;
525 71728 : if (l == 2 || !n) return a;
526 64858 : l += n;
527 64858 : if (n < 0)
528 : {
529 35897 : if (l <= 2) return pol_0(varn(a));
530 29665 : b = cgetg(l, t_POL); b[1] = a[1];
531 29664 : a -= n;
532 611742 : for (i=2; i<l; i++) gel(b,i) = gel(a,i);
533 : } else {
534 28961 : b = cgetg(l, t_POL); b[1] = a[1];
535 28964 : a -= n; n += 2;
536 503229 : for (i=2; i<n; i++) gel(b,i) = pol0_Flx(vs);
537 364028 : for ( ; i<l; i++) gel(b,i) = gel(a,i);
538 : }
539 58625 : 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 13027 : long i,j, k=2, l = lg(P);
547 39081 : for(i=1; i<=m; i++)
548 : {
549 26054 : GEN zi = cgetg(n+2,t_POL);
550 26052 : zi[1] = P[1];
551 26052 : gel(z,i) = zi;
552 159374 : for(j=2; j<n+2; j++)
553 133387 : gel(zi, j) = k==l ? pol0_Flx(vs) : gel(P,k++);
554 25987 : zi = FlxX_renormalize(zi, n+2);
555 : }
556 13027 : return z;
557 : }
558 :
559 : static GEN
560 233162 : FlxX_recipspec(GEN x, long l, long n, long vs)
561 : {
562 : long i;
563 233162 : GEN z = cgetg(n+2,t_POL);
564 233162 : z[1] = 0; z += 2;
565 5243011 : for(i=0; i<l; i++)
566 5009849 : gel(z,n-i-1) = Flx_copy(gel(x,i));
567 240624 : for( ; i<n; i++)
568 7462 : gel(z,n-i-1) = pol0_Flx(vs);
569 233162 : 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 47149 : for (i=d; i>=2; i--)
583 : {
584 44766 : gel(y,i+2) = Flx_Fl_mul(gel(x,i+2), t, p);
585 44768 : 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 23655 : for (i=2; i<=d; i++)
604 : {
605 22460 : t = Fl_mul(t, i%p, p);
606 22459 : 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 139284 : FlxXV_to_FlxM_lg(GEN x, long m, long n, long sv)
623 : {
624 : long i;
625 139284 : GEN y = cgetg(n+1, t_MAT);
626 463226 : for (i=1; i<=n; i++) gel(y,i) = FlxX_to_FlxC(gel(x,i), m, sv);
627 139284 : 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 12551 : FlxXC_to_ZXXC(GEN x)
636 62730 : { 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 3593593 : get_FlxqX_red(GEN T, GEN *B)
650 : {
651 3593593 : if (typ(T)!=t_VEC) { *B=NULL; return T; }
652 139587 : *B = gel(T,1); return gel(T,2);
653 : }
654 :
655 : GEN
656 152661 : RgX_to_FlxqX(GEN x, GEN T, ulong p)
657 : {
658 152661 : long i, l = lg(x);
659 152661 : GEN z = cgetg(l, t_POL); z[1] = x[1];
660 2332935 : for (i = 2; i < l; i++)
661 2180274 : gel(z,i) = Rg_to_Flxq(gel(x,i), T, p);
662 152661 : 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 695 : random_FlxqX(long d1, long v, GEN T, ulong p)
670 : {
671 695 : long dT = get_Flx_degree(T), vT = get_Flx_var(T);
672 695 : long i, d = d1+2;
673 695 : GEN y = cgetg(d,t_POL); y[1] = evalsigne(1) | evalvarn(v);
674 3620 : for (i=2; i<d; i++) gel(y,i) = random_Flx(dT, vT, p);
675 695 : return FlxX_renormalize(y,d);
676 : }
677 :
678 : /*Not stack clean*/
679 : GEN
680 2355810 : Kronecker_to_FlxqX_pre(GEN z, GEN T, ulong p, ulong pi)
681 : {
682 2355810 : long i,j,lx,l, N = (get_Flx_degree(T)<<1) + 1;
683 2355799 : GEN x, t = cgetg(N,t_VECSMALL);
684 2355799 : t[1] = get_Flx_var(T);
685 2355796 : l = lg(z); lx = (l-2) / (N-2);
686 2355796 : x = cgetg(lx+3,t_POL);
687 2355797 : x[1] = z[1];
688 28779045 : for (i=2; i<lx+2; i++)
689 : {
690 367930924 : for (j=2; j<N; j++) t[j] = z[j];
691 26423253 : z += (N-2);
692 26423253 : gel(x,i) = Flx_rem_pre(Flx_renormalize(t,N), T,p,pi);
693 : }
694 2355792 : N = (l-2) % (N-2) + 2;
695 7479015 : for (j=2; j<N; j++) t[j] = z[j];
696 2355792 : gel(x,i) = Flx_rem_pre(Flx_renormalize(t,N), T,p,pi);
697 2355747 : 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 1260819 : FlxqX_red_pre(GEN z, GEN T, ulong p, ulong pi)
705 : {
706 : GEN res;
707 1260819 : long i, l = lg(z);
708 1260819 : res = cgetg(l,t_POL); res[1] = z[1];
709 13005853 : for(i=2;i<l;i++) gel(res,i) = Flx_rem_pre(gel(z,i),T,p,pi);
710 1260767 : 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 1709662 : FlxqX_mul_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
731 : {
732 1709662 : pari_sp av = avma;
733 1709662 : GEN z, kx, ky, Tm = get_Flx_mod(T);
734 1709659 : kx= zxX_to_Kronecker(x, Tm);
735 1709650 : ky= zxX_to_Kronecker(y, Tm);
736 1709648 : z = Flx_mul_pre(ky, kx, p, pi);
737 1709614 : z = Kronecker_to_FlxqX_pre(z, T, p, pi);
738 1709579 : 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 355860 : FlxqX_sqr_pre(GEN x, GEN T, ulong p, ulong pi)
746 : {
747 355860 : pari_sp av = avma;
748 : GEN z,kx;
749 355860 : kx= zxX_to_Kronecker(x,get_Flx_mod(T));
750 355860 : z = Flx_sqr_pre(kx, p, pi);
751 355860 : z = Kronecker_to_FlxqX_pre(z,T,p,pi);
752 355857 : 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 155107 : FlxqX_Flxq_mul_pre(GEN P, GEN U, GEN T, ulong p, ulong pi)
760 : {
761 155107 : long i, lP = lg(P);
762 155107 : GEN res = cgetg(lP,t_POL);
763 155106 : res[1] = P[1];
764 554006 : for(i=2; i<lP; i++) gel(res,i) = Flxq_mul_pre(U,gel(P,i), T,p,pi);
765 155083 : 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 636056 : FlxqX_Flxq_mul_to_monic_pre(GEN P, GEN U, GEN T, ulong p, ulong pi)
773 : {
774 636056 : long i, lP = lg(P);
775 636056 : GEN res = cgetg(lP,t_POL);
776 636051 : res[1] = P[1];
777 3452918 : for(i=2; i<lP-1; i++) gel(res,i) = Flxq_mul_pre(U,gel(P,i), T,p,pi);
778 635689 : gel(res,lP-1) = pol1_Flx(get_Flx_var(T));
779 636059 : 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 190401 : FlxqX_normalize_pre(GEN z, GEN T, ulong p, ulong pi)
790 : {
791 190401 : GEN p1 = leading_coeff(z);
792 190401 : if (!lgpol(z) || (!degpol(p1) && p1[1] == 1)) return z;
793 190380 : 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 538490 : static GEN _FlxqX_mul(void *data,GEN a,GEN b)
801 : {
802 538490 : struct _FlxqX *d=(struct _FlxqX*)data;
803 538490 : 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 3145726 : 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 3145726 : if (!signe(y)) pari_err_INV("FlxqX_divrem",y);
831 3145726 : vx=varn(x); dy=degpol(y); dx=degpol(x);
832 3145756 : if (dx < dy)
833 : {
834 21709 : if (pr)
835 : {
836 21357 : av0 = avma; x = FlxqX_red_pre(x, T, p, pi);
837 21357 : if (pr == ONLY_DIVIDES) { set_avma(av0); return signe(x)? NULL: pol_0(vx); }
838 21357 : if (pr == ONLY_REM) return x;
839 21357 : *pr = x;
840 : }
841 21709 : return pol_0(vx);
842 : }
843 3124047 : lead = leading_coeff(y);
844 3124060 : if (!dy) /* y is constant */
845 : {
846 326127 : if (pr && pr != ONLY_DIVIDES)
847 : {
848 321468 : if (pr == ONLY_REM) return pol_0(vx);
849 133817 : *pr = pol_0(vx);
850 : }
851 138476 : if (Flx_equal1(lead)) return gcopy(x);
852 134292 : av0 = avma; x = FlxqX_Flxq_mul_pre(x,Flxq_inv(lead,T,p),T,p,pi);
853 134293 : return gerepileupto(av0,x);
854 : }
855 2797933 : av0 = avma; dz = dx-dy;
856 2797933 : lead = Flx_equal1(lead)? NULL: gclone(Flxq_inv_pre(lead,T,p,pi));
857 2797978 : set_avma(av0);
858 2797972 : z = cgetg(dz+3,t_POL); z[1] = x[1];
859 2797966 : x += 2; y += 2; z += 2;
860 :
861 2797966 : p1 = gel(x,dx); av = avma;
862 2797966 : gel(z,dz) = lead? gerepileupto(av, Flxq_mul_pre(p1,lead, T,p,pi)): gcopy(p1);
863 6454167 : for (i=dx-1; i>=dy; i--)
864 : {
865 3656204 : av=avma; p1=gel(x,i);
866 11938509 : for (j=i-dy+1; j<=i && j<=dz; j++)
867 8282566 : p1 = Flx_sub(p1, Flx_mul_pre(gel(z,j),gel(y,i-j),p,pi),p);
868 3655943 : if (lead) p1 = Flx_mul_pre(p1, lead,p,pi);
869 3655943 : tetpil=avma; gel(z,i-dy) = gerepile(av,tetpil,Flx_rem_pre(p1,T,p,pi));
870 : }
871 2797963 : if (!pr) { guncloneNULL(lead); return z-2; }
872 :
873 2603570 : rem = (GEN)avma; av = (pari_sp)new_chunk(dx+3);
874 2912761 : for (sx=0; ; i--)
875 : {
876 2912761 : p1 = gel(x,i);
877 8592286 : for (j=0; j<=i && j<=dz; j++)
878 5679819 : p1 = Flx_sub(p1, Flx_mul_pre(gel(z,j),gel(y,i-j),p,pi),p);
879 2912467 : tetpil=avma; p1 = Flx_rem_pre(p1, T,p,pi); if (lgpol(p1)) { sx = 1; break; }
880 381185 : if (!i) break;
881 309173 : set_avma(av);
882 : }
883 2603305 : 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 2603305 : lr=i+3; rem -= lr;
890 2603305 : rem[0] = evaltyp(t_POL) | _evallg(lr);
891 2603305 : rem[1] = z[-1];
892 2603305 : p1 = gerepile((pari_sp)rem,tetpil,p1);
893 2603544 : rem += 2; gel(rem,i) = p1;
894 16290286 : for (i--; i>=0; i--)
895 : {
896 13686752 : av=avma; p1 = gel(x,i);
897 44718260 : for (j=0; j<=i && j<=dz; j++)
898 31036537 : p1 = Flx_sub(p1, Flx_mul_pre(gel(z,j),gel(y,i-j),p,pi), p);
899 13681723 : tetpil=avma; gel(rem,i) = gerepile(av,tetpil, Flx_rem_pre(p1, T,p,pi));
900 : }
901 2603534 : rem -= 2;
902 2603534 : guncloneNULL(lead);
903 2603542 : if (!sx) (void)FlxX_renormalize(rem, lr);
904 2603543 : if (pr == ONLY_REM) return gerepileupto(av0,rem);
905 1221236 : *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 493432 : FlxqX_get_red_pre(GEN S, GEN T, ulong p, ulong pi)
1023 : {
1024 493432 : if (typ(S)==t_POL && lg(S)>FlxqX_BARRETT_LIMIT)
1025 6588 : retmkvec2(FlxqX_invBarrett_pre(S, T, p, pi), S);
1026 486844 : 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 1576737 : 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 1576737 : if (pr==ONLY_REM) return FlxqX_rem_pre(x, S, T, p, pi);
1124 1576737 : y = get_FlxqX_red(S, &B);
1125 1576736 : dy = degpol(y); dx = degpol(x); d = dx-dy;
1126 1576709 : if (!B && d+3 < FlxqX_DIVREM_BARRETT_LIMIT)
1127 1575644 : 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 1074065 : FlxqX_divrem(GEN x, GEN S, GEN T, ulong p, GEN *pr)
1140 : {
1141 1074065 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1142 1074065 : return FlxqX_divrem_pre(x, S, T, p, pi, pr);
1143 : }
1144 :
1145 : GEN
1146 2016211 : FlxqX_rem_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
1147 : {
1148 2016211 : GEN B, y = get_FlxqX_red(S, &B);
1149 2016210 : long dy = degpol(y), dx = degpol(x), d = dx-dy;
1150 2016210 : if (d < 0) return FlxqX_red_pre(x, T, p, pi);
1151 1671343 : if (!B && d+3 < FlxqX_REM_BARRETT_LIMIT)
1152 1569998 : 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 4133 : FlxqX_halfres_basecase(GEN a, GEN b, GEN T, ulong p, ulong pi, GEN *pa, GEN *pb, struct FlxqX_res *res)
1284 : {
1285 4133 : pari_sp av=avma;
1286 : GEN u,u1,v,v1, M;
1287 4133 : long vx = varn(a), vT = get_Flx_var(T), n = lgpol(a)>>1;
1288 4133 : u1 = v = pol_0(vx);
1289 4133 : u = v1 = pol1_FlxX(vx, vT);
1290 22890 : while (lgpol(b)>n)
1291 : {
1292 : GEN r, q;
1293 18757 : q = FlxqX_divrem(a,b, T, p, &r);
1294 18757 : 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 18757 : a = b; b = r; swap(u,u1); swap(v,v1);
1307 18757 : u1 = FlxX_sub(u1, FlxqX_mul_pre(u, q, T, p, pi), p);
1308 18757 : v1 = FlxX_sub(v1, FlxqX_mul_pre(v, q, T, p, pi), p);
1309 18757 : if (gc_needed(av,2))
1310 : {
1311 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_halfgcd (d = %ld)",degpol(b));
1312 0 : if (res)
1313 0 : gerepileall(av, 8, &a,&b,&u1,&v1,&u,&v,&res->res,&res->lc);
1314 : else
1315 0 : gerepileall(av, 6, &a,&b,&u1,&v1,&u,&v);
1316 : }
1317 : }
1318 4133 : M = mkmat22(u,v,u1,v1); *pa = a; *pb = b;
1319 0 : return res ? gc_all(av, 5, &M, pa, pb, &res->res, &res->lc)
1320 4133 : : gc_all(av, 3, &M, pa, pb);
1321 : }
1322 :
1323 : static GEN FlxqX_halfres_i(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, struct FlxqX_res *res);
1324 :
1325 : static GEN
1326 3362 : FlxqX_halfres_split(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, struct FlxqX_res *res)
1327 : {
1328 3362 : pari_sp av = avma;
1329 : GEN Q, R, S, V1, V2;
1330 : GEN x1, y1, r, q;
1331 3362 : long l = lgpol(x), n = l>>1, k, vT = get_Flx_var(T);
1332 3362 : if (lgpol(y) <= n)
1333 0 : { *a = RgX_copy(x); *b = RgX_copy(y); return matid2_FlxXM(varn(x), vT); }
1334 3362 : if (res)
1335 : {
1336 0 : res->lc = leading_coeff(y);
1337 0 : res->deg0 -= n;
1338 0 : res->deg1 -= n;
1339 0 : res->off += n;
1340 : }
1341 3362 : R = FlxqX_halfres_i(FlxX_shift(x,-n, vT),FlxX_shift(y,-n, vT), T, p, pi, a, b, res);
1342 3362 : if (res)
1343 : {
1344 0 : res->off -= n;
1345 0 : res->deg0 += n;
1346 0 : res->deg1 += n;
1347 : }
1348 3362 : V1 = FlxqXM_FlxqX_mul2(R, Flxn_red(x,n), Flxn_red(y,n), T, p, pi);
1349 3362 : x1 = FlxX_add(FlxX_shift(*a,n,vT), gel(V1,1), p);
1350 3362 : y1 = FlxX_add(FlxX_shift(*b,n,vT), gel(V1,2), p);
1351 3362 : if (lgpol(y1) <= n)
1352 : {
1353 73 : *a = x1; *b = y1;
1354 0 : return res ? gc_all(av, 5, &R, a, b, &res->res, &res->lc)
1355 73 : : gc_all(av, 3, &R, a, b);
1356 : }
1357 3289 : k = 2*n-degpol(y1);
1358 3289 : q = FlxqX_divrem(x1, y1, T, p, &r);
1359 3289 : if (res)
1360 : {
1361 0 : long dx1 = degpol(x1), dy1 = degpol(y1), dr = degpol(r);
1362 0 : if (dy1 < degpol(y))
1363 0 : FlxqX_halfres_update(res->deg0, res->deg1, dy1, T, p, pi, res);
1364 0 : res->lc = leading_coeff(y1);
1365 0 : res->deg0 = dx1;
1366 0 : res->deg1 = dy1;
1367 0 : if (dr >= n)
1368 : {
1369 0 : FlxqX_halfres_update(dx1, dy1, dr, T, p, pi, res);
1370 0 : res->deg0 = dy1;
1371 0 : res->deg1 = dr;
1372 : }
1373 0 : res->deg0 -= k;
1374 0 : res->deg1 -= k;
1375 0 : res->off += k;
1376 : }
1377 3289 : S = FlxqX_halfres_i(FlxX_shift(y1,-k, vT), FlxX_shift(r,-k, vT), T, p, pi, a, b, res);
1378 3289 : if (res)
1379 : {
1380 0 : res->deg0 += k;
1381 0 : res->deg1 += k;
1382 0 : res->off -= k;
1383 : }
1384 3289 : Q = FlxqXM_mul2(S, FlxqX_FlxqXM_qmul(q, R, T, p, pi), T, p, pi);
1385 3289 : V2 = FlxqXM_FlxqX_mul2(S, FlxXn_red(y1,k), FlxXn_red(r,k), T, p, pi);
1386 3289 : *a = FlxX_add(FlxX_shift(*a,k,vT), gel(V2,1), p);
1387 3289 : *b = FlxX_add(FlxX_shift(*b,k,vT), gel(V2,2), p);
1388 0 : return res ? gc_all(av, 5, &Q, a, b, &res->res, &res->lc)
1389 3289 : : gc_all(av, 3, &Q, a, b);
1390 : }
1391 :
1392 : static GEN
1393 7495 : FlxqX_halfres_i(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, struct FlxqX_res *res)
1394 : {
1395 7495 : if (lgpol(x) < FlxqX_HALFGCD_LIMIT)
1396 4133 : return FlxqX_halfres_basecase(x, y, T, p, pi, a, b, res);
1397 3362 : return FlxqX_halfres_split(x, y, T, p, pi, a, b, res);
1398 : }
1399 :
1400 : static GEN
1401 844 : FlxqX_halfgcd_all_i(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *pa, GEN *pb)
1402 : {
1403 : GEN a, b;
1404 844 : GEN R = FlxqX_halfres_i(x, y, T, p, pi, &a, &b, NULL);
1405 844 : if (pa) *pa = a;
1406 844 : if (pb) *pb = b;
1407 844 : return R;
1408 : }
1409 :
1410 : /* Return M in GL_2(Fp[X]/(T)[Y]) such that:
1411 : if [a',b']~=M*[a,b]~ then degpol(a')>= (lgpol(a)>>1) >degpol(b')
1412 : */
1413 :
1414 : GEN
1415 844 : FlxqX_halfgcd_all_pre(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b)
1416 : {
1417 844 : pari_sp av = avma;
1418 : GEN R,q,r;
1419 844 : if (!signe(x))
1420 : {
1421 0 : if (a) *a = RgX_copy(y);
1422 0 : if (b) *b = RgX_copy(x);
1423 0 : return matJ2_FlxXM(varn(x),get_Flx_var(T));
1424 : }
1425 844 : if (degpol(y)<degpol(x)) return FlxqX_halfgcd_all_i(x, y, T, p, pi, a, b);
1426 186 : q = FlxqX_divrem_pre(y, x, T, p, pi, &r);
1427 186 : R = FlxqX_halfgcd_all_i(x, r, T, p, pi, a, b);
1428 186 : gcoeff(R,1,1) = FlxX_sub(gcoeff(R,1,1),
1429 186 : FlxqX_mul_pre(q, gcoeff(R,1,2), T, p, pi), p);
1430 186 : gcoeff(R,2,1) = FlxX_sub(gcoeff(R,2,1),
1431 186 : FlxqX_mul_pre(q, gcoeff(R,2,2), T, p, pi), p);
1432 186 : return !a && b ? gc_all(av, 2, &R, b): gc_all(av, 1+!!a+!!b, &R, a, b);
1433 : }
1434 : GEN
1435 7 : FlxqX_halfgcd_all(GEN x, GEN y, GEN T, ulong p, GEN *a, GEN *b)
1436 7 : { return FlxqX_halfgcd_all_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p), a, b); }
1437 :
1438 : GEN
1439 320 : FlxqX_halfgcd_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
1440 320 : { return FlxqX_halfgcd_all_pre(x, y, T, p, pi, NULL, NULL); }
1441 :
1442 : GEN
1443 0 : FlxqX_halfgcd(GEN x, GEN y, GEN T, ulong p)
1444 0 : { return FlxqX_halfgcd_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1445 :
1446 : static GEN
1447 166986 : FlxqX_gcd_basecase(GEN a, GEN b, GEN T, ulong p, ulong pi)
1448 : {
1449 166986 : pari_sp av = avma, av0=avma;
1450 1076252 : while (signe(b))
1451 : {
1452 : GEN c;
1453 909266 : if (gc_needed(av0,2))
1454 : {
1455 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_gcd (d = %ld)",degpol(b));
1456 0 : gerepileall(av0,2, &a,&b);
1457 : }
1458 909266 : av = avma; c = FlxqX_rem_pre(a, b, T, p, pi); a=b; b=c;
1459 : }
1460 166986 : return gc_const(av, a);
1461 : }
1462 :
1463 : GEN
1464 176128 : FlxqX_gcd_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
1465 : {
1466 176128 : pari_sp av = avma;
1467 176128 : x = FlxqX_red_pre(x, T, p, pi);
1468 176128 : y = FlxqX_red_pre(y, T, p, pi);
1469 176128 : if (!signe(x)) return gerepileupto(av, y);
1470 167364 : while (lgpol(y)>=FlxqX_GCD_LIMIT)
1471 : {
1472 378 : if (lgpol(y)<=(lgpol(x)>>1))
1473 : {
1474 0 : GEN r = FlxqX_rem_pre(x, y, T, p, pi);
1475 0 : x = y; y = r;
1476 : }
1477 378 : (void) FlxqX_halfgcd_all_pre(x,y, T, p, pi, &x, &y);
1478 378 : if (gc_needed(av,2))
1479 : {
1480 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_gcd (y = %ld)",degpol(y));
1481 0 : gerepileall(av,2,&x,&y);
1482 : }
1483 : }
1484 166986 : return gerepileupto(av, FlxqX_gcd_basecase(x, y, T, p, pi));
1485 : }
1486 : GEN
1487 23646 : FlxqX_gcd(GEN x, GEN y, GEN T, ulong p)
1488 23646 : { return FlxqX_gcd_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1489 :
1490 : static GEN
1491 133823 : FlxqX_extgcd_basecase(GEN a, GEN b, GEN T, ulong p,ulong pi, GEN *ptu, GEN *ptv)
1492 : {
1493 133823 : pari_sp av=avma;
1494 : GEN u,v,d,d1,v1;
1495 133823 : long vx = varn(a);
1496 133823 : d = a; d1 = b;
1497 133823 : v = pol_0(vx); v1 = pol1_FlxX(vx, get_Flx_var(T));
1498 470896 : while (signe(d1))
1499 : {
1500 337065 : GEN r, q = FlxqX_divrem_pre(d, d1, T, p, pi, &r);
1501 337091 : v = FlxX_sub(v, FlxqX_mul_pre(q,v1,T, p, pi),p);
1502 337069 : u=v; v=v1; v1=u;
1503 337069 : u=r; d=d1; d1=u;
1504 337069 : if (gc_needed(av,2))
1505 : {
1506 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_extgcd (d = %ld)",degpol(d));
1507 0 : gerepileall(av,5, &d,&d1,&u,&v,&v1);
1508 : }
1509 : }
1510 133831 : if (ptu)
1511 133817 : *ptu = FlxqX_div_pre(FlxX_sub(d,FlxqX_mul_pre(b,v, T,p,pi), p), a, T,p,pi);
1512 133827 : *ptv = v; return d;
1513 : }
1514 :
1515 : static GEN
1516 130 : FlxqX_extgcd_halfgcd(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *ptu, GEN *ptv)
1517 : {
1518 : GEN u,v;
1519 130 : GEN V = cgetg(expu(lgpol(y))+2,t_VEC);
1520 130 : long i, n = 0, vs = varn(x), vT = get_Flx_var(T);
1521 269 : while (lgpol(y) >= FlxqX_EXTGCD_LIMIT)
1522 : {
1523 139 : if (lgpol(y)<=(lgpol(x)>>1))
1524 : {
1525 0 : GEN r, q = FlxqX_divrem_pre(x, y, T, p, pi, &r);
1526 0 : x = y; y = r;
1527 0 : gel(V,++n) = mkmat22(pol_0(vs),pol1_FlxX(vs,vT),pol1_FlxX(vs,vT),FlxX_neg(q,p));
1528 : } else
1529 139 : gel(V,++n) = FlxqX_halfgcd_all_pre(x, y, T, p, pi, &x, &y);
1530 : }
1531 130 : y = FlxqX_extgcd_basecase(x,y, T, p, pi, &u,&v);
1532 139 : for (i = n; i>1; i--)
1533 : {
1534 9 : GEN R = gel(V,i);
1535 9 : GEN u1 = FlxqX_addmulmul(u, v, gcoeff(R,1,1), gcoeff(R,2,1), T, p, pi);
1536 9 : GEN v1 = FlxqX_addmulmul(u, v, gcoeff(R,1,2), gcoeff(R,2,2), T, p, pi);
1537 9 : u = u1; v = v1;
1538 : }
1539 : {
1540 130 : GEN R = gel(V,1);
1541 130 : if (ptu)
1542 130 : *ptu = FlxqX_addmulmul(u, v, gcoeff(R,1,1), gcoeff(R,2,1), T, p, pi);
1543 130 : *ptv = FlxqX_addmulmul(u, v, gcoeff(R,1,2), gcoeff(R,2,2), T, p, pi);
1544 : }
1545 130 : return y;
1546 : }
1547 :
1548 : /* x and y in Z[Y][X], return lift(gcd(x mod T,p, y mod T,p)). Set u and v st
1549 : * ux + vy = gcd (mod T,p) */
1550 : GEN
1551 133828 : FlxqX_extgcd_pre(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *ptu, GEN *ptv)
1552 : {
1553 133828 : pari_sp av = avma;
1554 : GEN d;
1555 133828 : x = FlxqX_red_pre(x, T, p, pi);
1556 133825 : y = FlxqX_red_pre(y, T, p, pi);
1557 133822 : if (lgpol(y)>=FlxqX_EXTGCD_LIMIT)
1558 130 : d = FlxqX_extgcd_halfgcd(x, y, T, p, pi, ptu, ptv);
1559 : else
1560 133691 : d = FlxqX_extgcd_basecase(x, y, T, p, pi, ptu, ptv);
1561 133827 : return gc_all(av, ptu?3:2, &d, ptv, ptu);
1562 : }
1563 : GEN
1564 133814 : FlxqX_extgcd(GEN x, GEN y, GEN T, ulong p, GEN *ptu, GEN *ptv)
1565 : {
1566 133814 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1567 133814 : return FlxqX_extgcd_pre(x, y, T, p, pi, ptu, ptv);
1568 : }
1569 :
1570 : static GEN
1571 356518 : FlxqX_saferem(GEN P, GEN Q, GEN T, ulong p, ulong pi)
1572 : {
1573 356518 : GEN U = Flxq_invsafe_pre(leading_coeff(Q), T, p, pi);
1574 356545 : if (!U) return NULL;
1575 356545 : Q = FlxqX_Flxq_mul_to_monic_pre(Q,U,T,p,pi);
1576 356555 : return FlxqX_rem_pre(P,Q,T,p,pi);
1577 : }
1578 :
1579 : GEN
1580 0 : FlxqX_eval(GEN x, GEN y, GEN T, ulong p)
1581 : {
1582 : pari_sp av;
1583 : GEN p1, r;
1584 0 : long j, i=lg(x)-1;
1585 0 : if (i<=2)
1586 0 : return (i==2)? gcopy(gel(x,2)): pol0_Flx(get_Flx_var(T));
1587 0 : av=avma; p1=gel(x,i);
1588 : /* specific attention to sparse polynomials (see poleval)*/
1589 : /*You've guessed it! It's a copy-paste(tm)*/
1590 0 : for (i--; i>=2; i=j-1)
1591 : {
1592 0 : for (j=i; lg(gel(x,j))==1; j--)
1593 0 : if (j==2)
1594 : {
1595 0 : if (i!=j) y = Flxq_powu(y, i-j+1, T, p);
1596 0 : return gerepileupto(av, Flxq_mul(p1,y, T, p));
1597 : }
1598 0 : r = (i==j)? y: Flxq_powu(y, i-j+1, T, p);
1599 0 : p1 = Flx_add(Flxq_mul(p1,r,T,p), gel(x,j), p);
1600 : }
1601 0 : return gerepileupto(av, p1);
1602 : }
1603 :
1604 : GEN
1605 89130 : FlxqX_safegcd(GEN P, GEN Q, GEN T, ulong p)
1606 : {
1607 89130 : pari_sp av = avma;
1608 : ulong pi;
1609 : GEN U;
1610 89130 : if (!signe(P)) return gcopy(Q);
1611 89130 : if (!signe(Q)) return gcopy(P);
1612 89130 : pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1613 89130 : T = Flx_get_red_pre(T,p,pi);
1614 : for(;;)
1615 : {
1616 309755 : P = FlxqX_saferem(P,Q,T,p,pi);
1617 309758 : if (!P) return gc_NULL(av);
1618 309758 : if (!signe(P)) break;
1619 220625 : if (gc_needed(av, 1))
1620 : {
1621 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_safegcd");
1622 0 : gerepileall(av, 2, &P,&Q);
1623 : }
1624 220625 : swap(P, Q);
1625 : }
1626 89133 : U = Flxq_invsafe_pre(leading_coeff(Q), T, p, pi);
1627 89133 : if (!U) return gc_NULL(av);
1628 89133 : Q = FlxqX_Flxq_mul_to_monic_pre(Q,U,T,p,pi);
1629 89133 : return gerepileupto(av, Q);
1630 : }
1631 :
1632 : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
1633 : GEN
1634 9010 : FlxqX_saferesultant(GEN a, GEN b, GEN T, ulong p)
1635 : {
1636 9010 : long vT = get_Flx_var(T), da,db,dc;
1637 : ulong pi;
1638 : pari_sp av;
1639 9010 : GEN c,lb, res = pol1_Flx(vT);
1640 :
1641 9010 : if (!signe(a) || !signe(b)) return pol0_Flx(vT);
1642 :
1643 9010 : da = degpol(a);
1644 9010 : db = degpol(b);
1645 9010 : if (db > da)
1646 : {
1647 0 : swapspec(a,b, da,db);
1648 0 : if (both_odd(da,db)) res = Flx_neg(res, p);
1649 : }
1650 9010 : if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
1651 9010 : pi = SMALL_ULONG(p)? 0: get_Fl_red(p); av = avma;
1652 55750 : while (db)
1653 : {
1654 46762 : lb = gel(b,db+2);
1655 46762 : c = FlxqX_saferem(a,b, T,p,pi);
1656 46824 : if (!c) return gc_NULL(av);
1657 46824 : a = b; b = c; dc = degpol(c);
1658 46825 : if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
1659 :
1660 46811 : if (both_odd(da,db)) res = Flx_neg(res, p);
1661 46809 : if (!Flx_equal1(lb))
1662 46753 : res = Flxq_mul_pre(res, Flxq_powu_pre(lb, da - dc, T, p, pi), T, p, pi);
1663 46740 : if (gc_needed(av,2))
1664 : {
1665 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
1666 0 : gerepileall(av,3, &a,&b,&res);
1667 : }
1668 46740 : da = db; /* = degpol(a) */
1669 46740 : db = dc; /* = degpol(b) */
1670 : }
1671 8988 : res = Flxq_mul_pre(res, Flxq_powu_pre(gel(b,2), da, T, p, pi), T, p, pi);
1672 8994 : return gerepileupto(av, res);
1673 : }
1674 :
1675 : static GEN
1676 0 : FlxqX_halfres(GEN x, GEN y, GEN T, ulong p, ulong pi, GEN *a, GEN *b, GEN *r)
1677 : {
1678 : struct FlxqX_res res;
1679 : GEN V;
1680 : long dB;
1681 :
1682 0 : res.res = *r;
1683 0 : res.lc = leading_coeff(y);
1684 0 : res.deg0 = degpol(x);
1685 0 : res.deg1 = degpol(y);
1686 0 : res.off = 0;
1687 0 : V = FlxqX_halfres_i(x, y, T, p, pi, a, b, &res);
1688 0 : dB = degpol(*b);
1689 0 : if (dB < degpol(y))
1690 0 : FlxqX_halfres_update(res.deg0, res.deg1, dB, T, p, pi, &res);
1691 0 : *r = res.res;
1692 0 : return V;
1693 : }
1694 :
1695 : static GEN
1696 56 : FlxqX_resultant_basecase(GEN a, GEN b, GEN T, ulong p, ulong pi)
1697 : {
1698 56 : pari_sp av = avma;
1699 56 : long vT = get_Flx_var(T), da,db,dc;
1700 56 : GEN c,lb, res = pol1_Flx(vT);
1701 :
1702 56 : if (!signe(a) || !signe(b)) return pol0_Flx(vT);
1703 :
1704 56 : da = degpol(a);
1705 56 : db = degpol(b);
1706 56 : if (db > da)
1707 : {
1708 0 : swapspec(a,b, da,db);
1709 0 : if (both_odd(da,db)) res = Flx_neg(res, p);
1710 : }
1711 56 : if (!da) return pol1_Flx(vT); /* = res * a[2] ^ db, since 0 <= db <= da = 0 */
1712 147 : while (db)
1713 : {
1714 91 : lb = gel(b,db+2);
1715 91 : c = FlxqX_rem_pre(a,b, T,p,pi);
1716 91 : a = b; b = c; dc = degpol(c);
1717 91 : if (dc < 0) { set_avma(av); return pol0_Flx(vT); }
1718 :
1719 91 : if (both_odd(da,db)) res = Flx_neg(res, p);
1720 91 : if (!Flx_equal1(lb))
1721 63 : res = Flxq_mul_pre(res, Flxq_powu_pre(lb, da - dc, T,p,pi), T,p,pi);
1722 91 : if (gc_needed(av,2))
1723 : {
1724 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (da = %ld)",da);
1725 0 : gerepileall(av,3, &a,&b,&res);
1726 : }
1727 91 : da = db; /* = degpol(a) */
1728 91 : db = dc; /* = degpol(b) */
1729 : }
1730 56 : res = Flxq_mul_pre(res, Flxq_powu_pre(gel(b,2), da, T,p,pi), T,p,pi);
1731 56 : return gerepileupto(av, res);
1732 : }
1733 :
1734 : /* Res(A,B) = Res(B,R) * lc(B)^(a-r) * (-1)^(ab), with R=A%B, a=deg(A) ...*/
1735 : GEN
1736 56 : FlxqX_resultant_pre(GEN x, GEN y, GEN T, ulong p, ulong pi)
1737 : {
1738 56 : pari_sp av = avma;
1739 56 : long dx, dy, vT = get_Flx_var(T);
1740 56 : GEN res = pol1_Flx(vT);
1741 56 : if (!signe(x) || !signe(y)) return pol0_Flx(vT);
1742 56 : dx = degpol(x); dy = degpol(y);
1743 56 : if (dx < dy)
1744 : {
1745 21 : swap(x,y);
1746 21 : if (both_odd(dx, dy))
1747 0 : res = Flx_neg(res, p);
1748 : }
1749 56 : while (lgpol(y) >= FlxqX_GCD_LIMIT)
1750 : {
1751 0 : if (lgpol(y)<=(lgpol(x)>>1))
1752 : {
1753 0 : GEN r = FlxqX_rem_pre(x, y, T, p, pi);
1754 0 : long dx = degpol(x), dy = degpol(y), dr = degpol(r);
1755 0 : GEN ly = gel(y,dy+2);
1756 0 : if (!Flx_equal1(ly))
1757 0 : res = Flxq_mul_pre(res, Flxq_powu_pre(ly, dx - dr, T, p, pi), T, p, pi);
1758 0 : if (both_odd(dx, dy))
1759 0 : res = Flx_neg(res, p);
1760 0 : x = y; y = r;
1761 : }
1762 0 : (void) FlxqX_halfres(x, y, T, p, pi, &x, &y, &res);
1763 0 : if (gc_needed(av,2))
1764 : {
1765 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqX_resultant (y = %ld)",degpol(y));
1766 0 : gerepileall(av,3,&x,&y,&res);
1767 : }
1768 : }
1769 56 : res = Flxq_mul_pre(res, FlxqX_resultant_basecase(x, y, T, p, pi), T, p, pi);
1770 56 : return gerepileupto(av, res);
1771 : }
1772 : GEN
1773 56 : FlxqX_resultant(GEN x, GEN y, GEN T, ulong p)
1774 56 : { return FlxqX_resultant_pre(x, y, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1775 :
1776 : /* disc P = (-1)^(n(n-1)/2) lc(P)^(n - deg P' - 2) Res(P,P'), n = deg P */
1777 : GEN
1778 14 : FlxqX_disc(GEN P, GEN T, ulong p)
1779 : {
1780 14 : pari_sp av = avma;
1781 14 : GEN L, dP = FlxX_deriv(P, p), D = FlxqX_resultant(P, dP, T, p);
1782 : long dd;
1783 14 : if (!lgpol(D)) return pol0_Flx(get_Flx_var(T));
1784 14 : dd = degpol(P) - 2 - degpol(dP); /* >= -1; > -1 iff p | deg(P) */
1785 14 : L = leading_coeff(P);
1786 14 : if (dd && !Flx_equal1(L))
1787 : {
1788 0 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1789 0 : D = (dd == -1)? Flxq_div_pre(D,L,T,p,pi)
1790 0 : : Flxq_mul_pre(D, Flxq_powu_pre(L, dd, T,p,pi), T,p,pi); }
1791 14 : if (degpol(P) & 2) D = Flx_neg(D, p);
1792 14 : return gerepileupto(av, D);
1793 : }
1794 :
1795 : INLINE GEN
1796 5975 : FlxXn_recip(GEN x, long n, long v)
1797 5975 : { return FlxX_recipspec(x+2, minss(lgpol(x), n), n, v); }
1798 :
1799 : GEN
1800 2390 : FlxqX_Newton_pre(GEN P, long n, GEN T, ulong p, ulong pi)
1801 : {
1802 2390 : pari_sp av = avma;
1803 2390 : long d = degpol(P), vT = get_Flx_var(T);
1804 2390 : GEN dP = FlxXn_recip(FlxX_deriv(P, p), d, vT);
1805 2390 : GEN Q = FlxqXn_mul_pre(FlxqXn_inv_pre(FlxXn_recip(P, d+1, vT), n, T,p,pi),
1806 : dP, n, T, p, pi);
1807 2390 : return gerepilecopy(av, Q);
1808 : }
1809 : GEN
1810 0 : FlxqX_Newton(GEN P, long n, GEN T, ulong p)
1811 0 : { return FlxqX_Newton_pre(P, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1812 :
1813 : GEN
1814 1195 : FlxqX_fromNewton_pre(GEN P, GEN T, ulong p, ulong pi)
1815 : {
1816 1195 : pari_sp av = avma;
1817 1195 : long vT = get_Flx_var(T);
1818 1195 : long n = Flx_constant(constant_coeff(P))+1;
1819 1195 : GEN z = FlxX_neg(FlxX_shift(P, -1, vT), p);
1820 1195 : GEN Q = FlxXn_recip(FlxqXn_expint_pre(z, n, T, p, pi), n, vT);
1821 1195 : return gerepilecopy(av, Q);
1822 : }
1823 : GEN
1824 0 : FlxqX_fromNewton(GEN P, GEN T, ulong p)
1825 0 : { return FlxqX_fromNewton_pre(P, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1826 :
1827 : GEN
1828 1195 : FlxqX_composedsum(GEN P, GEN Q, GEN T, ulong p)
1829 : {
1830 1195 : pari_sp av = avma;
1831 1195 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1832 1195 : long n = 1+ degpol(P)*degpol(Q);
1833 1195 : GEN Pl = FlxX_invLaplace(FlxqX_Newton_pre(P,n, T,p,pi), p);
1834 1195 : GEN Ql = FlxX_invLaplace(FlxqX_Newton_pre(Q,n, T,p,pi), p);
1835 1195 : GEN L = FlxX_Laplace(FlxqXn_mul_pre(Pl, Ql, n, T,p,pi), p);
1836 1195 : GEN R = FlxqX_fromNewton_pre(L, T, p, pi);
1837 1195 : GEN lead = Flxq_mul_pre(Flxq_powu_pre(leading_coeff(P),degpol(Q), T,p,pi),
1838 1195 : Flxq_powu_pre(leading_coeff(Q),degpol(P), T,p,pi),
1839 : T, p, pi);
1840 1195 : return gerepileupto(av, FlxqX_Flxq_mul_pre(R, lead, T, p, pi));
1841 : }
1842 :
1843 : GEN
1844 223086 : FlxqXV_prod(GEN V, GEN T, ulong p)
1845 : {
1846 223086 : struct _FlxqX d; d.p=p; d.T=T; d.pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1847 223086 : return gen_product(V, (void*)&d, &_FlxqX_mul);
1848 : }
1849 :
1850 : static GEN
1851 32145 : FlxqV_roots_to_deg1(GEN x, GEN T, ulong p, long v)
1852 : {
1853 32145 : long sv = get_Flx_var(T);
1854 124198 : pari_APPLY_same(deg1pol_shallow(pol1_Flx(sv),Flx_neg(gel(x,i),p),v))
1855 : }
1856 :
1857 : GEN
1858 32145 : FlxqV_roots_to_pol(GEN V, GEN T, ulong p, long v)
1859 : {
1860 32145 : pari_sp ltop = avma;
1861 32145 : GEN W = FlxqV_roots_to_deg1(V, T, p, v);
1862 32144 : return gerepileupto(ltop, FlxqXV_prod(W, T, p));
1863 : }
1864 :
1865 : /*******************************************************************/
1866 : /* */
1867 : /* (Fl[X]/T(X))[Y] / S(Y) */
1868 : /* */
1869 : /*******************************************************************/
1870 :
1871 : GEN
1872 470036 : FlxqXQ_mul_pre(GEN x, GEN y, GEN S, GEN T, ulong p, ulong pi)
1873 470036 : { return FlxqX_rem_pre(FlxqX_mul_pre(x,y,T,p,pi),S,T,p,pi); }
1874 : GEN
1875 1947 : FlxqXQ_mul(GEN x, GEN y, GEN S, GEN T, ulong p)
1876 1947 : { return FlxqXQ_mul_pre(x, y, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1877 :
1878 : GEN
1879 263889 : FlxqXQ_sqr_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
1880 263889 : { return FlxqX_rem_pre(FlxqX_sqr_pre(x,T,p,pi),S,T,p,pi); }
1881 : GEN
1882 0 : FlxqXQ_sqr(GEN x, GEN S, GEN T, ulong p)
1883 0 : { return FlxqXQ_sqr_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1884 :
1885 : GEN
1886 14 : FlxqXQ_invsafe_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
1887 : {
1888 14 : GEN V, z = FlxqX_extgcd_pre(get_FlxqX_mod(S), x, T, p, pi, NULL, &V);
1889 14 : if (degpol(z)) return NULL;
1890 14 : z = Flxq_invsafe_pre(gel(z,2),T,p,pi);
1891 14 : if (!z) return NULL;
1892 14 : return FlxqX_Flxq_mul_pre(V, z, T, p, pi);
1893 : }
1894 : GEN
1895 0 : FlxqXQ_invsafe(GEN x, GEN S, GEN T, ulong p)
1896 0 : { return FlxqXQ_invsafe_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1897 :
1898 : GEN
1899 14 : FlxqXQ_inv_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
1900 : {
1901 14 : pari_sp av = avma;
1902 14 : GEN U = FlxqXQ_invsafe_pre(x, S, T, p, pi);
1903 14 : if (!U) pari_err_INV("FlxqXQ_inv",x);
1904 14 : return gerepileupto(av, U);
1905 : }
1906 : GEN
1907 14 : FlxqXQ_inv(GEN x, GEN S, GEN T,ulong p)
1908 14 : { return FlxqXQ_inv_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1909 :
1910 : GEN
1911 0 : FlxqXQ_div_pre(GEN x, GEN y, GEN S, GEN T, ulong p, ulong pi)
1912 0 : { return FlxqXQ_mul_pre(x, FlxqXQ_inv_pre(y,S,T,p,pi),S,T,p,pi); }
1913 : GEN
1914 0 : FlxqXQ_div(GEN x, GEN y, GEN S, GEN T, ulong p)
1915 0 : { return FlxqXQ_div_pre(x, y, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
1916 :
1917 : struct _FlxqXQ {
1918 : GEN T, S;
1919 : ulong p, pi;
1920 : };
1921 : static GEN
1922 0 : _FlxqXQ_add(void *data, GEN x, GEN y) {
1923 0 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1924 0 : return FlxX_add(x,y, d->p);
1925 : }
1926 : static GEN
1927 5526 : _FlxqXQ_sub(void *data, GEN x, GEN y) {
1928 5526 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1929 5526 : return FlxX_sub(x,y, d->p);
1930 : }
1931 : #if 0
1932 : static GEN
1933 : _FlxqXQ_cmul(void *data, GEN P, long a, GEN x) {
1934 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1935 : return FlxX_Flx_mul(x,gel(P,a+2), d->p);
1936 : }
1937 : #endif
1938 : static GEN
1939 6044 : _FlxqXQ_red(void *data, GEN x) {
1940 6044 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1941 6044 : return FlxqX_red_pre(x, d->T, d->p, d->pi);
1942 : }
1943 : static GEN
1944 180037 : _FlxqXQ_mul(void *data, GEN x, GEN y) {
1945 180037 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1946 180037 : return FlxqXQ_mul_pre(x,y, d->S,d->T, d->p, d->pi);
1947 : }
1948 : static GEN
1949 263448 : _FlxqXQ_sqr(void *data, GEN x) {
1950 263448 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1951 263448 : return FlxqXQ_sqr_pre(x, d->S,d->T, d->p, d->pi);
1952 : }
1953 :
1954 : static GEN
1955 111778 : _FlxqXQ_one(void *data) {
1956 111778 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1957 111778 : return pol1_FlxX(get_FlxqX_var(d->S),get_Flx_var(d->T));
1958 : }
1959 :
1960 : static GEN
1961 316 : _FlxqXQ_zero(void *data) {
1962 316 : struct _FlxqXQ *d = (struct _FlxqXQ*) data;
1963 316 : return pol_0(get_FlxqX_var(d->S));
1964 : }
1965 :
1966 : static struct bb_algebra FlxqXQ_algebra = { _FlxqXQ_red, _FlxqXQ_add,
1967 : _FlxqXQ_sub, _FlxqXQ_mul, _FlxqXQ_sqr, _FlxqXQ_one, _FlxqXQ_zero };
1968 :
1969 : const struct bb_algebra *
1970 761 : get_FlxqXQ_algebra(void **E, GEN S, GEN T, ulong p)
1971 : {
1972 761 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1973 761 : GEN z = new_chunk(sizeof(struct _FlxqXQ));
1974 761 : struct _FlxqXQ *e = (struct _FlxqXQ *) z;
1975 761 : e->T = Flx_get_red_pre(T, p, pi);
1976 761 : e->S = FlxqX_get_red_pre(S, e->T, p, pi);
1977 761 : e->p = p;
1978 761 : e->pi= pi; *E = (void*)e;
1979 761 : return &FlxqXQ_algebra;
1980 : }
1981 :
1982 : /* x over Fq, return lift(x^n) mod S */
1983 : GEN
1984 91 : FlxqXQ_pow_pre(GEN x, GEN n, GEN S, GEN T, ulong p, ulong pi)
1985 : {
1986 91 : pari_sp av = avma;
1987 : struct _FlxqXQ D;
1988 91 : long s = signe(n);
1989 91 : if (!s) return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
1990 91 : if (s < 0) x = FlxqXQ_inv_pre(x,S,T,p,pi);
1991 91 : if (is_pm1(n)) return s < 0 ? x : gcopy(x);
1992 91 : if (degpol(x) >= get_FlxqX_degree(S)) x = FlxqX_rem_pre(x,S,T,p,pi);
1993 91 : T = Flx_get_red_pre(T, p, pi);
1994 91 : S = FlxqX_get_red_pre(S, T, p, pi);
1995 91 : D.S = S; D.T = T; D.p = p; D.pi = pi;
1996 91 : x = gen_pow_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
1997 91 : return gerepilecopy(av, x);
1998 : }
1999 : GEN
2000 35 : FlxqXQ_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
2001 35 : { return FlxqXQ_pow_pre(x, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2002 :
2003 : /* x over Fq, return lift(x^n) mod S */
2004 : GEN
2005 82153 : FlxqXQ_powu_pre(GEN x, ulong n, GEN S, GEN T, ulong p, ulong pi)
2006 : {
2007 82153 : pari_sp av = avma;
2008 : struct _FlxqXQ D;
2009 82153 : switch(n)
2010 : {
2011 0 : case 0: return pol1_FlxX(get_FlxqX_var(S),get_Flx_var(T));
2012 7678 : case 1: return gcopy(x);
2013 441 : case 2: return FlxqXQ_sqr_pre(x, S, T, p, pi);
2014 : }
2015 74034 : T = Flx_get_red_pre(T, p, pi);
2016 74034 : S = FlxqX_get_red_pre(S, T, p, pi);
2017 74034 : D.S = S; D.T = T; D.p = p; D.pi = pi;
2018 74034 : x = gen_powu_i(x, n, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul);
2019 74034 : return gerepilecopy(av, x);
2020 : }
2021 : GEN
2022 0 : FlxqXQ_powu(GEN x, ulong n, GEN S, GEN T, ulong p)
2023 0 : { return FlxqXQ_powu_pre(x, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2024 :
2025 : GEN
2026 108743 : FlxqXQ_powers_pre(GEN x, long l, GEN S, GEN T, ulong p, ulong pi)
2027 : {
2028 : struct _FlxqXQ D;
2029 108743 : int use_sqr = 2*degpol(x) >= get_FlxqX_degree(S);
2030 108743 : T = Flx_get_red_pre(T, p, pi);
2031 108743 : S = FlxqX_get_red_pre(S, T, p, pi);
2032 108743 : D.S = S; D.T = T; D.p = p; D.pi = pi;
2033 108743 : return gen_powers(x, l, use_sqr, (void*)&D, &_FlxqXQ_sqr, &_FlxqXQ_mul,&_FlxqXQ_one);
2034 : }
2035 : GEN
2036 0 : FlxqXQ_powers(GEN x, long l, GEN S, GEN T, ulong p)
2037 0 : { return FlxqXQ_powers_pre(x, l, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2038 :
2039 : /* Let v a linear form, return the linear form z->v(tau*z)
2040 : that is, v*(M_tau) */
2041 : static GEN
2042 640 : FlxqXQ_transmul_init(GEN tau, GEN S, GEN T, ulong p, ulong pi)
2043 : {
2044 : GEN bht;
2045 640 : GEN h, Sp = get_FlxqX_red(S, &h);
2046 640 : long n = degpol(Sp), vS = varn(Sp), vT = get_Flx_var(T);
2047 640 : GEN ft = FlxX_recipspec(Sp+2, n+1, n+1, vT);
2048 640 : GEN bt = FlxX_recipspec(tau+2, lgpol(tau), n, vT);
2049 640 : setvarn(ft, vS); setvarn(bt, vS);
2050 640 : if (h)
2051 0 : bht = FlxqXn_mul_pre(bt, h, n-1, T, p, pi);
2052 : else
2053 : {
2054 640 : GEN bh = FlxqX_div_pre(FlxX_shift(tau, n-1, vT), S, T, p, pi);
2055 640 : bht = FlxX_recipspec(bh+2, lgpol(bh), n-1, vT);
2056 640 : setvarn(bht, vS);
2057 : }
2058 640 : return mkvec3(bt, bht, ft);
2059 : }
2060 :
2061 : static GEN
2062 1364 : FlxqXQ_transmul(GEN tau, GEN a, long n, GEN T, ulong p, ulong pi)
2063 : {
2064 1364 : pari_sp ltop = avma;
2065 : GEN t1, t2, t3, vec;
2066 1364 : GEN bt = gel(tau, 1), bht = gel(tau, 2), ft = gel(tau, 3);
2067 1364 : long vT = get_Flx_var(T);
2068 1364 : if (signe(a)==0) return pol_0(varn(a));
2069 1309 : t2 = FlxX_shift(FlxqX_mul_pre(bt, a, T, p, pi),1-n,vT);
2070 1309 : if (signe(bht)==0) return gerepilecopy(ltop, t2);
2071 854 : t1 = FlxX_shift(FlxqX_mul_pre(ft, a, T, p, pi),-n,vT);
2072 854 : t3 = FlxqXn_mul_pre(t1, bht, n-1, T, p, pi);
2073 854 : vec = FlxX_sub(t2, FlxX_shift(t3, 1, vT), p);
2074 854 : return gerepileupto(ltop, vec);
2075 : }
2076 :
2077 : static GEN
2078 320 : polxn_FlxX(long n, long v, long vT)
2079 : {
2080 320 : long i, a = n+2;
2081 320 : GEN p = cgetg(a+1, t_POL);
2082 320 : p[1] = evalsigne(1)|evalvarn(v);
2083 2776 : for (i = 2; i < a; i++) gel(p,i) = pol0_Flx(vT);
2084 320 : gel(p,a) = pol1_Flx(vT); return p;
2085 : }
2086 :
2087 : GEN
2088 262 : FlxqXQ_minpoly_pre(GEN x, GEN S, GEN T, ulong p, ulong pi)
2089 : {
2090 262 : pari_sp ltop = avma;
2091 : long vS, vT, n;
2092 : GEN v_x, g, tau;
2093 262 : vS = get_FlxqX_var(S);
2094 262 : vT = get_Flx_var(T);
2095 262 : n = get_FlxqX_degree(S);
2096 262 : g = pol1_FlxX(vS,vT);
2097 262 : tau = pol1_FlxX(vS,vT);
2098 262 : S = FlxqX_get_red_pre(S, T, p, pi);
2099 262 : v_x = FlxqXQ_powers_pre(x, usqrt(2*n), S, T, p, pi);
2100 582 : while(signe(tau) != 0)
2101 : {
2102 : long i, j, m, k1;
2103 : GEN M, v, tr;
2104 : GEN g_prime, c;
2105 320 : if (degpol(g) == n) { tau = pol1_FlxX(vS, vT); g = pol1_FlxX(vS, vT); }
2106 320 : v = random_FlxqX(n, vS, T, p);
2107 320 : tr = FlxqXQ_transmul_init(tau, S, T, p, pi);
2108 320 : v = FlxqXQ_transmul(tr, v, n, T, p, pi);
2109 320 : m = 2*(n-degpol(g));
2110 320 : k1 = usqrt(m);
2111 320 : tr = FlxqXQ_transmul_init(gel(v_x,k1+1), S, T, p, pi);
2112 320 : c = cgetg(m+2,t_POL);
2113 320 : c[1] = evalsigne(1)|evalvarn(vS);
2114 1364 : for (i=0; i<m; i+=k1)
2115 : {
2116 1044 : long mj = minss(m-i, k1);
2117 3500 : for (j=0; j<mj; j++)
2118 2456 : gel(c,m+1-(i+j)) = FlxqX_dotproduct(v, gel(v_x,j+1), T, p);
2119 1044 : v = FlxqXQ_transmul(tr, v, n, T, p, pi);
2120 : }
2121 320 : c = FlxX_renormalize(c, m+2);
2122 : /* now c contains <v,x^i> , i = 0..m-1 */
2123 320 : M = FlxqX_halfgcd_pre(polxn_FlxX(m, vS, vT), c, T, p, pi);
2124 320 : g_prime = gmael(M, 2, 2);
2125 320 : if (degpol(g_prime) < 1) continue;
2126 279 : g = FlxqX_mul_pre(g, g_prime, T, p, pi);
2127 279 : tau = FlxqXQ_mul_pre(tau, FlxqX_FlxqXQV_eval_pre(g_prime, v_x, S, T,p,pi),
2128 : S, T, p,pi);
2129 : }
2130 262 : g = FlxqX_normalize_pre(g,T,p,pi);
2131 262 : return gerepilecopy(ltop,g);
2132 : }
2133 : GEN
2134 62 : FlxqXQ_minpoly(GEN x, GEN S, GEN T, ulong p)
2135 62 : { return FlxqXQ_minpoly_pre(x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2136 :
2137 : GEN
2138 0 : FlxqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, ulong p)
2139 0 : { return FlxXV_to_FlxM(FlxqXQ_powers(y,m-1,S,T,p), n, get_Flx_var(T)); }
2140 :
2141 : static GEN
2142 139284 : FlxX_blocks_FlxM(GEN P, long n, long m, long v)
2143 : {
2144 139284 : GEN z = cgetg(m+1,t_MAT);
2145 139284 : long i,j, k=2, l = lg(P);
2146 535843 : for(i=1; i<=m; i++)
2147 : {
2148 396559 : GEN zi = cgetg(n+1,t_COL);
2149 396559 : gel(z,i) = zi;
2150 1270321 : for(j=1; j<=n; j++)
2151 873762 : gel(zi, j) = k==l ? pol0_Flx(v) : gel(P,k++);
2152 : }
2153 139284 : return z;
2154 : }
2155 :
2156 : GEN
2157 139284 : FlxqX_FlxqXQV_eval_pre(GEN Q, GEN x, GEN S, GEN T, ulong p, ulong pi)
2158 : {
2159 139284 : pari_sp btop, av = avma;
2160 139284 : long v = get_FlxqX_var(S), m = get_FlxqX_degree(S);
2161 139284 : long vT = get_Flx_var(T);
2162 139284 : long i, l = lg(x)-1, lQ = lgpol(Q), n, d;
2163 : GEN A, B, C, R, g;
2164 139284 : if (lQ == 0) return pol_0(v);
2165 139284 : if (lQ <= l)
2166 : {
2167 45289 : n = l;
2168 45289 : d = 1;
2169 : }
2170 : else
2171 : {
2172 93995 : n = l-1;
2173 93995 : d = (lQ+n-1)/n;
2174 : }
2175 139284 : A = FlxXV_to_FlxM_lg(x, m, n, vT);
2176 139284 : B = FlxX_blocks_FlxM(Q, n, d, vT);
2177 139284 : C = gerepileupto(av, FlxqM_mul(A, B, T, p));
2178 139284 : g = gel(x, l);
2179 139284 : T = Flx_get_red_pre(T, p, pi);
2180 139284 : S = FlxqX_get_red_pre(S, T, p, pi);
2181 139284 : btop = avma;
2182 139284 : R = FlxV_to_FlxX(gel(C, d), v);
2183 396559 : for (i = d-1; i>0; i--)
2184 : {
2185 257275 : R = FlxX_add(FlxqXQ_mul_pre(R, g, S, T,p,pi), FlxV_to_FlxX(gel(C,i), v), p);
2186 257275 : if (gc_needed(btop,1))
2187 6 : R = gerepileupto(btop, R);
2188 : }
2189 139284 : return gerepilecopy(av, R);
2190 : }
2191 : GEN
2192 0 : FlxqX_FlxqXQV_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
2193 0 : { return FlxqX_FlxqXQV_eval_pre(Q,x,S,T,p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2194 :
2195 : GEN
2196 76763 : FlxqX_FlxqXQ_eval_pre(GEN Q, GEN x, GEN S, GEN T, ulong p, ulong pi)
2197 : {
2198 76763 : pari_sp av = avma;
2199 : GEN z, V;
2200 76763 : long d = degpol(Q), rtd;
2201 76763 : if (d < 0) return pol_0(get_FlxqX_var(S));
2202 76763 : rtd = (long) sqrt((double)d);
2203 76763 : T = Flx_get_red_pre(T, p, pi);
2204 76763 : S = FlxqX_get_red_pre(S, T, p, pi);
2205 76763 : V = FlxqXQ_powers_pre(x, rtd, S, T, p, pi);
2206 76763 : z = FlxqX_FlxqXQV_eval_pre(Q, V, S, T, p, pi);
2207 76763 : return gerepileupto(av, z);
2208 : }
2209 : GEN
2210 0 : FlxqX_FlxqXQ_eval(GEN Q, GEN x, GEN S, GEN T, ulong p)
2211 0 : { return FlxqX_FlxqXQ_eval_pre(Q, x, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2212 :
2213 : GEN
2214 0 : FlxqXC_FlxqXQV_eval_pre(GEN x, GEN v, GEN S, GEN T, ulong p, ulong pi)
2215 0 : { pari_APPLY_type(t_COL, FlxqX_FlxqXQV_eval_pre(gel(x,i), v, S, T, p, pi)) }
2216 : GEN
2217 0 : FlxqXC_FlxqXQV_eval(GEN x, GEN v, GEN S, GEN T, ulong p)
2218 0 : { return FlxqXC_FlxqXQV_eval_pre(x, v, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2219 :
2220 : GEN
2221 0 : FlxqXC_FlxqXQ_eval(GEN x, GEN F, GEN S, GEN T, ulong p)
2222 : {
2223 0 : long d = brent_kung_optpow(get_FlxqX_degree(S)-1,lg(x)-1,1);
2224 0 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
2225 0 : GEN Fp = FlxqXQ_powers_pre(F, d, S, T, p, pi);
2226 0 : return FlxqXC_FlxqXQV_eval_pre(x, Fp, S, T, p, pi);
2227 : }
2228 :
2229 : static GEN
2230 72666 : FlxqXQ_autpow_sqr(void * E, GEN x)
2231 : {
2232 72666 : struct _FlxqXQ *D = (struct _FlxqXQ *)E;
2233 72666 : GEN S = D->S, T = D->T;
2234 72666 : ulong p = D->p, pi = D->pi;
2235 72666 : GEN phi = gel(x,1), S1 = gel(x,2);
2236 72666 : long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
2237 72666 : GEN V = Flxq_powers_pre(phi, n, T, p, pi);
2238 72666 : GEN phi2 = Flx_FlxqV_eval_pre(phi, V, T, p, pi);
2239 72666 : GEN Sphi = FlxY_FlxqV_evalx_pre(S1, V, T, p, pi);
2240 72666 : GEN S2 = FlxqX_FlxqXQ_eval_pre(Sphi, S1, S, T, p, pi);
2241 72666 : return mkvec2(phi2, S2);
2242 : }
2243 :
2244 : static GEN
2245 3735 : FlxqXQ_autpow_mul(void * E, GEN x, GEN y)
2246 : {
2247 3735 : struct _FlxqXQ *D = (struct _FlxqXQ *)E;
2248 3735 : GEN S = D->S, T = D->T;
2249 3735 : ulong p = D->p, pi = D->pi;
2250 3735 : GEN phi1 = gel(x,1), S1 = gel(x,2);
2251 3735 : GEN phi2 = gel(y,1), S2 = gel(y,2);
2252 3735 : long n = brent_kung_optpow(get_Flx_degree(T)-1,lgpol(S1)+1,1);
2253 3735 : GEN V = Flxq_powers_pre(phi2, n, T, p, pi);
2254 3735 : GEN phi3 = Flx_FlxqV_eval_pre(phi1, V, T, p, pi);
2255 3735 : GEN Sphi = FlxY_FlxqV_evalx_pre(S1, V, T, p, pi);
2256 3735 : GEN S3 = FlxqX_FlxqXQ_eval_pre(Sphi, S2, S, T, p, pi);
2257 3735 : return mkvec2(phi3, S3);
2258 : }
2259 :
2260 : GEN
2261 69303 : FlxqXQ_autpow_pre(GEN aut, long n, GEN S, GEN T, ulong p, ulong pi)
2262 : {
2263 69303 : pari_sp av = avma;
2264 : struct _FlxqXQ D;
2265 69303 : T = Flx_get_red_pre(T, p, pi);
2266 69303 : S = FlxqX_get_red_pre(S, T, p, pi);
2267 69303 : D.S = S; D.T = T; D.p = p; D.pi = pi;
2268 69303 : aut = gen_powu_i(aut,n,&D,FlxqXQ_autpow_sqr,FlxqXQ_autpow_mul);
2269 69303 : return gerepilecopy(av, aut);
2270 : }
2271 : GEN
2272 0 : FlxqXQ_autpow(GEN aut, long n, GEN S, GEN T, ulong p)
2273 0 : { return FlxqXQ_autpow_pre(aut, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2274 :
2275 : static GEN
2276 30428 : FlxqXQ_autsum_mul(void *E, GEN x, GEN y)
2277 : {
2278 30428 : struct _FlxqXQ *D = (struct _FlxqXQ *)E;
2279 30428 : GEN S = D->S, T = D->T;
2280 30428 : ulong p = D->p, pi = D->pi;
2281 30428 : GEN phi1 = gel(x,1), S1 = gel(x,2), a1 = gel(x,3);
2282 30428 : GEN phi2 = gel(y,1), S2 = gel(y,2), a2 = gel(y,3);
2283 30428 : long n2 = brent_kung_optpow(get_Flx_degree(T)-1, lgpol(S1)+lgpol(a1)+1,1);
2284 30428 : GEN V2 = Flxq_powers_pre(phi2, n2, T, p, pi);
2285 30428 : GEN phi3 = Flx_FlxqV_eval_pre(phi1, V2, T, p, pi);
2286 30428 : GEN Sphi = FlxY_FlxqV_evalx_pre(S1, V2, T, p, pi);
2287 30428 : GEN aphi = FlxY_FlxqV_evalx_pre(a1, V2, T, p, pi);
2288 30428 : long n = brent_kung_optpow(maxss(degpol(Sphi),degpol(aphi)),2,1);
2289 30428 : GEN V = FlxqXQ_powers_pre(S2, n, S, T, p, pi);
2290 30428 : GEN S3 = FlxqX_FlxqXQV_eval_pre(Sphi, V, S, T, p, pi);
2291 30428 : GEN aS = FlxqX_FlxqXQV_eval_pre(aphi, V, S, T, p, pi);
2292 30428 : GEN a3 = FlxqXQ_mul_pre(aS, a2, S, T, p, pi);
2293 30428 : return mkvec3(phi3, S3, a3);
2294 : }
2295 :
2296 : static GEN
2297 18841 : FlxqXQ_autsum_sqr(void * T, GEN x)
2298 18841 : { return FlxqXQ_autsum_mul(T, x, x); }
2299 :
2300 : GEN
2301 12192 : FlxqXQ_autsum_pre(GEN aut, long n, GEN S, GEN T, ulong p, ulong pi)
2302 : {
2303 12192 : pari_sp av = avma;
2304 : struct _FlxqXQ D;
2305 12192 : T = Flx_get_red_pre(T, p, pi);
2306 12192 : S = FlxqX_get_red_pre(S, T, p, pi);
2307 12192 : D.S=S; D.T=T; D.p=p; D.pi=pi;
2308 12192 : aut = gen_powu_i(aut,n,&D,FlxqXQ_autsum_sqr,FlxqXQ_autsum_mul);
2309 12192 : return gerepilecopy(av, aut);
2310 : }
2311 : GEN
2312 0 : FlxqXQ_autsum(GEN aut, long n, GEN S, GEN T, ulong p)
2313 0 : { return FlxqXQ_autsum_pre(aut, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2314 :
2315 : static GEN
2316 27 : FlxqXQ_auttrace_mul(void *E, GEN x, GEN y)
2317 : {
2318 27 : struct _FlxqXQ *D = (struct _FlxqXQ *)E;
2319 27 : GEN S = D->S, T = D->T;
2320 27 : ulong p = D->p, pi = D->pi;
2321 27 : GEN S1 = gel(x,1), a1 = gel(x,2);
2322 27 : GEN S2 = gel(y,1), a2 = gel(y,2);
2323 27 : long n = brent_kung_optpow(maxss(degpol(S1),degpol(a1)),2,1);
2324 27 : GEN V = FlxqXQ_powers_pre(S2, n, S, T, p, pi);
2325 27 : GEN S3 = FlxqX_FlxqXQV_eval_pre(S1, V, S, T, p, pi);
2326 27 : GEN aS = FlxqX_FlxqXQV_eval_pre(a1, V, S, T, p, pi);
2327 27 : GEN a3 = FlxX_add(aS, a2, p);
2328 27 : return mkvec2(S3, a3);
2329 : }
2330 :
2331 : static GEN
2332 20 : FlxqXQ_auttrace_sqr(void *E, GEN x)
2333 20 : { return FlxqXQ_auttrace_mul(E, x, x); }
2334 :
2335 : GEN
2336 375 : FlxqXQ_auttrace_pre(GEN x, ulong n, GEN S, GEN T, ulong p, ulong pi)
2337 : {
2338 375 : pari_sp av = avma;
2339 : struct _FlxqXQ D;
2340 375 : T = Flx_get_red_pre(T, p, pi);
2341 375 : S = FlxqX_get_red_pre(S, T, p, pi);
2342 375 : D.S=S; D.T=T; D.p=p; D.pi = pi;
2343 375 : x = gen_powu_i(x,n,(void*)&D,FlxqXQ_auttrace_sqr,FlxqXQ_auttrace_mul);
2344 375 : return gerepilecopy(av, x);
2345 : }
2346 : GEN
2347 0 : FlxqXQ_auttrace(GEN x, ulong n, GEN S, GEN T, ulong p)
2348 0 : { return FlxqXQ_auttrace_pre(x, n, S, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2349 :
2350 : /*******************************************************************/
2351 : /* */
2352 : /* FlxYqQ */
2353 : /* */
2354 : /*******************************************************************/
2355 :
2356 : /*Preliminary implementation to speed up FpX_ffisom*/
2357 : typedef struct {
2358 : GEN S, T;
2359 : ulong p, pi;
2360 : } FlxYqq_muldata;
2361 :
2362 : /* reduce x in Fl[X, Y] in the algebra Fl[X, Y]/ (P(X),Q(Y)) */
2363 : static GEN
2364 90743 : FlxYqq_redswap(GEN x, GEN S, GEN T, ulong p, ulong pi)
2365 : {
2366 90743 : pari_sp ltop=avma;
2367 90743 : long n = get_Flx_degree(S);
2368 90743 : long m = get_Flx_degree(T);
2369 90743 : long w = get_Flx_var(T);
2370 90743 : GEN V = FlxX_swap(x,m,w);
2371 90741 : V = FlxqX_red_pre(V,S,p,pi);
2372 90737 : V = FlxX_swap(V,n,w);
2373 90741 : return gerepilecopy(ltop,V);
2374 : }
2375 : static GEN
2376 80428 : FlxYqq_sqr(void *data, GEN x)
2377 : {
2378 80428 : FlxYqq_muldata *D = (FlxYqq_muldata*)data;
2379 80428 : return FlxYqq_redswap(FlxqX_sqr_pre(x,D->T,D->p,D->pi),D->S,D->T,D->p,D->pi);
2380 : }
2381 :
2382 : static GEN
2383 10316 : FlxYqq_mul(void *data, GEN x, GEN y)
2384 : {
2385 10316 : FlxYqq_muldata *D = (FlxYqq_muldata*)data;
2386 10316 : return FlxYqq_redswap(FlxqX_mul_pre(x,y, D->T,D->p,D->pi),D->S,D->T,D->p,D->pi);
2387 : }
2388 :
2389 : /* x in Z[X,Y], S in Z[X] over Fq = Z[Y]/(p,T); compute lift(x^n mod (S,T,p)) */
2390 : GEN
2391 39197 : FlxYqq_pow(GEN x, GEN n, GEN S, GEN T, ulong p)
2392 : {
2393 : FlxYqq_muldata D;
2394 39197 : D.S = S; D.T = T; D.p = p; D.pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
2395 39197 : return gen_pow(x, n, (void*)&D, &FlxYqq_sqr, &FlxYqq_mul);
2396 : }
2397 :
2398 : /*******************************************************************/
2399 : /* */
2400 : /* FlxqXn */
2401 : /* */
2402 : /*******************************************************************/
2403 :
2404 : GEN
2405 65508 : FlxXn_red(GEN a, long n)
2406 : {
2407 65508 : long i, L = n+2, l = lg(a);
2408 : GEN b;
2409 65508 : if (L >= l) return a; /* deg(x) < n */
2410 36555 : b = cgetg(L, t_POL); b[1] = a[1];
2411 391617 : for (i=2; i<L; i++) gel(b,i) = gel(a,i);
2412 36556 : return FlxX_renormalize(b,L);
2413 : }
2414 :
2415 : GEN
2416 39970 : FlxqXn_mul_pre(GEN a, GEN b, long n, GEN T, ulong p, ulong pi)
2417 39970 : { return FlxXn_red(FlxqX_mul_pre(a, b, T, p, pi), n); }
2418 : GEN
2419 0 : FlxqXn_mul(GEN a, GEN b, long n, GEN T, ulong p)
2420 0 : { return FlxqXn_mul_pre(a, b, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2421 :
2422 : GEN
2423 0 : FlxqXn_sqr_pre(GEN a, long n, GEN T, ulong p, ulong pi)
2424 0 : { return FlxXn_red(FlxqX_sqr_pre(a, T, p, pi), n); }
2425 : GEN
2426 0 : FlxqXn_sqr(GEN a, long n, GEN T, ulong p)
2427 0 : { return FlxqXn_sqr_pre(a, n, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2428 :
2429 : /* (f*g) \/ x^n */
2430 : static GEN
2431 17768 : FlxqX_mulhigh_i(GEN f, GEN g, long n, GEN T, ulong p, ulong pi)
2432 17768 : { return FlxX_shift(FlxqX_mul_pre(f, g, T, p, pi), -n , get_Flx_var(T)); }
2433 :
2434 : static GEN
2435 13028 : FlxqXn_mulhigh(GEN f, GEN g, long n2, long n, GEN T, ulong p, ulong pi)
2436 : {
2437 13028 : long vT = get_Flx_var(T);
2438 13028 : GEN F = FlxX_blocks(f, n2, 2, vT), fl = gel(F,1), fh = gel(F,2);
2439 13027 : return FlxX_add(FlxqX_mulhigh_i(fl, g, n2, T, p, pi),
2440 : FlxqXn_mul_pre(fh, g, n - n2, T, p, pi), p);
2441 : }
2442 :
2443 : GEN
2444 2390 : FlxqXn_inv_pre(GEN f, long e, GEN T, ulong p, ulong pi)
2445 : {
2446 2390 : pari_sp av = avma, av2;
2447 : ulong mask;
2448 : GEN W, a;
2449 2390 : long v = varn(f), n = 1, vT = get_Flx_var(T);
2450 :
2451 2390 : if (!signe(f)) pari_err_INV("FlxqXn_inv",f);
2452 2390 : a = Flxq_inv_pre(gel(f,2), T, p, pi);
2453 2390 : if (e == 1) return scalarpol(a, v);
2454 2390 : else if (e == 2)
2455 : {
2456 : GEN b;
2457 0 : if (degpol(f) <= 0) return scalarpol(a, v);
2458 0 : b = Flx_neg(gel(f,3), p);
2459 0 : if (lgpol(b)==0) return scalarpol(a, v);
2460 0 : b = Flxq_mul_pre(b, Flxq_sqr_pre(a, T, p, pi), T, p, pi);
2461 0 : W = deg1pol_shallow(b, a, v);
2462 0 : return gerepilecopy(av, W);
2463 : }
2464 2390 : W = scalarpol_shallow(Flxq_inv_pre(gel(f,2), T, p, pi), v);
2465 2390 : mask = quadratic_prec_mask(e);
2466 2390 : av2 = avma;
2467 11872 : for (;mask>1;)
2468 : {
2469 : GEN u, fr;
2470 9482 : long n2 = n;
2471 9482 : n<<=1; if (mask & 1) n--;
2472 9482 : mask >>= 1;
2473 9482 : fr = FlxXn_red(f, n);
2474 9482 : u = FlxqXn_mul_pre(W, FlxqXn_mulhigh(fr, W, n2, n, T,p,pi), n-n2, T,p,pi);
2475 9482 : W = FlxX_sub(W, FlxX_shift(u, n2, vT), p);
2476 9482 : if (gc_needed(av2,2))
2477 : {
2478 0 : if(DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_inv, e = %ld", n);
2479 0 : W = gerepileupto(av2, W);
2480 : }
2481 : }
2482 2390 : return gerepileupto(av, W);
2483 : }
2484 : GEN
2485 0 : FlxqXn_inv(GEN f, long e, GEN T, ulong p)
2486 0 : { return FlxqXn_inv_pre(f, e, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
2487 :
2488 : /* Compute intformal(x^n*S)/x^(n+1) */
2489 : static GEN
2490 4741 : FlxX_integXn(GEN x, long n, ulong p)
2491 : {
2492 4741 : long i, lx = lg(x);
2493 : GEN y;
2494 4741 : if (lx == 2) return gcopy(x);
2495 3949 : y = cgetg(lx, t_POL); y[1] = x[1];
2496 26571 : for (i=2; i<lx; i++)
2497 : {
2498 22622 : GEN xi = gel(x,i);
2499 22622 : gel(y,i) = Flx_Fl_mul(xi, Fl_inv((n+i-1)%p, p), p);
2500 : }
2501 3949 : return FlxX_renormalize(y, lx);;
2502 : }
2503 :
2504 : GEN
2505 1195 : FlxqXn_expint_pre(GEN h, long e, GEN T, ulong p, ulong pi)
2506 : {
2507 1195 : pari_sp av = avma, av2;
2508 1195 : long v = varn(h), n = 1, vT = get_Flx_var(T);
2509 1195 : GEN f = pol1_FlxX(v, vT), g = pol1_FlxX(v, vT);
2510 1195 : ulong mask = quadratic_prec_mask(e);
2511 1195 : av2 = avma;
2512 4740 : for (;mask>1;)
2513 : {
2514 : GEN u, w;
2515 4740 : long n2 = n;
2516 4740 : n<<=1; if (mask & 1) n--;
2517 4740 : mask >>= 1;
2518 4740 : u = FlxqXn_mul_pre(g, FlxqX_mulhigh_i(f, FlxXn_red(h, n2-1), n2-1, T,p,pi), n-n2, T,p,pi);
2519 4741 : u = FlxX_add(u, FlxX_shift(FlxXn_red(h, n-1), 1-n2, vT), p);
2520 4741 : w = FlxqXn_mul_pre(f, FlxX_integXn(u, n2-1, p), n-n2, T, p, pi);
2521 4741 : f = FlxX_add(f, FlxX_shift(w, n2, vT), p);
2522 4741 : if (mask<=1) break;
2523 3546 : u = FlxqXn_mul_pre(g, FlxqXn_mulhigh(f, g, n2, n, T,p,pi), n-n2, T,p,pi);
2524 3546 : g = FlxX_sub(g, FlxX_shift(u, n2, vT), p);
2525 3545 : if (gc_needed(av2,2))
2526 : {
2527 0 : if (DEBUGMEM>1) pari_warn(warnmem,"FlxqXn_exp, e = %ld", n);
2528 0 : gerepileall(av2, 2, &f, &g);
2529 : }
2530 : }
2531 1195 : return gerepileupto(av, f);
2532 : }
2533 : GEN
2534 0 : FlxqXn_expint(GEN h, long e, GEN T, ulong p)
2535 0 : { return FlxqXn_expint_pre(h, e, T, p, SMALL_ULONG(p)? 0: get_Fl_red(p)); }
|