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 : /* */
19 : /*******************************************************************/
20 : #include "pari.h"
21 : #include "paripriv.h"
22 :
23 : #define DEBUGLEVEL DEBUGLEVEL_nf
24 :
25 : /*******************************************************************/
26 : /* */
27 : /* OPERATIONS OVER NUMBER FIELD ELEMENTS. */
28 : /* represented as column vectors over the integral basis */
29 : /* */
30 : /*******************************************************************/
31 : static GEN
32 40222504 : get_tab(GEN nf, long *N)
33 : {
34 40222504 : GEN tab = (typ(nf) == t_MAT)? nf: gel(nf,9);
35 40222504 : *N = nbrows(tab); return tab;
36 : }
37 :
38 : /* x != 0, y t_INT. Return x * y (not memory clean if x = 1) */
39 : static GEN
40 1086806533 : _mulii(GEN x, GEN y) {
41 1755942181 : return is_pm1(x)? (signe(x) < 0)? negi(y): y
42 1755761764 : : mulii(x, y);
43 : }
44 :
45 : GEN
46 22203 : tablemul_ei_ej(GEN M, long i, long j)
47 : {
48 : long N;
49 22203 : GEN tab = get_tab(M, &N);
50 22203 : tab += (i-1)*N; return gel(tab,j);
51 : }
52 :
53 : /* Outputs x.ei, where ei is the i-th elt of the algebra basis.
54 : * x an RgV of correct length and arbitrary content (polynomials, scalars...).
55 : * M is the multiplication table ei ej = sum_k M_k^(i,j) ek */
56 : GEN
57 11557 : tablemul_ei(GEN M, GEN x, long i)
58 : {
59 : long j, k, N;
60 : GEN v, tab;
61 :
62 11557 : if (i==1) return gcopy(x);
63 11557 : tab = get_tab(M, &N);
64 11557 : if (typ(x) != t_COL) { v = zerocol(N); gel(v,i) = gcopy(x); return v; }
65 11557 : tab += (i-1)*N; v = cgetg(N+1,t_COL);
66 : /* wi . x = [ sum_j tab[k,j] x[j] ]_k */
67 78491 : for (k=1; k<=N; k++)
68 : {
69 66934 : pari_sp av = avma;
70 66934 : GEN s = gen_0;
71 473214 : for (j=1; j<=N; j++)
72 : {
73 406280 : GEN c = gcoeff(tab,k,j);
74 406280 : if (!gequal0(c)) s = gadd(s, gmul(c, gel(x,j)));
75 : }
76 66934 : gel(v,k) = gerepileupto(av,s);
77 : }
78 11557 : return v;
79 : }
80 : /* as tablemul_ei, assume x a ZV of correct length */
81 : GEN
82 23964014 : zk_ei_mul(GEN nf, GEN x, long i)
83 : {
84 : long j, k, N;
85 : GEN v, tab;
86 :
87 23964014 : if (i==1) return ZC_copy(x);
88 23964014 : tab = get_tab(nf, &N); tab += (i-1)*N;
89 23964062 : v = cgetg(N+1,t_COL);
90 169857188 : for (k=1; k<=N; k++)
91 : {
92 145896732 : pari_sp av = avma;
93 145896732 : GEN s = gen_0;
94 2142658752 : for (j=1; j<=N; j++)
95 : {
96 1996912118 : GEN c = gcoeff(tab,k,j);
97 1996912118 : if (signe(c)) s = addii(s, _mulii(c, gel(x,j)));
98 : }
99 145746634 : gel(v,k) = gerepileuptoint(av, s);
100 : }
101 23960456 : return v;
102 : }
103 :
104 : /* table of multiplication by wi in R[w1,..., wN] */
105 : GEN
106 39371 : ei_multable(GEN TAB, long i)
107 : {
108 : long k,N;
109 39371 : GEN m, tab = get_tab(TAB, &N);
110 39371 : tab += (i-1)*N;
111 39371 : m = cgetg(N+1,t_MAT);
112 154451 : for (k=1; k<=N; k++) gel(m,k) = gel(tab,k);
113 39371 : return m;
114 : }
115 :
116 : GEN
117 10842867 : zk_multable(GEN nf, GEN x)
118 : {
119 10842867 : long i, l = lg(x);
120 10842867 : GEN mul = cgetg(l,t_MAT);
121 10842880 : gel(mul,1) = x; /* assume w_1 = 1 */
122 34442205 : for (i=2; i<l; i++) gel(mul,i) = zk_ei_mul(nf,x,i);
123 10839771 : return mul;
124 : }
125 : GEN
126 2751 : multable(GEN M, GEN x)
127 : {
128 : long i, N;
129 : GEN mul;
130 2751 : if (typ(x) == t_MAT) return x;
131 0 : M = get_tab(M, &N);
132 0 : if (typ(x) != t_COL) return scalarmat(x, N);
133 0 : mul = cgetg(N+1,t_MAT);
134 0 : gel(mul,1) = x; /* assume w_1 = 1 */
135 0 : for (i=2; i<=N; i++) gel(mul,i) = tablemul_ei(M,x,i);
136 0 : return mul;
137 : }
138 :
139 : /* x integral in nf; table of multiplication by x in ZK = Z[w1,..., wN].
140 : * Return a t_INT if x is scalar, and a ZM otherwise */
141 : GEN
142 5000444 : zk_scalar_or_multable(GEN nf, GEN x)
143 : {
144 5000444 : long tx = typ(x);
145 5000444 : if (tx == t_MAT || tx == t_INT) return x;
146 4837902 : x = nf_to_scalar_or_basis(nf, x);
147 4837820 : return (typ(x) == t_COL)? zk_multable(nf, x): x;
148 : }
149 :
150 : GEN
151 21306 : nftrace(GEN nf, GEN x)
152 : {
153 21306 : pari_sp av = avma;
154 21306 : nf = checknf(nf);
155 21306 : x = nf_to_scalar_or_basis(nf, x);
156 21286 : x = (typ(x) == t_COL)? RgV_dotproduct(x, gel(nf_get_Tr(nf),1))
157 21307 : : gmulgu(x, nf_get_degree(nf));
158 21307 : return gerepileupto(av, x);
159 : }
160 : GEN
161 1043 : rnfelttrace(GEN rnf, GEN x)
162 : {
163 1043 : pari_sp av = avma;
164 1043 : checkrnf(rnf);
165 : /* avoid rnfabstorel special t_POL case misinterpretation */
166 1036 : if (typ(x) == t_POL && varn(x) == rnf_get_varn(rnf))
167 63 : x = gmodulo(x, rnf_get_pol(rnf));
168 1036 : x = rnfeltabstorel(rnf, x);
169 721 : x = (typ(x) == t_POLMOD)? rnfeltdown(rnf, gtrace(x))
170 826 : : gmulgu(x, rnf_get_degree(rnf));
171 826 : return gerepileupto(av, x);
172 : }
173 :
174 : static GEN
175 35 : famatQ_to_famatZ(GEN fa)
176 : {
177 35 : GEN E, F, Q, P = gel(fa,1);
178 35 : long i, j, l = lg(P);
179 35 : if (l == 1 || RgV_is_ZV(P)) return fa;
180 7 : Q = cgetg(2*l, t_COL);
181 7 : F = cgetg(2*l, t_COL); E = gel(fa, 2);
182 35 : for (i = j = 1; i < l; i++)
183 : {
184 28 : GEN p = gel(P,i);
185 28 : if (typ(p) == t_INT)
186 14 : { gel(Q, j) = p; gel(F, j) = gel(E, i); j++; }
187 : else
188 : {
189 14 : gel(Q, j) = gel(p,1); gel(F, j) = gel(E, i); j++;
190 14 : gel(Q, j) = gel(p,2); gel(F, j) = negi(gel(E, i)); j++;
191 : }
192 : }
193 7 : setlg(Q, j); setlg(F, j); return mkmat2(Q, F);
194 : }
195 : static GEN
196 35 : famat_cba(GEN fa)
197 : {
198 35 : GEN Q, F, P = gel(fa, 1), E = gel(fa, 2);
199 35 : long i, j, lQ, l = lg(P);
200 35 : if (l == 1) return fa;
201 28 : Q = ZV_cba(P); lQ = lg(Q); settyp(Q, t_COL);
202 28 : F = cgetg(lQ, t_COL);
203 77 : for (j = 1; j < lQ; j++)
204 : {
205 49 : GEN v = gen_0, q = gel(Q,j);
206 49 : if (!equali1(q))
207 203 : for (i = 1; i < l; i++)
208 : {
209 161 : long e = Z_pval(gel(P,i), q);
210 161 : if (e) v = addii(v, muliu(gel(E,i), e));
211 : }
212 49 : gel(F, j) = v;
213 : }
214 28 : return mkmat2(Q, F);
215 : }
216 : static long
217 35 : famat_sign(GEN fa)
218 : {
219 35 : GEN P = gel(fa,1), E = gel(fa,2);
220 35 : long i, l = lg(P), s = 1;
221 126 : for (i = 1; i < l; i++)
222 91 : if (signe(gel(P,i)) < 0 && mpodd(gel(E,i))) s = -s;
223 35 : return s;
224 : }
225 : static GEN
226 35 : famat_abs(GEN fa)
227 : {
228 35 : GEN Q, P = gel(fa,1);
229 : long i, l;
230 35 : Q = cgetg_copy(P, &l);
231 126 : for (i = 1; i < l; i++) gel(Q,i) = absi_shallow(gel(P,i));
232 35 : return mkmat2(Q, gel(fa,2));
233 : }
234 :
235 : /* assume nf is a genuine nf, fa a famat */
236 : static GEN
237 35 : famat_norm(GEN nf, GEN fa)
238 : {
239 35 : pari_sp av = avma;
240 35 : GEN G, g = gel(fa,1);
241 : long i, l, s;
242 :
243 35 : G = cgetg_copy(g, &l);
244 112 : for (i = 1; i < l; i++) gel(G,i) = nfnorm(nf, gel(g,i));
245 35 : fa = mkmat2(G, gel(fa,2));
246 35 : fa = famatQ_to_famatZ(fa);
247 35 : s = famat_sign(fa);
248 35 : fa = famat_reduce(famat_abs(fa));
249 35 : fa = famat_cba(fa);
250 35 : g = factorback(fa);
251 35 : return gerepileupto(av, s < 0? gneg(g): g);
252 : }
253 : GEN
254 223373 : nfnorm(GEN nf, GEN x)
255 : {
256 223373 : pari_sp av = avma;
257 : GEN c, den;
258 : long n;
259 223373 : nf = checknf(nf);
260 223373 : n = nf_get_degree(nf);
261 223373 : if (typ(x) == t_MAT) return famat_norm(nf, x);
262 223338 : x = nf_to_scalar_or_basis(nf, x);
263 223336 : if (typ(x)!=t_COL)
264 126910 : return gerepileupto(av, gpowgs(x, n));
265 96426 : x = nf_to_scalar_or_alg(nf, Q_primitive_part(x, &c));
266 96428 : x = Q_remove_denom(x, &den);
267 96427 : x = ZX_resultant_all(nf_get_pol(nf), x, den, 0);
268 96428 : return gerepileupto(av, c ? gmul(x, gpowgs(c, n)): x);
269 : }
270 :
271 : static GEN
272 119 : to_RgX(GEN P, long vx)
273 : {
274 119 : return varn(P) == vx ? P: scalarpol_shallow(P, vx);
275 : }
276 :
277 : GEN
278 462 : rnfeltnorm(GEN rnf, GEN x)
279 : {
280 462 : pari_sp av = avma;
281 : GEN nf, pol;
282 : long v;
283 462 : checkrnf(rnf);
284 455 : v = rnf_get_varn(rnf);
285 : /* avoid rnfabstorel special t_POL case misinterpretation */
286 455 : if (typ(x) == t_POL && varn(x) == v) x = gmodulo(x, rnf_get_pol(rnf));
287 455 : x = liftpol_shallow(rnfeltabstorel(rnf, x));
288 245 : nf = rnf_get_nf(rnf); pol = rnf_get_pol(rnf);
289 490 : x = (typ(x) == t_POL)
290 119 : ? rnfeltdown(rnf, nfX_resultant(nf,pol,to_RgX(x,v)))
291 245 : : gpowgs(x, rnf_get_degree(rnf));
292 245 : return gerepileupto(av, x);
293 : }
294 :
295 : /* x + y in nf */
296 : GEN
297 23476606 : nfadd(GEN nf, GEN x, GEN y)
298 : {
299 23476606 : pari_sp av = avma;
300 : GEN z;
301 :
302 23476606 : nf = checknf(nf);
303 23476606 : x = nf_to_scalar_or_basis(nf, x);
304 23476606 : y = nf_to_scalar_or_basis(nf, y);
305 23476606 : if (typ(x) != t_COL)
306 17706842 : { z = (typ(y) == t_COL)? RgC_Rg_add(y, x): gadd(x,y); }
307 : else
308 5769764 : { z = (typ(y) == t_COL)? RgC_add(x, y): RgC_Rg_add(x, y); }
309 23476606 : return gerepileupto(av, z);
310 : }
311 : /* x - y in nf */
312 : GEN
313 1815196 : nfsub(GEN nf, GEN x, GEN y)
314 : {
315 1815196 : pari_sp av = avma;
316 : GEN z;
317 :
318 1815196 : nf = checknf(nf);
319 1815196 : x = nf_to_scalar_or_basis(nf, x);
320 1815196 : y = nf_to_scalar_or_basis(nf, y);
321 1815196 : if (typ(x) != t_COL)
322 1282372 : { z = (typ(y) == t_COL)? Rg_RgC_sub(x,y): gsub(x,y); }
323 : else
324 532824 : { z = (typ(y) == t_COL)? RgC_sub(x,y): RgC_Rg_sub(x,y); }
325 1815196 : return gerepileupto(av, z);
326 : }
327 :
328 : /* product of ZC x,y in (true) nf; ( sum_i x_i sum_j y_j m^{i,j}_k )_k */
329 : static GEN
330 9060748 : nfmuli_ZC(GEN nf, GEN x, GEN y)
331 : {
332 : long i, j, k, N;
333 9060748 : GEN TAB = get_tab(nf, &N), v = cgetg(N+1,t_COL);
334 :
335 44059403 : for (k = 1; k <= N; k++)
336 : {
337 34998726 : pari_sp av = avma;
338 34998726 : GEN s, TABi = TAB;
339 34998726 : if (k == 1)
340 9060744 : s = mulii(gel(x,1),gel(y,1));
341 : else
342 25937860 : s = addii(mulii(gel(x,1),gel(y,k)),
343 25937982 : mulii(gel(x,k),gel(y,1)));
344 226942762 : for (i=2; i<=N; i++)
345 : {
346 191948139 : GEN t, xi = gel(x,i);
347 191948139 : TABi += N;
348 191948139 : if (!signe(xi)) continue;
349 :
350 96675612 : t = NULL;
351 1083188565 : for (j=2; j<=N; j++)
352 : {
353 986514032 : GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
354 986514032 : if (!signe(c)) continue;
355 291367444 : p1 = _mulii(c, gel(y,j));
356 291372614 : t = t? addii(t, p1): p1;
357 : }
358 96674533 : if (t) s = addii(s, mulii(xi, t));
359 : }
360 34994623 : gel(v,k) = gerepileuptoint(av,s);
361 : }
362 9060677 : return v;
363 : }
364 : static int
365 74737688 : is_famat(GEN x) { return typ(x) == t_MAT && lg(x) == 3; }
366 : /* product of x and y in nf */
367 : GEN
368 36372608 : nfmul(GEN nf, GEN x, GEN y)
369 : {
370 : GEN z;
371 36372608 : pari_sp av = avma;
372 :
373 36372608 : if (x == y) return nfsqr(nf,x);
374 :
375 32276555 : nf = checknf(nf);
376 32276552 : if (is_famat(x) || is_famat(y)) return famat_mul(x, y);
377 32276242 : x = nf_to_scalar_or_basis(nf, x);
378 32276243 : y = nf_to_scalar_or_basis(nf, y);
379 32276239 : if (typ(x) != t_COL)
380 : {
381 21844069 : if (isintzero(x)) return gen_0;
382 15772942 : z = (typ(y) == t_COL)? RgC_Rg_mul(y, x): gmul(x,y); }
383 : else
384 : {
385 10432170 : if (typ(y) != t_COL)
386 : {
387 4547630 : if (isintzero(y)) return gen_0;
388 1613512 : z = RgC_Rg_mul(x, y);
389 : }
390 : else
391 : {
392 : GEN dx, dy;
393 5884540 : x = Q_remove_denom(x, &dx);
394 5884540 : y = Q_remove_denom(y, &dy);
395 5884540 : z = nfmuli_ZC(nf,x,y);
396 5884540 : dx = mul_denom(dx,dy);
397 5884540 : if (dx) z = ZC_Z_div(z, dx);
398 : }
399 : }
400 23270994 : return gerepileupto(av, z);
401 : }
402 : /* square of ZC x in nf */
403 : static GEN
404 7127160 : nfsqri_ZC(GEN nf, GEN x)
405 : {
406 : long i, j, k, N;
407 7127160 : GEN TAB = get_tab(nf, &N), v = cgetg(N+1,t_COL);
408 :
409 38994004 : for (k = 1; k <= N; k++)
410 : {
411 31866878 : pari_sp av = avma;
412 31866878 : GEN s, TABi = TAB;
413 31866878 : if (k == 1)
414 7127363 : s = sqri(gel(x,1));
415 : else
416 24739515 : s = shifti(mulii(gel(x,1),gel(x,k)), 1);
417 253621622 : for (i=2; i<=N; i++)
418 : {
419 221777690 : GEN p1, c, t, xi = gel(x,i);
420 221777690 : TABi += N;
421 221777690 : if (!signe(xi)) continue;
422 :
423 79847690 : c = gcoeff(TABi, k, i);
424 79847690 : t = signe(c)? _mulii(c,xi): NULL;
425 675285146 : for (j=i+1; j<=N; j++)
426 : {
427 595438013 : c = gcoeff(TABi, k, j);
428 595438013 : if (!signe(c)) continue;
429 231800898 : p1 = _mulii(c, shifti(gel(x,j),1));
430 231806676 : t = t? addii(t, p1): p1;
431 : }
432 79847133 : if (t) s = addii(s, mulii(xi, t));
433 : }
434 31843932 : gel(v,k) = gerepileuptoint(av,s);
435 : }
436 7127126 : return v;
437 : }
438 : /* square of x in nf */
439 : GEN
440 8915019 : nfsqr(GEN nf, GEN x)
441 : {
442 8915019 : pari_sp av = avma;
443 : GEN z;
444 :
445 8915019 : nf = checknf(nf);
446 8915018 : if (is_famat(x)) return famat_sqr(x);
447 8915024 : x = nf_to_scalar_or_basis(nf, x);
448 8915024 : if (typ(x) != t_COL) z = gsqr(x);
449 : else
450 : {
451 : GEN dx;
452 2632321 : x = Q_remove_denom(x, &dx);
453 2632322 : z = nfsqri_ZC(nf,x);
454 2632321 : if (dx) z = RgC_Rg_div(z, sqri(dx));
455 : }
456 8915024 : return gerepileupto(av, z);
457 : }
458 :
459 : /* x a ZC, v a t_COL of ZC/Z */
460 : GEN
461 205708 : zkC_multable_mul(GEN v, GEN x)
462 : {
463 205708 : long i, l = lg(v);
464 205708 : GEN y = cgetg(l, t_COL);
465 800229 : for (i = 1; i < l; i++)
466 : {
467 594522 : GEN c = gel(v,i);
468 594522 : if (typ(c)!=t_COL) {
469 0 : if (!isintzero(c)) c = ZC_Z_mul(gel(x,1), c);
470 : } else {
471 594522 : c = ZM_ZC_mul(x,c);
472 594521 : if (ZV_isscalar(c)) c = gel(c,1);
473 : }
474 594521 : gel(y,i) = c;
475 : }
476 205707 : return y;
477 : }
478 :
479 : GEN
480 57227 : nfC_multable_mul(GEN v, GEN x)
481 : {
482 57227 : long i, l = lg(v);
483 57227 : GEN y = cgetg(l, t_COL);
484 385363 : for (i = 1; i < l; i++)
485 : {
486 328136 : GEN c = gel(v,i);
487 328136 : if (typ(c)!=t_COL) {
488 273526 : if (!isintzero(c)) c = RgC_Rg_mul(gel(x,1), c);
489 : } else {
490 54610 : c = RgM_RgC_mul(x,c);
491 54610 : if (QV_isscalar(c)) c = gel(c,1);
492 : }
493 328136 : gel(y,i) = c;
494 : }
495 57227 : return y;
496 : }
497 :
498 : GEN
499 200022 : nfC_nf_mul(GEN nf, GEN v, GEN x)
500 : {
501 : long tx;
502 : GEN y;
503 :
504 200022 : x = nf_to_scalar_or_basis(nf, x);
505 200022 : tx = typ(x);
506 200022 : if (tx != t_COL)
507 : {
508 : long l, i;
509 151425 : if (tx == t_INT)
510 : {
511 142150 : long s = signe(x);
512 142150 : if (!s) return zerocol(lg(v)-1);
513 134676 : if (is_pm1(x)) return s > 0? leafcopy(v): RgC_neg(v);
514 : }
515 49098 : l = lg(v); y = cgetg(l, t_COL);
516 350483 : for (i=1; i < l; i++)
517 : {
518 301385 : GEN c = gel(v,i);
519 301385 : if (typ(c) != t_COL) c = gmul(c, x); else c = RgC_Rg_mul(c, x);
520 301385 : gel(y,i) = c;
521 : }
522 49098 : return y;
523 : }
524 : else
525 : {
526 : GEN dx;
527 48597 : x = zk_multable(nf, Q_remove_denom(x,&dx));
528 48597 : y = nfC_multable_mul(v, x);
529 48597 : return dx? RgC_Rg_div(y, dx): y;
530 : }
531 : }
532 : static GEN
533 11213 : mulbytab(GEN M, GEN c)
534 11213 : { return typ(c) == t_COL? RgM_RgC_mul(M,c): RgC_Rg_mul(gel(M,1), c); }
535 : GEN
536 2751 : tablemulvec(GEN M, GEN x, GEN v)
537 : {
538 : long l, i;
539 : GEN y;
540 :
541 2751 : if (typ(x) == t_COL && RgV_isscalar(x))
542 : {
543 0 : x = gel(x,1);
544 0 : return typ(v) == t_POL? RgX_Rg_mul(v,x): RgV_Rg_mul(v,x);
545 : }
546 2751 : x = multable(M, x); /* multiplication table by x */
547 2751 : y = cgetg_copy(v, &l);
548 2751 : if (typ(v) == t_POL)
549 : {
550 2751 : y[1] = v[1];
551 13964 : for (i=2; i < l; i++) gel(y,i) = mulbytab(x, gel(v,i));
552 2751 : y = normalizepol(y);
553 : }
554 : else
555 : {
556 0 : for (i=1; i < l; i++) gel(y,i) = mulbytab(x, gel(v,i));
557 : }
558 2751 : return y;
559 : }
560 :
561 : GEN
562 1261699 : zkmultable_capZ(GEN mx) { return Q_denom(zkmultable_inv(mx)); }
563 : GEN
564 1581200 : zkmultable_inv(GEN mx) { return ZM_gauss(mx, col_ei(lg(mx)-1,1)); }
565 : /* nf a true nf, x a ZC */
566 : GEN
567 319505 : zk_inv(GEN nf, GEN x) { return zkmultable_inv(zk_multable(nf,x)); }
568 :
569 : /* inverse of x in nf */
570 : GEN
571 240289 : nfinv(GEN nf, GEN x)
572 : {
573 240289 : pari_sp av = avma;
574 : GEN z;
575 :
576 240289 : nf = checknf(nf);
577 240289 : if (is_famat(x)) return famat_inv(x);
578 240289 : x = nf_to_scalar_or_basis(nf, x);
579 240289 : if (typ(x) == t_COL)
580 : {
581 : GEN d;
582 190865 : x = Q_remove_denom(x, &d);
583 190865 : z = zk_inv(nf, x);
584 190865 : if (d) z = RgC_Rg_mul(z, d);
585 : }
586 : else
587 49424 : z = ginv(x);
588 240289 : return gerepileupto(av, z);
589 : }
590 :
591 : /* quotient of x and y in nf */
592 : GEN
593 36173 : nfdiv(GEN nf, GEN x, GEN y)
594 : {
595 36173 : pari_sp av = avma;
596 : GEN z;
597 :
598 36173 : nf = checknf(nf);
599 36173 : if (is_famat(x) || is_famat(y)) return famat_div(x,y);
600 36082 : y = nf_to_scalar_or_basis(nf, y);
601 36082 : if (typ(y) != t_COL)
602 : {
603 22099 : x = nf_to_scalar_or_basis(nf, x);
604 22099 : z = (typ(x) == t_COL)? RgC_Rg_div(x, y): gdiv(x,y);
605 : }
606 : else
607 : {
608 : GEN d;
609 13983 : y = Q_remove_denom(y, &d);
610 13983 : z = nfmul(nf, x, zk_inv(nf,y));
611 13983 : if (d) z = typ(z) == t_COL? RgC_Rg_mul(z, d): gmul(z, d);
612 : }
613 36082 : return gerepileupto(av, z);
614 : }
615 :
616 : /* product of INTEGERS (t_INT or ZC) x and y in (true) nf */
617 : GEN
618 4548962 : nfmuli(GEN nf, GEN x, GEN y)
619 : {
620 4548962 : if (typ(x) == t_INT) return (typ(y) == t_COL)? ZC_Z_mul(y, x): mulii(x,y);
621 3409768 : if (typ(y) == t_INT) return ZC_Z_mul(x, y);
622 3176166 : return nfmuli_ZC(nf, x, y);
623 : }
624 : GEN
625 4494887 : nfsqri(GEN nf, GEN x)
626 4494887 : { return (typ(x) == t_INT)? sqri(x): nfsqri_ZC(nf, x); }
627 :
628 : /* both x and y are RgV */
629 : GEN
630 0 : tablemul(GEN TAB, GEN x, GEN y)
631 : {
632 : long i, j, k, N;
633 : GEN s, v;
634 0 : if (typ(x) != t_COL) return gmul(x, y);
635 0 : if (typ(y) != t_COL) return gmul(y, x);
636 0 : N = lg(x)-1;
637 0 : v = cgetg(N+1,t_COL);
638 0 : for (k=1; k<=N; k++)
639 : {
640 0 : pari_sp av = avma;
641 0 : GEN TABi = TAB;
642 0 : if (k == 1)
643 0 : s = gmul(gel(x,1),gel(y,1));
644 : else
645 0 : s = gadd(gmul(gel(x,1),gel(y,k)),
646 0 : gmul(gel(x,k),gel(y,1)));
647 0 : for (i=2; i<=N; i++)
648 : {
649 0 : GEN t, xi = gel(x,i);
650 0 : TABi += N;
651 0 : if (gequal0(xi)) continue;
652 :
653 0 : t = NULL;
654 0 : for (j=2; j<=N; j++)
655 : {
656 0 : GEN p1, c = gcoeff(TABi, k, j); /* m^{i,j}_k */
657 0 : if (gequal0(c)) continue;
658 0 : p1 = gmul(c, gel(y,j));
659 0 : t = t? gadd(t, p1): p1;
660 : }
661 0 : if (t) s = gadd(s, gmul(xi, t));
662 : }
663 0 : gel(v,k) = gerepileupto(av,s);
664 : }
665 0 : return v;
666 : }
667 : GEN
668 49090 : tablesqr(GEN TAB, GEN x)
669 : {
670 : long i, j, k, N;
671 : GEN s, v;
672 :
673 49090 : if (typ(x) != t_COL) return gsqr(x);
674 49090 : N = lg(x)-1;
675 49090 : v = cgetg(N+1,t_COL);
676 :
677 349430 : for (k=1; k<=N; k++)
678 : {
679 300340 : pari_sp av = avma;
680 300340 : GEN TABi = TAB;
681 300340 : if (k == 1)
682 49090 : s = gsqr(gel(x,1));
683 : else
684 251250 : s = gmul2n(gmul(gel(x,1),gel(x,k)), 1);
685 1909862 : for (i=2; i<=N; i++)
686 : {
687 1609522 : GEN p1, c, t, xi = gel(x,i);
688 1609522 : TABi += N;
689 1609522 : if (gequal0(xi)) continue;
690 :
691 419846 : c = gcoeff(TABi, k, i);
692 419846 : t = !gequal0(c)? gmul(c,xi): NULL;
693 1676969 : for (j=i+1; j<=N; j++)
694 : {
695 1257123 : c = gcoeff(TABi, k, j);
696 1257123 : if (gequal0(c)) continue;
697 646443 : p1 = gmul(gmul2n(c,1), gel(x,j));
698 646443 : t = t? gadd(t, p1): p1;
699 : }
700 419846 : if (t) s = gadd(s, gmul(xi, t));
701 : }
702 300340 : gel(v,k) = gerepileupto(av,s);
703 : }
704 49090 : return v;
705 : }
706 :
707 : static GEN
708 355330 : _mul(void *data, GEN x, GEN y) { return nfmuli((GEN)data,x,y); }
709 : static GEN
710 985441 : _sqr(void *data, GEN x) { return nfsqri((GEN)data,x); }
711 :
712 : /* Compute z^n in nf, left-shift binary powering */
713 : GEN
714 942301 : nfpow(GEN nf, GEN z, GEN n)
715 : {
716 942301 : pari_sp av = avma;
717 : long s;
718 : GEN x, cx;
719 :
720 942301 : if (typ(n)!=t_INT) pari_err_TYPE("nfpow",n);
721 942301 : nf = checknf(nf);
722 942301 : s = signe(n); if (!s) return gen_1;
723 942301 : if (is_famat(z)) return famat_pow(z, n);
724 881662 : x = nf_to_scalar_or_basis(nf, z);
725 881658 : if (typ(x) != t_COL) return powgi(x,n);
726 761789 : if (s < 0)
727 : { /* simplified nfinv */
728 : GEN d;
729 46160 : x = Q_remove_denom(x, &d);
730 46160 : x = zk_inv(nf, x);
731 46161 : x = primitive_part(x, &cx);
732 46161 : cx = mul_content(cx, d);
733 46161 : n = negi(n);
734 : }
735 : else
736 715629 : x = primitive_part(x, &cx);
737 761779 : x = gen_pow_i(x, n, (void*)nf, _sqr, _mul);
738 761793 : if (cx)
739 47344 : x = gerepileupto(av, gmul(x, powgi(cx, n)));
740 : else
741 714449 : x = gerepilecopy(av, x);
742 761796 : return x;
743 : }
744 : /* Compute z^n in nf, left-shift binary powering */
745 : GEN
746 354571 : nfpow_u(GEN nf, GEN z, ulong n)
747 : {
748 354571 : pari_sp av = avma;
749 : GEN x, cx;
750 :
751 354571 : if (!n) return gen_1;
752 354571 : x = nf_to_scalar_or_basis(nf, z);
753 354571 : if (typ(x) != t_COL) return gpowgs(x,n);
754 318290 : x = primitive_part(x, &cx);
755 318289 : x = gen_powu_i(x, n, (void*)nf, _sqr, _mul);
756 318290 : if (cx)
757 : {
758 114357 : x = gmul(x, powgi(cx, utoipos(n)));
759 114357 : return gerepileupto(av,x);
760 : }
761 203933 : return gerepilecopy(av, x);
762 : }
763 :
764 : long
765 1099 : nfissquare(GEN nf, GEN z, GEN *px)
766 : {
767 1099 : pari_sp av = avma;
768 1099 : long v = fetch_var_higher();
769 : GEN R;
770 1099 : nf = checknf(nf);
771 1099 : if (nf_get_degree(nf) == 1)
772 : {
773 189 : z = algtobasis(nf, z);
774 189 : if (!issquareall(gel(z,1), px)) return gc_long(av, 0);
775 21 : if (px) *px = gerepileupto(av, *px); else set_avma(av);
776 21 : return 1;
777 : }
778 910 : z = nf_to_scalar_or_alg(nf, z);
779 910 : R = nfroots(nf, deg2pol_shallow(gen_m1, gen_0, z, v));
780 910 : delete_var(); if (lg(R) == 1) return gc_long(av, 0);
781 560 : if (px) *px = gerepilecopy(av, nf_to_scalar_or_basis(nf, gel(R,1)));
782 14 : else set_avma(av);
783 560 : return 1;
784 : }
785 :
786 : long
787 7713 : nfispower(GEN nf, GEN z, long n, GEN *px)
788 : {
789 7713 : pari_sp av = avma;
790 7713 : long v = fetch_var_higher();
791 : GEN R;
792 7713 : nf = checknf(nf);
793 7713 : if (nf_get_degree(nf) == 1)
794 : {
795 329 : z = algtobasis(nf, z);
796 329 : if (!ispower(gel(z,1), stoi(n), px)) return gc_long(av, 0);
797 147 : if (px) *px = gerepileupto(av, *px); else set_avma(av);
798 147 : return 1;
799 : }
800 7384 : if (n <= 0)
801 0 : pari_err_DOMAIN("nfeltispower","exponent","<=",gen_0,stoi(n));
802 7384 : z = nf_to_scalar_or_alg(nf, z);
803 7384 : if (n==1)
804 : {
805 0 : if (px) *px = gerepilecopy(av, z);
806 0 : return 1;
807 : }
808 7384 : R = nfroots(nf, gsub(pol_xn(n, v), z));
809 7384 : delete_var(); if (lg(R) == 1) return gc_long(av, 0);
810 3157 : if (px) *px = gerepilecopy(av, nf_to_scalar_or_basis(nf, gel(R,1)));
811 3143 : else set_avma(av);
812 3157 : return 1;
813 : }
814 :
815 : static GEN
816 56 : idmulred(void *nf, GEN x, GEN y) { return idealmulred((GEN) nf, x, y); }
817 : static GEN
818 413 : idpowred(void *nf, GEN x, GEN n) { return idealpowred((GEN) nf, x, n); }
819 : static GEN
820 72020 : idmul(void *nf, GEN x, GEN y) { return idealmul((GEN) nf, x, y); }
821 : static GEN
822 87971 : idpow(void *nf, GEN x, GEN n) { return idealpow((GEN) nf, x, n); }
823 : GEN
824 86373 : idealfactorback(GEN nf, GEN L, GEN e, long red)
825 : {
826 86373 : nf = checknf(nf);
827 86373 : if (red) return gen_factorback(L, e, (void*)nf, &idmulred, &idpowred, NULL);
828 86016 : if (!e && typ(L) == t_MAT && lg(L) == 3) { e = gel(L,2); L = gel(L,1); }
829 86016 : if (is_vec_t(typ(L)) && RgV_is_prV(L))
830 : { /* don't use gen_factorback since *= pr^v can be done more efficiently */
831 65382 : pari_sp av = avma;
832 65382 : long i, l = lg(L);
833 : GEN a;
834 65382 : if (!e) e = const_vec(l-1, gen_1);
835 62526 : else switch(typ(e))
836 : {
837 7768 : case t_VECSMALL: e = zv_to_ZV(e); break;
838 54758 : case t_VEC: case t_COL:
839 54758 : if (!RgV_is_ZV(e))
840 0 : pari_err_TYPE("factorback [not an exponent vector]", e);
841 54758 : break;
842 0 : default: pari_err_TYPE("idealfactorback", e);
843 : }
844 65382 : if (l != lg(e))
845 0 : pari_err_TYPE("factorback [not an exponent vector]", e);
846 65382 : if (l == 1 || ZV_equal0(e)) return gc_const(av, gen_1);
847 23569 : a = idealpow(nf, gel(L,1), gel(e,1));
848 251149 : for (i = 2; i < l; i++)
849 227580 : if (signe(gel(e,i))) a = idealmulpowprime(nf, a, gel(L,i), gel(e,i));
850 23569 : return gerepileupto(av, a);
851 : }
852 20634 : return gen_factorback(L, e, (void*)nf, &idmul, &idpow, NULL);
853 : }
854 : static GEN
855 327880 : eltmul(void *nf, GEN x, GEN y) { return nfmul((GEN) nf, x, y); }
856 : static GEN
857 465136 : eltpow(void *nf, GEN x, GEN n) { return nfpow((GEN) nf, x, n); }
858 : GEN
859 265322 : nffactorback(GEN nf, GEN L, GEN e)
860 265322 : { return gen_factorback(L, e, (void*)checknf(nf), &eltmul, &eltpow, NULL); }
861 :
862 : static GEN
863 3099304 : _nf_red(void *E, GEN x) { (void)E; return gcopy(x); }
864 :
865 : static GEN
866 12672932 : _nf_add(void *E, GEN x, GEN y) { return nfadd((GEN)E,x,y); }
867 :
868 : static GEN
869 751655 : _nf_neg(void *E, GEN x) { (void)E; return gneg(x); }
870 :
871 : static GEN
872 15218661 : _nf_mul(void *E, GEN x, GEN y) { return nfmul((GEN)E,x,y); }
873 :
874 : static GEN
875 53959 : _nf_inv(void *E, GEN x) { return nfinv((GEN)E,x); }
876 :
877 : static GEN
878 11128 : _nf_s(void *E, long x) { (void)E; return stoi(x); }
879 :
880 : static const struct bb_field nf_field={_nf_red,_nf_add,_nf_mul,_nf_neg,
881 : _nf_inv,&gequal0,_nf_s };
882 :
883 227960 : const struct bb_field *get_nf_field(void **E, GEN nf)
884 227960 : { *E = (void*)nf; return &nf_field; }
885 :
886 : GEN
887 14 : nfM_det(GEN nf, GEN M)
888 : {
889 : void *E;
890 14 : const struct bb_field *S = get_nf_field(&E, nf);
891 14 : return gen_det(M, E, S);
892 : }
893 : GEN
894 11114 : nfM_inv(GEN nf, GEN M)
895 : {
896 : void *E;
897 11114 : const struct bb_field *S = get_nf_field(&E, nf);
898 11114 : return gen_Gauss(M, matid(lg(M)-1), E, S);
899 : }
900 :
901 : GEN
902 0 : nfM_ker(GEN nf, GEN M)
903 : {
904 : void *E;
905 0 : const struct bb_field *S = get_nf_field(&E, nf);
906 0 : return gen_ker(M, 0, E, S);
907 : }
908 :
909 : GEN
910 10610 : nfM_mul(GEN nf, GEN A, GEN B)
911 : {
912 : void *E;
913 10610 : const struct bb_field *S = get_nf_field(&E, nf);
914 10610 : return gen_matmul(A, B, E, S);
915 : }
916 : GEN
917 206222 : nfM_nfC_mul(GEN nf, GEN A, GEN B)
918 : {
919 : void *E;
920 206222 : const struct bb_field *S = get_nf_field(&E, nf);
921 206222 : return gen_matcolmul(A, B, E, S);
922 : }
923 :
924 : /* valuation of integral x (ZV), with resp. to prime ideal pr */
925 : long
926 24029879 : ZC_nfvalrem(GEN x, GEN pr, GEN *newx)
927 : {
928 24029879 : pari_sp av = avma;
929 : long i, v, l;
930 24029879 : GEN r, y, p = pr_get_p(pr), mul = pr_get_tau(pr);
931 :
932 : /* p inert */
933 24029903 : if (typ(mul) == t_INT) return newx? ZV_pvalrem(x, p, newx):ZV_pval(x, p);
934 23024815 : y = cgetg_copy(x, &l); /* will hold the new x */
935 23025237 : x = leafcopy(x);
936 37191162 : for(v=0;; v++)
937 : {
938 143029194 : for (i=1; i<l; i++)
939 : { /* is (x.b)[i] divisible by p ? */
940 128857813 : gel(y,i) = dvmdii(ZMrow_ZC_mul(mul,x,i),p,&r);
941 128861303 : if (r != gen_0) { if (newx) *newx = x; return v; }
942 : }
943 14171381 : swap(x, y);
944 14171381 : if (!newx && (v & 0xf) == 0xf) v += pr_get_e(pr) * ZV_pvalrem(x, p, &x);
945 14171381 : if (gc_needed(av,1))
946 : {
947 0 : if(DEBUGMEM>1) pari_warn(warnmem,"ZC_nfvalrem, v >= %ld", v);
948 0 : gerepileall(av, 2, &x, &y);
949 : }
950 : }
951 : }
952 : long
953 19757359 : ZC_nfval(GEN x, GEN P)
954 19757359 : { return ZC_nfvalrem(x, P, NULL); }
955 :
956 : /* v_P(x) != 0, x a ZV. Simpler version of ZC_nfvalrem */
957 : int
958 1250376 : ZC_prdvd(GEN x, GEN P)
959 : {
960 1250376 : pari_sp av = avma;
961 : long i, l;
962 1250376 : GEN p = pr_get_p(P), mul = pr_get_tau(P);
963 1250390 : if (typ(mul) == t_INT) return ZV_Z_dvd(x, p);
964 1249844 : l = lg(x);
965 5064373 : for (i=1; i<l; i++)
966 4546956 : if (!dvdii(ZMrow_ZC_mul(mul,x,i), p)) return gc_bool(av,0);
967 517417 : return gc_bool(av,1);
968 : }
969 :
970 : int
971 357 : pr_equal(GEN P, GEN Q)
972 : {
973 357 : GEN gQ, p = pr_get_p(P);
974 357 : long e = pr_get_e(P), f = pr_get_f(P), n;
975 357 : if (!equalii(p, pr_get_p(Q)) || e != pr_get_e(Q) || f != pr_get_f(Q))
976 336 : return 0;
977 21 : gQ = pr_get_gen(Q); n = lg(gQ)-1;
978 21 : if (2*e*f > n) return 1; /* room for only one such pr */
979 14 : return ZV_equal(pr_get_gen(P), gQ) || ZC_prdvd(gQ, P);
980 : }
981 :
982 : GEN
983 420735 : famat_nfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
984 : {
985 420735 : pari_sp av = avma;
986 420735 : GEN P = gel(x,1), E = gel(x,2), V = gen_0, y = NULL;
987 420735 : long l = lg(P), simplify = 0, i;
988 420735 : if (py) { *py = gen_1; y = cgetg(l, t_COL); }
989 :
990 2259155 : for (i = 1; i < l; i++)
991 : {
992 1838420 : GEN e = gel(E,i);
993 : long v;
994 1838420 : if (!signe(e))
995 : {
996 7 : if (py) gel(y,i) = gen_1;
997 7 : simplify = 1; continue;
998 : }
999 1838413 : v = nfvalrem(nf, gel(P,i), pr, py? &gel(y,i): NULL);
1000 1838413 : if (v == LONG_MAX) { set_avma(av); if (py) *py = gen_0; return mkoo(); }
1001 1838413 : V = addmulii(V, stoi(v), e);
1002 : }
1003 420735 : if (!py) V = gerepileuptoint(av, V);
1004 : else
1005 : {
1006 56 : y = mkmat2(y, gel(x,2));
1007 56 : if (simplify) y = famat_remove_trivial(y);
1008 56 : gerepileall(av, 2, &V, &y); *py = y;
1009 : }
1010 420735 : return V;
1011 : }
1012 : long
1013 5633086 : nfval(GEN nf, GEN x, GEN pr)
1014 : {
1015 5633086 : pari_sp av = avma;
1016 : long w, e;
1017 : GEN cx, p;
1018 :
1019 5633086 : if (gequal0(x)) return LONG_MAX;
1020 5619630 : nf = checknf(nf);
1021 5619627 : checkprid(pr);
1022 5619619 : p = pr_get_p(pr);
1023 5619614 : e = pr_get_e(pr);
1024 5619611 : x = nf_to_scalar_or_basis(nf, x);
1025 5619553 : if (typ(x) != t_COL) return e*Q_pval(x,p);
1026 2381027 : x = Q_primitive_part(x, &cx);
1027 2381045 : w = ZC_nfval(x,pr);
1028 2381025 : if (cx) w += e*Q_pval(cx,p);
1029 2381023 : return gc_long(av,w);
1030 : }
1031 :
1032 : /* want to write p^v = uniformizer^(e*v) * z^v, z coprime to pr */
1033 : /* z := tau^e / p^(e-1), algebraic integer coprime to pr; return z^v */
1034 : static GEN
1035 973413 : powp(GEN nf, GEN pr, long v)
1036 : {
1037 : GEN b, z;
1038 : long e;
1039 973413 : if (!v) return gen_1;
1040 446810 : b = pr_get_tau(pr);
1041 446810 : if (typ(b) == t_INT) return gen_1;
1042 131320 : e = pr_get_e(pr);
1043 131320 : z = gel(b,1);
1044 131320 : if (e != 1) z = gdiv(nfpow_u(nf, z, e), powiu(pr_get_p(pr),e-1));
1045 131320 : if (v < 0) { v = -v; z = nfinv(nf, z); }
1046 131320 : if (v != 1) z = nfpow_u(nf, z, v);
1047 131320 : return z;
1048 : }
1049 : long
1050 3662671 : nfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
1051 : {
1052 3662671 : pari_sp av = avma;
1053 : long w, e;
1054 : GEN cx, p, t;
1055 :
1056 3662671 : if (!py) return nfval(nf,x,pr);
1057 1810895 : if (gequal0(x)) { *py = gen_0; return LONG_MAX; }
1058 1810838 : nf = checknf(nf);
1059 1810837 : checkprid(pr);
1060 1810837 : p = pr_get_p(pr);
1061 1810837 : e = pr_get_e(pr);
1062 1810836 : x = nf_to_scalar_or_basis(nf, x);
1063 1810837 : if (typ(x) != t_COL) {
1064 557851 : w = Q_pvalrem(x,p, py);
1065 557851 : if (!w) { *py = gerepilecopy(av, x); return 0; }
1066 349272 : *py = gerepileupto(av, gmul(powp(nf, pr, w), *py));
1067 349272 : return e*w;
1068 : }
1069 1252986 : x = Q_primitive_part(x, &cx);
1070 1252990 : w = ZC_nfvalrem(x,pr, py);
1071 1252978 : if (cx)
1072 : {
1073 624141 : long v = Q_pvalrem(cx,p, &t);
1074 624141 : *py = nfmul(nf, *py, gmul(powp(nf,pr,v), t));
1075 624141 : *py = gerepileupto(av, *py);
1076 624141 : w += e*v;
1077 : }
1078 : else
1079 628837 : *py = gerepilecopy(av, *py);
1080 1252993 : return w;
1081 : }
1082 : GEN
1083 15015 : gpnfvalrem(GEN nf, GEN x, GEN pr, GEN *py)
1084 : {
1085 : long v;
1086 15015 : if (is_famat(x)) return famat_nfvalrem(nf, x, pr, py);
1087 15008 : v = nfvalrem(nf,x,pr,py);
1088 15008 : return v == LONG_MAX? mkoo(): stoi(v);
1089 : }
1090 :
1091 : /* true nf */
1092 : GEN
1093 335852 : coltoalg(GEN nf, GEN x)
1094 : {
1095 335852 : return mkpolmod( nf_to_scalar_or_alg(nf, x), nf_get_pol(nf) );
1096 : }
1097 :
1098 : GEN
1099 406276 : basistoalg(GEN nf, GEN x)
1100 : {
1101 : GEN T;
1102 :
1103 406276 : nf = checknf(nf);
1104 406276 : switch(typ(x))
1105 : {
1106 329587 : case t_COL: {
1107 329587 : pari_sp av = avma;
1108 329587 : return gerepilecopy(av, coltoalg(nf, x));
1109 : }
1110 40768 : case t_POLMOD:
1111 40768 : T = nf_get_pol(nf);
1112 40768 : if (!RgX_equal_var(T,gel(x,1)))
1113 0 : pari_err_MODULUS("basistoalg", T,gel(x,1));
1114 40768 : return gcopy(x);
1115 6321 : case t_POL:
1116 6321 : T = nf_get_pol(nf);
1117 6321 : if (varn(T) != varn(x)) pari_err_VAR("basistoalg",x,T);
1118 6314 : retmkpolmod(RgX_rem(x, T), ZX_copy(T));
1119 29600 : case t_INT:
1120 : case t_FRAC:
1121 29600 : T = nf_get_pol(nf);
1122 29600 : retmkpolmod(gcopy(x), ZX_copy(T));
1123 0 : default:
1124 0 : pari_err_TYPE("basistoalg",x);
1125 : return NULL; /* LCOV_EXCL_LINE */
1126 : }
1127 : }
1128 :
1129 : /* true nf, x a t_POL */
1130 : static GEN
1131 4590183 : pol_to_scalar_or_basis(GEN nf, GEN x)
1132 : {
1133 4590183 : GEN T = nf_get_pol(nf);
1134 4590177 : long l = lg(x);
1135 4590177 : if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_basis", x,T);
1136 4590072 : if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
1137 4590072 : if (l == 2) return gen_0;
1138 3578117 : if (l == 3)
1139 : {
1140 839211 : x = gel(x,2);
1141 839211 : if (!is_rational_t(typ(x))) pari_err_TYPE("nf_to_scalar_or_basis",x);
1142 839204 : return x;
1143 : }
1144 2738906 : return poltobasis(nf,x);
1145 : }
1146 : /* Assume nf is a genuine nf. */
1147 : GEN
1148 162235358 : nf_to_scalar_or_basis(GEN nf, GEN x)
1149 : {
1150 162235358 : switch(typ(x))
1151 : {
1152 97667361 : case t_INT: case t_FRAC:
1153 97667361 : return x;
1154 565079 : case t_POLMOD:
1155 565079 : x = checknfelt_mod(nf,x,"nf_to_scalar_or_basis");
1156 564943 : switch(typ(x))
1157 : {
1158 85848 : case t_INT: case t_FRAC: return x;
1159 479095 : case t_POL: return pol_to_scalar_or_basis(nf,x);
1160 : }
1161 0 : break;
1162 4111089 : case t_POL: return pol_to_scalar_or_basis(nf,x);
1163 59895619 : case t_COL:
1164 59895619 : if (lg(x)-1 != nf_get_degree(nf)) break;
1165 59895319 : return QV_isscalar(x)? gel(x,1): x;
1166 : }
1167 96 : pari_err_TYPE("nf_to_scalar_or_basis",x);
1168 : return NULL; /* LCOV_EXCL_LINE */
1169 : }
1170 : /* Let x be a polynomial with coefficients in Q or nf. Return the same
1171 : * polynomial with coefficients expressed as vectors (on the integral basis).
1172 : * No consistency checks, not memory-clean. */
1173 : GEN
1174 29224 : RgX_to_nfX(GEN nf, GEN x)
1175 237513 : { pari_APPLY_pol_normalized(nf_to_scalar_or_basis(nf, gel(x,i))); }
1176 :
1177 : /* Assume nf is a genuine nf. */
1178 : GEN
1179 4825145 : nf_to_scalar_or_alg(GEN nf, GEN x)
1180 : {
1181 4825145 : switch(typ(x))
1182 : {
1183 85258 : case t_INT: case t_FRAC:
1184 85258 : return x;
1185 427 : case t_POLMOD:
1186 427 : x = checknfelt_mod(nf,x,"nf_to_scalar_or_alg");
1187 427 : if (typ(x) != t_POL) return x;
1188 : /* fall through */
1189 : case t_POL:
1190 : {
1191 5334 : GEN T = nf_get_pol(nf);
1192 5334 : long l = lg(x);
1193 5334 : if (varn(x) != varn(T)) pari_err_VAR("nf_to_scalar_or_alg", x,T);
1194 5334 : if (l >= lg(T)) { x = RgX_rem(x, T); l = lg(x); }
1195 5334 : if (l == 2) return gen_0;
1196 5334 : if (l == 3) return gel(x,2);
1197 3794 : return x;
1198 : }
1199 4734509 : case t_COL:
1200 : {
1201 : GEN dx;
1202 4734509 : if (lg(x)-1 != nf_get_degree(nf)) break;
1203 9374306 : if (QV_isscalar(x)) return gel(x,1);
1204 4639622 : x = Q_remove_denom(x, &dx);
1205 4639668 : x = RgV_RgC_mul(nf_get_zkprimpart(nf), x);
1206 4639800 : dx = mul_denom(dx, nf_get_zkden(nf));
1207 4639762 : return gdiv(x,dx);
1208 : }
1209 : }
1210 54 : pari_err_TYPE("nf_to_scalar_or_alg",x);
1211 : return NULL; /* LCOV_EXCL_LINE */
1212 : }
1213 :
1214 : /* gmul(A, RgX_to_RgC(x)), A t_MAT of compatible dimensions */
1215 : GEN
1216 1365 : RgM_RgX_mul(GEN A, GEN x)
1217 : {
1218 1365 : long i, l = lg(x)-1;
1219 : GEN z;
1220 1365 : if (l == 1) return zerocol(nbrows(A));
1221 1351 : z = gmul(gel(x,2), gel(A,1));
1222 2555 : for (i = 2; i < l; i++)
1223 1204 : if (!gequal0(gel(x,i+1))) z = gadd(z, gmul(gel(x,i+1), gel(A,i)));
1224 1351 : return z;
1225 : }
1226 : GEN
1227 10365545 : ZM_ZX_mul(GEN A, GEN x)
1228 : {
1229 10365545 : long i, l = lg(x)-1;
1230 : GEN z;
1231 10365545 : if (l == 1) return zerocol(nbrows(A));
1232 10364411 : z = ZC_Z_mul(gel(A,1), gel(x,2));
1233 32339757 : for (i = 2; i < l ; i++)
1234 21978083 : if (signe(gel(x,i+1))) z = ZC_add(z, ZC_Z_mul(gel(A,i), gel(x,i+1)));
1235 10361674 : return z;
1236 : }
1237 : /* x a t_POL, nf a genuine nf. No garbage collecting. No check. */
1238 : GEN
1239 9764813 : poltobasis(GEN nf, GEN x)
1240 : {
1241 9764813 : GEN d, T = nf_get_pol(nf);
1242 9764786 : if (varn(x) != varn(T)) pari_err_VAR( "poltobasis", x,T);
1243 9764653 : if (degpol(x) >= degpol(T)) x = RgX_rem(x,T);
1244 9764587 : x = Q_remove_denom(x, &d);
1245 9764906 : if (!RgX_is_ZX(x)) pari_err_TYPE("poltobasis",x);
1246 9764827 : x = ZM_ZX_mul(nf_get_invzk(nf), x);
1247 9762714 : if (d) x = RgC_Rg_div(x, d);
1248 9762780 : return x;
1249 : }
1250 :
1251 : GEN
1252 952884 : algtobasis(GEN nf, GEN x)
1253 : {
1254 : pari_sp av;
1255 :
1256 952884 : nf = checknf(nf);
1257 952884 : switch(typ(x))
1258 : {
1259 140561 : case t_POLMOD:
1260 140561 : if (!RgX_equal_var(nf_get_pol(nf),gel(x,1)))
1261 7 : pari_err_MODULUS("algtobasis", nf_get_pol(nf),gel(x,1));
1262 140554 : x = gel(x,2);
1263 140554 : switch(typ(x))
1264 : {
1265 11340 : case t_INT:
1266 11340 : case t_FRAC: return scalarcol(x, nf_get_degree(nf));
1267 129214 : case t_POL:
1268 129214 : av = avma;
1269 129214 : return gerepileupto(av,poltobasis(nf,x));
1270 : }
1271 0 : break;
1272 :
1273 250762 : case t_POL:
1274 250762 : av = avma;
1275 250762 : return gerepileupto(av,poltobasis(nf,x));
1276 :
1277 83668 : case t_COL:
1278 83668 : if (!RgV_is_QV(x)) pari_err_TYPE("nfalgtobasis",x);
1279 83661 : if (lg(x)-1 != nf_get_degree(nf)) pari_err_DIM("nfalgtobasis");
1280 83661 : return gcopy(x);
1281 :
1282 477895 : case t_INT:
1283 477895 : case t_FRAC: return scalarcol(x, nf_get_degree(nf));
1284 : }
1285 0 : pari_err_TYPE("algtobasis",x);
1286 : return NULL; /* LCOV_EXCL_LINE */
1287 : }
1288 :
1289 : GEN
1290 55104 : rnfbasistoalg(GEN rnf,GEN x)
1291 : {
1292 55104 : const char *f = "rnfbasistoalg";
1293 : long lx, i;
1294 55104 : pari_sp av = avma;
1295 : GEN z, nf, R, T;
1296 :
1297 55104 : checkrnf(rnf);
1298 55104 : nf = rnf_get_nf(rnf);
1299 55104 : T = nf_get_pol(nf);
1300 55104 : R = QXQX_to_mod_shallow(rnf_get_pol(rnf), T);
1301 55104 : switch(typ(x))
1302 : {
1303 875 : case t_COL:
1304 875 : z = cgetg_copy(x, &lx);
1305 2597 : for (i=1; i<lx; i++)
1306 : {
1307 1778 : GEN c = nf_to_scalar_or_alg(nf, gel(x,i));
1308 1722 : if (typ(c) == t_POL) c = mkpolmod(c,T);
1309 1722 : gel(z,i) = c;
1310 : }
1311 819 : z = RgV_RgC_mul(gel(rnf_get_zk(rnf),1), z);
1312 735 : return gerepileupto(av, gmodulo(z,R));
1313 :
1314 34965 : case t_POLMOD:
1315 34965 : x = polmod_nffix(f, rnf, x, 0);
1316 34692 : if (typ(x) != t_POL) break;
1317 16046 : retmkpolmod(RgX_copy(x), RgX_copy(R));
1318 1582 : case t_POL:
1319 1582 : if (varn(x) == varn(T)) { RgX_check_QX(x,f); x = gmodulo(x,T); break; }
1320 1337 : if (varn(x) == varn(R))
1321 : {
1322 1281 : x = RgX_nffix(f,nf_get_pol(nf),x,0);
1323 1281 : return gmodulo(x, R);
1324 : }
1325 56 : pari_err_VAR(f, x,R);
1326 : }
1327 36517 : retmkpolmod(scalarpol(x, varn(R)), RgX_copy(R));
1328 : }
1329 :
1330 : GEN
1331 2653 : matbasistoalg(GEN nf,GEN x)
1332 : {
1333 : long i, j, li, lx;
1334 2653 : GEN z = cgetg_copy(x, &lx);
1335 :
1336 2653 : if (lx == 1) return z;
1337 2646 : switch(typ(x))
1338 : {
1339 77 : case t_VEC: case t_COL:
1340 273 : for (i=1; i<lx; i++) gel(z,i) = basistoalg(nf, gel(x,i));
1341 77 : return z;
1342 2569 : case t_MAT: break;
1343 0 : default: pari_err_TYPE("matbasistoalg",x);
1344 : }
1345 2569 : li = lgcols(x);
1346 9352 : for (j=1; j<lx; j++)
1347 : {
1348 6783 : GEN c = cgetg(li,t_COL), xj = gel(x,j);
1349 6783 : gel(z,j) = c;
1350 30709 : for (i=1; i<li; i++) gel(c,i) = basistoalg(nf, gel(xj,i));
1351 : }
1352 2569 : return z;
1353 : }
1354 :
1355 : GEN
1356 31947 : matalgtobasis(GEN nf,GEN x)
1357 : {
1358 : long i, j, li, lx;
1359 31947 : GEN z = cgetg_copy(x, &lx);
1360 :
1361 31947 : if (lx == 1) return z;
1362 31485 : switch(typ(x))
1363 : {
1364 31478 : case t_VEC: case t_COL:
1365 82567 : for (i=1; i<lx; i++) gel(z,i) = algtobasis(nf, gel(x,i));
1366 31478 : return z;
1367 7 : case t_MAT: break;
1368 0 : default: pari_err_TYPE("matalgtobasis",x);
1369 : }
1370 7 : li = lgcols(x);
1371 14 : for (j=1; j<lx; j++)
1372 : {
1373 7 : GEN c = cgetg(li,t_COL), xj = gel(x,j);
1374 7 : gel(z,j) = c;
1375 21 : for (i=1; i<li; i++) gel(c,i) = algtobasis(nf, gel(xj,i));
1376 : }
1377 7 : return z;
1378 : }
1379 : GEN
1380 11177 : RgM_to_nfM(GEN nf,GEN x)
1381 : {
1382 : long i, j, li, lx;
1383 11177 : GEN z = cgetg_copy(x, &lx);
1384 :
1385 11177 : if (lx == 1) return z;
1386 11177 : li = lgcols(x);
1387 82810 : for (j=1; j<lx; j++)
1388 : {
1389 71633 : GEN c = cgetg(li,t_COL), xj = gel(x,j);
1390 71633 : gel(z,j) = c;
1391 466633 : for (i=1; i<li; i++) gel(c,i) = nf_to_scalar_or_basis(nf, gel(xj,i));
1392 : }
1393 11177 : return z;
1394 : }
1395 : GEN
1396 149392 : RgC_to_nfC(GEN nf, GEN x)
1397 913026 : { pari_APPLY_type(t_COL, nf_to_scalar_or_basis(nf, gel(x,i))) }
1398 :
1399 : /* x a t_POLMOD, supposedly in rnf = K[z]/(T), K = Q[y]/(Tnf) */
1400 : GEN
1401 169037 : polmod_nffix(const char *f, GEN rnf, GEN x, int lift)
1402 169037 : { return polmod_nffix2(f, rnf_get_nfpol(rnf), rnf_get_pol(rnf), x,lift); }
1403 : GEN
1404 169128 : polmod_nffix2(const char *f, GEN T, GEN R, GEN x, int lift)
1405 : {
1406 169128 : if (RgX_equal_var(gel(x,1), R))
1407 : {
1408 141036 : x = gel(x,2);
1409 141036 : if (typ(x) == t_POL && varn(x) == varn(R))
1410 : {
1411 106098 : x = RgX_nffix(f, T, x, lift);
1412 106098 : switch(lg(x))
1413 : {
1414 5831 : case 2: return gen_0;
1415 13604 : case 3: return gel(x,2);
1416 : }
1417 86663 : return x;
1418 : }
1419 : }
1420 63030 : return Rg_nffix(f, T, x, lift);
1421 : }
1422 : GEN
1423 1428 : rnfalgtobasis(GEN rnf,GEN x)
1424 : {
1425 1428 : const char *f = "rnfalgtobasis";
1426 1428 : pari_sp av = avma;
1427 : GEN T, R;
1428 :
1429 1428 : checkrnf(rnf);
1430 1428 : R = rnf_get_pol(rnf);
1431 1428 : T = rnf_get_nfpol(rnf);
1432 1428 : switch(typ(x))
1433 : {
1434 98 : case t_COL:
1435 98 : if (lg(x)-1 != rnf_get_degree(rnf)) pari_err_DIM(f);
1436 49 : x = RgV_nffix(f, T, x, 0);
1437 42 : return gerepilecopy(av, x);
1438 :
1439 1162 : case t_POLMOD:
1440 1162 : x = polmod_nffix(f, rnf, x, 0);
1441 1057 : if (typ(x) != t_POL) break;
1442 714 : return gerepileupto(av, RgM_RgX_mul(rnf_get_invzk(rnf), x));
1443 112 : case t_POL:
1444 112 : if (varn(x) == varn(T))
1445 : {
1446 42 : RgX_check_QX(x,f);
1447 28 : if (degpol(x) >= degpol(T)) x = RgX_rem(x,T);
1448 28 : x = mkpolmod(x,T); break;
1449 : }
1450 70 : x = RgX_nffix(f, T, x, 0);
1451 56 : if (degpol(x) >= degpol(R)) x = RgX_rem(x, R);
1452 56 : return gerepileupto(av, RgM_RgX_mul(rnf_get_invzk(rnf), x));
1453 : }
1454 427 : return gerepileupto(av, scalarcol(x, rnf_get_degree(rnf)));
1455 : }
1456 :
1457 : /* Given a and b in nf, gives an algebraic integer y in nf such that a-b.y
1458 : * is "small" */
1459 : GEN
1460 259 : nfdiveuc(GEN nf, GEN a, GEN b)
1461 : {
1462 259 : pari_sp av = avma;
1463 259 : a = nfdiv(nf,a,b);
1464 259 : return gerepileupto(av, ground(a));
1465 : }
1466 :
1467 : /* Given a and b in nf, gives a "small" algebraic integer r in nf
1468 : * of the form a-b.y */
1469 : GEN
1470 259 : nfmod(GEN nf, GEN a, GEN b)
1471 : {
1472 259 : pari_sp av = avma;
1473 259 : GEN p1 = gneg_i(nfmul(nf,b,ground(nfdiv(nf,a,b))));
1474 259 : return gerepileupto(av, nfadd(nf,a,p1));
1475 : }
1476 :
1477 : /* Given a and b in nf, gives a two-component vector [y,r] in nf such
1478 : * that r=a-b.y is "small". */
1479 : GEN
1480 259 : nfdivrem(GEN nf, GEN a, GEN b)
1481 : {
1482 259 : pari_sp av = avma;
1483 259 : GEN p1,z, y = ground(nfdiv(nf,a,b));
1484 :
1485 259 : p1 = gneg_i(nfmul(nf,b,y));
1486 259 : z = cgetg(3,t_VEC);
1487 259 : gel(z,1) = gcopy(y);
1488 259 : gel(z,2) = nfadd(nf,a,p1); return gerepileupto(av, z);
1489 : }
1490 :
1491 : /*************************************************************************/
1492 : /** **/
1493 : /** LOGARITHMIC EMBEDDINGS **/
1494 : /** **/
1495 : /*************************************************************************/
1496 :
1497 : static int
1498 4612132 : low_prec(GEN x)
1499 : {
1500 4612132 : switch(typ(x))
1501 : {
1502 0 : case t_INT: return !signe(x);
1503 4612132 : case t_REAL: return !signe(x) || realprec(x) <= DEFAULTPREC;
1504 0 : default: return 0;
1505 : }
1506 : }
1507 :
1508 : static GEN
1509 23138 : cxlog_1(GEN nf) { return zerocol(lg(nf_get_roots(nf))-1); }
1510 : static GEN
1511 532 : cxlog_m1(GEN nf, long prec)
1512 : {
1513 532 : long i, l = lg(nf_get_roots(nf)), r1 = nf_get_r1(nf);
1514 532 : GEN v = cgetg(l, t_COL), p, P;
1515 532 : p = mppi(prec); P = mkcomplex(gen_0, p);
1516 1235 : for (i = 1; i <= r1; i++) gel(v,i) = P; /* IPi*/
1517 532 : if (i < l) P = gmul2n(P,1);
1518 1122 : for ( ; i < l; i++) gel(v,i) = P; /* 2IPi */
1519 532 : return v;
1520 : }
1521 : static GEN
1522 1715150 : ZC_cxlog(GEN nf, GEN x, long prec)
1523 : {
1524 : long i, l, r1;
1525 : GEN v;
1526 1715150 : x = RgM_RgC_mul(nf_get_M(nf), Q_primpart(x));
1527 1715150 : l = lg(x); r1 = nf_get_r1(nf);
1528 4330780 : for (i = 1; i <= r1; i++)
1529 2615630 : if (low_prec(gel(x,i))) return NULL;
1530 3514725 : for ( ; i < l; i++)
1531 1799575 : if (low_prec(gnorm(gel(x,i)))) return NULL;
1532 1715150 : v = cgetg(l,t_COL);
1533 4330780 : for (i = 1; i <= r1; i++) gel(v,i) = glog(gel(x,i),prec);
1534 3514723 : for ( ; i < l; i++) gel(v,i) = gmul2n(glog(gel(x,i),prec),1);
1535 1715148 : return v;
1536 : }
1537 : static GEN
1538 223285 : famat_cxlog(GEN nf, GEN fa, long prec)
1539 : {
1540 223285 : GEN G, E, y = NULL;
1541 : long i, l;
1542 :
1543 223285 : if (typ(fa) != t_MAT) pari_err_TYPE("famat_cxlog",fa);
1544 223285 : if (lg(fa) == 1) return cxlog_1(nf);
1545 223285 : G = gel(fa,1);
1546 223285 : E = gel(fa,2); l = lg(E);
1547 1119764 : for (i = 1; i < l; i++)
1548 : {
1549 896479 : GEN t, e = gel(E,i), x = nf_to_scalar_or_basis(nf, gel(G,i));
1550 : /* multiplicative arch would be better (save logs), but exponents overflow
1551 : * [ could keep track of expo separately, but not worth it ] */
1552 896479 : switch(typ(x))
1553 : { /* ignore positive rationals */
1554 16440 : case t_FRAC: x = gel(x,1); /* fall through */
1555 266500 : case t_INT: if (signe(x) > 0) continue;
1556 84 : if (!mpodd(e)) continue;
1557 28 : t = cxlog_m1(nf, prec); /* we probably should not reach this line */
1558 28 : break;
1559 629979 : default: /* t_COL */
1560 629979 : t = ZC_cxlog(nf,x,prec); if (!t) return NULL;
1561 629979 : t = RgC_Rg_mul(t, e);
1562 : }
1563 630007 : y = y? RgV_add(y,t): t;
1564 : }
1565 223285 : return y ? y: cxlog_1(nf);
1566 : }
1567 : /* Archimedean components: [e_i Log( sigma_i(X) )], where X = primpart(x),
1568 : * and e_i = 1 (resp 2.) for i <= R1 (resp. > R1) */
1569 : GEN
1570 1309604 : nf_cxlog(GEN nf, GEN x, long prec)
1571 : {
1572 1309604 : if (typ(x) == t_MAT) return famat_cxlog(nf,x,prec);
1573 1086319 : x = nf_to_scalar_or_basis(nf,x);
1574 1086319 : switch(typ(x))
1575 : {
1576 0 : case t_FRAC: x = gel(x,1); /* fall through */
1577 1148 : case t_INT:
1578 1148 : return signe(x) > 0? cxlog_1(nf): cxlog_m1(nf, prec);
1579 1085171 : default:
1580 1085171 : return ZC_cxlog(nf, x, prec);
1581 : }
1582 : }
1583 : GEN
1584 97 : nfV_cxlog(GEN nf, GEN x, long prec)
1585 : {
1586 : long i, l;
1587 97 : GEN v = cgetg_copy(x, &l);
1588 167 : for (i = 1; i < l; i++)
1589 70 : if (!(gel(v,i) = nf_cxlog(nf, gel(x,i), prec))) return NULL;
1590 97 : return v;
1591 : }
1592 :
1593 : static GEN
1594 15239 : scalar_logembed(GEN nf, GEN u, GEN *emb)
1595 : {
1596 : GEN v, logu;
1597 15239 : long i, s = signe(u), RU = lg(nf_get_roots(nf))-1, R1 = nf_get_r1(nf);
1598 :
1599 15239 : if (!s) pari_err_DOMAIN("nflogembed","argument","=",gen_0,u);
1600 15239 : v = cgetg(RU+1, t_COL); logu = logr_abs(u);
1601 17234 : for (i = 1; i <= R1; i++) gel(v,i) = logu;
1602 15239 : if (i <= RU)
1603 : {
1604 14350 : GEN logu2 = shiftr(logu,1);
1605 55839 : for ( ; i <= RU; i++) gel(v,i) = logu2;
1606 : }
1607 15239 : if (emb) *emb = const_col(RU, u);
1608 15239 : return v;
1609 : }
1610 :
1611 : static GEN
1612 1309 : famat_logembed(GEN nf,GEN x,GEN *emb,long prec)
1613 : {
1614 1309 : GEN A, M, T, a, t, g = gel(x,1), e = gel(x,2);
1615 1309 : long i, l = lg(e);
1616 :
1617 1309 : if (l == 1) return scalar_logembed(nf, real_1(prec), emb);
1618 1309 : A = NULL; T = emb? cgetg(l, t_COL): NULL;
1619 1309 : if (emb) *emb = M = mkmat2(T, e);
1620 62132 : for (i = 1; i < l; i++)
1621 : {
1622 60823 : a = nflogembed(nf, gel(g,i), &t, prec);
1623 60823 : if (!a) return NULL;
1624 60823 : a = RgC_Rg_mul(a, gel(e,i));
1625 60823 : A = A? RgC_add(A, a): a;
1626 60823 : if (emb) gel(T,i) = t;
1627 : }
1628 1309 : return A;
1629 : }
1630 :
1631 : /* Get archimedean components: [e_i log( | sigma_i(x) | )], with e_i = 1
1632 : * (resp 2.) for i <= R1 (resp. > R1) and set emb to the embeddings of x.
1633 : * Return NULL if precision problem */
1634 : GEN
1635 98707 : nflogembed(GEN nf, GEN x, GEN *emb, long prec)
1636 : {
1637 : long i, l, r1;
1638 : GEN v, t;
1639 :
1640 98707 : if (typ(x) == t_MAT) return famat_logembed(nf,x,emb,prec);
1641 97398 : x = nf_to_scalar_or_basis(nf,x);
1642 97398 : if (typ(x) != t_COL) return scalar_logembed(nf, gtofp(x,prec), emb);
1643 82159 : x = RgM_RgC_mul(nf_get_M(nf), x);
1644 82158 : l = lg(x); r1 = nf_get_r1(nf); v = cgetg(l,t_COL);
1645 109087 : for (i = 1; i <= r1; i++)
1646 : {
1647 26928 : t = gabs(gel(x,i),prec); if (low_prec(t)) return NULL;
1648 26927 : gel(v,i) = glog(t,prec);
1649 : }
1650 252160 : for ( ; i < l; i++)
1651 : {
1652 170002 : t = gnorm(gel(x,i)); if (low_prec(t)) return NULL;
1653 170001 : gel(v,i) = glog(t,prec);
1654 : }
1655 82158 : if (emb) *emb = x;
1656 82158 : return v;
1657 : }
1658 :
1659 : /*************************************************************************/
1660 : /** **/
1661 : /** REAL EMBEDDINGS **/
1662 : /** **/
1663 : /*************************************************************************/
1664 : static GEN
1665 486493 : sarch_get_cyc(GEN sarch) { return gel(sarch,1); }
1666 : static GEN
1667 1555160 : sarch_get_archp(GEN sarch) { return gel(sarch,2); }
1668 : static GEN
1669 608246 : sarch_get_MI(GEN sarch) { return gel(sarch,3); }
1670 : static GEN
1671 608246 : sarch_get_lambda(GEN sarch) { return gel(sarch,4); }
1672 : static GEN
1673 608246 : sarch_get_F(GEN sarch) { return gel(sarch,5); }
1674 :
1675 : /* true nf, x non-zero algebraic integer; return number of positive real roots
1676 : * of char_x */
1677 : static long
1678 909909 : num_positive(GEN nf, GEN x)
1679 : {
1680 909909 : GEN T = nf_get_pol(nf), B, charx;
1681 909907 : long dnf, vnf, N, r1 = nf_get_r1(nf);
1682 909907 : x = nf_to_scalar_or_alg(nf, x);
1683 909918 : if (typ(x) != t_POL) return (signe(x) < 0)? 0: degpol(T);
1684 : /* x not a scalar */
1685 904550 : if (r1 == 1)
1686 : {
1687 31346 : long s = signe(ZX_resultant(T, Q_primpart(x)));
1688 31346 : return s > 0? 1: 0;
1689 : }
1690 873204 : charx = ZXQ_charpoly(x, T, 0);
1691 873205 : charx = ZX_radical(charx);
1692 873200 : N = degpol(T) / degpol(charx);
1693 : /* real places are unramified ? */
1694 873198 : if (N == 1 || ZX_sturm(charx) * N == r1)
1695 872602 : return ZX_sturmpart(charx, mkvec2(gen_0,mkoo())) * N;
1696 : /* painful case, multiply by random square until primitive */
1697 596 : dnf = nf_get_degree(nf);
1698 596 : vnf = varn(T);
1699 596 : B = int2n(10);
1700 : for(;;)
1701 0 : {
1702 596 : GEN y = RgXQ_sqr(random_FpX(dnf, vnf, B), T);
1703 596 : y = RgXQ_mul(x, y, T);
1704 596 : charx = ZXQ_charpoly(y, T, 0);
1705 596 : if (ZX_is_squarefree(charx))
1706 596 : return ZX_sturmpart(charx, mkvec2(gen_0,mkoo()));
1707 : }
1708 : }
1709 :
1710 : /* x a QC: return sigma_k(x) where 1 <= k <= r1+r2; correct but inefficient
1711 : * if x in Q. M = nf_get_M(nf) */
1712 : static GEN
1713 2140 : nfembed_i(GEN M, GEN x, long k)
1714 : {
1715 2140 : long i, l = lg(M);
1716 2140 : GEN z = gel(x,1);
1717 24380 : for (i = 2; i < l; i++) z = gadd(z, gmul(gcoeff(M,k,i), gel(x,i)));
1718 2140 : return z;
1719 : }
1720 : GEN
1721 0 : nfembed(GEN nf, GEN x, long k)
1722 : {
1723 0 : pari_sp av = avma;
1724 0 : nf = checknf(nf);
1725 0 : x = nf_to_scalar_or_basis(nf,x);
1726 0 : if (typ(x) != t_COL) return gerepilecopy(av, x);
1727 0 : return gerepileupto(av, nfembed_i(nf_get_M(nf),x,k));
1728 : }
1729 :
1730 : /* x a ZC */
1731 : static GEN
1732 74704 : zk_embed(GEN M, GEN x, long k)
1733 : {
1734 74704 : long i, l = lg(x);
1735 74704 : GEN z = gel(x,1); /* times M[k,1], which is 1 */
1736 185958 : for (i = 2; i < l; i++) z = mpadd(z, mpmul(gcoeff(M,k,i), gel(x,i)));
1737 74705 : return z;
1738 : }
1739 :
1740 : /* check that signs[i..#signs] == s; signs = NULL encodes "totally positive" */
1741 : static int
1742 24930 : oksigns(long l, GEN signs, long i, long s)
1743 : {
1744 24930 : if (!signs) return s == 0;
1745 27042 : for (; i < l; i++)
1746 19880 : if (signs[i] != s) return 0;
1747 7162 : return 1;
1748 : }
1749 :
1750 : /* true nf, x a ZC (primitive for efficiency) which is not a scalar */
1751 : static int
1752 80573 : nfchecksigns_i(GEN nf, GEN x, GEN signs, GEN archp)
1753 : {
1754 80573 : long i, np, npc, l = lg(archp), r1 = nf_get_r1(nf);
1755 : GEN sarch;
1756 :
1757 80573 : if (r1 == 0) return 1;
1758 80180 : np = num_positive(nf, x);
1759 80182 : if (np == 0) return oksigns(l, signs, 1, 1);
1760 71125 : if (np == r1) return oksigns(l, signs, 1, 0);
1761 55252 : sarch = nfarchstar(nf, NULL, identity_perm(r1));
1762 63805 : for (i = 1, npc = 0; i < l; i++)
1763 : {
1764 63569 : GEN xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
1765 : long ni, s;
1766 63569 : xi = Q_primpart(xi);
1767 63569 : ni = num_positive(nf, nfmuli(nf,x,xi));
1768 63569 : s = ni < np? 0: 1;
1769 63569 : if (s != (signs? signs[i]: 0)) return 0;
1770 24888 : if (!s) npc++; /* found a positive root */
1771 24888 : if (npc == np)
1772 : { /* found all positive roots */
1773 15752 : if (!signs) return i == l-1;
1774 8848 : for (i++; i < l; i++)
1775 4233 : if (signs[i] != 1) return 0;
1776 4615 : return 1;
1777 : }
1778 9136 : if (i - npc == r1 - np)
1779 : { /* found all negative roots */
1780 583 : if (!signs) return 1;
1781 625 : for (i++; i < l; i++)
1782 49 : if (signs[i]) return 0;
1783 576 : return 1;
1784 : }
1785 : }
1786 236 : return 1;
1787 : }
1788 : static void
1789 985 : pl_convert(GEN pl, GEN *psigns, GEN *parchp)
1790 : {
1791 985 : long i, j, l = lg(pl);
1792 985 : GEN signs = cgetg(l, t_VECSMALL);
1793 985 : GEN archp = cgetg(l, t_VECSMALL);
1794 3080 : for (i = j = 1; i < l; i++)
1795 : {
1796 2095 : if (!pl[i]) continue;
1797 1578 : archp[j] = i;
1798 1578 : signs[j] = (pl[i] < 0)? 1: 0;
1799 1578 : j++;
1800 : }
1801 985 : setlg(archp, j); *parchp = archp;
1802 985 : setlg(signs, j); *psigns = signs;
1803 985 : }
1804 : /* pl : requested signs for real embeddings, 0 = no sign constraint */
1805 : int
1806 15111 : nfchecksigns(GEN nf, GEN x, GEN pl)
1807 : {
1808 15111 : pari_sp av = avma;
1809 : GEN signs, archp;
1810 15111 : nf = checknf(nf);
1811 15111 : x = nf_to_scalar_or_basis(nf,x);
1812 15111 : if (typ(x) != t_COL)
1813 : {
1814 14126 : long i, l = lg(pl), s = gsigne(x);
1815 28259 : for (i = 1; i < l; i++)
1816 14133 : if (pl[i] && pl[i] != s) return gc_bool(av,0);
1817 14126 : return gc_bool(av,1);
1818 : }
1819 985 : pl_convert(pl, &signs, &archp);
1820 985 : return gc_bool(av, nfchecksigns_i(nf, x, signs, archp));
1821 : }
1822 :
1823 : /* signs = NULL: totally positive, else sign[i] = 0 (+) or 1 (-) */
1824 : static GEN
1825 608247 : get_C(GEN lambda, long l, GEN signs)
1826 : {
1827 : long i;
1828 : GEN C, mlambda;
1829 608247 : if (!signs) return const_vec(l-1, lambda);
1830 578497 : C = cgetg(l, t_COL); mlambda = gneg(lambda);
1831 2318062 : for (i = 1; i < l; i++) gel(C,i) = signs[i]? mlambda: lambda;
1832 578496 : return C;
1833 : }
1834 : /* signs = NULL: totally positive at archp.
1835 : * Assume that a t_COL x is not a scalar */
1836 : static GEN
1837 722158 : nfsetsigns(GEN nf, GEN signs, GEN x, GEN sarch)
1838 : {
1839 722158 : long i, l = lg(sarch_get_archp(sarch));
1840 722157 : GEN ex = NULL;
1841 : /* Is signature already correct ? */
1842 722157 : if (typ(x) != t_COL)
1843 : {
1844 642570 : long s = gsigne(x);
1845 642571 : if (!s) i = 1;
1846 642550 : else if (!signs)
1847 7427 : i = (s < 0)? 1: l;
1848 : else
1849 : {
1850 635123 : s = s < 0? 1: 0;
1851 1110881 : for (i = 1; i < l; i++)
1852 1032129 : if (signs[i] != s) break;
1853 : }
1854 642571 : if (i < l) ex = const_col(l-1, x);
1855 : }
1856 : else
1857 : { /* inefficient if x scalar, wrong if x = 0 */
1858 79587 : pari_sp av = avma;
1859 79587 : GEN cex, M = nf_get_M(nf), archp = sarch_get_archp(sarch);
1860 79590 : GEN xp = Q_primitive_part(x,&cex);
1861 79588 : if (nfchecksigns_i(nf, xp, signs, archp)) set_avma(av);
1862 : else
1863 : {
1864 51708 : ex = cgetg(l,t_COL);
1865 126412 : for (i = 1; i < l; i++) gel(ex,i) = zk_embed(M,xp,archp[i]);
1866 51709 : if (cex) ex = RgC_Rg_mul(ex, cex); /* put back content */
1867 : }
1868 : }
1869 722162 : if (ex)
1870 : { /* If no, fix it */
1871 608246 : GEN MI = sarch_get_MI(sarch), F = sarch_get_F(sarch);
1872 608246 : GEN lambda = sarch_get_lambda(sarch);
1873 608246 : GEN t = RgC_sub(get_C(lambda, l, signs), ex);
1874 608233 : t = grndtoi(RgM_RgC_mul(MI,t), NULL);
1875 608226 : if (lg(F) != 1) t = ZM_ZC_mul(F, t);
1876 608241 : x = typ(x) == t_COL? RgC_add(t, x): RgC_Rg_add(t, x);
1877 : }
1878 722147 : return x;
1879 : }
1880 : /* - true nf
1881 : * - sarch = nfarchstar(nf, F);
1882 : * - x encodes a vector of signs at arch.archp: either a t_VECSMALL
1883 : * (vector of signs as {0,1}-vector), NULL (totally positive at archp),
1884 : * or a nonzero number field element (replaced by its signature at archp);
1885 : * - y is a nonzero number field element
1886 : * Return z = y (mod F) with signs(y, archp) = signs(x) (a {0,1}-vector).
1887 : * Not stack-clean */
1888 : GEN
1889 753422 : set_sign_mod_divisor(GEN nf, GEN x, GEN y, GEN sarch)
1890 : {
1891 753422 : GEN archp = sarch_get_archp(sarch);
1892 753421 : if (lg(archp) == 1) return y;
1893 720115 : if (x && typ(x) != t_VECSMALL) x = nfsign_arch(nf, x, archp);
1894 720115 : return nfsetsigns(nf, x, nf_to_scalar_or_basis(nf,y), sarch);
1895 : }
1896 :
1897 : static GEN
1898 391602 : setsigns_init(GEN nf, GEN archp, GEN F, GEN DATA)
1899 : {
1900 391602 : GEN lambda, Mr = rowpermute(nf_get_M(nf), archp), MI = F? RgM_mul(Mr,F): Mr;
1901 391601 : lambda = gmul2n(matrixnorm(MI,DEFAULTPREC), -1);
1902 391599 : if (typ(lambda) != t_REAL) lambda = gmul(lambda, uutoQ(1001,1000));
1903 391602 : if (lg(archp) < lg(MI))
1904 : {
1905 75706 : GEN perm = gel(indexrank(MI), 2);
1906 75707 : if (!F) F = matid(nf_get_degree(nf));
1907 75707 : MI = vecpermute(MI, perm);
1908 75707 : F = vecpermute(F, perm);
1909 : }
1910 391603 : if (!F) F = cgetg(1,t_MAT);
1911 391603 : MI = RgM_inv(MI);
1912 391601 : return mkvec5(DATA, archp, MI, lambda, F);
1913 : }
1914 : /* F nonzero integral ideal in HNF (or NULL: Z_K), compute elements in 1+F
1915 : * whose sign matrix at archp is identity; archp in 'indices' format */
1916 : GEN
1917 567438 : nfarchstar(GEN nf, GEN F, GEN archp)
1918 : {
1919 567438 : long nba = lg(archp) - 1;
1920 567438 : if (!nba) return mkvec2(cgetg(1,t_VEC), archp);
1921 389558 : if (F && equali1(gcoeff(F,1,1))) F = NULL;
1922 389558 : if (F) F = idealpseudored(F, nf_get_roundG(nf));
1923 389550 : return setsigns_init(nf, archp, F, const_vec(nba, gen_2));
1924 : }
1925 :
1926 : /*************************************************************************/
1927 : /** **/
1928 : /** IDEALCHINESE **/
1929 : /** **/
1930 : /*************************************************************************/
1931 : static int
1932 5305 : isprfact(GEN x)
1933 : {
1934 : long i, l;
1935 : GEN L, E;
1936 5305 : if (typ(x) != t_MAT || lg(x) != 3) return 0;
1937 5305 : L = gel(x,1); l = lg(L);
1938 5305 : E = gel(x,2);
1939 16632 : for(i=1; i<l; i++)
1940 : {
1941 11327 : checkprid(gel(L,i));
1942 11327 : if (typ(gel(E,i)) != t_INT) return 0;
1943 : }
1944 5305 : return 1;
1945 : }
1946 :
1947 : /* initialize projectors mod pr[i]^e[i] for idealchinese */
1948 : static GEN
1949 5305 : pr_init(GEN nf, GEN fa, GEN w, GEN dw)
1950 : {
1951 5305 : GEN U, E, F, FZ, L = gel(fa,1), E0 = gel(fa,2);
1952 5305 : long i, r = lg(L);
1953 :
1954 5305 : if (w && lg(w) != r) pari_err_TYPE("idealchinese", w);
1955 5305 : if (r == 1 && !dw) return cgetg(1,t_VEC);
1956 5291 : E = leafcopy(E0); /* do not destroy fa[2] */
1957 16618 : for (i = 1; i < r; i++)
1958 11327 : if (signe(gel(E,i)) < 0) gel(E,i) = gen_0;
1959 5291 : F = factorbackprime(nf, L, E);
1960 5291 : if (dw)
1961 : {
1962 693 : F = ZM_Z_mul(F, dw);
1963 1596 : for (i = 1; i < r; i++)
1964 : {
1965 903 : GEN pr = gel(L,i);
1966 903 : long e = itos(gel(E0,i)), v = idealval(nf, dw, pr);
1967 903 : if (e >= 0)
1968 896 : gel(E,i) = addiu(gel(E,i), v);
1969 7 : else if (v + e <= 0)
1970 0 : F = idealmulpowprime(nf, F, pr, stoi(-v)); /* coprime to pr */
1971 : else
1972 : {
1973 7 : F = idealmulpowprime(nf, F, pr, stoi(e));
1974 7 : gel(E,i) = stoi(v + e);
1975 : }
1976 : }
1977 : }
1978 5291 : U = cgetg(r, t_VEC);
1979 16618 : for (i = 1; i < r; i++)
1980 : {
1981 : GEN u;
1982 11327 : if (w && gequal0(gel(w,i))) u = gen_0; /* unused */
1983 : else
1984 : {
1985 11250 : GEN pr = gel(L,i), e = gel(E,i), t;
1986 11250 : t = idealdivpowprime(nf,F, pr, e);
1987 11250 : u = hnfmerge_get_1(t, idealpow(nf, pr, e));
1988 11250 : if (!u) pari_err_COPRIME("idealchinese", t,pr);
1989 : }
1990 11327 : gel(U,i) = u;
1991 : }
1992 5291 : FZ = gcoeff(F, 1, 1);
1993 5291 : F = idealpseudored(F, nf_get_roundG(nf));
1994 5291 : return mkvec2(mkvec2(F, FZ), U);
1995 : }
1996 :
1997 : static GEN
1998 2660 : pl_normalize(GEN nf, GEN pl)
1999 : {
2000 2660 : const char *fun = "idealchinese";
2001 2660 : if (lg(pl)-1 != nf_get_r1(nf)) pari_err_TYPE(fun,pl);
2002 2660 : switch(typ(pl))
2003 : {
2004 707 : case t_VEC: RgV_check_ZV(pl,fun); pl = ZV_to_zv(pl);
2005 : /* fall through */
2006 2660 : case t_VECSMALL: break;
2007 0 : default: pari_err_TYPE(fun,pl);
2008 : }
2009 2660 : return pl;
2010 : }
2011 :
2012 : static int
2013 11445 : is_chineseinit(GEN x)
2014 : {
2015 : GEN fa, pl;
2016 : long l;
2017 11445 : if (typ(x) != t_VEC || lg(x)!=3) return 0;
2018 9219 : fa = gel(x,1);
2019 9219 : pl = gel(x,2);
2020 9219 : if (typ(fa) != t_VEC || typ(pl) != t_VEC) return 0;
2021 5411 : l = lg(fa);
2022 5411 : if (l != 1)
2023 : {
2024 : GEN z;
2025 5369 : if (l != 3) return 0;
2026 5369 : z = gel(fa, 1);
2027 5369 : if (typ(z) != t_VEC || lg(z) != 3 || typ(gel(z,1)) != t_MAT
2028 5362 : || typ(gel(z,2)) != t_INT
2029 5362 : || typ(gel(fa,2)) != t_VEC)
2030 7 : return 0;
2031 : }
2032 5404 : l = lg(pl);
2033 5404 : if (l != 1)
2034 : {
2035 931 : if (l != 6 || typ(gel(pl,3)) != t_MAT || typ(gel(pl,1)) != t_VECSMALL
2036 931 : || typ(gel(pl,2)) != t_VECSMALL)
2037 0 : return 0;
2038 : }
2039 5404 : return 1;
2040 : }
2041 :
2042 : /* nf a true 'nf' */
2043 : static GEN
2044 5774 : chineseinit_i(GEN nf, GEN fa, GEN w, GEN dw)
2045 : {
2046 5774 : const char *fun = "idealchineseinit";
2047 5774 : GEN archp = NULL, pl = NULL;
2048 5774 : switch(typ(fa))
2049 : {
2050 2660 : case t_VEC:
2051 2660 : if (is_chineseinit(fa))
2052 : {
2053 0 : if (dw) pari_err_DOMAIN(fun, "denom(y)", "!=", gen_1, w);
2054 0 : return fa;
2055 : }
2056 2660 : if (lg(fa) != 3) pari_err_TYPE(fun, fa);
2057 : /* of the form [x,s] */
2058 2660 : pl = pl_normalize(nf, gel(fa,2));
2059 2660 : fa = gel(fa,1);
2060 2660 : archp = vecsmall01_to_indices(pl);
2061 : /* keep pr_init, reset pl */
2062 2660 : if (is_chineseinit(fa)) { fa = gel(fa,1); break; }
2063 : /* fall through */
2064 : case t_MAT: /* factorization? */
2065 5305 : if (isprfact(fa)) { fa = pr_init(nf, fa, w, dw); break; }
2066 0 : default: pari_err_TYPE(fun,fa);
2067 : }
2068 :
2069 5774 : if (!pl) pl = cgetg(1,t_VEC);
2070 : else
2071 : {
2072 2660 : long r = lg(archp);
2073 2660 : if (r == 1) pl = cgetg(1, t_VEC);
2074 : else
2075 : {
2076 2037 : GEN F = (lg(fa) == 1)? NULL: gmael(fa,1,1), signs = cgetg(r, t_VECSMALL);
2077 : long i;
2078 5733 : for (i = 1; i < r; i++) signs[i] = (pl[archp[i]] < 0)? 1: 0;
2079 2037 : pl = setsigns_init(nf, archp, F, signs);
2080 : }
2081 : }
2082 5774 : return mkvec2(fa, pl);
2083 : }
2084 :
2085 : /* Given a prime ideal factorization x, possibly with 0 or negative exponents,
2086 : * and a vector w of elements of nf, gives b such that
2087 : * v_p(b-w_p)>=v_p(x) for all prime ideals p in the ideal factorization
2088 : * and v_p(b)>=0 for all other p, using the standard proof given in GTM 138. */
2089 : GEN
2090 10709 : idealchinese(GEN nf, GEN x0, GEN w)
2091 : {
2092 10709 : const char *fun = "idealchinese";
2093 10709 : pari_sp av = avma;
2094 10709 : GEN x = x0, x1, x2, s, dw, F;
2095 :
2096 10709 : nf = checknf(nf);
2097 10709 : if (!w) return gerepilecopy(av, chineseinit_i(nf,x,NULL,NULL));
2098 :
2099 6125 : if (typ(w) != t_VEC) pari_err_TYPE(fun,w);
2100 6125 : w = Q_remove_denom(matalgtobasis(nf,w), &dw);
2101 6125 : if (!is_chineseinit(x)) x = chineseinit_i(nf,x,w,dw);
2102 : /* x is a 'chineseinit' */
2103 6125 : x1 = gel(x,1); s = NULL;
2104 6125 : x2 = gel(x,2);
2105 6125 : if (lg(x1) == 1) { F = NULL; dw = NULL; }
2106 : else
2107 : {
2108 6083 : GEN U = gel(x1,2), FZ;
2109 6083 : long i, r = lg(w);
2110 6083 : F = gmael(x1,1,1); FZ = gmael(x1,1,2);
2111 20571 : for (i=1; i<r; i++)
2112 14488 : if (!ZV_equal0(gel(w,i)))
2113 : {
2114 10984 : GEN t = nfmuli(nf, gel(U,i), gel(w,i));
2115 10984 : s = s? ZC_add(s,t): t;
2116 : }
2117 6083 : if (s)
2118 : {
2119 6062 : s = ZC_reducemodmatrix(s, F);
2120 6062 : if (dw && x == x0) /* input was a chineseinit */
2121 : {
2122 7 : dw = modii(dw, FZ);
2123 7 : s = FpC_Fp_mul(s, Fp_inv(dw, FZ), FZ);
2124 7 : dw = NULL;
2125 : }
2126 6062 : if (ZV_isscalar(s)) s = icopy(gel(s,1));
2127 : }
2128 : }
2129 6125 : if (lg(x2) != 1)
2130 : {
2131 2044 : s = nfsetsigns(nf, gel(x2,1), s? s: gen_0, x2);
2132 2044 : if (typ(s) == t_COL && QV_isscalar(s))
2133 : {
2134 371 : s = gel(s,1); if (!dw) s = gcopy(s);
2135 : }
2136 : }
2137 4081 : else if (!s) return gc_const(av, gen_0);
2138 6076 : return gerepileupto(av, dw? gdiv(s, dw): s);
2139 : }
2140 :
2141 : /*************************************************************************/
2142 : /** **/
2143 : /** (Z_K/I)^* **/
2144 : /** **/
2145 : /*************************************************************************/
2146 : GEN
2147 2660 : vecsmall01_to_indices(GEN v)
2148 : {
2149 2660 : long i, k, l = lg(v);
2150 2660 : GEN p = new_chunk(l) + l;
2151 7567 : for (k=1, i=l-1; i; i--)
2152 4907 : if (v[i]) { *--p = i; k++; }
2153 2660 : *--p = _evallg(k) | evaltyp(t_VECSMALL);
2154 2660 : set_avma((pari_sp)p); return p;
2155 : }
2156 : GEN
2157 1094320 : vec01_to_indices(GEN v)
2158 : {
2159 : long i, k, l;
2160 : GEN p;
2161 :
2162 1094320 : switch (typ(v))
2163 : {
2164 1047561 : case t_VECSMALL: return v;
2165 46760 : case t_VEC: break;
2166 0 : default: pari_err_TYPE("vec01_to_indices",v);
2167 : }
2168 46760 : l = lg(v);
2169 46760 : p = new_chunk(l) + l;
2170 140588 : for (k=1, i=l-1; i; i--)
2171 93828 : if (signe(gel(v,i))) { *--p = i; k++; }
2172 46760 : *--p = _evallg(k) | evaltyp(t_VECSMALL);
2173 46760 : set_avma((pari_sp)p); return p;
2174 : }
2175 : GEN
2176 136893 : indices_to_vec01(GEN p, long r)
2177 : {
2178 136893 : long i, l = lg(p);
2179 136893 : GEN v = zerovec(r);
2180 206634 : for (i = 1; i < l; i++) gel(v, p[i]) = gen_1;
2181 136894 : return v;
2182 : }
2183 :
2184 : /* return (column) vector of R1 signatures of x (0 or 1) */
2185 : GEN
2186 1047561 : nfsign_arch(GEN nf, GEN x, GEN arch)
2187 : {
2188 1047561 : GEN sarch, V, archp = vec01_to_indices(arch);
2189 1047561 : long i, s, np, npc, r1, n = lg(archp)-1;
2190 : pari_sp av;
2191 :
2192 1047561 : if (!n) return cgetg(1,t_VECSMALL);
2193 845415 : if (typ(x) == t_MAT)
2194 : { /* factorisation */
2195 276330 : GEN g = gel(x,1), e = gel(x,2);
2196 276330 : long l = lg(g);
2197 276330 : V = zero_zv(n);
2198 828833 : for (i = 1; i < l; i++)
2199 552504 : if (mpodd(gel(e,i)))
2200 436269 : Flv_add_inplace(V, nfsign_arch(nf,gel(g,i),archp), 2);
2201 276329 : set_avma((pari_sp)V); return V;
2202 : }
2203 569085 : av = avma; V = cgetg(n+1,t_VECSMALL);
2204 569084 : x = nf_to_scalar_or_basis(nf, x);
2205 569085 : switch(typ(x))
2206 : {
2207 183668 : case t_INT:
2208 183668 : s = signe(x);
2209 183668 : if (!s) pari_err_DOMAIN("nfsign_arch","element","=",gen_0,x);
2210 183668 : set_avma(av); return const_vecsmall(n, (s < 0)? 1: 0);
2211 644 : case t_FRAC:
2212 644 : s = signe(gel(x,1));
2213 644 : set_avma(av); return const_vecsmall(n, (s < 0)? 1: 0);
2214 : }
2215 384773 : r1 = nf_get_r1(nf); x = Q_primpart(x); np = num_positive(nf, x);
2216 384774 : if (np == 0) { set_avma(av); return const_vecsmall(n, 1); }
2217 338193 : if (np == r1){ set_avma(av); return const_vecsmall(n, 0); }
2218 253032 : sarch = nfarchstar(nf, NULL, identity_perm(r1));
2219 381737 : for (i = 1, npc = 0; i <= n; i++)
2220 : {
2221 381399 : GEN xi = set_sign_mod_divisor(nf, vecsmall_ei(r1, archp[i]), gen_1, sarch);
2222 : long ni;
2223 381395 : xi = Q_primpart(xi);
2224 381397 : ni = num_positive(nf, nfmuli(nf,x,xi));
2225 381398 : V[i] = ni < np? 0: 1;
2226 381398 : if (!V[i]) npc++; /* found a positive root */
2227 381398 : if (npc == np)
2228 : { /* found all positive roots */
2229 248720 : for (i++; i <= n; i++) V[i] = 1;
2230 135163 : break;
2231 : }
2232 246235 : if (i - npc == r1 - np)
2233 : { /* found all negative roots */
2234 183685 : for (i++; i <= n; i++) V[i] = 0;
2235 117531 : break;
2236 : }
2237 : }
2238 253032 : set_avma((pari_sp)V); return V;
2239 : }
2240 : static void
2241 36246 : chk_ind(const char *s, long i, long r1)
2242 : {
2243 36246 : if (i <= 0) pari_err_DOMAIN(s, "index", "<=", gen_0, stoi(i));
2244 36232 : if (i > r1) pari_err_DOMAIN(s, "index", ">", utoi(r1), utoi(i));
2245 36197 : }
2246 : static GEN
2247 128226 : parse_embed(GEN ind, long r, const char *f)
2248 : {
2249 : long l, i;
2250 128226 : if (!ind) return identity_perm(r);
2251 34111 : switch(typ(ind))
2252 : {
2253 70 : case t_INT: ind = mkvecsmall(itos(ind)); break;
2254 84 : case t_VEC: case t_COL: ind = vec_to_vecsmall(ind); break;
2255 33957 : case t_VECSMALL: break;
2256 0 : default: pari_err_TYPE(f, ind);
2257 : }
2258 34111 : l = lg(ind);
2259 70308 : for (i = 1; i < l; i++) chk_ind(f, ind[i], r);
2260 34062 : return ind;
2261 : }
2262 : GEN
2263 125601 : nfeltsign(GEN nf, GEN x, GEN ind0)
2264 : {
2265 125601 : pari_sp av = avma;
2266 : long i, l;
2267 : GEN v, ind;
2268 125601 : nf = checknf(nf);
2269 125601 : ind = parse_embed(ind0, nf_get_r1(nf), "nfeltsign");
2270 125580 : l = lg(ind);
2271 125580 : if (is_rational_t(typ(x)))
2272 : { /* nfsign_arch would test this, but avoid converting t_VECSMALL -> t_VEC */
2273 : GEN s;
2274 31486 : switch(gsigne(x))
2275 : {
2276 16513 : case -1:s = gen_m1; break;
2277 14966 : case 1: s = gen_1; break;
2278 7 : default: s = gen_0; break;
2279 : }
2280 31486 : set_avma(av);
2281 31486 : return (ind0 && typ(ind0) == t_INT)? s: const_vec(l-1, s);
2282 : }
2283 94094 : v = nfsign_arch(nf, x, ind);
2284 94094 : if (ind0 && typ(ind0) == t_INT) { set_avma(av); return v[1]? gen_m1: gen_1; }
2285 94080 : settyp(v, t_VEC);
2286 263928 : for (i = 1; i < l; i++) gel(v,i) = v[i]? gen_m1: gen_1;
2287 94080 : return gerepileupto(av, v);
2288 : }
2289 :
2290 : /* true nf */
2291 : GEN
2292 728 : nfeltembed_i(GEN *pnf, GEN x, GEN ind0, long prec0)
2293 : {
2294 : long i, e, l, r1, r2, prec, prec1;
2295 728 : GEN v, ind, cx, nf = *pnf;
2296 728 : nf_get_sign(nf,&r1,&r2);
2297 728 : x = nf_to_scalar_or_basis(nf, x);
2298 721 : ind = parse_embed(ind0, r1+r2, "nfeltembed");
2299 714 : l = lg(ind);
2300 714 : if (typ(x) != t_COL)
2301 : {
2302 224 : if (!(ind0 && typ(ind0) == t_INT)) x = const_vec(l-1, x);
2303 224 : return x;
2304 : }
2305 490 : x = Q_primitive_part(x, &cx);
2306 490 : prec1 = prec0; e = gexpo(x);
2307 490 : if (e > 8) prec1 += nbits2extraprec(e);
2308 490 : prec = prec1;
2309 490 : if (nf_get_prec(nf) < prec) nf = nfnewprec_shallow(nf, prec);
2310 490 : v = cgetg(l, t_VEC);
2311 : for(;;)
2312 138 : {
2313 628 : GEN M = nf_get_M(nf);
2314 2630 : for (i = 1; i < l; i++)
2315 : {
2316 2140 : GEN t = nfembed_i(M, x, ind[i]);
2317 2140 : long e = gexpo(t);
2318 2140 : if (gequal0(t) || precision(t) < prec0
2319 2140 : || (e < 0 && prec < prec1 + nbits2extraprec(-e)) ) break;
2320 2002 : if (cx) t = gmul(t, cx);
2321 2002 : gel(v,i) = t;
2322 : }
2323 628 : if (i == l) break;
2324 138 : prec = precdbl(prec);
2325 138 : if (DEBUGLEVEL>1) pari_warn(warnprec,"eltnfembed", prec);
2326 138 : *pnf = nf = nfnewprec_shallow(nf, prec);
2327 : }
2328 490 : if (ind0 && typ(ind0) == t_INT) v = gel(v,1);
2329 490 : return v;
2330 : }
2331 : GEN
2332 728 : nfeltembed(GEN nf, GEN x, GEN ind0, long prec0)
2333 : {
2334 728 : pari_sp av = avma; nf = checknf(nf);
2335 728 : return gerepilecopy(av, nfeltembed_i(&nf, x, ind0, prec0));
2336 : }
2337 :
2338 : /* number of distinct roots of sigma(f) */
2339 : GEN
2340 1904 : nfpolsturm(GEN nf, GEN f, GEN ind0)
2341 : {
2342 1904 : pari_sp av = avma;
2343 : long d, l, r1, single;
2344 : GEN ind, u, v, vr1, T, s, t;
2345 :
2346 1904 : nf = checknf(nf); T = nf_get_pol(nf); r1 = nf_get_r1(nf);
2347 1904 : ind = parse_embed(ind0, r1, "nfpolsturm");
2348 1883 : single = ind0 && typ(ind0) == t_INT;
2349 1883 : l = lg(ind);
2350 :
2351 1883 : if (gequal0(f)) pari_err_ROOTS0("nfpolsturm");
2352 1876 : if (typ(f) == t_POL && varn(f) != varn(T))
2353 : {
2354 1855 : f = RgX_nffix("nfpolsturm", T, f,1);
2355 1855 : if (lg(f) == 3) f = NULL;
2356 : }
2357 : else
2358 : {
2359 21 : (void)Rg_nffix("nfpolsturm", T, f, 0);
2360 21 : f = NULL;
2361 : }
2362 1876 : if (!f) { set_avma(av); return single? gen_0: zerovec(l-1); }
2363 1855 : d = degpol(f);
2364 1855 : if (d == 1) { set_avma(av); return single? gen_1: const_vec(l-1,gen_1); }
2365 :
2366 1785 : vr1 = const_vecsmall(l-1, 1);
2367 1785 : u = Q_primpart(f); s = ZV_to_zv(nfeltsign(nf, gel(u,d+2), ind));
2368 1785 : v = RgX_deriv(u); t = odd(d)? leafcopy(s): zv_neg(s);
2369 : for(;;)
2370 245 : {
2371 2030 : GEN r = RgX_neg( Q_primpart(RgX_pseudorem(u, v)) ), sr;
2372 2030 : long i, dr = degpol(r);
2373 2030 : if (dr < 0) break;
2374 2030 : sr = ZV_to_zv(nfeltsign(nf, gel(r,dr+2), ind));
2375 4865 : for (i = 1; i < l; i++)
2376 2835 : if (sr[i] != s[i]) { s[i] = sr[i], vr1[i]--; }
2377 2030 : if (odd(dr)) sr = zv_neg(sr);
2378 4865 : for (i = 1; i < l; i++)
2379 2835 : if (sr[i] != t[i]) { t[i] = sr[i], vr1[i]++; }
2380 2030 : if (!dr) break;
2381 245 : u = v; v = r;
2382 : }
2383 1785 : if (single) return gc_stoi(av,vr1[1]);
2384 1778 : return gerepileupto(av, zv_to_ZV(vr1));
2385 : }
2386 :
2387 : /* True nf; return the vector of signs of x; the matrix of such if x is a vector
2388 : * of nf elements */
2389 : GEN
2390 44170 : nfsign(GEN nf, GEN x)
2391 : {
2392 : long i, l;
2393 : GEN archp, S;
2394 :
2395 44170 : archp = identity_perm( nf_get_r1(nf) );
2396 44170 : if (typ(x) != t_VEC) return nfsign_arch(nf, x, archp);
2397 35945 : l = lg(x); S = cgetg(l, t_MAT);
2398 148113 : for (i=1; i<l; i++) gel(S,i) = nfsign_arch(nf, gel(x,i), archp);
2399 35945 : return S;
2400 : }
2401 :
2402 : /* x integral elt, A integral ideal in HNF; reduce x mod A */
2403 : static GEN
2404 7820428 : zk_modHNF(GEN x, GEN A)
2405 7820428 : { return (typ(x) == t_COL)? ZC_hnfrem(x, A): modii(x, gcoeff(A,1,1)); }
2406 :
2407 : /* given an element x in Z_K and an integral ideal y in HNF, coprime with x,
2408 : outputs an element inverse of x modulo y */
2409 : GEN
2410 189 : nfinvmodideal(GEN nf, GEN x, GEN y)
2411 : {
2412 189 : pari_sp av = avma;
2413 189 : GEN a, yZ = gcoeff(y,1,1);
2414 :
2415 189 : if (equali1(yZ)) return gen_0;
2416 189 : x = nf_to_scalar_or_basis(nf, x);
2417 189 : if (typ(x) == t_INT) return gerepileupto(av, Fp_inv(x, yZ));
2418 :
2419 79 : a = hnfmerge_get_1(idealhnf_principal(nf,x), y);
2420 79 : if (!a) pari_err_INV("nfinvmodideal", x);
2421 79 : return gerepileupto(av, zk_modHNF(nfdiv(nf,a,x), y));
2422 : }
2423 :
2424 : static GEN
2425 2688415 : nfsqrmodideal(GEN nf, GEN x, GEN id)
2426 2688415 : { return zk_modHNF(nfsqri(nf,x), id); }
2427 : static GEN
2428 7293082 : nfmulmodideal(GEN nf, GEN x, GEN y, GEN id)
2429 7293082 : { return x? zk_modHNF(nfmuli(nf,x,y), id): y; }
2430 : /* assume x integral, k integer, A in HNF */
2431 : GEN
2432 5847445 : nfpowmodideal(GEN nf,GEN x,GEN k,GEN A)
2433 : {
2434 5847445 : long s = signe(k);
2435 : pari_sp av;
2436 : GEN y;
2437 :
2438 5847445 : if (!s) return gen_1;
2439 5847445 : av = avma;
2440 5847445 : x = nf_to_scalar_or_basis(nf, x);
2441 5847672 : if (typ(x) != t_COL) return Fp_pow(x, k, gcoeff(A,1,1));
2442 2629116 : if (s < 0) { k = negi(k); x = nfinvmodideal(nf, x,A); }
2443 2629116 : if (equali1(k)) return gerepileupto(av, s > 0? zk_modHNF(x, A): x);
2444 1149964 : for(y = NULL;;)
2445 : {
2446 3838406 : if (mpodd(k)) y = nfmulmodideal(nf,y,x,A);
2447 3838399 : k = shifti(k,-1); if (!signe(k)) break;
2448 2688082 : x = nfsqrmodideal(nf,x,A);
2449 : }
2450 1149944 : return gerepileupto(av, y);
2451 : }
2452 :
2453 : /* a * g^n mod id */
2454 : static GEN
2455 4696175 : nfmulpowmodideal(GEN nf, GEN a, GEN g, GEN n, GEN id)
2456 : {
2457 4696175 : return nfmulmodideal(nf, a, nfpowmodideal(nf,g,n,id), id);
2458 : }
2459 :
2460 : /* assume (num(g[i]), id) = 1 for all i. Return prod g[i]^e[i] mod id.
2461 : * EX = multiple of exponent of (O_K/id)^* */
2462 : GEN
2463 2622416 : famat_to_nf_modideal_coprime(GEN nf, GEN g, GEN e, GEN id, GEN EX)
2464 : {
2465 2622416 : GEN EXo2, plus = NULL, minus = NULL, idZ = gcoeff(id,1,1);
2466 2622416 : long i, lx = lg(g);
2467 :
2468 2622416 : if (equali1(idZ)) return gen_1; /* id = Z_K */
2469 2621930 : EXo2 = (expi(EX) > 10)? shifti(EX,-1): NULL;
2470 8330047 : for (i = 1; i < lx; i++)
2471 : {
2472 5708186 : GEN h, n = centermodii(gel(e,i), EX, EXo2);
2473 5707725 : long sn = signe(n);
2474 5707725 : if (!sn) continue;
2475 :
2476 4041319 : h = nf_to_scalar_or_basis(nf, gel(g,i));
2477 4041724 : switch(typ(h))
2478 : {
2479 2384156 : case t_INT: break;
2480 0 : case t_FRAC:
2481 0 : h = Fp_div(gel(h,1), gel(h,2), idZ); break;
2482 1657568 : default:
2483 : {
2484 : GEN dh;
2485 1657568 : h = Q_remove_denom(h, &dh);
2486 1657715 : if (dh) h = FpC_Fp_mul(h, Fp_inv(dh,idZ), idZ);
2487 : }
2488 : }
2489 4041784 : if (sn > 0)
2490 4039941 : plus = nfmulpowmodideal(nf, plus, h, n, id);
2491 : else /* sn < 0 */
2492 1843 : minus = nfmulpowmodideal(nf, minus, h, negi(n), id);
2493 : }
2494 2621861 : if (minus) plus = nfmulmodideal(nf, plus, nfinvmodideal(nf,minus,id), id);
2495 2621954 : return plus? plus: gen_1;
2496 : }
2497 :
2498 : /* given 2 integral ideals x, y in HNF s.t x | y | x^2, compute (1+x)/(1+y) in
2499 : * the form [[cyc],[gen], U], where U := ux^-1 as a pair [ZM, denom(U)] */
2500 : static GEN
2501 237215 : zidealij(GEN x, GEN y)
2502 : {
2503 237215 : GEN U, G, cyc, xp = gcoeff(x,1,1), xi = hnf_invscale(x, xp);
2504 : long j, N;
2505 :
2506 : /* x^(-1) y = relations between the 1 + x_i (HNF) */
2507 237207 : cyc = ZM_snf_group(ZM_Z_divexact(ZM_mul(xi, y), xp), &U, &G);
2508 237207 : N = lg(cyc); G = ZM_mul(x,G); settyp(G, t_VEC); /* new generators */
2509 574607 : for (j=1; j<N; j++)
2510 : {
2511 337403 : GEN c = gel(G,j);
2512 337403 : gel(c,1) = addiu(gel(c,1), 1); /* 1 + g_j */
2513 337397 : if (ZV_isscalar(c)) gel(G,j) = gel(c,1);
2514 : }
2515 237204 : return mkvec4(cyc, G, ZM_mul(U,xi), xp);
2516 : }
2517 :
2518 : /* lg(x) > 1, x + 1; shallow */
2519 : static GEN
2520 169778 : ZC_add1(GEN x)
2521 : {
2522 169778 : long i, l = lg(x);
2523 169778 : GEN y = cgetg(l, t_COL);
2524 396516 : for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
2525 169779 : gel(y,1) = addiu(gel(x,1), 1); return y;
2526 : }
2527 : /* lg(x) > 1, x - 1; shallow */
2528 : static GEN
2529 70488 : ZC_sub1(GEN x)
2530 : {
2531 70488 : long i, l = lg(x);
2532 70488 : GEN y = cgetg(l, t_COL);
2533 176907 : for (i = 2; i < l; i++) gel(y,i) = gel(x,i);
2534 70488 : gel(y,1) = subiu(gel(x,1), 1); return y;
2535 : }
2536 :
2537 : /* x,y are t_INT or ZC */
2538 : static GEN
2539 0 : zkadd(GEN x, GEN y)
2540 : {
2541 0 : long tx = typ(x);
2542 0 : if (tx == typ(y))
2543 0 : return tx == t_INT? addii(x,y): ZC_add(x,y);
2544 : else
2545 0 : return tx == t_INT? ZC_Z_add(y,x): ZC_Z_add(x,y);
2546 : }
2547 : /* x a t_INT or ZC, x+1; shallow */
2548 : static GEN
2549 255449 : zkadd1(GEN x)
2550 : {
2551 255449 : long tx = typ(x);
2552 255449 : return tx == t_INT? addiu(x,1): ZC_add1(x);
2553 : }
2554 : /* x a t_INT or ZC, x-1; shallow */
2555 : static GEN
2556 255488 : zksub1(GEN x)
2557 : {
2558 255488 : long tx = typ(x);
2559 255488 : return tx == t_INT? subiu(x,1): ZC_sub1(x);
2560 : }
2561 : /* x,y are t_INT or ZC; x - y */
2562 : static GEN
2563 0 : zksub(GEN x, GEN y)
2564 : {
2565 0 : long tx = typ(x), ty = typ(y);
2566 0 : if (tx == ty)
2567 0 : return tx == t_INT? subii(x,y): ZC_sub(x,y);
2568 : else
2569 0 : return tx == t_INT? Z_ZC_sub(x,y): ZC_Z_sub(x,y);
2570 : }
2571 : /* x is t_INT or ZM (mult. map), y is t_INT or ZC; x * y */
2572 : static GEN
2573 255466 : zkmul(GEN x, GEN y)
2574 : {
2575 255466 : long tx = typ(x), ty = typ(y);
2576 255466 : if (ty == t_INT)
2577 184994 : return tx == t_INT? mulii(x,y): ZC_Z_mul(gel(x,1),y);
2578 : else
2579 70472 : return tx == t_INT? ZC_Z_mul(y,x): ZM_ZC_mul(x,y);
2580 : }
2581 :
2582 : /* (U,V) = 1 coprime ideals. Want z = x mod U, = y mod V; namely
2583 : * z =vx + uy = v(x-y) + y, where u + v = 1, u in U, v in V.
2584 : * zkc = [v, UV], v a t_INT or ZM (mult. by v map), UV a ZM (ideal in HNF);
2585 : * shallow */
2586 : GEN
2587 0 : zkchinese(GEN zkc, GEN x, GEN y)
2588 : {
2589 0 : GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd(zkmul(v, zksub(x,y)), y);
2590 0 : return zk_modHNF(z, UV);
2591 : }
2592 : /* special case z = x mod U, = 1 mod V; shallow */
2593 : GEN
2594 255488 : zkchinese1(GEN zkc, GEN x)
2595 : {
2596 255488 : GEN v = gel(zkc,1), UV = gel(zkc,2), z = zkadd1(zkmul(v, zksub1(x)));
2597 255451 : return (typ(z) == t_INT)? z: ZC_hnfrem(z, UV);
2598 : }
2599 : static GEN
2600 237467 : zkVchinese1(GEN zkc, GEN v)
2601 : {
2602 : long i, ly;
2603 237467 : GEN y = cgetg_copy(v, &ly);
2604 492892 : for (i=1; i<ly; i++) gel(y,i) = zkchinese1(zkc, gel(v,i));
2605 237404 : return y;
2606 : }
2607 :
2608 : /* prepare to solve z = x (mod A), z = y mod (B) [zkchinese or zkchinese1] */
2609 : GEN
2610 237218 : zkchineseinit(GEN nf, GEN A, GEN B, GEN AB)
2611 : {
2612 237218 : GEN v = idealaddtoone_raw(nf, A, B);
2613 : long e;
2614 237211 : if ((e = gexpo(v)) > 5)
2615 : {
2616 83279 : GEN b = (typ(v) == t_COL)? v: scalarcol_shallow(v, nf_get_degree(nf));
2617 83279 : b= ZC_reducemodlll(b, AB);
2618 83281 : if (gexpo(b) < e) v = b;
2619 : }
2620 237204 : return mkvec2(zk_scalar_or_multable(nf,v), AB);
2621 : }
2622 : /* prepare to solve z = x (mod A), z = 1 mod (B)
2623 : * and then z = 1 (mod A), z = y mod (B) [zkchinese1 twice] */
2624 : static GEN
2625 259 : zkchinese1init2(GEN nf, GEN A, GEN B, GEN AB)
2626 : {
2627 259 : GEN zkc = zkchineseinit(nf, A, B, AB);
2628 259 : GEN mv = gel(zkc,1), mu;
2629 259 : if (typ(mv) == t_INT) return mkvec2(zkc, mkvec2(subui(1,mv),AB));
2630 35 : mu = RgM_Rg_add_shallow(ZM_neg(mv), gen_1);
2631 35 : return mkvec2(mkvec2(mv,AB), mkvec2(mu,AB));
2632 : }
2633 :
2634 : static GEN
2635 2156162 : apply_U(GEN L, GEN a)
2636 : {
2637 2156162 : GEN e, U = gel(L,3), dU = gel(L,4);
2638 2156162 : if (typ(a) == t_INT)
2639 672862 : e = ZC_Z_mul(gel(U,1), subiu(a, 1));
2640 : else
2641 : { /* t_COL */
2642 1483300 : GEN t = shallowcopy(a);
2643 1483347 : gel(t,1) = subiu(gel(t,1), 1); /* t = a - 1 */
2644 1483266 : e = ZM_ZC_mul(U, t);
2645 : }
2646 2156093 : return gdiv(e, dU);
2647 : }
2648 :
2649 : /* true nf; vectors of [[cyc],[g],U.X^-1]. Assume k > 1. */
2650 : static GEN
2651 169271 : principal_units(GEN nf, GEN pr, long k, GEN prk)
2652 : {
2653 : GEN list, prb;
2654 169271 : ulong mask = quadratic_prec_mask(k);
2655 169271 : long a = 1;
2656 :
2657 169271 : prb = pr_hnf(nf,pr);
2658 169274 : list = vectrunc_init(k);
2659 406486 : while (mask > 1)
2660 : {
2661 237217 : GEN pra = prb;
2662 237217 : long b = a << 1;
2663 :
2664 237217 : if (mask & 1) b--;
2665 237217 : mask >>= 1;
2666 : /* compute 1 + pr^a / 1 + pr^b, 2a <= b */
2667 237217 : prb = (b >= k)? prk: idealpows(nf,pr,b);
2668 237217 : vectrunc_append(list, zidealij(pra, prb));
2669 237214 : a = b;
2670 : }
2671 169269 : return list;
2672 : }
2673 : /* a = 1 mod (pr) return log(a) on local-gens of 1+pr/1+pr^k */
2674 : static GEN
2675 1331619 : log_prk1(GEN nf, GEN a, long nh, GEN L2, GEN prk)
2676 : {
2677 1331619 : GEN y = cgetg(nh+1, t_COL);
2678 1331620 : long j, iy, c = lg(L2)-1;
2679 3487772 : for (j = iy = 1; j <= c; j++)
2680 : {
2681 2156155 : GEN L = gel(L2,j), cyc = gel(L,1), gen = gel(L,2), E = apply_U(L,a);
2682 2156017 : long i, nc = lg(cyc)-1;
2683 2156017 : int last = (j == c);
2684 5825228 : for (i = 1; i <= nc; i++, iy++)
2685 : {
2686 3669076 : GEN t, e = gel(E,i);
2687 3669076 : if (typ(e) != t_INT) pari_err_COPRIME("zlog_prk1", a, prk);
2688 3669069 : t = Fp_neg(e, gel(cyc,i));
2689 3669071 : gel(y,iy) = negi(t);
2690 3669217 : if (!last && signe(t)) a = nfmulpowmodideal(nf, a, gel(gen,i), t, prk);
2691 : }
2692 : }
2693 1331617 : return y;
2694 : }
2695 : /* true nf */
2696 : static GEN
2697 56774 : principal_units_relations(GEN nf, GEN L2, GEN prk, long nh)
2698 : {
2699 56774 : GEN h = cgetg(nh+1,t_MAT);
2700 56774 : long ih, j, c = lg(L2)-1;
2701 181493 : for (j = ih = 1; j <= c; j++)
2702 : {
2703 124716 : GEN L = gel(L2,j), F = gel(L,1), G = gel(L,2);
2704 124716 : long k, lG = lg(G);
2705 304952 : for (k = 1; k < lG; k++,ih++)
2706 : { /* log(g^f) mod pr^e */
2707 180233 : GEN a = nfpowmodideal(nf,gel(G,k),gel(F,k),prk);
2708 180232 : gel(h,ih) = ZC_neg(log_prk1(nf, a, nh, L2, prk));
2709 180236 : gcoeff(h,ih,ih) = gel(F,k);
2710 : }
2711 : }
2712 56777 : return h;
2713 : }
2714 : /* true nf; k > 1; multiplicative group (1 + pr) / (1 + pr^k) */
2715 : static GEN
2716 169271 : idealprincipalunits_i(GEN nf, GEN pr, long k, GEN *pU)
2717 : {
2718 169271 : GEN cyc, gen, L2, prk = idealpows(nf, pr, k);
2719 :
2720 169271 : L2 = principal_units(nf, pr, k, prk);
2721 169274 : if (k == 2)
2722 : {
2723 112500 : GEN L = gel(L2,1);
2724 112500 : cyc = gel(L,1);
2725 112500 : gen = gel(L,2);
2726 112500 : if (pU) *pU = matid(lg(gen)-1);
2727 : }
2728 : else
2729 : {
2730 56774 : long c = lg(L2), j;
2731 56774 : GEN EX, h, Ui, vg = cgetg(c, t_VEC);
2732 181486 : for (j = 1; j < c; j++) gel(vg, j) = gmael(L2,j,2);
2733 56774 : vg = shallowconcat1(vg);
2734 56774 : h = principal_units_relations(nf, L2, prk, lg(vg)-1);
2735 56777 : h = ZM_hnfall_i(h, NULL, 0);
2736 56777 : cyc = ZM_snf_group(h, pU, &Ui);
2737 56776 : c = lg(Ui); gen = cgetg(c, t_VEC); EX = cyc_get_expo(cyc);
2738 188795 : for (j = 1; j < c; j++)
2739 132018 : gel(gen,j) = famat_to_nf_modideal_coprime(nf, vg, gel(Ui,j), prk, EX);
2740 : }
2741 169278 : return mkvec4(cyc, gen, prk, L2);
2742 : }
2743 : GEN
2744 182 : idealprincipalunits(GEN nf, GEN pr, long k)
2745 : {
2746 : pari_sp av;
2747 : GEN v;
2748 182 : nf = checknf(nf);
2749 182 : if (k == 1) { checkprid(pr); retmkvec3(gen_1,cgetg(1,t_VEC),cgetg(1,t_VEC)); }
2750 175 : av = avma; v = idealprincipalunits_i(nf, pr, k, NULL);
2751 175 : return gerepilecopy(av, mkvec3(powiu(pr_norm(pr), k-1), gel(v,1), gel(v,2)));
2752 : }
2753 :
2754 : /* true nf; given an ideal pr^k dividing an integral ideal x (in HNF form)
2755 : * compute an 'sprk', the structure of G = (Z_K/pr^k)^* [ x = NULL for x=pr^k ]
2756 : * Return a vector with at least 4 components [cyc],[gen],[HNF pr^k,pr,k],ff,
2757 : * where
2758 : * cyc : type of G as abelian group (SNF)
2759 : * gen : generators of G, coprime to x
2760 : * pr^k: in HNF
2761 : * ff : data for log_g in (Z_K/pr)^*
2762 : * Two extra components are present iff k > 1: L2, U
2763 : * L2 : list of data structures to compute local DL in (Z_K/pr)^*,
2764 : * and 1 + pr^a/ 1 + pr^b for various a < b <= min(2a, k)
2765 : * U : base change matrices to convert a vector of local DL to DL wrt gen
2766 : * If MOD is not NULL, initialize G / G^MOD instead */
2767 : static GEN
2768 426102 : sprkinit(GEN nf, GEN pr, long k, GEN x, GEN MOD)
2769 : {
2770 426102 : GEN T, p, Ld, modpr, cyc, gen, g, g0, A, prk, U, L2, ord0 = NULL;
2771 426102 : long f = pr_get_f(pr);
2772 :
2773 426092 : if(DEBUGLEVEL>3) err_printf("treating pr^%ld, pr = %Ps\n",k,pr);
2774 426092 : modpr = nf_to_Fq_init(nf, &pr,&T,&p);
2775 426120 : if (MOD)
2776 : {
2777 378499 : GEN o = subiu(powiu(p,f), 1), d = gcdii(o, MOD), fa = Z_factor(d);
2778 378470 : ord0 = mkvec2(o, fa); /* true order, factorization of order in G/G^MOD */
2779 378468 : Ld = gel(fa,1);
2780 378468 : if (lg(Ld) > 1 && equaliu(gel(Ld,1),2)) Ld = vecslice(Ld,2,lg(Ld)-1);
2781 : }
2782 : /* (Z_K / pr)^* */
2783 426098 : if (f == 1)
2784 : {
2785 336912 : g0 = g = MOD? pgener_Fp_local(p, Ld): pgener_Fp(p);
2786 336924 : if (!ord0) ord0 = get_arith_ZZM(subiu(p,1));
2787 : }
2788 : else
2789 : {
2790 89186 : g0 = g = MOD? gener_FpXQ_local(T, p, Ld): gener_FpXQ(T,p, &ord0);
2791 89186 : g = Fq_to_nf(g, modpr);
2792 89185 : if (typ(g) == t_POL) g = poltobasis(nf, g);
2793 : }
2794 426116 : A = gel(ord0, 1); /* Norm(pr)-1 */
2795 : /* If MOD != NULL, d = gcd(A, MOD): g^(A/d) has order d */
2796 426116 : if (k == 1)
2797 : {
2798 257020 : cyc = mkvec(A);
2799 257012 : gen = mkvec(g);
2800 257011 : prk = pr_hnf(nf,pr);
2801 257023 : L2 = U = NULL;
2802 : }
2803 : else
2804 : { /* local-gens of (1 + pr)/(1 + pr^k) = SNF-gens * U */
2805 : GEN AB, B, u, v, w;
2806 : long j, l;
2807 169096 : w = idealprincipalunits_i(nf, pr, k, &U);
2808 : /* incorporate (Z_K/pr)^*, order A coprime to B = expo(1+pr/1+pr^k)*/
2809 169101 : cyc = leafcopy(gel(w,1)); B = cyc_get_expo(cyc); AB = mulii(A,B);
2810 169083 : gen = leafcopy(gel(w,2));
2811 169081 : prk = gel(w,3);
2812 169081 : g = nfpowmodideal(nf, g, B, prk);
2813 169103 : g0 = Fq_pow(g0, modii(B,A), T, p); /* update primitive root */
2814 169097 : L2 = mkvec3(A, g, gel(w,4));
2815 169101 : gel(cyc,1) = AB;
2816 169101 : gel(gen,1) = nfmulmodideal(nf, gel(gen,1), g, prk);
2817 169086 : u = mulii(Fp_inv(A,B), A);
2818 169081 : v = subui(1, u); l = lg(U);
2819 505915 : for (j = 1; j < l; j++) gcoeff(U,1,j) = Fp_mul(u, gcoeff(U,1,j), AB);
2820 169086 : U = mkvec2(Rg_col_ei(v, lg(gen)-1, 1), U);
2821 : }
2822 : /* local-gens of (Z_K/pr^k)^* = SNF-gens * U */
2823 426121 : if (x)
2824 : {
2825 236957 : GEN uv = zkchineseinit(nf, idealmulpowprime(nf,x,pr,utoineg(k)), prk, x);
2826 236951 : gen = zkVchinese1(uv, gen);
2827 : }
2828 426052 : return mkvecn(U? 6: 4, cyc, gen, prk, mkvec3(modpr,g0,ord0), L2, U);
2829 : }
2830 : GEN
2831 3984235 : sprk_get_cyc(GEN s) { return gel(s,1); }
2832 : GEN
2833 1969689 : sprk_get_expo(GEN s) { return cyc_get_expo(sprk_get_cyc(s)); }
2834 : GEN
2835 335935 : sprk_get_gen(GEN s) { return gel(s,2); }
2836 : GEN
2837 4917776 : sprk_get_prk(GEN s) { return gel(s,3); }
2838 : GEN
2839 2543606 : sprk_get_ff(GEN s) { return gel(s,4); }
2840 : GEN
2841 2604039 : sprk_get_pr(GEN s) { GEN ff = gel(s,4); return modpr_get_pr(gel(ff,1)); }
2842 : /* L2 to 1 + pr / 1 + pr^k */
2843 : static GEN
2844 1213680 : sprk_get_L2(GEN s) { return gmael(s,5,3); }
2845 : /* lift to nf of primitive root of k(pr) */
2846 : static GEN
2847 318207 : sprk_get_gnf(GEN s) { return gmael(s,5,2); }
2848 : /* A = Npr-1, <g> = (Z_K/pr)^*, L2 to 1 + pr / 1 + pr^k */
2849 : void
2850 0 : sprk_get_AgL2(GEN s, GEN *A, GEN *g, GEN *L2)
2851 0 : { GEN v = gel(s,5); *A = gel(v,1); *g = gel(v,2); *L2 = gel(v,3); }
2852 : void
2853 1205070 : sprk_get_U2(GEN s, GEN *U1, GEN *U2)
2854 1205070 : { GEN v = gel(s,6); *U1 = gel(v,1); *U2 = gel(v,2); }
2855 : static int
2856 2543602 : sprk_is_prime(GEN s) { return lg(s) == 5; }
2857 :
2858 : GEN
2859 1969493 : famat_zlog_pr(GEN nf, GEN g, GEN e, GEN sprk, GEN mod)
2860 : {
2861 1969493 : GEN x, expo = sprk_get_expo(sprk);
2862 1969493 : if (mod) expo = gcdii(expo,mod);
2863 1969483 : x = famat_makecoprime(nf, g, e, sprk_get_pr(sprk), sprk_get_prk(sprk), expo);
2864 1969490 : return log_prk(nf, x, sprk, mod);
2865 : }
2866 : /* famat_zlog_pr assuming (g,sprk.pr) = 1 */
2867 : static GEN
2868 196 : famat_zlog_pr_coprime(GEN nf, GEN g, GEN e, GEN sprk, GEN MOD)
2869 : {
2870 196 : GEN x = famat_to_nf_modideal_coprime(nf, g, e, sprk_get_prk(sprk),
2871 : sprk_get_expo(sprk));
2872 196 : return log_prk(nf, x, sprk, MOD);
2873 : }
2874 :
2875 : /* o t_INT, O = [ord,fa] format for multiple of o (for Fq_log);
2876 : * return o in [ord,fa] format */
2877 : static GEN
2878 560264 : order_update(GEN o, GEN O)
2879 : {
2880 560264 : GEN p = gmael(O,2,1), z = o, P, E;
2881 560264 : long i, j, l = lg(p);
2882 560264 : P = cgetg(l, t_COL);
2883 560259 : E = cgetg(l, t_COL);
2884 617474 : for (i = j = 1; i < l; i++)
2885 : {
2886 617474 : long v = Z_pvalrem(z, gel(p,i), &z);
2887 617425 : if (v)
2888 : {
2889 604330 : gel(P,j) = gel(p,i);
2890 604330 : gel(E,j) = utoipos(v); j++;
2891 604358 : if (is_pm1(z)) break;
2892 : }
2893 : }
2894 560230 : setlg(P, j);
2895 560229 : setlg(E, j); return mkvec2(o, mkmat2(P,E));
2896 : }
2897 :
2898 : /* a in Z_K (t_COL or t_INT), pr prime ideal, sprk = sprkinit(nf,pr,k,x),
2899 : * mod positive t_INT or NULL (meaning mod=0).
2900 : * return log(a) modulo mod on SNF-generators of (Z_K/pr^k)^* */
2901 : GEN
2902 2617556 : log_prk(GEN nf, GEN a, GEN sprk, GEN mod)
2903 : {
2904 : GEN e, prk, g, U1, U2, y, ff, O, o, oN, gN, N, T, p, modpr, pr, cyc;
2905 :
2906 2617556 : if (typ(a) == t_MAT) return famat_zlog_pr(nf, gel(a,1), gel(a,2), sprk, mod);
2907 2543594 : N = NULL;
2908 2543594 : ff = sprk_get_ff(sprk);
2909 2543608 : pr = gel(ff,1); /* modpr */
2910 2543608 : g = gN = gel(ff,2);
2911 2543608 : O = gel(ff,3); /* order of g = |Fq^*|, in [ord, fa] format */
2912 2543608 : o = oN = gel(O,1); /* order as a t_INT */
2913 2543608 : prk = sprk_get_prk(sprk);
2914 2543623 : modpr = nf_to_Fq_init(nf, &pr, &T, &p);
2915 2543630 : if (mod)
2916 : {
2917 2027233 : GEN d = gcdii(o,mod);
2918 2026986 : if (!equalii(o, d))
2919 : {
2920 751083 : N = diviiexact(o,d); /* > 1, coprime to p */
2921 751041 : a = nfpowmodideal(nf, a, N, prk);
2922 751222 : oN = d; /* order of g^N mod pr */
2923 : }
2924 : }
2925 2543460 : if (equali1(oN))
2926 398171 : e = gen_0;
2927 : else
2928 : {
2929 2145362 : if (N) { O = order_update(oN, O); gN = Fq_pow(g, N, T, p); }
2930 2145357 : e = Fq_log(nf_to_Fq(nf,a,modpr), gN, O, T, p);
2931 : }
2932 : /* 0 <= e < oN is correct modulo oN */
2933 2543622 : if (sprk_is_prime(sprk)) return mkcol(e); /* k = 1 */
2934 :
2935 800595 : sprk_get_U2(sprk, &U1,&U2);
2936 800696 : cyc = sprk_get_cyc(sprk);
2937 800700 : if (mod)
2938 : {
2939 379343 : cyc = ZV_snf_gcd(cyc, mod);
2940 379331 : if (signe(remii(mod,p))) return ZV_ZV_mod(ZC_Z_mul(U1,e), cyc);
2941 : }
2942 746973 : if (signe(e))
2943 : {
2944 318207 : GEN E = N? mulii(e, N): e;
2945 318207 : a = nfmulpowmodideal(nf, a, sprk_get_gnf(sprk), Fp_neg(E, o), prk);
2946 : }
2947 : /* a = 1 mod pr */
2948 746973 : y = log_prk1(nf, a, lg(U2)-1, sprk_get_L2(sprk), prk);
2949 747004 : if (N)
2950 : { /* from DL(a^N) to DL(a) */
2951 135415 : GEN E = gel(sprk_get_cyc(sprk), 1), q = powiu(p, Z_pval(E, p));
2952 135415 : y = ZC_Z_mul(y, Fp_inv(N, q));
2953 : }
2954 747002 : y = ZC_lincomb(gen_1, e, ZM_ZC_mul(U2,y), U1);
2955 746999 : return ZV_ZV_mod(y, cyc);
2956 : }
2957 : /* true nf */
2958 : GEN
2959 90237 : log_prk_init(GEN nf, GEN pr, long k, GEN MOD)
2960 90237 : { return sprkinit(nf,pr,k,NULL,MOD);}
2961 : GEN
2962 497 : veclog_prk(GEN nf, GEN v, GEN sprk)
2963 : {
2964 497 : long l = lg(v), i;
2965 497 : GEN w = cgetg(l, t_MAT);
2966 1232 : for (i = 1; i < l; i++) gel(w,i) = log_prk(nf, gel(v,i), sprk, NULL);
2967 497 : return w;
2968 : }
2969 :
2970 : static GEN
2971 1374193 : famat_zlog(GEN nf, GEN fa, GEN sgn, zlog_S *S)
2972 : {
2973 1374193 : long i, l0, l = lg(S->U);
2974 1374193 : GEN g = gel(fa,1), e = gel(fa,2), y = cgetg(l, t_COL);
2975 1374194 : l0 = lg(S->sprk); /* = l (trivial arch. part), or l-1 */
2976 2852246 : for (i=1; i < l0; i++) gel(y,i) = famat_zlog_pr(nf, g, e, gel(S->sprk,i), S->mod);
2977 1374193 : if (l0 != l)
2978 : {
2979 190902 : if (!sgn) sgn = nfsign_arch(nf, fa, S->archp);
2980 190902 : gel(y,l0) = Flc_to_ZC(sgn);
2981 : }
2982 1374193 : return y;
2983 : }
2984 :
2985 : /* assume that cyclic factors are normalized, in particular != [1] */
2986 : static GEN
2987 257561 : split_U(GEN U, GEN Sprk)
2988 : {
2989 257561 : long t = 0, k, n, l = lg(Sprk);
2990 257561 : GEN vU = cgetg(l+1, t_VEC);
2991 592744 : for (k = 1; k < l; k++)
2992 : {
2993 335181 : n = lg(sprk_get_cyc(gel(Sprk,k))) - 1; /* > 0 */
2994 335182 : gel(vU,k) = vecslice(U, t+1, t+n);
2995 335185 : t += n;
2996 : }
2997 : /* t+1 .. lg(U)-1 */
2998 257563 : n = lg(U) - t - 1; /* can be 0 */
2999 257563 : if (!n) setlg(vU,l); else gel(vU,l) = vecslice(U, t+1, t+n);
3000 257566 : return vU;
3001 : }
3002 :
3003 : static void
3004 1990767 : init_zlog_mod(zlog_S *S, GEN bid, GEN mod)
3005 : {
3006 1990767 : GEN fa2 = bid_get_fact2(bid), MOD = bid_get_MOD(bid);
3007 1990758 : S->U = bid_get_U(bid);
3008 1990758 : S->hU = lg(bid_get_cyc(bid))-1;
3009 1990758 : S->archp = bid_get_archp(bid);
3010 1990755 : S->sprk = bid_get_sprk(bid);
3011 1990756 : S->bid = bid;
3012 1990756 : if (MOD) mod = mod? gcdii(mod, MOD): MOD;
3013 1990670 : S->mod = mod;
3014 1990670 : S->P = gel(fa2,1);
3015 1990670 : S->k = gel(fa2,2);
3016 1990670 : S->no2 = lg(S->P) == lg(gel(bid_get_fact(bid),1));
3017 1990680 : }
3018 : void
3019 380265 : init_zlog(zlog_S *S, GEN bid)
3020 : {
3021 380265 : return init_zlog_mod(S, bid, NULL);
3022 : }
3023 :
3024 : /* a a t_FRAC/t_INT, reduce mod bid */
3025 : static GEN
3026 14 : Q_mod_bid(GEN bid, GEN a)
3027 : {
3028 14 : GEN xZ = gcoeff(bid_get_ideal(bid),1,1);
3029 14 : GEN b = Rg_to_Fp(a, xZ);
3030 14 : if (gsigne(a) < 0) b = subii(b, xZ);
3031 14 : return signe(b)? b: xZ;
3032 : }
3033 : /* Return decomposition of a on the CRT generators blocks attached to the
3034 : * S->sprk and sarch; sgn = sign(a, S->arch), NULL if unknown */
3035 : static GEN
3036 381557 : zlog(GEN nf, GEN a, GEN sgn, zlog_S *S)
3037 : {
3038 : long k, l;
3039 : GEN y;
3040 381557 : a = nf_to_scalar_or_basis(nf, a);
3041 381530 : switch(typ(a))
3042 : {
3043 162592 : case t_INT: break;
3044 14 : case t_FRAC: a = Q_mod_bid(S->bid, a); break;
3045 218924 : default: /* case t_COL: */
3046 : {
3047 : GEN den;
3048 218924 : check_nfelt(a, &den);
3049 218948 : if (den)
3050 : {
3051 105 : a = Q_muli_to_int(a, den);
3052 105 : a = mkmat2(mkcol2(a, den), mkcol2(gen_1, gen_m1));
3053 105 : return famat_zlog(nf, a, sgn, S);
3054 : }
3055 : }
3056 : }
3057 381441 : if (sgn)
3058 374539 : sgn = (lg(sgn) == 1)? NULL: leafcopy(sgn);
3059 : else
3060 6902 : sgn = (lg(S->archp) == 1)? NULL: nfsign_arch(nf, a, S->archp);
3061 381446 : l = lg(S->sprk);
3062 381446 : y = cgetg(sgn? l+1: l, t_COL);
3063 922822 : for (k = 1; k < l; k++)
3064 : {
3065 541406 : GEN sprk = gel(S->sprk,k);
3066 541406 : gel(y,k) = log_prk(nf, a, sprk, S->mod);
3067 : }
3068 381416 : if (sgn) gel(y,l) = Flc_to_ZC(sgn);
3069 381423 : return y;
3070 : }
3071 :
3072 : /* true nf */
3073 : GEN
3074 43813 : pr_basis_perm(GEN nf, GEN pr)
3075 : {
3076 43813 : long f = pr_get_f(pr);
3077 : GEN perm;
3078 43813 : if (f == nf_get_degree(nf)) return identity_perm(f);
3079 38164 : perm = cgetg(f+1, t_VECSMALL);
3080 38164 : perm[1] = 1;
3081 38164 : if (f > 1)
3082 : {
3083 2912 : GEN H = pr_hnf(nf,pr);
3084 : long i, k;
3085 10808 : for (i = k = 2; k <= f; i++)
3086 7896 : if (!equali1(gcoeff(H,i,i))) perm[k++] = i;
3087 : }
3088 38164 : return perm;
3089 : }
3090 :
3091 : /* \sum U[i]*y[i], U[i] ZM, y[i] ZC. We allow lg(y) > lg(U). */
3092 : static GEN
3093 1755716 : ZMV_ZCV_mul(GEN U, GEN y)
3094 : {
3095 1755716 : long i, l = lg(U);
3096 1755716 : GEN z = NULL;
3097 1755716 : if (l == 1) return cgetg(1,t_COL);
3098 4140166 : for (i = 1; i < l; i++)
3099 : {
3100 2384560 : GEN u = ZM_ZC_mul(gel(U,i), gel(y,i));
3101 2384508 : z = z? ZC_add(z, u): u;
3102 : }
3103 1755606 : return z;
3104 : }
3105 : /* A * (x[1], ..., x[d] */
3106 : static GEN
3107 518 : ZM_ZMV_mul(GEN A, GEN x)
3108 1057 : { pari_APPLY_same(ZM_mul(A,gel(x,i))); }
3109 :
3110 : /* a = 1 mod pr, sprk mod pr^e, e >= 1 */
3111 : static GEN
3112 404400 : sprk_log_prk1_2(GEN nf, GEN a, GEN sprk)
3113 : {
3114 404400 : GEN U1, U2, y, L2 = sprk_get_L2(sprk);
3115 404400 : sprk_get_U2(sprk, &U1,&U2);
3116 404401 : y = ZM_ZC_mul(U2, log_prk1(nf, a, lg(U2)-1, L2, sprk_get_prk(sprk)));
3117 404387 : return ZV_ZV_mod(y, sprk_get_cyc(sprk));
3118 : }
3119 : /* true nf; assume e >= 2 */
3120 : GEN
3121 105863 : sprk_log_gen_pr2(GEN nf, GEN sprk, long e)
3122 : {
3123 105863 : GEN M, G, pr = sprk_get_pr(sprk);
3124 : long i, l;
3125 105863 : if (e == 2)
3126 : {
3127 62304 : GEN L2 = sprk_get_L2(sprk), L = gel(L2,1);
3128 62304 : G = gel(L,2); l = lg(G);
3129 : }
3130 : else
3131 : {
3132 43559 : GEN perm = pr_basis_perm(nf,pr), PI = nfpow_u(nf, pr_get_gen(pr), e-1);
3133 43560 : l = lg(perm);
3134 43560 : G = cgetg(l, t_VEC);
3135 43560 : if (typ(PI) == t_INT)
3136 : { /* zk_ei_mul doesn't allow t_INT */
3137 5641 : long N = nf_get_degree(nf);
3138 5641 : gel(G,1) = addiu(PI,1);
3139 8644 : for (i = 2; i < l; i++)
3140 : {
3141 3003 : GEN z = col_ei(N, 1);
3142 3003 : gel(G,i) = z; gel(z, perm[i]) = PI;
3143 : }
3144 : }
3145 : else
3146 : {
3147 37919 : gel(G,1) = nfadd(nf, gen_1, PI);
3148 44702 : for (i = 2; i < l; i++)
3149 6783 : gel(G,i) = nfadd(nf, gen_1, zk_ei_mul(nf, PI, perm[i]));
3150 : }
3151 : }
3152 105864 : M = cgetg(l, t_MAT);
3153 234387 : for (i = 1; i < l; i++) gel(M,i) = sprk_log_prk1_2(nf, gel(G,i), sprk);
3154 105841 : return M;
3155 : }
3156 : /* Log on bid.gen of generators of P_{1,I pr^{e-1}} / P_{1,I pr^e} (I,pr) = 1,
3157 : * defined implicitly via CRT. 'ind' is the index of pr in modulus
3158 : * factorization; true nf */
3159 : GEN
3160 413955 : log_gen_pr(zlog_S *S, long ind, GEN nf, long e)
3161 : {
3162 413955 : GEN Uind = gel(S->U, ind);
3163 413955 : if (e == 1) retmkmat( gel(Uind,1) );
3164 103161 : return ZM_mul(Uind, sprk_log_gen_pr2(nf, gel(S->sprk,ind), e));
3165 : }
3166 : /* true nf */
3167 : GEN
3168 2037 : sprk_log_gen_pr(GEN nf, GEN sprk, long e)
3169 : {
3170 2037 : if (e == 1)
3171 : {
3172 0 : long n = lg(sprk_get_cyc(sprk))-1;
3173 0 : retmkmat(col_ei(n, 1));
3174 : }
3175 2037 : return sprk_log_gen_pr2(nf, sprk, e);
3176 : }
3177 : /* a = 1 mod pr */
3178 : GEN
3179 275854 : sprk_log_prk1(GEN nf, GEN a, GEN sprk)
3180 : {
3181 275854 : if (lg(sprk) == 5) return mkcol(gen_0); /* mod pr */
3182 275854 : return sprk_log_prk1_2(nf, a, sprk);
3183 : }
3184 : /* Log on bid.gen of generator of P_{1,f} / P_{1,f v[index]}
3185 : * v = vector of r1 real places */
3186 : GEN
3187 86265 : log_gen_arch(zlog_S *S, long index) { return gel(veclast(S->U), index); }
3188 :
3189 : /* compute bid.clgp: [h,cyc] or [h,cyc,gen] */
3190 : static GEN
3191 258585 : bid_grp(GEN nf, GEN U, GEN cyc, GEN g, GEN F, GEN sarch)
3192 : {
3193 258585 : GEN G, h = ZV_prod(cyc);
3194 : long c;
3195 258600 : if (!U) return mkvec2(h,cyc);
3196 258243 : c = lg(U);
3197 258243 : G = cgetg(c,t_VEC);
3198 258253 : if (c > 1)
3199 : {
3200 228153 : GEN U0, Uoo, EX = cyc_get_expo(cyc); /* exponent of bid */
3201 228153 : long i, hU = nbrows(U), nba = lg(sarch_get_cyc(sarch))-1; /* #f_oo */
3202 228160 : if (!nba) { U0 = U; Uoo = NULL; }
3203 80418 : else if (nba == hU) { U0 = NULL; Uoo = U; }
3204 : else
3205 : {
3206 71276 : U0 = rowslice(U, 1, hU-nba);
3207 71280 : Uoo = rowslice(U, hU-nba+1, hU);
3208 : }
3209 695705 : for (i = 1; i < c; i++)
3210 : {
3211 467565 : GEN t = gen_1;
3212 467565 : if (U0) t = famat_to_nf_modideal_coprime(nf, g, gel(U0,i), F, EX);
3213 467560 : if (Uoo) t = set_sign_mod_divisor(nf, ZV_to_Flv(gel(Uoo,i),2), t, sarch);
3214 467543 : gel(G,i) = t;
3215 : }
3216 : }
3217 258240 : return mkvec3(h, cyc, G);
3218 : }
3219 :
3220 : /* remove prime ideals of norm 2 with exponent 1 from factorization */
3221 : static GEN
3222 258925 : famat_strip2(GEN fa)
3223 : {
3224 258925 : GEN P = gel(fa,1), E = gel(fa,2), Q, F;
3225 258925 : long l = lg(P), i, j;
3226 258925 : Q = cgetg(l, t_COL);
3227 258918 : F = cgetg(l, t_COL);
3228 634094 : for (i = j = 1; i < l; i++)
3229 : {
3230 375170 : GEN pr = gel(P,i), e = gel(E,i);
3231 375170 : if (!absequaliu(pr_get_p(pr), 2) || itou(e) != 1 || pr_get_f(pr) != 1)
3232 : {
3233 336534 : gel(Q,j) = pr;
3234 336534 : gel(F,j) = e; j++;
3235 : }
3236 : }
3237 258924 : setlg(Q,j);
3238 258921 : setlg(F,j); return mkmat2(Q,F);
3239 : }
3240 : static int
3241 134094 : checkarchp(GEN v, long r1)
3242 : {
3243 134094 : long i, l = lg(v);
3244 134094 : pari_sp av = avma;
3245 : GEN p;
3246 134094 : if (l == 1) return 1;
3247 47156 : if (l == 2) return v[1] > 0 && v[1] <= r1;
3248 22019 : p = zero_zv(r1);
3249 66150 : for (i = 1; i < l; i++)
3250 : {
3251 44163 : long j = v[i];
3252 44163 : if (j <= 0 || j > r1 || p[j]) return gc_long(av, 0);
3253 44128 : p[j] = 1;
3254 : }
3255 21987 : return gc_long(av, 1);
3256 : }
3257 :
3258 : /* True nf. Put ideal to form [[ideal,arch]] and set fa and fa2 to its
3259 : * factorization, archp to the indices of arch places */
3260 : GEN
3261 258883 : check_mod_factored(GEN nf, GEN ideal, GEN *fa_, GEN *fa2_, GEN *archp_, GEN MOD)
3262 : {
3263 : GEN arch, x, fa, fa2, archp;
3264 : long R1;
3265 :
3266 258883 : R1 = nf_get_r1(nf);
3267 258926 : if (typ(ideal) == t_VEC && lg(ideal) == 3)
3268 : {
3269 178719 : arch = gel(ideal,2);
3270 178719 : ideal= gel(ideal,1);
3271 178719 : switch(typ(arch))
3272 : {
3273 44625 : case t_VEC:
3274 44625 : if (lg(arch) != R1+1)
3275 7 : pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
3276 44618 : archp = vec01_to_indices(arch);
3277 44618 : break;
3278 134094 : case t_VECSMALL:
3279 134094 : if (!checkarchp(arch, R1))
3280 35 : pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
3281 134058 : archp = arch;
3282 134058 : arch = indices_to_vec01(archp, R1);
3283 134059 : break;
3284 0 : default:
3285 0 : pari_err_TYPE("Idealstar [incorrect archimedean component]",arch);
3286 : return NULL;/*LCOV_EXCL_LINE*/
3287 : }
3288 : }
3289 : else
3290 : {
3291 80207 : arch = zerovec(R1);
3292 80203 : archp = cgetg(1, t_VECSMALL);
3293 : }
3294 258879 : if (MOD)
3295 : {
3296 214275 : if (typ(MOD) != t_INT) pari_err_TYPE("bnrinit [incorrect cycmod]", MOD);
3297 214275 : if (mpodd(MOD) && lg(archp) != 1)
3298 231 : MOD = shifti(MOD, 1); /* ensure elements of G^MOD are >> 0 */
3299 : }
3300 258881 : if (is_nf_factor(ideal))
3301 : {
3302 40362 : fa = ideal;
3303 40362 : x = factorbackprime(nf, gel(fa,1), gel(fa,2));
3304 : }
3305 : else
3306 : {
3307 218518 : fa = idealfactor(nf, ideal);
3308 218521 : x = ideal;
3309 : }
3310 258883 : if (typ(x) != t_MAT) x = idealhnf_shallow(nf, x);
3311 258868 : if (lg(x) == 1) pari_err_DOMAIN("Idealstar", "ideal","=",gen_0,x);
3312 258868 : if (typ(gcoeff(x,1,1)) != t_INT)
3313 7 : pari_err_DOMAIN("Idealstar","denominator(ideal)", "!=",gen_1,x);
3314 :
3315 258861 : fa2 = famat_strip2(fa);
3316 258857 : if (fa_ != NULL) *fa_ = fa;
3317 258857 : if (fa2_ != NULL) *fa2_ = fa2;
3318 258857 : if (fa2_ != NULL) *archp_ = archp;
3319 258857 : return mkvec2(x, arch);
3320 : }
3321 :
3322 : /* True nf. Compute [[ideal,arch], [h,[cyc],[gen]], idealfact, [liste], U]
3323 : flag may include nf_GEN | nf_INIT */
3324 : static GEN
3325 258288 : Idealstarmod_i(GEN nf, GEN ideal, long flag, GEN MOD)
3326 : {
3327 : long i, nbp;
3328 258288 : GEN y, cyc, U, u1 = NULL, fa, fa2, sprk, x_arch, x, arch, archp, E, P, sarch, gen;
3329 :
3330 258288 : x_arch = check_mod_factored(nf, ideal, &fa, &fa2, &archp, MOD);
3331 258224 : x = gel(x_arch, 1);
3332 258224 : arch = gel(x_arch, 2);
3333 :
3334 258224 : sarch = nfarchstar(nf, x, archp);
3335 258228 : P = gel(fa2,1);
3336 258228 : E = gel(fa2,2);
3337 258228 : nbp = lg(P)-1;
3338 258228 : sprk = cgetg(nbp+1,t_VEC);
3339 258236 : if (nbp)
3340 : {
3341 218952 : GEN t = (lg(gel(fa,1))==2)? NULL: x; /* beware fa != fa2 */
3342 218952 : cyc = cgetg(nbp+2,t_VEC);
3343 218947 : gen = cgetg(nbp+1,t_VEC);
3344 554830 : for (i = 1; i <= nbp; i++)
3345 : {
3346 335863 : GEN L = sprkinit(nf, gel(P,i), itou(gel(E,i)), t, MOD);
3347 335873 : gel(sprk,i) = L;
3348 335873 : gel(cyc,i) = sprk_get_cyc(L);
3349 : /* true gens are congruent to those mod x AND positive at archp */
3350 335872 : gel(gen,i) = sprk_get_gen(L);
3351 : }
3352 218967 : gel(cyc,i) = sarch_get_cyc(sarch);
3353 218967 : cyc = shallowconcat1(cyc);
3354 218976 : gen = shallowconcat1(gen);
3355 218977 : cyc = ZV_snf_group(cyc, &U, (flag & nf_GEN)? &u1: NULL);
3356 : }
3357 : else
3358 : {
3359 39284 : cyc = sarch_get_cyc(sarch);
3360 39284 : gen = cgetg(1,t_VEC);
3361 39284 : U = matid(lg(cyc)-1);
3362 39284 : if (flag & nf_GEN) u1 = U;
3363 : }
3364 258232 : if (MOD) cyc = ZV_snf_gcd(cyc, MOD);
3365 258226 : y = bid_grp(nf, u1, cyc, gen, x, sarch);
3366 258254 : if (!(flag & nf_INIT)) return y;
3367 257456 : U = split_U(U, sprk);
3368 514917 : return mkvec5(mkvec2(x, arch), y, mkvec2(fa,fa2),
3369 257458 : MOD? mkvec3(sprk, sarch, MOD): mkvec2(sprk, sarch),
3370 : U);
3371 : }
3372 :
3373 : static long
3374 63 : idealHNF_norm_pval(GEN x, GEN p)
3375 : {
3376 63 : long i, v = 0, l = lg(x);
3377 175 : for (i = 1; i < l; i++) v += Z_pval(gcoeff(x,i,i), p);
3378 63 : return v;
3379 : }
3380 : static long
3381 63 : sprk_get_k(GEN sprk)
3382 : {
3383 : GEN pr, prk;
3384 63 : if (sprk_is_prime(sprk)) return 1;
3385 63 : pr = sprk_get_pr(sprk);
3386 63 : prk = sprk_get_prk(sprk);
3387 63 : return idealHNF_norm_pval(prk, pr_get_p(pr)) / pr_get_f(pr);
3388 : }
3389 : /* true nf, L a sprk */
3390 : GEN
3391 63 : sprk_to_bid(GEN nf, GEN L, long flag)
3392 : {
3393 63 : GEN y, cyc, U, u1 = NULL, fa, fa2, arch, sarch, gen, sprk;
3394 :
3395 63 : arch = zerovec(nf_get_r1(nf));
3396 63 : fa = to_famat_shallow(sprk_get_pr(L), utoipos(sprk_get_k(L)));
3397 63 : sarch = nfarchstar(nf, NULL, cgetg(1, t_VECSMALL));
3398 63 : fa2 = famat_strip2(fa);
3399 63 : sprk = mkvec(L);
3400 63 : cyc = shallowconcat(sprk_get_cyc(L), sarch_get_cyc(sarch));
3401 63 : gen = sprk_get_gen(L);
3402 63 : cyc = ZV_snf_group(cyc, &U, (flag & nf_GEN)? &u1: NULL);
3403 63 : y = bid_grp(nf, u1, cyc, gen, NULL, sarch);
3404 63 : if (!(flag & nf_INIT)) return y;
3405 63 : return mkvec5(mkvec2(sprk_get_prk(L), arch), y, mkvec2(fa,fa2),
3406 : mkvec2(sprk, sarch), split_U(U, sprk));
3407 : }
3408 : GEN
3409 258012 : Idealstarmod(GEN nf, GEN ideal, long flag, GEN MOD)
3410 : {
3411 258012 : pari_sp av = avma;
3412 258012 : nf = nf? checknf(nf): nfinit(pol_x(0), DEFAULTPREC);
3413 258015 : return gerepilecopy(av, Idealstarmod_i(nf, ideal, flag, MOD));
3414 : }
3415 : GEN
3416 938 : Idealstar(GEN nf, GEN ideal, long flag) { return Idealstarmod(nf, ideal, flag, NULL); }
3417 : GEN
3418 273 : Idealstarprk(GEN nf, GEN pr, long k, long flag)
3419 : {
3420 273 : pari_sp av = avma;
3421 273 : GEN z = Idealstarmod_i(nf, mkmat2(mkcol(pr),mkcols(k)), flag, NULL);
3422 273 : return gerepilecopy(av, z);
3423 : }
3424 :
3425 : /* FIXME: obsolete */
3426 : GEN
3427 0 : zidealstarinitgen(GEN nf, GEN ideal)
3428 0 : { return Idealstar(nf,ideal, nf_INIT|nf_GEN); }
3429 : GEN
3430 0 : zidealstarinit(GEN nf, GEN ideal)
3431 0 : { return Idealstar(nf,ideal, nf_INIT); }
3432 : GEN
3433 0 : zidealstar(GEN nf, GEN ideal)
3434 0 : { return Idealstar(nf,ideal, nf_GEN); }
3435 :
3436 : GEN
3437 112 : idealstarmod(GEN nf, GEN ideal, long flag, GEN MOD)
3438 : {
3439 112 : switch(flag)
3440 : {
3441 0 : case 0: return Idealstarmod(nf,ideal, nf_GEN, MOD);
3442 98 : case 1: return Idealstarmod(nf,ideal, nf_INIT, MOD);
3443 14 : case 2: return Idealstarmod(nf,ideal, nf_INIT|nf_GEN, MOD);
3444 0 : default: pari_err_FLAG("idealstar");
3445 : }
3446 : return NULL; /* LCOV_EXCL_LINE */
3447 : }
3448 : GEN
3449 0 : idealstar0(GEN nf, GEN ideal,long flag) { return idealstarmod(nf, ideal, flag, NULL); }
3450 :
3451 : void
3452 218940 : check_nfelt(GEN x, GEN *den)
3453 : {
3454 218940 : long l = lg(x), i;
3455 218940 : GEN t, d = NULL;
3456 218940 : if (typ(x) != t_COL) pari_err_TYPE("check_nfelt", x);
3457 809092 : for (i=1; i<l; i++)
3458 : {
3459 590145 : t = gel(x,i);
3460 590145 : switch (typ(t))
3461 : {
3462 589914 : case t_INT: break;
3463 231 : case t_FRAC:
3464 231 : if (!d) d = gel(t,2); else d = lcmii(d, gel(t,2));
3465 231 : break;
3466 0 : default: pari_err_TYPE("check_nfelt", x);
3467 : }
3468 : }
3469 218947 : *den = d;
3470 218947 : }
3471 :
3472 : GEN
3473 1953096 : ZV_snf_gcd(GEN x, GEN mod)
3474 4358745 : { pari_APPLY_same(gcdii(gel(x,i), mod)); }
3475 :
3476 : /* assume a true bnf and bid */
3477 : GEN
3478 227127 : ideallog_units0(GEN bnf, GEN bid, GEN MOD)
3479 : {
3480 227127 : GEN nf = bnf_get_nf(bnf), D, y, C, cyc;
3481 227126 : long j, lU = lg(bnf_get_logfu(bnf)); /* r1+r2 */
3482 : zlog_S S;
3483 227126 : init_zlog_mod(&S, bid, MOD);
3484 227113 : if (!S.hU) return zeromat(0,lU);
3485 227113 : cyc = bid_get_cyc(bid);
3486 227111 : D = nfsign_fu(bnf, bid_get_archp(bid));
3487 227118 : y = cgetg(lU, t_MAT);
3488 227116 : if ((C = bnf_build_cheapfu(bnf)))
3489 374500 : { for (j = 1; j < lU; j++) gel(y,j) = zlog(nf, gel(C,j), gel(D,j), &S); }
3490 : else
3491 : {
3492 49 : long i, l = lg(S.U), l0 = lg(S.sprk);
3493 : GEN X, U;
3494 49 : if (!(C = bnf_compactfu_mat(bnf))) bnf_build_units(bnf); /* error */
3495 49 : X = gel(C,1); U = gel(C,2);
3496 147 : for (j = 1; j < lU; j++) gel(y,j) = cgetg(l, t_COL);
3497 126 : for (i = 1; i < l0; i++)
3498 : {
3499 77 : GEN sprk = gel(S.sprk, i);
3500 77 : GEN Xi = sunits_makecoprime(X, sprk_get_pr(sprk), sprk_get_prk(sprk));
3501 231 : for (j = 1; j < lU; j++)
3502 154 : gcoeff(y,i,j) = famat_zlog_pr_coprime(nf, Xi, gel(U,j), sprk, MOD);
3503 : }
3504 49 : if (l0 != l)
3505 56 : for (j = 1; j < lU; j++) gcoeff(y,l0,j) = Flc_to_ZC(gel(D,j));
3506 : }
3507 227119 : y = vec_prepend(y, zlog(nf, bnf_get_tuU(bnf), nfsign_tu(bnf, S.archp), &S));
3508 601738 : for (j = 1; j <= lU; j++)
3509 374636 : gel(y,j) = ZV_ZV_mod(ZMV_ZCV_mul(S.U, gel(y,j)), cyc);
3510 227102 : return y;
3511 : }
3512 : GEN
3513 84 : ideallog_units(GEN bnf, GEN bid)
3514 84 : { return ideallog_units0(bnf, bid, NULL); }
3515 : GEN
3516 518 : log_prk_units(GEN nf, GEN D, GEN sprk)
3517 : {
3518 518 : GEN L, Ltu = log_prk(nf, gel(D,1), sprk, NULL);
3519 518 : D = gel(D,2);
3520 518 : if (lg(D) != 3 || typ(gel(D,2)) != t_MAT) L = veclog_prk(nf, D, sprk);
3521 : else
3522 : {
3523 21 : GEN X = gel(D,1), U = gel(D,2);
3524 21 : long j, lU = lg(U);
3525 21 : X = sunits_makecoprime(X, sprk_get_pr(sprk), sprk_get_prk(sprk));
3526 21 : L = cgetg(lU, t_MAT);
3527 63 : for (j = 1; j < lU; j++)
3528 42 : gel(L,j) = famat_zlog_pr_coprime(nf, X, gel(U,j), sprk, NULL);
3529 : }
3530 518 : return vec_prepend(L, Ltu);
3531 : }
3532 :
3533 : static GEN
3534 1383390 : ideallog_i(GEN nf, GEN x, zlog_S *S)
3535 : {
3536 1383390 : pari_sp av = avma;
3537 : GEN y;
3538 1383390 : if (!S->hU) return cgetg(1, t_COL);
3539 1381094 : if (typ(x) == t_MAT)
3540 1374087 : y = famat_zlog(nf, x, NULL, S);
3541 : else
3542 7007 : y = zlog(nf, x, NULL, S);
3543 1381090 : y = ZMV_ZCV_mul(S->U, y);
3544 1381086 : return gerepileupto(av, ZV_ZV_mod(y, bid_get_cyc(S->bid)));
3545 : }
3546 : GEN
3547 1390071 : ideallogmod(GEN nf, GEN x, GEN bid, GEN mod)
3548 : {
3549 : zlog_S S;
3550 1390071 : if (!nf)
3551 : {
3552 6671 : if (mod) pari_err_IMPL("Zideallogmod");
3553 6671 : return Zideallog(bid, x);
3554 : }
3555 1383400 : checkbid(bid); init_zlog_mod(&S, bid, mod);
3556 1383390 : return ideallog_i(checknf(nf), x, &S);
3557 : }
3558 : GEN
3559 13769 : ideallog(GEN nf, GEN x, GEN bid) { return ideallogmod(nf, x, bid, NULL); }
3560 :
3561 : /*************************************************************************/
3562 : /** **/
3563 : /** JOIN BID STRUCTURES, IDEAL LISTS **/
3564 : /** **/
3565 : /*************************************************************************/
3566 : /* bid1, bid2: for coprime modules m1 and m2 (without arch. part).
3567 : * Output: bid for m1 m2 */
3568 : static GEN
3569 469 : join_bid(GEN nf, GEN bid1, GEN bid2)
3570 : {
3571 469 : pari_sp av = avma;
3572 : long nbgen, l1,l2;
3573 : GEN I1,I2, G1,G2, sprk1,sprk2, cyc1,cyc2, sarch;
3574 469 : GEN sprk, fa,fa2, U, cyc, y, u1 = NULL, x, gen;
3575 :
3576 469 : I1 = bid_get_ideal(bid1);
3577 469 : I2 = bid_get_ideal(bid2);
3578 469 : if (gequal1(gcoeff(I1,1,1))) return bid2; /* frequent trivial case */
3579 259 : G1 = bid_get_grp(bid1);
3580 259 : G2 = bid_get_grp(bid2);
3581 259 : x = idealmul(nf, I1,I2);
3582 259 : fa = famat_mul_shallow(bid_get_fact(bid1), bid_get_fact(bid2));
3583 259 : fa2= famat_mul_shallow(bid_get_fact2(bid1), bid_get_fact2(bid2));
3584 259 : sprk1 = bid_get_sprk(bid1);
3585 259 : sprk2 = bid_get_sprk(bid2);
3586 259 : sprk = shallowconcat(sprk1, sprk2);
3587 :
3588 259 : cyc1 = abgrp_get_cyc(G1); l1 = lg(cyc1);
3589 259 : cyc2 = abgrp_get_cyc(G2); l2 = lg(cyc2);
3590 259 : gen = (lg(G1)>3 && lg(G2)>3)? gen_1: NULL;
3591 259 : nbgen = l1+l2-2;
3592 259 : cyc = ZV_snf_group(shallowconcat(cyc1,cyc2), &U, gen? &u1: NULL);
3593 259 : if (nbgen)
3594 : {
3595 259 : GEN U1 = bid_get_U(bid1), U2 = bid_get_U(bid2);
3596 0 : U1 = l1==1? const_vec(lg(sprk1), cgetg(1,t_MAT))
3597 259 : : ZM_ZMV_mul(vecslice(U, 1, l1-1), U1);
3598 0 : U2 = l2==1? const_vec(lg(sprk2), cgetg(1,t_MAT))
3599 259 : : ZM_ZMV_mul(vecslice(U, l1, nbgen), U2);
3600 259 : U = shallowconcat(U1, U2);
3601 : }
3602 : else
3603 0 : U = const_vec(lg(sprk), cgetg(1,t_MAT));
3604 :
3605 259 : if (gen)
3606 : {
3607 259 : GEN uv = zkchinese1init2(nf, I2, I1, x);
3608 518 : gen = shallowconcat(zkVchinese1(gel(uv,1), abgrp_get_gen(G1)),
3609 259 : zkVchinese1(gel(uv,2), abgrp_get_gen(G2)));
3610 : }
3611 259 : sarch = bid_get_sarch(bid1); /* trivial */
3612 259 : y = bid_grp(nf, u1, cyc, gen, x, sarch);
3613 259 : x = mkvec2(x, bid_get_arch(bid1));
3614 259 : y = mkvec5(x, y, mkvec2(fa, fa2), mkvec2(sprk, sarch), U);
3615 259 : return gerepilecopy(av,y);
3616 : }
3617 :
3618 : typedef struct _ideal_data {
3619 : GEN nf, emb, L, pr, prL, sgnU, archp;
3620 : } ideal_data;
3621 :
3622 : /* z <- ( z | f(v[i])_{i=1..#v} ) */
3623 : static void
3624 759157 : concat_join(GEN *pz, GEN v, GEN (*f)(ideal_data*,GEN), ideal_data *data)
3625 : {
3626 759157 : long i, nz, lv = lg(v);
3627 : GEN z, Z;
3628 759157 : if (lv == 1) return;
3629 223086 : z = *pz; nz = lg(z)-1;
3630 223086 : *pz = Z = cgetg(lv + nz, typ(z));
3631 371685 : for (i = 1; i <=nz; i++) gel(Z,i) = gel(z,i);
3632 223336 : Z += nz;
3633 492130 : for (i = 1; i < lv; i++) gel(Z,i) = f(data, gel(v,i));
3634 : }
3635 : static GEN
3636 469 : join_idealinit(ideal_data *D, GEN x)
3637 469 : { return join_bid(D->nf, x, D->prL); }
3638 : static GEN
3639 268493 : join_ideal(ideal_data *D, GEN x)
3640 268493 : { return idealmulpowprime(D->nf, x, D->pr, D->L); }
3641 : static GEN
3642 448 : join_unit(ideal_data *D, GEN x)
3643 : {
3644 448 : GEN bid = join_idealinit(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
3645 448 : if (lg(u) != 1) v = shallowconcat(u, v);
3646 448 : return mkvec2(bid, v);
3647 : }
3648 :
3649 : GEN
3650 49 : log_prk_units_init(GEN bnf)
3651 : {
3652 49 : GEN U = bnf_has_fu(bnf);
3653 49 : if (U) U = matalgtobasis(bnf_get_nf(bnf), U);
3654 21 : else if (!(U = bnf_compactfu_mat(bnf))) (void)bnf_build_units(bnf);
3655 49 : return mkvec2(bnf_get_tuU(bnf), U);
3656 : }
3657 : /* flag & nf_GEN : generators, otherwise no
3658 : * flag &2 : units, otherwise no
3659 : * flag &4 : ideals in HNF, otherwise bid
3660 : * flag &8 : omit ideals which cannot be conductors (pr^1 with Npr=2) */
3661 : static GEN
3662 11333 : Ideallist(GEN bnf, ulong bound, long flag)
3663 : {
3664 11333 : const long do_units = flag & 2, big_id = !(flag & 4), cond = flag & 8;
3665 11333 : const long istar_flag = (flag & nf_GEN) | nf_INIT;
3666 : pari_sp av;
3667 : long i, j;
3668 11333 : GEN nf, z, p, fa, id, BOUND, U, empty = cgetg(1,t_VEC);
3669 : forprime_t S;
3670 : ideal_data ID;
3671 : GEN (*join_z)(ideal_data*, GEN);
3672 :
3673 11333 : if (do_units)
3674 : {
3675 21 : bnf = checkbnf(bnf);
3676 21 : nf = bnf_get_nf(bnf);
3677 21 : join_z = &join_unit;
3678 : }
3679 : else
3680 : {
3681 11312 : nf = checknf(bnf);
3682 11312 : join_z = big_id? &join_idealinit: &join_ideal;
3683 : }
3684 11333 : if ((long)bound <= 0) return empty;
3685 11333 : id = matid(nf_get_degree(nf));
3686 11333 : if (big_id) id = Idealstar(nf,id, istar_flag);
3687 :
3688 : /* z[i] will contain all "objects" of norm i. Depending on flag, this means
3689 : * an ideal, a bid, or a couple [bid, log(units)]. Such objects are stored
3690 : * in vectors, computed one primary component at a time; join_z
3691 : * reconstructs the global object */
3692 11333 : BOUND = utoipos(bound);
3693 11333 : z = const_vec(bound, empty);
3694 11333 : U = do_units? log_prk_units_init(bnf): NULL;
3695 11333 : gel(z,1) = mkvec(U? mkvec2(id, empty): id);
3696 11333 : ID.nf = nf;
3697 :
3698 11333 : p = cgetipos(3);
3699 11333 : u_forprime_init(&S, 2, bound);
3700 11333 : av = avma;
3701 92918 : while ((p[2] = u_forprime_next(&S)))
3702 : {
3703 81614 : if (DEBUGLEVEL>1) err_printf("%ld ",p[2]);
3704 81614 : fa = idealprimedec_limit_norm(nf, p, BOUND);
3705 163096 : for (j = 1; j < lg(fa); j++)
3706 : {
3707 81511 : GEN pr = gel(fa,j), z2 = leafcopy(z);
3708 81514 : ulong Q, q = upr_norm(pr);
3709 : long l;
3710 81513 : ID.pr = ID.prL = pr;
3711 81513 : if (cond && q == 2) { l = 2; Q = 4; } else { l = 1; Q = q; }
3712 184525 : for (; Q <= bound; l++, Q *= q) /* add pr^l */
3713 : {
3714 : ulong iQ;
3715 103043 : ID.L = utoipos(l);
3716 103040 : if (big_id) {
3717 210 : ID.prL = Idealstarprk(nf, pr, l, istar_flag);
3718 210 : if (U)
3719 189 : ID.emb = Q == 2? empty
3720 189 : : log_prk_units(nf, U, gel(bid_get_sprk(ID.prL),1));
3721 : }
3722 862160 : for (iQ = Q,i = 1; iQ <= bound; iQ += Q,i++)
3723 759148 : concat_join(&gel(z,iQ), gel(z2,i), join_z, &ID);
3724 : }
3725 : }
3726 81585 : if (gc_needed(av,1))
3727 : {
3728 18 : if(DEBUGMEM>1) pari_warn(warnmem,"Ideallist");
3729 18 : z = gerepilecopy(av, z);
3730 : }
3731 : }
3732 11333 : return z;
3733 : }
3734 : GEN
3735 63 : gideallist(GEN bnf, GEN B, long flag)
3736 : {
3737 63 : pari_sp av = avma;
3738 63 : if (typ(B) != t_INT)
3739 : {
3740 0 : B = gfloor(B);
3741 0 : if (typ(B) != t_INT) pari_err_TYPE("ideallist", B);
3742 0 : if (signe(B) < 0) B = gen_0;
3743 : }
3744 63 : if (signe(B) < 0)
3745 : {
3746 28 : if (flag != 4) pari_err_IMPL("ideallist with bid for single norm");
3747 28 : return gerepilecopy(av, ideals_by_norm(checknf(bnf), absi(B)));
3748 : }
3749 35 : if (flag < 0 || flag > 15) pari_err_FLAG("ideallist");
3750 35 : return gerepilecopy(av, Ideallist(bnf, itou(B), flag));
3751 : }
3752 : GEN
3753 11298 : ideallist0(GEN bnf, long bound, long flag)
3754 : {
3755 11298 : pari_sp av = avma;
3756 11298 : if (flag < 0 || flag > 15) pari_err_FLAG("ideallist");
3757 11298 : return gerepilecopy(av, Ideallist(bnf, bound, flag));
3758 : }
3759 : GEN
3760 10563 : ideallist(GEN bnf,long bound) { return ideallist0(bnf,bound,4); }
3761 :
3762 : /* bid = for module m (without arch. part), arch = archimedean part.
3763 : * Output: bid for [m,arch] */
3764 : static GEN
3765 42 : join_bid_arch(GEN nf, GEN bid, GEN archp)
3766 : {
3767 42 : pari_sp av = avma;
3768 : GEN G, U;
3769 42 : GEN sprk, cyc, y, u1 = NULL, x, sarch, gen;
3770 :
3771 42 : checkbid(bid);
3772 42 : G = bid_get_grp(bid);
3773 42 : x = bid_get_ideal(bid);
3774 42 : sarch = nfarchstar(nf, bid_get_ideal(bid), archp);
3775 42 : sprk = bid_get_sprk(bid);
3776 :
3777 42 : gen = (lg(G)>3)? gel(G,3): NULL;
3778 42 : cyc = diagonal_shallow(shallowconcat(gel(G,2), sarch_get_cyc(sarch)));
3779 42 : cyc = ZM_snf_group(cyc, &U, gen? &u1: NULL);
3780 42 : y = bid_grp(nf, u1, cyc, gen, x, sarch);
3781 42 : U = split_U(U, sprk);
3782 42 : y = mkvec5(mkvec2(x, archp), y, gel(bid,3), mkvec2(sprk, sarch), U);
3783 42 : return gerepilecopy(av,y);
3784 : }
3785 : static GEN
3786 42 : join_arch(ideal_data *D, GEN x) {
3787 42 : return join_bid_arch(D->nf, x, D->archp);
3788 : }
3789 : static GEN
3790 14 : join_archunit(ideal_data *D, GEN x) {
3791 14 : GEN bid = join_arch(D, gel(x,1)), u = gel(x,2), v = mkvec(D->emb);
3792 14 : if (lg(u) != 1) v = shallowconcat(u, v);
3793 14 : return mkvec2(bid, v);
3794 : }
3795 :
3796 : /* L from ideallist, add archimedean part */
3797 : GEN
3798 14 : ideallistarch(GEN bnf, GEN L, GEN arch)
3799 : {
3800 : pari_sp av;
3801 14 : long i, j, l = lg(L), lz;
3802 : GEN v, z, V, nf;
3803 : ideal_data ID;
3804 : GEN (*join_z)(ideal_data*, GEN);
3805 :
3806 14 : if (typ(L) != t_VEC) pari_err_TYPE("ideallistarch",L);
3807 14 : if (l == 1) return cgetg(1,t_VEC);
3808 14 : z = gel(L,1);
3809 14 : if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
3810 14 : z = gel(z,1); /* either a bid or [bid,U] */
3811 14 : ID.archp = vec01_to_indices(arch);
3812 14 : if (lg(z) == 3)
3813 : { /* [bid,U]: do units */
3814 7 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
3815 7 : if (typ(z) != t_VEC) pari_err_TYPE("ideallistarch",z);
3816 7 : ID.emb = zm_to_ZM( rowpermute(nfsign_units(bnf,NULL,1), ID.archp) );
3817 7 : join_z = &join_archunit;
3818 : }
3819 : else
3820 : {
3821 7 : join_z = &join_arch;
3822 7 : nf = checknf(bnf);
3823 : }
3824 14 : ID.nf = nf;
3825 14 : av = avma; V = cgetg(l, t_VEC);
3826 63 : for (i = 1; i < l; i++)
3827 : {
3828 49 : z = gel(L,i); lz = lg(z);
3829 49 : gel(V,i) = v = cgetg(lz,t_VEC);
3830 91 : for (j=1; j<lz; j++) gel(v,j) = join_z(&ID, gel(z,j));
3831 : }
3832 14 : return gerepilecopy(av,V);
3833 : }
|