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 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.8.0 lcov report (development 19378-5335519) Lines: 2327 2491 93.4 %
Date: 2016-08-31 06:11:49 Functions: 140 148 94.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : #include "pari.h"
      14             : #include "paripriv.h"
      15             : /*******************************************************************/
      16             : /*                                                                 */
      17             : /*         CLASS GROUP AND REGULATOR (McCURLEY, BUCHMANN)          */
      18             : /*                    GENERAL NUMBER FIELDS                        */
      19             : /*                                                                 */
      20             : /*******************************************************************/
      21             : /* get_random_ideal */
      22             : static const long RANDOM_BITS = 4;
      23             : /* Buchall */
      24             : static const double BNF_C1 = 0.0, BNF_C2 = 0.0;
      25             : static const long RELSUP = 5;
      26             : static const long FAIL_DIVISOR = 32;
      27             : static const long MINFAIL = 10;
      28             : /* small_norm */
      29             : static const long BNF_RELPID = 4;
      30             : static const long BMULT = 8;
      31             : static const long maxtry_ELEMENT = 1000*1000;
      32             : static const long maxtry_DEP = 20;
      33             : static const long maxtry_FACT = 500;
      34             : /* rnd_rel */
      35             : static const long RND_REL_RELPID = 1;
      36             : static const long PREVENT_LLL_IN_RND_REL = 1;
      37             : /* random relations */
      38             : static const long MINSFB = 3;
      39             : static const long SFB_MAX = 3;
      40             : static const long DEPSIZESFBMULT = 16;
      41             : static const long DEPSFBDIV = 10;
      42             : /* add_rel_i */
      43             : static const ulong mod_p = 27449UL;
      44             : /* be_honest */
      45             : static const long maxtry_HONEST = 50;
      46             : 
      47             : typedef struct FACT {
      48             :     long pr, ex;
      49             : } FACT;
      50             : 
      51             : typedef struct subFB_t {
      52             :   GEN subFB;
      53             :   struct subFB_t *old;
      54             : } subFB_t;
      55             : 
      56             : /* a factor base contains only non-inert primes
      57             :  * KC = # of P in factor base (p <= n, NP <= n2)
      58             :  * KC2= # of P assumed to generate class group (NP <= n2)
      59             :  *
      60             :  * KCZ = # of rational primes under ideals counted by KC
      61             :  * KCZ2= same for KC2 */
      62             : 
      63             : typedef struct FB_t {
      64             :   GEN FB; /* FB[i] = i-th rational prime used in factor base */
      65             :   GEN LP; /* vector of all prime ideals in FB */
      66             :   GEN *LV; /* LV[p] = vector of P|p, NP <= n2
      67             :             * isclone() is set for LV[p] iff all P|p are in FB
      68             :             * LV[i], i not prime or i > n2, is undefined! */
      69             :   GEN iLP; /* iLP[p] = i such that LV[p] = [LP[i],...] */
      70             :   GEN id2; /* id2[i] = powers of ideal i */
      71             :   GEN L_jid; /* indexes of "useful" prime ideals for rnd_rel */
      72             :   long KC, KCZ, KCZ2;
      73             :   GEN subFB; /* LP o subFB =  part of FB used to build random relations */
      74             :   int sfb_chg; /* need to change subFB ? */
      75             :   int newpow; /* need to compute powFB */
      76             :   GEN perm; /* permutation of LP used to represent relations [updated by
      77             :                hnfspec/hnfadd: dense rows come first] */
      78             :   GEN vecG, G0;
      79             :   GEN idealperm; /* permutation of ideals under field automorphisms */
      80             :   GEN minidx; /* minidx[i] min ideal in orbit of LP[i] under field autom */
      81             :   subFB_t *allsubFB; /* all subFB's used */
      82             :   GEN embperm; /* permutations of the complex embeddings */
      83             :   GEN invs; /* inverse of automorphism */
      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 non-zero 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 junk[3]; /*make sure sizeof(struct) is a power of two.*/
      95             : } REL_t;
      96             : 
      97             : typedef struct RELCACHE_t {
      98             :   REL_t *chk; /* last checkpoint */
      99             :   REL_t *base; /* first rel found */
     100             :   REL_t *last; /* last rel found so far */
     101             :   REL_t *end; /* target for last relation. base <= last <= end */
     102             :   size_t len; /* number of rels pre-allocated in base */
     103             :   long relsup; /* how many linearly dependent relations we allow */
     104             :   GEN basis; /* mod p basis (generating family actually) */
     105             :   ulong missing; /* missing vectors in generating family above */
     106             : } RELCACHE_t;
     107             : 
     108             : typedef struct FP_t {
     109             :   double **q;
     110             :   GEN x;
     111             :   double *y;
     112             :   double *z;
     113             :   double *v;
     114             : } FP_t;
     115             : 
     116             : typedef struct RNDREL_t {
     117             :   GEN Nideal;
     118             :   long jid;
     119             :   GEN ex;
     120             :   GEN m1;
     121             : } RNDREL_t;
     122             : 
     123             : static void
     124           0 : wr_rel(GEN col)
     125             : {
     126           0 :   long i, l = lg(col);
     127           0 :   err_printf("\nrel = ");
     128           0 :   for (i=1; i<l; i++)
     129           0 :     if (col[i]) err_printf("%ld^%ld ",i,col[i]);
     130           0 :   err_printf("\n");
     131           0 : }
     132             : static void
     133           0 : dbg_newrel(RELCACHE_t *cache)
     134             : {
     135           0 :   if (DEBUGLEVEL > 1)
     136             :   {
     137           0 :     err_printf("\n++++ cglob = %ld", cache->last - cache->base);
     138           0 :     wr_rel(cache->last->R);
     139             :   }
     140             :   else
     141           0 :     err_printf("%ld ", cache->last - cache->base);
     142           0 : }
     143             : 
     144             : static void
     145           0 : dbg_cancelrel(long jid, long jdir, GEN col)
     146             : {
     147           0 :   err_printf("relation cancelled: ");
     148           0 :   if (DEBUGLEVEL>3) err_printf("(jid=%ld,jdir=%ld)",jid,jdir);
     149           0 :   wr_rel(col); err_flush();
     150           0 : }
     151             : 
     152             : 
     153             : static void
     154        1854 : delete_cache(RELCACHE_t *M)
     155             : {
     156             :   REL_t *rel;
     157       60873 :   for (rel = M->base+1; rel <= M->last; rel++)
     158             :   {
     159       59019 :     gunclone(rel->R);
     160       59019 :     if (!rel->m) continue;
     161       25020 :     gunclone(rel->m);
     162             :   }
     163        1854 :   pari_free((void*)M->base); M->base = NULL;
     164        1854 : }
     165             : 
     166             : static void
     167        1854 : unclone_subFB(FB_t *F)
     168             : {
     169             :   subFB_t *sub, *subold;
     170        1854 :   GEN id2 = F->id2;
     171             :   long i;
     172             : 
     173        3835 :   for (sub = F->allsubFB; sub; sub = subold)
     174             :   {
     175        1981 :     GEN subFB = sub->subFB;
     176        6663 :     for (i = 1; i < lg(subFB); i++)
     177             :     {
     178        4682 :       long id = subFB[i];
     179        4682 :       if (gel(id2, id) == gen_0) continue;
     180             : 
     181         920 :       gunclone(gel(id2, id));
     182         920 :       gel(id2, id) = gen_0;
     183             :     }
     184        1981 :     subold = sub->old;
     185        1981 :     pari_free(sub);
     186             :   }
     187        1854 : }
     188             : 
     189             : static void
     190        1854 : delete_FB(FB_t *F)
     191             : {
     192        1854 :   unclone_subFB(F);
     193        1854 :   gunclone(F->minidx);
     194        1854 :   gunclone(F->idealperm);
     195        1854 : }
     196             : 
     197             : static void
     198        1875 : reallocate(RELCACHE_t *M, long len)
     199             : {
     200        1875 :   REL_t *old = M->base;
     201        1875 :   M->len = len;
     202        1875 :   M->base = (REL_t*)pari_realloc((void*)old, (len+1) * sizeof(REL_t));
     203        1875 :   if (old)
     204             :   {
     205          21 :     size_t last = M->last - old, chk = M->chk - old, end = M->end - old;
     206          21 :     M->last = M->base + last;
     207          21 :     M->chk  = M->base + chk;
     208          21 :     M->end  = M->base + end;
     209             :   }
     210        1875 : }
     211             : 
     212             : #define pr_get_smallp(pr) gel(pr,1)[2]
     213             : 
     214             : /* don't take P|p all other Q|p are already there */
     215             : static int
     216        8206 : bad_subFB(FB_t *F, long t)
     217             : {
     218        8206 :   GEN LP, P = gel(F->LP,t);
     219        8206 :   long p = pr_get_smallp(P);
     220        8206 :   LP = F->LV[p];
     221        8206 :   return (isclone(LP) && t == F->iLP[p] + lg(LP)-1);
     222             : }
     223             : 
     224             : static void
     225        1981 : assign_subFB(FB_t *F, GEN yes, long iyes)
     226             : {
     227             :   subFB_t *sub;
     228             :   long i, lv;
     229             : 
     230             :   /* single malloc for struct + GEN */
     231        1981 :   lv = sizeof(subFB_t) + iyes*sizeof(long);
     232        1981 :   sub = (subFB_t *)pari_malloc(lv);
     233        1981 :   sub->subFB = (GEN)&sub[1];
     234        1981 :   sub->old = F->allsubFB;
     235        1981 :   F->allsubFB = sub;
     236        1981 :   for (i = 0; i < iyes; i++) sub->subFB[i] = yes[i];
     237        1981 :   F->subFB = sub->subFB;
     238        1981 :   F->newpow = 1;
     239        1981 : }
     240             : 
     241             : /*
     242             :  * Determine the permutation of the ideals made by each field automorphism.
     243             :  */
     244             : static void
     245        1854 : FB_aut_perm(FB_t *F, GEN nf, GEN auts, GEN cyclic)
     246             : {
     247        1854 :   pari_sp av0 = avma;
     248        1854 :   long i, KC = F->KC, nauts = lg(auts);
     249        1854 :   GEN minidx = zero_Flv(KC), perm = zero_Flm_copy(KC, nauts-1);
     250             : 
     251        1854 :   if (nauts == 1)
     252             :   {
     253         419 :     for (i = 1; i <= KC; i++) minidx[i] = i;
     254             :   }
     255             :   else
     256             :   {
     257             :     long j, m;
     258        3535 :     for (m = 1; m < lg(cyclic); m++)
     259             :     {
     260        2100 :       GEN thiscyc = gel(cyclic, m);
     261        2100 :       long k0 = thiscyc[1];
     262        2100 :       GEN aut = gel(auts, k0), permk0 = gel(perm, k0), ppermk;
     263        2100 :       i = 1;
     264       18802 :       while (i <= KC)
     265             :       {
     266       14602 :         pari_sp av2 = avma;
     267       14602 :         GEN seen = zero_Flv(KC), P = gel(F->LP, i);
     268       14602 :         long imin = i, p, f, l;
     269       14602 :         p = pr_get_p(P)[2];
     270       14602 :         f = pr_get_f(P);
     271             :         do
     272             :         {
     273       59451 :           if (++i > KC) break;
     274       57351 :           P = gel(F->LP, i);
     275             :         }
     276       57351 :         while (p == pr_get_p(P)[2] && f == pr_get_f(P));
     277       74053 :         for (j = imin; j < i; j++)
     278             :         {
     279       59451 :           GEN img = ZM_ZC_mul(aut, pr_get_gen(gel(F->LP, j)));
     280      257460 :           for (l = imin; l < i; l++)
     281      257460 :             if (!seen[l] && ZC_prdvd(nf, img, gel(F->LP, l)))
     282             :             {
     283       59451 :               seen[l] = 1; permk0[j] = l; break;
     284             :             }
     285             :         }
     286       14602 :         avma = av2;
     287             :       }
     288        2751 :       for (ppermk = permk0, i = 2; i < lg(thiscyc); i++)
     289             :       {
     290         651 :         GEN permk = gel(perm, thiscyc[i]);
     291         651 :         for (j = 1; j <= KC; j++) permk[j] = permk0[ppermk[j]];
     292         651 :         ppermk = permk;
     293             :       }
     294             :     }
     295       23730 :     for (j = 1; j <= KC; j++)
     296             :     {
     297       22295 :       if (minidx[j]) continue;
     298        8141 :       minidx[j] = j;
     299        8141 :       for (i = 1; i < nauts; i++) minidx[coeff(perm, j, i)] = j;
     300             :     }
     301             :   }
     302        1854 :   F->minidx = gclone(minidx);
     303        1854 :   F->idealperm = gclone(perm);
     304        1854 :   avma = av0;
     305        1854 : }
     306             : 
     307             : /* set subFB.
     308             :  * Fill F->perm (if != NULL): primes ideals sorted by increasing norm (except
     309             :  * the ones in subFB come first [dense rows for hnfspec]) */
     310             : static int
     311        1854 : subFBgen(FB_t *F, GEN nf, GEN auts, GEN cyclic, double PROD, long minsFB)
     312             : {
     313             :   GEN y, perm, yes, no;
     314        1854 :   long i, j, k, iyes, ino, lv = F->KC + 1;
     315             :   double prod;
     316             :   pari_sp av;
     317             : 
     318        1854 :   F->LP   = cgetg(lv, t_VEC);
     319        1854 :   F->L_jid = F->perm = cgetg(lv, t_VECSMALL);
     320        1854 :   av = avma;
     321        1854 :   y = cgetg(lv,t_COL); /* Norm P */
     322       15096 :   for (k=0, i=1; i <= F->KCZ; i++)
     323             :   {
     324       13242 :     GEN LP = F->LV[F->FB[i]];
     325       13242 :     long l = lg(LP);
     326       43640 :     for (j = 1; j < l; j++)
     327             :     {
     328       30398 :       GEN P = gel(LP,j);
     329       30398 :       k++;
     330       30398 :       gel(y,k) = pr_norm(P);
     331       30398 :       gel(F->LP,k) = P;
     332             :     }
     333             :   }
     334             :   /* perm sorts LP by increasing norm */
     335        1854 :   perm = indexsort(y);
     336        1854 :   no  = cgetg(lv, t_VECSMALL); ino  = 1;
     337        1854 :   yes = cgetg(lv, t_VECSMALL); iyes = 1;
     338        1854 :   prod = 1.0;
     339        9011 :   for (i = 1; i < lv; i++)
     340             :   {
     341        8206 :     long t = perm[i];
     342        8206 :     if (bad_subFB(F, t)) { no[ino++] = t; continue; }
     343             : 
     344        4273 :     yes[iyes++] = t;
     345        4273 :     prod *= (double)itos(gel(y,t));
     346        4273 :     if (iyes > minsFB && prod > PROD) break;
     347             :   }
     348        1854 :   setlg(yes, iyes);
     349        1854 :   for (j=1; j<iyes; j++)     F->perm[j] = yes[j];
     350        1854 :   for (i=1; i<ino; i++, j++) F->perm[j] =  no[i];
     351        1854 :   for (   ; j<lv; j++)       F->perm[j] =  perm[j];
     352        1854 :   F->allsubFB = NULL;
     353        1854 :   FB_aut_perm(F, nf, auts, cyclic);
     354        1854 :   if (iyes) assign_subFB(F, yes, iyes);
     355        1854 :   avma = av; return 1;
     356             : }
     357             : static int
     358         442 : subFB_change(FB_t *F)
     359             : {
     360         442 :   long i, iyes, minsFB, lv = F->KC + 1, l = lg(F->subFB)-1;
     361         442 :   pari_sp av = avma;
     362         442 :   GEN yes, L_jid = F->L_jid, present = zero_zv(lv-1);
     363             : 
     364         442 :   switch (F->sfb_chg)
     365             :   {
     366          21 :     case sfb_INCREASE: minsFB = l + 1; break;
     367         421 :     default: minsFB = l; break;
     368             :   }
     369             : 
     370         442 :   yes = cgetg(minsFB+1, t_VECSMALL); iyes = 1;
     371         442 :   if (L_jid)
     372             :   {
     373        1375 :     for (i = 1; i < lg(L_jid); i++)
     374             :     {
     375        1172 :       long l = L_jid[i];
     376        1172 :       yes[iyes++] = l;
     377        1172 :       present[l] = 1;
     378        1172 :       if (iyes > minsFB) break;
     379             :     }
     380             :   }
     381           0 :   else i = 1;
     382         442 :   if (iyes <= minsFB)
     383             :   {
     384         322 :     for ( ; i < lv; i++)
     385             :     {
     386         322 :       long l = F->perm[i];
     387         322 :       if (present[l]) continue;
     388         322 :       yes[iyes++] = l;
     389         322 :       if (iyes > minsFB) break;
     390             :     }
     391         203 :     if (i == lv) return 0;
     392             :   }
     393         442 :   if (zv_equal(F->subFB, yes))
     394             :   {
     395         315 :     if (DEBUGLEVEL) err_printf("\n*** NOT Changing sub factor base\n");
     396             :   }
     397             :   else
     398             :   {
     399         127 :     if (DEBUGLEVEL) err_printf("\n*** Changing sub factor base\n");
     400         127 :     assign_subFB(F, yes, iyes);
     401             :   }
     402         442 :   F->sfb_chg = 0;
     403         442 :   avma = av; return 1;
     404             : }
     405             : 
     406             : static GEN
     407       14810 : init_famat(GEN x) { return mkvec2(x, cgetg(1,t_MAT)); }
     408             : 
     409             : static GEN
     410        2828 : red(GEN nf, GEN I, GEN G0, GEN *pm)
     411             : {
     412             :   GEN m, y, norm, norm2;
     413        2828 :   norm = typ(I) == t_MAT ? ZM_det_triangular(I) : idealnorm(nf, I);
     414        2828 :   y = idealred0(nf, init_famat(I), G0);
     415        2828 :   m = gel(y,2);
     416        2828 :   y = gel(y,1); *pm = lg(m)==1? gen_1: Q_primpart(gmael(m, 1, 1));
     417        2828 :   norm2 = typ(y) == t_MAT ? ZM_det_triangular(y) : idealnorm(nf, y);
     418        2828 :   if (gcmp(norm, norm2) < 0 || is_pm1(gcoeff(y,1,1)))
     419             :   {
     420        1218 :     *pm = gen_1;
     421        1218 :     y = I;
     422             :   }
     423        2828 :   return idealtwoelt(nf,y);
     424             : }
     425             : 
     426             : /* make sure enough room to store n more relations */
     427             : static void
     428       17216 : pre_allocate(RELCACHE_t *cache, size_t n)
     429             : {
     430       17216 :   size_t len = (cache->last - cache->base) + n;
     431       17216 :   if (len >= cache->len) reallocate(cache, len << 1);
     432       17216 : }
     433             : 
     434             : void
     435       42656 : init_GRHcheck(GRHcheck_t *S, long N, long R1, double LOGD)
     436             : {
     437       42656 :   const double c1 = M_PI*M_PI/2;
     438       42656 :   const double c2 = 3.663862376709;
     439       42656 :   const double c3 = 3.801387092431; /* Euler + log(8*Pi)*/
     440       42656 :   S->clone = 0;
     441       42656 :   S->cN = R1*c2 + N*c1;
     442       42656 :   S->cD = LOGD - N*c3 - R1*M_PI/2;
     443       42656 :   S->maxprimes = 16000; /* sufficient for LIMC=176081*/
     444       42656 :   S->primes = (GRHprime_t*)pari_malloc(S->maxprimes*sizeof(*S->primes));
     445       42656 :   S->nprimes = 0;
     446       42656 :   S->limp = 0;
     447       42656 :   u_forprime_init(&S->P, 2, ULONG_MAX);
     448       42656 : }
     449             : 
     450             : void
     451       42656 : free_GRHcheck(GRHcheck_t *S)
     452             : {
     453       42656 :   if (S->clone)
     454             :   {
     455        1707 :     long i = S->nprimes;
     456             :     GRHprime_t *pr;
     457        1707 :     for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--) gunclone(pr->dec);
     458             :   }
     459       42656 :   pari_free(S->primes);
     460       42656 : }
     461             : 
     462             : int
     463      421475 : GRHok(GRHcheck_t *S, double L, double SA, double SB)
     464             : {
     465      421475 :   return (S->cD + (S->cN + 2*SB) / L - 2*SA < -1e-8);
     466             : }
     467             : 
     468             : /* Return factorization pattern of p: [f,n], where n[i] primes of
     469             :  * residue degree f[i] */
     470             : static GEN
     471      269566 : get_fs(GEN nf, GEN P, GEN index, ulong p)
     472             : {
     473             :   long j, k, f, n, l;
     474             :   GEN fs, ns;
     475             : 
     476      269566 :   if (umodiu(index, p))
     477             :   { /* easy case: p does not divide index */
     478      268246 :     GEN F = Flx_degfact(ZX_to_Flx(P,p), p);
     479      268246 :     fs = gel(F,1); l = lg(fs);
     480             :   }
     481             :   else
     482             :   {
     483        1320 :     GEN F = idealprimedec(nf, utoipos(p));
     484        1320 :     l = lg(F);
     485        1320 :     fs = cgetg(l, t_VECSMALL);
     486        1320 :     for (j = 1; j < l; j++) fs[j] = pr_get_f(gel(F,j));
     487             :   }
     488      269566 :   ns = cgetg(l, t_VECSMALL);
     489      269566 :   f = fs[1]; n = 1;
     490      597624 :   for (j = 2, k = 1; j < l; j++)
     491      328058 :     if (fs[j] == f)
     492      279831 :       n++;
     493             :     else
     494             :     {
     495       48227 :       ns[k] = n; fs[k] = f; k++;
     496       48227 :       f = fs[j]; n = 1;
     497             :     }
     498      269566 :   ns[k] = n; fs[k] = f; k++;
     499      269566 :   setlg(fs, k);
     500      269566 :   setlg(ns, k); return mkvec2(fs,ns);
     501             : }
     502             : 
     503             : /* cache data for all rational primes up to the LIM */
     504             : static void
     505       33608 : cache_prime_dec(GRHcheck_t *S, ulong LIM, GEN nf)
     506             : {
     507       33608 :   pari_sp av = avma;
     508             :   GRHprime_t *pr;
     509             :   GEN index, P;
     510             :   double nb;
     511             : 
     512       67216 :   if (S->limp >= LIM) return;
     513       10183 :   S->clone = 1;
     514       10183 :   nb = primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
     515       10183 :   GRH_ensure(S, nb+1); /* room for one extra prime */
     516       10183 :   P = nf_get_pol(nf);
     517       10183 :   index = nf_get_index(nf);
     518       10183 :   for (pr = S->primes + S->nprimes;;)
     519             :   {
     520      269566 :     ulong p = u_forprime_next(&(S->P));
     521      269566 :     pr->p = p;
     522      269566 :     pr->logp = log((double)p);
     523      269566 :     pr->dec = gclone(get_fs(nf, P, index, p));
     524      269566 :     S->nprimes++;
     525      269566 :     pr++;
     526             :     /* store up to nextprime(LIM) included */
     527      269566 :     if (p >= LIM) { S->limp = p; break; }
     528      259383 :   }
     529       10183 :   avma = av;
     530             : }
     531             : 
     532             : static double
     533       62144 : tailresback(long R1, long R2, double rK, long C, double C2, double C3, double r1K, double r2K, double logC, double logC2, double logC3)
     534             : {
     535       62144 :   const double  rQ = 1.83787706641;
     536       62144 :   const double r1Q = 1.98505372441;
     537       62144 :   const double r2Q = 1.07991541347;
     538      124288 :   return fabs((R1+R2-1)*(12*logC3+4*logC2-9*logC-6)/(2*C*logC3)
     539       62144 :          + (rK-rQ)*(6*logC2 + 5*logC + 2)/(C*logC3)
     540       62144 :          - R2*(6*logC2+11*logC+6)/(C2*logC2)
     541       62144 :          - 2*(r1K-r1Q)*(3*logC2 + 4*logC + 2)/(C2*logC3)
     542       62144 :          + (R1+R2-1)*(12*logC3+40*logC2+45*logC+18)/(6*C3*logC3)
     543       62144 :          + (r2K-r2Q)*(2*logC2 + 3*logC + 2)/(C3*logC3));
     544             : }
     545             : 
     546             : static double
     547       31072 : tailres(long R1, long R2, double al2K, double rKm, double rKM, double r1Km,
     548             :         double r1KM, double r2Km, double r2KM, double C, long i)
     549             : {
     550             :   /* C >= 3*2^i, lower bound for eint1(log(C)/2) */
     551             :   /* for(i=0,30,print(eint1(log(3*2^i)/2))) */
     552             :   static double tab[] = {
     553             :     0.50409264803,
     554             :     0.26205336997,
     555             :     0.14815491171,
     556             :     0.08770540561,
     557             :     0.05347651832,
     558             :     0.03328934284,
     559             :     0.02104510690,
     560             :     0.01346475900,
     561             :     0.00869778586,
     562             :     0.00566279855,
     563             :     0.00371111950,
     564             :     0.00244567837,
     565             :     0.00161948049,
     566             :     0.00107686891,
     567             :     0.00071868750,
     568             :     0.00048119961,
     569             :     0.00032312188,
     570             :     0.00021753772,
     571             :     0.00014679818,
     572             :     9.9272855581E-5,
     573             :     6.7263969995E-5,
     574             :     4.5656812967E-5,
     575             :     3.1041124593E-5,
     576             :     2.1136011590E-5,
     577             :     1.4411645381E-5,
     578             :     9.8393304088E-6,
     579             :     6.7257395409E-6,
     580             :     4.6025878272E-6,
     581             :     3.1529719271E-6,
     582             :     2.1620490021E-6,
     583             :     1.4839266071E-6
     584             :   };
     585       31072 :   const double logC = log(C), logC2 = logC*logC, logC3 = logC*logC2;
     586       31072 :   const double C2 = C*C, C3 = C*C2;
     587       31072 :   double E1 = i >30? 0: tab[i];
     588       62144 :   return al2K*((33*logC2+22*logC+8)/(8*logC3*sqrt(C))+15*E1/16)
     589       62144 :     + maxdd(tailresback(rKm,r1KM,r2Km, C,C2,C3,R1,R2,logC,logC2,logC3),
     590       31072 :             tailresback(rKM,r1Km,r2KM, C,C2,C3,R1,R2,logC,logC2,logC3))/2
     591       31072 :     + ((R1+R2-1)*4*C+R2)*(C2+6*logC)/(4*C2*C2*logC2);
     592             : }
     593             : 
     594             : static long
     595        1707 : primeneeded(long N, long R1, long R2, double LOGD)
     596             : {
     597        1707 :   const double lim = 0.25; /* should be log(2)/2 == 0.34657... */
     598        1707 :   const double al2K =  0.3526*LOGD - 0.8212*N + 4.5007;
     599        1707 :   const double  rKm = -1.0155*LOGD + 2.1042*N - 8.3419;
     600        1707 :   const double  rKM = -0.5   *LOGD + 1.2076*N + 1;
     601        1707 :   const double r1Km = -       LOGD + 1.4150*N;
     602        1707 :   const double r1KM = -       LOGD + 1.9851*N;
     603        1707 :   const double r2Km = -       LOGD + 0.9151*N;
     604        1707 :   const double r2KM = -       LOGD + 1.0800*N;
     605        1707 :   long Cmin = 3, Cmax = 3, i = 0;
     606       17460 :   while (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, Cmax, i) > lim)
     607             :   {
     608       14046 :     Cmin = Cmax;
     609       14046 :     Cmax *= 2;
     610       14046 :     i++;
     611             :   }
     612        1707 :   i--;
     613       18733 :   while (Cmax - Cmin > 1)
     614             :   {
     615       15319 :     long t = (Cmin + Cmax)/2;
     616       15319 :     if (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, t, i) > lim)
     617       10382 :       Cmin = t;
     618             :     else
     619        4937 :       Cmax = t;
     620             :   }
     621        1707 :   return Cmax;
     622             : }
     623             : 
     624             : /*
     625             :   for (; i > 0; pr++, i--)
     626             :   {
     627             :     GEN dec, a = NULL, b = NULL, fs, ns;
     628             :     long j, k, limp = (long)(llimc/pr->logp);
     629             :     ulong p = pr->p;
     630             :     dec = pr->dec;
     631             :     fs = gel(dec, 1); ns = gel(dec, 2);
     632             :     k = lg(fs);
     633             :     for (j = 1; j < k; j++)
     634             :     {
     635             :       long f, nb;
     636             :       GEN nor;
     637             :       f = fs[j]; if (f > limp) continue;
     638             :       nb = ns[j];
     639             :       nor = powuu(p, f);
     640             :       if (a)
     641             :       {
     642             :         a = mulii(a, powiu(nor, nb));
     643             :         b = mulii(b, powiu(subii(nor, gen_1), nb));
     644             :       }
     645             :       else
     646             :       {
     647             :         a = powuu(p, f*nb-1);
     648             :         b = diviuexact(powiu(subii(nor, gen_1), nb), p-1);
     649             :       }
     650             :     }
     651             :     if (a)
     652             :       invres = divri(mulir(b, invres), a);
     653             :     else
     654             :       invres = divru(mulur(p, invres), p-1);
     655             :   }
     656             : */
     657             : 
     658             : static GEN
     659        1707 : compute_invres(GRHcheck_t *S, long LIMC)
     660             : {
     661        1707 :   pari_sp av = avma;
     662        1707 :   double loginvres = 0.;
     663             :   GRHprime_t *pr;
     664             :   long i;
     665        1707 :   double logLIMC = log((double)LIMC);
     666        1707 :   double logLIMC2 = logLIMC*logLIMC, denc;
     667             :   double c0, c1, c2;
     668        1707 :   denc = 1/(pow((double)LIMC, 3.) * logLIMC * logLIMC2);
     669        1707 :   c2 = (    logLIMC2 + 3 * logLIMC / 2 + 1) * denc;
     670        1707 :   denc *= LIMC;
     671        1707 :   c1 = (3 * logLIMC2 + 4 * logLIMC     + 2) * denc;
     672        1707 :   denc *= LIMC;
     673        1707 :   c0 = (3 * logLIMC2 + 5 * logLIMC / 2 + 1) * denc;
     674      269755 :   for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--)
     675             :   {
     676             :     GEN dec, fs, ns;
     677             :     long addpsi;
     678             :     double addpsi1, addpsi2;
     679      269566 :     double logp = pr->logp, NPk;
     680      269566 :     long j, k, limp = logLIMC/logp;
     681      269566 :     ulong p = pr->p, p2 = p*p;
     682      269566 :     if (limp < 1) break;
     683      268048 :     dec = pr->dec;
     684      268048 :     fs = gel(dec, 1); ns = gel(dec, 2);
     685      268048 :     loginvres += 1./p;
     686             :     /*
     687             :      * note for optimization: limp == 1 nearly always and limp >= 3 for
     688             :      * only very few primes.
     689             :      */
     690      307358 :     for (k = 2, NPk = p; k <= limp; k++)
     691             :     {
     692       39310 :       NPk *= p;
     693       39310 :       loginvres += 1/(k * NPk);
     694             :     }
     695      268048 :     addpsi = limp;
     696      268048 :     addpsi1 = p *(pow((double)p , (double)limp)-1)/(p -1);
     697      268048 :     addpsi2 = p2*(pow((double)p2, (double)limp)-1)/(p2-1);
     698      268048 :     j = lg(fs);
     699      852105 :     while (--j > 0)
     700             :     {
     701             :       long f, nb, kmax;
     702             :       double NP, NP2, addinvres;
     703      316009 :       f = fs[j]; if (f > limp) continue;
     704      124755 :       nb = ns[j];
     705      124755 :       NP = pow((double)p, (double)f);
     706      124755 :       addinvres = 1/NP;
     707      124755 :       kmax = limp / f;
     708      148617 :       for (k = 2, NPk = NP; k <= kmax; k++)
     709             :       {
     710       23862 :         NPk *= NP;
     711       23862 :         addinvres += 1/(k*NPk);
     712             :       }
     713      124755 :       NP2 = NP*NP;
     714      124755 :       loginvres -= nb * addinvres;
     715      124755 :       addpsi -= nb * f * kmax;
     716      124755 :       addpsi1 -= nb*(f*NP *(pow(NP ,(double)kmax)-1)/(NP -1));
     717      124755 :       addpsi2 -= nb*(f*NP2*(pow(NP2,(double)kmax)-1)/(NP2-1));
     718             :     }
     719      268048 :     loginvres -= (addpsi*c0 - addpsi1*c1 + addpsi2*c2)*logp;
     720             :   }
     721        1707 :   return gerepileuptoleaf(av, mpexp(dbltor(loginvres)));
     722             : }
     723             : 
     724             : static long
     725        3414 : nthideal(GRHcheck_t *S, GEN nf, long n)
     726             : {
     727        3414 :   pari_sp av = avma;
     728        3414 :   GEN P = nf_get_pol(nf);
     729        3414 :   ulong p = 0, *vecN = (ulong*)const_vecsmall(n, LONG_MAX);
     730        3414 :   long i, res, N = poldegree(P, -1);
     731       14369 :   for (i = 0; ; i++)
     732             :   {
     733             :     GRHprime_t *pr;
     734             :     GEN fs;
     735       14369 :     cache_prime_dec(S, p+1, nf);
     736       14369 :     pr = S->primes + i;
     737       14369 :     fs = gel(pr->dec, 1);
     738       14369 :     p = pr->p;
     739       14369 :     if (fs[1] != N)
     740             :     {
     741       11097 :       GEN ns = gel(pr->dec, 2);
     742       11097 :       long k, l, j = lg(fs);
     743       35220 :       while (--j > 0)
     744             :       {
     745       13026 :         ulong NP = upowuu(p, fs[j]);
     746             :         long nf;
     747       13026 :         if (!NP) continue;
     748       13026 :         for (k = 1; k <= n; k++) if (vecN[k] > NP) break;
     749       13026 :         if (k > n) continue;
     750             :         /* vecN[k] <= NP */
     751        6882 :         nf = ns[j]; /*#{primes of norme NP} = nf, insert them here*/
     752        6882 :         for (l = k+nf; l <= n; l++) vecN[l] = vecN[l-nf];
     753        6882 :         for (l = 0; l < nf && k+l <= n; l++) vecN[k+l] = NP;
     754        6882 :         while (l <= k) vecN[l++] = NP;
     755             :       }
     756             :     }
     757       14369 :     if (p > vecN[n]) break;
     758       10955 :   }
     759        3414 :   res = vecN[n]; avma = av; return res;
     760             : }
     761             : 
     762             : 
     763             : /* Compute FB, LV, iLP + KC*. Reset perm
     764             :  * C2: bound for norm of tested prime ideals (includes be_honest())
     765             :  * C1: bound for p, such that P|p (NP <= C2) used to build relations
     766             : 
     767             :  * Return prod_{p<=C2} (1-1/p) / prod_{Norm(P)<=C2} (1-1/Norm(P)),
     768             :  * close to residue of zeta_K at 1 = 2^r1 (2pi)^r2 h R / (w D) */
     769             : static void
     770        1854 : FBgen(FB_t *F, GEN nf, long N, ulong C1, ulong C2, GRHcheck_t *S)
     771             : {
     772             :   GRHprime_t *pr;
     773             :   long i, ip;
     774             :   GEN prim;
     775        1854 :   const double L = log((double)C2 + 0.5);
     776             : 
     777        1854 :   cache_prime_dec(S, C2, nf);
     778        1854 :   pr = S->primes;
     779        1854 :   F->sfb_chg = 0;
     780        1854 :   F->FB  = cgetg(C2+1, t_VECSMALL);
     781        1854 :   F->iLP = cgetg(C2+1, t_VECSMALL);
     782        1854 :   F->LV = (GEN*)const_vec(C2, NULL);
     783             : 
     784        1854 :   prim = icopy(gen_1);
     785        1854 :   i = ip = 0;
     786        1854 :   F->KC = F->KCZ = 0;
     787       31319 :   for (;; pr++) /* p <= C2 */
     788             :   {
     789       33173 :     ulong p = pr->p;
     790             :     long k, l, m;
     791             :     GEN LP, nb, f;
     792             : 
     793       33173 :     if (!F->KC && p > C1) { F->KCZ = i; F->KC = ip; }
     794       33173 :     if (p > C2) break;
     795             : 
     796       32131 :     if (DEBUGLEVEL>1) { err_printf(" %ld",p); err_flush(); }
     797             : 
     798       32131 :     f = gel(pr->dec, 1); nb = gel(pr->dec, 2);
     799       32131 :     if (f[1] == N)
     800             :     {
     801        7024 :       if (p == C2) break;
     802        6863 :       continue; /* p inert */
     803             :     }/* compute l such that p^f <= C2  <=> f <= l */
     804       25107 :     l = (long)(L/pr->logp);
     805       25107 :     for (k=0, m=1; m < lg(f) && f[m]<=l; m++) k += nb[m];
     806       25107 :     if (!k) /* p too inert to appear in FB */
     807             :     {
     808       11858 :       if (p == C2) break;
     809       11823 :       continue;
     810             :     }
     811       13249 :     prim[2] = p; LP = idealprimedec_limit_f(nf,prim, l);
     812             :     /* keep non-inert ideals with Norm <= C2 */
     813       13249 :     if (m == lg(f)) setisclone(LP); /* flag it: all prime divisors in FB */
     814       13249 :     F->FB[++i]= p;
     815       13249 :     F->LV[p]  = LP;
     816       13249 :     F->iLP[p] = ip; ip += k;
     817       13249 :     if (p == C2) break;
     818       31319 :   }
     819        1854 :   if (!F->KC) { F->KCZ = i; F->KC = ip; }
     820             :   /* Note F->KC > 0 otherwise GRHchk is false */
     821        1854 :   setlg(F->FB, F->KCZ+1); F->KCZ2 = i;
     822        1854 :   if (DEBUGLEVEL>1)
     823             :   {
     824           0 :     err_printf("\n");
     825           0 :     if (DEBUGLEVEL>6)
     826             :     {
     827           0 :       err_printf("########## FACTORBASE ##########\n\n");
     828           0 :       err_printf("KC2=%ld, KC=%ld, KCZ=%ld, KCZ2=%ld\n",
     829             :                   ip, F->KC, F->KCZ, F->KCZ2);
     830           0 :       for (i=1; i<=F->KCZ; i++) err_printf("++ LV[%ld] = %Ps",i,F->LV[F->FB[i]]);
     831             :     }
     832             :   }
     833        1854 :   F->perm = NULL; F->L_jid = NULL;
     834        1854 : }
     835             : 
     836             : static int
     837       15678 : GRHchk(GEN nf, GRHcheck_t *S, ulong LIMC)
     838             : {
     839       15678 :   double logC = log((double)LIMC), SA = 0, SB = 0;
     840       15678 :   GRHprime_t *pr = S->primes;
     841             : 
     842       15678 :   cache_prime_dec(S, LIMC, nf);
     843      313294 :   for (pr = S->primes;; pr++)
     844             :   {
     845      313294 :     ulong p = pr->p;
     846             :     GEN dec, fs, ns;
     847             :     double logCslogp;
     848             :     long j;
     849             : 
     850      313294 :     if (p > LIMC) break;
     851      300387 :     dec = pr->dec; fs = gel(dec, 1); ns = gel(dec,2);
     852      300387 :     logCslogp = logC/pr->logp;
     853      428270 :     for (j = 1; j < lg(fs); j++)
     854             :     {
     855      347597 :       long f = fs[j], M, nb;
     856             :       double logNP, q, A, B;
     857      347597 :       if (f > logCslogp) break;
     858      127883 :       logNP = f * pr->logp;
     859      127883 :       q = 1/sqrt((double)upowuu(p, f));
     860      127883 :       A = logNP * q; B = logNP * A; M = (long)(logCslogp/f);
     861      127883 :       if (M > 1)
     862             :       {
     863       19388 :         double inv1_q = 1 / (1-q);
     864       19388 :         A *= (1 - pow(q, (double)M)) * inv1_q;
     865       19388 :         B *= (1 - pow(q, (double)M)*(M+1 - M*q)) * inv1_q * inv1_q;
     866             :       }
     867      127883 :       nb = ns[j];
     868      127883 :       SA += nb * A;
     869      127883 :       SB += nb * B;
     870             :     }
     871      300387 :     if (p == LIMC) break;
     872      297616 :   }
     873       15678 :   return GRHok(S, logC, SA, SB);
     874             : }
     875             : 
     876             : /*  SMOOTH IDEALS */
     877             : static void
     878     1889048 : store(long i, long e, FACT *fact)
     879             : {
     880     1889048 :   ++fact[0].pr;
     881     1889048 :   fact[fact[0].pr].pr = i; /* index */
     882     1889048 :   fact[fact[0].pr].ex = e; /* exponent */
     883     1889048 : }
     884             : 
     885             : /* divide out x by all P|p, where x as in can_factor().  k = v_p(Nx) */
     886             : static int
     887      889165 : divide_p_elt(GEN LP, long ip, long k, GEN nf, GEN m, FACT *fact)
     888             : {
     889      889165 :   long j, l = lg(LP);
     890     3663077 :   for (j=1; j<l; j++)
     891             :   {
     892     3662653 :     GEN P = gel(LP,j);
     893     3662653 :     long v = ZC_nfval(nf, m, P);
     894     3662653 :     if (!v) continue;
     895     1614532 :     store(ip + j, v, fact); /* v = v_P(m) > 0 */
     896     1614532 :     k -= v * pr_get_f(P);
     897     1614532 :     if (!k) return 1;
     898             :   }
     899         424 :   return 0;
     900             : }
     901             : static int
     902       49332 : divide_p_id(GEN LP, long ip, long k, GEN nf, GEN I, FACT *fact)
     903             : {
     904       49332 :   long j, l = lg(LP);
     905       71541 :   for (j=1; j<l; j++)
     906             :   {
     907       64926 :     GEN P = gel(LP,j);
     908       64926 :     long v = idealval(nf,I, P);
     909       64926 :     if (!v) continue;
     910       43348 :     store(ip + j, v, fact); /* v = v_P(I) > 0 */
     911       43348 :     k -= v * pr_get_f(P);
     912       43348 :     if (!k) return 1;
     913             :   }
     914        6615 :   return 0;
     915             : }
     916             : static int
     917      213822 : divide_p_quo(GEN LP, long ip, long k, GEN nf, GEN I, GEN m, FACT *fact)
     918             : {
     919      213822 :   long j, l = lg(LP);
     920      292791 :   for (j=1; j<l; j++)
     921             :   {
     922      292607 :     GEN P = gel(LP,j);
     923      292607 :     long v = ZC_nfval(nf, m, P);
     924      292607 :     if (!v) continue;
     925      219632 :     v -= idealval(nf,I, P);
     926      219632 :     if (!v) continue;
     927      218617 :     store(ip + j, v, fact); /* v = v_P(m / I) > 0 */
     928      218617 :     k -= v * pr_get_f(P);
     929      218617 :     if (!k) return 1;
     930             :   }
     931         184 :   return 0;
     932             : }
     933             : 
     934             : /* |*N| != 0 is the norm of a primitive ideal, in particular not divisible by
     935             :  * any inert prime. Is |*N| a smooth rational integer wrt F ? (put the
     936             :  * exponents in *ex) */
     937             : static int
     938     1238125 : smooth_norm(FB_t *F, GEN *N, GEN *ex)
     939             : {
     940     1238125 :   GEN FB = F->FB;
     941     1238125 :   const long KCZ = F->KCZ;
     942     1238125 :   const ulong limp = uel(FB,KCZ); /* last p in FB */
     943             :   long i;
     944             : 
     945     1238125 :   *ex = new_chunk(KCZ+1);
     946    66131171 :   for (i=1; ; i++)
     947             :   {
     948             :     int stop;
     949    66131171 :     ulong p = uel(FB,i);
     950    66131171 :     long v = Z_lvalrem_stop(N, p, &stop);
     951    66131171 :     (*ex)[i] = v;
     952    66131171 :     if (v)
     953             :     {
     954     2166298 :       GEN LP = F->LV[p];
     955     2166298 :       if(!LP) pari_err_BUG("can_factor");
     956     2645698 :       if (lg(LP) == 1) return 0;
     957     2925023 :       if (stop) break;
     958             :     }
     959    65372446 :     if (i == KCZ) return 0;
     960    64893046 :   }
     961      758725 :   (*ex)[0] = i;
     962      758725 :   return (abscmpiu(*N,limp) <= 0);
     963             : }
     964             : 
     965             : static int
     966     1152319 : divide_p(FB_t *F, long p, long k, GEN nf, GEN I, GEN m, FACT *fact)
     967             : {
     968     1152319 :   GEN LP = F->LV[p];
     969     1152319 :   long ip = F->iLP[p];
     970     1152319 :   if (!m) return divide_p_id (LP,ip,k,nf,I,fact);
     971     1102987 :   if (!I) return divide_p_elt(LP,ip,k,nf,m,fact);
     972      213822 :   return divide_p_quo(LP,ip,k,nf,I,m,fact);
     973             : }
     974             : 
     975             : /* Let x = m if I == NULL,
     976             :  *         I if m == NULL,
     977             :  *         m/I otherwise.
     978             :  * Can we factor the integral primitive ideal x ? |N| = Norm x > 0 */
     979             : static long
     980     1333460 : can_factor(FB_t *F, GEN nf, GEN I, GEN m, GEN N, FACT *fact)
     981             : {
     982             :   GEN ex;
     983     1333460 :   long i, res = 0;
     984     1333460 :   fact[0].pr = 0;
     985     1333460 :   if (is_pm1(N)) return 1;
     986     1238125 :   if (!smooth_norm(F, &N, &ex)) goto END;
     987     6884125 :   for (i=1; i<=ex[0]; i++)
     988     6269378 :     if (ex[i] && !divide_p(F, F->FB[i], ex[i], nf, I, m, fact)) goto END;
     989      614747 :   res = is_pm1(N) || divide_p(F, itou(N), 1, nf, I, m, fact);
     990             : END:
     991     1238125 :   if (!res && DEBUGLEVEL > 1) { err_printf("."); err_flush(); }
     992     1238125 :   return res;
     993             : }
     994             : 
     995             : /* can we factor m/I ? [m in I from idealpseudomin_nonscalar], NI = norm I */
     996             : static long
     997      290747 : factorgen(FB_t *F, GEN nf, GEN I, GEN NI, GEN m, FACT *fact)
     998             : {
     999      290747 :   long e, r1 = nf_get_r1(nf);
    1000      290747 :   GEN M = nf_get_M(nf);
    1001      290747 :   GEN N = divri(embed_norm(RgM_RgC_mul(M,m), r1), NI); /* ~ N(m/I) */
    1002      290747 :   N = grndtoi(N, &e);
    1003      290747 :   if (e > -1)
    1004             :   {
    1005           0 :     if (DEBUGLEVEL > 1) { err_printf("+"); err_flush(); }
    1006           0 :     return 0;
    1007             :   }
    1008      290747 :   return can_factor(F, nf, I, m, N, fact);
    1009             : }
    1010             : 
    1011             : /*  FUNDAMENTAL UNITS */
    1012             : 
    1013             : /* a, m real. Return  (Re(x) + a) + I * (Im(x) % m) */
    1014             : static GEN
    1015      657729 : addRe_modIm(GEN x, GEN a, GEN m)
    1016             : {
    1017             :   GEN re, im, z;
    1018      657729 :   if (typ(x) == t_COMPLEX)
    1019             :   {
    1020      463736 :     im = modr_safe(gel(x,2), m);
    1021      463736 :     if (!im) return NULL;
    1022      463736 :     re = gadd(gel(x,1), a);
    1023      463736 :     z = gequal0(im)? re: mkcomplex(re, im);
    1024             :   }
    1025             :   else
    1026      193993 :     z = gadd(x, a);
    1027      657729 :   return z;
    1028             : }
    1029             : 
    1030             : /* clean archimedean components */
    1031             : static GEN
    1032      250476 : cleanarch(GEN x, long N, long prec)
    1033             : {
    1034      250476 :   long i, R1, RU, tx = typ(x);
    1035             :   GEN s, y, pi2;
    1036             : 
    1037      250476 :   if (tx == t_MAT)
    1038             :   {
    1039        3483 :     y = cgetg(lg(x), tx);
    1040       36152 :     for (i=1; i < lg(x); i++) {
    1041       32669 :       gel(y,i) = cleanarch(gel(x,i), N, prec);
    1042       32669 :       if (!gel(y,i)) return NULL;
    1043             :     }
    1044        3483 :     return y;
    1045             :   }
    1046      246993 :   if (!is_vec_t(tx)) pari_err_TYPE("cleanarch",x);
    1047      246993 :   RU = lg(x)-1; R1 = (RU<<1)-N;
    1048      246993 :   s = gdivgs(RgV_sum(real_i(x)), -N); /* -log |norm(x)| / N */
    1049      246993 :   y = cgetg(RU+1,tx);
    1050      246993 :   pi2 = Pi2n(1, prec);
    1051      747059 :   for (i=1; i<=R1; i++) {
    1052      500066 :     gel(y,i) = addRe_modIm(gel(x,i), s, pi2);
    1053      500066 :     if (!gel(y,i)) return NULL;
    1054             :   }
    1055      246993 :   if (i <= RU)
    1056             :   {
    1057       76762 :     GEN pi4 = Pi2n(2, prec), s2 = gmul2n(s, 1);
    1058      234425 :     for (   ; i<=RU; i++) {
    1059      157663 :       gel(y,i) = addRe_modIm(gel(x,i), s2, pi4);
    1060      157663 :       if (!gel(y,i)) return NULL;
    1061             :     }
    1062             :   }
    1063      246993 :   return y;
    1064             : }
    1065             : 
    1066             : static GEN
    1067          76 : not_given(long reason)
    1068             : {
    1069          76 :   if (DEBUGLEVEL)
    1070           0 :     switch(reason)
    1071             :     {
    1072             :       case fupb_LARGE:
    1073           0 :         pari_warn(warner,"fundamental units too large, not given");
    1074           0 :         break;
    1075             :       case fupb_PRECI:
    1076           0 :         pari_warn(warner,"insufficient precision for fundamental units, not given");
    1077           0 :         break;
    1078             :     }
    1079          76 :   return NULL;
    1080             : }
    1081             : 
    1082             : /* check whether exp(x) will 1) get too big (real(x) large), 2) require
    1083             :  * large accuracy for argument reduction (imag(x) large) */
    1084             : static int
    1085        1440 : exp_OK(GEN x, long *pte)
    1086             : {
    1087        1440 :   long i,I,j,J, e = - (long)HIGHEXPOBIT;
    1088        1440 :   RgM_dimensions(x, &I,&J);
    1089        4026 :   for (j=1; j<=J; j++)
    1090       12988 :     for (i=1; i<=I; i++)
    1091             :     {
    1092       10402 :       GEN c = gcoeff(x,i,j), re;
    1093       10402 :       if (typ(c)!=t_COMPLEX) re = c;
    1094             :       else
    1095             :       {
    1096        8753 :         GEN im = gel(c,2);
    1097        8753 :         e = maxss(e, expo(im) + 5 - bit_prec(im));
    1098        8753 :         re = gel(c,1);
    1099             :       }
    1100       10402 :       if (expo(re) > 20) { *pte = LONG_MAX; return 0; }
    1101             :     }
    1102        1440 :   *pte = -e; return (e < 0);
    1103             : }
    1104             : 
    1105             : static GEN
    1106        1364 : log_m1(long r1, long ru, long prec)
    1107             : {
    1108        1364 :   GEN v = cgetg(ru+1,t_COL);
    1109        1364 :   GEN a = r1? PiI2n(0,prec): NULL;
    1110        1364 :   GEN a2 = (r1 != ru)? PiI2n(1,prec): NULL;
    1111             :   long i;
    1112        1364 :   for (i=1; i<=r1; i++) gel(v,i) = a;
    1113        1364 :   for (   ; i<=ru; i++) gel(v,i) = a2;
    1114        1364 :   return v;
    1115             : }
    1116             : static GEN
    1117        1776 : getfu(GEN nf, GEN *ptA, long *pte, long prec)
    1118             : {
    1119        1776 :   GEN u, y, matep, A, vec, T = nf_get_pol(nf), M = nf_get_M(nf);
    1120        1776 :   long e, i, j, R1, RU, N = degpol(T);
    1121             : 
    1122        1776 :   if (DEBUGLEVEL) err_printf("\n#### Computing fundamental units\n");
    1123        1776 :   R1 = nf_get_r1(nf); RU = (N+R1)>>1;
    1124        1776 :   if (RU==1) { *pte=LONG_MAX; return cgetg(1,t_VEC); }
    1125             : 
    1126        1440 :   *pte = 0; A = *ptA;
    1127        1440 :   matep = cgetg(RU,t_MAT);
    1128        4026 :   for (j=1; j<RU; j++)
    1129             :   {
    1130        2586 :     GEN c = cgetg(RU+1,t_COL), Aj = gel(A,j);
    1131        2586 :     GEN s = gdivgs(RgV_sum(real_i(Aj)), -N); /* -log |norm(Aj)| / N */
    1132        2586 :     gel(matep,j) = c;
    1133        2586 :     for (i=1; i<=R1; i++) gel(c,i) = gadd(s, gel(Aj,i));
    1134        2586 :     for (   ; i<=RU; i++) gel(c,i) = gadd(s, gmul2n(gel(Aj,i),-1));
    1135             :   }
    1136        1440 :   u = lll(real_i(matep));
    1137        1440 :   if (lg(u) < RU) return not_given(fupb_PRECI);
    1138             : 
    1139        1440 :   y = RgM_mul(matep,u);
    1140        1440 :   if (!exp_OK(y, pte))
    1141           0 :     return not_given(*pte == LONG_MAX? fupb_LARGE: fupb_PRECI);
    1142        1440 :   if (prec <= 0) prec = gprecision(A);
    1143        1440 :   y = RgM_solve_realimag(M, gexp(y,prec));
    1144        1440 :   if (!y) return not_given(fupb_PRECI);
    1145        1440 :   y = grndtoi(y, &e);
    1146        1440 :   *pte = -e;
    1147        1440 :   if (e >= 0) return not_given(fupb_PRECI);
    1148        3792 :   for (j=1; j<RU; j++)
    1149        2428 :     if (!is_pm1(nfnorm(nf, gel(y,j)))) { *pte=0; return not_given(fupb_PRECI); }
    1150        1364 :   A = RgM_mul(A,u);
    1151        1364 :   settyp(y, t_VEC);
    1152             :   /* y[i] are unit generators. Normalize: smallest T2 norm + lead coeff > 0 */
    1153        1364 :   vec = log_m1(R1,RU,prec);
    1154        3764 :   for (j=1; j<RU; j++)
    1155             :   {
    1156        2400 :     GEN u = gel(y,j), v = zk_inv(nf, u);
    1157        2400 :     if (gcmp(RgC_fpnorml2(v,DEFAULTPREC),
    1158             :              RgC_fpnorml2(u,DEFAULTPREC)) < 0)
    1159             :     {
    1160         848 :       gel(A,j) = RgC_neg(gel(A,j));
    1161         848 :       u = v;
    1162             :     }
    1163        2400 :     u = coltoliftalg(nf,u);
    1164        2400 :     if (gsigne(leading_coeff(u)) < 0)
    1165             :     {
    1166        1154 :       gel(A,j) = RgC_add(gel(A,j), vec);
    1167        1154 :       u = RgX_neg(u);
    1168             :     }
    1169        2400 :     gel(y,j) = u;
    1170             :   }
    1171        1364 :   *ptA = A; return y;
    1172             : }
    1173             : 
    1174             : static GEN
    1175        1484 : makeunits(GEN BNF)
    1176             : {
    1177        1484 :   GEN bnf = checkbnf(BNF), fu = bnf_get_fu_nocheck(bnf), v;
    1178        1484 :   GEN nf = bnf_get_nf(bnf);
    1179             :   long i, l;
    1180        1484 :   if (typ(fu) == t_MAT)
    1181             :   {
    1182           0 :     pari_sp av = avma;
    1183           0 :     GEN A = bnf_get_logfu(bnf);
    1184           0 :     fu = getfu(nf, &A, &l, 0);
    1185           0 :     if (!fu)
    1186           0 :       pari_err_PREC("makeunits [cannot compute units, use bnfinit(,1)]");
    1187           0 :     fu = gerepilecopy(av, fu);
    1188             :   }
    1189        1484 :   l = lg(fu) + 1; v = cgetg(l, t_VEC);
    1190        1484 :   gel(v,1) = nf_to_scalar_or_basis(nf,bnf_get_tuU(bnf));
    1191        1484 :   for (i = 2; i < l; i++) gel(v,i) = algtobasis(nf, gel(fu,i-1));
    1192        1484 :   return v;
    1193             : }
    1194             : 
    1195             : /*******************************************************************/
    1196             : /*                                                                 */
    1197             : /*           PRINCIPAL IDEAL ALGORITHM (DISCRETE LOG)              */
    1198             : /*                                                                 */
    1199             : /*******************************************************************/
    1200             : 
    1201             : /* G: prime ideals, E: vector of non-negative exponents.
    1202             :  * C = possible extra prime (^1) or NULL
    1203             :  * Return Norm (product) */
    1204             : static GEN
    1205        1530 : get_norm_fact_primes(GEN G, GEN E, GEN C)
    1206             : {
    1207        1530 :   pari_sp av=avma;
    1208        1530 :   GEN N = gen_1, P, p;
    1209        1530 :   long i, c = lg(E);
    1210        4621 :   for (i=1; i<c; i++)
    1211             :   {
    1212        3091 :     GEN ex = gel(E,i);
    1213        3091 :     long s = signe(ex);
    1214        3091 :     if (!s) continue;
    1215             : 
    1216        2283 :     P = gel(G,i); p = pr_get_p(P);
    1217        2283 :     N = mulii(N, powii(p, mului(pr_get_f(P), ex)));
    1218             :   }
    1219        1530 :   if (C) N = mulii(N, pr_norm(C));
    1220        1530 :   return gerepileuptoint(av, N);
    1221             : }
    1222             : 
    1223             : /* gen: HNF ideals */
    1224             : static GEN
    1225      210258 : get_norm_fact(GEN gen, GEN ex, GEN *pd)
    1226             : {
    1227      210258 :   long i, c = lg(ex);
    1228             :   GEN d,N,I,e,n,ne,de;
    1229      210258 :   d = N = gen_1;
    1230      356638 :   for (i=1; i<c; i++)
    1231      146380 :     if (signe(gel(ex,i)))
    1232             :     {
    1233       94511 :       I = gel(gen,i); e = gel(ex,i); n = ZM_det_triangular(I);
    1234       94511 :       ne = powii(n,e);
    1235       94511 :       de = equalii(n, gcoeff(I,1,1))? ne: powii(gcoeff(I,1,1), e);
    1236       94511 :       N = mulii(N, ne);
    1237       94511 :       d = mulii(d, de);
    1238             :     }
    1239      210258 :   *pd = d; return N;
    1240             : }
    1241             : 
    1242             : static GEN
    1243      257130 : get_pr_lists(GEN FB, long N, int list_pr)
    1244             : {
    1245             :   GEN pr, L;
    1246      257130 :   long i, l = lg(FB), p, pmax;
    1247             : 
    1248      257130 :   pmax = 0;
    1249     2414549 :   for (i=1; i<l; i++)
    1250             :   {
    1251     2157419 :     pr = gel(FB,i); p = pr_get_smallp(pr);
    1252     2157419 :     if (p > pmax) pmax = p;
    1253             :   }
    1254      257130 :   L = const_vec(pmax, NULL);
    1255      257130 :   if (list_pr)
    1256             :   {
    1257          56 :     for (i=1; i<l; i++)
    1258             :     {
    1259          49 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1260          49 :       if (!L[p]) gel(L,p) = vectrunc_init(N+1);
    1261          49 :       vectrunc_append(gel(L,p), pr);
    1262             :     }
    1263          98 :     for (p=1; p<=pmax; p++)
    1264          91 :       if (L[p]) gen_sort_inplace(gel(L,p), (void*)&cmp_prime_over_p,
    1265             :                                  &cmp_nodata, NULL);
    1266             :   }
    1267             :   else
    1268             :   {
    1269     2414493 :     for (i=1; i<l; i++)
    1270             :     {
    1271     2157370 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1272     2157370 :       if (!L[p]) gel(L,p) = vecsmalltrunc_init(N+1);
    1273     2157370 :       vecsmalltrunc_append(gel(L,p), i);
    1274             :     }
    1275             :   }
    1276      257130 :   return L;
    1277             : }
    1278             : 
    1279             : /* recover FB, LV, iLP, KCZ from Vbase */
    1280             : static GEN
    1281      257123 : recover_partFB(FB_t *F, GEN Vbase, long N)
    1282             : {
    1283      257123 :   GEN FB, LV, iLP, L = get_pr_lists(Vbase, N, 0);
    1284      257123 :   long l = lg(L), p, ip, i;
    1285             : 
    1286      257123 :   i = ip = 0;
    1287      257123 :   FB = cgetg(l, t_VECSMALL);
    1288      257123 :   iLP= cgetg(l, t_VECSMALL);
    1289      257123 :   LV = cgetg(l, t_VEC);
    1290     5335331 :   for (p = 2; p < l; p++)
    1291             :   {
    1292     5078208 :     if (!L[p]) continue;
    1293     1288444 :     FB[++i] = p;
    1294     1288444 :     gel(LV,p) = vecpermute(Vbase, gel(L,p));
    1295     1288444 :     iLP[p]= ip; ip += lg(gel(L,p))-1;
    1296             :   }
    1297      257123 :   F->KCZ = i;
    1298      257123 :   F->KC = ip;
    1299      257123 :   F->FB = FB; setlg(FB, i+1);
    1300      257123 :   F->LV = (GEN*)LV;
    1301      257123 :   F->iLP= iLP; return L;
    1302             : }
    1303             : 
    1304             : /* add v^e to factorization */
    1305             : static void
    1306       13440 : add_to_fact(long v, long e, FACT *fact)
    1307             : {
    1308       13440 :   long i, l = fact[0].pr;
    1309       13440 :   for (i=1; i<=l && fact[i].pr < v; i++)/*empty*/;
    1310       13440 :   if (i <= l && fact[i].pr == v) fact[i].ex += e; else store(v, e, fact);
    1311       13440 : }
    1312             : 
    1313             : /* L (small) list of primes above the same p including pr. Return pr index */
    1314             : static int
    1315        9970 : pr_index(GEN L, GEN pr)
    1316             : {
    1317        9970 :   long j, l = lg(L);
    1318        9970 :   GEN al = pr_get_gen(pr);
    1319        9998 :   for (j=1; j<l; j++)
    1320        9998 :     if (ZV_equal(al, pr_get_gen(gel(L,j)))) return j;
    1321           0 :   pari_err_BUG("codeprime");
    1322           0 :   return 0; /* not reached */
    1323             : }
    1324             : 
    1325             : static long
    1326        9921 : Vbase_to_FB(FB_t *F, GEN pr)
    1327             : {
    1328        9921 :   long p = pr_get_smallp(pr);
    1329        9921 :   return F->iLP[p] + pr_index(F->LV[p], pr);
    1330             : }
    1331             : 
    1332             : /* return famat y (principal ideal) such that y / x is smooth [wrt Vbase] */
    1333             : static GEN
    1334      272628 : SPLIT(FB_t *F, GEN nf, GEN x, GEN Vbase, FACT *fact)
    1335             : {
    1336      272628 :   GEN vecG, z, ex, y, x0, Nx = ZM_det_triangular(x);
    1337             :   long nbtest_lim, nbtest, i, j, ru, lgsub;
    1338             :   pari_sp av;
    1339             : 
    1340      272628 :   if (nf_get_degree(nf) != lg(x)-1)
    1341           0 :     pari_err_TYPE("idealtyp [dimension != degree]", x);
    1342             : 
    1343             :   /* try without reduction if x is small */
    1344      545235 :   if (gexpo(gcoeff(x,1,1)) < 100 &&
    1345      335807 :       can_factor(F, nf, x, NULL, Nx, fact)) return NULL;
    1346             : 
    1347      209428 :   av = avma;
    1348      209428 :   y = idealpseudomin_nonscalar(x, nf_get_roundG(nf));
    1349      209428 :   if (factorgen(F, nf, x, Nx, y, fact)) return y;
    1350       17426 :   avma = av;
    1351             : 
    1352             :   /* reduce in various directions */
    1353       17426 :   ru = lg(nf_get_roots(nf));
    1354       17426 :   vecG = cgetg(ru, t_VEC);
    1355       31944 :   for (j=1; j<ru; j++)
    1356             :   {
    1357       26827 :     gel(vecG,j) = nf_get_Gtwist1(nf, j);
    1358       26827 :     av = avma;
    1359       26827 :     y = idealpseudomin_nonscalar(x, gel(vecG,j));
    1360       26827 :     if (factorgen(F, nf, x, Nx, y, fact)) return y;
    1361       14518 :     avma = av;
    1362             :   }
    1363             : 
    1364             :   /* tough case, multiply by random products */
    1365        5117 :   lgsub = 3;
    1366        5117 :   ex = cgetg(lgsub, t_VECSMALL);
    1367        5117 :   z  = init_famat(NULL);
    1368        5117 :   x0 = init_famat(x);
    1369        5117 :   nbtest = 1; nbtest_lim = 4;
    1370             :   for(;;)
    1371             :   {
    1372        9503 :     GEN I, NI, id = x0;
    1373        9503 :     av = avma;
    1374        9503 :     if (DEBUGLEVEL>2) err_printf("# ideals tried = %ld\n",nbtest);
    1375       28957 :     for (i=1; i<lgsub; i++)
    1376             :     {
    1377       19454 :       ex[i] = random_bits(RANDOM_BITS);
    1378       19454 :       if (ex[i])
    1379             :       { /* avoid prec pb: don't let id become too large as lgsub increases */
    1380       18287 :         if (id != x0) id = idealred(nf,id);
    1381       18287 :         z[1] = Vbase[i];
    1382       18287 :         id = extideal_HNF_mul(nf, id, idealpowred(nf,z,utoipos(ex[i])));
    1383             :       }
    1384             :     }
    1385        9503 :     if (id == x0) continue;
    1386             : 
    1387        9454 :     I = gel(id,1); NI = ZM_det_triangular(I);
    1388       20149 :     for (j=1; j<ru; j++)
    1389             :     {
    1390       15812 :       pari_sp av2 = avma;
    1391       15812 :       y = idealpseudomin_nonscalar(I, gel(vecG,j));
    1392       15812 :       if (factorgen(F, nf, I, NI, y, fact))
    1393             :       {
    1394       15624 :         for (i=1; i<lgsub; i++)
    1395       10507 :           if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1396        5117 :         return famat_mul(gel(id,2), y);
    1397             :       }
    1398       10695 :       avma = av2;
    1399             :     }
    1400        4337 :     avma = av;
    1401        4337 :     if (++nbtest > nbtest_lim)
    1402             :     {
    1403         273 :       nbtest = 0;
    1404         273 :       if (++lgsub < 7)
    1405             :       {
    1406         273 :         nbtest_lim <<= 1;
    1407         273 :         ex = cgetg(lgsub, t_VECSMALL);
    1408             :       }
    1409           0 :       else nbtest_lim = LONG_MAX; /* don't increase further */
    1410         273 :       if (DEBUGLEVEL>2) err_printf("SPLIT: increasing factor base [%ld]\n",lgsub);
    1411             :     }
    1412        4386 :   }
    1413             : }
    1414             : 
    1415             : /* return principal y such that y / x is smooth. Store factorization of latter*/
    1416             : static GEN
    1417      256171 : split_ideal(GEN nf, FB_t *F, GEN x, GEN Vbase, GEN L, FACT *fact)
    1418             : {
    1419      256171 :   GEN y = SPLIT(F, nf, x, Vbase, fact);
    1420      256171 :   long p,j, i, l = lg(F->FB);
    1421             : 
    1422      256171 :   p = j = 0; /* -Wall */
    1423      464645 :   for (i=1; i<=fact[0].pr; i++)
    1424             :   { /* decode index C = ip+j --> (p,j) */
    1425      208474 :     long q,k,t, C = fact[i].pr;
    1426      708225 :     for (t=1; t<l; t++)
    1427             :     {
    1428      694950 :       q = F->FB[t];
    1429      694950 :       k = C - F->iLP[q];
    1430      694950 :       if (k <= 0) break;
    1431      499751 :       p = q;
    1432      499751 :       j = k;
    1433             :     }
    1434      208474 :     fact[i].pr = gel(L, p)[j];
    1435             :   }
    1436      256171 :   return y;
    1437             : }
    1438             : 
    1439             : INLINE GEN
    1440      257149 : bnf_get_W(GEN bnf) { return gel(bnf,1); }
    1441             : INLINE GEN
    1442      257115 : bnf_get_B(GEN bnf) { return gel(bnf,2); }
    1443             : INLINE GEN
    1444      539270 : bnf_get_C(GEN bnf) { return gel(bnf,4); }
    1445             : INLINE GEN
    1446      257198 : bnf_get_vbase(GEN bnf) { return gel(bnf,5); }
    1447             : 
    1448             : /* all primes up to Minkowski bound factor on factorbase ? */
    1449             : void
    1450          49 : testprimes(GEN bnf, GEN BOUND)
    1451             : {
    1452          49 :   pari_sp av0 = avma, av;
    1453          49 :   ulong pmax, count = 0;
    1454          49 :   GEN Vbase, fb, p, nf = bnf_get_nf(bnf);
    1455             :   forprime_t S;
    1456             :   FACT *fact;
    1457             :   FB_t F;
    1458             : 
    1459          49 :   if (DEBUGLEVEL)
    1460             :   {
    1461           0 :     err_printf("PHASE 1 [CLASS GROUP]: are all primes good ?\n");
    1462           0 :     err_printf("  Testing primes <= %Ps\n", BOUND); err_flush();
    1463             :   }
    1464          49 :   if (is_bigint(BOUND))
    1465           0 :     pari_warn(warner,"Zimmert's bound is large (%Ps), certification will take a long time", BOUND);
    1466          49 :   if (!is_pm1(nf_get_index(nf)))
    1467             :   {
    1468          35 :     GEN D = nf_get_diff(nf), L;
    1469          35 :     if (DEBUGLEVEL>1) err_printf("**** Testing Different = %Ps\n",D);
    1470          35 :     L = bnfisprincipal0(bnf, D, nf_FORCE);
    1471          35 :     if (DEBUGLEVEL>1) err_printf("     is %Ps\n", L);
    1472             :   }
    1473             :   /* sort factorbase for tablesearch */
    1474          49 :   Vbase = bnf_get_vbase(bnf);
    1475          49 :   fb = gen_sort(Vbase, (void*)&cmp_prime_ideal, cmp_nodata);
    1476          49 :   pmax = itou( pr_get_p(gel(fb, lg(fb)-1)) ); /* largest p in factorbase */
    1477          49 :   (void)recover_partFB(&F, Vbase, nf_get_degree(nf));
    1478          49 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    1479          49 :   forprime_init(&S, gen_2, BOUND);
    1480          49 :   av = avma;
    1481       37240 :   while (( p = forprime_next(&S) ))
    1482             :   {
    1483             :     GEN vP;
    1484             :     long i, l;
    1485             : 
    1486       37142 :     if (DEBUGLEVEL == 1 && ++count > 1000)
    1487             :     {
    1488           0 :       err_printf("passing p = %Ps / %Ps\n", p, BOUND);
    1489           0 :       count = 0;
    1490             :     }
    1491             : 
    1492       37142 :     avma = av;
    1493       37142 :     vP = idealprimedec_limit_norm(bnf, p, BOUND);
    1494       37142 :     l = lg(vP); if (l == 1) continue;
    1495       21280 :     if (DEBUGLEVEL>1) err_printf("*** p = %Ps\n",p);
    1496             :     /* if vP[1] unramified, skip it */
    1497       21280 :     i = (pr_get_e(gel(vP,1))) == 1? 2: 1;
    1498       38171 :     for (; i<l; i++)
    1499             :     {
    1500       16891 :       GEN P = gel(vP,i);
    1501             :       long k;
    1502       16891 :       if (DEBUGLEVEL>1) err_printf("  Testing P = %Ps\n",P);
    1503       16891 :       if (abscmpiu(p, pmax) <= 0 && (k = tablesearch(fb, P, &cmp_prime_ideal)))
    1504         434 :       { if (DEBUGLEVEL>1) err_printf("    #%ld in factor base\n",k); }
    1505       16457 :       else if (DEBUGLEVEL>1)
    1506           0 :         err_printf("    is %Ps\n", isprincipal(bnf,P));
    1507             :       else /* faster: don't compute result */
    1508       16457 :         (void)SPLIT(&F, nf, idealhnf_two(nf,P), Vbase, fact);
    1509             :     }
    1510             :   }
    1511          49 :   avma = av0;
    1512          49 : }
    1513             : 
    1514             : /**** logarithmic embeddings ****/
    1515             : static GEN famat_to_arch(GEN nf, GEN fa, long prec);
    1516             : static GEN
    1517        1463 : triv_arch(GEN nf) { return zerovec(lg(nf_get_roots(nf))-1); }
    1518             : 
    1519             : /* Get archimedean components: [e_i Log( sigma_i(X) )], where X = primpart(x),
    1520             :  * and e_i = 1 (resp 2.) for i <= R1 (resp. > R1) */
    1521             : static GEN
    1522      178319 : get_arch(GEN nf, GEN x, long prec)
    1523             : {
    1524             :   long i, l, R1;
    1525             :   GEN v;
    1526      178319 :   if (typ(x) == t_MAT) return famat_to_arch(nf,x,prec);
    1527      178171 :   x = nf_to_scalar_or_basis(nf,x);
    1528      178171 :   if (typ(x) != t_COL) return triv_arch(nf);
    1529      177693 :   x = RgM_RgC_mul(nf_get_M(nf), Q_primpart(x));
    1530      177693 :   l = lg(x);
    1531      177693 :   for (i=1; i < l; i++) if (gequal0(gabs(gel(x,i),prec))) return NULL;
    1532      177669 :   v = cgetg(l,t_VEC); R1 = nf_get_r1(nf);
    1533      177669 :   for (i=1; i<=R1; i++) gel(v,i) = glog(gel(x,i),prec);
    1534      177669 :   for (   ; i < l; i++) gel(v,i) = gmul2n(glog(gel(x,i),prec),1);
    1535      177669 :   return v;
    1536             : }
    1537             : static GEN
    1538        1639 : famat_to_arch(GEN nf, GEN fa, long prec)
    1539             : {
    1540        1639 :   GEN g,e, y = NULL;
    1541             :   long i,l;
    1542             : 
    1543        1639 :   if (typ(fa) != t_MAT) pari_err_TYPE("famat_to_arch",fa);
    1544        1639 :   if (lg(fa) == 1) return triv_arch(nf);
    1545         966 :   g = gel(fa,1);
    1546         966 :   e = gel(fa,2); l = lg(e);
    1547        4970 :   for (i=1; i<l; i++)
    1548             :   {
    1549        4004 :     GEN t, x = nf_to_scalar_or_basis(nf, gel(g,i));
    1550             :     /* multiplicative arch would be better (save logs), but exponents overflow
    1551             :      * [ could keep track of expo separately, but not worth it ] */
    1552        4004 :     t = get_arch(nf,x,prec); if (!t) return NULL;
    1553        4004 :     if (gel(t,1) == gen_0) continue; /* rational */
    1554        3545 :     t = RgV_Rg_mul(t, gel(e,i));
    1555        3545 :     y = y? RgV_add(y,t): t;
    1556             :   }
    1557         966 :   return y ? y: triv_arch(nf);
    1558             : }
    1559             : 
    1560             : static GEN
    1561        1334 : famat_get_arch_real(GEN nf,GEN x,GEN *emb,long prec)
    1562             : {
    1563        1334 :   GEN A, T, a, t, g = gel(x,1), e = gel(x,2);
    1564        1334 :   long i, l = lg(e);
    1565             : 
    1566        1334 :   if (l <= 1)
    1567           0 :     return get_arch_real(nf, gen_1, emb, prec);
    1568        1334 :   A = T = NULL; /* -Wall */
    1569        5604 :   for (i=1; i<l; i++)
    1570             :   {
    1571        4274 :     a = get_arch_real(nf, gel(g,i), &t, prec);
    1572        4274 :     if (!a) return NULL;
    1573        4270 :     a = RgC_Rg_mul(a, gel(e,i));
    1574        4270 :     t = vecpow(t, gel(e,i));
    1575        4270 :     if (i == 1) { A = a;          T = t; }
    1576        2940 :     else        { A = gadd(A, a); T = vecmul(T, t); }
    1577             :   }
    1578        1330 :   *emb = T; return A;
    1579             : }
    1580             : 
    1581             : static GEN
    1582        1309 : scalar_get_arch_real(GEN nf, GEN u, GEN *emb)
    1583             : {
    1584             :   GEN v, logu;
    1585        1309 :   long i, s = signe(u), RU = lg(nf_get_roots(nf))-1, R1 = nf_get_r1(nf);
    1586             : 
    1587        1309 :   if (!s) pari_err_DOMAIN("get_arch_real","argument","=",gen_0,u);
    1588        1309 :   v = cgetg(RU+1, t_COL);
    1589        1309 :   logu = logr_abs(u);
    1590        1309 :   for (i=1; i<=R1; i++) gel(v,i) = logu;
    1591        1309 :   if (i <= RU)
    1592             :   {
    1593         567 :     GEN logu2 = shiftr(logu,1);
    1594         567 :     for (   ; i<=RU; i++) gel(v,i) = logu2;
    1595             :   }
    1596        1309 :   *emb = const_col(RU, u); return v;
    1597             : }
    1598             : 
    1599             : static int
    1600       13588 : low_prec(GEN x) { return gequal0(x) || (typ(x) == t_REAL && realprec(x) <= DEFAULTPREC); }
    1601             : 
    1602             : /* For internal use. Get archimedean components: [e_i log( | sigma_i(x) | )],
    1603             :  * with e_i = 1 (resp 2.) for i <= R1 (resp. > R1)
    1604             :  * Return NULL if precision problem, and set *emb to the embeddings of x */
    1605             : GEN
    1606        6847 : get_arch_real(GEN nf, GEN x, GEN *emb, long prec)
    1607             : {
    1608             :   long i, lx, R1;
    1609             :   GEN v, t;
    1610             : 
    1611        6847 :   if (typ(x) == t_MAT) return famat_get_arch_real(nf,x,emb,prec);
    1612        5513 :   x = nf_to_scalar_or_basis(nf,x);
    1613        5513 :   if (typ(x) != t_COL) return scalar_get_arch_real(nf, gtofp(x,prec), emb);
    1614        4204 :   R1 = nf_get_r1(nf);
    1615        4204 :   x = RgM_RgC_mul(nf_get_M(nf), x);
    1616        4204 :   lx = lg(x);
    1617        4204 :   v = cgetg(lx,t_COL);
    1618        7935 :   for (i=1; i<=R1; i++)
    1619             :   {
    1620        3738 :     t = gabs(gel(x,i),prec); if (low_prec(t)) return NULL;
    1621        3731 :     gel(v,i) = glog(t,prec);
    1622             :   }
    1623       13987 :   for (   ; i< lx; i++)
    1624             :   {
    1625        9850 :     t = gnorm(gel(x,i)); if (low_prec(t)) return NULL;
    1626        9790 :     gel(v,i) = glog(t,prec);
    1627             :   }
    1628        4137 :   *emb = x; return v;
    1629             : }
    1630             : 
    1631             : 
    1632             : GEN
    1633      207630 : init_red_mod_units(GEN bnf, long prec)
    1634             : {
    1635      207630 :   GEN s = gen_0, p1,s1,mat, logfu = bnf_get_logfu(bnf);
    1636      207630 :   long i,j, RU = lg(logfu);
    1637             : 
    1638      207630 :   if (RU == 1) return NULL;
    1639      207630 :   mat = cgetg(RU,t_MAT);
    1640      532142 :   for (j=1; j<RU; j++)
    1641             :   {
    1642      324512 :     p1 = cgetg(RU+1,t_COL); gel(mat,j) = p1;
    1643      324512 :     s1 = gen_0;
    1644      909594 :     for (i=1; i<RU; i++)
    1645             :     {
    1646      585082 :       gel(p1,i) = real_i(gcoeff(logfu,i,j));
    1647      585082 :       s1 = mpadd(s1, mpsqr(gel(p1,i)));
    1648             :     }
    1649      324512 :     gel(p1,RU) = gen_0; if (mpcmp(s1,s) > 0) s = s1;
    1650             :   }
    1651      207630 :   s = gsqrt(gmul2n(s,RU),prec);
    1652      207630 :   if (expo(s) < 27) s = utoipos(1UL << 27);
    1653      207630 :   return mkvec2(mat, s);
    1654             : }
    1655             : 
    1656             : /* z computed above. Return unit exponents that would reduce col (arch) */
    1657             : GEN
    1658      207630 : red_mod_units(GEN col, GEN z)
    1659             : {
    1660             :   long i,RU;
    1661             :   GEN x,mat,N2;
    1662             : 
    1663      207630 :   if (!z) return NULL;
    1664      207630 :   mat= gel(z,1);
    1665      207630 :   N2 = gel(z,2);
    1666      207630 :   RU = lg(mat); x = cgetg(RU+1,t_COL);
    1667      207630 :   for (i=1; i<RU; i++) gel(x,i) = real_i(gel(col,i));
    1668      207630 :   gel(x,RU) = N2;
    1669      207630 :   x = lll(shallowconcat(mat,x));
    1670      207630 :   if (typ(x) != t_MAT) return NULL;
    1671      207630 :   x = gel(x,RU);
    1672      207630 :   if (signe(gel(x,RU)) < 0) x = gneg_i(x);
    1673      207630 :   if (!gequal1(gel(x,RU))) pari_err_BUG("red_mod_units");
    1674      207630 :   setlg(x,RU); return x;
    1675             : }
    1676             : 
    1677             : /* [x] archimedian components, A column vector. return [x] A
    1678             :  * x may be a translated GEN (y + k) */
    1679             : static GEN
    1680      503357 : act_arch(GEN A, GEN x)
    1681             : {
    1682             :   GEN a;
    1683      503357 :   long i,l = lg(A), tA = typ(A);
    1684      503357 :   if (tA == t_MAT)
    1685             :   { /* assume lg(x) >= l */
    1686        6992 :     a = cgetg(l, t_VEC);
    1687        6992 :     for (i=1; i<l; i++) gel(a,i) = act_arch(gel(A,i), x);
    1688        6992 :     return a;
    1689             :   }
    1690      496365 :   if (l==1) return cgetg(1, t_VEC);
    1691      496365 :   a = NULL;
    1692      496365 :   if (tA == t_VECSMALL)
    1693             :   {
    1694     1593728 :     for (i=1; i<l; i++)
    1695             :     {
    1696     1383575 :       long c = A[i];
    1697     1383575 :       if (!c) continue;
    1698      111980 :       if (!a) { a = gmulsg(c, gel(x,i)); continue; }
    1699       20340 :       a = gadd(a, gmulsg(c, gel(x,i)));
    1700             :     }
    1701             :   }
    1702             :   else
    1703             :   { /* A a t_COL of t_INT. Assume lg(A)==lg(x) */
    1704      594800 :     for (i=1; i<l; i++)
    1705             :     {
    1706      308588 :       GEN c = gel(A,i);
    1707      308588 :       if (!signe(c)) continue;
    1708      161811 :       if (!a) { a = gmul(c, gel(x,i)); continue; }
    1709        5842 :       a = gadd(a, gmul(gel(A,i), gel(x,i)));
    1710             :     }
    1711             :   }
    1712      496365 :   if (!a) return zerovec(lgcols(x)-1);
    1713      247609 :   settyp(a, t_VEC); return a;
    1714             : }
    1715             : 
    1716             : static long
    1717      282128 : prec_arch(GEN bnf)
    1718             : {
    1719      282128 :   GEN a = bnf_get_C(bnf);
    1720      282128 :   long i, l = lg(a), prec;
    1721             : 
    1722      283038 :   for (i=1; i<l; i++)
    1723      282814 :     if ( (prec = gprecision(gel(a,i))) ) return prec;
    1724         224 :   return DEFAULTPREC;
    1725             : }
    1726             : 
    1727             : static long
    1728        1100 : needed_bitprec(GEN x)
    1729             : {
    1730        1100 :   long i, e = 0, l = lg(x);
    1731        6287 :   for (i = 1; i < l; i++)
    1732             :   {
    1733        5187 :     GEN c = gel(x,i);
    1734        5187 :     long f = gexpo(c) - prec2nbits(gprecision(c));
    1735        5187 :     if (f > e) e = f;
    1736             :   }
    1737        1100 :   return e;
    1738             : }
    1739             : 
    1740             : /* col = archimedian components of x, Nx = kNx^e its norm (e > 0, usually = 1),
    1741             :  * dx a bound for its denominator. Return x or NULL (fail) */
    1742             : GEN
    1743      212432 : isprincipalarch(GEN bnf, GEN col, GEN kNx, GEN e, GEN dx, long *pe)
    1744             : {
    1745             :   GEN nf, x, y, logfu, s, M;
    1746      212432 :   long N, R1, RU, i, prec = gprecision(col);
    1747      212432 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf); M = nf_get_M(nf);
    1748      212432 :   if (!prec) prec = prec_arch(bnf);
    1749      212432 :   logfu = bnf_get_logfu(bnf);
    1750      212432 :   N = nf_get_degree(nf);
    1751      212432 :   R1 = nf_get_r1(nf);
    1752      212432 :   RU = (N + R1)>>1;
    1753      212432 :   col = cleanarch(col,N,prec); settyp(col, t_COL);
    1754      212432 :   if (!col) pari_err_PREC( "isprincipalarch");
    1755      212432 :   if (RU > 1)
    1756             :   { /* reduce mod units */
    1757      207630 :     GEN u, z = init_red_mod_units(bnf,prec);
    1758      207630 :     u = red_mod_units(col,z);
    1759      207630 :     if (!u && z) return NULL;
    1760      207630 :     if (u) col = RgC_add(col, RgM_RgC_mul(logfu, u));
    1761             :   }
    1762      212432 :   s = divru(mulir(e, glog(kNx,prec)), N);
    1763      212432 :   for (i=1; i<=R1; i++) gel(col,i) = gexp(gadd(s, gel(col,i)),prec);
    1764      212432 :   for (   ; i<=RU; i++) gel(col,i) = gexp(gadd(s, gmul2n(gel(col,i),-1)),prec);
    1765             :   /* d.alpha such that x = alpha \prod gj^ej */
    1766      212432 :   x = RgM_solve_realimag(M,col); if (!x) return NULL;
    1767      212432 :   x = RgC_Rg_mul(x, dx);
    1768      212432 :   y = grndtoi(x, pe);
    1769      212432 :   if (*pe > -5)
    1770             :   {
    1771        1100 :     *pe = needed_bitprec(x);
    1772        1100 :     return NULL;
    1773             :   }
    1774      211332 :   return RgC_Rg_div(y, dx);
    1775             : }
    1776             : 
    1777             : /* y = C \prod g[i]^e[i] ? */
    1778             : static int
    1779      211332 : fact_ok(GEN nf, GEN y, GEN C, GEN g, GEN e)
    1780             : {
    1781      211332 :   pari_sp av = avma;
    1782      211332 :   long i, c = lg(e);
    1783      211332 :   GEN z = C? C: gen_1;
    1784      360164 :   for (i=1; i<c; i++)
    1785      148832 :     if (signe(gel(e,i))) z = idealmul(nf, z, idealpow(nf, gel(g,i), gel(e,i)));
    1786      211332 :   if (typ(z) != t_MAT) z = idealhnf_shallow(nf,z);
    1787      211332 :   if (typ(y) != t_MAT) y = idealhnf_shallow(nf,y);
    1788      211332 :   i = ZM_equal(y, z); avma = av; return i;
    1789             : }
    1790             : 
    1791             : /* assume x in HNF. cf class_group_gen for notations.
    1792             :  * Return NULL iff flag & nf_FORCE and computation of principal ideal generator
    1793             :  * fails */
    1794             : static GEN
    1795      257074 : isprincipalall(GEN bnf, GEN x, long *ptprec, long flag)
    1796             : {
    1797      257074 :   long i,nW,nB,e,c, prec = *ptprec;
    1798             :   GEN Q,xar,Wex,Bex,U,p1,gen,cyc,xc,ex,d,col,A;
    1799      257074 :   GEN W  = bnf_get_W(bnf);
    1800      257074 :   GEN B  = bnf_get_B(bnf);
    1801      257074 :   GEN C  = bnf_get_C(bnf);
    1802      257074 :   GEN nf = bnf_get_nf(bnf);
    1803      257074 :   GEN clg2 = gel(bnf,9);
    1804             :   FB_t F;
    1805      257074 :   GEN Vbase = bnf_get_vbase(bnf);
    1806      257074 :   GEN L = recover_partFB(&F, Vbase, lg(x)-1);
    1807             :   pari_sp av;
    1808             :   FACT *fact;
    1809             : 
    1810      257074 :   U = gel(clg2,1);
    1811      257074 :   cyc = bnf_get_cyc(bnf); c = lg(cyc)-1;
    1812      257074 :   gen = bnf_get_gen(bnf);
    1813      257074 :   ex = cgetg(c+1,t_COL);
    1814      257074 :   if (c == 0 && !(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return ex;
    1815             : 
    1816             :   /* factor x */
    1817      256171 :   x = Q_primitive_part(x, &xc);
    1818      256171 :   av = avma;
    1819             : 
    1820      256171 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    1821      256171 :   xar = split_ideal(nf, &F, x, Vbase, L, fact);
    1822      256171 :   nW = lg(W)-1; Wex = zero_zv(nW);
    1823      256171 :   nB = lg(B)-1; Bex = zero_zv(nB);
    1824      464645 :   for (i=1; i<=fact[0].pr; i++)
    1825             :   {
    1826      208474 :     long k = fact[i].pr;
    1827      208474 :     long l = k - nW;
    1828      208474 :     if (l <= 0) Wex[k] = fact[i].ex;
    1829      149703 :     else        Bex[l] = fact[i].ex;
    1830             :   }
    1831             : 
    1832             :   /* x = -g_W Wex - g_B Bex + [xar]  | x = g_W Wex + g_B Bex if xar = NULL
    1833             :    *   = g_W A + [xar] - [C_B]Bex    |   = g_W A + [C_B]Bex
    1834             :    * since g_W B + g_B = [C_B] */
    1835      256171 :   if (xar)
    1836             :   {
    1837      192971 :     if (!nB) /*treat specially B = matrix(n,0): PARI can't represent it*/
    1838          14 :       A = zc_to_ZC(zv_neg(Wex));
    1839             :     else
    1840             :     {
    1841      192957 :       A = ZC_sub(ZM_zc_mul(B,Bex), zc_to_ZC(Wex));
    1842      192957 :       Bex = zv_neg(Bex);
    1843             :     }
    1844             :   }
    1845             :   else
    1846             :   {
    1847       63200 :     if (!nB)
    1848         105 :       A = zc_to_ZC(Wex);
    1849             :     else
    1850       63095 :       A = ZC_sub(zc_to_ZC(Wex), ZM_zc_mul(B,Bex));
    1851             :   }
    1852      256171 :   Q = ZM_ZC_mul(U, A);
    1853      475372 :   for (i=1; i<=c; i++)
    1854      219201 :     gel(Q,i) = truedvmdii(gel(Q,i), gel(cyc,i), (GEN*)(ex+i));
    1855      256171 :   if ((flag & nf_GEN_IF_PRINCIPAL))
    1856       30665 :     { if (!ZV_equal0(ex)) return gen_0; }
    1857      225506 :   else if (!(flag & (nf_GEN|nf_GENMAT)))
    1858       45906 :     return ZC_copy(ex);
    1859             : 
    1860             :   /* compute arch component of the missing principal ideal */
    1861             :   { /* g A = G Ur A + [ga]A, Ur A = D Q + R as above (R = ex)
    1862             :            = G R + [GD]Q + [ga]A */
    1863      210258 :     GEN ga = gel(clg2,2), GD = gel(clg2,3);
    1864      210258 :     if (nB) col = act_arch(Bex, C + nW); else col = triv_arch(nf);
    1865      210258 :     if (nW) col = gadd(col, act_arch(A, ga));
    1866      210258 :     if (c)  col = gadd(col, act_arch(Q, GD));
    1867             :   }
    1868      210258 :   if (xar)
    1869             :   {
    1870      172328 :     GEN t = get_arch(nf, xar, prec);
    1871      172328 :     col = t? gadd(col, t):NULL;
    1872             :   }
    1873             : 
    1874             :   /* find coords on Zk; Q = N (x / \prod gj^ej) = N(alpha), denom(alpha) | d */
    1875      210258 :   Q = gdiv(ZM_det_triangular(x), get_norm_fact(gen, ex, &d));
    1876      210258 :   col = col?isprincipalarch(bnf, col, Q, gen_1, d, &e):NULL;
    1877      210258 :   if (col && !fact_ok(nf,x, col,gen,ex)) col = NULL;
    1878      210258 :   if (!col && !ZV_equal0(ex))
    1879             :   {
    1880             :     /* in case isprincipalfact calls bnfinit() due to prec trouble...*/
    1881        1022 :     ex = gerepilecopy(av, ex);
    1882        1022 :     p1 = isprincipalfact(bnf, x, gen, ZC_neg(ex), flag);
    1883        1022 :     if (typ(p1) != t_VEC) return p1;
    1884        1022 :     col = gel(p1,2);
    1885             :   }
    1886      210258 :   if (col)
    1887             :   { /* add back missing content */
    1888      210217 :     if (xc) col = (typ(col)==t_MAT)? famat_mul(col,xc): RgC_Rg_mul(col,xc);
    1889             :   }
    1890             :   else
    1891             :   {
    1892          41 :     if (e < 0) e = 0;
    1893          41 :     *ptprec = prec + nbits2extraprec(e + 128);
    1894          41 :     if (flag & nf_FORCE)
    1895             :     {
    1896          34 :       if (DEBUGLEVEL) pari_warn(warner,"precision too low for generators, e = %ld",e);
    1897          34 :       return NULL;
    1898             :     }
    1899           7 :     pari_warn(warner,"precision too low for generators, not given");
    1900           7 :     col = cgetg(1, t_COL);
    1901             :   }
    1902      210224 :   return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(ex, col);
    1903             : }
    1904             : 
    1905             : static GEN
    1906       10213 : triv_gen(GEN bnf, GEN x, long flag)
    1907             : {
    1908       10213 :   GEN y, nf = bnf_get_nf(bnf);
    1909             :   long c;
    1910       10213 :   if (flag & nf_GEN_IF_PRINCIPAL) return algtobasis(nf,x);
    1911       10213 :   c = lg(bnf_get_cyc(bnf)) - 1;
    1912       10213 :   if (!(flag & (nf_GEN|nf_GENMAT))) return zerocol(c);
    1913        9352 :   y = cgetg(3,t_VEC);
    1914        9352 :   gel(y,1) = zerocol(c);
    1915        9352 :   gel(y,2) = algtobasis(nf,x); return y;
    1916             : }
    1917             : 
    1918             : GEN
    1919      233427 : bnfisprincipal0(GEN bnf,GEN x,long flag)
    1920             : {
    1921             :   GEN arch, c;
    1922             :   long pr;
    1923      233427 :   pari_sp av = avma;
    1924             : 
    1925      233427 :   bnf = checkbnf(bnf);
    1926      233427 :   switch( idealtyp(&x, &arch) )
    1927             :   {
    1928             :     case id_PRINCIPAL:
    1929        7490 :       if (gequal0(x)) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1930        7490 :       return triv_gen(bnf, x, flag);
    1931             :     case id_PRIME:
    1932      222612 :       if (pr_is_inert(x))
    1933        2723 :         return gerepileupto(av, triv_gen(bnf, gel(x,1), flag));
    1934      219889 :       x = idealhnf_two(bnf_get_nf(bnf), x);
    1935      219889 :       break;
    1936             :     case id_MAT:
    1937        3325 :       if (lg(x)==1) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1938             :   }
    1939      223214 :   pr = prec_arch(bnf); /* precision of unit matrix */
    1940      223214 :   c = getrand();
    1941             :   for (;;)
    1942             :   {
    1943      223214 :     pari_sp av1 = avma;
    1944      223214 :     GEN y = isprincipalall(bnf,x,&pr,flag);
    1945      223214 :     if (y) return gerepilecopy(av, y);
    1946             : 
    1947           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",pr);
    1948           0 :     avma = av1; bnf = bnfnewprec_shallow(bnf,pr); setrand(c);
    1949           0 :   }
    1950             : }
    1951             : GEN
    1952       47628 : isprincipal(GEN bnf,GEN x) { return bnfisprincipal0(bnf,x,0); }
    1953             : 
    1954             : /* FIXME: OBSOLETE */
    1955             : GEN
    1956           0 : isprincipalgen(GEN bnf,GEN x)
    1957           0 : { return bnfisprincipal0(bnf,x,nf_GEN); }
    1958             : GEN
    1959           0 : isprincipalforce(GEN bnf,GEN x)
    1960           0 : { return bnfisprincipal0(bnf,x,nf_FORCE); }
    1961             : GEN
    1962           0 : isprincipalgenforce(GEN bnf,GEN x)
    1963           0 : { return bnfisprincipal0(bnf,x,nf_GEN | nf_FORCE); }
    1964             : 
    1965             : static GEN
    1966       33826 : add_principal_part(GEN nf, GEN u, GEN v, long flag)
    1967             : {
    1968       33826 :   if (flag & nf_GENMAT)
    1969        1906 :     return (typ(u) == t_COL && RgV_isscalar(u) && gequal1(gel(u,1)))? v: famat_mul(v,u);
    1970             :   else
    1971       31920 :     return nfmul(nf, v, u);
    1972             : }
    1973             : 
    1974             : #if 0
    1975             : /* compute C prod P[i]^e[i],  e[i] >=0 for all i. C may be NULL (omitted)
    1976             :  * e destroyed ! */
    1977             : static GEN
    1978             : expand(GEN nf, GEN C, GEN P, GEN e)
    1979             : {
    1980             :   long i, l = lg(e), done = 1;
    1981             :   GEN id = C;
    1982             :   for (i=1; i<l; i++)
    1983             :   {
    1984             :     GEN ei = gel(e,i);
    1985             :     if (signe(ei))
    1986             :     {
    1987             :       if (mod2(ei)) id = id? idealmul(nf, id, gel(P,i)): gel(P,i);
    1988             :       ei = shifti(ei,-1);
    1989             :       if (signe(ei)) done = 0;
    1990             :       gel(e,i) = ei;
    1991             :     }
    1992             :   }
    1993             :   if (id != C) id = idealred(nf, id);
    1994             :   if (done) return id;
    1995             :   return idealmulred(nf, id, idealsqr(nf, expand(nf,id,P,e)));
    1996             : }
    1997             : /* C is an extended ideal, possibly with C[1] = NULL */
    1998             : static GEN
    1999             : expandext(GEN nf, GEN C, GEN P, GEN e)
    2000             : {
    2001             :   long i, l = lg(e), done = 1;
    2002             :   GEN A = gel(C,1);
    2003             :   for (i=1; i<l; i++)
    2004             :   {
    2005             :     GEN ei = gel(e,i);
    2006             :     if (signe(ei))
    2007             :     {
    2008             :       if (mod2(ei)) A = A? idealmul(nf, A, gel(P,i)): gel(P,i);
    2009             :       ei = shifti(ei,-1);
    2010             :       if (signe(ei)) done = 0;
    2011             :       gel(e,i) = ei;
    2012             :     }
    2013             :   }
    2014             :   if (A == gel(C,1))
    2015             :     A = C;
    2016             :   else
    2017             :     A = idealred(nf, mkvec2(A, gel(C,2)));
    2018             :   if (done) return A;
    2019             :   return idealmulred(nf, A, idealsqr(nf, expand(nf,A,P,e)));
    2020             : }
    2021             : #endif
    2022             : 
    2023             : static GEN
    2024           0 : expand(GEN nf, GEN C, GEN P, GEN e)
    2025             : {
    2026           0 :   long i, l = lg(e);
    2027           0 :   GEN B, A = C;
    2028           0 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    2029           0 :     if (signe(gel(e,i)))
    2030             :     {
    2031           0 :       B = idealpowred(nf, gel(P,i), gel(e,i));
    2032           0 :       A = A? idealmulred(nf,A,B): B;
    2033             :     }
    2034           0 :   return A;
    2035             : }
    2036             : static GEN
    2037       33840 : expandext(GEN nf, GEN C, GEN P, GEN e)
    2038             : {
    2039       33840 :   long i, l = lg(e);
    2040       33840 :   GEN B, A = gel(C,1), C1 = A;
    2041      157008 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    2042      123168 :     if (signe(gel(e,i)))
    2043             :     {
    2044       46698 :       gel(C,1) = gel(P,i);
    2045       46698 :       B = idealpowred(nf, C, gel(e,i));
    2046       46698 :       A = A? idealmulred(nf,A,B): B;
    2047             :     }
    2048       33840 :   return A == C1? C: A;
    2049             : }
    2050             : 
    2051             : /* isprincipal for C * \prod P[i]^e[i] (C omitted if NULL) */
    2052             : GEN
    2053       33789 : isprincipalfact(GEN bnf, GEN C, GEN P, GEN e, long flag)
    2054             : {
    2055       33789 :   const long gen = flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL);
    2056             :   long prec;
    2057       33789 :   pari_sp av = avma;
    2058       33789 :   GEN C0, Cext, c, id, nf = checknf(bnf);
    2059             : 
    2060       33789 :   if (gen)
    2061             :   {
    2062       33789 :     Cext = (flag & nf_GENMAT)? cgetg(1, t_MAT): mkpolmod(gen_1,nf_get_pol(nf));
    2063       33789 :     C0 = mkvec2(C, Cext);
    2064       33789 :     id = expandext(nf, C0, P, e);
    2065             :   } else {
    2066           0 :     Cext = NULL;
    2067           0 :     C0 = C;
    2068           0 :     id = expand(nf, C, P, e);
    2069             :   }
    2070       33789 :   if (id == C0) /* e = 0 */
    2071             :   {
    2072        1701 :     if (!C) return bnfisprincipal0(bnf, gen_1, flag);
    2073        1701 :     C = idealhnf_shallow(nf,C);
    2074             :   }
    2075             :   else
    2076             :   {
    2077       32088 :     if (gen) { C = gel(id,1); Cext = gel(id,2); } else C = id;
    2078             :   }
    2079       33789 :   prec = prec_arch(bnf);
    2080       33789 :   c = getrand();
    2081             :   for (;;)
    2082             :   {
    2083       33809 :     pari_sp av1 = avma;
    2084       33809 :     GEN y = isprincipalall(bnf, C, &prec, flag);
    2085       33809 :     if (y)
    2086             :     {
    2087       33789 :       if (flag & nf_GEN_IF_PRINCIPAL)
    2088             :       {
    2089       30618 :         if (typ(y) == t_INT) { avma = av; return NULL; }
    2090       30618 :         y = add_principal_part(nf, y, Cext, flag);
    2091             :       }
    2092             :       else
    2093             :       {
    2094        3171 :         GEN u = gel(y,2);
    2095        3171 :         if (!gen || typ(y) != t_VEC) return gerepileupto(av,y);
    2096        3171 :         if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    2097             :       }
    2098       33789 :       return gerepilecopy(av, y);
    2099             :     }
    2100          20 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",prec);
    2101          20 :     avma = av1; bnf = bnfnewprec_shallow(bnf,prec); setrand(c);
    2102          20 :   }
    2103             : }
    2104             : GEN
    2105          51 : isprincipalfact_or_fail(GEN bnf, GEN C, GEN P, GEN e)
    2106             : {
    2107          51 :   const long flag = nf_GENMAT|nf_FORCE;
    2108             :   long prec;
    2109          51 :   pari_sp av = avma;
    2110          51 :   GEN u, y, id, C0, Cext, nf = bnf_get_nf(bnf);
    2111             : 
    2112          51 :   Cext = cgetg(1, t_MAT);
    2113          51 :   C0 = mkvec2(C, Cext);
    2114          51 :   id = expandext(nf, C0, P, e);
    2115          51 :   if (id == C0) /* e = 0 */
    2116          12 :     C = idealhnf_shallow(nf,C);
    2117             :   else {
    2118          39 :     C = gel(id,1); Cext = gel(id,2);
    2119             :   }
    2120          51 :   prec = prec_arch(bnf);
    2121          51 :   y = isprincipalall(bnf, C, &prec, flag);
    2122          51 :   if (!y) { avma = av; return utoipos(prec); }
    2123          37 :   u = gel(y,2);
    2124          37 :   if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    2125          37 :   return gerepilecopy(av, y);
    2126             : }
    2127             : 
    2128             : /* if x a famat, assume it is an algebraic integer (very costly to check) */
    2129             : GEN
    2130        2310 : bnfisunit(GEN bnf,GEN x)
    2131             : {
    2132        2310 :   long tx = typ(x), i, R1, RU, e, n, prec;
    2133        2310 :   pari_sp av = avma;
    2134             :   GEN p1, v, rlog, logunit, ex, nf, pi2_sur_w, emb;
    2135             : 
    2136        2310 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    2137        2310 :   logunit = bnf_get_logfu(bnf); RU = lg(logunit);
    2138        2310 :   n = bnf_get_tuN(bnf); /* # { roots of 1 } */
    2139        2310 :   if (tx == t_MAT)
    2140             :   { /* famat, assumed integral */
    2141        1330 :     if (lg(x) != 3) pari_err_TYPE("bnfisunit [not a factorization]", x);
    2142             :   } else {
    2143         980 :     x = nf_to_scalar_or_basis(nf,x);
    2144         980 :     if (typ(x) != t_COL)
    2145             :     { /* rational unit ? */
    2146             :       long s;
    2147         126 :       if (typ(x) != t_INT || !is_pm1(x)) return cgetg(1,t_COL);
    2148         126 :       s = signe(x); avma = av; v = zerocol(RU);
    2149         126 :       gel(v,RU) = mkintmodu((s > 0)? 0: n>>1, n);
    2150         126 :       return v;
    2151             :     }
    2152         854 :     if (!isint1(Q_denom(x))) { avma = av; return cgetg(1,t_COL); }
    2153             :   }
    2154             : 
    2155        2184 :   R1 = nf_get_r1(nf); v = cgetg(RU+1,t_COL);
    2156        2184 :   for (i=1; i<=R1; i++) gel(v,i) = gen_1;
    2157        2184 :   for (   ; i<=RU; i++) gel(v,i) = gen_2;
    2158        2184 :   logunit = shallowconcat(logunit, v);
    2159             :   /* ex = fundamental units exponents */
    2160        2184 :   rlog = real_i(logunit);
    2161        2184 :   prec = nf_get_prec(nf);
    2162        2230 :   for (i=1;; i++)
    2163             :   {
    2164        2230 :     GEN rx = get_arch_real(nf,x,&emb, MEDDEFAULTPREC);
    2165        2230 :     if (rx)
    2166             :     {
    2167        2184 :       GEN logN = RgV_sum(rx); /* log(Nx), should be ~ 0 */
    2168        2184 :       if (gexpo(logN) > -20)
    2169             :       { /* precision problem ? */
    2170           7 :         if (typ(logN) != t_REAL) { avma = av; return cgetg(1,t_COL); } /*no*/
    2171           7 :         if (i == 1)
    2172             :         {
    2173           7 :           GEN N = nfnorm(nf, x);
    2174           7 :           if (!is_pm1(N)) { avma = av; return cgetg(1, t_COL); }
    2175             :         }
    2176             :       }
    2177             :       else
    2178             :       {
    2179        2177 :         ex = RgM_solve(rlog, rx);
    2180        2177 :         if (ex)
    2181             :         {
    2182        2177 :           ex = grndtoi(ex, &e);
    2183        2177 :           if (!signe(gel(ex,RU)) && e < -4) break;
    2184             :         }
    2185             :       }
    2186             :     }
    2187          46 :     if (i == 1)
    2188          23 :       prec = nbits2prec(gexpo(x) + 128);
    2189             :     else
    2190             :     {
    2191          23 :       if (i > 4) pari_err_PREC("bnfisunit");
    2192          23 :       prec = precdbl(prec);
    2193             :     }
    2194          46 :     if (DEBUGLEVEL) pari_warn(warnprec,"bnfisunit",prec);
    2195          46 :     nf = nfnewprec_shallow(nf, prec);
    2196          46 :   }
    2197             : 
    2198        2177 :   setlg(ex, RU); /* ZC */
    2199        2177 :   p1 = imag_i( row_i(logunit,1, 1,RU-1) );
    2200        2177 :   p1 = RgV_dotproduct(p1, ex); if (!R1) p1 = gmul2n(p1, -1);
    2201        2177 :   p1 = gsub(garg(gel(emb,1),prec), p1);
    2202             :   /* p1 = arg(the missing root of 1) */
    2203             : 
    2204        2177 :   pi2_sur_w = divru(mppi(prec), n>>1); /* 2pi / n */
    2205        2177 :   e = umodiu(roundr(divrr(p1, pi2_sur_w)), n);
    2206        2177 :   if (n > 2)
    2207             :   {
    2208         826 :     GEN z = algtobasis(nf, bnf_get_tuU(bnf)); /* primitive root of 1 */
    2209         826 :     GEN ro = RgV_dotproduct(row(nf_get_M(nf), 1), z);
    2210         826 :     GEN p2 = roundr(divrr(garg(ro, prec), pi2_sur_w));
    2211         826 :     e *= Fl_inv(umodiu(p2,n), n);
    2212         826 :     e %= n;
    2213             :   }
    2214             : 
    2215        2177 :   gel(ex,RU) = mkintmodu(e, n);
    2216        2177 :   setlg(ex, RU+1); return gerepilecopy(av, ex);
    2217             : }
    2218             : 
    2219             : GEN
    2220        7280 : nfsign_from_logarch(GEN LA, GEN invpi, GEN archp)
    2221             : {
    2222        7280 :   long l = lg(archp), i;
    2223        7280 :   GEN y = cgetg(l, t_VECSMALL);
    2224        7280 :   pari_sp av = avma;
    2225             : 
    2226       17563 :   for (i=1; i<l; i++)
    2227             :   {
    2228       10283 :     GEN c = ground( gmul(imag_i(gel(LA,archp[i])), invpi) );
    2229       10283 :     y[i] = mpodd(c)? 1: 0;
    2230             :   }
    2231        7280 :   avma = av; return y;
    2232             : }
    2233             : 
    2234             : GEN
    2235        6650 : nfsign_units(GEN bnf, GEN archp, int add_zu)
    2236             : {
    2237        6650 :   GEN invpi, y, A = bnf_get_logfu(bnf), nf = bnf_get_nf(bnf);
    2238        6650 :   long j = 1, RU = lg(A);
    2239             : 
    2240        6650 :   invpi = invr( mppi(nf_get_prec(nf)) );
    2241        6650 :   if (!archp) archp = identity_perm( nf_get_r1(nf) );
    2242        6650 :   if (add_zu) { RU++; A--; }
    2243        6650 :   y = cgetg(RU,t_MAT);
    2244        6650 :   if (add_zu)
    2245             :   {
    2246        5495 :     long w = bnf_get_tuN(bnf);
    2247       16261 :     gel(y, j++) = (w == 2)? const_vecsmall(lg(archp)-1, 1)
    2248       10766 :                           : cgetg(1, t_VECSMALL);
    2249             :   }
    2250        6650 :   for ( ; j < RU; j++) gel(y,j) = nfsign_from_logarch(gel(A,j), invpi, archp);
    2251        6650 :   return y;
    2252             : }
    2253             : 
    2254             : /* obsolete */
    2255             : GEN
    2256           7 : signunits(GEN bnf)
    2257             : {
    2258             :   pari_sp av;
    2259             :   GEN S, y, nf;
    2260             :   long i, j, r1, r2;
    2261             : 
    2262           7 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    2263           7 :   nf_get_sign(nf, &r1,&r2);
    2264           7 :   S = zeromatcopy(r1, r1+r2-1); av = avma;
    2265           7 :   y = nfsign_units(bnf, NULL, 0);
    2266          14 :   for (j = 1; j < lg(y); j++)
    2267             :   {
    2268           7 :     GEN Sj = gel(S,j), yj = gel(y,j);
    2269           7 :     for (i = 1; i <= r1; i++) gel(Sj,i) = yj[i]? gen_m1: gen_1;
    2270             :   }
    2271           7 :   avma = av; return S;
    2272             : }
    2273             : 
    2274             : static GEN
    2275       42380 : get_log_embed(REL_t *rel, GEN M, long RU, long R1, long prec)
    2276             : {
    2277       42380 :   GEN arch, C, z = rel->m;
    2278             :   long i;
    2279       42380 :   if (!z) return zerocol(RU);
    2280       31306 :   arch = typ(z) == t_COL? RgM_RgC_mul(M, z): RgC_Rg_mul(gel(M,1), z);
    2281       31306 :   C = cgetg(RU+1, t_COL); arch = glog(arch, prec);
    2282       31306 :   for (i=1; i<=R1; i++) gel(C,i) = gel(arch,i);
    2283       31306 :   for (   ; i<=RU; i++) gel(C,i) = gmul2n(gel(arch,i), 1);
    2284       31306 :   return C;
    2285             : }
    2286             : 
    2287             : static GEN
    2288       25778 : perm_log_embed(GEN C, GEN perm)
    2289             : {
    2290             :   long i, n;
    2291       25778 :   GEN Cnew = cgetg_copy(C, &n);
    2292      135414 :   for (i = 1; i < n; i++)
    2293             :   {
    2294      109636 :     long v = perm[i];
    2295      109636 :     if (v > 0)
    2296       75400 :       gel(Cnew, i) = gel(C, v);
    2297             :     else
    2298       34236 :       gel(Cnew, i) = gconj(gel(C, -v));
    2299             :   }
    2300       25778 :   return Cnew;
    2301             : }
    2302             : 
    2303             : static GEN
    2304      437440 : set_fact(FB_t *F, FACT *fact, GEN ex, long *pnz)
    2305             : {
    2306      437440 :   long i, n = fact[0].pr;
    2307             :   long nz;
    2308      437440 :   GEN c = zero_Flv(F->KC);
    2309      437440 :   if (!n) /* trivial factorization */
    2310           0 :     *pnz = F->KC+1;
    2311             :   else {
    2312      437440 :     nz = fact[1].pr;
    2313      437440 :     if (fact[n].pr < nz) /* Possible with jid in rnd_rel */
    2314         189 :       nz = fact[n].pr;
    2315      437440 :     for (i=1; i<=n; i++) c[fact[i].pr] = fact[i].ex;
    2316      437440 :     if (ex)
    2317             :     {
    2318       15735 :       for (i=1; i<lg(ex); i++)
    2319       12216 :         if (ex[i]) {
    2320       11524 :           long v = F->subFB[i];
    2321       11524 :           c[v] += ex[i];
    2322       11524 :           if (v < nz) nz = v;
    2323             :         }
    2324             :     }
    2325      437440 :     *pnz = nz;
    2326             :   }
    2327      437440 :   return c;
    2328             : }
    2329             : 
    2330             : /* Is cols already in the cache ? bs = index of first non zero coeff in cols
    2331             :  * General check for colinearity useless since exceedingly rare */
    2332             : static int
    2333      500168 : already_known(RELCACHE_t *cache, long bs, GEN cols)
    2334             : {
    2335             :   REL_t *r;
    2336      500168 :   long l = lg(cols);
    2337    43343422 :   for (r = cache->last; r > cache->base; r--)
    2338    42915182 :     if (bs == r->nz)
    2339             :     {
    2340     3602908 :       GEN coll = r->R;
    2341     3602908 :       long b = bs;
    2342     3602908 :       while (b < l && cols[b] == coll[b]) b++;
    2343     3602908 :       if (b == l) return 1;
    2344             :     }
    2345      428240 :   return 0;
    2346             : }
    2347             : 
    2348             : /* Add relation R to cache, nz = index of first non zero coeff in R.
    2349             :  * If relation is a linear combination of the previous ones, return 0.
    2350             :  * Otherwise, update basis and return > 0. Compute mod p (much faster)
    2351             :  * so some kernel vector might not be genuine. */
    2352             : static int
    2353      500301 : add_rel_i(RELCACHE_t *cache, GEN R, long nz, GEN m, long orig, long aut, REL_t **relp, long in_rnd_rel)
    2354             : {
    2355      500301 :   long i, k, n = lg(R)-1;
    2356             : 
    2357      500301 :   if (nz == n+1) { k = 0; goto ADD_REL; }
    2358      500168 :   if (already_known(cache, nz, R)) return -1;
    2359      428240 :   if (cache->last >= cache->base + cache->len) return 0;
    2360      428240 :   if (DEBUGLEVEL>6)
    2361             :   {
    2362           0 :     err_printf("adding vector = %Ps\n",R);
    2363           0 :     err_printf("generators =\n%Ps\n", cache->basis);
    2364             :   }
    2365      428240 :   if (cache->missing)
    2366             :   {
    2367      406548 :     GEN a = leafcopy(R), basis = cache->basis;
    2368      406548 :     k = lg(a);
    2369    24003911 :     do --k; while (!a[k]);
    2370     1808504 :     while (k)
    2371             :     {
    2372     1027207 :       GEN c = gel(basis, k);
    2373     1027207 :       if (c[k])
    2374             :       {
    2375      995408 :         long ak = a[k];
    2376      995408 :         for (i=1; i < k; i++) if (c[i]) a[i] = (a[i] + ak*(mod_p-c[i])) % mod_p;
    2377      995408 :         a[k] = 0;
    2378    27729900 :         do --k; while (!a[k]); /* k cannot go below 0: codeword is a sentinel */
    2379             :       }
    2380             :       else
    2381             :       {
    2382       31799 :         ulong invak = Fl_inv(uel(a,k), mod_p);
    2383             :         /* Cleanup a */
    2384     1821046 :         for (i = k; i-- > 1; )
    2385             :         {
    2386     1757448 :           long j, ai = a[i];
    2387     1757448 :           c = gel(basis, i);
    2388     1757448 :           if (!ai || !c[i]) continue;
    2389       21982 :           ai = mod_p-ai;
    2390       21982 :           for (j = 1; j < i; j++) if (c[j]) a[j] = (a[j] + ai*c[j]) % mod_p;
    2391       21982 :           a[i] = 0;
    2392             :         }
    2393             :         /* Insert a/a[k] as k-th column */
    2394       31799 :         c = gel(basis, k);
    2395       31799 :         for (i = 1; i<k; i++) if (a[i]) c[i] = (a[i] * invak) % mod_p;
    2396       31799 :         c[k] = 1; a = c;
    2397             :         /* Cleanup above k */
    2398     1769222 :         for (i = k+1; i<n; i++)
    2399             :         {
    2400             :           long j, ck;
    2401     1737423 :           c = gel(basis, i);
    2402     1737423 :           ck = c[k];
    2403     1737423 :           if (!ck) continue;
    2404      356882 :           ck = mod_p-ck;
    2405      356882 :           for (j = 1; j < k; j++) if (a[j]) c[j] = (c[j] + ck*a[j]) % mod_p;
    2406      356882 :           c[k] = 0;
    2407             :         }
    2408       31799 :         cache->missing--;
    2409       31799 :         break;
    2410             :       }
    2411             :     }
    2412             :   }
    2413             :   else
    2414       21692 :     k = (cache->last - cache->base) + 1;
    2415      428240 :   if (k || cache->relsup > 0 || (m && in_rnd_rel))
    2416             :   {
    2417             :     REL_t *rel;
    2418             : 
    2419             : ADD_REL:
    2420       59019 :     rel = ++cache->last;
    2421       59019 :     if (!k && cache->relsup && nz < n+1)
    2422             :     {
    2423        5367 :       cache->relsup--;
    2424        5367 :       k = (rel - cache->base) + cache->missing;
    2425             :     }
    2426       59019 :     rel->R  = gclone(R);
    2427       59019 :     rel->m  =  m ? gclone(m) : NULL;
    2428       59019 :     rel->nz = nz;
    2429       59019 :     if (aut)
    2430             :     {
    2431       24859 :       rel->relorig = (rel - cache->base) - orig;
    2432       24859 :       rel->relaut = aut;
    2433             :     }
    2434             :     else
    2435       34160 :       rel->relaut = 0;
    2436       59019 :     if (relp) *relp = rel;
    2437       59019 :     if (DEBUGLEVEL) dbg_newrel(cache);
    2438             :   }
    2439      428373 :   return k;
    2440             : }
    2441             : 
    2442             : static int
    2443      446713 : add_rel(RELCACHE_t *cache, FB_t *F, GEN R, long nz, GEN m, long in_rnd_rel)
    2444             : {
    2445             :   REL_t *rel;
    2446             :   long k, l, reln;
    2447      446713 :   const long nauts = lg(F->idealperm), KC = F->KC;
    2448             : 
    2449      446713 :   k = add_rel_i(cache, R, nz, m, 0, 0, &rel, in_rnd_rel);
    2450      446713 :   if (k > 0 && m)
    2451             :   {
    2452       24859 :     GEN Rl = cgetg(KC+1, t_VECSMALL);
    2453       24859 :     reln = rel - cache->base;
    2454       78447 :     for (l = 1; l < nauts; l++)
    2455             :     {
    2456       53588 :       GEN perml = gel(F->idealperm, l);
    2457       53588 :       long i, nzl = perml[nz];
    2458             : 
    2459       53588 :       for (i = 1; i <= KC; i++) Rl[i] = 0;
    2460     4215005 :       for (i = nz; i <= KC; i++)
    2461     4161417 :         if (R[i])
    2462             :         {
    2463      194924 :           long v = perml[i];
    2464             : 
    2465      194924 :           if (v < nzl) nzl = v;
    2466      194924 :           Rl[v] = R[i];
    2467             :         }
    2468       53588 :       (void)add_rel_i(cache, Rl, nzl, NULL, reln, l, NULL, in_rnd_rel);
    2469             :     }
    2470             :   }
    2471      446713 :   return k;
    2472             : }
    2473             : 
    2474             : /* Compute powers of prime ideal (P^0,...,P^a) (a > 1) */
    2475             : static void
    2476         668 : powPgen(GEN nf, GEN vp, GEN *ppowP, long a)
    2477             : {
    2478             :   GEN id2, J;
    2479             :   long j;
    2480             : 
    2481         668 :   id2 = cgetg(a+1,t_VEC);
    2482         668 :   J = mkvec2(pr_get_p(vp), zk_scalar_or_multable(nf,pr_get_gen(vp)));
    2483         668 :   gel(id2,1) = J;
    2484         668 :   vp = idealhnf_two(nf,vp);
    2485       10688 :   for (j=2; j<=a; j++)
    2486             :   {
    2487       10020 :     if (DEBUGLEVEL>1) err_printf(" %ld", j);
    2488       10020 :     J = idealtwoelt(nf, idealmul_HNF(nf, vp, J));
    2489       10020 :     gel(J, 2) = zk_scalar_or_multable(nf, gel(J,2));
    2490       10020 :     gel(id2,j) = J;
    2491             :   }
    2492         668 :   setlg(id2, j);
    2493         668 :   *ppowP = id2;
    2494         668 :   if (DEBUGLEVEL>1) err_printf("\n");
    2495         668 : }
    2496             : 
    2497             : 
    2498             : /* Compute powers of prime ideals (P^0,...,P^a) in subFB (a > 1) */
    2499             : static void
    2500         358 : powFBgen(RELCACHE_t *cache, FB_t *F, GEN nf, GEN auts)
    2501             : {
    2502         358 :   const long a = 1L<<RANDOM_BITS;
    2503         358 :   pari_sp av = avma;
    2504         358 :   GEN subFB = F->subFB, idealperm = F->idealperm;
    2505         358 :   long i, k, l, id, n = lg(F->subFB), naut = lg(auts);
    2506             : 
    2507         358 :   if (DEBUGLEVEL) err_printf("Computing powers for subFB: %Ps\n",subFB);
    2508         358 :   if (cache) pre_allocate(cache, n*naut);
    2509        1467 :   for (i=1; i<n; i++)
    2510             :   {
    2511        1109 :     id = subFB[i];
    2512        1109 :     if (gel(F->id2, id) == gen_0)
    2513             :     {
    2514         920 :       GEN id2 = NULL;
    2515             : 
    2516        3895 :       for (k = 1; k < naut; k++)
    2517             :       {
    2518        3227 :         long sigmaid = coeff(idealperm, id, k);
    2519        3227 :         GEN sigmaid2 = gel(F->id2, sigmaid);
    2520        3227 :         if (sigmaid2 != gen_0)
    2521             :         {
    2522         252 :           GEN aut = gel(auts, k), invaut = gel(auts, F->invs[k]);
    2523             :           long lid2;
    2524         252 :           id2 = cgetg_copy(sigmaid2, &lid2);
    2525         252 :           if (DEBUGLEVEL>1) err_printf("%ld: automorphism(%ld)\n", id,sigmaid);
    2526        4284 :           for (l = 1; l < lid2; l++)
    2527             :           {
    2528        4032 :             GEN id2l = gel(sigmaid2, l);
    2529        8064 :             gel(id2, l) =
    2530        4032 :               mkvec2(gel(id2l, 1), ZM_mul(ZM_mul(invaut, gel(id2l, 2)), aut));
    2531             :           }
    2532         252 :           break;
    2533             :         }
    2534             :       }
    2535         920 :       if (!id2)
    2536             :       {
    2537         668 :         if (DEBUGLEVEL>1) err_printf("%ld: 1", id);
    2538         668 :         powPgen(nf, gel(F->LP, id), &id2, a);
    2539             :       }
    2540         920 :       gel(F->id2, id) = gclone(id2);
    2541         920 :       avma = av;
    2542             :     }
    2543             :   }
    2544         358 :   F->sfb_chg = 0;
    2545         358 :   F->newpow = 0;
    2546         358 : }
    2547             : 
    2548             : INLINE void
    2549     4289419 : step(GEN x, double *y, GEN inc, long k)
    2550             : {
    2551     4289419 :   if (!y[k])
    2552     1503701 :     x[k]++; /* leading coeff > 0 */
    2553             :   else
    2554             :   {
    2555     2785718 :     long i = inc[k];
    2556     2785718 :     x[k] += i;
    2557     2785718 :     inc[k] = (i > 0)? -1-i: 1-i;
    2558             :   }
    2559     4289419 : }
    2560             : 
    2561             : INLINE long
    2562      314205 : Fincke_Pohst_ideal(RELCACHE_t *cache, FB_t *F, GEN nf, GEN M,
    2563             :     GEN G, GEN ideal0, FACT *fact, long nbrelpid, FP_t *fp,
    2564             :     RNDREL_t *rr, long prec, long *nbsmallnorm, long *nbfact)
    2565             : {
    2566             :   pari_sp av;
    2567      314205 :   const long N = nf_get_degree(nf), R1 = nf_get_r1(nf);
    2568      314205 :   GEN r, u, gx, inc=const_vecsmall(N, 1), ideal;
    2569      314205 :   GEN Nideal = nbrelpid ? NULL : idealnorm(nf, ideal0);
    2570             :   double BOUND;
    2571      314205 :   long j, k, skipfirst, nbrelideal=0, dependent=0, try_elt=0,  try_factor=0;
    2572             : 
    2573      314205 :   u = ZM_lll(ZM_mul(F->G0, ideal0), 0.99, LLL_IM|LLL_COMPATIBLE);
    2574      314205 :   ideal = ZM_mul(ideal0,u); /* approximate T2-LLL reduction */
    2575      314205 :   r = gaussred_from_QR(RgM_mul(G, ideal), prec); /* Cholesky for T2 | ideal */
    2576      314205 :   if (!r) pari_err_BUG("small_norm (precision too low)");
    2577             : 
    2578      314205 :   skipfirst = ZV_isscalar(gel(ideal,1))? 1: 0; /* 1 probable */
    2579     1228577 :   for (k=1; k<=N; k++)
    2580             :   {
    2581      914372 :     fp->v[k] = gtodouble(gcoeff(r,k,k));
    2582      914372 :     for (j=1; j<k; j++) fp->q[j][k] = gtodouble(gcoeff(r,j,k));
    2583      914372 :     if (DEBUGLEVEL>3) err_printf("fp->v[%ld]=%.4g ",k,fp->v[k]);
    2584             :   }
    2585      314205 :   BOUND = mindd(BMULT*fp->v[1], 2*(fp->v[2]+fp->v[1]*fp->q[1][2]*fp->q[1][2]));
    2586             :   /* BOUND at most BMULT fp->x smallest known vector */
    2587      314205 :   if (DEBUGLEVEL>1)
    2588             :   {
    2589           0 :     if (DEBUGLEVEL>3) err_printf("\n");
    2590           0 :     err_printf("BOUND = %.4g\n",BOUND); err_flush();
    2591             :   }
    2592      314205 :   BOUND *= 1 + 1e-6;
    2593      314205 :   k = N; fp->y[N] = fp->z[N] = 0; fp->x[N] = 0;
    2594     1184225 :   for (av = avma;; avma = av, step(fp->x,fp->y,inc,k))
    2595             :   {
    2596             :     GEN R;
    2597             :     long nz;
    2598             :     do
    2599             :     { /* look for primitive element of small norm, cf minim00 */
    2600     2450093 :       int fl = 0;
    2601             :       double p;
    2602     2450093 :       if (k > 1)
    2603             :       {
    2604     1580073 :         long l = k-1;
    2605     1580073 :         fp->z[l] = 0;
    2606     1580073 :         for (j=k; j<=N; j++) fp->z[l] += fp->q[l][j]*fp->x[j];
    2607     1580073 :         p = (double)fp->x[k] + fp->z[k];
    2608     1580073 :         fp->y[l] = fp->y[k] + p*p*fp->v[k];
    2609     1580073 :         if (l <= skipfirst && !fp->y[1]) fl = 1;
    2610     1580073 :         fp->x[l] = (long)floor(-fp->z[l] + 0.5);
    2611     1580073 :         k = l;
    2612             :       }
    2613     1539203 :       for(;; step(fp->x,fp->y,inc,k))
    2614             :       {
    2615     4300722 :         if (++try_elt > maxtry_ELEMENT) return 0;
    2616     3989296 :         if (!fl)
    2617             :         {
    2618     3733369 :           p = (double)fp->x[k] + fp->z[k];
    2619     3733369 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2620             : 
    2621     1880196 :           step(fp->x,fp->y,inc,k);
    2622             : 
    2623     1880196 :           p = (double)fp->x[k] + fp->z[k];
    2624     1880196 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2625             :         }
    2626     1843983 :         fl = 0; inc[k] = 1;
    2627     1843983 :         if (++k > N) return 0;
    2628     1539203 :       }
    2629     2145313 :     } while (k > 1);
    2630             : 
    2631             :     /* element complete */
    2632     1733212 :     if (zv_content(fp->x) !=1) continue; /* not primitive */
    2633      812923 :     gx = ZM_zc_mul(ideal,fp->x);
    2634      812923 :     if (ZV_isscalar(gx)) continue;
    2635      807141 :     if (++try_factor > maxtry_FACT) return 0;
    2636             : 
    2637      807134 :     if (!nbrelpid)
    2638             :     {
    2639          63 :       if (!factorgen(F,nf,ideal0,Nideal,gx,fact))
    2640          49 :          continue;
    2641          14 :       return 1;
    2642             :     }
    2643      807071 :     else if (rr)
    2644             :     {
    2645       38617 :       if (!factorgen(F,nf,ideal0,rr->Nideal,gx,fact))
    2646       35098 :          continue;
    2647        3519 :       add_to_fact(rr->jid, 1, fact);
    2648        3519 :       gx = nfmul(nf, rr->m1, gx);
    2649             :     }
    2650             :     else
    2651             :     {
    2652      768454 :       GEN Nx, xembed = RgM_RgC_mul(M, gx);
    2653             :       long e;
    2654      768454 :       if (nbsmallnorm) (*nbsmallnorm)++;
    2655      768454 :       Nx = grndtoi(embed_norm(xembed, R1), &e);
    2656      768454 :       if (e >= 0) {
    2657           0 :         if (DEBUGLEVEL > 1) { err_printf("+"); err_flush(); }
    2658      336185 :         continue;
    2659             :       }
    2660      768454 :       if (!can_factor(F, nf, NULL, gx, Nx, fact)) continue;
    2661             :     }
    2662             : 
    2663             :     /* smooth element */
    2664      435788 :     R = set_fact(F, fact, rr ? rr->ex : NULL, &nz);
    2665             :     /* make sure we get maximal rank first, then allow all relations */
    2666      435788 :     if (add_rel(cache, F, R, nz, gx, rr ? 1 : 0) <= 0)
    2667             :     { /* probably Q-dependent from previous ones: forget it */
    2668      410971 :       if (DEBUGLEVEL>1) err_printf("*");
    2669      413750 :       if (++dependent > maxtry_DEP) break;
    2670      410131 :       continue;
    2671             :     }
    2672       24817 :     dependent = 0;
    2673       24817 :     if (DEBUGLEVEL && nbfact) (*nbfact)++;
    2674       24817 :     if (cache->last >= cache->end) return 1; /* we have enough */
    2675       18192 :     if (++nbrelideal == nbrelpid) break;
    2676      870020 :   }
    2677        2779 :   return 0;
    2678             : }
    2679             : 
    2680             : static void
    2681       13596 : small_norm(RELCACHE_t *cache, FB_t *F, GEN nf, long nbrelpid, GEN M,
    2682             :            FACT *fact, GEN p0)
    2683             : {
    2684             :   pari_timer T;
    2685       13596 :   const long prec = nf_get_prec(nf);
    2686             :   FP_t fp;
    2687             :   pari_sp av;
    2688       13596 :   GEN G = nf_get_G(nf), L_jid = F->L_jid;
    2689       13596 :   long nbsmallnorm, nbfact, noideal = lg(L_jid);
    2690       13596 :   REL_t *last = cache->last;
    2691             : 
    2692       13596 :   if (DEBUGLEVEL)
    2693             :   {
    2694           0 :     timer_start(&T);
    2695           0 :     err_printf("\n#### Look for %ld relations in %ld ideals (small_norm)\n",
    2696           0 :                cache->end - last, lg(L_jid)-1);
    2697             :   }
    2698       13596 :   nbsmallnorm = nbfact = 0;
    2699             : 
    2700       13596 :   minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2701      311007 :   for (av = avma; --noideal; avma = av)
    2702             :   {
    2703      301700 :     GEN ideal=gel(F->LP,L_jid[noideal]);
    2704             : 
    2705      301700 :     if (DEBUGLEVEL>1)
    2706           0 :       err_printf("\n*** Ideal no %ld: %Ps\n", L_jid[noideal], vecslice(ideal,1,4));
    2707      301700 :     else if (DEBUGLEVEL)
    2708           0 :       err_printf("(%ld) ", L_jid[noideal]);
    2709      301700 :     if (p0)
    2710      286219 :       ideal = idealmul(nf, p0, ideal);
    2711             :     else
    2712       15481 :       ideal = idealhnf_two(nf, ideal);
    2713      301700 :     if (Fincke_Pohst_ideal(cache, F, nf, M, G, ideal, fact,
    2714             :           nbrelpid, &fp, NULL, prec, &nbsmallnorm, &nbfact))
    2715        4289 :       break;
    2716      297411 :     if (DEBUGLEVEL>1) timer_printf(&T, "for this ideal");
    2717             :   }
    2718       13596 :   if (DEBUGLEVEL)
    2719             :   {
    2720           0 :     err_printf("\n");
    2721           0 :     timer_printf(&T, "small norm relations");
    2722           0 :     if (nbsmallnorm && DEBUGLEVEL > 1)
    2723           0 :       err_printf("  nb. fact./nb. small norm = %ld/%ld = %.3f\n",
    2724           0 :                   nbfact,nbsmallnorm,((double)nbfact)/nbsmallnorm);
    2725             :   }
    2726       13596 : }
    2727             : 
    2728             : /* I integral ideal in HNF form */
    2729             : static GEN
    2730        2828 : remove_content(GEN I)
    2731             : {
    2732        2828 :   long N = lg(I)-1;
    2733        2828 :   if (!is_pm1(gcoeff(I,N,N))) I = Q_primpart(I);
    2734        2828 :   return I;
    2735             : }
    2736             : 
    2737             : static GEN
    2738        2828 : get_random_ideal(FB_t *F, GEN nf, GEN ex)
    2739             : {
    2740        2828 :   long l = lg(ex);
    2741             :   for (;;) {
    2742        2828 :     GEN ideal = NULL;
    2743             :     long i;
    2744       12803 :     for (i=1; i<l; i++)
    2745             :     {
    2746        9975 :       long id = F->subFB[i];
    2747        9975 :       ex[i] = random_bits(RANDOM_BITS);
    2748        9975 :       if (ex[i])
    2749             :       {
    2750        9415 :         GEN a = gmael(F->id2,id,ex[i]);
    2751        9415 :         ideal = ideal? idealmul_HNF(nf,ideal, a): idealhnf_two(nf,a);
    2752             :       }
    2753             :     }
    2754        2828 :     if (ideal) { /* ex  != 0 */
    2755        2828 :       ideal = remove_content(ideal);
    2756        5656 :       if (!is_pm1(gcoeff(ideal,1,1))) return ideal; /* ideal != Z_K */
    2757             :     }
    2758           0 :   }
    2759             : }
    2760             : 
    2761             : static void
    2762        2828 : rnd_rel(RELCACHE_t *cache, FB_t *F, GEN nf, FACT *fact)
    2763             : {
    2764             :   pari_timer T;
    2765        2828 :   const GEN L_jid = F->L_jid, M = nf_get_M(nf), G = F->G0;
    2766             :   GEN baseideal;
    2767             :   RNDREL_t rr;
    2768             :   FP_t fp;
    2769        2828 :   const long nbG = lg(F->vecG)-1, lgsub = lg(F->subFB), l_jid = lg(L_jid);
    2770        2828 :   const long prec = nf_get_prec(nf);
    2771             :   long jlist;
    2772             :   pari_sp av;
    2773             : 
    2774             :   /* will compute P[ L_jid[i] ] * (random product from subFB) */
    2775        2828 :   if (DEBUGLEVEL) {
    2776           0 :     timer_start(&T);
    2777           0 :     err_printf("\n#### Look for %ld relations in %ld ideals (rnd_rel)\n",
    2778           0 :                cache->end - cache->last, lg(L_jid)-1);
    2779             :   }
    2780        2828 :   rr.ex = cgetg(lgsub, t_VECSMALL);
    2781        2828 :   baseideal = get_random_ideal(F, nf, rr.ex);
    2782        2828 :   baseideal = red(nf, baseideal, F->G0, &rr.m1);
    2783        2828 :   baseideal = idealhnf_two(nf, baseideal);
    2784        2828 :   minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2785       12983 :   for (av = avma, jlist = 1; jlist < l_jid; jlist++, avma = av)
    2786             :   {
    2787             :     long j;
    2788             :     GEN ideal;
    2789             :     pari_sp av1;
    2790       12491 :     REL_t *last = cache->last;
    2791             : 
    2792       12491 :     rr.jid = L_jid[jlist];
    2793       12491 :     ideal = gel(F->LP,rr.jid);
    2794       12491 :     if (DEBUGLEVEL>1)
    2795           0 :       err_printf("\n*** Ideal no %ld: %Ps\n", rr.jid, vecslice(ideal,1,4));
    2796       12491 :     else if (DEBUGLEVEL)
    2797           0 :       err_printf("(%ld) ", rr.jid);
    2798       12491 :     ideal = idealmul_HNF(nf, baseideal, ideal);
    2799       12491 :     rr.Nideal = ZM_det_triangular(ideal);
    2800       12491 :     if (Fincke_Pohst_ideal(cache, F, nf, M, G, ideal, fact,
    2801             :                            RND_REL_RELPID, &fp, &rr, prec, NULL, NULL))
    2802        2336 :       break;
    2803       10155 :     if (PREVENT_LLL_IN_RND_REL || cache->last != last) continue;
    2804           0 :     for (av1 = avma, j = 1; j <= nbG; j++, avma = av1)
    2805             :     { /* reduce along various directions */
    2806           0 :       GEN m = idealpseudomin_nonscalar(ideal, gel(F->vecG,j));
    2807             :       GEN R;
    2808             :       long nz;
    2809           0 :       if (!factorgen(F,nf,ideal,rr.Nideal,m,fact)) continue;
    2810             :       /* can factor ideal, record relation */
    2811           0 :       add_to_fact(rr.jid, 1, fact);
    2812           0 :       R = set_fact(F, fact, rr.ex, &nz);
    2813           0 :       switch (add_rel(cache, F, R, nz, nfmul(nf, m, rr.m1), 1))
    2814             :       {
    2815             :         case -1: /* forget it */
    2816           0 :           if (DEBUGLEVEL>1) dbg_cancelrel(rr.jid,j,R);
    2817           0 :           continue;
    2818             :       }
    2819           0 :       if (DEBUGLEVEL) timer_printf(&T, "for this relation");
    2820             :       /* Need more, try next prime ideal */
    2821           0 :       if (cache->last < cache->end) break;
    2822             :       /* We have found enough. Return */
    2823        2828 :       avma = av; return;
    2824             :     }
    2825             :   }
    2826        2828 :   if (DEBUGLEVEL)
    2827             :   {
    2828           0 :     err_printf("\n");
    2829           0 :     timer_printf(&T, "for remaining ideals");
    2830             :   }
    2831             : }
    2832             : 
    2833             : /* remark: F->KCZ changes if be_honest() fails */
    2834             : static int
    2835           7 : be_honest(FB_t *F, GEN nf, GEN auts, FACT *fact)
    2836             : {
    2837             :   GEN P, done_by_autom;
    2838             :   long ex, i, j, J, iz, nbtest;
    2839           7 :   long lgsub = lg(F->subFB), KCZ0 = F->KCZ;
    2840           7 :   long N = nf_get_degree(nf), prec = nf_get_prec(nf);
    2841           7 :   GEN M = nf_get_M(nf), G = nf_get_G(nf);
    2842             :   FP_t fp;
    2843             :   pari_sp av;
    2844             : 
    2845           7 :   if (DEBUGLEVEL) {
    2846           0 :     err_printf("Be honest for %ld primes from %ld to %ld\n", F->KCZ2 - F->KCZ,
    2847           0 :                F->FB[ F->KCZ+1 ], F->FB[ F->KCZ2 ]);
    2848             :   }
    2849           7 :   minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2850           7 :   av = avma;
    2851          14 :   for (iz=F->KCZ+1; iz<=F->KCZ2; iz++, avma = av)
    2852             :   {
    2853           7 :     long p = F->FB[iz];
    2854           7 :     P = F->LV[p]; J = lg(P);
    2855             :     /* all P|p in FB + last is unramified --> check all but last */
    2856           7 :     if (isclone(P) && pr_get_e(gel(P,J-1)) == 1) J--;
    2857           7 :     if (DEBUGLEVEL>1) err_printf("%ld ", p);
    2858           7 :     done_by_autom = zero_zv(J);
    2859             : 
    2860          28 :     for (j=1; j<J; j++)
    2861             :     {
    2862          21 :       GEN ideal0 = idealhnf_two(nf,gel(P,j)), ideal = ideal0;
    2863          21 :       GEN gen0 = pr_get_gen(gel(P,j));
    2864          21 :       pari_sp av2 = avma;
    2865          21 :       if (done_by_autom[j]) continue;
    2866          28 :       for (i = 1; i < lg(auts); i++)
    2867             :       {
    2868          14 :         GEN gen = gmul(gel(auts,i), gen0);
    2869             :         long k;
    2870          28 :         for (k = j; k < J; k++)
    2871          21 :           if (ZC_prdvd(nf, gen, gel(P, k))) { done_by_autom[k] = 1; break; }
    2872             :       }
    2873          14 :       for(nbtest=0;;)
    2874             :       {
    2875          14 :         if (Fincke_Pohst_ideal(NULL, F, nf, M, G, ideal, fact, 0, &fp,
    2876             :               NULL, prec, NULL, NULL))
    2877          14 :           break;
    2878           0 :         avma = av2;
    2879           0 :         if (++nbtest > maxtry_HONEST)
    2880             :         {
    2881           0 :           if (DEBUGLEVEL)
    2882           0 :             pari_warn(warner,"be_honest() failure on prime %Ps\n", P[j]);
    2883           0 :           return 0;
    2884             :         }
    2885           0 :         ideal = ideal0;
    2886           0 :         if (F->newpow) powFBgen(NULL, F, nf, auts);
    2887           0 :         for (i=1; i<lgsub; i++)
    2888             :         {
    2889           0 :           long id = F->subFB[i];
    2890           0 :           ex = random_bits(RANDOM_BITS);
    2891           0 :           if (ex) ideal = idealmul_HNF(nf,ideal, gmael(F->id2,id,ex));
    2892             :         }
    2893           0 :         ideal = remove_content(ideal);
    2894           0 :       }
    2895          14 :       avma = av2;
    2896             :     }
    2897           7 :     F->KCZ++; /* SUCCESS, "enlarge" factorbase */
    2898             :   }
    2899           7 :   F->KCZ = KCZ0; avma = av; return 1;
    2900             : }
    2901             : 
    2902             : /* A t_MAT of complex floats, in fact reals. Extract a submatrix B
    2903             :  * whose columns are definitely non-0, i.e. gexpo(A[j]) >= -2
    2904             :  *
    2905             :  * If possible precision problem (t_REAL 0 with large exponent), set
    2906             :  * *precpb to 1 */
    2907             : static GEN
    2908        7366 : clean_cols(GEN A, int *precpb)
    2909             : {
    2910        7366 :   long l = lg(A), h, i, j, k;
    2911             :   GEN B;
    2912        7366 :   *precpb = 0;
    2913        7366 :   if (l == 1) return A;
    2914        7366 :   h = lgcols(A);;
    2915        7366 :   B = cgetg(l, t_MAT);
    2916      854590 :   for (i = k = 1; i < l; i++)
    2917             :   {
    2918      847224 :     GEN Ai = gel(A,i);
    2919      847224 :     int non0 = 0;
    2920     4260501 :     for (j = 1; j < h; j++)
    2921             :     {
    2922     3413277 :       GEN c = gel(Ai,j);
    2923     3413277 :       if (gexpo(c) >= -2)
    2924             :       {
    2925     3261901 :         if (gequal0(c)) *precpb = 1; else non0 = 1;
    2926             :       }
    2927             :     }
    2928      847224 :     if (non0) gel(B, k++) = Ai;
    2929             :   }
    2930        7366 :   setlg(B, k); return B;
    2931             : }
    2932             : 
    2933             : static long
    2934      821664 : compute_multiple_of_R_pivot(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2935             : {
    2936      821664 :   GEN x = gel(X,ix);
    2937      821664 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2938             :   (void)x0;
    2939     4143120 :   for (i=1; i<lx; i++)
    2940     3321456 :     if (!c[i] && !gequal0(gel(x,i)))
    2941             :     {
    2942      847831 :       long e = gexpo(gel(x,i));
    2943      847831 :       if (e > ex) { ex = e; k = i; }
    2944             :     }
    2945      821664 :   return (k && ex > -32)? k: lx;
    2946             : }
    2947             : 
    2948             : /* A = complex logarithmic embeddings of units (u_j) found so far,
    2949             :  * RU = R1+R2 = unit rank, N = field degree
    2950             :  * need = unit rank defect
    2951             :  * L = NULL (prec problem) or B^(-1) * A with approximate rational entries
    2952             :  * (as t_REAL), B a submatrix of A, with (probably) maximal rank RU */
    2953             : static GEN
    2954        7702 : compute_multiple_of_R(GEN A, long RU, long N, long *pneed, GEN *ptL)
    2955             : {
    2956             :   GEN T, d, mdet, Im_mdet, kR, xreal, L;
    2957        7702 :   long i, j, r, R1 = 2*RU - N;
    2958             :   int precpb;
    2959        7702 :   pari_sp av = avma;
    2960             : 
    2961        7702 :   if (RU == 1) { *ptL = zeromat(0, lg(A)-1); return gen_1; }
    2962             : 
    2963        7366 :   if (DEBUGLEVEL) err_printf("\n#### Computing regulator multiple\n");
    2964        7366 :   xreal = real_i(A); /* = (log |sigma_i(u_j)|) */
    2965        7366 :   mdet = clean_cols(xreal, &precpb);
    2966             :   /* will cause precision to increase on later failure, but we may succeed! */
    2967        7366 :   *ptL = precpb? NULL: gen_1;
    2968        7366 :   T = cgetg(RU+1,t_COL);
    2969        7366 :   for (i=1; i<=R1; i++) gel(T,i) = gen_1;
    2970        7366 :   for (   ; i<=RU; i++) gel(T,i) = gen_2;
    2971        7366 :   mdet = shallowconcat(T, mdet); /* det(Span(mdet)) = N * R */
    2972             : 
    2973             :   /* could be using indexrank(), but need custom "get_pivot" function */
    2974        7366 :   d = RgM_pivots(mdet, NULL, &r, &compute_multiple_of_R_pivot);
    2975             :   /* # of independent columns == unit rank ? */
    2976        7366 :   if (lg(mdet)-1 - r != RU)
    2977             :   {
    2978        4498 :     if (DEBUGLEVEL)
    2979           0 :       err_printf("Unit group rank = %ld < %ld\n",lg(mdet)-1 - r, RU);
    2980        4498 :     *pneed = RU - (lg(mdet)-1-r);
    2981        4498 :     avma = av; return NULL;
    2982             :   }
    2983             : 
    2984        2868 :   Im_mdet = cgetg(RU+1, t_MAT); /* extract independent columns */
    2985             :   /* N.B: d[1] = 1, corresponding to T above */
    2986        2868 :   gel(Im_mdet, 1) = T;
    2987       38372 :   for (i = j = 2; i <= RU; j++)
    2988       35504 :     if (d[j]) gel(Im_mdet, i++) = gel(mdet,j);
    2989             : 
    2990             :   /* integral multiple of R: the cols we picked form a Q-basis, they have an
    2991             :    * index in the full lattice. First column is T */
    2992        2868 :   kR = divru(det2(Im_mdet), N);
    2993             :   /* R > 0.2 uniformly */
    2994        2868 :   if (!signe(kR) || expo(kR) < -3) { avma=av; *pneed = 0; return NULL; }
    2995             : 
    2996        2868 :   setabssign(kR);
    2997        2868 :   L = RgM_inv(Im_mdet);
    2998        2868 :   if (!L) { *ptL = NULL; return kR; }
    2999             : 
    3000        2868 :   L = rowslice(L, 2, RU); /* remove first line */
    3001        2868 :   L = RgM_mul(L, xreal); /* approximate rational entries */
    3002        2868 :   gerepileall(av,2, &L, &kR);
    3003        2868 :   *ptL = L; return kR;
    3004             : }
    3005             : 
    3006             : static GEN
    3007        3204 : bestappr_noer(GEN x, GEN k)
    3008             : {
    3009             :   GEN y;
    3010        3204 :   pari_CATCH(e_PREC) { y = NULL; }
    3011        3204 :   pari_TRY { y = bestappr(x,k); } pari_ENDCATCH;
    3012        3204 :   return y;
    3013             : }
    3014             : 
    3015             : /* Input:
    3016             :  * lambda = approximate rational entries: coords of units found so far on a
    3017             :  * sublattice of maximal rank (sublambda)
    3018             :  * *ptkR = regulator of sublambda = multiple of regulator of lambda
    3019             :  * Compute R = true regulator of lambda.
    3020             :  *
    3021             :  * If c := Rz ~ 1, by Dirichlet's formula, then lambda is the full group of
    3022             :  * units AND the full set of relations for the class group has been computed.
    3023             :  *
    3024             :  * In fact z is a very rough approximation and we only expect 0.75 < Rz < 1.3
    3025             :  *
    3026             :  * Output: *ptkR = R, *ptU = basis of fundamental units (in terms lambda) */
    3027             : static int
    3028        3204 : compute_R(GEN lambda, GEN z, GEN *ptL, GEN *ptkR, pari_timer *T)
    3029             : {
    3030        3204 :   pari_sp av = avma;
    3031             :   long r, ec;
    3032             :   GEN L, H, D, den, R, c;
    3033             : 
    3034        3204 :   if (DEBUGLEVEL) { err_printf("\n#### Computing check\n"); err_flush(); }
    3035        3204 :   D = gmul2n(mpmul(*ptkR,z), 1); /* bound for denom(lambda) */
    3036        3204 :   if (expo(D) < 0 && rtodbl(D) < 0.95) return fupb_PRECI;
    3037        3204 :   lambda = bestappr_noer(lambda,D);
    3038        3204 :   if (!lambda)
    3039             :   {
    3040           0 :     if (DEBUGLEVEL) err_printf("truncation error in bestappr\n");
    3041           0 :     return fupb_PRECI;
    3042             :   }
    3043        3204 :   den = Q_denom(lambda);
    3044        3204 :   if (mpcmp(den,D) > 0)
    3045             :   {
    3046          12 :     if (DEBUGLEVEL) err_printf("D = %Ps\nden = %Ps\n",D,
    3047           0 :                     lgefint(den) <= DEFAULTPREC? den: itor(den,LOWDEFAULTPREC));
    3048          12 :     return fupb_PRECI;
    3049             :   }
    3050        3192 :   L = Q_muli_to_int(lambda, den);
    3051        3192 :   H = ZM_hnf(L);
    3052        3192 :   r = lg(H)-1;
    3053        3192 :   if (r && r != nbrows(H))
    3054           1 :     R = gen_0; /* wrong rank */
    3055             :   else
    3056        3191 :     R = gmul(*ptkR, gdiv(ZM_det_triangular(H), powiu(den, r)));
    3057             :   /* R = tentative regulator; regulator > 0.2 uniformly */
    3058        3192 :   if (gexpo(R) < -3) {
    3059           1 :     if (DEBUGLEVEL)
    3060             :     {
    3061           0 :       err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3062           0 :       timer_printf(T, "computing check");
    3063             :     }
    3064           1 :     avma = av; return fupb_PRECI;
    3065             :   }
    3066        3191 :   c = gmul(R,z); /* should be n (= 1 if we are done) */
    3067        3191 :   if (DEBUGLEVEL)
    3068             :   {
    3069           0 :     err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3070           0 :     err_printf("\n ***** check = %.28Pg\n",c);
    3071           0 :     timer_printf(T, "computing check");
    3072             :   }
    3073        3191 :   ec = gexpo(c);
    3074             :   /* safe check for c < 0.75 : avoid underflow in gtodouble() */
    3075        3191 :   if (ec < -1 || (ec == -1 && gtodouble(c) < 0.75)) {
    3076           0 :     avma = av; return fupb_PRECI;
    3077             :   }
    3078             :   /* safe check for c > 1.3 : avoid overflow */
    3079        3191 :   if (ec > 0 || (ec == 0 && gtodouble(c) > 1.3)) {
    3080        1415 :     avma = av; return fupb_RELAT;
    3081             :   }
    3082        1776 :   *ptkR = R; *ptL = L; return fupb_NONE;
    3083             : }
    3084             : 
    3085             : /* norm of an extended ideal I, whose 1st component is in integral HNF */
    3086             : static GEN
    3087        4473 : idnorm(GEN I) { return ZM_det_triangular(gel(I,1)); }
    3088             : 
    3089             : /* find the smallest (wrt norm) among I, I^-1 and red(I^-1) */
    3090             : static GEN
    3091        1491 : inverse_if_smaller(GEN nf, GEN I)
    3092             : {
    3093             :   GEN d, dmin, I1;
    3094             : 
    3095        1491 :   dmin = idnorm(I);
    3096        1491 :   I1 = idealinv(nf,I); gel(I1,1) = Q_remove_denom(gel(I1,1), NULL);
    3097        1491 :   d = idnorm(I1); if (cmpii(d,dmin) < 0) {I=I1; dmin=d;}
    3098             :   /* try reducing (often _increases_ the norm) */
    3099        1491 :   I1 = idealred(nf,I1);
    3100        1491 :   d = idnorm(I1); if (cmpii(d,dmin) < 0) I=I1;
    3101        1491 :   return I;
    3102             : }
    3103             : 
    3104             : /* in place */
    3105             : static void
    3106         190 : neg_row(GEN U, long i)
    3107             : {
    3108         190 :   GEN c = U + lg(U)-1;
    3109         190 :   for (; c>U; c--) gcoeff(c,i,0) = negi(gcoeff(c,i,0));
    3110         190 : }
    3111             : 
    3112             : static void
    3113         162 : setlg_col(GEN U, long l)
    3114             : {
    3115         162 :   GEN c = U + lg(U)-1;
    3116         162 :   for (; c>U; c--) setlg(*c, l);
    3117         162 : }
    3118             : 
    3119             : /* compute class group (clg1) + data for isprincipal (clg2) */
    3120             : static void
    3121        1748 : class_group_gen(GEN nf,GEN W,GEN C,GEN Vbase,long prec, GEN nf0,
    3122             :                 GEN *ptclg1,GEN *ptclg2)
    3123             : {
    3124             :   pari_timer T;
    3125             :   GEN z,G,Ga,ga,GD,cyc,X,Y,D,U,V,Ur,Ui,Uir,I,J,arch;
    3126             :   long i,j,lo,lo0;
    3127             : 
    3128        1748 :   if (DEBUGLEVEL) timer_start(&T);
    3129        1748 :   D = ZM_snfall(W,&U,&V); /* UWV = D, D diagonal, G = g Ui (G=new gens, g=old) */
    3130        1748 :   Ui = RgM_inv(U);
    3131        1748 :   lo0 = lo = lg(D);
    3132             :  /* we could set lo = lg(cyc) and truncate all matrices below
    3133             :   *   setlg_col(D && U && Y, lo) + setlg(D && V && X && Ui, lo)
    3134             :   * but it's not worth the complication:
    3135             :   * 1) gain is negligible (avoid computing z^0 if lo < lo0)
    3136             :   * 2) when computing ga, the products XU and VY use the original matrices
    3137             :   */
    3138        1748 :   Ur  = ZM_hnfdivrem(U, D, &Y);
    3139        1748 :   Uir = ZM_hnfdivrem(Ui,W, &X);
    3140             :  /* [x] = logarithmic embedding of x (arch. component)
    3141             :   * NB: z = idealred(I) --> I = y z[1], with [y] = - z[2]
    3142             :   * P invertible diagonal matrix (\pm 1) which is only implicitly defined
    3143             :   * G = g Uir P + [Ga],  Uir = Ui + WX
    3144             :   * g = G P Ur  + [ga],  Ur  = U + DY */
    3145        1748 :   G = cgetg(lo,t_VEC);
    3146        1748 :   Ga= cgetg(lo,t_VEC);
    3147        1748 :   z = init_famat(NULL);
    3148        1748 :   if (!nf0) nf0 = nf;
    3149        3239 :   for (j=1; j<lo; j++)
    3150             :   {
    3151        1491 :     GEN p1 = gcoeff(Uir,1,j);
    3152        1491 :     z[1]=Vbase[1]; I = idealpowred(nf0,z,p1);
    3153        2883 :     for (i=2; i<lo0; i++)
    3154             :     {
    3155        1392 :       p1 = gcoeff(Uir,i,j);
    3156        1392 :       if (signe(p1))
    3157             :       {
    3158         582 :         z[1]=Vbase[i];
    3159         582 :         I = extideal_HNF_mul(nf0, I, idealpowred(nf0,z,p1));
    3160         582 :         I = idealred(nf0,I);
    3161             :       }
    3162             :     }
    3163        1491 :     J = inverse_if_smaller(nf0, I);
    3164        1491 :     if (J != I)
    3165             :     { /* update wrt P */
    3166          95 :       neg_row(Y ,j); gel(V,j) = ZC_neg(gel(V,j));
    3167          95 :       neg_row(Ur,j); gel(X,j) = ZC_neg(gel(X,j));
    3168             :     }
    3169        1491 :     G[j] = J[1]; /* generator, order cyc[j] */
    3170        1491 :     arch = famat_to_arch(nf, gel(J,2), prec);
    3171        1491 :     if (!arch) pari_err_PREC("class_group_gen");
    3172        1491 :     gel(Ga,j) = gneg(arch);
    3173             :   }
    3174             :   /* at this point Y = PY, Ur = PUr, V = VP, X = XP */
    3175             : 
    3176             :   /* G D =: [GD] = g (UiP + W XP) D + [Ga]D = g W (VP + XP D) + [Ga]D
    3177             :    * NB: DP = PD and Ui D = W V. gW is given by (first lo0-1 cols of) C
    3178             :    */
    3179        1748 :   GD = gadd(act_arch(ZM_add(V, ZM_mul(X,D)), C),
    3180             :             act_arch(D, Ga));
    3181             :   /* -[ga] = [GD]PY + G PU - g = [GD]PY + [Ga] PU + gW XP PU
    3182             :                                = gW (XP PUr + VP PY) + [Ga]PUr */
    3183        1748 :   ga = gadd(act_arch(ZM_add(ZM_mul(X,Ur), ZM_mul(V,Y)), C),
    3184             :             act_arch(Ur, Ga));
    3185        1748 :   ga = gneg(ga);
    3186             :   /* TODO: could (LLL)reduce ga and GD mod units ? */
    3187             : 
    3188        1748 :   cyc = cgetg(lo,t_VEC); /* elementary divisors */
    3189        3077 :   for (j=1; j<lo; j++)
    3190             :   {
    3191        1491 :     gel(cyc,j) = gcoeff(D,j,j);
    3192        1491 :     if (gequal1(gel(cyc,j)))
    3193             :     { /* strip useless components */
    3194         162 :       lo = j; setlg(cyc,lo); setlg_col(Ur,lo);
    3195         162 :       setlg(G,lo); setlg(Ga,lo); setlg(GD,lo); break;
    3196             :     }
    3197             :   }
    3198        1748 :   *ptclg1 = mkvec3(ZM_det_triangular(W), cyc, G);
    3199        1748 :   *ptclg2 = mkvec3(Ur, ga,GD);
    3200        1748 :   if (DEBUGLEVEL) timer_printf(&T, "classgroup generators");
    3201        1748 : }
    3202             : 
    3203             : /* SMALLBUCHINIT */
    3204             : 
    3205             : static GEN
    3206          49 : decodeprime(GEN T, GEN L, long n)
    3207             : {
    3208          49 :   long t = itos(T);
    3209          49 :   return gmael(L, t/n, t%n + 1);
    3210             : }
    3211             : static GEN
    3212          49 : codeprime(GEN L, long N, GEN pr)
    3213             : {
    3214          49 :   long p = pr_get_smallp(pr);
    3215          49 :   return utoipos( N*p + pr_index(gel(L,p), pr)-1 );
    3216             : }
    3217             : 
    3218             : static GEN
    3219           7 : decode_pr_lists(GEN nf, GEN pfc)
    3220             : {
    3221           7 :   long i, n = nf_get_degree(nf), l = lg(pfc);
    3222           7 :   GEN L, P = cgetg(l, t_VECSMALL), Vbase = cgetg(l, t_COL);
    3223             : 
    3224           7 :   for (i = 1; i < l; i++) P[i] = itou(gel(pfc,i)) / n;
    3225           7 :   L = const_vec(vecsmall_max(P), NULL);
    3226          56 :   for (i = 1; i < l; i++)
    3227             :   {
    3228          49 :     long p = P[i];
    3229          49 :     if (!gel(L,p)) gel(L,p) = idealprimedec(nf, utoipos(p));
    3230             :   }
    3231           7 :   for (i = 1; i < l; i++) gel(Vbase,i) = decodeprime(gel(pfc,i), L, n);
    3232           7 :   return Vbase;
    3233             : }
    3234             : 
    3235             : static GEN
    3236           7 : codeprimes(GEN Vbase, long N)
    3237             : {
    3238           7 :   GEN v, L = get_pr_lists(Vbase, N, 1);
    3239           7 :   long i, l = lg(Vbase);
    3240           7 :   v = cgetg(l, t_VEC);
    3241           7 :   for (i=1; i<l; i++) gel(v,i) = codeprime(L, N, gel(Vbase,i));
    3242           7 :   return v;
    3243             : }
    3244             : 
    3245             : /* compute principal ideals corresponding to (gen[i]^cyc[i]) */
    3246             : static GEN
    3247        1463 : makecycgen(GEN bnf)
    3248             : {
    3249             :   GEN cyc,gen,h,nf,y,GD;
    3250             :   long e,i,l;
    3251             : 
    3252        1463 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building cycgen)");
    3253        1463 :   nf = bnf_get_nf(bnf);
    3254        1463 :   cyc = bnf_get_cyc(bnf);
    3255        1463 :   gen = bnf_get_gen(bnf); GD = gmael(bnf,9,3);
    3256        1463 :   h = cgetg_copy(gen, &l);
    3257        2408 :   for (i=1; i<l; i++)
    3258             :   {
    3259         945 :     GEN gi = gel(gen,i), ci = gel(cyc,i);
    3260         945 :     if (abscmpiu(ci, 5) < 0)
    3261             :     {
    3262         644 :       GEN N = ZM_det_triangular(gi);
    3263         644 :       y = isprincipalarch(bnf,gel(GD,i), N, ci, gen_1, &e);
    3264         644 :       if (y && fact_ok(nf,y,NULL,mkvec(gi),mkvec(ci)))
    3265             :       {
    3266         644 :         gel(h,i) = to_famat_shallow(y,gen_1);
    3267         644 :         continue;
    3268             :       }
    3269             :     }
    3270         301 :     y = isprincipalfact(bnf, NULL, mkvec(gi), mkvec(ci), nf_GENMAT|nf_FORCE);
    3271         301 :     h[i] = y[2];
    3272             :   }
    3273        1463 :   return h;
    3274             : }
    3275             : 
    3276             : static GEN
    3277        1544 : get_y(GEN bnf, GEN W, GEN B, GEN C, GEN pFB, long j)
    3278             : {
    3279        1544 :   GEN y, nf  = bnf_get_nf(bnf);
    3280        1544 :   long e, lW = lg(W)-1;
    3281        1544 :   GEN ex = (j<=lW)? gel(W,j): gel(B,j-lW);
    3282        1544 :   GEN P = (j<=lW)? NULL: gel(pFB,j);
    3283        1544 :   if (C)
    3284             :   { /* archimedean embeddings known: cheap trial */
    3285        1530 :     GEN Nx = get_norm_fact_primes(pFB, ex, P);
    3286        1530 :     y = isprincipalarch(bnf,gel(C,j), Nx,gen_1, gen_1, &e);
    3287        1530 :     if (y && fact_ok(nf,y,P,pFB,ex)) return y;
    3288             :   }
    3289          51 :   y = isprincipalfact_or_fail(bnf, P, pFB, ex);
    3290          51 :   return typ(y) == t_INT? y: gel(y,2);
    3291             : }
    3292             : /* compute principal ideals corresponding to bnf relations */
    3293             : static GEN
    3294          34 : makematal(GEN bnf)
    3295             : {
    3296          34 :   GEN W = bnf_get_W(bnf), B = bnf_get_B(bnf), C = bnf_get_C(bnf);
    3297             :   GEN pFB, ma, retry;
    3298          34 :   long lma, j, prec = 0;
    3299             : 
    3300          34 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building matal)");
    3301          34 :   lma=lg(W)+lg(B)-1;
    3302          34 :   pFB = bnf_get_vbase(bnf);
    3303          34 :   ma = cgetg(lma,t_VEC);
    3304          34 :   retry = vecsmalltrunc_init(lma);
    3305        1564 :   for (j=lma-1; j>0; j--)
    3306             :   {
    3307        1530 :     pari_sp av = avma;
    3308        1530 :     GEN y = get_y(bnf, W, B, C, pFB, j);
    3309        1530 :     if (typ(y) == t_INT)
    3310             :     {
    3311          14 :       long E = itos(y);
    3312          14 :       if (DEBUGLEVEL>1) err_printf("\n%ld done later at prec %ld\n",j,E);
    3313          14 :       avma = av;
    3314          14 :       vecsmalltrunc_append(retry, j);
    3315          14 :       if (E > prec) prec = E;
    3316             :     }
    3317             :     else
    3318             :     {
    3319        1516 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3320        1516 :       gel(ma,j) = gerepileupto(av,y);
    3321             :     }
    3322             :   }
    3323          34 :   if (prec)
    3324             :   {
    3325          13 :     long k, l = lg(retry);
    3326          13 :     GEN y, nf = bnf_get_nf(bnf);
    3327          13 :     if (DEBUGLEVEL) pari_warn(warnprec,"makematal",prec);
    3328          13 :     nf = nfnewprec_shallow(nf,prec);
    3329          13 :     bnf = Buchall(nf, nf_FORCE, prec);
    3330          13 :     if (DEBUGLEVEL) err_printf("makematal, adding missing entries:");
    3331          27 :     for (k=1; k<l; k++)
    3332             :     {
    3333          14 :       pari_sp av = avma;
    3334          14 :       long j = retry[k];
    3335          14 :       y = get_y(bnf,W,B,NULL, pFB, j);
    3336          14 :       if (typ(y) == t_INT) pari_err_PREC("makematal");
    3337          14 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3338          14 :       gel(ma,j) = gerepileupto(av,y);
    3339             :     }
    3340             :   }
    3341          34 :   if (DEBUGLEVEL>1) err_printf("\n");
    3342          34 :   return ma;
    3343             : }
    3344             : 
    3345             : enum { MATAL = 1, CYCGEN, UNITS };
    3346             : 
    3347             : GEN
    3348        5754 : bnf_build_cycgen(GEN bnf)
    3349        5754 : { return obj_checkbuild(bnf, CYCGEN, &makecycgen); }
    3350             : GEN
    3351          41 : bnf_build_matalpha(GEN bnf)
    3352          41 : { return obj_checkbuild(bnf, MATAL, &makematal); }
    3353             : GEN
    3354        5963 : bnf_build_units(GEN bnf)
    3355        5963 : { return obj_checkbuild(bnf, UNITS, &makeunits); }
    3356             : 
    3357             : static GEN
    3358          41 : get_regulator(GEN mun)
    3359             : {
    3360          41 :   pari_sp av = avma;
    3361             :   GEN R;
    3362             : 
    3363          41 :   if (lg(mun) == 1) return gen_1;
    3364          41 :   R = det( rowslice(real_i(mun), 1, lgcols(mun)-2) );
    3365          41 :   setabssign(R); return gerepileuptoleaf(av, R);
    3366             : }
    3367             : 
    3368             : /* return corrected archimedian components for elts of x (vector)
    3369             :  * (= log(sigma_i(x)) - log(|Nx|) / [K:Q]) */
    3370             : static GEN
    3371         106 : get_archclean(GEN nf, GEN x, long prec, int units)
    3372             : {
    3373         106 :   long k,N, la = lg(x);
    3374         106 :   GEN M = cgetg(la,t_MAT);
    3375             : 
    3376         106 :   if (la == 1) return M;
    3377         106 :   N = nf_get_degree(nf);
    3378        2069 :   for (k=1; k<la; k++)
    3379             :   {
    3380        1987 :     pari_sp av = avma;
    3381        1987 :     GEN c = get_arch(nf, gel(x,k), prec);
    3382        1987 :     if (!c) return NULL;
    3383        1963 :     if (!units) {
    3384        1892 :       c = cleanarch(c, N, prec);
    3385        1892 :       if (!c) return NULL;
    3386             :     }
    3387        1963 :     settyp(c,t_COL);
    3388        1963 :     gel(M,k) = gerepilecopy(av, c);
    3389             :   }
    3390          82 :   return M;
    3391             : }
    3392             : 
    3393             : static void
    3394          34 : my_class_group_gen(GEN bnf, long prec, GEN nf0, GEN *ptcl, GEN *ptcl2)
    3395             : {
    3396          34 :   GEN W = bnf_get_W(bnf), C = bnf_get_C(bnf), nf = bnf_get_nf(bnf);
    3397          34 :   class_group_gen(nf,W,C,bnf_get_vbase(bnf),prec,nf0, ptcl,ptcl2);
    3398          34 : }
    3399             : 
    3400             : GEN
    3401          34 : bnfnewprec_shallow(GEN bnf, long prec)
    3402             : {
    3403          34 :   GEN nf0 = bnf_get_nf(bnf), nf, res, fu, mun, gac, matal, clgp, clgp2, y;
    3404             :   long r1, r2, prec1;
    3405             : 
    3406          34 :   nf_get_sign(nf0, &r1, &r2);
    3407          34 :   fu = bnf_build_units(bnf);
    3408          34 :   fu = vecslice(fu, 2, lg(fu)-1);
    3409             : 
    3410          34 :   prec1 = prec;
    3411          34 :   if (r1 + r2 > 1) {
    3412          34 :     long e = gexpo(bnf_get_logfu(bnf)) + 1 - TWOPOTBITS_IN_LONG;
    3413          34 :     if (e >= 0) prec += nbits2extraprec(e);
    3414             :   }
    3415          34 :   if (DEBUGLEVEL && prec1!=prec) pari_warn(warnprec,"bnfnewprec",prec);
    3416          34 :   matal = bnf_build_matalpha(bnf);
    3417             :   for(;;)
    3418             :   {
    3419          58 :     pari_sp av = avma;
    3420          58 :     nf = nfnewprec_shallow(nf0,prec);
    3421          58 :     mun = get_archclean(nf, fu, prec, 1);
    3422          58 :     if (mun)
    3423             :     {
    3424          34 :       gac = get_archclean(nf, matal, prec, 0);
    3425          34 :       if (gac) break;
    3426             :     }
    3427          24 :     avma = av; prec = precdbl(prec);
    3428          24 :     if (DEBUGLEVEL) pari_warn(warnprec,"bnfnewprec(extra)",prec);
    3429          24 :   }
    3430          34 :   y = leafcopy(bnf);
    3431          34 :   gel(y,3) = mun;
    3432          34 :   gel(y,4) = gac;
    3433          34 :   gel(y,7) = nf;
    3434          34 :   my_class_group_gen(y,prec,nf0, &clgp,&clgp2);
    3435          34 :   res = leafcopy(gel(bnf,8));
    3436          34 :   gel(res,1) = clgp;
    3437          34 :   gel(res,2) = get_regulator(mun);
    3438          34 :   gel(y,8) = res;
    3439          34 :   gel(y,9) = clgp2; return y;
    3440             : }
    3441             : GEN
    3442          14 : bnfnewprec(GEN bnf, long prec)
    3443             : {
    3444          14 :   pari_sp av = avma;
    3445          14 :   return gerepilecopy(av, bnfnewprec_shallow(checkbnf(bnf), prec));
    3446             : }
    3447             : 
    3448             : GEN
    3449           0 : bnrnewprec_shallow(GEN bnr, long prec)
    3450             : {
    3451           0 :   GEN y = cgetg(7,t_VEC);
    3452             :   long i;
    3453           0 :   gel(y,1) = bnfnewprec_shallow(bnr_get_bnf(bnr), prec);
    3454           0 :   for (i=2; i<7; i++) gel(y,i) = gel(bnr,i);
    3455           0 :   return y;
    3456             : }
    3457             : GEN
    3458           7 : bnrnewprec(GEN bnr, long prec)
    3459             : {
    3460           7 :   GEN y = cgetg(7,t_VEC);
    3461             :   long i;
    3462           7 :   checkbnr(bnr);
    3463           7 :   gel(y,1) = bnfnewprec(bnr_get_bnf(bnr), prec);
    3464           7 :   for (i=2; i<7; i++) gel(y,i) = gcopy(gel(bnr,i));
    3465           7 :   return y;
    3466             : }
    3467             : 
    3468             : static void
    3469           7 : nfbasic_from_sbnf(GEN sbnf, nfbasic_t *T)
    3470             : {
    3471           7 :   T->x0 = T->x = gel(sbnf,1);
    3472           7 :   T->dK   = gel(sbnf,3);
    3473           7 :   T->bas  = gel(sbnf,4);
    3474           7 :   T->index= get_nfindex(T->bas);
    3475           7 :   T->r1   = itos(gel(sbnf,2));
    3476           7 :   T->dx   = NULL;
    3477           7 :   T->dKP  = NULL;
    3478           7 :   T->basden = NULL;
    3479           7 : }
    3480             : 
    3481             : static GEN
    3482        2141 : get_clfu(GEN clgp, GEN reg, GEN zu, GEN fu)
    3483             : {
    3484        2141 :   if (!fu) fu = cgetg(1,t_MAT);
    3485        2141 :   return mkvec5(clgp, reg, gen_1/*DUMMY*/, zu, fu);
    3486             : }
    3487             : 
    3488             : static GEN
    3489        2141 : buchall_end(GEN nf,GEN res, GEN clg2, GEN W, GEN B, GEN A, GEN C,GEN Vbase)
    3490             : {
    3491        2141 :   GEN z = obj_init(9, 3);
    3492        2141 :   gel(z,1) = W;
    3493        2141 :   gel(z,2) = B;
    3494        2141 :   gel(z,3) = A;
    3495        2141 :   gel(z,4) = C;
    3496        2141 :   gel(z,5) = Vbase;
    3497        2141 :   gel(z,6) = gen_0;
    3498        2141 :   gel(z,7) = nf;
    3499        2141 :   gel(z,8) = res;
    3500        2141 :   gel(z,9) = clg2;
    3501        2141 :   return z;
    3502             : }
    3503             : 
    3504             : /* FIXME: obsolete function */
    3505             : GEN
    3506           7 : bnfcompress(GEN bnf)
    3507             : {
    3508           7 :   pari_sp av = avma;
    3509           7 :   GEN nf, fu, y = cgetg(13,t_VEC);
    3510             : 
    3511           7 :   bnf = checkbnf(bnf);
    3512           7 :   nf = bnf_get_nf(bnf);
    3513           7 :   gel(y,1) = nf_get_pol(nf);
    3514           7 :   gel(y,2) = gmael(nf,2,1);
    3515           7 :   gel(y,3) = nf_get_disc(nf);
    3516           7 :   gel(y,4) = nf_get_zk(nf);
    3517           7 :   gel(y,5) = nf_get_roots(nf);
    3518           7 :   gel(y,6) = gen_0; /* FIXME: unused */
    3519           7 :   gel(y,7) = bnf_get_W(bnf);
    3520           7 :   gel(y,8) = bnf_get_B(bnf);
    3521           7 :   gel(y,9) = codeprimes(bnf_get_vbase(bnf), nf_get_degree(nf));
    3522           7 :   gel(y,10) = mkvec2(utoipos(bnf_get_tuN(bnf)),
    3523             :                      nf_to_scalar_or_basis(nf, bnf_get_tuU(bnf)));
    3524           7 :   fu = bnf_build_units(bnf); fu = vecslice(fu,2,lg(fu)-1);
    3525           7 :   gel(y,11) = fu;
    3526           7 :   gel(y,12) = bnf_build_matalpha(bnf);
    3527           7 :   return gerepilecopy(av, y);
    3528             : }
    3529             : 
    3530             : /* FIXME: obsolete feature */
    3531             : static GEN
    3532           7 : sbnf2bnf(GEN sbnf, long prec)
    3533             : {
    3534           7 :   pari_sp av = avma;
    3535             :   GEN ro, nf, A, fu, FU, C, clgp, clgp2, res, y, W, zu, matal, Vbase;
    3536             :   long k, l;
    3537             :   nfbasic_t T;
    3538             : 
    3539           7 :   if (typ(sbnf) != t_VEC || lg(sbnf) != 13) pari_err_TYPE("bnfmake",sbnf);
    3540           7 :   if (prec < DEFAULTPREC) prec = DEFAULTPREC;
    3541             : 
    3542           7 :   nfbasic_from_sbnf(sbnf, &T);
    3543           7 :   ro = gel(sbnf,5); if (prec > gprecision(ro)) ro = NULL;
    3544           7 :   nf = nfbasic_to_nf(&T, ro, prec);
    3545             : 
    3546           7 :   fu = gel(sbnf,11);
    3547           7 :   A = get_archclean(nf, fu, prec, 1);
    3548           7 :   if (!A) pari_err_PREC("bnfmake");
    3549             : 
    3550           7 :   prec = nf_get_prec(nf);
    3551           7 :   matal = gel(sbnf,12);
    3552           7 :   C = get_archclean(nf,matal,prec,0);
    3553           7 :   if (!C) pari_err_PREC("bnfmake");
    3554             : 
    3555           7 :   Vbase = decode_pr_lists(nf, gel(sbnf,9));
    3556           7 :   W = gel(sbnf,7);
    3557           7 :   class_group_gen(nf,W,C,Vbase,prec,NULL, &clgp,&clgp2);
    3558             : 
    3559           7 :   zu = gel(sbnf,10);
    3560           7 :   zu = mkvec2(gel(zu,1), nf_to_scalar_or_alg(nf, gel(zu,2)));
    3561           7 :   FU = cgetg_copy(fu, &l);
    3562           7 :   for (k=1; k < l; k++) gel(FU,k) = coltoliftalg(nf, gel(fu,k));
    3563             : 
    3564           7 :   res = get_clfu(clgp, get_regulator(A), zu, FU);
    3565           7 :   y = buchall_end(nf,res,clgp2,W,gel(sbnf,8),A,C,Vbase);
    3566           7 :   return gerepilecopy(av,y);
    3567             : }
    3568             : 
    3569             : GEN
    3570         938 : bnfinit0(GEN P, long flag, GEN data, long prec)
    3571             : {
    3572         938 :   double c1 = BNF_C1, c2 = BNF_C2;
    3573         938 :   long fl, relpid = BNF_RELPID;
    3574             : 
    3575         938 :   if (typ(P) == t_VEC && lg(P) == 13) return sbnf2bnf(P, prec); /* sbnf */
    3576         931 :   if (data)
    3577             :   {
    3578          21 :     long lx = lg(data);
    3579          21 :     if (typ(data) != t_VEC || lx > 5) pari_err_TYPE("bnfinit",data);
    3580          21 :     switch(lx)
    3581             :     {
    3582           0 :       case 4: relpid = itos(gel(data,3));
    3583          14 :       case 3: c2 = gtodouble(gel(data,2));
    3584          14 :       case 2: c1 = gtodouble(gel(data,1));
    3585             :     }
    3586             :   }
    3587         931 :   switch(flag)
    3588             :   {
    3589             :     case 2:
    3590         742 :     case 0: fl = 0; break;
    3591         189 :     case 1: fl = nf_FORCE; break;
    3592           0 :     default: pari_err_FLAG("bnfinit");
    3593           0 :       return NULL; /* not reached */
    3594             :   }
    3595         931 :   return Buchall_param(P, c1, c2, relpid, fl, prec);
    3596             : }
    3597             : GEN
    3598        1203 : Buchall(GEN P, long flag, long prec)
    3599        1203 : { return Buchall_param(P, BNF_C1, BNF_C2, BNF_RELPID, flag, prec); }
    3600             : 
    3601             : static GEN
    3602         427 : Buchall_deg1(GEN nf)
    3603             : {
    3604         427 :   GEN v = cgetg(1,t_VEC), m = cgetg(1,t_MAT);
    3605             :   GEN W, A, B, C, Vbase, res;
    3606         427 :   GEN fu = v, R = gen_1, zu = mkvec2(gen_2, gen_m1);
    3607         427 :   GEN clg1 = mkvec3(gen_1,v,v), clg2 = mkvec3(m,v,v);
    3608             : 
    3609         427 :   W = A = B = C = m;
    3610         427 :   Vbase = cgetg(1,t_COL);
    3611         427 :   res = get_clfu(clg1, R, zu, fu);
    3612         427 :   return buchall_end(nf,res,clg2,W,B,A,C,Vbase);
    3613             : }
    3614             : 
    3615             : /* return (small set of) indices of columns generating the same lattice as x.
    3616             :  * Assume HNF(x) is inexpensive (few rows, many columns).
    3617             :  * Dichotomy approach since interesting columns may be at the very end */
    3618             : GEN
    3619        1776 : extract_full_lattice(GEN x)
    3620             : {
    3621        1776 :   long dj, j, k, l = lg(x);
    3622             :   GEN h, h2, H, v;
    3623             : 
    3624        1776 :   if (l < 200) return NULL; /* not worth it */
    3625             : 
    3626           7 :   v = vecsmalltrunc_init(l);
    3627           7 :   H = ZM_hnf(x);
    3628           7 :   h = cgetg(1, t_MAT);
    3629           7 :   dj = 1;
    3630         378 :   for (j = 1; j < l; )
    3631             :   {
    3632         371 :     pari_sp av = avma;
    3633         371 :     long lv = lg(v);
    3634             : 
    3635         371 :     for (k = 0; k < dj; k++) v[lv+k] = j+k;
    3636         371 :     setlg(v, lv + dj);
    3637         371 :     h2 = ZM_hnf(vecpermute(x, v));
    3638         371 :     if (ZM_equal(h, h2))
    3639             :     { /* these dj columns can be eliminated */
    3640         140 :       avma = av; setlg(v, lv);
    3641         140 :       j += dj;
    3642         140 :       if (j >= l) break;
    3643         140 :       dj <<= 1;
    3644         140 :       if (j + dj >= l) { dj = (l - j) >> 1; if (!dj) dj = 1; }
    3645             :     }
    3646         231 :     else if (dj > 1)
    3647             :     { /* at least one interesting column, try with first half of this set */
    3648         140 :       avma = av; setlg(v, lv);
    3649         140 :       dj >>= 1; /* > 0 */
    3650             :     }
    3651             :     else
    3652             :     { /* this column should be kept */
    3653          91 :       if (ZM_equal(h2, H)) break;
    3654          84 :       h = h2; j++;
    3655             :     }
    3656             :   }
    3657           7 :   return v;
    3658             : }
    3659             : 
    3660             : static void
    3661        1854 : init_rel(RELCACHE_t *cache, FB_t *F, long add_need)
    3662             : {
    3663        1854 :   const long n = F->KC + add_need; /* expected # of needed relations */
    3664             :   long i, j, k, p;
    3665             :   GEN c, P;
    3666             :   GEN R;
    3667             : 
    3668        1854 :   if (DEBUGLEVEL) err_printf("KCZ = %ld, KC = %ld, n = %ld\n", F->KCZ,F->KC,n);
    3669        1854 :   reallocate(cache, 10*n + 50); /* make room for lots of relations */
    3670        1854 :   cache->chk = cache->base;
    3671        1854 :   cache->end = cache->base + n;
    3672        1854 :   cache->relsup = add_need;
    3673        1854 :   cache->last = cache->base;
    3674        1854 :   cache->missing = lg(cache->basis) - 1;
    3675       15096 :   for (i = 1; i <= F->KCZ; i++)
    3676             :   { /* trivial relations (p) = prod P^e */
    3677       13242 :     p = F->FB[i]; P = F->LV[p];
    3678       13242 :     if (!isclone(P)) continue;
    3679             : 
    3680             :     /* all prime divisors in FB */
    3681        9140 :     c = zero_Flv(F->KC); k = F->iLP[p];
    3682        9140 :     R = c; c += k;
    3683        9140 :     for (j = lg(P)-1; j; j--) c[j] = pr_get_e(gel(P,j));
    3684        9140 :     add_rel(cache, F, R, k+1, /*m*/NULL, 0);
    3685             :   }
    3686        1854 : }
    3687             : 
    3688             : /* Let z = \zeta_n in nf. List of not-obviously-dependent generators for
    3689             :  * cyclotomic units modulo torsion in Q(z) [independent when n a prime power]:
    3690             :  * - z^a - 1,  n/(a,n) not a prime power, a \nmid n unless a=1,  1 <= a < n/2
    3691             :  * - (Z^a - 1)/(Z - 1),  p^k || n, Z = z^{n/p^k}, (p,a) = 1, 1 < a <= (p^k-1)/2
    3692             :  */
    3693             : GEN
    3694        1854 : nfcyclotomicunits(GEN nf, GEN zu)
    3695             : {
    3696        1854 :   long n = itos(gel(zu, 1)), n2, lP, i, a;
    3697             :   GEN z, fa, P, E, L, mz, powz;
    3698        1854 :   if (n <= 6) return cgetg(1, t_VEC);
    3699             : 
    3700         119 :   z = algtobasis(nf,gel(zu, 2));
    3701         119 :   if ((n & 3) == 2) { n = n >> 1; z = ZC_neg(z); } /* ensure n != 2 (mod 4) */
    3702         119 :   n2 = n/2;
    3703         119 :   mz = zk_multable(nf, z); /* multiplication by z */
    3704         119 :   powz = cgetg(n2, t_VEC); gel(powz,1) = z;
    3705         119 :   for (i = 2; i < n2; i++) gel(powz,i) = ZM_ZC_mul(mz, gel(powz,i-1));
    3706             :   /* powz[i] = z^i */
    3707             : 
    3708         119 :   L = vectrunc_init(n);
    3709         119 :   fa = factoru(n);
    3710         119 :   P = gel(fa,1); lP = lg(P);
    3711         119 :   E = gel(fa,2);
    3712         252 :   for (i = 1; i < lP; i++)
    3713             :   { /* second kind */
    3714         133 :     long p = P[i], k = E[i], pk = upowuu(p,k), pk2 = (pk-1) / 2;
    3715         133 :     GEN u = gen_1;
    3716         259 :     for (a = 2; a <= pk2; a++)
    3717             :     {
    3718         126 :       u = nfadd(nf, u, gel(powz, (n/pk) * (a-1))); /* = (Z^a-1)/(Z-1) */
    3719         126 :       if (a % p) vectrunc_append(L, u);
    3720             :     }
    3721             :   }
    3722         189 :   if (lP > 2) for (a = 1; a < n2; a++)
    3723             :   { /* first kind, when n not a prime power */
    3724             :     ulong p;
    3725          70 :     if (a > 1 && (n % a == 0 || uisprimepower(n/ugcd(a,n), &p))) continue;
    3726          28 :     vectrunc_append(L, nfadd(nf, gel(powz, a), gen_m1));
    3727             :   }
    3728         119 :   return L;
    3729             : }
    3730             : static void
    3731        1854 : add_cyclotomic_units(GEN nf, GEN zu, RELCACHE_t *cache, FB_t *F)
    3732             : {
    3733        1854 :   pari_sp av = avma;
    3734        1854 :   GEN L = nfcyclotomicunits(nf, zu);
    3735        1854 :   long i, l = lg(L);
    3736        1854 :   if (l > 1)
    3737             :   {
    3738         119 :     GEN R = zero_Flv(F->KC);
    3739         119 :     for(i = 1; i < l; i++) add_rel(cache, F, R, F->KC+1, gel(L,i), 0);
    3740             :   }
    3741        1854 :   avma = av;
    3742        1854 : }
    3743             : 
    3744             : static void
    3745       12316 : shift_embed(GEN G, GEN Gtw, long a, long r1)
    3746             : {
    3747       12316 :   long j, k, l = lg(G);
    3748       12316 :   if (a <= r1)
    3749        7859 :     for (j=1; j<l; j++) gcoeff(G,a,j) = gcoeff(Gtw,a,j);
    3750             :   else
    3751             :   {
    3752        4457 :     k = (a<<1) - r1;
    3753       42517 :     for (j=1; j<l; j++)
    3754             :     {
    3755       38060 :       gcoeff(G,k-1,j) = gcoeff(Gtw,k-1,j);
    3756       38060 :       gcoeff(G,k  ,j) = gcoeff(Gtw,k,  j);
    3757             :     }
    3758             :   }
    3759       12316 : }
    3760             : 
    3761             : /* G where embeddings a and b are multiplied by 2^10 */
    3762             : static GEN
    3763        8026 : shift_G(GEN G, GEN Gtw, long a, long b, long r1)
    3764             : {
    3765        8026 :   GEN g = RgM_shallowcopy(G);
    3766        8026 :   if (a != b) shift_embed(g,Gtw,a,r1);
    3767        8026 :   shift_embed(g,Gtw,b,r1); return g;
    3768             : }
    3769             : 
    3770             : static void
    3771        1707 : compute_vecG(GEN nf, FB_t *F, long n)
    3772             : {
    3773        1707 :   GEN G0, Gtw0, vecG, G = nf_get_G(nf);
    3774        1707 :   long e, i, j, ind, r1 = nf_get_r1(nf), r = lg(G)-1;
    3775        3414 :   if (n == 1) { F->G0 = G0 = ground(G); F->vecG = mkvec( G0 ); return; }
    3776        1371 :   for (e = 32;;)
    3777             :   {
    3778        1371 :     G = gmul2n(G, e);
    3779        1371 :     G0 = ground(G); if (ZM_rank(G0) == r) break; /* maximal rank ? */
    3780           0 :   }
    3781        1371 :   Gtw0 = ground(gmul2n(G, 10));
    3782        1371 :   vecG = cgetg(1 + n*(n+1)/2,t_VEC);
    3783        5107 :   for (ind=j=1; j<=n; j++)
    3784        3736 :     for (i=1; i<=j; i++) gel(vecG,ind++) = shift_G(G0,Gtw0,i,j,r1);
    3785        1371 :   F->G0 = G0; F->vecG = vecG;
    3786             : }
    3787             : 
    3788             : static GEN
    3789        1707 : automorphism_perms(GEN M, GEN auts, GEN cyclic, long N)
    3790             : {
    3791             :   pari_sp av;
    3792        1707 :   const long r1plusr2 = lgcols(M), r1 = 2*r1plusr2-N-2, r2 = r1plusr2-r1-1;
    3793        1707 :   long nauts = lg(auts), ncyc = lg(cyclic), i, j, l, m;
    3794        1707 :   GEN Mt, perms = cgetg(nauts, t_VEC);
    3795             : 
    3796        4192 :   for (l = 1; l < nauts; l++)
    3797        2485 :     gel(perms, l) = cgetg(r1plusr2, t_VECSMALL);
    3798        1707 :   av = avma;
    3799        1707 :   Mt = shallowtrans(gprec_w(M, 3)); /* need little accuracy */
    3800        1707 :   Mt = shallowconcat(Mt, gconj(vecslice(Mt, r1+1, r1+r2)));
    3801        3646 :   for (l = 1; l < ncyc; l++)
    3802             :   {
    3803        1939 :     GEN thiscyc = gel(cyclic, l);
    3804        1939 :     long k = thiscyc[1];
    3805        1939 :     GEN Nt = RgM_mul(shallowtrans(gel(auts, k)), Mt);
    3806        1939 :     GEN perm = gel(perms, k), permprec;
    3807        1939 :     pari_sp av2 = avma;
    3808        7497 :     for (i = 1; i < r1plusr2; i++, avma = av2)
    3809             :     {
    3810        5558 :       GEN vec = gel(Nt, i), minnorm;
    3811        5558 :       minnorm = gnorml2(gsub(vec, gel(Mt, 1)));
    3812        5558 :       perm[i] = 1;
    3813       37135 :       for (j = 2; j <= N; j++)
    3814             :       {
    3815       31577 :         GEN thisnorm = gnorml2(gsub(vec, gel(Mt, j)));
    3816       31577 :         if (gcmp(thisnorm, minnorm) < 0)
    3817             :         {
    3818        7462 :           minnorm = thisnorm;
    3819        7462 :           perm[i] = j >= r1plusr2 ? r2-j : j;
    3820             :         }
    3821             :       }
    3822             :     }
    3823        2590 :     for (permprec = perm, m = 2; m < lg(thiscyc); m++)
    3824             :     {
    3825         651 :       GEN thisperm = gel(perms, thiscyc[m]);
    3826        3864 :       for (i = 1; i < r1plusr2; i++)
    3827             :       {
    3828        3213 :         long pp = labs(permprec[i]);
    3829        3213 :         thisperm[i] = permprec[i] < 0 ? -perm[pp] : perm[pp];
    3830             :       }
    3831         651 :       permprec = thisperm;
    3832             :     }
    3833             :   }
    3834        1707 :   avma = av;
    3835        1707 :   return perms;
    3836             : }
    3837             : 
    3838             : /* Determine the field automorphisms and its matrix in the integral basis. */
    3839             : static GEN
    3840        1707 : automorphism_matrices(GEN nf, GEN *invp, GEN *cycp)
    3841             : {
    3842        1707 :   pari_sp av = avma;
    3843        1707 :   GEN auts = galoisconj(nf, NULL), mats, cyclic, cyclicidx;
    3844             :   GEN invs;
    3845        1707 :   long nauts = lg(auts)-1, i, j, k, l;
    3846             : 
    3847        1707 :   cyclic = cgetg(nauts+1, t_VEC);
    3848        1707 :   cyclicidx = zero_Flv(nauts);
    3849        1707 :   invs = zero_Flv(nauts-1);
    3850        1952 :   for (l = 1; l <= nauts; l++)
    3851             :   {
    3852        1952 :     GEN aut = gel(auts, l);
    3853        3659 :     if (degpol(aut) == 1 && isint1(leading_coeff(aut)) &&
    3854        1707 :         isintzero(constant_coeff(aut)))
    3855             :     {
    3856        1707 :       swap(gel(auts, l), gel(auts, nauts));
    3857        1707 :       break;
    3858             :     }
    3859             :   }
    3860        1707 :   for (l = 1; l <= nauts; l++) gel(auts, l) = algtobasis(nf, gel(auts, l));
    3861             :   /* Compute maximal cyclic subgroups */
    3862        5899 :   for (l = nauts; --l > 0; )
    3863        2485 :     if (!cyclicidx[l])
    3864             :     {
    3865        2023 :       GEN elt = gel(auts, l), aut = elt, cyc = cgetg(nauts+1, t_VECSMALL);
    3866        2023 :       cyclicidx[l] = l;
    3867        2023 :       cyc[1] = l;
    3868        2023 :       j = 1;
    3869             :       do
    3870             :       {
    3871        2681 :         elt = galoisapply(nf, elt, aut);
    3872        2681 :         for (k = 1; k <= nauts; k++) if (gequal(elt, gel(auts, k))) break;
    3873        2681 :         cyclicidx[k] = l;
    3874        2681 :         cyc[++j] = k;
    3875             :       }
    3876        2681 :       while (k != nauts);
    3877        2023 :       setlg(cyc, j);
    3878        2023 :       gel(cyclic, l) = cyc;
    3879             :       /* Store the inverses */
    3880        4340 :       for (i = 1; i <= j/2; i++)
    3881             :       {
    3882        2317 :         invs[cyc[i]] = cyc[j-i];
    3883        2317 :         invs[cyc[j-i]] = cyc[i];
    3884             :       }
    3885             :     }
    3886        4192 :   for (i = j = 1; i < nauts; i++)
    3887        2485 :     if (cyclicidx[i] == i) cyclic[j++] = cyclic[i];
    3888        1707 :   setlg(cyclic, j);
    3889        1707 :   mats = cgetg(nauts, t_VEC);
    3890        5353 :   while (--j > 0)
    3891             :   {
    3892        1939 :     GEN cyc = gel(cyclic, j);
    3893        1939 :     long id = cyc[1];
    3894        1939 :     GEN M, Mi, aut = gel(auts, id);
    3895             : 
    3896        1939 :     gel(mats, id) = Mi = M = nfgaloismatrix(nf, aut);
    3897        2590 :     for (i = 2; i < lg(cyc); i++)
    3898             :     {
    3899         651 :       Mi = ZM_mul(Mi, M);
    3900         651 :       gel(mats, cyc[i]) = Mi;
    3901             :     }
    3902             :   }
    3903        1707 :   gerepileall(av, 3, &mats, &invs, &cyclic);
    3904        1707 :   *invp = invs;
    3905        1707 :   *cycp = cyclic;
    3906        1707 :   return mats;
    3907             : }
    3908             : 
    3909             : static GEN
    3910       16858 : trim_list(FB_t *F)
    3911             : {
    3912       16858 :   pari_sp av = avma;
    3913       16858 :   GEN L_jid = F->L_jid, present = zero_Flv(F->KC);
    3914       16858 :   long i, j, imax = minss(lg(L_jid), F->KC + 1);
    3915       16858 :   GEN minidx = F->minidx, idx = cgetg(imax, t_VECSMALL);
    3916             : 
    3917      678268 :   for (i = j = 1; i < imax; i++)
    3918             :   {
    3919      661410 :     long id = minidx[L_jid[i]];
    3920             : 
    3921      661410 :     if (!present[id])
    3922             :     {
    3923      425902 :       idx[j++] = L_jid[i];
    3924      425902 :       present[id] = 1;
    3925             :     }
    3926             :   }
    3927       16858 :   setlg(idx, j);
    3928       16858 :   return gerepileuptoleaf(av, idx);
    3929             : }
    3930             : 
    3931             : static void
    3932        1659 : try_elt(RELCACHE_t *cache, FB_t *F, GEN nf, GEN x, FACT *fact)
    3933             : {
    3934        1659 :   pari_sp av = avma;
    3935             :   GEN R, Nx;
    3936        1659 :   long nz, tx = typ(x);
    3937             : 
    3938        1666 :   if (tx == t_INT || tx == t_FRAC) return;
    3939        1659 :   if (tx != t_COL) x = algtobasis(nf, x);
    3940        1659 :   if (RgV_isscalar(x)) return;
    3941        1652 :   x = Q_primpart(x);
    3942        1652 :   Nx = nfnorm(nf, x);
    3943        1652 :   if (!can_factor(F, nf, NULL, x, Nx, fact)) return;
    3944             : 
    3945             :   /* smooth element */
    3946        1652 :   R = set_fact(F, fact, NULL, &nz);
    3947             :   /* make sure we get maximal rank first, then allow all relations */
    3948        1652 :   (void) add_rel(cache, F, R, nz, x, 0);
    3949        1652 :   avma = av;
    3950             : }
    3951             : 
    3952             : GEN
    3953        2134 : Buchall_param(GEN P, double cbach, double cbach2, long nbrelpid, long flun, long prec)
    3954             : {
    3955             :   pari_timer T;
    3956        2134 :   pari_sp av0 = avma, av, av2;
    3957             :   long PRECREG, N, R1, R2, RU, low, high, LIMC0, LIMC, LIMC2, LIMCMAX, zc, i;
    3958             :   long LIMres;
    3959             :   long MAXDEPSIZESFB, MAXDEPSFB;
    3960        2134 :   long nreldep, sfb_trials, need, old_need, precdouble = 0, precadd = 0;
    3961             :   long done_small, small_fail, fail_limit, squash_index, small_norm_prec;
    3962        2134 :   long flag_nfinit = 0;
    3963             :   double LOGD, LOGD2, lim;
    3964        2134 :   GEN computed = NULL, zu, nf, M_sn, D, A, W, R, h, PERM, fu = NULL /*-Wall*/;
    3965             :   GEN small_multiplier;
    3966             :   GEN res, L, invhr, B, C, C0, lambda, dep, clg1, clg2, Vbase;
    3967             :   GEN auts, cyclic;
    3968        2134 :   const char *precpb = NULL;
    3969        2134 :   int FIRST = 1, class1 = 0;
    3970             :   nfbasic_t nfT;
    3971             :   RELCACHE_t cache;
    3972             :   FB_t F;
    3973             :   GRHcheck_t GRHcheck;
    3974             :   FACT *fact;
    3975             : 
    3976        2134 :   if (DEBUGLEVEL) timer_start(&T);
    3977        2134 :   P = get_nfpol(P, &nf);
    3978        2134 :   if (nf)
    3979             :   {
    3980         111 :     PRECREG = nf_get_prec(nf);
    3981         111 :     D = nf_get_disc(nf);
    3982             :   }
    3983             :   else
    3984             :   {
    3985        2023 :     PRECREG = maxss(prec, MEDDEFAULTPREC);
    3986        2023 :     nfinit_step1(&nfT, P, 0);
    3987        2023 :     D = nfT.dK;
    3988        2023 :     if (!equali1(leading_coeff(nfT.x0)))
    3989             :     {
    3990          14 :       pari_warn(warner,"non-monic polynomial in bnfinit, using polredbest");
    3991          14 :       flag_nfinit = nf_RED;
    3992             :     }
    3993             :   }
    3994        2134 :   N = degpol(P);
    3995        2134 :   if (N <= 1)
    3996             :   {
    3997         427 :     if (!nf) nf = nfinit_step2(&nfT, flag_nfinit, PRECREG);
    3998         427 :     return gerepilecopy(av0, Buchall_deg1(nf));
    3999             :   }
    4000        1707 :   D = absi(D);
    4001        1707 :   LOGD = dbllog2(D) * LOG2;
    4002        1707 :   LOGD2 = LOGD*LOGD;
    4003        1707 :   LIMCMAX = (long)(12.*LOGD2);
    4004             :   /* In small_norm, LLL reduction produces v0 in I such that
    4005             :    *     T2(v0) <= (4/3)^((n-1)/2) NI^(2/n) disc(K)^(1/n)
    4006             :    * We consider v with T2(v) <= BMULT * T2(v0)
    4007             :    * Hence Nv <= ((4/3)^((n-1)/2) * BMULT / n)^(n/2) NI sqrt(disc(K)).
    4008             :    * NI <= LIMCMAX^2 */
    4009        1707 :   small_norm_prec = nbits2prec( BITS_IN_LONG +
    4010        1707 :     (N/2. * ((N-1)/2.*log(4./3) + log(BMULT/(double)N))
    4011        1707 :      + 2*log((double) LIMCMAX) + LOGD/2) / LOG2 ); /* enough to compute norms */
    4012        1707 :   if (small_norm_prec > PRECREG) PRECREG = small_norm_prec;
    4013        1707 :   if (!nf)
    4014        1624 :     nf = nfinit_step2(&nfT, flag_nfinit, PRECREG);
    4015          83 :   else if (nf_get_prec(nf) < PRECREG)
    4016           0 :     nf = nfnewprec_shallow(nf, PRECREG);
    4017        1707 :   M_sn = nf_get_M(nf);
    4018        1707 :   if (PRECREG > small_norm_prec) M_sn = gprec_w(M_sn, small_norm_prec);
    4019             : 
    4020        1707 :   zu = rootsof1(nf);
    4021        1707 :   gel(zu,2) = nf_to_scalar_or_alg(nf, gel(zu,2));
    4022             : 
    4023        1707 :   auts = automorphism_matrices(nf, &F.invs, &cyclic);
    4024        1707 :   F.embperm = automorphism_perms(nf_get_M(nf), auts, cyclic, N);
    4025             : 
    4026        1707 :   nf_get_sign(nf, &R1, &R2); RU = R1+R2;
    4027        1707 :   compute_vecG(nf, &F, minss(RU, 9));
    4028        1707 :   if (DEBUGLEVEL)
    4029             :   {
    4030           0 :     timer_printf(&T, "nfinit & rootsof1");
    4031           0 :     err_printf("R1 = %ld, R2 = %ld\nD = %Ps\n",R1,R2, D);
    4032             :   }
    4033        1707 :   if (LOGD < 20.) /* tiny disc, Minkowski *may* be smaller than Bach */
    4034             :   {
    4035        1463 :     lim = exp(-N + R2 * log(4/M_PI) + LOGD/2) * sqrt(2*M_PI*N);
    4036        1463 :     if (lim < 3) lim = 3;
    4037             :   }
    4038             :   else /* to be ignored */
    4039         244 :     lim = -1;
    4040        1707 :   if (cbach > 12.) {
    4041           0 :     if (cbach2 < cbach) cbach2 = cbach;
    4042           0 :     cbach = 12.;
    4043             :   }
    4044        1707 :   if (cbach < 0.)
    4045           0 :     pari_err_DOMAIN("Buchall","Bach constant","<",gen_0,dbltor(cbach));
    4046             : 
    4047        1707 :   cache.base = NULL; F.subFB = NULL; F.LP = NULL;
    4048        1707 :   init_GRHcheck(&GRHcheck, N, R1, LOGD);
    4049        1707 :   high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
    4050       11232 :   while (!GRHchk(nf, &GRHcheck, high))
    4051             :   {
    4052        7818 :     low = high;
    4053        7818 :     high *= 2;
    4054             :   }
    4055        9567 :   while (high - low > 1)
    4056             :   {
    4057        6153 :     long test = (low+high)/2;
    4058        6153 :     if (GRHchk(nf, &GRHcheck, test))
    4059        3483 :       high = test;
    4060             :     else
    4061        2670 :       low = test;
    4062             :   }
    4063        1707 :   if (high == LIMC0+1 && GRHchk(nf, &GRHcheck, LIMC0))
    4064           0 :     LIMC2 = LIMC0;
    4065             :   else
    4066        1707 :     LIMC2 = high;
    4067        1707 :   if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
    4068        1707 :   if (DEBUGLEVEL) err_printf("LIMC2 = %ld\n", LIMC2);
    4069        1707 :   if (LIMC2 < nthideal(&GRHcheck, nf, 1)) class1 = 1;
    4070        1707 :   if (DEBUGLEVEL && class1) err_printf("Class 1\n", LIMC2);
    4071        1707 :   LIMC0 = (long)(cbach*LOGD2);
    4072        1707 :   LIMC = cbach ? LIMC0 : LIMC2;
    4073        1707 :   LIMC = maxss(LIMC, nthideal(&GRHcheck, nf, N));
    4074        1707 :   if (DEBUGLEVEL) timer_printf(&T, "computing Bach constant");
    4075        1707 :   LIMres = primeneeded(N, R1, R2, LOGD);
    4076        1707 :   cache_prime_dec(&GRHcheck, LIMres, nf);
    4077             :   /* invhr ~ 2^r1 (2pi)^r2 / sqrt(D) w * Res(zeta_K, s=1) = 1 / hR */
    4078        3414 :   invhr = gmul(gdiv(gmul2n(powru(mppi(DEFAULTPREC), R2), RU),
    4079        1707 :               mulri(gsqrt(D,DEFAULTPREC),gel(zu,1))),
    4080             :               compute_invres(&GRHcheck, LIMres));
    4081        1707 :   if (DEBUGLEVEL) timer_printf(&T, "computing inverse of hR");
    4082        1707 :   av = avma;
    4083             : 
    4084             : START:
    4085        1854 :   if (DEBUGLEVEL) timer_start(&T);
    4086        1854 :   if (!FIRST) LIMC = check_LIMC(LIMC,LIMCMAX);
    4087        1854 :   if (DEBUGLEVEL && LIMC > LIMC0)
    4088           0 :     err_printf("%s*** Bach constant: %f\n", FIRST?"":"\n", LIMC/LOGD2);
    4089        1854 :   if (cache.base)
    4090             :   {
    4091             :     REL_t *rel;
    4092        6461 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    4093        6314 :       if (rel->m) i++;
    4094         147 :     computed = cgetg(i, t_VEC);
    4095        6461 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    4096        6314 :       if (rel->m) gel(computed, i++) = rel->m;
    4097         147 :     computed = gclone(computed);
    4098         147 :     delete_cache(&cache);
    4099             :   }
    4100        1854 :   FIRST = 0; avma = av;
    4101        1854 :   if (F.LP) delete_FB(&F);
    4102        1854 :   if (LIMC2 < LIMC) LIMC2 = LIMC;
    4103        1854 :   if (DEBUGLEVEL) { err_printf("LIMC = %ld, LIMC2 = %ld\n",LIMC,LIMC2); }
    4104             : 
    4105        1854 :   FBgen(&F, nf, N, LIMC, LIMC2, &GRHcheck);
    4106        1854 :   if (!F.KC) goto START;
    4107        1854 :   av = avma;
    4108        1854 :   subFBgen(&F,nf,auts,cyclic,lim < 0? LIMC2: mindd(lim,LIMC2),MINSFB);
    4109        1854 :   if (DEBUGLEVEL)
    4110             :   {
    4111           0 :     if (lg(F.subFB) > 1)
    4112           0 :       timer_printf(&T, "factorbase (#subFB = %ld) and ideal permutations",
    4113           0 :                        lg(F.subFB)-1);
    4114             :     else
    4115           0 :       timer_printf(&T, "factorbase (no subFB) and ideal permutations");
    4116             :   }
    4117        1854 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    4118        1854 :   PERM = leafcopy(F.perm); /* to be restored in case of precision increase */
    4119        1854 :   cache.basis = zero_Flm_copy(F.KC,F.KC);
    4120        1854 :   small_multiplier = zero_Flv(F.KC);
    4121        1854 :   F.id2 = zerovec(F.KC);
    4122        1854 :   MAXDEPSIZESFB = (lg(F.subFB) - 1) * DEPSIZESFBMULT;
    4123        1854 :   MAXDEPSFB = MAXDEPSIZESFB / DEPSFBDIV;
    4124        1854 :   done_small = 0; small_fail = 0; squash_index = 0;
    4125        1854 :   fail_limit = F.KC + 1;
    4126        1854 :   R = NULL; A = NULL;
    4127        1854 :   av2 = avma;
    4128        1854 :   init_rel(&cache, &F, RELSUP + RU-1); /* trivial relations */
    4129        1854 :   old_need = need = cache.end - cache.last;
    4130        1854 :   add_cyclotomic_units(nf, zu, &cache, &F);
    4131        1854 :   cache.end = cache.last + need;
    4132             : 
    4133        1854 :   W = NULL; zc = 0;
    4134        1854 :   sfb_trials = nreldep = 0;
    4135             : 
    4136        1854 :   if (computed)
    4137             :   {
    4138        1806 :     for (i = 1; i < lg(computed); i++)
    4139        1659 :       try_elt(&cache, &F, nf, gel(computed, i), fact);
    4140         147 :     if (isclone(computed)) gunclone(computed);
    4141         147 :     if (DEBUGLEVEL && i > 1)
    4142             :     {
    4143           0 :       err_printf("\n");
    4144           0 :       timer_printf(&T, "including already computed relations");
    4145             :     }
    4146         147 :     need = 0;
    4147             :   }
    4148             : 
    4149             :   do
    4150             :   {
    4151             :     do
    4152             :     {
    4153       17087 :       pari_sp av4 = avma;
    4154       17087 :       if (need > 0)
    4155             :       {
    4156       16858 :         long oneed = cache.end - cache.last;
    4157             :         /* Test below can be true if small_norm did not find enough linearly
    4158             :          * dependent relations */
    4159       16858 :         if (need < oneed) need = oneed;
    4160       16858 :         pre_allocate(&cache, need+lg(auts)-1+(R ? lg(W)-1 : 0));
    4161       16858 :         cache.end = cache.last + need;
    4162       16858 :         F.L_jid = trim_list(&F);
    4163             :       }
    4164       17087 :       if (need > 0 && nbrelpid > 0 && (done_small <= F.KC+1 || A) &&
    4165       15738 :           small_fail <= fail_limit &&
    4166       15738 :           cache.last < cache.base + 2*F.KC+2*RU+RELSUP /* heuristic */)
    4167             :       {
    4168       13883 :         pari_sp av3 = avma;
    4169       13883 :         GEN p0 = NULL;
    4170             :         long j, k;
    4171       13883 :         REL_t *last = cache.last;
    4172       13883 :         if (R && lg(W) > 1 && (done_small % 2))
    4173             :         {
    4174             :           /* We have full rank for class group and unit, however those
    4175             :            * lattices are too small. The following tries to improve the
    4176             :            * prime group lattice: it specifically looks for relations
    4177             :            * involving the primes generating the class group. */
    4178         517 :           long l = lg(W) - 1;
    4179             :           /* We need lg(W)-1 relations to squash the class group. */
    4180         517 :           F.L_jid = vecslice(F.perm, 1, l); cache.end = cache.last + l;
    4181             :           /* Lie to the add_rel subsystem: pretend we miss relations involving
    4182             :            * the primes generating the class group (and only those). */
    4183         517 :           cache.missing = l;
    4184         517 :           for ( ; l > 0; l--) mael(cache.basis, F.perm[l], F.perm[l]) = 0;
    4185             :         }
    4186       13883 :         j = done_small % (F.KC+1);
    4187       13883 :         if (j)
    4188             :         {
    4189       11798 :           long mj = small_multiplier[j];
    4190       11798 :           p0 = gel(F.LP, j);
    4191       11798 :           if (!A)
    4192             :           {
    4193             :             /* Prevent considering both P_iP_j and P_jP_i in small_norm */
    4194             :             /* Since not all elements end up in F.L_jid (because they can
    4195             :              * be eliminated by hnfspec/add or by trim_list, keep track
    4196             :              * of which ideals are being considered at each run. */
    4197      322136 :             for (i = k = 1; i < lg(F.L_jid); i++)
    4198      313893 :               if (F.L_jid[i] > mj)
    4199             :               {
    4200      262283 :                 small_multiplier[F.L_jid[i]] = j;
    4201      262283 :                 F.L_jid[k++] = F.L_jid[i];
    4202             :               }
    4203        8243 :             setlg(F.L_jid, k);
    4204             :           }
    4205             :         }
    4206       13883 :         if (lg(F.L_jid) > 1)
    4207       13596 :           small_norm(&cache, &F, nf, nbrelpid, M_sn, fact, p0);
    4208       13883 :         avma = av3;
    4209       13883 :         if (!A && cache.last != last)
    4210        3475 :           small_fail = 0;
    4211             :         else
    4212       10408 :           small_fail++;
    4213       13883 :         if (R && lg(W) > 1 && (done_small % 2))
    4214             :         {
    4215         517 :           long l = lg(W) - 1;
    4216         517 :           for ( ; l > 0; l--) mael(cache.basis, F.perm[l], F.perm[l]) = 1;
    4217         517 :           cache.missing = 0;
    4218             :         }
    4219       13883 :         F.L_jid = F.perm;
    4220       13883 :         need = 0; cache.end = cache.last;
    4221       13883 :         done_small++;
    4222       13883 :         if (!need) F.sfb_chg = 0;
    4223             :       }
    4224       17087 :       if (need > 0)
    4225             :       {
    4226             :         /* Random relations */
    4227        2975 :         if (lg(F.subFB) == 1) goto START;
    4228        2835 :         nreldep++;
    4229        2835 :         if (nreldep > MAXDEPSIZESFB) {
    4230          28 :           if (++sfb_trials > SFB_MAX && LIMC < LIMCMAX/6) goto START;
    4231          21 :           F.sfb_chg = sfb_INCREASE;
    4232          21 :           nreldep = 0;
    4233             :         }
    4234        2807 :         else if (!(nreldep % MAXDEPSFB))
    4235         421 :           F.sfb_chg = sfb_CHANGE;
    4236        2828 :         if (F.newpow)
    4237             :         {
    4238         231 :           F.sfb_chg = 0;
    4239         231 :           if (DEBUGLEVEL) err_printf("\n");
    4240             :         }
    4241        2828 :         if (F.sfb_chg && !subFB_change(&F)) goto START;
    4242        2828 :         if (F.newpow) {
    4243         358 :           powFBgen(&cache, &F, nf, auts);
    4244         358 :           MAXDEPSIZESFB = (lg(F.subFB) - 1) * DEPSIZESFBMULT;
    4245         358 :           MAXDEPSFB = MAXDEPSIZESFB / DEPSFBDIV;
    4246         358 :           if (DEBUGLEVEL) timer_printf(&T, "powFBgen");
    4247             :         }
    4248        2828 :         if (!F.sfb_chg) rnd_rel(&cache, &F, nf, fact);
    4249        2828 :         F.L_jid = F.perm;
    4250             :       }
    4251       16940 :       if (DEBUGLEVEL) timer_start(&T);
    4252       16940 :       if (precpb)
    4253             :       {
    4254          90 :         GEN nf0 = nf;
    4255          90 :         if (precadd) { PRECREG += precadd; precadd = 0; }
    4256          35 :         else           PRECREG = precdbl(PRECREG);
    4257          90 :         if (DEBUGLEVEL)
    4258             :         {
    4259           0 :           char str[64]; sprintf(str,"Buchall_param (%s)",precpb);
    4260           0 :           pari_warn(warnprec,str,PRECREG);
    4261             :         }
    4262          90 :         nf = gclone( nfnewprec_shallow(nf, PRECREG) );
    4263          90 :         if (precdouble) gunclone(nf0);
    4264          90 :         precdouble++; precpb = NULL;
    4265             : 
    4266          90 :         for (i = 1; i < lg(PERM); i++) F.perm[i] = PERM[i];
    4267          90 :         cache.chk = cache.base; W = NULL; /* recompute arch components+reduce */
    4268             :       }
    4269       16940 :       avma = av4;
    4270       16940 :       if (cache.chk != cache.last)
    4271             :       { /* Reduce relation matrices */
    4272        9416 :         long l = cache.last - cache.chk + 1, j;
    4273        9416 :         GEN M = nf_get_M(nf), mat = cgetg(l, t_MAT), emb = cgetg(l, t_MAT);
    4274        9416 :         int first = (W == NULL); /* never reduced before */
    4275             :         REL_t *rel;
    4276             : 
    4277       77574 :         for (j=1,rel = cache.chk + 1; j < l; rel++,j++)
    4278             :         {
    4279       68158 :           gel(mat,j) = rel->R;
    4280       68158 :           if (!rel->relaut)
    4281       42380 :             gel(emb,j) = get_log_embed(rel, M, RU, R1, PRECREG);
    4282             :           else
    4283       51556 :             gel(emb,j) = perm_log_embed(gel(emb, j-rel->relorig),
    4284       25778 :                                         gel(F.embperm, rel->relaut));
    4285             :         }
    4286        9416 :         if (DEBUGLEVEL) timer_printf(&T, "floating point embeddings");
    4287        9416 :         if (first) {
    4288        1944 :           C = emb;
    4289        1944 :           W = hnfspec_i(mat, F.perm, &dep, &B, &C, F.subFB ? lg(F.subFB)-1:0);
    4290             :         }
    4291             :         else
    4292        7472 :           W = hnfadd_i(W, F.perm, &dep, &B, &C, mat, emb);
    4293        9416 :         gerepileall(av2, 4, &W,&C,&B,&dep);
    4294        9416 :         cache.chk = cache.last;
    4295        9416 :         if (DEBUGLEVEL)
    4296             :         {
    4297           0 :           if (first)
    4298           0 :             timer_printf(&T, "hnfspec [%ld x %ld]", lg(F.perm)-1, l-1);
    4299             :           else
    4300           0 :             timer_printf(&T, "hnfadd (%ld + %ld)", l-1, lg(dep)-1);
    4301             :         }
    4302             :       }
    4303        7524 :       else if (!W)
    4304             :       {
    4305           0 :         need = old_need;
    4306           0 :         F.L_jid = vecslice(F.perm, 1, need);
    4307           0 :         continue;
    4308             :       }
    4309       16940 :       need = F.KC - (lg(W)-1) - (lg(B)-1);
    4310             :       /* FIXME: replace by err(e_BUG,"") */
    4311       16940 :       if (!need && cache.missing)
    4312             :       { /* The test above will never be true except if 27449|class number,
    4313             :          * but the code implicitely assumes that if we have maximal rank
    4314             :          * for the ideal lattice, then cache.missing == 0. */
    4315          14 :         for (i = 1; cache.missing; i++)
    4316           7 :           if (!mael(cache.basis, i, i))
    4317             :           {
    4318             :             long j;
    4319           7 :             mael(cache.basis, i, i) = 1;
    4320           7 :             cache.missing--;
    4321           7 :             for (j = i+1; j <= F.KC; j++) mael(cache.basis, j, i) = 0;
    4322             :           }
    4323             :       }
    4324       16940 :       zc = (lg(C)-1) - (lg(B)-1) - (lg(W)-1);
    4325       16940 :       if (zc < RU-1)
    4326             :       {
    4327             :         /* need more columns for units */
    4328        3779 :         need += RU-1 - zc;
    4329        3779 :         if (need > F.KC) need = F.KC;
    4330             :       }
    4331       16940 :       if (need)
    4332             :       { /* dependent rows */
    4333        9238 :         F.L_jid = vecslice(F.perm, 1, need);
    4334        9238 :         vecsmall_sort(F.L_jid);
    4335        9238 :         if (need != old_need) nreldep = 0;
    4336        9238 :         old_need = need;
    4337             :       }
    4338             :       else
    4339             :       {
    4340             :         /* If the relation lattice is too small, check will be > 1 and we
    4341             :          * will do a new run of small_norm/rnd_rel asking for 1 relation.
    4342             :          * However they tend to give a relation involving the first element
    4343             :          * of L_jid. We thus permute which element is the first of L_jid in
    4344             :          * order to increase the probability of finding a good relation, i.e.
    4345             :          * one that increases the relation lattice. */
    4346        7702 :         if (lg(W) > 2 && squash_index % (lg(W) - 1))
    4347        2132 :         {
    4348        2132 :           long j, l = lg(W) - 1;
    4349        2132 :           F.L_jid = leafcopy(F.perm);
    4350       12940 :           for (j = 1; j <= l; j++)
    4351       10808 :             F.L_jid[j] = F.perm[1 + (j + squash_index - 1) % l];
    4352             :         }
    4353             :         else
    4354        5570 :           F.L_jid = F.perm;
    4355        7702 :         squash_index++;
    4356             :       }
    4357             :     }
    4358       16940 :     while (need);
    4359        7702 :     if (!A)
    4360             :     {
    4361        1714 :       small_fail = 0; fail_limit = maxss(F.KC / FAIL_DIVISOR, MINFAIL);
    4362        1714 :       old_need = 0;
    4363             :     }
    4364        7702 :     A = vecslice(C, 1, zc); /* cols corresponding to units */
    4365        7702 :     R = compute_multiple_of_R(A, RU, N, &need, &lambda);
    4366        7702 :     if (need < old_need) small_fail = 0;
    4367        7702 :     old_need = need;
    4368        7702 :     if (!lambda) { precpb = "bestappr"; continue; }
    4369        7694 :     if (!R)
    4370             :     { /* not full rank for units */
    4371        4490 :       if (DEBUGLEVEL) err_printf("regulator is zero.\n");
    4372        4490 :       if (!need) precpb = "regulator";
    4373        4490 :       continue;
    4374             :     }
    4375             : 
    4376        3204 :     h = ZM_det_triangular(W);
    4377        3204 :     if (DEBUGLEVEL) err_printf("\n#### Tentative class number: %Ps\n", h);
    4378             : 
    4379        3204 :     switch (compute_R(lambda, mulir(h,invhr), &L, &R, &T))
    4380             :     {
    4381             :       case fupb_RELAT:
    4382        1415 :         need = 1; /* not enough relations */
    4383        1415 :         continue;
    4384             :       case fupb_PRECI: /* prec problem unless we cheat on Bach constant */
    4385          13 :         if ((precdouble&7) == 7 && LIMC<=LIMCMAX/6) goto START;
    4386          13 :         precpb = "compute_R";
    4387          13 :         continue;
    4388             :     }
    4389             :     /* DONE */
    4390             : 
    4391        1776 :     if (F.KCZ2 > F.KCZ)
    4392             :     {
    4393           7 :       if (F.sfb_chg && !subFB_change(&F)) goto START;
    4394           7 :       if (!be_honest(&F, nf, auts, fact)) goto START;
    4395           7 :       if (DEBUGLEVEL) timer_printf(&T, "to be honest");
    4396             :     }
    4397        1776 :     F.KCZ2 = 0; /* be honest only once */
    4398             : 
    4399             :     /* fundamental units */
    4400             :     {
    4401        1776 :       pari_sp av3 = avma;
    4402        1776 :       GEN AU, U, H, v = extract_full_lattice(L); /* L may be very large */
    4403             :       long e;
    4404        1776 :       if (v)
    4405             :       {
    4406           7 :         A = vecpermute(A, v);
    4407           7 :         L = vecpermute(L, v);
    4408             :       }
    4409             :       /* arch. components of fund. units */
    4410        1776 :       H = ZM_hnflll(L, &U, 1); U = vecslice(U, lg(U)-(RU-1), lg(U)-1);
    4411        1776 :       U = ZM_mul(U, ZM_lll(H, 0.99, LLL_IM|LLL_COMPATIBLE));
    4412        1776 :       AU = RgM_mul(A, U);
    4413        1776 :       A = cleanarch(AU, N, PRECREG);
    4414        1776 :       if (DEBUGLEVEL) timer_printf(&T, "cleanarch");
    4415        1776 :       if (!A) {
    4416           0 :         precadd = nbits2extraprec( gexpo(AU) + 64 ) - gprecision(AU);
    4417           0 :         if (precadd <= 0) precadd = 1;
    4418          69 :         precpb = "cleanarch"; continue;
    4419             :       }
    4420        1776 :       fu = getfu(nf, &A, &e, PRECREG);
    4421        1776 :       if (DEBUGLEVEL) timer_printf(&T, "getfu");
    4422        1776 :       if (!fu && (flun & nf_FORCE))
    4423             :       { /* units not found but we want them */
    4424          69 :         if (e > 0) pari_err_OVERFLOW("bnfinit [fundamental units too large]");
    4425          69 :         if (e < 0) precadd = nbits2extraprec( (-e - (BITS_IN_LONG - 1)) + 64);
    4426          69 :         avma = av3; precpb = "getfu"; continue;
    4427             :       }
    4428             :     }
    4429             :     /* class group generators */
    4430        1707 :     i = lg(C)-zc; C += zc; C[0] = evaltyp(t_MAT)|evallg(i);
    4431        1707 :     C0 = C; C = cleanarch(C, N, PRECREG);
    4432        1707 :     if (!C) {
    4433           0 :       precadd = nbits2extraprec( gexpo(C0) + 64 ) - gprecision(C0);
    4434           0 :       if (precadd <= 0) precadd = 1;
    4435           0 :       precpb = "cleanarch";
    4436             :     }
    4437        7702 :   } while (need || precpb);
    4438             : 
    4439        1707 :   delete_cache(&cache); delete_FB(&F); free_GRHcheck(&GRHcheck);
    4440        1707 :   Vbase = vecpermute(F.LP, F.perm);
    4441        1707 :   class_group_gen(nf,W,C,Vbase,PRECREG,NULL, &clg1, &clg2);
    4442        1707 :   res = get_clfu(clg1, R, zu, fu);
    4443        1707 :   res = buchall_end(nf,res,clg2,W,B,A,C,Vbase);
    4444        1707 :   res = gerepilecopy(av0, res); if (precdouble) gunclone(nf);
    4445        1707 :   return res;
    4446             : }

Generated by: LCOV version 1.11