Line data Source code
1 : /* Copyright (C) 2000-2005 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 : /***********************************************************************/
16 : /** **/
17 : /** ARITHMETIC OPERATIONS ON POLYNOMIALS **/
18 : /** (third part) **/
19 : /** **/
20 : /***********************************************************************/
21 : #include "pari.h"
22 : #include "paripriv.h"
23 :
24 : #define DEBUGLEVEL DEBUGLEVEL_pol
25 :
26 : /************************************************************************
27 : ** **
28 : ** Ring membership **
29 : ** **
30 : ************************************************************************/
31 : struct charact {
32 : GEN q;
33 : int isprime;
34 : };
35 : static void
36 1225 : char_update_prime(struct charact *S, GEN p)
37 : {
38 1225 : if (!S->isprime) { S->isprime = 1; S->q = p; }
39 1225 : if (!equalii(p, S->q)) pari_err_MODULUS("characteristic", S->q, p);
40 1218 : }
41 : static void
42 6580 : char_update_int(struct charact *S, GEN n)
43 : {
44 6580 : if (S->isprime)
45 : {
46 7 : if (dvdii(n, S->q)) return;
47 7 : pari_err_MODULUS("characteristic", S->q, n);
48 : }
49 6573 : S->q = gcdii(S->q, n);
50 : }
51 : static void
52 178689 : charact(struct charact *S, GEN x)
53 : {
54 178689 : const long tx = typ(x);
55 : long i, l;
56 178689 : switch(tx)
57 : {
58 5131 : case t_INTMOD:char_update_int(S, gel(x,1)); break;
59 1134 : case t_FFELT: char_update_prime(S, gel(x,4)); break;
60 26635 : case t_COMPLEX: case t_QUAD:
61 : case t_POLMOD: case t_POL: case t_SER: case t_RFRAC:
62 : case t_VEC: case t_COL: case t_MAT:
63 26635 : l = lg(x);
64 177730 : for (i=lontyp[tx]; i < l; i++) charact(S,gel(x,i));
65 26621 : break;
66 7 : case t_LIST:
67 7 : x = list_data(x);
68 7 : if (x) charact(S, x);
69 7 : break;
70 : }
71 178661 : }
72 : static void
73 4634 : charact_res(struct charact *S, GEN x)
74 : {
75 4634 : const long tx = typ(x);
76 : long i, l;
77 4634 : switch(tx)
78 : {
79 1449 : case t_INTMOD:char_update_int(S, gel(x,1)); break;
80 0 : case t_FFELT: char_update_prime(S, gel(x,4)); break;
81 91 : case t_PADIC: char_update_prime(S, gel(x,2)); break;
82 1617 : case t_COMPLEX: case t_QUAD:
83 : case t_POLMOD: case t_POL: case t_SER: case t_RFRAC:
84 : case t_VEC: case t_COL: case t_MAT:
85 1617 : l = lg(x);
86 5922 : for (i=lontyp[tx]; i < l; i++) charact_res(S,gel(x,i));
87 1617 : break;
88 0 : case t_LIST:
89 0 : x = list_data(x);
90 0 : if (x) charact_res(S, x);
91 0 : break;
92 : }
93 4634 : }
94 : GEN
95 27580 : characteristic(GEN x)
96 : {
97 : struct charact S;
98 27580 : S.q = gen_0; S.isprime = 0;
99 27580 : charact(&S, x); return S.q;
100 : }
101 : GEN
102 329 : residual_characteristic(GEN x)
103 : {
104 : struct charact S;
105 329 : S.q = gen_0; S.isprime = 0;
106 329 : charact_res(&S, x); return S.q;
107 : }
108 :
109 : int
110 71049126 : Rg_is_Fp(GEN x, GEN *pp)
111 : {
112 : GEN mod;
113 71049126 : switch(typ(x))
114 : {
115 2483628 : case t_INTMOD:
116 2483628 : mod = gel(x,1);
117 2483628 : if (!*pp) *pp = mod;
118 2342732 : else if (mod != *pp && !equalii(mod, *pp))
119 : {
120 0 : if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_Fp");
121 0 : return 0;
122 : }
123 2483628 : return 1;
124 57159775 : case t_INT:
125 57159775 : return 1;
126 11405723 : default: return 0;
127 : }
128 : }
129 :
130 : int
131 28175412 : RgX_is_FpX(GEN x, GEN *pp)
132 : {
133 28175412 : long i, lx = lg(x);
134 87792714 : for (i=2; i<lx; i++)
135 71023033 : if (!Rg_is_Fp(gel(x, i), pp))
136 11405722 : return 0;
137 16769681 : return 1;
138 : }
139 :
140 : int
141 0 : RgV_is_FpV(GEN x, GEN *pp)
142 : {
143 0 : long i, lx = lg(x);
144 0 : for (i=1; i<lx; i++)
145 0 : if (!Rg_is_Fp(gel(x,i), pp)) return 0;
146 0 : return 1;
147 : }
148 :
149 : int
150 0 : RgM_is_FpM(GEN x, GEN *pp)
151 : {
152 0 : long i, lx = lg(x);
153 0 : for (i=1; i<lx; i++)
154 0 : if (!RgV_is_FpV(gel(x, i), pp)) return 0;
155 0 : return 1;
156 : }
157 :
158 : int
159 60571 : Rg_is_FpXQ(GEN x, GEN *pT, GEN *pp)
160 : {
161 : GEN pol, mod, p;
162 60571 : switch(typ(x))
163 : {
164 26089 : case t_INTMOD:
165 26089 : return Rg_is_Fp(x, pp);
166 8372 : case t_INT:
167 8372 : return 1;
168 21 : case t_POL:
169 21 : return RgX_is_FpX(x, pp);
170 21350 : case t_FFELT:
171 21350 : mod = x; p = FF_p_i(x);
172 21350 : if (!*pp) *pp = p;
173 21350 : if (!*pT) *pT = mod;
174 19824 : else if (typ(*pT)!=t_FFELT || !FF_samefield(*pT,mod))
175 : {
176 42 : if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_FpXQ");
177 42 : return 0;
178 : }
179 21308 : return 1;
180 4585 : case t_POLMOD:
181 4585 : mod = gel(x,1); pol = gel(x, 2);
182 4585 : if (!RgX_is_FpX(mod, pp)) return 0;
183 4585 : if (typ(pol)==t_POL)
184 : {
185 4578 : if (!RgX_is_FpX(pol, pp)) return 0;
186 : }
187 7 : else if (!Rg_is_Fp(pol, pp)) return 0;
188 4585 : if (!*pT) *pT = mod;
189 4431 : else if (mod != *pT && !gequal(mod, *pT))
190 : {
191 0 : if (DEBUGLEVEL) pari_warn(warner,"different moduli in Rg_is_FpXQ");
192 0 : return 0;
193 : }
194 4585 : return 1;
195 :
196 154 : default: return 0;
197 : }
198 : }
199 :
200 : int
201 3353 : RgX_is_FpXQX(GEN x, GEN *pT, GEN *pp)
202 : {
203 3353 : long i, lx = lg(x);
204 63168 : for (i = 2; i < lx; i++)
205 59913 : if (!Rg_is_FpXQ(gel(x,i), pT, pp)) return 0;
206 3255 : return 1;
207 : }
208 :
209 : /************************************************************************
210 : ** **
211 : ** Ring conversion **
212 : ** **
213 : ************************************************************************/
214 :
215 : /* p > 0 a t_INT, return lift(x * Mod(1,p)).
216 : * If x is an INTMOD, assume modulus is a multiple of p. */
217 : GEN
218 51888072 : Rg_to_Fp(GEN x, GEN p)
219 : {
220 51888072 : if (lgefint(p) == 3) return utoi(Rg_to_Fl(x, uel(p,2)));
221 4554735 : switch(typ(x))
222 : {
223 288232 : case t_INT: return modii(x, p);
224 18790 : case t_FRAC: {
225 18790 : pari_sp av = avma;
226 18790 : GEN z = modii(gel(x,1), p);
227 18790 : if (z == gen_0) return gen_0;
228 18785 : return gerepileuptoint(av, remii(mulii(z, Fp_inv(gel(x,2), p)), p));
229 : }
230 70 : case t_PADIC: return padic_to_Fp(x, p);
231 4247643 : case t_INTMOD: {
232 4247643 : GEN q = gel(x,1), a = gel(x,2);
233 4247643 : if (equalii(q, p)) return icopy(a);
234 14 : if (!dvdii(q,p)) pari_err_MODULUS("Rg_to_Fp", q, p);
235 0 : return remii(a, p);
236 : }
237 0 : default: pari_err_TYPE("Rg_to_Fp",x);
238 : return NULL; /* LCOV_EXCL_LINE */
239 : }
240 : }
241 : /* If x is a POLMOD, assume modulus is a multiple of T. */
242 : GEN
243 1291958 : Rg_to_FpXQ(GEN x, GEN T, GEN p)
244 : {
245 1291958 : long ta, tx = typ(x), v = get_FpX_var(T);
246 : GEN a, b;
247 1291958 : if (is_const_t(tx))
248 : {
249 59175 : if (tx == t_FFELT)
250 : {
251 17355 : GEN z = FF_to_FpXQ(x);
252 17355 : setvarn(z, v);
253 17355 : return z;
254 : }
255 41820 : return scalar_ZX(degpol(T)? Rg_to_Fp(x, p): gen_0, v);
256 : }
257 1232783 : switch(tx)
258 : {
259 1230676 : case t_POLMOD:
260 1230676 : b = gel(x,1);
261 1230676 : a = gel(x,2); ta = typ(a);
262 1230676 : if (is_const_t(ta))
263 3885 : return scalar_ZX(degpol(T)? Rg_to_Fp(a, p): gen_0, v);
264 1226791 : b = RgX_to_FpX(b, p); if (varn(b) != v) break;
265 1226791 : a = RgX_to_FpX(a, p);
266 1226791 : if (ZX_equal(b,get_FpX_mod(T)) || signe(FpX_rem(b,T,p))==0)
267 1226791 : return FpX_rem(a, T, p);
268 0 : break;
269 2107 : case t_POL:
270 2107 : if (varn(x) != v) break;
271 2100 : return FpX_rem(RgX_to_FpX(x,p), T, p);
272 0 : case t_RFRAC:
273 0 : a = Rg_to_FpXQ(gel(x,1), T,p);
274 0 : b = Rg_to_FpXQ(gel(x,2), T,p);
275 0 : return FpXQ_div(a,b, T,p);
276 : }
277 7 : pari_err_TYPE("Rg_to_FpXQ",x);
278 : return NULL; /* LCOV_EXCL_LINE */
279 : }
280 : GEN
281 3335260 : RgX_to_FpX(GEN x, GEN p)
282 : {
283 : long i, l;
284 3335260 : GEN z = cgetg_copy(x, &l); z[1] = x[1];
285 14763859 : for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
286 3335260 : return FpX_renormalize(z, l);
287 : }
288 :
289 : GEN
290 140 : RgV_to_FpV(GEN x, GEN p)
291 483 : { pari_APPLY_type(t_VEC, Rg_to_Fp(gel(x,i), p)) }
292 :
293 : GEN
294 1685157 : RgC_to_FpC(GEN x, GEN p)
295 28092759 : { pari_APPLY_type(t_COL, Rg_to_Fp(gel(x,i), p)) }
296 :
297 : GEN
298 210170 : RgM_to_FpM(GEN x, GEN p)
299 1895285 : { pari_APPLY_same(RgC_to_FpC(gel(x,i), p)) }
300 :
301 : GEN
302 369001 : RgV_to_Flv(GEN x, ulong p)
303 1676894 : { pari_APPLY_ulong(Rg_to_Fl(gel(x,i), p)) }
304 :
305 : GEN
306 118296 : RgM_to_Flm(GEN x, ulong p)
307 422998 : { pari_APPLY_same(RgV_to_Flv(gel(x,i), p)) }
308 :
309 : GEN
310 5098 : RgX_to_FpXQX(GEN x, GEN T, GEN p)
311 : {
312 5098 : long i, l = lg(x);
313 5098 : GEN z = cgetg(l, t_POL); z[1] = x[1];
314 43366 : for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T,p);
315 5098 : return FpXQX_renormalize(z, l);
316 : }
317 : GEN
318 49186 : RgX_to_FqX(GEN x, GEN T, GEN p)
319 : {
320 49186 : long i, l = lg(x);
321 49186 : GEN z = cgetg(l, t_POL); z[1] = x[1];
322 49186 : if (T)
323 10990 : for (i = 2; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
324 : else
325 791394 : for (i = 2; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
326 49186 : return FpXQX_renormalize(z, l);
327 : }
328 :
329 : GEN
330 219128 : RgC_to_FqC(GEN x, GEN T, GEN p)
331 : {
332 219128 : long i, l = lg(x);
333 219128 : GEN z = cgetg(l, t_COL);
334 219128 : if (T)
335 1430310 : for (i = 1; i < l; i++) gel(z,i) = Rg_to_FpXQ(gel(x,i), T, p);
336 : else
337 0 : for (i = 1; i < l; i++) gel(z,i) = Rg_to_Fp(gel(x,i), p);
338 219128 : return z;
339 : }
340 :
341 : GEN
342 52325 : RgM_to_FqM(GEN x, GEN T, GEN p)
343 271418 : { pari_APPLY_same(RgC_to_FqC(gel(x, i), T, p)) }
344 :
345 : /* lg(V) > 1 */
346 : GEN
347 851487 : FpXV_FpC_mul(GEN V, GEN W, GEN p)
348 : {
349 851487 : pari_sp av = avma;
350 851487 : long i, l = lg(V);
351 851487 : GEN z = ZX_Z_mul(gel(V,1),gel(W,1));
352 4201029 : for(i=2; i<l; i++)
353 : {
354 3349542 : z = ZX_add(z, ZX_Z_mul(gel(V,i),gel(W,i)));
355 3349542 : if ((i & 7) == 0) z = gerepileupto(av, z);
356 : }
357 851487 : return gerepileupto(av, FpX_red(z,p));
358 : }
359 :
360 : GEN
361 55832 : FqX_Fq_add(GEN y, GEN x, GEN T, GEN p)
362 : {
363 55832 : long i, lz = lg(y);
364 : GEN z;
365 55832 : if (!T) return FpX_Fp_add(y, x, p);
366 8666 : if (lz == 2) return scalarpol(x, varn(y));
367 7952 : z = cgetg(lz,t_POL); z[1] = y[1];
368 7952 : gel(z,2) = Fq_add(gel(y,2),x, T, p);
369 7952 : if (lz == 3) z = FpXX_renormalize(z,lz);
370 : else
371 33145 : for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
372 7952 : return z;
373 : }
374 :
375 : GEN
376 1069 : FqX_Fq_sub(GEN y, GEN x, GEN T, GEN p)
377 : {
378 1069 : long i, lz = lg(y);
379 : GEN z;
380 1069 : if (!T) return FpX_Fp_sub(y, x, p);
381 1069 : if (lz == 2) return scalarpol(x, varn(y));
382 1069 : z = cgetg(lz,t_POL); z[1] = y[1];
383 1069 : gel(z,2) = Fq_sub(gel(y,2), x, T, p);
384 1069 : if (lz == 3) z = FpXX_renormalize(z,lz);
385 : else
386 10275 : for(i=3;i<lz;i++) gel(z,i) = gcopy(gel(y,i));
387 1069 : return z;
388 : }
389 :
390 : GEN
391 149023 : FqX_Fq_mul_to_monic(GEN P, GEN U, GEN T, GEN p)
392 : {
393 : long i, lP;
394 149023 : GEN res = cgetg_copy(P, &lP); res[1] = P[1];
395 918785 : for(i=2; i<lP-1; i++) gel(res,i) = Fq_mul(U,gel(P,i), T,p);
396 149023 : gel(res,lP-1) = gen_1; return res;
397 : }
398 :
399 : GEN
400 38124 : FpXQX_normalize(GEN z, GEN T, GEN p)
401 : {
402 : GEN lc;
403 38124 : if (lg(z) == 2) return z;
404 38110 : lc = leading_coeff(z);
405 38110 : if (typ(lc) == t_POL)
406 : {
407 2138 : if (lg(lc) > 3) /* nonconstant */
408 1873 : return FqX_Fq_mul_to_monic(z, Fq_inv(lc,T,p), T,p);
409 : /* constant */
410 265 : lc = gel(lc,2);
411 265 : z = shallowcopy(z);
412 265 : gel(z, lg(z)-1) = lc;
413 : }
414 : /* lc a t_INT */
415 36237 : if (equali1(lc)) return z;
416 87 : return FqX_Fq_mul_to_monic(z, Fp_inv(lc,p), T,p);
417 : }
418 :
419 : GEN
420 398872 : FqX_eval(GEN x, GEN y, GEN T, GEN p)
421 : {
422 : pari_sp av;
423 : GEN p1, r;
424 398872 : long j, i=lg(x)-1;
425 398872 : if (i<=2)
426 45971 : return (i==2)? Fq_red(gel(x,2), T, p): gen_0;
427 352901 : av=avma; p1=gel(x,i);
428 : /* specific attention to sparse polynomials (see poleval)*/
429 : /*You've guessed it! It's a copy-paste(tm)*/
430 1174019 : for (i--; i>=2; i=j-1)
431 : {
432 821804 : for (j=i; !signe(gel(x,j)); j--)
433 686 : if (j==2)
434 : {
435 490 : if (i!=j) y = Fq_pow(y,utoipos(i-j+1), T, p);
436 490 : return gerepileupto(av, Fq_mul(p1,y, T, p));
437 : }
438 821118 : r = (i==j)? y: Fq_pow(y, utoipos(i-j+1), T, p);
439 821118 : p1 = Fq_add(Fq_mul(p1,r,T,p), gel(x,j), T, p);
440 : }
441 352411 : return gerepileupto(av, p1);
442 : }
443 :
444 : GEN
445 99679 : FqXY_evalx(GEN Q, GEN x, GEN T, GEN p)
446 : {
447 99679 : long i, lb = lg(Q);
448 : GEN z;
449 99679 : if (!T) return FpXY_evalx(Q, x, p);
450 89319 : z = cgetg(lb, t_POL); z[1] = Q[1];
451 474735 : for (i=2; i<lb; i++)
452 : {
453 385416 : GEN q = gel(Q,i);
454 385416 : gel(z,i) = typ(q) == t_INT? modii(q,p): FqX_eval(q, x, T, p);
455 : }
456 89319 : return FpXQX_renormalize(z, lb);
457 : }
458 :
459 : /* Q an FpXY, evaluate at (X,Y) = (x,y) */
460 : GEN
461 14623 : FqXY_eval(GEN Q, GEN y, GEN x, GEN T, GEN p)
462 : {
463 14623 : pari_sp av = avma;
464 14623 : if (!T) return FpXY_eval(Q, y, x, p);
465 966 : return gerepileupto(av, FqX_eval(FqXY_evalx(Q, x, T, p), y, T, p));
466 : }
467 :
468 : /* a X^d */
469 : GEN
470 12244496 : monomial(GEN a, long d, long v)
471 : {
472 : long i, n;
473 : GEN P;
474 12244496 : if (d < 0) {
475 14 : if (isrationalzero(a)) return pol_0(v);
476 14 : retmkrfrac(a, pol_xn(-d, v));
477 : }
478 12244482 : if (gequal0(a))
479 : {
480 93275 : if (isexactzero(a)) return scalarpol_shallow(a,v);
481 0 : n = d+2; P = cgetg(n+1, t_POL);
482 0 : P[1] = evalsigne(0) | evalvarn(v);
483 : }
484 : else
485 : {
486 12151209 : n = d+2; P = cgetg(n+1, t_POL);
487 12151213 : P[1] = evalsigne(1) | evalvarn(v);
488 : }
489 31278782 : for (i = 2; i < n; i++) gel(P,i) = gen_0;
490 12151213 : gel(P,i) = a; return P;
491 : }
492 : GEN
493 1860552 : monomialcopy(GEN a, long d, long v)
494 : {
495 : long i, n;
496 : GEN P;
497 1860552 : if (d < 0) {
498 14 : if (isrationalzero(a)) return pol_0(v);
499 14 : retmkrfrac(gcopy(a), pol_xn(-d, v));
500 : }
501 1860538 : if (gequal0(a))
502 : {
503 14 : if (isexactzero(a)) return scalarpol(a,v);
504 7 : n = d+2; P = cgetg(n+1, t_POL);
505 7 : P[1] = evalsigne(0) | evalvarn(v);
506 : }
507 : else
508 : {
509 1860524 : n = d+2; P = cgetg(n+1, t_POL);
510 1860524 : P[1] = evalsigne(1) | evalvarn(v);
511 : }
512 3503983 : for (i = 2; i < n; i++) gel(P,i) = gen_0;
513 1860531 : gel(P,i) = gcopy(a); return P;
514 : }
515 : GEN
516 326026 : pol_x_powers(long N, long v)
517 : {
518 326026 : GEN L = cgetg(N+1,t_VEC);
519 : long i;
520 789275 : for (i=1; i<=N; i++) gel(L,i) = pol_xn(i-1, v);
521 326033 : return L;
522 : }
523 :
524 : GEN
525 0 : FqXQ_powers(GEN x, long l, GEN S, GEN T, GEN p)
526 : {
527 0 : return T ? FpXQXQ_powers(x, l, S, T, p): FpXQ_powers(x, l, S, p);
528 : }
529 :
530 : GEN
531 0 : FqXQ_matrix_pow(GEN y, long n, long m, GEN S, GEN T, GEN p)
532 : {
533 0 : return T ? FpXQXQ_matrix_pow(y, n, m, S, T, p): FpXQ_matrix_pow(y, n, m, S, p);
534 : }
535 :
536 : /*******************************************************************/
537 : /* */
538 : /* Fq */
539 : /* */
540 : /*******************************************************************/
541 :
542 : GEN
543 11611066 : Fq_add(GEN x, GEN y, GEN T/*unused*/, GEN p)
544 : {
545 : (void)T;
546 11611066 : switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
547 : {
548 1143673 : case 0: return Fp_add(x,y,p);
549 764628 : case 1: return FpX_Fp_add(x,y,p);
550 92133 : case 2: return FpX_Fp_add(y,x,p);
551 9610633 : case 3: return FpX_add(x,y,p);
552 : }
553 : return NULL;/*LCOV_EXCL_LINE*/
554 : }
555 :
556 : GEN
557 8574583 : Fq_sub(GEN x, GEN y, GEN T/*unused*/, GEN p)
558 : {
559 : (void)T;
560 8574583 : switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
561 : {
562 256095 : case 0: return Fp_sub(x,y,p);
563 24480 : case 1: return FpX_Fp_sub(x,y,p);
564 23908 : case 2: return Fp_FpX_sub(x,y,p);
565 8270100 : case 3: return FpX_sub(x,y,p);
566 : }
567 : return NULL;/*LCOV_EXCL_LINE*/
568 : }
569 :
570 : GEN
571 1080324 : Fq_neg(GEN x, GEN T/*unused*/, GEN p)
572 : {
573 : (void)T;
574 1080324 : return (typ(x)==t_POL)? FpX_neg(x,p): Fp_neg(x,p);
575 : }
576 :
577 : GEN
578 83635 : Fq_halve(GEN x, GEN T/*unused*/, GEN p)
579 : {
580 : (void)T;
581 83635 : return (typ(x)==t_POL)? FpX_halve(x,p): Fp_halve(x,p);
582 : }
583 :
584 : /* If T==NULL do not reduce*/
585 : GEN
586 8633168 : Fq_mul(GEN x, GEN y, GEN T, GEN p)
587 : {
588 8633168 : switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
589 : {
590 1037917 : case 0: return Fp_mul(x,y,p);
591 129003 : case 1: return FpX_Fp_mul(x,y,p);
592 401744 : case 2: return FpX_Fp_mul(y,x,p);
593 7064507 : case 3: if (T) return FpXQ_mul(x,y,T,p);
594 4485949 : else return FpX_mul(x,y,p);
595 : }
596 : return NULL;/*LCOV_EXCL_LINE*/
597 : }
598 :
599 : /* If T==NULL do not reduce*/
600 : GEN
601 492780 : Fq_mulu(GEN x, ulong y, /*unused*/GEN T, GEN p)
602 : {
603 : (void) T;
604 492780 : return typ(x)==t_POL ? FpX_Fp_mul(x,utoi(y),p): Fp_mulu(x, y, p);
605 : }
606 :
607 : /* y t_INT */
608 : GEN
609 43902 : Fq_Fp_mul(GEN x, GEN y, GEN T/*unused*/, GEN p)
610 : {
611 : (void)T;
612 6823 : return (typ(x) == t_POL)? FpX_Fp_mul(x,y,p)
613 50725 : : Fp_mul(x,y,p);
614 : }
615 : /* If T==NULL do not reduce*/
616 : GEN
617 613604 : Fq_sqr(GEN x, GEN T, GEN p)
618 : {
619 613604 : if (typ(x) == t_POL)
620 : {
621 72865 : if (T) return FpXQ_sqr(x,T,p);
622 0 : else return FpX_sqr(x,p);
623 : }
624 : else
625 540739 : return Fp_sqr(x,p);
626 : }
627 :
628 : GEN
629 0 : Fq_neg_inv(GEN x, GEN T, GEN p)
630 : {
631 0 : if (typ(x) == t_INT) return Fp_inv(Fp_neg(x,p),p);
632 0 : return FpXQ_inv(FpX_neg(x,p),T,p);
633 : }
634 :
635 : GEN
636 0 : Fq_invsafe(GEN x, GEN pol, GEN p)
637 : {
638 0 : if (typ(x) == t_INT) return Fp_invsafe(x,p);
639 0 : return FpXQ_invsafe(x,pol,p);
640 : }
641 :
642 : GEN
643 89265 : Fq_inv(GEN x, GEN pol, GEN p)
644 : {
645 89265 : if (typ(x) == t_INT) return Fp_inv(x,p);
646 81499 : return FpXQ_inv(x,pol,p);
647 : }
648 :
649 : GEN
650 343784 : Fq_div(GEN x, GEN y, GEN pol, GEN p)
651 : {
652 343784 : switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
653 : {
654 318402 : case 0: return Fp_div(x,y,p);
655 16702 : case 1: return FpX_Fp_div(x,y,p);
656 406 : case 2: return FpX_Fp_mul(FpXQ_inv(y,pol,p),x,p);
657 8274 : case 3: return FpXQ_div(x,y,pol,p);
658 : }
659 : return NULL;/*LCOV_EXCL_LINE*/
660 : }
661 :
662 : GEN
663 795418 : Fq_pow(GEN x, GEN n, GEN pol, GEN p)
664 : {
665 795418 : if (typ(x) == t_INT) return Fp_pow(x,n,p);
666 136925 : return FpXQ_pow(x,n,pol,p);
667 : }
668 :
669 : GEN
670 15050 : Fq_powu(GEN x, ulong n, GEN pol, GEN p)
671 : {
672 15050 : if (typ(x) == t_INT) return Fp_powu(x,n,p);
673 1267 : return FpXQ_powu(x,n,pol,p);
674 : }
675 :
676 : GEN
677 1895165 : Fq_sqrt(GEN x, GEN T, GEN p)
678 : {
679 1895165 : if (typ(x) == t_INT)
680 : {
681 1825039 : if (!T || odd(get_FpX_degree(T))) return Fp_sqrt(x,p);
682 9596 : x = scalarpol_shallow(x, get_FpX_var(T));
683 : }
684 79722 : return FpXQ_sqrt(x,T,p);
685 : }
686 : GEN
687 170737 : Fq_sqrtn(GEN x, GEN n, GEN T, GEN p, GEN *zeta)
688 : {
689 170737 : if (typ(x) == t_INT)
690 : {
691 : long d;
692 170380 : if (!T) return Fp_sqrtn(x,n,p,zeta);
693 119 : d = get_FpX_degree(T);
694 119 : if (ugcdiu(n,d) == 1)
695 : {
696 56 : if (!zeta) return Fp_sqrtn(x,n,p,NULL);
697 : /* gcd(n,p-1)=gcd(n,q-1): same number of solutions in Fp and F_q */
698 21 : if (equalii(gcdii(subiu(p,1),n), gcdii(subiu(Fp_powu(p,d,n), 1), n)))
699 14 : return Fp_sqrtn(x,n,p,zeta);
700 : }
701 70 : x = scalarpol(x, get_FpX_var(T)); /* left on stack */
702 : }
703 427 : return FpXQ_sqrtn(x,n,T,p,zeta);
704 : }
705 :
706 : struct _Fq_field
707 : {
708 : GEN T, p;
709 : };
710 :
711 : static GEN
712 303247 : _Fq_red(void *E, GEN x)
713 303247 : { struct _Fq_field *s = (struct _Fq_field *)E;
714 303247 : return Fq_red(x, s->T, s->p);
715 : }
716 :
717 : static GEN
718 362523 : _Fq_add(void *E, GEN x, GEN y)
719 : {
720 : (void) E;
721 362523 : switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
722 : {
723 14 : case 0: return addii(x,y);
724 0 : case 1: return ZX_Z_add(x,y);
725 15918 : case 2: return ZX_Z_add(y,x);
726 346591 : default: return ZX_add(x,y);
727 : }
728 : }
729 :
730 : static GEN
731 315028 : _Fq_neg(void *E, GEN x) { (void) E; return typ(x)==t_POL?ZX_neg(x):negi(x); }
732 :
733 : static GEN
734 243341 : _Fq_mul(void *E, GEN x, GEN y)
735 : {
736 : (void) E;
737 243341 : switch((typ(x)==t_POL)|((typ(y)==t_POL)<<1))
738 : {
739 679 : case 0: return mulii(x,y);
740 1085 : case 1: return ZX_Z_mul(x,y);
741 1043 : case 2: return ZX_Z_mul(y,x);
742 240534 : default: return ZX_mul(x,y);
743 : }
744 : }
745 :
746 : static GEN
747 9331 : _Fq_inv(void *E, GEN x)
748 9331 : { struct _Fq_field *s = (struct _Fq_field *)E;
749 9331 : return Fq_inv(x,s->T,s->p);
750 : }
751 :
752 : static int
753 38388 : _Fq_equal0(GEN x) { return signe(x)==0; }
754 :
755 : static GEN
756 4151 : _Fq_s(void *E, long x) { (void) E; return stoi(x); }
757 :
758 : static const struct bb_field Fq_field={_Fq_red,_Fq_add,_Fq_mul,_Fq_neg,
759 : _Fq_inv,_Fq_equal0,_Fq_s};
760 :
761 4725 : const struct bb_field *get_Fq_field(void **E, GEN T, GEN p)
762 : {
763 4725 : if (!T)
764 0 : return get_Fp_field(E, p);
765 : else
766 : {
767 4725 : GEN z = new_chunk(sizeof(struct _Fq_field));
768 4725 : struct _Fq_field *e = (struct _Fq_field *) z;
769 4725 : e->T = T; e->p = p; *E = (void*)e;
770 4725 : return &Fq_field;
771 : }
772 : }
773 :
774 : /*******************************************************************/
775 : /* */
776 : /* Fq[X] */
777 : /* */
778 : /*******************************************************************/
779 : /* P(X + c) */
780 : GEN
781 266 : FpX_translate(GEN P, GEN c, GEN p)
782 : {
783 266 : pari_sp av = avma;
784 : GEN Q, *R;
785 : long i, k, n;
786 :
787 266 : if (!signe(P) || !signe(c)) return ZX_copy(P);
788 266 : Q = leafcopy(P);
789 266 : R = (GEN*)(Q+2); n = degpol(P);
790 3738 : for (i=1; i<=n; i++)
791 : {
792 118153 : for (k=n-i; k<n; k++)
793 114681 : R[k] = Fp_add(R[k], Fp_mul(c, R[k+1], p), p);
794 :
795 3472 : if (gc_needed(av,2))
796 : {
797 0 : if(DEBUGMEM>1) pari_warn(warnmem,"FpX_translate, i = %ld/%ld", i,n);
798 0 : Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
799 : }
800 : }
801 266 : return gerepilecopy(av, FpX_renormalize(Q, lg(Q)));
802 : }
803 : /* P(X + c), c an Fq */
804 : GEN
805 33880 : FqX_translate(GEN P, GEN c, GEN T, GEN p)
806 : {
807 33880 : pari_sp av = avma;
808 : GEN Q, *R;
809 : long i, k, n;
810 :
811 : /* signe works for t_(INT|POL) */
812 33880 : if (!signe(P) || !signe(c)) return RgX_copy(P);
813 33880 : Q = leafcopy(P);
814 33880 : R = (GEN*)(Q+2); n = degpol(P);
815 150059 : for (i=1; i<=n; i++)
816 : {
817 433559 : for (k=n-i; k<n; k++)
818 317380 : R[k] = Fq_add(R[k], Fq_mul(c, R[k+1], T, p), T, p);
819 :
820 116179 : if (gc_needed(av,2))
821 : {
822 0 : if(DEBUGMEM>1) pari_warn(warnmem,"FqX_translate, i = %ld/%ld", i,n);
823 0 : Q = gerepilecopy(av, Q); R = (GEN*)Q+2;
824 : }
825 : }
826 33880 : return gerepilecopy(av, FpXQX_renormalize(Q, lg(Q)));
827 : }
828 :
829 : GEN
830 40450 : FqV_roots_to_pol(GEN V, GEN T, GEN p, long v)
831 : {
832 40450 : pari_sp ltop = avma;
833 : long k;
834 : GEN W;
835 40450 : if (lgefint(p) == 3)
836 : {
837 31720 : ulong pp = p[2];
838 31720 : GEN Tl = ZX_to_Flx(T, pp);
839 31722 : GEN Vl = ZXC_to_FlxC(V, pp, get_Flx_var(Tl));
840 31722 : Tl = FlxqV_roots_to_pol(Vl, Tl, pp, v);
841 31723 : return gerepileupto(ltop, FlxX_to_ZXX(Tl));
842 : }
843 8730 : W = cgetg(lg(V),t_VEC);
844 78254 : for(k=1; k < lg(V); k++)
845 69524 : gel(W,k) = deg1pol_shallow(gen_1,Fq_neg(gel(V,k),T,p),v);
846 8730 : return gerepileupto(ltop, FpXQXV_prod(W, T, p));
847 : }
848 :
849 : GEN
850 109486 : FqV_red(GEN x, GEN T, GEN p)
851 778030 : { pari_APPLY_type(t_VEC, Fq_red(gel(x,i), T, p)) }
852 :
853 : GEN
854 23945 : FqC_red(GEN x, GEN T, GEN p)
855 163003 : { pari_APPLY_type(t_COL, Fq_red(gel(x,i), T, p)) }
856 :
857 : GEN
858 1701 : FqM_red(GEN x, GEN T, GEN p)
859 5411 : { pari_APPLY_same(FqC_red(gel(x,i), T, p)) }
860 :
861 : GEN
862 0 : FqC_add(GEN x, GEN y, GEN T, GEN p)
863 : {
864 0 : if (!T) return FpC_add(x, y, p);
865 0 : pari_APPLY_type(t_COL, Fq_add(gel(x,i), gel(y,i), T, p))
866 : }
867 :
868 : GEN
869 0 : FqC_sub(GEN x, GEN y, GEN T, GEN p)
870 : {
871 0 : if (!T) return FpC_sub(x, y, p);
872 0 : pari_APPLY_type(t_COL, Fq_sub(gel(x,i), gel(y,i), T, p))
873 : }
874 :
875 : GEN
876 0 : FqC_Fq_mul(GEN x, GEN y, GEN T, GEN p)
877 : {
878 0 : if (!T) return FpC_Fp_mul(x, y, p);
879 0 : pari_APPLY_type(t_COL, Fq_mul(gel(x,i),y,T,p))
880 : }
881 :
882 : GEN
883 105 : FqC_FqV_mul(GEN x, GEN y, GEN T, GEN p)
884 : {
885 105 : long i,j, lx=lg(x), ly=lg(y);
886 : GEN z;
887 105 : if (ly==1) return cgetg(1,t_MAT);
888 105 : z = cgetg(ly,t_MAT);
889 819 : for (j=1; j < ly; j++)
890 : {
891 714 : GEN zj = cgetg(lx,t_COL);
892 4200 : for (i=1; i<lx; i++) gel(zj,i) = Fq_mul(gel(x,i),gel(y,j), T, p);
893 714 : gel(z, j) = zj;
894 : }
895 105 : return z;
896 : }
897 :
898 : GEN
899 5313 : FpXC_center(GEN x, GEN p, GEN pov2)
900 40818 : { pari_APPLY_type(t_COL, FpX_center(gel(x,i), p, pov2)) }
901 :
902 : GEN
903 1737 : FpXM_center(GEN x, GEN p, GEN pov2)
904 7050 : { pari_APPLY_same(FpXC_center(gel(x,i), p, pov2)) }
905 :
906 : /*******************************************************************/
907 : /* */
908 : /* GENERIC CRT */
909 : /* */
910 : /*******************************************************************/
911 : static GEN
912 8281368 : primelist(forprime_t *S, long n, GEN dB)
913 : {
914 8281368 : GEN P = cgetg(n+1, t_VECSMALL);
915 8281348 : long i = 1;
916 : ulong p;
917 19957679 : while (i <= n && (p = u_forprime_next(S)))
918 11676331 : if (!dB || umodiu(dB, p)) P[i++] = p;
919 8281349 : return P;
920 : }
921 :
922 : void
923 7708192 : gen_inccrt_i(const char *str, GEN worker, GEN dB, long n, long mmin,
924 : forprime_t *S, GEN *pH, GEN *pmod, GEN crt(GEN, GEN, GEN*),
925 : GEN center(GEN, GEN, GEN))
926 : {
927 7708192 : long m = mmin? minss(mmin, n): usqrt(n);
928 : GEN H, P, mod;
929 : pari_timer ti;
930 7708190 : if (DEBUGLEVEL > 4)
931 : {
932 0 : timer_start(&ti);
933 0 : err_printf("%s: nb primes: %ld\n",str, n);
934 : }
935 7708180 : if (m == 1)
936 : {
937 7400513 : GEN P = primelist(S, n, dB);
938 7400486 : GEN done = closure_callgen1(worker, P);
939 7400456 : H = gel(done,1);
940 7400456 : mod = gel(done,2);
941 7400456 : if (!*pH && center) H = center(H, mod, shifti(mod,-1));
942 7400414 : if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
943 : }
944 : else
945 : {
946 307667 : long i, s = (n+m-1)/m, r = m - (m*s-n), di = 0;
947 : struct pari_mt pt;
948 307667 : long pending = 0;
949 307667 : H = cgetg(m+1, t_VEC); P = cgetg(m+1, t_VEC);
950 307667 : mt_queue_start_lim(&pt, worker, m);
951 1252976 : for (i=1; i<=m || pending; i++)
952 : {
953 : GEN done;
954 945309 : GEN pr = i <= m ? mkvec(primelist(S, i<=r ? s: s-1, dB)): NULL;
955 945307 : mt_queue_submit(&pt, i, pr);
956 945308 : done = mt_queue_get(&pt, NULL, &pending);
957 945308 : if (done)
958 : {
959 880856 : di++;
960 880856 : gel(H, di) = gel(done,1);
961 880856 : gel(P, di) = gel(done,2);
962 880856 : if (DEBUGLEVEL>5) err_printf("%ld%% ",100*di/m);
963 : }
964 : }
965 307667 : mt_queue_end(&pt);
966 307667 : if (DEBUGLEVEL>5) err_printf("\n");
967 307667 : if (DEBUGLEVEL>4) timer_printf(&ti,"%s: modular", str);
968 307667 : H = crt(H, P, &mod);
969 307667 : if (DEBUGLEVEL>4) timer_printf(&ti,"%s: chinese", str);
970 : }
971 7708081 : if (*pH) H = crt(mkvec2(*pH, H), mkvec2(*pmod, mod), &mod);
972 7708081 : *pH = H; *pmod = mod;
973 7708081 : }
974 : void
975 2053809 : gen_inccrt(const char *str, GEN worker, GEN dB, long n, long mmin,
976 : forprime_t *S, GEN *pH, GEN *pmod, GEN crt(GEN, GEN, GEN*),
977 : GEN center(GEN, GEN, GEN))
978 : {
979 2053809 : pari_sp av = avma;
980 2053809 : gen_inccrt_i(str, worker, dB, n, mmin, S, pH, pmod, crt, center);
981 2053743 : gerepileall(av, 2, pH, pmod);
982 2053880 : }
983 :
984 : GEN
985 1269521 : gen_crt(const char *str, GEN worker, forprime_t *S, GEN dB, ulong bound, long mmin, GEN *pmod,
986 : GEN crt(GEN, GEN, GEN*), GEN center(GEN, GEN, GEN))
987 : {
988 1269521 : GEN mod = gen_1, H = NULL;
989 : ulong e;
990 :
991 1269521 : bound++;
992 2539106 : while (bound > (e = expi(mod)))
993 : {
994 1269476 : long n = (bound - e) / expu(S->p) + 1;
995 1269514 : gen_inccrt(str, worker, dB, n, mmin, S, &H, &mod, crt, center);
996 : }
997 1269566 : if (pmod) *pmod = mod;
998 1269566 : return H;
999 : }
1000 :
1001 : /*******************************************************************/
1002 : /* */
1003 : /* MODULAR GCD */
1004 : /* */
1005 : /*******************************************************************/
1006 : /* return z = a mod q, b mod p (p,q) = 1; qinv = 1/q mod p; a in ]-q,q] */
1007 : static GEN
1008 5156557 : Fl_chinese_coprime(GEN a, ulong b, GEN q, ulong p, ulong qinv, GEN pq, GEN pq2)
1009 : {
1010 5156557 : ulong d, amod = umodiu(a, p);
1011 5156610 : pari_sp av = avma;
1012 : GEN ax;
1013 :
1014 5156610 : if (b == amod) return NULL;
1015 2126081 : d = Fl_mul(Fl_sub(b, amod, p), qinv, p); /* != 0 */
1016 2126901 : if (d >= 1 + (p>>1))
1017 1037810 : ax = subii(a, mului(p-d, q));
1018 : else
1019 : {
1020 1089091 : ax = addii(a, mului(d, q)); /* in ]0, pq[ assuming a in ]-q,q[ */
1021 1088527 : if (cmpii(ax,pq2) > 0) ax = subii(ax,pq);
1022 : }
1023 2125803 : return gerepileuptoint(av, ax);
1024 : }
1025 : GEN
1026 378 : Z_init_CRT(ulong Hp, ulong p) { return stoi(Fl_center(Hp, p, p>>1)); }
1027 : GEN
1028 31689 : ZX_init_CRT(GEN Hp, ulong p, long v)
1029 : {
1030 31689 : long i, l = lg(Hp), lim = (long)(p>>1);
1031 31689 : GEN H = cgetg(l, t_POL);
1032 31689 : H[1] = evalsigne(1) | evalvarn(v);
1033 796134 : for (i=2; i<l; i++)
1034 764445 : gel(H,i) = stoi(Fl_center(Hp[i], p, lim));
1035 31689 : return ZX_renormalize(H,l);
1036 : }
1037 :
1038 : GEN
1039 5789 : ZM_init_CRT(GEN Hp, ulong p)
1040 : {
1041 5789 : long i,j, m, l = lg(Hp), lim = (long)(p>>1);
1042 5789 : GEN c, cp, H = cgetg(l, t_MAT);
1043 5789 : if (l==1) return H;
1044 5789 : m = lgcols(Hp);
1045 19012 : for (j=1; j<l; j++)
1046 : {
1047 13223 : cp = gel(Hp,j);
1048 13223 : c = cgetg(m, t_COL);
1049 13223 : gel(H,j) = c;
1050 166691 : for (i=1; i<m; i++) gel(c,i) = stoi(Fl_center(cp[i],p, lim));
1051 : }
1052 5789 : return H;
1053 : }
1054 :
1055 : int
1056 7616 : Z_incremental_CRT(GEN *H, ulong Hp, GEN *ptq, ulong p)
1057 : {
1058 7616 : GEN h, q = *ptq, qp = muliu(q,p);
1059 7616 : ulong qinv = Fl_inv(umodiu(q,p), p);
1060 7616 : int stable = 1;
1061 7616 : h = Fl_chinese_coprime(*H,Hp,q,p,qinv,qp,shifti(qp,-1));
1062 7616 : if (h) { *H = h; stable = 0; }
1063 7616 : *ptq = qp; return stable;
1064 : }
1065 :
1066 : static int
1067 147472 : ZX_incremental_CRT_raw(GEN *ptH, GEN Hp, GEN q, GEN qp, ulong p)
1068 : {
1069 147472 : GEN H = *ptH, h, qp2 = shifti(qp,-1);
1070 147468 : ulong qinv = Fl_inv(umodiu(q,p), p);
1071 147474 : long i, l = lg(H), lp = lg(Hp);
1072 147474 : int stable = 1;
1073 :
1074 147474 : if (l < lp)
1075 : { /* degree increases */
1076 0 : GEN x = cgetg(lp, t_POL);
1077 0 : for (i=1; i<l; i++) x[i] = H[i];
1078 0 : for ( ; i<lp; i++) gel(x,i) = gen_0;
1079 0 : *ptH = H = x;
1080 0 : stable = 0;
1081 147474 : } else if (l > lp)
1082 : { /* degree decreases */
1083 0 : GEN x = cgetg(l, t_VECSMALL);
1084 0 : for (i=1; i<lp; i++) x[i] = Hp[i];
1085 0 : for ( ; i<l; i++) x[i] = 0;
1086 0 : Hp = x; lp = l;
1087 : }
1088 4933180 : for (i=2; i<lp; i++)
1089 : {
1090 4785813 : h = Fl_chinese_coprime(gel(H,i),Hp[i],q,p,qinv,qp,qp2);
1091 4785706 : if (h) { gel(H,i) = h; stable = 0; }
1092 : }
1093 147367 : (void)ZX_renormalize(H,lp);
1094 147472 : return stable;
1095 : }
1096 :
1097 : int
1098 0 : ZX_incremental_CRT(GEN *ptH, GEN Hp, GEN *ptq, ulong p)
1099 : {
1100 0 : GEN q = *ptq, qp = muliu(q,p);
1101 0 : int stable = ZX_incremental_CRT_raw(ptH, Hp, q, qp, p);
1102 0 : *ptq = qp; return stable;
1103 : }
1104 :
1105 : int
1106 7611 : ZM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
1107 : {
1108 7611 : GEN h, H = *pH, q = *ptq, qp = muliu(q, p), qp2 = shifti(qp,-1);
1109 7611 : ulong qinv = Fl_inv(umodiu(q,p), p);
1110 7611 : long i,j, l = lg(H), m = lgcols(H);
1111 7611 : int stable = 1;
1112 26374 : for (j=1; j<l; j++)
1113 204136 : for (i=1; i<m; i++)
1114 : {
1115 185373 : h = Fl_chinese_coprime(gcoeff(H,i,j), coeff(Hp,i,j),q,p,qinv,qp,qp2);
1116 185373 : if (h) { gcoeff(H,i,j) = h; stable = 0; }
1117 : }
1118 7611 : *ptq = qp; return stable;
1119 : }
1120 :
1121 : GEN
1122 623 : ZXM_init_CRT(GEN Hp, long deg, ulong p)
1123 : {
1124 : long i, j, k;
1125 : GEN H;
1126 623 : long m, l = lg(Hp), lim = (long)(p>>1), n;
1127 623 : H = cgetg(l, t_MAT);
1128 623 : if (l==1) return H;
1129 623 : m = lgcols(Hp);
1130 623 : n = deg + 3;
1131 2114 : for (j=1; j<l; j++)
1132 : {
1133 1491 : GEN cp = gel(Hp,j);
1134 1491 : GEN c = cgetg(m, t_COL);
1135 1491 : gel(H,j) = c;
1136 23905 : for (i=1; i<m; i++)
1137 : {
1138 22414 : GEN dp = gel(cp, i);
1139 22414 : long l = lg(dp);
1140 22414 : GEN d = cgetg(n, t_POL);
1141 22414 : gel(c, i) = d;
1142 22414 : d[1] = dp[1] | evalsigne(1);
1143 45647 : for (k=2; k<l; k++)
1144 23233 : gel(d,k) = stoi(Fl_center(dp[k], p, lim));
1145 44457 : for ( ; k<n; k++)
1146 22043 : gel(d,k) = gen_0;
1147 : }
1148 : }
1149 623 : return H;
1150 : }
1151 :
1152 : int
1153 653 : ZXM_incremental_CRT(GEN *pH, GEN Hp, GEN *ptq, ulong p)
1154 : {
1155 653 : GEN v, H = *pH, q = *ptq, qp = muliu(q, p), qp2 = shifti(qp,-1);
1156 653 : ulong qinv = Fl_inv(umodiu(q,p), p);
1157 653 : long i,j,k, l = lg(H), m = lgcols(H), n = lg(gmael(H,1,1));
1158 653 : int stable = 1;
1159 2225 : for (j=1; j<l; j++)
1160 90418 : for (i=1; i<m; i++)
1161 : {
1162 88846 : GEN h = gmael(H,j,i), hp = gmael(Hp,j,i);
1163 88846 : long lh = lg(hp);
1164 246641 : for (k=2; k<lh; k++)
1165 : {
1166 157795 : v = Fl_chinese_coprime(gel(h,k),uel(hp,k),q,p,qinv,qp,qp2);
1167 157795 : if (v) { gel(h,k) = v; stable = 0; }
1168 : }
1169 108763 : for (; k<n; k++)
1170 : {
1171 19917 : v = Fl_chinese_coprime(gel(h,k),0,q,p,qinv,qp,qp2);
1172 19917 : if (v) { gel(h,k) = v; stable = 0; }
1173 : }
1174 : }
1175 653 : *ptq = qp; return stable;
1176 : }
1177 :
1178 : /* record the degrees of Euclidean remainders (make them as large as
1179 : * possible : smaller values correspond to a degenerate sequence) */
1180 : static void
1181 23209 : Flx_resultant_set_dglist(GEN a, GEN b, GEN dglist, ulong p)
1182 : {
1183 : long da,db,dc, ind;
1184 23209 : pari_sp av = avma;
1185 :
1186 23209 : if (lgpol(a)==0 || lgpol(b)==0) return;
1187 21942 : da = degpol(a);
1188 21942 : db = degpol(b);
1189 21942 : if (db > da)
1190 0 : { swapspec(a,b, da,db); }
1191 21942 : else if (!da) return;
1192 21942 : ind = 0;
1193 144197 : while (db)
1194 : {
1195 122255 : GEN c = Flx_rem(a,b, p);
1196 122255 : a = b; b = c; dc = degpol(c);
1197 122255 : if (dc < 0) break;
1198 :
1199 122255 : ind++;
1200 122255 : if (dc > dglist[ind]) dglist[ind] = dc;
1201 122255 : if (gc_needed(av,2))
1202 : {
1203 0 : if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
1204 0 : gerepileall(av, 2, &a,&b);
1205 : }
1206 122255 : db = dc; /* = degpol(b) */
1207 : }
1208 21942 : if (ind+1 > lg(dglist)) setlg(dglist,ind+1);
1209 21942 : set_avma(av);
1210 : }
1211 : /* assuming the PRS finishes on a degree 1 polynomial C0 + C1X, with
1212 : * "generic" degree sequence as given by dglist, set *Ci and return
1213 : * resultant(a,b). Modular version of Collins's subresultant */
1214 : static ulong
1215 2084952 : Flx_resultant_all(GEN a, GEN b, long *C0, long *C1, GEN dglist, ulong p)
1216 : {
1217 : long da,db,dc, ind;
1218 2084952 : ulong lb, res, g = 1UL, h = 1UL, ca = 1UL, cb = 1UL;
1219 2084952 : int s = 1;
1220 2084952 : pari_sp av = avma;
1221 :
1222 2084952 : *C0 = 1; *C1 = 0;
1223 2084952 : if (lgpol(a)==0 || lgpol(b)==0) return 0;
1224 2075529 : da = degpol(a);
1225 2075560 : db = degpol(b);
1226 2075551 : if (db > da)
1227 : {
1228 0 : swapspec(a,b, da,db);
1229 0 : if (both_odd(da,db)) s = -s;
1230 : }
1231 2075551 : else if (!da) return 1; /* = a[2] ^ db, since 0 <= db <= da = 0 */
1232 2075551 : ind = 0;
1233 19798642 : while (db)
1234 : { /* sub-resultant algo., applied to ca * a and cb * b, ca,cb scalars,
1235 : * da = deg a, db = deg b */
1236 17727734 : GEN c = Flx_rem(a,b, p);
1237 17618362 : long delta = da - db;
1238 :
1239 17618362 : if (both_odd(da,db)) s = -s;
1240 17614144 : lb = Fl_mul(b[db+2], cb, p);
1241 17630917 : a = b; b = c; dc = degpol(c);
1242 17635572 : ind++;
1243 17635572 : if (dc != dglist[ind]) return gc_ulong(av,0); /* degenerates */
1244 17630681 : if (g == h)
1245 : { /* frequent */
1246 17570847 : ulong cc = Fl_mul(ca, Fl_powu(Fl_div(lb,g,p), delta+1, p), p);
1247 17665484 : ca = cb;
1248 17665484 : cb = cc;
1249 : }
1250 : else
1251 : {
1252 59834 : ulong cc = Fl_mul(ca, Fl_powu(lb, delta+1, p), p);
1253 59837 : ulong ghdelta = Fl_mul(g, Fl_powu(h, delta, p), p);
1254 59837 : ca = cb;
1255 59837 : cb = Fl_div(cc, ghdelta, p);
1256 : }
1257 17723885 : da = db; /* = degpol(a) */
1258 17723885 : db = dc; /* = degpol(b) */
1259 :
1260 17723885 : g = lb;
1261 17723885 : if (delta == 1)
1262 17624364 : h = g; /* frequent */
1263 : else
1264 99521 : h = Fl_mul(h, Fl_powu(Fl_div(g,h,p), delta, p), p);
1265 :
1266 17724075 : if (gc_needed(av,2))
1267 : {
1268 0 : if (DEBUGMEM>1) pari_warn(warnmem,"Flx_resultant_all");
1269 0 : gerepileall(av, 2, &a,&b);
1270 : }
1271 : }
1272 2070908 : if (da > 1) return 0; /* Failure */
1273 : /* last nonconstant polynomial has degree 1 */
1274 2070908 : *C0 = Fl_mul(ca, a[2], p);
1275 2070873 : *C1 = Fl_mul(ca, a[3], p);
1276 2070880 : res = Fl_mul(cb, b[2], p);
1277 2070886 : if (s == -1) res = p - res;
1278 2070886 : return gc_ulong(av,res);
1279 : }
1280 :
1281 : /* Q a vector of polynomials representing B in Fp[X][Y], evaluate at X = x,
1282 : * Return 0 in case of degree drop. */
1283 : static GEN
1284 2108378 : FlxY_evalx_drop(GEN Q, ulong x, ulong p)
1285 : {
1286 : GEN z;
1287 2108378 : long i, lb = lg(Q);
1288 2108378 : ulong leadz = Flx_eval(leading_coeff(Q), x, p);
1289 2108057 : long vs=mael(Q,2,1);
1290 2108057 : if (!leadz) return zero_Flx(vs);
1291 :
1292 2097397 : z = cgetg(lb, t_VECSMALL); z[1] = vs;
1293 20072519 : for (i=2; i<lb-1; i++) z[i] = Flx_eval(gel(Q,i), x, p);
1294 2096355 : z[i] = leadz; return z;
1295 : }
1296 :
1297 : GEN
1298 2072 : FpXY_FpXQ_evaly(GEN Q, GEN y, GEN T, GEN p, long vx)
1299 : {
1300 2072 : pari_sp av = avma;
1301 2072 : long i, lb = lg(Q);
1302 : GEN z;
1303 2072 : if (lb == 2) return pol_0(vx);
1304 2072 : z = gel(Q, lb-1);
1305 2072 : if (lb == 3 || !signe(y)) return typ(z)==t_INT? scalar_ZX(z, vx): ZX_copy(z);
1306 :
1307 2072 : if (typ(z) == t_INT) z = scalar_ZX_shallow(z, vx);
1308 48636 : for (i=lb-2; i>=2; i--)
1309 : {
1310 46564 : GEN c = gel(Q,i);
1311 46564 : z = FqX_Fq_mul(z, y, T, p);
1312 46564 : z = typ(c) == t_INT? FqX_Fq_add(z,c,T,p): FqX_add(z,c,T,p);
1313 : }
1314 2072 : return gerepileupto(av, z);
1315 : }
1316 :
1317 : static GEN
1318 291664 : ZX_norml1(GEN x)
1319 : {
1320 291664 : long i, l = lg(x);
1321 : GEN s;
1322 :
1323 291664 : if (l == 2) return gen_0;
1324 199110 : s = gel(x, l-1); /* != 0 */
1325 697053 : for (i = l-2; i > 1; i--) {
1326 497950 : GEN xi = gel(x,i);
1327 497950 : if (!signe(xi)) continue;
1328 259250 : s = addii_sign(s,1, xi,1);
1329 : }
1330 199103 : return s;
1331 : }
1332 : /* x >= 0, y != 0, return x + |y| */
1333 : static GEN
1334 25557 : addii_abs(GEN x, GEN y)
1335 : {
1336 25557 : if (!signe(x)) return absi_shallow(y);
1337 16047 : return addii_sign(x,1, y,1);
1338 : }
1339 :
1340 : /* x a ZX, return sum_{i >= k} |x[i]| binomial(i, k) */
1341 : static GEN
1342 31650 : ZX_norml1_1(GEN x, long k)
1343 : {
1344 31650 : long i, d = degpol(x);
1345 : GEN s, C; /* = binomial(i, k) */
1346 :
1347 31650 : if (!d || k > d) return gen_0;
1348 31650 : s = absi_shallow(gel(x, k+2)); /* may be 0 */
1349 31652 : C = gen_1;
1350 68053 : for (i = k+1; i <= d; i++) {
1351 36411 : GEN xi = gel(x,i+2);
1352 36411 : if (k) C = diviuexact(muliu(C, i), i-k);
1353 36412 : if (signe(xi)) s = addii_abs(s, mulii(C, xi));
1354 : }
1355 31642 : return s;
1356 : }
1357 : /* x has non-negative real coefficients */
1358 : static GEN
1359 3283 : RgX_norml1_1(GEN x, long k)
1360 : {
1361 3283 : long i, d = degpol(x);
1362 : GEN s, C; /* = binomial(i, k) */
1363 :
1364 3283 : if (!d || k > d) return gen_0;
1365 3283 : s = gel(x, k+2); /* may be 0 */
1366 3283 : C = gen_1;
1367 9198 : for (i = k+1; i <= d; i++) {
1368 5915 : GEN xi = gel(x,i+2);
1369 5915 : if (k) C = diviuexact(muliu(C, i), i-k);
1370 5915 : if (!gequal0(xi)) s = gadd(s, gmul(C, xi));
1371 : }
1372 3283 : return s;
1373 : }
1374 :
1375 : /* N_2(A)^2 */
1376 : static GEN
1377 8179 : sqrN2(GEN A, long prec)
1378 : {
1379 8179 : pari_sp av = avma;
1380 8179 : long i, l = lg(A);
1381 8179 : GEN a = gen_0;
1382 39961 : for (i = 2; i < l; i++)
1383 : {
1384 31782 : a = gadd(a, gabs(gnorm(gel(A,i)), prec));
1385 31782 : if (gc_needed(av,1))
1386 : {
1387 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
1388 0 : a = gerepileupto(av, a);
1389 : }
1390 : }
1391 8179 : return a;
1392 : }
1393 : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
1394 : * bound = N_2(A)^degpol B N_2(B)^degpol(A), where
1395 : * N_2(A) = sqrt(sum (N_1(Ai))^2)
1396 : * Return e such that Res(A, B) < 2^e */
1397 : static GEN
1398 7325 : RgX_RgXY_ResBound(GEN A, GEN B, long prec)
1399 : {
1400 7325 : pari_sp av = avma;
1401 7325 : GEN b = gen_0, bnd;
1402 7325 : long i, lB = lg(B);
1403 28821 : for (i=2; i<lB; i++)
1404 : {
1405 21496 : GEN t = gel(B,i);
1406 21496 : if (typ(t) == t_POL) t = gnorml1(t, prec);
1407 21496 : b = gadd(b, gabs(gsqr(t), prec));
1408 21496 : if (gc_needed(av,1))
1409 : {
1410 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
1411 0 : b = gerepileupto(av, b);
1412 : }
1413 : }
1414 7325 : bnd = gsqrt(gmul(gpowgs(sqrN2(A,prec), degpol(B)),
1415 : gpowgs(b, degpol(A))), prec);
1416 7325 : return gerepileupto(av, bnd);
1417 : }
1418 : /* A,B in C[X] return RgX_RgXY_ResBound(A, B(x+y)) */
1419 : static GEN
1420 854 : RgX_RgXY_ResBound_1(GEN A, GEN B, long prec)
1421 : {
1422 854 : pari_sp av = avma, av2;
1423 854 : GEN b = gen_0, bnd;
1424 854 : long i, lB = lg(B);
1425 854 : B = shallowcopy(B);
1426 4137 : for (i=2; i<lB; i++) gel(B,i) = gabs(gel(B,i), prec);
1427 854 : av2 = avma;
1428 4137 : for (i=2; i<lB; i++)
1429 : {
1430 3283 : b = gadd(b, gsqr(RgX_norml1_1(B, i-2)));
1431 3283 : if (gc_needed(av2,1))
1432 : {
1433 0 : if(DEBUGMEM>1) pari_warn(warnmem,"RgX_RgXY_ResBound i = %ld",i);
1434 0 : b = gerepileupto(av2, b);
1435 : }
1436 : }
1437 854 : bnd = gsqrt(gmul(gpowgs(sqrN2(A,prec), degpol(B)),
1438 : gpowgs(b, degpol(A))), prec);
1439 854 : return gerepileupto(av, bnd);
1440 : }
1441 :
1442 : /* log2 N_2(A)^2 */
1443 : static double
1444 176552 : log2N2(GEN A)
1445 : {
1446 176552 : pari_sp av = avma;
1447 176552 : long i, l = lg(A);
1448 176552 : GEN a = gen_0;
1449 1334068 : for (i=2; i < l; i++)
1450 : {
1451 1157523 : a = addii(a, sqri(gel(A,i)));
1452 1157516 : if (gc_needed(av,1))
1453 : {
1454 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
1455 0 : a = gerepileupto(av, a);
1456 : }
1457 : }
1458 176545 : return gc_double(av, dbllog2(a));
1459 : }
1460 : /* Interpolate at roots of 1 and use Hadamard bound for univariate resultant:
1461 : * bound = N_2(A)^degpol B N_2(B)^degpol(A), where
1462 : * N_2(A) = sqrt(sum (N_1(Ai))^2)
1463 : * Return e such that Res(A, B) < 2^e */
1464 : ulong
1465 166472 : ZX_ZXY_ResBound(GEN A, GEN B, GEN dB)
1466 : {
1467 166472 : pari_sp av = avma;
1468 166472 : GEN b = gen_0;
1469 166472 : long i, lB = lg(B);
1470 : double logb;
1471 1260069 : for (i=2; i<lB; i++)
1472 : {
1473 1093599 : GEN t = gel(B,i);
1474 1093599 : if (typ(t) == t_POL) t = ZX_norml1(t);
1475 1093597 : b = addii(b, sqri(t));
1476 1093597 : if (gc_needed(av,1))
1477 : {
1478 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
1479 0 : b = gerepileupto(av, b);
1480 : }
1481 : }
1482 166470 : logb = dbllog2(b); if (dB) logb -= 2 * dbllog2(dB);
1483 166468 : i = (long)((degpol(B) * log2N2(A) + degpol(A) * logb) / 2);
1484 166467 : return gc_ulong(av, (i <= 0)? 1: 1 + (ulong)i);
1485 : }
1486 : /* A,B ZX. Return ZX_ZXY_ResBound(A(x), B(x+y)) */
1487 : static ulong
1488 10085 : ZX_ZXY_ResBound_1(GEN A, GEN B)
1489 : {
1490 10085 : pari_sp av = avma;
1491 10085 : GEN b = gen_0;
1492 10085 : long i, lB = lg(B);
1493 41732 : for (i=2; i<lB; i++)
1494 : {
1495 31650 : b = addii(b, sqri(ZX_norml1_1(B, i-2)));
1496 31647 : if (gc_needed(av,1))
1497 : {
1498 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_ResBound i = %ld",i);
1499 0 : b = gerepileupto(av, b);
1500 : }
1501 : }
1502 10082 : i = (long)((degpol(B) * log2N2(A) + degpol(A) * dbllog2(b)) / 2);
1503 10085 : return gc_ulong(av, (i <= 0)? 1: 1 + (ulong)i);
1504 : }
1505 : /* special case B = A' */
1506 : static ulong
1507 1130509 : ZX_discbound(GEN A)
1508 : {
1509 1130509 : pari_sp av = avma;
1510 1130509 : GEN a = gen_0, b = gen_0;
1511 1130509 : long i , lA = lg(A), dA = degpol(A);
1512 : double loga, logb;
1513 6739341 : for (i = 2; i < lA; i++)
1514 : {
1515 5609082 : GEN c = sqri(gel(A,i));
1516 5608781 : a = addii(a, c);
1517 5608854 : if (i > 2) b = addii(b, mulii(c, sqru(i-2)));
1518 5608789 : if (gc_needed(av,1))
1519 : {
1520 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZX_discbound i = %ld",i);
1521 0 : gerepileall(av, 2, &a, &b);
1522 : }
1523 : }
1524 1130259 : loga = dbllog2(a);
1525 1130399 : logb = dbllog2(b); set_avma(av);
1526 1130430 : i = (long)(((dA-1) * loga + dA * logb) / 2);
1527 1130430 : return (i <= 0)? 1: 1 + (ulong)i;
1528 : }
1529 :
1530 : /* return Res(a(Y), b(n,Y)) over Fp. la = leading_coeff(a) [for efficiency] */
1531 : static ulong
1532 5535616 : Flx_FlxY_eval_resultant(GEN a, GEN b, ulong n, ulong p, ulong pi, ulong la)
1533 : {
1534 5535616 : GEN ev = FlxY_evalx_pre(b, n, p, pi);
1535 5536475 : long drop = lg(b) - lg(ev);
1536 5536475 : ulong r = Flx_resultant_pre(a, ev, p, pi);
1537 5535519 : if (drop && la != 1) r = Fl_mul(r, Fl_powu_pre(la, drop, p, pi), p);
1538 5535529 : return r;
1539 : }
1540 : static GEN
1541 284 : FpX_FpXY_eval_resultant(GEN a, GEN b, GEN n, GEN p, GEN la, long db, long vX)
1542 : {
1543 284 : GEN ev = FpXY_evaly(b, n, p, vX);
1544 284 : long drop = db-degpol(ev);
1545 284 : GEN r = FpX_resultant(a, ev, p);
1546 284 : if (drop && !gequal1(la)) r = Fp_mul(r, Fp_powu(la, drop,p),p);
1547 284 : return r;
1548 : }
1549 :
1550 : /* assume dres := deg(Res_X(a,b), Y) <= deg(a,X) * deg(b,Y) < p */
1551 : /* Return a Fly */
1552 : static GEN
1553 368323 : Flx_FlxY_resultant_polint(GEN a, GEN b, ulong p, ulong pi, long dres, long sx)
1554 : {
1555 : long i;
1556 368323 : ulong n, la = Flx_lead(a);
1557 368323 : GEN x = cgetg(dres+2, t_VECSMALL);
1558 368322 : GEN y = cgetg(dres+2, t_VECSMALL);
1559 : /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
1560 : * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
1561 2955446 : for (i=0,n = 1; i < dres; n++)
1562 : {
1563 2587128 : x[++i] = n; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
1564 2587041 : x[++i] = p-n; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
1565 : }
1566 368318 : if (i == dres)
1567 : {
1568 362812 : x[++i] = 0; y[i] = Flx_FlxY_eval_resultant(a,b, x[i], p,pi,la);
1569 : }
1570 368316 : return Flv_polint(x,y, p, sx);
1571 : }
1572 :
1573 : static GEN
1574 7496 : FlxX_pseudorem(GEN x, GEN y, ulong p, ulong pi)
1575 : {
1576 7496 : long vx = varn(x), dx, dy, dz, i, lx, dp;
1577 7496 : pari_sp av = avma, av2;
1578 :
1579 7496 : if (!signe(y)) pari_err_INV("FlxX_pseudorem",y);
1580 7496 : (void)new_chunk(2);
1581 7498 : dx=degpol(x); x = RgX_recip_i(x)+2;
1582 7499 : dy=degpol(y); y = RgX_recip_i(y)+2; dz=dx-dy; dp = dz+1;
1583 7500 : av2 = avma;
1584 : for (;;)
1585 : {
1586 61358 : gel(x,0) = Flx_neg(gel(x,0), p); dp--;
1587 229753 : for (i=1; i<=dy; i++)
1588 167898 : gel(x,i) = Flx_add( Flx_mul_pre(gel(y,0), gel(x,i), p, pi),
1589 168325 : Flx_mul_pre(gel(x,0), gel(y,i), p, pi), p );
1590 1103889 : for ( ; i<=dx; i++)
1591 1043265 : gel(x,i) = Flx_mul_pre(gel(y,0), gel(x,i), p, pi);
1592 65213 : do { x++; dx--; } while (dx >= 0 && lg(gel(x,0))==2);
1593 60624 : if (dx < dy) break;
1594 53129 : if (gc_needed(av2,1))
1595 : {
1596 0 : if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_pseudorem dx = %ld >= %ld",dx,dy);
1597 0 : gerepilecoeffs(av2,x,dx+1);
1598 : }
1599 : }
1600 7495 : if (dx < 0) return zero_Flx(0);
1601 7495 : lx = dx+3; x -= 2;
1602 7495 : x[0]=evaltyp(t_POL) | _evallg(lx);
1603 7495 : x[1]=evalsigne(1) | evalvarn(vx);
1604 7495 : x = RgX_recip_i(x);
1605 7495 : if (dp)
1606 : { /* multiply by y[0]^dp [beware dummy vars from FpX_FpXY_resultant] */
1607 1958 : GEN t = Flx_powu_pre(gel(y,0), dp, p, pi);
1608 7835 : for (i=2; i<lx; i++) gel(x,i) = Flx_mul_pre(gel(x,i), t, p, pi);
1609 : }
1610 7496 : return gerepilecopy(av, x);
1611 : }
1612 :
1613 : /* return a Flx */
1614 : GEN
1615 2510 : FlxX_resultant(GEN u, GEN v, ulong p, long sx)
1616 : {
1617 2510 : pari_sp av = avma, av2;
1618 : long degq, dx, dy, du, dv, dr, signh;
1619 : ulong pi;
1620 : GEN z, g, h, r, p1;
1621 :
1622 2510 : dx = degpol(u); dy = degpol(v); signh = 1;
1623 2510 : if (dx < dy)
1624 : {
1625 7 : swap(u,v); lswap(dx,dy);
1626 7 : if (both_odd(dx, dy)) signh = -signh;
1627 : }
1628 2510 : if (dy < 0) return zero_Flx(sx);
1629 2510 : pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1630 2510 : if (dy==0) return gerepileupto(av, Flx_powu_pre(gel(v,2),dx,p,pi));
1631 :
1632 2510 : g = h = pol1_Flx(sx); av2 = avma;
1633 : for(;;)
1634 : {
1635 7497 : r = FlxX_pseudorem(u,v,p,pi); dr = lg(r);
1636 7501 : if (dr == 2) { set_avma(av); return zero_Flx(sx); }
1637 7501 : du = degpol(u); dv = degpol(v); degq = du-dv;
1638 7501 : u = v; p1 = g; g = leading_coeff(u);
1639 7502 : switch(degq)
1640 : {
1641 0 : case 0: break;
1642 5529 : case 1:
1643 5529 : p1 = Flx_mul_pre(h,p1, p, pi); h = g; break;
1644 1973 : default:
1645 1973 : p1 = Flx_mul_pre(Flx_powu_pre(h,degq,p,pi), p1, p, pi);
1646 1972 : h = Flx_div_pre(Flx_powu_pre(g,degq,p,pi),
1647 1973 : Flx_powu_pre(h,degq-1,p,pi), p, pi);
1648 : }
1649 7494 : if (both_odd(du,dv)) signh = -signh;
1650 7493 : v = FlxY_Flx_div(r, p1, p);
1651 7492 : if (dr==3) break;
1652 4984 : if (gc_needed(av2,1))
1653 : {
1654 0 : if(DEBUGMEM>1) pari_warn(warnmem,"FlxX_resultant, dr = %ld",dr);
1655 0 : gerepileall(av2,4, &u, &v, &g, &h);
1656 : }
1657 : }
1658 2508 : z = gel(v,2);
1659 2508 : if (dv > 1) z = Flx_div_pre(Flx_powu_pre(z,dv,p,pi),
1660 0 : Flx_powu_pre(h,dv-1,p,pi), p, pi);
1661 2508 : if (signh < 0) z = Flx_neg(z,p);
1662 2508 : return gerepileupto(av, z);
1663 : }
1664 :
1665 : /* Warning:
1666 : * This function switches between valid and invalid variable ordering*/
1667 :
1668 : static GEN
1669 6127 : FlxY_to_FlyX(GEN b, long sv)
1670 : {
1671 6127 : long i, n=-1;
1672 6127 : long sw = b[1]&VARNBITS;
1673 20897 : for(i=2;i<lg(b);i++) n = maxss(n,lgpol(gel(b,i)));
1674 6127 : return Flm_to_FlxX(Flm_transpose(FlxX_to_Flm(b,n)),sv,sw);
1675 : }
1676 :
1677 : /* Return a Fly*/
1678 : GEN
1679 6127 : Flx_FlxY_resultant(GEN a, GEN b, ulong p)
1680 : {
1681 6127 : pari_sp ltop=avma;
1682 6127 : long dres = degpol(a)*degpol(b);
1683 6127 : long sx=a[1], sy=b[1]&VARNBITS;
1684 : GEN z;
1685 6127 : b = FlxY_to_FlyX(b,sx);
1686 6126 : if ((ulong)dres >= p)
1687 2508 : z = FlxX_resultant(Fly_to_FlxY(a, sy), b, p, sx);
1688 : else
1689 : {
1690 3618 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
1691 3618 : z = Flx_FlxY_resultant_polint(a, b, p, pi, (ulong)dres, sy);
1692 : }
1693 6128 : return gerepileupto(ltop,z);
1694 : }
1695 :
1696 : /* Return a t_POL in variable vc whose coeffs are the coeffs of b in
1697 : * variable v; vc must have higher priority than all variables occuring in b. */
1698 : GEN
1699 145797 : swap_vars(GEN b, long v, long vc)
1700 : {
1701 145797 : long i, n = RgX_degree(b, v);
1702 : GEN c, x;
1703 145795 : if (n < 0) return pol_0(vc);
1704 145795 : c = cgetg(n+3, t_POL); x = c + 2;
1705 145795 : c[1] = evalsigne(1) | evalvarn(vc);
1706 966837 : for (i = 0; i <= n; i++) gel(x,i) = polcoef_i(b, i, v);
1707 145796 : return c;
1708 : }
1709 :
1710 : /* assume varn(b) << varn(a) */
1711 : /* return a FpY*/
1712 : GEN
1713 15 : FpX_FpXY_resultant(GEN a, GEN b, GEN p)
1714 : {
1715 15 : long i,n,dres, db, vY = varn(b), vX = varn(a);
1716 : GEN la,x,y;
1717 :
1718 15 : if (lgefint(p) == 3)
1719 : {
1720 0 : ulong pp = uel(p,2);
1721 0 : b = ZXX_to_FlxX(b, pp, vX);
1722 0 : a = ZX_to_Flx(a, pp);
1723 0 : x = Flx_FlxY_resultant(a, b, pp);
1724 0 : return Flx_to_ZX(x);
1725 : }
1726 15 : db = RgXY_degreex(b);
1727 15 : dres = degpol(a)*degpol(b);
1728 15 : la = leading_coeff(a);
1729 15 : x = cgetg(dres+2, t_VEC);
1730 15 : y = cgetg(dres+2, t_VEC);
1731 : /* Evaluate at dres+ 1 points: 0 (if dres even) and +/- n, so that P_n(X) =
1732 : * P_{-n}(-X), where P_i is Lagrange polynomial: P_i(j) = delta_{i,j} */
1733 157 : for (i=0,n = 1; i < dres; n++)
1734 : {
1735 142 : gel(x,++i) = utoipos(n);
1736 142 : gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
1737 142 : gel(x,++i) = subiu(p,n);
1738 142 : gel(y,i) = FpX_FpXY_eval_resultant(a,b,gel(x,i),p,la,db,vY);
1739 : }
1740 15 : if (i == dres)
1741 : {
1742 0 : gel(x,++i) = gen_0;
1743 0 : gel(y,i) = FpX_FpXY_eval_resultant(a,b, gel(x,i), p,la,db,vY);
1744 : }
1745 15 : return FpV_polint(x,y, p, vY);
1746 : }
1747 :
1748 : GEN
1749 79 : FpX_composedsum(GEN P, GEN Q, GEN p)
1750 : {
1751 79 : pari_sp av = avma;
1752 79 : if (lgefint(p)==3)
1753 : {
1754 0 : ulong pp = p[2];
1755 0 : GEN z = Flx_composedsum(ZX_to_Flx(P, pp), ZX_to_Flx(Q, pp), pp);
1756 0 : return gerepileupto(av, Flx_to_ZX(z));
1757 : }
1758 : else
1759 : {
1760 79 : long n = 1+ degpol(P)*degpol(Q);
1761 79 : GEN Pl = FpX_invLaplace(FpX_Newton(P,n,p), p);
1762 79 : GEN Ql = FpX_invLaplace(FpX_Newton(Q,n,p), p);
1763 79 : GEN L = FpX_Laplace(FpXn_mul(Pl, Ql, n, p), p);
1764 79 : GEN lead = Fp_mul(Fp_powu(leading_coeff(P),degpol(Q), p),
1765 79 : Fp_powu(leading_coeff(Q),degpol(P), p), p);
1766 79 : GEN R = FpX_fromNewton(L, p);
1767 79 : return gerepileupto(av, FpX_Fp_mul(R, lead, p));
1768 : }
1769 : }
1770 :
1771 : GEN
1772 0 : FpX_composedprod(GEN P, GEN Q, GEN p)
1773 : {
1774 0 : pari_sp av = avma;
1775 0 : if (lgefint(p)==3)
1776 : {
1777 0 : ulong pp = p[2];
1778 0 : GEN z = Flx_composedprod(ZX_to_Flx(P, pp), ZX_to_Flx(Q, pp), pp);
1779 0 : return gerepileupto(av, Flx_to_ZX(z));
1780 : }
1781 : else
1782 : {
1783 0 : long n = 1+ degpol(P)*degpol(Q);
1784 0 : GEN L = FpX_convol(FpX_Newton(P,n,p), FpX_Newton(Q,n,p), p);
1785 0 : return gerepileupto(av,FpX_fromNewton(L, p));
1786 : }
1787 : }
1788 :
1789 : static GEN
1790 79 : _FpX_composedsum(void *E, GEN a, GEN b)
1791 79 : { return FpX_composedsum(a,b, (GEN)E); }
1792 :
1793 : GEN
1794 1574 : FpXV_composedsum(GEN V, GEN p)
1795 : {
1796 1574 : if (lgefint(p)==3)
1797 : {
1798 0 : ulong pp = p[2];
1799 0 : return Flx_to_ZX(FlxV_composedsum(ZXV_to_FlxV(V, pp), pp));
1800 : }
1801 1574 : return gen_product(V, (void *)p, &_FpX_composedsum);
1802 : }
1803 :
1804 : /* 0, 1, -1, 2, -2, ... */
1805 : #define next_lambda(a) (a>0 ? -a : 1-a)
1806 :
1807 : /* Assume A in Z[Y], B in Q[Y][X], B squarefree in (Q[Y]/(A))[X] and
1808 : * Res_Y(A, B) in Z[X]. Find a small lambda (start from *lambda, use
1809 : * next_lambda successively) such that C(X) = Res_Y(A(Y), B(X + lambda Y))
1810 : * is squarefree, reset *lambda to the chosen value and return C. Set LERS to
1811 : * the Last nonconstant polynomial in the Euclidean Remainder Sequence */
1812 : static GEN
1813 21623 : ZX_ZXY_resultant_LERS(GEN A, GEN B0, long *plambda, GEN *LERS)
1814 : {
1815 : ulong bound, dp;
1816 21623 : pari_sp av = avma, av2 = 0;
1817 21623 : long lambda = *plambda, degA = degpol(A), dres = degA*degpol(B0);
1818 : long stable, checksqfree, i,n, cnt, degB;
1819 21623 : long v, vX = varn(B0), vY = varn(A); /* vY < vX */
1820 : GEN x, y, dglist, B, q, a, b, ev, H, H0, H1, Hp, H0p, H1p, C0, C1;
1821 : forprime_t S;
1822 :
1823 21623 : if (degA == 1)
1824 : {
1825 1197 : GEN a1 = gel(A,3), a0 = gel(A,2);
1826 1197 : B = lambda? RgX_translate(B0, monomial(stoi(lambda), 1, vY)): B0;
1827 1197 : H = gsubst(B, vY, gdiv(gneg(a0),a1));
1828 1197 : if (!equali1(a1)) H = RgX_Rg_mul(H, powiu(a1, poldegree(B,vY)));
1829 1197 : *LERS = mkvec2(scalarpol_shallow(a0,vX), scalarpol_shallow(a1,vX));
1830 1197 : return gc_all(av, 2, &H, LERS);
1831 : }
1832 :
1833 20426 : dglist = Hp = H0p = H1p = C0 = C1 = NULL; /* gcc -Wall */
1834 20426 : C0 = cgetg(dres+2, t_VECSMALL);
1835 20426 : C1 = cgetg(dres+2, t_VECSMALL);
1836 20426 : dglist = cgetg(dres+1, t_VECSMALL);
1837 20426 : x = cgetg(dres+2, t_VECSMALL);
1838 20426 : y = cgetg(dres+2, t_VECSMALL);
1839 20426 : B0 = leafcopy(B0);
1840 20426 : A = leafcopy(A);
1841 20426 : B = B0;
1842 20426 : v = fetch_var_higher(); setvarn(A,v);
1843 : /* make sure p large enough */
1844 21242 : INIT:
1845 : /* always except the first time */
1846 21242 : if (av2) { set_avma(av2); lambda = next_lambda(lambda); }
1847 21242 : if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
1848 21242 : B = swap_vars(B, vY, v);
1849 : /* B0(lambda v + x, v) */
1850 21242 : if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
1851 21242 : av2 = avma;
1852 :
1853 21242 : if (degA <= 3)
1854 : { /* sub-resultant faster for small degrees */
1855 10570 : H = RgX_resultant_all(A,B,&q);
1856 10570 : if (typ(q) != t_POL || degpol(q)!=1) goto INIT;
1857 9905 : H0 = gel(q,2);
1858 9905 : if (typ(H0) == t_POL) setvarn(H0,vX); else H0 = scalarpol(H0,vX);
1859 9905 : H1 = gel(q,3);
1860 9905 : if (typ(H1) == t_POL) setvarn(H1,vX); else H1 = scalarpol(H1,vX);
1861 9905 : if (!ZX_is_squarefree(H)) goto INIT;
1862 9863 : goto END;
1863 : }
1864 :
1865 10672 : H = H0 = H1 = NULL;
1866 10672 : degB = degpol(B);
1867 10672 : bound = ZX_ZXY_ResBound(A, B, NULL);
1868 10671 : if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
1869 10671 : dp = 1;
1870 10671 : init_modular_big(&S);
1871 10671 : for(cnt = 0, checksqfree = 1;;)
1872 49157 : {
1873 59828 : ulong p = u_forprime_next(&S);
1874 : GEN Hi;
1875 59828 : a = ZX_to_Flx(A, p);
1876 59830 : b = ZXX_to_FlxX(B, p, varn(A));
1877 59830 : if (degpol(a) < degA || degpol(b) < degB) continue; /* p | lc(A)lc(B) */
1878 59830 : if (checksqfree)
1879 : { /* find degree list for generic Euclidean Remainder Sequence */
1880 10672 : long goal = minss(degpol(a), degpol(b)); /* longest possible */
1881 73077 : for (n=1; n <= goal; n++) dglist[n] = 0;
1882 10672 : setlg(dglist, 1);
1883 23601 : for (n=0; n <= dres; n++)
1884 : {
1885 23209 : ev = FlxY_evalx_drop(b, n, p);
1886 23209 : Flx_resultant_set_dglist(a, ev, dglist, p);
1887 23209 : if (lg(dglist)-1 == goal) break;
1888 : }
1889 : /* last pol in ERS has degree > 1 ? */
1890 10672 : goal = lg(dglist)-1;
1891 10672 : if (degpol(B) == 1) { if (!goal) goto INIT; }
1892 : else
1893 : {
1894 10616 : if (goal <= 1) goto INIT;
1895 10560 : if (dglist[goal] != 0 || dglist[goal-1] != 1) goto INIT;
1896 : }
1897 10616 : if (DEBUGLEVEL>4)
1898 0 : err_printf("Degree list for ERS (trials: %ld) = %Ps\n",n+1,dglist);
1899 : }
1900 :
1901 2144954 : for (i=0,n = 0; i <= dres; n++)
1902 : {
1903 2085180 : ev = FlxY_evalx_drop(b, n, p);
1904 2084954 : x[++i] = n; y[i] = Flx_resultant_all(a, ev, C0+i, C1+i, dglist, p);
1905 2085180 : if (!C1[i]) i--; /* C1(i) = 0. No way to recover C0(i) */
1906 : }
1907 59774 : Hi = Flv_Flm_polint(x, mkvec3(y,C0,C1), p, 0);
1908 59775 : Hp = gel(Hi,1); H0p = gel(Hi,2); H1p = gel(Hi,3);
1909 59775 : if (!H && degpol(Hp) != dres) continue;
1910 59775 : if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
1911 59775 : if (checksqfree) {
1912 10616 : if (!Flx_is_squarefree(Hp, p)) goto INIT;
1913 10563 : if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
1914 10563 : checksqfree = 0;
1915 : }
1916 :
1917 59722 : if (!H)
1918 : { /* initialize */
1919 10563 : q = utoipos(p); stable = 0;
1920 10563 : H = ZX_init_CRT(Hp, p,vX);
1921 10563 : H0= ZX_init_CRT(H0p, p,vX);
1922 10563 : H1= ZX_init_CRT(H1p, p,vX);
1923 : }
1924 : else
1925 : {
1926 49159 : GEN qp = muliu(q,p);
1927 49158 : stable = ZX_incremental_CRT_raw(&H, Hp, q,qp, p)
1928 49158 : & ZX_incremental_CRT_raw(&H0,H0p, q,qp, p)
1929 49158 : & ZX_incremental_CRT_raw(&H1,H1p, q,qp, p);
1930 49157 : q = qp;
1931 : }
1932 : /* could make it probabilistic for H ? [e.g if stable twice, etc]
1933 : * Probabilistic anyway for H0, H1 */
1934 59720 : if (DEBUGLEVEL>5 && (stable || ++cnt==100))
1935 0 : { cnt=0; err_printf("%ld%%%s ",100*expi(q)/bound,stable?"s":""); }
1936 59721 : if (stable && (ulong)expi(q) >= bound) break; /* DONE */
1937 49158 : if (gc_needed(av,2))
1938 : {
1939 0 : if (DEBUGMEM>1) pari_warn(warnmem,"ZX_ZXY_rnfequation");
1940 0 : gerepileall(av2, 4, &H, &q, &H0, &H1);
1941 : }
1942 : }
1943 20426 : END:
1944 20426 : if (DEBUGLEVEL>5) err_printf(" done\n");
1945 20426 : setvarn(H, vX); (void)delete_var();
1946 20426 : *LERS = mkvec2(H0,H1);
1947 20426 : *plambda = lambda; return gc_all(av, 2, &H, LERS);
1948 : }
1949 :
1950 : GEN
1951 59436 : ZX_ZXY_resultant_all(GEN A, GEN B, long *plambda, GEN *LERS)
1952 : {
1953 59436 : if (LERS)
1954 : {
1955 21623 : if (!plambda)
1956 0 : pari_err_BUG("ZX_ZXY_resultant_all [LERS != NULL needs lambda]");
1957 21623 : return ZX_ZXY_resultant_LERS(A, B, plambda, LERS);
1958 : }
1959 37813 : return ZX_ZXY_rnfequation(A, B, plambda);
1960 : }
1961 :
1962 : /* If lambda = NULL, return caract(Mod(A, T)), T,A in Z[X].
1963 : * Otherwise find a small lambda such that caract (Mod(A + lambda X, T)) is
1964 : * squarefree */
1965 : GEN
1966 22554 : ZXQ_charpoly_sqf(GEN A, GEN T, long *lambda, long v)
1967 : {
1968 22554 : pari_sp av = avma;
1969 : GEN R, a;
1970 : long dA;
1971 : int delvar;
1972 :
1973 22554 : if (v < 0) v = 0;
1974 22554 : switch (typ(A))
1975 : {
1976 22554 : case t_POL: dA = degpol(A); if (dA > 0) break;
1977 0 : A = constant_coeff(A);
1978 0 : default:
1979 0 : if (lambda) { A = scalar_ZX_shallow(A,varn(T)); dA = 0; break;}
1980 0 : return gerepileupto(av, gpowgs(gsub(pol_x(v), A), degpol(T)));
1981 : }
1982 22554 : delvar = 0;
1983 22554 : if (varncmp(varn(T), 0) <= 0)
1984 : {
1985 3646 : long v0 = fetch_var(); delvar = 1;
1986 3646 : T = leafcopy(T); setvarn(T,v0);
1987 3646 : A = leafcopy(A); setvarn(A,v0);
1988 : }
1989 22554 : R = ZX_ZXY_rnfequation(T, deg1pol_shallow(gen_1, gneg_i(A), 0), lambda);
1990 22554 : if (delvar) (void)delete_var();
1991 22554 : setvarn(R, v); a = leading_coeff(T);
1992 22554 : if (!gequal1(a)) R = gdiv(R, powiu(a, dA));
1993 22554 : return gerepileupto(av, R);
1994 : }
1995 :
1996 : /* charpoly(Mod(A,T)), A may be in Q[X], but assume T and result are integral */
1997 : GEN
1998 994052 : ZXQ_charpoly(GEN A, GEN T, long v)
1999 : {
2000 994052 : return (degpol(T) < 16) ? RgXQ_charpoly_i(A,T,v): ZXQ_charpoly_sqf(A,T, NULL, v);
2001 : }
2002 :
2003 : GEN
2004 9780 : QXQ_charpoly(GEN A, GEN T, long v)
2005 : {
2006 9780 : pari_sp av = avma;
2007 9780 : GEN den, B = Q_remove_denom(A, &den);
2008 9780 : GEN P = ZXQ_charpoly(B, T, v);
2009 9780 : return gerepilecopy(av, den ? RgX_rescale(P, ginv(den)): P);
2010 : }
2011 :
2012 : static ulong
2013 3799821 : ZX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, ulong p)
2014 : {
2015 3799821 : long dropa = degA - degpol(a), dropb = degB - degpol(b);
2016 : ulong H, dp;
2017 3799712 : if (dropa && dropb) return 0; /* p | lc(A), p | lc(B) */
2018 3799712 : H = Flx_resultant(a, b, p);
2019 3799290 : if (dropa)
2020 : { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
2021 0 : ulong c = b[degB+2]; /* lc(B) */
2022 0 : if (odd(degB)) c = p - c;
2023 0 : c = Fl_powu(c, dropa, p);
2024 0 : if (c != 1) H = Fl_mul(H, c, p);
2025 : }
2026 3799290 : else if (dropb)
2027 : { /* multiply by lc(A)^(deg B - deg b) */
2028 0 : ulong c = a[degA+2]; /* lc(A) */
2029 0 : c = Fl_powu(c, dropb, p);
2030 0 : if (c != 1) H = Fl_mul(H, c, p);
2031 : }
2032 3799300 : dp = dB ? umodiu(dB, p): 1;
2033 3799300 : if (dp != 1) H = Fl_mul(H, Fl_powu(Fl_inv(dp,p), degA, p), p);
2034 3799303 : return H;
2035 : }
2036 :
2037 : /* If B=NULL, assume B=A' */
2038 : static GEN
2039 1482354 : ZX_resultant_slice(GEN A, GEN B, GEN dB, GEN P, GEN *mod)
2040 : {
2041 1482354 : pari_sp av = avma, av2;
2042 1482354 : long degA, degB, i, n = lg(P)-1;
2043 : GEN H, T;
2044 :
2045 1482354 : degA = degpol(A);
2046 1482350 : degB = B? degpol(B): degA - 1;
2047 1482350 : if (n == 1)
2048 : {
2049 810315 : ulong Hp, p = uel(P,1);
2050 810315 : GEN a = ZX_to_Flx(A, p), b = B? ZX_to_Flx(B, p): Flx_deriv(a, p);
2051 810313 : Hp = ZX_resultant_prime(a, b, dB, degA, degB, p);
2052 810293 : set_avma(av); *mod = utoipos(p); return utoi(Hp);
2053 : }
2054 672035 : T = ZV_producttree(P);
2055 672034 : A = ZX_nv_mod_tree(A, P, T);
2056 672032 : if (B) B = ZX_nv_mod_tree(B, P, T);
2057 672032 : H = cgetg(n+1, t_VECSMALL); av2 = avma;
2058 3661117 : for(i=1; i <= n; i++, set_avma(av2))
2059 : {
2060 2989092 : ulong p = P[i];
2061 2989092 : GEN a = gel(A,i), b = B? gel(B,i): Flx_deriv(a, p);
2062 2989510 : H[i] = ZX_resultant_prime(a, b, dB, degA, degB, p);
2063 : }
2064 672025 : H = ZV_chinese_tree(H, P, T, ZV_chinesetree(P,T));
2065 672031 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
2066 : }
2067 :
2068 : GEN
2069 1482370 : ZX_resultant_worker(GEN P, GEN A, GEN B, GEN dB)
2070 : {
2071 1482370 : GEN V = cgetg(3, t_VEC);
2072 1482353 : if (typ(B) == t_INT) B = NULL;
2073 1482353 : if (!signe(dB)) dB = NULL;
2074 1482353 : gel(V,1) = ZX_resultant_slice(A, B, dB, P, &gel(V,2));
2075 1482352 : return V;
2076 : }
2077 :
2078 : /* Compute Res(A, B/dB) in Z, assuming A,B in Z[X], dB in Z or NULL (= 1)
2079 : * If B=NULL, take B = A' and assume deg A > 1 and 'bound' is set */
2080 : GEN
2081 1347322 : ZX_resultant_all(GEN A, GEN B, GEN dB, ulong bound)
2082 : {
2083 1347322 : pari_sp av = avma;
2084 : forprime_t S;
2085 : GEN H, worker;
2086 1347322 : if (!B && degpol(A)==2)
2087 : {
2088 113919 : GEN a = gel(A,4), b = gel(A,3), c = gel(A,2);
2089 113919 : H = mulii(a, subii(shifti(mulii(a, c), 2), sqri(b)));
2090 113909 : if (dB) H = diviiexact(H, sqri(dB));
2091 113909 : return gerepileuptoint(av, H);
2092 : }
2093 1233403 : if (B)
2094 : {
2095 155067 : long a = degpol(A), b = degpol(B);
2096 155067 : if (a < 0 || b < 0) return gen_0;
2097 155037 : if (!a) return powiu(gel(A,2), b);
2098 155037 : if (!b) return powiu(gel(B,2), a);
2099 153292 : if (minss(a, b) <= 1)
2100 : {
2101 76662 : H = RgX_resultant_all(A, B, NULL);
2102 76662 : if (dB) H = diviiexact(H, powiu(dB, a));
2103 76662 : return gerepileuptoint(av, H);
2104 : }
2105 76630 : if (!bound) bound = ZX_ZXY_ResBound(A, B, dB);
2106 : }
2107 1154976 : worker = snm_closure(is_entry("_ZX_resultant_worker"),
2108 : mkvec3(A, B? B: gen_0, dB? dB: gen_0));
2109 1155085 : init_modular_big(&S);
2110 1155036 : H = gen_crt("ZX_resultant_all", worker, &S, dB, bound, 0, NULL,
2111 : ZV_chinese_center, Fp_center);
2112 1155067 : return gerepileuptoint(av, H);
2113 : }
2114 :
2115 : /* A0 and B0 in Q[X] */
2116 : GEN
2117 56 : QX_resultant(GEN A0, GEN B0)
2118 : {
2119 : GEN s, a, b, A, B;
2120 56 : pari_sp av = avma;
2121 :
2122 56 : A = Q_primitive_part(A0, &a);
2123 56 : B = Q_primitive_part(B0, &b);
2124 56 : s = ZX_resultant(A, B);
2125 56 : if (!signe(s)) { set_avma(av); return gen_0; }
2126 56 : if (a) s = gmul(s, gpowgs(a,degpol(B)));
2127 56 : if (b) s = gmul(s, gpowgs(b,degpol(A)));
2128 56 : return gerepileupto(av, s);
2129 : }
2130 :
2131 : GEN
2132 57239 : ZX_resultant(GEN A, GEN B) { return ZX_resultant_all(A,B,NULL,0); }
2133 :
2134 : GEN
2135 0 : QXQ_intnorm(GEN A, GEN B)
2136 : {
2137 : GEN c, n, R, lB;
2138 0 : long dA = degpol(A), dB = degpol(B);
2139 0 : pari_sp av = avma;
2140 0 : if (dA < 0) return gen_0;
2141 0 : A = Q_primitive_part(A, &c);
2142 0 : if (!c || typ(c) == t_INT) {
2143 0 : n = c;
2144 0 : R = ZX_resultant(B, A);
2145 : } else {
2146 0 : n = gel(c,1);
2147 0 : R = ZX_resultant_all(B, A, gel(c,2), 0);
2148 : }
2149 0 : if (n && !equali1(n)) R = mulii(R, powiu(n, dB));
2150 0 : lB = leading_coeff(B);
2151 0 : if (!equali1(lB)) R = diviiexact(R, powiu(lB, dA));
2152 0 : return gerepileuptoint(av, R);
2153 : }
2154 :
2155 : GEN
2156 19418 : QXQ_norm(GEN A, GEN B)
2157 : {
2158 : GEN c, R, lB;
2159 19418 : long dA = degpol(A), dB = degpol(B);
2160 19418 : pari_sp av = avma;
2161 19418 : if (dA < 0) return gen_0;
2162 19418 : A = Q_primitive_part(A, &c);
2163 19418 : R = ZX_resultant(B, A);
2164 19418 : if (c) R = gmul(R, gpowgs(c, dB));
2165 19418 : lB = leading_coeff(B);
2166 19418 : if (!equali1(lB)) R = gdiv(R, gpowgs(lB, dA));
2167 19418 : return gerepileupto(av, R);
2168 : }
2169 :
2170 : /* assume x has integral coefficients */
2171 : GEN
2172 1195553 : ZX_disc_all(GEN x, ulong bound)
2173 : {
2174 1195553 : pari_sp av = avma;
2175 1195553 : long s, d = degpol(x);
2176 : GEN l, R;
2177 :
2178 1195555 : if (d <= 1) return d == 1? gen_1: gen_0;
2179 1192325 : s = (d & 2) ? -1: 1;
2180 1192325 : l = leading_coeff(x);
2181 1192324 : if (!bound) bound = ZX_discbound(x);
2182 1192244 : R = ZX_resultant_all(x, NULL, NULL, bound);
2183 1192320 : if (is_pm1(l))
2184 1016823 : { if (signe(l) < 0) s = -s; }
2185 : else
2186 175495 : R = diviiexact(R,l);
2187 1192318 : if (s == -1) togglesign_safe(&R);
2188 1192315 : return gerepileuptoint(av,R);
2189 : }
2190 :
2191 : GEN
2192 1133691 : ZX_disc(GEN x) { return ZX_disc_all(x,0); }
2193 :
2194 : static GEN
2195 10404 : ZXQX_resultant_prime(GEN a, GEN b, GEN dB, long degA, long degB, GEN T, ulong p)
2196 : {
2197 10404 : long dropa = degA - degpol(a), dropb = degB - degpol(b);
2198 : GEN H, dp;
2199 10404 : if (dropa && dropb) return pol0_Flx(T[1]); /* p | lc(A), p | lc(B) */
2200 10404 : H = FlxqX_saferesultant(a, b, T, p);
2201 10405 : if (!H) return NULL;
2202 10405 : if (dropa)
2203 : { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
2204 0 : GEN c = gel(b,degB+2); /* lc(B) */
2205 0 : if (odd(degB)) c = Flx_neg(c, p);
2206 0 : c = Flxq_powu(c, dropa, T, p);
2207 0 : if (!Flx_equal1(c)) H = Flxq_mul(H, c, T, p);
2208 : }
2209 10405 : else if (dropb)
2210 : { /* multiply by lc(A)^(deg B - deg b) */
2211 0 : GEN c = gel(a,degA+2); /* lc(A) */
2212 0 : c = Flxq_powu(c, dropb, T, p);
2213 0 : if (!Flx_equal1(c)) H = Flxq_mul(H, c, T, p);
2214 : }
2215 10405 : dp = dB ? ZX_to_Flx(dB, p): pol1_Flx(T[1]);
2216 10405 : if (!Flx_equal1(dp))
2217 : {
2218 0 : GEN idp = Flxq_invsafe(dp, T, p);
2219 0 : if (!idp) return NULL;
2220 0 : H = Flxq_mul(H, Flxq_powu(idp, degA, T, p), T, p);
2221 : }
2222 10405 : return H;
2223 : }
2224 :
2225 : /* If B=NULL, assume B=A' */
2226 : static GEN
2227 4447 : ZXQX_resultant_slice(GEN A, GEN B, GEN U, GEN dB, GEN P, GEN *mod)
2228 : {
2229 4447 : pari_sp av = avma;
2230 4447 : long degA, degB, i, n = lg(P)-1;
2231 : GEN H, T;
2232 4447 : long v = varn(U), redo = 0;
2233 :
2234 4447 : degA = degpol(A);
2235 4447 : degB = B? degpol(B): degA - 1;
2236 4447 : if (n == 1)
2237 : {
2238 2760 : ulong p = uel(P,1);
2239 2760 : GEN a = ZXX_to_FlxX(A, p, v), b = B? ZXX_to_FlxX(B, p, v): FlxX_deriv(a, p);
2240 2760 : GEN u = ZX_to_Flx(U, p);
2241 2760 : GEN Hp = ZXQX_resultant_prime(a, b, dB, degA, degB, u, p);
2242 2760 : if (!Hp) { set_avma(av); *mod = gen_1; return pol_0(v); }
2243 2760 : Hp = gerepileupto(av, Flx_to_ZX(Hp)); *mod = utoipos(p); return Hp;
2244 : }
2245 1687 : T = ZV_producttree(P);
2246 1687 : A = ZXX_nv_mod_tree(A, P, T, v);
2247 1687 : if (B) B = ZXX_nv_mod_tree(B, P, T, v);
2248 1687 : U = ZX_nv_mod_tree(U, P, T);
2249 1687 : H = cgetg(n+1, t_VEC);
2250 9332 : for(i=1; i <= n; i++)
2251 : {
2252 7645 : ulong p = P[i];
2253 7645 : GEN a = gel(A,i), b = B? gel(B,i): FlxX_deriv(a, p), u = gel(U, i);
2254 7644 : GEN h = ZXQX_resultant_prime(a, b, dB, degA, degB, u, p);
2255 7645 : if (!h)
2256 : {
2257 0 : gel(H,i) = pol_0(v);
2258 0 : P[i] = 1; redo = 1;
2259 : }
2260 : else
2261 7645 : gel(H,i) = h;
2262 : }
2263 1687 : if (redo) T = ZV_producttree(P);
2264 1687 : H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
2265 1687 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
2266 : }
2267 :
2268 : GEN
2269 4447 : ZXQX_resultant_worker(GEN P, GEN A, GEN B, GEN T, GEN dB)
2270 : {
2271 4447 : GEN V = cgetg(3, t_VEC);
2272 4447 : if (isintzero(B)) B = NULL;
2273 4447 : if (!signe(dB)) dB = NULL;
2274 4447 : gel(V,1) = ZXQX_resultant_slice(A, B, T, dB, P, &gel(V,2));
2275 4447 : return V;
2276 : }
2277 :
2278 : static ulong
2279 3874 : ZXQX_resultant_bound_i(GEN nf, GEN A, GEN B, GEN (*f)(GEN,GEN,long))
2280 : {
2281 3874 : pari_sp av = avma;
2282 3874 : GEN r, M = nf_L2_bound(nf, NULL, &r);
2283 3874 : long v = nf_get_varn(nf), i, l = lg(r);
2284 3874 : GEN a = cgetg(l, t_COL);
2285 12053 : for (i = 1; i < l; i++)
2286 8179 : gel(a, i) = f(gsubst(A, v, gel(r,i)), gsubst(B, v, gel(r,i)), DEFAULTPREC);
2287 3874 : return gc_ulong(av, (ulong) dbllog2(gmul(M,RgC_fpnorml2(a, DEFAULTPREC))));
2288 : }
2289 : static ulong
2290 3559 : ZXQX_resultant_bound(GEN nf, GEN A, GEN B)
2291 3559 : { return ZXQX_resultant_bound_i(nf, A, B, &RgX_RgXY_ResBound); }
2292 :
2293 : static GEN
2294 56 : _ZXQ_powu(GEN x, ulong u, GEN T)
2295 56 : { return typ(x) == t_INT? powiu(x, u): ZXQ_powu(x, u, T); }
2296 :
2297 : /* Compute Res(A, B/dB) in Z[X]/T, assuming A,B in Z[X,Y], dB in Z or NULL (= 1)
2298 : * If B=NULL, take B = A' and assume deg A > 1 */
2299 : static GEN
2300 3556 : ZXQX_resultant_all(GEN A, GEN B, GEN T, GEN dB, ulong bound)
2301 : {
2302 3556 : pari_sp av = avma;
2303 : forprime_t S;
2304 : GEN H, worker;
2305 3556 : if (B)
2306 : {
2307 63 : long a = degpol(A), b = degpol(B);
2308 63 : if (a < 0 || b < 0) return gen_0;
2309 63 : if (!a) return _ZXQ_powu(gel(A,2), b, T);
2310 63 : if (!b) return _ZXQ_powu(gel(B,2), a, T);
2311 : } else
2312 3493 : if (!bound) B = RgX_deriv(A);
2313 3556 : if (!bound) bound = ZXQX_resultant_bound(nfinit(T, DEFAULTPREC), A, B);
2314 3556 : worker = snm_closure(is_entry("_ZXQX_resultant_worker"),
2315 : mkvec4(A, B? B: gen_0, T, dB? dB: gen_0));
2316 3556 : init_modular_big(&S);
2317 3556 : H = gen_crt("ZXQX_resultant_all", worker, &S, dB, bound, 0, NULL,
2318 : nxV_chinese_center, FpX_center);
2319 3556 : if (DEBUGLEVEL)
2320 0 : err_printf("ZXQX_resultant_all: a priori bound: %lu, a posteriori: %lu\n",
2321 : bound, expi(gsupnorm(H, DEFAULTPREC)));
2322 3556 : return gerepileupto(av, H);
2323 : }
2324 :
2325 : GEN
2326 119 : nfX_resultant(GEN nf, GEN x, GEN y)
2327 : {
2328 119 : pari_sp av = avma;
2329 119 : GEN cx, cy, D, T = nf_get_pol(nf);
2330 119 : long dx = degpol(x), dy = degpol(y);
2331 119 : if (dx < 0 || dy < 0) return gen_0;
2332 119 : x = Q_primitive_part(x, &cx); if (cx) cx = gpowgs(cx, dy);
2333 119 : y = Q_primitive_part(y, &cy); if (cy) cy = gpowgs(cy, dx);
2334 119 : if (!dx) D = _ZXQ_powu(gel(x,2), dy, T);
2335 119 : else if (!dy) D = _ZXQ_powu(gel(y,2), dx, T);
2336 : else
2337 : {
2338 63 : ulong bound = ZXQX_resultant_bound(nf, x, y);
2339 63 : D = ZXQX_resultant_all(x, y, T, NULL, bound);
2340 : }
2341 119 : cx = mul_content(cx, cy); if (cx) D = gmul(D, cx);
2342 119 : return gerepileupto(av, D);
2343 : }
2344 :
2345 : static GEN
2346 231 : to_ZX(GEN a, long v) { return typ(a)==t_INT? scalarpol(a,v): a; }
2347 :
2348 : static GEN
2349 3493 : ZXQX_disc_all(GEN x, GEN T, ulong bound)
2350 : {
2351 3493 : pari_sp av = avma;
2352 3493 : long s, d = degpol(x), v = varn(T);
2353 : GEN l, R;
2354 :
2355 3493 : if (d <= 1) return d == 1? pol_1(v): pol_0(v);
2356 3493 : s = (d & 2) ? -1: 1;
2357 3493 : l = leading_coeff(x);
2358 3493 : R = ZXQX_resultant_all(x, NULL, T, NULL, bound);
2359 3493 : if (!gequal1(l)) R = QXQ_div(R, to_ZX(l,v), T);
2360 3493 : if (s == -1) R = RgX_neg(R);
2361 3493 : return gerepileupto(av, R);
2362 : }
2363 :
2364 : GEN
2365 7 : QX_disc(GEN x)
2366 : {
2367 7 : pari_sp av = avma;
2368 7 : GEN c, d = ZX_disc( Q_primitive_part(x, &c) );
2369 7 : if (c) d = gmul(d, gpowgs(c, 2*degpol(x) - 2));
2370 7 : return gerepileupto(av, d);
2371 : }
2372 :
2373 : GEN
2374 3689 : nfX_disc(GEN nf, GEN x)
2375 : {
2376 3689 : pari_sp av = avma;
2377 3689 : GEN c, D, T = nf_get_pol(nf);
2378 : ulong bound;
2379 3689 : long d = degpol(x), v = varn(T);
2380 3689 : if (d <= 1) return d == 1? pol_1(v): pol_0(v);
2381 3493 : x = Q_primitive_part(x, &c);
2382 3493 : bound = ZXQX_resultant_bound(nf, x, RgX_deriv(x));
2383 3493 : D = ZXQX_disc_all(x, T, bound);
2384 3493 : if (c) D = gmul(D, gpowgs(c, 2*d - 2));
2385 3493 : return gerepileupto(av, D);
2386 : }
2387 :
2388 : GEN
2389 835533 : QXQ_mul(GEN x, GEN y, GEN T)
2390 : {
2391 835533 : GEN dx, nx = Q_primitive_part(x, &dx);
2392 835533 : GEN dy, ny = Q_primitive_part(y, &dy);
2393 835529 : GEN z = ZXQ_mul(nx, ny, T);
2394 835532 : if (dx || dy)
2395 : {
2396 832733 : GEN d = dx ? dy ? gmul(dx, dy): dx : dy;
2397 832733 : if (!gequal1(d)) z = ZX_Q_mul(z, d);
2398 : }
2399 835533 : return z;
2400 : }
2401 :
2402 : GEN
2403 399428 : QXQ_sqr(GEN x, GEN T)
2404 : {
2405 399428 : GEN dx, nx = Q_primitive_part(x, &dx);
2406 399428 : GEN z = ZXQ_sqr(nx, T);
2407 399428 : if (dx)
2408 397692 : z = ZX_Q_mul(z, gsqr(dx));
2409 399428 : return z;
2410 : }
2411 :
2412 : static GEN
2413 212259 : QXQ_inv_slice(GEN A, GEN B, GEN P, GEN *mod)
2414 : {
2415 212259 : pari_sp av = avma;
2416 212259 : long i, n = lg(P)-1, v = varn(A), redo = 0;
2417 : GEN H, T;
2418 212259 : if (n == 1)
2419 : {
2420 165271 : ulong p = uel(P,1);
2421 165271 : GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
2422 165271 : GEN U = Flxq_invsafe(a, b, p);
2423 165271 : if (!U)
2424 : {
2425 24 : set_avma(av);
2426 24 : *mod = gen_1; return pol_0(v);
2427 : }
2428 165247 : H = gerepilecopy(av, Flx_to_ZX(U));
2429 165247 : *mod = utoipos(p); return H;
2430 : }
2431 46988 : T = ZV_producttree(P);
2432 46988 : A = ZX_nv_mod_tree(A, P, T);
2433 46988 : B = ZX_nv_mod_tree(B, P, T);
2434 46988 : H = cgetg(n+1, t_VEC);
2435 237693 : for(i=1; i <= n; i++)
2436 : {
2437 190705 : ulong p = P[i];
2438 190705 : GEN a = gel(A,i), b = gel(B,i);
2439 190705 : GEN U = Flxq_invsafe(a, b, p);
2440 190706 : if (!U)
2441 : {
2442 601 : gel(H,i) = pol_0(v);
2443 601 : P[i] = 1; redo = 1;
2444 : }
2445 : else
2446 190105 : gel(H,i) = U;
2447 : }
2448 46988 : if (redo) T = ZV_producttree(P);
2449 46988 : H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
2450 46988 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
2451 : }
2452 :
2453 : GEN
2454 212259 : QXQ_inv_worker(GEN P, GEN A, GEN B)
2455 : {
2456 212259 : GEN V = cgetg(3, t_VEC);
2457 212259 : gel(V,1) = QXQ_inv_slice(A, B, P, &gel(V,2));
2458 212259 : return V;
2459 : }
2460 :
2461 : /* lift(1 / Mod(A,B)). B a ZX, A a scalar or a QX */
2462 : GEN
2463 145672 : QXQ_inv(GEN A, GEN B)
2464 : {
2465 : GEN D, Ap, Bp;
2466 : ulong pp;
2467 145672 : pari_sp av2, av = avma;
2468 : forprime_t S;
2469 145672 : GEN worker, U, H = NULL, mod = gen_1;
2470 : pari_timer ti;
2471 : long k, dA, dB;
2472 145672 : if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
2473 : /* A a QX, B a ZX */
2474 145672 : A = Q_primitive_part(A, &D);
2475 145672 : dA = degpol(A); dB= degpol(B);
2476 : /* A, B in Z[X] */
2477 145672 : init_modular_small(&S);
2478 : do {
2479 145672 : pp = u_forprime_next(&S);
2480 145672 : Ap = ZX_to_Flx(A, pp);
2481 145672 : Bp = ZX_to_Flx(B, pp);
2482 145672 : } while (degpol(Ap) != dA || degpol(Bp) != dB);
2483 145672 : if (degpol(Flx_gcd(Ap, Bp, pp)) != 0 && degpol(ZX_gcd(A,B))!=0)
2484 14 : pari_err_INV("QXQ_inv",mkpolmod(A,B));
2485 145658 : worker = snm_closure(is_entry("_QXQ_inv_worker"), mkvec2(A, B));
2486 145658 : av2 = avma;
2487 145658 : for (k = 1; ;k *= 2)
2488 42407 : {
2489 : GEN res, b, N, den;
2490 188065 : gen_inccrt_i("QXQ_inv", worker, NULL, (k+1)>>1, 0, &S, &H, &mod,
2491 : nxV_chinese_center, FpX_center);
2492 188064 : gerepileall(av2, 2, &H, &mod);
2493 188065 : b = sqrti(shifti(mod,-1));
2494 188065 : if (DEBUGLEVEL>5) timer_start(&ti);
2495 188065 : U = FpX_ratlift(H, mod, b, b, NULL);
2496 188065 : if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: ratlift");
2497 193760 : if (!U) continue;
2498 151353 : N = Q_remove_denom(U, &den); if (!den) den = gen_1;
2499 151352 : res = Flx_rem(Flx_Fl_sub(Flx_mul(Ap, ZX_to_Flx(N,pp), pp),
2500 : umodiu(den, pp), pp), Bp, pp);
2501 151353 : if (degpol(res) >= 0) continue;
2502 145658 : res = ZX_Z_sub(ZX_mul(A, N), den);
2503 145657 : res = ZX_is_monic(B) ? ZX_rem(res, B): RgX_pseudorem(res, B);
2504 145657 : if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_inv: final check");
2505 145657 : if (degpol(res)<0)
2506 : {
2507 145657 : if (D) U = RgX_Rg_div(U, D);
2508 145657 : return gerepilecopy(av, U);
2509 : }
2510 : }
2511 : }
2512 :
2513 : static GEN
2514 120238 : QXQ_div_slice(GEN A, GEN B, GEN C, GEN P, GEN *mod)
2515 : {
2516 120238 : pari_sp av = avma;
2517 120238 : long i, n = lg(P)-1, v = varn(A), redo = 0;
2518 : GEN H, T;
2519 120238 : if (n == 1)
2520 : {
2521 43886 : ulong p = uel(P,1);
2522 43886 : GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p), c = ZX_to_Flx(C, p);
2523 43886 : GEN bi = Flxq_invsafe(b, c, p), U;
2524 43886 : if (!bi)
2525 : {
2526 0 : set_avma(av);
2527 0 : *mod = gen_1; return pol_0(v);
2528 : }
2529 43886 : U = Flxq_mul(a, bi, c, p);
2530 43886 : H = gerepilecopy(av, Flx_to_ZX(U));
2531 43886 : *mod = utoipos(p); return H;
2532 : }
2533 76352 : T = ZV_producttree(P);
2534 76352 : A = ZX_nv_mod_tree(A, P, T);
2535 76352 : B = ZX_nv_mod_tree(B, P, T);
2536 76352 : C = ZX_nv_mod_tree(C, P, T);
2537 76352 : H = cgetg(n+1, t_VEC);
2538 337294 : for(i=1; i <= n; i++)
2539 : {
2540 260942 : ulong p = P[i];
2541 260942 : GEN a = gel(A,i), b = gel(B,i), c = gel(C, i);
2542 260942 : GEN bi = Flxq_invsafe(b, c, p);
2543 260942 : if (!bi)
2544 : {
2545 4 : gel(H,i) = pol_0(v);
2546 4 : P[i] = 1; redo = 1;
2547 : }
2548 : else
2549 260938 : gel(H,i) = Flxq_mul(a, bi, c, p);
2550 : }
2551 76352 : if (redo) T = ZV_producttree(P);
2552 76352 : H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
2553 76352 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
2554 : }
2555 :
2556 : GEN
2557 120238 : QXQ_div_worker(GEN P, GEN A, GEN B, GEN C)
2558 : {
2559 120238 : GEN V = cgetg(3, t_VEC);
2560 120238 : gel(V,1) = QXQ_div_slice(A, B, C, P, &gel(V,2));
2561 120238 : return V;
2562 : }
2563 :
2564 : /* lift(Mod(A/B, C)). C a ZX, A, B a scalar or a QX */
2565 : GEN
2566 32521 : QXQ_div(GEN A, GEN B, GEN C)
2567 : {
2568 : GEN DA, DB, Ap, Bp, Cp;
2569 : ulong pp;
2570 32521 : pari_sp av2, av = avma;
2571 : forprime_t S;
2572 32521 : GEN worker, U, H = NULL, mod = gen_1;
2573 : pari_timer ti;
2574 : long k, dA, dB, dC;
2575 32521 : if (is_scalar_t(typ(A))) return scalarpol(ginv(A), varn(B));
2576 : /* A a QX, B a ZX */
2577 32521 : A = Q_primitive_part(A, &DA);
2578 32521 : B = Q_primitive_part(B, &DB);
2579 32521 : dA = degpol(A); dB = degpol(B); dC = degpol(C);
2580 : /* A, B in Z[X] */
2581 32521 : init_modular_small(&S);
2582 : do {
2583 32521 : pp = u_forprime_next(&S);
2584 32521 : Ap = ZX_to_Flx(A, pp);
2585 32521 : Bp = ZX_to_Flx(B, pp);
2586 32521 : Cp = ZX_to_Flx(C, pp);
2587 32521 : } while (degpol(Ap) != dA || degpol(Bp) != dB || degpol(Cp) != dC);
2588 32521 : if (degpol(Flx_gcd(Bp, Cp, pp)) != 0 && degpol(ZX_gcd(B,C))!=0)
2589 0 : pari_err_INV("QXQ_div",mkpolmod(B,C));
2590 32521 : worker = snm_closure(is_entry("_QXQ_div_worker"), mkvec3(A, B, C));
2591 32521 : av2 = avma;
2592 32521 : for (k = 1; ;k *= 2)
2593 46540 : {
2594 : GEN res, b, N, den;
2595 79061 : gen_inccrt_i("QXQ_div", worker, NULL, (k+1)>>1, 0, &S, &H, &mod,
2596 : nxV_chinese_center, FpX_center);
2597 79061 : gerepileall(av2, 2, &H, &mod);
2598 79061 : b = sqrti(shifti(mod,-1));
2599 79061 : if (DEBUGLEVEL>5) timer_start(&ti);
2600 79061 : U = FpX_ratlift(H, mod, b, b, NULL);
2601 79061 : if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: ratlift");
2602 89676 : if (!U) continue;
2603 43136 : N = Q_remove_denom(U, &den); if (!den) den = gen_1;
2604 43136 : res = Flx_rem(Flx_sub(Flx_mul(Bp, ZX_to_Flx(N,pp), pp),
2605 : Flx_Fl_mul(Ap, umodiu(den, pp), pp), pp), Cp, pp);
2606 43136 : if (degpol(res) >= 0) continue;
2607 32521 : res = ZX_sub(ZX_mul(B, N), ZX_Z_mul(A,den));
2608 32521 : res = ZX_is_monic(C) ? ZX_rem(res, C): RgX_pseudorem(res, C);
2609 32521 : if (DEBUGLEVEL>5) timer_printf(&ti,"QXQ_div: final check");
2610 32521 : if (degpol(res)<0)
2611 : {
2612 32521 : if (DA && DB) U = RgX_Rg_mul(U, gdiv(DA,DB));
2613 27635 : else if (DA) U = RgX_Rg_mul(U, DA);
2614 15596 : else if (DB) U = RgX_Rg_div(U, DB);
2615 32521 : return gerepilecopy(av, U);
2616 : }
2617 : }
2618 : }
2619 :
2620 : /************************************************************************
2621 : * *
2622 : * ZXQ_minpoly *
2623 : * *
2624 : ************************************************************************/
2625 :
2626 : static GEN
2627 3523 : ZXQ_minpoly_slice(GEN A, GEN B, long d, GEN P, GEN *mod)
2628 : {
2629 3523 : pari_sp av = avma;
2630 3523 : long i, n = lg(P)-1, v = evalvarn(varn(B));
2631 : GEN H, T;
2632 3523 : if (n == 1)
2633 : {
2634 716 : ulong p = uel(P,1);
2635 716 : GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
2636 716 : GEN Hp = Flxq_minpoly(a, b, p);
2637 716 : if (degpol(Hp) != d) { p = 1; Hp = pol0_Flx(v); }
2638 716 : H = gerepileupto(av, Flx_to_ZX(Hp));
2639 716 : *mod = utoipos(p); return H;
2640 : }
2641 2807 : T = ZV_producttree(P);
2642 2807 : A = ZX_nv_mod_tree(A, P, T);
2643 2807 : B = ZX_nv_mod_tree(B, P, T);
2644 2807 : H = cgetg(n+1, t_VEC);
2645 16838 : for(i=1; i <= n; i++)
2646 : {
2647 14031 : ulong p = P[i];
2648 14031 : GEN a = gel(A,i), b = gel(B,i);
2649 14031 : GEN m = Flxq_minpoly(a, b, p);
2650 14031 : if (degpol(m) != d) { P[i] = 1; m = pol0_Flx(v); }
2651 14031 : gel(H, i) = m;
2652 : }
2653 2807 : H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
2654 2807 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
2655 : }
2656 :
2657 : GEN
2658 3523 : ZXQ_minpoly_worker(GEN P, GEN A, GEN B, long d)
2659 : {
2660 3523 : GEN V = cgetg(3, t_VEC);
2661 3523 : gel(V,1) = ZXQ_minpoly_slice(A, B, d, P, &gel(V,2));
2662 3523 : return V;
2663 : }
2664 :
2665 : GEN
2666 1701 : ZXQ_minpoly(GEN A, GEN B, long d, ulong bound)
2667 : {
2668 1701 : pari_sp av = avma;
2669 : GEN worker, H, dB;
2670 : forprime_t S;
2671 1701 : B = Q_remove_denom(B, &dB);
2672 1701 : worker = strtoclosure("_ZXQ_minpoly_worker", 3, A, B, stoi(d));
2673 1701 : init_modular_big(&S);
2674 1701 : H = gen_crt("ZXQ_minpoly", worker, &S, dB, bound, 0, NULL,
2675 : nxV_chinese_center, FpX_center_i);
2676 1701 : return gerepilecopy(av, H);
2677 : }
2678 :
2679 : /************************************************************************
2680 : * *
2681 : * ZX_ZXY_resultant *
2682 : * *
2683 : ************************************************************************/
2684 :
2685 : static GEN
2686 364704 : ZX_ZXY_resultant_prime(GEN a, GEN b, ulong dp, ulong p,
2687 : long degA, long degB, long dres, long sX)
2688 : {
2689 364704 : long dropa = degA - degpol(a), dropb = degB - degpol(b);
2690 364705 : ulong pi = SMALL_ULONG(p)? 0: get_Fl_red(p);
2691 364705 : GEN Hp = Flx_FlxY_resultant_polint(a, b, p, pi, dres, sX);
2692 364705 : if (dropa && dropb)
2693 0 : Hp = zero_Flx(sX);
2694 : else {
2695 364705 : if (dropa)
2696 : { /* multiply by ((-1)^deg B lc(B))^(deg A - deg a) */
2697 0 : GEN c = gel(b,degB+2); /* lc(B) */
2698 0 : if (odd(degB)) c = Flx_neg(c, p);
2699 0 : if (!Flx_equal1(c)) {
2700 0 : c = Flx_powu_pre(c, dropa, p, pi);
2701 0 : if (!Flx_equal1(c)) Hp = Flx_mul_pre(Hp, c, p, pi);
2702 : }
2703 : }
2704 364705 : else if (dropb)
2705 : { /* multiply by lc(A)^(deg B - deg b) */
2706 0 : ulong c = uel(a, degA+2); /* lc(A) */
2707 0 : c = Fl_powu(c, dropb, p);
2708 0 : if (c != 1) Hp = Flx_Fl_mul_pre(Hp, c, p, pi);
2709 : }
2710 : }
2711 364705 : if (dp != 1) Hp = Flx_Fl_mul_pre(Hp, Fl_powu_pre(Fl_inv(dp,p), degA, p, pi), p, pi);
2712 364706 : return Hp;
2713 : }
2714 :
2715 : static GEN
2716 124910 : ZX_ZXY_resultant_slice(GEN A, GEN B, GEN dB, long degA, long degB, long dres,
2717 : GEN P, GEN *mod, long sX, long vY)
2718 : {
2719 124910 : pari_sp av = avma;
2720 124910 : long i, n = lg(P)-1;
2721 : GEN H, T, D;
2722 124910 : if (n == 1)
2723 : {
2724 40200 : ulong p = uel(P,1);
2725 40200 : ulong dp = dB ? umodiu(dB, p): 1;
2726 40200 : GEN a = ZX_to_Flx(A, p), b = ZXX_to_FlxX(B, p, vY);
2727 40200 : GEN Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
2728 40200 : H = gerepileupto(av, Flx_to_ZX(Hp));
2729 40200 : *mod = utoipos(p); return H;
2730 : }
2731 84710 : T = ZV_producttree(P);
2732 84710 : A = ZX_nv_mod_tree(A, P, T);
2733 84710 : B = ZXX_nv_mod_tree(B, P, T, vY);
2734 84710 : D = dB ? Z_ZV_mod_tree(dB, P, T): NULL;
2735 84710 : H = cgetg(n+1, t_VEC);
2736 363866 : for(i=1; i <= n; i++)
2737 : {
2738 279155 : ulong p = P[i];
2739 279155 : GEN a = gel(A,i), b = gel(B,i);
2740 279155 : ulong dp = D ? uel(D, i): 1;
2741 279155 : gel(H,i) = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
2742 : }
2743 84711 : H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
2744 84710 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
2745 : }
2746 :
2747 : GEN
2748 124910 : ZX_ZXY_resultant_worker(GEN P, GEN A, GEN B, GEN dB, GEN v)
2749 : {
2750 124910 : GEN V = cgetg(3, t_VEC);
2751 124910 : if (isintzero(dB)) dB = NULL;
2752 124910 : gel(V,1) = ZX_ZXY_resultant_slice(A, B, dB, v[1], v[2], v[3], P, &gel(V,2), v[4], v[5]);
2753 124910 : return V;
2754 : }
2755 :
2756 : GEN
2757 79170 : ZX_ZXY_resultant(GEN A, GEN B)
2758 : {
2759 79170 : pari_sp av = avma;
2760 : forprime_t S;
2761 : ulong bound;
2762 79170 : long v = fetch_var_higher();
2763 79170 : long degA = degpol(A), degB, dres = degA * degpol(B);
2764 79170 : long vX = varn(B), vY = varn(A); /* assume vY has lower priority */
2765 79170 : long sX = evalvarn(vX);
2766 : GEN worker, H, dB;
2767 79170 : B = Q_remove_denom(B, &dB);
2768 79170 : if (!dB) B = leafcopy(B);
2769 79170 : A = leafcopy(A); setvarn(A,v);
2770 79170 : B = swap_vars(B, vY, v); degB = degpol(B);
2771 79170 : bound = ZX_ZXY_ResBound(A, B, dB);
2772 79170 : if (DEBUGLEVEL>4) err_printf("bound for resultant coeffs: 2^%ld\n",bound);
2773 158340 : worker = snm_closure(is_entry("_ZX_ZXY_resultant_worker"),
2774 79170 : mkvec4(A, B, dB? dB: gen_0,
2775 : mkvecsmall5(degA, degB, dres, sX, vY)));
2776 79170 : init_modular_big(&S);
2777 79170 : H = gen_crt("ZX_ZXY_resultant_all", worker, &S, dB, bound, 0, NULL,
2778 : nxV_chinese_center, FpX_center_i);
2779 79170 : setvarn(H, vX); (void)delete_var();
2780 79170 : return gerepilecopy(av, H);
2781 : }
2782 :
2783 : static long
2784 40536 : ZX_ZXY_rnfequation_lambda(GEN A, GEN B0, long lambda)
2785 : {
2786 40536 : pari_sp av = avma;
2787 40536 : long degA = degpol(A), degB, dres = degA*degpol(B0);
2788 40536 : long v = fetch_var_higher();
2789 40536 : long vX = varn(B0), vY = varn(A); /* assume vY has lower priority */
2790 40536 : long sX = evalvarn(vX);
2791 : GEN dB, B, a, b, Hp;
2792 : forprime_t S;
2793 :
2794 40536 : B0 = Q_remove_denom(B0, &dB);
2795 40537 : if (!dB) B0 = leafcopy(B0);
2796 40537 : A = leafcopy(A);
2797 40537 : B = B0;
2798 40537 : setvarn(A,v);
2799 45350 : INIT:
2800 45350 : if (lambda) B = RgX_translate(B0, monomial(stoi(lambda), 1, vY));
2801 45350 : B = swap_vars(B, vY, v);
2802 : /* B0(lambda v + x, v) */
2803 45348 : if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
2804 :
2805 45348 : degB = degpol(B);
2806 45348 : init_modular_big(&S);
2807 : while (1)
2808 0 : {
2809 45348 : ulong p = u_forprime_next(&S);
2810 45348 : ulong dp = dB ? umodiu(dB, p): 1;
2811 45348 : if (!dp) continue;
2812 45348 : a = ZX_to_Flx(A, p);
2813 45350 : b = ZXX_to_FlxX(B, p, v);
2814 45350 : Hp = ZX_ZXY_resultant_prime(a, b, dp, p, degA, degB, dres, sX);
2815 45350 : if (degpol(Hp) != dres) continue;
2816 45350 : if (dp != 1) Hp = Flx_Fl_mul(Hp, Fl_powu(Fl_inv(dp,p), degA, p), p);
2817 45350 : if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); goto INIT; }
2818 40537 : if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
2819 40537 : (void)delete_var(); return gc_long(av,lambda);
2820 : }
2821 : }
2822 :
2823 : GEN
2824 60535 : ZX_ZXY_rnfequation(GEN A, GEN B, long *lambda)
2825 : {
2826 60535 : if (lambda)
2827 : {
2828 40536 : *lambda = ZX_ZXY_rnfequation_lambda(A, B, *lambda);
2829 40537 : if (*lambda) B = RgX_translate(B, monomial(stoi(*lambda), 1, varn(A)));
2830 : }
2831 60536 : return ZX_ZXY_resultant(A,B);
2832 : }
2833 :
2834 : static GEN
2835 10352 : ZX_composedsum_slice(GEN A, GEN B, GEN P, GEN *mod)
2836 : {
2837 10352 : pari_sp av = avma;
2838 10352 : long i, n = lg(P)-1;
2839 : GEN H, T;
2840 10352 : if (n == 1)
2841 : {
2842 9850 : ulong p = uel(P,1);
2843 9850 : GEN a = ZX_to_Flx(A, p), b = ZX_to_Flx(B, p);
2844 9846 : GEN Hp = Flx_composedsum(a, b, p);
2845 9843 : H = gerepileupto(av, Flx_to_ZX(Hp));
2846 9847 : *mod = utoipos(p); return H;
2847 : }
2848 502 : T = ZV_producttree(P);
2849 502 : A = ZX_nv_mod_tree(A, P, T);
2850 502 : B = ZX_nv_mod_tree(B, P, T);
2851 502 : H = cgetg(n+1, t_VEC);
2852 4526 : for(i=1; i <= n; i++)
2853 : {
2854 4024 : ulong p = P[i];
2855 4024 : GEN a = gel(A,i), b = gel(B,i);
2856 4024 : gel(H,i) = Flx_composedsum(a, b, p);
2857 : }
2858 502 : H = nxV_chinese_center_tree(H, P, T, ZV_chinesetree(P, T));
2859 502 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
2860 : }
2861 :
2862 : GEN
2863 10352 : ZX_composedsum_worker(GEN P, GEN A, GEN B)
2864 : {
2865 10352 : GEN V = cgetg(3, t_VEC);
2866 10352 : gel(V,1) = ZX_composedsum_slice(A, B, P, &gel(V,2));
2867 10349 : return V;
2868 : }
2869 :
2870 : static GEN
2871 10084 : ZX_composedsum_i(GEN A, GEN B, GEN lead)
2872 : {
2873 10084 : pari_sp av = avma;
2874 : forprime_t S;
2875 : ulong bound;
2876 : GEN H, worker, mod;
2877 10084 : if (degpol(A) < degpol(B)) swap(A, B);
2878 10085 : if (!lead) lead = mulii(leading_coeff(A),leading_coeff(B));
2879 10085 : bound = ZX_ZXY_ResBound_1(A, B);
2880 10085 : worker = snm_closure(is_entry("_ZX_composedsum_worker"), mkvec2(A,B));
2881 10086 : init_modular_big(&S);
2882 10084 : H = gen_crt("ZX_composedsum", worker, &S, lead, bound, 0, &mod,
2883 : nxV_chinese_center, FpX_center);
2884 10084 : return gerepileupto(av, H);
2885 : }
2886 :
2887 : static long
2888 9699 : ZX_compositum_lambda(GEN A, GEN B, GEN lead, long lambda)
2889 : {
2890 9699 : pari_sp av = avma;
2891 : forprime_t S;
2892 : ulong p;
2893 9699 : init_modular_big(&S);
2894 9700 : p = u_forprime_next(&S);
2895 : while (1)
2896 112 : {
2897 : GEN Hp, a;
2898 9812 : if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
2899 9812 : if (lead && dvdiu(lead,p)) { p = u_forprime_next(&S); continue; }
2900 9805 : a = ZX_to_Flx(ZX_rescale(A, stoi(-lambda)), p);
2901 9805 : Hp = Flx_composedsum(a, ZX_to_Flx(B, p), p);
2902 9798 : if (!Flx_is_squarefree(Hp, p)) { lambda = next_lambda(lambda); continue; }
2903 9687 : if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
2904 9687 : return gc_long(av, lambda);
2905 : }
2906 : }
2907 :
2908 : GEN
2909 9701 : ZX_compositum(GEN A, GEN B, long *lambda)
2910 : {
2911 9701 : GEN lead = mulii(leading_coeff(A),leading_coeff(B));
2912 9699 : if (lambda)
2913 : {
2914 9699 : *lambda = ZX_compositum_lambda(A, B, lead, *lambda);
2915 9687 : A = ZX_rescale(A, stoi(-*lambda));
2916 : }
2917 9699 : return ZX_composedsum_i(A, B, lead);
2918 : }
2919 :
2920 : GEN
2921 385 : ZX_composedsum(GEN A, GEN B)
2922 385 : { return ZX_composedsum_i(A, B, NULL); }
2923 :
2924 : static GEN
2925 359 : ZXQX_composedsum_slice(GEN A, GEN B, GEN C, GEN P, GEN *mod)
2926 : {
2927 359 : pari_sp av = avma;
2928 359 : long i, n = lg(P)-1, dC = degpol(C), v = varn(C);
2929 : GEN H, T;
2930 359 : if (n == 1)
2931 : {
2932 181 : ulong p = uel(P,1);
2933 181 : GEN a = ZXX_to_FlxX(A, p, v), b = ZXX_to_FlxX(B, p, v);
2934 181 : GEN c = ZX_to_Flx(C, p);
2935 181 : GEN Hp = FlxX_to_Flm(FlxqX_composedsum(a, b, c, p), dC);
2936 181 : H = gerepileupto(av, Flm_to_ZM(Hp));
2937 181 : *mod = utoipos(p); return H;
2938 : }
2939 178 : T = ZV_producttree(P);
2940 178 : A = ZXX_nv_mod_tree(A, P, T, v);
2941 178 : B = ZXX_nv_mod_tree(B, P, T, v);
2942 178 : C = ZX_nv_mod_tree(C, P, T);
2943 178 : H = cgetg(n+1, t_VEC);
2944 660 : for(i=1; i <= n; i++)
2945 : {
2946 482 : ulong p = P[i];
2947 482 : GEN a = gel(A,i), b = gel(B,i), c = gel(C,i);
2948 482 : gel(H,i) = FlxX_to_Flm(FlxqX_composedsum(a, b, c, p), dC);
2949 : }
2950 178 : H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
2951 178 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
2952 : }
2953 :
2954 : GEN
2955 359 : ZXQX_composedsum_worker(GEN P, GEN A, GEN B, GEN C)
2956 : {
2957 359 : GEN V = cgetg(3, t_VEC);
2958 359 : gel(V,1) = ZXQX_composedsum_slice(A, B, C, P, &gel(V,2));
2959 359 : return V;
2960 : }
2961 :
2962 : static GEN
2963 315 : ZXQX_composedsum(GEN A, GEN B, GEN T, ulong bound)
2964 : {
2965 315 : pari_sp av = avma;
2966 : forprime_t S;
2967 : GEN H, worker, mod;
2968 315 : GEN lead = mulii(Q_content(leading_coeff(A)), Q_content(leading_coeff(B)));
2969 315 : worker = snm_closure(is_entry("_ZXQX_composedsum_worker")
2970 : , mkvec3(A,B,T));
2971 315 : init_modular_big(&S);
2972 315 : H = gen_crt("ZXQX_composedsum", worker, &S, lead, bound, 0, &mod,
2973 : nmV_chinese_center, FpM_center);
2974 315 : if (DEBUGLEVEL > 4)
2975 0 : err_printf("nfcompositum: a priori bound: %lu, a posteriori: %lu\n",
2976 : bound, expi(gsupnorm(H, DEFAULTPREC)));
2977 315 : return gerepilecopy(av, RgM_to_RgXX(H, varn(A), varn(T)));
2978 : }
2979 :
2980 : static long
2981 315 : ZXQX_composedsum_bound(GEN nf, GEN A, GEN B)
2982 315 : { return ZXQX_resultant_bound_i(nf, A, B, &RgX_RgXY_ResBound_1); }
2983 :
2984 : GEN
2985 315 : nf_direct_compositum(GEN nf, GEN A, GEN B)
2986 : {
2987 315 : ulong bnd = ZXQX_composedsum_bound(nf, A, B);
2988 315 : return ZXQX_composedsum(A, B, nf_get_pol(nf), bnd);
2989 : }
2990 :
2991 : /************************************************************************
2992 : * *
2993 : * IRREDUCIBLE POLYNOMIAL / Fp *
2994 : * *
2995 : ************************************************************************/
2996 :
2997 : /* irreducible (unitary) polynomial of degree n over Fp */
2998 : GEN
2999 0 : ffinit_rand(GEN p,long n)
3000 : {
3001 0 : for(;;) {
3002 0 : pari_sp av = avma;
3003 0 : GEN pol = ZX_add(pol_xn(n, 0), random_FpX(n-1,0, p));
3004 0 : if (FpX_is_irred(pol, p)) return pol;
3005 0 : set_avma(av);
3006 : }
3007 : }
3008 :
3009 : /* return an extension of degree 2^l of F_2, assume l > 0
3010 : * Not stack clean. */
3011 : static GEN
3012 600 : ffinit_Artin_Schreier_2(long l)
3013 : {
3014 : GEN Q, T, S;
3015 : long i, v;
3016 :
3017 600 : if (l == 1) return mkvecsmall4(0,1,1,1); /*x^2 + x + 1*/
3018 551 : v = fetch_var_higher();
3019 551 : S = mkvecsmall5(0, 0, 0, 1, 1); /* y(y^2 + y) */
3020 551 : Q = mkpoln(3, pol1_Flx(0), pol1_Flx(0), S); /* x^2 + x + y(y^2+y) */
3021 551 : setvarn(Q, v);
3022 :
3023 : /* x^4+x+1, irred over F_2, minimal polynomial of a root of Q */
3024 551 : T = mkvecsmalln(6,evalvarn(v),1UL,1UL,0UL,0UL,1UL);
3025 : /* Q = x^2 + x + a(y) irred. over K = F2[y] / (T(y))
3026 : * ==> x^2 + x + a(y) b irred. over K for any root b of Q
3027 : * ==> x^2 + x + (b^2+b)b */
3028 3039 : for (i=2; i<l; i++) T = Flx_FlxY_resultant(T, Q, 2); /* minpoly(b) / F2*/
3029 551 : (void)delete_var(); T[1] = 0; return T;
3030 : }
3031 :
3032 : /* return an extension of degree p^l of F_p, assume l > 0
3033 : * Not stack clean. */
3034 : GEN
3035 957 : ffinit_Artin_Schreier(ulong p, long l)
3036 : {
3037 : long i, v;
3038 : GEN Q, R, S, T, xp;
3039 957 : if (p==2) return ffinit_Artin_Schreier_2(l);
3040 357 : xp = polxn_Flx(p,0); /* x^p */
3041 357 : T = Flx_sub(xp, mkvecsmall3(0,1,1),p); /* x^p - x - 1 */
3042 357 : if (l == 1) return T;
3043 :
3044 7 : v = evalvarn(fetch_var_higher());
3045 7 : xp[1] = v;
3046 7 : R = Flx_sub(polxn_Flx(2*p-1,0), polxn_Flx(p,0),p);
3047 7 : S = Flx_sub(xp, polx_Flx(0), p);
3048 7 : Q = FlxX_Flx_sub(Flx_to_FlxX(S, v), R, p); /* x^p - x - (y^(2p-1)-y^p) */
3049 14 : for (i = 2; i <= l; ++i) T = Flx_FlxY_resultant(T, Q, p);
3050 7 : (void)delete_var(); T[1] = 0; return T;
3051 : }
3052 :
3053 : static long
3054 148780 : flinit_check(ulong p, long n, long l)
3055 : {
3056 : ulong q;
3057 148780 : if (!uisprime(n)) return 0;
3058 102027 : q = p % n; if (!q) return 0;
3059 99486 : return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
3060 : }
3061 :
3062 : static GEN
3063 31776 : flinit(ulong p, long l)
3064 : {
3065 31776 : ulong n = 1+l;
3066 95935 : while (!flinit_check(p,n,l)) n += l;
3067 31776 : if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
3068 31776 : return ZX_to_Flx(polsubcyclo(n,l,0), p);
3069 : }
3070 :
3071 : static GEN
3072 28907 : ffinit_fact_Flx(ulong p, long n)
3073 : {
3074 28907 : GEN P, F = factoru_pow(n), Fp = gel(F,1), Fe = gel(F,2), Fm = gel(F,3);
3075 28907 : long i, l = lg(Fm);
3076 28907 : P = cgetg(l, t_VEC);
3077 61640 : for (i = 1; i < l; i++)
3078 32733 : gel(P,i) = p==uel(Fp,i) ? ffinit_Artin_Schreier(p, Fe[i])
3079 32733 : : flinit(p, uel(Fm,i));
3080 28907 : return FlxV_composedsum(P, p);
3081 : }
3082 :
3083 : static GEN
3084 52852 : init_Flxq_i(ulong p, long n, long sv)
3085 : {
3086 : GEN P;
3087 52852 : if (!odd(p) && p != 2) pari_err_PRIME("ffinit", utoi(p));
3088 52845 : if (n == 1) return polx_Flx(sv);
3089 52845 : if (flinit_check(p, n+1, n))
3090 : {
3091 23938 : P = const_vecsmall(n+2,1);
3092 23938 : P[1] = sv; return P;
3093 : }
3094 28907 : P = ffinit_fact_Flx(p,n);
3095 28907 : P[1] = sv; return P;
3096 : }
3097 :
3098 : GEN
3099 0 : init_Flxq(ulong p, long n, long v)
3100 : {
3101 0 : pari_sp av = avma;
3102 0 : return gerepileupto(av, init_Flxq_i(p, n, v));
3103 : }
3104 :
3105 : /* check if polsubcyclo(n,l,0) is irreducible modulo p */
3106 : static long
3107 7185 : fpinit_check(GEN p, long n, long l)
3108 : {
3109 : ulong q;
3110 7185 : if (!uisprime(n)) return 0;
3111 4450 : q = umodiu(p,n); if (!q) return 0;
3112 4450 : return ugcd((n-1)/Fl_order(q, n-1, n), l) == 1;
3113 : }
3114 :
3115 : /* let k=2 if p%4==1, and k=4 else and assume k*p does not divide l.
3116 : * Return an irreducible polynomial of degree l over F_p.
3117 : * Variant of Adleman and Lenstra "Finding irreducible polynomials over
3118 : * finite fields", ACM, 1986 (5) 350--355.
3119 : * Not stack clean */
3120 : static GEN
3121 1653 : fpinit(GEN p, long l)
3122 : {
3123 1653 : ulong n = 1+l;
3124 5202 : while (!fpinit_check(p,n,l)) n += l;
3125 1653 : if (DEBUGLEVEL>=4) err_printf("FFInit: using polsubcyclo(%ld, %ld)\n",n,l);
3126 1653 : return FpX_red(polsubcyclo(n,l,0),p);
3127 : }
3128 :
3129 : static GEN
3130 1574 : ffinit_fact(GEN p, long n)
3131 : {
3132 1574 : GEN P, F = factoru_pow(n), Fp = gel(F,1), Fe = gel(F,2), Fm = gel(F,3);
3133 1574 : long i, l = lg(Fm);
3134 1574 : P = cgetg(l, t_VEC);
3135 3227 : for (i = 1; i < l; ++i)
3136 3306 : gel(P,i) = absequaliu(p, Fp[i]) ?
3137 0 : Flx_to_ZX(ffinit_Artin_Schreier(Fp[i], Fe[i]))
3138 1653 : : fpinit(p, Fm[i]);
3139 1574 : return FpXV_composedsum(P, p);
3140 : }
3141 :
3142 : static GEN
3143 55101 : init_Fq_i(GEN p, long n, long v)
3144 : {
3145 : GEN P;
3146 55101 : if (n <= 0) pari_err_DOMAIN("ffinit", "degree", "<=", gen_0, stoi(n));
3147 55101 : if (typ(p) != t_INT) pari_err_TYPE("ffinit",p);
3148 55101 : if (cmpiu(p, 2) < 0) pari_err_PRIME("ffinit",p);
3149 55094 : if (v < 0) v = 0;
3150 55094 : if (n == 1) return pol_x(v);
3151 54842 : if (lgefint(p) == 3)
3152 52852 : return Flx_to_ZX(init_Flxq_i(p[2], n, evalvarn(v)));
3153 1990 : if (!mpodd(p)) pari_err_PRIME("ffinit", p);
3154 1983 : if (fpinit_check(p, n+1, n)) return polcyclo(n+1, v);
3155 1574 : P = ffinit_fact(p,n);
3156 1574 : setvarn(P, v); return P;
3157 : }
3158 : GEN
3159 54534 : init_Fq(GEN p, long n, long v)
3160 : {
3161 54534 : pari_sp av = avma;
3162 54534 : return gerepileupto(av, init_Fq_i(p, n, v));
3163 : }
3164 : GEN
3165 567 : ffinit(GEN p, long n, long v)
3166 : {
3167 567 : pari_sp av = avma;
3168 567 : return gerepileupto(av, FpX_to_mod(init_Fq_i(p, n, v), p));
3169 : }
3170 :
3171 : GEN
3172 3178 : ffnbirred(GEN p, long n)
3173 : {
3174 3178 : pari_sp av = avma;
3175 3178 : GEN s = powiu(p,n), F = factoru(n), D = divisorsu_moebius(gel(F, 1));
3176 3178 : long j, l = lg(D);
3177 6797 : for (j = 2; j < l; j++) /* skip d = 1 */
3178 : {
3179 3619 : long md = D[j]; /* mu(d) * d, d squarefree */
3180 3619 : GEN pd = powiu(p, n / labs(md)); /* p^{n/d} */
3181 3619 : s = md > 0? addii(s, pd): subii(s,pd);
3182 : }
3183 3178 : return gerepileuptoint(av, diviuexact(s, n));
3184 : }
3185 :
3186 : GEN
3187 616 : ffsumnbirred(GEN p, long n)
3188 : {
3189 616 : pari_sp av = avma, av2;
3190 616 : GEN q, t = p, v = vecfactoru_i(1, n);
3191 : long i;
3192 616 : q = cgetg(n+1,t_VEC); gel(q,1) = p;
3193 1764 : for (i=2; i<=n; i++) gel(q,i) = mulii(gel(q,i-1), p);
3194 616 : av2 = avma;
3195 1764 : for (i=2; i<=n; i++)
3196 : {
3197 1148 : GEN s = gel(q,i), F = gel(v,i), D = divisorsu_moebius(gel(F,1));
3198 1148 : long j, l = lg(D);
3199 2534 : for (j = 2; j < l; j++) /* skip 1 */
3200 : {
3201 1386 : long md = D[j];
3202 1386 : GEN pd = gel(q, i / labs(md)); /* p^{i/d} */
3203 1386 : s = md > 0? addii(s, pd): subii(s, pd);
3204 : }
3205 1148 : t = gerepileuptoint(av2, addii(t, diviuexact(s, i)));
3206 : }
3207 616 : return gerepileuptoint(av, t);
3208 : }
3209 :
3210 : GEN
3211 140 : ffnbirred0(GEN p, long n, long flag)
3212 : {
3213 140 : if (typ(p) != t_INT) pari_err_TYPE("ffnbirred", p);
3214 140 : if (n <= 0) pari_err_DOMAIN("ffnbirred", "degree", "<=", gen_0, stoi(n));
3215 140 : switch(flag)
3216 : {
3217 70 : case 0: return ffnbirred(p, n);
3218 70 : case 1: return ffsumnbirred(p, n);
3219 : }
3220 0 : pari_err_FLAG("ffnbirred");
3221 : return NULL; /* LCOV_EXCL_LINE */
3222 : }
3223 :
3224 : static void
3225 2261 : checkmap(GEN m, const char *s)
3226 : {
3227 2261 : if (typ(m)!=t_VEC || lg(m)!=3 || typ(gel(m,1))!=t_FFELT)
3228 0 : pari_err_TYPE(s,m);
3229 2261 : }
3230 :
3231 : GEN
3232 189 : ffembed(GEN a, GEN b)
3233 : {
3234 189 : pari_sp av = avma;
3235 189 : GEN p, Ta, Tb, g, r = NULL;
3236 189 : if (typ(a)!=t_FFELT) pari_err_TYPE("ffembed",a);
3237 189 : if (typ(b)!=t_FFELT) pari_err_TYPE("ffembed",b);
3238 189 : p = FF_p_i(a); g = FF_gen(a);
3239 189 : if (!equalii(p, FF_p_i(b))) pari_err_MODULUS("ffembed",a,b);
3240 189 : Ta = FF_mod(a);
3241 189 : Tb = FF_mod(b);
3242 189 : if (degpol(Tb)%degpol(Ta)!=0)
3243 7 : pari_err_DOMAIN("ffembed",GENtostr_raw(a),"is not a subfield of",b,a);
3244 182 : r = gel(FFX_roots(Ta, b), 1);
3245 182 : return gerepilecopy(av, mkvec2(g,r));
3246 : }
3247 :
3248 : GEN
3249 91 : ffextend(GEN a, GEN P, long v)
3250 : {
3251 91 : pari_sp av = avma;
3252 : long n;
3253 : GEN p, T, R, g, m;
3254 91 : if (typ(a)!=t_FFELT) pari_err_TYPE("ffextend",a);
3255 91 : T = a; p = FF_p_i(a);
3256 91 : if (typ(P)!=t_POL || !RgX_is_FpXQX(P,&T,&p)) pari_err_TYPE("ffextend", P);
3257 49 : if (!FF_samefield(a, T)) pari_err_MODULUS("ffextend",a,T);
3258 49 : if (v < 0) v = varn(P);
3259 49 : n = FF_f(T) * degpol(P); R = ffinit(p, n, v); g = ffgen(R, v);
3260 49 : m = ffembed(a, g);
3261 49 : R = FFX_roots(ffmap(m, P),g);
3262 49 : return gerepilecopy(av, mkvec2(gel(R,1), m));
3263 : }
3264 :
3265 : GEN
3266 42 : fffrobenius(GEN a, long n)
3267 : {
3268 42 : if (typ(a)!=t_FFELT) pari_err_TYPE("fffrobenius",a);
3269 42 : retmkvec2(FF_gen(a), FF_Frobenius(a, n));
3270 : }
3271 :
3272 : GEN
3273 133 : ffinvmap(GEN m)
3274 : {
3275 133 : pari_sp av = avma;
3276 : long i, l;
3277 133 : GEN T, F, a, g, r, f = NULL;
3278 133 : checkmap(m, "ffinvmap");
3279 133 : a = gel(m,1); r = gel(m,2);
3280 133 : if (typ(r) != t_FFELT)
3281 7 : pari_err_TYPE("ffinvmap", m);
3282 126 : g = FF_gen(a);
3283 126 : T = FF_mod(r);
3284 126 : F = gel(FFX_factor(T, a), 1);
3285 126 : l = lg(F);
3286 490 : for(i=1; i<l; i++)
3287 : {
3288 490 : GEN s = FFX_rem(FF_to_FpXQ_i(r), gel(F, i), a);
3289 490 : if (degpol(s)==0 && gequal(constant_coeff(s),g)) { f = gel(F, i); break; }
3290 : }
3291 126 : if (f==NULL) pari_err_TYPE("ffinvmap", m);
3292 126 : if (degpol(f)==1) f = FF_neg_i(gel(f,2));
3293 126 : return gerepilecopy(av, mkvec2(FF_gen(r),f));
3294 : }
3295 :
3296 : static GEN
3297 1260 : ffpartmapimage(const char *s, GEN r)
3298 : {
3299 1260 : GEN a = NULL, p = NULL;
3300 1260 : if (typ(r)==t_POL && degpol(r) >= 1
3301 1260 : && RgX_is_FpXQX(r,&a,&p) && a && typ(a)==t_FFELT) return a;
3302 0 : pari_err_TYPE(s, r);
3303 : return NULL; /* LCOV_EXCL_LINE */
3304 : }
3305 :
3306 : static GEN
3307 2709 : ffeltmap_i(GEN m, GEN x)
3308 : {
3309 2709 : GEN r = gel(m,2);
3310 2709 : if (!FF_samefield(x, gel(m,1)))
3311 84 : pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
3312 2625 : if (typ(r)==t_FFELT)
3313 1659 : return FF_map(r, x);
3314 : else
3315 966 : return FFX_preimage(x, r, ffpartmapimage("ffmap", r));
3316 : }
3317 :
3318 : static GEN
3319 4459 : ffmap_i(GEN m, GEN x)
3320 : {
3321 : GEN y;
3322 4459 : long i, lx, tx = typ(x);
3323 4459 : switch(tx)
3324 : {
3325 2541 : case t_FFELT:
3326 2541 : return ffeltmap_i(m, x);
3327 1267 : case t_POL: case t_RFRAC: case t_SER:
3328 : case t_VEC: case t_COL: case t_MAT:
3329 1267 : y = cgetg_copy(x, &lx);
3330 1988 : for (i=1; i<lontyp[tx]; i++) y[i] = x[1];
3331 4564 : for (i=lontyp[tx]; i<lx; i++)
3332 : {
3333 3339 : GEN yi = ffmap_i(m, gel(x,i));
3334 3297 : if (!yi) return NULL;
3335 3297 : gel(y,i) = yi;
3336 : }
3337 1225 : return y;
3338 : }
3339 651 : return gcopy(x);
3340 : }
3341 :
3342 : GEN
3343 1036 : ffmap(GEN m, GEN x)
3344 : {
3345 1036 : pari_sp ltop = avma;
3346 : GEN y;
3347 1036 : checkmap(m, "ffmap");
3348 1036 : y = ffmap_i(m, x);
3349 1036 : if (y) return y;
3350 42 : set_avma(ltop); return cgetg(1,t_VEC);
3351 : }
3352 :
3353 : static GEN
3354 252 : ffeltmaprel_i(GEN m, GEN x)
3355 : {
3356 252 : GEN g = gel(m,1), r = gel(m,2);
3357 252 : if (!FF_samefield(x, g))
3358 0 : pari_err_DOMAIN("ffmap","m","domain does not contain", x, r);
3359 252 : if (typ(r)==t_FFELT)
3360 84 : retmkpolmod(FF_map(r, x), pol_x(FF_var(g)));
3361 : else
3362 168 : retmkpolmod(FFX_preimagerel(x, r, ffpartmapimage("ffmap", r)), gcopy(r));
3363 : }
3364 :
3365 : static GEN
3366 252 : ffmaprel_i(GEN m, GEN x)
3367 : {
3368 252 : switch(typ(x))
3369 : {
3370 252 : case t_FFELT:
3371 252 : return ffeltmaprel_i(m, x);
3372 0 : case t_POL: pari_APPLY_pol_normalized(ffmaprel_i(m, gel(x,i)));
3373 0 : case t_SER: pari_APPLY_ser_normalized(ffmaprel_i(m, gel(x,i)));
3374 0 : case t_RFRAC: case t_VEC: case t_COL: case t_MAT:
3375 0 : pari_APPLY_same(ffmaprel_i(m, gel(x,i)));
3376 : }
3377 0 : return gcopy(x);
3378 : }
3379 : GEN
3380 252 : ffmaprel(GEN m, GEN x) { checkmap(m, "ffmaprel"); return ffmaprel_i(m, x); }
3381 :
3382 : static void
3383 84 : err_compo(GEN m, GEN n)
3384 84 : { pari_err_DOMAIN("ffcompomap","m","domain does not contain codomain of",n,m); }
3385 :
3386 : GEN
3387 420 : ffcompomap(GEN m, GEN n)
3388 : {
3389 420 : pari_sp av = avma;
3390 420 : GEN g = gel(n,1), r, m2, n2;
3391 420 : checkmap(m, "ffcompomap");
3392 420 : checkmap(n, "ffcompomap");
3393 420 : m2 = gel(m,2); n2 = gel(n,2);
3394 420 : switch((typ(m2)==t_POL)|((typ(n2)==t_POL)<<1))
3395 : {
3396 84 : case 0:
3397 84 : if (!FF_samefield(gel(m,1),n2)) err_compo(m,n);
3398 42 : r = FF_map(gel(m,2), n2);
3399 42 : break;
3400 84 : case 2:
3401 84 : r = ffmap_i(m, n2);
3402 42 : if (lg(r) == 1) err_compo(m,n);
3403 42 : break;
3404 168 : case 1:
3405 168 : r = ffeltmap_i(m, n2);
3406 126 : if (!r)
3407 : {
3408 : GEN a, A, R, M;
3409 : long dm, dn;
3410 42 : a = ffpartmapimage("ffcompomap",m2);
3411 42 : A = FF_to_FpXQ_i(FF_neg(n2));
3412 42 : setvarn(A, 1);
3413 42 : R = deg1pol(gen_1, A, 0);
3414 42 : setvarn(R, 0);
3415 42 : M = gcopy(m2);
3416 42 : setvarn(M, 1);
3417 42 : r = polresultant0(R, M, 1, 0);
3418 42 : dm = FF_f(gel(m,1)); dn = FF_f(gel(n,1));
3419 42 : if (dm % dn || !FFX_ispower(r, dm/dn, a, &r)) err_compo(m,n);
3420 42 : setvarn(r, varn(FF_mod(g)));
3421 : }
3422 126 : break;
3423 84 : case 3:
3424 : {
3425 : GEN M, R, T, p, a;
3426 84 : a = ffpartmapimage("ffcompomap",n2);
3427 84 : if (!FF_samefield(a, gel(m,1))) err_compo(m,n);
3428 42 : p = FF_p_i(gel(n,1));
3429 42 : T = FF_mod(gel(n,1));
3430 42 : setvarn(T, 1);
3431 42 : R = RgX_to_FpXQX(n2,T,p);
3432 42 : setvarn(R, 0);
3433 42 : M = gcopy(m2);
3434 42 : setvarn(M, 1);
3435 42 : r = polresultant0(R, M, 1, 0);
3436 42 : setvarn(r, varn(n2));
3437 : }
3438 : }
3439 252 : return gerepilecopy(av, mkvec2(g,r));
3440 : }
|