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 :
11 : Check the License for details. You should have received a copy of it, along
12 : with the package; see the file 'COPYING'. If not, write to the Free Software
13 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14 :
15 : /*******************************************************************/
16 : /* */
17 : /* BASIC NF OPERATIONS */
18 : /* (continued) */
19 : /* */
20 : /*******************************************************************/
21 : #include "pari.h"
22 : #include "paripriv.h"
23 :
24 : #define DEBUGLEVEL DEBUGLEVEL_nf
25 :
26 : /*******************************************************************/
27 : /* */
28 : /* IDEAL OPERATIONS */
29 : /* */
30 : /*******************************************************************/
31 :
32 : /* A valid ideal is either principal (valid nf_element), or prime, or a matrix
33 : * on the integer basis in HNF.
34 : * A prime ideal is of the form [p,a,e,f,b], where the ideal is p.Z_K+a.Z_K,
35 : * p is a rational prime, a belongs to Z_K, e=e(P/p), f=f(P/p), and b
36 : * is Lenstra's constant, such that p.P^(-1)= p Z_K + b Z_K.
37 : *
38 : * An extended ideal is a couple [I,F] where I is an ideal and F is either an
39 : * algebraic number, or a factorization matrix attached to an algebraic number.
40 : * All routines work with either extended ideals or ideals (an omitted F is
41 : * assumed to be factor(1)). All ideals are output in HNF form. */
42 :
43 : /* types and conversions */
44 :
45 : long
46 8507742 : idealtyp(GEN *ideal, GEN *arch)
47 : {
48 8507742 : GEN x = *ideal;
49 8507742 : long t,lx,tx = typ(x);
50 :
51 8507742 : if (tx!=t_VEC || lg(x)!=3) { if (arch) *arch = NULL; }
52 : else
53 : {
54 1236307 : GEN a = gel(x,2);
55 1236307 : if (typ(a) == t_MAT && lg(a) != 3)
56 : { /* allow [;] */
57 14 : if (lg(a) != 1) pari_err_TYPE("idealtyp [extended ideal]",x);
58 7 : if (arch) *arch = trivial_fact();
59 : }
60 : else
61 1236293 : if (arch) *arch = a;
62 1236300 : x = gel(x,1); tx = typ(x);
63 : }
64 8507735 : switch(tx)
65 : {
66 4676583 : case t_MAT: lx = lg(x);
67 4676583 : if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
68 4676422 : if (lx != lgcols(x)) pari_err_TYPE("idealtyp [nonsquare t_MAT]",x);
69 4676409 : t = id_MAT;
70 4676409 : break;
71 :
72 3039868 : case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
73 3039835 : t = id_PRIME; break;
74 :
75 791423 : case t_POL: case t_POLMOD: case t_COL:
76 : case t_INT: case t_FRAC:
77 791423 : t = id_PRINCIPAL; break;
78 0 : default:
79 0 : pari_err_TYPE("idealtyp",x);
80 : return 0; /*LCOV_EXCL_LINE*/
81 : }
82 8507828 : *ideal = x; return t;
83 : }
84 :
85 : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
86 : GEN
87 490487 : idealhnf_two(GEN nf, GEN v)
88 : {
89 490487 : GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
90 490488 : if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
91 421028 : return ZM_hnfmodid(m, p);
92 : }
93 : /* true nf */
94 : GEN
95 3524905 : pr_hnf(GEN nf, GEN pr)
96 : {
97 3524905 : GEN p = pr_get_p(pr), m;
98 3524878 : if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
99 3097653 : m = zk_scalar_or_multable(nf, pr_get_gen(pr));
100 3097319 : return ZM_hnfmodprime(m, p);
101 : }
102 :
103 : GEN
104 1302344 : idealhnf_principal(GEN nf, GEN x)
105 : {
106 : GEN cx;
107 1302344 : x = nf_to_scalar_or_basis(nf, x);
108 1302343 : switch(typ(x))
109 : {
110 1045161 : case t_COL: break;
111 224311 : case t_INT: if (!signe(x)) return cgetg(1,t_MAT);
112 223548 : return scalarmat(absi_shallow(x), nf_get_degree(nf));
113 32871 : case t_FRAC:
114 32871 : return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
115 0 : default: pari_err_TYPE("idealhnf",x);
116 : }
117 1045161 : x = Q_primitive_part(x, &cx);
118 1045159 : RgV_check_ZV(x, "idealhnf");
119 1045159 : x = zk_multable(nf, x);
120 1045159 : x = ZM_hnfmodid(x, zkmultable_capZ(x));
121 1045163 : return cx? ZM_Q_mul(x,cx): x;
122 : }
123 :
124 : /* x integral ideal in t_MAT form, nx columns */
125 : static GEN
126 7 : vec_mulid(GEN nf, GEN x, long nx, long N)
127 : {
128 7 : GEN m = cgetg(nx*N + 1, t_MAT);
129 : long i, j, k;
130 21 : for (i=k=1; i<=nx; i++)
131 56 : for (j=1; j<=N; j++) gel(m, k++) = zk_ei_mul(nf, gel(x,i),j);
132 7 : return m;
133 : }
134 : /* true nf */
135 : GEN
136 1571129 : idealhnf_shallow(GEN nf, GEN x)
137 : {
138 1571129 : long tx = typ(x), lx = lg(x), N;
139 :
140 : /* cannot use idealtyp because here we allow nonsquare matrices */
141 1571129 : if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
142 1571129 : if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
143 1193977 : switch(tx)
144 : {
145 101141 : case t_MAT:
146 : {
147 : GEN cx;
148 101141 : long nx = lx-1;
149 101141 : N = nf_get_degree(nf);
150 101141 : if (nx == 0) return cgetg(1, t_MAT);
151 101120 : if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
152 101113 : if (nx == 1) return idealhnf_principal(nf, gel(x,1));
153 :
154 85153 : if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
155 49516 : x = Q_primitive_part(x, &cx);
156 49516 : if (nx < N) x = vec_mulid(nf, x, nx, N);
157 49516 : x = ZM_hnfmod(x, ZM_detmult(x));
158 49516 : return cx? ZM_Q_mul(x,cx): x;
159 : }
160 14 : case t_QFB:
161 : {
162 14 : pari_sp av = avma;
163 14 : GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
164 14 : GEN A = gel(x,1), B = gel(x,2);
165 14 : N = nf_get_degree(nf);
166 14 : if (N != 2)
167 0 : pari_err_TYPE("idealhnf [Qfb for nonquadratic fields]", x);
168 14 : if (!equalii(qfb_disc(x), D))
169 7 : pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
170 : /* x -> A Z + (-B + sqrt(D)) / 2 Z
171 : K = Q[t]/T(t), t^2 + ut + v = 0, u^2 - 4v = Df^2
172 : => t = (-u + sqrt(D) f)/2
173 : => sqrt(D)/2 = (t + u/2)/f */
174 7 : u = gel(T,3);
175 7 : B = deg1pol_shallow(ginv(f),
176 : gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
177 7 : varn(T));
178 7 : return gerepileupto(av, idealhnf_two(nf, mkvec2(A,B)));
179 : }
180 1092822 : default: return idealhnf_principal(nf, x); /* PRINCIPAL */
181 : }
182 : }
183 : /* true nf */
184 : GEN
185 273 : idealhnf(GEN nf, GEN x)
186 : {
187 273 : pari_sp av = avma;
188 273 : GEN y = idealhnf_shallow(nf, x);
189 266 : return (avma == av)? gcopy(y): gerepileupto(av, y);
190 : }
191 :
192 : /* GP functions */
193 :
194 : GEN
195 70 : idealtwoelt0(GEN nf, GEN x, GEN a)
196 : {
197 70 : if (!a) return idealtwoelt(nf,x);
198 42 : return idealtwoelt2(nf,x,a);
199 : }
200 :
201 : GEN
202 84 : idealpow0(GEN nf, GEN x, GEN n, long flag)
203 : {
204 84 : if (flag) return idealpowred(nf,x,n);
205 77 : return idealpow(nf,x,n);
206 : }
207 :
208 : GEN
209 70 : idealmul0(GEN nf, GEN x, GEN y, long flag)
210 : {
211 70 : if (flag) return idealmulred(nf,x,y);
212 63 : return idealmul(nf,x,y);
213 : }
214 :
215 : GEN
216 56 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
217 : {
218 56 : switch(flag)
219 : {
220 28 : case 0: return idealdiv(nf,x,y);
221 28 : case 1: return idealdivexact(nf,x,y);
222 0 : default: pari_err_FLAG("idealdiv");
223 : }
224 : return NULL; /* LCOV_EXCL_LINE */
225 : }
226 :
227 : GEN
228 70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
229 : {
230 70 : if (!arg2) return idealaddmultoone(nf,arg1);
231 35 : return idealaddtoone(nf,arg1,arg2);
232 : }
233 :
234 : /* b not a scalar */
235 : static GEN
236 49 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
237 : /* b not a scalar */
238 : static GEN
239 42 : hnf_Z_QC(GEN nf, GEN a, GEN b)
240 : {
241 : GEN db;
242 42 : b = Q_remove_denom(b, &db);
243 42 : if (db) a = mulii(a, db);
244 42 : b = hnf_Z_ZC(nf,a,b);
245 42 : return db? RgM_Rg_div(b, db): b;
246 : }
247 : /* b not a scalar (not point in trying to optimize for this case) */
248 : static GEN
249 49 : hnf_Q_QC(GEN nf, GEN a, GEN b)
250 : {
251 : GEN da, db;
252 49 : if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
253 7 : da = gel(a,2);
254 7 : a = gel(a,1);
255 7 : b = Q_remove_denom(b, &db);
256 : /* write da = d*A, db = d*B, gcd(A,B) = 1
257 : * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
258 7 : if (db)
259 : {
260 7 : GEN d = gcdii(da,db);
261 7 : if (!is_pm1(d)) db = diviiexact(db,d); /* B */
262 7 : if (!is_pm1(db))
263 : {
264 7 : a = mulii(a, db); /* a B */
265 7 : da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
266 : }
267 : }
268 7 : return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
269 : }
270 : static GEN
271 7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
272 : {
273 : GEN da, db, d, x;
274 7 : a = Q_remove_denom(a, &da);
275 7 : b = Q_remove_denom(b, &db);
276 7 : if (da) b = ZC_Z_mul(b, da);
277 7 : if (db) a = ZC_Z_mul(a, db);
278 7 : d = mul_denom(da, db);
279 7 : a = zk_multable(nf,a); da = zkmultable_capZ(a);
280 7 : b = zk_multable(nf,b); db = zkmultable_capZ(b);
281 7 : x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
282 7 : return d? RgM_Rg_div(x, d): x;
283 : }
284 : static GEN
285 21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
286 : GEN
287 182 : idealhnf0(GEN nf, GEN a, GEN b)
288 : {
289 : long ta, tb;
290 : pari_sp av;
291 : GEN x;
292 182 : nf = checknf(nf);
293 182 : if (!b) return idealhnf(nf,a);
294 :
295 : /* HNF of aZ_K+bZ_K */
296 84 : av = avma;
297 84 : a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
298 84 : b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
299 77 : if (ta == t_COL)
300 14 : x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
301 : else
302 63 : x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
303 77 : return gerepileupto(av, x);
304 : }
305 :
306 : /*******************************************************************/
307 : /* */
308 : /* TWO-ELEMENT FORM */
309 : /* */
310 : /*******************************************************************/
311 : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
312 :
313 : static int
314 232893 : ok_elt(GEN x, GEN xZ, GEN y)
315 : {
316 232893 : pari_sp av = avma;
317 232893 : return gc_bool(av, ZM_equal(x, ZM_hnfmodid(y, xZ)));
318 : }
319 :
320 : /* a + s * b, a and b ZM, s integer */
321 : static GEN
322 59749 : addmul_mat(GEN a, GEN s, GEN b)
323 : {
324 59749 : if (!signe(s)) return a;
325 51273 : if (!equali1(s)) b = ZM_Z_mul(b, s);
326 51274 : return a? ZM_add(a, b): b;
327 : }
328 :
329 : static GEN
330 123003 : get_random_a(GEN nf, GEN x, GEN xZ)
331 : {
332 : pari_sp av;
333 123003 : long i, lm, l = lg(x);
334 : GEN z, beta, mul;
335 :
336 123003 : beta= cgetg(l, t_MAT);
337 123002 : mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
338 : /* look for a in x such that a O/xZ = x O/xZ */
339 257330 : for (i = 2; i < l; i++)
340 : {
341 248987 : GEN xi = gel(x,i);
342 248987 : GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
343 248978 : if (gequal0(t)) continue;
344 206676 : if (ok_elt(x,xZ, t)) return xi;
345 92017 : gel(beta,lm) = xi;
346 : /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
347 92017 : gel(mul,lm) = t; lm++;
348 : }
349 8343 : setlg(mul, lm);
350 8343 : setlg(beta,lm); z = cgetg(lm, t_VEC);
351 28030 : for(av = avma;; set_avma(av))
352 19687 : {
353 28030 : GEN a = NULL;
354 87780 : for (i = 1; i < lm; i++)
355 : {
356 59750 : gel(z,i) = randomi(xZ);
357 59749 : a = addmul_mat(a, gel(z,i), gel(mul,i));
358 : }
359 : /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
360 28030 : if (a && ok_elt(x,xZ, a)) break;
361 : }
362 8343 : return ZM_ZC_mul(beta, z);
363 : }
364 :
365 : /* x square matrix, assume it is HNF */
366 : static GEN
367 260204 : mat_ideal_two_elt(GEN nf, GEN x)
368 : {
369 : GEN y, a, cx, xZ;
370 260204 : long N = nf_get_degree(nf);
371 : pari_sp av, tetpil;
372 :
373 260204 : if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
374 260190 : if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
375 :
376 138195 : y = cgetg(3,t_VEC); av = avma;
377 138195 : cx = Q_content(x);
378 138195 : xZ = gcoeff(x,1,1);
379 138195 : if (gequal(xZ, cx)) /* x = (cx) */
380 : {
381 5131 : gel(y,1) = cx;
382 5131 : gel(y,2) = gen_0; return y;
383 : }
384 133064 : if (equali1(cx)) cx = NULL;
385 : else
386 : {
387 3212 : x = Q_div_to_int(x, cx);
388 3212 : xZ = gcoeff(x,1,1);
389 : }
390 133064 : if (N < 6)
391 112384 : a = get_random_a(nf, x, xZ);
392 : else
393 : {
394 20680 : const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
395 : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
396 : };
397 20680 : GEN P, E, a1 = Z_lsmoothen(xZ, (GEN)FB, &P, &E);
398 20680 : if (!a1) /* factors completely */
399 10061 : a = idealapprfact_i(nf, idealfactor(nf,x), 1);
400 10619 : else if (lg(P) == 1) /* no small factors */
401 3257 : a = get_random_a(nf, x, xZ);
402 : else /* general case */
403 : {
404 : GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
405 7362 : a0 = diviiexact(xZ, a1);
406 7362 : A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
407 7362 : A1 = ZM_hnfmodid(x, a1); /* cofactor */
408 7362 : pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
409 7362 : pi1 = get_random_a(nf, A1, a1);
410 7362 : (void)bezout(a0, a1, &v0,&v1);
411 7362 : u0 = mulii(a0, v0);
412 7362 : u1 = mulii(a1, v1);
413 7362 : if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
414 : else
415 7362 : { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
416 7362 : u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
417 7362 : a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
418 : }
419 : }
420 133065 : if (cx)
421 : {
422 3212 : a = centermod(a, xZ);
423 3212 : tetpil = avma;
424 3212 : if (typ(cx) == t_INT)
425 : {
426 77 : gel(y,1) = mulii(xZ, cx);
427 77 : gel(y,2) = ZC_Z_mul(a, cx);
428 : }
429 : else
430 : {
431 3135 : gel(y,1) = gmul(xZ, cx);
432 3135 : gel(y,2) = RgC_Rg_mul(a, cx);
433 : }
434 : }
435 : else
436 : {
437 129853 : tetpil = avma;
438 129853 : gel(y,1) = icopy(xZ);
439 129852 : gel(y,2) = centermod(a, xZ);
440 : }
441 133065 : gerepilecoeffssp(av,tetpil,y+1,2); return y;
442 : }
443 :
444 : /* Given an ideal x, returns [a,alpha] such that a is in Q,
445 : * x = a Z_K + alpha Z_K, alpha in K^*
446 : * a = 0 or alpha = 0 are possible, but do not try to determine whether
447 : * x is principal. */
448 : GEN
449 102023 : idealtwoelt(GEN nf, GEN x)
450 : {
451 : pari_sp av;
452 102023 : long tx = idealtyp(&x, NULL);
453 102017 : nf = checknf(nf);
454 102016 : if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
455 1054 : if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
456 : /* id_PRINCIPAL */
457 1047 : av = avma; x = nf_to_scalar_or_basis(nf, x);
458 1898 : return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
459 942 : mkvec2(Q_abs_shallow(x),gen_0));
460 : }
461 :
462 : /*******************************************************************/
463 : /* */
464 : /* FACTORIZATION */
465 : /* */
466 : /*******************************************************************/
467 : /* x integral ideal in HNF, Zval = v_p(x \cap Z) > 0; return v_p(Nx) */
468 : static long
469 727615 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
470 : {
471 727615 : long i, v = Zval, l = lg(x);
472 2298422 : for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
473 727613 : return v;
474 : }
475 :
476 : /* x integral in HNF, f0 = partial factorization of a multiple of
477 : * x[1,1] = x\cap Z */
478 : GEN
479 269152 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
480 : {
481 269152 : GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
482 : long i, l;
483 269157 : P = gel(f,1); l = lg(P);
484 269157 : E = gel(f,2);
485 269157 : *pvN = vN = cgetg(l, t_VECSMALL);
486 269161 : *pvZ = vZ = cgetg(l, t_VECSMALL);
487 693796 : for (i = 1; i < l; i++)
488 : {
489 424636 : GEN p = gel(P,i);
490 424636 : vZ[i] = f0? Z_pval(xZ, p): (long) itou(gel(E,i));
491 424634 : vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
492 : }
493 269160 : return P;
494 : }
495 : /* return P, primes dividing Nx and xZ = x\cap Z, set v_p(Nx), v_p(xZ);
496 : * x integral in HNF */
497 : GEN
498 0 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
499 0 : { return idealHNF_Z_factor_i(x, NULL, pvN, pvZ); }
500 :
501 : /* v_P(A)*f(P) <= Nval [e.g. Nval = v_p(Norm A)], Zval = v_p(A \cap Z).
502 : * Return v_P(A) */
503 : static long
504 863474 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
505 : {
506 863474 : long f = pr_get_f(P), vmax, v, e, i, j, k, l;
507 : GEN mul, B, a, y, r, p, pk, cx, vals;
508 : pari_sp av;
509 :
510 863472 : if (Nval < f) return 0;
511 863168 : p = pr_get_p(P);
512 863168 : e = pr_get_e(P);
513 : /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
514 863172 : vmax = minss(Zval * e, Nval / f);
515 863168 : mul = pr_get_tau(P);
516 863167 : l = lg(mul);
517 863167 : B = cgetg(l,t_MAT);
518 : /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
519 863510 : gel(B,1) = gen_0; /* dummy */
520 2565239 : for (j = 2; j < l; j++)
521 : {
522 1913813 : GEN x = gel(A,j);
523 1913813 : gel(B,j) = y = cgetg(l, t_COL);
524 15207678 : for (i = 1; i < l; i++)
525 : { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
526 13505949 : a = mulii(gel(x,1), gcoeff(mul,i,1));
527 123614945 : for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
528 : /* p | a ? */
529 13505527 : gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
530 : }
531 : }
532 651426 : vals = cgetg(l, t_VECSMALL);
533 : /* vals[1] not needed */
534 2195148 : for (j = 2; j < l; j++)
535 : {
536 1543655 : gel(B,j) = Q_primitive_part(gel(B,j), &cx);
537 1543655 : vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
538 : }
539 651493 : pk = powiu(p, ceildivuu(vmax, e));
540 651472 : av = avma; y = cgetg(l,t_COL);
541 : /* can compute mod p^ceil((vmax-v)/e) */
542 1234082 : for (v = 1; v < vmax; v++)
543 : { /* we know v_pr(Bj) >= v for all j */
544 606444 : if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
545 3387384 : for (j = 2; j < l; j++)
546 : {
547 2804792 : GEN x = gel(B,j); if (v < vals[j]) continue;
548 17011562 : for (i = 1; i < l; i++)
549 : {
550 15372060 : pari_sp av2 = avma;
551 15372060 : a = mulii(gel(x,1), gcoeff(mul,i,1));
552 246461430 : for (k = 2; k < l; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
553 : /* a = (x.t_0)_i; p | a ? */
554 15370968 : a = dvmdii(a,p,&r); if (signe(r)) return v;
555 15347096 : if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
556 15347098 : gel(y,i) = gerepileuptoint(av2, a);
557 : }
558 1639502 : gel(B,j) = y; y = x;
559 1639502 : if (gc_needed(av,3))
560 : {
561 0 : if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
562 0 : gerepileall(av,3, &y,&B,&pk);
563 : }
564 : }
565 : }
566 627638 : return v;
567 : }
568 : /* true nf, x != 0 integral ideal in HNF, cx t_INT or NULL,
569 : * FA integer factorization matrix or NULL. Return partial factorization of
570 : * cx * x above primes in FA (complete factorization if !FA)*/
571 : static GEN
572 269153 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
573 : {
574 269153 : const long N = lg(x)-1;
575 : long i, j, k, l, v;
576 269153 : GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
577 :
578 269161 : l = lg(vp);
579 269161 : i = cx? expi(cx)+1: 1;
580 269162 : vP = cgetg((l+i-2)*N+1, t_COL);
581 269158 : vE = cgetg((l+i-2)*N+1, t_COL);
582 693790 : for (i = k = 1; i < l; i++)
583 : {
584 424632 : GEN L, p = gel(vp,i);
585 424632 : long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
586 424630 : if (vc)
587 : {
588 45811 : L = idealprimedec(nf,p);
589 45810 : if (is_pm1(cx)) cx = NULL;
590 : }
591 : else
592 378819 : L = idealprimedec_limit_f(nf,p,Nval);
593 985116 : for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
594 : {
595 560490 : GEN P = gel(L,j);
596 560490 : pari_sp av = avma;
597 560490 : v = idealHNF_val(x, P, Nval, Zval);
598 560459 : set_avma(av);
599 560460 : Nval -= v*pr_get_f(P);
600 560462 : v += vc * pr_get_e(P); if (!v) continue;
601 467653 : gel(vP,k) = P;
602 467653 : gel(vE,k) = utoipos(v); k++;
603 : }
604 469029 : if (vc) for (; j<lg(L); j++)
605 : {
606 44406 : GEN P = gel(L,j);
607 44406 : gel(vP,k) = P;
608 44406 : gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
609 : }
610 : }
611 269158 : if (cx && !FA)
612 : { /* complete factorization */
613 60141 : GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
614 60141 : long lc = lg(cP);
615 128193 : for (i=1; i<lc; i++)
616 : {
617 68051 : GEN p = gel(cP,i), L = idealprimedec(nf,p);
618 68052 : long vc = itos(gel(cE,i));
619 146611 : for (j=1; j<lg(L); j++)
620 : {
621 78559 : GEN P = gel(L,j);
622 78559 : gel(vP,k) = P;
623 78559 : gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
624 : }
625 : }
626 : }
627 269159 : setlg(vP, k);
628 269158 : setlg(vE, k); return mkmat2(vP, vE);
629 : }
630 : /* true nf, x integral ideal */
631 : static GEN
632 223789 : idealHNF_factor(GEN nf, GEN x, ulong lim)
633 : {
634 223789 : GEN cx, F = NULL;
635 223789 : if (lim)
636 : {
637 : GEN P, E;
638 : long i;
639 : /* strict useless because of prime table */
640 42 : F = absZ_factor_limit(gcoeff(x,1,1), lim);
641 42 : P = gel(F,1);
642 42 : E = gel(F,2);
643 : /* filter out entries > lim */
644 77 : for (i = lg(P)-1; i; i--)
645 77 : if (cmpiu(gel(P,i), lim) <= 0) break;
646 42 : setlg(P, i+1);
647 42 : setlg(E, i+1);
648 : }
649 223789 : x = Q_primitive_part(x, &cx);
650 223775 : return idealHNF_factor_i(nf, x, cx, F);
651 : }
652 : /* c * vector(#L,i,L[i].e), assume results fit in ulong */
653 : static GEN
654 33666 : prV_e_muls(GEN L, long c)
655 : {
656 33666 : long j, l = lg(L);
657 33666 : GEN z = cgetg(l, t_COL);
658 68933 : for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
659 33671 : return z;
660 : }
661 : /* true nf, y in Q */
662 : static GEN
663 28063 : Q_nffactor(GEN nf, GEN y, ulong lim)
664 : {
665 : GEN f, P, E;
666 : long l, i;
667 28063 : if (typ(y) == t_INT)
668 : {
669 28041 : if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
670 28027 : if (is_pm1(y)) return trivial_fact();
671 : }
672 17720 : y = Q_abs_shallow(y);
673 17723 : if (!lim) f = Q_factor(y);
674 : else
675 : {
676 37 : f = Q_factor_limit(y, lim);
677 35 : P = gel(f,1);
678 35 : E = gel(f,2);
679 77 : for (i = lg(P)-1; i > 0; i--)
680 63 : if (abscmpiu(gel(P,i), lim) < 0) break;
681 35 : setlg(P,i+1); setlg(E,i+1);
682 : }
683 17727 : P = gel(f,1); l = lg(P); if (l == 1) return f;
684 17713 : E = gel(f,2);
685 51415 : for (i = 1; i < l; i++)
686 : {
687 33676 : gel(P,i) = idealprimedec(nf, gel(P,i));
688 33658 : gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
689 : }
690 17739 : P = shallowconcat1(P); gel(f,1) = P; settyp(P, t_COL);
691 17745 : E = shallowconcat1(E); gel(f,2) = E; return f;
692 : }
693 :
694 : GEN
695 3766 : idealfactor_partial(GEN nf, GEN x, GEN L)
696 : {
697 3766 : pari_sp av = avma;
698 : long i, j, l;
699 : GEN P, E;
700 3766 : if (!L) return idealfactor(nf, x);
701 2975 : if (typ(L) == t_INT) return idealfactor_limit(nf, x, itou(L));
702 2954 : l = lg(L); if (l == 1) return trivial_fact();
703 2170 : P = cgetg(l, t_VEC);
704 4480 : for (i = 1; i < l; i++)
705 : {
706 2310 : GEN p = gel(L,i);
707 2310 : gel(P,i) = typ(p) == t_INT? idealprimedec(nf, p): mkvec(p);
708 : }
709 2170 : P = shallowconcat1(P); settyp(P, t_COL);
710 2170 : P = gen_sort_uniq(P, (void*)&cmp_prime_ideal, &cmp_nodata);
711 2170 : E = cgetg_copy(P, &l);
712 12215 : for (i = j = 1; i < l; i++)
713 : {
714 10045 : long v = idealval(nf, x, gel(P,i));
715 10045 : if (v) { gel(P,j) = gel(P,i); gel(E,j) = stoi(v); j++; }
716 : }
717 2170 : setlg(P,j);
718 2170 : setlg(E,j); return gerepilecopy(av, mkmat2(P, E));
719 : }
720 : GEN
721 251950 : idealfactor_limit(GEN nf, GEN x, ulong lim)
722 : {
723 251950 : pari_sp av = avma;
724 : GEN fa, y;
725 251950 : long tx = idealtyp(&x, NULL);
726 :
727 251943 : if (tx == id_PRIME)
728 : {
729 105 : if (lim && abscmpiu(pr_get_p(x), lim) >= 0) return trivial_fact();
730 98 : retmkmat2(mkcolcopy(x), mkcol(gen_1));
731 : }
732 251838 : nf = checknf(nf);
733 251836 : if (tx == id_PRINCIPAL)
734 : {
735 29622 : y = nf_to_scalar_or_basis(nf, x);
736 29626 : if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y, lim));
737 : }
738 223775 : y = idealnumden(nf, x);
739 223778 : fa = idealHNF_factor(nf, gel(y,1), lim);
740 223777 : if (!isint1(gel(y,2)))
741 14 : fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
742 223777 : fa = gerepilecopy(av, fa);
743 223784 : return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
744 : }
745 : GEN
746 251746 : idealfactor(GEN nf, GEN x) { return idealfactor_limit(nf, x, 0); }
747 : GEN
748 182 : gpidealfactor(GEN nf, GEN x, GEN lim)
749 : {
750 182 : ulong L = 0;
751 182 : if (lim)
752 : {
753 70 : if (typ(lim) != t_INT || signe(lim) < 0) pari_err_FLAG("idealfactor");
754 70 : L = itou(lim);
755 : }
756 182 : return idealfactor_limit(nf, x, L);
757 : }
758 :
759 : static GEN
760 7525 : ramified_root(GEN nf, GEN R, GEN A, long n)
761 : {
762 7525 : GEN v, P = gel(idealfactor(nf, R), 1);
763 7525 : long i, l = lg(P);
764 7525 : v = cgetg(l, t_VECSMALL);
765 8017 : for (i = 1; i < l; i++)
766 : {
767 499 : long w = idealval(nf, A, gel(P,i));
768 499 : if (w % n) return NULL;
769 492 : v[i] = w / n;
770 : }
771 7518 : return idealfactorback(nf, P, v, 0);
772 : }
773 : static int
774 7 : ramified_root_simple(GEN nf, long n, GEN P, GEN v)
775 : {
776 7 : long i, l = lg(v);
777 21 : for (i = 1; i < l; i++)
778 : {
779 14 : long w = v[i] % n;
780 14 : if (w)
781 : {
782 7 : GEN vpr = idealprimedec(nf, gel(P,i));
783 7 : long lpr = lg(vpr), j;
784 14 : for (j = 1; j < lpr; j++)
785 : {
786 7 : long e = pr_get_e(gel(vpr,j));
787 7 : if ((e * w) % n) return 0;
788 : }
789 : }
790 : }
791 7 : return 1;
792 : }
793 : /* true nf, n > 1, A a non-zero integral ideal; check whether A is the n-th
794 : * power of an ideal and set *pB to its n-th root if so */
795 : static long
796 7532 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
797 : {
798 : GEN C, root;
799 : long i, l;
800 :
801 7532 : if (typ(A) == t_MAT && ZM_isscalar(A, NULL)) A = gcoeff(A,1,1);
802 7532 : if (typ(A) == t_INT) /* > 0 */
803 : {
804 5236 : GEN P = nf_get_ramified_primes(nf), v, q;
805 5236 : l = lg(P); v = cgetg(l, t_VECSMALL);
806 24577 : for (i = 1; i < l; i++) v[i] = Z_pvalrem(A, gel(P,i), &A);
807 5236 : C = gen_1;
808 5236 : if (!isint1(A) && !Z_ispowerall(A, n, pB? &C: NULL)) return 0;
809 5236 : if (!pB) return ramified_root_simple(nf, n, P, v);
810 5229 : q = factorback2(P, v);
811 5229 : root = ramified_root(nf, q, q, n);
812 5229 : if (!root) return 0;
813 5229 : if (!equali1(C)) root = isint1(root)? C: ZM_Z_mul(root, C);
814 5229 : *pB = root; return 1;
815 : }
816 : /* compute valuations at ramified primes */
817 2296 : root = ramified_root(nf, idealadd(nf, nf_get_diff(nf), A), A, n);
818 2296 : if (!root) return 0;
819 : /* remove ramified primes */
820 2289 : if (isint1(root))
821 2092 : root = matid(nf_get_degree(nf));
822 : else
823 197 : A = idealdivexact(nf, A, idealpows(nf,root,n));
824 2289 : A = Q_primitive_part(A, &C);
825 2289 : if (C)
826 : {
827 182 : if (!Z_ispowerall(C,n,&C)) return 0;
828 182 : if (pB) root = ZM_Z_mul(root, C);
829 : }
830 :
831 : /* compute final n-th root, at most degree(nf)-1 iterations */
832 2289 : for (i = 0;; i++)
833 2322 : {
834 4611 : GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
835 4611 : if (is_pm1(a)) break;
836 2343 : if (!Z_ispowerall(a,n,&b)) return 0;
837 2322 : J = idealadd(nf, b, A);
838 2322 : A = idealdivexact(nf, idealpows(nf,J,n), A);
839 : /* div and not divexact here */
840 2322 : if (pB) root = odd(i)? idealdiv(nf, root, J): idealmul(nf, root, J);
841 : }
842 2268 : if (pB) *pB = root;
843 2268 : return 1;
844 : }
845 :
846 : /* A is assumed to be the n-th power of an ideal in nf
847 : returns its n-th root. */
848 : long
849 3787 : idealispower(GEN nf, GEN A, long n, GEN *pB)
850 : {
851 3787 : pari_sp av = avma;
852 : GEN v, N, D;
853 3787 : nf = checknf(nf);
854 3787 : if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
855 3787 : if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
856 3780 : v = idealnumden(nf,A);
857 3780 : if (gequal0(gel(v,1))) { set_avma(av); if (pB) *pB = cgetg(1,t_MAT); return 1; }
858 3780 : if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
859 3752 : if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
860 3752 : if (pB) *pB = gerepileupto(av, idealdiv(nf,N,D)); else set_avma(av);
861 3752 : return 1;
862 : }
863 :
864 : /* x t_INT or integral nonzero ideal in HNF */
865 : static GEN
866 96866 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
867 : {
868 : GEN cx, y, U, N, F, Q;
869 96866 : if (typ(x) == t_INT)
870 : {
871 50916 : if (!signe(x) || is_pm1(x)) return gen_1;
872 1860 : F = Z_factor_limit(x, B);
873 1860 : gel(F,2) = gdiventgs(gel(F,2), k);
874 1860 : return ginv(factorback(F));
875 : }
876 45950 : N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
877 45374 : F = absZ_factor_limit_strict(N, B, &U);
878 45374 : if (U)
879 : {
880 273 : GEN M = powii(gel(U,1), gel(U,2));
881 273 : y = hnfmodid(x, M); /* coprime part to B! */
882 273 : if (!idealispower(nf, y, k, &U)) U = NULL;
883 273 : x = hnfmodid(x, diviiexact(N, M));
884 : }
885 : /* x = B-smooth part of initial x */
886 45374 : x = Q_primitive_part(x, &cx);
887 45374 : F = idealHNF_factor_i(nf, x, cx, F);
888 45374 : gel(F,2) = gdiventgs(gel(F,2), k);
889 45374 : Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
890 45373 : if (U) Q = idealmul(nf,Q,U);
891 45374 : if (typ(Q) == t_INT) return Q;
892 13774 : y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
893 13774 : return gdiv(y, gcoeff(Q,1,1));
894 : }
895 : GEN
896 48438 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
897 : {
898 48438 : pari_sp av = avma;
899 : GEN a, b;
900 48438 : nf = checknf(nf);
901 48438 : if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
902 48438 : x = idealnumden(nf, x);
903 48440 : a = gel(x,1);
904 48440 : if (isintzero(a)) { set_avma(av); return gen_1; }
905 48433 : a = idealredmodpower_i(nf, gel(x,1), n, B);
906 48433 : b = idealredmodpower_i(nf, gel(x,2), n, B);
907 48433 : if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
908 48433 : return gerepilecopy(av, a);
909 : }
910 :
911 : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
912 : long
913 2076684 : idealval(GEN nf, GEN A, GEN P)
914 : {
915 2076684 : pari_sp av = avma;
916 : GEN p, cA;
917 2076684 : long vcA, v, Zval, tx = idealtyp(&A, NULL);
918 :
919 2076679 : if (tx == id_PRINCIPAL) return nfval(nf,A,P);
920 1992088 : checkprid(P);
921 1992089 : if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
922 : /* id_MAT */
923 1992061 : nf = checknf(nf);
924 1992062 : A = Q_primitive_part(A, &cA);
925 1992061 : p = pr_get_p(P);
926 1992061 : vcA = cA? Q_pval(cA,p): 0;
927 1992061 : if (pr_is_inert(P)) return gc_long(av,vcA);
928 1594218 : Zval = Z_pval(gcoeff(A,1,1), p);
929 1594219 : if (!Zval) v = 0;
930 : else
931 : {
932 302986 : long Nval = idealHNF_norm_pval(A, p, Zval);
933 302987 : v = idealHNF_val(A, P, Nval, Zval);
934 : }
935 1594216 : return gc_long(av, vcA? v + vcA*pr_get_e(P): v);
936 : }
937 : GEN
938 7105 : gpidealval(GEN nf, GEN ix, GEN P)
939 : {
940 7105 : long v = idealval(nf,ix,P);
941 7105 : return v == LONG_MAX? mkoo(): stoi(v);
942 : }
943 :
944 : /* gcd and generalized Bezout */
945 :
946 : GEN
947 78738 : idealadd(GEN nf, GEN x, GEN y)
948 : {
949 78738 : pari_sp av = avma;
950 : long tx, ty;
951 : GEN z, a, dx, dy, dz;
952 :
953 78738 : tx = idealtyp(&x, NULL);
954 78738 : ty = idealtyp(&y, NULL); nf = checknf(nf);
955 78738 : if (tx != id_MAT) x = idealhnf_shallow(nf,x);
956 78738 : if (ty != id_MAT) y = idealhnf_shallow(nf,y);
957 78738 : if (lg(x) == 1) return gerepilecopy(av,y);
958 78612 : if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
959 78157 : dx = Q_denom(x);
960 78157 : dy = Q_denom(y); dz = lcmii(dx,dy);
961 78157 : if (is_pm1(dz)) dz = NULL; else {
962 15674 : x = Q_muli_to_int(x, dz);
963 15674 : y = Q_muli_to_int(y, dz);
964 : }
965 78157 : a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
966 78157 : if (is_pm1(a))
967 : {
968 35299 : long N = lg(x)-1;
969 35299 : if (!dz) { set_avma(av); return matid(N); }
970 3947 : return gerepileupto(av, scalarmat(ginv(dz), N));
971 : }
972 42858 : z = ZM_hnfmodid(shallowconcat(x,y), a);
973 42858 : if (dz) z = RgM_Rg_div(z,dz);
974 42858 : return gerepileupto(av,z);
975 : }
976 :
977 : static GEN
978 28 : trivial_merge(GEN x)
979 28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
980 : /* true nf */
981 : static GEN
982 624708 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
983 : {
984 : GEN a;
985 624708 : long tx = idealtyp(&x, NULL);
986 624700 : long ty = idealtyp(&y, NULL);
987 : long ea;
988 624706 : if (tx != id_MAT) x = idealhnf_shallow(nf, x);
989 624737 : if (ty != id_MAT) y = idealhnf_shallow(nf, y);
990 624737 : if (lg(x) == 1)
991 14 : a = trivial_merge(y);
992 624723 : else if (lg(y) == 1)
993 14 : a = trivial_merge(x);
994 : else
995 624709 : a = hnfmerge_get_1(x, y);
996 624699 : if (!a) pari_err_COPRIME("idealaddtoone",x,y);
997 624681 : if (red && (ea = gexpo(a)) > 10)
998 : {
999 5636 : GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
1000 5636 : b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
1001 5636 : if (gexpo(b) < ea) a = b;
1002 : }
1003 624681 : return a;
1004 : }
1005 : /* true nf */
1006 : GEN
1007 19702 : idealaddtoone_i(GEN nf, GEN x, GEN y)
1008 19702 : { return _idealaddtoone(nf, x, y, 1); }
1009 : /* true nf */
1010 : GEN
1011 605017 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
1012 605017 : { return _idealaddtoone(nf, x, y, 0); }
1013 :
1014 : GEN
1015 98 : idealaddtoone(GEN nf, GEN x, GEN y)
1016 : {
1017 98 : GEN z = cgetg(3,t_VEC), a;
1018 98 : pari_sp av = avma;
1019 98 : nf = checknf(nf);
1020 98 : a = gerepileupto(av, idealaddtoone_i(nf,x,y));
1021 84 : gel(z,1) = a;
1022 84 : gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
1023 84 : return z;
1024 : }
1025 :
1026 : /* assume elements of list are integral ideals */
1027 : GEN
1028 35 : idealaddmultoone(GEN nf, GEN list)
1029 : {
1030 35 : pari_sp av = avma;
1031 35 : long N, i, l, nz, tx = typ(list);
1032 : GEN H, U, perm, L;
1033 :
1034 35 : nf = checknf(nf); N = nf_get_degree(nf);
1035 35 : if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
1036 35 : l = lg(list);
1037 35 : L = cgetg(l, t_VEC);
1038 35 : if (l == 1)
1039 0 : pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
1040 35 : nz = 0; /* number of nonzero ideals in L */
1041 98 : for (i=1; i<l; i++)
1042 : {
1043 70 : GEN I = gel(list,i);
1044 70 : if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
1045 70 : if (lg(I) != 1)
1046 : {
1047 42 : nz++; RgM_check_ZM(I,"idealaddmultoone");
1048 35 : if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
1049 : }
1050 63 : gel(L,i) = I;
1051 : }
1052 28 : H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
1053 28 : if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
1054 7 : pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
1055 49 : for (i=1; i<=N; i++)
1056 49 : if (perm[i] == 1) break;
1057 21 : U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
1058 21 : nz = 0;
1059 63 : for (i=1; i<l; i++)
1060 : {
1061 42 : GEN c = gel(L,i);
1062 42 : if (lg(c) == 1)
1063 14 : c = gen_0;
1064 : else {
1065 28 : c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
1066 28 : nz++;
1067 : }
1068 42 : gel(L,i) = c;
1069 : }
1070 21 : return gerepilecopy(av, L);
1071 : }
1072 :
1073 : /* multiplication */
1074 :
1075 : /* x integral ideal (without archimedean component) in HNF form
1076 : * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
1077 : * alpha a ZV or a ZM (multiplication table). Multiply them */
1078 : static GEN
1079 1069354 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
1080 : {
1081 1069354 : GEN m, a = gel(y,1), alpha = gel(y,2);
1082 : long i, N;
1083 :
1084 1069354 : if (typ(alpha) != t_MAT)
1085 : {
1086 729017 : alpha = zk_scalar_or_multable(nf, alpha);
1087 729026 : if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
1088 17700 : return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
1089 : }
1090 1051663 : N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
1091 5224203 : for (i=1; i<=N; i++) gel(m,i) = ZM_ZC_mul(alpha,gel(x,i));
1092 5223052 : for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
1093 1050837 : return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
1094 : }
1095 :
1096 : /* Assume x and y are integral in HNF form [NOT extended]. Not memory clean.
1097 : * HACK: ideal in y can be of the form [a,b], a in Z, b in Z_K */
1098 : GEN
1099 397432 : idealHNF_mul(GEN nf, GEN x, GEN y)
1100 : {
1101 : GEN z;
1102 397432 : if (typ(y) == t_VEC)
1103 216711 : z = idealHNF_mul_two(nf,x,y);
1104 : else
1105 : { /* reduce one ideal to two-elt form. The smallest */
1106 180721 : GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
1107 180721 : if (cmpii(xZ, yZ) < 0)
1108 : {
1109 39477 : if (is_pm1(xZ)) return gcopy(y);
1110 23358 : z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
1111 : }
1112 : else
1113 : {
1114 141244 : if (is_pm1(yZ)) return gcopy(x);
1115 35843 : z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
1116 : }
1117 : }
1118 275913 : return z;
1119 : }
1120 :
1121 : /* operations on elements in factored form */
1122 :
1123 : GEN
1124 233428 : famat_mul_shallow(GEN f, GEN g)
1125 : {
1126 233428 : if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
1127 233428 : if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
1128 233428 : if (lgcols(f) == 1) return g;
1129 181219 : if (lgcols(g) == 1) return f;
1130 179489 : return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
1131 179482 : shallowconcat(gel(f,2), gel(g,2)));
1132 : }
1133 : GEN
1134 89020 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
1135 : {
1136 89020 : if (!signe(e)) return f;
1137 58116 : return famat_mul_shallow(f, famat_pow_shallow(g, e));
1138 : }
1139 :
1140 : GEN
1141 146283 : famat_mulpows_shallow(GEN f, GEN g, long e)
1142 : {
1143 146283 : if (e==0) return f;
1144 123077 : return famat_mul_shallow(f, famat_pows_shallow(g, e));
1145 : }
1146 :
1147 : GEN
1148 10157 : famat_div_shallow(GEN f, GEN g)
1149 10157 : { return famat_mul_shallow(f, famat_inv_shallow(g)); }
1150 :
1151 : GEN
1152 0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
1153 : GEN
1154 2488663 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
1155 :
1156 : /* concat the single elt x; not gconcat since x may be a t_COL */
1157 : static GEN
1158 186414 : append(GEN v, GEN x)
1159 : {
1160 186414 : long i, l = lg(v);
1161 186414 : GEN w = cgetg(l+1, typ(v));
1162 1141093 : for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
1163 186414 : gel(w,i) = gcopy(x); return w;
1164 : }
1165 : /* add x^1 to famat f */
1166 : static GEN
1167 171951 : famat_add(GEN f, GEN x)
1168 : {
1169 171951 : GEN h = cgetg(3,t_MAT);
1170 171951 : if (lgcols(f) == 1)
1171 : {
1172 10120 : gel(h,1) = mkcolcopy(x);
1173 10120 : gel(h,2) = mkcol(gen_1);
1174 : }
1175 : else
1176 : {
1177 161831 : gel(h,1) = append(gel(f,1), x);
1178 161831 : gel(h,2) = gconcat(gel(f,2), gen_1);
1179 : }
1180 171951 : return h;
1181 : }
1182 : /* add x^-1 to famat f */
1183 : static GEN
1184 41753 : famat_sub(GEN f, GEN x)
1185 : {
1186 41753 : GEN h = cgetg(3,t_MAT);
1187 41753 : if (lgcols(f) == 1)
1188 : {
1189 17170 : gel(h,1) = mkcolcopy(x);
1190 17170 : gel(h,2) = mkcol(gen_m1);
1191 : }
1192 : else
1193 : {
1194 24583 : gel(h,1) = append(gel(f,1), x);
1195 24583 : gel(h,2) = gconcat(gel(f,2), gen_m1);
1196 : }
1197 41753 : return h;
1198 : }
1199 :
1200 : GEN
1201 267440 : famat_mul(GEN f, GEN g)
1202 : {
1203 : GEN h;
1204 267440 : if (typ(g) != t_MAT) {
1205 43879 : if (typ(f) == t_MAT) return famat_add(f, g);
1206 0 : h = cgetg(3, t_MAT);
1207 0 : gel(h,1) = mkcol2(gcopy(f), gcopy(g));
1208 0 : gel(h,2) = mkcol2(gen_1, gen_1);
1209 : }
1210 223561 : if (typ(f) != t_MAT) return famat_add(g, f);
1211 95489 : if (lgcols(f) == 1) return gcopy(g);
1212 72921 : if (lgcols(g) == 1) return gcopy(f);
1213 68798 : h = cgetg(3,t_MAT);
1214 68798 : gel(h,1) = gconcat(gel(f,1), gel(g,1));
1215 68798 : gel(h,2) = gconcat(gel(f,2), gel(g,2));
1216 68798 : return h;
1217 : }
1218 :
1219 : GEN
1220 41760 : famat_div(GEN f, GEN g)
1221 : {
1222 : GEN h;
1223 41760 : if (typ(g) != t_MAT) {
1224 41711 : if (typ(f) == t_MAT) return famat_sub(f, g);
1225 0 : h = cgetg(3, t_MAT);
1226 0 : gel(h,1) = mkcol2(gcopy(f), gcopy(g));
1227 0 : gel(h,2) = mkcol2(gen_1, gen_m1);
1228 : }
1229 49 : if (typ(f) != t_MAT) return famat_sub(g, f);
1230 7 : if (lgcols(f) == 1) return famat_inv(g);
1231 7 : if (lgcols(g) == 1) return gcopy(f);
1232 7 : h = cgetg(3,t_MAT);
1233 7 : gel(h,1) = gconcat(gel(f,1), gel(g,1));
1234 7 : gel(h,2) = gconcat(gel(f,2), gneg(gel(g,2)));
1235 7 : return h;
1236 : }
1237 :
1238 : GEN
1239 23261 : famat_sqr(GEN f)
1240 : {
1241 : GEN h;
1242 23261 : if (typ(f) != t_MAT) return to_famat(f,gen_2);
1243 23261 : if (lgcols(f) == 1) return gcopy(f);
1244 14388 : h = cgetg(3,t_MAT);
1245 14388 : gel(h,1) = gcopy(gel(f,1));
1246 14388 : gel(h,2) = gmul2n(gel(f,2),1);
1247 14388 : return h;
1248 : }
1249 :
1250 : GEN
1251 26727 : famat_inv_shallow(GEN f)
1252 : {
1253 26727 : if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
1254 10304 : if (lgcols(f) == 1) return f;
1255 10304 : return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
1256 : }
1257 : GEN
1258 20398 : famat_inv(GEN f)
1259 : {
1260 20398 : if (typ(f) != t_MAT) return to_famat(f,gen_m1);
1261 20398 : if (lgcols(f) == 1) return gcopy(f);
1262 2152 : retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
1263 : }
1264 : GEN
1265 60662 : famat_pow(GEN f, GEN n)
1266 : {
1267 60662 : if (typ(f) != t_MAT) return to_famat(f,n);
1268 60662 : if (lgcols(f) == 1) return gcopy(f);
1269 60662 : retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
1270 : }
1271 : GEN
1272 67155 : famat_pow_shallow(GEN f, GEN n)
1273 : {
1274 67155 : if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
1275 38464 : if (typ(f) != t_MAT) return to_famat_shallow(f,n);
1276 9968 : if (lgcols(f) == 1) return f;
1277 7390 : return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
1278 : }
1279 :
1280 : GEN
1281 150452 : famat_pows_shallow(GEN f, long n)
1282 : {
1283 150452 : if (n==1) return f;
1284 63320 : if (n==-1) return famat_inv_shallow(f);
1285 63306 : if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
1286 26701 : if (lgcols(f) == 1) return f;
1287 26701 : return mkmat2(gel(f,1), ZC_z_mul(gel(f,2),n));
1288 : }
1289 :
1290 : GEN
1291 0 : famat_Z_gcd(GEN M, GEN n)
1292 : {
1293 0 : pari_sp av=avma;
1294 0 : long i, j, l=lgcols(M);
1295 0 : GEN F=cgetg(3,t_MAT);
1296 0 : gel(F,1)=cgetg(l,t_COL);
1297 0 : gel(F,2)=cgetg(l,t_COL);
1298 0 : for (i=1, j=1; i<l; i++)
1299 : {
1300 0 : GEN p = gcoeff(M,i,1);
1301 0 : GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
1302 0 : if (signe(e))
1303 : {
1304 0 : gcoeff(F,j,1)=p;
1305 0 : gcoeff(F,j,2)=e;
1306 0 : j++;
1307 : }
1308 : }
1309 0 : setlg(gel(F,1),j); setlg(gel(F,2),j);
1310 0 : return gerepilecopy(av,F);
1311 : }
1312 :
1313 : /* x assumed to be a t_MATs (factorization matrix), or compatible with
1314 : * the element_* functions. */
1315 : static GEN
1316 34209 : ext_sqr(GEN nf, GEN x)
1317 34209 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
1318 : static GEN
1319 90069 : ext_mul(GEN nf, GEN x, GEN y)
1320 90069 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
1321 : static GEN
1322 20398 : ext_inv(GEN nf, GEN x)
1323 20398 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
1324 : static GEN
1325 0 : ext_pow(GEN nf, GEN x, GEN n)
1326 0 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
1327 :
1328 : GEN
1329 0 : famat_to_nf(GEN nf, GEN f)
1330 : {
1331 : GEN t, x, e;
1332 : long i;
1333 0 : if (lgcols(f) == 1) return gen_1;
1334 0 : x = gel(f,1);
1335 0 : e = gel(f,2);
1336 0 : t = nfpow(nf, gel(x,1), gel(e,1));
1337 0 : for (i=lg(x)-1; i>1; i--)
1338 0 : t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
1339 0 : return t;
1340 : }
1341 :
1342 : GEN
1343 0 : famat_idealfactor(GEN nf, GEN x)
1344 : {
1345 : long i, l;
1346 0 : GEN g = gel(x,1), e = gel(x,2), h = cgetg_copy(g, &l);
1347 0 : for (i = 1; i < l; i++) gel(h,i) = idealfactor(nf, gel(g,i));
1348 0 : h = famat_reduce(famatV_factorback(h,e));
1349 0 : return sort_factor(h, (void*)&cmp_prime_ideal, &cmp_nodata);
1350 : }
1351 :
1352 : GEN
1353 325063 : famat_reduce(GEN fa)
1354 : {
1355 : GEN E, G, L, g, e;
1356 : long i, k, l;
1357 :
1358 325063 : if (typ(fa) != t_MAT || lgcols(fa) == 1) return fa;
1359 312789 : g = gel(fa,1); l = lg(g);
1360 312789 : e = gel(fa,2);
1361 312789 : L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
1362 312787 : G = cgetg(l, t_COL);
1363 312790 : E = cgetg(l, t_COL);
1364 : /* merge */
1365 2662946 : for (k=i=1; i<l; i++,k++)
1366 : {
1367 2350154 : gel(G,k) = gel(g,L[i]);
1368 2350154 : gel(E,k) = gel(e,L[i]);
1369 2350154 : if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
1370 : {
1371 1080607 : gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
1372 1080594 : k--;
1373 : }
1374 : }
1375 : /* kill 0 exponents */
1376 312792 : l = k;
1377 1582354 : for (k=i=1; i<l; i++)
1378 1269560 : if (!gequal0(gel(E,i)))
1379 : {
1380 1257202 : gel(G,k) = gel(G,i);
1381 1257202 : gel(E,k) = gel(E,i); k++;
1382 : }
1383 312794 : setlg(G, k);
1384 312792 : setlg(E, k); return mkmat2(G,E);
1385 : }
1386 : GEN
1387 63 : matreduce(GEN f)
1388 63 : { pari_sp av = avma;
1389 63 : switch(typ(f))
1390 : {
1391 21 : case t_VEC: case t_COL:
1392 : {
1393 21 : GEN e; f = vec_reduce(f, &e); settyp(f, t_COL);
1394 21 : return gerepilecopy(av, mkmat2(f, zc_to_ZC(e)));
1395 : }
1396 35 : case t_MAT:
1397 35 : if (lg(f) == 3) break;
1398 : default:
1399 14 : pari_err_TYPE("matreduce", f);
1400 : }
1401 28 : if (typ(gel(f,1)) == t_VECSMALL)
1402 0 : f = famatsmall_reduce(f);
1403 : else
1404 : {
1405 28 : if (!RgV_is_ZV(gel(f,2))) pari_err_TYPE("matreduce",f);
1406 21 : f = famat_reduce(f);
1407 : }
1408 21 : return gerepilecopy(av, f);
1409 : }
1410 :
1411 : GEN
1412 172888 : famatsmall_reduce(GEN fa)
1413 : {
1414 : GEN E, G, L, g, e;
1415 : long i, k, l;
1416 172888 : if (lgcols(fa) == 1) return fa;
1417 172888 : g = gel(fa,1); l = lg(g);
1418 172888 : e = gel(fa,2);
1419 172888 : L = vecsmall_indexsort(g);
1420 172888 : G = cgetg(l, t_VECSMALL);
1421 172888 : E = cgetg(l, t_VECSMALL);
1422 : /* merge */
1423 478279 : for (k=i=1; i<l; i++,k++)
1424 : {
1425 305391 : G[k] = g[L[i]];
1426 305391 : E[k] = e[L[i]];
1427 305391 : if (k > 1 && G[k] == G[k-1])
1428 : {
1429 8096 : E[k-1] += E[k];
1430 8096 : k--;
1431 : }
1432 : }
1433 : /* kill 0 exponents */
1434 172888 : l = k;
1435 470183 : for (k=i=1; i<l; i++)
1436 297295 : if (E[i])
1437 : {
1438 293541 : G[k] = G[i];
1439 293541 : E[k] = E[i]; k++;
1440 : }
1441 172888 : setlg(G, k);
1442 172888 : setlg(E, k); return mkmat2(G,E);
1443 : }
1444 :
1445 : GEN
1446 55409 : famat_remove_trivial(GEN fa)
1447 : {
1448 55409 : GEN P, E, p = gel(fa,1), e = gel(fa,2);
1449 55409 : long j, k, l = lg(p);
1450 55409 : P = cgetg(l, t_COL);
1451 55409 : E = cgetg(l, t_COL);
1452 1820800 : for (j = k = 1; j < l; j++)
1453 1765391 : if (signe(gel(e,j))) { gel(P,k) = gel(p,j); gel(E,k++) = gel(e,j); }
1454 55409 : setlg(P, k); setlg(E, k); return mkmat2(P,E);
1455 : }
1456 :
1457 : GEN
1458 15624 : famatV_factorback(GEN v, GEN e)
1459 : {
1460 15624 : long i, l = lg(e);
1461 : GEN V;
1462 15624 : if (l == 1) return trivial_fact();
1463 15239 : V = signe(gel(e,1))? famat_pow_shallow(gel(v,1), gel(e,1)): trivial_fact();
1464 58521 : for (i = 2; i < l; i++) V = famat_mulpow_shallow(V, gel(v,i), gel(e,i));
1465 15239 : return V;
1466 : }
1467 :
1468 : GEN
1469 46528 : famatV_zv_factorback(GEN v, GEN e)
1470 : {
1471 46528 : long i, l = lg(e);
1472 : GEN V;
1473 46528 : if (l == 1) return trivial_fact();
1474 44141 : V = uel(e,1)? famat_pows_shallow(gel(v,1), uel(e,1)): trivial_fact();
1475 138696 : for (i = 2; i < l; i++) V = famat_mulpows_shallow(V, gel(v,i), uel(e,i));
1476 44141 : return V;
1477 : }
1478 :
1479 : GEN
1480 568320 : ZM_famat_limit(GEN fa, GEN limit)
1481 : {
1482 : pari_sp av;
1483 : GEN E, G, g, e, r;
1484 : long i, k, l, n, lG;
1485 :
1486 568320 : if (lgcols(fa) == 1) return fa;
1487 568313 : g = gel(fa,1); l = lg(g);
1488 568313 : e = gel(fa,2);
1489 1137776 : for(n=0, i=1; i<l; i++)
1490 569461 : if (cmpii(gel(g,i),limit)<=0) n++;
1491 568315 : lG = n<l-1 ? n+2 : n+1;
1492 568315 : G = cgetg(lG, t_COL);
1493 568315 : E = cgetg(lG, t_COL);
1494 568315 : av = avma;
1495 1137778 : for (i=1, k=1, r = gen_1; i<l; i++)
1496 : {
1497 569463 : if (cmpii(gel(g,i),limit)<=0)
1498 : {
1499 569330 : gel(G,k) = gel(g,i);
1500 569330 : gel(E,k) = gel(e,i);
1501 569330 : k++;
1502 133 : } else r = mulii(r, powii(gel(g,i), gel(e,i)));
1503 : }
1504 568315 : if (k<i)
1505 : {
1506 133 : gel(G, k) = gerepileuptoint(av, r);
1507 133 : gel(E, k) = gen_1;
1508 : }
1509 568315 : return mkmat2(G,E);
1510 : }
1511 :
1512 : /* assume pr has degree 1 and coprime to Q_denom(x) */
1513 : static GEN
1514 119525 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
1515 : {
1516 119525 : GEN d, r, p = modpr_get_p(modpr);
1517 119525 : x = nf_to_scalar_or_basis(nf,x);
1518 119525 : if (typ(x) != t_COL) return Rg_to_Fp(x,p);
1519 118377 : x = Q_remove_denom(x, &d);
1520 118377 : r = zk_to_Fq(x, modpr);
1521 118375 : if (d) r = Fp_div(r, d, p);
1522 118375 : return r;
1523 : }
1524 :
1525 : /* pr coprime to all denominators occurring in x */
1526 : static GEN
1527 602 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
1528 : {
1529 602 : GEN p = modpr_get_p(modpr);
1530 602 : GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
1531 602 : long i, l = lg(g);
1532 3094 : for (i = 1; i < l; i++)
1533 : {
1534 2492 : GEN n = modii(gel(e,i), q);
1535 2492 : if (signe(n))
1536 : {
1537 2492 : GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
1538 2492 : h = Fp_pow(h, n, p);
1539 2492 : t = t? Fp_mul(t, h, p): h;
1540 : }
1541 : }
1542 602 : return t? modii(t, p): gen_1;
1543 : }
1544 :
1545 : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
1546 : GEN
1547 117635 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
1548 : {
1549 602 : return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
1550 118237 : : to_Fp_coprime(nf, x, modpr);
1551 : }
1552 :
1553 : static long
1554 3804484 : zk_pvalrem(GEN x, GEN p, GEN *py)
1555 3804484 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
1556 : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
1557 : * such that x = p^v (newx / dx); dx = NULL if 1 */
1558 : static GEN
1559 3901576 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
1560 : {
1561 : long vcx;
1562 : GEN dx;
1563 3901576 : x = nf_to_scalar_or_basis(nf, x);
1564 3901578 : x = Q_remove_denom(x, &dx);
1565 3901584 : if (dx)
1566 : {
1567 351710 : vcx = - Z_pvalrem(dx, p, &dx);
1568 351712 : if (!vcx) vcx = zk_pvalrem(x, p, &x);
1569 351712 : if (isint1(dx)) dx = NULL;
1570 : }
1571 : else
1572 : {
1573 3549874 : vcx = zk_pvalrem(x, p, &x);
1574 3549876 : dx = NULL;
1575 : }
1576 3901588 : *pv = vcx;
1577 3901588 : *pdx = dx; return x;
1578 : }
1579 : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
1580 : * if p inert (instead of 1) */
1581 : static GEN
1582 89797 : p_makecoprime(GEN pr)
1583 : {
1584 89797 : GEN B = pr_get_tau(pr), b;
1585 : long i, e;
1586 :
1587 89797 : if (typ(B) == t_INT) return NULL;
1588 67061 : b = gel(B,1); /* B = multiplication table by b */
1589 67061 : e = pr_get_e(pr);
1590 67061 : if (e == 1) return b;
1591 : /* one could also divide (exactly) by p in each iteration */
1592 40735 : for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
1593 19914 : return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
1594 : }
1595 :
1596 : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
1597 : * Method: modify each g[i] so that it becomes coprime to pr,
1598 : * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
1599 : * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
1600 : * Optimizations:
1601 : * 1) remove all powers of p from contents, and consider extra generator p^vp;
1602 : * modified as p * (b/p)^e = b^e / p^(e-1)
1603 : * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
1604 : *
1605 : * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
1606 : * case the e[i] are large */
1607 : GEN
1608 1996469 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
1609 : {
1610 1996469 : GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
1611 1996468 : long i, l = lg(g);
1612 :
1613 1996468 : G = cgetg(l+1, t_VEC);
1614 1996476 : E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
1615 5898051 : for (i=1; i < l; i++)
1616 : {
1617 : long vcx;
1618 3901574 : GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
1619 3901586 : if (vcx) /* = v_p(content(g[i])) */
1620 : {
1621 135766 : GEN a = mulsi(vcx, gel(e,i));
1622 135763 : vp = vp? addii(vp, a): a;
1623 : }
1624 : /* x integral, content coprime to p; dx coprime to p */
1625 3901583 : if (typ(x) == t_INT)
1626 : { /* x coprime to p, hence to pr */
1627 1003107 : x = modii(x, prkZ);
1628 1003105 : if (dx) x = Fp_div(x, dx, prkZ);
1629 : }
1630 : else
1631 : {
1632 2898476 : (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
1633 2898447 : x = ZC_hnfrem(FpC_red(x,prkZ), prk);
1634 2898465 : if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
1635 : }
1636 3901567 : gel(G,i) = x;
1637 3901567 : gel(E,i) = gel(e,i);
1638 : }
1639 :
1640 1996477 : t = vp? p_makecoprime(pr): NULL;
1641 1996480 : if (!t)
1642 : { /* no need for extra generator */
1643 1929447 : setlg(G,l);
1644 1929448 : setlg(E,l);
1645 : }
1646 : else
1647 : {
1648 67033 : gel(G,i) = FpC_red(t, prkZ);
1649 67033 : gel(E,i) = vp;
1650 : }
1651 1996482 : return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
1652 : }
1653 :
1654 : /* simplified version of famat_makecoprime for X = SUnits[1] */
1655 : GEN
1656 98 : sunits_makecoprime(GEN X, GEN pr, GEN prk)
1657 : {
1658 98 : GEN G, p = pr_get_p(pr), prkZ = gcoeff(prk,1,1);
1659 98 : long i, l = lg(X);
1660 :
1661 98 : G = cgetg(l, t_VEC);
1662 8571 : for (i = 1; i < l; i++)
1663 : {
1664 8473 : GEN x = gel(X,i);
1665 8473 : if (typ(x) == t_INT) /* a prime */
1666 1344 : x = equalii(x,p)? p_makecoprime(pr): modii(x, prkZ);
1667 : else
1668 : {
1669 7129 : (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
1670 7129 : x = ZC_hnfrem(FpC_red(x,prkZ), prk);
1671 : }
1672 8473 : gel(G,i) = x;
1673 : }
1674 98 : return G;
1675 : }
1676 :
1677 : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 and 1 < lg(g) <= lg(e) */
1678 : GEN
1679 19992 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
1680 : {
1681 19992 : GEN t, cyc = bid_get_cyc(bid);
1682 19992 : if (lg(cyc) == 1)
1683 0 : t = gen_1;
1684 : else
1685 19992 : t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid),
1686 : cyc_get_expo(cyc));
1687 19992 : return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
1688 : }
1689 :
1690 : GEN
1691 960749 : vecmul(GEN x, GEN y)
1692 : {
1693 960749 : if (is_scalar_t(typ(x))) return gmul(x,y);
1694 779027 : pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
1695 : }
1696 :
1697 : GEN
1698 4949 : vecsqr(GEN x)
1699 : {
1700 4949 : if (is_scalar_t(typ(x))) return gsqr(x);
1701 630 : pari_APPLY_same(vecsqr(gel(x,i)))
1702 : }
1703 :
1704 : GEN
1705 770 : vecinv(GEN x)
1706 : {
1707 770 : if (is_scalar_t(typ(x))) return ginv(x);
1708 0 : pari_APPLY_same(vecinv(gel(x,i)))
1709 : }
1710 :
1711 : GEN
1712 0 : vecpow(GEN x, GEN n)
1713 : {
1714 0 : if (is_scalar_t(typ(x))) return powgi(x,n);
1715 0 : pari_APPLY_same(vecpow(gel(x,i), n))
1716 : }
1717 :
1718 : GEN
1719 903 : vecdiv(GEN x, GEN y)
1720 : {
1721 903 : if (is_scalar_t(typ(x))) return gdiv(x,y);
1722 903 : pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
1723 : }
1724 :
1725 : /* A ideal as a square t_MAT */
1726 : static GEN
1727 306217 : idealmulelt(GEN nf, GEN x, GEN A)
1728 : {
1729 : long i, lx;
1730 : GEN dx, dA, D;
1731 306217 : if (lg(A) == 1) return cgetg(1, t_MAT);
1732 306217 : x = nf_to_scalar_or_basis(nf,x);
1733 306217 : if (typ(x) != t_COL)
1734 98156 : return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
1735 208061 : x = Q_remove_denom(x, &dx);
1736 208061 : A = Q_remove_denom(A, &dA);
1737 208061 : x = zk_multable(nf, x);
1738 208061 : D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
1739 208061 : x = zkC_multable_mul(A, x);
1740 208061 : settyp(x, t_MAT); lx = lg(x);
1741 : /* x may contain scalars (at most 1 since the ideal is nonzero)*/
1742 789668 : for (i=1; i<lx; i++)
1743 595894 : if (typ(gel(x,i)) == t_INT)
1744 : {
1745 14287 : if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
1746 14287 : gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
1747 14287 : break;
1748 : }
1749 208061 : x = ZM_hnfmodid(x, D);
1750 208061 : dx = mul_denom(dx,dA);
1751 208061 : return dx? gdiv(x,dx): x;
1752 : }
1753 :
1754 : /* nf a true nf, tx <= ty */
1755 : static GEN
1756 823494 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
1757 : {
1758 : GEN z, cx, cy;
1759 823494 : switch(tx)
1760 : {
1761 363444 : case id_PRINCIPAL:
1762 363444 : switch(ty)
1763 : {
1764 56835 : case id_PRINCIPAL:
1765 56835 : return idealhnf_principal(nf, nfmul(nf,x,y));
1766 392 : case id_PRIME:
1767 : {
1768 392 : GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
1769 392 : if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
1770 :
1771 217 : x = nf_to_scalar_or_basis(nf, x);
1772 217 : switch(typ(x))
1773 : {
1774 203 : case t_INT:
1775 203 : if (!signe(x)) return cgetg(1,t_MAT);
1776 203 : return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
1777 7 : case t_FRAC:
1778 7 : return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
1779 : }
1780 : /* t_COL */
1781 7 : x = Q_primitive_part(x, &cx);
1782 7 : x = zk_multable(nf, x);
1783 7 : z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
1784 7 : z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
1785 7 : return cx? ZM_Q_mul(z, cx): z;
1786 : }
1787 306217 : default: /* id_MAT */
1788 306217 : return idealmulelt(nf, x,y);
1789 : }
1790 288825 : case id_PRIME:
1791 288825 : if (ty==id_PRIME)
1792 282449 : { y = pr_hnf(nf,y); cy = NULL; }
1793 : else
1794 6376 : y = Q_primitive_part(y, &cy);
1795 288825 : y = idealHNF_mul_two(nf,y,x);
1796 288824 : return cy? ZM_Q_mul(y,cy): y;
1797 :
1798 171225 : default: /* id_MAT */
1799 : {
1800 171225 : long N = nf_get_degree(nf);
1801 171225 : if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
1802 171211 : x = Q_primitive_part(x, &cx);
1803 171211 : y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
1804 171211 : y = idealHNF_mul(nf,x,y);
1805 171211 : return cx? ZM_Q_mul(y,cx): y;
1806 : }
1807 : }
1808 : }
1809 :
1810 : /* output the ideal product x.y */
1811 : GEN
1812 823493 : idealmul(GEN nf, GEN x, GEN y)
1813 : {
1814 : pari_sp av;
1815 : GEN res, ax, ay, z;
1816 823493 : long tx = idealtyp(&x,&ax);
1817 823494 : long ty = idealtyp(&y,&ay), f;
1818 823494 : if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
1819 823494 : f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
1820 823494 : av = avma;
1821 823494 : z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
1822 823479 : if (!f) return z;
1823 27735 : if (ax && ay)
1824 26203 : ax = ext_mul(nf, ax, ay);
1825 : else
1826 1532 : ax = gcopy(ax? ax: ay);
1827 27735 : gel(res,1) = z; gel(res,2) = ax; return res;
1828 : }
1829 :
1830 : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
1831 : * nf = true nf */
1832 : static GEN
1833 208757 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
1834 : {
1835 208757 : GEN p = pr_get_p(pr), q, gen;
1836 208755 : long e = pr_get_e(pr), f = pr_get_f(pr);
1837 :
1838 208761 : q = (e == 1)? sqri(p): p;
1839 208744 : if (e <= 2 && e * f == nf_get_degree(nf))
1840 : { /* pr^e = (p) */
1841 44201 : *pc = q;
1842 44201 : return mkvec2(gen_1,gen_0);
1843 : }
1844 164548 : gen = nfsqr(nf, pr_get_gen(pr));
1845 164558 : gen = FpC_red(gen, q);
1846 164545 : *pc = NULL;
1847 164545 : return mkvec2(q, gen);
1848 : }
1849 : /* cf idealpow_aux */
1850 : static GEN
1851 38835 : idealsqr_aux(GEN nf, GEN x, long tx)
1852 : {
1853 38835 : GEN T = nf_get_pol(nf), m, cx, a, alpha;
1854 38835 : long N = degpol(T);
1855 38835 : switch(tx)
1856 : {
1857 70 : case id_PRINCIPAL:
1858 70 : return idealhnf_principal(nf, nfsqr(nf,x));
1859 10773 : case id_PRIME:
1860 10773 : if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
1861 10605 : x = idealsqrprime(nf, x, &cx);
1862 10605 : x = idealhnf_two(nf,x);
1863 10605 : return cx? ZM_Z_mul(x, cx): x;
1864 27992 : default:
1865 27992 : x = Q_primitive_part(x, &cx);
1866 27992 : a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
1867 27992 : alpha = nfsqr(nf,alpha);
1868 27992 : m = zk_scalar_or_multable(nf, alpha);
1869 27992 : if (typ(m) == t_INT) {
1870 1579 : x = gcdii(sqri(a), m);
1871 1579 : if (cx) x = gmul(x, gsqr(cx));
1872 1579 : x = scalarmat(x, N);
1873 : }
1874 : else
1875 : { /* could use gcdii(sqri(a), zkmultable_capZ(m)), but costly */
1876 26413 : x = ZM_hnfmodid(m, sqri(a));
1877 26413 : if (cx) cx = gsqr(cx);
1878 26413 : if (cx) x = ZM_Q_mul(x, cx);
1879 : }
1880 27992 : return x;
1881 : }
1882 : }
1883 : GEN
1884 38835 : idealsqr(GEN nf, GEN x)
1885 : {
1886 : pari_sp av;
1887 : GEN res, ax, z;
1888 38835 : long tx = idealtyp(&x,&ax);
1889 38835 : res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
1890 38835 : av = avma;
1891 38835 : z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
1892 38835 : if (!ax) return z;
1893 34209 : gel(res,1) = z;
1894 34209 : gel(res,2) = ext_sqr(nf, ax); return res;
1895 : }
1896 :
1897 : /* norm of an ideal */
1898 : GEN
1899 13311 : idealnorm(GEN nf, GEN x)
1900 : {
1901 : pari_sp av;
1902 : long tx;
1903 :
1904 13311 : switch(idealtyp(&x, NULL))
1905 : {
1906 707 : case id_PRIME: return pr_norm(x);
1907 9755 : case id_MAT: return RgM_det_triangular(x);
1908 : }
1909 : /* id_PRINCIPAL */
1910 2849 : nf = checknf(nf); av = avma;
1911 2849 : x = nfnorm(nf, x);
1912 2849 : tx = typ(x);
1913 2849 : if (tx == t_INT) return gerepileuptoint(av, absi(x));
1914 406 : if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
1915 406 : return gerepileupto(av, Q_abs(x));
1916 : }
1917 :
1918 : /* x \cap Z */
1919 : GEN
1920 2982 : idealdown(GEN nf, GEN x)
1921 : {
1922 2982 : pari_sp av = avma;
1923 : GEN y, c;
1924 2982 : switch(idealtyp(&x, NULL))
1925 : {
1926 7 : case id_PRIME: return icopy(pr_get_p(x));
1927 2107 : case id_MAT: return gcopy(gcoeff(x,1,1));
1928 : }
1929 : /* id_PRINCIPAL */
1930 868 : nf = checknf(nf); av = avma;
1931 868 : x = nf_to_scalar_or_basis(nf, x);
1932 868 : if (is_rational_t(typ(x))) return Q_abs(x);
1933 14 : x = Q_primitive_part(x, &c);
1934 14 : y = zkmultable_capZ(zk_multable(nf, x));
1935 14 : return gerepilecopy(av, mul_content(c, y));
1936 : }
1937 :
1938 : /* true nf */
1939 : static GEN
1940 35 : idealismaximal_int(GEN nf, GEN p)
1941 : {
1942 : GEN L;
1943 35 : if (!BPSW_psp(p)) return NULL;
1944 70 : if (!dvdii(nf_get_index(nf), p) &&
1945 49 : !FpX_is_irred(FpX_red(nf_get_pol(nf),p), p)) return NULL;
1946 21 : L = idealprimedec(nf, p);
1947 21 : return lg(L) == 2? gel(L,1): NULL;
1948 : }
1949 : /* true nf */
1950 : static GEN
1951 21 : idealismaximal_mat(GEN nf, GEN x)
1952 : {
1953 : GEN p, c, L;
1954 : long i, l, f;
1955 21 : x = Q_primitive_part(x, &c);
1956 21 : p = gcoeff(x,1,1);
1957 21 : if (c)
1958 : {
1959 7 : if (typ(c) == t_FRAC || !equali1(p)) return NULL;
1960 7 : return idealismaximal_int(nf, c);
1961 : }
1962 14 : if (!BPSW_psp(p)) return NULL;
1963 14 : l = lg(x); f = 1;
1964 35 : for (i = 2; i < l; i++)
1965 : {
1966 21 : c = gcoeff(x,i,i);
1967 21 : if (equalii(c, p)) f++; else if (!equali1(c)) return NULL;
1968 : }
1969 14 : L = idealprimedec_limit_f(nf, p, f);
1970 28 : for (i = lg(L)-1; i; i--)
1971 : {
1972 28 : GEN pr = gel(L,i);
1973 28 : if (pr_get_f(pr) != f) break;
1974 28 : if (idealval(nf, x, pr) == 1) return pr;
1975 : }
1976 0 : return NULL;
1977 : }
1978 : /* true nf */
1979 : static GEN
1980 63 : idealismaximal_i(GEN nf, GEN x)
1981 : {
1982 : GEN L, p, pr, c;
1983 : long i, l;
1984 63 : switch(idealtyp(&x, NULL))
1985 : {
1986 7 : case id_PRIME: return x;
1987 21 : case id_MAT: return idealismaximal_mat(nf, x);
1988 : }
1989 : /* id_PRINCIPAL */
1990 35 : x = nf_to_scalar_or_basis(nf, x);
1991 35 : switch(typ(x))
1992 : {
1993 28 : case t_INT: return idealismaximal_int(nf, absi_shallow(x));
1994 0 : case t_FRAC: return NULL;
1995 : }
1996 7 : x = Q_primitive_part(x, &c);
1997 7 : if (c) return NULL;
1998 7 : p = zkmultable_capZ(zk_multable(nf, x));
1999 7 : L = idealprimedec(nf, p); l = lg(L); pr = NULL;
2000 21 : for (i = 1; i < l; i++)
2001 : {
2002 14 : long v = ZC_nfval(x, gel(L,i));
2003 14 : if (v > 1 || (v && pr)) return NULL;
2004 14 : pr = gel(L,i);
2005 : }
2006 7 : return pr;
2007 : }
2008 : GEN
2009 63 : idealismaximal(GEN nf, GEN x)
2010 : {
2011 63 : pari_sp av = avma;
2012 63 : x = idealismaximal_i(checknf(nf), x);
2013 63 : if (!x) { set_avma(av); return gen_0; }
2014 49 : return gerepilecopy(av, x);
2015 : }
2016 :
2017 : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
2018 : *
2019 : * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
2020 : * nf[5][7] = same in 2-elt form.
2021 : * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
2022 : GEN
2023 220479 : idealHNF_inv_Z(GEN nf, GEN I)
2024 : {
2025 220479 : GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
2026 220479 : if (isint1(IZ)) return matid(lg(I)-1);
2027 200241 : J = idealHNF_mul(nf,I, gmael(nf,5,7));
2028 : /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
2029 : * missing content cancels while solving the linear equation */
2030 200242 : dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
2031 200242 : return ZM_hnfmodid(dual, IZ);
2032 : }
2033 : /* I HNF with rational coefficients (denominator d). */
2034 : GEN
2035 78750 : idealHNF_inv(GEN nf, GEN I)
2036 : {
2037 78750 : GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
2038 78750 : J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
2039 78750 : return equali1(IQ)? J: RgM_Rg_div(J, IQ);
2040 : }
2041 :
2042 : /* return p * P^(-1) [integral] */
2043 : GEN
2044 36747 : pr_inv_p(GEN pr)
2045 : {
2046 36747 : if (pr_is_inert(pr)) return matid(pr_get_f(pr));
2047 35911 : return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
2048 : }
2049 : GEN
2050 18082 : pr_inv(GEN pr)
2051 : {
2052 18082 : GEN p = pr_get_p(pr);
2053 18082 : if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
2054 17686 : return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
2055 : }
2056 :
2057 : GEN
2058 149243 : idealinv(GEN nf, GEN x)
2059 : {
2060 : GEN res, ax;
2061 : pari_sp av;
2062 149243 : long tx = idealtyp(&x,&ax), N;
2063 :
2064 149243 : res = ax? cgetg(3,t_VEC): NULL;
2065 149243 : nf = checknf(nf); av = avma;
2066 149243 : N = nf_get_degree(nf);
2067 149243 : switch (tx)
2068 : {
2069 71767 : case id_MAT:
2070 71767 : if (lg(x)-1 != N) pari_err_DIM("idealinv");
2071 71767 : x = idealHNF_inv(nf,x); break;
2072 60650 : case id_PRINCIPAL:
2073 60650 : x = nf_to_scalar_or_basis(nf, x);
2074 60650 : if (typ(x) != t_COL)
2075 60601 : x = idealhnf_principal(nf,ginv(x));
2076 : else
2077 : { /* nfinv + idealhnf where we already know (x) \cap Z */
2078 : GEN c, d;
2079 49 : x = Q_remove_denom(x, &c);
2080 49 : x = zk_inv(nf, x);
2081 49 : x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
2082 49 : if (!d) /* x and x^(-1) integral => x a unit */
2083 14 : x = c? scalarmat(c, N): matid(N);
2084 : else
2085 : {
2086 35 : c = c? gdiv(c,d): ginv(d);
2087 35 : x = zk_multable(nf, x);
2088 35 : x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
2089 : }
2090 : }
2091 60650 : break;
2092 16826 : case id_PRIME:
2093 16826 : x = pr_inv(x); break;
2094 : }
2095 149243 : x = gerepileupto(av,x); if (!ax) return x;
2096 20398 : gel(res,1) = x;
2097 20398 : gel(res,2) = ext_inv(nf, ax); return res;
2098 : }
2099 :
2100 : /* write x = A/B, A,B coprime integral ideals */
2101 : GEN
2102 276213 : idealnumden(GEN nf, GEN x)
2103 : {
2104 276213 : pari_sp av = avma;
2105 : GEN x0, c, d, A, B, J;
2106 276213 : long tx = idealtyp(&x, NULL);
2107 276210 : nf = checknf(nf);
2108 276212 : switch (tx)
2109 : {
2110 7 : case id_PRIME:
2111 7 : retmkvec2(idealhnf(nf, x), gen_1);
2112 51301 : case id_PRINCIPAL:
2113 : {
2114 : GEN xZ, mx;
2115 51301 : x = nf_to_scalar_or_basis(nf, x);
2116 51301 : switch(typ(x))
2117 : {
2118 2644 : case t_INT: return gerepilecopy(av, mkvec2(absi_shallow(x),gen_1));
2119 14 : case t_FRAC:return gerepilecopy(av, mkvec2(absi_shallow(gel(x,1)), gel(x,2)));
2120 : }
2121 : /* t_COL */
2122 48643 : x = Q_remove_denom(x, &d);
2123 48644 : if (!d) return gerepilecopy(av, mkvec2(idealhnf_shallow(nf, x), gen_1));
2124 91 : mx = zk_multable(nf, x);
2125 91 : xZ = zkmultable_capZ(mx);
2126 91 : x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
2127 91 : x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
2128 91 : break;
2129 : }
2130 224904 : default: /* id_MAT */
2131 : {
2132 224904 : long n = lg(x)-1;
2133 224904 : if (n == 0) return mkvec2(gen_0, gen_1);
2134 224904 : if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
2135 224904 : x0 = x = Q_remove_denom(x, &d);
2136 224906 : if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
2137 21 : break;
2138 : }
2139 : }
2140 112 : J = hnfmodid(x, d); /* = d/B */
2141 112 : c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
2142 112 : B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
2143 112 : if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
2144 112 : A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
2145 112 : A = ZM_Z_divexact(A, d); /* = A ! */
2146 112 : return gerepilecopy(av, mkvec2(A, B));
2147 : }
2148 :
2149 : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
2150 : * nf = true nf */
2151 : static GEN
2152 932460 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
2153 : {
2154 932460 : GEN p = pr_get_p(pr), q, gen;
2155 :
2156 932440 : *pc = NULL;
2157 932440 : if (is_pm1(n)) /* n = 1 special cased for efficiency */
2158 : {
2159 539107 : q = p;
2160 539107 : if (typ(pr_get_tau(pr)) == t_INT) /* inert */
2161 : {
2162 0 : *pc = (signe(n) >= 0)? p: ginv(p);
2163 0 : return mkvec2(gen_1,gen_0);
2164 : }
2165 539095 : if (signe(n) >= 0) gen = pr_get_gen(pr);
2166 : else
2167 : {
2168 155681 : gen = pr_get_tau(pr); /* possibly t_MAT */
2169 155699 : *pc = ginv(p);
2170 : }
2171 : }
2172 393403 : else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
2173 : else
2174 : {
2175 195251 : long e = pr_get_e(pr), f = pr_get_f(pr);
2176 195257 : GEN r, m = truedvmdis(n, e, &r);
2177 195251 : if (e * f == nf_get_degree(nf))
2178 : { /* pr^e = (p) */
2179 80686 : if (signe(m)) *pc = powii(p,m);
2180 80687 : if (!signe(r)) return mkvec2(gen_1,gen_0);
2181 39500 : q = p;
2182 39500 : gen = nfpow(nf, pr_get_gen(pr), r);
2183 : }
2184 : else
2185 : {
2186 114568 : m = absi_shallow(m);
2187 114568 : if (signe(r)) m = addiu(m,1);
2188 114568 : q = powii(p,m); /* m = ceil(|n|/e) */
2189 114568 : if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
2190 : else
2191 : {
2192 24163 : gen = pr_get_tau(pr);
2193 24163 : if (typ(gen) == t_MAT) gen = gel(gen,1);
2194 24163 : n = negi(n);
2195 24163 : gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
2196 24162 : *pc = ginv(q);
2197 : }
2198 : }
2199 154071 : gen = FpC_red(gen, q);
2200 : }
2201 693165 : return mkvec2(q, gen);
2202 : }
2203 :
2204 : /* True nf. x * pr^n. Assume x in HNF or scalar (possibly nonintegral) */
2205 : GEN
2206 736159 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
2207 : {
2208 : GEN c, cx, y;
2209 736159 : long N = nf_get_degree(nf);
2210 :
2211 736163 : if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
2212 :
2213 : /* inert, special cased for efficiency */
2214 736156 : if (pr_is_inert(pr))
2215 : {
2216 65660 : GEN q = powii(pr_get_p(pr), n);
2217 63520 : return typ(x) == t_MAT? RgM_Rg_mul(x,q)
2218 129170 : : scalarmat_shallow(gmul(Q_abs(x),q), N);
2219 : }
2220 :
2221 670497 : y = idealpowprime(nf, pr, n, &c);
2222 670500 : if (typ(x) == t_MAT)
2223 667479 : { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
2224 : else
2225 3021 : { cx = x; x = NULL; }
2226 670415 : cx = mul_content(c,cx);
2227 670422 : if (x)
2228 504578 : x = idealHNF_mul_two(nf,x,y);
2229 : else
2230 165844 : x = idealhnf_two(nf,y);
2231 670622 : if (cx) x = ZM_Q_mul(x,cx);
2232 670275 : return x;
2233 : }
2234 : GEN
2235 6430 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
2236 : {
2237 6430 : return idealmulpowprime(nf,x,pr, negi(n));
2238 : }
2239 :
2240 : /* nf = true nf */
2241 : static GEN
2242 636854 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
2243 : {
2244 636854 : GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
2245 636851 : long N = degpol(T), s = signe(n);
2246 636852 : if (!s) return matid(N);
2247 621489 : switch(tx)
2248 : {
2249 75739 : case id_PRINCIPAL:
2250 75739 : return idealhnf_principal(nf, nfpow(nf,x,n));
2251 348779 : case id_PRIME:
2252 348779 : if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
2253 261936 : x = idealpowprime(nf, x, n, &cx);
2254 261931 : x = idealhnf_two(nf,x);
2255 261943 : return cx? ZM_Q_mul(x, cx): x;
2256 196971 : default:
2257 196971 : if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
2258 72049 : n1 = (s < 0)? negi(n): n;
2259 :
2260 72049 : x = Q_primitive_part(x, &cx);
2261 72049 : a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
2262 72049 : alpha = nfpow(nf,alpha,n1);
2263 72049 : m = zk_scalar_or_multable(nf, alpha);
2264 72049 : if (typ(m) == t_INT) {
2265 553 : x = gcdii(powii(a,n1), m);
2266 553 : if (s<0) x = ginv(x);
2267 553 : if (cx) x = gmul(x, powgi(cx,n));
2268 553 : x = scalarmat(x, N);
2269 : }
2270 : else
2271 : { /* could use gcdii(powii(a,n1), zkmultable_capZ(m)), but costly */
2272 71496 : x = ZM_hnfmodid(m, powii(a,n1));
2273 71496 : if (cx) cx = powgi(cx,n);
2274 71496 : if (s<0) {
2275 7 : GEN xZ = gcoeff(x,1,1);
2276 7 : cx = cx ? gdiv(cx, xZ): ginv(xZ);
2277 7 : x = idealHNF_inv_Z(nf,x);
2278 : }
2279 71496 : if (cx) x = ZM_Q_mul(x, cx);
2280 : }
2281 72049 : return x;
2282 : }
2283 : }
2284 :
2285 : /* raise the ideal x to the power n (in Z) */
2286 : GEN
2287 636854 : idealpow(GEN nf, GEN x, GEN n)
2288 : {
2289 : pari_sp av;
2290 : long tx;
2291 : GEN res, ax;
2292 :
2293 636854 : if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
2294 636854 : tx = idealtyp(&x,&ax);
2295 636855 : res = ax? cgetg(3,t_VEC): NULL;
2296 636855 : av = avma;
2297 636855 : x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
2298 636852 : if (!ax) return x;
2299 0 : gel(res,1) = x;
2300 0 : gel(res,2) = ext_pow(nf, ax, n);
2301 0 : return res;
2302 : }
2303 :
2304 : /* Return ideal^e in number field nf. e is a C integer. */
2305 : GEN
2306 247853 : idealpows(GEN nf, GEN ideal, long e)
2307 : {
2308 247853 : long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
2309 247853 : affsi(e,court); return idealpow(nf,ideal,court);
2310 : }
2311 :
2312 : static GEN
2313 28309 : _idealmulred(GEN nf, GEN x, GEN y)
2314 28309 : { return idealred(nf,idealmul(nf,x,y)); }
2315 : static GEN
2316 35986 : _idealsqrred(GEN nf, GEN x)
2317 35986 : { return idealred(nf,idealsqr(nf,x)); }
2318 : static GEN
2319 11411 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
2320 : static GEN
2321 35986 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
2322 :
2323 : /* compute x^n (x ideal, n integer), reducing along the way */
2324 : GEN
2325 77294 : idealpowred(GEN nf, GEN x, GEN n)
2326 : {
2327 77294 : pari_sp av = avma, av2;
2328 : long s;
2329 : GEN y;
2330 :
2331 77294 : if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
2332 77294 : s = signe(n); if (s == 0) return idealpow(nf,x,n);
2333 77294 : y = gen_pow_i(x, n, (void*)nf, &_sqr, &_mul);
2334 77294 : av2 = avma;
2335 77294 : if (s < 0) y = idealinv(nf,y);
2336 77294 : if (s < 0 || is_pm1(n)) y = idealred(nf,y);
2337 77293 : return avma == av2? gerepilecopy(av,y): gerepileupto(av,y);
2338 : }
2339 :
2340 : GEN
2341 16898 : idealmulred(GEN nf, GEN x, GEN y)
2342 : {
2343 16898 : pari_sp av = avma;
2344 16898 : return gerepileupto(av, _idealmulred(nf,x,y));
2345 : }
2346 :
2347 : long
2348 91 : isideal(GEN nf,GEN x)
2349 : {
2350 91 : long N, i, j, lx, tx = typ(x);
2351 : pari_sp av;
2352 : GEN T, xZ;
2353 :
2354 91 : nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
2355 91 : if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
2356 91 : switch(tx)
2357 : {
2358 14 : case t_INT: case t_FRAC: return 1;
2359 7 : case t_POL: return varn(x) == varn(T);
2360 7 : case t_POLMOD: return RgX_equal_var(T, gel(x,1));
2361 14 : case t_VEC: return get_prid(x)? 1 : 0;
2362 42 : case t_MAT: break;
2363 7 : default: return 0;
2364 : }
2365 42 : N = degpol(T);
2366 42 : if (lx-1 != N) return (lx == 1);
2367 28 : if (nbrows(x) != N) return 0;
2368 :
2369 28 : av = avma; x = Q_primpart(x);
2370 28 : if (!ZM_ishnf(x)) return 0;
2371 14 : xZ = gcoeff(x,1,1);
2372 21 : for (j=2; j<=N; j++)
2373 14 : if (!dvdii(xZ, gcoeff(x,j,j))) return gc_long(av,0);
2374 14 : for (i=2; i<=N; i++)
2375 14 : for (j=2; j<=N; j++)
2376 7 : if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) return gc_long(av,0);
2377 7 : return gc_long(av,1);
2378 : }
2379 :
2380 : GEN
2381 39831 : idealdiv(GEN nf, GEN x, GEN y)
2382 : {
2383 39831 : pari_sp av = avma, tetpil;
2384 39831 : GEN z = idealinv(nf,y);
2385 39831 : tetpil = avma; return gerepile(av,tetpil, idealmul(nf,x,z));
2386 : }
2387 :
2388 : /* This routine computes the quotient x/y of two ideals in the number field nf.
2389 : * It assumes that the quotient is an integral ideal. The idea is to find an
2390 : * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1. Then
2391 : *
2392 : * x + (Nx/Nz) x
2393 : * ----------- = ---
2394 : * y + (Ny/Nz) y
2395 : *
2396 : * Proof: we can assume x and y are integral. Let p be any prime ideal
2397 : *
2398 : * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
2399 : * product of the integers N(x/y) and N(y/z)). Both the numerator and the
2400 : * denominator on the left will be coprime to p. So will x/y, since x/y is
2401 : * assumed integral and its norm N(x/y) is coprime to p.
2402 : *
2403 : * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
2404 : * Hence v_p (x + Nx/Nz) = v_p(x). Likewise for the denominators. QED.
2405 : *
2406 : * Peter Montgomery. July, 1994. */
2407 : static void
2408 7 : err_divexact(GEN x, GEN y)
2409 7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
2410 0 : gen_1,mkvec2(x,y)); }
2411 : GEN
2412 3859 : idealdivexact(GEN nf, GEN x0, GEN y0)
2413 : {
2414 3859 : pari_sp av = avma;
2415 : GEN x, y, xZ, yZ, Nx, Ny, Nz, cy, q, r;
2416 :
2417 3859 : nf = checknf(nf);
2418 3859 : x = idealhnf_shallow(nf, x0);
2419 3859 : y = idealhnf_shallow(nf, y0);
2420 3859 : if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
2421 3852 : if (lg(x) == 1) { set_avma(av); return cgetg(1, t_MAT); } /* numerator is zero */
2422 3852 : y = Q_primitive_part(y, &cy);
2423 3852 : if (cy) x = RgM_Rg_div(x,cy);
2424 3852 : xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
2425 3845 : yZ = gcoeff(y,1,1); if (isint1(yZ)) return gerepilecopy(av, x);
2426 2935 : Nx = idealnorm(nf,x);
2427 2935 : Ny = idealnorm(nf,y);
2428 2935 : if (typ(Nx) != t_INT) err_divexact(x,y);
2429 2935 : q = dvmdii(Nx,Ny, &r);
2430 2935 : if (signe(r)) err_divexact(x,y);
2431 2935 : if (is_pm1(q)) { set_avma(av); return matid(nf_get_degree(nf)); }
2432 : /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
2433 653 : for (Nz = Ny;;) /* q = Nx/Nz */
2434 607 : {
2435 1260 : GEN p1 = gcdii(Nz, q);
2436 1260 : if (is_pm1(p1)) break;
2437 607 : Nz = diviiexact(Nz,p1);
2438 607 : q = mulii(q,p1);
2439 : }
2440 653 : xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
2441 653 : if (!equalii(xZ,q))
2442 : { /* Replace x/y by x+(Nx/Nz) / y+(Ny/Nz) */
2443 411 : x = ZM_hnfmodid(x, q);
2444 : /* y reduced to unit ideal ? */
2445 411 : if (Nz == Ny) return gerepileupto(av, x);
2446 :
2447 135 : yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
2448 135 : y = ZM_hnfmodid(y, q);
2449 : }
2450 377 : yZ = gcoeff(y,1,1);
2451 377 : y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
2452 377 : return gerepileupto(av, ZM_Z_divexact(y, yZ));
2453 : }
2454 :
2455 : GEN
2456 21 : idealintersect(GEN nf, GEN x, GEN y)
2457 : {
2458 21 : pari_sp av = avma;
2459 : long lz, lx, i;
2460 : GEN z, dx, dy, xZ, yZ;;
2461 :
2462 21 : nf = checknf(nf);
2463 21 : x = idealhnf_shallow(nf,x);
2464 21 : y = idealhnf_shallow(nf,y);
2465 21 : if (lg(x) == 1 || lg(y) == 1) { set_avma(av); return cgetg(1,t_MAT); }
2466 14 : x = Q_remove_denom(x, &dx);
2467 14 : y = Q_remove_denom(y, &dy);
2468 14 : if (dx) y = ZM_Z_mul(y, dx);
2469 14 : if (dy) x = ZM_Z_mul(x, dy);
2470 14 : xZ = gcoeff(x,1,1);
2471 14 : yZ = gcoeff(y,1,1);
2472 14 : dx = mul_denom(dx,dy);
2473 14 : z = ZM_lll(shallowconcat(x,y), 0.99, LLL_KER); lz = lg(z);
2474 14 : lx = lg(x);
2475 63 : for (i=1; i<lz; i++) setlg(z[i], lx);
2476 14 : z = ZM_hnfmodid(ZM_mul(x,z), lcmii(xZ, yZ));
2477 14 : if (dx) z = RgM_Rg_div(z,dx);
2478 14 : return gerepileupto(av,z);
2479 : }
2480 :
2481 : /*******************************************************************/
2482 : /* */
2483 : /* T2-IDEAL REDUCTION */
2484 : /* */
2485 : /*******************************************************************/
2486 :
2487 : static GEN
2488 21 : chk_vdir(GEN nf, GEN vdir)
2489 : {
2490 21 : long i, l = lg(vdir);
2491 : GEN v;
2492 21 : if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
2493 14 : switch(typ(vdir))
2494 : {
2495 0 : case t_VECSMALL: return vdir;
2496 14 : case t_VEC: break;
2497 0 : default: pari_err_TYPE("idealred",vdir);
2498 : }
2499 14 : v = cgetg(l, t_VECSMALL);
2500 56 : for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
2501 14 : return v;
2502 : }
2503 :
2504 : static void
2505 12812 : twistG(GEN G, long r1, long i, long v)
2506 : {
2507 12812 : long j, lG = lg(G);
2508 12812 : if (i <= r1) {
2509 37498 : for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
2510 : } else {
2511 726 : long k = (i<<1) - r1;
2512 5232 : for (j=1; j<lG; j++)
2513 : {
2514 4506 : gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
2515 4506 : gcoeff(G,k ,j) = gmul2n(gcoeff(G,k ,j), v);
2516 : }
2517 : }
2518 12812 : }
2519 :
2520 : GEN
2521 135945 : nf_get_Gtwist(GEN nf, GEN vdir)
2522 : {
2523 : long i, l, v, r1;
2524 : GEN G;
2525 :
2526 135945 : if (!vdir) return nf_get_roundG(nf);
2527 21 : if (typ(vdir) == t_MAT)
2528 : {
2529 0 : long N = nf_get_degree(nf);
2530 0 : if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
2531 0 : return vdir;
2532 : }
2533 21 : vdir = chk_vdir(nf, vdir);
2534 14 : G = RgM_shallowcopy(nf_get_G(nf));
2535 14 : r1 = nf_get_r1(nf);
2536 14 : l = lg(vdir);
2537 56 : for (i=1; i<l; i++)
2538 : {
2539 42 : v = vdir[i]; if (!v) continue;
2540 42 : twistG(G, r1, i, v);
2541 : }
2542 14 : return RM_round_maxrank(G);
2543 : }
2544 : GEN
2545 12770 : nf_get_Gtwist1(GEN nf, long i)
2546 : {
2547 12770 : GEN G = RgM_shallowcopy( nf_get_G(nf) );
2548 12770 : long r1 = nf_get_r1(nf);
2549 12770 : twistG(G, r1, i, 10);
2550 12770 : return RM_round_maxrank(G);
2551 : }
2552 :
2553 : GEN
2554 96095 : RM_round_maxrank(GEN G0)
2555 : {
2556 96095 : long e, r = lg(G0)-1;
2557 96095 : pari_sp av = avma;
2558 96095 : for (e = 4; ; e <<= 1, set_avma(av))
2559 0 : {
2560 96095 : GEN G = gmul2n(G0, e), H = ground(G);
2561 96094 : if (ZM_rank(H) == r) return H; /* maximal rank ? */
2562 : }
2563 : }
2564 :
2565 : GEN
2566 135938 : idealred0(GEN nf, GEN I, GEN vdir)
2567 : {
2568 135938 : pari_sp av = avma;
2569 135938 : GEN G, aI, IZ, J, y, my, yi, c1 = NULL;
2570 : long N;
2571 :
2572 135938 : nf = checknf(nf);
2573 135938 : N = nf_get_degree(nf);
2574 : /* put first for sanity checks, unused when I obviously principal */
2575 135938 : G = nf_get_Gtwist(nf, vdir);
2576 135931 : switch (idealtyp(&I,&aI))
2577 : {
2578 34498 : case id_PRIME:
2579 34498 : if (pr_is_inert(I)) {
2580 585 : if (!aI) { set_avma(av); return matid(N); }
2581 585 : c1 = gel(I,1); I = matid(N);
2582 585 : goto END;
2583 : }
2584 33913 : IZ = pr_get_p(I);
2585 33913 : J = pr_inv_p(I);
2586 33913 : I = idealhnf_two(nf,I);
2587 33913 : break;
2588 101405 : case id_MAT:
2589 101405 : if (lg(I)-1 != N) pari_err_DIM("idealred");
2590 101398 : I = Q_primitive_part(I, &c1);
2591 101399 : IZ = gcoeff(I,1,1);
2592 101399 : if (is_pm1(IZ))
2593 : {
2594 8687 : if (!aI) { set_avma(av); return matid(N); }
2595 8652 : goto END;
2596 : }
2597 92712 : J = idealHNF_inv_Z(nf, I);
2598 92712 : break;
2599 21 : default: /* id_PRINCIPAL, silly case */
2600 21 : if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
2601 21 : if (!aI) return I;
2602 14 : goto END;
2603 : }
2604 : /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
2605 126625 : y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
2606 126624 : if (equalii(ZV_content(y), IZ))
2607 : { /* already reduced */
2608 68673 : if (!aI) return gerepilecopy(av, I);
2609 65181 : goto END;
2610 : }
2611 :
2612 57951 : my = zk_multable(nf, y);
2613 57951 : I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
2614 57951 : c1 = mul_content(c1, IZ);
2615 57951 : if (equali1(c1)) c1 = NULL; /* can be simplified with IZ */
2616 57951 : yi = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
2617 57951 : I = hnfmodid(I, Q_denom(yi)); /* denom(yi) generates (y) \cap Z */
2618 57951 : if (!aI) return gerepileupto(av, I);
2619 56250 : if (typ(aI) == t_MAT) /* yi is not integral and usually larger than y */
2620 41669 : aI = famat_div(aI, y);
2621 : else
2622 14581 : c1 = c1? RgC_Rg_mul(yi, c1): yi;
2623 130680 : END:
2624 130680 : if (c1) aI = ext_mul(nf, aI,c1);
2625 130682 : return gerepilecopy(av, mkvec2(I, aI));
2626 : }
2627 :
2628 : /* I integral ZM (not HNF), G ZM, rounded Cholesky form of a weighted
2629 : * T2 matrix. Reduce I wrt G */
2630 : GEN
2631 1282324 : idealpseudored(GEN I, GEN G)
2632 1282324 : { return ZM_mul(I, ZM_lll(ZM_mul(G, I), 0.99, LLL_IM)); }
2633 :
2634 : /* Same I, G; m in I with T2(m) small */
2635 : GEN
2636 140427 : idealpseudomin(GEN I, GEN G)
2637 : {
2638 140427 : GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
2639 140426 : return ZM_ZC_mul(I, gel(u,1));
2640 : }
2641 : /* Same I,G; irrational m in I with T2(m) small */
2642 : GEN
2643 0 : idealpseudomin_nonscalar(GEN I, GEN G)
2644 : {
2645 0 : GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
2646 0 : GEN m = ZM_ZC_mul(I, gel(u,1));
2647 0 : if (ZV_isscalar(m) && lg(u) > 2) m = ZM_ZC_mul(I, gel(u,2));
2648 0 : return m;
2649 : }
2650 : /* Same I,G; t_VEC of irrational m in I with T2(m) small */
2651 : GEN
2652 1203448 : idealpseudominvec(GEN I, GEN G)
2653 : {
2654 1203448 : long i, j, k, n = lg(I)-1;
2655 1203448 : GEN x, L, b = idealpseudored(I, G);
2656 1203445 : L = cgetg(1 + (n*(n+1))/2, t_VEC);
2657 4249079 : for (i = k = 1; i <= n; i++)
2658 : {
2659 3045632 : x = gel(b,i);
2660 3045632 : if (!ZV_isscalar(x)) gel(L,k++) = x;
2661 : }
2662 3045632 : for (i = 2; i <= n; i++)
2663 : {
2664 1842189 : long J = minss(i, 4);
2665 4576483 : for (j = 1; j < J; j++)
2666 : {
2667 2734298 : x = ZC_add(gel(b,i),gel(b,j));
2668 2734294 : if (!ZV_isscalar(x)) gel(L,k++) = x;
2669 : }
2670 : }
2671 1203443 : setlg(L,k); return L;
2672 : }
2673 :
2674 : GEN
2675 13795 : idealred_elt(GEN nf, GEN I)
2676 : {
2677 13795 : pari_sp av = avma;
2678 13795 : GEN u = idealpseudomin(I, nf_get_roundG(nf));
2679 13795 : return gerepileupto(av, u);
2680 : }
2681 :
2682 : GEN
2683 7 : idealmin(GEN nf, GEN x, GEN vdir)
2684 : {
2685 7 : pari_sp av = avma;
2686 : GEN y, dx;
2687 7 : nf = checknf(nf);
2688 7 : switch( idealtyp(&x, NULL) )
2689 : {
2690 0 : case id_PRINCIPAL: return gcopy(x);
2691 0 : case id_PRIME: x = pr_hnf(nf,x); break;
2692 7 : case id_MAT: if (lg(x) == 1) return gen_0;
2693 : }
2694 7 : x = Q_remove_denom(x, &dx);
2695 7 : y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
2696 7 : if (dx) y = RgC_Rg_div(y, dx);
2697 7 : return gerepileupto(av, y);
2698 : }
2699 :
2700 : /*******************************************************************/
2701 : /* */
2702 : /* APPROXIMATION THEOREM */
2703 : /* */
2704 : /*******************************************************************/
2705 : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
2706 : * and ppo(a,b) = Z_ppo(a,b) */
2707 : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
2708 : GEN
2709 456217 : Z_ppio(GEN a, GEN b)
2710 : {
2711 456217 : GEN x, y, d = gcdii(a,b);
2712 456217 : if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
2713 346926 : x = d; y = diviiexact(a,d);
2714 : for(;;)
2715 63084 : {
2716 410010 : GEN g = gcdii(x,y);
2717 410010 : if (is_pm1(g)) return mkvec3(d, x, y);
2718 63084 : x = mulii(x,g); y = diviiexact(y,g);
2719 : }
2720 : }
2721 : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
2722 : * and pple all others */
2723 : /* return gcd(a,b),ppg(a,b),pple(a,b) */
2724 : GEN
2725 0 : Z_ppgle(GEN a, GEN b)
2726 : {
2727 0 : GEN x, y, g, d = gcdii(a,b);
2728 0 : if (equalii(a, d)) return mkvec3(a, gen_1, a);
2729 0 : x = diviiexact(a,d); y = d;
2730 : for(;;)
2731 : {
2732 0 : g = gcdii(x,y);
2733 0 : if (is_pm1(g)) return mkvec3(d, x, y);
2734 0 : x = mulii(x,g); y = diviiexact(y,g);
2735 : }
2736 : }
2737 : static void
2738 0 : Z_dcba_rec(GEN L, GEN a, GEN b)
2739 : {
2740 : GEN x, r, v, g, h, c, c0;
2741 : long n;
2742 0 : if (is_pm1(b)) {
2743 0 : if (!is_pm1(a)) vectrunc_append(L, a);
2744 0 : return;
2745 : }
2746 0 : v = Z_ppio(a,b);
2747 0 : a = gel(v,2);
2748 0 : r = gel(v,3);
2749 0 : if (!is_pm1(r)) vectrunc_append(L, r);
2750 0 : v = Z_ppgle(a,b);
2751 0 : g = gel(v,1);
2752 0 : h = gel(v,2);
2753 0 : x = c0 = gel(v,3);
2754 0 : for (n = 1; !is_pm1(h); n++)
2755 : {
2756 : GEN d, y;
2757 : long i;
2758 0 : v = Z_ppgle(h,sqri(g));
2759 0 : g = gel(v,1);
2760 0 : h = gel(v,2);
2761 0 : c = gel(v,3); if (is_pm1(c)) continue;
2762 0 : d = gcdii(c,b);
2763 0 : x = mulii(x,d);
2764 0 : y = d; for (i=1; i < n; i++) y = sqri(y);
2765 0 : Z_dcba_rec(L, diviiexact(c,y), d);
2766 : }
2767 0 : Z_dcba_rec(L,diviiexact(b,x), c0);
2768 : }
2769 : static GEN
2770 3089079 : Z_cba_rec(GEN L, GEN a, GEN b)
2771 : {
2772 : GEN g;
2773 3089079 : if (lg(L) > 10)
2774 : { /* a few naive steps before switching to dcba */
2775 0 : Z_dcba_rec(L, a, b);
2776 0 : return gel(L, lg(L)-1);
2777 : }
2778 3089079 : if (is_pm1(a)) return b;
2779 1835522 : g = gcdii(a,b);
2780 1835522 : if (is_pm1(g)) { vectrunc_append(L, a); return b; }
2781 1371192 : a = diviiexact(a,g);
2782 1371192 : b = diviiexact(b,g);
2783 1371192 : return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
2784 : }
2785 : GEN
2786 346695 : Z_cba(GEN a, GEN b)
2787 : {
2788 346695 : GEN L = vectrunc_init(expi(a) + expi(b) + 2);
2789 346695 : GEN t = Z_cba_rec(L, a, b);
2790 346695 : if (!is_pm1(t)) vectrunc_append(L, t);
2791 346695 : return L;
2792 : }
2793 : /* P = coprime base, extend it by b; TODO: quadratic for now */
2794 : GEN
2795 28 : ZV_cba_extend(GEN P, GEN b)
2796 : {
2797 28 : long i, l = lg(P);
2798 28 : GEN w = cgetg(l+1, t_VEC);
2799 91 : for (i = 1; i < l; i++)
2800 : {
2801 63 : GEN v = Z_cba(gel(P,i), b);
2802 63 : long nv = lg(v)-1;
2803 63 : gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
2804 63 : b = gel(v,nv);
2805 : }
2806 28 : gel(w,l) = b; return shallowconcat1(w);
2807 : }
2808 : GEN
2809 28 : ZV_cba(GEN v)
2810 : {
2811 28 : long i, l = lg(v);
2812 : GEN P;
2813 28 : if (l <= 2) return v;
2814 14 : P = Z_cba(gel(v,1), gel(v,2));
2815 42 : for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
2816 14 : return P;
2817 : }
2818 :
2819 : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
2820 : GEN
2821 3359654 : Z_ppo(GEN x, GEN f)
2822 : {
2823 : for (;;)
2824 : {
2825 3359654 : f = gcdii(x, f); if (is_pm1(f)) break;
2826 2303328 : x = diviiexact(x, f);
2827 : }
2828 1056327 : return x;
2829 : }
2830 : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
2831 : ulong
2832 66762377 : u_ppo(ulong x, ulong f)
2833 : {
2834 : for (;;)
2835 : {
2836 66762377 : f = ugcd(x, f); if (f == 1) break;
2837 14834530 : x /= f;
2838 : }
2839 51927790 : return x;
2840 : }
2841 :
2842 : /* result known to be representable as an ulong */
2843 : static ulong
2844 2178052 : lcmuu(ulong a, ulong b) { ulong d = ugcd(a,b); return (a/d) * b; }
2845 :
2846 : /* assume 0 < x < N; return u in (Z/NZ)^* such that u x = gcd(x,N) (mod N);
2847 : * set *pd = gcd(x,N) */
2848 : ulong
2849 6298088 : Fl_invgen(ulong x, ulong N, ulong *pd)
2850 : {
2851 : ulong d, d0, e, v, v1;
2852 : long s;
2853 6298088 : *pd = d = xgcduu(N, x, 0, &v, &v1, &s);
2854 6298436 : if (s > 0) v = N - v;
2855 6298436 : if (d == 1) return v;
2856 : /* vx = gcd(x,N) (mod N), v coprime to N/d but need not be coprime to N */
2857 3260707 : e = N / d;
2858 3260707 : d0 = u_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, rad(d1) | N/d */
2859 3260800 : if (d0 == 1) return v;
2860 2177998 : e = lcmuu(e, d / d0);
2861 2178051 : return u_chinese_coprime(v, 1, e, d0, e*d0);
2862 : }
2863 :
2864 : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
2865 : static GEN
2866 196 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
2867 : {
2868 196 : long v, j, lp = lg(listpr), N = nf_get_degree(nf);
2869 : GEN x1, x2, ex;
2870 :
2871 : #if 0 /*1) via many gcds. Expensive ! */
2872 : GEN f = idealprodprime(nf, listpr);
2873 : f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
2874 : x = scalarmat(x, N);
2875 : for (;;)
2876 : {
2877 : if (gequal1(gcoeff(f,1,1))) break;
2878 : x = idealdivexact(nf, x, f);
2879 : f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
2880 : }
2881 : x2 = x;
2882 : #else /*2) from prime decomposition */
2883 196 : x1 = NULL;
2884 532 : for (j=1; j<lp; j++)
2885 : {
2886 336 : GEN pr = gel(listpr,j);
2887 336 : v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
2888 :
2889 196 : ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
2890 196 : x1 = x1? idealmulpowprime(nf, x1, pr, ex)
2891 196 : : idealpow(nf, pr, ex);
2892 : }
2893 196 : x = scalarmat(x, N);
2894 196 : x2 = x1? idealdivexact(nf, x, x1): x;
2895 : #endif
2896 196 : return x2;
2897 : }
2898 :
2899 : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f */
2900 : GEN
2901 10654 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
2902 : {
2903 : GEN fZ, t, L, D2, d1, d2, d;
2904 :
2905 10654 : L = Q_remove_denom(L0, &d);
2906 10654 : if (!d) return L0;
2907 :
2908 : /* L0 = L / d, L integral */
2909 672 : fZ = gcoeff(f,1,1);
2910 672 : if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
2911 : /* Kill denom part coprime to fZ */
2912 196 : d2 = Z_ppo(d, fZ);
2913 196 : t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
2914 196 : if (equalii(d, d2)) return L;
2915 :
2916 196 : d1 = diviiexact(d, d2);
2917 : /* L0 = (L / d1) mod f. d1 not coprime to f
2918 : * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
2919 196 : D2 = nf_coprime_part(nf, d1, listpr);
2920 196 : t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
2921 196 : L = nfmuli(nf,t,L);
2922 :
2923 : /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
2924 196 : return Q_div_to_int(L, d1); /* exact division */
2925 : }
2926 :
2927 : /* assume L is a list of prime ideals. Return the product */
2928 : GEN
2929 342 : idealprodprime(GEN nf, GEN L)
2930 : {
2931 342 : long l = lg(L), i;
2932 : GEN z;
2933 342 : if (l == 1) return matid(nf_get_degree(nf));
2934 342 : z = pr_hnf(nf, gel(L,1));
2935 376 : for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
2936 342 : return z;
2937 : }
2938 :
2939 : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
2940 : GEN
2941 427 : idealprod(GEN nf, GEN I)
2942 : {
2943 427 : long i, l = lg(I);
2944 : GEN z;
2945 1046 : for (i = 1; i < l; i++)
2946 1039 : if (!equali1(gel(I,i))) break;
2947 427 : if (i == l) return gen_1;
2948 420 : z = gel(I,i);
2949 690 : for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
2950 420 : return z;
2951 : }
2952 :
2953 : /* v_pr(idealprod(nf,I)) */
2954 : long
2955 2820 : idealprodval(GEN nf, GEN I, GEN pr)
2956 : {
2957 2820 : long i, l = lg(I), v = 0;
2958 16118 : for (i = 1; i < l; i++)
2959 13298 : if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
2960 2820 : return v;
2961 : }
2962 :
2963 : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
2964 : GEN
2965 56222 : factorbackprime(GEN nf, GEN L, GEN e)
2966 : {
2967 56222 : long l = lg(L), i;
2968 : GEN z;
2969 :
2970 56222 : if (l == 1) return matid(nf_get_degree(nf));
2971 42811 : z = idealpow(nf, gel(L,1), gel(e,1));
2972 66603 : for (i=2; i<l; i++)
2973 23792 : if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
2974 42811 : return z;
2975 : }
2976 :
2977 : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
2978 : * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
2979 : GEN
2980 60294 : pr_uniformizer(GEN pr, GEN F)
2981 : {
2982 60294 : GEN p = pr_get_p(pr), t = pr_get_gen(pr);
2983 60294 : if (!equalii(F, p))
2984 : {
2985 38789 : long e = pr_get_e(pr);
2986 38789 : GEN u, v, q = (e == 1)? sqri(p): p;
2987 38789 : u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
2988 38789 : v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
2989 38789 : if (pr_is_inert(pr))
2990 28 : t = addii(mulii(p, v), u);
2991 : else
2992 : {
2993 38761 : t = ZC_Z_mul(t, v);
2994 38761 : gel(t,1) = addii(gel(t,1), u); /* return u + vt */
2995 : }
2996 : }
2997 60294 : return t;
2998 : }
2999 : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
3000 : GEN
3001 82544 : prV_lcm_capZ(GEN L)
3002 : {
3003 82544 : long i, r = lg(L);
3004 : GEN F;
3005 82544 : if (r == 1) return gen_1;
3006 70006 : F = pr_get_p(gel(L,1));
3007 125105 : for (i = 2; i < r; i++)
3008 : {
3009 55099 : GEN pr = gel(L,i), p = pr_get_p(pr);
3010 55099 : if (!dvdii(F, p)) F = mulii(F,p);
3011 : }
3012 70006 : return F;
3013 : }
3014 : /* v vector of prid. Return underlying list of rational primes */
3015 : GEN
3016 40187 : prV_primes(GEN v)
3017 : {
3018 40187 : long i, l = lg(v);
3019 40187 : GEN w = cgetg(l,t_VEC);
3020 110675 : for (i=1; i<l; i++) gel(w,i) = pr_get_p(gel(v,i));
3021 40187 : return ZV_sort_uniq(w);
3022 : }
3023 :
3024 : /* Given a prime ideal factorization with possibly zero or negative
3025 : * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
3026 : * and v_pr(b) >= 0 for all other pr.
3027 : * For optimal performance, all [anti-]uniformizers should be precomputed,
3028 : * but no support for this yet. If nored, do not reduce result. */
3029 : static GEN
3030 54670 : idealapprfact_i(GEN nf, GEN x, int nored)
3031 : {
3032 54670 : GEN d = NULL, z, L, e, e2, F;
3033 : long i, r;
3034 54670 : int hasden = 0;
3035 :
3036 54670 : nf = checknf(nf);
3037 54670 : L = gel(x,1);
3038 54670 : e = gel(x,2);
3039 54670 : F = prV_lcm_capZ(L);
3040 54669 : z = NULL; r = lg(e);
3041 139459 : for (i = 1; i < r; i++)
3042 : {
3043 84790 : long s = signe(gel(e,i));
3044 : GEN pi, q;
3045 84790 : if (!s) continue;
3046 55940 : if (s < 0) hasden = 1;
3047 55940 : pi = pr_uniformizer(gel(L,i), F);
3048 55940 : q = nfpow(nf, pi, gel(e,i));
3049 55940 : z = z? nfmul(nf, z, q): q;
3050 : }
3051 54669 : if (!z) return gen_1;
3052 27483 : if (hasden) /* denominator */
3053 : {
3054 10032 : z = Q_remove_denom(z, &d);
3055 10032 : d = diviiexact(d, Z_ppo(d, F));
3056 : }
3057 27482 : if (nored || typ(z) != t_COL) return d? gdiv(z, d): z;
3058 10031 : e2 = cgetg(r, t_VEC);
3059 28458 : for (i = 1; i < r; i++) gel(e2,i) = addiu(gel(e,i), 1);
3060 10032 : x = factorbackprime(nf, L, e2);
3061 10032 : if (d) x = RgM_Rg_mul(x, d);
3062 10031 : z = ZC_reducemodlll(z, x);
3063 10030 : return d? RgC_Rg_div(z,d): z;
3064 : }
3065 :
3066 : GEN
3067 0 : idealapprfact(GEN nf, GEN x) {
3068 0 : pari_sp av = avma;
3069 0 : return gerepileupto(av, idealapprfact_i(nf, x, 0));
3070 : }
3071 : GEN
3072 14 : idealappr(GEN nf, GEN x) {
3073 14 : pari_sp av = avma;
3074 14 : if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
3075 14 : return gerepileupto(av, idealapprfact_i(nf, x, 0));
3076 : }
3077 :
3078 : /* OBSOLETE */
3079 : GEN
3080 14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
3081 :
3082 : static GEN
3083 21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
3084 : {
3085 21 : GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
3086 21 : long i, r = lg(E);
3087 84 : for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
3088 21 : return idealapprfact_i(nf,F,1);
3089 : }
3090 :
3091 : static void
3092 14 : not_in_ideal(GEN a) {
3093 14 : pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
3094 0 : }
3095 : /* x integral in HNF, a an 'nf' */
3096 : static int
3097 28 : in_ideal(GEN x, GEN a)
3098 : {
3099 28 : switch(typ(a))
3100 : {
3101 14 : case t_INT: return dvdii(a, gcoeff(x,1,1));
3102 7 : case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
3103 7 : default: return 0;
3104 : }
3105 : }
3106 :
3107 : /* Given an integral ideal x and a in x, gives a b such that
3108 : * x = aZ_K + bZ_K using the approximation theorem */
3109 : GEN
3110 42 : idealtwoelt2(GEN nf, GEN x, GEN a)
3111 : {
3112 42 : pari_sp av = avma;
3113 : GEN cx, b;
3114 :
3115 42 : nf = checknf(nf);
3116 42 : a = nf_to_scalar_or_basis(nf, a);
3117 42 : x = idealhnf_shallow(nf,x);
3118 42 : if (lg(x) == 1)
3119 : {
3120 14 : if (!isintzero(a)) not_in_ideal(a);
3121 7 : set_avma(av); return gen_0;
3122 : }
3123 28 : x = Q_primitive_part(x, &cx);
3124 28 : if (cx) a = gdiv(a, cx);
3125 28 : if (!in_ideal(x, a)) not_in_ideal(a);
3126 21 : b = mat_ideal_two_elt2(nf, x, a);
3127 21 : if (typ(b) == t_COL)
3128 : {
3129 14 : GEN mod = idealhnf_principal(nf,a);
3130 14 : b = ZC_hnfrem(b,mod);
3131 14 : if (ZV_isscalar(b)) b = gel(b,1);
3132 : }
3133 : else
3134 : {
3135 7 : GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
3136 7 : b = centermodii(b, aZ, shifti(aZ,-1));
3137 : }
3138 21 : b = cx? gmul(b,cx): gcopy(b);
3139 21 : return gerepileupto(av, b);
3140 : }
3141 :
3142 : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
3143 : * beta * x is an integral ideal coprime to y */
3144 : GEN
3145 37209 : idealcoprimefact(GEN nf, GEN x, GEN fy)
3146 : {
3147 37209 : GEN L = gel(fy,1), e;
3148 37209 : long i, r = lg(L);
3149 :
3150 37209 : e = cgetg(r, t_COL);
3151 76092 : for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
3152 37212 : return idealapprfact_i(nf, mkmat2(L,e), 0);
3153 : }
3154 : GEN
3155 84 : idealcoprime(GEN nf, GEN x, GEN y)
3156 : {
3157 84 : pari_sp av = avma;
3158 84 : return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
3159 : }
3160 :
3161 : GEN
3162 7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
3163 : {
3164 7 : pari_sp av = avma;
3165 7 : GEN z, p, pr = modpr, T;
3166 :
3167 7 : nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
3168 0 : x = nf_to_Fq(nf,x,modpr);
3169 0 : y = nf_to_Fq(nf,y,modpr);
3170 0 : z = Fq_mul(x,y,T,p);
3171 0 : return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
3172 : }
3173 :
3174 : GEN
3175 0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
3176 : {
3177 0 : pari_sp av = avma;
3178 0 : nf = checknf(nf);
3179 0 : return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
3180 : }
3181 :
3182 : GEN
3183 0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
3184 : {
3185 0 : pari_sp av=avma;
3186 0 : GEN z, T, p, pr = modpr;
3187 :
3188 0 : nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
3189 0 : z = nf_to_Fq(nf,x,modpr);
3190 0 : z = Fq_pow(z,k,T,p);
3191 0 : return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
3192 : }
3193 :
3194 : GEN
3195 0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
3196 : {
3197 0 : pari_sp av = avma;
3198 0 : GEN T, p, pr = modpr;
3199 :
3200 0 : nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
3201 0 : if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
3202 0 : x = nfM_to_FqM(x, nf, modpr);
3203 0 : return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
3204 : }
3205 :
3206 : GEN
3207 0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
3208 : {
3209 0 : const char *f = "nfsolvemodpr";
3210 0 : pari_sp av = avma;
3211 : GEN T, p, modpr;
3212 :
3213 0 : nf = checknf(nf);
3214 0 : modpr = nf_to_Fq_init(nf, &pr,&T,&p);
3215 0 : if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
3216 0 : a = nfM_to_FqM(a, nf, modpr);
3217 0 : switch(typ(b))
3218 : {
3219 0 : case t_MAT:
3220 0 : b = nfM_to_FqM(b, nf, modpr);
3221 0 : b = FqM_gauss(a,b,T,p);
3222 0 : if (!b) pari_err_INV(f,a);
3223 0 : a = FqM_to_nfM(b, modpr);
3224 0 : break;
3225 0 : case t_COL:
3226 0 : b = nfV_to_FqV(b, nf, modpr);
3227 0 : b = FqM_FqC_gauss(a,b,T,p);
3228 0 : if (!b) pari_err_INV(f,a);
3229 0 : a = FqV_to_nfV(b, modpr);
3230 0 : break;
3231 0 : default: pari_err_TYPE(f,b);
3232 : }
3233 0 : return gerepilecopy(av, a);
3234 : }
|