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

Generated by: LCOV version 1.13