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

Generated by: LCOV version 1.11