Line data Source code
1 : /* Copyright (C) 2000 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 : Check the License for details. You should have received a copy of it, along
11 : with the package; see the file 'COPYING'. If not, write to the Free Software
12 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
13 :
14 : /*******************************************************************/
15 : /* */
16 : /* MAXIMAL ORDERS */
17 : /* */
18 : /*******************************************************************/
19 : #include "pari.h"
20 : #include "paripriv.h"
21 :
22 : #define DEBUGLEVEL DEBUGLEVEL_nf
23 :
24 : /* allow p = -1 from factorizations, avoid oo loop on p = 1 */
25 : static long
26 13930 : safe_Z_pvalrem(GEN x, GEN p, GEN *z)
27 : {
28 13930 : if (is_pm1(p))
29 : {
30 28 : if (signe(p) > 0) return gvaluation(x,p); /*error*/
31 21 : *z = absi(x); return 1;
32 : }
33 13902 : return Z_pvalrem(x, p, z);
34 : }
35 : /* D an integer, P a ZV, return a factorization matrix for D over P, removing
36 : * entries with 0 exponent. */
37 : static GEN
38 4018 : fact_from_factors(GEN D, GEN P, long flag)
39 : {
40 4018 : long i, l = lg(P), iq = 1;
41 4018 : GEN Q = cgetg(l+1,t_COL);
42 4018 : GEN E = cgetg(l+1,t_COL);
43 17941 : for (i=1; i<l; i++)
44 : {
45 13930 : GEN p = gel(P,i);
46 : long k;
47 13930 : if (flag && !equalim1(p))
48 : {
49 14 : p = gcdii(p, D);
50 14 : if (is_pm1(p)) continue;
51 : }
52 13930 : k = safe_Z_pvalrem(D, p, &D);
53 13923 : if (k) { gel(Q,iq) = p; gel(E,iq) = utoipos(k); iq++; }
54 : }
55 4011 : D = absi_shallow(D);
56 4011 : if (!equali1(D))
57 : {
58 812 : long k = Z_isanypower(D, &D);
59 812 : if (!k) k = 1;
60 812 : gel(Q,iq) = D; gel(E,iq) = utoipos(k); iq++;
61 : }
62 4011 : setlg(Q,iq);
63 4011 : setlg(E,iq); return mkmat2(Q,E);
64 : }
65 :
66 : /* d a t_INT; f a t_MAT factorisation of some t_INT sharing some divisors
67 : * with d, or a prime (t_INT). Return a factorization F of d: "primes"
68 : * entries in f _may_ be composite, and are included as is in d. */
69 : static GEN
70 2341 : update_fact(GEN d, GEN f)
71 : {
72 : GEN P;
73 2341 : switch (typ(f))
74 : {
75 2327 : case t_INT: case t_VEC: case t_COL: return f;
76 14 : case t_MAT:
77 14 : if (lg(f) == 3) { P = gel(f,1); break; }
78 : /*fall through*/
79 : default:
80 7 : pari_err_TYPE("nfbasis [factorization expected]",f);
81 : return NULL;/*LCOV_EXCL_LINE*/
82 : }
83 7 : return fact_from_factors(d, P, 1);
84 : }
85 :
86 : /* T = C T0(X/L); C = L^d / lt(T0), d = deg(T)
87 : * disc T = C^2(d - 1) L^-(d(d-1)) disc T0 = (L^d / lt(T0)^2)^(d-1) disc T0 */
88 : static GEN
89 812901 : set_disc(nfmaxord_t *S)
90 : {
91 : GEN L, dT;
92 : long d;
93 812901 : if (S->T0 == S->T) return ZX_disc(S->T);
94 249138 : d = degpol(S->T0);
95 249148 : L = S->unscale;
96 249148 : if (typ(L) == t_FRAC && abscmpii(gel(L,1), gel(L,2)) < 0)
97 11771 : dT = ZX_disc(S->T); /* more efficient */
98 : else
99 : {
100 237377 : GEN l0 = leading_coeff(S->T0);
101 237374 : GEN a = gpowgs(gdiv(gpowgs(L, d), sqri(l0)), d-1);
102 237366 : dT = gmul(a, ZX_disc(S->T0)); /* more efficient */
103 : }
104 249127 : return S->dT = dT;
105 : }
106 :
107 : /* dT != 0 */
108 : static GEN
109 788717 : poldiscfactors_i(GEN T, GEN dT, long flag)
110 : {
111 : GEN U, fa, Z, E, P, Tp;
112 : long i, l;
113 :
114 788717 : fa = absZ_factor_limit_strict(dT, minuu(tridiv_bound(dT), maxprime()), &U);
115 788755 : if (!U) return fa;
116 777 : Z = mkcol(gel(U,1)); P = gel(fa,1); Tp = NULL;
117 1680 : while (lg(Z) != 1)
118 : { /* pop and handle last element of Z */
119 903 : GEN p = veclast(Z), r;
120 903 : setlg(Z, lg(Z)-1);
121 903 : if (!Tp) /* first time: p is composite and not a power */
122 777 : Tp = ZX_deriv(T);
123 : else
124 : {
125 126 : (void)Z_isanypower(p, &p);
126 126 : if ((flag || lgefint(p)==3) && BPSW_psp(p))
127 96 : { P = vec_append(P, p); continue; }
128 : }
129 807 : r = FpX_gcd_check(T, Tp, p);
130 807 : if (r)
131 63 : Z = shallowconcat(Z, Z_cba(r, diviiexact(p,r)));
132 744 : else if (flag)
133 7 : P = shallowconcat(P, gel(Z_factor(p),1));
134 : else
135 737 : P = vec_append(P, p);
136 : }
137 777 : ZV_sort_inplace(P); l = lg(P); E = cgetg(l, t_COL);
138 7086 : for (i = 1; i < l; i++) gel(E,i) = utoipos(Z_pvalrem(dT, gel(P,i), &dT));
139 777 : return mkmat2(P,E);
140 : }
141 :
142 : GEN
143 42 : poldiscfactors(GEN T, long flag)
144 : {
145 42 : pari_sp av = avma;
146 : GEN dT;
147 42 : if (typ(T) != t_POL || !RgX_is_ZX(T)) pari_err_TYPE("poldiscfactors",T);
148 42 : if (flag < 0 || flag > 1) pari_err_FLAG("poldiscfactors");
149 42 : dT = ZX_disc(T);
150 42 : if (!signe(dT)) retmkvec2(gen_0, Z_factor(gen_0));
151 35 : return gc_GEN(av, mkvec2(dT, poldiscfactors_i(T, dT, flag)));
152 : }
153 :
154 : static void
155 812968 : nfmaxord_check_args(nfmaxord_t *S, GEN T, long flag)
156 : {
157 812968 : GEN dT, L, E, P, fa = NULL;
158 : pari_timer t;
159 812968 : long l, ty = typ(T);
160 :
161 812968 : if (DEBUGLEVEL) timer_start(&t);
162 812968 : if (ty == t_VEC) {
163 24199 : if (lg(T) != 3) pari_err_TYPE("nfmaxord",T);
164 24199 : fa = gel(T,2); T = gel(T,1); ty = typ(T);
165 : }
166 812968 : if (ty != t_POL) pari_err_TYPE("nfmaxord",T);
167 812968 : T = Q_primpart(T);
168 812873 : if (degpol(T) <= 0) pari_err_CONSTPOL("nfmaxord");
169 812870 : RgX_check_ZX(T, "nfmaxord");
170 812874 : S->T0 = T;
171 812874 : S->T = T = ZX_Q_normalize(T, &L);
172 812901 : S->unscale = L;
173 812901 : S->dT = dT = set_disc(S);
174 812880 : S->certify = 1;
175 812880 : if (!signe(dT)) pari_err_IRREDPOL("nfmaxord",T);
176 812880 : if (fa)
177 : {
178 24199 : const long MIN = 100; /* include at least all p < 101 */
179 24199 : GEN P0 = NULL, U;
180 24199 : S->certify = 0;
181 24199 : if (!isint1(L)) fa = update_fact(dT, fa);
182 24192 : switch(typ(fa))
183 : {
184 224 : case t_MAT:
185 224 : if (!is_Z_factornon0(fa)) pari_err_TYPE("nfmaxord",fa);
186 217 : P0 = gel(fa,1); /* fall through */
187 4011 : case t_VEC: case t_COL:
188 4011 : if (!P0)
189 : {
190 3794 : if (!RgV_is_ZV(fa)) pari_err_TYPE("nfmaxord",fa);
191 3794 : P0 = fa;
192 : }
193 4011 : P = gel(absZ_factor_limit_strict(dT, MIN, &U), 1);
194 4011 : if (lg(P) != 0) { settyp(P, typ(P0)); P0 = shallowconcat(P0,P); }
195 4011 : P0 = ZV_sort_uniq_shallow(P0);
196 4011 : fa = fact_from_factors(dT, P0, 0);
197 4004 : break;
198 20167 : case t_INT:
199 20167 : fa = absZ_factor_limit(dT, (signe(fa) <= 0)? 1: maxuu(itou(fa), MIN));
200 20167 : break;
201 7 : default:
202 7 : pari_err_TYPE("nfmaxord",fa);
203 : }
204 : }
205 : else
206 : {
207 788681 : S->certify = !(flag & nf_PARTIALFACT);
208 788681 : fa = poldiscfactors_i(T, dT, 0);
209 : }
210 812888 : P = gel(fa,1); l = lg(P);
211 812888 : E = gel(fa,2);
212 812888 : if (l > 1 && is_pm1(gel(P,1)))
213 : {
214 21 : l--;
215 21 : P = vecslice(P, 2, l);
216 21 : E = vecslice(E, 2, l);
217 : }
218 812886 : S->dTP = P;
219 812886 : S->dTE = vec_to_vecsmall(E);
220 812855 : if (DEBUGLEVEL>2) timer_printf(&t, "disc. factorisation");
221 812855 : }
222 :
223 : static int
224 221432 : fnz(GEN x,long j)
225 : {
226 : long i;
227 709899 : for (i=1; i<j; i++)
228 539038 : if (signe(gel(x,i))) return 0;
229 170861 : return 1;
230 : }
231 : /* return list u[i], 2 by 2 coprime with the same prime divisors as ab */
232 : static GEN
233 294 : get_coprimes(GEN a, GEN b)
234 : {
235 294 : long i, k = 1;
236 294 : GEN u = cgetg(3, t_COL);
237 294 : gel(u,1) = a;
238 294 : gel(u,2) = b;
239 : /* u1,..., uk 2 by 2 coprime */
240 1071 : while (k+1 < lg(u))
241 : {
242 777 : GEN d, c = gel(u,k+1);
243 777 : if (is_pm1(c)) { k++; continue; }
244 1309 : for (i=1; i<=k; i++)
245 : {
246 840 : GEN ui = gel(u,i);
247 840 : if (is_pm1(ui)) continue;
248 483 : d = gcdii(c, ui);
249 483 : if (d == gen_1) continue;
250 483 : c = diviiexact(c, d);
251 483 : gel(u,i) = diviiexact(ui, d);
252 483 : u = vec_append(u, d);
253 : }
254 469 : gel(u,++k) = c;
255 : }
256 1365 : for (i = k = 1; i < lg(u); i++)
257 1071 : if (!is_pm1(gel(u,i))) gel(u,k++) = gel(u,i);
258 294 : setlg(u, k); return u;
259 : }
260 :
261 : /*******************************************************************/
262 : /* */
263 : /* ROUND 4 */
264 : /* */
265 : /*******************************************************************/
266 : typedef struct {
267 : /* constants */
268 : long pisprime; /* -1: unknown, 1: prime, 0: composite */
269 : GEN p, f; /* goal: factor f p-adically */
270 : long df;
271 : GEN pdf; /* p^df = reduced discriminant of f */
272 : long mf; /* */
273 : GEN psf, pmf; /* stability precision for f, wanted precision for f */
274 : long vpsf; /* v_p(p_f) */
275 : /* these are updated along the way */
276 : GEN phi; /* a p-integer, in Q[X] */
277 : GEN phi0; /* a p-integer, in Q[X] from testb2 / testc2, to be composed with
278 : * phi when correct precision is known */
279 : GEN chi; /* characteristic polynomial of phi (mod psc) in Z[X] */
280 : GEN nu; /* irreducible divisor of chi mod p, in Z[X] */
281 : GEN invnu; /* numerator ( 1/ Mod(nu, chi) mod pmr ) */
282 : GEN Dinvnu;/* denominator ( ... ) */
283 : long vDinvnu; /* v_p(Dinvnu) */
284 : GEN prc, psc; /* reduced discriminant of chi, stability precision for chi */
285 : long vpsc; /* v_p(p_c) */
286 : GEN ns, nsf, precns; /* cached Newton sums for nsf and their precision */
287 : } decomp_t;
288 : static GEN maxord_i(decomp_t *S, GEN p, GEN f, long mf, GEN w, long flag);
289 : static GEN dbasis(GEN p, GEN f, long mf, GEN alpha, GEN U);
290 : static GEN maxord(GEN p,GEN f,long mf);
291 : static GEN ZX_Dedekind(GEN F, GEN *pg, GEN p);
292 :
293 : static void
294 498 : fix_PE(GEN *pP, GEN *pE, long i, GEN u, GEN N)
295 : {
296 : GEN P, E;
297 498 : long k, l = lg(u), lP = lg(*pP);
298 : pari_sp av;
299 :
300 498 : *pP = P = shallowconcat(*pP, vecslice(u, 2, l-1));
301 498 : *pE = E = vecsmall_lengthen(*pE, lP + l-2);
302 498 : gel(P,i) = gel(u,1); av = avma;
303 498 : E[i] = Z_pvalrem(N, gel(P,i), &N);
304 1003 : for (k=lP, lP=lg(P); k < lP; k++) E[k] = Z_pvalrem(N, gel(P,k), &N);
305 498 : set_avma(av);
306 498 : }
307 : static long
308 685889 : diag_denomval(GEN M, GEN p)
309 : {
310 : long j, v, l;
311 685889 : if (typ(M) != t_MAT) return 0;
312 463396 : v = 0; l = lg(M);
313 2072696 : for (j=1; j<l; j++)
314 : {
315 1609302 : GEN t = gcoeff(M,j,j);
316 1609302 : if (typ(t) == t_FRAC) v += Z_pval(gel(t,2), p);
317 : }
318 463394 : return v;
319 : }
320 :
321 : /* n > 1 is composite, not a pure power, and has no prime divisor < 2^14;
322 : * return a BPSW divisor of n and smallest k-th root of largest coprime cofactor */
323 : static GEN
324 183 : Z_fac(GEN n)
325 : {
326 183 : GEN p = icopy(n), part = ifac_start(p, 0);
327 : long e;
328 183 : ifac_next(&part, &p, &e); n = diviiexact(n, powiu(p, e));
329 183 : (void)Z_isanypower(n, &n); return mkvec2(p, n);
330 : }
331 :
332 : /* Warning: data computed for T = ZX_Q_normalize(T0). If S.unscale !=
333 : * gen_1, caller must take steps to correct the components if it wishes
334 : * to stick to the original T0. Return a vector of p-maximal orders, for
335 : * those p s.t p^2 | disc(T) [ = S->dTP ]*/
336 : static GEN
337 812959 : get_maxord(nfmaxord_t *S, GEN T0, long flag)
338 : {
339 : GEN P, E;
340 : VOLATILE GEN O;
341 : VOLATILE long lP, i, k;
342 :
343 812959 : nfmaxord_check_args(S, T0, flag);
344 812856 : P = S->dTP; lP = lg(P);
345 812856 : E = S->dTE;
346 812856 : O = cgetg(1, t_VEC);
347 3144104 : for (i=1; i<lP; i++)
348 : {
349 : VOLATILE pari_sp av;
350 : /* includes the silly case where P[i] = -1 */
351 2331208 : if (E[i] <= 1)
352 : {
353 1303357 : if (S->certify)
354 : {
355 1295286 : GEN p = gel(P,i);
356 1295286 : if (signe(p) > 0 && !BPSW_psp(p))
357 : {
358 183 : fix_PE(&P, &E, i, Z_fac(p), S->dT);
359 183 : lP = lg(P); i--; continue;
360 : }
361 : }
362 1303183 : O = vec_append(O, gen_1); continue;
363 : }
364 1027851 : av = avma;
365 1027851 : pari_CATCH(CATCH_ALL) {
366 294 : GEN u, err = pari_err_last();
367 : long l;
368 294 : switch(err_get_num(err))
369 : {
370 294 : case e_INV:
371 : {
372 294 : GEN p, x = err_get_compo(err, 2);
373 294 : if (typ(x) == t_INTMOD)
374 : { /* caught false prime, update factorization */
375 294 : p = gcdii(gel(x,1), gel(x,2));
376 294 : u = diviiexact(gel(x,1),p);
377 294 : if (DEBUGLEVEL) pari_warn(warner,"impossible inverse: %Ps", x);
378 294 : (void)gc_all(av, 2, &p, &u);
379 :
380 294 : u = get_coprimes(p, u); l = lg(u);
381 : /* no small factors, but often a prime power */
382 882 : for (k = 1; k < l; k++) (void)Z_isanypower(gel(u,k), &gel(u,k));
383 294 : break;
384 : }
385 : /* fall through */
386 : }
387 : case e_PRIME: case e_IRREDPOL:
388 : { /* we're here because we failed BPSW_isprime(), no point in
389 : * reporting a possible counter-example to the BPSW test */
390 0 : GEN p = gel(P,i);
391 0 : set_avma(av);
392 0 : if (DEBUGLEVEL)
393 0 : pari_warn(warner,"large composite in nfmaxord:loop(), %Ps", p);
394 0 : if (expi(p) < 100)
395 0 : u = gel(Z_factor(p), 1); /* p < 2^100 should take ~20ms */
396 0 : else if (S->certify)
397 0 : u = Z_fac(p);
398 : else
399 0 : { /* give up, probably not maximal */
400 0 : GEN B, g, k = ZX_Dedekind(S->T, &g, p);
401 0 : k = FpX_normalize(k, p);
402 0 : B = dbasis(p, S->T, E[i], NULL, FpX_div(S->T,k,p));
403 0 : O = vec_append(O, B);
404 0 : pari_CATCH_reset(); continue;
405 : }
406 0 : break;
407 : }
408 0 : default: pari_err(0, err);
409 : return NULL;/*LCOV_EXCL_LINE*/
410 : }
411 294 : fix_PE(&P, &E, i, u, S->dT);
412 294 : lP = lg(P); av = avma;
413 1028147 : } pari_RETRY {
414 1028147 : GEN p = gel(P,i), O2;
415 1028147 : if (DEBUGLEVEL>2) err_printf("Treating p^k = %Ps^%ld\n",p,E[i]);
416 1028147 : O2 = maxord(p,S->T,E[i]);
417 1027885 : if (S->certify && (odd(E[i]) || E[i] != 2*diag_denomval(O2, p))
418 610079 : && !BPSW_psp(p))
419 : {
420 21 : fix_PE(&P, &E, i, gel(Z_factor(p), 1), S->dT);
421 21 : lP = lg(P); i--;
422 : }
423 : else
424 1027866 : O = vec_append(O, O2);
425 1027878 : } pari_ENDCATCH;
426 : }
427 812896 : S->dTP = P; S->dTE = E; return O;
428 : }
429 :
430 : /* M a QM, return denominator of diagonal. All denominators are powers of
431 : * a given integer */
432 : static GEN
433 99459 : diag_denom(GEN M)
434 : {
435 99459 : GEN d = gen_1;
436 99459 : long j, l = lg(M);
437 694497 : for (j=1; j<l; j++)
438 : {
439 595038 : GEN t = gcoeff(M,j,j);
440 595038 : if (typ(t) == t_INT) continue;
441 211985 : t = gel(t,2);
442 211985 : if (abscmpii(t,d) > 0) d = t;
443 : }
444 99459 : return d;
445 : }
446 : static void
447 746459 : setPE(GEN D, GEN P, GEN *pP, GEN *pE)
448 : {
449 746459 : long k, j, l = lg(P);
450 : GEN P2, E2;
451 746459 : *pP = P2 = cgetg(l, t_VEC);
452 746475 : *pE = E2 = cgetg(l, t_VECSMALL);
453 2870104 : for (k = j = 1; j < l; j++)
454 : {
455 2123601 : long v = Z_pvalrem(D, gel(P,j), &D);
456 2123626 : if (v) { gel(P2,k) = gel(P,j); E2[k] = v; k++; }
457 : }
458 746503 : setlg(P2, k);
459 746497 : setlg(E2, k);
460 746494 : }
461 : void
462 101744 : nfmaxord(nfmaxord_t *S, GEN T0, long flag)
463 : {
464 101744 : GEN O = get_maxord(S, T0, flag);
465 101747 : GEN f = S->T, P = S->dTP, a = NULL, da = NULL;
466 101747 : long n = degpol(f), lP = lg(P), i, j, k;
467 101749 : int centered = 0;
468 101749 : pari_sp av = avma;
469 : /* r1 & basden not initialized here */
470 101749 : S->r1 = -1;
471 101749 : S->basden = NULL;
472 356337 : for (i=1; i<lP; i++)
473 : {
474 254590 : GEN M, db, b = gel(O,i);
475 254590 : if (b == gen_1) continue;
476 99459 : db = diag_denom(b);
477 99459 : if (db == gen_1) continue;
478 :
479 : /* db = denom(b), (da,db) = 1. Compute da Im(b) + db Im(a) */
480 99459 : b = Q_muli_to_int(b,db);
481 99458 : if (!da) { da = db; a = b; }
482 : else
483 : { /* optimization: easy as long as both matrix are diagonal */
484 134501 : j=2; while (j<=n && fnz(gel(a,j),j) && fnz(gel(b,j),j)) j++;
485 50585 : k = j-1; M = cgetg(2*n-k+1,t_MAT);
486 185086 : for (j=1; j<=k; j++)
487 : {
488 134501 : gel(M,j) = gel(a,j);
489 134501 : gcoeff(M,j,j) = mulii(gcoeff(a,j,j),gcoeff(b,j,j));
490 : }
491 : /* could reduce mod M(j,j) but not worth it: usually close to da*db */
492 279372 : for ( ; j<=n; j++) gel(M,j) = ZC_Z_mul(gel(a,j), db);
493 279372 : for ( ; j<=2*n-k; j++) gel(M,j) = ZC_Z_mul(gel(b,j+k-n), da);
494 50583 : da = mulii(da,db);
495 50585 : a = ZM_hnfmodall_i(M, da, hnf_MODID|hnf_CENTER);
496 50585 : (void)gc_all(av, 2, &a, &da);
497 50584 : centered = 1;
498 : }
499 : }
500 101747 : if (da)
501 : {
502 48873 : GEN index = diviiexact(da, gcoeff(a,1,1));
503 231731 : for (j=2; j<=n; j++) index = mulii(index, diviiexact(da, gcoeff(a,j,j)));
504 48873 : if (!centered) a = ZM_hnfcenter(a);
505 48871 : a = RgM_Rg_div(a, da);
506 48874 : S->index = index;
507 48874 : S->dK = diviiexact(S->dT, sqri(index));
508 : }
509 : else
510 : {
511 52874 : S->index = gen_1;
512 52874 : S->dK = S->dT;
513 52874 : a = matid(n);
514 : }
515 101744 : setPE(S->dK, P, &S->dKP, &S->dKE);
516 101745 : S->basis = RgM_to_RgXV(a, varn(f));
517 101746 : }
518 : GEN
519 938 : nfbasis(GEN x, GEN *pdK)
520 : {
521 938 : pari_sp av = avma;
522 : nfmaxord_t S;
523 : GEN B;
524 938 : nfmaxord(&S, x, 0);
525 938 : B = RgXV_unscale(S.basis, S.unscale);
526 938 : if (pdK) *pdK = S.dK;
527 938 : return gc_all(av, pdK? 2: 1, &B, pdK);
528 : }
529 : /* field discriminant: faster than nfmaxord, use local data only */
530 : static GEN
531 711218 : maxord_disc(nfmaxord_t *S, GEN x)
532 : {
533 711218 : GEN O = get_maxord(S, x, 0), I = gen_1;
534 711188 : long n = degpol(S->T), lP = lg(O), i, j;
535 2787640 : for (i = 1; i < lP; i++)
536 : {
537 2076470 : GEN b = gel(O,i);
538 2076470 : if (b == gen_1) continue;
539 2707727 : for (j = 1; j <= n; j++)
540 : {
541 2116183 : GEN c = gcoeff(b,j,j);
542 2116183 : if (typ(c) == t_FRAC) I = mulii(I, gel(c,2)) ;
543 : }
544 : }
545 711170 : return diviiexact(S->dT, sqri(I));
546 : }
547 : GEN
548 66462 : nfdisc(GEN x)
549 : {
550 66462 : pari_sp av = avma;
551 : nfmaxord_t S;
552 66462 : return gc_INT(av, maxord_disc(&S, x));
553 : }
554 : GEN
555 644763 : nfdiscfactors(GEN x)
556 : {
557 644763 : pari_sp av = avma;
558 644763 : GEN E, P, D, nf = checknf_i(x);
559 644751 : if (nf)
560 : {
561 7 : D = nf_get_disc(nf);
562 7 : P = nf_get_ramified_primes(nf);
563 : }
564 : else
565 : {
566 : nfmaxord_t S;
567 644744 : D = maxord_disc(&S, x);
568 644701 : P = S.dTP;
569 : }
570 644708 : setPE(D, P, &P, &E); settyp(P, t_COL);
571 644748 : return gc_GEN(av, mkvec2(D, mkmat2(P, zc_to_ZC(E))));
572 : }
573 :
574 : static ulong
575 1599628 : Flx_checkdeflate(GEN x)
576 : {
577 1599628 : ulong d = 0, i, lx = (ulong)lg(x);
578 2557984 : for (i=3; i<lx; i++)
579 1710492 : if (x[i]) { d = ugcd(d,i-2); if (d == 1) break; }
580 1599628 : return d;
581 : }
582 :
583 : /* product of (monic) irreducible factors of f over Fp[X]
584 : * Assume f reduced mod p, otherwise valuation at x may be wrong */
585 : static GEN
586 1599605 : Flx_radical(GEN f, ulong p)
587 : {
588 1599605 : long v0 = Flx_valrem(f, &f);
589 : ulong du, d, e;
590 : GEN u;
591 :
592 1599626 : d = Flx_checkdeflate(f);
593 1599660 : if (!d) return v0? polx_Flx(f[1]): pol1_Flx(f[1]);
594 1002759 : if (u_lvalrem(d,p, &e)) f = Flx_deflate(f, d/e); /* f(x^p^i) -> f(x) */
595 1002763 : u = Flx_gcd(f, Flx_deriv(f, p), p); /* (f,f') */
596 1002750 : du = degpol(u);
597 1002752 : if (du)
598 : {
599 315972 : if (du == (ulong)degpol(f))
600 0 : f = Flx_radical(Flx_deflate(f,p), p);
601 : else
602 : {
603 315971 : u = Flx_normalize(u, p);
604 315975 : f = Flx_div(f, u, p);
605 315970 : if (p <= du)
606 : {
607 66692 : GEN w = (degpol(f) >= degpol(u))? Flx_rem(f, u, p): f;
608 66692 : w = Flxq_powu(w, du, u, p);
609 66693 : w = Flx_div(u, Flx_gcd(w,u,p), p); /* u / gcd(u, v^(deg u-1)) */
610 66693 : f = Flx_mul(f, Flx_radical(Flx_deflate(w,p), p), p);
611 : }
612 : }
613 : }
614 1002754 : if (v0) f = Flx_shift(f, 1);
615 1002752 : return f;
616 : }
617 : /* Assume f reduced mod p, otherwise valuation at x may be wrong */
618 : static GEN
619 5692 : FpX_radical(GEN f, GEN p)
620 : {
621 : GEN u;
622 : long v0;
623 5692 : if (lgefint(p) == 3)
624 : {
625 1754 : ulong q = p[2];
626 1754 : return Flx_to_ZX( Flx_radical(ZX_to_Flx(f, q), q) );
627 : }
628 3938 : v0 = ZX_valrem(f, &f);
629 3938 : u = FpX_gcd(f,FpX_deriv(f, p), p);
630 3650 : if (degpol(u)) f = FpX_div(f, u, p);
631 3650 : if (v0) f = RgX_shift(f, 1);
632 3650 : return f;
633 : }
634 : /* f / a */
635 : static GEN
636 1531212 : zx_z_div(GEN f, ulong a)
637 : {
638 1531212 : long i, l = lg(f);
639 1531212 : GEN g = cgetg(l, t_VECSMALL);
640 1531194 : g[1] = f[1];
641 5186251 : for (i = 2; i < l; i++) g[i] = f[i] / a;
642 1531194 : return g;
643 : }
644 : /* Dedekind criterion; return k = gcd(g,h, (f-gh)/p), where
645 : * f = \prod f_i^e_i, g = \prod f_i, h = \prod f_i^{e_i-1}
646 : * k = 1 iff Z[X]/(f) is p-maximal */
647 : static GEN
648 1536932 : ZX_Dedekind(GEN F, GEN *pg, GEN p)
649 : {
650 : GEN k, h, g, f, f2;
651 1536932 : ulong q = p[2];
652 1536932 : if (lgefint(p) == 3 && q < (1UL << BITS_IN_HALFULONG))
653 1531147 : {
654 1531242 : ulong q2 = q*q;
655 1531242 : f2 = ZX_to_Flx(F, q2);
656 1531220 : f = Flx_red(f2, q);
657 1531160 : g = Flx_radical(f, q);
658 1531205 : h = Flx_div(f, g, q);
659 1531185 : k = zx_z_div(Flx_sub(f2, Flx_mul(g,h,q2), q2), q);
660 1531201 : k = Flx_gcd(k, Flx_gcd(g,h,q), q);
661 1531187 : k = Flx_to_ZX(k);
662 1531140 : g = Flx_to_ZX(g);
663 : }
664 : else
665 : {
666 5690 : f2 = FpX_red(F, sqri(p));
667 5692 : f = FpX_red(f2, p);
668 5692 : g = FpX_radical(f, p);
669 5398 : h = FpX_div(f, g, p);
670 5398 : k = ZX_Z_divexact(ZX_sub(f2, ZX_mul(g,h)), p);
671 5398 : k = FpX_gcd(FpX_red(k, p), FpX_gcd(g,h,p), p);
672 : }
673 1536543 : *pg = g; return k;
674 : }
675 :
676 : /* p-maximal order of Z[x]/f; mf = v_p(Disc(f)) or < 0 [unknown].
677 : * Return gen_1 if p-maximal */
678 : static GEN
679 1536932 : maxord(GEN p, GEN f, long mf)
680 : {
681 1536932 : const pari_sp av = avma;
682 1536932 : GEN res, g, k = ZX_Dedekind(f, &g, p);
683 1536541 : long dk = degpol(k);
684 1536536 : if (DEBUGLEVEL>2) err_printf(" ZX_Dedekind: gcd has degree %ld\n", dk);
685 1536588 : if (!dk) { set_avma(av); return gen_1; }
686 874343 : if (mf < 0) mf = ZpX_disc_val(f, p);
687 874344 : k = FpX_normalize(k, p);
688 874344 : if (2*dk >= mf-1)
689 420389 : res = dbasis(p, f, mf, NULL, FpX_div(f,k,p));
690 : else
691 : {
692 : GEN w, F1, F2;
693 : decomp_t S;
694 453955 : F1 = FpX_factor(k,p);
695 454002 : F2 = FpX_factor(FpX_div(g,k,p),p);
696 454004 : w = merge_sort_uniq(gel(F1,1),gel(F2,1),(void*)cmpii,&gen_cmp_RgX);
697 453999 : res = maxord_i(&S, p, f, mf, w, 0);
698 : }
699 874413 : return gc_GEN(av,res);
700 : }
701 : /* T monic separable ZX, p prime */
702 : GEN
703 0 : ZpX_primedec(GEN T, GEN p)
704 : {
705 0 : const pari_sp av = avma;
706 0 : GEN w, F1, F2, res, g, k = ZX_Dedekind(T, &g, p);
707 : decomp_t S;
708 0 : if (!degpol(k)) return zm_to_ZM(FpX_degfact(T, p));
709 0 : k = FpX_normalize(k, p);
710 0 : F1 = FpX_factor(k,p);
711 0 : F2 = FpX_factor(FpX_div(g,k,p),p);
712 0 : w = merge_sort_uniq(gel(F1,1),gel(F2,1),(void*)cmpii,&gen_cmp_RgX);
713 0 : res = maxord_i(&S, p, T, ZpX_disc_val(T, p), w, -1);
714 0 : if (!res)
715 : {
716 0 : long f = degpol(S.nu), e = degpol(T) / f;
717 0 : set_avma(av); retmkmat2(mkcols(f), mkcols(e));
718 : }
719 0 : return gc_GEN(av,res);
720 : }
721 :
722 : static GEN
723 4670577 : Zlx_sylvester_echelon(GEN f1, GEN f2, long early_abort, ulong p, ulong pm)
724 : {
725 4670577 : long j, n = degpol(f1);
726 4670560 : GEN h, a = cgetg(n+1,t_MAT);
727 4670531 : f1 = Flx_get_red(f1, pm);
728 4670545 : h = Flx_rem(f2,f1,pm);
729 16393155 : for (j=1;; j++)
730 : {
731 16393155 : gel(a,j) = Flx_to_Flv(h, n);
732 16392396 : if (j == n) break;
733 11721992 : h = Flx_rem(Flx_shift(h, 1), f1, pm);
734 : }
735 4670404 : return zlm_echelon(a, early_abort, p, pm);
736 : }
737 : /* Sylvester's matrix, mod p^m (assumes f1 monic). If early_abort
738 : * is set, return NULL if one pivot is 0 mod p^m */
739 : static GEN
740 74021 : ZpX_sylvester_echelon(GEN f1, GEN f2, long early_abort, GEN p, GEN pm)
741 : {
742 74021 : long j, n = degpol(f1);
743 74021 : GEN h, a = cgetg(n+1,t_MAT);
744 74021 : h = FpXQ_red(f2,f1,pm);
745 422136 : for (j=1;; j++)
746 : {
747 422136 : gel(a,j) = RgX_to_RgC(h, n);
748 422136 : if (j == n) break;
749 348115 : h = FpX_rem(RgX_shift_shallow(h, 1), f1, pm);
750 : }
751 74021 : return ZpM_echelon(a, early_abort, p, pm);
752 : }
753 :
754 : /* polynomial gcd mod p^m (assumes f1 monic). Return a QpX ! */
755 : static GEN
756 246176 : Zlx_gcd(GEN f1, GEN f2, ulong p, ulong pm)
757 : {
758 246176 : pari_sp av = avma;
759 246176 : GEN a = Zlx_sylvester_echelon(f1,f2,0,p,pm);
760 246177 : long c, l = lg(a), sv = f1[1];
761 754575 : for (c = 1; c < l; c++)
762 : {
763 754576 : ulong t = ucoeff(a,c,c);
764 754576 : if (t)
765 : {
766 246178 : a = Flx_to_ZX(Flv_to_Flx(gel(a,c), sv));
767 246174 : if (t == 1) return gc_GEN(av, a);
768 74781 : return gc_upto(av, RgX_Rg_div(a, utoipos(t)));
769 : }
770 : }
771 0 : set_avma(av);
772 0 : a = cgetg(2,t_POL); a[1] = sv; return a;
773 : }
774 : GEN
775 254690 : ZpX_gcd(GEN f1, GEN f2, GEN p, GEN pm)
776 : {
777 254690 : pari_sp av = avma;
778 : GEN a;
779 : long c, l, v;
780 254690 : if (lgefint(pm) == 3)
781 : {
782 246177 : ulong q = pm[2];
783 246177 : return Zlx_gcd(ZX_to_Flx(f1, q), ZX_to_Flx(f2,q), p[2], q);
784 : }
785 8513 : a = ZpX_sylvester_echelon(f1,f2,0,p,pm);
786 8513 : l = lg(a); v = varn(f1);
787 53096 : for (c = 1; c < l; c++)
788 : {
789 53096 : GEN t = gcoeff(a,c,c);
790 53096 : if (signe(t))
791 : {
792 8513 : a = RgV_to_RgX(gel(a,c), v);
793 8513 : if (equali1(t)) return gc_GEN(av, a);
794 2474 : return gc_upto(av, RgX_Rg_div(a, t));
795 : }
796 : }
797 0 : set_avma(av); return pol_0(v);
798 : }
799 :
800 : /* Return m > 0, such that p^m ~ 2^16 for initial value of m; assume p prime */
801 : static long
802 4382587 : init_m(GEN p)
803 : {
804 : ulong pp;
805 4382587 : if (lgefint(p) > 3) return 1;
806 4381450 : pp = p[2]; /* m ~ 16 / log2(pp) */
807 4381450 : if (pp < 41) switch(pp)
808 : {
809 1193046 : case 2: return 16;
810 351347 : case 3: return 10;
811 245192 : case 5: return 6;
812 153063 : case 7: return 5;
813 209211 : case 11: case 13: return 4;
814 300210 : default: return 3;
815 : }
816 1929381 : return pp < 257? 2: 1;
817 : }
818 :
819 : /* reduced resultant mod p^m (assumes x monic) */
820 : GEN
821 993030 : ZpX_reduced_resultant(GEN x, GEN y, GEN p, GEN pm)
822 : {
823 993030 : pari_sp av = avma;
824 : GEN z;
825 993030 : if (lgefint(pm) == 3)
826 : {
827 981227 : ulong q = pm[2];
828 981227 : z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q),0,p[2],q);
829 981271 : if (lg(z) > 1)
830 : {
831 981271 : ulong c = ucoeff(z,1,1);
832 981271 : if (c) return gc_utoipos(av, c);
833 : }
834 : }
835 : else
836 : {
837 11803 : z = ZpX_sylvester_echelon(x,y,0,p,pm);
838 11805 : if (lg(z) > 1)
839 : {
840 11805 : GEN c = gcoeff(z,1,1);
841 11805 : if (signe(c)) return gc_INT(av, c);
842 : }
843 : }
844 128661 : set_avma(av); return gen_0;
845 : }
846 : /* Assume Res(f,g) divides p^M. Return Res(f, g), using dynamic p-adic
847 : * precision (until result is nonzero or p^M). */
848 : GEN
849 931883 : ZpX_reduced_resultant_fast(GEN f, GEN g, GEN p, long M)
850 : {
851 931883 : GEN R, q = NULL;
852 : long m;
853 931883 : m = init_m(p); if (m < 1) m = 1;
854 61132 : for(;; m <<= 1) {
855 993007 : if (M < 2*m) break;
856 94054 : q = q? sqri(q): powiu(p, m); /* p^m */
857 94054 : R = ZpX_reduced_resultant(f,g, p, q); if (signe(R)) return R;
858 : }
859 898953 : q = powiu(p, M);
860 898979 : R = ZpX_reduced_resultant(f,g, p, q); return signe(R)? R: q;
861 : }
862 :
863 : /* v_p(Res(x,y) mod p^m), assumes (lc(x),p) = 1 */
864 : static long
865 3496898 : ZpX_resultant_val_i(GEN x, GEN y, GEN p, GEN pm)
866 : {
867 3496898 : pari_sp av = avma;
868 : GEN z;
869 : long i, l, v;
870 3496898 : if (lgefint(pm) == 3)
871 : {
872 3443195 : ulong q = pm[2], pp = p[2];
873 3443195 : z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q), 1, pp, q);
874 3443281 : if (!z) return gc_long(av,-1); /* failure */
875 3256712 : v = 0; l = lg(z);
876 13565740 : for (i = 1; i < l; i++) v += u_lval(ucoeff(z,i,i), pp);
877 : }
878 : else
879 : {
880 53703 : z = ZpX_sylvester_echelon(x, y, 1, p, pm);
881 53703 : if (!z) return gc_long(av,-1); /* failure */
882 52886 : v = 0; l = lg(z);
883 194474 : for (i = 1; i < l; i++) v += Z_pval(gcoeff(z,i,i), p);
884 : }
885 3309590 : return v;
886 : }
887 :
888 : /* assume (lc(f),p) = 1; no assumption on g */
889 : long
890 3450759 : ZpX_resultant_val(GEN f, GEN g, GEN p, long M)
891 : {
892 3450759 : pari_sp av = avma;
893 3450759 : GEN q = NULL;
894 : long v, m;
895 3450759 : m = init_m(p); if (m < 2) m = 2;
896 46068 : for(;; m <<= 1) {
897 3496823 : if (m > M) m = M;
898 3496823 : q = q? sqri(q): powiu(p, m); /* p^m */
899 3496906 : v = ZpX_resultant_val_i(f,g, p, q); if (v >= 0) return gc_long(av,v);
900 187387 : if (m == M) return gc_long(av,M);
901 : }
902 : }
903 :
904 : /* assume f separable and (lc(f),p) = 1 */
905 : long
906 184497 : ZpX_disc_val(GEN f, GEN p)
907 : {
908 184497 : pari_sp av = avma;
909 : long v;
910 184497 : if (degpol(f) == 1) return 0;
911 184497 : v = ZpX_resultant_val(f, ZX_deriv(f), p, LONG_MAX);
912 184498 : return gc_long(av,v);
913 : }
914 :
915 : /* *e a ZX, *d, *z in Z, *d = p^(*vd). Simplify e / d by cancelling a
916 : * common factor p^v; if z!=NULL, update it by cancelling the same power of p */
917 : static void
918 3569928 : update_den(GEN p, GEN *e, GEN *d, long *vd, GEN *z)
919 : {
920 : GEN newe;
921 3569928 : long ve = ZX_pvalrem(*e, p, &newe);
922 3569904 : if (ve) {
923 : GEN newd;
924 1756782 : long v = minss(*vd, ve);
925 1756770 : if (v) {
926 1756862 : if (v == *vd)
927 : { /* rare, denominator cancelled */
928 382178 : if (ve != v) newe = ZX_Z_mul(newe, powiu(p, ve - v));
929 382179 : newd = gen_1;
930 382179 : *vd = 0;
931 382179 : if (z) *z =diviiexact(*z, powiu(p, v));
932 : }
933 : else
934 : { /* v = ve < vd, generic case */
935 1374684 : GEN q = powiu(p, v);
936 1374764 : newd = diviiexact(*d, q);
937 1374556 : *vd -= v;
938 1374556 : if (z) *z = diviiexact(*z, q);
939 : }
940 1756730 : *e = newe;
941 1756730 : *d = newd;
942 : }
943 : }
944 3569760 : }
945 :
946 : /* return denominator, a power of p */
947 : static GEN
948 2749564 : QpX_denom(GEN x)
949 : {
950 2749564 : long i, l = lg(x);
951 2749564 : GEN maxd = gen_1;
952 9498782 : for (i=2; i<l; i++)
953 : {
954 6749217 : GEN d = gel(x,i);
955 6749217 : if (typ(d) == t_FRAC && cmpii(gel(d,2), maxd) > 0) maxd = gel(d,2);
956 : }
957 2749565 : return maxd;
958 : }
959 : static GEN
960 508780 : QpXV_denom(GEN x)
961 : {
962 508780 : long l = lg(x), i;
963 508780 : GEN maxd = gen_1;
964 1515549 : for (i = 1; i < l; i++)
965 : {
966 1006769 : GEN d = QpX_denom(gel(x,i));
967 1006769 : if (cmpii(d, maxd) > 0) maxd = d;
968 : }
969 508780 : return maxd;
970 : }
971 :
972 : static GEN
973 1742822 : QpX_remove_denom(GEN x, GEN p, GEN *pdx, long *pv)
974 : {
975 1742822 : *pdx = QpX_denom(x);
976 1742830 : if (*pdx == gen_1) { *pv = 0; *pdx = NULL; }
977 : else {
978 1266618 : x = Q_muli_to_int(x,*pdx);
979 1266547 : *pv = Z_pval(*pdx, p);
980 : }
981 1742770 : return x;
982 : }
983 :
984 : /* p^v * f o g mod (T,q). q = p^vq */
985 : static GEN
986 287135 : compmod(GEN p, GEN f, GEN g, GEN T, GEN q, long v)
987 : {
988 287135 : GEN D = NULL, z, df, dg, qD;
989 287135 : long vD = 0, vdf, vdg;
990 :
991 287135 : f = QpX_remove_denom(f, p, &df, &vdf);
992 287132 : if (typ(g) == t_VEC) /* [num,den,v_p(den)] */
993 0 : { vdg = itos(gel(g,3)); dg = gel(g,2); g = gel(g,1); }
994 : else
995 287132 : g = QpX_remove_denom(g, p, &dg, &vdg);
996 287131 : if (df) { D = df; vD = vdf; }
997 287131 : if (dg) {
998 56071 : long degf = degpol(f);
999 56071 : D = mul_content(D, powiu(dg, degf));
1000 56071 : vD += degf * vdg;
1001 : }
1002 287131 : qD = D ? mulii(q, D): q;
1003 287128 : if (dg) f = FpX_rescale(f, dg, qD);
1004 287129 : z = FpX_FpXQ_eval(f, g, T, qD);
1005 287136 : if (!D) {
1006 0 : if (v) {
1007 0 : if (v > 0)
1008 0 : z = ZX_Z_mul(z, powiu(p, v));
1009 : else
1010 0 : z = RgX_Rg_div(z, powiu(p, -v));
1011 : }
1012 0 : return z;
1013 : }
1014 287136 : update_den(p, &z, &D, &vD, NULL);
1015 287139 : qD = mulii(D,q);
1016 287127 : if (v) vD -= v;
1017 287127 : z = FpX_center_i(z, qD, shifti(qD,-1));
1018 287130 : if (vD > 0)
1019 287130 : z = RgX_Rg_div(z, powiu(p, vD));
1020 0 : else if (vD < 0)
1021 0 : z = ZX_Z_mul(z, powiu(p, -vD));
1022 287139 : return z;
1023 : }
1024 :
1025 : /* fast implementation of ZM_hnfmodid(M, D) / D, D = p^k */
1026 : static GEN
1027 454008 : ZpM_hnfmodid(GEN M, GEN p, GEN D)
1028 : {
1029 454008 : long i, l = lg(M);
1030 454008 : M = RgM_Rg_div(ZpM_echelon(M,0,p,D), D);
1031 2029471 : for (i = 1; i < l; i++)
1032 1575465 : if (gequal0(gcoeff(M,i,i))) gcoeff(M,i,i) = gen_1;
1033 454006 : return M;
1034 : }
1035 :
1036 : /* Return Z-basis for Z[a] + U(a)/p Z[a] in Z[t]/(f), mf = v_p(disc f), U
1037 : * a ZX. Special cases: a = t is coded as NULL, U = 0 is coded as NULL */
1038 : static GEN
1039 620014 : dbasis(GEN p, GEN f, long mf, GEN a, GEN U)
1040 : {
1041 620014 : long n = degpol(f), i, dU;
1042 : GEN b, h;
1043 :
1044 620013 : if (n == 1) return matid(1);
1045 620013 : if (a && gequalX(a)) a = NULL;
1046 620013 : if (DEBUGLEVEL>5)
1047 : {
1048 0 : err_printf(" entering Dedekind Basis with parameters p=%Ps\n",p);
1049 0 : err_printf(" f = %Ps,\n a = %Ps\n",f, a? a: pol_x(varn(f)));
1050 : }
1051 620015 : if (a)
1052 : {
1053 199615 : GEN pd = powiu(p, mf >> 1);
1054 199613 : GEN da, pdp = mulii(pd,p), D = pdp;
1055 : long vda;
1056 199614 : dU = U ? degpol(U): 0;
1057 199613 : b = cgetg(n+1, t_MAT);
1058 199613 : h = scalarpol(pd, varn(f));
1059 199616 : a = QpX_remove_denom(a, p, &da, &vda);
1060 199613 : if (da) D = mulii(D, da);
1061 199612 : gel(b,1) = scalarcol_shallow(pd, n);
1062 568694 : for (i=2; i<=n; i++)
1063 : {
1064 369077 : if (i == dU+1)
1065 0 : h = compmod(p, U, mkvec3(a,da,stoi(vda)), f, pdp, (mf>>1) - 1);
1066 : else
1067 : {
1068 369077 : h = FpXQ_mul(h, a, f, D);
1069 369079 : if (da) h = ZX_Z_divexact(h, da);
1070 : }
1071 369067 : gel(b,i) = RgX_to_RgC(h,n);
1072 : }
1073 199617 : return ZpM_hnfmodid(b, p, pd);
1074 : }
1075 : else
1076 : {
1077 420400 : if (!U) return matid(n);
1078 420400 : dU = degpol(U);
1079 420399 : if (dU == n) return matid(n);
1080 420399 : U = FpX_normalize(U, p);
1081 420405 : b = cgetg(n+1, t_MAT);
1082 1629064 : for (i = 1; i <= dU; i++) gel(b,i) = vec_ei(n, i);
1083 420409 : h = RgX_Rg_div(U, p);
1084 472541 : for ( ; i <= n; i++)
1085 : {
1086 472541 : gel(b, i) = RgX_to_RgC(h,n);
1087 472544 : if (i == n) break;
1088 52135 : h = RgX_shift_shallow(h,1);
1089 : }
1090 420409 : return b;
1091 : }
1092 : }
1093 :
1094 : static GEN
1095 508781 : get_partial_order_as_pols(GEN p, GEN f)
1096 : {
1097 508781 : GEN O = maxord(p, f, -1);
1098 508774 : long v = varn(f);
1099 508774 : return O == gen_1? pol_x_powers(degpol(f), v): RgM_to_RgXV(O, v);
1100 : }
1101 :
1102 : static long
1103 2218 : p_is_prime(decomp_t *S)
1104 : {
1105 2218 : if (S->pisprime < 0) S->pisprime = BPSW_psp(S->p);
1106 2218 : return S->pisprime;
1107 : }
1108 : static GEN ZpX_monic_factor_squarefree(GEN f, GEN p, long prec);
1109 :
1110 : /* if flag = 0, maximal order, else factorization to precision r = flag */
1111 : static GEN
1112 254688 : Decomp(decomp_t *S, long flag)
1113 : {
1114 254688 : pari_sp av = avma;
1115 : GEN fred, pr2, pr, pk, ph2, ph, b1, b2, a, e, de, f1, f2, dt, th, chip;
1116 254688 : GEN p = S->p;
1117 254688 : long vde, vdt, k, r = maxss(flag, 2*S->df + 1);
1118 :
1119 254688 : if (DEBUGLEVEL>5) err_printf(" entering Decomp: %Ps^%ld\n f = %Ps\n",
1120 : p, r, S->f);
1121 254688 : else if (DEBUGLEVEL>2) err_printf(" entering Decomp\n");
1122 254688 : chip = FpX_red(S->chi, p);
1123 254685 : if (!FpX_valrem(chip, S->nu, p, &b1))
1124 : {
1125 0 : if (!p_is_prime(S)) pari_err_PRIME("Decomp",p);
1126 0 : pari_err_BUG("Decomp (not a factor)");
1127 : }
1128 254691 : b2 = FpX_div(chip, b1, p);
1129 254682 : a = FpX_mul(FpXQ_inv(b2, b1, p), b2, p);
1130 : /* E = e / de, e in Z[X], de in Z, E = a(phi) mod (f, p) */
1131 254681 : th = QpX_remove_denom(S->phi, p, &dt, &vdt);
1132 254684 : if (dt)
1133 : {
1134 122862 : long dega = degpol(a);
1135 122861 : vde = dega * vdt;
1136 122861 : de = powiu(dt, dega);
1137 122861 : pr = mulii(p, de);
1138 122859 : a = FpX_rescale(a, dt, pr);
1139 : }
1140 : else
1141 : {
1142 131822 : vde = 0;
1143 131822 : de = gen_1;
1144 131822 : pr = p;
1145 : }
1146 254685 : e = FpX_FpXQ_eval(a, th, S->f, pr);
1147 254684 : update_den(p, &e, &de, &vde, NULL);
1148 :
1149 254689 : pk = p; k = 1;
1150 : /* E, (1 - E) tend to orthogonal idempotents in Zp[X]/(f) */
1151 1178842 : while (k < r + vde)
1152 : { /* E <-- E^2(3-2E) mod p^2k, with E = e/de */
1153 : GEN D;
1154 924155 : pk = sqri(pk); k <<= 1;
1155 924144 : e = ZX_mul(ZX_sqr(e), Z_ZX_sub(mului(3,de), gmul2n(e,1)));
1156 924188 : de= mulii(de, sqri(de));
1157 924137 : vde *= 3;
1158 924137 : D = mulii(pk, de);
1159 924139 : e = FpX_rem(e, centermod(S->f, D), D); /* e/de defined mod pk */
1160 924132 : update_den(p, &e, &de, &vde, NULL);
1161 : }
1162 : /* required precision of the factors */
1163 254687 : pr = powiu(p, r); pr2 = shifti(pr, -1);
1164 254686 : ph = mulii(de,pr);ph2 = shifti(ph, -1);
1165 254688 : e = FpX_center_i(FpX_red(e, ph), ph, ph2);
1166 254689 : fred = FpX_red(S->f, ph);
1167 :
1168 254689 : f1 = ZpX_gcd(fred, Z_ZX_sub(de, e), p, ph); /* p-adic gcd(f, 1-e) */
1169 254692 : if (!is_pm1(de))
1170 : {
1171 122864 : fred = FpX_red(fred, pr);
1172 122864 : f1 = FpX_red(f1, pr);
1173 : }
1174 254688 : f2 = FpX_div(fred,f1, pr);
1175 254689 : f1 = FpX_center_i(f1, pr, pr2);
1176 254687 : f2 = FpX_center_i(f2, pr, pr2);
1177 :
1178 254691 : if (DEBUGLEVEL>5)
1179 0 : err_printf(" leaving Decomp: f1 = %Ps\nf2 = %Ps\ne = %Ps\nde= %Ps\n", f1,f2,e,de);
1180 :
1181 254691 : if (flag < 0)
1182 : {
1183 0 : GEN m = vconcat(ZpX_primedec(f1, p), ZpX_primedec(f2, p));
1184 0 : return sort_factor(m, (void*)&cmpii, &cmp_nodata);
1185 : }
1186 254691 : else if (flag)
1187 : {
1188 301 : (void)gc_all(av, 2, &f1, &f2);
1189 301 : return shallowconcat(ZpX_monic_factor_squarefree(f1, p, flag),
1190 : ZpX_monic_factor_squarefree(f2, p, flag));
1191 : } else {
1192 : GEN D, d1, d2, B1, B2, M;
1193 : long n, n1, n2, i;
1194 254390 : (void)gc_all(av, 4, &f1, &f2, &e, &de);
1195 254391 : D = de;
1196 254391 : B1 = get_partial_order_as_pols(p,f1); n1 = lg(B1)-1;
1197 254391 : B2 = get_partial_order_as_pols(p,f2); n2 = lg(B2)-1; n = n1+n2;
1198 254390 : d1 = QpXV_denom(B1);
1199 254390 : d2 = QpXV_denom(B2); if (cmpii(d1, d2) < 0) d1 = d2;
1200 254390 : if (d1 != gen_1) {
1201 157029 : B1 = Q_muli_to_int(B1, d1);
1202 157029 : B2 = Q_muli_to_int(B2, d1);
1203 157027 : D = mulii(d1, D);
1204 : }
1205 254387 : fred = centermod_i(S->f, D, shifti(D,-1));
1206 254389 : M = cgetg(n+1, t_MAT);
1207 806661 : for (i=1; i<=n1; i++)
1208 552272 : gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B1,i),e,D), fred, D), n);
1209 254389 : e = Z_ZX_sub(de, e); B2 -= n1;
1210 708884 : for ( ; i<=n; i++)
1211 454494 : gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B2,i),e,D), fred, D), n);
1212 254390 : return ZpM_hnfmodid(M, p, D);
1213 : }
1214 : }
1215 :
1216 : /* minimum extension valuation: L/E */
1217 : static void
1218 624179 : vstar(GEN p,GEN h, long *L, long *E)
1219 : {
1220 624179 : long first, j, k, v, w, m = degpol(h);
1221 :
1222 624177 : first = 1; k = 1; v = 0;
1223 2580045 : for (j=1; j<=m; j++)
1224 : {
1225 1955866 : GEN c = gel(h, m-j+2);
1226 1955866 : if (signe(c))
1227 : {
1228 1881143 : w = Z_pval(c,p);
1229 1881145 : if (first || w*k < v*j) { v = w; k = j; }
1230 1881145 : first = 0;
1231 : }
1232 : }
1233 : /* v/k = min_j ( v_p(h_{m-j}) / j ) */
1234 624179 : w = (long)ugcd(v,k);
1235 624182 : *L = v/w;
1236 624182 : *E = k/w;
1237 624182 : }
1238 :
1239 : static GEN
1240 64356 : redelt_i(GEN a, GEN N, GEN p, GEN *pda, long *pvda)
1241 : {
1242 : GEN z;
1243 64356 : a = Q_remove_denom(a, pda);
1244 64356 : *pvda = 0;
1245 64356 : if (*pda)
1246 : {
1247 64356 : long v = Z_pvalrem(*pda, p, &z);
1248 64354 : if (v) {
1249 64354 : *pda = powiu(p, v);
1250 64354 : *pvda = v;
1251 64354 : N = mulii(*pda, N);
1252 : }
1253 : else
1254 0 : *pda = NULL;
1255 64355 : if (!is_pm1(z)) a = ZX_Z_mul(a, Fp_inv(z, N));
1256 : }
1257 64355 : return centermod(a, N);
1258 : }
1259 : /* reduce the element a modulo N [ a power of p ], taking first care of the
1260 : * denominators */
1261 : static GEN
1262 48534 : redelt(GEN a, GEN N, GEN p)
1263 : {
1264 : GEN da;
1265 : long vda;
1266 48534 : a = redelt_i(a, N, p, &da, &vda);
1267 48534 : if (da) a = RgX_Rg_div(a, da);
1268 48534 : return a;
1269 : }
1270 :
1271 : /* compute the c first Newton sums modulo pp of the
1272 : characteristic polynomial of a/d mod chi, d > 0 power of p (NULL = gen_1),
1273 : a, chi in Zp[X], vda = v_p(da)
1274 : ns = Newton sums of chi */
1275 : static GEN
1276 706111 : newtonsums(GEN p, GEN a, GEN da, long vda, GEN chi, long c, GEN pp, GEN ns)
1277 : {
1278 : GEN va, pa, dpa, s;
1279 706111 : long j, k, vdpa, lns = lg(ns);
1280 : pari_sp av;
1281 :
1282 706111 : a = centermod(a, pp); av = avma;
1283 706096 : dpa = pa = NULL; /* -Wall */
1284 706096 : vdpa = 0;
1285 706096 : va = zerovec(c);
1286 2915734 : for (j = 1; j <= c; j++)
1287 : { /* pa/dpa = (a/d)^(j-1) mod (chi, pp), dpa = p^vdpa */
1288 : long l;
1289 2216459 : pa = j == 1? a: FpXQ_mul(pa, a, chi, pp);
1290 2216572 : l = lg(pa); if (l == 2) break;
1291 2216572 : if (lns < l) l = lns;
1292 :
1293 2216572 : if (da) {
1294 2081417 : dpa = j == 1? da: mulii(dpa, da);
1295 2081275 : vdpa += vda;
1296 2081275 : update_den(p, &pa, &dpa, &vdpa, &pp);
1297 : }
1298 2216313 : s = mulii(gel(pa,2), gel(ns,2)); /* k = 2 */
1299 10957766 : for (k = 3; k < l; k++) s = addii(s, mulii(gel(pa,k), gel(ns,k)));
1300 2216166 : if (da) {
1301 : GEN r;
1302 2081030 : s = dvmdii(s, dpa, &r);
1303 2080939 : if (r != gen_0) return NULL;
1304 : }
1305 2209323 : gel(va,j) = centermodii(s, pp, shifti(pp,-1));
1306 :
1307 2209362 : if (gc_needed(av, 1))
1308 : {
1309 7 : if(DEBUGMEM>1) pari_warn(warnmem, "newtonsums");
1310 7 : (void)gc_all(av, dpa?4:3, &pa, &va, &pp, &dpa);
1311 : }
1312 : }
1313 699275 : for (; j <= c; j++) gel(va,j) = gen_0;
1314 699275 : return va;
1315 : }
1316 :
1317 : /* compute the characteristic polynomial of a/da mod chi (a in Z[X]), given
1318 : * by its Newton sums to a precision of pp using Newton sums */
1319 : static GEN
1320 699277 : newtoncharpoly(GEN pp, GEN p, GEN NS)
1321 : {
1322 699277 : long n = lg(NS)-1, j, k;
1323 699277 : GEN c = cgetg(n + 2, t_VEC), pp2 = shifti(pp,-1);
1324 :
1325 699303 : gel(c,1) = (n & 1 ? gen_m1: gen_1);
1326 2898448 : for (k = 2; k <= n+1; k++)
1327 : {
1328 2199220 : pari_sp av2 = avma;
1329 2199220 : GEN s = gen_0;
1330 : ulong z;
1331 2199220 : long v = u_pvalrem(k - 1, p, &z);
1332 9282662 : for (j = 1; j < k; j++)
1333 : {
1334 7084077 : GEN t = mulii(gel(NS,j), gel(c,k-j));
1335 7083449 : if (!odd(j)) t = negi(t);
1336 7083564 : s = addii(s, t);
1337 : }
1338 2198585 : if (v) {
1339 842274 : s = gdiv(s, powiu(p, v));
1340 842296 : if (typ(s) != t_INT) return NULL;
1341 : }
1342 2198509 : s = mulii(s, Fp_inv(utoipos(z), pp));
1343 2198864 : gel(c,k) = gc_INT(av2, Fp_center_i(s, pp, pp2));
1344 : }
1345 1862789 : for (k = odd(n)? 1: 2; k <= n+1; k += 2) gel(c,k) = negi(gel(c,k));
1346 699237 : return gtopoly(c, 0);
1347 : }
1348 :
1349 : static void
1350 706055 : manage_cache(decomp_t *S, GEN f, GEN pp)
1351 : {
1352 706055 : GEN t = S->precns;
1353 :
1354 706055 : if (!t) t = mulii(S->pmf, powiu(S->p, S->df));
1355 706053 : if (cmpii(t, pp) < 0) t = pp;
1356 :
1357 706049 : if (!S->precns || !RgX_equal(f, S->nsf) || cmpii(S->precns, t) < 0)
1358 : {
1359 521512 : if (DEBUGLEVEL>4)
1360 0 : err_printf(" Precision for cached Newton sums for %Ps: %Ps -> %Ps\n",
1361 0 : f, S->precns? S->precns: gen_0, t);
1362 521512 : S->nsf = f;
1363 521512 : S->ns = FpX_Newton(f, degpol(f), t);
1364 521535 : S->precns = t;
1365 : }
1366 706095 : }
1367 :
1368 : /* return NULL if a mod f is not an integer
1369 : * The denominator of any integer in Zp[X]/(f) divides pdr */
1370 : static GEN
1371 706102 : mycaract(decomp_t *S, GEN f, GEN a, GEN pp, GEN pdr)
1372 : {
1373 : pari_sp av;
1374 : GEN d, chi, prec1, prec2, prec3, ns;
1375 706102 : long vd, n = degpol(f);
1376 :
1377 706102 : if (gequal0(a)) return pol_0(varn(f));
1378 :
1379 706103 : a = QpX_remove_denom(a, S->p, &d, &vd);
1380 706085 : prec1 = pp;
1381 706085 : if (lgefint(S->p) == 3)
1382 706033 : prec1 = mulii(prec1, powiu(S->p, factorial_lval(n, itou(S->p))));
1383 706063 : if (d)
1384 : {
1385 641269 : GEN p1 = powiu(d, n);
1386 641297 : prec2 = mulii(prec1, p1);
1387 641269 : prec3 = mulii(prec1, gmin_shallow(mulii(p1, d), pdr));
1388 : }
1389 : else
1390 64794 : prec2 = prec3 = prec1;
1391 706062 : manage_cache(S, f, prec3);
1392 :
1393 706111 : av = avma;
1394 706111 : ns = newtonsums(S->p, a, d, vd, f, n, prec2, S->ns);
1395 706026 : if (!ns) return NULL;
1396 699274 : chi = newtoncharpoly(prec1, S->p, ns);
1397 699356 : if (!chi) return NULL;
1398 699258 : setvarn(chi, varn(f));
1399 699258 : return gc_upto(av, centermod(chi, pp));
1400 : }
1401 :
1402 : static GEN
1403 641168 : get_nu(GEN chi, GEN p, long *ptl)
1404 : { /* split off powers of x first for efficiency */
1405 641168 : long v = ZX_valrem(FpX_red(chi,p), &chi), n;
1406 : GEN P;
1407 641151 : if (!degpol(chi)) { *ptl = 1; return pol_x(varn(chi)); }
1408 475648 : P = gel(FpX_factor(chi,p), 1); n = lg(P)-1;
1409 475667 : *ptl = v? n+1: n; return gel(P,n);
1410 : }
1411 :
1412 : /* Factor characteristic polynomial chi of phi mod p. If it splits, update
1413 : * S->{phi, chi, nu} and return 1. In any case, set *nu to an irreducible
1414 : * factor mod p of chi */
1415 : static int
1416 480409 : split_char(decomp_t *S, GEN chi, GEN phi, GEN phi0, GEN *nu)
1417 : {
1418 : long l;
1419 480409 : *nu = get_nu(chi, S->p, &l);
1420 480415 : if (l == 1) return 0; /* single irreducible factor: doesn't split */
1421 : /* phi o phi0 mod (p, f) */
1422 122865 : S->phi = compmod(S->p, phi, phi0, S->f, S->p, 0);
1423 122863 : S->chi = chi;
1424 122863 : S->nu = *nu; return 1;
1425 : }
1426 :
1427 : /* Return the prime element in Zp[phi], a t_INT (iff *Ep = 1) or QX;
1428 : * nup, chip are ZX. phi = NULL codes X
1429 : * If *Ep < oE or Ep divides Ediv (!=0) return NULL (uninteresting) */
1430 : static GEN
1431 563513 : getprime(decomp_t *S, GEN phi, GEN chip, GEN nup, long *Lp, long *Ep,
1432 : long oE, long Ediv)
1433 : {
1434 : GEN z, chin, q, qp;
1435 : long r, s;
1436 :
1437 563513 : if (phi && dvdii(constant_coeff(chip), S->psc))
1438 : {
1439 1696 : chip = mycaract(S, S->chi, phi, S->pmf, S->prc);
1440 1696 : if (dvdii(constant_coeff(chip), S->pmf))
1441 1247 : chip = ZXQ_charpoly(phi, S->chi, varn(chip));
1442 : }
1443 563512 : if (degpol(nup) == 1)
1444 : {
1445 524554 : GEN c = gel(nup,2); /* nup = X + c */
1446 524554 : chin = signe(c)? RgX_translate(chip, negi(c)): chip;
1447 : }
1448 : else
1449 38951 : chin = ZXQ_charpoly(nup, chip, varn(chip));
1450 :
1451 563520 : vstar(S->p, chin, Lp, Ep);
1452 563526 : if (*Ep < oE || (Ediv && Ediv % *Ep == 0)) return NULL;
1453 :
1454 442945 : if (*Ep == 1) return S->p;
1455 305558 : (void)cbezout(*Lp, -*Ep, &r, &s); /* = 1 */
1456 305565 : if (r <= 0)
1457 : {
1458 60129 : long t = 1 + ((-r) / *Ep);
1459 60129 : r += t * *Ep;
1460 60129 : s += t * *Lp;
1461 : }
1462 : /* r > 0 minimal such that r L/E - s = 1/E
1463 : * pi = nu^r / p^s is an element of valuation 1/E,
1464 : * so is pi + O(p) since 1/E < 1. May compute nu^r mod p^(s+1) */
1465 305565 : q = powiu(S->p, s); qp = mulii(q, S->p);
1466 305545 : nup = FpXQ_powu(nup, r, S->chi, qp);
1467 305557 : if (!phi) return RgX_Rg_div(nup, q); /* phi = X : no composition */
1468 48533 : z = compmod(S->p, nup, phi, S->chi, qp, -s);
1469 48534 : return signe(z)? z: NULL;
1470 : }
1471 :
1472 : static int
1473 276502 : update_phi(decomp_t *S)
1474 : {
1475 276502 : GEN PHI = NULL, prc, psc, X = pol_x(varn(S->f));
1476 : long k, vpsc;
1477 276502 : for (k = 1;; k++)
1478 : {
1479 278795 : prc = ZpX_reduced_resultant_fast(S->chi, ZX_deriv(S->chi), S->p, S->vpsc);
1480 : /* if prc == S->psc then either chi is not separable or
1481 : the reduced discriminant of chi is too large */
1482 278794 : if (!equalii(prc, S->psc)) break;
1483 :
1484 : /* increase precision */
1485 2293 : S->vpsc = maxss(S->vpsf, S->vpsc + 1);
1486 2293 : S->psc = (S->vpsc == S->vpsf)? S->psf: mulii(S->psc, S->p);
1487 :
1488 2293 : PHI = S->phi;
1489 2293 : if (S->phi0) PHI = compmod(S->p, PHI, S->phi0, S->f, S->psc, 0);
1490 : /* change phi (in case not separable) */
1491 2293 : PHI = gadd(PHI, ZX_Z_mul(X, mului(k, S->p)));
1492 2293 : S->chi = mycaract(S, S->f, PHI, S->psc, S->pdf);
1493 : }
1494 276500 : psc = mulii(sqri(prc), S->p);
1495 276490 : vpsc = 2*Z_pval(prc, S->p) + 1;
1496 :
1497 276488 : if (!PHI) /* break out of above loop immediately (k = 1) */
1498 : {
1499 274195 : PHI = S->phi;
1500 274195 : if (S->phi0) PHI = compmod(S->p, PHI, S->phi0, S->f, psc, 0);
1501 274200 : if (S->phi0 || cmpii(psc,S->psc) > 0)
1502 : {
1503 : for(;;)
1504 : {
1505 114027 : S->chi = mycaract(S, S->f, PHI, psc, S->pdf);
1506 114030 : prc = ZpX_reduced_resultant_fast(S->chi, ZX_deriv(S->chi), S->p, vpsc);
1507 114030 : if (!equalii(prc, psc)) break;
1508 497 : psc = mulii(psc, S->p); vpsc++;
1509 : /* it can happen that S->chi is never squarefree: then change PHI */
1510 497 : if (vpsc > 2*S->mf) PHI = gadd(PHI, ZX_Z_mul(X, S->p));
1511 : }
1512 113533 : psc = mulii(sqri(prc), S->p);
1513 113528 : vpsc = 2*Z_pval(prc, S->p) + 1;
1514 : }
1515 : }
1516 276491 : S->phi = PHI;
1517 276491 : S->chi = FpX_red(S->chi, psc);
1518 :
1519 : /* may happen if p is unramified */
1520 276493 : if (is_pm1(prc)) return 0;
1521 232099 : S->prc = prc;
1522 232099 : S->psc = psc;
1523 232099 : S->vpsc = vpsc; return 1;
1524 : }
1525 :
1526 : /* return 1 if at least 2 factors mod p ==> chi splits
1527 : * Replace S->phi such that F increases (to D) */
1528 : static int
1529 67209 : testb2(decomp_t *S, long D, GEN theta)
1530 : {
1531 67209 : long v = varn(S->chi), dlim = degpol(S->chi)-1;
1532 67209 : GEN T0 = S->phi, chi, phi, nu;
1533 67209 : if (DEBUGLEVEL>4) err_printf(" Increasing Fa\n");
1534 : for (;;)
1535 : {
1536 67262 : phi = gadd(theta, random_FpX(dlim, v, S->p));
1537 67262 : chi = mycaract(S, S->chi, phi, S->psf, S->prc);
1538 : /* phi nonprimary ? */
1539 67261 : if (split_char(S, chi, phi, T0, &nu)) return 1;
1540 67262 : if (degpol(nu) == D) break;
1541 : }
1542 : /* F_phi=lcm(F_alpha, F_theta)=D and E_phi=E_alpha */
1543 67209 : S->phi0 = T0;
1544 67209 : S->chi = chi;
1545 67209 : S->phi = phi;
1546 67209 : S->nu = nu; return 0;
1547 : }
1548 :
1549 : /* return 1 if at least 2 factors mod p ==> chi can be split.
1550 : * compute a new S->phi such that E = lcm(Ea, Et);
1551 : * A a ZX, T a t_INT (iff Et = 1, probably impossible ?) or QX */
1552 : static int
1553 48534 : testc2(decomp_t *S, GEN A, long Ea, GEN T, long Et)
1554 : {
1555 48534 : GEN c, chi, phi, nu, T0 = S->phi;
1556 :
1557 48534 : if (DEBUGLEVEL>4) err_printf(" Increasing Ea\n");
1558 48534 : if (Et == 1) /* same as other branch, split for efficiency */
1559 0 : c = A; /* Et = 1 => s = 1, r = 0, t = 0 */
1560 : else
1561 : {
1562 : long r, s, t;
1563 48534 : (void)cbezout(Ea, Et, &r, &s); t = 0;
1564 48632 : while (r < 0) { r = r + Et; t++; }
1565 48744 : while (s < 0) { s = s + Ea; t++; }
1566 :
1567 : /* A^s T^r / p^t */
1568 48534 : c = RgXQ_mul(RgXQ_powu(A, s, S->chi), RgXQ_powu(T, r, S->chi), S->chi);
1569 48534 : c = RgX_Rg_div(c, powiu(S->p, t));
1570 48534 : c = redelt(c, S->psc, S->p);
1571 : }
1572 48534 : phi = RgX_add(c, pol_x(varn(S->chi)));
1573 48533 : chi = mycaract(S, S->chi, phi, S->psf, S->prc);
1574 48534 : if (split_char(S, chi, phi, T0, &nu)) return 1;
1575 : /* E_phi = lcm(E_alpha,E_theta) */
1576 48534 : S->phi0 = T0;
1577 48534 : S->chi = chi;
1578 48534 : S->phi = phi;
1579 48534 : S->nu = nu; return 0;
1580 : }
1581 :
1582 : /* Return h^(-degpol(P)) P(x * h) if result is integral, NULL otherwise */
1583 : static GEN
1584 59926 : ZX_rescale_inv(GEN P, GEN h)
1585 : {
1586 59926 : long i, l = lg(P);
1587 59926 : GEN Q = cgetg(l,t_POL), hi = h;
1588 59926 : gel(Q,l-1) = gel(P,l-1);
1589 174184 : for (i=l-2; i>=2; i--)
1590 : {
1591 : GEN r;
1592 174183 : gel(Q,i) = dvmdii(gel(P,i), hi, &r);
1593 174178 : if (signe(r)) return NULL;
1594 174178 : if (i == 2) break;
1595 114254 : hi = mulii(hi,h);
1596 : }
1597 59925 : Q[1] = P[1]; return Q;
1598 : }
1599 :
1600 : /* x p^-eq nu^-er mod p */
1601 : static GEN
1602 303352 : get_gamma(decomp_t *S, GEN x, long eq, long er)
1603 : {
1604 303352 : GEN q, g = x, Dg = powiu(S->p, eq);
1605 303349 : long vDg = eq;
1606 303349 : if (er)
1607 : {
1608 22970 : if (!S->invnu)
1609 : {
1610 15822 : while (gdvd(S->chi, S->nu)) S->nu = RgX_Rg_add(S->nu, S->p);
1611 15822 : S->invnu = QXQ_inv(S->nu, S->chi);
1612 15822 : S->invnu = redelt_i(S->invnu, S->psc, S->p, &S->Dinvnu, &S->vDinvnu);
1613 : }
1614 22970 : if (S->Dinvnu) {
1615 22970 : Dg = mulii(Dg, powiu(S->Dinvnu, er));
1616 22970 : vDg += er * S->vDinvnu;
1617 : }
1618 22970 : q = mulii(S->p, Dg);
1619 22970 : g = ZX_mul(g, FpXQ_powu(S->invnu, er, S->chi, q));
1620 22970 : g = FpX_rem(g, S->chi, q);
1621 22970 : update_den(S->p, &g, &Dg, &vDg, NULL);
1622 22968 : g = centermod(g, mulii(S->p, Dg));
1623 : }
1624 303349 : if (!is_pm1(Dg)) g = RgX_Rg_div(g, Dg);
1625 303351 : return g;
1626 : }
1627 : static GEN
1628 356425 : get_g(decomp_t *S, long Ea, long L, long E, GEN beta, GEN *pchig,
1629 : long *peq, long *per)
1630 : {
1631 : long eq, er;
1632 356425 : GEN g, chig, chib = NULL;
1633 : for(;;) /* at most twice */
1634 : {
1635 363275 : if (L < 0)
1636 : {
1637 60655 : chib = ZXQ_charpoly(beta, S->chi, varn(S->chi));
1638 60655 : vstar(S->p, chib, &L, &E);
1639 : }
1640 363277 : eq = L / E; er = L*Ea / E - eq*Ea;
1641 : /* floor(L Ea/E) = eq Ea + er */
1642 363277 : if (er || !chib)
1643 : { /* g might not be an integer ==> chig = NULL */
1644 303351 : g = get_gamma(S, beta, eq, er);
1645 303351 : chig = mycaract(S, S->chi, g, S->psc, S->prc);
1646 : }
1647 : else
1648 : { /* g = beta/p^eq, special case of the above */
1649 59926 : GEN h = powiu(S->p, eq);
1650 59926 : g = RgX_Rg_div(beta, h);
1651 59926 : chig = ZX_rescale_inv(chib, h); /* chib(x h) / h^N */
1652 59924 : if (chig) chig = FpX_red(chig, S->pmf);
1653 : }
1654 : /* either success or second consecutive failure */
1655 363278 : if (chig || chib) break;
1656 : /* if g fails the v*-test, v(beta) was wrong. Retry once */
1657 6850 : L = -1;
1658 : }
1659 356428 : *pchig = chig; *peq = eq; *per = er; return g;
1660 : }
1661 :
1662 : /* return 1 if at least 2 factors mod p ==> chi can be split */
1663 : static int
1664 238604 : loop(decomp_t *S, long Ea)
1665 : {
1666 238604 : pari_sp av = avma;
1667 238604 : GEN beta = FpXQ_powu(S->nu, Ea, S->chi, S->p);
1668 238603 : long N = degpol(S->f), v = varn(S->f);
1669 238603 : S->invnu = NULL;
1670 : for (;;)
1671 117820 : { /* beta tends to a factor of chi */
1672 : long L, i, Fg, eq, er;
1673 356423 : GEN chig = NULL, d, g, nug;
1674 :
1675 356423 : if (DEBUGLEVEL>4) err_printf(" beta = %Ps\n", beta);
1676 356423 : L = ZpX_resultant_val(S->chi, beta, S->p, S->mf+1);
1677 356425 : if (L > S->mf) L = -1; /* from scratch */
1678 356425 : g = get_g(S, Ea, L, N, beta, &chig, &eq, &er);
1679 356428 : if (DEBUGLEVEL>4) err_printf(" (eq,er) = (%ld,%ld)\n", eq,er);
1680 : /* g = beta p^-eq nu^-er (a unit), chig = charpoly(g) */
1681 474216 : if (split_char(S, chig, g,S->phi, &nug)) return 1;
1682 :
1683 235610 : Fg = degpol(nug);
1684 235610 : if (Fg == 1)
1685 : { /* frequent special case nug = x - d */
1686 : long Le, Ee;
1687 : GEN chie, nue, e, pie;
1688 160212 : d = negi(gel(nug,2));
1689 160211 : chie = RgX_translate(chig, d);
1690 160212 : nue = pol_x(v);
1691 160212 : e = RgX_Rg_sub(g, d);
1692 160211 : pie = getprime(S, e, chie, nue, &Le, &Ee, 0,Ea);
1693 160212 : if (pie) return testc2(S, S->nu, Ea, pie, Ee);
1694 : }
1695 : else
1696 : {
1697 75398 : long Fa = degpol(S->nu), vdeng;
1698 : GEN deng, numg, nume;
1699 78884 : if (Fa % Fg) return testb2(S, ulcm(Fa,Fg), g);
1700 : /* nu & nug irreducible mod p, deg nug | deg nu. To improve beta, look
1701 : * for a root d of nug in Fp[phi] such that v_p(g - d) > 0 */
1702 8189 : if (ZX_equal(nug, S->nu))
1703 5971 : d = pol_x(v);
1704 : else
1705 : {
1706 2218 : if (!p_is_prime(S)) pari_err_PRIME("FpX_ffisom",S->p);
1707 2218 : d = FpX_ffisom(nug, S->nu, S->p);
1708 : }
1709 : /* write g = numg / deng, e = nume / deng */
1710 8189 : numg = QpX_remove_denom(g, S->p, &deng, &vdeng);
1711 12836 : for (i = 1; i <= Fg; i++)
1712 : {
1713 : GEN chie, nue, e;
1714 12836 : if (i != 1) d = FpXQ_pow(d, S->p, S->nu, S->p); /* next root */
1715 12836 : nume = ZX_sub(numg, ZX_Z_mul(d, deng));
1716 : /* test e = nume / deng */
1717 12836 : if (ZpX_resultant_val(S->chi, nume, S->p, vdeng*N+1) <= vdeng*N)
1718 4647 : continue;
1719 8189 : e = RgX_Rg_div(nume, deng);
1720 8189 : chie = mycaract(S, S->chi, e, S->psc, S->prc);
1721 9630 : if (split_char(S, chie, e,S->phi, &nue)) return 1;
1722 6144 : if (RgX_is_monomial(nue))
1723 : { /* v_p(e) = v_p(g - d) > 0 */
1724 : long Le, Ee;
1725 : GEN pie;
1726 6144 : pie = getprime(S, e, chie, nue, &Le, &Ee, 0,Ea);
1727 6144 : if (pie) return testc2(S, S->nu, Ea, pie, Ee);
1728 4703 : break;
1729 : }
1730 : }
1731 4703 : if (i > Fg)
1732 : {
1733 0 : if (!p_is_prime(S)) pari_err_PRIME("nilord",S->p);
1734 0 : pari_err_BUG("nilord (no root)");
1735 : }
1736 : }
1737 117822 : if (eq) d = gmul(d, powiu(S->p, eq));
1738 117820 : if (er) d = gmul(d, gpowgs(S->nu, er));
1739 117820 : beta = gsub(beta, d);
1740 :
1741 117820 : if (gc_needed(av,1))
1742 : {
1743 0 : if (DEBUGMEM > 1) pari_warn(warnmem, "nilord");
1744 0 : (void)gc_all(av, S->invnu? 6: 4, &beta, &(S->precns), &(S->ns), &(S->nsf), &(S->invnu), &(S->Dinvnu));
1745 : }
1746 : }
1747 : }
1748 :
1749 : /* E and F cannot decrease; return 1 if O = Zp[phi], 2 if we can get a
1750 : * decomposition and 0 otherwise */
1751 : static long
1752 394402 : progress(decomp_t *S, GEN *ppa, long *pE)
1753 : {
1754 394402 : long E = *pE, F;
1755 394402 : GEN pa = *ppa;
1756 394402 : S->phi0 = NULL; /* no delayed composition */
1757 : for(;;)
1758 2759 : {
1759 : long l, La, Ea; /* N.B If E = 0, getprime cannot return NULL */
1760 397161 : GEN pia = getprime(S, NULL, S->chi, S->nu, &La, &Ea, E,0);
1761 397175 : if (pia) { /* success, we break out in THIS loop */
1762 394416 : pa = (typ(pia) == t_POL)? RgX_RgXQ_eval(pia, S->phi, S->f): pia;
1763 394420 : E = Ea;
1764 394420 : if (La == 1) break; /* no need to change phi so that nu = pia */
1765 : }
1766 : /* phi += prime elt */
1767 65064 : S->phi = typ(pa) == t_INT? RgX_Rg_add_shallow(S->phi, pa)
1768 160758 : : RgX_add(S->phi, pa);
1769 : /* recompute char. poly. chi from scratch */
1770 160757 : S->chi = mycaract(S, S->f, S->phi, S->psf, S->pdf);
1771 160760 : S->nu = get_nu(S->chi, S->p, &l);
1772 160760 : if (l > 1) return 2;
1773 160760 : if (!update_phi(S)) return 1; /* unramified */
1774 160758 : if (pia) break;
1775 : }
1776 394420 : *pE = E; *ppa = pa; F = degpol(S->nu);
1777 394419 : if (DEBUGLEVEL>4) err_printf(" (E, F) = (%ld,%ld)\n", E, F);
1778 394419 : if (E * F == degpol(S->f)) return 1;
1779 238604 : if (loop(S, E)) return 2;
1780 115742 : if (!update_phi(S)) return 1;
1781 71345 : return 0;
1782 : }
1783 :
1784 : /* flag != 0 iff we're looking for the p-adic factorization,
1785 : in which case it is the p-adic precision we want */
1786 : static GEN
1787 454895 : maxord_i(decomp_t *S, GEN p, GEN f, long mf, GEN w, long flag)
1788 : {
1789 454895 : long oE, n = lg(w)-1; /* factor of largest degree */
1790 454895 : GEN opa, D = ZpX_reduced_resultant_fast(f, ZX_deriv(f), p, mf);
1791 454891 : S->pisprime = -1;
1792 454891 : S->p = p;
1793 454891 : S->mf = mf;
1794 454891 : S->nu = gel(w,n);
1795 454891 : S->df = Z_pval(D, p);
1796 454890 : S->pdf = powiu(p, S->df);
1797 454883 : S->phi = pol_x(varn(f));
1798 454888 : S->chi = S->f = f;
1799 454888 : if (n > 1) return Decomp(S, flag); /* FIXME: use bezout_lift_fact */
1800 :
1801 323063 : if (DEBUGLEVEL>4)
1802 0 : err_printf(" entering Nilord: %Ps^%ld\n f = %Ps, nu = %Ps\n",
1803 : p, S->df, S->f, S->nu);
1804 323063 : else if (DEBUGLEVEL>2) err_printf(" entering Nilord\n");
1805 323063 : S->psf = S->psc = mulii(sqri(D), p);
1806 323056 : S->vpsf = S->vpsc = 2*S->df + 1;
1807 323056 : S->prc = mulii(D, p);
1808 323055 : S->chi = FpX_red(S->f, S->psc);
1809 323064 : S->pmf = powiu(p, S->mf+1);
1810 323064 : S->precns = NULL;
1811 323064 : for(opa = NULL, oE = 0;;)
1812 71343 : {
1813 394407 : long n = progress(S, &opa, &oE);
1814 394416 : if (n == 1) return flag? NULL: dbasis(p, S->f, S->mf, S->phi, S->chi);
1815 194206 : if (n == 2) return Decomp(S, flag);
1816 : }
1817 : }
1818 :
1819 : static int
1820 889 : expo_is_squarefree(GEN e)
1821 : {
1822 889 : long i, l = lg(e);
1823 1260 : for (i=1; i<l; i++)
1824 1022 : if (e[i] != 1) return 0;
1825 238 : return 1;
1826 : }
1827 : /* pure round 4 */
1828 : static GEN
1829 896 : ZpX_round4(GEN f, GEN p, GEN w, long prec)
1830 : {
1831 : decomp_t S;
1832 896 : GEN L = maxord_i(&S, p, f, ZpX_disc_val(f,p), w, prec);
1833 896 : return L? L: mkvec(f);
1834 : }
1835 : /* f a squarefree ZX with leading_coeff 1, degree > 0. Return list of
1836 : * irreducible factors in Zp[X] (computed mod p^prec) */
1837 : static GEN
1838 1155 : ZpX_monic_factor_squarefree(GEN f, GEN p, long prec)
1839 : {
1840 1155 : pari_sp av = avma;
1841 : GEN L, fa, w, e;
1842 : long i, l;
1843 1155 : if (degpol(f) == 1) return mkvec(f);
1844 889 : fa = FpX_factor(f,p); w = gel(fa,1); e = gel(fa,2);
1845 : /* no repeated factors: Hensel lift */
1846 889 : if (expo_is_squarefree(e)) return ZpX_liftfact(f, w, powiu(p,prec), p, prec);
1847 651 : l = lg(w);
1848 651 : if (l == 2)
1849 : {
1850 392 : L = ZpX_round4(f,p,w,prec);
1851 392 : if (lg(L) == 2) { set_avma(av); return mkvec(f); }
1852 : }
1853 : else
1854 : { /* >= 2 factors mod p: partial Hensel lift */
1855 259 : GEN D = ZpX_reduced_resultant_fast(f, ZX_deriv(f), p, ZpX_disc_val(f,p));
1856 259 : long r = maxss(2*Z_pval(D,p)+1, prec);
1857 259 : GEN W = cgetg(l, t_VEC);
1858 833 : for (i = 1; i < l; i++)
1859 574 : gel(W,i) = e[i] == 1? gel(w,i): FpX_powu(gel(w,i), e[i], p);
1860 259 : L = ZpX_liftfact(f, W, powiu(p,r), p, r);
1861 833 : for (i = 1; i < l; i++)
1862 574 : gel(L,i) = e[i] == 1? mkvec(gel(L,i))
1863 574 : : ZpX_round4(gel(L,i), p, mkvec(gel(w,i)), prec);
1864 259 : L = shallowconcat1(L);
1865 : }
1866 399 : return gc_GEN(av, L);
1867 : }
1868 :
1869 : /* assume T a ZX with leading_coeff 1, degree > 0 */
1870 : GEN
1871 546 : ZpX_monic_factor(GEN T, GEN p, long prec)
1872 : {
1873 : GEN Q, P, E, F;
1874 : long L, l, i, v;
1875 :
1876 546 : if (degpol(T) == 1) return mkmat2(mkcol(T), mkcol(gen_1));
1877 546 : v = ZX_valrem(T, &T);
1878 546 : Q = ZX_squff(T, &F); l = lg(Q); L = v? l + 1: l;
1879 546 : P = cgetg(L, t_VEC);
1880 546 : E = cgetg(L, t_VEC);
1881 1099 : for (i = 1; i < l; i++)
1882 : {
1883 553 : GEN w = ZpX_monic_factor_squarefree(gel(Q,i), p, prec);
1884 553 : gel(P,i) = w; settyp(w, t_COL);
1885 553 : gel(E,i) = const_col(lg(w)-1, utoipos(F[i]));
1886 : }
1887 546 : if (v) { gel(P,i) = pol_x(varn(T)); gel(E,i) = utoipos(v); }
1888 546 : return mkmat2(shallowconcat1(P), shallowconcat1(E));
1889 : }
1890 :
1891 : /* DT = multiple of disc(T) or NULL
1892 : * Return a multiple of the denominator of an algebraic integer (in Q[X]/(T))
1893 : * when expressed in terms of the power basis */
1894 : GEN
1895 44089 : indexpartial(GEN T, GEN DT)
1896 : {
1897 44089 : pari_sp av = avma;
1898 : long i, nb;
1899 44089 : GEN fa, E, P, U, res = gen_1, dT = ZX_deriv(T);
1900 :
1901 44084 : if (!DT) DT = ZX_disc(T);
1902 44084 : fa = absZ_factor_limit_strict(DT, 0, &U);
1903 44089 : P = gel(fa,1);
1904 44089 : E = gel(fa,2); nb = lg(P)-1;
1905 211804 : for (i = 1; i <= nb; i++)
1906 : {
1907 167724 : long e = itou(gel(E,i)), e2 = e >> 1;
1908 167724 : GEN p = gel(P,i), q = p;
1909 167724 : if (e2 >= 2) q = ZpX_reduced_resultant_fast(T, dT, p, e2);
1910 167724 : res = mulii(res, q);
1911 : }
1912 44080 : if (U)
1913 : {
1914 1916 : long e = itou(gel(U,2)), e2 = e >> 1;
1915 1916 : GEN p = gel(U,1), q = powiu(p, odd(e)? e2+1: e2);
1916 1916 : res = mulii(res, q);
1917 : }
1918 44080 : return gc_INT(av,res);
1919 : }
1920 :
1921 : /*******************************************************************/
1922 : /* */
1923 : /* 2-ELT REPRESENTATION FOR PRIME IDEALS (dividing index) */
1924 : /* */
1925 : /*******************************************************************/
1926 : /* to compute norm of elt in basis form */
1927 : typedef struct {
1928 : long r1;
1929 : GEN M; /* via embed_norm */
1930 :
1931 : GEN D, w, T; /* via resultant if M = NULL */
1932 : } norm_S;
1933 :
1934 : static GEN
1935 502754 : get_norm(norm_S *S, GEN a)
1936 : {
1937 502754 : if (S->M)
1938 : {
1939 : long e;
1940 501348 : GEN N = grndtoi( embed_norm(RgM_RgC_mul(S->M, a), S->r1), &e );
1941 501383 : if (e > -5) pari_err_PREC( "get_norm");
1942 501383 : return N;
1943 : }
1944 1406 : if (S->w) a = RgV_RgC_mul(S->w, a);
1945 1406 : return ZX_resultant_all(S->T, a, S->D, 0);
1946 : }
1947 : static void
1948 214415 : init_norm(norm_S *S, GEN nf, GEN p)
1949 : {
1950 214415 : GEN T = nf_get_pol(nf), M = nf_get_M(nf);
1951 214416 : long N = degpol(T), ex = gexpo(M) + gexpo(mului(8 * N, p));
1952 :
1953 214430 : S->r1 = nf_get_r1(nf);
1954 214432 : if (N * ex <= gprecision(M) - 20)
1955 : { /* enough prec to use embed_norm */
1956 214255 : S->M = M;
1957 214255 : S->D = NULL;
1958 214255 : S->w = NULL;
1959 214255 : S->T = NULL;
1960 : }
1961 : else
1962 : {
1963 191 : GEN w = leafcopy(nf_get_zkprimpart(nf)), D = nf_get_zkden(nf), Dp = sqri(p);
1964 : long i;
1965 191 : if (!equali1(D))
1966 : {
1967 191 : GEN w1 = D;
1968 191 : long v = Z_pval(D, p);
1969 191 : D = powiu(p, v);
1970 191 : Dp = mulii(D, Dp);
1971 191 : gel(w, 1) = remii(w1, Dp);
1972 : }
1973 3969 : for (i=2; i<=N; i++) gel(w,i) = FpX_red(gel(w,i), Dp);
1974 191 : S->M = NULL;
1975 191 : S->D = D;
1976 191 : S->w = w;
1977 191 : S->T = T;
1978 : }
1979 214446 : }
1980 : /* f = f(pr/p), q = p^(f+1), a in pr.
1981 : * Return 1 if v_pr(a) = 1, and 0 otherwise */
1982 : static int
1983 502750 : is_uniformizer(GEN a, GEN q, norm_S *S) { return !dvdii(get_norm(S,a), q); }
1984 :
1985 : /* Return x * y, x, y are t_MAT (Fp-basis of in O_K/p), assume (x,y)=1.
1986 : * Either x or y may be NULL (= O_K), not both */
1987 : static GEN
1988 702333 : mul_intersect(GEN x, GEN y, GEN p)
1989 : {
1990 702333 : if (!x) return y;
1991 373446 : if (!y) return x;
1992 263815 : return FpM_intersect_i(x, y, p);
1993 : }
1994 : /* Fp-basis of (ZK/pr): applied to the primes found in primedec_aux()
1995 : * true nf */
1996 : static GEN
1997 307195 : Fp_basis(GEN nf, GEN pr)
1998 : {
1999 : long i, j, l;
2000 : GEN x, y;
2001 : /* already in basis form (from Buchman-Lenstra) ? */
2002 307195 : if (typ(pr) == t_MAT) return pr;
2003 : /* ordinary prid (from Kummer) */
2004 72767 : x = pr_hnf(nf, pr);
2005 72772 : l = lg(x);
2006 72772 : y = cgetg(l, t_MAT);
2007 613178 : for (i=j=1; i<l; i++)
2008 540407 : if (gequal1(gcoeff(x,i,i))) gel(y,j++) = gel(x,i);
2009 72771 : setlg(y, j); return y;
2010 : }
2011 : /* Let Ip = prod_{ P | p } P be the p-radical. The list L contains the
2012 : * P (mod Ip) seen as sub-Fp-vector spaces of ZK/Ip.
2013 : * Return the list of (Ip / P) (mod Ip).
2014 : * N.B: All ideal multiplications are computed as intersections of Fp-vector
2015 : * spaces. true nf */
2016 : static GEN
2017 214448 : get_LV(GEN nf, GEN L, GEN p, long N)
2018 : {
2019 214448 : long i, l = lg(L)-1;
2020 : GEN LV, LW, A, B;
2021 :
2022 214448 : LV = cgetg(l+1, t_VEC);
2023 214448 : if (l == 1) { gel(LV,1) = matid(N); return LV; }
2024 109631 : LW = cgetg(l+1, t_VEC);
2025 416826 : for (i=1; i<=l; i++) gel(LW,i) = Fp_basis(nf, gel(L,i));
2026 :
2027 : /* A[i] = L[1]...L[i-1], i = 2..l */
2028 109635 : A = cgetg(l+1, t_VEC); gel(A,1) = NULL;
2029 307198 : for (i=1; i < l; i++) gel(A,i+1) = mul_intersect(gel(A,i), gel(LW,i), p);
2030 : /* B[i] = L[i+1]...L[l], i = 1..(l-1) */
2031 109629 : B = cgetg(l+1, t_VEC); gel(B,l) = NULL;
2032 307199 : for (i=l; i>=2; i--) gel(B,i-1) = mul_intersect(gel(B,i), gel(LW,i), p);
2033 416829 : for (i=1; i<=l; i++) gel(LV,i) = mul_intersect(gel(A,i), gel(B,i), p);
2034 109630 : return LV;
2035 : }
2036 :
2037 : static void
2038 0 : errprime(GEN p) { pari_err_PRIME("idealprimedec",p); }
2039 :
2040 : /* P = Fp-basis (over O_K/p) for pr.
2041 : * V = Z-basis for I_p/pr. ramif != 0 iff some pr|p is ramified.
2042 : * Return a p-uniformizer for pr. Assume pr not inert, i.e. m > 0 */
2043 : static GEN
2044 296752 : uniformizer(GEN nf, norm_S *S, GEN P, GEN V, GEN p, int ramif)
2045 : {
2046 296752 : long i, l, f, m = lg(P)-1, N = nf_get_degree(nf);
2047 : GEN u, Mv, x, q;
2048 :
2049 296751 : f = N - m; /* we want v_p(Norm(x)) = p^f */
2050 296751 : q = powiu(p,f+1);
2051 :
2052 296731 : u = FpM_FpC_invimage(shallowconcat(P, V), col_ei(N,1), p);
2053 296749 : setlg(u, lg(P));
2054 296749 : u = centermod(ZM_ZC_mul(P, u), p);
2055 296735 : if (is_uniformizer(u, q, S)) return u;
2056 158045 : if (signe(gel(u,1)) <= 0) /* make sure u[1] in ]-p,p] */
2057 133987 : gel(u,1) = addii(gel(u,1), p); /* try u + p */
2058 : else
2059 24058 : gel(u,1) = subii(gel(u,1), p); /* try u - p */
2060 158035 : if (!ramif || is_uniformizer(u, q, S)) return u;
2061 :
2062 : /* P/p ramified, u in P^2, not in Q for all other Q|p */
2063 86006 : Mv = zk_multable(nf, Z_ZC_sub(gen_1,u));
2064 86011 : l = lg(P);
2065 115332 : for (i=1; i<l; i++)
2066 : {
2067 115332 : x = centermod(ZC_add(u, ZM_ZC_mul(Mv, gel(P,i))), p);
2068 115334 : if (is_uniformizer(x, q, S)) return x;
2069 : }
2070 0 : errprime(p);
2071 : return NULL; /* LCOV_EXCL_LINE */
2072 : }
2073 :
2074 : /*******************************************************************/
2075 : /* */
2076 : /* BUCHMANN-LENSTRA ALGORITHM */
2077 : /* */
2078 : /*******************************************************************/
2079 : static GEN
2080 4257038 : mk_pr(GEN p, GEN u, long e, long f, GEN t)
2081 4257038 : { return mkvec5(p, u, utoipos(e), utoipos(f), t); }
2082 :
2083 : /* nf a true nf, u in Z[X]/(T); pr = p Z_K + u Z_K of ramification index e */
2084 : GEN
2085 3799762 : idealprimedec_kummer(GEN nf,GEN u,long e,GEN p)
2086 : {
2087 3799762 : GEN t, T = nf_get_pol(nf);
2088 3799765 : long f = degpol(u), N = degpol(T);
2089 :
2090 3799752 : if (f == N)
2091 : { /* inert */
2092 624938 : u = scalarcol_shallow(p,N);
2093 624944 : t = gen_1;
2094 : }
2095 : else
2096 : {
2097 3174814 : t = centermod(poltobasis(nf, FpX_div(T, u, p)), p);
2098 3174563 : u = centermod(poltobasis(nf, u), p);
2099 3174534 : if (e == 1)
2100 : { /* make sure v_pr(u) = 1 (automatic if e>1) */
2101 2896795 : GEN cw, w = Q_primitive_part(nf_to_scalar_or_alg(nf, u), &cw);
2102 2897030 : long v = cw? f - Q_pval(cw, p) * N: f;
2103 2897030 : if (ZpX_resultant_val(T, w, p, v + 1) > v)
2104 : {
2105 107695 : GEN c = gel(u,1);
2106 107695 : gel(u,1) = signe(c) > 0? subii(c, p): addii(c, p);
2107 : }
2108 : }
2109 3174854 : t = zk_multable(nf, t);
2110 : }
2111 3799680 : return mk_pr(p,u,e,f,t);
2112 : }
2113 :
2114 : typedef struct {
2115 : GEN nf, p;
2116 : long I;
2117 : } eltmod_muldata;
2118 :
2119 : static GEN
2120 822105 : sqr_mod(void *data, GEN x)
2121 : {
2122 822105 : eltmod_muldata *D = (eltmod_muldata*)data;
2123 822105 : return FpC_red(nfsqri(D->nf, x), D->p);
2124 : }
2125 : static GEN
2126 347667 : ei_msqr_mod(void *data, GEN x)
2127 : {
2128 347667 : GEN x2 = sqr_mod(data, x);
2129 347665 : eltmod_muldata *D = (eltmod_muldata*)data;
2130 347665 : return FpC_red(zk_ei_mul(D->nf, x2, D->I), D->p);
2131 : }
2132 : /* nf a true nf; compute lift(nf.zk[I]^p mod p) */
2133 : static GEN
2134 738685 : pow_ei_mod_p(GEN nf, long I, GEN p)
2135 : {
2136 738685 : pari_sp av = avma;
2137 : eltmod_muldata D;
2138 738685 : long N = nf_get_degree(nf);
2139 738687 : GEN y = col_ei(N,I);
2140 738693 : if (I == 1) return y;
2141 521919 : D.nf = nf;
2142 521919 : D.p = p;
2143 521919 : D.I = I;
2144 521919 : y = gen_pow_fold(y, p, (void*)&D, &sqr_mod, &ei_msqr_mod);
2145 521922 : return gc_upto(av,y);
2146 : }
2147 :
2148 : /* nf a true nf; return a Z basis of Z_K's p-radical, phi = x--> x^p-x */
2149 : static GEN
2150 214443 : pradical(GEN nf, GEN p, GEN *phi)
2151 : {
2152 214443 : long i, N = nf_get_degree(nf);
2153 : GEN q,m,frob,rad;
2154 :
2155 : /* matrix of Frob: x->x^p over Z_K/p */
2156 214443 : frob = cgetg(N+1,t_MAT);
2157 943494 : for (i=1; i<=N; i++) gel(frob,i) = pow_ei_mod_p(nf,i,p);
2158 :
2159 214441 : m = frob; q = p;
2160 304425 : while (abscmpiu(q,N) < 0) { q = mulii(q,p); m = FpM_mul(m, frob, p); }
2161 214441 : rad = FpM_ker(m, p); /* m = Frob^k, s.t p^k >= N */
2162 943458 : for (i=1; i<=N; i++) gcoeff(frob,i,i) = subiu(gcoeff(frob,i,i), 1);
2163 214411 : *phi = frob; return rad;
2164 : }
2165 :
2166 : /* return powers of a: a^0, ... , a^d, d = dim A */
2167 : static GEN
2168 159581 : get_powers(GEN mul, GEN p)
2169 : {
2170 159581 : long i, d = lgcols(mul);
2171 159581 : GEN z, pow = cgetg(d+2,t_MAT), P = pow+1;
2172 :
2173 159580 : gel(P,0) = scalarcol_shallow(gen_1, d-1);
2174 159582 : z = gel(mul,1);
2175 756699 : for (i=1; i<=d; i++)
2176 : {
2177 597124 : gel(P,i) = z; /* a^i */
2178 597124 : if (i!=d) z = FpM_FpC_mul(mul, z, p);
2179 : }
2180 159575 : return pow;
2181 : }
2182 :
2183 : /* minimal polynomial of a in A (dim A = d).
2184 : * mul = multiplication table by a in A */
2185 : static GEN
2186 117697 : pol_min(GEN mul, GEN p)
2187 : {
2188 117697 : pari_sp av = avma;
2189 117697 : GEN z = FpM_deplin(get_powers(mul, p), p);
2190 117697 : return gc_GEN(av, RgV_to_RgX(z,0));
2191 : }
2192 :
2193 : static GEN
2194 411691 : get_pr(GEN nf, norm_S *S, GEN p, GEN P, GEN V, int ramif, long N, long flim)
2195 : {
2196 : GEN u, t;
2197 : long e, f;
2198 :
2199 411691 : if (typ(P) == t_VEC)
2200 : { /* already done (Kummer) */
2201 72772 : f = pr_get_f(P);
2202 72772 : if (flim > 0 && f > flim) return NULL;
2203 71880 : if (flim == -2) return (GEN)f;
2204 71873 : return P;
2205 : }
2206 338919 : f = N - (lg(P)-1);
2207 338919 : if (flim > 0 && f > flim) return NULL;
2208 337320 : if (flim == -2) return (GEN)f;
2209 : /* P = (p,u) prime. t is an anti-uniformizer: Z_K + t/p Z_K = P^(-1),
2210 : * so that v_P(t) = e(P/p)-1 */
2211 336942 : if (f == N) {
2212 40194 : u = scalarcol_shallow(p,N);
2213 40194 : t = gen_1;
2214 40194 : e = 1;
2215 : } else {
2216 : GEN mt;
2217 296748 : u = uniformizer(nf, S, P, V, p, ramif);
2218 296716 : t = FpM_deplin(zk_multable(nf,u), p);
2219 296745 : mt = zk_multable(nf, t);
2220 296746 : e = ramif? 1 + ZC_nfval(t,mk_pr(p,u,0,0,mt)): 1;
2221 296728 : t = mt;
2222 : }
2223 336922 : return mk_pr(p,u,e,f,t);
2224 : }
2225 :
2226 : /* true nf */
2227 : static GEN
2228 214448 : primedec_end(GEN nf, GEN L, GEN p, long flim)
2229 : {
2230 214448 : long i, j, l = lg(L), N = nf_get_degree(nf);
2231 214448 : GEN LV = get_LV(nf, L,p,N);
2232 214449 : int ramif = dvdii(nf_get_disc(nf), p);
2233 214413 : norm_S S; init_norm(&S, nf, p);
2234 625734 : for (i = j = 1; i < l; i++)
2235 : {
2236 411692 : GEN P = get_pr(nf, &S, p, gel(L,i), gel(LV,i), ramif, N, flim);
2237 411684 : if (!P) continue;
2238 409193 : gel(L,j++) = P;
2239 409193 : if (flim == -1) return P;
2240 : }
2241 214042 : setlg(L, j); return L;
2242 : }
2243 :
2244 : /* prime ideal decomposition of p; if flim>0, restrict to f(P,p) <= flim
2245 : * if flim = -1 return only the first P
2246 : * if flim = -2 return only the f(P/p) in a t_VECSMALL; true nf */
2247 : static GEN
2248 2707595 : primedec_aux(GEN nf, GEN p, long flim)
2249 : {
2250 2707595 : const long TYP = (flim == -2)? t_VECSMALL: t_VEC;
2251 2707595 : GEN E, F, L, Ip, phi, f, g, h, UN, T = nf_get_pol(nf);
2252 : long i, k, c, iL, N;
2253 : int kummer;
2254 :
2255 2707580 : F = FpX_factor(T, p);
2256 2707701 : E = gel(F,2);
2257 2707701 : F = gel(F,1);
2258 :
2259 2707701 : k = lg(F); if (k == 1) errprime(p);
2260 2707701 : if ( !dvdii(nf_get_index(nf),p) ) /* p doesn't divide index */
2261 : {
2262 2491656 : L = cgetg(k, TYP);
2263 6075130 : for (i=1; i<k; i++)
2264 : {
2265 4139321 : GEN t = gel(F,i);
2266 4139321 : long f = degpol(t);
2267 4139297 : if (flim > 0 && f > flim) { setlg(L, i); break; }
2268 3588142 : if (flim == -2)
2269 0 : L[i] = f;
2270 : else
2271 3588142 : gel(L,i) = idealprimedec_kummer(nf, t, E[i],p);
2272 3588270 : if (flim == -1) return gel(L,1);
2273 : }
2274 2486964 : return L;
2275 : }
2276 :
2277 215837 : kummer = 0;
2278 215837 : g = FpXV_prod(F, p);
2279 215835 : h = FpX_div(T,g,p);
2280 215836 : f = FpX_red(ZX_Z_divexact(ZX_sub(ZX_mul(g,h), T), p), p);
2281 :
2282 215812 : N = degpol(T);
2283 215823 : L = cgetg(N+1,TYP);
2284 215827 : iL = 1;
2285 524977 : for (i=1; i<k; i++)
2286 310535 : if (E[i] == 1 || signe(FpX_rem(f,gel(F,i),p)))
2287 72772 : {
2288 74164 : GEN t = gel(F,i);
2289 74164 : kummer = 1;
2290 74164 : gel(L,iL++) = idealprimedec_kummer(nf, t, E[i],p);
2291 74165 : if (flim == -1) return gel(L,1);
2292 : }
2293 : else /* F[i] | (f,g,h), happens at least once by Dedekind criterion */
2294 236378 : E[i] = 0;
2295 :
2296 : /* phi matrix of x -> x^p - x in algebra Z_K/p */
2297 214442 : Ip = pradical(nf,p,&phi);
2298 :
2299 : /* split etale algebra Z_K / (p,Ip) */
2300 214428 : h = cgetg(N+1,t_VEC);
2301 214426 : if (kummer)
2302 : { /* split off Kummer factors */
2303 46166 : GEN mb, b = NULL;
2304 172597 : for (i=1; i<k; i++)
2305 126431 : if (!E[i]) b = b? FpX_mul(b, gel(F,i), p): gel(F,i);
2306 46166 : if (!b) errprime(p);
2307 46166 : b = FpC_red(poltobasis(nf,b), p);
2308 46167 : mb = FpM_red(zk_multable(nf,b), p);
2309 : /* Fp-base of ideal (Ip, b) in ZK/p */
2310 46163 : gel(h,1) = FpM_image(shallowconcat(mb,Ip), p);
2311 : }
2312 : else
2313 168260 : gel(h,1) = Ip;
2314 :
2315 214427 : UN = col_ei(N, 1);
2316 468364 : for (c=1; c; c--)
2317 : { /* Let A:= (Z_K/p) / Ip etale; split A2 := A / Im H ~ Im M2
2318 : H * ? + M2 * Mi2 = Id_N ==> M2 * Mi2 projector A --> A2 */
2319 253915 : GEN M, Mi, M2, Mi2, phi2, mat1, H = gel(h,c); /* maximal rank */
2320 253915 : long dim, r = lg(H)-1;
2321 :
2322 253915 : M = FpM_suppl(shallowconcat(H,UN), p);
2323 253921 : Mi = FpM_inv(M, p);
2324 253919 : M2 = vecslice(M, r+1,N); /* M = (H|M2) invertible */
2325 253917 : Mi2 = rowslice(Mi,r+1,N);
2326 : /* FIXME: FpM_mul(,M2) could be done with vecpermute */
2327 253917 : phi2 = FpM_mul(Mi2, FpM_mul(phi,M2, p), p);
2328 253923 : mat1 = FpM_ker(phi2, p);
2329 253925 : dim = lg(mat1)-1; /* A2 product of 'dim' fields */
2330 253925 : if (dim > 1)
2331 : { /* phi2 v = 0 => a = M2 v in Ker phi, a not in Fp.1 + H */
2332 117696 : GEN R, a, mula, mul2, v = gel(mat1,2);
2333 : long n;
2334 :
2335 117696 : a = FpM_FpC_mul(M2,v, p); /* not a scalar */
2336 117694 : mula = FpM_red(zk_multable(nf,a), p);
2337 117693 : mul2 = FpM_mul(Mi2, FpM_mul(mula,M2, p), p);
2338 117697 : R = FpX_roots(pol_min(mul2,p), p); /* totally split mod p */
2339 117696 : n = lg(R)-1;
2340 360191 : for (i=1; i<=n; i++)
2341 : {
2342 242494 : GEN I = RgM_Rg_sub_shallow(mula, gel(R,i));
2343 242486 : gel(h,c++) = FpM_image(shallowconcat(H, I), p);
2344 : }
2345 117697 : if (n == dim)
2346 303026 : for (i=1; i<=n; i++) gel(L,iL++) = gel(h,--c);
2347 : }
2348 : else /* A2 field ==> H maximal, f = N-r = dim(A2) */
2349 136229 : gel(L,iL++) = H;
2350 : }
2351 214449 : setlg(L, iL);
2352 214448 : return primedec_end(nf, L, p, flim);
2353 : }
2354 :
2355 : GEN
2356 2700694 : idealprimedec_limit_f(GEN nf, GEN p, long f)
2357 : {
2358 2700694 : pari_sp av = avma;
2359 : GEN v;
2360 2700694 : if (typ(p) != t_INT) pari_err_TYPE("idealprimedec",p);
2361 2700694 : if (f < 0) pari_err_DOMAIN("idealprimedec", "f", "<", gen_0, stoi(f));
2362 2700694 : v = primedec_aux(checknf(nf), p, f);
2363 2700553 : v = gen_sort(v, (void*)&cmp_prime_over_p, &cmp_nodata);
2364 2700635 : return gc_upto(av,v);
2365 : }
2366 : /* true nf */
2367 : GEN
2368 6552 : idealprimedec_galois(GEN nf, GEN p)
2369 : {
2370 6552 : pari_sp av = avma;
2371 6552 : GEN v = primedec_aux(nf, p, -1);
2372 6552 : return gc_GEN(av,v);
2373 : }
2374 : /* true nf */
2375 : GEN
2376 371 : idealprimedec_degrees(GEN nf, GEN p)
2377 : {
2378 371 : pari_sp av = avma;
2379 371 : GEN v = primedec_aux(nf, p, -2);
2380 371 : vecsmall_sort(v); return gc_leaf(av, v);
2381 : }
2382 : GEN
2383 505220 : idealprimedec_limit_norm(GEN nf, GEN p, GEN B)
2384 505220 : { return idealprimedec_limit_f(nf, p, logint(B,p)); }
2385 : GEN
2386 1273334 : idealprimedec(GEN nf, GEN p)
2387 1273334 : { return idealprimedec_limit_f(nf, p, 0); }
2388 : static GEN
2389 26614 : nf_pV_to_prVV(GEN nf, GEN x)
2390 89649 : { pari_APPLY_same(idealprimedec(nf, gel(x,i))); }
2391 : GEN
2392 38605 : nf_pV_to_prV(GEN nf, GEN x)
2393 : {
2394 38605 : if (lg(x) == 1) return leafcopy(x);
2395 26614 : return shallowconcat1(nf_pV_to_prVV(nf, x));
2396 : }
2397 :
2398 : /* return [Fp[x]: Fp] */
2399 : static long
2400 4109 : ffdegree(GEN x, GEN frob, GEN p)
2401 : {
2402 4109 : pari_sp av = avma;
2403 4109 : long d, f = lg(frob)-1;
2404 4109 : GEN y = x;
2405 :
2406 13209 : for (d=1; d < f; d++)
2407 : {
2408 10878 : y = FpM_FpC_mul(frob, y, p);
2409 10878 : if (ZV_equal(y, x)) break;
2410 : }
2411 4109 : return gc_long(av,d);
2412 : }
2413 :
2414 : static GEN
2415 92185 : lift_to_zk(GEN v, GEN c, long N)
2416 : {
2417 92185 : GEN w = zerocol(N);
2418 92185 : long i, l = lg(c);
2419 307523 : for (i=1; i<l; i++) gel(w,c[i]) = gel(v,i);
2420 92185 : return w;
2421 : }
2422 :
2423 : /* return t = 1 mod pr, t = 0 mod p / pr^e(pr/p) */
2424 : GEN
2425 966800 : pr_anti_uniformizer(GEN nf, GEN pr)
2426 : {
2427 966800 : long N = nf_get_degree(nf), e = pr_get_e(pr);
2428 : GEN p, b, z;
2429 :
2430 966772 : if (e * pr_get_f(pr) == N) return gen_1;
2431 459289 : p = pr_get_p(pr);
2432 459286 : b = pr_get_tau(pr); /* ZM */
2433 459283 : if (e != 1)
2434 : {
2435 22785 : GEN q = powiu(pr_get_p(pr), e-1);
2436 22785 : b = ZM_Z_divexact(ZM_powu(b,e), q);
2437 : }
2438 : /* b = tau^e / p^(e-1), v_pr(b) = 0, v_Q(b) >= e(Q/p) for other Q | p */
2439 459281 : z = ZM_hnfmodid(FpM_red(b,p), p); /* ideal (p) / pr^e, coprime to pr */
2440 459312 : z = idealaddtoone_raw(nf, pr, z);
2441 459298 : return Z_ZC_sub(gen_1, FpC_center(FpC_red(z,p), p, shifti(p,-1)));
2442 : }
2443 :
2444 : #define mpr_TAU 1
2445 : #define mpr_FFP 2
2446 : #define mpr_NFP 5
2447 : #define SMALLMODPR 4
2448 : #define LARGEMODPR 6
2449 : static GEN
2450 3517485 : modpr_TAU(GEN modpr)
2451 : {
2452 3517485 : GEN tau = gel(modpr,mpr_TAU);
2453 3517485 : return isintzero(tau)? NULL: tau;
2454 : }
2455 :
2456 : /* H = HNF matrix, which is identity but for the first line. Return a
2457 : * projector to Z^n / H ~ Z/qZ, with q = H[1,1] */
2458 : GEN
2459 1030271 : hnf_Znproj(GEN H)
2460 : {
2461 1030271 : long i, l = lg(H);
2462 1030271 : GEN p = cgetg(l, t_VEC), q = gcoeff(H,1,1);
2463 1030265 : gel(p,1) = gen_1;
2464 2375161 : for (i = 2; i < l; i++) gel(p,i) = Fp_neg(gcoeff(H,1,i), q);
2465 1030183 : return p;
2466 : }
2467 :
2468 : /* p not necessarily prime, but coprime to denom(basis) */
2469 : GEN
2470 203 : QXQV_to_FpM(GEN basis, GEN T, GEN p)
2471 : {
2472 203 : long i, l = lg(basis), f = degpol(T);
2473 203 : GEN z = cgetg(l, t_MAT);
2474 4515 : for (i = 1; i < l; i++)
2475 : {
2476 4312 : GEN w = gel(basis,i);
2477 4312 : if (typ(w) == t_INT)
2478 0 : w = scalarcol_shallow(w, f);
2479 : else
2480 : {
2481 : GEN dx;
2482 4312 : w = Q_remove_denom(w, &dx);
2483 4312 : w = FpXQ_red(w, T, p);
2484 4312 : if (dx)
2485 : {
2486 0 : dx = Fp_inv(dx, p);
2487 0 : if (!equali1(dx)) w = FpX_Fp_mul(w, dx, p);
2488 : }
2489 4312 : w = RgX_to_RgC(w, f);
2490 : }
2491 4312 : gel(z,i) = w; /* w_i mod (T,p) */
2492 : }
2493 203 : return z;
2494 : }
2495 :
2496 : /* initialize reduction mod pr; if zk = 1, will only init data required to
2497 : * reduce *integral* element. Realize (O_K/pr) as Fp[X] / (T), for a
2498 : * *monic* T; use variable vT for varn(T) */
2499 : static GEN
2500 1158184 : modprinit(GEN nf, GEN pr, int zk, long vT)
2501 : {
2502 1158184 : pari_sp av = avma;
2503 : GEN res, tau, mul, x, p, T, pow, ffproj, nfproj, prh, c;
2504 : long N, i, k, f;
2505 :
2506 1158184 : nf = checknf(nf); checkprid(pr);
2507 1158154 : if (vT < 0) vT = nf_get_varn(nf);
2508 1158145 : f = pr_get_f(pr);
2509 1158142 : N = nf_get_degree(nf);
2510 1158131 : prh = pr_hnf(nf, pr);
2511 1158174 : tau = zk? gen_0: pr_anti_uniformizer(nf, pr);
2512 1158128 : p = pr_get_p(pr);
2513 :
2514 1158127 : if (f == 1)
2515 : {
2516 1012086 : res = cgetg(SMALLMODPR, t_COL);
2517 1012087 : gel(res,mpr_TAU) = tau;
2518 1012087 : gel(res,mpr_FFP) = hnf_Znproj(prh);
2519 1012018 : gel(res,3) = pr; return gc_GEN(av, res);
2520 : }
2521 :
2522 146041 : c = cgetg(f+1, t_VECSMALL);
2523 146048 : ffproj = cgetg(N+1, t_MAT);
2524 600683 : for (k=i=1; i<=N; i++)
2525 : {
2526 454635 : x = gcoeff(prh, i,i);
2527 454635 : if (!is_pm1(x)) { c[k] = i; gel(ffproj,i) = col_ei(N, i); k++; }
2528 : else
2529 128646 : gel(ffproj,i) = ZC_neg(gel(prh,i));
2530 : }
2531 146048 : ffproj = rowpermute(ffproj, c);
2532 146047 : if (! dvdii(nf_get_index(nf), p))
2533 : {
2534 104163 : GEN basis = nf_get_zkprimpart(nf), D = nf_get_zkden(nf);
2535 104164 : if (N == f)
2536 : { /* pr inert */
2537 45297 : T = nf_get_pol(nf);
2538 45297 : T = FpX_red(T,p);
2539 45296 : ffproj = RgV_to_RgM(basis, lg(basis)-1);
2540 : }
2541 : else
2542 : {
2543 58867 : T = RgV_RgC_mul(basis, pr_get_gen(pr));
2544 58867 : T = FpX_normalize(FpX_red(T,p),p);
2545 58867 : basis = FqV_red(vecpermute(basis,c), T, p);
2546 58867 : basis = RgV_to_RgM(basis, lg(basis)-1);
2547 58867 : ffproj = ZM_mul(basis, ffproj);
2548 : }
2549 104164 : setvarn(T, vT);
2550 104164 : ffproj = FpM_red(ffproj, p);
2551 104164 : if (!equali1(D))
2552 : {
2553 33259 : D = modii(D,p);
2554 33259 : if (!equali1(D)) ffproj = FpM_Fp_mul(ffproj, Fp_inv(D,p), p);
2555 : }
2556 :
2557 104164 : res = cgetg(SMALLMODPR+1, t_COL);
2558 104164 : gel(res,mpr_TAU) = tau;
2559 104164 : gel(res,mpr_FFP) = ffproj;
2560 104164 : gel(res,3) = pr;
2561 104164 : gel(res,4) = T; return gc_GEN(av, res);
2562 : }
2563 :
2564 41883 : if (uisprime(f))
2565 : {
2566 39552 : mul = ei_multable(nf, c[2]);
2567 39553 : mul = vecpermute(mul, c);
2568 : }
2569 : else
2570 : {
2571 : GEN v, u, u2, frob;
2572 : long deg,deg1,deg2;
2573 :
2574 : /* matrix of Frob: x->x^p over Z_K/pr = < w[c1], ..., w[cf] > over Fp */
2575 2331 : frob = cgetg(f+1, t_MAT);
2576 11963 : for (i=1; i<=f; i++)
2577 : {
2578 9632 : x = pow_ei_mod_p(nf,c[i],p);
2579 9632 : gel(frob,i) = FpM_FpC_mul(ffproj, x, p);
2580 : }
2581 2331 : u = col_ei(f,2); k = 2;
2582 2331 : deg1 = ffdegree(u, frob, p);
2583 4102 : while (deg1 < f)
2584 : {
2585 1771 : k++; u2 = col_ei(f, k);
2586 1771 : deg2 = ffdegree(u2, frob, p);
2587 1771 : deg = ulcm(deg1,deg2);
2588 1771 : if (deg == deg1) continue;
2589 1764 : if (deg == deg2) { deg1 = deg2; u = u2; continue; }
2590 7 : u = ZC_add(u, u2);
2591 7 : while (ffdegree(u, frob, p) < deg) u = ZC_add(u, u2);
2592 7 : deg1 = deg;
2593 : }
2594 2331 : v = lift_to_zk(u,c,N);
2595 :
2596 2331 : mul = cgetg(f+1,t_MAT);
2597 2331 : gel(mul,1) = v; /* assume w_1 = 1 */
2598 9632 : for (i=2; i<=f; i++) gel(mul,i) = zk_ei_mul(nf,v,c[i]);
2599 : }
2600 :
2601 : /* Z_K/pr = Fp(v), mul = mul by v */
2602 41884 : mul = FpM_red(mul, p);
2603 41884 : mul = FpM_mul(ffproj, mul, p);
2604 :
2605 41884 : pow = get_powers(mul, p);
2606 41882 : T = RgV_to_RgX(FpM_deplin(pow, p), vT);
2607 41883 : nfproj = cgetg(f+1, t_MAT);
2608 131736 : for (i=1; i<=f; i++) gel(nfproj,i) = lift_to_zk(gel(pow,i), c, N);
2609 :
2610 41882 : setlg(pow, f+1);
2611 41882 : ffproj = FpM_mul(FpM_inv(pow, p), ffproj, p);
2612 :
2613 41883 : res = cgetg(LARGEMODPR, t_COL);
2614 41882 : gel(res,mpr_TAU) = tau;
2615 41882 : gel(res,mpr_FFP) = ffproj;
2616 41882 : gel(res,3) = pr;
2617 41882 : gel(res,4) = T;
2618 41882 : gel(res,mpr_NFP) = nfproj; return gc_GEN(av, res);
2619 : }
2620 :
2621 : GEN
2622 7 : nfmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 0, -1); }
2623 : GEN
2624 175369 : zkmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 1, -1); }
2625 : GEN
2626 77 : nfmodprinit0(GEN nf, GEN pr, long v) { return modprinit(nf, pr, 0, v); }
2627 :
2628 : /* x may be a modpr */
2629 : static int
2630 4367638 : ok_modpr(GEN x)
2631 4367638 : { return typ(x) == t_COL && lg(x) >= SMALLMODPR && lg(x) <= LARGEMODPR; }
2632 : void
2633 210 : checkmodpr(GEN x)
2634 : {
2635 210 : if (!ok_modpr(x)) pari_err_TYPE("checkmodpr [use nfmodprinit]", x);
2636 210 : checkprid(modpr_get_pr(x));
2637 210 : }
2638 : GEN
2639 137753 : get_modpr(GEN x)
2640 137753 : { return ok_modpr(x)? x: NULL; }
2641 :
2642 : int
2643 22875895 : checkprid_i(GEN x)
2644 : {
2645 22057078 : return (typ(x) == t_VEC && lg(x) == 6
2646 21984042 : && typ(gel(x,2)) == t_COL && typ(gel(x,3)) == t_INT
2647 44932973 : && typ(gel(x,5)) != t_COL); /* tau changed to t_MAT/t_INT in 2.6 */
2648 : }
2649 : void
2650 17905789 : checkprid(GEN x)
2651 17905789 : { if (!checkprid_i(x)) pari_err_TYPE("checkprid",x); }
2652 : GEN
2653 939491 : get_prid(GEN x)
2654 : {
2655 939491 : long lx = lg(x);
2656 939491 : if (lx == 3 && typ(x) == t_VEC) x = gel(x,1);
2657 939491 : if (checkprid_i(x)) return x;
2658 694673 : if (ok_modpr(x)) {
2659 108465 : x = modpr_get_pr(x);
2660 108465 : if (checkprid_i(x)) return x;
2661 : }
2662 586208 : return NULL;
2663 : }
2664 :
2665 : static GEN
2666 3535011 : to_ff_init(GEN nf, GEN *pr, GEN *T, GEN *p, int zk)
2667 : {
2668 3535011 : GEN modpr = ok_modpr(*pr)? *pr: modprinit(nf, *pr, zk, -1);
2669 3535126 : *T = modpr_get_T(modpr);
2670 3535069 : *pr = modpr_get_pr(modpr);
2671 3535059 : *p = pr_get_p(*pr); return modpr;
2672 : }
2673 :
2674 : /* Return an element of O_K which is set to x Mod T */
2675 : GEN
2676 4508 : modpr_genFq(GEN modpr)
2677 : {
2678 4508 : switch(lg(modpr))
2679 : {
2680 917 : case SMALLMODPR: /* Fp */
2681 917 : return gen_1;
2682 1568 : case LARGEMODPR: /* painful case, p \mid index */
2683 1568 : return gmael(modpr,mpr_NFP, 2);
2684 2023 : default: /* trivial case : p \nmid index */
2685 : {
2686 2023 : long v = varn( modpr_get_T(modpr) );
2687 2023 : return pol_x(v);
2688 : }
2689 : }
2690 : }
2691 :
2692 : GEN
2693 3516232 : nf_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
2694 3516232 : GEN modpr = to_ff_init(nf,pr,T,p,0);
2695 3516292 : GEN tau = modpr_TAU(modpr);
2696 3516262 : if (!tau) gel(modpr,mpr_TAU) = pr_anti_uniformizer(nf, *pr);
2697 3516262 : return modpr;
2698 : }
2699 : GEN
2700 18760 : zk_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
2701 18760 : return to_ff_init(nf,pr,T,p,1);
2702 : }
2703 :
2704 : /* assume x in 'basis' form (t_COL) */
2705 : GEN
2706 6394898 : zk_to_Fq(GEN x, GEN modpr)
2707 : {
2708 6394898 : GEN pr = modpr_get_pr(modpr), p = pr_get_p(pr);
2709 6394892 : GEN ffproj = gel(modpr,mpr_FFP);
2710 6394892 : GEN T = modpr_get_T(modpr);
2711 6394912 : return T? FpM_FpC_mul_FpX(ffproj,x, p, varn(T)): FpV_dotproduct(ffproj,x, p);
2712 : }
2713 :
2714 : /* REDUCTION Modulo a prime ideal */
2715 :
2716 : /* nf a true nf, not GC-clean, OK for gc_upto */
2717 : static GEN
2718 13906335 : nf_to_Fq_i(GEN nf, GEN x0, GEN modpr)
2719 : {
2720 13906335 : GEN x = x0, den, pr = modpr_get_pr(modpr), p = pr_get_p(pr);
2721 13906335 : long tx = typ(x);
2722 :
2723 13906335 : if (tx == t_POLMOD) { x = gel(x,2); tx = typ(x); }
2724 13906335 : switch(tx)
2725 : {
2726 7600437 : case t_INT: return modii(x, p);
2727 5574 : case t_FRAC: return Rg_to_Fp(x, p);
2728 204195 : case t_POL:
2729 204195 : switch(lg(x))
2730 : {
2731 224 : case 2: return gen_0;
2732 25789 : case 3: return Rg_to_Fp(gel(x,2), p);
2733 : }
2734 178182 : x = Q_remove_denom(x, &den);
2735 178185 : x = poltobasis(nf, x);
2736 : /* content(x) and den may not be coprime */
2737 177981 : break;
2738 6096158 : case t_COL:
2739 6096158 : x = Q_remove_denom(x, &den);
2740 : /* content(x) and den are coprime */
2741 6096160 : if (lg(x)-1 == nf_get_degree(nf)) break;
2742 48 : default: pari_err_TYPE("Rg_to_ff",x);
2743 : return NULL;/*LCOV_EXCL_LINE*/
2744 : }
2745 6274086 : if (den)
2746 : {
2747 49013 : long v = Z_pvalrem(den, p, &den);
2748 49013 : if (v)
2749 : {
2750 1799 : if (tx == t_POL) v -= ZV_pvalrem(x, p, &x);
2751 : /* now v = valuation(true denominator of x) */
2752 1799 : if (v > 0)
2753 : {
2754 1197 : GEN tau = modpr_TAU(modpr);
2755 1197 : if (!tau) pari_err_TYPE("zk_to_ff", x0);
2756 1197 : x = nfmuli(nf,x, nfpow_u(nf, tau, v));
2757 1197 : v -= ZV_pvalrem(x, p, &x);
2758 : }
2759 1799 : if (v > 0) pari_err_INV("Rg_to_ff", mkintmod(gen_0,p));
2760 1771 : if (v) return gen_0;
2761 1232 : if (is_pm1(den)) den = NULL;
2762 : }
2763 48446 : x = FpC_red(x, p);
2764 : }
2765 6273519 : x = zk_to_Fq(x, modpr);
2766 6273565 : if (den)
2767 : {
2768 47914 : GEN c = Fp_inv(den, p);
2769 47921 : x = typ(x) == t_INT? Fp_mul(x,c,p): FpX_Fp_mul(x,c,p);
2770 : }
2771 6273572 : return x;
2772 : }
2773 :
2774 : GEN
2775 210 : nfreducemodpr(GEN nf, GEN x, GEN modpr)
2776 : {
2777 210 : pari_sp av = avma;
2778 210 : nf = checknf(nf); checkmodpr(modpr);
2779 210 : return gc_upto(av, algtobasis(nf, Fq_to_nf(nf_to_Fq_i(nf,x,modpr),modpr)));
2780 : }
2781 :
2782 : GEN
2783 350 : nfmodpr(GEN nf, GEN x, GEN pr)
2784 : {
2785 350 : pari_sp av = avma;
2786 : GEN T, p, modpr;
2787 350 : nf = checknf(nf);
2788 350 : modpr = nf_to_Fq_init(nf, &pr, &T, &p);
2789 343 : if (typ(x) == t_MAT && lg(x) == 3)
2790 42 : {
2791 49 : GEN y, v = famat_nfvalrem(nf, x, pr, &y);
2792 49 : long s = signe(v);
2793 49 : if (s < 0) pari_err_INV("nfmodpr", mkintmod(gen_0,p));
2794 42 : if (s > 0)
2795 28 : x = gen_0;
2796 : else
2797 14 : x = FqV_factorback(nfV_to_FqV(gel(y,1), nf, modpr), gel(y,2), T, p);
2798 : }
2799 : else
2800 294 : x = nf_to_Fq_i(nf, x, modpr);
2801 224 : if (!T) return gc_upto(av, Fp_to_mod(x, p));
2802 56 : x = Fq_to_FF(x, Tp_to_FF(T,p));
2803 56 : return gc_GEN(av, x);
2804 : }
2805 : GEN
2806 77 : nfmodprlift(GEN nf, GEN x, GEN pr)
2807 : {
2808 77 : pari_sp av = avma;
2809 : GEN T, p, modpr;
2810 : long d;
2811 77 : nf = checknf(nf);
2812 77 : switch(typ(x))
2813 : {
2814 7 : case t_INT: return icopy(x);
2815 28 : case t_INTMOD: return icopy(gel(x,2));
2816 14 : case t_FFELT: break;
2817 28 : case t_VEC: case t_COL: case t_MAT:
2818 63 : pari_APPLY_same(nfmodprlift(nf,gel(x,i),pr));
2819 0 : default: pari_err_TYPE("nfmodprlit",x);
2820 : }
2821 14 : x = FF_to_FpXQ(x);
2822 14 : setvarn(x, nf_get_varn(nf));
2823 14 : d = degpol(x);
2824 14 : if (d <= 0) { set_avma(av); return d? gen_0: icopy(gel(x,2)); }
2825 14 : modpr = nf_to_Fq_init(nf, &pr, &T, &p);
2826 14 : return gc_GEN(av, Fq_to_nf(x, modpr));
2827 : }
2828 :
2829 : /* lift A from residue field to nf */
2830 : GEN
2831 3086532 : Fq_to_nf(GEN A, GEN modpr)
2832 : {
2833 : long dA;
2834 3086532 : if (typ(A) == t_INT || lg(modpr) < LARGEMODPR) return A;
2835 44773 : dA = degpol(A);
2836 44773 : if (dA <= 0) return dA ? gen_0: gel(A,2);
2837 40580 : return ZM_ZX_mul(gel(modpr,mpr_NFP), A);
2838 : }
2839 : GEN
2840 0 : FqV_to_nfV(GEN x, GEN modpr)
2841 0 : { pari_APPLY_same(Fq_to_nf(gel(x,i), modpr)) }
2842 : GEN
2843 2934 : FqM_to_nfM(GEN A, GEN modpr)
2844 : {
2845 2934 : long i,j,h,l = lg(A);
2846 2934 : GEN B = cgetg(l, t_MAT);
2847 :
2848 2934 : if (l == 1) return B;
2849 2633 : h = lgcols(A);
2850 11010 : for (j=1; j<l; j++)
2851 : {
2852 8377 : GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
2853 52732 : for (i=1; i<h; i++) gel(Bj,i) = Fq_to_nf(gel(Aj,i), modpr);
2854 : }
2855 2633 : return B;
2856 : }
2857 : GEN
2858 10184 : FqX_to_nfX(GEN x, GEN modpr)
2859 : {
2860 10184 : if (typ(x) != t_POL) return icopy(x); /* scalar */
2861 42998 : pari_APPLY_pol(Fq_to_nf(gel(x,i), modpr));
2862 : }
2863 :
2864 : /* true nf */
2865 : static GEN
2866 13905819 : gc_nf_to_Fq(GEN nf, GEN A, GEN modpr)
2867 : {
2868 13905819 : pari_sp av = avma;
2869 13905819 : return gc_upto(av, nf_to_Fq_i(nf, A, modpr));
2870 : }
2871 : GEN
2872 12639480 : nf_to_Fq(GEN nf, GEN A, GEN modpr)
2873 12639480 : { return gc_nf_to_Fq(checknf(nf), A, modpr); }
2874 : /* A t_VEC/t_COL */
2875 : GEN
2876 167410 : nfV_to_FqV(GEN x, GEN nf, GEN modpr)
2877 : {
2878 167410 : nf = checknf(nf);
2879 916830 : pari_APPLY_same(gc_nf_to_Fq(nf, gel(x,i), modpr));
2880 : }
2881 : /* A t_MAT */
2882 : GEN
2883 1872 : nfM_to_FqM(GEN A, GEN nf, GEN modpr)
2884 : {
2885 1872 : long i,j,h,l = lg(A);
2886 1872 : GEN B = cgetg(l,t_MAT);
2887 :
2888 1872 : if (l == 1) return B;
2889 1872 : h = lgcols(A); nf = checknf(nf);
2890 42624 : for (j=1; j<l; j++)
2891 : {
2892 40752 : GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
2893 305810 : for (i=1; i<h; i++) gel(Bj,i) = gc_nf_to_Fq(nf, gel(Aj,i), modpr);
2894 : }
2895 1872 : return B;
2896 : }
2897 : /* A t_POL */
2898 : GEN
2899 9373 : nfX_to_FqX(GEN x, GEN nf, GEN modpr)
2900 : {
2901 9373 : nf = checknf(nf);
2902 51659 : pari_APPLY_pol(gc_nf_to_Fq(nf, gel(x,i), modpr));
2903 : }
2904 :
2905 : /*******************************************************************/
2906 : /* */
2907 : /* RELATIVE ROUND 2 */
2908 : /* */
2909 : /*******************************************************************/
2910 : /* Shallow functions */
2911 : /* FIXME: use a bb_field and export the nfX_* routines */
2912 : static GEN
2913 4123 : nfX_sub(GEN nf, GEN x, GEN y)
2914 : {
2915 4123 : long i, lx = lg(x), ly = lg(y);
2916 : GEN z;
2917 4123 : if (ly <= lx) {
2918 4123 : z = cgetg(lx,t_POL); z[1] = x[1];
2919 24640 : for (i=2; i < ly; i++) gel(z,i) = nfsub(nf,gel(x,i),gel(y,i));
2920 4123 : for ( ; i < lx; i++) gel(z,i) = gel(x,i);
2921 4123 : z = normalizepol_lg(z, lx);
2922 : } else {
2923 0 : z = cgetg(ly,t_POL); z[1] = y[1];
2924 0 : for (i=2; i < lx; i++) gel(z,i) = nfsub(nf,gel(x,i),gel(y,i));
2925 0 : for ( ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
2926 0 : z = normalizepol_lg(z, ly);
2927 : }
2928 4123 : return z;
2929 : }
2930 : /* FIXME: quadratic multiplication */
2931 : static GEN
2932 20646 : nfX_mul(GEN nf, GEN a, GEN b)
2933 : {
2934 20646 : long da = degpol(a), db = degpol(b), dc, lc, k;
2935 : GEN c;
2936 20646 : if (da < 0 || db < 0) return gen_0;
2937 20646 : dc = da + db;
2938 20646 : if (dc == 0) return nfmul(nf, gel(a,2),gel(b,2));
2939 20646 : lc = dc+3;
2940 20646 : c = cgetg(lc, t_POL); c[1] = a[1];
2941 169839 : for (k = 0; k <= dc; k++)
2942 : {
2943 149193 : long i, I = minss(k, da);
2944 149193 : GEN d = NULL;
2945 541142 : for (i = maxss(k-db, 0); i <= I; i++)
2946 : {
2947 391949 : GEN e = nfmul(nf, gel(a, i+2), gel(b, k-i+2));
2948 391949 : d = d? nfadd(nf, d, e): e;
2949 : }
2950 149193 : gel(c, k+2) = d;
2951 : }
2952 20646 : return normalizepol_lg(c, lc);
2953 : }
2954 : /* assume b monic */
2955 : static GEN
2956 16523 : nfX_rem(GEN nf, GEN a, GEN b)
2957 : {
2958 16523 : long da = degpol(a), db = degpol(b);
2959 16523 : if (da < 0) return gen_0;
2960 16523 : a = leafcopy(a);
2961 39759 : while (da >= db)
2962 : {
2963 23236 : long i, k = da;
2964 23236 : GEN A = gel(a, k+2);
2965 190325 : for (i = db-1, k--; i >= 0; i--, k--)
2966 167089 : gel(a,k+2) = nfsub(nf, gel(a,k+2), nfmul(nf, A, gel(b,i+2)));
2967 23236 : a = normalizepol_lg(a, lg(a)-1);
2968 23236 : da = degpol(a);
2969 : }
2970 16523 : return a;
2971 : }
2972 : static GEN
2973 16523 : nfXQ_mul(GEN nf, GEN a, GEN b, GEN T)
2974 : {
2975 16523 : GEN c = nfX_mul(nf, a, b);
2976 16523 : if (typ(c) != t_POL) return c;
2977 16523 : return nfX_rem(nf, c, T);
2978 : }
2979 :
2980 : static void
2981 4222 : fill(long l, GEN H, GEN Hx, GEN I, GEN Ix)
2982 : {
2983 : long i;
2984 4222 : if (typ(Ix) == t_VEC) /* standard */
2985 15335 : for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = gel(Ix,i); }
2986 : else /* constant ideal */
2987 3830 : for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = Ix; }
2988 4222 : }
2989 :
2990 : /* given MODULES x and y by their pseudo-bases, returns a pseudo-basis of the
2991 : * module generated by x and y. */
2992 : static GEN
2993 2111 : rnfjoinmodules_i(GEN nf, GEN Hx, GEN Ix, GEN Hy, GEN Iy)
2994 : {
2995 2111 : long lx = lg(Hx), ly = lg(Hy), l = lx+ly-1;
2996 2111 : GEN H = cgetg(l, t_MAT), I = cgetg(l, t_VEC);
2997 2111 : fill(lx, H , Hx, I , Ix);
2998 2111 : fill(ly, H+lx-1, Hy, I+lx-1, Iy); return nfhnf(nf, mkvec2(H, I));
2999 : }
3000 : static GEN
3001 1329 : rnfjoinmodules(GEN nf, GEN x, GEN y)
3002 : {
3003 1329 : if (!x) return y;
3004 644 : if (!y) return x;
3005 644 : return rnfjoinmodules_i(nf, gel(x,1), gel(x,2), gel(y,1), gel(y,2));
3006 : }
3007 :
3008 : typedef struct {
3009 : GEN multab, T,p;
3010 : long h;
3011 : } rnfeltmod_muldata;
3012 :
3013 : static GEN
3014 16190 : _sqr(void *data, GEN x)
3015 : {
3016 16190 : rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
3017 10356 : GEN z = x? tablesqr(D->multab,x)
3018 16190 : : tablemul_ei_ej(D->multab,D->h,D->h);
3019 16190 : return FqC_red(z,D->T,D->p);
3020 : }
3021 : static GEN
3022 4158 : _msqr(void *data, GEN x)
3023 : {
3024 4158 : GEN x2 = _sqr(data, x), z;
3025 4158 : rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
3026 4158 : z = tablemul_ei(D->multab, x2, D->h);
3027 4158 : return FqC_red(z,D->T,D->p);
3028 : }
3029 :
3030 : /* Compute W[h]^n mod (T,p) in the extension, assume n >= 0. T a ZX */
3031 : static GEN
3032 5834 : rnfeltid_powmod(GEN multab, long h, GEN n, GEN T, GEN p)
3033 : {
3034 5834 : pari_sp av = avma;
3035 : GEN y;
3036 : rnfeltmod_muldata D;
3037 :
3038 5834 : if (!signe(n)) return gen_1;
3039 :
3040 5834 : D.multab = multab;
3041 5834 : D.h = h;
3042 5834 : D.T = T;
3043 5834 : D.p = p;
3044 5834 : y = gen_pow_fold(NULL, n, (void*)&D, &_sqr, &_msqr);
3045 5834 : return gc_GEN(av, y);
3046 : }
3047 :
3048 : /* P != 0 has at most degpol(P) roots. Look for an element in Fq which is not
3049 : * a root, cf repres() */
3050 : static GEN
3051 21 : FqX_non_root(GEN P, GEN T, GEN p)
3052 : {
3053 21 : long dP = degpol(P), f, vT;
3054 : long i, j, k, pi, pp;
3055 : GEN v;
3056 :
3057 21 : if (dP == 0) return gen_1;
3058 21 : pp = is_bigint(p) ? dP+1: itos(p);
3059 21 : v = cgetg(dP + 2, t_VEC);
3060 21 : gel(v,1) = gen_0;
3061 21 : if (T)
3062 0 : { f = degpol(T); vT = varn(T); }
3063 : else
3064 21 : { f = 1; vT = 0; }
3065 42 : for (i=pi=1; i<=f; i++,pi*=pp)
3066 : {
3067 21 : GEN gi = i == 1? gen_1: pol_xn(i-1, vT), jgi = gi;
3068 42 : for (j=1; j<pp; j++)
3069 : {
3070 42 : for (k=1; k<=pi; k++)
3071 : {
3072 21 : GEN z = Fq_add(gel(v,k), jgi, T,p);
3073 21 : if (!gequal0(FqX_eval(P, z, T,p))) return z;
3074 21 : gel(v, j*pi+k) = z;
3075 : }
3076 21 : if (j < pp-1) jgi = Fq_add(jgi, gi, T,p); /* j*g[i] */
3077 : }
3078 : }
3079 21 : return NULL;
3080 : }
3081 :
3082 : /* true nf, x t_POL */
3083 : static int
3084 8225 : nfpolisintegral_i(GEN nf, GEN x)
3085 : {
3086 8225 : GEN d, T = nf_get_pol(nf);
3087 8225 : long l = lg(x);
3088 8225 : if (varn(x) != varn(T)) pari_err_VAR("nfisintegral", x,T);
3089 8225 : if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
3090 8225 : if (l == 2) return 1;
3091 8225 : if (l == 3)
3092 : {
3093 0 : switch(typ(gel(x,2)))
3094 : {
3095 0 : case t_INT: return 1;
3096 0 : case t_FRAC: return 0;
3097 0 : default: pari_err_TYPE("nfisintegral",x);
3098 : }
3099 : }
3100 8225 : x = Q_remove_denom(x, &d);
3101 8225 : if (!RgX_is_ZX(x)) pari_err_TYPE("nfisintegral",x);
3102 8225 : if (!d) return 1;
3103 672 : x = ZM_ZX_mul(nf_get_invzk(nf), x);
3104 672 : return ZV_Z_dvd(x, d);
3105 : }
3106 : static int
3107 8225 : nfpolisintegral(GEN nf, GEN x)
3108 8225 : { pari_sp av = avma; return gc_int(av, nfpolisintegral_i(nf, x)); }
3109 :
3110 : /* true nf */
3111 : static int
3112 31283 : nfisintegral(GEN nf, GEN x)
3113 : {
3114 31283 : switch(typ(x))
3115 : {
3116 23051 : case t_INT: return 1;
3117 7 : case t_FRAC: return 0;
3118 0 : case t_POLMOD:
3119 0 : x = checknfelt_mod(nf,x,"nfisintegral");
3120 0 : switch(typ(x))
3121 : {
3122 0 : case t_INT: return 1;
3123 0 : case t_FRAC: return 0;
3124 0 : case t_POL: return nfpolisintegral(nf,x);
3125 : }
3126 0 : break;
3127 8225 : case t_POL: return nfpolisintegral(nf,x);
3128 0 : case t_COL:
3129 0 : if (lg(x)-1 != nf_get_degree(nf)) break;
3130 0 : return RgV_is_ZV(x);
3131 : }
3132 0 : pari_err_TYPE("nfisintegral",x);
3133 : return 0; /* LCOV_EXCL_LINE */
3134 : }
3135 : /* true nf */
3136 : static int
3137 7077 : nfXisintegral(GEN nf, GEN x)
3138 : {
3139 7077 : long i, l = lg(x);
3140 38346 : for (i = 2; i < l; i++)
3141 31283 : if (!nfisintegral(nf, gel(x,i))) return 0;
3142 7063 : return 1;
3143 : }
3144 :
3145 : /* Relative Dedekind criterion over (true) nf, applied to the order defined by a
3146 : * root of monic irreducible polynomial P, modulo the prime ideal pr. Assume
3147 : * vdisc = v_pr( disc(P) ).
3148 : * Return NULL if nf[X]/P is pr-maximal. Otherwise, return [flag, O, v]:
3149 : * O = enlarged order, given by a pseudo-basis
3150 : * flag = 1 if O is proven pr-maximal (may be 0 and O nevertheless pr-maximal)
3151 : * v = v_pr(disc(O)). */
3152 : static GEN
3153 4151 : rnfdedekind_i(GEN nf, GEN P, GEN pr, long vdisc, long only_maximal)
3154 : {
3155 : GEN Ppr, A, I, p, tau, g, h, k, base, T, gzk, hzk, prinvp, pal, nfT, modpr;
3156 : long m, vt, r, d, i, j, mpr;
3157 :
3158 4151 : if (vdisc < 0) pari_err_TYPE("rnfdedekind [non integral pol]", P);
3159 4144 : if (vdisc == 1) return NULL; /* pr-maximal */
3160 4144 : if (!only_maximal && !gequal1(leading_coeff(P)))
3161 0 : pari_err_IMPL( "the full Dedekind criterion in the nonmonic case");
3162 4144 : if (!nfXisintegral(nf, P))
3163 0 : pari_err_IMPL("non integral polynomial in rnfdedekind");
3164 : /* either monic OR only_maximal = 1 */
3165 4144 : m = degpol(P);
3166 4144 : nfT = nf_get_pol(nf);
3167 4144 : modpr = nf_to_Fq_init(nf,&pr, &T, &p);
3168 4144 : Ppr = nfX_to_FqX(P, nf, modpr);
3169 4144 : mpr = degpol(Ppr);
3170 4144 : if (mpr < m) /* nonmonic => only_maximal = 1 */
3171 : {
3172 21 : if (mpr < 0) return NULL;
3173 21 : if (! RgX_valrem(Ppr, &Ppr))
3174 : { /* nonzero constant coefficient */
3175 0 : Ppr = RgX_shift_shallow(RgX_recip_i(Ppr), m - mpr);
3176 0 : P = RgX_recip_i(P);
3177 : }
3178 : else
3179 : {
3180 21 : GEN z = FqX_non_root(Ppr, T, p);
3181 21 : if (!z) pari_err_IMPL( "Dedekind in the difficult case");
3182 0 : z = Fq_to_nf(z, modpr);
3183 0 : if (typ(z) == t_INT)
3184 0 : P = RgX_translate(P, z);
3185 : else
3186 0 : P = RgXQX_translate(P, z, T);
3187 0 : P = RgX_recip_i(P);
3188 0 : Ppr = nfX_to_FqX(P, nf, modpr); /* degpol(P) = degpol(Ppr) = m */
3189 : }
3190 : }
3191 4123 : A = gel(FqX_factor(Ppr,T,p),1);
3192 4123 : r = lg(A); /* > 1 */
3193 4123 : g = gel(A,1);
3194 7245 : for (i=2; i<r; i++) g = FqX_mul(g, gel(A,i), T, p);
3195 4123 : h = FqX_div(Ppr,g, T, p);
3196 4123 : gzk = FqX_to_nfX(g, modpr);
3197 4123 : hzk = FqX_to_nfX(h, modpr);
3198 4123 : k = nfX_sub(nf, P, nfX_mul(nf, gzk,hzk));
3199 4123 : tau = pr_get_tau(pr);
3200 4123 : switch(typ(tau))
3201 : {
3202 2086 : case t_INT: k = gdiv(k, p); break;
3203 2037 : case t_MAT: k = RgX_Rg_div(tablemulvec(NULL,tau, k), p); break;
3204 : }
3205 4123 : k = nfX_to_FqX(k, nf, modpr);
3206 4123 : k = FqX_normalize(FqX_gcd(FqX_gcd(g,h, T,p), k, T,p), T,p);
3207 4123 : d = degpol(k); /* <= m */
3208 4123 : if (!d) return NULL; /* pr-maximal */
3209 1952 : if (only_maximal) return gen_0; /* not maximal */
3210 :
3211 1931 : A = cgetg(m+d+1,t_MAT);
3212 1931 : I = cgetg(m+d+1,t_VEC); base = mkvec2(A, I);
3213 : /* base[2] temporarily multiplied by p, for the final nfhnfmod,
3214 : * which requires integral ideals */
3215 1931 : prinvp = pr_inv_p(pr); /* again multiplied by p */
3216 10427 : for (j=1; j<=m; j++)
3217 : {
3218 8496 : gel(A,j) = col_ei(m, j);
3219 8496 : gel(I,j) = p;
3220 : }
3221 1931 : pal = FqX_to_nfX(FqX_div(Ppr,k, T,p), modpr);
3222 4198 : for ( ; j<=m+d; j++)
3223 : {
3224 2267 : gel(A,j) = RgX_to_RgC(pal,m);
3225 2267 : gel(I,j) = prinvp;
3226 2267 : if (j < m+d) pal = RgXQX_rem(RgX_shift_shallow(pal,1),P,nfT);
3227 : }
3228 : /* the modulus is integral */
3229 1931 : base = nfhnfmod(nf,base, idealmulpowprime(nf, powiu(p,m), pr, utoineg(d)));
3230 1931 : gel(base,2) = gdiv(gel(base,2), p); /* cancel the factor p */
3231 1931 : vt = vdisc - 2*d;
3232 1931 : return mkvec3(vt < 2? gen_1: gen_0, base, stoi(vt));
3233 : }
3234 :
3235 : /* [L:K] = n */
3236 : static GEN
3237 1506 : triv_order(long n)
3238 : {
3239 1506 : GEN z = cgetg(3, t_VEC);
3240 1506 : gel(z,1) = matid(n);
3241 1506 : gel(z,2) = const_vec(n, gen_1); return z;
3242 : }
3243 :
3244 : /* if flag is set, return gen_1 (resp. gen_0) if the order K[X]/(P)
3245 : * is pr-maximal (resp. not pr-maximal). */
3246 : GEN
3247 91 : rnfdedekind(GEN nf, GEN P, GEN pr, long flag)
3248 : {
3249 91 : pari_sp av = avma;
3250 : GEN z, dP;
3251 : long v;
3252 :
3253 91 : nf = checknf(nf);
3254 91 : P = RgX_nffix("rnfdedekind", nf_get_pol(nf), P, 1);
3255 91 : dP = nfX_disc(nf, P);
3256 91 : if (gequal0(dP))
3257 7 : pari_err_DOMAIN("rnfdedekind","issquarefree(pol)","=",gen_0,P);
3258 84 : if (!pr)
3259 : {
3260 21 : GEN fa = idealfactor(nf, dP);
3261 21 : GEN Q = gel(fa,1), E = gel(fa,2);
3262 21 : pari_sp av2 = avma;
3263 21 : long i, l = lg(Q);
3264 21 : for (i = 1; i < l; i++, set_avma(av2))
3265 : {
3266 21 : v = itos(gel(E,i));
3267 21 : if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { set_avma(av); return gen_0; }
3268 0 : set_avma(av2);
3269 : }
3270 0 : set_avma(av); return gen_1;
3271 : }
3272 63 : else if (typ(pr) == t_VEC)
3273 : { /* flag = 1 is implicit */
3274 63 : if (lg(pr) == 1) { set_avma(av); return gen_1; }
3275 63 : if (typ(gel(pr,1)) == t_VEC)
3276 : { /* list of primes */
3277 14 : GEN Q = pr;
3278 14 : pari_sp av2 = avma;
3279 14 : long i, l = lg(Q);
3280 14 : for (i = 1; i < l; i++, set_avma(av2))
3281 : {
3282 14 : v = nfval(nf, dP, gel(Q,i));
3283 14 : if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { set_avma(av); return gen_0; }
3284 : }
3285 0 : set_avma(av); return gen_1;
3286 : }
3287 : }
3288 : /* single prime */
3289 49 : v = nfval(nf, dP, pr);
3290 49 : z = rnfdedekind_i(nf, P, pr, v, flag);
3291 42 : if (z)
3292 : {
3293 21 : if (flag) { set_avma(av); return gen_0; }
3294 14 : z = gc_GEN(av, z);
3295 : }
3296 : else
3297 : {
3298 21 : set_avma(av); if (flag) return gen_1;
3299 7 : z = cgetg(4, t_VEC);
3300 7 : gel(z,1) = gen_1;
3301 7 : gel(z,2) = triv_order(degpol(P));
3302 7 : gel(z,3) = stoi(v);
3303 : }
3304 21 : return z;
3305 : }
3306 :
3307 : static int
3308 8049 : ideal_is1(GEN x) {
3309 8049 : switch(typ(x))
3310 : {
3311 4119 : case t_INT: return is_pm1(x);
3312 3391 : case t_MAT: return RgM_isidentity(x);
3313 : }
3314 539 : return 0;
3315 : }
3316 :
3317 : /* return a in ideal A such that v_pr(a) = v_pr(A) */
3318 : static GEN
3319 3776 : minval(GEN nf, GEN A, GEN pr)
3320 : {
3321 3776 : GEN ab = idealtwoelt(nf,A), a = gel(ab,1), b = gel(ab,2);
3322 3776 : if (nfval(nf,a,pr) > nfval(nf,b,pr)) a = b;
3323 3776 : return a;
3324 : }
3325 :
3326 : /* nf a true nf. Return NULL if power order is pr-maximal */
3327 : static GEN
3328 4067 : rnfmaxord(GEN nf, GEN pol, GEN pr, long vdisc)
3329 : {
3330 4067 : pari_sp av = avma, av1;
3331 : long i, j, k, n, nn, vpol, cnt, sep;
3332 : GEN q, q1, p, T, modpr, W, I, p1;
3333 : GEN prhinv, mpi, Id;
3334 :
3335 4067 : if (DEBUGLEVEL>1) err_printf(" treating %Ps^%ld\n", pr, vdisc);
3336 4067 : modpr = nf_to_Fq_init(nf,&pr,&T,&p);
3337 4067 : av1 = avma;
3338 4067 : p1 = rnfdedekind_i(nf, pol, modpr, vdisc, 0);
3339 4067 : if (!p1) return gc_NULL(av);
3340 1917 : if (is_pm1(gel(p1,1))) return gc_GEN(av,gel(p1,2));
3341 832 : sep = itos(gel(p1,3));
3342 832 : W = gmael(p1,2,1);
3343 832 : I = gmael(p1,2,2);
3344 832 : (void)gc_all(av1, 2, &W, &I);
3345 :
3346 832 : mpi = zk_multable(nf, pr_get_gen(pr));
3347 832 : n = degpol(pol); nn = n*n;
3348 832 : vpol = varn(pol);
3349 832 : q1 = q = pr_norm(pr);
3350 1021 : while (abscmpiu(q1,n) < 0) q1 = mulii(q1,q);
3351 832 : Id = matid(n);
3352 832 : prhinv = pr_inv(pr);
3353 832 : av1 = avma;
3354 832 : for(cnt=1;; cnt++)
3355 1040 : {
3356 1872 : GEN I0 = leafcopy(I), W0 = leafcopy(W);
3357 : GEN Wa, Winv, Ip, A, MW, MWmod, F, pseudo, C, G;
3358 1872 : GEN Tauinv = cgetg(n+1, t_VEC), Tau = cgetg(n+1, t_VEC);
3359 :
3360 1872 : if (DEBUGLEVEL>1) err_printf(" pass no %ld\n",cnt);
3361 9578 : for (j=1; j<=n; j++)
3362 : {
3363 : GEN tau, tauinv;
3364 7706 : if (ideal_is1(gel(I,j)))
3365 : {
3366 3930 : gel(I,j) = gel(Tau,j) = gel(Tauinv,j) = gen_1;
3367 3930 : continue;
3368 : }
3369 3776 : gel(Tau,j) = tau = minval(nf, gel(I,j), pr);
3370 3776 : gel(Tauinv,j) = tauinv = nfinv(nf, tau);
3371 3776 : gel(W,j) = nfC_nf_mul(nf, gel(W,j), tau);
3372 3776 : gel(I,j) = idealmul(nf, tauinv, gel(I,j)); /* v_pr(I[j]) = 0 */
3373 : }
3374 : /* W = (Z_K/pr)-basis of O/pr. O = (W0,I0) ~ (W, I) */
3375 :
3376 : /* compute MW: W_i*W_j = sum MW_k,(i,j) W_k */
3377 1872 : Wa = RgM_to_RgXV(W,vpol);
3378 1872 : Winv = nfM_inv(nf, W);
3379 1872 : MW = cgetg(nn+1, t_MAT);
3380 : /* W_1 = 1 */
3381 9578 : for (j=1; j<=n; j++) gel(MW, j) = gel(MW, (j-1)*n+1) = gel(Id,j);
3382 7706 : for (i=2; i<=n; i++)
3383 22357 : for (j=i; j<=n; j++)
3384 : {
3385 16523 : GEN z = nfXQ_mul(nf, gel(Wa,i), gel(Wa,j), pol);
3386 16523 : if (typ(z) != t_POL)
3387 0 : z = nfC_nf_mul(nf, gel(Winv,1), z);
3388 : else
3389 : {
3390 16523 : z = RgX_to_RgC(z, lg(Winv)-1);
3391 16523 : z = nfM_nfC_mul(nf, Winv, z);
3392 : }
3393 16523 : gel(MW, (i-1)*n+j) = gel(MW, (j-1)*n+i) = z;
3394 : }
3395 :
3396 : /* compute Ip = pr-radical [ could use Ker(trace) if q large ] */
3397 1872 : MWmod = nfM_to_FqM(MW,nf,modpr);
3398 1872 : F = cgetg(n+1, t_MAT); gel(F,1) = gel(Id,1);
3399 7706 : for (j=2; j<=n; j++) gel(F,j) = rnfeltid_powmod(MWmod, j, q1, T,p);
3400 1872 : Ip = FqM_ker(F,T,p);
3401 1872 : if (lg(Ip) == 1) { W = W0; I = I0; break; }
3402 :
3403 : /* Fill C: W_k A_j = sum_i C_(i,j),k A_i */
3404 1467 : A = FqM_to_nfM(FqM_suppl(Ip,T,p), modpr);
3405 3795 : for (j = lg(Ip); j<=n; j++) gel(A,j) = nfC_multable_mul(gel(A,j), mpi);
3406 1467 : MW = nfM_mul(nf, nfM_inv(nf,A), MW);
3407 1467 : C = cgetg(n+1, t_MAT);
3408 7481 : for (k=1; k<=n; k++)
3409 : {
3410 6014 : GEN mek = vecslice(MW, (k-1)*n+1, k*n), Ck;
3411 6014 : gel(C,k) = Ck = cgetg(nn+1, t_COL);
3412 38020 : for (j = 1; j <= n; j++)
3413 : {
3414 32006 : GEN z = nfM_nfC_mul(nf, mek, gel(A,j));
3415 241576 : for (i = 1; i <= n; i++)
3416 209570 : gel(Ck, (j-1)*n+i) = gc_nf_to_Fq(nf,gel(z,i),modpr);
3417 : }
3418 : }
3419 1467 : G = FqM_to_nfM(FqM_ker(C,T,p), modpr);
3420 :
3421 1467 : pseudo = rnfjoinmodules_i(nf, G,prhinv, Id,I);
3422 : /* express W in terms of the power basis */
3423 1467 : W = nfM_mul(nf, W, gel(pseudo,1));
3424 1467 : I = gel(pseudo,2);
3425 : /* restore the HNF property W[i,i] = 1. NB: W upper triangular, with
3426 : * W[i,i] = Tau[i] */
3427 7481 : for (j=1; j<=n; j++)
3428 6014 : if (gel(Tau,j) != gen_1)
3429 : {
3430 2699 : gel(W,j) = nfC_nf_mul(nf, gel(W,j), gel(Tauinv,j));
3431 2699 : gel(I,j) = idealmul(nf, gel(Tau,j), gel(I,j));
3432 : }
3433 1467 : if (DEBUGLEVEL>3) err_printf(" new order:\n%Ps\n%Ps\n", W, I);
3434 1467 : if (sep <= 3 || gequal(I,I0)) break;
3435 :
3436 1040 : if (gc_needed(av1,2))
3437 : {
3438 0 : if(DEBUGMEM>1) pari_warn(warnmem,"rnfmaxord");
3439 0 : (void)gc_all(av1,2, &W,&I);
3440 : }
3441 : }
3442 832 : return gc_GEN(av, mkvec2(W, I));
3443 : }
3444 :
3445 : GEN
3446 945821 : Rg_nffix(const char *f, GEN T, GEN c, int lift)
3447 : {
3448 945821 : switch(typ(c))
3449 : {
3450 517001 : case t_INT: case t_FRAC: return c;
3451 68788 : case t_POL:
3452 68788 : if (lg(c) >= lg(T)) c = RgX_rem(c,T);
3453 68788 : break;
3454 360025 : case t_POLMOD:
3455 360025 : if (!RgX_equal_var(gel(c,1), T)) pari_err_MODULUS(f, gel(c,1),T);
3456 359444 : c = gel(c,2);
3457 359444 : switch(typ(c))
3458 : {
3459 313487 : case t_POL: break;
3460 45957 : case t_INT: case t_FRAC: return c;
3461 0 : default: pari_err_TYPE(f, c);
3462 : }
3463 313487 : break;
3464 7 : default: pari_err_TYPE(f,c);
3465 : }
3466 : /* typ(c) = t_POL */
3467 382275 : if (varn(c) != varn(T)) pari_err_VAR(f, c,T);
3468 382261 : switch(lg(c))
3469 : {
3470 13395 : case 2: return gen_0;
3471 30022 : case 3:
3472 30022 : c = gel(c,2); if (is_rational_t(typ(c))) return c;
3473 0 : pari_err_TYPE(f,c);
3474 : }
3475 338844 : RgX_check_QX(c, f);
3476 338823 : return lift? c: mkpolmod(c, T);
3477 : }
3478 : /* check whether x is a polynomials with coeffs in number field Q[y]/(T)
3479 : * and returned a normalized copy. If 'lift' is set return lifted coefs
3480 : * (t_POL/t_FRAC/t_INT) else t_POLMOD/t_FRAC/t_INT */
3481 : GEN
3482 323005 : RgX_nffix(const char *f, GEN T, GEN x, int lift)
3483 : {
3484 323005 : long vT = varn(T);
3485 323005 : if (typ(x) != t_POL) pari_err_TYPE(stack_strcat(f," [t_POL expected]"), x);
3486 323005 : if (varncmp(varn(x), vT) >= 0) pari_err_PRIORITY(f, x, ">=", vT);
3487 1202887 : pari_APPLY_pol_normalized(Rg_nffix(f, T, gel(x,i), lift));
3488 : }
3489 : GEN
3490 49 : RgV_nffix(const char *f, GEN T, GEN x, int lift)
3491 119 : { pari_APPLY_same(Rg_nffix(f, T, gel(x,i), lift)); }
3492 :
3493 : static GEN
3494 2989 : get_d(GEN nf, GEN d)
3495 : {
3496 2989 : GEN b = idealredmodpower(nf, d, 2, 100000);
3497 2989 : return nfmul(nf, d, nfsqr(nf,b));
3498 : }
3499 :
3500 : /* true nf */
3501 : static GEN
3502 4263 : pr_factorback(GEN nf, GEN fa)
3503 : {
3504 4263 : GEN P = gel(fa,1), E = gel(fa,2), z = gen_1;
3505 4263 : long i, l = lg(P);
3506 8322 : for (i = 1; i < l; i++) z = idealmulpowprime(nf, z, gel(P,i), gel(E,i));
3507 4263 : return z;
3508 : }
3509 : /* true nf */
3510 : static GEN
3511 4263 : pr_factorback_scal(GEN nf, GEN fa)
3512 : {
3513 4263 : GEN D = pr_factorback(nf,fa);
3514 4263 : if (typ(D) == t_MAT && RgM_isscalar(D,NULL)) D = gcoeff(D,1,1);
3515 4263 : return D;
3516 : }
3517 :
3518 : /* nf = base field K
3519 : * pol= monic polynomial in Z_K[X] defining a relative extension L = K[X]/(pol).
3520 : * Returns a pseudo-basis [A,I] of Z_L, set *pD to [D,d] and *pf to the
3521 : * index-ideal; rnf is used when lim != 0 and may be NULL */
3522 : GEN
3523 2940 : rnfallbase(GEN nf, GEN pol, GEN lim, GEN rnf, GEN *pD, GEN *pf, GEN *pDKP)
3524 : {
3525 : long i, j, jf, l;
3526 : GEN fa, E, P, Ef, Pf, z, disc;
3527 :
3528 2940 : nf = checknf(nf); pol = liftpol_shallow(pol);
3529 2940 : if (!gequal1(leading_coeff(pol)))
3530 7 : pari_err_IMPL("nonmonic relative polynomials in rnfallbase");
3531 2933 : if (!nfXisintegral(nf, pol))
3532 14 : pari_err_IMPL("non integral polynomial in rnfallbase");
3533 2919 : disc = nf_to_scalar_or_basis(nf, nfX_disc(nf, pol));
3534 2919 : if (gequal0(disc))
3535 7 : pari_err_DOMAIN("rnfpseudobasis","issquarefree(pol)","=",gen_0, pol);
3536 2912 : if (lim)
3537 : {
3538 : GEN rnfeq, zknf, dzknf, U, vU, dA, A, MB, dB, BdB, vj, B, Tabs;
3539 791 : GEN D = idealhnf_shallow(nf, disc), extendP = NULL;
3540 791 : long rU, m = nf_get_degree(nf), n = degpol(pol), N = n*m;
3541 : nfmaxord_t S;
3542 :
3543 791 : if (typ(lim) == t_INT)
3544 133 : P = ZV_union_shallow(nf_get_ramified_primes(nf),
3545 133 : gel(Z_factor_limit(gcoeff(D,1,1), itou(lim)), 1));
3546 : else
3547 : {
3548 658 : P = cgetg_copy(lim, &l);
3549 2219 : for (i = 1; i < l; i++)
3550 : {
3551 1561 : GEN p = gel(lim,i);
3552 1561 : if (typ(p) != t_INT) p = pr_get_p(p);
3553 1561 : gel(P,i) = p;
3554 : }
3555 658 : P = ZV_sort_uniq_shallow(P);
3556 : }
3557 791 : if (rnf)
3558 : {
3559 742 : rnfeq = rnf_get_map(rnf);
3560 742 : zknf = rnf_get_nfzk(rnf);
3561 : }
3562 : else
3563 : {
3564 49 : rnfeq = nf_rnfeq(nf, pol);
3565 49 : zknf = nf_nfzk(nf, rnfeq);
3566 : }
3567 791 : dzknf = gel(zknf,1);
3568 791 : if (gequal1(dzknf)) dzknf = NULL;
3569 658 : RESTART:
3570 812 : if (extendP)
3571 : {
3572 21 : GEN oldP = P;
3573 21 : if (typ(extendP)==t_POL)
3574 : {
3575 0 : long l = lg(extendP);
3576 0 : for (i = 2; i < l; i++)
3577 : {
3578 0 : GEN q = gel(extendP,i);
3579 0 : if (typ(q) == t_FRAC) P = ZV_cba_extend(P, gel(q,2));
3580 : }
3581 : } else /*t_FRAC*/
3582 21 : P = ZV_cba_extend(P, gel(extendP,2));
3583 21 : if (ZV_equal(P, oldP))
3584 0 : pari_err(e_MISC, "rnfpseudobasis fails, try increasing B");
3585 21 : extendP = NULL;
3586 : }
3587 812 : Tabs = gel(rnfeq,1);
3588 812 : nfmaxord(&S, mkvec2(Tabs,P), 0);
3589 812 : B = RgXV_unscale(S.basis, S.unscale);
3590 812 : BdB = Q_remove_denom(B, &dB);
3591 812 : MB = RgXV_to_RgM(BdB, N); /* HNF */
3592 :
3593 812 : vU = cgetg(N+1, t_VEC);
3594 812 : vj = cgetg(N+1, t_VECSMALL);
3595 812 : gel(vU,1) = U = cgetg(m+1, t_MAT);
3596 812 : gel(U,1) = col_ei(N, 1);
3597 812 : A = dB? (dzknf? gdiv(dB,dzknf): dB): NULL;
3598 812 : if (A)
3599 : {
3600 763 : if (typ(A) != t_INT) { extendP = A; goto RESTART; }
3601 742 : if (equali1(A)) A = NULL;
3602 : }
3603 1617 : for (j = 2; j <= m; j++)
3604 : {
3605 826 : GEN t = gel(zknf,j);
3606 826 : if (!RgX_is_ZX(t)) { extendP = t; goto RESTART; }
3607 826 : if (A) t = ZX_Z_mul(t, A);
3608 826 : gel(U,j) = hnf_solve(MB, RgX_to_RgC(t, N));
3609 : }
3610 5425 : for (i = 2; i <= N; i++)
3611 : {
3612 4634 : GEN b = gel(BdB,i);
3613 4634 : gel(vU,i) = U = cgetg(m+1, t_MAT);
3614 4634 : gel(U,1) = hnf_solve(MB, RgX_to_RgC(b, N));
3615 10164 : for (j = 2; j <= m; j++)
3616 : {
3617 5530 : GEN t = ZX_rem(ZX_mul(b, gel(zknf,j)), Tabs);
3618 5530 : if (dzknf)
3619 : {
3620 4914 : t = RgX_Rg_div(t, dzknf);
3621 4914 : if (!RgX_is_ZX(t)) { extendP = t; goto RESTART; }
3622 : }
3623 5530 : gel(U,j) = hnf_solve(MB, RgX_to_RgC(t, N));
3624 : }
3625 : }
3626 791 : vj[1] = 1; U = gel(vU,1); rU = m;
3627 1932 : for (i = j = 2; i <= N; i++)
3628 : {
3629 1925 : GEN V = shallowconcat(U, gel(vU,i));
3630 1925 : if (ZM_rank(V) != rU)
3631 : {
3632 1925 : U = V; rU += m; vj[j++] = i;
3633 1925 : if (rU == N) break;
3634 : }
3635 : }
3636 791 : if (dB) for(;;)
3637 1092 : {
3638 1834 : GEN c = gen_1, H = ZM_hnfmodid(U, dB);
3639 1834 : long ic = 0;
3640 17717 : for (i = 1; i <= N; i++)
3641 15883 : if (cmpii(gcoeff(H,i,i), c) > 0) { c = gcoeff(H,i,i); ic = i; }
3642 1834 : if (!ic) break;
3643 1092 : vj[j++] = ic;
3644 1092 : U = shallowconcat(H, gel(vU, ic));
3645 : }
3646 791 : setlg(vj, j);
3647 791 : B = vecpermute(B, vj);
3648 :
3649 791 : l = lg(B);
3650 791 : A = cgetg(l,t_MAT);
3651 4599 : for (j = 1; j < l; j++)
3652 : {
3653 3808 : GEN t = eltabstorel_lift(rnfeq, gel(B,j));
3654 3808 : gel(A,j) = Rg_to_RgC(t, n);
3655 : }
3656 791 : A = RgM_to_nfM(nf, A);
3657 791 : A = Q_remove_denom(A, &dA);
3658 791 : if (!dA)
3659 : { /* order is maximal */
3660 63 : z = triv_order(n);
3661 63 : if (pf) *pf = gen_1;
3662 : }
3663 : else
3664 : {
3665 : GEN fi;
3666 : /* the first n columns of A are probably in HNF already */
3667 728 : A = shallowconcat(vecslice(A,n+1,lg(A)-1), vecslice(A,1,n));
3668 728 : A = mkvec2(A, const_vec(l-1,gen_1));
3669 728 : if (DEBUGLEVEL > 2) err_printf("rnfallbase: nfhnf in dim %ld\n", l-1);
3670 728 : z = nfhnfmod(nf, A, nfdetint(nf,A));
3671 728 : gel(z,2) = gdiv(gel(z,2), dA);
3672 728 : fi = idealprod(nf,gel(z,2));
3673 728 : D = idealmul(nf, D, idealsqr(nf, fi));
3674 728 : if (pf) *pf = idealinv(nf, fi);
3675 : }
3676 791 : if (RgM_isscalar(D,NULL)) D = gcoeff(D,1,1);
3677 791 : if (pDKP) *pDKP = S.dKP;
3678 791 : *pD = mkvec2(D, get_d(nf, disc)); return z;
3679 : }
3680 2121 : fa = idealfactor(nf, disc);
3681 2121 : P = gel(fa,1); l = lg(P); z = NULL;
3682 2121 : E = gel(fa,2);
3683 2121 : Pf = cgetg(l, t_COL);
3684 2121 : Ef = cgetg(l, t_COL);
3685 5970 : for (i = j = jf = 1; i < l; i++)
3686 : {
3687 3849 : GEN pr = gel(P,i);
3688 3849 : long e = itos(gel(E,i));
3689 3849 : if (e > 1)
3690 : {
3691 2856 : GEN vD = rnfmaxord(nf, pol, pr, e);
3692 2856 : if (vD)
3693 : {
3694 1329 : long ef = idealprodval(nf, gel(vD,2), pr);
3695 1329 : z = rnfjoinmodules(nf, z, vD);
3696 1329 : if (ef) { gel(Pf, jf) = pr; gel(Ef, jf++) = stoi(-ef); }
3697 1329 : e += 2 * ef;
3698 : }
3699 : }
3700 3849 : if (e) { gel(P, j) = pr; gel(E, j++) = stoi(e); }
3701 : }
3702 2121 : setlg(P,j);
3703 2121 : setlg(E,j);
3704 2121 : if (pDKP) *pDKP = prV_primes(P);
3705 2121 : if (pf)
3706 : {
3707 2065 : setlg(Pf, jf);
3708 2065 : setlg(Ef, jf); *pf = pr_factorback_scal(nf, mkmat2(Pf,Ef));
3709 : }
3710 2121 : *pD = mkvec2(pr_factorback_scal(nf,fa), get_d(nf, disc));
3711 2121 : return z? z: triv_order(degpol(pol));
3712 : }
3713 :
3714 : static GEN
3715 1666 : RgX_to_algX(GEN nf, GEN x)
3716 10143 : { pari_APPLY_pol_normalized(nf_to_scalar_or_alg(nf, gel(x,i))); }
3717 :
3718 : GEN
3719 1680 : nfX_to_monic(GEN nf, GEN T, GEN *pL)
3720 : {
3721 : GEN lT, g, a;
3722 1680 : long i, l = lg(T);
3723 1680 : if (l == 2) return pol_0(varn(T));
3724 1680 : if (l == 3) return pol_1(varn(T));
3725 1680 : nf = checknf(nf);
3726 1680 : T = Q_primpart(RgX_to_nfX(nf, T));
3727 1680 : lT = leading_coeff(T); if (pL) *pL = lT;
3728 1680 : if (isint1(T)) return T;
3729 1680 : g = cgetg_copy(T, &l); g[1] = T[1]; a = lT;
3730 1680 : gel(g, l-1) = gen_1;
3731 1680 : gel(g, l-2) = gel(T,l-2);
3732 1680 : if (l == 4) { gel(g,l-2) = nf_to_scalar_or_alg(nf, gel(g,l-2)); return g; }
3733 1666 : if (typ(lT) == t_INT)
3734 : {
3735 1652 : gel(g, l-3) = gmul(a, gel(T,l-3));
3736 5124 : for (i = l-4; i > 1; i--) { a = mulii(a,lT); gel(g,i) = gmul(a, gel(T,i)); }
3737 : }
3738 : else
3739 : {
3740 14 : gel(g, l-3) = nfmul(nf, a, gel(T,l-3));
3741 35 : for (i = l-3; i > 1; i--)
3742 : {
3743 21 : a = nfmul(nf,a,lT);
3744 21 : gel(g,i) = nfmul(nf, a, gel(T,i));
3745 : }
3746 : }
3747 1666 : return RgX_to_algX(nf, g);
3748 : }
3749 :
3750 : GEN
3751 868 : rnfdisc_factored(GEN nf, GEN pol, GEN *pd)
3752 : {
3753 : long i, j, l;
3754 : GEN fa, E, P, disc, lim;
3755 :
3756 868 : pol = rnfdisc_get_T(nf, pol, &lim);
3757 868 : disc = nf_to_scalar_or_basis(nf, nfX_disc(nf, pol));
3758 868 : if (gequal0(disc))
3759 0 : pari_err_DOMAIN("rnfdisc","issquarefree(pol)","=",gen_0, pol);
3760 868 : pol = nfX_to_monic(nf, pol, NULL);
3761 868 : fa = idealfactor_partial(nf, disc, lim);
3762 868 : P = gel(fa,1); l = lg(P);
3763 868 : E = gel(fa,2);
3764 2352 : for (i = j = 1; i < l; i++)
3765 : {
3766 1484 : long e = itos(gel(E,i));
3767 1484 : GEN pr = gel(P,i);
3768 1484 : if (e > 1)
3769 : {
3770 1211 : GEN vD = rnfmaxord(nf, pol, pr, e);
3771 1211 : if (vD) e += 2*idealprodval(nf, gel(vD,2), pr);
3772 : }
3773 1484 : if (e) { gel(P, j) = pr; gel(E, j++) = stoi(e); }
3774 : }
3775 868 : if (pd) *pd = get_d(nf, disc);
3776 868 : setlg(P, j);
3777 868 : setlg(E, j); return fa;
3778 : }
3779 : GEN
3780 77 : rnfdiscf(GEN nf, GEN pol)
3781 : {
3782 77 : pari_sp av = avma;
3783 : GEN d, fa;
3784 77 : nf = checknf(nf); fa = rnfdisc_factored(nf, pol, &d);
3785 77 : return gc_GEN(av, mkvec2(pr_factorback_scal(nf,fa), d));
3786 : }
3787 :
3788 : GEN
3789 35 : gen_if_principal(GEN bnf, GEN x)
3790 : {
3791 35 : pari_sp av = avma;
3792 35 : GEN z = bnfisprincipal0(bnf,x, nf_GEN_IF_PRINCIPAL | nf_FORCE);
3793 35 : return isintzero(z)? gc_NULL(av): z;
3794 : }
3795 :
3796 : /* given bnf and a HNF pseudo-basis of a proj. module, simplify the HNF as
3797 : * much as possible. The resulting matrix will be upper triangular but the
3798 : * diagonal coefficients will not be equal to 1. The ideals are integral and
3799 : * primitive. */
3800 : GEN
3801 0 : rnfsimplifybasis(GEN bnf, GEN M)
3802 : {
3803 0 : pari_sp av = avma;
3804 : long i, l;
3805 : GEN y, Az, Iz, nf, A, I;
3806 :
3807 0 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
3808 0 : if (!check_ZKmodule_i(M)) pari_err_TYPE("rnfsimplifybasis",M);
3809 0 : A = gel(M,1);
3810 0 : I = gel(M,2); l = lg(I);
3811 0 : Az = cgetg(l, t_MAT);
3812 0 : Iz = cgetg(l, t_VEC); y = mkvec2(Az, Iz);
3813 0 : for (i = 1; i < l; i++)
3814 : {
3815 : GEN c, d;
3816 0 : if (ideal_is1(gel(I,i)))
3817 : {
3818 0 : gel(Iz,i) = gen_1;
3819 0 : gel(Az,i) = gel(A,i); continue;
3820 : }
3821 :
3822 0 : gel(Iz,i) = Q_primitive_part(gel(I,i), &c);
3823 0 : gel(Az,i) = c? RgC_Rg_mul(gel(A,i),c): gel(A,i);
3824 0 : if (c && ideal_is1(gel(Iz,i))) continue;
3825 :
3826 0 : d = gen_if_principal(bnf, gel(Iz,i));
3827 0 : if (d)
3828 : {
3829 0 : gel(Iz,i) = gen_1;
3830 0 : gel(Az,i) = nfC_nf_mul(nf, gel(Az,i), d);
3831 : }
3832 : }
3833 0 : return gc_GEN(av, y);
3834 : }
3835 :
3836 : static GEN
3837 63 : get_module(GEN nf, GEN O, const char *s)
3838 : {
3839 63 : if (typ(O) == t_POL) return rnfpseudobasis(nf, O);
3840 56 : if (!check_ZKmodule_i(O)) pari_err_TYPE(s, O);
3841 56 : return shallowcopy(O);
3842 : }
3843 :
3844 : GEN
3845 14 : rnfdet(GEN nf, GEN M)
3846 : {
3847 14 : pari_sp av = avma;
3848 : GEN D;
3849 14 : nf = checknf(nf);
3850 14 : M = get_module(nf, M, "rnfdet");
3851 14 : D = idealmul(nf, nfM_det(nf, gel(M,1)), idealprod(nf, gel(M,2)));
3852 14 : return gc_upto(av, D);
3853 : }
3854 :
3855 : /* Given two fractional ideals a and b, gives x in a, y in b, z in b^-1,
3856 : t in a^-1 such that xt-yz=1. In the present version, z is in Z. */
3857 : static void
3858 63 : nfidealdet1(GEN nf, GEN a, GEN b, GEN *px, GEN *py, GEN *pz, GEN *pt)
3859 : {
3860 : GEN x, uv, y, da, db;
3861 :
3862 63 : a = idealinv(nf,a);
3863 63 : a = Q_remove_denom(a, &da);
3864 63 : b = Q_remove_denom(b, &db);
3865 63 : x = idealcoprime(nf,a,b);
3866 63 : uv = idealaddtoone(nf, idealmul(nf,x,a), b);
3867 63 : y = gel(uv,2);
3868 63 : if (da) x = gmul(x,da);
3869 63 : if (db) y = gdiv(y,db);
3870 63 : *px = x;
3871 63 : *py = y;
3872 63 : *pz = db ? negi(db): gen_m1;
3873 63 : *pt = nfdiv(nf, gel(uv,1), x);
3874 63 : }
3875 :
3876 : /* given a pseudo-basis of a proj. module in HNF [A,I] (or [A,I,D,d]), gives
3877 : * an n x n matrix (not HNF) of a pseudo-basis and an ideal vector
3878 : * [1,...,1,I] such that M ~ Z_K^(n-1) x I. Uses the approximation theorem.*/
3879 : GEN
3880 28 : rnfsteinitz(GEN nf, GEN M)
3881 : {
3882 28 : pari_sp av = avma;
3883 : long i, n;
3884 : GEN A, I;
3885 :
3886 28 : nf = checknf(nf);
3887 28 : M = get_module(nf, M, "rnfsteinitz");
3888 28 : A = RgM_to_nfM(nf, gel(M,1));
3889 28 : I = leafcopy(gel(M,2)); n = lg(A)-1;
3890 189 : for (i = 1; i < n; i++)
3891 : {
3892 161 : GEN c1, c2, b, a = gel(I,i);
3893 161 : gel(I,i) = gen_1;
3894 161 : if (ideal_is1(a)) continue;
3895 :
3896 63 : c1 = gel(A,i);
3897 63 : c2 = gel(A,i+1);
3898 63 : b = gel(I,i+1);
3899 63 : if (ideal_is1(b))
3900 : {
3901 0 : gel(A,i) = c2;
3902 0 : gel(A,i+1) = gneg(c1);
3903 0 : gel(I,i+1) = a;
3904 : }
3905 : else
3906 : {
3907 63 : pari_sp av2 = avma;
3908 : GEN x, y, z, t, c;
3909 63 : nfidealdet1(nf,a,b, &x,&y,&z,&t);
3910 63 : x = RgC_add(nfC_nf_mul(nf, c1, x), nfC_nf_mul(nf, c2, y));
3911 63 : y = RgC_add(nfC_nf_mul(nf, c1, z), nfC_nf_mul(nf, c2, t));
3912 63 : (void)gc_all(av2, 2, &x,&y);
3913 63 : gel(A,i) = x;
3914 63 : gel(A,i+1) = y;
3915 63 : gel(I,i+1) = Q_primitive_part(idealmul(nf,a,b), &c);
3916 63 : if (c) gel(A,i+1) = nfC_nf_mul(nf, gel(A,i+1), c);
3917 : }
3918 : }
3919 28 : gel(M,1) = A;
3920 28 : gel(M,2) = I; return gc_GEN(av, M);
3921 : }
3922 :
3923 : /* Given bnf and a proj. module (or a t_POL -> rnfpseudobasis), and outputs a
3924 : * basis if it is free, an n+1-generating set if it is not */
3925 : GEN
3926 21 : rnfbasis(GEN bnf, GEN M)
3927 : {
3928 21 : pari_sp av = avma;
3929 : long j, n;
3930 : GEN nf, A, I, cl, col, a;
3931 :
3932 21 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
3933 21 : M = get_module(nf, M, "rnfbasis");
3934 21 : I = gel(M,2); n = lg(I)-1;
3935 98 : j = 1; while (j < n && ideal_is1(gel(I,j))) j++;
3936 21 : if (j < n) { M = rnfsteinitz(nf,M); I = gel(M,2); }
3937 21 : A = gel(M,1);
3938 21 : col= gel(A,n); A = vecslice(A, 1, n-1);
3939 21 : cl = gel(I,n);
3940 21 : a = gen_if_principal(bnf, cl);
3941 21 : if (!a)
3942 : {
3943 7 : GEN v = idealtwoelt(nf, cl);
3944 7 : A = vec_append(A, gmul(gel(v,1), col));
3945 7 : a = gel(v,2);
3946 : }
3947 21 : A = vec_append(A, nfC_nf_mul(nf, col, a));
3948 21 : return gc_GEN(av, A);
3949 : }
3950 :
3951 : /* Given a Z_K-module M (or a polynomial => rnfpseudobasis) outputs a
3952 : * Z_K-basis in HNF if it exists, zero if not */
3953 : GEN
3954 7 : rnfhnfbasis(GEN bnf, GEN M)
3955 : {
3956 7 : pari_sp av = avma;
3957 : long j, l;
3958 : GEN nf, A, I, a;
3959 :
3960 7 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
3961 7 : if (typ(M) == t_POL) M = rnfpseudobasis(nf, M);
3962 : else
3963 : {
3964 7 : if (typ(M) != t_VEC) pari_err_TYPE("rnfhnfbasis", M);
3965 7 : if (lg(M) == 5) M = mkvec2(gel(M,1), gel(M,2));
3966 7 : M = nfhnf(nf, M); /* in case M is not in HNF */
3967 : }
3968 7 : A = shallowcopy(gel(M,1));
3969 7 : I = gel(M,2); l = lg(A);
3970 42 : for (j = 1; j < l; j++)
3971 : {
3972 35 : if (ideal_is1(gel(I,j))) continue;
3973 14 : a = gen_if_principal(bnf, gel(I,j));
3974 14 : if (!a) return gc_const(av, gen_0);
3975 14 : gel(A,j) = nfC_nf_mul(nf, gel(A,j), a);
3976 : }
3977 7 : return gc_GEN(av,A);
3978 : }
3979 :
3980 : long
3981 7 : rnfisfree(GEN bnf, GEN M)
3982 : {
3983 7 : pari_sp av = avma;
3984 : GEN nf, P, I;
3985 : long l, j;
3986 :
3987 7 : bnf = checkbnf(bnf);
3988 7 : if (is_pm1( bnf_get_no(bnf) )) return 1;
3989 0 : nf = bnf_get_nf(bnf);
3990 0 : M = get_module(nf, M, "rnfisfree");
3991 0 : I = gel(M,2); l = lg(I); P = NULL;
3992 0 : for (j = 1; j < l; j++)
3993 0 : if (!ideal_is1(gel(I,j))) P = P? idealmul(nf, P, gel(I,j)): gel(I,j);
3994 0 : return gc_long(av, P? gequal0( isprincipal(bnf,P) ): 1);
3995 : }
3996 :
3997 : /**********************************************************************/
3998 : /** **/
3999 : /** COMPOSITUM OF TWO NUMBER FIELDS **/
4000 : /** **/
4001 : /**********************************************************************/
4002 : static GEN
4003 26575 : compositum_fix(GEN nf, GEN A)
4004 : {
4005 : int ok;
4006 26575 : if (nf)
4007 : {
4008 980 : A = RgXQX_red(A, nf_get_pol(nf));
4009 980 : A = Q_primpart(liftpol_shallow(A)); RgX_check_ZXX(A,"polcompositum");
4010 980 : ok = nfissquarefree(nf,A);
4011 : }
4012 : else
4013 : {
4014 25595 : A = Q_primpart(A); RgX_check_ZX(A,"polcompositum");
4015 25587 : ok = ZX_is_squarefree(A);
4016 : }
4017 26581 : if (!ok) pari_err_DOMAIN("polcompositum","issquarefree(arg)","=",gen_0,A);
4018 26574 : return A;
4019 : }
4020 : #define next_lambda(a) (a>0 ? -a : 1-a)
4021 :
4022 : static long
4023 511 : nfcompositum_lambda(GEN nf, GEN A, GEN B, long lambda)
4024 : {
4025 511 : pari_sp av = avma;
4026 : forprime_t S;
4027 511 : GEN T = nf_get_pol(nf);
4028 511 : long vT = varn(T);
4029 : ulong p;
4030 511 : init_modular_big(&S);
4031 511 : p = u_forprime_next(&S);
4032 : while (1)
4033 42 : {
4034 : GEN Hp, Tp, a;
4035 553 : if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
4036 553 : a = ZXX_to_FlxX(RgX_rescale(A, stoi(-lambda)), p, vT);
4037 553 : Tp = ZX_to_Flx(T, p);
4038 553 : Hp = FlxqX_composedsum(a, ZXX_to_FlxX(B, p, vT), Tp, p);
4039 553 : if (!FlxqX_is_squarefree(Hp, Tp, p))
4040 42 : { lambda = next_lambda(lambda); continue; }
4041 511 : if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
4042 511 : return gc_long(av, lambda);
4043 : }
4044 : }
4045 :
4046 : /* modular version */
4047 : GEN
4048 13399 : nfcompositum(GEN nf, GEN A, GEN B, long flag)
4049 : {
4050 13399 : pari_sp av = avma;
4051 : int same;
4052 : long v, k;
4053 : GEN C, D, LPRS;
4054 :
4055 13399 : if (typ(A)!=t_POL) pari_err_TYPE("polcompositum",A);
4056 13399 : if (typ(B)!=t_POL) pari_err_TYPE("polcompositum",B);
4057 13399 : if (degpol(A)<=0 || degpol(B)<=0) pari_err_CONSTPOL("polcompositum");
4058 13399 : v = varn(A);
4059 13399 : if (varn(B) != v) pari_err_VAR("polcompositum", A,B);
4060 13399 : if (nf)
4061 : {
4062 553 : nf = checknf(nf);
4063 546 : if (varncmp(v,nf_get_varn(nf))>=0) pari_err_PRIORITY("polcompositum", nf, ">=", v);
4064 : }
4065 13357 : same = (A == B || RgX_equal(A,B));
4066 13357 : A = compositum_fix(nf,A);
4067 13354 : B = same ? A: compositum_fix(nf,B);
4068 :
4069 13355 : D = LPRS = NULL; /* -Wall */
4070 13355 : k = same? -1: 1;
4071 13355 : if (nf)
4072 : {
4073 511 : long v0 = fetch_var();
4074 511 : GEN q, T = nf_get_pol(nf);
4075 511 : A = liftpol_shallow(A);
4076 511 : B = liftpol_shallow(B);
4077 511 : k = nfcompositum_lambda(nf, A, B, k);
4078 511 : if (flag&1)
4079 : {
4080 : GEN H0, H1;
4081 196 : GEN chgvar = deg1pol_shallow(stoi(k),pol_x(v0),v);
4082 196 : GEN B1 = poleval(QXQX_to_mod_shallow(B, T), chgvar);
4083 196 : C = RgX_resultant_all(QXQX_to_mod_shallow(A, T), B1, &q);
4084 196 : C = gsubst(C,v0,pol_x(v));
4085 196 : C = lift_if_rational(C);
4086 196 : H0 = gsubst(gel(q,2),v0,pol_x(v));
4087 196 : H1 = gsubst(gel(q,3),v0,pol_x(v));
4088 196 : if (typ(H0) != t_POL) H0 = scalarpol_shallow(H0,v);
4089 196 : if (typ(H1) != t_POL) H1 = scalarpol_shallow(H1,v);
4090 196 : H0 = lift_if_rational(H0);
4091 196 : H1 = lift_if_rational(H1);
4092 196 : LPRS = mkvec2(H0,H1);
4093 : }
4094 : else
4095 : {
4096 315 : C = nf_direct_compositum(nf, RgX_rescale(A,stoi(-k)), B);
4097 315 : setvarn(C, v); C = QXQX_to_mod_shallow(C, T);
4098 : }
4099 511 : C = RgX_normalize(C);
4100 : }
4101 : else
4102 : {
4103 12844 : B = leafcopy(B); setvarn(B,fetch_var_higher());
4104 3143 : C = (flag&1)? ZX_ZXY_resultant_all(A, B, &k, &LPRS)
4105 12845 : : ZX_compositum(A, B, &k);
4106 12843 : setvarn(C, v);
4107 : }
4108 : /* C = Res_Y (A(Y), B(X + kY)) guaranteed squarefree */
4109 13354 : if (flag & 2)
4110 10253 : C = mkvec(C);
4111 : else
4112 : {
4113 3101 : if (same)
4114 : {
4115 105 : D = RgX_rescale(A, stoi(1 - k));
4116 105 : if (nf) D = RgX_normalize(QXQX_to_mod_shallow(D, nf_get_pol(nf)));
4117 105 : C = RgX_div(C, D);
4118 105 : if (degpol(C) <= 0)
4119 0 : C = mkvec(D);
4120 : else
4121 105 : C = shallowconcat(nf? gel(nffactor(nf,C),1): ZX_DDF(C), D);
4122 : }
4123 : else
4124 2996 : C = nf? gel(nffactor(nf,C),1): ZX_DDF(C);
4125 : }
4126 13352 : gen_sort_inplace(C, (void*)(nf?&cmp_RgX: &cmpii), &gen_cmp_RgX, NULL);
4127 13352 : if (flag&1)
4128 : { /* a,b,c root of A,B,C = compositum, c = b - k a */
4129 3339 : long i, l = lg(C);
4130 3339 : GEN a, b, mH0 = RgX_neg(gel(LPRS,1)), H1 = gel(LPRS,2);
4131 3339 : setvarn(mH0,v);
4132 3339 : setvarn(H1,v);
4133 6755 : for (i=1; i<l; i++)
4134 : {
4135 3416 : GEN D = gel(C,i);
4136 3416 : a = RgXQ_mul(mH0, nf? RgXQ_inv(H1,D): QXQ_inv(H1,D), D);
4137 3416 : b = gadd(pol_x(v), gmulsg(k,a));
4138 3416 : if (degpol(D) == 1) b = RgX_rem(b,D);
4139 3416 : gel(C,i) = mkvec4(D, mkpolmod(a,D), mkpolmod(b,D), stoi(-k));
4140 : }
4141 : }
4142 13352 : (void)delete_var();
4143 13352 : settyp(C, t_VEC);
4144 13352 : if (flag&2) C = gel(C,1);
4145 13352 : return gc_GEN(av, C);
4146 : }
4147 : GEN
4148 12846 : polcompositum0(GEN A, GEN B, long flag)
4149 12846 : { return nfcompositum(NULL,A,B,flag); }
4150 :
4151 : GEN
4152 91 : compositum(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,0); }
4153 : GEN
4154 2877 : compositum2(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,1); }
|