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

Generated by: LCOV version 1.11