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, *v, *y, *z;
110 : GEN x;
111 : } FP_t;
112 :
113 : static void
114 0 : wr_rel(GEN e)
115 : {
116 0 : long i, l = lg(e);
117 0 : for (i = 1; i < l; i++)
118 0 : if (e[i]) err_printf("%ld^%ld ",i,e[i]);
119 0 : }
120 : static void
121 0 : dbg_newrel(RELCACHE_t *cache)
122 : {
123 0 : if (DEBUGLEVEL > 1)
124 : {
125 0 : err_printf("\n++++ cglob = %ld\nrel = ", cache->last - cache->base);
126 0 : wr_rel(cache->last->R);
127 0 : err_printf("\n");
128 : }
129 : else
130 0 : err_printf("%ld ", cache->last - cache->base);
131 0 : }
132 :
133 : static void
134 64136 : delete_cache(RELCACHE_t *M)
135 : {
136 : REL_t *rel;
137 1057346 : for (rel = M->base+1; rel <= M->last; rel++)
138 : {
139 993210 : gunclone(rel->R);
140 993210 : if (rel->m) gunclone(rel->m);
141 : }
142 64136 : pari_free((void*)M->base); M->base = NULL;
143 64136 : }
144 :
145 : static void
146 66313 : delete_FB(FB_t *F)
147 : {
148 : subFB_t *s, *sold;
149 133277 : for (s = F->allsubFB; s; s = sold) { sold = s->old; pari_free(s); }
150 66313 : gunclone(F->minidx);
151 66313 : gunclone(F->idealperm);
152 66313 : }
153 :
154 : static void
155 64136 : reallocate(RELCACHE_t *M, long len)
156 : {
157 64136 : M->len = len;
158 64136 : if (!M->base)
159 64136 : M->base = (REL_t*)pari_malloc((len+1) * sizeof(REL_t));
160 : else
161 : {
162 0 : size_t l = M->last - M->base, c = M->chk - M->base, e = M->end - M->base;
163 0 : pari_realloc_ip((void**)&M->base, (len+1) * sizeof(REL_t));
164 0 : M->last = M->base + l;
165 0 : M->chk = M->base + c;
166 0 : M->end = M->base + e;
167 : }
168 64136 : }
169 :
170 : #define pr_get_smallp(pr) gel(pr,1)[2]
171 :
172 : /* don't take P|p all other Q|p are already there */
173 : static int
174 308078 : bad_subFB(FB_t *F, long t)
175 : {
176 308078 : GEN LP, P = gel(F->LP,t);
177 308078 : long p = pr_get_smallp(P);
178 308078 : LP = gel(F->LV,p);
179 308078 : return (isclone(LP) && t == F->iLP[p] + lg(LP)-1);
180 : }
181 :
182 : static void
183 66964 : assign_subFB(FB_t *F, GEN yes, long iyes)
184 : {
185 66964 : long i, lv = sizeof(subFB_t) + iyes*sizeof(long); /* for struct + GEN */
186 66964 : subFB_t *s = (subFB_t *)pari_malloc(lv);
187 66964 : s->subFB = (GEN)&s[1];
188 66964 : s->old = F->allsubFB; F->allsubFB = s;
189 288646 : for (i = 0; i < iyes; i++) s->subFB[i] = yes[i];
190 66964 : F->subFB = s->subFB;
191 66964 : F->MAXDEPSIZESFB = (iyes-1) * DEPSIZESFBMULT;
192 66964 : F->MAXDEPSFB = F->MAXDEPSIZESFB / DEPSFBDIV;
193 66964 : }
194 :
195 : /* Determine the permutation of the ideals made by each field automorphism */
196 : static GEN
197 66313 : FB_aut_perm(FB_t *F, GEN auts, GEN cyclic)
198 : {
199 66313 : long i, j, m, KC = F->KC, nauts = lg(auts)-1;
200 66313 : GEN minidx, perm = zero_Flm_copy(KC, nauts);
201 :
202 66313 : if (!nauts) { F->minidx = gclone(identity_zv(KC)); return cgetg(1,t_MAT); }
203 42023 : minidx = zero_Flv(KC);
204 91858 : for (m = 1; m < lg(cyclic); m++)
205 : {
206 49835 : GEN thiscyc = gel(cyclic, m);
207 49835 : long k0 = thiscyc[1];
208 49835 : GEN aut = gel(auts, k0), permk0 = gel(perm, k0), ppermk;
209 49835 : i = 1;
210 214575 : while (i <= KC)
211 : {
212 164740 : pari_sp av2 = avma;
213 164740 : GEN seen = zero_Flv(KC), P = gel(F->LP, i);
214 164740 : long imin = i, p, f, l;
215 164740 : p = pr_get_smallp(P);
216 164740 : f = pr_get_f(P);
217 : do
218 : {
219 483067 : if (++i > KC) break;
220 433232 : P = gel(F->LP, i);
221 : }
222 433232 : while (p == pr_get_smallp(P) && f == pr_get_f(P));
223 647807 : for (j = imin; j < i; j++)
224 : {
225 483067 : GEN img = ZM_ZC_mul(aut, pr_get_gen(gel(F->LP, j)));
226 1672656 : for (l = imin; l < i; l++)
227 1672656 : if (!seen[l] && ZC_prdvd(img, gel(F->LP, l)))
228 : {
229 483067 : seen[l] = 1; permk0[j] = l; break;
230 : }
231 : }
232 164740 : set_avma(av2);
233 : }
234 69094 : for (ppermk = permk0, i = 2; i < lg(thiscyc); i++)
235 : {
236 19259 : GEN permk = gel(perm, thiscyc[i]);
237 384504 : for (j = 1; j <= KC; j++) permk[j] = permk0[ppermk[j]];
238 19259 : ppermk = permk;
239 : }
240 : }
241 311324 : for (j = 1; j <= KC; j++)
242 : {
243 269301 : if (minidx[j]) continue;
244 129508 : minidx[j] = j;
245 362169 : for (i = 1; i <= nauts; i++) minidx[coeff(perm, j, i)] = j;
246 : }
247 42023 : F->minidx = gclone(minidx); return perm;
248 : }
249 :
250 : /* set subFB.
251 : * Fill F->perm (if != NULL): primes ideals sorted by increasing norm (except
252 : * the ones in subFB come first [dense rows for hnfspec]) */
253 : static void
254 66313 : subFBgen(FB_t *F, GEN auts, GEN cyclic, double PROD, long minsFB)
255 : {
256 : GEN y, perm, yes, no;
257 66313 : long i, j, k, iyes, ino, lv = F->KC + 1;
258 : double prod;
259 : pari_sp av;
260 :
261 66313 : F->LP = cgetg(lv, t_VEC);
262 66313 : F->L_jid = F->perm = cgetg(lv, t_VECSMALL);
263 66313 : av = avma;
264 66313 : y = cgetg(lv,t_COL); /* Norm P */
265 313576 : for (k=0, i=1; i <= F->KCZ; i++)
266 : {
267 247263 : GEN LP = gel(F->LV,F->FB[i]);
268 247263 : long l = lg(LP);
269 715028 : for (j = 1; j < l; j++)
270 : {
271 467765 : GEN P = gel(LP,j);
272 467765 : k++;
273 467765 : gel(y,k) = pr_norm(P);
274 467765 : gel(F->LP,k) = P;
275 : }
276 : }
277 : /* perm sorts LP by increasing norm */
278 66313 : perm = indexsort(y);
279 66313 : no = cgetg(lv, t_VECSMALL); ino = 1;
280 66313 : yes = cgetg(lv, t_VECSMALL); iyes = 1;
281 66313 : prod = 1.0;
282 303722 : for (i = 1; i < lv; i++)
283 : {
284 273727 : long t = perm[i];
285 273727 : if (bad_subFB(F, t)) { no[ino++] = t; continue; }
286 :
287 153159 : yes[iyes++] = t;
288 153159 : prod *= (double)itos(gel(y,t));
289 153159 : if (iyes > minsFB && prod > PROD) break;
290 : }
291 66313 : setlg(yes, iyes);
292 219472 : for (j=1; j<iyes; j++) F->perm[j] = yes[j];
293 186881 : for (i=1; i<ino; i++, j++) F->perm[j] = no[i];
294 260351 : for ( ; j<lv; j++) F->perm[j] = perm[j];
295 66313 : F->allsubFB = NULL;
296 66313 : F->idealperm = gclone(FB_aut_perm(F, auts, cyclic));
297 66313 : if (iyes) assign_subFB(F, yes, iyes);
298 66313 : set_avma(av);
299 66313 : }
300 : static int
301 26533 : subFB_change(FB_t *F)
302 : {
303 26533 : long i, iyes, minsFB = lg(F->subFB)-1, lv = F->KC + 1;
304 26533 : pari_sp av = avma;
305 26533 : GEN yes, L_jid = F->L_jid, present = zero_zv(lv-1);
306 :
307 26533 : if (F->sfb_chg == sfb_INCREASE) minsFB++;
308 :
309 26533 : yes = cgetg(minsFB+1, t_VECSMALL); iyes = 1;
310 26533 : if (L_jid)
311 : {
312 33973 : for (i = 1; i < lg(L_jid); i++)
313 : {
314 33723 : long l = L_jid[i];
315 33723 : if (bad_subFB(F, l)) continue;
316 31520 : yes[iyes++] = l;
317 31520 : present[l] = 1;
318 31520 : if (iyes > minsFB) break;
319 : }
320 : }
321 0 : else i = 1;
322 26533 : if (iyes <= minsFB)
323 : {
324 707 : for ( ; i < lv; i++)
325 : {
326 635 : long l = F->perm[i];
327 635 : if (present[l] || bad_subFB(F, l)) continue;
328 492 : yes[iyes++] = l;
329 492 : if (iyes > minsFB) break;
330 : }
331 250 : if (i == lv) return 0;
332 : }
333 26461 : if (zv_equal(F->subFB, yes))
334 : {
335 25810 : if (DEBUGLEVEL) err_printf("\n*** NOT Changing sub factor base\n");
336 : }
337 : else
338 : {
339 651 : if (DEBUGLEVEL) err_printf("\n*** Changing sub factor base\n");
340 651 : assign_subFB(F, yes, iyes);
341 : }
342 26461 : F->sfb_chg = 0; return gc_bool(av, 1);
343 : }
344 :
345 : /* make sure enough room to store n more relations */
346 : static void
347 123102 : pre_allocate(RELCACHE_t *cache, size_t n)
348 : {
349 123102 : size_t len = (cache->last - cache->base) + n;
350 123102 : if (len >= cache->len) reallocate(cache, len << 1);
351 123102 : }
352 :
353 : void
354 186440 : init_GRHcheck(GRHcheck_t *S, long N, long R1, double LOGD)
355 : {
356 186440 : const double c1 = M_PI*M_PI/2;
357 186440 : const double c2 = 3.663862376709;
358 186440 : const double c3 = 3.801387092431; /* Euler + log(8*Pi)*/
359 186440 : S->clone = 0;
360 186440 : S->cN = R1*c2 + N*c1;
361 186440 : S->cD = LOGD - N*c3 - R1*M_PI/2;
362 186440 : S->maxprimes = 16000; /* sufficient for LIMC=176081*/
363 186440 : S->primes = (GRHprime_t*)pari_malloc(S->maxprimes*sizeof(*S->primes));
364 186440 : S->nprimes = 0;
365 186440 : S->limp = 0;
366 186440 : u_forprime_init(&S->P, 2, ULONG_MAX);
367 186440 : }
368 :
369 : void
370 186440 : free_GRHcheck(GRHcheck_t *S)
371 : {
372 186440 : if (S->clone)
373 : {
374 64064 : long i = S->nprimes;
375 : GRHprime_t *pr;
376 7582862 : for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--) gunclone(pr->dec);
377 : }
378 186440 : pari_free(S->primes);
379 186440 : }
380 :
381 : int
382 2305868 : GRHok(GRHcheck_t *S, double L, double SA, double SB)
383 : {
384 2305868 : return (S->cD + (S->cN + 2*SB) / L - 2*SA < -1e-8);
385 : }
386 :
387 : /* Return factorization pattern of p: [f,n], where n[i] primes of
388 : * residue degree f[i] */
389 : static GEN
390 7518798 : get_fs(GEN nf, GEN P, GEN index, ulong p)
391 : {
392 : long j, k, f, n, l;
393 : GEN fs, ns;
394 :
395 7518798 : if (umodiu(index, p))
396 : { /* easy case: p does not divide index */
397 7480110 : GEN F = Flx_degfact(ZX_to_Flx(P,p), p);
398 7480110 : fs = gel(F,1); l = lg(fs);
399 : }
400 : else
401 : {
402 38688 : GEN F = idealprimedec(nf, utoipos(p));
403 38688 : l = lg(F);
404 38688 : fs = cgetg(l, t_VECSMALL);
405 121050 : for (j = 1; j < l; j++) fs[j] = pr_get_f(gel(F,j));
406 : }
407 7518798 : ns = cgetg(l, t_VECSMALL);
408 7518798 : f = fs[1]; n = 1;
409 13925317 : for (j = 2, k = 1; j < l; j++)
410 6406519 : if (fs[j] == f)
411 4681250 : n++;
412 : else
413 : {
414 1725269 : ns[k] = n; fs[k] = f; k++;
415 1725269 : f = fs[j]; n = 1;
416 : }
417 7518798 : ns[k] = n; fs[k] = f; k++;
418 7518798 : setlg(fs, k);
419 7518798 : setlg(ns, k); return mkvec2(fs,ns);
420 : }
421 :
422 : /* cache data for all rational primes up to the LIM */
423 : static void
424 922112 : cache_prime_dec(GRHcheck_t *S, ulong LIM, GEN nf)
425 : {
426 922112 : pari_sp av = avma;
427 : GRHprime_t *pr;
428 : GEN index, P;
429 : double nb;
430 :
431 922112 : if (S->limp >= LIM) return;
432 329938 : S->clone = 1;
433 329938 : nb = primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
434 329938 : GRH_ensure(S, nb+1); /* room for one extra prime */
435 329938 : P = nf_get_pol(nf);
436 329938 : index = nf_get_index(nf);
437 329938 : for (pr = S->primes + S->nprimes;;)
438 7188860 : {
439 7518798 : ulong p = u_forprime_next(&(S->P));
440 7518798 : pr->p = p;
441 7518798 : pr->logp = log((double)p);
442 7518798 : pr->dec = gclone(get_fs(nf, P, index, p));
443 7518798 : S->nprimes++;
444 7518798 : pr++;
445 7518798 : set_avma(av);
446 : /* store up to nextprime(LIM) included */
447 7518798 : if (p >= LIM) { S->limp = p; break; }
448 : }
449 : }
450 :
451 : static double
452 2260650 : tailresback(long R1, long R2, double rK, long C, double C2, double C3, double r1K, double r2K, double logC, double logC2, double logC3)
453 : {
454 2260650 : const double rQ = 1.83787706641;
455 2260650 : const double r1Q = 1.98505372441;
456 2260650 : const double r2Q = 1.07991541347;
457 4521300 : return fabs((R1+R2-1)*(12*logC3+4*logC2-9*logC-6)/(2*C*logC3)
458 2260650 : + (rK-rQ)*(6*logC2 + 5*logC + 2)/(C*logC3)
459 2260650 : - R2*(6*logC2+11*logC+6)/(C2*logC2)
460 2260650 : - 2*(r1K-r1Q)*(3*logC2 + 4*logC + 2)/(C2*logC3)
461 2260650 : + (R1+R2-1)*(12*logC3+40*logC2+45*logC+18)/(6*C3*logC3)
462 2260650 : + (r2K-r2Q)*(2*logC2 + 3*logC + 2)/(C3*logC3));
463 : }
464 :
465 : static double
466 1130325 : tailres(long R1, long R2, double al2K, double rKm, double rKM, double r1Km,
467 : double r1KM, double r2Km, double r2KM, double C, long i)
468 : {
469 : /* C >= 3*2^i, lower bound for eint1(log(C)/2) */
470 : /* for(i=0,30,print(eint1(log(3*2^i)/2))) */
471 : static double tab[] = {
472 : 0.50409264803,
473 : 0.26205336997,
474 : 0.14815491171,
475 : 0.08770540561,
476 : 0.05347651832,
477 : 0.03328934284,
478 : 0.02104510690,
479 : 0.01346475900,
480 : 0.00869778586,
481 : 0.00566279855,
482 : 0.00371111950,
483 : 0.00244567837,
484 : 0.00161948049,
485 : 0.00107686891,
486 : 0.00071868750,
487 : 0.00048119961,
488 : 0.00032312188,
489 : 0.00021753772,
490 : 0.00014679818,
491 : 9.9272855581E-5,
492 : 6.7263969995E-5,
493 : 4.5656812967E-5,
494 : 3.1041124593E-5,
495 : 2.1136011590E-5,
496 : 1.4411645381E-5,
497 : 9.8393304088E-6,
498 : 6.7257395409E-6,
499 : 4.6025878272E-6,
500 : 3.1529719271E-6,
501 : 2.1620490021E-6,
502 : 1.4839266071E-6
503 : };
504 1130325 : const double logC = log(C), logC2 = logC*logC, logC3 = logC*logC2;
505 1130325 : const double C2 = C*C, C3 = C*C2;
506 1130325 : double E1 = i >30? 0: tab[i];
507 1130325 : return al2K*((33*logC2+22*logC+8)/(8*logC3*sqrt(C))+15*E1/16)
508 1130325 : + maxdd(tailresback(rKm,r1KM,r2Km, C,C2,C3,R1,R2,logC,logC2,logC3),
509 1130325 : tailresback(rKM,r1Km,r2KM, C,C2,C3,R1,R2,logC,logC2,logC3))/2
510 1130325 : + ((R1+R2-1)*4*C+R2)*(C2+6*logC)/(4*C2*C2*logC2);
511 : }
512 :
513 : static long
514 64064 : primeneeded(long N, long R1, long R2, double LOGD)
515 : {
516 64064 : const double lim = 0.25; /* should be log(2)/2 == 0.34657... */
517 64064 : const double al2K = 0.3526*LOGD - 0.8212*N + 4.5007;
518 64064 : const double rKm = -1.0155*LOGD + 2.1042*N - 8.3419;
519 64064 : const double rKM = -0.5 *LOGD + 1.2076*N + 1;
520 64064 : const double r1Km = - LOGD + 1.4150*N;
521 64064 : const double r1KM = - LOGD + 1.9851*N;
522 64064 : const double r2Km = - LOGD + 0.9151*N;
523 64064 : const double r2KM = - LOGD + 1.0800*N;
524 64064 : long Cmin = 3, Cmax = 3, i = 0;
525 574651 : while (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, Cmax, i) > lim)
526 : {
527 510587 : Cmin = Cmax;
528 510587 : Cmax *= 2;
529 510587 : i++;
530 : }
531 64064 : i--;
532 619738 : while (Cmax - Cmin > 1)
533 : {
534 555674 : long t = (Cmin + Cmax)/2;
535 555674 : if (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, t, i) > lim)
536 344498 : Cmin = t;
537 : else
538 211176 : Cmax = t;
539 : }
540 64064 : return Cmax;
541 : }
542 :
543 : /* ~ 1 / Res(s = 1, zeta_K) */
544 : static GEN
545 64064 : compute_invres(GRHcheck_t *S, long LIMC)
546 : {
547 64064 : pari_sp av = avma;
548 64064 : double loginvres = 0.;
549 : GRHprime_t *pr;
550 : long i;
551 64064 : double logLIMC = log((double)LIMC);
552 64064 : double logLIMC2 = logLIMC*logLIMC, denc;
553 : double c0, c1, c2;
554 64064 : denc = 1/(pow((double)LIMC, 3.) * logLIMC * logLIMC2);
555 64064 : c2 = ( logLIMC2 + 3 * logLIMC / 2 + 1) * denc;
556 64064 : denc *= LIMC;
557 64064 : c1 = (3 * logLIMC2 + 4 * logLIMC + 2) * denc;
558 64064 : denc *= LIMC;
559 64064 : c0 = (3 * logLIMC2 + 5 * logLIMC / 2 + 1) * denc;
560 7526708 : for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--)
561 : {
562 : GEN dec, fs, ns;
563 : long addpsi;
564 : double addpsi1, addpsi2;
565 7518798 : double logp = pr->logp, NPk;
566 7518798 : long j, k, limp = logLIMC/logp;
567 7518798 : ulong p = pr->p, p2 = p*p;
568 7518798 : if (limp < 1) break;
569 7462644 : dec = pr->dec;
570 7462644 : fs = gel(dec, 1); ns = gel(dec, 2);
571 7462644 : loginvres += 1./p;
572 : /* NB: limp = 1 nearly always and limp > 2 for very few primes */
573 8830003 : for (k=2, NPk = p; k <= limp; k++) { NPk *= p; loginvres += 1/(k * NPk); }
574 7462644 : addpsi = limp;
575 7462644 : addpsi1 = p *(pow((double)p , (double)limp)-1)/(p -1);
576 7462644 : addpsi2 = p2*(pow((double)p2, (double)limp)-1)/(p2-1);
577 7462644 : j = lg(fs);
578 16638545 : while (--j > 0)
579 : {
580 : long f, nb, kmax;
581 : double NP, NP2, addinvres;
582 9175901 : f = fs[j]; if (f > limp) continue;
583 3984946 : nb = ns[j];
584 3984946 : NP = pow((double)p, (double)f);
585 3984946 : addinvres = 1/NP;
586 3984946 : kmax = limp / f;
587 4862070 : for (k=2, NPk = NP; k <= kmax; k++) { NPk *= NP; addinvres += 1/(k*NPk); }
588 3984946 : NP2 = NP*NP;
589 3984946 : loginvres -= nb * addinvres;
590 3984946 : addpsi -= nb * f * kmax;
591 3984946 : addpsi1 -= nb*(f*NP *(pow(NP ,(double)kmax)-1)/(NP -1));
592 3984946 : addpsi2 -= nb*(f*NP2*(pow(NP2,(double)kmax)-1)/(NP2-1));
593 : }
594 7462644 : loginvres -= (addpsi*c0 - addpsi1*c1 + addpsi2*c2)*logp;
595 : }
596 64064 : return gc_leaf(av, mpexp(dbltor(loginvres)));
597 : }
598 :
599 : static long
600 64064 : nthideal(GRHcheck_t *S, GEN nf, long n)
601 : {
602 64064 : pari_sp av = avma;
603 64064 : GEN P = nf_get_pol(nf);
604 64064 : ulong p = 0, *vecN = (ulong*)const_vecsmall(n, LONG_MAX);
605 64064 : long i, N = poldegree(P, -1);
606 64064 : for (i = 0; ; i++)
607 230895 : {
608 : GRHprime_t *pr;
609 : GEN fs;
610 294959 : cache_prime_dec(S, p+1, nf);
611 294959 : pr = S->primes + i;
612 294959 : fs = gel(pr->dec, 1);
613 294959 : p = pr->p;
614 294959 : if (fs[1] != N)
615 : {
616 198142 : GEN ns = gel(pr->dec, 2);
617 198142 : long k, l, j = lg(fs);
618 443982 : while (--j > 0)
619 : {
620 245840 : ulong NP = upowuu(p, fs[j]);
621 : long nf;
622 245840 : if (!NP) continue;
623 754569 : for (k = 1; k <= n; k++) if (vecN[k] > NP) break;
624 245448 : if (k > n) continue;
625 : /* vecN[k] <= NP */
626 158997 : nf = ns[j]; /*#{primes of norme NP} = nf, insert them here*/
627 355892 : for (l = k+nf; l <= n; l++) vecN[l] = vecN[l-nf];
628 401902 : for (l = 0; l < nf && k+l <= n; l++) vecN[k+l] = NP;
629 365421 : while (l <= k) vecN[l++] = NP;
630 : }
631 : }
632 294959 : if (p > vecN[n]) break;
633 : }
634 64064 : return gc_long(av, vecN[n]);
635 : }
636 :
637 : /* volume of unit ball in R^n: \pi^{n/2} / \Gamma(n/2 + 1) */
638 : static double
639 66313 : ballvol(long n)
640 : {
641 66313 : double v = odd(n)? 2: 1;
642 151689 : for (; n > 1; n -= 2) v *= (2 * M_PI) / n;
643 66313 : return v;
644 : }
645 :
646 : /* Compute FB, LV, iLP + KC*. Reset perm
647 : * C2: bound for norm of tested prime ideals (includes be_honest())
648 : * C1: bound for p, such that P|p (NP <= C2) used to build relations */
649 : static void
650 66313 : FBgen(FB_t *F, GEN nf, long N, ulong C1, ulong C2, GRHcheck_t *S)
651 : {
652 : GRHprime_t *pr;
653 : long i, ip;
654 : GEN prim;
655 66313 : const double L = log((double)C2 + 0.5);
656 :
657 66313 : cache_prime_dec(S, C2, nf);
658 66313 : pr = S->primes;
659 66313 : F->sfb_chg = 0;
660 66313 : F->FB = cgetg(C2+1, t_VECSMALL);
661 66313 : F->iLP = cgetg(C2+1, t_VECSMALL);
662 66313 : F->LV = zerovec(C2);
663 :
664 66313 : prim = icopy(gen_1);
665 66313 : i = ip = 0;
666 66313 : F->KC = F->KCZ = 0;
667 438316 : for (;; pr++) /* p <= C2 */
668 438316 : {
669 504629 : ulong p = pr->p;
670 : long k, l, m;
671 : GEN LP, nb, f;
672 :
673 504629 : if (!F->KC && p > C1) { F->KCZ = i; F->KC = ip; }
674 504629 : if (p > C2) break;
675 :
676 467422 : if (DEBUGLEVEL>1) err_printf(" %ld",p);
677 :
678 467422 : f = gel(pr->dec, 1); nb = gel(pr->dec, 2);
679 467422 : if (f[1] == N)
680 : {
681 146633 : if (p == C2) break;
682 138093 : continue; /* p inert */
683 : }
684 320789 : l = (long)(L/pr->logp); /* p^f <= C2 <=> f <= l */
685 584768 : for (k=0, m=1; m < lg(f) && f[m]<=l; m++) k += nb[m];
686 320789 : if (!k)
687 : { /* too inert to appear in FB */
688 73519 : if (p == C2) break;
689 72665 : continue;
690 : }
691 247270 : prim[2] = p; LP = idealprimedec_limit_f(nf,prim, l);
692 : /* keep noninert ideals with Norm <= C2 */
693 247270 : if (m == lg(f)) setisclone(LP); /* flag it: all prime divisors in FB */
694 247270 : F->FB[++i]= p;
695 247270 : gel(F->LV,p) = LP;
696 247270 : F->iLP[p] = ip; ip += k;
697 247270 : if (p == C2) break;
698 : }
699 66313 : if (!F->KC) { F->KCZ = i; F->KC = ip; }
700 : /* Note F->KC > 0 otherwise GRHchk is false */
701 66313 : setlg(F->FB, F->KCZ+1); F->KCZ2 = i;
702 66313 : F->prodZ = zv_prod_Z(F->FB);
703 66313 : if (DEBUGLEVEL>1)
704 : {
705 0 : err_printf("\n");
706 0 : if (DEBUGLEVEL>6)
707 : {
708 0 : err_printf("########## FACTORBASE ##########\n\n");
709 0 : err_printf("KC2=%ld, KC=%ld, KCZ=%ld, KCZ2=%ld\n",
710 : ip, F->KC, F->KCZ, F->KCZ2);
711 0 : for (i=1; i<=F->KCZ; i++) err_printf("++ LV[%ld] = %Ps",i,gel(F->LV,F->FB[i]));
712 : }
713 : }
714 66313 : F->perm = NULL; F->L_jid = NULL;
715 66313 : F->ballvol = ballvol(nf_get_degree(nf));
716 66313 : }
717 :
718 : static int
719 496776 : GRHchk(GEN nf, GRHcheck_t *S, ulong LIMC)
720 : {
721 496776 : double logC = log((double)LIMC), SA = 0, SB = 0;
722 496776 : GRHprime_t *pr = S->primes;
723 :
724 496776 : cache_prime_dec(S, LIMC, nf);
725 496776 : for (pr = S->primes;; pr++)
726 3085397 : {
727 3582173 : ulong p = pr->p;
728 : GEN dec, fs, ns;
729 : double logCslogp;
730 : long j;
731 :
732 3582173 : if (p > LIMC) break;
733 3191909 : dec = pr->dec; fs = gel(dec, 1); ns = gel(dec,2);
734 3191909 : logCslogp = logC/pr->logp;
735 5023305 : for (j = 1; j < lg(fs); j++)
736 : {
737 3930129 : long f = fs[j], M, nb;
738 : double logNP, q, A, B;
739 3930129 : if (f > logCslogp) break;
740 1831396 : logNP = f * pr->logp;
741 1831396 : q = 1/sqrt((double)upowuu(p, f));
742 1831396 : A = logNP * q; B = logNP * A; M = (long)(logCslogp/f);
743 1831396 : if (M > 1)
744 : {
745 376670 : double inv1_q = 1 / (1-q);
746 376670 : A *= (1 - pow(q, (double)M)) * inv1_q;
747 376670 : B *= (1 - pow(q, (double)M)*(M+1 - M*q)) * inv1_q * inv1_q;
748 : }
749 1831396 : nb = ns[j];
750 1831396 : SA += nb * A;
751 1831396 : SB += nb * B;
752 : }
753 3191909 : if (p == LIMC) break;
754 : }
755 496776 : return GRHok(S, logC, SA, SB);
756 : }
757 :
758 : /* SMOOTH IDEALS */
759 : static void
760 9537438 : store(long i, long e, FACT *fact)
761 : {
762 9537438 : ++fact[0].pr;
763 9537438 : fact[fact[0].pr].pr = i; /* index */
764 9537438 : fact[fact[0].pr].ex = e; /* exponent */
765 9537438 : }
766 :
767 : /* divide out m by all P|p, k = v_p(Nm) */
768 : static int
769 2344 : divide_p_elt(GEN LP, long ip, long k, GEN m, FACT *fact)
770 : {
771 2344 : long j, l = lg(LP);
772 3047 : for (j=1; j<l; j++)
773 : {
774 3047 : GEN P = gel(LP,j);
775 3047 : long v = ZC_nfval(m, P);
776 3047 : if (!v) continue;
777 2651 : store(ip + j, v, fact); /* v = v_P(m) > 0 */
778 2651 : k -= v * pr_get_f(P);
779 2651 : if (!k) return 1;
780 : }
781 0 : return 0;
782 : }
783 : /* divide out I by all P|p, k = v_p(NI) */
784 : static int
785 185887 : divide_p_id(GEN LP, long ip, long k, GEN nf, GEN I, FACT *fact)
786 : {
787 185887 : long j, l = lg(LP);
788 278675 : for (j=1; j<l; j++)
789 : {
790 270814 : GEN P = gel(LP,j);
791 270814 : long v = idealval(nf,I, P);
792 270814 : if (!v) continue;
793 181525 : store(ip + j, v, fact); /* v = v_P(I) > 0 */
794 181525 : k -= v * pr_get_f(P);
795 181525 : if (!k) return 1;
796 : }
797 7861 : return 0;
798 : }
799 : /* divide out m/I by all P|p, k = v_p(Nm/NI) */
800 : static int
801 5498195 : divide_p_quo(GEN LP, long ip, long k, GEN nf, GEN I, GEN m, FACT *fact)
802 : {
803 5498195 : long j, l = lg(LP);
804 17840789 : for (j=1; j<l; j++)
805 : {
806 17752373 : GEN P = gel(LP,j);
807 17752373 : long v = ZC_nfval(m, P);
808 17752373 : if (!v) continue;
809 8180443 : v -= idealval(nf,I, P);
810 8180443 : if (!v) continue;
811 6950008 : store(ip + j, v, fact); /* v = v_P(m / I) > 0 */
812 6950008 : k -= v * pr_get_f(P);
813 6950008 : if (!k) return 1;
814 : }
815 88416 : return 0;
816 : }
817 :
818 : static int
819 5686426 : divide_p(FB_t *F, long p, long k, GEN nf, GEN I, GEN m, FACT *fact)
820 : {
821 5686426 : GEN LP = gel(F->LV,p);
822 5686426 : long ip = F->iLP[p];
823 5686426 : if (!m) return divide_p_id (LP,ip,k,nf,I,fact);
824 5500539 : if (!I) return divide_p_elt(LP,ip,k,m,fact);
825 5498195 : return divide_p_quo(LP,ip,k,nf,I,m,fact);
826 : }
827 :
828 : /* Let x = m if I == NULL,
829 : * I if m == NULL,
830 : * m/I otherwise.
831 : * Can we factor the integral primitive ideal x ? |N| = Norm x > 0 */
832 : static long
833 18799277 : can_factor(FB_t *F, GEN nf, GEN I, GEN m, GEN N, FACT *fact)
834 : {
835 : GEN f, p, e;
836 : long i, l;
837 18799277 : fact[0].pr = 0;
838 18799277 : if (is_pm1(N)) return 1;
839 17818507 : if (!is_pm1(Z_ppo(N, F->prodZ))) return 0;
840 2855256 : f = absZ_factor(N); p = gel(f,1); e = gel(f,2); l = lg(p);
841 8445405 : for (i = 1; i < l; i++)
842 5686426 : if (!divide_p(F, itou(gel(p,i)), itou(gel(e,i)), nf, I, m, fact))
843 : {
844 96277 : if (DEBUGLEVEL > 1) err_printf(".");
845 96277 : return 0;
846 : }
847 2758979 : return 1;
848 : }
849 :
850 : /* can we factor m/I ? [m in I from idealpseudomin_nonscalar], NI = norm I */
851 : static long
852 17385040 : factorgen(FB_t *F, GEN nf, GEN I, GEN NI, GEN m, FACT *fact)
853 : {
854 : long e;
855 17385040 : GEN Nm = embed_norm(RgM_RgC_mul(nf_get_M(nf),m), nf_get_r1(nf));
856 17385040 : GEN N = grndtoi(NI? divri(Nm, NI): Nm, &e); /* ~ N(m/I) */
857 17385040 : if (e > -32)
858 : {
859 0 : if (DEBUGLEVEL > 1) err_printf("+");
860 0 : return 0;
861 : }
862 17385040 : return can_factor(F, nf, I, m, N, fact);
863 : }
864 :
865 : /* FUNDAMENTAL UNITS */
866 :
867 : /* a, y real. Return (Re(x) + a) + I * (Im(x) % y) */
868 : static GEN
869 6917655 : addRe_modIm(GEN x, GEN a, GEN y, GEN iy)
870 : {
871 : GEN z;
872 6917655 : if (typ(x) == t_COMPLEX)
873 : {
874 4913499 : GEN re, im = modRr_i(gel(x,2), y, iy);
875 4913499 : if (!im) return NULL;
876 4913499 : re = gadd(gel(x,1), a);
877 4913499 : z = gequal0(im)? re: mkcomplex(re, im);
878 : }
879 : else
880 2004156 : z = gadd(x, a);
881 6917655 : return z;
882 : }
883 : static GEN
884 204750 : modIm(GEN x, GEN y, GEN iy)
885 : {
886 204750 : if (typ(x) == t_COMPLEX)
887 : {
888 191693 : GEN im = modRr_i(gel(x,2), y, iy);
889 191693 : if (!im) return NULL;
890 191693 : x = gequal0(im)? gel(x,1): mkcomplex(gel(x,1), im);
891 : }
892 204750 : return x;
893 : }
894 :
895 : /* clean archimedean components. ipi = 2^n / pi (n arbitrary); its
896 : * exponent may be modified */
897 : static GEN
898 3076975 : cleanarch(GEN x, long N, GEN ipi, long prec)
899 : {
900 : long i, l, R1, RU;
901 3076975 : GEN s, y = cgetg_copy(x, &l);
902 :
903 3076975 : if (!ipi) ipi = invr(mppi(prec));
904 3076975 : if (typ(x) == t_MAT)
905 : {
906 529706 : for (i = 1; i < l; i++)
907 465488 : if (!(gel(y,i) = cleanarch(gel(x,i), N, ipi, prec))) return NULL;
908 64218 : return y;
909 : }
910 3012757 : RU = l-1; R1 = (RU<<1) - N;
911 3012757 : s = gdivgs(RgV_sum(real_i(x)), -N); /* -log |norm(x)| / N */
912 3012757 : i = 1;
913 3012757 : if (R1)
914 : {
915 2529064 : GEN pi2 = Pi2n(1, prec);
916 2529064 : setexpo(ipi, -3); /* 1/(2pi) */
917 7805978 : for (; i <= R1; i++)
918 5276914 : if (!(gel(y,i) = addRe_modIm(gel(x,i), s, pi2, ipi))) return NULL;
919 : }
920 3012757 : if (i <= RU)
921 : {
922 1080574 : GEN pi4 = Pi2n(2, prec), s2 = gmul2n(s, 1);
923 1080574 : setexpo(ipi, -4); /* 1/(4pi) */
924 2721315 : for (; i <= RU; i++)
925 1640741 : if (!(gel(y,i) = addRe_modIm(gel(x,i), s2, pi4, ipi))) return NULL;
926 : }
927 3012757 : return y;
928 : }
929 : GEN
930 199199 : nf_cxlog_normalize(GEN nf, GEN x, long prec)
931 : {
932 199199 : long N = nf_get_degree(nf);
933 199199 : return cleanarch(x, N, NULL, prec);
934 : }
935 :
936 : /* clean unit archimedean components. ipi = 2^n / pi (n arbitrary); its
937 : * exponent may be modified */
938 : static GEN
939 133994 : cleanarchunit(GEN x, long N, GEN ipi, long prec)
940 : {
941 : long i, l, R1, RU;
942 133994 : GEN y = cgetg_copy(x, &l);
943 :
944 133994 : if (!ipi) ipi = invr(mppi(prec));
945 133994 : if (typ(x) == t_MAT)
946 : {
947 133987 : for (i = 1; i < l; i++)
948 69923 : if (!(gel(y,i) = cleanarchunit(gel(x,i), N, ipi, prec))) return NULL;
949 64064 : return y;
950 : }
951 69923 : if (gexpo(RgV_sum(real_i(x))) > -10) return NULL;
952 69916 : RU = l-1; R1 = (RU<<1) - N;
953 69916 : i = 1;
954 69916 : if (R1)
955 : {
956 55391 : GEN pi2 = Pi2n(1, prec);
957 55391 : setexpo(ipi, -3); /* 1/(2pi) */
958 189371 : for (; i <= R1; i++)
959 133980 : if (!(gel(y,i) = modIm(gel(x,i), pi2, ipi))) return NULL;
960 : }
961 69916 : if (i <= RU)
962 : {
963 34496 : GEN pi4 = Pi2n(2, prec);
964 34496 : setexpo(ipi, -4); /* 1/(4pi) */
965 105266 : for (; i <= RU; i++)
966 70770 : if (!(gel(y,i) = modIm(gel(x,i), pi4, ipi))) return NULL;
967 : }
968 69916 : return y;
969 : }
970 :
971 : static GEN
972 396 : not_given(long reason)
973 : {
974 396 : if (DEBUGLEVEL)
975 0 : switch(reason)
976 : {
977 0 : case fupb_LARGE:
978 0 : pari_warn(warner,"fundamental units too large, not given");
979 0 : break;
980 0 : case fupb_PRECI:
981 0 : pari_warn(warner,"insufficient precision for fundamental units, not given");
982 0 : break;
983 : }
984 396 : return NULL;
985 : }
986 :
987 : /* check whether exp(x) will 1) get too big (real(x) large), 2) require
988 : * large accuracy for argument reduction (imag(x) large) */
989 : static long
990 2843552 : expbitprec(GEN x, long *e)
991 : {
992 : GEN re, im;
993 2843552 : if (typ(x) != t_COMPLEX) re = x;
994 : else
995 : {
996 1781581 : im = gel(x,2); *e = maxss(*e, expo(im) + 5 - bit_prec(im));
997 1781581 : re = gel(x,1);
998 : }
999 2843552 : return (expo(re) <= 20);
1000 :
1001 : }
1002 : static long
1003 1238642 : RgC_expbitprec(GEN x)
1004 : {
1005 1238642 : long l = lg(x), i, e = - (long)HIGHEXPOBIT;
1006 3876982 : for (i = 1; i < l; i++)
1007 2638914 : if (!expbitprec(gel(x,i), &e)) return LONG_MAX;
1008 1238068 : return e;
1009 : }
1010 : static long
1011 48853 : RgM_expbitprec(GEN x)
1012 : {
1013 48853 : long i, j, I, J, e = - (long)HIGHEXPOBIT;
1014 48853 : RgM_dimensions(x, &I,&J);
1015 118699 : for (j = 1; j <= J; j++)
1016 274484 : for (i = 1; i <= I; i++)
1017 204638 : if (!expbitprec(gcoeff(x,i,j), &e)) return LONG_MAX;
1018 48790 : return e;
1019 : }
1020 :
1021 : static GEN
1022 1379 : FlxqX_chinese_unit(GEN X, GEN U, GEN invzk, GEN D, GEN T, ulong p)
1023 : {
1024 1379 : long i, lU = lg(U), lX = lg(X), d = lg(invzk)-1;
1025 1379 : GEN M = cgetg(lU, t_MAT);
1026 1379 : if (D)
1027 : {
1028 1272 : D = Flv_inv(D, p);
1029 69798 : for (i = 1; i < lX; i++)
1030 68526 : if (uel(D, i) != 1)
1031 56650 : gel(X,i) = Flx_Fl_mul(gel(X,i), uel(D,i), p);
1032 : }
1033 3878 : for (i = 1; i < lU; i++)
1034 : {
1035 2499 : GEN H = FlxqV_factorback(X, gel(U, i), T, p);
1036 2499 : gel(M, i) = Flm_Flc_mul(invzk, Flx_to_Flv(H, d), p);
1037 : }
1038 1379 : return M;
1039 : }
1040 :
1041 : static GEN
1042 274 : chinese_unit_slice(GEN A, GEN U, GEN B, GEN D, GEN C, GEN P, GEN *mod)
1043 : {
1044 274 : pari_sp av = avma;
1045 274 : long i, n = lg(P)-1, v = varn(C);
1046 : GEN H, T;
1047 274 : if (n == 1)
1048 : {
1049 0 : ulong p = uel(P,1);
1050 0 : GEN a = ZXV_to_FlxV(A, p), b = ZM_to_Flm(B, p), c = ZX_to_Flx(C, p);
1051 0 : GEN d = D ? ZV_to_Flv(D, p): NULL;
1052 0 : GEN Hp = FlxqX_chinese_unit(a, U, b, d, c, p);
1053 0 : H = gc_upto(av, Flm_to_ZM(Hp));
1054 0 : *mod = utoi(p);
1055 0 : return H;
1056 : }
1057 274 : T = ZV_producttree(P);
1058 274 : A = ZXC_nv_mod_tree(A, P, T, v);
1059 274 : B = ZM_nv_mod_tree(B, P, T);
1060 274 : D = D ? ZV_nv_mod_tree(D, P, T): NULL;
1061 274 : C = ZX_nv_mod_tree(C, P, T);
1062 :
1063 274 : H = cgetg(n+1, t_VEC);
1064 1653 : for(i=1; i <= n; i++)
1065 : {
1066 1379 : ulong p = P[i];
1067 1379 : GEN a = gel(A,i), b = gel(B,i), c = gel(C,i), d = D ? gel(D,i): NULL;
1068 1379 : gel(H,i) = FlxqX_chinese_unit(a, U, b, d, c, p);
1069 : }
1070 274 : H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
1071 274 : *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
1072 : }
1073 :
1074 : GEN
1075 274 : chinese_unit_worker(GEN P, GEN A, GEN U, GEN B, GEN D, GEN C)
1076 : {
1077 274 : GEN V = cgetg(3, t_VEC);
1078 274 : gel(V,1) = chinese_unit_slice(A, U, B, isintzero(D) ? NULL: D, C, P, &gel(V,2));
1079 274 : return V;
1080 : }
1081 :
1082 : /* Let x = \prod X[i]^E[i] = u, return u.
1083 : * If dX != NULL, X[i] = nX[i] / dX[i] where nX[i] is a ZX, dX[i] in Z */
1084 : static GEN
1085 94 : chinese_unit(GEN nf, GEN nX, GEN dX, GEN U, ulong bnd)
1086 : {
1087 94 : pari_sp av = avma;
1088 94 : GEN f = nf_get_index(nf), T = nf_get_pol(nf), invzk = nf_get_invzk(nf);
1089 : GEN H, mod;
1090 : forprime_t S;
1091 94 : GEN worker = snm_closure(is_entry("_chinese_unit_worker"),
1092 : mkcol5(nX, U, invzk, dX? dX: gen_0, T));
1093 94 : init_modular_big(&S);
1094 94 : H = gen_crt("chinese_units", worker, &S, f, bnd, 0, &mod, nmV_chinese_center, FpM_center);
1095 94 : settyp(H, t_VEC); return gc_GEN(av, H);
1096 : }
1097 :
1098 : /* *pE a ZM */
1099 : static void
1100 164 : ZM_remove_unused(GEN *pE, GEN *pX)
1101 : {
1102 164 : long j, k, l = lg(*pX);
1103 164 : GEN E = *pE, v = cgetg(l, t_VECSMALL);
1104 16395 : for (j = k = 1; j < l; j++)
1105 16231 : if (!ZMrow_equal0(E, j)) v[k++] = j;
1106 164 : if (k < l)
1107 : {
1108 164 : setlg(v, k);
1109 164 : *pX = vecpermute(*pX,v);
1110 164 : *pE = rowpermute(E,v);
1111 : }
1112 164 : }
1113 :
1114 : /* s = -log|norm(x)|/N */
1115 : static GEN
1116 1308558 : fixarch(GEN x, GEN s, long R1)
1117 : {
1118 : long i, l;
1119 1308558 : GEN y = cgetg_copy(x, &l);
1120 3649591 : for (i = 1; i <= R1; i++) gel(y,i) = gadd(s, gel(x,i));
1121 1811777 : for ( ; i < l; i++) gel(y,i) = gadd(s, gmul2n(gel(x,i),-1));
1122 1308558 : return y;
1123 : }
1124 :
1125 : static GEN
1126 64064 : getfu(GEN nf, GEN *ptA, GEN *ptU, long prec)
1127 : {
1128 64064 : GEN U, y, matep, A, T = nf_get_pol(nf), M = nf_get_M(nf);
1129 64064 : long e, j, R1, RU, N = degpol(T);
1130 :
1131 64064 : R1 = nf_get_r1(nf); RU = (N+R1) >> 1;
1132 64064 : if (RU == 1) return cgetg(1,t_VEC);
1133 :
1134 48853 : A = *ptA;
1135 48853 : matep = cgetg(RU,t_MAT);
1136 118769 : for (j = 1; j < RU; j++)
1137 : {
1138 69916 : GEN Aj = gel(A,j), s = gdivgs(RgV_sum(real_i(Aj)), -N);
1139 69916 : gel(matep,j) = fixarch(Aj, s, R1);
1140 : }
1141 48853 : U = lll(real_i(matep));
1142 48853 : if (lg(U) < RU) return not_given(fupb_PRECI);
1143 48853 : if (ptU) { *ptU = U; *ptA = A = RgM_ZM_mul(A,U); }
1144 48853 : y = RgM_ZM_mul(matep,U);
1145 48853 : e = RgM_expbitprec(y);
1146 48853 : if (e >= 0) return not_given(e == LONG_MAX? fupb_LARGE: fupb_PRECI);
1147 48790 : if (prec <= 0) prec = gprecision(A);
1148 48790 : y = RgM_solve_realimag(M, gexp(y,prec));
1149 48790 : if (!y) return not_given(fupb_PRECI);
1150 48790 : y = grndtoi(y, &e); if (e >= 0) return not_given(fupb_PRECI);
1151 48465 : settyp(y, t_VEC);
1152 :
1153 48465 : if (!ptU) *ptA = A = RgM_ZM_mul(A, U);
1154 117633 : for (j = 1; j < RU; j++)
1155 : { /* y[i] are hopefully unit generators. Normalize: smallest T2 norm */
1156 69176 : GEN u = gel(y,j), v = zk_inv(nf, u);
1157 69176 : if (!v || !is_pm1(Q_denom(v)) || ZV_isscalar(u))
1158 8 : return not_given(fupb_PRECI);
1159 69168 : if (gcmp(RgC_fpnorml2(v,DEFAULTPREC), RgC_fpnorml2(u,DEFAULTPREC)) < 0)
1160 : {
1161 30041 : gel(A,j) = RgC_neg(gel(A,j));
1162 30041 : if (ptU) gel(U,j) = ZC_neg(gel(U,j));
1163 30041 : u = v;
1164 : }
1165 69168 : gel(y,j) = nf_to_scalar_or_alg(nf, u);
1166 : }
1167 48457 : return y;
1168 : }
1169 :
1170 : static void
1171 0 : err_units() { pari_err_PREC("makeunits [cannot get units, use bnfinit(,1)]"); }
1172 :
1173 : /* bound for log2 |sigma(u)|, sigma complex embedding, u fundamental unit
1174 : * attached to bnf_get_logfu */
1175 : static double
1176 94 : log2fubound(GEN bnf)
1177 : {
1178 94 : GEN LU = bnf_get_logfu(bnf);
1179 94 : long i, j, l = lg(LU), r1 = nf_get_r1(bnf_get_nf(bnf));
1180 94 : double e = 0.0;
1181 330 : for (j = 1; j < l; j++)
1182 : {
1183 236 : GEN u = gel(LU,j);
1184 624 : for (i = 1; i <= r1; i++)
1185 : {
1186 388 : GEN E = real_i(gel(u,i));
1187 388 : e = maxdd(e, gtodouble(E));
1188 : }
1189 842 : for ( ; i <= l; i++)
1190 : {
1191 606 : GEN E = real_i(gel(u,i));
1192 606 : e = maxdd(e, gtodouble(E) / 2);
1193 : }
1194 : }
1195 94 : return e / M_LN2;
1196 : }
1197 : /* bound for log2(|RgM_solve_realimag(M, y)|_oo / |y|_oo)*/
1198 : static double
1199 94 : log2Mbound(GEN nf)
1200 : {
1201 94 : GEN G = nf_get_G(nf), D = nf_get_disc(nf);
1202 94 : long r2 = nf_get_r2(nf), l = lg(G), i;
1203 94 : double e, d = dbllog2(D)/2 - r2 * M_LN2; /* log2 |det(split_realimag(M))| */
1204 94 : e = log2(nf_get_degree(nf));
1205 535 : for (i = 2; i < l; i++) e += dbllog2(gnorml2(gel(G,i))); /* Hadamard bound */
1206 94 : return e / 2 - d;
1207 : }
1208 :
1209 : static GEN
1210 94 : vec_chinese_units(GEN bnf)
1211 : {
1212 94 : GEN nf = bnf_get_nf(bnf), SUnits = bnf_get_sunits(bnf);
1213 94 : double bnd = ceil(log2Mbound(nf) + log2fubound(bnf));
1214 94 : GEN X, dX, Y, U, f = nf_get_index(nf);
1215 94 : long j, l, v = nf_get_varn(nf);
1216 94 : if (!SUnits) err_units(); /* no compact units */
1217 94 : Y = gel(SUnits,1);
1218 94 : U = gel(SUnits,2);
1219 94 : ZM_remove_unused(&U, &Y); l = lg(Y); X = cgetg(l, t_VEC);
1220 94 : if (is_pm1(f)) f = dX = NULL; else dX = cgetg(l, t_VEC);
1221 5142 : for (j = 1; j < l; j++)
1222 : {
1223 5048 : GEN t = nf_to_scalar_or_alg(nf, gel(Y,j));
1224 5048 : if (f)
1225 : {
1226 : GEN den;
1227 4202 : t = Q_remove_denom(t, &den);
1228 4202 : gel(dX,j) = den ? den: gen_1;
1229 : }
1230 5048 : gel(X,j) = typ(t) == t_INT? scalarpol_shallow(t,v): t;
1231 : }
1232 94 : if (dblexpo(bnd) >= BITS_IN_LONG)
1233 0 : pari_err_OVERFLOW("vec_chinese_units [units too large]");
1234 94 : return chinese_unit(nf, X, dX, U, (ulong)bnd);
1235 : }
1236 :
1237 : static GEN
1238 24922 : makeunits(GEN bnf)
1239 : {
1240 24922 : GEN nf = bnf_get_nf(bnf), fu = bnf_get_fu_nocheck(bnf);
1241 24922 : GEN tu = nf_to_scalar_or_basis(nf, bnf_get_tuU(bnf));
1242 24922 : fu = (typ(fu) == t_MAT)? vec_chinese_units(bnf): matalgtobasis(nf, fu);
1243 24922 : return vec_prepend(fu, tu);
1244 : }
1245 :
1246 : /*******************************************************************/
1247 : /* */
1248 : /* PRINCIPAL IDEAL ALGORITHM (DISCRETE LOG) */
1249 : /* */
1250 : /*******************************************************************/
1251 :
1252 : /* G: prime ideals, E: vector of nonnegative exponents.
1253 : * C = possible extra prime (^1) or NULL
1254 : * Return Norm (product) */
1255 : static GEN
1256 69 : get_norm_fact_primes(GEN G, GEN E, GEN C)
1257 : {
1258 69 : pari_sp av=avma;
1259 69 : GEN N = gen_1, P, p;
1260 69 : long i, c = lg(E);
1261 69 : for (i=1; i<c; i++)
1262 : {
1263 0 : GEN ex = gel(E,i);
1264 0 : long s = signe(ex);
1265 0 : if (!s) continue;
1266 :
1267 0 : P = gel(G,i); p = pr_get_p(P);
1268 0 : N = mulii(N, powii(p, mului(pr_get_f(P), ex)));
1269 : }
1270 69 : if (C) N = mulii(N, pr_norm(C));
1271 69 : return gc_INT(av, N);
1272 : }
1273 :
1274 : /* gen: HNF ideals */
1275 : static GEN
1276 1233029 : get_norm_fact(GEN gen, GEN ex, GEN *pd)
1277 : {
1278 1233029 : long i, c = lg(ex);
1279 : GEN d,N,I,e,n,ne,de;
1280 1233029 : d = N = gen_1;
1281 1529794 : for (i=1; i<c; i++)
1282 296765 : if (signe(gel(ex,i)))
1283 : {
1284 175581 : I = gel(gen,i); e = gel(ex,i); n = ZM_det_triangular(I);
1285 175581 : ne = powii(n,e);
1286 175581 : de = equalii(n, gcoeff(I,1,1))? ne: powii(gcoeff(I,1,1), e);
1287 175581 : N = mulii(N, ne);
1288 175581 : d = mulii(d, de);
1289 : }
1290 1233029 : *pd = d; return N;
1291 : }
1292 :
1293 : static GEN
1294 1394160 : get_pr_lists(GEN FB, long N, int list_pr)
1295 : {
1296 : GEN pr, L;
1297 1394160 : long i, l = lg(FB), p, pmax;
1298 :
1299 1394160 : pmax = 0;
1300 9564204 : for (i=1; i<l; i++)
1301 : {
1302 8170044 : pr = gel(FB,i); p = pr_get_smallp(pr);
1303 8170044 : if (p > pmax) pmax = p;
1304 : }
1305 1394160 : L = const_vec(pmax, NULL);
1306 1394160 : if (list_pr)
1307 : {
1308 0 : for (i=1; i<l; i++)
1309 : {
1310 0 : pr = gel(FB,i); p = pr_get_smallp(pr);
1311 0 : if (!L[p]) gel(L,p) = vectrunc_init(N+1);
1312 0 : vectrunc_append(gel(L,p), pr);
1313 : }
1314 0 : for (p=1; p<=pmax; p++)
1315 0 : if (L[p]) gen_sort_inplace(gel(L,p), (void*)&cmp_prime_over_p,
1316 : &cmp_nodata, NULL);
1317 : }
1318 : else
1319 : {
1320 9564204 : for (i=1; i<l; i++)
1321 : {
1322 8170044 : pr = gel(FB,i); p = pr_get_smallp(pr);
1323 8170044 : if (!L[p]) gel(L,p) = vecsmalltrunc_init(N+1);
1324 8170044 : vecsmalltrunc_append(gel(L,p), i);
1325 : }
1326 : }
1327 1394160 : return L;
1328 : }
1329 :
1330 : /* recover FB, LV, iLP, KCZ from Vbase */
1331 : static GEN
1332 1394160 : recover_partFB(FB_t *F, GEN Vbase, long N)
1333 : {
1334 1394160 : GEN FB, LV, iLP, L = get_pr_lists(Vbase, N, 0);
1335 1394160 : long l = lg(L), p, ip, i;
1336 :
1337 1394160 : i = ip = 0;
1338 1394160 : FB = cgetg(l, t_VECSMALL);
1339 1394160 : iLP= cgetg(l, t_VECSMALL);
1340 1394160 : LV = cgetg(l, t_VEC);
1341 20457684 : for (p = 2; p < l; p++)
1342 : {
1343 19063524 : if (!L[p]) continue;
1344 4458974 : FB[++i] = p;
1345 4458974 : gel(LV,p) = vecpermute(Vbase, gel(L,p));
1346 4458974 : iLP[p]= ip; ip += lg(gel(L,p))-1;
1347 : }
1348 1394160 : F->KCZ = i;
1349 1394160 : F->KC = ip;
1350 1394160 : F->FB = FB; setlg(FB, i+1);
1351 1394160 : F->prodZ = zv_prod_Z(F->FB);
1352 1394160 : F->LV = LV;
1353 1394160 : F->iLP= iLP; return L;
1354 : }
1355 :
1356 : /* add v^e to factorization */
1357 : static void
1358 2933613 : add_to_fact(long v, long e, FACT *fact)
1359 : {
1360 2933613 : long i, n = fact[0].pr;
1361 9984708 : for (i=1; i<=n; i++)
1362 7581454 : if (fact[i].pr == v) { fact[i].ex += e; return; }
1363 2403254 : store(v, e, fact);
1364 : }
1365 : static void
1366 0 : inv_fact(FACT *fact)
1367 : {
1368 0 : long i, n = fact[0].pr;
1369 0 : for (i=1; i<=n; i++) fact[i].ex = -fact[i].ex;
1370 0 : }
1371 :
1372 : /* L (small) list of primes above the same p including pr. Return pr index */
1373 : static int
1374 3426 : pr_index(GEN L, GEN pr)
1375 : {
1376 3426 : long j, l = lg(L);
1377 3426 : GEN al = pr_get_gen(pr);
1378 3426 : for (j=1; j<l; j++)
1379 3426 : if (ZV_equal(al, pr_get_gen(gel(L,j)))) return j;
1380 0 : pari_err_BUG("codeprime");
1381 : return 0; /* LCOV_EXCL_LINE */
1382 : }
1383 :
1384 : static long
1385 3426 : Vbase_to_FB(FB_t *F, GEN pr)
1386 : {
1387 3426 : long p = pr_get_smallp(pr);
1388 3426 : return F->iLP[p] + pr_index(gel(F->LV,p), pr);
1389 : }
1390 :
1391 : /* x, y 2 extended ideals whose first component is an integral HNF and second
1392 : * a famat */
1393 : static GEN
1394 3523 : idealHNF_mulred(GEN nf, GEN x, GEN y)
1395 : {
1396 3523 : GEN A = idealHNF_mul(nf, gel(x,1), gel(y,1));
1397 3523 : GEN F = famat_mul_shallow(gel(x,2), gel(y,2));
1398 3523 : return idealred(nf, mkvec2(A, F));
1399 : }
1400 : /* idealred(x * pr^n), n > 0 is small, x extended ideal. Reduction in order to
1401 : * avoid prec pb: don't let id become too large as lgsub increases */
1402 : static GEN
1403 4761 : idealmulpowprime2(GEN nf, GEN x, GEN pr, ulong n)
1404 : {
1405 4761 : GEN A = idealmulpowprime(nf, gel(x,1), pr, utoipos(n));
1406 4761 : return mkvec2(A, gel(x,2));
1407 : }
1408 : static GEN
1409 65868 : init_famat(GEN x) { return mkvec2(x, trivial_fact()); }
1410 : /* optimized idealfactorback + reduction; z = init_famat() */
1411 : static GEN
1412 28784 : genback(GEN z, GEN nf, GEN P, GEN E)
1413 : {
1414 28784 : long i, l = lg(E);
1415 28784 : GEN I = NULL;
1416 76687 : for (i = 1; i < l; i++)
1417 47903 : if (signe(gel(E,i)))
1418 : {
1419 : GEN J;
1420 32307 : gel(z,1) = gel(P,i);
1421 32307 : J = idealpowred(nf, z, gel(E,i));
1422 32307 : I = I? idealHNF_mulred(nf, I, J): J;
1423 : }
1424 28784 : return I; /* != NULL since a generator */
1425 : }
1426 :
1427 : static GEN
1428 1254117 : SPLIT_i(FB_t *F, GEN nf, GEN G, GEN x, GEN xred, GEN Nx, FACT *fact)
1429 : {
1430 1254117 : pari_sp av = avma;
1431 1254117 : GEN L = idealpseudominvec(xred, G);
1432 1254117 : long k, l = lg(L);
1433 1338064 : for(k = 1; k < l; k++)
1434 1321843 : if (factorgen(F, nf, x, Nx, gel(L,k), fact)) return gel(L,k);
1435 16221 : return gc_NULL(av);
1436 : }
1437 : /* return famat y (principal ideal) such that y / x is smooth [wrt Vbase] */
1438 : static GEN
1439 1410505 : SPLIT(FB_t *F, GEN nf, GEN x, GEN Vbase, FACT *fact)
1440 : {
1441 1410505 : GEN vecG, ex, y, x0, Nx = ZM_det_triangular(x);
1442 : long nbtest_lim, nbtest, i, j, ru, lgsub;
1443 : pari_sp av;
1444 :
1445 : /* try without reduction if x is small */
1446 2820805 : if (expi(gcoeff(x,1,1)) < 100 &&
1447 1582909 : can_factor(F, nf, x, NULL, Nx, fact)) return NULL;
1448 1237896 : if ((y = SPLIT_i(F, nf, nf_get_roundG(nf), x, x, Nx, fact))) return y;
1449 :
1450 : /* reduce in various directions */
1451 8866 : ru = lg(nf_get_roots(nf));
1452 8866 : vecG = cgetg(ru, t_VEC);
1453 14471 : for (j=1; j<ru; j++)
1454 : {
1455 12667 : gel(vecG,j) = nf_get_Gtwist1(nf, j);
1456 12667 : if ((y = SPLIT_i(F, nf, gel(vecG,j), x, x, Nx, fact))) return y;
1457 : }
1458 :
1459 : /* tough case, multiply by random products */
1460 1804 : lgsub = 3; nbtest = 1; nbtest_lim = 4;
1461 1804 : ex = cgetg(lgsub, t_VECSMALL);
1462 1804 : x0 = init_famat(x);
1463 : for(;;)
1464 678 : {
1465 2482 : GEN Ired, I, NI, id = x0;
1466 2482 : av = avma;
1467 2482 : if (DEBUGLEVEL>2) err_printf("# ideals tried = %ld\n",nbtest);
1468 7558 : for (i=1; i<lgsub; i++)
1469 : {
1470 5076 : ex[i] = random_bits(RANDOM_BITS);
1471 5076 : if (ex[i]) id = idealmulpowprime2(nf, id, gel(Vbase,i), ex[i]);
1472 : }
1473 2482 : if (id == x0) continue;
1474 : /* I^(-1) * \prod Vbase[i]^ex[i] = (id[2]) / x */
1475 :
1476 2482 : I = gel(id,1); NI = ZM_det_triangular(I);
1477 2482 : if (can_factor(F, nf, I, NULL, NI, fact))
1478 : {
1479 0 : inv_fact(fact); /* I^(-1) */
1480 0 : for (i=1; i<lgsub; i++)
1481 0 : if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
1482 0 : return gel(id,2);
1483 : }
1484 2482 : Ired = ru == 2? I: ZM_lll(I, 0.99, LLL_INPLACE);
1485 4232 : for (j=1; j<ru; j++)
1486 3554 : if ((y = SPLIT_i(F, nf, gel(vecG,j), I, Ired, NI, fact)))
1487 : {
1488 5433 : for (i=1; i<lgsub; i++)
1489 3629 : if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
1490 1804 : return famat_mul_shallow(gel(id,2), y);
1491 : }
1492 678 : set_avma(av);
1493 678 : if (++nbtest > nbtest_lim)
1494 : {
1495 21 : nbtest = 0;
1496 21 : if (++lgsub < minss(8, lg(Vbase)-1))
1497 : {
1498 21 : nbtest_lim <<= 1;
1499 21 : ex = cgetg(lgsub, t_VECSMALL);
1500 : }
1501 0 : else nbtest_lim = LONG_MAX; /* don't increase further */
1502 21 : if (DEBUGLEVEL>2) err_printf("SPLIT: increasing factor base [%ld]\n",lgsub);
1503 : }
1504 : }
1505 : }
1506 :
1507 : INLINE GEN
1508 1399073 : bnf_get_W(GEN bnf) { return gel(bnf,1); }
1509 : INLINE GEN
1510 2788214 : bnf_get_B(GEN bnf) { return gel(bnf,2); }
1511 : INLINE GEN
1512 2823084 : bnf_get_C(GEN bnf) { return gel(bnf,4); }
1513 : INLINE GEN
1514 1394180 : bnf_get_vbase(GEN bnf) { return gel(bnf,5); }
1515 : INLINE GEN
1516 1394097 : bnf_get_Ur(GEN bnf) { return gmael(bnf,9,1); }
1517 : INLINE GEN
1518 271834 : bnf_get_ga(GEN bnf) { return gmael(bnf,9,2); }
1519 : INLINE GEN
1520 276790 : bnf_get_GD(GEN bnf) { return gmael(bnf,9,3); }
1521 :
1522 : /* Return y (as an elt of K or a t_MAT representing an elt in Z[K])
1523 : * such that x / (y) is smooth and store the exponents of its factorization
1524 : * on g_W and g_B in Wex / Bex; return NULL for y = 1 */
1525 : static GEN
1526 1394097 : split_ideal(GEN bnf, GEN x, GEN *pWex, GEN *pBex)
1527 : {
1528 1394097 : GEN L, y, Vbase = bnf_get_vbase(bnf);
1529 1394097 : GEN Wex, W = bnf_get_W(bnf);
1530 1394097 : GEN Bex, B = bnf_get_B(bnf);
1531 : long p, j, i, l, nW, nB;
1532 : FACT *fact;
1533 : FB_t F;
1534 :
1535 1394097 : L = recover_partFB(&F, Vbase, lg(x)-1);
1536 1394097 : fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
1537 1394097 : y = SPLIT(&F, bnf_get_nf(bnf), x, Vbase, fact);
1538 1394097 : nW = lg(W)-1; *pWex = Wex = zero_zv(nW);
1539 1394097 : nB = lg(B)-1; *pBex = Bex = zero_zv(nB); l = lg(F.FB);
1540 1394097 : p = j = 0; /* -Wall */
1541 2094892 : for (i = 1; i <= fact[0].pr; i++)
1542 : { /* decode index C = ip+j --> (p,j) */
1543 700795 : long a, b, t, C = fact[i].pr;
1544 1954486 : for (t = 1; t < l; t++)
1545 : {
1546 1866737 : long q = F.FB[t], k = C - F.iLP[q];
1547 1866737 : if (k <= 0) break;
1548 1253691 : p = q;
1549 1253691 : j = k;
1550 : }
1551 700795 : a = gel(L, p)[j];
1552 700795 : b = a - nW;
1553 700795 : if (b <= 0) Wex[a] = y? -fact[i].ex: fact[i].ex;
1554 547245 : else Bex[b] = y? -fact[i].ex: fact[i].ex;
1555 : }
1556 1394097 : return y;
1557 : }
1558 :
1559 : GEN
1560 1109359 : init_red_mod_units(GEN bnf, long prec)
1561 : {
1562 1109359 : GEN s = gen_0, p1,s1,mat, logfu = bnf_get_logfu(bnf);
1563 1109359 : long i,j, RU = lg(logfu);
1564 :
1565 1109359 : if (RU == 1) return NULL;
1566 1109359 : mat = cgetg(RU,t_MAT);
1567 2510219 : for (j=1; j<RU; j++)
1568 : {
1569 1400860 : p1 = cgetg(RU+1,t_COL); gel(mat,j) = p1;
1570 1400860 : s1 = gen_0;
1571 3468968 : for (i=1; i<RU; i++)
1572 : {
1573 2068108 : gel(p1,i) = real_i(gcoeff(logfu,i,j));
1574 2068108 : s1 = mpadd(s1, mpsqr(gel(p1,i)));
1575 : }
1576 1400860 : gel(p1,RU) = gen_0; if (mpcmp(s1,s) > 0) s = s1;
1577 : }
1578 1109359 : s = gsqrt(gmul2n(s,RU),prec);
1579 1109359 : if (expo(s) < 27) s = utoipos(1UL << 27);
1580 1109359 : return mkvec2(mat, s);
1581 : }
1582 :
1583 : /* z computed above. Return unit exponents that would reduce col (arch) */
1584 : GEN
1585 1109359 : red_mod_units(GEN col, GEN z)
1586 : {
1587 : long i,RU;
1588 : GEN x,mat,N2;
1589 :
1590 1109359 : if (!z) return NULL;
1591 1109359 : mat= gel(z,1);
1592 1109359 : N2 = gel(z,2);
1593 1109359 : RU = lg(mat); x = cgetg(RU+1,t_COL);
1594 2510219 : for (i=1; i<RU; i++) gel(x,i) = real_i(gel(col,i));
1595 1109359 : gel(x,RU) = N2;
1596 1109359 : x = lll(shallowconcat(mat,x));
1597 1109359 : if (typ(x) != t_MAT || lg(x) <= RU) return NULL;
1598 1109359 : x = gel(x,RU);
1599 1109359 : if (signe(gel(x,RU)) < 0) x = gneg_i(x);
1600 1109359 : if (!gequal1(gel(x,RU))) pari_err_BUG("red_mod_units");
1601 1109359 : setlg(x,RU); return x;
1602 : }
1603 :
1604 : static GEN
1605 2252458 : add(GEN a, GEN t) { return a = a? RgC_add(a,t): t; }
1606 :
1607 : /* [x] archimedian components, A column vector. return [x] A */
1608 : static GEN
1609 2059038 : act_arch(GEN A, GEN x)
1610 : {
1611 : GEN a;
1612 2059038 : long i,l = lg(A), tA = typ(A);
1613 2059038 : if (tA == t_MAT)
1614 : { /* assume lg(x) >= l */
1615 192483 : a = cgetg(l, t_MAT);
1616 282341 : for (i=1; i<l; i++) gel(a,i) = act_arch(gel(A,i), x);
1617 192483 : return a;
1618 : }
1619 1866555 : if (l==1) return cgetg(1, t_COL);
1620 1866555 : a = NULL;
1621 1866555 : if (tA == t_VECSMALL)
1622 : {
1623 7209388 : for (i=1; i<l; i++)
1624 : {
1625 5976359 : long c = A[i];
1626 5976359 : if (c) a = add(a, gmulsg(c, gel(x,i)));
1627 : }
1628 : }
1629 : else
1630 : { /* A a t_COL of t_INT. Assume lg(A)==lg(x) */
1631 1384294 : for (i=1; i<l; i++)
1632 : {
1633 750768 : GEN c = gel(A,i);
1634 750768 : if (signe(c)) a = add(a, gmul(c, gel(x,i)));
1635 : }
1636 : }
1637 1866555 : return a? a: zerocol(lgcols(x)-1);
1638 : }
1639 : /* act_arch(matdiagonal(v), x) */
1640 : static GEN
1641 64161 : diagact_arch(GEN v, GEN x)
1642 : {
1643 64161 : long i, l = lg(v);
1644 64161 : GEN a = cgetg(l, t_MAT);
1645 93015 : for (i = 1; i < l; i++) gel(a,i) = gmul(gel(x,i), gel(v,i));
1646 64161 : return a;
1647 : }
1648 :
1649 : static long
1650 1412433 : prec_arch(GEN bnf)
1651 : {
1652 1412433 : GEN a = bnf_get_C(bnf);
1653 1412433 : long i, l = lg(a), prec;
1654 :
1655 1412433 : for (i=1; i<l; i++)
1656 1412349 : if ( (prec = gprecision(gel(a,i))) ) return prec;
1657 84 : return DEFAULTPREC;
1658 : }
1659 :
1660 : static long
1661 3857 : needed_bitprec(GEN x)
1662 : {
1663 3857 : long i, e = 0, l = lg(x);
1664 22552 : for (i = 1; i < l; i++)
1665 : {
1666 18695 : GEN c = gel(x,i);
1667 18695 : long f = gexpo(c) - gprecision(c);
1668 18695 : if (f > e) e = f;
1669 : }
1670 3857 : return e;
1671 : }
1672 :
1673 : /* col = archimedian components of x, Nx its norm, dx a multiple of its
1674 : * denominator. Return x or NULL (fail) */
1675 : GEN
1676 1238642 : isprincipalarch(GEN bnf, GEN col, GEN kNx, GEN e, GEN dx, long *pe)
1677 : {
1678 : GEN nf, x, y, logfu, s, M;
1679 1238642 : long N, prec = gprecision(col);
1680 1238642 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf); M = nf_get_M(nf);
1681 1238642 : if (!prec) prec = prec_arch(bnf);
1682 1238642 : *pe = 128;
1683 1238642 : logfu = bnf_get_logfu(bnf);
1684 1238642 : N = nf_get_degree(nf);
1685 1238642 : if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
1686 1238642 : if (lg(col) > 2)
1687 : { /* reduce mod units */
1688 1109359 : GEN u, z = init_red_mod_units(bnf,prec);
1689 1109359 : if (!(u = red_mod_units(col,z))) return NULL;
1690 1109359 : col = RgC_add(col, RgM_RgC_mul(logfu, u));
1691 1109359 : if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
1692 : }
1693 1238642 : s = divru(mulir(e, glog(kNx,prec)), N);
1694 1238642 : col = fixarch(col, s, nf_get_r1(nf));
1695 1238642 : if (RgC_expbitprec(col) >= 0) return NULL;
1696 1238068 : col = gexp(col, prec);
1697 : /* d.alpha such that x = alpha \prod gj^ej */
1698 1238068 : x = RgM_solve_realimag(M,col); if (!x) return NULL;
1699 1238068 : x = RgC_Rg_mul(x, dx);
1700 1238068 : y = grndtoi(x, pe);
1701 1238068 : if (*pe > -5) { *pe = needed_bitprec(x); return NULL; }
1702 1234211 : return RgC_Rg_div(y, dx);
1703 : }
1704 :
1705 : /* y = C \prod g[i]^e[i] ? */
1706 : static int
1707 1230087 : fact_ok(GEN nf, GEN y, GEN C, GEN g, GEN e)
1708 : {
1709 1230087 : pari_sp av = avma;
1710 1230087 : long i, c = lg(e);
1711 1230087 : GEN z = C? C: gen_1;
1712 1507111 : for (i=1; i<c; i++)
1713 277024 : if (signe(gel(e,i))) z = idealmul(nf, z, idealpow(nf, gel(g,i), gel(e,i)));
1714 1230087 : if (typ(z) != t_MAT) z = idealhnf_shallow(nf,z);
1715 1230087 : if (typ(y) != t_MAT) y = idealhnf_shallow(nf,y);
1716 1230087 : return gc_bool(av, ZM_equal(y,z));
1717 : }
1718 : static GEN
1719 1394097 : ZV_divrem(GEN A, GEN B, GEN *pR)
1720 : {
1721 1394097 : long i, l = lg(A);
1722 1394097 : GEN Q = cgetg(l, t_COL), R = cgetg(l, t_COL);
1723 1900981 : for (i = 1; i < l; i++) gel(Q,i) = truedvmdii(gel(A,i), gel(B,i), &gel(R,i));
1724 1394097 : *pR = R; return Q;
1725 : }
1726 :
1727 : static GEN
1728 1394097 : Ur_ZC_mul(GEN bnf, GEN v)
1729 : {
1730 1394097 : GEN w, U = bnf_get_Ur(bnf);
1731 1394097 : long i, l = lg(bnf_get_cyc(bnf)); /* may be < lgcols(U) */
1732 :
1733 1394097 : w = cgetg(l, t_COL);
1734 1900981 : for (i = 1; i < l; i++) gel(w,i) = ZMrow_ZC_mul(U, v, i);
1735 1394097 : return w;
1736 : }
1737 :
1738 : static GEN
1739 7332 : ZV_mul(GEN x, GEN y)
1740 : {
1741 7332 : long i, l = lg(x);
1742 7332 : GEN z = cgetg(l, t_COL);
1743 31962 : for (i = 1; i < l; i++) gel(z,i) = mulii(gel(x,i), gel(y,i));
1744 7332 : return z;
1745 : }
1746 : static int
1747 1229586 : dump_gen(GEN SUnits, GEN x, long flag)
1748 : {
1749 : GEN d;
1750 : long e;
1751 1229586 : if (!(flag & nf_GENMAT) || !SUnits) return 0;
1752 274520 : e = gexpo(gel(SUnits,2)); if (e > 64) return 0; /* U large */
1753 274424 : x = Q_remove_denom(x, &d);
1754 274424 : return (d && expi(d) > 32) || gexpo(x) > 32;
1755 : }
1756 :
1757 : /* assume x in HNF; cf class_group_gen for notations. Return NULL iff
1758 : * flag & nf_FORCE and computation of principal ideal generator fails */
1759 : static GEN
1760 1410631 : isprincipalall(GEN bnf, GEN x, long *pprec, long flag)
1761 : {
1762 : GEN xar, Wex, Bex, gen, xc, col, A, Q, Rmax, R, UA, SUnits;
1763 1410631 : GEN C = bnf_get_C(bnf), nf = bnf_get_nf(bnf), cyc = bnf_get_cyc(bnf);
1764 : long nB, nW, e;
1765 :
1766 1410631 : if (lg(cyc) == 1 && !(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL)))
1767 4732 : return cgetg(1,t_COL);
1768 1405899 : if (lg(x) == 2)
1769 : { /* nf = Q */
1770 84 : col = gel(x,1);
1771 84 : if (flag & nf_GENMAT) col = Q_to_famat(gel(col,1));
1772 84 : return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(cgetg(1,t_COL), col);
1773 : }
1774 :
1775 1405815 : x = Q_primitive_part(x, &xc);
1776 1405815 : if (equali1(gcoeff(x,1,1))) /* trivial ideal */
1777 : {
1778 11718 : R = zerocol(lg(cyc)-1);
1779 11718 : if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
1780 11669 : if (flag & nf_GEN_IF_PRINCIPAL)
1781 6832 : return scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
1782 4837 : if (flag & nf_GENMAT)
1783 2191 : col = xc? Q_to_famat(xc): trivial_fact();
1784 : else
1785 2646 : col = scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
1786 4837 : return mkvec2(R, col);
1787 : }
1788 1394097 : xar = split_ideal(bnf, x, &Wex, &Bex);
1789 : /* x = g_W Wex + g_B Bex + [xar] = g_W (Wex - B*Bex) + [xar] + [C_B]Bex */
1790 1394097 : A = zc_to_ZC(Wex); nB = lg(Bex)-1;
1791 1394097 : if (nB) A = ZC_sub(A, ZM_zc_mul(bnf_get_B(bnf), Bex));
1792 1394097 : UA = Ur_ZC_mul(bnf, A);
1793 1394097 : Q = ZV_divrem(UA, cyc, &R);
1794 : /* g_W (Wex - B*Bex) = G Ur A - [ga]A = G R + [GD]Q - [ga]A
1795 : * Finally: x = G R + [xar] + [C_B]Bex + [GD]Q - [ga]A */
1796 1394097 : if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
1797 1233626 : if ((flag & nf_GEN_IF_PRINCIPAL) && !ZV_equal0(R)) return gen_0;
1798 :
1799 1233619 : nW = lg(Wex)-1;
1800 1233619 : gen = bnf_get_gen(bnf);
1801 1233619 : col = NULL;
1802 1233619 : SUnits = bnf_get_sunits(bnf);
1803 1233619 : Rmax = NULL; if (lg(R) > 1) Rmax = gel(R,vecindexmax(R));
1804 1233619 : if (!Rmax || abscmpiu(Rmax, 4 * (*pprec)) < 0)
1805 1233029 : { /* q = N (x / prod gj^ej) = N(alpha), denom(alpha) | d */
1806 1233029 : GEN d, q = gdiv(ZM_det_triangular(x), get_norm_fact(gen, R, &d));
1807 1233029 : col = xar? nf_cxlog(nf, xar, *pprec): NULL;
1808 1233029 : if (nB) col = add(col, act_arch(Bex, nW? vecslice(C,nW+1,lg(C)-1): C));
1809 1233029 : if (nW) col = add(col, RgC_sub(act_arch(Q, bnf_get_GD(bnf)),
1810 : act_arch(A, bnf_get_ga(bnf))));
1811 1233029 : col = isprincipalarch(bnf, col, q, gen_1, d, &e);
1812 1233029 : if (col && (dump_gen(SUnits, col, flag)
1813 1229586 : || !fact_ok(nf,x, col,gen,R))) col = NULL;
1814 : }
1815 : else
1816 : { /* initialize e, ensure Rmax >= 4 (*pprec + e). If e doesn't fit in ulong,
1817 : * leave at 0 */
1818 590 : e = itou_or_0(shifti(Rmax, -2));
1819 590 : if (e) e -= *pprec;
1820 : }
1821 1233619 : if (!col && (flag & nf_GENMAT))
1822 : {
1823 8073 : if (SUnits)
1824 : {
1825 7584 : GEN X = gel(SUnits,1), U = gel(SUnits,2), C = gel(SUnits,3);
1826 7584 : GEN v = gel(bnf,9), Ge = gel(v,4), M1 = gel(v,5), M2 = gel(v,6);
1827 7584 : GEN z = NULL, F = NULL;
1828 7584 : if (nB)
1829 : {
1830 7584 : GEN C2 = nW? vecslice(C, nW+1, lg(C)-1): C;
1831 7584 : z = ZM_zc_mul(C2, Bex);
1832 : }
1833 7584 : if (nW)
1834 : { /* [GD]Q - [ga]A = ([X]M1 - [Ge]D) Q - ([X]M2 - [Ge]Ur) A */
1835 7332 : GEN C1 = vecslice(C, 1, nW);
1836 7332 : GEN v = ZC_sub(ZM_ZC_mul(M1,Q), ZM_ZC_mul(M2,A));
1837 7332 : z = add(z, ZM_ZC_mul(C1, v));
1838 7332 : F = famat_reduce(famatV_factorback(Ge, ZC_sub(UA, ZV_mul(cyc,Q))));
1839 7332 : if (lgcols(F) == 1) F = NULL;
1840 : }
1841 : /* reduce modulo units and Q^* */
1842 7584 : if (lg(U) != 1) z = ZC_sub(z, ZM_ZC_mul(U, RgM_Babai(U,z)));
1843 7584 : col = mkmat2(X, z);
1844 7584 : if (F) col = famat_mul_shallow(col, F);
1845 7584 : col = famat_remove_trivial(col);
1846 7584 : if (xar) col = famat_mul_shallow(col, xar);
1847 : }
1848 489 : else if (!ZV_equal0(R))
1849 : { /* in case isprincipalfact calls bnfinit() due to prec trouble...*/
1850 483 : GEN y = isprincipalfact(bnf, x, gen, ZC_neg(R), flag);
1851 483 : if (typ(y) != t_VEC) return y;
1852 483 : col = gel(y,2);
1853 : }
1854 : }
1855 1233619 : if (col)
1856 : { /* add back missing content */
1857 1233529 : if (typ(col) == t_MAT)
1858 8067 : { if (xc) col = famat_mul_shallow(col, xc); }
1859 1225462 : else if (flag & nf_GENMAT)
1860 : {
1861 : GEN c;
1862 1211763 : if (RgV_isscalar(col))
1863 3661 : col = Q_to_famat(mul_content(xc, gel(col,1)));
1864 : else
1865 : {
1866 1208102 : col = Q_primitive_part(col, &c);
1867 1208102 : col = to_famat_shallow(col, gen_1);
1868 1208102 : xc = mul_content(xc, c);
1869 1208102 : if (xc) col = famat_mul(col, Q_to_famat(xc));
1870 : }
1871 : }
1872 : else
1873 13699 : { if (xc) col = RgC_Rg_mul(col,xc); }
1874 : }
1875 : else
1876 : {
1877 90 : if (e < 0) e = 0;
1878 90 : *pprec += nbits2extraprec(e + 128);
1879 90 : if (flag & nf_FORCE)
1880 : {
1881 76 : if (DEBUGLEVEL)
1882 0 : pari_warn(warner,"precision too low for generators, e = %ld",e);
1883 76 : return NULL;
1884 : }
1885 14 : pari_warn(warner,"precision too low for generators, not given");
1886 14 : col = cgetg(1, t_COL);
1887 : }
1888 1233543 : return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(R, col);
1889 : }
1890 :
1891 : static GEN
1892 464919 : triv_gen(GEN bnf, GEN x, long flag)
1893 : {
1894 464919 : pari_sp av = avma;
1895 464919 : GEN nf = bnf_get_nf(bnf);
1896 : long c;
1897 464919 : if (flag & nf_GEN_IF_PRINCIPAL)
1898 : {
1899 7 : if (!(flag & nf_GENMAT)) return algtobasis(nf,x);
1900 7 : x = nf_to_scalar_or_basis(nf,x);
1901 7 : if (typ(x) == t_INT && is_pm1(x)) return trivial_fact();
1902 0 : return gc_GEN(av, to_famat_shallow(x, gen_1));
1903 : }
1904 464912 : c = lg(bnf_get_cyc(bnf)) - 1;
1905 464912 : if (flag & nf_GENMAT)
1906 455308 : retmkvec2(zerocol(c), to_famat_shallow(algtobasis(nf,x), gen_1));
1907 9604 : if (flag & nf_GEN)
1908 28 : retmkvec2(zerocol(c), algtobasis(nf,x));
1909 9576 : return zerocol(c);
1910 : }
1911 :
1912 : GEN
1913 1843140 : bnfisprincipal0(GEN bnf,GEN x,long flag)
1914 : {
1915 1843140 : pari_sp av = avma;
1916 : GEN c, nf;
1917 : long pr;
1918 :
1919 1843140 : bnf = checkbnf(bnf);
1920 1843140 : nf = bnf_get_nf(bnf);
1921 1843140 : switch( idealtyp(&x, NULL) )
1922 : {
1923 59080 : case id_PRINCIPAL:
1924 59080 : if (gequal0(x)) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
1925 59080 : return triv_gen(bnf, x, flag);
1926 1760400 : case id_PRIME:
1927 1760400 : if (pr_is_inert(x)) return triv_gen(bnf, pr_get_p(x), flag);
1928 1354568 : x = pr_hnf(nf, x);
1929 1354568 : break;
1930 23660 : case id_MAT:
1931 23660 : if (lg(x)==1) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
1932 23660 : if (nf_get_degree(nf) != lg(x)-1)
1933 0 : pari_err_TYPE("idealtyp [dimension != degree]", x);
1934 : }
1935 1378228 : pr = prec_arch(bnf); /* precision of unit matrix */
1936 1378228 : c = getrand();
1937 : for (;;)
1938 6 : {
1939 1378234 : pari_sp av1 = avma;
1940 1378234 : GEN y = isprincipalall(bnf,x,&pr,flag);
1941 1378234 : if (y) return gc_GEN(av, y);
1942 :
1943 6 : if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",pr);
1944 6 : set_avma(av1); bnf = bnfnewprec_shallow(bnf,pr); setrand(c);
1945 : }
1946 : }
1947 : GEN
1948 174779 : isprincipal(GEN bnf,GEN x) { return bnfisprincipal0(bnf,x,0); }
1949 :
1950 : /* FIXME: OBSOLETE */
1951 : GEN
1952 0 : isprincipalgen(GEN bnf,GEN x)
1953 0 : { return bnfisprincipal0(bnf,x,nf_GEN); }
1954 : GEN
1955 0 : isprincipalforce(GEN bnf,GEN x)
1956 0 : { return bnfisprincipal0(bnf,x,nf_FORCE); }
1957 : GEN
1958 0 : isprincipalgenforce(GEN bnf,GEN x)
1959 0 : { return bnfisprincipal0(bnf,x,nf_GEN | nf_FORCE); }
1960 :
1961 : /* lg(u) > 1 */
1962 : static int
1963 105 : RgV_is1(GEN u) { return isint1(gel(u,1)) && RgV_isscalar(u); }
1964 : static GEN
1965 32327 : add_principal_part(GEN nf, GEN u, GEN v, long flag)
1966 : {
1967 32327 : if (flag & nf_GENMAT)
1968 14540 : return (typ(u) == t_COL && RgV_is1(u))? v: famat_mul_shallow(v,u);
1969 : else
1970 17787 : return nfmul(nf, v, u);
1971 : }
1972 :
1973 : #if 0
1974 : /* compute C prod P[i]^e[i], e[i] >=0 for all i. C may be NULL (omitted)
1975 : * e destroyed ! */
1976 : static GEN
1977 : expand(GEN nf, GEN C, GEN P, GEN e)
1978 : {
1979 : long i, l = lg(e), done = 1;
1980 : GEN id = C;
1981 : for (i=1; i<l; i++)
1982 : {
1983 : GEN ei = gel(e,i);
1984 : if (signe(ei))
1985 : {
1986 : if (mod2(ei)) id = id? idealmul(nf, id, gel(P,i)): gel(P,i);
1987 : ei = shifti(ei,-1);
1988 : if (signe(ei)) done = 0;
1989 : gel(e,i) = ei;
1990 : }
1991 : }
1992 : if (id != C) id = idealred(nf, id);
1993 : if (done) return id;
1994 : return idealmulred(nf, id, idealsqr(nf, expand(nf,id,P,e)));
1995 : }
1996 : /* C is an extended ideal, possibly with C[1] = NULL */
1997 : static GEN
1998 : expandext(GEN nf, GEN C, GEN P, GEN e)
1999 : {
2000 : long i, l = lg(e), done = 1;
2001 : GEN A = gel(C,1);
2002 : for (i=1; i<l; i++)
2003 : {
2004 : GEN ei = gel(e,i);
2005 : if (signe(ei))
2006 : {
2007 : if (mod2(ei)) A = A? idealmul(nf, A, gel(P,i)): gel(P,i);
2008 : ei = shifti(ei,-1);
2009 : if (signe(ei)) done = 0;
2010 : gel(e,i) = ei;
2011 : }
2012 : }
2013 : if (A == gel(C,1))
2014 : A = C;
2015 : else
2016 : A = idealred(nf, mkvec2(A, gel(C,2)));
2017 : if (done) return A;
2018 : return idealmulred(nf, A, idealsqr(nf, expand(nf,A,P,e)));
2019 : }
2020 : #endif
2021 :
2022 : static GEN
2023 0 : expand(GEN nf, GEN C, GEN P, GEN e)
2024 : {
2025 0 : long i, l = lg(e);
2026 0 : GEN B, A = C;
2027 0 : for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
2028 0 : if (signe(gel(e,i)))
2029 : {
2030 0 : B = idealpowred(nf, gel(P,i), gel(e,i));
2031 0 : A = A? idealmulred(nf,A,B): B;
2032 : }
2033 0 : return A;
2034 : }
2035 : static GEN
2036 32348 : expandext(GEN nf, GEN C, GEN P, GEN e)
2037 : {
2038 32348 : long i, l = lg(e);
2039 32348 : GEN B, A = gel(C,1), C1 = A;
2040 96620 : for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
2041 64272 : if (signe(gel(e,i)))
2042 : {
2043 35370 : gel(C,1) = gel(P,i);
2044 35370 : B = idealpowred(nf, C, gel(e,i));
2045 35370 : A = A? idealmulred(nf,A,B): B;
2046 : }
2047 32348 : return A == C1? C: A;
2048 : }
2049 :
2050 : /* isprincipal for C * \prod P[i]^e[i] (C omitted if NULL) */
2051 : GEN
2052 32348 : isprincipalfact(GEN bnf, GEN C, GEN P, GEN e, long flag)
2053 : {
2054 32348 : const long gen = flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL);
2055 : long prec;
2056 32348 : pari_sp av = avma;
2057 32348 : GEN C0, Cext, c, id, nf = bnf_get_nf(bnf);
2058 :
2059 32348 : if (gen)
2060 : {
2061 14547 : Cext = (flag & nf_GENMAT)? trivial_fact()
2062 32348 : : mkpolmod(gen_1,nf_get_pol(nf));
2063 32348 : C0 = mkvec2(C, Cext);
2064 32348 : id = expandext(nf, C0, P, e);
2065 : } else {
2066 0 : Cext = NULL;
2067 0 : C0 = C;
2068 0 : id = expand(nf, C, P, e);
2069 : }
2070 32348 : if (id == C0) /* e = 0 */
2071 : {
2072 12477 : if (!C) return bnfisprincipal0(bnf, gen_1, flag);
2073 12463 : switch(typ(C))
2074 : {
2075 7 : case t_INT: case t_FRAC: case t_POL: case t_POLMOD: case t_COL:
2076 7 : return triv_gen(bnf, C, flag);
2077 : }
2078 12456 : C = idealhnf_shallow(nf,C);
2079 : }
2080 : else
2081 : {
2082 19871 : if (gen) { C = gel(id,1); Cext = gel(id,2); } else C = id;
2083 : }
2084 32327 : prec = prec_arch(bnf);
2085 32327 : c = getrand();
2086 : for (;;)
2087 70 : {
2088 32397 : pari_sp av1 = avma;
2089 32397 : GEN y = isprincipalall(bnf, C, &prec, flag);
2090 32397 : if (y)
2091 : {
2092 32327 : if (flag & nf_GEN_IF_PRINCIPAL)
2093 : {
2094 21182 : if (typ(y) == t_INT) return gc_NULL(av);
2095 21182 : y = add_principal_part(nf, y, Cext, flag);
2096 : }
2097 : else
2098 : {
2099 11145 : GEN u = gel(y,2);
2100 11145 : if (!gen || typ(y) != t_VEC) return gc_upto(av,y);
2101 11145 : if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
2102 : }
2103 32327 : return gc_GEN(av, y);
2104 : }
2105 70 : if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",prec);
2106 70 : set_avma(av1); bnf = bnfnewprec_shallow(bnf,prec); setrand(c);
2107 : }
2108 : }
2109 : GEN
2110 0 : isprincipalfact_or_fail(GEN bnf, GEN C, GEN P, GEN e)
2111 : {
2112 0 : const long flag = nf_GENMAT|nf_FORCE;
2113 : long prec;
2114 0 : pari_sp av = avma;
2115 0 : GEN u, y, id, C0, Cext, nf = bnf_get_nf(bnf);
2116 :
2117 0 : Cext = trivial_fact();
2118 0 : C0 = mkvec2(C, Cext);
2119 0 : id = expandext(nf, C0, P, e);
2120 0 : if (id == C0) /* e = 0 */
2121 0 : C = idealhnf_shallow(nf,C);
2122 : else {
2123 0 : C = gel(id,1); Cext = gel(id,2);
2124 : }
2125 0 : prec = prec_arch(bnf);
2126 0 : y = isprincipalall(bnf, C, &prec, flag);
2127 0 : if (!y) return gc_utoipos(av, prec);
2128 0 : u = gel(y,2);
2129 0 : if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
2130 0 : return gc_GEN(av, y);
2131 : }
2132 :
2133 : GEN
2134 162330 : nfsign_from_logarch(GEN LA, GEN invpi, GEN archp)
2135 : {
2136 162330 : long l = lg(archp), i;
2137 162330 : GEN y = cgetg(l, t_VECSMALL);
2138 162330 : pari_sp av = avma;
2139 :
2140 310072 : for (i=1; i<l; i++)
2141 : {
2142 147742 : GEN c = ground( gmul(imag_i(gel(LA,archp[i])), invpi) );
2143 147742 : y[i] = mpodd(c)? 1: 0;
2144 : }
2145 162330 : set_avma(av); return y;
2146 : }
2147 :
2148 : GEN
2149 240002 : nfsign_tu(GEN bnf, GEN archp)
2150 : {
2151 : long n;
2152 240002 : if (bnf_get_tuN(bnf) != 2) return cgetg(1, t_VECSMALL);
2153 172893 : n = archp? lg(archp) - 1: nf_get_r1(bnf_get_nf(bnf));
2154 172893 : return const_vecsmall(n, 1);
2155 : }
2156 : GEN
2157 241178 : nfsign_fu(GEN bnf, GEN archp)
2158 : {
2159 241178 : GEN invpi, y, A = bnf_get_logfu(bnf), nf = bnf_get_nf(bnf);
2160 241178 : long j = 1, RU = lg(A);
2161 :
2162 241178 : if (!archp) archp = identity_perm( nf_get_r1(nf) );
2163 241178 : invpi = invr( mppi(nf_get_prec(nf)) );
2164 241178 : y = cgetg(RU,t_MAT);
2165 403410 : for (j = 1; j < RU; j++)
2166 162232 : gel(y,j) = nfsign_from_logarch(gel(A,j), invpi, archp);
2167 241178 : return y;
2168 : }
2169 : GEN
2170 35 : nfsign_units(GEN bnf, GEN archp, int add_zu)
2171 : {
2172 35 : GEN sfu = nfsign_fu(bnf, archp);
2173 35 : return add_zu? vec_prepend(sfu, nfsign_tu(bnf, archp)): sfu;
2174 : }
2175 :
2176 : /* obsolete */
2177 : GEN
2178 7 : signunits(GEN bnf)
2179 : {
2180 : pari_sp av;
2181 : GEN S, y, nf;
2182 : long i, j, r1, r2;
2183 :
2184 7 : bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
2185 7 : nf_get_sign(nf, &r1,&r2);
2186 7 : S = zeromatcopy(r1, r1+r2-1); av = avma;
2187 7 : y = nfsign_fu(bnf, NULL);
2188 14 : for (j = 1; j < lg(y); j++)
2189 : {
2190 7 : GEN Sj = gel(S,j), yj = gel(y,j);
2191 21 : for (i = 1; i <= r1; i++) gel(Sj,i) = yj[i]? gen_m1: gen_1;
2192 : }
2193 7 : set_avma(av); return S;
2194 : }
2195 :
2196 : static GEN
2197 726035 : get_log_embed(REL_t *rel, GEN M, long RU, long R1, long prec)
2198 : {
2199 726035 : GEN arch, C, z = rel->m;
2200 : long i;
2201 726035 : arch = typ(z) == t_COL? RgM_RgC_mul(M, z): const_col(nbrows(M), z);
2202 726035 : C = cgetg(RU+1, t_COL); arch = glog(arch, prec);
2203 1653802 : for (i=1; i<=R1; i++) gel(C,i) = gel(arch,i);
2204 1565701 : for ( ; i<=RU; i++) gel(C,i) = gmul2n(gel(arch,i), 1);
2205 726035 : return C;
2206 : }
2207 : static GEN
2208 1017009 : rel_embed(REL_t *rel, FB_t *F, GEN embs, long ind, GEN M, long RU, long R1,
2209 : long prec)
2210 : {
2211 : GEN C, D, perm;
2212 : long i, n;
2213 1017009 : if (!rel->relaut) return get_log_embed(rel, M, RU, R1, prec);
2214 : /* image of another relation by automorphism */
2215 290974 : C = gel(embs, ind - rel->relorig);
2216 290974 : perm = gel(F->embperm, rel->relaut);
2217 290974 : D = cgetg_copy(C, &n);
2218 1216689 : for (i = 1; i < n; i++)
2219 : {
2220 925715 : long v = perm[i];
2221 925715 : gel(D,i) = (v > 0)? gel(C,v): conj_i(gel(C,-v));
2222 : }
2223 290974 : return D;
2224 : }
2225 : static GEN
2226 122198 : get_embs(FB_t *F, RELCACHE_t *cache, GEN nf, GEN embs, long PREC)
2227 : {
2228 122198 : long ru, j, k, l = cache->last - cache->chk + 1, r1 = nf_get_r1(nf);
2229 122198 : GEN M = nf_get_M(nf), nembs = cgetg(cache->last - cache->base+1, t_MAT);
2230 : REL_t *rel;
2231 :
2232 1513752 : for (k = 1; k <= cache->chk - cache->base; k++) gel(nembs,k) = gel(embs,k);
2233 122198 : embs = nembs; ru = nbrows(M);
2234 1126634 : for (j=1,rel = cache->chk + 1; j < l; rel++,j++,k++)
2235 1004436 : gel(embs,k) = rel_embed(rel, F, embs, k, M, ru, r1, PREC);
2236 122198 : return embs;
2237 : }
2238 : static void
2239 948718 : set_rel_alpha(REL_t *rel, GEN auts, GEN vA, long ind)
2240 : {
2241 : GEN u;
2242 948718 : if (!rel->relaut)
2243 674934 : u = rel->m;
2244 : else
2245 273784 : u = ZM_ZC_mul(gel(auts, rel->relaut), gel(vA, ind - rel->relorig));
2246 948718 : gel(vA, ind) = u;
2247 948718 : }
2248 : static GEN
2249 2329230 : set_fact(FB_t *F, FACT *fact, GEN e, long *pnz)
2250 : {
2251 2329230 : long i, n = fact[0].pr, nz = F->KC + 1;
2252 2329230 : GEN c = zero_Flv(F->KC);
2253 10950703 : for (i = 1; i <= n; i++)
2254 : {
2255 8621473 : long p = fact[i].pr;
2256 8621473 : if (p < nz) nz = p;
2257 8621473 : c[p] = fact[i].ex;
2258 : }
2259 2329230 : if (e)
2260 : {
2261 128060 : long l = lg(e);
2262 407805 : for (i = 1; i < l; i++)
2263 279745 : if (e[i]) { long v = F->subFB[i]; c[v] += e[i]; if (v < nz) nz = v; }
2264 : }
2265 2329230 : *pnz = nz; return c;
2266 : }
2267 :
2268 : /* Is cols already in the cache ? bs = index of first non zero coeff in cols
2269 : * General check for colinearity useless since exceedingly rare */
2270 : static int
2271 2982492 : already_known(RELCACHE_t *cache, long bs, GEN cols)
2272 : {
2273 : REL_t *r;
2274 2982492 : long l = lg(cols);
2275 221081716 : for (r = cache->last; r > cache->base; r--)
2276 218589111 : if (bs == r->nz)
2277 : {
2278 34812394 : GEN coll = r->R;
2279 34812394 : long b = bs;
2280 124616346 : while (b < l && cols[b] == coll[b]) b++;
2281 34812394 : if (b == l) return 1;
2282 : }
2283 2492605 : return 0;
2284 : }
2285 :
2286 : /* Add relation R to cache, nz = index of first non zero coeff in R.
2287 : * If relation is a linear combination of the previous ones, return 0.
2288 : * Otherwise, update basis and return > 0. Compute mod p (much faster)
2289 : * so some kernel vector might not be genuine. */
2290 : static int
2291 2986685 : add_rel_i(RELCACHE_t *cache, GEN R, long nz, GEN m, long orig, long aut, REL_t **relp, long mod_p)
2292 : {
2293 2986685 : long i, k, n = lg(R)-1;
2294 :
2295 2986685 : if (nz == n+1) { k = 0; goto ADD_REL; }
2296 2982492 : if (already_known(cache, nz, R)) return -1;
2297 2492605 : if (cache->last >= cache->base + cache->len) return 0;
2298 2492605 : if (DEBUGLEVEL>6)
2299 : {
2300 0 : err_printf("adding vector = %Ps\n",R);
2301 0 : err_printf("generators =\n%Ps\n", cache->basis);
2302 : }
2303 2492605 : if (cache->missing)
2304 : {
2305 2096174 : GEN a = leafcopy(R), basis = cache->basis;
2306 2096174 : k = lg(a);
2307 128246276 : do --k; while (!a[k]);
2308 7408536 : while (k)
2309 : {
2310 5782227 : GEN c = gel(basis, k);
2311 5782227 : if (c[k])
2312 : {
2313 5312362 : long ak = a[k];
2314 270689196 : for (i=1; i < k; i++) if (c[i]) a[i] = (a[i] + ak*(mod_p-c[i])) % mod_p;
2315 5312362 : a[k] = 0;
2316 133299162 : do --k; while (!a[k]); /* k cannot go below 0: codeword is a sentinel */
2317 : }
2318 : else
2319 : {
2320 469865 : ulong invak = Fl_inv(uel(a,k), mod_p);
2321 : /* Cleanup a */
2322 14002977 : for (i = k; i-- > 1; )
2323 : {
2324 13533112 : long j, ai = a[i];
2325 13533112 : c = gel(basis, i);
2326 13533112 : if (!ai || !c[i]) continue;
2327 237907 : ai = mod_p-ai;
2328 4507644 : for (j = 1; j < i; j++) if (c[j]) a[j] = (a[j] + ai*c[j]) % mod_p;
2329 237907 : a[i] = 0;
2330 : }
2331 : /* Insert a/a[k] as k-th column */
2332 469865 : c = gel(basis, k);
2333 14002977 : for (i = 1; i<k; i++) if (a[i]) c[i] = (a[i] * invak) % mod_p;
2334 469865 : c[k] = 1; a = c;
2335 : /* Cleanup above k */
2336 13796808 : for (i = k+1; i<n; i++)
2337 : {
2338 : long j, ck;
2339 13326943 : c = gel(basis, i);
2340 13326943 : ck = c[k];
2341 13326943 : if (!ck) continue;
2342 2764964 : ck = mod_p-ck;
2343 100012528 : for (j = 1; j < k; j++) if (a[j]) c[j] = (c[j] + ck*a[j]) % mod_p;
2344 2764964 : c[k] = 0;
2345 : }
2346 469865 : cache->missing--;
2347 469865 : break;
2348 : }
2349 : }
2350 : }
2351 : else
2352 396431 : k = (cache->last - cache->base) + 1;
2353 2492605 : if (k || cache->relsup > 0 || (m && !mod_p))
2354 : {
2355 : REL_t *rel;
2356 :
2357 989017 : ADD_REL:
2358 993210 : rel = ++cache->last;
2359 993210 : if (!k && cache->relsup && nz < n+1)
2360 : {
2361 122721 : cache->relsup--;
2362 122721 : k = (rel - cache->base) + cache->missing;
2363 : }
2364 993210 : rel->R = gclone(R);
2365 993210 : rel->m = m ? gclone(m) : NULL;
2366 993210 : rel->nz = nz;
2367 993210 : if (aut)
2368 : {
2369 288367 : rel->relorig = (rel - cache->base) - orig;
2370 288367 : rel->relaut = aut;
2371 : }
2372 : else
2373 704843 : rel->relaut = 0;
2374 993210 : if (relp) *relp = rel;
2375 993210 : if (DEBUGLEVEL) dbg_newrel(cache);
2376 : }
2377 2496798 : return k;
2378 : }
2379 :
2380 : /* m a t_INT or primitive t_COL */
2381 : static int
2382 2502441 : add_rel(RELCACHE_t *cache, FB_t *F, GEN R, long nz, GEN m, ulong mod_p)
2383 : {
2384 : REL_t *rel;
2385 : long k, l, reln;
2386 2502441 : const long lauts = lg(F->idealperm), KC = F->KC;
2387 :
2388 2502441 : k = add_rel_i(cache, R, nz, m, 0, 0, &rel, mod_p);
2389 2502441 : if (k > 0 && typ(m) != t_INT)
2390 : {
2391 531527 : GEN Rl = cgetg(KC+1, t_VECSMALL);
2392 531527 : reln = rel - cache->base;
2393 1015771 : for (l = 1; l < lauts; l++)
2394 : {
2395 484244 : GEN perml = gel(F->idealperm, l);
2396 484244 : long i, nzl = perml[nz];
2397 :
2398 20534546 : for (i = 1; i <= KC; i++) Rl[i] = 0;
2399 18308691 : for (i = nz; i <= KC; i++)
2400 17824447 : if (R[i])
2401 : {
2402 1286707 : long v = perml[i];
2403 :
2404 1286707 : if (v < nzl) nzl = v;
2405 1286707 : Rl[v] = R[i];
2406 : }
2407 484244 : (void)add_rel_i(cache, Rl, nzl, NULL, reln, l, NULL, mod_p);
2408 : }
2409 : }
2410 2502441 : return k;
2411 : }
2412 :
2413 : INLINE void
2414 29165365 : step(GEN x, double *y, GEN inc, long k)
2415 : {
2416 29165365 : if (!y[k])
2417 2282219 : x[k]++; /* leading coeff > 0 */
2418 : else
2419 : {
2420 26883146 : long i = inc[k];
2421 26883146 : x[k] += i;
2422 26883146 : inc[k] = (i > 0)? -1-i: 1-i;
2423 : }
2424 29165365 : }
2425 :
2426 : /* degree n >= 2 */
2427 : static double
2428 267323 : Fincke_Pohst_bound(double T, GEN r)
2429 : {
2430 267323 : pari_sp av = avma;
2431 267323 : GEN zT = dbltor(T * T), p = gmael(r,1,1), B = NULL;
2432 267323 : long i, n = lg(r)-1;
2433 267323 : double g = 0.;
2434 611865 : for (i = 2; i <= n; i++)
2435 : {
2436 611865 : p = gmul(p, gmael(r,i,i));
2437 611865 : B = sqrtnr(gmul(zT,p), i);
2438 611865 : if (i == n || cmprr(B, gmael(r,i+1,i+1)) < 0) break;
2439 : }
2440 267323 : if (!gisdouble(B,&g)) g = 0.;
2441 267323 : return gc_double(av, g);
2442 : }
2443 :
2444 : static void
2445 2124138 : fact_update(GEN R, FB_t *F, long ipr, GEN c)
2446 : {
2447 2124138 : GEN pr = gel(F->LP,ipr), p = pr_get_p(pr);
2448 2124138 : long v = Z_lval(c, itou(p));
2449 2124138 : if (v) R[ipr] -= pr_get_e(pr) * v;
2450 2124138 : }
2451 :
2452 : static long
2453 267323 : Fincke_Pohst_ideal_both(RELCACHE_t *cache, GEN V, FB_t *F, GEN nf, GEN I, GEN NI,
2454 : FACT *fact, long max_FACT, long Nrelid, FP_t *fp, GEN rex, long jid, long jid0, long e0,
2455 : long *Nsmall, long *Nfact, ulong mod_p)
2456 : {
2457 : pari_sp av;
2458 267323 : GEN G = nf_get_G(nf), G0 = nf_get_roundG(nf), r, u, gx, cgx, inc, ideal;
2459 267323 : long prec = nf_get_prec(nf), N = nf_get_degree(nf);
2460 267323 : long j, k, skipfirst, relid = 0, try_factor = 0, rel = 1;
2461 267323 : long try_elt = 0, maxtry_ELEMENT = 4*max_FACT*max_FACT;
2462 : double BOUND, B1, B2;
2463 :
2464 267323 : inc = const_vecsmall(N, 1);
2465 267323 : u = ZM_lll(ZM_mul(G0, I), 0.99, LLL_IM);
2466 267323 : ideal = ZM_mul(I,u); /* approximate T2-LLL reduction */
2467 267323 : r = gaussred_from_QR(RgM_mul(G, ideal), prec); /* Cholesky for T2 | ideal */
2468 267323 : if (!r) pari_err_BUG("small_norm (precision too low)");
2469 :
2470 1186204 : for (k=1; k<=N; k++)
2471 : {
2472 918881 : if (!gisdouble(gcoeff(r,k,k),&(fp->v[k]))) return 0;
2473 2725031 : for (j=1; j<k; j++) if (!gisdouble(gcoeff(r,j,k),&(fp->q[j][k]))) return 0;
2474 918881 : if (DEBUGLEVEL>3) err_printf("v[%ld]=%.4g ",k,fp->v[k]);
2475 : }
2476 267323 : B1 = fp->v[1]; /* T2(ideal[1]) */
2477 267323 : B2 = fp->v[2] + B1 * fp->q[1][2] * fp->q[1][2]; /* T2(ideal[2]) */
2478 267323 : skipfirst = ZV_isscalar(gel(ideal,1));
2479 267323 : BOUND = maxdd(2*B2, Fincke_Pohst_bound(4 * max_FACT / F->ballvol, r));
2480 267323 : if (DEBUGLEVEL>1)
2481 : {
2482 0 : if (DEBUGLEVEL>3) err_printf("\n");
2483 0 : err_printf("BOUND = %.4g\n",BOUND);
2484 : }
2485 :
2486 267323 : k = N; fp->y[N] = fp->z[N] = 0; fp->x[N] = 0;
2487 19544291 : for (av = avma;; set_avma(av), step(fp->x,fp->y,inc,k))
2488 19276968 : {
2489 : GEN R;
2490 : long nz;
2491 : do
2492 : { /* look for primitive element of small norm, cf minim00 */
2493 24408495 : int fl = 0;
2494 : double p;
2495 24408495 : if (k > 1)
2496 : {
2497 5131527 : long l = k-1;
2498 5131527 : fp->z[l] = 0;
2499 45550377 : for (j=k; j<=N; j++) fp->z[l] += fp->q[l][j]*fp->x[j];
2500 5131527 : p = (double)fp->x[k] + fp->z[k];
2501 5131527 : fp->y[l] = fp->y[k] + p*p*fp->v[k];
2502 5131527 : if (l <= skipfirst && !fp->y[1]) fl = 1;
2503 5131527 : fp->x[l] = (long)floor(-fp->z[l] + 0.5);
2504 5131527 : k = l;
2505 : }
2506 4486234 : for(;; step(fp->x,fp->y,inc,k))
2507 : {
2508 28894729 : if (!fl)
2509 : {
2510 28855182 : if (++try_elt > maxtry_ELEMENT) goto END_Fincke_Pohst_ideal;
2511 28852389 : p = (double)fp->x[k] + fp->z[k];
2512 28852389 : if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
2513 :
2514 5402163 : step(fp->x,fp->y,inc,k);
2515 :
2516 5402163 : p = (double)fp->x[k] + fp->z[k];
2517 5402163 : if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
2518 : }
2519 4489027 : fl = 0; inc[k] = 1;
2520 4489027 : if (++k > N) goto END_Fincke_Pohst_ideal;
2521 : }
2522 24405702 : } while (k > 1);
2523 :
2524 : /* element complete */
2525 35093163 : if (zv_content(fp->x) !=1) continue; /* not primitive */
2526 16104553 : gx = ZM_zc_mul(ideal,fp->x);
2527 16104553 : if (ZV_isscalar(gx)) continue;
2528 16228213 : if (++try_factor > max_FACT) break;
2529 :
2530 16063197 : if (DEBUGLEVEL && Nsmall) (*Nsmall)++;
2531 16063197 : if (!factorgen(F,nf,I,NI,gx,fact)) continue;
2532 2427289 : if (!Nrelid) return 1;
2533 2327775 : if (jid == jid0)
2534 28603 : add_to_fact(jid, 1 + e0, fact);
2535 : else
2536 : {
2537 2299172 : add_to_fact(jid, 1, fact);
2538 2299172 : if (jid0) add_to_fact(jid0, e0, fact);
2539 : }
2540 :
2541 : /* smooth element */
2542 2327775 : R = set_fact(F, fact, rex, &nz);
2543 2327775 : cgx = Z_content(gx);
2544 2327775 : if (cgx)
2545 : { /* relatively rare, compute relation attached to gx/cgx */
2546 510636 : long i, n = fact[0].pr;
2547 510636 : gx = Q_div_to_int(gx, cgx);
2548 2561602 : for (i = 1; i <= n; i++) fact_update(R, F, fact[i].pr, cgx);
2549 510636 : if (rex)
2550 : {
2551 41157 : long l = lg(rex);
2552 153868 : for (i = 1; i < l; i++)
2553 112711 : if (rex[i])
2554 : {
2555 108717 : long t, ipr = F->subFB[i];
2556 444031 : for (t = 1; t <= n; t++)
2557 370859 : if (fact[t].pr == ipr) break;
2558 108717 : if (t > n) fact_update(R, F, ipr, cgx);
2559 : }
2560 : }
2561 : }
2562 2327775 : if (DEBUGLEVEL && Nfact) (*Nfact)++;
2563 2327775 : if (cache)
2564 : {
2565 : /* make sure we get maximal rank first, then allow all relations */
2566 2327775 : if (add_rel(cache, F, R, nz, gx, mod_p) <= 0)
2567 : { /* probably Q-dependent from previous ones: forget it */
2568 1796807 : if (DEBUGLEVEL>1) err_printf("*");
2569 1796807 : continue;
2570 : }
2571 530968 : if (cache->last >= cache->end) return 1; /* we have enough */
2572 : } else
2573 : {
2574 0 : gel(V,rel++) = gerepilecopy(av, mkvec3(R, stoi(nz), gx));
2575 0 : av = avma;
2576 : }
2577 431468 : if (++relid == Nrelid) break;
2578 : }
2579 167809 : END_Fincke_Pohst_ideal:
2580 167809 : return 0;
2581 : }
2582 :
2583 : static long
2584 267323 : Fincke_Pohst_ideal(RELCACHE_t *cache, FB_t *F, GEN nf, GEN I, GEN NI,
2585 : FACT *fact, long Nrelid, long max_FACT, FP_t *fp, GEN rex, long jid, long jid0, long e0,
2586 : long *Nsmall, long *Nfact, ulong mod_p)
2587 : {
2588 267323 : return Fincke_Pohst_ideal_both(cache, NULL,
2589 : F, nf, I, NI, fact, max_FACT, Nrelid, fp, rex, jid, jid0, e0, Nsmall, Nfact, mod_p);
2590 : }
2591 :
2592 : static long
2593 0 : Fincke_Pohst_ideal_par(GEN V, FB_t *F, GEN nf, GEN I, GEN NI,
2594 : FACT *fact, long max_FACT, FP_t *fp, GEN rex, long jid, long jid0, long e0,
2595 : long *Nsmall, long *Nfact, ulong mod_p)
2596 : {
2597 0 : return Fincke_Pohst_ideal_both(NULL, V,
2598 : F, nf, I, NI, fact, max_FACT, max_FACT, fp, rex, jid, jid0, e0, Nsmall, Nfact, mod_p);
2599 : }
2600 :
2601 : static GEN
2602 0 : pack_FB(FB_t *F, long s)
2603 : {
2604 0 : return mkvecn(s ? 8: 7, F->FB, F->LP, F->LV, F->iLP, F->idealperm, F->prodZ,
2605 : mkvecsmall3(F->KC,F->KCZ,F->KCZ2), F->subFB);
2606 : }
2607 :
2608 : static void
2609 0 : unpack_FB(FB_t *F, GEN P)
2610 : {
2611 0 : F->FB = gel(P,1);
2612 0 : F->LP = gel(P,2);
2613 0 : F->LV = gel(P,3);
2614 0 : F->iLP = gel(P,4);
2615 0 : F->idealperm = gel(P,5);
2616 0 : F->prodZ = gel(P,6);
2617 0 : F->KC = mael(P,7,1);
2618 0 : F->KCZ = mael(P,7,2);
2619 0 : F->KCZ2 = mael(P,7,3);
2620 0 : if (lg(P) > 8)
2621 0 : F->subFB = gel(P,8);
2622 0 : }
2623 :
2624 : GEN
2625 0 : bnfinit_FP_worker(GEN INI, GEN PF, GEN nf, long max_FACT, GEN rex, long jid0, long e0, ulong mod_p)
2626 : {
2627 0 : pari_sp av = avma;
2628 : FB_t F;
2629 : FP_t fp;
2630 : FACT * fact;
2631 0 : long Nsmall = 0, Nfact = 0, res, N = nf_get_degree(nf), jid = itos(gel(INI,3));
2632 0 : GEN vec = zerovec(max_FACT);
2633 0 : GEN I = gel(INI,1), NI = gel(INI,2);
2634 0 : if (isintzero(rex)) rex = NULL;
2635 0 : unpack_FB(&F, PF);
2636 0 : F.ballvol = ballvol(N);
2637 0 : minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
2638 0 : fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
2639 0 : res = Fincke_Pohst_ideal_par(vec, &F, nf, I, NI, fact, max_FACT, &fp, rex, jid, jid0, e0, &Nsmall, &Nfact, mod_p);
2640 0 : return gerepilecopy(av, mkvec2(vec, mkvecsmall3(res, Nsmall, Nfact)));
2641 : }
2642 :
2643 : static void
2644 0 : small_norm_par(RELCACHE_t *cache, FB_t *F, GEN nf, long max_FACT, long idex, long nbthr, long j0, long mod_p)
2645 : {
2646 0 : GEN L_jid = F->L_jid, Np0 = NULL, p0 = j0? gel(F->LP,j0): NULL;
2647 0 : long Nsmall, Nfact, n = lg(L_jid)-1, e0 = 0;
2648 : pari_timer T;
2649 0 : long nt = nbthr? nbthr: mt_nbthreads();
2650 0 : GEN worker, vec = cgetg(nt+1, t_VEC);
2651 :
2652 0 : if (DEBUGLEVEL)
2653 : {
2654 0 : timer_start(&T);
2655 0 : err_printf("#### Look for %ld relations in %ld ideals (small_norm)\n",
2656 0 : cache->end - cache->last, lg(L_jid)-1);
2657 0 : if (p0) err_printf("Look in p0 = %Ps\n", vecslice(p0,1,4));
2658 : }
2659 0 : Nsmall = Nfact = 0;
2660 0 : if (p0)
2661 : {
2662 0 : GEN N0 = pr_norm(p0);
2663 0 : e0 = idex ? idex: logint0(sqri(pr_norm(veclast(F->LP))), N0, NULL);
2664 0 : p0 = idealpows(nf, p0, e0);
2665 0 : Np0 = powiu(N0, e0);
2666 : }
2667 0 : worker = snm_closure(is_entry("_bnfinit_FP_worker"),
2668 : mkcoln(7, pack_FB(F,0), nf, stoi(max_FACT), gen_0, stoi(j0), stoi(e0), utoi(mod_p)));
2669 0 : while(n)
2670 : {
2671 : GEN VB;
2672 0 : long k, m = minss(n, nt);
2673 0 : for (k = 1; k <= m; k++, n--)
2674 : {
2675 0 : long j = L_jid[n];
2676 0 : GEN id = gel(F->LP,j), Nid;
2677 0 : if (p0)
2678 0 : { Nid = mulii(Np0, pr_norm(id)); id = idealmul(nf, p0, id); }
2679 : else
2680 0 : { Nid = pr_norm(id); id = pr_hnf(nf, id); }
2681 0 : gel(vec,k) = mkvec3(id, Nid, stoi(j));
2682 : }
2683 0 : setlg(vec,k);
2684 0 : VB = gen_parapply(worker,vec);
2685 0 : for (k = 1; k <= m; k++)
2686 : {
2687 0 : GEN B = gel(VB,k), B1 = gel(B,1), B2 = gel(B,2);
2688 0 : long i, lB = lg(B1);
2689 0 : Nsmall += B2[2]; Nfact += B2[3];
2690 0 : for (i = 1; i<lB && !isintzero(gel(B1,i)); i++)
2691 : {
2692 0 : GEN Bi = gel(B1,i), R = gel(Bi,1), gx = gel(Bi,3);
2693 0 : long nz = itos(gel(Bi,2));
2694 0 : if (cache->last < cache->end)
2695 0 : add_rel(cache, F, R, nz, gx, mod_p);
2696 : }
2697 0 : if (cache->last >= cache->end) { n = 0; break; }
2698 : }
2699 : }
2700 0 : if (DEBUGLEVEL && Nsmall)
2701 : {
2702 0 : if (DEBUGLEVEL == 1)
2703 0 : { if (Nfact) err_printf("\n"); }
2704 : else
2705 0 : err_printf(" \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
2706 0 : Nfact,Nsmall,((double)Nfact)/Nsmall);
2707 0 : if (timer_get(&T)>1) timer_printf(&T,"small_norm");
2708 : }
2709 0 : }
2710 :
2711 : static void
2712 66857 : small_norm_seq(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, long max_fact, long idex, FACT *fact, long j0, ulong mod_p)
2713 : {
2714 66857 : const long N = nf_get_degree(nf);
2715 : FP_t fp;
2716 : pari_sp av;
2717 66857 : GEN L_jid = F->L_jid, Np0 = NULL, p0 = j0? gel(F->LP,j0): NULL;
2718 66857 : long Nsmall, Nfact, n = lg(L_jid), e0 = 0;
2719 : pari_timer T;
2720 :
2721 66857 : if (DEBUGLEVEL)
2722 : {
2723 0 : timer_start(&T);
2724 0 : err_printf("#### Look for %ld relations in %ld ideals (small_norm)\n",
2725 0 : cache->end - cache->last, lg(L_jid)-1);
2726 0 : if (p0) err_printf("Look in p0 = %Ps\n", vecslice(p0,1,4));
2727 : }
2728 66857 : Nsmall = Nfact = 0;
2729 66857 : minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
2730 66857 : if (p0)
2731 : {
2732 26759 : GEN n = pr_norm(p0);
2733 26759 : e0 = idex ? idex: logint0(sqri(pr_norm(veclast(F->LP))), n, NULL);
2734 26759 : p0 = idealpows(nf, p0, e0);
2735 26759 : Np0 = powiu(n,e0);
2736 : }
2737 165205 : for (av = avma; --n; set_avma(av))
2738 : {
2739 164777 : long j = L_jid[n];
2740 164777 : GEN id = gel(F->LP, j), Nid;
2741 164777 : if (DEBUGLEVEL>1)
2742 0 : err_printf("\n*** Ideal no %ld: %Ps\n", j, vecslice(id,1,4));
2743 164777 : if (p0)
2744 : {
2745 32566 : if (j == j0)
2746 : { /* avoid trivial relation */
2747 3897 : long e = pr_get_e(id);
2748 3897 : if ((e0 + 1) % e == 0 && e * pr_get_f(id) == N) continue;
2749 : }
2750 31897 : Nid = mulii(Np0, pr_norm(id)); id = idealmul(nf, p0, id);
2751 : }
2752 : else
2753 132211 : { Nid = pr_norm(id); id = pr_hnf(nf, id);}
2754 164108 : if (Fincke_Pohst_ideal(cache, F, nf, id, Nid, fact, Nrelid, max_fact, &fp,
2755 66429 : NULL, j, j0, e0, &Nsmall, &Nfact, mod_p)) break;
2756 : }
2757 66857 : if (DEBUGLEVEL && Nsmall)
2758 : {
2759 0 : if (DEBUGLEVEL == 1)
2760 0 : { if (Nfact) err_printf("\n"); }
2761 : else
2762 0 : err_printf(" \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
2763 0 : Nfact,Nsmall,((double)Nfact)/Nsmall);
2764 0 : if (timer_get(&T)>1) timer_printf(&T,"small_norm");
2765 : }
2766 66857 : }
2767 :
2768 : static void
2769 66857 : small_norm(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, long max_fact, long idex, long nbthr, FACT *fact, long j0, ulong mod_p)
2770 : {
2771 66857 : if (nbthr==1)
2772 66857 : return small_norm_seq(cache, F, nf, Nrelid, max_fact, idex, fact, j0, mod_p);
2773 : else
2774 0 : return small_norm_par(cache, F, nf, max_fact, idex, nbthr, j0, mod_p);
2775 : }
2776 :
2777 : static GEN
2778 56173 : get_random_ideal(FB_t *F, GEN nf, GEN ex)
2779 : {
2780 56173 : long i, l = lg(ex);
2781 : for (;;)
2782 980 : {
2783 57153 : GEN I = NULL;
2784 162096 : for (i = 1; i < l; i++)
2785 104943 : if ((ex[i] = random_bits(RANDOM_BITS)))
2786 : {
2787 100519 : GEN pr = gel(F->LP, F->subFB[i]), e = utoipos(ex[i]);
2788 100519 : I = I? idealmulpowprime(nf, I, pr, e): idealpow(nf, pr, e);
2789 : }
2790 57153 : if (I && !ZM_isscalar(I,NULL)) return I; /* != (n)Z_K */
2791 : }
2792 : }
2793 :
2794 : static void
2795 0 : rnd_rel_par(RELCACHE_t *cache, FB_t *F, GEN nf, long max_FACT, ulong mod_p)
2796 : {
2797 : pari_timer T;
2798 0 : GEN L_jid = F->L_jid, R, ex;
2799 0 : long k, l = lg(L_jid), Nfact = 0, Nsmall = 0;
2800 : GEN worker, vec, VB, NR;
2801 :
2802 0 : if (DEBUGLEVEL) {
2803 0 : timer_start(&T);
2804 0 : err_printf("#### Look for %ld relations in %ld ideals (rnd_rel)\n",
2805 0 : cache->end - cache->last, l-1);
2806 : }
2807 0 : ex = cgetg(lg(F->subFB), t_VECSMALL);
2808 0 : R = get_random_ideal(F, nf, ex); /* random product from subFB */
2809 0 : NR = ZM_det_triangular(R);
2810 0 : worker = snm_closure(is_entry("_bnfinit_FP_worker"),
2811 : mkcoln(7,pack_FB(F,1), nf, stoi(max_FACT), ex, gen_0, gen_0, utoi(mod_p)));
2812 0 : vec = cgetg(l, t_VEC);
2813 0 : for (k = 1; k < l; k++)
2814 : {
2815 0 : long j = L_jid[k];
2816 0 : GEN id = gel(F->LP,j), Nid;
2817 0 : Nid = mulii(NR, pr_norm(id)); id = idealmul(nf, R, id);
2818 0 : gel(vec,k) = mkvec3(id, Nid, stoi(j));
2819 : }
2820 0 : VB = gen_parapply(worker,vec);
2821 0 : for (k = 1; k < l; k++)
2822 : {
2823 0 : GEN B = gel(VB,k), B1 = gel(B,1), B2 = gel(B,2);
2824 0 : long i, lB = lg(B1);
2825 0 : Nsmall += B2[2]; Nfact += B2[3];
2826 0 : for (i = 1; i<lB && !isintzero(gel(B1,i)); i++)
2827 : {
2828 0 : GEN Bi = gel(B1,i), R = gel(Bi,1), gx = gel(Bi,3);
2829 0 : long nz = itos(gel(Bi,2));
2830 0 : if (cache->last < cache->end)
2831 0 : add_rel(cache, F, R, nz, gx, mod_p);
2832 : }
2833 0 : if (cache->last >= cache->end) break;
2834 : }
2835 :
2836 0 : if (DEBUGLEVEL)
2837 : {
2838 0 : if (DEBUGLEVEL == 1)
2839 0 : { if (Nfact) err_printf("\n"); }
2840 : else
2841 0 : err_printf(" \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
2842 0 : Nfact,Nsmall,((double)Nfact)/Nsmall);
2843 0 : if (timer_get(&T)>=0) timer_printf(&T,"rnd_rel");
2844 : }
2845 0 : }
2846 :
2847 : static void
2848 56173 : rnd_rel_seq(RELCACHE_t *cache, FB_t *F, GEN nf, long max_fact, FACT *fact, ulong mod_p)
2849 : {
2850 : pari_timer T;
2851 56173 : GEN L_jid = F->L_jid, R, NR, ex;
2852 56173 : long i, l = lg(L_jid), Nfact = 0;
2853 : FP_t fp;
2854 : pari_sp av;
2855 :
2856 56173 : if (DEBUGLEVEL) {
2857 0 : timer_start(&T);
2858 0 : err_printf("#### Look for %ld relations in %ld ideals (rnd_rel)\n",
2859 0 : cache->end - cache->last, l-1);
2860 : }
2861 56173 : ex = cgetg(lg(F->subFB), t_VECSMALL);
2862 56173 : R = get_random_ideal(F, nf, ex); /* random product from subFB */
2863 56173 : NR = ZM_det_triangular(R);
2864 56173 : minim_alloc(nf_get_degree(nf)+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
2865 126303 : for (av = avma, i = 1; i < l; i++, set_avma(av))
2866 : { /* try P[j] * base */
2867 103201 : long j = L_jid[i];
2868 103201 : GEN P = gel(F->LP, j), Nid = mulii(NR, pr_norm(P));
2869 103201 : if (DEBUGLEVEL>1) err_printf("\n*** Ideal %ld: %Ps\n", j, vecslice(P,1,4));
2870 103201 : if (Fincke_Pohst_ideal(cache, F, nf, idealHNF_mul(nf, R, P), Nid, fact,
2871 33071 : RND_REL_RELPID, max_fact, &fp, ex, j, 0, 0, NULL, &Nfact, mod_p)) break;
2872 : }
2873 56173 : if (DEBUGLEVEL)
2874 : {
2875 0 : if (Nfact) err_printf("\n");
2876 0 : if (timer_get(&T)>=0) timer_printf(&T,"rnd_rel");
2877 : }
2878 56173 : }
2879 : static void
2880 56173 : rnd_rel(RELCACHE_t *cache, FB_t *F, GEN nf, long max_fact, long nbthr, FACT *fact, ulong mod_p)
2881 : {
2882 56173 : if (nbthr==1)
2883 56173 : return rnd_rel_seq(cache, F, nf, max_fact, fact, mod_p);
2884 : else
2885 0 : return rnd_rel_par(cache, F, nf, max_fact, mod_p);
2886 : }
2887 :
2888 : static GEN
2889 64064 : automorphism_perms(GEN M, GEN auts, GEN cyclic, long r1, long r2, long N)
2890 : {
2891 64064 : long L = lgcols(M), lauts = lg(auts), lcyc = lg(cyclic), i, j, l, m;
2892 64064 : GEN Mt, perms = cgetg(lauts, t_VEC);
2893 : pari_sp av;
2894 :
2895 129059 : for (l = 1; l < lauts; l++) gel(perms, l) = cgetg(L, t_VECSMALL);
2896 64064 : av = avma;
2897 64064 : Mt = shallowtrans(gprec_w(M, LOWDEFAULTPREC));
2898 64064 : Mt = shallowconcat(Mt, conj_i(vecslice(Mt, r1+1, r1+r2)));
2899 111580 : for (l = 1; l < lcyc; l++)
2900 : {
2901 47516 : GEN thiscyc = gel(cyclic, l), thisperm, perm, prev, Nt;
2902 47516 : long k = thiscyc[1];
2903 :
2904 47516 : Nt = RgM_mul(shallowtrans(gel(auts, k)), Mt);
2905 47516 : perm = gel(perms, k);
2906 157458 : for (i = 1; i < L; i++)
2907 : {
2908 109942 : GEN v = gel(Nt, i), minD;
2909 109942 : minD = gnorml2(gsub(v, gel(Mt, 1)));
2910 109942 : perm[i] = 1;
2911 577633 : for (j = 2; j <= N; j++)
2912 : {
2913 467691 : GEN D = gnorml2(gsub(v, gel(Mt, j)));
2914 467691 : if (gcmp(D, minD) < 0) { minD = D; perm[i] = j >= L ? r2-j : j; }
2915 : }
2916 : }
2917 66227 : for (prev = perm, m = 2; m < lg(thiscyc); m++, prev = thisperm)
2918 : {
2919 18711 : thisperm = gel(perms, thiscyc[m]);
2920 94654 : for (i = 1; i < L; i++)
2921 : {
2922 75943 : long pp = labs(prev[i]);
2923 75943 : thisperm[i] = prev[i] < 0 ? -perm[pp] : perm[pp];
2924 : }
2925 : }
2926 : }
2927 64064 : set_avma(av); return perms;
2928 : }
2929 :
2930 : /* Determine the field automorphisms as matrices on the integral basis */
2931 : static GEN
2932 64127 : automorphism_matrices(GEN nf, GEN *cycp)
2933 : {
2934 64127 : pari_sp av = avma;
2935 64127 : GEN auts = galoisconj(nf, NULL), mats, cyclic, cyclicidx;
2936 64127 : long nauts = lg(auts)-1, i, j, k, l;
2937 :
2938 64127 : cyclic = cgetg(nauts+1, t_VEC);
2939 64127 : cyclicidx = zero_Flv(nauts);
2940 98560 : for (l = 1; l <= nauts; l++)
2941 : {
2942 98560 : GEN aut = gel(auts, l);
2943 98560 : if (gequalX(aut)) { swap(gel(auts, l), gel(auts, nauts)); break; }
2944 : }
2945 : /* trivial automorphism is last */
2946 193277 : for (l = 1; l <= nauts; l++) gel(auts, l) = algtobasis(nf, gel(auts, l));
2947 : /* Compute maximal cyclic subgroups */
2948 129150 : for (l = nauts; --l > 0; ) if (!cyclicidx[l])
2949 : {
2950 49070 : GEN elt = gel(auts, l), aut = elt, cyc = cgetg(nauts+1, t_VECSMALL);
2951 49070 : cyc[1] = cyclicidx[l] = l; j = 1;
2952 : do
2953 : {
2954 68327 : elt = galoisapply(nf, elt, aut);
2955 222250 : for (k = 1; k <= nauts; k++) if (gequal(elt, gel(auts, k))) break;
2956 68327 : cyclicidx[k] = l; cyc[++j] = k;
2957 : }
2958 68327 : while (k != nauts);
2959 49070 : setlg(cyc, j);
2960 49070 : gel(cyclic, l) = cyc;
2961 : }
2962 129150 : for (i = j = 1; i < nauts; i++)
2963 65023 : if (cyclicidx[i] == i) cyclic[j++] = cyclic[i];
2964 64127 : setlg(cyclic, j);
2965 64127 : mats = cgetg(nauts, t_VEC);
2966 111671 : while (--j > 0)
2967 : {
2968 47544 : GEN cyc = gel(cyclic, j);
2969 47544 : long id = cyc[1];
2970 47544 : GEN M, Mi, aut = gel(auts, id);
2971 :
2972 47544 : gel(mats, id) = Mi = M = nfgaloismatrix(nf, aut);
2973 66255 : for (i = 2; i < lg(cyc); i++) gel(mats, cyc[i]) = Mi = ZM_mul(Mi, M);
2974 : }
2975 64127 : (void)gc_all(av, 2, &mats, &cyclic);
2976 64127 : if (cycp) *cycp = cyclic;
2977 64127 : return mats;
2978 : }
2979 :
2980 : /* vP a list of maximal ideals above the same p from idealprimedec: f(P/p) is
2981 : * increasing; 1 <= j <= #vP; orbit a zc of length <= #vP; auts a vector of
2982 : * automorphisms in ZM form.
2983 : * Set orbit[i] = 1 for all vP[i], i >= j, in the orbit of pr = vP[j] wrt auts.
2984 : * N.B.1 orbit need not be initialized to 0: useful to incrementally run
2985 : * through successive orbits
2986 : * N.B.2 i >= j, so primes with index < j will be missed; run incrementally
2987 : * starting from j = 1 ! */
2988 : static void
2989 11865 : pr_orbit_fill(GEN orbit, GEN auts, GEN vP, long j)
2990 : {
2991 11865 : GEN pr = gel(vP,j), gen = pr_get_gen(pr);
2992 11865 : long i, l = lg(auts), J = lg(orbit), f = pr_get_f(pr);
2993 11865 : orbit[j] = 1;
2994 23730 : for (i = 1; i < l; i++)
2995 : {
2996 11865 : GEN g = ZM_ZC_mul(gel(auts,i), gen);
2997 : long k;
2998 11886 : for (k = j+1; k < J; k++)
2999 : {
3000 35 : GEN prk = gel(vP,k);
3001 35 : if (pr_get_f(prk) > f) break; /* f(P[k]) increases with k */
3002 : /* don't check that e matches: (almost) always 1 ! */
3003 35 : if (!orbit[k] && ZC_prdvd(g, prk)) { orbit[k] = 1; break; }
3004 : }
3005 : }
3006 11865 : }
3007 : /* remark: F->KCZ changes if be_honest() fails */
3008 : static int
3009 7 : be_honest(FB_t *F, GEN nf, GEN auts, FACT *fact)
3010 : {
3011 : long i, iz, nbtest;
3012 7 : long lgsub = lg(F->subFB), KCZ0 = F->KCZ, N = nf_get_degree(nf);
3013 : FP_t fp;
3014 : pari_sp av;
3015 :
3016 7 : if (DEBUGLEVEL) {
3017 0 : err_printf("Be honest for %ld primes from %ld to %ld\n", F->KCZ2 - F->KCZ,
3018 0 : F->FB[ F->KCZ+1 ], F->FB[ F->KCZ2 ]);
3019 : }
3020 7 : minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
3021 7 : if (lg(auts) == 1) auts = NULL;
3022 7 : av = avma;
3023 14 : for (iz=F->KCZ+1; iz<=F->KCZ2; iz++, set_avma(av))
3024 : {
3025 7 : long p = F->FB[iz];
3026 7 : GEN pr_orbit, P = gel(F->LV,p);
3027 7 : long j, J = lg(P); /* > 1 */
3028 : /* the P|p, NP > C2 are assumed in subgroup generated by FB + last P
3029 : * with NP <= C2 is unramified --> check all but last */
3030 7 : if (pr_get_e(gel(P,J-1)) == 1) J--;
3031 7 : if (J == 1) continue;
3032 7 : if (DEBUGLEVEL>1) err_printf("%ld ", p);
3033 7 : pr_orbit = auts? zero_zv(J-1): NULL;
3034 28 : for (j = 1; j < J; j++)
3035 : {
3036 : GEN Nid, id, id0;
3037 21 : if (pr_orbit)
3038 : {
3039 21 : if (pr_orbit[j]) continue;
3040 : /* discard all primes in automorphism orbit simultaneously */
3041 14 : pr_orbit_fill(pr_orbit, auts, P, j);
3042 : }
3043 14 : id = id0 = pr_hnf(nf,gel(P,j));
3044 14 : Nid = pr_norm(gel(P,j));
3045 14 : for (nbtest=0;;)
3046 : {
3047 14 : if (Fincke_Pohst_ideal(NULL, F, nf, id, Nid, fact, 0, MAXTRY_FACT, &fp,
3048 14 : NULL, 0, 0, 0, NULL, NULL, 0)) break;
3049 0 : if (++nbtest > maxtry_HONEST)
3050 : {
3051 0 : if (DEBUGLEVEL)
3052 0 : pari_warn(warner,"be_honest() failure on prime %Ps\n", gel(P,j));
3053 0 : return 0;
3054 : }
3055 : /* occurs at most once in the whole function */
3056 0 : for (i = 1, id = id0; i < lgsub; i++)
3057 : {
3058 0 : long ex = random_bits(RANDOM_BITS);
3059 0 : if (ex)
3060 : {
3061 0 : GEN pr = gel(F->LP, F->subFB[i]);
3062 0 : id = idealmulpowprime(nf, id, pr, utoipos(ex));
3063 : }
3064 : }
3065 0 : if (!equali1(gcoeff(id,N,N))) id = Q_primpart(id);
3066 0 : if (expi(gcoeff(id,1,1)) > 100) id = idealred(nf, id);
3067 0 : Nid = ZM_det_triangular(id);
3068 : }
3069 : }
3070 7 : F->KCZ++; /* SUCCESS, "enlarge" factorbase */
3071 : }
3072 7 : F->KCZ = KCZ0; return gc_bool(av,1);
3073 : }
3074 :
3075 : /* all primes with N(P) <= BOUND factor on factorbase ? */
3076 : void
3077 63 : bnftestprimes(GEN bnf, GEN BOUND)
3078 : {
3079 63 : pari_sp av0 = avma, av;
3080 63 : ulong count = 0;
3081 63 : GEN auts, p, nf = bnf_get_nf(bnf), Vbase = bnf_get_vbase(bnf);
3082 63 : GEN fb = gen_sort_shallow(Vbase, (void*)&cmp_prime_ideal, cmp_nodata);
3083 63 : ulong pmax = pr_get_smallp(veclast(fb)); /*largest p in factorbase*/
3084 : forprime_t S;
3085 : FACT *fact;
3086 : FB_t F;
3087 :
3088 63 : (void)recover_partFB(&F, Vbase, nf_get_degree(nf));
3089 63 : fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
3090 63 : forprime_init(&S, gen_2, BOUND);
3091 63 : auts = automorphism_matrices(nf, NULL);
3092 63 : if (lg(auts) == 1) auts = NULL;
3093 63 : av = avma;
3094 37604 : while (( p = forprime_next(&S) ))
3095 : {
3096 : GEN pr_orbit, vP;
3097 : long j, J;
3098 :
3099 37541 : if (DEBUGLEVEL == 1 && ++count > 1000)
3100 : {
3101 0 : err_printf("passing p = %Ps / %Ps\n", p, BOUND);
3102 0 : count = 0;
3103 : }
3104 37541 : set_avma(av);
3105 37541 : vP = idealprimedec_limit_norm(nf, p, BOUND);
3106 37541 : J = lg(vP);
3107 : /* if last is unramified, all P|p in subgroup generated by FB: skip last */
3108 37541 : if (J > 1 && pr_get_e(gel(vP,J-1)) == 1) J--;
3109 37541 : if (J == 1) continue;
3110 14525 : if (DEBUGLEVEL>1) err_printf("*** p = %Ps\n",p);
3111 14525 : pr_orbit = auts? zero_zv(J-1): NULL;
3112 31549 : for (j = 1; j < J; j++)
3113 : {
3114 17024 : GEN P = gel(vP,j);
3115 17024 : long k = 0;
3116 17024 : if (pr_orbit)
3117 : {
3118 11858 : if (pr_orbit[j]) continue;
3119 : /* discard all primes in automorphism orbit simultaneously */
3120 11851 : pr_orbit_fill(pr_orbit, auts, vP, j);
3121 : }
3122 17017 : if (abscmpiu(p, pmax) > 0 || !(k = tablesearch(fb, P, &cmp_prime_ideal)))
3123 16408 : (void)SPLIT(&F, nf, pr_hnf(nf,P), Vbase, fact);
3124 17017 : if (DEBUGLEVEL>1)
3125 : {
3126 0 : err_printf(" Testing P = %Ps\n",P);
3127 0 : if (k) err_printf(" #%ld in factor base\n",k);
3128 0 : else err_printf(" is %Ps\n", isprincipal(bnf,P));
3129 : }
3130 : }
3131 : }
3132 63 : set_avma(av0);
3133 63 : }
3134 :
3135 : /* A t_MAT of complex floats, in fact reals. Extract a submatrix B
3136 : * whose columns are definitely nonzero, i.e. gexpo(A[j]) >= -2
3137 : *
3138 : * If possible precision problem (t_REAL 0 with large exponent), set
3139 : * *precpb to 1 */
3140 : static GEN
3141 93206 : clean_cols(GEN A, int *precpb)
3142 : {
3143 93206 : long l = lg(A), h, i, j, k;
3144 : GEN B;
3145 93206 : *precpb = 0;
3146 93206 : if (l == 1) return A;
3147 93206 : h = lgcols(A);;
3148 93206 : B = cgetg(l, t_MAT);
3149 1092195 : for (i = k = 1; i < l; i++)
3150 : {
3151 998989 : GEN Ai = gel(A,i);
3152 998989 : int non0 = 0;
3153 4336491 : for (j = 1; j < h; j++)
3154 : {
3155 3337502 : GEN c = gel(Ai,j);
3156 3337502 : if (gexpo(c) >= -2)
3157 : {
3158 1997854 : if (gequal0(c)) *precpb = 1; else non0 = 1;
3159 : }
3160 : }
3161 998989 : if (non0) gel(B, k++) = Ai;
3162 : }
3163 93206 : setlg(B, k); return B;
3164 : }
3165 :
3166 : static long
3167 573286 : compute_multiple_of_R_pivot(GEN X, GEN x0/*unused*/, long ix, GEN c)
3168 : {
3169 573286 : GEN x = gel(X,ix);
3170 573286 : long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
3171 : (void)x0;
3172 2813497 : for (i=1; i<lx; i++)
3173 2240211 : if (!c[i] && !gequal0(gel(x,i)))
3174 : {
3175 727217 : long e = gexpo(gel(x,i));
3176 727217 : if (e > ex) { ex = e; k = i; }
3177 : }
3178 573286 : return (k && ex > -32)? k: lx;
3179 : }
3180 :
3181 : /* Ar = (log |sigma_i(u_j)|) for units (u_j) found so far;
3182 : * RU = R1+R2 = target rank for unit matrix, after adding [1 x r1, 2 x r2];
3183 : * N = field degree, need = unit rank defect;
3184 : * L = NULL (prec problem) or B^(-1) * A with approximate rational entries
3185 : * (as t_REAL), B a submatrix of A, with (probably) maximal rank RU */
3186 : static GEN
3187 121604 : compute_multiple_of_R(GEN Ar, long RU, long N, long *pneed, long *bit, GEN *ptL)
3188 : {
3189 : GEN T, d, mdet, Im_mdet, kR, L;
3190 121604 : long i, j, r, R1 = 2*RU - N;
3191 : int precpb;
3192 121604 : pari_sp av = avma;
3193 :
3194 121604 : if (RU == 1) { *ptL = zeromat(0, lg(Ar)-1); return gen_1; }
3195 :
3196 93206 : if (DEBUGLEVEL) err_printf("\n#### Computing regulator multiple\n");
3197 93206 : mdet = clean_cols(Ar, &precpb);
3198 : /* will cause precision to increase on later failure, but we may succeed! */
3199 93206 : *ptL = precpb? NULL: gen_1;
3200 93206 : T = cgetg(RU+1,t_COL);
3201 235211 : for (i=1; i<=R1; i++) gel(T,i) = gen_1;
3202 193480 : for ( ; i<=RU; i++) gel(T,i) = gen_2;
3203 93206 : mdet = shallowconcat(T, mdet); /* det(Span(mdet)) = N * R */
3204 :
3205 : /* could be using indexrank(), but need custom "get_pivot" function */
3206 93206 : d = RgM_pivots(mdet, &r, &compute_multiple_of_R_pivot, NULL);
3207 : /* # of independent columns = target rank ? */
3208 93206 : if (lg(mdet)-1 - r != RU)
3209 : {
3210 25462 : if (DEBUGLEVEL)
3211 0 : err_printf("Units matrix target rank = %ld < %ld\n",lg(mdet)-1 - r, RU);
3212 25462 : *pneed = RU - (lg(mdet)-1-r); return gc_NULL(av);
3213 : }
3214 :
3215 67744 : Im_mdet = cgetg(RU+1, t_MAT); /* extract independent columns */
3216 : /* N.B: d[1] = 1, corresponding to T above */
3217 67744 : gel(Im_mdet, 1) = T;
3218 257114 : for (i = j = 2; i <= RU; j++)
3219 189370 : if (d[j]) gel(Im_mdet, i++) = gel(mdet,j);
3220 :
3221 : /* integral multiple of R: the cols we picked form a Q-basis, they have an
3222 : * index in the full lattice. First column is T */
3223 67744 : kR = divru(det2(Im_mdet), N);
3224 : /* R > 0.2 uniformly */
3225 67744 : if (!signe(kR) || expo(kR) < -3)
3226 : {
3227 0 : if (DEBUGLEVEL) err_printf("Regulator is zero.\n");
3228 0 : *pneed = 0; return gc_NULL(av);
3229 : }
3230 67744 : d = det2(rowslice(vecslice(Im_mdet, 2, RU), 2, RU));
3231 67744 : setabssign(d); setabssign(kR);
3232 67744 : if (gexpo(gsub(d,kR)) - gexpo(d) > -20) { *ptL = NULL; return gc_NULL(av); }
3233 67744 : L = RgM_inv(Im_mdet);
3234 : /* estimate # of correct bits in result */
3235 67744 : if (!L || (*bit = -gexpo(RgM_Rg_sub_shallow(RgM_mul(L,Im_mdet), gen_1))) < 16)
3236 10 : { *ptL = NULL; return gc_NULL(av); }
3237 :
3238 67734 : *ptL = RgM_mul(rowslice(L,2,RU), Ar); /* approximate rational entries */
3239 67734 : return gc_all(av,2, &kR, ptL);
3240 : }
3241 :
3242 : /* leave small integer n as is, convert huge n to t_REAL (for readability) */
3243 : static GEN
3244 0 : i2print(GEN n)
3245 0 : { return lgefint(n) <= DEFAULTPREC? n: itor(n,LOWDEFAULTPREC); }
3246 :
3247 : static long
3248 95968 : bad_check(GEN c)
3249 : {
3250 95968 : long ec = gexpo(c);
3251 95968 : if (DEBUGLEVEL) err_printf("\n ***** check = %.28Pg\n",c);
3252 : /* safe check for c < 0.75 : avoid underflow in gtodouble() */
3253 95968 : if (ec < -1 || (ec == -1 && gtodouble(c) < 0.75)) return fupb_PRECI;
3254 : /* safe check for c > 1.3 : avoid overflow */
3255 95968 : if (ec > 0 || (ec == 0 && gtodouble(c) > 1.3)) return fupb_RELAT;
3256 64071 : return fupb_NONE;
3257 : }
3258 : /* Input:
3259 : * lambda = approximate rational entries: coords of units found so far on a
3260 : * sublattice of maximal rank (sublambda)
3261 : * *ptkR = regulator of sublambda = multiple of regulator of lambda
3262 : * Compute R = true regulator of lambda.
3263 : *
3264 : * If c := Rz ~ 1, by Dirichlet's formula, then lambda is the full group of
3265 : * units AND the full set of relations for the class group has been computed.
3266 : * In fact z is a very rough approximation and we only expect 0.75 < Rz < 1.3
3267 : *
3268 : * Output: *ptkR = R, *ptL = numerator(units) (in terms of lambda) */
3269 : static long
3270 96031 : compute_R(GEN lambda, GEN z, GEN *ptL, GEN *ptkR)
3271 : {
3272 96031 : pari_sp av = avma;
3273 96031 : long bit, r, reason, RU = lg(lambda) == 1? 1: lgcols(lambda);
3274 : GEN L, H, D, den, R, c;
3275 :
3276 96031 : *ptL = NULL;
3277 96031 : if (RU == 1) { *ptkR = gen_1; *ptL = lambda; return bad_check(z); }
3278 67633 : D = gmul2n(mpmul(*ptkR,z), 1); /* bound for denom(lambda) */
3279 67633 : if (expo(D) < 0 && rtodbl(D) < 0.95) return fupb_PRECI;
3280 67633 : L = bestappr(lambda,D);
3281 67633 : if (lg(L) == 1)
3282 : {
3283 0 : if (DEBUGLEVEL) err_printf("truncation error in bestappr\n");
3284 0 : return fupb_PRECI;
3285 : }
3286 67633 : den = Q_denom(L);
3287 67633 : if (mpcmp(den,D) > 0)
3288 : {
3289 22 : if (DEBUGLEVEL) err_printf("D = %Ps\nden = %Ps\n",D, i2print(den));
3290 22 : return fupb_PRECI;
3291 : }
3292 67611 : bit = -gexpo(gsub(L, lambda)); /* input accuracy */
3293 67611 : L = Q_muli_to_int(L, den);
3294 67611 : if (gexpo(L) + expi(den) > bit - 32)
3295 : {
3296 41 : if (DEBUGLEVEL) err_printf("dubious bestappr; den = %Ps\n", i2print(den));
3297 41 : return fupb_PRECI;
3298 : }
3299 67570 : H = ZM_hnf(L); r = lg(H)-1;
3300 67570 : if (!r || r != nbrows(H))
3301 0 : R = gen_0; /* wrong rank */
3302 : else
3303 67570 : R = gmul(*ptkR, gdiv(ZM_det_triangular(H), powiu(den, r)));
3304 : /* R = tentative regulator; regulator > 0.2 uniformly */
3305 67570 : if (gexpo(R) < -3) {
3306 0 : if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
3307 0 : return gc_long(av, fupb_PRECI);
3308 : }
3309 67570 : c = gmul(R,z); /* should be n (= 1 if we are done) */
3310 67570 : if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
3311 67570 : if ((reason = bad_check(c))) return gc_long(av, reason);
3312 48860 : *ptkR = R; *ptL = L; return fupb_NONE;
3313 : }
3314 : static GEN
3315 64161 : get_clg2(GEN cyc, GEN Ga, GEN C, GEN Ur, GEN Ge, GEN M1, GEN M2)
3316 : {
3317 64161 : GEN GD = gsub(act_arch(M1, C), diagact_arch(cyc, Ga));
3318 64161 : GEN ga = gsub(act_arch(M2, C), act_arch(Ur, Ga));
3319 64161 : return mkvecn(6, Ur, ga, GD, Ge, M1, M2);
3320 : }
3321 : /* compute class group (clg1) + data for isprincipal (clg2) */
3322 : static GEN
3323 64064 : class_group_gen(GEN nf,GEN W,GEN C,GEN Vbase,long prec, GEN *pclg2)
3324 : {
3325 : GEN M1, M2, z, G, Ga, Ge, cyc, X, Y, D, U, V, Ur, Ui, Uir;
3326 : long j, l;
3327 :
3328 64064 : D = ZM_snfall(W,&U,&V); /* UWV=D, D diagonal, G = g Ui (G=new gens, g=old) */
3329 64064 : Ui = ZM_inv(U, NULL);
3330 64064 : l = lg(D); cyc = cgetg(l, t_VEC); /* elementary divisors */
3331 92848 : for (j = 1; j < l; j++)
3332 : {
3333 30364 : gel(cyc,j) = gcoeff(D,j,j); /* strip useless components */
3334 30364 : if (is_pm1(gel(cyc,j))) break;
3335 : }
3336 64064 : l = j;
3337 64064 : Ur = ZM_hnfdivrem(U, D, &Y);
3338 64064 : Uir = ZM_hnfdivrem(Ui,W, &X);
3339 : /* {x} = logarithmic embedding of x (arch. component)
3340 : * NB: [J,z] = idealred(I) --> I = y J, with {y} = - z
3341 : * G = g Uir - {Ga}, Uir = Ui - WX
3342 : * g = G Ur - {ga}, Ur = U - DY */
3343 64064 : G = cgetg(l,t_VEC);
3344 64064 : Ga= cgetg(l,t_MAT);
3345 64064 : Ge= cgetg(l,t_COL);
3346 64064 : z = init_famat(NULL);
3347 92848 : for (j = 1; j < l; j++)
3348 : {
3349 28784 : GEN I = genback(z, nf, Vbase, gel(Uir,j));
3350 28784 : gel(G,j) = gel(I,1); /* generator, order cyc[j] */
3351 28784 : gel(Ge,j)= gel(I,2);
3352 28784 : gel(Ga,j)= nf_cxlog(nf, gel(I,2), prec);
3353 28784 : if (!gel(Ga,j)) pari_err_PREC("class_group_gen");
3354 : }
3355 : /* {ga} = - {GD}Y + G U - g = - {GD}Y - {Ga} U - gW X U
3356 : = - gW (X Ur + V Y) - {Ga}Ur */
3357 64064 : M2 = ZM_neg(ZM_add(ZM_mul(X,Ur), ZM_mul(V,Y)));
3358 64064 : setlg(cyc,l); setlg(V,l); setlg(D,l);
3359 : /* G D =: {GD} = g (Ui - W X) D - {Ga}D = g W (V - X D) - {Ga}D
3360 : * NB: Ui D = W V. gW is given by (first l-1 cols of) C */
3361 64064 : M1 = ZM_sub(V, ZM_mul(X,D));
3362 64064 : *pclg2 = get_clg2(cyc, Ga, C, Ur, Ge, M1, M2);
3363 64064 : return mkvec3(ZV_prod(cyc), cyc, G);
3364 : }
3365 :
3366 : /* compute principal ideals corresponding to (gen[i]^cyc[i]) */
3367 : static GEN
3368 4956 : makecycgen(GEN bnf)
3369 : {
3370 4956 : GEN cyc = bnf_get_cyc(bnf), gen = bnf_get_gen(bnf), nf = bnf_get_nf(bnf);
3371 4956 : GEN h, y, GD = bnf_get_GD(bnf), W = bnf_get_W(bnf); /* HNF */
3372 4956 : GEN SUnits = bnf_get_sunits(bnf);
3373 4956 : GEN X = SUnits? gel(SUnits,1): NULL, C = SUnits? gel(SUnits,3): NULL;
3374 : long e, i, l;
3375 :
3376 4956 : if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building cycgen)");
3377 4956 : h = cgetg_copy(gen, &l);
3378 11613 : for (i = 1; i < l; i++)
3379 : {
3380 6657 : GEN gi = gel(gen,i), ci = gel(cyc,i);
3381 6657 : if (X && equalii(ci, gcoeff(W,i,i)))
3382 : {
3383 : long j;
3384 8589 : for (j = i+1; j < l; j++)
3385 3213 : if (signe(gcoeff(W,i,j))) break;
3386 5550 : if (j == i) { gel(h,i) = mkmat2(X, gel(C,i)); continue; }
3387 : }
3388 6657 : if (abscmpiu(ci, 5) < 0)
3389 : {
3390 5544 : GEN N = ZM_det_triangular(gi);
3391 5544 : y = isprincipalarch(bnf,gel(GD,i), N, ci, gen_1, &e);
3392 5544 : if (y && fact_ok(nf,y,NULL,mkvec(gi),mkvec(ci)))
3393 : {
3394 4556 : gel(h,i) = to_famat_shallow(y,gen_1);
3395 4556 : continue;
3396 : }
3397 : }
3398 2101 : y = isprincipalfact(bnf, NULL, mkvec(gi), mkvec(ci), nf_GENMAT|nf_FORCE);
3399 2101 : gel(h,i) = gel(y,2);
3400 : }
3401 4956 : return h;
3402 : }
3403 :
3404 : static GEN
3405 69 : get_y(GEN bnf, GEN W, GEN B, GEN C, GEN pFB, long j)
3406 : {
3407 69 : GEN y, nf = bnf_get_nf(bnf);
3408 69 : long e, lW = lg(W)-1;
3409 69 : GEN ex = (j<=lW)? gel(W,j): gel(B,j-lW);
3410 69 : GEN P = (j<=lW)? NULL: gel(pFB,j);
3411 69 : if (C)
3412 : { /* archimedean embeddings known: cheap trial */
3413 69 : GEN Nx = get_norm_fact_primes(pFB, ex, P);
3414 69 : y = isprincipalarch(bnf,gel(C,j), Nx,gen_1, gen_1, &e);
3415 69 : if (y && fact_ok(nf,y,P,pFB,ex)) return y;
3416 : }
3417 0 : y = isprincipalfact_or_fail(bnf, P, pFB, ex);
3418 0 : return typ(y) == t_INT? y: gel(y,2);
3419 : }
3420 : /* compute principal ideals corresponding to bnf relations */
3421 : static GEN
3422 20 : makematal(GEN bnf)
3423 : {
3424 20 : GEN W = bnf_get_W(bnf), B = bnf_get_B(bnf), C = bnf_get_C(bnf);
3425 : GEN pFB, ma, retry;
3426 20 : long lma, j, prec = 0;
3427 :
3428 20 : if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building matal)");
3429 20 : lma=lg(W)+lg(B)-1;
3430 20 : pFB = bnf_get_vbase(bnf);
3431 20 : ma = cgetg(lma,t_VEC);
3432 20 : retry = vecsmalltrunc_init(lma);
3433 89 : for (j=lma-1; j>0; j--)
3434 : {
3435 69 : pari_sp av = avma;
3436 69 : GEN y = get_y(bnf, W, B, C, pFB, j);
3437 69 : if (typ(y) == t_INT)
3438 : {
3439 0 : long E = itos(y);
3440 0 : if (DEBUGLEVEL>1) err_printf("\n%ld done later at prec %ld\n",j,E);
3441 0 : set_avma(av);
3442 0 : vecsmalltrunc_append(retry, j);
3443 0 : if (E > prec) prec = E;
3444 : }
3445 : else
3446 : {
3447 69 : if (DEBUGLEVEL>1) err_printf("%ld ",j);
3448 69 : gel(ma,j) = gc_upto(av,y);
3449 : }
3450 : }
3451 20 : if (prec)
3452 : {
3453 0 : long k, l = lg(retry);
3454 0 : GEN y, nf = bnf_get_nf(bnf);
3455 0 : if (DEBUGLEVEL) pari_warn(warnprec,"makematal",prec);
3456 0 : nf = nfnewprec_shallow(nf,prec);
3457 0 : bnf = Buchall(nf, nf_FORCE, prec);
3458 0 : if (DEBUGLEVEL) err_printf("makematal, adding missing entries:");
3459 0 : for (k=1; k<l; k++)
3460 : {
3461 0 : pari_sp av = avma;
3462 0 : long j = retry[k];
3463 0 : y = get_y(bnf,W,B,NULL, pFB, j);
3464 0 : if (typ(y) == t_INT) pari_err_PREC("makematal");
3465 0 : if (DEBUGLEVEL>1) err_printf("%ld ",j);
3466 0 : gel(ma,j) = gc_upto(av,y);
3467 : }
3468 : }
3469 20 : if (DEBUGLEVEL>1) err_printf("\n");
3470 20 : return ma;
3471 : }
3472 :
3473 : enum { MATAL = 1, CYCGEN, UNITS };
3474 : GEN
3475 26726 : bnf_build_cycgen(GEN bnf)
3476 26726 : { return obj_checkbuild(bnf, CYCGEN, &makecycgen); }
3477 : GEN
3478 20 : bnf_build_matalpha(GEN bnf)
3479 20 : { return obj_checkbuild(bnf, MATAL, &makematal); }
3480 : GEN
3481 50745 : bnf_build_units(GEN bnf)
3482 50745 : { return obj_checkbuild(bnf, UNITS, &makeunits); }
3483 :
3484 : /* return fu in compact form if available; in terms of a fixed basis
3485 : * of S-units */
3486 : GEN
3487 70 : bnf_compactfu_mat(GEN bnf)
3488 : {
3489 70 : GEN X, U, SUnits = bnf_get_sunits(bnf);
3490 70 : if (!SUnits) return NULL;
3491 70 : X = gel(SUnits,1);
3492 70 : U = gel(SUnits,2); ZM_remove_unused(&U, &X);
3493 70 : return mkvec2(X, U);
3494 : }
3495 : /* return fu in compact form if available; individually as famat */
3496 : GEN
3497 37422 : bnf_compactfu(GEN bnf)
3498 : {
3499 37422 : GEN fu, X, U, SUnits = bnf_get_sunits(bnf);
3500 : long i, l;
3501 37422 : if (!SUnits) return NULL;
3502 37184 : X = gel(SUnits,1);
3503 37184 : U = gel(SUnits,2); l = lg(U); fu = cgetg(l, t_VEC);
3504 61215 : for (i = 1; i < l; i++)
3505 24031 : gel(fu,i) = famat_remove_trivial(mkmat2(X, gel(U,i)));
3506 37184 : return fu;
3507 : }
3508 : /* return expanded fu if available */
3509 : GEN
3510 285789 : bnf_has_fu(GEN bnf)
3511 : {
3512 285789 : GEN fu = obj_check(bnf, UNITS);
3513 285789 : if (fu) return vecsplice(fu, 1);
3514 264134 : fu = bnf_get_fu_nocheck(bnf);
3515 264134 : return (typ(fu) == t_MAT)? NULL: fu;
3516 : }
3517 : /* return expanded fu if available; build if cheap */
3518 : GEN
3519 285502 : bnf_build_cheapfu(GEN bnf)
3520 : {
3521 : GEN fu, SUnits;
3522 285502 : if ((fu = bnf_has_fu(bnf))) return fu;
3523 142 : if ((SUnits = bnf_get_sunits(bnf)))
3524 : {
3525 142 : pari_sp av = avma;
3526 142 : long e = gexpo(real_i(bnf_get_logfu(bnf)));
3527 142 : set_avma(av); if (e < 13) return vecsplice(bnf_build_units(bnf), 1);
3528 : }
3529 77 : return NULL;
3530 : }
3531 :
3532 : static GEN
3533 48950 : get_regulator(GEN A)
3534 : {
3535 48950 : pari_sp av = avma;
3536 : GEN R;
3537 :
3538 48950 : if (lg(A) == 1) return gen_1;
3539 48943 : R = det( rowslice(real_i(A), 1, lgcols(A)-2) );
3540 48943 : setabssign(R); return gc_leaf(av, R);
3541 : }
3542 :
3543 : /* return corrected archimedian components for elts of x (vector)
3544 : * (= log(sigma_i(x)) - log(|Nx|) / [K:Q]) */
3545 : static GEN
3546 40 : get_archclean(GEN nf, GEN x, long prec, int units)
3547 : {
3548 40 : long k, N, l = lg(x);
3549 40 : GEN M = cgetg(l, t_MAT);
3550 :
3551 40 : if (l == 1) return M;
3552 26 : N = nf_get_degree(nf);
3553 114 : for (k = 1; k < l; k++)
3554 : {
3555 88 : pari_sp av = avma;
3556 88 : GEN c = nf_cxlog(nf, gel(x,k), prec);
3557 88 : if (!c || (!units && !(c = cleanarch(c, N, NULL,prec)))) return NULL;
3558 88 : gel(M,k) = gc_GEN(av, c);
3559 : }
3560 26 : return M;
3561 : }
3562 : static void
3563 77 : SUnits_archclean(GEN nf, GEN SUnits, GEN *pmun, GEN *pC, long prec)
3564 : {
3565 77 : GEN ipi, M, X = gel(SUnits,1), U = gel(SUnits,2), G = gel(SUnits,3);
3566 77 : long k, N = nf_get_degree(nf), l = lg(X);
3567 :
3568 77 : M = cgetg(l, t_MAT);
3569 3640 : for (k = 1; k < l; k++)
3570 3563 : if (!(gel(M,k) = nf_cxlog(nf, gel(X,k), prec))) return;
3571 77 : ipi = invr(mppi(prec));
3572 77 : *pmun = cleanarch(RgM_ZM_mul(M, U), N, ipi, prec); /* not cleanarchunit ! */
3573 77 : if (*pmun) *pC = cleanarch(RgM_ZM_mul(M, G), N, ipi, prec);
3574 : }
3575 :
3576 : GEN
3577 97 : bnfnewprec_shallow(GEN bnf, long prec)
3578 : {
3579 97 : GEN nf0 = bnf_get_nf(bnf), nf, v, fu, matal, y, A, C;
3580 97 : GEN SUnits = bnf_get_sunits(bnf), Ur, Ga, Ge, M1, M2;
3581 97 : long r1, r2, prec0 = prec;
3582 :
3583 97 : nf_get_sign(nf0, &r1, &r2);
3584 97 : if (SUnits)
3585 : {
3586 77 : fu = matal = NULL;
3587 77 : prec += nbits2extraprec(gexpo(SUnits));
3588 : }
3589 : else
3590 : {
3591 20 : fu = bnf_build_units(bnf);
3592 20 : fu = vecslice(fu, 2, lg(fu)-1);
3593 20 : if (r1 + r2 > 1) {
3594 13 : long e = gexpo(bnf_get_logfu(bnf)) + 1 - TWOPOTBITS_IN_LONG;
3595 13 : if (e >= 0) prec += nbits2extraprec(e);
3596 : }
3597 20 : matal = bnf_build_matalpha(bnf);
3598 : }
3599 :
3600 97 : if (DEBUGLEVEL && prec0 != prec) pari_warn(warnprec,"bnfnewprec",prec);
3601 97 : for(C = NULL;;)
3602 0 : {
3603 97 : pari_sp av = avma;
3604 97 : nf = nfnewprec_shallow(nf0,prec);
3605 97 : if (SUnits)
3606 77 : SUnits_archclean(nf, SUnits, &A, &C, prec);
3607 : else
3608 : {
3609 20 : A = get_archclean(nf, fu, prec, 1);
3610 20 : if (A) C = get_archclean(nf, matal, prec, 0);
3611 : }
3612 97 : if (C) break;
3613 0 : set_avma(av); prec = precdbl(prec);
3614 0 : if (DEBUGLEVEL) pari_warn(warnprec,"bnfnewprec(extra)",prec);
3615 : }
3616 97 : y = leafcopy(bnf);
3617 97 : gel(y,3) = A;
3618 97 : gel(y,4) = C;
3619 97 : gel(y,7) = nf;
3620 97 : gel(y,8) = v = leafcopy(gel(bnf,8));
3621 97 : gel(v,2) = get_regulator(A);
3622 97 : v = gel(bnf,9);
3623 97 : if (lg(v) < 7) pari_err_TYPE("bnfnewprec [obsolete bnf format]", bnf);
3624 97 : Ur = gel(v,1);
3625 97 : Ge = gel(v,4);
3626 97 : Ga = nfV_cxlog(nf, Ge, prec);
3627 97 : M1 = gel(v,5);
3628 97 : M2 = gel(v,6);
3629 97 : gel(y,9) = get_clg2(bnf_get_cyc(bnf), Ga, C, Ur, Ge, M1, M2);
3630 97 : return y;
3631 : }
3632 : GEN
3633 7 : bnfnewprec(GEN bnf, long prec)
3634 : {
3635 7 : pari_sp av = avma;
3636 7 : return gc_GEN(av, bnfnewprec_shallow(checkbnf(bnf), prec));
3637 : }
3638 :
3639 : GEN
3640 0 : bnrnewprec_shallow(GEN bnr, long prec)
3641 : {
3642 0 : GEN y = cgetg(7,t_VEC);
3643 : long i;
3644 0 : gel(y,1) = bnfnewprec_shallow(bnr_get_bnf(bnr), prec);
3645 0 : for (i=2; i<7; i++) gel(y,i) = gel(bnr,i);
3646 0 : return y;
3647 : }
3648 : GEN
3649 7 : bnrnewprec(GEN bnr, long prec)
3650 : {
3651 7 : GEN y = cgetg(7,t_VEC);
3652 : long i;
3653 7 : checkbnr(bnr);
3654 7 : gel(y,1) = bnfnewprec(bnr_get_bnf(bnr), prec);
3655 42 : for (i=2; i<7; i++) gel(y,i) = gcopy(gel(bnr,i));
3656 7 : return y;
3657 : }
3658 :
3659 : static GEN
3660 65268 : buchall_end(GEN nf,GEN res, GEN clg2, GEN W, GEN B, GEN A, GEN C,GEN Vbase)
3661 : {
3662 65268 : GEN z = obj_init(9, 3);
3663 65268 : gel(z,1) = W;
3664 65268 : gel(z,2) = B;
3665 65268 : gel(z,3) = A;
3666 65268 : gel(z,4) = C;
3667 65268 : gel(z,5) = Vbase;
3668 65268 : gel(z,6) = gen_0;
3669 65268 : gel(z,7) = nf;
3670 65268 : gel(z,8) = res;
3671 65268 : gel(z,9) = clg2;
3672 65268 : return z;
3673 : }
3674 :
3675 : GEN
3676 2646 : bnfinit0(GEN P, long flag, GEN data, long prec)
3677 : {
3678 2646 : double c1 = 0., c2 = 0.;
3679 2646 : long fl, relpid = BNF_RELPID, max_fact = MAXTRY_FACT, idex = 0, nbthr = 1, s;
3680 :
3681 2646 : if (data)
3682 : {
3683 21 : long lx = lg(data);
3684 21 : if (typ(data) != t_VEC || lx > 7) pari_err_TYPE("bnfinit",data);
3685 21 : switch(lx)
3686 : {
3687 0 : case 7: nbthr = itou(gel(data,6)); if (nbthr <= 1) nbthr = 1-nbthr;
3688 0 : case 6: idex = itou(gel(data,5));
3689 0 : case 5: s = itou(gel(data,4)); if (s) max_fact = s;
3690 0 : case 4: s = itos(gel(data,3)); if (s) relpid = s < 0 ? 0 : s;
3691 14 : case 3: c2 = gtodouble(gel(data,2));
3692 21 : case 2: c1 = gtodouble(gel(data,1));
3693 : }
3694 : }
3695 2646 : switch(flag)
3696 : {
3697 1778 : case 2:
3698 1778 : case 0: fl = 0; break;
3699 868 : case 1: fl = nf_FORCE; break;
3700 0 : default: pari_err_FLAG("bnfinit");
3701 : return NULL; /* LCOV_EXCL_LINE */
3702 : }
3703 2646 : return Buchall_param(P, c1, c2, relpid, max_fact, idex, nbthr, fl, prec);
3704 : }
3705 : GEN
3706 62636 : Buchall(GEN P, long flag, long prec)
3707 62636 : { return Buchall_param(P, 0., 0., BNF_RELPID, MAXTRY_FACT, 0, 1, flag & nf_FORCE, prec); }
3708 :
3709 : static GEN
3710 1204 : Buchall_deg1(GEN nf)
3711 : {
3712 1204 : GEN v = cgetg(1,t_VEC), m = cgetg(1,t_MAT);
3713 1204 : GEN res, W, A, B, C, Vbase = cgetg(1,t_COL);
3714 1204 : GEN fu = v, R = gen_1, zu = mkvec2(gen_2, gen_m1);
3715 1204 : GEN clg1 = mkvec3(gen_1,v,v), clg2 = mkvecn(6, m,m,m,v,m,m);
3716 :
3717 1204 : W = A = B = C = m; res = mkvec5(clg1, R, gen_1, zu, fu);
3718 1204 : return buchall_end(nf,res,clg2,W,B,A,C,Vbase);
3719 : }
3720 :
3721 : /* return (small set of) indices of columns generating the same lattice as x.
3722 : * Assume HNF(x) is inexpensive (few rows, many columns).
3723 : * Dichotomy approach since interesting columns may be at the very end */
3724 : GEN
3725 64071 : extract_full_lattice(GEN x)
3726 : {
3727 64071 : long dj, j, k, l = lg(x);
3728 : GEN h, h2, H, v;
3729 :
3730 64071 : if (l < 200) return NULL; /* not worth it */
3731 :
3732 2 : v = vecsmalltrunc_init(l);
3733 2 : H = ZM_hnf(x);
3734 2 : h = cgetg(1, t_MAT);
3735 2 : dj = 1;
3736 86 : for (j = 1; j < l; )
3737 : {
3738 86 : pari_sp av = avma;
3739 86 : long lv = lg(v);
3740 :
3741 290 : for (k = 0; k < dj; k++) v[lv+k] = j+k;
3742 86 : setlg(v, lv + dj);
3743 86 : h2 = ZM_hnf(vecpermute(x, v));
3744 86 : if (ZM_equal(h, h2))
3745 : { /* these dj columns can be eliminated */
3746 34 : set_avma(av); setlg(v, lv);
3747 34 : j += dj;
3748 34 : if (j >= l) break;
3749 34 : dj <<= 1;
3750 34 : if (j + dj >= l) { dj = (l - j) >> 1; if (!dj) dj = 1; }
3751 : }
3752 52 : else if (dj > 1)
3753 : { /* at least one interesting column, try with first half of this set */
3754 34 : set_avma(av); setlg(v, lv);
3755 34 : dj >>= 1; /* > 0 */
3756 : }
3757 : else
3758 : { /* this column should be kept */
3759 18 : if (ZM_equal(h2, H)) break;
3760 16 : h = h2; j++;
3761 : }
3762 : }
3763 2 : return v;
3764 : }
3765 :
3766 : static void
3767 64136 : init_rel(RELCACHE_t *cache, FB_t *F, long add_need, ulong mod_p)
3768 : {
3769 64136 : const long n = F->KC + add_need; /* expected # of needed relations */
3770 : long i, j, k, p;
3771 : GEN c, P;
3772 : GEN R;
3773 :
3774 64136 : if (DEBUGLEVEL) err_printf("KCZ = %ld, KC = %ld, n = %ld\n", F->KCZ,F->KC,n);
3775 64136 : reallocate(cache, 10*n + 50); /* make room for lots of relations */
3776 64136 : cache->chk = cache->base;
3777 64136 : cache->end = cache->base + n;
3778 64136 : cache->relsup = add_need;
3779 64136 : cache->last = cache->base;
3780 64136 : cache->missing = lg(cache->basis) - 1;
3781 306429 : for (i = 1; i <= F->KCZ; i++)
3782 : { /* trivial relations (p) = prod P^e */
3783 242293 : p = F->FB[i]; P = gel(F->LV,p);
3784 242293 : if (!isclone(P)) continue;
3785 :
3786 : /* all prime divisors in FB */
3787 169123 : c = zero_Flv(F->KC); k = F->iLP[p];
3788 169123 : R = c; c += k;
3789 540060 : for (j = lg(P)-1; j; j--) c[j] = pr_get_e(gel(P,j));
3790 169123 : add_rel(cache, F, R, k+1, pr_get_p(gel(P,1)), mod_p);
3791 : }
3792 64136 : }
3793 :
3794 : /* Let z = \zeta_n in nf. List of not-obviously-dependent generators for
3795 : * cyclotomic units modulo torsion in Q(z) [independent when n a prime power]:
3796 : * - z^a - 1, n/(a,n) not a prime power, a \nmid n unless a=1, 1 <= a < n/2
3797 : * - (Z^a - 1)/(Z - 1), p^k || n, Z = z^{n/p^k}, (p,a) = 1, 1 < a <= (p^k-1)/2
3798 : */
3799 : GEN
3800 64136 : nfcyclotomicunits(GEN nf, GEN zu)
3801 : {
3802 64136 : long n = itos(gel(zu, 1)), n2, lP, i, a;
3803 : GEN z, fa, P, E, L, mz, powz;
3804 64136 : if (n <= 6) return cgetg(1, t_VEC);
3805 :
3806 1960 : z = algtobasis(nf,gel(zu, 2));
3807 1960 : if ((n & 3) == 2) { n = n >> 1; z = ZC_neg(z); } /* ensure n != 2 (mod 4) */
3808 1960 : n2 = n/2;
3809 1960 : mz = zk_multable(nf, z); /* multiplication by z */
3810 1960 : powz = cgetg(n2, t_VEC); gel(powz,1) = z;
3811 6335 : for (i = 2; i < n2; i++) gel(powz,i) = ZM_ZC_mul(mz, gel(powz,i-1));
3812 : /* powz[i] = z^i */
3813 :
3814 1960 : L = vectrunc_init(n);
3815 1960 : fa = factoru(n);
3816 1960 : P = gel(fa,1); lP = lg(P);
3817 1960 : E = gel(fa,2);
3818 4711 : for (i = 1; i < lP; i++)
3819 : { /* second kind */
3820 2751 : long p = P[i], k = E[i], pk = upowuu(p,k), pk2 = (pk-1) / 2;
3821 2751 : GEN u = gen_1;
3822 5068 : for (a = 2; a <= pk2; a++)
3823 : {
3824 2317 : u = nfadd(nf, u, gel(powz, (n/pk) * (a-1))); /* = (Z^a-1)/(Z-1) */
3825 2317 : if (a % p) vectrunc_append(L, u);
3826 : }
3827 : }
3828 6209 : if (lP > 2) for (a = 1; a < n2; a++)
3829 : { /* first kind, when n not a prime power */
3830 : ulong p;
3831 4249 : if (a > 1 && (n % a == 0 || uisprimepower(n/ugcd(a,n), &p))) continue;
3832 1869 : vectrunc_append(L, nfadd(nf, gel(powz, a), gen_m1));
3833 : }
3834 1960 : return L;
3835 : }
3836 : static void
3837 64136 : add_cyclotomic_units(GEN nf, GEN zu, RELCACHE_t *cache, FB_t *F, ulong mod_p)
3838 : {
3839 64136 : pari_sp av = avma;
3840 64136 : GEN L = nfcyclotomicunits(nf, zu);
3841 64136 : long i, l = lg(L);
3842 64136 : if (l > 1)
3843 : {
3844 1960 : GEN R = zero_Flv(F->KC);
3845 6048 : for(i = 1; i < l; i++) add_rel(cache, F, R, F->KC+1, gel(L,i), mod_p);
3846 : }
3847 64136 : set_avma(av);
3848 64136 : }
3849 :
3850 : static GEN
3851 123102 : trim_list(FB_t *F)
3852 : {
3853 123102 : pari_sp av = avma;
3854 123102 : GEN v, L_jid = F->L_jid, minidx = F->minidx, present = zero_Flv(F->KC);
3855 123102 : long i, j, imax = minss(lg(L_jid), F->KC + 1);
3856 :
3857 123102 : v = cgetg(imax, t_VECSMALL);
3858 1337966 : for (i = j = 1; i < imax; i++)
3859 : {
3860 1214864 : long k = minidx[ L_jid[i] ];
3861 1214864 : if (!present[k]) { v[j++] = L_jid[i]; present[k] = 1; }
3862 : }
3863 123102 : setlg(v, j); return gc_leaf(av, v);
3864 : }
3865 :
3866 : /* x t_INT or primitive ZC */
3867 : static void
3868 1668 : try_elt(RELCACHE_t *cache, FB_t *F, GEN nf, GEN x, FACT *fact, ulong mod_p)
3869 : {
3870 1668 : pari_sp av = avma;
3871 : long nz;
3872 : GEN R;
3873 :
3874 1668 : if (typ(x) == t_INT /* 2nd path can't fail */
3875 1668 : || !can_factor(F, nf, NULL, x, nfnorm(nf, x), fact)) return;
3876 : /* smooth element */
3877 1455 : R = set_fact(F, fact, NULL, &nz);
3878 : /* make sure we get maximal rank first, then allow all relations */
3879 1455 : (void)add_rel(cache, F, R, nz, x, mod_p);
3880 1455 : set_avma(av);
3881 : }
3882 :
3883 : static void
3884 55099 : matenlarge(GEN C, long h)
3885 : {
3886 55099 : GEN _0 = zerocol(h);
3887 : long i;
3888 1258547 : for (i = lg(C); --i; ) gel(C,i) = shallowconcat(gel(C,i), _0);
3889 55099 : }
3890 :
3891 : /* E = floating point embeddings */
3892 : static GEN
3893 55099 : matbotidembs(RELCACHE_t *cache, GEN E)
3894 : {
3895 55099 : long w = cache->last - cache->chk, h = cache->last - cache->base;
3896 55099 : long j, d = h - w, hE = nbrows(E);
3897 55099 : GEN y = cgetg(w+1,t_MAT), _0 = zerocol(h);
3898 216143 : for (j = 1; j <= w; j++)
3899 : {
3900 161044 : GEN c = shallowconcat(gel(E,j), _0);
3901 161044 : if (d + j >= 1) gel(c, d + j + hE) = gen_1;
3902 161044 : gel(y,j) = c;
3903 : }
3904 55099 : return y;
3905 : }
3906 : static GEN
3907 62540 : matbotid(RELCACHE_t *cache)
3908 : {
3909 62540 : long w = cache->last - cache->chk, h = cache->last - cache->base;
3910 62540 : long j, d = h - w;
3911 62540 : GEN y = cgetg(w+1,t_MAT);
3912 852352 : for (j = 1; j <= w; j++)
3913 : {
3914 789812 : GEN c = zerocol(h);
3915 789812 : if (d + j >= 1) gel(c, d + j) = gen_1;
3916 789812 : gel(y,j) = c;
3917 : }
3918 62540 : return y;
3919 : }
3920 :
3921 : static long
3922 73 : myprecdbl(long prec, GEN C)
3923 : {
3924 73 : long p = prec < 1280? precdbl(prec): (long)(prec * 1.5);
3925 73 : if (C) p = maxss(p, minss(3*p, prec + nbits2extraprec(gexpo(C))));
3926 73 : return p;
3927 : }
3928 :
3929 : static GEN
3930 57788 : _nfnewprec(GEN nf, long prec, long *isclone)
3931 : {
3932 57788 : GEN NF = gclone(nfnewprec_shallow(nf, prec));
3933 57788 : if (*isclone) gunclone(nf);
3934 57788 : *isclone = 1; return NF;
3935 : }
3936 :
3937 : /* In small_norm, LLL reduction produces v0 in I such that
3938 : * T2(v0) <= (4/3)^((n-1)/2) (NI sqrt(disc(K)))^(2/n)
3939 : * NI <= LIMCMAX^2. We consider v with T2(v) ~ T2(v0), hence
3940 : * Nv <= ((4/3)^((n-1)/2) / n)^(n/2) LIMCMAX^2 sqrt(disc(K)) */
3941 : static long
3942 64064 : small_norm_prec(long N, double LOGD, long LIMCMAX)
3943 : {
3944 64064 : double a = N/2. * ((N-1)/2.*log(4./3) - log((double)N));
3945 64064 : double b = 2*log((double)LIMCMAX) + LOGD/2;
3946 64064 : return nbits2prec(BITS_IN_LONG + (a + b) / M_LN2);
3947 : }
3948 :
3949 : /* Nrelid = nb relations per ideal, possibly 0. If flag is set, keep data in
3950 : * algebraic form. */
3951 : GEN
3952 65282 : Buchall_param(GEN P, double cbach, double cbach2, long Nrelid, long max_fact, long idex, long nbthr, long flag, long prec)
3953 : {
3954 : pari_timer T;
3955 65282 : pari_sp av0 = avma, av, av2;
3956 : long PREC, N, R1, R2, RU, low, high, LIMC0, LIMC, LIMC2, LIMCMAX, zc, i;
3957 65282 : long LIMres, bit = 0, flag_nfinit = 0, nfisclone = 0;
3958 65282 : long nreldep, sfb_trials, need, old_need, precdouble = 0, TRIES = 0;
3959 : long done_small, small_fail, fail_limit, squash_index;
3960 : double LOGD, LOGD2, lim;
3961 65282 : GEN computed = NULL, fu = NULL, zu, nf, D, A, W, R, h, Ce, PERM;
3962 : GEN small_multiplier, auts, cyclic, embs, SUnits;
3963 : GEN res, L, invhr, B, C, lambda, dep, clg1, clg2, Vbase;
3964 65282 : const char *precpb = NULL;
3965 65282 : ulong mod_p = Mod_p;
3966 65282 : long cnt_p = 0;
3967 65282 : REL_t *old_cache = NULL;
3968 : nfmaxord_t nfT;
3969 : RELCACHE_t cache;
3970 : FB_t F;
3971 : GRHcheck_t GRHcheck;
3972 : FACT *fact;
3973 :
3974 65282 : if (DEBUGLEVEL) timer_start(&T);
3975 65282 : P = get_nfpol(P, &nf);
3976 65268 : if (degpol(P)==2) Nrelid = 0;
3977 65268 : if (nf)
3978 3871 : D = nf_get_disc(nf);
3979 : else
3980 : {
3981 61397 : nfinit_basic(&nfT, P);
3982 61397 : D = nfT.dK;
3983 61397 : if (!ZX_is_monic(nfT.T0))
3984 : {
3985 14 : pari_warn(warner,"nonmonic polynomial in bnfinit, using polredbest");
3986 14 : flag_nfinit = nf_RED;
3987 : }
3988 : }
3989 65268 : PREC = maxss(DEFAULTPREC, prec);
3990 65268 : N = degpol(P);
3991 65268 : if (N <= 1)
3992 : {
3993 1204 : if (!nf) nf = nfinit_complete(&nfT, flag_nfinit, PREC);
3994 1204 : return gc_GEN(av0, Buchall_deg1(nf));
3995 : }
3996 64064 : D = absi_shallow(D);
3997 64064 : LOGD = dbllog2(D) * M_LN2;
3998 64064 : LOGD2 = LOGD*LOGD;
3999 64064 : LIMCMAX = (long)(4.*LOGD2);
4000 64064 : if (nf) PREC = maxss(PREC, nf_get_prec(nf));
4001 64064 : PREC = maxss(PREC, nbits2prec((long)(LOGD2 * 0.02) + N*N));
4002 :
4003 64064 : if (nf) PREC = maxss(PREC, nf_get_prec(nf));
4004 64064 : PREC = maxss(PREC, nbits2prec((long)(LOGD2 * 0.02) + N*N));
4005 64064 : PREC = maxss(PREC, small_norm_prec(N, LOGD, LIMCMAX));
4006 64064 : if (DEBUGLEVEL) err_printf("PREC = %ld\n", PREC);
4007 :
4008 64064 : if (!nf)
4009 60403 : nf = nfinit_complete(&nfT, flag_nfinit, PREC);
4010 3661 : else if (nf_get_prec(nf) < PREC)
4011 161 : nf = nfnewprec_shallow(nf, PREC);
4012 64064 : zu = nfrootsof1(nf);
4013 64064 : gel(zu,2) = nf_to_scalar_or_alg(nf, gel(zu,2));
4014 :
4015 64064 : nf_get_sign(nf, &R1, &R2); RU = R1+R2;
4016 64064 : auts = automorphism_matrices(nf, &cyclic);
4017 64064 : F.embperm = automorphism_perms(nf_get_M(nf), auts, cyclic, R1, R2, N);
4018 64064 : if (DEBUGLEVEL)
4019 : {
4020 0 : timer_printf(&T, "nfinit & nfrootsof1");
4021 0 : err_printf("%s bnf: R1 = %ld, R2 = %ld\nD = %Ps\n",
4022 : flag? "Algebraic": "Floating point", R1,R2, D);
4023 : }
4024 64064 : if (LOGD < 20.)
4025 : { /* tiny disc, Minkowski may be smaller than Bach */
4026 62601 : lim = exp(-N + R2 * log(4/M_PI) + LOGD/2) * sqrt(2*M_PI*N);
4027 62601 : if (lim < 3) lim = 3;
4028 : }
4029 : else /* to be ignored */
4030 1463 : lim = -1;
4031 64064 : if (cbach > 12.) {
4032 0 : if (cbach2 < cbach) cbach2 = cbach;
4033 0 : cbach = 12.;
4034 : }
4035 64064 : if (cbach < 0.)
4036 0 : pari_err_DOMAIN("Buchall","Bach constant","<",gen_0,dbltor(cbach));
4037 :
4038 64064 : cache.base = NULL; F.subFB = NULL; F.LP = NULL; SUnits = Ce = NULL;
4039 64064 : init_GRHcheck(&GRHcheck, N, R1, LOGD);
4040 64064 : high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
4041 312431 : while (!GRHchk(nf, &GRHcheck, high)) { low = high; high *= 2; }
4042 248409 : while (high - low > 1)
4043 : {
4044 184345 : long test = (low+high)/2;
4045 184345 : if (GRHchk(nf, &GRHcheck, test)) high = test; else low = test;
4046 : }
4047 64064 : LIMC2 = (high == LIMC0+1 && GRHchk(nf, &GRHcheck, LIMC0))? LIMC0: high;
4048 64064 : if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
4049 : /* Assuming GRH, {P, NP <= LIMC2} generate Cl(K) */
4050 64064 : if (DEBUGLEVEL) err_printf("LIMC2 = %ld\n", LIMC2);
4051 64064 : LIMC0 = (long)(cbach*LOGD2); /* initial value for LIMC */
4052 64064 : LIMC = cbach? LIMC0: LIMC2; /* use {P, NP <= LIMC} as a factorbase */
4053 64064 : LIMC = maxss(LIMC, nthideal(&GRHcheck, nf, N));
4054 64064 : if (DEBUGLEVEL) timer_printf(&T, "computing Bach constant");
4055 64064 : LIMres = primeneeded(N, R1, R2, LOGD);
4056 64064 : cache_prime_dec(&GRHcheck, LIMres, nf);
4057 : /* invhr ~ 2^r1 (2pi)^r2 / sqrt(D) w * Res(zeta_K, s=1) = 1 / hR */
4058 128128 : invhr = gmul(gdiv(gmul2n(powru(mppi(DEFAULTPREC), R2), RU),
4059 64064 : mulri(gsqrt(D,DEFAULTPREC),gel(zu,1))),
4060 : compute_invres(&GRHcheck, LIMres));
4061 64064 : if (DEBUGLEVEL) timer_printf(&T, "computing inverse of hR");
4062 64064 : av = avma;
4063 :
4064 66313 : START:
4065 66313 : if (DEBUGLEVEL) timer_start(&T);
4066 66313 : if (TRIES) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
4067 66313 : if (DEBUGLEVEL && LIMC > LIMC0)
4068 0 : err_printf("%s*** Bach constant: %f\n", TRIES?"\n":"", LIMC/LOGD2);
4069 66313 : if (cache.base)
4070 : {
4071 : REL_t *rel;
4072 3622 : for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
4073 3550 : if (rel->m) i++;
4074 72 : computed = cgetg(i, t_VEC);
4075 3622 : for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
4076 3550 : if (rel->m) gel(computed, i++) = rel->m;
4077 72 : computed = gclone(computed); delete_cache(&cache);
4078 : }
4079 66313 : TRIES++; set_avma(av);
4080 66313 : if (F.LP) delete_FB(&F);
4081 66313 : if (LIMC2 < LIMC) LIMC2 = LIMC;
4082 66313 : if (DEBUGLEVEL) { err_printf("LIMC = %ld, LIMC2 = %ld\n",LIMC,LIMC2); }
4083 :
4084 66313 : FBgen(&F, nf, N, LIMC, LIMC2, &GRHcheck);
4085 66313 : if (!F.KC) goto START;
4086 66313 : av = avma;
4087 66313 : subFBgen(&F,auts,cyclic,lim < 0? LIMC2: mindd(lim,LIMC2),MINSFB);
4088 66313 : if (lg(F.subFB) == 1) goto START;
4089 64136 : if (DEBUGLEVEL)
4090 0 : timer_printf(&T, "factorbase (#subFB = %ld) and ideal permutations",
4091 0 : lg(F.subFB)-1);
4092 :
4093 64136 : fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
4094 64136 : PERM = leafcopy(F.perm); /* to be restored in case of precision increase */
4095 64136 : cache.basis = zero_Flm_copy(F.KC,F.KC);
4096 64136 : small_multiplier = zero_Flv(F.KC);
4097 64136 : done_small = small_fail = squash_index = zc = sfb_trials = nreldep = 0;
4098 64136 : fail_limit = F.KC + 1;
4099 64136 : W = A = R = NULL;
4100 64136 : av2 = avma;
4101 64136 : init_rel(&cache, &F, RELSUP + RU-1, mod_p);
4102 64136 : old_need = need = cache.end - cache.last;
4103 64136 : add_cyclotomic_units(nf, zu, &cache, &F, mod_p);
4104 64136 : if (DEBUGLEVEL) err_printf("\n");
4105 64136 : cache.end = cache.last + need;
4106 :
4107 64136 : if (computed)
4108 : {
4109 1740 : for (i = 1; i < lg(computed); i++)
4110 1668 : try_elt(&cache, &F, nf, gel(computed, i), fact, mod_p);
4111 72 : gunclone(computed);
4112 72 : if (DEBUGLEVEL && i > 1)
4113 0 : timer_printf(&T, "including already computed relations");
4114 72 : need = 0;
4115 : }
4116 :
4117 : do
4118 : {
4119 : GEN Ar, C0;
4120 : do
4121 : {
4122 123254 : pari_sp av4 = avma;
4123 123254 : if (need > 0)
4124 : {
4125 123102 : long oneed = cache.end - cache.last;
4126 : /* Test below can be true if small_norm did not find enough linearly
4127 : * dependent relations */
4128 123102 : if (need < oneed) need = oneed;
4129 123102 : pre_allocate(&cache, need+lg(auts)-1+(R ? lg(W)-1 : 0));
4130 123102 : cache.end = cache.last + need;
4131 123102 : F.L_jid = trim_list(&F);
4132 : }
4133 123254 : if (need > 0 && Nrelid > 0 && (done_small <= F.KC+1 || A) &&
4134 70313 : small_fail <= fail_limit &&
4135 70313 : cache.last < cache.base + 2*F.KC+2*RU+RELSUP /* heuristic */)
4136 : {
4137 66857 : long j, k, LIE = (R && lg(W) > 1 && (done_small % 2));
4138 66857 : REL_t *last = cache.last;
4139 66857 : pari_sp av3 = avma;
4140 66857 : if (LIE)
4141 : { /* We have full rank for class group and unit. The following tries to
4142 : * improve the prime group lattice by looking for relations involving
4143 : * the primes generating the class group. */
4144 3393 : long n = lg(W)-1; /* need n relations to squash the class group */
4145 3393 : F.L_jid = vecslice(F.perm, 1, n);
4146 3393 : cache.end = cache.last + n;
4147 : /* Lie to the add_rel subsystem: pretend we miss relations involving
4148 : * the primes generating the class group (and only those). */
4149 3393 : cache.missing = n;
4150 10603 : for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 0;
4151 : }
4152 66857 : j = done_small % (F.KC+1);
4153 66857 : if (j && !A)
4154 : { /* Prevent considering both P_iP_j and P_jP_i in small_norm */
4155 : /* Not all elements end up in F.L_jid (eliminated by hnfspec/add or
4156 : * by trim_list): keep track of which ideals are being considered
4157 : * at each run. */
4158 414 : long mj = small_multiplier[j];
4159 6569 : for (i = k = 1; i < lg(F.L_jid); i++)
4160 6155 : if (F.L_jid[i] > mj)
4161 : {
4162 6155 : small_multiplier[F.L_jid[i]] = j;
4163 6155 : F.L_jid[k++] = F.L_jid[i];
4164 : }
4165 414 : setlg(F.L_jid, k);
4166 : }
4167 66857 : if (lg(F.L_jid) > 1) small_norm(&cache, &F, nf, Nrelid, max_fact, idex, nbthr, fact, j, mod_p);
4168 66857 : F.L_jid = F.perm; set_avma(av3);
4169 66857 : if (!A && cache.last != last) small_fail = 0; else small_fail++;
4170 66857 : if (LIE)
4171 : { /* restore add_rel subsystem: undo above lie */
4172 3393 : long n = lg(W) - 1;
4173 10603 : for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 1;
4174 3393 : cache.missing = 0;
4175 : }
4176 66857 : cache.end = cache.last;
4177 66857 : done_small++;
4178 66857 : need = F.sfb_chg = 0;
4179 : }
4180 123254 : if (need > 0)
4181 : { /* Random relations */
4182 56245 : if (++nreldep > F.MAXDEPSIZESFB) {
4183 28 : if (++sfb_trials > SFB_MAX && LIMC < LIMCMAX/2) goto START;
4184 28 : F.sfb_chg = sfb_INCREASE;
4185 28 : nreldep = 0;
4186 : }
4187 56217 : else if (!(nreldep % F.MAXDEPSFB))
4188 26505 : F.sfb_chg = sfb_CHANGE;
4189 56245 : if (F.sfb_chg && !subFB_change(&F)) goto START;
4190 56173 : rnd_rel(&cache, &F, nf, max_fact, nbthr, fact, mod_p);
4191 56173 : cnt_p++;
4192 56173 : if(cnt_p > 2*F.KC)
4193 : {
4194 : long i, j;
4195 141 : mod_p = unextprime(mod_p+1);
4196 141 : cnt_p = 0;
4197 141 : if (DEBUGLEVEL) pari_warn(warner,"Changing test modulus to %lu",mod_p);
4198 837 : for (i = 1; i < F.KC; i++)
4199 26740 : for (j = 1; j < F.KC; j++) ucoeff(cache.basis,i,j) = 0;
4200 : }
4201 56173 : F.L_jid = F.perm;
4202 : }
4203 123182 : if (DEBUGLEVEL) timer_start(&T);
4204 123182 : if (precpb)
4205 : {
4206 : REL_t *rel;
4207 80 : if (DEBUGLEVEL)
4208 : {
4209 0 : char str[64]; sprintf(str,"Buchall_param (%s)",precpb);
4210 0 : pari_warn(warnprec,str,PREC);
4211 : }
4212 80 : nf = _nfnewprec(nf, PREC, &nfisclone);
4213 80 : precdouble++; precpb = NULL;
4214 :
4215 80 : if (flag)
4216 : { /* recompute embs only, no need to redo HNF */
4217 38 : long j, le = lg(embs), lC = lg(C);
4218 38 : GEN E, M = nf_get_M(nf);
4219 38 : set_avma(av4);
4220 12611 : for (rel = cache.base+1, i = 1; i < le; i++,rel++)
4221 12573 : gel(embs,i) = rel_embed(rel, &F, embs, i, M, RU, R1, PREC);
4222 38 : E = RgM_ZM_mul(embs, rowslice(C, RU+1, nbrows(C)));
4223 12611 : for (j = 1; j < lC; j++)
4224 65595 : for (i = 1; i <= RU; i++) gcoeff(C,i,j) = gcoeff(E,i,j);
4225 38 : av4 = avma;
4226 : }
4227 : else
4228 : { /* recompute embs + HNF */
4229 10318 : for(i = 1; i < lg(PERM); i++) F.perm[i] = PERM[i];
4230 42 : cache.chk = cache.base;
4231 42 : W = NULL;
4232 : }
4233 80 : if (DEBUGLEVEL) timer_printf(&T, "increasing accuracy");
4234 : }
4235 123182 : set_avma(av4);
4236 123182 : if (cache.chk != cache.last)
4237 : { /* Reduce relation matrices */
4238 122198 : long l = cache.last - cache.chk + 1, j;
4239 122198 : GEN mat = cgetg(l, t_MAT);
4240 : REL_t *rel;
4241 :
4242 1126634 : for (j=1,rel = cache.chk + 1; j < l; rel++,j++) gel(mat,j) = rel->R;
4243 122198 : if (!flag || W)
4244 : {
4245 59658 : embs = get_embs(&F, &cache, nf, embs, PREC);
4246 59658 : if (DEBUGLEVEL && timer_get(&T) > 1)
4247 0 : timer_printf(&T, "floating point embeddings");
4248 : }
4249 122198 : if (!W)
4250 : { /* never reduced before */
4251 64178 : C = flag? matbotid(&cache): embs;
4252 64178 : W = hnfspec_i(mat, F.perm, &dep, &B, &C, F.subFB ? lg(F.subFB)-1:0);
4253 64178 : if (DEBUGLEVEL)
4254 0 : timer_printf(&T, "hnfspec [%ld x %ld]", lg(F.perm)-1, l-1);
4255 64178 : if (flag)
4256 : {
4257 62540 : PREC += nbits2extraprec(gexpo(C));
4258 62540 : if (nf_get_prec(nf) < PREC) nf = _nfnewprec(nf, PREC, &nfisclone);
4259 62540 : embs = get_embs(&F, &cache, nf, embs, PREC);
4260 62540 : C = vconcat(RgM_ZM_mul(embs, C), C);
4261 : }
4262 64178 : if (DEBUGLEVEL)
4263 0 : timer_printf(&T, "hnfspec floating points");
4264 : }
4265 : else
4266 : {
4267 58020 : long k = lg(embs);
4268 58020 : GEN E = vecslice(embs, k-l+1,k-1);
4269 58020 : if (flag)
4270 : {
4271 55099 : E = matbotidembs(&cache, E);
4272 55099 : matenlarge(C, cache.last - cache.chk);
4273 : }
4274 58020 : W = hnfadd_i(W, F.perm, &dep, &B, &C, mat, E);
4275 58020 : if (DEBUGLEVEL)
4276 0 : timer_printf(&T, "hnfadd (%ld + %ld)", l-1, lg(dep)-1);
4277 : }
4278 122198 : (void)gc_all(av2, 5, &W,&C,&B,&dep,&embs);
4279 122198 : cache.chk = cache.last;
4280 : }
4281 984 : else if (!W)
4282 : {
4283 0 : need = old_need;
4284 0 : F.L_jid = vecslice(F.perm, 1, need);
4285 0 : continue;
4286 : }
4287 123182 : need = F.KC - (lg(W)-1) - (lg(B)-1);
4288 123182 : if (!need && cache.missing)
4289 : { /* The test above will never be true except if 27449|class number.
4290 : * Ensure that if we have maximal rank for the ideal lattice, then
4291 : * cache.missing == 0. */
4292 0 : mod_p = 0;
4293 0 : for (i = 1; cache.missing; i++)
4294 0 : if (!mael(cache.basis, i, i))
4295 : {
4296 : long j;
4297 0 : cache.missing--; mael(cache.basis, i, i) = 1;
4298 0 : for (j = i+1; j <= F.KC; j++) mael(cache.basis, j, i) = 0;
4299 : }
4300 : }
4301 123182 : zc = (lg(C)-1) - (lg(B)-1) - (lg(W)-1);
4302 123182 : if (RU-1-zc > 0) need = minss(need + RU-1-zc, F.KC); /* for units */
4303 123182 : if (need)
4304 : { /* dependent rows */
4305 1578 : F.L_jid = vecslice(F.perm, 1, need);
4306 1578 : vecsmall_sort(F.L_jid);
4307 1578 : if (need != old_need) { nreldep = 0; old_need = need; }
4308 : }
4309 : else
4310 : { /* If the relation lattice is too small, check will be > 1 and we will
4311 : * do a new run of small_norm/rnd_rel asking for 1 relation. This often
4312 : * gives a relation involving L_jid[1]. We rotate the first element of
4313 : * L_jid in order to increase the probability of finding relations that
4314 : * increases the lattice. */
4315 121604 : long j, n = lg(W) - 1;
4316 121604 : if (n > 1 && squash_index % n)
4317 : {
4318 9031 : F.L_jid = leafcopy(F.perm);
4319 36660 : for (j = 1; j <= n; j++)
4320 27629 : F.L_jid[j] = F.perm[1 + (j + squash_index - 1) % n];
4321 : }
4322 : else
4323 112573 : F.L_jid = F.perm;
4324 121604 : squash_index++;
4325 : }
4326 : }
4327 123182 : while (need);
4328 :
4329 121604 : if (!A)
4330 : {
4331 64143 : small_fail = old_need = 0;
4332 64143 : fail_limit = maxss(F.KC / FAIL_DIVISOR, MINFAIL);
4333 : }
4334 121604 : A = vecslice(C, 1, zc); /* cols corresponding to units */
4335 121604 : if (flag) A = rowslice(A, 1, RU);
4336 121604 : Ar = real_i(A);
4337 121604 : R = compute_multiple_of_R(Ar, RU, N, &need, &bit, &lambda);
4338 121604 : if (need < old_need) small_fail = 0;
4339 : #if 0 /* A good idea if we are indeed stuck but needs tuning */
4340 : /* we have computed way more relations than should be necessary */
4341 : if (TRIES < 3 && LIMC < LIMCMAX / 8 &&
4342 : cache.last - cache.base > 10 * F.KC) goto START;
4343 : #endif
4344 121604 : old_need = need;
4345 121604 : if (!lambda)
4346 10 : { precpb = "bestappr"; PREC = myprecdbl(PREC, flag? C: NULL); continue; }
4347 121594 : if (!R)
4348 : { /* not full rank for units */
4349 25462 : if (!need)
4350 0 : { precpb = "regulator"; PREC = myprecdbl(PREC, flag? C: NULL); }
4351 25462 : continue;
4352 : }
4353 96132 : if (cache.last==old_cache) { need=1; continue; }
4354 96031 : old_cache = cache.last;
4355 96031 : h = ZM_det_triangular(W);
4356 96031 : if (DEBUGLEVEL) err_printf("\n#### Tentative class number: %Ps\n", h);
4357 96031 : i = compute_R(lambda, mulir(h,invhr), &L, &R);
4358 96031 : if (DEBUGLEVEL)
4359 : {
4360 0 : err_printf("\n");
4361 0 : timer_printf(&T, "computing regulator and check");
4362 : }
4363 96031 : switch(i)
4364 : {
4365 31897 : case fupb_RELAT:
4366 31897 : need = 1; /* not enough relations */
4367 31897 : continue;
4368 63 : case fupb_PRECI: /* prec problem unless we cheat on Bach constant */
4369 63 : if ((precdouble&7) == 7 && LIMC <= LIMCMAX/2) goto START;
4370 63 : precpb = "compute_R"; PREC = myprecdbl(PREC, flag? C: NULL);
4371 63 : continue;
4372 : }
4373 : /* DONE */
4374 :
4375 64071 : if (F.KCZ2 > F.KCZ)
4376 : {
4377 7 : if (F.sfb_chg && !subFB_change(&F)) goto START;
4378 7 : if (!be_honest(&F, nf, auts, fact)) goto START;
4379 7 : if (DEBUGLEVEL) timer_printf(&T, "to be honest");
4380 : }
4381 64071 : F.KCZ2 = 0; /* be honest only once */
4382 :
4383 : /* fundamental units */
4384 : {
4385 64071 : GEN AU, CU, U, v = extract_full_lattice(L); /* L may be large */
4386 64071 : CU = NULL;
4387 64071 : if (v) { A = vecpermute(A, v); L = vecpermute(L, v); }
4388 : /* arch. components of fund. units */
4389 64071 : U = ZM_lll(L, 0.99, LLL_IM);
4390 64071 : U = ZM_mul(U, lll(RgM_ZM_mul(real_i(A), U)));
4391 64071 : if (DEBUGLEVEL) timer_printf(&T, "units LLL");
4392 64071 : AU = RgM_ZM_mul(A, U);
4393 64071 : A = cleanarchunit(AU, N, NULL, PREC);
4394 64071 : if (RU > 1 /* if there are fund units, test we have correct regulator */
4395 48860 : && (!A || lg(A) < RU || expo(subrr(get_regulator(A), R)) > -1))
4396 7 : {
4397 7 : long add = nbits2extraprec( gexpo(AU) + 64 ) - gprecision(AU);
4398 7 : long t = maxss(PREC * 0.15, add);
4399 7 : if (!A && DEBUGLEVEL) err_printf("### Incorrect units lognorm");
4400 7 : precpb = "cleanarch"; PREC += maxss(t, EXTRAPREC64); continue;
4401 : }
4402 64064 : if (flag)
4403 : {
4404 62489 : long l = lgcols(C) - RU;
4405 : REL_t *rel;
4406 62489 : SUnits = cgetg(l, t_COL);
4407 1011207 : for (rel = cache.base+1, i = 1; i < l; i++,rel++)
4408 948718 : set_rel_alpha(rel, auts, SUnits, i);
4409 62489 : if (RU > 1)
4410 : {
4411 47775 : GEN c = v? vecpermute(C,v): vecslice(C,1,zc);
4412 47775 : CU = ZM_mul(rowslice(c, RU+1, nbrows(c)), U);
4413 : }
4414 : }
4415 64064 : if (DEBUGLEVEL) err_printf("\n#### Computing fundamental units\n");
4416 64064 : fu = getfu(nf, &A, CU? &U: NULL, PREC);
4417 64064 : CU = CU? ZM_mul(CU, U): cgetg(1, t_MAT);
4418 64064 : if (DEBUGLEVEL) timer_printf(&T, "getfu");
4419 64064 : Ce = vecslice(C, zc+1, lg(C)-1);
4420 64064 : if (flag) SUnits = mkvec4(SUnits, CU, rowslice(Ce, RU+1, nbrows(Ce)),
4421 : utoipos(LIMC));
4422 : }
4423 : /* class group generators */
4424 64064 : if (flag) Ce = rowslice(Ce, 1, RU);
4425 64064 : C0 = Ce; Ce = cleanarch(Ce, N, NULL, PREC);
4426 64064 : if (!Ce) {
4427 0 : long add = nbits2extraprec( gexpo(C0) + 64 ) - gprecision(C0);
4428 0 : precpb = "cleanarch"; PREC += maxss(add, 1);
4429 : }
4430 64064 : if (DEBUGLEVEL) timer_printf(&T, "cleanarch");
4431 121604 : } while (need || precpb);
4432 :
4433 64064 : Vbase = vecpermute(F.LP, F.perm);
4434 64064 : if (!fu) fu = cgetg(1, t_MAT);
4435 64064 : if (!SUnits) SUnits = gen_1;
4436 64064 : clg1 = class_group_gen(nf,W,Ce,Vbase,PREC, &clg2);
4437 64064 : res = mkvec5(clg1, R, SUnits, zu, fu);
4438 64064 : res = buchall_end(nf,res,clg2,W,B,A,Ce,Vbase);
4439 64064 : delete_FB(&F);
4440 64064 : res = gc_GEN(av0, res);
4441 64064 : if (flag) obj_insert_shallow(res, MATAL, cgetg(1,t_VEC));
4442 64064 : if (nfisclone) gunclone(nf);
4443 64064 : delete_cache(&cache);
4444 64064 : free_GRHcheck(&GRHcheck);
4445 64064 : return res;
4446 : }
|