Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is to exceed 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - buch2.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.1 lcov report (development 24988-2584e74448) Lines: 2081 2280 91.3 %
Date: 2020-01-26 05:57:03 Functions: 147 158 93.0 %
Legend: Lines: hit not hit

          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. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : #include "pari.h"
      14             : #include "paripriv.h"
      15             : /*******************************************************************/
      16             : /*                                                                 */
      17             : /*         CLASS GROUP AND REGULATOR (McCURLEY, BUCHMANN)          */
      18             : /*                    GENERAL NUMBER FIELDS                        */
      19             : /*                                                                 */
      20             : /*******************************************************************/
      21             : /* get_random_ideal */
      22             : static const long RANDOM_BITS = 4;
      23             : /* Buchall */
      24             : static const long RELSUP = 5;
      25             : static const long FAIL_DIVISOR = 32;
      26             : static const long MINFAIL = 10;
      27             : /* small_norm */
      28             : static const long BNF_RELPID = 4;
      29             : static const long BMULT = 8;
      30             : static const long maxtry_ELEMENT = 1000*1000;
      31             : static const long maxtry_DEP = 20;
      32             : static const long maxtry_FACT = 500;
      33             : /* rnd_rel */
      34             : static const long RND_REL_RELPID = 1;
      35             : /* random relations */
      36             : static const long MINSFB = 3;
      37             : static const long SFB_MAX = 3;
      38             : static const long DEPSIZESFBMULT = 16;
      39             : static const long DEPSFBDIV = 10;
      40             : /* add_rel_i */
      41             : static const ulong mod_p = 27449UL;
      42             : /* be_honest */
      43             : static const long maxtry_HONEST = 50;
      44             : 
      45             : typedef struct FACT {
      46             :     long pr, ex;
      47             : } FACT;
      48             : 
      49             : typedef struct subFB_t {
      50             :   GEN subFB;
      51             :   struct subFB_t *old;
      52             : } subFB_t;
      53             : 
      54             : /* a factor base contains only non-inert primes
      55             :  * KC = # of P in factor base (p <= n, NP <= n2)
      56             :  * KC2= # of P assumed to generate class group (NP <= n2)
      57             :  *
      58             :  * KCZ = # of rational primes under ideals counted by KC
      59             :  * KCZ2= same for KC2 */
      60             : 
      61             : typedef struct FB_t {
      62             :   GEN FB; /* FB[i] = i-th rational prime used in factor base */
      63             :   GEN LP; /* vector of all prime ideals in FB */
      64             :   GEN *LV; /* LV[p] = vector of P|p, NP <= n2
      65             :             * isclone() is set for LV[p] iff all P|p are in FB
      66             :             * LV[i], i not prime or i > n2, is undefined! */
      67             :   GEN iLP; /* iLP[p] = i such that LV[p] = [LP[i],...] */
      68             :   GEN L_jid; /* indexes of "useful" prime ideals for rnd_rel */
      69             :   long KC, KCZ, KCZ2;
      70             :   GEN subFB; /* LP o subFB =  part of FB used to build random relations */
      71             :   int sfb_chg; /* need to change subFB ? */
      72             :   GEN perm; /* permutation of LP used to represent relations [updated by
      73             :                hnfspec/hnfadd: dense rows come first] */
      74             :   GEN idealperm; /* permutation of ideals under field automorphisms */
      75             :   GEN minidx; /* minidx[i] min ideal in orbit of LP[i] under field autom */
      76             :   subFB_t *allsubFB; /* all subFB's used */
      77             :   GEN embperm; /* permutations of the complex embeddings */
      78             :   long MAXDEPSIZESFB; /* # trials before increasing subFB */
      79             :   long MAXDEPSFB; /* MAXDEPSIZESFB / DEPSFBDIV, # trials befor rotating subFB */
      80             : } FB_t;
      81             : 
      82             : enum { sfb_CHANGE = 1, sfb_INCREASE = 2 };
      83             : 
      84             : typedef struct REL_t {
      85             :   GEN R; /* relation vector as t_VECSMALL; clone */
      86             :   long nz; /* index of first non-zero elt in R (hash) */
      87             :   GEN m; /* pseudo-minimum yielding the relation; clone */
      88             :   long relorig; /* relation this one is an image of */
      89             :   long relaut; /* automorphim used to compute this relation from the original */
      90             :   GEN emb; /* archimedean embeddings */
      91             :   GEN junk[2]; /*make sure sizeof(struct) is a power of two.*/
      92             : } REL_t;
      93             : 
      94             : typedef struct RELCACHE_t {
      95             :   REL_t *chk; /* last checkpoint */
      96             :   REL_t *base; /* first rel found */
      97             :   REL_t *last; /* last rel found so far */
      98             :   REL_t *end; /* target for last relation. base <= last <= end */
      99             :   size_t len; /* number of rels pre-allocated in base */
     100             :   long relsup; /* how many linearly dependent relations we allow */
     101             :   GEN basis; /* mod p basis (generating family actually) */
     102             :   ulong missing; /* missing vectors in generating family above */
     103             : } RELCACHE_t;
     104             : 
     105             : typedef struct FP_t {
     106             :   double **q;
     107             :   GEN x;
     108             :   double *y;
     109             :   double *z;
     110             :   double *v;
     111             : } FP_t;
     112             : 
     113             : typedef struct RNDREL_t {
     114             :   long jid;
     115             :   GEN ex;
     116             : } RNDREL_t;
     117             : 
     118             : static void
     119           0 : wr_rel(GEN e)
     120             : {
     121           0 :   long i, l = lg(e);
     122           0 :   for (i = 1; i < l; i++)
     123           0 :     if (e[i]) err_printf("%ld^%ld ",i,e[i]);
     124           0 : }
     125             : static void
     126           0 : dbg_newrel(RELCACHE_t *cache)
     127             : {
     128           0 :   if (DEBUGLEVEL > 1)
     129             :   {
     130           0 :     err_printf("\n++++ cglob = %ld\nrel = ", cache->last - cache->base);
     131           0 :     wr_rel(cache->last->R);
     132           0 :     err_printf("\n");
     133             :   }
     134             :   else
     135           0 :     err_printf("%ld ", cache->last - cache->base);
     136           0 : }
     137             : 
     138             : static void
     139       11872 : delete_cache(RELCACHE_t *M)
     140             : {
     141             :   REL_t *rel;
     142      187413 :   for (rel = M->base+1; rel <= M->last; rel++)
     143             :   {
     144      175541 :     gunclone(rel->R);
     145      175541 :     if (rel->m) gunclone(rel->m);
     146             :   }
     147       11872 :   pari_free((void*)M->base); M->base = NULL;
     148       11872 : }
     149             : 
     150             : static void
     151       12495 : delete_FB(FB_t *F)
     152             : {
     153             :   subFB_t *s, *sold;
     154       12495 :   for (s = F->allsubFB; s; s = sold) { sold = s->old; pari_free(s); }
     155       12495 :   gunclone(F->minidx);
     156       12495 :   gunclone(F->idealperm);
     157       12495 : }
     158             : 
     159             : static void
     160       11872 : reallocate(RELCACHE_t *M, long len)
     161             : {
     162       11872 :   REL_t *old = M->base;
     163       11872 :   M->len = len;
     164       11872 :   M->base = (REL_t*)pari_realloc((void*)old, (len+1) * sizeof(REL_t));
     165       11872 :   if (old)
     166             :   {
     167           0 :     size_t last = M->last - old, chk = M->chk - old, end = M->end - old;
     168           0 :     M->last = M->base + last;
     169           0 :     M->chk  = M->base + chk;
     170           0 :     M->end  = M->base + end;
     171             :   }
     172       11872 : }
     173             : 
     174             : #define pr_get_smallp(pr) gel(pr,1)[2]
     175             : 
     176             : /* don't take P|p all other Q|p are already there */
     177             : static int
     178       52605 : bad_subFB(FB_t *F, long t)
     179             : {
     180       52605 :   GEN LP, P = gel(F->LP,t);
     181       52605 :   long p = pr_get_smallp(P);
     182       52605 :   LP = F->LV[p];
     183       52605 :   return (isclone(LP) && t == F->iLP[p] + lg(LP)-1);
     184             : }
     185             : 
     186             : static void
     187       13188 : assign_subFB(FB_t *F, GEN yes, long iyes)
     188             : {
     189       13188 :   long i, lv = sizeof(subFB_t) + iyes*sizeof(long); /* for struct + GEN */
     190       13188 :   subFB_t *s = (subFB_t *)pari_malloc(lv);
     191       13188 :   s->subFB = (GEN)&s[1];
     192       13188 :   s->old = F->allsubFB; F->allsubFB = s;
     193       13188 :   for (i = 0; i < iyes; i++) s->subFB[i] = yes[i];
     194       13188 :   F->subFB = s->subFB;
     195       13188 :   F->MAXDEPSIZESFB = (iyes-1) * DEPSIZESFBMULT;
     196       13188 :   F->MAXDEPSFB = F->MAXDEPSIZESFB / DEPSFBDIV;
     197       13188 : }
     198             : 
     199             : /* Determine the permutation of the ideals made by each field automorphism */
     200             : static GEN
     201       12495 : FB_aut_perm(FB_t *F, GEN auts, GEN cyclic)
     202             : {
     203       12495 :   long i, j, m, KC = F->KC, nauts = lg(auts)-1;
     204       12495 :   GEN minidx, perm = zero_Flm_copy(KC, nauts);
     205             : 
     206       12495 :   if (!nauts) { F->minidx = gclone(identity_zv(KC)); return cgetg(1,t_MAT); }
     207       11998 :   minidx = zero_Flv(KC);
     208       25914 :   for (m = 1; m < lg(cyclic); m++)
     209             :   {
     210       13916 :     GEN thiscyc = gel(cyclic, m);
     211       13916 :     long k0 = thiscyc[1];
     212       13916 :     GEN aut = gel(auts, k0), permk0 = gel(perm, k0), ppermk;
     213       13916 :     i = 1;
     214       83216 :     while (i <= KC)
     215             :     {
     216       55384 :       pari_sp av2 = avma;
     217       55384 :       GEN seen = zero_Flv(KC), P = gel(F->LP, i);
     218       55384 :       long imin = i, p, f, l;
     219       55384 :       p = pr_get_smallp(P);
     220       55384 :       f = pr_get_f(P);
     221             :       do
     222             :       {
     223      149821 :         if (++i > KC) break;
     224      135905 :         P = gel(F->LP, i);
     225             :       }
     226      135905 :       while (p == pr_get_smallp(P) && f == pr_get_f(P));
     227      205205 :       for (j = imin; j < i; j++)
     228             :       {
     229      149821 :         GEN img = ZM_ZC_mul(aut, pr_get_gen(gel(F->LP, j)));
     230      465528 :         for (l = imin; l < i; l++)
     231      465528 :           if (!seen[l] && ZC_prdvd(img, gel(F->LP, l)))
     232             :           {
     233      149821 :             seen[l] = 1; permk0[j] = l; break;
     234             :           }
     235             :       }
     236       55384 :       set_avma(av2);
     237             :     }
     238       15379 :     for (ppermk = permk0, i = 2; i < lg(thiscyc); i++)
     239             :     {
     240        1463 :       GEN permk = gel(perm, thiscyc[i]);
     241        1463 :       for (j = 1; j <= KC; j++) permk[j] = permk0[ppermk[j]];
     242        1463 :       ppermk = permk;
     243             :     }
     244             :   }
     245      100205 :   for (j = 1; j <= KC; j++)
     246             :   {
     247       88207 :     if (minidx[j]) continue;
     248       43414 :     minidx[j] = j;
     249       43414 :     for (i = 1; i <= nauts; i++) minidx[coeff(perm, j, i)] = j;
     250             :   }
     251       11998 :   F->minidx = gclone(minidx); return perm;
     252             : }
     253             : 
     254             : /* set subFB.
     255             :  * Fill F->perm (if != NULL): primes ideals sorted by increasing norm (except
     256             :  * the ones in subFB come first [dense rows for hnfspec]) */
     257             : static void
     258       12495 : subFBgen(FB_t *F, GEN auts, GEN cyclic, double PROD, long minsFB)
     259             : {
     260             :   GEN y, perm, yes, no;
     261       12495 :   long i, j, k, iyes, ino, lv = F->KC + 1;
     262             :   double prod;
     263             :   pari_sp av;
     264             : 
     265       12495 :   F->LP   = cgetg(lv, t_VEC);
     266       12495 :   F->L_jid = F->perm = cgetg(lv, t_VECSMALL);
     267       12495 :   av = avma;
     268       12495 :   y = cgetg(lv,t_COL); /* Norm P */
     269       62517 :   for (k=0, i=1; i <= F->KCZ; i++)
     270             :   {
     271       50022 :     GEN LP = F->LV[F->FB[i]];
     272       50022 :     long l = lg(LP);
     273      149478 :     for (j = 1; j < l; j++)
     274             :     {
     275       99456 :       GEN P = gel(LP,j);
     276       99456 :       k++;
     277       99456 :       gel(y,k) = pr_norm(P);
     278       99456 :       gel(F->LP,k) = P;
     279             :     }
     280             :   }
     281             :   /* perm sorts LP by increasing norm */
     282       12495 :   perm = indexsort(y);
     283       12495 :   no  = cgetg(lv, t_VECSMALL); ino  = 1;
     284       12495 :   yes = cgetg(lv, t_VECSMALL); iyes = 1;
     285       12495 :   prod = 1.0;
     286       61579 :   for (i = 1; i < lv; i++)
     287             :   {
     288       52605 :     long t = perm[i];
     289       52605 :     if (bad_subFB(F, t)) { no[ino++] = t; continue; }
     290             : 
     291       23520 :     yes[iyes++] = t;
     292       23520 :     prod *= (double)itos(gel(y,t));
     293       23520 :     if (iyes > minsFB && prod > PROD) break;
     294             :   }
     295       12495 :   setlg(yes, iyes);
     296       12495 :   for (j=1; j<iyes; j++)     F->perm[j] = yes[j];
     297       12495 :   for (i=1; i<ino; i++, j++) F->perm[j] =  no[i];
     298       12495 :   for (   ; j<lv; j++)       F->perm[j] =  perm[j];
     299       12495 :   F->allsubFB = NULL;
     300       12495 :   F->idealperm = gclone(FB_aut_perm(F, auts, cyclic));
     301       12495 :   if (iyes) assign_subFB(F, yes, iyes);
     302       12495 :   set_avma(av);
     303       12495 : }
     304             : static int
     305        2434 : subFB_change(FB_t *F)
     306             : {
     307        2434 :   long i, iyes, minsFB, lv = F->KC + 1, l = lg(F->subFB)-1;
     308        2434 :   pari_sp av = avma;
     309        2434 :   GEN yes, L_jid = F->L_jid, present = zero_zv(lv-1);
     310             : 
     311        2434 :   switch (F->sfb_chg)
     312             :   {
     313          50 :     case sfb_INCREASE: minsFB = l + 1; break;
     314        2384 :     default: minsFB = l; break;
     315             :   }
     316             : 
     317        2434 :   yes = cgetg(minsFB+1, t_VECSMALL); iyes = 1;
     318        2434 :   if (L_jid)
     319             :   {
     320        9995 :     for (i = 1; i < lg(L_jid); i++)
     321             :     {
     322        9393 :       long l = L_jid[i];
     323        9393 :       yes[iyes++] = l;
     324        9393 :       present[l] = 1;
     325        9393 :       if (iyes > minsFB) break;
     326             :     }
     327             :   }
     328           0 :   else i = 1;
     329        2434 :   if (iyes <= minsFB)
     330             :   {
     331        1749 :     for ( ; i < lv; i++)
     332             :     {
     333        1749 :       long l = F->perm[i];
     334        1749 :       if (present[l]) continue;
     335        1749 :       yes[iyes++] = l;
     336        1749 :       if (iyes > minsFB) break;
     337             :     }
     338         602 :     if (i == lv) return 0;
     339             :   }
     340        2434 :   if (zv_equal(F->subFB, yes))
     341             :   {
     342        1741 :     if (DEBUGLEVEL) err_printf("\n*** NOT Changing sub factor base\n");
     343             :   }
     344             :   else
     345             :   {
     346         693 :     if (DEBUGLEVEL) err_printf("\n*** Changing sub factor base\n");
     347         693 :     assign_subFB(F, yes, iyes);
     348             :   }
     349        2434 :   F->sfb_chg = 0; return gc_bool(av, 1);
     350             : }
     351             : 
     352             : /* make sure enough room to store n more relations */
     353             : static void
     354       66698 : pre_allocate(RELCACHE_t *cache, size_t n)
     355             : {
     356       66698 :   size_t len = (cache->last - cache->base) + n;
     357       66698 :   if (len >= cache->len) reallocate(cache, len << 1);
     358       66698 : }
     359             : 
     360             : void
     361       28111 : init_GRHcheck(GRHcheck_t *S, long N, long R1, double LOGD)
     362             : {
     363       28111 :   const double c1 = M_PI*M_PI/2;
     364       28111 :   const double c2 = 3.663862376709;
     365       28111 :   const double c3 = 3.801387092431; /* Euler + log(8*Pi)*/
     366       28111 :   S->clone = 0;
     367       28111 :   S->cN = R1*c2 + N*c1;
     368       28111 :   S->cD = LOGD - N*c3 - R1*M_PI/2;
     369       28111 :   S->maxprimes = 16000; /* sufficient for LIMC=176081*/
     370       28111 :   S->primes = (GRHprime_t*)pari_malloc(S->maxprimes*sizeof(*S->primes));
     371       28111 :   S->nprimes = 0;
     372       28111 :   S->limp = 0;
     373       28111 :   u_forprime_init(&S->P, 2, ULONG_MAX);
     374       28111 : }
     375             : 
     376             : void
     377       28111 : free_GRHcheck(GRHcheck_t *S)
     378             : {
     379       28111 :   if (S->clone)
     380             :   {
     381       11816 :     long i = S->nprimes;
     382             :     GRHprime_t *pr;
     383       11816 :     for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--) gunclone(pr->dec);
     384             :   }
     385       28111 :   pari_free(S->primes);
     386       28111 : }
     387             : 
     388             : int
     389      307209 : GRHok(GRHcheck_t *S, double L, double SA, double SB)
     390             : {
     391      307209 :   return (S->cD + (S->cN + 2*SB) / L - 2*SA < -1e-8);
     392             : }
     393             : 
     394             : /* Return factorization pattern of p: [f,n], where n[i] primes of
     395             :  * residue degree f[i] */
     396             : static GEN
     397     1417605 : get_fs(GEN nf, GEN P, GEN index, ulong p)
     398             : {
     399             :   long j, k, f, n, l;
     400             :   GEN fs, ns;
     401             : 
     402     1417605 :   if (umodiu(index, p))
     403             :   { /* easy case: p does not divide index */
     404     1414609 :     GEN F = Flx_degfact(ZX_to_Flx(P,p), p);
     405     1414609 :     fs = gel(F,1); l = lg(fs);
     406             :   }
     407             :   else
     408             :   {
     409        2996 :     GEN F = idealprimedec(nf, utoipos(p));
     410        2996 :     l = lg(F);
     411        2996 :     fs = cgetg(l, t_VECSMALL);
     412        2996 :     for (j = 1; j < l; j++) fs[j] = pr_get_f(gel(F,j));
     413             :   }
     414     1417605 :   ns = cgetg(l, t_VECSMALL);
     415     1417605 :   f = fs[1]; n = 1;
     416     2509409 :   for (j = 2, k = 1; j < l; j++)
     417     1091804 :     if (fs[j] == f)
     418     1021671 :       n++;
     419             :     else
     420             :     {
     421       70133 :       ns[k] = n; fs[k] = f; k++;
     422       70133 :       f = fs[j]; n = 1;
     423             :     }
     424     1417605 :   ns[k] = n; fs[k] = f; k++;
     425     1417605 :   setlg(fs, k);
     426     1417605 :   setlg(ns, k); return mkvec2(fs,ns);
     427             : }
     428             : 
     429             : /* cache data for all rational primes up to the LIM */
     430             : static void
     431      159642 : cache_prime_dec(GRHcheck_t *S, ulong LIM, GEN nf)
     432             : {
     433      159642 :   pari_sp av = avma;
     434             :   GRHprime_t *pr;
     435             :   GEN index, P;
     436             :   double nb;
     437             : 
     438      159642 :   if (S->limp >= LIM) return;
     439       58170 :   S->clone = 1;
     440       58170 :   nb = primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
     441       58170 :   GRH_ensure(S, nb+1); /* room for one extra prime */
     442       58170 :   P = nf_get_pol(nf);
     443       58170 :   index = nf_get_index(nf);
     444       58170 :   for (pr = S->primes + S->nprimes;;)
     445     1359435 :   {
     446     1417605 :     ulong p = u_forprime_next(&(S->P));
     447     1417605 :     pr->p = p;
     448     1417605 :     pr->logp = log((double)p);
     449     1417605 :     pr->dec = gclone(get_fs(nf, P, index, p));
     450     1417605 :     S->nprimes++;
     451     1417605 :     pr++;
     452     1417605 :     set_avma(av);
     453             :     /* store up to nextprime(LIM) included */
     454     1417605 :     if (p >= LIM) { S->limp = p; break; }
     455             :   }
     456             : }
     457             : 
     458             : static double
     459      415366 : tailresback(long R1, long R2, double rK, long C, double C2, double C3, double r1K, double r2K, double logC, double logC2, double logC3)
     460             : {
     461      415366 :   const double  rQ = 1.83787706641;
     462      415366 :   const double r1Q = 1.98505372441;
     463      415366 :   const double r2Q = 1.07991541347;
     464      830732 :   return fabs((R1+R2-1)*(12*logC3+4*logC2-9*logC-6)/(2*C*logC3)
     465      415366 :          + (rK-rQ)*(6*logC2 + 5*logC + 2)/(C*logC3)
     466      415366 :          - R2*(6*logC2+11*logC+6)/(C2*logC2)
     467      415366 :          - 2*(r1K-r1Q)*(3*logC2 + 4*logC + 2)/(C2*logC3)
     468      415366 :          + (R1+R2-1)*(12*logC3+40*logC2+45*logC+18)/(6*C3*logC3)
     469      415366 :          + (r2K-r2Q)*(2*logC2 + 3*logC + 2)/(C3*logC3));
     470             : }
     471             : 
     472             : static double
     473      207683 : tailres(long R1, long R2, double al2K, double rKm, double rKM, double r1Km,
     474             :         double r1KM, double r2Km, double r2KM, double C, long i)
     475             : {
     476             :   /* C >= 3*2^i, lower bound for eint1(log(C)/2) */
     477             :   /* for(i=0,30,print(eint1(log(3*2^i)/2))) */
     478             :   static double tab[] = {
     479             :     0.50409264803,
     480             :     0.26205336997,
     481             :     0.14815491171,
     482             :     0.08770540561,
     483             :     0.05347651832,
     484             :     0.03328934284,
     485             :     0.02104510690,
     486             :     0.01346475900,
     487             :     0.00869778586,
     488             :     0.00566279855,
     489             :     0.00371111950,
     490             :     0.00244567837,
     491             :     0.00161948049,
     492             :     0.00107686891,
     493             :     0.00071868750,
     494             :     0.00048119961,
     495             :     0.00032312188,
     496             :     0.00021753772,
     497             :     0.00014679818,
     498             :     9.9272855581E-5,
     499             :     6.7263969995E-5,
     500             :     4.5656812967E-5,
     501             :     3.1041124593E-5,
     502             :     2.1136011590E-5,
     503             :     1.4411645381E-5,
     504             :     9.8393304088E-6,
     505             :     6.7257395409E-6,
     506             :     4.6025878272E-6,
     507             :     3.1529719271E-6,
     508             :     2.1620490021E-6,
     509             :     1.4839266071E-6
     510             :   };
     511      207683 :   const double logC = log(C), logC2 = logC*logC, logC3 = logC*logC2;
     512      207683 :   const double C2 = C*C, C3 = C*C2;
     513      207683 :   double E1 = i >30? 0: tab[i];
     514      207683 :   return al2K*((33*logC2+22*logC+8)/(8*logC3*sqrt(C))+15*E1/16)
     515      415366 :     + maxdd(tailresback(rKm,r1KM,r2Km, C,C2,C3,R1,R2,logC,logC2,logC3),
     516      207683 :             tailresback(rKM,r1Km,r2KM, C,C2,C3,R1,R2,logC,logC2,logC3))/2
     517      207683 :     + ((R1+R2-1)*4*C+R2)*(C2+6*logC)/(4*C2*C2*logC2);
     518             : }
     519             : 
     520             : static long
     521       11816 : primeneeded(long N, long R1, long R2, double LOGD)
     522             : {
     523       11816 :   const double lim = 0.25; /* should be log(2)/2 == 0.34657... */
     524       11816 :   const double al2K =  0.3526*LOGD - 0.8212*N + 4.5007;
     525       11816 :   const double  rKm = -1.0155*LOGD + 2.1042*N - 8.3419;
     526       11816 :   const double  rKM = -0.5   *LOGD + 1.2076*N + 1;
     527       11816 :   const double r1Km = -       LOGD + 1.4150*N;
     528       11816 :   const double r1KM = -       LOGD + 1.9851*N;
     529       11816 :   const double r2Km = -       LOGD + 0.9151*N;
     530       11816 :   const double r2KM = -       LOGD + 1.0800*N;
     531       11816 :   long Cmin = 3, Cmax = 3, i = 0;
     532      117229 :   while (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, Cmax, i) > lim)
     533             :   {
     534       93597 :     Cmin = Cmax;
     535       93597 :     Cmax *= 2;
     536       93597 :     i++;
     537             :   }
     538       11816 :   i--;
     539      125902 :   while (Cmax - Cmin > 1)
     540             :   {
     541      102270 :     long t = (Cmin + Cmax)/2;
     542      102270 :     if (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, t, i) > lim)
     543       68040 :       Cmin = t;
     544             :     else
     545       34230 :       Cmax = t;
     546             :   }
     547       11816 :   return Cmax;
     548             : }
     549             : 
     550             : /* ~ 1 / Res(s = 1, zeta_K) */
     551             : static GEN
     552       11816 : compute_invres(GRHcheck_t *S, long LIMC)
     553             : {
     554       11816 :   pari_sp av = avma;
     555       11816 :   double loginvres = 0.;
     556             :   GRHprime_t *pr;
     557             :   long i;
     558       11816 :   double logLIMC = log((double)LIMC);
     559       11816 :   double logLIMC2 = logLIMC*logLIMC, denc;
     560             :   double c0, c1, c2;
     561       11816 :   denc = 1/(pow((double)LIMC, 3.) * logLIMC * logLIMC2);
     562       11816 :   c2 = (    logLIMC2 + 3 * logLIMC / 2 + 1) * denc;
     563       11816 :   denc *= LIMC;
     564       11816 :   c1 = (3 * logLIMC2 + 4 * logLIMC     + 2) * denc;
     565       11816 :   denc *= LIMC;
     566       11816 :   c0 = (3 * logLIMC2 + 5 * logLIMC / 2 + 1) * denc;
     567     1418739 :   for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--)
     568             :   {
     569             :     GEN dec, fs, ns;
     570             :     long addpsi;
     571             :     double addpsi1, addpsi2;
     572     1417605 :     double logp = pr->logp, NPk;
     573     1417605 :     long j, k, limp = logLIMC/logp;
     574     1417605 :     ulong p = pr->p, p2 = p*p;
     575     1417605 :     if (limp < 1) break;
     576     1406923 :     dec = pr->dec;
     577     1406923 :     fs = gel(dec, 1); ns = gel(dec, 2);
     578     1406923 :     loginvres += 1./p;
     579             :     /* NB: limp = 1 nearly always and limp > 2 for very few primes */
     580     1406923 :     for (k=2, NPk = p; k <= limp; k++) { NPk *= p; loginvres += 1/(k * NPk); }
     581     1406923 :     addpsi = limp;
     582     1406923 :     addpsi1 = p *(pow((double)p , (double)limp)-1)/(p -1);
     583     1406923 :     addpsi2 = p2*(pow((double)p2, (double)limp)-1)/(p2-1);
     584     1406923 :     j = lg(fs);
     585     4290517 :     while (--j > 0)
     586             :     {
     587             :       long f, nb, kmax;
     588             :       double NP, NP2, addinvres;
     589     1476671 :       f = fs[j]; if (f > limp) continue;
     590      693602 :       nb = ns[j];
     591      693602 :       NP = pow((double)p, (double)f);
     592      693602 :       addinvres = 1/NP;
     593      693602 :       kmax = limp / f;
     594      693602 :       for (k=2, NPk = NP; k <= kmax; k++) { NPk *= NP; addinvres += 1/(k*NPk); }
     595      693602 :       NP2 = NP*NP;
     596      693602 :       loginvres -= nb * addinvres;
     597      693602 :       addpsi -= nb * f * kmax;
     598      693602 :       addpsi1 -= nb*(f*NP *(pow(NP ,(double)kmax)-1)/(NP -1));
     599      693602 :       addpsi2 -= nb*(f*NP2*(pow(NP2,(double)kmax)-1)/(NP2-1));
     600             :     }
     601     1406923 :     loginvres -= (addpsi*c0 - addpsi1*c1 + addpsi2*c2)*logp;
     602             :   }
     603       11816 :   return gerepileuptoleaf(av, mpexp(dbltor(loginvres)));
     604             : }
     605             : 
     606             : static long
     607       11816 : nthideal(GRHcheck_t *S, GEN nf, long n)
     608             : {
     609       11816 :   pari_sp av = avma;
     610       11816 :   GEN P = nf_get_pol(nf);
     611       11816 :   ulong p = 0, *vecN = (ulong*)const_vecsmall(n, LONG_MAX);
     612       11816 :   long i, N = poldegree(P, -1);
     613       45836 :   for (i = 0; ; i++)
     614       34020 :   {
     615             :     GRHprime_t *pr;
     616             :     GEN fs;
     617       45836 :     cache_prime_dec(S, p+1, nf);
     618       45836 :     pr = S->primes + i;
     619       45836 :     fs = gel(pr->dec, 1);
     620       45836 :     p = pr->p;
     621       45836 :     if (fs[1] != N)
     622             :     {
     623       31010 :       GEN ns = gel(pr->dec, 2);
     624       31010 :       long k, l, j = lg(fs);
     625       95627 :       while (--j > 0)
     626             :       {
     627       33607 :         ulong NP = upowuu(p, fs[j]);
     628             :         long nf;
     629       33607 :         if (!NP) continue;
     630       33297 :         for (k = 1; k <= n; k++) if (vecN[k] > NP) break;
     631       33297 :         if (k > n) continue;
     632             :         /* vecN[k] <= NP */
     633       21300 :         nf = ns[j]; /*#{primes of norme NP} = nf, insert them here*/
     634       21300 :         for (l = k+nf; l <= n; l++) vecN[l] = vecN[l-nf];
     635       21300 :         for (l = 0; l < nf && k+l <= n; l++) vecN[k+l] = NP;
     636       21300 :         while (l <= k) vecN[l++] = NP;
     637             :       }
     638             :     }
     639       45836 :     if (p > vecN[n]) break;
     640             :   }
     641       11816 :   return gc_long(av, vecN[n]);
     642             : }
     643             : 
     644             : 
     645             : /* Compute FB, LV, iLP + KC*. Reset perm
     646             :  * C2: bound for norm of tested prime ideals (includes be_honest())
     647             :  * C1: bound for p, such that P|p (NP <= C2) used to build relations */
     648             : static void
     649       12495 : FBgen(FB_t *F, GEN nf, long N, ulong C1, ulong C2, GRHcheck_t *S)
     650             : {
     651             :   GRHprime_t *pr;
     652             :   long i, ip;
     653             :   GEN prim;
     654       12495 :   const double L = log((double)C2 + 0.5);
     655             : 
     656       12495 :   cache_prime_dec(S, C2, nf);
     657       12495 :   pr = S->primes;
     658       12495 :   F->sfb_chg = 0;
     659       12495 :   F->FB  = cgetg(C2+1, t_VECSMALL);
     660       12495 :   F->iLP = cgetg(C2+1, t_VECSMALL);
     661       12495 :   F->LV = (GEN*)const_vec(C2, NULL);
     662             : 
     663       12495 :   prim = icopy(gen_1);
     664       12495 :   i = ip = 0;
     665       12495 :   F->KC = F->KCZ = 0;
     666       99561 :   for (;; pr++) /* p <= C2 */
     667       99561 :   {
     668      112056 :     ulong p = pr->p;
     669             :     long k, l, m;
     670             :     GEN LP, nb, f;
     671             : 
     672      112056 :     if (!F->KC && p > C1) { F->KCZ = i; F->KC = ip; }
     673      112056 :     if (p > C2) break;
     674             : 
     675      105119 :     if (DEBUGLEVEL>1) err_printf(" %ld",p);
     676             : 
     677      105119 :     f = gel(pr->dec, 1); nb = gel(pr->dec, 2);
     678      105119 :     if (f[1] == N)
     679             :     {
     680       34608 :       if (p == C2) break;
     681       32606 :       continue; /* p inert */
     682             :     }
     683       70511 :     l = (long)(L/pr->logp); /* p^f <= C2  <=> f <= l */
     684       70511 :     for (k=0, m=1; m < lg(f) && f[m]<=l; m++) k += nb[m];
     685       70511 :     if (!k)
     686             :     { /* too inert to appear in FB */
     687       17836 :       if (p == C2) break;
     688       17731 :       continue;
     689             :     }
     690       52675 :     prim[2] = p; LP = idealprimedec_limit_f(nf,prim, l);
     691             :     /* keep non-inert ideals with Norm <= C2 */
     692       52675 :     if (m == lg(f)) setisclone(LP); /* flag it: all prime divisors in FB */
     693       52675 :     F->FB[++i]= p;
     694       52675 :     F->LV[p]  = LP;
     695       52675 :     F->iLP[p] = ip; ip += k;
     696       52675 :     if (p == C2) break;
     697             :   }
     698       12495 :   if (!F->KC) { F->KCZ = i; F->KC = ip; }
     699             :   /* Note F->KC > 0 otherwise GRHchk is false */
     700       12495 :   setlg(F->FB, F->KCZ+1); F->KCZ2 = i;
     701       12495 :   if (DEBUGLEVEL>1)
     702             :   {
     703           0 :     err_printf("\n");
     704           0 :     if (DEBUGLEVEL>6)
     705             :     {
     706           0 :       err_printf("########## FACTORBASE ##########\n\n");
     707           0 :       err_printf("KC2=%ld, KC=%ld, KCZ=%ld, KCZ2=%ld\n",
     708             :                   ip, F->KC, F->KCZ, F->KCZ2);
     709           0 :       for (i=1; i<=F->KCZ; i++) err_printf("++ LV[%ld] = %Ps",i,F->LV[F->FB[i]]);
     710             :     }
     711             :   }
     712       12495 :   F->perm = NULL; F->L_jid = NULL;
     713       12495 : }
     714             : 
     715             : static int
     716       89495 : GRHchk(GEN nf, GRHcheck_t *S, ulong LIMC)
     717             : {
     718       89495 :   double logC = log((double)LIMC), SA = 0, SB = 0;
     719       89495 :   GRHprime_t *pr = S->primes;
     720             : 
     721       89495 :   cache_prime_dec(S, LIMC, nf);
     722      736435 :   for (pr = S->primes;; pr++)
     723      646940 :   {
     724      736435 :     ulong p = pr->p;
     725             :     GEN dec, fs, ns;
     726             :     double logCslogp;
     727             :     long j;
     728             : 
     729      736435 :     if (p > LIMC) break;
     730      667100 :     dec = pr->dec; fs = gel(dec, 1); ns = gel(dec,2);
     731      667100 :     logCslogp = logC/pr->logp;
     732     1014769 :     for (j = 1; j < lg(fs); j++)
     733             :     {
     734      737646 :       long f = fs[j], M, nb;
     735             :       double logNP, q, A, B;
     736      737646 :       if (f > logCslogp) break;
     737      347669 :       logNP = f * pr->logp;
     738      347669 :       q = 1/sqrt((double)upowuu(p, f));
     739      347669 :       A = logNP * q; B = logNP * A; M = (long)(logCslogp/f);
     740      347669 :       if (M > 1)
     741             :       {
     742       71904 :         double inv1_q = 1 / (1-q);
     743       71904 :         A *= (1 - pow(q, (double)M)) * inv1_q;
     744       71904 :         B *= (1 - pow(q, (double)M)*(M+1 - M*q)) * inv1_q * inv1_q;
     745             :       }
     746      347669 :       nb = ns[j];
     747      347669 :       SA += nb * A;
     748      347669 :       SB += nb * B;
     749             :     }
     750      667100 :     if (p == LIMC) break;
     751             :   }
     752       89495 :   return GRHok(S, logC, SA, SB);
     753             : }
     754             : 
     755             : /*  SMOOTH IDEALS */
     756             : static void
     757     3516651 : store(long i, long e, FACT *fact)
     758             : {
     759     3516651 :   ++fact[0].pr;
     760     3516651 :   fact[fact[0].pr].pr = i; /* index */
     761     3516651 :   fact[fact[0].pr].ex = e; /* exponent */
     762     3516651 : }
     763             : 
     764             : /* divide out x by all P|p, where x as in can_factor().  k = v_p(Nx) */
     765             : static int
     766     1696381 : divide_p_elt(GEN LP, long ip, long k, GEN m, FACT *fact)
     767             : {
     768     1696381 :   long j, l = lg(LP);
     769     8077205 :   for (j=1; j<l; j++)
     770             :   {
     771     8075553 :     GEN P = gel(LP,j);
     772     8075553 :     long v = ZC_nfval(m, P);
     773     8075553 :     if (!v) continue;
     774     3054330 :     store(ip + j, v, fact); /* v = v_P(m) > 0 */
     775     3054330 :     k -= v * pr_get_f(P);
     776     3054330 :     if (!k) return 1;
     777             :   }
     778        1652 :   return 0;
     779             : }
     780             : static int
     781      113422 : divide_p_id(GEN LP, long ip, long k, GEN nf, GEN I, FACT *fact)
     782             : {
     783      113422 :   long j, l = lg(LP);
     784      169353 :   for (j=1; j<l; j++)
     785             :   {
     786      162792 :     GEN P = gel(LP,j);
     787      162792 :     long v = idealval(nf,I, P);
     788      162792 :     if (!v) continue;
     789      107930 :     store(ip + j, v, fact); /* v = v_P(I) > 0 */
     790      107930 :     k -= v * pr_get_f(P);
     791      107930 :     if (!k) return 1;
     792             :   }
     793        6561 :   return 0;
     794             : }
     795             : static int
     796      315812 : divide_p_quo(GEN LP, long ip, long k, GEN nf, GEN I, GEN m, FACT *fact)
     797             : {
     798      315812 :   long j, l = lg(LP);
     799      479785 :   for (j=1; j<l; j++)
     800             :   {
     801      479639 :     GEN P = gel(LP,j);
     802      479639 :     long v = ZC_nfval(m, P);
     803      479639 :     if (!v) continue;
     804      341691 :     v -= idealval(nf,I, P);
     805      341691 :     if (!v) continue;
     806      338077 :     store(ip + j, v, fact); /* v = v_P(m / I) > 0 */
     807      338077 :     k -= v * pr_get_f(P);
     808      338077 :     if (!k) return 1;
     809             :   }
     810         146 :   return 0;
     811             : }
     812             : 
     813             : /* |*N| != 0 is the norm of a primitive ideal, in particular not divisible by
     814             :  * any inert prime. Is |*N| a smooth rational integer wrt F ? (put the
     815             :  * exponents in *ex) */
     816             : static int
     817     3205310 : smooth_norm(FB_t *F, GEN *N, GEN *ex)
     818             : {
     819     3205310 :   GEN FB = F->FB;
     820     3205310 :   const long KCZ = F->KCZ;
     821     3205310 :   const ulong limp = uel(FB,KCZ); /* last p in FB */
     822             :   long i;
     823             : 
     824     3205310 :   *ex = new_chunk(KCZ+1);
     825   197150022 :   for (i=1; ; i++)
     826   193944712 :   {
     827             :     int stop;
     828   197150022 :     ulong p = uel(FB,i);
     829   197150022 :     long v = Z_lvalrem_stop(N, p, &stop);
     830   197150022 :     (*ex)[i] = v;
     831   197150022 :     if (v)
     832             :     {
     833     5508144 :       GEN LP = F->LV[p];
     834     5508144 :       if(!LP) pari_err_BUG("can_factor");
     835     7389754 :       if (lg(LP) == 1) return 0;
     836     6831844 :       if (stop) break;
     837             :     }
     838   195826322 :     if (i == KCZ) return 0;
     839             :   }
     840     1323700 :   (*ex)[0] = i;
     841     1323700 :   return (abscmpiu(*N,limp) <= 0);
     842             : }
     843             : 
     844             : static int
     845     2125615 : divide_p(FB_t *F, long p, long k, GEN nf, GEN I, GEN m, FACT *fact)
     846             : {
     847     2125615 :   GEN LP = F->LV[p];
     848     2125615 :   long ip = F->iLP[p];
     849     2125615 :   if (!m) return divide_p_id (LP,ip,k,nf,I,fact);
     850     2012193 :   if (!I) return divide_p_elt(LP,ip,k,m,fact);
     851      315812 :   return divide_p_quo(LP,ip,k,nf,I,m,fact);
     852             : }
     853             : 
     854             : /* Let x = m if I == NULL,
     855             :  *         I if m == NULL,
     856             :  *         m/I otherwise.
     857             :  * Can we factor the integral primitive ideal x ? |N| = Norm x > 0 */
     858             : static long
     859     3317895 : can_factor(FB_t *F, GEN nf, GEN I, GEN m, GEN N, FACT *fact)
     860             : {
     861             :   GEN ex;
     862     3317895 :   long i, res = 0;
     863     3317895 :   fact[0].pr = 0;
     864     3317895 :   if (is_pm1(N)) return 1;
     865     3205310 :   if (!smooth_norm(F, &N, &ex)) goto END;
     866    11430369 :   for (i=1; i<=ex[0]; i++)
     867    10308781 :     if (ex[i] && !divide_p(F, F->FB[i], ex[i], nf, I, m, fact)) goto END;
     868     1121588 :   res = is_pm1(N) || divide_p(F, itou(N), 1, nf, I, m, fact);
     869             : END:
     870     3205310 :   if (!res && DEBUGLEVEL > 1) err_printf(".");
     871     3205310 :   return res;
     872             : }
     873             : 
     874             : /* can we factor m/I ? [m in I from idealpseudomin_nonscalar], NI = norm I */
     875             : static long
     876     1705367 : factorgen(FB_t *F, GEN nf, GEN I, GEN NI, GEN m, FACT *fact)
     877             : {
     878     1705367 :   long e, r1 = nf_get_r1(nf);
     879     1705367 :   GEN M = nf_get_M(nf);
     880     1705367 :   GEN N = divri(embed_norm(RgM_RgC_mul(M,m), r1), NI); /* ~ N(m/I) */
     881     1705367 :   N = grndtoi(N, &e);
     882     1705367 :   if (e > -1)
     883             :   {
     884           0 :     if (DEBUGLEVEL > 1) err_printf("+");
     885           0 :     return 0;
     886             :   }
     887     1705367 :   return can_factor(F, nf, I, m, N, fact);
     888             : }
     889             : 
     890             : /*  FUNDAMENTAL UNITS */
     891             : 
     892             : /* a, m real. Return  (Re(x) + a) + I * (Im(x) % m) */
     893             : static GEN
     894     1379139 : addRe_modIm(GEN x, GEN a, GEN m)
     895             : {
     896             :   GEN re, im, z;
     897     1379139 :   if (typ(x) == t_COMPLEX)
     898             :   {
     899     1044305 :     im = modRr_safe(gel(x,2), m);
     900     1044305 :     if (!im) return NULL;
     901     1044305 :     re = gadd(gel(x,1), a);
     902     1044305 :     z = gequal0(im)? re: mkcomplex(re, im);
     903             :   }
     904             :   else
     905      334834 :     z = gadd(x, a);
     906     1379139 :   return z;
     907             : }
     908             : 
     909             : /* clean archimedean components */
     910             : static GEN
     911      579750 : cleanarch(GEN x, long N, long prec)
     912             : {
     913             :   long i, l, R1, RU;
     914      579750 :   GEN s, pi2, y = cgetg_copy(x, &l);
     915             : 
     916      579750 :   if (typ(x) == t_MAT)
     917             :   {
     918      123438 :     for (i = 1; i < l; i++)
     919       99652 :       if (!(gel(y,i) = cleanarch(gel(x,i), N, prec))) return NULL;
     920       23786 :     return y;
     921             :   }
     922      555964 :   RU = l-1; R1 = (RU<<1) - N; pi2 = Pi2n(1, prec);
     923      555964 :   s = gdivgs(RgV_sum(real_i(x)), -N); /* -log |norm(x)| / N */
     924     1584858 :   for (i = 1; i <= R1; i++)
     925     1028894 :     if (!(gel(y,i) = addRe_modIm(gel(x,i), s, pi2))) return NULL;
     926      555964 :   if (i <= RU)
     927             :   {
     928      206976 :     GEN pi4 = Pi2n(2, prec), s2 = gmul2n(s, 1);
     929      557221 :     for (   ; i <= RU; i++)
     930      350245 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s2, pi4))) return NULL;
     931             :   }
     932      555964 :   return y;
     933             : }
     934             : GEN
     935           0 : nf_cxlog_normalize(GEN nf, GEN x, long prec)
     936             : {
     937           0 :   long N = nf_get_degree(checknf(nf));
     938           0 :   return cleanarch(x, N, prec);
     939             : }
     940             : 
     941             : static GEN
     942         127 : not_given(long reason)
     943             : {
     944         127 :   if (DEBUGLEVEL)
     945           0 :     switch(reason)
     946             :     {
     947             :       case fupb_LARGE:
     948           0 :         pari_warn(warner,"fundamental units too large, not given");
     949           0 :         break;
     950             :       case fupb_PRECI:
     951           0 :         pari_warn(warner,"insufficient precision for fundamental units, not given");
     952           0 :         break;
     953             :     }
     954         127 :   return NULL;
     955             : }
     956             : 
     957             : /* check whether exp(x) will 1) get too big (real(x) large), 2) require
     958             :  * large accuracy for argument reduction (imag(x) large) */
     959             : static long
     960      604439 : expbitprec(GEN x, long *e)
     961             : {
     962             :   GEN re, im;
     963      604439 :   if (typ(x) != t_COMPLEX) re = x;
     964             :   else
     965             :   {
     966      410041 :     im = gel(x,2); *e = maxss(*e, expo(im) + 5 - bit_prec(im));
     967      410041 :     re = gel(x,1);
     968             :   }
     969      604439 :   return (expo(re) <= 20);
     970             : 
     971             : }
     972             : static long
     973      246195 : RgC_expbitprec(GEN x)
     974             : {
     975      246195 :   long l = lg(x), i, e = - (long)HIGHEXPOBIT;
     976      825574 :   for (i = 1; i < l; i++)
     977      579379 :     if (!expbitprec(gel(x,i), &e)) return LONG_MAX;
     978      246195 :   return e;
     979             : }
     980             : static long
     981        4109 : RgM_expbitprec(GEN x)
     982             : {
     983        4109 :   long i, j, I, J, e = - (long)HIGHEXPOBIT;
     984        4109 :   RgM_dimensions(x, &I,&J);
     985       10360 :   for (j = 1; j <= J; j++)
     986       31311 :     for (i = 1; i <= I; i++)
     987       25060 :       if (!expbitprec(gcoeff(x,i,j), &e)) return LONG_MAX;
     988        4095 :   return e;
     989             : }
     990             : 
     991             : static GEN
     992        1978 : FlxqX_chinese_unit(GEN X, GEN U, GEN invzk, GEN D, GEN T, ulong p)
     993             : {
     994        1978 :   long i, lU = lg(U), lX = lg(X), d = lg(invzk)-1;
     995        1978 :   GEN M = cgetg(lU, t_MAT);
     996        1978 :   if (D)
     997             :   {
     998        1800 :     D = Flv_inv(D, p);
     999       91676 :     for (i = 1; i < lX; i++)
    1000       89876 :       if (uel(D, i) != 1)
    1001       74959 :         gel(X,i) = Flx_Fl_mul(gel(X,i), uel(D,i), p);
    1002             :   }
    1003        5113 :   for (i = 1; i < lU; i++)
    1004             :   {
    1005        3135 :     GEN H = FlxqV_factorback(X, gel(U, i), T, p);
    1006        3135 :     gel(M, i) = Flm_Flc_mul(invzk, Flx_to_Flv(H, d), p);
    1007             :   }
    1008        1978 :   return M;
    1009             : }
    1010             : 
    1011             : /* Let x = \prod X[i]^E[i] = u, return u.
    1012             :  * If dX != NULL, X[i] = nX[i] / dX[i] where nX[i] is a ZX, dX[i] in Z */
    1013             : static GEN
    1014         106 : chinese_unit(GEN nf, GEN nX, GEN dX, GEN U)
    1015             : {
    1016         106 :   pari_sp av = avma;
    1017         106 :   GEN f = nf_get_index(nf), T = nf_get_pol(nf), invzk = nf_get_invzk(nf);
    1018         106 :   GEN q, M = NULL, Mp;
    1019         106 :   int stable = 0;
    1020             :   forprime_t S;
    1021             :   ulong p;
    1022         106 :   init_modular_big(&S);
    1023        2084 :   while ((p = u_forprime_next(&S)))
    1024             :   {
    1025             :     GEN Tp, Xp, Dp, invzkp;
    1026        1978 :     if (!umodiu(f,p)) continue;
    1027        1978 :     Tp = ZX_to_Flx(T, p);
    1028        1978 :     Xp = ZXV_to_FlxV(nX, p);
    1029        1978 :     invzkp = ZM_to_Flm(invzk, p);
    1030        1978 :     Dp = dX ? ZV_to_Flv(dX, p): NULL;
    1031        1978 :     Mp = FlxqX_chinese_unit(Xp, U, invzkp, Dp, Tp, p);
    1032        1978 :     if (!M)
    1033             :     { /* initialize */
    1034         106 :       q = utoipos(p);
    1035         106 :       M = ZM_init_CRT(Mp, p);
    1036             :     }
    1037             :     else
    1038        1872 :       stable = ZM_incremental_CRT(&M, Mp, &q, p);
    1039        1978 :     if (stable) break;
    1040        1872 :     if (gc_needed(av, 1))
    1041           0 :       gerepileall(av, 2, &M, &q);
    1042             :   }
    1043         106 :   settyp(M, t_VEC); return M;
    1044             : }
    1045             : 
    1046             : /* *pE a ZM */
    1047             : static void
    1048         120 : ZM_remove_unused(GEN *pE, GEN *pX)
    1049             : {
    1050         120 :   long j, k, l = lg(*pX);
    1051         120 :   GEN E = *pE, v = cgetg(l, t_VECSMALL);
    1052        9157 :   for (j = k = 1; j < l; j++)
    1053        9037 :     if (!ZMrow_equal0(E, j)) v[k++] = j;
    1054         120 :   if (k < l)
    1055             :   {
    1056         120 :     setlg(v, k);
    1057         120 :     *pX = vecpermute(*pX,v);
    1058         120 :     *pE = rowpermute(E,v);
    1059             :   }
    1060         120 : }
    1061             : 
    1062             : /* s = -log|norm(x)|/N */
    1063             : static GEN
    1064      252460 : fixarch(GEN x, GEN s, long R1)
    1065             : {
    1066             :   long i, l;
    1067      252460 :   GEN y = cgetg_copy(x, &l);
    1068      252460 :   for (i = 1; i <= R1; i++) gel(y,i) = gadd(s, gel(x,i));
    1069      252460 :   for (     ; i <   l; i++) gel(y,i) = gadd(s, gmul2n(gel(x,i),-1));
    1070      252460 :   return y;
    1071             : }
    1072             : 
    1073             : static GEN
    1074       11816 : getfu(GEN nf, GEN *ptA, GEN *ptU, long prec)
    1075             : {
    1076       11816 :   GEN U, y, matep, A, T = nf_get_pol(nf), M = nf_get_M(nf);
    1077       11816 :   long e, j, R1, RU, N = degpol(T);
    1078             : 
    1079       11816 :   R1 = nf_get_r1(nf); RU = (N+R1) >> 1;
    1080       11816 :   if (RU == 1) return cgetg(1,t_VEC);
    1081             : 
    1082        4109 :   A = *ptA;
    1083        4109 :   matep = cgetg(RU,t_MAT);
    1084       10374 :   for (j = 1; j < RU; j++)
    1085             :   {
    1086        6265 :     GEN Aj = gel(A,j), s = gdivgs(RgV_sum(real_i(Aj)), -N);
    1087        6265 :     gel(matep,j) = fixarch(Aj, s, R1);
    1088             :   }
    1089        4109 :   U = lll(real_i(matep));
    1090        4109 :   if (lg(U) < RU) return not_given(fupb_PRECI);
    1091        4109 :   if (ptU) { *ptU = U; *ptA = A = RgM_mul(A,U); }
    1092        4109 :   y = RgM_mul(matep,U);
    1093        4109 :   e = RgM_expbitprec(y);
    1094        4109 :   if (e >= 0) return not_given(e == LONG_MAX? fupb_LARGE: fupb_PRECI);
    1095        4095 :   if (prec <= 0) prec = gprecision(A);
    1096        4095 :   y = RgM_solve_realimag(M, gexp(y,prec));
    1097        4095 :   if (!y) return not_given(fupb_PRECI);
    1098        4095 :   y = grndtoi(y, &e); if (e >= 0) return not_given(fupb_PRECI);
    1099        3996 :   settyp(y, t_VEC);
    1100             : 
    1101        3996 :   if (!ptU) *ptA = A = RgM_mul(A, U);
    1102        9993 :   for (j = 1; j < RU; j++)
    1103             :   { /* y[i] are hopefully unit generators. Normalize: smallest T2 norm */
    1104        6011 :     GEN u = gel(y,j), v = zk_inv(nf, u);
    1105        6011 :     if (!v || !is_pm1(Q_denom(v)) || ZV_isscalar(u))
    1106          14 :       return not_given(fupb_PRECI);
    1107        5997 :     if (gcmp(RgC_fpnorml2(v,DEFAULTPREC), RgC_fpnorml2(u,DEFAULTPREC)) < 0)
    1108             :     {
    1109        2127 :       gel(A,j) = RgC_neg(gel(A,j));
    1110        2127 :       if (ptU) gel(U,j) = ZC_neg(gel(U,j));
    1111        2127 :       u = v;
    1112             :     }
    1113        5997 :     gel(y,j) = nf_to_scalar_or_alg(nf, u);
    1114             :   }
    1115        3982 :   return y;
    1116             : }
    1117             : 
    1118             : static void
    1119           0 : err_units() { pari_err_PREC("makeunits [cannot get units, use bnfinit(,1)]"); }
    1120             : static GEN
    1121         106 : vec_chinese_unit(GEN bnf)
    1122             : {
    1123         106 :   GEN nf = bnf_get_nf(bnf), SUnits = bnf_get_sunits(bnf);
    1124         106 :   GEN X, dX, Y, U, f = nf_get_index(nf);
    1125             :   long j, l, v;
    1126         106 :   if (!SUnits) err_units(); /* no compact units */
    1127         106 :   Y = gel(SUnits,1); v = nf_get_varn(nf);
    1128         106 :   U = gel(SUnits,2);
    1129         106 :   ZM_remove_unused(&U, &Y); l = lg(Y); X = cgetg(l, t_VEC);
    1130         106 :   if (is_pm1(f)) f = dX = NULL; else dX = cgetg(l, t_VEC);
    1131        6313 :   for (j = 1; j < l; j++)
    1132             :   {
    1133        6207 :     GEN t = nf_to_scalar_or_alg(nf, gel(Y,j));
    1134        6207 :     if (f)
    1135             :     {
    1136             :       GEN den;
    1137        5157 :       t = Q_remove_denom(t, &den);
    1138        5157 :       gel(dX,j) = den ? den: gen_1;
    1139             :     }
    1140        6207 :     gel(X,j) = typ(t) == t_INT? scalarpol_shallow(t,v): t;
    1141             :   }
    1142         106 :   return chinese_unit(nf, X, dX, U);
    1143             : }
    1144             : 
    1145             : static GEN
    1146         827 : makeunits(GEN bnf)
    1147             : {
    1148         827 :   GEN nf = bnf_get_nf(bnf), fu = bnf_get_fu_nocheck(bnf);
    1149         827 :   GEN tu = nf_to_scalar_or_basis(nf, bnf_get_tuU(bnf));
    1150         827 :   fu = (typ(fu) == t_MAT)? vec_chinese_unit(bnf): matalgtobasis(nf, fu);
    1151         827 :   return vec_prepend(fu, tu);
    1152             : }
    1153             : 
    1154             : /*******************************************************************/
    1155             : /*                                                                 */
    1156             : /*           PRINCIPAL IDEAL ALGORITHM (DISCRETE LOG)              */
    1157             : /*                                                                 */
    1158             : /*******************************************************************/
    1159             : 
    1160             : /* G: prime ideals, E: vector of non-negative exponents.
    1161             :  * C = possible extra prime (^1) or NULL
    1162             :  * Return Norm (product) */
    1163             : static GEN
    1164          21 : get_norm_fact_primes(GEN G, GEN E, GEN C)
    1165             : {
    1166          21 :   pari_sp av=avma;
    1167          21 :   GEN N = gen_1, P, p;
    1168          21 :   long i, c = lg(E);
    1169          21 :   for (i=1; i<c; i++)
    1170             :   {
    1171           0 :     GEN ex = gel(E,i);
    1172           0 :     long s = signe(ex);
    1173           0 :     if (!s) continue;
    1174             : 
    1175           0 :     P = gel(G,i); p = pr_get_p(P);
    1176           0 :     N = mulii(N, powii(p, mului(pr_get_f(P), ex)));
    1177             :   }
    1178          21 :   if (C) N = mulii(N, pr_norm(C));
    1179          21 :   return gerepileuptoint(av, N);
    1180             : }
    1181             : 
    1182             : /* gen: HNF ideals */
    1183             : static GEN
    1184      243773 : get_norm_fact(GEN gen, GEN ex, GEN *pd)
    1185             : {
    1186      243773 :   long i, c = lg(ex);
    1187             :   GEN d,N,I,e,n,ne,de;
    1188      243773 :   d = N = gen_1;
    1189      401361 :   for (i=1; i<c; i++)
    1190      157588 :     if (signe(gel(ex,i)))
    1191             :     {
    1192      102081 :       I = gel(gen,i); e = gel(ex,i); n = ZM_det_triangular(I);
    1193      102081 :       ne = powii(n,e);
    1194      102081 :       de = equalii(n, gcoeff(I,1,1))? ne: powii(gcoeff(I,1,1), e);
    1195      102081 :       N = mulii(N, ne);
    1196      102081 :       d = mulii(d, de);
    1197             :     }
    1198      243773 :   *pd = d; return N;
    1199             : }
    1200             : 
    1201             : static GEN
    1202      397711 : get_pr_lists(GEN FB, long N, int list_pr)
    1203             : {
    1204             :   GEN pr, L;
    1205      397711 :   long i, l = lg(FB), p, pmax;
    1206             : 
    1207      397711 :   pmax = 0;
    1208     3501196 :   for (i=1; i<l; i++)
    1209             :   {
    1210     3103485 :     pr = gel(FB,i); p = pr_get_smallp(pr);
    1211     3103485 :     if (p > pmax) pmax = p;
    1212             :   }
    1213      397711 :   L = const_vec(pmax, NULL);
    1214      397711 :   if (list_pr)
    1215             :   {
    1216           0 :     for (i=1; i<l; i++)
    1217             :     {
    1218           0 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1219           0 :       if (!L[p]) gel(L,p) = vectrunc_init(N+1);
    1220           0 :       vectrunc_append(gel(L,p), pr);
    1221             :     }
    1222           0 :     for (p=1; p<=pmax; p++)
    1223           0 :       if (L[p]) gen_sort_inplace(gel(L,p), (void*)&cmp_prime_over_p,
    1224             :                                  &cmp_nodata, NULL);
    1225             :   }
    1226             :   else
    1227             :   {
    1228     3501196 :     for (i=1; i<l; i++)
    1229             :     {
    1230     3103485 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1231     3103485 :       if (!L[p]) gel(L,p) = vecsmalltrunc_init(N+1);
    1232     3103485 :       vecsmalltrunc_append(gel(L,p), i);
    1233             :     }
    1234             :   }
    1235      397711 :   return L;
    1236             : }
    1237             : 
    1238             : /* recover FB, LV, iLP, KCZ from Vbase */
    1239             : static GEN
    1240      397711 : recover_partFB(FB_t *F, GEN Vbase, long N)
    1241             : {
    1242      397711 :   GEN FB, LV, iLP, L = get_pr_lists(Vbase, N, 0);
    1243      397711 :   long l = lg(L), p, ip, i;
    1244             : 
    1245      397711 :   i = ip = 0;
    1246      397711 :   FB = cgetg(l, t_VECSMALL);
    1247      397711 :   iLP= cgetg(l, t_VECSMALL);
    1248      397711 :   LV = cgetg(l, t_VEC);
    1249     6732661 :   for (p = 2; p < l; p++)
    1250             :   {
    1251     6334950 :     if (!L[p]) continue;
    1252     1707146 :     FB[++i] = p;
    1253     1707146 :     gel(LV,p) = vecpermute(Vbase, gel(L,p));
    1254     1707146 :     iLP[p]= ip; ip += lg(gel(L,p))-1;
    1255             :   }
    1256      397711 :   F->KCZ = i;
    1257      397711 :   F->KC = ip;
    1258      397711 :   F->FB = FB; setlg(FB, i+1);
    1259      397711 :   F->LV = (GEN*)LV;
    1260      397711 :   F->iLP= iLP; return L;
    1261             : }
    1262             : 
    1263             : /* add v^e to factorization */
    1264             : static void
    1265       17117 : add_to_fact(long v, long e, FACT *fact)
    1266             : {
    1267       17117 :   long i, l = fact[0].pr;
    1268       17117 :   for (i=1; i<=l && fact[i].pr < v; i++)/*empty*/;
    1269       17117 :   if (i <= l && fact[i].pr == v) fact[i].ex += e; else store(v, e, fact);
    1270       17117 : }
    1271             : static void
    1272        3030 : inv_fact(FACT *fact)
    1273             : {
    1274        3030 :   long i, l = fact[0].pr;
    1275        3030 :   for (i=1; i<=l; i++) fact[i].ex = -fact[i].ex;
    1276        3030 : }
    1277             : 
    1278             : /* L (small) list of primes above the same p including pr. Return pr index */
    1279             : static int
    1280       11094 : pr_index(GEN L, GEN pr)
    1281             : {
    1282       11094 :   long j, l = lg(L);
    1283       11094 :   GEN al = pr_get_gen(pr);
    1284       11094 :   for (j=1; j<l; j++)
    1285       11094 :     if (ZV_equal(al, pr_get_gen(gel(L,j)))) return j;
    1286           0 :   pari_err_BUG("codeprime");
    1287             :   return 0; /* LCOV_EXCL_LINE */
    1288             : }
    1289             : 
    1290             : static long
    1291       11094 : Vbase_to_FB(FB_t *F, GEN pr)
    1292             : {
    1293       11094 :   long p = pr_get_smallp(pr);
    1294       11094 :   return F->iLP[p] + pr_index(F->LV[p], pr);
    1295             : }
    1296             : 
    1297             : /* x, y 2 extended ideals whose first component is an integral HNF and second
    1298             :  * a famat */
    1299             : static GEN
    1300        1476 : idealHNF_mulred(GEN nf, GEN x, GEN y)
    1301             : {
    1302        1476 :   GEN A = idealHNF_mul(nf, gel(x,1), gel(y,1));
    1303        1476 :   GEN F = famat_mul_shallow(gel(x,2), gel(y,2));
    1304        1476 :   return idealred(nf, mkvec2(A, F));
    1305             : }
    1306             : /* idealred(x * pr^n), n > 0 is small, x extended ideal. Reduction in order to
    1307             :  * avoid prec pb: don't let id become too large as lgsub increases */
    1308             : static GEN
    1309       15161 : idealmulpowprimered(GEN nf, GEN x, GEN pr, ulong n)
    1310             : {
    1311       15161 :   GEN A = idealmulpowprime(nf, gel(x,1), pr, utoipos(n));
    1312       15161 :   return idealred(nf, mkvec2(A, gel(x,2)));
    1313             : }
    1314             : static GEN
    1315       17654 : init_famat(GEN x) { return mkvec2(x, trivial_fact()); }
    1316             : /* optimized idealfactorback + reduction; z = init_famat() */
    1317             : static GEN
    1318       10919 : powred(GEN z, GEN nf, GEN p, GEN e)
    1319       10919 : { gel(z,1) = p; return idealpowred(nf, z, e); }
    1320             : static GEN
    1321        9443 : genback(GEN z, GEN nf, GEN P, GEN E)
    1322             : {
    1323        9443 :   long i, l = lg(E);
    1324        9443 :   GEN I = NULL;
    1325       24192 :   for (i = 1; i < l; i++)
    1326       14749 :     if (signe(gel(E,i)))
    1327             :     {
    1328       10919 :       GEN J = powred(z, nf, gel(P,i), gel(E,i));
    1329       10919 :       I = I? idealHNF_mulred(nf, I, J): J;
    1330             :     }
    1331        9443 :   return I; /* != NULL since a generator */
    1332             : }
    1333             : 
    1334             : /* return famat y (principal ideal) such that y / x is smooth [wrt Vbase] */
    1335             : static GEN
    1336      413993 : SPLIT(FB_t *F, GEN nf, GEN x, GEN Vbase, FACT *fact)
    1337             : {
    1338      413993 :   GEN vecG, ex, y, x0, Nx = ZM_det_triangular(x);
    1339             :   long nbtest_lim, nbtest, i, j, ru, lgsub;
    1340             :   pari_sp av;
    1341             : 
    1342             :   /* try without reduction if x is small */
    1343      827965 :   if (gexpo(gcoeff(x,1,1)) < 100 &&
    1344      515031 :       can_factor(F, nf, x, NULL, Nx, fact)) return NULL;
    1345             : 
    1346      312934 :   av = avma;
    1347      312934 :   y = idealpseudomin_nonscalar(x, nf_get_roundG(nf));
    1348      312934 :   if (factorgen(F, nf, x, Nx, y, fact)) return y;
    1349       20391 :   set_avma(av);
    1350             : 
    1351             :   /* reduce in various directions */
    1352       20391 :   ru = lg(nf_get_roots(nf));
    1353       20391 :   vecG = cgetg(ru, t_VEC);
    1354       35490 :   for (j=1; j<ru; j++)
    1355             :   {
    1356       29652 :     gel(vecG,j) = nf_get_Gtwist1(nf, j);
    1357       29652 :     av = avma;
    1358       29652 :     y = idealpseudomin_nonscalar(x, gel(vecG,j));
    1359       29652 :     if (factorgen(F, nf, x, Nx, y, fact)) return y;
    1360       15099 :     set_avma(av);
    1361             :   }
    1362             : 
    1363             :   /* tough case, multiply by random products */
    1364        5838 :   lgsub = 3;
    1365        5838 :   ex = cgetg(lgsub, t_VECSMALL);
    1366        5838 :   x0 = init_famat(x);
    1367        5838 :   nbtest = 1; nbtest_lim = 4;
    1368             :   for(;;)
    1369        2221 :   {
    1370        8059 :     GEN Ired, I, NI, id = x0;
    1371        8059 :     av = avma;
    1372        8059 :     if (DEBUGLEVEL>2) err_printf("# ideals tried = %ld\n",nbtest);
    1373       24184 :     for (i=1; i<lgsub; i++)
    1374             :     {
    1375       16125 :       ex[i] = random_bits(RANDOM_BITS);
    1376       16125 :       if (ex[i]) id = idealmulpowprimered(nf, id, gel(Vbase,i), ex[i]);
    1377             :     }
    1378        8059 :     if (id == x0) continue;
    1379             :     /* I^(-1) * \prod Vbase[i]^ex[i] = (id[2]) / x */
    1380             : 
    1381        7990 :     I = gel(id,1); NI = ZM_det_triangular(I);
    1382        7990 :     if (can_factor(F, nf, I, NULL, NI, fact))
    1383             :     {
    1384        3030 :       inv_fact(fact); /* I^(-1) */
    1385        9097 :       for (i=1; i<lgsub; i++)
    1386        6067 :         if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1387        3030 :       return gel(id,2);
    1388             :     }
    1389        4960 :     Ired = ru == 2? I: ZM_lll(I, 0.99, LLL_INPLACE);
    1390       10224 :     for (j=1; j<ru; j++)
    1391             :     {
    1392        8072 :       pari_sp av2 = avma;
    1393        8072 :       y = idealpseudomin_nonscalar(Ired, gel(vecG,j));
    1394        8072 :       if (factorgen(F, nf, I, NI, y, fact))
    1395             :       {
    1396        8424 :         for (i=1; i<lgsub; i++)
    1397        5616 :           if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1398        2808 :         return famat_mul_shallow(gel(id,2), y);
    1399             :       }
    1400        5264 :       set_avma(av2);
    1401             :     }
    1402        2152 :     set_avma(av);
    1403        2152 :     if (++nbtest > nbtest_lim)
    1404             :     {
    1405           7 :       nbtest = 0;
    1406           7 :       if (++lgsub < minss(8, lg(Vbase)-1))
    1407             :       {
    1408           7 :         nbtest_lim <<= 1;
    1409           7 :         ex = cgetg(lgsub, t_VECSMALL);
    1410             :       }
    1411           0 :       else nbtest_lim = LONG_MAX; /* don't increase further */
    1412           7 :       if (DEBUGLEVEL>2) err_printf("SPLIT: increasing factor base [%ld]\n",lgsub);
    1413             :     }
    1414             :   }
    1415             : }
    1416             : 
    1417             : INLINE GEN
    1418      397669 : bnf_get_W(GEN bnf) { return gel(bnf,1); }
    1419             : INLINE GEN
    1420      795324 : bnf_get_B(GEN bnf) { return gel(bnf,2); }
    1421             : INLINE GEN
    1422      804480 : bnf_get_C(GEN bnf) { return gel(bnf,4); }
    1423             : INLINE GEN
    1424      397725 : bnf_get_vbase(GEN bnf) { return gel(bnf,5); }
    1425             : INLINE GEN
    1426      397655 : bnf_get_Ur(GEN bnf) { return gmael(bnf,9,1); }
    1427             : INLINE GEN
    1428      150147 : bnf_get_ga(GEN bnf) { return gmael(bnf,9,2); }
    1429             : INLINE GEN
    1430      153451 : bnf_get_GD(GEN bnf) { return gmael(bnf,9,3); }
    1431             : 
    1432             : /* Return y (as an elt of K or a t_MAT representing an elt in Z[K])
    1433             :  * such that x / (y) is smooth and store the exponents of  its factorization
    1434             :  * on g_W and g_B in Wex / Bex; return NULL for y = 1 */
    1435             : static GEN
    1436      397655 : split_ideal(GEN bnf, GEN x, GEN *pWex, GEN *pBex)
    1437             : {
    1438      397655 :   GEN L, y, Vbase = bnf_get_vbase(bnf);
    1439      397655 :   GEN Wex, W  = bnf_get_W(bnf);
    1440      397655 :   GEN Bex, B  = bnf_get_B(bnf);
    1441             :   long p, j, i, l, nW, nB;
    1442             :   FACT *fact;
    1443             :   FB_t F;
    1444             : 
    1445      397655 :   L = recover_partFB(&F, Vbase, lg(x)-1);
    1446      397655 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    1447      397655 :   y = SPLIT(&F, bnf_get_nf(bnf), x, Vbase, fact);
    1448      397655 :   nW = lg(W)-1; *pWex = Wex = zero_zv(nW);
    1449      397655 :   nB = lg(B)-1; *pBex = Bex = zero_zv(nB); l = lg(F.FB);
    1450      397655 :   p = j = 0; /* -Wall */
    1451      772286 :   for (i = 1; i <= fact[0].pr; i++)
    1452             :   { /* decode index C = ip+j --> (p,j) */
    1453      374631 :     long a, b, t, C = fact[i].pr;
    1454     1142783 :     for (t = 1; t < l; t++)
    1455             :     {
    1456     1095603 :       long q = F.FB[t], k = C - F.iLP[q];
    1457     1095603 :       if (k <= 0) break;
    1458      768152 :       p = q;
    1459      768152 :       j = k;
    1460             :     }
    1461      374631 :     a = gel(L, p)[j];
    1462      374631 :     b = a - nW;
    1463      374631 :     if (b <= 0) Wex[a] = y? -fact[i].ex: fact[i].ex;
    1464      279410 :     else        Bex[b] = y? -fact[i].ex: fact[i].ex;
    1465             :   }
    1466      397655 :   return y;
    1467             : }
    1468             : 
    1469             : GEN
    1470      210096 : init_red_mod_units(GEN bnf, long prec)
    1471             : {
    1472      210096 :   GEN s = gen_0, p1,s1,mat, logfu = bnf_get_logfu(bnf);
    1473      210096 :   long i,j, RU = lg(logfu);
    1474             : 
    1475      210096 :   if (RU == 1) return NULL;
    1476      210096 :   mat = cgetg(RU,t_MAT);
    1477      543280 :   for (j=1; j<RU; j++)
    1478             :   {
    1479      333184 :     p1 = cgetg(RU+1,t_COL); gel(mat,j) = p1;
    1480      333184 :     s1 = gen_0;
    1481      960816 :     for (i=1; i<RU; i++)
    1482             :     {
    1483      627632 :       gel(p1,i) = real_i(gcoeff(logfu,i,j));
    1484      627632 :       s1 = mpadd(s1, mpsqr(gel(p1,i)));
    1485             :     }
    1486      333184 :     gel(p1,RU) = gen_0; if (mpcmp(s1,s) > 0) s = s1;
    1487             :   }
    1488      210096 :   s = gsqrt(gmul2n(s,RU),prec);
    1489      210096 :   if (expo(s) < 27) s = utoipos(1UL << 27);
    1490      210096 :   return mkvec2(mat, s);
    1491             : }
    1492             : 
    1493             : /* z computed above. Return unit exponents that would reduce col (arch) */
    1494             : GEN
    1495      210096 : red_mod_units(GEN col, GEN z)
    1496             : {
    1497             :   long i,RU;
    1498             :   GEN x,mat,N2;
    1499             : 
    1500      210096 :   if (!z) return NULL;
    1501      210096 :   mat= gel(z,1);
    1502      210096 :   N2 = gel(z,2);
    1503      210096 :   RU = lg(mat); x = cgetg(RU+1,t_COL);
    1504      210096 :   for (i=1; i<RU; i++) gel(x,i) = real_i(gel(col,i));
    1505      210096 :   gel(x,RU) = N2;
    1506      210096 :   x = lll(shallowconcat(mat,x));
    1507      210096 :   if (typ(x) != t_MAT) return NULL;
    1508      210096 :   x = gel(x,RU);
    1509      210096 :   if (signe(gel(x,RU)) < 0) x = gneg_i(x);
    1510      210096 :   if (!gequal1(gel(x,RU))) pari_err_BUG("red_mod_units");
    1511      210096 :   setlg(x,RU); return x;
    1512             : }
    1513             : 
    1514             : static GEN
    1515      718686 : add(GEN a, GEN t) { return a = a? RgC_add(a,t): t; }
    1516             : 
    1517             : /* [x] archimedian components, A column vector. return [x] A */
    1518             : static GEN
    1519      610049 : act_arch(GEN A, GEN x)
    1520             : {
    1521             :   GEN a;
    1522      610049 :   long i,l = lg(A), tA = typ(A);
    1523      610049 :   if (tA == t_MAT)
    1524             :   { /* assume lg(x) >= l */
    1525       35721 :     a = cgetg(l, t_MAT);
    1526       35721 :     for (i=1; i<l; i++) gel(a,i) = act_arch(gel(A,i), x);
    1527       35721 :     return a;
    1528             :   }
    1529      574328 :   if (l==1) return cgetg(1, t_COL);
    1530      574328 :   a = NULL;
    1531      574328 :   if (tA == t_VECSMALL)
    1532             :   {
    1533     1781170 :     for (i=1; i<l; i++)
    1534             :     {
    1535     1537397 :       long c = A[i];
    1536     1537397 :       if (c) a = add(a, gmulsg(c, gel(x,i)));
    1537             :     }
    1538             :   }
    1539             :   else
    1540             :   { /* A a t_COL of t_INT. Assume lg(A)==lg(x) */
    1541      696866 :     for (i=1; i<l; i++)
    1542             :     {
    1543      366311 :       GEN c = gel(A,i);
    1544      366311 :       if (signe(c)) a = add(a, gmul(c, gel(x,i)));
    1545             :     }
    1546             :   }
    1547      574328 :   return a? a: zerocol(lgcols(x)-1);
    1548             : }
    1549             : /* act_arch(matdiagonal(v), x) */
    1550             : static GEN
    1551       11907 : diagact_arch(GEN v, GEN x)
    1552             : {
    1553       11907 :   long i, l = lg(v);
    1554       11907 :   GEN a = cgetg(l, t_MAT);
    1555       11907 :   for (i = 1; i < l; i++) gel(a,i) = gmul(gel(x,i), gel(v,i));
    1556       11907 :   return a;
    1557             : }
    1558             : 
    1559             : static long
    1560      405698 : prec_arch(GEN bnf)
    1561             : {
    1562      405698 :   GEN a = bnf_get_C(bnf);
    1563      405698 :   long i, l = lg(a), prec;
    1564             : 
    1565      405698 :   for (i=1; i<l; i++)
    1566      405572 :     if ( (prec = gprecision(gel(a,i))) ) return prec;
    1567         126 :   return DEFAULTPREC;
    1568             : }
    1569             : 
    1570             : static long
    1571        1212 : needed_bitprec(GEN x)
    1572             : {
    1573        1212 :   long i, e = 0, l = lg(x);
    1574        7031 :   for (i = 1; i < l; i++)
    1575             :   {
    1576        5819 :     GEN c = gel(x,i);
    1577        5819 :     long f = gexpo(c) - prec2nbits(gprecision(c));
    1578        5819 :     if (f > e) e = f;
    1579             :   }
    1580        1212 :   return e;
    1581             : }
    1582             : 
    1583             : /* col = archimedian components of x, Nx its norm, dx a multiple of its
    1584             :  * denominator. Return x or NULL (fail) */
    1585             : GEN
    1586      246195 : isprincipalarch(GEN bnf, GEN col, GEN kNx, GEN e, GEN dx, long *pe)
    1587             : {
    1588             :   GEN nf, x, y, logfu, s, M;
    1589      246195 :   long N, prec = gprecision(col);
    1590      246195 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf); M = nf_get_M(nf);
    1591      246195 :   if (!prec) prec = prec_arch(bnf);
    1592      246195 :   *pe = 128;
    1593      246195 :   logfu = bnf_get_logfu(bnf);
    1594      246195 :   N = nf_get_degree(nf);
    1595      246195 :   if (!(col = cleanarch(col,N,prec))) return NULL;
    1596      246195 :   if (lg(col) > 2)
    1597             :   { /* reduce mod units */
    1598      210096 :     GEN u, z = init_red_mod_units(bnf,prec);
    1599      210096 :     if (!(u = red_mod_units(col,z))) return NULL;
    1600      210096 :     col = RgC_add(col, RgM_RgC_mul(logfu, u));
    1601      210096 :     if (!(col = cleanarch(col,N,prec))) return NULL;
    1602             :   }
    1603      246195 :   s = divru(mulir(e, glog(kNx,prec)), N);
    1604      246195 :   col = fixarch(col, s, nf_get_r1(nf));
    1605      246195 :   if (RgC_expbitprec(col) >= 0) return NULL;
    1606      246195 :   col = gexp(col, prec);
    1607             :   /* d.alpha such that x = alpha \prod gj^ej */
    1608      246195 :   x = RgM_solve_realimag(M,col); if (!x) return NULL;
    1609      246195 :   x = RgC_Rg_mul(x, dx);
    1610      246195 :   y = grndtoi(x, pe);
    1611      246195 :   if (*pe > -5) { *pe = needed_bitprec(x); return NULL; }
    1612      244983 :   return RgC_Rg_div(y, dx);
    1613             : }
    1614             : 
    1615             : /* y = C \prod g[i]^e[i] ? */
    1616             : static int
    1617      244983 : fact_ok(GEN nf, GEN y, GEN C, GEN g, GEN e)
    1618             : {
    1619      244983 :   pari_sp av = avma;
    1620      244983 :   long i, c = lg(e);
    1621      244983 :   GEN z = C? C: gen_1;
    1622      403498 :   for (i=1; i<c; i++)
    1623      158515 :     if (signe(gel(e,i))) z = idealmul(nf, z, idealpow(nf, gel(g,i), gel(e,i)));
    1624      244983 :   if (typ(z) != t_MAT) z = idealhnf_shallow(nf,z);
    1625      244983 :   if (typ(y) != t_MAT) y = idealhnf_shallow(nf,y);
    1626      244983 :   return gc_bool(av, ZM_equal(y,z));
    1627             : }
    1628             : static GEN
    1629      397655 : ZV_divrem(GEN A, GEN B, GEN *pR)
    1630             : {
    1631      397655 :   long i, l = lg(A);
    1632      397655 :   GEN Q = cgetg(l, t_COL), R = cgetg(l, t_COL);
    1633      397655 :   for (i = 1; i < l; i++) gel(Q,i) = truedvmdii(gel(A,i), gel(B,i), &gel(R,i));
    1634      397655 :   *pR = R; return Q;
    1635             : }
    1636             : 
    1637             : static GEN
    1638      397655 : Ur_ZC_mul(GEN bnf, GEN v)
    1639             : {
    1640      397655 :   GEN w, U = bnf_get_Ur(bnf);
    1641      397655 :   long i, l = lg(bnf_get_cyc(bnf)); /* may be < lgcols(U) */
    1642             : 
    1643      397655 :   w = cgetg(l, t_COL);
    1644      397655 :   for (i = 1; i < l; i++) gel(w,i) = ZMrow_ZC_mul(U, v, i);
    1645      397655 :   return w;
    1646             : }
    1647             : 
    1648             : static GEN
    1649         175 : ZV_mul(GEN x, GEN y)
    1650             : {
    1651         175 :   long i, l = lg(x);
    1652         175 :   GEN z = cgetg(l, t_COL);
    1653         175 :   for (i = 1; i < l; i++) gel(z,i) = mulii(gel(x,i), gel(y,i));
    1654         175 :   return z;
    1655             : }
    1656             : 
    1657             : /* assume x in HNF; cf class_group_gen for notations. Return NULL iff
    1658             :  * flag & nf_FORCE and computation of principal ideal generator fails */
    1659             : static GEN
    1660      398768 : isprincipalall(GEN bnf, GEN x, long *pprec, long flag)
    1661             : {
    1662             :   GEN xar, Wex, Bex, gen, xc, d, col, A, Q, R, q, UA, SUnits;
    1663      398768 :   GEN C = bnf_get_C(bnf), nf = bnf_get_nf(bnf), cyc = bnf_get_cyc(bnf);
    1664             :   long nB, nW, e;
    1665             : 
    1666      398768 :   if (lg(cyc) == 1 && !(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL)))
    1667         987 :     return cgetg(1,t_COL);
    1668      397781 :   if (lg(x) == 2)
    1669             :   { /* nf = Q */
    1670         126 :     col = gel(x,1);
    1671         126 :     return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(cgetg(1,t_COL), col);
    1672             :   }
    1673             : 
    1674      397655 :   x = Q_primitive_part(x, &xc);
    1675      397655 :   xar = split_ideal(bnf, x, &Wex, &Bex);
    1676             :   /* x = g_W Wex + g_B Bex + [xar] = g_W (Wex - B*Bex) + [xar] + [C_B]Bex */
    1677      397655 :   A = zc_to_ZC(Wex); nB = lg(Bex)-1;
    1678      397655 :   if (nB) A = ZC_sub(A, ZM_zc_mul(bnf_get_B(bnf), Bex));
    1679      397655 :   UA = Ur_ZC_mul(bnf, A);
    1680      397655 :   Q = ZV_divrem(UA, cyc, &R);
    1681             :   /* g_W (Wex - B*Bex) = G Ur A - [ga]A = G R + [GD]Q - [ga]A
    1682             :    * Finally: x = G R + [xar] + [C_B]Bex + [GD]Q - [ga]A */
    1683      397655 :   if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1684      243780 :   if ((flag & nf_GEN_IF_PRINCIPAL) && !ZV_equal0(R)) return gen_0;
    1685             : 
    1686      243773 :   nW = lg(Wex)-1;
    1687      243773 :   col = xar? nf_cxlog(nf, xar, *pprec): NULL;
    1688      243773 :   if (nB) col = add(col, act_arch(Bex, nW? vecslice(C,nW+1,lg(C)-1): C));
    1689      243773 :   if (nW)
    1690             :   {
    1691      150147 :     GEN v = RgC_sub(act_arch(Q, bnf_get_GD(bnf)), act_arch(A, bnf_get_ga(bnf)));
    1692      150147 :     col = add(col, v);
    1693             :   }
    1694             : 
    1695             :   /* find coords on Zk; q = N (x / prod gj^ej) = N(alpha), denom(alpha) | d */
    1696      243773 :   gen = bnf_get_gen(bnf);
    1697      243773 :   q = gdiv(ZM_det_triangular(x), get_norm_fact(gen, R, &d));
    1698      243773 :   col = isprincipalarch(bnf, col, q, gen_1, d, &e);
    1699      243773 :   if (col && !fact_ok(nf,x, col,gen,R)) col = NULL;
    1700      243773 :   if (!col && (flag & nf_GENMAT) && (SUnits = bnf_get_sunits(bnf)))
    1701             :   {
    1702         204 :     GEN X = gel(SUnits,1), U = gel(SUnits,2), C = gel(SUnits,3);
    1703         204 :     GEN v = gel(bnf,9), Ge = gel(v,4), M1 = gel(v,5), M2 = gel(v,6);
    1704         204 :     GEN z = NULL, F = NULL;
    1705         204 :     if (nB)
    1706             :     {
    1707         204 :       GEN C2 = nW? vecslice(C, nW+1, lg(C)-1): C;
    1708         204 :       z = ZM_zc_mul(C2, Bex);
    1709             :     }
    1710         204 :     if (nW)
    1711             :     { /* [GD]Q - [ga]A = ([X]M1 - [Ge]D) Q - ([X]M2 - [Ge]Ur) A */
    1712         175 :       GEN C1 = vecslice(C, 1, nW);
    1713         175 :       GEN v = ZC_sub(ZM_ZC_mul(M1,Q), ZM_ZC_mul(M2,A));
    1714         175 :       z = add(z, ZM_ZC_mul(C1, v));
    1715         175 :       F = famat_reduce(famatV_factorback(Ge, ZC_sub(UA, ZV_mul(cyc,Q))));
    1716         175 :       if (lgcols(F) == 1) F = NULL;
    1717             :     }
    1718             :     /* reduce modulo units and Q^* */
    1719         204 :     if (lg(U) != 1) z = ZC_sub(z, ZM_ZC_mul(U, RgM_Babai(U,z)));
    1720         204 :     col = mkmat2(X, z);
    1721         204 :     if (F) col = famat_mul_shallow(col, F);
    1722         204 :     col = famat_remove_trivial(col);
    1723         204 :     if (xar) col = famat_mul_shallow(col, xar);
    1724             :   }
    1725      243773 :   if (!col && !ZV_equal0(R))
    1726             :   { /* in case isprincipalfact calls bnfinit() due to prec trouble...*/
    1727         924 :     GEN y = isprincipalfact(bnf, x, gen, ZC_neg(R), flag);
    1728         924 :     if (typ(y) != t_VEC) return y;
    1729         924 :     col = gel(y,2);
    1730             :   }
    1731      243773 :   if (col)
    1732             :   { /* add back missing content */
    1733      244697 :     if (xc) col = (typ(col)==t_MAT)? famat_mul_shallow(col,xc)
    1734        1001 :                                    : RgC_Rg_mul(col,xc);
    1735             :   }
    1736             :   else
    1737             :   {
    1738          77 :     if (e < 0) e = 0;
    1739          77 :     *pprec += nbits2extraprec(e + 128);
    1740          77 :     if (flag & nf_FORCE)
    1741             :     {
    1742          70 :       if (DEBUGLEVEL)
    1743           0 :         pari_warn(warner,"precision too low for generators, e = %ld",e);
    1744          70 :       return NULL;
    1745             :     }
    1746           7 :     pari_warn(warner,"precision too low for generators, not given");
    1747           7 :     col = cgetg(1, t_COL);
    1748             :   }
    1749      243703 :   return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(R, col);
    1750             : }
    1751             : 
    1752             : static GEN
    1753       61705 : triv_gen(GEN bnf, GEN x, long flag)
    1754             : {
    1755       61705 :   GEN nf = bnf_get_nf(bnf);
    1756             :   long c;
    1757       61705 :   if (flag & nf_GEN_IF_PRINCIPAL) return algtobasis(nf,x);
    1758       61705 :   c = lg(bnf_get_cyc(bnf)) - 1;
    1759       61705 :   if (flag & (nf_GEN|nf_GENMAT)) retmkvec2(zerocol(c), algtobasis(nf,x));
    1760        9485 :   return zerocol(c);
    1761             : }
    1762             : 
    1763             : GEN
    1764      437723 : bnfisprincipal0(GEN bnf,GEN x,long flag)
    1765             : {
    1766             :   GEN arch, c, nf;
    1767             :   long pr;
    1768      437723 :   pari_sp av = avma;
    1769             : 
    1770      437723 :   bnf = checkbnf(bnf);
    1771      437723 :   nf = bnf_get_nf(bnf);
    1772      437723 :   switch( idealtyp(&x, &arch) )
    1773             :   {
    1774             :     case id_PRINCIPAL:
    1775       49665 :       if (gequal0(x)) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1776       49665 :       return triv_gen(bnf, x, flag);
    1777             :     case id_PRIME:
    1778      376795 :       if (pr_is_inert(x))
    1779       12040 :         return gerepileupto(av, triv_gen(bnf, pr_get_p(x), flag));
    1780      364755 :       x = pr_hnf(nf, x);
    1781      364755 :       break;
    1782             :     case id_MAT:
    1783       11263 :       if (lg(x)==1) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1784       11263 :       if (nf_get_degree(nf) != lg(x)-1)
    1785           0 :         pari_err_TYPE("idealtyp [dimension != degree]", x);
    1786             :   }
    1787      376018 :   pr = prec_arch(bnf); /* precision of unit matrix */
    1788      376018 :   c = getrand();
    1789             :   for (;;)
    1790           0 :   {
    1791      376018 :     pari_sp av1 = avma;
    1792      376018 :     GEN y = isprincipalall(bnf,x,&pr,flag);
    1793      376018 :     if (y) return gerepilecopy(av, y);
    1794             : 
    1795           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",pr);
    1796           0 :     set_avma(av1); bnf = bnfnewprec_shallow(bnf,pr); setrand(c);
    1797             :   }
    1798             : }
    1799             : GEN
    1800      164305 : isprincipal(GEN bnf,GEN x) { return bnfisprincipal0(bnf,x,0); }
    1801             : 
    1802             : /* FIXME: OBSOLETE */
    1803             : GEN
    1804           0 : isprincipalgen(GEN bnf,GEN x)
    1805           0 : { return bnfisprincipal0(bnf,x,nf_GEN); }
    1806             : GEN
    1807           0 : isprincipalforce(GEN bnf,GEN x)
    1808           0 : { return bnfisprincipal0(bnf,x,nf_FORCE); }
    1809             : GEN
    1810           0 : isprincipalgenforce(GEN bnf,GEN x)
    1811           0 : { return bnfisprincipal0(bnf,x,nf_GEN | nf_FORCE); }
    1812             : 
    1813             : /* lg(u) > 1 */
    1814             : static int
    1815        9045 : RgV_is1(GEN u) { return isint1(gel(u,1)) && RgV_isscalar(u); }
    1816             : static GEN
    1817       22680 : add_principal_part(GEN nf, GEN u, GEN v, long flag)
    1818             : {
    1819       22680 :   if (flag & nf_GENMAT)
    1820        9163 :     return (typ(u) == t_COL && RgV_is1(u))? v: famat_mul_shallow(v,u);
    1821             :   else
    1822       13517 :     return nfmul(nf, v, u);
    1823             : }
    1824             : 
    1825             : #if 0
    1826             : /* compute C prod P[i]^e[i],  e[i] >=0 for all i. C may be NULL (omitted)
    1827             :  * e destroyed ! */
    1828             : static GEN
    1829             : expand(GEN nf, GEN C, GEN P, GEN e)
    1830             : {
    1831             :   long i, l = lg(e), done = 1;
    1832             :   GEN id = C;
    1833             :   for (i=1; i<l; i++)
    1834             :   {
    1835             :     GEN ei = gel(e,i);
    1836             :     if (signe(ei))
    1837             :     {
    1838             :       if (mod2(ei)) id = id? idealmul(nf, id, gel(P,i)): gel(P,i);
    1839             :       ei = shifti(ei,-1);
    1840             :       if (signe(ei)) done = 0;
    1841             :       gel(e,i) = ei;
    1842             :     }
    1843             :   }
    1844             :   if (id != C) id = idealred(nf, id);
    1845             :   if (done) return id;
    1846             :   return idealmulred(nf, id, idealsqr(nf, expand(nf,id,P,e)));
    1847             : }
    1848             : /* C is an extended ideal, possibly with C[1] = NULL */
    1849             : static GEN
    1850             : expandext(GEN nf, GEN C, GEN P, GEN e)
    1851             : {
    1852             :   long i, l = lg(e), done = 1;
    1853             :   GEN A = gel(C,1);
    1854             :   for (i=1; i<l; i++)
    1855             :   {
    1856             :     GEN ei = gel(e,i);
    1857             :     if (signe(ei))
    1858             :     {
    1859             :       if (mod2(ei)) A = A? idealmul(nf, A, gel(P,i)): gel(P,i);
    1860             :       ei = shifti(ei,-1);
    1861             :       if (signe(ei)) done = 0;
    1862             :       gel(e,i) = ei;
    1863             :     }
    1864             :   }
    1865             :   if (A == gel(C,1))
    1866             :     A = C;
    1867             :   else
    1868             :     A = idealred(nf, mkvec2(A, gel(C,2)));
    1869             :   if (done) return A;
    1870             :   return idealmulred(nf, A, idealsqr(nf, expand(nf,A,P,e)));
    1871             : }
    1872             : #endif
    1873             : 
    1874             : static GEN
    1875           0 : expand(GEN nf, GEN C, GEN P, GEN e)
    1876             : {
    1877           0 :   long i, l = lg(e);
    1878           0 :   GEN B, A = C;
    1879           0 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    1880           0 :     if (signe(gel(e,i)))
    1881             :     {
    1882           0 :       B = idealpowred(nf, gel(P,i), gel(e,i));
    1883           0 :       A = A? idealmulred(nf,A,B): B;
    1884             :     }
    1885           0 :   return A;
    1886             : }
    1887             : static GEN
    1888       22687 : expandext(GEN nf, GEN C, GEN P, GEN e)
    1889             : {
    1890       22687 :   long i, l = lg(e);
    1891       22687 :   GEN B, A = gel(C,1), C1 = A;
    1892       73353 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    1893       50666 :     if (signe(gel(e,i)))
    1894             :     {
    1895       28902 :       gel(C,1) = gel(P,i);
    1896       28902 :       B = idealpowred(nf, C, gel(e,i));
    1897       28902 :       A = A? idealmulred(nf,A,B): B;
    1898             :     }
    1899       22687 :   return A == C1? C: A;
    1900             : }
    1901             : 
    1902             : /* isprincipal for C * \prod P[i]^e[i] (C omitted if NULL) */
    1903             : GEN
    1904       22687 : isprincipalfact(GEN bnf, GEN C, GEN P, GEN e, long flag)
    1905             : {
    1906       22687 :   const long gen = flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL);
    1907             :   long prec;
    1908       22687 :   pari_sp av = avma;
    1909       22687 :   GEN C0, Cext, c, id, nf = checknf(bnf);
    1910             : 
    1911       22687 :   if (gen)
    1912             :   {
    1913       45374 :     Cext = (flag & nf_GENMAT)? trivial_fact()
    1914       22687 :                              : mkpolmod(gen_1,nf_get_pol(nf));
    1915       22687 :     C0 = mkvec2(C, Cext);
    1916       22687 :     id = expandext(nf, C0, P, e);
    1917             :   } else {
    1918           0 :     Cext = NULL;
    1919           0 :     C0 = C;
    1920           0 :     id = expand(nf, C, P, e);
    1921             :   }
    1922       22687 :   if (id == C0) /* e = 0 */
    1923             :   {
    1924        8331 :     if (!C) return bnfisprincipal0(bnf, gen_1, flag);
    1925        8324 :     C = idealhnf_shallow(nf,C);
    1926             :   }
    1927             :   else
    1928             :   {
    1929       14356 :     if (gen) { C = gel(id,1); Cext = gel(id,2); } else C = id;
    1930             :   }
    1931       22680 :   prec = prec_arch(bnf);
    1932       22680 :   c = getrand();
    1933             :   for (;;)
    1934          70 :   {
    1935       22750 :     pari_sp av1 = avma;
    1936       22750 :     GEN y = isprincipalall(bnf, C, &prec, flag);
    1937       22750 :     if (y)
    1938             :     {
    1939       22680 :       if (flag & nf_GEN_IF_PRINCIPAL)
    1940             :       {
    1941       18158 :         if (typ(y) == t_INT) return gc_NULL(av);
    1942       18158 :         y = add_principal_part(nf, y, Cext, flag);
    1943             :       }
    1944             :       else
    1945             :       {
    1946        4522 :         GEN u = gel(y,2);
    1947        4522 :         if (!gen || typ(y) != t_VEC) return gerepileupto(av,y);
    1948        4522 :         if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    1949             :       }
    1950       22680 :       return gerepilecopy(av, y);
    1951             :     }
    1952          70 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",prec);
    1953          70 :     set_avma(av1); bnf = bnfnewprec_shallow(bnf,prec); setrand(c);
    1954             :   }
    1955             : }
    1956             : GEN
    1957           0 : isprincipalfact_or_fail(GEN bnf, GEN C, GEN P, GEN e)
    1958             : {
    1959           0 :   const long flag = nf_GENMAT|nf_FORCE;
    1960             :   long prec;
    1961           0 :   pari_sp av = avma;
    1962           0 :   GEN u, y, id, C0, Cext, nf = bnf_get_nf(bnf);
    1963             : 
    1964           0 :   Cext = trivial_fact();
    1965           0 :   C0 = mkvec2(C, Cext);
    1966           0 :   id = expandext(nf, C0, P, e);
    1967           0 :   if (id == C0) /* e = 0 */
    1968           0 :     C = idealhnf_shallow(nf,C);
    1969             :   else {
    1970           0 :     C = gel(id,1); Cext = gel(id,2);
    1971             :   }
    1972           0 :   prec = prec_arch(bnf);
    1973           0 :   y = isprincipalall(bnf, C, &prec, flag);
    1974           0 :   if (!y) { set_avma(av); return utoipos(prec); }
    1975           0 :   u = gel(y,2);
    1976           0 :   if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    1977           0 :   return gerepilecopy(av, y);
    1978             : }
    1979             : 
    1980             : GEN
    1981       24661 : nfsign_from_logarch(GEN LA, GEN invpi, GEN archp)
    1982             : {
    1983       24661 :   long l = lg(archp), i;
    1984       24661 :   GEN y = cgetg(l, t_VECSMALL);
    1985       24661 :   pari_sp av = avma;
    1986             : 
    1987       55559 :   for (i=1; i<l; i++)
    1988             :   {
    1989       30898 :     GEN c = ground( gmul(imag_i(gel(LA,archp[i])), invpi) );
    1990       30898 :     y[i] = mpodd(c)? 1: 0;
    1991             :   }
    1992       24661 :   set_avma(av); return y;
    1993             : }
    1994             : 
    1995             : GEN
    1996       37464 : nfsign_tu(GEN bnf, GEN archp)
    1997             : {
    1998             :   long n;
    1999       37464 :   if (bnf_get_tuN(bnf) != 2) return cgetg(1, t_VECSMALL);
    2000       33306 :   n = archp? lg(archp) - 1: nf_get_r1(bnf_get_nf(bnf));
    2001       33306 :   return const_vecsmall(n, 1);
    2002             : }
    2003             : GEN
    2004       38703 : nfsign_fu(GEN bnf, GEN archp)
    2005             : {
    2006       38703 :   GEN invpi, y, A = bnf_get_logfu(bnf), nf = bnf_get_nf(bnf);
    2007       38703 :   long j = 1, RU = lg(A);
    2008             : 
    2009       38703 :   if (!archp) archp = identity_perm( nf_get_r1(nf) );
    2010       38703 :   invpi = invr( mppi(nf_get_prec(nf)) );
    2011       38703 :   y = cgetg(RU,t_MAT);
    2012       63266 :   for (j = 1; j < RU; j++)
    2013       24563 :     gel(y,j) = nfsign_from_logarch(gel(A,j), invpi, archp);
    2014       38703 :   return y;
    2015             : }
    2016             : GEN
    2017          35 : nfsign_units(GEN bnf, GEN archp, int add_zu)
    2018             : {
    2019          35 :   GEN sfu = nfsign_fu(bnf, archp);
    2020          35 :   return add_zu? vec_prepend(sfu, nfsign_tu(bnf, archp)): sfu;
    2021             : }
    2022             : 
    2023             : /* obsolete */
    2024             : GEN
    2025           7 : signunits(GEN bnf)
    2026             : {
    2027             :   pari_sp av;
    2028             :   GEN S, y, nf;
    2029             :   long i, j, r1, r2;
    2030             : 
    2031           7 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    2032           7 :   nf_get_sign(nf, &r1,&r2);
    2033           7 :   S = zeromatcopy(r1, r1+r2-1); av = avma;
    2034           7 :   y = nfsign_fu(bnf, NULL);
    2035          14 :   for (j = 1; j < lg(y); j++)
    2036             :   {
    2037           7 :     GEN Sj = gel(S,j), yj = gel(y,j);
    2038           7 :     for (i = 1; i <= r1; i++) gel(Sj,i) = yj[i]? gen_m1: gen_1;
    2039             :   }
    2040           7 :   set_avma(av); return S;
    2041             : }
    2042             : 
    2043             : static GEN
    2044      131519 : get_log_embed(REL_t *rel, GEN M, long RU, long R1, long prec)
    2045             : {
    2046      131519 :   GEN arch, C, z = rel->m;
    2047             :   long i;
    2048      131519 :   arch = typ(z) == t_COL? RgM_RgC_mul(M, z): const_col(nbrows(M), z);
    2049      131519 :   C = cgetg(RU+1, t_COL); arch = glog(arch, prec);
    2050      131519 :   for (i=1; i<=R1; i++) gel(C,i) = gel(arch,i);
    2051      131519 :   for (   ; i<=RU; i++) gel(C,i) = gmul2n(gel(arch,i), 1);
    2052      131519 :   return C;
    2053             : }
    2054             : static GEN
    2055      194854 : rel_embed(REL_t *rel, FB_t *F, GEN embs, long ind, GEN M, long RU, long R1,
    2056             :           long prec)
    2057             : {
    2058             :   GEN C, D, perm;
    2059             :   long i, n;
    2060      194854 :   if (!rel->relaut) return get_log_embed(rel, M, RU, R1, prec);
    2061             :   /* image of another relation by automorphism */
    2062       63335 :   C = gel(embs, ind - rel->relorig);
    2063       63335 :   perm = gel(F->embperm, rel->relaut);
    2064       63335 :   D = cgetg_copy(C, &n);
    2065      264975 :   for (i = 1; i < n; i++)
    2066             :   {
    2067      201640 :     long v = perm[i];
    2068      201640 :     gel(D,i) = (v > 0)? gel(C,v): conj_i(gel(C,-v));
    2069             :   }
    2070       63335 :   return D;
    2071             : }
    2072             : static GEN
    2073       25482 : get_embs(FB_t *F, RELCACHE_t *cache, GEN nf, long RU, long R1, GEN embs,
    2074             :          long PREC)
    2075             : {
    2076       25482 :   long l = cache->last - cache->chk + 1, j, k;
    2077       25482 :   GEN M = nf_get_M(nf), nembs = cgetg(cache->last - cache->base+1, t_MAT);
    2078             :   REL_t *rel;
    2079             : 
    2080       25482 :   for (k = 1; k <= cache->chk - cache->base; k++) gel(nembs,k) = gel(embs,k);
    2081       25482 :   embs = nembs;
    2082      214897 :   for (j=1,rel = cache->chk + 1; j < l; rel++,j++,k++)
    2083      189415 :     gel(embs,k) = rel_embed(rel, F, embs, k, M, RU, R1, PREC);
    2084       25482 :   return embs;
    2085             : }
    2086             : static void
    2087       58669 : set_rel_alpha(REL_t *rel, GEN auts, GEN vA, long ind)
    2088             : {
    2089             :   GEN u;
    2090       58669 :   if (!rel->relaut)
    2091       33030 :     u = rel->m;
    2092             :   else
    2093       25639 :     u = ZM_ZC_mul(gel(auts, rel->relaut), gel(vA, ind - rel->relorig));
    2094       58669 :   gel(vA, ind) = u;
    2095       58669 : }
    2096             : static GEN
    2097      820165 : set_fact(FB_t *F, FACT *fact, GEN e, long *pnz)
    2098             : {
    2099      820165 :   long n = fact[0].pr;
    2100      820165 :   GEN c = zero_Flv(F->KC);
    2101      820165 :   if (!n) /* trivial factorization */
    2102           0 :     *pnz = F->KC+1;
    2103             :   else
    2104             :   {
    2105      820165 :     long i, nz = minss(fact[1].pr, fact[n].pr);
    2106      820165 :     for (i = 1; i <= n; i++) c[fact[i].pr] = fact[i].ex;
    2107      820165 :     if (e)
    2108             :     {
    2109        6023 :       long l = lg(e);
    2110       27846 :       for (i = 1; i < l; i++)
    2111       21823 :         if (e[i]) { long v = F->subFB[i]; c[v] += e[i]; if (v < nz) nz = v; }
    2112             :     }
    2113      820165 :     *pnz = nz;
    2114             :   }
    2115      820165 :   return c;
    2116             : }
    2117             : 
    2118             : /* Is cols already in the cache ? bs = index of first non zero coeff in cols
    2119             :  * General check for colinearity useless since exceedingly rare */
    2120             : static int
    2121      980551 : already_known(RELCACHE_t *cache, long bs, GEN cols)
    2122             : {
    2123             :   REL_t *r;
    2124      980551 :   long l = lg(cols);
    2125    61639648 :   for (r = cache->last; r > cache->base; r--)
    2126    60798335 :     if (bs == r->nz)
    2127             :     {
    2128     3050167 :       GEN coll = r->R;
    2129     3050167 :       long b = bs;
    2130     3050167 :       while (b < l && cols[b] == coll[b]) b++;
    2131     3050167 :       if (b == l) return 1;
    2132             :     }
    2133      841313 :   return 0;
    2134             : }
    2135             : 
    2136             : /* Add relation R to cache, nz = index of first non zero coeff in R.
    2137             :  * If relation is a linear combination of the previous ones, return 0.
    2138             :  * Otherwise, update basis and return > 0. Compute mod p (much faster)
    2139             :  * so some kernel vector might not be genuine. */
    2140             : static int
    2141      980719 : add_rel_i(RELCACHE_t *cache, GEN R, long nz, GEN m, long orig, long aut, REL_t **relp, long in_rnd_rel)
    2142             : {
    2143      980719 :   long i, k, n = lg(R)-1;
    2144             : 
    2145      980719 :   if (nz == n+1) { k = 0; goto ADD_REL; }
    2146      980551 :   if (already_known(cache, nz, R)) return -1;
    2147      841313 :   if (cache->last >= cache->base + cache->len) return 0;
    2148      841313 :   if (DEBUGLEVEL>6)
    2149             :   {
    2150           0 :     err_printf("adding vector = %Ps\n",R);
    2151           0 :     err_printf("generators =\n%Ps\n", cache->basis);
    2152             :   }
    2153      841313 :   if (cache->missing)
    2154             :   {
    2155      787391 :     GEN a = leafcopy(R), basis = cache->basis;
    2156      787391 :     k = lg(a);
    2157    34628453 :     do --k; while (!a[k]);
    2158     3353665 :     while (k)
    2159             :     {
    2160     1878977 :       GEN c = gel(basis, k);
    2161     1878977 :       if (c[k])
    2162             :       {
    2163     1778883 :         long ak = a[k];
    2164     1778883 :         for (i=1; i < k; i++) if (c[i]) a[i] = (a[i] + ak*(mod_p-c[i])) % mod_p;
    2165     1778883 :         a[k] = 0;
    2166    48701328 :         do --k; while (!a[k]); /* k cannot go below 0: codeword is a sentinel */
    2167             :       }
    2168             :       else
    2169             :       {
    2170      100094 :         ulong invak = Fl_inv(uel(a,k), mod_p);
    2171             :         /* Cleanup a */
    2172     4973183 :         for (i = k; i-- > 1; )
    2173             :         {
    2174     4772995 :           long j, ai = a[i];
    2175     4772995 :           c = gel(basis, i);
    2176     4772995 :           if (!ai || !c[i]) continue;
    2177       73623 :           ai = mod_p-ai;
    2178       73623 :           for (j = 1; j < i; j++) if (c[j]) a[j] = (a[j] + ai*c[j]) % mod_p;
    2179       73623 :           a[i] = 0;
    2180             :         }
    2181             :         /* Insert a/a[k] as k-th column */
    2182      100094 :         c = gel(basis, k);
    2183      100094 :         for (i = 1; i<k; i++) if (a[i]) c[i] = (a[i] * invak) % mod_p;
    2184      100094 :         c[k] = 1; a = c;
    2185             :         /* Cleanup above k */
    2186     4786568 :         for (i = k+1; i<n; i++)
    2187             :         {
    2188             :           long j, ck;
    2189     4686474 :           c = gel(basis, i);
    2190     4686474 :           ck = c[k];
    2191     4686474 :           if (!ck) continue;
    2192      639026 :           ck = mod_p-ck;
    2193      639026 :           for (j = 1; j < k; j++) if (a[j]) c[j] = (c[j] + ck*a[j]) % mod_p;
    2194      639026 :           c[k] = 0;
    2195             :         }
    2196      100094 :         cache->missing--;
    2197      100094 :         break;
    2198             :       }
    2199             :     }
    2200             :   }
    2201             :   else
    2202       53922 :     k = (cache->last - cache->base) + 1;
    2203      841313 :   if (k || cache->relsup > 0 || (m && in_rnd_rel))
    2204             :   {
    2205             :     REL_t *rel;
    2206             : 
    2207             : ADD_REL:
    2208      175541 :     rel = ++cache->last;
    2209      175541 :     if (!k && cache->relsup && nz < n+1)
    2210             :     {
    2211       20979 :       cache->relsup--;
    2212       20979 :       k = (rel - cache->base) + cache->missing;
    2213             :     }
    2214      175541 :     rel->R  = gclone(R);
    2215      175541 :     rel->m  =  m ? gclone(m) : NULL;
    2216      175541 :     rel->nz = nz;
    2217      175541 :     if (aut)
    2218             :     {
    2219       62618 :       rel->relorig = (rel - cache->base) - orig;
    2220       62618 :       rel->relaut = aut;
    2221             :     }
    2222             :     else
    2223      112923 :       rel->relaut = 0;
    2224      175541 :     if (relp) *relp = rel;
    2225      175541 :     if (DEBUGLEVEL) dbg_newrel(cache);
    2226             :   }
    2227      841481 :   return k;
    2228             : }
    2229             : 
    2230             : static int
    2231      862942 : add_rel(RELCACHE_t *cache, FB_t *F, GEN R, long nz, GEN m, long in_rnd_rel)
    2232             : {
    2233             :   REL_t *rel;
    2234             :   long k, l, reln;
    2235      862942 :   const long lauts = lg(F->idealperm), KC = F->KC;
    2236             : 
    2237      862942 :   k = add_rel_i(cache, R, nz, m, 0, 0, &rel, in_rnd_rel);
    2238      862942 :   if (k > 0 && typ(m) != t_INT)
    2239             :   {
    2240       69768 :     GEN Rl = cgetg(KC+1, t_VECSMALL);
    2241       69768 :     reln = rel - cache->base;
    2242      187545 :     for (l = 1; l < lauts; l++)
    2243             :     {
    2244      117777 :       GEN perml = gel(F->idealperm, l);
    2245      117777 :       long i, nzl = perml[nz];
    2246             : 
    2247      117777 :       for (i = 1; i <= KC; i++) Rl[i] = 0;
    2248     7618648 :       for (i = nz; i <= KC; i++)
    2249     7500871 :         if (R[i])
    2250             :         {
    2251      393126 :           long v = perml[i];
    2252             : 
    2253      393126 :           if (v < nzl) nzl = v;
    2254      393126 :           Rl[v] = R[i];
    2255             :         }
    2256      117777 :       (void)add_rel_i(cache, Rl, nzl, NULL, reln, l, NULL, in_rnd_rel);
    2257             :     }
    2258             :   }
    2259      862942 :   return k;
    2260             : }
    2261             : 
    2262             : INLINE void
    2263    11145484 : step(GEN x, double *y, GEN inc, long k)
    2264             : {
    2265    11145484 :   if (!y[k])
    2266     6431217 :     x[k]++; /* leading coeff > 0 */
    2267             :   else
    2268             :   {
    2269     4714267 :     long i = inc[k];
    2270     4714267 :     x[k] += i;
    2271     4714267 :     inc[k] = (i > 0)? -1-i: 1-i;
    2272             :   }
    2273    11145484 : }
    2274             : 
    2275             : INLINE long
    2276     1544836 : Fincke_Pohst_ideal(RELCACHE_t *cache, FB_t *F, GEN nf, GEN M, GEN I,
    2277             :     GEN NI, FACT *fact, long Nrelid, FP_t *fp, RNDREL_t *rr, long prec,
    2278             :     long *Nsmall, long *Nfact)
    2279             : {
    2280             :   pari_sp av;
    2281     1544836 :   const long N = nf_get_degree(nf), R1 = nf_get_r1(nf);
    2282     1544836 :   GEN G = nf_get_G(nf), G0 = nf_get_roundG(nf), r, u, gx, inc, ideal;
    2283             :   double BOUND, B1, B2;
    2284     1544836 :   long j, k, skipfirst, relid=0, dependent=0, try_elt=0, try_factor=0;
    2285             : 
    2286     1544836 :   inc = const_vecsmall(N, 1);
    2287     1544836 :   u = ZM_lll(ZM_mul(G0, I), 0.99, LLL_IM);
    2288     1544836 :   ideal = ZM_mul(I,u); /* approximate T2-LLL reduction */
    2289     1544836 :   r = gaussred_from_QR(RgM_mul(G, ideal), prec); /* Cholesky for T2 | ideal */
    2290     1544836 :   if (!r) pari_err_BUG("small_norm (precision too low)");
    2291             : 
    2292     5134663 :   for (k=1; k<=N; k++)
    2293             :   {
    2294     3589827 :     fp->v[k] = gtodouble(gcoeff(r,k,k));
    2295     3589827 :     for (j=1; j<k; j++) fp->q[j][k] = gtodouble(gcoeff(r,j,k));
    2296     3589827 :     if (DEBUGLEVEL>3) err_printf("v[%ld]=%.4g ",k,fp->v[k]);
    2297             :   }
    2298     1544836 :   B1 = fp->v[1]; /* T2(ideal[1]) */
    2299     1544836 :   B2 = fp->v[2] + B1 * fp->q[1][2] * fp->q[1][2]; /* T2(ideal[2]) */
    2300     1544836 :   if (ZV_isscalar(gel(ideal,1))) /* probable */
    2301             :   {
    2302      981965 :     skipfirst = 1;
    2303      981965 :     BOUND = mindd(BMULT * B1, 2 * B2);
    2304             :   }
    2305             :   else
    2306             :   {
    2307      562871 :     BOUND = mindd(BMULT * B1, 2 * B2);
    2308      562871 :     skipfirst = 0;
    2309             :   }
    2310             :   /* BOUND at most BMULT fp->x smallest known vector */
    2311     1544836 :   if (DEBUGLEVEL>1)
    2312             :   {
    2313           0 :     if (DEBUGLEVEL>3) err_printf("\n");
    2314           0 :     err_printf("BOUND = %.4g\n",BOUND);
    2315             :   }
    2316     1544836 :   BOUND *= 1 + 1e-6;
    2317     1544836 :   k = N; fp->y[N] = fp->z[N] = 0; fp->x[N] = 0;
    2318     4963569 :   for (av = avma;; set_avma(av), step(fp->x,fp->y,inc,k))
    2319     3418733 :   {
    2320             :     GEN R;
    2321             :     long nz;
    2322             :     do
    2323             :     { /* look for primitive element of small norm, cf minim00 */
    2324     6941903 :       int fl = 0;
    2325             :       double p;
    2326     6941903 :       if (k > 1)
    2327             :       {
    2328     3523170 :         long l = k-1;
    2329     3523170 :         fp->z[l] = 0;
    2330     3523170 :         for (j=k; j<=N; j++) fp->z[l] += fp->q[l][j]*fp->x[j];
    2331     3523170 :         p = (double)fp->x[k] + fp->z[k];
    2332     3523170 :         fp->y[l] = fp->y[k] + p*p*fp->v[k];
    2333     3523170 :         if (l <= skipfirst && !fp->y[1]) fl = 1;
    2334     3523170 :         fp->x[l] = (long)floor(-fp->z[l] + 0.5);
    2335     3523170 :         k = l;
    2336             :       }
    2337     3358179 :       for(;; step(fp->x,fp->y,inc,k))
    2338             :       {
    2339    15188600 :         if (++try_elt > maxtry_ELEMENT) return 0;
    2340    10300082 :         if (!fl)
    2341             :         {
    2342     9318117 :           p = (double)fp->x[k] + fp->z[k];
    2343     9318117 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2344             : 
    2345     4368572 :           step(fp->x,fp->y,inc,k);
    2346             : 
    2347     4368572 :           p = (double)fp->x[k] + fp->z[k];
    2348     4368572 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2349             :         }
    2350     4873915 :         fl = 0; inc[k] = 1;
    2351     4873915 :         if (++k > N) return 0;
    2352             :       }
    2353     5426167 :     } while (k > 1);
    2354             : 
    2355             :     /* element complete */
    2356     6819772 :     if (zv_content(fp->x) !=1) continue; /* not primitive */
    2357     2558094 :     gx = ZM_zc_mul(ideal,fp->x);
    2358     2558094 :     if (ZV_isscalar(gx)) continue;
    2359     2540982 :     if (++try_factor > maxtry_FACT) return 0;
    2360             : 
    2361     2540968 :     if (!Nrelid)
    2362             :     {
    2363        1446 :       if (!factorgen(F,nf,I,NI,gx,fact)) continue;
    2364          15 :       return 1;
    2365             :     }
    2366     2539522 :     else if (rr)
    2367             :     {
    2368     1353263 :       if (!factorgen(F,nf,I,NI,gx,fact)) continue;
    2369        6023 :       add_to_fact(rr->jid, 1, fact);
    2370             :     }
    2371             :     else
    2372             :     {
    2373     1186259 :       GEN Nx, xembed = RgM_RgC_mul(M, gx);
    2374             :       long e;
    2375     1186259 :       if (Nsmall) (*Nsmall)++;
    2376     1186259 :       Nx = grndtoi(embed_norm(xembed, R1), &e);
    2377     1186259 :       if (e >= 0) {
    2378           0 :         if (DEBUGLEVEL > 1) err_printf("+");
    2379      376424 :         continue;
    2380             :       }
    2381     1186259 :       if (!can_factor(F, nf, NULL, gx, Nx, fact)) continue;
    2382             :     }
    2383             : 
    2384             :     /* smooth element */
    2385      815858 :     R = set_fact(F, fact, rr ? rr->ex : NULL, &nz);
    2386             :     /* make sure we get maximal rank first, then allow all relations */
    2387      815858 :     if (add_rel(cache, F, R, nz, gx, rr ? 1 : 0) <= 0)
    2388             :     { /* probably Q-dependent from previous ones: forget it */
    2389      749849 :       if (DEBUGLEVEL>1) err_printf("*");
    2390      764346 :       if (++dependent > maxtry_DEP) break;
    2391      739993 :       continue;
    2392             :     }
    2393       66009 :     dependent = 0;
    2394       66009 :     if (DEBUGLEVEL && Nfact) (*Nfact)++;
    2395       66009 :     if (cache->last >= cache->end) return 1; /* we have enough */
    2396       51435 :     if (++relid == Nrelid) break;
    2397             :   }
    2398       14497 :   return 0;
    2399             : }
    2400             : 
    2401             : static void
    2402       44963 : small_norm(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, GEN M,
    2403             :            FACT *fact, GEN p0)
    2404             : {
    2405       44963 :   const long prec = nf_get_prec(nf);
    2406             :   FP_t fp;
    2407             :   pari_sp av;
    2408       44963 :   GEN L_jid = F->L_jid, Np0;
    2409       44963 :   long Nsmall, Nfact, n = lg(L_jid);
    2410       44963 :   REL_t *last = cache->last;
    2411             :   pari_timer T;
    2412             : 
    2413       44963 :   if (DEBUGLEVEL)
    2414             :   {
    2415           0 :     timer_start(&T);
    2416           0 :     err_printf("#### Look for %ld relations in %ld ideals (small_norm)\n",
    2417           0 :                cache->end - last, lg(L_jid)-1);
    2418           0 :     if (p0) err_printf("Look in p0 = %Ps\n", vecslice(p0,1,4));
    2419             :   }
    2420       44963 :   Nsmall = Nfact = 0;
    2421       44963 :   minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2422       44963 :   Np0 = p0? pr_norm(p0): NULL;
    2423     1107250 :   for (av = avma; --n; set_avma(av))
    2424             :   {
    2425     1075345 :     long j = L_jid[n];
    2426     1075345 :     GEN id = gel(F->LP, j), Nid;
    2427     1075345 :     if (DEBUGLEVEL>1)
    2428           0 :       err_printf("\n*** Ideal no %ld: %Ps\n", j, vecslice(id,1,4));
    2429     1075345 :     if (p0)
    2430     1026191 :     { Nid = mulii(Np0, pr_norm(id)); id = idealmul(nf, p0, id); }
    2431             :     else
    2432       49154 :     { Nid = pr_norm(id); id = pr_hnf(nf, id);}
    2433     1075345 :     if (Fincke_Pohst_ideal(cache, F, nf, M, id, Nid, fact, Nrelid, &fp,
    2434       13058 :                            NULL, prec, &Nsmall, &Nfact)) break;
    2435             :   }
    2436       44963 :   if (DEBUGLEVEL && Nsmall)
    2437             :   {
    2438           0 :     if (DEBUGLEVEL == 1)
    2439           0 :     { if (Nfact) err_printf("\n"); }
    2440             :     else
    2441           0 :       err_printf("  \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
    2442           0 :                   Nfact,Nsmall,((double)Nfact)/Nsmall);
    2443           0 :     if (timer_get(&T)>1) timer_printf(&T,"small_norm");
    2444             :   }
    2445       44963 : }
    2446             : 
    2447             : static GEN
    2448       21406 : get_random_ideal(FB_t *F, GEN nf, GEN ex)
    2449             : {
    2450       21406 :   long i, l = lg(ex);
    2451             :   for (;;)
    2452           0 :   {
    2453       21406 :     GEN I = NULL;
    2454      121378 :     for (i = 1; i < l; i++)
    2455       99972 :       if ((ex[i] = random_bits(RANDOM_BITS)))
    2456             :       {
    2457       93980 :         GEN pr = gel(F->LP, F->subFB[i]), e = utoipos(ex[i]);
    2458       93980 :         I = I? idealmulpowprime(nf, I, pr, e): idealpow(nf, pr, e);
    2459             :       }
    2460       42812 :     if (I && !ZM_isscalar(I,NULL)) return I; /* != (n)Z_K */
    2461             :   }
    2462             : }
    2463             : 
    2464             : static void
    2465       21406 : rnd_rel(RELCACHE_t *cache, FB_t *F, GEN nf, FACT *fact)
    2466             : {
    2467             :   pari_timer T;
    2468       21406 :   GEN L_jid = F->L_jid, M = nf_get_M(nf), R, NR;
    2469       21406 :   long i, l = lg(L_jid), prec = nf_get_prec(nf);
    2470             :   RNDREL_t rr;
    2471             :   FP_t fp;
    2472             :   pari_sp av;
    2473             : 
    2474       21406 :   if (DEBUGLEVEL) {
    2475           0 :     timer_start(&T);
    2476           0 :     err_printf("\n#### Look for %ld relations in %ld ideals (rnd_rel)\n",
    2477           0 :                cache->end - cache->last, l-1);
    2478             :   }
    2479       21406 :   rr.ex = cgetg(lg(F->subFB), t_VECSMALL);
    2480       21406 :   R = get_random_ideal(F, nf, rr.ex); /* random product from subFB */
    2481       21406 :   NR = ZM_det_triangular(R);
    2482       21406 :   minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2483      488259 :   for (av = avma, i = 1; i < l; i++, set_avma(av))
    2484             :   { /* try P[j] * base */
    2485      468369 :     long j = L_jid[i];
    2486      468369 :     GEN P = gel(F->LP, j), Nid = mulii(NR, pr_norm(P));
    2487      468369 :     if (DEBUGLEVEL>1) err_printf("\n*** Ideal %ld: %Ps\n", j, vecslice(P,1,4));
    2488      468369 :     rr.jid = j;
    2489      468369 :     if (Fincke_Pohst_ideal(cache, F, nf, M, idealHNF_mul(nf, R, P), Nid, fact,
    2490        1516 :                            RND_REL_RELPID, &fp, &rr, prec, NULL, NULL)) break;
    2491             :   }
    2492       21406 :   if (DEBUGLEVEL)
    2493             :   {
    2494           0 :     err_printf("\n");
    2495           0 :     if (timer_get(&T) > 1) timer_printf(&T,"for remaining ideals");
    2496             :   }
    2497       21406 : }
    2498             : 
    2499             : static GEN
    2500       11816 : automorphism_perms(GEN M, GEN auts, GEN cyclic, long r1, long r2, long N)
    2501             : {
    2502       11816 :   long L = lgcols(M), lauts = lg(auts), lcyc = lg(cyclic), i, j, l, m;
    2503       11816 :   GEN Mt, perms = cgetg(lauts, t_VEC);
    2504             :   pari_sp av;
    2505             : 
    2506       11816 :   for (l = 1; l < lauts; l++) gel(perms, l) = cgetg(L, t_VECSMALL);
    2507       11816 :   av = avma;
    2508       11816 :   Mt = shallowtrans(gprec_w(M, LOWDEFAULTPREC));
    2509       11816 :   Mt = shallowconcat(Mt, conj_i(vecslice(Mt, r1+1, r1+r2)));
    2510       24941 :   for (l = 1; l < lcyc; l++)
    2511             :   {
    2512       13125 :     GEN thiscyc = gel(cyclic, l), thisperm, perm, prev, Nt;
    2513       13125 :     long k = thiscyc[1];
    2514             : 
    2515       13125 :     Nt = RgM_mul(shallowtrans(gel(auts, k)), Mt);
    2516       13125 :     perm = gel(perms, k);
    2517       36862 :     for (i = 1; i < L; i++)
    2518             :     {
    2519       23737 :       GEN v = gel(Nt, i), minD;
    2520       23737 :       minD = gnorml2(gsub(v, gel(Mt, 1)));
    2521       23737 :       perm[i] = 1;
    2522      116606 :       for (j = 2; j <= N; j++)
    2523             :       {
    2524       92869 :         GEN D = gnorml2(gsub(v, gel(Mt, j)));
    2525       92869 :         if (gcmp(D, minD) < 0) { minD = D; perm[i] = j >= L ? r2-j : j; }
    2526             :       }
    2527             :     }
    2528       14560 :     for (prev = perm, m = 2; m < lg(thiscyc); m++, prev = thisperm)
    2529             :     {
    2530        1435 :       thisperm = gel(perms, thiscyc[m]);
    2531        9688 :       for (i = 1; i < L; i++)
    2532             :       {
    2533        8253 :         long pp = labs(prev[i]);
    2534        8253 :         thisperm[i] = prev[i] < 0 ? -perm[pp] : perm[pp];
    2535             :       }
    2536             :     }
    2537             :   }
    2538       11816 :   set_avma(av); return perms;
    2539             : }
    2540             : 
    2541             : /* Determine the field automorphisms as matrices on the integral basis */
    2542             : static GEN
    2543       11872 : automorphism_matrices(GEN nf, GEN *cycp)
    2544             : {
    2545       11872 :   pari_sp av = avma;
    2546       11872 :   GEN auts = galoisconj(nf, NULL), mats, cyclic, cyclicidx;
    2547       11872 :   long nauts = lg(auts)-1, i, j, k, l;
    2548             : 
    2549       11872 :   cyclic = cgetg(nauts+1, t_VEC);
    2550       11872 :   cyclicidx = zero_Flv(nauts);
    2551       12348 :   for (l = 1; l <= nauts; l++)
    2552             :   {
    2553       12348 :     GEN aut = gel(auts, l);
    2554       12348 :     if (gequalX(aut)) { swap(gel(auts, l), gel(auts, nauts)); break; }
    2555             :   }
    2556             :   /* trivial automorphism is last */
    2557       11872 :   for (l = 1; l <= nauts; l++) gel(auts, l) = algtobasis(nf, gel(auts, l));
    2558             :   /* Compute maximal cyclic subgroups */
    2559       38185 :   for (l = nauts; --l > 0; ) if (!cyclicidx[l])
    2560             :   {
    2561       13314 :     GEN elt = gel(auts, l), aut = elt, cyc = cgetg(nauts+1, t_VECSMALL);
    2562       13314 :     cyc[1] = cyclicidx[l] = l; j = 1;
    2563             :     do
    2564             :     {
    2565       14791 :       elt = galoisapply(nf, elt, aut);
    2566       14791 :       for (k = 1; k <= nauts; k++) if (gequal(elt, gel(auts, k))) break;
    2567       14791 :       cyclicidx[k] = l; cyc[++j] = k;
    2568             :     }
    2569       14791 :     while (k != nauts);
    2570       13314 :     setlg(cyc, j);
    2571       13314 :     gel(cyclic, l) = cyc;
    2572             :   }
    2573       26313 :   for (i = j = 1; i < nauts; i++)
    2574       14441 :     if (cyclicidx[i] == i) cyclic[j++] = cyclic[i];
    2575       11872 :   setlg(cyclic, j);
    2576       11872 :   mats = cgetg(nauts, t_VEC);
    2577       36897 :   while (--j > 0)
    2578             :   {
    2579       13153 :     GEN cyc = gel(cyclic, j);
    2580       13153 :     long id = cyc[1];
    2581       13153 :     GEN M, Mi, aut = gel(auts, id);
    2582             : 
    2583       13153 :     gel(mats, id) = Mi = M = nfgaloismatrix(nf, aut);
    2584       13153 :     for (i = 2; i < lg(cyc); i++) gel(mats, cyc[i]) = Mi = ZM_mul(Mi, M);
    2585             :   }
    2586       11872 :   gerepileall(av, 2, &mats, &cyclic);
    2587       11872 :   if (cycp) *cycp = cyclic;
    2588       11872 :   return mats;
    2589             : }
    2590             : 
    2591             : /* vP a list of maximal ideals above the same p from idealprimedec: f(P/p) is
    2592             :  * increasing; 1 <= j <= #vP; orbit a zc of length <= #vP; auts a vector of
    2593             :  * automorphisms in ZM form.
    2594             :  * Set orbit[i] = 1 for all vP[i], i >= j, in the orbit of pr = vP[j] wrt auts.
    2595             :  * N.B.1 orbit need not be initialized to 0: useful to incrementally run
    2596             :  * through successive orbits
    2597             :  * N.B.2 i >= j, so primes with index < j will be missed; run incrementally
    2598             :  * starting from j = 1 ! */
    2599             : static void
    2600       11887 : pr_orbit_fill(GEN orbit, GEN auts, GEN vP, long j)
    2601             : {
    2602       11887 :   GEN pr = gel(vP,j), gen = pr_get_gen(pr);
    2603       11887 :   long i, l = lg(auts), J = lg(orbit), f = pr_get_f(pr);
    2604       11887 :   orbit[j] = 1;
    2605       23774 :   for (i = 1; i < l; i++)
    2606             :   {
    2607       11887 :     GEN g = ZM_ZC_mul(gel(auts,i), gen);
    2608             :     long k;
    2609       11894 :     for (k = j+1; k < J; k++)
    2610             :     {
    2611          21 :       GEN prk = gel(vP,k);
    2612          21 :       if (pr_get_f(prk) > f) break; /* f(P[k]) increases with k */
    2613             :       /* don't check that e matches: (almost) always 1 ! */
    2614          21 :       if (!orbit[k] && ZC_prdvd(g, prk)) { orbit[k] = 1; break; }
    2615             :     }
    2616             :   }
    2617       11887 : }
    2618             : /* remark: F->KCZ changes if be_honest() fails */
    2619             : static int
    2620          28 : be_honest(FB_t *F, GEN nf, GEN auts, FACT *fact)
    2621             : {
    2622             :   long i, iz, nbtest;
    2623          28 :   long lgsub = lg(F->subFB), KCZ0 = F->KCZ;
    2624          28 :   long N = nf_get_degree(nf), prec = nf_get_prec(nf);
    2625          28 :   GEN M = nf_get_M(nf);
    2626             :   FP_t fp;
    2627             :   pari_sp av;
    2628             : 
    2629          28 :   if (DEBUGLEVEL) {
    2630           0 :     err_printf("Be honest for %ld primes from %ld to %ld\n", F->KCZ2 - F->KCZ,
    2631           0 :                F->FB[ F->KCZ+1 ], F->FB[ F->KCZ2 ]);
    2632             :   }
    2633          28 :   minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2634          28 :   if (lg(auts) == 1) auts = NULL;
    2635          28 :   av = avma;
    2636          36 :   for (iz=F->KCZ+1; iz<=F->KCZ2; iz++, set_avma(av))
    2637             :   {
    2638          29 :     long p = F->FB[iz];
    2639          29 :     GEN pr_orbit, P = F->LV[p];
    2640          29 :     long j, J = lg(P); /* > 1 */
    2641             :     /* the P|p, NP > C2 are assumed in subgroup generated by FB + last P
    2642             :      * with NP <= C2 is unramified --> check all but last */
    2643          29 :     if (pr_get_e(gel(P,J-1)) == 1) J--;
    2644          29 :     if (J == 1) continue;
    2645          29 :     if (DEBUGLEVEL>1) err_printf("%ld ", p);
    2646          29 :     pr_orbit = auts? zero_zv(J-1): NULL;
    2647          51 :     for (j = 1; j < J; j++)
    2648             :     {
    2649             :       GEN Nid, id, id0;
    2650          43 :       if (pr_orbit)
    2651             :       {
    2652          43 :         if (pr_orbit[j]) continue;
    2653             :         /* discard all primes in automorphism orbit simultaneously */
    2654          36 :         pr_orbit_fill(pr_orbit, auts, P, j);
    2655             :       }
    2656          36 :       id = id0 = pr_hnf(nf,gel(P,j));
    2657          36 :       Nid = pr_norm(gel(P,j));
    2658          36 :       for (nbtest=0;;)
    2659             :       {
    2660        2208 :         if (Fincke_Pohst_ideal(NULL, F, nf, M, id, Nid, fact, 0, &fp,
    2661          15 :                                NULL, prec, NULL, NULL)) break;
    2662        1107 :         if (++nbtest > maxtry_HONEST)
    2663             :         {
    2664          21 :           if (DEBUGLEVEL)
    2665           0 :             pari_warn(warner,"be_honest() failure on prime %Ps\n", gel(P,j));
    2666          21 :           return 0;
    2667             :         }
    2668             :         /* occurs at most once in the whole function */
    2669        6830 :         for (i = 1, id = id0; i < lgsub; i++)
    2670             :         {
    2671        5744 :           long ex = random_bits(RANDOM_BITS);
    2672        5744 :           if (ex)
    2673             :           {
    2674        5417 :             GEN pr = gel(F->LP, F->subFB[i]);
    2675        5417 :             id = idealmulpowprime(nf, id, pr, utoipos(ex));
    2676             :           }
    2677             :         }
    2678        1086 :         if (!equali1(gcoeff(id,N,N))) id = Q_primpart(id);
    2679        1086 :         if (expi(gcoeff(id,1,1)) > 100) id = idealred(nf, id);
    2680        1086 :         Nid = ZM_det_triangular(id);
    2681             :       }
    2682             :     }
    2683           8 :     F->KCZ++; /* SUCCESS, "enlarge" factorbase */
    2684             :   }
    2685           7 :   F->KCZ = KCZ0; return gc_bool(av,1);
    2686             : }
    2687             : 
    2688             : /* all primes with N(P) <= BOUND factor on factorbase ? */
    2689             : void
    2690          56 : bnftestprimes(GEN bnf, GEN BOUND)
    2691             : {
    2692          56 :   pari_sp av0 = avma, av;
    2693          56 :   ulong count = 0;
    2694          56 :   GEN auts, p, nf = bnf_get_nf(bnf), Vbase = bnf_get_vbase(bnf);
    2695          56 :   GEN fb = gen_sort(Vbase, (void*)&cmp_prime_ideal, cmp_nodata); /*tablesearch*/
    2696          56 :   ulong pmax = pr_get_smallp(gel(fb, lg(fb)-1)); /*largest p in factorbase*/
    2697             :   forprime_t S;
    2698             :   FACT *fact;
    2699             :   FB_t F;
    2700             : 
    2701          56 :   (void)recover_partFB(&F, Vbase, nf_get_degree(nf));
    2702          56 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    2703          56 :   forprime_init(&S, gen_2, BOUND);
    2704          56 :   auts = automorphism_matrices(nf, NULL);
    2705          56 :   if (lg(auts) == 1) auts = NULL;
    2706          56 :   av = avma;
    2707       37282 :   while (( p = forprime_next(&S) ))
    2708             :   {
    2709             :     GEN pr_orbit, vP;
    2710             :     long j, J;
    2711             : 
    2712       37170 :     if (DEBUGLEVEL == 1 && ++count > 1000)
    2713             :     {
    2714           0 :       err_printf("passing p = %Ps / %Ps\n", p, BOUND);
    2715           0 :       count = 0;
    2716             :     }
    2717       37170 :     set_avma(av);
    2718       37170 :     vP = idealprimedec_limit_norm(bnf, p, BOUND);
    2719       37170 :     J = lg(vP);
    2720             :     /* if last is unramified, all P|p in subgroup generated by FB: skip last */
    2721       37170 :     if (J > 1 && pr_get_e(gel(vP,J-1)) == 1) J--;
    2722       37170 :     if (J == 1) continue;
    2723       14448 :     if (DEBUGLEVEL>1) err_printf("*** p = %Ps\n",p);
    2724       14448 :     pr_orbit = auts? zero_zv(J-1): NULL;
    2725       31353 :     for (j = 1; j < J; j++)
    2726             :     {
    2727       16905 :       GEN P = gel(vP,j);
    2728       16905 :       long k = 0;
    2729       16905 :       if (pr_orbit)
    2730             :       {
    2731       11858 :         if (pr_orbit[j]) continue;
    2732             :         /* discard all primes in automorphism orbit simultaneously */
    2733       11851 :         pr_orbit_fill(pr_orbit, auts, vP, j);
    2734             :       }
    2735       16898 :       if (abscmpiu(p, pmax) > 0 || !(k = tablesearch(fb, P, &cmp_prime_ideal)))
    2736       16338 :         (void)SPLIT(&F, nf, pr_hnf(nf,P), Vbase, fact);
    2737       16898 :       if (DEBUGLEVEL>1)
    2738             :       {
    2739           0 :         err_printf("  Testing P = %Ps\n",P);
    2740           0 :         if (k) err_printf("    #%ld in factor base\n",k);
    2741           0 :         else err_printf("    is %Ps\n", isprincipal(bnf,P));
    2742             :       }
    2743             :     }
    2744             :   }
    2745          56 :   set_avma(av0);
    2746          56 : }
    2747             : 
    2748             : /* A t_MAT of complex floats, in fact reals. Extract a submatrix B
    2749             :  * whose columns are definitely non-0, i.e. gexpo(A[j]) >= -2
    2750             :  *
    2751             :  * If possible precision problem (t_REAL 0 with large exponent), set
    2752             :  * *precpb to 1 */
    2753             : static GEN
    2754       13132 : clean_cols(GEN A, int *precpb)
    2755             : {
    2756       13132 :   long l = lg(A), h, i, j, k;
    2757             :   GEN B;
    2758       13132 :   *precpb = 0;
    2759       13132 :   if (l == 1) return A;
    2760       13132 :   h = lgcols(A);;
    2761       13132 :   B = cgetg(l, t_MAT);
    2762      243955 :   for (i = k = 1; i < l; i++)
    2763             :   {
    2764      230823 :     GEN Ai = gel(A,i);
    2765      230823 :     int non0 = 0;
    2766     1118528 :     for (j = 1; j < h; j++)
    2767             :     {
    2768      887705 :       GEN c = gel(Ai,j);
    2769      887705 :       if (gexpo(c) >= -2)
    2770             :       {
    2771      527664 :         if (gequal0(c)) *precpb = 1; else non0 = 1;
    2772             :       }
    2773             :     }
    2774      230823 :     if (non0) gel(B, k++) = Ai;
    2775             :   }
    2776       13132 :   setlg(B, k); return B;
    2777             : }
    2778             : 
    2779             : static long
    2780      114304 : compute_multiple_of_R_pivot(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2781             : {
    2782      114304 :   GEN x = gel(X,ix);
    2783      114304 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2784             :   (void)x0;
    2785      685427 :   for (i=1; i<lx; i++)
    2786      571123 :     if (!c[i] && !gequal0(gel(x,i)))
    2787             :     {
    2788      168042 :       long e = gexpo(gel(x,i));
    2789      168042 :       if (e > ex) { ex = e; k = i; }
    2790             :     }
    2791      114304 :   return (k && ex > -32)? k: lx;
    2792             : }
    2793             : 
    2794             : /* A = complex logarithmic embeddings of units (u_j) found so far,
    2795             :  * RU = R1+R2 = unit rank, N = field degree
    2796             :  * need = unit rank defect
    2797             :  * L = NULL (prec problem) or B^(-1) * A with approximate rational entries
    2798             :  * (as t_REAL), B a submatrix of A, with (probably) maximal rank RU */
    2799             : static GEN
    2800       21000 : compute_multiple_of_R(GEN A, long RU, long N, long *pneed, long *bit, GEN *ptL)
    2801             : {
    2802             :   GEN T, d, mdet, Im_mdet, kR, xreal, L;
    2803       21000 :   long i, j, r, R1 = 2*RU - N;
    2804             :   int precpb;
    2805       21000 :   pari_sp av = avma;
    2806             : 
    2807       21000 :   if (RU == 1) { *ptL = zeromat(0, lg(A)-1); return gen_1; }
    2808             : 
    2809       13132 :   if (DEBUGLEVEL) err_printf("\n#### Computing regulator multiple\n");
    2810       13132 :   xreal = real_i(A); /* = (log |sigma_i(u_j)|) */
    2811       13132 :   mdet = clean_cols(xreal, &precpb);
    2812             :   /* will cause precision to increase on later failure, but we may succeed! */
    2813       13132 :   *ptL = precpb? NULL: gen_1;
    2814       13132 :   T = cgetg(RU+1,t_COL);
    2815       13132 :   for (i=1; i<=R1; i++) gel(T,i) = gen_1;
    2816       13132 :   for (   ; i<=RU; i++) gel(T,i) = gen_2;
    2817       13132 :   mdet = shallowconcat(T, mdet); /* det(Span(mdet)) = N * R */
    2818             : 
    2819             :   /* could be using indexrank(), but need custom "get_pivot" function */
    2820       13132 :   d = RgM_pivots(mdet, NULL, &r, &compute_multiple_of_R_pivot);
    2821             :   /* # of independent columns == unit rank ? */
    2822       13132 :   if (lg(mdet)-1 - r != RU)
    2823             :   {
    2824        6310 :     if (DEBUGLEVEL)
    2825           0 :       err_printf("Unit group rank = %ld < %ld\n",lg(mdet)-1 - r, RU);
    2826        6310 :     *pneed = RU - (lg(mdet)-1-r); return gc_NULL(av);
    2827             :   }
    2828             : 
    2829        6822 :   Im_mdet = cgetg(RU+1, t_MAT); /* extract independent columns */
    2830             :   /* N.B: d[1] = 1, corresponding to T above */
    2831        6822 :   gel(Im_mdet, 1) = T;
    2832       39722 :   for (i = j = 2; i <= RU; j++)
    2833       32900 :     if (d[j]) gel(Im_mdet, i++) = gel(mdet,j);
    2834             : 
    2835             :   /* integral multiple of R: the cols we picked form a Q-basis, they have an
    2836             :    * index in the full lattice. First column is T */
    2837        6822 :   kR = divru(det2(Im_mdet), N);
    2838             :   /* R > 0.2 uniformly */
    2839        6822 :   if (!signe(kR) || expo(kR) < -3)
    2840             :   {
    2841           1 :     if (DEBUGLEVEL) err_printf("Regulator is zero.\n");
    2842           1 :     *pneed = 0; return gc_NULL(av);
    2843             :   }
    2844        6821 :   setabssign(kR); L = RgM_inv(Im_mdet);
    2845             :   /* estimate # of correct bits in result */
    2846        6821 :   if (!L || (*bit = - gexpo(RgM_Rg_sub(RgM_mul(L,Im_mdet), gen_1))) < 16)
    2847           1 :   { *ptL = NULL; return gerepilecopy(av,kR); }
    2848             : 
    2849        6820 :   L = RgM_mul(rowslice(L,2,RU), xreal); /* approximate rational entries */
    2850        6820 :   gerepileall(av,2, &L, &kR);
    2851        6820 :   *ptL = L; return kR;
    2852             : }
    2853             : 
    2854             : /* leave small integer n as is, convert huge n to t_REAL (for readability) */
    2855             : static GEN
    2856           0 : i2print(GEN n)
    2857           0 : { return lgefint(n) <= DEFAULTPREC? n: itor(n,LOWDEFAULTPREC); }
    2858             : 
    2859             : static long
    2860       14675 : bad_check(GEN c)
    2861             : {
    2862       14675 :   long ec = gexpo(c);
    2863       14675 :   if (DEBUGLEVEL) err_printf("\n ***** check = %.28Pg\n",c);
    2864             :   /* safe check for c < 0.75 : avoid underflow in gtodouble() */
    2865       14675 :   if (ec < -1 || (ec == -1 && gtodouble(c) < 0.75)) return fupb_PRECI;
    2866             :   /* safe check for c > 1.3 : avoid overflow */
    2867       14675 :   if (ec > 0 || (ec == 0 && gtodouble(c) > 1.3)) return fupb_RELAT;
    2868       11837 :   return fupb_NONE;
    2869             : }
    2870             : /* Input:
    2871             :  * lambda = approximate rational entries: coords of units found so far on a
    2872             :  * sublattice of maximal rank (sublambda)
    2873             :  * *ptkR = regulator of sublambda = multiple of regulator of lambda
    2874             :  * Compute R = true regulator of lambda.
    2875             :  *
    2876             :  * If c := Rz ~ 1, by Dirichlet's formula, then lambda is the full group of
    2877             :  * units AND the full set of relations for the class group has been computed.
    2878             :  *
    2879             :  * In fact z is a very rough approximation and we only expect 0.75 < Rz < 1.3
    2880             :  * bit is an estimate for the actual accuracy of lambda
    2881             :  *
    2882             :  * Output: *ptkR = R, *ptU = basis of fundamental units (in terms lambda) */
    2883             : static long
    2884       14688 : compute_R(GEN lambda, GEN z, long bit, GEN *ptL, GEN *ptkR)
    2885             : {
    2886       14688 :   pari_sp av = avma;
    2887       14688 :   long r, reason, RU = lg(lambda) == 1? 1: lgcols(lambda);
    2888             :   GEN L, H, D, den, R, c;
    2889             : 
    2890       14688 :   *ptL = NULL;
    2891       14688 :   if (DEBUGLEVEL) err_printf("\n#### Computing check\n");
    2892       14688 :   if (RU == 1) { *ptkR = gen_1; *ptL = lambda; return bad_check(z); }
    2893        6820 :   D = gmul2n(mpmul(*ptkR,z), 1); /* bound for denom(lambda) */
    2894        6820 :   if (expo(D) < 0 && rtodbl(D) < 0.95) return fupb_PRECI;
    2895        6820 :   lambda = bestappr(lambda,D);
    2896        6820 :   if (lg(lambda) == 1)
    2897             :   {
    2898           0 :     if (DEBUGLEVEL) err_printf("truncation error in bestappr\n");
    2899           0 :     return fupb_PRECI;
    2900             :   }
    2901        6820 :   den = Q_denom(lambda);
    2902        6820 :   if (mpcmp(den,D) > 0)
    2903             :   {
    2904          12 :     if (DEBUGLEVEL) err_printf("D = %Ps\nden = %Ps\n",D, i2print(den));
    2905          12 :     return fupb_PRECI;
    2906             :   }
    2907        6808 :   L = Q_muli_to_int(lambda, den);
    2908        6808 :   if (bit > 0)
    2909             :   {
    2910        4123 :     if (lg(L) > 1)
    2911             :     {
    2912        4123 :       if (RU > 5) bit -= 64;
    2913        3962 :       else if (RU > 3) bit -= 32;
    2914             :     }
    2915        4123 :     if (gexpo(L) + expi(den) > bit)
    2916             :     {
    2917           1 :       if (DEBUGLEVEL) err_printf("dubious bestappr; den = %Ps\n", i2print(den));
    2918           1 :       return fupb_PRECI;
    2919             :     }
    2920             :   }
    2921        6807 :   H = ZM_hnf(L); r = lg(H)-1;
    2922        6807 :   if (!r || r != nbrows(H))
    2923           0 :     R = gen_0; /* wrong rank */
    2924             :   else
    2925        6807 :     R = gmul(*ptkR, gdiv(ZM_det_triangular(H), powiu(den, r)));
    2926             :   /* R = tentative regulator; regulator > 0.2 uniformly */
    2927        6807 :   if (gexpo(R) < -3) {
    2928           0 :     if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    2929           0 :     return gc_long(av, fupb_PRECI);
    2930             :   }
    2931        6807 :   c = gmul(R,z); /* should be n (= 1 if we are done) */
    2932        6807 :   if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    2933        6807 :   if ((reason = bad_check(c))) return gc_long(av, reason);
    2934        4130 :   *ptkR = R; *ptL = L; return fupb_NONE;
    2935             : }
    2936             : static GEN
    2937       11907 : get_clg2(GEN cyc, GEN Ga, GEN C, GEN Ur, GEN Ge, GEN M1, GEN M2)
    2938             : {
    2939       11907 :   GEN GD = gsub(act_arch(M1, C), diagact_arch(cyc, Ga));
    2940       11907 :   GEN ga = gsub(act_arch(M2, C), act_arch(Ur, Ga));
    2941       11907 :   return mkvecn(6, Ur, ga, GD, Ge, M1, M2);
    2942             : }
    2943             : /* compute class group (clg1) + data for isprincipal (clg2) */
    2944             : static GEN
    2945       11816 : class_group_gen(GEN nf,GEN W,GEN C,GEN Vbase,long prec, GEN *pclg2)
    2946             : {
    2947             :   GEN M1, M2, z, G, Ga, Ge, cyc, X, Y, D, U, V, Ur, Ui, Uir;
    2948             :   long j, l;
    2949             : 
    2950       11816 :   D = ZM_snfall(W,&U,&V); /* UWV=D, D diagonal, G = g Ui (G=new gens, g=old) */
    2951       11816 :   Ui = ZM_inv(U, NULL);
    2952       11816 :   l = lg(D); cyc = cgetg(l, t_VEC); /* elementary divisors */
    2953       21259 :   for (j = 1; j < l; j++)
    2954             :   {
    2955       10276 :     gel(cyc,j) = gcoeff(D,j,j); /* strip useless components */
    2956       10276 :     if (is_pm1(gel(cyc,j))) break;
    2957             :   }
    2958       11816 :   l = j;
    2959       11816 :   Ur  = ZM_hnfdivrem(U, D, &Y);
    2960       11816 :   Uir = ZM_hnfdivrem(Ui,W, &X);
    2961             :  /* {x} = logarithmic embedding of x (arch. component)
    2962             :   * NB: [J,z] = idealred(I) --> I = y J, with {y} = - z
    2963             :   * G = g Uir - {Ga},  Uir = Ui + WX
    2964             :   * g = G Ur  - {ga},  Ur  = U + DY */
    2965       11816 :   G = cgetg(l,t_VEC);
    2966       11816 :   Ga= cgetg(l,t_MAT);
    2967       11816 :   Ge= cgetg(l,t_COL);
    2968       11816 :   z = init_famat(NULL);
    2969       21259 :   for (j = 1; j < l; j++)
    2970             :   {
    2971        9443 :     GEN I = genback(z, nf, Vbase, gel(Uir,j));
    2972        9443 :     gel(G,j) = gel(I,1); /* generator, order cyc[j] */
    2973        9443 :     gel(Ge,j)= gel(I,2);
    2974        9443 :     gel(Ga,j)= nf_cxlog(nf, gel(I,2), prec);
    2975        9443 :     if (!gel(Ga,j)) pari_err_PREC("class_group_gen");
    2976             :   }
    2977             :   /* {ga} = {GD}Y + G U - g = {GD}Y - {Ga} U + gW X U
    2978             :                             = gW (X Ur + V Y) - {Ga}Ur */
    2979       11816 :   M2 = ZM_add(ZM_mul(X,Ur), ZM_mul(V,Y));
    2980       11816 :   setlg(cyc,l); setlg(V,l); setlg(D,l);
    2981             :   /* G D =: {GD} = g (Ui + W X) D - {Ga}D = g W (V + X D) - {Ga}D
    2982             :    * NB: Ui D = W V. gW is given by (first l-1 cols of) C */
    2983       11816 :   M1 = ZM_add(V, ZM_mul(X,D));
    2984       11816 :   *pclg2 = get_clg2(cyc, Ga, C, Ur, Ge, M1, M2);
    2985       11816 :   return mkvec3(ZV_prod(cyc), cyc, G);
    2986             : }
    2987             : 
    2988             : /* compute principal ideals corresponding to (gen[i]^cyc[i]) */
    2989             : static GEN
    2990        3304 : makecycgen(GEN bnf)
    2991             : {
    2992             :   GEN cyc, gen, h, nf, y, GD;
    2993             :   long e,i,l;
    2994             : 
    2995        3304 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building cycgen)");
    2996        3304 :   nf = bnf_get_nf(bnf);
    2997        3304 :   cyc = bnf_get_cyc(bnf);
    2998        3304 :   gen = bnf_get_gen(bnf);
    2999        3304 :   GD = bnf_get_GD(bnf);
    3000        3304 :   h = cgetg_copy(gen, &l);
    3001        6594 :   for (i = 1; i < l; i++)
    3002             :   {
    3003        3290 :     GEN gi = gel(gen,i), ci = gel(cyc,i);
    3004        3290 :     if (abscmpiu(ci, 5) < 0)
    3005             :     {
    3006        2401 :       GEN N = ZM_det_triangular(gi);
    3007        2401 :       y = isprincipalarch(bnf,gel(GD,i), N, ci, gen_1, &e);
    3008        2401 :       if (y && fact_ok(nf,y,NULL,mkvec(gi),mkvec(ci)))
    3009             :       {
    3010        2394 :         gel(h,i) = to_famat_shallow(y,gen_1);
    3011        2394 :         continue;
    3012             :       }
    3013             :     }
    3014         896 :     y = isprincipalfact(bnf, NULL, mkvec(gi), mkvec(ci), nf_GENMAT|nf_FORCE);
    3015         896 :     gel(h,i) = gel(y,2);
    3016             :   }
    3017        3304 :   return h;
    3018             : }
    3019             : 
    3020             : static GEN
    3021          21 : get_y(GEN bnf, GEN W, GEN B, GEN C, GEN pFB, long j)
    3022             : {
    3023          21 :   GEN y, nf  = bnf_get_nf(bnf);
    3024          21 :   long e, lW = lg(W)-1;
    3025          21 :   GEN ex = (j<=lW)? gel(W,j): gel(B,j-lW);
    3026          21 :   GEN P = (j<=lW)? NULL: gel(pFB,j);
    3027          21 :   if (C)
    3028             :   { /* archimedean embeddings known: cheap trial */
    3029          21 :     GEN Nx = get_norm_fact_primes(pFB, ex, P);
    3030          21 :     y = isprincipalarch(bnf,gel(C,j), Nx,gen_1, gen_1, &e);
    3031          21 :     if (y && fact_ok(nf,y,P,pFB,ex)) return y;
    3032             :   }
    3033           0 :   y = isprincipalfact_or_fail(bnf, P, pFB, ex);
    3034           0 :   return typ(y) == t_INT? y: gel(y,2);
    3035             : }
    3036             : /* compute principal ideals corresponding to bnf relations */
    3037             : static GEN
    3038          14 : makematal(GEN bnf)
    3039             : {
    3040          14 :   GEN W = bnf_get_W(bnf), B = bnf_get_B(bnf), C = bnf_get_C(bnf);
    3041             :   GEN pFB, ma, retry;
    3042          14 :   long lma, j, prec = 0;
    3043             : 
    3044          14 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building matal)");
    3045          14 :   lma=lg(W)+lg(B)-1;
    3046          14 :   pFB = bnf_get_vbase(bnf);
    3047          14 :   ma = cgetg(lma,t_VEC);
    3048          14 :   retry = vecsmalltrunc_init(lma);
    3049          35 :   for (j=lma-1; j>0; j--)
    3050             :   {
    3051          21 :     pari_sp av = avma;
    3052          21 :     GEN y = get_y(bnf, W, B, C, pFB, j);
    3053          21 :     if (typ(y) == t_INT)
    3054             :     {
    3055           0 :       long E = itos(y);
    3056           0 :       if (DEBUGLEVEL>1) err_printf("\n%ld done later at prec %ld\n",j,E);
    3057           0 :       set_avma(av);
    3058           0 :       vecsmalltrunc_append(retry, j);
    3059           0 :       if (E > prec) prec = E;
    3060             :     }
    3061             :     else
    3062             :     {
    3063          21 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3064          21 :       gel(ma,j) = gerepileupto(av,y);
    3065             :     }
    3066             :   }
    3067          14 :   if (prec)
    3068             :   {
    3069           0 :     long k, l = lg(retry);
    3070           0 :     GEN y, nf = bnf_get_nf(bnf);
    3071           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"makematal",prec);
    3072           0 :     nf = nfnewprec_shallow(nf,prec);
    3073           0 :     bnf = Buchall(nf, nf_FORCE, prec);
    3074           0 :     if (DEBUGLEVEL) err_printf("makematal, adding missing entries:");
    3075           0 :     for (k=1; k<l; k++)
    3076             :     {
    3077           0 :       pari_sp av = avma;
    3078           0 :       long j = retry[k];
    3079           0 :       y = get_y(bnf,W,B,NULL, pFB, j);
    3080           0 :       if (typ(y) == t_INT) pari_err_PREC("makematal");
    3081           0 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3082           0 :       gel(ma,j) = gerepileupto(av,y);
    3083             :     }
    3084             :   }
    3085          14 :   if (DEBUGLEVEL>1) err_printf("\n");
    3086          14 :   return ma;
    3087             : }
    3088             : 
    3089             : enum { MATAL = 1, CYCGEN, UNITS };
    3090             : GEN
    3091       15120 : bnf_build_cycgen(GEN bnf)
    3092       15120 : { return obj_checkbuild(bnf, CYCGEN, &makecycgen); }
    3093             : GEN
    3094          14 : bnf_build_matalpha(GEN bnf)
    3095          14 : { return obj_checkbuild(bnf, MATAL, &makematal); }
    3096             : GEN
    3097        7883 : bnf_build_units(GEN bnf)
    3098        7883 : { return obj_checkbuild(bnf, UNITS, &makeunits); }
    3099             : 
    3100             : /* return fu in compact form if available; in terms of a fixed basis
    3101             :  * of S-units */
    3102             : GEN
    3103          14 : bnf_compactfu_mat(GEN bnf)
    3104             : {
    3105          14 :   GEN X, U, SUnits = bnf_get_sunits(bnf);
    3106          14 :   if (!SUnits) return NULL;
    3107          14 :   X = gel(SUnits,1);
    3108          14 :   U = gel(SUnits,2); ZM_remove_unused(&U, &X);
    3109          14 :   return mkvec2(X, U);
    3110             : }
    3111             : /* return fu in compact form if available; individually as famat */
    3112             : GEN
    3113         847 : bnf_compactfu(GEN bnf)
    3114             : {
    3115         847 :   GEN fu, X, U, SUnits = bnf_get_sunits(bnf);
    3116             :   long i, l;
    3117         847 :   if (!SUnits) return NULL;
    3118         833 :   X = gel(SUnits,1);
    3119         833 :   U = gel(SUnits,2); l = lg(U); fu = cgetg(l, t_VEC);
    3120        2688 :   for (i = 1; i < l; i++)
    3121        1855 :     gel(fu,i) = famat_remove_trivial(mkmat2(X, gel(U,i)));
    3122         833 :   return fu;
    3123             : }
    3124             : /* return expanded fu if available */
    3125             : GEN
    3126       39060 : bnf_has_fu(GEN bnf)
    3127             : {
    3128       39060 :   GEN fu = obj_check(bnf, UNITS);
    3129       39060 :   if (fu) return vecsplice(fu, 1);
    3130       38226 :   fu = bnf_get_fu_nocheck(bnf);
    3131       38226 :   return (typ(fu) == t_MAT)? NULL: fu;
    3132             : }
    3133             : /* return expanded fu if available; build if cheap */
    3134             : GEN
    3135       39004 : bnf_build_cheapfu(GEN bnf)
    3136             : {
    3137             :   GEN fu, SUnits;
    3138       39004 :   if ((fu = bnf_has_fu(bnf))) return fu;
    3139          92 :   if ((SUnits = bnf_get_sunits(bnf)))
    3140             :   {
    3141          92 :     pari_sp av = avma;
    3142          92 :     long e = gexpo(real_i(bnf_get_logfu(bnf)));
    3143          92 :     set_avma(av); if (e < 18) return vecsplice(bnf_build_units(bnf), 1);
    3144             :   }
    3145           0 :   return NULL;
    3146             : }
    3147             : 
    3148             : static GEN
    3149          91 : get_regulator(GEN mun)
    3150             : {
    3151          91 :   pari_sp av = avma;
    3152             :   GEN R;
    3153             : 
    3154          91 :   if (lg(mun) == 1) return gen_1;
    3155          84 :   R = det( rowslice(real_i(mun), 1, lgcols(mun)-2) );
    3156          84 :   setabssign(R); return gerepileuptoleaf(av, R);
    3157             : }
    3158             : 
    3159             : /* return corrected archimedian components for elts of x (vector)
    3160             :  * (= log(sigma_i(x)) - log(|Nx|) / [K:Q]) */
    3161             : static GEN
    3162          28 : get_archclean(GEN nf, GEN x, long prec, int units)
    3163             : {
    3164          28 :   long k, N, l = lg(x);
    3165          28 :   GEN M = cgetg(l, t_MAT);
    3166             : 
    3167          28 :   if (l == 1) return M;
    3168          14 :   N = nf_get_degree(nf);
    3169          42 :   for (k = 1; k < l; k++)
    3170             :   {
    3171          28 :     pari_sp av = avma;
    3172          28 :     GEN c = nf_cxlog(nf, gel(x,k), prec);
    3173          28 :     if (!c || (!units && !(c = cleanarch(c, N, prec)))) return NULL;
    3174          28 :     gel(M,k) = gerepilecopy(av, c);
    3175             :   }
    3176          14 :   return M;
    3177             : }
    3178             : static void
    3179          77 : Sunits_archclean(GEN nf, GEN Sunits, GEN *pmun, GEN *pC, long prec)
    3180             : {
    3181          77 :   GEN M, X = gel(Sunits,1), U = gel(Sunits,2), G = gel(Sunits,3);
    3182          77 :   long k, N = nf_get_degree(nf), l = lg(X);
    3183             : 
    3184          77 :   M = cgetg(l, t_MAT);
    3185        3290 :   for (k = 1; k < l; k++)
    3186        3213 :     if (!(gel(M,k) = nf_cxlog(nf, gel(X,k), prec))) return;
    3187          77 :   *pmun = cleanarch(RgM_mul(M, U), N, prec);
    3188          77 :   if (*pmun) *pC = cleanarch(RgM_mul(M, G), N, prec);
    3189             : }
    3190             : 
    3191             : GEN
    3192          91 : bnfnewprec_shallow(GEN bnf, long prec)
    3193             : {
    3194          91 :   GEN nf0 = bnf_get_nf(bnf), nf, v, fu, matal, y, mun, C;
    3195          91 :   GEN Sunits = bnf_get_sunits(bnf), Ur, Ga, Ge, M1, M2;
    3196          91 :   long r1, r2, prec0 = prec;
    3197             : 
    3198          91 :   nf_get_sign(nf0, &r1, &r2);
    3199          91 :   if (Sunits)
    3200             :   {
    3201          77 :     fu = matal = NULL;
    3202          77 :     prec += nbits2extraprec(gexpo(Sunits));
    3203             :   }
    3204             :   else
    3205             :   {
    3206          14 :     fu = bnf_build_units(bnf);
    3207          14 :     fu = vecslice(fu, 2, lg(fu)-1);
    3208          14 :     if (r1 + r2 > 1) {
    3209           7 :       long e = gexpo(bnf_get_logfu(bnf)) + 1 - TWOPOTBITS_IN_LONG;
    3210           7 :       if (e >= 0) prec += nbits2extraprec(e);
    3211             :     }
    3212          14 :     matal = bnf_build_matalpha(bnf);
    3213             :   }
    3214             : 
    3215          91 :   if (DEBUGLEVEL && prec0 != prec) pari_warn(warnprec,"bnfnewprec",prec);
    3216          91 :   for(C = NULL;;)
    3217           0 :   {
    3218          91 :     pari_sp av = avma;
    3219          91 :     nf = nfnewprec_shallow(nf0,prec);
    3220          91 :     if (Sunits)
    3221          77 :       Sunits_archclean(nf, Sunits, &mun, &C, prec);
    3222             :     else
    3223             :     {
    3224          14 :       mun = get_archclean(nf, fu, prec, 1);
    3225          14 :       if (mun) C = get_archclean(nf, matal, prec, 0);
    3226             :     }
    3227          91 :     if (C) break;
    3228           0 :     set_avma(av); prec = precdbl(prec);
    3229           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"bnfnewprec(extra)",prec);
    3230             :   }
    3231          91 :   y = leafcopy(bnf);
    3232          91 :   gel(y,3) = mun;
    3233          91 :   gel(y,4) = C;
    3234          91 :   gel(y,7) = nf;
    3235          91 :   gel(y,8) = v = leafcopy(gel(bnf,8));
    3236          91 :   gel(v,2) = get_regulator(mun);
    3237          91 :   v = gel(bnf,9);
    3238          91 :   if (lg(v) < 7) pari_err_TYPE("bnfnewprec [obsolete bnf format]", bnf);
    3239          91 :   Ur = gel(v,1);
    3240          91 :   Ge = gel(v,4);
    3241          91 :   Ga = nfV_cxlog(nf, Ge, prec);
    3242          91 :   M1 = gel(v,5);
    3243          91 :   M2 = gel(v,6);
    3244          91 :   gel(y,9) = get_clg2(bnf_get_cyc(bnf), Ga, C, Ur, Ge, M1, M2);
    3245          91 :   return y;
    3246             : }
    3247             : GEN
    3248          21 : bnfnewprec(GEN bnf, long prec)
    3249             : {
    3250          21 :   pari_sp av = avma;
    3251          21 :   return gerepilecopy(av, bnfnewprec_shallow(checkbnf(bnf), prec));
    3252             : }
    3253             : 
    3254             : GEN
    3255           0 : bnrnewprec_shallow(GEN bnr, long prec)
    3256             : {
    3257           0 :   GEN y = cgetg(7,t_VEC);
    3258             :   long i;
    3259           0 :   gel(y,1) = bnfnewprec_shallow(bnr_get_bnf(bnr), prec);
    3260           0 :   for (i=2; i<7; i++) gel(y,i) = gel(bnr,i);
    3261           0 :   return y;
    3262             : }
    3263             : GEN
    3264           7 : bnrnewprec(GEN bnr, long prec)
    3265             : {
    3266           7 :   GEN y = cgetg(7,t_VEC);
    3267             :   long i;
    3268           7 :   checkbnr(bnr);
    3269           7 :   gel(y,1) = bnfnewprec(bnr_get_bnf(bnr), prec);
    3270           7 :   for (i=2; i<7; i++) gel(y,i) = gcopy(gel(bnr,i));
    3271           7 :   return y;
    3272             : }
    3273             : 
    3274             : static GEN
    3275       12502 : buchall_end(GEN nf,GEN res, GEN clg2, GEN W, GEN B, GEN A, GEN C,GEN Vbase)
    3276             : {
    3277       12502 :   GEN z = obj_init(9, 3);
    3278       12502 :   gel(z,1) = W;
    3279       12502 :   gel(z,2) = B;
    3280       12502 :   gel(z,3) = A;
    3281       12502 :   gel(z,4) = C;
    3282       12502 :   gel(z,5) = Vbase;
    3283       12502 :   gel(z,6) = gen_0;
    3284       12502 :   gel(z,7) = nf;
    3285       12502 :   gel(z,8) = res;
    3286       12502 :   gel(z,9) = clg2;
    3287       12502 :   return z;
    3288             : }
    3289             : 
    3290             : GEN
    3291        1603 : bnfinit0(GEN P, long flag, GEN data, long prec)
    3292             : {
    3293        1603 :   double c1 = 0., c2 = 0.;
    3294        1603 :   long fl, relpid = BNF_RELPID;
    3295             : 
    3296        1603 :   if (data)
    3297             :   {
    3298          28 :     long lx = lg(data);
    3299          28 :     if (typ(data) != t_VEC || lx > 5) pari_err_TYPE("bnfinit",data);
    3300          28 :     switch(lx)
    3301             :     {
    3302           0 :       case 4: relpid = itos(gel(data,3));
    3303          21 :       case 3: c2 = gtodouble(gel(data,2));
    3304          21 :       case 2: c1 = gtodouble(gel(data,1));
    3305             :     }
    3306             :   }
    3307        1603 :   switch(flag)
    3308             :   {
    3309             :     case 2:
    3310        1316 :     case 0: fl = 0; break;
    3311         287 :     case 1: fl = nf_FORCE; break;
    3312           0 :     default: pari_err_FLAG("bnfinit");
    3313             :       return NULL; /* LCOV_EXCL_LINE */
    3314             :   }
    3315        1603 :   return Buchall_param(P, c1, c2, relpid, fl, prec);
    3316             : }
    3317             : GEN
    3318       10906 : Buchall(GEN P, long flag, long prec)
    3319       10906 : { return Buchall_param(P, 0., 0., BNF_RELPID, flag & nf_FORCE, prec); }
    3320             : 
    3321             : static GEN
    3322         686 : Buchall_deg1(GEN nf)
    3323             : {
    3324         686 :   GEN v = cgetg(1,t_VEC), m = cgetg(1,t_MAT);
    3325         686 :   GEN res, W, A, B, C, Vbase = cgetg(1,t_COL);
    3326         686 :   GEN fu = v, R = gen_1, zu = mkvec2(gen_2, gen_m1);
    3327         686 :   GEN clg1 = mkvec3(gen_1,v,v), clg2 = mkvecn(6, m,m,m,v,m,m);
    3328             : 
    3329         686 :   W = A = B = C = m; res = mkvec5(clg1, R, gen_1, zu, fu);
    3330         686 :   return buchall_end(nf,res,clg2,W,B,A,C,Vbase);
    3331             : }
    3332             : 
    3333             : /* return (small set of) indices of columns generating the same lattice as x.
    3334             :  * Assume HNF(x) is inexpensive (few rows, many columns).
    3335             :  * Dichotomy approach since interesting columns may be at the very end */
    3336             : GEN
    3337       11816 : extract_full_lattice(GEN x)
    3338             : {
    3339       11816 :   long dj, j, k, l = lg(x);
    3340             :   GEN h, h2, H, v;
    3341             : 
    3342       11816 :   if (l < 200) return NULL; /* not worth it */
    3343             : 
    3344           0 :   v = vecsmalltrunc_init(l);
    3345           0 :   H = ZM_hnf(x);
    3346           0 :   h = cgetg(1, t_MAT);
    3347           0 :   dj = 1;
    3348           0 :   for (j = 1; j < l; )
    3349             :   {
    3350           0 :     pari_sp av = avma;
    3351           0 :     long lv = lg(v);
    3352             : 
    3353           0 :     for (k = 0; k < dj; k++) v[lv+k] = j+k;
    3354           0 :     setlg(v, lv + dj);
    3355           0 :     h2 = ZM_hnf(vecpermute(x, v));
    3356           0 :     if (ZM_equal(h, h2))
    3357             :     { /* these dj columns can be eliminated */
    3358           0 :       set_avma(av); setlg(v, lv);
    3359           0 :       j += dj;
    3360           0 :       if (j >= l) break;
    3361           0 :       dj <<= 1;
    3362           0 :       if (j + dj >= l) { dj = (l - j) >> 1; if (!dj) dj = 1; }
    3363             :     }
    3364           0 :     else if (dj > 1)
    3365             :     { /* at least one interesting column, try with first half of this set */
    3366           0 :       set_avma(av); setlg(v, lv);
    3367           0 :       dj >>= 1; /* > 0 */
    3368             :     }
    3369             :     else
    3370             :     { /* this column should be kept */
    3371           0 :       if (ZM_equal(h2, H)) break;
    3372           0 :       h = h2; j++;
    3373             :     }
    3374             :   }
    3375           0 :   return v;
    3376             : }
    3377             : 
    3378             : static void
    3379       11872 : init_rel(RELCACHE_t *cache, FB_t *F, long add_need)
    3380             : {
    3381       11872 :   const long n = F->KC + add_need; /* expected # of needed relations */
    3382             :   long i, j, k, p;
    3383             :   GEN c, P;
    3384             :   GEN R;
    3385             : 
    3386       11872 :   if (DEBUGLEVEL) err_printf("KCZ = %ld, KC = %ld, n = %ld\n", F->KCZ,F->KC,n);
    3387       11872 :   reallocate(cache, 10*n + 50); /* make room for lots of relations */
    3388       11872 :   cache->chk = cache->base;
    3389       11872 :   cache->end = cache->base + n;
    3390       11872 :   cache->relsup = add_need;
    3391       11872 :   cache->last = cache->base;
    3392       11872 :   cache->missing = lg(cache->basis) - 1;
    3393       60438 :   for (i = 1; i <= F->KCZ; i++)
    3394             :   { /* trivial relations (p) = prod P^e */
    3395       48566 :     p = F->FB[i]; P = F->LV[p];
    3396       48566 :     if (!isclone(P)) continue;
    3397             : 
    3398             :     /* all prime divisors in FB */
    3399       42609 :     c = zero_Flv(F->KC); k = F->iLP[p];
    3400       42609 :     R = c; c += k;
    3401       42609 :     for (j = lg(P)-1; j; j--) c[j] = pr_get_e(gel(P,j));
    3402       42609 :     add_rel(cache, F, R, k+1, pr_get_p(gel(P,1)), 0);
    3403             :   }
    3404       11872 : }
    3405             : 
    3406             : /* Let z = \zeta_n in nf. List of not-obviously-dependent generators for
    3407             :  * cyclotomic units modulo torsion in Q(z) [independent when n a prime power]:
    3408             :  * - z^a - 1,  n/(a,n) not a prime power, a \nmid n unless a=1,  1 <= a < n/2
    3409             :  * - (Z^a - 1)/(Z - 1),  p^k || n, Z = z^{n/p^k}, (p,a) = 1, 1 < a <= (p^k-1)/2
    3410             :  */
    3411             : GEN
    3412       11872 : nfcyclotomicunits(GEN nf, GEN zu)
    3413             : {
    3414       11872 :   long n = itos(gel(zu, 1)), n2, lP, i, a;
    3415             :   GEN z, fa, P, E, L, mz, powz;
    3416       11872 :   if (n <= 6) return cgetg(1, t_VEC);
    3417             : 
    3418         147 :   z = algtobasis(nf,gel(zu, 2));
    3419         147 :   if ((n & 3) == 2) { n = n >> 1; z = ZC_neg(z); } /* ensure n != 2 (mod 4) */
    3420         147 :   n2 = n/2;
    3421         147 :   mz = zk_multable(nf, z); /* multiplication by z */
    3422         147 :   powz = cgetg(n2, t_VEC); gel(powz,1) = z;
    3423         147 :   for (i = 2; i < n2; i++) gel(powz,i) = ZM_ZC_mul(mz, gel(powz,i-1));
    3424             :   /* powz[i] = z^i */
    3425             : 
    3426         147 :   L = vectrunc_init(n);
    3427         147 :   fa = factoru(n);
    3428         147 :   P = gel(fa,1); lP = lg(P);
    3429         147 :   E = gel(fa,2);
    3430         308 :   for (i = 1; i < lP; i++)
    3431             :   { /* second kind */
    3432         161 :     long p = P[i], k = E[i], pk = upowuu(p,k), pk2 = (pk-1) / 2;
    3433         161 :     GEN u = gen_1;
    3434         322 :     for (a = 2; a <= pk2; a++)
    3435             :     {
    3436         161 :       u = nfadd(nf, u, gel(powz, (n/pk) * (a-1))); /* = (Z^a-1)/(Z-1) */
    3437         161 :       if (a % p) vectrunc_append(L, u);
    3438             :     }
    3439             :   }
    3440         217 :   if (lP > 2) for (a = 1; a < n2; a++)
    3441             :   { /* first kind, when n not a prime power */
    3442             :     ulong p;
    3443          70 :     if (a > 1 && (n % a == 0 || uisprimepower(n/ugcd(a,n), &p))) continue;
    3444          28 :     vectrunc_append(L, nfadd(nf, gel(powz, a), gen_m1));
    3445             :   }
    3446         147 :   return L;
    3447             : }
    3448             : static void
    3449       11872 : add_cyclotomic_units(GEN nf, GEN zu, RELCACHE_t *cache, FB_t *F)
    3450             : {
    3451       11872 :   pari_sp av = avma;
    3452       11872 :   GEN L = nfcyclotomicunits(nf, zu);
    3453       11872 :   long i, l = lg(L);
    3454       11872 :   if (l > 1)
    3455             :   {
    3456         147 :     GEN R = zero_Flv(F->KC);
    3457         147 :     for(i = 1; i < l; i++) add_rel(cache, F, R, F->KC+1, gel(L,i), 0);
    3458             :   }
    3459       11872 :   set_avma(av);
    3460       11872 : }
    3461             : 
    3462             : static GEN
    3463       66698 : trim_list(FB_t *F)
    3464             : {
    3465       66698 :   pari_sp av = avma;
    3466       66698 :   GEN v, L_jid = F->L_jid, minidx = F->minidx, present = zero_Flv(F->KC);
    3467       66698 :   long i, j, imax = minss(lg(L_jid), F->KC + 1);
    3468             : 
    3469       66698 :   v = cgetg(imax, t_VECSMALL);
    3470     2416747 :   for (i = j = 1; i < imax; i++)
    3471             :   {
    3472     2350049 :     long k = minidx[ L_jid[i] ];
    3473     2350049 :     if (!present[k]) { v[j++] = L_jid[i]; present[k] = 1; }
    3474             :   }
    3475       66698 :   setlg(v, j); return gerepileuptoleaf(av, v);
    3476             : }
    3477             : 
    3478             : static void
    3479        8164 : try_elt(RELCACHE_t *cache, FB_t *F, GEN nf, GEN x, FACT *fact)
    3480             : {
    3481        8164 :   pari_sp av = avma;
    3482             :   GEN R, Nx;
    3483        8164 :   long nz, tx = typ(x);
    3484             : 
    3485       12021 :   if (tx == t_INT || tx == t_FRAC) return;
    3486        4307 :   if (tx != t_COL) x = algtobasis(nf, x);
    3487        4307 :   if (RgV_isscalar(x)) return;
    3488        4307 :   x = Q_primpart(x);
    3489        4307 :   Nx = nfnorm(nf, x);
    3490        4307 :   if (!can_factor(F, nf, NULL, x, Nx, fact)) return;
    3491             : 
    3492             :   /* smooth element */
    3493        4307 :   R = set_fact(F, fact, NULL, &nz);
    3494             :   /* make sure we get maximal rank first, then allow all relations */
    3495        4307 :   (void) add_rel(cache, F, R, nz, x, 0);
    3496        4307 :   set_avma(av);
    3497             : }
    3498             : 
    3499             : static long
    3500      923419 : scalar_bit(GEN x) { return bit_accuracy(gprecision(x)) - gexpo(x); }
    3501             : static long
    3502       11795 : RgM_bit(GEN x, long bit)
    3503             : {
    3504       11795 :   long i, j, m, b = bit, l = lg(x);
    3505       11795 :   if (l == 1) return b;
    3506       11795 :   m = lgcols(x);
    3507      418650 :   for (j = 1; j < l; j++)
    3508      406855 :     for (i = 1; i < m; i++ ) b = minss(b, scalar_bit(gcoeff(x,i,j)));
    3509       11795 :   return b;
    3510             : }
    3511             : static void
    3512        6726 : matenlarge(GEN C, long h)
    3513             : {
    3514        6726 :   GEN _0 = zerocol(h);
    3515             :   long i;
    3516        6726 :   for (i = lg(C); --i; ) gel(C,i) = shallowconcat(gel(C,i), _0);
    3517        6726 : }
    3518             : 
    3519             : /* E = floating point embeddings */
    3520             : static GEN
    3521        6726 : matbotidembs(RELCACHE_t *cache, GEN E)
    3522             : {
    3523        6726 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3524        6726 :   long j, d = h - w, hE = nbrows(E);
    3525        6726 :   GEN y = cgetg(w+1,t_MAT), _0 = zerocol(h);
    3526       34588 :   for (j = 1; j <= w; j++)
    3527             :   {
    3528       27862 :     GEN c = shallowconcat(gel(E,j), _0);
    3529       27862 :     if (d + j >= 1) gel(c, d + j + hE) = gen_1;
    3530       27862 :     gel(y,j) = c;
    3531             :   }
    3532        6726 :   return y;
    3533             : }
    3534             : static GEN
    3535        1547 : matbotid(RELCACHE_t *cache)
    3536             : {
    3537        1547 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3538        1547 :   long j, d = h - w;
    3539        1547 :   GEN y = cgetg(w+1,t_MAT);
    3540       33838 :   for (j = 1; j <= w; j++)
    3541             :   {
    3542       32291 :     GEN c = zerocol(h);
    3543       32291 :     if (d + j >= 1) gel(c, d + j) = gen_1;
    3544       32291 :     gel(y,j) = c;
    3545             :   }
    3546        1547 :   return y;
    3547             : }
    3548             : 
    3549             : static long
    3550          45 : myprecdbl(long prec, GEN C)
    3551             : {
    3552          45 :   long p = precdbl(prec);
    3553          45 :   if (C) p = maxss(p, minss(3*p, prec + nbits2extraprec(gexpo(C))));
    3554          45 :   return p;
    3555             : }
    3556             : 
    3557             : /* Nrelid = nb relations per ideal, possibly 0. If flag is set, keep data in
    3558             :  * algebraic form. */
    3559             : GEN
    3560       12509 : Buchall_param(GEN P, double cbach, double cbach2, long Nrelid, long flag, long prec)
    3561             : {
    3562             :   pari_timer T;
    3563       12509 :   pari_sp av0 = avma, av, av2;
    3564             :   long PREC, N, R1, R2, RU, low, high, LIMC0, LIMC, LIMC2, LIMCMAX, zc, i;
    3565       12509 :   long LIMres, bit = 0, flag_nfinit = 0;
    3566       12509 :   long nreldep, sfb_trials, need, old_need, precdouble = 0, TRIES = 0;
    3567             :   long done_small, small_fail, fail_limit, squash_index, small_norm_prec;
    3568             :   double LOGD, LOGD2, lim;
    3569       12509 :   GEN computed = NULL, fu = NULL, zu, nf, M_sn, D, A, W, R, h, Ce, PERM;
    3570             :   GEN small_multiplier, auts, cyclic, embs, SUnits;
    3571             :   GEN res, L, invhr, B, C, C0, lambda, dep, clg1, clg2, Vbase;
    3572       12509 :   const char *precpb = NULL;
    3573             :   nfmaxord_t nfT;
    3574             :   RELCACHE_t cache;
    3575             :   FB_t F;
    3576             :   GRHcheck_t GRHcheck;
    3577             :   FACT *fact;
    3578             : 
    3579       12509 :   if (DEBUGLEVEL) timer_start(&T);
    3580       12509 :   P = get_nfpol(P, &nf);
    3581       12502 :   if (nf)
    3582             :   {
    3583         567 :     PREC = nf_get_prec(nf);
    3584         567 :     D = nf_get_disc(nf);
    3585             :   }
    3586             :   else
    3587             :   {
    3588       11935 :     PREC = maxss(prec, MEDDEFAULTPREC);
    3589       11935 :     nfinit_basic(&nfT, P);
    3590       11935 :     D = nfT.dK;
    3591       11935 :     if (!ZX_is_monic(nfT.T0))
    3592             :     {
    3593          14 :       pari_warn(warner,"non-monic polynomial in bnfinit, using polredbest");
    3594          14 :       flag_nfinit = nf_RED;
    3595             :     }
    3596             :   }
    3597       12502 :   N = degpol(P);
    3598       12502 :   if (N <= 1)
    3599             :   {
    3600         686 :     if (!nf) nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3601         686 :     return gerepilecopy(av0, Buchall_deg1(nf));
    3602             :   }
    3603       11816 :   D = absi_shallow(D);
    3604       11816 :   LOGD = dbllog2(D) * M_LN2;
    3605       11816 :   LOGD2 = LOGD*LOGD;
    3606       11816 :   LIMCMAX = (long)(12.*LOGD2);
    3607             :   /* In small_norm, LLL reduction produces v0 in I such that
    3608             :    *     T2(v0) <= (4/3)^((n-1)/2) NI^(2/n) disc(K)^(1/n)
    3609             :    * We consider v with T2(v) <= BMULT * T2(v0)
    3610             :    * Hence Nv <= ((4/3)^((n-1)/2) * BMULT / n)^(n/2) NI sqrt(disc(K)).
    3611             :    * NI <= LIMCMAX^2 */
    3612       11816 :   small_norm_prec = nbits2prec( BITS_IN_LONG +
    3613       11816 :     (N/2. * ((N-1)/2.*log(4./3) + log(BMULT/(double)N))
    3614       11816 :      + 2*log((double) LIMCMAX) + LOGD/2) / M_LN2 ); /*enough to compute norms*/
    3615       11816 :   if (small_norm_prec > PREC) PREC = small_norm_prec;
    3616       11816 :   if (!nf)
    3617       11424 :     nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3618         392 :   else if (nf_get_prec(nf) < PREC)
    3619           0 :     nf = nfnewprec_shallow(nf, PREC);
    3620       11816 :   M_sn = nf_get_M(nf);
    3621       11816 :   if (PREC > small_norm_prec) M_sn = gprec_w(M_sn, small_norm_prec);
    3622             : 
    3623       11816 :   zu = nfrootsof1(nf);
    3624       11816 :   gel(zu,2) = nf_to_scalar_or_alg(nf, gel(zu,2));
    3625             : 
    3626       11816 :   nf_get_sign(nf, &R1, &R2); RU = R1+R2;
    3627       11816 :   auts = automorphism_matrices(nf, &cyclic);
    3628       11816 :   F.embperm = automorphism_perms(nf_get_M(nf), auts, cyclic, R1, R2, N);
    3629       11816 :   if (DEBUGLEVEL)
    3630             :   {
    3631           0 :     timer_printf(&T, "nfinit & nfrootsof1");
    3632           0 :     err_printf("%sR1 = %ld, R2 = %ld\nD = %Ps\n",
    3633             :                flag? "Algebraic bnf: ":"Floating point bnf: ", R1,R2, D);
    3634             :   }
    3635       11816 :   if (LOGD < 20.)
    3636             :   { /* tiny disc, Minkowski may be smaller than Bach */
    3637       11382 :     lim = exp(-N + R2 * log(4/M_PI) + LOGD/2) * sqrt(2*M_PI*N);
    3638       11382 :     if (lim < 3) lim = 3;
    3639             :   }
    3640             :   else /* to be ignored */
    3641         434 :     lim = -1;
    3642       11816 :   if (cbach > 12.) {
    3643           0 :     if (cbach2 < cbach) cbach2 = cbach;
    3644           0 :     cbach = 12.;
    3645             :   }
    3646       11816 :   if (cbach < 0.)
    3647           0 :     pari_err_DOMAIN("Buchall","Bach constant","<",gen_0,dbltor(cbach));
    3648             : 
    3649       11816 :   cache.base = NULL; F.subFB = NULL; F.LP = NULL; SUnits = Ce = NULL;
    3650       11816 :   init_GRHcheck(&GRHcheck, N, R1, LOGD);
    3651       11816 :   high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
    3652       11816 :   while (!GRHchk(nf, &GRHcheck, high)) { low = high; high *= 2; }
    3653       56616 :   while (high - low > 1)
    3654             :   {
    3655       32984 :     long test = (low+high)/2;
    3656       32984 :     if (GRHchk(nf, &GRHcheck, test)) high = test; else low = test;
    3657             :   }
    3658       11816 :   LIMC2 = (high == LIMC0+1 && GRHchk(nf, &GRHcheck, LIMC0))? LIMC0: high;
    3659       11816 :   if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
    3660       11816 :   if (DEBUGLEVEL) err_printf("LIMC2 = %ld\n", LIMC2);
    3661       11816 :   LIMC0 = (long)(cbach*LOGD2);
    3662       11816 :   LIMC = cbach? LIMC0: LIMC2;
    3663       11816 :   LIMC = maxss(LIMC, nthideal(&GRHcheck, nf, N));
    3664       11816 :   if (DEBUGLEVEL) timer_printf(&T, "computing Bach constant");
    3665       11816 :   LIMres = primeneeded(N, R1, R2, LOGD);
    3666       11816 :   cache_prime_dec(&GRHcheck, LIMres, nf);
    3667             :   /* invhr ~ 2^r1 (2pi)^r2 / sqrt(D) w * Res(zeta_K, s=1) = 1 / hR */
    3668       23632 :   invhr = gmul(gdiv(gmul2n(powru(mppi(DEFAULTPREC), R2), RU),
    3669       11816 :               mulri(gsqrt(D,DEFAULTPREC),gel(zu,1))),
    3670             :               compute_invres(&GRHcheck, LIMres));
    3671       11816 :   if (DEBUGLEVEL) timer_printf(&T, "computing inverse of hR");
    3672       11816 :   av = avma;
    3673             : 
    3674             : START:
    3675       12495 :   if (DEBUGLEVEL) timer_start(&T);
    3676       12495 :   if (TRIES) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
    3677       12495 :   if (DEBUGLEVEL && LIMC > LIMC0)
    3678           0 :     err_printf("%s*** Bach constant: %f\n", TRIES?"\n":"", LIMC/LOGD2);
    3679       12495 :   if (cache.base)
    3680             :   {
    3681             :     REL_t *rel;
    3682        9720 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    3683        9664 :       if (rel->m) i++;
    3684          56 :     computed = cgetg(i, t_VEC);
    3685        9720 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    3686        9664 :       if (rel->m) gel(computed, i++) = rel->m;
    3687          56 :     computed = gclone(computed); delete_cache(&cache);
    3688             :   }
    3689       12495 :   TRIES++; set_avma(av);
    3690       12495 :   if (F.LP) delete_FB(&F);
    3691       12495 :   if (LIMC2 < LIMC) LIMC2 = LIMC;
    3692       12495 :   if (DEBUGLEVEL) { err_printf("LIMC = %ld, LIMC2 = %ld\n",LIMC,LIMC2); }
    3693             : 
    3694       12495 :   FBgen(&F, nf, N, LIMC, LIMC2, &GRHcheck);
    3695       12495 :   if (!F.KC) goto START;
    3696       12495 :   av = avma;
    3697       12495 :   subFBgen(&F,auts,cyclic,lim < 0? LIMC2: mindd(lim,LIMC2),MINSFB);
    3698       12495 :   if (lg(F.subFB) == 1) goto START;
    3699       11872 :   if (DEBUGLEVEL)
    3700           0 :     timer_printf(&T, "factorbase (#subFB = %ld) and ideal permutations",
    3701           0 :                      lg(F.subFB)-1);
    3702             : 
    3703       11872 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    3704       11872 :   PERM = leafcopy(F.perm); /* to be restored in case of precision increase */
    3705       11872 :   cache.basis = zero_Flm_copy(F.KC,F.KC);
    3706       11872 :   small_multiplier = zero_Flv(F.KC);
    3707       11872 :   done_small = small_fail = squash_index = zc = sfb_trials = nreldep = 0;
    3708       11872 :   fail_limit = F.KC + 1;
    3709       11872 :   W = A = R = NULL;
    3710       11872 :   av2 = avma;
    3711       11872 :   init_rel(&cache, &F, RELSUP + RU-1);
    3712       11872 :   old_need = need = cache.end - cache.last;
    3713       11872 :   add_cyclotomic_units(nf, zu, &cache, &F);
    3714       11872 :   if (DEBUGLEVEL) err_printf("\n");
    3715       11872 :   cache.end = cache.last + need;
    3716             : 
    3717       11872 :   if (computed)
    3718             :   {
    3719        8220 :     for (i = 1; i < lg(computed); i++)
    3720        8164 :       try_elt(&cache, &F, nf, gel(computed, i), fact);
    3721          56 :     gunclone(computed);
    3722          56 :     if (DEBUGLEVEL && i > 1)
    3723           0 :       timer_printf(&T, "including already computed relations");
    3724          56 :     need = 0;
    3725             :   }
    3726             : 
    3727             :   do
    3728             :   {
    3729             :     do
    3730             :     {
    3731       66769 :       pari_sp av4 = avma;
    3732       66769 :       if (need > 0)
    3733             :       {
    3734       66698 :         long oneed = cache.end - cache.last;
    3735             :         /* Test below can be true if small_norm did not find enough linearly
    3736             :          * dependent relations */
    3737       66698 :         if (need < oneed) need = oneed;
    3738       66698 :         pre_allocate(&cache, need+lg(auts)-1+(R ? lg(W)-1 : 0));
    3739       66698 :         cache.end = cache.last + need;
    3740       66698 :         F.L_jid = trim_list(&F);
    3741             :       }
    3742       66769 :       if (need > 0 && Nrelid > 0 && (done_small <= F.KC+1 || A) &&
    3743       46068 :           small_fail <= fail_limit &&
    3744       46068 :           cache.last < cache.base + 2*F.KC+2*RU+RELSUP /* heuristic */)
    3745             :       {
    3746       45285 :         long j, k, LIE = (R && lg(W) > 1 && (done_small % 2));
    3747       45285 :         REL_t *last = cache.last;
    3748       45285 :         pari_sp av3 = avma;
    3749             :         GEN p0;
    3750       45285 :         if (LIE)
    3751             :         { /* We have full rank for class group and unit. The following tries to
    3752             :            * improve the prime group lattice by looking for relations involving
    3753             :            * the primes generating the class group. */
    3754        1091 :           long n = lg(W)-1; /* need n relations to squash the class group */
    3755        1091 :           F.L_jid = vecslice(F.perm, 1, n);
    3756        1091 :           cache.end = cache.last + n;
    3757             :           /* Lie to the add_rel subsystem: pretend we miss relations involving
    3758             :            * the primes generating the class group (and only those). */
    3759        1091 :           cache.missing = n;
    3760        1091 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 0;
    3761             :         }
    3762       45285 :         j = done_small % (F.KC+1);
    3763       45285 :         if (j == 0) p0 = NULL;
    3764             :         else
    3765             :         {
    3766       33112 :           p0 = gel(F.LP, j);
    3767       33112 :           if (!A)
    3768             :           { /* Prevent considering both P_iP_j and P_jP_i in small_norm */
    3769             :             /* Not all elements end up in F.L_jid (eliminated by hnfspec/add or
    3770             :              * by trim_list): keep track of which ideals are being considered
    3771             :              * at each run. */
    3772       25578 :             long mj = small_multiplier[j];
    3773     1035020 :             for (i = k = 1; i < lg(F.L_jid); i++)
    3774     1009442 :               if (F.L_jid[i] > mj)
    3775             :               {
    3776      893067 :                 small_multiplier[F.L_jid[i]] = j;
    3777      893067 :                 F.L_jid[k++] = F.L_jid[i];
    3778             :               }
    3779       25578 :             setlg(F.L_jid, k);
    3780             :           }
    3781             :         }
    3782       45285 :         if (lg(F.L_jid) > 1)
    3783       44963 :           small_norm(&cache, &F, nf, Nrelid, M_sn, fact, p0);
    3784       45285 :         F.L_jid = F.perm; set_avma(av3);
    3785       45285 :         if (!A && cache.last != last) small_fail = 0; else small_fail++;
    3786       45285 :         if (LIE)
    3787             :         { /* restore add_rel subsystem: undo above lie */
    3788        1091 :           long n = lg(W) - 1;
    3789        1091 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 1;
    3790        1091 :           cache.missing = 0;
    3791             :         }
    3792       45285 :         cache.end = cache.last;
    3793       45285 :         done_small++;
    3794       45285 :         need = F.sfb_chg = 0;
    3795             :       }
    3796       66769 :       if (need > 0)
    3797             :       { /* Random relations */
    3798       21413 :         if (++nreldep > F.MAXDEPSIZESFB) {
    3799          57 :           if (++sfb_trials > SFB_MAX && LIMC < LIMCMAX/6) goto START;
    3800          50 :           F.sfb_chg = sfb_INCREASE;
    3801          50 :           nreldep = 0;
    3802             :         }
    3803       21356 :         else if (!(nreldep % F.MAXDEPSFB))
    3804        2384 :           F.sfb_chg = sfb_CHANGE;
    3805       21406 :         if (F.sfb_chg && !subFB_change(&F)) goto START;
    3806       21406 :         rnd_rel(&cache, &F, nf, fact);
    3807       21406 :         F.L_jid = F.perm;
    3808             :       }
    3809       66762 :       if (DEBUGLEVEL) timer_start(&T);
    3810       66762 :       if (precpb)
    3811             :       {
    3812          58 :         GEN nf0 = nf, M;
    3813             :         REL_t *rel;
    3814          58 :         if (DEBUGLEVEL)
    3815             :         {
    3816           0 :           char str[64]; sprintf(str,"Buchall_param (%s)",precpb);
    3817           0 :           pari_warn(warnprec,str,PREC);
    3818             :         }
    3819          58 :         nf = gclone( nfnewprec_shallow(nf, PREC) );
    3820          58 :         M = nf_get_M(nf);
    3821          58 :         if (precdouble) gunclone(nf0);
    3822          58 :         precdouble++; precpb = NULL;
    3823             : 
    3824          58 :         if (flag)
    3825             :         { /* recompute embs only, no need to redo HNF */
    3826          14 :           long j, le = lg(embs), lC = lg(C);
    3827             :           GEN E;
    3828          14 :           set_avma(av4);
    3829        5453 :           for (rel = cache.base+1, i = 1; i < le; i++,rel++)
    3830        5439 :             gel(embs,i) = rel_embed(rel, &F, embs, i, M, RU, R1, PREC);
    3831          14 :           E = RgM_mul(embs, rowslice(C, RU+1, nbrows(C)));
    3832        5453 :           for (j = 1; j < lC; j++)
    3833        5439 :             for (i = 1; i <= RU; i++) gcoeff(C,i,j) = gcoeff(E,i,j);
    3834          14 :           av4 = avma;
    3835             :         }
    3836             :         else
    3837             :         { /* recompute embs + HNF */
    3838          44 :           for(i = 1; i < lg(PERM); i++) F.perm[i] = PERM[i];
    3839          44 :           cache.chk = cache.base;
    3840          44 :           W = NULL;
    3841             :         }
    3842          58 :         if (DEBUGLEVEL) timer_printf(&T, "increasing accuracy");
    3843             :       }
    3844       66762 :       set_avma(av4);
    3845       66762 :       if (cache.chk != cache.last)
    3846             :       { /* Reduce relation matrices */
    3847       25482 :         long l = cache.last - cache.chk + 1, j;
    3848       25482 :         GEN mat = cgetg(l, t_MAT);
    3849             :         REL_t *rel;
    3850             : 
    3851       25482 :         for (j=1,rel = cache.chk + 1; j < l; rel++,j++) gel(mat,j) = rel->R;
    3852       25482 :         if (!flag || W)
    3853             :         {
    3854       23935 :           embs = get_embs(&F, &cache, nf, RU, R1, embs, PREC);
    3855       23935 :           if (DEBUGLEVEL && timer_get(&T) > 1)
    3856           0 :             timer_printf(&T, "floating point embeddings");
    3857             :         }
    3858       25482 :         if (!W)
    3859             :         { /* never reduced before */
    3860       11916 :           C = flag? matbotid(&cache): embs;
    3861       11916 :           W = hnfspec_i(mat, F.perm, &dep, &B, &C, F.subFB ? lg(F.subFB)-1:0);
    3862       11916 :           if (DEBUGLEVEL)
    3863           0 :             timer_printf(&T, "hnfspec [%ld x %ld]", lg(F.perm)-1, l-1);
    3864       11916 :           if (flag)
    3865             :           {
    3866        1547 :             PREC += nbits2extraprec(gexpo(C));
    3867        1547 :             embs = get_embs(&F, &cache, nf, RU, R1, embs, PREC);
    3868        1547 :             C = vconcat(RgM_mul(embs, C), C);
    3869             :           }
    3870       11916 :           if (DEBUGLEVEL)
    3871           0 :             timer_printf(&T, "hnfspec floating points");
    3872             :         }
    3873             :         else
    3874             :         {
    3875       13566 :           long k = lg(embs);
    3876       13566 :           GEN E = vecslice(embs, k-l+1,k-1);
    3877       13566 :           if (flag)
    3878             :           {
    3879        6726 :             E = matbotidembs(&cache, E);
    3880        6726 :             matenlarge(C, cache.last - cache.chk);
    3881             :           }
    3882       13566 :           W = hnfadd_i(W, F.perm, &dep, &B, &C, mat, E);
    3883       13566 :           if (DEBUGLEVEL)
    3884           0 :             timer_printf(&T, "hnfadd (%ld + %ld)", l-1, lg(dep)-1);
    3885             :         }
    3886       25482 :         gerepileall(av2, 5, &W,&C,&B,&dep,&embs);
    3887       25482 :         cache.chk = cache.last;
    3888             :       }
    3889       41280 :       else if (!W)
    3890             :       {
    3891           0 :         need = old_need;
    3892           0 :         F.L_jid = vecslice(F.perm, 1, need);
    3893           0 :         continue;
    3894             :       }
    3895       66762 :       need = F.KC - (lg(W)-1) - (lg(B)-1);
    3896       66762 :       if (!need && cache.missing)
    3897             :       { /* The test above will never be true except if 27449|class number.
    3898             :          * Ensure that if we have maximal rank for the ideal lattice, then
    3899             :          * cache.missing == 0. */
    3900          14 :         for (i = 1; cache.missing; i++)
    3901           7 :           if (!mael(cache.basis, i, i))
    3902             :           {
    3903             :             long j;
    3904           7 :             cache.missing--; mael(cache.basis, i, i) = 1;
    3905           7 :             for (j = i+1; j <= F.KC; j++) mael(cache.basis, j, i) = 0;
    3906             :           }
    3907             :       }
    3908       66762 :       zc = (lg(C)-1) - (lg(B)-1) - (lg(W)-1);
    3909       66762 :       if (RU-1-zc > 0) need = minss(need + RU-1-zc, F.KC); /* for units */
    3910       66762 :       if (need)
    3911             :       { /* dependent rows */
    3912       45762 :         F.L_jid = vecslice(F.perm, 1, need);
    3913       45762 :         vecsmall_sort(F.L_jid);
    3914       45762 :         if (need != old_need) { nreldep = 0; old_need = need; }
    3915             :       }
    3916             :       else
    3917             :       { /* If the relation lattice is too small, check will be > 1 and we will
    3918             :          * do a new run of small_norm/rnd_rel asking for 1 relation. This often
    3919             :          * gives a relation involving L_jid[1]. We rotate the first element of
    3920             :          * L_jid in order to increase the probability of finding relations that
    3921             :          * increases the lattice. */
    3922       21000 :         long j, n = lg(W) - 1;
    3923       25166 :         if (n > 1 && squash_index % n)
    3924             :         {
    3925        4166 :           F.L_jid = leafcopy(F.perm);
    3926       23551 :           for (j = 1; j <= n; j++)
    3927       19385 :             F.L_jid[j] = F.perm[1 + (j + squash_index - 1) % n];
    3928             :         }
    3929             :         else
    3930       16834 :           F.L_jid = F.perm;
    3931       21000 :         squash_index++;
    3932             :       }
    3933             :     }
    3934       66762 :     while (need);
    3935             : 
    3936       21000 :     if (!A)
    3937             :     {
    3938       11865 :       small_fail = old_need = 0;
    3939       11865 :       fail_limit = maxss(F.KC / FAIL_DIVISOR, MINFAIL);
    3940             :     }
    3941       21000 :     A = vecslice(C, 1, zc); /* cols corresponding to units */
    3942       21000 :     if (flag) A = rowslice(A, 1, RU);
    3943       21000 :     R = compute_multiple_of_R(A, RU, N, &need, &bit, &lambda);
    3944       21000 :     if (need < old_need) small_fail = 0;
    3945             :     /* we have computed way more relations than should be necessary */
    3946       34222 :     if (TRIES < 3 && LIMC < LIMCMAX / 24 &&
    3947       13250 :                      cache.last - cache.base > 10 * F.KC) goto START;
    3948       20972 :     old_need = need;
    3949       20972 :     if (!lambda)
    3950          44 :     { precpb = "bestappr"; PREC = myprecdbl(PREC, flag? C: NULL); continue; }
    3951       20928 :     if (!R)
    3952             :     { /* not full rank for units */
    3953        6240 :       if (!need)
    3954           1 :       { precpb = "regulator"; PREC = myprecdbl(PREC, flag? C: NULL); }
    3955        6240 :       continue;
    3956             :     }
    3957       14688 :     h = ZM_det_triangular(W);
    3958       14688 :     if (DEBUGLEVEL) err_printf("\n#### Tentative class number: %Ps\n", h);
    3959       14688 :     switch (compute_R(lambda, mulir(h,invhr), flag? 0: RgM_bit(C, bit), &L, &R))
    3960             :     {
    3961             :       case fupb_RELAT:
    3962        2838 :         need = 1; /* not enough relations */
    3963        2838 :         continue;
    3964             :       case fupb_PRECI: /* prec problem unless we cheat on Bach constant */
    3965          13 :         if ((precdouble&7) == 7 && LIMC<=LIMCMAX/6) goto START;
    3966          13 :         precpb = "compute_R"; PREC = precdbl(PREC);
    3967          13 :         continue;
    3968             :     }
    3969             :     /* DONE */
    3970             : 
    3971       11837 :     if (F.KCZ2 > F.KCZ)
    3972             :     {
    3973          28 :       if (F.sfb_chg && !subFB_change(&F)) goto START;
    3974          28 :       if (!be_honest(&F, nf, auts, fact)) goto START;
    3975           7 :       if (DEBUGLEVEL) timer_printf(&T, "to be honest");
    3976             :     }
    3977       11816 :     F.KCZ2 = 0; /* be honest only once */
    3978             : 
    3979             :     /* fundamental units */
    3980             :     {
    3981       11816 :       GEN AU, CU, U, H, v = extract_full_lattice(L); /* L may be very large */
    3982       11816 :       CU = NULL;
    3983       11816 :       if (v) { A = vecpermute(A, v); L = vecpermute(L, v); }
    3984             :       /* arch. components of fund. units */
    3985       11816 :       H = ZM_hnflll(L, &U, 1); U = vecslice(U, lg(U)-(RU-1), lg(U)-1);
    3986       11816 :       U = ZM_mul(U, ZM_lll(H, 0.99, LLL_IM));
    3987       11816 :       AU = RgM_mul(A, U);
    3988       11816 :       A = cleanarch(AU, N, PREC);
    3989       11816 :       if (DEBUGLEVEL) timer_printf(&T, "units LLL + cleanarch");
    3990       11816 :       if (!A) {
    3991           0 :         long add = nbits2extraprec( gexpo(AU) + 64 ) - gprecision(AU);
    3992           0 :         precpb = "cleanarch"; PREC += maxss(add, 1); continue;
    3993             :       }
    3994       11816 :       if (flag)
    3995             :       {
    3996        1526 :         long l = lgcols(C) - RU;
    3997             :         REL_t *rel;
    3998        1526 :         SUnits = cgetg(l, t_COL);
    3999       60195 :         for (rel = cache.base+1, i = 1; i < l; i++,rel++)
    4000       58669 :           set_rel_alpha(rel, auts, SUnits, i);
    4001        1526 :         if (RU > 1)
    4002             :         {
    4003        1246 :           GEN c = v? vecpermute(C,v): vecslice(C,1,zc);
    4004        1246 :           CU = ZM_mul(rowslice(c, RU+1, nbrows(c)), U);
    4005             :         }
    4006             :       }
    4007       11816 :       if (DEBUGLEVEL) err_printf("\n#### Computing fundamental units\n");
    4008       11816 :       fu = getfu(nf, &A, CU? &U: NULL, PREC);
    4009       11816 :       CU = CU? ZM_mul(CU, U): cgetg(1, t_MAT);
    4010       11816 :       if (DEBUGLEVEL) timer_printf(&T, "getfu");
    4011       11816 :       Ce = vecslice(C, zc+1, lg(C)-1);
    4012       11816 :       if (flag) SUnits = mkvec4(SUnits, CU, rowslice(Ce, RU+1, nbrows(Ce)),
    4013             :                                 utoipos(LIMC));
    4014             :     }
    4015             :     /* class group generators */
    4016       11816 :     if (flag) Ce = rowslice(Ce, 1, RU);
    4017       11816 :     C0 = Ce; Ce = cleanarch(Ce, N, PREC);
    4018       11816 :     if (!Ce) {
    4019           0 :       long add = nbits2extraprec( gexpo(C0) + 64 ) - gprecision(C0);
    4020           0 :       precpb = "cleanarch"; PREC += maxss(add, 1);
    4021             :     }
    4022       11816 :     if (DEBUGLEVEL) timer_printf(&T, "cleanarch");
    4023       20951 :   } while (need || precpb);
    4024             : 
    4025       11816 :   Vbase = vecpermute(F.LP, F.perm);
    4026       11816 :   if (!fu) fu = cgetg(1, t_MAT);
    4027       11816 :   if (!SUnits) SUnits = gen_1;
    4028       11816 :   clg1 = class_group_gen(nf,W,Ce,Vbase,PREC, &clg2);
    4029       11816 :   res = mkvec5(clg1, R, SUnits, zu, fu);
    4030       11816 :   res = buchall_end(nf,res,clg2,W,B,A,Ce,Vbase);
    4031       11816 :   delete_FB(&F);
    4032       11816 :   res = gerepilecopy(av0, res);
    4033       11816 :   if (flag) obj_insert_shallow(res, MATAL, cgetg(1,t_VEC));
    4034       11816 :   if (precdouble) gunclone(nf);
    4035       11816 :   delete_cache(&cache);
    4036       11816 :   free_GRHcheck(&GRHcheck);
    4037       11816 :   return res;
    4038             : }

Generated by: LCOV version 1.13