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.1 lcov report (development 28695-49bb1ac00f) Lines: 2207 2406 91.7 %
Date: 2023-09-24 07:47:42 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       63625 : delete_cache(RELCACHE_t *M)
     143             : {
     144             :   REL_t *rel;
     145     1054747 :   for (rel = M->base+1; rel <= M->last; rel++)
     146             :   {
     147      991120 :     gunclone(rel->R);
     148      991121 :     if (rel->m) gunclone(rel->m);
     149             :   }
     150       63627 :   pari_free((void*)M->base); M->base = NULL;
     151       63625 : }
     152             : 
     153             : static void
     154       65802 : delete_FB(FB_t *F)
     155             : {
     156             :   subFB_t *s, *sold;
     157      132435 :   for (s = F->allsubFB; s; s = sold) { sold = s->old; pari_free(s); }
     158       65802 :   gunclone(F->minidx);
     159       65802 :   gunclone(F->idealperm);
     160       65802 : }
     161             : 
     162             : static void
     163       63702 : reallocate(RELCACHE_t *M, long len)
     164             : {
     165       63702 :   M->len = len;
     166       63702 :   if (!M->base)
     167       63625 :     M->base = (REL_t*)pari_malloc((len+1) * sizeof(REL_t));
     168             :   else
     169             :   {
     170          77 :     size_t l = M->last - M->base, c = M->chk - M->base, e = M->end - M->base;
     171          77 :     pari_realloc_ip((void**)&M->base, (len+1) * sizeof(REL_t));
     172          77 :     M->last = M->base + l;
     173          77 :     M->chk  = M->base + c;
     174          77 :     M->end  = M->base + e;
     175             :   }
     176       63702 : }
     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      271388 : bad_subFB(FB_t *F, long t)
     183             : {
     184      271388 :   GEN LP, P = gel(F->LP,t);
     185      271388 :   long p = pr_get_smallp(P);
     186      271388 :   LP = gel(F->LV,p);
     187      271388 :   return (isclone(LP) && t == F->iLP[p] + lg(LP)-1);
     188             : }
     189             : 
     190             : static void
     191       66632 : assign_subFB(FB_t *F, GEN yes, long iyes)
     192             : {
     193       66632 :   long i, lv = sizeof(subFB_t) + iyes*sizeof(long); /* for struct + GEN */
     194       66632 :   subFB_t *s = (subFB_t *)pari_malloc(lv);
     195       66632 :   s->subFB = (GEN)&s[1];
     196       66632 :   s->old = F->allsubFB; F->allsubFB = s;
     197      286712 :   for (i = 0; i < iyes; i++) s->subFB[i] = yes[i];
     198       66632 :   F->subFB = s->subFB;
     199       66632 :   F->MAXDEPSIZESFB = (iyes-1) * DEPSIZESFBMULT;
     200       66632 :   F->MAXDEPSFB = F->MAXDEPSIZESFB / DEPSFBDIV;
     201       66632 : }
     202             : 
     203             : /* Determine the permutation of the ideals made by each field automorphism */
     204             : static GEN
     205       65802 : FB_aut_perm(FB_t *F, GEN auts, GEN cyclic)
     206             : {
     207       65802 :   long i, j, m, KC = F->KC, nauts = lg(auts)-1;
     208       65802 :   GEN minidx, perm = zero_Flm_copy(KC, nauts);
     209             : 
     210       65800 :   if (!nauts) { F->minidx = gclone(identity_zv(KC)); return cgetg(1,t_MAT); }
     211       41538 :   minidx = zero_Flv(KC);
     212       90406 :   for (m = 1; m < lg(cyclic); m++)
     213             :   {
     214       48867 :     GEN thiscyc = gel(cyclic, m);
     215       48867 :     long k0 = thiscyc[1];
     216       48867 :     GEN aut = gel(auts, k0), permk0 = gel(perm, k0), ppermk;
     217       48867 :     i = 1;
     218      209745 :     while (i <= KC)
     219             :     {
     220      160877 :       pari_sp av2 = avma;
     221      160877 :       GEN seen = zero_Flv(KC), P = gel(F->LP, i);
     222      160878 :       long imin = i, p, f, l;
     223      160878 :       p = pr_get_smallp(P);
     224      160878 :       f = pr_get_f(P);
     225             :       do
     226             :       {
     227      474276 :         if (++i > KC) break;
     228      425407 :         P = gel(F->LP, i);
     229             :       }
     230      425407 :       while (p == pr_get_smallp(P) && f == pr_get_f(P));
     231      635145 :       for (j = imin; j < i; j++)
     232             :       {
     233      474272 :         GEN img = ZM_ZC_mul(aut, pr_get_gen(gel(F->LP, j)));
     234     1655671 :         for (l = imin; l < i; l++)
     235     1655671 :           if (!seen[l] && ZC_prdvd(img, gel(F->LP, l)))
     236             :           {
     237      474265 :             seen[l] = 1; permk0[j] = l; break;
     238             :           }
     239             :       }
     240      160873 :       set_avma(av2);
     241             :     }
     242       67817 :     for (ppermk = permk0, i = 2; i < lg(thiscyc); i++)
     243             :     {
     244       18949 :       GEN permk = gel(perm, thiscyc[i]);
     245      381964 :       for (j = 1; j <= KC; j++) permk[j] = permk0[ppermk[j]];
     246       18949 :       ppermk = permk;
     247             :     }
     248             :   }
     249      306158 :   for (j = 1; j <= KC; j++)
     250             :   {
     251      264619 :     if (minidx[j]) continue;
     252      127279 :     minidx[j] = j;
     253      355256 :     for (i = 1; i <= nauts; i++) minidx[coeff(perm, j, i)] = j;
     254             :   }
     255       41539 :   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       65800 : subFBgen(FB_t *F, GEN auts, GEN cyclic, double PROD, long minsFB)
     263             : {
     264             :   GEN y, perm, yes, no;
     265       65800 :   long i, j, k, iyes, ino, lv = F->KC + 1;
     266             :   double prod;
     267             :   pari_sp av;
     268             : 
     269       65800 :   F->LP   = cgetg(lv, t_VEC);
     270       65800 :   F->L_jid = F->perm = cgetg(lv, t_VECSMALL);
     271       65800 :   av = avma;
     272       65800 :   y = cgetg(lv,t_COL); /* Norm P */
     273      309806 :   for (k=0, i=1; i <= F->KCZ; i++)
     274             :   {
     275      244005 :     GEN LP = gel(F->LV,F->FB[i]);
     276      244005 :     long l = lg(LP);
     277      705412 :     for (j = 1; j < l; j++)
     278             :     {
     279      461411 :       GEN P = gel(LP,j);
     280      461411 :       k++;
     281      461411 :       gel(y,k) = pr_norm(P);
     282      461407 :       gel(F->LP,k) = P;
     283             :     }
     284             :   }
     285             :   /* perm sorts LP by increasing norm */
     286       65801 :   perm = indexsort(y);
     287       65801 :   no  = cgetg(lv, t_VECSMALL); ino  = 1;
     288       65802 :   yes = cgetg(lv, t_VECSMALL); iyes = 1;
     289       65801 :   prod = 1.0;
     290      301285 :   for (i = 1; i < lv; i++)
     291             :   {
     292      271388 :     long t = perm[i];
     293      271388 :     if (bad_subFB(F, t)) { no[ino++] = t; continue; }
     294             : 
     295      151793 :     yes[iyes++] = t;
     296      151793 :     prod *= (double)itos(gel(y,t));
     297      151794 :     if (iyes > minsFB && prod > PROD) break;
     298             :   }
     299       65802 :   setlg(yes, iyes);
     300      217595 :   for (j=1; j<iyes; j++)     F->perm[j] = yes[j];
     301      185399 :   for (i=1; i<ino; i++, j++) F->perm[j] =  no[i];
     302      255842 :   for (   ; j<lv; j++)       F->perm[j] =  perm[j];
     303       65801 :   F->allsubFB = NULL;
     304       65801 :   F->idealperm = gclone(FB_aut_perm(F, auts, cyclic));
     305       65801 :   if (iyes) assign_subFB(F, yes, iyes);
     306       65801 :   set_avma(av);
     307       65801 : }
     308             : static int
     309        3453 : subFB_change(FB_t *F)
     310             : {
     311        3453 :   long i, iyes, minsFB, lv = F->KC + 1, l = lg(F->subFB)-1;
     312        3453 :   pari_sp av = avma;
     313        3453 :   GEN yes, L_jid = F->L_jid, present = zero_zv(lv-1);
     314             : 
     315        3453 :   switch (F->sfb_chg)
     316             :   {
     317         169 :     case sfb_INCREASE: minsFB = l + 1; break;
     318        3284 :     default: minsFB = l; break;
     319             :   }
     320             : 
     321        3453 :   yes = cgetg(minsFB+1, t_VECSMALL); iyes = 1;
     322        3453 :   if (L_jid)
     323             :   {
     324        7558 :     for (i = 1; i < lg(L_jid); i++)
     325             :     {
     326        7173 :       long l = L_jid[i];
     327        7173 :       yes[iyes++] = l;
     328        7173 :       present[l] = 1;
     329        7173 :       if (iyes > minsFB) break;
     330             :     }
     331             :   }
     332           0 :   else i = 1;
     333        3453 :   if (iyes <= minsFB)
     334             :   {
     335         463 :     for ( ; i < lv; i++)
     336             :     {
     337         463 :       long l = F->perm[i];
     338         463 :       if (present[l]) continue;
     339         463 :       yes[iyes++] = l;
     340         463 :       if (iyes > minsFB) break;
     341             :     }
     342         385 :     if (i == lv) return 0;
     343             :   }
     344        3453 :   if (zv_equal(F->subFB, yes))
     345             :   {
     346        2622 :     if (DEBUGLEVEL) err_printf("\n*** NOT Changing sub factor base\n");
     347             :   }
     348             :   else
     349             :   {
     350         831 :     if (DEBUGLEVEL) err_printf("\n*** Changing sub factor base\n");
     351         831 :     assign_subFB(F, yes, iyes);
     352             :   }
     353        3453 :   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      103773 : pre_allocate(RELCACHE_t *cache, size_t n)
     359             : {
     360      103773 :   size_t len = (cache->last - cache->base) + n;
     361      103773 :   if (len >= cache->len) reallocate(cache, len << 1);
     362      103773 : }
     363             : 
     364             : void
     365      133857 : init_GRHcheck(GRHcheck_t *S, long N, long R1, double LOGD)
     366             : {
     367      133857 :   const double c1 = M_PI*M_PI/2;
     368      133857 :   const double c2 = 3.663862376709;
     369      133857 :   const double c3 = 3.801387092431; /* Euler + log(8*Pi)*/
     370      133857 :   S->clone = 0;
     371      133857 :   S->cN = R1*c2 + N*c1;
     372      133857 :   S->cD = LOGD - N*c3 - R1*M_PI/2;
     373      133857 :   S->maxprimes = 16000; /* sufficient for LIMC=176081*/
     374      133857 :   S->primes = (GRHprime_t*)pari_malloc(S->maxprimes*sizeof(*S->primes));
     375      133859 :   S->nprimes = 0;
     376      133859 :   S->limp = 0;
     377      133859 :   u_forprime_init(&S->P, 2, ULONG_MAX);
     378      133858 : }
     379             : 
     380             : void
     381      133860 : free_GRHcheck(GRHcheck_t *S)
     382             : {
     383      133860 :   if (S->clone)
     384             :   {
     385       63602 :     long i = S->nprimes;
     386             :     GRHprime_t *pr;
     387     7523553 :     for (pr = S->primes, i = S->nprimes; i > 0; pr++, i--) gunclone(pr->dec);
     388             :   }
     389      133858 :   pari_free(S->primes);
     390      133859 : }
     391             : 
     392             : int
     393     1525928 : GRHok(GRHcheck_t *S, double L, double SA, double SB)
     394             : {
     395     1525928 :   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     7456022 : get_fs(GEN nf, GEN P, GEN index, ulong p)
     402             : {
     403             :   long j, k, f, n, l;
     404             :   GEN fs, ns;
     405             : 
     406     7456022 :   if (umodiu(index, p))
     407             :   { /* easy case: p does not divide index */
     408     7417738 :     GEN F = Flx_degfact(ZX_to_Flx(P,p), p);
     409     7418432 :     fs = gel(F,1); l = lg(fs);
     410             :   }
     411             :   else
     412             :   {
     413       37917 :     GEN F = idealprimedec(nf, utoipos(p));
     414       37975 :     l = lg(F);
     415       37975 :     fs = cgetg(l, t_VECSMALL);
     416      119165 :     for (j = 1; j < l; j++) fs[j] = pr_get_f(gel(F,j));
     417             :   }
     418     7456407 :   ns = cgetg(l, t_VECSMALL);
     419     7452983 :   f = fs[1]; n = 1;
     420    13791332 :   for (j = 2, k = 1; j < l; j++)
     421     6338349 :     if (fs[j] == f)
     422     4617692 :       n++;
     423             :     else
     424             :     {
     425     1720657 :       ns[k] = n; fs[k] = f; k++;
     426     1720657 :       f = fs[j]; n = 1;
     427             :     }
     428     7452983 :   ns[k] = n; fs[k] = f; k++;
     429     7452983 :   setlg(fs, k);
     430     7453029 :   setlg(ns, k); return mkvec2(fs,ns);
     431             : }
     432             : 
     433             : /* cache data for all rational primes up to the LIM */
     434             : static void
     435      915095 : cache_prime_dec(GRHcheck_t *S, ulong LIM, GEN nf)
     436             : {
     437      915095 :   pari_sp av = avma;
     438             :   GRHprime_t *pr;
     439             :   GEN index, P;
     440             :   double nb;
     441             : 
     442      915095 :   if (S->limp >= LIM) return;
     443      327534 :   S->clone = 1;
     444      327534 :   nb = primepi_upper_bound((double)LIM); /* #{p <= LIM} <= nb */
     445      327540 :   GRH_ensure(S, nb+1); /* room for one extra prime */
     446      327537 :   P = nf_get_pol(nf);
     447      327534 :   index = nf_get_index(nf);
     448      327533 :   for (pr = S->primes + S->nprimes;;)
     449     7128986 :   {
     450     7456519 :     ulong p = u_forprime_next(&(S->P));
     451     7455680 :     pr->p = p;
     452     7455680 :     pr->logp = log((double)p);
     453     7455680 :     pr->dec = gclone(get_fs(nf, P, index, p));
     454     7456581 :     S->nprimes++;
     455     7456581 :     pr++;
     456     7456581 :     set_avma(av);
     457             :     /* store up to nextprime(LIM) included */
     458     7456521 :     if (p >= LIM) { S->limp = p; break; }
     459             :   }
     460             : }
     461             : 
     462             : static double
     463     2244581 : 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     2244581 :   const double  rQ = 1.83787706641;
     466     2244581 :   const double r1Q = 1.98505372441;
     467     2244581 :   const double r2Q = 1.07991541347;
     468     4489162 :   return fabs((R1+R2-1)*(12*logC3+4*logC2-9*logC-6)/(2*C*logC3)
     469     2244581 :          + (rK-rQ)*(6*logC2 + 5*logC + 2)/(C*logC3)
     470     2244581 :          - R2*(6*logC2+11*logC+6)/(C2*logC2)
     471     2244581 :          - 2*(r1K-r1Q)*(3*logC2 + 4*logC + 2)/(C2*logC3)
     472     2244581 :          + (R1+R2-1)*(12*logC3+40*logC2+45*logC+18)/(6*C3*logC3)
     473     2244581 :          + (r2K-r2Q)*(2*logC2 + 3*logC + 2)/(C3*logC3));
     474             : }
     475             : 
     476             : static double
     477     1122282 : 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     1122282 :   const double logC = log(C), logC2 = logC*logC, logC3 = logC*logC2;
     516     1122282 :   const double C2 = C*C, C3 = C*C2;
     517     1122282 :   double E1 = i >30? 0: tab[i];
     518     1122282 :   return al2K*((33*logC2+22*logC+8)/(8*logC3*sqrt(C))+15*E1/16)
     519     1122282 :     + maxdd(tailresback(rKm,r1KM,r2Km, C,C2,C3,R1,R2,logC,logC2,logC3),
     520     1122289 :             tailresback(rKM,r1Km,r2KM, C,C2,C3,R1,R2,logC,logC2,logC3))/2
     521     1122289 :     + ((R1+R2-1)*4*C+R2)*(C2+6*logC)/(4*C2*C2*logC2);
     522             : }
     523             : 
     524             : static long
     525       63602 : primeneeded(long N, long R1, long R2, double LOGD)
     526             : {
     527       63602 :   const double lim = 0.25; /* should be log(2)/2 == 0.34657... */
     528       63602 :   const double al2K =  0.3526*LOGD - 0.8212*N + 4.5007;
     529       63602 :   const double  rKm = -1.0155*LOGD + 2.1042*N - 8.3419;
     530       63602 :   const double  rKM = -0.5   *LOGD + 1.2076*N + 1;
     531       63602 :   const double r1Km = -       LOGD + 1.4150*N;
     532       63602 :   const double r1KM = -       LOGD + 1.9851*N;
     533       63602 :   const double r2Km = -       LOGD + 0.9151*N;
     534       63602 :   const double r2KM = -       LOGD + 1.0800*N;
     535       63602 :   long Cmin = 3, Cmax = 3, i = 0;
     536      570580 :   while (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, Cmax, i) > lim)
     537             :   {
     538      506978 :     Cmin = Cmax;
     539      506978 :     Cmax *= 2;
     540      506978 :     i++;
     541             :   }
     542       63602 :   i--;
     543      615323 :   while (Cmax - Cmin > 1)
     544             :   {
     545      551720 :     long t = (Cmin + Cmax)/2;
     546      551720 :     if (tailres(R1, R2, al2K, rKm, rKM, r1Km, r1KM, r2Km, r2KM, t, i) > lim)
     547      341772 :       Cmin = t;
     548             :     else
     549      209949 :       Cmax = t;
     550             :   }
     551       63603 :   return Cmax;
     552             : }
     553             : 
     554             : /* ~ 1 / Res(s = 1, zeta_K) */
     555             : static GEN
     556       63601 : compute_invres(GRHcheck_t *S, long LIMC)
     557             : {
     558       63601 :   pari_sp av = avma;
     559       63601 :   double loginvres = 0.;
     560             :   GRHprime_t *pr;
     561             :   long i;
     562       63601 :   double logLIMC = log((double)LIMC);
     563       63601 :   double logLIMC2 = logLIMC*logLIMC, denc;
     564             :   double c0, c1, c2;
     565       63601 :   denc = 1/(pow((double)LIMC, 3.) * logLIMC * logLIMC2);
     566       63601 :   c2 = (    logLIMC2 + 3 * logLIMC / 2 + 1) * denc;
     567       63601 :   denc *= LIMC;
     568       63601 :   c1 = (3 * logLIMC2 + 4 * logLIMC     + 2) * denc;
     569       63601 :   denc *= LIMC;
     570       63601 :   c0 = (3 * logLIMC2 + 5 * logLIMC / 2 + 1) * denc;
     571     7466507 :   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     7458612 :     double logp = pr->logp, NPk;
     577     7458612 :     long j, k, limp = logLIMC/logp;
     578     7458612 :     ulong p = pr->p, p2 = p*p;
     579     7458612 :     if (limp < 1) break;
     580     7402906 :     dec = pr->dec;
     581     7402906 :     fs = gel(dec, 1); ns = gel(dec, 2);
     582     7402906 :     loginvres += 1./p;
     583             :     /* NB: limp = 1 nearly always and limp > 2 for very few primes */
     584     8760390 :     for (k=2, NPk = p; k <= limp; k++) { NPk *= p; loginvres += 1/(k * NPk); }
     585     7402906 :     addpsi = limp;
     586     7402906 :     addpsi1 = p *(pow((double)p , (double)limp)-1)/(p -1);
     587     7402906 :     addpsi2 = p2*(pow((double)p2, (double)limp)-1)/(p2-1);
     588     7402906 :     j = lg(fs);
     589    16516308 :     while (--j > 0)
     590             :     {
     591             :       long f, nb, kmax;
     592             :       double NP, NP2, addinvres;
     593     9113402 :       f = fs[j]; if (f > limp) continue;
     594     3962219 :       nb = ns[j];
     595     3962219 :       NP = pow((double)p, (double)f);
     596     3962219 :       addinvres = 1/NP;
     597     3962219 :       kmax = limp / f;
     598     4835197 :       for (k=2, NPk = NP; k <= kmax; k++) { NPk *= NP; addinvres += 1/(k*NPk); }
     599     3962219 :       NP2 = NP*NP;
     600     3962219 :       loginvres -= nb * addinvres;
     601     3962219 :       addpsi -= nb * f * kmax;
     602     3962219 :       addpsi1 -= nb*(f*NP *(pow(NP ,(double)kmax)-1)/(NP -1));
     603     3962219 :       addpsi2 -= nb*(f*NP2*(pow(NP2,(double)kmax)-1)/(NP2-1));
     604             :     }
     605     7402906 :     loginvres -= (addpsi*c0 - addpsi1*c1 + addpsi2*c2)*logp;
     606             :   }
     607       63601 :   return gerepileuptoleaf(av, mpexp(dbltor(loginvres)));
     608             : }
     609             : 
     610             : static long
     611       63602 : nthideal(GRHcheck_t *S, GEN nf, long n)
     612             : {
     613       63602 :   pari_sp av = avma;
     614       63602 :   GEN P = nf_get_pol(nf);
     615       63602 :   ulong p = 0, *vecN = (ulong*)const_vecsmall(n, LONG_MAX);
     616       63602 :   long i, N = poldegree(P, -1);
     617       63602 :   for (i = 0; ; i++)
     618      228961 :   {
     619             :     GRHprime_t *pr;
     620             :     GEN fs;
     621      292563 :     cache_prime_dec(S, p+1, nf);
     622      292563 :     pr = S->primes + i;
     623      292563 :     fs = gel(pr->dec, 1);
     624      292563 :     p = pr->p;
     625      292563 :     if (fs[1] != N)
     626             :     {
     627      196413 :       GEN ns = gel(pr->dec, 2);
     628      196413 :       long k, l, j = lg(fs);
     629      440454 :       while (--j > 0)
     630             :       {
     631      244041 :         ulong NP = upowuu(p, fs[j]);
     632             :         long nf;
     633      244041 :         if (!NP) continue;
     634      749305 :         for (k = 1; k <= n; k++) if (vecN[k] > NP) break;
     635      243649 :         if (k > n) continue;
     636             :         /* vecN[k] <= NP */
     637      157772 :         nf = ns[j]; /*#{primes of norme NP} = nf, insert them here*/
     638      353085 :         for (l = k+nf; l <= n; l++) vecN[l] = vecN[l-nf];
     639      398479 :         for (l = 0; l < nf && k+l <= n; l++) vecN[k+l] = NP;
     640      362915 :         while (l <= k) vecN[l++] = NP;
     641             :       }
     642             :     }
     643      292563 :     if (p > vecN[n]) break;
     644             :   }
     645       63602 :   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       65800 : ballvol(long n)
     651             : {
     652       65800 :   double v = odd(n)? 2: 1;
     653      150251 :   for (; n > 1; n -= 2) v *= (2 * M_PI) / n;
     654       65800 :   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       65801 : 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       65801 :   const double L = log((double)C2 + 0.5);
     667             : 
     668       65801 :   cache_prime_dec(S, C2, nf);
     669       65802 :   pr = S->primes;
     670       65802 :   F->sfb_chg = 0;
     671       65802 :   F->FB  = cgetg(C2+1, t_VECSMALL);
     672       65801 :   F->iLP = cgetg(C2+1, t_VECSMALL);
     673       65801 :   F->LV = zerovec(C2);
     674             : 
     675       65802 :   prim = icopy(gen_1);
     676       65802 :   i = ip = 0;
     677       65802 :   F->KC = F->KCZ = 0;
     678      432335 :   for (;; pr++) /* p <= C2 */
     679      432335 :   {
     680      498137 :     ulong p = pr->p;
     681             :     long k, l, m;
     682             :     GEN LP, nb, f;
     683             : 
     684      498137 :     if (!F->KC && p > C1) { F->KCZ = i; F->KC = ip; }
     685      498137 :     if (p > C2) break;
     686             : 
     687      461014 :     if (DEBUGLEVEL>1) err_printf(" %ld",p);
     688             : 
     689      461015 :     f = gel(pr->dec, 1); nb = gel(pr->dec, 2);
     690      461015 :     if (f[1] == N)
     691             :     {
     692      144776 :       if (p == C2) break;
     693      136257 :       continue; /* p inert */
     694             :     }
     695      316239 :     l = (long)(L/pr->logp); /* p^f <= C2  <=> f <= l */
     696      576929 :     for (k=0, m=1; m < lg(f) && f[m]<=l; m++) k += nb[m];
     697      316239 :     if (!k)
     698             :     { /* too inert to appear in FB */
     699       72222 :       if (p == C2) break;
     700       71592 :       continue;
     701             :     }
     702      244017 :     prim[2] = p; LP = idealprimedec_limit_f(nf,prim, l);
     703             :     /* keep noninert ideals with Norm <= C2 */
     704      244016 :     if (m == lg(f)) setisclone(LP); /* flag it: all prime divisors in FB */
     705      244016 :     F->FB[++i]= p;
     706      244016 :     gel(F->LV,p) = LP;
     707      244016 :     F->iLP[p] = ip; ip += k;
     708      244016 :     if (p == C2) break;
     709             :   }
     710       65802 :   if (!F->KC) { F->KCZ = i; F->KC = ip; }
     711             :   /* Note F->KC > 0 otherwise GRHchk is false */
     712       65802 :   setlg(F->FB, F->KCZ+1); F->KCZ2 = i;
     713       65800 :   F->prodZ = zv_prod_Z(F->FB);
     714       65798 :   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       65798 :   F->perm = NULL; F->L_jid = NULL;
     726       65798 :   F->ballvol = ballvol(nf_get_degree(nf));
     727       65801 : }
     728             : 
     729             : static int
     730      493141 : GRHchk(GEN nf, GRHcheck_t *S, ulong LIMC)
     731             : {
     732      493141 :   double logC = log((double)LIMC), SA = 0, SB = 0;
     733      493141 :   GRHprime_t *pr = S->primes;
     734             : 
     735      493141 :   cache_prime_dec(S, LIMC, nf);
     736      493140 :   for (pr = S->primes;; pr++)
     737     3031826 :   {
     738     3524966 :     ulong p = pr->p;
     739             :     GEN dec, fs, ns;
     740             :     double logCslogp;
     741             :     long j;
     742             : 
     743     3524966 :     if (p > LIMC) break;
     744     3137441 :     dec = pr->dec; fs = gel(dec, 1); ns = gel(dec,2);
     745     3137441 :     logCslogp = logC/pr->logp;
     746     4938541 :     for (j = 1; j < lg(fs); j++)
     747             :     {
     748     3865530 :       long f = fs[j], M, nb;
     749             :       double logNP, q, A, B;
     750     3865530 :       if (f > logCslogp) break;
     751     1801100 :       logNP = f * pr->logp;
     752     1801100 :       q = 1/sqrt((double)upowuu(p, f));
     753     1801100 :       A = logNP * q; B = logNP * A; M = (long)(logCslogp/f);
     754     1801100 :       if (M > 1)
     755             :       {
     756      374164 :         double inv1_q = 1 / (1-q);
     757      374164 :         A *= (1 - pow(q, (double)M)) * inv1_q;
     758      374164 :         B *= (1 - pow(q, (double)M)*(M+1 - M*q)) * inv1_q * inv1_q;
     759             :       }
     760     1801100 :       nb = ns[j];
     761     1801100 :       SA += nb * A;
     762     1801100 :       SB += nb * B;
     763             :     }
     764     3137441 :     if (p == LIMC) break;
     765             :   }
     766      493140 :   return GRHok(S, logC, SA, SB);
     767             : }
     768             : 
     769             : /*  SMOOTH IDEALS */
     770             : static void
     771     9436586 : store(long i, long e, FACT *fact)
     772             : {
     773     9436586 :   ++fact[0].pr;
     774     9436586 :   fact[fact[0].pr].pr = i; /* index */
     775     9436586 :   fact[fact[0].pr].ex = e; /* exponent */
     776     9436586 : }
     777             : 
     778             : /* divide out x by all P|p, where x as in can_factor().  k = v_p(Nx) */
     779             : static int
     780     5931189 : divide_p_elt(GEN LP, long ip, long k, GEN m, FACT *fact)
     781             : {
     782     5931189 :   long j, l = lg(LP);
     783    18834304 :   for (j=1; j<l; j++)
     784             :   {
     785    18737756 :     GEN P = gel(LP,j);
     786    18737756 :     long v = ZC_nfval(m, P);
     787    18734960 :     if (!v) continue;
     788     8698265 :     store(ip + j, v, fact); /* v = v_P(m) > 0 */
     789     8700549 :     k -= v * pr_get_f(P);
     790     8700830 :     if (!k) return 1;
     791             :   }
     792       96548 :   return 0;
     793             : }
     794             : static int
     795      163065 : divide_p_id(GEN LP, long ip, long k, GEN nf, GEN I, FACT *fact)
     796             : {
     797      163065 :   long j, l = lg(LP);
     798      243835 :   for (j=1; j<l; j++)
     799             :   {
     800      235986 :     GEN P = gel(LP,j);
     801      235986 :     long v = idealval(nf,I, P);
     802      235987 :     if (!v) continue;
     803      158458 :     store(ip + j, v, fact); /* v = v_P(I) > 0 */
     804      158458 :     k -= v * pr_get_f(P);
     805      158458 :     if (!k) return 1;
     806             :   }
     807        7849 :   return 0;
     808             : }
     809             : static int
     810      538303 : divide_p_quo(GEN LP, long ip, long k, GEN nf, GEN I, GEN m, FACT *fact)
     811             : {
     812      538303 :   long j, l = lg(LP);
     813      789000 :   for (j=1; j<l; j++)
     814             :   {
     815      788664 :     GEN P = gel(LP,j);
     816      788664 :     long v = ZC_nfval(m, P);
     817      788664 :     if (!v) continue;
     818      564882 :     v -= idealval(nf,I, P);
     819      564882 :     if (!v) continue;
     820      560984 :     store(ip + j, v, fact); /* v = v_P(m / I) > 0 */
     821      560984 :     k -= v * pr_get_f(P);
     822      560984 :     if (!k) return 1;
     823             :   }
     824         336 :   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    21358150 : Z_issmooth_prod(GEN N, GEN P)
     832             : {
     833    21358150 :   P = gcdii(P,N);
     834   131836414 :   while (!is_pm1(P))
     835             :   {
     836   110484617 :     N = diviiexact(N, P);
     837   110484530 :     P = gcdii(N, P);
     838             :   }
     839    21331226 :   return is_pm1(N);
     840             : }
     841             : 
     842             : static int
     843     6632477 : divide_p(FB_t *F, long p, long k, GEN nf, GEN I, GEN m, FACT *fact)
     844             : {
     845     6632477 :   GEN LP = gel(F->LV,p);
     846     6632477 :   long ip = F->iLP[p];
     847     6632477 :   if (!m) return divide_p_id (LP,ip,k,nf,I,fact);
     848     6469412 :   if (!I) return divide_p_elt(LP,ip,k,m,fact);
     849      538275 :   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    22123622 : 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    22123622 :   fact[0].pr = 0;
     862    22123622 :   if (is_pm1(N)) return 1;
     863    21358189 :   if (!Z_issmooth_prod(N, F->prodZ)) return 0;
     864     2977909 :   f = absZ_factor(N); p = gel(f,1); e = gel(f,2); l = lg(p);
     865     9507936 :   for (i = 1; i < l; i++)
     866     6632409 :     if (!divide_p(F, itou(gel(p,i)), itou(gel(e,i)), nf, I, m, fact))
     867             :     {
     868      103908 :       if (DEBUGLEVEL > 1) err_printf(".");
     869      103908 :       return 0;
     870             :     }
     871     2875527 :   return 1;
     872             : }
     873             : 
     874             : /* can we factor m/I ? [m in I from idealpseudomin_nonscalar], NI = norm I */
     875             : static long
     876     1319809 : factorgen(FB_t *F, GEN nf, GEN I, GEN NI, GEN m, FACT *fact)
     877             : {
     878     1319809 :   long e, r1 = nf_get_r1(nf);
     879     1319809 :   GEN M = nf_get_M(nf);
     880     1319809 :   GEN N = divri(embed_norm(RgM_RgC_mul(M,m), r1), NI); /* ~ N(m/I) */
     881     1319811 :   N = grndtoi(N, &e);
     882     1319809 :   if (e > -32)
     883             :   {
     884           0 :     if (DEBUGLEVEL > 1) err_printf("+");
     885           0 :     return 0;
     886             :   }
     887     1319809 :   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     6562451 : addRe_modIm(GEN x, GEN a, GEN y, GEN iy)
     895             : {
     896             :   GEN z;
     897     6562451 :   if (typ(x) == t_COMPLEX)
     898             :   {
     899     4589179 :     GEN re, im = modRr_i(gel(x,2), y, iy);
     900     4589092 :     if (!im) return NULL;
     901     4589092 :     re = gadd(gel(x,1), a);
     902     4589096 :     z = gequal0(im)? re: mkcomplex(re, im);
     903             :   }
     904             :   else
     905     1973272 :     z = gadd(x, a);
     906     6562392 :   return z;
     907             : }
     908             : static GEN
     909      201171 : modIm(GEN x, GEN y, GEN iy)
     910             : {
     911      201171 :   if (typ(x) == t_COMPLEX)
     912             :   {
     913      187115 :     GEN im = modRr_i(gel(x,2), y, iy);
     914      187106 :     if (!im) return NULL;
     915      187106 :     x = gequal0(im)? gel(x,1): mkcomplex(gel(x,1), im);
     916             :   }
     917      201168 :   return x;
     918             : }
     919             : 
     920             : /* clean archimedean components. ipi = 2^n / pi (n arbitrary); its
     921             :  * exponent may be modified */
     922             : static GEN
     923     2921509 : cleanarch(GEN x, long N, GEN ipi, long prec)
     924             : {
     925             :   long i, l, R1, RU;
     926     2921509 :   GEN s, y = cgetg_copy(x, &l);
     927             : 
     928     2921519 :   if (!ipi) ipi = invr(mppi(prec));
     929     2921519 :   if (typ(x) == t_MAT)
     930             :   {
     931      523110 :     for (i = 1; i < l; i++)
     932      459353 :       if (!(gel(y,i) = cleanarch(gel(x,i), N, ipi, prec))) return NULL;
     933       63757 :     return y;
     934             :   }
     935     2857764 :   RU = l-1; R1 = (RU<<1) - N;
     936     2857764 :   s = gdivgs(RgV_sum(real_i(x)), -N); /* -log |norm(x)| / N */
     937     2857751 :   i = 1;
     938     2857751 :   if (R1)
     939             :   {
     940     2379906 :     GEN pi2 = Pi2n(1, prec);
     941     2379908 :     setexpo(ipi, -3); /* 1/(2pi) */
     942     7309855 :     for (; i <= R1; i++)
     943     4929969 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s, pi2, ipi))) return NULL;
     944             :   }
     945     2857731 :   if (i <= RU)
     946             :   {
     947     1074718 :     GEN pi4 = Pi2n(2, prec), s2 = gmul2n(s, 1);
     948     1074730 :     setexpo(ipi, -4); /* 1/(4pi) */
     949     2707226 :     for (; i <= RU; i++)
     950     1632488 :       if (!(gel(y,i) = addRe_modIm(gel(x,i), s2, pi4, ipi))) return NULL;
     951             :   }
     952     2857751 :   return y;
     953             : }
     954             : GEN
     955      194871 : nf_cxlog_normalize(GEN nf, GEN x, long prec)
     956             : {
     957      194871 :   long N = nf_get_degree(nf);
     958      194871 :   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      132531 : cleanarchunit(GEN x, long N, GEN ipi, long prec)
     965             : {
     966             :   long i, l, R1, RU;
     967      132531 :   GEN y = cgetg_copy(x, &l);
     968             : 
     969      132531 :   if (!ipi) ipi = invr(mppi(prec));
     970      132530 :   if (typ(x) == t_MAT)
     971             :   {
     972      132533 :     for (i = 1; i < l; i++)
     973       68931 :       if (!(gel(y,i) = cleanarchunit(gel(x,i), N, ipi, prec))) return NULL;
     974       63602 :     return y;
     975             :   }
     976       68933 :   if (gexpo(RgV_sum(real_i(x))) > -10) return NULL;
     977       68934 :   RU = l-1; R1 = (RU<<1) - N;
     978       68934 :   i = 1;
     979       68934 :   if (R1)
     980             :   {
     981       54535 :     GEN pi2 = Pi2n(1, prec);
     982       54535 :     setexpo(ipi, -3); /* 1/(2pi) */
     983      185337 :     for (; i <= R1; i++)
     984      130803 :       if (!(gel(y,i) = modIm(gel(x,i), pi2, ipi))) return NULL;
     985             :   }
     986       68933 :   if (i <= RU)
     987             :   {
     988       34349 :     GEN pi4 = Pi2n(2, prec);
     989       34349 :     setexpo(ipi, -4); /* 1/(4pi) */
     990      104723 :     for (; i <= RU; i++)
     991       70371 :       if (!(gel(y,i) = modIm(gel(x,i), pi4, ipi))) return NULL;
     992             :   }
     993       68936 :   return y;
     994             : }
     995             : 
     996             : static GEN
     997         376 : not_given(long reason)
     998             : {
     999         376 :   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         376 :   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     2673267 : expbitprec(GEN x, long *e)
    1016             : {
    1017             :   GEN re, im;
    1018     2673267 :   if (typ(x) != t_COMPLEX) re = x;
    1019             :   else
    1020             :   {
    1021     1682227 :     im = gel(x,2); *e = maxss(*e, expo(im) + 5 - bit_prec(im));
    1022     1682226 :     re = gel(x,1);
    1023             :   }
    1024     2673266 :   return (expo(re) <= 20);
    1025             : 
    1026             : }
    1027             : static long
    1028     1165264 : RgC_expbitprec(GEN x)
    1029             : {
    1030     1165264 :   long l = lg(x), i, e = - (long)HIGHEXPOBIT;
    1031     3637004 :   for (i = 1; i < l; i++)
    1032     2472195 :     if (!expbitprec(gel(x,i), &e)) return LONG_MAX;
    1033     1164809 :   return e;
    1034             : }
    1035             : static long
    1036       48412 : RgM_expbitprec(GEN x)
    1037             : {
    1038       48412 :   long i, j, I, J, e = - (long)HIGHEXPOBIT;
    1039       48412 :   RgM_dimensions(x, &I,&J);
    1040      117285 :   for (j = 1; j <= J; j++)
    1041      269946 :     for (i = 1; i <= I; i++)
    1042      201073 :       if (!expbitprec(gcoeff(x,i,j), &e)) return LONG_MAX;
    1043       48356 :   return e;
    1044             : }
    1045             : 
    1046             : static GEN
    1047        1338 : FlxqX_chinese_unit(GEN X, GEN U, GEN invzk, GEN D, GEN T, ulong p)
    1048             : {
    1049        1338 :   long i, lU = lg(U), lX = lg(X), d = lg(invzk)-1;
    1050        1338 :   GEN M = cgetg(lU, t_MAT);
    1051        1338 :   if (D)
    1052             :   {
    1053        1229 :     D = Flv_inv(D, p);
    1054       66922 :     for (i = 1; i < lX; i++)
    1055       65693 :       if (uel(D, i) != 1)
    1056       54459 :         gel(X,i) = Flx_Fl_mul(gel(X,i), uel(D,i), p);
    1057             :   }
    1058        3756 :   for (i = 1; i < lU; i++)
    1059             :   {
    1060        2418 :     GEN H = FlxqV_factorback(X, gel(U, i), T, p);
    1061        2418 :     gel(M, i) = Flm_Flc_mul(invzk, Flx_to_Flv(H, d), p);
    1062             :   }
    1063        1338 :   return M;
    1064             : }
    1065             : 
    1066             : static GEN
    1067         268 : chinese_unit_slice(GEN A, GEN U, GEN B, GEN D, GEN C, GEN P, GEN *mod)
    1068             : {
    1069         268 :   pari_sp av = avma;
    1070         268 :   long i, n = lg(P)-1, v = varn(C);
    1071             :   GEN H, T;
    1072         268 :   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         268 :   T = ZV_producttree(P);
    1083         268 :   A = ZXC_nv_mod_tree(A, P, T, v);
    1084         268 :   B = ZM_nv_mod_tree(B, P, T);
    1085         268 :   D = D ? ZV_nv_mod_tree(D, P, T): NULL;
    1086         268 :   C = ZX_nv_mod_tree(C, P, T);
    1087             : 
    1088         268 :   H = cgetg(n+1, t_VEC);
    1089        1606 :   for(i=1; i <= n; i++)
    1090             :   {
    1091        1338 :     ulong p = P[i];
    1092        1338 :     GEN a = gel(A,i), b = gel(B,i), c = gel(C,i), d = D ? gel(D,i): NULL;
    1093        1338 :     gel(H,i) = FlxqX_chinese_unit(a, U, b, d, c, p);
    1094             :   }
    1095         268 :   H = nmV_chinese_center_tree_seq(H, P, T, ZV_chinesetree(P, T));
    1096         268 :   *mod = gmael(T, lg(T)-1, 1); return gc_all(av, 2, &H, mod);
    1097             : }
    1098             : 
    1099             : GEN
    1100         268 : chinese_unit_worker(GEN P, GEN A, GEN U, GEN B, GEN D, GEN C)
    1101             : {
    1102         268 :   GEN V = cgetg(3, t_VEC);
    1103         268 :   gel(V,1) = chinese_unit_slice(A, U, B, isintzero(D) ? NULL: D, C, P, &gel(V,2));
    1104         268 :   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       16138 :   for (j = k = 1; j < l; j++)
    1130       15974 :     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     1234196 : fixarch(GEN x, GEN s, long R1)
    1142             : {
    1143             :   long i, l;
    1144     1234196 :   GEN y = cgetg_copy(x, &l);
    1145     3407425 :   for (i = 1; i <= R1; i++) gel(y,i) = gadd(s, gel(x,i));
    1146     1734855 :   for (     ; i <   l; i++) gel(y,i) = gadd(s, gmul2n(gel(x,i),-1));
    1147     1234199 :   return y;
    1148             : }
    1149             : 
    1150             : static GEN
    1151       63601 : getfu(GEN nf, GEN *ptA, GEN *ptU, long prec)
    1152             : {
    1153       63601 :   GEN U, y, matep, A, T = nf_get_pol(nf), M = nf_get_M(nf);
    1154       63602 :   long e, j, R1, RU, N = degpol(T);
    1155             : 
    1156       63602 :   R1 = nf_get_r1(nf); RU = (N+R1) >> 1;
    1157       63601 :   if (RU == 1) return cgetg(1,t_VEC);
    1158             : 
    1159       48411 :   A = *ptA;
    1160       48411 :   matep = cgetg(RU,t_MAT);
    1161      117346 :   for (j = 1; j < RU; j++)
    1162             :   {
    1163       68935 :     GEN Aj = gel(A,j), s = gdivgs(RgV_sum(real_i(Aj)), -N);
    1164       68936 :     gel(matep,j) = fixarch(Aj, s, R1);
    1165             :   }
    1166       48411 :   U = lll(real_i(matep));
    1167       48412 :   if (lg(U) < RU) return not_given(fupb_PRECI);
    1168       48412 :   if (ptU) { *ptU = U; *ptA = A = RgM_ZM_mul(A,U); }
    1169       48412 :   y = RgM_ZM_mul(matep,U);
    1170       48412 :   e = RgM_expbitprec(y);
    1171       48411 :   if (e >= 0) return not_given(e == LONG_MAX? fupb_LARGE: fupb_PRECI);
    1172       48355 :   if (prec <= 0) prec = gprecision(A);
    1173       48355 :   y = RgM_solve_realimag(M, gexp(y,prec));
    1174       48355 :   if (!y) return not_given(fupb_PRECI);
    1175       48355 :   y = grndtoi(y, &e); if (e >= 0) return not_given(fupb_PRECI);
    1176       48044 :   settyp(y, t_VEC);
    1177             : 
    1178       48044 :   if (!ptU) *ptA = A = RgM_ZM_mul(A, U);
    1179      116259 :   for (j = 1; j < RU; j++)
    1180             :   { /* y[i] are hopefully unit generators. Normalize: smallest T2 norm */
    1181       68223 :     GEN u = gel(y,j), v = zk_inv(nf, u);
    1182       68224 :     if (!v || !is_pm1(Q_denom(v)) || ZV_isscalar(u))
    1183           9 :       return not_given(fupb_PRECI);
    1184       68214 :     if (gcmp(RgC_fpnorml2(v,DEFAULTPREC), RgC_fpnorml2(u,DEFAULTPREC)) < 0)
    1185             :     {
    1186       27946 :       gel(A,j) = RgC_neg(gel(A,j));
    1187       27946 :       if (ptU) gel(U,j) = ZC_neg(gel(U,j));
    1188       27945 :       u = v;
    1189             :     }
    1190       68214 :     gel(y,j) = nf_to_scalar_or_alg(nf, u);
    1191             :   }
    1192       48036 :   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        5249 :   for (j = 1; j < l; j++)
    1247             :   {
    1248        5155 :     GEN t = nf_to_scalar_or_alg(nf, gel(Y,j));
    1249        5155 :     if (f)
    1250             :     {
    1251             :       GEN den;
    1252        4277 :       t = Q_remove_denom(t, &den);
    1253        4277 :       gel(dX,j) = den ? den: gen_1;
    1254             :     }
    1255        5155 :     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       24893 :   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          77 : get_norm_fact_primes(GEN G, GEN E, GEN C)
    1282             : {
    1283          77 :   pari_sp av=avma;
    1284          77 :   GEN N = gen_1, P, p;
    1285          77 :   long i, c = lg(E);
    1286          77 :   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          77 :   if (C) N = mulii(N, pr_norm(C));
    1296          77 :   return gerepileuptoint(av, N);
    1297             : }
    1298             : 
    1299             : /* gen: HNF ideals */
    1300             : static GEN
    1301     1159626 : get_norm_fact(GEN gen, GEN ex, GEN *pd)
    1302             : {
    1303     1159626 :   long i, c = lg(ex);
    1304             :   GEN d,N,I,e,n,ne,de;
    1305     1159626 :   d = N = gen_1;
    1306     1460632 :   for (i=1; i<c; i++)
    1307      301006 :     if (signe(gel(ex,i)))
    1308             :     {
    1309      179625 :       I = gel(gen,i); e = gel(ex,i); n = ZM_det_triangular(I);
    1310      179625 :       ne = powii(n,e);
    1311      179625 :       de = equalii(n, gcoeff(I,1,1))? ne: powii(gcoeff(I,1,1), e);
    1312      179625 :       N = mulii(N, ne);
    1313      179625 :       d = mulii(d, de);
    1314             :     }
    1315     1159626 :   *pd = d; return N;
    1316             : }
    1317             : 
    1318             : static GEN
    1319     1320386 : get_pr_lists(GEN FB, long N, int list_pr)
    1320             : {
    1321             :   GEN pr, L;
    1322     1320386 :   long i, l = lg(FB), p, pmax;
    1323             : 
    1324     1320386 :   pmax = 0;
    1325     9184361 :   for (i=1; i<l; i++)
    1326             :   {
    1327     7863975 :     pr = gel(FB,i); p = pr_get_smallp(pr);
    1328     7863975 :     if (p > pmax) pmax = p;
    1329             :   }
    1330     1320386 :   L = const_vec(pmax, NULL);
    1331     1320390 :   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     9184374 :     for (i=1; i<l; i++)
    1346             :     {
    1347     7863984 :       pr = gel(FB,i); p = pr_get_smallp(pr);
    1348     7863984 :       if (!L[p]) gel(L,p) = vecsmalltrunc_init(N+1);
    1349     7863984 :       vecsmalltrunc_append(gel(L,p), i);
    1350             :     }
    1351             :   }
    1352     1320390 :   return L;
    1353             : }
    1354             : 
    1355             : /* recover FB, LV, iLP, KCZ from Vbase */
    1356             : static GEN
    1357     1320386 : recover_partFB(FB_t *F, GEN Vbase, long N)
    1358             : {
    1359     1320386 :   GEN FB, LV, iLP, L = get_pr_lists(Vbase, N, 0);
    1360     1320391 :   long l = lg(L), p, ip, i;
    1361             : 
    1362     1320391 :   i = ip = 0;
    1363     1320391 :   FB = cgetg(l, t_VECSMALL);
    1364     1320391 :   iLP= cgetg(l, t_VECSMALL);
    1365     1320390 :   LV = cgetg(l, t_VEC);
    1366    19937534 :   for (p = 2; p < l; p++)
    1367             :   {
    1368    18617141 :     if (!L[p]) continue;
    1369     4306761 :     FB[++i] = p;
    1370     4306761 :     gel(LV,p) = vecpermute(Vbase, gel(L,p));
    1371     4306762 :     iLP[p]= ip; ip += lg(gel(L,p))-1;
    1372             :   }
    1373     1320393 :   F->KCZ = i;
    1374     1320393 :   F->KC = ip;
    1375     1320393 :   F->FB = FB; setlg(FB, i+1);
    1376     1320393 :   F->prodZ = zv_prod_Z(F->FB);
    1377     1320388 :   F->LV = LV;
    1378     1320388 :   F->iLP= iLP; return L;
    1379             : }
    1380             : 
    1381             : /* add v^e to factorization */
    1382             : static void
    1383       19823 : add_to_fact(long v, long e, FACT *fact)
    1384             : {
    1385       19823 :   long i, l = fact[0].pr;
    1386       27281 :   for (i=1; i<=l && fact[i].pr < v; i++)/*empty*/;
    1387       19823 :   if (i <= l && fact[i].pr == v) fact[i].ex += e; else store(v, e, fact);
    1388       19823 : }
    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        3211 : pr_index(GEN L, GEN pr)
    1399             : {
    1400        3211 :   long j, l = lg(L);
    1401        3211 :   GEN al = pr_get_gen(pr);
    1402        3211 :   for (j=1; j<l; j++)
    1403        3211 :     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        3211 : Vbase_to_FB(FB_t *F, GEN pr)
    1410             : {
    1411        3211 :   long p = pr_get_smallp(pr);
    1412        3211 :   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        3454 : idealHNF_mulred(GEN nf, GEN x, GEN y)
    1419             : {
    1420        3454 :   GEN A = idealHNF_mul(nf, gel(x,1), gel(y,1));
    1421        3454 :   GEN F = famat_mul_shallow(gel(x,2), gel(y,2));
    1422        3454 :   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        5151 : idealmulpowprime2(GEN nf, GEN x, GEN pr, ulong n)
    1428             : {
    1429        5151 :   GEN A = idealmulpowprime(nf, gel(x,1), pr, utoipos(n));
    1430        5151 :   return mkvec2(A, gel(x,2));
    1431             : }
    1432             : static GEN
    1433       65271 : init_famat(GEN x) { return mkvec2(x, trivial_fact()); }
    1434             : /* optimized idealfactorback + reduction; z = init_famat() */
    1435             : static GEN
    1436       28714 : genback(GEN z, GEN nf, GEN P, GEN E)
    1437             : {
    1438       28714 :   long i, l = lg(E);
    1439       28714 :   GEN I = NULL;
    1440       76436 :   for (i = 1; i < l; i++)
    1441       47722 :     if (signe(gel(E,i)))
    1442             :     {
    1443             :       GEN J;
    1444       32168 :       gel(z,1) = gel(P,i);
    1445       32168 :       J = idealpowred(nf, z, gel(E,i));
    1446       32168 :       I = I? idealHNF_mulred(nf, I, J): J;
    1447             :     }
    1448       28714 :   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     1336734 : SPLIT(FB_t *F, GEN nf, GEN x, GEN Vbase, FACT *fact)
    1454             : {
    1455     1336734 :   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     2673262 :   if (gexpo(gcoeff(x,1,1)) < 100 &&
    1461     1486612 :       can_factor(F, nf, x, NULL, Nx, fact)) return NULL;
    1462             : 
    1463     1186649 :   av = avma;
    1464     1186649 :   Ly = idealpseudominvec(x, nf_get_roundG(nf));
    1465     1232434 :   for(k=1; k<lg(Ly); k++)
    1466             :   {
    1467     1223618 :     y = gel(Ly,k);
    1468     1223618 :     if (factorgen(F, nf, x, Nx, y, fact)) return y;
    1469             :   }
    1470        8816 :   set_avma(av);
    1471             : 
    1472             :   /* reduce in various directions */
    1473        8816 :   ru = lg(nf_get_roots(nf));
    1474        8816 :   vecG = cgetg(ru, t_VEC);
    1475       14145 :   for (j=1; j<ru; j++)
    1476             :   {
    1477       12476 :     gel(vecG,j) = nf_get_Gtwist1(nf, j);
    1478       12476 :     av = avma;
    1479       12476 :     Ly = idealpseudominvec(x, gel(vecG,j));
    1480       41619 :     for(k=1; k<lg(Ly); k++)
    1481             :     {
    1482       36290 :       y = gel(Ly,k);
    1483       36290 :       if (factorgen(F, nf, x, Nx, y, fact)) return y;
    1484             :     }
    1485        5329 :     set_avma(av);
    1486             :   }
    1487             : 
    1488             :   /* tough case, multiply by random products */
    1489        1669 :   lgsub = 3;
    1490        1669 :   ex = cgetg(lgsub, t_VECSMALL);
    1491        1669 :   x0 = init_famat(x);
    1492        1669 :   nbtest = 1; nbtest_lim = 4;
    1493             :   for(;;)
    1494         760 :   {
    1495        2429 :     GEN Ired, I, NI, id = x0;
    1496        2429 :     av = avma;
    1497        2429 :     if (DEBUGLEVEL>2) err_printf("# ideals tried = %ld\n",nbtest);
    1498        7927 :     for (i=1; i<lgsub; i++)
    1499             :     {
    1500        5498 :       ex[i] = random_bits(RANDOM_BITS);
    1501        5498 :       if (ex[i]) id = idealmulpowprime2(nf, id, gel(Vbase,i), ex[i]);
    1502             :     }
    1503        2429 :     if (id == x0) continue;
    1504             :     /* I^(-1) * \prod Vbase[i]^ex[i] = (id[2]) / x */
    1505             : 
    1506        2429 :     I = gel(id,1); NI = ZM_det_triangular(I);
    1507        2429 :     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        2429 :     Ired = ru == 2? I: ZM_lll(I, 0.99, LLL_INPLACE);
    1515        4252 :     for (j=1; j<ru; j++)
    1516             :     {
    1517        3492 :       pari_sp av2 = avma;
    1518        3492 :       Ly = idealpseudominvec(Ired, gel(vecG,j));
    1519       17742 :       for (k=1; k < lg(Ly); k++)
    1520             :       {
    1521       15919 :         y = gel(Ly,k);
    1522       15919 :         if (factorgen(F, nf, I, NI, y, fact))
    1523             :         {
    1524        5041 :           for (i=1; i<lgsub; i++)
    1525        3372 :             if (ex[i]) add_to_fact(Vbase_to_FB(F,gel(Vbase,i)), ex[i], fact);
    1526        1669 :           return famat_mul_shallow(gel(id,2), y);
    1527             :         }
    1528             :       }
    1529        1823 :       set_avma(av2);
    1530             :     }
    1531         760 :     set_avma(av);
    1532         760 :     if (++nbtest > nbtest_lim)
    1533             :     {
    1534          34 :       nbtest = 0;
    1535          34 :       if (++lgsub < minss(8, lg(Vbase)-1))
    1536             :       {
    1537          34 :         nbtest_lim <<= 1;
    1538          34 :         ex = cgetg(lgsub, t_VECSMALL);
    1539             :       }
    1540           0 :       else nbtest_lim = LONG_MAX; /* don't increase further */
    1541          34 :       if (DEBUGLEVEL>2) err_printf("SPLIT: increasing factor base [%ld]\n",lgsub);
    1542             :     }
    1543             :   }
    1544             : }
    1545             : 
    1546             : INLINE GEN
    1547     1325311 : bnf_get_W(GEN bnf) { return gel(bnf,1); }
    1548             : INLINE GEN
    1549     2640667 : bnf_get_B(GEN bnf) { return gel(bnf,2); }
    1550             : INLINE GEN
    1551     2675202 : bnf_get_C(GEN bnf) { return gel(bnf,4); }
    1552             : INLINE GEN
    1553     1320405 : bnf_get_vbase(GEN bnf) { return gel(bnf,5); }
    1554             : INLINE GEN
    1555     1320328 : bnf_get_Ur(GEN bnf) { return gmael(bnf,9,1); }
    1556             : INLINE GEN
    1557      277036 : bnf_get_ga(GEN bnf) { return gmael(bnf,9,2); }
    1558             : INLINE GEN
    1559      282005 : 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     1320321 : split_ideal(GEN bnf, GEN x, GEN *pWex, GEN *pBex)
    1566             : {
    1567     1320321 :   GEN L, y, Vbase = bnf_get_vbase(bnf);
    1568     1320321 :   GEN Wex, W  = bnf_get_W(bnf);
    1569     1320321 :   GEN Bex, B  = bnf_get_B(bnf);
    1570             :   long p, j, i, l, nW, nB;
    1571             :   FACT *fact;
    1572             :   FB_t F;
    1573             : 
    1574     1320322 :   L = recover_partFB(&F, Vbase, lg(x)-1);
    1575     1320325 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    1576     1320326 :   y = SPLIT(&F, bnf_get_nf(bnf), x, Vbase, fact);
    1577     1320328 :   nW = lg(W)-1; *pWex = Wex = zero_zv(nW);
    1578     1320328 :   nB = lg(B)-1; *pBex = Bex = zero_zv(nB); l = lg(F.FB);
    1579     1320328 :   p = j = 0; /* -Wall */
    1580     1978116 :   for (i = 1; i <= fact[0].pr; i++)
    1581             :   { /* decode index C = ip+j --> (p,j) */
    1582      657788 :     long a, b, t, C = fact[i].pr;
    1583     1881863 :     for (t = 1; t < l; t++)
    1584             :     {
    1585     1807671 :       long q = F.FB[t], k = C - F.iLP[q];
    1586     1807671 :       if (k <= 0) break;
    1587     1224075 :       p = q;
    1588     1224075 :       j = k;
    1589             :     }
    1590      657788 :     a = gel(L, p)[j];
    1591      657788 :     b = a - nW;
    1592      657788 :     if (b <= 0) Wex[a] = y? -fact[i].ex: fact[i].ex;
    1593      502292 :     else        Bex[b] = y? -fact[i].ex: fact[i].ex;
    1594             :   }
    1595     1320328 :   return y;
    1596             : }
    1597             : 
    1598             : GEN
    1599     1038194 : init_red_mod_units(GEN bnf, long prec)
    1600             : {
    1601     1038194 :   GEN s = gen_0, p1,s1,mat, logfu = bnf_get_logfu(bnf);
    1602     1038194 :   long i,j, RU = lg(logfu);
    1603             : 
    1604     1038194 :   if (RU == 1) return NULL;
    1605     1038194 :   mat = cgetg(RU,t_MAT);
    1606     2345650 :   for (j=1; j<RU; j++)
    1607             :   {
    1608     1307456 :     p1 = cgetg(RU+1,t_COL); gel(mat,j) = p1;
    1609     1307456 :     s1 = gen_0;
    1610     3223078 :     for (i=1; i<RU; i++)
    1611             :     {
    1612     1915622 :       gel(p1,i) = real_i(gcoeff(logfu,i,j));
    1613     1915622 :       s1 = mpadd(s1, mpsqr(gel(p1,i)));
    1614             :     }
    1615     1307456 :     gel(p1,RU) = gen_0; if (mpcmp(s1,s) > 0) s = s1;
    1616             :   }
    1617     1038194 :   s = gsqrt(gmul2n(s,RU),prec);
    1618     1038194 :   if (expo(s) < 27) s = utoipos(1UL << 27);
    1619     1038194 :   return mkvec2(mat, s);
    1620             : }
    1621             : 
    1622             : /* z computed above. Return unit exponents that would reduce col (arch) */
    1623             : GEN
    1624     1038194 : red_mod_units(GEN col, GEN z)
    1625             : {
    1626             :   long i,RU;
    1627             :   GEN x,mat,N2;
    1628             : 
    1629     1038194 :   if (!z) return NULL;
    1630     1038194 :   mat= gel(z,1);
    1631     1038194 :   N2 = gel(z,2);
    1632     1038194 :   RU = lg(mat); x = cgetg(RU+1,t_COL);
    1633     2345650 :   for (i=1; i<RU; i++) gel(x,i) = real_i(gel(col,i));
    1634     1038194 :   gel(x,RU) = N2;
    1635     1038194 :   x = lll(shallowconcat(mat,x));
    1636     1038194 :   if (typ(x) != t_MAT || lg(x) <= RU) return NULL;
    1637     1038194 :   x = gel(x,RU);
    1638     1038194 :   if (signe(gel(x,RU)) < 0) x = gneg_i(x);
    1639     1038194 :   if (!gequal1(gel(x,RU))) pari_err_BUG("red_mod_units");
    1640     1038194 :   setlg(x,RU); return x;
    1641             : }
    1642             : 
    1643             : static GEN
    1644     2147688 : 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     1994502 : act_arch(GEN A, GEN x)
    1649             : {
    1650             :   GEN a;
    1651     1994502 :   long i,l = lg(A), tA = typ(A);
    1652     1994502 :   if (tA == t_MAT)
    1653             :   { /* assume lg(x) >= l */
    1654      191098 :     a = cgetg(l, t_MAT);
    1655      280802 :     for (i=1; i<l; i++) gel(a,i) = act_arch(gel(A,i), x);
    1656      191097 :     return a;
    1657             :   }
    1658     1803404 :   if (l==1) return cgetg(1, t_COL);
    1659     1803404 :   a = NULL;
    1660     1803404 :   if (tA == t_VECSMALL)
    1661             :   {
    1662     6831293 :     for (i=1; i<l; i++)
    1663             :     {
    1664     5671666 :       long c = A[i];
    1665     5671666 :       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     1402567 :     for (i=1; i<l; i++)
    1671             :     {
    1672      758795 :       GEN c = gel(A,i);
    1673      758795 :       if (signe(c)) a = add(a, gmul(c, gel(x,i)));
    1674             :     }
    1675             :   }
    1676     1803399 :   return a? a: zerocol(lgcols(x)-1);
    1677             : }
    1678             : /* act_arch(matdiagonal(v), x) */
    1679             : static GEN
    1680       63699 : diagact_arch(GEN v, GEN x)
    1681             : {
    1682       63699 :   long i, l = lg(v);
    1683       63699 :   GEN a = cgetg(l, t_MAT);
    1684       92482 :   for (i = 1; i < l; i++) gel(a,i) = gmul(gel(x,i), gel(v,i));
    1685       63699 :   return a;
    1686             : }
    1687             : 
    1688             : static long
    1689     1338522 : prec_arch(GEN bnf)
    1690             : {
    1691     1338522 :   GEN a = bnf_get_C(bnf);
    1692     1338521 :   long i, l = lg(a), prec;
    1693             : 
    1694     1338521 :   for (i=1; i<l; i++)
    1695     1338437 :     if ( (prec = gprecision(gel(a,i))) ) return prec;
    1696          84 :   return DEFAULTPREC;
    1697             : }
    1698             : 
    1699             : static long
    1700        3740 : needed_bitprec(GEN x)
    1701             : {
    1702        3740 :   long i, e = 0, l = lg(x);
    1703       21276 :   for (i = 1; i < l; i++)
    1704             :   {
    1705       17536 :     GEN c = gel(x,i);
    1706       17536 :     long f = gexpo(c) - prec2nbits(gprecision(c));
    1707       17536 :     if (f > e) e = f;
    1708             :   }
    1709        3740 :   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     1165259 : isprincipalarch(GEN bnf, GEN col, GEN kNx, GEN e, GEN dx, long *pe)
    1716             : {
    1717             :   GEN nf, x, y, logfu, s, M;
    1718     1165259 :   long N, prec = gprecision(col);
    1719     1165260 :   bnf = checkbnf(bnf); nf = bnf_get_nf(bnf); M = nf_get_M(nf);
    1720     1165260 :   if (!prec) prec = prec_arch(bnf);
    1721     1165260 :   *pe = 128;
    1722     1165260 :   logfu = bnf_get_logfu(bnf);
    1723     1165260 :   N = nf_get_degree(nf);
    1724     1165260 :   if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
    1725     1165259 :   if (lg(col) > 2)
    1726             :   { /* reduce mod units */
    1727     1038194 :     GEN u, z = init_red_mod_units(bnf,prec);
    1728     1038194 :     if (!(u = red_mod_units(col,z))) return NULL;
    1729     1038194 :     col = RgC_add(col, RgM_RgC_mul(logfu, u));
    1730     1038194 :     if (!(col = cleanarch(col,N,NULL,prec))) return NULL;
    1731             :   }
    1732     1165259 :   s = divru(mulir(e, glog(kNx,prec)), N);
    1733     1165260 :   col = fixarch(col, s, nf_get_r1(nf));
    1734     1165265 :   if (RgC_expbitprec(col) >= 0) return NULL;
    1735     1164809 :   col = gexp(col, prec);
    1736             :   /* d.alpha such that x = alpha \prod gj^ej */
    1737     1164810 :   x = RgM_solve_realimag(M,col); if (!x) return NULL;
    1738     1164806 :   x = RgC_Rg_mul(x, dx);
    1739     1164806 :   y = grndtoi(x, pe);
    1740     1164805 :   if (*pe > -5) { *pe = needed_bitprec(x); return NULL; }
    1741     1161065 :   return RgC_Rg_div(y, dx);
    1742             : }
    1743             : 
    1744             : /* y = C \prod g[i]^e[i] ? */
    1745             : static int
    1746     1155002 : fact_ok(GEN nf, GEN y, GEN C, GEN g, GEN e)
    1747             : {
    1748     1155002 :   pari_sp av = avma;
    1749     1155002 :   long i, c = lg(e);
    1750     1155002 :   GEN z = C? C: gen_1;
    1751     1435057 :   for (i=1; i<c; i++)
    1752      280055 :     if (signe(gel(e,i))) z = idealmul(nf, z, idealpow(nf, gel(g,i), gel(e,i)));
    1753     1155002 :   if (typ(z) != t_MAT) z = idealhnf_shallow(nf,z);
    1754     1155003 :   if (typ(y) != t_MAT) y = idealhnf_shallow(nf,y);
    1755     1155003 :   return gc_bool(av, ZM_equal(y,z));
    1756             : }
    1757             : static GEN
    1758     1320327 : ZV_divrem(GEN A, GEN B, GEN *pR)
    1759             : {
    1760     1320327 :   long i, l = lg(A);
    1761     1320327 :   GEN Q = cgetg(l, t_COL), R = cgetg(l, t_COL);
    1762     1830728 :   for (i = 1; i < l; i++) gel(Q,i) = truedvmdii(gel(A,i), gel(B,i), &gel(R,i));
    1763     1320327 :   *pR = R; return Q;
    1764             : }
    1765             : 
    1766             : static GEN
    1767     1320328 : Ur_ZC_mul(GEN bnf, GEN v)
    1768             : {
    1769     1320328 :   GEN w, U = bnf_get_Ur(bnf);
    1770     1320328 :   long i, l = lg(bnf_get_cyc(bnf)); /* may be < lgcols(U) */
    1771             : 
    1772     1320328 :   w = cgetg(l, t_COL);
    1773     1830729 :   for (i = 1; i < l; i++) gel(w,i) = ZMrow_ZC_mul(U, v, i);
    1774     1320328 :   return w;
    1775             : }
    1776             : 
    1777             : static GEN
    1778        9043 : ZV_mul(GEN x, GEN y)
    1779             : {
    1780        9043 :   long i, l = lg(x);
    1781        9043 :   GEN z = cgetg(l, t_COL);
    1782       34850 :   for (i = 1; i < l; i++) gel(z,i) = mulii(gel(x,i), gel(y,i));
    1783        9043 :   return z;
    1784             : }
    1785             : static int
    1786     1156425 : dump_gen(GEN SUnits, GEN x, long flag)
    1787             : {
    1788             :   GEN d;
    1789             :   long e;
    1790     1156425 :   if (!(flag & nf_GENMAT) || !SUnits) return 0;
    1791      274444 :   e = gexpo(gel(SUnits,2)); if (e > 64) return 0; /* U large */
    1792      274347 :   x = Q_remove_denom(x, &d);
    1793      274346 :   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     1336660 : isprincipalall(GEN bnf, GEN x, long *pprec, long flag)
    1800             : {
    1801             :   GEN xar, Wex, Bex, gen, xc, col, A, Q, R, UA, SUnits;
    1802     1336660 :   GEN C = bnf_get_C(bnf), nf = bnf_get_nf(bnf), cyc = bnf_get_cyc(bnf);
    1803             :   long nB, nW, e;
    1804             : 
    1805     1336660 :   if (lg(cyc) == 1 && !(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL)))
    1806        4725 :     return cgetg(1,t_COL);
    1807     1331935 :   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     1331851 :   x = Q_primitive_part(x, &xc);
    1815     1331846 :   if (equali1(gcoeff(x,1,1))) /* trivial ideal */
    1816             :   {
    1817       11522 :     R = zerocol(lg(cyc)-1);
    1818       11521 :     if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1819       11472 :     if (flag & nf_GEN_IF_PRINCIPAL)
    1820        6453 :       return scalarcol_shallow(xc? xc: gen_1, nf_get_degree(nf));
    1821        5019 :     if (flag & nf_GENMAT)
    1822        2184 :       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        5019 :     return mkvec2(R, col);
    1826             :   }
    1827     1320321 :   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     1320328 :   A = zc_to_ZC(Wex); nB = lg(Bex)-1;
    1830     1320328 :   if (nB) A = ZC_sub(A, ZM_zc_mul(bnf_get_B(bnf), Bex));
    1831     1320329 :   UA = Ur_ZC_mul(bnf, A);
    1832     1320327 :   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     1320327 :   if (!(flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL))) return R;
    1836     1160217 :   if ((flag & nf_GEN_IF_PRINCIPAL) && !ZV_equal0(R)) return gen_0;
    1837             : 
    1838     1160210 :   nW = lg(Wex)-1;
    1839     1160210 :   gen = bnf_get_gen(bnf);
    1840     1160209 :   col = NULL;
    1841     1160209 :   SUnits = bnf_get_sunits(bnf);
    1842     1160208 :   if (lg(R) == 1
    1843      277618 :       || abscmpiu(gel(R,vecindexmax(R)), 4 * bit_accuracy(*pprec)) < 0)
    1844             :   { /* q = N (x / prod gj^ej) = N(alpha), denom(alpha) | d */
    1845     1159626 :     GEN d, q = gdiv(ZM_det_triangular(x), get_norm_fact(gen, R, &d));
    1846     1159628 :     col = xar? nf_cxlog(nf, xar, *pprec): NULL;
    1847     1159631 :     if (nB) col = add(col, act_arch(Bex, nW? vecslice(C,nW+1,lg(C)-1): C));
    1848     1159626 :     if (nW) col = add(col, RgC_sub(act_arch(Q, bnf_get_GD(bnf)),
    1849             :                                    act_arch(A, bnf_get_ga(bnf))));
    1850     1159626 :     col = isprincipalarch(bnf, col, q, gen_1, d, &e);
    1851     1159626 :     if (col && (dump_gen(SUnits, col, flag)
    1852     1156429 :                 || !fact_ok(nf,x, col,gen,R))) col = NULL;
    1853             :   }
    1854     1160211 :   if (!col && (flag & nf_GENMAT))
    1855             :   {
    1856        9764 :     if (SUnits)
    1857             :     {
    1858        9281 :       GEN X = gel(SUnits,1), U = gel(SUnits,2), C = gel(SUnits,3);
    1859        9281 :       GEN v = gel(bnf,9), Ge = gel(v,4), M1 = gel(v,5), M2 = gel(v,6);
    1860        9281 :       GEN z = NULL, F = NULL;
    1861        9281 :       if (nB)
    1862             :       {
    1863        9281 :         GEN C2 = nW? vecslice(C, nW+1, lg(C)-1): C;
    1864        9281 :         z = ZM_zc_mul(C2, Bex);
    1865             :       }
    1866        9281 :       if (nW)
    1867             :       { /* [GD]Q - [ga]A = ([X]M1 - [Ge]D) Q - ([X]M2 - [Ge]Ur) A */
    1868        9043 :         GEN C1 = vecslice(C, 1, nW);
    1869        9043 :         GEN v = ZC_sub(ZM_ZC_mul(M1,Q), ZM_ZC_mul(M2,A));
    1870        9043 :         z = add(z, ZM_ZC_mul(C1, v));
    1871        9043 :         F = famat_reduce(famatV_factorback(Ge, ZC_sub(UA, ZV_mul(cyc,Q))));
    1872        9043 :         if (lgcols(F) == 1) F = NULL;
    1873             :       }
    1874             :       /* reduce modulo units and Q^* */
    1875        9281 :       if (lg(U) != 1) z = ZC_sub(z, ZM_ZC_mul(U, RgM_Babai(U,z)));
    1876        9281 :       col = mkmat2(X, z);
    1877        9281 :       if (F) col = famat_mul_shallow(col, F);
    1878        9281 :       col = famat_remove_trivial(col);
    1879        9281 :       if (xar) col = famat_mul_shallow(col, xar);
    1880             :     }
    1881         483 :     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     1160211 :   if (col)
    1889             :   { /* add back missing content */
    1890     1160660 :     if (xc) col = (typ(col)==t_MAT)? famat_mul_shallow(col,xc)
    1891         540 :                                    : RgC_Rg_mul(col,xc);
    1892     1160120 :     if (typ(col) != t_MAT && lg(col) != 1 && (flag & nf_GENMAT))
    1893     1136664 :       col = to_famat_shallow(col, gen_1);
    1894             :   }
    1895             :   else
    1896             :   {
    1897          91 :     if (e < 0) e = 0;
    1898          91 :     *pprec += nbits2extraprec(e + 128);
    1899          91 :     if (flag & nf_FORCE)
    1900             :     {
    1901          77 :       if (DEBUGLEVEL)
    1902           0 :         pari_warn(warner,"precision too low for generators, e = %ld",e);
    1903          77 :       return NULL;
    1904             :     }
    1905          14 :     pari_warn(warner,"precision too low for generators, not given");
    1906          14 :     col = cgetg(1, t_COL);
    1907             :   }
    1908     1160135 :   return (flag & nf_GEN_IF_PRINCIPAL)? col: mkvec2(R, col);
    1909             : }
    1910             : 
    1911             : static GEN
    1912      460942 : triv_gen(GEN bnf, GEN x, long flag)
    1913             : {
    1914      460942 :   pari_sp av = avma;
    1915      460942 :   GEN nf = bnf_get_nf(bnf);
    1916             :   long c;
    1917      460942 :   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      460935 :   c = lg(bnf_get_cyc(bnf)) - 1;
    1925      460935 :   if (flag & nf_GENMAT)
    1926      451338 :     retmkvec2(zerocol(c), to_famat_shallow(algtobasis(nf,x), gen_1));
    1927        9597 :   if (flag & nf_GEN)
    1928          21 :     retmkvec2(zerocol(c), algtobasis(nf,x));
    1929        9576 :   return zerocol(c);
    1930             : }
    1931             : 
    1932             : GEN
    1933     1765646 : bnfisprincipal0(GEN bnf,GEN x,long flag)
    1934             : {
    1935     1765646 :   pari_sp av = avma;
    1936             :   GEN c, nf;
    1937             :   long pr;
    1938             : 
    1939     1765646 :   bnf = checkbnf(bnf);
    1940     1765645 :   nf = bnf_get_nf(bnf);
    1941     1765645 :   switch( idealtyp(&x, NULL) )
    1942             :   {
    1943       56070 :     case id_PRINCIPAL:
    1944       56070 :       if (gequal0(x)) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1945       56070 :       return triv_gen(bnf, x, flag);
    1946     1685893 :     case id_PRIME:
    1947     1685893 :       if (pr_is_inert(x)) return triv_gen(bnf, pr_get_p(x), flag);
    1948     1281028 :       x = pr_hnf(nf, x);
    1949     1281031 :       break;
    1950       23681 :     case id_MAT:
    1951       23681 :       if (lg(x)==1) pari_err_DOMAIN("bnfisprincipal","ideal","=",gen_0,x);
    1952       23681 :       if (nf_get_degree(nf) != lg(x)-1)
    1953           0 :         pari_err_TYPE("idealtyp [dimension != degree]", x);
    1954             :   }
    1955     1304712 :   pr = prec_arch(bnf); /* precision of unit matrix */
    1956     1304710 :   c = getrand();
    1957             :   for (;;)
    1958           7 :   {
    1959     1304718 :     pari_sp av1 = avma;
    1960     1304718 :     GEN y = isprincipalall(bnf,x,&pr,flag);
    1961     1304718 :     if (y) return gerepilecopy(av, y);
    1962             : 
    1963           7 :     if (DEBUGLEVEL) pari_warn(warnprec,"isprincipal",pr);
    1964           7 :     set_avma(av1); bnf = bnfnewprec_shallow(bnf,pr); setrand(c);
    1965             :   }
    1966             : }
    1967             : GEN
    1968      174411 : 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       31872 : add_principal_part(GEN nf, GEN u, GEN v, long flag)
    1986             : {
    1987       31872 :   if (flag & nf_GENMAT)
    1988       14260 :     return (typ(u) == t_COL && RgV_is1(u))? v: famat_mul_shallow(v,u);
    1989             :   else
    1990       17612 :     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       31884 : expandext(GEN nf, GEN C, GEN P, GEN e)
    2057             : {
    2058       31884 :   long i, l = lg(e);
    2059       31884 :   GEN B, A = gel(C,1), C1 = A;
    2060       94025 :   for (i=1; i<l; i++) /* compute prod P[i]^e[i] */
    2061       62140 :     if (signe(gel(e,i)))
    2062             :     {
    2063       34528 :       gel(C,1) = gel(P,i);
    2064       34528 :       B = idealpowred(nf, C, gel(e,i));
    2065       34529 :       A = A? idealmulred(nf,A,B): B;
    2066             :     }
    2067       31885 :   return A == C1? C: A;
    2068             : }
    2069             : 
    2070             : /* isprincipal for C * \prod P[i]^e[i] (C omitted if NULL) */
    2071             : GEN
    2072       31884 : isprincipalfact(GEN bnf, GEN C, GEN P, GEN e, long flag)
    2073             : {
    2074       31884 :   const long gen = flag & (nf_GEN|nf_GENMAT|nf_GEN_IF_PRINCIPAL);
    2075             :   long prec;
    2076       31884 :   pari_sp av = avma;
    2077       31884 :   GEN C0, Cext, c, id, nf = bnf_get_nf(bnf);
    2078             : 
    2079       31884 :   if (gen)
    2080             :   {
    2081       14267 :     Cext = (flag & nf_GENMAT)? trivial_fact()
    2082       31884 :                              : mkpolmod(gen_1,nf_get_pol(nf));
    2083       31884 :     C0 = mkvec2(C, Cext);
    2084       31884 :     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       31886 :   if (id == C0) /* e = 0 */
    2091             :   {
    2092       12463 :     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       19423 :     if (gen) { C = gel(id,1); Cext = gel(id,2); } else C = id;
    2103             :   }
    2104       31872 :   prec = prec_arch(bnf);
    2105       31872 :   c = getrand();
    2106             :   for (;;)
    2107          70 :   {
    2108       31942 :     pari_sp av1 = avma;
    2109       31942 :     GEN y = isprincipalall(bnf, C, &prec, flag);
    2110       31942 :     if (y)
    2111             :     {
    2112       31872 :       if (flag & nf_GEN_IF_PRINCIPAL)
    2113             :       {
    2114       20545 :         if (typ(y) == t_INT) return gc_NULL(av);
    2115       20545 :         y = add_principal_part(nf, y, Cext, flag);
    2116             :       }
    2117             :       else
    2118             :       {
    2119       11327 :         GEN u = gel(y,2);
    2120       11327 :         if (!gen || typ(y) != t_VEC) return gerepileupto(av,y);
    2121       11327 :         if (lg(u) != 1) gel(y,2) = add_principal_part(nf, u, Cext, flag);
    2122             :       }
    2123       31869 :       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      148620 : nfsign_from_logarch(GEN LA, GEN invpi, GEN archp)
    2155             : {
    2156      148620 :   long l = lg(archp), i;
    2157      148620 :   GEN y = cgetg(l, t_VECSMALL);
    2158      148621 :   pari_sp av = avma;
    2159             : 
    2160      279238 :   for (i=1; i<l; i++)
    2161             :   {
    2162      130616 :     GEN c = ground( gmul(imag_i(gel(LA,archp[i])), invpi) );
    2163      130618 :     y[i] = mpodd(c)? 1: 0;
    2164             :   }
    2165      148622 :   set_avma(av); return y;
    2166             : }
    2167             : 
    2168             : GEN
    2169      226810 : nfsign_tu(GEN bnf, GEN archp)
    2170             : {
    2171             :   long n;
    2172      226810 :   if (bnf_get_tuN(bnf) != 2) return cgetg(1, t_VECSMALL);
    2173      159803 :   n = archp? lg(archp) - 1: nf_get_r1(bnf_get_nf(bnf));
    2174      159803 :   return const_vecsmall(n, 1);
    2175             : }
    2176             : GEN
    2177      228008 : nfsign_fu(GEN bnf, GEN archp)
    2178             : {
    2179      228008 :   GEN invpi, y, A = bnf_get_logfu(bnf), nf = bnf_get_nf(bnf);
    2180      228020 :   long j = 1, RU = lg(A);
    2181             : 
    2182      228020 :   if (!archp) archp = identity_perm( nf_get_r1(nf) );
    2183      228020 :   invpi = invr( mppi(nf_get_prec(nf)) );
    2184      228028 :   y = cgetg(RU,t_MAT);
    2185      376560 :   for (j = 1; j < RU; j++)
    2186      148523 :     gel(y,j) = nfsign_from_logarch(gel(A,j), invpi, archp);
    2187      228037 :   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      727816 : get_log_embed(REL_t *rel, GEN M, long RU, long R1, long prec)
    2218             : {
    2219      727816 :   GEN arch, C, z = rel->m;
    2220             :   long i;
    2221      727816 :   arch = typ(z) == t_COL? RgM_RgC_mul(M, z): const_col(nbrows(M), z);
    2222      727815 :   C = cgetg(RU+1, t_COL); arch = glog(arch, prec);
    2223     1668932 :   for (i=1; i<=R1; i++) gel(C,i) = gel(arch,i);
    2224     1565274 :   for (   ; i<=RU; i++) gel(C,i) = gmul2n(gel(arch,i), 1);
    2225      727813 :   return C;
    2226             : }
    2227             : static GEN
    2228     1015095 : 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     1015095 :   if (!rel->relaut) return get_log_embed(rel, M, RU, R1, prec);
    2234             :   /* image of another relation by automorphism */
    2235      287280 :   C = gel(embs, ind - rel->relorig);
    2236      287280 :   perm = gel(F->embperm, rel->relaut);
    2237      287280 :   D = cgetg_copy(C, &n);
    2238     1216121 :   for (i = 1; i < n; i++)
    2239             :   {
    2240      928836 :     long v = perm[i];
    2241      928836 :     gel(D,i) = (v > 0)? gel(C,v): conj_i(gel(C,-v));
    2242             :   }
    2243      287285 :   return D;
    2244             : }
    2245             : static GEN
    2246      103305 : get_embs(FB_t *F, RELCACHE_t *cache, GEN nf, GEN embs, long PREC)
    2247             : {
    2248      103305 :   long ru, j, k, l = cache->last - cache->chk + 1, r1 = nf_get_r1(nf);
    2249      103305 :   GEN M = nf_get_M(nf), nembs = cgetg(cache->last - cache->base+1, t_MAT);
    2250             :   REL_t *rel;
    2251             : 
    2252     3550332 :   for (k = 1; k <= cache->chk - cache->base; k++) gel(nembs,k) = gel(embs,k);
    2253      103304 :   embs = nembs; ru = nbrows(M);
    2254     1105579 :   for (j=1,rel = cache->chk + 1; j < l; rel++,j++,k++)
    2255     1002286 :     gel(embs,k) = rel_embed(rel, F, embs, k, M, ru, r1, PREC);
    2256      103293 :   return embs;
    2257             : }
    2258             : static void
    2259      935421 : set_rel_alpha(REL_t *rel, GEN auts, GEN vA, long ind)
    2260             : {
    2261             :   GEN u;
    2262      935421 :   if (!rel->relaut)
    2263      673329 :     u = rel->m;
    2264             :   else
    2265      262092 :     u = ZM_ZC_mul(gel(auts, rel->relaut), gel(vA, ind - rel->relorig));
    2266      935426 :   gel(vA, ind) = u;
    2267      935426 : }
    2268             : static GEN
    2269     2304415 : set_fact(FB_t *F, FACT *fact, GEN e, long *pnz)
    2270             : {
    2271     2304415 :   long n = fact[0].pr;
    2272     2304415 :   GEN c = zero_Flv(F->KC);
    2273     2304515 :   if (!n) /* trivial factorization */
    2274          85 :     *pnz = F->KC+1;
    2275             :   else
    2276             :   {
    2277     2304430 :     long i, nz = minss(fact[1].pr, fact[n].pr);
    2278    10793575 :     for (i = 1; i <= n; i++) c[fact[i].pr] = fact[i].ex;
    2279     2304430 :     if (e)
    2280             :     {
    2281       16612 :       long l = lg(e);
    2282       60047 :       for (i = 1; i < l; i++)
    2283       43435 :         if (e[i]) { long v = F->subFB[i]; c[v] += e[i]; if (v < nz) nz = v; }
    2284             :     }
    2285     2304430 :     *pnz = nz;
    2286             :   }
    2287     2304515 :   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     2971839 : already_known(RELCACHE_t *cache, long bs, GEN cols)
    2294             : {
    2295             :   REL_t *r;
    2296     2971839 :   long l = lg(cols);
    2297   222410256 :   for (r = cache->last; r > cache->base; r--)
    2298   220005971 :     if (bs == r->nz)
    2299             :     {
    2300    40540878 :       GEN coll = r->R;
    2301    40540878 :       long b = bs;
    2302   127846783 :       while (b < l && cols[b] == coll[b]) b++;
    2303    40540878 :       if (b == l) return 1;
    2304             :     }
    2305     2404285 :   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     2975924 : 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     2975924 :   long i, k, n = lg(R)-1;
    2316             : 
    2317     2975924 :   if (nz == n+1) { k = 0; goto ADD_REL; }
    2318     2971835 :   if (already_known(cache, nz, R)) return -1;
    2319     2404315 :   if (cache->last >= cache->base + cache->len) return 0;
    2320     2404315 :   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     2404324 :   if (cache->missing)
    2326             :   {
    2327     2006818 :     GEN a = leafcopy(R), basis = cache->basis;
    2328     2006814 :     k = lg(a);
    2329   123665214 :     do --k; while (!a[k]);
    2330     7510613 :     while (k)
    2331             :     {
    2332     5966749 :       GEN c = gel(basis, k);
    2333     5966749 :       if (c[k])
    2334             :       {
    2335     5503799 :         long ak = a[k];
    2336   265893167 :         for (i=1; i < k; i++) if (c[i]) a[i] = (a[i] + ak*(mod_p-c[i])) % mod_p;
    2337     5503799 :         a[k] = 0;
    2338   129522989 :         do --k; while (!a[k]); /* k cannot go below 0: codeword is a sentinel */
    2339             :       }
    2340             :       else
    2341             :       {
    2342      462950 :         ulong invak = Fl_inv(uel(a,k), mod_p);
    2343             :         /* Cleanup a */
    2344    13667421 :         for (i = k; i-- > 1; )
    2345             :         {
    2346    13204470 :           long j, ai = a[i];
    2347    13204470 :           c = gel(basis, i);
    2348    13204470 :           if (!ai || !c[i]) continue;
    2349      259642 :           ai = mod_p-ai;
    2350     4414845 :           for (j = 1; j < i; j++) if (c[j]) a[j] = (a[j] + ai*c[j]) % mod_p;
    2351      259642 :           a[i] = 0;
    2352             :         }
    2353             :         /* Insert a/a[k] as k-th column */
    2354      462951 :         c = gel(basis, k);
    2355    13667427 :         for (i = 1; i<k; i++) if (a[i]) c[i] = (a[i] * invak) % mod_p;
    2356      462951 :         c[k] = 1; a = c;
    2357             :         /* Cleanup above k */
    2358    13505794 :         for (i = k+1; i<n; i++)
    2359             :         {
    2360             :           long j, ck;
    2361    13042843 :           c = gel(basis, i);
    2362    13042843 :           ck = c[k];
    2363    13042843 :           if (!ck) continue;
    2364     2703807 :           ck = mod_p-ck;
    2365    98818089 :           for (j = 1; j < k; j++) if (a[j]) c[j] = (c[j] + ck*a[j]) % mod_p;
    2366     2703807 :           c[k] = 0;
    2367             :         }
    2368      462951 :         cache->missing--;
    2369      462951 :         break;
    2370             :       }
    2371             :     }
    2372             :   }
    2373             :   else
    2374      397506 :     k = (cache->last - cache->base) + 1;
    2375     2404321 :   if (k || cache->relsup > 0 || (m && in_rnd_rel))
    2376             :   {
    2377             :     REL_t *rel;
    2378             : 
    2379      987018 : ADD_REL:
    2380      991107 :     rel = ++cache->last;
    2381      991107 :     if (!k && cache->relsup && nz < n+1)
    2382             :     {
    2383      126462 :       cache->relsup--;
    2384      126462 :       k = (rel - cache->base) + cache->missing;
    2385             :     }
    2386      991107 :     rel->R  = gclone(R);
    2387      991107 :     rel->m  =  m ? gclone(m) : NULL;
    2388      991104 :     rel->nz = nz;
    2389      991104 :     if (aut)
    2390             :     {
    2391      284521 :       rel->relorig = (rel - cache->base) - orig;
    2392      284521 :       rel->relaut = aut;
    2393             :     }
    2394             :     else
    2395      706583 :       rel->relaut = 0;
    2396      991104 :     if (relp) *relp = rel;
    2397      991104 :     if (DEBUGLEVEL) dbg_newrel(cache);
    2398             :   }
    2399     2408392 :   return k;
    2400             : }
    2401             : 
    2402             : static int
    2403     2475149 : 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     2475149 :   const long lauts = lg(F->idealperm), KC = F->KC;
    2408             : 
    2409     2475149 :   k = add_rel_i(cache, R, nz, m, 0, 0, &rel, in_rnd_rel);
    2410     2475176 :   if (k > 0 && typ(m) != t_INT)
    2411             :   {
    2412      535760 :     GEN Rl = cgetg(KC+1, t_VECSMALL);
    2413      535759 :     reln = rel - cache->base;
    2414     1036533 :     for (l = 1; l < lauts; l++)
    2415             :     {
    2416      500768 :       GEN perml = gel(F->idealperm, l);
    2417      500768 :       long i, nzl = perml[nz];
    2418             : 
    2419    20243822 :       for (i = 1; i <= KC; i++) Rl[i] = 0;
    2420    18003439 :       for (i = nz; i <= KC; i++)
    2421    17502671 :         if (R[i])
    2422             :         {
    2423     1394601 :           long v = perml[i];
    2424             : 
    2425     1394601 :           if (v < nzl) nzl = v;
    2426     1394601 :           Rl[v] = R[i];
    2427             :         }
    2428      500768 :       (void)add_rel_i(cache, Rl, nzl, NULL, reln, l, NULL, in_rnd_rel);
    2429             :     }
    2430             :   }
    2431     2475181 :   return k;
    2432             : }
    2433             : 
    2434             : INLINE void
    2435    32749358 : step(GEN x, double *y, GEN inc, long k)
    2436             : {
    2437    32749358 :   if (!y[k])
    2438     2034109 :     x[k]++; /* leading coeff > 0 */
    2439             :   else
    2440             :   {
    2441    30715249 :     long i = inc[k];
    2442    30715249 :     x[k] += i;
    2443    30715249 :     inc[k] = (i > 0)? -1-i: 1-i;
    2444             :   }
    2445    32749358 : }
    2446             : 
    2447             : static double
    2448      209008 : Fincke_Pohst_bound(double T, GEN r)
    2449             : {
    2450      209008 :   pari_sp av = avma;
    2451      209008 :   GEN zT = dbltor(T * T), p = gmael(r,1,1), B = real_1(DEFAULTPREC);
    2452      209005 :   long i, n = lg(r)-1;
    2453      567452 :   for (i = 2; i <= n; i++)
    2454             :   {
    2455      567445 :     p = gmul(p, gmael(r,i,i));
    2456      567437 :     B = sqrtnr(gmul(zT,p), i);
    2457      567449 :     if (i == n || cmprr(B, gmael(r,i+1,i+1)) < 0) break;
    2458             :   }
    2459      209009 :   return gc_double(av, rtodbl(B));
    2460             : }
    2461             : 
    2462             : INLINE long
    2463      209009 : Fincke_Pohst_ideal(RELCACHE_t *cache, FB_t *F, GEN nf, GEN M, GEN I,
    2464             :     GEN NI, FACT *fact, long Nrelid, FP_t *fp, RNDREL_t *rr, long prec,
    2465             :     long *Nsmall, long *Nfact)
    2466             : {
    2467             :   pari_sp av;
    2468      209009 :   const long N = nf_get_degree(nf), R1 = nf_get_r1(nf);
    2469      209007 :   GEN G = nf_get_G(nf), G0 = nf_get_roundG(nf), r, u, gx, inc, ideal;
    2470             :   double BOUND, B1, B2;
    2471      209005 :   long j, k, skipfirst, relid=0, try_factor=0;
    2472             : 
    2473      209005 :   inc = const_vecsmall(N, 1);
    2474      209004 :   u = ZM_lll(ZM_mul(G0, I), 0.99, LLL_IM);
    2475      209006 :   ideal = ZM_mul(I,u); /* approximate T2-LLL reduction */
    2476      209003 :   r = gaussred_from_QR(RgM_mul(G, ideal), prec); /* Cholesky for T2 | ideal */
    2477      209007 :   if (!r) pari_err_BUG("small_norm (precision too low)");
    2478             : 
    2479     1024397 :   for (k=1; k<=N; k++)
    2480             :   {
    2481      815388 :     if (!gisdouble(gcoeff(r,k,k),&(fp->v[k]))) return 0;
    2482     2588613 :     for (j=1; j<k; j++) if (!gisdouble(gcoeff(r,j,k),&(fp->q[j][k]))) return 0;
    2483      815391 :     if (DEBUGLEVEL>3) err_printf("v[%ld]=%.4g ",k,fp->v[k]);
    2484             :   }
    2485      209009 :   B1 = fp->v[1]; /* T2(ideal[1]) */
    2486      209009 :   B2 = fp->v[2] + B1 * fp->q[1][2] * fp->q[1][2]; /* T2(ideal[2]) */
    2487      209009 :   skipfirst = ZV_isscalar(gel(ideal,1));
    2488      209008 :   BOUND = maxdd(2*B2, Fincke_Pohst_bound(4 * maxtry_FACT / F->ballvol, r));
    2489      209007 :   if (DEBUGLEVEL>1)
    2490             :   {
    2491           0 :     if (DEBUGLEVEL>3) err_printf("\n");
    2492           0 :     err_printf("BOUND = %.4g\n",BOUND);
    2493             :   }
    2494             : 
    2495      209007 :   k = N; fp->y[N] = fp->z[N] = 0; fp->x[N] = 0;
    2496    23092656 :   for (av = avma;; set_avma(av), step(fp->x,fp->y,inc,k))
    2497    22885987 :   {
    2498             :     GEN R;
    2499             :     long nz;
    2500             :     do
    2501             :     { /* look for primitive element of small norm, cf minim00 */
    2502    27972306 :       int fl = 0;
    2503             :       double p;
    2504    27972306 :       if (k > 1)
    2505             :       {
    2506     5086364 :         long l = k-1;
    2507     5086364 :         fp->z[l] = 0;
    2508    44894763 :         for (j=k; j<=N; j++) fp->z[l] += fp->q[l][j]*fp->x[j];
    2509     5086364 :         p = (double)fp->x[k] + fp->z[k];
    2510     5086364 :         fp->y[l] = fp->y[k] + p*p*fp->v[k];
    2511     5086364 :         if (l <= skipfirst && !fp->y[1]) fl = 1;
    2512     5086364 :         fp->x[l] = (long)floor(-fp->z[l] + 0.5);
    2513     5086364 :         k = l;
    2514             :       }
    2515     4485206 :       for(;; step(fp->x,fp->y,inc,k))
    2516             :       {
    2517    32457492 :         if (!fl)
    2518             :         {
    2519    32395049 :           p = (double)fp->x[k] + fp->z[k];
    2520    32395049 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2521             : 
    2522     5377904 :           step(fp->x,fp->y,inc,k);
    2523             : 
    2524     5378254 :           p = (double)fp->x[k] + fp->z[k];
    2525     5378254 :           if (fp->y[k] + p*p*fp->v[k] <= BOUND) break;
    2526             :         }
    2527     4487593 :         fl = 0; inc[k] = 1;
    2528     4487593 :         if (++k > N) goto END_Fincke_Pohst_ideal;
    2529             :       }
    2530    27970249 :     } while (k > 1);
    2531             : 
    2532             :     /* element complete */
    2533    42086651 :     if (zv_content(fp->x) !=1) continue; /* not primitive */
    2534    19560770 :     gx = ZM_zc_mul(ideal,fp->x);
    2535    19560503 :     if (ZV_isscalar(gx)) continue;
    2536    19607031 :     if (++try_factor > maxtry_FACT) break;
    2537             : 
    2538    19503164 :     if (!Nrelid)
    2539             :     {
    2540         259 :       if (!factorgen(F,nf,I,NI,gx,fact)) continue;
    2541      102756 :       return 1;
    2542             :     }
    2543    19502905 :     else if (rr)
    2544             :     {
    2545       43723 :       if (!factorgen(F,nf,I,NI,gx,fact)) continue;
    2546       16612 :       add_to_fact(rr->jid, 1, fact);
    2547             :     }
    2548             :     else
    2549             :     {
    2550    19459182 :       GEN Nx, xembed = RgM_RgC_mul(M, gx);
    2551             :       long e;
    2552    19459871 :       if (Nsmall) (*Nsmall)++;
    2553    19459871 :       Nx = grndtoi(embed_norm(xembed, R1), &e);
    2554    19459617 :       if (e >= 0) {
    2555           0 :         if (DEBUGLEVEL > 1) err_printf("+");
    2556    17172914 :         continue;
    2557             :       }
    2558    19459617 :       if (!can_factor(F, nf, NULL, gx, Nx, fact)) continue;
    2559             :     }
    2560             : 
    2561             :     /* smooth element */
    2562     2299039 :     R = set_fact(F, fact, rr ? rr->ex : NULL, &nz);
    2563             :     /* make sure we get maximal rank first, then allow all relations */
    2564     2299182 :     if (add_rel(cache, F, R, nz, gx, rr ? 1 : 0) <= 0)
    2565             :     { /* probably Q-dependent from previous ones: forget it */
    2566     1763598 :       if (DEBUGLEVEL>1) err_printf("*");
    2567     1763596 :       if (DEBUGLEVEL && Nfact && rr) (*Nfact)++;
    2568     1763596 :       continue;
    2569             :     }
    2570      535630 :     if (DEBUGLEVEL && Nfact) (*Nfact)++;
    2571      535630 :     if (cache->last >= cache->end) return 1; /* we have enough */
    2572      432888 :     if (++relid == Nrelid) break;
    2573             :   }
    2574      106254 :   END_Fincke_Pohst_ideal:
    2575      106254 :   return 0;
    2576             : }
    2577             : 
    2578             : static void
    2579       91658 : small_norm(RELCACHE_t *cache, FB_t *F, GEN nf, long Nrelid, GEN M,
    2580             :            FACT *fact, GEN p0)
    2581             : {
    2582       91658 :   const long prec = nf_get_prec(nf);
    2583             :   FP_t fp;
    2584             :   pari_sp av;
    2585       91657 :   GEN L_jid = F->L_jid, Np0 = NULL;
    2586       91657 :   long Nsmall, Nfact, n = lg(L_jid);
    2587             :   pari_timer T;
    2588             : 
    2589       91657 :   if (DEBUGLEVEL)
    2590             :   {
    2591           0 :     timer_start(&T);
    2592           0 :     err_printf("#### Look for %ld relations in %ld ideals (small_norm)\n",
    2593           0 :                cache->end - cache->last, lg(L_jid)-1);
    2594           0 :     if (p0) err_printf("Look in p0 = %Ps\n", vecslice(p0,1,4));
    2595             :   }
    2596       91657 :   Nsmall = Nfact = 0;
    2597       91657 :   minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2598       91658 :   if (p0)
    2599             :   {
    2600       27877 :     GEN n = pr_norm(p0);
    2601       27877 :     ulong e = maxuu(1,logint0(sqri(pr_norm(veclast(F->LP))), n, NULL));
    2602       27877 :     p0 = idealpow(nf, p0, utoi(e));
    2603       27877 :     Np0 = powiu(n,e);
    2604             :   }
    2605      197559 :   for (av = avma; --n; set_avma(av))
    2606             :   {
    2607      196558 :     long j = L_jid[n];
    2608      196558 :     GEN id = gel(F->LP, j), Nid;
    2609      196558 :     if (DEBUGLEVEL>1)
    2610           0 :       err_printf("\n*** Ideal no %ld: %Ps\n", j, vecslice(id,1,4));
    2611      196557 :     if (p0)
    2612       35868 :     { Nid = mulii(Np0, pr_norm(id)); id = idealmul(nf, p0, id); }
    2613             :     else
    2614      160689 :     { Nid = pr_norm(id); id = pr_hnf(nf, id);}
    2615      196558 :     if (Fincke_Pohst_ideal(cache, F, nf, M, id, Nid, fact, Nrelid, &fp,
    2616       90656 :                            NULL, prec, &Nsmall, &Nfact)) break;
    2617             :   }
    2618       91658 :   if (DEBUGLEVEL && Nsmall)
    2619             :   {
    2620           0 :     if (DEBUGLEVEL == 1)
    2621           0 :     { if (Nfact) err_printf("\n"); }
    2622             :     else
    2623           0 :       err_printf("  \nnb. fact./nb. small norm = %ld/%ld = %.3f\n",
    2624           0 :                   Nfact,Nsmall,((double)Nfact)/Nsmall);
    2625           0 :     if (timer_get(&T)>1) timer_printf(&T,"small_norm");
    2626             :   }
    2627       91658 : }
    2628             : 
    2629             : static GEN
    2630       12093 : get_random_ideal(FB_t *F, GEN nf, GEN ex)
    2631             : {
    2632       12093 :   long i, l = lg(ex);
    2633             :   for (;;)
    2634         326 :   {
    2635       12419 :     GEN I = NULL;
    2636       49518 :     for (i = 1; i < l; i++)
    2637       37099 :       if ((ex[i] = random_bits(RANDOM_BITS)))
    2638             :       {
    2639       34765 :         GEN pr = gel(F->LP, F->subFB[i]), e = utoipos(ex[i]);
    2640       34765 :         I = I? idealmulpowprime(nf, I, pr, e): idealpow(nf, pr, e);
    2641             :       }
    2642       12419 :     if (I && !ZM_isscalar(I,NULL)) return I; /* != (n)Z_K */
    2643             :   }
    2644             : }
    2645             : 
    2646             : static void
    2647       12093 : rnd_rel(RELCACHE_t *cache, FB_t *F, GEN nf, FACT *fact)
    2648             : {
    2649             :   pari_timer T;
    2650       12093 :   GEN L_jid = F->L_jid, M = nf_get_M(nf), R, NR;
    2651       12093 :   long i, l = lg(L_jid), prec = nf_get_prec(nf), Nfact = 0;
    2652             :   RNDREL_t rr;
    2653             :   FP_t fp;
    2654             :   pari_sp av;
    2655             : 
    2656       12093 :   if (DEBUGLEVEL) {
    2657           0 :     timer_start(&T);
    2658           0 :     err_printf("#### Look for %ld relations in %ld ideals (rnd_rel)\n",
    2659           0 :                cache->end - cache->last, l-1);
    2660             :   }
    2661       12093 :   rr.ex = cgetg(lg(F->subFB), t_VECSMALL);
    2662       12093 :   R = get_random_ideal(F, nf, rr.ex); /* random product from subFB */
    2663       12093 :   NR = ZM_det_triangular(R);
    2664       12093 :   minim_alloc(lg(M), &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2665       12444 :   for (av = avma, i = 1; i < l; i++, set_avma(av))
    2666             :   { /* try P[j] * base */
    2667       12437 :     long j = L_jid[i];
    2668       12437 :     GEN P = gel(F->LP, j), Nid = mulii(NR, pr_norm(P));
    2669       12437 :     if (DEBUGLEVEL>1) err_printf("\n*** Ideal %ld: %Ps\n", j, vecslice(P,1,4));
    2670       12437 :     rr.jid = j;
    2671       12437 :     if (Fincke_Pohst_ideal(cache, F, nf, M, idealHNF_mul(nf, R, P), Nid, fact,
    2672       12086 :                            RND_REL_RELPID, &fp, &rr, prec, NULL, &Nfact)) break;
    2673             :   }
    2674       12093 :   if (DEBUGLEVEL)
    2675             :   {
    2676           0 :     if (Nfact) err_printf("\n");
    2677           0 :     if (timer_get(&T)>=0) timer_printf(&T,"rnd_rel");
    2678             :   }
    2679       12093 : }
    2680             : 
    2681             : static GEN
    2682       63601 : automorphism_perms(GEN M, GEN auts, GEN cyclic, long r1, long r2, long N)
    2683             : {
    2684       63601 :   long L = lgcols(M), lauts = lg(auts), lcyc = lg(cyclic), i, j, l, m;
    2685       63601 :   GEN Mt, perms = cgetg(lauts, t_VEC);
    2686             :   pari_sp av;
    2687             : 
    2688      127433 :   for (l = 1; l < lauts; l++) gel(perms, l) = cgetg(L, t_VECSMALL);
    2689       63601 :   av = avma;
    2690       63601 :   Mt = shallowtrans(gprec_w(M, LOWDEFAULTPREC));
    2691       63601 :   Mt = shallowconcat(Mt, conj_i(vecslice(Mt, r1+1, r1+r2)));
    2692      110199 :   for (l = 1; l < lcyc; l++)
    2693             :   {
    2694       46598 :     GEN thiscyc = gel(cyclic, l), thisperm, perm, prev, Nt;
    2695       46598 :     long k = thiscyc[1];
    2696             : 
    2697       46598 :     Nt = RgM_mul(shallowtrans(gel(auts, k)), Mt);
    2698       46598 :     perm = gel(perms, k);
    2699      153171 :     for (i = 1; i < L; i++)
    2700             :     {
    2701      106573 :       GEN v = gel(Nt, i), minD;
    2702      106573 :       minD = gnorml2(gsub(v, gel(Mt, 1)));
    2703      106569 :       perm[i] = 1;
    2704      563184 :       for (j = 2; j <= N; j++)
    2705             :       {
    2706      456611 :         GEN D = gnorml2(gsub(v, gel(Mt, j)));
    2707      456615 :         if (gcmp(D, minD) < 0) { minD = D; perm[i] = j >= L ? r2-j : j; }
    2708             :       }
    2709             :     }
    2710       65043 :     for (prev = perm, m = 2; m < lg(thiscyc); m++, prev = thisperm)
    2711             :     {
    2712       18445 :       thisperm = gel(perms, thiscyc[m]);
    2713       93604 :       for (i = 1; i < L; i++)
    2714             :       {
    2715       75159 :         long pp = labs(prev[i]);
    2716       75159 :         thisperm[i] = prev[i] < 0 ? -perm[pp] : perm[pp];
    2717             :       }
    2718             :     }
    2719             :   }
    2720       63601 :   set_avma(av); return perms;
    2721             : }
    2722             : 
    2723             : /* Determine the field automorphisms as matrices on the integral basis */
    2724             : static GEN
    2725       63663 : automorphism_matrices(GEN nf, GEN *cycp)
    2726             : {
    2727       63663 :   pari_sp av = avma;
    2728       63663 :   GEN auts = galoisconj(nf, NULL), mats, cyclic, cyclicidx;
    2729       63664 :   long nauts = lg(auts)-1, i, j, k, l;
    2730             : 
    2731       63664 :   cyclic = cgetg(nauts+1, t_VEC);
    2732       63664 :   cyclicidx = zero_Flv(nauts);
    2733       97771 :   for (l = 1; l <= nauts; l++)
    2734             :   {
    2735       97770 :     GEN aut = gel(auts, l);
    2736       97770 :     if (gequalX(aut)) { swap(gel(auts, l), gel(auts, nauts)); break; }
    2737             :   }
    2738             :   /* trivial automorphism is last */
    2739      191188 :   for (l = 1; l <= nauts; l++) gel(auts, l) = algtobasis(nf, gel(auts, l));
    2740             :   /* Compute maximal cyclic subgroups */
    2741      127522 :   for (l = nauts; --l > 0; ) if (!cyclicidx[l])
    2742             :   {
    2743       48096 :     GEN elt = gel(auts, l), aut = elt, cyc = cgetg(nauts+1, t_VECSMALL);
    2744       48096 :     cyc[1] = cyclicidx[l] = l; j = 1;
    2745             :     do
    2746             :     {
    2747       67101 :       elt = galoisapply(nf, elt, aut);
    2748      217655 :       for (k = 1; k <= nauts; k++) if (gequal(elt, gel(auts, k))) break;
    2749       67101 :       cyclicidx[k] = l; cyc[++j] = k;
    2750             :     }
    2751       67101 :     while (k != nauts);
    2752       48096 :     setlg(cyc, j);
    2753       48096 :     gel(cyclic, l) = cyc;
    2754             :   }
    2755      127523 :   for (i = j = 1; i < nauts; i++)
    2756       63860 :     if (cyclicidx[i] == i) cyclic[j++] = cyclic[i];
    2757       63663 :   setlg(cyclic, j);
    2758       63665 :   mats = cgetg(nauts, t_VEC);
    2759      110290 :   while (--j > 0)
    2760             :   {
    2761       46626 :     GEN cyc = gel(cyclic, j);
    2762       46626 :     long id = cyc[1];
    2763       46626 :     GEN M, Mi, aut = gel(auts, id);
    2764             : 
    2765       46626 :     gel(mats, id) = Mi = M = nfgaloismatrix(nf, aut);
    2766       65071 :     for (i = 2; i < lg(cyc); i++) gel(mats, cyc[i]) = Mi = ZM_mul(Mi, M);
    2767             :   }
    2768       63664 :   gerepileall(av, 2, &mats, &cyclic);
    2769       63664 :   if (cycp) *cycp = cyclic;
    2770       63664 :   return mats;
    2771             : }
    2772             : 
    2773             : /* vP a list of maximal ideals above the same p from idealprimedec: f(P/p) is
    2774             :  * increasing; 1 <= j <= #vP; orbit a zc of length <= #vP; auts a vector of
    2775             :  * automorphisms in ZM form.
    2776             :  * Set orbit[i] = 1 for all vP[i], i >= j, in the orbit of pr = vP[j] wrt auts.
    2777             :  * N.B.1 orbit need not be initialized to 0: useful to incrementally run
    2778             :  * through successive orbits
    2779             :  * N.B.2 i >= j, so primes with index < j will be missed; run incrementally
    2780             :  * starting from j = 1 ! */
    2781             : static void
    2782       11865 : pr_orbit_fill(GEN orbit, GEN auts, GEN vP, long j)
    2783             : {
    2784       11865 :   GEN pr = gel(vP,j), gen = pr_get_gen(pr);
    2785       11865 :   long i, l = lg(auts), J = lg(orbit), f = pr_get_f(pr);
    2786       11865 :   orbit[j] = 1;
    2787       23730 :   for (i = 1; i < l; i++)
    2788             :   {
    2789       11865 :     GEN g = ZM_ZC_mul(gel(auts,i), gen);
    2790             :     long k;
    2791       11886 :     for (k = j+1; k < J; k++)
    2792             :     {
    2793          35 :       GEN prk = gel(vP,k);
    2794          35 :       if (pr_get_f(prk) > f) break; /* f(P[k]) increases with k */
    2795             :       /* don't check that e matches: (almost) always 1 ! */
    2796          35 :       if (!orbit[k] && ZC_prdvd(g, prk)) { orbit[k] = 1; break; }
    2797             :     }
    2798             :   }
    2799       11865 : }
    2800             : /* remark: F->KCZ changes if be_honest() fails */
    2801             : static int
    2802           7 : be_honest(FB_t *F, GEN nf, GEN auts, FACT *fact)
    2803             : {
    2804             :   long i, iz, nbtest;
    2805           7 :   long lgsub = lg(F->subFB), KCZ0 = F->KCZ;
    2806           7 :   long N = nf_get_degree(nf), prec = nf_get_prec(nf);
    2807           7 :   GEN M = nf_get_M(nf);
    2808             :   FP_t fp;
    2809             :   pari_sp av;
    2810             : 
    2811           7 :   if (DEBUGLEVEL) {
    2812           0 :     err_printf("Be honest for %ld primes from %ld to %ld\n", F->KCZ2 - F->KCZ,
    2813           0 :                F->FB[ F->KCZ+1 ], F->FB[ F->KCZ2 ]);
    2814             :   }
    2815           7 :   minim_alloc(N+1, &fp.q, &fp.x, &fp.y, &fp.z, &fp.v);
    2816           7 :   if (lg(auts) == 1) auts = NULL;
    2817           7 :   av = avma;
    2818          14 :   for (iz=F->KCZ+1; iz<=F->KCZ2; iz++, set_avma(av))
    2819             :   {
    2820           7 :     long p = F->FB[iz];
    2821           7 :     GEN pr_orbit, P = gel(F->LV,p);
    2822           7 :     long j, J = lg(P); /* > 1 */
    2823             :     /* the P|p, NP > C2 are assumed in subgroup generated by FB + last P
    2824             :      * with NP <= C2 is unramified --> check all but last */
    2825           7 :     if (pr_get_e(gel(P,J-1)) == 1) J--;
    2826           7 :     if (J == 1) continue;
    2827           7 :     if (DEBUGLEVEL>1) err_printf("%ld ", p);
    2828           7 :     pr_orbit = auts? zero_zv(J-1): NULL;
    2829          28 :     for (j = 1; j < J; j++)
    2830             :     {
    2831             :       GEN Nid, id, id0;
    2832          21 :       if (pr_orbit)
    2833             :       {
    2834          21 :         if (pr_orbit[j]) continue;
    2835             :         /* discard all primes in automorphism orbit simultaneously */
    2836          14 :         pr_orbit_fill(pr_orbit, auts, P, j);
    2837             :       }
    2838          14 :       id = id0 = pr_hnf(nf,gel(P,j));
    2839          14 :       Nid = pr_norm(gel(P,j));
    2840          14 :       for (nbtest=0;;)
    2841             :       {
    2842          14 :         if (Fincke_Pohst_ideal(NULL, F, nf, M, id, Nid, fact, 0, &fp,
    2843          14 :                                NULL, prec, NULL, NULL)) break;
    2844           0 :         if (++nbtest > maxtry_HONEST)
    2845             :         {
    2846           0 :           if (DEBUGLEVEL)
    2847           0 :             pari_warn(warner,"be_honest() failure on prime %Ps\n", gel(P,j));
    2848           0 :           return 0;
    2849             :         }
    2850             :         /* occurs at most once in the whole function */
    2851           0 :         for (i = 1, id = id0; i < lgsub; i++)
    2852             :         {
    2853           0 :           long ex = random_bits(RANDOM_BITS);
    2854           0 :           if (ex)
    2855             :           {
    2856           0 :             GEN pr = gel(F->LP, F->subFB[i]);
    2857           0 :             id = idealmulpowprime(nf, id, pr, utoipos(ex));
    2858             :           }
    2859             :         }
    2860           0 :         if (!equali1(gcoeff(id,N,N))) id = Q_primpart(id);
    2861           0 :         if (expi(gcoeff(id,1,1)) > 100) id = idealred(nf, id);
    2862           0 :         Nid = ZM_det_triangular(id);
    2863             :       }
    2864             :     }
    2865           7 :     F->KCZ++; /* SUCCESS, "enlarge" factorbase */
    2866             :   }
    2867           7 :   F->KCZ = KCZ0; return gc_bool(av,1);
    2868             : }
    2869             : 
    2870             : /* all primes with N(P) <= BOUND factor on factorbase ? */
    2871             : void
    2872          63 : bnftestprimes(GEN bnf, GEN BOUND)
    2873             : {
    2874          63 :   pari_sp av0 = avma, av;
    2875          63 :   ulong count = 0;
    2876          63 :   GEN auts, p, nf = bnf_get_nf(bnf), Vbase = bnf_get_vbase(bnf);
    2877          63 :   GEN fb = gen_sort_shallow(Vbase, (void*)&cmp_prime_ideal, cmp_nodata);
    2878          63 :   ulong pmax = pr_get_smallp(veclast(fb)); /*largest p in factorbase*/
    2879             :   forprime_t S;
    2880             :   FACT *fact;
    2881             :   FB_t F;
    2882             : 
    2883          63 :   (void)recover_partFB(&F, Vbase, nf_get_degree(nf));
    2884          63 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    2885          63 :   forprime_init(&S, gen_2, BOUND);
    2886          63 :   auts = automorphism_matrices(nf, NULL);
    2887          63 :   if (lg(auts) == 1) auts = NULL;
    2888          63 :   av = avma;
    2889       37604 :   while (( p = forprime_next(&S) ))
    2890             :   {
    2891             :     GEN pr_orbit, vP;
    2892             :     long j, J;
    2893             : 
    2894       37541 :     if (DEBUGLEVEL == 1 && ++count > 1000)
    2895             :     {
    2896           0 :       err_printf("passing p = %Ps / %Ps\n", p, BOUND);
    2897           0 :       count = 0;
    2898             :     }
    2899       37541 :     set_avma(av);
    2900       37541 :     vP = idealprimedec_limit_norm(nf, p, BOUND);
    2901       37541 :     J = lg(vP);
    2902             :     /* if last is unramified, all P|p in subgroup generated by FB: skip last */
    2903       37541 :     if (J > 1 && pr_get_e(gel(vP,J-1)) == 1) J--;
    2904       37541 :     if (J == 1) continue;
    2905       14525 :     if (DEBUGLEVEL>1) err_printf("*** p = %Ps\n",p);
    2906       14525 :     pr_orbit = auts? zero_zv(J-1): NULL;
    2907       31549 :     for (j = 1; j < J; j++)
    2908             :     {
    2909       17024 :       GEN P = gel(vP,j);
    2910       17024 :       long k = 0;
    2911       17024 :       if (pr_orbit)
    2912             :       {
    2913       11858 :         if (pr_orbit[j]) continue;
    2914             :         /* discard all primes in automorphism orbit simultaneously */
    2915       11851 :         pr_orbit_fill(pr_orbit, auts, vP, j);
    2916             :       }
    2917       17017 :       if (abscmpiu(p, pmax) > 0 || !(k = tablesearch(fb, P, &cmp_prime_ideal)))
    2918       16408 :         (void)SPLIT(&F, nf, pr_hnf(nf,P), Vbase, fact);
    2919       17017 :       if (DEBUGLEVEL>1)
    2920             :       {
    2921           0 :         err_printf("  Testing P = %Ps\n",P);
    2922           0 :         if (k) err_printf("    #%ld in factor base\n",k);
    2923           0 :         else err_printf("    is %Ps\n", isprincipal(bnf,P));
    2924             :       }
    2925             :     }
    2926             :   }
    2927          63 :   set_avma(av0);
    2928          63 : }
    2929             : 
    2930             : /* A t_MAT of complex floats, in fact reals. Extract a submatrix B
    2931             :  * whose columns are definitely nonzero, i.e. gexpo(A[j]) >= -2
    2932             :  *
    2933             :  * If possible precision problem (t_REAL 0 with large exponent), set
    2934             :  * *precpb to 1 */
    2935             : static GEN
    2936       87697 : clean_cols(GEN A, int *precpb)
    2937             : {
    2938       87697 :   long l = lg(A), h, i, j, k;
    2939             :   GEN B;
    2940       87697 :   *precpb = 0;
    2941       87697 :   if (l == 1) return A;
    2942       87697 :   h = lgcols(A);;
    2943       87697 :   B = cgetg(l, t_MAT);
    2944     3267409 :   for (i = k = 1; i < l; i++)
    2945             :   {
    2946     3179712 :     GEN Ai = gel(A,i);
    2947     3179712 :     int non0 = 0;
    2948    15392083 :     for (j = 1; j < h; j++)
    2949             :     {
    2950    12212368 :       GEN c = gel(Ai,j);
    2951    12212368 :       if (gexpo(c) >= -2)
    2952             :       {
    2953    10756015 :         if (gequal0(c)) *precpb = 1; else non0 = 1;
    2954             :       }
    2955             :     }
    2956     3179715 :     if (non0) gel(B, k++) = Ai;
    2957             :   }
    2958       87697 :   setlg(B, k); return B;
    2959             : }
    2960             : 
    2961             : static long
    2962     2748089 : compute_multiple_of_R_pivot(GEN X, GEN x0/*unused*/, long ix, GEN c)
    2963             : {
    2964     2748089 :   GEN x = gel(X,ix);
    2965     2748089 :   long i, k = 0, ex = - (long)HIGHEXPOBIT, lx = lg(x);
    2966             :   (void)x0;
    2967    13790702 :   for (i=1; i<lx; i++)
    2968    11042555 :     if (!c[i] && !gequal0(gel(x,i)))
    2969             :     {
    2970     2647459 :       long e = gexpo(gel(x,i));
    2971     2647516 :       if (e > ex) { ex = e; k = i; }
    2972             :     }
    2973     2748147 :   return (k && ex > -32)? k: lx;
    2974             : }
    2975             : 
    2976             : /* Ar = (log |sigma_i(u_j)|) for units (u_j) found so far;
    2977             :  * RU = R1+R2 = target rank for unit matrix, after adding [1 x r1, 2 x r2];
    2978             :  * N = field degree, need = unit rank defect;
    2979             :  * L = NULL (prec problem) or B^(-1) * A with approximate rational entries
    2980             :  * (as t_REAL), B a submatrix of A, with (probably) maximal rank RU */
    2981             : static GEN
    2982      102908 : compute_multiple_of_R(GEN Ar, long RU, long N, long *pneed, long *bit, GEN *ptL)
    2983             : {
    2984             :   GEN T, d, mdet, Im_mdet, kR, L;
    2985      102908 :   long i, j, r, R1 = 2*RU - N;
    2986             :   int precpb;
    2987      102908 :   pari_sp av = avma;
    2988             : 
    2989      102908 :   if (RU == 1) { *ptL = zeromat(0, lg(Ar)-1); return gen_1; }
    2990             : 
    2991       87697 :   if (DEBUGLEVEL) err_printf("\n#### Computing regulator multiple\n");
    2992       87697 :   mdet = clean_cols(Ar, &precpb);
    2993             :   /* will cause precision to increase on later failure, but we may succeed! */
    2994       87697 :   *ptL = precpb? NULL: gen_1;
    2995       87697 :   T = cgetg(RU+1,t_COL);
    2996      234937 :   for (i=1; i<=R1; i++) gel(T,i) = gen_1;
    2997      187024 :   for (   ; i<=RU; i++) gel(T,i) = gen_2;
    2998       87697 :   mdet = shallowconcat(T, mdet); /* det(Span(mdet)) = N * R */
    2999             : 
    3000             :   /* could be using indexrank(), but need custom "get_pivot" function */
    3001       87697 :   d = RgM_pivots(mdet, NULL, &r, &compute_multiple_of_R_pivot);
    3002             :   /* # of independent columns = target rank ? */
    3003       87697 :   if (lg(mdet)-1 - r != RU)
    3004             :   {
    3005       30938 :     if (DEBUGLEVEL)
    3006           0 :       err_printf("Units matrix target rank = %ld < %ld\n",lg(mdet)-1 - r, RU);
    3007       30938 :     *pneed = RU - (lg(mdet)-1-r); return gc_NULL(av);
    3008             :   }
    3009             : 
    3010       56759 :   Im_mdet = cgetg(RU+1, t_MAT); /* extract independent columns */
    3011             :   /* N.B: d[1] = 1, corresponding to T above */
    3012       56759 :   gel(Im_mdet, 1) = T;
    3013      237897 :   for (i = j = 2; i <= RU; j++)
    3014      181138 :     if (d[j]) gel(Im_mdet, i++) = gel(mdet,j);
    3015             : 
    3016             :   /* integral multiple of R: the cols we picked form a Q-basis, they have an
    3017             :    * index in the full lattice. First column is T */
    3018       56759 :   kR = divru(det2(Im_mdet), N);
    3019             :   /* R > 0.2 uniformly */
    3020       56757 :   if (!signe(kR) || expo(kR) < -3)
    3021             :   {
    3022           0 :     if (DEBUGLEVEL) err_printf("Regulator is zero.\n");
    3023           0 :     *pneed = 0; return gc_NULL(av);
    3024             :   }
    3025       56757 :   d = det2(rowslice(vecslice(Im_mdet, 2, RU), 2, RU));
    3026       56757 :   setabssign(d); setabssign(kR);
    3027       56757 :   if (gexpo(gsub(d,kR)) - gexpo(d) > -20) { *ptL = NULL; return gc_NULL(av); }
    3028       56752 :   L = RgM_inv(Im_mdet);
    3029             :   /* estimate # of correct bits in result */
    3030       56752 :   if (!L || (*bit = -gexpo(RgM_Rg_sub_shallow(RgM_mul(L,Im_mdet), gen_1))) < 16)
    3031          10 :   { *ptL = NULL; return gc_NULL(av); }
    3032             : 
    3033       56742 :   *ptL = RgM_mul(rowslice(L,2,RU), Ar); /* approximate rational entries */
    3034       56742 :   return gc_all(av,2, &kR, ptL);
    3035             : }
    3036             : 
    3037             : /* leave small integer n as is, convert huge n to t_REAL (for readability) */
    3038             : static GEN
    3039           0 : i2print(GEN n)
    3040           0 : { return lgefint(n) <= DEFAULTPREC? n: itor(n,LOWDEFAULTPREC); }
    3041             : 
    3042             : static long
    3043       71824 : bad_check(GEN c)
    3044             : {
    3045       71824 :   long ec = gexpo(c);
    3046       71824 :   if (DEBUGLEVEL) err_printf("\n ***** check = %.28Pg\n",c);
    3047             :   /* safe check for c < 0.75 : avoid underflow in gtodouble() */
    3048       71824 :   if (ec < -1 || (ec == -1 && gtodouble(c) < 0.75)) return fupb_PRECI;
    3049             :   /* safe check for c > 1.3 : avoid overflow */
    3050       71824 :   if (ec > 0 || (ec == 0 && gtodouble(c) > 1.3)) return fupb_RELAT;
    3051       63601 :   return fupb_NONE;
    3052             : }
    3053             : /* Input:
    3054             :  * lambda = approximate rational entries: coords of units found so far on a
    3055             :  * sublattice of maximal rank (sublambda)
    3056             :  * *ptkR = regulator of sublambda = multiple of regulator of lambda
    3057             :  * Compute R = true regulator of lambda.
    3058             :  *
    3059             :  * If c := Rz ~ 1, by Dirichlet's formula, then lambda is the full group of
    3060             :  * units AND the full set of relations for the class group has been computed.
    3061             :  * In fact z is a very rough approximation and we only expect 0.75 < Rz < 1.3
    3062             :  *
    3063             :  * Output: *ptkR = R, *ptL = numerator(units) (in terms of lambda) */
    3064             : static long
    3065       71889 : compute_R(GEN lambda, GEN z, GEN *ptL, GEN *ptkR)
    3066             : {
    3067       71889 :   pari_sp av = avma;
    3068       71889 :   long bit, r, reason, RU = lg(lambda) == 1? 1: lgcols(lambda);
    3069             :   GEN L, H, D, den, R, c;
    3070             : 
    3071       71889 :   *ptL = NULL;
    3072       71889 :   if (RU == 1) { *ptkR = gen_1; *ptL = lambda; return bad_check(z); }
    3073       56678 :   D = gmul2n(mpmul(*ptkR,z), 1); /* bound for denom(lambda) */
    3074       56678 :   if (expo(D) < 0 && rtodbl(D) < 0.95) return fupb_PRECI;
    3075       56678 :   L = bestappr(lambda,D);
    3076       56678 :   if (lg(L) == 1)
    3077             :   {
    3078           0 :     if (DEBUGLEVEL) err_printf("truncation error in bestappr\n");
    3079           0 :     return fupb_PRECI;
    3080             :   }
    3081       56678 :   den = Q_denom(L);
    3082       56678 :   if (mpcmp(den,D) > 0)
    3083             :   {
    3084          27 :     if (DEBUGLEVEL) err_printf("D = %Ps\nden = %Ps\n",D, i2print(den));
    3085          27 :     return fupb_PRECI;
    3086             :   }
    3087       56651 :   bit = -gexpo(gsub(L, lambda)); /* input accuracy */
    3088       56651 :   L = Q_muli_to_int(L, den);
    3089       56649 :   if (gexpo(L) + expi(den) > bit - 32)
    3090             :   {
    3091          37 :     if (DEBUGLEVEL) err_printf("dubious bestappr; den = %Ps\n", i2print(den));
    3092          37 :     return fupb_PRECI;
    3093             :   }
    3094       56614 :   H = ZM_hnf(L); r = lg(H)-1;
    3095       56614 :   if (!r || r != nbrows(H))
    3096           0 :     R = gen_0; /* wrong rank */
    3097             :   else
    3098       56614 :     R = gmul(*ptkR, gdiv(ZM_det_triangular(H), powiu(den, r)));
    3099             :   /* R = tentative regulator; regulator > 0.2 uniformly */
    3100       56614 :   if (gexpo(R) < -3) {
    3101           0 :     if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3102           0 :     return gc_long(av, fupb_PRECI);
    3103             :   }
    3104       56614 :   c = gmul(R,z); /* should be n (= 1 if we are done) */
    3105       56613 :   if (DEBUGLEVEL) err_printf("\n#### Tentative regulator: %.28Pg\n", R);
    3106       56613 :   if ((reason = bad_check(c))) return gc_long(av, reason);
    3107       48411 :   *ptkR = R; *ptL = L; return fupb_NONE;
    3108             : }
    3109             : static GEN
    3110       63698 : get_clg2(GEN cyc, GEN Ga, GEN C, GEN Ur, GEN Ge, GEN M1, GEN M2)
    3111             : {
    3112       63698 :   GEN GD = gsub(act_arch(M1, C), diagact_arch(cyc, Ga));
    3113       63699 :   GEN ga = gsub(act_arch(M2, C), act_arch(Ur, Ga));
    3114       63700 :   return mkvecn(6, Ur, ga, GD, Ge, M1, M2);
    3115             : }
    3116             : /* compute class group (clg1) + data for isprincipal (clg2) */
    3117             : static GEN
    3118       63602 : class_group_gen(GEN nf,GEN W,GEN C,GEN Vbase,long prec, GEN *pclg2)
    3119             : {
    3120             :   GEN M1, M2, z, G, Ga, Ge, cyc, X, Y, D, U, V, Ur, Ui, Uir;
    3121             :   long j, l;
    3122             : 
    3123       63602 :   D = ZM_snfall(W,&U,&V); /* UWV=D, D diagonal, G = g Ui (G=new gens, g=old) */
    3124       63602 :   Ui = ZM_inv(U, NULL);
    3125       63602 :   l = lg(D); cyc = cgetg(l, t_VEC); /* elementary divisors */
    3126       92316 :   for (j = 1; j < l; j++)
    3127             :   {
    3128       30308 :     gel(cyc,j) = gcoeff(D,j,j); /* strip useless components */
    3129       30308 :     if (is_pm1(gel(cyc,j))) break;
    3130             :   }
    3131       63602 :   l = j;
    3132       63602 :   Ur  = ZM_hnfdivrem(U, D, &Y);
    3133       63601 :   Uir = ZM_hnfdivrem(Ui,W, &X);
    3134             :  /* {x} = logarithmic embedding of x (arch. component)
    3135             :   * NB: [J,z] = idealred(I) --> I = y J, with {y} = - z
    3136             :   * G = g Uir - {Ga},  Uir = Ui + WX
    3137             :   * g = G Ur  - {ga},  Ur  = U + DY */
    3138       63601 :   G = cgetg(l,t_VEC);
    3139       63602 :   Ga= cgetg(l,t_MAT);
    3140       63602 :   Ge= cgetg(l,t_COL);
    3141       63602 :   z = init_famat(NULL);
    3142       92316 :   for (j = 1; j < l; j++)
    3143             :   {
    3144       28714 :     GEN I = genback(z, nf, Vbase, gel(Uir,j));
    3145       28714 :     gel(G,j) = gel(I,1); /* generator, order cyc[j] */
    3146       28714 :     gel(Ge,j)= gel(I,2);
    3147       28714 :     gel(Ga,j)= nf_cxlog(nf, gel(I,2), prec);
    3148       28714 :     if (!gel(Ga,j)) pari_err_PREC("class_group_gen");
    3149             :   }
    3150             :   /* {ga} = {GD}Y + G U - g = {GD}Y - {Ga} U + gW X U
    3151             :                             = gW (X Ur + V Y) - {Ga}Ur */
    3152       63602 :   M2 = ZM_add(ZM_mul(X,Ur), ZM_mul(V,Y));
    3153       63602 :   setlg(cyc,l); setlg(V,l); setlg(D,l);
    3154             :   /* G D =: {GD} = g (Ui + W X) D - {Ga}D = g W (V + X D) - {Ga}D
    3155             :    * NB: Ui D = W V. gW is given by (first l-1 cols of) C */
    3156       63602 :   M1 = ZM_add(V, ZM_mul(X,D));
    3157       63600 :   *pclg2 = get_clg2(cyc, Ga, C, Ur, Ge, M1, M2);
    3158       63602 :   return mkvec3(ZV_prod(cyc), cyc, G);
    3159             : }
    3160             : 
    3161             : /* compute principal ideals corresponding to (gen[i]^cyc[i]) */
    3162             : static GEN
    3163        4969 : makecycgen(GEN bnf)
    3164             : {
    3165        4969 :   GEN cyc = bnf_get_cyc(bnf), gen = bnf_get_gen(bnf), nf = bnf_get_nf(bnf);
    3166        4969 :   GEN h, y, GD = bnf_get_GD(bnf), W = bnf_get_W(bnf); /* HNF */
    3167        4969 :   GEN Sunits = bnf_get_sunits(bnf);
    3168        4969 :   GEN X = Sunits? gel(Sunits,1): NULL, C = Sunits? gel(Sunits,3): NULL;
    3169             :   long e, i, l;
    3170             : 
    3171        4969 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building cycgen)");
    3172        4969 :   h = cgetg_copy(gen, &l);
    3173       11639 :   for (i = 1; i < l; i++)
    3174             :   {
    3175        6670 :     GEN gi = gel(gen,i), ci = gel(cyc,i);
    3176        6670 :     if (X && equalii(ci, gcoeff(W,i,i)))
    3177             :     {
    3178             :       long j;
    3179        8616 :       for (j = i+1; j < l; j++)
    3180        3254 :         if (signe(gcoeff(W,i,j))) break;
    3181        5542 :       if (j == i) { gel(h,i) = mkmat2(X, gel(C,i)); continue; }
    3182             :     }
    3183        6670 :     if (abscmpiu(ci, 5) < 0)
    3184             :     {
    3185        5557 :       GEN N = ZM_det_triangular(gi);
    3186        5557 :       y = isprincipalarch(bnf,gel(GD,i), N, ci, gen_1, &e);
    3187        5557 :       if (y && fact_ok(nf,y,NULL,mkvec(gi),mkvec(ci)))
    3188             :       {
    3189        4562 :         gel(h,i) = to_famat_shallow(y,gen_1);
    3190        4562 :         continue;
    3191             :       }
    3192             :     }
    3193        2108 :     y = isprincipalfact(bnf, NULL, mkvec(gi), mkvec(ci), nf_GENMAT|nf_FORCE);
    3194        2108 :     gel(h,i) = gel(y,2);
    3195             :   }
    3196        4969 :   return h;
    3197             : }
    3198             : 
    3199             : static GEN
    3200          77 : get_y(GEN bnf, GEN W, GEN B, GEN C, GEN pFB, long j)
    3201             : {
    3202          77 :   GEN y, nf  = bnf_get_nf(bnf);
    3203          77 :   long e, lW = lg(W)-1;
    3204          77 :   GEN ex = (j<=lW)? gel(W,j): gel(B,j-lW);
    3205          77 :   GEN P = (j<=lW)? NULL: gel(pFB,j);
    3206          77 :   if (C)
    3207             :   { /* archimedean embeddings known: cheap trial */
    3208          77 :     GEN Nx = get_norm_fact_primes(pFB, ex, P);
    3209          77 :     y = isprincipalarch(bnf,gel(C,j), Nx,gen_1, gen_1, &e);
    3210          77 :     if (y && fact_ok(nf,y,P,pFB,ex)) return y;
    3211             :   }
    3212           0 :   y = isprincipalfact_or_fail(bnf, P, pFB, ex);
    3213           0 :   return typ(y) == t_INT? y: gel(y,2);
    3214             : }
    3215             : /* compute principal ideals corresponding to bnf relations */
    3216             : static GEN
    3217          21 : makematal(GEN bnf)
    3218             : {
    3219          21 :   GEN W = bnf_get_W(bnf), B = bnf_get_B(bnf), C = bnf_get_C(bnf);
    3220             :   GEN pFB, ma, retry;
    3221          21 :   long lma, j, prec = 0;
    3222             : 
    3223          21 :   if (DEBUGLEVEL) pari_warn(warner,"completing bnf (building matal)");
    3224          21 :   lma=lg(W)+lg(B)-1;
    3225          21 :   pFB = bnf_get_vbase(bnf);
    3226          21 :   ma = cgetg(lma,t_VEC);
    3227          21 :   retry = vecsmalltrunc_init(lma);
    3228          98 :   for (j=lma-1; j>0; j--)
    3229             :   {
    3230          77 :     pari_sp av = avma;
    3231          77 :     GEN y = get_y(bnf, W, B, C, pFB, j);
    3232          77 :     if (typ(y) == t_INT)
    3233             :     {
    3234           0 :       long E = itos(y);
    3235           0 :       if (DEBUGLEVEL>1) err_printf("\n%ld done later at prec %ld\n",j,E);
    3236           0 :       set_avma(av);
    3237           0 :       vecsmalltrunc_append(retry, j);
    3238           0 :       if (E > prec) prec = E;
    3239             :     }
    3240             :     else
    3241             :     {
    3242          77 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3243          77 :       gel(ma,j) = gerepileupto(av,y);
    3244             :     }
    3245             :   }
    3246          21 :   if (prec)
    3247             :   {
    3248           0 :     long k, l = lg(retry);
    3249           0 :     GEN y, nf = bnf_get_nf(bnf);
    3250           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"makematal",prec);
    3251           0 :     nf = nfnewprec_shallow(nf,prec);
    3252           0 :     bnf = Buchall(nf, nf_FORCE, prec);
    3253           0 :     if (DEBUGLEVEL) err_printf("makematal, adding missing entries:");
    3254           0 :     for (k=1; k<l; k++)
    3255             :     {
    3256           0 :       pari_sp av = avma;
    3257           0 :       long j = retry[k];
    3258           0 :       y = get_y(bnf,W,B,NULL, pFB, j);
    3259           0 :       if (typ(y) == t_INT) pari_err_PREC("makematal");
    3260           0 :       if (DEBUGLEVEL>1) err_printf("%ld ",j);
    3261           0 :       gel(ma,j) = gerepileupto(av,y);
    3262             :     }
    3263             :   }
    3264          21 :   if (DEBUGLEVEL>1) err_printf("\n");
    3265          21 :   return ma;
    3266             : }
    3267             : 
    3268             : enum { MATAL = 1, CYCGEN, UNITS };
    3269             : GEN
    3270       26739 : bnf_build_cycgen(GEN bnf)
    3271       26739 : { return obj_checkbuild(bnf, CYCGEN, &makecycgen); }
    3272             : GEN
    3273          21 : bnf_build_matalpha(GEN bnf)
    3274          21 : { return obj_checkbuild(bnf, MATAL, &makematal); }
    3275             : GEN
    3276       31942 : bnf_build_units(GEN bnf)
    3277       31942 : { return obj_checkbuild(bnf, UNITS, &makeunits); }
    3278             : 
    3279             : /* return fu in compact form if available; in terms of a fixed basis
    3280             :  * of S-units */
    3281             : GEN
    3282          70 : bnf_compactfu_mat(GEN bnf)
    3283             : {
    3284          70 :   GEN X, U, SUnits = bnf_get_sunits(bnf);
    3285          70 :   if (!SUnits) return NULL;
    3286          70 :   X = gel(SUnits,1);
    3287          70 :   U = gel(SUnits,2); ZM_remove_unused(&U, &X);
    3288          70 :   return mkvec2(X, U);
    3289             : }
    3290             : /* return fu in compact form if available; individually as famat */
    3291             : GEN
    3292       37127 : bnf_compactfu(GEN bnf)
    3293             : {
    3294       37127 :   GEN fu, X, U, SUnits = bnf_get_sunits(bnf);
    3295             :   long i, l;
    3296       37127 :   if (!SUnits) return NULL;
    3297       36896 :   X = gel(SUnits,1);
    3298       36896 :   U = gel(SUnits,2); l = lg(U); fu = cgetg(l, t_VEC);
    3299       60178 :   for (i = 1; i < l; i++)
    3300       23281 :     gel(fu,i) = famat_remove_trivial(mkmat2(X, gel(U,i)));
    3301       36897 :   return fu;
    3302             : }
    3303             : /* return expanded fu if available */
    3304             : GEN
    3305      263578 : bnf_has_fu(GEN bnf)
    3306             : {
    3307      263578 :   GEN fu = obj_check(bnf, UNITS);
    3308      263578 :   if (fu) return vecsplice(fu, 1);
    3309      262783 :   fu = bnf_get_fu_nocheck(bnf);
    3310      262780 :   return (typ(fu) == t_MAT)? NULL: fu;
    3311             : }
    3312             : /* return expanded fu if available; build if cheap */
    3313             : GEN
    3314      263297 : bnf_build_cheapfu(GEN bnf)
    3315             : {
    3316             :   GEN fu, SUnits;
    3317      263297 :   if ((fu = bnf_has_fu(bnf))) return fu;
    3318         142 :   if ((SUnits = bnf_get_sunits(bnf)))
    3319             :   {
    3320         142 :     pari_sp av = avma;
    3321         142 :     long e = gexpo(real_i(bnf_get_logfu(bnf)));
    3322         142 :     set_avma(av); if (e < 13) return vecsplice(bnf_build_units(bnf), 1);
    3323             :   }
    3324          77 :   return NULL;
    3325             : }
    3326             : 
    3327             : static GEN
    3328       63700 : get_regulator(GEN A)
    3329             : {
    3330       63700 :   pari_sp av = avma;
    3331             :   GEN R;
    3332             : 
    3333       63700 :   if (lg(A) == 1) return gen_1;
    3334       48503 :   R = det( rowslice(real_i(A), 1, lgcols(A)-2) );
    3335       48503 :   setabssign(R); return gerepileuptoleaf(av, R);
    3336             : }
    3337             : 
    3338             : /* return corrected archimedian components for elts of x (vector)
    3339             :  * (= log(sigma_i(x)) - log(|Nx|) / [K:Q]) */
    3340             : static GEN
    3341          42 : get_archclean(GEN nf, GEN x, long prec, int units)
    3342             : {
    3343          42 :   long k, N, l = lg(x);
    3344          42 :   GEN M = cgetg(l, t_MAT);
    3345             : 
    3346          42 :   if (l == 1) return M;
    3347          28 :   N = nf_get_degree(nf);
    3348         126 :   for (k = 1; k < l; k++)
    3349             :   {
    3350          98 :     pari_sp av = avma;
    3351          98 :     GEN c = nf_cxlog(nf, gel(x,k), prec);
    3352          98 :     if (!c || (!units && !(c = cleanarch(c, N, NULL,prec)))) return NULL;
    3353          98 :     gel(M,k) = gerepilecopy(av, c);
    3354             :   }
    3355          28 :   return M;
    3356             : }
    3357             : static void
    3358          77 : Sunits_archclean(GEN nf, GEN Sunits, GEN *pmun, GEN *pC, long prec)
    3359             : {
    3360          77 :   GEN ipi, M, X = gel(Sunits,1), U = gel(Sunits,2), G = gel(Sunits,3);
    3361          77 :   long k, N = nf_get_degree(nf), l = lg(X);
    3362             : 
    3363          77 :   M = cgetg(l, t_MAT);
    3364        3640 :   for (k = 1; k < l; k++)
    3365        3563 :     if (!(gel(M,k) = nf_cxlog(nf, gel(X,k), prec))) return;
    3366          77 :   ipi = invr(mppi(prec));
    3367          77 :   *pmun = cleanarch(RgM_ZM_mul(M, U), N, ipi, prec); /* not cleanarchunit ! */
    3368          77 :   if (*pmun) *pC = cleanarch(RgM_ZM_mul(M, G), N, ipi, prec);
    3369             : }
    3370             : 
    3371             : GEN
    3372          98 : bnfnewprec_shallow(GEN bnf, long prec)
    3373             : {
    3374          98 :   GEN nf0 = bnf_get_nf(bnf), nf, v, fu, matal, y, A, C;
    3375          98 :   GEN Sunits = bnf_get_sunits(bnf), Ur, Ga, Ge, M1, M2;
    3376          98 :   long r1, r2, prec0 = prec;
    3377             : 
    3378          98 :   nf_get_sign(nf0, &r1, &r2);
    3379          98 :   if (Sunits)
    3380             :   {
    3381          77 :     fu = matal = NULL;
    3382          77 :     prec += nbits2extraprec(gexpo(Sunits));
    3383             :   }
    3384             :   else
    3385             :   {
    3386          21 :     fu = bnf_build_units(bnf);
    3387          21 :     fu = vecslice(fu, 2, lg(fu)-1);
    3388          21 :     if (r1 + r2 > 1) {
    3389          14 :       long e = gexpo(bnf_get_logfu(bnf)) + 1 - TWOPOTBITS_IN_LONG;
    3390          14 :       if (e >= 0) prec += nbits2extraprec(e);
    3391             :     }
    3392          21 :     matal = bnf_build_matalpha(bnf);
    3393             :   }
    3394             : 
    3395          98 :   if (DEBUGLEVEL && prec0 != prec) pari_warn(warnprec,"bnfnewprec",prec);
    3396          98 :   for(C = NULL;;)
    3397           0 :   {
    3398          98 :     pari_sp av = avma;
    3399          98 :     nf = nfnewprec_shallow(nf0,prec);
    3400          98 :     if (Sunits)
    3401          77 :       Sunits_archclean(nf, Sunits, &A, &C, prec);
    3402             :     else
    3403             :     {
    3404          21 :       A = get_archclean(nf, fu, prec, 1);
    3405          21 :       if (A) C = get_archclean(nf, matal, prec, 0);
    3406             :     }
    3407          98 :     if (C) break;
    3408           0 :     set_avma(av); prec = precdbl(prec);
    3409           0 :     if (DEBUGLEVEL) pari_warn(warnprec,"bnfnewprec(extra)",prec);
    3410             :   }
    3411          98 :   y = leafcopy(bnf);
    3412          98 :   gel(y,3) = A;
    3413          98 :   gel(y,4) = C;
    3414          98 :   gel(y,7) = nf;
    3415          98 :   gel(y,8) = v = leafcopy(gel(bnf,8));
    3416          98 :   gel(v,2) = get_regulator(A);
    3417          98 :   v = gel(bnf,9);
    3418          98 :   if (lg(v) < 7) pari_err_TYPE("bnfnewprec [obsolete bnf format]", bnf);
    3419          98 :   Ur = gel(v,1);
    3420          98 :   Ge = gel(v,4);
    3421          98 :   Ga = nfV_cxlog(nf, Ge, prec);
    3422          98 :   M1 = gel(v,5);
    3423          98 :   M2 = gel(v,6);
    3424          98 :   gel(y,9) = get_clg2(bnf_get_cyc(bnf), Ga, C, Ur, Ge, M1, M2);
    3425          98 :   return y;
    3426             : }
    3427             : GEN
    3428          21 : bnfnewprec(GEN bnf, long prec)
    3429             : {
    3430          21 :   pari_sp av = avma;
    3431          21 :   return gerepilecopy(av, bnfnewprec_shallow(checkbnf(bnf), prec));
    3432             : }
    3433             : 
    3434             : GEN
    3435           0 : bnrnewprec_shallow(GEN bnr, long prec)
    3436             : {
    3437           0 :   GEN y = cgetg(7,t_VEC);
    3438             :   long i;
    3439           0 :   gel(y,1) = bnfnewprec_shallow(bnr_get_bnf(bnr), prec);
    3440           0 :   for (i=2; i<7; i++) gel(y,i) = gel(bnr,i);
    3441           0 :   return y;
    3442             : }
    3443             : GEN
    3444           7 : bnrnewprec(GEN bnr, long prec)
    3445             : {
    3446           7 :   GEN y = cgetg(7,t_VEC);
    3447             :   long i;
    3448           7 :   checkbnr(bnr);
    3449           7 :   gel(y,1) = bnfnewprec(bnr_get_bnf(bnr), prec);
    3450          42 :   for (i=2; i<7; i++) gel(y,i) = gcopy(gel(bnr,i));
    3451           7 :   return y;
    3452             : }
    3453             : 
    3454             : static GEN
    3455       64729 : buchall_end(GEN nf,GEN res, GEN clg2, GEN W, GEN B, GEN A, GEN C,GEN Vbase)
    3456             : {
    3457       64729 :   GEN z = obj_init(9, 3);
    3458       64729 :   gel(z,1) = W;
    3459       64729 :   gel(z,2) = B;
    3460       64729 :   gel(z,3) = A;
    3461       64729 :   gel(z,4) = C;
    3462       64729 :   gel(z,5) = Vbase;
    3463       64729 :   gel(z,6) = gen_0;
    3464       64729 :   gel(z,7) = nf;
    3465       64729 :   gel(z,8) = res;
    3466       64729 :   gel(z,9) = clg2;
    3467       64729 :   return z;
    3468             : }
    3469             : 
    3470             : GEN
    3471        2541 : bnfinit0(GEN P, long flag, GEN data, long prec)
    3472             : {
    3473        2541 :   double c1 = 0., c2 = 0.;
    3474        2541 :   long fl, relpid = BNF_RELPID;
    3475             : 
    3476        2541 :   if (data)
    3477             :   {
    3478          21 :     long lx = lg(data);
    3479          21 :     if (typ(data) != t_VEC || lx > 5) pari_err_TYPE("bnfinit",data);
    3480          21 :     switch(lx)
    3481             :     {
    3482           0 :       case 4: relpid = itos(gel(data,3));
    3483          14 :       case 3: c2 = gtodouble(gel(data,2));
    3484          21 :       case 2: c1 = gtodouble(gel(data,1));
    3485             :     }
    3486             :   }
    3487        2541 :   switch(flag)
    3488             :   {
    3489        1715 :     case 2:
    3490        1715 :     case 0: fl = 0; break;
    3491         826 :     case 1: fl = nf_FORCE; break;
    3492           0 :     default: pari_err_FLAG("bnfinit");
    3493             :       return NULL; /* LCOV_EXCL_LINE */
    3494             :   }
    3495        2541 :   return Buchall_param(P, c1, c2, relpid, fl, prec);
    3496             : }
    3497             : GEN
    3498       62180 : Buchall(GEN P, long flag, long prec)
    3499       62180 : { return Buchall_param(P, 0., 0., BNF_RELPID, flag & nf_FORCE, prec); }
    3500             : 
    3501             : static GEN
    3502        1127 : Buchall_deg1(GEN nf)
    3503             : {
    3504        1127 :   GEN v = cgetg(1,t_VEC), m = cgetg(1,t_MAT);
    3505        1127 :   GEN res, W, A, B, C, Vbase = cgetg(1,t_COL);
    3506        1127 :   GEN fu = v, R = gen_1, zu = mkvec2(gen_2, gen_m1);
    3507        1127 :   GEN clg1 = mkvec3(gen_1,v,v), clg2 = mkvecn(6, m,m,m,v,m,m);
    3508             : 
    3509        1127 :   W = A = B = C = m; res = mkvec5(clg1, R, gen_1, zu, fu);
    3510        1127 :   return buchall_end(nf,res,clg2,W,B,A,C,Vbase);
    3511             : }
    3512             : 
    3513             : /* return (small set of) indices of columns generating the same lattice as x.
    3514             :  * Assume HNF(x) is inexpensive (few rows, many columns).
    3515             :  * Dichotomy approach since interesting columns may be at the very end */
    3516             : GEN
    3517       63601 : extract_full_lattice(GEN x)
    3518             : {
    3519       63601 :   long dj, j, k, l = lg(x);
    3520             :   GEN h, h2, H, v;
    3521             : 
    3522       63601 :   if (l < 200) return NULL; /* not worth it */
    3523             : 
    3524          21 :   v = vecsmalltrunc_init(l);
    3525          21 :   H = ZM_hnf(x);
    3526          21 :   h = cgetg(1, t_MAT);
    3527          21 :   dj = 1;
    3528         685 :   for (j = 1; j < l; )
    3529             :   {
    3530         685 :     pari_sp av = avma;
    3531         685 :     long lv = lg(v);
    3532             : 
    3533        5495 :     for (k = 0; k < dj; k++) v[lv+k] = j+k;
    3534         685 :     setlg(v, lv + dj);
    3535         685 :     h2 = ZM_hnf(vecpermute(x, v));
    3536         685 :     if (ZM_equal(h, h2))
    3537             :     { /* these dj columns can be eliminated */
    3538         359 :       set_avma(av); setlg(v, lv);
    3539         359 :       j += dj;
    3540         359 :       if (j >= l) break;
    3541         359 :       dj <<= 1;
    3542         359 :       if (j + dj >= l) { dj = (l - j) >> 1; if (!dj) dj = 1; }
    3543             :     }
    3544         326 :     else if (dj > 1)
    3545             :     { /* at least one interesting column, try with first half of this set */
    3546         203 :       set_avma(av); setlg(v, lv);
    3547         203 :       dj >>= 1; /* > 0 */
    3548             :     }
    3549             :     else
    3550             :     { /* this column should be kept */
    3551         123 :       if (ZM_equal(h2, H)) break;
    3552         102 :       h = h2; j++;
    3553             :     }
    3554             :   }
    3555          21 :   return v;
    3556             : }
    3557             : 
    3558             : static void
    3559       63625 : init_rel(RELCACHE_t *cache, FB_t *F, long add_need)
    3560             : {
    3561       63625 :   const long n = F->KC + add_need; /* expected # of needed relations */
    3562             :   long i, j, k, p;
    3563             :   GEN c, P;
    3564             :   GEN R;
    3565             : 
    3566       63625 :   if (DEBUGLEVEL) err_printf("KCZ = %ld, KC = %ld, n = %ld\n", F->KCZ,F->KC,n);
    3567       63625 :   reallocate(cache, 10*n + 50); /* make room for lots of relations */
    3568       63625 :   cache->chk = cache->base;
    3569       63625 :   cache->end = cache->base + n;
    3570       63625 :   cache->relsup = add_need;
    3571       63625 :   cache->last = cache->base;
    3572       63625 :   cache->missing = lg(cache->basis) - 1;
    3573      302665 :   for (i = 1; i <= F->KCZ; i++)
    3574             :   { /* trivial relations (p) = prod P^e */
    3575      239040 :     p = F->FB[i]; P = gel(F->LV,p);
    3576      239040 :     if (!isclone(P)) continue;
    3577             : 
    3578             :     /* all prime divisors in FB */
    3579      166632 :     c = zero_Flv(F->KC); k = F->iLP[p];
    3580      166632 :     R = c; c += k;
    3581      532007 :     for (j = lg(P)-1; j; j--) c[j] = pr_get_e(gel(P,j));
    3582      166632 :     add_rel(cache, F, R, k+1, pr_get_p(gel(P,1)), 0);
    3583             :   }
    3584       63625 : }
    3585             : 
    3586             : /* Let z = \zeta_n in nf. List of not-obviously-dependent generators for
    3587             :  * cyclotomic units modulo torsion in Q(z) [independent when n a prime power]:
    3588             :  * - z^a - 1,  n/(a,n) not a prime power, a \nmid n unless a=1,  1 <= a < n/2
    3589             :  * - (Z^a - 1)/(Z - 1),  p^k || n, Z = z^{n/p^k}, (p,a) = 1, 1 < a <= (p^k-1)/2
    3590             :  */
    3591             : GEN
    3592       63625 : nfcyclotomicunits(GEN nf, GEN zu)
    3593             : {
    3594       63625 :   long n = itos(gel(zu, 1)), n2, lP, i, a;
    3595             :   GEN z, fa, P, E, L, mz, powz;
    3596       63625 :   if (n <= 6) return cgetg(1, t_VEC);
    3597             : 
    3598        1897 :   z = algtobasis(nf,gel(zu, 2));
    3599        1896 :   if ((n & 3) == 2) { n = n >> 1; z = ZC_neg(z); } /* ensure n != 2 (mod 4) */
    3600        1896 :   n2 = n/2;
    3601        1896 :   mz = zk_multable(nf, z); /* multiplication by z */
    3602        1897 :   powz = cgetg(n2, t_VEC); gel(powz,1) = z;
    3603        6237 :   for (i = 2; i < n2; i++) gel(powz,i) = ZM_ZC_mul(mz, gel(powz,i-1));
    3604             :   /* powz[i] = z^i */
    3605             : 
    3606        1897 :   L = vectrunc_init(n);
    3607        1897 :   fa = factoru(n);
    3608        1897 :   P = gel(fa,1); lP = lg(P);
    3609        1897 :   E = gel(fa,2);
    3610        4578 :   for (i = 1; i < lP; i++)
    3611             :   { /* second kind */
    3612        2681 :     long p = P[i], k = E[i], pk = upowuu(p,k), pk2 = (pk-1) / 2;
    3613        2681 :     GEN u = gen_1;
    3614        4935 :     for (a = 2; a <= pk2; a++)
    3615             :     {
    3616        2254 :       u = nfadd(nf, u, gel(powz, (n/pk) * (a-1))); /* = (Z^a-1)/(Z-1) */
    3617        2254 :       if (a % p) vectrunc_append(L, u);
    3618             :     }
    3619             :   }
    3620        6104 :   if (lP > 2) for (a = 1; a < n2; a++)
    3621             :   { /* first kind, when n not a prime power */
    3622             :     ulong p;
    3623        4207 :     if (a > 1 && (n % a == 0 || uisprimepower(n/ugcd(a,n), &p))) continue;
    3624        1848 :     vectrunc_append(L, nfadd(nf, gel(powz, a), gen_m1));
    3625             :   }
    3626        1897 :   return L;
    3627             : }
    3628             : static void
    3629       63625 : add_cyclotomic_units(GEN nf, GEN zu, RELCACHE_t *cache, FB_t *F)
    3630             : {
    3631       63625 :   pari_sp av = avma;
    3632       63625 :   GEN L = nfcyclotomicunits(nf, zu);
    3633       63624 :   long i, l = lg(L);
    3634       63624 :   if (l > 1)
    3635             :   {
    3636        1897 :     GEN R = zero_Flv(F->KC);
    3637        5901 :     for(i = 1; i < l; i++) add_rel(cache, F, R, F->KC+1, gel(L,i), 0);
    3638             :   }
    3639       63624 :   set_avma(av);
    3640       63624 : }
    3641             : 
    3642             : static GEN
    3643      103773 : trim_list(FB_t *F)
    3644             : {
    3645      103773 :   pari_sp av = avma;
    3646      103773 :   GEN v, L_jid = F->L_jid, minidx = F->minidx, present = zero_Flv(F->KC);
    3647      103774 :   long i, j, imax = minss(lg(L_jid), F->KC + 1);
    3648             : 
    3649      103774 :   v = cgetg(imax, t_VECSMALL);
    3650     1246047 :   for (i = j = 1; i < imax; i++)
    3651             :   {
    3652     1142273 :     long k = minidx[ L_jid[i] ];
    3653     1142273 :     if (!present[k]) { v[j++] = L_jid[i]; present[k] = 1; }
    3654             :   }
    3655      103774 :   setlg(v, j); return gerepileuptoleaf(av, v);
    3656             : }
    3657             : 
    3658             : static void
    3659        5416 : try_elt(RELCACHE_t *cache, FB_t *F, GEN nf, GEN x, FACT *fact)
    3660             : {
    3661        5416 :   pari_sp av = avma;
    3662             :   GEN R, Nx;
    3663        5416 :   long nz, tx = typ(x);
    3664             : 
    3665        5416 :   if (tx == t_INT || tx == t_FRAC) return;
    3666        5336 :   if (tx != t_COL) x = algtobasis(nf, x);
    3667        5336 :   if (RgV_isscalar(x)) return;
    3668        5336 :   x = Q_primpart(x);
    3669        5336 :   Nx = nfnorm(nf, x);
    3670        5336 :   if (!can_factor(F, nf, NULL, x, Nx, fact)) return;
    3671             : 
    3672             :   /* smooth element */
    3673        5336 :   R = set_fact(F, fact, NULL, &nz);
    3674             :   /* make sure we get maximal rank first, then allow all relations */
    3675        5336 :   (void) add_rel(cache, F, R, nz, x, 0);
    3676        5336 :   set_avma(av);
    3677             : }
    3678             : 
    3679             : static void
    3680       36817 : matenlarge(GEN C, long h)
    3681             : {
    3682       36817 :   GEN _0 = zerocol(h);
    3683             :   long i;
    3684     2600948 :   for (i = lg(C); --i; ) gel(C,i) = shallowconcat(gel(C,i), _0);
    3685       36817 : }
    3686             : 
    3687             : /* E = floating point embeddings */
    3688             : static GEN
    3689       36817 : matbotidembs(RELCACHE_t *cache, GEN E)
    3690             : {
    3691       36817 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3692       36817 :   long j, d = h - w, hE = nbrows(E);
    3693       36817 :   GEN y = cgetg(w+1,t_MAT), _0 = zerocol(h);
    3694      146418 :   for (j = 1; j <= w; j++)
    3695             :   {
    3696      109601 :     GEN c = shallowconcat(gel(E,j), _0);
    3697      109601 :     if (d + j >= 1) gel(c, d + j + hE) = gen_1;
    3698      109601 :     gel(y,j) = c;
    3699             :   }
    3700       36817 :   return y;
    3701             : }
    3702             : static GEN
    3703       62071 : matbotid(RELCACHE_t *cache)
    3704             : {
    3705       62071 :   long w = cache->last - cache->chk, h = cache->last - cache->base;
    3706       62071 :   long j, d = h - w;
    3707       62071 :   GEN y = cgetg(w+1,t_MAT);
    3708      897948 :   for (j = 1; j <= w; j++)
    3709             :   {
    3710      835877 :     GEN c = zerocol(h);
    3711      835877 :     if (d + j >= 1) gel(c, d + j) = gen_1;
    3712      835877 :     gel(y,j) = c;
    3713             :   }
    3714       62071 :   return y;
    3715             : }
    3716             : 
    3717             : static long
    3718          81 : myprecdbl(long prec, GEN C)
    3719             : {
    3720          81 :   long p = prec2nbits(prec) < 1280? precdbl(prec): (long)(prec * 1.5);
    3721          81 :   if (C) p = maxss(p, minss(3*p, prec + nbits2extraprec(gexpo(C))));
    3722          81 :   return p;
    3723             : }
    3724             : 
    3725             : static GEN
    3726       57460 : _nfnewprec(GEN nf, long prec, long *isclone)
    3727             : {
    3728       57460 :   GEN NF = gclone(nfnewprec_shallow(nf, prec));
    3729       57460 :   if (*isclone) gunclone(nf);
    3730       57460 :   *isclone = 1; return NF;
    3731             : }
    3732             : 
    3733             : /* Nrelid = nb relations per ideal, possibly 0. If flag is set, keep data in
    3734             :  * algebraic form. */
    3735             : GEN
    3736       64720 : Buchall_param(GEN P, double cbach, double cbach2, long Nrelid, long flag, long prec)
    3737             : {
    3738             :   pari_timer T;
    3739       64720 :   pari_sp av0 = avma, av, av2;
    3740             :   long PREC, N, R1, R2, RU, low, high, LIMC0, LIMC, LIMC2, LIMCMAX, zc, i;
    3741       64720 :   long LIMres, bit = 0, flag_nfinit = 0;
    3742       64720 :   long nreldep, sfb_trials, need, old_need, precdouble = 0, TRIES = 0;
    3743       64720 :   long nfisclone = 0;
    3744             :   long done_small, small_fail, fail_limit, squash_index, small_norm_prec;
    3745             :   double LOGD, LOGD2, lim;
    3746       64720 :   GEN computed = NULL, fu = NULL, zu, nf, M_sn, D, A, W, R, h, Ce, PERM;
    3747             :   GEN small_multiplier, auts, cyclic, embs, SUnits;
    3748             :   GEN res, L, invhr, B, C, lambda, dep, clg1, clg2, Vbase;
    3749       64720 :   const char *precpb = NULL;
    3750       64720 :   REL_t *old_cache = NULL;
    3751             :   nfmaxord_t nfT;
    3752             :   RELCACHE_t cache;
    3753             :   FB_t F;
    3754             :   GRHcheck_t GRHcheck;
    3755             :   FACT *fact;
    3756             : 
    3757       64720 :   if (DEBUGLEVEL) timer_start(&T);
    3758       64720 :   P = get_nfpol(P, &nf);
    3759       64713 :   if (nf)
    3760        3556 :     D = nf_get_disc(nf);
    3761             :   else
    3762             :   {
    3763       61157 :     nfinit_basic(&nfT, P);
    3764       61170 :     D = nfT.dK;
    3765       61170 :     if (!ZX_is_monic(nfT.T0))
    3766             :     {
    3767          14 :       pari_warn(warner,"nonmonic polynomial in bnfinit, using polredbest");
    3768          14 :       flag_nfinit = nf_RED;
    3769             :     }
    3770             :   }
    3771       64727 :   PREC = maxss(DEFAULTPREC, prec);
    3772       64727 :   N = degpol(P);
    3773       64726 :   if (N <= 1)
    3774             :   {
    3775        1127 :     if (!nf) nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3776        1127 :     return gerepilecopy(av0, Buchall_deg1(nf));
    3777             :   }
    3778       63599 :   D = absi_shallow(D);
    3779       63598 :   LOGD = dbllog2(D) * M_LN2;
    3780       63599 :   LOGD2 = LOGD*LOGD;
    3781       63599 :   LIMCMAX = (long)(4.*LOGD2);
    3782             :   /* In small_norm, LLL reduction produces v0 in I such that
    3783             :    *     T2(v0) <= (4/3)^((n-1)/2) NI^(2/n) disc(K)^(1/n)
    3784             :    * We consider v with T2(v) <= BMULT * T2(v0)
    3785             :    * Hence Nv <= ((4/3)^((n-1)/2) * BMULT / n)^(n/2) NI sqrt(disc(K)).
    3786             :    * NI <= LIMCMAX^2 */
    3787       63599 :   if (nf) PREC = maxss(PREC, nf_get_prec(nf));
    3788       63599 :   PREC = maxss(PREC, nbits2prec((long)(LOGD2 * 0.02) + N*N));
    3789       63598 :   if (DEBUGLEVEL) err_printf("PREC = %ld\n", PREC);
    3790       63598 :   small_norm_prec = nbits2prec( BITS_IN_LONG +
    3791       63598 :     (N/2. * ((N-1)/2.*log(4./3) + log(8/(double)N))
    3792       63598 :      + 2*log((double) LIMCMAX) + LOGD/2) / M_LN2 ); /*enough to compute norms*/
    3793       63600 :   if (small_norm_prec > PREC) PREC = small_norm_prec;
    3794       63600 :   if (!nf)
    3795       60218 :     nf = nfinit_complete(&nfT, flag_nfinit, PREC);
    3796        3382 :   else if (nf_get_prec(nf) < PREC)
    3797         192 :     nf = nfnewprec_shallow(nf, PREC);
    3798       63598 :   M_sn = nf_get_M(nf);
    3799       63598 :   if (PREC > small_norm_prec) M_sn = gprec_w(M_sn, small_norm_prec);
    3800             : 
    3801       63598 :   zu = nfrootsof1(nf);
    3802       63600 :   gel(zu,2) = nf_to_scalar_or_alg(nf, gel(zu,2));
    3803             : 
    3804       63601 :   nf_get_sign(nf, &R1, &R2); RU = R1+R2;
    3805       63601 :   auts = automorphism_matrices(nf, &cyclic);
    3806       63601 :   F.embperm = automorphism_perms(nf_get_M(nf), auts, cyclic, R1, R2, N);
    3807       63601 :   if (DEBUGLEVEL)
    3808             :   {
    3809           0 :     timer_printf(&T, "nfinit & nfrootsof1");
    3810           0 :     err_printf("%s bnf: R1 = %ld, R2 = %ld\nD = %Ps\n",
    3811             :                flag? "Algebraic": "Floating point", R1,R2, D);
    3812             :   }
    3813       63601 :   if (LOGD < 20.)
    3814             :   { /* tiny disc, Minkowski may be smaller than Bach */
    3815       62159 :     lim = exp(-N + R2 * log(4/M_PI) + LOGD/2) * sqrt(2*M_PI*N);
    3816       62159 :     if (lim < 3) lim = 3;
    3817             :   }
    3818             :   else /* to be ignored */
    3819        1442 :     lim = -1;
    3820       63601 :   if (cbach > 12.) {
    3821           0 :     if (cbach2 < cbach) cbach2 = cbach;
    3822           0 :     cbach = 12.;
    3823             :   }
    3824       63601 :   if (cbach < 0.)
    3825           0 :     pari_err_DOMAIN("Buchall","Bach constant","<",gen_0,dbltor(cbach));
    3826             : 
    3827       63601 :   cache.base = NULL; F.subFB = NULL; F.LP = NULL; SUnits = Ce = NULL;
    3828       63601 :   init_GRHcheck(&GRHcheck, N, R1, LOGD);
    3829       63601 :   high = low = LIMC0 = maxss((long)(cbach2*LOGD2), 1);
    3830      310153 :   while (!GRHchk(nf, &GRHcheck, high)) { low = high; high *= 2; }
    3831      246594 :   while (high - low > 1)
    3832             :   {
    3833      182991 :     long test = (low+high)/2;
    3834      182991 :     if (GRHchk(nf, &GRHcheck, test)) high = test; else low = test;
    3835             :   }
    3836       63603 :   LIMC2 = (high == LIMC0+1 && GRHchk(nf, &GRHcheck, LIMC0))? LIMC0: high;
    3837       63603 :   if (LIMC2 > LIMCMAX) LIMC2 = LIMCMAX;
    3838             :   /* Assuming GRH, {P, NP <= LIMC2} generate Cl(K) */
    3839       63603 :   if (DEBUGLEVEL) err_printf("LIMC2 = %ld\n", LIMC2);
    3840       63602 :   LIMC0 = (long)(cbach*LOGD2); /* initial value for LIMC */
    3841       63602 :   LIMC = cbach? LIMC0: LIMC2; /* use {P, NP <= LIMC} as a factorbase */
    3842       63602 :   LIMC = maxss(LIMC, nthideal(&GRHcheck, nf, N));
    3843       63602 :   if (DEBUGLEVEL) timer_printf(&T, "computing Bach constant");
    3844       63602 :   LIMres = primeneeded(N, R1, R2, LOGD);
    3845       63602 :   cache_prime_dec(&GRHcheck, LIMres, nf);
    3846             :   /* invhr ~ 2^r1 (2pi)^r2 / sqrt(D) w * Res(zeta_K, s=1) = 1 / hR */
    3847      127201 :   invhr = gmul(gdiv(gmul2n(powru(mppi(DEFAULTPREC), R2), RU),
    3848       63602 :               mulri(gsqrt(D,DEFAULTPREC),gel(zu,1))),
    3849             :               compute_invres(&GRHcheck, LIMres));
    3850       63602 :   if (DEBUGLEVEL) timer_printf(&T, "computing inverse of hR");
    3851       63602 :   av = avma;
    3852             : 
    3853       65802 : START:
    3854       65802 :   if (DEBUGLEVEL) timer_start(&T);
    3855       65802 :   if (TRIES) LIMC = bnf_increase_LIMC(LIMC,LIMCMAX);
    3856       65802 :   if (DEBUGLEVEL && LIMC > LIMC0)
    3857           0 :     err_printf("%s*** Bach constant: %f\n", TRIES?"\n":"", LIMC/LOGD2);
    3858       65802 :   if (cache.base)
    3859             :   {
    3860             :     REL_t *rel;
    3861       16310 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    3862       16287 :       if (rel->m) i++;
    3863          23 :     computed = cgetg(i, t_VEC);
    3864       16310 :     for (i = 1, rel = cache.base + 1; rel < cache.last; rel++)
    3865       16287 :       if (rel->m) gel(computed, i++) = rel->m;
    3866          23 :     computed = gclone(computed); delete_cache(&cache);
    3867             :   }
    3868       65802 :   TRIES++; set_avma(av);
    3869       65801 :   if (F.LP) delete_FB(&F);
    3870       65801 :   if (LIMC2 < LIMC) LIMC2 = LIMC;
    3871       65801 :   if (DEBUGLEVEL) { err_printf("LIMC = %ld, LIMC2 = %ld\n",LIMC,LIMC2); }
    3872             : 
    3873       65801 :   FBgen(&F, nf, N, LIMC, LIMC2, &GRHcheck);
    3874       65800 :   if (!F.KC) goto START;
    3875       65800 :   av = avma;
    3876       65800 :   subFBgen(&F,auts,cyclic,lim < 0? LIMC2: mindd(lim,LIMC2),MINSFB);
    3877       65801 :   if (lg(F.subFB) == 1) goto START;
    3878       63624 :   if (DEBUGLEVEL)
    3879           0 :     timer_printf(&T, "factorbase (#subFB = %ld) and ideal permutations",
    3880           0 :                      lg(F.subFB)-1);
    3881             : 
    3882       63624 :   fact = (FACT*)stack_malloc((F.KC+1)*sizeof(FACT));
    3883       63624 :   PERM = leafcopy(F.perm); /* to be restored in case of precision increase */
    3884       63624 :   cache.basis = zero_Flm_copy(F.KC,F.KC);
    3885       63625 :   small_multiplier = zero_Flv(F.KC);
    3886       63625 :   done_small = small_fail = squash_index = zc = sfb_trials = nreldep = 0;
    3887       63625 :   fail_limit = F.KC + 1;
    3888       63625 :   W = A = R = NULL;
    3889       63625 :   av2 = avma;
    3890       63625 :   init_rel(&cache, &F, RELSUP + RU-1);
    3891       63625 :   old_need = need = cache.end - cache.last;
    3892       63625 :   add_cyclotomic_units(nf, zu, &cache, &F);
    3893       63624 :   if (DEBUGLEVEL) err_printf("\n");
    3894       63624 :   cache.end = cache.last + need;
    3895             : 
    3896       63624 :   if (computed)
    3897             :   {
    3898        5439 :     for (i = 1; i < lg(computed); i++)
    3899        5416 :       try_elt(&cache, &F, nf, gel(computed, i), fact);
    3900          23 :     gunclone(computed);
    3901          23 :     if (DEBUGLEVEL && i > 1)
    3902           0 :       timer_printf(&T, "including already computed relations");
    3903          23 :     need = 0;
    3904             :   }
    3905             : 
    3906             :   do
    3907             :   {
    3908             :     GEN Ar, C0;
    3909             :     do
    3910             :     {
    3911      103877 :       pari_sp av4 = avma;
    3912      103877 :       if (need > 0)
    3913             :       {
    3914      103773 :         long oneed = cache.end - cache.last;
    3915             :         /* Test below can be true if small_norm did not find enough linearly
    3916             :          * dependent relations */
    3917      103773 :         if (need < oneed) need = oneed;
    3918      103773 :         pre_allocate(&cache, need+lg(auts)-1+(R ? lg(W)-1 : 0));
    3919      103773 :         cache.end = cache.last + need;
    3920      103773 :         F.L_jid = trim_list(&F);
    3921             :       }
    3922      103878 :       if (need > 0 && Nrelid > 0 && (done_small <= F.KC+1 || A) &&
    3923      103691 :           small_fail <= fail_limit &&
    3924      103691 :           cache.last < cache.base + 2*F.KC+2*RU+RELSUP /* heuristic */)
    3925             :       {
    3926       91658 :         long j, k, LIE = (R && lg(W) > 1 && (done_small % 2));
    3927       91658 :         REL_t *last = cache.last;
    3928       91658 :         pari_sp av3 = avma;
    3929             :         GEN p0;
    3930       91658 :         if (LIE)
    3931             :         { /* We have full rank for class group and unit. The following tries to
    3932             :            * improve the prime group lattice by looking for relations involving
    3933             :            * the primes generating the class group. */
    3934        3152 :           long n = lg(W)-1; /* need n relations to squash the class group */
    3935        3152 :           F.L_jid = vecslice(F.perm, 1, n);
    3936        3152 :           cache.end = cache.last + n;
    3937             :           /* Lie to the add_rel subsystem: pretend we miss relations involving
    3938             :            * the primes generating the class group (and only those). */
    3939        3152 :           cache.missing = n;
    3940        9807 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 0;
    3941             :         }
    3942       91658 :         j = done_small % (F.KC+1);
    3943       91658 :         if (j == 0) p0 = NULL;
    3944             :         else
    3945             :         {
    3946       27877 :           p0 = gel(F.LP, j);
    3947       27877 :           if (!A)
    3948             :           { /* Prevent considering both P_iP_j and P_jP_i in small_norm */
    3949             :             /* Not all elements end up in F.L_jid (eliminated by hnfspec/add or
    3950             :              * by trim_list): keep track of which ideals are being considered
    3951             :              * at each run. */
    3952         910 :             long mj = small_multiplier[j];
    3953       11830 :             for (i = k = 1; i < lg(F.L_jid); i++)
    3954       10920 :               if (F.L_jid[i] > mj)
    3955             :               {
    3956       10920 :                 small_multiplier[F.L_jid[i]] = j;
    3957       10920 :                 F.L_jid[k++] = F.L_jid[i];
    3958             :               }
    3959         910 :             setlg(F.L_jid, k);
    3960             :           }
    3961             :         }
    3962       91658 :         if (lg(F.L_jid) > 1)
    3963       91658 :           small_norm(&cache, &F, nf, Nrelid, M_sn, fact, p0);
    3964       91658 :         F.L_jid = F.perm; set_avma(av3);
    3965       91658 :         if (!A && cache.last != last) small_fail = 0; else small_fail++;
    3966       91658 :         if (LIE)
    3967             :         { /* restore add_rel subsystem: undo above lie */
    3968        3152 :           long n = lg(W) - 1;
    3969        9807 :           for ( ; n > 0; n--) mael(cache.basis, F.perm[n], F.perm[n]) = 1;
    3970        3152 :           cache.missing = 0;
    3971             :         }
    3972       91658 :         cache.end = cache.last;
    3973       91658 :         done_small++;
    3974       91658 :         need = F.sfb_chg = 0;
    3975             :       }
    3976      103878 :       if (need > 0)
    3977             :       { /* Random relations */
    3978       12116 :         if (++nreldep > F.MAXDEPSIZESFB) {
    3979         192 :           if (++sfb_trials > SFB_MAX && LIMC < LIMCMAX/2) goto START;
    3980         169 :           F.sfb_chg = sfb_INCREASE;
    3981         169 :           nreldep = 0;
    3982             :         }
    3983       11924 :         else if (!(nreldep % F.MAXDEPSFB))
    3984        3284 :           F.sfb_chg = sfb_CHANGE;
    3985       12093 :         if (F.sfb_chg && !subFB_change(&F)) goto START;
    3986       12093 :         rnd_rel(&cache, &F, nf, fact);
    3987       12093 :         F.L_jid = F.perm;
    3988             :       }
    3989      103855 :       if (DEBUGLEVEL) timer_start(&T);
    3990      103855 :       if (precpb)
    3991             :       {
    3992             :         REL_t *rel;
    3993          81 :         if (DEBUGLEVEL)
    3994             :         {
    3995           0 :           char str[64]; sprintf(str,"Buchall_param (%s)",precpb);
    3996           0 :           pari_warn(warnprec,str,PREC);
    3997             :         }
    3998          81 :         nf = _nfnewprec(nf, PREC, &nfisclone);
    3999          81 :         precdouble++; precpb = NULL;
    4000             : 
    4001          81 :         if (flag)
    4002             :         { /* recompute embs only, no need to redo HNF */
    4003          39 :           long j, le = lg(embs), lC = lg(C);
    4004          39 :           GEN E, M = nf_get_M(nf);
    4005          39 :           set_avma(av4);
    4006       12848 :           for (rel = cache.base+1, i = 1; i < le; i++,rel++)
    4007       12809 :             gel(embs,i) = rel_embed(rel, &F, embs, i, M, RU, R1, PREC);
    4008          39 :           E = RgM_ZM_mul(embs, rowslice(C, RU+1, nbrows(C)));
    4009       12848 :           for (j = 1; j < lC; j++)
    4010       67573 :             for (i = 1; i <= RU; i++) gcoeff(C,i,j) = gcoeff(E,i,j);
    4011          39 :           av4 = avma;
    4012             :         }
    4013             :         else
    4014             :         { /* recompute embs + HNF */
    4015       10318 :           for(i = 1; i < lg(PERM); i++) F.perm[i] = PERM[i];
    4016          42 :           cache.chk = cache.base;
    4017          42 :           W = NULL;
    4018             :         }
    4019          81 :         if (DEBUGLEVEL) timer_printf(&T, "increasing accuracy");
    4020             :       }
    4021      103855 :       set_avma(av4);
    4022      103855 :       if (cache.chk != cache.last)
    4023             :       { /* Reduce relation matrices */
    4024      103305 :         long l = cache.last - cache.chk + 1, j;
    4025      103305 :         GEN mat = cgetg(l, t_MAT);
    4026             :         REL_t *rel;
    4027             : 
    4028     1105623 :         for (j=1,rel = cache.chk + 1; j < l; rel++,j++) gel(mat,j) = rel->R;
    4029      103305 :         if (!flag || W)
    4030             :         {
    4031       41234 :           embs = get_embs(&F, &cache, nf, embs, PREC);
    4032       41234 :           if (DEBUGLEVEL && timer_get(&T) > 1)
    4033           0 :             timer_printf(&T, "floating point embeddings");
    4034             :         }
    4035      103305 :         if (!W)
    4036             :         { /* never reduced before */
    4037       63667 :           C = flag? matbotid(&cache): embs;
    4038       63667 :           W = hnfspec_i(mat, F.perm, &dep, &B, &C, F.subFB ? lg(F.subFB)-1:0);
    4039       63667 :           if (DEBUGLEVEL)
    4040           0 :             timer_printf(&T, "hnfspec [%ld x %ld]", lg(F.perm)-1, l-1);
    4041       63667 :           if (flag)
    4042             :           {
    4043       62071 :             PREC += nbits2extraprec(gexpo(C));
    4044       62071 :             if (nf_get_prec(nf) < PREC) nf = _nfnewprec(nf, PREC, &nfisclone);
    4045       62071 :             embs = get_embs(&F, &cache, nf, embs, PREC);
    4046       62071 :             C = vconcat(RgM_ZM_mul(embs, C), C);
    4047             :           }
    4048       63667 :           if (DEBUGLEVEL)
    4049           0 :             timer_printf(&T, "hnfspec floating points");
    4050             :         }
    4051             :         else
    4052             :         {
    4053       39638 :           long k = lg(embs);
    4054       39638 :           GEN E = vecslice(embs, k-l+1,k-1);
    4055       39638 :           if (flag)
    4056             :           {
    4057       36817 :             E = matbotidembs(&cache, E);
    4058       36817 :             matenlarge(C, cache.last - cache.chk);
    4059             :           }
    4060       39638 :           W = hnfadd_i(W, F.perm, &dep, &B, &C, mat, E);
    4061       39638 :           if (DEBUGLEVEL)
    4062           0 :             timer_printf(&T, "hnfadd (%ld + %ld)", l-1, lg(dep)-1);
    4063             :         }
    4064      103305 :         gerepileall(av2, 5, &W,&C,&B,&dep,&embs);
    4065      103305 :         cache.chk = cache.last;
    4066             :       }
    4067         550 :       else if (!W)
    4068             :       {
    4069           0 :         need = old_need;
    4070           0 :         F.L_jid = vecslice(F.perm, 1, need);
    4071           0 :         continue;
    4072             :       }
    4073      103855 :       need = F.KC - (lg(W)-1) - (lg(B)-1);
    4074      103855 :       if (!need && cache.missing)
    4075             :       { /* The test above will never be true except if 27449|class number.
    4076             :          * Ensure that if we have maximal rank for the ideal lattice, then
    4077             :          * cache.missing == 0. */
    4078          14 :         for (i = 1; cache.missing; i++)
    4079           7 :           if (!mael(cache.basis, i, i))
    4080             :           {
    4081             :             long j;
    4082           7 :             cache.missing--; mael(cache.basis, i, i) = 1;
    4083         427 :             for (j = i+1; j <= F.KC; j++) mael(cache.basis, j, i) = 0;
    4084             :           }
    4085             :       }
    4086      103855 :       zc = (lg(C)-1) - (lg(B)-1) - (lg(W)-1);
    4087      103855 :       if (RU-1-zc > 0) need = minss(need + RU-1-zc, F.KC); /* for units */
    4088      103855 :       if (need)
    4089             :       { /* dependent rows */
    4090         947 :         F.L_jid = vecslice(F.perm, 1, need);
    4091         947 :         vecsmall_sort(F.L_jid);
    4092         947 :         if (need != old_need) { nreldep = 0; old_need = need; }
    4093             :       }
    4094             :       else
    4095             :       { /* If the relation lattice is too small, check will be > 1 and we will
    4096             :          * do a new run of small_norm/rnd_rel asking for 1 relation. This often
    4097             :          * gives a relation involving L_jid[1]. We rotate the first element of
    4098             :          * L_jid in order to increase the probability of finding relations that
    4099             :          * increases the lattice. */
    4100      102908 :         long j, n = lg(W) - 1;
    4101      102908 :         if (n > 1 && squash_index % n)
    4102             :         {
    4103        7102 :           F.L_jid = leafcopy(F.perm);
    4104       30368 :           for (j = 1; j <= n; j++)
    4105       23266 :             F.L_jid[j] = F.perm[1 + (j + squash_index - 1) % n];
    4106             :         }
    4107             :         else
    4108       95806 :           F.L_jid = F.perm;
    4109      102908 :         squash_index++;
    4110             :       }
    4111             :     }
    4112      103855 :     while (need);
    4113             : 
    4114      102908 :     if (!A)
    4115             :     {
    4116       63625 :       small_fail = old_need = 0;
    4117       63625 :       fail_limit = maxss(F.KC / FAIL_DIVISOR, MINFAIL);
    4118             :     }
    4119      102908 :     A = vecslice(C, 1, zc); /* cols corresponding to units */
    4120      102908 :     if (flag) A = rowslice(A, 1, RU);
    4121      102908 :     Ar = real_i(A);
    4122      102908 :     R = compute_multiple_of_R(Ar, RU, N, &need, &bit, &lambda);
    4123      102908 :     if (need < old_need) small_fail = 0;
    4124             : #if 0 /* A good idea if we are indeed stuck but needs tuning */
    4125             :     /* we have computed way more relations than should be necessary */
    4126             :     if (TRIES < 3 && LIMC < LIMCMAX / 8 &&
    4127             :                      cache.last - cache.base > 10 * F.KC) goto START;
    4128             : #endif
    4129      102908 :     old_need = need;
    4130      102908 :     if (!lambda)
    4131          17 :     { precpb = "bestappr"; PREC = myprecdbl(PREC, flag? C: NULL); continue; }
    4132      102891 :     if (!R)
    4133             :     { /* not full rank for units */
    4134       30938 :       if (!need)
    4135           0 :       { precpb = "regulator"; PREC = myprecdbl(PREC, flag? C: NULL); }
    4136       30938 :       continue;
    4137             :     }
    4138       71953 :     if (cache.last==old_cache) { need=1; continue; }
    4139       71889 :     old_cache = cache.last;
    4140       71889 :     h = ZM_det_triangular(W);
    4141       71888 :     if (DEBUGLEVEL) err_printf("\n#### Tentative class number: %Ps\n", h);
    4142       71888 :     i = compute_R(lambda, mulir(h,invhr), &L, &R);
    4143       71888 :     if (DEBUGLEVEL)
    4144             :     {
    4145           0 :       err_printf("\n");
    4146           0 :       timer_printf(&T, "computing regulator and check");
    4147             :     }
    4148       71888 :     switch(i)
    4149             :     {
    4150        8223 :       case fupb_RELAT:
    4151        8223 :         need = 1; /* not enough relations */
    4152        8223 :         continue;
    4153          64 :       case fupb_PRECI: /* prec problem unless we cheat on Bach constant */
    4154          64 :         if ((precdouble&7) == 7 && LIMC <= LIMCMAX/2) goto START;
    4155          64 :         precpb = "compute_R"; PREC = myprecdbl(PREC, flag? C: NULL);
    4156          64 :         continue;
    4157             :     }
    4158             :     /* DONE */
    4159             : 
    4160       63601 :     if (F.KCZ2 > F.KCZ)
    4161             :     {
    4162           7 :       if (F.sfb_chg && !subFB_change(&F)) goto START;
    4163           7 :       if (!be_honest(&F, nf, auts, fact)) goto START;
    4164           7 :       if (DEBUGLEVEL) timer_printf(&T, "to be honest");
    4165             :     }
    4166       63601 :     F.KCZ2 = 0; /* be honest only once */
    4167             : 
    4168             :     /* fundamental units */
    4169             :     {
    4170       63601 :       GEN AU, CU, U, v = extract_full_lattice(L); /* L may be large */
    4171       63601 :       CU = NULL;
    4172       63601 :       if (v) { A = vecpermute(A, v); L = vecpermute(L, v); }
    4173             :       /* arch. components of fund. units */
    4174       63601 :       U = ZM_lll(L, 0.99, LLL_IM);
    4175       63602 :       U = ZM_mul(U, lll(RgM_ZM_mul(real_i(A), U)));
    4176       63602 :       if (DEBUGLEVEL) timer_printf(&T, "units LLL");
    4177       63602 :       AU = RgM_ZM_mul(A, U);
    4178       63602 :       A = cleanarchunit(AU, N, NULL, PREC);
    4179       63602 :       if (!A || lg(A) < RU || expo(gsub(get_regulator(A), R)) > -1)
    4180             :       {
    4181           0 :         long add = nbits2extraprec( gexpo(AU) + 64 ) - gprecision(AU);
    4182           0 :         long t = maxss((PREC-2) * 0.15, add);
    4183           0 :         if (!A && DEBUGLEVEL) err_printf("### Incorrect units lognorm");
    4184           0 :         precpb = "cleanarch"; PREC += maxss(t, EXTRAPREC64); continue;
    4185             :       }
    4186       63602 :       if (flag)
    4187             :       {
    4188       62055 :         long l = lgcols(C) - RU;
    4189             :         REL_t *rel;
    4190       62055 :         SUnits = cgetg(l, t_COL);
    4191      997474 :         for (rel = cache.base+1, i = 1; i < l; i++,rel++)
    4192      935420 :           set_rel_alpha(rel, auts, SUnits, i);
    4193       62054 :         if (RU > 1)
    4194             :         {
    4195       47382 :           GEN c = v? vecpermute(C,v): vecslice(C,1,zc);
    4196       47383 :           CU = ZM_mul(rowslice(c, RU+1, nbrows(c)), U);
    4197             :         }
    4198             :       }
    4199       63601 :       if (DEBUGLEVEL) err_printf("\n#### Computing fundamental units\n");
    4200       63601 :       fu = getfu(nf, &A, CU? &U: NULL, PREC);
    4201       63602 :       CU = CU? ZM_mul(CU, U): cgetg(1, t_MAT);
    4202       63601 :       if (DEBUGLEVEL) timer_printf(&T, "getfu");
    4203       63601 :       Ce = vecslice(C, zc+1, lg(C)-1);
    4204       63602 :       if (flag) SUnits = mkvec4(SUnits, CU, rowslice(Ce, RU+1, nbrows(Ce)),
    4205             :                                 utoipos(LIMC));
    4206             :     }
    4207             :     /* class group generators */
    4208       63602 :     if (flag) Ce = rowslice(Ce, 1, RU);
    4209       63601 :     C0 = Ce; Ce = cleanarch(Ce, N, NULL, PREC);
    4210       63602 :     if (!Ce) {
    4211           0 :       long add = nbits2extraprec( gexpo(C0) + 64 ) - gprecision(C0);
    4212           0 :       precpb = "cleanarch"; PREC += maxss(add, 1);
    4213             :     }
    4214       63602 :     if (DEBUGLEVEL) timer_printf(&T, "cleanarch");
    4215      102908 :   } while (need || precpb);
    4216             : 
    4217       63602 :   Vbase = vecpermute(F.LP, F.perm);
    4218       63602 :   if (!fu) fu = cgetg(1, t_MAT);
    4219       63602 :   if (!SUnits) SUnits = gen_1;
    4220       63602 :   clg1 = class_group_gen(nf,W,Ce,Vbase,PREC, &clg2);
    4221       63602 :   res = mkvec5(clg1, R, SUnits, zu, fu);
    4222       63602 :   res = buchall_end(nf,res,clg2,W,B,A,Ce,Vbase);
    4223       63602 :   delete_FB(&F);
    4224       63602 :   res = gerepilecopy(av0, res);
    4225       63602 :   if (flag) obj_insert_shallow(res, MATAL, cgetg(1,t_VEC));
    4226       63601 :   if (nfisclone) gunclone(nf);
    4227       63602 :   delete_cache(&cache);
    4228       63602 :   free_GRHcheck(&GRHcheck);
    4229       63602 :   return res;
    4230             : }

Generated by: LCOV version 1.14