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.16.1 lcov report (development 28559-a7bed2c7d7) Lines: 2201 2390 92.1 %
Date: 2023-06-04 07:48:19 Functions: 152 163 93.3 %
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 prodZ; /* product of the primes in KCZ*/
      74             :   GEN subFB; /* LP o subFB =  part of FB used to build random relations */
      75             :   int sfb_chg; /* need to change subFB ? */
      76             :   GEN perm; /* permutation of LP used to represent relations [updated by
      77             :                hnfspec/hnfadd: dense rows come first] */
      78             :   GEN idealperm; /* permutation of ideals under field automorphisms */
      79             :   GEN minidx; /* minidx[i] min ideal in orbit of LP[i] under field autom */
      80             :   subFB_t *allsubFB; /* all subFB's used */
      81             :   GEN embperm; /* permutations of the complex embeddings */
      82             :   long MAXDEPSIZESFB; /* # trials before increasing subFB */
      83             :   long MAXDEPSFB; /* MAXDEPSIZESFB / DEPSFBDIV, # trials befor rotating subFB */
      84             : } FB_t;
      85             : 
      86             : enum { sfb_CHANGE = 1, sfb_INCREASE = 2 };
      87             : 
      88             : typedef struct REL_t {
      89             :   GEN R; /* relation vector as t_VECSMALL; clone */
      90             :   long nz; /* index of first nonzero elt in R (hash) */
      91             :   GEN m; /* pseudo-minimum yielding the relation; clone */
      92             :   long relorig; /* relation this one is an image of */
      93             :   long relaut; /* automorphim used to compute this relation from the original */
      94             :   GEN emb; /* archimedean embeddings */
      95             :   GEN junk[2]; /*make sure sizeof(struct) is a power of two.*/
      96             : } REL_t;
      97             : 
      98             : typedef struct RELCACHE_t {
      99             :   REL_t *chk; /* last checkpoint */
     100             :   REL_t *base; /* first rel found */
     101             :   REL_t *last; /* last rel found so far */
     102             :   REL_t *end; /* target for last relation. base <= last <= end */
     103             :   size_t len; /* number of rels pre-allocated in base */
     104             :   long relsup; /* how many linearly dependent relations we allow */
     105             :   GEN basis; /* mod p basis (generating family actually) */
     106             :   ulong missing; /* missing vectors in generating family above */
     107             : } RELCACHE_t;
     108             : 
     109             : typedef struct FP_t {
     110             :   double **q;
     111             :   GEN x;
     112             :   double *y;
     113             :   double *z;
     114             :   double *v;
     115             : } FP_t;
     116             : 
     117             : typedef struct RNDREL_t {
     118             :   long jid;
     119             :   GEN ex;
     120             : } RNDREL_t;
     121             : 
     122             : static void
     123           0 : wr_rel(GEN e)
     124             : {
     125           0 :   long i, l = lg(e);
     126           0 :   for (i = 1; i < l; i++)
     127           0 :     if (e[i]) err_printf("%ld^%ld ",i,e[i]);
     128           0 : }
     129             : static void
     130           0 : dbg_newrel(RELCACHE_t *cache)
     131             : {
     132           0 :   if (DEBUGLEVEL > 1)
     133             :   {
     134           0 :     err_printf("\n++++ cglob = %ld\nrel = ", cache->last - cache->base);
     135           0 :     wr_rel(cache->last->R);
     136           0 :     err_printf("\n");
     137             :   }
     138             :   else
     139           0 :     err_printf("%ld ", cache->last - cache->base);
     140           0 : }
     141             : 
     142             : static void
     143       63603 : delete_cache(RELCACHE_t *M)
     144             : {
     145             :   REL_t *rel;
     146     1028259 :   for (rel = M->base+1; rel <= M->last; rel++)
     147             :   {
     148      964656 :     gunclone(rel->R);
     149      964659 :     if (rel->m) gunclone(rel->m);
     150             :   }
     151       63603 :   pari_free((void*)M->base); M->base = NULL;
     152       63603 : }
     153             : 
     154             : static void
     155       65779 : delete_FB(FB_t *F)
     156             : {
     157             :   subFB_t *s, *sold;
     158      132071 :   for (s = F->allsubFB; s; s = sold) { sold = s->old; pari_free(s); }
     159       65780 :   gunclone(F->minidx);
     160       65780 :   gunclone(F->idealperm);
     161       65780 : }
     162             : 
     163             : static void
     164       63688 : reallocate(RELCACHE_t *M, long len)
     165             : {
     166       63688 :   M->len = len;
     167       63688 :   if (!M->base)
     168       63602 :     M->base = (REL_t*)pari_malloc((len+1) * sizeof(REL_t));
     169             :   else
     170             :   {
     171          86 :     size_t l = M->last - M->base, c = M->chk - M->base, e = M->end - M->base;
     172          86 :     pari_realloc_ip((void**)&M->base, (len+1) * sizeof(REL_t));
     173          86 :     M->last = M->base + l;
     174          86 :     M->chk  = M->base + c;
     175          86 :     M->end  = M->base + e;
     176             :   }
     177       63689 : }
     178             : 
     179             : #define pr_get_smallp(pr) gel(pr,1)[2]
     180             : 
     181             : /* don't take P|p all other Q|p are already there */
     182             : static int
     183      271337 : bad_subFB(FB_t *F, long t)
     184             : {
     185      271337 :   GEN LP, P = gel(F->LP,t);
     186      271337 :   long p = pr_get_smallp(P);
     187      271337 :   LP = gel(F->LV,p);
     188      271337 :   return (isclone(LP) && t == F->iLP[p] + lg(LP)-1);
     189             : }
     190             : 
     191             : static void
     192       66292 : assign_subFB(FB_t *F, GEN yes, long iyes)
     193             : {
     194       66292 :   long i, lv = sizeof(subFB_t) + iyes*sizeof(long); /* for struct + GEN */
     195       66292 :   subFB_t *s = (subFB_t *)pari_malloc(lv);
     196       66293 :   s->subFB = (GEN)&s[1];
     197       66293 :   s->old = F->allsubFB; F->allsubFB = s;
     198      286012 :   for (i = 0; i < iyes; i++) s->subFB[i] = yes[i];
     199       66293 :   F->subFB = s->subFB;
     200       66293 :   F->MAXDEPSIZESFB = (iyes-1) * DEPSIZESFBMULT;
     201       66293 :   F->MAXDEPSFB = F->MAXDEPSIZESFB / DEPSFBDIV;
     202       66293 : }
     203             : 
     204             : /* Determine the permutation of the ideals made by each field automorphism */
     205             : static GEN
     206       65779 : FB_aut_perm(FB_t *F, GEN auts, GEN cyclic)
     207             : {
     208       65779 :   long i, j, m, KC = F->KC, nauts = lg(auts)-1;
     209       65779 :   GEN minidx, perm = zero_Flm_copy(KC, nauts);
     210             : 
     211       65779 :   if (!nauts) { F->minidx = gclone(identity_zv(KC)); return cgetg(1,t_MAT); }
     212       41517 :   minidx = zero_Flv(KC);
     213       90292 :   for (m = 1; m < lg(cyclic); m++)
     214             :   {
     215       48776 :     GEN thiscyc = gel(cyclic, m);
     216       48776 :     long k0 = thiscyc[1];
     217       48776 :     GEN aut = gel(auts, k0), permk0 = gel(perm, k0), ppermk;
     218       48776 :     i = 1;
     219      209422 :     while (i <= KC)
     220             :     {
     221      160647 :       pari_sp av2 = avma;
     222      160647 :       GEN seen = zero_Flv(KC), P = gel(F->LP, i);
     223      160649 :       long imin = i, p, f, l;
     224      160649 :       p = pr_get_smallp(P);
     225      160649 :       f = pr_get_f(P);
     226             :       do
     227             :       {
     228      474036 :         if (++i > KC) break;
     229      425259 :         P = gel(F->LP, i);
     230             :       }
     231      425259 :       while (p == pr_get_smallp(P) && f == pr_get_f(P));
     232      634667 :       for (j = imin; j < i; j++)
     233             :       {
     234      474030 :         GEN img = ZM_ZC_mul(aut, pr_get_gen(gel(F->LP, j)));
     235     1655505 :         for (l = imin; l < i; l++)
     236     1655505 :           if (!seen[l] && ZC_prdvd(img, gel(F->LP, l)))
     237             :           {
     238      474018 :             seen[l] = 1; permk0[j] = l; break;
     239             :           }
     240             :       }
     241      160637 :       set_avma(av2);
     242             :     }
     243       67796 :     for (ppermk = permk0, i = 2; i < lg(thiscyc); i++)
     244             :     {
     245       19021 :       GEN permk = gel(perm, thiscyc[i]);
     246      382700 :       for (j = 1; j <= KC; j++) permk[j] = permk0[ppermk[j]];
     247       19021 :       ppermk = permk;
     248             :     }
     249             :   }
     250      306200 :   for (j = 1; j <= KC; j++)
     251             :   {
     252      264684 :     if (minidx[j]) continue;
     253      127256 :     minidx[j] = j;
     254      355291 :     for (i = 1; i <= nauts; i++) minidx[coeff(perm, j, i)] = j;
     255             :   }
     256       41516 :   F->minidx = gclone(minidx); return perm;
     257             : }
     258             : 
     259             : /* set subFB.
     260             :  * Fill F->perm (if != NULL): primes ideals sorted by increasing norm (except
     261             :  * the ones in subFB come first [dense rows for hnfspec]) */
     262             : static void
     263       65776 : subFBgen(FB_t *F, GEN auts, GEN cyclic, double PROD, long minsFB)
     264             : {
     265             :   GEN y, perm, yes, no;
     266       65776 :   long i, j, k, iyes, ino, lv = F->KC + 1;
     267             :   double prod;
     268             :   pari_sp av;
     269             : 
     270       65776 :   F->LP   = cgetg(lv, t_VEC);
     271       65779 :   F->L_jid = F->perm = cgetg(lv, t_VECSMALL);
     272       65779 :   av = avma;
     273       65779 :   y = cgetg(lv,t_COL); /* Norm P */
     274      309756 :   for (k=0, i=1; i <= F->KCZ; i++)
     275             :   {
     276      243977 :     GEN LP = gel(F->LV,F->FB[i]);
     277      243977 :     long l = lg(LP);
     278      705434 :     for (j = 1; j < l; j++)
     279             :     {
     280      461468 :       GEN P = gel(LP,j);
     281      461468 :       k++;
     282      461468 :       gel(y,k) = pr_norm(P);
     283      461457 :       gel(F->LP,k) = P;
     284             :     }
     285             :   }
     286             :   /* perm sorts LP by increasing norm */
     287       65779 :   perm = indexsort(y);
     288       65779 :   no  = cgetg(lv, t_VECSMALL); ino  = 1;
     289       65779 :   yes = cgetg(lv, t_VECSMALL); iyes = 1;
     290       65779 :   prod = 1.0;
     291      301199 :   for (i = 1; i < lv; i++)
     292             :   {
     293      271338 :     long t = perm[i];
     294      271338 :     if (bad_subFB(F, t)) { no[ino++] = t; continue; }
     295             : 
     296      151796 :     yes[iyes++] = t;
     297      151796 :     prod *= (double)itos(gel(y,t));
     298      151797 :     if (iyes > minsFB && prod > PROD) break;
     299             :   }
     300       65779 :   setlg(yes, iyes);
     301      217576 :   for (j=1; j<iyes; j++)     F->perm[j] = yes[j];
     302      185321 :   for (i=1; i<ino; i++, j++) F->perm[j] =  no[i];
     303      255938 :   for (   ; j<lv; j++)       F->perm[j] =  perm[j];
     304       65779 :   F->allsubFB = NULL;
     305       65779 :   F->idealperm = gclone(FB_aut_perm(F, auts, cyclic));
     306       65779 :   if (iyes) assign_subFB(F, yes, iyes);
     307       65780 :   set_avma(av);
     308       65780 : }
     309             : static int
     310        2400 : subFB_change(FB_t *F)
     311             : {
     312        2400 :   long i, iyes, minsFB, lv = F->KC + 1, l = lg(F->subFB)-1;
     313        2400 :   pari_sp av = avma;
     314        2400 :   GEN yes, L_jid = F->L_jid, present = zero_zv(lv-1);
     315             : 
     316        2400 :   switch (F->sfb_chg)
     317             :   {
     318         128 :     case sfb_INCREASE: minsFB = l + 1; break;
     319        2272 :     default: minsFB = l; break;
     320             :   }
     321             : 
     322        2400 :   yes = cgetg(minsFB+1, t_VECSMALL); iyes = 1;
     323        2400 :   if (L_jid)
     324             :   {
     325        7452 :     for (i = 1; i < lg(L_jid); i++)
     326             :     {
     327        6786 :       long l = L_jid[i];
     328        6786 :       yes[iyes++] = l;
     329        6786 :       present[l] = 1;
     330        6786 :       if (iyes > minsFB) break;
     331             :     }
     332             :   }
     333           0 :   else i = 1;
     334        2400 :   if (iyes <= minsFB)
     335             :   {
     336         968 :     for ( ; i < lv; i++)
     337             :     {
     338         954 :       long l = F->perm[i];
     339         954 :       if (present[l]) continue;
     340         755 :       yes[iyes++] = l;
     341         755 :       if (iyes > minsFB) break;
     342             :     }
     343         666 :     if (i == lv) return 0;
     344             :   }
     345        2386 :   if (zv_equal(F->subFB, yes))
     346             :   {
     347        1873 :     if (DEBUGLEVEL) err_printf("\n*** NOT Changing sub factor base\n");
     348             :   }
     349             :   else
     350             :   {
     351         513 :     if (DEBUGLEVEL) err_printf("\n*** Changing sub factor base\n");
     352         513 :     assign_subFB(F, yes, iyes);
     353             :   }
     354        2386 :   F->sfb_chg = 0; return gc_bool(av, 1);
     355             : }
     356             : 
     357             : /* make sure enough room to store n more relations */
     358             : static void
     359      128545 : pre_allocate(RELCACHE_t *cache, size_t n)
     360             : {
     361      128545 :   size_t len = (cache->last - cache->base) + n;
     362      128545 :   if (len >= cache->len) reallocate(cache, len << 1);
     363      128545 : }
     364             : 
     365             : void
     366      133793 : init_GRHcheck(GRHcheck_t *S, long N, long R1, double LOGD)
     367             : {
     368      133793 :   const double c1 = M_PI*M_PI/2;
     369      133793 :   const double c2 = 3.663862376709;
     370      133793 :   const double c3 = 3.801387092431; /* Euler + log(8*Pi)*/
     371      133793 :   S->clone = 0;
     372      133793 :   S->cN = R1*c2 + N*c1;
     373      133793 :   S->cD = LOGD - N*c3 - R1*M_PI/2;
     374      133793 :   S->maxprimes = 16000; /* sufficient for LIMC=176081*/
     375      133793 :   S->primes = (GRHprime_t*)pari_malloc(S->maxprimes*sizeof(*S->primes));
     376      133796 :   S->nprimes = 0;
     377      133796 :   S->limp = 0;
     378      133796 :   u_forprime_init(&S->P, 2, ULONG_MAX);
     379      133794 : }
     380             : 
     381             : void
     382      133797 : free_GRHcheck(GRHcheck_t *S)
     383             : {
     384      133797 :   if (S->clone)
     385             :   {
     386       63560 :     long i = S->nprimes;
     387             :     GRHprime_t *pr;
     388     7519806 :     for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--) gunclone(pr->dec);
     389             :   }
     390      133801 :   pari_free(S->primes);
     391      133797 : }
     392             : 
     393             : int
     394     1525495 : GRHok(GRHcheck_t *S, double L, double SA, double SB)
     395             : {
     396     1525495 :   return (S->cD + (S->cN + 2*SB) / L - 2*SA < -1e-8);
     397             : }
     398             : 
     399             : /* Return factorization pattern of p: [f,n], where n[i] primes of
     400             :  * residue degree f[i] */
     401             : static GEN
     402     7451103 : get_fs(GEN nf, GEN P, GEN index, ulong p)
     403             : {
     404             :   long j, k, f, n, l;
     405             :   GEN fs, ns;
     406             : 
     407     7451103 :   if (umodiu(index, p))
     408             :   { /* easy case: p does not divide index */
     409     7413330 :     GEN F = Flx_degfact(ZX_to_Flx(P,p), p);
     410     7414245 :     fs = gel(F,1); l = lg(fs);
     411             :   }
     412             :   else
     413             :   {
     414       37654 :     GEN F = idealprimedec(nf, utoipos(p));
     415       37701 :     l = lg(F);
     416       37701 :     fs = cgetg(l, t_VECSMALL);
     417      118264 :     for (j = 1; j < l; j++) fs[j] = pr_get_f(gel(F,j));
     418             :   }
     419     7451946 :   ns = cgetg(l, t_VECSMALL);
     420     7448785 :   f = fs[1]; n = 1;
     421    13782504 :   for (j = 2, k = 1; j < l; j++)
     422     6333719 :     if (fs[j] == f)
     423     4612926 :       n++;
     424             :     else
     425             :     {
     426     1720793 :       ns[k] = n; fs[k] = f; k++;
     427     1720793 :       f = fs[j]; n = 1;
     428             :     }
     429     7448785 :   ns[k] = n; fs[k] = f; k++;
     430     7448785 :   setlg(fs, k);
     431     7448691 :   setlg(ns, k); return mkvec2(fs,ns);
     432             : }
     433             : 
     434             : /* cache data for all rational primes up to the LIM */
     435             : static void
     436      914322 : cache_prime_dec(GRHcheck_t *S, ulong LIM, GEN nf)
     437             : {
     438      914322 :   pari_sp av = avma;
     439             :   GRHprime_t *pr;
     440             :   GEN index, P;
     441             :   double nb;
     442             : 
     443      914322 :   if (S->limp >= LIM) return;
     444      327213 :   S->clone = 1;
     445      327213 :   nb = primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
     446      327229 :   GRH_ensure(S, nb+1); /* room for one extra prime */
     447      327228 :   P = nf_get_pol(nf);
     448      327224 :   index = nf_get_index(nf);
     449      327222 :   for (pr = S->primes + S->nprimes;;)
     450     7124587 :   {
     451     7451809 :     ulong p = u_forprime_next(&(S->P));
     452     7450450 :     pr->p = p;
     453     7450450 :     pr->logp = log((double)p);
     454     7450450 :     pr->dec = gclone(get_fs(nf, P, index, p));
     455     7451912 :     S->nprimes++;
     456     7451912 :     pr++;
     457     7451912 :     set_avma(av);
     458             :     /* store up to nextprime(LIM) included */
     459     7451810 :     if (p >= LIM) { S->limp = p; break; }
     460             :   }
     461             : }
     462             : 
     463             : static double
     464     2243074 : tailresback(long R1, long R2, double rK, long C, double C2, double C3, double r1K, double r2K, double logC, double logC2, double logC3)
     465             : {
     466     2243074 :   const double  rQ = 1.83787706641;
     467     2243074 :   const double r1Q = 1.98505372441;
     468     2243074 :   const double r2Q = 1.07991541347;
     469     4486148 :   return fabs((R1+R2-1)*(12*logC3+4*logC2-9*logC-6)/(2*C*logC3)
     470     2243074 :          + (rK-rQ)*(6*logC2 + 5*logC + 2)/(C*logC3)
     471     2243074 :          - R2*(6*logC2+11*logC+6)/(C2*logC2)
     472     2243074 :          - 2*(r1K-r1Q)*(3*logC2 + 4*logC + 2)/(C2*logC3)
     473     2243074 :          + (R1+R2-1)*(12*logC3+40*logC2+45*logC+18)/(6*C3*logC3)
     474     2243074 :          + (r2K-r2Q)*(2*logC2 + 3*logC + 2)/(C3*logC3));
     475             : }
     476             : 
     477             : static double
     478     1121541 : tailres(long R1, long R2, double al2K, double rKm, double rKM, double r1Km,
     479             :         double r1KM, double r2Km, double r2KM, double C, long i)
     480             : {
     481             :   /* C >= 3*2^i, lower bound for eint1(log(C)/2) */
     482             :   /* for(i=0,30,print(eint1(log(3*2^i)/2))) */
     483             :   static double tab[] = {
     484             :     0.50409264803,
     485             :     0.26205336997,
     486             :     0.14815491171,
     487             :     0.08770540561,
     488             :     0.05347651832,
     489             :     0.03328934284,
     490             :     0.02104510690,
     491             :     0.01346475900,
     492             :     0.00869778586,
     493             :     0.00566279855,
     494             :     0.00371111950,
     495             :     0.00244567837,
     496             :     0.00161948049,
     497             :     0.00107686891,
     498             :     0.00071868750,
     499             :     0.00048119961,
     500             :     0.00032312188,
     501             :     0.00021753772,
     502             :     0.00014679818,
     503             :     9.9272855581E-5,
     504             :     6.7263969995E-5,
     505             :     4.5656812967E-5,
     506             :     3.1041124593E-5,
     507             :     2.1136011590E-5,
     508             :     1.4411645381E-5,
     509             :     9.8393304088E-6,
     510             :     6.7257395409E-6,
     511             :     4.6025878272E-6,
     512             :     3.1529719271E-6,
     513             :     2.1620490021E-6,
     514             :     1.4839266071E-6
     515             :   };
     516     1121541 :   const double logC = log(C), logC2 = logC*logC, logC3 = logC*logC2;
     517     1121541 :   const double C2 = C*C, C3 = C*C2;
     518     1121541 :   double E1 = i >30? 0: tab[i];
     519     1121541 :   return al2K*((33*logC2+22*logC+8)/(8*logC3*sqrt(C))+15*E1/16)
     520     1121541 :     + maxdd(tailresback(rKm,r1KM,r2Km, C,C2,C3,R1,R2,logC,logC2,logC3),
     521     1121548 :             tailresback(rKM,r1Km,r2KM, C,C2,C3,R1,R2,logC,logC2,logC3))/2
     522     1121548 :     + ((R1+R2-1)*4*C+R2)*(C2+6*logC)/(4*C2*C2*logC2);
     523             : }
     524             : 
     525             : static long
     526       63558 : primeneeded(long N, long R1, long R2, double LOGD)
     527             : {
     528       63558 :   const double lim = 0.25; /* should be log(2)/2 == 0.34657... */
     529       63558 :   const double al2K =  0.3526*LOGD - 0.8212*N + 4.5007;
     530       63558 :   const double  rKm = -1.0155*LOGD + 2.1042*N - 8.3419;
     531       63558 :   const double  rKM = -0.5   *LOGD + 1.2076*N + 1;
     532       63558 :   const double r1Km = -       LOGD + 1.4150*N;
     533       63558 :   const double r1KM = -       LOGD + 1.9851*N;
     534       63558 :   const double r2Km = -       LOGD + 0.9151*N;
     535       63558 :   const double r2KM = -       LOGD + 1.0800*N;
     536       63558 :   long Cmin = 3, Cmax = 3, i = 0;
     537      570199 :   while (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, Cmax, i) > lim)
     538             :   {
     539      506641 :     Cmin = Cmax;
     540      506641 :     Cmax *= 2;
     541      506641 :     i++;
     542             :   }
     543       63557 :   i--;
     544      614929 :   while (Cmax - Cmin > 1)
     545             :   {
     546      551370 :     long t = (Cmin + Cmax)/2;
     547      551370 :     if (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, t, i) > lim)
     548      341635 :       Cmin = t;
     549             :     else
     550      209737 :       Cmax = t;
     551             :   }
     552       63559 :   return Cmax;
     553             : }
     554             : 
     555             : /* ~ 1 / Res(s = 1, zeta_K) */
     556             : static GEN
     557       63560 : compute_invres(GRHcheck_t *S, long LIMC)
     558             : {
     559       63560 :   pari_sp av = avma;
     560       63560 :   double loginvres = 0.;
     561             :   GRHprime_t *pr;
     562             :   long i;
     563       63560 :   double logLIMC = log((double)LIMC);
     564       63560 :   double logLIMC2 = logLIMC*logLIMC, denc;
     565             :   double c0, c1, c2;
     566       63560 :   denc = 1/(pow((double)LIMC, 3.) * logLIMC * logLIMC2);
     567       63560 :   c2 = (    logLIMC2 + 3 * logLIMC / 2 + 1) * denc;
     568       63560 :   denc *= LIMC;
     569       63560 :   c1 = (3 * logLIMC2 + 4 * logLIMC     + 2) * denc;
     570       63560 :   denc *= LIMC;
     571       63560 :   c0 = (3 * logLIMC2 + 5 * logLIMC / 2 + 1) * denc;
     572     7463547 :   for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--)
     573             :   {
     574             :     GEN dec, fs, ns;
     575             :     long addpsi;
     576             :     double addpsi1, addpsi2;
     577     7455679 :     double logp = pr->logp, NPk;
     578     7455679 :     long j, k, limp = logLIMC/logp;
     579     7455679 :     ulong p = pr->p, p2 = p*p;
     580     7455679 :     if (limp < 1) break;
     581     7399987 :     dec = pr->dec;
     582     7399987 :     fs = gel(dec, 1); ns = gel(dec, 2);
     583     7399987 :     loginvres += 1./p;
     584             :     /* NB: limp = 1 nearly always and limp > 2 for very few primes */
     585     8756678 :     for (k=2, NPk = p; k <= limp; k++) { NPk *= p; loginvres += 1/(k * NPk); }
     586     7399987 :     addpsi = limp;
     587     7399987 :     addpsi1 = p *(pow((double)p , (double)limp)-1)/(p -1);
     588     7399987 :     addpsi2 = p2*(pow((double)p2, (double)limp)-1)/(p2-1);
     589     7399987 :     j = lg(fs);
     590    16510195 :     while (--j > 0)
     591             :     {
     592             :       long f, nb, kmax;
     593             :       double NP, NP2, addinvres;
     594     9110208 :       f = fs[j]; if (f > limp) continue;
     595     3961148 :       nb = ns[j];
     596     3961148 :       NP = pow((double)p, (double)f);
     597     3961148 :       addinvres = 1/NP;
     598     3961148 :       kmax = limp / f;
     599     4833868 :       for (k=2, NPk = NP; k <= kmax; k++) { NPk *= NP; addinvres += 1/(k*NPk); }
     600     3961148 :       NP2 = NP*NP;
     601     3961148 :       loginvres -= nb * addinvres;
     602     3961148 :       addpsi -= nb * f * kmax;
     603     3961148 :       addpsi1 -= nb*(f*NP *(pow(NP ,(double)kmax)-1)/(NP -1));
     604     3961148 :       addpsi2 -= nb*(f*NP2*(pow(NP2,(double)kmax)-1)/(NP2-1));
     605             :     }
     606     7399987 :     loginvres -= (addpsi*c0 - addpsi1*c1 + addpsi2*c2)*logp;
     607             :   }
     608       63560 :   return gerepileuptoleaf(av, mpexp(dbltor(loginvres)));
     609             : }
     610             : 
     611             : static long
     612       63559 : nthideal(GRHcheck_t *S, GEN nf, long n)
     613             : {
     614       63559 :   pari_sp av = avma;
     615       63559 :   GEN P = nf_get_pol(nf);
     616       63558 :   ulong p = 0, *vecN = (ulong*)const_vecsmall(n, LONG_MAX);
     617       63558 :   long i, N = poldegree(P, -1);
     618       63557 :   for (i = 0; ; i++)
     619      228628 :   {
     620             :     GRHprime_t *pr;
     621             :     GEN fs;
     622      292185 :     cache_prime_dec(S, p+1, nf);
     623      292187 :     pr = S->primes + i;
     624      292187 :     fs = gel(pr->dec, 1);
     625      292187 :     p = pr->p;
     626      292187 :     if (fs[1] != N)
     627             :     {
     628      196047 :       GEN ns = gel(pr->dec, 2);
     629      196047 :       long k, l, j = lg(fs);
     630      439721 :       while (--j > 0)
     631             :       {
     632      243673 :         ulong NP = upowuu(p, fs[j]);
     633             :         long nf;
     634      243674 :         if (!NP) continue;
     635      747839 :         for (k = 1; k <= n; k++) if (vecN[k] > NP) break;
     636      243282 :         if (k > n) continue;
     637             :         /* vecN[k] <= NP */
     638      157643 :         nf = ns[j]; /*#{primes of norme NP} = nf, insert them here*/
     639      352799 :         for (l = k+nf; l <= n; l++) vecN[l] = vecN[l-nf];
     640      398174 :         for (l = 0; l < nf && k+l <= n; l++) vecN[k+l] = NP;
     641      362562 :         while (l <= k) vecN[l++] = NP;
     642             :       }
     643             :     }
     644      292188 :     if (p > vecN[n]) break;
     645             :   }
     646       63560 :   return gc_long(av, vecN[n]);
     647             : }
     648             : 
     649             : /* Compute FB, LV, iLP + KC*. Reset perm
     650             :  * C2: bound for norm of tested prime ideals (includes be_honest())
     651             :  * C1: bound for p, such that P|p (NP <= C2) used to build relations */
     652             : static void
     653       65780 : FBgen(FB_t *F, GEN nf, long N, ulong C1, ulong C2, GRHcheck_t *S)
     654             : {
     655             :   GRHprime_t *pr;
     656             :   long i, ip;
     657             :   GEN prim;
     658       65780 :   const double L = log((double)C2 + 0.5);
     659             : 
     660       65780 :   cache_prime_dec(S, C2, nf);
     661       65780 :   pr = S->primes;
     662       65780 :   F->sfb_chg = 0;
     663       65780 :   F->FB  = cgetg(C2+1, t_VECSMALL);
     664       65780 :   F->iLP = cgetg(C2+1, t_VECSMALL);
     665       65780 :   F->LV = zerovec(C2);
     666             : 
     667       65780 :   prim = icopy(gen_1);
     668       65781 :   i = ip = 0;
     669       65781 :   F->KC = F->KCZ = 0;
     670      432417 :   for (;; pr++) /* p <= C2 */
     671      432417 :   {
     672      498198 :     ulong p = pr->p;
     673             :     long k, l, m;
     674             :     GEN LP, nb, f;
     675             : 
     676      498198 :     if (!F->KC && p > C1) { F->KCZ = i; F->KC = ip; }
     677      498198 :     if (p > C2) break;
     678             : 
     679      461083 :     if (DEBUGLEVEL>1) err_printf(" %ld",p);
     680             : 
     681      461085 :     f = gel(pr->dec, 1); nb = gel(pr->dec, 2);
     682      461085 :     if (f[1] == N)
     683             :     {
     684      144961 :       if (p == C2) break;
     685      136449 :       continue; /* p inert */
     686             :     }
     687      316124 :     l = (long)(L/pr->logp); /* p^f <= C2  <=> f <= l */
     688      576795 :     for (k=0, m=1; m < lg(f) && f[m]<=l; m++) k += nb[m];
     689      316124 :     if (!k)
     690             :     { /* too inert to appear in FB */
     691       72125 :       if (p == C2) break;
     692       71495 :       continue;
     693             :     }
     694      243999 :     prim[2] = p; LP = idealprimedec_limit_f(nf,prim, l);
     695             :     /* keep noninert ideals with Norm <= C2 */
     696      243996 :     if (m == lg(f)) setisclone(LP); /* flag it: all prime divisors in FB */
     697      243996 :     F->FB[++i]= p;
     698      243996 :     gel(F->LV,p) = LP;
     699      243996 :     F->iLP[p] = ip; ip += k;
     700      243996 :     if (p == C2) break;
     701             :   }
     702       65780 :   if (!F->KC) { F->KCZ = i; F->KC = ip; }
     703             :   /* Note F->KC > 0 otherwise GRHchk is false */
     704       65780 :   setlg(F->FB, F->KCZ+1); F->KCZ2 = i;
     705       65780 :   F->prodZ = zv_prod_Z(F->FB);
     706       65775 :   if (DEBUGLEVEL>1)
     707             :   {
     708           0 :     err_printf("\n");
     709           0 :     if (DEBUGLEVEL>6)
     710             :     {
     711           0 :       err_printf("########## FACTORBASE ##########\n\n");
     712           0 :       err_printf("KC2=%ld, KC=%ld, KCZ=%ld, KCZ2=%ld\n",
     713             :                   ip, F->KC, F->KCZ, F->KCZ2);
     714           0 :       for (i=1; i<=F->KCZ; i++) err_printf("++ LV[%ld] = %Ps",i,gel(F->LV,F->FB[i]));
     715             :     }
     716             :   }
     717       65775 :   F->perm = NULL; F->L_jid = NULL;
     718       65775 : }
     719             : 
     720             : static int
     721      492814 : GRHchk(GEN nf, GRHcheck_t *S, ulong LIMC)
     722             : {
     723      492814 :   double logC = log((double)LIMC), SA = 0, SB = 0;
     724      492814 :   GRHprime_t *pr = S->primes;
     725             : 
     726      492814 :   cache_prime_dec(S, LIMC, nf);
     727      492809 :   for (pr = S->primes;; pr++)
     728     3030831 :   {
     729     3523640 :     ulong p = pr->p;
     730             :     GEN dec, fs, ns;
     731             :     double logCslogp;
     732             :     long j;
     733             : 
     734     3523640 :     if (p > LIMC) break;
     735     3136355 :     dec = pr->dec; fs = gel(dec, 1); ns = gel(dec,2);
     736     3136355 :     logCslogp = logC/pr->logp;
     737     4937017 :     for (j = 1; j < lg(fs); j++)
     738             :     {
     739     3864471 :       long f = fs[j], M, nb;
     740             :       double logNP, q, A, B;
     741     3864471 :       if (f > logCslogp) break;
     742     1800658 :       logNP = f * pr->logp;
     743     1800658 :       q = 1/sqrt((double)upowuu(p, f));
     744     1800662 :       A = logNP * q; B = logNP * A; M = (long)(logCslogp/f);
     745     1800662 :       if (M > 1)
     746             :       {
     747      374065 :         double inv1_q = 1 / (1-q);
     748      374065 :         A *= (1 - pow(q, (double)M)) * inv1_q;
     749      374065 :         B *= (1 - pow(q, (double)M)*(M+1 - M*q)) * inv1_q * inv1_q;
     750             :       }
     751     1800662 :       nb = ns[j];
     752     1800662 :       SA += nb * A;
     753     1800662 :       SB += nb * B;
     754             :     }
     755     3136359 :     if (p == LIMC) break;
     756             :   }
     757      492813 :   return GRHok(S, logC, SA, SB);
     758             : }
     759             : 
     760             : /*  SMOOTH IDEALS */
     761             : static void
     762    12433454 : store(long i, long e, FACT *fact)
     763             : {
     764    12433454 :   ++fact[0].pr;
     765    12433454 :   fact[fact[0].pr].pr = i; /* index */
     766    12433454 :   fact[fact[0].pr].ex = e; /* exponent */
     767    12433454 : }
     768             : 
     769             : /* divide out x by all P|p, where x as in can_factor().  k = v_p(Nx) */
     770             : static int
     771     7263089 : divide_p_elt(GEN LP, long ip, long k, GEN m, FACT *fact)
     772             : {
     773     7263089 :   long j, l = lg(LP);
     774    30535617 :   for (j=1; j<l; j++)
     775             :   {
     776    30503543 :     GEN P = gel(LP,j);
     777    30503543 :     long v = ZC_nfval(m, P);
     778    30501416 :     if (!v) continue;
     779    11687936 :     store(ip + j, v, fact); /* v = v_P(m) > 0 */
     780    11689352 :     k -= v * pr_get_f(P);
     781    11689617 :     if (!k) return 1;
     782             :   }
     783       32074 :   return 0;
     784             : }
     785             : static int
     786      163547 : divide_p_id(GEN LP, long ip, long k, GEN nf, GEN I, FACT *fact)
     787             : {
     788      163547 :   long j, l = lg(LP);
     789      246548 :   for (j=1; j<l; j++)
     790             :   {
     791      238708 :     GEN P = gel(LP,j);
     792      238708 :     long v = idealval(nf,I, P);
     793      238707 :     if (!v) continue;
     794      158946 :     store(ip + j, v, fact); /* v = v_P(I) > 0 */
     795      158947 :     k -= v * pr_get_f(P);
     796      158947 :     if (!k) return 1;
     797             :   }
     798        7840 :   return 0;
     799             : }
     800             : static int
     801      542471 : divide_p_quo(GEN LP, long ip, long k, GEN nf, GEN I, GEN m, FACT *fact)
     802             : {
     803      542471 :   long j, l = lg(LP);
     804      816866 :   for (j=1; j<l; j++)
     805             :   {
     806      816525 :     GEN P = gel(LP,j);
     807      816525 :     long v = ZC_nfval(m, P);
     808      816525 :     if (!v) continue;
     809      573527 :     v -= idealval(nf,I, P);
     810      573527 :     if (!v) continue;
     811      566861 :     store(ip + j, v, fact); /* v = v_P(m / I) > 0 */
     812      566861 :     k -= v * pr_get_f(P);
     813      566861 :     if (!k) return 1;
     814             :   }
     815         341 :   return 0;
     816             : }
     817             : 
     818             : /* |*N| != 0 is the norm of a primitive ideal, in particular not divisible by
     819             :  * any inert prime. Is |*N| a smooth rational integer wrt F ?
     820             :  */
     821             : static int
     822    32717361 : Z_issmooth_prod(GEN N, GEN P)
     823             : {
     824    32717361 :   P = gcdii(P,N);
     825   101519000 :   while (!is_pm1(P))
     826             :   {
     827    68802374 :     N = diviiexact(N, P);
     828    68802246 :     P = gcdii(N, P);
     829             :   }
     830    32714144 :   return is_pm1(N);
     831             : }
     832             : 
     833             : static int
     834     7969033 : divide_p(FB_t *F, long p, long k, GEN nf, GEN I, GEN m, FACT *fact)
     835             : {
     836     7969033 :   GEN LP = gel(F->LV,p);
     837     7969033 :   long ip = F->iLP[p];
     838     7969033 :   if (!m) return divide_p_id (LP,ip,k,nf,I,fact);
     839     7805486 :   if (!I) return divide_p_elt(LP,ip,k,m,fact);
     840      542441 :   return divide_p_quo(LP,ip,k,nf,I,m,fact);
     841             : }
     842             : 
     843             : /* Let x = m if I == NULL,
     844             :  *         I if m == NULL,
     845             :  *         m/I otherwise.
     846             :  * Can we factor the integral primitive ideal x ? |N| = Norm x > 0 */
     847             : static long
     848    33483654 : can_factor(FB_t *F, GEN nf, GEN I, GEN m, GEN N, FACT *fact)
     849             : {
     850             :   GEN f, p, e;
     851             :   long i, l;
     852    33483654 :   fact[0].pr = 0;
     853    33483654 :   if (is_pm1(N)) return 1;
     854    32717357 :   if (!Z_issmooth_prod(N, F->prodZ)) return 0;
     855     3927572 :   f = absZ_factor(N); p = gel(f,1); e = gel(f,2); l = lg(p);
     856    11857100 :   for (i = 1; i < l; i++)
     857     7969012 :     if (!divide_p(F, itou(gel(p,i)), itou(gel(e,i)), nf, I, m, fact))
     858             :     {
     859       39671 :       if (DEBUGLEVEL > 1) err_printf(".");
     860       39671 :       return 0;
     861             :     }
     862     3888088 :   return 1;
     863             : }
     864             : 
     865             : /* can we factor m/I ? [m in I from idealpseudomin_nonscalar], NI = norm I */
     866             : static long
     867     1398349 : factorgen(FB_t *F, GEN nf, GEN I, GEN NI, GEN m, FACT *fact)
     868             : {
     869     1398349 :   long e, r1 = nf_get_r1(nf);
     870     1398350 :   GEN M = nf_get_M(nf);
     871     1398350 :   GEN N = divri(embed_norm(RgM_RgC_mul(M,m), r1), NI); /* ~ N(m/I) */
     872     1398352 :   N = grndtoi(N, &e);
     873     1398351 :   if (e > -32)
     874             :   {
     875           0 :     if (DEBUGLEVEL > 1) err_printf("+");
     876           0 :     return 0;
     877             :   }
     878     1398351 :   return can_factor(F, nf, I, m, N, fact);
     879             : }
     880             : 
     881             : /*  FUNDAMENTAL UNITS */
     882             : 
     883             : /* a, y real. Return  (Re(x) + a) + I * (Im(x) % y) */
     884             : static GEN
     885     6566689 : addRe_modIm(GEN x, GEN a, GEN y, GEN iy)
     886             : {
     887             :   GEN z;
     888     6566689 :   if (typ(x) == t_COMPLEX)
     889             :   {
     890     4556271 :     GEN re, im = modRr_i(gel(x,2), y, iy);
     891     4556193 :     if (!im) return NULL;
     892     4556192 :     re = gadd(gel(x,1), a);
     893     4556182 :     z = gequal0(im)? re: mkcomplex(re, im);
     894             :   }
     895             :   else
     896     2010418 :     z = gadd(x, a);
     897     6566631 :   return z;
     898             : }
     899             : static GEN
     900      200770 : modIm(GEN x, GEN y, GEN iy)
     901             : {
     902      200770 :   if (typ(x) == t_COMPLEX)
     903             :   {
     904      183847 :     GEN im = modRr_i(gel(x,2), y, iy);
     905      183834 :     if (!im) return NULL;
     906      183834 :     x = gequal0(im)? gel(x,1): mkcomplex(gel(x,1), im);
     907             :   }
     908      200761 :   return x;
     909             : }
     910             : 
     911             : /* clean archimedean components. ipi = 2^n / pi (n arbitrary); its
     912             :  * exponent may be modified */
     913             : static GEN
     914     2922785 : cleanarch(GEN x, long N, GEN ipi, long prec)
     915             : {
     916             :   long i, l, R1, RU;
     917     2922785 :   GEN s, y = cgetg_copy(x, &l);
     918             : 
     919     2922799 :   if (!ipi) ipi = invr(mppi(prec));
     920     2922796 :   if (typ(x) == t_MAT)
     921             :   {
     922      523028 :     for (i = 1; i < l; i++)
     923      459322 :       if (!(gel(y,i) = cleanarch(gel(x,i), N, ipi, prec))) return NULL;
     924       63706 :     return y;
     925             :   }
     926     2859083 :   RU = l-1; R1 = (RU<<1) - N;
     927     2859083 :   s = gdivgs(RgV_sum(real_i(x)), -N); /* -log |norm(x)| / N */
     928     2859063 :   i = 1;
     929     2859063 :   if (R1)
     930             :   {
     931     2380316 :     GEN pi2 = Pi2n(1, prec);
     932     2380326 :     setexpo(ipi, -3); /* 1/(2pi) */
     933     7311902 :     for (; i <= R1; i++)
     934     4931605 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s, pi2, ipi))) return NULL;
     935             :   }
     936     2859044 :   if (i <= RU)
     937             :   {
     938     1075703 :     GEN pi4 = Pi2n(2, prec), s2 = gmul2n(s, 1);
     939     1075714 :     setexpo(ipi, -4); /* 1/(4pi) */
     940     2710808 :     for (; i <= RU; i++)
     941     1635085 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s2, pi4, ipi))) return NULL;
     942             :   }
     943     2859064 :   return y;
     944             : }
     945             : GEN
     946      195096 : nf_cxlog_normalize(GEN nf, GEN x, long prec)
     947             : {
     948      195096 :   long N = nf_get_degree(nf);
     949      195096 :   return cleanarch(x, N, NULL, prec);
     950             : }
     951             : 
     952             : /* clean unit archimedean components. ipi = 2^n / pi (n arbitrary); its
     953             :  * exponent may be modified */
     954             : static GEN
     955      132404 : cleanarchunit(GEN x, long N, GEN ipi, long prec)
     956             : {
     957             :   long i, l, R1, RU;
     958      132404 :   GEN y = cgetg_copy(x, &l);
     959             : 
     960      132406 :   if (!ipi) ipi = invr(mppi(prec));
     961      132406 :   if (typ(x) == t_MAT)
     962             :   {
     963      132400 :     for (i = 1; i < l; i++)
     964       68841 :       if (!(gel(y,i) = cleanarchunit(gel(x,i), N, ipi, prec))) return NULL;
     965       63559 :     return y;
     966             :   }
     967       68842 :   if (gexpo(RgV_sum(real_i(x))) > -10) return NULL;
     968       68832 :   RU = l-1; R1 = (RU<<1) - N;
     969       68832 :   i = 1;
     970       68832 :   if (R1)
     971             :   {
     972       54430 :     GEN pi2 = Pi2n(1, prec);
     973       54432 :     setexpo(ipi, -3); /* 1/(2pi) */
     974      184805 :     for (; i <= R1; i++)
     975      130382 :       if (!(gel(y,i) = modIm(gel(x,i), pi2, ipi))) return NULL;
     976             :   }
     977       68825 :   if (i <= RU)
     978             :   {
     979       34354 :     GEN pi4 = Pi2n(2, prec);
     980       34354 :     setexpo(ipi, -4); /* 1/(4pi) */
     981      104753 :     for (; i <= RU; i++)
     982       70388 :       if (!(gel(y,i) = modIm(gel(x,i), pi4, ipi))) return NULL;
     983             :   }
     984       68836 :   return y;
     985             : }
     986             : 
     987             : static GEN
     988         388 : not_given(long reason)
     989             : {
     990         388 :   if (DEBUGLEVEL)
     991           0 :     switch(reason)
     992             :     {
     993           0 :       case fupb_LARGE:
     994           0 :         pari_warn(warner,"fundamental units too large, not given");
     995           0 :         break;
     996           0 :       case fupb_PRECI:
     997           0 :         pari_warn(warner,"insufficient precision for fundamental units, not given");
     998           0 :         break;
     999             :     }
    1000         388 :   return NULL;
    1001             : }
    1002             : 
    1003             : /* check whether exp(x) will 1) get too big (real(x) large), 2) require
    1004             :  * large accuracy for argument reduction (imag(x) large) */
    1005             : static long
    1006     2674728 : expbitprec(GEN x, long *e)
    1007             : {
    1008             :   GEN re, im;
    1009     2674728 :   if (typ(x) != t_COMPLEX) re = x;
    1010             :   else
    1011             :   {
    1012     1685268 :     im = gel(x,2); *e = maxss(*e, expo(im) + 5 - bit_prec(im));
    1013     1685269 :     re = gel(x,1);
    1014             :   }
    1015     2674729 :   return (expo(re) <= 20);
    1016             : 
    1017             : }
    1018             : static long
    1019     1165828 : RgC_expbitprec(GEN x)
    1020             : {
    1021     1165828 :   long l = lg(x), i, e = - (long)HIGHEXPOBIT;
    1022     3639454 :   for (i = 1; i < l; i++)
    1023     2474073 :     if (!expbitprec(gel(x,i), &e)) return LONG_MAX;
    1024     1165381 :   return e;
    1025             : }
    1026             : static long
    1027       48377 : RgM_expbitprec(GEN x)
    1028             : {
    1029       48377 :   long i, j, I, J, e = - (long)HIGHEXPOBIT;
    1030       48377 :   RgM_dimensions(x, &I,&J);
    1031      117145 :   for (j = 1; j <= J; j++)
    1032      269423 :     for (i = 1; i <= I; i++)
    1033      200655 :       if (!expbitprec(gcoeff(x,i,j), &e)) return LONG_MAX;
    1034       48321 :   return e;
    1035             : }
    1036             : 
    1037             : static GEN
    1038        1345 : FlxqX_chinese_unit(GEN X, GEN U, GEN invzk, GEN D, GEN T, ulong p)
    1039             : {
    1040        1345 :   long i, lU = lg(U), lX = lg(X), d = lg(invzk)-1;
    1041        1345 :   GEN M = cgetg(lU, t_MAT);
    1042        1345 :   if (D)
    1043             :   {
    1044        1228 :     D = Flv_inv(D, p);
    1045       69240 :     for (i = 1; i < lX; i++)
    1046       68012 :       if (uel(D, i) != 1)
    1047       57030 :         gel(X,i) = Flx_Fl_mul(gel(X,i), uel(D,i), p);
    1048             :   }
    1049        3808 :   for (i = 1; i < lU; i++)
    1050             :   {
    1051        2463 :     GEN H = FlxqV_factorback(X, gel(U, i), T, p);
    1052        2463 :     gel(M, i) = Flm_Flc_mul(invzk, Flx_to_Flv(H, d), p);
    1053             :   }
    1054        1345 :   return M;
    1055             : }
    1056             : 
    1057             : static GEN
    1058         271 : chinese_unit_slice(GEN A, GEN U, GEN B, GEN D, GEN C, GEN P, GEN *mod)
    1059             : {
    1060         271 :   pari_sp av = avma;
    1061         271 :   long i, n = lg(P)-1, v = varn(C);
    1062             :   GEN H, T;
    1063         271 :   if (n == 1)
    1064             :   {
    1065           0 :     ulong p = uel(P,1);
    1066           0 :     GEN a = ZXV_to_FlxV(A, p), b = ZM_to_Flm(B, p), c = ZX_to_Flx(C, p);
    1067           0 :     GEN d = D ? ZV_to_Flv(D, p): NULL;
    1068           0 :     GEN Hp = FlxqX_chinese_unit(a, U, b, d, c, p);
    1069           0 :     H = gerepileupto(av, Flm_to_ZM(Hp));
    1070           0 :     *mod = utoi(p);
    1071           0 :     return H;
    1072             :   }
    1073         271 :   T = ZV_producttree(P);
    1074         271 :   A = ZXC_nv_mod_tree(A, P, T, v);
    1075         271 :   B = ZM_nv_mod_tree(B, P, T);
    1076         271 :   D = D ? ZV_nv_mod_tree(D, P, T): NULL;
    1077         271 :   C = ZX_nv_mod_tree(C, P, T);
    1078             : 
    1079         271 :   H = cgetg(n+1, t_VEC);
    1080        1616 :   for(i=1; i <= n; i++)
    1081             :   {
    1082        1345 :     ulong p = P[i];
    1083        1345 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C,i), d = D ? gel(D,i): NULL;
    1084        1345 :     gel(H,i) = FlxqX_chinese_unit(a, U, b, d, c, p);
    1085             :   }
    1086         271 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
    1087         271 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    1088             : }
    1089             : 
    1090             : GEN
    1091         271 : chinese_unit_worker(GEN P, GEN A, GEN U, GEN B, GEN D, GEN C)
    1092             : {
    1093         271 :   GEN V = cgetg(3, t_VEC);
    1094         271 :   gel(V,1) = chinese_unit_slice(A, U, B, isintzero(D) ? NULL: D, C, P, &gel(V,2));
    1095         271 :   return V;
    1096             : }
    1097             : 
    1098             : /* Let x = \prod X[i]^E[i] = u, return u.
    1099             :  * If dX != NULL, X[i] = nX[i] / dX[i] where nX[i] is a ZX, dX[i] in Z */
    1100             : static GEN
    1101         100 : chinese_unit(GEN nf, GEN nX, GEN dX, GEN U, ulong bnd)
    1102             : {
    1103         100 :   pari_sp av = avma;
    1104         100 :   GEN f = nf_get_index(nf), T = nf_get_pol(nf), invzk = nf_get_invzk(nf);
    1105             :   GEN H, mod;
    1106             :   forprime_t S;
    1107         100 :   GEN worker = snm_closure(is_entry("_chinese_unit_worker"),
    1108             :                mkcol5(nX, U, invzk, dX? dX: gen_0, T));
    1109         100 :   init_modular_big(&S);
    1110         100 :   H = gen_crt("chinese_units", worker, &S, f, bnd, 0, &mod, nmV_chinese_center, FpM_center);
    1111         100 :   settyp(H, t_VEC); return gerepilecopy(av, H);
    1112             : }
    1113             : 
    1114             : /* *pE a ZM */
    1115             : static void
    1116         170 : ZM_remove_unused(GEN *pE, GEN *pX)
    1117             : {
    1118         170 :   long j, k, l = lg(*pX);
    1119         170 :   GEN E = *pE, v = cgetg(l, t_VECSMALL);
    1120       19295 :   for (j = k = 1; j < l; j++)
    1121       19125 :     if (!ZMrow_equal0(E, j)) v[k++] = j;
    1122         170 :   if (k < l)
    1123             :   {
    1124         170 :     setlg(v, k);
    1125         170 :     *pX = vecpermute(*pX,v);
    1126         170 :     *pE = rowpermute(E,v);
    1127             :   }
    1128         170 : }
    1129             : 
    1130             : /* s = -log|norm(x)|/N */
    1131             : static GEN
    1132     1234647 : fixarch(GEN x, GEN s, long R1)
    1133             : {
    1134             :   long i, l;
    1135     1234647 :   GEN y = cgetg_copy(x, &l);
    1136     3408387 :   for (i = 1; i <= R1; i++) gel(y,i) = gadd(s, gel(x,i));
    1137     1736180 :   for (     ; i <   l; i++) gel(y,i) = gadd(s, gmul2n(gel(x,i),-1));
    1138     1234655 :   return y;
    1139             : }
    1140             : 
    1141             : static GEN
    1142       63559 : getfu(GEN nf, GEN *ptA, GEN *ptU, long prec)
    1143             : {
    1144       63559 :   GEN U, y, matep, A, T = nf_get_pol(nf), M = nf_get_M(nf);
    1145       63559 :   long e, j, R1, RU, N = degpol(T);
    1146             : 
    1147       63559 :   R1 = nf_get_r1(nf); RU = (N+R1) >> 1;
    1148       63560 :   if (RU == 1) return cgetg(1,t_VEC);
    1149             : 
    1150       48376 :   A = *ptA;
    1151       48376 :   matep = cgetg(RU,t_MAT);
    1152      117205 :   for (j = 1; j < RU; j++)
    1153             :   {
    1154       68831 :     GEN Aj = gel(A,j), s = gdivgs(RgV_sum(real_i(Aj)), -N);
    1155       68830 :     gel(matep,j) = fixarch(Aj, s, R1);
    1156             :   }
    1157       48374 :   U = lll(real_i(matep));
    1158       48376 :   if (lg(U) < RU) return not_given(fupb_PRECI);
    1159       48376 :   if (ptU) { *ptU = U; *ptA = A = RgM_ZM_mul(A,U); }
    1160       48377 :   y = RgM_ZM_mul(matep,U);
    1161       48377 :   e = RgM_expbitprec(y);
    1162       48377 :   if (e >= 0) return not_given(e == LONG_MAX? fupb_LARGE: fupb_PRECI);
    1163       48321 :   if (prec <= 0) prec = gprecision(A);
    1164       48321 :   y = RgM_solve_realimag(M, gexp(y,prec));
    1165       48321 :   if (!y) return not_given(fupb_PRECI);
    1166       48321 :   y = grndtoi(y, &e); if (e >= 0) return not_given(fupb_PRECI);
    1167       48010 :   settyp(y, t_VEC);
    1168             : 
    1169       48010 :   if (!ptU) *ptA = A = RgM_ZM_mul(A, U);
    1170      116108 :   for (j = 1; j < RU; j++)
    1171             :   { /* y[i] are hopefully unit generators. Normalize: smallest T2 norm */
    1172       68119 :     GEN u = gel(y,j), v = zk_inv(nf, u);
    1173       68119 :     if (!v || !is_pm1(Q_denom(v)) || ZV_isscalar(u))
    1174          21 :       return not_given(fupb_PRECI);
    1175       68098 :     if (gcmp(RgC_fpnorml2(v,DEFAULTPREC), RgC_fpnorml2(u,DEFAULTPREC)) < 0)
    1176             :     {
    1177       27823 :       gel(A,j) = RgC_neg(gel(A,j));
    1178       27823 :       if (ptU) gel(U,j) = ZC_neg(gel(U,j));
    1179       27823 :       u = v;
    1180             :     }
    1181       68098 :     gel(y,j) = nf_to_scalar_or_alg(nf, u);
    1182             :   }
    1183       47989 :   return y;
    1184             : }
    1185             : 
    1186             : static void
    1187           0 : err_units() { pari_err_PREC("makeunits [cannot get units, use bnfinit(,1)]"); }
    1188             : 
    1189             : /* bound for log2 |sigma(u)|, sigma complex embedding, u fundamental unit
    1190             :  * attached to bnf_get_logfu */
    1191             : static double
    1192         100 : log2fubound(GEN bnf)
    1193             : {
    1194         100 :   GEN LU = bnf_get_logfu(bnf);
    1195         100 :   long i, j, l = lg(LU), r1 = nf_get_r1(bnf_get_nf(bnf));
    1196         100 :   double e = 0.0;
    1197         355 :   for (j = 1; j < l; j++)
    1198             :   {
    1199         255 :     GEN u = gel(LU,j);
    1200         637 :     for (i = 1; i <= r1; i++)
    1201             :     {
    1202         382 :       GEN E = real_i(gel(u,i));
    1203         382 :       e = maxdd(e, gtodouble(E));
    1204             :     }
    1205         945 :     for (     ; i <= l; i++)
    1206             :     {
    1207         690 :       GEN E = real_i(gel(u,i));
    1208         690 :       e = maxdd(e, gtodouble(E) / 2);
    1209             :     }
    1210             :   }
    1211         100 :   return e / M_LN2;
    1212             : }
    1213             : /* bound for log2(|RgM_solve_realimag(M, y)|_oo / |y|_oo)*/
    1214             : static double
    1215         100 : log2Mbound(GEN nf)
    1216             : {
    1217         100 :   GEN G = nf_get_G(nf), D = nf_get_disc(nf);
    1218         100 :   long r2 = nf_get_r2(nf), l = lg(G), i;
    1219         100 :   double e, d = dbllog2(D)/2 - r2 * M_LN2; /* log2 |det(split_realimag(M))| */
    1220         100 :   e = log2(nf_get_degree(nf));
    1221         588 :   for (i = 2; i < l; i++) e += dbllog2(gnorml2(gel(G,i))); /* Hadamard bound */
    1222         100 :   return e / 2 - d;
    1223             : }
    1224             : 
    1225             : static GEN
    1226         100 : vec_chinese_units(GEN bnf)
    1227             : {
    1228         100 :   GEN nf = bnf_get_nf(bnf), SUnits = bnf_get_sunits(bnf);
    1229         100 :   double bnd = ceil(log2Mbound(nf) + log2fubound(bnf));
    1230         100 :   GEN X, dX, Y, U, f = nf_get_index(nf);
    1231         100 :   long j, l, v = nf_get_varn(nf);
    1232         100 :   if (!SUnits) err_units(); /* no compact units */
    1233         100 :   Y = gel(SUnits,1);
    1234         100 :   U = gel(SUnits,2);
    1235         100 :   ZM_remove_unused(&U, &Y); l = lg(Y); X = cgetg(l, t_VEC);
    1236         100 :   if (is_pm1(f)) f = dX = NULL; else dX = cgetg(l, t_VEC);
    1237        6280 :   for (j = 1; j < l; j++)
    1238             :   {
    1239        6180 :     GEN t = nf_to_scalar_or_alg(nf, gel(Y,j));
    1240        6180 :     if (f)
    1241             :     {
    1242             :       GEN den;
    1243        5260 :       t = Q_remove_denom(t, &den);
    1244        5260 :       gel(dX,j) = den ? den: gen_1;
    1245             :     }
    1246        6180 :     gel(X,j) = typ(t) == t_INT? scalarpol_shallow(t,v): t;
    1247             :   }
    1248         100 :   if (dblexpo(bnd) >= BITS_IN_LONG)
    1249           0 :     pari_err_OVERFLOW("vec_chinese_units [units too large]");
    1250         100 :   return chinese_unit(nf, X, dX, U, (ulong)bnd);
    1251             : }
    1252             : 
    1253             : static GEN
    1254       24901 : makeunits(GEN bnf)
    1255             : {
    1256       24901 :   GEN nf = bnf_get_nf(bnf), fu = bnf_get_fu_nocheck(bnf);
    1257       24901 :   GEN tu = nf_to_scalar_or_basis(nf, bnf_get_tuU(bnf));
    1258       24901 :   fu = (typ(fu) == t_MAT)? vec_chinese_units(bnf): matalgtobasis(nf, fu);
    1259       24901 :   return vec_prepend(fu, tu);
    1260             : }
    1261             : 
    1262             : /*******************************************************************/
    1263             : /*                                                                 */
    1264             : /*           PRINCIPAL IDEAL ALGORITHM (DISCRETE LOG)              */
    1265             : /*                                                                 */
    1266             : /*******************************************************************/
    1267             : 
    1268             : /* G: prime ideals, E: vector of nonnegative exponents.
    1269             :  * C = possible extra prime (^1) or NULL
    1270             :  * Return Norm (product) */
    1271             : static GEN
    1272          77 : get_norm_fact_primes(GEN G, GEN E, GEN C)
    1273             : {
    1274          77 :   pari_sp av=avma;
    1275          77 :   GEN N = gen_1, P, p;
    1276          77 :   long i, c = lg(E);
    1277          77 :   for (i=1; i<c; i++)
    1278             :   {
    1279           0 :     GEN ex = gel(E,i);
    1280           0 :     long s = signe(ex);
    1281           0 :     if (!s) continue;
    1282             : 
    1283           0 :     P = gel(G,i); p = pr_get_p(P);
    1284           0 :     N = mulii(N, powii(p, mului(pr_get_f(P), ex)));
    1285             :   }
    1286          77 :   if (C) N = mulii(N, pr_norm(C));
    1287          77 :   return gerepileuptoint(av, N);
    1288             : }
    1289             : 
    1290             : /* gen: HNF ideals */
    1291             : static GEN
    1292     1160193 : get_norm_fact(GEN gen, GEN ex, GEN *pd)
    1293             : {
    1294     1160193 :   long i, c = lg(ex);
    1295             :   GEN d,N,I,e,n,ne,de;
    1296     1160193 :   d = N = gen_1;
    1297     1462406 :   for (i=1; i<c; i++)
    1298      302214 :     if (signe(gel(ex,i)))
    1299             :     {
    1300      181683 :       I = gel(gen,i); e = gel(ex,i); n = ZM_det_triangular(I);
    1301      181683 :       ne = powii(n,e);
    1302      181683 :       de = equalii(n, gcoeff(I,1,1))? ne: powii(gcoeff(I,1,1), e);
    1303      181683 :       N = mulii(N, ne);
    1304      181683 :       d = mulii(d, de);
    1305             :     }
    1306     1160192 :   *pd = d; return N;
    1307             : }
    1308             : 
    1309             : static GEN
    1310     1321111 : get_pr_lists(GEN FB, long N, int list_pr)
    1311             : {
    1312             :   GEN pr, L;
    1313     1321111 :   long i, l = lg(FB), p, pmax;
    1314             : 
    1315     1321111 :   pmax = 0;
    1316     9227828 :   for (i=1; i<l; i++)
    1317             :   {
    1318     7906717 :     pr = gel(FB,i); p = pr_get_smallp(pr);
    1319     7906717 :     if (p > pmax) pmax = p;
    1320             :   }
    1321     1321111 :   L = const_vec(pmax, NULL);
    1322     1321116 :   if (list_pr)
    1323             :   {
    1324           0 :     for (i=1; i<l; i++)
    1325             :     {
    1326           0 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1327           0 :       if (!L[p]) gel(L,p) = vectrunc_init(N+1);
    1328           0 :       vectrunc_append(gel(L,p), pr);
    1329             :     }
    1330           0 :     for (p=1; p<=pmax; p++)
    1331           0 :       if (L[p]) gen_sort_inplace(gel(L,p), (void*)&cmp_prime_over_p,
    1332             :                                  &cmp_nodata, NULL);
    1333             :   }
    1334             :   else
    1335             :   {
    1336     9227842 :     for (i=1; i<l; i++)
    1337             :     {
    1338     7906726 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1339     7906726 :       if (!L[p]) gel(L,p) = vecsmalltrunc_init(N+1);
    1340     7906725 :       vecsmalltrunc_append(gel(L,p), i);
    1341             :     }
    1342             :   }
    1343     1321116 :   return L;
    1344             : }
    1345             : 
    1346             : /* recover FB, LV, iLP, KCZ from Vbase */
    1347             : static GEN
    1348     1321111 : recover_partFB(FB_t *F, GEN Vbase, long N)
    1349             : {
    1350     1321111 :   GEN FB, LV, iLP, L = get_pr_lists(Vbase, N, 0);
    1351     1321117 :   long l = lg(L), p, ip, i;
    1352             : 
    1353     1321117 :   i = ip = 0;
    1354     1321117 :   FB = cgetg(l, t_VECSMALL);
    1355     1321116 :   iLP= cgetg(l, t_VECSMALL);
    1356     1321116 :   LV = cgetg(l, t_VEC);
    1357    20140425 :   for (p = 2; p < l; p++)
    1358             :   {
    1359    18819308 :     if (!L[p]) continue;
    1360     4318541 :     FB[++i] = p;
    1361     4318541 :     gel(LV,p) = vecpermute(Vbase, gel(L,p));
    1362     4318541 :     iLP[p]= ip; ip += lg(gel(L,p))-1;
    1363             :   }
    1364     1321117 :   F->KCZ = i;
    1365     1321117 :   F->KC = ip;
    1366     1321117 :   F->FB = FB; setlg(FB, i+1);
    1367     1321117 :   F->prodZ = zv_prod_Z(F->FB);
    1368     1321113 :   F->LV = LV;
    1369     1321113 :   F->iLP= iLP; return L;
    1370             : }
    1371             : 
    1372             : /* add v^e to factorization */
    1373             : static void
    1374       19919 : add_to_fact(long v, long e, FACT *fact)
    1375             : {
    1376       19919 :   long i, l = fact[0].pr;
    1377       34685 :   for (i=1; i<=l && fact[i].pr < v; i++)/*empty*/;
    1378       19919 :   if (i <= l && fact[i].pr == v) fact[i].ex += e; else store(v, e, fact);
    1379       19919 : }
    1380             : static void
    1381           0 : inv_fact(FACT *fact)
    1382             : {
    1383           0 :   long i, l = fact[0].pr;
    1384           0 :   for (i=1; i<=l; i++) fact[i].ex = -fact[i].ex;
    1385           0 : }
    1386             : 
    1387             : /* L (small) list of primes above the same p including pr. Return pr index */
    1388             : static int
    1389        3250 : pr_index(GEN L, GEN pr)
    1390             : {
    1391        3250 :   long j, l = lg(L);
    1392        3250 :   GEN al = pr_get_gen(pr);
    1393        3251 :   for (j=1; j<l; j++)
    1394        3251 :     if (ZV_equal(al, pr_get_gen(gel(L,j)))) return j;
    1395           0 :   pari_err_BUG("codeprime");
    1396             :   return 0; /* LCOV_EXCL_LINE */
    1397             : }
    1398             : 
    1399             : static long
    1400        3250 : Vbase_to_FB(FB_t *F, GEN pr)
    1401             : {
    1402        3250 :   long p = pr_get_smallp(pr);
    1403        3250 :   return F->iLP[p] + pr_index(gel(F->LV,p), pr);
    1404             : }
    1405             : 
    1406             : /* x, y 2 extended ideals whose first component is an integral HNF and second
    1407             :  * a famat */
    1408             : static GEN
    1409        3476 : idealHNF_mulred(GEN nf, GEN x, GEN y)
    1410             : {
    1411        3476 :   GEN A = idealHNF_mul(nf, gel(x,1), gel(y,1));
    1412        3476 :   GEN F = famat_mul_shallow(gel(x,2), gel(y,2));
    1413        3476 :   return idealred(nf, mkvec2(A, F));
    1414             : }
    1415             : /* idealred(x * pr^n), n > 0 is small, x extended ideal. Reduction in order to
    1416             :  * avoid prec pb: don't let id become too large as lgsub increases */
    1417             : static GEN
    1418        4510 : idealmulpowprime2(GEN nf, GEN x, GEN pr, ulong n)
    1419             : {
    1420        4510 :   GEN A = idealmulpowprime(nf, gel(x,1), pr, utoipos(n));
    1421        4510 :   return mkvec2(A, gel(x,2));
    1422             : }
    1423             : static GEN
    1424       65258 : init_famat(GEN x) { return mkvec2(x, trivial_fact()); }
    1425             : /* optimized idealfactorback + reduction; z = init_famat() */
    1426             : static GEN
    1427       28711 : genback(GEN z, GEN nf, GEN P, GEN E)
    1428             : {
    1429       28711 :   long i, l = lg(E);
    1430       28711 :   GEN I = NULL;
    1431       75993 :   for (i = 1; i < l; i++)
    1432       47279 :     if (signe(gel(E,i)))
    1433             :     {
    1434             :       GEN J;
    1435       32187 :       gel(z,1) = gel(P,i);
    1436       32187 :       J = idealpowred(nf, z, gel(E,i));
    1437       32190 :       I = I? idealHNF_mulred(nf, I, J): J;
    1438             :     }
    1439       28714 :   return I; /* != NULL since a generator */
    1440             : }
    1441             : 
    1442             : /* return famat y (principal ideal) such that y / x is smooth [wrt Vbase] */
    1443             : static GEN
    1444     1337459 : SPLIT(FB_t *F, GEN nf, GEN x, GEN Vbase, FACT *fact)
    1445             : {
    1446     1337459 :   GEN vecG, ex, Ly, y, x0, Nx = ZM_det_triangular(x);
    1447             :   long nbtest_lim, nbtest, i, j, k, ru, lgsub;
    1448             :   pari_sp av;
    1449             : 
    1450             :   /* try without reduction if x is small */
    1451     2674721 :   if (gexpo(gcoeff(x,1,1)) < 100 &&
    1452     1487420 :       can_factor(F, nf, x, NULL, Nx, fact)) return NULL;
    1453             : 
    1454     1187303 :   av = avma;
    1455     1187303 :   Ly = idealpseudominvec(x, nf_get_roundG(nf));
    1456     1236350 :   for(k=1; k<lg(Ly); k++)
    1457             :   {
    1458     1227320 :     y = gel(Ly,k);
    1459     1227320 :     if (factorgen(F, nf, x, Nx, y, fact)) return y;
    1460             :   }
    1461        9030 :   set_avma(av);
    1462             : 
    1463             :   /* reduce in various directions */
    1464        9030 :   ru = lg(nf_get_roots(nf));
    1465        9030 :   vecG = cgetg(ru, t_VEC);
    1466       14469 :   for (j=1; j<ru; j++)
    1467             :   {
    1468       12770 :     gel(vecG,j) = nf_get_Gtwist1(nf, j);
    1469       12770 :     av = avma;
    1470       12770 :     Ly = idealpseudominvec(x, gel(vecG,j));
    1471       42867 :     for(k=1; k<lg(Ly); k++)
    1472             :     {
    1473       37428 :       y = gel(Ly,k);
    1474       37428 :       if (factorgen(F, nf, x, Nx, y, fact)) return y;
    1475             :     }
    1476        5439 :     set_avma(av);
    1477             :   }
    1478             : 
    1479             :   /* tough case, multiply by random products */
    1480        1699 :   lgsub = 3;
    1481        1699 :   ex = cgetg(lgsub, t_VECSMALL);
    1482        1699 :   x0 = init_famat(x);
    1483        1699 :   nbtest = 1; nbtest_lim = 4;
    1484             :   for(;;)
    1485         630 :   {
    1486        2329 :     GEN Ired, I, NI, id = x0;
    1487        2329 :     av = avma;
    1488        2329 :     if (DEBUGLEVEL>2) err_printf("# ideals tried = %ld\n",nbtest);
    1489        7125 :     for (i=1; i<lgsub; i++)
    1490             :     {
    1491        4796 :       ex[i] = random_bits(RANDOM_BITS);
    1492        4796 :       if (ex[i]) id = idealmulpowprime2(nf, id, gel(Vbase,i), ex[i]);
    1493             :     }
    1494        2329 :     if (id == x0) continue;
    1495             :     /* I^(-1) * \prod Vbase[i]^ex[i] = (id[2]) / x */
    1496             : 
    1497        2329 :     I = gel(id,1); NI = ZM_det_triangular(I);
    1498        2329 :     if (can_factor(F, nf, I, NULL, NI, fact))
    1499             :     {
    1500           0 :       inv_fact(fact); /* I^(-1) */
    1501           0 :       for (i=1; i<lgsub; i++)
    1502           0 :         if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1503           0 :       return gel(id,2);
    1504             :     }
    1505        2329 :     Ired = ru == 2? I: ZM_lll(I, 0.99, LLL_INPLACE);
    1506        3997 :     for (j=1; j<ru; j++)
    1507             :     {
    1508        3367 :       pari_sp av2 = avma;
    1509        3367 :       Ly = idealpseudominvec(Ired, gel(vecG,j));
    1510       13674 :       for (k=1; k < lg(Ly); k++)
    1511             :       {
    1512       12006 :         y = gel(Ly,k);
    1513       12006 :         if (factorgen(F, nf, I, NI, y, fact))
    1514             :         {
    1515        5130 :           for (i=1; i<lgsub; i++)
    1516        3431 :             if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1517        1699 :           return famat_mul_shallow(gel(id,2), y);
    1518             :         }
    1519             :       }
    1520        1668 :       set_avma(av2);
    1521             :     }
    1522         630 :     set_avma(av);
    1523         630 :     if (++nbtest > nbtest_lim)
    1524             :     {
    1525          33 :       nbtest = 0;
    1526          33 :       if (++lgsub < minss(8, lg(Vbase)-1))
    1527             :       {
    1528          33 :         nbtest_lim <<= 1;
    1529          33 :         ex = cgetg(lgsub, t_VECSMALL);
    1530             :       }
    1531           0 :       else nbtest_lim = LONG_MAX; /* don't increase further */
    1532          33 :       if (DEBUGLEVEL>2) err_printf("SPLIT: increasing factor base [%ld]\n",lgsub);
    1533             :     }
    1534             :   }
    1535             : }
    1536             : 
    1537             : INLINE GEN
    1538     1326038 : bnf_get_W(GEN bnf) { return gel(bnf,1); }
    1539             : INLINE GEN
    1540     2642115 : bnf_get_B(GEN bnf) { return gel(bnf,2); }
    1541             : INLINE GEN
    1542     2671292 : bnf_get_C(GEN bnf) { return gel(bnf,4); }
    1543             : INLINE GEN
    1544     1321132 : bnf_get_vbase(GEN bnf) { return gel(bnf,5); }
    1545             : INLINE GEN
    1546     1321050 : bnf_get_Ur(GEN bnf) { return gmael(bnf,9,1); }
    1547             : INLINE GEN
    1548      277614 : bnf_get_ga(GEN bnf) { return gmael(bnf,9,2); }
    1549             : INLINE GEN
    1550      282583 : bnf_get_GD(GEN bnf) { return gmael(bnf,9,3); }
    1551             : 
    1552             : /* Return y (as an elt of K or a t_MAT representing an elt in Z[K])
    1553             :  * such that x / (y) is smooth and store the exponents of  its factorization
    1554             :  * on g_W and g_B in Wex / Bex; return NULL for y = 1 */
    1555             : static GEN
    1556     1321048 : split_ideal(GEN bnf, GEN x, GEN *pWex, GEN *pBex)
    1557             : {
    1558     1321048 :   GEN L, y, Vbase = bnf_get_vbase(bnf);
    1559     1321048 :   GEN Wex, W  = bnf_get_W(bnf);
    1560     1321048 :   GEN Bex, B  = bnf_get_B(bnf);
    1561             :   long p, j, i, l, nW, nB;
    1562             :   FACT *fact;
    1563             :   FB_t F;
    1564             : 
    1565     1321048 :   L = recover_partFB(&F, Vbase, lg(x)-1);
    1566     1321050 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    1567     1321051 :   y = SPLIT(&F, bnf_get_nf(bnf), x, Vbase, fact);
    1568     1321051 :   nW = lg(W)-1; *pWex = Wex = zero_zv(nW);
    1569     1321051 :   nB = lg(B)-1; *pBex = Bex = zero_zv(nB); l = lg(F.FB);
    1570     1321051 :   p = j = 0; /* -Wall */
    1571     1980376 :   for (i = 1; i <= fact[0].pr; i++)
    1572             :   { /* decode index C = ip+j --> (p,j) */
    1573      659325 :     long a, b, t, C = fact[i].pr;
    1574     1946190 :     for (t = 1; t < l; t++)
    1575             :     {
    1576     1871476 :       long q = F.FB[t], k = C - F.iLP[q];
    1577     1871476 :       if (k <= 0) break;
    1578     1286865 :       p = q;
    1579     1286865 :       j = k;
    1580             :     }
    1581      659325 :     a = gel(L, p)[j];
    1582      659325 :     b = a - nW;
    1583      659325 :     if (b <= 0) Wex[a] = y? -fact[i].ex: fact[i].ex;
    1584      506452 :     else        Bex[b] = y? -fact[i].ex: fact[i].ex;
    1585             :   }
    1586     1321051 :   return y;
    1587             : }
    1588             : 
    1589             : GEN
    1590     1038758 : init_red_mod_units(GEN bnf, long prec)
    1591             : {
    1592     1038758 :   GEN s = gen_0, p1,s1,mat, logfu = bnf_get_logfu(bnf);
    1593     1038758 :   long i,j, RU = lg(logfu);
    1594             : 
    1595     1038758 :   if (RU == 1) return NULL;
    1596     1038758 :   mat = cgetg(RU,t_MAT);
    1597     2347466 :   for (j=1; j<RU; j++)
    1598             :   {
    1599     1308708 :     p1 = cgetg(RU+1,t_COL); gel(mat,j) = p1;
    1600     1308708 :     s1 = gen_0;
    1601     3228045 :     for (i=1; i<RU; i++)
    1602             :     {
    1603     1919338 :       gel(p1,i) = real_i(gcoeff(logfu,i,j));
    1604     1919338 :       s1 = mpadd(s1, mpsqr(gel(p1,i)));
    1605             :     }
    1606     1308707 :     gel(p1,RU) = gen_0; if (mpcmp(s1,s) > 0) s = s1;
    1607             :   }
    1608     1038758 :   s = gsqrt(gmul2n(s,RU),prec);
    1609     1038758 :   if (expo(s) < 27) s = utoipos(1UL << 27);
    1610     1038758 :   return mkvec2(mat, s);
    1611             : }
    1612             : 
    1613             : /* z computed above. Return unit exponents that would reduce col (arch) */
    1614             : GEN
    1615     1038758 : red_mod_units(GEN col, GEN z)
    1616             : {
    1617             :   long i,RU;
    1618             :   GEN x,mat,N2;
    1619             : 
    1620     1038758 :   if (!z) return NULL;
    1621     1038758 :   mat= gel(z,1);
    1622     1038758 :   N2 = gel(z,2);
    1623     1038758 :   RU = lg(mat); x = cgetg(RU+1,t_COL);
    1624     2347466 :   for (i=1; i<RU; i++) gel(x,i) = real_i(gel(col,i));
    1625     1038758 :   gel(x,RU) = N2;
    1626     1038758 :   x = lll(shallowconcat(mat,x));
    1627     1038758 :   if (typ(x) != t_MAT || lg(x) <= RU) return NULL;
    1628     1038758 :   x = gel(x,RU);
    1629     1038758 :   if (signe(gel(x,RU)) < 0) x = gneg_i(x);
    1630     1038758 :   if (!gequal1(gel(x,RU))) pari_err_BUG("red_mod_units");
    1631     1038758 :   setlg(x,RU); return x;
    1632             : }
    1633             : 
    1634             : static GEN
    1635     2158915 : add(GEN a, GEN t) { return a = a? RgC_add(a,t): t; }
    1636             : 
    1637             : /* [x] archimedian components, A column vector. return [x] A */
    1638             : static GEN
    1639     1996037 : act_arch(GEN A, GEN x)
    1640             : {
    1641             :   GEN a;
    1642     1996037 :   long i,l = lg(A), tA = typ(A);
    1643     1996037 :   if (tA == t_MAT)
    1644             :   { /* assume lg(x) >= l */
    1645      190968 :     a = cgetg(l, t_MAT);
    1646      280620 :     for (i=1; i<l; i++) gel(a,i) = act_arch(gel(A,i), x);
    1647      190970 :     return a;
    1648             :   }
    1649     1805069 :   if (l==1) return cgetg(1, t_COL);
    1650     1805069 :   a = NULL;
    1651     1805069 :   if (tA == t_VECSMALL)
    1652             :   {
    1653     6871712 :     for (i=1; i<l; i++)
    1654             :     {
    1655     5711519 :       long c = A[i];
    1656     5711519 :       if (c) a = add(a, gmulsg(c, gel(x,i)));
    1657             :     }
    1658             :   }
    1659             :   else
    1660             :   { /* A a t_COL of t_INT. Assume lg(A)==lg(x) */
    1661     1403012 :     for (i=1; i<l; i++)
    1662             :     {
    1663      758138 :       GEN c = gel(A,i);
    1664      758138 :       if (signe(c)) a = add(a, gmul(c, gel(x,i)));
    1665             :     }
    1666             :   }
    1667     1805067 :   return a? a: zerocol(lgcols(x)-1);
    1668             : }
    1669             : /* act_arch(matdiagonal(v), x) */
    1670             : static GEN
    1671       63657 : diagact_arch(GEN v, GEN x)
    1672             : {
    1673       63657 :   long i, l = lg(v);
    1674       63657 :   GEN a = cgetg(l, t_MAT);
    1675       92441 :   for (i = 1; i < l; i++) gel(a,i) = gmul(gel(x,i), gel(v,i));
    1676       63657 :   return a;
    1677             : }
    1678             : 
    1679             : static long
    1680     1336435 : prec_arch(GEN bnf)
    1681             : {
    1682     1336435 :   GEN a = bnf_get_C(bnf);
    1683     1336435 :   long i, l = lg(a), prec;
    1684             : 
    1685     1336435 :   for (i=1; i<l; i++)
    1686     1336351 :     if ( (prec = gprecision(gel(a,i))) ) return prec;
    1687          84 :   return DEFAULTPREC;
    1688             : }
    1689             : 
    1690             : static long
    1691        3925 : needed_bitprec(GEN x)
    1692             : {
    1693        3925 :   long i, e = 0, l = lg(x);
    1694       22629 :   for (i = 1; i < l; i++)
    1695             :   {
    1696       18704 :     GEN c = gel(x,i);
    1697       18704 :     long f = gexpo(c) - prec2nbits(gprecision(c));
    1698       18704 :     if (f > e) e = f;
    1699             :   }
    1700        3925 :   return e;
    1701             : }
    1702             : 
    1703             : /* col = archimedian components of x, Nx its norm, dx a multiple of its
    1704             :  * denominator. Return x or NULL (fail) */
    1705             : GEN
    1706     1165825 : isprincipalarch(GEN bnf, GEN col, GEN kNx, GEN e, GEN dx, long *pe)
    1707             : {
    1708             :   GEN nf, x, y, logfu, s, M;
    1709     1165825 :   long N, prec = gprecision(col);
    1710     1165825 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf); M = nf_get_M(nf);
    1711     1165826 :   if (!prec) prec = prec_arch(bnf);
    1712     1165826 :   *pe = 128;
    1713     1165826 :   logfu = bnf_get_logfu(bnf);
    1714     1165826 :   N = nf_get_degree(nf);
    1715     1165826 :   if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
    1716     1165824 :   if (lg(col) > 2)
    1717             :   { /* reduce mod units */
    1718     1038758 :     GEN u, z = init_red_mod_units(bnf,prec);
    1719     1038758 :     if (!(u = red_mod_units(col,z))) return NULL;
    1720     1038758 :     col = RgC_add(col, RgM_RgC_mul(logfu, u));
    1721     1038757 :     if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
    1722             :   }
    1723     1165824 :   s = divru(mulir(e, glog(kNx,prec)), N);
    1724     1165815 :   col = fixarch(col, s, nf_get_r1(nf));
    1725     1165827 :   if (RgC_expbitprec(col) >= 0) return NULL;
    1726     1165380 :   col = gexp(col, prec);
    1727             :   /* d.alpha such that x = alpha \prod gj^ej */
    1728     1165381 :   x = RgM_solve_realimag(M,col); if (!x) return NULL;
    1729     1165379 :   x = RgC_Rg_mul(x, dx);
    1730     1165375 :   y = grndtoi(x, pe);
    1731     1165379 :   if (*pe > -5) { *pe = needed_bitprec(x); return NULL; }
    1732     1161454 :   return RgC_Rg_div(y, dx);
    1733             : }
    1734             : 
    1735             : /* y = C \prod g[i]^e[i] ? */
    1736             : static int
    1737     1155433 : fact_ok(GEN nf, GEN y, GEN C, GEN g, GEN e)
    1738             : {
    1739     1155433 :   pari_sp av = avma;
    1740     1155433 :   long i, c = lg(e);
    1741     1155433 :   GEN z = C? C: gen_1;
    1742     1435974 :   for (i=1; i<c; i++)
    1743      280541 :     if (signe(gel(e,i))) z = idealmul(nf, z, idealpow(nf, gel(g,i), gel(e,i)));
    1744     1155433 :   if (typ(z) != t_MAT) z = idealhnf_shallow(nf,z);
    1745     1155434 :   if (typ(y) != t_MAT) y = idealhnf_shallow(nf,y);
    1746     1155434 :   return gc_bool(av, ZM_equal(y,z));
    1747             : }
    1748             : static GEN
    1749     1321051 : ZV_divrem(GEN A, GEN B, GEN *pR)
    1750             : {
    1751     1321051 :   long i, l = lg(A);
    1752     1321051 :   GEN Q = cgetg(l, t_COL), R = cgetg(l, t_COL);
    1753     1832975 :   for (i = 1; i < l; i++) gel(Q,i) = truedvmdii(gel(A,i), gel(B,i), &gel(R,i));
    1754     1321052 :   *pR = R; return Q;
    1755             : }
    1756             : 
    1757             : static GEN
    1758     1321049 : Ur_ZC_mul(GEN bnf, GEN v)
    1759             : {
    1760     1321049 :   GEN w, U = bnf_get_Ur(bnf);
    1761     1321050 :   long i, l = lg(bnf_get_cyc(bnf)); /* may be < lgcols(U) */
    1762             : 
    1763     1321050 :   w = cgetg(l, t_COL);
    1764     1832975 :   for (i = 1; i < l; i++) gel(w,i) = ZMrow_ZC_mul(U, v, i);
    1765     1321052 :   return w;
    1766             : }
    1767             : 
    1768             : static GEN
    1769        9184 : ZV_mul(GEN x, GEN y)
    1770             : {
    1771        9184 :   long i, l = lg(x);
    1772        9184 :   GEN z = cgetg(l, t_COL);
    1773       35631 :   for (i = 1; i < l; i++) gel(z,i) = mulii(gel(x,i), gel(y,i));
    1774        9184 :   return z;
    1775             : }
    1776             : static int
    1777     1156786 : dump_gen(GEN SUnits, GEN x, long flag)
    1778             : {
    1779             :   GEN d;
    1780             :   long e;
    1781     1156786 :   if (!(flag & nf_GENMAT) || !SUnits) return 0;
    1782      274588 :   e = gexpo(gel(SUnits,2)); if (e > 64) return 0; /* U large */
    1783      274395 :   x = Q_remove_denom(x, &d);
    1784      274394 :   return (d && expi(d) > 32) || gexpo(x) > 32;
    1785             : }
    1786             : 
    1787             : /* assume x in HNF; cf class_group_gen for notations. Return NULL iff
    1788             :  * flag & nf_FORCE and computation of principal ideal generator fails */
    1789             : static GEN
    1790     1334837 : isprincipalall(GEN bnf, GEN x, long *pprec, long flag)
    1791             : {
    1792             :   GEN xar, Wex, Bex, gen, xc, col, A, Q, R, UA, SUnits;
    1793     1334837 :   GEN C = bnf_get_C(bnf), nf = bnf_get_nf(bnf), cyc = bnf_get_cyc(bnf);
    1794             :   long nB, nW, e;
    1795             : 
    1796     1334837 :   if (lg(cyc) == 1 && !(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL)))
    1797        4725 :     return cgetg(1,t_COL);
    1798     1330112 :   if (lg(x) == 2)
    1799             :   { /* nf = Q */
    1800          84 :     col = gel(x,1);
    1801          84 :     if (flag & nf_GENMAT) col = to_famat_shallow(col, gen_1);
    1802          84 :     return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(cgetg(1,t_COL), col);
    1803             :   }
    1804             : 
    1805     1330028 :   x = Q_primitive_part(x, &xc);
    1806     1330023 :   if (equali1(gcoeff(x,1,1))) /* trivial ideal */
    1807             :   {
    1808        8974 :     R = zerocol(lg(cyc)-1);
    1809        8974 :     if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1810        8960 :     if (flag & nf_GEN_IF_PRINCIPAL)
    1811        6454 :       return scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
    1812        2506 :     if (flag & nf_GENMAT)
    1813        1869 :       col = xc? to_famat_shallow(xc, gen_1): trivial_fact();
    1814             :     else
    1815         637 :       col = scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
    1816        2506 :     return mkvec2(R, col);
    1817             :   }
    1818     1321048 :   xar = split_ideal(bnf, x, &Wex, &Bex);
    1819             :   /* x = g_W Wex + g_B Bex + [xar] = g_W (Wex - B*Bex) + [xar] + [C_B]Bex */
    1820     1321051 :   A = zc_to_ZC(Wex); nB = lg(Bex)-1;
    1821     1321050 :   if (nB) A = ZC_sub(A, ZM_zc_mul(bnf_get_B(bnf), Bex));
    1822     1321049 :   UA = Ur_ZC_mul(bnf, A);
    1823     1321052 :   Q = ZV_divrem(UA, cyc, &R);
    1824             :   /* g_W (Wex - B*Bex) = G Ur A - [ga]A = G R + [GD]Q - [ga]A
    1825             :    * Finally: x = G R + [xar] + [C_B]Bex + [GD]Q - [ga]A */
    1826     1321052 :   if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1827     1160765 :   if ((flag & nf_GEN_IF_PRINCIPAL) && !ZV_equal0(R)) return gen_0;
    1828             : 
    1829     1160758 :   nW = lg(Wex)-1;
    1830     1160758 :   gen = bnf_get_gen(bnf);
    1831     1160758 :   col = NULL;
    1832     1160758 :   SUnits = bnf_get_sunits(bnf);
    1833     1160758 :   if (lg(R) == 1
    1834      278179 :       || abscmpiu(gel(R,vecindexmax(R)), 4 * bit_accuracy(*pprec)) < 0)
    1835             :   { /* q = N (x / prod gj^ej) = N(alpha), denom(alpha) | d */
    1836     1160193 :     GEN d, q = gdiv(ZM_det_triangular(x), get_norm_fact(gen, R, &d));
    1837     1160192 :     col = xar? nf_cxlog(nf, xar, *pprec): NULL;
    1838     1160194 :     if (nB) col = add(col, act_arch(Bex, nW? vecslice(C,nW+1,lg(C)-1): C));
    1839     1160191 :     if (nW) col = add(col, RgC_sub(act_arch(Q, bnf_get_GD(bnf)),
    1840             :                                    act_arch(A, bnf_get_ga(bnf))));
    1841     1160190 :     col = isprincipalarch(bnf, col, q, gen_1, d, &e);
    1842     1160195 :     if (col && (dump_gen(SUnits, col, flag)
    1843     1156785 :                 || !fact_ok(nf,x, col,gen,R))) col = NULL;
    1844             :   }
    1845     1160759 :   if (!col && (flag & nf_GENMAT))
    1846             :   {
    1847        9910 :     if (SUnits)
    1848             :     {
    1849        9420 :       GEN X = gel(SUnits,1), U = gel(SUnits,2), C = gel(SUnits,3);
    1850        9420 :       GEN v = gel(bnf,9), Ge = gel(v,4), M1 = gel(v,5), M2 = gel(v,6);
    1851        9420 :       GEN z = NULL, F = NULL;
    1852        9420 :       if (nB)
    1853             :       {
    1854        9420 :         GEN C2 = nW? vecslice(C, nW+1, lg(C)-1): C;
    1855        9420 :         z = ZM_zc_mul(C2, Bex);
    1856             :       }
    1857        9420 :       if (nW)
    1858             :       { /* [GD]Q - [ga]A = ([X]M1 - [Ge]D) Q - ([X]M2 - [Ge]Ur) A */
    1859        9184 :         GEN C1 = vecslice(C, 1, nW);
    1860        9184 :         GEN v = ZC_sub(ZM_ZC_mul(M1,Q), ZM_ZC_mul(M2,A));
    1861        9184 :         z = add(z, ZM_ZC_mul(C1, v));
    1862        9184 :         F = famat_reduce(famatV_factorback(Ge, ZC_sub(UA, ZV_mul(cyc,Q))));
    1863        9184 :         if (lgcols(F) == 1) F = NULL;
    1864             :       }
    1865             :       /* reduce modulo units and Q^* */
    1866        9420 :       if (lg(U) != 1) z = ZC_sub(z, ZM_ZC_mul(U, RgM_Babai(U,z)));
    1867        9420 :       col = mkmat2(X, z);
    1868        9420 :       if (F) col = famat_mul_shallow(col, F);
    1869        9420 :       col = famat_remove_trivial(col);
    1870        9420 :       if (xar) col = famat_mul_shallow(col, xar);
    1871             :     }
    1872         490 :     else if (!ZV_equal0(R))
    1873             :     { /* in case isprincipalfact calls bnfinit() due to prec trouble...*/
    1874         483 :       GEN y = isprincipalfact(bnf, x, gen, ZC_neg(R), flag);
    1875         483 :       if (typ(y) != t_VEC) return y;
    1876         483 :       col = gel(y,2);
    1877             :     }
    1878             :   }
    1879     1160759 :   if (col)
    1880             :   { /* add back missing content */
    1881     1161214 :     if (xc) col = (typ(col)==t_MAT)? famat_mul_shallow(col,xc)
    1882         546 :                                    : RgC_Rg_mul(col,xc);
    1883     1160668 :     if (typ(col) != t_MAT && lg(col) != 1 && (flag & nf_GENMAT))
    1884     1137093 :       col = to_famat_shallow(col, gen_1);
    1885             :   }
    1886             :   else
    1887             :   {
    1888          91 :     if (e < 0) e = 0;
    1889          91 :     *pprec += nbits2extraprec(e + 128);
    1890          91 :     if (flag & nf_FORCE)
    1891             :     {
    1892          77 :       if (DEBUGLEVEL)
    1893           0 :         pari_warn(warner,"precision too low for generators, e = %ld",e);
    1894          77 :       return NULL;
    1895             :     }
    1896          14 :     pari_warn(warner,"precision too low for generators, not given");
    1897          14 :     col = cgetg(1, t_COL);
    1898             :   }
    1899     1160682 :   return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(R, col);
    1900             : }
    1901             : 
    1902             : static GEN
    1903      460942 : triv_gen(GEN bnf, GEN x, long flag)
    1904             : {
    1905      460942 :   pari_sp av = avma;
    1906      460942 :   GEN nf = bnf_get_nf(bnf);
    1907             :   long c;
    1908      460942 :   if (flag & nf_GEN_IF_PRINCIPAL)
    1909             :   {
    1910           7 :     if (!(flag & nf_GENMAT)) return algtobasis(nf,x);
    1911           7 :     x = nf_to_scalar_or_basis(nf,x);
    1912           7 :     if (typ(x) == t_INT && is_pm1(x)) return trivial_fact();
    1913           0 :     return gerepilecopy(av, to_famat_shallow(x, gen_1));
    1914             :   }
    1915      460935 :   c = lg(bnf_get_cyc(bnf)) - 1;
    1916      460935 :   if (flag & nf_GENMAT)
    1917      451338 :     retmkvec2(zerocol(c), to_famat_shallow(algtobasis(nf,x), gen_1));
    1918        9597 :   if (flag & nf_GEN)
    1919          21 :     retmkvec2(zerocol(c), algtobasis(nf,x));
    1920        9576 :   return zerocol(c);
    1921             : }
    1922             : 
    1923             : GEN
    1924     1766069 : bnfisprincipal0(GEN bnf,GEN x,long flag)
    1925             : {
    1926     1766069 :   pari_sp av = avma;
    1927             :   GEN c, nf;
    1928             :   long pr;
    1929             : 
    1930     1766069 :   bnf = checkbnf(bnf);
    1931     1766069 :   nf = bnf_get_nf(bnf);
    1932     1766069 :   switch( idealtyp(&x, NULL) )
    1933             :   {
    1934       56070 :     case id_PRINCIPAL:
    1935       56070 :       if (gequal0(x)) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1936       56070 :       return triv_gen(bnf, x, flag);
    1937     1686319 :     case id_PRIME:
    1938     1686319 :       if (pr_is_inert(x)) return triv_gen(bnf, pr_get_p(x), flag);
    1939     1281454 :       x = pr_hnf(nf, x);
    1940     1281453 :       break;
    1941       23681 :     case id_MAT:
    1942       23681 :       if (lg(x)==1) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1943       23681 :       if (nf_get_degree(nf) != lg(x)-1)
    1944           0 :         pari_err_TYPE("idealtyp [dimension != degree]", x);
    1945             :   }
    1946     1305133 :   pr = prec_arch(bnf); /* precision of unit matrix */
    1947     1305134 :   c = getrand();
    1948             :   for (;;)
    1949           7 :   {
    1950     1305144 :     pari_sp av1 = avma;
    1951     1305144 :     GEN y = isprincipalall(bnf,x,&pr,flag);
    1952     1305142 :     if (y) return gerepilecopy(av, y);
    1953             : 
    1954           7 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",pr);
    1955           7 :     set_avma(av1); bnf = bnfnewprec_shallow(bnf,pr); setrand(c);
    1956             :   }
    1957             : }
    1958             : GEN
    1959      174553 : isprincipal(GEN bnf,GEN x) { return bnfisprincipal0(bnf,x,0); }
    1960             : 
    1961             : /* FIXME: OBSOLETE */
    1962             : GEN
    1963           0 : isprincipalgen(GEN bnf,GEN x)
    1964           0 : { return bnfisprincipal0(bnf,x,nf_GEN); }
    1965             : GEN
    1966           0 : isprincipalforce(GEN bnf,GEN x)
    1967           0 : { return bnfisprincipal0(bnf,x,nf_FORCE); }
    1968             : GEN
    1969           0 : isprincipalgenforce(GEN bnf,GEN x)
    1970           0 : { return bnfisprincipal0(bnf,x,nf_GEN | nf_FORCE); }
    1971             : 
    1972             : /* lg(u) > 1 */
    1973             : static int
    1974          91 : RgV_is1(GEN u) { return isint1(gel(u,1)) && RgV_isscalar(u); }
    1975             : static GEN
    1976       29623 : add_principal_part(GEN nf, GEN u, GEN v, long flag)
    1977             : {
    1978       29623 :   if (flag & nf_GENMAT)
    1979       14237 :     return (typ(u) == t_COL && RgV_is1(u))? v: famat_mul_shallow(v,u);
    1980             :   else
    1981       15386 :     return nfmul(nf, v, u);
    1982             : }
    1983             : 
    1984             : #if 0
    1985             : /* compute C prod P[i]^e[i],  e[i] >=0 for all i. C may be NULL (omitted)
    1986             :  * e destroyed ! */
    1987             : static GEN
    1988             : expand(GEN nf, GEN C, GEN P, GEN e)
    1989             : {
    1990             :   long i, l = lg(e), done = 1;
    1991             :   GEN id = C;
    1992             :   for (i=1; i<l; i++)
    1993             :   {
    1994             :     GEN ei = gel(e,i);
    1995             :     if (signe(ei))
    1996             :     {
    1997             :       if (mod2(ei)) id = id? idealmul(nf, id, gel(P,i)): gel(P,i);
    1998             :       ei = shifti(ei,-1);
    1999             :       if (signe(ei)) done = 0;
    2000             :       gel(e,i) = ei;
    2001             :     }
    2002             :   }
    2003             :   if (id != C) id = idealred(nf, id);
    2004             :   if (done) return id;
    2005             :   return idealmulred(nf, id, idealsqr(nf, expand(nf,id,P,e)));
    2006             : }
    2007             : /* C is an extended ideal, possibly with C[1] = NULL */
    2008             : static GEN
    2009             : expandext(GEN nf, GEN C, GEN P, GEN e)
    2010             : {
    2011             :   long i, l = lg(e), done = 1;
    2012             :   GEN A = gel(C,1);
    2013             :   for (i=1; i<l; i++)
    2014             :   {
    2015             :     GEN ei = gel(e,i);
    2016             :     if (signe(ei))
    2017             :     {
    2018             :       if (mod2(ei)) A = A? idealmul(nf, A, gel(P,i)): gel(P,i);
    2019             :       ei = shifti(ei,-1);
    2020             :       if (signe(ei)) done = 0;
    2021             :       gel(e,i) = ei;
    2022             :     }
    2023             :   }
    2024             :   if (A == gel(C,1))
    2025             :     A = C;
    2026             :   else
    2027             :     A = idealred(nf, mkvec2(A, gel(C,2)));
    2028             :   if (done) return A;
    2029             :   return idealmulred(nf, A, idealsqr(nf, expand(nf,A,P,e)));
    2030             : }
    2031             : #endif
    2032             : 
    2033             : static GEN
    2034           0 : expand(GEN nf, GEN C, GEN P, GEN e)
    2035             : {
    2036           0 :   long i, l = lg(e);
    2037           0 :   GEN B, A = C;
    2038           0 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    2039           0 :     if (signe(gel(e,i)))
    2040             :     {
    2041           0 :       B = idealpowred(nf, gel(P,i), gel(e,i));
    2042           0 :       A = A? idealmulred(nf,A,B): B;
    2043             :     }
    2044           0 :   return A;
    2045             : }
    2046             : static GEN
    2047       29636 : expandext(GEN nf, GEN C, GEN P, GEN e)
    2048             : {
    2049       29636 :   long i, l = lg(e);
    2050       29636 :   GEN B, A = gel(C,1), C1 = A;
    2051       89542 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    2052       59905 :     if (signe(gel(e,i)))
    2053             :     {
    2054       32363 :       gel(C,1) = gel(P,i);
    2055       32363 :       B = idealpowred(nf, C, gel(e,i));
    2056       32364 :       A = A? idealmulred(nf,A,B): B;
    2057             :     }
    2058       29637 :   return A == C1? C: A;
    2059             : }
    2060             : 
    2061             : /* isprincipal for C * \prod P[i]^e[i] (C omitted if NULL) */
    2062             : GEN
    2063       29637 : isprincipalfact(GEN bnf, GEN C, GEN P, GEN e, long flag)
    2064             : {
    2065       29637 :   const long gen = flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL);
    2066             :   long prec;
    2067       29637 :   pari_sp av = avma;
    2068       29637 :   GEN C0, Cext, c, id, nf = bnf_get_nf(bnf);
    2069             : 
    2070       29637 :   if (gen)
    2071             :   {
    2072       14244 :     Cext = (flag & nf_GENMAT)? trivial_fact()
    2073       29637 :                              : mkpolmod(gen_1,nf_get_pol(nf));
    2074       29637 :     C0 = mkvec2(C, Cext);
    2075       29636 :     id = expandext(nf, C0, P, e);
    2076             :   } else {
    2077           0 :     Cext = NULL;
    2078           0 :     C0 = C;
    2079           0 :     id = expand(nf, C, P, e);
    2080             :   }
    2081       29637 :   if (id == C0) /* e = 0 */
    2082             :   {
    2083       12415 :     if (!C) return bnfisprincipal0(bnf, gen_1, flag);
    2084       12408 :     switch(typ(C))
    2085             :     {
    2086           7 :       case t_INT: case t_FRAC: case t_POL: case t_POLMOD: case t_COL:
    2087           7 :         return triv_gen(bnf, C, flag);
    2088             :     }
    2089       12401 :     C = idealhnf_shallow(nf,C);
    2090             :   }
    2091             :   else
    2092             :   {
    2093       17222 :     if (gen) { C = gel(id,1); Cext = gel(id,2); } else C = id;
    2094             :   }
    2095       29623 :   prec = prec_arch(bnf);
    2096       29623 :   c = getrand();
    2097             :   for (;;)
    2098          70 :   {
    2099       29693 :     pari_sp av1 = avma;
    2100       29693 :     GEN y = isprincipalall(bnf, C, &prec, flag);
    2101       29693 :     if (y)
    2102             :     {
    2103       29623 :       if (flag & nf_GEN_IF_PRINCIPAL)
    2104             :       {
    2105       20545 :         if (typ(y) == t_INT) return gc_NULL(av);
    2106       20545 :         y = add_principal_part(nf, y, Cext, flag);
    2107             :       }
    2108             :       else
    2109             :       {
    2110        9078 :         GEN u = gel(y,2);
    2111        9078 :         if (!gen || typ(y) != t_VEC) return gerepileupto(av,y);
    2112        9078 :         if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    2113             :       }
    2114       29623 :       return gerepilecopy(av, y);
    2115             :     }
    2116          70 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",prec);
    2117          70 :     set_avma(av1); bnf = bnfnewprec_shallow(bnf,prec); setrand(c);
    2118             :   }
    2119             : }
    2120             : GEN
    2121           0 : isprincipalfact_or_fail(GEN bnf, GEN C, GEN P, GEN e)
    2122             : {
    2123           0 :   const long flag = nf_GENMAT|nf_FORCE;
    2124             :   long prec;
    2125           0 :   pari_sp av = avma;
    2126           0 :   GEN u, y, id, C0, Cext, nf = bnf_get_nf(bnf);
    2127             : 
    2128           0 :   Cext = trivial_fact();
    2129           0 :   C0 = mkvec2(C, Cext);
    2130           0 :   id = expandext(nf, C0, P, e);
    2131           0 :   if (id == C0) /* e = 0 */
    2132           0 :     C = idealhnf_shallow(nf,C);
    2133             :   else {
    2134           0 :     C = gel(id,1); Cext = gel(id,2);
    2135             :   }
    2136           0 :   prec = prec_arch(bnf);
    2137           0 :   y = isprincipalall(bnf, C, &prec, flag);
    2138           0 :   if (!y) return gc_utoipos(av, prec);
    2139           0 :   u = gel(y,2);
    2140           0 :   if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    2141           0 :   return gerepilecopy(av, y);
    2142             : }
    2143             : 
    2144             : GEN
    2145      148627 : nfsign_from_logarch(GEN LA, GEN invpi, GEN archp)
    2146             : {
    2147      148627 :   long l = lg(archp), i;
    2148      148627 :   GEN y = cgetg(l, t_VECSMALL);
    2149      148628 :   pari_sp av = avma;
    2150             : 
    2151      279246 :   for (i=1; i<l; i++)
    2152             :   {
    2153      130618 :     GEN c = ground( gmul(imag_i(gel(LA,archp[i])), invpi) );
    2154      130617 :     y[i] = mpodd(c)? 1: 0;
    2155             :   }
    2156      148628 :   set_avma(av); return y;
    2157             : }
    2158             : 
    2159             : GEN
    2160      226809 : nfsign_tu(GEN bnf, GEN archp)
    2161             : {
    2162             :   long n;
    2163      226809 :   if (bnf_get_tuN(bnf) != 2) return cgetg(1, t_VECSMALL);
    2164      159806 :   n = archp? lg(archp) - 1: nf_get_r1(bnf_get_nf(bnf));
    2165      159806 :   return const_vecsmall(n, 1);
    2166             : }
    2167             : GEN
    2168      228011 : nfsign_fu(GEN bnf, GEN archp)
    2169             : {
    2170      228011 :   GEN invpi, y, A = bnf_get_logfu(bnf), nf = bnf_get_nf(bnf);
    2171      228032 :   long j = 1, RU = lg(A);
    2172             : 
    2173      228032 :   if (!archp) archp = identity_perm( nf_get_r1(nf) );
    2174      228032 :   invpi = invr( mppi(nf_get_prec(nf)) );
    2175      228031 :   y = cgetg(RU,t_MAT);
    2176      376583 :   for (j = 1; j < RU; j++)
    2177      148529 :     gel(y,j) = nfsign_from_logarch(gel(A,j), invpi, archp);
    2178      228054 :   return y;
    2179             : }
    2180             : GEN
    2181          35 : nfsign_units(GEN bnf, GEN archp, int add_zu)
    2182             : {
    2183          35 :   GEN sfu = nfsign_fu(bnf, archp);
    2184          35 :   return add_zu? vec_prepend(sfu, nfsign_tu(bnf, archp)): sfu;
    2185             : }
    2186             : 
    2187             : /* obsolete */
    2188             : GEN
    2189           7 : signunits(GEN bnf)
    2190             : {
    2191             :   pari_sp av;
    2192             :   GEN S, y, nf;
    2193             :   long i, j, r1, r2;
    2194             : 
    2195           7 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    2196           7 :   nf_get_sign(nf, &r1,&r2);
    2197           7 :   S = zeromatcopy(r1, r1+r2-1); av = avma;
    2198           7 :   y = nfsign_fu(bnf, NULL);
    2199          14 :   for (j = 1; j < lg(y); j++)
    2200             :   {
    2201           7 :     GEN Sj = gel(S,j), yj = gel(y,j);
    2202          21 :     for (i = 1; i <= r1; i++) gel(Sj,i) = yj[i]? gen_m1: gen_1;
    2203             :   }
    2204           7 :   set_avma(av); return S;
    2205             : }
    2206             : 
    2207             : static GEN
    2208      728477 : get_log_embed(REL_t *rel, GEN M, long RU, long R1, long prec)
    2209             : {
    2210      728477 :   GEN arch, C, z = rel->m;
    2211             :   long i;
    2212      728477 :   arch = typ(z) == t_COL? RgM_RgC_mul(M, z): const_col(nbrows(M), z);
    2213      728479 :   C = cgetg(RU+1, t_COL); arch = glog(arch, prec);
    2214     1676152 :   for (i=1; i<=R1; i++) gel(C,i) = gel(arch,i);
    2215     1592669 :   for (   ; i<=RU; i++) gel(C,i) = gmul2n(gel(arch,i), 1);
    2216      728467 :   return C;
    2217             : }
    2218             : static GEN
    2219     1008082 : rel_embed(REL_t *rel, FB_t *F, GEN embs, long ind, GEN M, long RU, long R1,
    2220             :           long prec)
    2221             : {
    2222             :   GEN C, D, perm;
    2223             :   long i, n;
    2224     1008082 :   if (!rel->relaut) return get_log_embed(rel, M, RU, R1, prec);
    2225             :   /* image of another relation by automorphism */
    2226      279611 :   C = gel(embs, ind - rel->relorig);
    2227      279611 :   perm = gel(F->embperm, rel->relaut);
    2228      279611 :   D = cgetg_copy(C, &n);
    2229     1237001 :   for (i = 1; i < n; i++)
    2230             :   {
    2231      957377 :     long v = perm[i];
    2232      957377 :     gel(D,i) = (v > 0)? gel(C,v): conj_i(gel(C,-v));
    2233             :   }
    2234      279624 :   return D;
    2235             : }
    2236             : static GEN
    2237      114042 : get_embs(FB_t *F, RELCACHE_t *cache, GEN nf, GEN embs, long PREC)
    2238             : {
    2239      114042 :   long ru, j, k, l = cache->last - cache->chk + 1, r1 = nf_get_r1(nf);
    2240      114042 :   GEN M = nf_get_M(nf), nembs = cgetg(cache->last - cache->base+1, t_MAT);
    2241             :   REL_t *rel;
    2242             : 
    2243     5181041 :   for (k = 1; k <= cache->chk - cache->base; k++) gel(nembs,k) = gel(embs,k);
    2244      114042 :   embs = nembs; ru = nbrows(M);
    2245     1089684 :   for (j=1,rel = cache->chk + 1; j < l; rel++,j++,k++)
    2246      975648 :     gel(embs,k) = rel_embed(rel, F, embs, k, M, ru, r1, PREC);
    2247      114036 :   return embs;
    2248             : }
    2249             : static void
    2250      902111 : set_rel_alpha(REL_t *rel, GEN auts, GEN vA, long ind)
    2251             : {
    2252             :   GEN u;
    2253      902111 :   if (!rel->relaut)
    2254      653258 :     u = rel->m;
    2255             :   else
    2256      248853 :     u = ZM_ZC_mul(gel(auts, rel->relaut), gel(vA, ind - rel->relorig));
    2257      902110 :   gel(vA, ind) = u;
    2258      902110 : }
    2259             : static GEN
    2260     3317250 : set_fact(FB_t *F, FACT *fact, GEN e, long *pnz)
    2261             : {
    2262     3317250 :   long n = fact[0].pr;
    2263     3317250 :   GEN c = zero_Flv(F->KC);
    2264     3317380 :   if (!n) /* trivial factorization */
    2265           0 :     *pnz = F->KC+1;
    2266             :   else
    2267             :   {
    2268     3317380 :     long i, nz = minss(fact[1].pr, fact[n].pr);
    2269    14960033 :     for (i = 1; i <= n; i++) c[fact[i].pr] = fact[i].ex;
    2270     3317384 :     if (e)
    2271             :     {
    2272       16669 :       long l = lg(e);
    2273       70855 :       for (i = 1; i < l; i++)
    2274       54186 :         if (e[i]) { long v = F->subFB[i]; c[v] += e[i]; if (v < nz) nz = v; }
    2275             :     }
    2276     3317384 :     *pnz = nz;
    2277             :   }
    2278     3317384 :   return c;
    2279             : }
    2280             : 
    2281             : /* Is cols already in the cache ? bs = index of first non zero coeff in cols
    2282             :  * General check for colinearity useless since exceedingly rare */
    2283             : static int
    2284     3972464 : already_known(RELCACHE_t *cache, long bs, GEN cols)
    2285             : {
    2286             :   REL_t *r;
    2287     3972464 :   long l = lg(cols);
    2288   260831036 :   for (r = cache->last; r > cache->base; r--)
    2289   257806208 :     if (bs == r->nz)
    2290             :     {
    2291    30212457 :       GEN coll = r->R;
    2292    30212457 :       long b = bs;
    2293   220291869 :       while (b < l && cols[b] == coll[b]) b++;
    2294    30212457 :       if (b == l) return 1;
    2295             :     }
    2296     3024828 :   return 0;
    2297             : }
    2298             : 
    2299             : /* Add relation R to cache, nz = index of first non zero coeff in R.
    2300             :  * If relation is a linear combination of the previous ones, return 0.
    2301             :  * Otherwise, update basis and return > 0. Compute mod p (much faster)
    2302             :  * so some kernel vector might not be genuine. */
    2303             : static int
    2304     3976436 : add_rel_i(RELCACHE_t *cache, GEN R, long nz, GEN m, long orig, long aut, REL_t **relp, long in_rnd_rel)
    2305             : {
    2306     3976436 :   long i, k, n = lg(R)-1;
    2307             : 
    2308     3976436 :   if (nz == n+1) { k = 0; goto ADD_REL; }
    2309     3972432 :   if (already_known(cache, nz, R)) return -1;
    2310     3024862 :   if (cache->last >= cache->base + cache->len) return 0;
    2311     3024862 :   if (DEBUGLEVEL>6)
    2312             :   {
    2313           0 :     err_printf("adding vector = %Ps\n",R);
    2314           0 :     err_printf("generators =\n%Ps\n", cache->basis);
    2315             :   }
    2316     3024885 :   if (cache->missing)
    2317             :   {
    2318     2650900 :     GEN a = leafcopy(R), basis = cache->basis;
    2319     2650897 :     k = lg(a);
    2320   134859627 :     do --k; while (!a[k]);
    2321     8524763 :     while (k)
    2322             :     {
    2323     6337927 :       GEN c = gel(basis, k);
    2324     6337927 :       if (c[k])
    2325             :       {
    2326     5873866 :         long ak = a[k];
    2327   401558913 :         for (i=1; i < k; i++) if (c[i]) a[i] = (a[i] + ak*(mod_p-c[i])) % mod_p;
    2328     5873866 :         a[k] = 0;
    2329   219955994 :         do --k; while (!a[k]); /* k cannot go below 0: codeword is a sentinel */
    2330             :       }
    2331             :       else
    2332             :       {
    2333      464061 :         ulong invak = Fl_inv(uel(a,k), mod_p);
    2334             :         /* Cleanup a */
    2335    13958171 :         for (i = k; i-- > 1; )
    2336             :         {
    2337    13494114 :           long j, ai = a[i];
    2338    13494114 :           c = gel(basis, i);
    2339    13494114 :           if (!ai || !c[i]) continue;
    2340      251031 :           ai = mod_p-ai;
    2341     4639967 :           for (j = 1; j < i; j++) if (c[j]) a[j] = (a[j] + ai*c[j]) % mod_p;
    2342      251031 :           a[i] = 0;
    2343             :         }
    2344             :         /* Insert a/a[k] as k-th column */
    2345      464057 :         c = gel(basis, k);
    2346    13958172 :         for (i = 1; i<k; i++) if (a[i]) c[i] = (a[i] * invak) % mod_p;
    2347      464057 :         c[k] = 1; a = c;
    2348             :         /* Cleanup above k */
    2349    13559137 :         for (i = k+1; i<n; i++)
    2350             :         {
    2351             :           long j, ck;
    2352    13095080 :           c = gel(basis, i);
    2353    13095080 :           ck = c[k];
    2354    13095080 :           if (!ck) continue;
    2355     2465490 :           ck = mod_p-ck;
    2356    84871638 :           for (j = 1; j < k; j++) if (a[j]) c[j] = (c[j] + ck*a[j]) % mod_p;
    2357     2465490 :           c[k] = 0;
    2358             :         }
    2359      464057 :         cache->missing--;
    2360      464057 :         break;
    2361             :       }
    2362             :     }
    2363             :   }
    2364             :   else
    2365      373985 :     k = (cache->last - cache->base) + 1;
    2366     3024878 :   if (k || cache->relsup > 0 || (m && in_rnd_rel))
    2367             :   {
    2368             :     REL_t *rel;
    2369             : 
    2370      960649 : ADD_REL:
    2371      964653 :     rel = ++cache->last;
    2372      964653 :     if (!k && cache->relsup && nz < n+1)
    2373             :     {
    2374      122478 :       cache->relsup--;
    2375      122478 :       k = (rel - cache->base) + cache->missing;
    2376             :     }
    2377      964653 :     rel->R  = gclone(R);
    2378      964645 :     rel->m  =  m ? gclone(m) : NULL;
    2379      964644 :     rel->nz = nz;
    2380      964644 :     if (aut)
    2381             :     {
    2382      275492 :       rel->relorig = (rel - cache->base) - orig;
    2383      275492 :       rel->relaut = aut;
    2384             :     }
    2385             :     else
    2386      689152 :       rel->relaut = 0;
    2387      964644 :     if (relp) *relp = rel;
    2388      964644 :     if (DEBUGLEVEL) dbg_newrel(cache);
    2389             :   }
    2390     3028873 :   return k;
    2391             : }
    2392             : 
    2393             : static int
    2394     3488006 : add_rel(RELCACHE_t *cache, FB_t *F, GEN R, long nz, GEN m, long in_rnd_rel)
    2395             : {
    2396             :   REL_t *rel;
    2397             :   long k, l, reln;
    2398     3488006 :   const long lauts = lg(F->idealperm), KC = F->KC;
    2399             : 
    2400     3488006 :   k = add_rel_i(cache, R, nz, m, 0, 0, &rel, in_rnd_rel);
    2401     3488051 :   if (k > 0 && typ(m) != t_INT)
    2402             :   {
    2403      518407 :     GEN Rl = cgetg(KC+1, t_VECSMALL);
    2404      518401 :     reln = rel - cache->base;
    2405     1006858 :     for (l = 1; l < lauts; l++)
    2406             :     {
    2407      488447 :       GEN perml = gel(F->idealperm, l);
    2408      488447 :       long i, nzl = perml[nz];
    2409             : 
    2410    22619390 :       for (i = 1; i <= KC; i++) Rl[i] = 0;
    2411    20070193 :       for (i = nz; i <= KC; i++)
    2412    19581746 :         if (R[i])
    2413             :         {
    2414     1362992 :           long v = perml[i];
    2415             : 
    2416     1362992 :           if (v < nzl) nzl = v;
    2417     1362992 :           Rl[v] = R[i];
    2418             :         }
    2419      488447 :       (void)add_rel_i(cache, Rl, nzl, NULL, reln, l, NULL, in_rnd_rel);
    2420             :     }
    2421             :   }
    2422     3488055 :   return k;
    2423             : }
    2424             : 
    2425             : INLINE void
    2426    58309434 : step(GEN x, double *y, GEN inc, long k)
    2427             : {
    2428    58309434 :   if (!y[k])
    2429     5848369 :     x[k]++; /* leading coeff > 0 */
    2430             :   else
    2431             :   {
    2432    52461065 :     long i = inc[k];
    2433    52461065 :     x[k] += i;
    2434    52461065 :     inc[k] = (i > 0)? -1-i: 1-i;
    2435             :   }
    2436    58309434 : }
    2437             : 
    2438             : INLINE long
    2439      534568 : Fincke_Pohst_ideal(RELCACHE_t *cache, FB_t *F, GEN nf, GEN M, GEN I,
    2440             :     GEN NI, FACT *fact, long Nrelid, FP_t *fp, RNDREL_t *rr, long prec,
    2441             :     long *Nsmall, long *Nfact)
    2442             : {
    2443             :   pari_sp av;
    2444      534568 :   const long N = nf_get_degree(nf), R1 = nf_get_r1(nf);
    2445      534570 :   GEN G = nf_get_G(nf), G0 = nf_get_roundG(nf), r, u, gx, inc, ideal;
    2446             :   double BOUND, B1, B2;
    2447      534569 :   long j, k, skipfirst, relid=0, try_elt=0, try_factor=0;
    2448             : 
    2449      534569 :   inc = const_vecsmall(N, 1);
    2450      534567 :   u = ZM_lll(ZM_mul(G0, I), 0.99, LLL_IM);
    2451      534565 :   ideal = ZM_mul(I,u); /* approximate T2-LLL reduction */
    2452      534553 :   r = gaussred_from_QR(RgM_mul(G, ideal), prec); /* Cholesky for T2 | ideal */
    2453      534569 :   if (!r) pari_err_BUG("small_norm (precision too low)");
    2454             : 
    2455     3223966 :   for (k=1; k<=N; k++)
    2456             :   {
    2457     2689399 :     if (!gisdouble(gcoeff(r,k,k),&(fp->v[k]))) return 0;
    2458    10273621 :     for (j=1; j<k; j++) if (!gisdouble(gcoeff(r,j,k),&(fp->q[j][k]))) return 0;
    2459     2689398 :     if (DEBUGLEVEL>3) err_printf("v[%ld]=%.4g ",k,fp->v[k]);
    2460             :   }
    2461      534567 :   B1 = fp->v[1]; /* T2(ideal[1]) */
    2462      534567 :   B2 = fp->v[2] + B1 * fp->q[1][2] * fp->q[1][2]; /* T2(ideal[2]) */
    2463      534567 :   if (ZV_isscalar(gel(ideal,1))) /* probable */
    2464             :   {
    2465      148167 :     skipfirst = 1;
    2466      148167 :     BOUND = maxdd(BMULT * B1, 2 * B2);
    2467             :   }
    2468             :   else
    2469             :   {
    2470      386399 :     skipfirst = 0;
    2471      386399 :     BOUND = mindd(BMULT * B1, 2 * B2);
    2472             :   }
    2473             :   /* BOUND at most BMULT fp->x smallest known vector */
    2474      534570 :   if (DEBUGLEVEL>1)
    2475             :   {
    2476           0 :     if (DEBUGLEVEL>3) err_printf("\n");
    2477           0 :     err_printf("BOUND = %.4g\n",BOUND);
    2478             :   }
    2479      534567 :   BOUND *= 1 + 1e-6;
    2480      534567 :   k = N; fp->y[N] = fp->z[N] = 0; fp->x[N] = 0;
    2481    31770660 :   for (av = avma;; set_avma(av), step(fp->x,fp->y,inc,k))
    2482    31236063 :   {
    2483             :     GEN R;
    2484             :     long nz;
    2485             :     do
    2486             :     { /* look for primitive element of small norm, cf minim00 */
    2487    43931035 :       int fl = 0;
    2488             :       double p;
    2489    43931035 :       if (k > 1)
    2490             :       {
    2491    12694993 :         long l = k-1;
    2492    12694993 :         fp->z[l] = 0;
    2493   101055465 :         for (j=k; j<=N; j++) fp->z[l] += fp->q[l][j]*fp->x[j];
    2494    12694993 :         p = (double)fp->x[k] + fp->z[k];
    2495    12694993 :         fp->y[l] = fp->y[k] + p*p*fp->v[k];
    2496    12694993 :         if (l <= skipfirst && !fp->y[1]) fl = 1;
    2497    12694993 :         fp->x[l] = (long)floor(-fp->z[l] + 0.5);
    2498    12694993 :         k = l;
    2499             :       }
    2500    12236407 :       for(;; step(fp->x,fp->y,inc,k))
    2501             :       {
    2502    56167394 :         if (!fl)
    2503             :         {
    2504    56509928 :           if (++try_elt > maxtry_ELEMENT) return 0;
    2505    56019211 :           p = (double)fp->x[k] + fp->z[k];
    2506    56019211 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2507             : 
    2508    14836928 :           step(fp->x,fp->y,inc,k);
    2509             : 
    2510    14837019 :           p = (double)fp->x[k] + fp->z[k];
    2511    14837019 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2512             :         }
    2513    12585216 :         fl = 0; inc[k] = 1;
    2514    12585216 :         if (++k > N) return 0;
    2515             :       }
    2516    43582269 :     } while (k > 1);
    2517             : 
    2518             :     /* element complete */
    2519    61800939 :     if (zv_content(fp->x) !=1) continue; /* not primitive */
    2520    30942834 :     gx = ZM_zc_mul(ideal,fp->x);
    2521    30942741 :     if (ZV_isscalar(gx)) continue;
    2522    30904718 :     if (++try_factor > maxtry_FACT) return 0;
    2523             : 
    2524    30859105 :     if (!Nrelid)
    2525             :     {
    2526          63 :       if (!factorgen(F,nf,I,NI,gx,fact)) continue;
    2527          14 :       return 1;
    2528             :     }
    2529    30859042 :     else if (rr)
    2530             :     {
    2531      121532 :       if (!factorgen(F,nf,I,NI,gx,fact)) continue;
    2532       16669 :       add_to_fact(rr->jid, 1, fact);
    2533             :     }
    2534             :     else
    2535             :     {
    2536    30737510 :       GEN Nx, xembed = RgM_RgC_mul(M, gx);
    2537             :       long e;
    2538    30737542 :       if (Nsmall) (*Nsmall)++;
    2539    30737542 :       Nx = grndtoi(embed_norm(xembed, R1), &e);
    2540    30737516 :       if (e >= 0) {
    2541           0 :         if (DEBUGLEVEL > 1) err_printf("+");
    2542    27445032 :         continue;
    2543             :       }
    2544    30737516 :       if (!can_factor(F, nf, NULL, gx, Nx, fact)) continue;
    2545             :     }
    2546             : 
    2547             :     /* smooth element */
    2548     3309006 :     R = set_fact(F, fact, rr ? rr->ex : NULL, &nz);
    2549             :     /* make sure we get maximal rank first, then allow all relations */
    2550     3309185 :     if (add_rel(cache, F, R, nz, gx, rr ? 1 : 0) <= 0)
    2551             :     { /* probably Q-dependent from previous ones: forget it */
    2552     2791102 :       if (DEBUGLEVEL>1) err_printf("*");
    2553     2791099 :       if (DEBUGLEVEL && Nfact && rr) (*Nfact)++;
    2554     2791099 :       continue;
    2555             :     }
    2556      518138 :     if (DEBUGLEVEL && Nfact) (*Nfact)++;
    2557      518138 :     if (cache->last >= cache->end) return 1; /* we have enough */
    2558      421857 :     if (++relid == Nrelid) break;
    2559             :   }
    2560       43856 :   return 0;
    2561             : }
    2562             : 
    2563             : static void
    2564      114669 : small_norm(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, GEN M,
    2565             :            FACT *fact, GEN p0)
    2566             : {
    2567      114669 :   const long prec = nf_get_prec(nf);
    2568             :   FP_t fp;
    2569             :   pari_sp av;
    2570      114669 :   GEN L_jid = F->L_jid, Np0;
    2571      114669 :   long Nsmall, Nfact, n = lg(L_jid);
    2572             :   pari_timer T;
    2573             : 
    2574      114669 :   if (DEBUGLEVEL)
    2575             :   {
    2576           0 :     timer_start(&T);
    2577           0 :     err_printf("#### Look for %ld relations in %ld ideals (small_norm)\n",
    2578           0 :                cache->end - cache->last, lg(L_jid)-1);
    2579           0 :     if (p0) err_printf("Look in p0 = %Ps\n", vecslice(p0,1,4));
    2580             :   }
    2581      114669 :   Nsmall = Nfact = 0;
    2582      114669 :   minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2583      114669 :   Np0 = p0? pr_norm(p0): NULL;
    2584      550161 :   for (av = avma; --n; set_avma(av))
    2585             :   {
    2586      518316 :     long j = L_jid[n];
    2587      518316 :     GEN id = gel(F->LP, j), Nid;
    2588      518316 :     if (DEBUGLEVEL>1)
    2589           0 :       err_printf("\n*** Ideal no %ld: %Ps\n", j, vecslice(id,1,4));
    2590      518317 :     if (p0)
    2591      278129 :     { Nid = mulii(Np0, pr_norm(id)); id = idealmul(nf, p0, id); }
    2592             :     else
    2593      240188 :     { Nid = pr_norm(id); id = pr_hnf(nf, id);}
    2594      518316 :     if (Fincke_Pohst_ideal(cache, F, nf, M, id, Nid, fact, Nrelid, &fp,
    2595       82825 :                            NULL, prec, &Nsmall, &Nfact)) break;
    2596             :   }
    2597      114671 :   if (DEBUGLEVEL && Nsmall)
    2598             :   {
    2599           0 :     if (DEBUGLEVEL == 1)
    2600           0 :     { if (Nfact) err_printf("\n"); }
    2601             :     else
    2602           0 :       err_printf("  \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
    2603           0 :                   Nfact,Nsmall,((double)Nfact)/Nsmall);
    2604           0 :     if (timer_get(&T)>1) timer_printf(&T,"small_norm");
    2605             :   }
    2606      114671 : }
    2607             : 
    2608             : static GEN
    2609       13665 : get_random_ideal(FB_t *F, GEN nf, GEN ex)
    2610             : {
    2611       13665 :   long i, l = lg(ex);
    2612             :   for (;;)
    2613          11 :   {
    2614       13676 :     GEN I = NULL;
    2615       60011 :     for (i = 1; i < l; i++)
    2616       46335 :       if ((ex[i] = random_bits(RANDOM_BITS)))
    2617             :       {
    2618       43457 :         GEN pr = gel(F->LP, F->subFB[i]), e = utoipos(ex[i]);
    2619       43457 :         I = I? idealmulpowprime(nf, I, pr, e): idealpow(nf, pr, e);
    2620             :       }
    2621       13676 :     if (I && !ZM_isscalar(I,NULL)) return I; /* != (n)Z_K */
    2622             :   }
    2623             : }
    2624             : 
    2625             : static void
    2626       13665 : rnd_rel(RELCACHE_t *cache, FB_t *F, GEN nf, FACT *fact)
    2627             : {
    2628             :   pari_timer T;
    2629       13665 :   GEN L_jid = F->L_jid, M = nf_get_M(nf), R, NR;
    2630       13665 :   long i, l = lg(L_jid), prec = nf_get_prec(nf), Nfact = 0;
    2631             :   RNDREL_t rr;
    2632             :   FP_t fp;
    2633             :   pari_sp av;
    2634             : 
    2635       13665 :   if (DEBUGLEVEL) {
    2636           0 :     timer_start(&T);
    2637           0 :     err_printf("#### Look for %ld relations in %ld ideals (rnd_rel)\n",
    2638           0 :                cache->end - cache->last, l-1);
    2639             :   }
    2640       13665 :   rr.ex = cgetg(lg(F->subFB), t_VECSMALL);
    2641       13665 :   R = get_random_ideal(F, nf, rr.ex); /* random product from subFB */
    2642       13665 :   NR = ZM_det_triangular(R);
    2643       13665 :   minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2644       16449 :   for (av = avma, i = 1; i < l; i++, set_avma(av))
    2645             :   { /* try P[j] * base */
    2646       16240 :     long j = L_jid[i];
    2647       16240 :     GEN P = gel(F->LP, j), Nid = mulii(NR, pr_norm(P));
    2648       16240 :     if (DEBUGLEVEL>1) err_printf("\n*** Ideal %ld: %Ps\n", j, vecslice(P,1,4));
    2649       16240 :     rr.jid = j;
    2650       16240 :     if (Fincke_Pohst_ideal(cache, F, nf, M, idealHNF_mul(nf, R, P), Nid, fact,
    2651       13456 :                            RND_REL_RELPID, &fp, &rr, prec, NULL, &Nfact)) break;
    2652             :   }
    2653       13665 :   if (DEBUGLEVEL)
    2654             :   {
    2655           0 :     if (Nfact) err_printf("\n");
    2656           0 :     if (timer_get(&T)>=0) timer_printf(&T,"rnd_rel");
    2657             :   }
    2658       13665 : }
    2659             : 
    2660             : static GEN
    2661       63558 : automorphism_perms(GEN M, GEN auts, GEN cyclic, long r1, long r2, long N)
    2662             : {
    2663       63558 :   long L = lgcols(M), lauts = lg(auts), lcyc = lg(cyclic), i, j, l, m;
    2664       63558 :   GEN Mt, perms = cgetg(lauts, t_VEC);
    2665             :   pari_sp av;
    2666             : 
    2667      127278 :   for (l = 1; l < lauts; l++) gel(perms, l) = cgetg(L, t_VECSMALL);
    2668       63559 :   av = avma;
    2669       63559 :   Mt = shallowtrans(gprec_w(M, LOWDEFAULTPREC));
    2670       63558 :   Mt = shallowconcat(Mt, conj_i(vecslice(Mt, r1+1, r1+r2)));
    2671      110040 :   for (l = 1; l < lcyc; l++)
    2672             :   {
    2673       46484 :     GEN thiscyc = gel(cyclic, l), thisperm, perm, prev, Nt;
    2674       46484 :     long k = thiscyc[1];
    2675             : 
    2676       46484 :     Nt = RgM_mul(shallowtrans(gel(auts, k)), Mt);
    2677       46485 :     perm = gel(perms, k);
    2678      152629 :     for (i = 1; i < L; i++)
    2679             :     {
    2680      106145 :       GEN v = gel(Nt, i), minD;
    2681      106145 :       minD = gnorml2(gsub(v, gel(Mt, 1)));
    2682      106148 :       perm[i] = 1;
    2683      561488 :       for (j = 2; j <= N; j++)
    2684             :       {
    2685      455344 :         GEN D = gnorml2(gsub(v, gel(Mt, j)));
    2686      455337 :         if (gcmp(D, minD) < 0) { minD = D; perm[i] = j >= L ? r2-j : j; }
    2687             :       }
    2688             :     }
    2689       64929 :     for (prev = perm, m = 2; m < lg(thiscyc); m++, prev = thisperm)
    2690             :     {
    2691       18445 :       thisperm = gel(perms, thiscyc[m]);
    2692       93604 :       for (i = 1; i < L; i++)
    2693             :       {
    2694       75159 :         long pp = labs(prev[i]);
    2695       75159 :         thisperm[i] = prev[i] < 0 ? -perm[pp] : perm[pp];
    2696             :       }
    2697             :     }
    2698             :   }
    2699       63556 :   set_avma(av); return perms;
    2700             : }
    2701             : 
    2702             : /* Determine the field automorphisms as matrices on the integral basis */
    2703             : static GEN
    2704       63620 : automorphism_matrices(GEN nf, GEN *cycp)
    2705             : {
    2706       63620 :   pari_sp av = avma;
    2707       63620 :   GEN auts = galoisconj(nf, NULL), mats, cyclic, cyclicidx;
    2708       63620 :   long nauts = lg(auts)-1, i, j, k, l;
    2709             : 
    2710       63620 :   cyclic = cgetg(nauts+1, t_VEC);
    2711       63619 :   cyclicidx = zero_Flv(nauts);
    2712       97680 :   for (l = 1; l <= nauts; l++)
    2713             :   {
    2714       97680 :     GEN aut = gel(auts, l);
    2715       97680 :     if (gequalX(aut)) { swap(gel(auts, l), gel(auts, nauts)); break; }
    2716             :   }
    2717             :   /* trivial automorphism is last */
    2718      190988 :   for (l = 1; l <= nauts; l++) gel(auts, l) = algtobasis(nf, gel(auts, l));
    2719             :   /* Compute maximal cyclic subgroups */
    2720      127369 :   for (l = nauts; --l > 0; ) if (!cyclicidx[l])
    2721             :   {
    2722       47984 :     GEN elt = gel(auts, l), aut = elt, cyc = cgetg(nauts+1, t_VECSMALL);
    2723       47984 :     cyc[1] = cyclicidx[l] = l; j = 1;
    2724             :     do
    2725             :     {
    2726       66987 :       elt = galoisapply(nf, elt, aut);
    2727      217203 :       for (k = 1; k <= nauts; k++) if (gequal(elt, gel(auts, k))) break;
    2728       66983 :       cyclicidx[k] = l; cyc[++j] = k;
    2729             :     }
    2730       66983 :     while (k != nauts);
    2731       47980 :     setlg(cyc, j);
    2732       47982 :     gel(cyclic, l) = cyc;
    2733             :   }
    2734      127368 :   for (i = j = 1; i < nauts; i++)
    2735       63748 :     if (cyclicidx[i] == i) cyclic[j++] = cyclic[i];
    2736       63620 :   setlg(cyclic, j);
    2737       63621 :   mats = cgetg(nauts, t_VEC);
    2738      110135 :   while (--j > 0)
    2739             :   {
    2740       46514 :     GEN cyc = gel(cyclic, j);
    2741       46514 :     long id = cyc[1];
    2742       46514 :     GEN M, Mi, aut = gel(auts, id);
    2743             : 
    2744       46514 :     gel(mats, id) = Mi = M = nfgaloismatrix(nf, aut);
    2745       64960 :     for (i = 2; i < lg(cyc); i++) gel(mats, cyc[i]) = Mi = ZM_mul(Mi, M);
    2746             :   }
    2747       63621 :   gerepileall(av, 2, &mats, &cyclic);
    2748       63620 :   if (cycp) *cycp = cyclic;
    2749       63620 :   return mats;
    2750             : }
    2751             : 
    2752             : /* vP a list of maximal ideals above the same p from idealprimedec: f(P/p) is
    2753             :  * increasing; 1 <= j <= #vP; orbit a zc of length <= #vP; auts a vector of
    2754             :  * automorphisms in ZM form.
    2755             :  * Set orbit[i] = 1 for all vP[i], i >= j, in the orbit of pr = vP[j] wrt auts.
    2756             :  * N.B.1 orbit need not be initialized to 0: useful to incrementally run
    2757             :  * through successive orbits
    2758             :  * N.B.2 i >= j, so primes with index < j will be missed; run incrementally
    2759             :  * starting from j = 1 ! */
    2760             : static void
    2761       11865 : pr_orbit_fill(GEN orbit, GEN auts, GEN vP, long j)
    2762             : {
    2763       11865 :   GEN pr = gel(vP,j), gen = pr_get_gen(pr);
    2764       11865 :   long i, l = lg(auts), J = lg(orbit), f = pr_get_f(pr);
    2765       11865 :   orbit[j] = 1;
    2766       23730 :   for (i = 1; i < l; i++)
    2767             :   {
    2768       11865 :     GEN g = ZM_ZC_mul(gel(auts,i), gen);
    2769             :     long k;
    2770       11886 :     for (k = j+1; k < J; k++)
    2771             :     {
    2772          35 :       GEN prk = gel(vP,k);
    2773          35 :       if (pr_get_f(prk) > f) break; /* f(P[k]) increases with k */
    2774             :       /* don't check that e matches: (almost) always 1 ! */
    2775          35 :       if (!orbit[k] && ZC_prdvd(g, prk)) { orbit[k] = 1; break; }
    2776             :     }
    2777             :   }
    2778       11865 : }
    2779             : /* remark: F->KCZ changes if be_honest() fails */
    2780             : static int
    2781           7 : be_honest(FB_t *F, GEN nf, GEN auts, FACT *fact)
    2782             : {
    2783             :   long i, iz, nbtest;
    2784           7 :   long lgsub = lg(F->subFB), KCZ0 = F->KCZ;
    2785           7 :   long N = nf_get_degree(nf), prec = nf_get_prec(nf);
    2786           7 :   GEN M = nf_get_M(nf);
    2787             :   FP_t fp;
    2788             :   pari_sp av;
    2789             : 
    2790           7 :   if (DEBUGLEVEL) {
    2791           0 :     err_printf("Be honest for %ld primes from %ld to %ld\n", F->KCZ2 - F->KCZ,
    2792           0 :                F->FB[ F->KCZ+1 ], F->FB[ F->KCZ2 ]);
    2793             :   }
    2794           7 :   minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2795           7 :   if (lg(auts) == 1) auts = NULL;
    2796           7 :   av = avma;
    2797          14 :   for (iz=F->KCZ+1; iz<=F->KCZ2; iz++, set_avma(av))
    2798             :   {
    2799           7 :     long p = F->FB[iz];
    2800           7 :     GEN pr_orbit, P = gel(F->LV,p);
    2801           7 :     long j, J = lg(P); /* > 1 */
    2802             :     /* the P|p, NP > C2 are assumed in subgroup generated by FB + last P
    2803             :      * with NP <= C2 is unramified --> check all but last */
    2804           7 :     if (pr_get_e(gel(P,J-1)) == 1) J--;
    2805           7 :     if (J == 1) continue;
    2806           7 :     if (DEBUGLEVEL>1) err_printf("%ld ", p);
    2807           7 :     pr_orbit = auts? zero_zv(J-1): NULL;
    2808          28 :     for (j = 1; j < J; j++)
    2809             :     {
    2810             :       GEN Nid, id, id0;
    2811          21 :       if (pr_orbit)
    2812             :       {
    2813          21 :         if (pr_orbit[j]) continue;
    2814             :         /* discard all primes in automorphism orbit simultaneously */
    2815          14 :         pr_orbit_fill(pr_orbit, auts, P, j);
    2816             :       }
    2817          14 :       id = id0 = pr_hnf(nf,gel(P,j));
    2818          14 :       Nid = pr_norm(gel(P,j));
    2819          14 :       for (nbtest=0;;)
    2820             :       {
    2821          14 :         if (Fincke_Pohst_ideal(NULL, F, nf, M, id, Nid, fact, 0, &fp,
    2822          14 :                                NULL, prec, NULL, NULL)) break;
    2823           0 :         if (++nbtest > maxtry_HONEST)
    2824             :         {
    2825           0 :           if (DEBUGLEVEL)
    2826           0 :             pari_warn(warner,"be_honest() failure on prime %Ps\n", gel(P,j));
    2827           0 :           return 0;
    2828             :         }
    2829             :         /* occurs at most once in the whole function */
    2830           0 :         for (i = 1, id = id0; i < lgsub; i++)
    2831             :         {
    2832           0 :           long ex = random_bits(RANDOM_BITS);
    2833           0 :           if (ex)
    2834             :           {
    2835           0 :             GEN pr = gel(F->LP, F->subFB[i]);
    2836           0 :             id = idealmulpowprime(nf, id, pr, utoipos(ex));
    2837             :           }
    2838             :         }
    2839           0 :         if (!equali1(gcoeff(id,N,N))) id = Q_primpart(id);
    2840           0 :         if (expi(gcoeff(id,1,1)) > 100) id = idealred(nf, id);
    2841           0 :         Nid = ZM_det_triangular(id);
    2842             :       }
    2843             :     }
    2844           7 :     F->KCZ++; /* SUCCESS, "enlarge" factorbase */
    2845             :   }
    2846           7 :   F->KCZ = KCZ0; return gc_bool(av,1);
    2847             : }
    2848             : 
    2849             : /* all primes with N(P) <= BOUND factor on factorbase ? */
    2850             : void
    2851          63 : bnftestprimes(GEN bnf, GEN BOUND)
    2852             : {
    2853          63 :   pari_sp av0 = avma, av;
    2854          63 :   ulong count = 0;
    2855          63 :   GEN auts, p, nf = bnf_get_nf(bnf), Vbase = bnf_get_vbase(bnf);
    2856          63 :   GEN fb = gen_sort_shallow(Vbase, (void*)&cmp_prime_ideal, cmp_nodata);
    2857          63 :   ulong pmax = pr_get_smallp(gel(fb, lg(fb)-1)); /*largest p in factorbase*/
    2858             :   forprime_t S;
    2859             :   FACT *fact;
    2860             :   FB_t F;
    2861             : 
    2862          63 :   (void)recover_partFB(&F, Vbase, nf_get_degree(nf));
    2863          63 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    2864          63 :   forprime_init(&S, gen_2, BOUND);
    2865          63 :   auts = automorphism_matrices(nf, NULL);
    2866          63 :   if (lg(auts) == 1) auts = NULL;
    2867          63 :   av = avma;
    2868       37604 :   while (( p = forprime_next(&S) ))
    2869             :   {
    2870             :     GEN pr_orbit, vP;
    2871             :     long j, J;
    2872             : 
    2873       37541 :     if (DEBUGLEVEL == 1 && ++count > 1000)
    2874             :     {
    2875           0 :       err_printf("passing p = %Ps / %Ps\n", p, BOUND);
    2876           0 :       count = 0;
    2877             :     }
    2878       37541 :     set_avma(av);
    2879       37541 :     vP = idealprimedec_limit_norm(nf, p, BOUND);
    2880       37541 :     J = lg(vP);
    2881             :     /* if last is unramified, all P|p in subgroup generated by FB: skip last */
    2882       37541 :     if (J > 1 && pr_get_e(gel(vP,J-1)) == 1) J--;
    2883       37541 :     if (J == 1) continue;
    2884       14525 :     if (DEBUGLEVEL>1) err_printf("*** p = %Ps\n",p);
    2885       14525 :     pr_orbit = auts? zero_zv(J-1): NULL;
    2886       31549 :     for (j = 1; j < J; j++)
    2887             :     {
    2888       17024 :       GEN P = gel(vP,j);
    2889       17024 :       long k = 0;
    2890       17024 :       if (pr_orbit)
    2891             :       {
    2892       11858 :         if (pr_orbit[j]) continue;
    2893             :         /* discard all primes in automorphism orbit simultaneously */
    2894       11851 :         pr_orbit_fill(pr_orbit, auts, vP, j);
    2895             :       }
    2896       17017 :       if (abscmpiu(p, pmax) > 0 || !(k = tablesearch(fb, P, &cmp_prime_ideal)))
    2897       16408 :         (void)SPLIT(&F, nf, pr_hnf(nf,P), Vbase, fact);
    2898       17017 :       if (DEBUGLEVEL>1)
    2899             :       {
    2900           0 :         err_printf("  Testing P = %Ps\n",P);
    2901           0 :         if (k) err_printf("    #%ld in factor base\n",k);
    2902           0 :         else err_printf("    is %Ps\n", isprincipal(bnf,P));
    2903             :       }
    2904             :     }
    2905             :   }
    2906          63 :   set_avma(av0);
    2907          63 : }
    2908             : 
    2909             : /* A t_MAT of complex floats, in fact reals. Extract a submatrix B
    2910             :  * whose columns are definitely nonzero, i.e. gexpo(A[j]) >= -2
    2911             :  *
    2912             :  * If possible precision problem (t_REAL 0 with large exponent), set
    2913             :  * *precpb to 1 */
    2914             : static GEN
    2915       95015 : clean_cols(GEN A, int *precpb)
    2916             : {
    2917       95015 :   long l = lg(A), h, i, j, k;
    2918             :   GEN B;
    2919       95015 :   *precpb = 0;
    2920       95015 :   if (l == 1) return A;
    2921       95015 :   h = lgcols(A);;
    2922       95015 :   B = cgetg(l, t_MAT);
    2923     3674574 :   for (i = k = 1; i < l; i++)
    2924             :   {
    2925     3579559 :     GEN Ai = gel(A,i);
    2926     3579559 :     int non0 = 0;
    2927    17907574 :     for (j = 1; j < h; j++)
    2928             :     {
    2929    14328016 :       GEN c = gel(Ai,j);
    2930    14328016 :       if (gexpo(c) >= -2)
    2931             :       {
    2932    12365801 :         if (gequal0(c)) *precpb = 1; else non0 = 1;
    2933             :       }
    2934             :     }
    2935     3579558 :     if (non0) gel(B, k++) = Ai;
    2936             :   }
    2937       95015 :   setlg(B, k); return B;
    2938             : }
    2939             : 
    2940             : static long
    2941     3035017 : compute_multiple_of_R_pivot(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2942             : {
    2943     3035017 :   GEN x = gel(X,ix);
    2944     3035017 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2945             :   (void)x0;
    2946    15800800 :   for (i=1; i<lx; i++)
    2947    12765787 :     if (!c[i] && !gequal0(gel(x,i)))
    2948             :     {
    2949     3036971 :       long e = gexpo(gel(x,i));
    2950     3036968 :       if (e > ex) { ex = e; k = i; }
    2951             :     }
    2952     3035013 :   return (k && ex > -32)? k: lx;
    2953             : }
    2954             : 
    2955             : /* Ar = (log |sigma_i(u_j)|) for units (u_j) found so far;
    2956             :  * RU = R1+R2 = target rank for unit matrix, after adding [1 x r1, 2 x r2];
    2957             :  * N = field degree, need = unit rank defect;
    2958             :  * L = NULL (prec problem) or B^(-1) * A with approximate rational entries
    2959             :  * (as t_REAL), B a submatrix of A, with (probably) maximal rank RU */
    2960             : static GEN
    2961      110234 : compute_multiple_of_R(GEN Ar, long RU, long N, long *pneed, long *bit, GEN *ptL)
    2962             : {
    2963             :   GEN T, d, mdet, Im_mdet, kR, L;
    2964      110234 :   long i, j, r, R1 = 2*RU - N;
    2965             :   int precpb;
    2966      110234 :   pari_sp av = avma;
    2967             : 
    2968      110234 :   if (RU == 1) { *ptL = zeromat(0, lg(Ar)-1); return gen_1; }
    2969             : 
    2970       95015 :   if (DEBUGLEVEL) err_printf("\n#### Computing regulator multiple\n");
    2971       95015 :   mdet = clean_cols(Ar, &precpb);
    2972             :   /* will cause precision to increase on later failure, but we may succeed! */
    2973       95015 :   *ptL = precpb? NULL: gen_1;
    2974       95015 :   T = cgetg(RU+1,t_COL);
    2975      255259 :   for (i=1; i<=R1; i++) gel(T,i) = gen_1;
    2976      205161 :   for (   ; i<=RU; i++) gel(T,i) = gen_2;
    2977       95015 :   mdet = shallowconcat(T, mdet); /* det(Span(mdet)) = N * R */
    2978             : 
    2979             :   /* could be using indexrank(), but need custom "get_pivot" function */
    2980       95015 :   d = RgM_pivots(mdet, NULL, &r, &compute_multiple_of_R_pivot);
    2981             :   /* # of independent columns = target rank ? */
    2982       95014 :   if (lg(mdet)-1 - r != RU)
    2983             :   {
    2984       37129 :     if (DEBUGLEVEL)
    2985           0 :       err_printf("Units matrix target rank = %ld < %ld\n",lg(mdet)-1 - r, RU);
    2986       37129 :     *pneed = RU - (lg(mdet)-1-r); return gc_NULL(av);
    2987             :   }
    2988             : 
    2989       57885 :   Im_mdet = cgetg(RU+1, t_MAT); /* extract independent columns */
    2990             :   /* N.B: d[1] = 1, corresponding to T above */
    2991       57885 :   gel(Im_mdet, 1) = T;
    2992      251747 :   for (i = j = 2; i <= RU; j++)
    2993      193862 :     if (d[j]) gel(Im_mdet, i++) = gel(mdet,j);
    2994             : 
    2995             :   /* integral multiple of R: the cols we picked form a Q-basis, they have an
    2996             :    * index in the full lattice. First column is T */
    2997       57885 :   kR = divru(det2(Im_mdet), N);
    2998             :   /* R > 0.2 uniformly */
    2999       57885 :   if (!signe(kR) || expo(kR) < -3)
    3000             :   {
    3001           1 :     if (DEBUGLEVEL) err_printf("Regulator is zero.\n");
    3002           1 :     *pneed = 0; return gc_NULL(av);
    3003             :   }
    3004       57884 :   d = det2(rowslice(vecslice(Im_mdet, 2, RU), 2, RU));
    3005       57884 :   setabssign(d); setabssign(kR);
    3006       57884 :   if (gexpo(gsub(d,kR)) - gexpo(d) > -20) { *ptL = NULL; return gc_NULL(av); }
    3007       57850 :   L = RgM_inv(Im_mdet);
    3008             :   /* estimate # of correct bits in result */
    3009       57851 :   if (!L || (*bit = -gexpo(RgM_Rg_sub_shallow(RgM_mul(L,Im_mdet), gen_1))) < 16)
    3010          18 :   { *ptL = NULL; return gc_NULL(av); }
    3011             : 
    3012       57833 :   *ptL = RgM_mul(rowslice(L,2,RU), Ar); /* approximate rational entries */
    3013       57833 :   return gc_all(av,2, &kR, ptL);
    3014             : }
    3015             : 
    3016             : /* leave small integer n as is, convert huge n to t_REAL (for readability) */
    3017             : static GEN
    3018           0 : i2print(GEN n)
    3019           0 : { return lgefint(n) <= DEFAULTPREC? n: itor(n,LOWDEFAULTPREC); }
    3020             : 
    3021             : static long
    3022       72989 : bad_check(GEN c)
    3023             : {
    3024       72989 :   long ec = gexpo(c);
    3025       72989 :   if (DEBUGLEVEL) err_printf("\n ***** check = %.28Pg\n",c);
    3026             :   /* safe check for c < 0.75 : avoid underflow in gtodouble() */
    3027       72989 :   if (ec < -1 || (ec == -1 && gtodouble(c) < 0.75)) return fupb_PRECI;
    3028             :   /* safe check for c > 1.3 : avoid overflow */
    3029       72989 :   if (ec > 0 || (ec == 0 && gtodouble(c) > 1.3)) return fupb_RELAT;
    3030       63567 :   return fupb_NONE;
    3031             : }
    3032             : /* Input:
    3033             :  * lambda = approximate rational entries: coords of units found so far on a
    3034             :  * sublattice of maximal rank (sublambda)
    3035             :  * *ptkR = regulator of sublambda = multiple of regulator of lambda
    3036             :  * Compute R = true regulator of lambda.
    3037             :  *
    3038             :  * If c := Rz ~ 1, by Dirichlet's formula, then lambda is the full group of
    3039             :  * units AND the full set of relations for the class group has been computed.
    3040             :  * In fact z is a very rough approximation and we only expect 0.75 < Rz < 1.3
    3041             :  *
    3042             :  * Output: *ptkR = R, *ptL = numerator(units) (in terms of lambda) */
    3043             : static long
    3044       73049 : compute_R(GEN lambda, GEN z, GEN *ptL, GEN *ptkR)
    3045             : {
    3046       73049 :   pari_sp av = avma;
    3047       73049 :   long bit, r, reason, RU = lg(lambda) == 1? 1: lgcols(lambda);
    3048             :   GEN L, H, D, den, R, c;
    3049             : 
    3050       73050 :   *ptL = NULL;
    3051       73050 :   if (DEBUGLEVEL) err_printf("\n#### Computing check\n");
    3052       73050 :   if (RU == 1) { *ptkR = gen_1; *ptL = lambda; return bad_check(z); }
    3053       57831 :   D = gmul2n(mpmul(*ptkR,z), 1); /* bound for denom(lambda) */
    3054       57832 :   if (expo(D) < 0 && rtodbl(D) < 0.95) return fupb_PRECI;
    3055       57832 :   L = bestappr(lambda,D);
    3056       57833 :   if (lg(L) == 1)
    3057             :   {
    3058           1 :     if (DEBUGLEVEL) err_printf("truncation error in bestappr\n");
    3059           1 :     return fupb_PRECI;
    3060             :   }
    3061       57832 :   den = Q_denom(L);
    3062       57832 :   if (mpcmp(den,D) > 0)
    3063             :   {
    3064          14 :     if (DEBUGLEVEL) err_printf("D = %Ps\nden = %Ps\n",D, i2print(den));
    3065          14 :     return fupb_PRECI;
    3066             :   }
    3067       57817 :   bit = -gexpo(gsub(L, lambda)); /* input accuracy */
    3068       57818 :   L = Q_muli_to_int(L, den);
    3069       57815 :   if (gexpo(L) + expi(den) > bit - 32)
    3070             :   {
    3071          48 :     if (DEBUGLEVEL) err_printf("dubious bestappr; den = %Ps\n", i2print(den));
    3072          48 :     return fupb_PRECI;
    3073             :   }
    3074       57770 :   H = ZM_hnf(L); r = lg(H)-1;
    3075       57769 :   if (!r || r != nbrows(H))
    3076           0 :     R = gen_0; /* wrong rank */
    3077             :   else
    3078       57769 :     R = gmul(*ptkR, gdiv(ZM_det_triangular(H), powiu(den, r)));
    3079             :   /* R = tentative regulator; regulator > 0.2 uniformly */
    3080       57767 :   if (gexpo(R) < -3) {
    3081           0 :     if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3082           0 :     return gc_long(av, fupb_PRECI);
    3083             :   }
    3084       57770 :   c = gmul(R,z); /* should be n (= 1 if we are done) */
    3085       57770 :   if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3086       57770 :   if ((reason = bad_check(c))) return gc_long(av, reason);
    3087       48383 :   *ptkR = R; *ptL = L; return fupb_NONE;
    3088             : }
    3089             : static GEN
    3090       63657 : get_clg2(GEN cyc, GEN Ga, GEN C, GEN Ur, GEN Ge, GEN M1, GEN M2)
    3091             : {
    3092       63657 :   GEN GD = gsub(act_arch(M1, C), diagact_arch(cyc, Ga));
    3093       63656 :   GEN ga = gsub(act_arch(M2, C), act_arch(Ur, Ga));
    3094       63657 :   return mkvecn(6, Ur, ga, GD, Ge, M1, M2);
    3095             : }
    3096             : /* compute class group (clg1) + data for isprincipal (clg2) */
    3097             : static GEN
    3098       63560 : class_group_gen(GEN nf,GEN W,GEN C,GEN Vbase,long prec, GEN *pclg2)
    3099             : {
    3100             :   GEN M1, M2, z, G, Ga, Ge, cyc, X, Y, D, U, V, Ur, Ui, Uir;
    3101             :   long j, l;
    3102             : 
    3103       63560 :   D = ZM_snfall(W,&U,&V); /* UWV=D, D diagonal, G = g Ui (G=new gens, g=old) */
    3104       63560 :   Ui = ZM_inv(U, NULL);
    3105       63560 :   l = lg(D); cyc = cgetg(l, t_VEC); /* elementary divisors */
    3106       92274 :   for (j = 1; j < l; j++)
    3107             :   {
    3108       30319 :     gel(cyc,j) = gcoeff(D,j,j); /* strip useless components */
    3109       30319 :     if (is_pm1(gel(cyc,j))) break;
    3110             :   }
    3111       63560 :   l = j;
    3112       63560 :   Ur  = ZM_hnfdivrem(U, D, &Y);
    3113       63559 :   Uir = ZM_hnfdivrem(Ui,W, &X);
    3114             :  /* {x} = logarithmic embedding of x (arch. component)
    3115             :   * NB: [J,z] = idealred(I) --> I = y J, with {y} = - z
    3116             :   * G = g Uir - {Ga},  Uir = Ui + WX
    3117             :   * g = G Ur  - {ga},  Ur  = U + DY */
    3118       63559 :   G = cgetg(l,t_VEC);
    3119       63558 :   Ga= cgetg(l,t_MAT);
    3120       63558 :   Ge= cgetg(l,t_COL);
    3121       63558 :   z = init_famat(NULL);
    3122       92270 :   for (j = 1; j < l; j++)
    3123             :   {
    3124       28711 :     GEN I = genback(z, nf, Vbase, gel(Uir,j));
    3125       28714 :     gel(G,j) = gel(I,1); /* generator, order cyc[j] */
    3126       28714 :     gel(Ge,j)= gel(I,2);
    3127       28714 :     gel(Ga,j)= nf_cxlog(nf, gel(I,2), prec);
    3128       28714 :     if (!gel(Ga,j)) pari_err_PREC("class_group_gen");
    3129             :   }
    3130             :   /* {ga} = {GD}Y + G U - g = {GD}Y - {Ga} U + gW X U
    3131             :                             = gW (X Ur + V Y) - {Ga}Ur */
    3132       63559 :   M2 = ZM_add(ZM_mul(X,Ur), ZM_mul(V,Y));
    3133       63559 :   setlg(cyc,l); setlg(V,l); setlg(D,l);
    3134             :   /* G D =: {GD} = g (Ui + W X) D - {Ga}D = g W (V + X D) - {Ga}D
    3135             :    * NB: Ui D = W V. gW is given by (first l-1 cols of) C */
    3136       63559 :   M1 = ZM_add(V, ZM_mul(X,D));
    3137       63558 :   *pclg2 = get_clg2(cyc, Ga, C, Ur, Ge, M1, M2);
    3138       63560 :   return mkvec3(ZV_prod(cyc), cyc, G);
    3139             : }
    3140             : 
    3141             : /* compute principal ideals corresponding to (gen[i]^cyc[i]) */
    3142             : static GEN
    3143        4969 : makecycgen(GEN bnf)
    3144             : {
    3145        4969 :   GEN cyc = bnf_get_cyc(bnf), gen = bnf_get_gen(bnf), nf = bnf_get_nf(bnf);
    3146        4969 :   GEN h, y, GD = bnf_get_GD(bnf), W = bnf_get_W(bnf); /* HNF */
    3147        4969 :   GEN Sunits = bnf_get_sunits(bnf);
    3148        4969 :   GEN X = Sunits? gel(Sunits,1): NULL, C = Sunits? gel(Sunits,3): NULL;
    3149             :   long e, i, l;
    3150             : 
    3151        4969 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building cycgen)");
    3152        4969 :   h = cgetg_copy(gen, &l);
    3153       11639 :   for (i = 1; i < l; i++)
    3154             :   {
    3155        6670 :     GEN gi = gel(gen,i), ci = gel(cyc,i);
    3156        6670 :     if (X && equalii(ci, gcoeff(W,i,i)))
    3157             :     {
    3158             :       long j;
    3159        8557 :       for (j = i+1; j < l; j++)
    3160        3186 :         if (signe(gcoeff(W,i,j))) break;
    3161        5579 :       if (j == i) { gel(h,i) = mkmat2(X, gel(C,i)); continue; }
    3162             :     }
    3163        6670 :     if (abscmpiu(ci, 5) < 0)
    3164             :     {
    3165        5557 :       GEN N = ZM_det_triangular(gi);
    3166        5557 :       y = isprincipalarch(bnf,gel(GD,i), N, ci, gen_1, &e);
    3167        5557 :       if (y && fact_ok(nf,y,NULL,mkvec(gi),mkvec(ci)))
    3168             :       {
    3169        4592 :         gel(h,i) = to_famat_shallow(y,gen_1);
    3170        4592 :         continue;
    3171             :       }
    3172             :     }
    3173        2078 :     y = isprincipalfact(bnf, NULL, mkvec(gi), mkvec(ci), nf_GENMAT|nf_FORCE);
    3174        2078 :     gel(h,i) = gel(y,2);
    3175             :   }
    3176        4969 :   return h;
    3177             : }
    3178             : 
    3179             : static GEN
    3180          77 : get_y(GEN bnf, GEN W, GEN B, GEN C, GEN pFB, long j)
    3181             : {
    3182          77 :   GEN y, nf  = bnf_get_nf(bnf);
    3183          77 :   long e, lW = lg(W)-1;
    3184          77 :   GEN ex = (j<=lW)? gel(W,j): gel(B,j-lW);
    3185          77 :   GEN P = (j<=lW)? NULL: gel(pFB,j);
    3186          77 :   if (C)
    3187             :   { /* archimedean embeddings known: cheap trial */
    3188          77 :     GEN Nx = get_norm_fact_primes(pFB, ex, P);
    3189          77 :     y = isprincipalarch(bnf,gel(C,j), Nx,gen_1, gen_1, &e);
    3190          77 :     if (y && fact_ok(nf,y,P,pFB,ex)) return y;
    3191             :   }
    3192           0 :   y = isprincipalfact_or_fail(bnf, P, pFB, ex);
    3193           0 :   return typ(y) == t_INT? y: gel(y,2);
    3194             : }
    3195             : /* compute principal ideals corresponding to bnf relations */
    3196             : static GEN
    3197          21 : makematal(GEN bnf)
    3198             : {
    3199          21 :   GEN W = bnf_get_W(bnf), B = bnf_get_B(bnf), C = bnf_get_C(bnf);
    3200             :   GEN pFB, ma, retry;
    3201          21 :   long lma, j, prec = 0;
    3202             : 
    3203          21 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building matal)");
    3204          21 :   lma=lg(W)+lg(B)-1;
    3205          21 :   pFB = bnf_get_vbase(bnf);
    3206          21 :   ma = cgetg(lma,t_VEC);
    3207          21 :   retry = vecsmalltrunc_init(lma);
    3208          98 :   for (j=lma-1; j>0; j--)
    3209             :   {
    3210          77 :     pari_sp av = avma;
    3211          77 :     GEN y = get_y(bnf, W, B, C, pFB, j);
    3212          77 :     if (typ(y) == t_INT)
    3213             :     {
    3214           0 :       long E = itos(y);
    3215           0 :       if (DEBUGLEVEL>1) err_printf("\n%ld done later at prec %ld\n",j,E);
    3216           0 :       set_avma(av);
    3217           0 :       vecsmalltrunc_append(retry, j);
    3218           0 :       if (E > prec) prec = E;
    3219             :     }
    3220             :     else
    3221             :     {
    3222          77 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3223          77 :       gel(ma,j) = gerepileupto(av,y);
    3224             :     }
    3225             :   }
    3226          21 :   if (prec)
    3227             :   {
    3228           0 :     long k, l = lg(retry);
    3229           0 :     GEN y, nf = bnf_get_nf(bnf);
    3230           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"makematal",prec);
    3231           0 :     nf = nfnewprec_shallow(nf,prec);
    3232           0 :     bnf = Buchall(nf, nf_FORCE, prec);
    3233           0 :     if (DEBUGLEVEL) err_printf("makematal, adding missing entries:");
    3234           0 :     for (k=1; k<l; k++)
    3235             :     {
    3236           0 :       pari_sp av = avma;
    3237           0 :       long j = retry[k];
    3238           0 :       y = get_y(bnf,W,B,NULL, pFB, j);
    3239           0 :       if (typ(y) == t_INT) pari_err_PREC("makematal");
    3240           0 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3241           0 :       gel(ma,j) = gerepileupto(av,y);
    3242             :     }
    3243             :   }
    3244          21 :   if (DEBUGLEVEL>1) err_printf("\n");
    3245          21 :   return ma;
    3246             : }
    3247             : 
    3248             : enum { MATAL = 1, CYCGEN, UNITS };
    3249             : GEN
    3250       26739 : bnf_build_cycgen(GEN bnf)
    3251       26739 : { return obj_checkbuild(bnf, CYCGEN, &makecycgen); }
    3252             : GEN
    3253          21 : bnf_build_matalpha(GEN bnf)
    3254          21 : { return obj_checkbuild(bnf, MATAL, &makematal); }
    3255             : GEN
    3256       31898 : bnf_build_units(GEN bnf)
    3257       31898 : { return obj_checkbuild(bnf, UNITS, &makeunits); }
    3258             : 
    3259             : /* return fu in compact form if available; in terms of a fixed basis
    3260             :  * of S-units */
    3261             : GEN
    3262          70 : bnf_compactfu_mat(GEN bnf)
    3263             : {
    3264          70 :   GEN X, U, SUnits = bnf_get_sunits(bnf);
    3265          70 :   if (!SUnits) return NULL;
    3266          70 :   X = gel(SUnits,1);
    3267          70 :   U = gel(SUnits,2); ZM_remove_unused(&U, &X);
    3268          70 :   return mkvec2(X, U);
    3269             : }
    3270             : /* return fu in compact form if available; individually as famat */
    3271             : GEN
    3272       37127 : bnf_compactfu(GEN bnf)
    3273             : {
    3274       37127 :   GEN fu, X, U, SUnits = bnf_get_sunits(bnf);
    3275             :   long i, l;
    3276       37127 :   if (!SUnits) return NULL;
    3277       36896 :   X = gel(SUnits,1);
    3278       36896 :   U = gel(SUnits,2); l = lg(U); fu = cgetg(l, t_VEC);
    3279       60177 :   for (i = 1; i < l; i++)
    3280       23282 :     gel(fu,i) = famat_remove_trivial(mkmat2(X, gel(U,i)));
    3281       36895 :   return fu;
    3282             : }
    3283             : /* return expanded fu if available */
    3284             : GEN
    3285      263576 : bnf_has_fu(GEN bnf)
    3286             : {
    3287      263576 :   GEN fu = obj_check(bnf, UNITS);
    3288      263571 :   if (fu) return vecsplice(fu, 1);
    3289      262769 :   fu = bnf_get_fu_nocheck(bnf);
    3290      262769 :   return (typ(fu) == t_MAT)? NULL: fu;
    3291             : }
    3292             : /* return expanded fu if available; build if cheap */
    3293             : GEN
    3294      263297 : bnf_build_cheapfu(GEN bnf)
    3295             : {
    3296             :   GEN fu, SUnits;
    3297      263297 :   if ((fu = bnf_has_fu(bnf))) return fu;
    3298         149 :   if ((SUnits = bnf_get_sunits(bnf)))
    3299             :   {
    3300         149 :     pari_sp av = avma;
    3301         149 :     long e = gexpo(real_i(bnf_get_logfu(bnf)));
    3302         149 :     set_avma(av); if (e < 13) return vecsplice(bnf_build_units(bnf), 1);
    3303             :   }
    3304          77 :   return NULL;
    3305             : }
    3306             : 
    3307             : static GEN
    3308       63659 : get_regulator(GEN A)
    3309             : {
    3310       63659 :   pari_sp av = avma;
    3311             :   GEN R;
    3312             : 
    3313       63659 :   if (lg(A) == 1) return gen_1;
    3314       48468 :   R = det( rowslice(real_i(A), 1, lgcols(A)-2) );
    3315       48467 :   setabssign(R); return gerepileuptoleaf(av, R);
    3316             : }
    3317             : 
    3318             : /* return corrected archimedian components for elts of x (vector)
    3319             :  * (= log(sigma_i(x)) - log(|Nx|) / [K:Q]) */
    3320             : static GEN
    3321          42 : get_archclean(GEN nf, GEN x, long prec, int units)
    3322             : {
    3323          42 :   long k, N, l = lg(x);
    3324          42 :   GEN M = cgetg(l, t_MAT);
    3325             : 
    3326          42 :   if (l == 1) return M;
    3327          28 :   N = nf_get_degree(nf);
    3328         126 :   for (k = 1; k < l; k++)
    3329             :   {
    3330          98 :     pari_sp av = avma;
    3331          98 :     GEN c = nf_cxlog(nf, gel(x,k), prec);
    3332          98 :     if (!c || (!units && !(c = cleanarch(c, N, NULL,prec)))) return NULL;
    3333          98 :     gel(M,k) = gerepilecopy(av, c);
    3334             :   }
    3335          28 :   return M;
    3336             : }
    3337             : static void
    3338          77 : Sunits_archclean(GEN nf, GEN Sunits, GEN *pmun, GEN *pC, long prec)
    3339             : {
    3340          77 :   GEN ipi, M, X = gel(Sunits,1), U = gel(Sunits,2), G = gel(Sunits,3);
    3341          77 :   long k, N = nf_get_degree(nf), l = lg(X);
    3342             : 
    3343          77 :   M = cgetg(l, t_MAT);
    3344        3640 :   for (k = 1; k < l; k++)
    3345        3563 :     if (!(gel(M,k) = nf_cxlog(nf, gel(X,k), prec))) return;
    3346          77 :   ipi = invr(mppi(prec));
    3347          77 :   *pmun = cleanarch(RgM_ZM_mul(M, U), N, ipi, prec); /* not cleanarchunit ! */
    3348          77 :   if (*pmun) *pC = cleanarch(RgM_ZM_mul(M, G), N, ipi, prec);
    3349             : }
    3350             : 
    3351             : GEN
    3352          98 : bnfnewprec_shallow(GEN bnf, long prec)
    3353             : {
    3354          98 :   GEN nf0 = bnf_get_nf(bnf), nf, v, fu, matal, y, A, C;
    3355          98 :   GEN Sunits = bnf_get_sunits(bnf), Ur, Ga, Ge, M1, M2;
    3356          98 :   long r1, r2, prec0 = prec;
    3357             : 
    3358          98 :   nf_get_sign(nf0, &r1, &r2);
    3359          98 :   if (Sunits)
    3360             :   {
    3361          77 :     fu = matal = NULL;
    3362          77 :     prec += nbits2extraprec(gexpo(Sunits));
    3363             :   }
    3364             :   else
    3365             :   {
    3366          21 :     fu = bnf_build_units(bnf);
    3367          21 :     fu = vecslice(fu, 2, lg(fu)-1);
    3368          21 :     if (r1 + r2 > 1) {
    3369          14 :       long e = gexpo(bnf_get_logfu(bnf)) + 1 - TWOPOTBITS_IN_LONG;
    3370          14 :       if (e >= 0) prec += nbits2extraprec(e);
    3371             :     }
    3372          21 :     matal = bnf_build_matalpha(bnf);
    3373             :   }
    3374             : 
    3375          98 :   if (DEBUGLEVEL && prec0 != prec) pari_warn(warnprec,"bnfnewprec",prec);
    3376          98 :   for(C = NULL;;)
    3377           0 :   {
    3378          98 :     pari_sp av = avma;
    3379          98 :     nf = nfnewprec_shallow(nf0,prec);
    3380          98 :     if (Sunits)
    3381          77 :       Sunits_archclean(nf, Sunits, &A, &C, prec);
    3382             :     else
    3383             :     {
    3384          21 :       A = get_archclean(nf, fu, prec, 1);
    3385          21 :       if (A) C = get_archclean(nf, matal, prec, 0);
    3386             :     }
    3387          98 :     if (C) break;
    3388           0 :     set_avma(av); prec = precdbl(prec);
    3389           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"bnfnewprec(extra)",prec);
    3390             :   }
    3391          98 :   y = leafcopy(bnf);
    3392          98 :   gel(y,3) = A;
    3393          98 :   gel(y,4) = C;
    3394          98 :   gel(y,7) = nf;
    3395          98 :   gel(y,8) = v = leafcopy(gel(bnf,8));
    3396          98 :   gel(v,2) = get_regulator(A);
    3397          98 :   v = gel(bnf,9);
    3398          98 :   if (lg(v) < 7) pari_err_TYPE("bnfnewprec [obsolete bnf format]", bnf);
    3399          98 :   Ur = gel(v,1);
    3400          98 :   Ge = gel(v,4);
    3401          98 :   Ga = nfV_cxlog(nf, Ge, prec);
    3402          98 :   M1 = gel(v,5);
    3403          98 :   M2 = gel(v,6);
    3404          98 :   gel(y,9) = get_clg2(bnf_get_cyc(bnf), Ga, C, Ur, Ge, M1, M2);
    3405          98 :   return y;
    3406             : }
    3407             : GEN
    3408          21 : bnfnewprec(GEN bnf, long prec)
    3409             : {
    3410          21 :   pari_sp av = avma;
    3411          21 :   return gerepilecopy(av, bnfnewprec_shallow(checkbnf(bnf), prec));
    3412             : }
    3413             : 
    3414             : GEN
    3415           0 : bnrnewprec_shallow(GEN bnr, long prec)
    3416             : {
    3417           0 :   GEN y = cgetg(7,t_VEC);
    3418             :   long i;
    3419           0 :   gel(y,1) = bnfnewprec_shallow(bnr_get_bnf(bnr), prec);
    3420           0 :   for (i=2; i<7; i++) gel(y,i) = gel(bnr,i);
    3421           0 :   return y;
    3422             : }
    3423             : GEN
    3424           7 : bnrnewprec(GEN bnr, long prec)
    3425             : {
    3426           7 :   GEN y = cgetg(7,t_VEC);
    3427             :   long i;
    3428           7 :   checkbnr(bnr);
    3429           7 :   gel(y,1) = bnfnewprec(bnr_get_bnf(bnr), prec);
    3430          42 :   for (i=2; i<7; i++) gel(y,i) = gcopy(gel(bnr,i));
    3431           7 :   return y;
    3432             : }
    3433             : 
    3434             : static GEN
    3435       64687 : buchall_end(GEN nf,GEN res, GEN clg2, GEN W, GEN B, GEN A, GEN C,GEN Vbase)
    3436             : {
    3437       64687 :   GEN z = obj_init(9, 3);
    3438       64686 :   gel(z,1) = W;
    3439       64686 :   gel(z,2) = B;
    3440       64686 :   gel(z,3) = A;
    3441       64686 :   gel(z,4) = C;
    3442       64686 :   gel(z,5) = Vbase;
    3443       64686 :   gel(z,6) = gen_0;
    3444       64686 :   gel(z,7) = nf;
    3445       64686 :   gel(z,8) = res;
    3446       64686 :   gel(z,9) = clg2;
    3447       64686 :   return z;
    3448             : }
    3449             : 
    3450             : GEN
    3451        2499 : bnfinit0(GEN P, long flag, GEN data, long prec)
    3452             : {
    3453        2499 :   double c1 = 0., c2 = 0.;
    3454        2499 :   long fl, relpid = BNF_RELPID;
    3455             : 
    3456        2499 :   if (data)
    3457             :   {
    3458          21 :     long lx = lg(data);
    3459          21 :     if (typ(data) != t_VEC || lx > 5) pari_err_TYPE("bnfinit",data);
    3460          21 :     switch(lx)
    3461             :     {
    3462           0 :       case 4: relpid = itos(gel(data,3));
    3463          14 :       case 3: c2 = gtodouble(gel(data,2));
    3464          21 :       case 2: c1 = gtodouble(gel(data,1));
    3465             :     }
    3466             :   }
    3467        2499 :   switch(flag)
    3468             :   {
    3469        1722 :     case 2:
    3470        1722 :     case 0: fl = 0; break;
    3471         777 :     case 1: fl = nf_FORCE; break;
    3472           0 :     default: pari_err_FLAG("bnfinit");
    3473             :       return NULL; /* LCOV_EXCL_LINE */
    3474             :   }
    3475        2499 :   return Buchall_param(P, c1, c2, relpid, fl, prec);
    3476             : }
    3477             : GEN
    3478       62191 : Buchall(GEN P, long flag, long prec)
    3479       62191 : { return Buchall_param(P, 0., 0., BNF_RELPID, flag & nf_FORCE, prec); }
    3480             : 
    3481             : static GEN
    3482        1127 : Buchall_deg1(GEN nf)
    3483             : {
    3484        1127 :   GEN v = cgetg(1,t_VEC), m = cgetg(1,t_MAT);
    3485        1127 :   GEN res, W, A, B, C, Vbase = cgetg(1,t_COL);
    3486        1127 :   GEN fu = v, R = gen_1, zu = mkvec2(gen_2, gen_m1);
    3487        1127 :   GEN clg1 = mkvec3(gen_1,v,v), clg2 = mkvecn(6, m,m,m,v,m,m);
    3488             : 
    3489        1127 :   W = A = B = C = m; res = mkvec5(clg1, R, gen_1, zu, fu);
    3490        1127 :   return buchall_end(nf,res,clg2,W,B,A,C,Vbase);
    3491             : }
    3492             : 
    3493             : /* return (small set of) indices of columns generating the same lattice as x.
    3494             :  * Assume HNF(x) is inexpensive (few rows, many columns).
    3495             :  * Dichotomy approach since interesting columns may be at the very end */
    3496             : GEN
    3497       63567 : extract_full_lattice(GEN x)
    3498             : {
    3499       63567 :   long dj, j, k, l = lg(x);
    3500             :   GEN h, h2, H, v;
    3501             : 
    3502       63567 :   if (l < 200) return NULL; /* not worth it */
    3503             : 
    3504           9 :   v = vecsmalltrunc_init(l);
    3505           9 :   H = ZM_hnf(x);
    3506           9 :   h = cgetg(1, t_MAT);
    3507           9 :   dj = 1;
    3508         431 :   for (j = 1; j < l; )
    3509             :   {
    3510         431 :     pari_sp av = avma;
    3511         431 :     long lv = lg(v);
    3512             : 
    3513        3351 :     for (k = 0; k < dj; k++) v[lv+k] = j+k;
    3514         431 :     setlg(v, lv + dj);
    3515         431 :     h2 = ZM_hnf(vecpermute(x, v));
    3516         431 :     if (ZM_equal(h, h2))
    3517             :     { /* these dj columns can be eliminated */
    3518         206 :       set_avma(av); setlg(v, lv);
    3519         206 :       j += dj;
    3520         206 :       if (j >= l) break;
    3521         206 :       dj <<= 1;
    3522         206 :       if (j + dj >= l) { dj = (l - j) >> 1; if (!dj) dj = 1; }
    3523             :     }
    3524         225 :     else if (dj > 1)
    3525             :     { /* at least one interesting column, try with first half of this set */
    3526         164 :       set_avma(av); setlg(v, lv);
    3527         164 :       dj >>= 1; /* > 0 */
    3528             :     }
    3529             :     else
    3530             :     { /* this column should be kept */
    3531          61 :       if (ZM_equal(h2, H)) break;
    3532          52 :       h = h2; j++;
    3533             :     }
    3534             :   }
    3535           9 :   return v;
    3536             : }
    3537             : 
    3538             : static void
    3539       63602 : init_rel(RELCACHE_t *cache, FB_t *F, long add_need)
    3540             : {
    3541       63602 :   const long n = F->KC + add_need; /* expected # of needed relations */
    3542             :   long i, j, k, p;
    3543             :   GEN c, P;
    3544             :   GEN R;
    3545             : 
    3546       63602 :   if (DEBUGLEVEL) err_printf("KCZ = %ld, KC = %ld, n = %ld\n", F->KCZ,F->KC,n);
    3547       63602 :   reallocate(cache, 10*n + 50); /* make room for lots of relations */
    3548       63603 :   cache->chk = cache->base;
    3549       63603 :   cache->end = cache->base + n;
    3550       63603 :   cache->relsup = add_need;
    3551       63603 :   cache->last = cache->base;
    3552       63603 :   cache->missing = lg(cache->basis) - 1;
    3553      302625 :   for (i = 1; i <= F->KCZ; i++)
    3554             :   { /* trivial relations (p) = prod P^e */
    3555      239021 :     p = F->FB[i]; P = gel(F->LV,p);
    3556      239021 :     if (!isclone(P)) continue;
    3557             : 
    3558             :     /* all prime divisors in FB */
    3559      166613 :     c = zero_Flv(F->KC); k = F->iLP[p];
    3560      166613 :     R = c; c += k;
    3561      532056 :     for (j = lg(P)-1; j; j--) c[j] = pr_get_e(gel(P,j));
    3562      166612 :     add_rel(cache, F, R, k+1, pr_get_p(gel(P,1)), 0);
    3563             :   }
    3564       63604 : }
    3565             : 
    3566             : /* Let z = \zeta_n in nf. List of not-obviously-dependent generators for
    3567             :  * cyclotomic units modulo torsion in Q(z) [independent when n a prime power]:
    3568             :  * - z^a - 1,  n/(a,n) not a prime power, a \nmid n unless a=1,  1 <= a < n/2
    3569             :  * - (Z^a - 1)/(Z - 1),  p^k || n, Z = z^{n/p^k}, (p,a) = 1, 1 < a <= (p^k-1)/2
    3570             :  */
    3571             : GEN
    3572       63603 : nfcyclotomicunits(GEN nf, GEN zu)
    3573             : {
    3574       63603 :   long n = itos(gel(zu, 1)), n2, lP, i, a;
    3575             :   GEN z, fa, P, E, L, mz, powz;
    3576       63603 :   if (n <= 6) return cgetg(1, t_VEC);
    3577             : 
    3578        1897 :   z = algtobasis(nf,gel(zu, 2));
    3579        1897 :   if ((n & 3) == 2) { n = n >> 1; z = ZC_neg(z); } /* ensure n != 2 (mod 4) */
    3580        1897 :   n2 = n/2;
    3581        1897 :   mz = zk_multable(nf, z); /* multiplication by z */
    3582        1897 :   powz = cgetg(n2, t_VEC); gel(powz,1) = z;
    3583        6237 :   for (i = 2; i < n2; i++) gel(powz,i) = ZM_ZC_mul(mz, gel(powz,i-1));
    3584             :   /* powz[i] = z^i */
    3585             : 
    3586        1897 :   L = vectrunc_init(n);
    3587        1897 :   fa = factoru(n);
    3588        1897 :   P = gel(fa,1); lP = lg(P);
    3589        1897 :   E = gel(fa,2);
    3590        4578 :   for (i = 1; i < lP; i++)
    3591             :   { /* second kind */
    3592        2681 :     long p = P[i], k = E[i], pk = upowuu(p,k), pk2 = (pk-1) / 2;
    3593        2681 :     GEN u = gen_1;
    3594        4935 :     for (a = 2; a <= pk2; a++)
    3595             :     {
    3596        2254 :       u = nfadd(nf, u, gel(powz, (n/pk) * (a-1))); /* = (Z^a-1)/(Z-1) */
    3597        2254 :       if (a % p) vectrunc_append(L, u);
    3598             :     }
    3599             :   }
    3600        6104 :   if (lP > 2) for (a = 1; a < n2; a++)
    3601             :   { /* first kind, when n not a prime power */
    3602             :     ulong p;
    3603        4207 :     if (a > 1 && (n % a == 0 || uisprimepower(n/ugcd(a,n), &p))) continue;
    3604        1848 :     vectrunc_append(L, nfadd(nf, gel(powz, a), gen_m1));
    3605             :   }
    3606        1897 :   return L;
    3607             : }
    3608             : static void
    3609       63603 : add_cyclotomic_units(GEN nf, GEN zu, RELCACHE_t *cache, FB_t *F)
    3610             : {
    3611       63603 :   pari_sp av = avma;
    3612       63603 :   GEN L = nfcyclotomicunits(nf, zu);
    3613       63603 :   long i, l = lg(L);
    3614       63603 :   if (l > 1)
    3615             :   {
    3616        1897 :     GEN R = zero_Flv(F->KC);
    3617        5901 :     for(i = 1; i < l; i++) add_rel(cache, F, R, F->KC+1, gel(L,i), 0);
    3618             :   }
    3619       63603 :   set_avma(av);
    3620       63603 : }
    3621             : 
    3622             : static GEN
    3623      128545 : trim_list(FB_t *F)
    3624             : {
    3625      128545 :   pari_sp av = avma;
    3626      128545 :   GEN v, L_jid = F->L_jid, minidx = F->minidx, present = zero_Flv(F->KC);
    3627      128545 :   long i, j, imax = minss(lg(L_jid), F->KC + 1);
    3628             : 
    3629      128545 :   v = cgetg(imax, t_VECSMALL);
    3630     2297400 :   for (i = j = 1; i < imax; i++)
    3631             :   {
    3632     2168855 :     long k = minidx[ L_jid[i] ];
    3633     2168855 :     if (!present[k]) { v[j++] = L_jid[i]; present[k] = 1; }
    3634             :   }
    3635      128545 :   setlg(v, j); return gerepileuptoleaf(av, v);
    3636             : }
    3637             : 
    3638             : static void
    3639        8342 : try_elt(RELCACHE_t *cache, FB_t *F, GEN nf, GEN x, FACT *fact)
    3640             : {
    3641        8342 :   pari_sp av = avma;
    3642             :   GEN R, Nx;
    3643        8342 :   long nz, tx = typ(x);
    3644             : 
    3645        8342 :   if (tx == t_INT || tx == t_FRAC) return;
    3646        8205 :   if (tx != t_COL) x = algtobasis(nf, x);
    3647        8205 :   if (RgV_isscalar(x)) return;
    3648        8205 :   x = Q_primpart(x);
    3649        8205 :   Nx = nfnorm(nf, x);
    3650        8205 :   if (!can_factor(F, nf, NULL, x, Nx, fact)) return;
    3651             : 
    3652             :   /* smooth element */
    3653        8205 :   R = set_fact(F, fact, NULL, &nz);
    3654             :   /* make sure we get maximal rank first, then allow all relations */
    3655        8205 :   (void) add_rel(cache, F, R, nz, x, 0);
    3656        8205 :   set_avma(av);
    3657             : }
    3658             : 
    3659             : static void
    3660       46187 : matenlarge(GEN C, long h)
    3661             : {
    3662       46187 :   GEN _0 = zerocol(h);
    3663             :   long i;
    3664     4087247 :   for (i = lg(C); --i; ) gel(C,i) = shallowconcat(gel(C,i), _0);
    3665       46187 : }
    3666             : 
    3667             : /* E = floating point embeddings */
    3668             : static GEN
    3669       46187 : matbotidembs(RELCACHE_t *cache, GEN E)
    3670             : {
    3671       46187 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3672       46187 :   long j, d = h - w, hE = nbrows(E);
    3673       46187 :   GEN y = cgetg(w+1,t_MAT), _0 = zerocol(h);
    3674      192806 :   for (j = 1; j <= w; j++)
    3675             :   {
    3676      146619 :     GEN c = shallowconcat(gel(E,j), _0);
    3677      146619 :     if (d + j >= 1) gel(c, d + j + hE) = gen_1;
    3678      146619 :     gel(y,j) = c;
    3679             :   }
    3680       46187 :   return y;
    3681             : }
    3682             : static GEN
    3683       62042 : matbotid(RELCACHE_t *cache)
    3684             : {
    3685       62042 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3686       62042 :   long j, d = h - w;
    3687       62042 :   GEN y = cgetg(w+1,t_MAT);
    3688      834826 :   for (j = 1; j <= w; j++)
    3689             :   {
    3690      772784 :     GEN c = zerocol(h);
    3691      772784 :     if (d + j >= 1) gel(c, d + j) = gen_1;
    3692      772784 :     gel(y,j) = c;
    3693             :   }
    3694       62042 :   return y;
    3695             : }
    3696             : 
    3697             : static long
    3698         141 : myprecdbl(long prec, GEN C)
    3699             : {
    3700         141 :   long p = prec2nbits(prec) < 1280? precdbl(prec): (long)(prec * 1.5);
    3701         141 :   if (C) p = maxss(p, minss(3*p, prec + nbits2extraprec(gexpo(C))));
    3702         141 :   return p;
    3703             : }
    3704             : 
    3705             : static GEN
    3706       54289 : _nfnewprec(GEN nf, long prec, long *isclone)
    3707             : {
    3708       54289 :   GEN NF = gclone(nfnewprec_shallow(nf, prec));
    3709       54289 :   if (*isclone) gunclone(nf);
    3710       54289 :   *isclone = 1; return NF;
    3711             : }
    3712             : 
    3713             : /* Nrelid = nb relations per ideal, possibly 0. If flag is set, keep data in
    3714             :  * algebraic form. */
    3715             : GEN
    3716       64687 : Buchall_param(GEN P, double cbach, double cbach2, long Nrelid, long flag, long prec)
    3717             : {
    3718             :   pari_timer T;
    3719       64687 :   pari_sp av0 = avma, av, av2;
    3720             :   long PREC, N, R1, R2, RU, low, high, LIMC0, LIMC, LIMC2, LIMCMAX, zc, i;
    3721       64687 :   long LIMres, bit = 0, flag_nfinit = 0;
    3722       64687 :   long nreldep, sfb_trials, need, old_need, precdouble = 0, TRIES = 0;
    3723       64687 :   long nfisclone = 0;
    3724             :   long done_small, small_fail, fail_limit, squash_index, small_norm_prec;
    3725             :   double LOGD, LOGD2, lim;
    3726       64687 :   GEN computed = NULL, fu = NULL, zu, nf, M_sn, D, A, W, R, h, Ce, PERM;
    3727             :   GEN small_multiplier, auts, cyclic, embs, SUnits;
    3728             :   GEN res, L, invhr, B, C, lambda, dep, clg1, clg2, Vbase;
    3729       64687 :   const char *precpb = NULL;
    3730             :   nfmaxord_t nfT;
    3731             :   RELCACHE_t cache;
    3732             :   FB_t F;
    3733             :   GRHcheck_t GRHcheck;
    3734             :   FACT *fact;
    3735             : 
    3736       64687 :   if (DEBUGLEVEL) timer_start(&T);
    3737       64687 :   P = get_nfpol(P, &nf);
    3738       64677 :   if (nf)
    3739        3514 :     D = nf_get_disc(nf);
    3740             :   else
    3741             :   {
    3742       61163 :     nfinit_basic(&nfT, P);
    3743       61168 :     D = nfT.dK;
    3744       61168 :     if (!ZX_is_monic(nfT.T0))
    3745             :     {
    3746          14 :       pari_warn(warner,"nonmonic polynomial in bnfinit, using polredbest");
    3747          14 :       flag_nfinit = nf_RED;
    3748             :     }
    3749             :   }
    3750       64682 :   PREC = maxss(DEFAULTPREC, prec);
    3751       64682 :   N = degpol(P);
    3752       64683 :   if (N <= 1)
    3753             :   {
    3754        1127 :     if (!nf) nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3755        1127 :     return gerepilecopy(av0, Buchall_deg1(nf));
    3756             :   }
    3757       63556 :   D = absi_shallow(D);
    3758       63556 :   LOGD = dbllog2(D) * M_LN2;
    3759       63555 :   LOGD2 = LOGD*LOGD;
    3760       63555 :   LIMCMAX = (long)(12.*LOGD2);
    3761             :   /* In small_norm, LLL reduction produces v0 in I such that
    3762             :    *     T2(v0) <= (4/3)^((n-1)/2) NI^(2/n) disc(K)^(1/n)
    3763             :    * We consider v with T2(v) <= BMULT * T2(v0)
    3764             :    * Hence Nv <= ((4/3)^((n-1)/2) * BMULT / n)^(n/2) NI sqrt(disc(K)).
    3765             :    * NI <= LIMCMAX^2 */
    3766       63555 :   if (nf) PREC = maxss(PREC, nf_get_prec(nf));
    3767       63555 :   PREC = maxss(PREC, nbits2prec((long)(LOGD2 * 0.02) + N*N));
    3768       63555 :   if (DEBUGLEVEL) err_printf("PREC = %ld\n", PREC);
    3769       63555 :   small_norm_prec = nbits2prec( BITS_IN_LONG +
    3770       63555 :     (N/2. * ((N-1)/2.*log(4./3) + log(BMULT/(double)N))
    3771       63555 :      + 2*log((double) LIMCMAX) + LOGD/2) / M_LN2 ); /*enough to compute norms*/
    3772       63555 :   if (small_norm_prec > PREC) PREC = small_norm_prec;
    3773       63555 :   if (!nf)
    3774       60217 :     nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3775        3338 :   else if (nf_get_prec(nf) < PREC)
    3776         199 :     nf = nfnewprec_shallow(nf, PREC);
    3777       63558 :   M_sn = nf_get_M(nf);
    3778       63558 :   if (PREC > small_norm_prec) M_sn = gprec_w(M_sn, small_norm_prec);
    3779             : 
    3780       63558 :   zu = nfrootsof1(nf);
    3781       63557 :   gel(zu,2) = nf_to_scalar_or_alg(nf, gel(zu,2));
    3782             : 
    3783       63558 :   nf_get_sign(nf, &R1, &R2); RU = R1+R2;
    3784       63558 :   auts = automorphism_matrices(nf, &cyclic);
    3785       63557 :   F.embperm = automorphism_perms(nf_get_M(nf), auts, cyclic, R1, R2, N);
    3786       63556 :   if (DEBUGLEVEL)
    3787             :   {
    3788           0 :     timer_printf(&T, "nfinit & nfrootsof1");
    3789           0 :     err_printf("%s bnf: R1 = %ld, R2 = %ld\nD = %Ps\n",
    3790             :                flag? "Algebraic": "Floating point", R1,R2, D);
    3791             :   }
    3792       63556 :   if (LOGD < 20.)
    3793             :   { /* tiny disc, Minkowski may be smaller than Bach */
    3794       62114 :     lim = exp(-N + R2 * log(4/M_PI) + LOGD/2) * sqrt(2*M_PI*N);
    3795       62114 :     if (lim < 3) lim = 3;
    3796             :   }
    3797             :   else /* to be ignored */
    3798        1442 :     lim = -1;
    3799       63556 :   if (cbach > 12.) {
    3800           0 :     if (cbach2 < cbach) cbach2 = cbach;
    3801           0 :     cbach = 12.;
    3802             :   }
    3803       63556 :   if (cbach < 0.)
    3804           0 :     pari_err_DOMAIN("Buchall","Bach constant","<",gen_0,dbltor(cbach));
    3805             : 
    3806       63556 :   cache.base = NULL; F.subFB = NULL; F.LP = NULL; SUnits = Ce = NULL;
    3807       63556 :   init_GRHcheck(&GRHcheck, N, R1, LOGD);
    3808       63559 :   high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
    3809      309948 :   while (!GRHchk(nf, &GRHcheck, high)) { low = high; high *= 2; }
    3810      246434 :   while (high - low > 1)
    3811             :   {
    3812      182874 :     long test = (low+high)/2;
    3813      182874 :     if (GRHchk(nf, &GRHcheck, test)) high = test; else low = test;
    3814             :   }
    3815       63560 :   LIMC2 = (high == LIMC0+1 && GRHchk(nf, &GRHcheck, LIMC0))? LIMC0: high;
    3816       63560 :   if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
    3817             :   /* Assuming GRH, {P, NP <= LIMC2} generate Cl(K) */
    3818       63560 :   if (DEBUGLEVEL) err_printf("LIMC2 = %ld\n", LIMC2);
    3819       63560 :   LIMC0 = (long)(cbach*LOGD2); /* initial value for LIMC */
    3820       63560 :   LIMC = cbach? LIMC0: LIMC2; /* use {P, NP <= LIMC} as a factorbase */
    3821       63560 :   LIMC = maxss(LIMC, nthideal(&GRHcheck, nf, N));
    3822       63560 :   if (DEBUGLEVEL) timer_printf(&T, "computing Bach constant");
    3823       63560 :   LIMres = primeneeded(N, R1, R2, LOGD);
    3824       63560 :   cache_prime_dec(&GRHcheck, LIMres, nf);
    3825             :   /* invhr ~ 2^r1 (2pi)^r2 / sqrt(D) w * Res(zeta_K, s=1) = 1 / hR */
    3826      127120 :   invhr = gmul(gdiv(gmul2n(powru(mppi(DEFAULTPREC), R2), RU),
    3827       63560 :               mulri(gsqrt(D,DEFAULTPREC),gel(zu,1))),
    3828             :               compute_invres(&GRHcheck, LIMres));
    3829       63560 :   if (DEBUGLEVEL) timer_printf(&T, "computing inverse of hR");
    3830       63560 :   av = avma;
    3831             : 
    3832       65780 : START:
    3833       65780 :   if (DEBUGLEVEL) timer_start(&T);
    3834       65780 :   if (TRIES) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
    3835       65780 :   if (DEBUGLEVEL && LIMC > LIMC0)
    3836           0 :     err_printf("%s*** Bach constant: %f\n", TRIES?"\n":"", LIMC/LOGD2);
    3837       65780 :   if (cache.base)
    3838             :   {
    3839             :     REL_t *rel;
    3840       23556 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    3841       23513 :       if (rel->m) i++;
    3842          43 :     computed = cgetg(i, t_VEC);
    3843       23556 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    3844       23513 :       if (rel->m) gel(computed, i++) = rel->m;
    3845          43 :     computed = gclone(computed); delete_cache(&cache);
    3846             :   }
    3847       65780 :   TRIES++; set_avma(av);
    3848       65780 :   if (F.LP) delete_FB(&F);
    3849       65780 :   if (LIMC2 < LIMC) LIMC2 = LIMC;
    3850       65780 :   if (DEBUGLEVEL) { err_printf("LIMC = %ld, LIMC2 = %ld\n",LIMC,LIMC2); }
    3851             : 
    3852       65780 :   FBgen(&F, nf, N, LIMC, LIMC2, &GRHcheck);
    3853       65775 :   if (!F.KC) goto START;
    3854       65775 :   av = avma;
    3855       65775 :   subFBgen(&F,auts,cyclic,lim < 0? LIMC2: mindd(lim,LIMC2),MINSFB);
    3856       65780 :   if (lg(F.subFB) == 1) goto START;
    3857       63603 :   if (DEBUGLEVEL)
    3858           0 :     timer_printf(&T, "factorbase (#subFB = %ld) and ideal permutations",
    3859           0 :                      lg(F.subFB)-1);
    3860             : 
    3861       63603 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    3862       63602 :   PERM = leafcopy(F.perm); /* to be restored in case of precision increase */
    3863       63603 :   cache.basis = zero_Flm_copy(F.KC,F.KC);
    3864       63603 :   small_multiplier = zero_Flv(F.KC);
    3865       63603 :   done_small = small_fail = squash_index = zc = sfb_trials = nreldep = 0;
    3866       63603 :   fail_limit = F.KC + 1;
    3867       63603 :   W = A = R = NULL;
    3868       63603 :   av2 = avma;
    3869       63603 :   init_rel(&cache, &F, RELSUP + RU-1);
    3870       63603 :   old_need = need = cache.end - cache.last;
    3871       63603 :   add_cyclotomic_units(nf, zu, &cache, &F);
    3872       63603 :   if (DEBUGLEVEL) err_printf("\n");
    3873       63603 :   cache.end = cache.last + need;
    3874             : 
    3875       63603 :   if (computed)
    3876             :   {
    3877        8385 :     for (i = 1; i < lg(computed); i++)
    3878        8342 :       try_elt(&cache, &F, nf, gel(computed, i), fact);
    3879          43 :     gunclone(computed);
    3880          43 :     if (DEBUGLEVEL && i > 1)
    3881           0 :       timer_printf(&T, "including already computed relations");
    3882          43 :     need = 0;
    3883             :   }
    3884             : 
    3885             :   do
    3886             :   {
    3887             :     GEN Ar, C0;
    3888             :     do
    3889             :     {
    3890      128711 :       pari_sp av4 = avma;
    3891      128711 :       if (need > 0)
    3892             :       {
    3893      128545 :         long oneed = cache.end - cache.last;
    3894             :         /* Test below can be true if small_norm did not find enough linearly
    3895             :          * dependent relations */
    3896      128545 :         if (need < oneed) need = oneed;
    3897      128545 :         pre_allocate(&cache, need+lg(auts)-1+(R ? lg(W)-1 : 0));
    3898      128545 :         cache.end = cache.last + need;
    3899      128545 :         F.L_jid = trim_list(&F);
    3900             :       }
    3901      128711 :       if (need > 0 && Nrelid > 0 && (done_small <= F.KC+1 || A) &&
    3902      127531 :           small_fail <= fail_limit &&
    3903      127531 :           cache.last < cache.base + 2*F.KC+2*RU+RELSUP /* heuristic */)
    3904             :       {
    3905      114837 :         long j, k, LIE = (R && lg(W) > 1 && (done_small % 2));
    3906      114837 :         REL_t *last = cache.last;
    3907      114837 :         pari_sp av3 = avma;
    3908             :         GEN p0;
    3909      114837 :         if (LIE)
    3910             :         { /* We have full rank for class group and unit. The following tries to
    3911             :            * improve the prime group lattice by looking for relations involving
    3912             :            * the primes generating the class group. */
    3913        3196 :           long n = lg(W)-1; /* need n relations to squash the class group */
    3914        3196 :           F.L_jid = vecslice(F.perm, 1, n);
    3915        3196 :           cache.end = cache.last + n;
    3916             :           /* Lie to the add_rel subsystem: pretend we miss relations involving
    3917             :            * the primes generating the class group (and only those). */
    3918        3196 :           cache.missing = n;
    3919       14892 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 0;
    3920             :         }
    3921      114837 :         j = done_small % (F.KC+1);
    3922      114837 :         if (j == 0) p0 = NULL;
    3923             :         else
    3924             :         {
    3925       49946 :           p0 = gel(F.LP, j);
    3926       49946 :           if (!A)
    3927             :           { /* Prevent considering both P_iP_j and P_jP_i in small_norm */
    3928             :             /* Not all elements end up in F.L_jid (eliminated by hnfspec/add or
    3929             :              * by trim_list): keep track of which ideals are being considered
    3930             :              * at each run. */
    3931       18020 :             long mj = small_multiplier[j];
    3932      254575 :             for (i = k = 1; i < lg(F.L_jid); i++)
    3933      236555 :               if (F.L_jid[i] > mj)
    3934             :               {
    3935      228871 :                 small_multiplier[F.L_jid[i]] = j;
    3936      228871 :                 F.L_jid[k++] = F.L_jid[i];
    3937             :               }
    3938       18020 :             setlg(F.L_jid, k);
    3939             :           }
    3940             :         }
    3941      114837 :         if (lg(F.L_jid) > 1)
    3942      114669 :           small_norm(&cache, &F, nf, Nrelid, M_sn, fact, p0);
    3943      114837 :         F.L_jid = F.perm; set_avma(av3);
    3944      114837 :         if (!A && cache.last != last) small_fail = 0; else small_fail++;
    3945      114837 :         if (LIE)
    3946             :         { /* restore add_rel subsystem: undo above lie */
    3947        3196 :           long n = lg(W) - 1;
    3948       14892 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 1;
    3949        3196 :           cache.missing = 0;
    3950             :         }
    3951      114837 :         cache.end = cache.last;
    3952      114837 :         done_small++;
    3953      114837 :         need = F.sfb_chg = 0;
    3954             :       }
    3955      128711 :       if (need > 0)
    3956             :       { /* Random relations */
    3957       13708 :         if (++nreldep > F.MAXDEPSIZESFB) {
    3958         157 :           if (++sfb_trials > SFB_MAX && LIMC < LIMCMAX/6) goto START;
    3959         128 :           F.sfb_chg = sfb_INCREASE;
    3960         128 :           nreldep = 0;
    3961             :         }
    3962       13551 :         else if (!(nreldep % F.MAXDEPSFB))
    3963        2272 :           F.sfb_chg = sfb_CHANGE;
    3964       13679 :         if (F.sfb_chg && !subFB_change(&F)) goto START;
    3965       13665 :         rnd_rel(&cache, &F, nf, fact);
    3966       13665 :         F.L_jid = F.perm;
    3967             :       }
    3968      128668 :       if (DEBUGLEVEL) timer_start(&T);
    3969      128668 :       if (precpb)
    3970             :       {
    3971             :         REL_t *rel;
    3972         148 :         if (DEBUGLEVEL)
    3973             :         {
    3974           0 :           char str[64]; sprintf(str,"Buchall_param (%s)",precpb);
    3975           0 :           pari_warn(warnprec,str,PREC);
    3976             :         }
    3977         148 :         nf = _nfnewprec(nf, PREC, &nfisclone);
    3978         148 :         precdouble++; precpb = NULL;
    3979             : 
    3980         148 :         if (flag)
    3981             :         { /* recompute embs only, no need to redo HNF */
    3982         105 :           long j, le = lg(embs), lC = lg(C);
    3983         105 :           GEN E, M = nf_get_M(nf);
    3984         105 :           set_avma(av4);
    3985       32540 :           for (rel = cache.base+1, i = 1; i < le; i++,rel++)
    3986       32435 :             gel(embs,i) = rel_embed(rel, &F, embs, i, M, RU, R1, PREC);
    3987         105 :           E = RgM_ZM_mul(embs, rowslice(C, RU+1, nbrows(C)));
    3988       32540 :           for (j = 1; j < lC; j++)
    3989      148511 :             for (i = 1; i <= RU; i++) gcoeff(C,i,j) = gcoeff(E,i,j);
    3990         105 :           av4 = avma;
    3991             :         }
    3992             :         else
    3993             :         { /* recompute embs + HNF */
    3994       10532 :           for(i = 1; i < lg(PERM); i++) F.perm[i] = PERM[i];
    3995          43 :           cache.chk = cache.base;
    3996          43 :           W = NULL;
    3997             :         }
    3998         148 :         if (DEBUGLEVEL) timer_printf(&T, "increasing accuracy");
    3999             :       }
    4000      128668 :       set_avma(av4);
    4001      128668 :       if (cache.chk != cache.last)
    4002             :       { /* Reduce relation matrices */
    4003      114042 :         long l = cache.last - cache.chk + 1, j;
    4004      114042 :         GEN mat = cgetg(l, t_MAT);
    4005             :         REL_t *rel;
    4006             : 
    4007     1089744 :         for (j=1,rel = cache.chk + 1; j < l; rel++,j++) gel(mat,j) = rel->R;
    4008      114042 :         if (!flag || W)
    4009             :         {
    4010       52000 :           embs = get_embs(&F, &cache, nf, embs, PREC);
    4011       52000 :           if (DEBUGLEVEL && timer_get(&T) > 1)
    4012           0 :             timer_printf(&T, "floating point embeddings");
    4013             :         }
    4014      114042 :         if (!W)
    4015             :         { /* never reduced before */
    4016       63646 :           C = flag? matbotid(&cache): embs;
    4017       63646 :           W = hnfspec_i(mat, F.perm, &dep, &B, &C, F.subFB ? lg(F.subFB)-1:0);
    4018       63646 :           if (DEBUGLEVEL)
    4019           0 :             timer_printf(&T, "hnfspec [%ld x %ld]", lg(F.perm)-1, l-1);
    4020       63646 :           if (flag)
    4021             :           {
    4022       62042 :             PREC += nbits2extraprec(gexpo(C));
    4023       62042 :             if (nf_get_prec(nf) < PREC) nf = _nfnewprec(nf, PREC, &nfisclone);
    4024       62042 :             embs = get_embs(&F, &cache, nf, embs, PREC);
    4025       62040 :             C = vconcat(RgM_ZM_mul(embs, C), C);
    4026             :           }
    4027       63646 :           if (DEBUGLEVEL)
    4028           0 :             timer_printf(&T, "hnfspec floating points");
    4029             :         }
    4030             :         else
    4031             :         {
    4032       50396 :           long k = lg(embs);
    4033       50396 :           GEN E = vecslice(embs, k-l+1,k-1);
    4034       50396 :           if (flag)
    4035             :           {
    4036       46187 :             E = matbotidembs(&cache, E);
    4037       46187 :             matenlarge(C, cache.last - cache.chk);
    4038             :           }
    4039       50396 :           W = hnfadd_i(W, F.perm, &dep, &B, &C, mat, E);
    4040       50396 :           if (DEBUGLEVEL)
    4041           0 :             timer_printf(&T, "hnfadd (%ld + %ld)", l-1, lg(dep)-1);
    4042             :         }
    4043      114042 :         gerepileall(av2, 5, &W,&C,&B,&dep,&embs);
    4044      114042 :         cache.chk = cache.last;
    4045             :       }
    4046       14626 :       else if (!W)
    4047             :       {
    4048           0 :         need = old_need;
    4049           0 :         F.L_jid = vecslice(F.perm, 1, need);
    4050           0 :         continue;
    4051             :       }
    4052      128668 :       need = F.KC - (lg(W)-1) - (lg(B)-1);
    4053      128668 :       if (!need && cache.missing)
    4054             :       { /* The test above will never be true except if 27449|class number.
    4055             :          * Ensure that if we have maximal rank for the ideal lattice, then
    4056             :          * cache.missing == 0. */
    4057          14 :         for (i = 1; cache.missing; i++)
    4058           7 :           if (!mael(cache.basis, i, i))
    4059             :           {
    4060             :             long j;
    4061           7 :             cache.missing--; mael(cache.basis, i, i) = 1;
    4062         427 :             for (j = i+1; j <= F.KC; j++) mael(cache.basis, j, i) = 0;
    4063             :           }
    4064             :       }
    4065      128668 :       zc = (lg(C)-1) - (lg(B)-1) - (lg(W)-1);
    4066      128668 :       if (RU-1-zc > 0) need = minss(need + RU-1-zc, F.KC); /* for units */
    4067      128668 :       if (need)
    4068             :       { /* dependent rows */
    4069       18434 :         F.L_jid = vecslice(F.perm, 1, need);
    4070       18434 :         vecsmall_sort(F.L_jid);
    4071       18434 :         if (need != old_need) { nreldep = 0; old_need = need; }
    4072             :       }
    4073             :       else
    4074             :       { /* If the relation lattice is too small, check will be > 1 and we will
    4075             :          * do a new run of small_norm/rnd_rel asking for 1 relation. This often
    4076             :          * gives a relation involving L_jid[1]. We rotate the first element of
    4077             :          * L_jid in order to increase the probability of finding relations that
    4078             :          * increases the lattice. */
    4079      110234 :         long j, n = lg(W) - 1;
    4080      110234 :         if (n > 1 && squash_index % n)
    4081             :         {
    4082        7601 :           F.L_jid = leafcopy(F.perm);
    4083       41678 :           for (j = 1; j <= n; j++)
    4084       34077 :             F.L_jid[j] = F.perm[1 + (j + squash_index - 1) % n];
    4085             :         }
    4086             :         else
    4087      102633 :           F.L_jid = F.perm;
    4088      110234 :         squash_index++;
    4089             :       }
    4090             :     }
    4091      128668 :     while (need);
    4092             : 
    4093      110234 :     if (!A)
    4094             :     {
    4095       63609 :       small_fail = old_need = 0;
    4096       63609 :       fail_limit = maxss(F.KC / FAIL_DIVISOR, MINFAIL);
    4097             :     }
    4098      110234 :     A = vecslice(C, 1, zc); /* cols corresponding to units */
    4099      110234 :     if (flag) A = rowslice(A, 1, RU);
    4100      110234 :     Ar = real_i(A);
    4101      110234 :     R = compute_multiple_of_R(Ar, RU, N, &need, &bit, &lambda);
    4102      110233 :     if (need < old_need) small_fail = 0;
    4103             : #if 0 /* A good idea if we are indeed stuck but needs tuning */
    4104             :     /* we have computed way more relations than should be necessary */
    4105             :     if (TRIES < 3 && LIMC < LIMCMAX / 24 &&
    4106             :                      cache.last - cache.base > 10 * F.KC) goto START;
    4107             : #endif
    4108      110233 :     old_need = need;
    4109      110233 :     if (!lambda)
    4110          77 :     { precpb = "bestappr"; PREC = myprecdbl(PREC, flag? C: NULL); continue; }
    4111      110156 :     if (!R)
    4112             :     { /* not full rank for units */
    4113       37105 :       if (!need)
    4114           1 :       { precpb = "regulator"; PREC = myprecdbl(PREC, flag? C: NULL); }
    4115       37105 :       continue;
    4116             :     }
    4117       73051 :     h = ZM_det_triangular(W);
    4118       73051 :     if (DEBUGLEVEL) err_printf("\n#### Tentative class number: %Ps\n", h);
    4119       73051 :     i = compute_R(lambda, mulir(h,invhr), &L, &R);
    4120       73052 :     if (DEBUGLEVEL)
    4121             :     {
    4122           0 :       err_printf("\n");
    4123           0 :       timer_printf(&T, "computing regulator and check");
    4124             :     }
    4125       73052 :     switch(i)
    4126             :     {
    4127        9422 :       case fupb_RELAT:
    4128        9422 :         need = 1; /* not enough relations */
    4129        9422 :         continue;
    4130          63 :       case fupb_PRECI: /* prec problem unless we cheat on Bach constant */
    4131          63 :         if ((precdouble&7) == 7 && LIMC<=LIMCMAX/6) goto START;
    4132          63 :         precpb = "compute_R"; PREC = myprecdbl(PREC, flag? C: NULL);
    4133          63 :         continue;
    4134             :     }
    4135             :     /* DONE */
    4136             : 
    4137       63567 :     if (F.KCZ2 > F.KCZ)
    4138             :     {
    4139           7 :       if (F.sfb_chg && !subFB_change(&F)) goto START;
    4140           7 :       if (!be_honest(&F, nf, auts, fact)) goto START;
    4141           7 :       if (DEBUGLEVEL) timer_printf(&T, "to be honest");
    4142             :     }
    4143       63567 :     F.KCZ2 = 0; /* be honest only once */
    4144             : 
    4145             :     /* fundamental units */
    4146             :     {
    4147       63567 :       GEN AU, CU, U, v = extract_full_lattice(L); /* L may be large */
    4148       63567 :       CU = NULL;
    4149       63567 :       if (v) { A = vecpermute(A, v); L = vecpermute(L, v); }
    4150             :       /* arch. components of fund. units */
    4151       63567 :       U = ZM_lll(L, 0.99, LLL_IM);
    4152       63566 :       U = ZM_mul(U, lll(RgM_ZM_mul(real_i(A), U)));
    4153       63565 :       if (DEBUGLEVEL) timer_printf(&T, "units LLL");
    4154       63565 :       AU = RgM_ZM_mul(A, U);
    4155       63565 :       A = cleanarchunit(AU, N, NULL, PREC);
    4156       63567 :       if (!A || lg(A) < RU || expo(gsub(get_regulator(A), R)) > -1)
    4157             :       {
    4158           6 :         long add = nbits2extraprec( gexpo(AU) + 64 ) - gprecision(AU);
    4159           6 :         long t = maxss((PREC-2) * 0.15, add);
    4160           6 :         if (!A && DEBUGLEVEL) err_printf("### Incorrect units lognorm");
    4161           6 :         precpb = "cleanarch"; PREC += maxss(t, EXTRAPRECWORD); continue;
    4162             :       }
    4163       63561 :       if (flag)
    4164             :       {
    4165       62006 :         long l = lgcols(C) - RU;
    4166             :         REL_t *rel;
    4167       62006 :         SUnits = cgetg(l, t_COL);
    4168      964116 :         for (rel = cache.base+1, i = 1; i < l; i++,rel++)
    4169      902111 :           set_rel_alpha(rel, auts, SUnits, i);
    4170       62005 :         if (RU > 1)
    4171             :         {
    4172       47340 :           GEN c = v? vecpermute(C,v): vecslice(C,1,zc);
    4173       47340 :           CU = ZM_mul(rowslice(c, RU+1, nbrows(c)), U);
    4174             :         }
    4175             :       }
    4176       63559 :       if (DEBUGLEVEL) err_printf("\n#### Computing fundamental units\n");
    4177       63560 :       fu = getfu(nf, &A, CU? &U: NULL, PREC);
    4178       63561 :       CU = CU? ZM_mul(CU, U): cgetg(1, t_MAT);
    4179       63561 :       if (DEBUGLEVEL) timer_printf(&T, "getfu");
    4180       63561 :       Ce = vecslice(C, zc+1, lg(C)-1);
    4181       63561 :       if (flag) SUnits = mkvec4(SUnits, CU, rowslice(Ce, RU+1, nbrows(Ce)),
    4182             :                                 utoipos(LIMC));
    4183             :     }
    4184             :     /* class group generators */
    4185       63561 :     if (flag) Ce = rowslice(Ce, 1, RU);
    4186       63559 :     C0 = Ce; Ce = cleanarch(Ce, N, NULL, PREC);
    4187       63561 :     if (!Ce) {
    4188           1 :       long add = nbits2extraprec( gexpo(C0) + 64 ) - gprecision(C0);
    4189           1 :       precpb = "cleanarch"; PREC += maxss(add, 1);
    4190             :     }
    4191       63561 :     if (DEBUGLEVEL) timer_printf(&T, "cleanarch");
    4192      110234 :   } while (need || precpb);
    4193             : 
    4194       63560 :   Vbase = vecpermute(F.LP, F.perm);
    4195       63560 :   if (!fu) fu = cgetg(1, t_MAT);
    4196       63560 :   if (!SUnits) SUnits = gen_1;
    4197       63560 :   clg1 = class_group_gen(nf,W,Ce,Vbase,PREC, &clg2);
    4198       63560 :   res = mkvec5(clg1, R, SUnits, zu, fu);
    4199       63559 :   res = buchall_end(nf,res,clg2,W,B,A,Ce,Vbase);
    4200       63559 :   delete_FB(&F);
    4201       63560 :   res = gerepilecopy(av0, res);
    4202       63560 :   if (flag) obj_insert_shallow(res, MATAL, cgetg(1,t_VEC));
    4203       63560 :   if (nfisclone) gunclone(nf);
    4204       63560 :   delete_cache(&cache);
    4205       63560 :   free_GRHcheck(&GRHcheck);
    4206       63560 :   return res;
    4207             : }

Generated by: LCOV version 1.14