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

Generated by: LCOV version 1.13