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.16.2 lcov report (development 29419-8afb0ed749) Lines: 2190 2395 91.4 %
Date: 2024-07-02 09:03:41 Functions: 155 166 93.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation; 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       63749 : delete_cache(RELCACHE_t *M)
     135             : {
     136             :   REL_t *rel;
     137     1037569 :   for (rel = M->base+1; rel <= M->last; rel++)
     138             :   {
     139      973819 :     gunclone(rel->R);
     140      973819 :     if (rel->m) gunclone(rel->m);
     141             :   }
     142       63750 :   pari_free((void*)M->base); M->base = NULL;
     143       63749 : }
     144             : 
     145             : static void
     146       65926 : delete_FB(FB_t *F)
     147             : {
     148             :   subFB_t *s, *sold;
     149      132030 :   for (s = F->allsubFB; s; s = sold) { sold = s->old; pari_free(s); }
     150       65926 :   gunclone(F->minidx);
     151       65926 :   gunclone(F->idealperm);
     152       65926 : }
     153             : 
     154             : static void
     155       63749 : reallocate(RELCACHE_t *M, long len)
     156             : {
     157       63749 :   M->len = len;
     158       63749 :   if (!M->base)
     159       63749 :     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       63749 : }
     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      275200 : bad_subFB(FB_t *F, long t)
     175             : {
     176      275200 :   GEN LP, P = gel(F->LP,t);
     177      275200 :   long p = pr_get_smallp(P);
     178      275200 :   LP = gel(F->LV,p);
     179      275200 :   return (isclone(LP) && t == F->iLP[p] + lg(LP)-1);
     180             : }
     181             : 
     182             : static void
     183       66104 : assign_subFB(FB_t *F, GEN yes, long iyes)
     184             : {
     185       66104 :   long i, lv = sizeof(subFB_t) + iyes*sizeof(long); /* for struct + GEN */
     186       66104 :   subFB_t *s = (subFB_t *)pari_malloc(lv);
     187       66104 :   s->subFB = (GEN)&s[1];
     188       66104 :   s->old = F->allsubFB; F->allsubFB = s;
     189      284815 :   for (i = 0; i < iyes; i++) s->subFB[i] = yes[i];
     190       66104 :   F->subFB = s->subFB;
     191       66104 :   F->MAXDEPSIZESFB = (iyes-1) * DEPSIZESFBMULT;
     192       66104 :   F->MAXDEPSFB = F->MAXDEPSIZESFB / DEPSFBDIV;
     193       66104 : }
     194             : 
     195             : /* Determine the permutation of the ideals made by each field automorphism */
     196             : static GEN
     197       65926 : FB_aut_perm(FB_t *F, GEN auts, GEN cyclic)
     198             : {
     199       65926 :   long i, j, m, KC = F->KC, nauts = lg(auts)-1;
     200       65926 :   GEN minidx, perm = zero_Flm_copy(KC, nauts);
     201             : 
     202       65926 :   if (!nauts) { F->minidx = gclone(identity_zv(KC)); return cgetg(1,t_MAT); }
     203       41657 :   minidx = zero_Flv(KC);
     204       90664 :   for (m = 1; m < lg(cyclic); m++)
     205             :   {
     206       49007 :     GEN thiscyc = gel(cyclic, m);
     207       49007 :     long k0 = thiscyc[1];
     208       49007 :     GEN aut = gel(auts, k0), permk0 = gel(perm, k0), ppermk;
     209       49007 :     i = 1;
     210      210960 :     while (i <= KC)
     211             :     {
     212      161953 :       pari_sp av2 = avma;
     213      161953 :       GEN seen = zero_Flv(KC), P = gel(F->LP, i);
     214      161954 :       long imin = i, p, f, l;
     215      161954 :       p = pr_get_smallp(P);
     216      161954 :       f = pr_get_f(P);
     217             :       do
     218             :       {
     219      476382 :         if (++i > KC) break;
     220      427375 :         P = gel(F->LP, i);
     221             :       }
     222      427375 :       while (p == pr_get_smallp(P) && f == pr_get_f(P));
     223      638328 :       for (j = imin; j < i; j++)
     224             :       {
     225      476380 :         GEN img = ZM_ZC_mul(aut, pr_get_gen(gel(F->LP, j)));
     226     1659079 :         for (l = imin; l < i; l++)
     227     1659079 :           if (!seen[l] && ZC_prdvd(img, gel(F->LP, l)))
     228             :           {
     229      476374 :             seen[l] = 1; permk0[j] = l; break;
     230             :           }
     231             :       }
     232      161948 :       set_avma(av2);
     233             :     }
     234       68033 :     for (ppermk = permk0, i = 2; i < lg(thiscyc); i++)
     235             :     {
     236       19026 :       GEN permk = gel(perm, thiscyc[i]);
     237      382578 :       for (j = 1; j <= KC; j++) permk[j] = permk0[ppermk[j]];
     238       19026 :       ppermk = permk;
     239             :     }
     240             :   }
     241      308195 :   for (j = 1; j <= KC; j++)
     242             :   {
     243      266538 :     if (minidx[j]) continue;
     244      128302 :     minidx[j] = j;
     245      357691 :     for (i = 1; i <= nauts; i++) minidx[coeff(perm, j, i)] = j;
     246             :   }
     247       41657 :   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       65925 : subFBgen(FB_t *F, GEN auts, GEN cyclic, double PROD, long minsFB)
     255             : {
     256             :   GEN y, perm, yes, no;
     257       65925 :   long i, j, k, iyes, ino, lv = F->KC + 1;
     258             :   double prod;
     259             :   pari_sp av;
     260             : 
     261       65925 :   F->LP   = cgetg(lv, t_VEC);
     262       65925 :   F->L_jid = F->perm = cgetg(lv, t_VECSMALL);
     263       65925 :   av = avma;
     264       65925 :   y = cgetg(lv,t_COL); /* Norm P */
     265      310958 :   for (k=0, i=1; i <= F->KCZ; i++)
     266             :   {
     267      245032 :     GEN LP = gel(F->LV,F->FB[i]);
     268      245032 :     long l = lg(LP);
     269      708451 :     for (j = 1; j < l; j++)
     270             :     {
     271      463419 :       GEN P = gel(LP,j);
     272      463419 :       k++;
     273      463419 :       gel(y,k) = pr_norm(P);
     274      463419 :       gel(F->LP,k) = P;
     275             :     }
     276             :   }
     277             :   /* perm sorts LP by increasing norm */
     278       65926 :   perm = indexsort(y);
     279       65926 :   no  = cgetg(lv, t_VECSMALL); ino  = 1;
     280       65926 :   yes = cgetg(lv, t_VECSMALL); iyes = 1;
     281       65926 :   prod = 1.0;
     282      301940 :   for (i = 1; i < lv; i++)
     283             :   {
     284      272001 :     long t = perm[i];
     285      272001 :     if (bad_subFB(F, t)) { no[ino++] = t; continue; }
     286             : 
     287      152103 :     yes[iyes++] = t;
     288      152103 :     prod *= (double)itos(gel(y,t));
     289      152103 :     if (iyes > minsFB && prod > PROD) break;
     290             :   }
     291       65926 :   setlg(yes, iyes);
     292      218029 :   for (j=1; j<iyes; j++)     F->perm[j] = yes[j];
     293      185824 :   for (i=1; i<ino; i++, j++) F->perm[j] =  no[i];
     294      257345 :   for (   ; j<lv; j++)       F->perm[j] =  perm[j];
     295       65926 :   F->allsubFB = NULL;
     296       65926 :   F->idealperm = gclone(FB_aut_perm(F, auts, cyclic));
     297       65926 :   if (iyes) assign_subFB(F, yes, iyes);
     298       65926 :   set_avma(av);
     299       65926 : }
     300             : static int
     301        1848 : subFB_change(FB_t *F)
     302             : {
     303        1848 :   long i, iyes, minsFB, lv = F->KC + 1, l = lg(F->subFB)-1;
     304        1848 :   pari_sp av = avma;
     305        1848 :   GEN yes, L_jid = F->L_jid, present = zero_zv(lv-1);
     306             : 
     307        1848 :   switch (F->sfb_chg)
     308             :   {
     309          14 :     case sfb_INCREASE: minsFB = l + 1; break;
     310        1834 :     default: minsFB = l; break;
     311             :   }
     312             : 
     313        1848 :   yes = cgetg(minsFB+1, t_VECSMALL); iyes = 1;
     314        1848 :   if (L_jid)
     315             :   {
     316        3113 :     for (i = 1; i < lg(L_jid); i++)
     317             :     {
     318        2970 :       long l = L_jid[i];
     319        2970 :       if (bad_subFB(F, l)) continue;
     320        2523 :       yes[iyes++] = l;
     321        2523 :       present[l] = 1;
     322        2523 :       if (iyes > minsFB) break;
     323             :     }
     324             :   }
     325           0 :   else i = 1;
     326        1848 :   if (iyes <= minsFB)
     327             :   {
     328         306 :     for ( ; i < lv; i++)
     329             :     {
     330         236 :       long l = F->perm[i];
     331         236 :       if (present[l] || bad_subFB(F, l)) continue;
     332          80 :       yes[iyes++] = l;
     333          80 :       if (iyes > minsFB) break;
     334             :     }
     335         143 :     if (i == lv) return 0;
     336             :   }
     337        1778 :   if (zv_equal(F->subFB, yes))
     338             :   {
     339        1600 :     if (DEBUGLEVEL) err_printf("\n*** NOT Changing sub factor base\n");
     340             :   }
     341             :   else
     342             :   {
     343         178 :     if (DEBUGLEVEL) err_printf("\n*** Changing sub factor base\n");
     344         178 :     assign_subFB(F, yes, iyes);
     345             :   }
     346        1778 :   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       95119 : pre_allocate(RELCACHE_t *cache, size_t n)
     352             : {
     353       95119 :   size_t len = (cache->last - cache->base) + n;
     354       95119 :   if (len >= cache->len) reallocate(cache, len << 1);
     355       95119 : }
     356             : 
     357             : void
     358      134013 : init_GRHcheck(GRHcheck_t *S, long N, long R1, double LOGD)
     359             : {
     360      134013 :   const double c1 = M_PI*M_PI/2;
     361      134013 :   const double c2 = 3.663862376709;
     362      134013 :   const double c3 = 3.801387092431; /* Euler + log(8*Pi)*/
     363      134013 :   S->clone = 0;
     364      134013 :   S->cN = R1*c2 + N*c1;
     365      134013 :   S->cD = LOGD - N*c3 - R1*M_PI/2;
     366      134013 :   S->maxprimes = 16000; /* sufficient for LIMC=176081*/
     367      134013 :   S->primes = (GRHprime_t*)pari_malloc(S->maxprimes*sizeof(*S->primes));
     368      134014 :   S->nprimes = 0;
     369      134014 :   S->limp = 0;
     370      134014 :   u_forprime_init(&S->P, 2, ULONG_MAX);
     371      134012 : }
     372             : 
     373             : void
     374      134014 : free_GRHcheck(GRHcheck_t *S)
     375             : {
     376      134014 :   if (S->clone)
     377             :   {
     378       63679 :     long i = S->nprimes;
     379             :     GRHprime_t *pr;
     380     7537011 :     for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--) gunclone(pr->dec);
     381             :   }
     382      134017 :   pari_free(S->primes);
     383      134014 : }
     384             : 
     385             : int
     386     1527122 : GRHok(GRHcheck_t *S, double L, double SA, double SB)
     387             : {
     388     1527122 :   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     7471041 : get_fs(GEN nf, GEN P, GEN index, ulong p)
     395             : {
     396             :   long j, k, f, n, l;
     397             :   GEN fs, ns;
     398             : 
     399     7471041 :   if (umodiu(index, p))
     400             :   { /* easy case: p does not divide index */
     401     7432956 :     GEN F = Flx_degfact(ZX_to_Flx(P,p), p);
     402     7433755 :     fs = gel(F,1); l = lg(fs);
     403             :   }
     404             :   else
     405             :   {
     406       37944 :     GEN F = idealprimedec(nf, utoipos(p));
     407       37961 :     l = lg(F);
     408       37961 :     fs = cgetg(l, t_VECSMALL);
     409      118881 :     for (j = 1; j < l; j++) fs[j] = pr_get_f(gel(F,j));
     410             :   }
     411     7471716 :   ns = cgetg(l, t_VECSMALL);
     412     7470559 :   f = fs[1]; n = 1;
     413    13818445 :   for (j = 2, k = 1; j < l; j++)
     414     6347886 :     if (fs[j] == f)
     415     4625844 :       n++;
     416             :     else
     417             :     {
     418     1722042 :       ns[k] = n; fs[k] = f; k++;
     419     1722042 :       f = fs[j]; n = 1;
     420             :     }
     421     7470559 :   ns[k] = n; fs[k] = f; k++;
     422     7470559 :   setlg(fs, k);
     423     7470245 :   setlg(ns, k); return mkvec2(fs,ns);
     424             : }
     425             : 
     426             : /* cache data for all rational primes up to the LIM */
     427             : static void
     428      916328 : cache_prime_dec(GRHcheck_t *S, ulong LIM, GEN nf)
     429             : {
     430      916328 :   pari_sp av = avma;
     431             :   GRHprime_t *pr;
     432             :   GEN index, P;
     433             :   double nb;
     434             : 
     435      916328 :   if (S->limp >= LIM) return;
     436      327959 :   S->clone = 1;
     437      327959 :   nb = primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
     438      327968 :   GRH_ensure(S, nb+1); /* room for one extra prime */
     439      327967 :   P = nf_get_pol(nf);
     440      327967 :   index = nf_get_index(nf);
     441      327966 :   for (pr = S->primes + S->nprimes;;)
     442     7143273 :   {
     443     7471239 :     ulong p = u_forprime_next(&(S->P));
     444     7471066 :     pr->p = p;
     445     7471066 :     pr->logp = log((double)p);
     446     7471066 :     pr->dec = gclone(get_fs(nf, P, index, p));
     447     7471197 :     S->nprimes++;
     448     7471197 :     pr++;
     449     7471197 :     set_avma(av);
     450             :     /* store up to nextprime(LIM) included */
     451     7471239 :     if (p >= LIM) { S->limp = p; break; }
     452             :   }
     453             : }
     454             : 
     455             : static double
     456     2247474 : 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     2247474 :   const double  rQ = 1.83787706641;
     459     2247474 :   const double r1Q = 1.98505372441;
     460     2247474 :   const double r2Q = 1.07991541347;
     461     4494948 :   return fabs((R1+R2-1)*(12*logC3+4*logC2-9*logC-6)/(2*C*logC3)
     462     2247474 :          + (rK-rQ)*(6*logC2 + 5*logC + 2)/(C*logC3)
     463     2247474 :          - R2*(6*logC2+11*logC+6)/(C2*logC2)
     464     2247474 :          - 2*(r1K-r1Q)*(3*logC2 + 4*logC + 2)/(C2*logC3)
     465     2247474 :          + (R1+R2-1)*(12*logC3+40*logC2+45*logC+18)/(6*C3*logC3)
     466     2247474 :          + (r2K-r2Q)*(2*logC2 + 3*logC + 2)/(C3*logC3));
     467             : }
     468             : 
     469             : static double
     470     1123738 : 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     1123738 :   const double logC = log(C), logC2 = logC*logC, logC3 = logC*logC2;
     509     1123738 :   const double C2 = C*C, C3 = C*C2;
     510     1123738 :   double E1 = i >30? 0: tab[i];
     511     1123738 :   return al2K*((33*logC2+22*logC+8)/(8*logC3*sqrt(C))+15*E1/16)
     512     1123738 :     + maxdd(tailresback(rKm,r1KM,r2Km, C,C2,C3,R1,R2,logC,logC2,logC3),
     513     1123741 :             tailresback(rKM,r1Km,r2KM, C,C2,C3,R1,R2,logC,logC2,logC3))/2
     514     1123741 :     + ((R1+R2-1)*4*C+R2)*(C2+6*logC)/(4*C2*C2*logC2);
     515             : }
     516             : 
     517             : static long
     518       63679 : primeneeded(long N, long R1, long R2, double LOGD)
     519             : {
     520       63679 :   const double lim = 0.25; /* should be log(2)/2 == 0.34657... */
     521       63679 :   const double al2K =  0.3526*LOGD - 0.8212*N + 4.5007;
     522       63679 :   const double  rKm = -1.0155*LOGD + 2.1042*N - 8.3419;
     523       63679 :   const double  rKM = -0.5   *LOGD + 1.2076*N + 1;
     524       63679 :   const double r1Km = -       LOGD + 1.4150*N;
     525       63679 :   const double r1KM = -       LOGD + 1.9851*N;
     526       63679 :   const double r2Km = -       LOGD + 0.9151*N;
     527       63679 :   const double r2KM = -       LOGD + 1.0800*N;
     528       63679 :   long Cmin = 3, Cmax = 3, i = 0;
     529      571312 :   while (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, Cmax, i) > lim)
     530             :   {
     531      507633 :     Cmin = Cmax;
     532      507633 :     Cmax *= 2;
     533      507633 :     i++;
     534             :   }
     535       63678 :   i--;
     536      616118 :   while (Cmax - Cmin > 1)
     537             :   {
     538      552439 :     long t = (Cmin + Cmax)/2;
     539      552439 :     if (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, t, i) > lim)
     540      342230 :       Cmin = t;
     541             :     else
     542      210210 :       Cmax = t;
     543             :   }
     544       63679 :   return Cmax;
     545             : }
     546             : 
     547             : /* ~ 1 / Res(s = 1, zeta_K) */
     548             : static GEN
     549       63679 : compute_invres(GRHcheck_t *S, long LIMC)
     550             : {
     551       63679 :   pari_sp av = avma;
     552       63679 :   double loginvres = 0.;
     553             :   GRHprime_t *pr;
     554             :   long i;
     555       63679 :   double logLIMC = log((double)LIMC);
     556       63679 :   double logLIMC2 = logLIMC*logLIMC, denc;
     557             :   double c0, c1, c2;
     558       63679 :   denc = 1/(pow((double)LIMC, 3.) * logLIMC * logLIMC2);
     559       63679 :   c2 = (    logLIMC2 + 3 * logLIMC / 2 + 1) * denc;
     560       63679 :   denc *= LIMC;
     561       63679 :   c1 = (3 * logLIMC2 + 4 * logLIMC     + 2) * denc;
     562       63679 :   denc *= LIMC;
     563       63679 :   c0 = (3 * logLIMC2 + 5 * logLIMC / 2 + 1) * denc;
     564     7480510 :   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     7472607 :     double logp = pr->logp, NPk;
     570     7472607 :     long j, k, limp = logLIMC/logp;
     571     7472607 :     ulong p = pr->p, p2 = p*p;
     572     7472607 :     if (limp < 1) break;
     573     7416831 :     dec = pr->dec;
     574     7416831 :     fs = gel(dec, 1); ns = gel(dec, 2);
     575     7416831 :     loginvres += 1./p;
     576             :     /* NB: limp = 1 nearly always and limp > 2 for very few primes */
     577     8776160 :     for (k=2, NPk = p; k <= limp; k++) { NPk *= p; loginvres += 1/(k * NPk); }
     578     7416831 :     addpsi = limp;
     579     7416831 :     addpsi1 = p *(pow((double)p , (double)limp)-1)/(p -1);
     580     7416831 :     addpsi2 = p2*(pow((double)p2, (double)limp)-1)/(p2-1);
     581     7416831 :     j = lg(fs);
     582    16544394 :     while (--j > 0)
     583             :     {
     584             :       long f, nb, kmax;
     585             :       double NP, NP2, addinvres;
     586     9127563 :       f = fs[j]; if (f > limp) continue;
     587     3969168 :       nb = ns[j];
     588     3969168 :       NP = pow((double)p, (double)f);
     589     3969168 :       addinvres = 1/NP;
     590     3969168 :       kmax = limp / f;
     591     4843358 :       for (k=2, NPk = NP; k <= kmax; k++) { NPk *= NP; addinvres += 1/(k*NPk); }
     592     3969168 :       NP2 = NP*NP;
     593     3969168 :       loginvres -= nb * addinvres;
     594     3969168 :       addpsi -= nb * f * kmax;
     595     3969168 :       addpsi1 -= nb*(f*NP *(pow(NP ,(double)kmax)-1)/(NP -1));
     596     3969168 :       addpsi2 -= nb*(f*NP2*(pow(NP2,(double)kmax)-1)/(NP2-1));
     597             :     }
     598     7416831 :     loginvres -= (addpsi*c0 - addpsi1*c1 + addpsi2*c2)*logp;
     599             :   }
     600       63679 :   return gerepileuptoleaf(av, mpexp(dbltor(loginvres)));
     601             : }
     602             : 
     603             : static long
     604       63679 : nthideal(GRHcheck_t *S, GEN nf, long n)
     605             : {
     606       63679 :   pari_sp av = avma;
     607       63679 :   GEN P = nf_get_pol(nf);
     608       63679 :   ulong p = 0, *vecN = (ulong*)const_vecsmall(n, LONG_MAX);
     609       63679 :   long i, N = poldegree(P, -1);
     610       63679 :   for (i = 0; ; i++)
     611      229215 :   {
     612             :     GRHprime_t *pr;
     613             :     GEN fs;
     614      292894 :     cache_prime_dec(S, p+1, nf);
     615      292894 :     pr = S->primes + i;
     616      292894 :     fs = gel(pr->dec, 1);
     617      292894 :     p = pr->p;
     618      292894 :     if (fs[1] != N)
     619             :     {
     620      196616 :       GEN ns = gel(pr->dec, 2);
     621      196616 :       long k, l, j = lg(fs);
     622      440874 :       while (--j > 0)
     623             :       {
     624      244258 :         ulong NP = upowuu(p, fs[j]);
     625             :         long nf;
     626      244258 :         if (!NP) continue;
     627      749788 :         for (k = 1; k <= n; k++) if (vecN[k] > NP) break;
     628      243866 :         if (k > n) continue;
     629             :         /* vecN[k] <= NP */
     630      157933 :         nf = ns[j]; /*#{primes of norme NP} = nf, insert them here*/
     631      353386 :         for (l = k+nf; l <= n; l++) vecN[l] = vecN[l-nf];
     632      398850 :         for (l = 0; l < nf && k+l <= n; l++) vecN[k+l] = NP;
     633      363286 :         while (l <= k) vecN[l++] = NP;
     634             :       }
     635             :     }
     636      292894 :     if (p > vecN[n]) break;
     637             :   }
     638       63679 :   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       65925 : ballvol(long n)
     644             : {
     645       65925 :   double v = odd(n)? 2: 1;
     646      150568 :   for (; n > 1; n -= 2) v *= (2 * M_PI) / n;
     647       65925 :   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       65924 : 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       65924 :   const double L = log((double)C2 + 0.5);
     660             : 
     661       65924 :   cache_prime_dec(S, C2, nf);
     662       65924 :   pr = S->primes;
     663       65924 :   F->sfb_chg = 0;
     664       65924 :   F->FB  = cgetg(C2+1, t_VECSMALL);
     665       65924 :   F->iLP = cgetg(C2+1, t_VECSMALL);
     666       65924 :   F->LV = zerovec(C2);
     667             : 
     668       65925 :   prim = icopy(gen_1);
     669       65925 :   i = ip = 0;
     670       65925 :   F->KC = F->KCZ = 0;
     671      434425 :   for (;; pr++) /* p <= C2 */
     672      434425 :   {
     673      500350 :     ulong p = pr->p;
     674             :     long k, l, m;
     675             :     GEN LP, nb, f;
     676             : 
     677      500350 :     if (!F->KC && p > C1) { F->KCZ = i; F->KC = ip; }
     678      500350 :     if (p > C2) break;
     679             : 
     680      463145 :     if (DEBUGLEVEL>1) err_printf(" %ld",p);
     681             : 
     682      463145 :     f = gel(pr->dec, 1); nb = gel(pr->dec, 2);
     683      463145 :     if (f[1] == N)
     684             :     {
     685      145647 :       if (p == C2) break;
     686      137114 :       continue; /* p inert */
     687             :     }
     688      317498 :     l = (long)(L/pr->logp); /* p^f <= C2  <=> f <= l */
     689      579210 :     for (k=0, m=1; m < lg(f) && f[m]<=l; m++) k += nb[m];
     690      317498 :     if (!k)
     691             :     { /* too inert to appear in FB */
     692       72460 :       if (p == C2) break;
     693       71823 :       continue;
     694             :     }
     695      245038 :     prim[2] = p; LP = idealprimedec_limit_f(nf,prim, l);
     696             :     /* keep noninert ideals with Norm <= C2 */
     697      245038 :     if (m == lg(f)) setisclone(LP); /* flag it: all prime divisors in FB */
     698      245038 :     F->FB[++i]= p;
     699      245038 :     gel(F->LV,p) = LP;
     700      245038 :     F->iLP[p] = ip; ip += k;
     701      245038 :     if (p == C2) break;
     702             :   }
     703       65925 :   if (!F->KC) { F->KCZ = i; F->KC = ip; }
     704             :   /* Note F->KC > 0 otherwise GRHchk is false */
     705       65925 :   setlg(F->FB, F->KCZ+1); F->KCZ2 = i;
     706       65925 :   F->prodZ = zv_prod_Z(F->FB);
     707       65926 :   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       65926 :   F->perm = NULL; F->L_jid = NULL;
     719       65926 :   F->ballvol = ballvol(nf_get_degree(nf));
     720       65925 : }
     721             : 
     722             : static int
     723      493844 : GRHchk(GEN nf, GRHcheck_t *S, ulong LIMC)
     724             : {
     725      493844 :   double logC = log((double)LIMC), SA = 0, SB = 0;
     726      493844 :   GRHprime_t *pr = S->primes;
     727             : 
     728      493844 :   cache_prime_dec(S, LIMC, nf);
     729      493844 :   for (pr = S->primes;; pr++)
     730     3046532 :   {
     731     3540376 :     ulong p = pr->p;
     732             :     GEN dec, fs, ns;
     733             :     double logCslogp;
     734             :     long j;
     735             : 
     736     3540376 :     if (p > LIMC) break;
     737     3152284 :     dec = pr->dec; fs = gel(dec, 1); ns = gel(dec,2);
     738     3152284 :     logCslogp = logC/pr->logp;
     739     4962107 :     for (j = 1; j < lg(fs); j++)
     740             :     {
     741     3880408 :       long f = fs[j], M, nb;
     742             :       double logNP, q, A, B;
     743     3880408 :       if (f > logCslogp) break;
     744     1809825 :       logNP = f * pr->logp;
     745     1809825 :       q = 1/sqrt((double)upowuu(p, f));
     746     1809823 :       A = logNP * q; B = logNP * A; M = (long)(logCslogp/f);
     747     1809823 :       if (M > 1)
     748             :       {
     749      375221 :         double inv1_q = 1 / (1-q);
     750      375221 :         A *= (1 - pow(q, (double)M)) * inv1_q;
     751      375221 :         B *= (1 - pow(q, (double)M)*(M+1 - M*q)) * inv1_q * inv1_q;
     752             :       }
     753     1809823 :       nb = ns[j];
     754     1809823 :       SA += nb * A;
     755     1809823 :       SB += nb * B;
     756             :     }
     757     3152282 :     if (p == LIMC) break;
     758             :   }
     759      493842 :   return GRHok(S, logC, SA, SB);
     760             : }
     761             : 
     762             : /*  SMOOTH IDEALS */
     763             : static void
     764     9245478 : store(long i, long e, FACT *fact)
     765             : {
     766     9245478 :   ++fact[0].pr;
     767     9245478 :   fact[fact[0].pr].pr = i; /* index */
     768     9245478 :   fact[fact[0].pr].ex = e; /* exponent */
     769     9245478 : }
     770             : 
     771             : /* divide out m by all P|p, k = v_p(Nm) */
     772             : static int
     773        2228 : divide_p_elt(GEN LP, long ip, long k, GEN m, FACT *fact)
     774             : {
     775        2228 :   long j, l = lg(LP);
     776        3008 :   for (j=1; j<l; j++)
     777             :   {
     778        3008 :     GEN P = gel(LP,j);
     779        3008 :     long v = ZC_nfval(m, P);
     780        3008 :     if (!v) continue;
     781        2599 :     store(ip + j, v, fact); /* v = v_P(m) > 0 */
     782        2599 :     k -= v * pr_get_f(P);
     783        2599 :     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      163444 : divide_p_id(GEN LP, long ip, long k, GEN nf, GEN I, FACT *fact)
     790             : {
     791      163444 :   long j, l = lg(LP);
     792      245232 :   for (j=1; j<l; j++)
     793             :   {
     794      237370 :     GEN P = gel(LP,j);
     795      237370 :     long v = idealval(nf,I, P);
     796      237370 :     if (!v) continue;
     797      159081 :     store(ip + j, v, fact); /* v = v_P(I) > 0 */
     798      159082 :     k -= v * pr_get_f(P);
     799      159082 :     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     5335975 : divide_p_quo(GEN LP, long ip, long k, GEN nf, GEN I, GEN m, FACT *fact)
     806             : {
     807     5335975 :   long j, l = lg(LP);
     808    17472896 :   for (j=1; j<l; j++)
     809             :   {
     810    17384165 :     GEN P = gel(LP,j);
     811    17384165 :     long v = ZC_nfval(m, P);
     812    17383289 :     if (!v) continue;
     813     7889255 :     v -= idealval(nf,I, P);
     814     7890330 :     if (!v) continue;
     815     6752977 :     store(ip + j, v, fact); /* v = v_P(m / I) > 0 */
     816     6753012 :     k -= v * pr_get_f(P);
     817     6752928 :     if (!k) return 1;
     818             :   }
     819       88731 :   return 0;
     820             : }
     821             : 
     822             : static int
     823     5501643 : divide_p(FB_t *F, long p, long k, GEN nf, GEN I, GEN m, FACT *fact)
     824             : {
     825     5501643 :   GEN LP = gel(F->LV,p);
     826     5501643 :   long ip = F->iLP[p];
     827     5501643 :   if (!m) return divide_p_id (LP,ip,k,nf,I,fact);
     828     5338199 :   if (!I) return divide_p_elt(LP,ip,k,m,fact);
     829     5335971 :   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    19765529 : 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    19765529 :   fact[0].pr = 0;
     842    19765529 :   if (is_pm1(N)) return 1;
     843    18830328 :   if (!is_pm1(Z_ppo(N, F->prodZ))) return 0;
     844     2774922 :   f = absZ_factor(N); p = gel(f,1); e = gel(f,2); l = lg(p);
     845     8180340 :   for (i = 1; i < l; i++)
     846     5501633 :     if (!divide_p(F, itou(gel(p,i)), itou(gel(e,i)), nf, I, m, fact))
     847             :     {
     848       96368 :       if (DEBUGLEVEL > 1) err_printf(".");
     849       96368 :       return 0;
     850             :     }
     851     2678707 :   return 1;
     852             : }
     853             : 
     854             : /* can we factor m/I ? [m in I from idealpseudomin_nonscalar], NI = norm I */
     855             : static long
     856    18422774 : factorgen(FB_t *F, GEN nf, GEN I, GEN NI, GEN m, FACT *fact)
     857             : {
     858             :   long e;
     859    18422774 :   GEN Nm = embed_norm(RgM_RgC_mul(nf_get_M(nf),m), nf_get_r1(nf));
     860    18424113 :   GEN N = grndtoi(NI? divri(Nm, NI): Nm, &e); /* ~ N(m/I) */
     861    18423298 :   if (e > -32)
     862             :   {
     863           0 :     if (DEBUGLEVEL > 1) err_printf("+");
     864           0 :     return 0;
     865             :   }
     866    18423298 :   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     6591420 : addRe_modIm(GEN x, GEN a, GEN y, GEN iy)
     874             : {
     875             :   GEN z;
     876     6591420 :   if (typ(x) == t_COMPLEX)
     877             :   {
     878     4660723 :     GEN re, im = modRr_i(gel(x,2), y, iy);
     879     4660665 :     if (!im) return NULL;
     880     4660665 :     re = gadd(gel(x,1), a);
     881     4660645 :     z = gequal0(im)? re: mkcomplex(re, im);
     882             :   }
     883             :   else
     884     1930697 :     z = gadd(x, a);
     885     6591332 :   return z;
     886             : }
     887             : static GEN
     888      201383 : modIm(GEN x, GEN y, GEN iy)
     889             : {
     890      201383 :   if (typ(x) == t_COMPLEX)
     891             :   {
     892      185947 :     GEN im = modRr_i(gel(x,2), y, iy);
     893      185949 :     if (!im) return NULL;
     894      185949 :     x = gequal0(im)? gel(x,1): mkcomplex(gel(x,1), im);
     895             :   }
     896      201385 :   return x;
     897             : }
     898             : 
     899             : /* clean archimedean components. ipi = 2^n / pi (n arbitrary); its
     900             :  * exponent may be modified */
     901             : static GEN
     902     2926940 : cleanarch(GEN x, long N, GEN ipi, long prec)
     903             : {
     904             :   long i, l, R1, RU;
     905     2926940 :   GEN s, y = cgetg_copy(x, &l);
     906             : 
     907     2926943 :   if (!ipi) ipi = invr(mppi(prec));
     908     2926939 :   if (typ(x) == t_MAT)
     909             :   {
     910      524942 :     for (i = 1; i < l; i++)
     911      461108 :       if (!(gel(y,i) = cleanarch(gel(x,i), N, ipi, prec))) return NULL;
     912       63834 :     return y;
     913             :   }
     914     2863107 :   RU = l-1; R1 = (RU<<1) - N;
     915     2863107 :   s = gdivgs(RgV_sum(real_i(x)), -N); /* -log |norm(x)| / N */
     916     2863095 :   i = 1;
     917     2863095 :   if (R1)
     918             :   {
     919     2385175 :     GEN pi2 = Pi2n(1, prec);
     920     2385180 :     setexpo(ipi, -3); /* 1/(2pi) */
     921     7343339 :     for (; i <= R1; i++)
     922     4958190 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s, pi2, ipi))) return NULL;
     923             :   }
     924     2863069 :   if (i <= RU)
     925             :   {
     926     1074742 :     GEN pi4 = Pi2n(2, prec), s2 = gmul2n(s, 1);
     927     1074756 :     setexpo(ipi, -4); /* 1/(4pi) */
     928     2708004 :     for (; i <= RU; i++)
     929     1633234 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s2, pi4, ipi))) return NULL;
     930             :   }
     931     2863097 :   return y;
     932             : }
     933             : GEN
     934      194925 : nf_cxlog_normalize(GEN nf, GEN x, long prec)
     935             : {
     936      194925 :   long N = nf_get_degree(nf);
     937      194925 :   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      132696 : cleanarchunit(GEN x, long N, GEN ipi, long prec)
     944             : {
     945             :   long i, l, R1, RU;
     946      132696 :   GEN y = cgetg_copy(x, &l);
     947             : 
     948      132696 :   if (!ipi) ipi = invr(mppi(prec));
     949      132697 :   if (typ(x) == t_MAT)
     950             :   {
     951      132696 :     for (i = 1; i < l; i++)
     952       69019 :       if (!(gel(y,i) = cleanarchunit(gel(x,i), N, ipi, prec))) return NULL;
     953       63677 :     return y;
     954             :   }
     955       69019 :   if (gexpo(RgV_sum(real_i(x))) > -10) return NULL;
     956       69018 :   RU = l-1; R1 = (RU<<1) - N;
     957       69018 :   i = 1;
     958       69018 :   if (R1)
     959             :   {
     960       54612 :     GEN pi2 = Pi2n(1, prec);
     961       54614 :     setexpo(ipi, -3); /* 1/(2pi) */
     962      185610 :     for (; i <= R1; i++)
     963      130994 :       if (!(gel(y,i) = modIm(gel(x,i), pi2, ipi))) return NULL;
     964             :   }
     965       69022 :   if (i <= RU)
     966             :   {
     967       34362 :     GEN pi4 = Pi2n(2, prec);
     968       34363 :     setexpo(ipi, -4); /* 1/(4pi) */
     969      104751 :     for (; i <= RU; i++)
     970       70391 :       if (!(gel(y,i) = modIm(gel(x,i), pi4, ipi))) return NULL;
     971             :   }
     972       69020 :   return y;
     973             : }
     974             : 
     975             : static GEN
     976         389 : not_given(long reason)
     977             : {
     978         389 :   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         389 :   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     2685783 : expbitprec(GEN x, long *e)
     995             : {
     996             :   GEN re, im;
     997     2685783 :   if (typ(x) != t_COMPLEX) re = x;
     998             :   else
     999             :   {
    1000     1654609 :     im = gel(x,2); *e = maxss(*e, expo(im) + 5 - bit_prec(im));
    1001     1654609 :     re = gel(x,1);
    1002             :   }
    1003     2685783 :   return (expo(re) <= 20);
    1004             : 
    1005             : }
    1006             : static long
    1007     1167059 : RgC_expbitprec(GEN x)
    1008             : {
    1009     1167059 :   long l = lg(x), i, e = - (long)HIGHEXPOBIT;
    1010     3650990 :   for (i = 1; i < l; i++)
    1011     2484505 :     if (!expbitprec(gel(x,i), &e)) return LONG_MAX;
    1012     1166485 :   return e;
    1013             : }
    1014             : static long
    1015       48482 : RgM_expbitprec(GEN x)
    1016             : {
    1017       48482 :   long i, j, I, J, e = - (long)HIGHEXPOBIT;
    1018       48482 :   RgM_dimensions(x, &I,&J);
    1019      117432 :   for (j = 1; j <= J; j++)
    1020      270228 :     for (i = 1; i <= I; i++)
    1021      201278 :       if (!expbitprec(gcoeff(x,i,j), &e)) return LONG_MAX;
    1022       48419 :   return e;
    1023             : }
    1024             : 
    1025             : static GEN
    1026        1499 : FlxqX_chinese_unit(GEN X, GEN U, GEN invzk, GEN D, GEN T, ulong p)
    1027             : {
    1028        1499 :   long i, lU = lg(U), lX = lg(X), d = lg(invzk)-1;
    1029        1499 :   GEN M = cgetg(lU, t_MAT);
    1030        1498 :   if (D)
    1031             :   {
    1032        1389 :     D = Flv_inv(D, p);
    1033       72260 :     for (i = 1; i < lX; i++)
    1034       70870 :       if (uel(D, i) != 1)
    1035       58402 :         gel(X,i) = Flx_Fl_mul(gel(X,i), uel(D,i), p);
    1036             :   }
    1037        4118 :   for (i = 1; i < lU; i++)
    1038             :   {
    1039        2619 :     GEN H = FlxqV_factorback(X, gel(U, i), T, p);
    1040        2619 :     gel(M, i) = Flm_Flc_mul(invzk, Flx_to_Flv(H, d), p);
    1041             :   }
    1042        1499 :   return M;
    1043             : }
    1044             : 
    1045             : static GEN
    1046         297 : chinese_unit_slice(GEN A, GEN U, GEN B, GEN D, GEN C, GEN P, GEN *mod)
    1047             : {
    1048         297 :   pari_sp av = avma;
    1049         297 :   long i, n = lg(P)-1, v = varn(C);
    1050             :   GEN H, T;
    1051         297 :   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 = gerepileupto(av, Flm_to_ZM(Hp));
    1058           0 :     *mod = utoi(p);
    1059           0 :     return H;
    1060             :   }
    1061         297 :   T = ZV_producttree(P);
    1062         297 :   A = ZXC_nv_mod_tree(A, P, T, v);
    1063         297 :   B = ZM_nv_mod_tree(B, P, T);
    1064         297 :   D = D ? ZV_nv_mod_tree(D, P, T): NULL;
    1065         297 :   C = ZX_nv_mod_tree(C, P, T);
    1066             : 
    1067         297 :   H = cgetg(n+1, t_VEC);
    1068        1796 :   for(i=1; i <= n; i++)
    1069             :   {
    1070        1499 :     ulong p = P[i];
    1071        1499 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C,i), d = D ? gel(D,i): NULL;
    1072        1499 :     gel(H,i) = FlxqX_chinese_unit(a, U, b, d, c, p);
    1073             :   }
    1074         297 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
    1075         297 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    1076             : }
    1077             : 
    1078             : GEN
    1079         297 : chinese_unit_worker(GEN P, GEN A, GEN U, GEN B, GEN D, GEN C)
    1080             : {
    1081         297 :   GEN V = cgetg(3, t_VEC);
    1082         297 :   gel(V,1) = chinese_unit_slice(A, U, B, isintzero(D) ? NULL: D, C, P, &gel(V,2));
    1083         297 :   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         101 : chinese_unit(GEN nf, GEN nX, GEN dX, GEN U, ulong bnd)
    1090             : {
    1091         101 :   pari_sp av = avma;
    1092         101 :   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         101 :   GEN worker = snm_closure(is_entry("_chinese_unit_worker"),
    1096             :                mkcol5(nX, U, invzk, dX? dX: gen_0, T));
    1097         101 :   init_modular_big(&S);
    1098         101 :   H = gen_crt("chinese_units", worker, &S, f, bnd, 0, &mod, nmV_chinese_center, FpM_center);
    1099         101 :   settyp(H, t_VEC); return gerepilecopy(av, H);
    1100             : }
    1101             : 
    1102             : /* *pE a ZM */
    1103             : static void
    1104         171 : ZM_remove_unused(GEN *pE, GEN *pX)
    1105             : {
    1106         171 :   long j, k, l = lg(*pX);
    1107         171 :   GEN E = *pE, v = cgetg(l, t_VECSMALL);
    1108       16580 :   for (j = k = 1; j < l; j++)
    1109       16409 :     if (!ZMrow_equal0(E, j)) v[k++] = j;
    1110         171 :   if (k < l)
    1111             :   {
    1112         171 :     setlg(v, k);
    1113         171 :     *pX = vecpermute(*pX,v);
    1114         171 :     *pE = rowpermute(E,v);
    1115             :   }
    1116         171 : }
    1117             : 
    1118             : /* s = -log|norm(x)|/N */
    1119             : static GEN
    1120     1236075 : fixarch(GEN x, GEN s, long R1)
    1121             : {
    1122             :   long i, l;
    1123     1236075 :   GEN y = cgetg_copy(x, &l);
    1124     3422047 :   for (i = 1; i <= R1; i++) gel(y,i) = gadd(s, gel(x,i));
    1125     1736561 :   for (     ; i <   l; i++) gel(y,i) = gadd(s, gmul2n(gel(x,i),-1));
    1126     1236078 :   return y;
    1127             : }
    1128             : 
    1129             : static GEN
    1130       63679 : getfu(GEN nf, GEN *ptA, GEN *ptU, long prec)
    1131             : {
    1132       63679 :   GEN U, y, matep, A, T = nf_get_pol(nf), M = nf_get_M(nf);
    1133       63679 :   long e, j, R1, RU, N = degpol(T);
    1134             : 
    1135       63679 :   R1 = nf_get_r1(nf); RU = (N+R1) >> 1;
    1136       63679 :   if (RU == 1) return cgetg(1,t_VEC);
    1137             : 
    1138       48482 :   A = *ptA;
    1139       48482 :   matep = cgetg(RU,t_MAT);
    1140      117501 :   for (j = 1; j < RU; j++)
    1141             :   {
    1142       69020 :     GEN Aj = gel(A,j), s = gdivgs(RgV_sum(real_i(Aj)), -N);
    1143       69019 :     gel(matep,j) = fixarch(Aj, s, R1);
    1144             :   }
    1145       48481 :   U = lll(real_i(matep));
    1146       48482 :   if (lg(U) < RU) return not_given(fupb_PRECI);
    1147       48482 :   if (ptU) { *ptU = U; *ptA = A = RgM_ZM_mul(A,U); }
    1148       48482 :   y = RgM_ZM_mul(matep,U);
    1149       48482 :   e = RgM_expbitprec(y);
    1150       48482 :   if (e >= 0) return not_given(e == LONG_MAX? fupb_LARGE: fupb_PRECI);
    1151       48419 :   if (prec <= 0) prec = gprecision(A);
    1152       48419 :   y = RgM_solve_realimag(M, gexp(y,prec));
    1153       48419 :   if (!y) return not_given(fupb_PRECI);
    1154       48419 :   y = grndtoi(y, &e); if (e >= 0) return not_given(fupb_PRECI);
    1155       48101 :   settyp(y, t_VEC);
    1156             : 
    1157       48101 :   if (!ptU) *ptA = A = RgM_ZM_mul(A, U);
    1158      116386 :   for (j = 1; j < RU; j++)
    1159             :   { /* y[i] are hopefully unit generators. Normalize: smallest T2 norm */
    1160       68294 :     GEN u = gel(y,j), v = zk_inv(nf, u);
    1161       68294 :     if (!v || !is_pm1(Q_denom(v)) || ZV_isscalar(u))
    1162           8 :       return not_given(fupb_PRECI);
    1163       68285 :     if (gcmp(RgC_fpnorml2(v,DEFAULTPREC), RgC_fpnorml2(u,DEFAULTPREC)) < 0)
    1164             :     {
    1165       29457 :       gel(A,j) = RgC_neg(gel(A,j));
    1166       29457 :       if (ptU) gel(U,j) = ZC_neg(gel(U,j));
    1167       29457 :       u = v;
    1168             :     }
    1169       68286 :     gel(y,j) = nf_to_scalar_or_alg(nf, u);
    1170             :   }
    1171       48092 :   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         101 : log2fubound(GEN bnf)
    1181             : {
    1182         101 :   GEN LU = bnf_get_logfu(bnf);
    1183         101 :   long i, j, l = lg(LU), r1 = nf_get_r1(bnf_get_nf(bnf));
    1184         101 :   double e = 0.0;
    1185         344 :   for (j = 1; j < l; j++)
    1186             :   {
    1187         243 :     GEN u = gel(LU,j);
    1188         645 :     for (i = 1; i <= r1; i++)
    1189             :     {
    1190         402 :       GEN E = real_i(gel(u,i));
    1191         402 :       e = maxdd(e, gtodouble(E));
    1192             :     }
    1193         849 :     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         101 :   return e / M_LN2;
    1200             : }
    1201             : /* bound for log2(|RgM_solve_realimag(M, y)|_oo / |y|_oo)*/
    1202             : static double
    1203         101 : log2Mbound(GEN nf)
    1204             : {
    1205         101 :   GEN G = nf_get_G(nf), D = nf_get_disc(nf);
    1206         101 :   long r2 = nf_get_r2(nf), l = lg(G), i;
    1207         101 :   double e, d = dbllog2(D)/2 - r2 * M_LN2; /* log2 |det(split_realimag(M))| */
    1208         101 :   e = log2(nf_get_degree(nf));
    1209         549 :   for (i = 2; i < l; i++) e += dbllog2(gnorml2(gel(G,i))); /* Hadamard bound */
    1210         101 :   return e / 2 - d;
    1211             : }
    1212             : 
    1213             : static GEN
    1214         101 : vec_chinese_units(GEN bnf)
    1215             : {
    1216         101 :   GEN nf = bnf_get_nf(bnf), SUnits = bnf_get_sunits(bnf);
    1217         101 :   double bnd = ceil(log2Mbound(nf) + log2fubound(bnf));
    1218         101 :   GEN X, dX, Y, U, f = nf_get_index(nf);
    1219         101 :   long j, l, v = nf_get_varn(nf);
    1220         101 :   if (!SUnits) err_units(); /* no compact units */
    1221         101 :   Y = gel(SUnits,1);
    1222         101 :   U = gel(SUnits,2);
    1223         101 :   ZM_remove_unused(&U, &Y); l = lg(Y); X = cgetg(l, t_VEC);
    1224         101 :   if (is_pm1(f)) f = dX = NULL; else dX = cgetg(l, t_VEC);
    1225        5293 :   for (j = 1; j < l; j++)
    1226             :   {
    1227        5192 :     GEN t = nf_to_scalar_or_alg(nf, gel(Y,j));
    1228        5192 :     if (f)
    1229             :     {
    1230             :       GEN den;
    1231        4342 :       t = Q_remove_denom(t, &den);
    1232        4342 :       gel(dX,j) = den ? den: gen_1;
    1233             :     }
    1234        5192 :     gel(X,j) = typ(t) == t_INT? scalarpol_shallow(t,v): t;
    1235             :   }
    1236         101 :   if (dblexpo(bnd) >= BITS_IN_LONG)
    1237           0 :     pari_err_OVERFLOW("vec_chinese_units [units too large]");
    1238         101 :   return chinese_unit(nf, X, dX, U, (ulong)bnd);
    1239             : }
    1240             : 
    1241             : static GEN
    1242       24893 : makeunits(GEN bnf)
    1243             : {
    1244       24893 :   GEN nf = bnf_get_nf(bnf), fu = bnf_get_fu_nocheck(bnf);
    1245       24892 :   GEN tu = nf_to_scalar_or_basis(nf, bnf_get_tuU(bnf));
    1246       24892 :   fu = (typ(fu) == t_MAT)? vec_chinese_units(bnf): matalgtobasis(nf, fu);
    1247       24894 :   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 gerepileuptoint(av, N);
    1276             : }
    1277             : 
    1278             : /* gen: HNF ideals */
    1279             : static GEN
    1280     1161445 : get_norm_fact(GEN gen, GEN ex, GEN *pd)
    1281             : {
    1282     1161445 :   long i, c = lg(ex);
    1283             :   GEN d,N,I,e,n,ne,de;
    1284     1161445 :   d = N = gen_1;
    1285     1458013 :   for (i=1; i<c; i++)
    1286      296568 :     if (signe(gel(ex,i)))
    1287             :     {
    1288      175463 :       I = gel(gen,i); e = gel(ex,i); n = ZM_det_triangular(I);
    1289      175463 :       ne = powii(n,e);
    1290      175463 :       de = equalii(n, gcoeff(I,1,1))? ne: powii(gcoeff(I,1,1), e);
    1291      175463 :       N = mulii(N, ne);
    1292      175463 :       d = mulii(d, de);
    1293             :     }
    1294     1161445 :   *pd = d; return N;
    1295             : }
    1296             : 
    1297             : static GEN
    1298     1322336 : get_pr_lists(GEN FB, long N, int list_pr)
    1299             : {
    1300             :   GEN pr, L;
    1301     1322336 :   long i, l = lg(FB), p, pmax;
    1302             : 
    1303     1322336 :   pmax = 0;
    1304     9183911 :   for (i=1; i<l; i++)
    1305             :   {
    1306     7861575 :     pr = gel(FB,i); p = pr_get_smallp(pr);
    1307     7861575 :     if (p > pmax) pmax = p;
    1308             :   }
    1309     1322336 :   L = const_vec(pmax, NULL);
    1310     1322336 :   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     9183911 :     for (i=1; i<l; i++)
    1325             :     {
    1326     7861575 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1327     7861575 :       if (!L[p]) gel(L,p) = vecsmalltrunc_init(N+1);
    1328     7861575 :       vecsmalltrunc_append(gel(L,p), i);
    1329             :     }
    1330             :   }
    1331     1322336 :   return L;
    1332             : }
    1333             : 
    1334             : /* recover FB, LV, iLP, KCZ from Vbase */
    1335             : static GEN
    1336     1322336 : recover_partFB(FB_t *F, GEN Vbase, long N)
    1337             : {
    1338     1322336 :   GEN FB, LV, iLP, L = get_pr_lists(Vbase, N, 0);
    1339     1322336 :   long l = lg(L), p, ip, i;
    1340             : 
    1341     1322336 :   i = ip = 0;
    1342     1322336 :   FB = cgetg(l, t_VECSMALL);
    1343     1322338 :   iLP= cgetg(l, t_VECSMALL);
    1344     1322337 :   LV = cgetg(l, t_VEC);
    1345    20035283 :   for (p = 2; p < l; p++)
    1346             :   {
    1347    18712945 :     if (!L[p]) continue;
    1348     4304357 :     FB[++i] = p;
    1349     4304357 :     gel(LV,p) = vecpermute(Vbase, gel(L,p));
    1350     4304358 :     iLP[p]= ip; ip += lg(gel(L,p))-1;
    1351             :   }
    1352     1322338 :   F->KCZ = i;
    1353     1322338 :   F->KC = ip;
    1354     1322338 :   F->FB = FB; setlg(FB, i+1);
    1355     1322338 :   F->prodZ = zv_prod_Z(F->FB);
    1356     1322334 :   F->LV = LV;
    1357     1322334 :   F->iLP= iLP; return L;
    1358             : }
    1359             : 
    1360             : /* add v^e to factorization */
    1361             : static void
    1362     2866868 : add_to_fact(long v, long e, FACT *fact)
    1363             : {
    1364     2866868 :   long i, n = fact[0].pr;
    1365     9700211 :   for (i=1; i<=n; i++)
    1366     7369380 :     if (fact[i].pr == v) { fact[i].ex += e; return; }
    1367     2330831 :   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        3335 : pr_index(GEN L, GEN pr)
    1379             : {
    1380        3335 :   long j, l = lg(L);
    1381        3335 :   GEN al = pr_get_gen(pr);
    1382        3335 :   for (j=1; j<l; j++)
    1383        3335 :     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        3335 : Vbase_to_FB(FB_t *F, GEN pr)
    1390             : {
    1391        3335 :   long p = pr_get_smallp(pr);
    1392        3335 :   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        3483 : idealHNF_mulred(GEN nf, GEN x, GEN y)
    1399             : {
    1400        3483 :   GEN A = idealHNF_mul(nf, gel(x,1), gel(y,1));
    1401        3483 :   GEN F = famat_mul_shallow(gel(x,2), gel(y,2));
    1402        3483 :   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        4474 : idealmulpowprime2(GEN nf, GEN x, GEN pr, ulong n)
    1408             : {
    1409        4474 :   GEN A = idealmulpowprime(nf, gel(x,1), pr, utoipos(n));
    1410        4474 :   return mkvec2(A, gel(x,2));
    1411             : }
    1412             : static GEN
    1413       65420 : init_famat(GEN x) { return mkvec2(x, trivial_fact()); }
    1414             : /* optimized idealfactorback + reduction; z = init_famat() */
    1415             : static GEN
    1416       28784 : genback(GEN z, GEN nf, GEN P, GEN E)
    1417             : {
    1418       28784 :   long i, l = lg(E);
    1419       28784 :   GEN I = NULL;
    1420       76595 :   for (i = 1; i < l; i++)
    1421       47811 :     if (signe(gel(E,i)))
    1422             :     {
    1423             :       GEN J;
    1424       32267 :       gel(z,1) = gel(P,i);
    1425       32267 :       J = idealpowred(nf, z, gel(E,i));
    1426       32267 :       I = I? idealHNF_mulred(nf, I, J): J;
    1427             :     }
    1428       28784 :   return I; /* != NULL since a generator */
    1429             : }
    1430             : 
    1431             : static GEN
    1432     1204391 : SPLIT_i(FB_t *F, GEN nf, GEN G, GEN x, GEN xred, GEN Nx, FACT *fact)
    1433             : {
    1434     1204391 :   pari_sp av = avma;
    1435     1204391 :   GEN L = idealpseudominvec(xred, G);
    1436     1204390 :   long k, l = lg(L);
    1437     1287280 :   for(k = 1; k < l; k++)
    1438     1271375 :     if (factorgen(F, nf, x, Nx, gel(L,k), fact)) return gel(L,k);
    1439       15905 :   return gc_NULL(av);
    1440             : }
    1441             : /* return famat y (principal ideal) such that y / x is smooth [wrt Vbase] */
    1442             : static GEN
    1443     1338678 : SPLIT(FB_t *F, GEN nf, GEN x, GEN Vbase, FACT *fact)
    1444             : {
    1445     1338678 :   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     2677165 :   if (expi(gcoeff(x,1,1)) < 100 &&
    1451     1488681 :       can_factor(F, nf, x, NULL, Nx, fact)) return NULL;
    1452     1188484 :   if ((y = SPLIT_i(F, nf, nf_get_roundG(nf), x, x, Nx, fact))) return y;
    1453             : 
    1454             :   /* reduce in various directions */
    1455        8789 :   ru = lg(nf_get_roots(nf));
    1456        8789 :   vecG = cgetg(ru, t_VEC);
    1457       14324 :   for (j=1; j<ru; j++)
    1458             :   {
    1459       12583 :     gel(vecG,j) = nf_get_Gtwist1(nf, j);
    1460       12583 :     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        1741 :   lgsub = 3; nbtest = 1; nbtest_lim = 4;
    1465        1741 :   ex = cgetg(lgsub, t_VECSMALL);
    1466        1741 :   x0 = init_famat(x);
    1467             :   for(;;)
    1468         594 :   {
    1469        2335 :     GEN Ired, I, NI, id = x0;
    1470        2335 :     av = avma;
    1471        2335 :     if (DEBUGLEVEL>2) err_printf("# ideals tried = %ld\n",nbtest);
    1472        7117 :     for (i=1; i<lgsub; i++)
    1473             :     {
    1474        4782 :       ex[i] = random_bits(RANDOM_BITS);
    1475        4782 :       if (ex[i]) id = idealmulpowprime2(nf, id, gel(Vbase,i), ex[i]);
    1476             :     }
    1477        2335 :     if (id == x0) continue;
    1478             :     /* I^(-1) * \prod Vbase[i]^ex[i] = (id[2]) / x */
    1479             : 
    1480        2314 :     I = gel(id,1); NI = ZM_det_triangular(I);
    1481        2314 :     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        2314 :     Ired = ru == 2? I: ZM_lll(I, 0.99, LLL_INPLACE);
    1489        3896 :     for (j=1; j<ru; j++)
    1490        3323 :       if ((y = SPLIT_i(F, nf, gel(vecG,j), I, Ired, NI, fact)))
    1491             :       {
    1492        5244 :         for (i=1; i<lgsub; i++)
    1493        3503 :           if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1494        1741 :         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     1327249 : bnf_get_W(GEN bnf) { return gel(bnf,1); }
    1513             : INLINE GEN
    1514     2644561 : bnf_get_B(GEN bnf) { return gel(bnf,2); }
    1515             : INLINE GEN
    1516     2679066 : bnf_get_C(GEN bnf) { return gel(bnf,4); }
    1517             : INLINE GEN
    1518     1322356 : bnf_get_vbase(GEN bnf) { return gel(bnf,5); }
    1519             : INLINE GEN
    1520     1322270 : bnf_get_Ur(GEN bnf) { return gmael(bnf,9,1); }
    1521             : INLINE GEN
    1522      271829 : bnf_get_ga(GEN bnf) { return gmael(bnf,9,2); }
    1523             : INLINE GEN
    1524      276785 : 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     1322273 : split_ideal(GEN bnf, GEN x, GEN *pWex, GEN *pBex)
    1531             : {
    1532     1322273 :   GEN L, y, Vbase = bnf_get_vbase(bnf);
    1533     1322273 :   GEN Wex, W  = bnf_get_W(bnf);
    1534     1322273 :   GEN Bex, B  = bnf_get_B(bnf);
    1535             :   long p, j, i, l, nW, nB;
    1536             :   FACT *fact;
    1537             :   FB_t F;
    1538             : 
    1539     1322273 :   L = recover_partFB(&F, Vbase, lg(x)-1);
    1540     1322271 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    1541     1322270 :   y = SPLIT(&F, bnf_get_nf(bnf), x, Vbase, fact);
    1542     1322272 :   nW = lg(W)-1; *pWex = Wex = zero_zv(nW);
    1543     1322272 :   nB = lg(B)-1; *pBex = Bex = zero_zv(nB); l = lg(F.FB);
    1544     1322272 :   p = j = 0; /* -Wall */
    1545     1969850 :   for (i = 1; i <= fact[0].pr; i++)
    1546             :   { /* decode index C = ip+j --> (p,j) */
    1547      647578 :     long a, b, t, C = fact[i].pr;
    1548     1826356 :     for (t = 1; t < l; t++)
    1549             :     {
    1550     1753130 :       long q = F.FB[t], k = C - F.iLP[q];
    1551     1753130 :       if (k <= 0) break;
    1552     1178778 :       p = q;
    1553     1178778 :       j = k;
    1554             :     }
    1555      647578 :     a = gel(L, p)[j];
    1556      647578 :     b = a - nW;
    1557      647578 :     if (b <= 0) Wex[a] = y? -fact[i].ex: fact[i].ex;
    1558      494046 :     else        Bex[b] = y? -fact[i].ex: fact[i].ex;
    1559             :   }
    1560     1322272 :   return y;
    1561             : }
    1562             : 
    1563             : GEN
    1564     1039948 : init_red_mod_units(GEN bnf, long prec)
    1565             : {
    1566     1039948 :   GEN s = gen_0, p1,s1,mat, logfu = bnf_get_logfu(bnf);
    1567     1039948 :   long i,j, RU = lg(logfu);
    1568             : 
    1569     1039948 :   if (RU == 1) return NULL;
    1570     1039948 :   mat = cgetg(RU,t_MAT);
    1571     2357983 :   for (j=1; j<RU; j++)
    1572             :   {
    1573     1318034 :     p1 = cgetg(RU+1,t_COL); gel(mat,j) = p1;
    1574     1318034 :     s1 = gen_0;
    1575     3263086 :     for (i=1; i<RU; i++)
    1576             :     {
    1577     1945052 :       gel(p1,i) = real_i(gcoeff(logfu,i,j));
    1578     1945052 :       s1 = mpadd(s1, mpsqr(gel(p1,i)));
    1579             :     }
    1580     1318034 :     gel(p1,RU) = gen_0; if (mpcmp(s1,s) > 0) s = s1;
    1581             :   }
    1582     1039949 :   s = gsqrt(gmul2n(s,RU),prec);
    1583     1039949 :   if (expo(s) < 27) s = utoipos(1UL << 27);
    1584     1039948 :   return mkvec2(mat, s);
    1585             : }
    1586             : 
    1587             : /* z computed above. Return unit exponents that would reduce col (arch) */
    1588             : GEN
    1589     1039949 : red_mod_units(GEN col, GEN z)
    1590             : {
    1591             :   long i,RU;
    1592             :   GEN x,mat,N2;
    1593             : 
    1594     1039949 :   if (!z) return NULL;
    1595     1039949 :   mat= gel(z,1);
    1596     1039949 :   N2 = gel(z,2);
    1597     1039949 :   RU = lg(mat); x = cgetg(RU+1,t_COL);
    1598     2357983 :   for (i=1; i<RU; i++) gel(x,i) = real_i(gel(col,i));
    1599     1039949 :   gel(x,RU) = N2;
    1600     1039949 :   x = lll(shallowconcat(mat,x));
    1601     1039947 :   if (typ(x) != t_MAT || lg(x) <= RU) return NULL;
    1602     1039947 :   x = gel(x,RU);
    1603     1039947 :   if (signe(gel(x,RU)) < 0) x = gneg_i(x);
    1604     1039947 :   if (!gequal1(gel(x,RU))) pari_err_BUG("red_mod_units");
    1605     1039947 :   setlg(x,RU); return x;
    1606             : }
    1607             : 
    1608             : static GEN
    1609     2127848 : 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     1986256 : act_arch(GEN A, GEN x)
    1614             : {
    1615             :   GEN a;
    1616     1986256 :   long i,l = lg(A), tA = typ(A);
    1617     1986256 :   if (tA == t_MAT)
    1618             :   { /* assume lg(x) >= l */
    1619      191326 :     a = cgetg(l, t_MAT);
    1620      281155 :     for (i=1; i<l; i++) gel(a,i) = act_arch(gel(A,i), x);
    1621      191327 :     return a;
    1622             :   }
    1623     1794930 :   if (l==1) return cgetg(1, t_COL);
    1624     1794930 :   a = NULL;
    1625     1794930 :   if (tA == t_VECSMALL)
    1626             :   {
    1627     6834196 :     for (i=1; i<l; i++)
    1628             :     {
    1629     5672751 :       long c = A[i];
    1630     5672751 :       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     1383458 :     for (i=1; i<l; i++)
    1636             :     {
    1637      749973 :       GEN c = gel(A,i);
    1638      749973 :       if (signe(c)) a = add(a, gmul(c, gel(x,i)));
    1639             :     }
    1640             :   }
    1641     1794930 :   return a? a: zerocol(lgcols(x)-1);
    1642             : }
    1643             : /* act_arch(matdiagonal(v), x) */
    1644             : static GEN
    1645       63776 : diagact_arch(GEN v, GEN x)
    1646             : {
    1647       63776 :   long i, l = lg(v);
    1648       63776 :   GEN a = cgetg(l, t_MAT);
    1649       92630 :   for (i = 1; i < l; i++) gel(a,i) = gmul(gel(x,i), gel(v,i));
    1650       63776 :   return a;
    1651             : }
    1652             : 
    1653             : static long
    1654     1340423 : prec_arch(GEN bnf)
    1655             : {
    1656     1340423 :   GEN a = bnf_get_C(bnf);
    1657     1340421 :   long i, l = lg(a), prec;
    1658             : 
    1659     1340420 :   for (i=1; i<l; i++)
    1660     1340337 :     if ( (prec = gprecision(gel(a,i))) ) return prec;
    1661          83 :   return DEFAULTPREC;
    1662             : }
    1663             : 
    1664             : static long
    1665        3831 : needed_bitprec(GEN x)
    1666             : {
    1667        3831 :   long i, e = 0, l = lg(x);
    1668       22386 :   for (i = 1; i < l; i++)
    1669             :   {
    1670       18555 :     GEN c = gel(x,i);
    1671       18555 :     long f = gexpo(c) - gprecision(c);
    1672       18555 :     if (f > e) e = f;
    1673             :   }
    1674        3831 :   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     1167060 : isprincipalarch(GEN bnf, GEN col, GEN kNx, GEN e, GEN dx, long *pe)
    1681             : {
    1682             :   GEN nf, x, y, logfu, s, M;
    1683     1167060 :   long N, prec = gprecision(col);
    1684     1167059 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf); M = nf_get_M(nf);
    1685     1167059 :   if (!prec) prec = prec_arch(bnf);
    1686     1167059 :   *pe = 128;
    1687     1167059 :   logfu = bnf_get_logfu(bnf);
    1688     1167059 :   N = nf_get_degree(nf);
    1689     1167059 :   if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
    1690     1167053 :   if (lg(col) > 2)
    1691             :   { /* reduce mod units */
    1692     1039948 :     GEN u, z = init_red_mod_units(bnf,prec);
    1693     1039949 :     if (!(u = red_mod_units(col,z))) return NULL;
    1694     1039947 :     col = RgC_add(col, RgM_RgC_mul(logfu, u));
    1695     1039949 :     if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
    1696             :   }
    1697     1167052 :   s = divru(mulir(e, glog(kNx,prec)), N);
    1698     1167056 :   col = fixarch(col, s, nf_get_r1(nf));
    1699     1167059 :   if (RgC_expbitprec(col) >= 0) return NULL;
    1700     1166485 :   col = gexp(col, prec);
    1701             :   /* d.alpha such that x = alpha \prod gj^ej */
    1702     1166485 :   x = RgM_solve_realimag(M,col); if (!x) return NULL;
    1703     1166487 :   x = RgC_Rg_mul(x, dx);
    1704     1166486 :   y = grndtoi(x, pe);
    1705     1166481 :   if (*pe > -5) { *pe = needed_bitprec(x); return NULL; }
    1706     1162650 :   return RgC_Rg_div(y, dx);
    1707             : }
    1708             : 
    1709             : /* y = C \prod g[i]^e[i] ? */
    1710             : static int
    1711     1158576 : fact_ok(GEN nf, GEN y, GEN C, GEN g, GEN e)
    1712             : {
    1713     1158576 :   pari_sp av = avma;
    1714     1158576 :   long i, c = lg(e);
    1715     1158576 :   GEN z = C? C: gen_1;
    1716     1435669 :   for (i=1; i<c; i++)
    1717      277092 :     if (signe(gel(e,i))) z = idealmul(nf, z, idealpow(nf, gel(g,i), gel(e,i)));
    1718     1158577 :   if (typ(z) != t_MAT) z = idealhnf_shallow(nf,z);
    1719     1158579 :   if (typ(y) != t_MAT) y = idealhnf_shallow(nf,y);
    1720     1158579 :   return gc_bool(av, ZM_equal(y,z));
    1721             : }
    1722             : static GEN
    1723     1322272 : ZV_divrem(GEN A, GEN B, GEN *pR)
    1724             : {
    1725     1322272 :   long i, l = lg(A);
    1726     1322272 :   GEN Q = cgetg(l, t_COL), R = cgetg(l, t_COL);
    1727     1828548 :   for (i = 1; i < l; i++) gel(Q,i) = truedvmdii(gel(A,i), gel(B,i), &gel(R,i));
    1728     1322273 :   *pR = R; return Q;
    1729             : }
    1730             : 
    1731             : static GEN
    1732     1322270 : Ur_ZC_mul(GEN bnf, GEN v)
    1733             : {
    1734     1322270 :   GEN w, U = bnf_get_Ur(bnf);
    1735     1322270 :   long i, l = lg(bnf_get_cyc(bnf)); /* may be < lgcols(U) */
    1736             : 
    1737     1322270 :   w = cgetg(l, t_COL);
    1738     1828547 :   for (i = 1; i < l; i++) gel(w,i) = ZMrow_ZC_mul(U, v, i);
    1739     1322272 :   return w;
    1740             : }
    1741             : 
    1742             : static GEN
    1743        7277 : ZV_mul(GEN x, GEN y)
    1744             : {
    1745        7277 :   long i, l = lg(x);
    1746        7277 :   GEN z = cgetg(l, t_COL);
    1747       31768 :   for (i = 1; i < l; i++) gel(z,i) = mulii(gel(x,i), gel(y,i));
    1748        7277 :   return z;
    1749             : }
    1750             : static int
    1751     1158027 : dump_gen(GEN SUnits, GEN x, long flag)
    1752             : {
    1753             :   GEN d;
    1754             :   long e;
    1755     1158027 :   if (!(flag & nf_GENMAT) || !SUnits) return 0;
    1756      266423 :   e = gexpo(gel(SUnits,2)); if (e > 64) return 0; /* U large */
    1757      266329 :   x = Q_remove_denom(x, &d);
    1758      266326 :   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     1338625 : isprincipalall(GEN bnf, GEN x, long *pprec, long flag)
    1765             : {
    1766             :   GEN xar, Wex, Bex, gen, xc, col, A, Q, R, UA, SUnits;
    1767     1338625 :   GEN C = bnf_get_C(bnf), nf = bnf_get_nf(bnf), cyc = bnf_get_cyc(bnf);
    1768             :   long nB, nW, e;
    1769             : 
    1770     1338624 :   if (lg(cyc) == 1 && !(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL)))
    1771        4725 :     return cgetg(1,t_COL);
    1772     1333899 :   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     1333815 :   x = Q_primitive_part(x, &xc);
    1780     1333814 :   if (equali1(gcoeff(x,1,1))) /* trivial ideal */
    1781             :   {
    1782       11541 :     R = zerocol(lg(cyc)-1);
    1783       11541 :     if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1784       11492 :     if (flag & nf_GEN_IF_PRINCIPAL)
    1785        6466 :       return scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
    1786        5026 :     if (flag & nf_GENMAT)
    1787        2191 :       col = xc? Q_to_famat(xc): trivial_fact();
    1788             :     else
    1789        2835 :       col = scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
    1790        5026 :     return mkvec2(R, col);
    1791             :   }
    1792     1322273 :   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     1322272 :   A = zc_to_ZC(Wex); nB = lg(Bex)-1;
    1795     1322270 :   if (nB) A = ZC_sub(A, ZM_zc_mul(bnf_get_B(bnf), Bex));
    1796     1322270 :   UA = Ur_ZC_mul(bnf, A);
    1797     1322272 :   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     1322273 :   if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1801     1162061 :   if ((flag & nf_GEN_IF_PRINCIPAL) && !ZV_equal0(R)) return gen_0;
    1802             : 
    1803     1162054 :   nW = lg(Wex)-1;
    1804     1162054 :   gen = bnf_get_gen(bnf);
    1805     1162054 :   col = NULL;
    1806     1162054 :   SUnits = bnf_get_sunits(bnf);
    1807     1162053 :   if (lg(R) == 1
    1808      272438 :       || abscmpiu(gel(R,vecindexmax(R)), 4 * (*pprec)) < 0)
    1809             :   { /* q = N (x / prod gj^ej) = N(alpha), denom(alpha) | d */
    1810     1161444 :     GEN d, q = gdiv(ZM_det_triangular(x), get_norm_fact(gen, R, &d));
    1811     1161445 :     col = xar? nf_cxlog(nf, xar, *pprec): NULL;
    1812     1161446 :     if (nB) col = add(col, act_arch(Bex, nW? vecslice(C,nW+1,lg(C)-1): C));
    1813     1161448 :     if (nW) col = add(col, RgC_sub(act_arch(Q, bnf_get_GD(bnf)),
    1814             :                                    act_arch(A, bnf_get_ga(bnf))));
    1815     1161448 :     col = isprincipalarch(bnf, col, q, gen_1, d, &e);
    1816     1161444 :     if (col && (dump_gen(SUnits, col, flag)
    1817     1158027 :                 || !fact_ok(nf,x, col,gen,R))) col = NULL;
    1818             :   }
    1819     1162055 :   if (!col && (flag & nf_GENMAT))
    1820             :   {
    1821        8018 :     if (SUnits)
    1822             :     {
    1823        7529 :       GEN X = gel(SUnits,1), U = gel(SUnits,2), C = gel(SUnits,3);
    1824        7529 :       GEN v = gel(bnf,9), Ge = gel(v,4), M1 = gel(v,5), M2 = gel(v,6);
    1825        7529 :       GEN z = NULL, F = NULL;
    1826        7529 :       if (nB)
    1827             :       {
    1828        7529 :         GEN C2 = nW? vecslice(C, nW+1, lg(C)-1): C;
    1829        7529 :         z = ZM_zc_mul(C2, Bex);
    1830             :       }
    1831        7529 :       if (nW)
    1832             :       { /* [GD]Q - [ga]A = ([X]M1 - [Ge]D) Q - ([X]M2 - [Ge]Ur) A */
    1833        7277 :         GEN C1 = vecslice(C, 1, nW);
    1834        7277 :         GEN v = ZC_sub(ZM_ZC_mul(M1,Q), ZM_ZC_mul(M2,A));
    1835        7277 :         z = add(z, ZM_ZC_mul(C1, v));
    1836        7277 :         F = famat_reduce(famatV_factorback(Ge, ZC_sub(UA, ZV_mul(cyc,Q))));
    1837        7277 :         if (lgcols(F) == 1) F = NULL;
    1838             :       }
    1839             :       /* reduce modulo units and Q^* */
    1840        7529 :       if (lg(U) != 1) z = ZC_sub(z, ZM_ZC_mul(U, RgM_Babai(U,z)));
    1841        7529 :       col = mkmat2(X, z);
    1842        7529 :       if (F) col = famat_mul_shallow(col, F);
    1843        7529 :       col = famat_remove_trivial(col);
    1844        7529 :       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     1162055 :   if (col)
    1854             :   { /* add back missing content */
    1855     1161965 :     if (typ(col) == t_MAT)
    1856        8012 :     { if (xc) col = famat_mul_shallow(col, xc); }
    1857     1153953 :     else if (flag & nf_GENMAT)
    1858             :     {
    1859             :       GEN c;
    1860     1140254 :       if (RgV_isscalar(col))
    1861        3659 :         col = Q_to_famat(mul_content(xc, gel(col,1)));
    1862             :       else
    1863             :       {
    1864     1136595 :         col = Q_primitive_part(col, &c);
    1865     1136594 :         col = to_famat_shallow(col, gen_1);
    1866     1136595 :         xc = mul_content(xc, c);
    1867     1136594 :         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     1161978 :   return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(R, col);
    1887             : }
    1888             : 
    1889             : static GEN
    1890      462798 : triv_gen(GEN bnf, GEN x, long flag)
    1891             : {
    1892      462798 :   pari_sp av = avma;
    1893      462798 :   GEN nf = bnf_get_nf(bnf);
    1894             :   long c;
    1895      462798 :   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 gerepilecopy(av, to_famat_shallow(x, gen_1));
    1901             :   }
    1902      462791 :   c = lg(bnf_get_cyc(bnf)) - 1;
    1903      462791 :   if (flag & nf_GENMAT)
    1904      453187 :     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     1769197 : bnfisprincipal0(GEN bnf,GEN x,long flag)
    1912             : {
    1913     1769197 :   pari_sp av = avma;
    1914             :   GEN c, nf;
    1915             :   long pr;
    1916             : 
    1917     1769197 :   bnf = checkbnf(bnf);
    1918     1769197 :   nf = bnf_get_nf(bnf);
    1919     1769197 :   switch( idealtyp(&x, NULL) )
    1920             :   {
    1921       57386 :     case id_PRINCIPAL:
    1922       57386 :       if (gequal0(x)) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1923       57386 :       return triv_gen(bnf, x, flag);
    1924     1688145 :     case id_PRIME:
    1925     1688145 :       if (pr_is_inert(x)) return triv_gen(bnf, pr_get_p(x), flag);
    1926     1282740 :       x = pr_hnf(nf, x);
    1927     1282736 :       break;
    1928       23667 :     case id_MAT:
    1929       23667 :       if (lg(x)==1) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1930       23667 :       if (nf_get_degree(nf) != lg(x)-1)
    1931           0 :         pari_err_TYPE("idealtyp [dimension != degree]", x);
    1932             :   }
    1933     1306402 :   pr = prec_arch(bnf); /* precision of unit matrix */
    1934     1306400 :   c = getrand();
    1935             :   for (;;)
    1936           6 :   {
    1937     1306410 :     pari_sp av1 = avma;
    1938     1306410 :     GEN y = isprincipalall(bnf,x,&pr,flag);
    1939     1306410 :     if (y) return gerepilecopy(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      174513 : 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       32143 : add_principal_part(GEN nf, GEN u, GEN v, long flag)
    1964             : {
    1965       32143 :   if (flag & nf_GENMAT)
    1966       14533 :     return (typ(u) == t_COL && RgV_is1(u))? v: famat_mul_shallow(v,u);
    1967             :   else
    1968       17610 :     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       32166 : expandext(GEN nf, GEN C, GEN P, GEN e)
    2035             : {
    2036       32166 :   long i, l = lg(e);
    2037       32166 :   GEN B, A = gel(C,1), C1 = A;
    2038       95170 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    2039       63004 :     if (signe(gel(e,i)))
    2040             :     {
    2041       35195 :       gel(C,1) = gel(P,i);
    2042       35195 :       B = idealpowred(nf, C, gel(e,i));
    2043       35195 :       A = A? idealmulred(nf,A,B): B;
    2044             :     }
    2045       32166 :   return A == C1? C: A;
    2046             : }
    2047             : 
    2048             : /* isprincipal for C * \prod P[i]^e[i] (C omitted if NULL) */
    2049             : GEN
    2050       32166 : isprincipalfact(GEN bnf, GEN C, GEN P, GEN e, long flag)
    2051             : {
    2052       32166 :   const long gen = flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL);
    2053             :   long prec;
    2054       32166 :   pari_sp av = avma;
    2055       32166 :   GEN C0, Cext, c, id, nf = bnf_get_nf(bnf);
    2056             : 
    2057       32166 :   if (gen)
    2058             :   {
    2059       14540 :     Cext = (flag & nf_GENMAT)? trivial_fact()
    2060       32166 :                              : mkpolmod(gen_1,nf_get_pol(nf));
    2061       32166 :     C0 = mkvec2(C, Cext);
    2062       32166 :     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       32166 :   if (id == C0) /* e = 0 */
    2069             :   {
    2070       12470 :     if (!C) return bnfisprincipal0(bnf, gen_1, flag);
    2071       12456 :     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       12449 :     C = idealhnf_shallow(nf,C);
    2077             :   }
    2078             :   else
    2079             :   {
    2080       19696 :     if (gen) { C = gel(id,1); Cext = gel(id,2); } else C = id;
    2081             :   }
    2082       32145 :   prec = prec_arch(bnf);
    2083       32144 :   c = getrand();
    2084             :   for (;;)
    2085          70 :   {
    2086       32215 :     pari_sp av1 = avma;
    2087       32215 :     GEN y = isprincipalall(bnf, C, &prec, flag);
    2088       32213 :     if (y)
    2089             :     {
    2090       32143 :       if (flag & nf_GEN_IF_PRINCIPAL)
    2091             :       {
    2092       20816 :         if (typ(y) == t_INT) return gc_NULL(av);
    2093       20816 :         y = add_principal_part(nf, y, Cext, flag);
    2094             :       }
    2095             :       else
    2096             :       {
    2097       11327 :         GEN u = gel(y,2);
    2098       11327 :         if (!gen || typ(y) != t_VEC) return gerepileupto(av,y);
    2099       11327 :         if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    2100             :       }
    2101       32143 :       return gerepilecopy(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 gerepilecopy(av, y);
    2129             : }
    2130             : 
    2131             : GEN
    2132      148688 : nfsign_from_logarch(GEN LA, GEN invpi, GEN archp)
    2133             : {
    2134      148688 :   long l = lg(archp), i;
    2135      148688 :   GEN y = cgetg(l, t_VECSMALL);
    2136      148689 :   pari_sp av = avma;
    2137             : 
    2138      279416 :   for (i=1; i<l; i++)
    2139             :   {
    2140      130728 :     GEN c = ground( gmul(imag_i(gel(LA,archp[i])), invpi) );
    2141      130726 :     y[i] = mpodd(c)? 1: 0;
    2142             :   }
    2143      148688 :   set_avma(av); return y;
    2144             : }
    2145             : 
    2146             : GEN
    2147      227040 : nfsign_tu(GEN bnf, GEN archp)
    2148             : {
    2149             :   long n;
    2150      227040 :   if (bnf_get_tuN(bnf) != 2) return cgetg(1, t_VECSMALL);
    2151      159944 :   n = archp? lg(archp) - 1: nf_get_r1(bnf_get_nf(bnf));
    2152      159944 :   return const_vecsmall(n, 1);
    2153             : }
    2154             : GEN
    2155      228220 : nfsign_fu(GEN bnf, GEN archp)
    2156             : {
    2157      228220 :   GEN invpi, y, A = bnf_get_logfu(bnf), nf = bnf_get_nf(bnf);
    2158      228216 :   long j = 1, RU = lg(A);
    2159             : 
    2160      228216 :   if (!archp) archp = identity_perm( nf_get_r1(nf) );
    2161      228216 :   invpi = invr( mppi(nf_get_prec(nf)) );
    2162      228207 :   y = cgetg(RU,t_MAT);
    2163      376818 :   for (j = 1; j < RU; j++)
    2164      148590 :     gel(y,j) = nfsign_from_logarch(gel(A,j), invpi, archp);
    2165      228228 :   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      722670 : get_log_embed(REL_t *rel, GEN M, long RU, long R1, long prec)
    2196             : {
    2197      722670 :   GEN arch, C, z = rel->m;
    2198             :   long i;
    2199      722670 :   arch = typ(z) == t_COL? RgM_RgC_mul(M, z): const_col(nbrows(M), z);
    2200      722669 :   C = cgetg(RU+1, t_COL); arch = glog(arch, prec);
    2201     1633124 :   for (i=1; i<=R1; i++) gel(C,i) = gel(arch,i);
    2202     1562541 :   for (   ; i<=RU; i++) gel(C,i) = gmul2n(gel(arch,i), 1);
    2203      722665 :   return C;
    2204             : }
    2205             : static GEN
    2206      997581 : 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      997581 :   if (!rel->relaut) return get_log_embed(rel, M, RU, R1, prec);
    2212             :   /* image of another relation by automorphism */
    2213      274912 :   C = gel(embs, ind - rel->relorig);
    2214      274912 :   perm = gel(F->embperm, rel->relaut);
    2215      274912 :   D = cgetg_copy(C, &n);
    2216     1163907 :   for (i = 1; i < n; i++)
    2217             :   {
    2218      888992 :     long v = perm[i];
    2219      888992 :     gel(D,i) = (v > 0)? gel(C,v): conj_i(gel(C,-v));
    2220             :   }
    2221      274915 :   return D;
    2222             : }
    2223             : static GEN
    2224       95084 : get_embs(FB_t *F, RELCACHE_t *cache, GEN nf, GEN embs, long PREC)
    2225             : {
    2226       95084 :   long ru, j, k, l = cache->last - cache->chk + 1, r1 = nf_get_r1(nf);
    2227       95084 :   GEN M = nf_get_M(nf), nembs = cgetg(cache->last - cache->base+1, t_MAT);
    2228             :   REL_t *rel;
    2229             : 
    2230     1222897 :   for (k = 1; k <= cache->chk - cache->base; k++) gel(nembs,k) = gel(embs,k);
    2231       95084 :   embs = nembs; ru = nbrows(M);
    2232     1080088 :   for (j=1,rel = cache->chk + 1; j < l; rel++,j++,k++)
    2233      985005 :     gel(embs,k) = rel_embed(rel, F, embs, k, M, ru, r1, PREC);
    2234       95083 :   return embs;
    2235             : }
    2236             : static void
    2237      929007 : set_rel_alpha(REL_t *rel, GEN auts, GEN vA, long ind)
    2238             : {
    2239             :   GEN u;
    2240      929007 :   if (!rel->relaut)
    2241      671368 :     u = rel->m;
    2242             :   else
    2243      257639 :     u = ZM_ZC_mul(gel(auts, rel->relaut), gel(vA, ind - rel->relorig));
    2244      929004 :   gel(vA, ind) = u;
    2245      929004 : }
    2246             : static GEN
    2247     2275254 : set_fact(FB_t *F, FACT *fact, GEN e, long *pnz)
    2248             : {
    2249     2275254 :   long i, n = fact[0].pr, nz = F->KC + 1;
    2250     2275254 :   GEN c = zero_Flv(F->KC);
    2251    10657720 :   for (i = 1; i <= n; i++)
    2252             :   {
    2253     8382460 :     long p = fact[i].pr;
    2254     8382460 :     if (p < nz) nz = p;
    2255     8382460 :     c[p] = fact[i].ex;
    2256             :   }
    2257     2275260 :   if (e)
    2258             :   {
    2259       12912 :     long l = lg(e);
    2260       46493 :     for (i = 1; i < l; i++)
    2261       33581 :       if (e[i]) { long v = F->subFB[i]; c[v] += e[i]; if (v < nz) nz = v; }
    2262             :   }
    2263     2275260 :   *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     2920501 : already_known(RELCACHE_t *cache, long bs, GEN cols)
    2270             : {
    2271             :   REL_t *r;
    2272     2920501 :   long l = lg(cols);
    2273   214174952 :   for (r = cache->last; r > cache->base; r--)
    2274   211782083 :     if (bs == r->nz)
    2275             :     {
    2276    34049911 :       GEN coll = r->R;
    2277    34049911 :       long b = bs;
    2278   121793041 :       while (b < l && cols[b] == coll[b]) b++;
    2279    34049911 :       if (b == l) return 1;
    2280             :     }
    2281     2392869 :   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     2924609 : 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     2924609 :   long i, k, n = lg(R)-1;
    2292             : 
    2293     2924609 :   if (nz == n+1) { k = 0; goto ADD_REL; }
    2294     2920497 :   if (already_known(cache, nz, R)) return -1;
    2295     2392913 :   if (cache->last >= cache->base + cache->len) return 0;
    2296     2392913 :   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     2392930 :   if (cache->missing)
    2302             :   {
    2303     2020143 :     GEN a = leafcopy(R), basis = cache->basis;
    2304     2020140 :     k = lg(a);
    2305   124180262 :     do --k; while (!a[k]);
    2306     7079860 :     while (k)
    2307             :     {
    2308     5525276 :       GEN c = gel(basis, k);
    2309     5525276 :       if (c[k])
    2310             :       {
    2311     5059720 :         long ak = a[k];
    2312   262064218 :         for (i=1; i < k; i++) if (c[i]) a[i] = (a[i] + ak*(mod_p-c[i])) % mod_p;
    2313     5059720 :         a[k] = 0;
    2314   128727571 :         do --k; while (!a[k]); /* k cannot go below 0: codeword is a sentinel */
    2315             :       }
    2316             :       else
    2317             :       {
    2318      465556 :         ulong invak = Fl_inv(uel(a,k), mod_p);
    2319             :         /* Cleanup a */
    2320    13774449 :         for (i = k; i-- > 1; )
    2321             :         {
    2322    13308892 :           long j, ai = a[i];
    2323    13308892 :           c = gel(basis, i);
    2324    13308892 :           if (!ai || !c[i]) continue;
    2325      256295 :           ai = mod_p-ai;
    2326     4480031 :           for (j = 1; j < i; j++) if (c[j]) a[j] = (a[j] + ai*c[j]) % mod_p;
    2327      256295 :           a[i] = 0;
    2328             :         }
    2329             :         /* Insert a/a[k] as k-th column */
    2330      465557 :         c = gel(basis, k);
    2331    13774452 :         for (i = 1; i<k; i++) if (a[i]) c[i] = (a[i] * invak) % mod_p;
    2332      465557 :         c[k] = 1; a = c;
    2333             :         /* Cleanup above k */
    2334    13575040 :         for (i = k+1; i<n; i++)
    2335             :         {
    2336             :           long j, ck;
    2337    13109483 :           c = gel(basis, i);
    2338    13109483 :           ck = c[k];
    2339    13109483 :           if (!ck) continue;
    2340     2721235 :           ck = mod_p-ck;
    2341    99097990 :           for (j = 1; j < k; j++) if (a[j]) c[j] = (c[j] + ck*a[j]) % mod_p;
    2342     2721235 :           c[k] = 0;
    2343             :         }
    2344      465557 :         cache->missing--;
    2345      465557 :         break;
    2346             :       }
    2347             :     }
    2348             :   }
    2349             :   else
    2350      372787 :     k = (cache->last - cache->base) + 1;
    2351     2392928 :   if (k || cache->relsup > 0 || (m && in_rnd_rel))
    2352             :   {
    2353             :     REL_t *rel;
    2354             : 
    2355      969692 : ADD_REL:
    2356      973804 :     rel = ++cache->last;
    2357      973804 :     if (!k && cache->relsup && nz < n+1)
    2358             :     {
    2359      131040 :       cache->relsup--;
    2360      131040 :       k = (rel - cache->base) + cache->missing;
    2361             :     }
    2362      973804 :     rel->R  = gclone(R);
    2363      973795 :     rel->m  = m ? gclone(m) : NULL;
    2364      973808 :     rel->nz = nz;
    2365      973808 :     if (aut)
    2366             :     {
    2367      272315 :       rel->relorig = (rel - cache->base) - orig;
    2368      272315 :       rel->relaut = aut;
    2369             :     }
    2370             :     else
    2371      701493 :       rel->relaut = 0;
    2372      973808 :     if (relp) *relp = rel;
    2373      973808 :     if (DEBUGLEVEL) dbg_newrel(cache);
    2374             :   }
    2375     2397039 :   return k;
    2376             : }
    2377             : 
    2378             : /* m a t_INT or primitive t_COL */
    2379             : static int
    2380     2446861 : 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     2446861 :   const long lauts = lg(F->idealperm), KC = F->KC;
    2385             : 
    2386     2446861 :   k = add_rel_i(cache, R, nz, m, 0, 0, &rel, in_rnd_rel);
    2387     2446916 :   if (k > 0 && typ(m) != t_INT)
    2388             :   {
    2389      529440 :     GEN Rl = cgetg(KC+1, t_VECSMALL);
    2390      529436 :     reln = rel - cache->base;
    2391     1007194 :     for (l = 1; l < lauts; l++)
    2392             :     {
    2393      477754 :       GEN perml = gel(F->idealperm, l);
    2394      477754 :       long i, nzl = perml[nz];
    2395             : 
    2396    20315225 :       for (i = 1; i <= KC; i++) Rl[i] = 0;
    2397    18107486 :       for (i = nz; i <= KC; i++)
    2398    17629732 :         if (R[i])
    2399             :         {
    2400     1255756 :           long v = perml[i];
    2401             : 
    2402     1255756 :           if (v < nzl) nzl = v;
    2403     1255756 :           Rl[v] = R[i];
    2404             :         }
    2405      477754 :       (void)add_rel_i(cache, Rl, nzl, NULL, reln, l, NULL, in_rnd_rel);
    2406             :     }
    2407             :   }
    2408     2446916 :   return k;
    2409             : }
    2410             : 
    2411             : INLINE void
    2412    30411787 : step(GEN x, double *y, GEN inc, long k)
    2413             : {
    2414    30411787 :   if (!y[k])
    2415     2147292 :     x[k]++; /* leading coeff > 0 */
    2416             :   else
    2417             :   {
    2418    28264495 :     long i = inc[k];
    2419    28264495 :     x[k] += i;
    2420    28264495 :     inc[k] = (i > 0)? -1-i: 1-i;
    2421             :   }
    2422    30411787 : }
    2423             : 
    2424             : static double
    2425      201175 : Fincke_Pohst_bound(double T, GEN r)
    2426             : {
    2427      201175 :   pari_sp av = avma;
    2428      201175 :   GEN zT = dbltor(T * T), p = gmael(r,1,1), B = real_1(DEFAULTPREC);
    2429      201170 :   long i, n = lg(r)-1;
    2430             :   double g;
    2431      542468 :   for (i = 2; i <= n; i++)
    2432             :   {
    2433      542462 :     p = gmul(p, gmael(r,i,i));
    2434      542471 :     B = sqrtnr(gmul(zT,p), i);
    2435      542467 :     if (i == n || cmprr(B, gmael(r,i+1,i+1)) < 0) break;
    2436             :   }
    2437      201179 :   if (!gisdouble(B,&g)) return gc_double(av, 0.);
    2438      201174 :   return gc_double(av, rtodbl(B));
    2439             : }
    2440             : 
    2441             : static void
    2442     1906145 : fact_update(GEN R, FB_t *F, long ipr, GEN c)
    2443             : {
    2444     1906145 :   GEN pr = gel(F->LP,ipr), p = pr_get_p(pr);
    2445     1906144 :   long v = Z_lval(c, itou(p));
    2446     1906148 :   if (v) R[ipr] -= pr_get_e(pr) * v;
    2447     1906148 : }
    2448             : 
    2449             : static long
    2450      201179 : Fincke_Pohst_ideal(RELCACHE_t *cache, FB_t *F, GEN nf, GEN I, GEN NI,
    2451             :   FACT *fact, long Nrelid, FP_t *fp, GEN rex, long jid, long jid0, long e0,
    2452             :   long *Nsmall, long *Nfact)
    2453             : {
    2454             :   pari_sp av;
    2455      201179 :   GEN G = nf_get_G(nf), G0 = nf_get_roundG(nf), r, u, gx, cgx, inc, ideal;
    2456      201179 :   long prec = nf_get_prec(nf), N = nf_get_degree(nf);
    2457      201176 :   long j, k, skipfirst, relid = 0, try_factor = 0;
    2458      201176 :   long try_elt = 0, maxtry_ELEMENT = 4*maxtry_FACT*maxtry_FACT;
    2459             :   double BOUND, B1, B2;
    2460             : 
    2461      201176 :   inc = const_vecsmall(N, 1);
    2462      201176 :   u = ZM_lll(ZM_mul(G0, I), 0.99, LLL_IM);
    2463      201178 :   ideal = ZM_mul(I,u); /* approximate T2-LLL reduction */
    2464      201169 :   r = gaussred_from_QR(RgM_mul(G, ideal), prec); /* Cholesky for T2 | ideal */
    2465      201175 :   if (!r) pari_err_BUG("small_norm (precision too low)");
    2466             : 
    2467      983490 :   for (k=1; k<=N; k++)
    2468             :   {
    2469      782313 :     if (!gisdouble(gcoeff(r,k,k),&(fp->v[k]))) return 0;
    2470     2512864 :     for (j=1; j<k; j++) if (!gisdouble(gcoeff(r,j,k),&(fp->q[j][k]))) return 0;
    2471      782314 :     if (DEBUGLEVEL>3) err_printf("v[%ld]=%.4g ",k,fp->v[k]);
    2472             :   }
    2473      201177 :   B1 = fp->v[1]; /* T2(ideal[1]) */
    2474      201177 :   B2 = fp->v[2] + B1 * fp->q[1][2] * fp->q[1][2]; /* T2(ideal[2]) */
    2475      201177 :   skipfirst = ZV_isscalar(gel(ideal,1));
    2476      201174 :   BOUND = maxdd(2*B2, Fincke_Pohst_bound(4 * maxtry_FACT / F->ballvol, r));
    2477      201176 :   if (DEBUGLEVEL>1)
    2478             :   {
    2479           0 :     if (DEBUGLEVEL>3) err_printf("\n");
    2480           0 :     err_printf("BOUND = %.4g\n",BOUND);
    2481             :   }
    2482             : 
    2483      201176 :   k = N; fp->y[N] = fp->z[N] = 0; fp->x[N] = 0;
    2484    20722937 :   for (av = avma;; set_avma(av), step(fp->x,fp->y,inc,k))
    2485    20518056 :   {
    2486             :     GEN R;
    2487             :     long nz;
    2488             :     do
    2489             :     { /* look for primitive element of small norm, cf minim00 */
    2490    25590078 :       int fl = 0;
    2491             :       double p;
    2492    25590078 :       if (k > 1)
    2493             :       {
    2494     5072475 :         long l = k-1;
    2495     5072475 :         fp->z[l] = 0;
    2496    45397878 :         for (j=k; j<=N; j++) fp->z[l] += fp->q[l][j]*fp->x[j];
    2497     5072475 :         p = (double)fp->x[k] + fp->z[k];
    2498     5072475 :         fp->y[l] = fp->y[k] + p*p*fp->v[k];
    2499     5072475 :         if (l <= skipfirst && !fp->y[1]) fl = 1;
    2500     5072475 :         fp->x[l] = (long)floor(-fp->z[l] + 0.5);
    2501     5072475 :         k = l;
    2502             :       }
    2503     4496597 :       for(;; step(fp->x,fp->y,inc,k))
    2504             :       {
    2505    30086630 :         if (!fl)
    2506             :         {
    2507    30032982 :           if (++try_elt > maxtry_ELEMENT) goto END_Fincke_Pohst_ideal;
    2508    30030574 :           p = (double)fp->x[k] + fp->z[k];
    2509    30030574 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2510             : 
    2511     5396430 :           step(fp->x,fp->y,inc,k);
    2512             : 
    2513     5397624 :           p = (double)fp->x[k] + fp->z[k];
    2514     5397624 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2515             :         }
    2516     4499005 :         fl = 0; inc[k] = 1;
    2517     4499005 :         if (++k > N) goto END_Fincke_Pohst_ideal;
    2518             :       }
    2519    25588819 :     } while (k > 1);
    2520             : 
    2521             :     /* element complete */
    2522    37368558 :     if (zv_content(fp->x) !=1) continue; /* not primitive */
    2523    17204252 :     gx = ZM_zc_mul(ideal,fp->x);
    2524    17203637 :     if (ZV_isscalar(gx)) continue;
    2525    17256193 :     if (++try_factor > maxtry_FACT) break;
    2526             : 
    2527    17150922 :     if (DEBUGLEVEL && Nsmall) (*Nsmall)++;
    2528    17150922 :     if (!factorgen(F,nf,I,NI,gx,fact)) continue;
    2529     2366943 :     if (!Nrelid) return 1;
    2530     2273443 :     if (jid == jid0)
    2531       27170 :       add_to_fact(jid, 1 + e0, fact);
    2532             :     else
    2533             :     {
    2534     2246273 :       add_to_fact(jid, 1, fact);
    2535     2246704 :       if (jid0) add_to_fact(jid0, e0, fact);
    2536             :     }
    2537             : 
    2538             :     /* smooth element */
    2539     2273874 :     R = set_fact(F, fact, rex, &nz);
    2540     2273883 :     cgx = Z_content(gx);
    2541     2273839 :     if (cgx)
    2542             :     { /* relatively rare, compute relation attached to gx/cgx */
    2543      471031 :       long i, n = fact[0].pr;
    2544      471031 :       gx = Q_div_to_int(gx, cgx);
    2545     2367994 :       for (i = 1; i <= n; i++) fact_update(R, F, fact[i].pr, cgx);
    2546      471032 :       if (rex)
    2547             :       {
    2548        3943 :         long l = lg(rex);
    2549       15952 :         for (i = 1; i < l; i++)
    2550       12009 :           if (rex[i])
    2551             :           {
    2552       11616 :             long t, ipr = F->subFB[i];
    2553       58949 :             for (t = 1; t <= n; t++)
    2554       49771 :               if (fact[t].pr == ipr) break;
    2555       11616 :             if (t > n) fact_update(R, F, ipr, cgx);
    2556             :           }
    2557             :       }
    2558             :     }
    2559     2273840 :     if (DEBUGLEVEL && Nfact) (*Nfact)++;
    2560             :     /* make sure we get maximal rank first, then allow all relations */
    2561     2273840 :     if (add_rel(cache, F, R, nz, gx, rex? 1: 0) <= 0)
    2562             :     { /* probably Q-dependent from previous ones: forget it */
    2563     1744866 :       if (DEBUGLEVEL>1) err_printf("*");
    2564     1744872 :       continue;
    2565             :     }
    2566      529036 :     if (cache->last >= cache->end) return 1; /* we have enough */
    2567      435550 :     if (++relid == Nrelid) break;
    2568             :   }
    2569      107679 :   END_Fincke_Pohst_ideal:
    2570      107679 :   return 0;
    2571             : }
    2572             : 
    2573             : static void
    2574       89163 : small_norm(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, FACT *fact, long j0)
    2575             : {
    2576       89163 :   const long N = nf_get_degree(nf);
    2577             :   FP_t fp;
    2578             :   pari_sp av;
    2579       89163 :   GEN L_jid = F->L_jid, Np0 = NULL, p0 = j0? gel(F->LP,j0): NULL;
    2580       89163 :   long Nsmall, Nfact, n = lg(L_jid), e0 = 0;
    2581             :   pari_timer T;
    2582             : 
    2583       89163 :   if (DEBUGLEVEL)
    2584             :   {
    2585           0 :     timer_start(&T);
    2586           0 :     err_printf("#### Look for %ld relations in %ld ideals (small_norm)\n",
    2587           0 :                cache->end - cache->last, lg(L_jid)-1);
    2588           0 :     if (p0) err_printf("Look in p0 = %Ps\n", vecslice(p0,1,4));
    2589             :   }
    2590       89163 :   Nsmall = Nfact = 0;
    2591       89163 :   minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2592       89162 :   if (p0)
    2593             :   {
    2594       26472 :     GEN n = pr_norm(p0);
    2595       26472 :     e0 = logint0(sqri(pr_norm(veclast(F->LP))), n, NULL);
    2596       26472 :     p0 = idealpows(nf, p0, e0);
    2597       26472 :     Np0 = powiu(n,e0);
    2598             :   }
    2599      191618 :   for (av = avma; --n; set_avma(av))
    2600             :   {
    2601      190874 :     long j = L_jid[n];
    2602      190874 :     GEN id = gel(F->LP, j), Nid;
    2603      190874 :     if (DEBUGLEVEL>1)
    2604           0 :       err_printf("\n*** Ideal no %ld: %Ps\n", j, vecslice(id,1,4));
    2605      190874 :     if (p0)
    2606             :     {
    2607       32360 :       if (j == j0)
    2608             :       { /* avoid trivial relation */
    2609        3633 :         long e = pr_get_e(id);
    2610        3633 :         if ((e0 + 1) % e == 0 && e * pr_get_f(id) == N) continue;
    2611             :       }
    2612       31701 :       Nid = mulii(Np0, pr_norm(id)); id = idealmul(nf, p0, id);
    2613             :     }
    2614             :     else
    2615      158514 :     { Nid = pr_norm(id); id = pr_hnf(nf, id);}
    2616      190218 :     if (Fincke_Pohst_ideal(cache, F, nf, id, Nid, fact, Nrelid, &fp,
    2617       88421 :                            NULL, j, j0, e0, &Nsmall, &Nfact)) break;
    2618             :   }
    2619       89164 :   if (DEBUGLEVEL && Nsmall)
    2620             :   {
    2621           0 :     if (DEBUGLEVEL == 1)
    2622           0 :     { if (Nfact) err_printf("\n"); }
    2623             :     else
    2624           0 :       err_printf("  \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
    2625           0 :                   Nfact,Nsmall,((double)Nfact)/Nsmall);
    2626           0 :     if (timer_get(&T)>1) timer_printf(&T,"small_norm");
    2627             :   }
    2628       89164 : }
    2629             : 
    2630             : static GEN
    2631        5886 : get_random_ideal(FB_t *F, GEN nf, GEN ex)
    2632             : {
    2633        5886 :   long i, l = lg(ex);
    2634             :   for (;;)
    2635          77 :   {
    2636        5963 :     GEN I = NULL;
    2637       19705 :     for (i = 1; i < l; i++)
    2638       13742 :       if ((ex[i] = random_bits(RANDOM_BITS)))
    2639             :       {
    2640       12866 :         GEN pr = gel(F->LP, F->subFB[i]), e = utoipos(ex[i]);
    2641       12866 :         I = I? idealmulpowprime(nf, I, pr, e): idealpow(nf, pr, e);
    2642             :       }
    2643        5963 :     if (I && !ZM_isscalar(I,NULL)) return I; /* != (n)Z_K */
    2644             :   }
    2645             : }
    2646             : 
    2647             : static void
    2648        5886 : rnd_rel(RELCACHE_t *cache, FB_t *F, GEN nf, FACT *fact)
    2649             : {
    2650             :   pari_timer T;
    2651        5886 :   GEN L_jid = F->L_jid, R, NR, ex;
    2652        5886 :   long i, l = lg(L_jid), Nfact = 0;
    2653             :   FP_t fp;
    2654             :   pari_sp av;
    2655             : 
    2656        5886 :   if (DEBUGLEVEL) {
    2657           0 :     timer_start(&T);
    2658           0 :     err_printf("#### Look for %ld relations in %ld ideals (rnd_rel)\n",
    2659           0 :                cache->end - cache->last, l-1);
    2660             :   }
    2661        5886 :   ex = cgetg(lg(F->subFB), t_VECSMALL);
    2662        5886 :   R = get_random_ideal(F, nf, ex); /* random product from subFB */
    2663        5886 :   NR = ZM_det_triangular(R);
    2664        5886 :   minim_alloc(nf_get_degree(nf)+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2665       11768 :   for (av = avma, i = 1; i < l; i++, set_avma(av))
    2666             :   { /* try P[j] * base */
    2667       10947 :     long j = L_jid[i];
    2668       10947 :     GEN P = gel(F->LP, j), Nid = mulii(NR, pr_norm(P));
    2669       10947 :     if (DEBUGLEVEL>1) err_printf("\n*** Ideal %ld: %Ps\n", j, vecslice(P,1,4));
    2670       10947 :     if (Fincke_Pohst_ideal(cache, F, nf, idealHNF_mul(nf, R, P), Nid, fact,
    2671        5065 :           RND_REL_RELPID, &fp, ex, j, 0, 0, NULL, &Nfact)) break;
    2672             :   }
    2673        5886 :   if (DEBUGLEVEL)
    2674             :   {
    2675           0 :     if (Nfact) err_printf("\n");
    2676           0 :     if (timer_get(&T)>=0) timer_printf(&T,"rnd_rel");
    2677             :   }
    2678        5886 : }
    2679             : 
    2680             : static GEN
    2681       63678 : automorphism_perms(GEN M, GEN auts, GEN cyclic, long r1, long r2, long N)
    2682             : {
    2683       63678 :   long L = lgcols(M), lauts = lg(auts), lcyc = lg(cyclic), i, j, l, m;
    2684       63678 :   GEN Mt, perms = cgetg(lauts, t_VEC);
    2685             :   pari_sp av;
    2686             : 
    2687      127595 :   for (l = 1; l < lauts; l++) gel(perms, l) = cgetg(L, t_VECSMALL);
    2688       63678 :   av = avma;
    2689       63678 :   Mt = shallowtrans(gprec_w(M, LOWDEFAULTPREC));
    2690       63678 :   Mt = shallowconcat(Mt, conj_i(vecslice(Mt, r1+1, r1+r2)));
    2691      110345 :   for (l = 1; l < lcyc; l++)
    2692             :   {
    2693       46669 :     GEN thiscyc = gel(cyclic, l), thisperm, perm, prev, Nt;
    2694       46669 :     long k = thiscyc[1];
    2695             : 
    2696       46669 :     Nt = RgM_mul(shallowtrans(gel(auts, k)), Mt);
    2697       46672 :     perm = gel(perms, k);
    2698      153387 :     for (i = 1; i < L; i++)
    2699             :     {
    2700      106721 :       GEN v = gel(Nt, i), minD;
    2701      106721 :       minD = gnorml2(gsub(v, gel(Mt, 1)));
    2702      106720 :       perm[i] = 1;
    2703      563558 :       for (j = 2; j <= N; j++)
    2704             :       {
    2705      456843 :         GEN D = gnorml2(gsub(v, gel(Mt, j)));
    2706      456835 :         if (gcmp(D, minD) < 0) { minD = D; perm[i] = j >= L ? r2-j : j; }
    2707             :       }
    2708             :     }
    2709       65124 :     for (prev = perm, m = 2; m < lg(thiscyc); m++, prev = thisperm)
    2710             :     {
    2711       18458 :       thisperm = gel(perms, thiscyc[m]);
    2712       93670 :       for (i = 1; i < L; i++)
    2713             :       {
    2714       75212 :         long pp = labs(prev[i]);
    2715       75212 :         thisperm[i] = prev[i] < 0 ? -perm[pp] : perm[pp];
    2716             :       }
    2717             :     }
    2718             :   }
    2719       63676 :   set_avma(av); return perms;
    2720             : }
    2721             : 
    2722             : /* Determine the field automorphisms as matrices on the integral basis */
    2723             : static GEN
    2724       63742 : automorphism_matrices(GEN nf, GEN *cycp)
    2725             : {
    2726       63742 :   pari_sp av = avma;
    2727       63742 :   GEN auts = galoisconj(nf, NULL), mats, cyclic, cyclicidx;
    2728       63742 :   long nauts = lg(auts)-1, i, j, k, l;
    2729             : 
    2730       63742 :   cyclic = cgetg(nauts+1, t_VEC);
    2731       63742 :   cyclicidx = zero_Flv(nauts);
    2732       97881 :   for (l = 1; l <= nauts; l++)
    2733             :   {
    2734       97881 :     GEN aut = gel(auts, l);
    2735       97881 :     if (gequalX(aut)) { swap(gel(auts, l), gel(auts, nauts)); break; }
    2736             :   }
    2737             :   /* trivial automorphism is last */
    2738      191426 :   for (l = 1; l <= nauts; l++) gel(auts, l) = algtobasis(nf, gel(auts, l));
    2739             :   /* Compute maximal cyclic subgroups */
    2740      127683 :   for (l = nauts; --l > 0; ) if (!cyclicidx[l])
    2741             :   {
    2742       48153 :     GEN elt = gel(auts, l), aut = elt, cyc = cgetg(nauts+1, t_VECSMALL);
    2743       48153 :     cyc[1] = cyclicidx[l] = l; j = 1;
    2744             :     do
    2745             :     {
    2746       67158 :       elt = galoisapply(nf, elt, aut);
    2747      217647 :       for (k = 1; k <= nauts; k++) if (gequal(elt, gel(auts, k))) break;
    2748       67156 :       cyclicidx[k] = l; cyc[++j] = k;
    2749             :     }
    2750       67156 :     while (k != nauts);
    2751       48151 :     setlg(cyc, j);
    2752       48151 :     gel(cyclic, l) = cyc;
    2753             :   }
    2754      127681 :   for (i = j = 1; i < nauts; i++)
    2755       63943 :     if (cyclicidx[i] == i) cyclic[j++] = cyclic[i];
    2756       63738 :   setlg(cyclic, j);
    2757       63740 :   mats = cgetg(nauts, t_VEC);
    2758      110436 :   while (--j > 0)
    2759             :   {
    2760       46695 :     GEN cyc = gel(cyclic, j);
    2761       46695 :     long id = cyc[1];
    2762       46695 :     GEN M, Mi, aut = gel(auts, id);
    2763             : 
    2764       46695 :     gel(mats, id) = Mi = M = nfgaloismatrix(nf, aut);
    2765       65156 :     for (i = 2; i < lg(cyc); i++) gel(mats, cyc[i]) = Mi = ZM_mul(Mi, M);
    2766             :   }
    2767       63741 :   gerepileall(av, 2, &mats, &cyclic);
    2768       63741 :   if (cycp) *cycp = cyclic;
    2769       63741 :   return mats;
    2770             : }
    2771             : 
    2772             : /* vP a list of maximal ideals above the same p from idealprimedec: f(P/p) is
    2773             :  * increasing; 1 <= j <= #vP; orbit a zc of length <= #vP; auts a vector of
    2774             :  * automorphisms in ZM form.
    2775             :  * Set orbit[i] = 1 for all vP[i], i >= j, in the orbit of pr = vP[j] wrt auts.
    2776             :  * N.B.1 orbit need not be initialized to 0: useful to incrementally run
    2777             :  * through successive orbits
    2778             :  * N.B.2 i >= j, so primes with index < j will be missed; run incrementally
    2779             :  * starting from j = 1 ! */
    2780             : static void
    2781       11865 : pr_orbit_fill(GEN orbit, GEN auts, GEN vP, long j)
    2782             : {
    2783       11865 :   GEN pr = gel(vP,j), gen = pr_get_gen(pr);
    2784       11865 :   long i, l = lg(auts), J = lg(orbit), f = pr_get_f(pr);
    2785       11865 :   orbit[j] = 1;
    2786       23730 :   for (i = 1; i < l; i++)
    2787             :   {
    2788       11865 :     GEN g = ZM_ZC_mul(gel(auts,i), gen);
    2789             :     long k;
    2790       11886 :     for (k = j+1; k < J; k++)
    2791             :     {
    2792          35 :       GEN prk = gel(vP,k);
    2793          35 :       if (pr_get_f(prk) > f) break; /* f(P[k]) increases with k */
    2794             :       /* don't check that e matches: (almost) always 1 ! */
    2795          35 :       if (!orbit[k] && ZC_prdvd(g, prk)) { orbit[k] = 1; break; }
    2796             :     }
    2797             :   }
    2798       11865 : }
    2799             : /* remark: F->KCZ changes if be_honest() fails */
    2800             : static int
    2801           7 : be_honest(FB_t *F, GEN nf, GEN auts, FACT *fact)
    2802             : {
    2803             :   long i, iz, nbtest;
    2804           7 :   long lgsub = lg(F->subFB), KCZ0 = F->KCZ, N = nf_get_degree(nf);
    2805             :   FP_t fp;
    2806             :   pari_sp av;
    2807             : 
    2808           7 :   if (DEBUGLEVEL) {
    2809           0 :     err_printf("Be honest for %ld primes from %ld to %ld\n", F->KCZ2 - F->KCZ,
    2810           0 :                F->FB[ F->KCZ+1 ], F->FB[ F->KCZ2 ]);
    2811             :   }
    2812           7 :   minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2813           7 :   if (lg(auts) == 1) auts = NULL;
    2814           7 :   av = avma;
    2815          14 :   for (iz=F->KCZ+1; iz<=F->KCZ2; iz++, set_avma(av))
    2816             :   {
    2817           7 :     long p = F->FB[iz];
    2818           7 :     GEN pr_orbit, P = gel(F->LV,p);
    2819           7 :     long j, J = lg(P); /* > 1 */
    2820             :     /* the P|p, NP > C2 are assumed in subgroup generated by FB + last P
    2821             :      * with NP <= C2 is unramified --> check all but last */
    2822           7 :     if (pr_get_e(gel(P,J-1)) == 1) J--;
    2823           7 :     if (J == 1) continue;
    2824           7 :     if (DEBUGLEVEL>1) err_printf("%ld ", p);
    2825           7 :     pr_orbit = auts? zero_zv(J-1): NULL;
    2826          28 :     for (j = 1; j < J; j++)
    2827             :     {
    2828             :       GEN Nid, id, id0;
    2829          21 :       if (pr_orbit)
    2830             :       {
    2831          21 :         if (pr_orbit[j]) continue;
    2832             :         /* discard all primes in automorphism orbit simultaneously */
    2833          14 :         pr_orbit_fill(pr_orbit, auts, P, j);
    2834             :       }
    2835          14 :       id = id0 = pr_hnf(nf,gel(P,j));
    2836          14 :       Nid = pr_norm(gel(P,j));
    2837          14 :       for (nbtest=0;;)
    2838             :       {
    2839          14 :         if (Fincke_Pohst_ideal(NULL, F, nf, id, Nid, fact, 0, &fp,
    2840          14 :                                NULL, 0, 0, 0, NULL, NULL)) break;
    2841           0 :         if (++nbtest > maxtry_HONEST)
    2842             :         {
    2843           0 :           if (DEBUGLEVEL)
    2844           0 :             pari_warn(warner,"be_honest() failure on prime %Ps\n", gel(P,j));
    2845           0 :           return 0;
    2846             :         }
    2847             :         /* occurs at most once in the whole function */
    2848           0 :         for (i = 1, id = id0; i < lgsub; i++)
    2849             :         {
    2850           0 :           long ex = random_bits(RANDOM_BITS);
    2851           0 :           if (ex)
    2852             :           {
    2853           0 :             GEN pr = gel(F->LP, F->subFB[i]);
    2854           0 :             id = idealmulpowprime(nf, id, pr, utoipos(ex));
    2855             :           }
    2856             :         }
    2857           0 :         if (!equali1(gcoeff(id,N,N))) id = Q_primpart(id);
    2858           0 :         if (expi(gcoeff(id,1,1)) > 100) id = idealred(nf, id);
    2859           0 :         Nid = ZM_det_triangular(id);
    2860             :       }
    2861             :     }
    2862           7 :     F->KCZ++; /* SUCCESS, "enlarge" factorbase */
    2863             :   }
    2864           7 :   F->KCZ = KCZ0; return gc_bool(av,1);
    2865             : }
    2866             : 
    2867             : /* all primes with N(P) <= BOUND factor on factorbase ? */
    2868             : void
    2869          63 : bnftestprimes(GEN bnf, GEN BOUND)
    2870             : {
    2871          63 :   pari_sp av0 = avma, av;
    2872          63 :   ulong count = 0;
    2873          63 :   GEN auts, p, nf = bnf_get_nf(bnf), Vbase = bnf_get_vbase(bnf);
    2874          63 :   GEN fb = gen_sort_shallow(Vbase, (void*)&cmp_prime_ideal, cmp_nodata);
    2875          63 :   ulong pmax = pr_get_smallp(veclast(fb)); /*largest p in factorbase*/
    2876             :   forprime_t S;
    2877             :   FACT *fact;
    2878             :   FB_t F;
    2879             : 
    2880          63 :   (void)recover_partFB(&F, Vbase, nf_get_degree(nf));
    2881          63 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    2882          63 :   forprime_init(&S, gen_2, BOUND);
    2883          63 :   auts = automorphism_matrices(nf, NULL);
    2884          63 :   if (lg(auts) == 1) auts = NULL;
    2885          63 :   av = avma;
    2886       37604 :   while (( p = forprime_next(&S) ))
    2887             :   {
    2888             :     GEN pr_orbit, vP;
    2889             :     long j, J;
    2890             : 
    2891       37541 :     if (DEBUGLEVEL == 1 && ++count > 1000)
    2892             :     {
    2893           0 :       err_printf("passing p = %Ps / %Ps\n", p, BOUND);
    2894           0 :       count = 0;
    2895             :     }
    2896       37541 :     set_avma(av);
    2897       37541 :     vP = idealprimedec_limit_norm(nf, p, BOUND);
    2898       37541 :     J = lg(vP);
    2899             :     /* if last is unramified, all P|p in subgroup generated by FB: skip last */
    2900       37541 :     if (J > 1 && pr_get_e(gel(vP,J-1)) == 1) J--;
    2901       37541 :     if (J == 1) continue;
    2902       14525 :     if (DEBUGLEVEL>1) err_printf("*** p = %Ps\n",p);
    2903       14525 :     pr_orbit = auts? zero_zv(J-1): NULL;
    2904       31549 :     for (j = 1; j < J; j++)
    2905             :     {
    2906       17024 :       GEN P = gel(vP,j);
    2907       17024 :       long k = 0;
    2908       17024 :       if (pr_orbit)
    2909             :       {
    2910       11858 :         if (pr_orbit[j]) continue;
    2911             :         /* discard all primes in automorphism orbit simultaneously */
    2912       11851 :         pr_orbit_fill(pr_orbit, auts, vP, j);
    2913             :       }
    2914       17017 :       if (abscmpiu(p, pmax) > 0 || !(k = tablesearch(fb, P, &cmp_prime_ideal)))
    2915       16408 :         (void)SPLIT(&F, nf, pr_hnf(nf,P), Vbase, fact);
    2916       17017 :       if (DEBUGLEVEL>1)
    2917             :       {
    2918           0 :         err_printf("  Testing P = %Ps\n",P);
    2919           0 :         if (k) err_printf("    #%ld in factor base\n",k);
    2920           0 :         else err_printf("    is %Ps\n", isprincipal(bnf,P));
    2921             :       }
    2922             :     }
    2923             :   }
    2924          63 :   set_avma(av0);
    2925          63 : }
    2926             : 
    2927             : /* A t_MAT of complex floats, in fact reals. Extract a submatrix B
    2928             :  * whose columns are definitely nonzero, i.e. gexpo(A[j]) >= -2
    2929             :  *
    2930             :  * If possible precision problem (t_REAL 0 with large exponent), set
    2931             :  * *precpb to 1 */
    2932             : static GEN
    2933       78962 : clean_cols(GEN A, int *precpb)
    2934             : {
    2935       78962 :   long l = lg(A), h, i, j, k;
    2936             :   GEN B;
    2937       78962 :   *precpb = 0;
    2938       78962 :   if (l == 1) return A;
    2939       78962 :   h = lgcols(A);;
    2940       78962 :   B = cgetg(l, t_MAT);
    2941      980106 :   for (i = k = 1; i < l; i++)
    2942             :   {
    2943      901144 :     GEN Ai = gel(A,i);
    2944      901144 :     int non0 = 0;
    2945     4014906 :     for (j = 1; j < h; j++)
    2946             :     {
    2947     3113763 :       GEN c = gel(Ai,j);
    2948     3113763 :       if (gexpo(c) >= -2)
    2949             :       {
    2950     1892749 :         if (gequal0(c)) *precpb = 1; else non0 = 1;
    2951             :       }
    2952             :     }
    2953      901143 :     if (non0) gel(B, k++) = Ai;
    2954             :   }
    2955       78962 :   setlg(B, k); return B;
    2956             : }
    2957             : 
    2958             : static long
    2959      516082 : compute_multiple_of_R_pivot(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2960             : {
    2961      516082 :   GEN x = gel(X,ix);
    2962      516082 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2963             :   (void)x0;
    2964     2627477 :   for (i=1; i<lx; i++)
    2965     2111393 :     if (!c[i] && !gequal0(gel(x,i)))
    2966             :     {
    2967      676178 :       long e = gexpo(gel(x,i));
    2968      676180 :       if (e > ex) { ex = e; k = i; }
    2969             :     }
    2970      516084 :   return (k && ex > -32)? k: lx;
    2971             : }
    2972             : 
    2973             : /* Ar = (log |sigma_i(u_j)|) for units (u_j) found so far;
    2974             :  * RU = R1+R2 = target rank for unit matrix, after adding [1 x r1, 2 x r2];
    2975             :  * N = field degree, need = unit rank defect;
    2976             :  * L = NULL (prec problem) or B^(-1) * A with approximate rational entries
    2977             :  * (as t_REAL), B a submatrix of A, with (probably) maximal rank RU */
    2978             : static GEN
    2979       94673 : compute_multiple_of_R(GEN Ar, long RU, long N, long *pneed, long *bit, GEN *ptL)
    2980             : {
    2981             :   GEN T, d, mdet, Im_mdet, kR, L;
    2982       94673 :   long i, j, r, R1 = 2*RU - N;
    2983             :   int precpb;
    2984       94673 :   pari_sp av = avma;
    2985             : 
    2986       94673 :   if (RU == 1) { *ptL = zeromat(0, lg(Ar)-1); return gen_1; }
    2987             : 
    2988       78962 :   if (DEBUGLEVEL) err_printf("\n#### Computing regulator multiple\n");
    2989       78962 :   mdet = clean_cols(Ar, &precpb);
    2990             :   /* will cause precision to increase on later failure, but we may succeed! */
    2991       78962 :   *ptL = precpb? NULL: gen_1;
    2992       78962 :   T = cgetg(RU+1,t_COL);
    2993      189850 :   for (i=1; i<=R1; i++) gel(T,i) = gen_1;
    2994      179298 :   for (   ; i<=RU; i++) gel(T,i) = gen_2;
    2995       78962 :   mdet = shallowconcat(T, mdet); /* det(Span(mdet)) = N * R */
    2996             : 
    2997             :   /* could be using indexrank(), but need custom "get_pivot" function */
    2998       78962 :   d = RgM_pivots(mdet, NULL, &r, &compute_multiple_of_R_pivot);
    2999             :   /* # of independent columns = target rank ? */
    3000       78962 :   if (lg(mdet)-1 - r != RU)
    3001             :   {
    3002       21296 :     if (DEBUGLEVEL)
    3003           0 :       err_printf("Units matrix target rank = %ld < %ld\n",lg(mdet)-1 - r, RU);
    3004       21296 :     *pneed = RU - (lg(mdet)-1-r); return gc_NULL(av);
    3005             :   }
    3006             : 
    3007       57666 :   Im_mdet = cgetg(RU+1, t_MAT); /* extract independent columns */
    3008             :   /* N.B: d[1] = 1, corresponding to T above */
    3009       57666 :   gel(Im_mdet, 1) = T;
    3010      233381 :   for (i = j = 2; i <= RU; j++)
    3011      175715 :     if (d[j]) gel(Im_mdet, i++) = gel(mdet,j);
    3012             : 
    3013             :   /* integral multiple of R: the cols we picked form a Q-basis, they have an
    3014             :    * index in the full lattice. First column is T */
    3015       57666 :   kR = divru(det2(Im_mdet), N);
    3016             :   /* R > 0.2 uniformly */
    3017       57666 :   if (!signe(kR) || expo(kR) < -3)
    3018             :   {
    3019           0 :     if (DEBUGLEVEL) err_printf("Regulator is zero.\n");
    3020           0 :     *pneed = 0; return gc_NULL(av);
    3021             :   }
    3022       57666 :   d = det2(rowslice(vecslice(Im_mdet, 2, RU), 2, RU));
    3023       57665 :   setabssign(d); setabssign(kR);
    3024       57665 :   if (gexpo(gsub(d,kR)) - gexpo(d) > -20) { *ptL = NULL; return gc_NULL(av); }
    3025       57664 :   L = RgM_inv(Im_mdet);
    3026             :   /* estimate # of correct bits in result */
    3027       57665 :   if (!L || (*bit = -gexpo(RgM_Rg_sub_shallow(RgM_mul(L,Im_mdet), gen_1))) < 16)
    3028          10 :   { *ptL = NULL; return gc_NULL(av); }
    3029             : 
    3030       57655 :   *ptL = RgM_mul(rowslice(L,2,RU), Ar); /* approximate rational entries */
    3031       57655 :   return gc_all(av,2, &kR, ptL);
    3032             : }
    3033             : 
    3034             : /* leave small integer n as is, convert huge n to t_REAL (for readability) */
    3035             : static GEN
    3036           0 : i2print(GEN n)
    3037           0 : { return lgefint(n) <= DEFAULTPREC? n: itor(n,LOWDEFAULTPREC); }
    3038             : 
    3039             : static long
    3040       73214 : bad_check(GEN c)
    3041             : {
    3042       73214 :   long ec = gexpo(c);
    3043       73214 :   if (DEBUGLEVEL) err_printf("\n ***** check = %.28Pg\n",c);
    3044             :   /* safe check for c < 0.75 : avoid underflow in gtodouble() */
    3045       73214 :   if (ec < -1 || (ec == -1 && gtodouble(c) < 0.75)) return fupb_PRECI;
    3046             :   /* safe check for c > 1.3 : avoid overflow */
    3047       73214 :   if (ec > 0 || (ec == 0 && gtodouble(c) > 1.3)) return fupb_RELAT;
    3048       63677 :   return fupb_NONE;
    3049             : }
    3050             : /* Input:
    3051             :  * lambda = approximate rational entries: coords of units found so far on a
    3052             :  * sublattice of maximal rank (sublambda)
    3053             :  * *ptkR = regulator of sublambda = multiple of regulator of lambda
    3054             :  * Compute R = true regulator of lambda.
    3055             :  *
    3056             :  * If c := Rz ~ 1, by Dirichlet's formula, then lambda is the full group of
    3057             :  * units AND the full set of relations for the class group has been computed.
    3058             :  * In fact z is a very rough approximation and we only expect 0.75 < Rz < 1.3
    3059             :  *
    3060             :  * Output: *ptkR = R, *ptL = numerator(units) (in terms of lambda) */
    3061             : static long
    3062       73285 : compute_R(GEN lambda, GEN z, GEN *ptL, GEN *ptkR)
    3063             : {
    3064       73285 :   pari_sp av = avma;
    3065       73285 :   long bit, r, reason, RU = lg(lambda) == 1? 1: lgcols(lambda);
    3066             :   GEN L, H, D, den, R, c;
    3067             : 
    3068       73285 :   *ptL = NULL;
    3069       73285 :   if (RU == 1) { *ptkR = gen_1; *ptL = lambda; return bad_check(z); }
    3070       57574 :   D = gmul2n(mpmul(*ptkR,z), 1); /* bound for denom(lambda) */
    3071       57571 :   if (expo(D) < 0 && rtodbl(D) < 0.95) return fupb_PRECI;
    3072       57571 :   L = bestappr(lambda,D);
    3073       57572 :   if (lg(L) == 1)
    3074             :   {
    3075           0 :     if (DEBUGLEVEL) err_printf("truncation error in bestappr\n");
    3076           0 :     return fupb_PRECI;
    3077             :   }
    3078       57572 :   den = Q_denom(L);
    3079       57573 :   if (mpcmp(den,D) > 0)
    3080             :   {
    3081          21 :     if (DEBUGLEVEL) err_printf("D = %Ps\nden = %Ps\n",D, i2print(den));
    3082          21 :     return fupb_PRECI;
    3083             :   }
    3084       57551 :   bit = -gexpo(gsub(L, lambda)); /* input accuracy */
    3085       57553 :   L = Q_muli_to_int(L, den);
    3086       57550 :   if (gexpo(L) + expi(den) > bit - 32)
    3087             :   {
    3088          48 :     if (DEBUGLEVEL) err_printf("dubious bestappr; den = %Ps\n", i2print(den));
    3089          48 :     return fupb_PRECI;
    3090             :   }
    3091       57505 :   H = ZM_hnf(L); r = lg(H)-1;
    3092       57505 :   if (!r || r != nbrows(H))
    3093           0 :     R = gen_0; /* wrong rank */
    3094             :   else
    3095       57505 :     R = gmul(*ptkR, gdiv(ZM_det_triangular(H), powiu(den, r)));
    3096             :   /* R = tentative regulator; regulator > 0.2 uniformly */
    3097       57504 :   if (gexpo(R) < -3) {
    3098           0 :     if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3099           0 :     return gc_long(av, fupb_PRECI);
    3100             :   }
    3101       57503 :   c = gmul(R,z); /* should be n (= 1 if we are done) */
    3102       57504 :   if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3103       57504 :   if ((reason = bad_check(c))) return gc_long(av, reason);
    3104       48481 :   *ptkR = R; *ptL = L; return fupb_NONE;
    3105             : }
    3106             : static GEN
    3107       63776 : get_clg2(GEN cyc, GEN Ga, GEN C, GEN Ur, GEN Ge, GEN M1, GEN M2)
    3108             : {
    3109       63776 :   GEN GD = gsub(act_arch(M1, C), diagact_arch(cyc, Ga));
    3110       63776 :   GEN ga = gsub(act_arch(M2, C), act_arch(Ur, Ga));
    3111       63776 :   return mkvecn(6, Ur, ga, GD, Ge, M1, M2);
    3112             : }
    3113             : /* compute class group (clg1) + data for isprincipal (clg2) */
    3114             : static GEN
    3115       63679 : class_group_gen(GEN nf,GEN W,GEN C,GEN Vbase,long prec, GEN *pclg2)
    3116             : {
    3117             :   GEN M1, M2, z, G, Ga, Ge, cyc, X, Y, D, U, V, Ur, Ui, Uir;
    3118             :   long j, l;
    3119             : 
    3120       63679 :   D = ZM_snfall(W,&U,&V); /* UWV=D, D diagonal, G = g Ui (G=new gens, g=old) */
    3121       63679 :   Ui = ZM_inv(U, NULL);
    3122       63679 :   l = lg(D); cyc = cgetg(l, t_VEC); /* elementary divisors */
    3123       92463 :   for (j = 1; j < l; j++)
    3124             :   {
    3125       30350 :     gel(cyc,j) = gcoeff(D,j,j); /* strip useless components */
    3126       30350 :     if (is_pm1(gel(cyc,j))) break;
    3127             :   }
    3128       63679 :   l = j;
    3129       63679 :   Ur  = ZM_hnfdivrem(U, D, &Y);
    3130       63679 :   Uir = ZM_hnfdivrem(Ui,W, &X);
    3131             :  /* {x} = logarithmic embedding of x (arch. component)
    3132             :   * NB: [J,z] = idealred(I) --> I = y J, with {y} = - z
    3133             :   * G = g Uir - {Ga},  Uir = Ui + WX
    3134             :   * g = G Ur  - {ga},  Ur  = U + DY */
    3135       63679 :   G = cgetg(l,t_VEC);
    3136       63679 :   Ga= cgetg(l,t_MAT);
    3137       63679 :   Ge= cgetg(l,t_COL);
    3138       63679 :   z = init_famat(NULL);
    3139       92463 :   for (j = 1; j < l; j++)
    3140             :   {
    3141       28784 :     GEN I = genback(z, nf, Vbase, gel(Uir,j));
    3142       28784 :     gel(G,j) = gel(I,1); /* generator, order cyc[j] */
    3143       28784 :     gel(Ge,j)= gel(I,2);
    3144       28784 :     gel(Ga,j)= nf_cxlog(nf, gel(I,2), prec);
    3145       28784 :     if (!gel(Ga,j)) pari_err_PREC("class_group_gen");
    3146             :   }
    3147             :   /* {ga} = {GD}Y + G U - g = {GD}Y - {Ga} U + gW X U
    3148             :                             = gW (X Ur + V Y) - {Ga}Ur */
    3149       63679 :   M2 = ZM_add(ZM_mul(X,Ur), ZM_mul(V,Y));
    3150       63679 :   setlg(cyc,l); setlg(V,l); setlg(D,l);
    3151             :   /* G D =: {GD} = g (Ui + W X) D - {Ga}D = g W (V + X D) - {Ga}D
    3152             :    * NB: Ui D = W V. gW is given by (first l-1 cols of) C */
    3153       63679 :   M1 = ZM_add(V, ZM_mul(X,D));
    3154       63679 :   *pclg2 = get_clg2(cyc, Ga, C, Ur, Ge, M1, M2);
    3155       63679 :   return mkvec3(ZV_prod(cyc), cyc, G);
    3156             : }
    3157             : 
    3158             : /* compute principal ideals corresponding to (gen[i]^cyc[i]) */
    3159             : static GEN
    3160        4956 : makecycgen(GEN bnf)
    3161             : {
    3162        4956 :   GEN cyc = bnf_get_cyc(bnf), gen = bnf_get_gen(bnf), nf = bnf_get_nf(bnf);
    3163        4956 :   GEN h, y, GD = bnf_get_GD(bnf), W = bnf_get_W(bnf); /* HNF */
    3164        4956 :   GEN Sunits = bnf_get_sunits(bnf);
    3165        4956 :   GEN X = Sunits? gel(Sunits,1): NULL, C = Sunits? gel(Sunits,3): NULL;
    3166             :   long e, i, l;
    3167             : 
    3168        4956 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building cycgen)");
    3169        4956 :   h = cgetg_copy(gen, &l);
    3170       11613 :   for (i = 1; i < l; i++)
    3171             :   {
    3172        6657 :     GEN gi = gel(gen,i), ci = gel(cyc,i);
    3173        6657 :     if (X && equalii(ci, gcoeff(W,i,i)))
    3174             :     {
    3175             :       long j;
    3176        8589 :       for (j = i+1; j < l; j++)
    3177        3213 :         if (signe(gcoeff(W,i,j))) break;
    3178        5550 :       if (j == i) { gel(h,i) = mkmat2(X, gel(C,i)); continue; }
    3179             :     }
    3180        6657 :     if (abscmpiu(ci, 5) < 0)
    3181             :     {
    3182        5544 :       GEN N = ZM_det_triangular(gi);
    3183        5544 :       y = isprincipalarch(bnf,gel(GD,i), N, ci, gen_1, &e);
    3184        5544 :       if (y && fact_ok(nf,y,NULL,mkvec(gi),mkvec(ci)))
    3185             :       {
    3186        4556 :         gel(h,i) = to_famat_shallow(y,gen_1);
    3187        4556 :         continue;
    3188             :       }
    3189             :     }
    3190        2101 :     y = isprincipalfact(bnf, NULL, mkvec(gi), mkvec(ci), nf_GENMAT|nf_FORCE);
    3191        2101 :     gel(h,i) = gel(y,2);
    3192             :   }
    3193        4956 :   return h;
    3194             : }
    3195             : 
    3196             : static GEN
    3197          69 : get_y(GEN bnf, GEN W, GEN B, GEN C, GEN pFB, long j)
    3198             : {
    3199          69 :   GEN y, nf  = bnf_get_nf(bnf);
    3200          69 :   long e, lW = lg(W)-1;
    3201          69 :   GEN ex = (j<=lW)? gel(W,j): gel(B,j-lW);
    3202          69 :   GEN P = (j<=lW)? NULL: gel(pFB,j);
    3203          69 :   if (C)
    3204             :   { /* archimedean embeddings known: cheap trial */
    3205          69 :     GEN Nx = get_norm_fact_primes(pFB, ex, P);
    3206          69 :     y = isprincipalarch(bnf,gel(C,j), Nx,gen_1, gen_1, &e);
    3207          69 :     if (y && fact_ok(nf,y,P,pFB,ex)) return y;
    3208             :   }
    3209           0 :   y = isprincipalfact_or_fail(bnf, P, pFB, ex);
    3210           0 :   return typ(y) == t_INT? y: gel(y,2);
    3211             : }
    3212             : /* compute principal ideals corresponding to bnf relations */
    3213             : static GEN
    3214          20 : makematal(GEN bnf)
    3215             : {
    3216          20 :   GEN W = bnf_get_W(bnf), B = bnf_get_B(bnf), C = bnf_get_C(bnf);
    3217             :   GEN pFB, ma, retry;
    3218          20 :   long lma, j, prec = 0;
    3219             : 
    3220          20 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building matal)");
    3221          20 :   lma=lg(W)+lg(B)-1;
    3222          20 :   pFB = bnf_get_vbase(bnf);
    3223          20 :   ma = cgetg(lma,t_VEC);
    3224          20 :   retry = vecsmalltrunc_init(lma);
    3225          89 :   for (j=lma-1; j>0; j--)
    3226             :   {
    3227          69 :     pari_sp av = avma;
    3228          69 :     GEN y = get_y(bnf, W, B, C, pFB, j);
    3229          69 :     if (typ(y) == t_INT)
    3230             :     {
    3231           0 :       long E = itos(y);
    3232           0 :       if (DEBUGLEVEL>1) err_printf("\n%ld done later at prec %ld\n",j,E);
    3233           0 :       set_avma(av);
    3234           0 :       vecsmalltrunc_append(retry, j);
    3235           0 :       if (E > prec) prec = E;
    3236             :     }
    3237             :     else
    3238             :     {
    3239          69 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3240          69 :       gel(ma,j) = gerepileupto(av,y);
    3241             :     }
    3242             :   }
    3243          20 :   if (prec)
    3244             :   {
    3245           0 :     long k, l = lg(retry);
    3246           0 :     GEN y, nf = bnf_get_nf(bnf);
    3247           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"makematal",prec);
    3248           0 :     nf = nfnewprec_shallow(nf,prec);
    3249           0 :     bnf = Buchall(nf, nf_FORCE, prec);
    3250           0 :     if (DEBUGLEVEL) err_printf("makematal, adding missing entries:");
    3251           0 :     for (k=1; k<l; k++)
    3252             :     {
    3253           0 :       pari_sp av = avma;
    3254           0 :       long j = retry[k];
    3255           0 :       y = get_y(bnf,W,B,NULL, pFB, j);
    3256           0 :       if (typ(y) == t_INT) pari_err_PREC("makematal");
    3257           0 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3258           0 :       gel(ma,j) = gerepileupto(av,y);
    3259             :     }
    3260             :   }
    3261          20 :   if (DEBUGLEVEL>1) err_printf("\n");
    3262          20 :   return ma;
    3263             : }
    3264             : 
    3265             : enum { MATAL = 1, CYCGEN, UNITS };
    3266             : GEN
    3267       26726 : bnf_build_cycgen(GEN bnf)
    3268       26726 : { return obj_checkbuild(bnf, CYCGEN, &makecycgen); }
    3269             : GEN
    3270          20 : bnf_build_matalpha(GEN bnf)
    3271          20 : { return obj_checkbuild(bnf, MATAL, &makematal); }
    3272             : GEN
    3273       32047 : bnf_build_units(GEN bnf)
    3274       32047 : { return obj_checkbuild(bnf, UNITS, &makeunits); }
    3275             : 
    3276             : /* return fu in compact form if available; in terms of a fixed basis
    3277             :  * of S-units */
    3278             : GEN
    3279          70 : bnf_compactfu_mat(GEN bnf)
    3280             : {
    3281          70 :   GEN X, U, SUnits = bnf_get_sunits(bnf);
    3282          70 :   if (!SUnits) return NULL;
    3283          70 :   X = gel(SUnits,1);
    3284          70 :   U = gel(SUnits,2); ZM_remove_unused(&U, &X);
    3285          70 :   return mkvec2(X, U);
    3286             : }
    3287             : /* return fu in compact form if available; individually as famat */
    3288             : GEN
    3289       37135 : bnf_compactfu(GEN bnf)
    3290             : {
    3291       37135 :   GEN fu, X, U, SUnits = bnf_get_sunits(bnf);
    3292             :   long i, l;
    3293       37135 :   if (!SUnits) return NULL;
    3294       36904 :   X = gel(SUnits,1);
    3295       36904 :   U = gel(SUnits,2); l = lg(U); fu = cgetg(l, t_VEC);
    3296       60193 :   for (i = 1; i < l; i++)
    3297       23289 :     gel(fu,i) = famat_remove_trivial(mkmat2(X, gel(U,i)));
    3298       36904 :   return fu;
    3299             : }
    3300             : /* return expanded fu if available */
    3301             : GEN
    3302      263827 : bnf_has_fu(GEN bnf)
    3303             : {
    3304      263827 :   GEN fu = obj_check(bnf, UNITS);
    3305      263823 :   if (fu) return vecsplice(fu, 1);
    3306      263028 :   fu = bnf_get_fu_nocheck(bnf);
    3307      263028 :   return (typ(fu) == t_MAT)? NULL: fu;
    3308             : }
    3309             : /* return expanded fu if available; build if cheap */
    3310             : GEN
    3311      263547 : bnf_build_cheapfu(GEN bnf)
    3312             : {
    3313             :   GEN fu, SUnits;
    3314      263547 :   if ((fu = bnf_has_fu(bnf))) return fu;
    3315         149 :   if ((SUnits = bnf_get_sunits(bnf)))
    3316             :   {
    3317         149 :     pari_sp av = avma;
    3318         149 :     long e = gexpo(real_i(bnf_get_logfu(bnf)));
    3319         149 :     set_avma(av); if (e < 13) return vecsplice(bnf_build_units(bnf), 1);
    3320             :   }
    3321          77 :   return NULL;
    3322             : }
    3323             : 
    3324             : static GEN
    3325       63776 : get_regulator(GEN A)
    3326             : {
    3327       63776 :   pari_sp av = avma;
    3328             :   GEN R;
    3329             : 
    3330       63776 :   if (lg(A) == 1) return gen_1;
    3331       48572 :   R = det( rowslice(real_i(A), 1, lgcols(A)-2) );
    3332       48572 :   setabssign(R); return gerepileuptoleaf(av, R);
    3333             : }
    3334             : 
    3335             : /* return corrected archimedian components for elts of x (vector)
    3336             :  * (= log(sigma_i(x)) - log(|Nx|) / [K:Q]) */
    3337             : static GEN
    3338          40 : get_archclean(GEN nf, GEN x, long prec, int units)
    3339             : {
    3340          40 :   long k, N, l = lg(x);
    3341          40 :   GEN M = cgetg(l, t_MAT);
    3342             : 
    3343          40 :   if (l == 1) return M;
    3344          26 :   N = nf_get_degree(nf);
    3345         114 :   for (k = 1; k < l; k++)
    3346             :   {
    3347          88 :     pari_sp av = avma;
    3348          88 :     GEN c = nf_cxlog(nf, gel(x,k), prec);
    3349          88 :     if (!c || (!units && !(c = cleanarch(c, N, NULL,prec)))) return NULL;
    3350          88 :     gel(M,k) = gerepilecopy(av, c);
    3351             :   }
    3352          26 :   return M;
    3353             : }
    3354             : static void
    3355          77 : Sunits_archclean(GEN nf, GEN Sunits, GEN *pmun, GEN *pC, long prec)
    3356             : {
    3357          77 :   GEN ipi, M, X = gel(Sunits,1), U = gel(Sunits,2), G = gel(Sunits,3);
    3358          77 :   long k, N = nf_get_degree(nf), l = lg(X);
    3359             : 
    3360          77 :   M = cgetg(l, t_MAT);
    3361        3640 :   for (k = 1; k < l; k++)
    3362        3563 :     if (!(gel(M,k) = nf_cxlog(nf, gel(X,k), prec))) return;
    3363          77 :   ipi = invr(mppi(prec));
    3364          77 :   *pmun = cleanarch(RgM_ZM_mul(M, U), N, ipi, prec); /* not cleanarchunit ! */
    3365          77 :   if (*pmun) *pC = cleanarch(RgM_ZM_mul(M, G), N, ipi, prec);
    3366             : }
    3367             : 
    3368             : GEN
    3369          97 : bnfnewprec_shallow(GEN bnf, long prec)
    3370             : {
    3371          97 :   GEN nf0 = bnf_get_nf(bnf), nf, v, fu, matal, y, A, C;
    3372          97 :   GEN Sunits = bnf_get_sunits(bnf), Ur, Ga, Ge, M1, M2;
    3373          97 :   long r1, r2, prec0 = prec;
    3374             : 
    3375          97 :   nf_get_sign(nf0, &r1, &r2);
    3376          97 :   if (Sunits)
    3377             :   {
    3378          77 :     fu = matal = NULL;
    3379          77 :     prec += nbits2extraprec(gexpo(Sunits));
    3380             :   }
    3381             :   else
    3382             :   {
    3383          20 :     fu = bnf_build_units(bnf);
    3384          20 :     fu = vecslice(fu, 2, lg(fu)-1);
    3385          20 :     if (r1 + r2 > 1) {
    3386          13 :       long e = gexpo(bnf_get_logfu(bnf)) + 1 - TWOPOTBITS_IN_LONG;
    3387          13 :       if (e >= 0) prec += nbits2extraprec(e);
    3388             :     }
    3389          20 :     matal = bnf_build_matalpha(bnf);
    3390             :   }
    3391             : 
    3392          97 :   if (DEBUGLEVEL && prec0 != prec) pari_warn(warnprec,"bnfnewprec",prec);
    3393          97 :   for(C = NULL;;)
    3394           0 :   {
    3395          97 :     pari_sp av = avma;
    3396          97 :     nf = nfnewprec_shallow(nf0,prec);
    3397          97 :     if (Sunits)
    3398          77 :       Sunits_archclean(nf, Sunits, &A, &C, prec);
    3399             :     else
    3400             :     {
    3401          20 :       A = get_archclean(nf, fu, prec, 1);
    3402          20 :       if (A) C = get_archclean(nf, matal, prec, 0);
    3403             :     }
    3404          97 :     if (C) break;
    3405           0 :     set_avma(av); prec = precdbl(prec);
    3406           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"bnfnewprec(extra)",prec);
    3407             :   }
    3408          97 :   y = leafcopy(bnf);
    3409          97 :   gel(y,3) = A;
    3410          97 :   gel(y,4) = C;
    3411          97 :   gel(y,7) = nf;
    3412          97 :   gel(y,8) = v = leafcopy(gel(bnf,8));
    3413          97 :   gel(v,2) = get_regulator(A);
    3414          97 :   v = gel(bnf,9);
    3415          97 :   if (lg(v) < 7) pari_err_TYPE("bnfnewprec [obsolete bnf format]", bnf);
    3416          97 :   Ur = gel(v,1);
    3417          97 :   Ge = gel(v,4);
    3418          97 :   Ga = nfV_cxlog(nf, Ge, prec);
    3419          97 :   M1 = gel(v,5);
    3420          97 :   M2 = gel(v,6);
    3421          97 :   gel(y,9) = get_clg2(bnf_get_cyc(bnf), Ga, C, Ur, Ge, M1, M2);
    3422          97 :   return y;
    3423             : }
    3424             : GEN
    3425           7 : bnfnewprec(GEN bnf, long prec)
    3426             : {
    3427           7 :   pari_sp av = avma;
    3428           7 :   return gerepilecopy(av, bnfnewprec_shallow(checkbnf(bnf), prec));
    3429             : }
    3430             : 
    3431             : GEN
    3432           0 : bnrnewprec_shallow(GEN bnr, long prec)
    3433             : {
    3434           0 :   GEN y = cgetg(7,t_VEC);
    3435             :   long i;
    3436           0 :   gel(y,1) = bnfnewprec_shallow(bnr_get_bnf(bnr), prec);
    3437           0 :   for (i=2; i<7; i++) gel(y,i) = gel(bnr,i);
    3438           0 :   return y;
    3439             : }
    3440             : GEN
    3441           7 : bnrnewprec(GEN bnr, long prec)
    3442             : {
    3443           7 :   GEN y = cgetg(7,t_VEC);
    3444             :   long i;
    3445           7 :   checkbnr(bnr);
    3446           7 :   gel(y,1) = bnfnewprec(bnr_get_bnf(bnr), prec);
    3447          42 :   for (i=2; i<7; i++) gel(y,i) = gcopy(gel(bnr,i));
    3448           7 :   return y;
    3449             : }
    3450             : 
    3451             : static GEN
    3452       64834 : buchall_end(GEN nf,GEN res, GEN clg2, GEN W, GEN B, GEN A, GEN C,GEN Vbase)
    3453             : {
    3454       64834 :   GEN z = obj_init(9, 3);
    3455       64834 :   gel(z,1) = W;
    3456       64834 :   gel(z,2) = B;
    3457       64834 :   gel(z,3) = A;
    3458       64834 :   gel(z,4) = C;
    3459       64834 :   gel(z,5) = Vbase;
    3460       64834 :   gel(z,6) = gen_0;
    3461       64834 :   gel(z,7) = nf;
    3462       64834 :   gel(z,8) = res;
    3463       64834 :   gel(z,9) = clg2;
    3464       64834 :   return z;
    3465             : }
    3466             : 
    3467             : GEN
    3468        2611 : bnfinit0(GEN P, long flag, GEN data, long prec)
    3469             : {
    3470        2611 :   double c1 = 0., c2 = 0.;
    3471        2611 :   long fl, relpid = degpol(P)==2 ? 0: BNF_RELPID;
    3472             : 
    3473        2611 :   if (data)
    3474             :   {
    3475          21 :     long lx = lg(data);
    3476          21 :     if (typ(data) != t_VEC || lx > 5) pari_err_TYPE("bnfinit",data);
    3477          21 :     switch(lx)
    3478             :     {
    3479           0 :       case 4: relpid = itos(gel(data,3));
    3480          14 :       case 3: c2 = gtodouble(gel(data,2));
    3481          21 :       case 2: c1 = gtodouble(gel(data,1));
    3482             :     }
    3483             :   }
    3484        2611 :   switch(flag)
    3485             :   {
    3486        1764 :     case 2:
    3487        1764 :     case 0: fl = 0; break;
    3488         847 :     case 1: fl = nf_FORCE; break;
    3489           0 :     default: pari_err_FLAG("bnfinit");
    3490             :       return NULL; /* LCOV_EXCL_LINE */
    3491             :   }
    3492        2611 :   return Buchall_param(P, c1, c2, relpid, fl, prec);
    3493             : }
    3494             : GEN
    3495       62222 : Buchall(GEN P, long flag, long prec)
    3496       62222 : { return Buchall_param(P, 0., 0., BNF_RELPID, flag & nf_FORCE, prec); }
    3497             : 
    3498             : static GEN
    3499        1155 : Buchall_deg1(GEN nf)
    3500             : {
    3501        1155 :   GEN v = cgetg(1,t_VEC), m = cgetg(1,t_MAT);
    3502        1155 :   GEN res, W, A, B, C, Vbase = cgetg(1,t_COL);
    3503        1155 :   GEN fu = v, R = gen_1, zu = mkvec2(gen_2, gen_m1);
    3504        1155 :   GEN clg1 = mkvec3(gen_1,v,v), clg2 = mkvecn(6, m,m,m,v,m,m);
    3505             : 
    3506        1155 :   W = A = B = C = m; res = mkvec5(clg1, R, gen_1, zu, fu);
    3507        1155 :   return buchall_end(nf,res,clg2,W,B,A,C,Vbase);
    3508             : }
    3509             : 
    3510             : /* return (small set of) indices of columns generating the same lattice as x.
    3511             :  * Assume HNF(x) is inexpensive (few rows, many columns).
    3512             :  * Dichotomy approach since interesting columns may be at the very end */
    3513             : GEN
    3514       63678 : extract_full_lattice(GEN x)
    3515             : {
    3516       63678 :   long dj, j, k, l = lg(x);
    3517             :   GEN h, h2, H, v;
    3518             : 
    3519       63678 :   if (l < 200) return NULL; /* not worth it */
    3520             : 
    3521           2 :   v = vecsmalltrunc_init(l);
    3522           2 :   H = ZM_hnf(x);
    3523           2 :   h = cgetg(1, t_MAT);
    3524           2 :   dj = 1;
    3525          86 :   for (j = 1; j < l; )
    3526             :   {
    3527          86 :     pari_sp av = avma;
    3528          86 :     long lv = lg(v);
    3529             : 
    3530         290 :     for (k = 0; k < dj; k++) v[lv+k] = j+k;
    3531          86 :     setlg(v, lv + dj);
    3532          86 :     h2 = ZM_hnf(vecpermute(x, v));
    3533          86 :     if (ZM_equal(h, h2))
    3534             :     { /* these dj columns can be eliminated */
    3535          34 :       set_avma(av); setlg(v, lv);
    3536          34 :       j += dj;
    3537          34 :       if (j >= l) break;
    3538          34 :       dj <<= 1;
    3539          34 :       if (j + dj >= l) { dj = (l - j) >> 1; if (!dj) dj = 1; }
    3540             :     }
    3541          52 :     else if (dj > 1)
    3542             :     { /* at least one interesting column, try with first half of this set */
    3543          34 :       set_avma(av); setlg(v, lv);
    3544          34 :       dj >>= 1; /* > 0 */
    3545             :     }
    3546             :     else
    3547             :     { /* this column should be kept */
    3548          18 :       if (ZM_equal(h2, H)) break;
    3549          16 :       h = h2; j++;
    3550             :     }
    3551             :   }
    3552           2 :   return v;
    3553             : }
    3554             : 
    3555             : static void
    3556       63749 : init_rel(RELCACHE_t *cache, FB_t *F, long add_need)
    3557             : {
    3558       63749 :   const long n = F->KC + add_need; /* expected # of needed relations */
    3559             :   long i, j, k, p;
    3560             :   GEN c, P;
    3561             :   GEN R;
    3562             : 
    3563       63749 :   if (DEBUGLEVEL) err_printf("KCZ = %ld, KC = %ld, n = %ld\n", F->KCZ,F->KC,n);
    3564       63749 :   reallocate(cache, 10*n + 50); /* make room for lots of relations */
    3565       63749 :   cache->chk = cache->base;
    3566       63749 :   cache->end = cache->base + n;
    3567       63749 :   cache->relsup = add_need;
    3568       63749 :   cache->last = cache->base;
    3569       63749 :   cache->missing = lg(cache->basis) - 1;
    3570      303811 :   for (i = 1; i <= F->KCZ; i++)
    3571             :   { /* trivial relations (p) = prod P^e */
    3572      240063 :     p = F->FB[i]; P = gel(F->LV,p);
    3573      240063 :     if (!isclone(P)) continue;
    3574             : 
    3575             :     /* all prime divisors in FB */
    3576      167630 :     c = zero_Flv(F->KC); k = F->iLP[p];
    3577      167630 :     R = c; c += k;
    3578      534952 :     for (j = lg(P)-1; j; j--) c[j] = pr_get_e(gel(P,j));
    3579      167630 :     add_rel(cache, F, R, k+1, pr_get_p(gel(P,1)), 0);
    3580             :   }
    3581       63748 : }
    3582             : 
    3583             : /* Let z = \zeta_n in nf. List of not-obviously-dependent generators for
    3584             :  * cyclotomic units modulo torsion in Q(z) [independent when n a prime power]:
    3585             :  * - z^a - 1,  n/(a,n) not a prime power, a \nmid n unless a=1,  1 <= a < n/2
    3586             :  * - (Z^a - 1)/(Z - 1),  p^k || n, Z = z^{n/p^k}, (p,a) = 1, 1 < a <= (p^k-1)/2
    3587             :  */
    3588             : GEN
    3589       63748 : nfcyclotomicunits(GEN nf, GEN zu)
    3590             : {
    3591       63748 :   long n = itos(gel(zu, 1)), n2, lP, i, a;
    3592             :   GEN z, fa, P, E, L, mz, powz;
    3593       63748 :   if (n <= 6) return cgetg(1, t_VEC);
    3594             : 
    3595        1897 :   z = algtobasis(nf,gel(zu, 2));
    3596        1897 :   if ((n & 3) == 2) { n = n >> 1; z = ZC_neg(z); } /* ensure n != 2 (mod 4) */
    3597        1897 :   n2 = n/2;
    3598        1897 :   mz = zk_multable(nf, z); /* multiplication by z */
    3599        1897 :   powz = cgetg(n2, t_VEC); gel(powz,1) = z;
    3600        6237 :   for (i = 2; i < n2; i++) gel(powz,i) = ZM_ZC_mul(mz, gel(powz,i-1));
    3601             :   /* powz[i] = z^i */
    3602             : 
    3603        1897 :   L = vectrunc_init(n);
    3604        1897 :   fa = factoru(n);
    3605        1897 :   P = gel(fa,1); lP = lg(P);
    3606        1897 :   E = gel(fa,2);
    3607        4578 :   for (i = 1; i < lP; i++)
    3608             :   { /* second kind */
    3609        2681 :     long p = P[i], k = E[i], pk = upowuu(p,k), pk2 = (pk-1) / 2;
    3610        2681 :     GEN u = gen_1;
    3611        4935 :     for (a = 2; a <= pk2; a++)
    3612             :     {
    3613        2254 :       u = nfadd(nf, u, gel(powz, (n/pk) * (a-1))); /* = (Z^a-1)/(Z-1) */
    3614        2254 :       if (a % p) vectrunc_append(L, u);
    3615             :     }
    3616             :   }
    3617        6104 :   if (lP > 2) for (a = 1; a < n2; a++)
    3618             :   { /* first kind, when n not a prime power */
    3619             :     ulong p;
    3620        4207 :     if (a > 1 && (n % a == 0 || uisprimepower(n/ugcd(a,n), &p))) continue;
    3621        1848 :     vectrunc_append(L, nfadd(nf, gel(powz, a), gen_m1));
    3622             :   }
    3623        1897 :   return L;
    3624             : }
    3625             : static void
    3626       63748 : add_cyclotomic_units(GEN nf, GEN zu, RELCACHE_t *cache, FB_t *F)
    3627             : {
    3628       63748 :   pari_sp av = avma;
    3629       63748 :   GEN L = nfcyclotomicunits(nf, zu);
    3630       63749 :   long i, l = lg(L);
    3631       63749 :   if (l > 1)
    3632             :   {
    3633        1897 :     GEN R = zero_Flv(F->KC);
    3634        5901 :     for(i = 1; i < l; i++) add_rel(cache, F, R, F->KC+1, gel(L,i), 0);
    3635             :   }
    3636       63749 :   set_avma(av);
    3637       63748 : }
    3638             : 
    3639             : static GEN
    3640       95119 : trim_list(FB_t *F)
    3641             : {
    3642       95119 :   pari_sp av = avma;
    3643       95119 :   GEN v, L_jid = F->L_jid, minidx = F->minidx, present = zero_Flv(F->KC);
    3644       95119 :   long i, j, imax = minss(lg(L_jid), F->KC + 1);
    3645             : 
    3646       95119 :   v = cgetg(imax, t_VECSMALL);
    3647     1177240 :   for (i = j = 1; i < imax; i++)
    3648             :   {
    3649     1082120 :     long k = minidx[ L_jid[i] ];
    3650     1082120 :     if (!present[k]) { v[j++] = L_jid[i]; present[k] = 1; }
    3651             :   }
    3652       95120 :   setlg(v, j); return gerepileuptoleaf(av, v);
    3653             : }
    3654             : 
    3655             : /* x t_INT or primitive ZC */
    3656             : static void
    3657        1599 : try_elt(RELCACHE_t *cache, FB_t *F, GEN nf, GEN x, FACT *fact)
    3658             : {
    3659        1599 :   pari_sp av = avma;
    3660             :   long nz;
    3661             :   GEN R;
    3662             : 
    3663        1599 :   if (typ(x) == t_INT /* 2nd path can't fail */
    3664        1599 :      || !can_factor(F, nf, NULL, x, nfnorm(nf, x), fact)) return;
    3665             :   /* smooth element */
    3666        1382 :   R = set_fact(F, fact, NULL, &nz);
    3667             :   /* make sure we get maximal rank first, then allow all relations */
    3668        1382 :   (void)add_rel(cache, F, R, nz, x, 0);
    3669        1382 :   set_avma(av);
    3670             : }
    3671             : 
    3672             : static void
    3673       28567 : matenlarge(GEN C, long h)
    3674             : {
    3675       28567 :   GEN _0 = zerocol(h);
    3676             :   long i;
    3677      971225 :   for (i = lg(C); --i; ) gel(C,i) = shallowconcat(gel(C,i), _0);
    3678       28567 : }
    3679             : 
    3680             : /* E = floating point embeddings */
    3681             : static GEN
    3682       28567 : matbotidembs(RELCACHE_t *cache, GEN E)
    3683             : {
    3684       28567 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3685       28567 :   long j, d = h - w, hE = nbrows(E);
    3686       28567 :   GEN y = cgetg(w+1,t_MAT), _0 = zerocol(h);
    3687      123373 :   for (j = 1; j <= w; j++)
    3688             :   {
    3689       94806 :     GEN c = shallowconcat(gel(E,j), _0);
    3690       94806 :     if (d + j >= 1) gel(c, d + j + hE) = gen_1;
    3691       94806 :     gel(y,j) = c;
    3692             :   }
    3693       28567 :   return y;
    3694             : }
    3695             : static GEN
    3696       62139 : matbotid(RELCACHE_t *cache)
    3697             : {
    3698       62139 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3699       62139 :   long j, d = h - w;
    3700       62139 :   GEN y = cgetg(w+1,t_MAT);
    3701      898847 :   for (j = 1; j <= w; j++)
    3702             :   {
    3703      836708 :     GEN c = zerocol(h);
    3704      836708 :     if (d + j >= 1) gel(c, d + j) = gen_1;
    3705      836708 :     gel(y,j) = c;
    3706             :   }
    3707       62139 :   return y;
    3708             : }
    3709             : 
    3710             : static long
    3711          80 : myprecdbl(long prec, GEN C)
    3712             : {
    3713          80 :   long p = prec < 1280? precdbl(prec): (long)(prec * 1.5);
    3714          80 :   if (C) p = maxss(p, minss(3*p, prec + nbits2extraprec(gexpo(C))));
    3715          80 :   return p;
    3716             : }
    3717             : 
    3718             : static GEN
    3719       56727 : _nfnewprec(GEN nf, long prec, long *isclone)
    3720             : {
    3721       56727 :   GEN NF = gclone(nfnewprec_shallow(nf, prec));
    3722       56727 :   if (*isclone) gunclone(nf);
    3723       56727 :   *isclone = 1; return NF;
    3724             : }
    3725             : 
    3726             : /* Nrelid = nb relations per ideal, possibly 0. If flag is set, keep data in
    3727             :  * algebraic form. */
    3728             : GEN
    3729       64842 : Buchall_param(GEN P, double cbach, double cbach2, long Nrelid, long flag, long prec)
    3730             : {
    3731             :   pari_timer T;
    3732       64842 :   pari_sp av0 = avma, av, av2;
    3733             :   long PREC, N, R1, R2, RU, low, high, LIMC0, LIMC, LIMC2, LIMCMAX, zc, i;
    3734       64842 :   long LIMres, bit = 0, flag_nfinit = 0, nfisclone = 0;
    3735       64842 :   long nreldep, sfb_trials, need, old_need, precdouble = 0, TRIES = 0;
    3736             :   long done_small, small_fail, fail_limit, squash_index;
    3737             :   double LOGD, LOGD2, lim;
    3738       64842 :   GEN computed = NULL, fu = NULL, zu, nf, D, A, W, R, h, Ce, PERM;
    3739             :   GEN small_multiplier, auts, cyclic, embs, SUnits;
    3740             :   GEN res, L, invhr, B, C, lambda, dep, clg1, clg2, Vbase;
    3741       64842 :   const char *precpb = NULL;
    3742       64842 :   REL_t *old_cache = NULL;
    3743             :   nfmaxord_t nfT;
    3744             :   RELCACHE_t cache;
    3745             :   FB_t F;
    3746             :   GRHcheck_t GRHcheck;
    3747             :   FACT *fact;
    3748             : 
    3749       64842 :   if (DEBUGLEVEL) timer_start(&T);
    3750       64842 :   P = get_nfpol(P, &nf);
    3751       64826 :   if (nf)
    3752        3556 :     D = nf_get_disc(nf);
    3753             :   else
    3754             :   {
    3755       61270 :     nfinit_basic(&nfT, P);
    3756       61270 :     D = nfT.dK;
    3757       61270 :     if (!ZX_is_monic(nfT.T0))
    3758             :     {
    3759          14 :       pari_warn(warner,"nonmonic polynomial in bnfinit, using polredbest");
    3760          14 :       flag_nfinit = nf_RED;
    3761             :     }
    3762             :   }
    3763       64826 :   PREC = maxss(DEFAULTPREC, prec);
    3764       64826 :   N = degpol(P);
    3765       64826 :   if (N <= 1)
    3766             :   {
    3767        1155 :     if (!nf) nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3768        1155 :     return gerepilecopy(av0, Buchall_deg1(nf));
    3769             :   }
    3770       63671 :   D = absi_shallow(D);
    3771       63671 :   LOGD = dbllog2(D) * M_LN2;
    3772       63671 :   LOGD2 = LOGD*LOGD;
    3773       63671 :   LIMCMAX = (long)(4.*LOGD2);
    3774       63671 :   if (nf) PREC = maxss(PREC, nf_get_prec(nf));
    3775       63671 :   PREC = maxss(PREC, nbits2prec((long)(LOGD2 * 0.02) + N*N));
    3776       63671 :   if (DEBUGLEVEL) err_printf("PREC = %ld\n", PREC);
    3777       63671 :   if (!nf)
    3778       60290 :     nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3779        3381 :   else if (nf_get_prec(nf) < PREC)
    3780         161 :     nf = nfnewprec_shallow(nf, PREC);
    3781       63678 :   zu = nfrootsof1(nf);
    3782       63679 :   gel(zu,2) = nf_to_scalar_or_alg(nf, gel(zu,2));
    3783             : 
    3784       63679 :   nf_get_sign(nf, &R1, &R2); RU = R1+R2;
    3785       63679 :   auts = automorphism_matrices(nf, &cyclic);
    3786       63678 :   F.embperm = automorphism_perms(nf_get_M(nf), auts, cyclic, R1, R2, N);
    3787       63676 :   if (DEBUGLEVEL)
    3788             :   {
    3789           0 :     timer_printf(&T, "nfinit & nfrootsof1");
    3790           0 :     err_printf("%s bnf: R1 = %ld, R2 = %ld\nD = %Ps\n",
    3791             :                flag? "Algebraic": "Floating point", R1,R2, D);
    3792             :   }
    3793       63676 :   if (LOGD < 20.)
    3794             :   { /* tiny disc, Minkowski may be smaller than Bach */
    3795       62227 :     lim = exp(-N + R2 * log(4/M_PI) + LOGD/2) * sqrt(2*M_PI*N);
    3796       62227 :     if (lim < 3) lim = 3;
    3797             :   }
    3798             :   else /* to be ignored */
    3799        1449 :     lim = -1;
    3800       63676 :   if (cbach > 12.) {
    3801           0 :     if (cbach2 < cbach) cbach2 = cbach;
    3802           0 :     cbach = 12.;
    3803             :   }
    3804       63676 :   if (cbach < 0.)
    3805           0 :     pari_err_DOMAIN("Buchall","Bach constant","<",gen_0,dbltor(cbach));
    3806             : 
    3807       63676 :   cache.base = NULL; F.subFB = NULL; F.LP = NULL; SUnits = Ce = NULL;
    3808       63676 :   init_GRHcheck(&GRHcheck, N, R1, LOGD);
    3809       63677 :   high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
    3810      310578 :   while (!GRHchk(nf, &GRHcheck, high)) { low = high; high *= 2; }
    3811      246946 :   while (high - low > 1)
    3812             :   {
    3813      183266 :     long test = (low+high)/2;
    3814      183266 :     if (GRHchk(nf, &GRHcheck, test)) high = test; else low = test;
    3815             :   }
    3816       63680 :   LIMC2 = (high == LIMC0+1 && GRHchk(nf, &GRHcheck, LIMC0))? LIMC0: high;
    3817       63680 :   if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
    3818             :   /* Assuming GRH, {P, NP <= LIMC2} generate Cl(K) */
    3819       63680 :   if (DEBUGLEVEL) err_printf("LIMC2 = %ld\n", LIMC2);
    3820       63679 :   LIMC0 = (long)(cbach*LOGD2); /* initial value for LIMC */
    3821       63679 :   LIMC = cbach? LIMC0: LIMC2; /* use {P, NP <= LIMC} as a factorbase */
    3822       63679 :   LIMC = maxss(LIMC, nthideal(&GRHcheck, nf, N));
    3823       63679 :   if (DEBUGLEVEL) timer_printf(&T, "computing Bach constant");
    3824       63679 :   LIMres = primeneeded(N, R1, R2, LOGD);
    3825       63679 :   cache_prime_dec(&GRHcheck, LIMres, nf);
    3826             :   /* invhr ~ 2^r1 (2pi)^r2 / sqrt(D) w * Res(zeta_K, s=1) = 1 / hR */
    3827      127355 :   invhr = gmul(gdiv(gmul2n(powru(mppi(DEFAULTPREC), R2), RU),
    3828       63677 :               mulri(gsqrt(D,DEFAULTPREC),gel(zu,1))),
    3829             :               compute_invres(&GRHcheck, LIMres));
    3830       63676 :   if (DEBUGLEVEL) timer_printf(&T, "computing inverse of hR");
    3831       63676 :   av = avma;
    3832             : 
    3833       65923 : START:
    3834       65923 :   if (DEBUGLEVEL) timer_start(&T);
    3835       65923 :   if (TRIES) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
    3836       65923 :   if (DEBUGLEVEL && LIMC > LIMC0)
    3837           0 :     err_printf("%s*** Bach constant: %f\n", TRIES?"\n":"", LIMC/LOGD2);
    3838       65923 :   if (cache.base)
    3839             :   {
    3840             :     REL_t *rel;
    3841        3717 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    3842        3647 :       if (rel->m) i++;
    3843          70 :     computed = cgetg(i, t_VEC);
    3844        3717 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    3845        3647 :       if (rel->m) gel(computed, i++) = rel->m;
    3846          70 :     computed = gclone(computed); delete_cache(&cache);
    3847             :   }
    3848       65923 :   TRIES++; set_avma(av);
    3849       65923 :   if (F.LP) delete_FB(&F);
    3850       65923 :   if (LIMC2 < LIMC) LIMC2 = LIMC;
    3851       65923 :   if (DEBUGLEVEL) { err_printf("LIMC = %ld, LIMC2 = %ld\n",LIMC,LIMC2); }
    3852             : 
    3853       65923 :   FBgen(&F, nf, N, LIMC, LIMC2, &GRHcheck);
    3854       65925 :   if (!F.KC) goto START;
    3855       65925 :   av = avma;
    3856       65925 :   subFBgen(&F,auts,cyclic,lim < 0? LIMC2: mindd(lim,LIMC2),MINSFB);
    3857       65926 :   if (lg(F.subFB) == 1) goto START;
    3858       63749 :   if (DEBUGLEVEL)
    3859           0 :     timer_printf(&T, "factorbase (#subFB = %ld) and ideal permutations",
    3860           0 :                      lg(F.subFB)-1);
    3861             : 
    3862       63749 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    3863       63749 :   PERM = leafcopy(F.perm); /* to be restored in case of precision increase */
    3864       63749 :   cache.basis = zero_Flm_copy(F.KC,F.KC);
    3865       63749 :   small_multiplier = zero_Flv(F.KC);
    3866       63749 :   done_small = small_fail = squash_index = zc = sfb_trials = nreldep = 0;
    3867       63749 :   fail_limit = F.KC + 1;
    3868       63749 :   W = A = R = NULL;
    3869       63749 :   av2 = avma;
    3870       63749 :   init_rel(&cache, &F, RELSUP + RU-1);
    3871       63748 :   old_need = need = cache.end - cache.last;
    3872       63748 :   add_cyclotomic_units(nf, zu, &cache, &F);
    3873       63748 :   if (DEBUGLEVEL) err_printf("\n");
    3874       63748 :   cache.end = cache.last + need;
    3875             : 
    3876       63748 :   if (computed)
    3877             :   {
    3878        1669 :     for (i = 1; i < lg(computed); i++)
    3879        1599 :       try_elt(&cache, &F, nf, gel(computed, i), fact);
    3880          70 :     gunclone(computed);
    3881          70 :     if (DEBUGLEVEL && i > 1)
    3882           0 :       timer_printf(&T, "including already computed relations");
    3883          70 :     need = 0;
    3884             :   }
    3885             : 
    3886             :   do
    3887             :   {
    3888             :     GEN Ar, C0;
    3889             :     do
    3890             :     {
    3891       95269 :       pari_sp av4 = avma;
    3892       95269 :       if (need > 0)
    3893             :       {
    3894       95119 :         long oneed = cache.end - cache.last;
    3895             :         /* Test below can be true if small_norm did not find enough linearly
    3896             :          * dependent relations */
    3897       95119 :         if (need < oneed) need = oneed;
    3898       95119 :         pre_allocate(&cache, need+lg(auts)-1+(R ? lg(W)-1 : 0));
    3899       95119 :         cache.end = cache.last + need;
    3900       95119 :         F.L_jid = trim_list(&F);
    3901             :       }
    3902       95269 :       if (need > 0 && Nrelid > 0 && (done_small <= F.KC+1 || A) &&
    3903       92203 :           small_fail <= fail_limit &&
    3904       92203 :           cache.last < cache.base + 2*F.KC+2*RU+RELSUP /* heuristic */)
    3905             :       {
    3906       89163 :         long j, k, LIE = (R && lg(W) > 1 && (done_small % 2));
    3907       89163 :         REL_t *last = cache.last;
    3908       89163 :         pari_sp av3 = avma;
    3909       89163 :         if (LIE)
    3910             :         { /* We have full rank for class group and unit. The following tries to
    3911             :            * improve the prime group lattice by looking for relations involving
    3912             :            * the primes generating the class group. */
    3913        3529 :           long n = lg(W)-1; /* need n relations to squash the class group */
    3914        3529 :           F.L_jid = vecslice(F.perm, 1, n);
    3915        3529 :           cache.end = cache.last + n;
    3916             :           /* Lie to the add_rel subsystem: pretend we miss relations involving
    3917             :            * the primes generating the class group (and only those). */
    3918        3529 :           cache.missing = n;
    3919       10800 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 0;
    3920             :         }
    3921       89163 :         j = done_small % (F.KC+1);
    3922       89163 :         if (j && !A)
    3923             :         { /* Prevent considering both P_iP_j and P_jP_i in small_norm */
    3924             :           /* Not all elements end up in F.L_jid (eliminated by hnfspec/add or
    3925             :            * by trim_list): keep track of which ideals are being considered
    3926             :            * at each run. */
    3927         421 :           long mj = small_multiplier[j];
    3928        7087 :           for (i = k = 1; i < lg(F.L_jid); i++)
    3929        6666 :             if (F.L_jid[i] > mj)
    3930             :             {
    3931        6666 :               small_multiplier[F.L_jid[i]] = j;
    3932        6666 :               F.L_jid[k++] = F.L_jid[i];
    3933             :             }
    3934         421 :           setlg(F.L_jid, k);
    3935             :         }
    3936       89163 :         if (lg(F.L_jid) > 1) small_norm(&cache, &F, nf, Nrelid, fact, j);
    3937       89164 :         F.L_jid = F.perm; set_avma(av3);
    3938       89164 :         if (!A && cache.last != last) small_fail = 0; else small_fail++;
    3939       89164 :         if (LIE)
    3940             :         { /* restore add_rel subsystem: undo above lie */
    3941        3529 :           long n = lg(W) - 1;
    3942       10800 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 1;
    3943        3529 :           cache.missing = 0;
    3944             :         }
    3945       89164 :         cache.end = cache.last;
    3946       89164 :         done_small++;
    3947       89164 :         need = F.sfb_chg = 0;
    3948             :       }
    3949       95270 :       if (need > 0)
    3950             :       { /* Random relations */
    3951        5956 :         if (++nreldep > F.MAXDEPSIZESFB) {
    3952          14 :           if (++sfb_trials > SFB_MAX && LIMC < LIMCMAX/2) goto START;
    3953          14 :           F.sfb_chg = sfb_INCREASE;
    3954          14 :           nreldep = 0;
    3955             :         }
    3956        5942 :         else if (!(nreldep % F.MAXDEPSFB))
    3957        1834 :           F.sfb_chg = sfb_CHANGE;
    3958        5956 :         if (F.sfb_chg && !subFB_change(&F)) goto START;
    3959        5886 :         rnd_rel(&cache, &F, nf, fact);
    3960        5886 :         F.L_jid = F.perm;
    3961             :       }
    3962       95200 :       if (DEBUGLEVEL) timer_start(&T);
    3963       95200 :       if (precpb)
    3964             :       {
    3965             :         REL_t *rel;
    3966          80 :         if (DEBUGLEVEL)
    3967             :         {
    3968           0 :           char str[64]; sprintf(str,"Buchall_param (%s)",precpb);
    3969           0 :           pari_warn(warnprec,str,PREC);
    3970             :         }
    3971          80 :         nf = _nfnewprec(nf, PREC, &nfisclone);
    3972          80 :         precdouble++; precpb = NULL;
    3973             : 
    3974          80 :         if (flag)
    3975             :         { /* recompute embs only, no need to redo HNF */
    3976          38 :           long j, le = lg(embs), lC = lg(C);
    3977          38 :           GEN E, M = nf_get_M(nf);
    3978          38 :           set_avma(av4);
    3979       12611 :           for (rel = cache.base+1, i = 1; i < le; i++,rel++)
    3980       12573 :             gel(embs,i) = rel_embed(rel, &F, embs, i, M, RU, R1, PREC);
    3981          38 :           E = RgM_ZM_mul(embs, rowslice(C, RU+1, nbrows(C)));
    3982       12611 :           for (j = 1; j < lC; j++)
    3983       65595 :             for (i = 1; i <= RU; i++) gcoeff(C,i,j) = gcoeff(E,i,j);
    3984          38 :           av4 = avma;
    3985             :         }
    3986             :         else
    3987             :         { /* recompute embs + HNF */
    3988       10318 :           for(i = 1; i < lg(PERM); i++) F.perm[i] = PERM[i];
    3989          42 :           cache.chk = cache.base;
    3990          42 :           W = NULL;
    3991             :         }
    3992          80 :         if (DEBUGLEVEL) timer_printf(&T, "increasing accuracy");
    3993             :       }
    3994       95200 :       set_avma(av4);
    3995       95200 :       if (cache.chk != cache.last)
    3996             :       { /* Reduce relation matrices */
    3997       95084 :         long l = cache.last - cache.chk + 1, j;
    3998       95084 :         GEN mat = cgetg(l, t_MAT);
    3999             :         REL_t *rel;
    4000             : 
    4001     1080135 :         for (j=1,rel = cache.chk + 1; j < l; rel++,j++) gel(mat,j) = rel->R;
    4002       95084 :         if (!flag || W)
    4003             :         {
    4004       32945 :           embs = get_embs(&F, &cache, nf, embs, PREC);
    4005       32945 :           if (DEBUGLEVEL && timer_get(&T) > 1)
    4006           0 :             timer_printf(&T, "floating point embeddings");
    4007             :         }
    4008       95084 :         if (!W)
    4009             :         { /* never reduced before */
    4010       63791 :           C = flag? matbotid(&cache): embs;
    4011       63791 :           W = hnfspec_i(mat, F.perm, &dep, &B, &C, F.subFB ? lg(F.subFB)-1:0);
    4012       63791 :           if (DEBUGLEVEL)
    4013           0 :             timer_printf(&T, "hnfspec [%ld x %ld]", lg(F.perm)-1, l-1);
    4014       63791 :           if (flag)
    4015             :           {
    4016       62139 :             PREC += nbits2extraprec(gexpo(C));
    4017       62138 :             if (nf_get_prec(nf) < PREC) nf = _nfnewprec(nf, PREC, &nfisclone);
    4018       62139 :             embs = get_embs(&F, &cache, nf, embs, PREC);
    4019       62139 :             C = vconcat(RgM_ZM_mul(embs, C), C);
    4020             :           }
    4021       63791 :           if (DEBUGLEVEL)
    4022           0 :             timer_printf(&T, "hnfspec floating points");
    4023             :         }
    4024             :         else
    4025             :         {
    4026       31293 :           long k = lg(embs);
    4027       31293 :           GEN E = vecslice(embs, k-l+1,k-1);
    4028       31293 :           if (flag)
    4029             :           {
    4030       28567 :             E = matbotidembs(&cache, E);
    4031       28567 :             matenlarge(C, cache.last - cache.chk);
    4032             :           }
    4033       31293 :           W = hnfadd_i(W, F.perm, &dep, &B, &C, mat, E);
    4034       31293 :           if (DEBUGLEVEL)
    4035           0 :             timer_printf(&T, "hnfadd (%ld + %ld)", l-1, lg(dep)-1);
    4036             :         }
    4037       95084 :         gerepileall(av2, 5, &W,&C,&B,&dep,&embs);
    4038       95084 :         cache.chk = cache.last;
    4039             :       }
    4040         116 :       else if (!W)
    4041             :       {
    4042           0 :         need = old_need;
    4043           0 :         F.L_jid = vecslice(F.perm, 1, need);
    4044           0 :         continue;
    4045             :       }
    4046       95200 :       need = F.KC - (lg(W)-1) - (lg(B)-1);
    4047       95200 :       if (!need && cache.missing)
    4048             :       { /* The test above will never be true except if 27449|class number.
    4049             :          * Ensure that if we have maximal rank for the ideal lattice, then
    4050             :          * cache.missing == 0. */
    4051          14 :         for (i = 1; cache.missing; i++)
    4052           7 :           if (!mael(cache.basis, i, i))
    4053             :           {
    4054             :             long j;
    4055           7 :             cache.missing--; mael(cache.basis, i, i) = 1;
    4056         427 :             for (j = i+1; j <= F.KC; j++) mael(cache.basis, j, i) = 0;
    4057             :           }
    4058             :       }
    4059       95200 :       zc = (lg(C)-1) - (lg(B)-1) - (lg(W)-1);
    4060       95200 :       if (RU-1-zc > 0) need = minss(need + RU-1-zc, F.KC); /* for units */
    4061       95200 :       if (need)
    4062             :       { /* dependent rows */
    4063         527 :         F.L_jid = vecslice(F.perm, 1, need);
    4064         527 :         vecsmall_sort(F.L_jid);
    4065         527 :         if (need != old_need) { nreldep = 0; old_need = need; }
    4066             :       }
    4067             :       else
    4068             :       { /* If the relation lattice is too small, check will be > 1 and we will
    4069             :          * do a new run of small_norm/rnd_rel asking for 1 relation. This often
    4070             :          * gives a relation involving L_jid[1]. We rotate the first element of
    4071             :          * L_jid in order to increase the probability of finding relations that
    4072             :          * increases the lattice. */
    4073       94673 :         long j, n = lg(W) - 1;
    4074       94673 :         if (n > 1 && squash_index % n)
    4075             :         {
    4076        6278 :           F.L_jid = leafcopy(F.perm);
    4077       27241 :           for (j = 1; j <= n; j++)
    4078       20963 :             F.L_jid[j] = F.perm[1 + (j + squash_index - 1) % n];
    4079             :         }
    4080             :         else
    4081       88395 :           F.L_jid = F.perm;
    4082       94673 :         squash_index++;
    4083             :       }
    4084             :     }
    4085       95200 :     while (need);
    4086             : 
    4087       94673 :     if (!A)
    4088             :     {
    4089       63749 :       small_fail = old_need = 0;
    4090       63749 :       fail_limit = maxss(F.KC / FAIL_DIVISOR, MINFAIL);
    4091             :     }
    4092       94673 :     A = vecslice(C, 1, zc); /* cols corresponding to units */
    4093       94673 :     if (flag) A = rowslice(A, 1, RU);
    4094       94673 :     Ar = real_i(A);
    4095       94673 :     R = compute_multiple_of_R(Ar, RU, N, &need, &bit, &lambda);
    4096       94673 :     if (need < old_need) small_fail = 0;
    4097             : #if 0 /* A good idea if we are indeed stuck but needs tuning */
    4098             :     /* we have computed way more relations than should be necessary */
    4099             :     if (TRIES < 3 && LIMC < LIMCMAX / 8 &&
    4100             :                      cache.last - cache.base > 10 * F.KC) goto START;
    4101             : #endif
    4102       94673 :     old_need = need;
    4103       94673 :     if (!lambda)
    4104          11 :     { precpb = "bestappr"; PREC = myprecdbl(PREC, flag? C: NULL); continue; }
    4105       94662 :     if (!R)
    4106             :     { /* not full rank for units */
    4107       21296 :       if (!need)
    4108           0 :       { precpb = "regulator"; PREC = myprecdbl(PREC, flag? C: NULL); }
    4109       21296 :       continue;
    4110             :     }
    4111       73366 :     if (cache.last==old_cache) { need=1; continue; }
    4112       73285 :     old_cache = cache.last;
    4113       73285 :     h = ZM_det_triangular(W);
    4114       73285 :     if (DEBUGLEVEL) err_printf("\n#### Tentative class number: %Ps\n", h);
    4115       73285 :     i = compute_R(lambda, mulir(h,invhr), &L, &R);
    4116       73283 :     if (DEBUGLEVEL)
    4117             :     {
    4118           0 :       err_printf("\n");
    4119           0 :       timer_printf(&T, "computing regulator and check");
    4120             :     }
    4121       73283 :     switch(i)
    4122             :     {
    4123        9537 :       case fupb_RELAT:
    4124        9537 :         need = 1; /* not enough relations */
    4125        9537 :         continue;
    4126          69 :       case fupb_PRECI: /* prec problem unless we cheat on Bach constant */
    4127          69 :         if ((precdouble&7) == 7 && LIMC <= LIMCMAX/2) goto START;
    4128          69 :         precpb = "compute_R"; PREC = myprecdbl(PREC, flag? C: NULL);
    4129          69 :         continue;
    4130             :     }
    4131             :     /* DONE */
    4132             : 
    4133       63677 :     if (F.KCZ2 > F.KCZ)
    4134             :     {
    4135           7 :       if (F.sfb_chg && !subFB_change(&F)) goto START;
    4136           7 :       if (!be_honest(&F, nf, auts, fact)) goto START;
    4137           7 :       if (DEBUGLEVEL) timer_printf(&T, "to be honest");
    4138             :     }
    4139       63677 :     F.KCZ2 = 0; /* be honest only once */
    4140             : 
    4141             :     /* fundamental units */
    4142             :     {
    4143       63677 :       GEN AU, CU, U, v = extract_full_lattice(L); /* L may be large */
    4144       63678 :       CU = NULL;
    4145       63678 :       if (v) { A = vecpermute(A, v); L = vecpermute(L, v); }
    4146             :       /* arch. components of fund. units */
    4147       63678 :       U = ZM_lll(L, 0.99, LLL_IM);
    4148       63679 :       U = ZM_mul(U, lll(RgM_ZM_mul(real_i(A), U)));
    4149       63678 :       if (DEBUGLEVEL) timer_printf(&T, "units LLL");
    4150       63678 :       AU = RgM_ZM_mul(A, U);
    4151       63679 :       A = cleanarchunit(AU, N, NULL, PREC);
    4152       63679 :       if (!A || lg(A) < RU || expo(gsub(get_regulator(A), R)) > -1)
    4153           0 :       {
    4154           0 :         long add = nbits2extraprec( gexpo(AU) + 64 ) - gprecision(AU);
    4155           0 :         long t = maxss(PREC * 0.15, add);
    4156           0 :         if (!A && DEBUGLEVEL) err_printf("### Incorrect units lognorm");
    4157           0 :         precpb = "cleanarch"; PREC += maxss(t, EXTRAPREC64); continue;
    4158             :       }
    4159       63679 :       if (flag)
    4160             :       {
    4161       62083 :         long l = lgcols(C) - RU;
    4162             :         REL_t *rel;
    4163       62083 :         SUnits = cgetg(l, t_COL);
    4164      991086 :         for (rel = cache.base+1, i = 1; i < l; i++,rel++)
    4165      929004 :           set_rel_alpha(rel, auts, SUnits, i);
    4166       62082 :         if (RU > 1)
    4167             :         {
    4168       47410 :           GEN c = v? vecpermute(C,v): vecslice(C,1,zc);
    4169       47410 :           CU = ZM_mul(rowslice(c, RU+1, nbrows(c)), U);
    4170             :         }
    4171             :       }
    4172       63679 :       if (DEBUGLEVEL) err_printf("\n#### Computing fundamental units\n");
    4173       63679 :       fu = getfu(nf, &A, CU? &U: NULL, PREC);
    4174       63679 :       CU = CU? ZM_mul(CU, U): cgetg(1, t_MAT);
    4175       63679 :       if (DEBUGLEVEL) timer_printf(&T, "getfu");
    4176       63679 :       Ce = vecslice(C, zc+1, lg(C)-1);
    4177       63679 :       if (flag) SUnits = mkvec4(SUnits, CU, rowslice(Ce, RU+1, nbrows(Ce)),
    4178             :                                 utoipos(LIMC));
    4179             :     }
    4180             :     /* class group generators */
    4181       63679 :     if (flag) Ce = rowslice(Ce, 1, RU);
    4182       63678 :     C0 = Ce; Ce = cleanarch(Ce, N, NULL, PREC);
    4183       63679 :     if (!Ce) {
    4184           0 :       long add = nbits2extraprec( gexpo(C0) + 64 ) - gprecision(C0);
    4185           0 :       precpb = "cleanarch"; PREC += maxss(add, 1);
    4186             :     }
    4187       63679 :     if (DEBUGLEVEL) timer_printf(&T, "cleanarch");
    4188       94673 :   } while (need || precpb);
    4189             : 
    4190       63679 :   Vbase = vecpermute(F.LP, F.perm);
    4191       63679 :   if (!fu) fu = cgetg(1, t_MAT);
    4192       63679 :   if (!SUnits) SUnits = gen_1;
    4193       63679 :   clg1 = class_group_gen(nf,W,Ce,Vbase,PREC, &clg2);
    4194       63679 :   res = mkvec5(clg1, R, SUnits, zu, fu);
    4195       63679 :   res = buchall_end(nf,res,clg2,W,B,A,Ce,Vbase);
    4196       63679 :   delete_FB(&F);
    4197       63679 :   res = gerepilecopy(av0, res);
    4198       63679 :   if (flag) obj_insert_shallow(res, MATAL, cgetg(1,t_VEC));
    4199       63679 :   if (nfisclone) gunclone(nf);
    4200       63679 :   delete_cache(&cache);
    4201       63679 :   free_GRHcheck(&GRHcheck);
    4202       63679 :   return res;
    4203             : }

Generated by: LCOV version 1.16