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

Generated by: LCOV version 1.13