Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

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

Generated by: LCOV version 1.11