Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - buch2.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 20916-a74d914) Lines: 2332 2493 93.5 %
Date: 2017-08-18 06:23:59 Functions: 143 151 94.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11