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 29115-f22e516b23) Lines: 2208 2407 91.7 %
Date: 2024-04-20 08:07:50 Functions: 154 165 93.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation; either version 2 of the License, or (at your option) any later
       8             : version. It is distributed in the hope that it will be useful, but WITHOUT
       9             : ANY WARRANTY WHATSOEVER.
      10             : 
      11             : Check the License for details. You should have received a copy of it, along
      12             : with the package; see the file 'COPYING'. If not, write to the Free Software
      13             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : #define DEBUGLEVEL DEBUGLEVEL_bnf
      18             : 
      19             : /*******************************************************************/
      20             : /*                                                                 */
      21             : /*         CLASS GROUP AND REGULATOR (McCURLEY, BUCHMANN)          */
      22             : /*                    GENERAL NUMBER FIELDS                        */
      23             : /*                                                                 */
      24             : /*******************************************************************/
      25             : /* get_random_ideal */
      26             : static const long RANDOM_BITS = 4;
      27             : /* Buchall */
      28             : static const long RELSUP = 5;
      29             : static const long FAIL_DIVISOR = 32;
      30             : static const long MINFAIL = 10;
      31             : /* small_norm */
      32             : static const long BNF_RELPID = 4;
      33             : static const long maxtry_FACT = 500;
      34             : /* rnd_rel */
      35             : static const long RND_REL_RELPID = 1;
      36             : /* random relations */
      37             : static const long MINSFB = 3;
      38             : static const long SFB_MAX = 3;
      39             : static const long DEPSIZESFBMULT = 16;
      40             : static const long DEPSFBDIV = 10;
      41             : /* add_rel_i */
      42             : static const ulong mod_p = 27449UL;
      43             : /* be_honest */
      44             : static const long maxtry_HONEST = 50;
      45             : 
      46             : typedef struct FACT {
      47             :     long pr, ex;
      48             : } FACT;
      49             : 
      50             : typedef struct subFB_t {
      51             :   GEN subFB;
      52             :   struct subFB_t *old;
      53             : } subFB_t;
      54             : 
      55             : /* a factor base contains only noninert primes
      56             :  * KC = # of P in factor base (p <= n, NP <= n2)
      57             :  * KC2= # of P assumed to generate class group (NP <= n2)
      58             :  *
      59             :  * KCZ = # of rational primes under ideals counted by KC
      60             :  * KCZ2= same for KC2 */
      61             : 
      62             : typedef struct FB_t {
      63             :   GEN FB; /* FB[i] = i-th rational prime used in factor base */
      64             :   GEN LP; /* vector of all prime ideals in FB, by increasing norm */
      65             :   GEN LV; /* LV[p] = vector of P|p, NP <= n2
      66             :             * isclone() is set for LV[p] iff all P|p are in FB
      67             :             * LV[i], i not prime or i > n2, is undefined! */
      68             :   GEN iLP; /* iLP[p] = i such that LV[p] = [LP[i],...] */
      69             :   GEN L_jid; /* indexes of "useful" prime ideals for rnd_rel */
      70             :   long KC, KCZ, KCZ2;
      71             :   GEN prodZ; /* product of the primes in KCZ*/
      72             :   GEN subFB; /* LP o subFB =  part of FB used to build random relations */
      73             :   int sfb_chg; /* need to change subFB ? */
      74             :   GEN perm; /* permutation of LP used to represent relations [updated by
      75             :                hnfspec/hnfadd: dense rows come first] */
      76             :   GEN idealperm; /* permutation of ideals under field automorphisms */
      77             :   GEN minidx; /* minidx[i] min ideal in orbit of LP[i] under field autom */
      78             :   subFB_t *allsubFB; /* all subFB's used */
      79             :   GEN embperm; /* permutations of the complex embeddings */
      80             :   long MAXDEPSIZESFB; /* # trials before increasing subFB */
      81             :   long MAXDEPSFB; /* MAXDEPSIZESFB / DEPSFBDIV, # trials befor rotating subFB */
      82             :   double ballvol;
      83             : } FB_t;
      84             : 
      85             : enum { sfb_CHANGE = 1, sfb_INCREASE = 2 };
      86             : 
      87             : typedef struct REL_t {
      88             :   GEN R; /* relation vector as t_VECSMALL; clone */
      89             :   long nz; /* index of first nonzero elt in R (hash) */
      90             :   GEN m; /* pseudo-minimum yielding the relation; clone */
      91             :   long relorig; /* relation this one is an image of */
      92             :   long relaut; /* automorphim used to compute this relation from the original */
      93             :   GEN emb; /* archimedean embeddings */
      94             :   GEN junk[2]; /*make sure sizeof(struct) is a power of two.*/
      95             : } REL_t;
      96             : 
      97             : typedef struct RELCACHE_t {
      98             :   REL_t *chk; /* last checkpoint */
      99             :   REL_t *base; /* first rel found */
     100             :   REL_t *last; /* last rel found so far */
     101             :   REL_t *end; /* target for last relation. base <= last <= end */
     102             :   size_t len; /* number of rels pre-allocated in base */
     103             :   long relsup; /* how many linearly dependent relations we allow */
     104             :   GEN basis; /* mod p basis (generating family actually) */
     105             :   ulong missing; /* missing vectors in generating family above */
     106             : } RELCACHE_t;
     107             : 
     108             : typedef struct FP_t {
     109             :   double **q;
     110             :   GEN x;
     111             :   double *y;
     112             :   double *z;
     113             :   double *v;
     114             : } FP_t;
     115             : 
     116             : typedef struct RNDREL_t {
     117             :   long jid;
     118             :   GEN ex;
     119             : } RNDREL_t;
     120             : 
     121             : static void
     122           0 : wr_rel(GEN e)
     123             : {
     124           0 :   long i, l = lg(e);
     125           0 :   for (i = 1; i < l; i++)
     126           0 :     if (e[i]) err_printf("%ld^%ld ",i,e[i]);
     127           0 : }
     128             : static void
     129           0 : dbg_newrel(RELCACHE_t *cache)
     130             : {
     131           0 :   if (DEBUGLEVEL > 1)
     132             :   {
     133           0 :     err_printf("\n++++ cglob = %ld\nrel = ", cache->last - cache->base);
     134           0 :     wr_rel(cache->last->R);
     135           0 :     err_printf("\n");
     136             :   }
     137             :   else
     138           0 :     err_printf("%ld ", cache->last - cache->base);
     139           0 : }
     140             : 
     141             : static void
     142       63663 : delete_cache(RELCACHE_t *M)
     143             : {
     144             :   REL_t *rel;
     145     1064011 :   for (rel = M->base+1; rel <= M->last; rel++)
     146             :   {
     147     1000348 :     gunclone(rel->R);
     148     1000350 :     if (rel->m) gunclone(rel->m);
     149             :   }
     150       63663 :   pari_free((void*)M->base); M->base = NULL;
     151       63663 : }
     152             : 
     153             : static void
     154       65840 : delete_FB(FB_t *F)
     155             : {
     156             :   subFB_t *s, *sold;
     157      132818 :   for (s = F->allsubFB; s; s = sold) { sold = s->old; pari_free(s); }
     158       65840 :   gunclone(F->minidx);
     159       65840 :   gunclone(F->idealperm);
     160       65840 : }
     161             : 
     162             : static void
     163       63756 : reallocate(RELCACHE_t *M, long len)
     164             : {
     165       63756 :   M->len = len;
     166       63756 :   if (!M->base)
     167       63663 :     M->base = (REL_t*)pari_malloc((len+1) * sizeof(REL_t));
     168             :   else
     169             :   {
     170          93 :     size_t l = M->last - M->base, c = M->chk - M->base, e = M->end - M->base;
     171          93 :     pari_realloc_ip((void**)&M->base, (len+1) * sizeof(REL_t));
     172          93 :     M->last = M->base + l;
     173          93 :     M->chk  = M->base + c;
     174          93 :     M->end  = M->base + e;
     175             :   }
     176       63756 : }
     177             : 
     178             : #define pr_get_smallp(pr) gel(pr,1)[2]
     179             : 
     180             : /* don't take P|p all other Q|p are already there */
     181             : static int
     182      271595 : bad_subFB(FB_t *F, long t)
     183             : {
     184      271595 :   GEN LP, P = gel(F->LP,t);
     185      271595 :   long p = pr_get_smallp(P);
     186      271595 :   LP = gel(F->LV,p);
     187      271595 :   return (isclone(LP) && t == F->iLP[p] + lg(LP)-1);
     188             : }
     189             : 
     190             : static void
     191       66978 : assign_subFB(FB_t *F, GEN yes, long iyes)
     192             : {
     193       66978 :   long i, lv = sizeof(subFB_t) + iyes*sizeof(long); /* for struct + GEN */
     194       66978 :   subFB_t *s = (subFB_t *)pari_malloc(lv);
     195       66978 :   s->subFB = (GEN)&s[1];
     196       66978 :   s->old = F->allsubFB; F->allsubFB = s;
     197      288000 :   for (i = 0; i < iyes; i++) s->subFB[i] = yes[i];
     198       66978 :   F->subFB = s->subFB;
     199       66978 :   F->MAXDEPSIZESFB = (iyes-1) * DEPSIZESFBMULT;
     200       66978 :   F->MAXDEPSFB = F->MAXDEPSIZESFB / DEPSFBDIV;
     201       66978 : }
     202             : 
     203             : /* Determine the permutation of the ideals made by each field automorphism */
     204             : static GEN
     205       65840 : FB_aut_perm(FB_t *F, GEN auts, GEN cyclic)
     206             : {
     207       65840 :   long i, j, m, KC = F->KC, nauts = lg(auts)-1;
     208       65840 :   GEN minidx, perm = zero_Flm_copy(KC, nauts);
     209             : 
     210       65840 :   if (!nauts) { F->minidx = gclone(identity_zv(KC)); return cgetg(1,t_MAT); }
     211       41571 :   minidx = zero_Flv(KC);
     212       90470 :   for (m = 1; m < lg(cyclic); m++)
     213             :   {
     214       48900 :     GEN thiscyc = gel(cyclic, m);
     215       48900 :     long k0 = thiscyc[1];
     216       48900 :     GEN aut = gel(auts, k0), permk0 = gel(perm, k0), ppermk;
     217       48900 :     i = 1;
     218      209916 :     while (i <= KC)
     219             :     {
     220      161017 :       pari_sp av2 = avma;
     221      161017 :       GEN seen = zero_Flv(KC), P = gel(F->LP, i);
     222      161017 :       long imin = i, p, f, l;
     223      161017 :       p = pr_get_smallp(P);
     224      161017 :       f = pr_get_f(P);
     225             :       do
     226             :       {
     227      474595 :         if (++i > KC) break;
     228      425696 :         P = gel(F->LP, i);
     229             :       }
     230      425696 :       while (p == pr_get_smallp(P) && f == pr_get_f(P));
     231      635594 :       for (j = imin; j < i; j++)
     232             :       {
     233      474587 :         GEN img = ZM_ZC_mul(aut, pr_get_gen(gel(F->LP, j)));
     234     1656356 :         for (l = imin; l < i; l++)
     235     1656355 :           if (!seen[l] && ZC_prdvd(img, gel(F->LP, l)))
     236             :           {
     237      474576 :             seen[l] = 1; permk0[j] = l; break;
     238             :           }
     239             :       }
     240      161007 :       set_avma(av2);
     241             :     }
     242       67886 :     for (ppermk = permk0, i = 2; i < lg(thiscyc); i++)
     243             :     {
     244       18987 :       GEN permk = gel(perm, thiscyc[i]);
     245      382526 :       for (j = 1; j <= KC; j++) permk[j] = permk0[ppermk[j]];
     246       18987 :       ppermk = permk;
     247             :     }
     248             :   }
     249      306506 :   for (j = 1; j <= KC; j++)
     250             :   {
     251      264936 :     if (minidx[j]) continue;
     252      127416 :     minidx[j] = j;
     253      355745 :     for (i = 1; i <= nauts; i++) minidx[coeff(perm, j, i)] = j;
     254             :   }
     255       41570 :   F->minidx = gclone(minidx); return perm;
     256             : }
     257             : 
     258             : /* set subFB.
     259             :  * Fill F->perm (if != NULL): primes ideals sorted by increasing norm (except
     260             :  * the ones in subFB come first [dense rows for hnfspec]) */
     261             : static void
     262       65839 : subFBgen(FB_t *F, GEN auts, GEN cyclic, double PROD, long minsFB)
     263             : {
     264             :   GEN y, perm, yes, no;
     265       65839 :   long i, j, k, iyes, ino, lv = F->KC + 1;
     266             :   double prod;
     267             :   pari_sp av;
     268             : 
     269       65839 :   F->LP   = cgetg(lv, t_VEC);
     270       65839 :   F->L_jid = F->perm = cgetg(lv, t_VECSMALL);
     271       65839 :   av = avma;
     272       65839 :   y = cgetg(lv,t_COL); /* Norm P */
     273      310013 :   for (k=0, i=1; i <= F->KCZ; i++)
     274             :   {
     275      244174 :     GEN LP = gel(F->LV,F->FB[i]);
     276      244174 :     long l = lg(LP);
     277      705983 :     for (j = 1; j < l; j++)
     278             :     {
     279      461808 :       GEN P = gel(LP,j);
     280      461808 :       k++;
     281      461808 :       gel(y,k) = pr_norm(P);
     282      461809 :       gel(F->LP,k) = P;
     283             :     }
     284             :   }
     285             :   /* perm sorts LP by increasing norm */
     286       65839 :   perm = indexsort(y);
     287       65840 :   no  = cgetg(lv, t_VECSMALL); ino  = 1;
     288       65840 :   yes = cgetg(lv, t_VECSMALL); iyes = 1;
     289       65840 :   prod = 1.0;
     290      301506 :   for (i = 1; i < lv; i++)
     291             :   {
     292      271595 :     long t = perm[i];
     293      271595 :     if (bad_subFB(F, t)) { no[ino++] = t; continue; }
     294             : 
     295      151894 :     yes[iyes++] = t;
     296      151894 :     prod *= (double)itos(gel(y,t));
     297      151894 :     if (iyes > minsFB && prod > PROD) break;
     298             :   }
     299       65840 :   setlg(yes, iyes);
     300      217734 :   for (j=1; j<iyes; j++)     F->perm[j] = yes[j];
     301      185541 :   for (i=1; i<ino; i++, j++) F->perm[j] =  no[i];
     302      256067 :   for (   ; j<lv; j++)       F->perm[j] =  perm[j];
     303       65840 :   F->allsubFB = NULL;
     304       65840 :   F->idealperm = gclone(FB_aut_perm(F, auts, cyclic));
     305       65840 :   if (iyes) assign_subFB(F, yes, iyes);
     306       65839 :   set_avma(av);
     307       65839 : }
     308             : static int
     309        5324 : subFB_change(FB_t *F)
     310             : {
     311        5324 :   long i, iyes, minsFB, lv = F->KC + 1, l = lg(F->subFB)-1;
     312        5324 :   pari_sp av = avma;
     313        5324 :   GEN yes, L_jid = F->L_jid, present = zero_zv(lv-1);
     314             : 
     315        5324 :   switch (F->sfb_chg)
     316             :   {
     317         205 :     case sfb_INCREASE: minsFB = l + 1; break;
     318        5119 :     default: minsFB = l; break;
     319             :   }
     320             : 
     321        5324 :   yes = cgetg(minsFB+1, t_VECSMALL); iyes = 1;
     322        5324 :   if (L_jid)
     323             :   {
     324       10508 :     for (i = 1; i < lg(L_jid); i++)
     325             :     {
     326       10007 :       long l = L_jid[i];
     327       10007 :       yes[iyes++] = l;
     328       10007 :       present[l] = 1;
     329       10007 :       if (iyes > minsFB) break;
     330             :     }
     331             :   }
     332           0 :   else i = 1;
     333        5324 :   if (iyes <= minsFB)
     334             :   {
     335         590 :     for ( ; i < lv; i++)
     336             :     {
     337         585 :       long l = F->perm[i];
     338         585 :       if (present[l]) continue;
     339         580 :       yes[iyes++] = l;
     340         580 :       if (iyes > minsFB) break;
     341             :     }
     342         501 :     if (i == lv) return 0;
     343             :   }
     344        5319 :   if (zv_equal(F->subFB, yes))
     345             :   {
     346        4181 :     if (DEBUGLEVEL) err_printf("\n*** NOT Changing sub factor base\n");
     347             :   }
     348             :   else
     349             :   {
     350        1138 :     if (DEBUGLEVEL) err_printf("\n*** Changing sub factor base\n");
     351        1138 :     assign_subFB(F, yes, iyes);
     352             :   }
     353        5319 :   F->sfb_chg = 0; return gc_bool(av, 1);
     354             : }
     355             : 
     356             : /* make sure enough room to store n more relations */
     357             : static void
     358      106786 : pre_allocate(RELCACHE_t *cache, size_t n)
     359             : {
     360      106786 :   size_t len = (cache->last - cache->base) + n;
     361      106786 :   if (len >= cache->len) reallocate(cache, len << 1);
     362      106786 : }
     363             : 
     364             : void
     365      133881 : init_GRHcheck(GRHcheck_t *S, long N, long R1, double LOGD)
     366             : {
     367      133881 :   const double c1 = M_PI*M_PI/2;
     368      133881 :   const double c2 = 3.663862376709;
     369      133881 :   const double c3 = 3.801387092431; /* Euler + log(8*Pi)*/
     370      133881 :   S->clone = 0;
     371      133881 :   S->cN = R1*c2 + N*c1;
     372      133881 :   S->cD = LOGD - N*c3 - R1*M_PI/2;
     373      133881 :   S->maxprimes = 16000; /* sufficient for LIMC=176081*/
     374      133881 :   S->primes = (GRHprime_t*)pari_malloc(S->maxprimes*sizeof(*S->primes));
     375      133880 :   S->nprimes = 0;
     376      133880 :   S->limp = 0;
     377      133880 :   u_forprime_init(&S->P, 2, ULONG_MAX);
     378      133879 : }
     379             : 
     380             : void
     381      133881 : free_GRHcheck(GRHcheck_t *S)
     382             : {
     383      133881 :   if (S->clone)
     384             :   {
     385       63623 :     long i = S->nprimes;
     386             :     GRHprime_t *pr;
     387     7526362 :     for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--) gunclone(pr->dec);
     388             :   }
     389      133879 :   pari_free(S->primes);
     390      133881 : }
     391             : 
     392             : int
     393     1526118 : GRHok(GRHcheck_t *S, double L, double SA, double SB)
     394             : {
     395     1526118 :   return (S->cD + (S->cN + 2*SB) / L - 2*SA < -1e-8);
     396             : }
     397             : 
     398             : /* Return factorization pattern of p: [f,n], where n[i] primes of
     399             :  * residue degree f[i] */
     400             : static GEN
     401     7460109 : get_fs(GEN nf, GEN P, GEN index, ulong p)
     402             : {
     403             :   long j, k, f, n, l;
     404             :   GEN fs, ns;
     405             : 
     406     7460109 :   if (umodiu(index, p))
     407             :   { /* easy case: p does not divide index */
     408     7422049 :     GEN F = Flx_degfact(ZX_to_Flx(P,p), p);
     409     7422463 :     fs = gel(F,1); l = lg(fs);
     410             :   }
     411             :   else
     412             :   {
     413       37925 :     GEN F = idealprimedec(nf, utoipos(p));
     414       37961 :     l = lg(F);
     415       37961 :     fs = cgetg(l, t_VECSMALL);
     416      118944 :     for (j = 1; j < l; j++) fs[j] = pr_get_f(gel(F,j));
     417             :   }
     418     7460424 :   ns = cgetg(l, t_VECSMALL);
     419     7458420 :   f = fs[1]; n = 1;
     420    13799390 :   for (j = 2, k = 1; j < l; j++)
     421     6340970 :     if (fs[j] == f)
     422     4619269 :       n++;
     423             :     else
     424             :     {
     425     1721701 :       ns[k] = n; fs[k] = f; k++;
     426     1721701 :       f = fs[j]; n = 1;
     427             :     }
     428     7458420 :   ns[k] = n; fs[k] = f; k++;
     429     7458420 :   setlg(fs, k);
     430     7457828 :   setlg(ns, k); return mkvec2(fs,ns);
     431             : }
     432             : 
     433             : /* cache data for all rational primes up to the LIM */
     434             : static void
     435      915386 : cache_prime_dec(GRHcheck_t *S, ulong LIM, GEN nf)
     436             : {
     437      915386 :   pari_sp av = avma;
     438             :   GRHprime_t *pr;
     439             :   GEN index, P;
     440             :   double nb;
     441             : 
     442      915386 :   if (S->limp >= LIM) return;
     443      327628 :   S->clone = 1;
     444      327628 :   nb = primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
     445      327639 :   GRH_ensure(S, nb+1); /* room for one extra prime */
     446      327638 :   P = nf_get_pol(nf);
     447      327636 :   index = nf_get_index(nf);
     448      327634 :   for (pr = S->primes + S->nprimes;;)
     449     7132670 :   {
     450     7460304 :     ulong p = u_forprime_next(&(S->P));
     451     7460086 :     pr->p = p;
     452     7460086 :     pr->logp = log((double)p);
     453     7460086 :     pr->dec = gclone(get_fs(nf, P, index, p));
     454     7460278 :     S->nprimes++;
     455     7460278 :     pr++;
     456     7460278 :     set_avma(av);
     457             :     /* store up to nextprime(LIM) included */
     458     7460305 :     if (p >= LIM) { S->limp = p; break; }
     459             :   }
     460             : }
     461             : 
     462             : static double
     463     2245362 : tailresback(long R1, long R2, double rK, long C, double C2, double C3, double r1K, double r2K, double logC, double logC2, double logC3)
     464             : {
     465     2245362 :   const double  rQ = 1.83787706641;
     466     2245362 :   const double r1Q = 1.98505372441;
     467     2245362 :   const double r2Q = 1.07991541347;
     468     4490724 :   return fabs((R1+R2-1)*(12*logC3+4*logC2-9*logC-6)/(2*C*logC3)
     469     2245362 :          + (rK-rQ)*(6*logC2 + 5*logC + 2)/(C*logC3)
     470     2245362 :          - R2*(6*logC2+11*logC+6)/(C2*logC2)
     471     2245362 :          - 2*(r1K-r1Q)*(3*logC2 + 4*logC + 2)/(C2*logC3)
     472     2245362 :          + (R1+R2-1)*(12*logC3+40*logC2+45*logC+18)/(6*C3*logC3)
     473     2245362 :          + (r2K-r2Q)*(2*logC2 + 3*logC + 2)/(C3*logC3));
     474             : }
     475             : 
     476             : static double
     477     1122682 : tailres(long R1, long R2, double al2K, double rKm, double rKM, double r1Km,
     478             :         double r1KM, double r2Km, double r2KM, double C, long i)
     479             : {
     480             :   /* C >= 3*2^i, lower bound for eint1(log(C)/2) */
     481             :   /* for(i=0,30,print(eint1(log(3*2^i)/2))) */
     482             :   static double tab[] = {
     483             :     0.50409264803,
     484             :     0.26205336997,
     485             :     0.14815491171,
     486             :     0.08770540561,
     487             :     0.05347651832,
     488             :     0.03328934284,
     489             :     0.02104510690,
     490             :     0.01346475900,
     491             :     0.00869778586,
     492             :     0.00566279855,
     493             :     0.00371111950,
     494             :     0.00244567837,
     495             :     0.00161948049,
     496             :     0.00107686891,
     497             :     0.00071868750,
     498             :     0.00048119961,
     499             :     0.00032312188,
     500             :     0.00021753772,
     501             :     0.00014679818,
     502             :     9.9272855581E-5,
     503             :     6.7263969995E-5,
     504             :     4.5656812967E-5,
     505             :     3.1041124593E-5,
     506             :     2.1136011590E-5,
     507             :     1.4411645381E-5,
     508             :     9.8393304088E-6,
     509             :     6.7257395409E-6,
     510             :     4.6025878272E-6,
     511             :     3.1529719271E-6,
     512             :     2.1620490021E-6,
     513             :     1.4839266071E-6
     514             :   };
     515     1122682 :   const double logC = log(C), logC2 = logC*logC, logC3 = logC*logC2;
     516     1122682 :   const double C2 = C*C, C3 = C*C2;
     517     1122682 :   double E1 = i >30? 0: tab[i];
     518     1122682 :   return al2K*((33*logC2+22*logC+8)/(8*logC3*sqrt(C))+15*E1/16)
     519     1122682 :     + maxdd(tailresback(rKm,r1KM,r2Km, C,C2,C3,R1,R2,logC,logC2,logC3),
     520     1122683 :             tailresback(rKM,r1Km,r2KM, C,C2,C3,R1,R2,logC,logC2,logC3))/2
     521     1122683 :     + ((R1+R2-1)*4*C+R2)*(C2+6*logC)/(4*C2*C2*logC2);
     522             : }
     523             : 
     524             : static long
     525       63622 : primeneeded(long N, long R1, long R2, double LOGD)
     526             : {
     527       63622 :   const double lim = 0.25; /* should be log(2)/2 == 0.34657... */
     528       63622 :   const double al2K =  0.3526*LOGD - 0.8212*N + 4.5007;
     529       63622 :   const double  rKm = -1.0155*LOGD + 2.1042*N - 8.3419;
     530       63622 :   const double  rKM = -0.5   *LOGD + 1.2076*N + 1;
     531       63622 :   const double r1Km = -       LOGD + 1.4150*N;
     532       63622 :   const double r1KM = -       LOGD + 1.9851*N;
     533       63622 :   const double r2Km = -       LOGD + 0.9151*N;
     534       63622 :   const double r2KM = -       LOGD + 1.0800*N;
     535       63622 :   long Cmin = 3, Cmax = 3, i = 0;
     536      570774 :   while (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, Cmax, i) > lim)
     537             :   {
     538      507152 :     Cmin = Cmax;
     539      507152 :     Cmax *= 2;
     540      507152 :     i++;
     541             :   }
     542       63622 :   i--;
     543      615538 :   while (Cmax - Cmin > 1)
     544             :   {
     545      551916 :     long t = (Cmin + Cmax)/2;
     546      551916 :     if (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, t, i) > lim)
     547      341899 :       Cmin = t;
     548             :     else
     549      210017 :       Cmax = t;
     550             :   }
     551       63622 :   return Cmax;
     552             : }
     553             : 
     554             : /* ~ 1 / Res(s = 1, zeta_K) */
     555             : static GEN
     556       63623 : compute_invres(GRHcheck_t *S, long LIMC)
     557             : {
     558       63623 :   pari_sp av = avma;
     559       63623 :   double loginvres = 0.;
     560             :   GRHprime_t *pr;
     561             :   long i;
     562       63623 :   double logLIMC = log((double)LIMC);
     563       63623 :   double logLIMC2 = logLIMC*logLIMC, denc;
     564             :   double c0, c1, c2;
     565       63623 :   denc = 1/(pow((double)LIMC, 3.) * logLIMC * logLIMC2);
     566       63623 :   c2 = (    logLIMC2 + 3 * logLIMC / 2 + 1) * denc;
     567       63623 :   denc *= LIMC;
     568       63623 :   c1 = (3 * logLIMC2 + 4 * logLIMC     + 2) * denc;
     569       63623 :   denc *= LIMC;
     570       63623 :   c0 = (3 * logLIMC2 + 5 * logLIMC / 2 + 1) * denc;
     571     7470409 :   for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--)
     572             :   {
     573             :     GEN dec, fs, ns;
     574             :     long addpsi;
     575             :     double addpsi1, addpsi2;
     576     7462513 :     double logp = pr->logp, NPk;
     577     7462513 :     long j, k, limp = logLIMC/logp;
     578     7462513 :     ulong p = pr->p, p2 = p*p;
     579     7462513 :     if (limp < 1) break;
     580     7406786 :     dec = pr->dec;
     581     7406786 :     fs = gel(dec, 1); ns = gel(dec, 2);
     582     7406786 :     loginvres += 1./p;
     583             :     /* NB: limp = 1 nearly always and limp > 2 for very few primes */
     584     8764736 :     for (k=2, NPk = p; k <= limp; k++) { NPk *= p; loginvres += 1/(k * NPk); }
     585     7406786 :     addpsi = limp;
     586     7406786 :     addpsi1 = p *(pow((double)p , (double)limp)-1)/(p -1);
     587     7406786 :     addpsi2 = p2*(pow((double)p2, (double)limp)-1)/(p2-1);
     588     7406786 :     j = lg(fs);
     589    16523940 :     while (--j > 0)
     590             :     {
     591             :       long f, nb, kmax;
     592             :       double NP, NP2, addinvres;
     593     9117154 :       f = fs[j]; if (f > limp) continue;
     594     3964080 :       nb = ns[j];
     595     3964080 :       NP = pow((double)p, (double)f);
     596     3964080 :       addinvres = 1/NP;
     597     3964080 :       kmax = limp / f;
     598     4837382 :       for (k=2, NPk = NP; k <= kmax; k++) { NPk *= NP; addinvres += 1/(k*NPk); }
     599     3964080 :       NP2 = NP*NP;
     600     3964080 :       loginvres -= nb * addinvres;
     601     3964080 :       addpsi -= nb * f * kmax;
     602     3964080 :       addpsi1 -= nb*(f*NP *(pow(NP ,(double)kmax)-1)/(NP -1));
     603     3964080 :       addpsi2 -= nb*(f*NP2*(pow(NP2,(double)kmax)-1)/(NP2-1));
     604             :     }
     605     7406786 :     loginvres -= (addpsi*c0 - addpsi1*c1 + addpsi2*c2)*logp;
     606             :   }
     607       63623 :   return gerepileuptoleaf(av, mpexp(dbltor(loginvres)));
     608             : }
     609             : 
     610             : static long
     611       63623 : nthideal(GRHcheck_t *S, GEN nf, long n)
     612             : {
     613       63623 :   pari_sp av = avma;
     614       63623 :   GEN P = nf_get_pol(nf);
     615       63623 :   ulong p = 0, *vecN = (ulong*)const_vecsmall(n, LONG_MAX);
     616       63622 :   long i, N = poldegree(P, -1);
     617       63622 :   for (i = 0; ; i++)
     618      229021 :   {
     619             :     GRHprime_t *pr;
     620             :     GEN fs;
     621      292643 :     cache_prime_dec(S, p+1, nf);
     622      292642 :     pr = S->primes + i;
     623      292642 :     fs = gel(pr->dec, 1);
     624      292642 :     p = pr->p;
     625      292642 :     if (fs[1] != N)
     626             :     {
     627      196459 :       GEN ns = gel(pr->dec, 2);
     628      196459 :       long k, l, j = lg(fs);
     629      440546 :       while (--j > 0)
     630             :       {
     631      244086 :         ulong NP = upowuu(p, fs[j]);
     632             :         long nf;
     633      244087 :         if (!NP) continue;
     634      749399 :         for (k = 1; k <= n; k++) if (vecN[k] > NP) break;
     635      243695 :         if (k > n) continue;
     636             :         /* vecN[k] <= NP */
     637      157804 :         nf = ns[j]; /*#{primes of norme NP} = nf, insert them here*/
     638      353130 :         for (l = k+nf; l <= n; l++) vecN[l] = vecN[l-nf];
     639      398558 :         for (l = 0; l < nf && k+l <= n; l++) vecN[k+l] = NP;
     640      362986 :         while (l <= k) vecN[l++] = NP;
     641             :       }
     642             :     }
     643      292643 :     if (p > vecN[n]) break;
     644             :   }
     645       63622 :   return gc_long(av, vecN[n]);
     646             : }
     647             : 
     648             : /* volume of unit ball in R^n: \pi^{n/2} / \Gamma(n/2 + 1) */
     649             : static double
     650       65838 : ballvol(long n)
     651             : {
     652       65838 :   double v = odd(n)? 2: 1;
     653      150343 :   for (; n > 1; n -= 2) v *= (2 * M_PI) / n;
     654       65838 :   return v;
     655             : }
     656             : 
     657             : /* Compute FB, LV, iLP + KC*. Reset perm
     658             :  * C2: bound for norm of tested prime ideals (includes be_honest())
     659             :  * C1: bound for p, such that P|p (NP <= C2) used to build relations */
     660             : static void
     661       65840 : FBgen(FB_t *F, GEN nf, long N, ulong C1, ulong C2, GRHcheck_t *S)
     662             : {
     663             :   GRHprime_t *pr;
     664             :   long i, ip;
     665             :   GEN prim;
     666       65840 :   const double L = log((double)C2 + 0.5);
     667             : 
     668       65840 :   cache_prime_dec(S, C2, nf);
     669       65840 :   pr = S->primes;
     670       65840 :   F->sfb_chg = 0;
     671       65840 :   F->FB  = cgetg(C2+1, t_VECSMALL);
     672       65840 :   F->iLP = cgetg(C2+1, t_VECSMALL);
     673       65839 :   F->LV = zerovec(C2);
     674             : 
     675       65839 :   prim = icopy(gen_1);
     676       65838 :   i = ip = 0;
     677       65838 :   F->KC = F->KCZ = 0;
     678      432781 :   for (;; pr++) /* p <= C2 */
     679      432781 :   {
     680      498619 :     ulong p = pr->p;
     681             :     long k, l, m;
     682             :     GEN LP, nb, f;
     683             : 
     684      498619 :     if (!F->KC && p > C1) { F->KCZ = i; F->KC = ip; }
     685      498619 :     if (p > C2) break;
     686             : 
     687      461465 :     if (DEBUGLEVEL>1) err_printf(" %ld",p);
     688             : 
     689      461465 :     f = gel(pr->dec, 1); nb = gel(pr->dec, 2);
     690      461465 :     if (f[1] == N)
     691             :     {
     692      145009 :       if (p == C2) break;
     693      136483 :       continue; /* p inert */
     694             :     }
     695      316456 :     l = (long)(L/pr->logp); /* p^f <= C2  <=> f <= l */
     696      577313 :     for (k=0, m=1; m < lg(f) && f[m]<=l; m++) k += nb[m];
     697      316456 :     if (!k)
     698             :     { /* too inert to appear in FB */
     699       72273 :       if (p == C2) break;
     700       71643 :       continue;
     701             :     }
     702      244183 :     prim[2] = p; LP = idealprimedec_limit_f(nf,prim, l);
     703             :     /* keep noninert ideals with Norm <= C2 */
     704      244185 :     if (m == lg(f)) setisclone(LP); /* flag it: all prime divisors in FB */
     705      244185 :     F->FB[++i]= p;
     706      244185 :     gel(F->LV,p) = LP;
     707      244185 :     F->iLP[p] = ip; ip += k;
     708      244185 :     if (p == C2) break;
     709             :   }
     710       65840 :   if (!F->KC) { F->KCZ = i; F->KC = ip; }
     711             :   /* Note F->KC > 0 otherwise GRHchk is false */
     712       65840 :   setlg(F->FB, F->KCZ+1); F->KCZ2 = i;
     713       65840 :   F->prodZ = zv_prod_Z(F->FB);
     714       65838 :   if (DEBUGLEVEL>1)
     715             :   {
     716           0 :     err_printf("\n");
     717           0 :     if (DEBUGLEVEL>6)
     718             :     {
     719           0 :       err_printf("########## FACTORBASE ##########\n\n");
     720           0 :       err_printf("KC2=%ld, KC=%ld, KCZ=%ld, KCZ2=%ld\n",
     721             :                   ip, F->KC, F->KCZ, F->KCZ2);
     722           0 :       for (i=1; i<=F->KCZ; i++) err_printf("++ LV[%ld] = %Ps",i,gel(F->LV,F->FB[i]));
     723             :     }
     724             :   }
     725       65838 :   F->perm = NULL; F->L_jid = NULL;
     726       65838 :   F->ballvol = ballvol(nf_get_degree(nf));
     727       65838 : }
     728             : 
     729             : static int
     730      493294 : GRHchk(GEN nf, GRHcheck_t *S, ulong LIMC)
     731             : {
     732      493294 :   double logC = log((double)LIMC), SA = 0, SB = 0;
     733      493294 :   GRHprime_t *pr = S->primes;
     734             : 
     735      493294 :   cache_prime_dec(S, LIMC, nf);
     736      493291 :   for (pr = S->primes;; pr++)
     737     3032296 :   {
     738     3525587 :     ulong p = pr->p;
     739             :     GEN dec, fs, ns;
     740             :     double logCslogp;
     741             :     long j;
     742             : 
     743     3525587 :     if (p > LIMC) break;
     744     3137946 :     dec = pr->dec; fs = gel(dec, 1); ns = gel(dec,2);
     745     3137946 :     logCslogp = logC/pr->logp;
     746     4939393 :     for (j = 1; j < lg(fs); j++)
     747             :     {
     748     3866041 :       long f = fs[j], M, nb;
     749             :       double logNP, q, A, B;
     750     3866041 :       if (f > logCslogp) break;
     751     1801450 :       logNP = f * pr->logp;
     752     1801450 :       q = 1/sqrt((double)upowuu(p, f));
     753     1801447 :       A = logNP * q; B = logNP * A; M = (long)(logCslogp/f);
     754     1801447 :       if (M > 1)
     755             :       {
     756      374275 :         double inv1_q = 1 / (1-q);
     757      374275 :         A *= (1 - pow(q, (double)M)) * inv1_q;
     758      374275 :         B *= (1 - pow(q, (double)M)*(M+1 - M*q)) * inv1_q * inv1_q;
     759             :       }
     760     1801447 :       nb = ns[j];
     761     1801447 :       SA += nb * A;
     762     1801447 :       SB += nb * B;
     763             :     }
     764     3137943 :     if (p == LIMC) break;
     765             :   }
     766      493288 :   return GRHok(S, logC, SA, SB);
     767             : }
     768             : 
     769             : /*  SMOOTH IDEALS */
     770             : static void
     771     9298266 : store(long i, long e, FACT *fact)
     772             : {
     773     9298266 :   ++fact[0].pr;
     774     9298266 :   fact[fact[0].pr].pr = i; /* index */
     775     9298266 :   fact[fact[0].pr].ex = e; /* exponent */
     776     9298266 : }
     777             : 
     778             : /* divide out x by all P|p, where x as in can_factor().  k = v_p(Nx) */
     779             : static int
     780     5816028 : divide_p_elt(GEN LP, long ip, long k, GEN m, FACT *fact)
     781             : {
     782     5816028 :   long j, l = lg(LP);
     783    18549614 :   for (j=1; j<l; j++)
     784             :   {
     785    18461329 :     GEN P = gel(LP,j);
     786    18461329 :     long v = ZC_nfval(m, P);
     787    18459515 :     if (!v) continue;
     788     8541754 :     store(ip + j, v, fact); /* v = v_P(m) > 0 */
     789     8543482 :     k -= v * pr_get_f(P);
     790     8543434 :     if (!k) return 1;
     791             :   }
     792       88285 :   return 0;
     793             : }
     794             : static int
     795      162967 : divide_p_id(GEN LP, long ip, long k, GEN nf, GEN I, FACT *fact)
     796             : {
     797      162967 :   long j, l = lg(LP);
     798      244427 :   for (j=1; j<l; j++)
     799             :   {
     800      236566 :     GEN P = gel(LP,j);
     801      236566 :     long v = idealval(nf,I, P);
     802      236565 :     if (!v) continue;
     803      158561 :     store(ip + j, v, fact); /* v = v_P(I) > 0 */
     804      158562 :     k -= v * pr_get_f(P);
     805      158563 :     if (!k) return 1;
     806             :   }
     807        7861 :   return 0;
     808             : }
     809             : static int
     810      547684 : divide_p_quo(GEN LP, long ip, long k, GEN nf, GEN I, GEN m, FACT *fact)
     811             : {
     812      547684 :   long j, l = lg(LP);
     813      799692 :   for (j=1; j<l; j++)
     814             :   {
     815      799417 :     GEN P = gel(LP,j);
     816      799417 :     long v = ZC_nfval(m, P);
     817      799417 :     if (!v) continue;
     818      577286 :     v -= idealval(nf,I, P);
     819      577286 :     if (!v) continue;
     820      570359 :     store(ip + j, v, fact); /* v = v_P(m / I) > 0 */
     821      570359 :     k -= v * pr_get_f(P);
     822      570359 :     if (!k) return 1;
     823             :   }
     824         275 :   return 0;
     825             : }
     826             : 
     827             : /* |*N| != 0 is the norm of a primitive ideal, in particular not divisible by
     828             :  * any inert prime. Is |*N| a smooth rational integer wrt F ?
     829             :  */
     830             : static int
     831    18935939 : Z_issmooth_prod(GEN N, GEN P)
     832             : {
     833    18935939 :   P = gcdii(P,N);
     834   105240642 :   while (!is_pm1(P))
     835             :   {
     836    86308136 :     N = diviiexact(N, P);
     837    86304041 :     P = gcdii(N, P);
     838             :   }
     839    18917547 :   return is_pm1(N);
     840             : }
     841             : 
     842             : static int
     843     6526505 : divide_p(FB_t *F, long p, long k, GEN nf, GEN I, GEN m, FACT *fact)
     844             : {
     845     6526505 :   GEN LP = gel(F->LV,p);
     846     6526505 :   long ip = F->iLP[p];
     847     6526505 :   if (!m) return divide_p_id (LP,ip,k,nf,I,fact);
     848     6363538 :   if (!I) return divide_p_elt(LP,ip,k,m,fact);
     849      547653 :   return divide_p_quo(LP,ip,k,nf,I,m,fact);
     850             : }
     851             : 
     852             : /* Let x = m if I == NULL,
     853             :  *         I if m == NULL,
     854             :  *         m/I otherwise.
     855             :  * Can we factor the integral primitive ideal x ? |N| = Norm x > 0 */
     856             : static long
     857    19710793 : can_factor(FB_t *F, GEN nf, GEN I, GEN m, GEN N, FACT *fact)
     858             : {
     859             :   GEN f, p, e;
     860             :   long i, l;
     861    19710793 :   fact[0].pr = 0;
     862    19710793 :   if (is_pm1(N)) return 1;
     863    18935952 :   if (!Z_issmooth_prod(N, F->prodZ)) return 0;
     864     2947688 :   f = absZ_factor(N); p = gel(f,1); e = gel(f,2); l = lg(p);
     865     9379296 :   for (i = 1; i < l; i++)
     866     6526336 :     if (!divide_p(F, itou(gel(p,i)), itou(gel(e,i)), nf, I, m, fact))
     867             :     {
     868       95668 :       if (DEBUGLEVEL > 1) err_printf(".");
     869       95668 :       return 0;
     870             :     }
     871     2852960 :   return 1;
     872             : }
     873             : 
     874             : /* can we factor m/I ? [m in I from idealpseudomin_nonscalar], NI = norm I */
     875             : static long
     876     1500912 : factorgen(FB_t *F, GEN nf, GEN I, GEN NI, GEN m, FACT *fact)
     877             : {
     878     1500912 :   long e, r1 = nf_get_r1(nf);
     879     1500912 :   GEN M = nf_get_M(nf);
     880     1500912 :   GEN N = divri(embed_norm(RgM_RgC_mul(M,m), r1), NI); /* ~ N(m/I) */
     881     1500915 :   N = grndtoi(N, &e);
     882     1500910 :   if (e > -32)
     883             :   {
     884           0 :     if (DEBUGLEVEL > 1) err_printf("+");
     885           0 :     return 0;
     886             :   }
     887     1500910 :   return can_factor(F, nf, I, m, N, fact);
     888             : }
     889             : 
     890             : /*  FUNDAMENTAL UNITS */
     891             : 
     892             : /* a, y real. Return  (Re(x) + a) + I * (Im(x) % y) */
     893             : static GEN
     894     6585063 : addRe_modIm(GEN x, GEN a, GEN y, GEN iy)
     895             : {
     896             :   GEN z;
     897     6585063 :   if (typ(x) == t_COMPLEX)
     898             :   {
     899     4667971 :     GEN re, im = modRr_i(gel(x,2), y, iy);
     900     4667950 :     if (!im) return NULL;
     901     4667950 :     re = gadd(gel(x,1), a);
     902     4667907 :     z = gequal0(im)? re: mkcomplex(re, im);
     903             :   }
     904             :   else
     905     1917092 :     z = gadd(x, a);
     906     6585001 :   return z;
     907             : }
     908             : static GEN
     909      201215 : modIm(GEN x, GEN y, GEN iy)
     910             : {
     911      201215 :   if (typ(x) == t_COMPLEX)
     912             :   {
     913      188599 :     GEN im = modRr_i(gel(x,2), y, iy);
     914      188595 :     if (!im) return NULL;
     915      188595 :     x = gequal0(im)? gel(x,1): mkcomplex(gel(x,1), im);
     916             :   }
     917      201208 :   return x;
     918             : }
     919             : 
     920             : /* clean archimedean components. ipi = 2^n / pi (n arbitrary); its
     921             :  * exponent may be modified */
     922             : static GEN
     923     2923639 : cleanarch(GEN x, long N, GEN ipi, long prec)
     924             : {
     925             :   long i, l, R1, RU;
     926     2923639 :   GEN s, y = cgetg_copy(x, &l);
     927             : 
     928     2923639 :   if (!ipi) ipi = invr(mppi(prec));
     929     2923639 :   if (typ(x) == t_MAT)
     930             :   {
     931      523437 :     for (i = 1; i < l; i++)
     932      459663 :       if (!(gel(y,i) = cleanarch(gel(x,i), N, ipi, prec))) return NULL;
     933       63774 :     return y;
     934             :   }
     935     2859863 :   RU = l-1; R1 = (RU<<1) - N;
     936     2859863 :   s = gdivgs(RgV_sum(real_i(x)), -N); /* -log |norm(x)| / N */
     937     2859851 :   i = 1;
     938     2859851 :   if (R1)
     939             :   {
     940     2382210 :     GEN pi2 = Pi2n(1, prec);
     941     2382213 :     setexpo(ipi, -3); /* 1/(2pi) */
     942     7334482 :     for (; i <= R1; i++)
     943     4952290 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s, pi2, ipi))) return NULL;
     944             :   }
     945     2859833 :   if (i <= RU)
     946             :   {
     947     1074699 :     GEN pi4 = Pi2n(2, prec), s2 = gmul2n(s, 1);
     948     1074709 :     setexpo(ipi, -4); /* 1/(4pi) */
     949     2707491 :     for (; i <= RU; i++)
     950     1632772 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s2, pi4, ipi))) return NULL;
     951             :   }
     952     2859853 :   return y;
     953             : }
     954             : GEN
     955      195050 : nf_cxlog_normalize(GEN nf, GEN x, long prec)
     956             : {
     957      195050 :   long N = nf_get_degree(nf);
     958      195050 :   return cleanarch(x, N, NULL, prec);
     959             : }
     960             : 
     961             : /* clean unit archimedean components. ipi = 2^n / pi (n arbitrary); its
     962             :  * exponent may be modified */
     963             : static GEN
     964      132579 : cleanarchunit(GEN x, long N, GEN ipi, long prec)
     965             : {
     966             :   long i, l, R1, RU;
     967      132579 :   GEN y = cgetg_copy(x, &l);
     968             : 
     969      132579 :   if (!ipi) ipi = invr(mppi(prec));
     970      132580 :   if (typ(x) == t_MAT)
     971             :   {
     972      132579 :     for (i = 1; i < l; i++)
     973       68957 :       if (!(gel(y,i) = cleanarchunit(gel(x,i), N, ipi, prec))) return NULL;
     974       63622 :     return y;
     975             :   }
     976       68957 :   if (gexpo(RgV_sum(real_i(x))) > -10) return NULL;
     977       68955 :   RU = l-1; R1 = (RU<<1) - N;
     978       68955 :   i = 1;
     979       68955 :   if (R1)
     980             :   {
     981       54556 :     GEN pi2 = Pi2n(1, prec);
     982       54557 :     setexpo(ipi, -3); /* 1/(2pi) */
     983      185388 :     for (; i <= R1; i++)
     984      130836 :       if (!(gel(y,i) = modIm(gel(x,i), pi2, ipi))) return NULL;
     985             :   }
     986       68951 :   if (i <= RU)
     987             :   {
     988       34355 :     GEN pi4 = Pi2n(2, prec);
     989       34356 :     setexpo(ipi, -4); /* 1/(4pi) */
     990      104738 :     for (; i <= RU; i++)
     991       70378 :       if (!(gel(y,i) = modIm(gel(x,i), pi4, ipi))) return NULL;
     992             :   }
     993       68956 :   return y;
     994             : }
     995             : 
     996             : static GEN
     997         375 : not_given(long reason)
     998             : {
     999         375 :   if (DEBUGLEVEL)
    1000           0 :     switch(reason)
    1001             :     {
    1002           0 :       case fupb_LARGE:
    1003           0 :         pari_warn(warner,"fundamental units too large, not given");
    1004           0 :         break;
    1005           0 :       case fupb_PRECI:
    1006           0 :         pari_warn(warner,"insufficient precision for fundamental units, not given");
    1007           0 :         break;
    1008             :     }
    1009         375 :   return NULL;
    1010             : }
    1011             : 
    1012             : /* check whether exp(x) will 1) get too big (real(x) large), 2) require
    1013             :  * large accuracy for argument reduction (imag(x) large) */
    1014             : static long
    1015     2683778 : expbitprec(GEN x, long *e)
    1016             : {
    1017             :   GEN re, im;
    1018     2683778 :   if (typ(x) != t_COMPLEX) re = x;
    1019             :   else
    1020             :   {
    1021     1670256 :     im = gel(x,2); *e = maxss(*e, expo(im) + 5 - bit_prec(im));
    1022     1670256 :     re = gel(x,1);
    1023             :   }
    1024     2683778 :   return (expo(re) <= 20);
    1025             : 
    1026             : }
    1027             : static long
    1028     1166075 : RgC_expbitprec(GEN x)
    1029             : {
    1030     1166075 :   long l = lg(x), i, e = - (long)HIGHEXPOBIT;
    1031     3648282 :   for (i = 1; i < l; i++)
    1032     2482661 :     if (!expbitprec(gel(x,i), &e)) return LONG_MAX;
    1033     1165621 :   return e;
    1034             : }
    1035             : static long
    1036       48433 : RgM_expbitprec(GEN x)
    1037             : {
    1038       48433 :   long i, j, I, J, e = - (long)HIGHEXPOBIT;
    1039       48433 :   RgM_dimensions(x, &I,&J);
    1040      117327 :   for (j = 1; j <= J; j++)
    1041      270011 :     for (i = 1; i <= I; i++)
    1042      201117 :       if (!expbitprec(gcoeff(x,i,j), &e)) return LONG_MAX;
    1043       48377 :   return e;
    1044             : }
    1045             : 
    1046             : static GEN
    1047        1379 : FlxqX_chinese_unit(GEN X, GEN U, GEN invzk, GEN D, GEN T, ulong p)
    1048             : {
    1049        1379 :   long i, lU = lg(U), lX = lg(X), d = lg(invzk)-1;
    1050        1379 :   GEN M = cgetg(lU, t_MAT);
    1051        1379 :   if (D)
    1052             :   {
    1053        1272 :     D = Flv_inv(D, p);
    1054       69717 :     for (i = 1; i < lX; i++)
    1055       68445 :       if (uel(D, i) != 1)
    1056       56578 :         gel(X,i) = Flx_Fl_mul(gel(X,i), uel(D,i), p);
    1057             :   }
    1058        3878 :   for (i = 1; i < lU; i++)
    1059             :   {
    1060        2499 :     GEN H = FlxqV_factorback(X, gel(U, i), T, p);
    1061        2499 :     gel(M, i) = Flm_Flc_mul(invzk, Flx_to_Flv(H, d), p);
    1062             :   }
    1063        1379 :   return M;
    1064             : }
    1065             : 
    1066             : static GEN
    1067         274 : chinese_unit_slice(GEN A, GEN U, GEN B, GEN D, GEN C, GEN P, GEN *mod)
    1068             : {
    1069         274 :   pari_sp av = avma;
    1070         274 :   long i, n = lg(P)-1, v = varn(C);
    1071             :   GEN H, T;
    1072         274 :   if (n == 1)
    1073             :   {
    1074           0 :     ulong p = uel(P,1);
    1075           0 :     GEN a = ZXV_to_FlxV(A, p), b = ZM_to_Flm(B, p), c = ZX_to_Flx(C, p);
    1076           0 :     GEN d = D ? ZV_to_Flv(D, p): NULL;
    1077           0 :     GEN Hp = FlxqX_chinese_unit(a, U, b, d, c, p);
    1078           0 :     H = gerepileupto(av, Flm_to_ZM(Hp));
    1079           0 :     *mod = utoi(p);
    1080           0 :     return H;
    1081             :   }
    1082         274 :   T = ZV_producttree(P);
    1083         274 :   A = ZXC_nv_mod_tree(A, P, T, v);
    1084         274 :   B = ZM_nv_mod_tree(B, P, T);
    1085         274 :   D = D ? ZV_nv_mod_tree(D, P, T): NULL;
    1086         274 :   C = ZX_nv_mod_tree(C, P, T);
    1087             : 
    1088         274 :   H = cgetg(n+1, t_VEC);
    1089        1653 :   for(i=1; i <= n; i++)
    1090             :   {
    1091        1379 :     ulong p = P[i];
    1092        1379 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C,i), d = D ? gel(D,i): NULL;
    1093        1379 :     gel(H,i) = FlxqX_chinese_unit(a, U, b, d, c, p);
    1094             :   }
    1095         274 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
    1096         274 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    1097             : }
    1098             : 
    1099             : GEN
    1100         274 : chinese_unit_worker(GEN P, GEN A, GEN U, GEN B, GEN D, GEN C)
    1101             : {
    1102         274 :   GEN V = cgetg(3, t_VEC);
    1103         274 :   gel(V,1) = chinese_unit_slice(A, U, B, isintzero(D) ? NULL: D, C, P, &gel(V,2));
    1104         274 :   return V;
    1105             : }
    1106             : 
    1107             : /* Let x = \prod X[i]^E[i] = u, return u.
    1108             :  * If dX != NULL, X[i] = nX[i] / dX[i] where nX[i] is a ZX, dX[i] in Z */
    1109             : static GEN
    1110          94 : chinese_unit(GEN nf, GEN nX, GEN dX, GEN U, ulong bnd)
    1111             : {
    1112          94 :   pari_sp av = avma;
    1113          94 :   GEN f = nf_get_index(nf), T = nf_get_pol(nf), invzk = nf_get_invzk(nf);
    1114             :   GEN H, mod;
    1115             :   forprime_t S;
    1116          94 :   GEN worker = snm_closure(is_entry("_chinese_unit_worker"),
    1117             :                mkcol5(nX, U, invzk, dX? dX: gen_0, T));
    1118          94 :   init_modular_big(&S);
    1119          94 :   H = gen_crt("chinese_units", worker, &S, f, bnd, 0, &mod, nmV_chinese_center, FpM_center);
    1120          94 :   settyp(H, t_VEC); return gerepilecopy(av, H);
    1121             : }
    1122             : 
    1123             : /* *pE a ZM */
    1124             : static void
    1125         164 : ZM_remove_unused(GEN *pE, GEN *pX)
    1126             : {
    1127         164 :   long j, k, l = lg(*pX);
    1128         164 :   GEN E = *pE, v = cgetg(l, t_VECSMALL);
    1129       16349 :   for (j = k = 1; j < l; j++)
    1130       16185 :     if (!ZMrow_equal0(E, j)) v[k++] = j;
    1131         164 :   if (k < l)
    1132             :   {
    1133         164 :     setlg(v, k);
    1134         164 :     *pX = vecpermute(*pX,v);
    1135         164 :     *pE = rowpermute(E,v);
    1136             :   }
    1137         164 : }
    1138             : 
    1139             : /* s = -log|norm(x)|/N */
    1140             : static GEN
    1141     1235021 : fixarch(GEN x, GEN s, long R1)
    1142             : {
    1143             :   long i, l;
    1144     1235021 :   GEN y = cgetg_copy(x, &l);
    1145     3418935 :   for (i = 1; i <= R1; i++) gel(y,i) = gadd(s, gel(x,i));
    1146     1735509 :   for (     ; i <   l; i++) gel(y,i) = gadd(s, gmul2n(gel(x,i),-1));
    1147     1235030 :   return y;
    1148             : }
    1149             : 
    1150             : static GEN
    1151       63621 : getfu(GEN nf, GEN *ptA, GEN *ptU, long prec)
    1152             : {
    1153       63621 :   GEN U, y, matep, A, T = nf_get_pol(nf), M = nf_get_M(nf);
    1154       63621 :   long e, j, R1, RU, N = degpol(T);
    1155             : 
    1156       63621 :   R1 = nf_get_r1(nf); RU = (N+R1) >> 1;
    1157       63621 :   if (RU == 1) return cgetg(1,t_VEC);
    1158             : 
    1159       48431 :   A = *ptA;
    1160       48431 :   matep = cgetg(RU,t_MAT);
    1161      117388 :   for (j = 1; j < RU; j++)
    1162             :   {
    1163       68955 :     GEN Aj = gel(A,j), s = gdivgs(RgV_sum(real_i(Aj)), -N);
    1164       68955 :     gel(matep,j) = fixarch(Aj, s, R1);
    1165             :   }
    1166       48433 :   U = lll(real_i(matep));
    1167       48433 :   if (lg(U) < RU) return not_given(fupb_PRECI);
    1168       48433 :   if (ptU) { *ptU = U; *ptA = A = RgM_ZM_mul(A,U); }
    1169       48433 :   y = RgM_ZM_mul(matep,U);
    1170       48433 :   e = RgM_expbitprec(y);
    1171       48433 :   if (e >= 0) return not_given(e == LONG_MAX? fupb_LARGE: fupb_PRECI);
    1172       48377 :   if (prec <= 0) prec = gprecision(A);
    1173       48377 :   y = RgM_solve_realimag(M, gexp(y,prec));
    1174       48377 :   if (!y) return not_given(fupb_PRECI);
    1175       48377 :   y = grndtoi(y, &e); if (e >= 0) return not_given(fupb_PRECI);
    1176       48065 :   settyp(y, t_VEC);
    1177             : 
    1178       48065 :   if (!ptU) *ptA = A = RgM_ZM_mul(A, U);
    1179      116302 :   for (j = 1; j < RU; j++)
    1180             :   { /* y[i] are hopefully unit generators. Normalize: smallest T2 norm */
    1181       68244 :     GEN u = gel(y,j), v = zk_inv(nf, u);
    1182       68245 :     if (!v || !is_pm1(Q_denom(v)) || ZV_isscalar(u))
    1183           8 :       return not_given(fupb_PRECI);
    1184       68237 :     if (gcmp(RgC_fpnorml2(v,DEFAULTPREC), RgC_fpnorml2(u,DEFAULTPREC)) < 0)
    1185             :     {
    1186       28528 :       gel(A,j) = RgC_neg(gel(A,j));
    1187       28528 :       if (ptU) gel(U,j) = ZC_neg(gel(U,j));
    1188       28528 :       u = v;
    1189             :     }
    1190       68237 :     gel(y,j) = nf_to_scalar_or_alg(nf, u);
    1191             :   }
    1192       48058 :   return y;
    1193             : }
    1194             : 
    1195             : static void
    1196           0 : err_units() { pari_err_PREC("makeunits [cannot get units, use bnfinit(,1)]"); }
    1197             : 
    1198             : /* bound for log2 |sigma(u)|, sigma complex embedding, u fundamental unit
    1199             :  * attached to bnf_get_logfu */
    1200             : static double
    1201          94 : log2fubound(GEN bnf)
    1202             : {
    1203          94 :   GEN LU = bnf_get_logfu(bnf);
    1204          94 :   long i, j, l = lg(LU), r1 = nf_get_r1(bnf_get_nf(bnf));
    1205          94 :   double e = 0.0;
    1206         330 :   for (j = 1; j < l; j++)
    1207             :   {
    1208         236 :     GEN u = gel(LU,j);
    1209         624 :     for (i = 1; i <= r1; i++)
    1210             :     {
    1211         388 :       GEN E = real_i(gel(u,i));
    1212         388 :       e = maxdd(e, gtodouble(E));
    1213             :     }
    1214         842 :     for (     ; i <= l; i++)
    1215             :     {
    1216         606 :       GEN E = real_i(gel(u,i));
    1217         606 :       e = maxdd(e, gtodouble(E) / 2);
    1218             :     }
    1219             :   }
    1220          94 :   return e / M_LN2;
    1221             : }
    1222             : /* bound for log2(|RgM_solve_realimag(M, y)|_oo / |y|_oo)*/
    1223             : static double
    1224          94 : log2Mbound(GEN nf)
    1225             : {
    1226          94 :   GEN G = nf_get_G(nf), D = nf_get_disc(nf);
    1227          94 :   long r2 = nf_get_r2(nf), l = lg(G), i;
    1228          94 :   double e, d = dbllog2(D)/2 - r2 * M_LN2; /* log2 |det(split_realimag(M))| */
    1229          94 :   e = log2(nf_get_degree(nf));
    1230         535 :   for (i = 2; i < l; i++) e += dbllog2(gnorml2(gel(G,i))); /* Hadamard bound */
    1231          94 :   return e / 2 - d;
    1232             : }
    1233             : 
    1234             : static GEN
    1235          94 : vec_chinese_units(GEN bnf)
    1236             : {
    1237          94 :   GEN nf = bnf_get_nf(bnf), SUnits = bnf_get_sunits(bnf);
    1238          94 :   double bnd = ceil(log2Mbound(nf) + log2fubound(bnf));
    1239          94 :   GEN X, dX, Y, U, f = nf_get_index(nf);
    1240          94 :   long j, l, v = nf_get_varn(nf);
    1241          94 :   if (!SUnits) err_units(); /* no compact units */
    1242          94 :   Y = gel(SUnits,1);
    1243          94 :   U = gel(SUnits,2);
    1244          94 :   ZM_remove_unused(&U, &Y); l = lg(Y); X = cgetg(l, t_VEC);
    1245          94 :   if (is_pm1(f)) f = dX = NULL; else dX = cgetg(l, t_VEC);
    1246        5153 :   for (j = 1; j < l; j++)
    1247             :   {
    1248        5059 :     GEN t = nf_to_scalar_or_alg(nf, gel(Y,j));
    1249        5059 :     if (f)
    1250             :     {
    1251             :       GEN den;
    1252        4202 :       t = Q_remove_denom(t, &den);
    1253        4202 :       gel(dX,j) = den ? den: gen_1;
    1254             :     }
    1255        5059 :     gel(X,j) = typ(t) == t_INT? scalarpol_shallow(t,v): t;
    1256             :   }
    1257          94 :   if (dblexpo(bnd) >= BITS_IN_LONG)
    1258           0 :     pari_err_OVERFLOW("vec_chinese_units [units too large]");
    1259          94 :   return chinese_unit(nf, X, dX, U, (ulong)bnd);
    1260             : }
    1261             : 
    1262             : static GEN
    1263       24894 : makeunits(GEN bnf)
    1264             : {
    1265       24894 :   GEN nf = bnf_get_nf(bnf), fu = bnf_get_fu_nocheck(bnf);
    1266       24894 :   GEN tu = nf_to_scalar_or_basis(nf, bnf_get_tuU(bnf));
    1267       24894 :   fu = (typ(fu) == t_MAT)? vec_chinese_units(bnf): matalgtobasis(nf, fu);
    1268       24894 :   return vec_prepend(fu, tu);
    1269             : }
    1270             : 
    1271             : /*******************************************************************/
    1272             : /*                                                                 */
    1273             : /*           PRINCIPAL IDEAL ALGORITHM (DISCRETE LOG)              */
    1274             : /*                                                                 */
    1275             : /*******************************************************************/
    1276             : 
    1277             : /* G: prime ideals, E: vector of nonnegative exponents.
    1278             :  * C = possible extra prime (^1) or NULL
    1279             :  * Return Norm (product) */
    1280             : static GEN
    1281          69 : get_norm_fact_primes(GEN G, GEN E, GEN C)
    1282             : {
    1283          69 :   pari_sp av=avma;
    1284          69 :   GEN N = gen_1, P, p;
    1285          69 :   long i, c = lg(E);
    1286          69 :   for (i=1; i<c; i++)
    1287             :   {
    1288           0 :     GEN ex = gel(E,i);
    1289           0 :     long s = signe(ex);
    1290           0 :     if (!s) continue;
    1291             : 
    1292           0 :     P = gel(G,i); p = pr_get_p(P);
    1293           0 :     N = mulii(N, powii(p, mului(pr_get_f(P), ex)));
    1294             :   }
    1295          69 :   if (C) N = mulii(N, pr_norm(C));
    1296          69 :   return gerepileuptoint(av, N);
    1297             : }
    1298             : 
    1299             : /* gen: HNF ideals */
    1300             : static GEN
    1301     1160464 : get_norm_fact(GEN gen, GEN ex, GEN *pd)
    1302             : {
    1303     1160464 :   long i, c = lg(ex);
    1304             :   GEN d,N,I,e,n,ne,de;
    1305     1160464 :   d = N = gen_1;
    1306     1456302 :   for (i=1; i<c; i++)
    1307      295838 :     if (signe(gel(ex,i)))
    1308             :     {
    1309      175511 :       I = gel(gen,i); e = gel(ex,i); n = ZM_det_triangular(I);
    1310      175511 :       ne = powii(n,e);
    1311      175511 :       de = equalii(n, gcoeff(I,1,1))? ne: powii(gcoeff(I,1,1), e);
    1312      175511 :       N = mulii(N, ne);
    1313      175511 :       d = mulii(d, de);
    1314             :     }
    1315     1160464 :   *pd = d; return N;
    1316             : }
    1317             : 
    1318             : static GEN
    1319     1321302 : get_pr_lists(GEN FB, long N, int list_pr)
    1320             : {
    1321             :   GEN pr, L;
    1322     1321302 :   long i, l = lg(FB), p, pmax;
    1323             : 
    1324     1321302 :   pmax = 0;
    1325     9141602 :   for (i=1; i<l; i++)
    1326             :   {
    1327     7820300 :     pr = gel(FB,i); p = pr_get_smallp(pr);
    1328     7820300 :     if (p > pmax) pmax = p;
    1329             :   }
    1330     1321302 :   L = const_vec(pmax, NULL);
    1331     1321303 :   if (list_pr)
    1332             :   {
    1333           0 :     for (i=1; i<l; i++)
    1334             :     {
    1335           0 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1336           0 :       if (!L[p]) gel(L,p) = vectrunc_init(N+1);
    1337           0 :       vectrunc_append(gel(L,p), pr);
    1338             :     }
    1339           0 :     for (p=1; p<=pmax; p++)
    1340           0 :       if (L[p]) gen_sort_inplace(gel(L,p), (void*)&cmp_prime_over_p,
    1341             :                                  &cmp_nodata, NULL);
    1342             :   }
    1343             :   else
    1344             :   {
    1345     9141605 :     for (i=1; i<l; i++)
    1346             :     {
    1347     7820302 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1348     7820302 :       if (!L[p]) gel(L,p) = vecsmalltrunc_init(N+1);
    1349     7820301 :       vecsmalltrunc_append(gel(L,p), i);
    1350             :     }
    1351             :   }
    1352     1321303 :   return L;
    1353             : }
    1354             : 
    1355             : /* recover FB, LV, iLP, KCZ from Vbase */
    1356             : static GEN
    1357     1321302 : recover_partFB(FB_t *F, GEN Vbase, long N)
    1358             : {
    1359     1321302 :   GEN FB, LV, iLP, L = get_pr_lists(Vbase, N, 0);
    1360     1321303 :   long l = lg(L), p, ip, i;
    1361             : 
    1362     1321303 :   i = ip = 0;
    1363     1321303 :   FB = cgetg(l, t_VECSMALL);
    1364     1321302 :   iLP= cgetg(l, t_VECSMALL);
    1365     1321301 :   LV = cgetg(l, t_VEC);
    1366    19811818 :   for (p = 2; p < l; p++)
    1367             :   {
    1368    18490516 :     if (!L[p]) continue;
    1369     4282749 :     FB[++i] = p;
    1370     4282749 :     gel(LV,p) = vecpermute(Vbase, gel(L,p));
    1371     4282749 :     iLP[p]= ip; ip += lg(gel(L,p))-1;
    1372             :   }
    1373     1321302 :   F->KCZ = i;
    1374     1321302 :   F->KC = ip;
    1375     1321302 :   F->FB = FB; setlg(FB, i+1);
    1376     1321302 :   F->prodZ = zv_prod_Z(F->FB);
    1377     1321296 :   F->LV = LV;
    1378     1321296 :   F->iLP= iLP; return L;
    1379             : }
    1380             : 
    1381             : /* add v^e to factorization */
    1382             : static void
    1383       29881 : add_to_fact(long v, long e, FACT *fact)
    1384             : {
    1385       29881 :   long i, l = fact[0].pr;
    1386       56890 :   for (i=1; i<=l && fact[i].pr < v; i++)/*empty*/;
    1387       29881 :   if (i <= l && fact[i].pr == v) fact[i].ex += e; else store(v, e, fact);
    1388       29881 : }
    1389             : static void
    1390           0 : inv_fact(FACT *fact)
    1391             : {
    1392           0 :   long i, l = fact[0].pr;
    1393           0 :   for (i=1; i<=l; i++) fact[i].ex = -fact[i].ex;
    1394           0 : }
    1395             : 
    1396             : /* L (small) list of primes above the same p including pr. Return pr index */
    1397             : static int
    1398        3307 : pr_index(GEN L, GEN pr)
    1399             : {
    1400        3307 :   long j, l = lg(L);
    1401        3307 :   GEN al = pr_get_gen(pr);
    1402        3307 :   for (j=1; j<l; j++)
    1403        3307 :     if (ZV_equal(al, pr_get_gen(gel(L,j)))) return j;
    1404           0 :   pari_err_BUG("codeprime");
    1405             :   return 0; /* LCOV_EXCL_LINE */
    1406             : }
    1407             : 
    1408             : static long
    1409        3307 : Vbase_to_FB(FB_t *F, GEN pr)
    1410             : {
    1411        3307 :   long p = pr_get_smallp(pr);
    1412        3307 :   return F->iLP[p] + pr_index(gel(F->LV,p), pr);
    1413             : }
    1414             : 
    1415             : /* x, y 2 extended ideals whose first component is an integral HNF and second
    1416             :  * a famat */
    1417             : static GEN
    1418        3561 : idealHNF_mulred(GEN nf, GEN x, GEN y)
    1419             : {
    1420        3561 :   GEN A = idealHNF_mul(nf, gel(x,1), gel(y,1));
    1421        3561 :   GEN F = famat_mul_shallow(gel(x,2), gel(y,2));
    1422        3561 :   return idealred(nf, mkvec2(A, F));
    1423             : }
    1424             : /* idealred(x * pr^n), n > 0 is small, x extended ideal. Reduction in order to
    1425             :  * avoid prec pb: don't let id become too large as lgsub increases */
    1426             : static GEN
    1427        4544 : idealmulpowprime2(GEN nf, GEN x, GEN pr, ulong n)
    1428             : {
    1429        4544 :   GEN A = idealmulpowprime(nf, gel(x,1), pr, utoipos(n));
    1430        4544 :   return mkvec2(A, gel(x,2));
    1431             : }
    1432             : static GEN
    1433       65363 : init_famat(GEN x) { return mkvec2(x, trivial_fact()); }
    1434             : /* optimized idealfactorback + reduction; z = init_famat() */
    1435             : static GEN
    1436       28728 : genback(GEN z, GEN nf, GEN P, GEN E)
    1437             : {
    1438       28728 :   long i, l = lg(E);
    1439       28728 :   GEN I = NULL;
    1440       76442 :   for (i = 1; i < l; i++)
    1441       47713 :     if (signe(gel(E,i)))
    1442             :     {
    1443             :       GEN J;
    1444       32288 :       gel(z,1) = gel(P,i);
    1445       32288 :       J = idealpowred(nf, z, gel(E,i));
    1446       32289 :       I = I? idealHNF_mulred(nf, I, J): J;
    1447             :     }
    1448       28729 :   return I; /* != NULL since a generator */
    1449             : }
    1450             : 
    1451             : /* return famat y (principal ideal) such that y / x is smooth [wrt Vbase] */
    1452             : static GEN
    1453     1337641 : SPLIT(FB_t *F, GEN nf, GEN x, GEN Vbase, FACT *fact)
    1454             : {
    1455     1337641 :   GEN vecG, ex, Ly, y, x0, Nx = ZM_det_triangular(x);
    1456             :   long nbtest_lim, nbtest, i, j, k, ru, lgsub;
    1457             :   pari_sp av;
    1458             : 
    1459             :   /* try without reduction if x is small */
    1460     2675097 :   if (gexpo(gcoeff(x,1,1)) < 100 &&
    1461     1487265 :       can_factor(F, nf, x, NULL, Nx, fact)) return NULL;
    1462             : 
    1463     1187834 :   av = avma;
    1464     1187834 :   Ly = idealpseudominvec(x, nf_get_roundG(nf));
    1465     1231132 :   for(k=1; k<lg(Ly); k++)
    1466             :   {
    1467     1222335 :     y = gel(Ly,k);
    1468     1222335 :     if (factorgen(F, nf, x, Nx, y, fact)) return y;
    1469             :   }
    1470        8797 :   set_avma(av);
    1471             : 
    1472             :   /* reduce in various directions */
    1473        8797 :   ru = lg(nf_get_roots(nf));
    1474        8797 :   vecG = cgetg(ru, t_VEC);
    1475       14312 :   for (j=1; j<ru; j++)
    1476             :   {
    1477       12571 :     gel(vecG,j) = nf_get_Gtwist1(nf, j);
    1478       12571 :     av = avma;
    1479       12571 :     Ly = idealpseudominvec(x, gel(vecG,j));
    1480       41421 :     for(k=1; k<lg(Ly); k++)
    1481             :     {
    1482       35906 :       y = gel(Ly,k);
    1483       35906 :       if (factorgen(F, nf, x, Nx, y, fact)) return y;
    1484             :     }
    1485        5515 :     set_avma(av);
    1486             :   }
    1487             : 
    1488             :   /* tough case, multiply by random products */
    1489        1741 :   lgsub = 3;
    1490        1741 :   ex = cgetg(lgsub, t_VECSMALL);
    1491        1741 :   x0 = init_famat(x);
    1492        1741 :   nbtest = 1; nbtest_lim = 4;
    1493             :   for(;;)
    1494         622 :   {
    1495        2363 :     GEN Ired, I, NI, id = x0;
    1496        2363 :     av = avma;
    1497        2363 :     if (DEBUGLEVEL>2) err_printf("# ideals tried = %ld\n",nbtest);
    1498        7208 :     for (i=1; i<lgsub; i++)
    1499             :     {
    1500        4845 :       ex[i] = random_bits(RANDOM_BITS);
    1501        4845 :       if (ex[i]) id = idealmulpowprime2(nf, id, gel(Vbase,i), ex[i]);
    1502             :     }
    1503        2363 :     if (id == x0) continue;
    1504             :     /* I^(-1) * \prod Vbase[i]^ex[i] = (id[2]) / x */
    1505             : 
    1506        2363 :     I = gel(id,1); NI = ZM_det_triangular(I);
    1507        2363 :     if (can_factor(F, nf, I, NULL, NI, fact))
    1508             :     {
    1509           0 :       inv_fact(fact); /* I^(-1) */
    1510           0 :       for (i=1; i<lgsub; i++)
    1511           0 :         if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1512           0 :       return gel(id,2);
    1513             :     }
    1514        2363 :     Ired = ru == 2? I: ZM_lll(I, 0.99, LLL_INPLACE);
    1515        3987 :     for (j=1; j<ru; j++)
    1516             :     {
    1517        3365 :       pari_sp av2 = avma;
    1518        3365 :       Ly = idealpseudominvec(Ired, gel(vecG,j));
    1519       11463 :       for (k=1; k < lg(Ly); k++)
    1520             :       {
    1521        9839 :         y = gel(Ly,k);
    1522        9839 :         if (factorgen(F, nf, I, NI, y, fact))
    1523             :         {
    1524        5251 :           for (i=1; i<lgsub; i++)
    1525        3510 :             if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1526        1741 :           return famat_mul_shallow(gel(id,2), y);
    1527             :         }
    1528             :       }
    1529        1624 :       set_avma(av2);
    1530             :     }
    1531         622 :     set_avma(av);
    1532         622 :     if (++nbtest > nbtest_lim)
    1533             :     {
    1534          28 :       nbtest = 0;
    1535          28 :       if (++lgsub < minss(8, lg(Vbase)-1))
    1536             :       {
    1537          28 :         nbtest_lim <<= 1;
    1538          28 :         ex = cgetg(lgsub, t_VECSMALL);
    1539             :       }
    1540           0 :       else nbtest_lim = LONG_MAX; /* don't increase further */
    1541          28 :       if (DEBUGLEVEL>2) err_printf("SPLIT: increasing factor base [%ld]\n",lgsub);
    1542             :     }
    1543             :   }
    1544             : }
    1545             : 
    1546             : INLINE GEN
    1547     1326214 : bnf_get_W(GEN bnf) { return gel(bnf,1); }
    1548             : INLINE GEN
    1549     2642492 : bnf_get_B(GEN bnf) { return gel(bnf,2); }
    1550             : INLINE GEN
    1551     2676974 : bnf_get_C(GEN bnf) { return gel(bnf,4); }
    1552             : INLINE GEN
    1553     1321321 : bnf_get_vbase(GEN bnf) { return gel(bnf,5); }
    1554             : INLINE GEN
    1555     1321238 : bnf_get_Ur(GEN bnf) { return gmael(bnf,9,1); }
    1556             : INLINE GEN
    1557      271617 : bnf_get_ga(GEN bnf) { return gmael(bnf,9,2); }
    1558             : INLINE GEN
    1559      276574 : bnf_get_GD(GEN bnf) { return gmael(bnf,9,3); }
    1560             : 
    1561             : /* Return y (as an elt of K or a t_MAT representing an elt in Z[K])
    1562             :  * such that x / (y) is smooth and store the exponents of  its factorization
    1563             :  * on g_W and g_B in Wex / Bex; return NULL for y = 1 */
    1564             : static GEN
    1565     1321238 : split_ideal(GEN bnf, GEN x, GEN *pWex, GEN *pBex)
    1566             : {
    1567     1321238 :   GEN L, y, Vbase = bnf_get_vbase(bnf);
    1568     1321238 :   GEN Wex, W  = bnf_get_W(bnf);
    1569     1321238 :   GEN Bex, B  = bnf_get_B(bnf);
    1570             :   long p, j, i, l, nW, nB;
    1571             :   FACT *fact;
    1572             :   FB_t F;
    1573             : 
    1574     1321239 :   L = recover_partFB(&F, Vbase, lg(x)-1);
    1575     1321234 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    1576     1321233 :   y = SPLIT(&F, bnf_get_nf(bnf), x, Vbase, fact);
    1577     1321237 :   nW = lg(W)-1; *pWex = Wex = zero_zv(nW);
    1578     1321235 :   nB = lg(B)-1; *pBex = Bex = zero_zv(nB); l = lg(F.FB);
    1579     1321235 :   p = j = 0; /* -Wall */
    1580     1968387 :   for (i = 1; i <= fact[0].pr; i++)
    1581             :   { /* decode index C = ip+j --> (p,j) */
    1582      647152 :     long a, b, t, C = fact[i].pr;
    1583     1824999 :     for (t = 1; t < l; t++)
    1584             :     {
    1585     1751795 :       long q = F.FB[t], k = C - F.iLP[q];
    1586     1751795 :       if (k <= 0) break;
    1587     1177847 :       p = q;
    1588     1177847 :       j = k;
    1589             :     }
    1590      647152 :     a = gel(L, p)[j];
    1591      647152 :     b = a - nW;
    1592      647152 :     if (b <= 0) Wex[a] = y? -fact[i].ex: fact[i].ex;
    1593      493784 :     else        Bex[b] = y? -fact[i].ex: fact[i].ex;
    1594             :   }
    1595     1321235 :   return y;
    1596             : }
    1597             : 
    1598             : GEN
    1599     1039009 : init_red_mod_units(GEN bnf, long prec)
    1600             : {
    1601     1039009 :   GEN s = gen_0, p1,s1,mat, logfu = bnf_get_logfu(bnf);
    1602     1039009 :   long i,j, RU = lg(logfu);
    1603             : 
    1604     1039009 :   if (RU == 1) return NULL;
    1605     1039009 :   mat = cgetg(RU,t_MAT);
    1606     2356120 :   for (j=1; j<RU; j++)
    1607             :   {
    1608     1317112 :     p1 = cgetg(RU+1,t_COL); gel(mat,j) = p1;
    1609     1317112 :     s1 = gen_0;
    1610     3261277 :     for (i=1; i<RU; i++)
    1611             :     {
    1612     1944166 :       gel(p1,i) = real_i(gcoeff(logfu,i,j));
    1613     1944166 :       s1 = mpadd(s1, mpsqr(gel(p1,i)));
    1614             :     }
    1615     1317111 :     gel(p1,RU) = gen_0; if (mpcmp(s1,s) > 0) s = s1;
    1616             :   }
    1617     1039008 :   s = gsqrt(gmul2n(s,RU),prec);
    1618     1039009 :   if (expo(s) < 27) s = utoipos(1UL << 27);
    1619     1039009 :   return mkvec2(mat, s);
    1620             : }
    1621             : 
    1622             : /* z computed above. Return unit exponents that would reduce col (arch) */
    1623             : GEN
    1624     1039009 : red_mod_units(GEN col, GEN z)
    1625             : {
    1626             :   long i,RU;
    1627             :   GEN x,mat,N2;
    1628             : 
    1629     1039009 :   if (!z) return NULL;
    1630     1039009 :   mat= gel(z,1);
    1631     1039009 :   N2 = gel(z,2);
    1632     1039009 :   RU = lg(mat); x = cgetg(RU+1,t_COL);
    1633     2356121 :   for (i=1; i<RU; i++) gel(x,i) = real_i(gel(col,i));
    1634     1039009 :   gel(x,RU) = N2;
    1635     1039009 :   x = lll(shallowconcat(mat,x));
    1636     1039009 :   if (typ(x) != t_MAT || lg(x) <= RU) return NULL;
    1637     1039009 :   x = gel(x,RU);
    1638     1039009 :   if (signe(gel(x,RU)) < 0) x = gneg_i(x);
    1639     1039009 :   if (!gequal1(gel(x,RU))) pari_err_BUG("red_mod_units");
    1640     1039009 :   setlg(x,RU); return x;
    1641             : }
    1642             : 
    1643             : static GEN
    1644     2126027 : add(GEN a, GEN t) { return a = a? RgC_add(a,t): t; }
    1645             : 
    1646             : /* [x] archimedian components, A column vector. return [x] A */
    1647             : static GEN
    1648     1984587 : act_arch(GEN A, GEN x)
    1649             : {
    1650             :   GEN a;
    1651     1984587 :   long i,l = lg(A), tA = typ(A);
    1652     1984587 :   if (tA == t_MAT)
    1653             :   { /* assume lg(x) >= l */
    1654      191158 :     a = cgetg(l, t_MAT);
    1655      280888 :     for (i=1; i<l; i++) gel(a,i) = act_arch(gel(A,i), x);
    1656      191155 :     return a;
    1657             :   }
    1658     1793429 :   if (l==1) return cgetg(1, t_COL);
    1659     1793429 :   a = NULL;
    1660     1793429 :   if (tA == t_VECSMALL)
    1661             :   {
    1662     6800698 :     for (i=1; i<l; i++)
    1663             :     {
    1664     5640237 :       long c = A[i];
    1665     5640237 :       if (c) a = add(a, gmulsg(c, gel(x,i)));
    1666             :     }
    1667             :   }
    1668             :   else
    1669             :   { /* A a t_COL of t_INT. Assume lg(A)==lg(x) */
    1670     1381548 :     for (i=1; i<l; i++)
    1671             :     {
    1672      748585 :       GEN c = gel(A,i);
    1673      748585 :       if (signe(c)) a = add(a, gmul(c, gel(x,i)));
    1674             :     }
    1675             :   }
    1676     1793424 :   return a? a: zerocol(lgcols(x)-1);
    1677             : }
    1678             : /* act_arch(matdiagonal(v), x) */
    1679             : static GEN
    1680       63720 : diagact_arch(GEN v, GEN x)
    1681             : {
    1682       63720 :   long i, l = lg(v);
    1683       63720 :   GEN a = cgetg(l, t_MAT);
    1684       92518 :   for (i = 1; i < l; i++) gel(a,i) = gmul(gel(x,i), gel(v,i));
    1685       63720 :   return a;
    1686             : }
    1687             : 
    1688             : static long
    1689     1339405 : prec_arch(GEN bnf)
    1690             : {
    1691     1339405 :   GEN a = bnf_get_C(bnf);
    1692     1339405 :   long i, l = lg(a), prec;
    1693             : 
    1694     1339405 :   for (i=1; i<l; i++)
    1695     1339321 :     if ( (prec = gprecision(gel(a,i))) ) return prec;
    1696          84 :   return DEFAULTPREC;
    1697             : }
    1698             : 
    1699             : static long
    1700        3778 : needed_bitprec(GEN x)
    1701             : {
    1702        3778 :   long i, e = 0, l = lg(x);
    1703       22246 :   for (i = 1; i < l; i++)
    1704             :   {
    1705       18468 :     GEN c = gel(x,i);
    1706       18468 :     long f = gexpo(c) - gprecision(c);
    1707       18468 :     if (f > e) e = f;
    1708             :   }
    1709        3778 :   return e;
    1710             : }
    1711             : 
    1712             : /* col = archimedian components of x, Nx its norm, dx a multiple of its
    1713             :  * denominator. Return x or NULL (fail) */
    1714             : GEN
    1715     1166076 : isprincipalarch(GEN bnf, GEN col, GEN kNx, GEN e, GEN dx, long *pe)
    1716             : {
    1717             :   GEN nf, x, y, logfu, s, M;
    1718     1166076 :   long N, prec = gprecision(col);
    1719     1166075 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf); M = nf_get_M(nf);
    1720     1166075 :   if (!prec) prec = prec_arch(bnf);
    1721     1166075 :   *pe = 128;
    1722     1166075 :   logfu = bnf_get_logfu(bnf);
    1723     1166075 :   N = nf_get_degree(nf);
    1724     1166074 :   if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
    1725     1166075 :   if (lg(col) > 2)
    1726             :   { /* reduce mod units */
    1727     1039009 :     GEN u, z = init_red_mod_units(bnf,prec);
    1728     1039009 :     if (!(u = red_mod_units(col,z))) return NULL;
    1729     1039009 :     col = RgC_add(col, RgM_RgC_mul(logfu, u));
    1730     1039008 :     if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
    1731             :   }
    1732     1166074 :   s = divru(mulir(e, glog(kNx,prec)), N);
    1733     1166068 :   col = fixarch(col, s, nf_get_r1(nf));
    1734     1166075 :   if (RgC_expbitprec(col) >= 0) return NULL;
    1735     1165621 :   col = gexp(col, prec);
    1736             :   /* d.alpha such that x = alpha \prod gj^ej */
    1737     1165621 :   x = RgM_solve_realimag(M,col); if (!x) return NULL;
    1738     1165623 :   x = RgC_Rg_mul(x, dx);
    1739     1165620 :   y = grndtoi(x, pe);
    1740     1165622 :   if (*pe > -5) { *pe = needed_bitprec(x); return NULL; }
    1741     1161844 :   return RgC_Rg_div(y, dx);
    1742             : }
    1743             : 
    1744             : /* y = C \prod g[i]^e[i] ? */
    1745             : static int
    1746     1157823 : fact_ok(GEN nf, GEN y, GEN C, GEN g, GEN e)
    1747             : {
    1748     1157823 :   pari_sp av = avma;
    1749     1157823 :   long i, c = lg(e);
    1750     1157823 :   GEN z = C? C: gen_1;
    1751     1434824 :   for (i=1; i<c; i++)
    1752      277002 :     if (signe(gel(e,i))) z = idealmul(nf, z, idealpow(nf, gel(g,i), gel(e,i)));
    1753     1157822 :   if (typ(z) != t_MAT) z = idealhnf_shallow(nf,z);
    1754     1157823 :   if (typ(y) != t_MAT) y = idealhnf_shallow(nf,y);
    1755     1157823 :   return gc_bool(av, ZM_equal(y,z));
    1756             : }
    1757             : static GEN
    1758     1321237 : ZV_divrem(GEN A, GEN B, GEN *pR)
    1759             : {
    1760     1321237 :   long i, l = lg(A);
    1761     1321237 :   GEN Q = cgetg(l, t_COL), R = cgetg(l, t_COL);
    1762     1826586 :   for (i = 1; i < l; i++) gel(Q,i) = truedvmdii(gel(A,i), gel(B,i), &gel(R,i));
    1763     1321238 :   *pR = R; return Q;
    1764             : }
    1765             : 
    1766             : static GEN
    1767     1321238 : Ur_ZC_mul(GEN bnf, GEN v)
    1768             : {
    1769     1321238 :   GEN w, U = bnf_get_Ur(bnf);
    1770     1321238 :   long i, l = lg(bnf_get_cyc(bnf)); /* may be < lgcols(U) */
    1771             : 
    1772     1321236 :   w = cgetg(l, t_COL);
    1773     1826585 :   for (i = 1; i < l; i++) gel(w,i) = ZMrow_ZC_mul(U, v, i);
    1774     1321237 :   return w;
    1775             : }
    1776             : 
    1777             : static GEN
    1778        7074 : ZV_mul(GEN x, GEN y)
    1779             : {
    1780        7074 :   long i, l = lg(x);
    1781        7074 :   GEN z = cgetg(l, t_COL);
    1782       30826 :   for (i = 1; i < l; i++) gel(z,i) = mulii(gel(x,i), gel(y,i));
    1783        7074 :   return z;
    1784             : }
    1785             : static int
    1786     1157215 : dump_gen(GEN SUnits, GEN x, long flag)
    1787             : {
    1788             :   GEN d;
    1789             :   long e;
    1790     1157215 :   if (!(flag & nf_GENMAT) || !SUnits) return 0;
    1791      266325 :   e = gexpo(gel(SUnits,2)); if (e > 64) return 0; /* U large */
    1792      266229 :   x = Q_remove_denom(x, &d);
    1793      266229 :   return (d && expi(d) > 32) || gexpo(x) > 32;
    1794             : }
    1795             : 
    1796             : /* assume x in HNF; cf class_group_gen for notations. Return NULL iff
    1797             :  * flag & nf_FORCE and computation of principal ideal generator fails */
    1798             : static GEN
    1799     1337551 : isprincipalall(GEN bnf, GEN x, long *pprec, long flag)
    1800             : {
    1801             :   GEN xar, Wex, Bex, gen, xc, col, A, Q, R, UA, SUnits;
    1802     1337551 :   GEN C = bnf_get_C(bnf), nf = bnf_get_nf(bnf), cyc = bnf_get_cyc(bnf);
    1803             :   long nB, nW, e;
    1804             : 
    1805     1337551 :   if (lg(cyc) == 1 && !(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL)))
    1806        4725 :     return cgetg(1,t_COL);
    1807     1332826 :   if (lg(x) == 2)
    1808             :   { /* nf = Q */
    1809          84 :     col = gel(x,1);
    1810          84 :     if (flag & nf_GENMAT) col = to_famat_shallow(col, gen_1);
    1811          84 :     return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(cgetg(1,t_COL), col);
    1812             :   }
    1813             : 
    1814     1332742 :   x = Q_primitive_part(x, &xc);
    1815     1332738 :   if (equali1(gcoeff(x,1,1))) /* trivial ideal */
    1816             :   {
    1817       11500 :     R = zerocol(lg(cyc)-1);
    1818       11500 :     if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1819       11451 :     if (flag & nf_GEN_IF_PRINCIPAL)
    1820        6453 :       return scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
    1821        4998 :     if (flag & nf_GENMAT)
    1822        2163 :       col = xc? to_famat_shallow(xc, gen_1): trivial_fact();
    1823             :     else
    1824        2835 :       col = scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
    1825        4998 :     return mkvec2(R, col);
    1826             :   }
    1827     1321238 :   xar = split_ideal(bnf, x, &Wex, &Bex);
    1828             :   /* x = g_W Wex + g_B Bex + [xar] = g_W (Wex - B*Bex) + [xar] + [C_B]Bex */
    1829     1321235 :   A = zc_to_ZC(Wex); nB = lg(Bex)-1;
    1830     1321235 :   if (nB) A = ZC_sub(A, ZM_zc_mul(bnf_get_B(bnf), Bex));
    1831     1321238 :   UA = Ur_ZC_mul(bnf, A);
    1832     1321237 :   Q = ZV_divrem(UA, cyc, &R);
    1833             :   /* g_W (Wex - B*Bex) = G Ur A - [ga]A = G R + [GD]Q - [ga]A
    1834             :    * Finally: x = G R + [xar] + [C_B]Bex + [GD]Q - [ga]A */
    1835     1321238 :   if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1836     1161061 :   if ((flag & nf_GEN_IF_PRINCIPAL) && !ZV_equal0(R)) return gen_0;
    1837             : 
    1838     1161054 :   nW = lg(Wex)-1;
    1839     1161054 :   gen = bnf_get_gen(bnf);
    1840     1161054 :   col = NULL;
    1841     1161054 :   SUnits = bnf_get_sunits(bnf);
    1842     1161054 :   if (lg(R) == 1
    1843      272208 :       || abscmpiu(gel(R,vecindexmax(R)), 4 * (*pprec)) < 0)
    1844             :   { /* q = N (x / prod gj^ej) = N(alpha), denom(alpha) | d */
    1845     1160464 :     GEN d, q = gdiv(ZM_det_triangular(x), get_norm_fact(gen, R, &d));
    1846     1160463 :     col = xar? nf_cxlog(nf, xar, *pprec): NULL;
    1847     1160466 :     if (nB) col = add(col, act_arch(Bex, nW? vecslice(C,nW+1,lg(C)-1): C));
    1848     1160463 :     if (nW) col = add(col, RgC_sub(act_arch(Q, bnf_get_GD(bnf)),
    1849             :                                    act_arch(A, bnf_get_ga(bnf))));
    1850     1160464 :     col = isprincipalarch(bnf, col, q, gen_1, d, &e);
    1851     1160466 :     if (col && (dump_gen(SUnits, col, flag)
    1852     1157215 :                 || !fact_ok(nf,x, col,gen,R))) col = NULL;
    1853             :   }
    1854     1161055 :   if (!col && (flag & nf_GENMAT))
    1855             :   {
    1856        7780 :     if (SUnits)
    1857             :     {
    1858        7298 :       GEN X = gel(SUnits,1), U = gel(SUnits,2), C = gel(SUnits,3);
    1859        7298 :       GEN v = gel(bnf,9), Ge = gel(v,4), M1 = gel(v,5), M2 = gel(v,6);
    1860        7298 :       GEN z = NULL, F = NULL;
    1861        7298 :       if (nB)
    1862             :       {
    1863        7298 :         GEN C2 = nW? vecslice(C, nW+1, lg(C)-1): C;
    1864        7298 :         z = ZM_zc_mul(C2, Bex);
    1865             :       }
    1866        7298 :       if (nW)
    1867             :       { /* [GD]Q - [ga]A = ([X]M1 - [Ge]D) Q - ([X]M2 - [Ge]Ur) A */
    1868        7074 :         GEN C1 = vecslice(C, 1, nW);
    1869        7074 :         GEN v = ZC_sub(ZM_ZC_mul(M1,Q), ZM_ZC_mul(M2,A));
    1870        7074 :         z = add(z, ZM_ZC_mul(C1, v));
    1871        7074 :         F = famat_reduce(famatV_factorback(Ge, ZC_sub(UA, ZV_mul(cyc,Q))));
    1872        7074 :         if (lgcols(F) == 1) F = NULL;
    1873             :       }
    1874             :       /* reduce modulo units and Q^* */
    1875        7298 :       if (lg(U) != 1) z = ZC_sub(z, ZM_ZC_mul(U, RgM_Babai(U,z)));
    1876        7298 :       col = mkmat2(X, z);
    1877        7298 :       if (F) col = famat_mul_shallow(col, F);
    1878        7298 :       col = famat_remove_trivial(col);
    1879        7298 :       if (xar) col = famat_mul_shallow(col, xar);
    1880             :     }
    1881         482 :     else if (!ZV_equal0(R))
    1882             :     { /* in case isprincipalfact calls bnfinit() due to prec trouble...*/
    1883         476 :       GEN y = isprincipalfact(bnf, x, gen, ZC_neg(R), flag);
    1884         476 :       if (typ(y) != t_VEC) return y;
    1885         476 :       col = gel(y,2);
    1886             :     }
    1887             :   }
    1888     1161055 :   if (col)
    1889             :   { /* add back missing content */
    1890     1161497 :     if (xc) col = (typ(col)==t_MAT)? famat_mul_shallow(col,xc)
    1891         532 :                                    : RgC_Rg_mul(col,xc);
    1892     1160965 :     if (typ(col) != t_MAT && lg(col) != 1 && (flag & nf_GENMAT))
    1893     1139492 :       col = to_famat_shallow(col, gen_1);
    1894             :   }
    1895             :   else
    1896             :   {
    1897          90 :     if (e < 0) e = 0;
    1898          90 :     *pprec += nbits2extraprec(e + 128);
    1899          90 :     if (flag & nf_FORCE)
    1900             :     {
    1901          76 :       if (DEBUGLEVEL)
    1902           0 :         pari_warn(warner,"precision too low for generators, e = %ld",e);
    1903          76 :       return NULL;
    1904             :     }
    1905          14 :     pari_warn(warner,"precision too low for generators, not given");
    1906          14 :     col = cgetg(1, t_COL);
    1907             :   }
    1908     1160978 :   return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(R, col);
    1909             : }
    1910             : 
    1911             : static GEN
    1912      461128 : triv_gen(GEN bnf, GEN x, long flag)
    1913             : {
    1914      461128 :   pari_sp av = avma;
    1915      461128 :   GEN nf = bnf_get_nf(bnf);
    1916             :   long c;
    1917      461128 :   if (flag & nf_GEN_IF_PRINCIPAL)
    1918             :   {
    1919           7 :     if (!(flag & nf_GENMAT)) return algtobasis(nf,x);
    1920           7 :     x = nf_to_scalar_or_basis(nf,x);
    1921           7 :     if (typ(x) == t_INT && is_pm1(x)) return trivial_fact();
    1922           0 :     return gerepilecopy(av, to_famat_shallow(x, gen_1));
    1923             :   }
    1924      461121 :   c = lg(bnf_get_cyc(bnf)) - 1;
    1925      461121 :   if (flag & nf_GENMAT)
    1926      451517 :     retmkvec2(zerocol(c), to_famat_shallow(algtobasis(nf,x), gen_1));
    1927        9604 :   if (flag & nf_GEN)
    1928          28 :     retmkvec2(zerocol(c), algtobasis(nf,x));
    1929        9576 :   return zerocol(c);
    1930             : }
    1931             : 
    1932             : GEN
    1933     1766735 : bnfisprincipal0(GEN bnf,GEN x,long flag)
    1934             : {
    1935     1766735 :   pari_sp av = avma;
    1936             :   GEN c, nf;
    1937             :   long pr;
    1938             : 
    1939     1766735 :   bnf = checkbnf(bnf);
    1940     1766735 :   nf = bnf_get_nf(bnf);
    1941     1766735 :   switch( idealtyp(&x, NULL) )
    1942             :   {
    1943       55821 :     case id_PRINCIPAL:
    1944       55821 :       if (gequal0(x)) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1945       55821 :       return triv_gen(bnf, x, flag);
    1946     1687260 :     case id_PRIME:
    1947     1687260 :       if (pr_is_inert(x)) return triv_gen(bnf, pr_get_p(x), flag);
    1948     1281960 :       x = pr_hnf(nf, x);
    1949     1281962 :       break;
    1950       23653 :     case id_MAT:
    1951       23653 :       if (lg(x)==1) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1952       23653 :       if (nf_get_degree(nf) != lg(x)-1)
    1953           0 :         pari_err_TYPE("idealtyp [dimension != degree]", x);
    1954             :   }
    1955     1305615 :   pr = prec_arch(bnf); /* precision of unit matrix */
    1956     1305615 :   c = getrand();
    1957             :   for (;;)
    1958           6 :   {
    1959     1305622 :     pari_sp av1 = avma;
    1960     1305622 :     GEN y = isprincipalall(bnf,x,&pr,flag);
    1961     1305619 :     if (y) return gerepilecopy(av, y);
    1962             : 
    1963           6 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",pr);
    1964           6 :     set_avma(av1); bnf = bnfnewprec_shallow(bnf,pr); setrand(c);
    1965             :   }
    1966             : }
    1967             : GEN
    1968      174478 : isprincipal(GEN bnf,GEN x) { return bnfisprincipal0(bnf,x,0); }
    1969             : 
    1970             : /* FIXME: OBSOLETE */
    1971             : GEN
    1972           0 : isprincipalgen(GEN bnf,GEN x)
    1973           0 : { return bnfisprincipal0(bnf,x,nf_GEN); }
    1974             : GEN
    1975           0 : isprincipalforce(GEN bnf,GEN x)
    1976           0 : { return bnfisprincipal0(bnf,x,nf_FORCE); }
    1977             : GEN
    1978           0 : isprincipalgenforce(GEN bnf,GEN x)
    1979           0 : { return bnfisprincipal0(bnf,x,nf_GEN | nf_FORCE); }
    1980             : 
    1981             : /* lg(u) > 1 */
    1982             : static int
    1983          91 : RgV_is1(GEN u) { return isint1(gel(u,1)) && RgV_isscalar(u); }
    1984             : static GEN
    1985       31858 : add_principal_part(GEN nf, GEN u, GEN v, long flag)
    1986             : {
    1987       31858 :   if (flag & nf_GENMAT)
    1988       14247 :     return (typ(u) == t_COL && RgV_is1(u))? v: famat_mul_shallow(v,u);
    1989             :   else
    1990       17611 :     return nfmul(nf, v, u);
    1991             : }
    1992             : 
    1993             : #if 0
    1994             : /* compute C prod P[i]^e[i],  e[i] >=0 for all i. C may be NULL (omitted)
    1995             :  * e destroyed ! */
    1996             : static GEN
    1997             : expand(GEN nf, GEN C, GEN P, GEN e)
    1998             : {
    1999             :   long i, l = lg(e), done = 1;
    2000             :   GEN id = C;
    2001             :   for (i=1; i<l; i++)
    2002             :   {
    2003             :     GEN ei = gel(e,i);
    2004             :     if (signe(ei))
    2005             :     {
    2006             :       if (mod2(ei)) id = id? idealmul(nf, id, gel(P,i)): gel(P,i);
    2007             :       ei = shifti(ei,-1);
    2008             :       if (signe(ei)) done = 0;
    2009             :       gel(e,i) = ei;
    2010             :     }
    2011             :   }
    2012             :   if (id != C) id = idealred(nf, id);
    2013             :   if (done) return id;
    2014             :   return idealmulred(nf, id, idealsqr(nf, expand(nf,id,P,e)));
    2015             : }
    2016             : /* C is an extended ideal, possibly with C[1] = NULL */
    2017             : static GEN
    2018             : expandext(GEN nf, GEN C, GEN P, GEN e)
    2019             : {
    2020             :   long i, l = lg(e), done = 1;
    2021             :   GEN A = gel(C,1);
    2022             :   for (i=1; i<l; i++)
    2023             :   {
    2024             :     GEN ei = gel(e,i);
    2025             :     if (signe(ei))
    2026             :     {
    2027             :       if (mod2(ei)) A = A? idealmul(nf, A, gel(P,i)): gel(P,i);
    2028             :       ei = shifti(ei,-1);
    2029             :       if (signe(ei)) done = 0;
    2030             :       gel(e,i) = ei;
    2031             :     }
    2032             :   }
    2033             :   if (A == gel(C,1))
    2034             :     A = C;
    2035             :   else
    2036             :     A = idealred(nf, mkvec2(A, gel(C,2)));
    2037             :   if (done) return A;
    2038             :   return idealmulred(nf, A, idealsqr(nf, expand(nf,A,P,e)));
    2039             : }
    2040             : #endif
    2041             : 
    2042             : static GEN
    2043           0 : expand(GEN nf, GEN C, GEN P, GEN e)
    2044             : {
    2045           0 :   long i, l = lg(e);
    2046           0 :   GEN B, A = C;
    2047           0 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    2048           0 :     if (signe(gel(e,i)))
    2049             :     {
    2050           0 :       B = idealpowred(nf, gel(P,i), gel(e,i));
    2051           0 :       A = A? idealmulred(nf,A,B): B;
    2052             :     }
    2053           0 :   return A;
    2054             : }
    2055             : static GEN
    2056       31880 : expandext(GEN nf, GEN C, GEN P, GEN e)
    2057             : {
    2058       31880 :   long i, l = lg(e);
    2059       31880 :   GEN B, A = gel(C,1), C1 = A;
    2060       94011 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    2061       62131 :     if (signe(gel(e,i)))
    2062             :     {
    2063       34426 :       gel(C,1) = gel(P,i);
    2064       34426 :       B = idealpowred(nf, C, gel(e,i));
    2065       34426 :       A = A? idealmulred(nf,A,B): B;
    2066             :     }
    2067       31880 :   return A == C1? C: A;
    2068             : }
    2069             : 
    2070             : /* isprincipal for C * \prod P[i]^e[i] (C omitted if NULL) */
    2071             : GEN
    2072       31880 : isprincipalfact(GEN bnf, GEN C, GEN P, GEN e, long flag)
    2073             : {
    2074       31880 :   const long gen = flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL);
    2075             :   long prec;
    2076       31880 :   pari_sp av = avma;
    2077       31880 :   GEN C0, Cext, c, id, nf = bnf_get_nf(bnf);
    2078             : 
    2079       31880 :   if (gen)
    2080             :   {
    2081       14254 :     Cext = (flag & nf_GENMAT)? trivial_fact()
    2082       31880 :                              : mkpolmod(gen_1,nf_get_pol(nf));
    2083       31880 :     C0 = mkvec2(C, Cext);
    2084       31880 :     id = expandext(nf, C0, P, e);
    2085             :   } else {
    2086           0 :     Cext = NULL;
    2087           0 :     C0 = C;
    2088           0 :     id = expand(nf, C, P, e);
    2089             :   }
    2090       31880 :   if (id == C0) /* e = 0 */
    2091             :   {
    2092       12470 :     if (!C) return bnfisprincipal0(bnf, gen_1, flag);
    2093       12456 :     switch(typ(C))
    2094             :     {
    2095           7 :       case t_INT: case t_FRAC: case t_POL: case t_POLMOD: case t_COL:
    2096           7 :         return triv_gen(bnf, C, flag);
    2097             :     }
    2098       12449 :     C = idealhnf_shallow(nf,C);
    2099             :   }
    2100             :   else
    2101             :   {
    2102       19410 :     if (gen) { C = gel(id,1); Cext = gel(id,2); } else C = id;
    2103             :   }
    2104       31859 :   prec = prec_arch(bnf);
    2105       31859 :   c = getrand();
    2106             :   for (;;)
    2107          70 :   {
    2108       31929 :     pari_sp av1 = avma;
    2109       31929 :     GEN y = isprincipalall(bnf, C, &prec, flag);
    2110       31928 :     if (y)
    2111             :     {
    2112       31858 :       if (flag & nf_GEN_IF_PRINCIPAL)
    2113             :       {
    2114       20544 :         if (typ(y) == t_INT) return gc_NULL(av);
    2115       20544 :         y = add_principal_part(nf, y, Cext, flag);
    2116             :       }
    2117             :       else
    2118             :       {
    2119       11314 :         GEN u = gel(y,2);
    2120       11314 :         if (!gen || typ(y) != t_VEC) return gerepileupto(av,y);
    2121       11314 :         if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    2122             :       }
    2123       31855 :       return gerepilecopy(av, y);
    2124             :     }
    2125          70 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",prec);
    2126          70 :     set_avma(av1); bnf = bnfnewprec_shallow(bnf,prec); setrand(c);
    2127             :   }
    2128             : }
    2129             : GEN
    2130           0 : isprincipalfact_or_fail(GEN bnf, GEN C, GEN P, GEN e)
    2131             : {
    2132           0 :   const long flag = nf_GENMAT|nf_FORCE;
    2133             :   long prec;
    2134           0 :   pari_sp av = avma;
    2135           0 :   GEN u, y, id, C0, Cext, nf = bnf_get_nf(bnf);
    2136             : 
    2137           0 :   Cext = trivial_fact();
    2138           0 :   C0 = mkvec2(C, Cext);
    2139           0 :   id = expandext(nf, C0, P, e);
    2140           0 :   if (id == C0) /* e = 0 */
    2141           0 :     C = idealhnf_shallow(nf,C);
    2142             :   else {
    2143           0 :     C = gel(id,1); Cext = gel(id,2);
    2144             :   }
    2145           0 :   prec = prec_arch(bnf);
    2146           0 :   y = isprincipalall(bnf, C, &prec, flag);
    2147           0 :   if (!y) return gc_utoipos(av, prec);
    2148           0 :   u = gel(y,2);
    2149           0 :   if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    2150           0 :   return gerepilecopy(av, y);
    2151             : }
    2152             : 
    2153             : GEN
    2154      148711 : nfsign_from_logarch(GEN LA, GEN invpi, GEN archp)
    2155             : {
    2156      148711 :   long l = lg(archp), i;
    2157      148711 :   GEN y = cgetg(l, t_VECSMALL);
    2158      148714 :   pari_sp av = avma;
    2159             : 
    2160      279383 :   for (i=1; i<l; i++)
    2161             :   {
    2162      130665 :     GEN c = ground( gmul(imag_i(gel(LA,archp[i])), invpi) );
    2163      130666 :     y[i] = mpodd(c)? 1: 0;
    2164             :   }
    2165      148718 :   set_avma(av); return y;
    2166             : }
    2167             : 
    2168             : GEN
    2169      227001 : nfsign_tu(GEN bnf, GEN archp)
    2170             : {
    2171             :   long n;
    2172      227001 :   if (bnf_get_tuN(bnf) != 2) return cgetg(1, t_VECSMALL);
    2173      159906 :   n = archp? lg(archp) - 1: nf_get_r1(bnf_get_nf(bnf));
    2174      159906 :   return const_vecsmall(n, 1);
    2175             : }
    2176             : GEN
    2177      228257 : nfsign_fu(GEN bnf, GEN archp)
    2178             : {
    2179      228257 :   GEN invpi, y, A = bnf_get_logfu(bnf), nf = bnf_get_nf(bnf);
    2180      228256 :   long j = 1, RU = lg(A);
    2181             : 
    2182      228256 :   if (!archp) archp = identity_perm( nf_get_r1(nf) );
    2183      228256 :   invpi = invr( mppi(nf_get_prec(nf)) );
    2184      228224 :   y = cgetg(RU,t_MAT);
    2185      376847 :   for (j = 1; j < RU; j++)
    2186      148613 :     gel(y,j) = nfsign_from_logarch(gel(A,j), invpi, archp);
    2187      228234 :   return y;
    2188             : }
    2189             : GEN
    2190          35 : nfsign_units(GEN bnf, GEN archp, int add_zu)
    2191             : {
    2192          35 :   GEN sfu = nfsign_fu(bnf, archp);
    2193          35 :   return add_zu? vec_prepend(sfu, nfsign_tu(bnf, archp)): sfu;
    2194             : }
    2195             : 
    2196             : /* obsolete */
    2197             : GEN
    2198           7 : signunits(GEN bnf)
    2199             : {
    2200             :   pari_sp av;
    2201             :   GEN S, y, nf;
    2202             :   long i, j, r1, r2;
    2203             : 
    2204           7 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf);
    2205           7 :   nf_get_sign(nf, &r1,&r2);
    2206           7 :   S = zeromatcopy(r1, r1+r2-1); av = avma;
    2207           7 :   y = nfsign_fu(bnf, NULL);
    2208          14 :   for (j = 1; j < lg(y); j++)
    2209             :   {
    2210           7 :     GEN Sj = gel(S,j), yj = gel(y,j);
    2211          21 :     for (i = 1; i <= r1; i++) gel(Sj,i) = yj[i]? gen_m1: gen_1;
    2212             :   }
    2213           7 :   set_avma(av); return S;
    2214             : }
    2215             : 
    2216             : static GEN
    2217      729797 : get_log_embed(REL_t *rel, GEN M, long RU, long R1, long prec)
    2218             : {
    2219      729797 :   GEN arch, C, z = rel->m;
    2220             :   long i;
    2221      729797 :   arch = typ(z) == t_COL? RgM_RgC_mul(M, z): const_col(nbrows(M), z);
    2222      729809 :   C = cgetg(RU+1, t_COL); arch = glog(arch, prec);
    2223     1679535 :   for (i=1; i<=R1; i++) gel(C,i) = gel(arch,i);
    2224     1565689 :   for (   ; i<=RU; i++) gel(C,i) = gmul2n(gel(arch,i), 1);
    2225      729782 :   return C;
    2226             : }
    2227             : static GEN
    2228     1023106 : rel_embed(REL_t *rel, FB_t *F, GEN embs, long ind, GEN M, long RU, long R1,
    2229             :           long prec)
    2230             : {
    2231             :   GEN C, D, perm;
    2232             :   long i, n;
    2233     1023106 :   if (!rel->relaut) return get_log_embed(rel, M, RU, R1, prec);
    2234             :   /* image of another relation by automorphism */
    2235      293309 :   C = gel(embs, ind - rel->relorig);
    2236      293309 :   perm = gel(F->embperm, rel->relaut);
    2237      293309 :   D = cgetg_copy(C, &n);
    2238     1245913 :   for (i = 1; i < n; i++)
    2239             :   {
    2240      952587 :     long v = perm[i];
    2241      952587 :     gel(D,i) = (v > 0)? gel(C,v): conj_i(gel(C,-v));
    2242             :   }
    2243      293326 :   return D;
    2244             : }
    2245             : static GEN
    2246      106757 : get_embs(FB_t *F, RELCACHE_t *cache, GEN nf, GEN embs, long PREC)
    2247             : {
    2248      106757 :   long ru, j, k, l = cache->last - cache->chk + 1, r1 = nf_get_r1(nf);
    2249      106756 :   GEN M = nf_get_M(nf), nembs = cgetg(cache->last - cache->base+1, t_MAT);
    2250             :   REL_t *rel;
    2251             : 
    2252     3965315 :   for (k = 1; k <= cache->chk - cache->base; k++) gel(nembs,k) = gel(embs,k);
    2253      106756 :   embs = nembs; ru = nbrows(M);
    2254     1118240 :   for (j=1,rel = cache->chk + 1; j < l; rel++,j++,k++)
    2255     1011490 :     gel(embs,k) = rel_embed(rel, F, embs, k, M, ru, r1, PREC);
    2256      106750 :   return embs;
    2257             : }
    2258             : static void
    2259      936609 : set_rel_alpha(REL_t *rel, GEN auts, GEN vA, long ind)
    2260             : {
    2261             :   GEN u;
    2262      936609 :   if (!rel->relaut)
    2263      672426 :     u = rel->m;
    2264             :   else
    2265      264183 :     u = ZM_ZC_mul(gel(auts, rel->relaut), gel(vA, ind - rel->relorig));
    2266      936614 :   gel(vA, ind) = u;
    2267      936614 : }
    2268             : static GEN
    2269     2290457 : set_fact(FB_t *F, FACT *fact, GEN e, long *pnz)
    2270             : {
    2271     2290457 :   long n = fact[0].pr;
    2272     2290457 :   GEN c = zero_Flv(F->KC);
    2273     2290661 :   if (!n) /* trivial factorization */
    2274          74 :     *pnz = F->KC+1;
    2275             :   else
    2276             :   {
    2277     2290587 :     long i, nz = minss(fact[1].pr, fact[n].pr);
    2278    10680945 :     for (i = 1; i <= n; i++) c[fact[i].pr] = fact[i].ex;
    2279     2290587 :     if (e)
    2280             :     {
    2281       26574 :       long l = lg(e);
    2282       96277 :       for (i = 1; i < l; i++)
    2283       69703 :         if (e[i]) { long v = F->subFB[i]; c[v] += e[i]; if (v < nz) nz = v; }
    2284             :     }
    2285     2290587 :     *pnz = nz;
    2286             :   }
    2287     2290661 :   return c;
    2288             : }
    2289             : 
    2290             : /* Is cols already in the cache ? bs = index of first non zero coeff in cols
    2291             :  * General check for colinearity useless since exceedingly rare */
    2292             : static int
    2293     2966309 : already_known(RELCACHE_t *cache, long bs, GEN cols)
    2294             : {
    2295             :   REL_t *r;
    2296     2966309 :   long l = lg(cols);
    2297   222253074 :   for (r = cache->last; r > cache->base; r--)
    2298   219848798 :     if (bs == r->nz)
    2299             :     {
    2300    39718236 :       GEN coll = r->R;
    2301    39718236 :       long b = bs;
    2302   126540964 :       while (b < l && cols[b] == coll[b]) b++;
    2303    39718236 :       if (b == l) return 1;
    2304             :     }
    2305     2404276 :   return 0;
    2306             : }
    2307             : 
    2308             : /* Add relation R to cache, nz = index of first non zero coeff in R.
    2309             :  * If relation is a linear combination of the previous ones, return 0.
    2310             :  * Otherwise, update basis and return > 0. Compute mod p (much faster)
    2311             :  * so some kernel vector might not be genuine. */
    2312             : static int
    2313     2970380 : add_rel_i(RELCACHE_t *cache, GEN R, long nz, GEN m, long orig, long aut, REL_t **relp, long in_rnd_rel)
    2314             : {
    2315     2970380 :   long i, k, n = lg(R)-1;
    2316             : 
    2317     2970380 :   if (nz == n+1) { k = 0; goto ADD_REL; }
    2318     2966302 :   if (already_known(cache, nz, R)) return -1;
    2319     2404304 :   if (cache->last >= cache->base + cache->len) return 0;
    2320     2404304 :   if (DEBUGLEVEL>6)
    2321             :   {
    2322           0 :     err_printf("adding vector = %Ps\n",R);
    2323           0 :     err_printf("generators =\n%Ps\n", cache->basis);
    2324             :   }
    2325     2404323 :   if (cache->missing)
    2326             :   {
    2327     1998773 :     GEN a = leafcopy(R), basis = cache->basis;
    2328     1998770 :     k = lg(a);
    2329   123143679 :     do --k; while (!a[k]);
    2330     7430062 :     while (k)
    2331             :     {
    2332     5895000 :       GEN c = gel(basis, k);
    2333     5895000 :       if (c[k])
    2334             :       {
    2335     5431292 :         long ak = a[k];
    2336   263336315 :         for (i=1; i < k; i++) if (c[i]) a[i] = (a[i] + ak*(mod_p-c[i])) % mod_p;
    2337     5431292 :         a[k] = 0;
    2338   128866464 :         do --k; while (!a[k]); /* k cannot go below 0: codeword is a sentinel */
    2339             :       }
    2340             :       else
    2341             :       {
    2342      463708 :         ulong invak = Fl_inv(uel(a,k), mod_p);
    2343             :         /* Cleanup a */
    2344    13681320 :         for (i = k; i-- > 1; )
    2345             :         {
    2346    13217612 :           long j, ai = a[i];
    2347    13217612 :           c = gel(basis, i);
    2348    13217612 :           if (!ai || !c[i]) continue;
    2349      261538 :           ai = mod_p-ai;
    2350     4466899 :           for (j = 1; j < i; j++) if (c[j]) a[j] = (a[j] + ai*c[j]) % mod_p;
    2351      261538 :           a[i] = 0;
    2352             :         }
    2353             :         /* Insert a/a[k] as k-th column */
    2354      463708 :         c = gel(basis, k);
    2355    13681318 :         for (i = 1; i<k; i++) if (a[i]) c[i] = (a[i] * invak) % mod_p;
    2356      463708 :         c[k] = 1; a = c;
    2357             :         /* Cleanup above k */
    2358    13495878 :         for (i = k+1; i<n; i++)
    2359             :         {
    2360             :           long j, ck;
    2361    13032170 :           c = gel(basis, i);
    2362    13032170 :           ck = c[k];
    2363    13032170 :           if (!ck) continue;
    2364     2706388 :           ck = mod_p-ck;
    2365    98786051 :           for (j = 1; j < k; j++) if (a[j]) c[j] = (c[j] + ck*a[j]) % mod_p;
    2366     2706388 :           c[k] = 0;
    2367             :         }
    2368      463708 :         cache->missing--;
    2369      463708 :         break;
    2370             :       }
    2371             :     }
    2372             :   }
    2373             :   else
    2374      405550 :     k = (cache->last - cache->base) + 1;
    2375     2404320 :   if (k || cache->relsup > 0 || (m && in_rnd_rel))
    2376             :   {
    2377             :     REL_t *rel;
    2378             : 
    2379      996256 : ADD_REL:
    2380     1000334 :     rel = ++cache->last;
    2381     1000334 :     if (!k && cache->relsup && nz < n+1)
    2382             :     {
    2383      126772 :       cache->relsup--;
    2384      126772 :       k = (rel - cache->base) + cache->missing;
    2385             :     }
    2386     1000334 :     rel->R  = gclone(R);
    2387     1000328 :     rel->m  =  m ? gclone(m) : NULL;
    2388     1000332 :     rel->nz = nz;
    2389     1000332 :     if (aut)
    2390             :     {
    2391      290731 :       rel->relorig = (rel - cache->base) - orig;
    2392      290731 :       rel->relaut = aut;
    2393             :     }
    2394             :     else
    2395      709601 :       rel->relaut = 0;
    2396     1000332 :     if (relp) *relp = rel;
    2397     1000332 :     if (DEBUGLEVEL) dbg_newrel(cache);
    2398             :   }
    2399     2408389 :   return k;
    2400             : }
    2401             : 
    2402             : static int
    2403     2461466 : add_rel(RELCACHE_t *cache, FB_t *F, GEN R, long nz, GEN m, long in_rnd_rel)
    2404             : {
    2405             :   REL_t *rel;
    2406             :   long k, l, reln;
    2407     2461466 :   const long lauts = lg(F->idealperm), KC = F->KC;
    2408             : 
    2409     2461466 :   k = add_rel_i(cache, R, nz, m, 0, 0, &rel, in_rnd_rel);
    2410     2461519 :   if (k > 0 && typ(m) != t_INT)
    2411             :   {
    2412      538495 :     GEN Rl = cgetg(KC+1, t_VECSMALL);
    2413      538494 :     reln = rel - cache->base;
    2414     1047394 :     for (l = 1; l < lauts; l++)
    2415             :     {
    2416      508901 :       GEN perml = gel(F->idealperm, l);
    2417      508901 :       long i, nzl = perml[nz];
    2418             : 
    2419    20492968 :       for (i = 1; i <= KC; i++) Rl[i] = 0;
    2420    18277528 :       for (i = nz; i <= KC; i++)
    2421    17768627 :         if (R[i])
    2422             :         {
    2423     1423809 :           long v = perml[i];
    2424             : 
    2425     1423809 :           if (v < nzl) nzl = v;
    2426     1423809 :           Rl[v] = R[i];
    2427             :         }
    2428      508901 :       (void)add_rel_i(cache, Rl, nzl, NULL, reln, l, NULL, in_rnd_rel);
    2429             :     }
    2430             :   }
    2431     2461517 :   return k;
    2432             : }
    2433             : 
    2434             : INLINE void
    2435    30240696 : step(GEN x, double *y, GEN inc, long k)
    2436             : {
    2437    30240696 :   if (!y[k])
    2438     2109559 :     x[k]++; /* leading coeff > 0 */
    2439             :   else
    2440             :   {
    2441    28131137 :     long i = inc[k];
    2442    28131137 :     x[k] += i;
    2443    28131137 :     inc[k] = (i > 0)? -1-i: 1-i;
    2444             :   }
    2445    30240696 : }
    2446             : 
    2447             : static double
    2448      211359 : Fincke_Pohst_bound(double T, GEN r)
    2449             : {
    2450      211359 :   pari_sp av = avma;
    2451      211359 :   GEN zT = dbltor(T * T), p = gmael(r,1,1), B = real_1(DEFAULTPREC);
    2452      211357 :   long i, n = lg(r)-1;
    2453             :   double g;
    2454      575633 :   for (i = 2; i <= n; i++)
    2455             :   {
    2456      575625 :     p = gmul(p, gmael(r,i,i));
    2457      575624 :     B = sqrtnr(gmul(zT,p), i);
    2458      575617 :     if (i == n || cmprr(B, gmael(r,i+1,i+1)) < 0) break;
    2459             :   }
    2460      211353 :   if (!gisdouble(B,&g)) return gc_double(av, 0.);
    2461      211351 :   return gc_double(av, rtodbl(B));
    2462             : }
    2463             : 
    2464             : INLINE long
    2465      211360 : Fincke_Pohst_ideal(RELCACHE_t *cache, FB_t *F, GEN nf, GEN M, GEN I,
    2466             :     GEN NI, FACT *fact, long Nrelid, FP_t *fp, RNDREL_t *rr, long prec,
    2467             :     long *Nsmall, long *Nfact)
    2468             : {
    2469             :   pari_sp av;
    2470      211360 :   const long N = nf_get_degree(nf), R1 = nf_get_r1(nf);
    2471      211359 :   GEN G = nf_get_G(nf), G0 = nf_get_roundG(nf), r, u, gx, inc, ideal;
    2472             :   double BOUND, B1, B2;
    2473      211359 :   long j, k, skipfirst, relid=0, try_factor=0;
    2474             : 
    2475      211359 :   inc = const_vecsmall(N, 1);
    2476      211359 :   u = ZM_lll(ZM_mul(G0, I), 0.99, LLL_IM);
    2477      211358 :   ideal = ZM_mul(I,u); /* approximate T2-LLL reduction */
    2478      211350 :   r = gaussred_from_QR(RgM_mul(G, ideal), prec); /* Cholesky for T2 | ideal */
    2479      211360 :   if (!r) pari_err_BUG("small_norm (precision too low)");
    2480             : 
    2481     1037132 :   for (k=1; k<=N; k++)
    2482             :   {
    2483      825772 :     if (!gisdouble(gcoeff(r,k,k),&(fp->v[k]))) return 0;
    2484     2625632 :     for (j=1; j<k; j++) if (!gisdouble(gcoeff(r,j,k),&(fp->q[j][k]))) return 0;
    2485      825772 :     if (DEBUGLEVEL>3) err_printf("v[%ld]=%.4g ",k,fp->v[k]);
    2486             :   }
    2487      211360 :   B1 = fp->v[1]; /* T2(ideal[1]) */
    2488      211360 :   B2 = fp->v[2] + B1 * fp->q[1][2] * fp->q[1][2]; /* T2(ideal[2]) */
    2489      211360 :   skipfirst = ZV_isscalar(gel(ideal,1));
    2490      211359 :   BOUND = maxdd(2*B2, Fincke_Pohst_bound(4 * maxtry_FACT / F->ballvol, r));
    2491      211351 :   if (DEBUGLEVEL>1)
    2492             :   {
    2493           0 :     if (DEBUGLEVEL>3) err_printf("\n");
    2494           0 :     err_printf("BOUND = %.4g\n",BOUND);
    2495             :   }
    2496             : 
    2497      211351 :   k = N; fp->y[N] = fp->z[N] = 0; fp->x[N] = 0;
    2498    20572481 :   for (av = avma;; set_avma(av), step(fp->x,fp->y,inc,k))
    2499    20358403 :   {
    2500             :     GEN R;
    2501             :     long nz;
    2502             :     do
    2503             :     { /* look for primitive element of small norm, cf minim00 */
    2504    25457966 :       int fl = 0;
    2505             :       double p;
    2506    25457966 :       if (k > 1)
    2507             :       {
    2508     5100033 :         long l = k-1;
    2509     5100033 :         fp->z[l] = 0;
    2510    45456830 :         for (j=k; j<=N; j++) fp->z[l] += fp->q[l][j]*fp->x[j];
    2511     5100033 :         p = (double)fp->x[k] + fp->z[k];
    2512     5100033 :         fp->y[l] = fp->y[k] + p*p*fp->v[k];
    2513     5100033 :         if (l <= skipfirst && !fp->y[1]) fl = 1;
    2514     5100033 :         fp->x[l] = (long)floor(-fp->z[l] + 0.5);
    2515     5100033 :         k = l;
    2516             :       }
    2517     4490864 :       for(;; step(fp->x,fp->y,inc,k))
    2518             :       {
    2519    29948766 :         if (!fl)
    2520             :         {
    2521    29892070 :           p = (double)fp->x[k] + fp->z[k];
    2522    29892070 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2523             : 
    2524     5391206 :           step(fp->x,fp->y,inc,k);
    2525             : 
    2526     5392072 :           p = (double)fp->x[k] + fp->z[k];
    2527     5392072 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2528             :         }
    2529     4493251 :         fl = 0; inc[k] = 1;
    2530     4493251 :         if (++k > N) goto END_Fincke_Pohst_ideal;
    2531             :       }
    2532    25456381 :     } while (k > 1);
    2533             : 
    2534             :     /* element complete */
    2535    37151314 :     if (zv_content(fp->x) !=1) continue; /* not primitive */
    2536    17146409 :     gx = ZM_zc_mul(ideal,fp->x);
    2537    17145405 :     if (ZV_isscalar(gx)) continue;
    2538    17196868 :     if (++try_factor > maxtry_FACT) break;
    2539             : 
    2540    17093309 :     if (!Nrelid)
    2541             :     {
    2542         259 :       if (!factorgen(F,nf,I,NI,gx,fact)) continue;
    2543      105412 :       return 1;
    2544             :     }
    2545    17093050 :     else if (rr)
    2546             :     {
    2547      232573 :       if (!factorgen(F,nf,I,NI,gx,fact)) continue;
    2548       26574 :       add_to_fact(rr->jid, 1, fact);
    2549             :     }
    2550             :     else
    2551             :     {
    2552    16860477 :       GEN Nx, xembed = RgM_RgC_mul(M, gx);
    2553             :       long e;
    2554    16862286 :       if (Nsmall) (*Nsmall)++;
    2555    16862286 :       Nx = grndtoi(embed_norm(xembed, R1), &e);
    2556    16861468 :       if (e >= 0) {
    2557           0 :         if (DEBUGLEVEL > 1) err_printf("+");
    2558    14603109 :         continue;
    2559             :       }
    2560    16861468 :       if (!can_factor(F, nf, NULL, gx, Nx, fact)) continue;
    2561             :     }
    2562             : 
    2563             :     /* smooth element */
    2564     2281869 :     R = set_fact(F, fact, rr ? rr->ex : NULL, &nz);
    2565             :     /* make sure we get maximal rank first, then allow all relations */
    2566     2282072 :     if (add_rel(cache, F, R, nz, gx, rr ? 1 : 0) <= 0)
    2567             :     { /* probably Q-dependent from previous ones: forget it */
    2568     1743883 :       if (DEBUGLEVEL>1) err_printf("*");
    2569     1743883 :       if (DEBUGLEVEL && Nfact && rr) (*Nfact)++;
    2570     1743883 :       continue;
    2571             :     }
    2572      538256 :     if (DEBUGLEVEL && Nfact) (*Nfact)++;
    2573      538256 :     if (cache->last >= cache->end) return 1; /* we have enough */
    2574      432858 :     if (++relid == Nrelid) break;
    2575             :   }
    2576      105946 :   END_Fincke_Pohst_ideal:
    2577      105946 :   return 0;
    2578             : }
    2579             : 
    2580             : static void
    2581       89752 : small_norm(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, GEN M,
    2582             :            FACT *fact, GEN p0)
    2583             : {
    2584       89752 :   const long prec = nf_get_prec(nf);
    2585             :   FP_t fp;
    2586             :   pari_sp av;
    2587       89752 :   GEN L_jid = F->L_jid, Np0 = NULL;
    2588       89752 :   long Nsmall, Nfact, n = lg(L_jid);
    2589             :   pari_timer T;
    2590             : 
    2591       89752 :   if (DEBUGLEVEL)
    2592             :   {
    2593           0 :     timer_start(&T);
    2594           0 :     err_printf("#### Look for %ld relations in %ld ideals (small_norm)\n",
    2595           0 :                cache->end - cache->last, lg(L_jid)-1);
    2596           0 :     if (p0) err_printf("Look in p0 = %Ps\n", vecslice(p0,1,4));
    2597             :   }
    2598       89752 :   Nsmall = Nfact = 0;
    2599       89752 :   minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2600       89752 :   if (p0)
    2601             :   {
    2602       27013 :     GEN n = pr_norm(p0);
    2603       27013 :     ulong e = maxuu(1,logint0(sqri(pr_norm(veclast(F->LP))), n, NULL));
    2604       27013 :     p0 = idealpow(nf, p0, utoi(e));
    2605       27013 :     Np0 = powiu(n,e);
    2606             :   }
    2607      190161 :   for (av = avma; --n; set_avma(av))
    2608             :   {
    2609      189672 :     long j = L_jid[n];
    2610      189672 :     GEN id = gel(F->LP, j), Nid;
    2611      189672 :     if (DEBUGLEVEL>1)
    2612           0 :       err_printf("\n*** Ideal no %ld: %Ps\n", j, vecslice(id,1,4));
    2613      189672 :     if (p0)
    2614       32172 :     { Nid = mulii(Np0, pr_norm(id)); id = idealmul(nf, p0, id); }
    2615             :     else
    2616      157500 :     { Nid = pr_norm(id); id = pr_hnf(nf, id);}
    2617      189671 :     if (Fincke_Pohst_ideal(cache, F, nf, M, id, Nid, fact, Nrelid, &fp,
    2618       89258 :                            NULL, prec, &Nsmall, &Nfact)) break;
    2619             :   }
    2620       89749 :   if (DEBUGLEVEL && Nsmall)
    2621             :   {
    2622           0 :     if (DEBUGLEVEL == 1)
    2623           0 :     { if (Nfact) err_printf("\n"); }
    2624             :     else
    2625           0 :       err_printf("  \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
    2626           0 :                   Nfact,Nsmall,((double)Nfact)/Nsmall);
    2627           0 :     if (timer_get(&T)>1) timer_printf(&T,"small_norm");
    2628             :   }
    2629       89749 : }
    2630             : 
    2631             : static GEN
    2632       16994 : get_random_ideal(FB_t *F, GEN nf, GEN ex)
    2633             : {
    2634       16994 :   long i, l = lg(ex);
    2635             :   for (;;)
    2636         652 :   {
    2637       17646 :     GEN I = NULL;
    2638       67063 :     for (i = 1; i < l; i++)
    2639       49417 :       if ((ex[i] = random_bits(RANDOM_BITS)))
    2640             :       {
    2641       46270 :         GEN pr = gel(F->LP, F->subFB[i]), e = utoipos(ex[i]);
    2642       46270 :         I = I? idealmulpowprime(nf, I, pr, e): idealpow(nf, pr, e);
    2643             :       }
    2644       17646 :     if (I && !ZM_isscalar(I,NULL)) return I; /* != (n)Z_K */
    2645             :   }
    2646             : }
    2647             : 
    2648             : static void
    2649       16994 : rnd_rel(RELCACHE_t *cache, FB_t *F, GEN nf, FACT *fact)
    2650             : {
    2651             :   pari_timer T;
    2652       16994 :   GEN L_jid = F->L_jid, M = nf_get_M(nf), R, NR;
    2653       16994 :   long i, l = lg(L_jid), prec = nf_get_prec(nf), Nfact = 0;
    2654             :   RNDREL_t rr;
    2655             :   FP_t fp;
    2656             :   pari_sp av;
    2657             : 
    2658       16994 :   if (DEBUGLEVEL) {
    2659           0 :     timer_start(&T);
    2660           0 :     err_printf("#### Look for %ld relations in %ld ideals (rnd_rel)\n",
    2661           0 :                cache->end - cache->last, l-1);
    2662             :   }
    2663       16994 :   rr.ex = cgetg(lg(F->subFB), t_VECSMALL);
    2664       16994 :   R = get_random_ideal(F, nf, rr.ex); /* random product from subFB */
    2665       16994 :   NR = ZM_det_triangular(R);
    2666       16994 :   minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2667       22529 :   for (av = avma, i = 1; i < l; i++, set_avma(av))
    2668             :   { /* try P[j] * base */
    2669       21675 :     long j = L_jid[i];
    2670       21675 :     GEN P = gel(F->LP, j), Nid = mulii(NR, pr_norm(P));
    2671       21675 :     if (DEBUGLEVEL>1) err_printf("\n*** Ideal %ld: %Ps\n", j, vecslice(P,1,4));
    2672       21675 :     rr.jid = j;
    2673       21675 :     if (Fincke_Pohst_ideal(cache, F, nf, M, idealHNF_mul(nf, R, P), Nid, fact,
    2674       16140 :                            RND_REL_RELPID, &fp, &rr, prec, NULL, &Nfact)) break;
    2675             :   }
    2676       16994 :   if (DEBUGLEVEL)
    2677             :   {
    2678           0 :     if (Nfact) err_printf("\n");
    2679           0 :     if (timer_get(&T)>=0) timer_printf(&T,"rnd_rel");
    2680             :   }
    2681       16994 : }
    2682             : 
    2683             : static GEN
    2684       63623 : automorphism_perms(GEN M, GEN auts, GEN cyclic, long r1, long r2, long N)
    2685             : {
    2686       63623 :   long L = lgcols(M), lauts = lg(auts), lcyc = lg(cyclic), i, j, l, m;
    2687       63623 :   GEN Mt, perms = cgetg(lauts, t_VEC);
    2688             :   pari_sp av;
    2689             : 
    2690      127470 :   for (l = 1; l < lauts; l++) gel(perms, l) = cgetg(L, t_VECSMALL);
    2691       63623 :   av = avma;
    2692       63623 :   Mt = shallowtrans(gprec_w(M, LOWDEFAULTPREC));
    2693       63622 :   Mt = shallowconcat(Mt, conj_i(vecslice(Mt, r1+1, r1+r2)));
    2694      110235 :   for (l = 1; l < lcyc; l++)
    2695             :   {
    2696       46612 :     GEN thiscyc = gel(cyclic, l), thisperm, perm, prev, Nt;
    2697       46612 :     long k = thiscyc[1];
    2698             : 
    2699       46612 :     Nt = RgM_mul(shallowtrans(gel(auts, k)), Mt);
    2700       46614 :     perm = gel(perms, k);
    2701      153216 :     for (i = 1; i < L; i++)
    2702             :     {
    2703      106603 :       GEN v = gel(Nt, i), minD;
    2704      106603 :       minD = gnorml2(gsub(v, gel(Mt, 1)));
    2705      106601 :       perm[i] = 1;
    2706      563245 :       for (j = 2; j <= N; j++)
    2707             :       {
    2708      456643 :         GEN D = gnorml2(gsub(v, gel(Mt, j)));
    2709      456641 :         if (gcmp(D, minD) < 0) { minD = D; perm[i] = j >= L ? r2-j : j; }
    2710             :       }
    2711             :     }
    2712       65058 :     for (prev = perm, m = 2; m < lg(thiscyc); m++, prev = thisperm)
    2713             :     {
    2714       18445 :       thisperm = gel(perms, thiscyc[m]);
    2715       93604 :       for (i = 1; i < L; i++)
    2716             :       {
    2717       75159 :         long pp = labs(prev[i]);
    2718       75159 :         thisperm[i] = prev[i] < 0 ? -perm[pp] : perm[pp];
    2719             :       }
    2720             :     }
    2721             :   }
    2722       63623 :   set_avma(av); return perms;
    2723             : }
    2724             : 
    2725             : /* Determine the field automorphisms as matrices on the integral basis */
    2726             : static GEN
    2727       63685 : automorphism_matrices(GEN nf, GEN *cycp)
    2728             : {
    2729       63685 :   pari_sp av = avma;
    2730       63685 :   GEN auts = galoisconj(nf, NULL), mats, cyclic, cyclicidx;
    2731       63685 :   long nauts = lg(auts)-1, i, j, k, l;
    2732             : 
    2733       63685 :   cyclic = cgetg(nauts+1, t_VEC);
    2734       63685 :   cyclicidx = zero_Flv(nauts);
    2735       97767 :   for (l = 1; l <= nauts; l++)
    2736             :   {
    2737       97767 :     GEN aut = gel(auts, l);
    2738       97767 :     if (gequalX(aut)) { swap(gel(auts, l), gel(auts, nauts)); break; }
    2739             :   }
    2740             :   /* trivial automorphism is last */
    2741      191245 :   for (l = 1; l <= nauts; l++) gel(auts, l) = algtobasis(nf, gel(auts, l));
    2742             :   /* Compute maximal cyclic subgroups */
    2743      127561 :   for (l = nauts; --l > 0; ) if (!cyclicidx[l])
    2744             :   {
    2745       48097 :     GEN elt = gel(auts, l), aut = elt, cyc = cgetg(nauts+1, t_VECSMALL);
    2746       48097 :     cyc[1] = cyclicidx[l] = l; j = 1;
    2747             :     do
    2748             :     {
    2749       67088 :       elt = galoisapply(nf, elt, aut);
    2750      217504 :       for (k = 1; k <= nauts; k++) if (gequal(elt, gel(auts, k))) break;
    2751       67088 :       cyclicidx[k] = l; cyc[++j] = k;
    2752             :     }
    2753       67088 :     while (k != nauts);
    2754       48097 :     setlg(cyc, j);
    2755       48097 :     gel(cyclic, l) = cyc;
    2756             :   }
    2757      127561 :   for (i = j = 1; i < nauts; i++)
    2758       63875 :     if (cyclicidx[i] == i) cyclic[j++] = cyclic[i];
    2759       63686 :   setlg(cyclic, j);
    2760       63686 :   mats = cgetg(nauts, t_VEC);
    2761      110327 :   while (--j > 0)
    2762             :   {
    2763       46641 :     GEN cyc = gel(cyclic, j);
    2764       46641 :     long id = cyc[1];
    2765       46641 :     GEN M, Mi, aut = gel(auts, id);
    2766             : 
    2767       46641 :     gel(mats, id) = Mi = M = nfgaloismatrix(nf, aut);
    2768       65086 :     for (i = 2; i < lg(cyc); i++) gel(mats, cyc[i]) = Mi = ZM_mul(Mi, M);
    2769             :   }
    2770       63686 :   gerepileall(av, 2, &mats, &cyclic);
    2771       63686 :   if (cycp) *cycp = cyclic;
    2772       63686 :   return mats;
    2773             : }
    2774             : 
    2775             : /* vP a list of maximal ideals above the same p from idealprimedec: f(P/p) is
    2776             :  * increasing; 1 <= j <= #vP; orbit a zc of length <= #vP; auts a vector of
    2777             :  * automorphisms in ZM form.
    2778             :  * Set orbit[i] = 1 for all vP[i], i >= j, in the orbit of pr = vP[j] wrt auts.
    2779             :  * N.B.1 orbit need not be initialized to 0: useful to incrementally run
    2780             :  * through successive orbits
    2781             :  * N.B.2 i >= j, so primes with index < j will be missed; run incrementally
    2782             :  * starting from j = 1 ! */
    2783             : static void
    2784       11865 : pr_orbit_fill(GEN orbit, GEN auts, GEN vP, long j)
    2785             : {
    2786       11865 :   GEN pr = gel(vP,j), gen = pr_get_gen(pr);
    2787       11865 :   long i, l = lg(auts), J = lg(orbit), f = pr_get_f(pr);
    2788       11865 :   orbit[j] = 1;
    2789       23730 :   for (i = 1; i < l; i++)
    2790             :   {
    2791       11865 :     GEN g = ZM_ZC_mul(gel(auts,i), gen);
    2792             :     long k;
    2793       11886 :     for (k = j+1; k < J; k++)
    2794             :     {
    2795          35 :       GEN prk = gel(vP,k);
    2796          35 :       if (pr_get_f(prk) > f) break; /* f(P[k]) increases with k */
    2797             :       /* don't check that e matches: (almost) always 1 ! */
    2798          35 :       if (!orbit[k] && ZC_prdvd(g, prk)) { orbit[k] = 1; break; }
    2799             :     }
    2800             :   }
    2801       11865 : }
    2802             : /* remark: F->KCZ changes if be_honest() fails */
    2803             : static int
    2804           7 : be_honest(FB_t *F, GEN nf, GEN auts, FACT *fact)
    2805             : {
    2806             :   long i, iz, nbtest;
    2807           7 :   long lgsub = lg(F->subFB), KCZ0 = F->KCZ;
    2808           7 :   long N = nf_get_degree(nf), prec = nf_get_prec(nf);
    2809           7 :   GEN M = nf_get_M(nf);
    2810             :   FP_t fp;
    2811             :   pari_sp av;
    2812             : 
    2813           7 :   if (DEBUGLEVEL) {
    2814           0 :     err_printf("Be honest for %ld primes from %ld to %ld\n", F->KCZ2 - F->KCZ,
    2815           0 :                F->FB[ F->KCZ+1 ], F->FB[ F->KCZ2 ]);
    2816             :   }
    2817           7 :   minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2818           7 :   if (lg(auts) == 1) auts = NULL;
    2819           7 :   av = avma;
    2820          14 :   for (iz=F->KCZ+1; iz<=F->KCZ2; iz++, set_avma(av))
    2821             :   {
    2822           7 :     long p = F->FB[iz];
    2823           7 :     GEN pr_orbit, P = gel(F->LV,p);
    2824           7 :     long j, J = lg(P); /* > 1 */
    2825             :     /* the P|p, NP > C2 are assumed in subgroup generated by FB + last P
    2826             :      * with NP <= C2 is unramified --> check all but last */
    2827           7 :     if (pr_get_e(gel(P,J-1)) == 1) J--;
    2828           7 :     if (J == 1) continue;
    2829           7 :     if (DEBUGLEVEL>1) err_printf("%ld ", p);
    2830           7 :     pr_orbit = auts? zero_zv(J-1): NULL;
    2831          28 :     for (j = 1; j < J; j++)
    2832             :     {
    2833             :       GEN Nid, id, id0;
    2834          21 :       if (pr_orbit)
    2835             :       {
    2836          21 :         if (pr_orbit[j]) continue;
    2837             :         /* discard all primes in automorphism orbit simultaneously */
    2838          14 :         pr_orbit_fill(pr_orbit, auts, P, j);
    2839             :       }
    2840          14 :       id = id0 = pr_hnf(nf,gel(P,j));
    2841          14 :       Nid = pr_norm(gel(P,j));
    2842          14 :       for (nbtest=0;;)
    2843             :       {
    2844          14 :         if (Fincke_Pohst_ideal(NULL, F, nf, M, id, Nid, fact, 0, &fp,
    2845          14 :                                NULL, prec, NULL, NULL)) break;
    2846           0 :         if (++nbtest > maxtry_HONEST)
    2847             :         {
    2848           0 :           if (DEBUGLEVEL)
    2849           0 :             pari_warn(warner,"be_honest() failure on prime %Ps\n", gel(P,j));
    2850           0 :           return 0;
    2851             :         }
    2852             :         /* occurs at most once in the whole function */
    2853           0 :         for (i = 1, id = id0; i < lgsub; i++)
    2854             :         {
    2855           0 :           long ex = random_bits(RANDOM_BITS);
    2856           0 :           if (ex)
    2857             :           {
    2858           0 :             GEN pr = gel(F->LP, F->subFB[i]);
    2859           0 :             id = idealmulpowprime(nf, id, pr, utoipos(ex));
    2860             :           }
    2861             :         }
    2862           0 :         if (!equali1(gcoeff(id,N,N))) id = Q_primpart(id);
    2863           0 :         if (expi(gcoeff(id,1,1)) > 100) id = idealred(nf, id);
    2864           0 :         Nid = ZM_det_triangular(id);
    2865             :       }
    2866             :     }
    2867           7 :     F->KCZ++; /* SUCCESS, "enlarge" factorbase */
    2868             :   }
    2869           7 :   F->KCZ = KCZ0; return gc_bool(av,1);
    2870             : }
    2871             : 
    2872             : /* all primes with N(P) <= BOUND factor on factorbase ? */
    2873             : void
    2874          63 : bnftestprimes(GEN bnf, GEN BOUND)
    2875             : {
    2876          63 :   pari_sp av0 = avma, av;
    2877          63 :   ulong count = 0;
    2878          63 :   GEN auts, p, nf = bnf_get_nf(bnf), Vbase = bnf_get_vbase(bnf);
    2879          63 :   GEN fb = gen_sort_shallow(Vbase, (void*)&cmp_prime_ideal, cmp_nodata);
    2880          63 :   ulong pmax = pr_get_smallp(veclast(fb)); /*largest p in factorbase*/
    2881             :   forprime_t S;
    2882             :   FACT *fact;
    2883             :   FB_t F;
    2884             : 
    2885          63 :   (void)recover_partFB(&F, Vbase, nf_get_degree(nf));
    2886          63 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    2887          63 :   forprime_init(&S, gen_2, BOUND);
    2888          63 :   auts = automorphism_matrices(nf, NULL);
    2889          63 :   if (lg(auts) == 1) auts = NULL;
    2890          63 :   av = avma;
    2891       37604 :   while (( p = forprime_next(&S) ))
    2892             :   {
    2893             :     GEN pr_orbit, vP;
    2894             :     long j, J;
    2895             : 
    2896       37541 :     if (DEBUGLEVEL == 1 && ++count > 1000)
    2897             :     {
    2898           0 :       err_printf("passing p = %Ps / %Ps\n", p, BOUND);
    2899           0 :       count = 0;
    2900             :     }
    2901       37541 :     set_avma(av);
    2902       37541 :     vP = idealprimedec_limit_norm(nf, p, BOUND);
    2903       37541 :     J = lg(vP);
    2904             :     /* if last is unramified, all P|p in subgroup generated by FB: skip last */
    2905       37541 :     if (J > 1 && pr_get_e(gel(vP,J-1)) == 1) J--;
    2906       37541 :     if (J == 1) continue;
    2907       14525 :     if (DEBUGLEVEL>1) err_printf("*** p = %Ps\n",p);
    2908       14525 :     pr_orbit = auts? zero_zv(J-1): NULL;
    2909       31549 :     for (j = 1; j < J; j++)
    2910             :     {
    2911       17024 :       GEN P = gel(vP,j);
    2912       17024 :       long k = 0;
    2913       17024 :       if (pr_orbit)
    2914             :       {
    2915       11858 :         if (pr_orbit[j]) continue;
    2916             :         /* discard all primes in automorphism orbit simultaneously */
    2917       11851 :         pr_orbit_fill(pr_orbit, auts, vP, j);
    2918             :       }
    2919       17017 :       if (abscmpiu(p, pmax) > 0 || !(k = tablesearch(fb, P, &cmp_prime_ideal)))
    2920       16408 :         (void)SPLIT(&F, nf, pr_hnf(nf,P), Vbase, fact);
    2921       17017 :       if (DEBUGLEVEL>1)
    2922             :       {
    2923           0 :         err_printf("  Testing P = %Ps\n",P);
    2924           0 :         if (k) err_printf("    #%ld in factor base\n",k);
    2925           0 :         else err_printf("    is %Ps\n", isprincipal(bnf,P));
    2926             :       }
    2927             :     }
    2928             :   }
    2929          63 :   set_avma(av0);
    2930          63 : }
    2931             : 
    2932             : /* A t_MAT of complex floats, in fact reals. Extract a submatrix B
    2933             :  * whose columns are definitely nonzero, i.e. gexpo(A[j]) >= -2
    2934             :  *
    2935             :  * If possible precision problem (t_REAL 0 with large exponent), set
    2936             :  * *precpb to 1 */
    2937             : static GEN
    2938       90457 : clean_cols(GEN A, int *precpb)
    2939             : {
    2940       90457 :   long l = lg(A), h, i, j, k;
    2941             :   GEN B;
    2942       90457 :   *precpb = 0;
    2943       90457 :   if (l == 1) return A;
    2944       90457 :   h = lgcols(A);;
    2945       90457 :   B = cgetg(l, t_MAT);
    2946     3680557 :   for (i = k = 1; i < l; i++)
    2947             :   {
    2948     3590100 :     GEN Ai = gel(A,i);
    2949     3590100 :     int non0 = 0;
    2950    17445979 :     for (j = 1; j < h; j++)
    2951             :     {
    2952    13855879 :       GEN c = gel(Ai,j);
    2953    13855879 :       if (gexpo(c) >= -2)
    2954             :       {
    2955    12375272 :         if (gequal0(c)) *precpb = 1; else non0 = 1;
    2956             :       }
    2957             :     }
    2958     3590100 :     if (non0) gel(B, k++) = Ai;
    2959             :   }
    2960       90457 :   setlg(B, k); return B;
    2961             : }
    2962             : 
    2963             : static long
    2964     3161979 : compute_multiple_of_R_pivot(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2965             : {
    2966     3161979 :   GEN x = gel(X,ix);
    2967     3161979 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2968             :   (void)x0;
    2969    15842872 :   for (i=1; i<lx; i++)
    2970    12680891 :     if (!c[i] && !gequal0(gel(x,i)))
    2971             :     {
    2972     3054016 :       long e = gexpo(gel(x,i));
    2973     3054018 :       if (e > ex) { ex = e; k = i; }
    2974             :     }
    2975     3161981 :   return (k && ex > -32)? k: lx;
    2976             : }
    2977             : 
    2978             : /* Ar = (log |sigma_i(u_j)|) for units (u_j) found so far;
    2979             :  * RU = R1+R2 = target rank for unit matrix, after adding [1 x r1, 2 x r2];
    2980             :  * N = field degree, need = unit rank defect;
    2981             :  * L = NULL (prec problem) or B^(-1) * A with approximate rational entries
    2982             :  * (as t_REAL), B a submatrix of A, with (probably) maximal rank RU */
    2983             : static GEN
    2984      106303 : compute_multiple_of_R(GEN Ar, long RU, long N, long *pneed, long *bit, GEN *ptL)
    2985             : {
    2986             :   GEN T, d, mdet, Im_mdet, kR, L;
    2987      106303 :   long i, j, r, R1 = 2*RU - N;
    2988             :   int precpb;
    2989      106303 :   pari_sp av = avma;
    2990             : 
    2991      106303 :   if (RU == 1) { *ptL = zeromat(0, lg(Ar)-1); return gen_1; }
    2992             : 
    2993       90457 :   if (DEBUGLEVEL) err_printf("\n#### Computing regulator multiple\n");
    2994       90457 :   mdet = clean_cols(Ar, &precpb);
    2995             :   /* will cause precision to increase on later failure, but we may succeed! */
    2996       90457 :   *ptL = precpb? NULL: gen_1;
    2997       90457 :   T = cgetg(RU+1,t_COL);
    2998      246197 :   for (i=1; i<=R1; i++) gel(T,i) = gen_1;
    2999      191478 :   for (   ; i<=RU; i++) gel(T,i) = gen_2;
    3000       90457 :   mdet = shallowconcat(T, mdet); /* det(Span(mdet)) = N * R */
    3001             : 
    3002             :   /* could be using indexrank(), but need custom "get_pivot" function */
    3003       90457 :   d = RgM_pivots(mdet, NULL, &r, &compute_multiple_of_R_pivot);
    3004             :   /* # of independent columns = target rank ? */
    3005       90457 :   if (lg(mdet)-1 - r != RU)
    3006             :   {
    3007       32636 :     if (DEBUGLEVEL)
    3008           0 :       err_printf("Units matrix target rank = %ld < %ld\n",lg(mdet)-1 - r, RU);
    3009       32636 :     *pneed = RU - (lg(mdet)-1-r); return gc_NULL(av);
    3010             :   }
    3011             : 
    3012       57821 :   Im_mdet = cgetg(RU+1, t_MAT); /* extract independent columns */
    3013             :   /* N.B: d[1] = 1, corresponding to T above */
    3014       57821 :   gel(Im_mdet, 1) = T;
    3015      248057 :   for (i = j = 2; i <= RU; j++)
    3016      190236 :     if (d[j]) gel(Im_mdet, i++) = gel(mdet,j);
    3017             : 
    3018             :   /* integral multiple of R: the cols we picked form a Q-basis, they have an
    3019             :    * index in the full lattice. First column is T */
    3020       57821 :   kR = divru(det2(Im_mdet), N);
    3021             :   /* R > 0.2 uniformly */
    3022       57820 :   if (!signe(kR) || expo(kR) < -3)
    3023             :   {
    3024           0 :     if (DEBUGLEVEL) err_printf("Regulator is zero.\n");
    3025           0 :     *pneed = 0; return gc_NULL(av);
    3026             :   }
    3027       57820 :   d = det2(rowslice(vecslice(Im_mdet, 2, RU), 2, RU));
    3028       57820 :   setabssign(d); setabssign(kR);
    3029       57820 :   if (gexpo(gsub(d,kR)) - gexpo(d) > -20) { *ptL = NULL; return gc_NULL(av); }
    3030       57813 :   L = RgM_inv(Im_mdet);
    3031             :   /* estimate # of correct bits in result */
    3032       57814 :   if (!L || (*bit = -gexpo(RgM_Rg_sub_shallow(RgM_mul(L,Im_mdet), gen_1))) < 16)
    3033          16 :   { *ptL = NULL; return gc_NULL(av); }
    3034             : 
    3035       57798 :   *ptL = RgM_mul(rowslice(L,2,RU), Ar); /* approximate rational entries */
    3036       57798 :   return gc_all(av,2, &kR, ptL);
    3037             : }
    3038             : 
    3039             : /* leave small integer n as is, convert huge n to t_REAL (for readability) */
    3040             : static GEN
    3041           0 : i2print(GEN n)
    3042           0 : { return lgefint(n) <= DEFAULTPREC? n: itor(n,LOWDEFAULTPREC); }
    3043             : 
    3044             : static long
    3045       73540 : bad_check(GEN c)
    3046             : {
    3047       73540 :   long ec = gexpo(c);
    3048       73540 :   if (DEBUGLEVEL) err_printf("\n ***** check = %.28Pg\n",c);
    3049             :   /* safe check for c < 0.75 : avoid underflow in gtodouble() */
    3050       73540 :   if (ec < -1 || (ec == -1 && gtodouble(c) < 0.75)) return fupb_PRECI;
    3051             :   /* safe check for c > 1.3 : avoid overflow */
    3052       73540 :   if (ec > 0 || (ec == 0 && gtodouble(c) > 1.3)) return fupb_RELAT;
    3053       63623 :   return fupb_NONE;
    3054             : }
    3055             : /* Input:
    3056             :  * lambda = approximate rational entries: coords of units found so far on a
    3057             :  * sublattice of maximal rank (sublambda)
    3058             :  * *ptkR = regulator of sublambda = multiple of regulator of lambda
    3059             :  * Compute R = true regulator of lambda.
    3060             :  *
    3061             :  * If c := Rz ~ 1, by Dirichlet's formula, then lambda is the full group of
    3062             :  * units AND the full set of relations for the class group has been computed.
    3063             :  * In fact z is a very rough approximation and we only expect 0.75 < Rz < 1.3
    3064             :  *
    3065             :  * Output: *ptkR = R, *ptL = numerator(units) (in terms of lambda) */
    3066             : static long
    3067       73592 : compute_R(GEN lambda, GEN z, GEN *ptL, GEN *ptkR)
    3068             : {
    3069       73592 :   pari_sp av = avma;
    3070       73592 :   long bit, r, reason, RU = lg(lambda) == 1? 1: lgcols(lambda);
    3071             :   GEN L, H, D, den, R, c;
    3072             : 
    3073       73592 :   *ptL = NULL;
    3074       73592 :   if (RU == 1) { *ptkR = gen_1; *ptL = lambda; return bad_check(z); }
    3075       57746 :   D = gmul2n(mpmul(*ptkR,z), 1); /* bound for denom(lambda) */
    3076       57746 :   if (expo(D) < 0 && rtodbl(D) < 0.95) return fupb_PRECI;
    3077       57746 :   L = bestappr(lambda,D);
    3078       57746 :   if (lg(L) == 1)
    3079             :   {
    3080           0 :     if (DEBUGLEVEL) err_printf("truncation error in bestappr\n");
    3081           0 :     return fupb_PRECI;
    3082             :   }
    3083       57746 :   den = Q_denom(L);
    3084       57746 :   if (mpcmp(den,D) > 0)
    3085             :   {
    3086          20 :     if (DEBUGLEVEL) err_printf("D = %Ps\nden = %Ps\n",D, i2print(den));
    3087          20 :     return fupb_PRECI;
    3088             :   }
    3089       57726 :   bit = -gexpo(gsub(L, lambda)); /* input accuracy */
    3090       57726 :   L = Q_muli_to_int(L, den);
    3091       57726 :   if (gexpo(L) + expi(den) > bit - 32)
    3092             :   {
    3093          32 :     if (DEBUGLEVEL) err_printf("dubious bestappr; den = %Ps\n", i2print(den));
    3094          32 :     return fupb_PRECI;
    3095             :   }
    3096       57694 :   H = ZM_hnf(L); r = lg(H)-1;
    3097       57694 :   if (!r || r != nbrows(H))
    3098           0 :     R = gen_0; /* wrong rank */
    3099             :   else
    3100       57694 :     R = gmul(*ptkR, gdiv(ZM_det_triangular(H), powiu(den, r)));
    3101             :   /* R = tentative regulator; regulator > 0.2 uniformly */
    3102       57694 :   if (gexpo(R) < -3) {
    3103           0 :     if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3104           0 :     return gc_long(av, fupb_PRECI);
    3105             :   }
    3106       57694 :   c = gmul(R,z); /* should be n (= 1 if we are done) */
    3107       57694 :   if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3108       57694 :   if ((reason = bad_check(c))) return gc_long(av, reason);
    3109       48433 :   *ptkR = R; *ptL = L; return fupb_NONE;
    3110             : }
    3111             : static GEN
    3112       63720 : get_clg2(GEN cyc, GEN Ga, GEN C, GEN Ur, GEN Ge, GEN M1, GEN M2)
    3113             : {
    3114       63720 :   GEN GD = gsub(act_arch(M1, C), diagact_arch(cyc, Ga));
    3115       63720 :   GEN ga = gsub(act_arch(M2, C), act_arch(Ur, Ga));
    3116       63720 :   return mkvecn(6, Ur, ga, GD, Ge, M1, M2);
    3117             : }
    3118             : /* compute class group (clg1) + data for isprincipal (clg2) */
    3119             : static GEN
    3120       63622 : class_group_gen(GEN nf,GEN W,GEN C,GEN Vbase,long prec, GEN *pclg2)
    3121             : {
    3122             :   GEN M1, M2, z, G, Ga, Ge, cyc, X, Y, D, U, V, Ur, Ui, Uir;
    3123             :   long j, l;
    3124             : 
    3125       63622 :   D = ZM_snfall(W,&U,&V); /* UWV=D, D diagonal, G = g Ui (G=new gens, g=old) */
    3126       63623 :   Ui = ZM_inv(U, NULL);
    3127       63623 :   l = lg(D); cyc = cgetg(l, t_VEC); /* elementary divisors */
    3128       92351 :   for (j = 1; j < l; j++)
    3129             :   {
    3130       30323 :     gel(cyc,j) = gcoeff(D,j,j); /* strip useless components */
    3131       30323 :     if (is_pm1(gel(cyc,j))) break;
    3132             :   }
    3133       63623 :   l = j;
    3134       63623 :   Ur  = ZM_hnfdivrem(U, D, &Y);
    3135       63622 :   Uir = ZM_hnfdivrem(Ui,W, &X);
    3136             :  /* {x} = logarithmic embedding of x (arch. component)
    3137             :   * NB: [J,z] = idealred(I) --> I = y J, with {y} = - z
    3138             :   * G = g Uir - {Ga},  Uir = Ui + WX
    3139             :   * g = G Ur  - {ga},  Ur  = U + DY */
    3140       63622 :   G = cgetg(l,t_VEC);
    3141       63622 :   Ga= cgetg(l,t_MAT);
    3142       63622 :   Ge= cgetg(l,t_COL);
    3143       63622 :   z = init_famat(NULL);
    3144       92350 :   for (j = 1; j < l; j++)
    3145             :   {
    3146       28728 :     GEN I = genback(z, nf, Vbase, gel(Uir,j));
    3147       28728 :     gel(G,j) = gel(I,1); /* generator, order cyc[j] */
    3148       28728 :     gel(Ge,j)= gel(I,2);
    3149       28728 :     gel(Ga,j)= nf_cxlog(nf, gel(I,2), prec);
    3150       28728 :     if (!gel(Ga,j)) pari_err_PREC("class_group_gen");
    3151             :   }
    3152             :   /* {ga} = {GD}Y + G U - g = {GD}Y - {Ga} U + gW X U
    3153             :                             = gW (X Ur + V Y) - {Ga}Ur */
    3154       63622 :   M2 = ZM_add(ZM_mul(X,Ur), ZM_mul(V,Y));
    3155       63623 :   setlg(cyc,l); setlg(V,l); setlg(D,l);
    3156             :   /* G D =: {GD} = g (Ui + W X) D - {Ga}D = g W (V + X D) - {Ga}D
    3157             :    * NB: Ui D = W V. gW is given by (first l-1 cols of) C */
    3158       63623 :   M1 = ZM_add(V, ZM_mul(X,D));
    3159       63623 :   *pclg2 = get_clg2(cyc, Ga, C, Ur, Ge, M1, M2);
    3160       63623 :   return mkvec3(ZV_prod(cyc), cyc, G);
    3161             : }
    3162             : 
    3163             : /* compute principal ideals corresponding to (gen[i]^cyc[i]) */
    3164             : static GEN
    3165        4956 : makecycgen(GEN bnf)
    3166             : {
    3167        4956 :   GEN cyc = bnf_get_cyc(bnf), gen = bnf_get_gen(bnf), nf = bnf_get_nf(bnf);
    3168        4956 :   GEN h, y, GD = bnf_get_GD(bnf), W = bnf_get_W(bnf); /* HNF */
    3169        4956 :   GEN Sunits = bnf_get_sunits(bnf);
    3170        4956 :   GEN X = Sunits? gel(Sunits,1): NULL, C = Sunits? gel(Sunits,3): NULL;
    3171             :   long e, i, l;
    3172             : 
    3173        4956 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building cycgen)");
    3174        4956 :   h = cgetg_copy(gen, &l);
    3175       11613 :   for (i = 1; i < l; i++)
    3176             :   {
    3177        6657 :     GEN gi = gel(gen,i), ci = gel(cyc,i);
    3178        6657 :     if (X && equalii(ci, gcoeff(W,i,i)))
    3179             :     {
    3180             :       long j;
    3181        8610 :       for (j = i+1; j < l; j++)
    3182        3241 :         if (signe(gcoeff(W,i,j))) break;
    3183        5543 :       if (j == i) { gel(h,i) = mkmat2(X, gel(C,i)); continue; }
    3184             :     }
    3185        6657 :     if (abscmpiu(ci, 5) < 0)
    3186             :     {
    3187        5544 :       GEN N = ZM_det_triangular(gi);
    3188        5544 :       y = isprincipalarch(bnf,gel(GD,i), N, ci, gen_1, &e);
    3189        5544 :       if (y && fact_ok(nf,y,NULL,mkvec(gi),mkvec(ci)))
    3190             :       {
    3191        4562 :         gel(h,i) = to_famat_shallow(y,gen_1);
    3192        4562 :         continue;
    3193             :       }
    3194             :     }
    3195        2095 :     y = isprincipalfact(bnf, NULL, mkvec(gi), mkvec(ci), nf_GENMAT|nf_FORCE);
    3196        2095 :     gel(h,i) = gel(y,2);
    3197             :   }
    3198        4956 :   return h;
    3199             : }
    3200             : 
    3201             : static GEN
    3202          69 : get_y(GEN bnf, GEN W, GEN B, GEN C, GEN pFB, long j)
    3203             : {
    3204          69 :   GEN y, nf  = bnf_get_nf(bnf);
    3205          69 :   long e, lW = lg(W)-1;
    3206          69 :   GEN ex = (j<=lW)? gel(W,j): gel(B,j-lW);
    3207          69 :   GEN P = (j<=lW)? NULL: gel(pFB,j);
    3208          69 :   if (C)
    3209             :   { /* archimedean embeddings known: cheap trial */
    3210          69 :     GEN Nx = get_norm_fact_primes(pFB, ex, P);
    3211          69 :     y = isprincipalarch(bnf,gel(C,j), Nx,gen_1, gen_1, &e);
    3212          69 :     if (y && fact_ok(nf,y,P,pFB,ex)) return y;
    3213             :   }
    3214           0 :   y = isprincipalfact_or_fail(bnf, P, pFB, ex);
    3215           0 :   return typ(y) == t_INT? y: gel(y,2);
    3216             : }
    3217             : /* compute principal ideals corresponding to bnf relations */
    3218             : static GEN
    3219          20 : makematal(GEN bnf)
    3220             : {
    3221          20 :   GEN W = bnf_get_W(bnf), B = bnf_get_B(bnf), C = bnf_get_C(bnf);
    3222             :   GEN pFB, ma, retry;
    3223          20 :   long lma, j, prec = 0;
    3224             : 
    3225          20 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building matal)");
    3226          20 :   lma=lg(W)+lg(B)-1;
    3227          20 :   pFB = bnf_get_vbase(bnf);
    3228          20 :   ma = cgetg(lma,t_VEC);
    3229          20 :   retry = vecsmalltrunc_init(lma);
    3230          89 :   for (j=lma-1; j>0; j--)
    3231             :   {
    3232          69 :     pari_sp av = avma;
    3233          69 :     GEN y = get_y(bnf, W, B, C, pFB, j);
    3234          69 :     if (typ(y) == t_INT)
    3235             :     {
    3236           0 :       long E = itos(y);
    3237           0 :       if (DEBUGLEVEL>1) err_printf("\n%ld done later at prec %ld\n",j,E);
    3238           0 :       set_avma(av);
    3239           0 :       vecsmalltrunc_append(retry, j);
    3240           0 :       if (E > prec) prec = E;
    3241             :     }
    3242             :     else
    3243             :     {
    3244          69 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3245          69 :       gel(ma,j) = gerepileupto(av,y);
    3246             :     }
    3247             :   }
    3248          20 :   if (prec)
    3249             :   {
    3250           0 :     long k, l = lg(retry);
    3251           0 :     GEN y, nf = bnf_get_nf(bnf);
    3252           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"makematal",prec);
    3253           0 :     nf = nfnewprec_shallow(nf,prec);
    3254           0 :     bnf = Buchall(nf, nf_FORCE, prec);
    3255           0 :     if (DEBUGLEVEL) err_printf("makematal, adding missing entries:");
    3256           0 :     for (k=1; k<l; k++)
    3257             :     {
    3258           0 :       pari_sp av = avma;
    3259           0 :       long j = retry[k];
    3260           0 :       y = get_y(bnf,W,B,NULL, pFB, j);
    3261           0 :       if (typ(y) == t_INT) pari_err_PREC("makematal");
    3262           0 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3263           0 :       gel(ma,j) = gerepileupto(av,y);
    3264             :     }
    3265             :   }
    3266          20 :   if (DEBUGLEVEL>1) err_printf("\n");
    3267          20 :   return ma;
    3268             : }
    3269             : 
    3270             : enum { MATAL = 1, CYCGEN, UNITS };
    3271             : GEN
    3272       26726 : bnf_build_cycgen(GEN bnf)
    3273       26726 : { return obj_checkbuild(bnf, CYCGEN, &makecycgen); }
    3274             : GEN
    3275          20 : bnf_build_matalpha(GEN bnf)
    3276          20 : { return obj_checkbuild(bnf, MATAL, &makematal); }
    3277             : GEN
    3278       32048 : bnf_build_units(GEN bnf)
    3279       32048 : { return obj_checkbuild(bnf, UNITS, &makeunits); }
    3280             : 
    3281             : /* return fu in compact form if available; in terms of a fixed basis
    3282             :  * of S-units */
    3283             : GEN
    3284          70 : bnf_compactfu_mat(GEN bnf)
    3285             : {
    3286          70 :   GEN X, U, SUnits = bnf_get_sunits(bnf);
    3287          70 :   if (!SUnits) return NULL;
    3288          70 :   X = gel(SUnits,1);
    3289          70 :   U = gel(SUnits,2); ZM_remove_unused(&U, &X);
    3290          70 :   return mkvec2(X, U);
    3291             : }
    3292             : /* return fu in compact form if available; individually as famat */
    3293             : GEN
    3294       37135 : bnf_compactfu(GEN bnf)
    3295             : {
    3296       37135 :   GEN fu, X, U, SUnits = bnf_get_sunits(bnf);
    3297             :   long i, l;
    3298       37135 :   if (!SUnits) return NULL;
    3299       36904 :   X = gel(SUnits,1);
    3300       36904 :   U = gel(SUnits,2); l = lg(U); fu = cgetg(l, t_VEC);
    3301       60193 :   for (i = 1; i < l; i++)
    3302       23289 :     gel(fu,i) = famat_remove_trivial(mkmat2(X, gel(U,i)));
    3303       36904 :   return fu;
    3304             : }
    3305             : /* return expanded fu if available */
    3306             : GEN
    3307      263771 : bnf_has_fu(GEN bnf)
    3308             : {
    3309      263771 :   GEN fu = obj_check(bnf, UNITS);
    3310      263769 :   if (fu) return vecsplice(fu, 1);
    3311      262974 :   fu = bnf_get_fu_nocheck(bnf);
    3312      262973 :   return (typ(fu) == t_MAT)? NULL: fu;
    3313             : }
    3314             : /* return expanded fu if available; build if cheap */
    3315             : GEN
    3316      263491 : bnf_build_cheapfu(GEN bnf)
    3317             : {
    3318             :   GEN fu, SUnits;
    3319      263491 :   if ((fu = bnf_has_fu(bnf))) return fu;
    3320         142 :   if ((SUnits = bnf_get_sunits(bnf)))
    3321             :   {
    3322         142 :     pari_sp av = avma;
    3323         142 :     long e = gexpo(real_i(bnf_get_logfu(bnf)));
    3324         142 :     set_avma(av); if (e < 13) return vecsplice(bnf_build_units(bnf), 1);
    3325             :   }
    3326          77 :   return NULL;
    3327             : }
    3328             : 
    3329             : static GEN
    3330       63720 : get_regulator(GEN A)
    3331             : {
    3332       63720 :   pari_sp av = avma;
    3333             :   GEN R;
    3334             : 
    3335       63720 :   if (lg(A) == 1) return gen_1;
    3336       48523 :   R = det( rowslice(real_i(A), 1, lgcols(A)-2) );
    3337       48523 :   setabssign(R); return gerepileuptoleaf(av, R);
    3338             : }
    3339             : 
    3340             : /* return corrected archimedian components for elts of x (vector)
    3341             :  * (= log(sigma_i(x)) - log(|Nx|) / [K:Q]) */
    3342             : static GEN
    3343          40 : get_archclean(GEN nf, GEN x, long prec, int units)
    3344             : {
    3345          40 :   long k, N, l = lg(x);
    3346          40 :   GEN M = cgetg(l, t_MAT);
    3347             : 
    3348          40 :   if (l == 1) return M;
    3349          26 :   N = nf_get_degree(nf);
    3350         114 :   for (k = 1; k < l; k++)
    3351             :   {
    3352          88 :     pari_sp av = avma;
    3353          88 :     GEN c = nf_cxlog(nf, gel(x,k), prec);
    3354          88 :     if (!c || (!units && !(c = cleanarch(c, N, NULL,prec)))) return NULL;
    3355          88 :     gel(M,k) = gerepilecopy(av, c);
    3356             :   }
    3357          26 :   return M;
    3358             : }
    3359             : static void
    3360          77 : Sunits_archclean(GEN nf, GEN Sunits, GEN *pmun, GEN *pC, long prec)
    3361             : {
    3362          77 :   GEN ipi, M, X = gel(Sunits,1), U = gel(Sunits,2), G = gel(Sunits,3);
    3363          77 :   long k, N = nf_get_degree(nf), l = lg(X);
    3364             : 
    3365          77 :   M = cgetg(l, t_MAT);
    3366        3640 :   for (k = 1; k < l; k++)
    3367        3563 :     if (!(gel(M,k) = nf_cxlog(nf, gel(X,k), prec))) return;
    3368          77 :   ipi = invr(mppi(prec));
    3369          77 :   *pmun = cleanarch(RgM_ZM_mul(M, U), N, ipi, prec); /* not cleanarchunit ! */
    3370          77 :   if (*pmun) *pC = cleanarch(RgM_ZM_mul(M, G), N, ipi, prec);
    3371             : }
    3372             : 
    3373             : GEN
    3374          97 : bnfnewprec_shallow(GEN bnf, long prec)
    3375             : {
    3376          97 :   GEN nf0 = bnf_get_nf(bnf), nf, v, fu, matal, y, A, C;
    3377          97 :   GEN Sunits = bnf_get_sunits(bnf), Ur, Ga, Ge, M1, M2;
    3378          97 :   long r1, r2, prec0 = prec;
    3379             : 
    3380          97 :   nf_get_sign(nf0, &r1, &r2);
    3381          97 :   if (Sunits)
    3382             :   {
    3383          77 :     fu = matal = NULL;
    3384          77 :     prec += nbits2extraprec(gexpo(Sunits));
    3385             :   }
    3386             :   else
    3387             :   {
    3388          20 :     fu = bnf_build_units(bnf);
    3389          20 :     fu = vecslice(fu, 2, lg(fu)-1);
    3390          20 :     if (r1 + r2 > 1) {
    3391          13 :       long e = gexpo(bnf_get_logfu(bnf)) + 1 - TWOPOTBITS_IN_LONG;
    3392          13 :       if (e >= 0) prec += nbits2extraprec(e);
    3393             :     }
    3394          20 :     matal = bnf_build_matalpha(bnf);
    3395             :   }
    3396             : 
    3397          97 :   if (DEBUGLEVEL && prec0 != prec) pari_warn(warnprec,"bnfnewprec",prec);
    3398          97 :   for(C = NULL;;)
    3399           0 :   {
    3400          97 :     pari_sp av = avma;
    3401          97 :     nf = nfnewprec_shallow(nf0,prec);
    3402          97 :     if (Sunits)
    3403          77 :       Sunits_archclean(nf, Sunits, &A, &C, prec);
    3404             :     else
    3405             :     {
    3406          20 :       A = get_archclean(nf, fu, prec, 1);
    3407          20 :       if (A) C = get_archclean(nf, matal, prec, 0);
    3408             :     }
    3409          97 :     if (C) break;
    3410           0 :     set_avma(av); prec = precdbl(prec);
    3411           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"bnfnewprec(extra)",prec);
    3412             :   }
    3413          97 :   y = leafcopy(bnf);
    3414          97 :   gel(y,3) = A;
    3415          97 :   gel(y,4) = C;
    3416          97 :   gel(y,7) = nf;
    3417          97 :   gel(y,8) = v = leafcopy(gel(bnf,8));
    3418          97 :   gel(v,2) = get_regulator(A);
    3419          97 :   v = gel(bnf,9);
    3420          97 :   if (lg(v) < 7) pari_err_TYPE("bnfnewprec [obsolete bnf format]", bnf);
    3421          97 :   Ur = gel(v,1);
    3422          97 :   Ge = gel(v,4);
    3423          97 :   Ga = nfV_cxlog(nf, Ge, prec);
    3424          97 :   M1 = gel(v,5);
    3425          97 :   M2 = gel(v,6);
    3426          97 :   gel(y,9) = get_clg2(bnf_get_cyc(bnf), Ga, C, Ur, Ge, M1, M2);
    3427          97 :   return y;
    3428             : }
    3429             : GEN
    3430          21 : bnfnewprec(GEN bnf, long prec)
    3431             : {
    3432          21 :   pari_sp av = avma;
    3433          21 :   return gerepilecopy(av, bnfnewprec_shallow(checkbnf(bnf), prec));
    3434             : }
    3435             : 
    3436             : GEN
    3437           0 : bnrnewprec_shallow(GEN bnr, long prec)
    3438             : {
    3439           0 :   GEN y = cgetg(7,t_VEC);
    3440             :   long i;
    3441           0 :   gel(y,1) = bnfnewprec_shallow(bnr_get_bnf(bnr), prec);
    3442           0 :   for (i=2; i<7; i++) gel(y,i) = gel(bnr,i);
    3443           0 :   return y;
    3444             : }
    3445             : GEN
    3446           7 : bnrnewprec(GEN bnr, long prec)
    3447             : {
    3448           7 :   GEN y = cgetg(7,t_VEC);
    3449             :   long i;
    3450           7 :   checkbnr(bnr);
    3451           7 :   gel(y,1) = bnfnewprec(bnr_get_bnf(bnr), prec);
    3452          42 :   for (i=2; i<7; i++) gel(y,i) = gcopy(gel(bnr,i));
    3453           7 :   return y;
    3454             : }
    3455             : 
    3456             : static GEN
    3457       64778 : buchall_end(GEN nf,GEN res, GEN clg2, GEN W, GEN B, GEN A, GEN C,GEN Vbase)
    3458             : {
    3459       64778 :   GEN z = obj_init(9, 3);
    3460       64778 :   gel(z,1) = W;
    3461       64778 :   gel(z,2) = B;
    3462       64778 :   gel(z,3) = A;
    3463       64778 :   gel(z,4) = C;
    3464       64778 :   gel(z,5) = Vbase;
    3465       64778 :   gel(z,6) = gen_0;
    3466       64778 :   gel(z,7) = nf;
    3467       64778 :   gel(z,8) = res;
    3468       64778 :   gel(z,9) = clg2;
    3469       64778 :   return z;
    3470             : }
    3471             : 
    3472             : GEN
    3473        2555 : bnfinit0(GEN P, long flag, GEN data, long prec)
    3474             : {
    3475        2555 :   double c1 = 0., c2 = 0.;
    3476        2555 :   long fl, relpid = degpol(P)==2 ? 0: BNF_RELPID;
    3477             : 
    3478        2555 :   if (data)
    3479             :   {
    3480          21 :     long lx = lg(data);
    3481          21 :     if (typ(data) != t_VEC || lx > 5) pari_err_TYPE("bnfinit",data);
    3482          21 :     switch(lx)
    3483             :     {
    3484           0 :       case 4: relpid = itos(gel(data,3));
    3485          14 :       case 3: c2 = gtodouble(gel(data,2));
    3486          21 :       case 2: c1 = gtodouble(gel(data,1));
    3487             :     }
    3488             :   }
    3489        2555 :   switch(flag)
    3490             :   {
    3491        1729 :     case 2:
    3492        1729 :     case 0: fl = 0; break;
    3493         826 :     case 1: fl = nf_FORCE; break;
    3494           0 :     default: pari_err_FLAG("bnfinit");
    3495             :       return NULL; /* LCOV_EXCL_LINE */
    3496             :   }
    3497        2555 :   return Buchall_param(P, c1, c2, relpid, fl, prec);
    3498             : }
    3499             : GEN
    3500       62234 : Buchall(GEN P, long flag, long prec)
    3501       62234 : { return Buchall_param(P, 0., 0., BNF_RELPID, flag & nf_FORCE, prec); }
    3502             : 
    3503             : static GEN
    3504        1155 : Buchall_deg1(GEN nf)
    3505             : {
    3506        1155 :   GEN v = cgetg(1,t_VEC), m = cgetg(1,t_MAT);
    3507        1155 :   GEN res, W, A, B, C, Vbase = cgetg(1,t_COL);
    3508        1155 :   GEN fu = v, R = gen_1, zu = mkvec2(gen_2, gen_m1);
    3509        1155 :   GEN clg1 = mkvec3(gen_1,v,v), clg2 = mkvecn(6, m,m,m,v,m,m);
    3510             : 
    3511        1155 :   W = A = B = C = m; res = mkvec5(clg1, R, gen_1, zu, fu);
    3512        1155 :   return buchall_end(nf,res,clg2,W,B,A,C,Vbase);
    3513             : }
    3514             : 
    3515             : /* return (small set of) indices of columns generating the same lattice as x.
    3516             :  * Assume HNF(x) is inexpensive (few rows, many columns).
    3517             :  * Dichotomy approach since interesting columns may be at the very end */
    3518             : GEN
    3519       63623 : extract_full_lattice(GEN x)
    3520             : {
    3521       63623 :   long dj, j, k, l = lg(x);
    3522             :   GEN h, h2, H, v;
    3523             : 
    3524       63623 :   if (l < 200) return NULL; /* not worth it */
    3525             : 
    3526           5 :   v = vecsmalltrunc_init(l);
    3527           5 :   H = ZM_hnf(x);
    3528           5 :   h = cgetg(1, t_MAT);
    3529           5 :   dj = 1;
    3530         215 :   for (j = 1; j < l; )
    3531             :   {
    3532         215 :     pari_sp av = avma;
    3533         215 :     long lv = lg(v);
    3534             : 
    3535         725 :     for (k = 0; k < dj; k++) v[lv+k] = j+k;
    3536         215 :     setlg(v, lv + dj);
    3537         215 :     h2 = ZM_hnf(vecpermute(x, v));
    3538         215 :     if (ZM_equal(h, h2))
    3539             :     { /* these dj columns can be eliminated */
    3540          85 :       set_avma(av); setlg(v, lv);
    3541          85 :       j += dj;
    3542          85 :       if (j >= l) break;
    3543          85 :       dj <<= 1;
    3544          85 :       if (j + dj >= l) { dj = (l - j) >> 1; if (!dj) dj = 1; }
    3545             :     }
    3546         130 :     else if (dj > 1)
    3547             :     { /* at least one interesting column, try with first half of this set */
    3548          85 :       set_avma(av); setlg(v, lv);
    3549          85 :       dj >>= 1; /* > 0 */
    3550             :     }
    3551             :     else
    3552             :     { /* this column should be kept */
    3553          45 :       if (ZM_equal(h2, H)) break;
    3554          40 :       h = h2; j++;
    3555             :     }
    3556             :   }
    3557           5 :   return v;
    3558             : }
    3559             : 
    3560             : static void
    3561       63663 : init_rel(RELCACHE_t *cache, FB_t *F, long add_need)
    3562             : {
    3563       63663 :   const long n = F->KC + add_need; /* expected # of needed relations */
    3564             :   long i, j, k, p;
    3565             :   GEN c, P;
    3566             :   GEN R;
    3567             : 
    3568       63663 :   if (DEBUGLEVEL) err_printf("KCZ = %ld, KC = %ld, n = %ld\n", F->KCZ,F->KC,n);
    3569       63663 :   reallocate(cache, 10*n + 50); /* make room for lots of relations */
    3570       63663 :   cache->chk = cache->base;
    3571       63663 :   cache->end = cache->base + n;
    3572       63663 :   cache->relsup = add_need;
    3573       63663 :   cache->last = cache->base;
    3574       63663 :   cache->missing = lg(cache->basis) - 1;
    3575      302870 :   for (i = 1; i <= F->KCZ; i++)
    3576             :   { /* trivial relations (p) = prod P^e */
    3577      239207 :     p = F->FB[i]; P = gel(F->LV,p);
    3578      239207 :     if (!isclone(P)) continue;
    3579             : 
    3580             :     /* all prime divisors in FB */
    3581      166799 :     c = zero_Flv(F->KC); k = F->iLP[p];
    3582      166795 :     R = c; c += k;
    3583      532555 :     for (j = lg(P)-1; j; j--) c[j] = pr_get_e(gel(P,j));
    3584      166795 :     add_rel(cache, F, R, k+1, pr_get_p(gel(P,1)), 0);
    3585             :   }
    3586       63663 : }
    3587             : 
    3588             : /* Let z = \zeta_n in nf. List of not-obviously-dependent generators for
    3589             :  * cyclotomic units modulo torsion in Q(z) [independent when n a prime power]:
    3590             :  * - z^a - 1,  n/(a,n) not a prime power, a \nmid n unless a=1,  1 <= a < n/2
    3591             :  * - (Z^a - 1)/(Z - 1),  p^k || n, Z = z^{n/p^k}, (p,a) = 1, 1 < a <= (p^k-1)/2
    3592             :  */
    3593             : GEN
    3594       63663 : nfcyclotomicunits(GEN nf, GEN zu)
    3595             : {
    3596       63663 :   long n = itos(gel(zu, 1)), n2, lP, i, a;
    3597             :   GEN z, fa, P, E, L, mz, powz;
    3598       63663 :   if (n <= 6) return cgetg(1, t_VEC);
    3599             : 
    3600        1897 :   z = algtobasis(nf,gel(zu, 2));
    3601        1897 :   if ((n & 3) == 2) { n = n >> 1; z = ZC_neg(z); } /* ensure n != 2 (mod 4) */
    3602        1897 :   n2 = n/2;
    3603        1897 :   mz = zk_multable(nf, z); /* multiplication by z */
    3604        1897 :   powz = cgetg(n2, t_VEC); gel(powz,1) = z;
    3605        6237 :   for (i = 2; i < n2; i++) gel(powz,i) = ZM_ZC_mul(mz, gel(powz,i-1));
    3606             :   /* powz[i] = z^i */
    3607             : 
    3608        1897 :   L = vectrunc_init(n);
    3609        1897 :   fa = factoru(n);
    3610        1897 :   P = gel(fa,1); lP = lg(P);
    3611        1897 :   E = gel(fa,2);
    3612        4578 :   for (i = 1; i < lP; i++)
    3613             :   { /* second kind */
    3614        2681 :     long p = P[i], k = E[i], pk = upowuu(p,k), pk2 = (pk-1) / 2;
    3615        2681 :     GEN u = gen_1;
    3616        4935 :     for (a = 2; a <= pk2; a++)
    3617             :     {
    3618        2254 :       u = nfadd(nf, u, gel(powz, (n/pk) * (a-1))); /* = (Z^a-1)/(Z-1) */
    3619        2254 :       if (a % p) vectrunc_append(L, u);
    3620             :     }
    3621             :   }
    3622        6104 :   if (lP > 2) for (a = 1; a < n2; a++)
    3623             :   { /* first kind, when n not a prime power */
    3624             :     ulong p;
    3625        4207 :     if (a > 1 && (n % a == 0 || uisprimepower(n/ugcd(a,n), &p))) continue;
    3626        1848 :     vectrunc_append(L, nfadd(nf, gel(powz, a), gen_m1));
    3627             :   }
    3628        1897 :   return L;
    3629             : }
    3630             : static void
    3631       63663 : add_cyclotomic_units(GEN nf, GEN zu, RELCACHE_t *cache, FB_t *F)
    3632             : {
    3633       63663 :   pari_sp av = avma;
    3634       63663 :   GEN L = nfcyclotomicunits(nf, zu);
    3635       63663 :   long i, l = lg(L);
    3636       63663 :   if (l > 1)
    3637             :   {
    3638        1897 :     GEN R = zero_Flv(F->KC);
    3639        5901 :     for(i = 1; i < l; i++) add_rel(cache, F, R, F->KC+1, gel(L,i), 0);
    3640             :   }
    3641       63663 :   set_avma(av);
    3642       63663 : }
    3643             : 
    3644             : static GEN
    3645      106786 : trim_list(FB_t *F)
    3646             : {
    3647      106786 :   pari_sp av = avma;
    3648      106786 :   GEN v, L_jid = F->L_jid, minidx = F->minidx, present = zero_Flv(F->KC);
    3649      106785 :   long i, j, imax = minss(lg(L_jid), F->KC + 1);
    3650             : 
    3651      106785 :   v = cgetg(imax, t_VECSMALL);
    3652     1253975 :   for (i = j = 1; i < imax; i++)
    3653             :   {
    3654     1147189 :     long k = minidx[ L_jid[i] ];
    3655     1147189 :     if (!present[k]) { v[j++] = L_jid[i]; present[k] = 1; }
    3656             :   }
    3657      106786 :   setlg(v, j); return gerepileuptoleaf(av, v);
    3658             : }
    3659             : 
    3660             : static void
    3661        8730 : try_elt(RELCACHE_t *cache, FB_t *F, GEN nf, GEN x, FACT *fact)
    3662             : {
    3663        8730 :   pari_sp av = avma;
    3664             :   GEN R, Nx;
    3665        8730 :   long nz, tx = typ(x);
    3666             : 
    3667        8730 :   if (tx == t_INT || tx == t_FRAC) return;
    3668        8589 :   if (tx != t_COL) x = algtobasis(nf, x);
    3669        8589 :   if (RgV_isscalar(x)) return;
    3670        8589 :   x = Q_primpart(x);
    3671        8589 :   Nx = nfnorm(nf, x);
    3672        8589 :   if (!can_factor(F, nf, NULL, x, Nx, fact)) return;
    3673             : 
    3674             :   /* smooth element */
    3675        8589 :   R = set_fact(F, fact, NULL, &nz);
    3676             :   /* make sure we get maximal rank first, then allow all relations */
    3677        8589 :   (void) add_rel(cache, F, R, nz, x, 0);
    3678        8589 :   set_avma(av);
    3679             : }
    3680             : 
    3681             : static void
    3682       38810 : matenlarge(GEN C, long h)
    3683             : {
    3684       38810 :   GEN _0 = zerocol(h);
    3685             :   long i;
    3686     2996602 :   for (i = lg(C); --i; ) gel(C,i) = shallowconcat(gel(C,i), _0);
    3687       38812 : }
    3688             : 
    3689             : /* E = floating point embeddings */
    3690             : static GEN
    3691       38810 : matbotidembs(RELCACHE_t *cache, GEN E)
    3692             : {
    3693       38810 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3694       38810 :   long j, d = h - w, hE = nbrows(E);
    3695       38810 :   GEN y = cgetg(w+1,t_MAT), _0 = zerocol(h);
    3696      156072 :   for (j = 1; j <= w; j++)
    3697             :   {
    3698      117262 :     GEN c = shallowconcat(gel(E,j), _0);
    3699      117262 :     if (d + j >= 1) gel(c, d + j + hE) = gen_1;
    3700      117262 :     gel(y,j) = c;
    3701             :   }
    3702       38810 :   return y;
    3703             : }
    3704             : static GEN
    3705       62094 : matbotid(RELCACHE_t *cache)
    3706             : {
    3707       62094 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3708       62094 :   long j, d = h - w;
    3709       62094 :   GEN y = cgetg(w+1,t_MAT);
    3710      898016 :   for (j = 1; j <= w; j++)
    3711             :   {
    3712      835925 :     GEN c = zerocol(h);
    3713      835923 :     if (d + j >= 1) gel(c, d + j) = gen_1;
    3714      835923 :     gel(y,j) = c;
    3715             :   }
    3716       62091 :   return y;
    3717             : }
    3718             : 
    3719             : static long
    3720          75 : myprecdbl(long prec, GEN C)
    3721             : {
    3722          75 :   long p = prec < 1280? precdbl(prec): (long)(prec * 1.5);
    3723          75 :   if (C) p = maxss(p, minss(3*p, prec + nbits2extraprec(gexpo(C))));
    3724          75 :   return p;
    3725             : }
    3726             : 
    3727             : static GEN
    3728       57505 : _nfnewprec(GEN nf, long prec, long *isclone)
    3729             : {
    3730       57505 :   GEN NF = gclone(nfnewprec_shallow(nf, prec));
    3731       57505 :   if (*isclone) gunclone(nf);
    3732       57505 :   *isclone = 1; return NF;
    3733             : }
    3734             : 
    3735             : /* Nrelid = nb relations per ideal, possibly 0. If flag is set, keep data in
    3736             :  * algebraic form. */
    3737             : GEN
    3738       64789 : Buchall_param(GEN P, double cbach, double cbach2, long Nrelid, long flag, long prec)
    3739             : {
    3740             :   pari_timer T;
    3741       64789 :   pari_sp av0 = avma, av, av2;
    3742             :   long PREC, N, R1, R2, RU, low, high, LIMC0, LIMC, LIMC2, LIMCMAX, zc, i;
    3743       64789 :   long LIMres, bit = 0, flag_nfinit = 0;
    3744       64789 :   long nreldep, sfb_trials, need, old_need, precdouble = 0, TRIES = 0;
    3745       64789 :   long nfisclone = 0;
    3746             :   long done_small, small_fail, fail_limit, squash_index, small_norm_prec;
    3747             :   double LOGD, LOGD2, lim;
    3748       64789 :   GEN computed = NULL, fu = NULL, zu, nf, M_sn, D, A, W, R, h, Ce, PERM;
    3749             :   GEN small_multiplier, auts, cyclic, embs, SUnits;
    3750             :   GEN res, L, invhr, B, C, lambda, dep, clg1, clg2, Vbase;
    3751       64789 :   const char *precpb = NULL;
    3752       64789 :   REL_t *old_cache = NULL;
    3753             :   nfmaxord_t nfT;
    3754             :   RELCACHE_t cache;
    3755             :   FB_t F;
    3756             :   GRHcheck_t GRHcheck;
    3757             :   FACT *fact;
    3758             : 
    3759       64789 :   if (DEBUGLEVEL) timer_start(&T);
    3760       64789 :   P = get_nfpol(P, &nf);
    3761       64775 :   if (nf)
    3762        3556 :     D = nf_get_disc(nf);
    3763             :   else
    3764             :   {
    3765       61219 :     nfinit_basic(&nfT, P);
    3766       61221 :     D = nfT.dK;
    3767       61221 :     if (!ZX_is_monic(nfT.T0))
    3768             :     {
    3769          14 :       pari_warn(warner,"nonmonic polynomial in bnfinit, using polredbest");
    3770          14 :       flag_nfinit = nf_RED;
    3771             :     }
    3772             :   }
    3773       64777 :   PREC = maxss(DEFAULTPREC, prec);
    3774       64777 :   N = degpol(P);
    3775       64777 :   if (N <= 1)
    3776             :   {
    3777        1155 :     if (!nf) nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3778        1155 :     return gerepilecopy(av0, Buchall_deg1(nf));
    3779             :   }
    3780       63622 :   D = absi_shallow(D);
    3781       63622 :   LOGD = dbllog2(D) * M_LN2;
    3782       63622 :   LOGD2 = LOGD*LOGD;
    3783       63622 :   LIMCMAX = (long)(4.*LOGD2);
    3784             :   /* In small_norm, LLL reduction produces v0 in I such that
    3785             :    *     T2(v0) <= (4/3)^((n-1)/2) NI^(2/n) disc(K)^(1/n)
    3786             :    * We consider v with T2(v) <= BMULT * T2(v0)
    3787             :    * Hence Nv <= ((4/3)^((n-1)/2) * BMULT / n)^(n/2) NI sqrt(disc(K)).
    3788             :    * NI <= LIMCMAX^2 */
    3789       63622 :   if (nf) PREC = maxss(PREC, nf_get_prec(nf));
    3790       63622 :   PREC = maxss(PREC, nbits2prec((long)(LOGD2 * 0.02) + N*N));
    3791       63622 :   if (DEBUGLEVEL) err_printf("PREC = %ld\n", PREC);
    3792       63622 :   small_norm_prec = nbits2prec( BITS_IN_LONG +
    3793       63622 :     (N/2. * ((N-1)/2.*log(4./3) + log(8/(double)N))
    3794       63622 :      + 2*log((double) LIMCMAX) + LOGD/2) / M_LN2 ); /*enough to compute norms*/
    3795       63622 :   if (small_norm_prec > PREC) PREC = small_norm_prec;
    3796       63622 :   if (!nf)
    3797       60241 :     nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3798        3381 :   else if (nf_get_prec(nf) < PREC)
    3799         192 :     nf = nfnewprec_shallow(nf, PREC);
    3800       63623 :   M_sn = nf_get_M(nf);
    3801       63623 :   if (PREC > small_norm_prec) M_sn = gprec_w(M_sn, small_norm_prec);
    3802             : 
    3803       63623 :   zu = nfrootsof1(nf);
    3804       63622 :   gel(zu,2) = nf_to_scalar_or_alg(nf, gel(zu,2));
    3805             : 
    3806       63622 :   nf_get_sign(nf, &R1, &R2); RU = R1+R2;
    3807       63622 :   auts = automorphism_matrices(nf, &cyclic);
    3808       63623 :   F.embperm = automorphism_perms(nf_get_M(nf), auts, cyclic, R1, R2, N);
    3809       63623 :   if (DEBUGLEVEL)
    3810             :   {
    3811           0 :     timer_printf(&T, "nfinit & nfrootsof1");
    3812           0 :     err_printf("%s bnf: R1 = %ld, R2 = %ld\nD = %Ps\n",
    3813             :                flag? "Algebraic": "Floating point", R1,R2, D);
    3814             :   }
    3815       63623 :   if (LOGD < 20.)
    3816             :   { /* tiny disc, Minkowski may be smaller than Bach */
    3817       62181 :     lim = exp(-N + R2 * log(4/M_PI) + LOGD/2) * sqrt(2*M_PI*N);
    3818       62181 :     if (lim < 3) lim = 3;
    3819             :   }
    3820             :   else /* to be ignored */
    3821        1442 :     lim = -1;
    3822       63623 :   if (cbach > 12.) {
    3823           0 :     if (cbach2 < cbach) cbach2 = cbach;
    3824           0 :     cbach = 12.;
    3825             :   }
    3826       63623 :   if (cbach < 0.)
    3827           0 :     pari_err_DOMAIN("Buchall","Bach constant","<",gen_0,dbltor(cbach));
    3828             : 
    3829       63623 :   cache.base = NULL; F.subFB = NULL; F.LP = NULL; SUnits = Ce = NULL;
    3830       63623 :   init_GRHcheck(&GRHcheck, N, R1, LOGD);
    3831       63621 :   high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
    3832      310249 :   while (!GRHchk(nf, &GRHcheck, high)) { low = high; high *= 2; }
    3833      246670 :   while (high - low > 1)
    3834             :   {
    3835      183049 :     long test = (low+high)/2;
    3836      183049 :     if (GRHchk(nf, &GRHcheck, test)) high = test; else low = test;
    3837             :   }
    3838       63621 :   LIMC2 = (high == LIMC0+1 && GRHchk(nf, &GRHcheck, LIMC0))? LIMC0: high;
    3839       63621 :   if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
    3840             :   /* Assuming GRH, {P, NP <= LIMC2} generate Cl(K) */
    3841       63621 :   if (DEBUGLEVEL) err_printf("LIMC2 = %ld\n", LIMC2);
    3842       63623 :   LIMC0 = (long)(cbach*LOGD2); /* initial value for LIMC */
    3843       63623 :   LIMC = cbach? LIMC0: LIMC2; /* use {P, NP <= LIMC} as a factorbase */
    3844       63623 :   LIMC = maxss(LIMC, nthideal(&GRHcheck, nf, N));
    3845       63622 :   if (DEBUGLEVEL) timer_printf(&T, "computing Bach constant");
    3846       63622 :   LIMres = primeneeded(N, R1, R2, LOGD);
    3847       63623 :   cache_prime_dec(&GRHcheck, LIMres, nf);
    3848             :   /* invhr ~ 2^r1 (2pi)^r2 / sqrt(D) w * Res(zeta_K, s=1) = 1 / hR */
    3849      127246 :   invhr = gmul(gdiv(gmul2n(powru(mppi(DEFAULTPREC), R2), RU),
    3850       63621 :               mulri(gsqrt(D,DEFAULTPREC),gel(zu,1))),
    3851             :               compute_invres(&GRHcheck, LIMres));
    3852       63623 :   if (DEBUGLEVEL) timer_printf(&T, "computing inverse of hR");
    3853       63623 :   av = avma;
    3854             : 
    3855       65840 : START:
    3856       65840 :   if (DEBUGLEVEL) timer_start(&T);
    3857       65840 :   if (TRIES) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
    3858       65840 :   if (DEBUGLEVEL && LIMC > LIMC0)
    3859           0 :     err_printf("%s*** Bach constant: %f\n", TRIES?"\n":"", LIMC/LOGD2);
    3860       65840 :   if (cache.base)
    3861             :   {
    3862             :     REL_t *rel;
    3863       22822 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    3864       22782 :       if (rel->m) i++;
    3865          40 :     computed = cgetg(i, t_VEC);
    3866       22822 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    3867       22782 :       if (rel->m) gel(computed, i++) = rel->m;
    3868          40 :     computed = gclone(computed); delete_cache(&cache);
    3869             :   }
    3870       65840 :   TRIES++; set_avma(av);
    3871       65840 :   if (F.LP) delete_FB(&F);
    3872       65840 :   if (LIMC2 < LIMC) LIMC2 = LIMC;
    3873       65840 :   if (DEBUGLEVEL) { err_printf("LIMC = %ld, LIMC2 = %ld\n",LIMC,LIMC2); }
    3874             : 
    3875       65840 :   FBgen(&F, nf, N, LIMC, LIMC2, &GRHcheck);
    3876       65838 :   if (!F.KC) goto START;
    3877       65838 :   av = avma;
    3878       65838 :   subFBgen(&F,auts,cyclic,lim < 0? LIMC2: mindd(lim,LIMC2),MINSFB);
    3879       65839 :   if (lg(F.subFB) == 1) goto START;
    3880       63662 :   if (DEBUGLEVEL)
    3881           0 :     timer_printf(&T, "factorbase (#subFB = %ld) and ideal permutations",
    3882           0 :                      lg(F.subFB)-1);
    3883             : 
    3884       63662 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    3885       63662 :   PERM = leafcopy(F.perm); /* to be restored in case of precision increase */
    3886       63661 :   cache.basis = zero_Flm_copy(F.KC,F.KC);
    3887       63663 :   small_multiplier = zero_Flv(F.KC);
    3888       63663 :   done_small = small_fail = squash_index = zc = sfb_trials = nreldep = 0;
    3889       63663 :   fail_limit = F.KC + 1;
    3890       63663 :   W = A = R = NULL;
    3891       63663 :   av2 = avma;
    3892       63663 :   init_rel(&cache, &F, RELSUP + RU-1);
    3893       63663 :   old_need = need = cache.end - cache.last;
    3894       63663 :   add_cyclotomic_units(nf, zu, &cache, &F);
    3895       63663 :   if (DEBUGLEVEL) err_printf("\n");
    3896       63663 :   cache.end = cache.last + need;
    3897             : 
    3898       63663 :   if (computed)
    3899             :   {
    3900        8770 :     for (i = 1; i < lg(computed); i++)
    3901        8730 :       try_elt(&cache, &F, nf, gel(computed, i), fact);
    3902          40 :     gunclone(computed);
    3903          40 :     if (DEBUGLEVEL && i > 1)
    3904           0 :       timer_printf(&T, "including already computed relations");
    3905          40 :     need = 0;
    3906             :   }
    3907             : 
    3908             :   do
    3909             :   {
    3910             :     GEN Ar, C0;
    3911             :     do
    3912             :     {
    3913      106900 :       pari_sp av4 = avma;
    3914      106900 :       if (need > 0)
    3915             :       {
    3916      106785 :         long oneed = cache.end - cache.last;
    3917             :         /* Test below can be true if small_norm did not find enough linearly
    3918             :          * dependent relations */
    3919      106785 :         if (need < oneed) need = oneed;
    3920      106785 :         pre_allocate(&cache, need+lg(auts)-1+(R ? lg(W)-1 : 0));
    3921      106786 :         cache.end = cache.last + need;
    3922      106786 :         F.L_jid = trim_list(&F);
    3923             :       }
    3924      106901 :       if (need > 0 && Nrelid > 0 && (done_small <= F.KC+1 || A) &&
    3925      103687 :           small_fail <= fail_limit &&
    3926      103687 :           cache.last < cache.base + 2*F.KC+2*RU+RELSUP /* heuristic */)
    3927             :       {
    3928       89752 :         long j, k, LIE = (R && lg(W) > 1 && (done_small % 2));
    3929       89752 :         REL_t *last = cache.last;
    3930       89752 :         pari_sp av3 = avma;
    3931             :         GEN p0;
    3932       89752 :         if (LIE)
    3933             :         { /* We have full rank for class group and unit. The following tries to
    3934             :            * improve the prime group lattice by looking for relations involving
    3935             :            * the primes generating the class group. */
    3936        3291 :           long n = lg(W)-1; /* need n relations to squash the class group */
    3937        3291 :           F.L_jid = vecslice(F.perm, 1, n);
    3938        3291 :           cache.end = cache.last + n;
    3939             :           /* Lie to the add_rel subsystem: pretend we miss relations involving
    3940             :            * the primes generating the class group (and only those). */
    3941        3291 :           cache.missing = n;
    3942       10309 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 0;
    3943             :         }
    3944       89752 :         j = done_small % (F.KC+1);
    3945       89752 :         if (j == 0) p0 = NULL;
    3946             :         else
    3947             :         {
    3948       27013 :           p0 = gel(F.LP, j);
    3949       27013 :           if (!A)
    3950             :           { /* Prevent considering both P_iP_j and P_jP_i in small_norm */
    3951             :             /* Not all elements end up in F.L_jid (eliminated by hnfspec/add or
    3952             :              * by trim_list): keep track of which ideals are being considered
    3953             :              * at each run. */
    3954         421 :             long mj = small_multiplier[j];
    3955        7066 :             for (i = k = 1; i < lg(F.L_jid); i++)
    3956        6645 :               if (F.L_jid[i] > mj)
    3957             :               {
    3958        6645 :                 small_multiplier[F.L_jid[i]] = j;
    3959        6645 :                 F.L_jid[k++] = F.L_jid[i];
    3960             :               }
    3961         421 :             setlg(F.L_jid, k);
    3962             :           }
    3963             :         }
    3964       89752 :         if (lg(F.L_jid) > 1)
    3965       89752 :           small_norm(&cache, &F, nf, Nrelid, M_sn, fact, p0);
    3966       89749 :         F.L_jid = F.perm; set_avma(av3);
    3967       89749 :         if (!A && cache.last != last) small_fail = 0; else small_fail++;
    3968       89749 :         if (LIE)
    3969             :         { /* restore add_rel subsystem: undo above lie */
    3970        3291 :           long n = lg(W) - 1;
    3971       10309 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 1;
    3972        3291 :           cache.missing = 0;
    3973             :         }
    3974       89749 :         cache.end = cache.last;
    3975       89749 :         done_small++;
    3976       89749 :         need = F.sfb_chg = 0;
    3977             :       }
    3978      106898 :       if (need > 0)
    3979             :       { /* Random relations */
    3980       17034 :         if (++nreldep > F.MAXDEPSIZESFB) {
    3981         240 :           if (++sfb_trials > SFB_MAX && LIMC < LIMCMAX/2) goto START;
    3982         205 :           F.sfb_chg = sfb_INCREASE;
    3983         205 :           nreldep = 0;
    3984             :         }
    3985       16794 :         else if (!(nreldep % F.MAXDEPSFB))
    3986        5119 :           F.sfb_chg = sfb_CHANGE;
    3987       16999 :         if (F.sfb_chg && !subFB_change(&F)) goto START;
    3988       16994 :         rnd_rel(&cache, &F, nf, fact);
    3989       16994 :         F.L_jid = F.perm;
    3990             :       }
    3991      106858 :       if (DEBUGLEVEL) timer_start(&T);
    3992      106858 :       if (precpb)
    3993             :       {
    3994             :         REL_t *rel;
    3995          75 :         if (DEBUGLEVEL)
    3996             :         {
    3997           0 :           char str[64]; sprintf(str,"Buchall_param (%s)",precpb);
    3998           0 :           pari_warn(warnprec,str,PREC);
    3999             :         }
    4000          75 :         nf = _nfnewprec(nf, PREC, &nfisclone);
    4001          75 :         precdouble++; precpb = NULL;
    4002             : 
    4003          75 :         if (flag)
    4004             :         { /* recompute embs only, no need to redo HNF */
    4005          33 :           long j, le = lg(embs), lC = lg(C);
    4006          33 :           GEN E, M = nf_get_M(nf);
    4007          33 :           set_avma(av4);
    4008       11648 :           for (rel = cache.base+1, i = 1; i < le; i++,rel++)
    4009       11615 :             gel(embs,i) = rel_embed(rel, &F, embs, i, M, RU, R1, PREC);
    4010          33 :           E = RgM_ZM_mul(embs, rowslice(C, RU+1, nbrows(C)));
    4011       11648 :           for (j = 1; j < lC; j++)
    4012       61603 :             for (i = 1; i <= RU; i++) gcoeff(C,i,j) = gcoeff(E,i,j);
    4013          33 :           av4 = avma;
    4014             :         }
    4015             :         else
    4016             :         { /* recompute embs + HNF */
    4017       10318 :           for(i = 1; i < lg(PERM); i++) F.perm[i] = PERM[i];
    4018          42 :           cache.chk = cache.base;
    4019          42 :           W = NULL;
    4020             :         }
    4021          75 :         if (DEBUGLEVEL) timer_printf(&T, "increasing accuracy");
    4022             :       }
    4023      106858 :       set_avma(av4);
    4024      106858 :       if (cache.chk != cache.last)
    4025             :       { /* Reduce relation matrices */
    4026      106754 :         long l = cache.last - cache.chk + 1, j;
    4027      106754 :         GEN mat = cgetg(l, t_MAT);
    4028             :         REL_t *rel;
    4029             : 
    4030     1118273 :         for (j=1,rel = cache.chk + 1; j < l; rel++,j++) gel(mat,j) = rel->R;
    4031      106754 :         if (!flag || W)
    4032             :         {
    4033       44661 :           embs = get_embs(&F, &cache, nf, embs, PREC);
    4034       44662 :           if (DEBUGLEVEL && timer_get(&T) > 1)
    4035           0 :             timer_printf(&T, "floating point embeddings");
    4036             :         }
    4037      106755 :         if (!W)
    4038             :         { /* never reduced before */
    4039       63703 :           C = flag? matbotid(&cache): embs;
    4040       63705 :           W = hnfspec_i(mat, F.perm, &dep, &B, &C, F.subFB ? lg(F.subFB)-1:0);
    4041       63705 :           if (DEBUGLEVEL)
    4042           0 :             timer_printf(&T, "hnfspec [%ld x %ld]", lg(F.perm)-1, l-1);
    4043       63705 :           if (flag)
    4044             :           {
    4045       62095 :             PREC += nbits2extraprec(gexpo(C));
    4046       62095 :             if (nf_get_prec(nf) < PREC) nf = _nfnewprec(nf, PREC, &nfisclone);
    4047       62095 :             embs = get_embs(&F, &cache, nf, embs, PREC);
    4048       62095 :             C = vconcat(RgM_ZM_mul(embs, C), C);
    4049             :           }
    4050       63705 :           if (DEBUGLEVEL)
    4051           0 :             timer_printf(&T, "hnfspec floating points");
    4052             :         }
    4053             :         else
    4054             :         {
    4055       43052 :           long k = lg(embs);
    4056       43052 :           GEN E = vecslice(embs, k-l+1,k-1);
    4057       43052 :           if (flag)
    4058             :           {
    4059       38810 :             E = matbotidembs(&cache, E);
    4060       38810 :             matenlarge(C, cache.last - cache.chk);
    4061             :           }
    4062       43052 :           W = hnfadd_i(W, F.perm, &dep, &B, &C, mat, E);
    4063       43052 :           if (DEBUGLEVEL)
    4064           0 :             timer_printf(&T, "hnfadd (%ld + %ld)", l-1, lg(dep)-1);
    4065             :         }
    4066      106757 :         gerepileall(av2, 5, &W,&C,&B,&dep,&embs);
    4067      106757 :         cache.chk = cache.last;
    4068             :       }
    4069         104 :       else if (!W)
    4070             :       {
    4071           0 :         need = old_need;
    4072           0 :         F.L_jid = vecslice(F.perm, 1, need);
    4073           0 :         continue;
    4074             :       }
    4075      106861 :       need = F.KC - (lg(W)-1) - (lg(B)-1);
    4076      106861 :       if (!need && cache.missing)
    4077             :       { /* The test above will never be true except if 27449|class number.
    4078             :          * Ensure that if we have maximal rank for the ideal lattice, then
    4079             :          * cache.missing == 0. */
    4080          14 :         for (i = 1; cache.missing; i++)
    4081           7 :           if (!mael(cache.basis, i, i))
    4082             :           {
    4083             :             long j;
    4084           7 :             cache.missing--; mael(cache.basis, i, i) = 1;
    4085         427 :             for (j = i+1; j <= F.KC; j++) mael(cache.basis, j, i) = 0;
    4086             :           }
    4087             :       }
    4088      106861 :       zc = (lg(C)-1) - (lg(B)-1) - (lg(W)-1);
    4089      106861 :       if (RU-1-zc > 0) need = minss(need + RU-1-zc, F.KC); /* for units */
    4090      106861 :       if (need)
    4091             :       { /* dependent rows */
    4092         558 :         F.L_jid = vecslice(F.perm, 1, need);
    4093         558 :         vecsmall_sort(F.L_jid);
    4094         558 :         if (need != old_need) { nreldep = 0; old_need = need; }
    4095             :       }
    4096             :       else
    4097             :       { /* If the relation lattice is too small, check will be > 1 and we will
    4098             :          * do a new run of small_norm/rnd_rel asking for 1 relation. This often
    4099             :          * gives a relation involving L_jid[1]. We rotate the first element of
    4100             :          * L_jid in order to increase the probability of finding relations that
    4101             :          * increases the lattice. */
    4102      106303 :         long j, n = lg(W) - 1;
    4103      106303 :         if (n > 1 && squash_index % n)
    4104             :         {
    4105        7182 :           F.L_jid = leafcopy(F.perm);
    4106       30770 :           for (j = 1; j <= n; j++)
    4107       23588 :             F.L_jid[j] = F.perm[1 + (j + squash_index - 1) % n];
    4108             :         }
    4109             :         else
    4110       99121 :           F.L_jid = F.perm;
    4111      106303 :         squash_index++;
    4112             :       }
    4113             :     }
    4114      106861 :     while (need);
    4115             : 
    4116      106303 :     if (!A)
    4117             :     {
    4118       63663 :       small_fail = old_need = 0;
    4119       63663 :       fail_limit = maxss(F.KC / FAIL_DIVISOR, MINFAIL);
    4120             :     }
    4121      106303 :     A = vecslice(C, 1, zc); /* cols corresponding to units */
    4122      106303 :     if (flag) A = rowslice(A, 1, RU);
    4123      106303 :     Ar = real_i(A);
    4124      106303 :     R = compute_multiple_of_R(Ar, RU, N, &need, &bit, &lambda);
    4125      106303 :     if (need < old_need) small_fail = 0;
    4126             : #if 0 /* A good idea if we are indeed stuck but needs tuning */
    4127             :     /* we have computed way more relations than should be necessary */
    4128             :     if (TRIES < 3 && LIMC < LIMCMAX / 8 &&
    4129             :                      cache.last - cache.base > 10 * F.KC) goto START;
    4130             : #endif
    4131      106303 :     old_need = need;
    4132      106303 :     if (!lambda)
    4133          23 :     { precpb = "bestappr"; PREC = myprecdbl(PREC, flag? C: NULL); continue; }
    4134      106280 :     if (!R)
    4135             :     { /* not full rank for units */
    4136       32636 :       if (!need)
    4137           0 :       { precpb = "regulator"; PREC = myprecdbl(PREC, flag? C: NULL); }
    4138       32636 :       continue;
    4139             :     }
    4140       73644 :     if (cache.last==old_cache) { need=1; continue; }
    4141       73592 :     old_cache = cache.last;
    4142       73592 :     h = ZM_det_triangular(W);
    4143       73592 :     if (DEBUGLEVEL) err_printf("\n#### Tentative class number: %Ps\n", h);
    4144       73592 :     i = compute_R(lambda, mulir(h,invhr), &L, &R);
    4145       73592 :     if (DEBUGLEVEL)
    4146             :     {
    4147           0 :       err_printf("\n");
    4148           0 :       timer_printf(&T, "computing regulator and check");
    4149             :     }
    4150       73592 :     switch(i)
    4151             :     {
    4152        9917 :       case fupb_RELAT:
    4153        9917 :         need = 1; /* not enough relations */
    4154        9917 :         continue;
    4155          52 :       case fupb_PRECI: /* prec problem unless we cheat on Bach constant */
    4156          52 :         if ((precdouble&7) == 7 && LIMC <= LIMCMAX/2) goto START;
    4157          52 :         precpb = "compute_R"; PREC = myprecdbl(PREC, flag? C: NULL);
    4158          52 :         continue;
    4159             :     }
    4160             :     /* DONE */
    4161             : 
    4162       63623 :     if (F.KCZ2 > F.KCZ)
    4163             :     {
    4164           7 :       if (F.sfb_chg && !subFB_change(&F)) goto START;
    4165           7 :       if (!be_honest(&F, nf, auts, fact)) goto START;
    4166           7 :       if (DEBUGLEVEL) timer_printf(&T, "to be honest");
    4167             :     }
    4168       63623 :     F.KCZ2 = 0; /* be honest only once */
    4169             : 
    4170             :     /* fundamental units */
    4171             :     {
    4172       63623 :       GEN AU, CU, U, v = extract_full_lattice(L); /* L may be large */
    4173       63623 :       CU = NULL;
    4174       63623 :       if (v) { A = vecpermute(A, v); L = vecpermute(L, v); }
    4175             :       /* arch. components of fund. units */
    4176       63623 :       U = ZM_lll(L, 0.99, LLL_IM);
    4177       63622 :       U = ZM_mul(U, lll(RgM_ZM_mul(real_i(A), U)));
    4178       63623 :       if (DEBUGLEVEL) timer_printf(&T, "units LLL");
    4179       63623 :       AU = RgM_ZM_mul(A, U);
    4180       63623 :       A = cleanarchunit(AU, N, NULL, PREC);
    4181       63622 :       if (!A || lg(A) < RU || expo(gsub(get_regulator(A), R)) > -1)
    4182             :       {
    4183           0 :         long add = nbits2extraprec( gexpo(AU) + 64 ) - gprecision(AU);
    4184           0 :         long t = maxss((PREC-2) * 0.15, add);
    4185           0 :         if (!A && DEBUGLEVEL) err_printf("### Incorrect units lognorm");
    4186           0 :         precpb = "cleanarch"; PREC += maxss(t, EXTRAPREC64); continue;
    4187             :       }
    4188       63623 :       if (flag)
    4189             :       {
    4190       62062 :         long l = lgcols(C) - RU;
    4191             :         REL_t *rel;
    4192       62062 :         SUnits = cgetg(l, t_COL);
    4193      998671 :         for (rel = cache.base+1, i = 1; i < l; i++,rel++)
    4194      936609 :           set_rel_alpha(rel, auts, SUnits, i);
    4195       62062 :         if (RU > 1)
    4196             :         {
    4197       47390 :           GEN c = v? vecpermute(C,v): vecslice(C,1,zc);
    4198       47390 :           CU = ZM_mul(rowslice(c, RU+1, nbrows(c)), U);
    4199             :         }
    4200             :       }
    4201       63621 :       if (DEBUGLEVEL) err_printf("\n#### Computing fundamental units\n");
    4202       63621 :       fu = getfu(nf, &A, CU? &U: NULL, PREC);
    4203       63623 :       CU = CU? ZM_mul(CU, U): cgetg(1, t_MAT);
    4204       63623 :       if (DEBUGLEVEL) timer_printf(&T, "getfu");
    4205       63623 :       Ce = vecslice(C, zc+1, lg(C)-1);
    4206       63623 :       if (flag) SUnits = mkvec4(SUnits, CU, rowslice(Ce, RU+1, nbrows(Ce)),
    4207             :                                 utoipos(LIMC));
    4208             :     }
    4209             :     /* class group generators */
    4210       63623 :     if (flag) Ce = rowslice(Ce, 1, RU);
    4211       63623 :     C0 = Ce; Ce = cleanarch(Ce, N, NULL, PREC);
    4212       63622 :     if (!Ce) {
    4213           0 :       long add = nbits2extraprec( gexpo(C0) + 64 ) - gprecision(C0);
    4214           0 :       precpb = "cleanarch"; PREC += maxss(add, 1);
    4215             :     }
    4216       63622 :     if (DEBUGLEVEL) timer_printf(&T, "cleanarch");
    4217      106301 :   } while (need || precpb);
    4218             : 
    4219       63622 :   Vbase = vecpermute(F.LP, F.perm);
    4220       63622 :   if (!fu) fu = cgetg(1, t_MAT);
    4221       63622 :   if (!SUnits) SUnits = gen_1;
    4222       63622 :   clg1 = class_group_gen(nf,W,Ce,Vbase,PREC, &clg2);
    4223       63623 :   res = mkvec5(clg1, R, SUnits, zu, fu);
    4224       63623 :   res = buchall_end(nf,res,clg2,W,B,A,Ce,Vbase);
    4225       63623 :   delete_FB(&F);
    4226       63623 :   res = gerepilecopy(av0, res);
    4227       63623 :   if (flag) obj_insert_shallow(res, MATAL, cgetg(1,t_VEC));
    4228       63623 :   if (nfisclone) gunclone(nf);
    4229       63623 :   delete_cache(&cache);
    4230       63623 :   free_GRHcheck(&GRHcheck);
    4231       63623 :   return res;
    4232             : }

Generated by: LCOV version 1.14