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 12000 : safe_Z_pvalrem(GEN x, GEN p, GEN *z)
27 : {
28 12000 : 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 11972 : 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 3500 : fact_from_factors(GEN D, GEN P, long flag)
39 : {
40 3500 : long i, l = lg(P), iq = 1;
41 3500 : GEN Q = cgetg(l+1,t_COL);
42 3500 : GEN E = cgetg(l+1,t_COL);
43 15493 : for (i=1; i<l; i++)
44 : {
45 12000 : GEN p = gel(P,i);
46 : long k;
47 12000 : if (flag && !equalim1(p))
48 : {
49 14 : p = gcdii(p, D);
50 14 : if (is_pm1(p)) continue;
51 : }
52 12000 : k = safe_Z_pvalrem(D, p, &D);
53 11993 : if (k) { gel(Q,iq) = p; gel(E,iq) = utoipos(k); iq++; }
54 : }
55 3493 : D = absi_shallow(D);
56 3493 : if (!equali1(D))
57 : {
58 616 : long k = Z_isanypower(D, &D);
59 616 : if (!k) k = 1;
60 616 : gel(Q,iq) = D; gel(E,iq) = utoipos(k); iq++;
61 : }
62 3493 : setlg(Q,iq);
63 3493 : 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 2079 : update_fact(GEN d, GEN f)
71 : {
72 : GEN P;
73 2079 : switch (typ(f))
74 : {
75 2065 : 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 811339 : set_disc(nfmaxord_t *S)
90 : {
91 : GEN L, dT;
92 : long d;
93 811339 : if (S->T0 == S->T) return ZX_disc(S->T);
94 248999 : d = degpol(S->T0);
95 249009 : L = S->unscale;
96 249009 : if (typ(L) == t_FRAC && abscmpii(gel(L,1), gel(L,2)) < 0)
97 11860 : dT = ZX_disc(S->T); /* more efficient */
98 : else
99 : {
100 237149 : GEN l0 = leading_coeff(S->T0);
101 237148 : GEN a = gpowgs(gdiv(gpowgs(L, d), sqri(l0)), d-1);
102 237153 : dT = gmul(a, ZX_disc(S->T0)); /* more efficient */
103 : }
104 248991 : return S->dT = dT;
105 : }
106 :
107 : /* dT != 0 */
108 : static GEN
109 787710 : poldiscfactors_i(GEN T, GEN dT, long flag)
110 : {
111 : GEN U, fa, Z, E, P, Tp;
112 : long i, l;
113 :
114 787710 : fa = absZ_factor_limit_strict(dT, minuu(tridiv_bound(dT), maxprime()), &U);
115 787732 : if (!U) return fa;
116 768 : Z = mkcol(gel(U,1)); P = gel(fa,1); Tp = NULL;
117 1648 : while (lg(Z) != 1)
118 : { /* pop and handle last element of Z */
119 880 : GEN p = veclast(Z), r;
120 880 : setlg(Z, lg(Z)-1);
121 880 : if (!Tp) /* first time: p is composite and not a power */
122 768 : Tp = ZX_deriv(T);
123 : else
124 : {
125 112 : (void)Z_isanypower(p, &p);
126 112 : if ((flag || lgefint(p)==3) && BPSW_psp(p))
127 89 : { P = vec_append(P, p); continue; }
128 : }
129 791 : r = FpX_gcd_check(T, Tp, p);
130 791 : if (r)
131 56 : Z = shallowconcat(Z, Z_cba(r, diviiexact(p,r)));
132 735 : else if (flag)
133 7 : P = shallowconcat(P, gel(Z_factor(p),1));
134 : else
135 728 : P = vec_append(P, p);
136 : }
137 768 : ZV_sort_inplace(P); l = lg(P); E = cgetg(l, t_COL);
138 7076 : for (i = 1; i < l; i++) gel(E,i) = utoipos(Z_pvalrem(dT, gel(P,i), &dT));
139 768 : 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 gerepilecopy(av, mkvec2(dT, poldiscfactors_i(T, dT, flag)));
152 : }
153 :
154 : static void
155 811363 : nfmaxord_check_args(nfmaxord_t *S, GEN T, long flag)
156 : {
157 811363 : GEN dT, L, E, P, fa = NULL;
158 : pari_timer t;
159 811363 : long l, ty = typ(T);
160 :
161 811363 : if (DEBUGLEVEL) timer_start(&t);
162 811363 : if (ty == t_VEC) {
163 23642 : if (lg(T) != 3) pari_err_TYPE("nfmaxord",T);
164 23642 : fa = gel(T,2); T = gel(T,1); ty = typ(T);
165 : }
166 811363 : if (ty != t_POL) pari_err_TYPE("nfmaxord",T);
167 811363 : T = Q_primpart(T);
168 811326 : if (degpol(T) <= 0) pari_err_CONSTPOL("nfmaxord");
169 811322 : RgX_check_ZX(T, "nfmaxord");
170 811324 : S->T0 = T;
171 811324 : S->T = T = ZX_Q_normalize(T, &L);
172 811341 : S->unscale = L;
173 811341 : S->dT = dT = set_disc(S);
174 811303 : S->certify = 1;
175 811303 : if (!signe(dT)) pari_err_IRREDPOL("nfmaxord",T);
176 811309 : if (fa)
177 : {
178 23642 : const long MIN = 100; /* include at least all p < 101 */
179 23642 : GEN P0 = NULL, U;
180 23642 : S->certify = 0;
181 23642 : if (!isint1(L)) fa = update_fact(dT, fa);
182 23635 : switch(typ(fa))
183 : {
184 238 : case t_MAT:
185 238 : if (!is_Z_factornon0(fa)) pari_err_TYPE("nfmaxord",fa);
186 231 : P0 = gel(fa,1); /* fall through */
187 3493 : case t_VEC: case t_COL:
188 3493 : if (!P0)
189 : {
190 3262 : if (!RgV_is_ZV(fa)) pari_err_TYPE("nfmaxord",fa);
191 3262 : P0 = fa;
192 : }
193 3493 : P = gel(absZ_factor_limit_strict(dT, MIN, &U), 1);
194 3493 : if (lg(P) != 0) { settyp(P, typ(P0)); P0 = shallowconcat(P0,P); }
195 3493 : P0 = ZV_sort_uniq_shallow(P0);
196 3493 : fa = fact_from_factors(dT, P0, 0);
197 3486 : break;
198 20128 : case t_INT:
199 20128 : fa = absZ_factor_limit(dT, (signe(fa) <= 0)? 1: maxuu(itou(fa), MIN));
200 20128 : break;
201 7 : default:
202 7 : pari_err_TYPE("nfmaxord",fa);
203 : }
204 : }
205 : else
206 : {
207 787667 : S->certify = !(flag & nf_PARTIALFACT);
208 787667 : fa = poldiscfactors_i(T, dT, 0);
209 : }
210 811308 : P = gel(fa,1); l = lg(P);
211 811308 : E = gel(fa,2);
212 811308 : 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 811313 : S->dTP = P;
219 811313 : S->dTE = vec_to_vecsmall(E);
220 811334 : if (DEBUGLEVEL>2) timer_printf(&t, "disc. factorisation");
221 811334 : }
222 :
223 : static int
224 214383 : fnz(GEN x,long j)
225 : {
226 : long i;
227 693423 : for (i=1; i<j; i++)
228 528078 : if (signe(gel(x,i))) return 0;
229 165345 : return 1;
230 : }
231 : /* return list u[i], 2 by 2 coprime with the same prime divisors as ab */
232 : static GEN
233 280 : get_coprimes(GEN a, GEN b)
234 : {
235 280 : long i, k = 1;
236 280 : GEN u = cgetg(3, t_COL);
237 280 : gel(u,1) = a;
238 280 : gel(u,2) = b;
239 : /* u1,..., uk 2 by 2 coprime */
240 1015 : while (k+1 < lg(u))
241 : {
242 735 : GEN d, c = gel(u,k+1);
243 735 : if (is_pm1(c)) { k++; continue; }
244 1232 : for (i=1; i<=k; i++)
245 : {
246 791 : GEN ui = gel(u,i);
247 791 : if (is_pm1(ui)) continue;
248 455 : d = gcdii(c, ui);
249 455 : if (d == gen_1) continue;
250 455 : c = diviiexact(c, d);
251 455 : gel(u,i) = diviiexact(ui, d);
252 455 : u = vec_append(u, d);
253 : }
254 441 : gel(u,++k) = c;
255 : }
256 1295 : for (i = k = 1; i < lg(u); i++)
257 1015 : if (!is_pm1(gel(u,i))) gel(u,k++) = gel(u,i);
258 280 : 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 483 : fix_PE(GEN *pP, GEN *pE, long i, GEN u, GEN N)
295 : {
296 : GEN P, E;
297 483 : long k, l = lg(u), lP = lg(*pP);
298 : pari_sp av;
299 :
300 483 : *pP = P = shallowconcat(*pP, vecslice(u, 2, l-1));
301 483 : *pE = E = vecsmall_lengthen(*pE, lP + l-2);
302 483 : gel(P,i) = gel(u,1); av = avma;
303 483 : E[i] = Z_pvalrem(N, gel(P,i), &N);
304 973 : for (k=lP, lP=lg(P); k < lP; k++) E[k] = Z_pvalrem(N, gel(P,k), &N);
305 483 : set_avma(av);
306 483 : }
307 : static long
308 685476 : diag_denomval(GEN M, GEN p)
309 : {
310 : long j, v, l;
311 685476 : if (typ(M) != t_MAT) return 0;
312 462981 : v = 0; l = lg(M);
313 2071331 : for (j=1; j<l; j++)
314 : {
315 1608352 : GEN t = gcoeff(M,j,j);
316 1608352 : if (typ(t) == t_FRAC) v += Z_pval(gel(t,2), p);
317 : }
318 462979 : 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 182 : Z_fac(GEN n)
325 : {
326 182 : GEN p = icopy(n), part = ifac_start(p, 0);
327 : long e;
328 182 : ifac_next(&part , &p, &e); n = diviiexact(n, powiu(p, e));
329 182 : (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 811346 : 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 811346 : nfmaxord_check_args(S, T0, flag);
344 811320 : P = S->dTP; lP = lg(P);
345 811320 : E = S->dTE;
346 811320 : O = cgetg(1, t_VEC);
347 3139209 : for (i=1; i<lP; i++)
348 : {
349 : VOLATILE pari_sp av;
350 : /* includes the silly case where P[i] = -1 */
351 2327890 : if (E[i] <= 1)
352 : {
353 1302864 : if (S->certify)
354 : {
355 1295012 : GEN p = gel(P,i);
356 1295012 : if (signe(p) > 0 && !BPSW_psp(p))
357 : {
358 182 : fix_PE(&P, &E, i, Z_fac(p), S->dT);
359 182 : lP = lg(P); i--; continue;
360 : }
361 : }
362 1302688 : O = vec_append(O, gen_1); continue;
363 : }
364 1025026 : av = avma;
365 1025026 : pari_CATCH(CATCH_ALL) {
366 280 : GEN u, err = pari_err_last();
367 : long l;
368 280 : switch(err_get_num(err))
369 : {
370 280 : case e_INV:
371 : {
372 280 : GEN p, x = err_get_compo(err, 2);
373 280 : if (typ(x) == t_INTMOD)
374 : { /* caught false prime, update factorization */
375 280 : p = gcdii(gel(x,1), gel(x,2));
376 280 : u = diviiexact(gel(x,1),p);
377 280 : if (DEBUGLEVEL) pari_warn(warner,"impossible inverse: %Ps", x);
378 280 : gerepileall(av, 2, &p, &u);
379 :
380 280 : u = get_coprimes(p, u); l = lg(u);
381 : /* no small factors, but often a prime power */
382 840 : for (k = 1; k < l; k++) (void)Z_isanypower(gel(u,k), &gel(u,k));
383 280 : 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 : { /* 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 280 : fix_PE(&P, &E, i, u, S->dT);
412 280 : lP = lg(P); av = avma;
413 1025307 : } pari_RETRY {
414 1025307 : GEN p = gel(P,i), O2;
415 1025307 : if (DEBUGLEVEL>2) err_printf("Treating p^k = %Ps^%ld\n",p,E[i]);
416 1025307 : O2 = maxord(p,S->T,E[i]);
417 1025044 : if (S->certify && (odd(E[i]) || E[i] != 2*diag_denomval(O2, p))
418 609221 : && !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 1025024 : O = vec_append(O, O2);
425 1025031 : } pari_ENDCATCH;
426 : }
427 811319 : 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 97339 : diag_denom(GEN M)
434 : {
435 97339 : GEN d = gen_1;
436 97339 : long j, l = lg(M);
437 677603 : for (j=1; j<l; j++)
438 : {
439 580264 : GEN t = gcoeff(M,j,j);
440 580264 : if (typ(t) == t_INT) continue;
441 207832 : t = gel(t,2);
442 207832 : if (abscmpii(t,d) > 0) d = t;
443 : }
444 97339 : return d;
445 : }
446 : static void
447 744879 : setPE(GEN D, GEN P, GEN *pP, GEN *pE)
448 : {
449 744879 : long k, j, l = lg(P);
450 : GEN P2, E2;
451 744879 : *pP = P2 = cgetg(l, t_VEC);
452 744914 : *pE = E2 = cgetg(l, t_VECSMALL);
453 2864704 : for (k = j = 1; j < l; j++)
454 : {
455 2119750 : long v = Z_pvalrem(D, gel(P,j), &D);
456 2119760 : if (v) { gel(P2,k) = gel(P,j); E2[k] = v; k++; }
457 : }
458 744954 : setlg(P2, k);
459 744951 : setlg(E2, k);
460 744950 : }
461 : void
462 100169 : nfmaxord(nfmaxord_t *S, GEN T0, long flag)
463 : {
464 100169 : GEN O = get_maxord(S, T0, flag);
465 100181 : GEN f = S->T, P = S->dTP, a = NULL, da = NULL;
466 100181 : long n = degpol(f), lP = lg(P), i, j, k;
467 100182 : int centered = 0;
468 100182 : pari_sp av = avma;
469 : /* r1 & basden not initialized here */
470 100182 : S->r1 = -1;
471 100182 : S->basden = NULL;
472 350895 : for (i=1; i<lP; i++)
473 : {
474 250714 : GEN M, db, b = gel(O,i);
475 250714 : if (b == gen_1) continue;
476 97339 : db = diag_denom(b);
477 97339 : if (db == gen_1) continue;
478 :
479 : /* db = denom(b), (da,db) = 1. Compute da Im(b) + db Im(a) */
480 97339 : b = Q_muli_to_int(b,db);
481 97337 : if (!da) { da = db; a = b; }
482 : else
483 : { /* optimization: easy as long as both matrix are diagonal */
484 130215 : j=2; while (j<=n && fnz(gel(a,j),j) && fnz(gel(b,j),j)) j++;
485 49050 : k = j-1; M = cgetg(2*n-k+1,t_MAT);
486 179266 : for (j=1; j<=k; j++)
487 : {
488 130217 : gel(M,j) = gel(a,j);
489 130217 : 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 271595 : for ( ; j<=n; j++) gel(M,j) = ZC_Z_mul(gel(a,j), db);
493 271591 : for ( ; j<=2*n-k; j++) gel(M,j) = ZC_Z_mul(gel(b,j+k-n), da);
494 49044 : da = mulii(da,db);
495 49050 : a = ZM_hnfmodall_i(M, da, hnf_MODID|hnf_CENTER);
496 49052 : gerepileall(av, 2, &a, &da);
497 49051 : centered = 1;
498 : }
499 : }
500 100181 : if (da)
501 : {
502 48286 : GEN index = diviiexact(da, gcoeff(a,1,1));
503 227469 : for (j=2; j<=n; j++) index = mulii(index, diviiexact(da, gcoeff(a,j,j)));
504 48283 : if (!centered) a = ZM_hnfcenter(a);
505 48284 : a = RgM_Rg_div(a, da);
506 48287 : S->index = index;
507 48287 : S->dK = diviiexact(S->dT, sqri(index));
508 : }
509 : else
510 : {
511 51895 : S->index = gen_1;
512 51895 : S->dK = S->dT;
513 51895 : a = matid(n);
514 : }
515 100178 : setPE(S->dK, P, &S->dKP, &S->dKE);
516 100181 : S->basis = RgM_to_RgXV(a, varn(f));
517 100182 : }
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 711114 : maxord_disc(nfmaxord_t *S, GEN x)
532 : {
533 711114 : GEN O = get_maxord(S, x, 0), I = gen_1;
534 711172 : long n = degpol(S->T), lP = lg(O), i, j;
535 2788072 : for (i = 1; i < lP; i++)
536 : {
537 2076939 : GEN b = gel(O,i);
538 2076939 : if (b == gen_1) continue;
539 2706294 : for (j = 1; j <= n; j++)
540 : {
541 2114753 : GEN c = gcoeff(b,j,j);
542 2114753 : if (typ(c) == t_FRAC) I = mulii(I, gel(c,2)) ;
543 : }
544 : }
545 711133 : return diviiexact(S->dT, sqri(I));
546 : }
547 : GEN
548 66435 : nfdisc(GEN x)
549 : {
550 66435 : pari_sp av = avma;
551 : nfmaxord_t S;
552 66435 : return gerepileuptoint(av, maxord_disc(&S, x));
553 : }
554 : GEN
555 644760 : nfdiscfactors(GEN x)
556 : {
557 644760 : pari_sp av = avma;
558 644760 : GEN E, P, D, nf = checknf_i(x);
559 644753 : 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 644746 : D = maxord_disc(&S, x);
568 644687 : P = S.dTP;
569 : }
570 644694 : setPE(D, P, &P, &E); settyp(P, t_COL);
571 644767 : return gerepilecopy(av, mkvec2(D, mkmat2(P, zc_to_ZC(E))));
572 : }
573 :
574 : static ulong
575 1594248 : Flx_checkdeflate(GEN x)
576 : {
577 1594248 : ulong d = 0, i, lx = (ulong)lg(x);
578 2542325 : for (i=3; i<lx; i++)
579 1697905 : if (x[i]) { d = ugcd(d,i-2); if (d == 1) break; }
580 1594263 : 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 1594257 : Flx_radical(GEN f, ulong p)
587 : {
588 1594257 : long v0 = Flx_valrem(f, &f);
589 : ulong du, d, e;
590 : GEN u;
591 :
592 1594245 : d = Flx_checkdeflate(f);
593 1594339 : if (!d) return v0? polx_Flx(f[1]): pol1_Flx(f[1]);
594 998471 : if (u_lvalrem(d,p, &e)) f = Flx_deflate(f, d/e); /* f(x^p^i) -> f(x) */
595 998468 : u = Flx_gcd(f, Flx_deriv(f, p), p); /* (f,f') */
596 998460 : du = degpol(u);
597 998459 : if (du)
598 : {
599 313993 : if (du == (ulong)degpol(f))
600 0 : f = Flx_radical(Flx_deflate(f,p), p);
601 : else
602 : {
603 313992 : u = Flx_normalize(u, p);
604 313994 : f = Flx_div(f, u, p);
605 313991 : if (p <= du)
606 : {
607 65991 : GEN w = (degpol(f) >= degpol(u))? Flx_rem(f, u, p): f;
608 65991 : w = Flxq_powu(w, du, u, p);
609 65993 : w = Flx_div(u, Flx_gcd(w,u,p), p); /* u / gcd(u, v^(deg u-1)) */
610 65991 : f = Flx_mul(f, Flx_radical(Flx_deflate(w,p), p), p);
611 : }
612 : }
613 : }
614 998455 : if (v0) f = Flx_shift(f, 1);
615 998441 : return f;
616 : }
617 : /* Assume f reduced mod p, otherwise valuation at x may be wrong */
618 : static GEN
619 5495 : FpX_radical(GEN f, GEN p)
620 : {
621 : GEN u;
622 : long v0;
623 5495 : if (lgefint(p) == 3)
624 : {
625 1716 : ulong q = p[2];
626 1716 : return Flx_to_ZX( Flx_radical(ZX_to_Flx(f, q), q) );
627 : }
628 3779 : v0 = ZX_valrem(f, &f);
629 3779 : u = FpX_gcd(f,FpX_deriv(f, p), p);
630 3505 : if (degpol(u)) f = FpX_div(f, u, p);
631 3505 : if (v0) f = RgX_shift(f, 1);
632 3505 : return f;
633 : }
634 : /* f / a */
635 : static GEN
636 1526595 : zx_z_div(GEN f, ulong a)
637 : {
638 1526595 : long i, l = lg(f);
639 1526595 : GEN g = cgetg(l, t_VECSMALL);
640 1526637 : g[1] = f[1];
641 5156129 : for (i = 2; i < l; i++) g[i] = f[i] / a;
642 1526637 : 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 1532172 : ZX_Dedekind(GEN F, GEN *pg, GEN p)
649 : {
650 : GEN k, h, g, f, f2;
651 1532172 : ulong q = p[2];
652 1532172 : if (lgefint(p) == 3 && q < (1UL << BITS_IN_HALFULONG))
653 1526587 : {
654 1526679 : ulong q2 = q*q;
655 1526679 : f2 = ZX_to_Flx(F, q2);
656 1526578 : f = Flx_red(f2, q);
657 1526561 : g = Flx_radical(f, q);
658 1526601 : h = Flx_div(f, g, q);
659 1526589 : k = zx_z_div(Flx_sub(f2, Flx_mul(g,h,q2), q2), q);
660 1526643 : k = Flx_gcd(k, Flx_gcd(g,h,q), q);
661 1526633 : k = Flx_to_ZX(k);
662 1526568 : g = Flx_to_ZX(g);
663 : }
664 : else
665 : {
666 5493 : f2 = FpX_red(F, sqri(p));
667 5495 : f = FpX_red(f2, p);
668 5495 : g = FpX_radical(f, p);
669 5215 : h = FpX_div(f, g, p);
670 5215 : k = ZX_Z_divexact(ZX_sub(f2, ZX_mul(g,h)), p);
671 5215 : k = FpX_gcd(FpX_red(k, p), FpX_gcd(g,h,p), p);
672 : }
673 1531800 : *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 1532161 : maxord(GEN p, GEN f, long mf)
680 : {
681 1532161 : const pari_sp av = avma;
682 1532161 : GEN res, g, k = ZX_Dedekind(f, &g, p);
683 1531800 : long dk = degpol(k);
684 1531802 : if (DEBUGLEVEL>2) err_printf(" ZX_Dedekind: gcd has degree %ld\n", dk);
685 1531848 : if (!dk) { set_avma(av); return gen_1; }
686 872581 : if (mf < 0) mf = ZpX_disc_val(f, p);
687 872580 : k = FpX_normalize(k, p);
688 872594 : if (2*dk >= mf-1)
689 418089 : res = dbasis(p, f, mf, NULL, FpX_div(f,k,p));
690 : else
691 : {
692 : GEN w, F1, F2;
693 : decomp_t S;
694 454505 : F1 = FpX_factor(k,p);
695 454527 : F2 = FpX_factor(FpX_div(g,k,p),p);
696 454528 : w = merge_sort_uniq(gel(F1,1),gel(F2,1),(void*)cmpii,&gen_cmp_RgX);
697 454527 : res = maxord_i(&S, p, f, mf, w, 0);
698 : }
699 872622 : return gerepilecopy(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 gerepilecopy(av,res);
720 : }
721 :
722 : static GEN
723 4469616 : Zlx_sylvester_echelon(GEN f1, GEN f2, long early_abort, ulong p, ulong pm)
724 : {
725 4469616 : long j, n = degpol(f1);
726 4469586 : GEN h, a = cgetg(n+1,t_MAT);
727 4469607 : f1 = Flx_get_red(f1, pm);
728 4469575 : h = Flx_rem(f2,f1,pm);
729 15759584 : for (j=1;; j++)
730 : {
731 15759584 : gel(a,j) = Flx_to_Flv(h, n);
732 15758814 : if (j == n) break;
733 11289271 : h = Flx_rem(Flx_shift(h, 1), f1, pm);
734 : }
735 4469543 : 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 68349 : ZpX_sylvester_echelon(GEN f1, GEN f2, long early_abort, GEN p, GEN pm)
741 : {
742 68349 : long j, n = degpol(f1);
743 68349 : GEN h, a = cgetg(n+1,t_MAT);
744 68349 : h = FpXQ_red(f2,f1,pm);
745 402853 : for (j=1;; j++)
746 : {
747 402853 : gel(a,j) = RgX_to_RgC(h, n);
748 402855 : if (j == n) break;
749 334506 : h = FpX_rem(RgX_shift_shallow(h, 1), f1, pm);
750 : }
751 68349 : 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 245470 : Zlx_gcd(GEN f1, GEN f2, ulong p, ulong pm)
757 : {
758 245470 : pari_sp av = avma;
759 245470 : GEN a = Zlx_sylvester_echelon(f1,f2,0,p,pm);
760 245474 : long c, l = lg(a), sv = f1[1];
761 750918 : for (c = 1; c < l; c++)
762 : {
763 750917 : ulong t = ucoeff(a,c,c);
764 750917 : if (t)
765 : {
766 245473 : a = Flx_to_ZX(Flv_to_Flx(gel(a,c), sv));
767 245470 : if (t == 1) return gerepilecopy(av, a);
768 74884 : return gerepileupto(av, RgX_Rg_div(a, utoipos(t)));
769 : }
770 : }
771 1 : set_avma(av);
772 0 : a = cgetg(2,t_POL); a[1] = sv; return a;
773 : }
774 : GEN
775 253718 : ZpX_gcd(GEN f1, GEN f2, GEN p, GEN pm)
776 : {
777 253718 : pari_sp av = avma;
778 : GEN a;
779 : long c, l, v;
780 253718 : if (lgefint(pm) == 3)
781 : {
782 245473 : ulong q = pm[2];
783 245473 : return Zlx_gcd(ZX_to_Flx(f1, q), ZX_to_Flx(f2,q), p[2], q);
784 : }
785 8245 : a = ZpX_sylvester_echelon(f1,f2,0,p,pm);
786 8245 : l = lg(a); v = varn(f1);
787 52173 : for (c = 1; c < l; c++)
788 : {
789 52173 : GEN t = gcoeff(a,c,c);
790 52173 : if (signe(t))
791 : {
792 8245 : a = RgV_to_RgX(gel(a,c), v);
793 8245 : if (equali1(t)) return gerepilecopy(av, a);
794 2379 : return gerepileupto(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 4180642 : init_m(GEN p)
803 : {
804 : ulong pp;
805 4180642 : if (lgefint(p) > 3) return 1;
806 4179778 : pp = p[2]; /* m ~ 16 / log2(pp) */
807 4179778 : if (pp < 41) switch(pp)
808 : {
809 1194942 : case 2: return 16;
810 345577 : case 3: return 10;
811 172566 : case 5: return 6;
812 148986 : case 7: return 5;
813 185686 : case 11: case 13: return 4;
814 265186 : default: return 3;
815 : }
816 1866835 : return pp < 257? 2: 1;
817 : }
818 :
819 : /* reduced resultant mod p^m (assumes x monic) */
820 : GEN
821 992713 : ZpX_reduced_resultant(GEN x, GEN y, GEN p, GEN pm)
822 : {
823 992713 : pari_sp av = avma;
824 : GEN z;
825 992713 : if (lgefint(pm) == 3)
826 : {
827 981862 : ulong q = pm[2];
828 981862 : z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q),0,p[2],q);
829 981956 : if (lg(z) > 1)
830 : {
831 981956 : ulong c = ucoeff(z,1,1);
832 981956 : if (c) return gc_utoipos(av, c);
833 : }
834 : }
835 : else
836 : {
837 10851 : z = ZpX_sylvester_echelon(x,y,0,p,pm);
838 10867 : if (lg(z) > 1)
839 : {
840 10867 : GEN c = gcoeff(z,1,1);
841 10867 : if (signe(c)) return gerepileuptoint(av, c);
842 : }
843 : }
844 126891 : 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 933709 : ZpX_reduced_resultant_fast(GEN f, GEN g, GEN p, long M)
850 : {
851 933709 : GEN R, q = NULL;
852 : long m;
853 933709 : m = init_m(p); if (m < 1) m = 1;
854 58980 : for(;; m <<= 1) {
855 992770 : if (M < 2*m) break;
856 91084 : q = q? sqri(q): powiu(p, m); /* p^m */
857 91084 : R = ZpX_reduced_resultant(f,g, p, q); if (signe(R)) return R;
858 : }
859 901686 : q = powiu(p, M);
860 901619 : 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 3291432 : ZpX_resultant_val_i(GEN x, GEN y, GEN p, GEN pm)
866 : {
867 3291432 : pari_sp av = avma;
868 : GEN z;
869 : long i, l, v;
870 3291432 : if (lgefint(pm) == 3)
871 : {
872 3242200 : ulong q = pm[2], pp = p[2];
873 3242200 : z = Zlx_sylvester_echelon(ZX_to_Flx(x,q), ZX_to_Flx(y,q), 1, pp, q);
874 3242341 : if (!z) return gc_long(av,-1); /* failure */
875 3057950 : v = 0; l = lg(z);
876 12754789 : for (i = 1; i < l; i++) v += u_lval(ucoeff(z,i,i), pp);
877 : }
878 : else
879 : {
880 49232 : z = ZpX_sylvester_echelon(x, y, 1, p, pm);
881 49237 : if (!z) return gc_long(av,-1); /* failure */
882 48547 : v = 0; l = lg(z);
883 179344 : for (i = 1; i < l; i++) v += Z_pval(gcoeff(z,i,i), p);
884 : }
885 3106502 : return v;
886 : }
887 :
888 : /* assume (lc(f),p) = 1; no assumption on g */
889 : long
890 3246949 : ZpX_resultant_val(GEN f, GEN g, GEN p, long M)
891 : {
892 3246949 : pari_sp av = avma;
893 3246949 : GEN q = NULL;
894 : long v, m;
895 3246949 : m = init_m(p); if (m < 2) m = 2;
896 44509 : for(;; m <<= 1) {
897 3291454 : if (m > M) m = M;
898 3291454 : q = q? sqri(q): powiu(p, m); /* p^m */
899 3291433 : v = ZpX_resultant_val_i(f,g, p, q); if (v >= 0) return gc_long(av,v);
900 185085 : if (m == M) return gc_long(av,M);
901 : }
902 : }
903 :
904 : /* assume f separable and (lc(f),p) = 1 */
905 : long
906 184590 : ZpX_disc_val(GEN f, GEN p)
907 : {
908 184590 : pari_sp av = avma;
909 : long v;
910 184590 : if (degpol(f) == 1) return 0;
911 184590 : v = ZpX_resultant_val(f, ZX_deriv(f), p, LONG_MAX);
912 184587 : 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 3541981 : update_den(GEN p, GEN *e, GEN *d, long *vd, GEN *z)
919 : {
920 : GEN newe;
921 3541981 : long ve = ZX_pvalrem(*e, p, &newe);
922 3542139 : if (ve) {
923 : GEN newd;
924 1734311 : long v = minss(*vd, ve);
925 1734505 : if (v) {
926 1734589 : if (v == *vd)
927 : { /* rare, denominator cancelled */
928 375902 : if (ve != v) newe = ZX_Z_mul(newe, powiu(p, ve - v));
929 375902 : newd = gen_1;
930 375902 : *vd = 0;
931 375902 : if (z) *z =diviiexact(*z, powiu(p, v));
932 : }
933 : else
934 : { /* v = ve < vd, generic case */
935 1358687 : GEN q = powiu(p, v);
936 1358569 : newd = diviiexact(*d, q);
937 1358316 : *vd -= v;
938 1358316 : if (z) *z = diviiexact(*z, q);
939 : }
940 1734205 : *e = newe;
941 1734205 : *d = newd;
942 : }
943 : }
944 3541949 : }
945 :
946 : /* return denominator, a power of p */
947 : static GEN
948 2740899 : QpX_denom(GEN x)
949 : {
950 2740899 : long i, l = lg(x);
951 2740899 : GEN maxd = gen_1;
952 9460119 : for (i=2; i<l; i++)
953 : {
954 6719234 : GEN d = gel(x,i);
955 6719234 : if (typ(d) == t_FRAC && cmpii(gel(d,2), maxd) > 0) maxd = gel(d,2);
956 : }
957 2740885 : return maxd;
958 : }
959 : static GEN
960 506859 : QpXV_denom(GEN x)
961 : {
962 506859 : long l = lg(x), i;
963 506859 : GEN maxd = gen_1;
964 1507208 : for (i = 1; i < l; i++)
965 : {
966 1000349 : GEN d = QpX_denom(gel(x,i));
967 1000348 : if (cmpii(d, maxd) > 0) maxd = d;
968 : }
969 506859 : return maxd;
970 : }
971 :
972 : static GEN
973 1740579 : QpX_remove_denom(GEN x, GEN p, GEN *pdx, long *pv)
974 : {
975 1740579 : *pdx = QpX_denom(x);
976 1740578 : if (*pdx == gen_1) { *pv = 0; *pdx = NULL; }
977 : else {
978 1264111 : x = Q_muli_to_int(x,*pdx);
979 1264051 : *pv = Z_pval(*pdx, p);
980 : }
981 1740534 : return x;
982 : }
983 :
984 : /* p^v * f o g mod (T,q). q = p^vq */
985 : static GEN
986 286094 : compmod(GEN p, GEN f, GEN g, GEN T, GEN q, long v)
987 : {
988 286094 : GEN D = NULL, z, df, dg, qD;
989 286094 : long vD = 0, vdf, vdg;
990 :
991 286094 : f = QpX_remove_denom(f, p, &df, &vdf);
992 286091 : 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 286091 : g = QpX_remove_denom(g, p, &dg, &vdg);
996 286093 : if (df) { D = df; vD = vdf; }
997 286093 : if (dg) {
998 54516 : long degf = degpol(f);
999 54516 : D = mul_content(D, powiu(dg, degf));
1000 54516 : vD += degf * vdg;
1001 : }
1002 286093 : qD = D ? mulii(q, D): q;
1003 286077 : if (dg) f = FpX_rescale(f, dg, qD);
1004 286077 : z = FpX_FpXQ_eval(f, g, T, qD);
1005 286088 : 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 286088 : update_den(p, &z, &D, &vD, NULL);
1015 286093 : qD = mulii(D,q);
1016 286078 : if (v) vD -= v;
1017 286078 : z = FpX_center_i(z, qD, shifti(qD,-1));
1018 286095 : if (vD > 0)
1019 286095 : 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 286096 : return z;
1023 : }
1024 :
1025 : /* fast implementation of ZM_hnfmodid(M, D) / D, D = p^k */
1026 : static GEN
1027 454525 : ZpM_hnfmodid(GEN M, GEN p, GEN D)
1028 : {
1029 454525 : long i, l = lg(M);
1030 454525 : M = RgM_Rg_div(ZpM_echelon(M,0,p,D), D);
1031 2025924 : for (i = 1; i < l; i++)
1032 1571392 : if (gequal0(gcoeff(M,i,i))) gcoeff(M,i,i) = gen_1;
1033 454532 : 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 619182 : dbasis(GEN p, GEN f, long mf, GEN a, GEN U)
1040 : {
1041 619182 : long n = degpol(f), i, dU;
1042 : GEN b, h;
1043 :
1044 619194 : if (n == 1) return matid(1);
1045 619194 : if (a && gequalX(a)) a = NULL;
1046 619195 : 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 619199 : if (a)
1052 : {
1053 201103 : GEN pd = powiu(p, mf >> 1);
1054 201100 : GEN da, pdp = mulii(pd,p), D = pdp;
1055 : long vda;
1056 201094 : dU = U ? degpol(U): 0;
1057 201096 : b = cgetg(n+1, t_MAT);
1058 201099 : h = scalarpol(pd, varn(f));
1059 201100 : a = QpX_remove_denom(a, p, &da, &vda);
1060 201103 : if (da) D = mulii(D, da);
1061 201096 : gel(b,1) = scalarcol_shallow(pd, n);
1062 571034 : for (i=2; i<=n; i++)
1063 : {
1064 369937 : if (i == dU+1)
1065 0 : h = compmod(p, U, mkvec3(a,da,stoi(vda)), f, pdp, (mf>>1) - 1);
1066 : else
1067 : {
1068 369937 : h = FpXQ_mul(h, a, f, D);
1069 369929 : if (da) h = ZX_Z_divexact(h, da);
1070 : }
1071 369907 : gel(b,i) = RgX_to_RgC(h,n);
1072 : }
1073 201097 : return ZpM_hnfmodid(b, p, pd);
1074 : }
1075 : else
1076 : {
1077 418096 : if (!U) return matid(n);
1078 418096 : dU = degpol(U);
1079 418096 : if (dU == n) return matid(n);
1080 418096 : U = FpX_normalize(U, p);
1081 418097 : b = cgetg(n+1, t_MAT);
1082 1617007 : for (i = 1; i <= dU; i++) gel(b,i) = vec_ei(n, i);
1083 418097 : h = RgX_Rg_div(U, p);
1084 468545 : for ( ; i <= n; i++)
1085 : {
1086 468545 : gel(b, i) = RgX_to_RgC(h,n);
1087 468547 : if (i == n) break;
1088 50453 : h = RgX_shift_shallow(h,1);
1089 : }
1090 418094 : return b;
1091 : }
1092 : }
1093 :
1094 : static GEN
1095 506857 : get_partial_order_as_pols(GEN p, GEN f)
1096 : {
1097 506857 : GEN O = maxord(p, f, -1);
1098 506856 : long v = varn(f);
1099 506856 : return O == gen_1? pol_x_powers(degpol(f), v): RgM_to_RgXV(O, v);
1100 : }
1101 :
1102 : static long
1103 2144 : p_is_prime(decomp_t *S)
1104 : {
1105 2144 : if (S->pisprime < 0) S->pisprime = BPSW_psp(S->p);
1106 2144 : 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 253714 : Decomp(decomp_t *S, long flag)
1113 : {
1114 253714 : pari_sp av = avma;
1115 : GEN fred, pr2, pr, pk, ph2, ph, b1, b2, a, e, de, f1, f2, dt, th, chip;
1116 253714 : GEN p = S->p;
1117 253714 : long vde, vdt, k, r = maxss(flag, 2*S->df + 1);
1118 :
1119 253714 : if (DEBUGLEVEL>5) err_printf(" entering Decomp: %Ps^%ld\n f = %Ps\n",
1120 : p, r, S->f);
1121 253714 : else if (DEBUGLEVEL>2) err_printf(" entering Decomp\n");
1122 253714 : chip = FpX_red(S->chi, p);
1123 253715 : 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 253716 : b2 = FpX_div(chip, b1, p);
1129 253707 : 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 253718 : th = QpX_remove_denom(S->phi, p, &dt, &vdt);
1132 253716 : if (dt)
1133 : {
1134 122044 : long dega = degpol(a);
1135 122044 : vde = dega * vdt;
1136 122044 : de = powiu(dt, dega);
1137 122044 : pr = mulii(p, de);
1138 122041 : a = FpX_rescale(a, dt, pr);
1139 : }
1140 : else
1141 : {
1142 131672 : vde = 0;
1143 131672 : de = gen_1;
1144 131672 : pr = p;
1145 : }
1146 253714 : e = FpX_FpXQ_eval(a, th, S->f, pr);
1147 253709 : update_den(p, &e, &de, &vde, NULL);
1148 :
1149 253712 : pk = p; k = 1;
1150 : /* E, (1 - E) tend to orthogonal idempotents in Zp[X]/(f) */
1151 1173575 : while (k < r + vde)
1152 : { /* E <-- E^2(3-2E) mod p^2k, with E = e/de */
1153 : GEN D;
1154 919862 : pk = sqri(pk); k <<= 1;
1155 919820 : e = ZX_mul(ZX_sqr(e), Z_ZX_sub(mului(3,de), gmul2n(e,1)));
1156 919893 : de= mulii(de, sqri(de));
1157 919820 : vde *= 3;
1158 919820 : D = mulii(pk, de);
1159 919801 : e = FpX_rem(e, centermod(S->f, D), D); /* e/de defined mod pk */
1160 919848 : update_den(p, &e, &de, &vde, NULL);
1161 : }
1162 : /* required precision of the factors */
1163 253713 : pr = powiu(p, r); pr2 = shifti(pr, -1);
1164 253709 : ph = mulii(de,pr);ph2 = shifti(ph, -1);
1165 253707 : e = FpX_center_i(FpX_red(e, ph), ph, ph2);
1166 253714 : fred = FpX_red(S->f, ph);
1167 :
1168 253715 : f1 = ZpX_gcd(fred, Z_ZX_sub(de, e), p, ph); /* p-adic gcd(f, 1-e) */
1169 253717 : if (!is_pm1(de))
1170 : {
1171 122045 : fred = FpX_red(fred, pr);
1172 122044 : f1 = FpX_red(f1, pr);
1173 : }
1174 253716 : f2 = FpX_div(fred,f1, pr);
1175 253714 : f1 = FpX_center_i(f1, pr, pr2);
1176 253718 : f2 = FpX_center_i(f2, pr, pr2);
1177 :
1178 253718 : if (DEBUGLEVEL>5)
1179 0 : err_printf(" leaving Decomp: f1 = %Ps\nf2 = %Ps\ne = %Ps\nde= %Ps\n", f1,f2,e,de);
1180 :
1181 253718 : 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 253718 : else if (flag)
1187 : {
1188 287 : gerepileall(av, 2, &f1, &f2);
1189 287 : 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 253431 : gerepileall(av, 4, &f1, &f2, &e, &de);
1195 253431 : D = de;
1196 253431 : B1 = get_partial_order_as_pols(p,f1); n1 = lg(B1)-1;
1197 253427 : B2 = get_partial_order_as_pols(p,f2); n2 = lg(B2)-1; n = n1+n2;
1198 253429 : d1 = QpXV_denom(B1);
1199 253430 : d2 = QpXV_denom(B2); if (cmpii(d1, d2) < 0) d1 = d2;
1200 253430 : if (d1 != gen_1) {
1201 156770 : B1 = Q_muli_to_int(B1, d1);
1202 156770 : B2 = Q_muli_to_int(B2, d1);
1203 156762 : D = mulii(d1, D);
1204 : }
1205 253428 : fred = centermod_i(S->f, D, shifti(D,-1));
1206 253430 : M = cgetg(n+1, t_MAT);
1207 802114 : for (i=1; i<=n1; i++)
1208 548684 : gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B1,i),e,D), fred, D), n);
1209 253430 : e = Z_ZX_sub(de, e); B2 -= n1;
1210 705091 : for ( ; i<=n; i++)
1211 451665 : gel(M,i) = RgX_to_RgC(FpX_rem(FpX_mul(gel(B2,i),e,D), fred, D), n);
1212 253426 : return ZpM_hnfmodid(M, p, D);
1213 : }
1214 : }
1215 :
1216 : /* minimum extension valuation: L/E */
1217 : static void
1218 622610 : vstar(GEN p,GEN h, long *L, long *E)
1219 : {
1220 622610 : long first, j, k, v, w, m = degpol(h);
1221 :
1222 622608 : first = 1; k = 1; v = 0;
1223 2563236 : for (j=1; j<=m; j++)
1224 : {
1225 1940624 : GEN c = gel(h, m-j+2);
1226 1940624 : if (signe(c))
1227 : {
1228 1864691 : w = Z_pval(c,p);
1229 1864695 : if (first || w*k < v*j) { v = w; k = j; }
1230 1864695 : first = 0;
1231 : }
1232 : }
1233 : /* v/k = min_j ( v_p(h_{m-j}) / j ) */
1234 622612 : w = (long)ugcd(v,k);
1235 622610 : *L = v/w;
1236 622610 : *E = k/w;
1237 622610 : }
1238 :
1239 : static GEN
1240 63058 : redelt_i(GEN a, GEN N, GEN p, GEN *pda, long *pvda)
1241 : {
1242 : GEN z;
1243 63058 : a = Q_remove_denom(a, pda);
1244 63058 : *pvda = 0;
1245 63058 : if (*pda)
1246 : {
1247 63058 : long v = Z_pvalrem(*pda, p, &z);
1248 63055 : if (v) {
1249 63055 : *pda = powiu(p, v);
1250 63058 : *pvda = v;
1251 63058 : N = mulii(*pda, N);
1252 : }
1253 : else
1254 0 : *pda = NULL;
1255 63057 : if (!is_pm1(z)) a = ZX_Z_mul(a, Fp_inv(z, N));
1256 : }
1257 63057 : 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 47746 : redelt(GEN a, GEN N, GEN p)
1263 : {
1264 : GEN da;
1265 : long vda;
1266 47746 : a = redelt_i(a, N, p, &da, &vda);
1267 47746 : if (da) a = RgX_Rg_div(a, da);
1268 47746 : 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 706302 : 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 706302 : long j, k, vdpa, lns = lg(ns);
1280 : pari_sp av;
1281 :
1282 706302 : a = centermod(a, pp); av = avma;
1283 706278 : dpa = pa = NULL; /* -Wall */
1284 706278 : vdpa = 0;
1285 706278 : va = zerovec(c);
1286 2896303 : for (j = 1; j <= c; j++)
1287 : { /* pa/dpa = (a/d)^(j-1) mod (chi, pp), dpa = p^vdpa */
1288 : long l;
1289 2196796 : pa = j == 1? a: FpXQ_mul(pa, a, chi, pp);
1290 2197027 : l = lg(pa); if (l == 2) break;
1291 2197027 : if (lns < l) l = lns;
1292 :
1293 2197027 : if (da) {
1294 2060831 : dpa = j == 1? da: mulii(dpa, da);
1295 2060380 : vdpa += vda;
1296 2060380 : update_den(p, &pa, &dpa, &vdpa, &pp);
1297 : }
1298 2196582 : s = mulii(gel(pa,2), gel(ns,2)); /* k = 2 */
1299 10818126 : for (k = 3; k < l; k++) s = addii(s, mulii(gel(pa,k), gel(ns,k)));
1300 2196041 : if (da) {
1301 : GEN r;
1302 2059923 : s = dvmdii(s, dpa, &r);
1303 2060102 : if (r != gen_0) return NULL;
1304 : }
1305 2189496 : gel(va,j) = centermodii(s, pp, shifti(pp,-1));
1306 :
1307 2189557 : if (gc_needed(av, 1))
1308 : {
1309 7 : if(DEBUGMEM>1) pari_warn(warnmem, "newtonsums");
1310 7 : gerepileall(av, dpa?4:3, &pa, &va, &pp, &dpa);
1311 : }
1312 : }
1313 699507 : for (; j <= c; j++) gel(va,j) = gen_0;
1314 699507 : 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 699539 : newtoncharpoly(GEN pp, GEN p, GEN NS)
1321 : {
1322 699539 : long n = lg(NS)-1, j, k;
1323 699539 : GEN c = cgetg(n + 2, t_VEC), pp2 = shifti(pp,-1);
1324 :
1325 699496 : gel(c,1) = (n & 1 ? gen_m1: gen_1);
1326 2879325 : for (k = 2; k <= n+1; k++)
1327 : {
1328 2179844 : pari_sp av2 = avma;
1329 2179844 : GEN s = gen_0;
1330 : ulong z;
1331 2179844 : long v = u_pvalrem(k - 1, p, &z);
1332 9178612 : for (j = 1; j < k; j++)
1333 : {
1334 7000340 : GEN t = mulii(gel(NS,j), gel(c,k-j));
1335 6997891 : if (!odd(j)) t = negi(t);
1336 6998592 : s = addii(s, t);
1337 : }
1338 2178272 : if (v) {
1339 833950 : s = gdiv(s, powiu(p, v));
1340 834058 : if (typ(s) != t_INT) return NULL;
1341 : }
1342 2178289 : s = mulii(s, Fp_inv(utoipos(z), pp));
1343 2179002 : gel(c,k) = gerepileuptoint(av2, Fp_center_i(s, pp, pp2));
1344 : }
1345 1853273 : for (k = odd(n)? 1: 2; k <= n+1; k += 2) gel(c,k) = negi(gel(c,k));
1346 699491 : return gtopoly(c, 0);
1347 : }
1348 :
1349 : static void
1350 706169 : manage_cache(decomp_t *S, GEN f, GEN pp)
1351 : {
1352 706169 : GEN t = S->precns;
1353 :
1354 706169 : if (!t) t = mulii(S->pmf, powiu(S->p, S->df));
1355 706172 : if (cmpii(t, pp) < 0) t = pp;
1356 :
1357 706247 : if (!S->precns || !RgX_equal(f, S->nsf) || cmpii(S->precns, t) < 0)
1358 : {
1359 523044 : 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 523044 : S->nsf = f;
1363 523044 : S->ns = FpX_Newton(f, degpol(f), t);
1364 523060 : S->precns = t;
1365 : }
1366 706280 : }
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 706311 : 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 706311 : long vd, n = degpol(f);
1376 :
1377 706312 : if (gequal0(a)) return pol_0(varn(f));
1378 :
1379 706310 : a = QpX_remove_denom(a, S->p, &d, &vd);
1380 706299 : prec1 = pp;
1381 706299 : if (lgefint(S->p) == 3)
1382 706268 : prec1 = mulii(prec1, powiu(S->p, factorial_lval(n, itou(S->p))));
1383 706168 : if (d)
1384 : {
1385 640701 : GEN p1 = powiu(d, n);
1386 640787 : prec2 = mulii(prec1, p1);
1387 640716 : prec3 = mulii(prec1, gmin_shallow(mulii(p1, d), pdr));
1388 : }
1389 : else
1390 65467 : prec2 = prec3 = prec1;
1391 706182 : manage_cache(S, f, prec3);
1392 :
1393 706307 : av = avma;
1394 706307 : ns = newtonsums(S->p, a, d, vd, f, n, prec2, S->ns);
1395 706238 : if (!ns) return NULL;
1396 699514 : chi = newtoncharpoly(prec1, S->p, ns);
1397 699583 : if (!chi) return NULL;
1398 699492 : setvarn(chi, varn(f));
1399 699492 : return gerepileupto(av, centermod(chi, pp));
1400 : }
1401 :
1402 : static GEN
1403 641217 : get_nu(GEN chi, GEN p, long *ptl)
1404 : { /* split off powers of x first for efficiency */
1405 641217 : long v = ZX_valrem(FpX_red(chi,p), &chi), n;
1406 : GEN P;
1407 641212 : if (!degpol(chi)) { *ptl = 1; return pol_x(varn(chi)); }
1408 476632 : P = gel(FpX_factor(chi,p), 1); n = lg(P)-1;
1409 476644 : *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 479824 : split_char(decomp_t *S, GEN chi, GEN phi, GEN phi0, GEN *nu)
1417 : {
1418 : long l;
1419 479824 : *nu = get_nu(chi, S->p, &l);
1420 479831 : if (l == 1) return 0; /* single irreducible factor: doesn't split */
1421 : /* phi o phi0 mod (p, f) */
1422 122044 : S->phi = compmod(S->p, phi, phi0, S->f, S->p, 0);
1423 122044 : S->chi = chi;
1424 122044 : 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 562092 : 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 562092 : if (phi && dvdii(constant_coeff(chip), S->psc))
1438 : {
1439 1630 : chip = mycaract(S, S->chi, phi, S->pmf, S->prc);
1440 1630 : if (dvdii(constant_coeff(chip), S->pmf))
1441 1254 : chip = ZXQ_charpoly(phi, S->chi, varn(chip));
1442 : }
1443 562083 : if (degpol(nup) == 1)
1444 : {
1445 525194 : GEN c = gel(nup,2); /* nup = X + c */
1446 525194 : chin = signe(c)? RgX_translate(chip, negi(c)): chip;
1447 : }
1448 : else
1449 36882 : chin = ZXQ_charpoly(nup, chip, varn(chip));
1450 :
1451 562079 : vstar(S->p, chin, Lp, Ep);
1452 562090 : if (*Ep < oE || (Ediv && Ediv % *Ep == 0)) return NULL;
1453 :
1454 441412 : if (*Ep == 1) return S->p;
1455 303468 : (void)cbezout(*Lp, -*Ep, &r, &s); /* = 1 */
1456 303473 : if (r <= 0)
1457 : {
1458 60178 : long t = 1 + ((-r) / *Ep);
1459 60178 : r += t * *Ep;
1460 60178 : 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 303473 : q = powiu(S->p, s); qp = mulii(q, S->p);
1466 303446 : nup = FpXQ_powu(nup, r, S->chi, qp);
1467 303467 : if (!phi) return RgX_Rg_div(nup, q); /* phi = X : no composition */
1468 47746 : z = compmod(S->p, nup, phi, S->chi, qp, -s);
1469 47746 : return signe(z)? z: NULL;
1470 : }
1471 :
1472 : static int
1473 277694 : update_phi(decomp_t *S)
1474 : {
1475 277694 : GEN PHI = NULL, prc, psc, X = pol_x(varn(S->f));
1476 : long k, vpsc;
1477 277695 : for (k = 1;; k++)
1478 : {
1479 280040 : 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 280033 : if (!equalii(prc, S->psc)) break;
1483 :
1484 : /* increase precision */
1485 2345 : S->vpsc = maxss(S->vpsf, S->vpsc + 1);
1486 2345 : S->psc = (S->vpsc == S->vpsf)? S->psf: mulii(S->psc, S->p);
1487 :
1488 2345 : PHI = S->phi;
1489 2345 : if (S->phi0) PHI = compmod(S->p, PHI, S->phi0, S->f, S->psc, 0);
1490 : /* change phi (in case not separable) */
1491 2345 : PHI = gadd(PHI, ZX_Z_mul(X, mului(k, S->p)));
1492 2345 : S->chi = mycaract(S, S->f, PHI, S->psc, S->pdf);
1493 : }
1494 277688 : psc = mulii(sqri(prc), S->p);
1495 277666 : vpsc = 2*Z_pval(prc, S->p) + 1;
1496 :
1497 277691 : if (!PHI) /* break out of above loop immediately (k = 1) */
1498 : {
1499 275355 : PHI = S->phi;
1500 275355 : if (S->phi0) PHI = compmod(S->p, PHI, S->phi0, S->f, psc, 0);
1501 275356 : if (S->phi0 || cmpii(psc,S->psc) > 0)
1502 : {
1503 : for(;;)
1504 : {
1505 114086 : S->chi = mycaract(S, S->f, PHI, psc, S->pdf);
1506 114086 : prc = ZpX_reduced_resultant_fast(S->chi, ZX_deriv(S->chi), S->p, vpsc);
1507 114088 : if (!equalii(prc, psc)) break;
1508 49 : psc = mulii(psc, S->p); vpsc++;
1509 : }
1510 114038 : psc = mulii(sqri(prc), S->p);
1511 114031 : vpsc = 2*Z_pval(prc, S->p) + 1;
1512 : }
1513 : }
1514 277691 : S->phi = PHI;
1515 277691 : S->chi = FpX_red(S->chi, psc);
1516 :
1517 : /* may happen if p is unramified */
1518 277694 : if (is_pm1(prc)) return 0;
1519 231440 : S->prc = prc;
1520 231440 : S->psc = psc;
1521 231440 : S->vpsc = vpsc; return 1;
1522 : }
1523 :
1524 : /* return 1 if at least 2 factors mod p ==> chi splits
1525 : * Replace S->phi such that F increases (to D) */
1526 : static int
1527 68553 : testb2(decomp_t *S, long D, GEN theta)
1528 : {
1529 68553 : long v = varn(S->chi), dlim = degpol(S->chi)-1;
1530 68554 : GEN T0 = S->phi, chi, phi, nu;
1531 68554 : if (DEBUGLEVEL>4) err_printf(" Increasing Fa\n");
1532 : for (;;)
1533 : {
1534 68632 : phi = gadd(theta, random_FpX(dlim, v, S->p));
1535 68632 : chi = mycaract(S, S->chi, phi, S->psf, S->prc);
1536 : /* phi nonprimary ? */
1537 68632 : if (split_char(S, chi, phi, T0, &nu)) return 1;
1538 68632 : if (degpol(nu) == D) break;
1539 : }
1540 : /* F_phi=lcm(F_alpha, F_theta)=D and E_phi=E_alpha */
1541 68554 : S->phi0 = T0;
1542 68554 : S->chi = chi;
1543 68554 : S->phi = phi;
1544 68554 : S->nu = nu; return 0;
1545 : }
1546 :
1547 : /* return 1 if at least 2 factors mod p ==> chi can be split.
1548 : * compute a new S->phi such that E = lcm(Ea, Et);
1549 : * A a ZX, T a t_INT (iff Et = 1, probably impossible ?) or QX */
1550 : static int
1551 47746 : testc2(decomp_t *S, GEN A, long Ea, GEN T, long Et)
1552 : {
1553 47746 : GEN c, chi, phi, nu, T0 = S->phi;
1554 :
1555 47746 : if (DEBUGLEVEL>4) err_printf(" Increasing Ea\n");
1556 47746 : if (Et == 1) /* same as other branch, split for efficiency */
1557 0 : c = A; /* Et = 1 => s = 1, r = 0, t = 0 */
1558 : else
1559 : {
1560 : long r, s, t;
1561 47746 : (void)cbezout(Ea, Et, &r, &s); t = 0;
1562 47844 : while (r < 0) { r = r + Et; t++; }
1563 47954 : while (s < 0) { s = s + Ea; t++; }
1564 :
1565 : /* A^s T^r / p^t */
1566 47746 : c = RgXQ_mul(RgXQ_powu(A, s, S->chi), RgXQ_powu(T, r, S->chi), S->chi);
1567 47746 : c = RgX_Rg_div(c, powiu(S->p, t));
1568 47746 : c = redelt(c, S->psc, S->p);
1569 : }
1570 47746 : phi = RgX_add(c, pol_x(varn(S->chi)));
1571 47746 : chi = mycaract(S, S->chi, phi, S->psf, S->prc);
1572 47746 : if (split_char(S, chi, phi, T0, &nu)) return 1;
1573 : /* E_phi = lcm(E_alpha,E_theta) */
1574 47746 : S->phi0 = T0;
1575 47746 : S->chi = chi;
1576 47746 : S->phi = phi;
1577 47746 : S->nu = nu; return 0;
1578 : }
1579 :
1580 : /* Return h^(-degpol(P)) P(x * h) if result is integral, NULL otherwise */
1581 : static GEN
1582 59787 : ZX_rescale_inv(GEN P, GEN h)
1583 : {
1584 59787 : long i, l = lg(P);
1585 59787 : GEN Q = cgetg(l,t_POL), hi = h;
1586 59787 : gel(Q,l-1) = gel(P,l-1);
1587 174119 : for (i=l-2; i>=2; i--)
1588 : {
1589 : GEN r;
1590 174118 : gel(Q,i) = dvmdii(gel(P,i), hi, &r);
1591 174118 : if (signe(r)) return NULL;
1592 174118 : if (i == 2) break;
1593 114331 : hi = mulii(hi,h);
1594 : }
1595 59788 : Q[1] = P[1]; return Q;
1596 : }
1597 :
1598 : /* x p^-eq nu^-er mod p */
1599 : static GEN
1600 303183 : get_gamma(decomp_t *S, GEN x, long eq, long er)
1601 : {
1602 303183 : GEN q, g = x, Dg = powiu(S->p, eq);
1603 303162 : long vDg = eq;
1604 303162 : if (er)
1605 : {
1606 22343 : if (!S->invnu)
1607 : {
1608 15312 : while (gdvd(S->chi, S->nu)) S->nu = RgX_Rg_add(S->nu, S->p);
1609 15311 : S->invnu = QXQ_inv(S->nu, S->chi);
1610 15312 : S->invnu = redelt_i(S->invnu, S->psc, S->p, &S->Dinvnu, &S->vDinvnu);
1611 : }
1612 22343 : if (S->Dinvnu) {
1613 22342 : Dg = mulii(Dg, powiu(S->Dinvnu, er));
1614 22342 : vDg += er * S->vDinvnu;
1615 : }
1616 22343 : q = mulii(S->p, Dg);
1617 22342 : g = ZX_mul(g, FpXQ_powu(S->invnu, er, S->chi, q));
1618 22343 : g = FpX_rem(g, S->chi, q);
1619 22343 : update_den(S->p, &g, &Dg, &vDg, NULL);
1620 22342 : g = centermod(g, mulii(S->p, Dg));
1621 : }
1622 303162 : if (!is_pm1(Dg)) g = RgX_Rg_div(g, Dg);
1623 303185 : return g;
1624 : }
1625 : static GEN
1626 356155 : get_g(decomp_t *S, long Ea, long L, long E, GEN beta, GEN *pchig,
1627 : long *peq, long *per)
1628 : {
1629 : long eq, er;
1630 356155 : GEN g, chig, chib = NULL;
1631 : for(;;) /* at most twice */
1632 : {
1633 362970 : if (L < 0)
1634 : {
1635 60522 : chib = ZXQ_charpoly(beta, S->chi, varn(S->chi));
1636 60522 : vstar(S->p, chib, &L, &E);
1637 : }
1638 362970 : eq = L / E; er = L*Ea / E - eq*Ea;
1639 : /* floor(L Ea/E) = eq Ea + er */
1640 362970 : if (er || !chib)
1641 : { /* g might not be an integer ==> chig = NULL */
1642 303183 : g = get_gamma(S, beta, eq, er);
1643 303185 : chig = mycaract(S, S->chi, g, S->psc, S->prc);
1644 : }
1645 : else
1646 : { /* g = beta/p^eq, special case of the above */
1647 59787 : GEN h = powiu(S->p, eq);
1648 59787 : g = RgX_Rg_div(beta, h);
1649 59787 : chig = ZX_rescale_inv(chib, h); /* chib(x h) / h^N */
1650 59787 : if (chig) chig = FpX_red(chig, S->pmf);
1651 : }
1652 : /* either success or second consecutive failure */
1653 362968 : if (chig || chib) break;
1654 : /* if g fails the v*-test, v(beta) was wrong. Retry once */
1655 6815 : L = -1;
1656 : }
1657 356153 : *pchig = chig; *peq = eq; *per = er; return g;
1658 : }
1659 :
1660 : /* return 1 if at least 2 factors mod p ==> chi can be split */
1661 : static int
1662 238338 : loop(decomp_t *S, long Ea)
1663 : {
1664 238338 : pari_sp av = avma;
1665 238338 : GEN beta = FpXQ_powu(S->nu, Ea, S->chi, S->p);
1666 238332 : long N = degpol(S->f), v = varn(S->f);
1667 238334 : S->invnu = NULL;
1668 : for (;;)
1669 117814 : { /* beta tends to a factor of chi */
1670 : long L, i, Fg, eq, er;
1671 356148 : GEN chig = NULL, d, g, nug;
1672 :
1673 356148 : if (DEBUGLEVEL>4) err_printf(" beta = %Ps\n", beta);
1674 356148 : L = ZpX_resultant_val(S->chi, beta, S->p, S->mf+1);
1675 356155 : if (L > S->mf) L = -1; /* from scratch */
1676 356155 : g = get_g(S, Ea, L, N, beta, &chig, &eq, &er);
1677 356153 : if (DEBUGLEVEL>4) err_printf(" (eq,er) = (%ld,%ld)\n", eq,er);
1678 : /* g = beta p^-eq nu^-er (a unit), chig = charpoly(g) */
1679 474249 : if (split_char(S, chig, g,S->phi, &nug)) return 1;
1680 :
1681 235907 : Fg = degpol(nug);
1682 235907 : if (Fg == 1)
1683 : { /* frequent special case nug = x - d */
1684 : long Le, Ee;
1685 : GEN chie, nue, e, pie;
1686 160059 : d = negi(gel(nug,2));
1687 160061 : chie = RgX_translate(chig, d);
1688 160059 : nue = pol_x(v);
1689 160058 : e = RgX_Rg_sub(g, d);
1690 160059 : pie = getprime(S, e, chie, nue, &Le, &Ee, 0,Ea);
1691 160061 : if (pie) return testc2(S, S->nu, Ea, pie, Ee);
1692 : }
1693 : else
1694 : {
1695 75848 : long Fa = degpol(S->nu), vdeng;
1696 : GEN deng, numg, nume;
1697 78783 : if (Fa % Fg) return testb2(S, ulcm(Fa,Fg), g);
1698 : /* nu & nug irreducible mod p, deg nug | deg nu. To improve beta, look
1699 : * for a root d of nug in Fp[phi] such that v_p(g - d) > 0 */
1700 7297 : if (ZX_equal(nug, S->nu))
1701 5153 : d = pol_x(v);
1702 : else
1703 : {
1704 2144 : if (!p_is_prime(S)) pari_err_PRIME("FpX_ffisom",S->p);
1705 2144 : d = FpX_ffisom(nug, S->nu, S->p);
1706 : }
1707 : /* write g = numg / deng, e = nume / deng */
1708 7297 : numg = QpX_remove_denom(g, S->p, &deng, &vdeng);
1709 11712 : for (i = 1; i <= Fg; i++)
1710 : {
1711 : GEN chie, nue, e;
1712 11712 : if (i != 1) d = FpXQ_pow(d, S->p, S->nu, S->p); /* next root */
1713 11712 : nume = ZX_sub(numg, ZX_Z_mul(d, deng));
1714 : /* test e = nume / deng */
1715 11712 : if (ZpX_resultant_val(S->chi, nume, S->p, vdeng*N+1) <= vdeng*N)
1716 4415 : continue;
1717 7297 : e = RgX_Rg_div(nume, deng);
1718 7297 : chie = mycaract(S, S->chi, e, S->psc, S->prc);
1719 8433 : if (split_char(S, chie, e,S->phi, &nue)) return 1;
1720 5501 : if (RgX_is_monomial(nue))
1721 : { /* v_p(e) = v_p(g - d) > 0 */
1722 : long Le, Ee;
1723 : GEN pie;
1724 5501 : pie = getprime(S, e, chie, nue, &Le, &Ee, 0,Ea);
1725 5501 : if (pie) return testc2(S, S->nu, Ea, pie, Ee);
1726 4365 : break;
1727 : }
1728 : }
1729 4365 : if (i > Fg)
1730 : {
1731 0 : if (!p_is_prime(S)) pari_err_PRIME("nilord",S->p);
1732 0 : pari_err_BUG("nilord (no root)");
1733 : }
1734 : }
1735 117816 : if (eq) d = gmul(d, powiu(S->p, eq));
1736 117808 : if (er) d = gmul(d, gpowgs(S->nu, er));
1737 117808 : beta = gsub(beta, d);
1738 :
1739 117814 : if (gc_needed(av,1))
1740 : {
1741 0 : if (DEBUGMEM > 1) pari_warn(warnmem, "nilord");
1742 0 : gerepileall(av, S->invnu? 6: 4, &beta, &(S->precns), &(S->ns), &(S->nsf), &(S->invnu), &(S->Dinvnu));
1743 : }
1744 : }
1745 : }
1746 :
1747 : /* E and F cannot decrease; return 1 if O = Zp[phi], 2 if we can get a
1748 : * decomposition and 0 otherwise */
1749 : static long
1750 393651 : progress(decomp_t *S, GEN *ppa, long *pE)
1751 : {
1752 393651 : long E = *pE, F;
1753 393651 : GEN pa = *ppa;
1754 393651 : S->phi0 = NULL; /* no delayed composition */
1755 : for(;;)
1756 2862 : {
1757 : long l, La, Ea; /* N.B If E = 0, getprime cannot return NULL */
1758 396513 : GEN pia = getprime(S, NULL, S->chi, S->nu, &La, &Ea, E,0);
1759 396533 : if (pia) { /* success, we break out in THIS loop */
1760 393671 : pa = (typ(pia) == t_POL)? RgX_RgXQ_eval(pia, S->phi, S->f): pia;
1761 393668 : E = Ea;
1762 393668 : if (La == 1) break; /* no need to change phi so that nu = pia */
1763 : }
1764 : /* phi += prime elt */
1765 65657 : S->phi = typ(pa) == t_INT? RgX_Rg_add_shallow(S->phi, pa)
1766 161393 : : RgX_add(S->phi, pa);
1767 : /* recompute char. poly. chi from scratch */
1768 161395 : S->chi = mycaract(S, S->f, S->phi, S->psf, S->pdf);
1769 161393 : S->nu = get_nu(S->chi, S->p, &l);
1770 161395 : if (l > 1) return 2;
1771 161395 : if (!update_phi(S)) return 1; /* unramified */
1772 161395 : if (pia) break;
1773 : }
1774 393670 : *pE = E; *ppa = pa; F = degpol(S->nu);
1775 393670 : if (DEBUGLEVEL>4) err_printf(" (E, F) = (%ld,%ld)\n", E, F);
1776 393670 : if (E * F == degpol(S->f)) return 1;
1777 238336 : if (loop(S, E)) return 2;
1778 116300 : if (!update_phi(S)) return 1;
1779 70044 : return 0;
1780 : }
1781 :
1782 : /* flag != 0 iff we're looking for the p-adic factorization,
1783 : in which case it is the p-adic precision we want */
1784 : static GEN
1785 455294 : maxord_i(decomp_t *S, GEN p, GEN f, long mf, GEN w, long flag)
1786 : {
1787 455294 : long oE, n = lg(w)-1; /* factor of largest degree */
1788 455294 : GEN opa, D = ZpX_reduced_resultant_fast(f, ZX_deriv(f), p, mf);
1789 455285 : S->pisprime = -1;
1790 455285 : S->p = p;
1791 455285 : S->mf = mf;
1792 455285 : S->nu = gel(w,n);
1793 455285 : S->df = Z_pval(D, p);
1794 455288 : S->pdf = powiu(p, S->df);
1795 455257 : S->phi = pol_x(varn(f));
1796 455295 : S->chi = S->f = f;
1797 455295 : if (n > 1) return Decomp(S, flag); /* FIXME: use bezout_lift_fact */
1798 :
1799 323624 : if (DEBUGLEVEL>4)
1800 0 : err_printf(" entering Nilord: %Ps^%ld\n f = %Ps, nu = %Ps\n",
1801 : p, S->df, S->f, S->nu);
1802 323624 : else if (DEBUGLEVEL>2) err_printf(" entering Nilord\n");
1803 323624 : S->psf = S->psc = mulii(sqri(D), p);
1804 323594 : S->vpsf = S->vpsc = 2*S->df + 1;
1805 323594 : S->prc = mulii(D, p);
1806 323589 : S->chi = FpX_red(S->f, S->psc);
1807 323621 : S->pmf = powiu(p, S->mf+1);
1808 323608 : S->precns = NULL;
1809 323608 : for(opa = NULL, oE = 0;;)
1810 70041 : {
1811 393649 : long n = progress(S, &opa, &oE);
1812 393671 : if (n == 1) return flag? NULL: dbasis(p, S->f, S->mf, S->phi, S->chi);
1813 192085 : if (n == 2) return Decomp(S, flag);
1814 : }
1815 : }
1816 :
1817 : static int
1818 812 : expo_is_squarefree(GEN e)
1819 : {
1820 812 : long i, l = lg(e);
1821 1183 : for (i=1; i<l; i++)
1822 945 : if (e[i] != 1) return 0;
1823 238 : return 1;
1824 : }
1825 : /* pure round 4 */
1826 : static GEN
1827 770 : ZpX_round4(GEN f, GEN p, GEN w, long prec)
1828 : {
1829 : decomp_t S;
1830 770 : GEN L = maxord_i(&S, p, f, ZpX_disc_val(f,p), w, prec);
1831 770 : return L? L: mkvec(f);
1832 : }
1833 : /* f a squarefree ZX with leading_coeff 1, degree > 0. Return list of
1834 : * irreducible factors in Zp[X] (computed mod p^prec) */
1835 : static GEN
1836 1071 : ZpX_monic_factor_squarefree(GEN f, GEN p, long prec)
1837 : {
1838 1071 : pari_sp av = avma;
1839 : GEN L, fa, w, e;
1840 : long i, l;
1841 1071 : if (degpol(f) == 1) return mkvec(f);
1842 812 : fa = FpX_factor(f,p); w = gel(fa,1); e = gel(fa,2);
1843 : /* no repeated factors: Hensel lift */
1844 812 : if (expo_is_squarefree(e)) return ZpX_liftfact(f, w, powiu(p,prec), p, prec);
1845 574 : l = lg(w);
1846 574 : if (l == 2)
1847 : {
1848 371 : L = ZpX_round4(f,p,w,prec);
1849 371 : if (lg(L) == 2) { set_avma(av); return mkvec(f); }
1850 : }
1851 : else
1852 : { /* >= 2 factors mod p: partial Hensel lift */
1853 203 : GEN D = ZpX_reduced_resultant_fast(f, ZX_deriv(f), p, ZpX_disc_val(f,p));
1854 203 : long r = maxss(2*Z_pval(D,p)+1, prec);
1855 203 : GEN W = cgetg(l, t_VEC);
1856 665 : for (i = 1; i < l; i++)
1857 462 : gel(W,i) = e[i] == 1? gel(w,i): FpX_powu(gel(w,i), e[i], p);
1858 203 : L = ZpX_liftfact(f, W, powiu(p,r), p, r);
1859 665 : for (i = 1; i < l; i++)
1860 462 : gel(L,i) = e[i] == 1? mkvec(gel(L,i))
1861 462 : : ZpX_round4(gel(L,i), p, mkvec(gel(w,i)), prec);
1862 203 : L = shallowconcat1(L);
1863 : }
1864 343 : return gerepilecopy(av, L);
1865 : }
1866 :
1867 : /* assume T a ZX with leading_coeff 1, degree > 0 */
1868 : GEN
1869 490 : ZpX_monic_factor(GEN T, GEN p, long prec)
1870 : {
1871 : GEN Q, P, E, F;
1872 : long L, l, i, v;
1873 :
1874 490 : if (degpol(T) == 1) return mkmat2(mkcol(T), mkcol(gen_1));
1875 490 : v = ZX_valrem(T, &T);
1876 490 : Q = ZX_squff(T, &F); l = lg(Q); L = v? l + 1: l;
1877 490 : P = cgetg(L, t_VEC);
1878 490 : E = cgetg(L, t_VEC);
1879 987 : for (i = 1; i < l; i++)
1880 : {
1881 497 : GEN w = ZpX_monic_factor_squarefree(gel(Q,i), p, prec);
1882 497 : gel(P,i) = w; settyp(w, t_COL);
1883 497 : gel(E,i) = const_col(lg(w)-1, utoipos(F[i]));
1884 : }
1885 490 : if (v) { gel(P,i) = pol_x(varn(T)); gel(E,i) = utoipos(v); }
1886 490 : return mkmat2(shallowconcat1(P), shallowconcat1(E));
1887 : }
1888 :
1889 : /* DT = multiple of disc(T) or NULL
1890 : * Return a multiple of the denominator of an algebraic integer (in Q[X]/(T))
1891 : * when expressed in terms of the power basis */
1892 : GEN
1893 44103 : indexpartial(GEN T, GEN DT)
1894 : {
1895 44103 : pari_sp av = avma;
1896 : long i, nb;
1897 44103 : GEN fa, E, P, U, res = gen_1, dT = ZX_deriv(T);
1898 :
1899 44098 : if (!DT) DT = ZX_disc(T);
1900 44098 : fa = absZ_factor_limit_strict(DT, 0, &U);
1901 44105 : P = gel(fa,1);
1902 44105 : E = gel(fa,2); nb = lg(P)-1;
1903 212636 : for (i = 1; i <= nb; i++)
1904 : {
1905 168546 : long e = itou(gel(E,i)), e2 = e >> 1;
1906 168548 : GEN p = gel(P,i), q = p;
1907 168548 : if (e2 >= 2) q = ZpX_reduced_resultant_fast(T, dT, p, e2);
1908 168549 : res = mulii(res, q);
1909 : }
1910 44090 : if (U)
1911 : {
1912 1936 : long e = itou(gel(U,2)), e2 = e >> 1;
1913 1936 : GEN p = gel(U,1), q = powiu(p, odd(e)? e2+1: e2);
1914 1936 : res = mulii(res, q);
1915 : }
1916 44090 : return gerepileuptoint(av,res);
1917 : }
1918 :
1919 : /*******************************************************************/
1920 : /* */
1921 : /* 2-ELT REPRESENTATION FOR PRIME IDEALS (dividing index) */
1922 : /* */
1923 : /*******************************************************************/
1924 : /* to compute norm of elt in basis form */
1925 : typedef struct {
1926 : long r1;
1927 : GEN M; /* via embed_norm */
1928 :
1929 : GEN D, w, T; /* via resultant if M = NULL */
1930 : } norm_S;
1931 :
1932 : static GEN
1933 506589 : get_norm(norm_S *S, GEN a)
1934 : {
1935 506589 : if (S->M)
1936 : {
1937 : long e;
1938 505175 : GEN N = grndtoi( embed_norm(RgM_RgC_mul(S->M, a), S->r1), &e );
1939 505215 : if (e > -5) pari_err_PREC( "get_norm");
1940 505215 : return N;
1941 : }
1942 1414 : if (S->w) a = RgV_RgC_mul(S->w, a);
1943 1414 : return ZX_resultant_all(S->T, a, S->D, 0);
1944 : }
1945 : static void
1946 212887 : init_norm(norm_S *S, GEN nf, GEN p)
1947 : {
1948 212887 : GEN T = nf_get_pol(nf), M = nf_get_M(nf);
1949 212900 : long N = degpol(T), ex = gexpo(M) + gexpo(mului(8 * N, p));
1950 :
1951 212900 : S->r1 = nf_get_r1(nf);
1952 212902 : if (N * ex <= prec2nbits(gprecision(M)) - 20)
1953 : { /* enough prec to use embed_norm */
1954 212713 : S->M = M;
1955 212713 : S->D = NULL;
1956 212713 : S->w = NULL;
1957 212713 : S->T = NULL;
1958 : }
1959 : else
1960 : {
1961 191 : GEN w = leafcopy(nf_get_zkprimpart(nf)), D = nf_get_zkden(nf), Dp = sqri(p);
1962 : long i;
1963 191 : if (!equali1(D))
1964 : {
1965 191 : GEN w1 = D;
1966 191 : long v = Z_pval(D, p);
1967 191 : D = powiu(p, v);
1968 191 : Dp = mulii(D, Dp);
1969 191 : gel(w, 1) = remii(w1, Dp);
1970 : }
1971 3969 : for (i=2; i<=N; i++) gel(w,i) = FpX_red(gel(w,i), Dp);
1972 191 : S->M = NULL;
1973 191 : S->D = D;
1974 191 : S->w = w;
1975 191 : S->T = T;
1976 : }
1977 212904 : }
1978 : /* f = f(pr/p), q = p^(f+1), a in pr.
1979 : * Return 1 if v_pr(a) = 1, and 0 otherwise */
1980 : static int
1981 506585 : is_uniformizer(GEN a, GEN q, norm_S *S) { return !dvdii(get_norm(S,a), q); }
1982 :
1983 : /* Return x * y, x, y are t_MAT (Fp-basis of in O_K/p), assume (x,y)=1.
1984 : * Either x or y may be NULL (= O_K), not both */
1985 : static GEN
1986 707563 : mul_intersect(GEN x, GEN y, GEN p)
1987 : {
1988 707563 : if (!x) return y;
1989 377694 : if (!y) return x;
1990 267738 : return FpM_intersect_i(x, y, p);
1991 : }
1992 : /* Fp-basis of (ZK/pr): applied to the primes found in primedec_aux()
1993 : * true nf */
1994 : static GEN
1995 309159 : Fp_basis(GEN nf, GEN pr)
1996 : {
1997 : long i, j, l;
1998 : GEN x, y;
1999 : /* already in basis form (from Buchman-Lenstra) ? */
2000 309159 : if (typ(pr) == t_MAT) return pr;
2001 : /* ordinary prid (from Kummer) */
2002 73797 : x = pr_hnf(nf, pr);
2003 73799 : l = lg(x);
2004 73799 : y = cgetg(l, t_MAT);
2005 631800 : for (i=j=1; i<l; i++)
2006 558001 : if (gequal1(gcoeff(x,i,i))) gel(y,j++) = gel(x,i);
2007 73799 : setlg(y, j); return y;
2008 : }
2009 : /* Let Ip = prod_{ P | p } P be the p-radical. The list L contains the
2010 : * P (mod Ip) seen as sub-Fp-vector spaces of ZK/Ip.
2011 : * Return the list of (Ip / P) (mod Ip).
2012 : * N.B: All ideal multiplications are computed as intersections of Fp-vector
2013 : * spaces. true nf */
2014 : static GEN
2015 212914 : get_LV(GEN nf, GEN L, GEN p, long N)
2016 : {
2017 212914 : long i, l = lg(L)-1;
2018 : GEN LV, LW, A, B;
2019 :
2020 212914 : LV = cgetg(l+1, t_VEC);
2021 212913 : if (l == 1) { gel(LV,1) = matid(N); return LV; }
2022 109955 : LW = cgetg(l+1, t_VEC);
2023 419115 : for (i=1; i<=l; i++) gel(LW,i) = Fp_basis(nf, gel(L,i));
2024 :
2025 : /* A[i] = L[1]...L[i-1], i = 2..l */
2026 109958 : A = cgetg(l+1, t_VEC); gel(A,1) = NULL;
2027 309160 : for (i=1; i < l; i++) gel(A,i+1) = mul_intersect(gel(A,i), gel(LW,i), p);
2028 : /* B[i] = L[i+1]...L[l], i = 1..(l-1) */
2029 109956 : B = cgetg(l+1, t_VEC); gel(B,l) = NULL;
2030 309157 : for (i=l; i>=2; i--) gel(B,i-1) = mul_intersect(gel(B,i), gel(LW,i), p);
2031 419115 : for (i=1; i<=l; i++) gel(LV,i) = mul_intersect(gel(A,i), gel(B,i), p);
2032 109957 : return LV;
2033 : }
2034 :
2035 : static void
2036 0 : errprime(GEN p) { pari_err_PRIME("idealprimedec",p); }
2037 :
2038 : /* P = Fp-basis (over O_K/p) for pr.
2039 : * V = Z-basis for I_p/pr. ramif != 0 iff some pr|p is ramified.
2040 : * Return a p-uniformizer for pr. Assume pr not inert, i.e. m > 0 */
2041 : static GEN
2042 296957 : uniformizer(GEN nf, norm_S *S, GEN P, GEN V, GEN p, int ramif)
2043 : {
2044 296957 : long i, l, f, m = lg(P)-1, N = nf_get_degree(nf);
2045 : GEN u, Mv, x, q;
2046 :
2047 296956 : f = N - m; /* we want v_p(Norm(x)) = p^f */
2048 296956 : q = powiu(p,f+1);
2049 :
2050 296921 : u = FpM_FpC_invimage(shallowconcat(P, V), col_ei(N,1), p);
2051 296961 : setlg(u, lg(P));
2052 296961 : u = centermod(ZM_ZC_mul(P, u), p);
2053 296941 : if (is_uniformizer(u, q, S)) return u;
2054 157342 : if (signe(gel(u,1)) <= 0) /* make sure u[1] in ]-p,p] */
2055 131767 : gel(u,1) = addii(gel(u,1), p); /* try u + p */
2056 : else
2057 25575 : gel(u,1) = subii(gel(u,1), p); /* try u - p */
2058 157327 : if (!ramif || is_uniformizer(u, q, S)) return u;
2059 :
2060 : /* P/p ramified, u in P^2, not in Q for all other Q|p */
2061 86098 : Mv = zk_multable(nf, Z_ZC_sub(gen_1,u));
2062 86107 : l = lg(P);
2063 118743 : for (i=1; i<l; i++)
2064 : {
2065 118743 : x = centermod(ZC_add(u, ZM_ZC_mul(Mv, gel(P,i))), p);
2066 118743 : if (is_uniformizer(x, q, S)) return x;
2067 : }
2068 0 : errprime(p);
2069 : return NULL; /* LCOV_EXCL_LINE */
2070 : }
2071 :
2072 : /*******************************************************************/
2073 : /* */
2074 : /* BUCHMANN-LENSTRA ALGORITHM */
2075 : /* */
2076 : /*******************************************************************/
2077 : static GEN
2078 3950704 : mk_pr(GEN p, GEN u, long e, long f, GEN t)
2079 3950704 : { return mkvec5(p, u, utoipos(e), utoipos(f), t); }
2080 :
2081 : /* nf a true nf, u in Z[X]/(T); pr = p Z_K + u Z_K of ramification index e */
2082 : GEN
2083 3492523 : idealprimedec_kummer(GEN nf,GEN u,long e,GEN p)
2084 : {
2085 3492523 : GEN t, T = nf_get_pol(nf);
2086 3492533 : long f = degpol(u), N = degpol(T);
2087 :
2088 3492527 : if (f == N)
2089 : { /* inert */
2090 606480 : u = scalarcol_shallow(p,N);
2091 606512 : t = gen_1;
2092 : }
2093 : else
2094 : {
2095 2886047 : t = centermod(poltobasis(nf, FpX_div(T, u, p)), p);
2096 2885810 : u = centermod(poltobasis(nf, u), p);
2097 2885836 : if (e == 1)
2098 : { /* make sure v_pr(u) = 1 (automatic if e>1) */
2099 2694367 : GEN cw, w = Q_primitive_part(nf_to_scalar_or_alg(nf, u), &cw);
2100 2694527 : long v = cw? f - Q_pval(cw, p) * N: f;
2101 2694526 : if (ZpX_resultant_val(T, w, p, v + 1) > v)
2102 : {
2103 107236 : GEN c = gel(u,1);
2104 107236 : gel(u,1) = signe(c) > 0? subii(c, p): addii(c, p);
2105 : }
2106 : }
2107 2886041 : t = zk_multable(nf, t);
2108 : }
2109 3492498 : return mk_pr(p,u,e,f,t);
2110 : }
2111 :
2112 : typedef struct {
2113 : GEN nf, p;
2114 : long I;
2115 : } eltmod_muldata;
2116 :
2117 : static GEN
2118 826188 : sqr_mod(void *data, GEN x)
2119 : {
2120 826188 : eltmod_muldata *D = (eltmod_muldata*)data;
2121 826188 : return FpC_red(nfsqri(D->nf, x), D->p);
2122 : }
2123 : static GEN
2124 351172 : ei_msqr_mod(void *data, GEN x)
2125 : {
2126 351172 : GEN x2 = sqr_mod(data, x);
2127 351171 : eltmod_muldata *D = (eltmod_muldata*)data;
2128 351171 : return FpC_red(zk_ei_mul(D->nf, x2, D->I), D->p);
2129 : }
2130 : /* nf a true nf; compute lift(nf.zk[I]^p mod p) */
2131 : static GEN
2132 742019 : pow_ei_mod_p(GEN nf, long I, GEN p)
2133 : {
2134 742019 : pari_sp av = avma;
2135 : eltmod_muldata D;
2136 742019 : long N = nf_get_degree(nf);
2137 742019 : GEN y = col_ei(N,I);
2138 742030 : if (I == 1) return y;
2139 526774 : D.nf = nf;
2140 526774 : D.p = p;
2141 526774 : D.I = I;
2142 526774 : y = gen_pow_fold(y, p, (void*)&D, &sqr_mod, &ei_msqr_mod);
2143 526780 : return gerepileupto(av,y);
2144 : }
2145 :
2146 : /* nf a true nf; return a Z basis of Z_K's p-radical, phi = x--> x^p-x */
2147 : static GEN
2148 212909 : pradical(GEN nf, GEN p, GEN *phi)
2149 : {
2150 212909 : long i, N = nf_get_degree(nf);
2151 : GEN q,m,frob,rad;
2152 :
2153 : /* matrix of Frob: x->x^p over Z_K/p */
2154 212910 : frob = cgetg(N+1,t_MAT);
2155 945216 : for (i=1; i<=N; i++) gel(frob,i) = pow_ei_mod_p(nf,i,p);
2156 :
2157 212910 : m = frob; q = p;
2158 304303 : while (abscmpiu(q,N) < 0) { q = mulii(q,p); m = FpM_mul(m, frob, p); }
2159 212909 : rad = FpM_ker(m, p); /* m = Frob^k, s.t p^k >= N */
2160 945179 : for (i=1; i<=N; i++) gcoeff(frob,i,i) = subiu(gcoeff(frob,i,i), 1);
2161 212894 : *phi = frob; return rad;
2162 : }
2163 :
2164 : /* return powers of a: a^0, ... , a^d, d = dim A */
2165 : static GEN
2166 158747 : get_powers(GEN mul, GEN p)
2167 : {
2168 158747 : long i, d = lgcols(mul);
2169 158746 : GEN z, pow = cgetg(d+2,t_MAT), P = pow+1;
2170 :
2171 158745 : gel(P,0) = scalarcol_shallow(gen_1, d-1);
2172 158750 : z = gel(mul,1);
2173 754547 : for (i=1; i<=d; i++)
2174 : {
2175 595802 : gel(P,i) = z; /* a^i */
2176 595802 : if (i!=d) z = FpM_FpC_mul(mul, z, p);
2177 : }
2178 158745 : return pow;
2179 : }
2180 :
2181 : /* minimal polynomial of a in A (dim A = d).
2182 : * mul = multiplication table by a in A */
2183 : static GEN
2184 117699 : pol_min(GEN mul, GEN p)
2185 : {
2186 117699 : pari_sp av = avma;
2187 117699 : GEN z = FpM_deplin(get_powers(mul, p), p);
2188 117700 : return gerepilecopy(av, RgV_to_RgX(z,0));
2189 : }
2190 :
2191 : static GEN
2192 411784 : get_pr(GEN nf, norm_S *S, GEN p, GEN P, GEN V, int ramif, long N, long flim)
2193 : {
2194 : GEN u, t;
2195 : long e, f;
2196 :
2197 411784 : if (typ(P) == t_VEC)
2198 : { /* already done (Kummer) */
2199 73799 : f = pr_get_f(P);
2200 73799 : if (flim > 0 && f > flim) return NULL;
2201 72683 : if (flim == -2) return (GEN)f;
2202 72683 : return P;
2203 : }
2204 337985 : f = N - (lg(P)-1);
2205 337985 : if (flim > 0 && f > flim) return NULL;
2206 336508 : if (flim == -2) return (GEN)f;
2207 : /* P = (p,u) prime. t is an anti-uniformizer: Z_K + t/p Z_K = P^(-1),
2208 : * so that v_P(t) = e(P/p)-1 */
2209 336137 : if (f == N) {
2210 39185 : u = scalarcol_shallow(p,N);
2211 39185 : t = gen_1;
2212 39185 : e = 1;
2213 : } else {
2214 : GEN mt;
2215 296952 : u = uniformizer(nf, S, P, V, p, ramif);
2216 296915 : t = FpM_deplin(zk_multable(nf,u), p);
2217 296955 : mt = zk_multable(nf, t);
2218 296956 : e = ramif? 1 + ZC_nfval(t,mk_pr(p,u,0,0,mt)): 1;
2219 296937 : t = mt;
2220 : }
2221 336122 : return mk_pr(p,u,e,f,t);
2222 : }
2223 :
2224 : /* true nf */
2225 : static GEN
2226 212914 : primedec_end(GEN nf, GEN L, GEN p, long flim)
2227 : {
2228 212914 : long i, j, l = lg(L), N = nf_get_degree(nf);
2229 212914 : GEN LV = get_LV(nf, L,p,N);
2230 212914 : int ramif = dvdii(nf_get_disc(nf), p);
2231 212874 : norm_S S; init_norm(&S, nf, p);
2232 624296 : for (i = j = 1; i < l; i++)
2233 : {
2234 411783 : GEN P = get_pr(nf, &S, p, gel(L,i), gel(LV,i), ramif, N, flim);
2235 411790 : if (!P) continue;
2236 409197 : gel(L,j++) = P;
2237 409197 : if (flim == -1) return P;
2238 : }
2239 212513 : setlg(L, j); return L;
2240 : }
2241 :
2242 : /* prime ideal decomposition of p; if flim>0, restrict to f(P,p) <= flim
2243 : * if flim = -1 return only the first P
2244 : * if flim = -2 return only the f(P/p) in a t_VECSMALL; true nf */
2245 : static GEN
2246 2562936 : primedec_aux(GEN nf, GEN p, long flim)
2247 : {
2248 2562936 : const long TYP = (flim == -2)? t_VECSMALL: t_VEC;
2249 2562936 : GEN E, F, L, Ip, phi, f, g, h, UN, T = nf_get_pol(nf);
2250 : long i, k, c, iL, N;
2251 : int kummer;
2252 :
2253 2562938 : F = FpX_factor(T, p);
2254 2563035 : E = gel(F,2);
2255 2563035 : F = gel(F,1);
2256 :
2257 2563035 : k = lg(F); if (k == 1) errprime(p);
2258 2563035 : if ( !dvdii(nf_get_index(nf),p) ) /* p doesn't divide index */
2259 : {
2260 2348395 : L = cgetg(k, TYP);
2261 5757936 : for (i=1; i<k; i++)
2262 : {
2263 3959206 : GEN t = gel(F,i);
2264 3959206 : long f = degpol(t);
2265 3959205 : if (flim > 0 && f > flim) { setlg(L, i); break; }
2266 3414073 : if (flim == -2)
2267 0 : L[i] = f;
2268 : else
2269 3414073 : gel(L,i) = idealprimedec_kummer(nf, t, E[i],p);
2270 3414247 : if (flim == -1) return gel(L,1);
2271 : }
2272 2343861 : return L;
2273 : }
2274 :
2275 214300 : kummer = 0;
2276 214300 : g = FpXV_prod(F, p);
2277 214300 : h = FpX_div(T,g,p);
2278 214301 : f = FpX_red(ZX_Z_divexact(ZX_sub(ZX_mul(g,h), T), p), p);
2279 :
2280 214291 : N = degpol(T);
2281 214292 : L = cgetg(N+1,TYP);
2282 214293 : iL = 1;
2283 524761 : for (i=1; i<k; i++)
2284 311851 : if (E[i] == 1 || signe(FpX_rem(f,gel(F,i),p)))
2285 73799 : {
2286 75189 : GEN t = gel(F,i);
2287 75189 : kummer = 1;
2288 75189 : gel(L,iL++) = idealprimedec_kummer(nf, t, E[i],p);
2289 75192 : if (flim == -1) return gel(L,1);
2290 : }
2291 : else /* F[i] | (f,g,h), happens at least once by Dedekind criterion */
2292 236669 : E[i] = 0;
2293 :
2294 : /* phi matrix of x -> x^p - x in algebra Z_K/p */
2295 212910 : Ip = pradical(nf,p,&phi);
2296 :
2297 : /* split etale algebra Z_K / (p,Ip) */
2298 212901 : h = cgetg(N+1,t_VEC);
2299 212906 : if (kummer)
2300 : { /* split off Kummer factors */
2301 46533 : GEN mb, b = NULL;
2302 174882 : for (i=1; i<k; i++)
2303 128349 : if (!E[i]) b = b? FpX_mul(b, gel(F,i), p): gel(F,i);
2304 46533 : if (!b) errprime(p);
2305 46533 : b = FpC_red(poltobasis(nf,b), p);
2306 46530 : mb = FpM_red(zk_multable(nf,b), p);
2307 : /* Fp-base of ideal (Ip, b) in ZK/p */
2308 46532 : gel(h,1) = FpM_image(shallowconcat(mb,Ip), p);
2309 : }
2310 : else
2311 166373 : gel(h,1) = Ip;
2312 :
2313 212906 : UN = col_ei(N, 1);
2314 464779 : for (c=1; c; c--)
2315 : { /* Let A:= (Z_K/p) / Ip etale; split A2 := A / Im H ~ Im M2
2316 : H * ? + M2 * Mi2 = Id_N ==> M2 * Mi2 projector A --> A2 */
2317 251863 : GEN M, Mi, M2, Mi2, phi2, mat1, H = gel(h,c); /* maximal rank */
2318 251863 : long dim, r = lg(H)-1;
2319 :
2320 251863 : M = FpM_suppl(shallowconcat(H,UN), p);
2321 251864 : Mi = FpM_inv(M, p);
2322 251863 : M2 = vecslice(M, r+1,N); /* M = (H|M2) invertible */
2323 251864 : Mi2 = rowslice(Mi,r+1,N);
2324 : /* FIXME: FpM_mul(,M2) could be done with vecpermute */
2325 251863 : phi2 = FpM_mul(Mi2, FpM_mul(phi,M2, p), p);
2326 251865 : mat1 = FpM_ker(phi2, p);
2327 251868 : dim = lg(mat1)-1; /* A2 product of 'dim' fields */
2328 251868 : if (dim > 1)
2329 : { /* phi2 v = 0 => a = M2 v in Ker phi, a not in Fp.1 + H */
2330 117701 : GEN R, a, mula, mul2, v = gel(mat1,2);
2331 : long n;
2332 :
2333 117701 : a = FpM_FpC_mul(M2,v, p); /* not a scalar */
2334 117701 : mula = FpM_red(zk_multable(nf,a), p);
2335 117691 : mul2 = FpM_mul(Mi2, FpM_mul(mula,M2, p), p);
2336 117699 : R = FpX_roots(pol_min(mul2,p), p); /* totally split mod p */
2337 117700 : n = lg(R)-1;
2338 360804 : for (i=1; i<=n; i++)
2339 : {
2340 243103 : GEN I = RgM_Rg_sub_shallow(mula, gel(R,i));
2341 243098 : gel(h,c++) = FpM_image(shallowconcat(H, I), p);
2342 : }
2343 117701 : if (n == dim)
2344 304494 : for (i=1; i<=n; i++) gel(L,iL++) = gel(h,--c);
2345 : }
2346 : else /* A2 field ==> H maximal, f = N-r = dim(A2) */
2347 134167 : gel(L,iL++) = H;
2348 : }
2349 212916 : setlg(L, iL);
2350 212914 : return primedec_end(nf, L, p, flim);
2351 : }
2352 :
2353 : GEN
2354 2555988 : idealprimedec_limit_f(GEN nf, GEN p, long f)
2355 : {
2356 2555988 : pari_sp av = avma;
2357 : GEN v;
2358 2555988 : if (typ(p) != t_INT) pari_err_TYPE("idealprimedec",p);
2359 2555988 : if (f < 0) pari_err_DOMAIN("idealprimedec", "f", "<", gen_0, stoi(f));
2360 2555988 : v = primedec_aux(checknf(nf), p, f);
2361 2555912 : v = gen_sort(v, (void*)&cmp_prime_over_p, &cmp_nodata);
2362 2555923 : return gerepileupto(av,v);
2363 : }
2364 : /* true nf */
2365 : GEN
2366 6587 : idealprimedec_galois(GEN nf, GEN p)
2367 : {
2368 6587 : pari_sp av = avma;
2369 6587 : GEN v = primedec_aux(nf, p, -1);
2370 6587 : return gerepilecopy(av,v);
2371 : }
2372 : /* true nf */
2373 : GEN
2374 364 : idealprimedec_degrees(GEN nf, GEN p)
2375 : {
2376 364 : pari_sp av = avma;
2377 364 : GEN v = primedec_aux(nf, p, -2);
2378 364 : vecsmall_sort(v); return gerepileuptoleaf(av, v);
2379 : }
2380 : GEN
2381 496036 : idealprimedec_limit_norm(GEN nf, GEN p, GEN B)
2382 496036 : { return idealprimedec_limit_f(nf, p, logint(B,p)); }
2383 : GEN
2384 1140582 : idealprimedec(GEN nf, GEN p)
2385 1140582 : { return idealprimedec_limit_f(nf, p, 0); }
2386 : GEN
2387 9974 : nf_pV_to_prV(GEN nf, GEN P)
2388 : {
2389 : long i, l;
2390 9974 : GEN Q = cgetg_copy(P,&l);
2391 9973 : if (l == 1) return Q;
2392 7070 : for (i = 1; i < l; i++) gel(Q,i) = idealprimedec(nf, gel(P,i));
2393 2114 : return shallowconcat1(Q);
2394 : }
2395 :
2396 : /* return [Fp[x]: Fp] */
2397 : static long
2398 4151 : ffdegree(GEN x, GEN frob, GEN p)
2399 : {
2400 4151 : pari_sp av = avma;
2401 4151 : long d, f = lg(frob)-1;
2402 4151 : GEN y = x;
2403 :
2404 13363 : for (d=1; d < f; d++)
2405 : {
2406 11018 : y = FpM_FpC_mul(frob, y, p);
2407 11018 : if (ZV_equal(y, x)) break;
2408 : }
2409 4151 : return gc_long(av,d);
2410 : }
2411 :
2412 : static GEN
2413 90572 : lift_to_zk(GEN v, GEN c, long N)
2414 : {
2415 90572 : GEN w = zerocol(N);
2416 90571 : long i, l = lg(c);
2417 302991 : for (i=1; i<l; i++) gel(w,c[i]) = gel(v,i);
2418 90571 : return w;
2419 : }
2420 :
2421 : /* return t = 1 mod pr, t = 0 mod p / pr^e(pr/p) */
2422 : static GEN
2423 773883 : anti_uniformizer(GEN nf, GEN pr)
2424 : {
2425 773883 : long N = nf_get_degree(nf), e = pr_get_e(pr);
2426 : GEN p, b, z;
2427 :
2428 773861 : if (e * pr_get_f(pr) == N) return gen_1;
2429 358761 : p = pr_get_p(pr);
2430 358760 : b = pr_get_tau(pr); /* ZM */
2431 358761 : if (e != 1)
2432 : {
2433 22659 : GEN q = powiu(pr_get_p(pr), e-1);
2434 22659 : b = ZM_Z_divexact(ZM_powu(b,e), q);
2435 : }
2436 : /* b = tau^e / p^(e-1), v_pr(b) = 0, v_Q(b) >= e(Q/p) for other Q | p */
2437 358761 : z = ZM_hnfmodid(FpM_red(b,p), p); /* ideal (p) / pr^e, coprime to pr */
2438 358786 : z = idealaddtoone_raw(nf, pr, z);
2439 358774 : return Z_ZC_sub(gen_1, FpC_center(FpC_red(z,p), p, shifti(p,-1)));
2440 : }
2441 :
2442 : #define mpr_TAU 1
2443 : #define mpr_FFP 2
2444 : #define mpr_NFP 5
2445 : #define SMALLMODPR 4
2446 : #define LARGEMODPR 6
2447 : static GEN
2448 3326582 : modpr_TAU(GEN modpr)
2449 : {
2450 3326582 : GEN tau = gel(modpr,mpr_TAU);
2451 3326582 : return isintzero(tau)? NULL: tau;
2452 : }
2453 :
2454 : /* prh = HNF matrix, which is identity but for the first line. Return a
2455 : * projector to ZK / prh ~ Z/prh[1,1] */
2456 : GEN
2457 746357 : dim1proj(GEN prh)
2458 : {
2459 746357 : long i, N = lg(prh)-1;
2460 746357 : GEN ffproj = cgetg(N+1, t_VEC);
2461 746362 : GEN x, q = gcoeff(prh,1,1);
2462 746362 : gel(ffproj,1) = gen_1;
2463 1526784 : for (i=2; i<=N; i++)
2464 : {
2465 780522 : x = gcoeff(prh,1,i);
2466 780522 : if (signe(x)) x = subii(q,x);
2467 780422 : gel(ffproj,i) = x;
2468 : }
2469 746262 : return ffproj;
2470 : }
2471 :
2472 : /* p not necessarily prime, but coprime to denom(basis) */
2473 : GEN
2474 203 : QXQV_to_FpM(GEN basis, GEN T, GEN p)
2475 : {
2476 203 : long i, l = lg(basis), f = degpol(T);
2477 203 : GEN z = cgetg(l, t_MAT);
2478 4515 : for (i = 1; i < l; i++)
2479 : {
2480 4312 : GEN w = gel(basis,i);
2481 4312 : if (typ(w) == t_INT)
2482 0 : w = scalarcol_shallow(w, f);
2483 : else
2484 : {
2485 : GEN dx;
2486 4312 : w = Q_remove_denom(w, &dx);
2487 4312 : w = FpXQ_red(w, T, p);
2488 4312 : if (dx)
2489 : {
2490 0 : dx = Fp_inv(dx, p);
2491 0 : if (!equali1(dx)) w = FpX_Fp_mul(w, dx, p);
2492 : }
2493 4312 : w = RgX_to_RgC(w, f);
2494 : }
2495 4312 : gel(z,i) = w; /* w_i mod (T,p) */
2496 : }
2497 203 : return z;
2498 : }
2499 :
2500 : /* initialize reduction mod pr; if zk = 1, will only init data required to
2501 : * reduce *integral* element. Realize (O_K/pr) as Fp[X] / (T), for a
2502 : * *monic* T; use variable vT for varn(T) */
2503 : static GEN
2504 833089 : modprinit(GEN nf, GEN pr, int zk, long vT)
2505 : {
2506 833089 : pari_sp av = avma;
2507 : GEN res, tau, mul, x, p, T, pow, ffproj, nfproj, prh, c;
2508 : long N, i, k, f;
2509 :
2510 833089 : nf = checknf(nf); checkprid(pr);
2511 833057 : if (vT < 0) vT = nf_get_varn(nf);
2512 833051 : f = pr_get_f(pr);
2513 833043 : N = nf_get_degree(nf);
2514 833033 : prh = pr_hnf(nf, pr);
2515 833075 : tau = zk? gen_0: anti_uniformizer(nf, pr);
2516 833032 : p = pr_get_p(pr);
2517 :
2518 833032 : if (f == 1)
2519 : {
2520 728328 : res = cgetg(SMALLMODPR, t_COL);
2521 728331 : gel(res,mpr_TAU) = tau;
2522 728331 : gel(res,mpr_FFP) = dim1proj(prh);
2523 728257 : gel(res,3) = pr; return gerepilecopy(av, res);
2524 : }
2525 :
2526 104704 : c = cgetg(f+1, t_VECSMALL);
2527 104713 : ffproj = cgetg(N+1, t_MAT);
2528 434693 : for (k=i=1; i<=N; i++)
2529 : {
2530 329980 : x = gcoeff(prh, i,i);
2531 329980 : if (!is_pm1(x)) { c[k] = i; gel(ffproj,i) = col_ei(N, i); k++; }
2532 : else
2533 92696 : gel(ffproj,i) = ZC_neg(gel(prh,i));
2534 : }
2535 104713 : ffproj = rowpermute(ffproj, c);
2536 104713 : if (! dvdii(nf_get_index(nf), p))
2537 : {
2538 63665 : GEN basis = nf_get_zkprimpart(nf), D = nf_get_zkden(nf);
2539 63665 : if (N == f)
2540 : { /* pr inert */
2541 39367 : T = nf_get_pol(nf);
2542 39367 : T = FpX_red(T,p);
2543 39367 : ffproj = RgV_to_RgM(basis, lg(basis)-1);
2544 : }
2545 : else
2546 : {
2547 24298 : T = RgV_RgC_mul(basis, pr_get_gen(pr));
2548 24298 : T = FpX_normalize(FpX_red(T,p),p);
2549 24298 : basis = FqV_red(vecpermute(basis,c), T, p);
2550 24298 : basis = RgV_to_RgM(basis, lg(basis)-1);
2551 24298 : ffproj = ZM_mul(basis, ffproj);
2552 : }
2553 63665 : setvarn(T, vT);
2554 63665 : ffproj = FpM_red(ffproj, p);
2555 63664 : if (!equali1(D))
2556 : {
2557 33907 : D = modii(D,p);
2558 33907 : if (!equali1(D)) ffproj = FpM_Fp_mul(ffproj, Fp_inv(D,p), p);
2559 : }
2560 :
2561 63665 : res = cgetg(SMALLMODPR+1, t_COL);
2562 63665 : gel(res,mpr_TAU) = tau;
2563 63665 : gel(res,mpr_FFP) = ffproj;
2564 63665 : gel(res,3) = pr;
2565 63665 : gel(res,4) = T; return gerepilecopy(av, res);
2566 : }
2567 :
2568 41048 : if (uisprime(f))
2569 : {
2570 38703 : mul = ei_multable(nf, c[2]);
2571 38703 : mul = vecpermute(mul, c);
2572 : }
2573 : else
2574 : {
2575 : GEN v, u, u2, frob;
2576 : long deg,deg1,deg2;
2577 :
2578 : /* matrix of Frob: x->x^p over Z_K/pr = < w[c1], ..., w[cf] > over Fp */
2579 2345 : frob = cgetg(f+1, t_MAT);
2580 12061 : for (i=1; i<=f; i++)
2581 : {
2582 9716 : x = pow_ei_mod_p(nf,c[i],p);
2583 9716 : gel(frob,i) = FpM_FpC_mul(ffproj, x, p);
2584 : }
2585 2345 : u = col_ei(f,2); k = 2;
2586 2345 : deg1 = ffdegree(u, frob, p);
2587 4130 : while (deg1 < f)
2588 : {
2589 1785 : k++; u2 = col_ei(f, k);
2590 1785 : deg2 = ffdegree(u2, frob, p);
2591 1785 : deg = ulcm(deg1,deg2);
2592 1785 : if (deg == deg1) continue;
2593 1778 : if (deg == deg2) { deg1 = deg2; u = u2; continue; }
2594 21 : u = ZC_add(u, u2);
2595 21 : while (ffdegree(u, frob, p) < deg) u = ZC_add(u, u2);
2596 21 : deg1 = deg;
2597 : }
2598 2345 : v = lift_to_zk(u,c,N);
2599 :
2600 2345 : mul = cgetg(f+1,t_MAT);
2601 2345 : gel(mul,1) = v; /* assume w_1 = 1 */
2602 9716 : for (i=2; i<=f; i++) gel(mul,i) = zk_ei_mul(nf,v,c[i]);
2603 : }
2604 :
2605 : /* Z_K/pr = Fp(v), mul = mul by v */
2606 41048 : mul = FpM_red(mul, p);
2607 41048 : mul = FpM_mul(ffproj, mul, p);
2608 :
2609 41048 : pow = get_powers(mul, p);
2610 41048 : T = RgV_to_RgX(FpM_deplin(pow, p), vT);
2611 41047 : nfproj = cgetg(f+1, t_MAT);
2612 129275 : for (i=1; i<=f; i++) gel(nfproj,i) = lift_to_zk(gel(pow,i), c, N);
2613 :
2614 41048 : setlg(pow, f+1);
2615 41048 : ffproj = FpM_mul(FpM_inv(pow, p), ffproj, p);
2616 :
2617 41048 : res = cgetg(LARGEMODPR, t_COL);
2618 41048 : gel(res,mpr_TAU) = tau;
2619 41048 : gel(res,mpr_FFP) = ffproj;
2620 41048 : gel(res,3) = pr;
2621 41048 : gel(res,4) = T;
2622 41048 : gel(res,mpr_NFP) = nfproj; return gerepilecopy(av, res);
2623 : }
2624 :
2625 : GEN
2626 7 : nfmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 0, -1); }
2627 : GEN
2628 40595 : zkmodprinit(GEN nf, GEN pr) { return modprinit(nf, pr, 1, -1); }
2629 : GEN
2630 70 : nfmodprinit0(GEN nf, GEN pr, long v) { return modprinit(nf, pr, 0, v); }
2631 :
2632 : /* x may be a modpr */
2633 : static int
2634 3870679 : ok_modpr(GEN x)
2635 3870679 : { return typ(x) == t_COL && lg(x) >= SMALLMODPR && lg(x) <= LARGEMODPR; }
2636 : void
2637 210 : checkmodpr(GEN x)
2638 : {
2639 210 : if (!ok_modpr(x)) pari_err_TYPE("checkmodpr [use nfmodprinit]", x);
2640 210 : checkprid(modpr_get_pr(x));
2641 210 : }
2642 : GEN
2643 3549 : get_modpr(GEN x)
2644 3549 : { return ok_modpr(x)? x: NULL; }
2645 :
2646 : int
2647 9188316 : checkprid_i(GEN x)
2648 : {
2649 8495810 : return (typ(x) == t_VEC && lg(x) == 6
2650 8458627 : && typ(gel(x,2)) == t_COL && typ(gel(x,3)) == t_INT
2651 17684126 : && typ(gel(x,5)) != t_COL); /* tau changed to t_MAT/t_INT in 2.6 */
2652 : }
2653 : void
2654 7901126 : checkprid(GEN x)
2655 7901126 : { if (!checkprid_i(x)) pari_err_TYPE("checkprid",x); }
2656 : GEN
2657 748216 : get_prid(GEN x)
2658 : {
2659 748216 : long lx = lg(x);
2660 748216 : if (lx == 3 && typ(x) == t_VEC) x = gel(x,1);
2661 748216 : if (checkprid_i(x)) return x;
2662 534723 : if (ok_modpr(x)) {
2663 2989 : x = modpr_get_pr(x);
2664 2989 : if (checkprid_i(x)) return x;
2665 : }
2666 531734 : return NULL;
2667 : }
2668 :
2669 : static GEN
2670 3332203 : to_ff_init(GEN nf, GEN *pr, GEN *T, GEN *p, int zk)
2671 : {
2672 3332203 : GEN modpr = ok_modpr(*pr)? *pr: modprinit(nf, *pr, zk, -1);
2673 3332285 : *T = modpr_get_T(modpr);
2674 3332224 : *pr = modpr_get_pr(modpr);
2675 3332218 : *p = pr_get_p(*pr); return modpr;
2676 : }
2677 :
2678 : /* Return an element of O_K which is set to x Mod T */
2679 : GEN
2680 4361 : modpr_genFq(GEN modpr)
2681 : {
2682 4361 : switch(lg(modpr))
2683 : {
2684 917 : case SMALLMODPR: /* Fp */
2685 917 : return gen_1;
2686 1582 : case LARGEMODPR: /* painful case, p \mid index */
2687 1582 : return gmael(modpr,mpr_NFP, 2);
2688 1862 : default: /* trivial case : p \nmid index */
2689 : {
2690 1862 : long v = varn( modpr_get_T(modpr) );
2691 1862 : return pol_x(v);
2692 : }
2693 : }
2694 : }
2695 :
2696 : GEN
2697 3313604 : nf_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
2698 3313604 : GEN modpr = to_ff_init(nf,pr,T,p,0);
2699 3313630 : GEN tau = modpr_TAU(modpr);
2700 3313581 : if (!tau) gel(modpr,mpr_TAU) = anti_uniformizer(nf, *pr);
2701 3313581 : return modpr;
2702 : }
2703 : GEN
2704 18579 : zk_to_Fq_init(GEN nf, GEN *pr, GEN *T, GEN *p) {
2705 18579 : return to_ff_init(nf,pr,T,p,1);
2706 : }
2707 :
2708 : /* assume x in 'basis' form (t_COL) */
2709 : GEN
2710 3257732 : zk_to_Fq(GEN x, GEN modpr)
2711 : {
2712 3257732 : GEN pr = modpr_get_pr(modpr), p = pr_get_p(pr);
2713 3257757 : GEN ffproj = gel(modpr,mpr_FFP);
2714 3257757 : GEN T = modpr_get_T(modpr);
2715 3257778 : return T? FpM_FpC_mul_FpX(ffproj,x, p, varn(T)): FpV_dotproduct(ffproj,x, p);
2716 : }
2717 :
2718 : /* REDUCTION Modulo a prime ideal */
2719 :
2720 : /* nf a true nf */
2721 : static GEN
2722 9175336 : Rg_to_ff(GEN nf, GEN x0, GEN modpr)
2723 : {
2724 9175336 : GEN x = x0, den, pr = modpr_get_pr(modpr), p = pr_get_p(pr);
2725 9175343 : long tx = typ(x);
2726 :
2727 9175343 : if (tx == t_POLMOD) { x = gel(x,2); tx = typ(x); }
2728 9175343 : switch(tx)
2729 : {
2730 5999976 : case t_INT: return modii(x, p);
2731 6832 : case t_FRAC: return Rg_to_Fp(x, p);
2732 207821 : case t_POL:
2733 207821 : switch(lg(x))
2734 : {
2735 224 : case 2: return gen_0;
2736 25785 : case 3: return Rg_to_Fp(gel(x,2), p);
2737 : }
2738 181812 : x = Q_remove_denom(x, &den);
2739 181813 : x = poltobasis(nf, x);
2740 : /* content(x) and den may not be coprime */
2741 181604 : break;
2742 2960757 : case t_COL:
2743 2960757 : x = Q_remove_denom(x, &den);
2744 : /* content(x) and den are coprime */
2745 2960753 : if (lg(x)-1 == nf_get_degree(nf)) break;
2746 48 : default: pari_err_TYPE("Rg_to_ff",x);
2747 : return NULL;/*LCOV_EXCL_LINE*/
2748 : }
2749 3142301 : if (den)
2750 : {
2751 135476 : long v = Z_pvalrem(den, p, &den);
2752 135476 : if (v)
2753 : {
2754 13533 : if (tx == t_POL) v -= ZV_pvalrem(x, p, &x);
2755 : /* now v = valuation(true denominator of x) */
2756 13533 : if (v > 0)
2757 : {
2758 12959 : GEN tau = modpr_TAU(modpr);
2759 12959 : if (!tau) pari_err_TYPE("zk_to_ff", x0);
2760 12959 : x = nfmuli(nf,x, nfpow_u(nf, tau, v));
2761 12959 : v -= ZV_pvalrem(x, p, &x);
2762 : }
2763 13533 : if (v > 0) pari_err_INV("Rg_to_ff", mkintmod(gen_0,p));
2764 13505 : if (v) return gen_0;
2765 8696 : if (is_pm1(den)) den = NULL;
2766 : }
2767 130639 : x = FpC_red(x, p);
2768 : }
2769 3137464 : x = zk_to_Fq(x, modpr);
2770 3137518 : if (den)
2771 : {
2772 125317 : GEN c = Fp_inv(den, p);
2773 125319 : x = typ(x) == t_INT? Fp_mul(x,c,p): FpX_Fp_mul(x,c,p);
2774 : }
2775 3137520 : return x;
2776 : }
2777 :
2778 : GEN
2779 210 : nfreducemodpr(GEN nf, GEN x, GEN modpr)
2780 : {
2781 210 : pari_sp av = avma;
2782 210 : nf = checknf(nf); checkmodpr(modpr);
2783 210 : return gerepileupto(av, algtobasis(nf, Fq_to_nf(Rg_to_ff(nf,x,modpr),modpr)));
2784 : }
2785 :
2786 : GEN
2787 329 : nfmodpr(GEN nf, GEN x, GEN pr)
2788 : {
2789 329 : pari_sp av = avma;
2790 : GEN T, p, modpr;
2791 329 : nf = checknf(nf);
2792 329 : modpr = nf_to_Fq_init(nf, &pr, &T, &p);
2793 322 : if (typ(x) == t_MAT && lg(x) == 3)
2794 : {
2795 35 : GEN y, v = famat_nfvalrem(nf, x, pr, &y);
2796 35 : long s = signe(v);
2797 35 : if (s < 0) pari_err_INV("Rg_to_ff", mkintmod(gen_0,p));
2798 28 : if (s > 0) return gc_const(av, gen_0);
2799 14 : x = FqV_factorback(nfV_to_FqV(gel(y,1), nf, modpr), gel(y,2), T, p);
2800 14 : return gerepileupto(av, x);
2801 : }
2802 287 : x = Rg_to_ff(nf, x, modpr);
2803 175 : x = Fq_to_FF(x, Tp_to_FF(T,p));
2804 175 : return gerepilecopy(av, x);
2805 : }
2806 : GEN
2807 77 : nfmodprlift(GEN nf, GEN x, GEN pr)
2808 : {
2809 77 : pari_sp av = avma;
2810 : GEN y, T, p, modpr;
2811 : long i, l, d;
2812 77 : nf = checknf(nf);
2813 77 : switch(typ(x))
2814 : {
2815 7 : case t_INT: return icopy(x);
2816 42 : case t_FFELT: break;
2817 28 : case t_VEC: case t_COL: case t_MAT:
2818 28 : y = cgetg_copy(x,&l);
2819 63 : for (i = 1; i < l; i++) gel(y,i) = nfmodprlift(nf,gel(x,i),pr);
2820 28 : return y;
2821 0 : default: pari_err_TYPE("nfmodprlit",x);
2822 : }
2823 42 : x = FF_to_FpXQ(x);
2824 42 : setvarn(x, nf_get_varn(nf));
2825 42 : d = degpol(x);
2826 42 : if (d <= 0) { set_avma(av); return d? gen_0: icopy(gel(x,2)); }
2827 14 : modpr = nf_to_Fq_init(nf, &pr, &T, &p);
2828 14 : return gerepilecopy(av, Fq_to_nf(x, modpr));
2829 : }
2830 :
2831 : /* lift A from residue field to nf */
2832 : GEN
2833 1464190 : Fq_to_nf(GEN A, GEN modpr)
2834 : {
2835 : long dA;
2836 1464190 : if (typ(A) == t_INT || lg(modpr) < LARGEMODPR) return A;
2837 43654 : dA = degpol(A);
2838 43654 : if (dA <= 0) return dA ? gen_0: gel(A,2);
2839 39921 : return ZM_ZX_mul(gel(modpr,mpr_NFP), A);
2840 : }
2841 : GEN
2842 0 : FqV_to_nfV(GEN x, GEN modpr)
2843 0 : { pari_APPLY_same(Fq_to_nf(gel(x,i), modpr)) }
2844 : GEN
2845 9994 : FqM_to_nfM(GEN A, GEN modpr)
2846 : {
2847 9994 : long i,j,h,l = lg(A);
2848 9994 : GEN B = cgetg(l, t_MAT);
2849 :
2850 9994 : if (l == 1) return B;
2851 9295 : h = lgcols(A);
2852 42060 : for (j=1; j<l; j++)
2853 : {
2854 32765 : GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
2855 216028 : for (i=1; i<h; i++) gel(Bj,i) = Fq_to_nf(gel(Aj,i), modpr);
2856 : }
2857 9295 : return B;
2858 : }
2859 : GEN
2860 11591 : FqX_to_nfX(GEN A, GEN modpr)
2861 : {
2862 : long i, l;
2863 : GEN B;
2864 :
2865 11591 : if (typ(A)!=t_POL) return icopy(A); /* scalar */
2866 11591 : B = cgetg_copy(A, &l); B[1] = A[1];
2867 51734 : for (i=2; i<l; i++) gel(B,i) = Fq_to_nf(gel(A,i), modpr);
2868 11591 : return B;
2869 : }
2870 :
2871 : /* reduce A to residue field */
2872 : GEN
2873 9174844 : nf_to_Fq(GEN nf, GEN A, GEN modpr)
2874 : {
2875 9174844 : pari_sp av = avma;
2876 9174844 : return gerepileupto(av, Rg_to_ff(checknf(nf), A, modpr));
2877 : }
2878 : /* A t_VEC/t_COL */
2879 : GEN
2880 4482 : nfV_to_FqV(GEN A, GEN nf,GEN modpr)
2881 : {
2882 4482 : long i,l = lg(A);
2883 4482 : GEN B = cgetg(l,typ(A));
2884 25385 : for (i=1; i<l; i++) gel(B,i) = nf_to_Fq(nf,gel(A,i), modpr);
2885 4482 : return B;
2886 : }
2887 : /* A t_MAT */
2888 : GEN
2889 5417 : nfM_to_FqM(GEN A, GEN nf,GEN modpr)
2890 : {
2891 5417 : long i,j,h,l = lg(A);
2892 5417 : GEN B = cgetg(l,t_MAT);
2893 :
2894 5417 : if (l == 1) return B;
2895 5417 : h = lgcols(A);
2896 154793 : for (j=1; j<l; j++)
2897 : {
2898 149376 : GEN Aj = gel(A,j), Bj = cgetg(h,t_COL); gel(B,j) = Bj;
2899 1054580 : for (i=1; i<h; i++) gel(Bj,i) = nf_to_Fq(nf, gel(Aj,i), modpr);
2900 : }
2901 5417 : return B;
2902 : }
2903 : /* A t_POL */
2904 : GEN
2905 9737 : nfX_to_FqX(GEN A, GEN nf,GEN modpr)
2906 : {
2907 9737 : long i,l = lg(A);
2908 9737 : GEN B = cgetg(l,t_POL); B[1] = A[1];
2909 54767 : for (i=2; i<l; i++) gel(B,i) = nf_to_Fq(nf,gel(A,i),modpr);
2910 9730 : return normalizepol_lg(B, l);
2911 : }
2912 :
2913 : /*******************************************************************/
2914 : /* */
2915 : /* RELATIVE ROUND 2 */
2916 : /* */
2917 : /*******************************************************************/
2918 : /* Shallow functions */
2919 : /* FIXME: use a bb_field and export the nfX_* routines */
2920 : static GEN
2921 4396 : nfX_sub(GEN nf, GEN x, GEN y)
2922 : {
2923 4396 : long i, lx = lg(x), ly = lg(y);
2924 : GEN z;
2925 4396 : if (ly <= lx) {
2926 4396 : z = cgetg(lx,t_POL); z[1] = x[1];
2927 27314 : for (i=2; i < ly; i++) gel(z,i) = nfsub(nf,gel(x,i),gel(y,i));
2928 4396 : for ( ; i < lx; i++) gel(z,i) = gel(x,i);
2929 4396 : z = normalizepol_lg(z, lx);
2930 : } else {
2931 0 : z = cgetg(ly,t_POL); z[1] = y[1];
2932 0 : for (i=2; i < lx; i++) gel(z,i) = nfsub(nf,gel(x,i),gel(y,i));
2933 0 : for ( ; i < ly; i++) gel(z,i) = gneg(gel(y,i));
2934 0 : z = normalizepol_lg(z, ly);
2935 : }
2936 4396 : return z;
2937 : }
2938 : /* FIXME: quadratic multiplication */
2939 : static GEN
2940 65736 : nfX_mul(GEN nf, GEN a, GEN b)
2941 : {
2942 65736 : long da = degpol(a), db = degpol(b), dc, lc, k;
2943 : GEN c;
2944 65736 : if (da < 0 || db < 0) return gen_0;
2945 65736 : dc = da + db;
2946 65736 : if (dc == 0) return nfmul(nf, gel(a,2),gel(b,2));
2947 65736 : lc = dc+3;
2948 65736 : c = cgetg(lc, t_POL); c[1] = a[1];
2949 527908 : for (k = 0; k <= dc; k++)
2950 : {
2951 462172 : long i, I = minss(k, da);
2952 462172 : GEN d = NULL;
2953 1573894 : for (i = maxss(k-db, 0); i <= I; i++)
2954 : {
2955 1111722 : GEN e = nfmul(nf, gel(a, i+2), gel(b, k-i+2));
2956 1111722 : d = d? nfadd(nf, d, e): e;
2957 : }
2958 462172 : gel(c, k+2) = d;
2959 : }
2960 65736 : return normalizepol_lg(c, lc);
2961 : }
2962 : /* assume b monic */
2963 : static GEN
2964 61340 : nfX_rem(GEN nf, GEN a, GEN b)
2965 : {
2966 61340 : long da = degpol(a), db = degpol(b);
2967 61340 : if (da < 0) return gen_0;
2968 61340 : a = leafcopy(a);
2969 147124 : while (da >= db)
2970 : {
2971 85784 : long i, k = da;
2972 85784 : GEN A = gel(a, k+2);
2973 626035 : for (i = db-1, k--; i >= 0; i--, k--)
2974 540251 : gel(a,k+2) = nfsub(nf, gel(a,k+2), nfmul(nf, A, gel(b,i+2)));
2975 85784 : a = normalizepol_lg(a, lg(a)-1);
2976 85784 : da = degpol(a);
2977 : }
2978 61340 : return a;
2979 : }
2980 : static GEN
2981 61340 : nfXQ_mul(GEN nf, GEN a, GEN b, GEN T)
2982 : {
2983 61340 : GEN c = nfX_mul(nf, a, b);
2984 61340 : if (typ(c) != t_POL) return c;
2985 61340 : return nfX_rem(nf, c, T);
2986 : }
2987 :
2988 : static void
2989 12752 : fill(long l, GEN H, GEN Hx, GEN I, GEN Ix)
2990 : {
2991 : long i;
2992 12752 : if (typ(Ix) == t_VEC) /* standard */
2993 47268 : for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = gel(Ix,i); }
2994 : else /* constant ideal */
2995 12837 : for (i=1; i<l; i++) { gel(H,i) = gel(Hx,i); gel(I,i) = Ix; }
2996 12752 : }
2997 :
2998 : /* given MODULES x and y by their pseudo-bases, returns a pseudo-basis of the
2999 : * module generated by x and y. */
3000 : static GEN
3001 6376 : rnfjoinmodules_i(GEN nf, GEN Hx, GEN Ix, GEN Hy, GEN Iy)
3002 : {
3003 6376 : long lx = lg(Hx), ly = lg(Hy), l = lx+ly-1;
3004 6376 : GEN H = cgetg(l, t_MAT), I = cgetg(l, t_VEC);
3005 6376 : fill(lx, H , Hx, I , Ix);
3006 6376 : fill(ly, H+lx-1, Hy, I+lx-1, Iy); return nfhnf(nf, mkvec2(H, I));
3007 : }
3008 : static GEN
3009 2190 : rnfjoinmodules(GEN nf, GEN x, GEN y)
3010 : {
3011 2190 : if (!x) return y;
3012 1379 : if (!y) return x;
3013 1379 : return rnfjoinmodules_i(nf, gel(x,1), gel(x,2), gel(y,1), gel(y,2));
3014 : }
3015 :
3016 : typedef struct {
3017 : GEN multab, T,p;
3018 : long h;
3019 : } rnfeltmod_muldata;
3020 :
3021 : static GEN
3022 70635 : _sqr(void *data, GEN x)
3023 : {
3024 70635 : rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
3025 49356 : GEN z = x? tablesqr(D->multab,x)
3026 70635 : : tablemul_ei_ej(D->multab,D->h,D->h);
3027 70635 : return FqV_red(z,D->T,D->p);
3028 : }
3029 : static GEN
3030 11648 : _msqr(void *data, GEN x)
3031 : {
3032 11648 : GEN x2 = _sqr(data, x), z;
3033 11648 : rnfeltmod_muldata *D = (rnfeltmod_muldata *) data;
3034 11648 : z = tablemul_ei(D->multab, x2, D->h);
3035 11648 : return FqV_red(z,D->T,D->p);
3036 : }
3037 :
3038 : /* Compute W[h]^n mod (T,p) in the extension, assume n >= 0. T a ZX */
3039 : static GEN
3040 21279 : rnfeltid_powmod(GEN multab, long h, GEN n, GEN T, GEN p)
3041 : {
3042 21279 : pari_sp av = avma;
3043 : GEN y;
3044 : rnfeltmod_muldata D;
3045 :
3046 21279 : if (!signe(n)) return gen_1;
3047 :
3048 21279 : D.multab = multab;
3049 21279 : D.h = h;
3050 21279 : D.T = T;
3051 21279 : D.p = p;
3052 21279 : y = gen_pow_fold(NULL, n, (void*)&D, &_sqr, &_msqr);
3053 21279 : return gerepilecopy(av, y);
3054 : }
3055 :
3056 : /* P != 0 has at most degpol(P) roots. Look for an element in Fq which is not
3057 : * a root, cf repres() */
3058 : static GEN
3059 21 : FqX_non_root(GEN P, GEN T, GEN p)
3060 : {
3061 21 : long dP = degpol(P), f, vT;
3062 : long i, j, k, pi, pp;
3063 : GEN v;
3064 :
3065 21 : if (dP == 0) return gen_1;
3066 21 : pp = is_bigint(p) ? dP+1: itos(p);
3067 21 : v = cgetg(dP + 2, t_VEC);
3068 21 : gel(v,1) = gen_0;
3069 21 : if (T)
3070 0 : { f = degpol(T); vT = varn(T); }
3071 : else
3072 21 : { f = 1; vT = 0; }
3073 42 : for (i=pi=1; i<=f; i++,pi*=pp)
3074 : {
3075 21 : GEN gi = i == 1? gen_1: pol_xn(i-1, vT), jgi = gi;
3076 42 : for (j=1; j<pp; j++)
3077 : {
3078 42 : for (k=1; k<=pi; k++)
3079 : {
3080 21 : GEN z = Fq_add(gel(v,k), jgi, T,p);
3081 21 : if (!gequal0(FqX_eval(P, z, T,p))) return z;
3082 21 : gel(v, j*pi+k) = z;
3083 : }
3084 21 : if (j < pp-1) jgi = Fq_add(jgi, gi, T,p); /* j*g[i] */
3085 : }
3086 : }
3087 21 : return NULL;
3088 : }
3089 :
3090 : /* Relative Dedekind criterion over (true) nf, applied to the order defined by a
3091 : * root of monic irreducible polynomial P, modulo the prime ideal pr. Assume
3092 : * vdisc = v_pr( disc(P) ).
3093 : * Return NULL if nf[X]/P is pr-maximal. Otherwise, return [flag, O, v]:
3094 : * O = enlarged order, given by a pseudo-basis
3095 : * flag = 1 if O is proven pr-maximal (may be 0 and O nevertheless pr-maximal)
3096 : * v = v_pr(disc(O)). */
3097 : static GEN
3098 4431 : rnfdedekind_i(GEN nf, GEN P, GEN pr, long vdisc, long only_maximal)
3099 : {
3100 : GEN Ppr, A, I, p, tau, g, h, k, base, T, gzk, hzk, prinvp, pal, nfT, modpr;
3101 : long m, vt, r, d, i, j, mpr;
3102 :
3103 4431 : if (vdisc < 0) pari_err_TYPE("rnfdedekind [non integral pol]", P);
3104 4424 : if (vdisc == 1) return NULL; /* pr-maximal */
3105 4424 : if (!only_maximal && !gequal1(leading_coeff(P)))
3106 0 : pari_err_IMPL( "the full Dedekind criterion in the nonmonic case");
3107 : /* either monic OR only_maximal = 1 */
3108 4424 : m = degpol(P);
3109 4424 : nfT = nf_get_pol(nf);
3110 4424 : modpr = nf_to_Fq_init(nf,&pr, &T, &p);
3111 4424 : Ppr = nfX_to_FqX(P, nf, modpr);
3112 4417 : mpr = degpol(Ppr);
3113 4417 : if (mpr < m) /* nonmonic => only_maximal = 1 */
3114 : {
3115 21 : if (mpr < 0) return NULL;
3116 21 : if (! RgX_valrem(Ppr, &Ppr))
3117 : { /* nonzero constant coefficient */
3118 0 : Ppr = RgX_shift_shallow(RgX_recip_i(Ppr), m - mpr);
3119 0 : P = RgX_recip_i(P);
3120 : }
3121 : else
3122 : {
3123 21 : GEN z = FqX_non_root(Ppr, T, p);
3124 21 : if (!z) pari_err_IMPL( "Dedekind in the difficult case");
3125 0 : z = Fq_to_nf(z, modpr);
3126 0 : if (typ(z) == t_INT)
3127 0 : P = RgX_translate(P, z);
3128 : else
3129 0 : P = RgXQX_translate(P, z, T);
3130 0 : P = RgX_recip_i(P);
3131 0 : Ppr = nfX_to_FqX(P, nf, modpr); /* degpol(P) = degpol(Ppr) = m */
3132 : }
3133 : }
3134 4396 : A = gel(FqX_factor(Ppr,T,p),1);
3135 4396 : r = lg(A); /* > 1 */
3136 4396 : g = gel(A,1);
3137 8925 : for (i=2; i<r; i++) g = FqX_mul(g, gel(A,i), T, p);
3138 4396 : h = FqX_div(Ppr,g, T, p);
3139 4396 : gzk = FqX_to_nfX(g, modpr);
3140 4396 : hzk = FqX_to_nfX(h, modpr);
3141 4396 : k = nfX_sub(nf, P, nfX_mul(nf, gzk,hzk));
3142 4396 : tau = pr_get_tau(pr);
3143 4396 : switch(typ(tau))
3144 : {
3145 1834 : case t_INT: k = gdiv(k, p); break;
3146 2562 : case t_MAT: k = RgX_Rg_div(tablemulvec(NULL,tau, k), p); break;
3147 : }
3148 4396 : k = nfX_to_FqX(k, nf, modpr);
3149 4396 : k = FqX_normalize(FqX_gcd(FqX_gcd(g,h, T,p), k, T,p), T,p);
3150 4396 : d = degpol(k); /* <= m */
3151 4396 : if (!d) return NULL; /* pr-maximal */
3152 2813 : if (only_maximal) return gen_0; /* not maximal */
3153 :
3154 2792 : A = cgetg(m+d+1,t_MAT);
3155 2792 : I = cgetg(m+d+1,t_VEC); base = mkvec2(A, I);
3156 : /* base[2] temporarily multiplied by p, for the final nfhnfmod,
3157 : * which requires integral ideals */
3158 2792 : prinvp = pr_inv_p(pr); /* again multiplied by p */
3159 16006 : for (j=1; j<=m; j++)
3160 : {
3161 13214 : gel(A,j) = col_ei(m, j);
3162 13214 : gel(I,j) = p;
3163 : }
3164 2792 : pal = FqX_to_nfX(FqX_div(Ppr,k, T,p), modpr);
3165 5983 : for ( ; j<=m+d; j++)
3166 : {
3167 3191 : gel(A,j) = RgX_to_RgC(pal,m);
3168 3191 : gel(I,j) = prinvp;
3169 3191 : if (j < m+d) pal = RgXQX_rem(RgX_shift_shallow(pal,1),P,nfT);
3170 : }
3171 : /* the modulus is integral */
3172 2792 : base = nfhnfmod(nf,base, idealmulpowprime(nf, powiu(p,m), pr, utoineg(d)));
3173 2792 : gel(base,2) = gdiv(gel(base,2), p); /* cancel the factor p */
3174 2792 : vt = vdisc - 2*d;
3175 2792 : return mkvec3(vt < 2? gen_1: gen_0, base, stoi(vt));
3176 : }
3177 :
3178 : /* [L:K] = n */
3179 : static GEN
3180 953 : triv_order(long n)
3181 : {
3182 953 : GEN z = cgetg(3, t_VEC);
3183 953 : gel(z,1) = matid(n);
3184 953 : gel(z,2) = const_vec(n, gen_1); return z;
3185 : }
3186 :
3187 : /* if flag is set, return gen_1 (resp. gen_0) if the order K[X]/(P)
3188 : * is pr-maximal (resp. not pr-maximal). */
3189 : GEN
3190 91 : rnfdedekind(GEN nf, GEN P, GEN pr, long flag)
3191 : {
3192 91 : pari_sp av = avma;
3193 : GEN z, dP;
3194 : long v;
3195 :
3196 91 : nf = checknf(nf);
3197 91 : P = RgX_nffix("rnfdedekind", nf_get_pol(nf), P, 1);
3198 91 : dP = nfX_disc(nf, P);
3199 91 : if (gequal0(dP))
3200 7 : pari_err_DOMAIN("rnfdedekind","issquarefree(pol)","=",gen_0,P);
3201 84 : if (!pr)
3202 : {
3203 21 : GEN fa = idealfactor(nf, dP);
3204 21 : GEN Q = gel(fa,1), E = gel(fa,2);
3205 21 : pari_sp av2 = avma;
3206 21 : long i, l = lg(Q);
3207 21 : for (i = 1; i < l; i++, set_avma(av2))
3208 : {
3209 21 : v = itos(gel(E,i));
3210 21 : if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { set_avma(av); return gen_0; }
3211 0 : set_avma(av2);
3212 : }
3213 0 : set_avma(av); return gen_1;
3214 : }
3215 63 : else if (typ(pr) == t_VEC)
3216 : { /* flag = 1 is implicit */
3217 63 : if (lg(pr) == 1) { set_avma(av); return gen_1; }
3218 63 : if (typ(gel(pr,1)) == t_VEC)
3219 : { /* list of primes */
3220 14 : GEN Q = pr;
3221 14 : pari_sp av2 = avma;
3222 14 : long i, l = lg(Q);
3223 14 : for (i = 1; i < l; i++, set_avma(av2))
3224 : {
3225 14 : v = nfval(nf, dP, gel(Q,i));
3226 14 : if (rnfdedekind_i(nf,P,gel(Q,i),v,1)) { set_avma(av); return gen_0; }
3227 : }
3228 0 : set_avma(av); return gen_1;
3229 : }
3230 : }
3231 : /* single prime */
3232 49 : v = nfval(nf, dP, pr);
3233 49 : z = rnfdedekind_i(nf, P, pr, v, flag);
3234 42 : if (z)
3235 : {
3236 21 : if (flag) { set_avma(av); return gen_0; }
3237 14 : z = gerepilecopy(av, z);
3238 : }
3239 : else
3240 : {
3241 21 : set_avma(av); if (flag) return gen_1;
3242 7 : z = cgetg(4, t_VEC);
3243 7 : gel(z,1) = gen_1;
3244 7 : gel(z,2) = triv_order(degpol(P));
3245 7 : gel(z,3) = stoi(v);
3246 : }
3247 21 : return z;
3248 : }
3249 :
3250 : static int
3251 27039 : ideal_is1(GEN x) {
3252 27039 : switch(typ(x))
3253 : {
3254 11633 : case t_INT: return is_pm1(x);
3255 14531 : case t_MAT: return RgM_isidentity(x);
3256 : }
3257 875 : return 0;
3258 : }
3259 :
3260 : /* return a in ideal A such that v_pr(a) = v_pr(A) */
3261 : static GEN
3262 15252 : minval(GEN nf, GEN A, GEN pr)
3263 : {
3264 15252 : GEN ab = idealtwoelt(nf,A), a = gel(ab,1), b = gel(ab,2);
3265 15252 : if (nfval(nf,a,pr) > nfval(nf,b,pr)) a = b;
3266 15252 : return a;
3267 : }
3268 :
3269 : /* nf a true nf. Return NULL if power order is pr-maximal */
3270 : static GEN
3271 4347 : rnfmaxord(GEN nf, GEN pol, GEN pr, long vdisc)
3272 : {
3273 4347 : pari_sp av = avma, av1;
3274 : long i, j, k, n, nn, vpol, cnt, sep;
3275 : GEN q, q1, p, T, modpr, W, I, p1;
3276 : GEN prhinv, mpi, Id;
3277 :
3278 4347 : if (DEBUGLEVEL>1) err_printf(" treating %Ps^%ld\n", pr, vdisc);
3279 4347 : modpr = nf_to_Fq_init(nf,&pr,&T,&p);
3280 4347 : av1 = avma;
3281 4347 : p1 = rnfdedekind_i(nf, pol, modpr, vdisc, 0);
3282 4340 : if (!p1) return gc_NULL(av);
3283 2778 : if (is_pm1(gel(p1,1))) return gerepilecopy(av,gel(p1,2));
3284 1252 : sep = itos(gel(p1,3));
3285 1252 : W = gmael(p1,2,1);
3286 1252 : I = gmael(p1,2,2);
3287 1252 : gerepileall(av1, 2, &W, &I);
3288 :
3289 1252 : mpi = zk_multable(nf, pr_get_gen(pr));
3290 1252 : n = degpol(pol); nn = n*n;
3291 1252 : vpol = varn(pol);
3292 1252 : q1 = q = pr_norm(pr);
3293 1756 : while (abscmpiu(q1,n) < 0) q1 = mulii(q1,q);
3294 1252 : Id = matid(n);
3295 1252 : prhinv = pr_inv(pr);
3296 1252 : av1 = avma;
3297 1252 : for(cnt=1;; cnt++)
3298 4165 : {
3299 5417 : GEN I0 = leafcopy(I), W0 = leafcopy(W);
3300 : GEN Wa, Winv, Ip, A, MW, MWmod, F, pseudo, C, G;
3301 5417 : GEN Tauinv = cgetg(n+1, t_VEC), Tau = cgetg(n+1, t_VEC);
3302 :
3303 5417 : if (DEBUGLEVEL>1) err_printf(" pass no %ld\n",cnt);
3304 32113 : for (j=1; j<=n; j++)
3305 : {
3306 : GEN tau, tauinv;
3307 26696 : if (ideal_is1(gel(I,j)))
3308 : {
3309 11444 : gel(I,j) = gel(Tau,j) = gel(Tauinv,j) = gen_1;
3310 11444 : continue;
3311 : }
3312 15252 : gel(Tau,j) = tau = minval(nf, gel(I,j), pr);
3313 15252 : gel(Tauinv,j) = tauinv = nfinv(nf, tau);
3314 15252 : gel(W,j) = nfC_nf_mul(nf, gel(W,j), tau);
3315 15252 : gel(I,j) = idealmul(nf, tauinv, gel(I,j)); /* v_pr(I[j]) = 0 */
3316 : }
3317 : /* W = (Z_K/pr)-basis of O/pr. O = (W0,I0) ~ (W, I) */
3318 :
3319 : /* compute MW: W_i*W_j = sum MW_k,(i,j) W_k */
3320 5417 : Wa = RgM_to_RgXV(W,vpol);
3321 5417 : Winv = nfM_inv(nf, W);
3322 5417 : MW = cgetg(nn+1, t_MAT);
3323 : /* W_1 = 1 */
3324 32113 : for (j=1; j<=n; j++) gel(MW, j) = gel(MW, (j-1)*n+1) = gel(Id,j);
3325 26696 : for (i=2; i<=n; i++)
3326 82619 : for (j=i; j<=n; j++)
3327 : {
3328 61340 : GEN z = nfXQ_mul(nf, gel(Wa,i), gel(Wa,j), pol);
3329 61340 : if (typ(z) != t_POL)
3330 0 : z = nfC_nf_mul(nf, gel(Winv,1), z);
3331 : else
3332 : {
3333 61340 : z = RgX_to_RgC(z, lg(Winv)-1);
3334 61340 : z = nfM_nfC_mul(nf, Winv, z);
3335 : }
3336 61340 : gel(MW, (i-1)*n+j) = gel(MW, (j-1)*n+i) = z;
3337 : }
3338 :
3339 : /* compute Ip = pr-radical [ could use Ker(trace) if q large ] */
3340 5417 : MWmod = nfM_to_FqM(MW,nf,modpr);
3341 5417 : F = cgetg(n+1, t_MAT); gel(F,1) = gel(Id,1);
3342 26696 : for (j=2; j<=n; j++) gel(F,j) = rnfeltid_powmod(MWmod, j, q1, T,p);
3343 5417 : Ip = FqM_ker(F,T,p);
3344 5417 : if (lg(Ip) == 1) { W = W0; I = I0; break; }
3345 :
3346 : /* Fill C: W_k A_j = sum_i C_(i,j),k A_i */
3347 4997 : A = FqM_to_nfM(FqM_suppl(Ip,T,p), modpr);
3348 13144 : for (j = lg(Ip); j<=n; j++) gel(A,j) = nfC_multable_mul(gel(A,j), mpi);
3349 4997 : MW = nfM_mul(nf, nfM_inv(nf,A), MW);
3350 4997 : C = cgetg(n+1, t_MAT);
3351 29922 : for (k=1; k<=n; k++)
3352 : {
3353 24925 : GEN mek = vecslice(MW, (k-1)*n+1, k*n), Ck;
3354 24925 : gel(C,k) = Ck = cgetg(nn+1, t_COL);
3355 164844 : for (j=1; j<=n; j++)
3356 : {
3357 139919 : GEN z = nfM_nfC_mul(nf, mek, gel(A,j));
3358 983838 : for (i=1; i<=n; i++) gel(Ck, (j-1)*n+i) = nf_to_Fq(nf,gel(z,i),modpr);
3359 : }
3360 : }
3361 4997 : G = FqM_to_nfM(FqM_ker(C,T,p), modpr);
3362 :
3363 4997 : pseudo = rnfjoinmodules_i(nf, G,prhinv, Id,I);
3364 : /* express W in terms of the power basis */
3365 4997 : W = nfM_mul(nf, W, gel(pseudo,1));
3366 4997 : I = gel(pseudo,2);
3367 : /* restore the HNF property W[i,i] = 1. NB: W upper triangular, with
3368 : * W[i,i] = Tau[i] */
3369 29922 : for (j=1; j<=n; j++)
3370 24925 : if (gel(Tau,j) != gen_1)
3371 : {
3372 14090 : gel(W,j) = nfC_nf_mul(nf, gel(W,j), gel(Tauinv,j));
3373 14090 : gel(I,j) = idealmul(nf, gel(Tau,j), gel(I,j));
3374 : }
3375 4997 : if (DEBUGLEVEL>3) err_printf(" new order:\n%Ps\n%Ps\n", W, I);
3376 4997 : if (sep <= 3 || gequal(I,I0)) break;
3377 :
3378 4165 : if (gc_needed(av1,2))
3379 : {
3380 0 : if(DEBUGMEM>1) pari_warn(warnmem,"rnfmaxord");
3381 0 : gerepileall(av1,2, &W,&I);
3382 : }
3383 : }
3384 1252 : return gerepilecopy(av, mkvec2(W, I));
3385 : }
3386 :
3387 : GEN
3388 833681 : Rg_nffix(const char *f, GEN T, GEN c, int lift)
3389 : {
3390 833681 : switch(typ(c))
3391 : {
3392 457428 : case t_INT: case t_FRAC: return c;
3393 66724 : case t_POL:
3394 66724 : if (lg(c) >= lg(T)) c = RgX_rem(c,T);
3395 66724 : break;
3396 309522 : case t_POLMOD:
3397 309522 : if (!RgX_equal_var(gel(c,1), T)) pari_err_MODULUS(f, gel(c,1),T);
3398 309221 : c = gel(c,2);
3399 309221 : switch(typ(c))
3400 : {
3401 274029 : case t_POL: break;
3402 35192 : case t_INT: case t_FRAC: return c;
3403 0 : default: pari_err_TYPE(f, c);
3404 : }
3405 274029 : break;
3406 7 : default: pari_err_TYPE(f,c);
3407 : }
3408 : /* typ(c) = t_POL */
3409 340753 : if (varn(c) != varn(T)) pari_err_VAR(f, c,T);
3410 340746 : switch(lg(c))
3411 : {
3412 13277 : case 2: return gen_0;
3413 27894 : case 3:
3414 27894 : c = gel(c,2); if (is_rational_t(typ(c))) return c;
3415 0 : pari_err_TYPE(f,c);
3416 : }
3417 299575 : RgX_check_QX(c, f);
3418 299561 : return lift? c: mkpolmod(c, T);
3419 : }
3420 : /* check whether P is a polynomials with coeffs in number field Q[y]/(T) */
3421 : GEN
3422 276349 : RgX_nffix(const char *f, GEN T, GEN P, int lift)
3423 : {
3424 276349 : long i, l, vT = varn(T);
3425 276349 : GEN Q = cgetg_copy(P, &l);
3426 276349 : if (typ(P) != t_POL) pari_err_TYPE(stack_strcat(f," [t_POL expected]"), P);
3427 276349 : if (varncmp(varn(P), vT) >= 0) pari_err_PRIORITY(f, P, ">=", vT);
3428 276335 : Q[1] = P[1];
3429 1068897 : for (i=2; i<l; i++) gel(Q,i) = Rg_nffix(f, T, gel(P,i), lift);
3430 276328 : return normalizepol_lg(Q, l);
3431 : }
3432 : GEN
3433 28 : RgV_nffix(const char *f, GEN T, GEN P, int lift)
3434 : {
3435 : long i, l;
3436 28 : GEN Q = cgetg_copy(P, &l);
3437 77 : for (i=1; i<l; i++) gel(Q,i) = Rg_nffix(f, T, gel(P,i), lift);
3438 21 : return Q;
3439 : }
3440 :
3441 : static GEN
3442 2149 : get_d(GEN nf, GEN d)
3443 : {
3444 2149 : GEN b = idealredmodpower(nf, d, 2, 100000);
3445 2149 : return nfmul(nf, d, nfsqr(nf,b));
3446 : }
3447 :
3448 : /* true nf */
3449 : static GEN
3450 3507 : pr_factorback(GEN nf, GEN fa)
3451 : {
3452 3507 : GEN P = gel(fa,1), E = gel(fa,2), z = gen_1;
3453 3507 : long i, l = lg(P);
3454 8335 : for (i = 1; i < l; i++) z = idealmulpowprime(nf, z, gel(P,i), gel(E,i));
3455 3507 : return z;
3456 : }
3457 : /* true nf */
3458 : static GEN
3459 3507 : pr_factorback_scal(GEN nf, GEN fa)
3460 : {
3461 3507 : GEN D = pr_factorback(nf,fa);
3462 3507 : if (typ(D) == t_MAT && RgM_isscalar(D,NULL)) D = gcoeff(D,1,1);
3463 3507 : return D;
3464 : }
3465 :
3466 : /* nf = base field K
3467 : * pol= monic polynomial in Z_K[X] defining a relative extension L = K[X]/(pol).
3468 : * Returns a pseudo-basis [A,I] of Z_L, set *pD to [D,d] and *pf to the
3469 : * index-ideal; rnf is used when lim != 0 and may be NULL */
3470 : GEN
3471 2093 : rnfallbase(GEN nf, GEN pol, GEN lim, GEN rnf, GEN *pD, GEN *pf, GEN *pDKP)
3472 : {
3473 : long i, j, jf, l;
3474 : GEN fa, E, P, Ef, Pf, z, disc;
3475 :
3476 2093 : nf = checknf(nf); pol = liftpol_shallow(pol);
3477 2093 : if (!gequal1(leading_coeff(pol)))
3478 7 : pari_err_IMPL("nonmonic relative polynomials in rnfallbase");
3479 2086 : disc = nf_to_scalar_or_basis(nf, nfX_disc(nf, pol));
3480 2086 : if (gequal0(disc))
3481 7 : pari_err_DOMAIN("rnfpseudobasis","issquarefree(pol)","=",gen_0, pol);
3482 2079 : if (lim)
3483 : {
3484 : GEN rnfeq, zknf, dzknf, U, vU, dA, A, MB, dB, BdB, vj, B, Tabs;
3485 329 : GEN D = idealhnf_shallow(nf, disc);
3486 329 : long rU, m = nf_get_degree(nf), n = degpol(pol), N = n*m;
3487 : nfmaxord_t S;
3488 :
3489 329 : if (typ(lim) == t_INT)
3490 35 : P = ZV_union_shallow(nf_get_ramified_primes(nf),
3491 35 : gel(Z_factor_limit(gcoeff(D,1,1), itou(lim)), 1));
3492 : else
3493 : {
3494 294 : P = cgetg_copy(lim, &l);
3495 882 : for (i = 1; i < l; i++)
3496 : {
3497 588 : GEN p = gel(lim,i);
3498 588 : if (typ(p) != t_INT) p = pr_get_p(p);
3499 588 : gel(P,i) = p;
3500 : }
3501 294 : P = ZV_sort_uniq_shallow(P);
3502 : }
3503 329 : if (rnf)
3504 : {
3505 280 : rnfeq = rnf_get_map(rnf);
3506 280 : zknf = rnf_get_nfzk(rnf);
3507 : }
3508 : else
3509 : {
3510 49 : rnfeq = nf_rnfeq(nf, pol);
3511 49 : zknf = nf_nfzk(nf, rnfeq);
3512 : }
3513 329 : dzknf = gel(zknf,1);
3514 329 : if (gequal1(dzknf)) dzknf = NULL;
3515 329 : Tabs = gel(rnfeq,1);
3516 329 : nfmaxord(&S, mkvec2(Tabs,P), 0);
3517 329 : B = RgXV_unscale(S.basis, S.unscale);
3518 329 : BdB = Q_remove_denom(B, &dB);
3519 329 : MB = RgXV_to_RgM(BdB, N); /* HNF */
3520 :
3521 329 : vU = cgetg(N+1, t_VEC);
3522 329 : vj = cgetg(N+1, t_VECSMALL);
3523 329 : gel(vU,1) = U = cgetg(m+1, t_MAT);
3524 329 : gel(U,1) = col_ei(N, 1);
3525 329 : A = dB? (dzknf? gdiv(dB,dzknf): dB): NULL;
3526 329 : if (A && gequal1(A)) A = NULL;
3527 679 : for (j = 2; j <= m; j++)
3528 : {
3529 350 : GEN t = gel(zknf,j);
3530 350 : if (A) t = ZX_Z_mul(t, A);
3531 350 : gel(U,j) = hnf_solve(MB, RgX_to_RgC(t, N));
3532 : }
3533 2163 : for (i = 2; i <= N; i++)
3534 : {
3535 1834 : GEN b = gel(BdB,i);
3536 1834 : gel(vU,i) = U = cgetg(m+1, t_MAT);
3537 1834 : gel(U,1) = hnf_solve(MB, RgX_to_RgC(b, N));
3538 4004 : for (j = 2; j <= m; j++)
3539 : {
3540 2170 : GEN t = ZX_rem(ZX_mul(b, gel(zknf,j)), Tabs);
3541 2170 : if (dzknf) t = gdiv(t, dzknf);
3542 2170 : gel(U,j) = hnf_solve(MB, RgX_to_RgC(t, N));
3543 : }
3544 : }
3545 329 : vj[1] = 1; U = gel(vU,1); rU = m;
3546 812 : for (i = j = 2; i <= N; i++)
3547 : {
3548 812 : GEN V = shallowconcat(U, gel(vU,i));
3549 812 : if (ZM_rank(V) != rU)
3550 : {
3551 812 : U = V; rU += m; vj[j++] = i;
3552 812 : if (rU == N) break;
3553 : }
3554 : }
3555 329 : if (dB) for(;;)
3556 371 : {
3557 693 : GEN c = gen_1, H = ZM_hnfmodid(U, dB);
3558 693 : long ic = 0;
3559 5964 : for (i = 1; i <= N; i++)
3560 5271 : if (cmpii(gcoeff(H,i,i), c) > 0) { c = gcoeff(H,i,i); ic = i; }
3561 693 : if (!ic) break;
3562 371 : vj[j++] = ic;
3563 371 : U = shallowconcat(H, gel(vU, ic));
3564 : }
3565 329 : setlg(vj, j);
3566 329 : B = vecpermute(B, vj);
3567 :
3568 329 : l = lg(B);
3569 329 : A = cgetg(l,t_MAT);
3570 1841 : for (j = 1; j < l; j++)
3571 : {
3572 1512 : GEN t = eltabstorel_lift(rnfeq, gel(B,j));
3573 1512 : gel(A,j) = Rg_to_RgC(t, n);
3574 : }
3575 329 : A = RgM_to_nfM(nf, A);
3576 329 : A = Q_remove_denom(A, &dA);
3577 329 : if (!dA)
3578 : { /* order is maximal */
3579 14 : z = triv_order(n);
3580 14 : if (pf) *pf = gen_1;
3581 : }
3582 : else
3583 : {
3584 : GEN fi;
3585 : /* the first n columns of A are probably in HNF already */
3586 315 : A = shallowconcat(vecslice(A,n+1,lg(A)-1), vecslice(A,1,n));
3587 315 : A = mkvec2(A, const_vec(l-1,gen_1));
3588 315 : if (DEBUGLEVEL > 2) err_printf("rnfallbase: nfhnf in dim %ld\n", l-1);
3589 315 : z = nfhnfmod(nf, A, nfdetint(nf,A));
3590 315 : gel(z,2) = gdiv(gel(z,2), dA);
3591 315 : fi = idealprod(nf,gel(z,2));
3592 315 : D = idealmul(nf, D, idealsqr(nf, fi));
3593 315 : if (pf) *pf = idealinv(nf, fi);
3594 : }
3595 329 : if (RgM_isscalar(D,NULL)) D = gcoeff(D,1,1);
3596 329 : if (pDKP) *pDKP = S.dKP;
3597 329 : *pD = mkvec2(D, get_d(nf, disc)); return z;
3598 : }
3599 1750 : fa = idealfactor(nf, disc);
3600 1750 : P = gel(fa,1); l = lg(P); z = NULL;
3601 1750 : E = gel(fa,2);
3602 1750 : Pf = cgetg(l, t_COL);
3603 1750 : Ef = cgetg(l, t_COL);
3604 5907 : for (i = j = jf = 1; i < l; i++)
3605 : {
3606 4164 : GEN pr = gel(P,i);
3607 4164 : long e = itos(gel(E,i));
3608 4164 : if (e > 1)
3609 : {
3610 3213 : GEN vD = rnfmaxord(nf, pol, pr, e);
3611 3206 : if (vD)
3612 : {
3613 2190 : long ef = idealprodval(nf, gel(vD,2), pr);
3614 2190 : z = rnfjoinmodules(nf, z, vD);
3615 2190 : if (ef) { gel(Pf, jf) = pr; gel(Ef, jf++) = stoi(-ef); }
3616 2190 : e += 2 * ef;
3617 : }
3618 : }
3619 4157 : if (e) { gel(P, j) = pr; gel(E, j++) = stoi(e); }
3620 : }
3621 1743 : setlg(P,j);
3622 1743 : setlg(E,j);
3623 1743 : if (pDKP) *pDKP = prV_primes(P);
3624 1743 : if (pf)
3625 : {
3626 1687 : setlg(Pf, jf);
3627 1687 : setlg(Ef, jf); *pf = pr_factorback_scal(nf, mkmat2(Pf,Ef));
3628 : }
3629 1743 : *pD = mkvec2(pr_factorback_scal(nf,fa), get_d(nf, disc));
3630 1743 : return z? z: triv_order(degpol(pol));
3631 : }
3632 :
3633 : static GEN
3634 1533 : RgX_to_algX(GEN nf, GEN x)
3635 : {
3636 : long i, l;
3637 1533 : GEN y = cgetg_copy(x, &l); y[1] = x[1];
3638 7959 : for (i=2; i<l; i++) gel(y,i) = nf_to_scalar_or_alg(nf, gel(x,i));
3639 1533 : return y;
3640 : }
3641 :
3642 : GEN
3643 1547 : nfX_to_monic(GEN nf, GEN T, GEN *pL)
3644 : {
3645 : GEN lT, g, a;
3646 1547 : long i, l = lg(T);
3647 1547 : if (l == 2) return pol_0(varn(T));
3648 1547 : if (l == 3) return pol_1(varn(T));
3649 1547 : nf = checknf(nf);
3650 1547 : T = Q_primpart(RgX_to_nfX(nf, T));
3651 1547 : lT = leading_coeff(T); if (pL) *pL = lT;
3652 1547 : if (isint1(T)) return T;
3653 1547 : g = cgetg_copy(T, &l); g[1] = T[1]; a = lT;
3654 1547 : gel(g, l-1) = gen_1;
3655 1547 : gel(g, l-2) = gel(T,l-2);
3656 1547 : if (l == 4) { gel(g,l-2) = nf_to_scalar_or_alg(nf, gel(g,l-2)); return g; }
3657 1533 : if (typ(lT) == t_INT)
3658 : {
3659 1519 : gel(g, l-3) = gmul(a, gel(T,l-3));
3660 3339 : for (i = l-4; i > 1; i--) { a = mulii(a,lT); gel(g,i) = gmul(a, gel(T,i)); }
3661 : }
3662 : else
3663 : {
3664 14 : gel(g, l-3) = nfmul(nf, a, gel(T,l-3));
3665 35 : for (i = l-3; i > 1; i--)
3666 : {
3667 21 : a = nfmul(nf,a,lT);
3668 21 : gel(g,i) = nfmul(nf, a, gel(T,i));
3669 : }
3670 : }
3671 1533 : return RgX_to_algX(nf, g);
3672 : }
3673 :
3674 : GEN
3675 805 : rnfdisc_factored(GEN nf, GEN pol, GEN *pd)
3676 : {
3677 : long i, j, l;
3678 : GEN fa, E, P, disc, lim;
3679 :
3680 805 : pol = rnfdisc_get_T(nf, pol, &lim);
3681 805 : disc = nf_to_scalar_or_basis(nf, nfX_disc(nf, pol));
3682 805 : if (gequal0(disc))
3683 0 : pari_err_DOMAIN("rnfdisc","issquarefree(pol)","=",gen_0, pol);
3684 805 : pol = nfX_to_monic(nf, pol, NULL);
3685 805 : fa = idealfactor_partial(nf, disc, lim);
3686 805 : P = gel(fa,1); l = lg(P);
3687 805 : E = gel(fa,2);
3688 2212 : for (i = j = 1; i < l; i++)
3689 : {
3690 1407 : long e = itos(gel(E,i));
3691 1407 : GEN pr = gel(P,i);
3692 1407 : if (e > 1)
3693 : {
3694 1134 : GEN vD = rnfmaxord(nf, pol, pr, e);
3695 1134 : if (vD) e += 2*idealprodval(nf, gel(vD,2), pr);
3696 : }
3697 1407 : if (e) { gel(P, j) = pr; gel(E, j++) = stoi(e); }
3698 : }
3699 805 : if (pd) *pd = get_d(nf, disc);
3700 805 : setlg(P, j);
3701 805 : setlg(E, j); return fa;
3702 : }
3703 : GEN
3704 77 : rnfdiscf(GEN nf, GEN pol)
3705 : {
3706 77 : pari_sp av = avma;
3707 : GEN d, fa;
3708 77 : nf = checknf(nf); fa = rnfdisc_factored(nf, pol, &d);
3709 77 : return gerepilecopy(av, mkvec2(pr_factorback_scal(nf,fa), d));
3710 : }
3711 :
3712 : GEN
3713 35 : gen_if_principal(GEN bnf, GEN x)
3714 : {
3715 35 : pari_sp av = avma;
3716 35 : GEN z = bnfisprincipal0(bnf,x, nf_GEN_IF_PRINCIPAL | nf_FORCE);
3717 35 : return isintzero(z)? gc_NULL(av): z;
3718 : }
3719 :
3720 : /* given bnf and a HNF pseudo-basis of a proj. module, simplify the HNF as
3721 : * much as possible. The resulting matrix will be upper triangular but the
3722 : * diagonal coefficients will not be equal to 1. The ideals are integral and
3723 : * primitive. */
3724 : GEN
3725 0 : rnfsimplifybasis(GEN bnf, GEN M)
3726 : {
3727 0 : pari_sp av = avma;
3728 : long i, l;
3729 : GEN y, Az, Iz, nf, A, I;
3730 :
3731 0 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
3732 0 : if (!check_ZKmodule_i(M)) pari_err_TYPE("rnfsimplifybasis",M);
3733 0 : A = gel(M,1);
3734 0 : I = gel(M,2); l = lg(I);
3735 0 : Az = cgetg(l, t_MAT);
3736 0 : Iz = cgetg(l, t_VEC); y = mkvec2(Az, Iz);
3737 0 : for (i = 1; i < l; i++)
3738 : {
3739 : GEN c, d;
3740 0 : if (ideal_is1(gel(I,i)))
3741 : {
3742 0 : gel(Iz,i) = gen_1;
3743 0 : gel(Az,i) = gel(A,i); continue;
3744 : }
3745 :
3746 0 : gel(Iz,i) = Q_primitive_part(gel(I,i), &c);
3747 0 : gel(Az,i) = c? RgC_Rg_mul(gel(A,i),c): gel(A,i);
3748 0 : if (c && ideal_is1(gel(Iz,i))) continue;
3749 :
3750 0 : d = gen_if_principal(bnf, gel(Iz,i));
3751 0 : if (d)
3752 : {
3753 0 : gel(Iz,i) = gen_1;
3754 0 : gel(Az,i) = nfC_nf_mul(nf, gel(Az,i), d);
3755 : }
3756 : }
3757 0 : return gerepilecopy(av, y);
3758 : }
3759 :
3760 : static GEN
3761 63 : get_module(GEN nf, GEN O, const char *s)
3762 : {
3763 63 : if (typ(O) == t_POL) return rnfpseudobasis(nf, O);
3764 56 : if (!check_ZKmodule_i(O)) pari_err_TYPE(s, O);
3765 56 : return shallowcopy(O);
3766 : }
3767 :
3768 : GEN
3769 14 : rnfdet(GEN nf, GEN M)
3770 : {
3771 14 : pari_sp av = avma;
3772 : GEN D;
3773 14 : nf = checknf(nf);
3774 14 : M = get_module(nf, M, "rnfdet");
3775 14 : D = idealmul(nf, nfM_det(nf, gel(M,1)), idealprod(nf, gel(M,2)));
3776 14 : return gerepileupto(av, D);
3777 : }
3778 :
3779 : /* Given two fractional ideals a and b, gives x in a, y in b, z in b^-1,
3780 : t in a^-1 such that xt-yz=1. In the present version, z is in Z. */
3781 : static void
3782 63 : nfidealdet1(GEN nf, GEN a, GEN b, GEN *px, GEN *py, GEN *pz, GEN *pt)
3783 : {
3784 : GEN x, uv, y, da, db;
3785 :
3786 63 : a = idealinv(nf,a);
3787 63 : a = Q_remove_denom(a, &da);
3788 63 : b = Q_remove_denom(b, &db);
3789 63 : x = idealcoprime(nf,a,b);
3790 63 : uv = idealaddtoone(nf, idealmul(nf,x,a), b);
3791 63 : y = gel(uv,2);
3792 63 : if (da) x = gmul(x,da);
3793 63 : if (db) y = gdiv(y,db);
3794 63 : *px = x;
3795 63 : *py = y;
3796 63 : *pz = db ? negi(db): gen_m1;
3797 63 : *pt = nfdiv(nf, gel(uv,1), x);
3798 63 : }
3799 :
3800 : /* given a pseudo-basis of a proj. module in HNF [A,I] (or [A,I,D,d]), gives
3801 : * an n x n matrix (not HNF) of a pseudo-basis and an ideal vector
3802 : * [1,...,1,I] such that M ~ Z_K^(n-1) x I. Uses the approximation theorem.*/
3803 : GEN
3804 28 : rnfsteinitz(GEN nf, GEN M)
3805 : {
3806 28 : pari_sp av = avma;
3807 : long i, n;
3808 : GEN A, I;
3809 :
3810 28 : nf = checknf(nf);
3811 28 : M = get_module(nf, M, "rnfsteinitz");
3812 28 : A = RgM_to_nfM(nf, gel(M,1));
3813 28 : I = leafcopy(gel(M,2)); n = lg(A)-1;
3814 189 : for (i = 1; i < n; i++)
3815 : {
3816 161 : GEN c1, c2, b, a = gel(I,i);
3817 161 : gel(I,i) = gen_1;
3818 161 : if (ideal_is1(a)) continue;
3819 :
3820 63 : c1 = gel(A,i);
3821 63 : c2 = gel(A,i+1);
3822 63 : b = gel(I,i+1);
3823 63 : if (ideal_is1(b))
3824 : {
3825 0 : gel(A,i) = c2;
3826 0 : gel(A,i+1) = gneg(c1);
3827 0 : gel(I,i+1) = a;
3828 : }
3829 : else
3830 : {
3831 63 : pari_sp av2 = avma;
3832 : GEN x, y, z, t, c;
3833 63 : nfidealdet1(nf,a,b, &x,&y,&z,&t);
3834 63 : x = RgC_add(nfC_nf_mul(nf, c1, x), nfC_nf_mul(nf, c2, y));
3835 63 : y = RgC_add(nfC_nf_mul(nf, c1, z), nfC_nf_mul(nf, c2, t));
3836 63 : gerepileall(av2, 2, &x,&y);
3837 63 : gel(A,i) = x;
3838 63 : gel(A,i+1) = y;
3839 63 : gel(I,i+1) = Q_primitive_part(idealmul(nf,a,b), &c);
3840 63 : if (c) gel(A,i+1) = nfC_nf_mul(nf, gel(A,i+1), c);
3841 : }
3842 : }
3843 28 : gel(M,1) = A;
3844 28 : gel(M,2) = I; return gerepilecopy(av, M);
3845 : }
3846 :
3847 : /* Given bnf and a proj. module (or a t_POL -> rnfpseudobasis), and outputs a
3848 : * basis if it is free, an n+1-generating set if it is not */
3849 : GEN
3850 21 : rnfbasis(GEN bnf, GEN M)
3851 : {
3852 21 : pari_sp av = avma;
3853 : long j, n;
3854 : GEN nf, A, I, cl, col, a;
3855 :
3856 21 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
3857 21 : M = get_module(nf, M, "rnfbasis");
3858 21 : I = gel(M,2); n = lg(I)-1;
3859 98 : j = 1; while (j < n && ideal_is1(gel(I,j))) j++;
3860 21 : if (j < n) { M = rnfsteinitz(nf,M); I = gel(M,2); }
3861 21 : A = gel(M,1);
3862 21 : col= gel(A,n); A = vecslice(A, 1, n-1);
3863 21 : cl = gel(I,n);
3864 21 : a = gen_if_principal(bnf, cl);
3865 21 : if (!a)
3866 : {
3867 7 : GEN v = idealtwoelt(nf, cl);
3868 7 : A = vec_append(A, gmul(gel(v,1), col));
3869 7 : a = gel(v,2);
3870 : }
3871 21 : A = vec_append(A, nfC_nf_mul(nf, col, a));
3872 21 : return gerepilecopy(av, A);
3873 : }
3874 :
3875 : /* Given a Z_K-module M (or a polynomial => rnfpseudobasis) outputs a
3876 : * Z_K-basis in HNF if it exists, zero if not */
3877 : GEN
3878 7 : rnfhnfbasis(GEN bnf, GEN M)
3879 : {
3880 7 : pari_sp av = avma;
3881 : long j, l;
3882 : GEN nf, A, I, a;
3883 :
3884 7 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
3885 7 : if (typ(M) == t_POL) M = rnfpseudobasis(nf, M);
3886 : else
3887 : {
3888 7 : if (typ(M) != t_VEC) pari_err_TYPE("rnfhnfbasis", M);
3889 7 : if (lg(M) == 5) M = mkvec2(gel(M,1), gel(M,2));
3890 7 : M = nfhnf(nf, M); /* in case M is not in HNF */
3891 : }
3892 7 : A = shallowcopy(gel(M,1));
3893 7 : I = gel(M,2); l = lg(A);
3894 42 : for (j = 1; j < l; j++)
3895 : {
3896 35 : if (ideal_is1(gel(I,j))) continue;
3897 14 : a = gen_if_principal(bnf, gel(I,j));
3898 14 : if (!a) return gc_const(av, gen_0);
3899 14 : gel(A,j) = nfC_nf_mul(nf, gel(A,j), a);
3900 : }
3901 7 : return gerepilecopy(av,A);
3902 : }
3903 :
3904 : long
3905 7 : rnfisfree(GEN bnf, GEN M)
3906 : {
3907 7 : pari_sp av = avma;
3908 : GEN nf, P, I;
3909 : long l, j;
3910 :
3911 7 : bnf = checkbnf(bnf);
3912 7 : if (is_pm1( bnf_get_no(bnf) )) return 1;
3913 0 : nf = bnf_get_nf(bnf);
3914 0 : M = get_module(nf, M, "rnfisfree");
3915 0 : I = gel(M,2); l = lg(I); P = NULL;
3916 0 : for (j = 1; j < l; j++)
3917 0 : if (!ideal_is1(gel(I,j))) P = P? idealmul(nf, P, gel(I,j)): gel(I,j);
3918 0 : return gc_long(av, P? gequal0( isprincipal(bnf,P) ): 1);
3919 : }
3920 :
3921 : /**********************************************************************/
3922 : /** **/
3923 : /** COMPOSITUM OF TWO NUMBER FIELDS **/
3924 : /** **/
3925 : /**********************************************************************/
3926 : static GEN
3927 26466 : compositum_fix(GEN nf, GEN A)
3928 : {
3929 : int ok;
3930 26466 : if (nf)
3931 : {
3932 952 : A = Q_primpart(liftpol_shallow(A)); RgX_check_ZXX(A,"polcompositum");
3933 952 : ok = nfissquarefree(nf,A);
3934 : }
3935 : else
3936 : {
3937 25514 : A = Q_primpart(A); RgX_check_ZX(A,"polcompositum");
3938 25510 : ok = ZX_is_squarefree(A);
3939 : }
3940 26467 : if (!ok) pari_err_DOMAIN("polcompositum","issquarefree(arg)","=",gen_0,A);
3941 26460 : return A;
3942 : }
3943 : #define next_lambda(a) (a>0 ? -a : 1-a)
3944 :
3945 : static long
3946 497 : nfcompositum_lambda(GEN nf, GEN A, GEN B, long lambda)
3947 : {
3948 497 : pari_sp av = avma;
3949 : forprime_t S;
3950 497 : GEN T = nf_get_pol(nf);
3951 497 : long vT = varn(T);
3952 : ulong p;
3953 497 : init_modular_big(&S);
3954 497 : p = u_forprime_next(&S);
3955 : while (1)
3956 42 : {
3957 : GEN Hp, Tp, a;
3958 539 : if (DEBUGLEVEL>4) err_printf("Trying lambda = %ld\n", lambda);
3959 539 : a = ZXX_to_FlxX(RgX_rescale(A, stoi(-lambda)), p, vT);
3960 539 : Tp = ZX_to_Flx(T, p);
3961 539 : Hp = FlxqX_composedsum(a, ZXX_to_FlxX(B, p, vT), Tp, p);
3962 539 : if (!FlxqX_is_squarefree(Hp, Tp, p))
3963 42 : { lambda = next_lambda(lambda); continue; }
3964 497 : if (DEBUGLEVEL>4) err_printf("Final lambda = %ld\n", lambda);
3965 497 : return gc_long(av, lambda);
3966 : }
3967 : }
3968 :
3969 : /* modular version */
3970 : GEN
3971 13344 : nfcompositum(GEN nf, GEN A, GEN B, long flag)
3972 : {
3973 13344 : pari_sp av = avma;
3974 : int same;
3975 : long v, k;
3976 : GEN C, D, LPRS;
3977 :
3978 13344 : if (typ(A)!=t_POL) pari_err_TYPE("polcompositum",A);
3979 13344 : if (typ(B)!=t_POL) pari_err_TYPE("polcompositum",B);
3980 13344 : if (degpol(A)<=0 || degpol(B)<=0) pari_err_CONSTPOL("polcompositum");
3981 13346 : v = varn(A);
3982 13346 : if (varn(B) != v) pari_err_VAR("polcompositum", A,B);
3983 13346 : if (nf)
3984 : {
3985 539 : nf = checknf(nf);
3986 532 : if (varncmp(v,nf_get_varn(nf))>=0) pari_err_PRIORITY("polcompositum", nf, ">=", v);
3987 : }
3988 13304 : same = (A == B || RgX_equal(A,B));
3989 13304 : A = compositum_fix(nf,A);
3990 13297 : B = same ? A: compositum_fix(nf,B);
3991 :
3992 13298 : D = LPRS = NULL; /* -Wall */
3993 13298 : k = same? -1: 1;
3994 13298 : if (nf)
3995 : {
3996 497 : long v0 = fetch_var();
3997 497 : GEN q, T = nf_get_pol(nf);
3998 497 : A = liftpol_shallow(A);
3999 497 : B = liftpol_shallow(B);
4000 497 : k = nfcompositum_lambda(nf, A, B, k);
4001 497 : if (flag&1)
4002 : {
4003 : GEN H0, H1;
4004 189 : GEN chgvar = deg1pol_shallow(stoi(k),pol_x(v0),v);
4005 189 : GEN B1 = poleval(QXQX_to_mod_shallow(B, T), chgvar);
4006 189 : C = RgX_resultant_all(QXQX_to_mod_shallow(A, T), B1, &q);
4007 189 : C = gsubst(C,v0,pol_x(v));
4008 189 : C = lift_if_rational(C);
4009 189 : H0 = gsubst(gel(q,2),v0,pol_x(v));
4010 189 : H1 = gsubst(gel(q,3),v0,pol_x(v));
4011 189 : if (typ(H0) != t_POL) H0 = scalarpol_shallow(H0,v);
4012 189 : if (typ(H1) != t_POL) H1 = scalarpol_shallow(H1,v);
4013 189 : H0 = lift_if_rational(H0);
4014 189 : H1 = lift_if_rational(H1);
4015 189 : LPRS = mkvec2(H0,H1);
4016 : }
4017 : else
4018 : {
4019 308 : C = nf_direct_compositum(nf, RgX_rescale(A,stoi(-k)), B);
4020 308 : setvarn(C, v); C = QXQX_to_mod_shallow(C, T);
4021 : }
4022 497 : C = RgX_normalize(C);
4023 : }
4024 : else
4025 : {
4026 12801 : B = leafcopy(B); setvarn(B,fetch_var_higher());
4027 3080 : C = (flag&1)? ZX_ZXY_resultant_all(A, B, &k, &LPRS)
4028 12800 : : ZX_compositum(A, B, &k);
4029 12802 : setvarn(C, v);
4030 : }
4031 : /* C = Res_Y (A(Y), B(X + kY)) guaranteed squarefree */
4032 13299 : if (flag & 2)
4033 10254 : C = mkvec(C);
4034 : else
4035 : {
4036 3045 : if (same)
4037 : {
4038 105 : D = RgX_rescale(A, stoi(1 - k));
4039 105 : if (nf) D = RgX_normalize(QXQX_to_mod_shallow(D, nf_get_pol(nf)));
4040 105 : C = RgX_div(C, D);
4041 105 : if (degpol(C) <= 0)
4042 0 : C = mkvec(D);
4043 : else
4044 105 : C = shallowconcat(nf? gel(nffactor(nf,C),1): ZX_DDF(C), D);
4045 : }
4046 : else
4047 2940 : C = nf? gel(nffactor(nf,C),1): ZX_DDF(C);
4048 : }
4049 13299 : gen_sort_inplace(C, (void*)(nf?&cmp_RgX: &cmpii), &gen_cmp_RgX, NULL);
4050 13299 : if (flag&1)
4051 : { /* a,b,c root of A,B,C = compositum, c = b - k a */
4052 3269 : long i, l = lg(C);
4053 3269 : GEN a, b, mH0 = RgX_neg(gel(LPRS,1)), H1 = gel(LPRS,2);
4054 3269 : setvarn(mH0,v);
4055 3269 : setvarn(H1,v);
4056 6615 : for (i=1; i<l; i++)
4057 : {
4058 3346 : GEN D = gel(C,i);
4059 3346 : a = RgXQ_mul(mH0, nf? RgXQ_inv(H1,D): QXQ_inv(H1,D), D);
4060 3346 : b = gadd(pol_x(v), gmulsg(k,a));
4061 3346 : if (degpol(D) == 1) b = RgX_rem(b,D);
4062 3346 : gel(C,i) = mkvec4(D, mkpolmod(a,D), mkpolmod(b,D), stoi(-k));
4063 : }
4064 : }
4065 13299 : (void)delete_var();
4066 13299 : settyp(C, t_VEC);
4067 13299 : if (flag&2) C = gel(C,1);
4068 13299 : return gerepilecopy(av, C);
4069 : }
4070 : GEN
4071 12805 : polcompositum0(GEN A, GEN B, long flag)
4072 12805 : { return nfcompositum(NULL,A,B,flag); }
4073 :
4074 : GEN
4075 91 : compositum(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,0); }
4076 : GEN
4077 2828 : compositum2(GEN pol1,GEN pol2) { return polcompositum0(pol1,pol2,1); }
|