Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - galconj.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21196-f12677d) Lines: 1601 1678 95.4 %
Date: 2017-10-22 06:23:24 Functions: 99 101 98.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000-2003  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : /*************************************************************************/
      17             : /**                                                                     **/
      18             : /**                           GALOIS CONJUGATES                         **/
      19             : /**                                                                     **/
      20             : /*************************************************************************/
      21             : 
      22             : static int
      23         119 : is2sparse(GEN x)
      24             : {
      25         119 :   long i, l = lg(x);
      26         119 :   if (odd(l-3)) return 0;
      27         392 :   for(i=3; i<l; i+=2)
      28         329 :     if (signe(gel(x,i))) return 0;
      29          63 :   return 1;
      30             : }
      31             : 
      32             : static GEN
      33         637 : galoisconj1(GEN nf)
      34             : {
      35         637 :   GEN x = get_nfpol(nf, &nf), f = nf? nf : x, y, z;
      36         637 :   long i, lz, v = varn(x), nbmax;
      37         637 :   pari_sp av = avma;
      38         637 :   RgX_check_ZX(x, "nfgaloisconj");
      39         637 :   nbmax = numberofconjugates(x, 2);
      40         637 :   if (nbmax==1) retmkcol(pol_x(v));
      41         169 :   if (nbmax==2 && is2sparse(x))
      42             :   {
      43          63 :     GEN res = cgetg(3,t_COL);
      44          63 :     gel(res,1) = deg1pol_shallow(gen_m1, gen_0, v);
      45          63 :     gel(res,2) = pol_x(v);
      46          63 :     return res;
      47             :   }
      48         106 :   x = leafcopy(x); setvarn(x, fetch_var_higher());
      49         106 :   z = nfroots(f, x); lz = lg(z);
      50         106 :   y = cgetg(lz, t_COL);
      51         621 :   for (i = 1; i < lz; i++)
      52             :   {
      53         515 :     GEN t = lift(gel(z,i));
      54         515 :     if (typ(t) == t_POL) setvarn(t, v);
      55         515 :     gel(y,i) = t;
      56             :   }
      57         106 :   (void)delete_var();
      58         106 :   return gerepileupto(av, y);
      59             : }
      60             : 
      61             : /*************************************************************************/
      62             : /**                                                                     **/
      63             : /**                           GALOISCONJ4                               **/
      64             : /**                                                                     **/
      65             : /*************************************************************************/
      66             : /*DEBUGLEVEL:
      67             :   1: timing
      68             :   2: outline
      69             :   4: complete outline
      70             :   6: detail
      71             :   7: memory
      72             :   9: complete detail
      73             : */
      74             : struct galois_borne {
      75             :   GEN  l;
      76             :   long valsol;
      77             :   long valabs;
      78             :   GEN  bornesol;
      79             :   GEN  ladicsol;
      80             :   GEN  ladicabs;
      81             : };
      82             : struct galois_lift {
      83             :   GEN  T;
      84             :   GEN  den;
      85             :   GEN  p;
      86             :   GEN  L;
      87             :   GEN  Lden;
      88             :   long e;
      89             :   GEN  Q;
      90             :   GEN  TQ;
      91             :   struct galois_borne *gb;
      92             : };
      93             : struct galois_testlift {
      94             :   long n;
      95             :   long f;
      96             :   long g;
      97             :   GEN  bezoutcoeff;
      98             :   GEN  pauto;
      99             :   GEN  C;
     100             :   GEN  Cd;
     101             : };
     102             : struct galois_test { /* data for permutation test */
     103             :   GEN order; /* order of tests pour galois_test_perm */
     104             :   GEN borne, lborne; /* coefficient bounds */
     105             :   GEN ladic;
     106             :   GEN PV; /* NULL or vector of test matrices (Vmatrix) */
     107             :   GEN TM; /* transpose of M */
     108             :   GEN L; /* p-adic roots, known mod ladic */
     109             :   GEN M; /* vandermonde inverse */
     110             : };
     111             : /* result of the study of Frobenius degrees */
     112             : enum ga_code {ga_all_normal=1,ga_ext_2=2,ga_non_wss=4};
     113             : struct galois_analysis {
     114             :   long p; /* prime to be lifted */
     115             :   long deg; /* degree of the lift */
     116             :   long mindeg; /* minimal acceptable degree */
     117             :   long ord;
     118             :   long l; /* l: prime number such that T is totally split mod l */
     119             :   long p4;
     120             :   enum ga_code group;
     121             : };
     122             : struct galois_frobenius {
     123             :   long p;
     124             :   long fp;
     125             :   long deg;
     126             :   GEN Tmod;
     127             :   GEN psi;
     128             : };
     129             : 
     130             : /* given complex roots L[i], i <= n of some monic T in C[X], return
     131             :  * the T'(L[i]), computed stably via products of differences */
     132             : static GEN
     133        5539 : vandermondeinverseprep(GEN L)
     134             : {
     135        5539 :   long i, j, n = lg(L);
     136             :   GEN V;
     137        5539 :   V = cgetg(n, t_VEC);
     138       43489 :   for (i = 1; i < n; i++)
     139             :   {
     140       37950 :     pari_sp ltop = avma;
     141       37950 :     GEN W = cgetg(n-1,t_VEC);
     142       37950 :     long k = 1;
     143      823736 :     for (j = 1; j < n; j++)
     144      785786 :       if (i != j) gel(W, k++) = gsub(gel(L,i),gel(L,j));
     145       37950 :     gel(V,i) = gerepileupto(ltop, RgV_prod(W));
     146             :   }
     147        5539 :   return V;
     148             : }
     149             : 
     150             : /* Compute the inverse of the van der Monde matrix of T multiplied by den */
     151             : GEN
     152        5448 : vandermondeinverse(GEN L, GEN T, GEN den, GEN prep)
     153             : {
     154        5448 :   pari_sp ltop = avma;
     155        5448 :   long i, n = lg(L)-1;
     156             :   GEN M, P;
     157        5448 :   if (!prep) prep = vandermondeinverseprep(L);
     158        5448 :   if (den && !equali1(den)) T = RgX_Rg_mul(T,den);
     159        5448 :   M = cgetg(n+1, t_MAT);
     160       42159 :   for (i = 1; i <= n; i++)
     161             :   {
     162       36711 :     P = RgX_Rg_div(RgX_div_by_X_x(T, gel(L,i), NULL), gel(prep,i));
     163       36711 :     gel(M,i) = RgX_to_RgC(P,n);
     164             :   }
     165        5448 :   return gerepilecopy(ltop, M);
     166             : }
     167             : 
     168             : /* #r = r1 + r2 */
     169             : GEN
     170          80 : embed_roots(GEN ro, long r1)
     171             : {
     172          80 :   long r2 = lg(ro)-1-r1;
     173             :   GEN L;
     174          80 :   if (!r2) L = ro;
     175             :   else
     176             :   {
     177          50 :     long i,j, N = r1+2*r2;
     178          50 :     L = cgetg(N+1, t_VEC);
     179          50 :     for (i = 1; i <= r1; i++) gel(L,i) = gel(ro,i);
     180         247 :     for (j = i; j <= N; i++)
     181             :     {
     182         197 :       GEN z = gel(ro,i);
     183         197 :       gel(L,j++) = z;
     184         197 :       gel(L,j++) = mkcomplex(gel(z,1), gneg(gel(z,2)));
     185             :     }
     186             :   }
     187          80 :   return L;
     188             : }
     189             : GEN
     190       19453 : embed_disc(GEN z, long r1, long prec)
     191             : {
     192       19453 :   pari_sp av = avma;
     193       19453 :   GEN t = real_1(prec);
     194       19453 :   long i, j, n = lg(z)-1, r2 = n-r1;
     195       92883 :   for (i = 1; i < r1; i++)
     196             :   {
     197       73430 :     GEN zi = gel(z,i);
     198       73430 :     for (j = i+1; j <= r1; j++) t = gmul(t, gsub(zi, gel(z,j)));
     199             :   }
     200       91266 :   for (j = r1+1; j <= n; j++)
     201             :   {
     202       71813 :     GEN zj = gel(z,j), a = gel(zj,1), b = gel(zj,2), b2 = gsqr(b);
     203       78785 :     for (i = 1; i <= r1; i++)
     204             :     {
     205        6972 :       GEN zi = gel(z,i);
     206        6972 :       t = gmul(t, gadd(gsqr(gsub(zi, a)), b2));
     207             :     }
     208       71813 :     t = gmul(t, b);
     209             :   }
     210       19453 :   if (r2) t = gmul2n(t, r2);
     211       19453 :   if (r2 > 1)
     212             :   {
     213       11263 :     GEN T = real_1(prec);
     214       71358 :     for (i = r1+1; i < n; i++)
     215             :     {
     216       60095 :       GEN zi = gel(z,i), a = gel(zi,1), b = gel(zi,2);
     217      470687 :       for (j = i+1; j <= n; j++)
     218             :       {
     219      410592 :         GEN zj = gel(z,j), c = gel(zj,1), d = gel(zj,2);
     220      410592 :         GEN f = gsqr(gsub(a,c)), g = gsqr(gsub(b,d)), h = gsqr(gadd(b,d));
     221      410592 :         T = gmul(T, gmul(gadd(f,g), gadd(f,h)));
     222             :       }
     223             :     }
     224       11263 :     t = gmul(t, T);
     225             :   }
     226       19453 :   t = gsqr(t);
     227       19453 :   if (odd(r2)) t = gneg(t);
     228       19453 :   return gerepileupto(av, t);
     229             : }
     230             : 
     231             : /* Compute bound for the coefficients of automorphisms.
     232             :  * T a ZX, dn a t_INT denominator or NULL */
     233             : GEN
     234        5539 : initgaloisborne(GEN T, GEN dn, long prec, GEN *ptL, GEN *ptprep, GEN *ptdis)
     235             : {
     236             :   GEN L, prep, den, nf, r;
     237             :   pari_timer ti;
     238             : 
     239        5539 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     240        5539 :   T = get_nfpol(T, &nf);
     241        5539 :   r = nf ? nf_get_roots(nf) : NULL;
     242        5539 :   if (nf &&  precision(gel(r, 1)) >= prec)
     243          80 :     L = embed_roots(r, nf_get_r1(nf));
     244             :   else
     245        5459 :     L = QX_complex_roots(T, prec);
     246        5539 :   if (DEBUGLEVEL>=4) timer_printf(&ti,"roots");
     247        5539 :   prep = vandermondeinverseprep(L);
     248        5539 :   if (!dn)
     249             :   {
     250        1029 :     GEN dis, res = RgV_prod(gabs(prep,prec));
     251             :     /*Add +1 to cater for accuracy error in res */
     252        1029 :     dis = ZX_disc_all(T, 1+expi(ceil_safe(res)));
     253        1029 :     den = indexpartial(T,dis);
     254        1029 :     if (ptdis) *ptdis = dis;
     255             :   }
     256             :   else
     257             :   {
     258        4510 :     if (typ(dn) != t_INT || signe(dn) <= 0)
     259           0 :       pari_err_TYPE("initgaloisborne [incorrect denominator]", dn);
     260        4510 :     den = dn;
     261             :   }
     262        5539 :   if (ptprep) *ptprep = prep;
     263        5539 :   *ptL = L; return den;
     264             : }
     265             : 
     266             : /* ||| M ||| with respect to || x ||_oo, M t_MAT */
     267             : GEN
     268       16615 : matrixnorm(GEN M, long prec)
     269             : {
     270       16615 :   long i,j,m, l = lg(M);
     271       16615 :   GEN B = real_0(prec);
     272             : 
     273       16615 :   if (l == 1) return B;
     274       16615 :   m = lgcols(M);
     275       57111 :   for (i = 1; i < m; i++)
     276             :   {
     277       40496 :     GEN z = gabs(gcoeff(M,i,1), prec);
     278       40496 :     for (j = 2; j < l; j++) z = gadd(z, gabs(gcoeff(M,i,j), prec));
     279       40496 :     if (gcmp(z, B) > 0) B = z;
     280             :   }
     281       16615 :   return B;
     282             : }
     283             : 
     284             : static GEN
     285        2247 : galoisborne(GEN T, GEN dn, struct galois_borne *gb, long d)
     286             : {
     287             :   pari_sp ltop, av2;
     288             :   GEN borne, borneroots, bornetrace, borneabs;
     289             :   long prec;
     290             :   GEN L, M, prep, den;
     291             :   pari_timer ti;
     292        2247 :   const long step=3;
     293             : 
     294        2247 :   prec = nbits2prec(bit_accuracy(ZX_max_lg(T)));
     295        2247 :   den = initgaloisborne(T,dn,prec, &L,&prep,NULL);
     296        2247 :   if (!dn) dn = den;
     297        2247 :   ltop = avma;
     298        2247 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     299        2247 :   M = vandermondeinverse(L, RgX_gtofp(T, prec), den, prep);
     300        2247 :   if (DEBUGLEVEL>=4) timer_printf(&ti,"vandermondeinverse");
     301        2247 :   borne = matrixnorm(M, prec);
     302        2247 :   borneroots = gsupnorm(L, prec); /*t_REAL*/
     303        2247 :   bornetrace = gmulsg((2*step)*degpol(T)/d,
     304        2247 :                       powru(borneroots, minss(degpol(T), step)));
     305        2247 :   borneroots = ceil_safe(gmul(borne, borneroots));
     306        2247 :   borneabs = ceil_safe(gmax(gmul(borne, bornetrace), powru(bornetrace, d)));
     307        2247 :   av2 = avma;
     308             :   /*We use d-1 test, so we must overlift to 2^BITS_IN_LONG*/
     309        2247 :   gb->valsol = logint(shifti(borneroots,2+BITS_IN_LONG), gb->l) + 1;
     310        2247 :   gb->valabs = logint(shifti(borneabs,2), gb->l) + 1;
     311        2247 :   gb->valabs = maxss(gb->valsol, gb->valabs);
     312        2247 :   if (DEBUGLEVEL >= 4)
     313           0 :     err_printf("GaloisConj: val1=%ld val2=%ld\n", gb->valsol, gb->valabs);
     314        2247 :   avma = av2;
     315        2247 :   gb->bornesol = gerepileuptoint(ltop, shifti(borneroots,1));
     316        2247 :   if (DEBUGLEVEL >= 9)
     317           0 :     err_printf("GaloisConj: Bound %Ps\n",borneroots);
     318        2247 :   gb->ladicsol = powiu(gb->l, gb->valsol);
     319        2247 :   gb->ladicabs = powiu(gb->l, gb->valabs);
     320        2247 :   return dn;
     321             : }
     322             : 
     323             : static GEN
     324        2128 : makeLden(GEN L,GEN den, struct galois_borne *gb)
     325        2128 : { return FpC_Fp_mul(L, den, gb->ladicsol); }
     326             : 
     327             : /* Initialize the galois_lift structure */
     328             : static void
     329        2191 : initlift(GEN T, GEN den, GEN p, GEN L, GEN Lden, struct galois_borne *gb, struct galois_lift *gl)
     330             : {
     331        2191 :   pari_sp av = avma;
     332             :   long e;
     333        2191 :   gl->gb = gb;
     334        2191 :   gl->T = T;
     335        2191 :   gl->den = is_pm1(den)? gen_1: den;
     336        2191 :   gl->p = p;
     337        2191 :   gl->L = L;
     338        2191 :   gl->Lden = Lden;
     339        2191 :   e = logint(shifti(gb->bornesol, 2+BITS_IN_LONG),p) + 1;
     340        2191 :   avma = av;
     341        2191 :   if (e < 2) e = 2;
     342        2191 :   gl->e = e;
     343        2191 :   gl->Q = powiu(p, e);
     344        2191 :   gl->TQ = FpX_red(T,gl->Q);
     345        2191 : }
     346             : 
     347             : /* Check whether f is (with high probability) a solution and compute its
     348             :  * permutation */
     349             : static int
     350        4536 : poltopermtest(GEN f, struct galois_lift *gl, GEN pf)
     351             : {
     352             :   pari_sp av;
     353        4536 :   GEN fx, fp, B = gl->gb->bornesol;
     354             :   long i, j, ll;
     355       39333 :   for (i = 2; i < lg(f); i++)
     356       36610 :     if (abscmpii(gel(f,i),B) > 0)
     357             :     {
     358        1813 :       if (DEBUGLEVEL>=4) err_printf("GaloisConj: Solution too large.\n");
     359        1813 :       if (DEBUGLEVEL>=8) err_printf("f=%Ps\n borne=%Ps\n",f,B);
     360        1813 :       return 0;
     361             :     }
     362        2723 :   ll = lg(gl->L);
     363        2723 :   fp = const_vecsmall(ll-1, 1); /* left on stack */
     364        2723 :   av = avma;
     365       41839 :   for (i = 1; i < ll; i++, avma = av)
     366             :   {
     367       39123 :     fx = FpX_eval(f, gel(gl->L,i), gl->gb->ladicsol);
     368      645680 :     for (j = 1; j < ll; j++)
     369      645673 :       if (fp[j] && equalii(fx, gel(gl->Lden,j))) { pf[i]=j; fp[j]=0; break; }
     370       39123 :     if (j == ll) return 0;
     371             :   }
     372        2716 :   return 1;
     373             : }
     374             : 
     375             : struct monoratlift
     376             : {
     377             :   struct galois_lift *gl;
     378             :   GEN frob;
     379             :   long early;
     380             : };
     381             : 
     382             : static int
     383        6458 : monoratlift(void *E, GEN S, GEN q)
     384             : {
     385        6458 :   struct monoratlift *d = (struct monoratlift *) E;
     386        6458 :   struct galois_lift *gl = d->gl;
     387        6458 :   GEN qm1old = sqrti(shifti(q,-2));
     388        6458 :   GEN tlift = FpX_ratlift(S,q,qm1old,qm1old,gl->den);
     389        6458 :   if (tlift)
     390             :   {
     391        1281 :     pari_sp ltop = avma;
     392        1281 :     if(DEBUGLEVEL>=4)
     393           0 :       err_printf("MonomorphismLift: trying early solution %Ps\n",tlift);
     394        1281 :     if (gl->den != gen_1) {
     395         770 :       GEN N = gl->gb->ladicsol, N2 = shifti(N,-1);
     396         770 :       tlift = FpX_center(FpX_red(Q_muli_to_int(tlift, gl->den), N), N,N2);
     397             :     }
     398        1281 :     if (poltopermtest(tlift, gl, d->frob))
     399             :     {
     400        1274 :       if(DEBUGLEVEL>=4) err_printf("MonomorphismLift: true early solution.\n");
     401        1274 :       d->early = 1;
     402        1274 :       avma = ltop; return 1;
     403             :     }
     404           7 :     avma = ltop;
     405           7 :     if(DEBUGLEVEL>=4) err_printf("MonomorphismLift: false early solution.\n");
     406             :   }
     407        5184 :   return 0;
     408             : }
     409             : 
     410             : static GEN
     411        2387 : monomorphismratlift(GEN P, GEN S, struct galois_lift *gl, GEN frob)
     412             : {
     413             :   pari_timer ti;
     414        2387 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
     415        2387 :   if (frob)
     416             :   {
     417             :     struct monoratlift d;
     418        2163 :     d.gl = gl; d.frob = frob; d.early = 0;
     419        2163 :     S = ZpX_ZpXQ_liftroot_ea(P,S,gl->T,gl->p, gl->e, (void*)&d, monoratlift);
     420        2163 :     S = d.early ? NULL: S;
     421             :   }
     422             :   else
     423         224 :     S = ZpX_ZpXQ_liftroot(P,S,gl->T,gl->p, gl->e);
     424        2387 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "monomorphismlift()");
     425        2387 :   return S;
     426             : }
     427             : 
     428             : /* Let T be a polynomial in Z[X] , p a prime number, S in Fp[X]/(T) so
     429             :  * that T(S)=0 [p,T]. Lift S in S_0 so that T(S_0)=0 [T,p^e]
     430             :  * Unclean stack */
     431             : static GEN
     432        2387 : automorphismlift(GEN S, struct galois_lift *gl, GEN frob)
     433             : {
     434        2387 :   return monomorphismratlift(gl->T, S, gl, frob);
     435             : }
     436             : 
     437             : static GEN
     438        2191 : galoisdolift(struct galois_lift *gl, GEN frob)
     439             : {
     440        2191 :   pari_sp av = avma;
     441        2191 :   GEN Tp = FpX_red(gl->T, gl->p);
     442        2191 :   GEN S = FpX_Frobenius(Tp, gl->p);
     443        2191 :   return gerepileupto(av, automorphismlift(S, gl, frob));
     444             : }
     445             : 
     446             : static void
     447         770 : inittestlift(GEN plift, GEN Tmod, struct galois_lift *gl,
     448             :              struct galois_testlift *gt)
     449             : {
     450             :   pari_timer ti;
     451         770 :   gt->n = lg(gl->L) - 1;
     452         770 :   gt->g = lg(Tmod) - 1;
     453         770 :   gt->f = gt->n / gt->g;
     454         770 :   gt->bezoutcoeff = bezout_lift_fact(gl->T, Tmod, gl->p, gl->e);
     455         770 :   if (DEBUGLEVEL >= 2) timer_start(&ti);
     456         770 :   gt->pauto = FpXQ_autpowers(plift, gt->f-1, gl->TQ, gl->Q);
     457         770 :   if (DEBUGLEVEL >= 2) timer_printf(&ti, "Frobenius power");
     458         770 : }
     459             : 
     460             : /* Explanation of the intheadlong technique:
     461             :  * Let C be a bound, B = BITS_IN_LONG, M > C*2^B a modulus and 0 <= a_i < M for
     462             :  * i=1,...,n where n < 2^B. We want to test if there exist k,l, |k| < C < M/2^B,
     463             :  * such that sum a_i = k + l*M
     464             :  * We write a_i*2^B/M = b_i+c_i with b_i integer and 0<=c_i<1, so that
     465             :  *   sum b_i - l*2^B = k*2^B/M - sum c_i
     466             :  * Since -1 < k*2^B/M < 1 and 0<=c_i<1, it follows that
     467             :  *   -n-1 < sum b_i - l*2^B < 1  i.e.  -n <= sum b_i -l*2^B <= 0
     468             :  * So we compute z = - sum b_i [mod 2^B] and check if 0 <= z <= n. */
     469             : 
     470             : /* Assume 0 <= x < mod. */
     471             : static ulong
     472     1013698 : intheadlong(GEN x, GEN mod)
     473             : {
     474     1013698 :   pari_sp av = avma;
     475     1013698 :   long res = (long) itou(divii(shifti(x,BITS_IN_LONG),mod));
     476     1013698 :   avma = av; return res;
     477             : }
     478             : static GEN
     479       26712 : vecheadlong(GEN W, GEN mod)
     480             : {
     481       26712 :   long i, l = lg(W);
     482       26712 :   GEN V = cgetg(l, t_VECSMALL);
     483       26712 :   for(i=1; i<l; i++) V[i] = intheadlong(gel(W,i), mod);
     484       26712 :   return V;
     485             : }
     486             : static GEN
     487        1540 : matheadlong(GEN W, GEN mod)
     488             : {
     489        1540 :   long i, l = lg(W);
     490        1540 :   GEN V = cgetg(l,t_MAT);
     491        1540 :   for(i=1; i<l; i++) gel(V,i) = vecheadlong(gel(W,i), mod);
     492        1540 :   return V;
     493             : }
     494             : static ulong
     495       42504 : polheadlong(GEN P, long n, GEN mod)
     496             : {
     497       42504 :   return (lg(P)>n+2)? intheadlong(gel(P,n+2),mod): 0;
     498             : }
     499             : 
     500             : #define headlongisint(Z,N) (-(ulong)(Z)<=(ulong)(N))
     501             : 
     502             : static long
     503         875 : frobeniusliftall(GEN sg, long el, GEN *psi, struct galois_lift *gl,
     504             :                  struct galois_testlift *gt, GEN frob)
     505             : {
     506         875 :   pari_sp av, ltop2, ltop = avma;
     507         875 :   long i,j,k, c = lg(sg)-1, n = lg(gl->L)-1, m = gt->g, d = m / c;
     508             :   GEN pf, u, v, C, Cd, SG, cache;
     509         875 :   long N1, N2, R1, Ni, ord = gt->f, c_idx = gt->g-1;
     510             :   ulong headcache;
     511         875 :   long hop = 0;
     512             :   GEN NN, NQ;
     513             :   pari_timer ti;
     514             : 
     515         875 :   *psi = pf = cgetg(m, t_VECSMALL);
     516         875 :   ltop2 = avma;
     517         875 :   NN = diviiexact(mpfact(m), mului(c, powiu(mpfact(d), c)));
     518         875 :   if (DEBUGLEVEL >= 4)
     519           0 :     err_printf("GaloisConj: I will try %Ps permutations\n", NN);
     520         875 :   N1=10000000;
     521         875 :   NQ=divis_rem(NN,N1,&R1);
     522         875 :   if (abscmpiu(NQ,1000000000)>0)
     523             :   {
     524           0 :     pari_warn(warner,"Combinatorics too hard : would need %Ps tests!\n"
     525             :         "I will skip it, but it may induce an infinite loop",NN);
     526           0 :     avma = ltop; *psi = NULL; return 0;
     527             :   }
     528         875 :   N2=itos(NQ); if(!N2) N1=R1;
     529         875 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     530         875 :   avma = ltop2;
     531         875 :   C = gt->C;
     532         875 :   Cd= gt->Cd;
     533         875 :   v = FpXQ_mul(gel(gt->pauto, 1+el%ord), gel(gt->bezoutcoeff, m),gl->TQ,gl->Q);
     534         875 :   if (gl->den != gen_1) v = FpX_Fp_mul(v, gl->den, gl->Q);
     535         875 :   SG = cgetg(lg(sg),t_VECSMALL);
     536         875 :   for(i=1; i<lg(SG); i++) SG[i] = (el*sg[i])%ord + 1;
     537         875 :   cache = cgetg(m+1,t_VECSMALL); cache[m] = polheadlong(v,1,gl->Q);
     538         875 :   headcache = polheadlong(v,2,gl->Q);
     539         875 :   for (i = 1; i < m; i++) pf[i] = 1 + i/d;
     540         875 :   av = avma;
     541      233072 :   for (Ni = 0, i = 0; ;i++)
     542             :   {
     543     1105503 :     for (j = c_idx ; j > 0; j--)
     544             :     {
     545      872431 :       long h = SG[pf[j]];
     546      872431 :       if (!mael(C,h,j))
     547             :       {
     548        3521 :         pari_sp av3 = avma;
     549        3521 :         GEN r = FpXQ_mul(gel(gt->pauto,h), gel(gt->bezoutcoeff,j),gl->TQ,gl->Q);
     550        3521 :         if (gl->den != gen_1) r = FpX_Fp_mul(r, gl->den, gl->Q);
     551        3521 :         gmael(C,h,j) = gclone(r);
     552        3521 :         mael(Cd,h,j) = polheadlong(r,1,gl->Q);
     553        3521 :         avma = av3;
     554             :       }
     555      872431 :       uel(cache,j) = uel(cache,j+1)+umael(Cd,h,j);
     556             :     }
     557      233072 :     if (headlongisint(uel(cache,1),n))
     558             :     {
     559        2954 :       ulong head = headcache;
     560        2954 :       for (j = 1; j < m; j++) head += polheadlong(gmael(C,SG[pf[j]],j),2,gl->Q);
     561        2954 :       if (headlongisint(head,n))
     562             :       {
     563         721 :         u = v;
     564         721 :         for (j = 1; j < m; j++) u = ZX_add(u, gmael(C,SG[pf[j]],j));
     565         721 :         u = FpX_center(FpX_red(u, gl->Q), gl->Q, shifti(gl->Q,-1));
     566         721 :         if (poltopermtest(u, gl, frob))
     567             :         {
     568         714 :           if (DEBUGLEVEL >= 4)
     569             :           {
     570           0 :             timer_printf(&ti, "");
     571           0 :             err_printf("GaloisConj: %d hops on %Ps tests\n",hop,addis(mulss(Ni,N1),i));
     572             :           }
     573         714 :           avma = ltop2; return 1;
     574             :         }
     575           7 :         if (DEBUGLEVEL >= 4) err_printf("M");
     576             :       }
     577        2233 :       else hop++;
     578             :     }
     579      232358 :     if (DEBUGLEVEL >= 4 && i % maxss(N1/20, 1) == 0)
     580           0 :       timer_printf(&ti, "GaloisConj:Testing %Ps", addis(mulss(Ni,N1),i));
     581      232358 :     avma = av;
     582      232358 :     if (i == N1-1)
     583             :     {
     584         161 :       if (Ni==N2-1) N1 = R1;
     585         161 :       if (Ni==N2) break;
     586           0 :       Ni++; i = 0;
     587           0 :       if (DEBUGLEVEL>=4) timer_start(&ti);
     588             :     }
     589      232197 :     for (j = 2; j < m && pf[j-1] >= pf[j]; j++)
     590             :       /*empty*/; /* to kill clang Warning */
     591      232197 :     for (k = 1; k < j-k && pf[k] != pf[j-k]; k++) { lswap(pf[k], pf[j-k]); }
     592      232197 :     for (k = j - 1; pf[k] >= pf[j]; k--)
     593             :       /*empty*/;
     594      232197 :     lswap(pf[j], pf[k]); c_idx = j;
     595      232197 :   }
     596         161 :   if (DEBUGLEVEL>=4) err_printf("GaloisConj: not found, %d hops \n",hop);
     597         161 :   *psi = NULL; avma = ltop; return 0;
     598             : }
     599             : 
     600             : /* Compute the test matrix for the i-th line of V. Clone. */
     601             : static GEN
     602        1540 : Vmatrix(long i, struct galois_test *td)
     603             : {
     604        1540 :   pari_sp av = avma;
     605        1540 :   GEN m = gclone( matheadlong(FpC_FpV_mul(td->L, row(td->M,i), td->ladic), td->ladic));
     606        1540 :   avma = av; return m;
     607             : }
     608             : 
     609             : /* Initialize galois_test */
     610             : static void
     611        1344 : inittest(GEN L, GEN M, GEN borne, GEN ladic, struct galois_test *td)
     612             : {
     613        1344 :   long i, n = lg(L)-1;
     614        1344 :   GEN p = cgetg(n+1, t_VECSMALL);
     615        1344 :   if (DEBUGLEVEL >= 8) err_printf("GaloisConj: Init Test\n");
     616        1344 :   td->order = p;
     617        1344 :   for (i = 1; i <= n-2; i++) p[i] = i+2;
     618        1344 :   p[n-1] = 1; p[n] = 2;
     619        1344 :   td->borne = borne;
     620        1344 :   td->lborne = subii(ladic, borne);
     621        1344 :   td->ladic = ladic;
     622        1344 :   td->L = L;
     623        1344 :   td->M = M;
     624        1344 :   td->TM = shallowtrans(M);
     625        1344 :   td->PV = zero_zv(n);
     626        1344 :   gel(td->PV, 2) = Vmatrix(2, td);
     627        1344 : }
     628             : 
     629             : /* Free clones stored inside galois_test */
     630             : static void
     631        1344 : freetest(struct galois_test *td)
     632             : {
     633             :   long i;
     634       20454 :   for (i = 1; i < lg(td->PV); i++)
     635       19110 :     if (td->PV[i]) { gunclone(gel(td->PV,i)); td->PV[i] = 0; }
     636        1344 : }
     637             : 
     638             : /* Check if the integer P seen as a p-adic number is close to an integer less
     639             :  * than td->borne in absolute value */
     640             : static long
     641       39424 : padicisint(GEN P, struct galois_test *td)
     642             : {
     643       39424 :   pari_sp ltop = avma;
     644       39424 :   GEN U  = modii(P, td->ladic);
     645       39424 :   long r = cmpii(U, td->borne) <= 0 || cmpii(U, td->lborne) >= 0;
     646       39424 :   avma = ltop; return r;
     647             : }
     648             : 
     649             : /* Check if the permutation pf is valid according to td.
     650             :  * If not, update td to make subsequent test faster (hopefully) */
     651             : static long
     652       92848 : galois_test_perm(struct galois_test *td, GEN pf)
     653             : {
     654       92848 :   pari_sp av = avma;
     655       92848 :   long i, j, n = lg(td->L)-1;
     656       92848 :   GEN V, P = NULL;
     657      133462 :   for (i = 1; i < n; i++)
     658             :   {
     659      131474 :     long ord = td->order[i];
     660      131474 :     GEN PW = gel(td->PV, ord);
     661      131474 :     if (PW)
     662             :     {
     663       92050 :       ulong head = umael(PW,1,pf[1]);
     664       92050 :       for (j = 2; j <= n; j++) head += umael(PW,j,pf[j]);
     665       92050 :       if (!headlongisint(head,n)) break;
     666             :     } else
     667             :     {
     668       39424 :       if (!P) P = vecpermute(td->L, pf);
     669       39424 :       V = FpV_dotproduct(gel(td->TM,ord), P, td->ladic);
     670       39424 :       if (!padicisint(V, td)) {
     671         196 :         gel(td->PV, ord) = Vmatrix(ord, td);
     672         196 :         if (DEBUGLEVEL >= 4) err_printf("M");
     673         196 :         break;
     674             :       }
     675             :     }
     676             :   }
     677       92848 :   if (i == n) { avma = av; return 1; }
     678       90860 :   if (DEBUGLEVEL >= 4) err_printf("%d.", i);
     679       90860 :   if (i > 1)
     680             :   {
     681         819 :     long z = td->order[i];
     682         819 :     for (j = i; j > 1; j--) td->order[j] = td->order[j-1];
     683         819 :     td->order[1] = z;
     684         819 :     if (DEBUGLEVEL >= 8) err_printf("%Ps", td->order);
     685             :   }
     686       90860 :   avma = av; return 0;
     687             : }
     688             : /*Compute a*b/c when a*b will overflow*/
     689             : static long
     690           0 : muldiv(long a,long b,long c)
     691             : {
     692           0 :   return (long)((double)a*(double)b/c);
     693             : }
     694             : 
     695             : /* F = cycle decomposition of sigma,
     696             :  * B = cycle decomposition of cl(tau).
     697             :  * Check all permutations pf who can possibly correspond to tau, such that
     698             :  * tau*sigma*tau^-1 = sigma^s and tau^d = sigma^t, where d = ord cl(tau)
     699             :  * x: vector of choices,
     700             :  * G: vector allowing linear access to elts of F.
     701             :  * Choices multiple of e are not changed. */
     702             : static GEN
     703        2128 : testpermutation(GEN F, GEN B, GEN x, long s, long e, long cut,
     704             :                 struct galois_test *td)
     705             : {
     706        2128 :   pari_sp av, avm = avma;
     707             :   long a, b, c, d, n, p1, p2, p3, p4, p5, p6, l1, l2, N1, N2, R1;
     708        2128 :   long i, j, cx, hop = 0, start = 0;
     709             :   GEN pf, ar, G, W, NN, NQ;
     710             :   pari_timer ti;
     711        2128 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
     712        2128 :   a = lg(F)-1; b = lg(gel(F,1))-1;
     713        2128 :   c = lg(B)-1; d = lg(gel(B,1))-1;
     714        2128 :   n = a*b;
     715        2128 :   s = (b+s) % b;
     716        2128 :   pf = cgetg(n+1, t_VECSMALL);
     717        2128 :   av = avma;
     718        2128 :   ar = cgetg(a+2, t_VECSMALL); ar[a+1]=0;
     719        2128 :   G  = cgetg(a+1, t_VECSMALL);
     720        2128 :   W  = gel(td->PV, td->order[n]);
     721       17584 :   for (cx=1, i=1, j=1; cx <= a; cx++, i++)
     722             :   {
     723       15456 :     gel(G,cx) = gel(F, coeff(B,i,j));
     724       15456 :     if (i == d) { i = 0; j++; }
     725             :   }
     726        2128 :   NN = divis(powuu(b, c * (d - d/e)), cut);
     727        2128 :   if (DEBUGLEVEL>=4) err_printf("GaloisConj: I will try %Ps permutations\n", NN);
     728        2128 :   N1 = 1000000;
     729        2128 :   NQ = divis_rem(NN,N1,&R1);
     730        2128 :   if (abscmpiu(NQ,100000000)>0)
     731             :   {
     732           0 :     avma = avm;
     733           0 :     pari_warn(warner,"Combinatorics too hard: would need %Ps tests!\n"
     734             :                      "I'll skip it but you will get a partial result...",NN);
     735           0 :     return identity_perm(n);
     736             :   }
     737        2128 :   N2 = itos(NQ);
     738        2373 :   for (l2 = 0; l2 <= N2; l2++)
     739             :   {
     740        2128 :     long nbiter = (l2<N2) ? N1: R1;
     741        2128 :     if (DEBUGLEVEL >= 2 && N2) err_printf("%d%% ", muldiv(l2,100,N2));
     742     5600063 :     for (l1 = 0; l1 < nbiter; l1++)
     743             :     {
     744     5599818 :       if (start)
     745             :       {
     746    14651280 :         for (i=1, j=e; i < a;)
     747             :         {
     748     9053590 :           if ((++(x[i])) != b) break;
     749     3455900 :           x[i++] = 0;
     750     3455900 :           if (i == j) { i++; j += e; }
     751             :         }
     752             :       }
     753        2128 :       else { start=1; i = a-1; }
     754             :       /* intheadlong test: overflow in + is OK, we compute mod 2^BIL */
     755    23411038 :       for (p1 = i+1, p5 = p1%d - 1 ; p1 >= 1; p1--, p5--) /* p5 = (p1%d) - 1 */
     756             :       {
     757    17811220 :         ulong V = 0;
     758    17811220 :         if (p5 == - 1) { p5 = d - 1; p6 = p1 + 1 - d; } else p6 = p1 + 1;
     759    17811220 :         p4 = p5 ? x[p1-1] : 0;
     760    17811220 :         V = 0;
     761    60189283 :         for (p2 = 1+p4, p3 = 1 + x[p1]; p2 <= b; p2++)
     762             :         {
     763    42378063 :           V += umael(W,mael(G,p6,p3),mael(G,p1,p2));
     764    42378063 :           p3 += s; if (p3 > b) p3 -= b;
     765             :         }
     766    17811220 :         p3 = 1 + x[p1] - s; if (p3 <= 0) p3 += b;
     767    27393730 :         for (p2 = p4; p2 >= 1; p2--)
     768             :         {
     769     9582510 :           V += umael(W,mael(G,p6,p3),mael(G,p1,p2));
     770     9582510 :           p3 -= s; if (p3 <= 0) p3 += b;
     771             :         }
     772    17811220 :         ar[p1] = ar[p1+1] + V;
     773             :       }
     774     5599818 :       if (!headlongisint(uel(ar,1),n)) continue;
     775             : 
     776             :       /* intheadlong succeeds. Full computation */
     777     2902921 :       for (p1=1, p5=d; p1 <= a; p1++, p5++)
     778             :       {
     779     2810178 :         if (p5 == d) { p5 = 0; p4 = 0; } else p4 = x[p1-1];
     780     2810178 :         if (p5 == d-1) p6 = p1+1-d; else p6 = p1+1;
     781     7808297 :         for (p2 = 1+p4, p3 = 1 + x[p1]; p2 <= b; p2++)
     782             :         {
     783     4998119 :           pf[mael(G,p1,p2)] = mael(G,p6,p3);
     784     4998119 :           p3 += s; if (p3 > b) p3 -= b;
     785             :         }
     786     2810178 :         p3 = 1 + x[p1] - s; if (p3 <= 0) p3 += b;
     787     3648834 :         for (p2 = p4; p2 >= 1; p2--)
     788             :         {
     789      838656 :           pf[mael(G,p1,p2)] = mael(G,p6,p3);
     790      838656 :           p3 -= s; if (p3 <= 0) p3 += b;
     791             :         }
     792             :       }
     793       92743 :       if (galois_test_perm(td, pf))
     794             :       {
     795        1883 :         if (DEBUGLEVEL >= 1)
     796             :         {
     797           0 :           GEN nb = addis(mulss(l2,N1),l1);
     798           0 :           timer_printf(&ti, "testpermutation(%Ps)", nb);
     799           0 :           if (DEBUGLEVEL >= 2 && hop)
     800           0 :             err_printf("GaloisConj: %d hop over %Ps iterations\n", hop, nb);
     801             :         }
     802        1883 :         avma = av; return pf;
     803             :       }
     804       90860 :       hop++;
     805             :     }
     806             :   }
     807         245 :   if (DEBUGLEVEL >= 1)
     808             :   {
     809           0 :     timer_printf(&ti, "testpermutation(%Ps)", NN);
     810           0 :     if (DEBUGLEVEL >= 2 && hop)
     811           0 :       err_printf("GaloisConj: %d hop over %Ps iterations\n", hop, NN);
     812             :   }
     813         245 :   avma = avm; return NULL;
     814             : }
     815             : 
     816             : /* List of subgroups of (Z/mZ)^* whose order divide o, and return the list
     817             :  * of their elements, sorted by increasing order */
     818             : static GEN
     819         875 : listznstarelts(long m, long o)
     820             : {
     821         875 :   pari_sp av = avma;
     822             :   GEN L, zn, zns;
     823             :   long i, phi, ind, l;
     824         875 :   if (m == 2) retmkvec(mkvecsmall(1));
     825         875 :   zn = znstar(stoi(m));
     826         875 :   phi = itos(gel(zn,1));
     827         875 :   o = ugcd(o, phi); /* do we impose this on input ? */
     828         875 :   zns = znstar_small(zn);
     829         875 :   L = cgetg(o+1, t_VEC);
     830        2688 :   for (i=1,ind = phi; ind; ind -= phi/o, i++) /* by *decreasing* exact index */
     831        1813 :     gel(L,i) = subgrouplist(gel(zn,2), mkvec(utoipos(ind)));
     832         875 :   L = shallowconcat1(L); l = lg(L);
     833         875 :   for (i = 1; i < l; i++) gel(L,i) = znstar_hnf_elts(zns, gel(L,i));
     834         875 :   return gerepilecopy(av, L);
     835             : }
     836             : 
     837             : /* A sympol is a symmetric polynomial
     838             :  *
     839             :  * Currently sympol are couple of t_VECSMALL [v,w]
     840             :  * v[1]...v[k], w[1]...w[k]  represent the polynomial sum(i=1,k,v[i]*s_w[i])
     841             :  * where s_i(X_1,...,X_n) = sum(j=1,n,X_j^i) */
     842             : 
     843             : /*Return s_e*/
     844             : static GEN
     845        3941 : sympol_eval_newtonsum(long e, GEN O, GEN mod)
     846             : {
     847        3941 :   long f = lg(O), g = lg(gel(O,1)), i, j;
     848        3941 :   GEN PL = cgetg(f, t_COL);
     849       21616 :   for(i=1; i<f; i++)
     850             :   {
     851       17675 :     pari_sp av = avma;
     852       17675 :     GEN s = gen_0;
     853       17675 :     for(j=1; j<g; j++) s = addii(s, Fp_powu(gmael(O,i,j), (ulong)e, mod));
     854       17675 :     gel(PL,i) = gerepileuptoint(av, remii(s,mod));
     855             :   }
     856        3941 :   return PL;
     857             : }
     858             : 
     859             : static GEN
     860        2660 : sympol_eval(GEN v, GEN NS)
     861             : {
     862        2660 :   pari_sp av = avma;
     863             :   long i;
     864        2660 :   GEN S = gen_0;
     865        5978 :   for (i=1; i<lg(v); i++)
     866        3318 :     if (v[i]) S = gadd(S, gmulsg(v[i], gel(NS,i)));
     867        2660 :   return gerepileupto(av, S);
     868             : }
     869             : 
     870             : /* Let sigma be an automorphism of L (as a polynomial with rational coefs)
     871             :  * Let 'sym' be a symmetric polynomial defining alpha in L.
     872             :  * We have alpha = sym(x,sigma(x),,,sigma^(g-1)(x)). Compute alpha mod p */
     873             : static GEN
     874        1316 : sympol_aut_evalmod(GEN sym, long g, GEN sigma, GEN Tp, GEN p)
     875             : {
     876        1316 :   pari_sp ltop=avma;
     877        1316 :   long i, j, npows = brent_kung_optpow(degpol(Tp)-1, g-1, 1);
     878        1316 :   GEN s, f, pows, v = zv_to_ZV(gel(sym,1)), w = zv_to_ZV(gel(sym,2));
     879        1316 :   sigma = RgX_to_FpX(sigma, p);
     880        1316 :   pows  = FpXQ_powers(sigma,npows,Tp,p);
     881        1316 :   f = pol_x(varn(sigma));
     882        1316 :   s = pol_0(varn(sigma));
     883        7014 :   for(i=1; i<=g;i++)
     884             :   {
     885        5698 :     if (i > 1) f = FpX_FpXQV_eval(f,pows,Tp,p);
     886       11921 :     for(j=1; j<lg(v); j++)
     887        6223 :       s = FpX_add(s, FpX_Fp_mul(FpXQ_pow(f,gel(w,j),Tp,p),gel(v,j),p),p);
     888             :   }
     889        1316 :   return gerepileupto(ltop, s);
     890             : }
     891             : 
     892             : /* Let Sp be as computed with sympol_aut_evalmod
     893             :  * Let Tmod be the factorisation of T mod p.
     894             :  * Return the factorisation of the minimal polynomial of S mod p w.r.t. Tmod */
     895             : static GEN
     896        1316 : fixedfieldfactmod(GEN Sp, GEN p, GEN Tmod)
     897             : {
     898        1316 :   long i, l = lg(Tmod);
     899        1316 :   GEN F = cgetg(l,t_VEC);
     900        5404 :   for(i=1; i<l; i++)
     901             :   {
     902        4088 :     GEN Ti = gel(Tmod,i);
     903        4088 :     gel(F,i) = FpXQ_minpoly(FpX_rem(Sp,Ti,p), Ti,p);
     904             :   }
     905        1316 :   return F;
     906             : }
     907             : 
     908             : static GEN
     909        2415 : fixedfieldsurmer(GEN mod, GEN l, GEN p, long v, GEN NS, GEN W)
     910             : {
     911        2415 :   const long step=3;
     912        2415 :   long i, j, n = lg(W)-1, m = 1L<<((n-1)<<1);
     913        2415 :   GEN sym = cgetg(n+1,t_VECSMALL), mod2 = shifti(mod,-1);
     914        2415 :   for (j=1;j<n;j++) sym[j] = step;
     915        2415 :   sym[n] = 0;
     916        2415 :   if (DEBUGLEVEL>=4) err_printf("FixedField: Weight: %Ps\n",W);
     917        2737 :   for (i=0; i<m; i++)
     918             :   {
     919        2660 :     pari_sp av = avma;
     920             :     GEN L, P;
     921        2660 :     for (j=1; sym[j]==step; j++) sym[j]=0;
     922        2660 :     sym[j]++;
     923        2660 :     if (DEBUGLEVEL>=6) err_printf("FixedField: Sym: %Ps\n",sym);
     924        2660 :     L = sympol_eval(sym,NS);
     925        2660 :     if (!vec_is1to1(FpC_red(L,l))) { avma = av; continue; }
     926        2520 :     P = FpX_center(FpV_roots_to_pol(L,mod,v),mod,mod2);
     927        2520 :     if (!p || FpX_is_squarefree(P,p)) return mkvec3(mkvec2(sym,W),L,P);
     928         182 :     avma = av;
     929             :   }
     930          77 :   return NULL;
     931             : }
     932             : 
     933             : /*Check whether the line of NS are pair-wise distinct.*/
     934             : static long
     935        2562 : sympol_is1to1_lg(GEN NS, long n)
     936             : {
     937        2562 :   long i, j, k, l = lgcols(NS);
     938       14357 :   for (i=1; i<l; i++)
     939       69958 :     for(j=i+1; j<l; j++)
     940             :     {
     941       59633 :       for(k=1; k<n; k++)
     942       59486 :         if (!equalii(gmael(NS,k,j),gmael(NS,k,i))) break;
     943       58163 :       if (k>=n) return 0;
     944             :     }
     945        2415 :   return 1;
     946             : }
     947             : 
     948             : /* Let O a set of orbits of roots (see fixedfieldorbits) modulo mod,
     949             :  * l | mod and p two prime numbers. Return a vector [sym,s,P] where:
     950             :  * sym is a sympol, s is the set of images of sym on O and
     951             :  * P is the polynomial with roots s. */
     952             : static GEN
     953        2338 : fixedfieldsympol(GEN O, GEN mod, GEN l, GEN p, long v)
     954             : {
     955        2338 :   pari_sp ltop=avma;
     956        2338 :   const long n=(BITS_IN_LONG>>1)-1;
     957        2338 :   GEN NS = cgetg(n+1,t_MAT), sym = NULL, W = cgetg(n+1,t_VECSMALL);
     958        2338 :   long i, e=1;
     959        2338 :   if (DEBUGLEVEL>=4)
     960           0 :     err_printf("FixedField: Size: %ldx%ld\n",lg(O)-1,lg(gel(O,1))-1);
     961        4900 :   for (i=1; !sym && i<=n; i++)
     962             :   {
     963        2562 :     GEN L = sympol_eval_newtonsum(e++, O, mod);
     964        2562 :     if (lg(O)>2)
     965        2513 :       while (vec_isconst(L)) L = sympol_eval_newtonsum(e++, O, mod);
     966        2562 :     W[i] = e-1; gel(NS,i) = L;
     967        2562 :     if (sympol_is1to1_lg(NS,i+1))
     968        2415 :       sym = fixedfieldsurmer(mod,l,p,v,NS,vecsmall_shorten(W,i));
     969             :   }
     970        2338 :   if (!sym) pari_err_BUG("fixedfieldsympol [p too small]");
     971        2338 :   if (DEBUGLEVEL>=2) err_printf("FixedField: Found: %Ps\n",gel(sym,1));
     972        2338 :   return gerepilecopy(ltop,sym);
     973             : }
     974             : 
     975             : /* Let O a set of orbits as indices and L the corresponding roots.
     976             :  * Return the set of orbits as roots. */
     977             : static GEN
     978        2338 : fixedfieldorbits(GEN O, GEN L)
     979             : {
     980        2338 :   GEN S = cgetg(lg(O), t_MAT);
     981             :   long i;
     982        2338 :   for (i = 1; i < lg(O); i++) gel(S,i) = vecpermute(L, gel(O,i));
     983        2338 :   return S;
     984             : }
     985             : 
     986             : static GEN
     987         784 : fixedfieldinclusion(GEN O, GEN PL)
     988             : {
     989         784 :   long i, j, f = lg(O)-1, g = lg(gel(O,1))-1;
     990         784 :   GEN S = cgetg(f*g + 1, t_COL);
     991        5733 :   for (i = 1; i <= f; i++)
     992             :   {
     993        4949 :     GEN Oi = gel(O,i);
     994        4949 :     for (j = 1; j <= g; j++) gel(S, Oi[j]) = gel(PL, i);
     995             :   }
     996         784 :   return S;
     997             : }
     998             : 
     999             : /* Polynomial attached to a vector of conjugates. Not stack clean */
    1000             : static GEN
    1001       20300 : vectopol(GEN v, GEN M, GEN den , GEN mod, GEN mod2, long x)
    1002             : {
    1003       20300 :   long l = lg(v)+1, i;
    1004       20300 :   GEN z = cgetg(l,t_POL);
    1005       20300 :   z[1] = evalsigne(1)|evalvarn(x);
    1006      247674 :   for (i=2; i<l; i++)
    1007      227374 :     gel(z,i) = gdiv(centermodii(ZMrow_ZC_mul(M,v,i-1), mod, mod2), den);
    1008       20300 :   return normalizepol_lg(z, l);
    1009             : }
    1010             : 
    1011             : /* Polynomial associate to a permutation of the roots. Not stack clean */
    1012             : static GEN
    1013       19166 : permtopol(GEN p, GEN L, GEN M, GEN den, GEN mod, GEN mod2, long x)
    1014             : {
    1015       19166 :   if (lg(p) != lg(L)) pari_err_TYPE("permtopol [permutation]", p);
    1016       19166 :   return vectopol(vecpermute(L,p), M, den, mod, mod2, x);
    1017             : }
    1018             : 
    1019             : static GEN
    1020         441 : galoisgrouptopol(GEN res, GEN L, GEN M, GEN den, GEN mod, long v)
    1021             : {
    1022         441 :   long i, l = lg(res);
    1023         441 :   GEN mod2 = shifti(mod,-1), aut = cgetg(l, t_COL);
    1024        2716 :   for (i = 1; i < l; i++)
    1025             :   {
    1026        2275 :     if (DEBUGLEVEL>=6) err_printf("%d ",i);
    1027        2275 :     gel(aut,i) = permtopol(gel(res,i), L, M, den, mod, mod2, v);
    1028             :   }
    1029         441 :   return aut;
    1030             : }
    1031             : 
    1032             : static void
    1033        1407 : notgalois(long p, struct galois_analysis *ga)
    1034             : {
    1035        1407 :   if (DEBUGLEVEL >= 2) err_printf("GaloisAnalysis:non Galois for p=%ld\n", p);
    1036        1407 :   ga->p = p;
    1037        1407 :   ga->deg = 0;
    1038        1407 : }
    1039             : 
    1040             : /*Gather information about the group*/
    1041             : static long
    1042        3570 : init_group(long n, long np, GEN Fp, GEN Fe, long *porder)
    1043             : {
    1044        3570 :   const long prim_nonwss_orders[] = { 36,48,56,60,75,80,196,200 };
    1045        3570 :   long i, phi_order = 1, order = 1, group = 0;
    1046             : 
    1047             :  /* non-WSS groups of this order? */
    1048       32032 :   for (i=0; i < (long)numberof(prim_nonwss_orders); i++)
    1049       28476 :     if (n % prim_nonwss_orders[i] == 0) { group |= ga_non_wss; break; }
    1050        3570 :   if (np == 2 && Fp[2] == 3 && Fe[2] == 1 && Fe[1] > 2) group |= ga_ext_2;
    1051             : 
    1052        5558 :   for (i = np; i > 0; i--)
    1053             :   {
    1054        4229 :     long p = Fp[i];
    1055        4229 :     if (phi_order % p == 0) { group |= ga_all_normal; break; }
    1056        3612 :     order *= p; phi_order *= p-1;
    1057        3612 :     if (Fe[i] > 1) break;
    1058             :   }
    1059        3570 :   *porder = order; return group;
    1060             : }
    1061             : 
    1062             : /*is a "better" than b ? (if so, update karma) */
    1063             : static int
    1064       16249 : improves(long a, long b, long plift, long p, long n, long *karma)
    1065             : {
    1066       16249 :   if (!plift || a > b) { *karma = ugcd(p-1,n); return 1; }
    1067       15381 :   if (a == b) {
    1068       13351 :     long k = ugcd(p-1,n);
    1069       13351 :     if (k > *karma) { *karma = k; return 1; }
    1070             :   }
    1071       13973 :   return 0; /* worse */
    1072             : }
    1073             : 
    1074             : /* return 0 if not galois or not wss */
    1075             : static int
    1076        3570 : galoisanalysis(GEN T, struct galois_analysis *ga, long calcul_l)
    1077             : {
    1078        3570 :   pari_sp ltop = avma, av;
    1079        3570 :   long group, linf, n, p, i, karma = 0;
    1080             :   GEN F, Fp, Fe, Fpe, O;
    1081             :   long np, order, plift, nbmax, nbtest, deg;
    1082             :   pari_timer ti;
    1083             :   forprime_t S;
    1084        3570 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    1085        3570 :   n = degpol(T);
    1086        3570 :   O = zero_zv(n);
    1087        3570 :   F = factoru_pow(n);
    1088        3570 :   Fp = gel(F,1); np = lg(Fp)-1;
    1089        3570 :   Fe = gel(F,2);
    1090        3570 :   Fpe= gel(F,3);
    1091        3570 :   group = init_group(n, np, Fp, Fe, &order);
    1092             : 
    1093             :   /*Now we study the orders of the Frobenius elements*/
    1094        3570 :   deg = Fp[np]; /* largest prime | n */
    1095        3570 :   plift = 0;
    1096        3570 :   nbtest = 0;
    1097        3570 :   nbmax = 8+(n>>1);
    1098        3570 :   u_forprime_init(&S, n*maxss(expu(n)-3, 2), ULONG_MAX);
    1099        3570 :   av = avma;
    1100       35767 :   while (!plift || (nbtest < nbmax && (nbtest <=8 || order < (n>>1)))
    1101        2178 :                 || (n == 24 && O[6] == 0 && O[4] == 0)
    1102        2178 :                 || ((group&ga_non_wss) && order == Fp[np]))
    1103             :   {
    1104       30019 :     long d, o, norm_o = 1;
    1105             :     GEN D, Tp;
    1106             : 
    1107       30019 :     if ((group&ga_non_wss) && nbtest >= 3*nbmax) break; /* in all cases */
    1108       30019 :     nbtest++; avma = av;
    1109       30019 :     p = u_forprime_next(&S);
    1110       30019 :     if (!p) pari_err_OVERFLOW("galoisanalysis [ran out of primes]");
    1111       30019 :     Tp = ZX_to_Flx(T,p);
    1112       30019 :     if (!Flx_is_squarefree(Tp,p)) { if (!--nbtest) nbtest = 1; continue; }
    1113             : 
    1114       28337 :     D = Flx_nbfact_by_degree(Tp, &d, p);
    1115       28337 :     o = n / d; /* d factors, all should have degree o */
    1116       28337 :     if (D[o] != d) { notgalois(p, ga); avma = ltop; return 0; }
    1117             : 
    1118       26945 :     if (!O[o]) O[o] = p;
    1119       26945 :     if (o % deg) goto ga_end; /* NB: deg > 1 */
    1120       19015 :     if ((group&ga_all_normal) && o < order) goto ga_end;
    1121             : 
    1122             :     /*Frob_p has order o > 1, find a power which generates a normal subgroup*/
    1123       18917 :     if (o * Fp[1] >= n)
    1124       10818 :       norm_o = o; /*subgroups of smallest index are normal*/
    1125             :     else
    1126             :     {
    1127        9086 :       for (i = np; i > 0; i--)
    1128             :       {
    1129        9086 :         if (o % Fpe[i]) break;
    1130         987 :         norm_o *= Fpe[i];
    1131             :       }
    1132             :     }
    1133             :     /* Frob_p^(o/norm_o) generates a normal subgroup of order norm_o */
    1134       18917 :     if (norm_o != 1)
    1135             :     {
    1136       11805 :       if (!(group&ga_all_normal) || o > order)
    1137        2668 :         karma = ugcd(p-1,n);
    1138        9137 :       else if (!improves(norm_o, deg, plift,p,n, &karma)) goto ga_end;
    1139             :       /* karma0=0, deg0<=norm_o -> the first improves() returns 1 */
    1140        4139 :       deg = norm_o; group |= ga_all_normal; /* STORE */
    1141             :     }
    1142        7112 :     else if (group&ga_all_normal) goto ga_end;
    1143        7112 :     else if (!improves(o, order, plift,p,n, &karma)) goto ga_end;
    1144             : 
    1145        4944 :     order = o; plift = p; /* STORE */
    1146             :     ga_end:
    1147       26945 :     if (DEBUGLEVEL >= 5)
    1148           0 :       err_printf("GaloisAnalysis:Nbtest=%ld,p=%ld,o=%ld,n_o=%d,best p=%ld,ord=%ld,k=%ld\n", nbtest, p, o, norm_o, plift, order,karma);
    1149             :   }
    1150             :   /* To avoid looping on non-wss group.
    1151             :    * TODO: check for large groups. Would it be better to disable this check if
    1152             :    * we are in a good case (ga_all_normal && !(ga_ext_2) (e.g. 60)) ?*/
    1153        2178 :   ga->p = plift;
    1154        2178 :   if (!plift || ((group&ga_non_wss) && order == Fp[np]))
    1155             :   {
    1156           0 :     pari_warn(warner,"Galois group almost certainly not weakly super solvable");
    1157           0 :     return 0;
    1158             :   }
    1159        2178 :   linf = 2*n*usqrt(n);
    1160        2178 :   if (calcul_l && O[1] <= linf)
    1161             :   {
    1162             :     pari_sp av2;
    1163             :     forprime_t S2;
    1164             :     ulong p;
    1165        1093 :     u_forprime_init(&S2, linf+1,ULONG_MAX);
    1166        1093 :     av2 = avma;
    1167       35424 :     while ((p = u_forprime_next(&S2)))
    1168             :     { /*find a totally split prime l > linf*/
    1169       34331 :       GEN Tp = ZX_to_Flx(T, p);
    1170       34331 :       long nb = Flx_nbroots(Tp, p);
    1171       34331 :       if (nb == n) { O[1] = p; break; }
    1172       33253 :       if (nb && Flx_is_squarefree(Tp,p)) {
    1173          15 :         notgalois(p,ga);
    1174          15 :         avma = ltop; return 0;
    1175             :       }
    1176       33238 :       avma = av2;
    1177             :     }
    1178        1078 :     if (!p) pari_err_OVERFLOW("galoisanalysis [ran out of primes]");
    1179             :   }
    1180        2163 :   ga->group = (enum ga_code)group;
    1181        2163 :   ga->deg = deg;
    1182        2163 :   ga->mindeg =  n == 135 ? 15: 0; /* otherwise the second phase is too slow */
    1183        2163 :   ga->ord = order;
    1184        2163 :   ga->l  = O[1];
    1185        2163 :   ga->p4 = n >= 4 ? O[4] : 0;
    1186        2163 :   if (DEBUGLEVEL >= 4)
    1187           0 :     err_printf("GaloisAnalysis:p=%ld l=%ld group=%ld deg=%ld ord=%ld\n",
    1188           0 :                plift, O[1], group, deg, order);
    1189        2163 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "galoisanalysis()");
    1190        2163 :   avma = ltop; return 1;
    1191             : }
    1192             : 
    1193             : static GEN
    1194          35 : a4galoisgen(struct galois_test *td)
    1195             : {
    1196          35 :   const long n = 12;
    1197          35 :   pari_sp ltop = avma, av, av2;
    1198          35 :   long i, j, k, N, hop = 0;
    1199             :   GEN MT, O,O1,O2,O3, ar, mt, t, u, res, orb, pft, pfu, pfv;
    1200             : 
    1201          35 :   res = cgetg(3, t_VEC);
    1202          35 :   pft = cgetg(n+1, t_VECSMALL);
    1203          35 :   pfu = cgetg(n+1, t_VECSMALL);
    1204          35 :   pfv = cgetg(n+1, t_VECSMALL);
    1205          35 :   gel(res,1) = mkvec3(pft,pfu,pfv);
    1206          35 :   gel(res,2) = mkvecsmall3(2,2,3);
    1207          35 :   av = avma;
    1208          35 :   ar = cgetg(5, t_VECSMALL);
    1209          35 :   mt = gel(td->PV, td->order[n]);
    1210          35 :   t = identity_perm(n) + 1; /* Sorry for this hack */
    1211          35 :   u = cgetg(n+1, t_VECSMALL) + 1; /* too lazy to correct */
    1212          35 :   MT = cgetg(n+1, t_MAT);
    1213          35 :   for (j = 1; j <= n; j++) gel(MT,j) = cgetg(n+1, t_VECSMALL);
    1214         455 :   for (j = 1; j <= n; j++)
    1215        2730 :     for (i = 1; i < j; i++)
    1216        2310 :       ucoeff(MT,i,j) = ucoeff(MT,j,i) = ucoeff(mt,i,j)+ucoeff(mt,j,i);
    1217             :   /* MT(i,i) unused */
    1218             : 
    1219          35 :   av2 = avma;
    1220             :   /* N = itos(gdiv(mpfact(n), mpfact(n >> 1))) >> (n >> 1); */
    1221             :   /* n = 2k = 12; N = (2k)! / (k! * 2^k) = 10395 */
    1222          35 :   N = 10395;
    1223          35 :   if (DEBUGLEVEL>=4) err_printf("A4GaloisConj: will test %ld permutations\n", N);
    1224          35 :   uel(ar,4) = umael(MT,11,12);
    1225          35 :   uel(ar,3) = uel(ar,4) + umael(MT,9,10);
    1226          35 :   uel(ar,2) = uel(ar,3) + umael(MT,7,8);
    1227          35 :   uel(ar,1) = uel(ar,2) + umael(MT,5,6);
    1228       85295 :   for (i = 0; i < N; i++)
    1229             :   {
    1230             :     long g;
    1231       85295 :     if (i)
    1232             :     {
    1233       85260 :       long a, x = i, y = 1;
    1234      120190 :       do { y += 2; a = x%y; x = x/y; } while (!a);
    1235       85260 :       switch (y)
    1236             :       {
    1237             :       case 3:
    1238       56854 :         lswap(t[2], t[2-a]);
    1239       56854 :         break;
    1240             :       case 5:
    1241       22736 :         x = t[0]; t[0] = t[2]; t[2] = t[1]; t[1] = x;
    1242       22736 :         lswap(t[4], t[4-a]);
    1243       22736 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1244       22736 :         break;
    1245             :       case 7:
    1246        4879 :         x = t[0]; t[0] = t[4]; t[4] = t[3]; t[3] = t[1]; t[1] = t[2]; t[2] = x;
    1247        4879 :         lswap(t[6], t[6-a]);
    1248        4879 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1249        4879 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1250        4879 :         break;
    1251             :       case 9:
    1252         728 :         x = t[0]; t[0] = t[6]; t[6] = t[5]; t[5] = t[3]; t[3] = x;
    1253         728 :         lswap(t[1], t[4]);
    1254         728 :         lswap(t[8], t[8-a]);
    1255         728 :         uel(ar,3) = uel(ar,4) + umael(MT,t[8],t[9]);
    1256         728 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1257         728 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1258         728 :         break;
    1259             :       case 11:
    1260          63 :         x = t[0]; t[0] = t[8]; t[8] = t[7]; t[7] = t[5]; t[5] = t[1];
    1261          63 :         t[1] = t[6]; t[6] = t[3]; t[3] = t[2]; t[2] = t[4]; t[4] = x;
    1262          63 :         lswap(t[10], t[10-a]);
    1263          63 :         uel(ar,4) = umael(MT,t[10],t[11]);
    1264          63 :         uel(ar,3) = uel(ar,4) + umael(MT,t[8],t[9]);
    1265          63 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1266          63 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1267             :       }
    1268             :     }
    1269       85295 :     g = uel(ar,1)+umael(MT,t[0],t[1])+umael(MT,t[2],t[3]);
    1270       85295 :     if (headlongisint(g,n))
    1271             :     {
    1272         245 :       for (k = 0; k < n; k += 2)
    1273             :       {
    1274         210 :         pft[t[k]] = t[k+1];
    1275         210 :         pft[t[k+1]] = t[k];
    1276             :       }
    1277          35 :       if (galois_test_perm(td, pft)) break;
    1278           0 :       hop++;
    1279             :     }
    1280       85260 :     avma = av2;
    1281             :   }
    1282          35 :   if (DEBUGLEVEL >= 1 && hop)
    1283           0 :     err_printf("A4GaloisConj: %ld hop over %ld iterations\n", hop, N);
    1284          35 :   if (i == N) { avma = ltop; return gen_0; }
    1285             :   /* N = itos(gdiv(mpfact(n >> 1), mpfact(n >> 2))) >> 1; */
    1286          35 :   N = 60;
    1287          35 :   if (DEBUGLEVEL >= 4) err_printf("A4GaloisConj: sigma=%Ps \n", pft);
    1288         140 :   for (k = 0; k < n; k += 4)
    1289             :   {
    1290         105 :     u[k+3] = t[k+3];
    1291         105 :     u[k+2] = t[k+1];
    1292         105 :     u[k+1] = t[k+2];
    1293         105 :     u[k]   = t[k];
    1294             :   }
    1295        1631 :   for (i = 0; i < N; i++)
    1296             :   {
    1297        1631 :     ulong g = 0;
    1298        1631 :     if (i)
    1299             :     {
    1300        1596 :       long a, x = i, y = -2;
    1301        2513 :       do { y += 4; a = x%y; x = x/y; } while (!a);
    1302        1596 :       lswap(u[0],u[2]);
    1303        1596 :       switch (y)
    1304             :       {
    1305             :       case 2:
    1306         798 :         break;
    1307             :       case 6:
    1308         679 :         lswap(u[4],u[6]);
    1309         679 :         if (!(a & 1))
    1310             :         {
    1311         273 :           a = 4 - (a>>1);
    1312         273 :           lswap(u[6], u[a]);
    1313         273 :           lswap(u[4], u[a-2]);
    1314             :         }
    1315         679 :         break;
    1316             :       case 10:
    1317         119 :         x = u[6];
    1318         119 :         u[6] = u[3];
    1319         119 :         u[3] = u[2];
    1320         119 :         u[2] = u[4];
    1321         119 :         u[4] = u[1];
    1322         119 :         u[1] = u[0];
    1323         119 :         u[0] = x;
    1324         119 :         if (a >= 3) a += 2;
    1325         119 :         a = 8 - a;
    1326         119 :         lswap(u[10],u[a]);
    1327         119 :         lswap(u[8], u[a-2]);
    1328         119 :         break;
    1329             :       }
    1330             :     }
    1331        1631 :     for (k = 0; k < n; k += 2) g += mael(MT,u[k],u[k+1]);
    1332        1631 :     if (headlongisint(g,n))
    1333             :     {
    1334         245 :       for (k = 0; k < n; k += 2)
    1335             :       {
    1336         210 :         pfu[u[k]] = u[k+1];
    1337         210 :         pfu[u[k+1]] = u[k];
    1338             :       }
    1339          35 :       if (galois_test_perm(td, pfu)) break;
    1340           0 :       hop++;
    1341             :     }
    1342        1596 :     avma = av2;
    1343             :   }
    1344          35 :   if (i == N) { avma = ltop; return gen_0; }
    1345          35 :   if (DEBUGLEVEL >= 1 && hop)
    1346           0 :     err_printf("A4GaloisConj: %ld hop over %ld iterations\n", hop, N);
    1347          35 :   if (DEBUGLEVEL >= 4) err_printf("A4GaloisConj: tau=%Ps \n", pfu);
    1348          35 :   avma = av2;
    1349          35 :   orb = mkvec2(pft,pfu);
    1350          35 :   O = vecperm_orbits(orb, 12);
    1351          35 :   if (DEBUGLEVEL >= 4) {
    1352           0 :     err_printf("A4GaloisConj: orb=%Ps\n", orb);
    1353           0 :     err_printf("A4GaloisConj: O=%Ps \n", O);
    1354             :   }
    1355          35 :   av2 = avma;
    1356          35 :   O1 = gel(O,1); O2 = gel(O,2); O3 = gel(O,3);
    1357          49 :   for (j = 0; j < 2; j++)
    1358             :   {
    1359          49 :     pfv[O1[1]] = O2[1];
    1360          49 :     pfv[O1[2]] = O2[3+j];
    1361          49 :     pfv[O1[3]] = O2[4 - (j << 1)];
    1362          49 :     pfv[O1[4]] = O2[2+j];
    1363         161 :     for (i = 0; i < 4; i++)
    1364             :     {
    1365         147 :       ulong g = 0;
    1366         147 :       switch (i)
    1367             :       {
    1368          49 :       case 0: break;
    1369          49 :       case 1: lswap(O3[1], O3[2]); lswap(O3[3], O3[4]); break;
    1370          35 :       case 2: lswap(O3[1], O3[4]); lswap(O3[2], O3[3]); break;
    1371          14 :       case 3: lswap(O3[1], O3[2]); lswap(O3[3], O3[4]); break;
    1372             :       }
    1373         147 :       pfv[O2[1]]          = O3[1];
    1374         147 :       pfv[O2[3+j]]        = O3[4-j];
    1375         147 :       pfv[O2[4 - (j<<1)]] = O3[2 + (j<<1)];
    1376         147 :       pfv[O2[2+j]]        = O3[3-j];
    1377         147 :       pfv[O3[1]]          = O1[1];
    1378         147 :       pfv[O3[4-j]]        = O1[2];
    1379         147 :       pfv[O3[2 + (j<<1)]] = O1[3];
    1380         147 :       pfv[O3[3-j]]        = O1[4];
    1381         147 :       for (k = 1; k <= n; k++) g += mael(mt,k,pfv[k]);
    1382         147 :       if (headlongisint(g,n) && galois_test_perm(td, pfv))
    1383             :       {
    1384          35 :         avma = av;
    1385          35 :         if (DEBUGLEVEL >= 1)
    1386           0 :           err_printf("A4GaloisConj: %ld hop over %d iterations max\n",
    1387             :                      hop, 10395 + 68);
    1388          35 :         return res;
    1389             :       }
    1390         112 :       hop++; avma = av2;
    1391             :     }
    1392             :   }
    1393           0 :   avma = ltop; return gen_0; /* Fail */
    1394             : }
    1395             : 
    1396             : /* S4 */
    1397             : static void
    1398         196 : s4makelift(GEN u, struct galois_lift *gl, GEN liftpow)
    1399             : {
    1400         196 :   GEN s = automorphismlift(u, gl, NULL);
    1401             :   long i;
    1402         196 :   gel(liftpow,1) = s;
    1403        4508 :   for (i = 2; i < lg(liftpow); i++)
    1404        4312 :     gel(liftpow,i) = FpXQ_mul(gel(liftpow,i-1), s, gl->TQ, gl->Q);
    1405         196 : }
    1406             : static long
    1407        3437 : s4test(GEN u, GEN liftpow, struct galois_lift *gl, GEN phi)
    1408             : {
    1409        3437 :   pari_sp av = avma;
    1410             :   GEN res, Q, Q2;
    1411        3437 :   long bl, i, d = lg(u)-2;
    1412             :   pari_timer ti;
    1413        3437 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
    1414        3437 :   if (!d) return 0;
    1415        3437 :   Q = gl->Q; Q2 = shifti(Q,-1);
    1416        3437 :   res = gel(u,2);
    1417       80794 :   for (i = 1; i < d; i++)
    1418       77357 :     if (lg(gel(liftpow,i))>2)
    1419       77357 :       res = addii(res, mulii(gmael(liftpow,i,2), gel(u,i+2)));
    1420        3437 :   res = remii(res,Q);
    1421        3437 :   if (gl->den != gen_1) res = mulii(res, gl->den);
    1422        3437 :   res = centermodii(res, Q,Q2);
    1423        3437 :   if (abscmpii(res, gl->gb->bornesol) > 0) { avma = av; return 0; }
    1424         273 :   res = scalar_ZX_shallow(gel(u,2),varn(u));
    1425        6090 :   for (i = 1; i < d ; i++)
    1426        5817 :     if (lg(gel(liftpow,i))>2)
    1427        5817 :       res = ZX_add(res, ZX_Z_mul(gel(liftpow,i), gel(u,i+2)));
    1428         273 :   res = FpX_red(res, Q);
    1429         273 :   if (gl->den != gen_1) res = FpX_Fp_mul(res, gl->den, Q);
    1430         273 :   res = FpX_center(res, Q, shifti(Q,-1));
    1431         273 :   bl = poltopermtest(res, gl, phi);
    1432         273 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "s4test()");
    1433         273 :   avma = av; return bl;
    1434             : }
    1435             : 
    1436             : static GEN
    1437         588 : aux(long a, long b, GEN T, GEN M, GEN p, GEN *pu)
    1438             : {
    1439         588 :   *pu = FpX_mul(gel(T,b), gel(T,a),p);
    1440        1764 :   return FpX_chinese_coprime(gmael(M,a,b), gmael(M,b,a),
    1441        1176 :                              gel(T,b), gel(T,a), *pu, p);
    1442             : }
    1443             : 
    1444             : static GEN
    1445         196 : s4releveauto(GEN misom,GEN Tmod,GEN Tp,GEN p,long a1,long a2,long a3,long a4,long a5,long a6)
    1446             : {
    1447         196 :   pari_sp av = avma;
    1448             :   GEN u4,u5;
    1449             :   GEN pu1, pu2, pu3, pu4;
    1450         196 :   GEN u1 = aux(a1, a2, Tmod, misom, p, &pu1);
    1451         196 :   GEN u2 = aux(a3, a4, Tmod, misom, p, &pu2);
    1452         196 :   GEN u3 = aux(a5, a6, Tmod, misom, p, &pu3);
    1453         196 :   pu4 = FpX_mul(pu1,pu2,p);
    1454         196 :   u4 = FpX_chinese_coprime(u1,u2,pu1,pu2,pu4,p);
    1455         196 :   u5 = FpX_chinese_coprime(u4,u3,pu4,pu3,Tp,p);
    1456         196 :   return gerepileupto(av, u5);
    1457             : }
    1458             : static GEN
    1459        5607 : lincomb(GEN A, GEN B, GEN pauto, long j)
    1460             : {
    1461        5607 :   long k = (-j) & 3;
    1462        5607 :   if (j == k) return ZX_mul(ZX_add(A,B), gel(pauto, j+1));
    1463        2870 :   return ZX_add(ZX_mul(A, gel(pauto, j+1)), ZX_mul(B, gel(pauto, k+1)));
    1464             : }
    1465             : /* FIXME: could use the intheadlong technique */
    1466             : static GEN
    1467          28 : s4galoisgen(struct galois_lift *gl)
    1468             : {
    1469          28 :   const long n = 24;
    1470             :   struct galois_testlift gt;
    1471          28 :   pari_sp av, ltop2, ltop = avma;
    1472             :   long i, j;
    1473          28 :   GEN sigma, tau, phi, res, r1,r2,r3,r4, pj, p = gl->p, Q = gl->Q, TQ = gl->TQ;
    1474             :   GEN sg, Tp, Tmod, isom, isominv, misom, Bcoeff, pauto, liftpow, aut;
    1475             : 
    1476          28 :   res = cgetg(3, t_VEC);
    1477          28 :   r1 = cgetg(n+1, t_VECSMALL);
    1478          28 :   r2 = cgetg(n+1, t_VECSMALL);
    1479          28 :   r3 = cgetg(n+1, t_VECSMALL);
    1480          28 :   r4 = cgetg(n+1, t_VECSMALL);
    1481          28 :   gel(res,1)= mkvec4(r1,r2,r3,r4);
    1482          28 :   gel(res,2) = mkvecsmall4(2,2,3,2);
    1483          28 :   ltop2 = avma;
    1484          28 :   sg = identity_perm(6);
    1485          28 :   pj = zero_zv(6);
    1486          28 :   sigma = cgetg(n+1, t_VECSMALL);
    1487          28 :   tau = cgetg(n+1, t_VECSMALL);
    1488          28 :   phi = cgetg(n+1, t_VECSMALL);
    1489          28 :   Tp = FpX_red(gl->T,p);
    1490          28 :   Tmod = gel(FpX_factor(Tp,p), 1);
    1491          28 :   isom    = cgetg(lg(Tmod), t_VEC);
    1492          28 :   isominv = cgetg(lg(Tmod), t_VEC);
    1493          28 :   misom   = cgetg(lg(Tmod), t_MAT);
    1494          28 :   aut = galoisdolift(gl, NULL);
    1495          28 :   inittestlift(aut, Tmod, gl, &gt);
    1496          28 :   Bcoeff = gt.bezoutcoeff;
    1497          28 :   pauto = gt.pauto;
    1498         196 :   for (i = 1; i < lg(isom); i++)
    1499             :   {
    1500         168 :     gel(misom,i) = cgetg(lg(Tmod), t_COL);
    1501         168 :     gel(isom,i) = FpX_ffisom(gel(Tmod,1), gel(Tmod,i), p);
    1502         168 :     if (DEBUGLEVEL >= 6)
    1503           0 :       err_printf("S4GaloisConj: Computing isomorphisms %d:%Ps\n", i,
    1504           0 :                  gel(isom,i));
    1505         168 :     gel(isominv,i) = FpXQ_ffisom_inv(gel(isom,i), gel(Tmod,i),p);
    1506             :   }
    1507         196 :   for (i = 1; i < lg(isom); i++)
    1508        1176 :     for (j = 1; j < lg(isom); j++)
    1509        2016 :       gmael(misom,i,j) = FpX_FpXQ_eval(gel(isominv,i),gel(isom,j),
    1510        1008 :                                          gel(Tmod,j),p);
    1511          28 :   liftpow = cgetg(24, t_VEC);
    1512          28 :   av = avma;
    1513          49 :   for (i = 0; i < 3; i++, avma = av)
    1514             :   {
    1515             :     pari_sp av1, av2, av3;
    1516             :     GEN u, u1, u2, u3;
    1517             :     long j1, j2, j3;
    1518          49 :     if (i)
    1519             :     {
    1520          21 :       if (i == 1) { lswap(sg[2],sg[3]); }
    1521           0 :       else        { lswap(sg[1],sg[3]); }
    1522             :     }
    1523          49 :     u = s4releveauto(misom,Tmod,Tp,p,sg[1],sg[2],sg[3],sg[4],sg[5],sg[6]);
    1524          49 :     s4makelift(u, gl, liftpow);
    1525          49 :     av1 = avma;
    1526         182 :     for (j1 = 0; j1 < 4; j1++, avma = av1)
    1527             :     {
    1528         161 :       u1 = lincomb(gel(Bcoeff,sg[5]),gel(Bcoeff,sg[6]), pauto,j1);
    1529         161 :       u1 = FpX_rem(u1, TQ, Q); av2 = avma;
    1530         742 :       for (j2 = 0; j2 < 4; j2++, avma = av2)
    1531             :       {
    1532         609 :         u2 = lincomb(gel(Bcoeff,sg[3]),gel(Bcoeff,sg[4]), pauto,j2);
    1533         609 :         u2 = FpX_rem(FpX_add(u1, u2, Q), TQ,Q); av3 = avma;
    1534        2982 :         for (j3 = 0; j3 < 4; j3++, avma = av3)
    1535             :         {
    1536        2401 :           u3 = lincomb(gel(Bcoeff,sg[1]),gel(Bcoeff,sg[2]), pauto,j3);
    1537        2401 :           u3 = FpX_rem(FpX_add(u2, u3, Q), TQ,Q);
    1538        2401 :           if (DEBUGLEVEL >= 4)
    1539           0 :             err_printf("S4GaloisConj: Testing %d/3:%d/4:%d/4:%d/4:%Ps\n",
    1540             :                        i,j1,j2,j3, sg);
    1541        2401 :           if (s4test(u3, liftpow, gl, sigma))
    1542             :           {
    1543          28 :             pj[1] = j3;
    1544          28 :             pj[2] = j2;
    1545          28 :             pj[3] = j1; goto suites4;
    1546             :           }
    1547             :         }
    1548             :       }
    1549             :     }
    1550             :   }
    1551           0 :   avma = ltop; return gen_0;
    1552             : suites4:
    1553          28 :   if (DEBUGLEVEL >= 4) err_printf("S4GaloisConj: sigma=%Ps\n", sigma);
    1554          28 :   if (DEBUGLEVEL >= 4) err_printf("S4GaloisConj: pj=%Ps\n", pj);
    1555          28 :   avma = av;
    1556          63 :   for (j = 1; j <= 3; j++)
    1557             :   {
    1558             :     pari_sp av2, av3;
    1559             :     GEN u;
    1560             :     long w, l, z;
    1561          63 :     z = sg[1]; sg[1] = sg[3]; sg[3] = sg[5]; sg[5] = z;
    1562          63 :     z = sg[2]; sg[2] = sg[4]; sg[4] = sg[6]; sg[6] = z;
    1563          63 :     z = pj[1]; pj[1] = pj[2]; pj[2] = pj[3]; pj[3] = z;
    1564         154 :     for (l = 0; l < 2; l++, avma = av)
    1565             :     {
    1566         119 :       u = s4releveauto(misom,Tmod,Tp,p,sg[1],sg[3],sg[2],sg[4],sg[5],sg[6]);
    1567         119 :       s4makelift(u, gl, liftpow);
    1568         119 :       av2 = avma;
    1569         329 :       for (w = 0; w < 4; w += 2, avma = av2)
    1570             :       {
    1571             :         GEN uu;
    1572         238 :         pj[6] = (w + pj[3]) & 3;
    1573         238 :         uu = lincomb(gel(Bcoeff,sg[5]),gel(Bcoeff,sg[6]), pauto, pj[6]);
    1574         238 :         uu = FpX_rem(FpX_red(uu,Q), TQ, Q);
    1575         238 :         av3 = avma;
    1576        1120 :         for (i = 0; i < 4; i++, avma = av3)
    1577             :         {
    1578             :           GEN u;
    1579         910 :           pj[4] = i;
    1580         910 :           pj[5] = (i + pj[2] - pj[1]) & 3;
    1581         910 :           if (DEBUGLEVEL >= 4)
    1582           0 :             err_printf("S4GaloisConj: Testing %d/3:%d/2:%d/2:%d/4:%Ps:%Ps\n",
    1583             :                        j-1, w >> 1, l, i, sg, pj);
    1584        2730 :           u = ZX_add(lincomb(gel(Bcoeff,sg[1]),gel(Bcoeff,sg[3]), pauto,pj[4]),
    1585        2730 :                      lincomb(gel(Bcoeff,sg[2]),gel(Bcoeff,sg[4]), pauto,pj[5]));
    1586         910 :           u = FpX_rem(FpX_add(uu,u,Q), TQ, Q);
    1587         910 :           if (s4test(u, liftpow, gl, tau)) goto suites4_2;
    1588             :         }
    1589             :       }
    1590          91 :       lswap(sg[3], sg[4]);
    1591          91 :       pj[2] = (-pj[2]) & 3;
    1592             :     }
    1593             :   }
    1594           0 :   avma = ltop; return gen_0;
    1595             : suites4_2:
    1596          28 :   avma = av;
    1597             :   {
    1598          28 :     long abc = (pj[1] + pj[2] + pj[3]) & 3;
    1599          28 :     long abcdef = ((abc + pj[4] + pj[5] - pj[6]) & 3) >> 1;
    1600             :     GEN u;
    1601             :     pari_sp av2;
    1602          28 :     u = s4releveauto(misom,Tmod,Tp,p,sg[1],sg[4],sg[2],sg[5],sg[3],sg[6]);
    1603          28 :     s4makelift(u, gl, liftpow);
    1604          28 :     av2 = avma;
    1605         126 :     for (j = 0; j < 8; j++)
    1606             :     {
    1607             :       long h, g, i;
    1608         126 :       h = j & 3;
    1609         126 :       g = (abcdef + ((j & 4) >> 1)) & 3;
    1610         126 :       i = (h + abc - g) & 3;
    1611         252 :       u = ZX_add(   lincomb(gel(Bcoeff,sg[1]), gel(Bcoeff,sg[4]), pauto, g),
    1612         252 :                     lincomb(gel(Bcoeff,sg[2]), gel(Bcoeff,sg[5]), pauto, h));
    1613         126 :       u = FpX_add(u, lincomb(gel(Bcoeff,sg[3]), gel(Bcoeff,sg[6]), pauto, i),Q);
    1614         126 :       u = FpX_rem(u, TQ, Q);
    1615         126 :       if (DEBUGLEVEL >= 4)
    1616           0 :         err_printf("S4GaloisConj: Testing %d/8 %d:%d:%d\n", j,g,h,i);
    1617         126 :       if (s4test(u, liftpow, gl, phi)) break;
    1618          98 :       avma = av2;
    1619             :     }
    1620             :   }
    1621          28 :   if (j == 8) { avma = ltop; return gen_0; }
    1622         700 :   for (i = 1; i <= n; i++)
    1623             :   {
    1624         672 :     r1[i] = sigma[tau[i]];
    1625         672 :     r2[i] = phi[sigma[tau[phi[i]]]];
    1626         672 :     r3[i] = phi[sigma[i]];
    1627         672 :     r4[i] = sigma[i];
    1628             :   }
    1629          28 :   avma = ltop2; return res;
    1630             : }
    1631             : 
    1632             : static GEN
    1633         469 : galoisfindgroups(GEN lo, GEN sg, long f)
    1634             : {
    1635         469 :   pari_sp ltop = avma;
    1636             :   long i, j, k;
    1637         469 :   GEN V = cgetg(lg(lo), t_VEC);
    1638        1750 :   for(j=1,i=1; i<lg(lo); i++)
    1639             :   {
    1640        1281 :     pari_sp av = avma;
    1641        1281 :     GEN loi = gel(lo,i), W = cgetg(lg(loi),t_VECSMALL);
    1642        1281 :     for (k=1; k<lg(loi); k++) W[k] = loi[k] % f;
    1643        1281 :     W = vecsmall_uniq(W);
    1644        1281 :     if (zv_equal(W, sg)) gel(V,j++) = loi;
    1645        1281 :     avma = av;
    1646             :   }
    1647         469 :   setlg(V,j); return gerepilecopy(ltop,V);
    1648             : }
    1649             : 
    1650             : static long
    1651        2261 : galoisfrobeniustest(GEN aut, struct galois_lift *gl, GEN frob)
    1652             : {
    1653        2261 :   pari_sp av = avma;
    1654        2261 :   GEN tlift = aut;
    1655             :   long res;
    1656        2261 :   if (gl->den != gen_1) tlift = FpX_Fp_mul(tlift, gl->den, gl->Q);
    1657        2261 :   tlift = FpX_center(tlift, gl->Q, shifti(gl->Q,-1));
    1658        2261 :   res = poltopermtest(tlift, gl, frob);
    1659        2261 :   avma = av; return res;
    1660             : }
    1661             : 
    1662             : static GEN
    1663         714 : galoismakepsi(long g, GEN sg, GEN pf)
    1664             : {
    1665         714 :   GEN psi=cgetg(g+1,t_VECSMALL);
    1666             :   long i;
    1667         714 :   for (i = 1; i < g; i++) psi[i] = sg[pf[i]];
    1668         714 :   psi[g] = sg[1]; return psi;
    1669             : }
    1670             : 
    1671             : static GEN
    1672        2163 : galoisfrobeniuslift(GEN T, GEN den, GEN L,  GEN Lden,
    1673             :     struct galois_frobenius *gf,  struct galois_borne *gb, long mindeg)
    1674             : {
    1675        2163 :   pari_sp ltop=avma, av2;
    1676             :   struct galois_testlift gt;
    1677             :   struct galois_lift gl;
    1678        2163 :   long i, j, k, n = lg(L)-1, deg = 1, g = lg(gf->Tmod)-1;
    1679        2163 :   GEN F,Fp,Fe, aut, frob, ip = utoipos(gf->p), res = cgetg(lg(L), t_VECSMALL);
    1680        2163 :   gf->psi = const_vecsmall(g,1);
    1681        2163 :   av2 = avma;
    1682        2163 :   initlift(T, den, ip, L, Lden, gb, &gl);
    1683        2163 :   if (DEBUGLEVEL >= 4)
    1684           0 :     err_printf("GaloisConj: p=%ld e=%ld deg=%ld fp=%ld\n",
    1685             :                             gf->p, gl.e, deg, gf->fp);
    1686        2163 :   aut = galoisdolift(&gl, res);
    1687        2163 :   if (!aut || galoisfrobeniustest(aut,&gl,res))
    1688             :   {
    1689        1421 :     avma = av2; gf->deg = gf->fp; return res;
    1690             :   }
    1691         742 :   if (gf->fp <= mindeg) return NULL;
    1692         742 :   inittestlift(aut,gf->Tmod, &gl, &gt);
    1693         742 :   gt.C = cgetg(gf->fp+1,t_VEC);
    1694         742 :   gt.Cd= cgetg(gf->fp+1,t_VEC);
    1695        5187 :   for (i = 1; i <= gf->fp; i++) {
    1696        4445 :     gel(gt.C,i)  = zero_zv(gt.g);
    1697        4445 :     gel(gt.Cd,i) = zero_zv(gt.g);
    1698             :   }
    1699             : 
    1700         742 :   F =factoru(gf->fp);
    1701         742 :   Fp = gel(F,1);
    1702         742 :   Fe = gel(F,2);
    1703         742 :   frob = cgetg(lg(L), t_VECSMALL);
    1704        1582 :   for(k=lg(Fp)-1;k>=1;k--)
    1705             :   {
    1706         840 :     pari_sp btop=avma;
    1707         840 :     GEN psi=NULL, fres=NULL, sg = identity_perm(1);
    1708         840 :     long el=gf->fp, dg=1, dgf=1, e, pr;
    1709        2051 :     for(e=1; e<=Fe[k]; e++)
    1710             :     {
    1711             :       GEN lo, pf;
    1712             :       long l;
    1713        1372 :       dg *= Fp[k]; el /= Fp[k];
    1714        1869 :       if (dg < mindeg) continue;
    1715        1372 :       if (DEBUGLEVEL>=4) err_printf("Trying degre %d.\n",dg);
    1716        1372 :       if (galoisfrobeniustest(gel(gt.pauto,el+1),&gl,frob))
    1717             :       {
    1718         497 :         psi = const_vecsmall(g,1);
    1719         497 :         dgf = dg; fres = gcopy(frob); continue;
    1720             :       }
    1721         875 :       lo = listznstarelts(dg, n / gf->fp);
    1722         875 :       if (e!=1) lo = galoisfindgroups(lo, sg, dgf);
    1723         875 :       if (DEBUGLEVEL>=4) err_printf("Galoisconj:Subgroups list:%Ps\n", lo);
    1724        1799 :       for (l = 1; l < lg(lo); l++)
    1725        1638 :         if (lg(gel(lo,l))>2 && frobeniusliftall(gel(lo,l), el, &pf,&gl,&gt, frob))
    1726             :         {
    1727         714 :           sg  = gcopy(gel(lo,l));
    1728         714 :           psi = galoismakepsi(g,sg,pf);
    1729         714 :           dgf = dg; fres = gcopy(frob); break;
    1730             :         }
    1731         875 :       if (l == lg(lo)) break;
    1732             :     }
    1733         840 :     if (dgf == 1) { avma = btop; continue; }
    1734         770 :     pr = deg*dgf;
    1735         770 :     if (deg == 1)
    1736             :     {
    1737         679 :       for(i=1;i<lg(res);i++) res[i]=fres[i];
    1738         679 :       for(i=1;i<lg(psi);i++) gf->psi[i]=psi[i];
    1739             :     }
    1740             :     else
    1741             :     {
    1742          91 :       GEN cp = perm_mul(res,fres);
    1743          91 :       for(i=1;i<lg(res);i++) res[i] = cp[i];
    1744          91 :       for(i=1;i<lg(psi);i++) gf->psi[i] = (dgf*gf->psi[i] + deg*psi[i]) % pr;
    1745             :     }
    1746         770 :     deg = pr; avma = btop;
    1747             :   }
    1748        5187 :   for (i = 1; i <= gf->fp; i++)
    1749       20146 :     for (j = 1; j <= gt.g; j++)
    1750       15701 :       if (mael(gt.C,i,j)) gunclone(gmael(gt.C,i,j));
    1751         742 :   if (DEBUGLEVEL>=4 && res) err_printf("Best lift: %d\n",deg);
    1752         742 :   if (deg==1) { avma = ltop; return NULL; }
    1753             :   else
    1754             :   {
    1755             :     /* Normalize result so that psi[g]=1 */
    1756         679 :     long im = Fl_inv(gf->psi[g], deg);
    1757         679 :     GEN cp = perm_pow(res, im);
    1758         679 :     for(i=1;i<lg(res);i++) res[i] = cp[i];
    1759         679 :     for(i=1;i<lg(gf->psi);i++) gf->psi[i] = Fl_mul(im, gf->psi[i], deg);
    1760         679 :     avma = av2; gf->deg = deg; return res;
    1761             :   }
    1762             : }
    1763             : 
    1764             : /* return NULL if not Galois */
    1765             : static GEN
    1766        2100 : galoisfindfrobenius(GEN T, GEN L, GEN den, struct galois_frobenius *gf,
    1767             :     struct galois_borne *gb, const struct galois_analysis *ga)
    1768             : {
    1769        2100 :   pari_sp ltop = avma, av;
    1770        2100 :   long Try = 0, n = degpol(T), deg, gmask = (ga->group&ga_ext_2)? 3: 1;
    1771        2100 :   GEN frob, Lden = makeLden(L,den,gb);
    1772             :   forprime_t S;
    1773             : 
    1774        2100 :   u_forprime_init(&S, ga->p, ULONG_MAX);
    1775        2100 :   av = avma;
    1776        2100 :   deg = gf->deg = ga->deg;
    1777        4263 :   while ((gf->p = u_forprime_next(&S)))
    1778             :   {
    1779             :     pari_sp lbot;
    1780             :     GEN Ti, Tp;
    1781             :     long nb, d;
    1782        2163 :     avma = av;
    1783        2163 :     Tp = ZX_to_Flx(T, gf->p);
    1784        2163 :     if (!Flx_is_squarefree(Tp, gf->p)) continue;
    1785        2163 :     Ti = gel(Flx_factor(Tp, gf->p), 1);
    1786        2163 :     nb = lg(Ti)-1; d = degpol(gel(Ti,1));
    1787        2163 :     if (d < ga->mindeg) continue;
    1788        2163 :     if (nb > 1 && degpol(gel(Ti,nb)) != d) { avma = ltop; return NULL; }
    1789        2163 :     if (((gmask&1)==0 || d % deg) && ((gmask&2)==0 || odd(d))) continue;
    1790        2163 :     if (DEBUGLEVEL >= 1) err_printf("GaloisConj: Trying p=%ld\n", gf->p);
    1791        2163 :     FlxV_to_ZXV_inplace(Ti);
    1792        2163 :     gf->fp = d;
    1793        2163 :     gf->Tmod = Ti; lbot = avma;
    1794        2163 :     frob = galoisfrobeniuslift(T, den, L, Lden, gf, gb, ga->mindeg);
    1795        2163 :     if (frob)
    1796             :     {
    1797             :       GEN *gptr[3];
    1798        2100 :       if (gf->deg < ga->mindeg)
    1799             :       {
    1800           0 :         if (DEBUGLEVEL >= 4)
    1801           0 :           err_printf("GaloisConj: lift degree too small %ld < %ld\n",
    1802             :                      gf->deg, ga->mindeg);
    1803           0 :         continue;
    1804             :       }
    1805        2100 :       gf->Tmod = gcopy(Ti);
    1806        2100 :       gptr[0]=&gf->Tmod; gptr[1]=&gf->psi; gptr[2]=&frob;
    1807        2100 :       gerepilemanysp(ltop,lbot,gptr,3); return frob;
    1808             :     }
    1809          63 :     if ((ga->group&ga_all_normal) && d % deg == 0) gmask &= ~1;
    1810             :     /* The first prime degree is always divisible by deg, so we don't
    1811             :      * have to worry about ext_2 being used before regular supersolvable*/
    1812          63 :     if (!gmask) { avma = ltop; return NULL; }
    1813          63 :     if ((ga->group&ga_non_wss) && ++Try > ((3*n)>>1))
    1814             :     {
    1815           0 :       pari_warn(warner,"Galois group probably not weakly super solvable");
    1816           0 :       return NULL;
    1817             :     }
    1818             :   }
    1819           0 :   if (!gf->p) pari_err_OVERFLOW("galoisfindfrobenius [ran out of primes]");
    1820           0 :   return NULL;
    1821             : }
    1822             : 
    1823             : /* compute g such that tau(Pmod[#])= tau(Pmod[g]) */
    1824             : 
    1825             : static long
    1826        1645 : get_image(GEN tau, GEN P, GEN Pmod, GEN p)
    1827             : {
    1828        1645 :   pari_sp av = avma;
    1829        1645 :   long g, gp = lg(Pmod)-1;
    1830        1645 :   tau = RgX_to_FpX(tau, p);
    1831        1645 :   tau = FpX_FpXQ_eval(gel(Pmod, gp), tau, P, p);
    1832        1645 :   tau = FpX_normalize(FpX_gcd(P, tau, p), p);
    1833        3269 :   for (g = 1; g <= gp; g++)
    1834        3269 :     if (ZX_equal(tau, gel(Pmod,g))) { avma = av; return g; }
    1835           0 :   avma = av; return 0;
    1836             : }
    1837             : 
    1838             : static GEN
    1839             : galoisgen(GEN T, GEN L, GEN M, GEN den, struct galois_borne *gb,
    1840             :           const struct galois_analysis *ga);
    1841             : static GEN
    1842        1316 : galoisgenfixedfield(GEN Tp, GEN Pmod, GEN V, GEN ip, struct galois_borne *gb)
    1843             : {
    1844             :   GEN  P, PL, Pden, PM, Pp;
    1845             :   GEN  tau, PG, Pg;
    1846             :   long g, lP;
    1847        1316 :   long x=varn(Tp);
    1848        1316 :   P=gel(V,3);
    1849        1316 :   PL=gel(V,2);
    1850        1316 :   Pp = FpX_red(P,ip);
    1851        1316 :   if (DEBUGLEVEL>=6)
    1852           0 :     err_printf("GaloisConj: Fixed field %Ps\n",P);
    1853        1316 :   if (degpol(P)==2)
    1854             :   {
    1855         917 :     PG=cgetg(3,t_VEC);
    1856         917 :     gel(PG,1) = mkvec( mkvecsmall2(2,1) );
    1857         917 :     gel(PG,2) = mkvecsmall(2);
    1858         917 :     tau = deg1pol_shallow(gen_m1, negi(gel(P,3)), x);
    1859         917 :     g = get_image(tau, Pp, Pmod, ip);
    1860         917 :     if (!g) return NULL;
    1861         917 :     Pg = mkvecsmall(g);
    1862             :   }
    1863             :   else
    1864             :   {
    1865             :     struct galois_analysis Pga;
    1866             :     struct galois_borne Pgb;
    1867             :     GEN mod, mod2;
    1868             :     long j;
    1869         406 :     if (!galoisanalysis(P, &Pga, 0)) return NULL;
    1870         392 :     Pgb.l = gb->l;
    1871         392 :     Pden = galoisborne(P, NULL, &Pgb, degpol(P));
    1872             : 
    1873         392 :     if (Pgb.valabs > gb->valabs)
    1874             :     {
    1875          28 :       if (DEBUGLEVEL>=4)
    1876           0 :         err_printf("GaloisConj: increase prec of p-adic roots of %ld.\n"
    1877           0 :             ,Pgb.valabs-gb->valabs);
    1878          28 :       PL = ZpX_liftroots(P,PL,gb->l,Pgb.valabs);
    1879             :     }
    1880         364 :     else if (Pgb.valabs < gb->valabs)
    1881         338 :       PL = FpC_red(PL, Pgb.ladicabs);
    1882         392 :     PM = FpV_invVandermonde(PL, Pden, Pgb.ladicabs);
    1883         392 :     PG = galoisgen(P, PL, PM, Pden, &Pgb, &Pga);
    1884         392 :     if (PG == gen_0) return NULL;
    1885         392 :     lP = lg(gel(PG,1));
    1886         392 :     mod = Pgb.ladicabs; mod2 = shifti(mod, -1);
    1887         392 :     Pg = cgetg(lP, t_VECSMALL);
    1888        1120 :     for (j = 1; j < lP; j++)
    1889             :     {
    1890         728 :       pari_sp btop=avma;
    1891         728 :       tau = permtopol(gmael(PG,1,j), PL, PM, Pden, mod, mod2, x);
    1892         728 :       g = get_image(tau, Pp, Pmod, ip);
    1893         728 :       if (!g) return NULL;
    1894         728 :       Pg[j] = g;
    1895         728 :       avma = btop;
    1896             :     }
    1897             :   }
    1898        1309 :   return mkvec2(PG,Pg);
    1899             : }
    1900             : 
    1901             : /* Let sigma^m=1,  tau*sigma*tau^-1=sigma^s.
    1902             :  * Compute n so that (sigma*tau)^e = sigma^n*tau^e
    1903             :  * We have n = sum_{k=0}^{e-1} s^k mod m.
    1904             :  * so n*(1-s) = 1-s^e mod m
    1905             :  * Unfortunately (1-s) might not invertible mod m.
    1906             :  */
    1907             : 
    1908             : static long
    1909        3766 : stpow(long s, long e, long m)
    1910             : {
    1911             :   long i;
    1912        3766 :   long n = 1;
    1913        6020 :   for (i = 1; i < e; i++)
    1914        2254 :     n = (1 + n * s) % m;
    1915        3766 :   return n;
    1916             : }
    1917             : 
    1918             : static GEN
    1919        1645 : wpow(long s, long m, long e, long n)
    1920             : {
    1921        1645 :   GEN   w = cgetg(n+1,t_VECSMALL);
    1922        1645 :   long si = s;
    1923             :   long i;
    1924        1645 :   w[1] = 1;
    1925        1645 :   for(i=2; i<=n; i++) w[i] = w[i-1]*e;
    1926        3528 :   for(i=n; i>=1; i--)
    1927             :   {
    1928        1883 :     si = Fl_powu(si,e,m);
    1929        1883 :     w[i] = Fl_mul(s-1, stpow(si, w[i], m), m);
    1930             :   }
    1931        1645 :   return w;
    1932             : }
    1933             : 
    1934             : static GEN
    1935        1645 : galoisgenliftauto(GEN O, GEN gj, long s, long n, struct galois_test *td)
    1936             : {
    1937        1645 :   pari_sp av = avma;
    1938             :   long sr, k;
    1939        1645 :   long deg = lg(gel(O,1))-1;
    1940        1645 :   GEN  X  = cgetg(lg(O), t_VECSMALL);
    1941        1645 :   GEN  oX = cgetg(lg(O), t_VECSMALL);
    1942        1645 :   GEN  B  = perm_cycles(gj);
    1943        1645 :   long oj = lg(gel(B,1)) - 1;
    1944        1645 :   GEN  F  = factoru(oj);
    1945        1645 :   GEN  Fp = gel(F,1);
    1946        1645 :   GEN  Fe = gel(F,2);
    1947        1645 :   GEN  pf = identity_perm(n);
    1948        1645 :   if (DEBUGLEVEL >= 6)
    1949           0 :     err_printf("GaloisConj: %Ps of relative order %d\n", gj, oj);
    1950        3290 :   for (k=lg(Fp)-1; k>=1; k--)
    1951             :   {
    1952        1645 :     long f, dg = 1, el = oj, osel = 1, a = 0;
    1953        1645 :     long p  = Fp[k], e  = Fe[k], op = oj / upowuu(p,e);
    1954             :     long i;
    1955        1645 :     GEN  pf1 = NULL, w, wg, Be = cgetg(e+1,t_VEC);
    1956        1645 :     gel(Be,e) = cyc_pow(B, op);
    1957        1645 :     for(i=e-1; i>=1; i--) gel(Be,i) = cyc_pow(gel(Be,i+1), p);
    1958        1645 :     w = wpow(Fl_powu(s,op,deg),deg,p,e);
    1959        1645 :     wg = cgetg(e+2,t_VECSMALL);
    1960        1645 :     wg[e+1] = deg;
    1961        1645 :     for (i=e; i>=1; i--) wg[i] = ugcd(wg[i+1], w[i]);
    1962        1645 :     for (i=1; i<lg(O); i++) oX[i] = 0;
    1963        3528 :     for (f=1; f<=e; f++)
    1964             :     {
    1965             :       long sel, t;
    1966        1883 :       GEN Bel = gel(Be,f);
    1967        1883 :       dg *= p; el /= p;
    1968        1883 :       sel = Fl_powu(s,el,deg);
    1969        1883 :       if (DEBUGLEVEL >= 6) err_printf("GaloisConj: B=%Ps\n", Bel);
    1970        1883 :       sr  = cgcd(stpow(sel,p,deg),deg);
    1971        1883 :       if (DEBUGLEVEL >= 6)
    1972           0 :         err_printf("GaloisConj: exp %d: s=%ld [%ld] a=%ld w=%ld wg=%ld sr=%ld\n",
    1973           0 :             dg, sel, deg, a, w[f], wg[f+1], sr);
    1974        2268 :       for (t = 0; t < sr; t++)
    1975        2268 :         if ((a+t*w[f])%wg[f+1]==0)
    1976             :         {
    1977             :           long i, j, k, st;
    1978        2128 :           for (i = 1; i < lg(X); i++) X[i] = 0;
    1979        8449 :           for (i = 0; i < lg(X)-1; i+=dg)
    1980       13951 :             for (j = 1, k = p, st = t; k <= dg; j++, k += p)
    1981             :             {
    1982        7630 :               X[k+i] = (oX[j+i] + st)%deg;
    1983        7630 :               st = (t + st*osel)%deg;
    1984             :             }
    1985        2128 :           pf1 = testpermutation(O, Bel, X, sel, p, sr, td);
    1986        2128 :           if (pf1) break;
    1987             :         }
    1988        1883 :       if (!pf1) return NULL;
    1989        1883 :       for (i=1; i<lg(O); i++) oX[i] = X[i];
    1990        1883 :       osel = sel; a = (a+t*w[f])%deg;
    1991             :     }
    1992        1645 :     pf = perm_mul(pf, perm_pow(pf1, el));
    1993             :   }
    1994        1645 :   return gerepileuptoleaf(av, pf);
    1995             : }
    1996             : 
    1997             : static GEN
    1998        2163 : galoisgen(GEN T, GEN L, GEN M, GEN den, struct galois_borne *gb,
    1999             :           const struct galois_analysis *ga)
    2000             : {
    2001             :   struct galois_test td;
    2002             :   struct galois_frobenius gf;
    2003        2163 :   pari_sp ltop = avma;
    2004        2163 :   long p, deg, x, j, n = degpol(T), lP;
    2005             :   GEN sigma, Tmod, res, res1, ip, frob, O, PG, PG1, PG2, Pg;
    2006             : 
    2007        2163 :   if (!ga->deg) return gen_0;
    2008        2163 :   x = varn(T);
    2009        2163 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: denominator:%Ps\n", den);
    2010        2163 :   if (n == 12 && ga->ord==3 && !ga->p4)
    2011             :   { /* A4 is very probable: test it first */
    2012          35 :     pari_sp av = avma;
    2013          35 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Testing A4 first\n");
    2014          35 :     inittest(L, M, gb->bornesol, gb->ladicsol, &td);
    2015          35 :     PG = a4galoisgen(&td);
    2016          35 :     freetest(&td);
    2017          35 :     if (PG != gen_0) return gerepileupto(ltop, PG);
    2018           0 :     avma = av;
    2019             :   }
    2020        2128 :   if (n == 24 && ga->ord==3)
    2021             :   { /* S4 is very probable: test it first */
    2022          28 :     pari_sp av = avma;
    2023             :     struct galois_lift gl;
    2024          28 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Testing S4 first\n");
    2025          28 :     initlift(T, den, stoi(ga->p4), L, makeLden(L,den,gb), gb, &gl);
    2026          28 :     PG = s4galoisgen(&gl);
    2027          28 :     if (PG != gen_0) return gerepileupto(ltop, PG);
    2028           0 :     avma = av;
    2029             :   }
    2030        2100 :   frob = galoisfindfrobenius(T, L, den, &gf, gb, ga);
    2031        2100 :   if (!frob) { avma=ltop; return gen_0; }
    2032        2100 :   p = gf.p; ip = utoipos(p);
    2033        2100 :   Tmod = gf.Tmod;
    2034        2100 :   O = perm_cycles(frob);
    2035        2100 :   deg = lg(gel(O,1))-1;
    2036        2100 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: Orbit:%Ps\n", O);
    2037        2100 :   if (deg == n)        /* cyclic */
    2038         784 :     return gerepilecopy(ltop, mkvec2(mkvec(frob), mkvecsmall(deg)));
    2039        1316 :   sigma = permtopol(frob, L, M, den, gb->ladicabs, shifti(gb->ladicabs,-1), x);
    2040        1316 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: Frobenius:%Ps\n", sigma);
    2041             :   {
    2042        1316 :     pari_sp btop=avma;
    2043             :     GEN V, Tp, Sp, Pmod;
    2044        1316 :     GEN OL = fixedfieldorbits(O,L);
    2045        1316 :     V  = fixedfieldsympol(OL, gb->ladicabs, gb->l, ip, x);
    2046        1316 :     Tp = FpX_red(T,ip);
    2047        1316 :     Sp = sympol_aut_evalmod(gel(V,1),deg,sigma,Tp,ip);
    2048        1316 :     Pmod = fixedfieldfactmod(Sp,ip,Tmod);
    2049        1316 :     PG = galoisgenfixedfield(Tp, Pmod, V, ip, gb);
    2050        1316 :     if (PG == NULL) { avma = ltop; return gen_0; }
    2051        1309 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Back to Earth:%Ps\n", PG);
    2052        1309 :     PG=gerepilecopy(btop, PG);
    2053             :   }
    2054        1309 :   inittest(L, M, gb->bornesol, gb->ladicsol, &td);
    2055        1309 :   PG1 = gmael(PG, 1, 1); lP = lg(PG1);
    2056        1309 :   PG2 = gmael(PG, 1, 2);
    2057        1309 :   Pg = gel(PG, 2);
    2058        1309 :   res = cgetg(3, t_VEC);
    2059        1309 :   gel(res,1) = res1 = cgetg(lP + 1, t_VEC);
    2060        1309 :   gel(res,2) = vecsmall_prepend(PG2, deg);
    2061        1309 :   gel(res1, 1) = vecsmall_copy(frob);
    2062        2954 :   for (j = 1; j < lP; j++)
    2063             :   {
    2064        1645 :     GEN pf = galoisgenliftauto(O, gel(PG1, j), gf.psi[Pg[j]], n, &td);
    2065        1645 :     if (!pf) { freetest(&td); avma = ltop; return gen_0; }
    2066        1645 :     gel(res1, j+1) = pf;
    2067             :   }
    2068        1309 :   if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Fini!\n");
    2069        1309 :   freetest(&td);
    2070        1309 :   return gerepileupto(ltop, res);
    2071             : }
    2072             : 
    2073             : /* T = polcyclo(N) */
    2074             : static GEN
    2075          63 : conjcyclo(GEN T, long N)
    2076             : {
    2077          63 :   pari_sp av = avma;
    2078          63 :   long i, k = 1, d = eulerphiu(N), v = varn(T);
    2079          63 :   GEN L = cgetg(d+1,t_COL);
    2080         560 :   for (i=1; i<=N; i++)
    2081         497 :     if (ugcd(i, N)==1)
    2082             :     {
    2083         266 :       GEN s = pol_xn(i, v);
    2084         266 :       if (i >= d) s = ZX_rem(s, T);
    2085         266 :       gel(L,k++) = s;
    2086             :     }
    2087          63 :   return gerepileupto(av, gen_sort(L, (void*)&gcmp, &gen_cmp_RgX));
    2088             : }
    2089             : 
    2090             : /* T: polynomial or nf, den multiple of common denominator of solutions or
    2091             :  * NULL (unknown). If T is nf, and den unknown, use den = denom(nf.zk) */
    2092             : static GEN
    2093        3458 : galoisconj4_main(GEN T, GEN den, long flag)
    2094             : {
    2095        3458 :   pari_sp ltop = avma;
    2096             :   GEN nf, G, L, M, aut;
    2097             :   struct galois_analysis ga;
    2098             :   struct galois_borne gb;
    2099             :   long n;
    2100             :   pari_timer ti;
    2101             : 
    2102        3458 :   T = get_nfpol(T, &nf);
    2103        3458 :   n = poliscyclo(T);
    2104        3458 :   if (n) return flag? galoiscyclo(n, varn(T)): conjcyclo(T, n);
    2105        3206 :   n = degpol(T);
    2106        3206 :   if (nf)
    2107        2303 :   { if (!den) den = nf_get_zkden(nf); }
    2108             :   else
    2109             :   {
    2110         903 :     if (n <= 0) pari_err_IRREDPOL("galoisinit",T);
    2111         903 :     RgX_check_ZX(T, "galoisinit");
    2112         903 :     if (!ZX_is_squarefree(T))
    2113           7 :       pari_err_DOMAIN("galoisinit","issquarefree(pol)","=",gen_0,T);
    2114         896 :     if (!gequal1(gel(T,n+2))) pari_err_IMPL("galoisinit(non-monic)");
    2115             :   }
    2116        3192 :   if (n == 1)
    2117             :   {
    2118          21 :     if (!flag) { G = cgetg(2, t_COL); gel(G,1) = pol_x(varn(T)); return G;}
    2119          21 :     ga.l = 3;
    2120          21 :     ga.deg = 1;
    2121          21 :     den = gen_1;
    2122             :   }
    2123        3171 :   else if (!galoisanalysis(T, &ga, 1)) { avma = ltop; return utoipos(ga.p); }
    2124             : 
    2125        1792 :   if (den)
    2126             :   {
    2127        1309 :     if (typ(den) != t_INT) pari_err_TYPE("galoisconj4 [2nd arg integer]", den);
    2128        1309 :     den = absi(den);
    2129             :   }
    2130        1792 :   gb.l = utoipos(ga.l);
    2131        1792 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    2132        1792 :   den = galoisborne(T, den, &gb, degpol(T));
    2133        1792 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "galoisborne()");
    2134        1792 :   L = ZpX_roots(T, gb.l, gb.valabs);
    2135        1792 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "ZpX_roots");
    2136        1792 :   M = FpV_invVandermonde(L, den, gb.ladicabs);
    2137        1792 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "FpV_invVandermonde()");
    2138        1792 :   if (n == 1)
    2139             :   {
    2140          21 :     G = cgetg(3, t_VEC);
    2141          21 :     gel(G,1) = cgetg(1, t_VEC);
    2142          21 :     gel(G,2) = cgetg(1, t_VECSMALL);
    2143             :   }
    2144             :   else
    2145        1771 :     G = galoisgen(T, L, M, den, &gb, &ga);
    2146        1792 :   if (DEBUGLEVEL >= 6) err_printf("GaloisConj: %Ps\n", G);
    2147        1792 :   if (G == gen_0) { avma = ltop; return gen_0; }
    2148        1785 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    2149        1785 :   if (flag)
    2150             :   {
    2151        1344 :     GEN grp = cgetg(9, t_VEC);
    2152        1344 :     gel(grp,1) = ZX_copy(T);
    2153        1344 :     gel(grp,2) = mkvec3(utoipos(ga.l), utoipos(gb.valabs), icopy(gb.ladicabs));
    2154        1344 :     gel(grp,3) = ZC_copy(L);
    2155        1344 :     gel(grp,4) = ZM_copy(M);
    2156        1344 :     gel(grp,5) = icopy(den);
    2157        1344 :     gel(grp,6) = group_elts(G,n);
    2158        1344 :     gel(grp,7) = gcopy(gel(G,1));
    2159        1344 :     gel(grp,8) = gcopy(gel(G,2)); return gerepileupto(ltop, grp);
    2160             :   }
    2161         441 :   aut = galoisgrouptopol(group_elts(G, n),L,M,den,gb.ladicsol, varn(T));
    2162         441 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "Computation of polynomials");
    2163         441 :   return gerepileupto(ltop, gen_sort(aut, (void*)&gcmp, &gen_cmp_RgX));
    2164             : }
    2165             : 
    2166             : /* Heuristic computation of #Aut(T), pinit = first prime to be tested */
    2167             : long
    2168         637 : numberofconjugates(GEN T, long pinit)
    2169             : {
    2170         637 :   pari_sp av = avma;
    2171         637 :   long c, nbtest, nbmax, n = degpol(T);
    2172             :   ulong p;
    2173             :   forprime_t S;
    2174             : 
    2175         637 :   if (n == 1) return 1;
    2176         637 :   nbmax = (n < 10)? 20: (n<<1) + 1;
    2177         637 :   nbtest = 0;
    2178             : #if 0
    2179             :   c = ZX_sturm(T); c = ugcd(c, n-c); /* too costly: finite primes are cheaper */
    2180             : #else
    2181         637 :   c = n;
    2182             : #endif
    2183         637 :   u_forprime_init(&S, pinit, ULONG_MAX);
    2184         637 :   while((p = u_forprime_next(&S)))
    2185             :   {
    2186        5326 :     GEN L, Tp = ZX_to_Flx(T,p);
    2187             :     long i, nb;
    2188        5326 :     if (!Flx_is_squarefree(Tp, p)) continue;
    2189             :     /* unramified */
    2190        4300 :     nbtest++;
    2191        4300 :     L = Flx_nbfact_by_degree(Tp, &nb, p); /* L[i] = #factors of degree i */
    2192        4300 :     if (L[n/nb] == nb) {
    2193        3538 :       if (c == n && nbtest > 10) break; /* probably Galois */
    2194             :     }
    2195             :     else
    2196             :     {
    2197        1399 :       c = ugcd(c, L[1]);
    2198        9148 :       for (i = 2; i <= n; i++)
    2199        8217 :         if (L[i]) { c = ugcd(c, L[i]*i); if (c == 1) break; }
    2200        1399 :       if (c == 1) break;
    2201             :     }
    2202        3817 :     if (nbtest == nbmax) break;
    2203        3663 :     if (DEBUGLEVEL >= 6)
    2204           0 :       err_printf("NumberOfConjugates [%ld]:c=%ld,p=%ld\n", nbtest,c,p);
    2205        3663 :     avma = av;
    2206             :   }
    2207         637 :   if (DEBUGLEVEL >= 2) err_printf("NumberOfConjugates:c=%ld,p=%ld\n", c, p);
    2208         637 :   avma = av; return c;
    2209             : }
    2210             : static GEN
    2211           0 : galoisconj4(GEN nf, GEN d)
    2212             : {
    2213           0 :   pari_sp av = avma;
    2214             :   GEN G, T;
    2215           0 :   G = galoisconj4_main(nf, d, 0);
    2216           0 :   if (typ(G) != t_INT) return G; /* Success */
    2217           0 :   avma = av; T = get_nfpol(nf, &nf);
    2218           0 :   G = cgetg(2, t_COL); gel(G,1) = pol_x(varn(T)); return G; /* Fail */
    2219             : 
    2220             : }
    2221             : 
    2222             : /* d multiplicative bound for the automorphism's denominators */
    2223             : GEN
    2224       11172 : galoisconj(GEN nf, GEN d)
    2225             : {
    2226       11172 :   pari_sp av = avma;
    2227       11172 :   GEN G, NF, T = get_nfpol(nf,&NF);
    2228       11172 :   if (degpol(T) == 2)
    2229             :   { /* fast shortcut */
    2230       10038 :     GEN a = gel(T,4), b = gel(T,3);
    2231       10038 :     long v = varn(T);
    2232       10038 :     RgX_check_ZX(T, "galoisconj");
    2233       10038 :     if (!gequal1(a)) pari_err_IMPL("galoisconj(non-monic)");
    2234       10038 :     b = negi(b);
    2235       10038 :     G = cgetg(3, t_COL);
    2236       10038 :     gel(G,1) = pol_x(v);
    2237       10038 :     gel(G,2) = deg1pol(gen_m1, b, v); return G;
    2238             :   }
    2239        1134 :   G = galoisconj4_main(nf, d, 0);
    2240        1134 :   if (typ(G) != t_INT) return G; /* Success */
    2241         630 :   avma = av; return galoisconj1(nf);
    2242             : }
    2243             : 
    2244             : /* FIXME: obsolete, use galoisconj(nf, d) directly */
    2245             : GEN
    2246          42 : galoisconj0(GEN nf, long flag, GEN d, long prec)
    2247             : {
    2248             :   (void)prec;
    2249          42 :   switch(flag) {
    2250             :     case 2:
    2251          35 :     case 0: return galoisconj(nf, d);
    2252           7 :     case 1: return galoisconj1(nf);
    2253           0 :     case 4: return galoisconj4(nf, d);
    2254             :   }
    2255           0 :   pari_err_FLAG("nfgaloisconj");
    2256             :   return NULL; /*LCOV_EXCL_LINE*/
    2257             : }
    2258             : 
    2259             : /******************************************************************************/
    2260             : /* Galois theory related algorithms                                           */
    2261             : /******************************************************************************/
    2262             : GEN
    2263       19950 : checkgal(GEN gal)
    2264             : {
    2265       19950 :   if (typ(gal) == t_POL) pari_err_TYPE("checkgal [apply galoisinit first]",gal);
    2266       19950 :   if (typ(gal) != t_VEC || lg(gal) != 9) pari_err_TYPE("checkgal",gal);
    2267       19943 :   return gal;
    2268             : }
    2269             : 
    2270             : GEN
    2271        2324 : galoisinit(GEN nf, GEN den)
    2272             : {
    2273        2324 :   GEN G = galoisconj4_main(nf, den, 1);
    2274        2310 :   return (typ(G) == t_INT)? gen_0: G;
    2275             : }
    2276             : 
    2277             : static GEN
    2278       15085 : galoispermtopol_i(GEN gal, GEN perm, GEN mod, GEN mod2)
    2279             : {
    2280       15085 :   switch (typ(perm))
    2281             :   {
    2282             :     case t_VECSMALL:
    2283       14847 :       return permtopol(perm, gal_get_roots(gal), gal_get_invvdm(gal),
    2284             :                              gal_get_den(gal), mod, mod2,
    2285       14847 :                              varn(gal_get_pol(gal)));
    2286             :     case t_VEC: case t_COL: case t_MAT:
    2287             :     {
    2288             :       long i, lv;
    2289         238 :       GEN v = cgetg_copy(perm, &lv);
    2290         238 :       if (DEBUGLEVEL>=4) err_printf("GaloisPermToPol:");
    2291         721 :       for (i = 1; i < lv; i++)
    2292             :       {
    2293         483 :         gel(v,i) = galoispermtopol_i(gal, gel(perm,i), mod, mod2);
    2294         483 :         if (DEBUGLEVEL>=4) err_printf("%ld ",i);
    2295             :       }
    2296         238 :       if (DEBUGLEVEL>=4) err_printf("\n");
    2297         238 :       return v;
    2298             :     }
    2299             :   }
    2300           0 :   pari_err_TYPE("galoispermtopol", perm);
    2301             :   return NULL; /* LCOV_EXCL_LINE */
    2302             : }
    2303             : 
    2304             : GEN
    2305       14602 : galoispermtopol(GEN gal, GEN perm)
    2306             : {
    2307       14602 :   pari_sp av = avma;
    2308             :   GEN mod, mod2;
    2309       14602 :   gal = checkgal(gal);
    2310       14602 :   mod = gal_get_mod(gal);
    2311       14602 :   mod2 = shifti(mod,-1);
    2312       14602 :   return gerepilecopy(av, galoispermtopol_i(gal, perm, mod, mod2));
    2313             : }
    2314             : 
    2315             : GEN
    2316          63 : galoiscosets(GEN O, GEN perm)
    2317             : {
    2318          63 :   long i, j, k, u, f, l = lg(O);
    2319          63 :   GEN RC, C = cgetg(l,t_VECSMALL), o = gel(O,1);
    2320          63 :   pari_sp av = avma;
    2321          63 :   f = lg(o); u = o[1]; RC = zero_zv(lg(perm)-1);
    2322         280 :   for(i=1,j=1; j<l; i++)
    2323             :   {
    2324         217 :     GEN p = gel(perm,i);
    2325         217 :     if (RC[ p[u] ]) continue;
    2326         133 :     for(k=1; k<f; k++) RC[ p[ o[k] ] ] = 1;
    2327         133 :     C[j++] = i;
    2328             :   }
    2329          63 :   avma = av; return C;
    2330             : }
    2331             : 
    2332             : static GEN
    2333          63 : fixedfieldfactor(GEN L, GEN O, GEN perm, GEN M, GEN den, GEN mod, GEN mod2,
    2334             :                  long x,long y)
    2335             : {
    2336          63 :   pari_sp ltop = avma;
    2337          63 :   long i, j, k, l = lg(O), lo = lg(gel(O,1));
    2338          63 :   GEN V, res, cosets = galoiscosets(O,perm), F = cgetg(lo+1,t_COL);
    2339             : 
    2340          63 :   gel(F, lo) = gen_1;
    2341          63 :   if (DEBUGLEVEL>=4) err_printf("GaloisFixedField:cosets=%Ps \n",cosets);
    2342          63 :   if (DEBUGLEVEL>=6) err_printf("GaloisFixedField:den=%Ps mod=%Ps \n",den,mod);
    2343          63 :   V = cgetg(l,t_COL); res = cgetg(l,t_VEC);
    2344         196 :   for (i = 1; i < l; i++)
    2345             :   {
    2346         133 :     pari_sp av = avma;
    2347         133 :     GEN G = cgetg(l,t_VEC), Lp = vecpermute(L, gel(perm, cosets[i]));
    2348         448 :     for (k = 1; k < l; k++)
    2349         315 :       gel(G,k) = FpV_roots_to_pol(vecpermute(Lp, gel(O,k)), mod, x);
    2350         483 :     for (j = 1; j < lo; j++)
    2351             :     {
    2352         350 :       for(k = 1; k < l; k++) gel(V,k) = gmael(G,k,j+1);
    2353         350 :       gel(F,j) = vectopol(V, M, den, mod, mod2, y);
    2354             :     }
    2355         133 :     gel(res,i) = gerepileupto(av,gtopolyrev(F,x));
    2356             :   }
    2357          63 :   return gerepileupto(ltop,res);
    2358             : }
    2359             : 
    2360             : static void
    2361        1491 : chk_perm(GEN perm, long n)
    2362             : {
    2363        1491 :   if (typ(perm) != t_VECSMALL || lg(perm)!=n+1)
    2364           0 :     pari_err_TYPE("galoisfixedfield", perm);
    2365        1491 : }
    2366             : 
    2367             : static int
    2368        5971 : is_group(GEN g)
    2369             : {
    2370       17906 :   return typ(g)==t_VEC && lg(g)==3 && typ(gel(g,1))==t_VEC
    2371        6706 :       && typ(gel(g,2))==t_VECSMALL;
    2372             : }
    2373             : 
    2374             : GEN
    2375        1022 : galoisfixedfield(GEN gal, GEN perm, long flag, long y)
    2376             : {
    2377        1022 :   pari_sp ltop = avma;
    2378             :   GEN T, L, P, S, PL, O, res, mod, mod2;
    2379             :   long vT, n, i;
    2380        1022 :   if (flag<0 || flag>2) pari_err_FLAG("galoisfixedfield");
    2381        1022 :   gal = checkgal(gal); T = gal_get_pol(gal);
    2382        1022 :   vT = varn(T);
    2383        1022 :   L = gal_get_roots(gal); n = lg(L)-1;
    2384        1022 :   mod = gal_get_mod(gal);
    2385        1022 :   if (typ(perm) == t_VEC)
    2386             :   {
    2387         770 :     if (is_group(perm)) perm = gel(perm, 1);
    2388         770 :     for (i = 1; i < lg(perm); i++) chk_perm(gel(perm,i), n);
    2389         770 :     O = vecperm_orbits(perm, n);
    2390             :   }
    2391             :   else
    2392             :   {
    2393         252 :     chk_perm(perm, n);
    2394         252 :     O = perm_cycles(perm);
    2395             :   }
    2396             : 
    2397             :   {
    2398        1022 :     GEN OL= fixedfieldorbits(O,L);
    2399        1022 :     GEN V = fixedfieldsympol(OL, mod, gal_get_p(gal), NULL, vT);
    2400        1022 :     PL= gel(V,2);
    2401        1022 :     P = gel(V,3);
    2402             :   }
    2403        1022 :   if (flag==1) return gerepileupto(ltop,P);
    2404         784 :   mod2 = shifti(mod,-1);
    2405         784 :   S = fixedfieldinclusion(O, PL);
    2406         784 :   S = vectopol(S, gal_get_invvdm(gal), gal_get_den(gal), mod, mod2, vT);
    2407         784 :   if (flag==0)
    2408         721 :     res = cgetg(3, t_VEC);
    2409             :   else
    2410             :   {
    2411             :     GEN PM, Pden;
    2412             :     struct galois_borne Pgb;
    2413          63 :     long val = itos(gal_get_e(gal));
    2414          63 :     Pgb.l = gal_get_p(gal);
    2415          63 :     Pden = galoisborne(P, NULL, &Pgb, degpol(T)/degpol(P));
    2416          63 :     if (Pgb.valabs > val)
    2417             :     {
    2418          12 :       if (DEBUGLEVEL>=4)
    2419           0 :         err_printf("GaloisConj: increase p-adic prec by %ld.\n", Pgb.valabs-val);
    2420          12 :       PL = ZpX_liftroots(P, PL, Pgb.l, Pgb.valabs);
    2421          12 :       L  = ZpX_liftroots(T, L, Pgb.l, Pgb.valabs);
    2422          12 :       mod = Pgb.ladicabs; mod2 = shifti(mod,-1);
    2423             :     }
    2424          63 :     PM = FpV_invVandermonde(PL, Pden, mod);
    2425          63 :     if (y < 0) y = 1;
    2426          63 :     if (varncmp(y, vT) <= 0)
    2427           0 :       pari_err_PRIORITY("galoisfixedfield", T, "<=", y);
    2428          63 :     res = cgetg(4, t_VEC);
    2429          63 :     gel(res,3) = fixedfieldfactor(L,O,gal_get_group(gal), PM,Pden,mod,mod2,vT,y);
    2430             :   }
    2431         784 :   gel(res,1) = gcopy(P);
    2432         784 :   gel(res,2) = gmodulo(S, T);
    2433         784 :   return gerepileupto(ltop, res);
    2434             : }
    2435             : 
    2436             : /* gal a galois group output the underlying wss group */
    2437             : GEN
    2438        1225 : galois_group(GEN gal) { return mkvec2(gal_get_gen(gal), gal_get_orders(gal)); }
    2439             : 
    2440             : GEN
    2441         763 : checkgroup(GEN g, GEN *S)
    2442             : {
    2443         763 :   if (is_group(g)) { *S = NULL; return g; }
    2444         406 :   g  = checkgal(g);
    2445         399 :   *S = gal_get_group(g); return galois_group(g);
    2446             : }
    2447             : 
    2448             : GEN
    2449        4452 : checkgroupelts(GEN G)
    2450             : {
    2451             :   long i, n;
    2452        4452 :   if (typ(G)!=t_VEC) pari_err_TYPE("checkgroupelts", G);
    2453        4438 :   if (is_group(G))
    2454             :   { /* subgroup of S_n */
    2455         357 :     if (lg(gel(G,1))==1) return mkvec(mkvecsmall(1));
    2456         357 :     return group_elts(G, group_domain(G));
    2457             :   }
    2458        4081 :   if (lg(G)==9 && typ(gel(G,1))==t_POL)
    2459        3766 :     return gal_get_group(G); /* galoisinit */
    2460             :   /* vector of permutations ? */
    2461         315 :   n = lg(G)-1;
    2462         315 :   if (n==0) pari_err_DIM("checkgroupelts");
    2463        4984 :   for (i = 1; i <= n; i++)
    2464             :   {
    2465        4704 :     if (typ(gel(G,i)) != t_VECSMALL)
    2466          21 :       pari_err_TYPE("checkgroupelts (element)", gel(G,i));
    2467        4683 :     if (lg(gel(G,i)) != lg(gel(G,1)))
    2468          14 :       pari_err_DIM("checkgroupelts [length of permutations]");
    2469             :   }
    2470         280 :   return G;
    2471             : }
    2472             : 
    2473             : GEN
    2474         168 : galoisisabelian(GEN gal, long flag)
    2475             : {
    2476         168 :   pari_sp av = avma;
    2477         168 :   GEN S, G = checkgroup(gal,&S);
    2478         168 :   if (!group_isabelian(G)) { avma=av; return gen_0; }
    2479         147 :   switch(flag)
    2480             :   {
    2481          49 :     case 0: return gerepileupto(av, group_abelianHNF(G,S));
    2482          49 :     case 1: avma=av; return gen_1;
    2483          49 :     case 2: return gerepileupto(av, group_abelianSNF(G,S));
    2484           0 :     default: pari_err_FLAG("galoisisabelian");
    2485             :   }
    2486             :   return NULL; /* LCOV_EXCL_LINE */
    2487             : }
    2488             : 
    2489             : long
    2490          56 : galoisisnormal(GEN gal, GEN sub)
    2491             : {
    2492          56 :   pari_sp av = avma;
    2493          56 :   GEN S, G = checkgroup(gal, &S), H = checkgroup(sub, &S);
    2494          56 :   long res = group_subgroup_isnormal(G, H);
    2495          56 :   avma = av;
    2496          56 :   return res;
    2497             : }
    2498             : 
    2499             : static GEN
    2500         301 : conjclasses_count(GEN conj, long nb)
    2501             : {
    2502         301 :   long i, l = lg(conj);
    2503         301 :   GEN c = zero_zv(nb);
    2504         301 :   for (i = 1; i < l; i++) c[conj[i]]++;
    2505         301 :   return c;
    2506             : }
    2507             : GEN
    2508         301 : galoisconjclasses(GEN G)
    2509             : {
    2510         301 :   pari_sp av = avma;
    2511         301 :   GEN c, e, cc = group_to_cc(G);
    2512         301 :   GEN elts = gel(cc,1), conj = gel(cc,2), repr = gel(cc,3);
    2513         301 :   long i, l = lg(conj), lc = lg(repr);
    2514         301 :   c = conjclasses_count(conj, lc-1);
    2515         301 :   e = cgetg(lc, t_VEC);
    2516         301 :   for (i = 1; i < lc; i++) gel(e,i) = cgetg(c[i]+1, t_VEC);
    2517        3738 :   for (i = 1; i < l; i++)
    2518             :   {
    2519        3437 :     long ci = conj[i];
    2520        3437 :     gmael(e, ci, c[ci]) = gel(elts, i);
    2521        3437 :     c[ci]--;
    2522             :   }
    2523         301 :   return gerepilecopy(av, e);
    2524             : }
    2525             : 
    2526             : GEN
    2527          63 : galoissubgroups(GEN gal)
    2528             : {
    2529          63 :   pari_sp av = avma;
    2530          63 :   GEN S, G = checkgroup(gal,&S);
    2531          63 :   return gerepileupto(av, group_subgroups(G));
    2532             : }
    2533             : 
    2534             : GEN
    2535          49 : galoissubfields(GEN G, long flag, long v)
    2536             : {
    2537          49 :   pari_sp av = avma;
    2538          49 :   GEN L = galoissubgroups(G);
    2539          49 :   long i, l = lg(L);
    2540          49 :   GEN S = cgetg(l, t_VEC);
    2541          49 :   for (i = 1; i < l; ++i) gel(S,i) = galoisfixedfield(G, gmael(L,i,1), flag, v);
    2542          49 :   return gerepileupto(av, S);
    2543             : }
    2544             : 
    2545             : GEN
    2546          28 : galoisexport(GEN gal, long format)
    2547             : {
    2548          28 :   pari_sp av = avma;
    2549          28 :   GEN S, G = checkgroup(gal,&S);
    2550          28 :   return gerepileupto(av, group_export(G,format));
    2551             : }
    2552             : 
    2553             : GEN
    2554         392 : galoisidentify(GEN gal)
    2555             : {
    2556         392 :   pari_sp av = avma;
    2557         392 :   GEN S, G = checkgroup(gal,&S);
    2558         385 :   long idx = group_ident(G,S), card = group_order(G);
    2559         385 :   avma = av; return mkvec2s(card, idx);
    2560             : }
    2561             : 
    2562             : static GEN
    2563        1057 : Qevproj_RgX(GEN c, long d, GEN pro)
    2564        1057 : { return RgV_to_RgX(Qevproj_down(RgX_to_RgC(c,d), pro), varn(c)); }
    2565             : 
    2566             : /* index of conjugacy class containing g */
    2567             : static long
    2568       34783 : cc_id(GEN cc, GEN g)
    2569             : {
    2570       34783 :   GEN conj = gel(cc,2);
    2571       34783 :   long k = signe(gel(cc,4))? g[1]: vecvecsmall_search(gel(cc,1),g,0);
    2572       34783 :   return conj[k];
    2573             : }
    2574             : 
    2575             : static GEN
    2576         511 : cc_chartable(GEN cc)
    2577             : {
    2578             :   GEN al, elts, rep, ctp, ct, dec, id, vjg, H, vord, operm;
    2579             :   long i, j, k, f, l, expo, lcl, n;
    2580             :   ulong p, pov2;
    2581             : 
    2582         511 :   elts = gel(cc,1); n = lg(elts)-1;
    2583         511 :   if (n == 1) return mkvec2(mkmat(mkcol(gen_1)), gen_1);
    2584         497 :   rep = gel(cc,3);
    2585         497 :   lcl = lg(rep);
    2586         497 :   vjg = cgetg(lcl, t_VEC);
    2587         497 :   vord = cgetg(lcl,t_VECSMALL);
    2588         497 :   id = identity_perm(lg(gel(elts,1))-1);
    2589         497 :   expo = 1;
    2590        4620 :   for(j=1;j<lcl;j++)
    2591             :   {
    2592        4123 :     GEN jg, h = id, g = gel(elts,rep[j]);
    2593             :     long o;
    2594        4123 :     vord[j] = o = perm_order(g);
    2595        4123 :     expo = clcm(expo, o);
    2596        4123 :     gel(vjg,j) = jg = cgetg(o+1,t_VECSMALL);
    2597       25494 :     for (l=1; l<=o; l++)
    2598             :     {
    2599       21371 :       jg[l] = cc_id(cc, h); /* index of conjugacy class of g^(l-1) */
    2600       21371 :       if (l < o) h = perm_mul(h, g);
    2601             :     }
    2602             :   }
    2603             :   /* would sort conjugacy classes by inc. order */
    2604         497 :   operm = vecsmall_indexsort(vord);
    2605             : 
    2606             :   /* expo > 1, exponent of G */
    2607         497 :   p = unextprime(2*n+1);
    2608         497 :   while (p%expo != 1) p = unextprime(p+1);
    2609             :   /* compute character table modulo p: idempotents of Z(KG) */
    2610         497 :   al = conjclasses_algcenter(cc, utoipos(p));
    2611         497 :   dec = algsimpledec(al,1);
    2612         497 :   ctp = cgetg(lcl,t_VEC);
    2613        4620 :   for(i=1; i<lcl; i++)
    2614             :   {
    2615        4123 :     GEN e = ZV_to_Flv(gmael3(dec,i,3,1), p); /*(1/n)[(dim chi)chi(g): g in G]*/
    2616        4123 :     ulong d = usqrt(Fl_mul(e[1], n, p)); /* = chi(1) <= sqrt(n) < sqrt(p) */
    2617        4123 :     gel(ctp,i) = Flv_Fl_mul(e,Fl_div(n,d,p), p); /*[chi(g): g in G]*/
    2618             :   }
    2619             :   /* Find minimal f such that table is defined over Q(zeta(f)): the conductor
    2620             :    * of the class field Q(\zeta_e)^H defined by subgroup
    2621             :    * H = { k in (Z/e)^*: g^k ~ g, for all g } */
    2622         497 :   H = coprimes_zv(expo);
    2623        3059 :   for (k = 2; k < expo; k++)
    2624             :   {
    2625        2562 :     if (!H[k]) continue;
    2626        2268 :     for (j = 2; j < lcl; j++) /* skip g ~ 1 */
    2627        2114 :       if (umael(vjg,j,(k % vord[j])+1) != umael(vjg,j,2)) { H[k] = 0; break; }
    2628             :   }
    2629         497 :   f = znstar_conductor_bits(Flv_to_F2v(H));
    2630             :   /* lift character table to Z[zeta_f] */
    2631         497 :   pov2 = p>>1;
    2632         497 :   ct = cgetg(lcl, t_MAT);
    2633         497 :   for (j=1; j<lcl; j++) gel(ct,j) = cgetg(lcl,t_COL);
    2634         497 :   if (f == 1)
    2635             :   { /* rational representation */
    2636         833 :     for(j=1; j<lcl; j++)
    2637             :     {
    2638         714 :       GEN jg = gel(vjg,j); /* jg[l+1] = class of g^l */
    2639         714 :       long t = lg(jg) > 2? jg[2]: jg[1];
    2640        6412 :       for(i=1; i<lcl; i++)
    2641             :       {
    2642        5698 :         GEN cp = gel(ctp,i); /* cp[i] = chi(g_i) mod \P */
    2643        5698 :         gcoeff(ct,j,i) = stoi(Fl_center(cp[t], p, pov2));
    2644             :       }
    2645             :     }
    2646             :   }
    2647             :   else
    2648             :   {
    2649         378 :     hashtable *H = hash_create_ulong(p, 1);
    2650         378 :     const long var = 1;
    2651         378 :     ulong ze = Fl_powu(pgener_Fl(p),(p-1)/expo, p); /* seen as zeta_e^(-1) */
    2652         378 :     GEN vze = Fl_powers(ze, expo-1, p); /* vze[i] = ze^(i-1)/f */
    2653         378 :     GEN T = polcyclo(expo, var), pro = NULL;
    2654         378 :     long phie = degpol(T);
    2655         378 :     if (f != expo)
    2656             :     {
    2657         140 :       long phif = eulerphiu(f);
    2658         140 :       GEN zf = ZX_rem(pol_xn(expo/f,var), T), zfj = zf;
    2659         140 :       GEN M = cgetg(phif+1, t_MAT);
    2660         140 :       gel(M,1) = col_ei(phie,1);
    2661         476 :       for (j = 2; j <= phif; j++)
    2662             :       {
    2663         336 :         gel(M,j) = RgX_to_RgC(zfj, phie);
    2664         336 :         if (j < phif) zfj = ZX_rem(ZX_mul(zfj, zf), T);
    2665             :       }
    2666         140 :       pro = Qevproj_init(M);
    2667             :     }
    2668        3787 :     for (j=lcl-1; j; j--)
    2669             :     { /* loop over conjugacy classes, decreasing order */
    2670        3409 :       long jperm = operm[j];
    2671        3409 :       GEN jg = gel(vjg,jperm); /* jg[l+1] = class of g^l */
    2672        3409 :       long o = vord[jperm], oinv = Fl_inv(o,p), e = expo/o;
    2673        3409 :       GEN To = e == 1? T: polcyclo(o, var);
    2674       41300 :       for(i=1; i<lcl; i++)
    2675             :       { /* loop over characters */
    2676       37891 :         GEN cp = gel(ctp,i), cj, cj0; /* cp[i] = chi(g_i) mod \P */
    2677       37891 :         void *vj = (void*)uel(cp, jg[o == 1? 1: 2]); /* chi(g) */
    2678       37891 :         ulong h = H->hash(vj);
    2679       37891 :         hashentry *E = hash_search2(H, vj, h);
    2680       37891 :         if (E) { gcoeff(ct,jperm,i) = (GEN)E->val; continue; }
    2681             : 
    2682             :        /* chi(g^l) = sum_{k=0}^{o-1} a_k zeta_o^{l*k} for all l;
    2683             :         * => a_k = 1/o sum_{l=0}^{o-1} chi(g^l) zeta_o^{-k*l} */
    2684        1022 :         cj = cgetg(o+2, t_POL);
    2685        1022 :         cj[1] = evalvarn(var) | evalsigne(1);
    2686        7182 :         for (k=0; k<o; k++)
    2687             :         {
    2688        6160 :           ulong a = 0;
    2689       58492 :           for (l=0; l<o; l++)
    2690             :           {
    2691       52332 :             ulong z = vze[Fl_mul(k,l,o)*e + 1];/* zeta_o^{-k*l} */
    2692       52332 :             a = Fl_add(a, Fl_mul(uel(cp,jg[l+1]), z, p), p);
    2693             :           }
    2694        6160 :           gel(cj,k+2) = stoi(Fl_center(Fl_mul(a,oinv,p), p, pov2)); /* a_k */
    2695             :         }
    2696        1022 :         cj0 = cj = ZX_renormalize(cj,o+2);
    2697        1022 :         cj = ZX_rem(cj, To);
    2698        1022 :         if (e != 1) cj = ZX_rem(RgX_inflate(cj,e), T);
    2699        1022 :         if (pro) cj = Qevproj_RgX(cj, phie, pro);
    2700        1022 :         gcoeff(ct,jperm,i) = cj;
    2701        1022 :         hash_insert2(H, vj, cj, h);
    2702        5208 :         for (k = 2; k < o; k++)
    2703             :         {
    2704        4186 :           void *vk = (void*)uel(cp, jg[k+1]); /* chi(g^k) */
    2705        4186 :           ulong hk = H->hash(vk);
    2706             :           GEN ck;
    2707        4186 :           if (hash_search2(H, vk, hk)) continue;
    2708        1869 :           ck = ZX_rem(RgX_inflate(cj0, k), To);
    2709        1869 :           if (e != 1) ck = ZX_rem(RgX_inflate(ck,e), T);
    2710        1869 :           if (pro) ck = Qevproj_RgX(ck, phie, pro);
    2711        1869 :           hash_insert2(H, vk, ck, hk);
    2712             :         }
    2713             :       }
    2714             :     }
    2715             :   }
    2716         497 :   ct = gen_sort(ct,(void*)cmp_universal,cmp_nodata);
    2717         497 :   i = 1; while (!vec_isconst(gel(ct,i))) i++;
    2718         497 :   if (i > 1) swap(gel(ct,1), gel(ct,i));
    2719         497 :   return mkvec2(ct, utoipos(f));
    2720             : }
    2721             : GEN
    2722         511 : galoischartable(GEN gal)
    2723             : {
    2724         511 :   pari_sp av = avma;
    2725         511 :   GEN cc = group_to_cc(gal);
    2726         511 :   return gerepilecopy(av, cc_chartable(cc));
    2727             : }
    2728             : 
    2729             : static void
    2730        1442 : checkgaloischar(GEN ch, GEN repr)
    2731             : {
    2732        1442 :   if (gvar(ch) == 0) pari_err_PRIORITY("galoischarpoly",ch,"=",0);
    2733        1442 :   if (!is_vec_t(typ(ch))) pari_err_TYPE("galoischarpoly", ch);
    2734        1442 :   if (lg(repr) != lg(ch)) pari_err_DIM("galoischarpoly");
    2735        1442 : }
    2736             : 
    2737             : static long
    2738        1498 : galoischar_dim(GEN ch)
    2739             : {
    2740        1498 :   pari_sp av = avma;
    2741        1498 :   long d = gtos(simplify_shallow(lift_shallow(gel(ch,1))));
    2742        1498 :   avma = av; return d;
    2743             : }
    2744             : 
    2745             : static GEN
    2746       12215 : galoischar_aut_charpoly(GEN cc, GEN ch, GEN p, long d)
    2747             : {
    2748       12215 :   GEN q = p, V = cgetg(d+3, t_POL);
    2749             :   long i;
    2750       12215 :   V[1] = evalsigne(1)|evalvarn(0);
    2751       12215 :   gel(V,2) = gen_0;
    2752       25627 :   for (i = 1; i <= d; i++)
    2753             :   {
    2754       13412 :     gel(V,i+2) = gdivgs(gel(ch, cc_id(cc,q)), -i);
    2755       13412 :     if (i < d) q = perm_mul(q, p);
    2756             :   }
    2757       12215 :   return liftpol_shallow(RgXn_exp(V,d+1));
    2758             : }
    2759             : 
    2760             : static GEN
    2761        1442 : galoischar_charpoly(GEN cc, GEN ch, long o)
    2762             : {
    2763        1442 :   GEN chm, V, elts = gel(cc,1), repr = gel(cc,3);
    2764        1442 :   long i, d, l = lg(ch);
    2765        1442 :   checkgaloischar(ch, repr);
    2766        1442 :   chm = gmodulo(ch, polcyclo(o, gvar(ch)));
    2767        1442 :   V = cgetg(l, t_COL); d = galoischar_dim(ch);
    2768       13657 :   for (i = 1; i < l; i++)
    2769       12215 :     gel(V,i) = galoischar_aut_charpoly(cc, chm, gel(elts,repr[i]), d);
    2770        1442 :   return V;
    2771             : }
    2772             : 
    2773             : GEN
    2774        1386 : galoischarpoly(GEN gal, GEN ch, long o)
    2775             : {
    2776        1386 :   pari_sp av = avma;
    2777        1386 :   GEN cc = group_to_cc(gal);
    2778        1386 :   return gerepilecopy(av, galoischar_charpoly(cc, ch, o));
    2779             : }
    2780             : 
    2781             : static GEN
    2782          56 : cc_char_det(GEN cc, GEN ch, long o)
    2783             : {
    2784          56 :   long i, l = lg(ch), d = galoischar_dim(ch);
    2785          56 :   GEN V = galoischar_charpoly(cc, ch, o);
    2786          56 :   for (i = 1; i < l; i++) gel(V,i) = leading_coeff(gel(V,i));
    2787          56 :   return odd(d)? gneg(V): V;
    2788             : }
    2789             : 
    2790             : GEN
    2791          56 : galoischardet(GEN gal, GEN ch, long o)
    2792             : {
    2793          56 :   pari_sp av = avma;
    2794          56 :   GEN cc = group_to_cc(gal);
    2795          56 :   return gerepilecopy(av, cc_char_det(cc, ch, o));
    2796             : }

Generated by: LCOV version 1.11