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 16596614 : idealtyp(GEN *ideal, GEN *arch)
47 : {
48 16596614 : GEN x = *ideal;
49 16596614 : long t,lx,tx = typ(x);
50 :
51 16596614 : if (tx!=t_VEC || lg(x)!=3) { if (arch) *arch = NULL; }
52 : else
53 : {
54 1240404 : GEN a = gel(x,2);
55 1240404 : 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 1240390 : if (arch) *arch = a;
62 1240397 : x = gel(x,1); tx = typ(x);
63 : }
64 16596607 : switch(tx)
65 : {
66 12633004 : case t_MAT: lx = lg(x);
67 12633004 : if (lx == 1) { t = id_PRINCIPAL; x = gen_0; break; }
68 12632815 : if (lx != lgcols(x)) pari_err_TYPE("idealtyp [nonsquare t_MAT]",x);
69 12632808 : t = id_MAT;
70 12632808 : break;
71 :
72 3050629 : case t_VEC:
73 3050629 : if (!checkprid_i(x)) pari_err_TYPE("idealtyp [fake prime ideal]",x);
74 3050587 : t = id_PRIME; break;
75 :
76 912967 : case t_POL: case t_POLMOD: case t_COL:
77 : case t_INT: case t_FRAC:
78 912967 : t = id_PRINCIPAL; break;
79 7 : default:
80 7 : pari_err_TYPE("idealtyp",x);
81 : return 0; /*LCOV_EXCL_LINE*/
82 : }
83 16596551 : *ideal = x; return t;
84 : }
85 :
86 : /* true nf; v = [a,x,...], a in Z. Return (a,x) */
87 : GEN
88 803463 : idealhnf_two(GEN nf, GEN v)
89 : {
90 803463 : GEN p = gel(v,1), pi = gel(v,2), m = zk_scalar_or_multable(nf, pi);
91 803463 : if (typ(m) == t_INT) return scalarmat(gcdii(m,p), nf_get_degree(nf));
92 730081 : return ZM_hnfmodid(m, p);
93 : }
94 : /* true nf */
95 : GEN
96 3803304 : pr_hnf(GEN nf, GEN pr)
97 : {
98 3803304 : GEN p = pr_get_p(pr), m;
99 3803304 : if (pr_is_inert(pr)) return scalarmat(p, nf_get_degree(nf));
100 3366403 : m = zk_scalar_or_multable(nf, pr_get_gen(pr));
101 3366403 : return ZM_hnfmodprime(m, p);
102 : }
103 :
104 : GEN
105 1335360 : idealhnf_principal(GEN nf, GEN x)
106 : {
107 : GEN cx;
108 1335360 : x = nf_to_scalar_or_basis(nf, x);
109 1335360 : switch(typ(x))
110 : {
111 1126436 : case t_COL: break;
112 205879 : case t_INT: if (!signe(x)) return cgetg(1,t_MAT);
113 204199 : return scalarmat(absi_shallow(x), nf_get_degree(nf));
114 3045 : case t_FRAC:
115 3045 : return scalarmat(Q_abs_shallow(x), nf_get_degree(nf));
116 0 : default: pari_err_TYPE("idealhnf",x);
117 : }
118 1126436 : x = Q_primitive_part(x, &cx);
119 1126436 : RgV_check_ZV(x, "idealhnf");
120 1126436 : x = zk_multable(nf, x);
121 1126436 : x = ZM_hnfmodid(x, zkmultable_capZ(x));
122 1126436 : return cx? ZM_Q_mul(x,cx): x;
123 : }
124 :
125 : /* true nf; x integral Z_K-module as t_MAT generated by its columns.
126 : * Return square hnf representation */
127 : static GEN
128 462 : vec_mulid(GEN nf, GEN x)
129 : {
130 462 : long i, j, l = lg(x);
131 462 : GEN D = NULL, v;
132 462 : v = cgetg(l, t_VEC);
133 791 : for (i = j = 1; i < l; i++)
134 : {
135 637 : GEN m, d, a = gel(x,i);
136 637 : if (D && ZV_Z_dvd(a, D)) { l--; continue; }
137 630 : gel(v,j++) = m = zk_multable(nf, a);
138 630 : d = zkmultable_capZ(m);
139 630 : D = D? gcdii(D, d): d;
140 630 : if (is_pm1(D)) return matid(lg(m)-1);
141 : }
142 154 : setlg(v, j); if (j == 1) return cgetg(1, t_MAT);
143 154 : return ZM_hnfmodid(shallowconcat1(v), D);
144 : }
145 :
146 : static GEN
147 455 : nfV_to_ZM(GEN nf, GEN x)
148 2282 : { pari_APPLY_type(t_MAT, algtobasis(nf, gel(x,i))) }
149 :
150 :
151 : GEN
152 455 : nfV_idealhnf(GEN nf, GEN v, GEN *pden)
153 : {
154 455 : GEN H = ZM_hnf(Q_remove_denom(nfV_to_ZM(nf, v), pden));
155 455 : return vec_mulid(nf, H);
156 : }
157 :
158 : GEN
159 49 : idealfromgens(GEN nf, GEN v)
160 : {
161 49 : pari_sp av = avma;
162 : GEN H, den;
163 49 : long t = typ(v);
164 49 : if (t==t_MAT) { v = shallowcopy(v); settyp(v, t_VEC); }
165 42 : else if (!is_vec_t(t)) pari_err_TYPE("idealfromgens", v);
166 49 : nf = checknf(nf);
167 49 : H = nfV_idealhnf(nf, v, &den);
168 49 : return gc_upto(av, den ? gdiv(H, den): H);
169 : }
170 :
171 : /* true nf */
172 : GEN
173 1834553 : idealhnf_shallow(GEN nf, GEN x)
174 : {
175 1834553 : long tx = typ(x), lx = lg(x), N;
176 :
177 : /* cannot use idealtyp because here we allow nonsquare matrices */
178 1834553 : if (tx == t_VEC && lx == 3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
179 1834553 : if (tx == t_VEC && lx == 6)
180 : {
181 538722 : if (!checkprid_i(x)) pari_err_TYPE("idealhnf [fake prime ideal]",x);
182 538715 : return pr_hnf(nf,x); /* PRIME */
183 : }
184 1295831 : switch(tx)
185 : {
186 61508 : case t_MAT:
187 : {
188 : GEN cx;
189 61508 : long nx = lx-1;
190 61508 : N = nf_get_degree(nf);
191 61508 : if (nx == 0) return cgetg(1, t_MAT);
192 61501 : if (nbrows(x) != N) pari_err_TYPE("idealhnf [wrong dimension]",x);
193 61494 : if (nx == 1) return idealhnf_principal(nf, gel(x,1)); /* deprecated */
194 :
195 48531 : if (nx == N && RgM_is_QM(x) && QM_ishnf(x)) return x;
196 : /* deprecated */
197 43274 : x = Q_primitive_part(x, &cx);
198 43274 : if (nx < N)
199 7 : x = vec_mulid(nf, x); /* build ZK-module generated from cols */
200 : else
201 43267 : x = ZM_hnfmod(x, ZM_detmult(x)); /* assume Z-span cols is ZK-module */
202 43274 : return cx? ZM_Q_mul(x,cx): x;
203 : }
204 14 : case t_QFB:
205 : {
206 14 : pari_sp av = avma;
207 14 : GEN u, D = nf_get_disc(nf), T = nf_get_pol(nf), f = nf_get_index(nf);
208 14 : GEN A = gel(x,1), B = gel(x,2);
209 14 : N = nf_get_degree(nf);
210 14 : if (N != 2)
211 0 : pari_err_TYPE("idealhnf [Qfb for nonquadratic fields]", x);
212 14 : if (!equalii(qfb_disc(x), D))
213 7 : pari_err_DOMAIN("idealhnf [Qfb]", "disc(q)", "!=", D, x);
214 : /* x -> A Z + (-B + sqrt(D)) / 2 Z
215 : K = Q[t]/T(t), t^2 + ut + v = 0, u^2 - 4v = Df^2
216 : => t = (-u + sqrt(D) f)/2
217 : => sqrt(D)/2 = (t + u/2)/f */
218 7 : u = gel(T,3);
219 7 : B = deg1pol_shallow(ginv(f),
220 : gsub(gdiv(u, shifti(f,1)), gdiv(B,gen_2)),
221 7 : varn(T));
222 7 : return gc_upto(av, idealhnf_two(nf, mkvec2(A,B)));
223 : }
224 1234309 : default: return idealhnf_principal(nf, x); /* PRINCIPAL */
225 : }
226 : }
227 : /* true nf */
228 : GEN
229 665 : idealhnf(GEN nf, GEN x)
230 : {
231 665 : pari_sp av = avma;
232 665 : GEN y = idealhnf_shallow(nf, x);
233 651 : return (avma == av)? gcopy(y): gc_upto(av, y);
234 : }
235 :
236 : static GEN
237 84 : nfV_eltembed(GEN nf, GEN x, long prec)
238 518 : { pari_APPLY_type(t_VEC, nfeltembed(nf, gel(x,i), NULL, prec)) }
239 :
240 : /* true nf */
241 : static GEN
242 84 : nfweilheight_i(GEN nf, GEN v, long prec)
243 : {
244 84 : long i, j, r1, r2, u, N, l = lg(v);
245 84 : GEN den, h = gen_1, id = nfV_idealhnf(nf, v, &den);
246 84 : GEN V = nfV_eltembed(nf, v, prec);
247 :
248 84 : nf_get_sign(nf, &r1, &r2); u = r1 + r2; N = u + r2;
249 259 : for (i = 1; i <= r1; i++)
250 1029 : for (j = 1; j < l; j++) gmael(V,j,i) = gabs(gmael(V,j,i), prec);
251 343 : for ( ; i <= u; i++)
252 1771 : for (j = 1; j < l; j++) gmael(V,j,i) = gnorm(gmael(V,j,i));
253 518 : for (i = 1; i <= u; i++)
254 : {
255 434 : long j0 = 1;
256 2366 : for (j = 2; j < l; j++)
257 1932 : if (gcmp(gmael(V,j,i), gmael(V,j0,i)) > 0) j0 = j;
258 434 : h = gmul(h, gmael(V,j0,i));
259 : }
260 84 : if (den) h = gmul(h, powiu(den, N));
261 84 : return divru(glog(gdiv(h, idealnorm(nf, id)), prec), N);
262 : }
263 :
264 : GEN
265 84 : nfweilheight(GEN nf, GEN v, long prec)
266 : {
267 84 : pari_sp av = avma;
268 84 : nf = checknf(nf);
269 84 : if (!is_vec_t(typ(v)) || lg(v) < 2) pari_err_TYPE("nfweilheight",v);
270 84 : return gc_upto(av, nfweilheight_i(nf, v, prec));
271 : }
272 :
273 : /* GP functions */
274 :
275 : GEN
276 2485 : idealtwoelt0(GEN nf, GEN x, GEN a)
277 : {
278 2485 : if (!a) return idealtwoelt(nf,x);
279 42 : return idealtwoelt2(nf,x,a);
280 : }
281 :
282 : GEN
283 2499 : idealpow0(GEN nf, GEN x, GEN n, long flag)
284 : {
285 2499 : if (flag) return idealpowred(nf,x,n);
286 2492 : return idealpow(nf,x,n);
287 : }
288 :
289 : GEN
290 70 : idealmul0(GEN nf, GEN x, GEN y, long flag)
291 : {
292 70 : if (flag) return idealmulred(nf,x,y);
293 63 : return idealmul(nf,x,y);
294 : }
295 :
296 : GEN
297 56 : idealdiv0(GEN nf, GEN x, GEN y, long flag)
298 : {
299 56 : switch(flag)
300 : {
301 28 : case 0: return idealdiv(nf,x,y);
302 28 : case 1: return idealdivexact(nf,x,y);
303 0 : default: pari_err_FLAG("idealdiv");
304 : }
305 : return NULL; /* LCOV_EXCL_LINE */
306 : }
307 :
308 : GEN
309 70 : idealaddtoone0(GEN nf, GEN arg1, GEN arg2)
310 : {
311 70 : if (!arg2) return idealaddmultoone(nf,arg1);
312 35 : return idealaddtoone(nf,arg1,arg2);
313 : }
314 :
315 : /* b not a scalar */
316 : static GEN
317 77 : hnf_Z_ZC(GEN nf, GEN a, GEN b) { return hnfmodid(zk_multable(nf,b), a); }
318 : /* b not a scalar */
319 : static GEN
320 70 : hnf_Z_QC(GEN nf, GEN a, GEN b)
321 : {
322 : GEN db;
323 70 : b = Q_remove_denom(b, &db);
324 70 : if (db) a = mulii(a, db);
325 70 : b = hnf_Z_ZC(nf,a,b);
326 70 : return db? RgM_Rg_div(b, db): b;
327 : }
328 : /* b not a scalar (not point in trying to optimize for this case) */
329 : static GEN
330 77 : hnf_Q_QC(GEN nf, GEN a, GEN b)
331 : {
332 : GEN da, db;
333 77 : if (typ(a) == t_INT) return hnf_Z_QC(nf, a, b);
334 7 : da = gel(a,2);
335 7 : a = gel(a,1);
336 7 : b = Q_remove_denom(b, &db);
337 : /* write da = d*A, db = d*B, gcd(A,B) = 1
338 : * gcd(a/(d A), b/(d B)) = gcd(a B, A b) / A B d = gcd(a B, b) / A B d */
339 7 : if (db)
340 : {
341 7 : GEN d = gcdii(da,db);
342 7 : if (!is_pm1(d)) db = diviiexact(db,d); /* B */
343 7 : if (!is_pm1(db))
344 : {
345 7 : a = mulii(a, db); /* a B */
346 7 : da = mulii(da, db); /* A B d = lcm(denom(a),denom(b)) */
347 : }
348 : }
349 7 : return RgM_Rg_div(hnf_Z_ZC(nf,a,b), da);
350 : }
351 : static GEN
352 7 : hnf_QC_QC(GEN nf, GEN a, GEN b)
353 : {
354 : GEN da, db, d, x;
355 7 : a = Q_remove_denom(a, &da);
356 7 : b = Q_remove_denom(b, &db);
357 7 : if (da) b = ZC_Z_mul(b, da);
358 7 : if (db) a = ZC_Z_mul(a, db);
359 7 : d = mul_denom(da, db);
360 7 : a = zk_multable(nf,a); da = zkmultable_capZ(a);
361 7 : b = zk_multable(nf,b); db = zkmultable_capZ(b);
362 7 : x = ZM_hnfmodid(shallowconcat(a,b), gcdii(da,db));
363 7 : return d? RgM_Rg_div(x, d): x;
364 : }
365 : static GEN
366 21 : hnf_Q_Q(GEN nf, GEN a, GEN b) {return scalarmat(Q_gcd(a,b), nf_get_degree(nf));}
367 : GEN
368 413 : idealhnf0(GEN nf, GEN a, GEN b)
369 : {
370 : long ta, tb;
371 : pari_sp av;
372 : GEN x;
373 413 : nf = checknf(nf);
374 413 : if (!b) return idealhnf(nf,a);
375 :
376 : /* HNF of aZ_K+bZ_K */
377 112 : av = avma;
378 112 : a = nf_to_scalar_or_basis(nf,a); ta = typ(a);
379 112 : b = nf_to_scalar_or_basis(nf,b); tb = typ(b);
380 105 : if (ta == t_COL)
381 14 : x = (tb==t_COL)? hnf_QC_QC(nf, a,b): hnf_Q_QC(nf, b,a);
382 : else
383 91 : x = (tb==t_COL)? hnf_Q_QC(nf, a,b): hnf_Q_Q(nf, a,b);
384 105 : return gc_upto(av, x);
385 : }
386 :
387 : /*******************************************************************/
388 : /* */
389 : /* TWO-ELEMENT FORM */
390 : /* */
391 : /*******************************************************************/
392 : static GEN idealapprfact_i(GEN nf, GEN x, int nored);
393 :
394 : static int
395 226065 : ok_elt(GEN x, GEN xZ, GEN y)
396 : {
397 226065 : pari_sp av = avma;
398 226065 : return gc_bool(av, ZM_equal(x, ZM_hnfmodid(y, xZ)));
399 : }
400 :
401 : /* a + s * b, a and b ZM, s integer */
402 : static GEN
403 66304 : addmul_mat(GEN a, GEN s, GEN b)
404 : {
405 66304 : if (!signe(s)) return a;
406 57655 : if (!equali1(s)) b = ZM_Z_mul(b, s);
407 57655 : return a? ZM_add(a, b): b;
408 : }
409 :
410 : static GEN
411 118265 : get_random_a(GEN nf, GEN x, GEN xZ)
412 : {
413 : pari_sp av;
414 118265 : long i, lm, l = lg(x);
415 : GEN z, beta, mul;
416 :
417 118265 : beta= cgetg(l, t_MAT);
418 118265 : mul = cgetg(l, t_VEC); lm = 1; /* = lg(mul) */
419 : /* look for a in x such that a O/xZ = x O/xZ */
420 251665 : for (i = 2; i < l; i++)
421 : {
422 241432 : GEN xi = gel(x,i);
423 241432 : GEN t = FpM_red(zk_multable(nf,xi), xZ); /* ZM, cannot be a scalar */
424 241432 : if (gequal0(t)) continue;
425 197919 : if (ok_elt(x,xZ, t)) return xi;
426 89887 : gel(beta,lm) = xi;
427 : /* mul[i] = { canonical generators for x[i] O/xZ as Z-module } */
428 89887 : gel(mul,lm) = t; lm++;
429 : }
430 10233 : setlg(mul, lm);
431 10233 : setlg(beta,lm); z = cgetg(lm, t_VEC);
432 29999 : for(av = avma;; set_avma(av))
433 19766 : {
434 29999 : GEN a = NULL;
435 96303 : for (i = 1; i < lm; i++)
436 : {
437 66304 : gel(z,i) = randomi(xZ);
438 66304 : a = addmul_mat(a, gel(z,i), gel(mul,i));
439 : }
440 : /* a = matrix (NOT HNF) of ideal generated by beta.z in O/xZ */
441 29999 : if (a && ok_elt(x,xZ, a)) break;
442 : }
443 10233 : return ZM_ZC_mul(beta, z);
444 : }
445 :
446 : /* x square matrix, assume it is HNF */
447 : static GEN
448 251103 : mat_ideal_two_elt(GEN nf, GEN x)
449 : {
450 : GEN y, a, cx, xZ;
451 251103 : long N = nf_get_degree(nf);
452 : pari_sp av, tetpil;
453 :
454 251103 : if (lg(x)-1 != N) pari_err_DIM("idealtwoelt");
455 251089 : if (N == 2) return mkvec2copy(gcoeff(x,1,1), gel(x,2));
456 :
457 136847 : y = cgetg(3,t_VEC); av = avma;
458 136847 : cx = Q_content(x);
459 136847 : xZ = gcoeff(x,1,1);
460 136847 : if (gequal(xZ, cx)) /* x = (cx) */
461 : {
462 7566 : gel(y,1) = cx;
463 7566 : gel(y,2) = gen_0; return y;
464 : }
465 129281 : if (equali1(cx)) cx = NULL;
466 : else
467 : {
468 1026 : x = Q_div_to_int(x, cx);
469 1026 : xZ = gcoeff(x,1,1);
470 : }
471 129281 : if (N < 6)
472 109814 : a = get_random_a(nf, x, xZ);
473 : else
474 : {
475 19467 : const long FB[] = { _evallg(15+1) | evaltyp(t_VECSMALL),
476 : 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
477 : };
478 19467 : GEN P, E, a1 = Z_lsmoothen(xZ, (GEN)FB, &P, &E);
479 19467 : if (!a1) /* factors completely */
480 11016 : a = idealapprfact_i(nf, idealfactor(nf,x), 1);
481 8451 : else if (lg(P) == 1) /* no small factors */
482 2604 : a = get_random_a(nf, x, xZ);
483 : else /* general case */
484 : {
485 : GEN A0, A1, a0, u0, u1, v0, v1, pi0, pi1, t, u;
486 5847 : a0 = diviiexact(xZ, a1);
487 5847 : A0 = ZM_hnfmodid(x, a0); /* smooth part of x */
488 5847 : A1 = ZM_hnfmodid(x, a1); /* cofactor */
489 5847 : pi0 = idealapprfact_i(nf, idealfactor(nf,A0), 1);
490 5847 : pi1 = get_random_a(nf, A1, a1);
491 5847 : (void)bezout(a0, a1, &v0,&v1);
492 5847 : u0 = mulii(a0, v0);
493 5847 : u1 = mulii(a1, v1);
494 5847 : if (typ(pi0) != t_COL) t = addmulii(u0, pi0, u1);
495 : else
496 5847 : { t = ZC_Z_mul(pi0, u1); gel(t,1) = addii(gel(t,1), u0); }
497 5847 : u = ZC_Z_mul(pi1, u0); gel(u,1) = addii(gel(u,1), u1);
498 5847 : a = nfmuli(nf, centermod(u, xZ), centermod(t, xZ));
499 : }
500 : }
501 129281 : if (cx)
502 : {
503 1026 : a = centermod(a, xZ);
504 1026 : tetpil = avma;
505 1026 : if (typ(cx) == t_INT)
506 : {
507 91 : gel(y,1) = mulii(xZ, cx);
508 91 : gel(y,2) = ZC_Z_mul(a, cx);
509 : }
510 : else
511 : {
512 935 : gel(y,1) = gmul(xZ, cx);
513 935 : gel(y,2) = RgC_Rg_mul(a, cx);
514 : }
515 : }
516 : else
517 : {
518 128255 : tetpil = avma;
519 128255 : gel(y,1) = icopy(xZ);
520 128255 : gel(y,2) = centermod(a, xZ);
521 : }
522 129281 : gc_slice_unsafe(av,tetpil,y+1,2); return y;
523 : }
524 :
525 : /* Given an ideal x, returns [a,alpha] such that a is in Q,
526 : * x = a Z_K + alpha Z_K, alpha in K^*
527 : * a = 0 or alpha = 0 are possible, but do not try to determine whether
528 : * x is principal. */
529 : GEN
530 97948 : idealtwoelt(GEN nf, GEN x)
531 : {
532 : pari_sp av;
533 97948 : long tx = idealtyp(&x, NULL);
534 97941 : nf = checknf(nf);
535 97941 : if (tx == id_MAT) return mat_ideal_two_elt(nf,x);
536 735 : if (tx == id_PRIME) return mkvec2copy(gel(x,1), gel(x,2));
537 : /* id_PRINCIPAL */
538 714 : av = avma; x = nf_to_scalar_or_basis(nf, x);
539 1232 : return gc_GEN(av, typ(x)==t_COL? mkvec2(gen_0,x):
540 609 : mkvec2(Q_abs_shallow(x),gen_0));
541 : }
542 :
543 : /*******************************************************************/
544 : /* */
545 : /* FACTORIZATION */
546 : /* */
547 : /*******************************************************************/
548 : /* x integral ideal in HNF, Zval = v_p(x \cap Z) > 0; return v_p(Nx) */
549 : static long
550 4247877 : idealHNF_norm_pval(GEN x, GEN p, long Zval)
551 : {
552 4247877 : long i, v = Zval, l = lg(x);
553 32402405 : for (i = 2; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
554 4247877 : return v;
555 : }
556 :
557 : /* x integral in HNF, f0 = partial factorization of a multiple of
558 : * x[1,1] = x\cap Z */
559 : GEN
560 294023 : idealHNF_Z_factor_i(GEN x, GEN f0, GEN *pvN, GEN *pvZ)
561 : {
562 294023 : GEN P, E, vN, vZ, xZ = gcoeff(x,1,1), f = f0? f0: Z_factor(xZ);
563 : long i, l;
564 294023 : P = gel(f,1); l = lg(P);
565 294023 : E = gel(f,2);
566 294023 : *pvN = vN = cgetg(l, t_VECSMALL);
567 294023 : *pvZ = vZ = cgetg(l, t_VECSMALL);
568 734763 : for (i = 1; i < l; i++)
569 : {
570 440740 : GEN p = gel(P,i);
571 440740 : vZ[i] = f0? Z_pval(xZ, p): (long) itou(gel(E,i));
572 440740 : vN[i] = idealHNF_norm_pval(x,p, vZ[i]);
573 : }
574 294023 : return P;
575 : }
576 : /* return P, primes dividing Nx and xZ = x\cap Z, set v_p(Nx), v_p(xZ);
577 : * x integral in HNF */
578 : GEN
579 0 : idealHNF_Z_factor(GEN x, GEN *pvN, GEN *pvZ)
580 0 : { return idealHNF_Z_factor_i(x, NULL, pvN, pvZ); }
581 :
582 : /* v_P(A)*f(P) <= Nval [e.g. Nval = v_p(Norm A)], Zval = v_p(A \cap Z).
583 : * Return v_P(A) */
584 : static long
585 4385112 : idealHNF_val(GEN A, GEN P, long Nval, long Zval)
586 : {
587 4385112 : long f = pr_get_f(P), vmax, v, e, i, j, k, l;
588 : GEN mul, B, a, y, r, p, pk, cx, vals;
589 : pari_sp av;
590 :
591 4385112 : if (Nval < f) return 0;
592 4381942 : p = pr_get_p(P);
593 4381942 : e = pr_get_e(P);
594 : /* v_P(A) <= max [ e * v_p(A \cap Z), floor[v_p(Nix) / f ] */
595 4381942 : vmax = minss(Zval * e, Nval / f);
596 4381942 : mul = pr_get_tau(P);
597 4381942 : l = lg(mul);
598 4381942 : B = cgetg(l,t_MAT);
599 : /* B[1] not needed: v_pr(A[1]) = v_pr(A \cap Z) is known already */
600 4381942 : gel(B,1) = gen_0; /* dummy */
601 23791847 : for (j = 2; j < l; j++)
602 : {
603 21320562 : GEN x = gel(A,j);
604 21320562 : gel(B,j) = y = cgetg(l, t_COL);
605 229652768 : for (i = 1; i < l; i++)
606 : { /* compute a = (x.t0)_i, A in HNF ==> x[j+1..l-1] = 0 */
607 210242863 : a = mulii(gel(x,1), gcoeff(mul,i,1));
608 1471531497 : for (k = 2; k <= j; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
609 : /* p | a ? */
610 210242863 : gel(y,i) = dvmdii(a,p,&r); if (signe(r)) return 0;
611 : }
612 : }
613 2471285 : vals = cgetg(l, t_VECSMALL);
614 : /* vals[1] not needed */
615 16969457 : for (j = 2; j < l; j++)
616 : {
617 14498172 : gel(B,j) = Q_primitive_part(gel(B,j), &cx);
618 14498172 : vals[j] = cx? 1 + e * Q_pval(cx, p): 1;
619 : }
620 2471285 : pk = powiu(p, ceildivuu(vmax, e));
621 2471285 : av = avma; y = cgetg(l,t_COL);
622 : /* can compute mod p^ceil((vmax-v)/e) */
623 4160445 : for (v = 1; v < vmax; v++)
624 : { /* we know v_pr(Bj) >= v for all j */
625 1719462 : if (e == 1 || (vmax - v) % e == 0) pk = diviiexact(pk, p);
626 8524965 : for (j = 2; j < l; j++)
627 : {
628 6835805 : GEN x = gel(B,j); if (v < vals[j]) continue;
629 44673281 : for (i = 1; i < l; i++)
630 : {
631 40062286 : pari_sp av2 = avma;
632 40062286 : a = mulii(gel(x,1), gcoeff(mul,i,1));
633 521972570 : for (k = 2; k < l; k++) a = addii(a, mulii(gel(x,k), gcoeff(mul,i,k)));
634 : /* a = (x.t_0)_i; p | a ? */
635 40062286 : a = dvmdii(a,p,&r); if (signe(r)) return v;
636 40031984 : if (lgefint(a) > lgefint(pk)) a = remii(a, pk);
637 40031984 : gel(y,i) = gc_INT(av2, a);
638 : }
639 4610995 : gel(B,j) = y; y = x;
640 4610995 : if (gc_needed(av,3))
641 : {
642 0 : if(DEBUGMEM>1) pari_warn(warnmem,"idealval");
643 0 : (void)gc_all(av,3, &y,&B,&pk);
644 : }
645 : }
646 : }
647 2440983 : return v;
648 : }
649 : /* true nf, x != 0 integral ideal in HNF, cx t_INT or NULL,
650 : * FA integer factorization matrix or NULL. Return partial factorization of
651 : * cx * x above primes in FA (complete factorization if !FA)*/
652 : static GEN
653 294023 : idealHNF_factor_i(GEN nf, GEN x, GEN cx, GEN FA)
654 : {
655 294023 : const long N = lg(x)-1;
656 : long i, j, k, l, v;
657 294023 : GEN vN, vZ, vP, vE, vp = idealHNF_Z_factor_i(x, FA, &vN,&vZ);
658 :
659 294023 : l = lg(vp);
660 294023 : i = cx? expi(cx)+1: 1;
661 294023 : vP = cgetg((l+i-2)*N+1, t_COL);
662 294023 : vE = cgetg((l+i-2)*N+1, t_COL);
663 734763 : for (i = k = 1; i < l; i++)
664 : {
665 440740 : GEN L, p = gel(vp,i);
666 440740 : long Nval = vN[i], Zval = vZ[i], vc = cx? Z_pvalrem(cx,p,&cx): 0;
667 440740 : if (vc)
668 : {
669 48621 : L = idealprimedec(nf,p);
670 48621 : if (is_pm1(cx)) cx = NULL;
671 : }
672 : else
673 392119 : L = idealprimedec_limit_f(nf,p,Nval);
674 1018715 : for (j = 1; Nval && j < lg(L); j++) /* !Nval => only cx contributes */
675 : {
676 577975 : GEN P = gel(L,j);
677 577975 : pari_sp av = avma;
678 577975 : v = idealHNF_val(x, P, Nval, Zval);
679 577975 : set_avma(av);
680 577975 : Nval -= v*pr_get_f(P);
681 577975 : v += vc * pr_get_e(P); if (!v) continue;
682 483541 : gel(vP,k) = P;
683 483541 : gel(vE,k) = utoipos(v); k++;
684 : }
685 491675 : if (vc) for (; j<lg(L); j++)
686 : {
687 50935 : GEN P = gel(L,j);
688 50935 : gel(vP,k) = P;
689 50935 : gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
690 : }
691 : }
692 294023 : if (cx && !FA)
693 : { /* complete factorization */
694 73365 : GEN f = Z_factor(cx), cP = gel(f,1), cE = gel(f,2);
695 73365 : long lc = lg(cP);
696 159421 : for (i=1; i<lc; i++)
697 : {
698 86056 : GEN p = gel(cP,i), L = idealprimedec(nf,p);
699 86056 : long vc = itos(gel(cE,i));
700 188317 : for (j=1; j<lg(L); j++)
701 : {
702 102261 : GEN P = gel(L,j);
703 102261 : gel(vP,k) = P;
704 102261 : gel(vE,k) = utoipos(vc * pr_get_e(P)); k++;
705 : }
706 : }
707 : }
708 294023 : setlg(vP, k);
709 294023 : setlg(vE, k); return mkmat2(vP, vE);
710 : }
711 : /* true nf, x integral ideal */
712 : static GEN
713 240113 : idealHNF_factor(GEN nf, GEN x, ulong lim)
714 : {
715 240113 : GEN cx, F = NULL;
716 240113 : if (lim)
717 : {
718 : GEN P, E;
719 : long i;
720 : /* strict useless because of prime table */
721 77 : F = absZ_factor_limit(gcoeff(x,1,1), lim);
722 77 : P = gel(F,1);
723 77 : E = gel(F,2);
724 : /* filter out entries > lim */
725 126 : for (i = lg(P)-1; i; i--)
726 126 : if (cmpiu(gel(P,i), lim) <= 0) break;
727 77 : setlg(P, i+1);
728 77 : setlg(E, i+1);
729 : }
730 240113 : x = Q_primitive_part(x, &cx);
731 240113 : return idealHNF_factor_i(nf, x, cx, F);
732 : }
733 : /* c * vector(#L,i,L[i].e), assume results fit in ulong */
734 : static GEN
735 28588 : prV_e_muls(GEN L, long c)
736 : {
737 28588 : long j, l = lg(L);
738 28588 : GEN z = cgetg(l, t_COL);
739 58891 : for (j = 1; j < l; j++) gel(z,j) = stoi(c * pr_get_e(gel(L,j)));
740 28588 : return z;
741 : }
742 : /* true nf, y in Q */
743 : static GEN
744 28532 : Q_nffactor(GEN nf, GEN y, ulong lim)
745 : {
746 : GEN f, P, E;
747 : long l, i;
748 28532 : if (typ(y) == t_INT)
749 : {
750 28504 : if (!signe(y)) pari_err_DOMAIN("idealfactor", "ideal", "=",gen_0,y);
751 28490 : if (is_pm1(y)) return trivial_fact();
752 : }
753 18025 : y = Q_abs_shallow(y);
754 18025 : if (!lim) f = Q_factor(y);
755 : else
756 : {
757 189 : f = Q_factor_limit(y, lim);
758 189 : P = gel(f,1);
759 189 : E = gel(f,2);
760 273 : for (i = lg(P)-1; i > 0; i--)
761 238 : if (abscmpiu(gel(P,i), lim) < 0) break;
762 189 : setlg(P,i+1); setlg(E,i+1);
763 : }
764 18025 : P = gel(f,1); l = lg(P); if (l == 1) return f;
765 17990 : E = gel(f,2);
766 46578 : for (i = 1; i < l; i++)
767 : {
768 28588 : gel(P,i) = idealprimedec(nf, gel(P,i));
769 28588 : gel(E,i) = prV_e_muls(gel(P,i), itos(gel(E,i)));
770 : }
771 17990 : P = shallowconcat1(P); gel(f,1) = P; settyp(P, t_COL);
772 17990 : E = shallowconcat1(E); gel(f,2) = E; return f;
773 : }
774 :
775 : GEN
776 25515 : idealfactor_partial(GEN nf, GEN x, GEN L)
777 : {
778 25515 : pari_sp av = avma;
779 : long i, j, l;
780 : GEN P, E;
781 25515 : if (!L) return idealfactor(nf, x);
782 24661 : if (typ(L) == t_INT) return idealfactor_limit(nf, x, itou(L));
783 24633 : l = lg(L); if (l == 1) return trivial_fact();
784 23863 : P = cgetg(l, t_VEC);
785 89964 : for (i = 1; i < l; i++)
786 : {
787 66101 : GEN p = gel(L,i);
788 66101 : gel(P,i) = typ(p) == t_INT? idealprimedec(nf, p): mkvec(p);
789 : }
790 23863 : P = shallowconcat1(P); settyp(P, t_COL);
791 23863 : P = gen_sort_uniq(P, (void*)&cmp_prime_ideal, &cmp_nodata);
792 23863 : E = cgetg_copy(P, &l);
793 114695 : for (i = j = 1; i < l; i++)
794 : {
795 90832 : long v = idealval(nf, x, gel(P,i));
796 90832 : if (v) { gel(P,j) = gel(P,i); gel(E,j) = stoi(v); j++; }
797 : }
798 23863 : setlg(P,j);
799 23863 : setlg(E,j); return gc_GEN(av, mkmat2(P, E));
800 : }
801 : GEN
802 268771 : idealfactor_limit(GEN nf, GEN x, ulong lim)
803 : {
804 268771 : pari_sp av = avma;
805 : GEN fa, y;
806 268771 : long tx = idealtyp(&x, NULL);
807 :
808 268750 : if (tx == id_PRIME)
809 : {
810 119 : if (lim && abscmpiu(pr_get_p(x), lim) >= 0) return trivial_fact();
811 112 : retmkmat2(mkcolcopy(x), mkcol(gen_1));
812 : }
813 268631 : nf = checknf(nf);
814 268631 : if (tx == id_PRINCIPAL)
815 : {
816 29883 : y = nf_to_scalar_or_basis(nf, x);
817 29883 : if (typ(y) != t_COL) return gc_GEN(av, Q_nffactor(nf, y, lim));
818 : }
819 240099 : y = idealnumden(nf, x);
820 240099 : fa = idealHNF_factor(nf, gel(y,1), lim);
821 240099 : if (!isint1(gel(y,2)))
822 14 : fa = famat_div_shallow(fa, idealHNF_factor(nf, gel(y,2), lim));
823 240099 : fa = gc_GEN(av, fa);
824 240099 : return sort_factor(fa, (void*)&cmp_prime_ideal, &cmp_nodata);
825 : }
826 : GEN
827 268344 : idealfactor(GEN nf, GEN x) { return idealfactor_limit(nf, x, 0); }
828 : GEN
829 189 : gpidealfactor(GEN nf, GEN x, GEN lim)
830 : {
831 189 : ulong L = 0;
832 189 : if (lim)
833 : {
834 70 : if (typ(lim) != t_INT || signe(lim) < 0) pari_err_FLAG("idealfactor");
835 70 : L = itou(lim);
836 : }
837 189 : return idealfactor_limit(nf, x, L);
838 : }
839 :
840 : static GEN
841 7749 : ramified_root(GEN nf, GEN R, GEN A, long n)
842 : {
843 7749 : GEN v, P = gel(idealfactor(nf, R), 1);
844 7749 : long i, l = lg(P);
845 7749 : v = cgetg(l, t_VECSMALL);
846 8414 : for (i = 1; i < l; i++)
847 : {
848 672 : long w = idealval(nf, A, gel(P,i));
849 672 : if (w % n) return NULL;
850 665 : v[i] = w / n;
851 : }
852 7742 : return idealfactorback(nf, P, v, 0);
853 : }
854 : static int
855 7 : ramified_root_simple(GEN nf, long n, GEN P, GEN v)
856 : {
857 7 : long i, l = lg(v);
858 21 : for (i = 1; i < l; i++)
859 : {
860 14 : long w = v[i] % n;
861 14 : if (w)
862 : {
863 7 : GEN vpr = idealprimedec(nf, gel(P,i));
864 7 : long lpr = lg(vpr), j;
865 14 : for (j = 1; j < lpr; j++)
866 : {
867 7 : long e = pr_get_e(gel(vpr,j));
868 7 : if ((e * w) % n) return 0;
869 : }
870 : }
871 : }
872 7 : return 1;
873 : }
874 : /* true nf, n > 1, A a non-zero integral ideal; check whether A is the n-th
875 : * power of an ideal and set *pB to its n-th root if so */
876 : static long
877 7756 : idealsqrtn_int(GEN nf, GEN A, long n, GEN *pB)
878 : {
879 : GEN C, root;
880 : long i, l;
881 :
882 7756 : if (typ(A) == t_MAT && ZM_isscalar(A, NULL)) A = gcoeff(A,1,1);
883 7756 : if (typ(A) == t_INT) /* > 0 */
884 : {
885 5551 : GEN P = nf_get_ramified_primes(nf), v, q;
886 5551 : l = lg(P); v = cgetg(l, t_VECSMALL);
887 24962 : for (i = 1; i < l; i++) v[i] = Z_pvalrem(A, gel(P,i), &A);
888 5551 : C = gen_1;
889 5551 : if (!isint1(A) && !Z_ispowerall(A, n, pB? &C: NULL)) return 0;
890 5551 : if (!pB) return ramified_root_simple(nf, n, P, v);
891 5544 : q = factorback2(P, v);
892 5544 : root = ramified_root(nf, q, q, n);
893 5544 : if (!root) return 0;
894 5544 : if (!equali1(C)) root = isint1(root)? C: ZM_Z_mul(root, C);
895 5544 : *pB = root; return 1;
896 : }
897 : /* compute valuations at ramified primes */
898 2205 : root = ramified_root(nf, idealadd(nf, nf_get_diff(nf), A), A, n);
899 2205 : if (!root) return 0;
900 : /* remove ramified primes */
901 2198 : if (isint1(root))
902 1834 : root = matid(nf_get_degree(nf));
903 : else
904 364 : A = idealdivexact(nf, A, idealpows(nf,root,n));
905 2198 : A = Q_primitive_part(A, &C);
906 2198 : if (C)
907 : {
908 7 : if (!Z_ispowerall(C,n,&C)) return 0;
909 0 : if (pB) root = ZM_Z_mul(root, C);
910 : }
911 :
912 : /* compute final n-th root, at most degree(nf)-1 iterations */
913 2191 : for (i = 0;; i++)
914 2079 : {
915 4270 : GEN J, b, a = gcoeff(A,1,1); /* A \cap Z */
916 4270 : if (is_pm1(a)) break;
917 2107 : if (!Z_ispowerall(a,n,&b)) return 0;
918 2079 : J = idealadd(nf, b, A);
919 2079 : A = idealdivexact(nf, idealpows(nf,J,n), A);
920 : /* div and not divexact here */
921 2079 : if (pB) root = odd(i)? idealdiv(nf, root, J): idealmul(nf, root, J);
922 : }
923 2163 : if (pB) *pB = root;
924 2163 : return 1;
925 : }
926 :
927 : /* A is assumed to be the n-th power of an ideal in nf
928 : returns its n-th root. */
929 : long
930 3906 : idealispower(GEN nf, GEN A, long n, GEN *pB)
931 : {
932 3906 : pari_sp av = avma;
933 : GEN v, N, D;
934 3906 : nf = checknf(nf);
935 3906 : if (n <= 0) pari_err_DOMAIN("idealispower", "n", "<=", gen_0, stoi(n));
936 3906 : if (n == 1) { if (pB) *pB = idealhnf(nf,A); return 1; }
937 3899 : v = idealnumden(nf,A);
938 3899 : if (gequal0(gel(v,1))) { set_avma(av); if (pB) *pB = cgetg(1,t_MAT); return 1; }
939 3899 : if (!idealsqrtn_int(nf, gel(v,1), n, pB? &N: NULL)) return 0;
940 3857 : if (!idealsqrtn_int(nf, gel(v,2), n, pB? &D: NULL)) return 0;
941 3857 : if (pB) *pB = gc_upto(av, idealdiv(nf,N,D)); else set_avma(av);
942 3857 : return 1;
943 : }
944 :
945 : /* x t_INT or integral nonzero ideal in HNF */
946 : static GEN
947 118370 : idealredmodpower_i(GEN nf, GEN x, ulong k, ulong B)
948 : {
949 : GEN cx, y, U, N, F, Q;
950 118370 : if (typ(x) == t_INT)
951 : {
952 63644 : if (!signe(x) || is_pm1(x)) return gen_1;
953 3549 : F = Z_factor_limit(x, B);
954 3549 : gel(F,2) = gdiventgs(gel(F,2), k);
955 3549 : return ginv(factorback(F));
956 : }
957 54726 : N = gcoeff(x,1,1); if (is_pm1(N)) return gen_1;
958 53910 : F = absZ_factor_limit_strict(N, B, &U);
959 53910 : if (U)
960 : {
961 147 : GEN M = powii(gel(U,1), gel(U,2));
962 147 : y = hnfmodid(x, M); /* coprime part to B! */
963 147 : if (!idealispower(nf, y, k, &U)) U = NULL;
964 147 : x = hnfmodid(x, diviiexact(N, M));
965 : }
966 : /* x = B-smooth part of initial x */
967 53910 : x = Q_primitive_part(x, &cx);
968 53910 : F = idealHNF_factor_i(nf, x, cx, F);
969 53910 : gel(F,2) = gdiventgs(gel(F,2), k);
970 53910 : Q = idealfactorback(nf, gel(F,1), gel(F,2), 0);
971 53910 : if (U) Q = idealmul(nf,Q,U);
972 53910 : if (typ(Q) == t_INT) return Q;
973 13154 : y = idealred_elt(nf, idealHNF_inv_Z(nf, Q));
974 13154 : return gdiv(y, gcoeff(Q,1,1));
975 : }
976 : GEN
977 59192 : idealredmodpower(GEN nf, GEN x, ulong n, ulong B)
978 : {
979 59192 : pari_sp av = avma;
980 : GEN a, b;
981 59192 : nf = checknf(nf);
982 59192 : if (!n) pari_err_DOMAIN("idealredmodpower","n", "=", gen_0, gen_0);
983 59192 : x = idealnumden(nf, x);
984 59192 : a = gel(x,1);
985 59192 : if (isintzero(a)) { set_avma(av); return gen_1; }
986 59185 : a = idealredmodpower_i(nf, gel(x,1), n, B);
987 59185 : b = idealredmodpower_i(nf, gel(x,2), n, B);
988 59185 : if (!isint1(b)) a = nf_to_scalar_or_basis(nf, nfdiv(nf, a, b));
989 59185 : return gc_GEN(av, a);
990 : }
991 :
992 : /* P prime ideal in idealprimedec format. Return valuation(A) at P */
993 : long
994 9806852 : idealval(GEN nf, GEN A, GEN P)
995 : {
996 9806852 : pari_sp av = avma;
997 : GEN p, cA;
998 9806852 : long vcA, v, Zval, tx = idealtyp(&A, NULL);
999 :
1000 9806852 : if (tx == id_PRINCIPAL) return nfval(nf,A,P);
1001 9694187 : checkprid(P);
1002 9694180 : if (tx == id_PRIME) return pr_equal(P, A)? 1: 0;
1003 : /* id_MAT */
1004 9694152 : nf = checknf(nf);
1005 9694152 : A = Q_primitive_part(A, &cA);
1006 9694152 : p = pr_get_p(P);
1007 9694152 : vcA = cA? Q_pval(cA,p): 0;
1008 9694152 : if (pr_is_inert(P)) return gc_long(av,vcA);
1009 9296083 : Zval = Z_pval(gcoeff(A,1,1), p);
1010 9296083 : if (!Zval) v = 0;
1011 : else
1012 : {
1013 3807137 : long Nval = idealHNF_norm_pval(A, p, Zval);
1014 3807137 : v = idealHNF_val(A, P, Nval, Zval);
1015 : }
1016 9296083 : return gc_long(av, vcA? v + vcA*pr_get_e(P): v);
1017 : }
1018 : GEN
1019 7119 : gpidealval(GEN nf, GEN ix, GEN P)
1020 : {
1021 7119 : long v = idealval(nf,ix,P);
1022 7105 : return v == LONG_MAX? mkoo(): stoi(v);
1023 : }
1024 :
1025 : /* gcd and generalized Bezout */
1026 :
1027 : GEN
1028 83578 : idealadd(GEN nf, GEN x, GEN y)
1029 : {
1030 83578 : pari_sp av = avma;
1031 : long tx, ty;
1032 : GEN z, a, dx, dy, dz;
1033 :
1034 83578 : tx = idealtyp(&x, NULL);
1035 83578 : ty = idealtyp(&y, NULL); nf = checknf(nf);
1036 83578 : if (tx != id_MAT) x = idealhnf_shallow(nf,x);
1037 83578 : if (ty != id_MAT) y = idealhnf_shallow(nf,y);
1038 83578 : if (lg(x) == 1) return gc_GEN(av,y);
1039 82703 : if (lg(y) == 1) return gc_GEN(av,x); /* check for 0 ideal */
1040 82108 : dx = Q_denom(x);
1041 82108 : dy = Q_denom(y); dz = lcmii(dx,dy);
1042 82108 : if (is_pm1(dz)) dz = NULL; else {
1043 5975 : x = Q_muli_to_int(x, dz);
1044 5975 : y = Q_muli_to_int(y, dz);
1045 : }
1046 82108 : a = gcdii(gcoeff(x,1,1), gcoeff(y,1,1));
1047 82108 : if (is_pm1(a))
1048 : {
1049 22889 : long N = lg(x)-1;
1050 22889 : if (!dz) { set_avma(av); return matid(N); }
1051 1085 : return gc_upto(av, scalarmat(ginv(dz), N));
1052 : }
1053 59219 : z = ZM_hnfmodid(shallowconcat(x,y), a);
1054 59219 : if (dz) z = RgM_Rg_div(z,dz);
1055 59219 : return gc_upto(av,z);
1056 : }
1057 :
1058 : static GEN
1059 28 : trivial_merge(GEN x)
1060 28 : { return (lg(x) == 1 || !is_pm1(gcoeff(x,1,1)))? NULL: gen_1; }
1061 : /* true nf */
1062 : static GEN
1063 803264 : _idealaddtoone(GEN nf, GEN x, GEN y, long red)
1064 : {
1065 : GEN a;
1066 803264 : long tx = idealtyp(&x, NULL);
1067 803264 : long ty = idealtyp(&y, NULL);
1068 : long ea;
1069 803264 : if (tx != id_MAT) x = idealhnf_shallow(nf, x);
1070 803264 : if (ty != id_MAT) y = idealhnf_shallow(nf, y);
1071 803264 : if (lg(x) == 1)
1072 14 : a = trivial_merge(y);
1073 803250 : else if (lg(y) == 1)
1074 14 : a = trivial_merge(x);
1075 : else
1076 803236 : a = hnfmerge_get_1(x, y);
1077 803264 : if (!a) pari_err_COPRIME("idealaddtoone",x,y);
1078 803250 : if (red && (ea = gexpo(a)) > 10)
1079 : {
1080 4557 : GEN b = (typ(a) == t_COL)? a: scalarcol_shallow(a, nf_get_degree(nf));
1081 4557 : b = ZC_reducemodlll(b, idealHNF_mul(nf,x,y));
1082 4557 : if (gexpo(b) < ea) a = b;
1083 : }
1084 803250 : return a;
1085 : }
1086 : /* true nf */
1087 : GEN
1088 17633 : idealaddtoone_i(GEN nf, GEN x, GEN y)
1089 17633 : { return _idealaddtoone(nf, x, y, 1); }
1090 : /* true nf */
1091 : GEN
1092 785631 : idealaddtoone_raw(GEN nf, GEN x, GEN y)
1093 785631 : { return _idealaddtoone(nf, x, y, 0); }
1094 :
1095 : GEN
1096 98 : idealaddtoone(GEN nf, GEN x, GEN y)
1097 : {
1098 98 : GEN z = cgetg(3,t_VEC), a;
1099 98 : pari_sp av = avma;
1100 98 : nf = checknf(nf);
1101 98 : a = gc_upto(av, idealaddtoone_i(nf,x,y));
1102 84 : gel(z,1) = a;
1103 84 : gel(z,2) = typ(a) == t_COL? Z_ZC_sub(gen_1,a): subui(1,a);
1104 84 : return z;
1105 : }
1106 :
1107 : /* assume elements of list are integral ideals */
1108 : GEN
1109 35 : idealaddmultoone(GEN nf, GEN list)
1110 : {
1111 35 : pari_sp av = avma;
1112 35 : long N, i, l, nz, tx = typ(list);
1113 : GEN H, U, perm, L;
1114 :
1115 35 : nf = checknf(nf); N = nf_get_degree(nf);
1116 35 : if (!is_vec_t(tx)) pari_err_TYPE("idealaddmultoone",list);
1117 35 : l = lg(list);
1118 35 : L = cgetg(l, t_VEC);
1119 35 : if (l == 1)
1120 0 : pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
1121 35 : nz = 0; /* number of nonzero ideals in L */
1122 98 : for (i=1; i<l; i++)
1123 : {
1124 70 : GEN I = gel(list,i);
1125 70 : if (typ(I) != t_MAT) I = idealhnf_shallow(nf,I);
1126 70 : if (lg(I) != 1)
1127 : {
1128 42 : nz++; RgM_check_ZM(I,"idealaddmultoone");
1129 35 : if (lgcols(I) != N+1) pari_err_TYPE("idealaddmultoone [not an ideal]", I);
1130 : }
1131 63 : gel(L,i) = I;
1132 : }
1133 28 : H = ZM_hnfperm(shallowconcat1(L), &U, &perm);
1134 28 : if (lg(H) == 1 || !equali1(gcoeff(H,1,1)))
1135 7 : pari_err_DOMAIN("idealaddmultoone", "sum(ideals)", "!=", gen_1, L);
1136 49 : for (i=1; i<=N; i++)
1137 49 : if (perm[i] == 1) break;
1138 21 : U = gel(U,(nz-1)*N + i); /* (L[1]|...|L[nz]) U = 1 */
1139 21 : nz = 0;
1140 63 : for (i=1; i<l; i++)
1141 : {
1142 42 : GEN c = gel(L,i);
1143 42 : if (lg(c) == 1)
1144 14 : c = gen_0;
1145 : else {
1146 28 : c = ZM_ZC_mul(c, vecslice(U, nz*N + 1, (nz+1)*N));
1147 28 : nz++;
1148 : }
1149 42 : gel(L,i) = c;
1150 : }
1151 21 : return gc_GEN(av, L);
1152 : }
1153 :
1154 : /* multiplication */
1155 :
1156 : /* x integral ideal (without archimedean component) in HNF form
1157 : * y = [a,alpha] corresponds to the integral ideal aZ_K+alpha Z_K, a in Z,
1158 : * alpha a ZV or a ZM (multiplication table). Multiply them */
1159 : static GEN
1160 990501 : idealHNF_mul_two(GEN nf, GEN x, GEN y)
1161 : {
1162 990501 : GEN m, a = gel(y,1), alpha = gel(y,2);
1163 : long i, N;
1164 :
1165 990501 : if (typ(alpha) != t_MAT)
1166 : {
1167 628041 : alpha = zk_scalar_or_multable(nf, alpha);
1168 628041 : if (typ(alpha) == t_INT) /* e.g. y inert ? 0 should not (but may) occur */
1169 16925 : return signe(a)? ZM_Z_mul(x, gcdii(a, alpha)): cgetg(1,t_MAT);
1170 : }
1171 973576 : N = lg(x)-1; m = cgetg((N<<1)+1,t_MAT);
1172 3852569 : for (i=1; i<=N; i++) gel(m,i) = ZM_ZC_mul(alpha,gel(x,i));
1173 3852569 : for (i=1; i<=N; i++) gel(m,i+N) = ZC_Z_mul(gel(x,i), a);
1174 973576 : return ZM_hnfmodid(m, mulii(a, gcoeff(x,1,1)));
1175 : }
1176 :
1177 : /* Assume x and y are integral in HNF form [NOT extended]. Not memory clean.
1178 : * HACK: ideal in y can be of the form [a,b], a in Z, b in Z_K */
1179 : GEN
1180 501928 : idealHNF_mul(GEN nf, GEN x, GEN y)
1181 : {
1182 : GEN z;
1183 501928 : if (typ(y) == t_VEC)
1184 316586 : z = idealHNF_mul_two(nf,x,y);
1185 : else
1186 : { /* reduce one ideal to two-elt form. The smallest */
1187 185342 : GEN xZ = gcoeff(x,1,1), yZ = gcoeff(y,1,1);
1188 185342 : if (cmpii(xZ, yZ) < 0)
1189 : {
1190 42706 : if (is_pm1(xZ)) return gcopy(y);
1191 20343 : z = idealHNF_mul_two(nf, y, mat_ideal_two_elt(nf,x));
1192 : }
1193 : else
1194 : {
1195 142636 : if (is_pm1(yZ)) return gcopy(x);
1196 36307 : z = idealHNF_mul_two(nf, x, mat_ideal_two_elt(nf,y));
1197 : }
1198 : }
1199 373236 : return z;
1200 : }
1201 :
1202 : /* operations on elements in factored form */
1203 :
1204 : GEN
1205 224916 : famat_mul_shallow(GEN f, GEN g)
1206 : {
1207 224916 : if (typ(f) != t_MAT) f = to_famat_shallow(f,gen_1);
1208 224916 : if (typ(g) != t_MAT) g = to_famat_shallow(g,gen_1);
1209 224916 : if (lgcols(f) == 1) return g;
1210 167619 : if (lgcols(g) == 1) return f;
1211 165674 : return mkmat2(shallowconcat(gel(f,1), gel(g,1)),
1212 165674 : shallowconcat(gel(f,2), gel(g,2)));
1213 : }
1214 : GEN
1215 88342 : famat_mulpow_shallow(GEN f, GEN g, GEN e)
1216 : {
1217 88342 : if (!signe(e)) return f;
1218 54580 : return famat_mul_shallow(f, famat_pow_shallow(g, e));
1219 : }
1220 :
1221 : GEN
1222 147672 : famat_mulpows_shallow(GEN f, GEN g, long e)
1223 : {
1224 147672 : if (e==0) return f;
1225 118747 : return famat_mul_shallow(f, famat_pows_shallow(g, e));
1226 : }
1227 :
1228 : GEN
1229 10311 : famat_div_shallow(GEN f, GEN g)
1230 10311 : { return famat_mul_shallow(f, famat_inv_shallow(g)); }
1231 :
1232 : GEN
1233 376294 : Z_to_famat(GEN x)
1234 : {
1235 : long k;
1236 376294 : if (equali1(x)) return trivial_fact();
1237 192495 : k = Z_isanypower(x, &x) ;
1238 192495 : return to_famat_shallow(x, k? utoi(k): gen_1);
1239 : }
1240 : GEN
1241 197087 : Q_to_famat(GEN x)
1242 : {
1243 197087 : if (typ(x) == t_INT) return Z_to_famat(x);
1244 179207 : return famat_div(Z_to_famat(gel(x,1)), Z_to_famat(gel(x,2)));
1245 : }
1246 : GEN
1247 0 : to_famat(GEN x, GEN y) { retmkmat2(mkcolcopy(x), mkcolcopy(y)); }
1248 : GEN
1249 2769802 : to_famat_shallow(GEN x, GEN y) { return mkmat2(mkcol(x), mkcol(y)); }
1250 :
1251 : /* concat the single elt x; not gconcat since x may be a t_COL */
1252 : static GEN
1253 155939 : append(GEN v, GEN x)
1254 : {
1255 155939 : long i, l = lg(v);
1256 155939 : GEN w = cgetg(l+1, typ(v));
1257 661460 : for (i=1; i<l; i++) gel(w,i) = gcopy(gel(v,i));
1258 155939 : gel(w,i) = gcopy(x); return w;
1259 : }
1260 : /* add x^1 to famat f */
1261 : static GEN
1262 162633 : famat_add(GEN f, GEN x)
1263 : {
1264 162633 : GEN h = cgetg(3,t_MAT);
1265 162633 : if (lgcols(f) == 1)
1266 : {
1267 13203 : gel(h,1) = mkcolcopy(x);
1268 13203 : gel(h,2) = mkcol(gen_1);
1269 : }
1270 : else
1271 : {
1272 149430 : gel(h,1) = append(gel(f,1), x);
1273 149430 : gel(h,2) = gconcat(gel(f,2), gen_1);
1274 : }
1275 162633 : return h;
1276 : }
1277 : /* add x^-1 to famat f */
1278 : static GEN
1279 20913 : famat_sub(GEN f, GEN x)
1280 : {
1281 20913 : GEN h = cgetg(3,t_MAT);
1282 20913 : if (lgcols(f) == 1)
1283 : {
1284 14404 : gel(h,1) = mkcolcopy(x);
1285 14404 : gel(h,2) = mkcol(gen_m1);
1286 : }
1287 : else
1288 : {
1289 6509 : gel(h,1) = append(gel(f,1), x);
1290 6509 : gel(h,2) = gconcat(gel(f,2), gen_m1);
1291 : }
1292 20913 : return h;
1293 : }
1294 :
1295 : GEN
1296 451462 : famat_mul(GEN f, GEN g)
1297 : {
1298 : GEN h;
1299 451462 : if (typ(g) != t_MAT) {
1300 30522 : if (typ(f) == t_MAT) return famat_add(f, g);
1301 0 : h = cgetg(3, t_MAT);
1302 0 : gel(h,1) = mkcol2(gcopy(f), gcopy(g));
1303 0 : gel(h,2) = mkcol2(gen_1, gen_1);
1304 0 : return h;
1305 : }
1306 420940 : if (typ(f) != t_MAT) return famat_add(g, f);
1307 288829 : if (lgcols(f) == 1) return gcopy(g);
1308 265496 : if (lgcols(g) == 1) return gcopy(f);
1309 259502 : h = cgetg(3,t_MAT);
1310 259502 : gel(h,1) = gconcat(gel(f,1), gel(g,1));
1311 259502 : gel(h,2) = gconcat(gel(f,2), gel(g,2));
1312 259502 : return h;
1313 : }
1314 :
1315 : GEN
1316 200127 : famat_div(GEN f, GEN g)
1317 : {
1318 : GEN h;
1319 200127 : if (typ(g) != t_MAT) {
1320 20871 : if (typ(f) == t_MAT) return famat_sub(f, g);
1321 0 : h = cgetg(3, t_MAT);
1322 0 : gel(h,1) = mkcol2(gcopy(f), gcopy(g));
1323 0 : gel(h,2) = mkcol2(gen_1, gen_m1);
1324 0 : return h;
1325 : }
1326 179256 : if (typ(f) != t_MAT) return famat_sub(g, f);
1327 179214 : if (lgcols(f) == 1) return famat_inv(g);
1328 260 : if (lgcols(g) == 1) return gcopy(f);
1329 260 : h = cgetg(3,t_MAT);
1330 260 : gel(h,1) = gconcat(gel(f,1), gel(g,1));
1331 260 : gel(h,2) = gconcat(gel(f,2), gneg(gel(g,2)));
1332 260 : return h;
1333 : }
1334 :
1335 : GEN
1336 22984 : famat_sqr(GEN f)
1337 : {
1338 : GEN h;
1339 22984 : if (typ(f) != t_MAT) return to_famat(f,gen_2);
1340 22984 : if (lgcols(f) == 1) return gcopy(f);
1341 13138 : h = cgetg(3,t_MAT);
1342 13138 : gel(h,1) = gcopy(gel(f,1));
1343 13138 : gel(h,2) = gmul2n(gel(f,2),1);
1344 13138 : return h;
1345 : }
1346 :
1347 : GEN
1348 26948 : famat_inv_shallow(GEN f)
1349 : {
1350 26948 : if (typ(f) != t_MAT) return to_famat_shallow(f,gen_m1);
1351 10451 : if (lgcols(f) == 1) return f;
1352 10451 : return mkmat2(gel(f,1), ZC_neg(gel(f,2)));
1353 : }
1354 : GEN
1355 199364 : famat_inv(GEN f)
1356 : {
1357 199364 : if (typ(f) != t_MAT) return to_famat(f,gen_m1);
1358 199364 : if (lgcols(f) == 1) return gcopy(f);
1359 180817 : retmkmat2(gcopy(gel(f,1)), ZC_neg(gel(f,2)));
1360 : }
1361 : GEN
1362 60642 : famat_pow(GEN f, GEN n)
1363 : {
1364 60642 : if (typ(f) != t_MAT) return to_famat(f,n);
1365 60642 : if (lgcols(f) == 1) return gcopy(f);
1366 60642 : retmkmat2(gcopy(gel(f,1)), ZC_Z_mul(gel(f,2),n));
1367 : }
1368 : GEN
1369 62027 : famat_pow_shallow(GEN f, GEN n)
1370 : {
1371 62027 : if (is_pm1(n)) return signe(n) > 0? f: famat_inv_shallow(f);
1372 35690 : if (typ(f) != t_MAT) return to_famat_shallow(f,n);
1373 8016 : if (lgcols(f) == 1) return f;
1374 6103 : return mkmat2(gel(f,1), ZC_Z_mul(gel(f,2),n));
1375 : }
1376 :
1377 : GEN
1378 151898 : famat_pows_shallow(GEN f, long n)
1379 : {
1380 151898 : if (n==1) return f;
1381 35008 : if (n==-1) return famat_inv_shallow(f);
1382 35001 : if (typ(f) != t_MAT) return to_famat_shallow(f, stoi(n));
1383 26740 : if (lgcols(f) == 1) return f;
1384 26740 : return mkmat2(gel(f,1), ZC_z_mul(gel(f,2),n));
1385 : }
1386 :
1387 : GEN
1388 0 : famat_Z_gcd(GEN M, GEN n)
1389 : {
1390 0 : pari_sp av=avma;
1391 0 : long i, j, l=lgcols(M);
1392 0 : GEN F=cgetg(3,t_MAT);
1393 0 : gel(F,1)=cgetg(l,t_COL);
1394 0 : gel(F,2)=cgetg(l,t_COL);
1395 0 : for (i=1, j=1; i<l; i++)
1396 : {
1397 0 : GEN p = gcoeff(M,i,1);
1398 0 : GEN e = gminsg(Z_pval(n,p),gcoeff(M,i,2));
1399 0 : if (signe(e))
1400 : {
1401 0 : gcoeff(F,j,1)=p;
1402 0 : gcoeff(F,j,2)=e;
1403 0 : j++;
1404 : }
1405 : }
1406 0 : setlg(gel(F,1),j); setlg(gel(F,2),j);
1407 0 : return gc_GEN(av,F);
1408 : }
1409 :
1410 : /* x assumed to be a t_MATs (factorization matrix), or compatible with
1411 : * the element_* functions. */
1412 : static GEN
1413 33946 : ext_sqr(GEN nf, GEN x)
1414 33946 : { return (typ(x)==t_MAT)? famat_sqr(x): nfsqr(nf, x); }
1415 : static GEN
1416 61064 : ext_mul(GEN nf, GEN x, GEN y)
1417 61064 : { return (typ(x)==t_MAT)? famat_mul(x,y): nfmul(nf, x, y); }
1418 : static GEN
1419 20410 : ext_inv(GEN nf, GEN x)
1420 20410 : { return (typ(x)==t_MAT)? famat_inv(x): nfinv(nf, x); }
1421 : static GEN
1422 0 : ext_pow(GEN nf, GEN x, GEN n)
1423 0 : { return (typ(x)==t_MAT)? famat_pow(x,n): nfpow(nf, x, n); }
1424 :
1425 : GEN
1426 0 : famat_to_nf(GEN nf, GEN f)
1427 : {
1428 : GEN t, x, e;
1429 : long i;
1430 0 : if (lgcols(f) == 1) return gen_1;
1431 0 : x = gel(f,1);
1432 0 : e = gel(f,2);
1433 0 : t = nfpow(nf, gel(x,1), gel(e,1));
1434 0 : for (i=lg(x)-1; i>1; i--)
1435 0 : t = nfmul(nf, t, nfpow(nf, gel(x,i), gel(e,i)));
1436 0 : return t;
1437 : }
1438 :
1439 : GEN
1440 0 : famat_idealfactor(GEN nf, GEN x)
1441 : {
1442 : long i, l;
1443 0 : GEN g = gel(x,1), e = gel(x,2), h = cgetg_copy(g, &l);
1444 0 : for (i = 1; i < l; i++) gel(h,i) = idealfactor(nf, gel(g,i));
1445 0 : h = famat_reduce(famatV_factorback(h,e));
1446 0 : return sort_factor(h, (void*)&cmp_prime_ideal, &cmp_nodata);
1447 : }
1448 :
1449 : GEN
1450 308206 : famat_reduce(GEN fa)
1451 : {
1452 : GEN E, G, L, g, e;
1453 : long i, k, l;
1454 :
1455 308206 : if (typ(fa) != t_MAT || lgcols(fa) == 1) return fa;
1456 297263 : g = gel(fa,1); l = lg(g);
1457 297263 : e = gel(fa,2);
1458 297263 : L = gen_indexsort(g, (void*)&cmp_universal, &cmp_nodata);
1459 297263 : G = cgetg(l, t_COL);
1460 297263 : E = cgetg(l, t_COL);
1461 : /* merge */
1462 2366665 : for (k=i=1; i<l; i++,k++)
1463 : {
1464 2069402 : gel(G,k) = gel(g,L[i]);
1465 2069402 : gel(E,k) = gel(e,L[i]);
1466 2069402 : if (k > 1 && gidentical(gel(G,k), gel(G,k-1)))
1467 : {
1468 839987 : gel(E,k-1) = addii(gel(E,k), gel(E,k-1));
1469 839987 : k--;
1470 : }
1471 : }
1472 : /* kill 0 exponents */
1473 297263 : l = k;
1474 1526678 : for (k=i=1; i<l; i++)
1475 1229415 : if (!gequal0(gel(E,i)))
1476 : {
1477 1204644 : gel(G,k) = gel(G,i);
1478 1204644 : gel(E,k) = gel(E,i); k++;
1479 : }
1480 297263 : setlg(G, k);
1481 297263 : setlg(E, k); return mkmat2(G,E);
1482 : }
1483 : GEN
1484 77 : matreduce(GEN f)
1485 77 : { pari_sp av = avma;
1486 77 : switch(typ(f))
1487 : {
1488 35 : case t_VEC: case t_COL:
1489 : {
1490 35 : GEN e; f = vec_reduce(f, &e); settyp(f, t_COL);
1491 35 : return gc_GEN(av, mkmat2(f, zc_to_ZC(e)));
1492 : }
1493 35 : case t_MAT:
1494 35 : if (lg(f) == 3) break;
1495 : default:
1496 14 : pari_err_TYPE("matreduce", f);
1497 : }
1498 28 : if (typ(gel(f,1)) == t_VECSMALL)
1499 0 : f = famatsmall_reduce(f);
1500 : else
1501 : {
1502 28 : if (!RgV_is_ZV(gel(f,2))) pari_err_TYPE("matreduce",f);
1503 21 : f = famat_reduce(f);
1504 : }
1505 21 : return gc_GEN(av, f);
1506 : }
1507 :
1508 : GEN
1509 185882 : famatsmall_reduce(GEN fa)
1510 : {
1511 : GEN E, G, L, g, e;
1512 : long i, k, l;
1513 185882 : if (lgcols(fa) == 1) return fa;
1514 185882 : g = gel(fa,1); l = lg(g);
1515 185882 : e = gel(fa,2);
1516 185882 : L = vecsmall_indexsort(g);
1517 185882 : G = cgetg(l, t_VECSMALL);
1518 185882 : E = cgetg(l, t_VECSMALL);
1519 : /* merge */
1520 504633 : for (k=i=1; i<l; i++,k++)
1521 : {
1522 318751 : G[k] = g[L[i]];
1523 318751 : E[k] = e[L[i]];
1524 318751 : if (k > 1 && G[k] == G[k-1])
1525 : {
1526 7966 : E[k-1] += E[k];
1527 7966 : k--;
1528 : }
1529 : }
1530 : /* kill 0 exponents */
1531 185882 : l = k;
1532 496667 : for (k=i=1; i<l; i++)
1533 310785 : if (E[i])
1534 : {
1535 307149 : G[k] = G[i];
1536 307149 : E[k] = E[i]; k++;
1537 : }
1538 185882 : setlg(G, k);
1539 185882 : setlg(E, k); return mkmat2(G,E);
1540 : }
1541 :
1542 : GEN
1543 67007 : famat_remove_trivial(GEN fa)
1544 : {
1545 67007 : GEN P, E, p = gel(fa,1), e = gel(fa,2);
1546 67007 : long j, k, l = lg(p);
1547 67007 : P = cgetg(l, t_COL);
1548 67007 : E = cgetg(l, t_COL);
1549 1826945 : for (j = k = 1; j < l; j++)
1550 1759938 : if (signe(gel(e,j))) { gel(P,k) = gel(p,j); gel(E,k++) = gel(e,j); }
1551 67007 : setlg(P, k); setlg(E, k); return mkmat2(P,E);
1552 : }
1553 :
1554 : GEN
1555 13814 : famatV_factorback(GEN v, GEN e)
1556 : {
1557 13814 : long i, l = lg(e);
1558 : GEN V;
1559 13814 : if (l == 1) return trivial_fact();
1560 13429 : V = signe(gel(e,1))? famat_pow_shallow(gel(v,1), gel(e,1)): trivial_fact();
1561 56788 : for (i = 2; i < l; i++) V = famat_mulpow_shallow(V, gel(v,i), gel(e,i));
1562 13429 : return V;
1563 : }
1564 :
1565 : GEN
1566 56273 : famatV_zv_factorback(GEN v, GEN e)
1567 : {
1568 56273 : long i, l = lg(e);
1569 : GEN V;
1570 56273 : if (l == 1) return trivial_fact();
1571 53816 : V = uel(e,1)? famat_pows_shallow(gel(v,1), uel(e,1)): trivial_fact();
1572 177814 : for (i = 2; i < l; i++) V = famat_mulpows_shallow(V, gel(v,i), uel(e,i));
1573 53816 : return V;
1574 : }
1575 :
1576 : GEN
1577 568151 : ZM_famat_limit(GEN fa, GEN limit)
1578 : {
1579 : pari_sp av;
1580 : GEN E, G, g, e, r;
1581 : long i, k, l, n, lG;
1582 :
1583 568151 : if (lgcols(fa) == 1) return fa;
1584 568144 : g = gel(fa,1); l = lg(g);
1585 568144 : e = gel(fa,2);
1586 1137436 : for(n=0, i=1; i<l; i++)
1587 569292 : if (cmpii(gel(g,i),limit)<=0) n++;
1588 568144 : lG = n<l-1 ? n+2 : n+1;
1589 568144 : G = cgetg(lG, t_COL);
1590 568144 : E = cgetg(lG, t_COL);
1591 568144 : av = avma;
1592 1137436 : for (i=1, k=1, r = gen_1; i<l; i++)
1593 : {
1594 569292 : if (cmpii(gel(g,i),limit)<=0)
1595 : {
1596 569159 : gel(G,k) = gel(g,i);
1597 569159 : gel(E,k) = gel(e,i);
1598 569159 : k++;
1599 133 : } else r = mulii(r, powii(gel(g,i), gel(e,i)));
1600 : }
1601 568144 : if (k<i)
1602 : {
1603 133 : gel(G, k) = gc_INT(av, r);
1604 133 : gel(E, k) = gen_1;
1605 : }
1606 568144 : return mkmat2(G,E);
1607 : }
1608 :
1609 : /* assume pr has degree 1 and coprime to Q_denom(x) */
1610 : static GEN
1611 122255 : to_Fp_coprime(GEN nf, GEN x, GEN modpr)
1612 : {
1613 122255 : GEN d, r, p = modpr_get_p(modpr);
1614 122255 : x = nf_to_scalar_or_basis(nf,x);
1615 122255 : if (typ(x) != t_COL) return Rg_to_Fp(x,p);
1616 121016 : x = Q_remove_denom(x, &d);
1617 121016 : r = zk_to_Fq(x, modpr);
1618 121016 : if (d) r = Fp_div(r, d, p);
1619 121016 : return r;
1620 : }
1621 :
1622 : /* pr coprime to all denominators occurring in x */
1623 : static GEN
1624 665 : famat_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
1625 : {
1626 665 : GEN p = modpr_get_p(modpr);
1627 665 : GEN t = NULL, g = gel(x,1), e = gel(x,2), q = subiu(p,1);
1628 665 : long i, l = lg(g);
1629 3808 : for (i = 1; i < l; i++)
1630 : {
1631 3143 : GEN n = modii(gel(e,i), q);
1632 3143 : if (signe(n))
1633 : {
1634 3122 : GEN h = to_Fp_coprime(nf, gel(g,i), modpr);
1635 3122 : h = Fp_pow(h, n, p);
1636 3122 : t = t? Fp_mul(t, h, p): h;
1637 : }
1638 : }
1639 665 : return t? modii(t, p): gen_1;
1640 : }
1641 :
1642 : /* cf famat_to_nf_modideal_coprime, modpr attached to prime of degree 1 */
1643 : GEN
1644 119798 : nf_to_Fp_coprime(GEN nf, GEN x, GEN modpr)
1645 : {
1646 665 : return typ(x)==t_MAT? famat_to_Fp_coprime(nf, x, modpr)
1647 120463 : : to_Fp_coprime(nf, x, modpr);
1648 : }
1649 :
1650 : static long
1651 4331943 : zk_pvalrem(GEN x, GEN p, GEN *py)
1652 4331943 : { return (typ(x) == t_INT)? Z_pvalrem(x, p, py): ZV_pvalrem(x, p, py); }
1653 : /* x a QC or Q. Return a ZC or Z, whose content is coprime to Z. Set v, dx
1654 : * such that x = p^v (newx / dx); dx = NULL if 1 */
1655 : static GEN
1656 4377767 : nf_remove_denom_p(GEN nf, GEN x, GEN p, GEN *pdx, long *pv)
1657 : {
1658 : long vcx;
1659 : GEN dx;
1660 4377767 : x = nf_to_scalar_or_basis(nf, x);
1661 4377767 : x = Q_remove_denom(x, &dx);
1662 4377767 : if (dx)
1663 : {
1664 61224 : vcx = - Z_pvalrem(dx, p, &dx);
1665 61224 : if (!vcx) vcx = zk_pvalrem(x, p, &x);
1666 61224 : if (isint1(dx)) dx = NULL;
1667 : }
1668 : else
1669 : {
1670 4316543 : vcx = zk_pvalrem(x, p, &x);
1671 4316543 : dx = NULL;
1672 : }
1673 4377767 : *pv = vcx;
1674 4377767 : *pdx = dx; return x;
1675 : }
1676 : /* x = b^e/p^(e-1) in Z_K; x = 0 mod p/pr^e, (x,pr) = 1. Return NULL
1677 : * if p inert (instead of 1) */
1678 : static GEN
1679 95075 : p_makecoprime(GEN pr)
1680 : {
1681 95075 : GEN B = pr_get_tau(pr), b;
1682 : long i, e;
1683 :
1684 95075 : if (typ(B) == t_INT) return NULL;
1685 72318 : b = gel(B,1); /* B = multiplication table by b */
1686 72318 : e = pr_get_e(pr);
1687 72318 : if (e == 1) return b;
1688 : /* one could also divide (exactly) by p in each iteration */
1689 45385 : for (i = 1; i < e; i++) b = ZM_ZC_mul(B, b);
1690 22179 : return ZC_Z_divexact(b, powiu(pr_get_p(pr), e-1));
1691 : }
1692 :
1693 : /* Compute A = prod g[i]^e[i] mod pr^k, assuming (A, pr) = 1.
1694 : * Method: modify each g[i] so that it becomes coprime to pr,
1695 : * g[i] *= (b/p)^v_pr(g[i]), where b/p = pr^(-1) times something integral
1696 : * and prime to p; globally, we multiply by (b/p)^v_pr(A) = 1.
1697 : * Optimizations:
1698 : * 1) remove all powers of p from contents, and consider extra generator p^vp;
1699 : * modified as p * (b/p)^e = b^e / p^(e-1)
1700 : * 2) remove denominators, coprime to p, by multiplying by inverse mod prk\cap Z
1701 : *
1702 : * EX = multiple of exponent of (O_K / pr^k)^* used to reduce the product in
1703 : * case the e[i] are large */
1704 : GEN
1705 2220969 : famat_makecoprime(GEN nf, GEN g, GEN e, GEN pr, GEN prk, GEN EX)
1706 : {
1707 2220969 : GEN G, E, t, vp = NULL, p = pr_get_p(pr), prkZ = gcoeff(prk, 1,1);
1708 2220969 : long i, l = lg(g);
1709 :
1710 2220969 : G = cgetg(l+1, t_VEC);
1711 2220969 : E = cgetg(l+1, t_VEC); /* l+1: room for "modified p" */
1712 6598736 : for (i=1; i < l; i++)
1713 : {
1714 : long vcx;
1715 4377767 : GEN dx, x = nf_remove_denom_p(nf, gel(g,i), p, &dx, &vcx);
1716 4377767 : if (vcx) /* = v_p(content(g[i])) */
1717 : {
1718 142283 : GEN a = mulsi(vcx, gel(e,i));
1719 142283 : vp = vp? addii(vp, a): a;
1720 : }
1721 : /* x integral, content coprime to p; dx coprime to p */
1722 4377767 : if (typ(x) == t_INT)
1723 : { /* x coprime to p, hence to pr */
1724 1136142 : x = modii(x, prkZ);
1725 1136142 : if (dx) x = Fp_div(x, dx, prkZ);
1726 : }
1727 : else
1728 : {
1729 3241625 : (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
1730 3241625 : x = ZC_hnfrem(FpC_red(x,prkZ), prk);
1731 3241625 : if (dx) x = FpC_Fp_mul(x, Fp_inv(dx,prkZ), prkZ);
1732 : }
1733 4377767 : gel(G,i) = x;
1734 4377767 : gel(E,i) = gel(e,i);
1735 : }
1736 :
1737 2220969 : t = vp? p_makecoprime(pr): NULL;
1738 2220969 : if (!t)
1739 : { /* no need for extra generator */
1740 2148728 : setlg(G,l);
1741 2148728 : setlg(E,l);
1742 : }
1743 : else
1744 : {
1745 72241 : gel(G,i) = FpC_red(t, prkZ);
1746 72241 : gel(E,i) = vp;
1747 : }
1748 2220969 : return famat_to_nf_modideal_coprime(nf, G, E, prk, EX);
1749 : }
1750 :
1751 : /* simplified version of famat_makecoprime for X = SUnits[1] */
1752 : GEN
1753 98 : sunits_makecoprime(GEN X, GEN pr, GEN prk)
1754 : {
1755 98 : GEN G, p = pr_get_p(pr), prkZ = gcoeff(prk,1,1);
1756 98 : long i, l = lg(X);
1757 :
1758 98 : G = cgetg(l, t_VEC);
1759 9205 : for (i = 1; i < l; i++)
1760 : {
1761 9107 : GEN x = gel(X,i);
1762 9107 : if (typ(x) == t_INT) /* a prime */
1763 1491 : x = equalii(x,p)? p_makecoprime(pr): modii(x, prkZ);
1764 : else
1765 : {
1766 7616 : (void)ZC_nfvalrem(x, pr, &x); /* x *= (b/p)^v_pr(x) */
1767 7616 : x = ZC_hnfrem(FpC_red(x,prkZ), prk);
1768 : }
1769 9107 : gel(G,i) = x;
1770 : }
1771 98 : return G;
1772 : }
1773 :
1774 : /* prod g[i]^e[i] mod bid, assume (g[i], id) = 1 and 1 < lg(g) <= lg(e) */
1775 : GEN
1776 18606 : famat_to_nf_moddivisor(GEN nf, GEN g, GEN e, GEN bid)
1777 : {
1778 18606 : GEN t, cyc = bid_get_cyc(bid);
1779 18606 : if (lg(cyc) == 1)
1780 0 : t = gen_1;
1781 : else
1782 18606 : t = famat_to_nf_modideal_coprime(nf, g, e, bid_get_ideal(bid),
1783 : cyc_get_expo(cyc));
1784 18606 : return set_sign_mod_divisor(nf, mkmat2(g,e), t, bid_get_sarch(bid));
1785 : }
1786 :
1787 : GEN
1788 16133742 : vecmul(GEN x, GEN y)
1789 : {
1790 16133742 : if (!is_vec_t(typ(x))) return gmul(x,y);
1791 3556394 : pari_APPLY_same(vecmul(gel(x,i), gel(y,i)))
1792 : }
1793 :
1794 : GEN
1795 185983 : vecsqr(GEN x)
1796 : {
1797 185983 : if (!is_vec_t(typ(x))) return gsqr(x);
1798 46606 : pari_APPLY_same(vecsqr(gel(x,i)))
1799 : }
1800 :
1801 : GEN
1802 826 : vecinv(GEN x)
1803 : {
1804 826 : if (!is_vec_t(typ(x))) return ginv(x);
1805 56 : pari_APPLY_same(vecinv(gel(x,i)))
1806 : }
1807 :
1808 : GEN
1809 0 : vecpow(GEN x, GEN n)
1810 : {
1811 0 : if (!is_vec_t(typ(x))) return powgi(x,n);
1812 0 : pari_APPLY_same(vecpow(gel(x,i), n))
1813 : }
1814 :
1815 : GEN
1816 903 : vecdiv(GEN x, GEN y)
1817 : {
1818 903 : if (!is_vec_t(typ(x))) return gdiv(x,y);
1819 903 : pari_APPLY_same(vecdiv(gel(x,i), gel(y,i)))
1820 : }
1821 :
1822 : /* A ideal as a square t_MAT */
1823 : static GEN
1824 287056 : idealmulelt(GEN nf, GEN x, GEN A)
1825 : {
1826 : long i, lx;
1827 : GEN dx, dA, D;
1828 287056 : if (lg(A) == 1) return cgetg(1, t_MAT);
1829 287056 : x = nf_to_scalar_or_basis(nf,x);
1830 287056 : if (typ(x) != t_COL)
1831 : {
1832 86976 : if (isintzero(x)) return cgetg(1,t_MAT);
1833 86976 : x = Q_abs_shallow(x);
1834 86976 : return isint1(x)? gcopy(A): RgM_Rg_mul(A, x);
1835 : }
1836 200080 : x = Q_remove_denom(x, &dx);
1837 200080 : A = Q_remove_denom(A, &dA);
1838 200080 : x = zk_multable(nf, x);
1839 200080 : D = mulii(zkmultable_capZ(x), gcoeff(A,1,1));
1840 200080 : x = zkC_multable_mul(A, x);
1841 200080 : settyp(x, t_MAT); lx = lg(x);
1842 : /* x may contain scalars (at most 1 since the ideal is nonzero)*/
1843 770710 : for (i=1; i<lx; i++)
1844 580620 : if (typ(gel(x,i)) == t_INT)
1845 : {
1846 9990 : if (i > 1) swap(gel(x,1), gel(x,i)); /* help HNF */
1847 9990 : gel(x,1) = scalarcol_shallow(gel(x,1), lx-1);
1848 9990 : break;
1849 : }
1850 200080 : x = ZM_hnfmodid(x, D);
1851 200080 : dx = mul_denom(dx,dA);
1852 200080 : return dx? gdiv(x,dx): x;
1853 : }
1854 :
1855 : /* nf a true nf, tx <= ty */
1856 : static GEN
1857 512061 : idealmul_aux(GEN nf, GEN x, GEN y, long tx, long ty)
1858 : {
1859 : GEN z, cx, cy;
1860 512061 : switch(tx)
1861 : {
1862 292278 : case id_PRINCIPAL:
1863 292278 : switch(ty)
1864 : {
1865 4809 : case id_PRINCIPAL:
1866 4809 : return idealhnf_principal(nf, nfmul(nf,x,y));
1867 413 : case id_PRIME:
1868 : {
1869 413 : GEN p = pr_get_p(y), pi = pr_get_gen(y), cx;
1870 413 : if (pr_is_inert(y)) return RgM_Rg_mul(idealhnf_principal(nf,x),p);
1871 :
1872 217 : x = nf_to_scalar_or_basis(nf, x);
1873 217 : switch(typ(x))
1874 : {
1875 203 : case t_INT:
1876 203 : if (!signe(x)) return cgetg(1,t_MAT);
1877 203 : return ZM_Z_mul(pr_hnf(nf,y), absi_shallow(x));
1878 7 : case t_FRAC:
1879 7 : return RgM_Rg_mul(pr_hnf(nf,y), Q_abs_shallow(x));
1880 : }
1881 : /* t_COL */
1882 7 : x = Q_primitive_part(x, &cx);
1883 7 : x = zk_multable(nf, x);
1884 7 : z = shallowconcat(ZM_Z_mul(x,p), ZM_ZC_mul(x,pi));
1885 7 : z = ZM_hnfmodid(z, mulii(p, zkmultable_capZ(x)));
1886 7 : return cx? ZM_Q_mul(z, cx): z;
1887 : }
1888 287056 : default: /* id_MAT */
1889 287056 : return idealmulelt(nf, x,y);
1890 : }
1891 42821 : case id_PRIME:
1892 42821 : if (ty==id_PRIME)
1893 4347 : { y = pr_hnf(nf,y); cy = NULL; }
1894 : else
1895 38474 : y = Q_primitive_part(y, &cy);
1896 42821 : y = idealHNF_mul_two(nf,y,x);
1897 42821 : return cy? ZM_Q_mul(y,cy): y;
1898 :
1899 176962 : default: /* id_MAT */
1900 : {
1901 176962 : long N = nf_get_degree(nf);
1902 176962 : if (lg(x)-1 != N || lg(y)-1 != N) pari_err_DIM("idealmul");
1903 176948 : x = Q_primitive_part(x, &cx);
1904 176948 : y = Q_primitive_part(y, &cy); cx = mul_content(cx,cy);
1905 176948 : y = idealHNF_mul(nf,x,y);
1906 176948 : return cx? ZM_Q_mul(y,cx): y;
1907 : }
1908 : }
1909 : }
1910 :
1911 : /* output the ideal product x.y */
1912 : GEN
1913 512061 : idealmul(GEN nf, GEN x, GEN y)
1914 : {
1915 : pari_sp av;
1916 : GEN res, ax, ay, z;
1917 512061 : long tx = idealtyp(&x,&ax);
1918 512061 : long ty = idealtyp(&y,&ay), f;
1919 512061 : if (tx>ty) { swap(ax,ay); swap(x,y); lswap(tx,ty); }
1920 512061 : f = (ax||ay); res = f? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
1921 512061 : av = avma;
1922 512061 : z = gc_upto(av, idealmul_aux(checknf(nf), x,y, tx,ty));
1923 512047 : if (!f) return z;
1924 28039 : if (ax && ay)
1925 26541 : ax = ext_mul(nf, ax, ay);
1926 : else
1927 1498 : ax = gcopy(ax? ax: ay);
1928 28039 : gel(res,1) = z; gel(res,2) = ax; return res;
1929 : }
1930 :
1931 : /* Return x, integral in 2-elt form, such that pr^2 = c * x. cf idealpowprime
1932 : * nf = true nf */
1933 : static GEN
1934 317550 : idealsqrprime(GEN nf, GEN pr, GEN *pc)
1935 : {
1936 317550 : GEN p = pr_get_p(pr), q, gen;
1937 317550 : long e = pr_get_e(pr), f = pr_get_f(pr);
1938 :
1939 317550 : q = (e == 1)? sqri(p): p;
1940 317550 : if (e <= 2 && e * f == nf_get_degree(nf))
1941 : { /* pr^e = (p) */
1942 45691 : *pc = q;
1943 45691 : return mkvec2(gen_1,gen_0);
1944 : }
1945 271859 : gen = nfsqr(nf, pr_get_gen(pr));
1946 271859 : gen = FpC_red(gen, q);
1947 271859 : *pc = NULL;
1948 271859 : return mkvec2(q, gen);
1949 : }
1950 : /* cf idealpow_aux */
1951 : static GEN
1952 39245 : idealsqr_aux(GEN nf, GEN x, long tx)
1953 : {
1954 39245 : GEN T = nf_get_pol(nf), m, cx, a, alpha;
1955 39245 : long N = degpol(T);
1956 39245 : switch(tx)
1957 : {
1958 385 : case id_PRINCIPAL:
1959 385 : return idealhnf_principal(nf, nfsqr(nf,x));
1960 10785 : case id_PRIME:
1961 10785 : if (pr_is_inert(x)) return scalarmat(sqri(gel(x,1)), N);
1962 10617 : x = idealsqrprime(nf, x, &cx);
1963 10617 : x = idealhnf_two(nf,x);
1964 10617 : return cx? ZM_Z_mul(x, cx): x;
1965 28075 : default:
1966 28075 : x = Q_primitive_part(x, &cx);
1967 28075 : a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
1968 28075 : alpha = nfsqr(nf,alpha);
1969 28075 : m = zk_scalar_or_multable(nf, alpha);
1970 28075 : if (typ(m) == t_INT) {
1971 1642 : x = gcdii(sqri(a), m);
1972 1642 : if (cx) x = gmul(x, gsqr(cx));
1973 1642 : x = scalarmat(x, N);
1974 : }
1975 : else
1976 : { /* could use gcdii(sqri(a), zkmultable_capZ(m)), but costly */
1977 26433 : x = ZM_hnfmodid(m, sqri(a));
1978 26433 : if (cx) cx = gsqr(cx);
1979 26433 : if (cx) x = ZM_Q_mul(x, cx);
1980 : }
1981 28075 : return x;
1982 : }
1983 : }
1984 : GEN
1985 39245 : idealsqr(GEN nf, GEN x)
1986 : {
1987 : pari_sp av;
1988 : GEN res, ax, z;
1989 39245 : long tx = idealtyp(&x,&ax);
1990 39245 : res = ax? cgetg(3,t_VEC): NULL; /*product is an extended ideal*/
1991 39245 : av = avma;
1992 39245 : z = gc_upto(av, idealsqr_aux(checknf(nf), x, tx));
1993 39245 : if (!ax) return z;
1994 33946 : gel(res,1) = z;
1995 33946 : gel(res,2) = ext_sqr(nf, ax); return res;
1996 : }
1997 :
1998 : /* norm of an ideal */
1999 : GEN
2000 106058 : idealnorm(GEN nf, GEN x)
2001 : {
2002 : pari_sp av;
2003 : long tx;
2004 :
2005 106058 : switch(idealtyp(&x, NULL))
2006 : {
2007 4935 : case id_PRIME: return pr_norm(x);
2008 11179 : case id_MAT: return RgM_det_triangular(x);
2009 : }
2010 : /* id_PRINCIPAL */
2011 89944 : nf = checknf(nf); av = avma;
2012 89944 : x = nfnorm(nf, x);
2013 89944 : tx = typ(x);
2014 89944 : if (tx == t_INT) return gc_INT(av, absi(x));
2015 420 : if (tx != t_FRAC) pari_err_TYPE("idealnorm",x);
2016 420 : return gc_upto(av, Q_abs(x));
2017 : }
2018 :
2019 : /* x \cap Z */
2020 : GEN
2021 3031 : idealdown(GEN nf, GEN x)
2022 : {
2023 3031 : pari_sp av = avma;
2024 : GEN y, c;
2025 3031 : switch(idealtyp(&x, NULL))
2026 : {
2027 7 : case id_PRIME: return icopy(pr_get_p(x));
2028 2121 : case id_MAT: return gcopy(gcoeff(x,1,1));
2029 : }
2030 : /* id_PRINCIPAL */
2031 903 : nf = checknf(nf); av = avma;
2032 903 : x = nf_to_scalar_or_basis(nf, x);
2033 903 : if (is_rational_t(typ(x))) return Q_abs(x);
2034 14 : x = Q_primitive_part(x, &c);
2035 14 : y = zkmultable_capZ(zk_multable(nf, x));
2036 14 : return gc_GEN(av, mul_content(c, y));
2037 : }
2038 :
2039 : /* true nf */
2040 : static GEN
2041 42 : idealismaximal_int(GEN nf, GEN p)
2042 : {
2043 : GEN L;
2044 42 : if (!BPSW_psp(p)) return NULL;
2045 77 : if (!dvdii(nf_get_index(nf), p) &&
2046 49 : !FpX_is_irred(FpX_red(nf_get_pol(nf),p), p)) return NULL;
2047 28 : L = idealprimedec(nf, p);
2048 28 : return (lg(L) == 2 && pr_get_e(gel(L,1)) == 1)? gel(L,1): NULL;
2049 : }
2050 : /* true nf */
2051 : static GEN
2052 21 : idealismaximal_mat(GEN nf, GEN x)
2053 : {
2054 : GEN p, c, L;
2055 : long i, l, f;
2056 21 : x = Q_primitive_part(x, &c);
2057 21 : p = gcoeff(x,1,1);
2058 21 : if (c)
2059 : {
2060 7 : if (typ(c) == t_FRAC || !equali1(p)) return NULL;
2061 7 : return idealismaximal_int(nf, c);
2062 : }
2063 14 : if (!BPSW_psp(p)) return NULL;
2064 14 : l = lg(x); f = 1;
2065 35 : for (i = 2; i < l; i++)
2066 : {
2067 21 : c = gcoeff(x,i,i);
2068 21 : if (equalii(c, p)) f++; else if (!equali1(c)) return NULL;
2069 : }
2070 14 : L = idealprimedec_limit_f(nf, p, f);
2071 28 : for (i = lg(L)-1; i; i--)
2072 : {
2073 28 : GEN pr = gel(L,i);
2074 28 : if (pr_get_f(pr) != f) break;
2075 28 : if (idealval(nf, x, pr) == 1) return pr;
2076 : }
2077 0 : return NULL;
2078 : }
2079 : /* true nf */
2080 : static GEN
2081 77 : idealismaximal_i(GEN nf, GEN x)
2082 : {
2083 : GEN L, p, pr, c;
2084 : long i, l;
2085 77 : switch(idealtyp(&x, NULL))
2086 : {
2087 7 : case id_PRIME: return x;
2088 21 : case id_MAT: return idealismaximal_mat(nf, x);
2089 : }
2090 : /* id_PRINCIPAL */
2091 49 : x = nf_to_scalar_or_basis(nf, x);
2092 49 : switch(typ(x))
2093 : {
2094 35 : case t_INT: return idealismaximal_int(nf, absi_shallow(x));
2095 0 : case t_FRAC: return NULL;
2096 : }
2097 14 : x = Q_primitive_part(x, &c);
2098 14 : if (c) return NULL;
2099 14 : p = zkmultable_capZ(zk_multable(nf, x));
2100 14 : if (!BPSW_psp(p)) return NULL;
2101 7 : L = idealprimedec(nf, p); l = lg(L); pr = NULL;
2102 21 : for (i = 1; i < l; i++)
2103 : {
2104 14 : long v = ZC_nfval(x, gel(L,i));
2105 14 : if (v > 1 || (v && pr)) return NULL;
2106 14 : pr = gel(L,i);
2107 : }
2108 7 : return pr;
2109 : }
2110 : GEN
2111 77 : idealismaximal(GEN nf, GEN x)
2112 : {
2113 77 : pari_sp av = avma;
2114 77 : x = idealismaximal_i(checknf(nf), x);
2115 77 : if (!x) { set_avma(av); return gen_0; }
2116 49 : return gc_GEN(av, x);
2117 : }
2118 :
2119 : /* I^(-1) = { x \in K, Tr(x D^(-1) I) \in Z }, D different of K/Q
2120 : *
2121 : * nf[5][6] = pp( D^(-1) ) = pp( HNF( T^(-1) ) ), T = (Tr(wi wj))
2122 : * nf[5][7] = same in 2-elt form.
2123 : * Assume I integral. Return the integral ideal (I\cap Z) I^(-1) */
2124 : GEN
2125 240982 : idealHNF_inv_Z(GEN nf, GEN I)
2126 : {
2127 240982 : GEN J, dual, IZ = gcoeff(I,1,1); /* I \cap Z */
2128 240982 : if (isint1(IZ)) return matid(lg(I)-1);
2129 213292 : J = idealHNF_mul(nf,I, gmael(nf,5,7));
2130 : /* I in HNF, hence easily inverted; multiply by IZ to get integer coeffs
2131 : * missing content cancels while solving the linear equation */
2132 213292 : dual = shallowtrans( hnf_divscale(J, gmael(nf,5,6), IZ) );
2133 213292 : return ZM_hnfmodid(dual, IZ);
2134 : }
2135 : /* I HNF with rational coefficients (denominator d). */
2136 : GEN
2137 98581 : idealHNF_inv(GEN nf, GEN I)
2138 : {
2139 98581 : GEN J, IQ = gcoeff(I,1,1); /* I \cap Q; d IQ = dI \cap Z */
2140 98581 : J = idealHNF_inv_Z(nf, Q_remove_denom(I, NULL)); /* = (dI)^(-1) * (d IQ) */
2141 98581 : return equali1(IQ)? J: RgM_Rg_div(J, IQ);
2142 : }
2143 :
2144 : /* return p * P^(-1) [integral] */
2145 : GEN
2146 38687 : pr_inv_p(GEN pr)
2147 : {
2148 38687 : if (pr_is_inert(pr)) return matid(pr_get_f(pr));
2149 38015 : return ZM_hnfmodid(pr_get_tau(pr), pr_get_p(pr));
2150 : }
2151 : GEN
2152 17867 : pr_inv(GEN pr)
2153 : {
2154 17867 : GEN p = pr_get_p(pr);
2155 17867 : if (pr_is_inert(pr)) return scalarmat(ginv(p), pr_get_f(pr));
2156 17594 : return RgM_Rg_div(ZM_hnfmodid(pr_get_tau(pr),p), p);
2157 : }
2158 :
2159 : GEN
2160 115842 : idealinv(GEN nf, GEN x)
2161 : {
2162 : GEN res, ax;
2163 : pari_sp av;
2164 115842 : long tx = idealtyp(&x,&ax), N;
2165 :
2166 115842 : res = ax? cgetg(3,t_VEC): NULL;
2167 115842 : nf = checknf(nf); av = avma;
2168 115842 : N = nf_get_degree(nf);
2169 115842 : switch (tx)
2170 : {
2171 91681 : case id_MAT:
2172 91681 : if (lg(x)-1 != N) pari_err_DIM("idealinv");
2173 91681 : x = idealHNF_inv(nf,x); break;
2174 7126 : case id_PRINCIPAL:
2175 7126 : x = nf_to_scalar_or_basis(nf, x);
2176 7126 : if (typ(x) != t_COL)
2177 7077 : x = idealhnf_principal(nf,ginv(x));
2178 : else
2179 : { /* nfinv + idealhnf where we already know (x) \cap Z */
2180 : GEN c, d;
2181 49 : x = Q_remove_denom(x, &c);
2182 49 : x = zk_inv(nf, x);
2183 49 : x = Q_remove_denom(x, &d); /* true inverse is c/d * x */
2184 49 : if (!d) /* x and x^(-1) integral => x a unit */
2185 14 : x = c? scalarmat(c, N): matid(N);
2186 : else
2187 : {
2188 35 : c = c? gdiv(c,d): ginv(d);
2189 35 : x = zk_multable(nf, x);
2190 35 : x = ZM_Q_mul(ZM_hnfmodid(x,d), c);
2191 : }
2192 : }
2193 7126 : break;
2194 17035 : case id_PRIME:
2195 17035 : x = pr_inv(x); break;
2196 : }
2197 115842 : x = gc_upto(av,x); if (!ax) return x;
2198 20410 : gel(res,1) = x;
2199 20410 : gel(res,2) = ext_inv(nf, ax); return res;
2200 : }
2201 :
2202 : /* write x = A/B, A,B coprime integral ideals */
2203 : GEN
2204 389759 : idealnumden(GEN nf, GEN x)
2205 : {
2206 389759 : pari_sp av = avma;
2207 : GEN x0, c, d, A, B, J;
2208 389759 : long tx = idealtyp(&x, NULL);
2209 389759 : nf = checknf(nf);
2210 389759 : switch (tx)
2211 : {
2212 7 : case id_PRIME:
2213 7 : retmkvec2(idealhnf(nf, x), gen_1);
2214 148183 : case id_PRINCIPAL:
2215 : {
2216 : GEN xZ, mx;
2217 148183 : x = nf_to_scalar_or_basis(nf, x);
2218 148183 : switch(typ(x))
2219 : {
2220 88060 : case t_INT: return gc_GEN(av, mkvec2(absi_shallow(x),gen_1));
2221 2639 : case t_FRAC:return gc_GEN(av, mkvec2(absi_shallow(gel(x,1)), gel(x,2)));
2222 : }
2223 : /* t_COL */
2224 57484 : x = Q_remove_denom(x, &d);
2225 57484 : if (!d) return gc_GEN(av, mkvec2(idealhnf_shallow(nf, x), gen_1));
2226 105 : mx = zk_multable(nf, x);
2227 105 : xZ = zkmultable_capZ(mx);
2228 105 : x = ZM_hnfmodid(mx, xZ); /* principal ideal (x) */
2229 105 : x0 = mkvec2(xZ, mx); /* same, for fast multiplication */
2230 105 : break;
2231 : }
2232 241569 : default: /* id_MAT */
2233 : {
2234 241569 : long n = lg(x)-1;
2235 241569 : if (n == 0) return mkvec2(gen_0, gen_1);
2236 241569 : if (n != nf_get_degree(nf)) pari_err_DIM("idealnumden");
2237 241569 : x0 = x = Q_remove_denom(x, &d);
2238 241569 : if (!d) return gc_GEN(av, mkvec2(x, gen_1));
2239 21 : break;
2240 : }
2241 : }
2242 126 : J = hnfmodid(x, d); /* = d/B */
2243 126 : c = gcoeff(J,1,1); /* (d/B) \cap Z, divides d */
2244 126 : B = idealHNF_inv_Z(nf, J); /* (d/B \cap Z) B/d */
2245 126 : if (!equalii(c,d)) B = ZM_Z_mul(B, diviiexact(d,c)); /* = B ! */
2246 126 : A = idealHNF_mul(nf, B, x0); /* d * (original x) * B = d A */
2247 126 : A = ZM_Z_divexact(A, d); /* = A ! */
2248 126 : return gc_GEN(av, mkvec2(A, B));
2249 : }
2250 :
2251 : /* Return x, integral in 2-elt form, such that pr^n = c * x. Assume n != 0.
2252 : * nf = true nf */
2253 : static GEN
2254 1309088 : idealpowprime(GEN nf, GEN pr, GEN n, GEN *pc)
2255 : {
2256 1309088 : GEN p = pr_get_p(pr), q, gen;
2257 :
2258 1309088 : *pc = NULL;
2259 1309088 : if (is_pm1(n)) /* n = 1 special cased for efficiency */
2260 : {
2261 636276 : q = p;
2262 636276 : if (typ(pr_get_tau(pr)) == t_INT) /* inert */
2263 : {
2264 0 : *pc = (signe(n) >= 0)? p: ginv(p);
2265 0 : return mkvec2(gen_1,gen_0);
2266 : }
2267 636276 : if (signe(n) >= 0) gen = pr_get_gen(pr);
2268 : else
2269 : {
2270 170826 : gen = pr_get_tau(pr); /* possibly t_MAT */
2271 170826 : *pc = ginv(p);
2272 : }
2273 : }
2274 672812 : else if (equalis(n,2)) return idealsqrprime(nf, pr, pc);
2275 : else
2276 : {
2277 365879 : long e = pr_get_e(pr), f = pr_get_f(pr);
2278 365879 : GEN r, m = truedvmdis(n, e, &r);
2279 365879 : if (e * f == nf_get_degree(nf))
2280 : { /* pr^e = (p) */
2281 76665 : if (signe(m)) *pc = powii(p,m);
2282 76665 : if (!signe(r)) return mkvec2(gen_1,gen_0);
2283 36564 : q = p;
2284 36564 : gen = nfpow(nf, pr_get_gen(pr), r);
2285 : }
2286 : else
2287 : {
2288 289214 : m = absi_shallow(m);
2289 289214 : if (signe(r)) m = addiu(m,1);
2290 289214 : q = powii(p,m); /* m = ceil(|n|/e) */
2291 289214 : if (signe(n) >= 0) gen = nfpow(nf, pr_get_gen(pr), n);
2292 : else
2293 : {
2294 43151 : gen = pr_get_tau(pr);
2295 43151 : if (typ(gen) == t_MAT) gen = gel(gen,1);
2296 43151 : n = negi(n);
2297 43151 : gen = ZC_Z_divexact(nfpow(nf, gen, n), powii(p, subii(n,m)));
2298 43151 : *pc = ginv(q);
2299 : }
2300 : }
2301 325778 : gen = FpC_red(gen, q);
2302 : }
2303 962054 : return mkvec2(q, gen);
2304 : }
2305 :
2306 : /* True nf. x * pr^n. Assume x in HNF or scalar (possibly nonintegral) */
2307 : GEN
2308 828120 : idealmulpowprime(GEN nf, GEN x, GEN pr, GEN n)
2309 : {
2310 : GEN c, cx, y;
2311 828120 : long N = nf_get_degree(nf);
2312 :
2313 828120 : if (!signe(n)) return typ(x) == t_MAT? x: scalarmat_shallow(x, N);
2314 :
2315 : /* inert, special cased for efficiency */
2316 828113 : if (pr_is_inert(pr))
2317 : {
2318 77013 : GEN q = powii(pr_get_p(pr), n);
2319 74955 : return typ(x) == t_MAT? RgM_Rg_mul(x,q)
2320 151968 : : scalarmat_shallow(gmul(Q_abs(x),q), N);
2321 : }
2322 :
2323 751100 : y = idealpowprime(nf, pr, n, &c);
2324 751100 : if (typ(x) == t_MAT)
2325 748757 : { x = Q_primitive_part(x, &cx); if (is_pm1(gcoeff(x,1,1))) x = NULL; }
2326 : else
2327 2343 : { cx = x; x = NULL; }
2328 751100 : cx = mul_content(c,cx);
2329 751100 : if (x)
2330 574416 : x = idealHNF_mul_two(nf,x,y);
2331 : else
2332 176684 : x = idealhnf_two(nf,y);
2333 751100 : if (cx) x = ZM_Q_mul(x,cx);
2334 751100 : return x;
2335 : }
2336 : GEN
2337 13007 : idealdivpowprime(GEN nf, GEN x, GEN pr, GEN n)
2338 : {
2339 13007 : return idealmulpowprime(nf,x,pr, negi(n));
2340 : }
2341 :
2342 : /* nf = true nf */
2343 : static GEN
2344 942009 : idealpow_aux(GEN nf, GEN x, long tx, GEN n)
2345 : {
2346 942009 : GEN T = nf_get_pol(nf), m, cx, n1, a, alpha;
2347 942009 : long N = degpol(T), s = signe(n);
2348 942009 : if (!s) return matid(N);
2349 927016 : switch(tx)
2350 : {
2351 75528 : case id_PRINCIPAL:
2352 75528 : return idealhnf_principal(nf, nfpow(nf,x,n));
2353 656513 : case id_PRIME:
2354 656513 : if (pr_is_inert(x)) return scalarmat(powii(gel(x,1), n), N);
2355 557988 : x = idealpowprime(nf, x, n, &cx);
2356 557988 : x = idealhnf_two(nf,x);
2357 557988 : return cx? ZM_Q_mul(x, cx): x;
2358 194975 : default:
2359 194975 : if (is_pm1(n)) return (s < 0)? idealinv(nf, x): gcopy(x);
2360 69172 : n1 = (s < 0)? negi(n): n;
2361 :
2362 69172 : x = Q_primitive_part(x, &cx);
2363 69172 : a = mat_ideal_two_elt(nf,x); alpha = gel(a,2); a = gel(a,1);
2364 69172 : alpha = nfpow(nf,alpha,n1);
2365 69172 : m = zk_scalar_or_multable(nf, alpha);
2366 69172 : if (typ(m) == t_INT) {
2367 553 : x = gcdii(powii(a,n1), m);
2368 553 : if (s<0) x = ginv(x);
2369 553 : if (cx) x = gmul(x, powgi(cx,n));
2370 553 : x = scalarmat(x, N);
2371 : }
2372 : else
2373 : { /* could use gcdii(powii(a,n1), zkmultable_capZ(m)), but costly */
2374 68619 : x = ZM_hnfmodid(m, powii(a,n1));
2375 68619 : if (cx) cx = powgi(cx,n);
2376 68619 : if (s<0) {
2377 7 : GEN xZ = gcoeff(x,1,1);
2378 7 : cx = cx ? gdiv(cx, xZ): ginv(xZ);
2379 7 : x = idealHNF_inv_Z(nf,x);
2380 : }
2381 68619 : if (cx) x = ZM_Q_mul(x, cx);
2382 : }
2383 69172 : return x;
2384 : }
2385 : }
2386 :
2387 : /* raise the ideal x to the power n (in Z) */
2388 : GEN
2389 942009 : idealpow(GEN nf, GEN x, GEN n)
2390 : {
2391 : pari_sp av;
2392 : long tx;
2393 : GEN res, ax;
2394 :
2395 942009 : if (typ(n) != t_INT) pari_err_TYPE("idealpow",n);
2396 942009 : tx = idealtyp(&x,&ax);
2397 942009 : res = ax? cgetg(3,t_VEC): NULL;
2398 942009 : av = avma;
2399 942009 : x = gc_upto(av, idealpow_aux(checknf(nf), x, tx, n));
2400 942009 : if (!ax) return x;
2401 0 : gel(res,1) = x;
2402 0 : gel(res,2) = ext_pow(nf, ax, n);
2403 0 : return res;
2404 : }
2405 :
2406 : /* Return ideal^e in number field nf. e is a C integer. */
2407 : GEN
2408 313404 : idealpows(GEN nf, GEN ideal, long e)
2409 : {
2410 313404 : long court[] = {evaltyp(t_INT) | _evallg(3),0,0};
2411 313404 : affsi(e,court); return idealpow(nf,ideal,court);
2412 : }
2413 :
2414 : static GEN
2415 28606 : _idealmulred(GEN nf, GEN x, GEN y)
2416 28606 : { return idealred(nf,idealmul(nf,x,y)); }
2417 : static GEN
2418 35759 : _idealsqrred(GEN nf, GEN x)
2419 35759 : { return idealred(nf,idealsqr(nf,x)); }
2420 : static GEN
2421 11385 : _mul(void *data, GEN x, GEN y) { return _idealmulred((GEN)data,x,y); }
2422 : static GEN
2423 35759 : _sqr(void *data, GEN x) { return _idealsqrred((GEN)data, x); }
2424 :
2425 : /* compute x^n (x ideal, n integer), reducing along the way */
2426 : GEN
2427 80388 : idealpowred(GEN nf, GEN x, GEN n)
2428 : {
2429 80388 : pari_sp av = avma, av2;
2430 : long s;
2431 : GEN y;
2432 :
2433 80388 : if (typ(n) != t_INT) pari_err_TYPE("idealpowred",n);
2434 80388 : s = signe(n); if (s == 0) return idealpow(nf,x,n);
2435 80388 : y = gen_pow_i(x, n, (void*)nf, &_sqr, &_mul);
2436 80388 : av2 = avma;
2437 80388 : if (s < 0) y = idealinv(nf,y);
2438 80388 : if (s < 0 || is_pm1(n)) y = idealred(nf,y);
2439 80388 : return avma == av2? gc_GEN(av,y): gc_upto(av,y);
2440 : }
2441 :
2442 : GEN
2443 17221 : idealmulred(GEN nf, GEN x, GEN y)
2444 : {
2445 17221 : pari_sp av = avma;
2446 17221 : return gc_upto(av, _idealmulred(nf,x,y));
2447 : }
2448 :
2449 : long
2450 91 : isideal(GEN nf,GEN x)
2451 : {
2452 91 : long N, i, j, lx, tx = typ(x);
2453 : pari_sp av;
2454 : GEN T, xZ;
2455 :
2456 91 : nf = checknf(nf); T = nf_get_pol(nf); lx = lg(x);
2457 91 : if (tx==t_VEC && lx==3) { x = gel(x,1); tx = typ(x); lx = lg(x); }
2458 91 : switch(tx)
2459 : {
2460 14 : case t_INT: case t_FRAC: return 1;
2461 7 : case t_POL: return varn(x) == varn(T);
2462 7 : case t_POLMOD: return RgX_equal_var(T, gel(x,1));
2463 14 : case t_VEC: return get_prid(x)? 1 : 0;
2464 42 : case t_MAT: break;
2465 7 : default: return 0;
2466 : }
2467 42 : N = degpol(T);
2468 42 : if (lx-1 != N) return (lx == 1);
2469 28 : if (nbrows(x) != N) return 0;
2470 :
2471 28 : av = avma; x = Q_primpart(x);
2472 28 : if (!ZM_ishnf(x)) return 0;
2473 14 : xZ = gcoeff(x,1,1);
2474 21 : for (j=2; j<=N; j++)
2475 14 : if (!dvdii(xZ, gcoeff(x,j,j))) return gc_long(av,0);
2476 14 : for (i=2; i<=N; i++)
2477 14 : for (j=2; j<=N; j++)
2478 7 : if (! hnf_invimage(x, zk_ei_mul(nf,gel(x,i),j))) return gc_long(av,0);
2479 7 : return gc_long(av,1);
2480 : }
2481 :
2482 : GEN
2483 39744 : idealdiv(GEN nf, GEN x, GEN y)
2484 : {
2485 39744 : pari_sp av = avma;
2486 39744 : return gc_upto(av, idealmul(nf, x, idealinv(nf,y)));
2487 : }
2488 :
2489 : /* This routine computes the quotient x/y of two ideals in the number field nf.
2490 : * It assumes that the quotient is an integral ideal. The idea is to find an
2491 : * ideal z dividing y such that gcd(Nx/Nz, Nz) = 1. Then
2492 : *
2493 : * x + (Nx/Nz) x
2494 : * ----------- = ---
2495 : * y + (Ny/Nz) y
2496 : *
2497 : * Proof: we can assume x and y are integral. Let p be any prime ideal
2498 : *
2499 : * If p | Nz, then it divides neither Nx/Nz nor Ny/Nz (since Nx/Nz is the
2500 : * product of the integers N(x/y) and N(y/z)). Both the numerator and the
2501 : * denominator on the left will be coprime to p. So will x/y, since x/y is
2502 : * assumed integral and its norm N(x/y) is coprime to p.
2503 : *
2504 : * If instead p does not divide Nz, then v_p (Nx/Nz) = v_p (Nx) >= v_p(x).
2505 : * Hence v_p (x + Nx/Nz) = v_p(x). Likewise for the denominators. QED.
2506 : *
2507 : * Peter Montgomery. July, 1994. */
2508 : static void
2509 7 : err_divexact(GEN x, GEN y)
2510 7 : { pari_err_DOMAIN("idealdivexact","denominator(x/y)", "!=",
2511 0 : gen_1,mkvec2(x,y)); }
2512 : GEN
2513 5263 : idealdivexact(GEN nf, GEN x, GEN y0)
2514 : {
2515 5263 : pari_sp av = avma;
2516 5263 : GEN y = y0, xZ, yZ, Nx, Ny, Nz, cy, q, r;
2517 :
2518 5263 : nf = checknf(nf);
2519 5263 : idealtyp(&x, NULL); if (typ(x) != t_MAT) x = idealhnf_shallow(nf, x);
2520 5263 : idealtyp(&y, NULL); if (typ(y) != t_MAT) y = idealhnf_shallow(nf, y);
2521 5263 : if (lg(y) == 1) pari_err_INV("idealdivexact", y0);
2522 5256 : if (lg(x) == 1) retgc_const(av, cgetg(1, t_MAT)); /* numerator is zero */
2523 5256 : y = Q_primitive_part(y, &cy);
2524 5256 : if (cy) x = RgM_Rg_div(x,cy);
2525 5256 : xZ = gcoeff(x,1,1); if (typ(xZ) != t_INT) err_divexact(x,y);
2526 5249 : yZ = gcoeff(y,1,1); if (isint1(yZ)) return gc_GEN(av, x);
2527 2870 : Nx = idealnorm(nf,x);
2528 2870 : Ny = idealnorm(nf,y);
2529 2870 : if (typ(Nx) != t_INT) err_divexact(x,y);
2530 2870 : q = dvmdii(Nx,Ny, &r);
2531 2870 : if (signe(r)) err_divexact(x,y);
2532 2870 : if (is_pm1(q)) { set_avma(av); return matid(nf_get_degree(nf)); }
2533 : /* Find a norm Nz | Ny such that gcd(Nx/Nz, Nz) = 1 */
2534 616 : for (Nz = Ny;;) /* q = Nx/Nz */
2535 533 : {
2536 1149 : GEN p1 = gcdii(Nz, q);
2537 1149 : if (is_pm1(p1)) break;
2538 533 : Nz = diviiexact(Nz,p1);
2539 533 : q = mulii(q,p1);
2540 : }
2541 616 : xZ = gcoeff(x,1,1); q = gcdii(q, xZ);
2542 616 : if (!equalii(xZ,q))
2543 : { /* Replace x/y by x+(Nx/Nz) / y+(Ny/Nz) */
2544 468 : x = ZM_hnfmodid(x, q);
2545 : /* y reduced to unit ideal ? */
2546 468 : if (Nz == Ny) return gc_upto(av, x);
2547 :
2548 146 : yZ = gcoeff(y,1,1); q = gcdii(diviiexact(Ny,Nz), yZ);
2549 146 : y = ZM_hnfmodid(y, q);
2550 : }
2551 294 : yZ = gcoeff(y,1,1);
2552 294 : y = idealHNF_mul(nf,x, idealHNF_inv_Z(nf,y));
2553 294 : return gc_upto(av, ZM_Z_divexact(y, yZ));
2554 : }
2555 :
2556 : GEN
2557 21 : idealintersect(GEN nf, GEN x, GEN y)
2558 : {
2559 21 : pari_sp av = avma;
2560 : GEN z, dx, dy;
2561 :
2562 21 : nf = checknf(nf);
2563 21 : idealtyp(&x, NULL); if (typ(x) != t_MAT) x = idealhnf_shallow(nf,x);
2564 21 : idealtyp(&y, NULL); if (typ(y) != t_MAT) y = idealhnf_shallow(nf,y);
2565 21 : if (lg(x) == 1 || lg(y) == 1) retgc_const(av, cgetg(1, t_MAT));
2566 14 : x = Q_remove_denom(x, &dx);
2567 14 : y = Q_remove_denom(y, &dy);
2568 14 : if (dx) y = ZM_Z_mul(y, dx);
2569 14 : if (dy) x = ZM_Z_mul(x, dy);
2570 14 : dx = mul_denom(dx,dy);
2571 14 : z = ZM_hnfintersectmod(x,y, lcmii(gcoeff(x,1,1), gcoeff(y,1,1)));
2572 14 : if (dx) z = RgM_Rg_div(z,dx);
2573 14 : return gc_upto(av,z);
2574 : }
2575 :
2576 : /*******************************************************************/
2577 : /* */
2578 : /* T2-IDEAL REDUCTION */
2579 : /* */
2580 : /*******************************************************************/
2581 :
2582 : static GEN
2583 21 : chk_vdir(GEN nf, GEN vdir)
2584 : {
2585 21 : long i, l = lg(vdir);
2586 : GEN v;
2587 21 : if (l != lg(nf_get_roots(nf))) pari_err_DIM("idealred");
2588 14 : switch(typ(vdir))
2589 : {
2590 0 : case t_VECSMALL: return vdir;
2591 14 : case t_VEC: break;
2592 0 : default: pari_err_TYPE("idealred",vdir);
2593 : }
2594 14 : v = cgetg(l, t_VECSMALL);
2595 56 : for (i = 1; i < l; i++) v[i] = itos(gceil(gel(vdir,i)));
2596 14 : return v;
2597 : }
2598 :
2599 : static void
2600 12709 : twistG(GEN G, long r1, long i, long v)
2601 : {
2602 12709 : long j, lG = lg(G);
2603 12709 : if (i <= r1) {
2604 37275 : for (j=1; j<lG; j++) gcoeff(G,i,j) = gmul2n(gcoeff(G,i,j), v);
2605 : } else {
2606 648 : long k = (i<<1) - r1;
2607 4640 : for (j=1; j<lG; j++)
2608 : {
2609 3992 : gcoeff(G,k-1,j) = gmul2n(gcoeff(G,k-1,j), v);
2610 3992 : gcoeff(G,k ,j) = gmul2n(gcoeff(G,k ,j), v);
2611 : }
2612 : }
2613 12709 : }
2614 :
2615 : GEN
2616 139190 : nf_get_Gtwist(GEN nf, GEN vdir)
2617 : {
2618 : long i, l, v, r1;
2619 : GEN G;
2620 :
2621 139190 : if (!vdir) return nf_get_roundG(nf);
2622 21 : if (typ(vdir) == t_MAT)
2623 : {
2624 0 : long N = nf_get_degree(nf);
2625 0 : if (lg(vdir) != N+1 || lgcols(vdir) != N+1) pari_err_DIM("idealred");
2626 0 : return vdir;
2627 : }
2628 21 : vdir = chk_vdir(nf, vdir);
2629 14 : G = RgM_shallowcopy(nf_get_G(nf));
2630 14 : r1 = nf_get_r1(nf);
2631 14 : l = lg(vdir);
2632 56 : for (i=1; i<l; i++)
2633 : {
2634 42 : v = vdir[i]; if (!v) continue;
2635 42 : twistG(G, r1, i, v);
2636 : }
2637 14 : return RM_round_maxrank(G);
2638 : }
2639 : GEN
2640 12667 : nf_get_Gtwist1(GEN nf, long i)
2641 : {
2642 12667 : GEN G = RgM_shallowcopy( nf_get_G(nf) );
2643 12667 : long r1 = nf_get_r1(nf);
2644 12667 : twistG(G, r1, i, 10);
2645 12667 : return RM_round_maxrank(G);
2646 : }
2647 :
2648 : GEN
2649 98530 : RM_round_maxrank(GEN G0)
2650 : {
2651 98530 : long e, r = lg(G0)-1;
2652 98530 : pari_sp av = avma;
2653 98530 : for (e = 4; ; e <<= 1, set_avma(av))
2654 0 : {
2655 98530 : GEN G = gmul2n(G0, e), H = ground(G);
2656 98530 : if (ZM_rank(H) == r) return H; /* maximal rank ? */
2657 : }
2658 : }
2659 :
2660 : GEN
2661 139183 : idealred0(GEN nf, GEN I, GEN vdir)
2662 : {
2663 139183 : pari_sp av = avma;
2664 139183 : GEN G, aI, IZ, J, y, my, dyi, yi, c1 = NULL;
2665 : long N;
2666 :
2667 139183 : nf = checknf(nf);
2668 139183 : N = nf_get_degree(nf);
2669 : /* put first for sanity checks, unused when I obviously principal */
2670 139183 : G = nf_get_Gtwist(nf, vdir);
2671 139176 : switch (idealtyp(&I,&aI))
2672 : {
2673 37341 : case id_PRIME:
2674 37341 : if (pr_is_inert(I)) {
2675 585 : if (!aI) { set_avma(av); return matid(N); }
2676 585 : c1 = gel(I,1); I = matid(N);
2677 585 : goto END;
2678 : }
2679 36756 : IZ = pr_get_p(I);
2680 36756 : J = pr_inv_p(I);
2681 36756 : I = idealhnf_two(nf,I);
2682 36756 : break;
2683 101807 : case id_MAT:
2684 101807 : if (lg(I)-1 != N) pari_err_DIM("idealred");
2685 101800 : I = Q_primitive_part(I, &c1);
2686 101800 : IZ = gcoeff(I,1,1);
2687 101800 : if (is_pm1(IZ))
2688 : {
2689 9083 : if (!aI) { set_avma(av); return matid(N); }
2690 8999 : goto END;
2691 : }
2692 92717 : J = idealHNF_inv_Z(nf, I);
2693 92717 : break;
2694 21 : default: /* id_PRINCIPAL, silly case */
2695 21 : if (gequal0(I)) I = cgetg(1,t_MAT); else { c1 = I; I = matid(N); }
2696 21 : if (!aI) return I;
2697 14 : goto END;
2698 : }
2699 : /* now I integral, HNF; and J = (I\cap Z) I^(-1), integral */
2700 129473 : y = idealpseudomin(J, G); /* small elt in (I\cap Z)I^(-1), integral */
2701 129473 : if (ZV_isscalar(y))
2702 : { /* already reduced */
2703 71090 : if (!aI) return gc_GEN(av, I);
2704 67911 : goto END;
2705 : }
2706 :
2707 58383 : my = zk_multable(nf, y);
2708 58383 : I = ZM_Z_divexact(ZM_mul(my, I), IZ); /* y I / (I\cap Z), integral */
2709 58383 : c1 = mul_content(c1, IZ);
2710 58383 : if (equali1(c1)) c1 = NULL; /* can be simplified with IZ */
2711 58383 : yi = ZM_gauss(my, col_ei(N,1)); /* y^-1 */
2712 58383 : dyi = Q_denom(yi); /* generates (y) \cap Z */
2713 58383 : I = hnfmodid(I, dyi);
2714 58383 : if (!aI) return gc_upto(av, I);
2715 56389 : if (typ(aI) == t_MAT)
2716 : {
2717 39309 : GEN nyi = Q_muli_to_int(yi, dyi);
2718 39309 : if (gexpo(nyi) >= gexpo(y))
2719 20829 : aI = famat_div(aI, y); /* yi "larger" than y, keep the latter */
2720 : else
2721 : { /* use yi */
2722 18480 : aI = famat_mul(aI, nyi);
2723 18480 : c1 = div_content(c1, dyi);
2724 : }
2725 39309 : if (c1) { aI = famat_mul(aI, Q_to_famat(c1)); c1 = NULL; }
2726 : }
2727 : else
2728 17080 : c1 = c1? RgC_Rg_mul(yi, c1): yi;
2729 133898 : END:
2730 133898 : if (c1) aI = ext_mul(nf, aI,c1);
2731 133898 : return gc_GEN(av, mkvec2(I, aI));
2732 : }
2733 :
2734 : /* I integral ZM (not HNF), G ZM, rounded Cholesky form of a weighted
2735 : * T2 matrix. Reduce I wrt G */
2736 : GEN
2737 1346028 : idealpseudored(GEN I, GEN G)
2738 1346028 : { return ZM_mul(I, ZM_lll(ZM_mul(G, I), 0.99, LLL_IM)); }
2739 :
2740 : /* Same I, G; m in I with T2(m) small */
2741 : GEN
2742 142669 : idealpseudomin(GEN I, GEN G)
2743 : {
2744 142669 : GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
2745 142669 : return ZM_ZC_mul(I, gel(u,1));
2746 : }
2747 : /* Same I,G; irrational m in I with T2(m) small */
2748 : GEN
2749 0 : idealpseudomin_nonscalar(GEN I, GEN G)
2750 : {
2751 0 : GEN u = ZM_lll(ZM_mul(G, I), 0.99, LLL_IM);
2752 0 : GEN m = ZM_ZC_mul(I, gel(u,1));
2753 0 : if (ZV_isscalar(m) && lg(u) > 2) m = ZM_ZC_mul(I, gel(u,2));
2754 0 : return m;
2755 : }
2756 : /* Same I,G; t_VEC of irrational m in I with T2(m) small */
2757 : GEN
2758 1254116 : idealpseudominvec(GEN I, GEN G)
2759 : {
2760 1254116 : long i, j, k, n = lg(I)-1;
2761 1254116 : GEN x, L, b = idealpseudored(I, G);
2762 1254116 : L = cgetg(1 + (n*(n+1))/2, t_VEC);
2763 4418496 : for (i = k = 1; i <= n; i++)
2764 : {
2765 3164380 : x = gel(b,i);
2766 3164380 : if (!ZV_isscalar(x)) gel(L,k++) = x;
2767 : }
2768 3164380 : for (i = 2; i <= n; i++)
2769 : {
2770 1910264 : long J = minss(i, 4);
2771 4733595 : for (j = 1; j < J; j++)
2772 : {
2773 2823331 : x = ZC_add(gel(b,i),gel(b,j));
2774 2823331 : if (!ZV_isscalar(x)) gel(L,k++) = x;
2775 : }
2776 : }
2777 1254116 : setlg(L,k); return L;
2778 : }
2779 :
2780 : GEN
2781 13189 : idealred_elt(GEN nf, GEN I)
2782 : {
2783 13189 : pari_sp av = avma;
2784 13189 : GEN u = idealpseudomin(I, nf_get_roundG(nf));
2785 13189 : return gc_upto(av, u);
2786 : }
2787 :
2788 : GEN
2789 7 : idealmin(GEN nf, GEN x, GEN vdir)
2790 : {
2791 7 : pari_sp av = avma;
2792 : GEN y, dx;
2793 7 : nf = checknf(nf);
2794 7 : switch( idealtyp(&x, NULL) )
2795 : {
2796 0 : case id_PRINCIPAL: return gcopy(x);
2797 0 : case id_PRIME: x = pr_hnf(nf,x); break;
2798 7 : case id_MAT: if (lg(x) == 1) return gen_0;
2799 : }
2800 7 : x = Q_remove_denom(x, &dx);
2801 7 : y = idealpseudomin(x, nf_get_Gtwist(nf,vdir));
2802 7 : if (dx) y = RgC_Rg_div(y, dx);
2803 7 : return gc_upto(av, y);
2804 : }
2805 :
2806 : /*******************************************************************/
2807 : /* */
2808 : /* APPROXIMATION THEOREM */
2809 : /* */
2810 : /*******************************************************************/
2811 : /* a = ppi(a,b) ppo(a,b), where ppi regroups primes common to a and b
2812 : * and ppo(a,b) = Z_ppo(a,b) */
2813 : /* return gcd(a,b),ppi(a,b),ppo(a,b) */
2814 : GEN
2815 986167 : Z_ppio(GEN a, GEN b)
2816 : {
2817 986167 : GEN x, y, d = gcdii(a,b);
2818 986167 : if (is_pm1(d)) return mkvec3(gen_1, gen_1, a);
2819 757533 : x = d; y = diviiexact(a,d);
2820 : for(;;)
2821 131117 : {
2822 888650 : GEN g = gcdii(x,y);
2823 888650 : if (is_pm1(g)) return mkvec3(d, x, y);
2824 131117 : x = mulii(x,g); y = diviiexact(y,g);
2825 : }
2826 : }
2827 : /* a = ppg(a,b)pple(a,b), where ppg regroups primes such that v(a) > v(b)
2828 : * and pple all others */
2829 : /* return gcd(a,b),ppg(a,b),pple(a,b) */
2830 : GEN
2831 0 : Z_ppgle(GEN a, GEN b)
2832 : {
2833 0 : GEN x, y, g, d = gcdii(a,b);
2834 0 : if (equalii(a, d)) return mkvec3(a, gen_1, a);
2835 0 : x = diviiexact(a,d); y = d;
2836 : for(;;)
2837 : {
2838 0 : g = gcdii(x,y);
2839 0 : if (is_pm1(g)) return mkvec3(d, x, y);
2840 0 : x = mulii(x,g); y = diviiexact(y,g);
2841 : }
2842 : }
2843 : static void
2844 0 : Z_dcba_rec(GEN L, GEN a, GEN b)
2845 : {
2846 : GEN x, r, v, g, h, c, c0;
2847 : long n;
2848 0 : if (is_pm1(b)) {
2849 0 : if (!is_pm1(a)) vectrunc_append(L, a);
2850 0 : return;
2851 : }
2852 0 : v = Z_ppio(a,b);
2853 0 : a = gel(v,2);
2854 0 : r = gel(v,3);
2855 0 : if (!is_pm1(r)) vectrunc_append(L, r);
2856 0 : v = Z_ppgle(a,b);
2857 0 : g = gel(v,1);
2858 0 : h = gel(v,2);
2859 0 : x = c0 = gel(v,3);
2860 0 : for (n = 1; !is_pm1(h); n++)
2861 : {
2862 : GEN d, y;
2863 : long i;
2864 0 : v = Z_ppgle(h,sqri(g));
2865 0 : g = gel(v,1);
2866 0 : h = gel(v,2);
2867 0 : c = gel(v,3); if (is_pm1(c)) continue;
2868 0 : d = gcdii(c,b);
2869 0 : x = mulii(x,d);
2870 0 : y = d; for (i=1; i < n; i++) y = sqri(y);
2871 0 : Z_dcba_rec(L, diviiexact(c,y), d);
2872 : }
2873 0 : Z_dcba_rec(L,diviiexact(b,x), c0);
2874 : }
2875 : static GEN
2876 6828640 : Z_cba_rec(GEN L, GEN a, GEN b)
2877 : {
2878 : GEN g;
2879 : /* a few naive steps before switching to dcba */
2880 6828640 : if (lg(L) > 10) { Z_dcba_rec(L, a, b); return veclast(L); }
2881 6828640 : if (is_pm1(a)) return b;
2882 4062408 : g = gcdii(a,b);
2883 4062408 : if (is_pm1(g)) { vectrunc_append(L, a); return b; }
2884 3035634 : a = diviiexact(a,g);
2885 3035634 : b = diviiexact(b,g);
2886 3035634 : return Z_cba_rec(L, Z_cba_rec(L, a, g), b);
2887 : }
2888 : GEN
2889 757372 : Z_cba(GEN a, GEN b)
2890 : {
2891 757372 : GEN L = vectrunc_init(expi(a) + expi(b) + 2);
2892 757372 : GEN t = Z_cba_rec(L, a, b);
2893 757372 : if (!is_pm1(t)) vectrunc_append(L, t);
2894 757372 : return L;
2895 : }
2896 : /* P = coprime base, extend it by b; TODO: quadratic for now */
2897 : GEN
2898 49 : ZV_cba_extend(GEN P, GEN b)
2899 : {
2900 49 : long i, l = lg(P);
2901 49 : GEN w = cgetg(l+1, t_VEC);
2902 175 : for (i = 1; i < l; i++)
2903 : {
2904 126 : GEN v = Z_cba(gel(P,i), b);
2905 126 : long nv = lg(v)-1;
2906 126 : gel(w,i) = vecslice(v, 1, nv-1); /* those divide P[i] but not b */
2907 126 : b = gel(v,nv);
2908 : }
2909 49 : gel(w,l) = b; return shallowconcat1(w);
2910 : }
2911 : GEN
2912 28 : ZV_cba(GEN v)
2913 : {
2914 28 : long i, l = lg(v);
2915 : GEN P;
2916 28 : if (l <= 2) return v;
2917 14 : P = Z_cba(gel(v,1), gel(v,2));
2918 42 : for (i = 3; i < l; i++) P = ZV_cba_extend(P, gel(v,i));
2919 14 : return P;
2920 : }
2921 :
2922 : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
2923 : GEN
2924 19281699 : Z_ppo(GEN x, GEN f)
2925 : {
2926 19281699 : (void)Z_pvalrem(x, f, &x);
2927 : for (;;)
2928 : {
2929 58208537 : f = gcdii(x, f); if (is_pm1(f)) break;
2930 38926838 : x = diviiexact(x, f);
2931 : }
2932 19281699 : return x;
2933 : }
2934 : /* write x = x1 x2, x2 maximal s.t. (x2,f) = 1, return x2 */
2935 : ulong
2936 70531620 : u_ppo(ulong x, ulong f)
2937 : {
2938 : for (;;)
2939 : {
2940 70531620 : f = ugcd(x, f); if (f == 1) break;
2941 16213494 : x /= f;
2942 : }
2943 54318126 : return x;
2944 : }
2945 :
2946 : /* result known to be representable as an ulong */
2947 : static ulong
2948 1656157 : lcmuu(ulong a, ulong b) { ulong d = ugcd(a,b); return (a/d) * b; }
2949 :
2950 : /* assume 0 < x < N; return u in (Z/NZ)^* such that u x = gcd(x,N) (mod N);
2951 : * set *pd = gcd(x,N) */
2952 : ulong
2953 5934335 : Fl_invgen(ulong x, ulong N, ulong *pd)
2954 : {
2955 : ulong d, d0, e, v, v1;
2956 : long s;
2957 5934335 : *pd = d = xgcduu(N, x, 0, &v, &v1, &s);
2958 5934335 : if (s > 0) v = N - v;
2959 5934335 : if (d == 1) return v;
2960 : /* vx = gcd(x,N) (mod N), v coprime to N/d but need not be coprime to N */
2961 2770772 : e = N / d;
2962 2770772 : d0 = u_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, rad(d1) | N/d */
2963 2770772 : if (d0 == 1) return v;
2964 1656157 : e = lcmuu(e, d / d0);
2965 1656157 : return u_chinese_coprime(v, 1, e, d0, e*d0);
2966 : }
2967 :
2968 : /* x t_INT, f ideal. Write x = x1 x2, sqf(x1) | f, (x2,f) = 1. Return x2 */
2969 : static GEN
2970 126 : nf_coprime_part(GEN nf, GEN x, GEN listpr)
2971 : {
2972 126 : long v, j, lp = lg(listpr), N = nf_get_degree(nf);
2973 : GEN x1, x2, ex;
2974 :
2975 : #if 0 /*1) via many gcds. Expensive ! */
2976 : GEN f = idealprodprime(nf, listpr);
2977 : f = ZM_hnfmodid(f, x); /* first gcd is less expensive since x in Z */
2978 : x = scalarmat(x, N);
2979 : for (;;)
2980 : {
2981 : if (gequal1(gcoeff(f,1,1))) break;
2982 : x = idealdivexact(nf, x, f);
2983 : f = ZM_hnfmodid(shallowconcat(f,x), gcoeff(x,1,1)); /* gcd(f,x) */
2984 : }
2985 : x2 = x;
2986 : #else /*2) from prime decomposition */
2987 126 : x1 = NULL;
2988 350 : for (j=1; j<lp; j++)
2989 : {
2990 224 : GEN pr = gel(listpr,j);
2991 224 : v = Z_pval(x, pr_get_p(pr)); if (!v) continue;
2992 :
2993 126 : ex = muluu(v, pr_get_e(pr)); /* = v_pr(x) > 0 */
2994 126 : x1 = x1? idealmulpowprime(nf, x1, pr, ex)
2995 126 : : idealpow(nf, pr, ex);
2996 : }
2997 126 : x = scalarmat(x, N);
2998 126 : x2 = x1? idealdivexact(nf, x, x1): x;
2999 : #endif
3000 126 : return x2;
3001 : }
3002 :
3003 : /* L0 in K^*, assume (L0,f) = 1. Return L integral, L0 = L mod f */
3004 : GEN
3005 10920 : make_integral(GEN nf, GEN L0, GEN f, GEN listpr)
3006 : {
3007 : GEN fZ, t, L, D2, d1, d2, d;
3008 :
3009 10920 : L = Q_remove_denom(L0, &d);
3010 10920 : if (!d) return L0;
3011 :
3012 : /* L0 = L / d, L integral */
3013 518 : fZ = gcoeff(f,1,1);
3014 518 : if (typ(L) == t_INT) return Fp_mul(L, Fp_inv(d, fZ), fZ);
3015 : /* Kill denom part coprime to fZ */
3016 126 : d2 = Z_ppo(d, fZ);
3017 126 : t = Fp_inv(d2, fZ); if (!is_pm1(t)) L = ZC_Z_mul(L,t);
3018 126 : if (equalii(d, d2)) return L;
3019 :
3020 126 : d1 = diviiexact(d, d2);
3021 : /* L0 = (L / d1) mod f. d1 not coprime to f
3022 : * write (d1) = D1 D2, D2 minimal, (D2,f) = 1. */
3023 126 : D2 = nf_coprime_part(nf, d1, listpr);
3024 126 : t = idealaddtoone_i(nf, D2, f); /* in D2, 1 mod f */
3025 126 : L = nfmuli(nf,t,L);
3026 :
3027 : /* if (L0, f) = 1, then L in D1 ==> in D1 D2 = (d1) */
3028 126 : return Q_div_to_int(L, d1); /* exact division */
3029 : }
3030 :
3031 : /* assume L is a list of prime ideals. Return the product */
3032 : GEN
3033 666 : idealprodprime(GEN nf, GEN L)
3034 : {
3035 666 : long l = lg(L), i;
3036 : GEN z;
3037 666 : if (l == 1) return matid(nf_get_degree(nf));
3038 666 : z = pr_hnf(nf, gel(L,1));
3039 694 : for (i=2; i<l; i++) z = idealHNF_mul_two(nf,z, gel(L,i));
3040 666 : return z;
3041 : }
3042 :
3043 : /* optimize for the frequent case I = nfhnf()[2]: lots of them are 1 */
3044 : GEN
3045 1064 : idealprod(GEN nf, GEN I)
3046 : {
3047 1064 : long i, l = lg(I);
3048 : GEN z;
3049 2450 : for (i = 1; i < l; i++)
3050 2443 : if (!equali1(gel(I,i))) break;
3051 1064 : if (i == l) return gen_1;
3052 1057 : z = gel(I,i);
3053 1855 : for (i++; i<l; i++) z = idealmul(nf, z, gel(I,i));
3054 1057 : return z;
3055 : }
3056 :
3057 : /* v_pr(idealprod(nf,I)) */
3058 : long
3059 1917 : idealprodval(GEN nf, GEN I, GEN pr)
3060 : {
3061 1917 : long i, l = lg(I), v = 0;
3062 10371 : for (i = 1; i < l; i++)
3063 8454 : if (!equali1(gel(I,i))) v += idealval(nf, gel(I,i), pr);
3064 1917 : return v;
3065 : }
3066 :
3067 : /* assume L is a list of prime ideals. Return prod L[i]^e[i] */
3068 : GEN
3069 75618 : factorbackprime(GEN nf, GEN L, GEN e)
3070 : {
3071 75618 : long l = lg(L), i;
3072 : GEN z;
3073 :
3074 75618 : if (l == 1) return matid(nf_get_degree(nf));
3075 60995 : z = idealpow(nf, gel(L,1), gel(e,1));
3076 113923 : for (i=2; i<l; i++)
3077 52928 : if (signe(gel(e,i))) z = idealmulpowprime(nf,z, gel(L,i),gel(e,i));
3078 60995 : return z;
3079 : }
3080 :
3081 : /* F in Z, divisible exactly by pr.p. Return F-uniformizer for pr, i.e.
3082 : * a t in Z_K such that v_pr(t) = 1 and (t, F/pr) = 1 */
3083 : GEN
3084 58434 : pr_uniformizer(GEN pr, GEN F)
3085 : {
3086 58434 : GEN p = pr_get_p(pr), t = pr_get_gen(pr);
3087 58434 : if (!equalii(F, p))
3088 : {
3089 36883 : long e = pr_get_e(pr);
3090 36883 : GEN u, v, q = (e == 1)? sqri(p): p;
3091 36883 : u = mulii(q, Fp_inv(q, diviiexact(F,p))); /* 1 mod F/p, 0 mod q */
3092 36883 : v = subui(1UL, u); /* 0 mod F/p, 1 mod q */
3093 36883 : if (pr_is_inert(pr))
3094 28 : t = addii(mulii(p, v), u);
3095 : else
3096 : {
3097 36855 : t = ZC_Z_mul(t, v);
3098 36855 : gel(t,1) = addii(gel(t,1), u); /* return u + vt */
3099 : }
3100 : }
3101 58434 : return t;
3102 : }
3103 : /* L = list of prime ideals, return lcm_i (L[i] \cap \ZM) */
3104 : GEN
3105 81194 : prV_lcm_capZ(GEN L)
3106 : {
3107 81194 : long i, r = lg(L);
3108 : GEN F;
3109 81194 : if (r == 1) return gen_1;
3110 68460 : F = pr_get_p(gel(L,1));
3111 121852 : for (i = 2; i < r; i++)
3112 : {
3113 53392 : GEN pr = gel(L,i), p = pr_get_p(pr);
3114 53392 : if (!dvdii(F, p)) F = mulii(F,p);
3115 : }
3116 68460 : return F;
3117 : }
3118 : /* v vector of prid. Return underlying list of rational primes */
3119 : GEN
3120 66234 : prV_primes(GEN v)
3121 : {
3122 66234 : long i, l = lg(v);
3123 66234 : GEN w = cgetg(l,t_VEC);
3124 219768 : for (i=1; i<l; i++) gel(w,i) = pr_get_p(gel(v,i));
3125 66234 : return ZV_sort_uniq(w);
3126 : }
3127 :
3128 : /* Given a prime ideal factorization with possibly zero or negative
3129 : * exponents, gives b such that v_p(b) = v_p(x) for all prime ideals pr | x
3130 : * and v_pr(b) >= 0 for all other pr.
3131 : * For optimal performance, all [anti-]uniformizers should be precomputed,
3132 : * but no support for this yet. If nored, do not reduce result. */
3133 : static GEN
3134 54089 : idealapprfact_i(GEN nf, GEN x, int nored)
3135 : {
3136 54089 : GEN d = NULL, z, L, e, e2, F;
3137 : long i, r;
3138 54089 : int hasden = 0;
3139 :
3140 54089 : nf = checknf(nf);
3141 54089 : L = gel(x,1);
3142 54089 : e = gel(x,2);
3143 54089 : F = prV_lcm_capZ(L);
3144 54089 : z = NULL; r = lg(e);
3145 136923 : for (i = 1; i < r; i++)
3146 : {
3147 82834 : long s = signe(gel(e,i));
3148 : GEN pi, q;
3149 82834 : if (!s) continue;
3150 54066 : if (s < 0) hasden = 1;
3151 54066 : pi = pr_uniformizer(gel(L,i), F);
3152 54066 : q = nfpow(nf, pi, gel(e,i));
3153 54066 : z = z? nfmul(nf, z, q): q;
3154 : }
3155 54089 : if (!z) return gen_1;
3156 26987 : if (hasden) /* denominator */
3157 : {
3158 10096 : z = Q_remove_denom(z, &d);
3159 10096 : d = diviiexact(d, Z_ppo(d, F));
3160 : }
3161 26987 : if (nored || typ(z) != t_COL) return d? gdiv(z, d): z;
3162 10096 : e2 = cgetg(r, t_VEC);
3163 28658 : for (i = 1; i < r; i++) gel(e2,i) = addiu(gel(e,i), 1);
3164 10096 : x = factorbackprime(nf, L, e2);
3165 10096 : if (d) x = RgM_Rg_mul(x, d);
3166 10096 : z = ZC_reducemodlll(z, x);
3167 10096 : return d? RgC_Rg_div(z,d): z;
3168 : }
3169 :
3170 : GEN
3171 0 : idealapprfact(GEN nf, GEN x) {
3172 0 : pari_sp av = avma;
3173 0 : return gc_upto(av, idealapprfact_i(nf, x, 0));
3174 : }
3175 : GEN
3176 14 : idealappr(GEN nf, GEN x) {
3177 14 : pari_sp av = avma;
3178 14 : if (!is_nf_extfactor(x)) x = idealfactor(nf, x);
3179 14 : return gc_upto(av, idealapprfact_i(nf, x, 0));
3180 : }
3181 :
3182 : /* OBSOLETE */
3183 : GEN
3184 14 : idealappr0(GEN nf, GEN x, long fl) { (void)fl; return idealappr(nf, x); }
3185 :
3186 : static GEN
3187 21 : mat_ideal_two_elt2(GEN nf, GEN x, GEN a)
3188 : {
3189 21 : GEN F = idealfactor(nf,a), P = gel(F,1), E = gel(F,2);
3190 21 : long i, r = lg(E);
3191 84 : for (i=1; i<r; i++) gel(E,i) = stoi( idealval(nf,x,gel(P,i)) );
3192 21 : return idealapprfact_i(nf,F,1);
3193 : }
3194 :
3195 : static void
3196 14 : not_in_ideal(GEN a) {
3197 14 : pari_err_DOMAIN("idealtwoelt2","element mod ideal", "!=", gen_0, a);
3198 0 : }
3199 : /* x integral in HNF, a an 'nf' */
3200 : static int
3201 28 : in_ideal(GEN x, GEN a)
3202 : {
3203 28 : switch(typ(a))
3204 : {
3205 14 : case t_INT: return dvdii(a, gcoeff(x,1,1));
3206 7 : case t_COL: return RgV_is_ZV(a) && !!hnf_invimage(x, a);
3207 7 : default: return 0;
3208 : }
3209 : }
3210 :
3211 : /* Given an integral ideal x and a in x, gives a b such that
3212 : * x = aZ_K + bZ_K using the approximation theorem */
3213 : GEN
3214 42 : idealtwoelt2(GEN nf, GEN x, GEN a)
3215 : {
3216 42 : pari_sp av = avma;
3217 : GEN cx, b;
3218 :
3219 42 : nf = checknf(nf);
3220 42 : idealtyp(&x, NULL); if (typ(x) != t_MAT) x = idealhnf_shallow(nf, x);
3221 42 : a = nf_to_scalar_or_basis(nf, a);
3222 42 : if (lg(x) == 1)
3223 : {
3224 14 : if (!isintzero(a)) not_in_ideal(a);
3225 7 : set_avma(av); return gen_0;
3226 : }
3227 28 : x = Q_primitive_part(x, &cx);
3228 28 : if (cx) a = gdiv(a, cx);
3229 28 : if (!in_ideal(x, a)) not_in_ideal(a);
3230 21 : b = mat_ideal_two_elt2(nf, x, a);
3231 21 : if (typ(b) == t_COL)
3232 : {
3233 14 : GEN mod = idealhnf_principal(nf,a);
3234 14 : b = ZC_hnfrem(b,mod);
3235 14 : if (ZV_isscalar(b)) b = gel(b,1);
3236 : }
3237 : else
3238 : {
3239 7 : GEN aZ = typ(a) == t_COL? Q_denom(zk_inv(nf,a)): a; /* (a) \cap Z */
3240 7 : b = centermodii(b, aZ, shifti(aZ,-1));
3241 : }
3242 21 : b = cx? gmul(b,cx): gcopy(b);
3243 21 : return gc_upto(av, b);
3244 : }
3245 :
3246 : /* Given 2 integral ideals x and y in nf, returns a beta in nf such that
3247 : * beta * x is an integral ideal coprime to y */
3248 : GEN
3249 37191 : idealcoprimefact(GEN nf, GEN x, GEN fy)
3250 : {
3251 37191 : GEN L = gel(fy,1), e;
3252 37191 : long i, r = lg(L);
3253 :
3254 37191 : e = cgetg(r, t_COL);
3255 76055 : for (i=1; i<r; i++) gel(e,i) = stoi( -idealval(nf,x,gel(L,i)) );
3256 37191 : return idealapprfact_i(nf, mkmat2(L,e), 0);
3257 : }
3258 : GEN
3259 84 : idealcoprime(GEN nf, GEN x, GEN y)
3260 : {
3261 84 : pari_sp av = avma;
3262 84 : return gc_upto(av, idealcoprimefact(nf, x, idealfactor(nf,y)));
3263 : }
3264 :
3265 : GEN
3266 7 : nfmulmodpr(GEN nf, GEN x, GEN y, GEN modpr)
3267 : {
3268 7 : pari_sp av = avma;
3269 7 : GEN z, p, pr = modpr, T;
3270 :
3271 7 : nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
3272 0 : x = nf_to_Fq(nf,x,modpr);
3273 0 : y = nf_to_Fq(nf,y,modpr);
3274 0 : z = Fq_mul(x,y,T,p);
3275 0 : return gc_upto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
3276 : }
3277 :
3278 : GEN
3279 0 : nfdivmodpr(GEN nf, GEN x, GEN y, GEN modpr)
3280 : {
3281 0 : pari_sp av = avma;
3282 0 : nf = checknf(nf);
3283 0 : return gc_upto(av, nfreducemodpr(nf, nfdiv(nf,x,y), modpr));
3284 : }
3285 :
3286 : GEN
3287 0 : nfpowmodpr(GEN nf, GEN x, GEN k, GEN modpr)
3288 : {
3289 0 : pari_sp av=avma;
3290 0 : GEN z, T, p, pr = modpr;
3291 :
3292 0 : nf = checknf(nf); modpr = nf_to_Fq_init(nf,&pr,&T,&p);
3293 0 : z = nf_to_Fq(nf,x,modpr);
3294 0 : z = Fq_pow(z,k,T,p);
3295 0 : return gc_upto(av, algtobasis(nf, Fq_to_nf(z,modpr)));
3296 : }
3297 :
3298 : GEN
3299 0 : nfkermodpr(GEN nf, GEN x, GEN modpr)
3300 : {
3301 0 : pari_sp av = avma;
3302 0 : GEN T, p, pr = modpr;
3303 :
3304 0 : nf = checknf(nf); modpr = nf_to_Fq_init(nf, &pr,&T,&p);
3305 0 : if (typ(x)!=t_MAT) pari_err_TYPE("nfkermodpr",x);
3306 0 : x = nfM_to_FqM(x, nf, modpr);
3307 0 : return gc_GEN(av, FqM_to_nfM(FqM_ker(x,T,p), modpr));
3308 : }
3309 :
3310 : GEN
3311 0 : nfsolvemodpr(GEN nf, GEN a, GEN b, GEN pr)
3312 : {
3313 0 : const char *f = "nfsolvemodpr";
3314 0 : pari_sp av = avma;
3315 : GEN T, p, modpr;
3316 :
3317 0 : nf = checknf(nf);
3318 0 : modpr = nf_to_Fq_init(nf, &pr,&T,&p);
3319 0 : if (typ(a)!=t_MAT) pari_err_TYPE(f,a);
3320 0 : a = nfM_to_FqM(a, nf, modpr);
3321 0 : switch(typ(b))
3322 : {
3323 0 : case t_MAT:
3324 0 : b = nfM_to_FqM(b, nf, modpr);
3325 0 : b = FqM_gauss(a,b,T,p);
3326 0 : if (!b) pari_err_INV(f,a);
3327 0 : a = FqM_to_nfM(b, modpr);
3328 0 : break;
3329 0 : case t_COL:
3330 0 : b = nfV_to_FqV(b, nf, modpr);
3331 0 : b = FqM_FqC_gauss(a,b,T,p);
3332 0 : if (!b) pari_err_INV(f,a);
3333 0 : a = FqV_to_nfV(b, modpr);
3334 0 : break;
3335 0 : default: pari_err_TYPE(f,b);
3336 : }
3337 0 : return gc_GEN(av, a);
3338 : }
|