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 : #include "pari.h"
15 : #include "paripriv.h"
16 :
17 : #define DEBUGLEVEL DEBUGLEVEL_bnf
18 :
19 : /*******************************************************************/
20 : /* */
21 : /* CLASS GROUP AND REGULATOR (McCURLEY, BUCHMANN) */
22 : /* GENERAL NUMBER FIELDS */
23 : /* */
24 : /*******************************************************************/
25 : /* get_random_ideal */
26 : static const long RANDOM_BITS = 4;
27 : /* Buchall */
28 : static const long RELSUP = 5;
29 : static const long FAIL_DIVISOR = 32;
30 : static const long MINFAIL = 10;
31 : /* small_norm */
32 : static const long BNF_RELPID = 4;
33 : static const long maxtry_FACT = 500;
34 : /* rnd_rel */
35 : static const long RND_REL_RELPID = 1;
36 : /* random relations */
37 : static const long MINSFB = 3;
38 : static const long SFB_MAX = 3;
39 : static const long DEPSIZESFBMULT = 16;
40 : static const long DEPSFBDIV = 10;
41 : /* add_rel_i */
42 : static const ulong mod_p = 27449UL;
43 : /* be_honest */
44 : static const long maxtry_HONEST = 50;
45 :
46 : typedef struct FACT {
47 : long pr, ex;
48 : } FACT;
49 :
50 : typedef struct subFB_t {
51 : GEN subFB;
52 : struct subFB_t *old;
53 : } subFB_t;
54 :
55 : /* a factor base contains only noninert primes
56 : * KC = # of P in factor base (p <= n, NP <= n2)
57 : * KC2= # of P assumed to generate class group (NP <= n2)
58 : *
59 : * KCZ = # of rational primes under ideals counted by KC
60 : * KCZ2= same for KC2 */
61 :
62 : typedef struct FB_t {
63 : GEN FB; /* FB[i] = i-th rational prime used in factor base */
64 : GEN LP; /* vector of all prime ideals in FB, by increasing norm */
65 : GEN LV; /* LV[p] = vector of P|p, NP <= n2
66 : * isclone() is set for LV[p] iff all P|p are in FB
67 : * LV[i], i not prime or i > n2, is undefined! */
68 : GEN iLP; /* iLP[p] = i such that LV[p] = [LP[i],...] */
69 : GEN L_jid; /* indexes of "useful" prime ideals for rnd_rel */
70 : long KC, KCZ, KCZ2;
71 : GEN prodZ; /* product of the primes in KCZ*/
72 : GEN subFB; /* LP o subFB = part of FB used to build random relations */
73 : int sfb_chg; /* need to change subFB ? */
74 : GEN perm; /* permutation of LP used to represent relations [updated by
75 : hnfspec/hnfadd: dense rows come first] */
76 : GEN idealperm; /* permutation of ideals under field automorphisms */
77 : GEN minidx; /* minidx[i] min ideal in orbit of LP[i] under field autom */
78 : subFB_t *allsubFB; /* all subFB's used */
79 : GEN embperm; /* permutations of the complex embeddings */
80 : long MAXDEPSIZESFB; /* # trials before increasing subFB */
81 : long MAXDEPSFB; /* MAXDEPSIZESFB / DEPSFBDIV, # trials befor rotating subFB */
82 : double ballvol;
83 : } FB_t;
84 :
85 : enum { sfb_CHANGE = 1, sfb_INCREASE = 2 };
86 :
87 : typedef struct REL_t {
88 : GEN R; /* relation vector as t_VECSMALL; clone */
89 : long nz; /* index of first nonzero elt in R (hash) */
90 : GEN m; /* pseudo-minimum yielding the relation; clone */
91 : long relorig; /* relation this one is an image of */
92 : long relaut; /* automorphim used to compute this relation from the original */
93 : GEN emb; /* archimedean embeddings */
94 : GEN junk[2]; /*make sure sizeof(struct) is a power of two.*/
95 : } REL_t;
96 :
97 : typedef struct RELCACHE_t {
98 : REL_t *chk; /* last checkpoint */
99 : REL_t *base; /* first rel found */
100 : REL_t *last; /* last rel found so far */
101 : REL_t *end; /* target for last relation. base <= last <= end */
102 : size_t len; /* number of rels pre-allocated in base */
103 : long relsup; /* how many linearly dependent relations we allow */
104 : GEN basis; /* mod p basis (generating family actually) */
105 : ulong missing; /* missing vectors in generating family above */
106 : } RELCACHE_t;
107 :
108 : typedef struct FP_t {
109 : double **q;
110 : GEN x;
111 : double *y;
112 : double *z;
113 : double *v;
114 : } FP_t;
115 :
116 : typedef struct RNDREL_t {
117 : long jid;
118 : GEN ex;
119 : } RNDREL_t;
120 :
121 : static void
122 0 : wr_rel(GEN e)
123 : {
124 0 : long i, l = lg(e);
125 0 : for (i = 1; i < l; i++)
126 0 : if (e[i]) err_printf("%ld^%ld ",i,e[i]);
127 0 : }
128 : static void
129 0 : dbg_newrel(RELCACHE_t *cache)
130 : {
131 0 : if (DEBUGLEVEL > 1)
132 : {
133 0 : err_printf("\n++++ cglob = %ld\nrel = ", cache->last - cache->base);
134 0 : wr_rel(cache->last->R);
135 0 : err_printf("\n");
136 : }
137 : else
138 0 : err_printf("%ld ", cache->last - cache->base);
139 0 : }
140 :
141 : static void
142 63661 : delete_cache(RELCACHE_t *M)
143 : {
144 : REL_t *rel;
145 1063415 : for (rel = M->base+1; rel <= M->last; rel++)
146 : {
147 999754 : gunclone(rel->R);
148 999754 : if (rel->m) gunclone(rel->m);
149 : }
150 63661 : pari_free((void*)M->base); M->base = NULL;
151 63661 : }
152 :
153 : static void
154 65838 : delete_FB(FB_t *F)
155 : {
156 : subFB_t *s, *sold;
157 132784 : for (s = F->allsubFB; s; s = sold) { sold = s->old; pari_free(s); }
158 65838 : gunclone(F->minidx);
159 65838 : gunclone(F->idealperm);
160 65838 : }
161 :
162 : static void
163 63750 : reallocate(RELCACHE_t *M, long len)
164 : {
165 63750 : M->len = len;
166 63750 : if (!M->base)
167 63661 : M->base = (REL_t*)pari_malloc((len+1) * sizeof(REL_t));
168 : else
169 : {
170 89 : size_t l = M->last - M->base, c = M->chk - M->base, e = M->end - M->base;
171 89 : pari_realloc_ip((void**)&M->base, (len+1) * sizeof(REL_t));
172 89 : M->last = M->base + l;
173 89 : M->chk = M->base + c;
174 89 : M->end = M->base + e;
175 : }
176 63750 : }
177 :
178 : #define pr_get_smallp(pr) gel(pr,1)[2]
179 :
180 : /* don't take P|p all other Q|p are already there */
181 : static int
182 271582 : bad_subFB(FB_t *F, long t)
183 : {
184 271582 : GEN LP, P = gel(F->LP,t);
185 271582 : long p = pr_get_smallp(P);
186 271582 : LP = gel(F->LV,p);
187 271582 : return (isclone(LP) && t == F->iLP[p] + lg(LP)-1);
188 : }
189 :
190 : static void
191 66946 : assign_subFB(FB_t *F, GEN yes, long iyes)
192 : {
193 66946 : long i, lv = sizeof(subFB_t) + iyes*sizeof(long); /* for struct + GEN */
194 66946 : subFB_t *s = (subFB_t *)pari_malloc(lv);
195 66946 : s->subFB = (GEN)&s[1];
196 66946 : s->old = F->allsubFB; F->allsubFB = s;
197 287875 : for (i = 0; i < iyes; i++) s->subFB[i] = yes[i];
198 66946 : F->subFB = s->subFB;
199 66946 : F->MAXDEPSIZESFB = (iyes-1) * DEPSIZESFBMULT;
200 66946 : F->MAXDEPSFB = F->MAXDEPSIZESFB / DEPSFBDIV;
201 66946 : }
202 :
203 : /* Determine the permutation of the ideals made by each field automorphism */
204 : static GEN
205 65838 : FB_aut_perm(FB_t *F, GEN auts, GEN cyclic)
206 : {
207 65838 : long i, j, m, KC = F->KC, nauts = lg(auts)-1;
208 65838 : GEN minidx, perm = zero_Flm_copy(KC, nauts);
209 :
210 65838 : if (!nauts) { F->minidx = gclone(identity_zv(KC)); return cgetg(1,t_MAT); }
211 41569 : minidx = zero_Flv(KC);
212 90466 : for (m = 1; m < lg(cyclic); m++)
213 : {
214 48898 : GEN thiscyc = gel(cyclic, m);
215 48898 : long k0 = thiscyc[1];
216 48898 : GEN aut = gel(auts, k0), permk0 = gel(perm, k0), ppermk;
217 48898 : i = 1;
218 209901 : while (i <= KC)
219 : {
220 161004 : pari_sp av2 = avma;
221 161004 : GEN seen = zero_Flv(KC), P = gel(F->LP, i);
222 161004 : long imin = i, p, f, l;
223 161004 : p = pr_get_smallp(P);
224 161004 : f = pr_get_f(P);
225 : do
226 : {
227 474567 : if (++i > KC) break;
228 425670 : P = gel(F->LP, i);
229 : }
230 425670 : while (p == pr_get_smallp(P) && f == pr_get_f(P));
231 635564 : for (j = imin; j < i; j++)
232 : {
233 474566 : GEN img = ZM_ZC_mul(aut, pr_get_gen(gel(F->LP, j)));
234 1656303 : for (l = imin; l < i; l++)
235 1656303 : if (!seen[l] && ZC_prdvd(img, gel(F->LP, l)))
236 : {
237 474560 : seen[l] = 1; permk0[j] = l; break;
238 : }
239 : }
240 160998 : set_avma(av2);
241 : }
242 67879 : for (ppermk = permk0, i = 2; i < lg(thiscyc); i++)
243 : {
244 18982 : GEN permk = gel(perm, thiscyc[i]);
245 382465 : for (j = 1; j <= KC; j++) permk[j] = permk0[ppermk[j]];
246 18982 : ppermk = permk;
247 : }
248 : }
249 306481 : for (j = 1; j <= KC; j++)
250 : {
251 264913 : if (minidx[j]) continue;
252 127407 : minidx[j] = j;
253 355705 : for (i = 1; i <= nauts; i++) minidx[coeff(perm, j, i)] = j;
254 : }
255 41568 : F->minidx = gclone(minidx); return perm;
256 : }
257 :
258 : /* set subFB.
259 : * Fill F->perm (if != NULL): primes ideals sorted by increasing norm (except
260 : * the ones in subFB come first [dense rows for hnfspec]) */
261 : static void
262 65835 : subFBgen(FB_t *F, GEN auts, GEN cyclic, double PROD, long minsFB)
263 : {
264 : GEN y, perm, yes, no;
265 65835 : long i, j, k, iyes, ino, lv = F->KC + 1;
266 : double prod;
267 : pari_sp av;
268 :
269 65835 : F->LP = cgetg(lv, t_VEC);
270 65835 : F->L_jid = F->perm = cgetg(lv, t_VECSMALL);
271 65836 : av = avma;
272 65836 : y = cgetg(lv,t_COL); /* Norm P */
273 309997 : for (k=0, i=1; i <= F->KCZ; i++)
274 : {
275 244160 : GEN LP = gel(F->LV,F->FB[i]);
276 244160 : long l = lg(LP);
277 705931 : for (j = 1; j < l; j++)
278 : {
279 461781 : GEN P = gel(LP,j);
280 461781 : k++;
281 461781 : gel(y,k) = pr_norm(P);
282 461771 : gel(F->LP,k) = P;
283 : }
284 : }
285 : /* perm sorts LP by increasing norm */
286 65837 : perm = indexsort(y);
287 65838 : no = cgetg(lv, t_VECSMALL); ino = 1;
288 65838 : yes = cgetg(lv, t_VECSMALL); iyes = 1;
289 65838 : prod = 1.0;
290 301493 : for (i = 1; i < lv; i++)
291 : {
292 271582 : long t = perm[i];
293 271582 : if (bad_subFB(F, t)) { no[ino++] = t; continue; }
294 :
295 151888 : yes[iyes++] = t;
296 151888 : prod *= (double)itos(gel(y,t));
297 151888 : if (iyes > minsFB && prod > PROD) break;
298 : }
299 65838 : setlg(yes, iyes);
300 217726 : for (j=1; j<iyes; j++) F->perm[j] = yes[j];
301 185532 : for (i=1; i<ino; i++, j++) F->perm[j] = no[i];
302 256052 : for ( ; j<lv; j++) F->perm[j] = perm[j];
303 65838 : F->allsubFB = NULL;
304 65838 : F->idealperm = gclone(FB_aut_perm(F, auts, cyclic));
305 65838 : if (iyes) assign_subFB(F, yes, iyes);
306 65838 : set_avma(av);
307 65838 : }
308 : static int
309 5249 : subFB_change(FB_t *F)
310 : {
311 5249 : long i, iyes, minsFB, lv = F->KC + 1, l = lg(F->subFB)-1;
312 5249 : pari_sp av = avma;
313 5249 : GEN yes, L_jid = F->L_jid, present = zero_zv(lv-1);
314 :
315 5249 : switch (F->sfb_chg)
316 : {
317 201 : case sfb_INCREASE: minsFB = l + 1; break;
318 5048 : default: minsFB = l; break;
319 : }
320 :
321 5249 : yes = cgetg(minsFB+1, t_VECSMALL); iyes = 1;
322 5249 : if (L_jid)
323 : {
324 10277 : for (i = 1; i < lg(L_jid); i++)
325 : {
326 9796 : long l = L_jid[i];
327 9796 : yes[iyes++] = l;
328 9796 : present[l] = 1;
329 9796 : if (iyes > minsFB) break;
330 : }
331 : }
332 0 : else i = 1;
333 5249 : if (iyes <= minsFB)
334 : {
335 570 : for ( ; i < lv; i++)
336 : {
337 565 : long l = F->perm[i];
338 565 : if (present[l]) continue;
339 560 : yes[iyes++] = l;
340 560 : if (iyes > minsFB) break;
341 : }
342 481 : if (i == lv) return 0;
343 : }
344 5244 : if (zv_equal(F->subFB, yes))
345 : {
346 4136 : if (DEBUGLEVEL) err_printf("\n*** NOT Changing sub factor base\n");
347 : }
348 : else
349 : {
350 1108 : if (DEBUGLEVEL) err_printf("\n*** Changing sub factor base\n");
351 1108 : assign_subFB(F, yes, iyes);
352 : }
353 5244 : F->sfb_chg = 0; return gc_bool(av, 1);
354 : }
355 :
356 : /* make sure enough room to store n more relations */
357 : static void
358 106471 : pre_allocate(RELCACHE_t *cache, size_t n)
359 : {
360 106471 : size_t len = (cache->last - cache->base) + n;
361 106471 : if (len >= cache->len) reallocate(cache, len << 1);
362 106471 : }
363 :
364 : void
365 133872 : init_GRHcheck(GRHcheck_t *S, long N, long R1, double LOGD)
366 : {
367 133872 : const double c1 = M_PI*M_PI/2;
368 133872 : const double c2 = 3.663862376709;
369 133872 : const double c3 = 3.801387092431; /* Euler + log(8*Pi)*/
370 133872 : S->clone = 0;
371 133872 : S->cN = R1*c2 + N*c1;
372 133872 : S->cD = LOGD - N*c3 - R1*M_PI/2;
373 133872 : S->maxprimes = 16000; /* sufficient for LIMC=176081*/
374 133872 : S->primes = (GRHprime_t*)pari_malloc(S->maxprimes*sizeof(*S->primes));
375 133879 : S->nprimes = 0;
376 133879 : S->limp = 0;
377 133879 : u_forprime_init(&S->P, 2, ULONG_MAX);
378 133876 : }
379 :
380 : void
381 133881 : free_GRHcheck(GRHcheck_t *S)
382 : {
383 133881 : if (S->clone)
384 : {
385 63623 : long i = S->nprimes;
386 : GRHprime_t *pr;
387 7526145 : for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--) gunclone(pr->dec);
388 : }
389 133893 : pari_free(S->primes);
390 133881 : }
391 :
392 : int
393 1526097 : GRHok(GRHcheck_t *S, double L, double SA, double SB)
394 : {
395 1526097 : return (S->cD + (S->cN + 2*SB) / L - 2*SA < -1e-8);
396 : }
397 :
398 : /* Return factorization pattern of p: [f,n], where n[i] primes of
399 : * residue degree f[i] */
400 : static GEN
401 7458154 : get_fs(GEN nf, GEN P, GEN index, ulong p)
402 : {
403 : long j, k, f, n, l;
404 : GEN fs, ns;
405 :
406 7458154 : if (umodiu(index, p))
407 : { /* easy case: p does not divide index */
408 7419881 : GEN F = Flx_degfact(ZX_to_Flx(P,p), p);
409 7420522 : fs = gel(F,1); l = lg(fs);
410 : }
411 : else
412 : {
413 37913 : GEN F = idealprimedec(nf, utoipos(p));
414 37961 : l = lg(F);
415 37961 : fs = cgetg(l, t_VECSMALL);
416 118944 : for (j = 1; j < l; j++) fs[j] = pr_get_f(gel(F,j));
417 : }
418 7458483 : ns = cgetg(l, t_VECSMALL);
419 7455032 : f = fs[1]; n = 1;
420 13794871 : for (j = 2, k = 1; j < l; j++)
421 6339839 : if (fs[j] == f)
422 4618734 : n++;
423 : else
424 : {
425 1721105 : ns[k] = n; fs[k] = f; k++;
426 1721105 : f = fs[j]; n = 1;
427 : }
428 7455032 : ns[k] = n; fs[k] = f; k++;
429 7455032 : setlg(fs, k);
430 7455222 : setlg(ns, k); return mkvec2(fs,ns);
431 : }
432 :
433 : /* cache data for all rational primes up to the LIM */
434 : static void
435 915379 : cache_prime_dec(GRHcheck_t *S, ulong LIM, GEN nf)
436 : {
437 915379 : pari_sp av = avma;
438 : GRHprime_t *pr;
439 : GEN index, P;
440 : double nb;
441 :
442 915379 : if (S->limp >= LIM) return;
443 327615 : S->clone = 1;
444 327615 : nb = primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
445 327634 : GRH_ensure(S, nb+1); /* room for one extra prime */
446 327633 : P = nf_get_pol(nf);
447 327631 : index = nf_get_index(nf);
448 327630 : for (pr = S->primes + S->nprimes;;)
449 7131093 : {
450 7458723 : ulong p = u_forprime_next(&(S->P));
451 7457486 : pr->p = p;
452 7457486 : pr->logp = log((double)p);
453 7457486 : pr->dec = gclone(get_fs(nf, P, index, p));
454 7458700 : S->nprimes++;
455 7458700 : pr++;
456 7458700 : set_avma(av);
457 : /* store up to nextprime(LIM) included */
458 7458724 : if (p >= LIM) { S->limp = p; break; }
459 : }
460 : }
461 :
462 : static double
463 2245287 : tailresback(long R1, long R2, double rK, long C, double C2, double C3, double r1K, double r2K, double logC, double logC2, double logC3)
464 : {
465 2245287 : const double rQ = 1.83787706641;
466 2245287 : const double r1Q = 1.98505372441;
467 2245287 : const double r2Q = 1.07991541347;
468 4490574 : return fabs((R1+R2-1)*(12*logC3+4*logC2-9*logC-6)/(2*C*logC3)
469 2245287 : + (rK-rQ)*(6*logC2 + 5*logC + 2)/(C*logC3)
470 2245287 : - R2*(6*logC2+11*logC+6)/(C2*logC2)
471 2245287 : - 2*(r1K-r1Q)*(3*logC2 + 4*logC + 2)/(C2*logC3)
472 2245287 : + (R1+R2-1)*(12*logC3+40*logC2+45*logC+18)/(6*C3*logC3)
473 2245287 : + (r2K-r2Q)*(2*logC2 + 3*logC + 2)/(C3*logC3));
474 : }
475 :
476 : static double
477 1122644 : tailres(long R1, long R2, double al2K, double rKm, double rKM, double r1Km,
478 : double r1KM, double r2Km, double r2KM, double C, long i)
479 : {
480 : /* C >= 3*2^i, lower bound for eint1(log(C)/2) */
481 : /* for(i=0,30,print(eint1(log(3*2^i)/2))) */
482 : static double tab[] = {
483 : 0.50409264803,
484 : 0.26205336997,
485 : 0.14815491171,
486 : 0.08770540561,
487 : 0.05347651832,
488 : 0.03328934284,
489 : 0.02104510690,
490 : 0.01346475900,
491 : 0.00869778586,
492 : 0.00566279855,
493 : 0.00371111950,
494 : 0.00244567837,
495 : 0.00161948049,
496 : 0.00107686891,
497 : 0.00071868750,
498 : 0.00048119961,
499 : 0.00032312188,
500 : 0.00021753772,
501 : 0.00014679818,
502 : 9.9272855581E-5,
503 : 6.7263969995E-5,
504 : 4.5656812967E-5,
505 : 3.1041124593E-5,
506 : 2.1136011590E-5,
507 : 1.4411645381E-5,
508 : 9.8393304088E-6,
509 : 6.7257395409E-6,
510 : 4.6025878272E-6,
511 : 3.1529719271E-6,
512 : 2.1620490021E-6,
513 : 1.4839266071E-6
514 : };
515 1122644 : const double logC = log(C), logC2 = logC*logC, logC3 = logC*logC2;
516 1122644 : const double C2 = C*C, C3 = C*C2;
517 1122644 : double E1 = i >30? 0: tab[i];
518 1122644 : return al2K*((33*logC2+22*logC+8)/(8*logC3*sqrt(C))+15*E1/16)
519 1122644 : + maxdd(tailresback(rKm,r1KM,r2Km, C,C2,C3,R1,R2,logC,logC2,logC3),
520 1122651 : tailresback(rKM,r1Km,r2KM, C,C2,C3,R1,R2,logC,logC2,logC3))/2
521 1122651 : + ((R1+R2-1)*4*C+R2)*(C2+6*logC)/(4*C2*C2*logC2);
522 : }
523 :
524 : static long
525 63623 : primeneeded(long N, long R1, long R2, double LOGD)
526 : {
527 63623 : const double lim = 0.25; /* should be log(2)/2 == 0.34657... */
528 63623 : const double al2K = 0.3526*LOGD - 0.8212*N + 4.5007;
529 63623 : const double rKm = -1.0155*LOGD + 2.1042*N - 8.3419;
530 63623 : const double rKM = -0.5 *LOGD + 1.2076*N + 1;
531 63623 : const double r1Km = - LOGD + 1.4150*N;
532 63623 : const double r1KM = - LOGD + 1.9851*N;
533 63623 : const double r2Km = - LOGD + 0.9151*N;
534 63623 : const double r2KM = - LOGD + 1.0800*N;
535 63623 : long Cmin = 3, Cmax = 3, i = 0;
536 570766 : while (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, Cmax, i) > lim)
537 : {
538 507143 : Cmin = Cmax;
539 507143 : Cmax *= 2;
540 507143 : i++;
541 : }
542 63618 : i--;
543 615520 : while (Cmax - Cmin > 1)
544 : {
545 551901 : long t = (Cmin + Cmax)/2;
546 551901 : if (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, t, i) > lim)
547 341896 : Cmin = t;
548 : else
549 210006 : Cmax = t;
550 : }
551 63619 : return Cmax;
552 : }
553 :
554 : /* ~ 1 / Res(s = 1, zeta_K) */
555 : static GEN
556 63621 : compute_invres(GRHcheck_t *S, long LIMC)
557 : {
558 63621 : pari_sp av = avma;
559 63621 : double loginvres = 0.;
560 : GRHprime_t *pr;
561 : long i;
562 63621 : double logLIMC = log((double)LIMC);
563 63621 : double logLIMC2 = logLIMC*logLIMC, denc;
564 : double c0, c1, c2;
565 63621 : denc = 1/(pow((double)LIMC, 3.) * logLIMC * logLIMC2);
566 63621 : c2 = ( logLIMC2 + 3 * logLIMC / 2 + 1) * denc;
567 63621 : denc *= LIMC;
568 63621 : c1 = (3 * logLIMC2 + 4 * logLIMC + 2) * denc;
569 63621 : denc *= LIMC;
570 63621 : c0 = (3 * logLIMC2 + 5 * logLIMC / 2 + 1) * denc;
571 7469752 : for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--)
572 : {
573 : GEN dec, fs, ns;
574 : long addpsi;
575 : double addpsi1, addpsi2;
576 7461858 : double logp = pr->logp, NPk;
577 7461858 : long j, k, limp = logLIMC/logp;
578 7461858 : ulong p = pr->p, p2 = p*p;
579 7461858 : if (limp < 1) break;
580 7406131 : dec = pr->dec;
581 7406131 : fs = gel(dec, 1); ns = gel(dec, 2);
582 7406131 : loginvres += 1./p;
583 : /* NB: limp = 1 nearly always and limp > 2 for very few primes */
584 8764054 : for (k=2, NPk = p; k <= limp; k++) { NPk *= p; loginvres += 1/(k * NPk); }
585 7406131 : addpsi = limp;
586 7406131 : addpsi1 = p *(pow((double)p , (double)limp)-1)/(p -1);
587 7406131 : addpsi2 = p2*(pow((double)p2, (double)limp)-1)/(p2-1);
588 7406131 : j = lg(fs);
589 16522979 : while (--j > 0)
590 : {
591 : long f, nb, kmax;
592 : double NP, NP2, addinvres;
593 9116848 : f = fs[j]; if (f > limp) continue;
594 3963944 : nb = ns[j];
595 3963944 : NP = pow((double)p, (double)f);
596 3963944 : addinvres = 1/NP;
597 3963944 : kmax = limp / f;
598 4837241 : for (k=2, NPk = NP; k <= kmax; k++) { NPk *= NP; addinvres += 1/(k*NPk); }
599 3963944 : NP2 = NP*NP;
600 3963944 : loginvres -= nb * addinvres;
601 3963944 : addpsi -= nb * f * kmax;
602 3963944 : addpsi1 -= nb*(f*NP *(pow(NP ,(double)kmax)-1)/(NP -1));
603 3963944 : addpsi2 -= nb*(f*NP2*(pow(NP2,(double)kmax)-1)/(NP2-1));
604 : }
605 7406131 : loginvres -= (addpsi*c0 - addpsi1*c1 + addpsi2*c2)*logp;
606 : }
607 63621 : return gerepileuptoleaf(av, mpexp(dbltor(loginvres)));
608 : }
609 :
610 : static long
611 63623 : nthideal(GRHcheck_t *S, GEN nf, long n)
612 : {
613 63623 : pari_sp av = avma;
614 63623 : GEN P = nf_get_pol(nf);
615 63623 : ulong p = 0, *vecN = (ulong*)const_vecsmall(n, LONG_MAX);
616 63622 : long i, N = poldegree(P, -1);
617 63623 : for (i = 0; ; i++)
618 229024 : {
619 : GRHprime_t *pr;
620 : GEN fs;
621 292647 : cache_prime_dec(S, p+1, nf);
622 292647 : pr = S->primes + i;
623 292647 : fs = gel(pr->dec, 1);
624 292647 : p = pr->p;
625 292647 : if (fs[1] != N)
626 : {
627 196462 : GEN ns = gel(pr->dec, 2);
628 196462 : long k, l, j = lg(fs);
629 440550 : while (--j > 0)
630 : {
631 244088 : ulong NP = upowuu(p, fs[j]);
632 : long nf;
633 244088 : if (!NP) continue;
634 749398 : for (k = 1; k <= n; k++) if (vecN[k] > NP) break;
635 243696 : if (k > n) continue;
636 : /* vecN[k] <= NP */
637 157805 : nf = ns[j]; /*#{primes of norme NP} = nf, insert them here*/
638 353132 : for (l = k+nf; l <= n; l++) vecN[l] = vecN[l-nf];
639 398561 : for (l = 0; l < nf && k+l <= n; l++) vecN[k+l] = NP;
640 362990 : while (l <= k) vecN[l++] = NP;
641 : }
642 : }
643 292647 : if (p > vecN[n]) break;
644 : }
645 63623 : return gc_long(av, vecN[n]);
646 : }
647 :
648 : /* volume of unit ball in R^n: \pi^{n/2} / \Gamma(n/2 + 1) */
649 : static double
650 65833 : ballvol(long n)
651 : {
652 65833 : double v = odd(n)? 2: 1;
653 150332 : for (; n > 1; n -= 2) v *= (2 * M_PI) / n;
654 65833 : return v;
655 : }
656 :
657 : /* Compute FB, LV, iLP + KC*. Reset perm
658 : * C2: bound for norm of tested prime ideals (includes be_honest())
659 : * C1: bound for p, such that P|p (NP <= C2) used to build relations */
660 : static void
661 65837 : FBgen(FB_t *F, GEN nf, long N, ulong C1, ulong C2, GRHcheck_t *S)
662 : {
663 : GRHprime_t *pr;
664 : long i, ip;
665 : GEN prim;
666 65837 : const double L = log((double)C2 + 0.5);
667 :
668 65837 : cache_prime_dec(S, C2, nf);
669 65838 : pr = S->primes;
670 65838 : F->sfb_chg = 0;
671 65838 : F->FB = cgetg(C2+1, t_VECSMALL);
672 65837 : F->iLP = cgetg(C2+1, t_VECSMALL);
673 65837 : F->LV = zerovec(C2);
674 :
675 65837 : prim = icopy(gen_1);
676 65838 : i = ip = 0;
677 65838 : F->KC = F->KCZ = 0;
678 432738 : for (;; pr++) /* p <= C2 */
679 432738 : {
680 498576 : ulong p = pr->p;
681 : long k, l, m;
682 : GEN LP, nb, f;
683 :
684 498576 : if (!F->KC && p > C1) { F->KCZ = i; F->KC = ip; }
685 498576 : if (p > C2) break;
686 :
687 461424 : if (DEBUGLEVEL>1) err_printf(" %ld",p);
688 :
689 461424 : f = gel(pr->dec, 1); nb = gel(pr->dec, 2);
690 461424 : if (f[1] == N)
691 : {
692 144986 : if (p == C2) break;
693 136460 : continue; /* p inert */
694 : }
695 316438 : l = (long)(L/pr->logp); /* p^f <= C2 <=> f <= l */
696 577285 : for (k=0, m=1; m < lg(f) && f[m]<=l; m++) k += nb[m];
697 316438 : if (!k)
698 : { /* too inert to appear in FB */
699 72266 : if (p == C2) break;
700 71636 : continue;
701 : }
702 244172 : prim[2] = p; LP = idealprimedec_limit_f(nf,prim, l);
703 : /* keep noninert ideals with Norm <= C2 */
704 244172 : if (m == lg(f)) setisclone(LP); /* flag it: all prime divisors in FB */
705 244172 : F->FB[++i]= p;
706 244172 : gel(F->LV,p) = LP;
707 244172 : F->iLP[p] = ip; ip += k;
708 244172 : if (p == C2) break;
709 : }
710 65838 : if (!F->KC) { F->KCZ = i; F->KC = ip; }
711 : /* Note F->KC > 0 otherwise GRHchk is false */
712 65838 : setlg(F->FB, F->KCZ+1); F->KCZ2 = i;
713 65837 : F->prodZ = zv_prod_Z(F->FB);
714 65832 : if (DEBUGLEVEL>1)
715 : {
716 0 : err_printf("\n");
717 0 : if (DEBUGLEVEL>6)
718 : {
719 0 : err_printf("########## FACTORBASE ##########\n\n");
720 0 : err_printf("KC2=%ld, KC=%ld, KCZ=%ld, KCZ2=%ld\n",
721 : ip, F->KC, F->KCZ, F->KCZ2);
722 0 : for (i=1; i<=F->KCZ; i++) err_printf("++ LV[%ld] = %Ps",i,gel(F->LV,F->FB[i]));
723 : }
724 : }
725 65832 : F->perm = NULL; F->L_jid = NULL;
726 65832 : F->ballvol = ballvol(nf_get_degree(nf));
727 65835 : }
728 :
729 : static int
730 493287 : GRHchk(GEN nf, GRHcheck_t *S, ulong LIMC)
731 : {
732 493287 : double logC = log((double)LIMC), SA = 0, SB = 0;
733 493287 : GRHprime_t *pr = S->primes;
734 :
735 493287 : cache_prime_dec(S, LIMC, nf);
736 493286 : for (pr = S->primes;; pr++)
737 3032270 : {
738 3525556 : ulong p = pr->p;
739 : GEN dec, fs, ns;
740 : double logCslogp;
741 : long j;
742 :
743 3525556 : if (p > LIMC) break;
744 3137915 : dec = pr->dec; fs = gel(dec, 1); ns = gel(dec,2);
745 3137915 : logCslogp = logC/pr->logp;
746 4939341 : for (j = 1; j < lg(fs); j++)
747 : {
748 3866002 : long f = fs[j], M, nb;
749 : double logNP, q, A, B;
750 3866002 : if (f > logCslogp) break;
751 1801423 : logNP = f * pr->logp;
752 1801423 : q = 1/sqrt((double)upowuu(p, f));
753 1801426 : A = logNP * q; B = logNP * A; M = (long)(logCslogp/f);
754 1801426 : if (M > 1)
755 : {
756 374275 : double inv1_q = 1 / (1-q);
757 374275 : A *= (1 - pow(q, (double)M)) * inv1_q;
758 374275 : B *= (1 - pow(q, (double)M)*(M+1 - M*q)) * inv1_q * inv1_q;
759 : }
760 1801426 : nb = ns[j];
761 1801426 : SA += nb * A;
762 1801426 : SB += nb * B;
763 : }
764 3137918 : if (p == LIMC) break;
765 : }
766 493289 : return GRHok(S, logC, SA, SB);
767 : }
768 :
769 : /* SMOOTH IDEALS */
770 : static void
771 9296047 : store(long i, long e, FACT *fact)
772 : {
773 9296047 : ++fact[0].pr;
774 9296047 : fact[fact[0].pr].pr = i; /* index */
775 9296047 : fact[fact[0].pr].ex = e; /* exponent */
776 9296047 : }
777 :
778 : /* divide out x by all P|p, where x as in can_factor(). k = v_p(Nx) */
779 : static int
780 5815062 : divide_p_elt(GEN LP, long ip, long k, GEN m, FACT *fact)
781 : {
782 5815062 : long j, l = lg(LP);
783 18548617 : for (j=1; j<l; j++)
784 : {
785 18460316 : GEN P = gel(LP,j);
786 18460316 : long v = ZC_nfval(m, P);
787 18457907 : if (!v) continue;
788 8540202 : store(ip + j, v, fact); /* v = v_P(m) > 0 */
789 8542288 : k -= v * pr_get_f(P);
790 8542580 : if (!k) return 1;
791 : }
792 88301 : return 0;
793 : }
794 : static int
795 162968 : divide_p_id(GEN LP, long ip, long k, GEN nf, GEN I, FACT *fact)
796 : {
797 162968 : long j, l = lg(LP);
798 244428 : for (j=1; j<l; j++)
799 : {
800 236567 : GEN P = gel(LP,j);
801 236567 : long v = idealval(nf,I, P);
802 236567 : if (!v) continue;
803 158563 : store(ip + j, v, fact); /* v = v_P(I) > 0 */
804 158563 : k -= v * pr_get_f(P);
805 158563 : if (!k) return 1;
806 : }
807 7861 : return 0;
808 : }
809 : static int
810 547210 : divide_p_quo(GEN LP, long ip, long k, GEN nf, GEN I, GEN m, FACT *fact)
811 : {
812 547210 : long j, l = lg(LP);
813 799195 : for (j=1; j<l; j++)
814 : {
815 798920 : GEN P = gel(LP,j);
816 798920 : long v = ZC_nfval(m, P);
817 798920 : if (!v) continue;
818 576764 : v -= idealval(nf,I, P);
819 576764 : if (!v) continue;
820 569886 : store(ip + j, v, fact); /* v = v_P(m / I) > 0 */
821 569886 : k -= v * pr_get_f(P);
822 569886 : if (!k) return 1;
823 : }
824 275 : return 0;
825 : }
826 :
827 : /* |*N| != 0 is the norm of a primitive ideal, in particular not divisible by
828 : * any inert prime. Is |*N| a smooth rational integer wrt F ?
829 : */
830 : static int
831 18935637 : Z_issmooth_prod(GEN N, GEN P)
832 : {
833 18935637 : P = gcdii(P,N);
834 105227807 : while (!is_pm1(P))
835 : {
836 86297381 : N = diviiexact(N, P);
837 86296129 : P = gcdii(N, P);
838 : }
839 18910739 : return is_pm1(N);
840 : }
841 :
842 : static int
843 6525185 : divide_p(FB_t *F, long p, long k, GEN nf, GEN I, GEN m, FACT *fact)
844 : {
845 6525185 : GEN LP = gel(F->LV,p);
846 6525185 : long ip = F->iLP[p];
847 6525185 : if (!m) return divide_p_id (LP,ip,k,nf,I,fact);
848 6362217 : if (!I) return divide_p_elt(LP,ip,k,m,fact);
849 547189 : return divide_p_quo(LP,ip,k,nf,I,m,fact);
850 : }
851 :
852 : /* Let x = m if I == NULL,
853 : * I if m == NULL,
854 : * m/I otherwise.
855 : * Can we factor the integral primitive ideal x ? |N| = Norm x > 0 */
856 : static long
857 19710579 : can_factor(FB_t *F, GEN nf, GEN I, GEN m, GEN N, FACT *fact)
858 : {
859 : GEN f, p, e;
860 : long i, l;
861 19710579 : fact[0].pr = 0;
862 19710579 : if (is_pm1(N)) return 1;
863 18935661 : if (!Z_issmooth_prod(N, F->prodZ)) return 0;
864 2946923 : f = absZ_factor(N); p = gel(f,1); e = gel(f,2); l = lg(p);
865 9377364 : for (i = 1; i < l; i++)
866 6525110 : if (!divide_p(F, itou(gel(p,i)), itou(gel(e,i)), nf, I, m, fact))
867 : {
868 95667 : if (DEBUGLEVEL > 1) err_printf(".");
869 95667 : return 0;
870 : }
871 2852254 : return 1;
872 : }
873 :
874 : /* can we factor m/I ? [m in I from idealpseudomin_nonscalar], NI = norm I */
875 : static long
876 1500591 : factorgen(FB_t *F, GEN nf, GEN I, GEN NI, GEN m, FACT *fact)
877 : {
878 1500591 : long e, r1 = nf_get_r1(nf);
879 1500593 : GEN M = nf_get_M(nf);
880 1500593 : GEN N = divri(embed_norm(RgM_RgC_mul(M,m), r1), NI); /* ~ N(m/I) */
881 1500594 : N = grndtoi(N, &e);
882 1500592 : if (e > -32)
883 : {
884 0 : if (DEBUGLEVEL > 1) err_printf("+");
885 0 : return 0;
886 : }
887 1500592 : return can_factor(F, nf, I, m, N, fact);
888 : }
889 :
890 : /* FUNDAMENTAL UNITS */
891 :
892 : /* a, y real. Return (Re(x) + a) + I * (Im(x) % y) */
893 : static GEN
894 6584991 : addRe_modIm(GEN x, GEN a, GEN y, GEN iy)
895 : {
896 : GEN z;
897 6584991 : if (typ(x) == t_COMPLEX)
898 : {
899 4667928 : GEN re, im = modRr_i(gel(x,2), y, iy);
900 4667822 : if (!im) return NULL;
901 4667822 : re = gadd(gel(x,1), a);
902 4667824 : z = gequal0(im)? re: mkcomplex(re, im);
903 : }
904 : else
905 1917063 : z = gadd(x, a);
906 6584935 : return z;
907 : }
908 : static GEN
909 201217 : modIm(GEN x, GEN y, GEN iy)
910 : {
911 201217 : if (typ(x) == t_COMPLEX)
912 : {
913 188588 : GEN im = modRr_i(gel(x,2), y, iy);
914 188579 : if (!im) return NULL;
915 188579 : x = gequal0(im)? gel(x,1): mkcomplex(gel(x,1), im);
916 : }
917 201216 : return x;
918 : }
919 :
920 : /* clean archimedean components. ipi = 2^n / pi (n arbitrary); its
921 : * exponent may be modified */
922 : static GEN
923 2923623 : cleanarch(GEN x, long N, GEN ipi, long prec)
924 : {
925 : long i, l, R1, RU;
926 2923623 : GEN s, y = cgetg_copy(x, &l);
927 :
928 2923633 : if (!ipi) ipi = invr(mppi(prec));
929 2923632 : if (typ(x) == t_MAT)
930 : {
931 523419 : for (i = 1; i < l; i++)
932 459644 : if (!(gel(y,i) = cleanarch(gel(x,i), N, ipi, prec))) return NULL;
933 63775 : return y;
934 : }
935 2859856 : RU = l-1; R1 = (RU<<1) - N;
936 2859856 : s = gdivgs(RgV_sum(real_i(x)), -N); /* -log |norm(x)| / N */
937 2859824 : i = 1;
938 2859824 : if (R1)
939 : {
940 2382180 : GEN pi2 = Pi2n(1, prec);
941 2382185 : setexpo(ipi, -3); /* 1/(2pi) */
942 7334373 : for (; i <= R1; i++)
943 4952213 : if (!(gel(y,i) = addRe_modIm(gel(x,i), s, pi2, ipi))) return NULL;
944 : }
945 2859804 : if (i <= RU)
946 : {
947 1074700 : GEN pi4 = Pi2n(2, prec), s2 = gmul2n(s, 1);
948 1074714 : setexpo(ipi, -4); /* 1/(4pi) */
949 2707518 : for (; i <= RU; i++)
950 1632779 : if (!(gel(y,i) = addRe_modIm(gel(x,i), s2, pi4, ipi))) return NULL;
951 : }
952 2859843 : return y;
953 : }
954 : GEN
955 195050 : nf_cxlog_normalize(GEN nf, GEN x, long prec)
956 : {
957 195050 : long N = nf_get_degree(nf);
958 195050 : return cleanarch(x, N, NULL, prec);
959 : }
960 :
961 : /* clean unit archimedean components. ipi = 2^n / pi (n arbitrary); its
962 : * exponent may be modified */
963 : static GEN
964 132578 : cleanarchunit(GEN x, long N, GEN ipi, long prec)
965 : {
966 : long i, l, R1, RU;
967 132578 : GEN y = cgetg_copy(x, &l);
968 :
969 132580 : if (!ipi) ipi = invr(mppi(prec));
970 132579 : if (typ(x) == t_MAT)
971 : {
972 132577 : for (i = 1; i < l; i++)
973 68955 : if (!(gel(y,i) = cleanarchunit(gel(x,i), N, ipi, prec))) return NULL;
974 63622 : return y;
975 : }
976 68957 : if (gexpo(RgV_sum(real_i(x))) > -10) return NULL;
977 68954 : RU = l-1; R1 = (RU<<1) - N;
978 68954 : i = 1;
979 68954 : if (R1)
980 : {
981 54556 : GEN pi2 = Pi2n(1, prec);
982 54555 : setexpo(ipi, -3); /* 1/(2pi) */
983 185394 : for (; i <= R1; i++)
984 130839 : if (!(gel(y,i) = modIm(gel(x,i), pi2, ipi))) return NULL;
985 : }
986 68953 : if (i <= RU)
987 : {
988 34356 : GEN pi4 = Pi2n(2, prec);
989 34356 : setexpo(ipi, -4); /* 1/(4pi) */
990 104736 : for (; i <= RU; i++)
991 70378 : if (!(gel(y,i) = modIm(gel(x,i), pi4, ipi))) return NULL;
992 : }
993 68955 : return y;
994 : }
995 :
996 : static GEN
997 375 : not_given(long reason)
998 : {
999 375 : if (DEBUGLEVEL)
1000 0 : switch(reason)
1001 : {
1002 0 : case fupb_LARGE:
1003 0 : pari_warn(warner,"fundamental units too large, not given");
1004 0 : break;
1005 0 : case fupb_PRECI:
1006 0 : pari_warn(warner,"insufficient precision for fundamental units, not given");
1007 0 : break;
1008 : }
1009 375 : return NULL;
1010 : }
1011 :
1012 : /* check whether exp(x) will 1) get too big (real(x) large), 2) require
1013 : * large accuracy for argument reduction (imag(x) large) */
1014 : static long
1015 2683782 : expbitprec(GEN x, long *e)
1016 : {
1017 : GEN re, im;
1018 2683782 : if (typ(x) != t_COMPLEX) re = x;
1019 : else
1020 : {
1021 1670250 : im = gel(x,2); *e = maxss(*e, expo(im) + 5 - bit_prec(im));
1022 1670250 : re = gel(x,1);
1023 : }
1024 2683782 : return (expo(re) <= 20);
1025 :
1026 : }
1027 : static long
1028 1166079 : RgC_expbitprec(GEN x)
1029 : {
1030 1166079 : long l = lg(x), i, e = - (long)HIGHEXPOBIT;
1031 3648290 : for (i = 1; i < l; i++)
1032 2482666 : if (!expbitprec(gel(x,i), &e)) return LONG_MAX;
1033 1165624 : return e;
1034 : }
1035 : static long
1036 48433 : RgM_expbitprec(GEN x)
1037 : {
1038 48433 : long i, j, I, J, e = - (long)HIGHEXPOBIT;
1039 48433 : RgM_dimensions(x, &I,&J);
1040 117327 : for (j = 1; j <= J; j++)
1041 270010 : for (i = 1; i <= I; i++)
1042 201116 : if (!expbitprec(gcoeff(x,i,j), &e)) return LONG_MAX;
1043 48377 : return e;
1044 : }
1045 :
1046 : static GEN
1047 1379 : FlxqX_chinese_unit(GEN X, GEN U, GEN invzk, GEN D, GEN T, ulong p)
1048 : {
1049 1379 : long i, lU = lg(U), lX = lg(X), d = lg(invzk)-1;
1050 1379 : GEN M = cgetg(lU, t_MAT);
1051 1379 : if (D)
1052 : {
1053 1272 : D = Flv_inv(D, p);
1054 69767 : for (i = 1; i < lX; i++)
1055 68495 : if (uel(D, i) != 1)
1056 56639 : gel(X,i) = Flx_Fl_mul(gel(X,i), uel(D,i), p);
1057 : }
1058 3878 : for (i = 1; i < lU; i++)
1059 : {
1060 2499 : GEN H = FlxqV_factorback(X, gel(U, i), T, p);
1061 2499 : gel(M, i) = Flm_Flc_mul(invzk, Flx_to_Flv(H, d), p);
1062 : }
1063 1379 : return M;
1064 : }
1065 :
1066 : static GEN
1067 274 : chinese_unit_slice(GEN A, GEN U, GEN B, GEN D, GEN C, GEN P, GEN *mod)
1068 : {
1069 274 : pari_sp av = avma;
1070 274 : long i, n = lg(P)-1, v = varn(C);
1071 : GEN H, T;
1072 274 : if (n == 1)
1073 : {
1074 0 : ulong p = uel(P,1);
1075 0 : GEN a = ZXV_to_FlxV(A, p), b = ZM_to_Flm(B, p), c = ZX_to_Flx(C, p);
1076 0 : GEN d = D ? ZV_to_Flv(D, p): NULL;
1077 0 : GEN Hp = FlxqX_chinese_unit(a, U, b, d, c, p);
1078 0 : H = gerepileupto(av, Flm_to_ZM(Hp));
1079 0 : *mod = utoi(p);
1080 0 : return H;
1081 : }
1082 274 : T = ZV_producttree(P);
1083 274 : A = ZXC_nv_mod_tree(A, P, T, v);
1084 274 : B = ZM_nv_mod_tree(B, P, T);
1085 274 : D = D ? ZV_nv_mod_tree(D, P, T): NULL;
1086 274 : C = ZX_nv_mod_tree(C, P, T);
1087 :
1088 274 : H = cgetg(n+1, t_VEC);
1089 1653 : for(i=1; i <= n; i++)
1090 : {
1091 1379 : ulong p = P[i];
1092 1379 : GEN a = gel(A,i), b = gel(B,i), c = gel(C,i), d = D ? gel(D,i): NULL;
1093 1379 : gel(H,i) = FlxqX_chinese_unit(a, U, b, d, c, p);
1094 : }
1095 274 : H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
1096 274 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
1097 : }
1098 :
1099 : GEN
1100 274 : chinese_unit_worker(GEN P, GEN A, GEN U, GEN B, GEN D, GEN C)
1101 : {
1102 274 : GEN V = cgetg(3, t_VEC);
1103 274 : gel(V,1) = chinese_unit_slice(A, U, B, isintzero(D) ? NULL: D, C, P, &gel(V,2));
1104 274 : return V;
1105 : }
1106 :
1107 : /* Let x = \prod X[i]^E[i] = u, return u.
1108 : * If dX != NULL, X[i] = nX[i] / dX[i] where nX[i] is a ZX, dX[i] in Z */
1109 : static GEN
1110 94 : chinese_unit(GEN nf, GEN nX, GEN dX, GEN U, ulong bnd)
1111 : {
1112 94 : pari_sp av = avma;
1113 94 : GEN f = nf_get_index(nf), T = nf_get_pol(nf), invzk = nf_get_invzk(nf);
1114 : GEN H, mod;
1115 : forprime_t S;
1116 94 : GEN worker = snm_closure(is_entry("_chinese_unit_worker"),
1117 : mkcol5(nX, U, invzk, dX? dX: gen_0, T));
1118 94 : init_modular_big(&S);
1119 94 : H = gen_crt("chinese_units", worker, &S, f, bnd, 0, &mod, nmV_chinese_center, FpM_center);
1120 94 : settyp(H, t_VEC); return gerepilecopy(av, H);
1121 : }
1122 :
1123 : /* *pE a ZM */
1124 : static void
1125 164 : ZM_remove_unused(GEN *pE, GEN *pX)
1126 : {
1127 164 : long j, k, l = lg(*pX);
1128 164 : GEN E = *pE, v = cgetg(l, t_VECSMALL);
1129 16349 : for (j = k = 1; j < l; j++)
1130 16185 : if (!ZMrow_equal0(E, j)) v[k++] = j;
1131 164 : if (k < l)
1132 : {
1133 164 : setlg(v, k);
1134 164 : *pX = vecpermute(*pX,v);
1135 164 : *pE = rowpermute(E,v);
1136 : }
1137 164 : }
1138 :
1139 : /* s = -log|norm(x)|/N */
1140 : static GEN
1141 1235028 : fixarch(GEN x, GEN s, long R1)
1142 : {
1143 : long i, l;
1144 1235028 : GEN y = cgetg_copy(x, &l);
1145 3418953 : for (i = 1; i <= R1; i++) gel(y,i) = gadd(s, gel(x,i));
1146 1735516 : for ( ; i < l; i++) gel(y,i) = gadd(s, gmul2n(gel(x,i),-1));
1147 1235037 : return y;
1148 : }
1149 :
1150 : static GEN
1151 63622 : getfu(GEN nf, GEN *ptA, GEN *ptU, long prec)
1152 : {
1153 63622 : GEN U, y, matep, A, T = nf_get_pol(nf), M = nf_get_M(nf);
1154 63622 : long e, j, R1, RU, N = degpol(T);
1155 :
1156 63622 : R1 = nf_get_r1(nf); RU = (N+R1) >> 1;
1157 63622 : if (RU == 1) return cgetg(1,t_VEC);
1158 :
1159 48432 : A = *ptA;
1160 48432 : matep = cgetg(RU,t_MAT);
1161 117389 : for (j = 1; j < RU; j++)
1162 : {
1163 68956 : GEN Aj = gel(A,j), s = gdivgs(RgV_sum(real_i(Aj)), -N);
1164 68956 : gel(matep,j) = fixarch(Aj, s, R1);
1165 : }
1166 48433 : U = lll(real_i(matep));
1167 48433 : if (lg(U) < RU) return not_given(fupb_PRECI);
1168 48433 : if (ptU) { *ptU = U; *ptA = A = RgM_ZM_mul(A,U); }
1169 48433 : y = RgM_ZM_mul(matep,U);
1170 48433 : e = RgM_expbitprec(y);
1171 48432 : if (e >= 0) return not_given(e == LONG_MAX? fupb_LARGE: fupb_PRECI);
1172 48376 : if (prec <= 0) prec = gprecision(A);
1173 48376 : y = RgM_solve_realimag(M, gexp(y,prec));
1174 48376 : if (!y) return not_given(fupb_PRECI);
1175 48376 : y = grndtoi(y, &e); if (e >= 0) return not_given(fupb_PRECI);
1176 48065 : settyp(y, t_VEC);
1177 :
1178 48065 : if (!ptU) *ptA = A = RgM_ZM_mul(A, U);
1179 116302 : for (j = 1; j < RU; j++)
1180 : { /* y[i] are hopefully unit generators. Normalize: smallest T2 norm */
1181 68244 : GEN u = gel(y,j), v = zk_inv(nf, u);
1182 68245 : if (!v || !is_pm1(Q_denom(v)) || ZV_isscalar(u))
1183 8 : return not_given(fupb_PRECI);
1184 68236 : if (gcmp(RgC_fpnorml2(v,DEFAULTPREC), RgC_fpnorml2(u,DEFAULTPREC)) < 0)
1185 : {
1186 28535 : gel(A,j) = RgC_neg(gel(A,j));
1187 28535 : if (ptU) gel(U,j) = ZC_neg(gel(U,j));
1188 28535 : u = v;
1189 : }
1190 68237 : gel(y,j) = nf_to_scalar_or_alg(nf, u);
1191 : }
1192 48058 : return y;
1193 : }
1194 :
1195 : static void
1196 0 : err_units() { pari_err_PREC("makeunits [cannot get units, use bnfinit(,1)]"); }
1197 :
1198 : /* bound for log2 |sigma(u)|, sigma complex embedding, u fundamental unit
1199 : * attached to bnf_get_logfu */
1200 : static double
1201 94 : log2fubound(GEN bnf)
1202 : {
1203 94 : GEN LU = bnf_get_logfu(bnf);
1204 94 : long i, j, l = lg(LU), r1 = nf_get_r1(bnf_get_nf(bnf));
1205 94 : double e = 0.0;
1206 330 : for (j = 1; j < l; j++)
1207 : {
1208 236 : GEN u = gel(LU,j);
1209 624 : for (i = 1; i <= r1; i++)
1210 : {
1211 388 : GEN E = real_i(gel(u,i));
1212 388 : e = maxdd(e, gtodouble(E));
1213 : }
1214 842 : for ( ; i <= l; i++)
1215 : {
1216 606 : GEN E = real_i(gel(u,i));
1217 606 : e = maxdd(e, gtodouble(E) / 2);
1218 : }
1219 : }
1220 94 : return e / M_LN2;
1221 : }
1222 : /* bound for log2(|RgM_solve_realimag(M, y)|_oo / |y|_oo)*/
1223 : static double
1224 94 : log2Mbound(GEN nf)
1225 : {
1226 94 : GEN G = nf_get_G(nf), D = nf_get_disc(nf);
1227 94 : long r2 = nf_get_r2(nf), l = lg(G), i;
1228 94 : double e, d = dbllog2(D)/2 - r2 * M_LN2; /* log2 |det(split_realimag(M))| */
1229 94 : e = log2(nf_get_degree(nf));
1230 535 : for (i = 2; i < l; i++) e += dbllog2(gnorml2(gel(G,i))); /* Hadamard bound */
1231 94 : return e / 2 - d;
1232 : }
1233 :
1234 : static GEN
1235 94 : vec_chinese_units(GEN bnf)
1236 : {
1237 94 : GEN nf = bnf_get_nf(bnf), SUnits = bnf_get_sunits(bnf);
1238 94 : double bnd = ceil(log2Mbound(nf) + log2fubound(bnf));
1239 94 : GEN X, dX, Y, U, f = nf_get_index(nf);
1240 94 : long j, l, v = nf_get_varn(nf);
1241 94 : if (!SUnits) err_units(); /* no compact units */
1242 94 : Y = gel(SUnits,1);
1243 94 : U = gel(SUnits,2);
1244 94 : ZM_remove_unused(&U, &Y); l = lg(Y); X = cgetg(l, t_VEC);
1245 94 : if (is_pm1(f)) f = dX = NULL; else dX = cgetg(l, t_VEC);
1246 5153 : for (j = 1; j < l; j++)
1247 : {
1248 5059 : GEN t = nf_to_scalar_or_alg(nf, gel(Y,j));
1249 5059 : if (f)
1250 : {
1251 : GEN den;
1252 4202 : t = Q_remove_denom(t, &den);
1253 4202 : gel(dX,j) = den ? den: gen_1;
1254 : }
1255 5059 : gel(X,j) = typ(t) == t_INT? scalarpol_shallow(t,v): t;
1256 : }
1257 94 : if (dblexpo(bnd) >= BITS_IN_LONG)
1258 0 : pari_err_OVERFLOW("vec_chinese_units [units too large]");
1259 94 : return chinese_unit(nf, X, dX, U, (ulong)bnd);
1260 : }
1261 :
1262 : static GEN
1263 24894 : makeunits(GEN bnf)
1264 : {
1265 24894 : GEN nf = bnf_get_nf(bnf), fu = bnf_get_fu_nocheck(bnf);
1266 24894 : GEN tu = nf_to_scalar_or_basis(nf, bnf_get_tuU(bnf));
1267 24894 : fu = (typ(fu) == t_MAT)? vec_chinese_units(bnf): matalgtobasis(nf, fu);
1268 24894 : return vec_prepend(fu, tu);
1269 : }
1270 :
1271 : /*******************************************************************/
1272 : /* */
1273 : /* PRINCIPAL IDEAL ALGORITHM (DISCRETE LOG) */
1274 : /* */
1275 : /*******************************************************************/
1276 :
1277 : /* G: prime ideals, E: vector of nonnegative exponents.
1278 : * C = possible extra prime (^1) or NULL
1279 : * Return Norm (product) */
1280 : static GEN
1281 69 : get_norm_fact_primes(GEN G, GEN E, GEN C)
1282 : {
1283 69 : pari_sp av=avma;
1284 69 : GEN N = gen_1, P, p;
1285 69 : long i, c = lg(E);
1286 69 : for (i=1; i<c; i++)
1287 : {
1288 0 : GEN ex = gel(E,i);
1289 0 : long s = signe(ex);
1290 0 : if (!s) continue;
1291 :
1292 0 : P = gel(G,i); p = pr_get_p(P);
1293 0 : N = mulii(N, powii(p, mului(pr_get_f(P), ex)));
1294 : }
1295 69 : if (C) N = mulii(N, pr_norm(C));
1296 69 : return gerepileuptoint(av, N);
1297 : }
1298 :
1299 : /* gen: HNF ideals */
1300 : static GEN
1301 1160463 : get_norm_fact(GEN gen, GEN ex, GEN *pd)
1302 : {
1303 1160463 : long i, c = lg(ex);
1304 : GEN d,N,I,e,n,ne,de;
1305 1160463 : d = N = gen_1;
1306 1456300 : for (i=1; i<c; i++)
1307 295838 : if (signe(gel(ex,i)))
1308 : {
1309 175511 : I = gel(gen,i); e = gel(ex,i); n = ZM_det_triangular(I);
1310 175511 : ne = powii(n,e);
1311 175511 : de = equalii(n, gcoeff(I,1,1))? ne: powii(gcoeff(I,1,1), e);
1312 175511 : N = mulii(N, ne);
1313 175511 : d = mulii(d, de);
1314 : }
1315 1160462 : *pd = d; return N;
1316 : }
1317 :
1318 : static GEN
1319 1321301 : get_pr_lists(GEN FB, long N, int list_pr)
1320 : {
1321 : GEN pr, L;
1322 1321301 : long i, l = lg(FB), p, pmax;
1323 :
1324 1321301 : pmax = 0;
1325 9141599 : for (i=1; i<l; i++)
1326 : {
1327 7820298 : pr = gel(FB,i); p = pr_get_smallp(pr);
1328 7820298 : if (p > pmax) pmax = p;
1329 : }
1330 1321301 : L = const_vec(pmax, NULL);
1331 1321302 : if (list_pr)
1332 : {
1333 0 : for (i=1; i<l; i++)
1334 : {
1335 0 : pr = gel(FB,i); p = pr_get_smallp(pr);
1336 0 : if (!L[p]) gel(L,p) = vectrunc_init(N+1);
1337 0 : vectrunc_append(gel(L,p), pr);
1338 : }
1339 0 : for (p=1; p<=pmax; p++)
1340 0 : if (L[p]) gen_sort_inplace(gel(L,p), (void*)&cmp_prime_over_p,
1341 : &cmp_nodata, NULL);
1342 : }
1343 : else
1344 : {
1345 9141601 : for (i=1; i<l; i++)
1346 : {
1347 7820299 : pr = gel(FB,i); p = pr_get_smallp(pr);
1348 7820299 : if (!L[p]) gel(L,p) = vecsmalltrunc_init(N+1);
1349 7820299 : vecsmalltrunc_append(gel(L,p), i);
1350 : }
1351 : }
1352 1321302 : return L;
1353 : }
1354 :
1355 : /* recover FB, LV, iLP, KCZ from Vbase */
1356 : static GEN
1357 1321301 : recover_partFB(FB_t *F, GEN Vbase, long N)
1358 : {
1359 1321301 : GEN FB, LV, iLP, L = get_pr_lists(Vbase, N, 0);
1360 1321302 : long l = lg(L), p, ip, i;
1361 :
1362 1321302 : i = ip = 0;
1363 1321302 : FB = cgetg(l, t_VECSMALL);
1364 1321302 : iLP= cgetg(l, t_VECSMALL);
1365 1321300 : LV = cgetg(l, t_VEC);
1366 19811818 : for (p = 2; p < l; p++)
1367 : {
1368 18490515 : if (!L[p]) continue;
1369 4282748 : FB[++i] = p;
1370 4282748 : gel(LV,p) = vecpermute(Vbase, gel(L,p));
1371 4282747 : iLP[p]= ip; ip += lg(gel(L,p))-1;
1372 : }
1373 1321303 : F->KCZ = i;
1374 1321303 : F->KC = ip;
1375 1321303 : F->FB = FB; setlg(FB, i+1);
1376 1321303 : F->prodZ = zv_prod_Z(F->FB);
1377 1321301 : F->LV = LV;
1378 1321301 : F->iLP= iLP; return L;
1379 : }
1380 :
1381 : /* add v^e to factorization */
1382 : static void
1383 29439 : add_to_fact(long v, long e, FACT *fact)
1384 : {
1385 29439 : long i, l = fact[0].pr;
1386 56257 : for (i=1; i<=l && fact[i].pr < v; i++)/*empty*/;
1387 29439 : if (i <= l && fact[i].pr == v) fact[i].ex += e; else store(v, e, fact);
1388 29439 : }
1389 : static void
1390 0 : inv_fact(FACT *fact)
1391 : {
1392 0 : long i, l = fact[0].pr;
1393 0 : for (i=1; i<=l; i++) fact[i].ex = -fact[i].ex;
1394 0 : }
1395 :
1396 : /* L (small) list of primes above the same p including pr. Return pr index */
1397 : static int
1398 3307 : pr_index(GEN L, GEN pr)
1399 : {
1400 3307 : long j, l = lg(L);
1401 3307 : GEN al = pr_get_gen(pr);
1402 3307 : for (j=1; j<l; j++)
1403 3307 : if (ZV_equal(al, pr_get_gen(gel(L,j)))) return j;
1404 0 : pari_err_BUG("codeprime");
1405 : return 0; /* LCOV_EXCL_LINE */
1406 : }
1407 :
1408 : static long
1409 3307 : Vbase_to_FB(FB_t *F, GEN pr)
1410 : {
1411 3307 : long p = pr_get_smallp(pr);
1412 3307 : return F->iLP[p] + pr_index(gel(F->LV,p), pr);
1413 : }
1414 :
1415 : /* x, y 2 extended ideals whose first component is an integral HNF and second
1416 : * a famat */
1417 : static GEN
1418 3561 : idealHNF_mulred(GEN nf, GEN x, GEN y)
1419 : {
1420 3561 : GEN A = idealHNF_mul(nf, gel(x,1), gel(y,1));
1421 3561 : GEN F = famat_mul_shallow(gel(x,2), gel(y,2));
1422 3561 : return idealred(nf, mkvec2(A, F));
1423 : }
1424 : /* idealred(x * pr^n), n > 0 is small, x extended ideal. Reduction in order to
1425 : * avoid prec pb: don't let id become too large as lgsub increases */
1426 : static GEN
1427 4544 : idealmulpowprime2(GEN nf, GEN x, GEN pr, ulong n)
1428 : {
1429 4544 : GEN A = idealmulpowprime(nf, gel(x,1), pr, utoipos(n));
1430 4544 : return mkvec2(A, gel(x,2));
1431 : }
1432 : static GEN
1433 65364 : init_famat(GEN x) { return mkvec2(x, trivial_fact()); }
1434 : /* optimized idealfactorback + reduction; z = init_famat() */
1435 : static GEN
1436 28728 : genback(GEN z, GEN nf, GEN P, GEN E)
1437 : {
1438 28728 : long i, l = lg(E);
1439 28728 : GEN I = NULL;
1440 76444 : for (i = 1; i < l; i++)
1441 47716 : if (signe(gel(E,i)))
1442 : {
1443 : GEN J;
1444 32289 : gel(z,1) = gel(P,i);
1445 32289 : J = idealpowred(nf, z, gel(E,i));
1446 32289 : I = I? idealHNF_mulred(nf, I, J): J;
1447 : }
1448 28728 : return I; /* != NULL since a generator */
1449 : }
1450 :
1451 : /* return famat y (principal ideal) such that y / x is smooth [wrt Vbase] */
1452 : static GEN
1453 1337645 : SPLIT(FB_t *F, GEN nf, GEN x, GEN Vbase, FACT *fact)
1454 : {
1455 1337645 : GEN vecG, ex, Ly, y, x0, Nx = ZM_det_triangular(x);
1456 : long nbtest_lim, nbtest, i, j, k, ru, lgsub;
1457 : pari_sp av;
1458 :
1459 : /* try without reduction if x is small */
1460 2675108 : if (gexpo(gcoeff(x,1,1)) < 100 &&
1461 1487271 : can_factor(F, nf, x, NULL, Nx, fact)) return NULL;
1462 :
1463 1187838 : av = avma;
1464 1187838 : Ly = idealpseudominvec(x, nf_get_roundG(nf));
1465 1231131 : for(k=1; k<lg(Ly); k++)
1466 : {
1467 1222334 : y = gel(Ly,k);
1468 1222334 : if (factorgen(F, nf, x, Nx, y, fact)) return y;
1469 : }
1470 8797 : set_avma(av);
1471 :
1472 : /* reduce in various directions */
1473 8797 : ru = lg(nf_get_roots(nf));
1474 8797 : vecG = cgetg(ru, t_VEC);
1475 14312 : for (j=1; j<ru; j++)
1476 : {
1477 12571 : gel(vecG,j) = nf_get_Gtwist1(nf, j);
1478 12571 : av = avma;
1479 12571 : Ly = idealpseudominvec(x, gel(vecG,j));
1480 41421 : for(k=1; k<lg(Ly); k++)
1481 : {
1482 35906 : y = gel(Ly,k);
1483 35906 : if (factorgen(F, nf, x, Nx, y, fact)) return y;
1484 : }
1485 5515 : set_avma(av);
1486 : }
1487 :
1488 : /* tough case, multiply by random products */
1489 1741 : lgsub = 3;
1490 1741 : ex = cgetg(lgsub, t_VECSMALL);
1491 1741 : x0 = init_famat(x);
1492 1741 : nbtest = 1; nbtest_lim = 4;
1493 : for(;;)
1494 622 : {
1495 2363 : GEN Ired, I, NI, id = x0;
1496 2363 : av = avma;
1497 2363 : if (DEBUGLEVEL>2) err_printf("# ideals tried = %ld\n",nbtest);
1498 7208 : for (i=1; i<lgsub; i++)
1499 : {
1500 4845 : ex[i] = random_bits(RANDOM_BITS);
1501 4845 : if (ex[i]) id = idealmulpowprime2(nf, id, gel(Vbase,i), ex[i]);
1502 : }
1503 2363 : if (id == x0) continue;
1504 : /* I^(-1) * \prod Vbase[i]^ex[i] = (id[2]) / x */
1505 :
1506 2363 : I = gel(id,1); NI = ZM_det_triangular(I);
1507 2363 : if (can_factor(F, nf, I, NULL, NI, fact))
1508 : {
1509 0 : inv_fact(fact); /* I^(-1) */
1510 0 : for (i=1; i<lgsub; i++)
1511 0 : if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
1512 0 : return gel(id,2);
1513 : }
1514 2363 : Ired = ru == 2? I: ZM_lll(I, 0.99, LLL_INPLACE);
1515 3987 : for (j=1; j<ru; j++)
1516 : {
1517 3365 : pari_sp av2 = avma;
1518 3365 : Ly = idealpseudominvec(Ired, gel(vecG,j));
1519 11463 : for (k=1; k < lg(Ly); k++)
1520 : {
1521 9839 : y = gel(Ly,k);
1522 9839 : if (factorgen(F, nf, I, NI, y, fact))
1523 : {
1524 5251 : for (i=1; i<lgsub; i++)
1525 3510 : if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
1526 1741 : return famat_mul_shallow(gel(id,2), y);
1527 : }
1528 : }
1529 1624 : set_avma(av2);
1530 : }
1531 622 : set_avma(av);
1532 622 : if (++nbtest > nbtest_lim)
1533 : {
1534 28 : nbtest = 0;
1535 28 : if (++lgsub < minss(8, lg(Vbase)-1))
1536 : {
1537 28 : nbtest_lim <<= 1;
1538 28 : ex = cgetg(lgsub, t_VECSMALL);
1539 : }
1540 0 : else nbtest_lim = LONG_MAX; /* don't increase further */
1541 28 : if (DEBUGLEVEL>2) err_printf("SPLIT: increasing factor base [%ld]\n",lgsub);
1542 : }
1543 : }
1544 : }
1545 :
1546 : INLINE GEN
1547 1326214 : bnf_get_W(GEN bnf) { return gel(bnf,1); }
1548 : INLINE GEN
1549 2642493 : bnf_get_B(GEN bnf) { return gel(bnf,2); }
1550 : INLINE GEN
1551 2676976 : bnf_get_C(GEN bnf) { return gel(bnf,4); }
1552 : INLINE GEN
1553 1321321 : bnf_get_vbase(GEN bnf) { return gel(bnf,5); }
1554 : INLINE GEN
1555 1321238 : bnf_get_Ur(GEN bnf) { return gmael(bnf,9,1); }
1556 : INLINE GEN
1557 271618 : bnf_get_ga(GEN bnf) { return gmael(bnf,9,2); }
1558 : INLINE GEN
1559 276574 : bnf_get_GD(GEN bnf) { return gmael(bnf,9,3); }
1560 :
1561 : /* Return y (as an elt of K or a t_MAT representing an elt in Z[K])
1562 : * such that x / (y) is smooth and store the exponents of its factorization
1563 : * on g_W and g_B in Wex / Bex; return NULL for y = 1 */
1564 : static GEN
1565 1321238 : split_ideal(GEN bnf, GEN x, GEN *pWex, GEN *pBex)
1566 : {
1567 1321238 : GEN L, y, Vbase = bnf_get_vbase(bnf);
1568 1321238 : GEN Wex, W = bnf_get_W(bnf);
1569 1321238 : GEN Bex, B = bnf_get_B(bnf);
1570 : long p, j, i, l, nW, nB;
1571 : FACT *fact;
1572 : FB_t F;
1573 :
1574 1321238 : L = recover_partFB(&F, Vbase, lg(x)-1);
1575 1321238 : fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
1576 1321237 : y = SPLIT(&F, bnf_get_nf(bnf), x, Vbase, fact);
1577 1321238 : nW = lg(W)-1; *pWex = Wex = zero_zv(nW);
1578 1321238 : nB = lg(B)-1; *pBex = Bex = zero_zv(nB); l = lg(F.FB);
1579 1321238 : p = j = 0; /* -Wall */
1580 1968390 : for (i = 1; i <= fact[0].pr; i++)
1581 : { /* decode index C = ip+j --> (p,j) */
1582 647152 : long a, b, t, C = fact[i].pr;
1583 1824999 : for (t = 1; t < l; t++)
1584 : {
1585 1751795 : long q = F.FB[t], k = C - F.iLP[q];
1586 1751795 : if (k <= 0) break;
1587 1177847 : p = q;
1588 1177847 : j = k;
1589 : }
1590 647152 : a = gel(L, p)[j];
1591 647152 : b = a - nW;
1592 647152 : if (b <= 0) Wex[a] = y? -fact[i].ex: fact[i].ex;
1593 493784 : else Bex[b] = y? -fact[i].ex: fact[i].ex;
1594 : }
1595 1321238 : return y;
1596 : }
1597 :
1598 : GEN
1599 1039008 : init_red_mod_units(GEN bnf, long prec)
1600 : {
1601 1039008 : GEN s = gen_0, p1,s1,mat, logfu = bnf_get_logfu(bnf);
1602 1039008 : long i,j, RU = lg(logfu);
1603 :
1604 1039008 : if (RU == 1) return NULL;
1605 1039008 : mat = cgetg(RU,t_MAT);
1606 2356119 : for (j=1; j<RU; j++)
1607 : {
1608 1317111 : p1 = cgetg(RU+1,t_COL); gel(mat,j) = p1;
1609 1317112 : s1 = gen_0;
1610 3261277 : for (i=1; i<RU; i++)
1611 : {
1612 1944166 : gel(p1,i) = real_i(gcoeff(logfu,i,j));
1613 1944166 : s1 = mpadd(s1, mpsqr(gel(p1,i)));
1614 : }
1615 1317111 : gel(p1,RU) = gen_0; if (mpcmp(s1,s) > 0) s = s1;
1616 : }
1617 1039008 : s = gsqrt(gmul2n(s,RU),prec);
1618 1039008 : if (expo(s) < 27) s = utoipos(1UL << 27);
1619 1039008 : return mkvec2(mat, s);
1620 : }
1621 :
1622 : /* z computed above. Return unit exponents that would reduce col (arch) */
1623 : GEN
1624 1039008 : red_mod_units(GEN col, GEN z)
1625 : {
1626 : long i,RU;
1627 : GEN x,mat,N2;
1628 :
1629 1039008 : if (!z) return NULL;
1630 1039008 : mat= gel(z,1);
1631 1039008 : N2 = gel(z,2);
1632 1039008 : RU = lg(mat); x = cgetg(RU+1,t_COL);
1633 2356119 : for (i=1; i<RU; i++) gel(x,i) = real_i(gel(col,i));
1634 1039008 : gel(x,RU) = N2;
1635 1039008 : x = lll(shallowconcat(mat,x));
1636 1039009 : if (typ(x) != t_MAT || lg(x) <= RU) return NULL;
1637 1039009 : x = gel(x,RU);
1638 1039009 : if (signe(gel(x,RU)) < 0) x = gneg_i(x);
1639 1039009 : if (!gequal1(gel(x,RU))) pari_err_BUG("red_mod_units");
1640 1039009 : setlg(x,RU); return x;
1641 : }
1642 :
1643 : static GEN
1644 2126035 : add(GEN a, GEN t) { return a = a? RgC_add(a,t): t; }
1645 :
1646 : /* [x] archimedian components, A column vector. return [x] A */
1647 : static GEN
1648 1984592 : act_arch(GEN A, GEN x)
1649 : {
1650 : GEN a;
1651 1984592 : long i,l = lg(A), tA = typ(A);
1652 1984592 : if (tA == t_MAT)
1653 : { /* assume lg(x) >= l */
1654 191159 : a = cgetg(l, t_MAT);
1655 280891 : for (i=1; i<l; i++) gel(a,i) = act_arch(gel(A,i), x);
1656 191160 : return a;
1657 : }
1658 1793433 : if (l==1) return cgetg(1, t_COL);
1659 1793433 : a = NULL;
1660 1793433 : if (tA == t_VECSMALL)
1661 : {
1662 6800711 : for (i=1; i<l; i++)
1663 : {
1664 5640247 : long c = A[i];
1665 5640247 : if (c) a = add(a, gmulsg(c, gel(x,i)));
1666 : }
1667 : }
1668 : else
1669 : { /* A a t_COL of t_INT. Assume lg(A)==lg(x) */
1670 1381553 : for (i=1; i<l; i++)
1671 : {
1672 748584 : GEN c = gel(A,i);
1673 748584 : if (signe(c)) a = add(a, gmul(c, gel(x,i)));
1674 : }
1675 : }
1676 1793433 : return a? a: zerocol(lgcols(x)-1);
1677 : }
1678 : /* act_arch(matdiagonal(v), x) */
1679 : static GEN
1680 63718 : diagact_arch(GEN v, GEN x)
1681 : {
1682 63718 : long i, l = lg(v);
1683 63718 : GEN a = cgetg(l, t_MAT);
1684 92516 : for (i = 1; i < l; i++) gel(a,i) = gmul(gel(x,i), gel(v,i));
1685 63720 : return a;
1686 : }
1687 :
1688 : static long
1689 1339406 : prec_arch(GEN bnf)
1690 : {
1691 1339406 : GEN a = bnf_get_C(bnf);
1692 1339406 : long i, l = lg(a), prec;
1693 :
1694 1339406 : for (i=1; i<l; i++)
1695 1339322 : if ( (prec = gprecision(gel(a,i))) ) return prec;
1696 84 : return DEFAULTPREC;
1697 : }
1698 :
1699 : static long
1700 3778 : needed_bitprec(GEN x)
1701 : {
1702 3778 : long i, e = 0, l = lg(x);
1703 22246 : for (i = 1; i < l; i++)
1704 : {
1705 18468 : GEN c = gel(x,i);
1706 18468 : long f = gexpo(c) - prec2nbits(gprecision(c));
1707 18468 : if (f > e) e = f;
1708 : }
1709 3778 : return e;
1710 : }
1711 :
1712 : /* col = archimedian components of x, Nx its norm, dx a multiple of its
1713 : * denominator. Return x or NULL (fail) */
1714 : GEN
1715 1166077 : isprincipalarch(GEN bnf, GEN col, GEN kNx, GEN e, GEN dx, long *pe)
1716 : {
1717 : GEN nf, x, y, logfu, s, M;
1718 1166077 : long N, prec = gprecision(col);
1719 1166078 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf); M = nf_get_M(nf);
1720 1166076 : if (!prec) prec = prec_arch(bnf);
1721 1166076 : *pe = 128;
1722 1166076 : logfu = bnf_get_logfu(bnf);
1723 1166076 : N = nf_get_degree(nf);
1724 1166076 : if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
1725 1166077 : if (lg(col) > 2)
1726 : { /* reduce mod units */
1727 1039008 : GEN u, z = init_red_mod_units(bnf,prec);
1728 1039008 : if (!(u = red_mod_units(col,z))) return NULL;
1729 1039009 : col = RgC_add(col, RgM_RgC_mul(logfu, u));
1730 1039008 : if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
1731 : }
1732 1166077 : s = divru(mulir(e, glog(kNx,prec)), N);
1733 1166072 : col = fixarch(col, s, nf_get_r1(nf));
1734 1166079 : if (RgC_expbitprec(col) >= 0) return NULL;
1735 1165624 : col = gexp(col, prec);
1736 : /* d.alpha such that x = alpha \prod gj^ej */
1737 1165621 : x = RgM_solve_realimag(M,col); if (!x) return NULL;
1738 1165622 : x = RgC_Rg_mul(x, dx);
1739 1165623 : y = grndtoi(x, pe);
1740 1165622 : if (*pe > -5) { *pe = needed_bitprec(x); return NULL; }
1741 1161844 : return RgC_Rg_div(y, dx);
1742 : }
1743 :
1744 : /* y = C \prod g[i]^e[i] ? */
1745 : static int
1746 1157822 : fact_ok(GEN nf, GEN y, GEN C, GEN g, GEN e)
1747 : {
1748 1157822 : pari_sp av = avma;
1749 1157822 : long i, c = lg(e);
1750 1157822 : GEN z = C? C: gen_1;
1751 1434824 : for (i=1; i<c; i++)
1752 277002 : if (signe(gel(e,i))) z = idealmul(nf, z, idealpow(nf, gel(g,i), gel(e,i)));
1753 1157822 : if (typ(z) != t_MAT) z = idealhnf_shallow(nf,z);
1754 1157821 : if (typ(y) != t_MAT) y = idealhnf_shallow(nf,y);
1755 1157821 : return gc_bool(av, ZM_equal(y,z));
1756 : }
1757 : static GEN
1758 1321237 : ZV_divrem(GEN A, GEN B, GEN *pR)
1759 : {
1760 1321237 : long i, l = lg(A);
1761 1321237 : GEN Q = cgetg(l, t_COL), R = cgetg(l, t_COL);
1762 1826583 : for (i = 1; i < l; i++) gel(Q,i) = truedvmdii(gel(A,i), gel(B,i), &gel(R,i));
1763 1321235 : *pR = R; return Q;
1764 : }
1765 :
1766 : static GEN
1767 1321238 : Ur_ZC_mul(GEN bnf, GEN v)
1768 : {
1769 1321238 : GEN w, U = bnf_get_Ur(bnf);
1770 1321238 : long i, l = lg(bnf_get_cyc(bnf)); /* may be < lgcols(U) */
1771 :
1772 1321238 : w = cgetg(l, t_COL);
1773 1826584 : for (i = 1; i < l; i++) gel(w,i) = ZMrow_ZC_mul(U, v, i);
1774 1321236 : return w;
1775 : }
1776 :
1777 : static GEN
1778 7074 : ZV_mul(GEN x, GEN y)
1779 : {
1780 7074 : long i, l = lg(x);
1781 7074 : GEN z = cgetg(l, t_COL);
1782 30826 : for (i = 1; i < l; i++) gel(z,i) = mulii(gel(x,i), gel(y,i));
1783 7074 : return z;
1784 : }
1785 : static int
1786 1157215 : dump_gen(GEN SUnits, GEN x, long flag)
1787 : {
1788 : GEN d;
1789 : long e;
1790 1157215 : if (!(flag & nf_GENMAT) || !SUnits) return 0;
1791 266325 : e = gexpo(gel(SUnits,2)); if (e > 64) return 0; /* U large */
1792 266228 : x = Q_remove_denom(x, &d);
1793 266228 : return (d && expi(d) > 32) || gexpo(x) > 32;
1794 : }
1795 :
1796 : /* assume x in HNF; cf class_group_gen for notations. Return NULL iff
1797 : * flag & nf_FORCE and computation of principal ideal generator fails */
1798 : static GEN
1799 1337551 : isprincipalall(GEN bnf, GEN x, long *pprec, long flag)
1800 : {
1801 : GEN xar, Wex, Bex, gen, xc, col, A, Q, R, UA, SUnits;
1802 1337551 : GEN C = bnf_get_C(bnf), nf = bnf_get_nf(bnf), cyc = bnf_get_cyc(bnf);
1803 : long nB, nW, e;
1804 :
1805 1337551 : if (lg(cyc) == 1 && !(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL)))
1806 4725 : return cgetg(1,t_COL);
1807 1332826 : if (lg(x) == 2)
1808 : { /* nf = Q */
1809 84 : col = gel(x,1);
1810 84 : if (flag & nf_GENMAT) col = to_famat_shallow(col, gen_1);
1811 84 : return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(cgetg(1,t_COL), col);
1812 : }
1813 :
1814 1332742 : x = Q_primitive_part(x, &xc);
1815 1332738 : if (equali1(gcoeff(x,1,1))) /* trivial ideal */
1816 : {
1817 11501 : R = zerocol(lg(cyc)-1);
1818 11501 : if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
1819 11452 : if (flag & nf_GEN_IF_PRINCIPAL)
1820 6454 : return scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
1821 4998 : if (flag & nf_GENMAT)
1822 2163 : col = xc? to_famat_shallow(xc, gen_1): trivial_fact();
1823 : else
1824 2835 : col = scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
1825 4998 : return mkvec2(R, col);
1826 : }
1827 1321238 : xar = split_ideal(bnf, x, &Wex, &Bex);
1828 : /* x = g_W Wex + g_B Bex + [xar] = g_W (Wex - B*Bex) + [xar] + [C_B]Bex */
1829 1321238 : A = zc_to_ZC(Wex); nB = lg(Bex)-1;
1830 1321238 : if (nB) A = ZC_sub(A, ZM_zc_mul(bnf_get_B(bnf), Bex));
1831 1321237 : UA = Ur_ZC_mul(bnf, A);
1832 1321236 : Q = ZV_divrem(UA, cyc, &R);
1833 : /* g_W (Wex - B*Bex) = G Ur A - [ga]A = G R + [GD]Q - [ga]A
1834 : * Finally: x = G R + [xar] + [C_B]Bex + [GD]Q - [ga]A */
1835 1321235 : if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
1836 1161058 : if ((flag & nf_GEN_IF_PRINCIPAL) && !ZV_equal0(R)) return gen_0;
1837 :
1838 1161051 : nW = lg(Wex)-1;
1839 1161051 : gen = bnf_get_gen(bnf);
1840 1161052 : col = NULL;
1841 1161052 : SUnits = bnf_get_sunits(bnf);
1842 1161052 : if (lg(R) == 1
1843 272208 : || abscmpiu(gel(R,vecindexmax(R)), 4 * prec2nbits(*pprec)) < 0)
1844 : { /* q = N (x / prod gj^ej) = N(alpha), denom(alpha) | d */
1845 1160462 : GEN d, q = gdiv(ZM_det_triangular(x), get_norm_fact(gen, R, &d));
1846 1160463 : col = xar? nf_cxlog(nf, xar, *pprec): NULL;
1847 1160467 : if (nB) col = add(col, act_arch(Bex, nW? vecslice(C,nW+1,lg(C)-1): C));
1848 1160466 : if (nW) col = add(col, RgC_sub(act_arch(Q, bnf_get_GD(bnf)),
1849 : act_arch(A, bnf_get_ga(bnf))));
1850 1160465 : col = isprincipalarch(bnf, col, q, gen_1, d, &e);
1851 1160466 : if (col && (dump_gen(SUnits, col, flag)
1852 1157214 : || !fact_ok(nf,x, col,gen,R))) col = NULL;
1853 : }
1854 1161053 : if (!col && (flag & nf_GENMAT))
1855 : {
1856 7780 : if (SUnits)
1857 : {
1858 7298 : GEN X = gel(SUnits,1), U = gel(SUnits,2), C = gel(SUnits,3);
1859 7298 : GEN v = gel(bnf,9), Ge = gel(v,4), M1 = gel(v,5), M2 = gel(v,6);
1860 7298 : GEN z = NULL, F = NULL;
1861 7298 : if (nB)
1862 : {
1863 7298 : GEN C2 = nW? vecslice(C, nW+1, lg(C)-1): C;
1864 7298 : z = ZM_zc_mul(C2, Bex);
1865 : }
1866 7298 : if (nW)
1867 : { /* [GD]Q - [ga]A = ([X]M1 - [Ge]D) Q - ([X]M2 - [Ge]Ur) A */
1868 7074 : GEN C1 = vecslice(C, 1, nW);
1869 7074 : GEN v = ZC_sub(ZM_ZC_mul(M1,Q), ZM_ZC_mul(M2,A));
1870 7074 : z = add(z, ZM_ZC_mul(C1, v));
1871 7074 : F = famat_reduce(famatV_factorback(Ge, ZC_sub(UA, ZV_mul(cyc,Q))));
1872 7074 : if (lgcols(F) == 1) F = NULL;
1873 : }
1874 : /* reduce modulo units and Q^* */
1875 7298 : if (lg(U) != 1) z = ZC_sub(z, ZM_ZC_mul(U, RgM_Babai(U,z)));
1876 7298 : col = mkmat2(X, z);
1877 7298 : if (F) col = famat_mul_shallow(col, F);
1878 7298 : col = famat_remove_trivial(col);
1879 7298 : if (xar) col = famat_mul_shallow(col, xar);
1880 : }
1881 482 : else if (!ZV_equal0(R))
1882 : { /* in case isprincipalfact calls bnfinit() due to prec trouble...*/
1883 476 : GEN y = isprincipalfact(bnf, x, gen, ZC_neg(R), flag);
1884 476 : if (typ(y) != t_VEC) return y;
1885 476 : col = gel(y,2);
1886 : }
1887 : }
1888 1161053 : if (col)
1889 : { /* add back missing content */
1890 1161495 : if (xc) col = (typ(col)==t_MAT)? famat_mul_shallow(col,xc)
1891 532 : : RgC_Rg_mul(col,xc);
1892 1160963 : if (typ(col) != t_MAT && lg(col) != 1 && (flag & nf_GENMAT))
1893 1139490 : col = to_famat_shallow(col, gen_1);
1894 : }
1895 : else
1896 : {
1897 90 : if (e < 0) e = 0;
1898 90 : *pprec += nbits2extraprec(e + 128);
1899 90 : if (flag & nf_FORCE)
1900 : {
1901 76 : if (DEBUGLEVEL)
1902 0 : pari_warn(warner,"precision too low for generators, e = %ld",e);
1903 76 : return NULL;
1904 : }
1905 14 : pari_warn(warner,"precision too low for generators, not given");
1906 14 : col = cgetg(1, t_COL);
1907 : }
1908 1160978 : return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(R, col);
1909 : }
1910 :
1911 : static GEN
1912 460145 : triv_gen(GEN bnf, GEN x, long flag)
1913 : {
1914 460145 : pari_sp av = avma;
1915 460145 : GEN nf = bnf_get_nf(bnf);
1916 : long c;
1917 460145 : if (flag & nf_GEN_IF_PRINCIPAL)
1918 : {
1919 7 : if (!(flag & nf_GENMAT)) return algtobasis(nf,x);
1920 7 : x = nf_to_scalar_or_basis(nf,x);
1921 7 : if (typ(x) == t_INT && is_pm1(x)) return trivial_fact();
1922 0 : return gerepilecopy(av, to_famat_shallow(x, gen_1));
1923 : }
1924 460138 : c = lg(bnf_get_cyc(bnf)) - 1;
1925 460138 : if (flag & nf_GENMAT)
1926 450534 : retmkvec2(zerocol(c), to_famat_shallow(algtobasis(nf,x), gen_1));
1927 9604 : if (flag & nf_GEN)
1928 28 : retmkvec2(zerocol(c), algtobasis(nf,x));
1929 9576 : return zerocol(c);
1930 : }
1931 :
1932 : GEN
1933 1765753 : bnfisprincipal0(GEN bnf,GEN x,long flag)
1934 : {
1935 1765753 : pari_sp av = avma;
1936 : GEN c, nf;
1937 : long pr;
1938 :
1939 1765753 : bnf = checkbnf(bnf);
1940 1765753 : nf = bnf_get_nf(bnf);
1941 1765751 : switch( idealtyp(&x, NULL) )
1942 : {
1943 55279 : case id_PRINCIPAL:
1944 55279 : if (gequal0(x)) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
1945 55279 : return triv_gen(bnf, x, flag);
1946 1686819 : case id_PRIME:
1947 1686819 : if (pr_is_inert(x)) return triv_gen(bnf, pr_get_p(x), flag);
1948 1281959 : x = pr_hnf(nf, x);
1949 1281963 : break;
1950 23653 : case id_MAT:
1951 23653 : if (lg(x)==1) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
1952 23653 : if (nf_get_degree(nf) != lg(x)-1)
1953 0 : pari_err_TYPE("idealtyp [dimension != degree]", x);
1954 : }
1955 1305615 : pr = prec_arch(bnf); /* precision of unit matrix */
1956 1305616 : c = getrand();
1957 : for (;;)
1958 6 : {
1959 1305622 : pari_sp av1 = avma;
1960 1305622 : GEN y = isprincipalall(bnf,x,&pr,flag);
1961 1305618 : if (y) return gerepilecopy(av, y);
1962 :
1963 6 : if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",pr);
1964 6 : set_avma(av1); bnf = bnfnewprec_shallow(bnf,pr); setrand(c);
1965 : }
1966 : }
1967 : GEN
1968 174478 : isprincipal(GEN bnf,GEN x) { return bnfisprincipal0(bnf,x,0); }
1969 :
1970 : /* FIXME: OBSOLETE */
1971 : GEN
1972 0 : isprincipalgen(GEN bnf,GEN x)
1973 0 : { return bnfisprincipal0(bnf,x,nf_GEN); }
1974 : GEN
1975 0 : isprincipalforce(GEN bnf,GEN x)
1976 0 : { return bnfisprincipal0(bnf,x,nf_FORCE); }
1977 : GEN
1978 0 : isprincipalgenforce(GEN bnf,GEN x)
1979 0 : { return bnfisprincipal0(bnf,x,nf_GEN | nf_FORCE); }
1980 :
1981 : /* lg(u) > 1 */
1982 : static int
1983 91 : RgV_is1(GEN u) { return isint1(gel(u,1)) && RgV_isscalar(u); }
1984 : static GEN
1985 31859 : add_principal_part(GEN nf, GEN u, GEN v, long flag)
1986 : {
1987 31859 : if (flag & nf_GENMAT)
1988 14247 : return (typ(u) == t_COL && RgV_is1(u))? v: famat_mul_shallow(v,u);
1989 : else
1990 17612 : return nfmul(nf, v, u);
1991 : }
1992 :
1993 : #if 0
1994 : /* compute C prod P[i]^e[i], e[i] >=0 for all i. C may be NULL (omitted)
1995 : * e destroyed ! */
1996 : static GEN
1997 : expand(GEN nf, GEN C, GEN P, GEN e)
1998 : {
1999 : long i, l = lg(e), done = 1;
2000 : GEN id = C;
2001 : for (i=1; i<l; i++)
2002 : {
2003 : GEN ei = gel(e,i);
2004 : if (signe(ei))
2005 : {
2006 : if (mod2(ei)) id = id? idealmul(nf, id, gel(P,i)): gel(P,i);
2007 : ei = shifti(ei,-1);
2008 : if (signe(ei)) done = 0;
2009 : gel(e,i) = ei;
2010 : }
2011 : }
2012 : if (id != C) id = idealred(nf, id);
2013 : if (done) return id;
2014 : return idealmulred(nf, id, idealsqr(nf, expand(nf,id,P,e)));
2015 : }
2016 : /* C is an extended ideal, possibly with C[1] = NULL */
2017 : static GEN
2018 : expandext(GEN nf, GEN C, GEN P, GEN e)
2019 : {
2020 : long i, l = lg(e), done = 1;
2021 : GEN A = gel(C,1);
2022 : for (i=1; i<l; i++)
2023 : {
2024 : GEN ei = gel(e,i);
2025 : if (signe(ei))
2026 : {
2027 : if (mod2(ei)) A = A? idealmul(nf, A, gel(P,i)): gel(P,i);
2028 : ei = shifti(ei,-1);
2029 : if (signe(ei)) done = 0;
2030 : gel(e,i) = ei;
2031 : }
2032 : }
2033 : if (A == gel(C,1))
2034 : A = C;
2035 : else
2036 : A = idealred(nf, mkvec2(A, gel(C,2)));
2037 : if (done) return A;
2038 : return idealmulred(nf, A, idealsqr(nf, expand(nf,A,P,e)));
2039 : }
2040 : #endif
2041 :
2042 : static GEN
2043 0 : expand(GEN nf, GEN C, GEN P, GEN e)
2044 : {
2045 0 : long i, l = lg(e);
2046 0 : GEN B, A = C;
2047 0 : for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
2048 0 : if (signe(gel(e,i)))
2049 : {
2050 0 : B = idealpowred(nf, gel(P,i), gel(e,i));
2051 0 : A = A? idealmulred(nf,A,B): B;
2052 : }
2053 0 : return A;
2054 : }
2055 : static GEN
2056 31880 : expandext(GEN nf, GEN C, GEN P, GEN e)
2057 : {
2058 31880 : long i, l = lg(e);
2059 31880 : GEN B, A = gel(C,1), C1 = A;
2060 94011 : for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
2061 62131 : if (signe(gel(e,i)))
2062 : {
2063 34426 : gel(C,1) = gel(P,i);
2064 34426 : B = idealpowred(nf, C, gel(e,i));
2065 34426 : A = A? idealmulred(nf,A,B): B;
2066 : }
2067 31880 : return A == C1? C: A;
2068 : }
2069 :
2070 : /* isprincipal for C * \prod P[i]^e[i] (C omitted if NULL) */
2071 : GEN
2072 31880 : isprincipalfact(GEN bnf, GEN C, GEN P, GEN e, long flag)
2073 : {
2074 31880 : const long gen = flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL);
2075 : long prec;
2076 31880 : pari_sp av = avma;
2077 31880 : GEN C0, Cext, c, id, nf = bnf_get_nf(bnf);
2078 :
2079 31880 : if (gen)
2080 : {
2081 14254 : Cext = (flag & nf_GENMAT)? trivial_fact()
2082 31880 : : mkpolmod(gen_1,nf_get_pol(nf));
2083 31880 : C0 = mkvec2(C, Cext);
2084 31880 : id = expandext(nf, C0, P, e);
2085 : } else {
2086 0 : Cext = NULL;
2087 0 : C0 = C;
2088 0 : id = expand(nf, C, P, e);
2089 : }
2090 31880 : if (id == C0) /* e = 0 */
2091 : {
2092 12470 : if (!C) return bnfisprincipal0(bnf, gen_1, flag);
2093 12456 : switch(typ(C))
2094 : {
2095 7 : case t_INT: case t_FRAC: case t_POL: case t_POLMOD: case t_COL:
2096 7 : return triv_gen(bnf, C, flag);
2097 : }
2098 12449 : C = idealhnf_shallow(nf,C);
2099 : }
2100 : else
2101 : {
2102 19410 : if (gen) { C = gel(id,1); Cext = gel(id,2); } else C = id;
2103 : }
2104 31859 : prec = prec_arch(bnf);
2105 31859 : c = getrand();
2106 : for (;;)
2107 70 : {
2108 31929 : pari_sp av1 = avma;
2109 31929 : GEN y = isprincipalall(bnf, C, &prec, flag);
2110 31929 : if (y)
2111 : {
2112 31859 : if (flag & nf_GEN_IF_PRINCIPAL)
2113 : {
2114 20545 : if (typ(y) == t_INT) return gc_NULL(av);
2115 20545 : y = add_principal_part(nf, y, Cext, flag);
2116 : }
2117 : else
2118 : {
2119 11314 : GEN u = gel(y,2);
2120 11314 : if (!gen || typ(y) != t_VEC) return gerepileupto(av,y);
2121 11314 : if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
2122 : }
2123 31859 : return gerepilecopy(av, y);
2124 : }
2125 70 : if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",prec);
2126 70 : set_avma(av1); bnf = bnfnewprec_shallow(bnf,prec); setrand(c);
2127 : }
2128 : }
2129 : GEN
2130 0 : isprincipalfact_or_fail(GEN bnf, GEN C, GEN P, GEN e)
2131 : {
2132 0 : const long flag = nf_GENMAT|nf_FORCE;
2133 : long prec;
2134 0 : pari_sp av = avma;
2135 0 : GEN u, y, id, C0, Cext, nf = bnf_get_nf(bnf);
2136 :
2137 0 : Cext = trivial_fact();
2138 0 : C0 = mkvec2(C, Cext);
2139 0 : id = expandext(nf, C0, P, e);
2140 0 : if (id == C0) /* e = 0 */
2141 0 : C = idealhnf_shallow(nf,C);
2142 : else {
2143 0 : C = gel(id,1); Cext = gel(id,2);
2144 : }
2145 0 : prec = prec_arch(bnf);
2146 0 : y = isprincipalall(bnf, C, &prec, flag);
2147 0 : if (!y) return gc_utoipos(av, prec);
2148 0 : u = gel(y,2);
2149 0 : if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
2150 0 : return gerepilecopy(av, y);
2151 : }
2152 :
2153 : GEN
2154 148709 : nfsign_from_logarch(GEN LA, GEN invpi, GEN archp)
2155 : {
2156 148709 : long l = lg(archp), i;
2157 148709 : GEN y = cgetg(l, t_VECSMALL);
2158 148710 : pari_sp av = avma;
2159 :
2160 279368 : for (i=1; i<l; i++)
2161 : {
2162 130656 : GEN c = ground( gmul(imag_i(gel(LA,archp[i])), invpi) );
2163 130656 : y[i] = mpodd(c)? 1: 0;
2164 : }
2165 148712 : set_avma(av); return y;
2166 : }
2167 :
2168 : GEN
2169 226889 : nfsign_tu(GEN bnf, GEN archp)
2170 : {
2171 : long n;
2172 226889 : if (bnf_get_tuN(bnf) != 2) return cgetg(1, t_VECSMALL);
2173 159801 : n = archp? lg(archp) - 1: nf_get_r1(bnf_get_nf(bnf));
2174 159801 : return const_vecsmall(n, 1);
2175 : }
2176 : GEN
2177 228089 : nfsign_fu(GEN bnf, GEN archp)
2178 : {
2179 228089 : GEN invpi, y, A = bnf_get_logfu(bnf), nf = bnf_get_nf(bnf);
2180 228092 : long j = 1, RU = lg(A);
2181 :
2182 228092 : if (!archp) archp = identity_perm( nf_get_r1(nf) );
2183 228092 : invpi = invr( mppi(nf_get_prec(nf)) );
2184 228118 : y = cgetg(RU,t_MAT);
2185 376742 : for (j = 1; j < RU; j++)
2186 148611 : gel(y,j) = nfsign_from_logarch(gel(A,j), invpi, archp);
2187 228131 : return y;
2188 : }
2189 : GEN
2190 35 : nfsign_units(GEN bnf, GEN archp, int add_zu)
2191 : {
2192 35 : GEN sfu = nfsign_fu(bnf, archp);
2193 35 : return add_zu? vec_prepend(sfu, nfsign_tu(bnf, archp)): sfu;
2194 : }
2195 :
2196 : /* obsolete */
2197 : GEN
2198 7 : signunits(GEN bnf)
2199 : {
2200 : pari_sp av;
2201 : GEN S, y, nf;
2202 : long i, j, r1, r2;
2203 :
2204 7 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
2205 7 : nf_get_sign(nf, &r1,&r2);
2206 7 : S = zeromatcopy(r1, r1+r2-1); av = avma;
2207 7 : y = nfsign_fu(bnf, NULL);
2208 14 : for (j = 1; j < lg(y); j++)
2209 : {
2210 7 : GEN Sj = gel(S,j), yj = gel(y,j);
2211 21 : for (i = 1; i <= r1; i++) gel(Sj,i) = yj[i]? gen_m1: gen_1;
2212 : }
2213 7 : set_avma(av); return S;
2214 : }
2215 :
2216 : static GEN
2217 729467 : get_log_embed(REL_t *rel, GEN M, long RU, long R1, long prec)
2218 : {
2219 729467 : GEN arch, C, z = rel->m;
2220 : long i;
2221 729467 : arch = typ(z) == t_COL? RgM_RgC_mul(M, z): const_col(nbrows(M), z);
2222 729452 : C = cgetg(RU+1, t_COL); arch = glog(arch, prec);
2223 1677700 : for (i=1; i<=R1; i++) gel(C,i) = gel(arch,i);
2224 1565402 : for ( ; i<=RU; i++) gel(C,i) = gmul2n(gel(arch,i), 1);
2225 729456 : return C;
2226 : }
2227 : static GEN
2228 1022553 : rel_embed(REL_t *rel, FB_t *F, GEN embs, long ind, GEN M, long RU, long R1,
2229 : long prec)
2230 : {
2231 : GEN C, D, perm;
2232 : long i, n;
2233 1022553 : if (!rel->relaut) return get_log_embed(rel, M, RU, R1, prec);
2234 : /* image of another relation by automorphism */
2235 293091 : C = gel(embs, ind - rel->relorig);
2236 293091 : perm = gel(F->embperm, rel->relaut);
2237 293091 : D = cgetg_copy(C, &n);
2238 1244729 : for (i = 1; i < n; i++)
2239 : {
2240 951635 : long v = perm[i];
2241 951635 : gel(D,i) = (v > 0)? gel(C,v): conj_i(gel(C,-v));
2242 : }
2243 293094 : return D;
2244 : }
2245 : static GEN
2246 106442 : get_embs(FB_t *F, RELCACHE_t *cache, GEN nf, GEN embs, long PREC)
2247 : {
2248 106442 : long ru, j, k, l = cache->last - cache->chk + 1, r1 = nf_get_r1(nf);
2249 106442 : GEN M = nf_get_M(nf), nembs = cgetg(cache->last - cache->base+1, t_MAT);
2250 : REL_t *rel;
2251 :
2252 3883757 : for (k = 1; k <= cache->chk - cache->base; k++) gel(nembs,k) = gel(embs,k);
2253 106442 : embs = nembs; ru = nbrows(M);
2254 1117367 : for (j=1,rel = cache->chk + 1; j < l; rel++,j++,k++)
2255 1010934 : gel(embs,k) = rel_embed(rel, F, embs, k, M, ru, r1, PREC);
2256 106433 : return embs;
2257 : }
2258 : static void
2259 936856 : set_rel_alpha(REL_t *rel, GEN auts, GEN vA, long ind)
2260 : {
2261 : GEN u;
2262 936856 : if (!rel->relaut)
2263 672527 : u = rel->m;
2264 : else
2265 264329 : u = ZM_ZC_mul(gel(auts, rel->relaut), gel(vA, ind - rel->relorig));
2266 936852 : gel(vA, ind) = u;
2267 936852 : }
2268 : static GEN
2269 2289649 : set_fact(FB_t *F, FACT *fact, GEN e, long *pnz)
2270 : {
2271 2289649 : long n = fact[0].pr;
2272 2289649 : GEN c = zero_Flv(F->KC);
2273 2289769 : if (!n) /* trivial factorization */
2274 63 : *pnz = F->KC+1;
2275 : else
2276 : {
2277 2289706 : long i, nz = minss(fact[1].pr, fact[n].pr);
2278 10678251 : for (i = 1; i <= n; i++) c[fact[i].pr] = fact[i].ex;
2279 2289704 : if (e)
2280 : {
2281 26132 : long l = lg(e);
2282 94481 : for (i = 1; i < l; i++)
2283 68349 : if (e[i]) { long v = F->subFB[i]; c[v] += e[i]; if (v < nz) nz = v; }
2284 : }
2285 2289704 : *pnz = nz;
2286 : }
2287 2289767 : return c;
2288 : }
2289 :
2290 : /* Is cols already in the cache ? bs = index of first non zero coeff in cols
2291 : * General check for colinearity useless since exceedingly rare */
2292 : static int
2293 2964396 : already_known(RELCACHE_t *cache, long bs, GEN cols)
2294 : {
2295 : REL_t *r;
2296 2964396 : long l = lg(cols);
2297 222086778 : for (r = cache->last; r > cache->base; r--)
2298 219683458 : if (bs == r->nz)
2299 : {
2300 39601511 : GEN coll = r->R;
2301 39601511 : long b = bs;
2302 126403432 : while (b < l && cols[b] == coll[b]) b++;
2303 39601511 : if (b == l) return 1;
2304 : }
2305 2403320 : return 0;
2306 : }
2307 :
2308 : /* Add relation R to cache, nz = index of first non zero coeff in R.
2309 : * If relation is a linear combination of the previous ones, return 0.
2310 : * Otherwise, update basis and return > 0. Compute mod p (much faster)
2311 : * so some kernel vector might not be genuine. */
2312 : static int
2313 2968465 : add_rel_i(RELCACHE_t *cache, GEN R, long nz, GEN m, long orig, long aut, REL_t **relp, long in_rnd_rel)
2314 : {
2315 2968465 : long i, k, n = lg(R)-1;
2316 :
2317 2968465 : if (nz == n+1) { k = 0; goto ADD_REL; }
2318 2964398 : if (already_known(cache, nz, R)) return -1;
2319 2403325 : if (cache->last >= cache->base + cache->len) return 0;
2320 2403325 : if (DEBUGLEVEL>6)
2321 : {
2322 0 : err_printf("adding vector = %Ps\n",R);
2323 0 : err_printf("generators =\n%Ps\n", cache->basis);
2324 : }
2325 2403366 : if (cache->missing)
2326 : {
2327 1998358 : GEN a = leafcopy(R), basis = cache->basis;
2328 1998359 : k = lg(a);
2329 123139354 : do --k; while (!a[k]);
2330 7428702 : while (k)
2331 : {
2332 5894029 : GEN c = gel(basis, k);
2333 5894029 : if (c[k])
2334 : {
2335 5430343 : long ak = a[k];
2336 263332881 : for (i=1; i < k; i++) if (c[i]) a[i] = (a[i] + ak*(mod_p-c[i])) % mod_p;
2337 5430343 : a[k] = 0;
2338 128864622 : do --k; while (!a[k]); /* k cannot go below 0: codeword is a sentinel */
2339 : }
2340 : else
2341 : {
2342 463686 : ulong invak = Fl_inv(uel(a,k), mod_p);
2343 : /* Cleanup a */
2344 13681127 : for (i = k; i-- > 1; )
2345 : {
2346 13217442 : long j, ai = a[i];
2347 13217442 : c = gel(basis, i);
2348 13217442 : if (!ai || !c[i]) continue;
2349 261532 : ai = mod_p-ai;
2350 4466878 : for (j = 1; j < i; j++) if (c[j]) a[j] = (a[j] + ai*c[j]) % mod_p;
2351 261532 : a[i] = 0;
2352 : }
2353 : /* Insert a/a[k] as k-th column */
2354 463685 : c = gel(basis, k);
2355 13681125 : for (i = 1; i<k; i++) if (a[i]) c[i] = (a[i] * invak) % mod_p;
2356 463685 : c[k] = 1; a = c;
2357 : /* Cleanup above k */
2358 13495699 : for (i = k+1; i<n; i++)
2359 : {
2360 : long j, ck;
2361 13032014 : c = gel(basis, i);
2362 13032014 : ck = c[k];
2363 13032014 : if (!ck) continue;
2364 2706378 : ck = mod_p-ck;
2365 98785964 : for (j = 1; j < k; j++) if (a[j]) c[j] = (c[j] + ck*a[j]) % mod_p;
2366 2706378 : c[k] = 0;
2367 : }
2368 463685 : cache->missing--;
2369 463685 : break;
2370 : }
2371 : }
2372 : }
2373 : else
2374 405008 : k = (cache->last - cache->base) + 1;
2375 2403366 : if (k || cache->relsup > 0 || (m && in_rnd_rel))
2376 : {
2377 : REL_t *rel;
2378 :
2379 995673 : ADD_REL:
2380 999740 : rel = ++cache->last;
2381 999740 : if (!k && cache->relsup && nz < n+1)
2382 : {
2383 126754 : cache->relsup--;
2384 126754 : k = (rel - cache->base) + cache->missing;
2385 : }
2386 999740 : rel->R = gclone(R);
2387 999738 : rel->m = m ? gclone(m) : NULL;
2388 999734 : rel->nz = nz;
2389 999734 : if (aut)
2390 : {
2391 290495 : rel->relorig = (rel - cache->base) - orig;
2392 290495 : rel->relaut = aut;
2393 : }
2394 : else
2395 709239 : rel->relaut = 0;
2396 999734 : if (relp) *relp = rel;
2397 999734 : if (DEBUGLEVEL) dbg_newrel(cache);
2398 : }
2399 2407413 : return k;
2400 : }
2401 :
2402 : static int
2403 2460552 : add_rel(RELCACHE_t *cache, FB_t *F, GEN R, long nz, GEN m, long in_rnd_rel)
2404 : {
2405 : REL_t *rel;
2406 : long k, l, reln;
2407 2460552 : const long lauts = lg(F->idealperm), KC = F->KC;
2408 :
2409 2460552 : k = add_rel_i(cache, R, nz, m, 0, 0, &rel, in_rnd_rel);
2410 2460582 : if (k > 0 && typ(m) != t_INT)
2411 : {
2412 538157 : GEN Rl = cgetg(KC+1, t_VECSMALL);
2413 538154 : reln = rel - cache->base;
2414 1046068 : for (l = 1; l < lauts; l++)
2415 : {
2416 507899 : GEN perml = gel(F->idealperm, l);
2417 507899 : long i, nzl = perml[nz];
2418 :
2419 20487703 : for (i = 1; i <= KC; i++) Rl[i] = 0;
2420 18272421 : for (i = nz; i <= KC; i++)
2421 17764522 : if (R[i])
2422 : {
2423 1420204 : long v = perml[i];
2424 :
2425 1420204 : if (v < nzl) nzl = v;
2426 1420204 : Rl[v] = R[i];
2427 : }
2428 507899 : (void)add_rel_i(cache, Rl, nzl, NULL, reln, l, NULL, in_rnd_rel);
2429 : }
2430 : }
2431 2460594 : return k;
2432 : }
2433 :
2434 : INLINE void
2435 30245106 : step(GEN x, double *y, GEN inc, long k)
2436 : {
2437 30245106 : if (!y[k])
2438 2109133 : x[k]++; /* leading coeff > 0 */
2439 : else
2440 : {
2441 28135973 : long i = inc[k];
2442 28135973 : x[k] += i;
2443 28135973 : inc[k] = (i > 0)? -1-i: 1-i;
2444 : }
2445 30245106 : }
2446 :
2447 : static double
2448 211038 : Fincke_Pohst_bound(double T, GEN r)
2449 : {
2450 211038 : pari_sp av = avma;
2451 211038 : GEN zT = dbltor(T * T), p = gmael(r,1,1), B = real_1(DEFAULTPREC);
2452 211032 : long i, n = lg(r)-1;
2453 574702 : for (i = 2; i <= n; i++)
2454 : {
2455 574700 : p = gmul(p, gmael(r,i,i));
2456 574699 : B = sqrtnr(gmul(zT,p), i);
2457 574704 : if (i == n || cmprr(B, gmael(r,i+1,i+1)) < 0) break;
2458 : }
2459 211036 : return gc_double(av, rtodbl(B));
2460 : }
2461 :
2462 : INLINE long
2463 211038 : Fincke_Pohst_ideal(RELCACHE_t *cache, FB_t *F, GEN nf, GEN M, GEN I,
2464 : GEN NI, FACT *fact, long Nrelid, FP_t *fp, RNDREL_t *rr, long prec,
2465 : long *Nsmall, long *Nfact)
2466 : {
2467 : pari_sp av;
2468 211038 : const long N = nf_get_degree(nf), R1 = nf_get_r1(nf);
2469 211036 : GEN G = nf_get_G(nf), G0 = nf_get_roundG(nf), r, u, gx, inc, ideal;
2470 : double BOUND, B1, B2;
2471 211035 : long j, k, skipfirst, relid=0, try_factor=0;
2472 :
2473 211035 : inc = const_vecsmall(N, 1);
2474 211036 : u = ZM_lll(ZM_mul(G0, I), 0.99, LLL_IM);
2475 211037 : ideal = ZM_mul(I,u); /* approximate T2-LLL reduction */
2476 211036 : r = gaussred_from_QR(RgM_mul(G, ideal), prec); /* Cholesky for T2 | ideal */
2477 211039 : if (!r) pari_err_BUG("small_norm (precision too low)");
2478 :
2479 1035554 : for (k=1; k<=N; k++)
2480 : {
2481 824516 : if (!gisdouble(gcoeff(r,k,k),&(fp->v[k]))) return 0;
2482 2622657 : for (j=1; j<k; j++) if (!gisdouble(gcoeff(r,j,k),&(fp->q[j][k]))) return 0;
2483 824516 : if (DEBUGLEVEL>3) err_printf("v[%ld]=%.4g ",k,fp->v[k]);
2484 : }
2485 211038 : B1 = fp->v[1]; /* T2(ideal[1]) */
2486 211038 : B2 = fp->v[2] + B1 * fp->q[1][2] * fp->q[1][2]; /* T2(ideal[2]) */
2487 211038 : skipfirst = ZV_isscalar(gel(ideal,1));
2488 211038 : BOUND = maxdd(2*B2, Fincke_Pohst_bound(4 * maxtry_FACT / F->ballvol, r));
2489 211036 : if (DEBUGLEVEL>1)
2490 : {
2491 0 : if (DEBUGLEVEL>3) err_printf("\n");
2492 0 : err_printf("BOUND = %.4g\n",BOUND);
2493 : }
2494 :
2495 211037 : k = N; fp->y[N] = fp->z[N] = 0; fp->x[N] = 0;
2496 20572221 : for (av = avma;; set_avma(av), step(fp->x,fp->y,inc,k))
2497 20362344 : {
2498 : GEN R;
2499 : long nz;
2500 : do
2501 : { /* look for primitive element of small norm, cf minim00 */
2502 25461387 : int fl = 0;
2503 : double p;
2504 25461387 : if (k > 1)
2505 : {
2506 5099149 : long l = k-1;
2507 5099149 : fp->z[l] = 0;
2508 45455222 : for (j=k; j<=N; j++) fp->z[l] += fp->q[l][j]*fp->x[j];
2509 5099149 : p = (double)fp->x[k] + fp->z[k];
2510 5099149 : fp->y[l] = fp->y[k] + p*p*fp->v[k];
2511 5099149 : if (l <= skipfirst && !fp->y[1]) fl = 1;
2512 5099149 : fp->x[l] = (long)floor(-fp->z[l] + 0.5);
2513 5099149 : k = l;
2514 : }
2515 4490890 : for(;; step(fp->x,fp->y,inc,k))
2516 : {
2517 29952273 : if (!fl)
2518 : {
2519 29895607 : p = (double)fp->x[k] + fp->z[k];
2520 29895607 : if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
2521 :
2522 5391798 : step(fp->x,fp->y,inc,k);
2523 :
2524 5392133 : p = (double)fp->x[k] + fp->z[k];
2525 5392133 : if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
2526 : }
2527 4493277 : fl = 0; inc[k] = 1;
2528 4493277 : if (++k > N) goto END_Fincke_Pohst_ideal;
2529 : }
2530 25459331 : } while (k > 1);
2531 :
2532 : /* element complete */
2533 37154778 : if (zv_content(fp->x) !=1) continue; /* not primitive */
2534 17146614 : gx = ZM_zc_mul(ideal,fp->x);
2535 17146493 : if (ZV_isscalar(gx)) continue;
2536 17197579 : if (++try_factor > maxtry_FACT) break;
2537 :
2538 17094021 : if (!Nrelid)
2539 : {
2540 259 : if (!factorgen(F,nf,I,NI,gx,fact)) continue;
2541 105096 : return 1;
2542 : }
2543 17093762 : else if (rr)
2544 : {
2545 232253 : if (!factorgen(F,nf,I,NI,gx,fact)) continue;
2546 26132 : add_to_fact(rr->jid, 1, fact);
2547 : }
2548 : else
2549 : {
2550 16861509 : GEN Nx, xembed = RgM_RgC_mul(M, gx);
2551 : long e;
2552 16862157 : if (Nsmall) (*Nsmall)++;
2553 16862157 : Nx = grndtoi(embed_norm(xembed, R1), &e);
2554 16862113 : if (e >= 0) {
2555 0 : if (DEBUGLEVEL > 1) err_printf("+");
2556 14603457 : continue;
2557 : }
2558 16862113 : if (!can_factor(F, nf, NULL, gx, Nx, fact)) continue;
2559 : }
2560 :
2561 : /* smooth element */
2562 2281478 : R = set_fact(F, fact, rr ? rr->ex : NULL, &nz);
2563 : /* make sure we get maximal rank first, then allow all relations */
2564 2281610 : if (add_rel(cache, F, R, nz, gx, rr ? 1 : 0) <= 0)
2565 : { /* probably Q-dependent from previous ones: forget it */
2566 1743717 : if (DEBUGLEVEL>1) err_printf("*");
2567 1743719 : if (DEBUGLEVEL && Nfact && rr) (*Nfact)++;
2568 1743719 : continue;
2569 : }
2570 537935 : if (DEBUGLEVEL && Nfact) (*Nfact)++;
2571 537935 : if (cache->last >= cache->end) return 1; /* we have enough */
2572 432853 : if (++relid == Nrelid) break;
2573 : }
2574 105945 : END_Fincke_Pohst_ideal:
2575 105945 : return 0;
2576 : }
2577 :
2578 : static void
2579 89741 : small_norm(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, GEN M,
2580 : FACT *fact, GEN p0)
2581 : {
2582 89741 : const long prec = nf_get_prec(nf);
2583 : FP_t fp;
2584 : pari_sp av;
2585 89741 : GEN L_jid = F->L_jid, Np0 = NULL;
2586 89741 : long Nsmall, Nfact, n = lg(L_jid);
2587 : pari_timer T;
2588 :
2589 89741 : if (DEBUGLEVEL)
2590 : {
2591 0 : timer_start(&T);
2592 0 : err_printf("#### Look for %ld relations in %ld ideals (small_norm)\n",
2593 0 : cache->end - cache->last, lg(L_jid)-1);
2594 0 : if (p0) err_printf("Look in p0 = %Ps\n", vecslice(p0,1,4));
2595 : }
2596 89741 : Nsmall = Nfact = 0;
2597 89741 : minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
2598 89741 : if (p0)
2599 : {
2600 27004 : GEN n = pr_norm(p0);
2601 27004 : ulong e = maxuu(1,logint0(sqri(pr_norm(veclast(F->LP))), n, NULL));
2602 27004 : p0 = idealpow(nf, p0, utoi(e));
2603 27004 : Np0 = powiu(n,e);
2604 : }
2605 190150 : for (av = avma; --n; set_avma(av))
2606 : {
2607 189661 : long j = L_jid[n];
2608 189661 : GEN id = gel(F->LP, j), Nid;
2609 189661 : if (DEBUGLEVEL>1)
2610 0 : err_printf("\n*** Ideal no %ld: %Ps\n", j, vecslice(id,1,4));
2611 189661 : if (p0)
2612 32163 : { Nid = mulii(Np0, pr_norm(id)); id = idealmul(nf, p0, id); }
2613 : else
2614 157498 : { Nid = pr_norm(id); id = pr_hnf(nf, id);}
2615 189658 : if (Fincke_Pohst_ideal(cache, F, nf, M, id, Nid, fact, Nrelid, &fp,
2616 89250 : NULL, prec, &Nsmall, &Nfact)) break;
2617 : }
2618 89741 : if (DEBUGLEVEL && Nsmall)
2619 : {
2620 0 : if (DEBUGLEVEL == 1)
2621 0 : { if (Nfact) err_printf("\n"); }
2622 : else
2623 0 : err_printf(" \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
2624 0 : Nfact,Nsmall,((double)Nfact)/Nsmall);
2625 0 : if (timer_get(&T)>1) timer_printf(&T,"small_norm");
2626 : }
2627 89741 : }
2628 :
2629 : static GEN
2630 16692 : get_random_ideal(FB_t *F, GEN nf, GEN ex)
2631 : {
2632 16692 : long i, l = lg(ex);
2633 : for (;;)
2634 641 : {
2635 17333 : GEN I = NULL;
2636 65609 : for (i = 1; i < l; i++)
2637 48276 : if ((ex[i] = random_bits(RANDOM_BITS)))
2638 : {
2639 45157 : GEN pr = gel(F->LP, F->subFB[i]), e = utoipos(ex[i]);
2640 45157 : I = I? idealmulpowprime(nf, I, pr, e): idealpow(nf, pr, e);
2641 : }
2642 17333 : if (I && !ZM_isscalar(I,NULL)) return I; /* != (n)Z_K */
2643 : }
2644 : }
2645 :
2646 : static void
2647 16692 : rnd_rel(RELCACHE_t *cache, FB_t *F, GEN nf, FACT *fact)
2648 : {
2649 : pari_timer T;
2650 16692 : GEN L_jid = F->L_jid, M = nf_get_M(nf), R, NR;
2651 16692 : long i, l = lg(L_jid), prec = nf_get_prec(nf), Nfact = 0;
2652 : RNDREL_t rr;
2653 : FP_t fp;
2654 : pari_sp av;
2655 :
2656 16692 : if (DEBUGLEVEL) {
2657 0 : timer_start(&T);
2658 0 : err_printf("#### Look for %ld relations in %ld ideals (rnd_rel)\n",
2659 0 : cache->end - cache->last, l-1);
2660 : }
2661 16692 : rr.ex = cgetg(lg(F->subFB), t_VECSMALL);
2662 16692 : R = get_random_ideal(F, nf, rr.ex); /* random product from subFB */
2663 16692 : NR = ZM_det_triangular(R);
2664 16692 : minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
2665 22226 : for (av = avma, i = 1; i < l; i++, set_avma(av))
2666 : { /* try P[j] * base */
2667 21366 : long j = L_jid[i];
2668 21366 : GEN P = gel(F->LP, j), Nid = mulii(NR, pr_norm(P));
2669 21366 : if (DEBUGLEVEL>1) err_printf("\n*** Ideal %ld: %Ps\n", j, vecslice(P,1,4));
2670 21366 : rr.jid = j;
2671 21366 : if (Fincke_Pohst_ideal(cache, F, nf, M, idealHNF_mul(nf, R, P), Nid, fact,
2672 15832 : RND_REL_RELPID, &fp, &rr, prec, NULL, &Nfact)) break;
2673 : }
2674 16692 : if (DEBUGLEVEL)
2675 : {
2676 0 : if (Nfact) err_printf("\n");
2677 0 : if (timer_get(&T)>=0) timer_printf(&T,"rnd_rel");
2678 : }
2679 16692 : }
2680 :
2681 : static GEN
2682 63621 : automorphism_perms(GEN M, GEN auts, GEN cyclic, long r1, long r2, long N)
2683 : {
2684 63621 : long L = lgcols(M), lauts = lg(auts), lcyc = lg(cyclic), i, j, l, m;
2685 63621 : GEN Mt, perms = cgetg(lauts, t_VEC);
2686 : pari_sp av;
2687 :
2688 127464 : for (l = 1; l < lauts; l++) gel(perms, l) = cgetg(L, t_VECSMALL);
2689 63621 : av = avma;
2690 63621 : Mt = shallowtrans(gprec_w(M, LOWDEFAULTPREC));
2691 63623 : Mt = shallowconcat(Mt, conj_i(vecslice(Mt, r1+1, r1+r2)));
2692 110229 : for (l = 1; l < lcyc; l++)
2693 : {
2694 46610 : GEN thiscyc = gel(cyclic, l), thisperm, perm, prev, Nt;
2695 46610 : long k = thiscyc[1];
2696 :
2697 46610 : Nt = RgM_mul(shallowtrans(gel(auts, k)), Mt);
2698 46611 : perm = gel(perms, k);
2699 153208 : for (i = 1; i < L; i++)
2700 : {
2701 106598 : GEN v = gel(Nt, i), minD;
2702 106598 : minD = gnorml2(gsub(v, gel(Mt, 1)));
2703 106604 : perm[i] = 1;
2704 563240 : for (j = 2; j <= N; j++)
2705 : {
2706 456643 : GEN D = gnorml2(gsub(v, gel(Mt, j)));
2707 456629 : if (gcmp(D, minD) < 0) { minD = D; perm[i] = j >= L ? r2-j : j; }
2708 : }
2709 : }
2710 65055 : for (prev = perm, m = 2; m < lg(thiscyc); m++, prev = thisperm)
2711 : {
2712 18445 : thisperm = gel(perms, thiscyc[m]);
2713 93604 : for (i = 1; i < L; i++)
2714 : {
2715 75159 : long pp = labs(prev[i]);
2716 75159 : thisperm[i] = prev[i] < 0 ? -perm[pp] : perm[pp];
2717 : }
2718 : }
2719 : }
2720 63619 : set_avma(av); return perms;
2721 : }
2722 :
2723 : /* Determine the field automorphisms as matrices on the integral basis */
2724 : static GEN
2725 63682 : automorphism_matrices(GEN nf, GEN *cycp)
2726 : {
2727 63682 : pari_sp av = avma;
2728 63682 : GEN auts = galoisconj(nf, NULL), mats, cyclic, cyclicidx;
2729 63682 : long nauts = lg(auts)-1, i, j, k, l;
2730 :
2731 63682 : cyclic = cgetg(nauts+1, t_VEC);
2732 63682 : cyclicidx = zero_Flv(nauts);
2733 97759 : for (l = 1; l <= nauts; l++)
2734 : {
2735 97759 : GEN aut = gel(auts, l);
2736 97759 : if (gequalX(aut)) { swap(gel(auts, l), gel(auts, nauts)); break; }
2737 : }
2738 : /* trivial automorphism is last */
2739 191241 : for (l = 1; l <= nauts; l++) gel(auts, l) = algtobasis(nf, gel(auts, l));
2740 : /* Compute maximal cyclic subgroups */
2741 127556 : for (l = nauts; --l > 0; ) if (!cyclicidx[l])
2742 : {
2743 48095 : GEN elt = gel(auts, l), aut = elt, cyc = cgetg(nauts+1, t_VECSMALL);
2744 48096 : cyc[1] = cyclicidx[l] = l; j = 1;
2745 : do
2746 : {
2747 67087 : elt = galoisapply(nf, elt, aut);
2748 217498 : for (k = 1; k <= nauts; k++) if (gequal(elt, gel(auts, k))) break;
2749 67085 : cyclicidx[k] = l; cyc[++j] = k;
2750 : }
2751 67085 : while (k != nauts);
2752 48094 : setlg(cyc, j);
2753 48094 : gel(cyclic, l) = cyc;
2754 : }
2755 127556 : for (i = j = 1; i < nauts; i++)
2756 63873 : if (cyclicidx[i] == i) cyclic[j++] = cyclic[i];
2757 63683 : setlg(cyclic, j);
2758 63683 : mats = cgetg(nauts, t_VEC);
2759 110323 : while (--j > 0)
2760 : {
2761 46638 : GEN cyc = gel(cyclic, j);
2762 46638 : long id = cyc[1];
2763 46638 : GEN M, Mi, aut = gel(auts, id);
2764 :
2765 46638 : gel(mats, id) = Mi = M = nfgaloismatrix(nf, aut);
2766 65084 : for (i = 2; i < lg(cyc); i++) gel(mats, cyc[i]) = Mi = ZM_mul(Mi, M);
2767 : }
2768 63685 : gerepileall(av, 2, &mats, &cyclic);
2769 63685 : if (cycp) *cycp = cyclic;
2770 63685 : return mats;
2771 : }
2772 :
2773 : /* vP a list of maximal ideals above the same p from idealprimedec: f(P/p) is
2774 : * increasing; 1 <= j <= #vP; orbit a zc of length <= #vP; auts a vector of
2775 : * automorphisms in ZM form.
2776 : * Set orbit[i] = 1 for all vP[i], i >= j, in the orbit of pr = vP[j] wrt auts.
2777 : * N.B.1 orbit need not be initialized to 0: useful to incrementally run
2778 : * through successive orbits
2779 : * N.B.2 i >= j, so primes with index < j will be missed; run incrementally
2780 : * starting from j = 1 ! */
2781 : static void
2782 11865 : pr_orbit_fill(GEN orbit, GEN auts, GEN vP, long j)
2783 : {
2784 11865 : GEN pr = gel(vP,j), gen = pr_get_gen(pr);
2785 11865 : long i, l = lg(auts), J = lg(orbit), f = pr_get_f(pr);
2786 11865 : orbit[j] = 1;
2787 23730 : for (i = 1; i < l; i++)
2788 : {
2789 11865 : GEN g = ZM_ZC_mul(gel(auts,i), gen);
2790 : long k;
2791 11886 : for (k = j+1; k < J; k++)
2792 : {
2793 35 : GEN prk = gel(vP,k);
2794 35 : if (pr_get_f(prk) > f) break; /* f(P[k]) increases with k */
2795 : /* don't check that e matches: (almost) always 1 ! */
2796 35 : if (!orbit[k] && ZC_prdvd(g, prk)) { orbit[k] = 1; break; }
2797 : }
2798 : }
2799 11865 : }
2800 : /* remark: F->KCZ changes if be_honest() fails */
2801 : static int
2802 7 : be_honest(FB_t *F, GEN nf, GEN auts, FACT *fact)
2803 : {
2804 : long i, iz, nbtest;
2805 7 : long lgsub = lg(F->subFB), KCZ0 = F->KCZ;
2806 7 : long N = nf_get_degree(nf), prec = nf_get_prec(nf);
2807 7 : GEN M = nf_get_M(nf);
2808 : FP_t fp;
2809 : pari_sp av;
2810 :
2811 7 : if (DEBUGLEVEL) {
2812 0 : err_printf("Be honest for %ld primes from %ld to %ld\n", F->KCZ2 - F->KCZ,
2813 0 : F->FB[ F->KCZ+1 ], F->FB[ F->KCZ2 ]);
2814 : }
2815 7 : minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
2816 7 : if (lg(auts) == 1) auts = NULL;
2817 7 : av = avma;
2818 14 : for (iz=F->KCZ+1; iz<=F->KCZ2; iz++, set_avma(av))
2819 : {
2820 7 : long p = F->FB[iz];
2821 7 : GEN pr_orbit, P = gel(F->LV,p);
2822 7 : long j, J = lg(P); /* > 1 */
2823 : /* the P|p, NP > C2 are assumed in subgroup generated by FB + last P
2824 : * with NP <= C2 is unramified --> check all but last */
2825 7 : if (pr_get_e(gel(P,J-1)) == 1) J--;
2826 7 : if (J == 1) continue;
2827 7 : if (DEBUGLEVEL>1) err_printf("%ld ", p);
2828 7 : pr_orbit = auts? zero_zv(J-1): NULL;
2829 28 : for (j = 1; j < J; j++)
2830 : {
2831 : GEN Nid, id, id0;
2832 21 : if (pr_orbit)
2833 : {
2834 21 : if (pr_orbit[j]) continue;
2835 : /* discard all primes in automorphism orbit simultaneously */
2836 14 : pr_orbit_fill(pr_orbit, auts, P, j);
2837 : }
2838 14 : id = id0 = pr_hnf(nf,gel(P,j));
2839 14 : Nid = pr_norm(gel(P,j));
2840 14 : for (nbtest=0;;)
2841 : {
2842 14 : if (Fincke_Pohst_ideal(NULL, F, nf, M, id, Nid, fact, 0, &fp,
2843 14 : NULL, prec, NULL, NULL)) break;
2844 0 : if (++nbtest > maxtry_HONEST)
2845 : {
2846 0 : if (DEBUGLEVEL)
2847 0 : pari_warn(warner,"be_honest() failure on prime %Ps\n", gel(P,j));
2848 0 : return 0;
2849 : }
2850 : /* occurs at most once in the whole function */
2851 0 : for (i = 1, id = id0; i < lgsub; i++)
2852 : {
2853 0 : long ex = random_bits(RANDOM_BITS);
2854 0 : if (ex)
2855 : {
2856 0 : GEN pr = gel(F->LP, F->subFB[i]);
2857 0 : id = idealmulpowprime(nf, id, pr, utoipos(ex));
2858 : }
2859 : }
2860 0 : if (!equali1(gcoeff(id,N,N))) id = Q_primpart(id);
2861 0 : if (expi(gcoeff(id,1,1)) > 100) id = idealred(nf, id);
2862 0 : Nid = ZM_det_triangular(id);
2863 : }
2864 : }
2865 7 : F->KCZ++; /* SUCCESS, "enlarge" factorbase */
2866 : }
2867 7 : F->KCZ = KCZ0; return gc_bool(av,1);
2868 : }
2869 :
2870 : /* all primes with N(P) <= BOUND factor on factorbase ? */
2871 : void
2872 63 : bnftestprimes(GEN bnf, GEN BOUND)
2873 : {
2874 63 : pari_sp av0 = avma, av;
2875 63 : ulong count = 0;
2876 63 : GEN auts, p, nf = bnf_get_nf(bnf), Vbase = bnf_get_vbase(bnf);
2877 63 : GEN fb = gen_sort_shallow(Vbase, (void*)&cmp_prime_ideal, cmp_nodata);
2878 63 : ulong pmax = pr_get_smallp(veclast(fb)); /*largest p in factorbase*/
2879 : forprime_t S;
2880 : FACT *fact;
2881 : FB_t F;
2882 :
2883 63 : (void)recover_partFB(&F, Vbase, nf_get_degree(nf));
2884 63 : fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
2885 63 : forprime_init(&S, gen_2, BOUND);
2886 63 : auts = automorphism_matrices(nf, NULL);
2887 63 : if (lg(auts) == 1) auts = NULL;
2888 63 : av = avma;
2889 37604 : while (( p = forprime_next(&S) ))
2890 : {
2891 : GEN pr_orbit, vP;
2892 : long j, J;
2893 :
2894 37541 : if (DEBUGLEVEL == 1 && ++count > 1000)
2895 : {
2896 0 : err_printf("passing p = %Ps / %Ps\n", p, BOUND);
2897 0 : count = 0;
2898 : }
2899 37541 : set_avma(av);
2900 37541 : vP = idealprimedec_limit_norm(nf, p, BOUND);
2901 37541 : J = lg(vP);
2902 : /* if last is unramified, all P|p in subgroup generated by FB: skip last */
2903 37541 : if (J > 1 && pr_get_e(gel(vP,J-1)) == 1) J--;
2904 37541 : if (J == 1) continue;
2905 14525 : if (DEBUGLEVEL>1) err_printf("*** p = %Ps\n",p);
2906 14525 : pr_orbit = auts? zero_zv(J-1): NULL;
2907 31549 : for (j = 1; j < J; j++)
2908 : {
2909 17024 : GEN P = gel(vP,j);
2910 17024 : long k = 0;
2911 17024 : if (pr_orbit)
2912 : {
2913 11858 : if (pr_orbit[j]) continue;
2914 : /* discard all primes in automorphism orbit simultaneously */
2915 11851 : pr_orbit_fill(pr_orbit, auts, vP, j);
2916 : }
2917 17017 : if (abscmpiu(p, pmax) > 0 || !(k = tablesearch(fb, P, &cmp_prime_ideal)))
2918 16408 : (void)SPLIT(&F, nf, pr_hnf(nf,P), Vbase, fact);
2919 17017 : if (DEBUGLEVEL>1)
2920 : {
2921 0 : err_printf(" Testing P = %Ps\n",P);
2922 0 : if (k) err_printf(" #%ld in factor base\n",k);
2923 0 : else err_printf(" is %Ps\n", isprincipal(bnf,P));
2924 : }
2925 : }
2926 : }
2927 63 : set_avma(av0);
2928 63 : }
2929 :
2930 : /* A t_MAT of complex floats, in fact reals. Extract a submatrix B
2931 : * whose columns are definitely nonzero, i.e. gexpo(A[j]) >= -2
2932 : *
2933 : * If possible precision problem (t_REAL 0 with large exponent), set
2934 : * *precpb to 1 */
2935 : static GEN
2936 90144 : clean_cols(GEN A, int *precpb)
2937 : {
2938 90144 : long l = lg(A), h, i, j, k;
2939 : GEN B;
2940 90144 : *precpb = 0;
2941 90144 : if (l == 1) return A;
2942 90144 : h = lgcols(A);;
2943 90144 : B = cgetg(l, t_MAT);
2944 3600006 : for (i = k = 1; i < l; i++)
2945 : {
2946 3509863 : GEN Ai = gel(A,i);
2947 3509863 : int non0 = 0;
2948 17044664 : for (j = 1; j < h; j++)
2949 : {
2950 13534802 : GEN c = gel(Ai,j);
2951 13534802 : if (gexpo(c) >= -2)
2952 : {
2953 12071107 : if (gequal0(c)) *precpb = 1; else non0 = 1;
2954 : }
2955 : }
2956 3509862 : if (non0) gel(B, k++) = Ai;
2957 : }
2958 90143 : setlg(B, k); return B;
2959 : }
2960 :
2961 : static long
2962 3085309 : compute_multiple_of_R_pivot(GEN X, GEN x0/*unused*/, long ix, GEN c)
2963 : {
2964 3085309 : GEN x = gel(X,ix);
2965 3085309 : long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
2966 : (void)x0;
2967 15459835 : for (i=1; i<lx; i++)
2968 12374529 : if (!c[i] && !gequal0(gel(x,i)))
2969 : {
2970 2989168 : long e = gexpo(gel(x,i));
2971 2989164 : if (e > ex) { ex = e; k = i; }
2972 : }
2973 3085306 : return (k && ex > -32)? k: lx;
2974 : }
2975 :
2976 : /* Ar = (log |sigma_i(u_j)|) for units (u_j) found so far;
2977 : * RU = R1+R2 = target rank for unit matrix, after adding [1 x r1, 2 x r2];
2978 : * N = field degree, need = unit rank defect;
2979 : * L = NULL (prec problem) or B^(-1) * A with approximate rational entries
2980 : * (as t_REAL), B a submatrix of A, with (probably) maximal rank RU */
2981 : static GEN
2982 105989 : compute_multiple_of_R(GEN Ar, long RU, long N, long *pneed, long *bit, GEN *ptL)
2983 : {
2984 : GEN T, d, mdet, Im_mdet, kR, L;
2985 105989 : long i, j, r, R1 = 2*RU - N;
2986 : int precpb;
2987 105989 : pari_sp av = avma;
2988 :
2989 105989 : if (RU == 1) { *ptL = zeromat(0, lg(Ar)-1); return gen_1; }
2990 :
2991 90143 : if (DEBUGLEVEL) err_printf("\n#### Computing regulator multiple\n");
2992 90143 : mdet = clean_cols(Ar, &precpb);
2993 : /* will cause precision to increase on later failure, but we may succeed! */
2994 90144 : *ptL = precpb? NULL: gen_1;
2995 90144 : T = cgetg(RU+1,t_COL);
2996 244532 : for (i=1; i<=R1; i++) gel(T,i) = gen_1;
2997 191231 : for ( ; i<=RU; i++) gel(T,i) = gen_2;
2998 90144 : mdet = shallowconcat(T, mdet); /* det(Span(mdet)) = N * R */
2999 :
3000 : /* could be using indexrank(), but need custom "get_pivot" function */
3001 90143 : d = RgM_pivots(mdet, NULL, &r, &compute_multiple_of_R_pivot);
3002 : /* # of independent columns = target rank ? */
3003 90144 : if (lg(mdet)-1 - r != RU)
3004 : {
3005 32306 : if (DEBUGLEVEL)
3006 0 : err_printf("Units matrix target rank = %ld < %ld\n",lg(mdet)-1 - r, RU);
3007 32306 : *pneed = RU - (lg(mdet)-1-r); return gc_NULL(av);
3008 : }
3009 :
3010 57838 : Im_mdet = cgetg(RU+1, t_MAT); /* extract independent columns */
3011 : /* N.B: d[1] = 1, corresponding to T above */
3012 57838 : gel(Im_mdet, 1) = T;
3013 248282 : for (i = j = 2; i <= RU; j++)
3014 190444 : if (d[j]) gel(Im_mdet, i++) = gel(mdet,j);
3015 :
3016 : /* integral multiple of R: the cols we picked form a Q-basis, they have an
3017 : * index in the full lattice. First column is T */
3018 57838 : kR = divru(det2(Im_mdet), N);
3019 : /* R > 0.2 uniformly */
3020 57837 : if (!signe(kR) || expo(kR) < -3)
3021 : {
3022 0 : if (DEBUGLEVEL) err_printf("Regulator is zero.\n");
3023 0 : *pneed = 0; return gc_NULL(av);
3024 : }
3025 57838 : d = det2(rowslice(vecslice(Im_mdet, 2, RU), 2, RU));
3026 57838 : setabssign(d); setabssign(kR);
3027 57838 : if (gexpo(gsub(d,kR)) - gexpo(d) > -20) { *ptL = NULL; return gc_NULL(av); }
3028 57831 : L = RgM_inv(Im_mdet);
3029 : /* estimate # of correct bits in result */
3030 57831 : if (!L || (*bit = -gexpo(RgM_Rg_sub_shallow(RgM_mul(L,Im_mdet), gen_1))) < 16)
3031 16 : { *ptL = NULL; return gc_NULL(av); }
3032 :
3033 57815 : *ptL = RgM_mul(rowslice(L,2,RU), Ar); /* approximate rational entries */
3034 57815 : return gc_all(av,2, &kR, ptL);
3035 : }
3036 :
3037 : /* leave small integer n as is, convert huge n to t_REAL (for readability) */
3038 : static GEN
3039 0 : i2print(GEN n)
3040 0 : { return lgefint(n) <= DEFAULTPREC? n: itor(n,LOWDEFAULTPREC); }
3041 :
3042 : static long
3043 73556 : bad_check(GEN c)
3044 : {
3045 73556 : long ec = gexpo(c);
3046 73556 : if (DEBUGLEVEL) err_printf("\n ***** check = %.28Pg\n",c);
3047 : /* safe check for c < 0.75 : avoid underflow in gtodouble() */
3048 73556 : if (ec < -1 || (ec == -1 && gtodouble(c) < 0.75)) return fupb_PRECI;
3049 : /* safe check for c > 1.3 : avoid overflow */
3050 73556 : if (ec > 0 || (ec == 0 && gtodouble(c) > 1.3)) return fupb_RELAT;
3051 63622 : return fupb_NONE;
3052 : }
3053 : /* Input:
3054 : * lambda = approximate rational entries: coords of units found so far on a
3055 : * sublattice of maximal rank (sublambda)
3056 : * *ptkR = regulator of sublambda = multiple of regulator of lambda
3057 : * Compute R = true regulator of lambda.
3058 : *
3059 : * If c := Rz ~ 1, by Dirichlet's formula, then lambda is the full group of
3060 : * units AND the full set of relations for the class group has been computed.
3061 : * In fact z is a very rough approximation and we only expect 0.75 < Rz < 1.3
3062 : *
3063 : * Output: *ptkR = R, *ptL = numerator(units) (in terms of lambda) */
3064 : static long
3065 73607 : compute_R(GEN lambda, GEN z, GEN *ptL, GEN *ptkR)
3066 : {
3067 73607 : pari_sp av = avma;
3068 73607 : long bit, r, reason, RU = lg(lambda) == 1? 1: lgcols(lambda);
3069 : GEN L, H, D, den, R, c;
3070 :
3071 73606 : *ptL = NULL;
3072 73606 : if (RU == 1) { *ptkR = gen_1; *ptL = lambda; return bad_check(z); }
3073 57760 : D = gmul2n(mpmul(*ptkR,z), 1); /* bound for denom(lambda) */
3074 57763 : if (expo(D) < 0 && rtodbl(D) < 0.95) return fupb_PRECI;
3075 57763 : L = bestappr(lambda,D);
3076 57761 : if (lg(L) == 1)
3077 : {
3078 0 : if (DEBUGLEVEL) err_printf("truncation error in bestappr\n");
3079 0 : return fupb_PRECI;
3080 : }
3081 57761 : den = Q_denom(L);
3082 57763 : if (mpcmp(den,D) > 0)
3083 : {
3084 20 : if (DEBUGLEVEL) err_printf("D = %Ps\nden = %Ps\n",D, i2print(den));
3085 20 : return fupb_PRECI;
3086 : }
3087 57743 : bit = -gexpo(gsub(L, lambda)); /* input accuracy */
3088 57743 : L = Q_muli_to_int(L, den);
3089 57741 : if (gexpo(L) + expi(den) > bit - 32)
3090 : {
3091 32 : if (DEBUGLEVEL) err_printf("dubious bestappr; den = %Ps\n", i2print(den));
3092 32 : return fupb_PRECI;
3093 : }
3094 57711 : H = ZM_hnf(L); r = lg(H)-1;
3095 57711 : if (!r || r != nbrows(H))
3096 0 : R = gen_0; /* wrong rank */
3097 : else
3098 57711 : R = gmul(*ptkR, gdiv(ZM_det_triangular(H), powiu(den, r)));
3099 : /* R = tentative regulator; regulator > 0.2 uniformly */
3100 57708 : if (gexpo(R) < -3) {
3101 0 : if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
3102 0 : return gc_long(av, fupb_PRECI);
3103 : }
3104 57711 : c = gmul(R,z); /* should be n (= 1 if we are done) */
3105 57710 : if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
3106 57710 : if ((reason = bad_check(c))) return gc_long(av, reason);
3107 48432 : *ptkR = R; *ptL = L; return fupb_NONE;
3108 : }
3109 : static GEN
3110 63718 : get_clg2(GEN cyc, GEN Ga, GEN C, GEN Ur, GEN Ge, GEN M1, GEN M2)
3111 : {
3112 63718 : GEN GD = gsub(act_arch(M1, C), diagact_arch(cyc, Ga));
3113 63720 : GEN ga = gsub(act_arch(M2, C), act_arch(Ur, Ga));
3114 63720 : return mkvecn(6, Ur, ga, GD, Ge, M1, M2);
3115 : }
3116 : /* compute class group (clg1) + data for isprincipal (clg2) */
3117 : static GEN
3118 63623 : class_group_gen(GEN nf,GEN W,GEN C,GEN Vbase,long prec, GEN *pclg2)
3119 : {
3120 : GEN M1, M2, z, G, Ga, Ge, cyc, X, Y, D, U, V, Ur, Ui, Uir;
3121 : long j, l;
3122 :
3123 63623 : D = ZM_snfall(W,&U,&V); /* UWV=D, D diagonal, G = g Ui (G=new gens, g=old) */
3124 63623 : Ui = ZM_inv(U, NULL);
3125 63623 : l = lg(D); cyc = cgetg(l, t_VEC); /* elementary divisors */
3126 92351 : for (j = 1; j < l; j++)
3127 : {
3128 30323 : gel(cyc,j) = gcoeff(D,j,j); /* strip useless components */
3129 30323 : if (is_pm1(gel(cyc,j))) break;
3130 : }
3131 63623 : l = j;
3132 63623 : Ur = ZM_hnfdivrem(U, D, &Y);
3133 63623 : Uir = ZM_hnfdivrem(Ui,W, &X);
3134 : /* {x} = logarithmic embedding of x (arch. component)
3135 : * NB: [J,z] = idealred(I) --> I = y J, with {y} = - z
3136 : * G = g Uir - {Ga}, Uir = Ui + WX
3137 : * g = G Ur - {ga}, Ur = U + DY */
3138 63623 : G = cgetg(l,t_VEC);
3139 63623 : Ga= cgetg(l,t_MAT);
3140 63623 : Ge= cgetg(l,t_COL);
3141 63623 : z = init_famat(NULL);
3142 92351 : for (j = 1; j < l; j++)
3143 : {
3144 28728 : GEN I = genback(z, nf, Vbase, gel(Uir,j));
3145 28728 : gel(G,j) = gel(I,1); /* generator, order cyc[j] */
3146 28728 : gel(Ge,j)= gel(I,2);
3147 28728 : gel(Ga,j)= nf_cxlog(nf, gel(I,2), prec);
3148 28728 : if (!gel(Ga,j)) pari_err_PREC("class_group_gen");
3149 : }
3150 : /* {ga} = {GD}Y + G U - g = {GD}Y - {Ga} U + gW X U
3151 : = gW (X Ur + V Y) - {Ga}Ur */
3152 63623 : M2 = ZM_add(ZM_mul(X,Ur), ZM_mul(V,Y));
3153 63623 : setlg(cyc,l); setlg(V,l); setlg(D,l);
3154 : /* G D =: {GD} = g (Ui + W X) D - {Ga}D = g W (V + X D) - {Ga}D
3155 : * NB: Ui D = W V. gW is given by (first l-1 cols of) C */
3156 63623 : M1 = ZM_add(V, ZM_mul(X,D));
3157 63621 : *pclg2 = get_clg2(cyc, Ga, C, Ur, Ge, M1, M2);
3158 63623 : return mkvec3(ZV_prod(cyc), cyc, G);
3159 : }
3160 :
3161 : /* compute principal ideals corresponding to (gen[i]^cyc[i]) */
3162 : static GEN
3163 4956 : makecycgen(GEN bnf)
3164 : {
3165 4956 : GEN cyc = bnf_get_cyc(bnf), gen = bnf_get_gen(bnf), nf = bnf_get_nf(bnf);
3166 4956 : GEN h, y, GD = bnf_get_GD(bnf), W = bnf_get_W(bnf); /* HNF */
3167 4956 : GEN Sunits = bnf_get_sunits(bnf);
3168 4956 : GEN X = Sunits? gel(Sunits,1): NULL, C = Sunits? gel(Sunits,3): NULL;
3169 : long e, i, l;
3170 :
3171 4956 : if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building cycgen)");
3172 4956 : h = cgetg_copy(gen, &l);
3173 11613 : for (i = 1; i < l; i++)
3174 : {
3175 6657 : GEN gi = gel(gen,i), ci = gel(cyc,i);
3176 6657 : if (X && equalii(ci, gcoeff(W,i,i)))
3177 : {
3178 : long j;
3179 8610 : for (j = i+1; j < l; j++)
3180 3241 : if (signe(gcoeff(W,i,j))) break;
3181 5543 : if (j == i) { gel(h,i) = mkmat2(X, gel(C,i)); continue; }
3182 : }
3183 6657 : if (abscmpiu(ci, 5) < 0)
3184 : {
3185 5544 : GEN N = ZM_det_triangular(gi);
3186 5544 : y = isprincipalarch(bnf,gel(GD,i), N, ci, gen_1, &e);
3187 5544 : if (y && fact_ok(nf,y,NULL,mkvec(gi),mkvec(ci)))
3188 : {
3189 4562 : gel(h,i) = to_famat_shallow(y,gen_1);
3190 4562 : continue;
3191 : }
3192 : }
3193 2095 : y = isprincipalfact(bnf, NULL, mkvec(gi), mkvec(ci), nf_GENMAT|nf_FORCE);
3194 2095 : gel(h,i) = gel(y,2);
3195 : }
3196 4956 : return h;
3197 : }
3198 :
3199 : static GEN
3200 69 : get_y(GEN bnf, GEN W, GEN B, GEN C, GEN pFB, long j)
3201 : {
3202 69 : GEN y, nf = bnf_get_nf(bnf);
3203 69 : long e, lW = lg(W)-1;
3204 69 : GEN ex = (j<=lW)? gel(W,j): gel(B,j-lW);
3205 69 : GEN P = (j<=lW)? NULL: gel(pFB,j);
3206 69 : if (C)
3207 : { /* archimedean embeddings known: cheap trial */
3208 69 : GEN Nx = get_norm_fact_primes(pFB, ex, P);
3209 69 : y = isprincipalarch(bnf,gel(C,j), Nx,gen_1, gen_1, &e);
3210 69 : if (y && fact_ok(nf,y,P,pFB,ex)) return y;
3211 : }
3212 0 : y = isprincipalfact_or_fail(bnf, P, pFB, ex);
3213 0 : return typ(y) == t_INT? y: gel(y,2);
3214 : }
3215 : /* compute principal ideals corresponding to bnf relations */
3216 : static GEN
3217 20 : makematal(GEN bnf)
3218 : {
3219 20 : GEN W = bnf_get_W(bnf), B = bnf_get_B(bnf), C = bnf_get_C(bnf);
3220 : GEN pFB, ma, retry;
3221 20 : long lma, j, prec = 0;
3222 :
3223 20 : if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building matal)");
3224 20 : lma=lg(W)+lg(B)-1;
3225 20 : pFB = bnf_get_vbase(bnf);
3226 20 : ma = cgetg(lma,t_VEC);
3227 20 : retry = vecsmalltrunc_init(lma);
3228 89 : for (j=lma-1; j>0; j--)
3229 : {
3230 69 : pari_sp av = avma;
3231 69 : GEN y = get_y(bnf, W, B, C, pFB, j);
3232 69 : if (typ(y) == t_INT)
3233 : {
3234 0 : long E = itos(y);
3235 0 : if (DEBUGLEVEL>1) err_printf("\n%ld done later at prec %ld\n",j,E);
3236 0 : set_avma(av);
3237 0 : vecsmalltrunc_append(retry, j);
3238 0 : if (E > prec) prec = E;
3239 : }
3240 : else
3241 : {
3242 69 : if (DEBUGLEVEL>1) err_printf("%ld ",j);
3243 69 : gel(ma,j) = gerepileupto(av,y);
3244 : }
3245 : }
3246 20 : if (prec)
3247 : {
3248 0 : long k, l = lg(retry);
3249 0 : GEN y, nf = bnf_get_nf(bnf);
3250 0 : if (DEBUGLEVEL) pari_warn(warnprec,"makematal",prec);
3251 0 : nf = nfnewprec_shallow(nf,prec);
3252 0 : bnf = Buchall(nf, nf_FORCE, prec);
3253 0 : if (DEBUGLEVEL) err_printf("makematal, adding missing entries:");
3254 0 : for (k=1; k<l; k++)
3255 : {
3256 0 : pari_sp av = avma;
3257 0 : long j = retry[k];
3258 0 : y = get_y(bnf,W,B,NULL, pFB, j);
3259 0 : if (typ(y) == t_INT) pari_err_PREC("makematal");
3260 0 : if (DEBUGLEVEL>1) err_printf("%ld ",j);
3261 0 : gel(ma,j) = gerepileupto(av,y);
3262 : }
3263 : }
3264 20 : if (DEBUGLEVEL>1) err_printf("\n");
3265 20 : return ma;
3266 : }
3267 :
3268 : enum { MATAL = 1, CYCGEN, UNITS };
3269 : GEN
3270 26724 : bnf_build_cycgen(GEN bnf)
3271 26724 : { return obj_checkbuild(bnf, CYCGEN, &makecycgen); }
3272 : GEN
3273 20 : bnf_build_matalpha(GEN bnf)
3274 20 : { return obj_checkbuild(bnf, MATAL, &makematal); }
3275 : GEN
3276 32048 : bnf_build_units(GEN bnf)
3277 32048 : { return obj_checkbuild(bnf, UNITS, &makeunits); }
3278 :
3279 : /* return fu in compact form if available; in terms of a fixed basis
3280 : * of S-units */
3281 : GEN
3282 70 : bnf_compactfu_mat(GEN bnf)
3283 : {
3284 70 : GEN X, U, SUnits = bnf_get_sunits(bnf);
3285 70 : if (!SUnits) return NULL;
3286 70 : X = gel(SUnits,1);
3287 70 : U = gel(SUnits,2); ZM_remove_unused(&U, &X);
3288 70 : return mkvec2(X, U);
3289 : }
3290 : /* return fu in compact form if available; individually as famat */
3291 : GEN
3292 37133 : bnf_compactfu(GEN bnf)
3293 : {
3294 37133 : GEN fu, X, U, SUnits = bnf_get_sunits(bnf);
3295 : long i, l;
3296 37133 : if (!SUnits) return NULL;
3297 36902 : X = gel(SUnits,1);
3298 36902 : U = gel(SUnits,2); l = lg(U); fu = cgetg(l, t_VEC);
3299 60191 : for (i = 1; i < l; i++)
3300 23289 : gel(fu,i) = famat_remove_trivial(mkmat2(X, gel(U,i)));
3301 36902 : return fu;
3302 : }
3303 : /* return expanded fu if available */
3304 : GEN
3305 263664 : bnf_has_fu(GEN bnf)
3306 : {
3307 263664 : GEN fu = obj_check(bnf, UNITS);
3308 263663 : if (fu) return vecsplice(fu, 1);
3309 262868 : fu = bnf_get_fu_nocheck(bnf);
3310 262867 : return (typ(fu) == t_MAT)? NULL: fu;
3311 : }
3312 : /* return expanded fu if available; build if cheap */
3313 : GEN
3314 263386 : bnf_build_cheapfu(GEN bnf)
3315 : {
3316 : GEN fu, SUnits;
3317 263386 : if ((fu = bnf_has_fu(bnf))) return fu;
3318 141 : if ((SUnits = bnf_get_sunits(bnf)))
3319 : {
3320 142 : pari_sp av = avma;
3321 142 : long e = gexpo(real_i(bnf_get_logfu(bnf)));
3322 142 : set_avma(av); if (e < 13) return vecsplice(bnf_build_units(bnf), 1);
3323 : }
3324 77 : return NULL;
3325 : }
3326 :
3327 : static GEN
3328 63719 : get_regulator(GEN A)
3329 : {
3330 63719 : pari_sp av = avma;
3331 : GEN R;
3332 :
3333 63719 : if (lg(A) == 1) return gen_1;
3334 48522 : R = det( rowslice(real_i(A), 1, lgcols(A)-2) );
3335 48522 : setabssign(R); return gerepileuptoleaf(av, R);
3336 : }
3337 :
3338 : /* return corrected archimedian components for elts of x (vector)
3339 : * (= log(sigma_i(x)) - log(|Nx|) / [K:Q]) */
3340 : static GEN
3341 40 : get_archclean(GEN nf, GEN x, long prec, int units)
3342 : {
3343 40 : long k, N, l = lg(x);
3344 40 : GEN M = cgetg(l, t_MAT);
3345 :
3346 40 : if (l == 1) return M;
3347 26 : N = nf_get_degree(nf);
3348 114 : for (k = 1; k < l; k++)
3349 : {
3350 88 : pari_sp av = avma;
3351 88 : GEN c = nf_cxlog(nf, gel(x,k), prec);
3352 88 : if (!c || (!units && !(c = cleanarch(c, N, NULL,prec)))) return NULL;
3353 88 : gel(M,k) = gerepilecopy(av, c);
3354 : }
3355 26 : return M;
3356 : }
3357 : static void
3358 77 : Sunits_archclean(GEN nf, GEN Sunits, GEN *pmun, GEN *pC, long prec)
3359 : {
3360 77 : GEN ipi, M, X = gel(Sunits,1), U = gel(Sunits,2), G = gel(Sunits,3);
3361 77 : long k, N = nf_get_degree(nf), l = lg(X);
3362 :
3363 77 : M = cgetg(l, t_MAT);
3364 3640 : for (k = 1; k < l; k++)
3365 3563 : if (!(gel(M,k) = nf_cxlog(nf, gel(X,k), prec))) return;
3366 77 : ipi = invr(mppi(prec));
3367 77 : *pmun = cleanarch(RgM_ZM_mul(M, U), N, ipi, prec); /* not cleanarchunit ! */
3368 77 : if (*pmun) *pC = cleanarch(RgM_ZM_mul(M, G), N, ipi, prec);
3369 : }
3370 :
3371 : GEN
3372 97 : bnfnewprec_shallow(GEN bnf, long prec)
3373 : {
3374 97 : GEN nf0 = bnf_get_nf(bnf), nf, v, fu, matal, y, A, C;
3375 97 : GEN Sunits = bnf_get_sunits(bnf), Ur, Ga, Ge, M1, M2;
3376 97 : long r1, r2, prec0 = prec;
3377 :
3378 97 : nf_get_sign(nf0, &r1, &r2);
3379 97 : if (Sunits)
3380 : {
3381 77 : fu = matal = NULL;
3382 77 : prec += nbits2extraprec(gexpo(Sunits));
3383 : }
3384 : else
3385 : {
3386 20 : fu = bnf_build_units(bnf);
3387 20 : fu = vecslice(fu, 2, lg(fu)-1);
3388 20 : if (r1 + r2 > 1) {
3389 13 : long e = gexpo(bnf_get_logfu(bnf)) + 1 - TWOPOTBITS_IN_LONG;
3390 13 : if (e >= 0) prec += nbits2extraprec(e);
3391 : }
3392 20 : matal = bnf_build_matalpha(bnf);
3393 : }
3394 :
3395 97 : if (DEBUGLEVEL && prec0 != prec) pari_warn(warnprec,"bnfnewprec",prec);
3396 97 : for(C = NULL;;)
3397 0 : {
3398 97 : pari_sp av = avma;
3399 97 : nf = nfnewprec_shallow(nf0,prec);
3400 97 : if (Sunits)
3401 77 : Sunits_archclean(nf, Sunits, &A, &C, prec);
3402 : else
3403 : {
3404 20 : A = get_archclean(nf, fu, prec, 1);
3405 20 : if (A) C = get_archclean(nf, matal, prec, 0);
3406 : }
3407 97 : if (C) break;
3408 0 : set_avma(av); prec = precdbl(prec);
3409 0 : if (DEBUGLEVEL) pari_warn(warnprec,"bnfnewprec(extra)",prec);
3410 : }
3411 97 : y = leafcopy(bnf);
3412 97 : gel(y,3) = A;
3413 97 : gel(y,4) = C;
3414 97 : gel(y,7) = nf;
3415 97 : gel(y,8) = v = leafcopy(gel(bnf,8));
3416 97 : gel(v,2) = get_regulator(A);
3417 97 : v = gel(bnf,9);
3418 97 : if (lg(v) < 7) pari_err_TYPE("bnfnewprec [obsolete bnf format]", bnf);
3419 97 : Ur = gel(v,1);
3420 97 : Ge = gel(v,4);
3421 97 : Ga = nfV_cxlog(nf, Ge, prec);
3422 97 : M1 = gel(v,5);
3423 97 : M2 = gel(v,6);
3424 97 : gel(y,9) = get_clg2(bnf_get_cyc(bnf), Ga, C, Ur, Ge, M1, M2);
3425 97 : return y;
3426 : }
3427 : GEN
3428 21 : bnfnewprec(GEN bnf, long prec)
3429 : {
3430 21 : pari_sp av = avma;
3431 21 : return gerepilecopy(av, bnfnewprec_shallow(checkbnf(bnf), prec));
3432 : }
3433 :
3434 : GEN
3435 0 : bnrnewprec_shallow(GEN bnr, long prec)
3436 : {
3437 0 : GEN y = cgetg(7,t_VEC);
3438 : long i;
3439 0 : gel(y,1) = bnfnewprec_shallow(bnr_get_bnf(bnr), prec);
3440 0 : for (i=2; i<7; i++) gel(y,i) = gel(bnr,i);
3441 0 : return y;
3442 : }
3443 : GEN
3444 7 : bnrnewprec(GEN bnr, long prec)
3445 : {
3446 7 : GEN y = cgetg(7,t_VEC);
3447 : long i;
3448 7 : checkbnr(bnr);
3449 7 : gel(y,1) = bnfnewprec(bnr_get_bnf(bnr), prec);
3450 42 : for (i=2; i<7; i++) gel(y,i) = gcopy(gel(bnr,i));
3451 7 : return y;
3452 : }
3453 :
3454 : static GEN
3455 64750 : buchall_end(GEN nf,GEN res, GEN clg2, GEN W, GEN B, GEN A, GEN C,GEN Vbase)
3456 : {
3457 64750 : GEN z = obj_init(9, 3);
3458 64750 : gel(z,1) = W;
3459 64750 : gel(z,2) = B;
3460 64750 : gel(z,3) = A;
3461 64750 : gel(z,4) = C;
3462 64750 : gel(z,5) = Vbase;
3463 64750 : gel(z,6) = gen_0;
3464 64750 : gel(z,7) = nf;
3465 64750 : gel(z,8) = res;
3466 64750 : gel(z,9) = clg2;
3467 64750 : return z;
3468 : }
3469 :
3470 : GEN
3471 2555 : bnfinit0(GEN P, long flag, GEN data, long prec)
3472 : {
3473 2555 : double c1 = 0., c2 = 0.;
3474 2555 : long fl, relpid = degpol(P)==2 ? 0: BNF_RELPID;
3475 :
3476 2555 : if (data)
3477 : {
3478 21 : long lx = lg(data);
3479 21 : if (typ(data) != t_VEC || lx > 5) pari_err_TYPE("bnfinit",data);
3480 21 : switch(lx)
3481 : {
3482 0 : case 4: relpid = itos(gel(data,3));
3483 14 : case 3: c2 = gtodouble(gel(data,2));
3484 21 : case 2: c1 = gtodouble(gel(data,1));
3485 : }
3486 : }
3487 2555 : switch(flag)
3488 : {
3489 1729 : case 2:
3490 1729 : case 0: fl = 0; break;
3491 826 : case 1: fl = nf_FORCE; break;
3492 0 : default: pari_err_FLAG("bnfinit");
3493 : return NULL; /* LCOV_EXCL_LINE */
3494 : }
3495 2555 : return Buchall_param(P, c1, c2, relpid, fl, prec);
3496 : }
3497 : GEN
3498 62191 : Buchall(GEN P, long flag, long prec)
3499 62191 : { return Buchall_param(P, 0., 0., BNF_RELPID, flag & nf_FORCE, prec); }
3500 :
3501 : static GEN
3502 1127 : Buchall_deg1(GEN nf)
3503 : {
3504 1127 : GEN v = cgetg(1,t_VEC), m = cgetg(1,t_MAT);
3505 1127 : GEN res, W, A, B, C, Vbase = cgetg(1,t_COL);
3506 1127 : GEN fu = v, R = gen_1, zu = mkvec2(gen_2, gen_m1);
3507 1127 : GEN clg1 = mkvec3(gen_1,v,v), clg2 = mkvecn(6, m,m,m,v,m,m);
3508 :
3509 1127 : W = A = B = C = m; res = mkvec5(clg1, R, gen_1, zu, fu);
3510 1127 : return buchall_end(nf,res,clg2,W,B,A,C,Vbase);
3511 : }
3512 :
3513 : /* return (small set of) indices of columns generating the same lattice as x.
3514 : * Assume HNF(x) is inexpensive (few rows, many columns).
3515 : * Dichotomy approach since interesting columns may be at the very end */
3516 : GEN
3517 63622 : extract_full_lattice(GEN x)
3518 : {
3519 63622 : long dj, j, k, l = lg(x);
3520 : GEN h, h2, H, v;
3521 :
3522 63622 : if (l < 200) return NULL; /* not worth it */
3523 :
3524 5 : v = vecsmalltrunc_init(l);
3525 5 : H = ZM_hnf(x);
3526 5 : h = cgetg(1, t_MAT);
3527 5 : dj = 1;
3528 215 : for (j = 1; j < l; )
3529 : {
3530 215 : pari_sp av = avma;
3531 215 : long lv = lg(v);
3532 :
3533 725 : for (k = 0; k < dj; k++) v[lv+k] = j+k;
3534 215 : setlg(v, lv + dj);
3535 215 : h2 = ZM_hnf(vecpermute(x, v));
3536 215 : if (ZM_equal(h, h2))
3537 : { /* these dj columns can be eliminated */
3538 85 : set_avma(av); setlg(v, lv);
3539 85 : j += dj;
3540 85 : if (j >= l) break;
3541 85 : dj <<= 1;
3542 85 : if (j + dj >= l) { dj = (l - j) >> 1; if (!dj) dj = 1; }
3543 : }
3544 130 : else if (dj > 1)
3545 : { /* at least one interesting column, try with first half of this set */
3546 85 : set_avma(av); setlg(v, lv);
3547 85 : dj >>= 1; /* > 0 */
3548 : }
3549 : else
3550 : { /* this column should be kept */
3551 45 : if (ZM_equal(h2, H)) break;
3552 40 : h = h2; j++;
3553 : }
3554 : }
3555 5 : return v;
3556 : }
3557 :
3558 : static void
3559 63661 : init_rel(RELCACHE_t *cache, FB_t *F, long add_need)
3560 : {
3561 63661 : const long n = F->KC + add_need; /* expected # of needed relations */
3562 : long i, j, k, p;
3563 : GEN c, P;
3564 : GEN R;
3565 :
3566 63661 : if (DEBUGLEVEL) err_printf("KCZ = %ld, KC = %ld, n = %ld\n", F->KCZ,F->KC,n);
3567 63661 : reallocate(cache, 10*n + 50); /* make room for lots of relations */
3568 63661 : cache->chk = cache->base;
3569 63661 : cache->end = cache->base + n;
3570 63661 : cache->relsup = add_need;
3571 63661 : cache->last = cache->base;
3572 63661 : cache->missing = lg(cache->basis) - 1;
3573 302856 : for (i = 1; i <= F->KCZ; i++)
3574 : { /* trivial relations (p) = prod P^e */
3575 239196 : p = F->FB[i]; P = gel(F->LV,p);
3576 239196 : if (!isclone(P)) continue;
3577 :
3578 : /* all prime divisors in FB */
3579 166788 : c = zero_Flv(F->KC); k = F->iLP[p];
3580 166787 : R = c; c += k;
3581 532524 : for (j = lg(P)-1; j; j--) c[j] = pr_get_e(gel(P,j));
3582 166787 : add_rel(cache, F, R, k+1, pr_get_p(gel(P,1)), 0);
3583 : }
3584 63660 : }
3585 :
3586 : /* Let z = \zeta_n in nf. List of not-obviously-dependent generators for
3587 : * cyclotomic units modulo torsion in Q(z) [independent when n a prime power]:
3588 : * - z^a - 1, n/(a,n) not a prime power, a \nmid n unless a=1, 1 <= a < n/2
3589 : * - (Z^a - 1)/(Z - 1), p^k || n, Z = z^{n/p^k}, (p,a) = 1, 1 < a <= (p^k-1)/2
3590 : */
3591 : GEN
3592 63661 : nfcyclotomicunits(GEN nf, GEN zu)
3593 : {
3594 63661 : long n = itos(gel(zu, 1)), n2, lP, i, a;
3595 : GEN z, fa, P, E, L, mz, powz;
3596 63661 : if (n <= 6) return cgetg(1, t_VEC);
3597 :
3598 1897 : z = algtobasis(nf,gel(zu, 2));
3599 1897 : if ((n & 3) == 2) { n = n >> 1; z = ZC_neg(z); } /* ensure n != 2 (mod 4) */
3600 1897 : n2 = n/2;
3601 1897 : mz = zk_multable(nf, z); /* multiplication by z */
3602 1897 : powz = cgetg(n2, t_VEC); gel(powz,1) = z;
3603 6237 : for (i = 2; i < n2; i++) gel(powz,i) = ZM_ZC_mul(mz, gel(powz,i-1));
3604 : /* powz[i] = z^i */
3605 :
3606 1897 : L = vectrunc_init(n);
3607 1897 : fa = factoru(n);
3608 1897 : P = gel(fa,1); lP = lg(P);
3609 1897 : E = gel(fa,2);
3610 4578 : for (i = 1; i < lP; i++)
3611 : { /* second kind */
3612 2681 : long p = P[i], k = E[i], pk = upowuu(p,k), pk2 = (pk-1) / 2;
3613 2681 : GEN u = gen_1;
3614 4935 : for (a = 2; a <= pk2; a++)
3615 : {
3616 2254 : u = nfadd(nf, u, gel(powz, (n/pk) * (a-1))); /* = (Z^a-1)/(Z-1) */
3617 2254 : if (a % p) vectrunc_append(L, u);
3618 : }
3619 : }
3620 6104 : if (lP > 2) for (a = 1; a < n2; a++)
3621 : { /* first kind, when n not a prime power */
3622 : ulong p;
3623 4207 : if (a > 1 && (n % a == 0 || uisprimepower(n/ugcd(a,n), &p))) continue;
3624 1848 : vectrunc_append(L, nfadd(nf, gel(powz, a), gen_m1));
3625 : }
3626 1897 : return L;
3627 : }
3628 : static void
3629 63661 : add_cyclotomic_units(GEN nf, GEN zu, RELCACHE_t *cache, FB_t *F)
3630 : {
3631 63661 : pari_sp av = avma;
3632 63661 : GEN L = nfcyclotomicunits(nf, zu);
3633 63661 : long i, l = lg(L);
3634 63661 : if (l > 1)
3635 : {
3636 1897 : GEN R = zero_Flv(F->KC);
3637 5901 : for(i = 1; i < l; i++) add_rel(cache, F, R, F->KC+1, gel(L,i), 0);
3638 : }
3639 63661 : set_avma(av);
3640 63661 : }
3641 :
3642 : static GEN
3643 106471 : trim_list(FB_t *F)
3644 : {
3645 106471 : pari_sp av = avma;
3646 106471 : GEN v, L_jid = F->L_jid, minidx = F->minidx, present = zero_Flv(F->KC);
3647 106470 : long i, j, imax = minss(lg(L_jid), F->KC + 1);
3648 :
3649 106470 : v = cgetg(imax, t_VECSMALL);
3650 1252074 : for (i = j = 1; i < imax; i++)
3651 : {
3652 1145603 : long k = minidx[ L_jid[i] ];
3653 1145603 : if (!present[k]) { v[j++] = L_jid[i]; present[k] = 1; }
3654 : }
3655 106471 : setlg(v, j); return gerepileuptoleaf(av, v);
3656 : }
3657 :
3658 : static void
3659 8288 : try_elt(RELCACHE_t *cache, FB_t *F, GEN nf, GEN x, FACT *fact)
3660 : {
3661 8288 : pari_sp av = avma;
3662 : GEN R, Nx;
3663 8288 : long nz, tx = typ(x);
3664 :
3665 8288 : if (tx == t_INT || tx == t_FRAC) return;
3666 8154 : if (tx != t_COL) x = algtobasis(nf, x);
3667 8154 : if (RgV_isscalar(x)) return;
3668 8154 : x = Q_primpart(x);
3669 8154 : Nx = nfnorm(nf, x);
3670 8154 : if (!can_factor(F, nf, NULL, x, Nx, fact)) return;
3671 :
3672 : /* smooth element */
3673 8154 : R = set_fact(F, fact, NULL, &nz);
3674 : /* make sure we get maximal rank first, then allow all relations */
3675 8154 : (void) add_rel(cache, F, R, nz, x, 0);
3676 8154 : set_avma(av);
3677 : }
3678 :
3679 : static void
3680 38499 : matenlarge(GEN C, long h)
3681 : {
3682 38499 : GEN _0 = zerocol(h);
3683 : long i;
3684 2915105 : for (i = lg(C); --i; ) gel(C,i) = shallowconcat(gel(C,i), _0);
3685 38499 : }
3686 :
3687 : /* E = floating point embeddings */
3688 : static GEN
3689 38498 : matbotidembs(RELCACHE_t *cache, GEN E)
3690 : {
3691 38498 : long w = cache->last - cache->chk, h = cache->last - cache->base;
3692 38498 : long j, d = h - w, hE = nbrows(E);
3693 38498 : GEN y = cgetg(w+1,t_MAT), _0 = zerocol(h);
3694 155193 : for (j = 1; j <= w; j++)
3695 : {
3696 116694 : GEN c = shallowconcat(gel(E,j), _0);
3697 116694 : if (d + j >= 1) gel(c, d + j + hE) = gen_1;
3698 116694 : gel(y,j) = c;
3699 : }
3700 38499 : return y;
3701 : }
3702 : static GEN
3703 62093 : matbotid(RELCACHE_t *cache)
3704 : {
3705 62093 : long w = cache->last - cache->chk, h = cache->last - cache->base;
3706 62093 : long j, d = h - w;
3707 62093 : GEN y = cgetg(w+1,t_MAT);
3708 897993 : for (j = 1; j <= w; j++)
3709 : {
3710 835901 : GEN c = zerocol(h);
3711 835900 : if (d + j >= 1) gel(c, d + j) = gen_1;
3712 835900 : gel(y,j) = c;
3713 : }
3714 62092 : return y;
3715 : }
3716 :
3717 : static long
3718 75 : myprecdbl(long prec, GEN C)
3719 : {
3720 75 : long p = prec2nbits(prec) < 1280? precdbl(prec): (long)(prec * 1.5);
3721 75 : if (C) p = maxss(p, minss(3*p, prec + nbits2extraprec(gexpo(C))));
3722 75 : return p;
3723 : }
3724 :
3725 : static GEN
3726 57505 : _nfnewprec(GEN nf, long prec, long *isclone)
3727 : {
3728 57505 : GEN NF = gclone(nfnewprec_shallow(nf, prec));
3729 57505 : if (*isclone) gunclone(nf);
3730 57505 : *isclone = 1; return NF;
3731 : }
3732 :
3733 : /* Nrelid = nb relations per ideal, possibly 0. If flag is set, keep data in
3734 : * algebraic form. */
3735 : GEN
3736 64746 : Buchall_param(GEN P, double cbach, double cbach2, long Nrelid, long flag, long prec)
3737 : {
3738 : pari_timer T;
3739 64746 : pari_sp av0 = avma, av, av2;
3740 : long PREC, N, R1, R2, RU, low, high, LIMC0, LIMC, LIMC2, LIMCMAX, zc, i;
3741 64746 : long LIMres, bit = 0, flag_nfinit = 0;
3742 64746 : long nreldep, sfb_trials, need, old_need, precdouble = 0, TRIES = 0;
3743 64746 : long nfisclone = 0;
3744 : long done_small, small_fail, fail_limit, squash_index, small_norm_prec;
3745 : double LOGD, LOGD2, lim;
3746 64746 : GEN computed = NULL, fu = NULL, zu, nf, M_sn, D, A, W, R, h, Ce, PERM;
3747 : GEN small_multiplier, auts, cyclic, embs, SUnits;
3748 : GEN res, L, invhr, B, C, lambda, dep, clg1, clg2, Vbase;
3749 64746 : const char *precpb = NULL;
3750 64746 : REL_t *old_cache = NULL;
3751 : nfmaxord_t nfT;
3752 : RELCACHE_t cache;
3753 : FB_t F;
3754 : GRHcheck_t GRHcheck;
3755 : FACT *fact;
3756 :
3757 64746 : if (DEBUGLEVEL) timer_start(&T);
3758 64746 : P = get_nfpol(P, &nf);
3759 64736 : if (nf)
3760 3556 : D = nf_get_disc(nf);
3761 : else
3762 : {
3763 61180 : nfinit_basic(&nfT, P);
3764 61191 : D = nfT.dK;
3765 61191 : if (!ZX_is_monic(nfT.T0))
3766 : {
3767 14 : pari_warn(warner,"nonmonic polynomial in bnfinit, using polredbest");
3768 14 : flag_nfinit = nf_RED;
3769 : }
3770 : }
3771 64747 : PREC = maxss(DEFAULTPREC, prec);
3772 64747 : N = degpol(P);
3773 64747 : if (N <= 1)
3774 : {
3775 1127 : if (!nf) nf = nfinit_complete(&nfT, flag_nfinit, PREC);
3776 1127 : return gerepilecopy(av0, Buchall_deg1(nf));
3777 : }
3778 63620 : D = absi_shallow(D);
3779 63619 : LOGD = dbllog2(D) * M_LN2;
3780 63619 : LOGD2 = LOGD*LOGD;
3781 63619 : LIMCMAX = (long)(4.*LOGD2);
3782 : /* In small_norm, LLL reduction produces v0 in I such that
3783 : * T2(v0) <= (4/3)^((n-1)/2) NI^(2/n) disc(K)^(1/n)
3784 : * We consider v with T2(v) <= BMULT * T2(v0)
3785 : * Hence Nv <= ((4/3)^((n-1)/2) * BMULT / n)^(n/2) NI sqrt(disc(K)).
3786 : * NI <= LIMCMAX^2 */
3787 63619 : if (nf) PREC = maxss(PREC, nf_get_prec(nf));
3788 63619 : PREC = maxss(PREC, nbits2prec((long)(LOGD2 * 0.02) + N*N));
3789 63620 : if (DEBUGLEVEL) err_printf("PREC = %ld\n", PREC);
3790 63620 : small_norm_prec = nbits2prec( BITS_IN_LONG +
3791 63620 : (N/2. * ((N-1)/2.*log(4./3) + log(8/(double)N))
3792 63620 : + 2*log((double) LIMCMAX) + LOGD/2) / M_LN2 ); /*enough to compute norms*/
3793 63621 : if (small_norm_prec > PREC) PREC = small_norm_prec;
3794 63621 : if (!nf)
3795 60240 : nf = nfinit_complete(&nfT, flag_nfinit, PREC);
3796 3381 : else if (nf_get_prec(nf) < PREC)
3797 192 : nf = nfnewprec_shallow(nf, PREC);
3798 63618 : M_sn = nf_get_M(nf);
3799 63618 : if (PREC > small_norm_prec) M_sn = gprec_w(M_sn, small_norm_prec);
3800 :
3801 63618 : zu = nfrootsof1(nf);
3802 63617 : gel(zu,2) = nf_to_scalar_or_alg(nf, gel(zu,2));
3803 :
3804 63618 : nf_get_sign(nf, &R1, &R2); RU = R1+R2;
3805 63621 : auts = automorphism_matrices(nf, &cyclic);
3806 63621 : F.embperm = automorphism_perms(nf_get_M(nf), auts, cyclic, R1, R2, N);
3807 63618 : if (DEBUGLEVEL)
3808 : {
3809 0 : timer_printf(&T, "nfinit & nfrootsof1");
3810 0 : err_printf("%s bnf: R1 = %ld, R2 = %ld\nD = %Ps\n",
3811 : flag? "Algebraic": "Floating point", R1,R2, D);
3812 : }
3813 63618 : if (LOGD < 20.)
3814 : { /* tiny disc, Minkowski may be smaller than Bach */
3815 62176 : lim = exp(-N + R2 * log(4/M_PI) + LOGD/2) * sqrt(2*M_PI*N);
3816 62176 : if (lim < 3) lim = 3;
3817 : }
3818 : else /* to be ignored */
3819 1442 : lim = -1;
3820 63618 : if (cbach > 12.) {
3821 0 : if (cbach2 < cbach) cbach2 = cbach;
3822 0 : cbach = 12.;
3823 : }
3824 63618 : if (cbach < 0.)
3825 0 : pari_err_DOMAIN("Buchall","Bach constant","<",gen_0,dbltor(cbach));
3826 :
3827 63618 : cache.base = NULL; F.subFB = NULL; F.LP = NULL; SUnits = Ce = NULL;
3828 63618 : init_GRHcheck(&GRHcheck, N, R1, LOGD);
3829 63620 : high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
3830 310244 : while (!GRHchk(nf, &GRHcheck, high)) { low = high; high *= 2; }
3831 246673 : while (high - low > 1)
3832 : {
3833 183049 : long test = (low+high)/2;
3834 183049 : if (GRHchk(nf, &GRHcheck, test)) high = test; else low = test;
3835 : }
3836 63624 : LIMC2 = (high == LIMC0+1 && GRHchk(nf, &GRHcheck, LIMC0))? LIMC0: high;
3837 63624 : if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
3838 : /* Assuming GRH, {P, NP <= LIMC2} generate Cl(K) */
3839 63624 : if (DEBUGLEVEL) err_printf("LIMC2 = %ld\n", LIMC2);
3840 63623 : LIMC0 = (long)(cbach*LOGD2); /* initial value for LIMC */
3841 63623 : LIMC = cbach? LIMC0: LIMC2; /* use {P, NP <= LIMC} as a factorbase */
3842 63623 : LIMC = maxss(LIMC, nthideal(&GRHcheck, nf, N));
3843 63623 : if (DEBUGLEVEL) timer_printf(&T, "computing Bach constant");
3844 63623 : LIMres = primeneeded(N, R1, R2, LOGD);
3845 63623 : cache_prime_dec(&GRHcheck, LIMres, nf);
3846 : /* invhr ~ 2^r1 (2pi)^r2 / sqrt(D) w * Res(zeta_K, s=1) = 1 / hR */
3847 127242 : invhr = gmul(gdiv(gmul2n(powru(mppi(DEFAULTPREC), R2), RU),
3848 63621 : mulri(gsqrt(D,DEFAULTPREC),gel(zu,1))),
3849 : compute_invres(&GRHcheck, LIMres));
3850 63622 : if (DEBUGLEVEL) timer_printf(&T, "computing inverse of hR");
3851 63622 : av = avma;
3852 :
3853 65837 : START:
3854 65837 : if (DEBUGLEVEL) timer_start(&T);
3855 65837 : if (TRIES) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
3856 65837 : if (DEBUGLEVEL && LIMC > LIMC0)
3857 0 : err_printf("%s*** Bach constant: %f\n", TRIES?"\n":"", LIMC/LOGD2);
3858 65837 : if (cache.base)
3859 : {
3860 : REL_t *rel;
3861 21994 : for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
3862 21956 : if (rel->m) i++;
3863 38 : computed = cgetg(i, t_VEC);
3864 21994 : for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
3865 21956 : if (rel->m) gel(computed, i++) = rel->m;
3866 38 : computed = gclone(computed); delete_cache(&cache);
3867 : }
3868 65837 : TRIES++; set_avma(av);
3869 65837 : if (F.LP) delete_FB(&F);
3870 65837 : if (LIMC2 < LIMC) LIMC2 = LIMC;
3871 65837 : if (DEBUGLEVEL) { err_printf("LIMC = %ld, LIMC2 = %ld\n",LIMC,LIMC2); }
3872 :
3873 65837 : FBgen(&F, nf, N, LIMC, LIMC2, &GRHcheck);
3874 65835 : if (!F.KC) goto START;
3875 65835 : av = avma;
3876 65835 : subFBgen(&F,auts,cyclic,lim < 0? LIMC2: mindd(lim,LIMC2),MINSFB);
3877 65838 : if (lg(F.subFB) == 1) goto START;
3878 63661 : if (DEBUGLEVEL)
3879 0 : timer_printf(&T, "factorbase (#subFB = %ld) and ideal permutations",
3880 0 : lg(F.subFB)-1);
3881 :
3882 63661 : fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
3883 63659 : PERM = leafcopy(F.perm); /* to be restored in case of precision increase */
3884 63660 : cache.basis = zero_Flm_copy(F.KC,F.KC);
3885 63660 : small_multiplier = zero_Flv(F.KC);
3886 63660 : done_small = small_fail = squash_index = zc = sfb_trials = nreldep = 0;
3887 63660 : fail_limit = F.KC + 1;
3888 63660 : W = A = R = NULL;
3889 63660 : av2 = avma;
3890 63660 : init_rel(&cache, &F, RELSUP + RU-1);
3891 63661 : old_need = need = cache.end - cache.last;
3892 63661 : add_cyclotomic_units(nf, zu, &cache, &F);
3893 63661 : if (DEBUGLEVEL) err_printf("\n");
3894 63661 : cache.end = cache.last + need;
3895 :
3896 63661 : if (computed)
3897 : {
3898 8326 : for (i = 1; i < lg(computed); i++)
3899 8288 : try_elt(&cache, &F, nf, gel(computed, i), fact);
3900 38 : gunclone(computed);
3901 38 : if (DEBUGLEVEL && i > 1)
3902 0 : timer_printf(&T, "including already computed relations");
3903 38 : need = 0;
3904 : }
3905 :
3906 : do
3907 : {
3908 : GEN Ar, C0;
3909 : do
3910 : {
3911 106584 : pari_sp av4 = avma;
3912 106584 : if (need > 0)
3913 : {
3914 106471 : long oneed = cache.end - cache.last;
3915 : /* Test below can be true if small_norm did not find enough linearly
3916 : * dependent relations */
3917 106471 : if (need < oneed) need = oneed;
3918 106471 : pre_allocate(&cache, need+lg(auts)-1+(R ? lg(W)-1 : 0));
3919 106471 : cache.end = cache.last + need;
3920 106471 : F.L_jid = trim_list(&F);
3921 : }
3922 106584 : if (need > 0 && Nrelid > 0 && (done_small <= F.KC+1 || A) &&
3923 103374 : small_fail <= fail_limit &&
3924 103374 : cache.last < cache.base + 2*F.KC+2*RU+RELSUP /* heuristic */)
3925 : {
3926 89741 : long j, k, LIE = (R && lg(W) > 1 && (done_small % 2));
3927 89741 : REL_t *last = cache.last;
3928 89741 : pari_sp av3 = avma;
3929 : GEN p0;
3930 89741 : if (LIE)
3931 : { /* We have full rank for class group and unit. The following tries to
3932 : * improve the prime group lattice by looking for relations involving
3933 : * the primes generating the class group. */
3934 3291 : long n = lg(W)-1; /* need n relations to squash the class group */
3935 3291 : F.L_jid = vecslice(F.perm, 1, n);
3936 3291 : cache.end = cache.last + n;
3937 : /* Lie to the add_rel subsystem: pretend we miss relations involving
3938 : * the primes generating the class group (and only those). */
3939 3291 : cache.missing = n;
3940 10309 : for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 0;
3941 : }
3942 89741 : j = done_small % (F.KC+1);
3943 89741 : if (j == 0) p0 = NULL;
3944 : else
3945 : {
3946 27004 : p0 = gel(F.LP, j);
3947 27004 : if (!A)
3948 : { /* Prevent considering both P_iP_j and P_jP_i in small_norm */
3949 : /* Not all elements end up in F.L_jid (eliminated by hnfspec/add or
3950 : * by trim_list): keep track of which ideals are being considered
3951 : * at each run. */
3952 421 : long mj = small_multiplier[j];
3953 7066 : for (i = k = 1; i < lg(F.L_jid); i++)
3954 6645 : if (F.L_jid[i] > mj)
3955 : {
3956 6645 : small_multiplier[F.L_jid[i]] = j;
3957 6645 : F.L_jid[k++] = F.L_jid[i];
3958 : }
3959 421 : setlg(F.L_jid, k);
3960 : }
3961 : }
3962 89741 : if (lg(F.L_jid) > 1)
3963 89741 : small_norm(&cache, &F, nf, Nrelid, M_sn, fact, p0);
3964 89741 : F.L_jid = F.perm; set_avma(av3);
3965 89741 : if (!A && cache.last != last) small_fail = 0; else small_fail++;
3966 89741 : if (LIE)
3967 : { /* restore add_rel subsystem: undo above lie */
3968 3291 : long n = lg(W) - 1;
3969 10309 : for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 1;
3970 3291 : cache.missing = 0;
3971 : }
3972 89741 : cache.end = cache.last;
3973 89741 : done_small++;
3974 89741 : need = F.sfb_chg = 0;
3975 : }
3976 106584 : if (need > 0)
3977 : { /* Random relations */
3978 16730 : if (++nreldep > F.MAXDEPSIZESFB) {
3979 234 : if (++sfb_trials > SFB_MAX && LIMC < LIMCMAX/2) goto START;
3980 201 : F.sfb_chg = sfb_INCREASE;
3981 201 : nreldep = 0;
3982 : }
3983 16496 : else if (!(nreldep % F.MAXDEPSFB))
3984 5048 : F.sfb_chg = sfb_CHANGE;
3985 16697 : if (F.sfb_chg && !subFB_change(&F)) goto START;
3986 16692 : rnd_rel(&cache, &F, nf, fact);
3987 16692 : F.L_jid = F.perm;
3988 : }
3989 106546 : if (DEBUGLEVEL) timer_start(&T);
3990 106546 : if (precpb)
3991 : {
3992 : REL_t *rel;
3993 75 : if (DEBUGLEVEL)
3994 : {
3995 0 : char str[64]; sprintf(str,"Buchall_param (%s)",precpb);
3996 0 : pari_warn(warnprec,str,PREC);
3997 : }
3998 75 : nf = _nfnewprec(nf, PREC, &nfisclone);
3999 75 : precdouble++; precpb = NULL;
4000 :
4001 75 : if (flag)
4002 : { /* recompute embs only, no need to redo HNF */
4003 33 : long j, le = lg(embs), lC = lg(C);
4004 33 : GEN E, M = nf_get_M(nf);
4005 33 : set_avma(av4);
4006 11648 : for (rel = cache.base+1, i = 1; i < le; i++,rel++)
4007 11615 : gel(embs,i) = rel_embed(rel, &F, embs, i, M, RU, R1, PREC);
4008 33 : E = RgM_ZM_mul(embs, rowslice(C, RU+1, nbrows(C)));
4009 11648 : for (j = 1; j < lC; j++)
4010 61603 : for (i = 1; i <= RU; i++) gcoeff(C,i,j) = gcoeff(E,i,j);
4011 33 : av4 = avma;
4012 : }
4013 : else
4014 : { /* recompute embs + HNF */
4015 10318 : for(i = 1; i < lg(PERM); i++) F.perm[i] = PERM[i];
4016 42 : cache.chk = cache.base;
4017 42 : W = NULL;
4018 : }
4019 75 : if (DEBUGLEVEL) timer_printf(&T, "increasing accuracy");
4020 : }
4021 106546 : set_avma(av4);
4022 106546 : if (cache.chk != cache.last)
4023 : { /* Reduce relation matrices */
4024 106442 : long l = cache.last - cache.chk + 1, j;
4025 106442 : GEN mat = cgetg(l, t_MAT);
4026 : REL_t *rel;
4027 :
4028 1117388 : for (j=1,rel = cache.chk + 1; j < l; rel++,j++) gel(mat,j) = rel->R;
4029 106441 : if (!flag || W)
4030 : {
4031 44348 : embs = get_embs(&F, &cache, nf, embs, PREC);
4032 44349 : if (DEBUGLEVEL && timer_get(&T) > 1)
4033 0 : timer_printf(&T, "floating point embeddings");
4034 : }
4035 106442 : if (!W)
4036 : { /* never reduced before */
4037 63703 : C = flag? matbotid(&cache): embs;
4038 63703 : W = hnfspec_i(mat, F.perm, &dep, &B, &C, F.subFB ? lg(F.subFB)-1:0);
4039 63703 : if (DEBUGLEVEL)
4040 0 : timer_printf(&T, "hnfspec [%ld x %ld]", lg(F.perm)-1, l-1);
4041 63703 : if (flag)
4042 : {
4043 62093 : PREC += nbits2extraprec(gexpo(C));
4044 62093 : if (nf_get_prec(nf) < PREC) nf = _nfnewprec(nf, PREC, &nfisclone);
4045 62093 : embs = get_embs(&F, &cache, nf, embs, PREC);
4046 62093 : C = vconcat(RgM_ZM_mul(embs, C), C);
4047 : }
4048 63703 : if (DEBUGLEVEL)
4049 0 : timer_printf(&T, "hnfspec floating points");
4050 : }
4051 : else
4052 : {
4053 42739 : long k = lg(embs);
4054 42739 : GEN E = vecslice(embs, k-l+1,k-1);
4055 42738 : if (flag)
4056 : {
4057 38498 : E = matbotidembs(&cache, E);
4058 38499 : matenlarge(C, cache.last - cache.chk);
4059 : }
4060 42739 : W = hnfadd_i(W, F.perm, &dep, &B, &C, mat, E);
4061 42739 : if (DEBUGLEVEL)
4062 0 : timer_printf(&T, "hnfadd (%ld + %ld)", l-1, lg(dep)-1);
4063 : }
4064 106442 : gerepileall(av2, 5, &W,&C,&B,&dep,&embs);
4065 106442 : cache.chk = cache.last;
4066 : }
4067 104 : else if (!W)
4068 : {
4069 0 : need = old_need;
4070 0 : F.L_jid = vecslice(F.perm, 1, need);
4071 0 : continue;
4072 : }
4073 106546 : need = F.KC - (lg(W)-1) - (lg(B)-1);
4074 106546 : if (!need && cache.missing)
4075 : { /* The test above will never be true except if 27449|class number.
4076 : * Ensure that if we have maximal rank for the ideal lattice, then
4077 : * cache.missing == 0. */
4078 14 : for (i = 1; cache.missing; i++)
4079 7 : if (!mael(cache.basis, i, i))
4080 : {
4081 : long j;
4082 7 : cache.missing--; mael(cache.basis, i, i) = 1;
4083 427 : for (j = i+1; j <= F.KC; j++) mael(cache.basis, j, i) = 0;
4084 : }
4085 : }
4086 106546 : zc = (lg(C)-1) - (lg(B)-1) - (lg(W)-1);
4087 106546 : if (RU-1-zc > 0) need = minss(need + RU-1-zc, F.KC); /* for units */
4088 106546 : if (need)
4089 : { /* dependent rows */
4090 556 : F.L_jid = vecslice(F.perm, 1, need);
4091 556 : vecsmall_sort(F.L_jid);
4092 556 : if (need != old_need) { nreldep = 0; old_need = need; }
4093 : }
4094 : else
4095 : { /* If the relation lattice is too small, check will be > 1 and we will
4096 : * do a new run of small_norm/rnd_rel asking for 1 relation. This often
4097 : * gives a relation involving L_jid[1]. We rotate the first element of
4098 : * L_jid in order to increase the probability of finding relations that
4099 : * increases the lattice. */
4100 105990 : long j, n = lg(W) - 1;
4101 105990 : if (n > 1 && squash_index % n)
4102 : {
4103 7086 : F.L_jid = leafcopy(F.perm);
4104 30395 : for (j = 1; j <= n; j++)
4105 23309 : F.L_jid[j] = F.perm[1 + (j + squash_index - 1) % n];
4106 : }
4107 : else
4108 98904 : F.L_jid = F.perm;
4109 105990 : squash_index++;
4110 : }
4111 : }
4112 106546 : while (need);
4113 :
4114 105990 : if (!A)
4115 : {
4116 63661 : small_fail = old_need = 0;
4117 63661 : fail_limit = maxss(F.KC / FAIL_DIVISOR, MINFAIL);
4118 : }
4119 105990 : A = vecslice(C, 1, zc); /* cols corresponding to units */
4120 105990 : if (flag) A = rowslice(A, 1, RU);
4121 105990 : Ar = real_i(A);
4122 105989 : R = compute_multiple_of_R(Ar, RU, N, &need, &bit, &lambda);
4123 105990 : if (need < old_need) small_fail = 0;
4124 : #if 0 /* A good idea if we are indeed stuck but needs tuning */
4125 : /* we have computed way more relations than should be necessary */
4126 : if (TRIES < 3 && LIMC < LIMCMAX / 8 &&
4127 : cache.last - cache.base > 10 * F.KC) goto START;
4128 : #endif
4129 105990 : old_need = need;
4130 105990 : if (!lambda)
4131 23 : { precpb = "bestappr"; PREC = myprecdbl(PREC, flag? C: NULL); continue; }
4132 105967 : if (!R)
4133 : { /* not full rank for units */
4134 32306 : if (!need)
4135 0 : { precpb = "regulator"; PREC = myprecdbl(PREC, flag? C: NULL); }
4136 32306 : continue;
4137 : }
4138 73661 : if (cache.last==old_cache) { need=1; continue; }
4139 73609 : old_cache = cache.last;
4140 73609 : h = ZM_det_triangular(W);
4141 73608 : if (DEBUGLEVEL) err_printf("\n#### Tentative class number: %Ps\n", h);
4142 73608 : i = compute_R(lambda, mulir(h,invhr), &L, &R);
4143 73608 : if (DEBUGLEVEL)
4144 : {
4145 0 : err_printf("\n");
4146 0 : timer_printf(&T, "computing regulator and check");
4147 : }
4148 73608 : switch(i)
4149 : {
4150 9934 : case fupb_RELAT:
4151 9934 : need = 1; /* not enough relations */
4152 9934 : continue;
4153 52 : case fupb_PRECI: /* prec problem unless we cheat on Bach constant */
4154 52 : if ((precdouble&7) == 7 && LIMC <= LIMCMAX/2) goto START;
4155 52 : precpb = "compute_R"; PREC = myprecdbl(PREC, flag? C: NULL);
4156 52 : continue;
4157 : }
4158 : /* DONE */
4159 :
4160 63622 : if (F.KCZ2 > F.KCZ)
4161 : {
4162 7 : if (F.sfb_chg && !subFB_change(&F)) goto START;
4163 7 : if (!be_honest(&F, nf, auts, fact)) goto START;
4164 7 : if (DEBUGLEVEL) timer_printf(&T, "to be honest");
4165 : }
4166 63622 : F.KCZ2 = 0; /* be honest only once */
4167 :
4168 : /* fundamental units */
4169 : {
4170 63622 : GEN AU, CU, U, v = extract_full_lattice(L); /* L may be large */
4171 63622 : CU = NULL;
4172 63622 : if (v) { A = vecpermute(A, v); L = vecpermute(L, v); }
4173 : /* arch. components of fund. units */
4174 63622 : U = ZM_lll(L, 0.99, LLL_IM);
4175 63622 : U = ZM_mul(U, lll(RgM_ZM_mul(real_i(A), U)));
4176 63623 : if (DEBUGLEVEL) timer_printf(&T, "units LLL");
4177 63623 : AU = RgM_ZM_mul(A, U);
4178 63623 : A = cleanarchunit(AU, N, NULL, PREC);
4179 63622 : if (!A || lg(A) < RU || expo(gsub(get_regulator(A), R)) > -1)
4180 : {
4181 0 : long add = nbits2extraprec( gexpo(AU) + 64 ) - gprecision(AU);
4182 0 : long t = maxss((PREC-2) * 0.15, add);
4183 0 : if (!A && DEBUGLEVEL) err_printf("### Incorrect units lognorm");
4184 0 : precpb = "cleanarch"; PREC += maxss(t, EXTRAPREC64); continue;
4185 : }
4186 63621 : if (flag)
4187 : {
4188 62060 : long l = lgcols(C) - RU;
4189 : REL_t *rel;
4190 62060 : SUnits = cgetg(l, t_COL);
4191 998912 : for (rel = cache.base+1, i = 1; i < l; i++,rel++)
4192 936850 : set_rel_alpha(rel, auts, SUnits, i);
4193 62062 : if (RU > 1)
4194 : {
4195 47390 : GEN c = v? vecpermute(C,v): vecslice(C,1,zc);
4196 47390 : CU = ZM_mul(rowslice(c, RU+1, nbrows(c)), U);
4197 : }
4198 : }
4199 63622 : if (DEBUGLEVEL) err_printf("\n#### Computing fundamental units\n");
4200 63622 : fu = getfu(nf, &A, CU? &U: NULL, PREC);
4201 63623 : CU = CU? ZM_mul(CU, U): cgetg(1, t_MAT);
4202 63622 : if (DEBUGLEVEL) timer_printf(&T, "getfu");
4203 63622 : Ce = vecslice(C, zc+1, lg(C)-1);
4204 63623 : if (flag) SUnits = mkvec4(SUnits, CU, rowslice(Ce, RU+1, nbrows(Ce)),
4205 : utoipos(LIMC));
4206 : }
4207 : /* class group generators */
4208 63623 : if (flag) Ce = rowslice(Ce, 1, RU);
4209 63623 : C0 = Ce; Ce = cleanarch(Ce, N, NULL, PREC);
4210 63623 : if (!Ce) {
4211 0 : long add = nbits2extraprec( gexpo(C0) + 64 ) - gprecision(C0);
4212 0 : precpb = "cleanarch"; PREC += maxss(add, 1);
4213 : }
4214 63623 : if (DEBUGLEVEL) timer_printf(&T, "cleanarch");
4215 105990 : } while (need || precpb);
4216 :
4217 63623 : Vbase = vecpermute(F.LP, F.perm);
4218 63623 : if (!fu) fu = cgetg(1, t_MAT);
4219 63623 : if (!SUnits) SUnits = gen_1;
4220 63623 : clg1 = class_group_gen(nf,W,Ce,Vbase,PREC, &clg2);
4221 63623 : res = mkvec5(clg1, R, SUnits, zu, fu);
4222 63623 : res = buchall_end(nf,res,clg2,W,B,A,Ce,Vbase);
4223 63623 : delete_FB(&F);
4224 63623 : res = gerepilecopy(av0, res);
4225 63623 : if (flag) obj_insert_shallow(res, MATAL, cgetg(1,t_VEC));
4226 63623 : if (nfisclone) gunclone(nf);
4227 63623 : delete_cache(&cache);
4228 63623 : free_GRHcheck(&GRHcheck);
4229 63623 : return res;
4230 : }
|