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 to exceed 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.18.1 lcov report (development 30530-11e908049b) Lines: 2215 2530 87.5 %
Date: 2025-10-13 09:22:58 Functions: 159 176 90.3 %
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; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : #define DEBUGLEVEL DEBUGLEVEL_bnf
      18             : 
      19             : /*******************************************************************/
      20             : /*                                                                 */
      21             : /*         CLASS GROUP AND REGULATOR (McCURLEY, BUCHMANN)          */
      22             : /*                    GENERAL NUMBER FIELDS                        */
      23             : /*                                                                 */
      24             : /*******************************************************************/
      25             : /* get_random_ideal */
      26             : static const long RANDOM_BITS = 4;
      27             : /* Buchall */
      28             : static const long RELSUP = 5;
      29             : static const long FAIL_DIVISOR = 32;
      30             : static const long MINFAIL = 10;
      31             : /* small_norm */
      32             : static const long BNF_RELPID = 4;
      33             : static const long MAXTRY_FACT = 500;
      34             : /* rnd_rel */
      35             : static const long RND_REL_RELPID = 1;
      36             : /* random relations */
      37             : static const long MINSFB = 3;
      38             : static const long SFB_MAX = 3;
      39             : static const long DEPSIZESFBMULT = 16;
      40             : static const long DEPSFBDIV = 10;
      41             : /* add_rel_i */
      42             : static const ulong mod_p = 27449UL;
      43             : /* be_honest */
      44             : static const long maxtry_HONEST = 50;
      45             : 
      46             : typedef struct FACT {
      47             :     long pr, ex;
      48             : } FACT;
      49             : 
      50             : typedef struct subFB_t {
      51             :   GEN subFB;
      52             :   struct subFB_t *old;
      53             : } subFB_t;
      54             : 
      55             : /* a factor base contains only noninert primes
      56             :  * KC = # of P in factor base (p <= n, NP <= n2)
      57             :  * KC2= # of P assumed to generate class group (NP <= n2)
      58             :  *
      59             :  * KCZ = # of rational primes under ideals counted by KC
      60             :  * KCZ2= same for KC2 */
      61             : 
      62             : typedef struct FB_t {
      63             :   GEN FB; /* FB[i] = i-th rational prime used in factor base */
      64             :   GEN LP; /* vector of all prime ideals in FB, by increasing norm */
      65             :   GEN LV; /* LV[p] = vector of P|p, NP <= n2
      66             :             * isclone() is set for LV[p] iff all P|p are in FB
      67             :             * LV[i], i not prime or i > n2, is undefined! */
      68             :   GEN iLP; /* iLP[p] = i such that LV[p] = [LP[i],...] */
      69             :   GEN L_jid; /* indexes of "useful" prime ideals for rnd_rel */
      70             :   long KC, KCZ, KCZ2;
      71             :   GEN prodZ; /* product of the primes in KCZ*/
      72             :   GEN subFB; /* LP o subFB =  part of FB used to build random relations */
      73             :   int sfb_chg; /* need to change subFB ? */
      74             :   GEN perm; /* permutation of LP used to represent relations [updated by
      75             :                hnfspec/hnfadd: dense rows come first] */
      76             :   GEN idealperm; /* permutation of ideals under field automorphisms */
      77             :   GEN minidx; /* minidx[i] min ideal in orbit of LP[i] under field autom */
      78             :   subFB_t *allsubFB; /* all subFB's used */
      79             :   GEN embperm; /* permutations of the complex embeddings */
      80             :   long MAXDEPSIZESFB; /* # trials before increasing subFB */
      81             :   long MAXDEPSFB; /* MAXDEPSIZESFB / DEPSFBDIV, # trials befor rotating subFB */
      82             :   double ballvol;
      83             : } FB_t;
      84             : 
      85             : enum { sfb_CHANGE = 1, sfb_INCREASE = 2 };
      86             : 
      87             : typedef struct REL_t {
      88             :   GEN R; /* relation vector as t_VECSMALL; clone */
      89             :   long nz; /* index of first nonzero elt in R (hash) */
      90             :   GEN m; /* pseudo-minimum yielding the relation; clone */
      91             :   long relorig; /* relation this one is an image of */
      92             :   long relaut; /* automorphim used to compute this relation from the original */
      93             :   GEN emb; /* archimedean embeddings */
      94             :   GEN junk[2]; /*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, *v, *y, *z;
     110             :   GEN x;
     111             : } FP_t;
     112             : 
     113             : static void
     114           0 : wr_rel(GEN e)
     115             : {
     116           0 :   long i, l = lg(e);
     117           0 :   for (i = 1; i < l; i++)
     118           0 :     if (e[i]) err_printf("%ld^%ld ",i,e[i]);
     119           0 : }
     120             : static void
     121           0 : dbg_newrel(RELCACHE_t *cache)
     122             : {
     123           0 :   if (DEBUGLEVEL > 1)
     124             :   {
     125           0 :     err_printf("\n++++ cglob = %ld\nrel = ", cache->last - cache->base);
     126           0 :     wr_rel(cache->last->R);
     127           0 :     err_printf("\n");
     128             :   }
     129             :   else
     130           0 :     err_printf("%ld ", cache->last - cache->base);
     131           0 : }
     132             : 
     133             : static void
     134       64099 : delete_cache(RELCACHE_t *M)
     135             : {
     136             :   REL_t *rel;
     137     1056751 :   for (rel = M->base+1; rel <= M->last; rel++)
     138             :   {
     139      992652 :     gunclone(rel->R);
     140      992651 :     if (rel->m) gunclone(rel->m);
     141             :   }
     142       64099 :   pari_free((void*)M->base); M->base = NULL;
     143       64099 : }
     144             : 
     145             : static void
     146       66274 : delete_FB(FB_t *F)
     147             : {
     148             :   subFB_t *s, *sold;
     149      133302 :   for (s = F->allsubFB; s; s = sold) { sold = s->old; pari_free(s); }
     150       66276 :   gunclone(F->minidx);
     151       66276 :   gunclone(F->idealperm);
     152       66276 : }
     153             : 
     154             : static void
     155       64098 : reallocate(RELCACHE_t *M, long len)
     156             : {
     157       64098 :   M->len = len;
     158       64098 :   if (!M->base)
     159       64098 :     M->base = (REL_t*)pari_malloc((len+1) * sizeof(REL_t));
     160             :   else
     161             :   {
     162           0 :     size_t l = M->last - M->base, c = M->chk - M->base, e = M->end - M->base;
     163           0 :     pari_realloc_ip((void**)&M->base, (len+1) * sizeof(REL_t));
     164           0 :     M->last = M->base + l;
     165           0 :     M->chk  = M->base + c;
     166           0 :     M->end  = M->base + e;
     167             :   }
     168       64099 : }
     169             : 
     170             : #define pr_get_smallp(pr) gel(pr,1)[2]
     171             : 
     172             : /* don't take P|p all other Q|p are already there */
     173             : static int
     174      307656 : bad_subFB(FB_t *F, long t)
     175             : {
     176      307656 :   GEN LP, P = gel(F->LP,t);
     177      307656 :   long p = pr_get_smallp(P);
     178      307656 :   LP = gel(F->LV,p);
     179      307656 :   return (isclone(LP) && t == F->iLP[p] + lg(LP)-1);
     180             : }
     181             : 
     182             : static void
     183       67030 : assign_subFB(FB_t *F, GEN yes, long iyes)
     184             : {
     185       67030 :   long i, lv = sizeof(subFB_t) + iyes*sizeof(long); /* for struct + GEN */
     186       67030 :   subFB_t *s = (subFB_t *)pari_malloc(lv);
     187       67029 :   s->subFB = (GEN)&s[1];
     188       67029 :   s->old = F->allsubFB; F->allsubFB = s;
     189      288925 :   for (i = 0; i < iyes; i++) s->subFB[i] = yes[i];
     190       67029 :   F->subFB = s->subFB;
     191       67029 :   F->MAXDEPSIZESFB = (iyes-1) * DEPSIZESFBMULT;
     192       67029 :   F->MAXDEPSFB = F->MAXDEPSIZESFB / DEPSFBDIV;
     193       67029 : }
     194             : 
     195             : /* Determine the permutation of the ideals made by each field automorphism */
     196             : static GEN
     197       66275 : FB_aut_perm(FB_t *F, GEN auts, GEN cyclic)
     198             : {
     199       66275 :   long i, j, m, KC = F->KC, nauts = lg(auts)-1;
     200       66275 :   GEN minidx, perm = zero_Flm_copy(KC, nauts);
     201             : 
     202       66275 :   if (!nauts) { F->minidx = gclone(identity_zv(KC)); return cgetg(1,t_MAT); }
     203       41985 :   minidx = zero_Flv(KC);
     204       91797 :   for (m = 1; m < lg(cyclic); m++)
     205             :   {
     206       49811 :     GEN thiscyc = gel(cyclic, m);
     207       49811 :     long k0 = thiscyc[1];
     208       49811 :     GEN aut = gel(auts, k0), permk0 = gel(perm, k0), ppermk;
     209       49811 :     i = 1;
     210      213887 :     while (i <= KC)
     211             :     {
     212      164075 :       pari_sp av2 = avma;
     213      164075 :       GEN seen = zero_Flv(KC), P = gel(F->LP, i);
     214      164077 :       long imin = i, p, f, l;
     215      164077 :       p = pr_get_smallp(P);
     216      164077 :       f = pr_get_f(P);
     217             :       do
     218             :       {
     219      480888 :         if (++i > KC) break;
     220      431076 :         P = gel(F->LP, i);
     221             :       }
     222      431076 :       while (p == pr_get_smallp(P) && f == pr_get_f(P));
     223      644946 :       for (j = imin; j < i; j++)
     224             :       {
     225      480885 :         GEN img = ZM_ZC_mul(aut, pr_get_gen(gel(F->LP, j)));
     226     1667580 :         for (l = imin; l < i; l++)
     227     1667580 :           if (!seen[l] && ZC_prdvd(img, gel(F->LP, l)))
     228             :           {
     229      480869 :             seen[l] = 1; permk0[j] = l; break;
     230             :           }
     231             :       }
     232      164061 :       set_avma(av2);
     233             :     }
     234       69001 :     for (ppermk = permk0, i = 2; i < lg(thiscyc); i++)
     235             :     {
     236       19189 :       GEN permk = gel(perm, thiscyc[i]);
     237      384218 :       for (j = 1; j <= KC; j++) permk[j] = permk0[ppermk[j]];
     238       19189 :       ppermk = permk;
     239             :     }
     240             :   }
     241      310370 :   for (j = 1; j <= KC; j++)
     242             :   {
     243      268384 :     if (minidx[j]) continue;
     244      129189 :     minidx[j] = j;
     245      361095 :     for (i = 1; i <= nauts; i++) minidx[coeff(perm, j, i)] = j;
     246             :   }
     247       41986 :   F->minidx = gclone(minidx); return perm;
     248             : }
     249             : 
     250             : /* set subFB.
     251             :  * Fill F->perm (if != NULL): primes ideals sorted by increasing norm (except
     252             :  * the ones in subFB come first [dense rows for hnfspec]) */
     253             : static void
     254       66276 : subFBgen(FB_t *F, GEN auts, GEN cyclic, double PROD, long minsFB)
     255             : {
     256             :   GEN y, perm, yes, no;
     257       66276 :   long i, j, k, iyes, ino, lv = F->KC + 1;
     258             :   double prod;
     259             :   pari_sp av;
     260             : 
     261       66276 :   F->LP   = cgetg(lv, t_VEC);
     262       66276 :   F->L_jid = F->perm = cgetg(lv, t_VECSMALL);
     263       66276 :   av = avma;
     264       66276 :   y = cgetg(lv,t_COL); /* Norm P */
     265      313218 :   for (k=0, i=1; i <= F->KCZ; i++)
     266             :   {
     267      246942 :     GEN LP = gel(F->LV,F->FB[i]);
     268      246942 :     long l = lg(LP);
     269      713772 :     for (j = 1; j < l; j++)
     270             :     {
     271      466829 :       GEN P = gel(LP,j);
     272      466829 :       k++;
     273      466829 :       gel(y,k) = pr_norm(P);
     274      466830 :       gel(F->LP,k) = P;
     275             :     }
     276             :   }
     277             :   /* perm sorts LP by increasing norm */
     278       66276 :   perm = indexsort(y);
     279       66275 :   no  = cgetg(lv, t_VECSMALL); ino  = 1;
     280       66275 :   yes = cgetg(lv, t_VECSMALL); iyes = 1;
     281       66275 :   prod = 1.0;
     282      303565 :   for (i = 1; i < lv; i++)
     283             :   {
     284      273579 :     long t = perm[i];
     285      273579 :     if (bad_subFB(F, t)) { no[ino++] = t; continue; }
     286             : 
     287      153066 :     yes[iyes++] = t;
     288      153066 :     prod *= (double)itos(gel(y,t));
     289      153065 :     if (iyes > minsFB && prod > PROD) break;
     290             :   }
     291       66274 :   setlg(yes, iyes);
     292      219343 :   for (j=1; j<iyes; j++)     F->perm[j] = yes[j];
     293      186788 :   for (i=1; i<ino; i++, j++) F->perm[j] =  no[i];
     294      259539 :   for (   ; j<lv; j++)       F->perm[j] =  perm[j];
     295       66275 :   F->allsubFB = NULL;
     296       66275 :   F->idealperm = gclone(FB_aut_perm(F, auts, cyclic));
     297       66276 :   if (iyes) assign_subFB(F, yes, iyes);
     298       66275 :   set_avma(av);
     299       66275 : }
     300             : static int
     301       26420 : subFB_change(FB_t *F)
     302             : {
     303       26420 :   long i, iyes, minsFB, lv = F->KC + 1, l = lg(F->subFB)-1;
     304       26420 :   pari_sp av = avma;
     305       26420 :   GEN yes, L_jid = F->L_jid, present = zero_zv(lv-1);
     306             : 
     307       26420 :   switch (F->sfb_chg)
     308             :   {
     309          14 :     case sfb_INCREASE: minsFB = l + 1; break;
     310       26406 :     default: minsFB = l; break;
     311             :   }
     312             : 
     313       26420 :   yes = cgetg(minsFB+1, t_VECSMALL); iyes = 1;
     314       26420 :   if (L_jid)
     315             :   {
     316       33988 :     for (i = 1; i < lg(L_jid); i++)
     317             :     {
     318       33850 :       long l = L_jid[i];
     319       33850 :       if (bad_subFB(F, l)) continue;
     320       31624 :       yes[iyes++] = l;
     321       31624 :       present[l] = 1;
     322       31624 :       if (iyes > minsFB) break;
     323             :     }
     324             :   }
     325           0 :   else i = 1;
     326       26420 :   if (iyes <= minsFB)
     327             :   {
     328         311 :     for ( ; i < lv; i++)
     329             :     {
     330         234 :       long l = F->perm[i];
     331         234 :       if (present[l] || bad_subFB(F, l)) continue;
     332          70 :       yes[iyes++] = l;
     333          70 :       if (iyes > minsFB) break;
     334             :     }
     335         138 :     if (i == lv) return 0;
     336             :   }
     337       26343 :   if (zv_equal(F->subFB, yes))
     338             :   {
     339       25589 :     if (DEBUGLEVEL) err_printf("\n*** NOT Changing sub factor base\n");
     340             :   }
     341             :   else
     342             :   {
     343         754 :     if (DEBUGLEVEL) err_printf("\n*** Changing sub factor base\n");
     344         754 :     assign_subFB(F, yes, iyes);
     345             :   }
     346       26343 :   F->sfb_chg = 0; return gc_bool(av, 1);
     347             : }
     348             : 
     349             : /* make sure enough room to store n more relations */
     350             : static void
     351      122453 : pre_allocate(RELCACHE_t *cache, size_t n)
     352             : {
     353      122453 :   size_t len = (cache->last - cache->base) + n;
     354      122453 :   if (len >= cache->len) reallocate(cache, len << 1);
     355      122453 : }
     356             : 
     357             : void
     358      134686 : init_GRHcheck(GRHcheck_t *S, long N, long R1, double LOGD)
     359             : {
     360      134686 :   const double c1 = M_PI*M_PI/2;
     361      134686 :   const double c2 = 3.663862376709;
     362      134686 :   const double c3 = 3.801387092431; /* Euler + log(8*Pi)*/
     363      134686 :   S->clone = 0;
     364      134686 :   S->cN = R1*c2 + N*c1;
     365      134686 :   S->cD = LOGD - N*c3 - R1*M_PI/2;
     366      134686 :   S->maxprimes = 16000; /* sufficient for LIMC=176081*/
     367      134686 :   S->primes = (GRHprime_t*)pari_malloc(S->maxprimes*sizeof(*S->primes));
     368      134687 :   S->nprimes = 0;
     369      134687 :   S->limp = 0;
     370      134687 :   u_forprime_init(&S->P, 2, ULONG_MAX);
     371      134685 : }
     372             : 
     373             : void
     374      134682 : free_GRHcheck(GRHcheck_t *S)
     375             : {
     376      134682 :   if (S->clone)
     377             :   {
     378       64022 :     long i = S->nprimes;
     379             :     GRHprime_t *pr;
     380     7578282 :     for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--) gunclone(pr->dec);
     381             :   }
     382      134672 :   pari_free(S->primes);
     383      134687 : }
     384             : 
     385             : int
     386     1532905 : GRHok(GRHcheck_t *S, double L, double SA, double SB)
     387             : {
     388     1532905 :   return (S->cD + (S->cN + 2*SB) / L - 2*SA < -1e-8);
     389             : }
     390             : 
     391             : /* Return factorization pattern of p: [f,n], where n[i] primes of
     392             :  * residue degree f[i] */
     393             : static GEN
     394     7512189 : get_fs(GEN nf, GEN P, GEN index, ulong p)
     395             : {
     396             :   long j, k, f, n, l;
     397             :   GEN fs, ns;
     398             : 
     399     7512189 :   if (umodiu(index, p))
     400             :   { /* easy case: p does not divide index */
     401     7473244 :     GEN F = Flx_degfact(ZX_to_Flx(P,p), p);
     402     7473920 :     fs = gel(F,1); l = lg(fs);
     403             :   }
     404             :   else
     405             :   {
     406       38683 :     GEN F = idealprimedec(nf, utoipos(p));
     407       38714 :     l = lg(F);
     408       38714 :     fs = cgetg(l, t_VECSMALL);
     409      121184 :     for (j = 1; j < l; j++) fs[j] = pr_get_f(gel(F,j));
     410             :   }
     411     7512634 :   ns = cgetg(l, t_VECSMALL);
     412     7510886 :   f = fs[1]; n = 1;
     413    13911782 :   for (j = 2, k = 1; j < l; j++)
     414     6400896 :     if (fs[j] == f)
     415     4676182 :       n++;
     416             :     else
     417             :     {
     418     1724714 :       ns[k] = n; fs[k] = f; k++;
     419     1724714 :       f = fs[j]; n = 1;
     420             :     }
     421     7510886 :   ns[k] = n; fs[k] = f; k++;
     422     7510886 :   setlg(fs, k);
     423     7510357 :   setlg(ns, k); return mkvec2(fs,ns);
     424             : }
     425             : 
     426             : /* cache data for all rational primes up to the LIM */
     427             : static void
     428      921253 : cache_prime_dec(GRHcheck_t *S, ulong LIM, GEN nf)
     429             : {
     430      921253 :   pari_sp av = avma;
     431             :   GRHprime_t *pr;
     432             :   GEN index, P;
     433             :   double nb;
     434             : 
     435      921253 :   if (S->limp >= LIM) return;
     436      329644 :   S->clone = 1;
     437      329644 :   nb = primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
     438      329656 :   GRH_ensure(S, nb+1); /* room for one extra prime */
     439      329656 :   P = nf_get_pol(nf);
     440      329655 :   index = nf_get_index(nf);
     441      329655 :   for (pr = S->primes + S->nprimes;;)
     442     7182847 :   {
     443     7512502 :     ulong p = u_forprime_next(&(S->P));
     444     7512231 :     pr->p = p;
     445     7512231 :     pr->logp = log((double)p);
     446     7512231 :     pr->dec = gclone(get_fs(nf, P, index, p));
     447     7512643 :     S->nprimes++;
     448     7512643 :     pr++;
     449     7512643 :     set_avma(av);
     450             :     /* store up to nextprime(LIM) included */
     451     7512503 :     if (p >= LIM) { S->limp = p; break; }
     452             :   }
     453             : }
     454             : 
     455             : static double
     456     2259289 : tailresback(long R1, long R2, double rK, long C, double C2, double C3, double r1K, double r2K, double logC, double logC2, double logC3)
     457             : {
     458     2259289 :   const double  rQ = 1.83787706641;
     459     2259289 :   const double r1Q = 1.98505372441;
     460     2259289 :   const double r2Q = 1.07991541347;
     461     4518578 :   return fabs((R1+R2-1)*(12*logC3+4*logC2-9*logC-6)/(2*C*logC3)
     462     2259289 :          + (rK-rQ)*(6*logC2 + 5*logC + 2)/(C*logC3)
     463     2259289 :          - R2*(6*logC2+11*logC+6)/(C2*logC2)
     464     2259289 :          - 2*(r1K-r1Q)*(3*logC2 + 4*logC + 2)/(C2*logC3)
     465     2259289 :          + (R1+R2-1)*(12*logC3+40*logC2+45*logC+18)/(6*C3*logC3)
     466     2259289 :          + (r2K-r2Q)*(2*logC2 + 3*logC + 2)/(C3*logC3));
     467             : }
     468             : 
     469             : static double
     470     1129660 : tailres(long R1, long R2, double al2K, double rKm, double rKM, double r1Km,
     471             :         double r1KM, double r2Km, double r2KM, double C, long i)
     472             : {
     473             :   /* C >= 3*2^i, lower bound for eint1(log(C)/2) */
     474             :   /* for(i=0,30,print(eint1(log(3*2^i)/2))) */
     475             :   static double tab[] = {
     476             :     0.50409264803,
     477             :     0.26205336997,
     478             :     0.14815491171,
     479             :     0.08770540561,
     480             :     0.05347651832,
     481             :     0.03328934284,
     482             :     0.02104510690,
     483             :     0.01346475900,
     484             :     0.00869778586,
     485             :     0.00566279855,
     486             :     0.00371111950,
     487             :     0.00244567837,
     488             :     0.00161948049,
     489             :     0.00107686891,
     490             :     0.00071868750,
     491             :     0.00048119961,
     492             :     0.00032312188,
     493             :     0.00021753772,
     494             :     0.00014679818,
     495             :     9.9272855581E-5,
     496             :     6.7263969995E-5,
     497             :     4.5656812967E-5,
     498             :     3.1041124593E-5,
     499             :     2.1136011590E-5,
     500             :     1.4411645381E-5,
     501             :     9.8393304088E-6,
     502             :     6.7257395409E-6,
     503             :     4.6025878272E-6,
     504             :     3.1529719271E-6,
     505             :     2.1620490021E-6,
     506             :     1.4839266071E-6
     507             :   };
     508     1129660 :   const double logC = log(C), logC2 = logC*logC, logC3 = logC*logC2;
     509     1129660 :   const double C2 = C*C, C3 = C*C2;
     510     1129660 :   double E1 = i >30? 0: tab[i];
     511     1129660 :   return al2K*((33*logC2+22*logC+8)/(8*logC3*sqrt(C))+15*E1/16)
     512     1129660 :     + maxdd(tailresback(rKm,r1KM,r2Km, C,C2,C3,R1,R2,logC,logC2,logC3),
     513     1129658 :             tailresback(rKM,r1Km,r2KM, C,C2,C3,R1,R2,logC,logC2,logC3))/2
     514     1129658 :     + ((R1+R2-1)*4*C+R2)*(C2+6*logC)/(4*C2*C2*logC2);
     515             : }
     516             : 
     517             : static long
     518       64022 : primeneeded(long N, long R1, long R2, double LOGD)
     519             : {
     520       64022 :   const double lim = 0.25; /* should be log(2)/2 == 0.34657... */
     521       64022 :   const double al2K =  0.3526*LOGD - 0.8212*N + 4.5007;
     522       64022 :   const double  rKm = -1.0155*LOGD + 2.1042*N - 8.3419;
     523       64022 :   const double  rKM = -0.5   *LOGD + 1.2076*N + 1;
     524       64022 :   const double r1Km = -       LOGD + 1.4150*N;
     525       64022 :   const double r1KM = -       LOGD + 1.9851*N;
     526       64022 :   const double r2Km = -       LOGD + 0.9151*N;
     527       64022 :   const double r2KM = -       LOGD + 1.0800*N;
     528       64022 :   long Cmin = 3, Cmax = 3, i = 0;
     529      574310 :   while (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, Cmax, i) > lim)
     530             :   {
     531      510288 :     Cmin = Cmax;
     532      510288 :     Cmax *= 2;
     533      510288 :     i++;
     534             :   }
     535       64020 :   i--;
     536      619386 :   while (Cmax - Cmin > 1)
     537             :   {
     538      555365 :     long t = (Cmin + Cmax)/2;
     539      555365 :     if (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, t, i) > lim)
     540      344348 :       Cmin = t;
     541             :     else
     542      211018 :       Cmax = t;
     543             :   }
     544       64021 :   return Cmax;
     545             : }
     546             : 
     547             : /* ~ 1 / Res(s = 1, zeta_K) */
     548             : static GEN
     549       64022 : compute_invres(GRHcheck_t *S, long LIMC)
     550             : {
     551       64022 :   pari_sp av = avma;
     552       64022 :   double loginvres = 0.;
     553             :   GRHprime_t *pr;
     554             :   long i;
     555       64022 :   double logLIMC = log((double)LIMC);
     556       64022 :   double logLIMC2 = logLIMC*logLIMC, denc;
     557             :   double c0, c1, c2;
     558       64022 :   denc = 1/(pow((double)LIMC, 3.) * logLIMC * logLIMC2);
     559       64022 :   c2 = (    logLIMC2 + 3 * logLIMC / 2 + 1) * denc;
     560       64022 :   denc *= LIMC;
     561       64022 :   c1 = (3 * logLIMC2 + 4 * logLIMC     + 2) * denc;
     562       64022 :   denc *= LIMC;
     563       64022 :   c0 = (3 * logLIMC2 + 5 * logLIMC / 2 + 1) * denc;
     564     7521722 :   for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--)
     565             :   {
     566             :     GEN dec, fs, ns;
     567             :     long addpsi;
     568             :     double addpsi1, addpsi2;
     569     7513812 :     double logp = pr->logp, NPk;
     570     7513812 :     long j, k, limp = logLIMC/logp;
     571     7513812 :     ulong p = pr->p, p2 = p*p;
     572     7513812 :     if (limp < 1) break;
     573     7457700 :     dec = pr->dec;
     574     7457700 :     fs = gel(dec, 1); ns = gel(dec, 2);
     575     7457700 :     loginvres += 1./p;
     576             :     /* NB: limp = 1 nearly always and limp > 2 for very few primes */
     577     8824362 :     for (k=2, NPk = p; k <= limp; k++) { NPk *= p; loginvres += 1/(k * NPk); }
     578     7457700 :     addpsi = limp;
     579     7457700 :     addpsi1 = p *(pow((double)p , (double)limp)-1)/(p -1);
     580     7457700 :     addpsi2 = p2*(pow((double)p2, (double)limp)-1)/(p2-1);
     581     7457700 :     j = lg(fs);
     582    16628908 :     while (--j > 0)
     583             :     {
     584             :       long f, nb, kmax;
     585             :       double NP, NP2, addinvres;
     586     9171208 :       f = fs[j]; if (f > limp) continue;
     587     3983356 :       nb = ns[j];
     588     3983356 :       NP = pow((double)p, (double)f);
     589     3983356 :       addinvres = 1/NP;
     590     3983356 :       kmax = limp / f;
     591     4860520 :       for (k=2, NPk = NP; k <= kmax; k++) { NPk *= NP; addinvres += 1/(k*NPk); }
     592     3983356 :       NP2 = NP*NP;
     593     3983356 :       loginvres -= nb * addinvres;
     594     3983356 :       addpsi -= nb * f * kmax;
     595     3983356 :       addpsi1 -= nb*(f*NP *(pow(NP ,(double)kmax)-1)/(NP -1));
     596     3983356 :       addpsi2 -= nb*(f*NP2*(pow(NP2,(double)kmax)-1)/(NP2-1));
     597             :     }
     598     7457700 :     loginvres -= (addpsi*c0 - addpsi1*c1 + addpsi2*c2)*logp;
     599             :   }
     600       64022 :   return gc_leaf(av, mpexp(dbltor(loginvres)));
     601             : }
     602             : 
     603             : static long
     604       64022 : nthideal(GRHcheck_t *S, GEN nf, long n)
     605             : {
     606       64022 :   pari_sp av = avma;
     607       64022 :   GEN P = nf_get_pol(nf);
     608       64022 :   ulong p = 0, *vecN = (ulong*)const_vecsmall(n, LONG_MAX);
     609       64022 :   long i, N = poldegree(P, -1);
     610       64022 :   for (i = 0; ; i++)
     611      230546 :   {
     612             :     GRHprime_t *pr;
     613             :     GEN fs;
     614      294568 :     cache_prime_dec(S, p+1, nf);
     615      294568 :     pr = S->primes + i;
     616      294568 :     fs = gel(pr->dec, 1);
     617      294568 :     p = pr->p;
     618      294568 :     if (fs[1] != N)
     619             :     {
     620      198020 :       GEN ns = gel(pr->dec, 2);
     621      198020 :       long k, l, j = lg(fs);
     622      443738 :       while (--j > 0)
     623             :       {
     624      245718 :         ulong NP = upowuu(p, fs[j]);
     625             :         long nf;
     626      245718 :         if (!NP) continue;
     627      754249 :         for (k = 1; k <= n; k++) if (vecN[k] > NP) break;
     628      245326 :         if (k > n) continue;
     629             :         /* vecN[k] <= NP */
     630      158910 :         nf = ns[j]; /*#{primes of norme NP} = nf, insert them here*/
     631      355678 :         for (l = k+nf; l <= n; l++) vecN[l] = vecN[l-nf];
     632      401672 :         for (l = 0; l < nf && k+l <= n; l++) vecN[k+l] = NP;
     633      365261 :         while (l <= k) vecN[l++] = NP;
     634             :       }
     635             :     }
     636      294568 :     if (p > vecN[n]) break;
     637             :   }
     638       64022 :   return gc_long(av, vecN[n]);
     639             : }
     640             : 
     641             : /* volume of unit ball in R^n: \pi^{n/2} / \Gamma(n/2 + 1) */
     642             : static double
     643       66276 : ballvol(long n)
     644             : {
     645       66276 :   double v = odd(n)? 2: 1;
     646      151585 :   for (; n > 1; n -= 2) v *= (2 * M_PI) / n;
     647       66276 :   return v;
     648             : }
     649             : 
     650             : /* Compute FB, LV, iLP + KC*. Reset perm
     651             :  * C2: bound for norm of tested prime ideals (includes be_honest())
     652             :  * C1: bound for p, such that P|p (NP <= C2) used to build relations */
     653             : static void
     654       66272 : FBgen(FB_t *F, GEN nf, long N, ulong C1, ulong C2, GRHcheck_t *S)
     655             : {
     656             :   GRHprime_t *pr;
     657             :   long i, ip;
     658             :   GEN prim;
     659       66272 :   const double L = log((double)C2 + 0.5);
     660             : 
     661       66272 :   cache_prime_dec(S, C2, nf);
     662       66272 :   pr = S->primes;
     663       66272 :   F->sfb_chg = 0;
     664       66272 :   F->FB  = cgetg(C2+1, t_VECSMALL);
     665       66274 :   F->iLP = cgetg(C2+1, t_VECSMALL);
     666       66274 :   F->LV = zerovec(C2);
     667             : 
     668       66274 :   prim = icopy(gen_1);
     669       66272 :   i = ip = 0;
     670       66272 :   F->KC = F->KCZ = 0;
     671      437327 :   for (;; pr++) /* p <= C2 */
     672      437327 :   {
     673      503599 :     ulong p = pr->p;
     674             :     long k, l, m;
     675             :     GEN LP, nb, f;
     676             : 
     677      503599 :     if (!F->KC && p > C1) { F->KCZ = i; F->KC = ip; }
     678      503599 :     if (p > C2) break;
     679             : 
     680      466373 :     if (DEBUGLEVEL>1) err_printf(" %ld",p);
     681             : 
     682      466377 :     f = gel(pr->dec, 1); nb = gel(pr->dec, 2);
     683      466377 :     if (f[1] == N)
     684             :     {
     685      146371 :       if (p == C2) break;
     686      137824 :       continue; /* p inert */
     687             :     }
     688      320006 :     l = (long)(L/pr->logp); /* p^f <= C2  <=> f <= l */
     689      583671 :     for (k=0, m=1; m < lg(f) && f[m]<=l; m++) k += nb[m];
     690      320006 :     if (!k)
     691             :     { /* too inert to appear in FB */
     692       73050 :       if (p == C2) break;
     693       72189 :       continue;
     694             :     }
     695      246956 :     prim[2] = p; LP = idealprimedec_limit_f(nf,prim, l);
     696             :     /* keep noninert ideals with Norm <= C2 */
     697      246956 :     if (m == lg(f)) setisclone(LP); /* flag it: all prime divisors in FB */
     698      246956 :     F->FB[++i]= p;
     699      246956 :     gel(F->LV,p) = LP;
     700      246956 :     F->iLP[p] = ip; ip += k;
     701      246956 :     if (p == C2) break;
     702             :   }
     703       66276 :   if (!F->KC) { F->KCZ = i; F->KC = ip; }
     704             :   /* Note F->KC > 0 otherwise GRHchk is false */
     705       66276 :   setlg(F->FB, F->KCZ+1); F->KCZ2 = i;
     706       66276 :   F->prodZ = zv_prod_Z(F->FB);
     707       66276 :   if (DEBUGLEVEL>1)
     708             :   {
     709           0 :     err_printf("\n");
     710           0 :     if (DEBUGLEVEL>6)
     711             :     {
     712           0 :       err_printf("########## FACTORBASE ##########\n\n");
     713           0 :       err_printf("KC2=%ld, KC=%ld, KCZ=%ld, KCZ2=%ld\n",
     714             :                   ip, F->KC, F->KCZ, F->KCZ2);
     715           0 :       for (i=1; i<=F->KCZ; i++) err_printf("++ LV[%ld] = %Ps",i,gel(F->LV,F->FB[i]));
     716             :     }
     717             :   }
     718       66276 :   F->perm = NULL; F->L_jid = NULL;
     719       66276 :   F->ballvol = ballvol(nf_get_degree(nf));
     720       66276 : }
     721             : 
     722             : static int
     723      496404 : GRHchk(GEN nf, GRHcheck_t *S, ulong LIMC)
     724             : {
     725      496404 :   double logC = log((double)LIMC), SA = 0, SB = 0;
     726      496404 :   GRHprime_t *pr = S->primes;
     727             : 
     728      496404 :   cache_prime_dec(S, LIMC, nf);
     729      496407 :   for (pr = S->primes;; pr++)
     730     3072966 :   {
     731     3569373 :     ulong p = pr->p;
     732             :     GEN dec, fs, ns;
     733             :     double logCslogp;
     734             :     long j;
     735             : 
     736     3569373 :     if (p > LIMC) break;
     737     3179390 :     dec = pr->dec; fs = gel(dec, 1); ns = gel(dec,2);
     738     3179390 :     logCslogp = logC/pr->logp;
     739     5006697 :     for (j = 1; j < lg(fs); j++)
     740             :     {
     741     3917624 :       long f = fs[j], M, nb;
     742             :       double logNP, q, A, B;
     743     3917624 :       if (f > logCslogp) break;
     744     1827305 :       logNP = f * pr->logp;
     745     1827305 :       q = 1/sqrt((double)upowuu(p, f));
     746     1827307 :       A = logNP * q; B = logNP * A; M = (long)(logCslogp/f);
     747     1827307 :       if (M > 1)
     748             :       {
     749      376451 :         double inv1_q = 1 / (1-q);
     750      376451 :         A *= (1 - pow(q, (double)M)) * inv1_q;
     751      376451 :         B *= (1 - pow(q, (double)M)*(M+1 - M*q)) * inv1_q * inv1_q;
     752             :       }
     753     1827307 :       nb = ns[j];
     754     1827307 :       SA += nb * A;
     755     1827307 :       SB += nb * B;
     756             :     }
     757     3179392 :     if (p == LIMC) break;
     758             :   }
     759      496409 :   return GRHok(S, logC, SA, SB);
     760             : }
     761             : 
     762             : /*  SMOOTH IDEALS */
     763             : static void
     764     9210325 : store(long i, long e, FACT *fact)
     765             : {
     766     9210325 :   ++fact[0].pr;
     767     9210325 :   fact[fact[0].pr].pr = i; /* index */
     768     9210325 :   fact[fact[0].pr].ex = e; /* exponent */
     769     9210325 : }
     770             : 
     771             : /* divide out m by all P|p, k = v_p(Nm) */
     772             : static int
     773        2437 : divide_p_elt(GEN LP, long ip, long k, GEN m, FACT *fact)
     774             : {
     775        2437 :   long j, l = lg(LP);
     776        3375 :   for (j=1; j<l; j++)
     777             :   {
     778        3375 :     GEN P = gel(LP,j);
     779        3375 :     long v = ZC_nfval(m, P);
     780        3375 :     if (!v) continue;
     781        2918 :     store(ip + j, v, fact); /* v = v_P(m) > 0 */
     782        2918 :     k -= v * pr_get_f(P);
     783        2918 :     if (!k) return 1;
     784             :   }
     785           0 :   return 0;
     786             : }
     787             : /* divide out I by all P|p, k = v_p(NI) */
     788             : static int
     789      185395 : divide_p_id(GEN LP, long ip, long k, GEN nf, GEN I, FACT *fact)
     790             : {
     791      185395 :   long j, l = lg(LP);
     792      278011 :   for (j=1; j<l; j++)
     793             :   {
     794      270149 :     GEN P = gel(LP,j);
     795      270149 :     long v = idealval(nf,I, P);
     796      270149 :     if (!v) continue;
     797      181032 :     store(ip + j, v, fact); /* v = v_P(I) > 0 */
     798      181032 :     k -= v * pr_get_f(P);
     799      181032 :     if (!k) return 1;
     800             :   }
     801        7862 :   return 0;
     802             : }
     803             : /* divide out m/I by all P|p, k = v_p(Nm/NI) */
     804             : static int
     805     5286663 : divide_p_quo(GEN LP, long ip, long k, GEN nf, GEN I, GEN m, FACT *fact)
     806             : {
     807     5286663 :   long j, l = lg(LP);
     808    17414166 :   for (j=1; j<l; j++)
     809             :   {
     810    17325435 :     GEN P = gel(LP,j);
     811    17325435 :     long v = ZC_nfval(m, P);
     812    17324342 :     if (!v) continue;
     813     7870926 :     v -= idealval(nf,I, P);
     814     7872286 :     if (!v) continue;
     815     6706246 :     store(ip + j, v, fact); /* v = v_P(m / I) > 0 */
     816     6706296 :     k -= v * pr_get_f(P);
     817     6706200 :     if (!k) return 1;
     818             :   }
     819       88731 :   return 0;
     820             : }
     821             : 
     822             : static int
     823     5474493 : divide_p(FB_t *F, long p, long k, GEN nf, GEN I, GEN m, FACT *fact)
     824             : {
     825     5474493 :   GEN LP = gel(F->LV,p);
     826     5474493 :   long ip = F->iLP[p];
     827     5474493 :   if (!m) return divide_p_id (LP,ip,k,nf,I,fact);
     828     5289098 :   if (!I) return divide_p_elt(LP,ip,k,m,fact);
     829     5286661 :   return divide_p_quo(LP,ip,k,nf,I,m,fact);
     830             : }
     831             : 
     832             : /* Let x = m if I == NULL,
     833             :  *         I if m == NULL,
     834             :  *         m/I otherwise.
     835             :  * Can we factor the integral primitive ideal x ? |N| = Norm x > 0 */
     836             : static long
     837    18220768 : can_factor(FB_t *F, GEN nf, GEN I, GEN m, GEN N, FACT *fact)
     838             : {
     839             :   GEN f, p, e;
     840             :   long i, l;
     841    18220768 :   fact[0].pr = 0;
     842    18220768 :   if (is_pm1(N)) return 1;
     843    17242009 :   if (!is_pm1(Z_ppo(N, F->prodZ))) return 0;
     844     2779883 :   f = absZ_factor(N); p = gel(f,1); e = gel(f,2); l = lg(p);
     845     8158146 :   for (i = 1; i < l; i++)
     846     5474480 :     if (!divide_p(F, itou(gel(p,i)), itou(gel(e,i)), nf, I, m, fact))
     847             :     {
     848       96312 :       if (DEBUGLEVEL > 1) err_printf(".");
     849       96312 :       return 0;
     850             :     }
     851     2683666 :   return 1;
     852             : }
     853             : 
     854             : /* can we factor m/I ? [m in I from idealpseudomin_nonscalar], NI = norm I */
     855             : static long
     856    16808509 : factorgen(FB_t *F, GEN nf, GEN I, GEN NI, GEN m, FACT *fact)
     857             : {
     858             :   long e;
     859    16808509 :   GEN Nm = embed_norm(RgM_RgC_mul(nf_get_M(nf),m), nf_get_r1(nf));
     860    16808570 :   GEN N = grndtoi(NI? divri(Nm, NI): Nm, &e); /* ~ N(m/I) */
     861    16808447 :   if (e > -32)
     862             :   {
     863           0 :     if (DEBUGLEVEL > 1) err_printf("+");
     864           0 :     return 0;
     865             :   }
     866    16808447 :   return can_factor(F, nf, I, m, N, fact);
     867             : }
     868             : 
     869             : /*  FUNDAMENTAL UNITS */
     870             : 
     871             : /* a, y real. Return  (Re(x) + a) + I * (Im(x) % y) */
     872             : static GEN
     873     6905463 : addRe_modIm(GEN x, GEN a, GEN y, GEN iy)
     874             : {
     875             :   GEN z;
     876     6905463 :   if (typ(x) == t_COMPLEX)
     877             :   {
     878     4904941 :     GEN re, im = modRr_i(gel(x,2), y, iy);
     879     4904919 :     if (!im) return NULL;
     880     4904919 :     re = gadd(gel(x,1), a);
     881     4904888 :     z = gequal0(im)? re: mkcomplex(re, im);
     882             :   }
     883             :   else
     884     2000522 :     z = gadd(x, a);
     885     6905391 :   return z;
     886             : }
     887             : static GEN
     888      204628 : modIm(GEN x, GEN y, GEN iy)
     889             : {
     890      204628 :   if (typ(x) == t_COMPLEX)
     891             :   {
     892      191467 :     GEN im = modRr_i(gel(x,2), y, iy);
     893      191465 :     if (!im) return NULL;
     894      191465 :     x = gequal0(im)? gel(x,1): mkcomplex(gel(x,1), im);
     895             :   }
     896      204624 :   return x;
     897             : }
     898             : 
     899             : /* clean archimedean components. ipi = 2^n / pi (n arbitrary); its
     900             :  * exponent may be modified */
     901             : static GEN
     902     3070742 : cleanarch(GEN x, long N, GEN ipi, long prec)
     903             : {
     904             :   long i, l, R1, RU;
     905     3070742 :   GEN s, y = cgetg_copy(x, &l);
     906             : 
     907     3070744 :   if (!ipi) ipi = invr(mppi(prec));
     908     3070744 :   if (typ(x) == t_MAT)
     909             :   {
     910      528665 :     for (i = 1; i < l; i++)
     911      464501 :       if (!(gel(y,i) = cleanarch(gel(x,i), N, ipi, prec))) return NULL;
     912       64164 :     return y;
     913             :   }
     914     3006569 :   RU = l-1; R1 = (RU<<1) - N;
     915     3006569 :   s = gdivgs(RgV_sum(real_i(x)), -N); /* -log |norm(x)| / N */
     916     3006529 :   i = 1;
     917     3006529 :   if (R1)
     918             :   {
     919     2523880 :     GEN pi2 = Pi2n(1, prec);
     920     2523893 :     setexpo(ipi, -3); /* 1/(2pi) */
     921     7790522 :     for (; i <= R1; i++)
     922     5266654 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s, pi2, ipi))) return NULL;
     923             :   }
     924     3006517 :   if (i <= RU)
     925             :   {
     926     1079534 :     GEN pi4 = Pi2n(2, prec), s2 = gmul2n(s, 1);
     927     1079548 :     setexpo(ipi, -4); /* 1/(4pi) */
     928     2718394 :     for (; i <= RU; i++)
     929     1638828 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s2, pi4, ipi))) return NULL;
     930             :   }
     931     3006549 :   return y;
     932             : }
     933             : GEN
     934      197126 : nf_cxlog_normalize(GEN nf, GEN x, long prec)
     935             : {
     936      197126 :   long N = nf_get_degree(nf);
     937      197126 :   return cleanarch(x, N, NULL, prec);
     938             : }
     939             : 
     940             : /* clean unit archimedean components. ipi = 2^n / pi (n arbitrary); its
     941             :  * exponent may be modified */
     942             : static GEN
     943      133895 : cleanarchunit(GEN x, long N, GEN ipi, long prec)
     944             : {
     945             :   long i, l, R1, RU;
     946      133895 :   GEN y = cgetg_copy(x, &l);
     947             : 
     948      133895 :   if (!ipi) ipi = invr(mppi(prec));
     949      133894 :   if (typ(x) == t_MAT)
     950             :   {
     951      133887 :     for (i = 1; i < l; i++)
     952       69865 :       if (!(gel(y,i) = cleanarchunit(gel(x,i), N, ipi, prec))) return NULL;
     953       64022 :     return y;
     954             :   }
     955       69866 :   if (gexpo(RgV_sum(real_i(x))) > -10) return NULL;
     956       69859 :   RU = l-1; R1 = (RU<<1) - N;
     957       69859 :   i = 1;
     958       69859 :   if (R1)
     959             :   {
     960       55376 :     GEN pi2 = Pi2n(1, prec);
     961       55377 :     setexpo(ipi, -3); /* 1/(2pi) */
     962      189315 :     for (; i <= R1; i++)
     963      133945 :       if (!(gel(y,i) = modIm(gel(x,i), pi2, ipi))) return NULL;
     964             :   }
     965       69853 :   if (i <= RU)
     966             :   {
     967       34452 :     GEN pi4 = Pi2n(2, prec);
     968       34452 :     setexpo(ipi, -4); /* 1/(4pi) */
     969      105141 :     for (; i <= RU; i++)
     970       70683 :       if (!(gel(y,i) = modIm(gel(x,i), pi4, ipi))) return NULL;
     971             :   }
     972       69859 :   return y;
     973             : }
     974             : 
     975             : static GEN
     976         396 : not_given(long reason)
     977             : {
     978         396 :   if (DEBUGLEVEL)
     979           0 :     switch(reason)
     980             :     {
     981           0 :       case fupb_LARGE:
     982           0 :         pari_warn(warner,"fundamental units too large, not given");
     983           0 :         break;
     984           0 :       case fupb_PRECI:
     985           0 :         pari_warn(warner,"insufficient precision for fundamental units, not given");
     986           0 :         break;
     987             :     }
     988         396 :   return NULL;
     989             : }
     990             : 
     991             : /* check whether exp(x) will 1) get too big (real(x) large), 2) require
     992             :  * large accuracy for argument reduction (imag(x) large) */
     993             : static long
     994     2840312 : expbitprec(GEN x, long *e)
     995             : {
     996             :   GEN re, im;
     997     2840312 :   if (typ(x) != t_COMPLEX) re = x;
     998             :   else
     999             :   {
    1000     1778642 :     im = gel(x,2); *e = maxss(*e, expo(im) + 5 - bit_prec(im));
    1001     1778642 :     re = gel(x,1);
    1002             :   }
    1003     2840312 :   return (expo(re) <= 20);
    1004             : 
    1005             : }
    1006             : static long
    1007     1237073 : RgC_expbitprec(GEN x)
    1008             : {
    1009     1237073 :   long l = lg(x), i, e = - (long)HIGHEXPOBIT;
    1010     3872285 :   for (i = 1; i < l; i++)
    1011     2635785 :     if (!expbitprec(gel(x,i), &e)) return LONG_MAX;
    1012     1236500 :   return e;
    1013             : }
    1014             : static long
    1015       48797 : RgM_expbitprec(GEN x)
    1016             : {
    1017       48797 :   long i, j, I, J, e = - (long)HIGHEXPOBIT;
    1018       48797 :   RgM_dimensions(x, &I,&J);
    1019      118587 :   for (j = 1; j <= J; j++)
    1020      274316 :     for (i = 1; i <= I; i++)
    1021      204526 :       if (!expbitprec(gcoeff(x,i,j), &e)) return LONG_MAX;
    1022       48734 :   return e;
    1023             : }
    1024             : 
    1025             : static GEN
    1026        1379 : FlxqX_chinese_unit(GEN X, GEN U, GEN invzk, GEN D, GEN T, ulong p)
    1027             : {
    1028        1379 :   long i, lU = lg(U), lX = lg(X), d = lg(invzk)-1;
    1029        1379 :   GEN M = cgetg(lU, t_MAT);
    1030        1379 :   if (D)
    1031             :   {
    1032        1274 :     D = Flv_inv(D, p);
    1033       69760 :     for (i = 1; i < lX; i++)
    1034       68486 :       if (uel(D, i) != 1)
    1035       56618 :         gel(X,i) = Flx_Fl_mul(gel(X,i), uel(D,i), p);
    1036             :   }
    1037        3878 :   for (i = 1; i < lU; i++)
    1038             :   {
    1039        2499 :     GEN H = FlxqV_factorback(X, gel(U, i), T, p);
    1040        2499 :     gel(M, i) = Flm_Flc_mul(invzk, Flx_to_Flv(H, d), p);
    1041             :   }
    1042        1379 :   return M;
    1043             : }
    1044             : 
    1045             : static GEN
    1046         274 : chinese_unit_slice(GEN A, GEN U, GEN B, GEN D, GEN C, GEN P, GEN *mod)
    1047             : {
    1048         274 :   pari_sp av = avma;
    1049         274 :   long i, n = lg(P)-1, v = varn(C);
    1050             :   GEN H, T;
    1051         274 :   if (n == 1)
    1052             :   {
    1053           0 :     ulong p = uel(P,1);
    1054           0 :     GEN a = ZXV_to_FlxV(A, p), b = ZM_to_Flm(B, p), c = ZX_to_Flx(C, p);
    1055           0 :     GEN d = D ? ZV_to_Flv(D, p): NULL;
    1056           0 :     GEN Hp = FlxqX_chinese_unit(a, U, b, d, c, p);
    1057           0 :     H = gc_upto(av, Flm_to_ZM(Hp));
    1058           0 :     *mod = utoi(p);
    1059           0 :     return H;
    1060             :   }
    1061         274 :   T = ZV_producttree(P);
    1062         274 :   A = ZXC_nv_mod_tree(A, P, T, v);
    1063         274 :   B = ZM_nv_mod_tree(B, P, T);
    1064         274 :   D = D ? ZV_nv_mod_tree(D, P, T): NULL;
    1065         274 :   C = ZX_nv_mod_tree(C, P, T);
    1066             : 
    1067         274 :   H = cgetg(n+1, t_VEC);
    1068        1653 :   for(i=1; i <= n; i++)
    1069             :   {
    1070        1379 :     ulong p = P[i];
    1071        1379 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C,i), d = D ? gel(D,i): NULL;
    1072        1379 :     gel(H,i) = FlxqX_chinese_unit(a, U, b, d, c, p);
    1073             :   }
    1074         274 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
    1075         274 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    1076             : }
    1077             : 
    1078             : GEN
    1079         274 : chinese_unit_worker(GEN P, GEN A, GEN U, GEN B, GEN D, GEN C)
    1080             : {
    1081         274 :   GEN V = cgetg(3, t_VEC);
    1082         274 :   gel(V,1) = chinese_unit_slice(A, U, B, isintzero(D) ? NULL: D, C, P, &gel(V,2));
    1083         274 :   return V;
    1084             : }
    1085             : 
    1086             : /* Let x = \prod X[i]^E[i] = u, return u.
    1087             :  * If dX != NULL, X[i] = nX[i] / dX[i] where nX[i] is a ZX, dX[i] in Z */
    1088             : static GEN
    1089          94 : chinese_unit(GEN nf, GEN nX, GEN dX, GEN U, ulong bnd)
    1090             : {
    1091          94 :   pari_sp av = avma;
    1092          94 :   GEN f = nf_get_index(nf), T = nf_get_pol(nf), invzk = nf_get_invzk(nf);
    1093             :   GEN H, mod;
    1094             :   forprime_t S;
    1095          94 :   GEN worker = snm_closure(is_entry("_chinese_unit_worker"),
    1096             :                mkcol5(nX, U, invzk, dX? dX: gen_0, T));
    1097          94 :   init_modular_big(&S);
    1098          94 :   H = gen_crt("chinese_units", worker, &S, f, bnd, 0, &mod, nmV_chinese_center, FpM_center);
    1099          94 :   settyp(H, t_VEC); return gc_GEN(av, H);
    1100             : }
    1101             : 
    1102             : /* *pE a ZM */
    1103             : static void
    1104         164 : ZM_remove_unused(GEN *pE, GEN *pX)
    1105             : {
    1106         164 :   long j, k, l = lg(*pX);
    1107         164 :   GEN E = *pE, v = cgetg(l, t_VECSMALL);
    1108       16395 :   for (j = k = 1; j < l; j++)
    1109       16231 :     if (!ZMrow_equal0(E, j)) v[k++] = j;
    1110         164 :   if (k < l)
    1111             :   {
    1112         164 :     setlg(v, k);
    1113         164 :     *pX = vecpermute(*pX,v);
    1114         164 :     *pE = rowpermute(E,v);
    1115             :   }
    1116         164 : }
    1117             : 
    1118             : /* s = -log|norm(x)|/N */
    1119             : static GEN
    1120     1306921 : fixarch(GEN x, GEN s, long R1)
    1121             : {
    1122             :   long i, l;
    1123     1306921 :   GEN y = cgetg_copy(x, &l);
    1124     3644816 :   for (i = 1; i <= R1; i++) gel(y,i) = gadd(s, gel(x,i));
    1125     1810016 :   for (     ; i <   l; i++) gel(y,i) = gadd(s, gmul2n(gel(x,i),-1));
    1126     1306932 :   return y;
    1127             : }
    1128             : 
    1129             : static GEN
    1130       64022 : getfu(GEN nf, GEN *ptA, GEN *ptU, long prec)
    1131             : {
    1132       64022 :   GEN U, y, matep, A, T = nf_get_pol(nf), M = nf_get_M(nf);
    1133       64022 :   long e, j, R1, RU, N = degpol(T);
    1134             : 
    1135       64022 :   R1 = nf_get_r1(nf); RU = (N+R1) >> 1;
    1136       64022 :   if (RU == 1) return cgetg(1,t_VEC);
    1137             : 
    1138       48797 :   A = *ptA;
    1139       48797 :   matep = cgetg(RU,t_MAT);
    1140      118656 :   for (j = 1; j < RU; j++)
    1141             :   {
    1142       69859 :     GEN Aj = gel(A,j), s = gdivgs(RgV_sum(real_i(Aj)), -N);
    1143       69858 :     gel(matep,j) = fixarch(Aj, s, R1);
    1144             :   }
    1145       48797 :   U = lll(real_i(matep));
    1146       48797 :   if (lg(U) < RU) return not_given(fupb_PRECI);
    1147       48797 :   if (ptU) { *ptU = U; *ptA = A = RgM_ZM_mul(A,U); }
    1148       48797 :   y = RgM_ZM_mul(matep,U);
    1149       48797 :   e = RgM_expbitprec(y);
    1150       48797 :   if (e >= 0) return not_given(e == LONG_MAX? fupb_LARGE: fupb_PRECI);
    1151       48734 :   if (prec <= 0) prec = gprecision(A);
    1152       48734 :   y = RgM_solve_realimag(M, gexp(y,prec));
    1153       48734 :   if (!y) return not_given(fupb_PRECI);
    1154       48734 :   y = grndtoi(y, &e); if (e >= 0) return not_given(fupb_PRECI);
    1155       48407 :   settyp(y, t_VEC);
    1156             : 
    1157       48407 :   if (!ptU) *ptA = A = RgM_ZM_mul(A, U);
    1158      117519 :   for (j = 1; j < RU; j++)
    1159             :   { /* y[i] are hopefully unit generators. Normalize: smallest T2 norm */
    1160       69117 :     GEN u = gel(y,j), v = zk_inv(nf, u);
    1161       69120 :     if (!v || !is_pm1(Q_denom(v)) || ZV_isscalar(u))
    1162           8 :       return not_given(fupb_PRECI);
    1163       69112 :     if (gcmp(RgC_fpnorml2(v,DEFAULTPREC), RgC_fpnorml2(u,DEFAULTPREC)) < 0)
    1164             :     {
    1165       29875 :       gel(A,j) = RgC_neg(gel(A,j));
    1166       29875 :       if (ptU) gel(U,j) = ZC_neg(gel(U,j));
    1167       29874 :       u = v;
    1168             :     }
    1169       69110 :     gel(y,j) = nf_to_scalar_or_alg(nf, u);
    1170             :   }
    1171       48402 :   return y;
    1172             : }
    1173             : 
    1174             : static void
    1175           0 : err_units() { pari_err_PREC("makeunits [cannot get units, use bnfinit(,1)]"); }
    1176             : 
    1177             : /* bound for log2 |sigma(u)|, sigma complex embedding, u fundamental unit
    1178             :  * attached to bnf_get_logfu */
    1179             : static double
    1180          94 : log2fubound(GEN bnf)
    1181             : {
    1182          94 :   GEN LU = bnf_get_logfu(bnf);
    1183          94 :   long i, j, l = lg(LU), r1 = nf_get_r1(bnf_get_nf(bnf));
    1184          94 :   double e = 0.0;
    1185         330 :   for (j = 1; j < l; j++)
    1186             :   {
    1187         236 :     GEN u = gel(LU,j);
    1188         624 :     for (i = 1; i <= r1; i++)
    1189             :     {
    1190         388 :       GEN E = real_i(gel(u,i));
    1191         388 :       e = maxdd(e, gtodouble(E));
    1192             :     }
    1193         842 :     for (     ; i <= l; i++)
    1194             :     {
    1195         606 :       GEN E = real_i(gel(u,i));
    1196         606 :       e = maxdd(e, gtodouble(E) / 2);
    1197             :     }
    1198             :   }
    1199          94 :   return e / M_LN2;
    1200             : }
    1201             : /* bound for log2(|RgM_solve_realimag(M, y)|_oo / |y|_oo)*/
    1202             : static double
    1203          94 : log2Mbound(GEN nf)
    1204             : {
    1205          94 :   GEN G = nf_get_G(nf), D = nf_get_disc(nf);
    1206          94 :   long r2 = nf_get_r2(nf), l = lg(G), i;
    1207          94 :   double e, d = dbllog2(D)/2 - r2 * M_LN2; /* log2 |det(split_realimag(M))| */
    1208          94 :   e = log2(nf_get_degree(nf));
    1209         535 :   for (i = 2; i < l; i++) e += dbllog2(gnorml2(gel(G,i))); /* Hadamard bound */
    1210          94 :   return e / 2 - d;
    1211             : }
    1212             : 
    1213             : static GEN
    1214          94 : vec_chinese_units(GEN bnf)
    1215             : {
    1216          94 :   GEN nf = bnf_get_nf(bnf), SUnits = bnf_get_sunits(bnf);
    1217          94 :   double bnd = ceil(log2Mbound(nf) + log2fubound(bnf));
    1218          94 :   GEN X, dX, Y, U, f = nf_get_index(nf);
    1219          94 :   long j, l, v = nf_get_varn(nf);
    1220          94 :   if (!SUnits) err_units(); /* no compact units */
    1221          94 :   Y = gel(SUnits,1);
    1222          94 :   U = gel(SUnits,2);
    1223          94 :   ZM_remove_unused(&U, &Y); l = lg(Y); X = cgetg(l, t_VEC);
    1224          94 :   if (is_pm1(f)) f = dX = NULL; else dX = cgetg(l, t_VEC);
    1225        5142 :   for (j = 1; j < l; j++)
    1226             :   {
    1227        5048 :     GEN t = nf_to_scalar_or_alg(nf, gel(Y,j));
    1228        5048 :     if (f)
    1229             :     {
    1230             :       GEN den;
    1231        4202 :       t = Q_remove_denom(t, &den);
    1232        4202 :       gel(dX,j) = den ? den: gen_1;
    1233             :     }
    1234        5048 :     gel(X,j) = typ(t) == t_INT? scalarpol_shallow(t,v): t;
    1235             :   }
    1236          94 :   if (dblexpo(bnd) >= BITS_IN_LONG)
    1237           0 :     pari_err_OVERFLOW("vec_chinese_units [units too large]");
    1238          94 :   return chinese_unit(nf, X, dX, U, (ulong)bnd);
    1239             : }
    1240             : 
    1241             : static GEN
    1242       24915 : makeunits(GEN bnf)
    1243             : {
    1244       24915 :   GEN nf = bnf_get_nf(bnf), fu = bnf_get_fu_nocheck(bnf);
    1245       24915 :   GEN tu = nf_to_scalar_or_basis(nf, bnf_get_tuU(bnf));
    1246       24915 :   fu = (typ(fu) == t_MAT)? vec_chinese_units(bnf): matalgtobasis(nf, fu);
    1247       24914 :   return vec_prepend(fu, tu);
    1248             : }
    1249             : 
    1250             : /*******************************************************************/
    1251             : /*                                                                 */
    1252             : /*           PRINCIPAL IDEAL ALGORITHM (DISCRETE LOG)              */
    1253             : /*                                                                 */
    1254             : /*******************************************************************/
    1255             : 
    1256             : /* G: prime ideals, E: vector of nonnegative exponents.
    1257             :  * C = possible extra prime (^1) or NULL
    1258             :  * Return Norm (product) */
    1259             : static GEN
    1260          69 : get_norm_fact_primes(GEN G, GEN E, GEN C)
    1261             : {
    1262          69 :   pari_sp av=avma;
    1263          69 :   GEN N = gen_1, P, p;
    1264          69 :   long i, c = lg(E);
    1265          69 :   for (i=1; i<c; i++)
    1266             :   {
    1267           0 :     GEN ex = gel(E,i);
    1268           0 :     long s = signe(ex);
    1269           0 :     if (!s) continue;
    1270             : 
    1271           0 :     P = gel(G,i); p = pr_get_p(P);
    1272           0 :     N = mulii(N, powii(p, mului(pr_get_f(P), ex)));
    1273             :   }
    1274          69 :   if (C) N = mulii(N, pr_norm(C));
    1275          69 :   return gc_INT(av, N);
    1276             : }
    1277             : 
    1278             : /* gen: HNF ideals */
    1279             : static GEN
    1280     1231459 : get_norm_fact(GEN gen, GEN ex, GEN *pd)
    1281             : {
    1282     1231459 :   long i, c = lg(ex);
    1283             :   GEN d,N,I,e,n,ne,de;
    1284     1231459 :   d = N = gen_1;
    1285     1528119 :   for (i=1; i<c; i++)
    1286      296660 :     if (signe(gel(ex,i)))
    1287             :     {
    1288      175539 :       I = gel(gen,i); e = gel(ex,i); n = ZM_det_triangular(I);
    1289      175539 :       ne = powii(n,e);
    1290      175539 :       de = equalii(n, gcoeff(I,1,1))? ne: powii(gcoeff(I,1,1), e);
    1291      175539 :       N = mulii(N, ne);
    1292      175539 :       d = mulii(d, de);
    1293             :     }
    1294     1231459 :   *pd = d; return N;
    1295             : }
    1296             : 
    1297             : static GEN
    1298     1392352 : get_pr_lists(GEN FB, long N, int list_pr)
    1299             : {
    1300             :   GEN pr, L;
    1301     1392352 :   long i, l = lg(FB), p, pmax;
    1302             : 
    1303     1392352 :   pmax = 0;
    1304     9543980 :   for (i=1; i<l; i++)
    1305             :   {
    1306     8151628 :     pr = gel(FB,i); p = pr_get_smallp(pr);
    1307     8151628 :     if (p > pmax) pmax = p;
    1308             :   }
    1309     1392352 :   L = const_vec(pmax, NULL);
    1310     1392354 :   if (list_pr)
    1311             :   {
    1312           0 :     for (i=1; i<l; i++)
    1313             :     {
    1314           0 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1315           0 :       if (!L[p]) gel(L,p) = vectrunc_init(N+1);
    1316           0 :       vectrunc_append(gel(L,p), pr);
    1317             :     }
    1318           0 :     for (p=1; p<=pmax; p++)
    1319           0 :       if (L[p]) gen_sort_inplace(gel(L,p), (void*)&cmp_prime_over_p,
    1320             :                                  &cmp_nodata, NULL);
    1321             :   }
    1322             :   else
    1323             :   {
    1324     9543983 :     for (i=1; i<l; i++)
    1325             :     {
    1326     8151628 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1327     8151628 :       if (!L[p]) gel(L,p) = vecsmalltrunc_init(N+1);
    1328     8151628 :       vecsmalltrunc_append(gel(L,p), i);
    1329             :     }
    1330             :   }
    1331     1392355 :   return L;
    1332             : }
    1333             : 
    1334             : /* recover FB, LV, iLP, KCZ from Vbase */
    1335             : static GEN
    1336     1392352 : recover_partFB(FB_t *F, GEN Vbase, long N)
    1337             : {
    1338     1392352 :   GEN FB, LV, iLP, L = get_pr_lists(Vbase, N, 0);
    1339     1392355 :   long l = lg(L), p, ip, i;
    1340             : 
    1341     1392355 :   i = ip = 0;
    1342     1392355 :   FB = cgetg(l, t_VECSMALL);
    1343     1392355 :   iLP= cgetg(l, t_VECSMALL);
    1344     1392355 :   LV = cgetg(l, t_VEC);
    1345    20376573 :   for (p = 2; p < l; p++)
    1346             :   {
    1347    18984218 :     if (!L[p]) continue;
    1348     4450403 :     FB[++i] = p;
    1349     4450403 :     gel(LV,p) = vecpermute(Vbase, gel(L,p));
    1350     4450403 :     iLP[p]= ip; ip += lg(gel(L,p))-1;
    1351             :   }
    1352     1392355 :   F->KCZ = i;
    1353     1392355 :   F->KC = ip;
    1354     1392355 :   F->FB = FB; setlg(FB, i+1);
    1355     1392355 :   F->prodZ = zv_prod_Z(F->FB);
    1356     1392352 :   F->LV = LV;
    1357     1392352 :   F->iLP= iLP; return L;
    1358             : }
    1359             : 
    1360             : /* add v^e to factorization */
    1361             : static void
    1362     2845229 : add_to_fact(long v, long e, FACT *fact)
    1363             : {
    1364     2845229 :   long i, n = fact[0].pr;
    1365     9618132 :   for (i=1; i<=n; i++)
    1366     7298014 :     if (fact[i].pr == v) { fact[i].ex += e; return; }
    1367     2320118 :   store(v, e, fact);
    1368             : }
    1369             : static void
    1370           0 : inv_fact(FACT *fact)
    1371             : {
    1372           0 :   long i, n = fact[0].pr;
    1373           0 :   for (i=1; i<=n; i++) fact[i].ex = -fact[i].ex;
    1374           0 : }
    1375             : 
    1376             : /* L (small) list of primes above the same p including pr. Return pr index */
    1377             : static int
    1378        3349 : pr_index(GEN L, GEN pr)
    1379             : {
    1380        3349 :   long j, l = lg(L);
    1381        3349 :   GEN al = pr_get_gen(pr);
    1382        3349 :   for (j=1; j<l; j++)
    1383        3349 :     if (ZV_equal(al, pr_get_gen(gel(L,j)))) return j;
    1384           0 :   pari_err_BUG("codeprime");
    1385             :   return 0; /* LCOV_EXCL_LINE */
    1386             : }
    1387             : 
    1388             : static long
    1389        3349 : Vbase_to_FB(FB_t *F, GEN pr)
    1390             : {
    1391        3349 :   long p = pr_get_smallp(pr);
    1392        3349 :   return F->iLP[p] + pr_index(gel(F->LV,p), pr);
    1393             : }
    1394             : 
    1395             : /* x, y 2 extended ideals whose first component is an integral HNF and second
    1396             :  * a famat */
    1397             : static GEN
    1398        3506 : idealHNF_mulred(GEN nf, GEN x, GEN y)
    1399             : {
    1400        3506 :   GEN A = idealHNF_mul(nf, gel(x,1), gel(y,1));
    1401        3506 :   GEN F = famat_mul_shallow(gel(x,2), gel(y,2));
    1402        3506 :   return idealred(nf, mkvec2(A, F));
    1403             : }
    1404             : /* idealred(x * pr^n), n > 0 is small, x extended ideal. Reduction in order to
    1405             :  * avoid prec pb: don't let id become too large as lgsub increases */
    1406             : static GEN
    1407        4488 : idealmulpowprime2(GEN nf, GEN x, GEN pr, ulong n)
    1408             : {
    1409        4488 :   GEN A = idealmulpowprime(nf, gel(x,1), pr, utoipos(n));
    1410        4488 :   return mkvec2(A, gel(x,2));
    1411             : }
    1412             : static GEN
    1413       65770 : init_famat(GEN x) { return mkvec2(x, trivial_fact()); }
    1414             : /* optimized idealfactorback + reduction; z = init_famat() */
    1415             : static GEN
    1416       28776 : genback(GEN z, GEN nf, GEN P, GEN E)
    1417             : {
    1418       28776 :   long i, l = lg(E);
    1419       28776 :   GEN I = NULL;
    1420       76617 :   for (i = 1; i < l; i++)
    1421       47840 :     if (signe(gel(E,i)))
    1422             :     {
    1423             :       GEN J;
    1424       32282 :       gel(z,1) = gel(P,i);
    1425       32282 :       J = idealpowred(nf, z, gel(E,i));
    1426       32283 :       I = I? idealHNF_mulred(nf, I, J): J;
    1427             :     }
    1428       28777 :   return I; /* != NULL since a generator */
    1429             : }
    1430             : 
    1431             : static GEN
    1432     1252510 : SPLIT_i(FB_t *F, GEN nf, GEN G, GEN x, GEN xred, GEN Nx, FACT *fact)
    1433             : {
    1434     1252510 :   pari_sp av = avma;
    1435     1252510 :   GEN L = idealpseudominvec(xred, G);
    1436     1252507 :   long k, l = lg(L);
    1437     1335460 :   for(k = 1; k < l; k++)
    1438     1319533 :     if (factorgen(F, nf, x, Nx, gel(L,k), fact)) return gel(L,k);
    1439       15927 :   return gc_NULL(av);
    1440             : }
    1441             : /* return famat y (principal ideal) such that y / x is smooth [wrt Vbase] */
    1442             : static GEN
    1443     1408695 : SPLIT(FB_t *F, GEN nf, GEN x, GEN Vbase, FACT *fact)
    1444             : {
    1445     1408695 :   GEN vecG, ex, y, x0, Nx = ZM_det_triangular(x);
    1446             :   long nbtest_lim, nbtest, i, j, ru, lgsub;
    1447             :   pari_sp av;
    1448             : 
    1449             :   /* try without reduction if x is small */
    1450     2817209 :   if (expi(gcoeff(x,1,1)) < 100 &&
    1451     1580626 :       can_factor(F, nf, x, NULL, Nx, fact)) return NULL;
    1452     1236583 :   if ((y = SPLIT_i(F, nf, nf_get_roundG(nf), x, x, Nx, fact))) return y;
    1453             : 
    1454             :   /* reduce in various directions */
    1455        8796 :   ru = lg(nf_get_roots(nf));
    1456        8796 :   vecG = cgetg(ru, t_VEC);
    1457       14345 :   for (j=1; j<ru; j++)
    1458             :   {
    1459       12597 :     gel(vecG,j) = nf_get_Gtwist1(nf, j);
    1460       12597 :     if ((y = SPLIT_i(F, nf, gel(vecG,j), x, x, Nx, fact))) return y;
    1461             :   }
    1462             : 
    1463             :   /* tough case, multiply by random products */
    1464        1748 :   lgsub = 3; nbtest = 1; nbtest_lim = 4;
    1465        1748 :   ex = cgetg(lgsub, t_VECSMALL);
    1466        1748 :   x0 = init_famat(x);
    1467             :   for(;;)
    1468         594 :   {
    1469        2342 :     GEN Ired, I, NI, id = x0;
    1470        2342 :     av = avma;
    1471        2342 :     if (DEBUGLEVEL>2) err_printf("# ideals tried = %ld\n",nbtest);
    1472        7138 :     for (i=1; i<lgsub; i++)
    1473             :     {
    1474        4796 :       ex[i] = random_bits(RANDOM_BITS);
    1475        4796 :       if (ex[i]) id = idealmulpowprime2(nf, id, gel(Vbase,i), ex[i]);
    1476             :     }
    1477        2342 :     if (id == x0) continue;
    1478             :     /* I^(-1) * \prod Vbase[i]^ex[i] = (id[2]) / x */
    1479             : 
    1480        2321 :     I = gel(id,1); NI = ZM_det_triangular(I);
    1481        2321 :     if (can_factor(F, nf, I, NULL, NI, fact))
    1482             :     {
    1483           0 :       inv_fact(fact); /* I^(-1) */
    1484           0 :       for (i=1; i<lgsub; i++)
    1485           0 :         if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1486           0 :       return gel(id,2);
    1487             :     }
    1488        2321 :     Ired = ru == 2? I: ZM_lll(I, 0.99, LLL_INPLACE);
    1489        3903 :     for (j=1; j<ru; j++)
    1490        3330 :       if ((y = SPLIT_i(F, nf, gel(vecG,j), I, Ired, NI, fact)))
    1491             :       {
    1492        5265 :         for (i=1; i<lgsub; i++)
    1493        3517 :           if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1494        1748 :         return famat_mul_shallow(gel(id,2), y);
    1495             :       }
    1496         573 :     set_avma(av);
    1497         573 :     if (++nbtest > nbtest_lim)
    1498             :     {
    1499          21 :       nbtest = 0;
    1500          21 :       if (++lgsub < minss(8, lg(Vbase)-1))
    1501             :       {
    1502          21 :         nbtest_lim <<= 1;
    1503          21 :         ex = cgetg(lgsub, t_VECSMALL);
    1504             :       }
    1505           0 :       else nbtest_lim = LONG_MAX; /* don't increase further */
    1506          21 :       if (DEBUGLEVEL>2) err_printf("SPLIT: increasing factor base [%ld]\n",lgsub);
    1507             :     }
    1508             :   }
    1509             : }
    1510             : 
    1511             : INLINE GEN
    1512     1397265 : bnf_get_W(GEN bnf) { return gel(bnf,1); }
    1513             : INLINE GEN
    1514     2784592 : bnf_get_B(GEN bnf) { return gel(bnf,2); }
    1515             : INLINE GEN
    1516     2819170 : bnf_get_C(GEN bnf) { return gel(bnf,4); }
    1517             : INLINE GEN
    1518     1392372 : bnf_get_vbase(GEN bnf) { return gel(bnf,5); }
    1519             : INLINE GEN
    1520     1392288 : bnf_get_Ur(GEN bnf) { return gmael(bnf,9,1); }
    1521             : INLINE GEN
    1522      271813 : bnf_get_ga(GEN bnf) { return gmael(bnf,9,2); }
    1523             : INLINE GEN
    1524      276769 : bnf_get_GD(GEN bnf) { return gmael(bnf,9,3); }
    1525             : 
    1526             : /* Return y (as an elt of K or a t_MAT representing an elt in Z[K])
    1527             :  * such that x / (y) is smooth and store the exponents of  its factorization
    1528             :  * on g_W and g_B in Wex / Bex; return NULL for y = 1 */
    1529             : static GEN
    1530     1392289 : split_ideal(GEN bnf, GEN x, GEN *pWex, GEN *pBex)
    1531             : {
    1532     1392289 :   GEN L, y, Vbase = bnf_get_vbase(bnf);
    1533     1392289 :   GEN Wex, W  = bnf_get_W(bnf);
    1534     1392289 :   GEN Bex, B  = bnf_get_B(bnf);
    1535             :   long p, j, i, l, nW, nB;
    1536             :   FACT *fact;
    1537             :   FB_t F;
    1538             : 
    1539     1392289 :   L = recover_partFB(&F, Vbase, lg(x)-1);
    1540     1392289 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    1541     1392288 :   y = SPLIT(&F, bnf_get_nf(bnf), x, Vbase, fact);
    1542     1392289 :   nW = lg(W)-1; *pWex = Wex = zero_zv(nW);
    1543     1392289 :   nB = lg(B)-1; *pBex = Bex = zero_zv(nB); l = lg(F.FB);
    1544     1392289 :   p = j = 0; /* -Wall */
    1545     2092127 :   for (i = 1; i <= fact[0].pr; i++)
    1546             :   { /* decode index C = ip+j --> (p,j) */
    1547      699838 :     long a, b, t, C = fact[i].pr;
    1548     1948261 :     for (t = 1; t < l; t++)
    1549             :     {
    1550     1860806 :       long q = F.FB[t], k = C - F.iLP[q];
    1551     1860806 :       if (k <= 0) break;
    1552     1248423 :       p = q;
    1553     1248423 :       j = k;
    1554             :     }
    1555      699838 :     a = gel(L, p)[j];
    1556      699838 :     b = a - nW;
    1557      699838 :     if (b <= 0) Wex[a] = y? -fact[i].ex: fact[i].ex;
    1558      546287 :     else        Bex[b] = y? -fact[i].ex: fact[i].ex;
    1559             :   }
    1560     1392289 :   return y;
    1561             : }
    1562             : 
    1563             : GEN
    1564     1107799 : init_red_mod_units(GEN bnf, long prec)
    1565             : {
    1566     1107799 :   GEN s = gen_0, p1,s1,mat, logfu = bnf_get_logfu(bnf);
    1567     1107799 :   long i,j, RU = lg(logfu);
    1568             : 
    1569     1107799 :   if (RU == 1) return NULL;
    1570     1107799 :   mat = cgetg(RU,t_MAT);
    1571     2507099 :   for (j=1; j<RU; j++)
    1572             :   {
    1573     1399300 :     p1 = cgetg(RU+1,t_COL); gel(mat,j) = p1;
    1574     1399300 :     s1 = gen_0;
    1575     3465848 :     for (i=1; i<RU; i++)
    1576             :     {
    1577     2066548 :       gel(p1,i) = real_i(gcoeff(logfu,i,j));
    1578     2066548 :       s1 = mpadd(s1, mpsqr(gel(p1,i)));
    1579             :     }
    1580     1399300 :     gel(p1,RU) = gen_0; if (mpcmp(s1,s) > 0) s = s1;
    1581             :   }
    1582     1107799 :   s = gsqrt(gmul2n(s,RU),prec);
    1583     1107799 :   if (expo(s) < 27) s = utoipos(1UL << 27);
    1584     1107799 :   return mkvec2(mat, s);
    1585             : }
    1586             : 
    1587             : /* z computed above. Return unit exponents that would reduce col (arch) */
    1588             : GEN
    1589     1107799 : red_mod_units(GEN col, GEN z)
    1590             : {
    1591             :   long i,RU;
    1592             :   GEN x,mat,N2;
    1593             : 
    1594     1107799 :   if (!z) return NULL;
    1595     1107799 :   mat= gel(z,1);
    1596     1107799 :   N2 = gel(z,2);
    1597     1107799 :   RU = lg(mat); x = cgetg(RU+1,t_COL);
    1598     2507099 :   for (i=1; i<RU; i++) gel(x,i) = real_i(gel(col,i));
    1599     1107799 :   gel(x,RU) = N2;
    1600     1107799 :   x = lll(shallowconcat(mat,x));
    1601     1107799 :   if (typ(x) != t_MAT || lg(x) <= RU) return NULL;
    1602     1107799 :   x = gel(x,RU);
    1603     1107799 :   if (signe(gel(x,RU)) < 0) x = gneg_i(x);
    1604     1107799 :   if (!gequal1(gel(x,RU))) pari_err_BUG("red_mod_units");
    1605     1107799 :   setlg(x,RU); return x;
    1606             : }
    1607             : 
    1608             : static GEN
    1609     2250114 : add(GEN a, GEN t) { return a = a? RgC_add(a,t): t; }
    1610             : 
    1611             : /* [x] archimedian components, A column vector. return [x] A */
    1612             : static GEN
    1613     2057263 : act_arch(GEN A, GEN x)
    1614             : {
    1615             :   GEN a;
    1616     2057263 :   long i,l = lg(A), tA = typ(A);
    1617     2057263 :   if (tA == t_MAT)
    1618             :   { /* assume lg(x) >= l */
    1619      192348 :     a = cgetg(l, t_MAT);
    1620      282180 :     for (i=1; i<l; i++) gel(a,i) = act_arch(gel(A,i), x);
    1621      192350 :     return a;
    1622             :   }
    1623     1864915 :   if (l==1) return cgetg(1, t_COL);
    1624     1864915 :   a = NULL;
    1625     1864915 :   if (tA == t_VECSMALL)
    1626             :   {
    1627     7198643 :     for (i=1; i<l; i++)
    1628             :     {
    1629     5967181 :       long c = A[i];
    1630     5967181 :       if (c) a = add(a, gmulsg(c, gel(x,i)));
    1631             :     }
    1632             :   }
    1633             :   else
    1634             :   { /* A a t_COL of t_INT. Assume lg(A)==lg(x) */
    1635     1383798 :     for (i=1; i<l; i++)
    1636             :     {
    1637      750344 :       GEN c = gel(A,i);
    1638      750344 :       if (signe(c)) a = add(a, gmul(c, gel(x,i)));
    1639             :     }
    1640             :   }
    1641     1864916 :   return a? a: zerocol(lgcols(x)-1);
    1642             : }
    1643             : /* act_arch(matdiagonal(v), x) */
    1644             : static GEN
    1645       64118 : diagact_arch(GEN v, GEN x)
    1646             : {
    1647       64118 :   long i, l = lg(v);
    1648       64118 :   GEN a = cgetg(l, t_MAT);
    1649       92965 :   for (i = 1; i < l; i++) gel(a,i) = gmul(gel(x,i), gel(v,i));
    1650       64118 :   return a;
    1651             : }
    1652             : 
    1653             : static long
    1654     1410482 : prec_arch(GEN bnf)
    1655             : {
    1656     1410482 :   GEN a = bnf_get_C(bnf);
    1657     1410480 :   long i, l = lg(a), prec;
    1658             : 
    1659     1410480 :   for (i=1; i<l; i++)
    1660     1410396 :     if ( (prec = gprecision(gel(a,i))) ) return prec;
    1661          84 :   return DEFAULTPREC;
    1662             : }
    1663             : 
    1664             : static long
    1665        3849 : needed_bitprec(GEN x)
    1666             : {
    1667        3849 :   long i, e = 0, l = lg(x);
    1668       22512 :   for (i = 1; i < l; i++)
    1669             :   {
    1670       18663 :     GEN c = gel(x,i);
    1671       18663 :     long f = gexpo(c) - gprecision(c);
    1672       18663 :     if (f > e) e = f;
    1673             :   }
    1674        3849 :   return e;
    1675             : }
    1676             : 
    1677             : /* col = archimedian components of x, Nx its norm, dx a multiple of its
    1678             :  * denominator. Return x or NULL (fail) */
    1679             : GEN
    1680     1237073 : isprincipalarch(GEN bnf, GEN col, GEN kNx, GEN e, GEN dx, long *pe)
    1681             : {
    1682             :   GEN nf, x, y, logfu, s, M;
    1683     1237073 :   long N, prec = gprecision(col);
    1684     1237073 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf); M = nf_get_M(nf);
    1685     1237072 :   if (!prec) prec = prec_arch(bnf);
    1686     1237072 :   *pe = 128;
    1687     1237072 :   logfu = bnf_get_logfu(bnf);
    1688     1237072 :   N = nf_get_degree(nf);
    1689     1237072 :   if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
    1690     1237068 :   if (lg(col) > 2)
    1691             :   { /* reduce mod units */
    1692     1107799 :     GEN u, z = init_red_mod_units(bnf,prec);
    1693     1107799 :     if (!(u = red_mod_units(col,z))) return NULL;
    1694     1107799 :     col = RgC_add(col, RgM_RgC_mul(logfu, u));
    1695     1107799 :     if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
    1696             :   }
    1697     1237067 :   s = divru(mulir(e, glog(kNx,prec)), N);
    1698     1237065 :   col = fixarch(col, s, nf_get_r1(nf));
    1699     1237073 :   if (RgC_expbitprec(col) >= 0) return NULL;
    1700     1236500 :   col = gexp(col, prec);
    1701             :   /* d.alpha such that x = alpha \prod gj^ej */
    1702     1236501 :   x = RgM_solve_realimag(M,col); if (!x) return NULL;
    1703     1236499 :   x = RgC_Rg_mul(x, dx);
    1704     1236496 :   y = grndtoi(x, pe);
    1705     1236498 :   if (*pe > -5) { *pe = needed_bitprec(x); return NULL; }
    1706     1232649 :   return RgC_Rg_div(y, dx);
    1707             : }
    1708             : 
    1709             : /* y = C \prod g[i]^e[i] ? */
    1710             : static int
    1711     1228575 : fact_ok(GEN nf, GEN y, GEN C, GEN g, GEN e)
    1712             : {
    1713     1228575 :   pari_sp av = avma;
    1714     1228575 :   long i, c = lg(e);
    1715     1228575 :   GEN z = C? C: gen_1;
    1716     1505634 :   for (i=1; i<c; i++)
    1717      277059 :     if (signe(gel(e,i))) z = idealmul(nf, z, idealpow(nf, gel(g,i), gel(e,i)));
    1718     1228575 :   if (typ(z) != t_MAT) z = idealhnf_shallow(nf,z);
    1719     1228575 :   if (typ(y) != t_MAT) y = idealhnf_shallow(nf,y);
    1720     1228575 :   return gc_bool(av, ZM_equal(y,z));
    1721             : }
    1722             : static GEN
    1723     1392289 : ZV_divrem(GEN A, GEN B, GEN *pR)
    1724             : {
    1725     1392289 :   long i, l = lg(A);
    1726     1392289 :   GEN Q = cgetg(l, t_COL), R = cgetg(l, t_COL);
    1727     1898549 :   for (i = 1; i < l; i++) gel(Q,i) = truedvmdii(gel(A,i), gel(B,i), &gel(R,i));
    1728     1392288 :   *pR = R; return Q;
    1729             : }
    1730             : 
    1731             : static GEN
    1732     1392288 : Ur_ZC_mul(GEN bnf, GEN v)
    1733             : {
    1734     1392288 :   GEN w, U = bnf_get_Ur(bnf);
    1735     1392288 :   long i, l = lg(bnf_get_cyc(bnf)); /* may be < lgcols(U) */
    1736             : 
    1737     1392288 :   w = cgetg(l, t_COL);
    1738     1898549 :   for (i = 1; i < l; i++) gel(w,i) = ZMrow_ZC_mul(U, v, i);
    1739     1392288 :   return w;
    1740             : }
    1741             : 
    1742             : static GEN
    1743        7283 : ZV_mul(GEN x, GEN y)
    1744             : {
    1745        7283 :   long i, l = lg(x);
    1746        7283 :   GEN z = cgetg(l, t_COL);
    1747       31780 :   for (i = 1; i < l; i++) gel(z,i) = mulii(gel(x,i), gel(y,i));
    1748        7283 :   return z;
    1749             : }
    1750             : static int
    1751     1228027 : dump_gen(GEN SUnits, GEN x, long flag)
    1752             : {
    1753             :   GEN d;
    1754             :   long e;
    1755     1228027 :   if (!(flag & nf_GENMAT) || !SUnits) return 0;
    1756      272947 :   e = gexpo(gel(SUnits,2)); if (e > 64) return 0; /* U large */
    1757      272851 :   x = Q_remove_denom(x, &d);
    1758      272850 :   return (d && expi(d) > 32) || gexpo(x) > 32;
    1759             : }
    1760             : 
    1761             : /* assume x in HNF; cf class_group_gen for notations. Return NULL iff
    1762             :  * flag & nf_FORCE and computation of principal ideal generator fails */
    1763             : static GEN
    1764     1408671 : isprincipalall(GEN bnf, GEN x, long *pprec, long flag)
    1765             : {
    1766             :   GEN xar, Wex, Bex, gen, xc, col, A, Q, R, UA, SUnits;
    1767     1408671 :   GEN C = bnf_get_C(bnf), nf = bnf_get_nf(bnf), cyc = bnf_get_cyc(bnf);
    1768             :   long nB, nW, e;
    1769             : 
    1770     1408670 :   if (lg(cyc) == 1 && !(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL)))
    1771        4732 :     return cgetg(1,t_COL);
    1772     1403938 :   if (lg(x) == 2)
    1773             :   { /* nf = Q */
    1774          84 :     col = gel(x,1);
    1775          84 :     if (flag & nf_GENMAT) col = Q_to_famat(gel(col,1));
    1776          84 :     return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(cgetg(1,t_COL), col);
    1777             :   }
    1778             : 
    1779     1403854 :   x = Q_primitive_part(x, &xc);
    1780     1403853 :   if (equali1(gcoeff(x,1,1))) /* trivial ideal */
    1781             :   {
    1782       11564 :     R = zerocol(lg(cyc)-1);
    1783       11564 :     if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1784       11515 :     if (flag & nf_GEN_IF_PRINCIPAL)
    1785        6468 :       return scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
    1786        5047 :     if (flag & nf_GENMAT)
    1787        2191 :       col = xc? Q_to_famat(xc): trivial_fact();
    1788             :     else
    1789        2856 :       col = scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
    1790        5047 :     return mkvec2(R, col);
    1791             :   }
    1792     1392289 :   xar = split_ideal(bnf, x, &Wex, &Bex);
    1793             :   /* x = g_W Wex + g_B Bex + [xar] = g_W (Wex - B*Bex) + [xar] + [C_B]Bex */
    1794     1392289 :   A = zc_to_ZC(Wex); nB = lg(Bex)-1;
    1795     1392288 :   if (nB) A = ZC_sub(A, ZM_zc_mul(bnf_get_B(bnf), Bex));
    1796     1392288 :   UA = Ur_ZC_mul(bnf, A);
    1797     1392288 :   Q = ZV_divrem(UA, cyc, &R);
    1798             :   /* g_W (Wex - B*Bex) = G Ur A - [ga]A = G R + [GD]Q - [ga]A
    1799             :    * Finally: x = G R + [xar] + [C_B]Bex + [GD]Q - [ga]A */
    1800     1392288 :   if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1801     1232062 :   if ((flag & nf_GEN_IF_PRINCIPAL) && !ZV_equal0(R)) return gen_0;
    1802             : 
    1803     1232055 :   nW = lg(Wex)-1;
    1804     1232055 :   gen = bnf_get_gen(bnf);
    1805     1232055 :   col = NULL;
    1806     1232055 :   SUnits = bnf_get_sunits(bnf);
    1807     1232055 :   if (lg(R) == 1
    1808      272410 :       || abscmpiu(gel(R,vecindexmax(R)), 4 * (*pprec)) < 0)
    1809             :   { /* q = N (x / prod gj^ej) = N(alpha), denom(alpha) | d */
    1810     1231458 :     GEN d, q = gdiv(ZM_det_triangular(x), get_norm_fact(gen, R, &d));
    1811     1231460 :     col = xar? nf_cxlog(nf, xar, *pprec): NULL;
    1812     1231462 :     if (nB) col = add(col, act_arch(Bex, nW? vecslice(C,nW+1,lg(C)-1): C));
    1813     1231460 :     if (nW) col = add(col, RgC_sub(act_arch(Q, bnf_get_GD(bnf)),
    1814             :                                    act_arch(A, bnf_get_ga(bnf))));
    1815     1231460 :     col = isprincipalarch(bnf, col, q, gen_1, d, &e);
    1816     1231462 :     if (col && (dump_gen(SUnits, col, flag)
    1817     1228026 :                 || !fact_ok(nf,x, col,gen,R))) col = NULL;
    1818             :   }
    1819     1232055 :   if (!col && (flag & nf_GENMAT))
    1820             :   {
    1821        8024 :     if (SUnits)
    1822             :     {
    1823        7535 :       GEN X = gel(SUnits,1), U = gel(SUnits,2), C = gel(SUnits,3);
    1824        7535 :       GEN v = gel(bnf,9), Ge = gel(v,4), M1 = gel(v,5), M2 = gel(v,6);
    1825        7535 :       GEN z = NULL, F = NULL;
    1826        7535 :       if (nB)
    1827             :       {
    1828        7535 :         GEN C2 = nW? vecslice(C, nW+1, lg(C)-1): C;
    1829        7535 :         z = ZM_zc_mul(C2, Bex);
    1830             :       }
    1831        7535 :       if (nW)
    1832             :       { /* [GD]Q - [ga]A = ([X]M1 - [Ge]D) Q - ([X]M2 - [Ge]Ur) A */
    1833        7283 :         GEN C1 = vecslice(C, 1, nW);
    1834        7283 :         GEN v = ZC_sub(ZM_ZC_mul(M1,Q), ZM_ZC_mul(M2,A));
    1835        7283 :         z = add(z, ZM_ZC_mul(C1, v));
    1836        7283 :         F = famat_reduce(famatV_factorback(Ge, ZC_sub(UA, ZV_mul(cyc,Q))));
    1837        7283 :         if (lgcols(F) == 1) F = NULL;
    1838             :       }
    1839             :       /* reduce modulo units and Q^* */
    1840        7535 :       if (lg(U) != 1) z = ZC_sub(z, ZM_ZC_mul(U, RgM_Babai(U,z)));
    1841        7535 :       col = mkmat2(X, z);
    1842        7535 :       if (F) col = famat_mul_shallow(col, F);
    1843        7535 :       col = famat_remove_trivial(col);
    1844        7535 :       if (xar) col = famat_mul_shallow(col, xar);
    1845             :     }
    1846         489 :     else if (!ZV_equal0(R))
    1847             :     { /* in case isprincipalfact calls bnfinit() due to prec trouble...*/
    1848         483 :       GEN y = isprincipalfact(bnf, x, gen, ZC_neg(R), flag);
    1849         483 :       if (typ(y) != t_VEC) return y;
    1850         483 :       col = gel(y,2);
    1851             :     }
    1852             :   }
    1853     1232055 :   if (col)
    1854             :   { /* add back missing content */
    1855     1231965 :     if (typ(col) == t_MAT)
    1856        8018 :     { if (xc) col = famat_mul_shallow(col, xc); }
    1857     1223947 :     else if (flag & nf_GENMAT)
    1858             :     {
    1859             :       GEN c;
    1860     1210248 :       if (RgV_isscalar(col))
    1861        3664 :         col = Q_to_famat(mul_content(xc, gel(col,1)));
    1862             :       else
    1863             :       {
    1864     1206585 :         col = Q_primitive_part(col, &c);
    1865     1206582 :         col = to_famat_shallow(col, gen_1);
    1866     1206584 :         xc = mul_content(xc, c);
    1867     1206584 :         if (xc) col = famat_mul(col, Q_to_famat(xc));
    1868             :       }
    1869             :     }
    1870             :     else
    1871       13699 :     { if (xc) col = RgC_Rg_mul(col,xc); }
    1872             :   }
    1873             :   else
    1874             :   {
    1875          90 :     if (e < 0) e = 0;
    1876          90 :     *pprec += nbits2extraprec(e + 128);
    1877          90 :     if (flag & nf_FORCE)
    1878             :     {
    1879          76 :       if (DEBUGLEVEL)
    1880           0 :         pari_warn(warner,"precision too low for generators, e = %ld",e);
    1881          76 :       return NULL;
    1882             :     }
    1883          14 :     pari_warn(warner,"precision too low for generators, not given");
    1884          14 :     col = cgetg(1, t_COL);
    1885             :   }
    1886     1231979 :   return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(R, col);
    1887             : }
    1888             : 
    1889             : static GEN
    1890      464660 : triv_gen(GEN bnf, GEN x, long flag)
    1891             : {
    1892      464660 :   pari_sp av = avma;
    1893      464660 :   GEN nf = bnf_get_nf(bnf);
    1894             :   long c;
    1895      464660 :   if (flag & nf_GEN_IF_PRINCIPAL)
    1896             :   {
    1897           7 :     if (!(flag & nf_GENMAT)) return algtobasis(nf,x);
    1898           7 :     x = nf_to_scalar_or_basis(nf,x);
    1899           7 :     if (typ(x) == t_INT && is_pm1(x)) return trivial_fact();
    1900           0 :     return gc_GEN(av, to_famat_shallow(x, gen_1));
    1901             :   }
    1902      464653 :   c = lg(bnf_get_cyc(bnf)) - 1;
    1903      464653 :   if (flag & nf_GENMAT)
    1904      455049 :     retmkvec2(zerocol(c), to_famat_shallow(algtobasis(nf,x), gen_1));
    1905        9604 :   if (flag & nf_GEN)
    1906          28 :     retmkvec2(zerocol(c), algtobasis(nf,x));
    1907        9576 :   return zerocol(c);
    1908             : }
    1909             : 
    1910             : GEN
    1911     1841072 : bnfisprincipal0(GEN bnf,GEN x,long flag)
    1912             : {
    1913     1841072 :   pari_sp av = avma;
    1914             :   GEN c, nf;
    1915             :   long pr;
    1916             : 
    1917     1841072 :   bnf = checkbnf(bnf);
    1918     1841073 :   nf = bnf_get_nf(bnf);
    1919     1841073 :   switch( idealtyp(&x, NULL) )
    1920             :   {
    1921       59080 :     case id_PRINCIPAL:
    1922       59080 :       if (gequal0(x)) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1923       59080 :       return triv_gen(bnf, x, flag);
    1924     1758354 :     case id_PRIME:
    1925     1758354 :       if (pr_is_inert(x)) return triv_gen(bnf, pr_get_p(x), flag);
    1926     1352781 :       x = pr_hnf(nf, x);
    1927     1352784 :       break;
    1928       23639 :     case id_MAT:
    1929       23639 :       if (lg(x)==1) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1930       23639 :       if (nf_get_degree(nf) != lg(x)-1)
    1931           0 :         pari_err_TYPE("idealtyp [dimension != degree]", x);
    1932             :   }
    1933     1376422 :   pr = prec_arch(bnf); /* precision of unit matrix */
    1934     1376421 :   c = getrand();
    1935             :   for (;;)
    1936           6 :   {
    1937     1376428 :     pari_sp av1 = avma;
    1938     1376428 :     GEN y = isprincipalall(bnf,x,&pr,flag);
    1939     1376425 :     if (y) return gc_GEN(av, y);
    1940             : 
    1941           6 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",pr);
    1942           6 :     set_avma(av1); bnf = bnfnewprec_shallow(bnf,pr); setrand(c);
    1943             :   }
    1944             : }
    1945             : GEN
    1946      174534 : isprincipal(GEN bnf,GEN x) { return bnfisprincipal0(bnf,x,0); }
    1947             : 
    1948             : /* FIXME: OBSOLETE */
    1949             : GEN
    1950           0 : isprincipalgen(GEN bnf,GEN x)
    1951           0 : { return bnfisprincipal0(bnf,x,nf_GEN); }
    1952             : GEN
    1953           0 : isprincipalforce(GEN bnf,GEN x)
    1954           0 : { return bnfisprincipal0(bnf,x,nf_FORCE); }
    1955             : GEN
    1956           0 : isprincipalgenforce(GEN bnf,GEN x)
    1957           0 : { return bnfisprincipal0(bnf,x,nf_GEN | nf_FORCE); }
    1958             : 
    1959             : /* lg(u) > 1 */
    1960             : static int
    1961         105 : RgV_is1(GEN u) { return isint1(gel(u,1)) && RgV_isscalar(u); }
    1962             : static GEN
    1963       32173 : add_principal_part(GEN nf, GEN u, GEN v, long flag)
    1964             : {
    1965       32173 :   if (flag & nf_GENMAT)
    1966       14540 :     return (typ(u) == t_COL && RgV_is1(u))? v: famat_mul_shallow(v,u);
    1967             :   else
    1968       17633 :     return nfmul(nf, v, u);
    1969             : }
    1970             : 
    1971             : #if 0
    1972             : /* compute C prod P[i]^e[i],  e[i] >=0 for all i. C may be NULL (omitted)
    1973             :  * e destroyed ! */
    1974             : static GEN
    1975             : expand(GEN nf, GEN C, GEN P, GEN e)
    1976             : {
    1977             :   long i, l = lg(e), done = 1;
    1978             :   GEN id = C;
    1979             :   for (i=1; i<l; i++)
    1980             :   {
    1981             :     GEN ei = gel(e,i);
    1982             :     if (signe(ei))
    1983             :     {
    1984             :       if (mod2(ei)) id = id? idealmul(nf, id, gel(P,i)): gel(P,i);
    1985             :       ei = shifti(ei,-1);
    1986             :       if (signe(ei)) done = 0;
    1987             :       gel(e,i) = ei;
    1988             :     }
    1989             :   }
    1990             :   if (id != C) id = idealred(nf, id);
    1991             :   if (done) return id;
    1992             :   return idealmulred(nf, id, idealsqr(nf, expand(nf,id,P,e)));
    1993             : }
    1994             : /* C is an extended ideal, possibly with C[1] = NULL */
    1995             : static GEN
    1996             : expandext(GEN nf, GEN C, GEN P, GEN e)
    1997             : {
    1998             :   long i, l = lg(e), done = 1;
    1999             :   GEN A = gel(C,1);
    2000             :   for (i=1; i<l; i++)
    2001             :   {
    2002             :     GEN ei = gel(e,i);
    2003             :     if (signe(ei))
    2004             :     {
    2005             :       if (mod2(ei)) A = A? idealmul(nf, A, gel(P,i)): gel(P,i);
    2006             :       ei = shifti(ei,-1);
    2007             :       if (signe(ei)) done = 0;
    2008             :       gel(e,i) = ei;
    2009             :     }
    2010             :   }
    2011             :   if (A == gel(C,1))
    2012             :     A = C;
    2013             :   else
    2014             :     A = idealred(nf, mkvec2(A, gel(C,2)));
    2015             :   if (done) return A;
    2016             :   return idealmulred(nf, A, idealsqr(nf, expand(nf,A,P,e)));
    2017             : }
    2018             : #endif
    2019             : 
    2020             : static GEN
    2021           0 : expand(GEN nf, GEN C, GEN P, GEN e)
    2022             : {
    2023           0 :   long i, l = lg(e);
    2024           0 :   GEN B, A = C;
    2025           0 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    2026           0 :     if (signe(gel(e,i)))
    2027             :     {
    2028           0 :       B = idealpowred(nf, gel(P,i), gel(e,i));
    2029           0 :       A = A? idealmulred(nf,A,B): B;
    2030             :     }
    2031           0 :   return A;
    2032             : }
    2033             : static GEN
    2034       32192 : expandext(GEN nf, GEN C, GEN P, GEN e)
    2035             : {
    2036       32192 :   long i, l = lg(e);
    2037       32192 :   GEN B, A = gel(C,1), C1 = A;
    2038       95212 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    2039       63018 :     if (signe(gel(e,i)))
    2040             :     {
    2041       35214 :       gel(C,1) = gel(P,i);
    2042       35214 :       B = idealpowred(nf, C, gel(e,i));
    2043       35216 :       A = A? idealmulred(nf,A,B): B;
    2044             :     }
    2045       32194 :   return A == C1? C: A;
    2046             : }
    2047             : 
    2048             : /* isprincipal for C * \prod P[i]^e[i] (C omitted if NULL) */
    2049             : GEN
    2050       32192 : isprincipalfact(GEN bnf, GEN C, GEN P, GEN e, long flag)
    2051             : {
    2052       32192 :   const long gen = flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL);
    2053             :   long prec;
    2054       32192 :   pari_sp av = avma;
    2055       32192 :   GEN C0, Cext, c, id, nf = bnf_get_nf(bnf);
    2056             : 
    2057       32192 :   if (gen)
    2058             :   {
    2059       14547 :     Cext = (flag & nf_GENMAT)? trivial_fact()
    2060       32192 :                              : mkpolmod(gen_1,nf_get_pol(nf));
    2061       32192 :     C0 = mkvec2(C, Cext);
    2062       32192 :     id = expandext(nf, C0, P, e);
    2063             :   } else {
    2064           0 :     Cext = NULL;
    2065           0 :     C0 = C;
    2066           0 :     id = expand(nf, C, P, e);
    2067             :   }
    2068       32194 :   if (id == C0) /* e = 0 */
    2069             :   {
    2070       12477 :     if (!C) return bnfisprincipal0(bnf, gen_1, flag);
    2071       12463 :     switch(typ(C))
    2072             :     {
    2073           7 :       case t_INT: case t_FRAC: case t_POL: case t_POLMOD: case t_COL:
    2074           7 :         return triv_gen(bnf, C, flag);
    2075             :     }
    2076       12456 :     C = idealhnf_shallow(nf,C);
    2077             :   }
    2078             :   else
    2079             :   {
    2080       19717 :     if (gen) { C = gel(id,1); Cext = gel(id,2); } else C = id;
    2081             :   }
    2082       32173 :   prec = prec_arch(bnf);
    2083       32173 :   c = getrand();
    2084             :   for (;;)
    2085          70 :   {
    2086       32243 :     pari_sp av1 = avma;
    2087       32243 :     GEN y = isprincipalall(bnf, C, &prec, flag);
    2088       32243 :     if (y)
    2089             :     {
    2090       32173 :       if (flag & nf_GEN_IF_PRINCIPAL)
    2091             :       {
    2092       20818 :         if (typ(y) == t_INT) return gc_NULL(av);
    2093       20818 :         y = add_principal_part(nf, y, Cext, flag);
    2094             :       }
    2095             :       else
    2096             :       {
    2097       11355 :         GEN u = gel(y,2);
    2098       11355 :         if (!gen || typ(y) != t_VEC) return gc_upto(av,y);
    2099       11355 :         if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    2100             :       }
    2101       32173 :       return gc_GEN(av, y);
    2102             :     }
    2103          70 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",prec);
    2104          70 :     set_avma(av1); bnf = bnfnewprec_shallow(bnf,prec); setrand(c);
    2105             :   }
    2106             : }
    2107             : GEN
    2108           0 : isprincipalfact_or_fail(GEN bnf, GEN C, GEN P, GEN e)
    2109             : {
    2110           0 :   const long flag = nf_GENMAT|nf_FORCE;
    2111             :   long prec;
    2112           0 :   pari_sp av = avma;
    2113           0 :   GEN u, y, id, C0, Cext, nf = bnf_get_nf(bnf);
    2114             : 
    2115           0 :   Cext = trivial_fact();
    2116           0 :   C0 = mkvec2(C, Cext);
    2117           0 :   id = expandext(nf, C0, P, e);
    2118           0 :   if (id == C0) /* e = 0 */
    2119           0 :     C = idealhnf_shallow(nf,C);
    2120             :   else {
    2121           0 :     C = gel(id,1); Cext = gel(id,2);
    2122             :   }
    2123           0 :   prec = prec_arch(bnf);
    2124           0 :   y = isprincipalall(bnf, C, &prec, flag);
    2125           0 :   if (!y) return gc_utoipos(av, prec);
    2126           0 :   u = gel(y,2);
    2127           0 :   if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    2128           0 :   return gc_GEN(av, y);
    2129             : }
    2130             : 
    2131             : GEN
    2132      162322 : nfsign_from_logarch(GEN LA, GEN invpi, GEN archp)
    2133             : {
    2134      162322 :   long l = lg(archp), i;
    2135      162322 :   GEN y = cgetg(l, t_VECSMALL);
    2136      162326 :   pari_sp av = avma;
    2137             : 
    2138      310063 :   for (i=1; i<l; i++)
    2139             :   {
    2140      147736 :     GEN c = ground( gmul(imag_i(gel(LA,archp[i])), invpi) );
    2141      147737 :     y[i] = mpodd(c)? 1: 0;
    2142             :   }
    2143      162327 :   set_avma(av); return y;
    2144             : }
    2145             : 
    2146             : GEN
    2147      239978 : nfsign_tu(GEN bnf, GEN archp)
    2148             : {
    2149             :   long n;
    2150      239978 :   if (bnf_get_tuN(bnf) != 2) return cgetg(1, t_VECSMALL);
    2151      172874 :   n = archp? lg(archp) - 1: nf_get_r1(bnf_get_nf(bnf));
    2152      172874 :   return const_vecsmall(n, 1);
    2153             : }
    2154             : GEN
    2155      241144 : nfsign_fu(GEN bnf, GEN archp)
    2156             : {
    2157      241144 :   GEN invpi, y, A = bnf_get_logfu(bnf), nf = bnf_get_nf(bnf);
    2158      241140 :   long j = 1, RU = lg(A);
    2159             : 
    2160      241140 :   if (!archp) archp = identity_perm( nf_get_r1(nf) );
    2161      241140 :   invpi = invr( mppi(nf_get_prec(nf)) );
    2162      241152 :   y = cgetg(RU,t_MAT);
    2163      403378 :   for (j = 1; j < RU; j++)
    2164      162224 :     gel(y,j) = nfsign_from_logarch(gel(A,j), invpi, archp);
    2165      241154 :   return y;
    2166             : }
    2167             : GEN
    2168          35 : nfsign_units(GEN bnf, GEN archp, int add_zu)
    2169             : {
    2170          35 :   GEN sfu = nfsign_fu(bnf, archp);
    2171          35 :   return add_zu? vec_prepend(sfu, nfsign_tu(bnf, archp)): sfu;
    2172             : }
    2173             : 
    2174             : /* obsolete */
    2175             : GEN
    2176           7 : signunits(GEN bnf)
    2177             : {
    2178             :   pari_sp av;
    2179             :   GEN S, y, nf;
    2180             :   long i, j, r1, r2;
    2181             : 
    2182           7 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    2183           7 :   nf_get_sign(nf, &r1,&r2);
    2184           7 :   S = zeromatcopy(r1, r1+r2-1); av = avma;
    2185           7 :   y = nfsign_fu(bnf, NULL);
    2186          14 :   for (j = 1; j < lg(y); j++)
    2187             :   {
    2188           7 :     GEN Sj = gel(S,j), yj = gel(y,j);
    2189          21 :     for (i = 1; i <= r1; i++) gel(Sj,i) = yj[i]? gen_m1: gen_1;
    2190             :   }
    2191           7 :   set_avma(av); return S;
    2192             : }
    2193             : 
    2194             : static GEN
    2195      725483 : get_log_embed(REL_t *rel, GEN M, long RU, long R1, long prec)
    2196             : {
    2197      725483 :   GEN arch, C, z = rel->m;
    2198             :   long i;
    2199      725483 :   arch = typ(z) == t_COL? RgM_RgC_mul(M, z): const_col(nbrows(M), z);
    2200      725474 :   C = cgetg(RU+1, t_COL); arch = glog(arch, prec);
    2201     1654159 :   for (i=1; i<=R1; i++) gel(C,i) = gel(arch,i);
    2202     1563564 :   for (   ; i<=RU; i++) gel(C,i) = gmul2n(gel(arch,i), 1);
    2203      725473 :   return C;
    2204             : }
    2205             : static GEN
    2206     1016410 : rel_embed(REL_t *rel, FB_t *F, GEN embs, long ind, GEN M, long RU, long R1,
    2207             :           long prec)
    2208             : {
    2209             :   GEN C, D, perm;
    2210             :   long i, n;
    2211     1016410 :   if (!rel->relaut) return get_log_embed(rel, M, RU, R1, prec);
    2212             :   /* image of another relation by automorphism */
    2213      290931 :   C = gel(embs, ind - rel->relorig);
    2214      290931 :   perm = gel(F->embperm, rel->relaut);
    2215      290931 :   D = cgetg_copy(C, &n);
    2216     1217961 :   for (i = 1; i < n; i++)
    2217             :   {
    2218      927023 :     long v = perm[i];
    2219      927023 :     gel(D,i) = (v > 0)? gel(C,v): conj_i(gel(C,-v));
    2220             :   }
    2221      290938 :   return D;
    2222             : }
    2223             : static GEN
    2224      122414 : get_embs(FB_t *F, RELCACHE_t *cache, GEN nf, GEN embs, long PREC)
    2225             : {
    2226      122414 :   long ru, j, k, l = cache->last - cache->chk + 1, r1 = nf_get_r1(nf);
    2227      122413 :   GEN M = nf_get_M(nf), nembs = cgetg(cache->last - cache->base+1, t_MAT);
    2228             :   REL_t *rel;
    2229             : 
    2230     1510635 :   for (k = 1; k <= cache->chk - cache->base; k++) gel(nembs,k) = gel(embs,k);
    2231      122413 :   embs = nembs; ru = nbrows(M);
    2232     1126243 :   for (j=1,rel = cache->chk + 1; j < l; rel++,j++,k++)
    2233     1003833 :     gel(embs,k) = rel_embed(rel, F, embs, k, M, ru, r1, PREC);
    2234      122410 :   return embs;
    2235             : }
    2236             : static void
    2237      947724 : set_rel_alpha(REL_t *rel, GEN auts, GEN vA, long ind)
    2238             : {
    2239             :   GEN u;
    2240      947724 :   if (!rel->relaut)
    2241      674057 :     u = rel->m;
    2242             :   else
    2243      273667 :     u = ZM_ZC_mul(gel(auts, rel->relaut), gel(vA, ind - rel->relorig));
    2244      947721 :   gel(vA, ind) = u;
    2245      947721 : }
    2246             : static GEN
    2247     2253754 : set_fact(FB_t *F, FACT *fact, GEN e, long *pnz)
    2248             : {
    2249     2253754 :   long i, n = fact[0].pr, nz = F->KC + 1;
    2250     2253754 :   GEN c = zero_Flv(F->KC);
    2251    10548842 :   for (i = 1; i <= n; i++)
    2252             :   {
    2253     8295082 :     long p = fact[i].pr;
    2254     8295082 :     if (p < nz) nz = p;
    2255     8295082 :     c[p] = fact[i].ex;
    2256             :   }
    2257     2253760 :   if (e)
    2258             :   {
    2259      114418 :     long l = lg(e);
    2260      333833 :     for (i = 1; i < l; i++)
    2261      219415 :       if (e[i]) { long v = F->subFB[i]; c[v] += e[i]; if (v < nz) nz = v; }
    2262             :   }
    2263     2253760 :   *pnz = nz; return c;
    2264             : }
    2265             : 
    2266             : /* Is cols already in the cache ? bs = index of first non zero coeff in cols
    2267             :  * General check for colinearity useless since exceedingly rare */
    2268             : static int
    2269     2905900 : already_known(RELCACHE_t *cache, long bs, GEN cols)
    2270             : {
    2271             :   REL_t *r;
    2272     2905900 :   long l = lg(cols);
    2273   215168052 :   for (r = cache->last; r > cache->base; r--)
    2274   212751515 :     if (bs == r->nz)
    2275             :     {
    2276    34176830 :       GEN coll = r->R;
    2277    34176830 :       long b = bs;
    2278   122045578 :       while (b < l && cols[b] == coll[b]) b++;
    2279    34176830 :       if (b == l) return 1;
    2280             :     }
    2281     2416537 :   return 0;
    2282             : }
    2283             : 
    2284             : /* Add relation R to cache, nz = index of first non zero coeff in R.
    2285             :  * If relation is a linear combination of the previous ones, return 0.
    2286             :  * Otherwise, update basis and return > 0. Compute mod p (much faster)
    2287             :  * so some kernel vector might not be genuine. */
    2288             : static int
    2289     2910036 : add_rel_i(RELCACHE_t *cache, GEN R, long nz, GEN m, long orig, long aut, REL_t **relp, long in_rnd_rel)
    2290             : {
    2291     2910036 :   long i, k, n = lg(R)-1;
    2292             : 
    2293     2910036 :   if (nz == n+1) { k = 0; goto ADD_REL; }
    2294     2905885 :   if (already_known(cache, nz, R)) return -1;
    2295     2416599 :   if (cache->last >= cache->base + cache->len) return 0;
    2296     2416599 :   if (DEBUGLEVEL>6)
    2297             :   {
    2298           0 :     err_printf("adding vector = %Ps\n",R);
    2299           0 :     err_printf("generators =\n%Ps\n", cache->basis);
    2300             :   }
    2301     2416627 :   if (cache->missing)
    2302             :   {
    2303     2020257 :     GEN a = leafcopy(R), basis = cache->basis;
    2304     2020255 :     k = lg(a);
    2305   124367570 :     do --k; while (!a[k]);
    2306     7084906 :     while (k)
    2307             :     {
    2308     5533536 :       GEN c = gel(basis, k);
    2309     5533536 :       if (c[k])
    2310             :       {
    2311     5064651 :         long ak = a[k];
    2312   263109863 :         for (i=1; i < k; i++) if (c[i]) a[i] = (a[i] + ak*(mod_p-c[i])) % mod_p;
    2313     5064651 :         a[k] = 0;
    2314   129167369 :         do --k; while (!a[k]); /* k cannot go below 0: codeword is a sentinel */
    2315             :       }
    2316             :       else
    2317             :       {
    2318      468885 :         ulong invak = Fl_inv(uel(a,k), mod_p);
    2319             :         /* Cleanup a */
    2320    13949877 :         for (i = k; i-- > 1; )
    2321             :         {
    2322    13480990 :           long j, ai = a[i];
    2323    13480990 :           c = gel(basis, i);
    2324    13480990 :           if (!ai || !c[i]) continue;
    2325      237382 :           ai = mod_p-ai;
    2326     4492739 :           for (j = 1; j < i; j++) if (c[j]) a[j] = (a[j] + ai*c[j]) % mod_p;
    2327      237382 :           a[i] = 0;
    2328             :         }
    2329             :         /* Insert a/a[k] as k-th column */
    2330      468887 :         c = gel(basis, k);
    2331    13949876 :         for (i = 1; i<k; i++) if (a[i]) c[i] = (a[i] * invak) % mod_p;
    2332      468887 :         c[k] = 1; a = c;
    2333             :         /* Cleanup above k */
    2334    13748706 :         for (i = k+1; i<n; i++)
    2335             :         {
    2336             :           long j, ck;
    2337    13279819 :           c = gel(basis, i);
    2338    13279819 :           ck = c[k];
    2339    13279819 :           if (!ck) continue;
    2340     2756766 :           ck = mod_p-ck;
    2341    99825239 :           for (j = 1; j < k; j++) if (a[j]) c[j] = (c[j] + ck*a[j]) % mod_p;
    2342     2756766 :           c[k] = 0;
    2343             :         }
    2344      468887 :         cache->missing--;
    2345      468887 :         break;
    2346             :       }
    2347             :     }
    2348             :   }
    2349             :   else
    2350      396370 :     k = (cache->last - cache->base) + 1;
    2351     2416627 :   if (k || cache->relsup > 0 || (m && in_rnd_rel))
    2352             :   {
    2353             :     REL_t *rel;
    2354             : 
    2355      988490 : ADD_REL:
    2356      992641 :     rel = ++cache->last;
    2357      992641 :     if (!k && cache->relsup && nz < n+1)
    2358             :     {
    2359      122906 :       cache->relsup--;
    2360      122906 :       k = (rel - cache->base) + cache->missing;
    2361             :     }
    2362      992641 :     rel->R  = gclone(R);
    2363      992652 :     rel->m  = m ? gclone(m) : NULL;
    2364      992630 :     rel->nz = nz;
    2365      992630 :     if (aut)
    2366             :     {
    2367      288340 :       rel->relorig = (rel - cache->base) - orig;
    2368      288340 :       rel->relaut = aut;
    2369             :     }
    2370             :     else
    2371      704290 :       rel->relaut = 0;
    2372      992630 :     if (relp) *relp = rel;
    2373      992630 :     if (DEBUGLEVEL) dbg_newrel(cache);
    2374             :   }
    2375     2420766 :   return k;
    2376             : }
    2377             : 
    2378             : /* m a t_INT or primitive t_COL */
    2379             : static int
    2380     2426560 : add_rel(RELCACHE_t *cache, FB_t *F, GEN R, long nz, GEN m, long in_rnd_rel)
    2381             : {
    2382             :   REL_t *rel;
    2383             :   long k, l, reln;
    2384     2426560 :   const long lauts = lg(F->idealperm), KC = F->KC;
    2385             : 
    2386     2426560 :   k = add_rel_i(cache, R, nz, m, 0, 0, &rel, in_rnd_rel);
    2387     2426622 :   if (k > 0 && typ(m) != t_INT)
    2388             :   {
    2389      531019 :     GEN Rl = cgetg(KC+1, t_VECSMALL);
    2390      531020 :     reln = rel - cache->base;
    2391     1014526 :     for (l = 1; l < lauts; l++)
    2392             :     {
    2393      483501 :       GEN perml = gel(F->idealperm, l);
    2394      483501 :       long i, nzl = perml[nz];
    2395             : 
    2396    20378208 :       for (i = 1; i <= KC; i++) Rl[i] = 0;
    2397    18180980 :       for (i = nz; i <= KC; i++)
    2398    17697479 :         if (R[i])
    2399             :         {
    2400     1283824 :           long v = perml[i];
    2401             : 
    2402     1283824 :           if (v < nzl) nzl = v;
    2403     1283824 :           Rl[v] = R[i];
    2404             :         }
    2405      483501 :       (void)add_rel_i(cache, Rl, nzl, NULL, reln, l, NULL, in_rnd_rel);
    2406             :     }
    2407             :   }
    2408     2426628 :   return k;
    2409             : }
    2410             : 
    2411             : INLINE void
    2412    28185313 : step(GEN x, double *y, GEN inc, long k)
    2413             : {
    2414    28185313 :   if (!y[k])
    2415     2211398 :     x[k]++; /* leading coeff > 0 */
    2416             :   else
    2417             :   {
    2418    25973915 :     long i = inc[k];
    2419    25973915 :     x[k] += i;
    2420    25973915 :     inc[k] = (i > 0)? -1-i: 1-i;
    2421             :   }
    2422    28185313 : }
    2423             : 
    2424             : /* degree n >= 2 */
    2425             : static double
    2426      266150 : Fincke_Pohst_bound(double T, GEN r)
    2427             : {
    2428      266150 :   pari_sp av = avma;
    2429      266150 :   GEN zT = dbltor(T * T), p = gmael(r,1,1), B = NULL;
    2430      266135 :   long i, n = lg(r)-1;
    2431      266135 :   double g = 0.;
    2432      610907 :   for (i = 2; i <= n; i++)
    2433             :   {
    2434      610895 :     p = gmul(p, gmael(r,i,i));
    2435      610918 :     B = sqrtnr(gmul(zT,p), i);
    2436      610908 :     if (i == n || cmprr(B, gmael(r,i+1,i+1)) < 0) break;
    2437             :   }
    2438      266151 :   if (!gisdouble(B,&g)) g = 0.;
    2439      266149 :   return gc_double(av, g);
    2440             : }
    2441             : 
    2442             : static void
    2443     1978470 : fact_update(GEN R, FB_t *F, long ipr, GEN c)
    2444             : {
    2445     1978470 :   GEN pr = gel(F->LP,ipr), p = pr_get_p(pr);
    2446     1978470 :   long v = Z_lval(c, itou(p));
    2447     1978478 :   if (v) R[ipr] -= pr_get_e(pr) * v;
    2448     1978478 : }
    2449             : 
    2450             : static long
    2451      266156 : Fincke_Pohst_ideal_both(RELCACHE_t *cache, GEN V, FB_t *F, GEN nf, GEN I, GEN NI,
    2452             :   FACT *fact, long max_FACT, long Nrelid, FP_t *fp, GEN rex, long jid, long jid0, long e0,
    2453             :   long *Nsmall, long *Nfact)
    2454             : {
    2455             :   pari_sp av;
    2456      266156 :   GEN G = nf_get_G(nf), G0 = nf_get_roundG(nf), r, u, gx, cgx, inc, ideal;
    2457      266155 :   long prec = nf_get_prec(nf), N = nf_get_degree(nf);
    2458      266152 :   long j, k, skipfirst, relid = 0, try_factor = 0, rel = 1;
    2459      266152 :   long try_elt = 0, maxtry_ELEMENT = 4*max_FACT*max_FACT;
    2460             :   double BOUND, B1, B2;
    2461             : 
    2462      266152 :   inc = const_vecsmall(N, 1);
    2463      266151 :   u = ZM_lll(ZM_mul(G0, I), 0.99, LLL_IM);
    2464      266157 :   ideal = ZM_mul(I,u); /* approximate T2-LLL reduction */
    2465      266137 :   r = gaussred_from_QR(RgM_mul(G, ideal), prec); /* Cholesky for T2 | ideal */
    2466      266154 :   if (!r) pari_err_BUG("small_norm (precision too low)");
    2467             : 
    2468     1182342 :   for (k=1; k<=N; k++)
    2469             :   {
    2470      916195 :     if (!gisdouble(gcoeff(r,k,k),&(fp->v[k]))) return 0;
    2471     2721520 :     for (j=1; j<k; j++) if (!gisdouble(gcoeff(r,j,k),&(fp->q[j][k]))) return 0;
    2472      916187 :     if (DEBUGLEVEL>3) err_printf("v[%ld]=%.4g ",k,fp->v[k]);
    2473             :   }
    2474      266147 :   B1 = fp->v[1]; /* T2(ideal[1]) */
    2475      266147 :   B2 = fp->v[2] + B1 * fp->q[1][2] * fp->q[1][2]; /* T2(ideal[2]) */
    2476      266147 :   skipfirst = ZV_isscalar(gel(ideal,1));
    2477      266149 :   BOUND = maxdd(2*B2, Fincke_Pohst_bound(4 * max_FACT / F->ballvol, r));
    2478      266148 :   if (DEBUGLEVEL>1)
    2479             :   {
    2480           0 :     if (DEBUGLEVEL>3) err_printf("\n");
    2481           0 :     err_printf("BOUND = %.4g\n",BOUND);
    2482             :   }
    2483             : 
    2484      266148 :   k = N; fp->y[N] = fp->z[N] = 0; fp->x[N] = 0;
    2485    18616932 :   for (av = avma;; set_avma(av), step(fp->x,fp->y,inc,k))
    2486    18350434 :   {
    2487             :     GEN R;
    2488             :     long nz;
    2489             :     do
    2490             :     { /* look for primitive element of small norm, cf minim00 */
    2491    23457881 :       int fl = 0;
    2492             :       double p;
    2493    23457881 :       if (k > 1)
    2494             :       {
    2495     5107504 :         long l = k-1;
    2496     5107504 :         fp->z[l] = 0;
    2497    45491766 :         for (j=k; j<=N; j++) fp->z[l] += fp->q[l][j]*fp->x[j];
    2498     5107504 :         p = (double)fp->x[k] + fp->z[k];
    2499     5107504 :         fp->y[l] = fp->y[k] + p*p*fp->v[k];
    2500     5107504 :         if (l <= skipfirst && !fp->y[1]) fl = 1;
    2501     5107504 :         fp->x[l] = (long)floor(-fp->z[l] + 0.5);
    2502     5107504 :         k = l;
    2503             :       }
    2504     4463022 :       for(;; step(fp->x,fp->y,inc,k))
    2505             :       {
    2506    27920869 :         if (!fl)
    2507             :         {
    2508    27881083 :           if (++try_elt > maxtry_ELEMENT) goto END_Fincke_Pohst_ideal;
    2509    27878521 :           p = (double)fp->x[k] + fp->z[k];
    2510    27878521 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2511             : 
    2512     5371693 :           step(fp->x,fp->y,inc,k);
    2513             : 
    2514     5371870 :           p = (double)fp->x[k] + fp->z[k];
    2515     5371870 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2516             :         }
    2517     4465584 :         fl = 0; inc[k] = 1;
    2518     4465584 :         if (++k > N) goto END_Fincke_Pohst_ideal;
    2519             :       }
    2520    23455462 :     } while (k > 1);
    2521             : 
    2522             :     /* element complete */
    2523    33592039 :     if (zv_content(fp->x) !=1) continue; /* not primitive */
    2524    15529312 :     gx = ZM_zc_mul(ideal,fp->x);
    2525    15529259 :     if (ZV_isscalar(gx)) continue;
    2526    15652850 :     if (++try_factor > max_FACT) break;
    2527             : 
    2528    15488956 :     if (DEBUGLEVEL && Nsmall) (*Nsmall)++;
    2529    15488956 :     if (!factorgen(F,nf,I,NI,gx,fact)) continue;
    2530     2351737 :     if (!Nrelid) return 1;
    2531     2252035 :     if (jid == jid0)
    2532       28742 :       add_to_fact(jid, 1 + e0, fact);
    2533             :     else
    2534             :     {
    2535     2223293 :       add_to_fact(jid, 1, fact);
    2536     2223524 :       if (jid0) add_to_fact(jid0, e0, fact);
    2537             :     }
    2538             : 
    2539             :     /* smooth element */
    2540     2252266 :     R = set_fact(F, fact, rex, &nz);
    2541     2252278 :     cgx = Z_content(gx);
    2542     2252245 :     if (cgx)
    2543             :     { /* relatively rare, compute relation attached to gx/cgx */
    2544      485362 :       long i, n = fact[0].pr;
    2545      485362 :       gx = Q_div_to_int(gx, cgx);
    2546     2414180 :       for (i = 1; i <= n; i++) fact_update(R, F, fact[i].pr, cgx);
    2547      485360 :       if (rex)
    2548             :       {
    2549       32864 :         long l = lg(rex);
    2550      108115 :         for (i = 1; i < l; i++)
    2551       75252 :           if (rex[i])
    2552             :           {
    2553       73250 :             long t, ipr = F->subFB[i];
    2554      235086 :             for (t = 1; t <= n; t++)
    2555      185444 :               if (fact[t].pr == ipr) break;
    2556       73250 :             if (t > n) fact_update(R, F, ipr, cgx);
    2557             :           }
    2558             :       }
    2559             :     }
    2560     2252242 :     if (DEBUGLEVEL && Nfact) (*Nfact)++;
    2561     2252242 :     if (cache)
    2562             :     {
    2563             :       /* make sure we get maximal rank first, then allow all relations */
    2564     2252242 :       if (add_rel(cache, F, R, nz, gx, rex? 1: 0) <= 0)
    2565             :       { /* probably Q-dependent from previous ones: forget it */
    2566     1721741 :         if (DEBUGLEVEL>1) err_printf("*");
    2567     1721741 :         continue;
    2568             :       }
    2569      530570 :       if (cache->last >= cache->end) return 1; /* we have enough */
    2570             :     } else
    2571             :     {
    2572           0 :       gel(V,rel++) = gerepilecopy(av, mkvec3(R, stoi(nz), gx));
    2573           5 :       av = avma;
    2574             :     }
    2575      430887 :     if (++relid == Nrelid) break;
    2576             :   }
    2577      166456 :   END_Fincke_Pohst_ideal:
    2578      166456 :   return 0;
    2579             : }
    2580             : 
    2581             : static long
    2582      266144 : Fincke_Pohst_ideal(RELCACHE_t *cache, FB_t *F, GEN nf, GEN I, GEN NI,
    2583             :   FACT *fact, long Nrelid, long max_FACT, FP_t *fp, GEN rex, long jid, long jid0, long e0,
    2584             :   long *Nsmall, long *Nfact)
    2585             : {
    2586      266144 :   return Fincke_Pohst_ideal_both(cache, NULL,
    2587             :     F, nf, I, NI, fact, max_FACT, Nrelid, fp, rex, jid, jid0, e0, Nsmall, Nfact);
    2588             : }
    2589             : 
    2590             : static long
    2591           0 : Fincke_Pohst_ideal_par(GEN V, FB_t *F, GEN nf, GEN I, GEN NI,
    2592             :   FACT *fact, long max_FACT, FP_t *fp, GEN rex, long jid, long jid0, long e0,
    2593             :   long *Nsmall, long *Nfact)
    2594             : {
    2595           0 :   return Fincke_Pohst_ideal_both(NULL, V,
    2596             :     F, nf, I, NI, fact, max_FACT, max_FACT, fp, rex, jid, jid0, e0, Nsmall, Nfact);
    2597             : }
    2598             : 
    2599             : static GEN
    2600           0 : pack_FB(FB_t *F, long s)
    2601             : {
    2602           0 :   return mkvecn(s ? 8: 7, F->FB, F->LP, F->LV, F->iLP, F->idealperm, F->prodZ,
    2603             :            mkvecsmall3(F->KC,F->KCZ,F->KCZ2), F->subFB);
    2604             : }
    2605             : 
    2606             : static void
    2607           0 : unpack_FB(FB_t *F, GEN P)
    2608             : {
    2609           0 :   F->FB = gel(P,1);
    2610           0 :   F->LP = gel(P,2);
    2611           0 :   F->LV = gel(P,3);
    2612           0 :   F->iLP = gel(P,4);
    2613           0 :   F->idealperm = gel(P,5);
    2614           0 :   F->prodZ = gel(P,6);
    2615           0 :   F->KC = mael(P,7,1);
    2616           0 :   F->KCZ = mael(P,7,2);
    2617           0 :   F->KCZ2 = mael(P,7,3);
    2618           0 :   if (lg(P) > 8)
    2619           0 :     F->subFB = gel(P,8);
    2620           0 : }
    2621             : 
    2622             : GEN
    2623           0 : bnfinit_FP_worker(GEN INI, GEN PF, GEN nf, long max_FACT, GEN rex, long jid0, long e0)
    2624             : {
    2625           0 :   pari_sp av = avma;
    2626             :   FB_t F;
    2627             :   FP_t fp;
    2628             :   FACT * fact;
    2629           0 :   long Nsmall = 0, Nfact = 0, res, N = nf_get_degree(nf), jid = itos(gel(INI,3));
    2630           0 :   GEN vec = zerovec(max_FACT);
    2631           0 :   GEN I = gel(INI,1), NI = gel(INI,2);
    2632           0 :   if (isintzero(rex)) rex = NULL;
    2633           0 :   unpack_FB(&F, PF);
    2634           0 :   F.ballvol = ballvol(N);
    2635           0 :   minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2636           0 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    2637           0 :   res = Fincke_Pohst_ideal_par(vec, &F, nf, I, NI, fact, max_FACT, &fp, rex, jid, jid0, e0, &Nsmall, &Nfact);
    2638           0 :   return gerepilecopy(av, mkvec2(vec, mkvecsmall3(res, Nsmall, Nfact)));
    2639             : }
    2640             : 
    2641             : static void
    2642           0 : small_norm_par(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, long max_FACT, long idex, long nbthr, long j0)
    2643             : {
    2644           0 :   GEN L_jid = F->L_jid, Np0 = NULL, p0 = j0? gel(F->LP,j0): NULL;
    2645           0 :   long Nsmall, Nfact, n = lg(L_jid)-1, e0 = 0;
    2646             :   pari_timer T;
    2647           0 :   long nt = nbthr? nbthr: mt_nbthreads();
    2648           0 :   GEN worker, vec = cgetg(nt+1, t_VEC);
    2649             : 
    2650           0 :   if (DEBUGLEVEL)
    2651             :   {
    2652           0 :     timer_start(&T);
    2653           0 :     err_printf("#### Look for %ld relations in %ld ideals (small_norm)\n",
    2654           0 :                cache->end - cache->last, lg(L_jid)-1);
    2655           0 :     if (p0) err_printf("Look in p0 = %Ps\n", vecslice(p0,1,4));
    2656             :   }
    2657           0 :   Nsmall = Nfact = 0;
    2658           0 :   if (p0)
    2659             :   {
    2660           0 :     GEN N0 = pr_norm(p0);
    2661           0 :     e0 = idex ? idex: logint0(sqri(pr_norm(veclast(F->LP))), N0, NULL);
    2662           0 :     p0 = idealpows(nf, p0, e0);
    2663           0 :     Np0 = powiu(N0, e0);
    2664             :   }
    2665           0 :   worker = snm_closure(is_entry("_bnfinit_FP_worker"),
    2666             :            mkcol6(pack_FB(F,0), nf, stoi(max_FACT), gen_0, stoi(j0), stoi(e0)));
    2667           0 :   while(n)
    2668             :   {
    2669             :     GEN VB;
    2670           0 :     long k, m = minss(n, nt);
    2671           0 :     for (k = 1; k <= m; k++, n--)
    2672             :     {
    2673           0 :       long j = L_jid[n];
    2674           0 :       GEN id = gel(F->LP,j), Nid;
    2675           0 :       if (p0)
    2676           0 :       { Nid = mulii(Np0, pr_norm(id)); id = idealmul(nf, p0, id); }
    2677             :       else
    2678           0 :       { Nid = pr_norm(id); id = pr_hnf(nf, id); }
    2679           0 :       gel(vec,k) = mkvec3(id, Nid, stoi(j));
    2680             :     }
    2681           0 :     setlg(vec,k);
    2682           0 :     VB = gen_parapply(worker,vec);
    2683           0 :     for (k = 1; k <= m; k++)
    2684             :     {
    2685           0 :       GEN B = gel(VB,k), B1 = gel(B,1), B2 = gel(B,2);
    2686           0 :       long i, lB = lg(B1);
    2687           0 :       Nsmall += B2[2]; Nfact += B2[3];
    2688           0 :       for (i = 1; i<lB && !isintzero(gel(B1,i)); i++)
    2689             :       {
    2690           0 :         GEN Bi = gel(B1,i), R = gel(Bi,1), gx = gel(Bi,3);
    2691           0 :         long nz = itos(gel(Bi,2));
    2692           0 :         if (cache->last < cache->end)
    2693           0 :           add_rel(cache, F, R, nz, gx, 0);
    2694             :       }
    2695           0 :       if (cache->last >= cache->end) { n = 0; break; }
    2696             :     }
    2697             :   }
    2698           0 :   if (DEBUGLEVEL && Nsmall)
    2699             :   {
    2700           0 :     if (DEBUGLEVEL == 1)
    2701           0 :     { if (Nfact) err_printf("\n"); }
    2702             :     else
    2703           0 :       err_printf("  \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
    2704           0 :                   Nfact,Nsmall,((double)Nfact)/Nsmall);
    2705           0 :     if (timer_get(&T)>1) timer_printf(&T,"small_norm");
    2706             :   }
    2707           0 : }
    2708             : 
    2709             : static void
    2710       66748 : small_norm_seq(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, long max_fact, long idex, FACT *fact, long j0)
    2711             : {
    2712       66748 :   const long N = nf_get_degree(nf);
    2713             :   FP_t fp;
    2714             :   pari_sp av;
    2715       66748 :   GEN L_jid = F->L_jid, Np0 = NULL, p0 = j0? gel(F->LP,j0): NULL;
    2716       66748 :   long Nsmall, Nfact, n = lg(L_jid), e0 = 0;
    2717             :   pari_timer T;
    2718             : 
    2719       66748 :   if (DEBUGLEVEL)
    2720             :   {
    2721           0 :     timer_start(&T);
    2722           0 :     err_printf("#### Look for %ld relations in %ld ideals (small_norm)\n",
    2723           0 :                cache->end - cache->last, lg(L_jid)-1);
    2724           0 :     if (p0) err_printf("Look in p0 = %Ps\n", vecslice(p0,1,4));
    2725             :   }
    2726       66748 :   Nsmall = Nfact = 0;
    2727       66748 :   minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2728       66748 :   if (p0)
    2729             :   {
    2730       26673 :     GEN n = pr_norm(p0);
    2731       26673 :     e0 = idex ? idex: logint0(sqri(pr_norm(veclast(F->LP))), n, NULL);
    2732       26672 :     p0 = idealpows(nf, p0, e0);
    2733       26673 :     Np0 = powiu(n,e0);
    2734             :   }
    2735      164710 :   for (av = avma; --n; set_avma(av))
    2736             :   {
    2737      164310 :     long j = L_jid[n];
    2738      164310 :     GEN id = gel(F->LP, j), Nid;
    2739      164310 :     if (DEBUGLEVEL>1)
    2740           0 :       err_printf("\n*** Ideal no %ld: %Ps\n", j, vecslice(id,1,4));
    2741      164310 :     if (p0)
    2742             :     {
    2743       32325 :       if (j == j0)
    2744             :       { /* avoid trivial relation */
    2745        3899 :         long e = pr_get_e(id);
    2746        3899 :         if ((e0 + 1) % e == 0 && e * pr_get_f(id) == N) continue;
    2747             :       }
    2748       31681 :       Nid = mulii(Np0, pr_norm(id)); id = idealmul(nf, p0, id);
    2749             :     }
    2750             :     else
    2751      131985 :     { Nid = pr_norm(id); id = pr_hnf(nf, id);}
    2752      163668 :     if (Fincke_Pohst_ideal(cache, F, nf, id, Nid, fact, Nrelid, max_fact, &fp,
    2753       66351 :                            NULL, j, j0, e0, &Nsmall, &Nfact)) break;
    2754             :   }
    2755       66749 :   if (DEBUGLEVEL && Nsmall)
    2756             :   {
    2757           0 :     if (DEBUGLEVEL == 1)
    2758           0 :     { if (Nfact) err_printf("\n"); }
    2759             :     else
    2760           0 :       err_printf("  \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
    2761           0 :                   Nfact,Nsmall,((double)Nfact)/Nsmall);
    2762           0 :     if (timer_get(&T)>1) timer_printf(&T,"small_norm");
    2763             :   }
    2764       66749 : }
    2765             : 
    2766             : static void
    2767       66748 : small_norm(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, long max_fact, long idex, long nbthr, FACT *fact, long j0)
    2768             : {
    2769       66748 :   if (nbthr==1)
    2770       66748 :     return small_norm_seq(cache, F, nf, Nrelid, max_fact, idex, fact, j0);
    2771             :   else
    2772           0 :     return small_norm_par(cache, F, nf, Nrelid, max_fact, idex, nbthr, j0);
    2773             : }
    2774             : 
    2775             : static GEN
    2776       55628 : get_random_ideal(FB_t *F, GEN nf, GEN ex)
    2777             : {
    2778       55628 :   long i, l = lg(ex);
    2779             :   for (;;)
    2780        1044 :   {
    2781       56672 :     GEN I = NULL;
    2782      158798 :     for (i = 1; i < l; i++)
    2783      102126 :       if ((ex[i] = random_bits(RANDOM_BITS)))
    2784             :       {
    2785       97863 :         GEN pr = gel(F->LP, F->subFB[i]), e = utoipos(ex[i]);
    2786       97863 :         I = I? idealmulpowprime(nf, I, pr, e): idealpow(nf, pr, e);
    2787             :       }
    2788       56672 :     if (I && !ZM_isscalar(I,NULL)) return I; /* != (n)Z_K */
    2789             :   }
    2790             : }
    2791             : 
    2792             : static void
    2793           0 : rnd_rel_par(RELCACHE_t *cache, FB_t *F, GEN nf, long max_FACT)
    2794             : {
    2795             :   pari_timer T;
    2796           0 :   GEN L_jid = F->L_jid, R, ex;
    2797           0 :   long k, l = lg(L_jid), Nfact = 0, Nsmall = 0;
    2798             :   GEN worker, vec, VB, NR;
    2799             : 
    2800           0 :   if (DEBUGLEVEL) {
    2801           0 :     timer_start(&T);
    2802           0 :     err_printf("#### Look for %ld relations in %ld ideals (rnd_rel)\n",
    2803           0 :                cache->end - cache->last, l-1);
    2804             :   }
    2805           0 :   ex = cgetg(lg(F->subFB), t_VECSMALL);
    2806           0 :   R = get_random_ideal(F, nf, ex); /* random product from subFB */
    2807           0 :   NR = ZM_det_triangular(R);
    2808           0 :   worker = snm_closure(is_entry("_bnfinit_FP_worker"),
    2809             :            mkcol6(pack_FB(F,1), nf, stoi(max_FACT), ex, gen_0, gen_0));
    2810           0 :   vec = cgetg(l, t_VEC);
    2811           0 :   for (k = 1; k < l; k++)
    2812             :   {
    2813           0 :     long j = L_jid[k];
    2814           0 :     GEN id = gel(F->LP,j), Nid;
    2815           0 :     Nid = mulii(NR, pr_norm(id)); id = idealmul(nf, R, id);
    2816           0 :     gel(vec,k) = mkvec3(id, Nid, stoi(j));
    2817             :   }
    2818           0 :   VB = gen_parapply(worker,vec);
    2819           0 :   for (k = 1; k < l; k++)
    2820             :   {
    2821           0 :     GEN B = gel(VB,k), B1 = gel(B,1), B2 = gel(B,2);
    2822           0 :     long i, lB = lg(B1);
    2823           0 :     Nsmall += B2[2]; Nfact += B2[3];
    2824           0 :     for (i = 1; i<lB && !isintzero(gel(B1,i)); i++)
    2825             :     {
    2826           0 :       GEN Bi = gel(B1,i), R = gel(Bi,1), gx = gel(Bi,3);
    2827           0 :       long nz = itos(gel(Bi,2));
    2828           0 :       if (cache->last < cache->end)
    2829           0 :         add_rel(cache, F, R, nz, gx, 1);
    2830             :     }
    2831           0 :     if (cache->last >= cache->end) break;
    2832             :   }
    2833             : 
    2834           0 :   if (DEBUGLEVEL)
    2835             :   {
    2836           0 :     if (DEBUGLEVEL == 1)
    2837           0 :     { if (Nfact) err_printf("\n"); }
    2838             :     else
    2839           0 :       err_printf("  \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
    2840           0 :                   Nfact,Nsmall,((double)Nfact)/Nsmall);
    2841           0 :     if (timer_get(&T)>=0) timer_printf(&T,"rnd_rel");
    2842             :   }
    2843           0 : }
    2844             : 
    2845             : static void
    2846       55629 : rnd_rel_seq(RELCACHE_t *cache, FB_t *F, GEN nf, long max_fact, FACT *fact)
    2847             : {
    2848             :   pari_timer T;
    2849       55629 :   GEN L_jid = F->L_jid, R, NR, ex;
    2850       55629 :   long i, l = lg(L_jid), Nfact = 0;
    2851             :   FP_t fp;
    2852             :   pari_sp av;
    2853             : 
    2854       55629 :   if (DEBUGLEVEL) {
    2855           0 :     timer_start(&T);
    2856           0 :     err_printf("#### Look for %ld relations in %ld ideals (rnd_rel)\n",
    2857           0 :                cache->end - cache->last, l-1);
    2858             :   }
    2859       55629 :   ex = cgetg(lg(F->subFB), t_VECSMALL);
    2860       55628 :   R = get_random_ideal(F, nf, ex); /* random product from subFB */
    2861       55629 :   NR = ZM_det_triangular(R);
    2862       55627 :   minim_alloc(nf_get_degree(nf)+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2863      124767 :   for (av = avma, i = 1; i < l; i++, set_avma(av))
    2864             :   { /* try P[j] * base */
    2865      102475 :     long j = L_jid[i];
    2866      102475 :     GEN P = gel(F->LP, j), Nid = mulii(NR, pr_norm(P));
    2867      102460 :     if (DEBUGLEVEL>1) err_printf("\n*** Ideal %ld: %Ps\n", j, vecslice(P,1,4));
    2868      102460 :     if (Fincke_Pohst_ideal(cache, F, nf, idealHNF_mul(nf, R, P), Nid, fact,
    2869       33337 :           RND_REL_RELPID, max_fact, &fp, ex, j, 0, 0, NULL, &Nfact)) break;
    2870             :   }
    2871       55629 :   if (DEBUGLEVEL)
    2872             :   {
    2873           0 :     if (Nfact) err_printf("\n");
    2874           0 :     if (timer_get(&T)>=0) timer_printf(&T,"rnd_rel");
    2875             :   }
    2876       55629 : }
    2877             : static void
    2878       55629 : rnd_rel(RELCACHE_t *cache, FB_t *F, GEN nf, long max_fact, long nbthr, FACT *fact)
    2879             : {
    2880       55629 :   if (nbthr==1)
    2881       55629 :     return rnd_rel_seq(cache, F, nf, max_fact, fact);
    2882             :   else
    2883           0 :     return rnd_rel_par(cache, F, nf, max_fact);
    2884             : }
    2885             : 
    2886             : static GEN
    2887       64022 : automorphism_perms(GEN M, GEN auts, GEN cyclic, long r1, long r2, long N)
    2888             : {
    2889       64022 :   long L = lgcols(M), lauts = lg(auts), lcyc = lg(cyclic), i, j, l, m;
    2890       64022 :   GEN Mt, perms = cgetg(lauts, t_VEC);
    2891             :   pari_sp av;
    2892             : 
    2893      128891 :   for (l = 1; l < lauts; l++) gel(perms, l) = cgetg(L, t_VECSMALL);
    2894       64022 :   av = avma;
    2895       64022 :   Mt = shallowtrans(gprec_w(M, LOWDEFAULTPREC));
    2896       64021 :   Mt = shallowconcat(Mt, conj_i(vecslice(Mt, r1+1, r1+r2)));
    2897      111510 :   for (l = 1; l < lcyc; l++)
    2898             :   {
    2899       47488 :     GEN thiscyc = gel(cyclic, l), thisperm, perm, prev, Nt;
    2900       47488 :     long k = thiscyc[1];
    2901             : 
    2902       47488 :     Nt = RgM_mul(shallowtrans(gel(auts, k)), Mt);
    2903       47490 :     perm = gel(perms, k);
    2904      157360 :     for (i = 1; i < L; i++)
    2905             :     {
    2906      109872 :       GEN v = gel(Nt, i), minD;
    2907      109872 :       minD = gnorml2(gsub(v, gel(Mt, 1)));
    2908      109875 :       perm[i] = 1;
    2909      577360 :       for (j = 2; j <= N; j++)
    2910             :       {
    2911      467490 :         GEN D = gnorml2(gsub(v, gel(Mt, j)));
    2912      467468 :         if (gcmp(D, minD) < 0) { minD = D; perm[i] = j >= L ? r2-j : j; }
    2913             :       }
    2914             :     }
    2915       66101 :     for (prev = perm, m = 2; m < lg(thiscyc); m++, prev = thisperm)
    2916             :     {
    2917       18613 :       thisperm = gel(perms, thiscyc[m]);
    2918       94360 :       for (i = 1; i < L; i++)
    2919             :       {
    2920       75747 :         long pp = labs(prev[i]);
    2921       75747 :         thisperm[i] = prev[i] < 0 ? -perm[pp] : perm[pp];
    2922             :       }
    2923             :     }
    2924             :   }
    2925       64022 :   set_avma(av); return perms;
    2926             : }
    2927             : 
    2928             : /* Determine the field automorphisms as matrices on the integral basis */
    2929             : static GEN
    2930       64084 : automorphism_matrices(GEN nf, GEN *cycp)
    2931             : {
    2932       64084 :   pari_sp av = avma;
    2933       64084 :   GEN auts = galoisconj(nf, NULL), mats, cyclic, cyclicidx;
    2934       64085 :   long nauts = lg(auts)-1, i, j, k, l;
    2935             : 
    2936       64085 :   cyclic = cgetg(nauts+1, t_VEC);
    2937       64085 :   cyclicidx = zero_Flv(nauts);
    2938       98518 :   for (l = 1; l <= nauts; l++)
    2939             :   {
    2940       98518 :     GEN aut = gel(auts, l);
    2941       98518 :     if (gequalX(aut)) { swap(gel(auts, l), gel(auts, nauts)); break; }
    2942             :   }
    2943             :   /* trivial automorphism is last */
    2944      193067 :   for (l = 1; l <= nauts; l++) gel(auts, l) = algtobasis(nf, gel(auts, l));
    2945             :   /* Compute maximal cyclic subgroups */
    2946      128982 :   for (l = nauts; --l > 0; ) if (!cyclicidx[l])
    2947             :   {
    2948       48993 :     GEN elt = gel(auts, l), aut = elt, cyc = cgetg(nauts+1, t_VECSMALL);
    2949       48993 :     cyc[1] = cyclicidx[l] = l; j = 1;
    2950             :     do
    2951             :     {
    2952       68151 :       elt = galoisapply(nf, elt, aut);
    2953      221746 :       for (k = 1; k <= nauts; k++) if (gequal(elt, gel(auts, k))) break;
    2954       68151 :       cyclicidx[k] = l; cyc[++j] = k;
    2955             :     }
    2956       68151 :     while (k != nauts);
    2957       48993 :     setlg(cyc, j);
    2958       48993 :     gel(cyclic, l) = cyc;
    2959             :   }
    2960      128982 :   for (i = j = 1; i < nauts; i++)
    2961       64897 :     if (cyclicidx[i] == i) cyclic[j++] = cyclic[i];
    2962       64085 :   setlg(cyclic, j);
    2963       64085 :   mats = cgetg(nauts, t_VEC);
    2964      111601 :   while (--j > 0)
    2965             :   {
    2966       47516 :     GEN cyc = gel(cyclic, j);
    2967       47516 :     long id = cyc[1];
    2968       47516 :     GEN M, Mi, aut = gel(auts, id);
    2969             : 
    2970       47516 :     gel(mats, id) = Mi = M = nfgaloismatrix(nf, aut);
    2971       66129 :     for (i = 2; i < lg(cyc); i++) gel(mats, cyc[i]) = Mi = ZM_mul(Mi, M);
    2972             :   }
    2973       64085 :   (void)gc_all(av, 2, &mats, &cyclic);
    2974       64085 :   if (cycp) *cycp = cyclic;
    2975       64085 :   return mats;
    2976             : }
    2977             : 
    2978             : /* vP a list of maximal ideals above the same p from idealprimedec: f(P/p) is
    2979             :  * increasing; 1 <= j <= #vP; orbit a zc of length <= #vP; auts a vector of
    2980             :  * automorphisms in ZM form.
    2981             :  * Set orbit[i] = 1 for all vP[i], i >= j, in the orbit of pr = vP[j] wrt auts.
    2982             :  * N.B.1 orbit need not be initialized to 0: useful to incrementally run
    2983             :  * through successive orbits
    2984             :  * N.B.2 i >= j, so primes with index < j will be missed; run incrementally
    2985             :  * starting from j = 1 ! */
    2986             : static void
    2987       11865 : pr_orbit_fill(GEN orbit, GEN auts, GEN vP, long j)
    2988             : {
    2989       11865 :   GEN pr = gel(vP,j), gen = pr_get_gen(pr);
    2990       11865 :   long i, l = lg(auts), J = lg(orbit), f = pr_get_f(pr);
    2991       11865 :   orbit[j] = 1;
    2992       23730 :   for (i = 1; i < l; i++)
    2993             :   {
    2994       11865 :     GEN g = ZM_ZC_mul(gel(auts,i), gen);
    2995             :     long k;
    2996       11886 :     for (k = j+1; k < J; k++)
    2997             :     {
    2998          35 :       GEN prk = gel(vP,k);
    2999          35 :       if (pr_get_f(prk) > f) break; /* f(P[k]) increases with k */
    3000             :       /* don't check that e matches: (almost) always 1 ! */
    3001          35 :       if (!orbit[k] && ZC_prdvd(g, prk)) { orbit[k] = 1; break; }
    3002             :     }
    3003             :   }
    3004       11865 : }
    3005             : /* remark: F->KCZ changes if be_honest() fails */
    3006             : static int
    3007           7 : be_honest(FB_t *F, GEN nf, GEN auts, FACT *fact)
    3008             : {
    3009             :   long i, iz, nbtest;
    3010           7 :   long lgsub = lg(F->subFB), KCZ0 = F->KCZ, N = nf_get_degree(nf);
    3011             :   FP_t fp;
    3012             :   pari_sp av;
    3013             : 
    3014           7 :   if (DEBUGLEVEL) {
    3015           0 :     err_printf("Be honest for %ld primes from %ld to %ld\n", F->KCZ2 - F->KCZ,
    3016           0 :                F->FB[ F->KCZ+1 ], F->FB[ F->KCZ2 ]);
    3017             :   }
    3018           7 :   minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    3019           7 :   if (lg(auts) == 1) auts = NULL;
    3020           7 :   av = avma;
    3021          14 :   for (iz=F->KCZ+1; iz<=F->KCZ2; iz++, set_avma(av))
    3022             :   {
    3023           7 :     long p = F->FB[iz];
    3024           7 :     GEN pr_orbit, P = gel(F->LV,p);
    3025           7 :     long j, J = lg(P); /* > 1 */
    3026             :     /* the P|p, NP > C2 are assumed in subgroup generated by FB + last P
    3027             :      * with NP <= C2 is unramified --> check all but last */
    3028           7 :     if (pr_get_e(gel(P,J-1)) == 1) J--;
    3029           7 :     if (J == 1) continue;
    3030           7 :     if (DEBUGLEVEL>1) err_printf("%ld ", p);
    3031           7 :     pr_orbit = auts? zero_zv(J-1): NULL;
    3032          28 :     for (j = 1; j < J; j++)
    3033             :     {
    3034             :       GEN Nid, id, id0;
    3035          21 :       if (pr_orbit)
    3036             :       {
    3037          21 :         if (pr_orbit[j]) continue;
    3038             :         /* discard all primes in automorphism orbit simultaneously */
    3039          14 :         pr_orbit_fill(pr_orbit, auts, P, j);
    3040             :       }
    3041          14 :       id = id0 = pr_hnf(nf,gel(P,j));
    3042          14 :       Nid = pr_norm(gel(P,j));
    3043          14 :       for (nbtest=0;;)
    3044             :       {
    3045          14 :         if (Fincke_Pohst_ideal(NULL, F, nf, id, Nid, fact, 0, MAXTRY_FACT, &fp,
    3046          14 :                                NULL, 0, 0, 0, NULL, NULL)) break;
    3047           0 :         if (++nbtest > maxtry_HONEST)
    3048             :         {
    3049           0 :           if (DEBUGLEVEL)
    3050           0 :             pari_warn(warner,"be_honest() failure on prime %Ps\n", gel(P,j));
    3051           0 :           return 0;
    3052             :         }
    3053             :         /* occurs at most once in the whole function */
    3054           0 :         for (i = 1, id = id0; i < lgsub; i++)
    3055             :         {
    3056           0 :           long ex = random_bits(RANDOM_BITS);
    3057           0 :           if (ex)
    3058             :           {
    3059           0 :             GEN pr = gel(F->LP, F->subFB[i]);
    3060           0 :             id = idealmulpowprime(nf, id, pr, utoipos(ex));
    3061             :           }
    3062             :         }
    3063           0 :         if (!equali1(gcoeff(id,N,N))) id = Q_primpart(id);
    3064           0 :         if (expi(gcoeff(id,1,1)) > 100) id = idealred(nf, id);
    3065           0 :         Nid = ZM_det_triangular(id);
    3066             :       }
    3067             :     }
    3068           7 :     F->KCZ++; /* SUCCESS, "enlarge" factorbase */
    3069             :   }
    3070           7 :   F->KCZ = KCZ0; return gc_bool(av,1);
    3071             : }
    3072             : 
    3073             : /* all primes with N(P) <= BOUND factor on factorbase ? */
    3074             : void
    3075          63 : bnftestprimes(GEN bnf, GEN BOUND)
    3076             : {
    3077          63 :   pari_sp av0 = avma, av;
    3078          63 :   ulong count = 0;
    3079          63 :   GEN auts, p, nf = bnf_get_nf(bnf), Vbase = bnf_get_vbase(bnf);
    3080          63 :   GEN fb = gen_sort_shallow(Vbase, (void*)&cmp_prime_ideal, cmp_nodata);
    3081          63 :   ulong pmax = pr_get_smallp(veclast(fb)); /*largest p in factorbase*/
    3082             :   forprime_t S;
    3083             :   FACT *fact;
    3084             :   FB_t F;
    3085             : 
    3086          63 :   (void)recover_partFB(&F, Vbase, nf_get_degree(nf));
    3087          63 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    3088          63 :   forprime_init(&S, gen_2, BOUND);
    3089          63 :   auts = automorphism_matrices(nf, NULL);
    3090          63 :   if (lg(auts) == 1) auts = NULL;
    3091          63 :   av = avma;
    3092       37604 :   while (( p = forprime_next(&S) ))
    3093             :   {
    3094             :     GEN pr_orbit, vP;
    3095             :     long j, J;
    3096             : 
    3097       37541 :     if (DEBUGLEVEL == 1 && ++count > 1000)
    3098             :     {
    3099           0 :       err_printf("passing p = %Ps / %Ps\n", p, BOUND);
    3100           0 :       count = 0;
    3101             :     }
    3102       37541 :     set_avma(av);
    3103       37541 :     vP = idealprimedec_limit_norm(nf, p, BOUND);
    3104       37541 :     J = lg(vP);
    3105             :     /* if last is unramified, all P|p in subgroup generated by FB: skip last */
    3106       37541 :     if (J > 1 && pr_get_e(gel(vP,J-1)) == 1) J--;
    3107       37541 :     if (J == 1) continue;
    3108       14525 :     if (DEBUGLEVEL>1) err_printf("*** p = %Ps\n",p);
    3109       14525 :     pr_orbit = auts? zero_zv(J-1): NULL;
    3110       31549 :     for (j = 1; j < J; j++)
    3111             :     {
    3112       17024 :       GEN P = gel(vP,j);
    3113       17024 :       long k = 0;
    3114       17024 :       if (pr_orbit)
    3115             :       {
    3116       11858 :         if (pr_orbit[j]) continue;
    3117             :         /* discard all primes in automorphism orbit simultaneously */
    3118       11851 :         pr_orbit_fill(pr_orbit, auts, vP, j);
    3119             :       }
    3120       17017 :       if (abscmpiu(p, pmax) > 0 || !(k = tablesearch(fb, P, &cmp_prime_ideal)))
    3121       16408 :         (void)SPLIT(&F, nf, pr_hnf(nf,P), Vbase, fact);
    3122       17017 :       if (DEBUGLEVEL>1)
    3123             :       {
    3124           0 :         err_printf("  Testing P = %Ps\n",P);
    3125           0 :         if (k) err_printf("    #%ld in factor base\n",k);
    3126           0 :         else err_printf("    is %Ps\n", isprincipal(bnf,P));
    3127             :       }
    3128             :     }
    3129             :   }
    3130          63 :   set_avma(av0);
    3131          63 : }
    3132             : 
    3133             : /* A t_MAT of complex floats, in fact reals. Extract a submatrix B
    3134             :  * whose columns are definitely nonzero, i.e. gexpo(A[j]) >= -2
    3135             :  *
    3136             :  * If possible precision problem (t_REAL 0 with large exponent), set
    3137             :  * *precpb to 1 */
    3138             : static GEN
    3139       93063 : clean_cols(GEN A, int *precpb)
    3140             : {
    3141       93063 :   long l = lg(A), h, i, j, k;
    3142             :   GEN B;
    3143       93063 :   *precpb = 0;
    3144       93063 :   if (l == 1) return A;
    3145       93063 :   h = lgcols(A);;
    3146       93063 :   B = cgetg(l, t_MAT);
    3147     1093493 :   for (i = k = 1; i < l; i++)
    3148             :   {
    3149     1000430 :     GEN Ai = gel(A,i);
    3150     1000430 :     int non0 = 0;
    3151     4361251 :     for (j = 1; j < h; j++)
    3152             :     {
    3153     3360821 :       GEN c = gel(Ai,j);
    3154     3360821 :       if (gexpo(c) >= -2)
    3155             :       {
    3156     2021505 :         if (gequal0(c)) *precpb = 1; else non0 = 1;
    3157             :       }
    3158             :     }
    3159     1000430 :     if (non0) gel(B, k++) = Ai;
    3160             :   }
    3161       93063 :   setlg(B, k); return B;
    3162             : }
    3163             : 
    3164             : static long
    3165      577156 : compute_multiple_of_R_pivot(GEN X, GEN x0/*unused*/, long ix, GEN c)
    3166             : {
    3167      577156 :   GEN x = gel(X,ix);
    3168      577156 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    3169             :   (void)x0;
    3170     2841729 :   for (i=1; i<lx; i++)
    3171     2264573 :     if (!c[i] && !gequal0(gel(x,i)))
    3172             :     {
    3173      730486 :       long e = gexpo(gel(x,i));
    3174      730486 :       if (e > ex) { ex = e; k = i; }
    3175             :     }
    3176      577156 :   return (k && ex > -32)? k: lx;
    3177             : }
    3178             : 
    3179             : /* Ar = (log |sigma_i(u_j)|) for units (u_j) found so far;
    3180             :  * RU = R1+R2 = target rank for unit matrix, after adding [1 x r1, 2 x r2];
    3181             :  * N = field degree, need = unit rank defect;
    3182             :  * L = NULL (prec problem) or B^(-1) * A with approximate rational entries
    3183             :  * (as t_REAL), B a submatrix of A, with (probably) maximal rank RU */
    3184             : static GEN
    3185      121720 : compute_multiple_of_R(GEN Ar, long RU, long N, long *pneed, long *bit, GEN *ptL)
    3186             : {
    3187             :   GEN T, d, mdet, Im_mdet, kR, L;
    3188      121720 :   long i, j, r, R1 = 2*RU - N;
    3189             :   int precpb;
    3190      121720 :   pari_sp av = avma;
    3191             : 
    3192      121720 :   if (RU == 1) { *ptL = zeromat(0, lg(Ar)-1); return gen_1; }
    3193             : 
    3194       93063 :   if (DEBUGLEVEL) err_printf("\n#### Computing regulator multiple\n");
    3195       93063 :   mdet = clean_cols(Ar, &precpb);
    3196             :   /* will cause precision to increase on later failure, but we may succeed! */
    3197       93063 :   *ptL = precpb? NULL: gen_1;
    3198       93063 :   T = cgetg(RU+1,t_COL);
    3199      235330 :   for (i=1; i<=R1; i++) gel(T,i) = gen_1;
    3200      193227 :   for (   ; i<=RU; i++) gel(T,i) = gen_2;
    3201       93063 :   mdet = shallowconcat(T, mdet); /* det(Span(mdet)) = N * R */
    3202             : 
    3203             :   /* could be using indexrank(), but need custom "get_pivot" function */
    3204       93063 :   d = RgM_pivots(mdet, &r, &compute_multiple_of_R_pivot, NULL);
    3205             :   /* # of independent columns = target rank ? */
    3206       93063 :   if (lg(mdet)-1 - r != RU)
    3207             :   {
    3208       25380 :     if (DEBUGLEVEL)
    3209           0 :       err_printf("Units matrix target rank = %ld < %ld\n",lg(mdet)-1 - r, RU);
    3210       25380 :     *pneed = RU - (lg(mdet)-1-r); return gc_NULL(av);
    3211             :   }
    3212             : 
    3213       67683 :   Im_mdet = cgetg(RU+1, t_MAT); /* extract independent columns */
    3214             :   /* N.B: d[1] = 1, corresponding to T above */
    3215       67683 :   gel(Im_mdet, 1) = T;
    3216      258111 :   for (i = j = 2; i <= RU; j++)
    3217      190428 :     if (d[j]) gel(Im_mdet, i++) = gel(mdet,j);
    3218             : 
    3219             :   /* integral multiple of R: the cols we picked form a Q-basis, they have an
    3220             :    * index in the full lattice. First column is T */
    3221       67683 :   kR = divru(det2(Im_mdet), N);
    3222             :   /* R > 0.2 uniformly */
    3223       67682 :   if (!signe(kR) || expo(kR) < -3)
    3224             :   {
    3225           0 :     if (DEBUGLEVEL) err_printf("Regulator is zero.\n");
    3226           0 :     *pneed = 0; return gc_NULL(av);
    3227             :   }
    3228       67682 :   d = det2(rowslice(vecslice(Im_mdet, 2, RU), 2, RU));
    3229       67683 :   setabssign(d); setabssign(kR);
    3230       67683 :   if (gexpo(gsub(d,kR)) - gexpo(d) > -20) { *ptL = NULL; return gc_NULL(av); }
    3231       67682 :   L = RgM_inv(Im_mdet);
    3232             :   /* estimate # of correct bits in result */
    3233       67682 :   if (!L || (*bit = -gexpo(RgM_Rg_sub_shallow(RgM_mul(L,Im_mdet), gen_1))) < 16)
    3234          10 :   { *ptL = NULL; return gc_NULL(av); }
    3235             : 
    3236       67672 :   *ptL = RgM_mul(rowslice(L,2,RU), Ar); /* approximate rational entries */
    3237       67672 :   return gc_all(av,2, &kR, ptL);
    3238             : }
    3239             : 
    3240             : /* leave small integer n as is, convert huge n to t_REAL (for readability) */
    3241             : static GEN
    3242           0 : i2print(GEN n)
    3243           0 : { return lgefint(n) <= DEFAULTPREC? n: itor(n,LOWDEFAULTPREC); }
    3244             : 
    3245             : static long
    3246       96180 : bad_check(GEN c)
    3247             : {
    3248       96180 :   long ec = gexpo(c);
    3249       96180 :   if (DEBUGLEVEL) err_printf("\n ***** check = %.28Pg\n",c);
    3250             :   /* safe check for c < 0.75 : avoid underflow in gtodouble() */
    3251       96181 :   if (ec < -1 || (ec == -1 && gtodouble(c) < 0.75)) return fupb_PRECI;
    3252             :   /* safe check for c > 1.3 : avoid overflow */
    3253       96181 :   if (ec > 0 || (ec == 0 && gtodouble(c) > 1.3)) return fupb_RELAT;
    3254       64027 :   return fupb_NONE;
    3255             : }
    3256             : /* Input:
    3257             :  * lambda = approximate rational entries: coords of units found so far on a
    3258             :  * sublattice of maximal rank (sublambda)
    3259             :  * *ptkR = regulator of sublambda = multiple of regulator of lambda
    3260             :  * Compute R = true regulator of lambda.
    3261             :  *
    3262             :  * If c := Rz ~ 1, by Dirichlet's formula, then lambda is the full group of
    3263             :  * units AND the full set of relations for the class group has been computed.
    3264             :  * In fact z is a very rough approximation and we only expect 0.75 < Rz < 1.3
    3265             :  *
    3266             :  * Output: *ptkR = R, *ptL = numerator(units) (in terms of lambda) */
    3267             : static long
    3268       96244 : compute_R(GEN lambda, GEN z, GEN *ptL, GEN *ptkR)
    3269             : {
    3270       96244 :   pari_sp av = avma;
    3271       96244 :   long bit, r, reason, RU = lg(lambda) == 1? 1: lgcols(lambda);
    3272             :   GEN L, H, D, den, R, c;
    3273             : 
    3274       96244 :   *ptL = NULL;
    3275       96244 :   if (RU == 1) { *ptkR = gen_1; *ptL = lambda; return bad_check(z); }
    3276       67588 :   D = gmul2n(mpmul(*ptkR,z), 1); /* bound for denom(lambda) */
    3277       67586 :   if (expo(D) < 0 && rtodbl(D) < 0.95) return fupb_PRECI;
    3278       67586 :   L = bestappr(lambda,D);
    3279       67589 :   if (lg(L) == 1)
    3280             :   {
    3281           0 :     if (DEBUGLEVEL) err_printf("truncation error in bestappr\n");
    3282           0 :     return fupb_PRECI;
    3283             :   }
    3284       67589 :   den = Q_denom(L);
    3285       67589 :   if (mpcmp(den,D) > 0)
    3286             :   {
    3287          15 :     if (DEBUGLEVEL) err_printf("D = %Ps\nden = %Ps\n",D, i2print(den));
    3288          15 :     return fupb_PRECI;
    3289             :   }
    3290       67573 :   bit = -gexpo(gsub(L, lambda)); /* input accuracy */
    3291       67574 :   L = Q_muli_to_int(L, den);
    3292       67574 :   if (gexpo(L) + expi(den) > bit - 32)
    3293             :   {
    3294          47 :     if (DEBUGLEVEL) err_printf("dubious bestappr; den = %Ps\n", i2print(den));
    3295          47 :     return fupb_PRECI;
    3296             :   }
    3297       67526 :   H = ZM_hnf(L); r = lg(H)-1;
    3298       67525 :   if (!r || r != nbrows(H))
    3299           0 :     R = gen_0; /* wrong rank */
    3300             :   else
    3301       67525 :     R = gmul(*ptkR, gdiv(ZM_det_triangular(H), powiu(den, r)));
    3302             :   /* R = tentative regulator; regulator > 0.2 uniformly */
    3303       67524 :   if (gexpo(R) < -3) {
    3304           0 :     if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3305           0 :     return gc_long(av, fupb_PRECI);
    3306             :   }
    3307       67524 :   c = gmul(R,z); /* should be n (= 1 if we are done) */
    3308       67525 :   if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3309       67525 :   if ((reason = bad_check(c))) return gc_long(av, reason);
    3310       48802 :   *ptkR = R; *ptL = L; return fupb_NONE;
    3311             : }
    3312             : static GEN
    3313       64118 : get_clg2(GEN cyc, GEN Ga, GEN C, GEN Ur, GEN Ge, GEN M1, GEN M2)
    3314             : {
    3315       64118 :   GEN GD = gsub(act_arch(M1, C), diagact_arch(cyc, Ga));
    3316       64117 :   GEN ga = gsub(act_arch(M2, C), act_arch(Ur, Ga));
    3317       64118 :   return mkvecn(6, Ur, ga, GD, Ge, M1, M2);
    3318             : }
    3319             : /* compute class group (clg1) + data for isprincipal (clg2) */
    3320             : static GEN
    3321       64022 : class_group_gen(GEN nf,GEN W,GEN C,GEN Vbase,long prec, GEN *pclg2)
    3322             : {
    3323             :   GEN M1, M2, z, G, Ga, Ge, cyc, X, Y, D, U, V, Ur, Ui, Uir;
    3324             :   long j, l;
    3325             : 
    3326       64022 :   D = ZM_snfall(W,&U,&V); /* UWV=D, D diagonal, G = g Ui (G=new gens, g=old) */
    3327       64022 :   Ui = ZM_inv(U, NULL);
    3328       64022 :   l = lg(D); cyc = cgetg(l, t_VEC); /* elementary divisors */
    3329       92799 :   for (j = 1; j < l; j++)
    3330             :   {
    3331       30358 :     gel(cyc,j) = gcoeff(D,j,j); /* strip useless components */
    3332       30358 :     if (is_pm1(gel(cyc,j))) break;
    3333             :   }
    3334       64022 :   l = j;
    3335       64022 :   Ur  = ZM_hnfdivrem(U, D, &Y);
    3336       64022 :   Uir = ZM_hnfdivrem(Ui,W, &X);
    3337             :  /* {x} = logarithmic embedding of x (arch. component)
    3338             :   * NB: [J,z] = idealred(I) --> I = y J, with {y} = - z
    3339             :   * G = g Uir - {Ga},  Uir = Ui - WX
    3340             :   * g = G Ur  - {ga},  Ur  = U - DY */
    3341       64022 :   G = cgetg(l,t_VEC);
    3342       64022 :   Ga= cgetg(l,t_MAT);
    3343       64022 :   Ge= cgetg(l,t_COL);
    3344       64022 :   z = init_famat(NULL);
    3345       92796 :   for (j = 1; j < l; j++)
    3346             :   {
    3347       28776 :     GEN I = genback(z, nf, Vbase, gel(Uir,j));
    3348       28777 :     gel(G,j) = gel(I,1); /* generator, order cyc[j] */
    3349       28777 :     gel(Ge,j)= gel(I,2);
    3350       28777 :     gel(Ga,j)= nf_cxlog(nf, gel(I,2), prec);
    3351       28774 :     if (!gel(Ga,j)) pari_err_PREC("class_group_gen");
    3352             :   }
    3353             :   /* {ga} = - {GD}Y + G U - g = - {GD}Y - {Ga} U - gW X U
    3354             :                             = - gW (X Ur + V Y) - {Ga}Ur */
    3355       64020 :   M2 = ZM_neg(ZM_add(ZM_mul(X,Ur), ZM_mul(V,Y)));
    3356       64022 :   setlg(cyc,l); setlg(V,l); setlg(D,l);
    3357             :   /* G D =: {GD} = g (Ui - W X) D - {Ga}D = g W (V - X D) - {Ga}D
    3358             :    * NB: Ui D = W V. gW is given by (first l-1 cols of) C */
    3359       64022 :   M1 = ZM_sub(V, ZM_mul(X,D));
    3360       64021 :   *pclg2 = get_clg2(cyc, Ga, C, Ur, Ge, M1, M2);
    3361       64020 :   return mkvec3(ZV_prod(cyc), cyc, G);
    3362             : }
    3363             : 
    3364             : /* compute principal ideals corresponding to (gen[i]^cyc[i]) */
    3365             : static GEN
    3366        4956 : makecycgen(GEN bnf)
    3367             : {
    3368        4956 :   GEN cyc = bnf_get_cyc(bnf), gen = bnf_get_gen(bnf), nf = bnf_get_nf(bnf);
    3369        4956 :   GEN h, y, GD = bnf_get_GD(bnf), W = bnf_get_W(bnf); /* HNF */
    3370        4956 :   GEN SUnits = bnf_get_sunits(bnf);
    3371        4956 :   GEN X = SUnits? gel(SUnits,1): NULL, C = SUnits? gel(SUnits,3): NULL;
    3372             :   long e, i, l;
    3373             : 
    3374        4956 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building cycgen)");
    3375        4956 :   h = cgetg_copy(gen, &l);
    3376       11613 :   for (i = 1; i < l; i++)
    3377             :   {
    3378        6657 :     GEN gi = gel(gen,i), ci = gel(cyc,i);
    3379        6657 :     if (X && equalii(ci, gcoeff(W,i,i)))
    3380             :     {
    3381             :       long j;
    3382        8589 :       for (j = i+1; j < l; j++)
    3383        3213 :         if (signe(gcoeff(W,i,j))) break;
    3384        5550 :       if (j == i) { gel(h,i) = mkmat2(X, gel(C,i)); continue; }
    3385             :     }
    3386        6657 :     if (abscmpiu(ci, 5) < 0)
    3387             :     {
    3388        5544 :       GEN N = ZM_det_triangular(gi);
    3389        5544 :       y = isprincipalarch(bnf,gel(GD,i), N, ci, gen_1, &e);
    3390        5544 :       if (y && fact_ok(nf,y,NULL,mkvec(gi),mkvec(ci)))
    3391             :       {
    3392        4556 :         gel(h,i) = to_famat_shallow(y,gen_1);
    3393        4556 :         continue;
    3394             :       }
    3395             :     }
    3396        2101 :     y = isprincipalfact(bnf, NULL, mkvec(gi), mkvec(ci), nf_GENMAT|nf_FORCE);
    3397        2101 :     gel(h,i) = gel(y,2);
    3398             :   }
    3399        4956 :   return h;
    3400             : }
    3401             : 
    3402             : static GEN
    3403          69 : get_y(GEN bnf, GEN W, GEN B, GEN C, GEN pFB, long j)
    3404             : {
    3405          69 :   GEN y, nf  = bnf_get_nf(bnf);
    3406          69 :   long e, lW = lg(W)-1;
    3407          69 :   GEN ex = (j<=lW)? gel(W,j): gel(B,j-lW);
    3408          69 :   GEN P = (j<=lW)? NULL: gel(pFB,j);
    3409          69 :   if (C)
    3410             :   { /* archimedean embeddings known: cheap trial */
    3411          69 :     GEN Nx = get_norm_fact_primes(pFB, ex, P);
    3412          69 :     y = isprincipalarch(bnf,gel(C,j), Nx,gen_1, gen_1, &e);
    3413          69 :     if (y && fact_ok(nf,y,P,pFB,ex)) return y;
    3414             :   }
    3415           0 :   y = isprincipalfact_or_fail(bnf, P, pFB, ex);
    3416           0 :   return typ(y) == t_INT? y: gel(y,2);
    3417             : }
    3418             : /* compute principal ideals corresponding to bnf relations */
    3419             : static GEN
    3420          20 : makematal(GEN bnf)
    3421             : {
    3422          20 :   GEN W = bnf_get_W(bnf), B = bnf_get_B(bnf), C = bnf_get_C(bnf);
    3423             :   GEN pFB, ma, retry;
    3424          20 :   long lma, j, prec = 0;
    3425             : 
    3426          20 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building matal)");
    3427          20 :   lma=lg(W)+lg(B)-1;
    3428          20 :   pFB = bnf_get_vbase(bnf);
    3429          20 :   ma = cgetg(lma,t_VEC);
    3430          20 :   retry = vecsmalltrunc_init(lma);
    3431          89 :   for (j=lma-1; j>0; j--)
    3432             :   {
    3433          69 :     pari_sp av = avma;
    3434          69 :     GEN y = get_y(bnf, W, B, C, pFB, j);
    3435          69 :     if (typ(y) == t_INT)
    3436             :     {
    3437           0 :       long E = itos(y);
    3438           0 :       if (DEBUGLEVEL>1) err_printf("\n%ld done later at prec %ld\n",j,E);
    3439           0 :       set_avma(av);
    3440           0 :       vecsmalltrunc_append(retry, j);
    3441           0 :       if (E > prec) prec = E;
    3442             :     }
    3443             :     else
    3444             :     {
    3445          69 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3446          69 :       gel(ma,j) = gc_upto(av,y);
    3447             :     }
    3448             :   }
    3449          20 :   if (prec)
    3450             :   {
    3451           0 :     long k, l = lg(retry);
    3452           0 :     GEN y, nf = bnf_get_nf(bnf);
    3453           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"makematal",prec);
    3454           0 :     nf = nfnewprec_shallow(nf,prec);
    3455           0 :     bnf = Buchall(nf, nf_FORCE, prec);
    3456           0 :     if (DEBUGLEVEL) err_printf("makematal, adding missing entries:");
    3457           0 :     for (k=1; k<l; k++)
    3458             :     {
    3459           0 :       pari_sp av = avma;
    3460           0 :       long j = retry[k];
    3461           0 :       y = get_y(bnf,W,B,NULL, pFB, j);
    3462           0 :       if (typ(y) == t_INT) pari_err_PREC("makematal");
    3463           0 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3464           0 :       gel(ma,j) = gc_upto(av,y);
    3465             :     }
    3466             :   }
    3467          20 :   if (DEBUGLEVEL>1) err_printf("\n");
    3468          20 :   return ma;
    3469             : }
    3470             : 
    3471             : enum { MATAL = 1, CYCGEN, UNITS };
    3472             : GEN
    3473       26726 : bnf_build_cycgen(GEN bnf)
    3474       26726 : { return obj_checkbuild(bnf, CYCGEN, &makecycgen); }
    3475             : GEN
    3476          20 : bnf_build_matalpha(GEN bnf)
    3477          20 : { return obj_checkbuild(bnf, MATAL, &makematal); }
    3478             : GEN
    3479       50738 : bnf_build_units(GEN bnf)
    3480       50738 : { return obj_checkbuild(bnf, UNITS, &makeunits); }
    3481             : 
    3482             : /* return fu in compact form if available; in terms of a fixed basis
    3483             :  * of S-units */
    3484             : GEN
    3485          70 : bnf_compactfu_mat(GEN bnf)
    3486             : {
    3487          70 :   GEN X, U, SUnits = bnf_get_sunits(bnf);
    3488          70 :   if (!SUnits) return NULL;
    3489          70 :   X = gel(SUnits,1);
    3490          70 :   U = gel(SUnits,2); ZM_remove_unused(&U, &X);
    3491          70 :   return mkvec2(X, U);
    3492             : }
    3493             : /* return fu in compact form if available; individually as famat */
    3494             : GEN
    3495       37421 : bnf_compactfu(GEN bnf)
    3496             : {
    3497       37421 :   GEN fu, X, U, SUnits = bnf_get_sunits(bnf);
    3498             :   long i, l;
    3499       37421 :   if (!SUnits) return NULL;
    3500       37183 :   X = gel(SUnits,1);
    3501       37183 :   U = gel(SUnits,2); l = lg(U); fu = cgetg(l, t_VEC);
    3502       61214 :   for (i = 1; i < l; i++)
    3503       24030 :     gel(fu,i) = famat_remove_trivial(mkmat2(X, gel(U,i)));
    3504       37184 :   return fu;
    3505             : }
    3506             : /* return expanded fu if available */
    3507             : GEN
    3508      285759 : bnf_has_fu(GEN bnf)
    3509             : {
    3510      285759 :   GEN fu = obj_check(bnf, UNITS);
    3511      285753 :   if (fu) return vecsplice(fu, 1);
    3512      264098 :   fu = bnf_get_fu_nocheck(bnf);
    3513      264098 :   return (typ(fu) == t_MAT)? NULL: fu;
    3514             : }
    3515             : /* return expanded fu if available; build if cheap */
    3516             : GEN
    3517      285474 : bnf_build_cheapfu(GEN bnf)
    3518             : {
    3519             :   GEN fu, SUnits;
    3520      285474 :   if ((fu = bnf_has_fu(bnf))) return fu;
    3521         142 :   if ((SUnits = bnf_get_sunits(bnf)))
    3522             :   {
    3523         142 :     pari_sp av = avma;
    3524         142 :     long e = gexpo(real_i(bnf_get_logfu(bnf)));
    3525         142 :     set_avma(av); if (e < 13) return vecsplice(bnf_build_units(bnf), 1);
    3526             :   }
    3527          77 :   return NULL;
    3528             : }
    3529             : 
    3530             : static GEN
    3531       48894 : get_regulator(GEN A)
    3532             : {
    3533       48894 :   pari_sp av = avma;
    3534             :   GEN R;
    3535             : 
    3536       48894 :   if (lg(A) == 1) return gen_1;
    3537       48887 :   R = det( rowslice(real_i(A), 1, lgcols(A)-2) );
    3538       48887 :   setabssign(R); return gc_leaf(av, R);
    3539             : }
    3540             : 
    3541             : /* return corrected archimedian components for elts of x (vector)
    3542             :  * (= log(sigma_i(x)) - log(|Nx|) / [K:Q]) */
    3543             : static GEN
    3544          40 : get_archclean(GEN nf, GEN x, long prec, int units)
    3545             : {
    3546          40 :   long k, N, l = lg(x);
    3547          40 :   GEN M = cgetg(l, t_MAT);
    3548             : 
    3549          40 :   if (l == 1) return M;
    3550          26 :   N = nf_get_degree(nf);
    3551         114 :   for (k = 1; k < l; k++)
    3552             :   {
    3553          88 :     pari_sp av = avma;
    3554          88 :     GEN c = nf_cxlog(nf, gel(x,k), prec);
    3555          88 :     if (!c || (!units && !(c = cleanarch(c, N, NULL,prec)))) return NULL;
    3556          88 :     gel(M,k) = gc_GEN(av, c);
    3557             :   }
    3558          26 :   return M;
    3559             : }
    3560             : static void
    3561          77 : SUnits_archclean(GEN nf, GEN SUnits, GEN *pmun, GEN *pC, long prec)
    3562             : {
    3563          77 :   GEN ipi, M, X = gel(SUnits,1), U = gel(SUnits,2), G = gel(SUnits,3);
    3564          77 :   long k, N = nf_get_degree(nf), l = lg(X);
    3565             : 
    3566          77 :   M = cgetg(l, t_MAT);
    3567        3640 :   for (k = 1; k < l; k++)
    3568        3563 :     if (!(gel(M,k) = nf_cxlog(nf, gel(X,k), prec))) return;
    3569          77 :   ipi = invr(mppi(prec));
    3570          77 :   *pmun = cleanarch(RgM_ZM_mul(M, U), N, ipi, prec); /* not cleanarchunit ! */
    3571          77 :   if (*pmun) *pC = cleanarch(RgM_ZM_mul(M, G), N, ipi, prec);
    3572             : }
    3573             : 
    3574             : GEN
    3575          97 : bnfnewprec_shallow(GEN bnf, long prec)
    3576             : {
    3577          97 :   GEN nf0 = bnf_get_nf(bnf), nf, v, fu, matal, y, A, C;
    3578          97 :   GEN SUnits = bnf_get_sunits(bnf), Ur, Ga, Ge, M1, M2;
    3579          97 :   long r1, r2, prec0 = prec;
    3580             : 
    3581          97 :   nf_get_sign(nf0, &r1, &r2);
    3582          97 :   if (SUnits)
    3583             :   {
    3584          77 :     fu = matal = NULL;
    3585          77 :     prec += nbits2extraprec(gexpo(SUnits));
    3586             :   }
    3587             :   else
    3588             :   {
    3589          20 :     fu = bnf_build_units(bnf);
    3590          20 :     fu = vecslice(fu, 2, lg(fu)-1);
    3591          20 :     if (r1 + r2 > 1) {
    3592          13 :       long e = gexpo(bnf_get_logfu(bnf)) + 1 - TWOPOTBITS_IN_LONG;
    3593          13 :       if (e >= 0) prec += nbits2extraprec(e);
    3594             :     }
    3595          20 :     matal = bnf_build_matalpha(bnf);
    3596             :   }
    3597             : 
    3598          97 :   if (DEBUGLEVEL && prec0 != prec) pari_warn(warnprec,"bnfnewprec",prec);
    3599          97 :   for(C = NULL;;)
    3600           0 :   {
    3601          97 :     pari_sp av = avma;
    3602          97 :     nf = nfnewprec_shallow(nf0,prec);
    3603          97 :     if (SUnits)
    3604          77 :       SUnits_archclean(nf, SUnits, &A, &C, prec);
    3605             :     else
    3606             :     {
    3607          20 :       A = get_archclean(nf, fu, prec, 1);
    3608          20 :       if (A) C = get_archclean(nf, matal, prec, 0);
    3609             :     }
    3610          97 :     if (C) break;
    3611           0 :     set_avma(av); prec = precdbl(prec);
    3612           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"bnfnewprec(extra)",prec);
    3613             :   }
    3614          97 :   y = leafcopy(bnf);
    3615          97 :   gel(y,3) = A;
    3616          97 :   gel(y,4) = C;
    3617          97 :   gel(y,7) = nf;
    3618          97 :   gel(y,8) = v = leafcopy(gel(bnf,8));
    3619          97 :   gel(v,2) = get_regulator(A);
    3620          97 :   v = gel(bnf,9);
    3621          97 :   if (lg(v) < 7) pari_err_TYPE("bnfnewprec [obsolete bnf format]", bnf);
    3622          97 :   Ur = gel(v,1);
    3623          97 :   Ge = gel(v,4);
    3624          97 :   Ga = nfV_cxlog(nf, Ge, prec);
    3625          97 :   M1 = gel(v,5);
    3626          97 :   M2 = gel(v,6);
    3627          97 :   gel(y,9) = get_clg2(bnf_get_cyc(bnf), Ga, C, Ur, Ge, M1, M2);
    3628          97 :   return y;
    3629             : }
    3630             : GEN
    3631           7 : bnfnewprec(GEN bnf, long prec)
    3632             : {
    3633           7 :   pari_sp av = avma;
    3634           7 :   return gc_GEN(av, bnfnewprec_shallow(checkbnf(bnf), prec));
    3635             : }
    3636             : 
    3637             : GEN
    3638           0 : bnrnewprec_shallow(GEN bnr, long prec)
    3639             : {
    3640           0 :   GEN y = cgetg(7,t_VEC);
    3641             :   long i;
    3642           0 :   gel(y,1) = bnfnewprec_shallow(bnr_get_bnf(bnr), prec);
    3643           0 :   for (i=2; i<7; i++) gel(y,i) = gel(bnr,i);
    3644           0 :   return y;
    3645             : }
    3646             : GEN
    3647           7 : bnrnewprec(GEN bnr, long prec)
    3648             : {
    3649           7 :   GEN y = cgetg(7,t_VEC);
    3650             :   long i;
    3651           7 :   checkbnr(bnr);
    3652           7 :   gel(y,1) = bnfnewprec(bnr_get_bnf(bnr), prec);
    3653          42 :   for (i=2; i<7; i++) gel(y,i) = gcopy(gel(bnr,i));
    3654           7 :   return y;
    3655             : }
    3656             : 
    3657             : static GEN
    3658       65224 : buchall_end(GEN nf,GEN res, GEN clg2, GEN W, GEN B, GEN A, GEN C,GEN Vbase)
    3659             : {
    3660       65224 :   GEN z = obj_init(9, 3);
    3661       65224 :   gel(z,1) = W;
    3662       65224 :   gel(z,2) = B;
    3663       65224 :   gel(z,3) = A;
    3664       65224 :   gel(z,4) = C;
    3665       65224 :   gel(z,5) = Vbase;
    3666       65224 :   gel(z,6) = gen_0;
    3667       65224 :   gel(z,7) = nf;
    3668       65224 :   gel(z,8) = res;
    3669       65224 :   gel(z,9) = clg2;
    3670       65224 :   return z;
    3671             : }
    3672             : 
    3673             : GEN
    3674        2632 : bnfinit0(GEN P, long flag, GEN data, long prec)
    3675             : {
    3676        2632 :   double c1 = 0., c2 = 0.;
    3677        2632 :   long fl, relpid = BNF_RELPID, max_fact = MAXTRY_FACT, idex = 0, nbthr = 1, s;
    3678             : 
    3679        2632 :   if (data)
    3680             :   {
    3681          21 :     long lx = lg(data);
    3682          21 :     if (typ(data) != t_VEC || lx > 7) pari_err_TYPE("bnfinit",data);
    3683          21 :     switch(lx)
    3684             :     {
    3685           0 :       case 7: nbthr = itou(gel(data,6)); if (nbthr <= 1) nbthr = 1-nbthr;
    3686           0 :       case 6: idex = itou(gel(data,5));
    3687           0 :       case 5: s = itou(gel(data,4)); if (s) max_fact = s;
    3688           0 :       case 4: s = itos(gel(data,3)); if (s) relpid = s < 0 ? 0 : s;
    3689          14 :       case 3: c2 = gtodouble(gel(data,2));
    3690          21 :       case 2: c1 = gtodouble(gel(data,1));
    3691             :     }
    3692             :   }
    3693        2632 :   switch(flag)
    3694             :   {
    3695        1778 :     case 2:
    3696        1778 :     case 0: fl = 0; break;
    3697         854 :     case 1: fl = nf_FORCE; break;
    3698           0 :     default: pari_err_FLAG("bnfinit");
    3699             :       return NULL; /* LCOV_EXCL_LINE */
    3700             :   }
    3701        2632 :   return Buchall_param(P, c1, c2, relpid, max_fact, idex, nbthr, fl, prec);
    3702             : }
    3703             : GEN
    3704       62600 : Buchall(GEN P, long flag, long prec)
    3705       62600 : { return Buchall_param(P, 0., 0., BNF_RELPID, MAXTRY_FACT, 0, 1, flag & nf_FORCE, prec); }
    3706             : 
    3707             : static GEN
    3708        1204 : Buchall_deg1(GEN nf)
    3709             : {
    3710        1204 :   GEN v = cgetg(1,t_VEC), m = cgetg(1,t_MAT);
    3711        1204 :   GEN res, W, A, B, C, Vbase = cgetg(1,t_COL);
    3712        1204 :   GEN fu = v, R = gen_1, zu = mkvec2(gen_2, gen_m1);
    3713        1204 :   GEN clg1 = mkvec3(gen_1,v,v), clg2 = mkvecn(6, m,m,m,v,m,m);
    3714             : 
    3715        1204 :   W = A = B = C = m; res = mkvec5(clg1, R, gen_1, zu, fu);
    3716        1204 :   return buchall_end(nf,res,clg2,W,B,A,C,Vbase);
    3717             : }
    3718             : 
    3719             : /* return (small set of) indices of columns generating the same lattice as x.
    3720             :  * Assume HNF(x) is inexpensive (few rows, many columns).
    3721             :  * Dichotomy approach since interesting columns may be at the very end */
    3722             : GEN
    3723       64027 : extract_full_lattice(GEN x)
    3724             : {
    3725       64027 :   long dj, j, k, l = lg(x);
    3726             :   GEN h, h2, H, v;
    3727             : 
    3728       64027 :   if (l < 200) return NULL; /* not worth it */
    3729             : 
    3730           1 :   v = vecsmalltrunc_init(l);
    3731           1 :   H = ZM_hnf(x);
    3732           1 :   h = cgetg(1, t_MAT);
    3733           1 :   dj = 1;
    3734          43 :   for (j = 1; j < l; )
    3735             :   {
    3736          43 :     pari_sp av = avma;
    3737          43 :     long lv = lg(v);
    3738             : 
    3739         145 :     for (k = 0; k < dj; k++) v[lv+k] = j+k;
    3740          43 :     setlg(v, lv + dj);
    3741          43 :     h2 = ZM_hnf(vecpermute(x, v));
    3742          43 :     if (ZM_equal(h, h2))
    3743             :     { /* these dj columns can be eliminated */
    3744          17 :       set_avma(av); setlg(v, lv);
    3745          17 :       j += dj;
    3746          17 :       if (j >= l) break;
    3747          17 :       dj <<= 1;
    3748          17 :       if (j + dj >= l) { dj = (l - j) >> 1; if (!dj) dj = 1; }
    3749             :     }
    3750          26 :     else if (dj > 1)
    3751             :     { /* at least one interesting column, try with first half of this set */
    3752          17 :       set_avma(av); setlg(v, lv);
    3753          17 :       dj >>= 1; /* > 0 */
    3754             :     }
    3755             :     else
    3756             :     { /* this column should be kept */
    3757           9 :       if (ZM_equal(h2, H)) break;
    3758           8 :       h = h2; j++;
    3759             :     }
    3760             :   }
    3761           1 :   return v;
    3762             : }
    3763             : 
    3764             : static void
    3765       64098 : init_rel(RELCACHE_t *cache, FB_t *F, long add_need)
    3766             : {
    3767       64098 :   const long n = F->KC + add_need; /* expected # of needed relations */
    3768             :   long i, j, k, p;
    3769             :   GEN c, P;
    3770             :   GEN R;
    3771             : 
    3772       64098 :   if (DEBUGLEVEL) err_printf("KCZ = %ld, KC = %ld, n = %ld\n", F->KCZ,F->KC,n);
    3773       64098 :   reallocate(cache, 10*n + 50); /* make room for lots of relations */
    3774       64099 :   cache->chk = cache->base;
    3775       64099 :   cache->end = cache->base + n;
    3776       64099 :   cache->relsup = add_need;
    3777       64099 :   cache->last = cache->base;
    3778       64099 :   cache->missing = lg(cache->basis) - 1;
    3779      306080 :   for (i = 1; i <= F->KCZ; i++)
    3780             :   { /* trivial relations (p) = prod P^e */
    3781      241981 :     p = F->FB[i]; P = gel(F->LV,p);
    3782      241981 :     if (!isclone(P)) continue;
    3783             : 
    3784             :     /* all prime divisors in FB */
    3785      168798 :     c = zero_Flv(F->KC); k = F->iLP[p];
    3786      168797 :     R = c; c += k;
    3787      538788 :     for (j = lg(P)-1; j; j--) c[j] = pr_get_e(gel(P,j));
    3788      168797 :     add_rel(cache, F, R, k+1, pr_get_p(gel(P,1)), 0);
    3789             :   }
    3790       64099 : }
    3791             : 
    3792             : /* Let z = \zeta_n in nf. List of not-obviously-dependent generators for
    3793             :  * cyclotomic units modulo torsion in Q(z) [independent when n a prime power]:
    3794             :  * - z^a - 1,  n/(a,n) not a prime power, a \nmid n unless a=1,  1 <= a < n/2
    3795             :  * - (Z^a - 1)/(Z - 1),  p^k || n, Z = z^{n/p^k}, (p,a) = 1, 1 < a <= (p^k-1)/2
    3796             :  */
    3797             : GEN
    3798       64099 : nfcyclotomicunits(GEN nf, GEN zu)
    3799             : {
    3800       64099 :   long n = itos(gel(zu, 1)), n2, lP, i, a;
    3801             :   GEN z, fa, P, E, L, mz, powz;
    3802       64099 :   if (n <= 6) return cgetg(1, t_VEC);
    3803             : 
    3804        1911 :   z = algtobasis(nf,gel(zu, 2));
    3805        1911 :   if ((n & 3) == 2) { n = n >> 1; z = ZC_neg(z); } /* ensure n != 2 (mod 4) */
    3806        1911 :   n2 = n/2;
    3807        1911 :   mz = zk_multable(nf, z); /* multiplication by z */
    3808        1911 :   powz = cgetg(n2, t_VEC); gel(powz,1) = z;
    3809        6286 :   for (i = 2; i < n2; i++) gel(powz,i) = ZM_ZC_mul(mz, gel(powz,i-1));
    3810             :   /* powz[i] = z^i */
    3811             : 
    3812        1911 :   L = vectrunc_init(n);
    3813        1911 :   fa = factoru(n);
    3814        1911 :   P = gel(fa,1); lP = lg(P);
    3815        1911 :   E = gel(fa,2);
    3816        4613 :   for (i = 1; i < lP; i++)
    3817             :   { /* second kind */
    3818        2702 :     long p = P[i], k = E[i], pk = upowuu(p,k), pk2 = (pk-1) / 2;
    3819        2702 :     GEN u = gen_1;
    3820        4970 :     for (a = 2; a <= pk2; a++)
    3821             :     {
    3822        2268 :       u = nfadd(nf, u, gel(powz, (n/pk) * (a-1))); /* = (Z^a-1)/(Z-1) */
    3823        2268 :       if (a % p) vectrunc_append(L, u);
    3824             :     }
    3825             :   }
    3826        6160 :   if (lP > 2) for (a = 1; a < n2; a++)
    3827             :   { /* first kind, when n not a prime power */
    3828             :     ulong p;
    3829        4249 :     if (a > 1 && (n % a == 0 || uisprimepower(n/ugcd(a,n), &p))) continue;
    3830        1869 :     vectrunc_append(L, nfadd(nf, gel(powz, a), gen_m1));
    3831             :   }
    3832        1911 :   return L;
    3833             : }
    3834             : static void
    3835       64099 : add_cyclotomic_units(GEN nf, GEN zu, RELCACHE_t *cache, FB_t *F)
    3836             : {
    3837       64099 :   pari_sp av = avma;
    3838       64099 :   GEN L = nfcyclotomicunits(nf, zu);
    3839       64099 :   long i, l = lg(L);
    3840       64099 :   if (l > 1)
    3841             :   {
    3842        1911 :     GEN R = zero_Flv(F->KC);
    3843        5950 :     for(i = 1; i < l; i++) add_rel(cache, F, R, F->KC+1, gel(L,i), 0);
    3844             :   }
    3845       64099 :   set_avma(av);
    3846       64099 : }
    3847             : 
    3848             : static GEN
    3849      122453 : trim_list(FB_t *F)
    3850             : {
    3851      122453 :   pari_sp av = avma;
    3852      122453 :   GEN v, L_jid = F->L_jid, minidx = F->minidx, present = zero_Flv(F->KC);
    3853      122453 :   long i, j, imax = minss(lg(L_jid), F->KC + 1);
    3854             : 
    3855      122453 :   v = cgetg(imax, t_VECSMALL);
    3856     1324839 :   for (i = j = 1; i < imax; i++)
    3857             :   {
    3858     1202386 :     long k = minidx[ L_jid[i] ];
    3859     1202386 :     if (!present[k]) { v[j++] = L_jid[i]; present[k] = 1; }
    3860             :   }
    3861      122453 :   setlg(v, j); return gc_leaf(av, v);
    3862             : }
    3863             : 
    3864             : /* x t_INT or primitive ZC */
    3865             : static void
    3866        1718 : try_elt(RELCACHE_t *cache, FB_t *F, GEN nf, GEN x, FACT *fact)
    3867             : {
    3868        1718 :   pari_sp av = avma;
    3869             :   long nz;
    3870             :   GEN R;
    3871             : 
    3872        1718 :   if (typ(x) == t_INT /* 2nd path can't fail */
    3873        1718 :      || !can_factor(F, nf, NULL, x, nfnorm(nf, x), fact)) return;
    3874             :   /* smooth element */
    3875        1487 :   R = set_fact(F, fact, NULL, &nz);
    3876             :   /* make sure we get maximal rank first, then allow all relations */
    3877        1487 :   (void)add_rel(cache, F, R, nz, x, 0);
    3878        1487 :   set_avma(av);
    3879             : }
    3880             : 
    3881             : static void
    3882       55465 : matenlarge(GEN C, long h)
    3883             : {
    3884       55465 :   GEN _0 = zerocol(h);
    3885             :   long i;
    3886     1258229 :   for (i = lg(C); --i; ) gel(C,i) = shallowconcat(gel(C,i), _0);
    3887       55465 : }
    3888             : 
    3889             : /* E = floating point embeddings */
    3890             : static GEN
    3891       55465 : matbotidembs(RELCACHE_t *cache, GEN E)
    3892             : {
    3893       55465 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3894       55465 :   long j, d = h - w, hE = nbrows(E);
    3895       55465 :   GEN y = cgetg(w+1,t_MAT), _0 = zerocol(h);
    3896      217089 :   for (j = 1; j <= w; j++)
    3897             :   {
    3898      161624 :     GEN c = shallowconcat(gel(E,j), _0);
    3899      161624 :     if (d + j >= 1) gel(c, d + j + hE) = gen_1;
    3900      161624 :     gel(y,j) = c;
    3901             :   }
    3902       55465 :   return y;
    3903             : }
    3904             : static GEN
    3905       62496 : matbotid(RELCACHE_t *cache)
    3906             : {
    3907       62496 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3908       62496 :   long j, d = h - w;
    3909       62496 :   GEN y = cgetg(w+1,t_MAT);
    3910      851201 :   for (j = 1; j <= w; j++)
    3911             :   {
    3912      788706 :     GEN c = zerocol(h);
    3913      788705 :     if (d + j >= 1) gel(c, d + j) = gen_1;
    3914      788705 :     gel(y,j) = c;
    3915             :   }
    3916       62495 :   return y;
    3917             : }
    3918             : 
    3919             : static long
    3920          73 : myprecdbl(long prec, GEN C)
    3921             : {
    3922          73 :   long p = prec < 1280? precdbl(prec): (long)(prec * 1.5);
    3923          73 :   if (C) p = maxss(p, minss(3*p, prec + nbits2extraprec(gexpo(C))));
    3924          73 :   return p;
    3925             : }
    3926             : 
    3927             : static GEN
    3928       57729 : _nfnewprec(GEN nf, long prec, long *isclone)
    3929             : {
    3930       57729 :   GEN NF = gclone(nfnewprec_shallow(nf, prec));
    3931       57729 :   if (*isclone) gunclone(nf);
    3932       57729 :   *isclone = 1; return NF;
    3933             : }
    3934             : 
    3935             : /* In small_norm, LLL reduction produces v0 in I such that
    3936             : *     T2(v0) <= (4/3)^((n-1)/2) (NI sqrt(disc(K)))^(2/n)
    3937             : * NI <= LIMCMAX^2. We consider v with T2(v) ~ T2(v0), hence
    3938             : *     Nv <= ((4/3)^((n-1)/2) / n)^(n/2) LIMCMAX^2 sqrt(disc(K)) */
    3939             : static long
    3940       64019 : small_norm_prec(long N, double LOGD, long LIMCMAX)
    3941             : {
    3942       64019 :   double a = N/2. * ((N-1)/2.*log(4./3) - log((double)N));
    3943       64019 :   double b = 2*log((double)LIMCMAX) + LOGD/2;
    3944       64019 :   return nbits2prec(BITS_IN_LONG + (a + b) / M_LN2);
    3945             : }
    3946             : 
    3947             : /* Nrelid = nb relations per ideal, possibly 0. If flag is set, keep data in
    3948             :  * algebraic form. */
    3949             : GEN
    3950       65231 : Buchall_param(GEN P, double cbach, double cbach2, long Nrelid, long max_fact, long idex, long nbthr, long flag, long prec)
    3951             : {
    3952             :   pari_timer T;
    3953       65231 :   pari_sp av0 = avma, av, av2;
    3954             :   long PREC, N, R1, R2, RU, low, high, LIMC0, LIMC, LIMC2, LIMCMAX, zc, i;
    3955       65231 :   long LIMres, bit = 0, flag_nfinit = 0, nfisclone = 0;
    3956       65231 :   long nreldep, sfb_trials, need, old_need, precdouble = 0, TRIES = 0;
    3957             :   long done_small, small_fail, fail_limit, squash_index;
    3958             :   double LOGD, LOGD2, lim;
    3959       65231 :   GEN computed = NULL, fu = NULL, zu, nf, D, A, W, R, h, Ce, PERM;
    3960             :   GEN small_multiplier, auts, cyclic, embs, SUnits;
    3961             :   GEN res, L, invhr, B, C, lambda, dep, clg1, clg2, Vbase;
    3962       65231 :   const char *precpb = NULL;
    3963       65231 :   REL_t *old_cache = NULL;
    3964             :   nfmaxord_t nfT;
    3965             :   RELCACHE_t cache;
    3966             :   FB_t F;
    3967             :   GRHcheck_t GRHcheck;
    3968             :   FACT *fact;
    3969             : 
    3970       65231 :   if (DEBUGLEVEL) timer_start(&T);
    3971       65231 :   P = get_nfpol(P, &nf);
    3972       65217 :   if (degpol(P)==2) Nrelid = 0;
    3973       65217 :   if (nf)
    3974        3885 :     D = nf_get_disc(nf);
    3975             :   else
    3976             :   {
    3977       61332 :     nfinit_basic(&nfT, P);
    3978       61338 :     D = nfT.dK;
    3979       61338 :     if (!ZX_is_monic(nfT.T0))
    3980             :     {
    3981          14 :       pari_warn(warner,"nonmonic polynomial in bnfinit, using polredbest");
    3982          14 :       flag_nfinit = nf_RED;
    3983             :     }
    3984             :   }
    3985       65223 :   PREC = maxss(DEFAULTPREC, prec);
    3986       65223 :   N = degpol(P);
    3987       65223 :   if (N <= 1)
    3988             :   {
    3989        1204 :     if (!nf) nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3990        1204 :     return gc_GEN(av0, Buchall_deg1(nf));
    3991             :   }
    3992       64019 :   D = absi_shallow(D);
    3993       64020 :   LOGD = dbllog2(D) * M_LN2;
    3994       64020 :   LOGD2 = LOGD*LOGD;
    3995       64020 :   LIMCMAX = (long)(4.*LOGD2);
    3996       64020 :   if (nf) PREC = maxss(PREC, nf_get_prec(nf));
    3997       64020 :   PREC = maxss(PREC, nbits2prec((long)(LOGD2 * 0.02) + N*N));
    3998             : 
    3999       64019 :   if (nf) PREC = maxss(PREC, nf_get_prec(nf));
    4000       64019 :   PREC = maxss(PREC, nbits2prec((long)(LOGD2 * 0.02) + N*N));
    4001       64019 :   PREC = maxss(PREC, small_norm_prec(N, LOGD, LIMCMAX));
    4002       64019 :   if (DEBUGLEVEL) err_printf("PREC = %ld\n", PREC);
    4003             : 
    4004       64019 :   if (!nf)
    4005       60344 :     nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    4006        3675 :   else if (nf_get_prec(nf) < PREC)
    4007         161 :     nf = nfnewprec_shallow(nf, PREC);
    4008       64022 :   zu = nfrootsof1(nf);
    4009       64021 :   gel(zu,2) = nf_to_scalar_or_alg(nf, gel(zu,2));
    4010             : 
    4011       64021 :   nf_get_sign(nf, &R1, &R2); RU = R1+R2;
    4012       64021 :   auts = automorphism_matrices(nf, &cyclic);
    4013       64022 :   F.embperm = automorphism_perms(nf_get_M(nf), auts, cyclic, R1, R2, N);
    4014       64022 :   if (DEBUGLEVEL)
    4015             :   {
    4016           0 :     timer_printf(&T, "nfinit & nfrootsof1");
    4017           0 :     err_printf("%s bnf: R1 = %ld, R2 = %ld\nD = %Ps\n",
    4018             :                flag? "Algebraic": "Floating point", R1,R2, D);
    4019             :   }
    4020       64022 :   if (LOGD < 20.)
    4021             :   { /* tiny disc, Minkowski may be smaller than Bach */
    4022       62566 :     lim = exp(-N + R2 * log(4/M_PI) + LOGD/2) * sqrt(2*M_PI*N);
    4023       62566 :     if (lim < 3) lim = 3;
    4024             :   }
    4025             :   else /* to be ignored */
    4026        1456 :     lim = -1;
    4027       64022 :   if (cbach > 12.) {
    4028           0 :     if (cbach2 < cbach) cbach2 = cbach;
    4029           0 :     cbach = 12.;
    4030             :   }
    4031       64022 :   if (cbach < 0.)
    4032           0 :     pari_err_DOMAIN("Buchall","Bach constant","<",gen_0,dbltor(cbach));
    4033             : 
    4034       64022 :   cache.base = NULL; F.subFB = NULL; F.LP = NULL; SUnits = Ce = NULL;
    4035       64022 :   init_GRHcheck(&GRHcheck, N, R1, LOGD);
    4036       64021 :   high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
    4037      312204 :   while (!GRHchk(nf, &GRHcheck, high)) { low = high; high *= 2; }
    4038      248225 :   while (high - low > 1)
    4039             :   {
    4040      184204 :     long test = (low+high)/2;
    4041      184204 :     if (GRHchk(nf, &GRHcheck, test)) high = test; else low = test;
    4042             :   }
    4043       64021 :   LIMC2 = (high == LIMC0+1 && GRHchk(nf, &GRHcheck, LIMC0))? LIMC0: high;
    4044       64021 :   if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
    4045             :   /* Assuming GRH, {P, NP <= LIMC2} generate Cl(K) */
    4046       64021 :   if (DEBUGLEVEL) err_printf("LIMC2 = %ld\n", LIMC2);
    4047       64022 :   LIMC0 = (long)(cbach*LOGD2); /* initial value for LIMC */
    4048       64022 :   LIMC = cbach? LIMC0: LIMC2; /* use {P, NP <= LIMC} as a factorbase */
    4049       64022 :   LIMC = maxss(LIMC, nthideal(&GRHcheck, nf, N));
    4050       64022 :   if (DEBUGLEVEL) timer_printf(&T, "computing Bach constant");
    4051       64022 :   LIMres = primeneeded(N, R1, R2, LOGD);
    4052       64022 :   cache_prime_dec(&GRHcheck, LIMres, nf);
    4053             :   /* invhr ~ 2^r1 (2pi)^r2 / sqrt(D) w * Res(zeta_K, s=1) = 1 / hR */
    4054      128043 :   invhr = gmul(gdiv(gmul2n(powru(mppi(DEFAULTPREC), R2), RU),
    4055       64022 :               mulri(gsqrt(D,DEFAULTPREC),gel(zu,1))),
    4056             :               compute_invres(&GRHcheck, LIMres));
    4057       64017 :   if (DEBUGLEVEL) timer_printf(&T, "computing inverse of hR");
    4058       64018 :   av = avma;
    4059             : 
    4060       66272 : START:
    4061       66272 :   if (DEBUGLEVEL) timer_start(&T);
    4062       66272 :   if (TRIES) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
    4063       66272 :   if (DEBUGLEVEL && LIMC > LIMC0)
    4064           0 :     err_printf("%s*** Bach constant: %f\n", TRIES?"\n":"", LIMC/LOGD2);
    4065       66272 :   if (cache.base)
    4066             :   {
    4067             :     REL_t *rel;
    4068        3820 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    4069        3743 :       if (rel->m) i++;
    4070          77 :     computed = cgetg(i, t_VEC);
    4071        3820 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    4072        3743 :       if (rel->m) gel(computed, i++) = rel->m;
    4073          77 :     computed = gclone(computed); delete_cache(&cache);
    4074             :   }
    4075       66272 :   TRIES++; set_avma(av);
    4076       66272 :   if (F.LP) delete_FB(&F);
    4077       66272 :   if (LIMC2 < LIMC) LIMC2 = LIMC;
    4078       66272 :   if (DEBUGLEVEL) { err_printf("LIMC = %ld, LIMC2 = %ld\n",LIMC,LIMC2); }
    4079             : 
    4080       66272 :   FBgen(&F, nf, N, LIMC, LIMC2, &GRHcheck);
    4081       66276 :   if (!F.KC) goto START;
    4082       66276 :   av = avma;
    4083       66276 :   subFBgen(&F,auts,cyclic,lim < 0? LIMC2: mindd(lim,LIMC2),MINSFB);
    4084       66275 :   if (lg(F.subFB) == 1) goto START;
    4085       64098 :   if (DEBUGLEVEL)
    4086           0 :     timer_printf(&T, "factorbase (#subFB = %ld) and ideal permutations",
    4087           0 :                      lg(F.subFB)-1);
    4088             : 
    4089       64098 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    4090       64098 :   PERM = leafcopy(F.perm); /* to be restored in case of precision increase */
    4091       64098 :   cache.basis = zero_Flm_copy(F.KC,F.KC);
    4092       64098 :   small_multiplier = zero_Flv(F.KC);
    4093       64098 :   done_small = small_fail = squash_index = zc = sfb_trials = nreldep = 0;
    4094       64098 :   fail_limit = F.KC + 1;
    4095       64098 :   W = A = R = NULL;
    4096       64098 :   av2 = avma;
    4097       64098 :   init_rel(&cache, &F, RELSUP + RU-1);
    4098       64099 :   old_need = need = cache.end - cache.last;
    4099       64099 :   add_cyclotomic_units(nf, zu, &cache, &F);
    4100       64099 :   if (DEBUGLEVEL) err_printf("\n");
    4101       64099 :   cache.end = cache.last + need;
    4102             : 
    4103       64099 :   if (computed)
    4104             :   {
    4105        1795 :     for (i = 1; i < lg(computed); i++)
    4106        1718 :       try_elt(&cache, &F, nf, gel(computed, i), fact);
    4107          77 :     gunclone(computed);
    4108          77 :     if (DEBUGLEVEL && i > 1)
    4109           0 :       timer_printf(&T, "including already computed relations");
    4110          77 :     need = 0;
    4111             :   }
    4112             : 
    4113             :   do
    4114             :   {
    4115             :     GEN Ar, C0;
    4116             :     do
    4117             :     {
    4118      122610 :       pari_sp av4 = avma;
    4119      122610 :       if (need > 0)
    4120             :       {
    4121      122453 :         long oneed = cache.end - cache.last;
    4122             :         /* Test below can be true if small_norm did not find enough linearly
    4123             :          * dependent relations */
    4124      122453 :         if (need < oneed) need = oneed;
    4125      122453 :         pre_allocate(&cache, need+lg(auts)-1+(R ? lg(W)-1 : 0));
    4126      122453 :         cache.end = cache.last + need;
    4127      122453 :         F.L_jid = trim_list(&F);
    4128             :       }
    4129      122610 :       if (need > 0 && Nrelid > 0 && (done_small <= F.KC+1 || A) &&
    4130       70377 :           small_fail <= fail_limit &&
    4131       70377 :           cache.last < cache.base + 2*F.KC+2*RU+RELSUP /* heuristic */)
    4132             :       {
    4133       66748 :         long j, k, LIE = (R && lg(W) > 1 && (done_small % 2));
    4134       66748 :         REL_t *last = cache.last;
    4135       66748 :         pari_sp av3 = avma;
    4136       66748 :         if (LIE)
    4137             :         { /* We have full rank for class group and unit. The following tries to
    4138             :            * improve the prime group lattice by looking for relations involving
    4139             :            * the primes generating the class group. */
    4140        3371 :           long n = lg(W)-1; /* need n relations to squash the class group */
    4141        3371 :           F.L_jid = vecslice(F.perm, 1, n);
    4142        3371 :           cache.end = cache.last + n;
    4143             :           /* Lie to the add_rel subsystem: pretend we miss relations involving
    4144             :            * the primes generating the class group (and only those). */
    4145        3371 :           cache.missing = n;
    4146       10526 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 0;
    4147             :         }
    4148       66748 :         j = done_small % (F.KC+1);
    4149       66748 :         if (j && !A)
    4150             :         { /* Prevent considering both P_iP_j and P_jP_i in small_norm */
    4151             :           /* Not all elements end up in F.L_jid (eliminated by hnfspec/add or
    4152             :            * by trim_list): keep track of which ideals are being considered
    4153             :            * at each run. */
    4154         407 :           long mj = small_multiplier[j];
    4155        6359 :           for (i = k = 1; i < lg(F.L_jid); i++)
    4156        5952 :             if (F.L_jid[i] > mj)
    4157             :             {
    4158        5952 :               small_multiplier[F.L_jid[i]] = j;
    4159        5952 :               F.L_jid[k++] = F.L_jid[i];
    4160             :             }
    4161         407 :           setlg(F.L_jid, k);
    4162             :         }
    4163       66748 :         if (lg(F.L_jid) > 1) small_norm(&cache, &F, nf, Nrelid, max_fact, idex, nbthr, fact, j);
    4164       66748 :         F.L_jid = F.perm; set_avma(av3);
    4165       66748 :         if (!A && cache.last != last) small_fail = 0; else small_fail++;
    4166       66748 :         if (LIE)
    4167             :         { /* restore add_rel subsystem: undo above lie */
    4168        3371 :           long n = lg(W) - 1;
    4169       10526 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 1;
    4170        3371 :           cache.missing = 0;
    4171             :         }
    4172       66748 :         cache.end = cache.last;
    4173       66748 :         done_small++;
    4174       66748 :         need = F.sfb_chg = 0;
    4175             :       }
    4176      122610 :       if (need > 0)
    4177             :       { /* Random relations */
    4178       55705 :         if (++nreldep > F.MAXDEPSIZESFB) {
    4179          14 :           if (++sfb_trials > SFB_MAX && LIMC < LIMCMAX/2) goto START;
    4180          14 :           F.sfb_chg = sfb_INCREASE;
    4181          14 :           nreldep = 0;
    4182             :         }
    4183       55691 :         else if (!(nreldep % F.MAXDEPSFB))
    4184       26406 :           F.sfb_chg = sfb_CHANGE;
    4185       55705 :         if (F.sfb_chg && !subFB_change(&F)) goto START;
    4186       55628 :         rnd_rel(&cache, &F, nf, max_fact, nbthr, fact);
    4187       55629 :         F.L_jid = F.perm;
    4188             :       }
    4189      122534 :       if (DEBUGLEVEL) timer_start(&T);
    4190      122534 :       if (precpb)
    4191             :       {
    4192             :         REL_t *rel;
    4193          80 :         if (DEBUGLEVEL)
    4194             :         {
    4195           0 :           char str[64]; sprintf(str,"Buchall_param (%s)",precpb);
    4196           0 :           pari_warn(warnprec,str,PREC);
    4197             :         }
    4198          80 :         nf = _nfnewprec(nf, PREC, &nfisclone);
    4199          80 :         precdouble++; precpb = NULL;
    4200             : 
    4201          80 :         if (flag)
    4202             :         { /* recompute embs only, no need to redo HNF */
    4203          38 :           long j, le = lg(embs), lC = lg(C);
    4204          38 :           GEN E, M = nf_get_M(nf);
    4205          38 :           set_avma(av4);
    4206       12611 :           for (rel = cache.base+1, i = 1; i < le; i++,rel++)
    4207       12573 :             gel(embs,i) = rel_embed(rel, &F, embs, i, M, RU, R1, PREC);
    4208          38 :           E = RgM_ZM_mul(embs, rowslice(C, RU+1, nbrows(C)));
    4209       12611 :           for (j = 1; j < lC; j++)
    4210       65595 :             for (i = 1; i <= RU; i++) gcoeff(C,i,j) = gcoeff(E,i,j);
    4211          38 :           av4 = avma;
    4212             :         }
    4213             :         else
    4214             :         { /* recompute embs + HNF */
    4215       10318 :           for(i = 1; i < lg(PERM); i++) F.perm[i] = PERM[i];
    4216          42 :           cache.chk = cache.base;
    4217          42 :           W = NULL;
    4218             :         }
    4219          80 :         if (DEBUGLEVEL) timer_printf(&T, "increasing accuracy");
    4220             :       }
    4221      122534 :       set_avma(av4);
    4222      122533 :       if (cache.chk != cache.last)
    4223             :       { /* Reduce relation matrices */
    4224      122414 :         long l = cache.last - cache.chk + 1, j;
    4225      122414 :         GEN mat = cgetg(l, t_MAT);
    4226             :         REL_t *rel;
    4227             : 
    4228     1126298 :         for (j=1,rel = cache.chk + 1; j < l; rel++,j++) gel(mat,j) = rel->R;
    4229      122414 :         if (!flag || W)
    4230             :         {
    4231       59918 :           embs = get_embs(&F, &cache, nf, embs, PREC);
    4232       59918 :           if (DEBUGLEVEL && timer_get(&T) > 1)
    4233           0 :             timer_printf(&T, "floating point embeddings");
    4234             :         }
    4235      122414 :         if (!W)
    4236             :         { /* never reduced before */
    4237       64141 :           C = flag? matbotid(&cache): embs;
    4238       64141 :           W = hnfspec_i(mat, F.perm, &dep, &B, &C, F.subFB ? lg(F.subFB)-1:0);
    4239       64141 :           if (DEBUGLEVEL)
    4240           0 :             timer_printf(&T, "hnfspec [%ld x %ld]", lg(F.perm)-1, l-1);
    4241       64141 :           if (flag)
    4242             :           {
    4243       62496 :             PREC += nbits2extraprec(gexpo(C));
    4244       62496 :             if (nf_get_prec(nf) < PREC) nf = _nfnewprec(nf, PREC, &nfisclone);
    4245       62496 :             embs = get_embs(&F, &cache, nf, embs, PREC);
    4246       62495 :             C = vconcat(RgM_ZM_mul(embs, C), C);
    4247             :           }
    4248       64141 :           if (DEBUGLEVEL)
    4249           0 :             timer_printf(&T, "hnfspec floating points");
    4250             :         }
    4251             :         else
    4252             :         {
    4253       58273 :           long k = lg(embs);
    4254       58273 :           GEN E = vecslice(embs, k-l+1,k-1);
    4255       58273 :           if (flag)
    4256             :           {
    4257       55465 :             E = matbotidembs(&cache, E);
    4258       55465 :             matenlarge(C, cache.last - cache.chk);
    4259             :           }
    4260       58273 :           W = hnfadd_i(W, F.perm, &dep, &B, &C, mat, E);
    4261       58273 :           if (DEBUGLEVEL)
    4262           0 :             timer_printf(&T, "hnfadd (%ld + %ld)", l-1, lg(dep)-1);
    4263             :         }
    4264      122414 :         (void)gc_all(av2, 5, &W,&C,&B,&dep,&embs);
    4265      122414 :         cache.chk = cache.last;
    4266             :       }
    4267         119 :       else if (!W)
    4268             :       {
    4269           0 :         need = old_need;
    4270           0 :         F.L_jid = vecslice(F.perm, 1, need);
    4271           0 :         continue;
    4272             :       }
    4273      122533 :       need = F.KC - (lg(W)-1) - (lg(B)-1);
    4274      122533 :       if (!need && cache.missing)
    4275             :       { /* The test above will never be true except if 27449|class number.
    4276             :          * Ensure that if we have maximal rank for the ideal lattice, then
    4277             :          * cache.missing == 0. */
    4278          14 :         for (i = 1; cache.missing; i++)
    4279           7 :           if (!mael(cache.basis, i, i))
    4280             :           {
    4281             :             long j;
    4282           7 :             cache.missing--; mael(cache.basis, i, i) = 1;
    4283         427 :             for (j = i+1; j <= F.KC; j++) mael(cache.basis, j, i) = 0;
    4284             :           }
    4285             :       }
    4286      122533 :       zc = (lg(C)-1) - (lg(B)-1) - (lg(W)-1);
    4287      122533 :       if (RU-1-zc > 0) need = minss(need + RU-1-zc, F.KC); /* for units */
    4288      122533 :       if (need)
    4289             :       { /* dependent rows */
    4290         814 :         F.L_jid = vecslice(F.perm, 1, need);
    4291         814 :         vecsmall_sort(F.L_jid);
    4292         814 :         if (need != old_need) { nreldep = 0; old_need = need; }
    4293             :       }
    4294             :       else
    4295             :       { /* If the relation lattice is too small, check will be > 1 and we will
    4296             :          * do a new run of small_norm/rnd_rel asking for 1 relation. This often
    4297             :          * gives a relation involving L_jid[1]. We rotate the first element of
    4298             :          * L_jid in order to increase the probability of finding relations that
    4299             :          * increases the lattice. */
    4300      121719 :         long j, n = lg(W) - 1;
    4301      121719 :         if (n > 1 && squash_index % n)
    4302             :         {
    4303        8983 :           F.L_jid = leafcopy(F.perm);
    4304       36396 :           for (j = 1; j <= n; j++)
    4305       27413 :             F.L_jid[j] = F.perm[1 + (j + squash_index - 1) % n];
    4306             :         }
    4307             :         else
    4308      112736 :           F.L_jid = F.perm;
    4309      121719 :         squash_index++;
    4310             :       }
    4311             :     }
    4312      122533 :     while (need);
    4313             : 
    4314      121719 :     if (!A)
    4315             :     {
    4316       64106 :       small_fail = old_need = 0;
    4317       64106 :       fail_limit = maxss(F.KC / FAIL_DIVISOR, MINFAIL);
    4318             :     }
    4319      121719 :     A = vecslice(C, 1, zc); /* cols corresponding to units */
    4320      121719 :     if (flag) A = rowslice(A, 1, RU);
    4321      121719 :     Ar = real_i(A);
    4322      121720 :     R = compute_multiple_of_R(Ar, RU, N, &need, &bit, &lambda);
    4323      121720 :     if (need < old_need) small_fail = 0;
    4324             : #if 0 /* A good idea if we are indeed stuck but needs tuning */
    4325             :     /* we have computed way more relations than should be necessary */
    4326             :     if (TRIES < 3 && LIMC < LIMCMAX / 8 &&
    4327             :                      cache.last - cache.base > 10 * F.KC) goto START;
    4328             : #endif
    4329      121720 :     old_need = need;
    4330      121720 :     if (!lambda)
    4331          11 :     { precpb = "bestappr"; PREC = myprecdbl(PREC, flag? C: NULL); continue; }
    4332      121709 :     if (!R)
    4333             :     { /* not full rank for units */
    4334       25380 :       if (!need)
    4335           0 :       { precpb = "regulator"; PREC = myprecdbl(PREC, flag? C: NULL); }
    4336       25380 :       continue;
    4337             :     }
    4338       96329 :     if (cache.last==old_cache) { need=1; continue; }
    4339       96246 :     old_cache = cache.last;
    4340       96246 :     h = ZM_det_triangular(W);
    4341       96244 :     if (DEBUGLEVEL) err_printf("\n#### Tentative class number: %Ps\n", h);
    4342       96244 :     i = compute_R(lambda, mulir(h,invhr), &L, &R);
    4343       96242 :     if (DEBUGLEVEL)
    4344             :     {
    4345           0 :       err_printf("\n");
    4346           0 :       timer_printf(&T, "computing regulator and check");
    4347             :     }
    4348       96243 :     switch(i)
    4349             :     {
    4350       32154 :       case fupb_RELAT:
    4351       32154 :         need = 1; /* not enough relations */
    4352       32154 :         continue;
    4353          62 :       case fupb_PRECI: /* prec problem unless we cheat on Bach constant */
    4354          62 :         if ((precdouble&7) == 7 && LIMC <= LIMCMAX/2) goto START;
    4355          62 :         precpb = "compute_R"; PREC = myprecdbl(PREC, flag? C: NULL);
    4356          62 :         continue;
    4357             :     }
    4358             :     /* DONE */
    4359             : 
    4360       64027 :     if (F.KCZ2 > F.KCZ)
    4361             :     {
    4362           7 :       if (F.sfb_chg && !subFB_change(&F)) goto START;
    4363           7 :       if (!be_honest(&F, nf, auts, fact)) goto START;
    4364           7 :       if (DEBUGLEVEL) timer_printf(&T, "to be honest");
    4365             :     }
    4366       64027 :     F.KCZ2 = 0; /* be honest only once */
    4367             : 
    4368             :     /* fundamental units */
    4369             :     {
    4370       64027 :       GEN AU, CU, U, v = extract_full_lattice(L); /* L may be large */
    4371       64027 :       CU = NULL;
    4372       64027 :       if (v) { A = vecpermute(A, v); L = vecpermute(L, v); }
    4373             :       /* arch. components of fund. units */
    4374       64027 :       U = ZM_lll(L, 0.99, LLL_IM);
    4375       64028 :       U = ZM_mul(U, lll(RgM_ZM_mul(real_i(A), U)));
    4376       64028 :       if (DEBUGLEVEL) timer_printf(&T, "units LLL");
    4377       64028 :       AU = RgM_ZM_mul(A, U);
    4378       64029 :       A = cleanarchunit(AU, N, NULL, PREC);
    4379       64029 :       if (RU > 1 /* if there are fund units, test we have correct regulator */
    4380       48804 :           && (!A || lg(A) < RU || expo(subrr(get_regulator(A), R)) > -1))
    4381           7 :       {
    4382           7 :         long add = nbits2extraprec( gexpo(AU) + 64 ) - gprecision(AU);
    4383           7 :         long t = maxss(PREC * 0.15, add);
    4384           7 :         if (!A && DEBUGLEVEL) err_printf("### Incorrect units lognorm");
    4385           7 :         precpb = "cleanarch"; PREC += maxss(t, EXTRAPREC64); continue;
    4386             :       }
    4387       64022 :       if (flag)
    4388             :       {
    4389       62433 :         long l = lgcols(C) - RU;
    4390             :         REL_t *rel;
    4391       62433 :         SUnits = cgetg(l, t_COL);
    4392     1010149 :         for (rel = cache.base+1, i = 1; i < l; i++,rel++)
    4393      947717 :           set_rel_alpha(rel, auts, SUnits, i);
    4394       62432 :         if (RU > 1)
    4395             :         {
    4396       47718 :           GEN c = v? vecpermute(C,v): vecslice(C,1,zc);
    4397       47718 :           CU = ZM_mul(rowslice(c, RU+1, nbrows(c)), U);
    4398             :         }
    4399             :       }
    4400       64022 :       if (DEBUGLEVEL) err_printf("\n#### Computing fundamental units\n");
    4401       64022 :       fu = getfu(nf, &A, CU? &U: NULL, PREC);
    4402       64022 :       CU = CU? ZM_mul(CU, U): cgetg(1, t_MAT);
    4403       64022 :       if (DEBUGLEVEL) timer_printf(&T, "getfu");
    4404       64022 :       Ce = vecslice(C, zc+1, lg(C)-1);
    4405       64022 :       if (flag) SUnits = mkvec4(SUnits, CU, rowslice(Ce, RU+1, nbrows(Ce)),
    4406             :                                 utoipos(LIMC));
    4407             :     }
    4408             :     /* class group generators */
    4409       64022 :     if (flag) Ce = rowslice(Ce, 1, RU);
    4410       64022 :     C0 = Ce; Ce = cleanarch(Ce, N, NULL, PREC);
    4411       64021 :     if (!Ce) {
    4412           0 :       long add = nbits2extraprec( gexpo(C0) + 64 ) - gprecision(C0);
    4413           0 :       precpb = "cleanarch"; PREC += maxss(add, 1);
    4414             :     }
    4415       64021 :     if (DEBUGLEVEL) timer_printf(&T, "cleanarch");
    4416      121718 :   } while (need || precpb);
    4417             : 
    4418       64021 :   Vbase = vecpermute(F.LP, F.perm);
    4419       64022 :   if (!fu) fu = cgetg(1, t_MAT);
    4420       64022 :   if (!SUnits) SUnits = gen_1;
    4421       64022 :   clg1 = class_group_gen(nf,W,Ce,Vbase,PREC, &clg2);
    4422       64020 :   res = mkvec5(clg1, R, SUnits, zu, fu);
    4423       64020 :   res = buchall_end(nf,res,clg2,W,B,A,Ce,Vbase);
    4424       64020 :   delete_FB(&F);
    4425       64022 :   res = gc_GEN(av0, res);
    4426       64022 :   if (flag) obj_insert_shallow(res, MATAL, cgetg(1,t_VEC));
    4427       64022 :   if (nfisclone) gunclone(nf);
    4428       64022 :   delete_cache(&cache);
    4429       64022 :   free_GRHcheck(&GRHcheck);
    4430       64022 :   return res;
    4431             : }

Generated by: LCOV version 1.16