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 8049235 : idealtyp(GEN *ideal, GEN *arch)
47 : {
48 8049235 : GEN x = *ideal;
49 8049235 : long t,lx,tx = typ(x);
50 :
51 8049235 : if (tx!=t_VEC || lg(x)!=3) { if (arch) *arch = NULL; }
52 : else
53 : {
54 1232831 : GEN a = gel(x,2);
55 1232831 : 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 1232817 : if (arch) *arch = a;
62 1232824 : x = gel(x,1); tx = typ(x);
63 : }
64 8049228 : switch(tx)
65 : {
66 4694590 : case t_MAT: lx = lg(x);
67 4694590 : if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
68 4694429 : if (lx != lgcols(x)) pari_err_TYPE("idealtyp [nonsquare t_MAT]",x);
69 4694410 : t = id_MAT;
70 4694410 : break;
71 :
72 2559193 : case t_VEC: if (lg(x)!=6) pari_err_TYPE("idealtyp",x);
73 2559160 : t = id_PRIME; break;
74 :
75 795567 : case t_POL: case t_POLMOD: case t_COL:
76 : case t_INT: case t_FRAC:
77 795567 : t = id_PRINCIPAL; break;
78 0 : default:
79 0 : pari_err_TYPE("idealtyp",x);
80 : return 0; /*LCOV_EXCL_LINE*/
81 : }
82 8049298 : *ideal = x; return t;
83 : }
84 :
85 : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
86 : GEN
87 524585 : idealhnf_two(GEN nf, GEN v)
88 : {
89 524585 : GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
90 524569 : if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
91 451490 : return ZM_hnfmodid(m, p);
92 : }
93 : /* true nf */
94 : GEN
95 3180638 : pr_hnf(GEN nf, GEN pr)
96 : {
97 3180638 : GEN p = pr_get_p(pr), m;
98 3180604 : if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
99 2751484 : m = zk_scalar_or_multable(nf, pr_get_gen(pr));
100 2751181 : return ZM_hnfmodprime(m, p);
101 : }
102 :
103 : GEN
104 1305645 : idealhnf_principal(GEN nf, GEN x)
105 : {
106 : GEN cx;
107 1305645 : x = nf_to_scalar_or_basis(nf, x);
108 1305641 : switch(typ(x))
109 : {
110 1050908 : case t_COL: break;
111 222439 : case t_INT: if (!signe(x)) return cgetg(1,t_MAT);
112 221676 : return scalarmat(absi_shallow(x), nf_get_degree(nf));
113 32294 : case t_FRAC:
114 32294 : return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
115 0 : default: pari_err_TYPE("idealhnf",x);
116 : }
117 1050908 : x = Q_primitive_part(x, &cx);
118 1050895 : RgV_check_ZV(x, "idealhnf");
119 1050896 : x = zk_multable(nf, x);
120 1050908 : x = ZM_hnfmodid(x, zkmultable_capZ(x));
121 1050911 : 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 1583188 : idealhnf_shallow(GEN nf, GEN x)
137 : {
138 1583188 : long tx = typ(x), lx = lg(x), N;
139 :
140 : /* cannot use idealtyp because here we allow nonsquare matrices */
141 1583188 : if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
142 1583188 : if (tx == t_VEC && lx == 6) return pr_hnf(nf,x); /* PRIME */
143 1199707 : switch(tx)
144 : {
145 100864 : case t_MAT:
146 : {
147 : GEN cx;
148 100864 : long nx = lx-1;
149 100864 : N = nf_get_degree(nf);
150 100864 : if (nx == 0) return cgetg(1, t_MAT);
151 100843 : if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
152 100836 : if (nx == 1) return idealhnf_principal(nf, gel(x,1));
153 :
154 84527 : if (nx == N && RgM_is_ZM(x) && ZM_ishnf(x)) return x;
155 49259 : x = Q_primitive_part(x, &cx);
156 49259 : if (nx < N) x = vec_mulid(nf, x, nx, N);
157 49259 : x = ZM_hnfmod(x, ZM_detmult(x));
158 49259 : 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 1098829 : default: return idealhnf_principal(nf, x); /* PRINCIPAL */
181 : }
182 : }
183 : /* true nf */
184 : GEN
185 301 : idealhnf(GEN nf, GEN x)
186 : {
187 301 : pari_sp av = avma;
188 301 : GEN y = idealhnf_shallow(nf, x);
189 294 : 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 77 : 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 70 : hnf_Z_QC(GEN nf, GEN a, GEN b)
240 : {
241 : GEN db;
242 70 : b = Q_remove_denom(b, &db);
243 70 : if (db) a = mulii(a, db);
244 70 : b = hnf_Z_ZC(nf,a,b);
245 70 : 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 77 : hnf_Q_QC(GEN nf, GEN a, GEN b)
250 : {
251 : GEN da, db;
252 77 : 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 210 : idealhnf0(GEN nf, GEN a, GEN b)
288 : {
289 : long ta, tb;
290 : pari_sp av;
291 : GEN x;
292 210 : nf = checknf(nf);
293 210 : if (!b) return idealhnf(nf,a);
294 :
295 : /* HNF of aZ_K+bZ_K */
296 112 : av = avma;
297 112 : a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
298 112 : b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
299 105 : 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 91 : x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
303 105 : 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 226478 : ok_elt(GEN x, GEN xZ, GEN y)
315 : {
316 226478 : pari_sp av = avma;
317 226478 : 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 65441 : addmul_mat(GEN a, GEN s, GEN b)
323 : {
324 65441 : if (!signe(s)) return a;
325 56957 : if (!equali1(s)) b = ZM_Z_mul(b, s);
326 56956 : return a? ZM_add(a, b): b;
327 : }
328 :
329 : static GEN
330 120095 : get_random_a(GEN nf, GEN x, GEN xZ)
331 : {
332 : pari_sp av;
333 120095 : long i, lm, l = lg(x);
334 : GEN z, beta, mul;
335 :
336 120095 : beta= cgetg(l, t_MAT);
337 120095 : mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
338 : /* look for a in x such that a O/xZ = x O/xZ */
339 251042 : for (i = 2; i < l; i++)
340 : {
341 241156 : GEN xi = gel(x,i);
342 241156 : GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
343 241151 : if (gequal0(t)) continue;
344 198541 : if (ok_elt(x,xZ, t)) return xi;
345 88332 : gel(beta,lm) = xi;
346 : /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
347 88332 : gel(mul,lm) = t; lm++;
348 : }
349 9886 : setlg(mul, lm);
350 9886 : setlg(beta,lm); z = cgetg(lm, t_VEC);
351 29784 : for(av = avma;; set_avma(av))
352 19898 : {
353 29784 : GEN a = NULL;
354 95225 : for (i = 1; i < lm; i++)
355 : {
356 65441 : gel(z,i) = randomi(xZ);
357 65441 : 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 29784 : if (a && ok_elt(x,xZ, a)) break;
361 : }
362 9886 : return ZM_ZC_mul(beta, z);
363 : }
364 :
365 : /* x square matrix, assume it is HNF */
366 : static GEN
367 254251 : mat_ideal_two_elt(GEN nf, GEN x)
368 : {
369 : GEN y, a, cx, xZ;
370 254251 : long N = nf_get_degree(nf);
371 : pari_sp av, tetpil;
372 :
373 254251 : if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
374 254237 : if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
375 :
376 136076 : y = cgetg(3,t_VEC); av = avma;
377 136076 : cx = Q_content(x);
378 136077 : xZ = gcoeff(x,1,1);
379 136077 : if (gequal(xZ, cx)) /* x = (cx) */
380 : {
381 5187 : gel(y,1) = cx;
382 5187 : gel(y,2) = gen_0; return y;
383 : }
384 130888 : if (equali1(cx)) cx = NULL;
385 : else
386 : {
387 3233 : x = Q_div_to_int(x, cx);
388 3233 : xZ = gcoeff(x,1,1);
389 : }
390 130889 : if (N < 6)
391 111706 : a = get_random_a(nf, x, xZ);
392 : else
393 : {
394 19183 : 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 19183 : GEN P, E, a1 = Z_lsmoothen(xZ, (GEN)FB, &P, &E);
398 19184 : if (!a1) /* factors completely */
399 10794 : a = idealapprfact_i(nf, idealfactor(nf,x), 1);
400 8390 : else if (lg(P) == 1) /* no small factors */
401 2547 : 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 5843 : a0 = diviiexact(xZ, a1);
406 5843 : A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
407 5843 : A1 = ZM_hnfmodid(x, a1); /* cofactor */
408 5843 : pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
409 5843 : pi1 = get_random_a(nf, A1, a1);
410 5843 : (void)bezout(a0, a1, &v0,&v1);
411 5843 : u0 = mulii(a0, v0);
412 5843 : u1 = mulii(a1, v1);
413 5843 : if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
414 : else
415 5843 : { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
416 5843 : u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
417 5843 : a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
418 : }
419 : }
420 130890 : if (cx)
421 : {
422 3233 : a = centermod(a, xZ);
423 3233 : tetpil = avma;
424 3233 : 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 3156 : gel(y,1) = gmul(xZ, cx);
432 3156 : gel(y,2) = RgC_Rg_mul(a, cx);
433 : }
434 : }
435 : else
436 : {
437 127657 : tetpil = avma;
438 127657 : gel(y,1) = icopy(xZ);
439 127657 : gel(y,2) = centermod(a, xZ);
440 : }
441 130891 : 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 101985 : idealtwoelt(GEN nf, GEN x)
450 : {
451 : pari_sp av;
452 101985 : long tx = idealtyp(&x, NULL);
453 101977 : nf = checknf(nf);
454 101977 : if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
455 1015 : if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
456 : /* id_PRINCIPAL */
457 1008 : av = avma; x = nf_to_scalar_or_basis(nf, x);
458 1820 : return gerepilecopy(av, typ(x)==t_COL? mkvec2(gen_0,x):
459 903 : 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 732036 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
470 : {
471 732036 : long i, v = Zval, l = lg(x);
472 2237606 : for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
473 732033 : 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 268910 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
480 : {
481 268910 : GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
482 : long i, l;
483 268930 : P = gel(f,1); l = lg(P);
484 268930 : E = gel(f,2);
485 268930 : *pvN = vN = cgetg(l, t_VECSMALL);
486 268938 : *pvZ = vZ = cgetg(l, t_VECSMALL);
487 696092 : for (i = 1; i < l; i++)
488 : {
489 427153 : GEN p = gel(P,i);
490 427153 : vZ[i] = f0? Z_pval(xZ, p): (long) itou(gel(E,i));
491 427150 : vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
492 : }
493 268939 : 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 868322 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
505 : {
506 868322 : 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 868317 : if (Nval < f) return 0;
511 868142 : p = pr_get_p(P);
512 868143 : e = pr_get_e(P);
513 : /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
514 868144 : vmax = minss(Zval * e, Nval / f);
515 868140 : mul = pr_get_tau(P);
516 868137 : l = lg(mul);
517 868137 : B = cgetg(l,t_MAT);
518 : /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
519 868442 : gel(B,1) = gen_0; /* dummy */
520 2557907 : for (j = 2; j < l; j++)
521 : {
522 1897561 : GEN x = gel(A,j);
523 1897561 : gel(B,j) = y = cgetg(l, t_COL);
524 15078228 : for (i = 1; i < l; i++)
525 : { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
526 13388763 : a = mulii(gel(x,1), gcoeff(mul,i,1));
527 123791479 : for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
528 : /* p | a ? */
529 13388335 : gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
530 : }
531 : }
532 660346 : vals = cgetg(l, t_VECSMALL);
533 : /* vals[1] not needed */
534 2200001 : for (j = 2; j < l; j++)
535 : {
536 1539579 : gel(B,j) = Q_primitive_part(gel(B,j), &cx);
537 1539564 : vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
538 : }
539 660422 : pk = powiu(p, ceildivuu(vmax, e));
540 660412 : av = avma; y = cgetg(l,t_COL);
541 : /* can compute mod p^ceil((vmax-v)/e) */
542 1261075 : for (v = 1; v < vmax; v++)
543 : { /* we know v_pr(Bj) >= v for all j */
544 624207 : if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
545 3155089 : for (j = 2; j < l; j++)
546 : {
547 2554447 : GEN x = gel(B,j); if (v < vals[j]) continue;
548 14503433 : for (i = 1; i < l; i++)
549 : {
550 13019063 : pari_sp av2 = avma;
551 13019063 : a = mulii(gel(x,1), gcoeff(mul,i,1));
552 217624204 : 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 13018856 : a = dvmdii(a,p,&r); if (signe(r)) return v;
555 12995134 : if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
556 12995131 : gel(y,i) = gerepileuptoint(av2, a);
557 : }
558 1484370 : gel(B,j) = y; y = x;
559 1484370 : 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 636868 : 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 268908 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
573 : {
574 268908 : const long N = lg(x)-1;
575 : long i, j, k, l, v;
576 268908 : GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
577 :
578 268939 : l = lg(vp);
579 268939 : i = cx? expi(cx)+1: 1;
580 268941 : vP = cgetg((l+i-2)*N+1, t_COL);
581 268937 : vE = cgetg((l+i-2)*N+1, t_COL);
582 696075 : for (i = k = 1; i < l; i++)
583 : {
584 427147 : GEN L, p = gel(vp,i);
585 427147 : long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
586 427145 : if (vc)
587 : {
588 46055 : L = idealprimedec(nf,p);
589 46055 : if (is_pm1(cx)) cx = NULL;
590 : }
591 : else
592 381090 : L = idealprimedec_limit_f(nf,p,Nval);
593 990581 : for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
594 : {
595 563433 : GEN P = gel(L,j);
596 563433 : pari_sp av = avma;
597 563433 : v = idealHNF_val(x, P, Nval, Zval);
598 563401 : set_avma(av);
599 563404 : Nval -= v*pr_get_f(P);
600 563408 : v += vc * pr_get_e(P); if (!v) continue;
601 474316 : gel(vP,k) = P;
602 474316 : gel(vE,k) = utoipos(v); k++;
603 : }
604 471083 : if (vc) for (; j<lg(L); j++)
605 : {
606 43942 : GEN P = gel(L,j);
607 43942 : gel(vP,k) = P;
608 43942 : gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
609 : }
610 : }
611 268928 : if (cx && !FA)
612 : { /* complete factorization */
613 60696 : GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
614 60696 : long lc = lg(cP);
615 130793 : for (i=1; i<lc; i++)
616 : {
617 70097 : GEN p = gel(cP,i), L = idealprimedec(nf,p);
618 70097 : long vc = itos(gel(cE,i));
619 150694 : for (j=1; j<lg(L); j++)
620 : {
621 80597 : GEN P = gel(L,j);
622 80597 : gel(vP,k) = P;
623 80597 : gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
624 : }
625 : }
626 : }
627 268928 : setlg(vP, k);
628 268930 : setlg(vE, k); return mkmat2(vP, vE);
629 : }
630 : /* true nf, x integral ideal */
631 : static GEN
632 223414 : idealHNF_factor(GEN nf, GEN x, ulong lim)
633 : {
634 223414 : GEN cx, F = NULL;
635 223414 : if (lim)
636 : {
637 : GEN P, E;
638 : long i;
639 : /* strict useless because of prime table */
640 70 : F = absZ_factor_limit(gcoeff(x,1,1), lim);
641 70 : P = gel(F,1);
642 70 : E = gel(F,2);
643 : /* filter out entries > lim */
644 119 : for (i = lg(P)-1; i; i--)
645 119 : if (cmpiu(gel(P,i), lim) <= 0) break;
646 70 : setlg(P, i+1);
647 70 : setlg(E, i+1);
648 : }
649 223414 : x = Q_primitive_part(x, &cx);
650 223385 : 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 27419 : prV_e_muls(GEN L, long c)
655 : {
656 27419 : long j, l = lg(L);
657 27419 : GEN z = cgetg(l, t_COL);
658 56430 : for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
659 27442 : return z;
660 : }
661 : /* true nf, y in Q */
662 : static GEN
663 26787 : Q_nffactor(GEN nf, GEN y, ulong lim)
664 : {
665 : GEN f, P, E;
666 : long l, i;
667 26787 : if (typ(y) == t_INT)
668 : {
669 26799 : if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
670 26785 : if (is_pm1(y)) return trivial_fact();
671 : }
672 17060 : y = Q_abs_shallow(y);
673 17095 : if (!lim) f = Q_factor(y);
674 : else
675 : {
676 148 : f = Q_factor_limit(y, lim);
677 147 : P = gel(f,1);
678 147 : E = gel(f,2);
679 217 : for (i = lg(P)-1; i > 0; i--)
680 196 : if (abscmpiu(gel(P,i), lim) < 0) break;
681 147 : setlg(P,i+1); setlg(E,i+1);
682 : }
683 17067 : P = gel(f,1); l = lg(P); if (l == 1) return f;
684 17046 : E = gel(f,2);
685 44513 : for (i = 1; i < l; i++)
686 : {
687 27429 : gel(P,i) = idealprimedec(nf, gel(P,i));
688 27413 : gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
689 : }
690 17084 : P = shallowconcat1(P); gel(f,1) = P; settyp(P, t_COL);
691 17096 : E = shallowconcat1(E); gel(f,2) = E; return f;
692 : }
693 :
694 : GEN
695 5285 : idealfactor_partial(GEN nf, GEN x, GEN L)
696 : {
697 5285 : pari_sp av = avma;
698 : long i, j, l;
699 : GEN P, E;
700 5285 : if (!L) return idealfactor(nf, x);
701 4473 : if (typ(L) == t_INT) return idealfactor_limit(nf, x, itou(L));
702 4452 : l = lg(L); if (l == 1) return trivial_fact();
703 3668 : P = cgetg(l, t_VEC);
704 19943 : for (i = 1; i < l; i++)
705 : {
706 16275 : GEN p = gel(L,i);
707 16275 : gel(P,i) = typ(p) == t_INT? idealprimedec(nf, p): mkvec(p);
708 : }
709 3668 : P = shallowconcat1(P); settyp(P, t_COL);
710 3668 : P = gen_sort_uniq(P, (void*)&cmp_prime_ideal, &cmp_nodata);
711 3668 : E = cgetg_copy(P, &l);
712 30100 : for (i = j = 1; i < l; i++)
713 : {
714 26432 : long v = idealval(nf, x, gel(P,i));
715 26432 : if (v) { gel(P,j) = gel(P,i); gel(E,j) = stoi(v); j++; }
716 : }
717 3668 : setlg(P,j);
718 3668 : setlg(E,j); return gerepilecopy(av, mkmat2(P, E));
719 : }
720 : GEN
721 250337 : idealfactor_limit(GEN nf, GEN x, ulong lim)
722 : {
723 250337 : pari_sp av = avma;
724 : GEN fa, y;
725 250337 : long tx = idealtyp(&x, NULL);
726 :
727 250327 : 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 250222 : nf = checknf(nf);
733 250220 : if (tx == id_PRINCIPAL)
734 : {
735 28131 : y = nf_to_scalar_or_basis(nf, x);
736 28138 : if (typ(y) != t_COL) return gerepilecopy(av, Q_nffactor(nf, y, lim));
737 : }
738 223398 : y = idealnumden(nf, x);
739 223402 : fa = idealHNF_factor(nf, gel(y,1), lim);
740 223395 : if (!isint1(gel(y,2)))
741 14 : fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
742 223394 : fa = gerepilecopy(av, fa);
743 223403 : return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
744 : }
745 : GEN
746 249959 : 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 7271 : ramified_root(GEN nf, GEN R, GEN A, long n)
761 : {
762 7271 : GEN v, P = gel(idealfactor(nf, R), 1);
763 7271 : long i, l = lg(P);
764 7271 : v = cgetg(l, t_VECSMALL);
765 7922 : for (i = 1; i < l; i++)
766 : {
767 658 : long w = idealval(nf, A, gel(P,i));
768 658 : if (w % n) return NULL;
769 651 : v[i] = w / n;
770 : }
771 7264 : 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 7278 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
797 : {
798 : GEN C, root;
799 : long i, l;
800 :
801 7278 : if (typ(A) == t_MAT && ZM_isscalar(A, NULL)) A = gcoeff(A,1,1);
802 7278 : if (typ(A) == t_INT) /* > 0 */
803 : {
804 5060 : GEN P = nf_get_ramified_primes(nf), v, q;
805 5060 : l = lg(P); v = cgetg(l, t_VECSMALL);
806 23980 : for (i = 1; i < l; i++) v[i] = Z_pvalrem(A, gel(P,i), &A);
807 5060 : C = gen_1;
808 5060 : if (!isint1(A) && !Z_ispowerall(A, n, pB? &C: NULL)) return 0;
809 5060 : if (!pB) return ramified_root_simple(nf, n, P, v);
810 5053 : q = factorback2(P, v);
811 5053 : root = ramified_root(nf, q, q, n);
812 5053 : if (!root) return 0;
813 5053 : if (!equali1(C)) root = isint1(root)? C: ZM_Z_mul(root, C);
814 5053 : *pB = root; return 1;
815 : }
816 : /* compute valuations at ramified primes */
817 2218 : root = ramified_root(nf, idealadd(nf, nf_get_diff(nf), A), A, n);
818 2218 : if (!root) return 0;
819 : /* remove ramified primes */
820 2211 : if (isint1(root))
821 1868 : root = matid(nf_get_degree(nf));
822 : else
823 343 : A = idealdivexact(nf, A, idealpows(nf,root,n));
824 2211 : A = Q_primitive_part(A, &C);
825 2211 : if (C)
826 : {
827 28 : if (!Z_ispowerall(C,n,&C)) return 0;
828 21 : if (pB) root = ZM_Z_mul(root, C);
829 : }
830 :
831 : /* compute final n-th root, at most degree(nf)-1 iterations */
832 2204 : for (i = 0;; i++)
833 2071 : {
834 4275 : GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
835 4275 : if (is_pm1(a)) break;
836 2099 : if (!Z_ispowerall(a,n,&b)) return 0;
837 2071 : J = idealadd(nf, b, A);
838 2071 : A = idealdivexact(nf, idealpows(nf,J,n), A);
839 : /* div and not divexact here */
840 2071 : if (pB) root = odd(i)? idealdiv(nf, root, J): idealmul(nf, root, J);
841 : }
842 2176 : if (pB) *pB = root;
843 2176 : 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 3667 : idealispower(GEN nf, GEN A, long n, GEN *pB)
850 : {
851 3667 : pari_sp av = avma;
852 : GEN v, N, D;
853 3667 : nf = checknf(nf);
854 3667 : if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
855 3667 : if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
856 3660 : v = idealnumden(nf,A);
857 3660 : if (gequal0(gel(v,1))) { set_avma(av); if (pB) *pB = cgetg(1,t_MAT); return 1; }
858 3660 : if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
859 3618 : if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
860 3618 : if (pB) *pB = gerepileupto(av, idealdiv(nf,N,D)); else set_avma(av);
861 3618 : return 1;
862 : }
863 :
864 : /* x t_INT or integral nonzero ideal in HNF */
865 : static GEN
866 97159 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
867 : {
868 : GEN cx, y, U, N, F, Q;
869 97159 : if (typ(x) == t_INT)
870 : {
871 51141 : if (!signe(x) || is_pm1(x)) return gen_1;
872 1911 : F = Z_factor_limit(x, B);
873 1911 : gel(F,2) = gdiventgs(gel(F,2), k);
874 1911 : return ginv(factorback(F));
875 : }
876 46018 : N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
877 45527 : F = absZ_factor_limit_strict(N, B, &U);
878 45527 : if (U)
879 : {
880 153 : GEN M = powii(gel(U,1), gel(U,2));
881 153 : y = hnfmodid(x, M); /* coprime part to B! */
882 153 : if (!idealispower(nf, y, k, &U)) U = NULL;
883 153 : x = hnfmodid(x, diviiexact(N, M));
884 : }
885 : /* x = B-smooth part of initial x */
886 45527 : x = Q_primitive_part(x, &cx);
887 45527 : F = idealHNF_factor_i(nf, x, cx, F);
888 45525 : gel(F,2) = gdiventgs(gel(F,2), k);
889 45526 : Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
890 45526 : if (U) Q = idealmul(nf,Q,U);
891 45526 : if (typ(Q) == t_INT) return Q;
892 12563 : y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
893 12563 : return gdiv(y, gcoeff(Q,1,1));
894 : }
895 : GEN
896 48587 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
897 : {
898 48587 : pari_sp av = avma;
899 : GEN a, b;
900 48587 : nf = checknf(nf);
901 48587 : if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
902 48587 : x = idealnumden(nf, x);
903 48587 : a = gel(x,1);
904 48587 : if (isintzero(a)) { set_avma(av); return gen_1; }
905 48580 : a = idealredmodpower_i(nf, gel(x,1), n, B);
906 48579 : b = idealredmodpower_i(nf, gel(x,2), n, B);
907 48579 : if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
908 48579 : return gerepilecopy(av, a);
909 : }
910 :
911 : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
912 : long
913 2092350 : idealval(GEN nf, GEN A, GEN P)
914 : {
915 2092350 : pari_sp av = avma;
916 : GEN p, cA;
917 2092350 : long vcA, v, Zval, tx = idealtyp(&A, NULL);
918 :
919 2092347 : if (tx == id_PRINCIPAL) return nfval(nf,A,P);
920 1993331 : checkprid(P);
921 1993331 : if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
922 : /* id_MAT */
923 1993303 : nf = checknf(nf);
924 1993302 : A = Q_primitive_part(A, &cA);
925 1993297 : p = pr_get_p(P);
926 1993298 : vcA = cA? Q_pval(cA,p): 0;
927 1993298 : if (pr_is_inert(P)) return gc_long(av,vcA);
928 1595488 : Zval = Z_pval(gcoeff(A,1,1), p);
929 1595489 : if (!Zval) v = 0;
930 : else
931 : {
932 304894 : long Nval = idealHNF_norm_pval(A, p, Zval);
933 304893 : v = idealHNF_val(A, P, Nval, Zval);
934 : }
935 1595489 : 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 77605 : idealadd(GEN nf, GEN x, GEN y)
948 : {
949 77605 : pari_sp av = avma;
950 : long tx, ty;
951 : GEN z, a, dx, dy, dz;
952 :
953 77605 : tx = idealtyp(&x, NULL);
954 77605 : ty = idealtyp(&y, NULL); nf = checknf(nf);
955 77605 : if (tx != id_MAT) x = idealhnf_shallow(nf,x);
956 77605 : if (ty != id_MAT) y = idealhnf_shallow(nf,y);
957 77605 : if (lg(x) == 1) return gerepilecopy(av,y);
958 77479 : if (lg(y) == 1) return gerepilecopy(av,x); /* check for 0 ideal */
959 77024 : dx = Q_denom(x);
960 77024 : dy = Q_denom(y); dz = lcmii(dx,dy);
961 77024 : if (is_pm1(dz)) dz = NULL; else {
962 15246 : x = Q_muli_to_int(x, dz);
963 15246 : y = Q_muli_to_int(y, dz);
964 : }
965 77024 : a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
966 77024 : if (is_pm1(a))
967 : {
968 34558 : long N = lg(x)-1;
969 34558 : if (!dz) { set_avma(av); return matid(N); }
970 3871 : return gerepileupto(av, scalarmat(ginv(dz), N));
971 : }
972 42466 : z = ZM_hnfmodid(shallowconcat(x,y), a);
973 42466 : if (dz) z = RgM_Rg_div(z,dz);
974 42466 : 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 630925 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
983 : {
984 : GEN a;
985 630925 : long tx = idealtyp(&x, NULL);
986 630907 : long ty = idealtyp(&y, NULL);
987 : long ea;
988 630912 : if (tx != id_MAT) x = idealhnf_shallow(nf, x);
989 630937 : if (ty != id_MAT) y = idealhnf_shallow(nf, y);
990 630937 : if (lg(x) == 1)
991 14 : a = trivial_merge(y);
992 630923 : else if (lg(y) == 1)
993 14 : a = trivial_merge(x);
994 : else
995 630909 : a = hnfmerge_get_1(x, y);
996 630884 : if (!a) pari_err_COPRIME("idealaddtoone",x,y);
997 630868 : if (red && (ea = gexpo(a)) > 10)
998 : {
999 5124 : GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
1000 5124 : b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
1001 5124 : if (gexpo(b) < ea) a = b;
1002 : }
1003 630868 : return a;
1004 : }
1005 : /* true nf */
1006 : GEN
1007 19446 : idealaddtoone_i(GEN nf, GEN x, GEN y)
1008 19446 : { return _idealaddtoone(nf, x, y, 1); }
1009 : /* true nf */
1010 : GEN
1011 611481 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
1012 611481 : { 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 830877 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
1080 : {
1081 830877 : GEN m, a = gel(y,1), alpha = gel(y,2);
1082 : long i, N;
1083 :
1084 830877 : if (typ(alpha) != t_MAT)
1085 : {
1086 494962 : alpha = zk_scalar_or_multable(nf, alpha);
1087 494960 : if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
1088 18413 : return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
1089 : }
1090 812462 : N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
1091 3407672 : for (i=1; i<=N; i++) gel(m,i) = ZM_ZC_mul(alpha,gel(x,i));
1092 3406417 : for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
1093 811559 : 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 394997 : idealHNF_mul(GEN nf, GEN x, GEN y)
1100 : {
1101 : GEN z;
1102 394997 : if (typ(y) == t_VEC)
1103 217408 : z = idealHNF_mul_two(nf,x,y);
1104 : else
1105 : { /* reduce one ideal to two-elt form. The smallest */
1106 177589 : GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
1107 177589 : if (cmpii(xZ, yZ) < 0)
1108 : {
1109 37893 : if (is_pm1(xZ)) return gcopy(y);
1110 21978 : z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
1111 : }
1112 : else
1113 : {
1114 139696 : if (is_pm1(yZ)) return gcopy(x);
1115 35448 : z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
1116 : }
1117 : }
1118 274834 : return z;
1119 : }
1120 :
1121 : /* operations on elements in factored form */
1122 :
1123 : GEN
1124 200014 : famat_mul_shallow(GEN f, GEN g)
1125 : {
1126 200014 : if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
1127 200014 : if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
1128 200014 : if (lgcols(f) == 1) return g;
1129 148811 : if (lgcols(g) == 1) return f;
1130 146960 : return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
1131 146959 : shallowconcat(gel(f,2), gel(g,2)));
1132 : }
1133 : GEN
1134 88438 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
1135 : {
1136 88438 : if (!signe(e)) return f;
1137 55170 : return famat_mul_shallow(f, famat_pow_shallow(g, e));
1138 : }
1139 :
1140 : GEN
1141 117983 : famat_mulpows_shallow(GEN f, GEN g, long e)
1142 : {
1143 117983 : if (e==0) return f;
1144 94571 : return famat_mul_shallow(f, famat_pows_shallow(g, e));
1145 : }
1146 :
1147 : GEN
1148 10311 : famat_div_shallow(GEN f, GEN g)
1149 10311 : { 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 2460451 : 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 179288 : append(GEN v, GEN x)
1159 : {
1160 179288 : long i, l = lg(v);
1161 179288 : GEN w = cgetg(l+1, typ(v));
1162 1032679 : for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
1163 179288 : gel(w,i) = gcopy(x); return w;
1164 : }
1165 : /* add x^1 to famat f */
1166 : static GEN
1167 168143 : famat_add(GEN f, GEN x)
1168 : {
1169 168143 : GEN h = cgetg(3,t_MAT);
1170 168143 : if (lgcols(f) == 1)
1171 : {
1172 10151 : gel(h,1) = mkcolcopy(x);
1173 10151 : gel(h,2) = mkcol(gen_1);
1174 : }
1175 : else
1176 : {
1177 157992 : gel(h,1) = append(gel(f,1), x);
1178 157992 : gel(h,2) = gconcat(gel(f,2), gen_1);
1179 : }
1180 168143 : return h;
1181 : }
1182 : /* add x^-1 to famat f */
1183 : static GEN
1184 37996 : famat_sub(GEN f, GEN x)
1185 : {
1186 37996 : GEN h = cgetg(3,t_MAT);
1187 37996 : if (lgcols(f) == 1)
1188 : {
1189 16700 : gel(h,1) = mkcolcopy(x);
1190 16700 : gel(h,2) = mkcol(gen_m1);
1191 : }
1192 : else
1193 : {
1194 21296 : gel(h,1) = append(gel(f,1), x);
1195 21296 : gel(h,2) = gconcat(gel(f,2), gen_m1);
1196 : }
1197 37996 : return h;
1198 : }
1199 :
1200 : GEN
1201 262793 : famat_mul(GEN f, GEN g)
1202 : {
1203 : GEN h;
1204 262793 : if (typ(g) != t_MAT) {
1205 40043 : 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 222750 : if (typ(f) != t_MAT) return famat_add(g, f);
1211 94650 : if (lgcols(f) == 1) return gcopy(g);
1212 72445 : if (lgcols(g) == 1) return gcopy(f);
1213 68651 : h = cgetg(3,t_MAT);
1214 68651 : gel(h,1) = gconcat(gel(f,1), gel(g,1));
1215 68651 : gel(h,2) = gconcat(gel(f,2), gel(g,2));
1216 68651 : return h;
1217 : }
1218 :
1219 : GEN
1220 38003 : famat_div(GEN f, GEN g)
1221 : {
1222 : GEN h;
1223 38003 : if (typ(g) != t_MAT) {
1224 37954 : 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 21609 : famat_sqr(GEN f)
1240 : {
1241 : GEN h;
1242 21609 : if (typ(f) != t_MAT) return to_famat(f,gen_2);
1243 21609 : if (lgcols(f) == 1) return gcopy(f);
1244 12938 : h = cgetg(3,t_MAT);
1245 12938 : gel(h,1) = gcopy(gel(f,1));
1246 12938 : gel(h,2) = gmul2n(gel(f,2),1);
1247 12938 : return h;
1248 : }
1249 :
1250 : GEN
1251 27087 : famat_inv_shallow(GEN f)
1252 : {
1253 27087 : if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
1254 10444 : if (lgcols(f) == 1) return f;
1255 10444 : return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
1256 : }
1257 : GEN
1258 20440 : famat_inv(GEN f)
1259 : {
1260 20440 : if (typ(f) != t_MAT) return to_famat(f,gen_m1);
1261 20440 : if (lgcols(f) == 1) return gcopy(f);
1262 1803 : retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
1263 : }
1264 : GEN
1265 60642 : famat_pow(GEN f, GEN n)
1266 : {
1267 60642 : if (typ(f) != t_MAT) return to_famat(f,n);
1268 60642 : if (lgcols(f) == 1) return gcopy(f);
1269 60642 : retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
1270 : }
1271 : GEN
1272 62603 : famat_pow_shallow(GEN f, GEN n)
1273 : {
1274 62603 : if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
1275 36162 : if (typ(f) != t_MAT) return to_famat_shallow(f,n);
1276 7925 : if (lgcols(f) == 1) return f;
1277 6507 : return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
1278 : }
1279 :
1280 : GEN
1281 123047 : famat_pows_shallow(GEN f, long n)
1282 : {
1283 123047 : if (n==1) return f;
1284 34739 : if (n==-1) return famat_inv_shallow(f);
1285 34739 : if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
1286 26479 : if (lgcols(f) == 1) return f;
1287 26479 : 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 32557 : ext_sqr(GEN nf, GEN x)
1317 32557 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
1318 : static GEN
1319 87622 : ext_mul(GEN nf, GEN x, GEN y)
1320 87622 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
1321 : static GEN
1322 20440 : ext_inv(GEN nf, GEN x)
1323 20440 : { 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 294949 : famat_reduce(GEN fa)
1354 : {
1355 : GEN E, G, L, g, e;
1356 : long i, k, l;
1357 :
1358 294949 : if (typ(fa) != t_MAT || lgcols(fa) == 1) return fa;
1359 284754 : g = gel(fa,1); l = lg(g);
1360 284754 : e = gel(fa,2);
1361 284754 : L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
1362 284754 : G = cgetg(l, t_COL);
1363 284754 : E = cgetg(l, t_COL);
1364 : /* merge */
1365 2274926 : for (k=i=1; i<l; i++,k++)
1366 : {
1367 1990172 : gel(G,k) = gel(g,L[i]);
1368 1990172 : gel(E,k) = gel(e,L[i]);
1369 1990172 : if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
1370 : {
1371 813571 : gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
1372 813571 : k--;
1373 : }
1374 : }
1375 : /* kill 0 exponents */
1376 284754 : l = k;
1377 1461355 : for (k=i=1; i<l; i++)
1378 1176601 : if (!gequal0(gel(E,i)))
1379 : {
1380 1159763 : gel(G,k) = gel(G,i);
1381 1159763 : gel(E,k) = gel(E,i); k++;
1382 : }
1383 284754 : setlg(G, k);
1384 284754 : 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 173805 : famatsmall_reduce(GEN fa)
1413 : {
1414 : GEN E, G, L, g, e;
1415 : long i, k, l;
1416 173805 : if (lgcols(fa) == 1) return fa;
1417 173805 : g = gel(fa,1); l = lg(g);
1418 173805 : e = gel(fa,2);
1419 173805 : L = vecsmall_indexsort(g);
1420 173805 : G = cgetg(l, t_VECSMALL);
1421 173805 : E = cgetg(l, t_VECSMALL);
1422 : /* merge */
1423 480381 : for (k=i=1; i<l; i++,k++)
1424 : {
1425 306576 : G[k] = g[L[i]];
1426 306576 : E[k] = e[L[i]];
1427 306576 : if (k > 1 && G[k] == G[k-1])
1428 : {
1429 8110 : E[k-1] += E[k];
1430 8110 : k--;
1431 : }
1432 : }
1433 : /* kill 0 exponents */
1434 173805 : l = k;
1435 472271 : for (k=i=1; i<l; i++)
1436 298466 : if (E[i])
1437 : {
1438 294703 : G[k] = G[i];
1439 294703 : E[k] = E[i]; k++;
1440 : }
1441 173805 : setlg(G, k);
1442 173805 : setlg(E, k); return mkmat2(G,E);
1443 : }
1444 :
1445 : GEN
1446 53316 : famat_remove_trivial(GEN fa)
1447 : {
1448 53316 : GEN P, E, p = gel(fa,1), e = gel(fa,2);
1449 53316 : long j, k, l = lg(p);
1450 53316 : P = cgetg(l, t_COL);
1451 53316 : E = cgetg(l, t_COL);
1452 1722692 : for (j = k = 1; j < l; j++)
1453 1669376 : if (signe(gel(e,j))) { gel(P,k) = gel(p,j); gel(E,k++) = gel(e,j); }
1454 53316 : setlg(P, k); setlg(E, k); return mkmat2(P,E);
1455 : }
1456 :
1457 : GEN
1458 13556 : famatV_factorback(GEN v, GEN e)
1459 : {
1460 13556 : long i, l = lg(e);
1461 : GEN V;
1462 13556 : if (l == 1) return trivial_fact();
1463 13171 : V = signe(gel(e,1))? famat_pow_shallow(gel(v,1), gel(e,1)): trivial_fact();
1464 55910 : for (i = 2; i < l; i++) V = famat_mulpow_shallow(V, gel(v,i), gel(e,i));
1465 13171 : return V;
1466 : }
1467 :
1468 : GEN
1469 46529 : famatV_zv_factorback(GEN v, GEN e)
1470 : {
1471 46529 : long i, l = lg(e);
1472 : GEN V;
1473 46529 : if (l == 1) return trivial_fact();
1474 44142 : V = uel(e,1)? famat_pows_shallow(gel(v,1), uel(e,1)): trivial_fact();
1475 138676 : for (i = 2; i < l; i++) V = famat_mulpows_shallow(V, gel(v,i), uel(e,i));
1476 44142 : return V;
1477 : }
1478 :
1479 : GEN
1480 567892 : 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 567892 : if (lgcols(fa) == 1) return fa;
1487 567885 : g = gel(fa,1); l = lg(g);
1488 567885 : e = gel(fa,2);
1489 1136919 : for(n=0, i=1; i<l; i++)
1490 569034 : if (cmpii(gel(g,i),limit)<=0) n++;
1491 567885 : lG = n<l-1 ? n+2 : n+1;
1492 567885 : G = cgetg(lG, t_COL);
1493 567886 : E = cgetg(lG, t_COL);
1494 567886 : av = avma;
1495 1136920 : for (i=1, k=1, r = gen_1; i<l; i++)
1496 : {
1497 569034 : if (cmpii(gel(g,i),limit)<=0)
1498 : {
1499 568901 : gel(G,k) = gel(g,i);
1500 568901 : gel(E,k) = gel(e,i);
1501 568901 : k++;
1502 133 : } else r = mulii(r, powii(gel(g,i), gel(e,i)));
1503 : }
1504 567886 : if (k<i)
1505 : {
1506 133 : gel(G, k) = gerepileuptoint(av, r);
1507 133 : gel(E, k) = gen_1;
1508 : }
1509 567886 : return mkmat2(G,E);
1510 : }
1511 :
1512 : /* assume pr has degree 1 and coprime to Q_denom(x) */
1513 : static GEN
1514 122514 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
1515 : {
1516 122514 : GEN d, r, p = modpr_get_p(modpr);
1517 122514 : x = nf_to_scalar_or_basis(nf,x);
1518 122514 : if (typ(x) != t_COL) return Rg_to_Fp(x,p);
1519 121030 : x = Q_remove_denom(x, &d);
1520 121030 : r = zk_to_Fq(x, modpr);
1521 121030 : if (d) r = Fp_div(r, d, p);
1522 121030 : return r;
1523 : }
1524 :
1525 : /* pr coprime to all denominators occurring in x */
1526 : static GEN
1527 763 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
1528 : {
1529 763 : GEN p = modpr_get_p(modpr);
1530 763 : GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
1531 763 : long i, l = lg(g);
1532 4711 : for (i = 1; i < l; i++)
1533 : {
1534 3948 : GEN n = modii(gel(e,i), q);
1535 3948 : if (signe(n))
1536 : {
1537 3941 : GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
1538 3941 : h = Fp_pow(h, n, p);
1539 3941 : t = t? Fp_mul(t, h, p): h;
1540 : }
1541 : }
1542 763 : 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 119336 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
1548 : {
1549 763 : return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
1550 120099 : : to_Fp_coprime(nf, x, modpr);
1551 : }
1552 :
1553 : static long
1554 3921628 : zk_pvalrem(GEN x, GEN p, GEN *py)
1555 3921628 : { 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 4020219 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
1560 : {
1561 : long vcx;
1562 : GEN dx;
1563 4020219 : x = nf_to_scalar_or_basis(nf, x);
1564 4020224 : x = Q_remove_denom(x, &dx);
1565 4020218 : if (dx)
1566 : {
1567 347846 : vcx = - Z_pvalrem(dx, p, &dx);
1568 347848 : if (!vcx) vcx = zk_pvalrem(x, p, &x);
1569 347848 : if (isint1(dx)) dx = NULL;
1570 : }
1571 : else
1572 : {
1573 3672372 : vcx = zk_pvalrem(x, p, &x);
1574 3672384 : dx = NULL;
1575 : }
1576 4020232 : *pv = vcx;
1577 4020232 : *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 91498 : p_makecoprime(GEN pr)
1583 : {
1584 91498 : GEN B = pr_get_tau(pr), b;
1585 : long i, e;
1586 :
1587 91498 : if (typ(B) == t_INT) return NULL;
1588 68741 : b = gel(B,1); /* B = multiplication table by b */
1589 68741 : e = pr_get_e(pr);
1590 68741 : if (e == 1) return b;
1591 : /* one could also divide (exactly) by p in each iteration */
1592 45519 : for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
1593 22215 : 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 1999728 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
1609 : {
1610 1999728 : GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
1611 1999726 : long i, l = lg(g);
1612 :
1613 1999726 : G = cgetg(l+1, t_VEC);
1614 1999735 : E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
1615 6019942 : for (i=1; i < l; i++)
1616 : {
1617 : long vcx;
1618 4020217 : GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
1619 4020229 : if (vcx) /* = v_p(content(g[i])) */
1620 : {
1621 148509 : GEN a = mulsi(vcx, gel(e,i));
1622 148506 : vp = vp? addii(vp, a): a;
1623 : }
1624 : /* x integral, content coprime to p; dx coprime to p */
1625 4020225 : if (typ(x) == t_INT)
1626 : { /* x coprime to p, hence to pr */
1627 1013696 : x = modii(x, prkZ);
1628 1013696 : if (dx) x = Fp_div(x, dx, prkZ);
1629 : }
1630 : else
1631 : {
1632 3006529 : (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
1633 3006494 : x = ZC_hnfrem(FpC_red(x,prkZ), prk);
1634 3006502 : if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
1635 : }
1636 4020198 : gel(G,i) = x;
1637 4020198 : gel(E,i) = gel(e,i);
1638 : }
1639 :
1640 1999725 : t = vp? p_makecoprime(pr): NULL;
1641 1999728 : if (!t)
1642 : { /* no need for extra generator */
1643 1931064 : setlg(G,l);
1644 1931064 : setlg(E,l);
1645 : }
1646 : else
1647 : {
1648 68664 : gel(G,i) = FpC_red(t, prkZ);
1649 68664 : gel(E,i) = vp;
1650 : }
1651 1999732 : 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 9086 : for (i = 1; i < l; i++)
1663 : {
1664 8988 : GEN x = gel(X,i);
1665 8988 : if (typ(x) == t_INT) /* a prime */
1666 1414 : x = equalii(x,p)? p_makecoprime(pr): modii(x, prkZ);
1667 : else
1668 : {
1669 7574 : (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
1670 7574 : x = ZC_hnfrem(FpC_red(x,prkZ), prk);
1671 : }
1672 8988 : 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 20013 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
1680 : {
1681 20013 : GEN t, cyc = bid_get_cyc(bid);
1682 20013 : if (lg(cyc) == 1)
1683 0 : t = gen_1;
1684 : else
1685 20013 : t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid),
1686 : cyc_get_expo(cyc));
1687 20013 : return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
1688 : }
1689 :
1690 : GEN
1691 867715 : vecmul(GEN x, GEN y)
1692 : {
1693 867715 : if (is_scalar_t(typ(x))) return gmul(x,y);
1694 685996 : 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 301896 : idealmulelt(GEN nf, GEN x, GEN A)
1728 : {
1729 : long i, lx;
1730 : GEN dx, dA, D;
1731 301896 : if (lg(A) == 1) return cgetg(1, t_MAT);
1732 301896 : x = nf_to_scalar_or_basis(nf,x);
1733 301896 : if (typ(x) != t_COL)
1734 97580 : return isintzero(x)? cgetg(1,t_MAT): RgM_Rg_mul(A, Q_abs_shallow(x));
1735 204316 : x = Q_remove_denom(x, &dx);
1736 204316 : A = Q_remove_denom(A, &dA);
1737 204316 : x = zk_multable(nf, x);
1738 204316 : D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
1739 204316 : x = zkC_multable_mul(A, x);
1740 204316 : settyp(x, t_MAT); lx = lg(x);
1741 : /* x may contain scalars (at most 1 since the ideal is nonzero)*/
1742 778267 : for (i=1; i<lx; i++)
1743 588098 : if (typ(gel(x,i)) == t_INT)
1744 : {
1745 14147 : if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
1746 14147 : gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
1747 14147 : break;
1748 : }
1749 204316 : x = ZM_hnfmodid(x, D);
1750 204316 : dx = mul_denom(dx,dA);
1751 204316 : return dx? gdiv(x,dx): x;
1752 : }
1753 :
1754 : /* nf a true nf, tx <= ty */
1755 : static GEN
1756 569043 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
1757 : {
1758 : GEN z, cx, cy;
1759 569043 : switch(tx)
1760 : {
1761 357961 : case id_PRINCIPAL:
1762 357961 : switch(ty)
1763 : {
1764 55673 : case id_PRINCIPAL:
1765 55673 : 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 301896 : default: /* id_MAT */
1788 301896 : return idealmulelt(nf, x,y);
1789 : }
1790 42446 : case id_PRIME:
1791 42446 : if (ty==id_PRIME)
1792 4340 : { y = pr_hnf(nf,y); cy = NULL; }
1793 : else
1794 38106 : y = Q_primitive_part(y, &cy);
1795 42446 : y = idealHNF_mul_two(nf,y,x);
1796 42446 : return cy? ZM_Q_mul(y,cy): y;
1797 :
1798 168636 : default: /* id_MAT */
1799 : {
1800 168636 : long N = nf_get_degree(nf);
1801 168636 : if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
1802 168622 : x = Q_primitive_part(x, &cx);
1803 168622 : y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
1804 168622 : y = idealHNF_mul(nf,x,y);
1805 168622 : return cx? ZM_Q_mul(y,cx): y;
1806 : }
1807 : }
1808 : }
1809 :
1810 : /* output the ideal product x.y */
1811 : GEN
1812 569043 : idealmul(GEN nf, GEN x, GEN y)
1813 : {
1814 : pari_sp av;
1815 : GEN res, ax, ay, z;
1816 569043 : long tx = idealtyp(&x,&ax);
1817 569043 : long ty = idealtyp(&y,&ay), f;
1818 569043 : if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
1819 569043 : f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
1820 569043 : av = avma;
1821 569043 : z = gerepileupto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
1822 569029 : if (!f) return z;
1823 26841 : if (ax && ay)
1824 25364 : ax = ext_mul(nf, ax, ay);
1825 : else
1826 1477 : ax = gcopy(ax? ax: ay);
1827 26841 : 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 228110 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
1834 : {
1835 228110 : GEN p = pr_get_p(pr), q, gen;
1836 228109 : long e = pr_get_e(pr), f = pr_get_f(pr);
1837 :
1838 228111 : q = (e == 1)? sqri(p): p;
1839 228100 : if (e <= 2 && e * f == nf_get_degree(nf))
1840 : { /* pr^e = (p) */
1841 45735 : *pc = q;
1842 45735 : return mkvec2(gen_1,gen_0);
1843 : }
1844 182369 : gen = nfsqr(nf, pr_get_gen(pr));
1845 182375 : gen = FpC_red(gen, q);
1846 182362 : *pc = NULL;
1847 182362 : return mkvec2(q, gen);
1848 : }
1849 : /* cf idealpow_aux */
1850 : static GEN
1851 37226 : idealsqr_aux(GEN nf, GEN x, long tx)
1852 : {
1853 37226 : GEN T = nf_get_pol(nf), m, cx, a, alpha;
1854 37226 : long N = degpol(T);
1855 37226 : switch(tx)
1856 : {
1857 84 : case id_PRINCIPAL:
1858 84 : return idealhnf_principal(nf, nfsqr(nf,x));
1859 10423 : case id_PRIME:
1860 10423 : if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
1861 10255 : x = idealsqrprime(nf, x, &cx);
1862 10255 : x = idealhnf_two(nf,x);
1863 10255 : return cx? ZM_Z_mul(x, cx): x;
1864 26719 : default:
1865 26719 : x = Q_primitive_part(x, &cx);
1866 26719 : a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
1867 26719 : alpha = nfsqr(nf,alpha);
1868 26719 : m = zk_scalar_or_multable(nf, alpha);
1869 26719 : if (typ(m) == t_INT) {
1870 1635 : x = gcdii(sqri(a), m);
1871 1635 : if (cx) x = gmul(x, gsqr(cx));
1872 1635 : x = scalarmat(x, N);
1873 : }
1874 : else
1875 : { /* could use gcdii(sqri(a), zkmultable_capZ(m)), but costly */
1876 25084 : x = ZM_hnfmodid(m, sqri(a));
1877 25084 : if (cx) cx = gsqr(cx);
1878 25084 : if (cx) x = ZM_Q_mul(x, cx);
1879 : }
1880 26719 : return x;
1881 : }
1882 : }
1883 : GEN
1884 37226 : idealsqr(GEN nf, GEN x)
1885 : {
1886 : pari_sp av;
1887 : GEN res, ax, z;
1888 37226 : long tx = idealtyp(&x,&ax);
1889 37226 : res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
1890 37226 : av = avma;
1891 37226 : z = gerepileupto(av, idealsqr_aux(checknf(nf), x, tx));
1892 37226 : if (!ax) return z;
1893 32557 : gel(res,1) = z;
1894 32557 : gel(res,2) = ext_sqr(nf, ax); return res;
1895 : }
1896 :
1897 : /* norm of an ideal */
1898 : GEN
1899 13512 : idealnorm(GEN nf, GEN x)
1900 : {
1901 : pari_sp av;
1902 : long tx;
1903 :
1904 13512 : switch(idealtyp(&x, NULL))
1905 : {
1906 952 : case id_PRIME: return pr_norm(x);
1907 9571 : case id_MAT: return RgM_det_triangular(x);
1908 : }
1909 : /* id_PRINCIPAL */
1910 2989 : nf = checknf(nf); av = avma;
1911 2989 : x = nfnorm(nf, x);
1912 2989 : tx = typ(x);
1913 2989 : 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 215608 : idealHNF_inv_Z(GEN nf, GEN I)
2024 : {
2025 215608 : GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
2026 215608 : if (isint1(IZ)) return matid(lg(I)-1);
2027 195882 : 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 195881 : dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
2031 195882 : return ZM_hnfmodid(dual, IZ);
2032 : }
2033 : /* I HNF with rational coefficients (denominator d). */
2034 : GEN
2035 77627 : idealHNF_inv(GEN nf, GEN I)
2036 : {
2037 77627 : GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
2038 77627 : J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
2039 77627 : return equali1(IQ)? J: RgM_Rg_div(J, IQ);
2040 : }
2041 :
2042 : /* return p * P^(-1) [integral] */
2043 : GEN
2044 38850 : pr_inv_p(GEN pr)
2045 : {
2046 38850 : if (pr_is_inert(pr)) return matid(pr_get_f(pr));
2047 38066 : return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
2048 : }
2049 : GEN
2050 18259 : pr_inv(GEN pr)
2051 : {
2052 18259 : GEN p = pr_get_p(pr);
2053 18259 : if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
2054 17867 : return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
2055 : }
2056 :
2057 : GEN
2058 147137 : idealinv(GEN nf, GEN x)
2059 : {
2060 : GEN res, ax;
2061 : pari_sp av;
2062 147137 : long tx = idealtyp(&x,&ax), N;
2063 :
2064 147137 : res = ax? cgetg(3,t_VEC): NULL;
2065 147137 : nf = checknf(nf); av = avma;
2066 147137 : N = nf_get_degree(nf);
2067 147137 : switch (tx)
2068 : {
2069 70888 : case id_MAT:
2070 70888 : if (lg(x)-1 != N) pari_err_DIM("idealinv");
2071 70888 : x = idealHNF_inv(nf,x); break;
2072 59228 : case id_PRINCIPAL:
2073 59228 : x = nf_to_scalar_or_basis(nf, x);
2074 59228 : if (typ(x) != t_COL)
2075 59179 : 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 59228 : break;
2092 17021 : case id_PRIME:
2093 17021 : x = pr_inv(x); break;
2094 : }
2095 147137 : x = gerepileupto(av,x); if (!ax) return x;
2096 20440 : gel(res,1) = x;
2097 20440 : gel(res,2) = ext_inv(nf, ax); return res;
2098 : }
2099 :
2100 : /* write x = A/B, A,B coprime integral ideals */
2101 : GEN
2102 275861 : idealnumden(GEN nf, GEN x)
2103 : {
2104 275861 : pari_sp av = avma;
2105 : GEN x0, c, d, A, B, J;
2106 275861 : long tx = idealtyp(&x, NULL);
2107 275857 : nf = checknf(nf);
2108 275860 : switch (tx)
2109 : {
2110 7 : case id_PRIME:
2111 7 : retmkvec2(idealhnf(nf, x), gen_1);
2112 51198 : case id_PRINCIPAL:
2113 : {
2114 : GEN xZ, mx;
2115 51198 : x = nf_to_scalar_or_basis(nf, x);
2116 51198 : switch(typ(x))
2117 : {
2118 2709 : 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 48475 : x = Q_remove_denom(x, &d);
2123 48474 : if (!d) return gerepilecopy(av, mkvec2(idealhnf_shallow(nf, x), gen_1));
2124 77 : mx = zk_multable(nf, x);
2125 77 : xZ = zkmultable_capZ(mx);
2126 77 : x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
2127 77 : x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
2128 77 : break;
2129 : }
2130 224655 : default: /* id_MAT */
2131 : {
2132 224655 : long n = lg(x)-1;
2133 224655 : if (n == 0) return mkvec2(gen_0, gen_1);
2134 224655 : if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
2135 224656 : x0 = x = Q_remove_denom(x, &d);
2136 224650 : if (!d) return gerepilecopy(av, mkvec2(x, gen_1));
2137 21 : break;
2138 : }
2139 : }
2140 98 : J = hnfmodid(x, d); /* = d/B */
2141 98 : c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
2142 98 : B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
2143 98 : if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
2144 98 : A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
2145 98 : A = ZM_Z_divexact(A, d); /* = A ! */
2146 98 : 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 971163 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
2153 : {
2154 971163 : GEN p = pr_get_p(pr), q, gen;
2155 :
2156 971143 : *pc = NULL;
2157 971143 : if (is_pm1(n)) /* n = 1 special cased for efficiency */
2158 : {
2159 543872 : q = p;
2160 543872 : 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 543870 : if (signe(n) >= 0) gen = pr_get_gen(pr);
2166 : else
2167 : {
2168 155477 : gen = pr_get_tau(pr); /* possibly t_MAT */
2169 155482 : *pc = ginv(p);
2170 : }
2171 : }
2172 427333 : else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
2173 : else
2174 : {
2175 209477 : long e = pr_get_e(pr), f = pr_get_f(pr);
2176 209483 : GEN r, m = truedvmdis(n, e, &r);
2177 209474 : if (e * f == nf_get_degree(nf))
2178 : { /* pr^e = (p) */
2179 86798 : if (signe(m)) *pc = powii(p,m);
2180 86800 : if (!signe(r)) return mkvec2(gen_1,gen_0);
2181 42779 : q = p;
2182 42779 : gen = nfpow(nf, pr_get_gen(pr), r);
2183 : }
2184 : else
2185 : {
2186 122685 : m = absi_shallow(m);
2187 122685 : if (signe(r)) m = addiu(m,1);
2188 122686 : q = powii(p,m); /* m = ceil(|n|/e) */
2189 122687 : if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
2190 : else
2191 : {
2192 24257 : gen = pr_get_tau(pr);
2193 24257 : if (typ(gen) == t_MAT) gen = gel(gen,1);
2194 24257 : n = negi(n);
2195 24258 : gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
2196 24255 : *pc = ginv(q);
2197 : }
2198 : }
2199 165469 : gen = FpC_red(gen, q);
2200 : }
2201 709324 : return mkvec2(q, gen);
2202 : }
2203 :
2204 : /* True nf. x * pr^n. Assume x in HNF or scalar (possibly nonintegral) */
2205 : GEN
2206 754154 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
2207 : {
2208 : GEN c, cx, y;
2209 754154 : long N = nf_get_degree(nf);
2210 :
2211 754164 : if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
2212 :
2213 : /* inert, special cased for efficiency */
2214 754157 : if (pr_is_inert(pr))
2215 : {
2216 74935 : GEN q = powii(pr_get_p(pr), n);
2217 72878 : return typ(x) == t_MAT? RgM_Rg_mul(x,q)
2218 147800 : : scalarmat_shallow(gmul(Q_abs(x),q), N);
2219 : }
2220 :
2221 679213 : y = idealpowprime(nf, pr, n, &c);
2222 679213 : if (typ(x) == t_MAT)
2223 676205 : { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
2224 : else
2225 3008 : { cx = x; x = NULL; }
2226 679105 : cx = mul_content(c,cx);
2227 679113 : if (x)
2228 513556 : x = idealHNF_mul_two(nf,x,y);
2229 : else
2230 165557 : x = idealhnf_two(nf,y);
2231 679313 : if (cx) x = ZM_Q_mul(x,cx);
2232 679037 : return x;
2233 : }
2234 : GEN
2235 9934 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
2236 : {
2237 9934 : return idealmulpowprime(nf,x,pr, negi(n));
2238 : }
2239 :
2240 : /* nf = true nf */
2241 : static GEN
2242 667330 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
2243 : {
2244 667330 : GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
2245 667330 : long N = degpol(T), s = signe(n);
2246 667335 : if (!s) return matid(N);
2247 652077 : switch(tx)
2248 : {
2249 75234 : case id_PRINCIPAL:
2250 75234 : return idealhnf_principal(nf, nfpow(nf,x,n));
2251 383424 : case id_PRIME:
2252 383424 : if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
2253 291946 : x = idealpowprime(nf, x, n, &cx);
2254 291942 : x = idealhnf_two(nf,x);
2255 291947 : return cx? ZM_Q_mul(x, cx): x;
2256 193419 : default:
2257 193419 : if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
2258 69145 : n1 = (s < 0)? negi(n): n;
2259 :
2260 69145 : x = Q_primitive_part(x, &cx);
2261 69145 : a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
2262 69145 : alpha = nfpow(nf,alpha,n1);
2263 69145 : m = zk_scalar_or_multable(nf, alpha);
2264 69145 : 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 68592 : x = ZM_hnfmodid(m, powii(a,n1));
2273 68592 : if (cx) cx = powgi(cx,n);
2274 68592 : 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 68592 : if (cx) x = ZM_Q_mul(x, cx);
2280 : }
2281 69145 : return x;
2282 : }
2283 : }
2284 :
2285 : /* raise the ideal x to the power n (in Z) */
2286 : GEN
2287 667328 : idealpow(GEN nf, GEN x, GEN n)
2288 : {
2289 : pari_sp av;
2290 : long tx;
2291 : GEN res, ax;
2292 :
2293 667328 : if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
2294 667328 : tx = idealtyp(&x,&ax);
2295 667332 : res = ax? cgetg(3,t_VEC): NULL;
2296 667332 : av = avma;
2297 667332 : x = gerepileupto(av, idealpow_aux(checknf(nf), x, tx, n));
2298 667324 : 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 248002 : idealpows(GEN nf, GEN ideal, long e)
2307 : {
2308 248002 : long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
2309 248002 : affsi(e,court); return idealpow(nf,ideal,court);
2310 : }
2311 :
2312 : static GEN
2313 27415 : _idealmulred(GEN nf, GEN x, GEN y)
2314 27415 : { return idealred(nf,idealmul(nf,x,y)); }
2315 : static GEN
2316 34342 : _idealsqrred(GEN nf, GEN x)
2317 34342 : { return idealred(nf,idealsqr(nf,x)); }
2318 : static GEN
2319 10698 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
2320 : static GEN
2321 34342 : _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 79447 : idealpowred(GEN nf, GEN x, GEN n)
2326 : {
2327 79447 : pari_sp av = avma, av2;
2328 : long s;
2329 : GEN y;
2330 :
2331 79447 : if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
2332 79448 : s = signe(n); if (s == 0) return idealpow(nf,x,n);
2333 79448 : y = gen_pow_i(x, n, (void*)nf, &_sqr, &_mul);
2334 79448 : av2 = avma;
2335 79448 : if (s < 0) y = idealinv(nf,y);
2336 79448 : if (s < 0 || is_pm1(n)) y = idealred(nf,y);
2337 79447 : return avma == av2? gerepilecopy(av,y): gerepileupto(av,y);
2338 : }
2339 :
2340 : GEN
2341 16717 : idealmulred(GEN nf, GEN x, GEN y)
2342 : {
2343 16717 : pari_sp av = avma;
2344 16717 : 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 39470 : idealdiv(GEN nf, GEN x, GEN y)
2382 : {
2383 39470 : pari_sp av = avma, tetpil;
2384 39470 : GEN z = idealinv(nf,y);
2385 39470 : 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 4411 : idealdivexact(GEN nf, GEN x0, GEN y0)
2413 : {
2414 4411 : pari_sp av = avma;
2415 : GEN x, y, xZ, yZ, Nx, Ny, Nz, cy, q, r;
2416 :
2417 4411 : nf = checknf(nf);
2418 4411 : x = idealhnf_shallow(nf, x0);
2419 4411 : y = idealhnf_shallow(nf, y0);
2420 4411 : if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
2421 4404 : if (lg(x) == 1) { set_avma(av); return cgetg(1, t_MAT); } /* numerator is zero */
2422 4404 : y = Q_primitive_part(y, &cy);
2423 4404 : if (cy) x = RgM_Rg_div(x,cy);
2424 4404 : xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
2425 4397 : yZ = gcoeff(y,1,1); if (isint1(yZ)) return gerepilecopy(av, x);
2426 2668 : Nx = idealnorm(nf,x);
2427 2668 : Ny = idealnorm(nf,y);
2428 2668 : if (typ(Nx) != t_INT) err_divexact(x,y);
2429 2668 : q = dvmdii(Nx,Ny, &r);
2430 2668 : if (signe(r)) err_divexact(x,y);
2431 2668 : 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 478 : for (Nz = Ny;;) /* q = Nx/Nz */
2434 458 : {
2435 936 : GEN p1 = gcdii(Nz, q);
2436 936 : if (is_pm1(p1)) break;
2437 458 : Nz = diviiexact(Nz,p1);
2438 458 : q = mulii(q,p1);
2439 : }
2440 478 : xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
2441 478 : if (!equalii(xZ,q))
2442 : { /* Replace x/y by x+(Nx/Nz) / y+(Ny/Nz) */
2443 328 : x = ZM_hnfmodid(x, q);
2444 : /* y reduced to unit ideal ? */
2445 328 : if (Nz == Ny) return gerepileupto(av, x);
2446 :
2447 111 : yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
2448 111 : y = ZM_hnfmodid(y, q);
2449 : }
2450 261 : yZ = gcoeff(y,1,1);
2451 261 : y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
2452 261 : 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 12613 : twistG(GEN G, long r1, long i, long v)
2506 : {
2507 12613 : long j, lG = lg(G);
2508 12613 : if (i <= r1) {
2509 37209 : for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
2510 : } else {
2511 565 : long k = (i<<1) - r1;
2512 4222 : for (j=1; j<lG; j++)
2513 : {
2514 3657 : gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
2515 3657 : gcoeff(G,k ,j) = gmul2n(gcoeff(G,k ,j), v);
2516 : }
2517 : }
2518 12613 : }
2519 :
2520 : GEN
2521 135820 : nf_get_Gtwist(GEN nf, GEN vdir)
2522 : {
2523 : long i, l, v, r1;
2524 : GEN G;
2525 :
2526 135820 : 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 12571 : nf_get_Gtwist1(GEN nf, long i)
2546 : {
2547 12571 : GEN G = RgM_shallowcopy( nf_get_G(nf) );
2548 12571 : long r1 = nf_get_r1(nf);
2549 12571 : twistG(G, r1, i, 10);
2550 12571 : return RM_round_maxrank(G);
2551 : }
2552 :
2553 : GEN
2554 96084 : RM_round_maxrank(GEN G0)
2555 : {
2556 96084 : long e, r = lg(G0)-1;
2557 96084 : pari_sp av = avma;
2558 96084 : for (e = 4; ; e <<= 1, set_avma(av))
2559 0 : {
2560 96084 : GEN G = gmul2n(G0, e), H = ground(G);
2561 96084 : if (ZM_rank(H) == r) return H; /* maximal rank ? */
2562 : }
2563 : }
2564 :
2565 : GEN
2566 135813 : idealred0(GEN nf, GEN I, GEN vdir)
2567 : {
2568 135813 : pari_sp av = avma;
2569 135813 : GEN G, aI, IZ, J, y, my, yi, c1 = NULL;
2570 : long N;
2571 :
2572 135813 : nf = checknf(nf);
2573 135813 : N = nf_get_degree(nf);
2574 : /* put first for sanity checks, unused when I obviously principal */
2575 135813 : G = nf_get_Gtwist(nf, vdir);
2576 135806 : switch (idealtyp(&I,&aI))
2577 : {
2578 36769 : case id_PRIME:
2579 36769 : if (pr_is_inert(I)) {
2580 655 : if (!aI) { set_avma(av); return matid(N); }
2581 655 : c1 = gel(I,1); I = matid(N);
2582 655 : goto END;
2583 : }
2584 36114 : IZ = pr_get_p(I);
2585 36114 : J = pr_inv_p(I);
2586 36113 : I = idealhnf_two(nf,I);
2587 36113 : break;
2588 99009 : case id_MAT:
2589 99009 : if (lg(I)-1 != N) pari_err_DIM("idealred");
2590 99002 : I = Q_primitive_part(I, &c1);
2591 99002 : IZ = gcoeff(I,1,1);
2592 99002 : if (is_pm1(IZ))
2593 : {
2594 8810 : if (!aI) { set_avma(av); return matid(N); }
2595 8726 : goto END;
2596 : }
2597 90192 : J = idealHNF_inv_Z(nf, I);
2598 90192 : 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 126305 : y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
2606 126304 : if (equalii(ZV_content(y), IZ))
2607 : { /* already reduced */
2608 69992 : if (!aI) return gerepilecopy(av, I);
2609 66504 : goto END;
2610 : }
2611 :
2612 56311 : my = zk_multable(nf, y);
2613 56311 : I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
2614 56310 : c1 = mul_content(c1, IZ);
2615 56310 : if (equali1(c1)) c1 = NULL; /* can be simplified with IZ */
2616 56310 : yi = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
2617 56313 : I = hnfmodid(I, Q_denom(yi)); /* denom(yi) generates (y) \cap Z */
2618 56312 : if (!aI) return gerepileupto(av, I);
2619 54648 : if (typ(aI) == t_MAT) /* yi is not integral and usually larger than y */
2620 37912 : aI = famat_div(aI, y);
2621 : else
2622 16736 : c1 = c1? RgC_Rg_mul(yi, c1): yi;
2623 130548 : END:
2624 130548 : if (c1) aI = ext_mul(nf, aI,c1);
2625 130547 : 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 1283421 : idealpseudored(GEN I, GEN G)
2632 1283421 : { 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 138895 : idealpseudomin(GEN I, GEN G)
2637 : {
2638 138895 : GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
2639 138894 : 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 1203773 : idealpseudominvec(GEN I, GEN G)
2653 : {
2654 1203773 : long i, j, k, n = lg(I)-1;
2655 1203773 : GEN x, L, b = idealpseudored(I, G);
2656 1203772 : L = cgetg(1 + (n*(n+1))/2, t_VEC);
2657 4254437 : for (i = k = 1; i <= n; i++)
2658 : {
2659 3050662 : x = gel(b,i);
2660 3050662 : if (!ZV_isscalar(x)) gel(L,k++) = x;
2661 : }
2662 3050664 : for (i = 2; i <= n; i++)
2663 : {
2664 1846889 : long J = minss(i, 4);
2665 4586884 : for (j = 1; j < J; j++)
2666 : {
2667 2739995 : x = ZC_add(gel(b,i),gel(b,j));
2668 2739994 : if (!ZV_isscalar(x)) gel(L,k++) = x;
2669 : }
2670 : }
2671 1203775 : setlg(L,k); return L;
2672 : }
2673 :
2674 : GEN
2675 12584 : idealred_elt(GEN nf, GEN I)
2676 : {
2677 12584 : pari_sp av = avma;
2678 12584 : GEN u = idealpseudomin(I, nf_get_roundG(nf));
2679 12584 : 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 456351 : Z_ppio(GEN a, GEN b)
2710 : {
2711 456351 : GEN x, y, d = gcdii(a,b);
2712 456351 : if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
2713 347053 : x = d; y = diviiexact(a,d);
2714 : for(;;)
2715 63098 : {
2716 410151 : GEN g = gcdii(x,y);
2717 410151 : if (is_pm1(g)) return mkvec3(d, x, y);
2718 63098 : 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 3090773 : Z_cba_rec(GEN L, GEN a, GEN b)
2771 : {
2772 : GEN g;
2773 : /* a few naive steps before switching to dcba */
2774 3090773 : if (lg(L) > 10) { Z_dcba_rec(L, a, b); return veclast(L); }
2775 3090773 : if (is_pm1(a)) return b;
2776 1836618 : g = gcdii(a,b);
2777 1836618 : if (is_pm1(g)) { vectrunc_append(L, a); return b; }
2778 1371958 : a = diviiexact(a,g);
2779 1371958 : b = diviiexact(b,g);
2780 1371958 : return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
2781 : }
2782 : GEN
2783 346857 : Z_cba(GEN a, GEN b)
2784 : {
2785 346857 : GEN L = vectrunc_init(expi(a) + expi(b) + 2);
2786 346857 : GEN t = Z_cba_rec(L, a, b);
2787 346857 : if (!is_pm1(t)) vectrunc_append(L, t);
2788 346857 : return L;
2789 : }
2790 : /* P = coprime base, extend it by b; TODO: quadratic for now */
2791 : GEN
2792 35 : ZV_cba_extend(GEN P, GEN b)
2793 : {
2794 35 : long i, l = lg(P);
2795 35 : GEN w = cgetg(l+1, t_VEC);
2796 133 : for (i = 1; i < l; i++)
2797 : {
2798 98 : GEN v = Z_cba(gel(P,i), b);
2799 98 : long nv = lg(v)-1;
2800 98 : gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
2801 98 : b = gel(v,nv);
2802 : }
2803 35 : gel(w,l) = b; return shallowconcat1(w);
2804 : }
2805 : GEN
2806 28 : ZV_cba(GEN v)
2807 : {
2808 28 : long i, l = lg(v);
2809 : GEN P;
2810 28 : if (l <= 2) return v;
2811 14 : P = Z_cba(gel(v,1), gel(v,2));
2812 42 : for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
2813 14 : return P;
2814 : }
2815 :
2816 : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
2817 : GEN
2818 3200610 : Z_ppo(GEN x, GEN f)
2819 : {
2820 : for (;;)
2821 : {
2822 3200610 : f = gcdii(x, f); if (is_pm1(f)) break;
2823 2173705 : x = diviiexact(x, f);
2824 : }
2825 1026905 : return x;
2826 : }
2827 : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
2828 : ulong
2829 69656412 : u_ppo(ulong x, ulong f)
2830 : {
2831 : for (;;)
2832 : {
2833 69656412 : f = ugcd(x, f); if (f == 1) break;
2834 15788130 : x /= f;
2835 : }
2836 53868220 : return x;
2837 : }
2838 :
2839 : /* result known to be representable as an ulong */
2840 : static ulong
2841 1521283 : lcmuu(ulong a, ulong b) { ulong d = ugcd(a,b); return (a/d) * b; }
2842 :
2843 : /* assume 0 < x < N; return u in (Z/NZ)^* such that u x = gcd(x,N) (mod N);
2844 : * set *pd = gcd(x,N) */
2845 : ulong
2846 5369813 : Fl_invgen(ulong x, ulong N, ulong *pd)
2847 : {
2848 : ulong d, d0, e, v, v1;
2849 : long s;
2850 5369813 : *pd = d = xgcduu(N, x, 0, &v, &v1, &s);
2851 5370112 : if (s > 0) v = N - v;
2852 5370112 : if (d == 1) return v;
2853 : /* vx = gcd(x,N) (mod N), v coprime to N/d but need not be coprime to N */
2854 2600949 : e = N / d;
2855 2600949 : d0 = u_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, rad(d1) | N/d */
2856 2601043 : if (d0 == 1) return v;
2857 1521228 : e = lcmuu(e, d / d0);
2858 1521277 : return u_chinese_coprime(v, 1, e, d0, e*d0);
2859 : }
2860 :
2861 : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
2862 : static GEN
2863 126 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
2864 : {
2865 126 : long v, j, lp = lg(listpr), N = nf_get_degree(nf);
2866 : GEN x1, x2, ex;
2867 :
2868 : #if 0 /*1) via many gcds. Expensive ! */
2869 : GEN f = idealprodprime(nf, listpr);
2870 : f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
2871 : x = scalarmat(x, N);
2872 : for (;;)
2873 : {
2874 : if (gequal1(gcoeff(f,1,1))) break;
2875 : x = idealdivexact(nf, x, f);
2876 : f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
2877 : }
2878 : x2 = x;
2879 : #else /*2) from prime decomposition */
2880 126 : x1 = NULL;
2881 350 : for (j=1; j<lp; j++)
2882 : {
2883 224 : GEN pr = gel(listpr,j);
2884 224 : v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
2885 :
2886 126 : ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
2887 126 : x1 = x1? idealmulpowprime(nf, x1, pr, ex)
2888 126 : : idealpow(nf, pr, ex);
2889 : }
2890 126 : x = scalarmat(x, N);
2891 126 : x2 = x1? idealdivexact(nf, x, x1): x;
2892 : #endif
2893 126 : return x2;
2894 : }
2895 :
2896 : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f */
2897 : GEN
2898 10976 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
2899 : {
2900 : GEN fZ, t, L, D2, d1, d2, d;
2901 :
2902 10976 : L = Q_remove_denom(L0, &d);
2903 10976 : if (!d) return L0;
2904 :
2905 : /* L0 = L / d, L integral */
2906 518 : fZ = gcoeff(f,1,1);
2907 518 : if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
2908 : /* Kill denom part coprime to fZ */
2909 126 : d2 = Z_ppo(d, fZ);
2910 126 : t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
2911 126 : if (equalii(d, d2)) return L;
2912 :
2913 126 : d1 = diviiexact(d, d2);
2914 : /* L0 = (L / d1) mod f. d1 not coprime to f
2915 : * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
2916 126 : D2 = nf_coprime_part(nf, d1, listpr);
2917 126 : t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
2918 126 : L = nfmuli(nf,t,L);
2919 :
2920 : /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
2921 126 : return Q_div_to_int(L, d1); /* exact division */
2922 : }
2923 :
2924 : /* assume L is a list of prime ideals. Return the product */
2925 : GEN
2926 336 : idealprodprime(GEN nf, GEN L)
2927 : {
2928 336 : long l = lg(L), i;
2929 : GEN z;
2930 336 : if (l == 1) return matid(nf_get_degree(nf));
2931 336 : z = pr_hnf(nf, gel(L,1));
2932 364 : for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
2933 336 : return z;
2934 : }
2935 :
2936 : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
2937 : GEN
2938 462 : idealprod(GEN nf, GEN I)
2939 : {
2940 462 : long i, l = lg(I);
2941 : GEN z;
2942 1134 : for (i = 1; i < l; i++)
2943 1127 : if (!equali1(gel(I,i))) break;
2944 462 : if (i == l) return gen_1;
2945 455 : z = gel(I,i);
2946 763 : for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
2947 455 : return z;
2948 : }
2949 :
2950 : /* v_pr(idealprod(nf,I)) */
2951 : long
2952 2722 : idealprodval(GEN nf, GEN I, GEN pr)
2953 : {
2954 2722 : long i, l = lg(I), v = 0;
2955 15586 : for (i = 1; i < l; i++)
2956 12864 : if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
2957 2722 : return v;
2958 : }
2959 :
2960 : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
2961 : GEN
2962 57433 : factorbackprime(GEN nf, GEN L, GEN e)
2963 : {
2964 57433 : long l = lg(L), i;
2965 : GEN z;
2966 :
2967 57433 : if (l == 1) return matid(nf_get_degree(nf));
2968 44042 : z = idealpow(nf, gel(L,1), gel(e,1));
2969 73630 : for (i=2; i<l; i++)
2970 29588 : if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
2971 44042 : return z;
2972 : }
2973 :
2974 : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
2975 : * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
2976 : GEN
2977 58049 : pr_uniformizer(GEN pr, GEN F)
2978 : {
2979 58049 : GEN p = pr_get_p(pr), t = pr_get_gen(pr);
2980 58049 : if (!equalii(F, p))
2981 : {
2982 36779 : long e = pr_get_e(pr);
2983 36779 : GEN u, v, q = (e == 1)? sqri(p): p;
2984 36779 : u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
2985 36779 : v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
2986 36779 : if (pr_is_inert(pr))
2987 28 : t = addii(mulii(p, v), u);
2988 : else
2989 : {
2990 36751 : t = ZC_Z_mul(t, v);
2991 36751 : gel(t,1) = addii(gel(t,1), u); /* return u + vt */
2992 : }
2993 : }
2994 58049 : return t;
2995 : }
2996 : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
2997 : GEN
2998 81771 : prV_lcm_capZ(GEN L)
2999 : {
3000 81771 : long i, r = lg(L);
3001 : GEN F;
3002 81771 : if (r == 1) return gen_1;
3003 69220 : F = pr_get_p(gel(L,1));
3004 122748 : for (i = 2; i < r; i++)
3005 : {
3006 53529 : GEN pr = gel(L,i), p = pr_get_p(pr);
3007 53529 : if (!dvdii(F, p)) F = mulii(F,p);
3008 : }
3009 69219 : return F;
3010 : }
3011 : /* v vector of prid. Return underlying list of rational primes */
3012 : GEN
3013 40362 : prV_primes(GEN v)
3014 : {
3015 40362 : long i, l = lg(v);
3016 40362 : GEN w = cgetg(l,t_VEC);
3017 112091 : for (i=1; i<l; i++) gel(w,i) = pr_get_p(gel(v,i));
3018 40362 : return ZV_sort_uniq(w);
3019 : }
3020 :
3021 : /* Given a prime ideal factorization with possibly zero or negative
3022 : * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
3023 : * and v_pr(b) >= 0 for all other pr.
3024 : * For optimal performance, all [anti-]uniformizers should be precomputed,
3025 : * but no support for this yet. If nored, do not reduce result. */
3026 : static GEN
3027 53876 : idealapprfact_i(GEN nf, GEN x, int nored)
3028 : {
3029 53876 : GEN d = NULL, z, L, e, e2, F;
3030 : long i, r;
3031 53876 : int hasden = 0;
3032 :
3033 53876 : nf = checknf(nf);
3034 53876 : L = gel(x,1);
3035 53876 : e = gel(x,2);
3036 53876 : F = prV_lcm_capZ(L);
3037 53875 : z = NULL; r = lg(e);
3038 136290 : for (i = 1; i < r; i++)
3039 : {
3040 82415 : long s = signe(gel(e,i));
3041 : GEN pi, q;
3042 82415 : if (!s) continue;
3043 53667 : if (s < 0) hasden = 1;
3044 53667 : pi = pr_uniformizer(gel(L,i), F);
3045 53667 : q = nfpow(nf, pi, gel(e,i));
3046 53667 : z = z? nfmul(nf, z, q): q;
3047 : }
3048 53875 : if (!z) return gen_1;
3049 26777 : if (hasden) /* denominator */
3050 : {
3051 10114 : z = Q_remove_denom(z, &d);
3052 10114 : d = diviiexact(d, Z_ppo(d, F));
3053 : }
3054 26779 : if (nored || typ(z) != t_COL) return d? gdiv(z, d): z;
3055 10114 : e2 = cgetg(r, t_VEC);
3056 28694 : for (i = 1; i < r; i++) gel(e2,i) = addiu(gel(e,i), 1);
3057 10114 : x = factorbackprime(nf, L, e2);
3058 10114 : if (d) x = RgM_Rg_mul(x, d);
3059 10114 : z = ZC_reducemodlll(z, x);
3060 10114 : return d? RgC_Rg_div(z,d): z;
3061 : }
3062 :
3063 : GEN
3064 0 : idealapprfact(GEN nf, GEN x) {
3065 0 : pari_sp av = avma;
3066 0 : return gerepileupto(av, idealapprfact_i(nf, x, 0));
3067 : }
3068 : GEN
3069 14 : idealappr(GEN nf, GEN x) {
3070 14 : pari_sp av = avma;
3071 14 : if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
3072 14 : return gerepileupto(av, idealapprfact_i(nf, x, 0));
3073 : }
3074 :
3075 : /* OBSOLETE */
3076 : GEN
3077 14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
3078 :
3079 : static GEN
3080 21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
3081 : {
3082 21 : GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
3083 21 : long i, r = lg(E);
3084 84 : for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
3085 21 : return idealapprfact_i(nf,F,1);
3086 : }
3087 :
3088 : static void
3089 14 : not_in_ideal(GEN a) {
3090 14 : pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
3091 0 : }
3092 : /* x integral in HNF, a an 'nf' */
3093 : static int
3094 28 : in_ideal(GEN x, GEN a)
3095 : {
3096 28 : switch(typ(a))
3097 : {
3098 14 : case t_INT: return dvdii(a, gcoeff(x,1,1));
3099 7 : case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
3100 7 : default: return 0;
3101 : }
3102 : }
3103 :
3104 : /* Given an integral ideal x and a in x, gives a b such that
3105 : * x = aZ_K + bZ_K using the approximation theorem */
3106 : GEN
3107 42 : idealtwoelt2(GEN nf, GEN x, GEN a)
3108 : {
3109 42 : pari_sp av = avma;
3110 : GEN cx, b;
3111 :
3112 42 : nf = checknf(nf);
3113 42 : a = nf_to_scalar_or_basis(nf, a);
3114 42 : x = idealhnf_shallow(nf,x);
3115 42 : if (lg(x) == 1)
3116 : {
3117 14 : if (!isintzero(a)) not_in_ideal(a);
3118 7 : set_avma(av); return gen_0;
3119 : }
3120 28 : x = Q_primitive_part(x, &cx);
3121 28 : if (cx) a = gdiv(a, cx);
3122 28 : if (!in_ideal(x, a)) not_in_ideal(a);
3123 21 : b = mat_ideal_two_elt2(nf, x, a);
3124 21 : if (typ(b) == t_COL)
3125 : {
3126 14 : GEN mod = idealhnf_principal(nf,a);
3127 14 : b = ZC_hnfrem(b,mod);
3128 14 : if (ZV_isscalar(b)) b = gel(b,1);
3129 : }
3130 : else
3131 : {
3132 7 : GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
3133 7 : b = centermodii(b, aZ, shifti(aZ,-1));
3134 : }
3135 21 : b = cx? gmul(b,cx): gcopy(b);
3136 21 : return gerepileupto(av, b);
3137 : }
3138 :
3139 : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
3140 : * beta * x is an integral ideal coprime to y */
3141 : GEN
3142 37204 : idealcoprimefact(GEN nf, GEN x, GEN fy)
3143 : {
3144 37204 : GEN L = gel(fy,1), e;
3145 37204 : long i, r = lg(L);
3146 :
3147 37204 : e = cgetg(r, t_COL);
3148 76067 : for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
3149 37204 : return idealapprfact_i(nf, mkmat2(L,e), 0);
3150 : }
3151 : GEN
3152 84 : idealcoprime(GEN nf, GEN x, GEN y)
3153 : {
3154 84 : pari_sp av = avma;
3155 84 : return gerepileupto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
3156 : }
3157 :
3158 : GEN
3159 7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
3160 : {
3161 7 : pari_sp av = avma;
3162 7 : GEN z, p, pr = modpr, T;
3163 :
3164 7 : nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
3165 0 : x = nf_to_Fq(nf,x,modpr);
3166 0 : y = nf_to_Fq(nf,y,modpr);
3167 0 : z = Fq_mul(x,y,T,p);
3168 0 : return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
3169 : }
3170 :
3171 : GEN
3172 0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
3173 : {
3174 0 : pari_sp av = avma;
3175 0 : nf = checknf(nf);
3176 0 : return gerepileupto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
3177 : }
3178 :
3179 : GEN
3180 0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
3181 : {
3182 0 : pari_sp av=avma;
3183 0 : GEN z, T, p, pr = modpr;
3184 :
3185 0 : nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
3186 0 : z = nf_to_Fq(nf,x,modpr);
3187 0 : z = Fq_pow(z,k,T,p);
3188 0 : return gerepileupto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
3189 : }
3190 :
3191 : GEN
3192 0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
3193 : {
3194 0 : pari_sp av = avma;
3195 0 : GEN T, p, pr = modpr;
3196 :
3197 0 : nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
3198 0 : if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
3199 0 : x = nfM_to_FqM(x, nf, modpr);
3200 0 : return gerepilecopy(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
3201 : }
3202 :
3203 : GEN
3204 0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
3205 : {
3206 0 : const char *f = "nfsolvemodpr";
3207 0 : pari_sp av = avma;
3208 : GEN T, p, modpr;
3209 :
3210 0 : nf = checknf(nf);
3211 0 : modpr = nf_to_Fq_init(nf, &pr,&T,&p);
3212 0 : if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
3213 0 : a = nfM_to_FqM(a, nf, modpr);
3214 0 : switch(typ(b))
3215 : {
3216 0 : case t_MAT:
3217 0 : b = nfM_to_FqM(b, nf, modpr);
3218 0 : b = FqM_gauss(a,b,T,p);
3219 0 : if (!b) pari_err_INV(f,a);
3220 0 : a = FqM_to_nfM(b, modpr);
3221 0 : break;
3222 0 : case t_COL:
3223 0 : b = nfV_to_FqV(b, nf, modpr);
3224 0 : b = FqM_FqC_gauss(a,b,T,p);
3225 0 : if (!b) pari_err_INV(f,a);
3226 0 : a = FqV_to_nfV(b, modpr);
3227 0 : break;
3228 0 : default: pari_err_TYPE(f,b);
3229 : }
3230 0 : return gerepilecopy(av, a);
3231 : }
|