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 19821-98a93fe) Lines: 1418 1498 94.7 %
Date: 2016-12-02 05:49:16 Functions: 85 87 97.7 %
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        3047 : vandermondeinverseprep(GEN L)
     134             : {
     135        3047 :   long i, j, n = lg(L);
     136             :   GEN V;
     137        3047 :   V = cgetg(n, t_VEC);
     138       31099 :   for (i = 1; i < n; i++)
     139             :   {
     140       28052 :     pari_sp ltop = avma;
     141       28052 :     GEN W = cgetg(n-1,t_VEC);
     142       28052 :     long k = 1;
     143      742438 :     for (j = 1; j < n; j++)
     144      714386 :       if (i != j) gel(W, k++) = gsub(gel(L,i),gel(L,j));
     145       28052 :     gel(V,i) = gerepileupto(ltop, RgV_prod(W));
     146             :   }
     147        3047 :   return V;
     148             : }
     149             : 
     150             : /* Compute the inverse of the van der Monde matrix of T multiplied by den */
     151             : GEN
     152        2956 : vandermondeinverse(GEN L, GEN T, GEN den, GEN prep)
     153             : {
     154        2956 :   pari_sp ltop = avma;
     155        2956 :   long i, n = lg(L)-1;
     156             :   GEN M, P;
     157        2956 :   if (!prep) prep = vandermondeinverseprep(L);
     158        2956 :   if (den && !equali1(den)) T = RgX_Rg_mul(T,den);
     159        2956 :   M = cgetg(n+1, t_MAT);
     160       29769 :   for (i = 1; i <= n; i++)
     161             :   {
     162       26813 :     P = RgX_Rg_div(RgX_div_by_X_x(T, gel(L,i), NULL), gel(prep,i));
     163       26813 :     gel(M,i) = RgX_to_RgC(P,n);
     164             :   }
     165        2956 :   return gerepilecopy(ltop, M);
     166             : }
     167             : 
     168             : /* #r = r1 + r2 */
     169             : GEN
     170        1224 : embed_roots(GEN ro, long r1)
     171             : {
     172        1224 :   long r2 = lg(ro)-1-r1;
     173             :   GEN L;
     174        1224 :   if (!r2) L = ro;
     175             :   else
     176             :   {
     177        1049 :     long i,j, N = r1+2*r2;
     178        1049 :     L = cgetg(N+1, t_VEC);
     179        1049 :     for (i = 1; i <= r1; i++) gel(L,i) = gel(ro,i);
     180        4228 :     for (j = i; j <= N; i++)
     181             :     {
     182        3179 :       GEN z = gel(ro,i);
     183        3179 :       gel(L,j++) = z;
     184        3179 :       gel(L,j++) = mkcomplex(gel(z,1), gneg(gel(z,2)));
     185             :     }
     186             :   }
     187        1224 :   return L;
     188             : }
     189             : GEN
     190       19397 : embed_disc(GEN z, long r1, long prec)
     191             : {
     192       19397 :   pari_sp av = avma;
     193       19397 :   GEN t = real_1(prec);
     194       19397 :   long i, j, n = lg(z)-1, r2 = n-r1;
     195       92827 :   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       91154 :   for (j = r1+1; j <= n; j++)
     201             :   {
     202       71757 :     GEN zj = gel(z,j), a = gel(zj,1), b = gel(zj,2), b2 = gsqr(b);
     203       78673 :     for (i = 1; i <= r1; i++)
     204             :     {
     205        6916 :       GEN zi = gel(z,i);
     206        6916 :       t = gmul(t, gadd(gsqr(gsub(zi, a)), b2));
     207             :     }
     208       71757 :     t = gmul(t, b);
     209             :   }
     210       19397 :   if (r2) t = gmul2n(t, r2);
     211       19397 :   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       19397 :   t = gsqr(t);
     227       19397 :   if (odd(r2)) t = gneg(t);
     228       19397 :   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        3047 : 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        3047 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     240        3047 :   T = get_nfpol(T, &nf);
     241        3047 :   r = nf ? nf_get_roots(nf) : NULL;
     242        3047 :   if (nf &&  precision(gel(r, 1)) >= prec)
     243        1224 :     L = embed_roots(r, nf_get_r1(nf));
     244             :   else
     245        1823 :     L = QX_complex_roots(T, prec);
     246        3047 :   if (DEBUGLEVEL>=4) timer_printf(&ti,"roots");
     247        3047 :   prep = vandermondeinverseprep(L);
     248        3047 :   if (!dn)
     249             :   {
     250        1849 :     GEN dis, res = RgV_prod(gabs(prep,prec));
     251             :     /*Add +1 to cater for accuracy error in res */
     252        1849 :     dis = ZX_disc_all(T, 1+expi(ceil_safe(res)));
     253        1849 :     den = indexpartial(T,dis);
     254        1849 :     if (ptdis) *ptdis = dis;
     255             :   }
     256             :   else
     257             :   {
     258        1198 :     if (typ(dn) != t_INT || signe(dn) <= 0)
     259           0 :       pari_err_TYPE("initgaloisborne [incorrect denominator]", dn);
     260        1198 :     den = dn;
     261             :   }
     262        3047 :   if (ptprep) *ptprep = prep;
     263        3047 :   *ptL = L; return den;
     264             : }
     265             : 
     266             : /* ||| M ||| with respect to || x ||_oo, M t_MAT */
     267             : GEN
     268        7047 : matrixnorm(GEN M, long prec)
     269             : {
     270        7047 :   long i,j,m, l = lg(M);
     271        7047 :   GEN B = real_0(prec);
     272             : 
     273        7047 :   if (l == 1) return B;
     274        7047 :   m = lgcols(M);
     275       32495 :   for (i = 1; i < m; i++)
     276             :   {
     277       25448 :     GEN z = gabs(gcoeff(M,i,1), prec);
     278       25448 :     for (j = 2; j < l; j++) z = gadd(z, gabs(gcoeff(M,i,j), prec));
     279       25448 :     if (gcmp(z, B) > 0) B = z;
     280             :   }
     281        7047 :   return B;
     282             : }
     283             : 
     284             : static GEN
     285        1582 : galoisborne(GEN T, GEN dn, struct galois_borne *gb, long d)
     286             : {
     287             :   pari_sp ltop, av2;
     288             :   GEN borne, borneroots, borneabs;
     289             :   long prec;
     290             :   GEN L, M, prep, den;
     291             :   pari_timer ti;
     292             : 
     293        1582 :   prec = nbits2prec(bit_accuracy(ZX_max_lg(T)));
     294        1582 :   den = initgaloisborne(T,dn,prec, &L,&prep,NULL);
     295        1582 :   if (!dn) dn = den;
     296        1582 :   ltop = avma;
     297        1582 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     298        1582 :   M = vandermondeinverse(L, RgX_gtofp(T, prec), den, prep);
     299        1582 :   if (DEBUGLEVEL>=4) timer_printf(&ti,"vandermondeinverse");
     300        1582 :   borne = matrixnorm(M, prec);
     301        1582 :   borneroots = gsupnorm(L, prec); /*t_REAL*/
     302        1582 :   borneabs = ceil_safe(gmul(borne,gmulsg(d, powru(borneroots, d))));
     303        1582 :   borneroots = ceil_safe(gmul(borne, borneroots));
     304        1582 :   av2 = avma;
     305             :   /*We use d-1 test, so we must overlift to 2^BITS_IN_LONG*/
     306        1582 :   gb->valsol = logint(shifti(borneroots,2+BITS_IN_LONG), gb->l) + 1;
     307        1582 :   gb->valabs = logint(shifti(borneabs,2), gb->l) + 1;
     308        1582 :   gb->valabs = maxss(gb->valsol, gb->valabs);
     309        1582 :   if (DEBUGLEVEL >= 4)
     310           0 :     err_printf("GaloisConj: val1=%ld val2=%ld\n", gb->valsol, gb->valabs);
     311        1582 :   avma = av2;
     312        1582 :   gb->bornesol = gerepileuptoint(ltop, shifti(borneroots,1));
     313        1582 :   if (DEBUGLEVEL >= 9)
     314           0 :     err_printf("GaloisConj: Bound %Ps\n",borneroots);
     315        1582 :   gb->ladicsol = powiu(gb->l, gb->valsol);
     316        1582 :   gb->ladicabs = powiu(gb->l, gb->valabs);
     317        1582 :   return dn;
     318             : }
     319             : 
     320             : static GEN
     321        1491 : makeLden(GEN L,GEN den, struct galois_borne *gb)
     322        1491 : { return FpC_Fp_mul(L, den, gb->ladicsol); }
     323             : 
     324             : /* Initialize the galois_lift structure */
     325             : static void
     326        1589 : initlift(GEN T, GEN den, GEN p, GEN L, GEN Lden, struct galois_borne *gb, struct galois_lift *gl)
     327             : {
     328        1589 :   pari_sp av = avma;
     329             :   long e;
     330        1589 :   gl->gb = gb;
     331        1589 :   gl->T = T;
     332        1589 :   gl->den = is_pm1(den)? gen_1: den;
     333        1589 :   gl->p = p;
     334        1589 :   gl->L = L;
     335        1589 :   gl->Lden = Lden;
     336        1589 :   e = logint(shifti(gb->bornesol, 2+BITS_IN_LONG),p) + 1;
     337        1589 :   avma = av;
     338        1589 :   if (e < 2) e = 2;
     339        1589 :   gl->e = e;
     340        1589 :   gl->Q = powiu(p, e);
     341        1589 :   gl->TQ = FpX_red(T,gl->Q);
     342        1589 : }
     343             : 
     344             : /* Check whether f is (with high probability) a solution and compute its
     345             :  * permutation */
     346             : static int
     347        3857 : poltopermtest(GEN f, struct galois_lift *gl, GEN pf)
     348             : {
     349             :   pari_sp av;
     350        3857 :   GEN fx, fp, B = gl->gb->bornesol;
     351             :   long i, j, ll;
     352       34636 :   for (i = 2; i < lg(f); i++)
     353       32711 :     if (abscmpii(gel(f,i),B) > 0)
     354             :     {
     355        1932 :       if (DEBUGLEVEL>=4) err_printf("GaloisConj: Solution too large.\n");
     356        1932 :       if (DEBUGLEVEL>=8) err_printf("f=%Ps\n borne=%Ps\n",f,B);
     357        1932 :       return 0;
     358             :     }
     359        1925 :   ll = lg(gl->L);
     360        1925 :   fp = const_vecsmall(ll-1, 1); /* left on stack */
     361        1925 :   av = avma;
     362       32949 :   for (i = 1; i < ll; i++, avma = av)
     363             :   {
     364       31031 :     fx = FpX_eval(f, gel(gl->L,i), gl->gb->ladicsol);
     365      593761 :     for (j = 1; j < ll; j++)
     366      593754 :       if (fp[j] && equalii(fx, gel(gl->Lden,j))) { pf[i]=j; fp[j]=0; break; }
     367       31031 :     if (j == ll) return 0;
     368             :   }
     369        1918 :   return 1;
     370             : }
     371             : 
     372             : struct monoratlift
     373             : {
     374             :   struct galois_lift *gl;
     375             :   GEN frob;
     376             :   long early;
     377             : };
     378             : 
     379             : static int
     380        5256 : monoratlift(void *E, GEN S, GEN q)
     381             : {
     382        5256 :   struct monoratlift *d = (struct monoratlift *) E;
     383        5256 :   struct galois_lift *gl = d->gl;
     384        5256 :   GEN qm1old = sqrti(shifti(q,-2));
     385        5256 :   GEN tlift = FpX_ratlift(S,q,qm1old,qm1old,gl->den);
     386        5256 :   if (tlift)
     387             :   {
     388         877 :     pari_sp ltop = avma;
     389         877 :     if(DEBUGLEVEL>=4)
     390           0 :       err_printf("MonomorphismLift: trying early solution %Ps\n",tlift);
     391         877 :     if (gl->den != gen_1) {
     392         632 :       GEN N = gl->gb->ladicsol, N2 = shifti(N,-1);
     393         632 :       tlift = FpX_center(FpX_red(Q_muli_to_int(tlift, gl->den), N), N,N2);
     394             :     }
     395         877 :     if (poltopermtest(tlift, gl, d->frob))
     396             :     {
     397         870 :       if(DEBUGLEVEL>=4) err_printf("MonomorphismLift: true early solution.\n");
     398         870 :       d->early = 1;
     399         870 :       avma = ltop; return 1;
     400             :     }
     401           7 :     avma = ltop;
     402           7 :     if(DEBUGLEVEL>=4) err_printf("MonomorphismLift: false early solution.\n");
     403             :   }
     404        4386 :   return 0;
     405             : }
     406             : 
     407             : static GEN
     408        1722 : monomorphismratlift(GEN P, GEN S, struct galois_lift *gl, GEN frob)
     409             : {
     410             :   pari_timer ti;
     411        1722 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
     412        1722 :   if (frob)
     413             :   {
     414             :     struct monoratlift d;
     415        1568 :     d.gl = gl; d.frob = frob; d.early = 0;
     416        1568 :     S = ZpX_ZpXQ_liftroot_ea(P,S,gl->T,gl->p, gl->e, (void*)&d, monoratlift);
     417        1568 :     S = d.early ? NULL: S;
     418             :   }
     419             :   else
     420         154 :     S = ZpX_ZpXQ_liftroot(P,S,gl->T,gl->p, gl->e);
     421        1722 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "monomorphismlift()");
     422        1722 :   return S;
     423             : }
     424             : 
     425             : /* Let T be a polynomial in Z[X] , p a prime number, S in Fp[X]/(T) so
     426             :  * that T(S)=0 [p,T]. Lift S in S_0 so that T(S_0)=0 [T,p^e]
     427             :  * Unclean stack */
     428             : static GEN
     429        1722 : automorphismlift(GEN S, struct galois_lift *gl, GEN frob)
     430             : {
     431        1722 :   return monomorphismratlift(gl->T, S, gl, frob);
     432             : }
     433             : 
     434             : static GEN
     435        1589 : galoisdolift(struct galois_lift *gl, GEN frob)
     436             : {
     437        1589 :   pari_sp av = avma;
     438        1589 :   GEN Tp = FpX_red(gl->T, gl->p);
     439        1589 :   GEN S = FpX_Frobenius(Tp, gl->p);
     440        1589 :   return gerepileupto(av, automorphismlift(S, gl, frob));
     441             : }
     442             : 
     443             : static void
     444         609 : inittestlift(GEN plift, GEN Tmod, struct galois_lift *gl,
     445             :              struct galois_testlift *gt)
     446             : {
     447             :   pari_timer ti;
     448         609 :   gt->n = lg(gl->L) - 1;
     449         609 :   gt->g = lg(Tmod) - 1;
     450         609 :   gt->f = gt->n / gt->g;
     451         609 :   gt->bezoutcoeff = bezout_lift_fact(gl->T, Tmod, gl->p, gl->e);
     452         609 :   if (DEBUGLEVEL >= 2) timer_start(&ti);
     453         609 :   gt->pauto = FpXQ_autpowers(plift, gt->f-1, gl->TQ, gl->Q);
     454         609 :   if (DEBUGLEVEL >= 2) timer_printf(&ti, "Frobenius power");
     455         609 : }
     456             : 
     457             : /* Explanation of the intheadlong technique:
     458             :  * Let C be a bound, B = BITS_IN_LONG, M > C*2^B a modulus and 0 <= a_i < M for
     459             :  * i=1,...,n where n < 2^B. We want to test if there exist k,l, |k| < C < M/2^B,
     460             :  * such that sum a_i = k + l*M
     461             :  * We write a_i*2^B/M = b_i+c_i with b_i integer and 0<=c_i<1, so that
     462             :  *   sum b_i - l*2^B = k*2^B/M - sum c_i
     463             :  * Since -1 < k*2^B/M < 1 and 0<=c_i<1, it follows that
     464             :  *   -n-1 < sum b_i - l*2^B < 1  i.e.  -n <= sum b_i -l*2^B <= 0
     465             :  * So we compute z = - sum b_i [mod 2^B] and check if 0 <= z <= n. */
     466             : 
     467             : /* Assume 0 <= x < mod. */
     468             : static ulong
     469      968548 : intheadlong(GEN x, GEN mod)
     470             : {
     471      968548 :   pari_sp av = avma;
     472      968548 :   long res = (long) itou(divii(shifti(x,BITS_IN_LONG),mod));
     473      968548 :   avma = av; return res;
     474             : }
     475             : static GEN
     476       22414 : vecheadlong(GEN W, GEN mod)
     477             : {
     478       22414 :   long i, l = lg(W);
     479       22414 :   GEN V = cgetg(l, t_VECSMALL);
     480       22414 :   for(i=1; i<l; i++) V[i] = intheadlong(gel(W,i), mod);
     481       22414 :   return V;
     482             : }
     483             : static GEN
     484        1127 : matheadlong(GEN W, GEN mod)
     485             : {
     486        1127 :   long i, l = lg(W);
     487        1127 :   GEN V = cgetg(l,t_MAT);
     488        1127 :   for(i=1; i<l; i++) gel(V,i) = vecheadlong(gel(W,i), mod);
     489        1127 :   return V;
     490             : }
     491             : static ulong
     492       45640 : polheadlong(GEN P, long n, GEN mod)
     493             : {
     494       45640 :   return (lg(P)>n+2)? intheadlong(gel(P,n+2),mod): 0;
     495             : }
     496             : 
     497             : #define headlongisint(Z,N) (-(ulong)(Z)<=(ulong)(N))
     498             : 
     499             : static long
     500         679 : frobeniusliftall(GEN sg, long el, GEN *psi, struct galois_lift *gl,
     501             :                  struct galois_testlift *gt, GEN frob)
     502             : {
     503         679 :   pari_sp av, ltop2, ltop = avma;
     504         679 :   long i,j,k, c = lg(sg)-1, n = lg(gl->L)-1, m = gt->g, d = m / c;
     505             :   GEN pf, u, v, C, Cd, SG, cache;
     506         679 :   long N1, N2, R1, Ni, ord = gt->f, c_idx = gt->g-1;
     507             :   ulong headcache;
     508         679 :   long hop = 0;
     509             :   GEN NN, NQ;
     510             :   pari_timer ti;
     511             : 
     512         679 :   *psi = pf = cgetg(m, t_VECSMALL);
     513         679 :   ltop2 = avma;
     514         679 :   NN = diviiexact(mpfact(m), mului(c, powiu(mpfact(d), c)));
     515         679 :   if (DEBUGLEVEL >= 4)
     516           0 :     err_printf("GaloisConj: I will try %Ps permutations\n", NN);
     517         679 :   N1=10000000;
     518         679 :   NQ=divis_rem(NN,N1,&R1);
     519         679 :   if (abscmpiu(NQ,1000000000)>0)
     520             :   {
     521           0 :     pari_warn(warner,"Combinatorics too hard : would need %Ps tests!\n"
     522             :         "I will skip it, but it may induce an infinite loop",NN);
     523           0 :     avma = ltop; *psi = NULL; return 0;
     524             :   }
     525         679 :   N2=itos(NQ); if(!N2) N1=R1;
     526         679 :   if (DEBUGLEVEL>=4) timer_start(&ti);
     527         679 :   avma = ltop2;
     528         679 :   C = gt->C;
     529         679 :   Cd= gt->Cd;
     530         679 :   v = FpXQ_mul(gel(gt->pauto, 1+el%ord), gel(gt->bezoutcoeff, m),gl->TQ,gl->Q);
     531         679 :   if (gl->den != gen_1) v = FpX_Fp_mul(v, gl->den, gl->Q);
     532         679 :   SG = cgetg(lg(sg),t_VECSMALL);
     533         679 :   for(i=1; i<lg(SG); i++) SG[i] = (el*sg[i])%ord + 1;
     534         679 :   cache = cgetg(m+1,t_VECSMALL); cache[m] = polheadlong(v,1,gl->Q);
     535         679 :   headcache = polheadlong(v,2,gl->Q);
     536         679 :   for (i = 1; i < m; i++) pf[i] = 1 + i/d;
     537         679 :   av = avma;
     538      233793 :   for (Ni = 0, i = 0; ;i++)
     539             :   {
     540     1109304 :     for (j = c_idx ; j > 0; j--)
     541             :     {
     542      875511 :       long h = SG[pf[j]];
     543      875511 :       if (!mael(C,h,j))
     544             :       {
     545        3549 :         pari_sp av3 = avma;
     546        3549 :         GEN r = FpXQ_mul(gel(gt->pauto,h), gel(gt->bezoutcoeff,j),gl->TQ,gl->Q);
     547        3549 :         if (gl->den != gen_1) r = FpX_Fp_mul(r, gl->den, gl->Q);
     548        3549 :         gmael(C,h,j) = gclone(r);
     549        3549 :         mael(Cd,h,j) = polheadlong(r,1,gl->Q);
     550        3549 :         avma = av3;
     551             :       }
     552      875511 :       uel(cache,j) = uel(cache,j+1)+umael(Cd,h,j);
     553             :     }
     554      233793 :     if (headlongisint(uel(cache,1),n))
     555             :     {
     556        3290 :       ulong head = headcache;
     557        3290 :       for (j = 1; j < m; j++) head += polheadlong(gmael(C,SG[pf[j]],j),2,gl->Q);
     558        3290 :       if (headlongisint(head,n))
     559             :       {
     560        1064 :         u = v;
     561        1064 :         for (j = 1; j < m; j++) u = ZX_add(u, gmael(C,SG[pf[j]],j));
     562        1064 :         u = FpX_center(FpX_red(u, gl->Q), gl->Q, shifti(gl->Q,-1));
     563        1064 :         if (poltopermtest(u, gl, frob))
     564             :         {
     565         511 :           if (DEBUGLEVEL >= 4)
     566             :           {
     567           0 :             timer_printf(&ti, "");
     568           0 :             err_printf("GaloisConj: %d hops on %Ps tests\n",hop,addis(mulss(Ni,N1),i));
     569             :           }
     570         511 :           avma = ltop2; return 1;
     571             :         }
     572         553 :         if (DEBUGLEVEL >= 4) err_printf("M");
     573             :       }
     574        2226 :       else hop++;
     575             :     }
     576      233282 :     if (DEBUGLEVEL >= 4 && i % maxss(N1/20, 1) == 0)
     577           0 :       timer_printf(&ti, "GaloisConj:Testing %Ps", addis(mulss(Ni,N1),i));
     578      233282 :     avma = av;
     579      233282 :     if (i == N1-1)
     580             :     {
     581         168 :       if (Ni==N2-1) N1 = R1;
     582         168 :       if (Ni==N2) break;
     583           0 :       Ni++; i = 0;
     584           0 :       if (DEBUGLEVEL>=4) timer_start(&ti);
     585             :     }
     586      233114 :     for (j = 2; j < m && pf[j-1] >= pf[j]; j++)
     587             :       /*empty*/; /* to kill clang Warning */
     588      233114 :     for (k = 1; k < j-k && pf[k] != pf[j-k]; k++) { lswap(pf[k], pf[j-k]); }
     589      233114 :     for (k = j - 1; pf[k] >= pf[j]; k--)
     590             :       /*empty*/;
     591      233114 :     lswap(pf[j], pf[k]); c_idx = j;
     592      233114 :   }
     593         168 :   if (DEBUGLEVEL>=4) err_printf("GaloisConj: not found, %d hops \n",hop);
     594         168 :   *psi = NULL; avma = ltop; return 0;
     595             : }
     596             : 
     597             : /* Compute the test matrix for the i-th line of V. Clone. */
     598             : static GEN
     599        1127 : Vmatrix(long i, struct galois_test *td)
     600             : {
     601        1127 :   pari_sp av = avma;
     602        1127 :   GEN m = gclone( matheadlong(FpC_FpV_mul(td->L, row(td->M,i), td->ladic), td->ladic));
     603        1127 :   avma = av; return m;
     604             : }
     605             : 
     606             : /* Initialize galois_test */
     607             : static void
     608         952 : inittest(GEN L, GEN M, GEN borne, GEN ladic, struct galois_test *td)
     609             : {
     610         952 :   long i, n = lg(L)-1;
     611         952 :   GEN p = cgetg(n+1, t_VECSMALL);
     612         952 :   if (DEBUGLEVEL >= 8) err_printf("GaloisConj: Init Test\n");
     613         952 :   td->order = p;
     614         952 :   for (i = 1; i <= n-2; i++) p[i] = i+2;
     615         952 :   p[n-1] = 1; p[n] = 2;
     616         952 :   td->borne = borne;
     617         952 :   td->lborne = subii(ladic, borne);
     618         952 :   td->ladic = ladic;
     619         952 :   td->L = L;
     620         952 :   td->M = M;
     621         952 :   td->TM = shallowtrans(M);
     622         952 :   td->PV = zero_zv(n);
     623         952 :   gel(td->PV, 2) = Vmatrix(2, td);
     624         952 : }
     625             : 
     626             : /* Free clones stored inside galois_test */
     627             : static void
     628         952 : freetest(struct galois_test *td)
     629             : {
     630             :   long i;
     631       15876 :   for (i = 1; i < lg(td->PV); i++)
     632       14924 :     if (td->PV[i]) { gunclone(gel(td->PV,i)); td->PV[i] = 0; }
     633         952 : }
     634             : 
     635             : /* Check if the integer P seen as a p-adic number is close to an integer less
     636             :  * than td->borne in absolute value */
     637             : static long
     638       33341 : padicisint(GEN P, struct galois_test *td)
     639             : {
     640       33341 :   pari_sp ltop = avma;
     641       33341 :   GEN U  = modii(P, td->ladic);
     642       33341 :   long r = cmpii(U, td->borne) <= 0 || cmpii(U, td->lborne) >= 0;
     643       33341 :   avma = ltop; return r;
     644             : }
     645             : 
     646             : /* Check if the permutation pf is valid according to td.
     647             :  * If not, update td to make subsequent test faster (hopefully) */
     648             : static long
     649       68614 : galois_test_perm(struct galois_test *td, GEN pf)
     650             : {
     651       68614 :   pari_sp av = avma;
     652       68614 :   long i, j, n = lg(td->L)-1;
     653       68614 :   GEN V, P = NULL;
     654      102767 :   for (i = 1; i < n; i++)
     655             :   {
     656      101318 :     long ord = td->order[i];
     657      101318 :     GEN PW = gel(td->PV, ord);
     658      101318 :     if (PW)
     659             :     {
     660       67977 :       ulong head = umael(PW,1,pf[1]);
     661       67977 :       for (j = 2; j <= n; j++) head += umael(PW,j,pf[j]);
     662       67977 :       if (!headlongisint(head,n)) break;
     663             :     } else
     664             :     {
     665       33341 :       if (!P) P = vecpermute(td->L, pf);
     666       33341 :       V = FpV_dotproduct(gel(td->TM,ord), P, td->ladic);
     667       33341 :       if (!padicisint(V, td)) {
     668         175 :         gel(td->PV, ord) = Vmatrix(ord, td);
     669         175 :         if (DEBUGLEVEL >= 4) err_printf("M");
     670         175 :         break;
     671             :       }
     672             :     }
     673             :   }
     674       68614 :   if (i == n) { avma = av; return 1; }
     675       67165 :   if (DEBUGLEVEL >= 4) err_printf("%d.", i);
     676       67165 :   if (i > 1)
     677             :   {
     678         483 :     long z = td->order[i];
     679         483 :     for (j = i; j > 1; j--) td->order[j] = td->order[j-1];
     680         483 :     td->order[1] = z;
     681         483 :     if (DEBUGLEVEL >= 8) err_printf("%Ps", td->order);
     682             :   }
     683       67165 :   avma = av; return 0;
     684             : }
     685             : /*Compute a*b/c when a*b will overflow*/
     686             : static long
     687           0 : muldiv(long a,long b,long c)
     688             : {
     689           0 :   return (long)((double)a*(double)b/c);
     690             : }
     691             : 
     692             : /* F = cycle decomposition of sigma,
     693             :  * B = cycle decomposition of cl(tau).
     694             :  * Check all permutations pf who can possibly correspond to tau, such that
     695             :  * tau*sigma*tau^-1 = sigma^s and tau^d = sigma^t, where d = ord cl(tau)
     696             :  * x: vector of choices,
     697             :  * G: vector allowing linear access to elts of F.
     698             :  * Choices multiple of e are not changed. */
     699             : static GEN
     700        1533 : testpermutation(GEN F, GEN B, GEN x, long s, long e, long cut,
     701             :                 struct galois_test *td)
     702             : {
     703        1533 :   pari_sp av, avm = avma;
     704             :   long a, b, c, d, n, p1, p2, p3, p4, p5, p6, l1, l2, N1, N2, R1;
     705        1533 :   long i, j, cx, hop = 0, start = 0;
     706             :   GEN pf, ar, G, W, NN, NQ;
     707             :   pari_timer ti;
     708        1533 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
     709        1533 :   a = lg(F)-1; b = lg(gel(F,1))-1;
     710        1533 :   c = lg(B)-1; d = lg(gel(B,1))-1;
     711        1533 :   n = a*b;
     712        1533 :   s = (b+s) % b;
     713        1533 :   pf = cgetg(n+1, t_VECSMALL);
     714        1533 :   av = avma;
     715        1533 :   ar = cgetg(a+2, t_VECSMALL); ar[a+1]=0;
     716        1533 :   G  = cgetg(a+1, t_VECSMALL);
     717        1533 :   W  = gel(td->PV, td->order[n]);
     718       14357 :   for (cx=1, i=1, j=1; cx <= a; cx++, i++)
     719             :   {
     720       12824 :     gel(G,cx) = gel(F, coeff(B,i,j));
     721       12824 :     if (i == d) { i = 0; j++; }
     722             :   }
     723        1533 :   NN = divis(powuu(b, c * (d - d/e)), cut);
     724        1533 :   if (DEBUGLEVEL>=4) err_printf("GaloisConj: I will try %Ps permutations\n", NN);
     725        1533 :   N1 = 1000000;
     726        1533 :   NQ = divis_rem(NN,N1,&R1);
     727        1533 :   if (abscmpiu(NQ,100000000)>0)
     728             :   {
     729           0 :     avma = avm;
     730           0 :     pari_warn(warner,"Combinatorics too hard: would need %Ps tests!\n"
     731             :                      "I'll skip it but you will get a partial result...",NN);
     732           0 :     return identity_perm(n);
     733             :   }
     734        1533 :   N2 = itos(NQ);
     735        1680 :   for (l2 = 0; l2 <= N2; l2++)
     736             :   {
     737        1533 :     long nbiter = (l2<N2) ? N1: R1;
     738        1533 :     if (DEBUGLEVEL >= 2 && N2) err_printf("%d%% ", muldiv(l2,100,N2));
     739     4813984 :     for (l1 = 0; l1 < nbiter; l1++)
     740             :     {
     741     4813837 :       if (start)
     742             :       {
     743    12671869 :         for (i=1, j=e; i < a;)
     744             :         {
     745     7859565 :           if ((++(x[i])) != b) break;
     746     3047261 :           x[i++] = 0;
     747     3047261 :           if (i == j) { i++; j += e; }
     748             :         }
     749             :       }
     750        1533 :       else { start=1; i = a-1; }
     751             :       /* intheadlong test: overflow in + is OK, we compute mod 2^BIL */
     752    20234382 :       for (p1 = i+1, p5 = p1%d - 1 ; p1 >= 1; p1--, p5--) /* p5 = (p1%d) - 1 */
     753             :       {
     754    15420545 :         ulong V = 0;
     755    15420545 :         if (p5 == - 1) { p5 = d - 1; p6 = p1 + 1 - d; } else p6 = p1 + 1;
     756    15420545 :         p4 = p5 ? x[p1-1] : 0;
     757    15420545 :         V = 0;
     758    51575496 :         for (p2 = 1+p4, p3 = 1 + x[p1]; p2 <= b; p2++)
     759             :         {
     760    36154951 :           V += umael(W,mael(G,p6,p3),mael(G,p1,p2));
     761    36154951 :           p3 += s; if (p3 > b) p3 -= b;
     762             :         }
     763    15420545 :         p3 = 1 + x[p1] - s; if (p3 <= 0) p3 += b;
     764    23552067 :         for (p2 = p4; p2 >= 1; p2--)
     765             :         {
     766     8131522 :           V += umael(W,mael(G,p6,p3),mael(G,p1,p2));
     767     8131522 :           p3 -= s; if (p3 <= 0) p3 += b;
     768             :         }
     769    15420545 :         ar[p1] = ar[p1+1] + V;
     770             :       }
     771     4813837 :       if (!headlongisint(uel(ar,1),n)) continue;
     772             : 
     773             :       /* intheadlong succeeds. Full computation */
     774     2115155 :       for (p1=1, p5=d; p1 <= a; p1++, p5++)
     775             :       {
     776     2046604 :         if (p5 == d) { p5 = 0; p4 = 0; } else p4 = x[p1-1];
     777     2046604 :         if (p5 == d-1) p6 = p1+1-d; else p6 = p1+1;
     778     5733266 :         for (p2 = 1+p4, p3 = 1 + x[p1]; p2 <= b; p2++)
     779             :         {
     780     3686662 :           pf[mael(G,p1,p2)] = mael(G,p6,p3);
     781     3686662 :           p3 += s; if (p3 > b) p3 -= b;
     782             :         }
     783     2046604 :         p3 = 1 + x[p1] - s; if (p3 <= 0) p3 += b;
     784     2671935 :         for (p2 = p4; p2 >= 1; p2--)
     785             :         {
     786      625331 :           pf[mael(G,p1,p2)] = mael(G,p6,p3);
     787      625331 :           p3 -= s; if (p3 <= 0) p3 += b;
     788             :         }
     789             :       }
     790       68551 :       if (galois_test_perm(td, pf))
     791             :       {
     792        1386 :         if (DEBUGLEVEL >= 1)
     793             :         {
     794           0 :           GEN nb = addis(mulss(l2,N1),l1);
     795           0 :           timer_printf(&ti, "testpermutation(%Ps)", nb);
     796           0 :           if (DEBUGLEVEL >= 2 && hop)
     797           0 :             err_printf("GaloisConj: %d hop over %Ps iterations\n", hop, nb);
     798             :         }
     799        1386 :         avma = av; return pf;
     800             :       }
     801       67165 :       hop++;
     802             :     }
     803             :   }
     804         147 :   if (DEBUGLEVEL >= 1)
     805             :   {
     806           0 :     timer_printf(&ti, "testpermutation(%Ps)", NN);
     807           0 :     if (DEBUGLEVEL >= 2 && hop)
     808           0 :       err_printf("GaloisConj: %d hop over %Ps iterations\n", hop, NN);
     809             :   }
     810         147 :   avma = avm; return NULL;
     811             : }
     812             : 
     813             : /* List of subgroups of (Z/mZ)^* whose order divide o, and return the list
     814             :  * of their elements, sorted by increasing order */
     815             : static GEN
     816         728 : listznstarelts(long m, long o)
     817             : {
     818         728 :   pari_sp av = avma;
     819             :   GEN L, zn, zns;
     820             :   long i, phi, ind, l;
     821         728 :   if (m == 2) retmkvec(mkvecsmall(1));
     822         693 :   zn = znstar(stoi(m));
     823         693 :   phi = itos(gel(zn,1));
     824         693 :   o = ugcd(o, phi); /* do we impose this on input ? */
     825         693 :   zns = znstar_small(zn);
     826         693 :   L = cgetg(o+1, t_VEC);
     827        2142 :   for (i=1,ind = phi; ind; ind -= phi/o, i++) /* by *decreasing* exact index */
     828        1449 :     gel(L,i) = subgrouplist(gel(zn,2), mkvec(utoipos(ind)));
     829         693 :   L = shallowconcat1(L); l = lg(L);
     830         693 :   for (i = 1; i < l; i++) gel(L,i) = znstar_hnf_elts(zns, gel(L,i));
     831         693 :   return gerepilecopy(av, L);
     832             : }
     833             : 
     834             : /* A sympol is a symmetric polynomial
     835             :  *
     836             :  * Currently sympol are couple of t_VECSMALL [v,w]
     837             :  * v[1]...v[k], w[1]...w[k]  represent the polynomial sum(i=1,k,v[i]*s_w[i])
     838             :  * where s_i(X_1,...,X_n) = sum(j=1,n,X_j^i) */
     839             : 
     840             : /*Return s_e*/
     841             : static GEN
     842        2919 : sympol_eval_newtonsum(long e, GEN O, GEN mod)
     843             : {
     844        2919 :   long f = lg(O), g = lg(gel(O,1)), i, j;
     845        2919 :   GEN PL = cgetg(f, t_COL);
     846       16758 :   for(i=1; i<f; i++)
     847             :   {
     848       13839 :     pari_sp av = avma;
     849       13839 :     GEN s = gen_0;
     850       13839 :     for(j=1; j<g; j++) s = addii(s, Fp_powu(gmael(O,i,j), (ulong)e, mod));
     851       13839 :     gel(PL,i) = gerepileuptoint(av, remii(s,mod));
     852             :   }
     853        2919 :   return PL;
     854             : }
     855             : 
     856             : static GEN
     857        2310 : sympol_eval(GEN v, GEN NS)
     858             : {
     859        2310 :   pari_sp av = avma;
     860             :   long i;
     861        2310 :   GEN S = gen_0;
     862        5824 :   for (i=1; i<lg(v); i++)
     863        3514 :     if (v[i]) S = gadd(S, gmulsg(v[i], gel(NS,i)));
     864        2310 :   return gerepileupto(av, S);
     865             : }
     866             : 
     867             : /* Let sigma be an automorphism of L (as a polynomial with rational coefs)
     868             :  * Let 'sym' be a symmetric polynomial defining alpha in L.
     869             :  * We have alpha = sym(x,sigma(x),,,sigma^(g-1)(x)). Compute alpha mod p */
     870             : static GEN
     871         945 : sympol_aut_evalmod(GEN sym, long g, GEN sigma, GEN Tp, GEN p)
     872             : {
     873         945 :   pari_sp ltop=avma;
     874         945 :   long i, j, npows = brent_kung_optpow(degpol(Tp)-1, g-1, 1);
     875         945 :   GEN s, f, pows, v = zv_to_ZV(gel(sym,1)), w = zv_to_ZV(gel(sym,2));
     876         945 :   sigma = RgX_to_FpX(sigma, p);
     877         945 :   pows  = FpXQ_powers(sigma,npows,Tp,p);
     878         945 :   f = pol_x(varn(sigma));
     879         945 :   s = pol_0(varn(sigma));
     880        5068 :   for(i=1; i<=g;i++)
     881             :   {
     882        4123 :     if (i > 1) f = FpX_FpXQV_eval(f,pows,Tp,p);
     883        8778 :     for(j=1; j<lg(v); j++)
     884        4655 :       s = FpX_add(s, FpX_Fp_mul(FpXQ_pow(f,gel(w,j),Tp,p),gel(v,j),p),p);
     885             :   }
     886         945 :   return gerepileupto(ltop, s);
     887             : }
     888             : 
     889             : /* Let Sp be as computed with sympol_aut_evalmod
     890             :  * Let Tmod be the factorisation of T mod p.
     891             :  * Return the factorisation of the minimal polynomial of S mod p w.r.t. Tmod */
     892             : static GEN
     893         945 : fixedfieldfactmod(GEN Sp, GEN p, GEN Tmod)
     894             : {
     895         945 :   long i, l = lg(Tmod);
     896         945 :   GEN F = cgetg(l,t_VEC);
     897        4207 :   for(i=1; i<l; i++)
     898             :   {
     899        3262 :     GEN Ti = gel(Tmod,i);
     900        3262 :     gel(F,i) = FpXQ_minpoly(FpX_rem(Sp,Ti,p), Ti,p);
     901             :   }
     902         945 :   return F;
     903             : }
     904             : 
     905             : static GEN
     906        1834 : fixedfieldsurmer(GEN mod, GEN l, GEN p, long v, GEN NS, GEN W)
     907             : {
     908        1834 :   const long step=3;
     909        1834 :   long i, j, n = lg(W)-1, m = 1L<<((n-1)<<1);
     910        1834 :   GEN sym = cgetg(n+1,t_VECSMALL), mod2 = shifti(mod,-1);
     911        1834 :   for (j=1;j<n;j++) sym[j] = step;
     912        1834 :   sym[n] = 0;
     913        1834 :   if (DEBUGLEVEL>=4) err_printf("FixedField: Weight: %Ps\n",W);
     914        2408 :   for (i=0; i<m; i++)
     915             :   {
     916        2310 :     pari_sp av = avma;
     917             :     GEN L, P;
     918        2310 :     for (j=1; sym[j]==step; j++) sym[j]=0;
     919        2310 :     sym[j]++;
     920        2310 :     if (DEBUGLEVEL>=6) err_printf("FixedField: Sym: %Ps\n",sym);
     921        2310 :     L = sympol_eval(sym,NS);
     922        2310 :     if (!vec_is1to1(FpC_red(L,l))) continue;
     923        1911 :     P = FpX_center(FpV_roots_to_pol(L,mod,v),mod,mod2);
     924        1911 :     if (!p || FpX_is_squarefree(P,p)) return mkvec3(mkvec2(sym,W),L,P);
     925         175 :     avma = av;
     926             :   }
     927          98 :   return NULL;
     928             : }
     929             : 
     930             : /*Check whether the line of NS are pair-wise distinct.*/
     931             : static long
     932        1967 : sympol_is1to1_lg(GEN NS, long n)
     933             : {
     934        1967 :   long i, j, k, l = lgcols(NS);
     935       11347 :   for (i=1; i<l; i++)
     936       58968 :     for(j=i+1; j<l; j++)
     937             :     {
     938       51051 :       for(k=1; k<n; k++)
     939       50918 :         if (!equalii(gmael(NS,k,j),gmael(NS,k,i))) break;
     940       49588 :       if (k>=n) return 0;
     941             :     }
     942        1834 :   return 1;
     943             : }
     944             : 
     945             : /* Let O a set of orbits of roots (see fixedfieldorbits) modulo mod,
     946             :  * l | mod and p two prime numbers. Return a vector [sym,s,P] where:
     947             :  * sym is a sympol, s is the set of images of sym on O and
     948             :  * P is the polynomial with roots s. */
     949             : static GEN
     950        1736 : fixedfieldsympol(GEN O, GEN mod, GEN l, GEN p, long v)
     951             : {
     952        1736 :   pari_sp ltop=avma;
     953        1736 :   const long n=(BITS_IN_LONG>>1)-1;
     954        1736 :   GEN NS = cgetg(n+1,t_MAT), sym = NULL, W = cgetg(n+1,t_VECSMALL);
     955        1736 :   long i, e=1;
     956        1736 :   if (DEBUGLEVEL>=4)
     957           0 :     err_printf("FixedField: Size: %ldx%ld\n",lg(O)-1,lg(gel(O,1))-1);
     958        3703 :   for (i=1; !sym && i<=n; i++)
     959             :   {
     960        1967 :     GEN L = sympol_eval_newtonsum(e++, O, mod);
     961        1967 :     if (lg(O)>2)
     962        1925 :       while (vec_isconst(L)) L = sympol_eval_newtonsum(e++, O, mod);
     963        1967 :     W[i] = e-1; gel(NS,i) = L;
     964        1967 :     if (sympol_is1to1_lg(NS,i+1))
     965        1834 :       sym = fixedfieldsurmer(mod,l,p,v,NS,vecsmall_shorten(W,i));
     966             :   }
     967        1736 :   if (!sym) pari_err_BUG("fixedfieldsympol [p too small]");
     968        1736 :   if (DEBUGLEVEL>=2) err_printf("FixedField: Found: %Ps\n",gel(sym,1));
     969        1736 :   return gerepilecopy(ltop,sym);
     970             : }
     971             : 
     972             : /* Let O a set of orbits as indices and L the corresponding roots.
     973             :  * Return the set of orbits as roots. */
     974             : static GEN
     975        1736 : fixedfieldorbits(GEN O, GEN L)
     976             : {
     977        1736 :   GEN S = cgetg(lg(O), t_MAT);
     978             :   long i;
     979        1736 :   for (i = 1; i < lg(O); i++) gel(S,i) = vecpermute(L, gel(O,i));
     980        1736 :   return S;
     981             : }
     982             : 
     983             : static GEN
     984         560 : fixedfieldinclusion(GEN O, GEN PL)
     985             : {
     986         560 :   long i, j, f = lg(O)-1, g = lg(gel(O,1))-1;
     987         560 :   GEN S = cgetg(f*g + 1, t_COL);
     988        3367 :   for (i = 1; i <= f; i++)
     989             :   {
     990        2807 :     GEN Oi = gel(O,i);
     991        2807 :     for (j = 1; j <= g; j++) gel(S, Oi[j]) = gel(PL, i);
     992             :   }
     993         560 :   return S;
     994             : }
     995             : 
     996             : /* Polynomial attached to a vector of conjugates. Not stack clean */
     997             : static GEN
     998        5929 : vectopol(GEN v, GEN M, GEN den , GEN mod, GEN mod2, long x)
     999             : {
    1000        5929 :   long l = lg(v)+1, i;
    1001        5929 :   GEN z = cgetg(l,t_POL);
    1002        5929 :   z[1] = evalsigne(1)|evalvarn(x);
    1003       68159 :   for (i=2; i<l; i++)
    1004       62230 :     gel(z,i) = gdiv(centermodii(ZMrow_ZC_mul(M,v,i-1), mod, mod2), den);
    1005        5929 :   return normalizepol_lg(z, l);
    1006             : }
    1007             : 
    1008             : /* Polynomial associate to a permutation of the roots. Not stack clean */
    1009             : static GEN
    1010        5019 : permtopol(GEN p, GEN L, GEN M, GEN den, GEN mod, GEN mod2, long x)
    1011             : {
    1012        5019 :   if (lg(p) != lg(L)) pari_err_TYPE("permtopol [permutation]", p);
    1013        5019 :   return vectopol(vecpermute(L,p), M, den, mod, mod2, x);
    1014             : }
    1015             : 
    1016             : static GEN
    1017         441 : galoisgrouptopol(GEN res, GEN L, GEN M, GEN den, GEN mod, long v)
    1018             : {
    1019         441 :   long i, l = lg(res);
    1020         441 :   GEN mod2 = shifti(mod,-1), aut = cgetg(l, t_COL);
    1021        2716 :   for (i = 1; i < l; i++)
    1022             :   {
    1023        2275 :     if (DEBUGLEVEL>=6) err_printf("%d ",i);
    1024        2275 :     gel(aut,i) = permtopol(gel(res,i), L, M, den, mod, mod2, v);
    1025             :   }
    1026         441 :   return aut;
    1027             : }
    1028             : 
    1029             : static void
    1030        1183 : notgalois(long p, struct galois_analysis *ga)
    1031             : {
    1032        1183 :   if (DEBUGLEVEL >= 2) err_printf("GaloisAnalysis:non Galois for p=%ld\n", p);
    1033        1183 :   ga->p = p;
    1034        1183 :   ga->deg = 0;
    1035        1183 : }
    1036             : 
    1037             : /*Gather information about the group*/
    1038             : static long
    1039        2695 : init_group(long n, long np, GEN Fp, GEN Fe, long *porder)
    1040             : {
    1041        2695 :   const long prim_nonwss_orders[] = { 36,48,56,60,75,80,196,200 };
    1042        2695 :   long i, phi_order = 1, order = 1, group = 0;
    1043             : 
    1044             :  /* non-WSS groups of this order? */
    1045       24157 :   for (i=0; i < (long)numberof(prim_nonwss_orders); i++)
    1046       21476 :     if (n % prim_nonwss_orders[i] == 0) { group |= ga_non_wss; break; }
    1047        2695 :   if (np == 2 && Fp[2] == 3 && Fe[2] == 1 && Fe[1] > 2) group |= ga_ext_2;
    1048             : 
    1049        4172 :   for (i = np; i > 0; i--)
    1050             :   {
    1051        3144 :     long p = Fp[i];
    1052        3144 :     if (phi_order % p == 0) { group |= ga_all_normal; break; }
    1053        2723 :     order *= p; phi_order *= p-1;
    1054        2723 :     if (Fe[i] > 1) break;
    1055             :   }
    1056        2695 :   *porder = order; return group;
    1057             : }
    1058             : 
    1059             : /*is a "better" than b ? (if so, update karma) */
    1060             : static int
    1061       12105 : improves(long a, long b, long plift, long p, long n, long *karma)
    1062             : {
    1063       12105 :   if (!plift || a > b) { *karma = ugcd(p-1,n); return 1; }
    1064       11475 :   if (a == b) {
    1065        9725 :     long k = ugcd(p-1,n);
    1066        9725 :     if (k > *karma) { *karma = k; return 1; }
    1067             :   }
    1068       10543 :   return 0; /* worse */
    1069             : }
    1070             : 
    1071             : /* return 0 if not galois or not wss */
    1072             : static int
    1073        2695 : galoisanalysis(GEN T, struct galois_analysis *ga, long calcul_l)
    1074             : {
    1075        2695 :   pari_sp ltop = avma, av;
    1076        2695 :   long group, linf, n, p, i, karma = 0;
    1077             :   GEN F, Fp, Fe, Fpe, O;
    1078             :   long np, order, plift, nbmax, nbtest, deg;
    1079             :   pari_timer ti;
    1080             :   forprime_t S;
    1081        2695 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    1082        2695 :   n = degpol(T);
    1083        2695 :   O = zero_zv(n);
    1084        2695 :   F = factoru_pow(n);
    1085        2695 :   Fp = gel(F,1); np = lg(Fp)-1;
    1086        2695 :   Fe = gel(F,2);
    1087        2695 :   Fpe= gel(F,3);
    1088        2695 :   group = init_group(n, np, Fp, Fe, &order);
    1089             : 
    1090             :   /*Now we study the orders of the Frobenius elements*/
    1091        2695 :   deg = Fp[np]; /* largest prime | n */
    1092        2695 :   plift = 0;
    1093        2695 :   nbtest = 0;
    1094        2695 :   nbmax = 8+(n>>1);
    1095        2695 :   u_forprime_init(&S, n*maxss(expu(n)-3, 2), ULONG_MAX);
    1096        2695 :   av = avma;
    1097       26814 :   while (!plift || (nbtest < nbmax && (nbtest <=8 || order < (n>>1)))
    1098        1520 :                 || (n == 24 && O[6] == 0 && O[4] == 0)
    1099        1520 :                 || ((group&ga_non_wss) && order == Fp[np]))
    1100             :   {
    1101       22599 :     long d, o, norm_o = 1;
    1102             :     GEN D, Tp;
    1103             : 
    1104       22599 :     if ((group&ga_non_wss) && nbtest >= 3*nbmax) break; /* in all cases */
    1105       22599 :     nbtest++; avma = av;
    1106       22599 :     p = u_forprime_next(&S);
    1107       22599 :     if (!p) pari_err_OVERFLOW("galoisanalysis [ran out of primes]");
    1108       22599 :     Tp = ZX_to_Flx(T,p);
    1109       22599 :     if (!Flx_is_squarefree(Tp,p)) { if (!--nbtest) nbtest = 1; continue; }
    1110             : 
    1111       21127 :     D = Flx_nbfact_by_degree(Tp, &d, p);
    1112       21127 :     o = n / d; /* d factors, all should have degree o */
    1113       21127 :     if (D[o] != d) { notgalois(p, ga); avma = ltop; return 0; }
    1114             : 
    1115       19952 :     if (!O[o]) O[o] = p;
    1116       19952 :     if (o % deg) goto ga_end; /* NB: deg > 1 */
    1117       14129 :     if ((group&ga_all_normal) && o < order) goto ga_end;
    1118             : 
    1119             :     /*Frob_p has order o > 1, find a power which generates a normal subgroup*/
    1120       14024 :     if (o * Fp[1] >= n)
    1121        7640 :       norm_o = o; /*subgroups of smallest index are normal*/
    1122             :     else
    1123             :     {
    1124        6958 :       for (i = np; i > 0; i--)
    1125             :       {
    1126        6958 :         if (o % Fpe[i]) break;
    1127         574 :         norm_o *= Fpe[i];
    1128             :       }
    1129             :     }
    1130             :     /* Frob_p^(o/norm_o) generates a normal subgroup of order norm_o */
    1131       14024 :     if (norm_o != 1)
    1132             :     {
    1133        8214 :       if (!(group&ga_all_normal) || o > order)
    1134        1919 :         karma = ugcd(p-1,n);
    1135        6295 :       else if (!improves(norm_o, deg, plift,p,n, &karma)) goto ga_end;
    1136             :       /* karma0=0, deg0<=norm_o -> the first improves() returns 1 */
    1137        2879 :       deg = norm_o; group |= ga_all_normal; /* STORE */
    1138             :     }
    1139        5810 :     else if (group&ga_all_normal) goto ga_end;
    1140        5810 :     else if (!improves(o, order, plift,p,n, &karma)) goto ga_end;
    1141             : 
    1142        3481 :     order = o; plift = p; /* STORE */
    1143             :     ga_end:
    1144       19952 :     if (DEBUGLEVEL >= 5)
    1145           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);
    1146             :   }
    1147             :   /* To avoid looping on non-wss group.
    1148             :    * TODO: check for large groups. Would it be better to disable this check if
    1149             :    * we are in a good case (ga_all_normal && !(ga_ext_2) (e.g. 60)) ?*/
    1150        1520 :   ga->p = plift;
    1151        1520 :   if (!plift || ((group&ga_non_wss) && order == Fp[np]))
    1152             :   {
    1153           0 :     pari_warn(warner,"Galois group almost certainly not weakly super solvable");
    1154           0 :     return 0;
    1155             :   }
    1156             :   /*linf=(n*(n-1))>>2;*/
    1157        1520 :   linf = n;
    1158        1520 :   if (calcul_l && O[1] <= linf)
    1159             :   {
    1160             :     pari_sp av2;
    1161             :     forprime_t S2;
    1162             :     ulong p;
    1163         547 :     u_forprime_init(&S2, linf+1,ULONG_MAX);
    1164         547 :     av2 = avma;
    1165       42026 :     while ((p = u_forprime_next(&S2)))
    1166             :     { /*find a totally split prime l > linf*/
    1167       41479 :       GEN Tp = ZX_to_Flx(T, p);
    1168       41479 :       long nb = Flx_nbroots(Tp, p);
    1169       41479 :       if (nb == n) { O[1] = p; break; }
    1170       40940 :       if (nb && Flx_is_squarefree(Tp,p)) {
    1171           8 :         notgalois(p,ga);
    1172           8 :         avma = ltop; return 0;
    1173             :       }
    1174       40932 :       avma = av2;
    1175             :     }
    1176         539 :     if (!p) pari_err_OVERFLOW("galoisanalysis [ran out of primes]");
    1177             :   }
    1178        1512 :   ga->group = (enum ga_code)group;
    1179        1512 :   ga->deg = deg;
    1180        1512 :   ga->mindeg =  n == 135 ? 15: 0; /* otherwise the second phase is too slow */
    1181        1512 :   ga->ord = order;
    1182        1512 :   ga->l  = O[1];
    1183        1512 :   ga->p4 = n >= 4 ? O[4] : 0;
    1184        1512 :   if (DEBUGLEVEL >= 4)
    1185           0 :     err_printf("GaloisAnalysis:p=%ld l=%ld group=%ld deg=%ld ord=%ld\n",
    1186           0 :                plift, O[1], group, deg, order);
    1187        1512 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "galoisanalysis()");
    1188        1512 :   avma = ltop; return 1;
    1189             : }
    1190             : 
    1191             : static GEN
    1192          21 : a4galoisgen(struct galois_test *td)
    1193             : {
    1194          21 :   const long n = 12;
    1195          21 :   pari_sp ltop = avma, av, av2;
    1196          21 :   long i, j, k, N, hop = 0;
    1197             :   GEN MT, O,O1,O2,O3, ar, mt, t, u, res, orb, pft, pfu, pfv;
    1198             : 
    1199          21 :   res = cgetg(3, t_VEC);
    1200          21 :   pft = cgetg(n+1, t_VECSMALL);
    1201          21 :   pfu = cgetg(n+1, t_VECSMALL);
    1202          21 :   pfv = cgetg(n+1, t_VECSMALL);
    1203          21 :   gel(res,1) = mkvec3(pft,pfu,pfv);
    1204          21 :   gel(res,2) = mkvecsmall3(2,2,3);
    1205          21 :   av = avma;
    1206          21 :   ar = cgetg(5, t_VECSMALL);
    1207          21 :   mt = gel(td->PV, td->order[n]);
    1208          21 :   t = identity_perm(n) + 1; /* Sorry for this hack */
    1209          21 :   u = cgetg(n+1, t_VECSMALL) + 1; /* too lazy to correct */
    1210          21 :   MT = cgetg(n+1, t_MAT);
    1211          21 :   for (j = 1; j <= n; j++) gel(MT,j) = cgetg(n+1, t_VECSMALL);
    1212         273 :   for (j = 1; j <= n; j++)
    1213        1638 :     for (i = 1; i < j; i++)
    1214        1386 :       ucoeff(MT,i,j) = ucoeff(MT,j,i) = ucoeff(mt,i,j)+ucoeff(mt,j,i);
    1215             :   /* MT(i,i) unused */
    1216             : 
    1217          21 :   av2 = avma;
    1218             :   /* N = itos(gdiv(mpfact(n), mpfact(n >> 1))) >> (n >> 1); */
    1219             :   /* n = 2k = 12; N = (2k)! / (k! * 2^k) = 10395 */
    1220          21 :   N = 10395;
    1221          21 :   if (DEBUGLEVEL>=4) err_printf("A4GaloisConj: will test %ld permutations\n", N);
    1222          21 :   uel(ar,4) = umael(MT,11,12);
    1223          21 :   uel(ar,3) = uel(ar,4) + umael(MT,9,10);
    1224          21 :   uel(ar,2) = uel(ar,3) + umael(MT,7,8);
    1225          21 :   uel(ar,1) = uel(ar,2) + umael(MT,5,6);
    1226       61159 :   for (i = 0; i < N; i++)
    1227             :   {
    1228             :     long g;
    1229       61159 :     if (i)
    1230             :     {
    1231       61138 :       long a, x = i, y = 1;
    1232       86198 :       do { y += 2; a = x%y; x = x/y; } while (!a);
    1233       61138 :       switch (y)
    1234             :       {
    1235             :       case 3:
    1236       40768 :         lswap(t[2], t[2-a]);
    1237       40768 :         break;
    1238             :       case 5:
    1239       16296 :         x = t[0]; t[0] = t[2]; t[2] = t[1]; t[1] = x;
    1240       16296 :         lswap(t[4], t[4-a]);
    1241       16296 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1242       16296 :         break;
    1243             :       case 7:
    1244        3507 :         x = t[0]; t[0] = t[4]; t[4] = t[3]; t[3] = t[1]; t[1] = t[2]; t[2] = x;
    1245        3507 :         lswap(t[6], t[6-a]);
    1246        3507 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1247        3507 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1248        3507 :         break;
    1249             :       case 9:
    1250         518 :         x = t[0]; t[0] = t[6]; t[6] = t[5]; t[5] = t[3]; t[3] = x;
    1251         518 :         lswap(t[1], t[4]);
    1252         518 :         lswap(t[8], t[8-a]);
    1253         518 :         uel(ar,3) = uel(ar,4) + umael(MT,t[8],t[9]);
    1254         518 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1255         518 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1256         518 :         break;
    1257             :       case 11:
    1258          49 :         x = t[0]; t[0] = t[8]; t[8] = t[7]; t[7] = t[5]; t[5] = t[1];
    1259          49 :         t[1] = t[6]; t[6] = t[3]; t[3] = t[2]; t[2] = t[4]; t[4] = x;
    1260          49 :         lswap(t[10], t[10-a]);
    1261          49 :         uel(ar,4) = umael(MT,t[10],t[11]);
    1262          49 :         uel(ar,3) = uel(ar,4) + umael(MT,t[8],t[9]);
    1263          49 :         uel(ar,2) = uel(ar,3) + umael(MT,t[6],t[7]);
    1264          49 :         uel(ar,1) = uel(ar,2) + umael(MT,t[4],t[5]);
    1265             :       }
    1266             :     }
    1267       61159 :     g = uel(ar,1)+umael(MT,t[0],t[1])+umael(MT,t[2],t[3]);
    1268       61159 :     if (headlongisint(g,n))
    1269             :     {
    1270         147 :       for (k = 0; k < n; k += 2)
    1271             :       {
    1272         126 :         pft[t[k]] = t[k+1];
    1273         126 :         pft[t[k+1]] = t[k];
    1274             :       }
    1275          21 :       if (galois_test_perm(td, pft)) break;
    1276           0 :       hop++;
    1277             :     }
    1278       61138 :     avma = av2;
    1279             :   }
    1280          21 :   if (DEBUGLEVEL >= 1 && hop)
    1281           0 :     err_printf("A4GaloisConj: %ld hop over %ld iterations\n", hop, N);
    1282          21 :   if (i == N) { avma = ltop; return gen_0; }
    1283             :   /* N = itos(gdiv(mpfact(n >> 1), mpfact(n >> 2))) >> 1; */
    1284          21 :   N = 60;
    1285          21 :   if (DEBUGLEVEL >= 4) err_printf("A4GaloisConj: sigma=%Ps \n", pft);
    1286          84 :   for (k = 0; k < n; k += 4)
    1287             :   {
    1288          63 :     u[k+3] = t[k+3];
    1289          63 :     u[k+2] = t[k+1];
    1290          63 :     u[k+1] = t[k+2];
    1291          63 :     u[k]   = t[k];
    1292             :   }
    1293         973 :   for (i = 0; i < N; i++)
    1294             :   {
    1295         973 :     ulong g = 0;
    1296         973 :     if (i)
    1297             :     {
    1298         952 :       long a, x = i, y = -2;
    1299        1505 :       do { y += 4; a = x%y; x = x/y; } while (!a);
    1300         952 :       lswap(u[0],u[2]);
    1301         952 :       switch (y)
    1302             :       {
    1303             :       case 2:
    1304         476 :         break;
    1305             :       case 6:
    1306         399 :         lswap(u[4],u[6]);
    1307         399 :         if (!(a & 1))
    1308             :         {
    1309         161 :           a = 4 - (a>>1);
    1310         161 :           lswap(u[6], u[a]);
    1311         161 :           lswap(u[4], u[a-2]);
    1312             :         }
    1313         399 :         break;
    1314             :       case 10:
    1315          77 :         x = u[6];
    1316          77 :         u[6] = u[3];
    1317          77 :         u[3] = u[2];
    1318          77 :         u[2] = u[4];
    1319          77 :         u[4] = u[1];
    1320          77 :         u[1] = u[0];
    1321          77 :         u[0] = x;
    1322          77 :         if (a >= 3) a += 2;
    1323          77 :         a = 8 - a;
    1324          77 :         lswap(u[10],u[a]);
    1325          77 :         lswap(u[8], u[a-2]);
    1326          77 :         break;
    1327             :       }
    1328             :     }
    1329         973 :     for (k = 0; k < n; k += 2) g += mael(MT,u[k],u[k+1]);
    1330         973 :     if (headlongisint(g,n))
    1331             :     {
    1332         147 :       for (k = 0; k < n; k += 2)
    1333             :       {
    1334         126 :         pfu[u[k]] = u[k+1];
    1335         126 :         pfu[u[k+1]] = u[k];
    1336             :       }
    1337          21 :       if (galois_test_perm(td, pfu)) break;
    1338           0 :       hop++;
    1339             :     }
    1340         952 :     avma = av2;
    1341             :   }
    1342          21 :   if (i == N) { avma = ltop; return gen_0; }
    1343          21 :   if (DEBUGLEVEL >= 1 && hop)
    1344           0 :     err_printf("A4GaloisConj: %ld hop over %ld iterations\n", hop, N);
    1345          21 :   if (DEBUGLEVEL >= 4) err_printf("A4GaloisConj: tau=%Ps \n", pfu);
    1346          21 :   avma = av2;
    1347          21 :   orb = mkvec2(pft,pfu);
    1348          21 :   O = vecperm_orbits(orb, 12);
    1349          21 :   if (DEBUGLEVEL >= 4) {
    1350           0 :     err_printf("A4GaloisConj: orb=%Ps\n", orb);
    1351           0 :     err_printf("A4GaloisConj: O=%Ps \n", O);
    1352             :   }
    1353          21 :   av2 = avma;
    1354          21 :   O1 = gel(O,1); O2 = gel(O,2); O3 = gel(O,3);
    1355          35 :   for (j = 0; j < 2; j++)
    1356             :   {
    1357          35 :     pfv[O1[1]] = O2[1];
    1358          35 :     pfv[O1[2]] = O2[3+j];
    1359          35 :     pfv[O1[3]] = O2[4 - (j << 1)];
    1360          35 :     pfv[O1[4]] = O2[2+j];
    1361         119 :     for (i = 0; i < 4; i++)
    1362             :     {
    1363         105 :       ulong g = 0;
    1364         105 :       switch (i)
    1365             :       {
    1366          35 :       case 0: break;
    1367          35 :       case 1: lswap(O3[1], O3[2]); lswap(O3[3], O3[4]); break;
    1368          21 :       case 2: lswap(O3[1], O3[4]); lswap(O3[2], O3[3]); break;
    1369          14 :       case 3: lswap(O3[1], O3[2]); lswap(O3[3], O3[4]); break;
    1370             :       }
    1371         105 :       pfv[O2[1]]          = O3[1];
    1372         105 :       pfv[O2[3+j]]        = O3[4-j];
    1373         105 :       pfv[O2[4 - (j<<1)]] = O3[2 + (j<<1)];
    1374         105 :       pfv[O2[2+j]]        = O3[3-j];
    1375         105 :       pfv[O3[1]]          = O1[1];
    1376         105 :       pfv[O3[4-j]]        = O1[2];
    1377         105 :       pfv[O3[2 + (j<<1)]] = O1[3];
    1378         105 :       pfv[O3[3-j]]        = O1[4];
    1379         105 :       for (k = 1; k <= n; k++) g += mael(mt,k,pfv[k]);
    1380         105 :       if (headlongisint(g,n) && galois_test_perm(td, pfv))
    1381             :       {
    1382          21 :         avma = av;
    1383          21 :         if (DEBUGLEVEL >= 1)
    1384           0 :           err_printf("A4GaloisConj: %ld hop over %d iterations max\n",
    1385             :                      hop, 10395 + 68);
    1386          21 :         return res;
    1387             :       }
    1388          84 :       hop++; avma = av2;
    1389             :     }
    1390             :   }
    1391           0 :   avma = ltop; return gen_0; /* Fail */
    1392             : }
    1393             : 
    1394             : /* S4 */
    1395             : static void
    1396         133 : s4makelift(GEN u, struct galois_lift *gl, GEN liftpow)
    1397             : {
    1398         133 :   GEN s = automorphismlift(u, gl, NULL);
    1399             :   long i;
    1400         133 :   gel(liftpow,1) = s;
    1401        3059 :   for (i = 2; i < lg(liftpow); i++)
    1402        2926 :     gel(liftpow,i) = FpXQ_mul(gel(liftpow,i-1), s, gl->TQ, gl->Q);
    1403         133 : }
    1404             : static long
    1405        2380 : s4test(GEN u, GEN liftpow, struct galois_lift *gl, GEN phi)
    1406             : {
    1407        2380 :   pari_sp av = avma;
    1408             :   GEN res, Q, Q2;
    1409        2380 :   long bl, i, d = lg(u)-2;
    1410             :   pari_timer ti;
    1411        2380 :   if (DEBUGLEVEL >= 6) timer_start(&ti);
    1412        2380 :   if (!d) return 0;
    1413        2380 :   Q = gl->Q; Q2 = shifti(Q,-1);
    1414        2380 :   res = gel(u,2);
    1415       55734 :   for (i = 1; i < d; i++)
    1416       53354 :     if (lg(gel(liftpow,i))>2)
    1417       53354 :       res = addii(res, mulii(gmael(liftpow,i,2), gel(u,i+2)));
    1418        2380 :   res = remii(res,Q);
    1419        2380 :   if (gl->den != gen_1) res = mulii(res, gl->den);
    1420        2380 :   res = centermodii(res, Q,Q2);
    1421        2380 :   if (abscmpii(res, gl->gb->bornesol) > 0) { avma = av; return 0; }
    1422         126 :   res = scalar_ZX_shallow(gel(u,2),varn(u));
    1423        2562 :   for (i = 1; i < d ; i++)
    1424        2436 :     if (lg(gel(liftpow,i))>2)
    1425        2436 :       res = ZX_add(res, ZX_Z_mul(gel(liftpow,i), gel(u,i+2)));
    1426         126 :   res = FpX_red(res, Q);
    1427         126 :   if (gl->den != gen_1) res = FpX_Fp_mul(res, gl->den, Q);
    1428         126 :   res = FpX_center(res, Q, shifti(Q,-1));
    1429         126 :   bl = poltopermtest(res, gl, phi);
    1430         126 :   if (DEBUGLEVEL >= 6) timer_printf(&ti, "s4test()");
    1431         126 :   avma = av; return bl;
    1432             : }
    1433             : 
    1434             : static GEN
    1435         399 : aux(long a, long b, GEN T, GEN M, GEN p, GEN *pu)
    1436             : {
    1437         399 :   *pu = FpX_mul(gel(T,b), gel(T,a),p);
    1438        1197 :   return FpX_chinese_coprime(gmael(M,a,b), gmael(M,b,a),
    1439         798 :                              gel(T,b), gel(T,a), *pu, p);
    1440             : }
    1441             : 
    1442             : static GEN
    1443         133 : s4releveauto(GEN misom,GEN Tmod,GEN Tp,GEN p,long a1,long a2,long a3,long a4,long a5,long a6)
    1444             : {
    1445         133 :   pari_sp av = avma;
    1446             :   GEN u4,u5;
    1447             :   GEN pu1, pu2, pu3, pu4;
    1448         133 :   GEN u1 = aux(a1, a2, Tmod, misom, p, &pu1);
    1449         133 :   GEN u2 = aux(a3, a4, Tmod, misom, p, &pu2);
    1450         133 :   GEN u3 = aux(a5, a6, Tmod, misom, p, &pu3);
    1451         133 :   pu4 = FpX_mul(pu1,pu2,p);
    1452         133 :   u4 = FpX_chinese_coprime(u1,u2,pu1,pu2,pu4,p);
    1453         133 :   u5 = FpX_chinese_coprime(u4,u3,pu4,pu3,Tp,p);
    1454         133 :   return gerepileupto(av, u5);
    1455             : }
    1456             : static GEN
    1457        3850 : lincomb(GEN A, GEN B, GEN pauto, long j)
    1458             : {
    1459        3850 :   long k = (-j) & 3;
    1460        3850 :   if (j == k) return ZX_mul(ZX_add(A,B), gel(pauto, j+1));
    1461        1939 :   return ZX_add(ZX_mul(A, gel(pauto, j+1)), ZX_mul(B, gel(pauto, k+1)));
    1462             : }
    1463             : /* FIXME: could use the intheadlong technique */
    1464             : static GEN
    1465          21 : s4galoisgen(struct galois_lift *gl)
    1466             : {
    1467          21 :   const long n = 24;
    1468             :   struct galois_testlift gt;
    1469          21 :   pari_sp av, ltop2, ltop = avma;
    1470             :   long i, j;
    1471          21 :   GEN sigma, tau, phi, res, r1,r2,r3,r4, pj, p = gl->p, Q = gl->Q, TQ = gl->TQ;
    1472             :   GEN sg, Tp, Tmod, isom, isominv, misom, Bcoeff, pauto, liftpow, aut;
    1473             : 
    1474          21 :   res = cgetg(3, t_VEC);
    1475          21 :   r1 = cgetg(n+1, t_VECSMALL);
    1476          21 :   r2 = cgetg(n+1, t_VECSMALL);
    1477          21 :   r3 = cgetg(n+1, t_VECSMALL);
    1478          21 :   r4 = cgetg(n+1, t_VECSMALL);
    1479          21 :   gel(res,1)= mkvec4(r1,r2,r3,r4);
    1480          21 :   gel(res,2) = mkvecsmall4(2,2,3,2);
    1481          21 :   ltop2 = avma;
    1482          21 :   sg = identity_perm(6);
    1483          21 :   pj = zero_zv(6);
    1484          21 :   sigma = cgetg(n+1, t_VECSMALL);
    1485          21 :   tau = cgetg(n+1, t_VECSMALL);
    1486          21 :   phi = cgetg(n+1, t_VECSMALL);
    1487          21 :   Tp = FpX_red(gl->T,p);
    1488          21 :   Tmod = gel(FpX_factor(Tp,p), 1);
    1489          21 :   isom    = cgetg(lg(Tmod), t_VEC);
    1490          21 :   isominv = cgetg(lg(Tmod), t_VEC);
    1491          21 :   misom   = cgetg(lg(Tmod), t_MAT);
    1492          21 :   aut = galoisdolift(gl, NULL);
    1493          21 :   inittestlift(aut, Tmod, gl, &gt);
    1494          21 :   Bcoeff = gt.bezoutcoeff;
    1495          21 :   pauto = gt.pauto;
    1496         147 :   for (i = 1; i < lg(isom); i++)
    1497             :   {
    1498         126 :     gel(misom,i) = cgetg(lg(Tmod), t_COL);
    1499         126 :     gel(isom,i) = FpX_ffisom(gel(Tmod,1), gel(Tmod,i), p);
    1500         126 :     if (DEBUGLEVEL >= 6)
    1501           0 :       err_printf("S4GaloisConj: Computing isomorphisms %d:%Ps\n", i,
    1502           0 :                  gel(isom,i));
    1503         126 :     gel(isominv,i) = FpXQ_ffisom_inv(gel(isom,i), gel(Tmod,i),p);
    1504             :   }
    1505         147 :   for (i = 1; i < lg(isom); i++)
    1506         882 :     for (j = 1; j < lg(isom); j++)
    1507        1512 :       gmael(misom,i,j) = FpX_FpXQ_eval(gel(isominv,i),gel(isom,j),
    1508         756 :                                          gel(Tmod,j),p);
    1509          21 :   liftpow = cgetg(24, t_VEC);
    1510          21 :   av = avma;
    1511          35 :   for (i = 0; i < 3; i++, avma = av)
    1512             :   {
    1513             :     pari_sp av1, av2, av3;
    1514             :     GEN u, u1, u2, u3;
    1515             :     long j1, j2, j3;
    1516          35 :     if (i)
    1517             :     {
    1518          14 :       if (i == 1) { lswap(sg[2],sg[3]); }
    1519           0 :       else        { lswap(sg[1],sg[3]); }
    1520             :     }
    1521          35 :     u = s4releveauto(misom,Tmod,Tp,p,sg[1],sg[2],sg[3],sg[4],sg[5],sg[6]);
    1522          35 :     s4makelift(u, gl, liftpow);
    1523          35 :     av1 = avma;
    1524         133 :     for (j1 = 0; j1 < 4; j1++, avma = av1)
    1525             :     {
    1526         119 :       u1 = lincomb(gel(Bcoeff,sg[5]),gel(Bcoeff,sg[6]), pauto,j1);
    1527         119 :       u1 = FpX_rem(u1, TQ, Q); av2 = avma;
    1528         539 :       for (j2 = 0; j2 < 4; j2++, avma = av2)
    1529             :       {
    1530         441 :         u2 = lincomb(gel(Bcoeff,sg[3]),gel(Bcoeff,sg[4]), pauto,j2);
    1531         441 :         u2 = FpX_rem(FpX_add(u1, u2, Q), TQ,Q); av3 = avma;
    1532        2135 :         for (j3 = 0; j3 < 4; j3++, avma = av3)
    1533             :         {
    1534        1715 :           u3 = lincomb(gel(Bcoeff,sg[1]),gel(Bcoeff,sg[2]), pauto,j3);
    1535        1715 :           u3 = FpX_rem(FpX_add(u2, u3, Q), TQ,Q);
    1536        1715 :           if (DEBUGLEVEL >= 4)
    1537           0 :             err_printf("S4GaloisConj: Testing %d/3:%d/4:%d/4:%d/4:%Ps\n",
    1538             :                        i,j1,j2,j3, sg);
    1539        1715 :           if (s4test(u3, liftpow, gl, sigma))
    1540             :           {
    1541          21 :             pj[1] = j3;
    1542          21 :             pj[2] = j2;
    1543          21 :             pj[3] = j1; goto suites4;
    1544             :           }
    1545             :         }
    1546             :       }
    1547             :     }
    1548             :   }
    1549           0 :   avma = ltop; return gen_0;
    1550             : suites4:
    1551          21 :   if (DEBUGLEVEL >= 4) err_printf("S4GaloisConj: sigma=%Ps\n", sigma);
    1552          21 :   if (DEBUGLEVEL >= 4) err_printf("S4GaloisConj: pj=%Ps\n", pj);
    1553          21 :   avma = av;
    1554          42 :   for (j = 1; j <= 3; j++)
    1555             :   {
    1556             :     pari_sp av2, av3;
    1557             :     GEN u;
    1558             :     long w, l, z;
    1559          42 :     z = sg[1]; sg[1] = sg[3]; sg[3] = sg[5]; sg[5] = z;
    1560          42 :     z = sg[2]; sg[2] = sg[4]; sg[4] = sg[6]; sg[6] = z;
    1561          42 :     z = pj[1]; pj[1] = pj[2]; pj[2] = pj[3]; pj[3] = z;
    1562          98 :     for (l = 0; l < 2; l++, avma = av)
    1563             :     {
    1564          77 :       u = s4releveauto(misom,Tmod,Tp,p,sg[1],sg[3],sg[2],sg[4],sg[5],sg[6]);
    1565          77 :       s4makelift(u, gl, liftpow);
    1566          77 :       av2 = avma;
    1567         210 :       for (w = 0; w < 4; w += 2, avma = av2)
    1568             :       {
    1569             :         GEN uu;
    1570         154 :         pj[6] = (w + pj[3]) & 3;
    1571         154 :         uu = lincomb(gel(Bcoeff,sg[5]),gel(Bcoeff,sg[6]), pauto, pj[6]);
    1572         154 :         uu = FpX_rem(FpX_red(uu,Q), TQ, Q);
    1573         154 :         av3 = avma;
    1574         707 :         for (i = 0; i < 4; i++, avma = av3)
    1575             :         {
    1576             :           GEN u;
    1577         574 :           pj[4] = i;
    1578         574 :           pj[5] = (i + pj[2] - pj[1]) & 3;
    1579         574 :           if (DEBUGLEVEL >= 4)
    1580           0 :             err_printf("S4GaloisConj: Testing %d/3:%d/2:%d/2:%d/4:%Ps:%Ps\n",
    1581             :                        j-1, w >> 1, l, i, sg, pj);
    1582        1722 :           u = ZX_add(lincomb(gel(Bcoeff,sg[1]),gel(Bcoeff,sg[3]), pauto,pj[4]),
    1583        1722 :                      lincomb(gel(Bcoeff,sg[2]),gel(Bcoeff,sg[4]), pauto,pj[5]));
    1584         574 :           u = FpX_rem(FpX_add(uu,u,Q), TQ, Q);
    1585         574 :           if (s4test(u, liftpow, gl, tau)) goto suites4_2;
    1586             :         }
    1587             :       }
    1588          56 :       lswap(sg[3], sg[4]);
    1589          56 :       pj[2] = (-pj[2]) & 3;
    1590             :     }
    1591             :   }
    1592           0 :   avma = ltop; return gen_0;
    1593             : suites4_2:
    1594          21 :   avma = av;
    1595             :   {
    1596          21 :     long abc = (pj[1] + pj[2] + pj[3]) & 3;
    1597          21 :     long abcdef = ((abc + pj[4] + pj[5] - pj[6]) & 3) >> 1;
    1598             :     GEN u;
    1599             :     pari_sp av2;
    1600          21 :     u = s4releveauto(misom,Tmod,Tp,p,sg[1],sg[4],sg[2],sg[5],sg[3],sg[6]);
    1601          21 :     s4makelift(u, gl, liftpow);
    1602          21 :     av2 = avma;
    1603          91 :     for (j = 0; j < 8; j++)
    1604             :     {
    1605             :       long h, g, i;
    1606          91 :       h = j & 3;
    1607          91 :       g = (abcdef + ((j & 4) >> 1)) & 3;
    1608          91 :       i = (h + abc - g) & 3;
    1609         182 :       u = ZX_add(   lincomb(gel(Bcoeff,sg[1]), gel(Bcoeff,sg[4]), pauto, g),
    1610         182 :                     lincomb(gel(Bcoeff,sg[2]), gel(Bcoeff,sg[5]), pauto, h));
    1611          91 :       u = FpX_add(u, lincomb(gel(Bcoeff,sg[3]), gel(Bcoeff,sg[6]), pauto, i),Q);
    1612          91 :       u = FpX_rem(u, TQ, Q);
    1613          91 :       if (DEBUGLEVEL >= 4)
    1614           0 :         err_printf("S4GaloisConj: Testing %d/8 %d:%d:%d\n", j,g,h,i);
    1615          91 :       if (s4test(u, liftpow, gl, phi)) break;
    1616          70 :       avma = av2;
    1617             :     }
    1618             :   }
    1619          21 :   if (j == 8) { avma = ltop; return gen_0; }
    1620         525 :   for (i = 1; i <= n; i++)
    1621             :   {
    1622         504 :     r1[i] = sigma[tau[i]];
    1623         504 :     r2[i] = phi[sigma[tau[phi[i]]]];
    1624         504 :     r3[i] = phi[sigma[i]];
    1625         504 :     r4[i] = sigma[i];
    1626             :   }
    1627          21 :   avma = ltop2; return res;
    1628             : }
    1629             : 
    1630             : static GEN
    1631         350 : galoisfindgroups(GEN lo, GEN sg, long f)
    1632             : {
    1633         350 :   pari_sp ltop = avma;
    1634             :   long i, j, k;
    1635         350 :   GEN V = cgetg(lg(lo), t_VEC);
    1636        1337 :   for(j=1,i=1; i<lg(lo); i++)
    1637             :   {
    1638         987 :     pari_sp av = avma;
    1639         987 :     GEN loi = gel(lo,i), W = cgetg(lg(loi),t_VECSMALL);
    1640         987 :     for (k=1; k<lg(loi); k++) W[k] = loi[k] % f;
    1641         987 :     W = vecsmall_uniq(W);
    1642         987 :     if (zv_equal(W, sg)) gel(V,j++) = loi;
    1643         987 :     avma = av;
    1644             :   }
    1645         350 :   setlg(V,j); return gerepilecopy(ltop,V);
    1646             : }
    1647             : 
    1648             : static long
    1649        1790 : galoisfrobeniustest(GEN aut, struct galois_lift *gl, GEN frob)
    1650             : {
    1651        1790 :   pari_sp av = avma;
    1652        1790 :   GEN tlift = aut;
    1653             :   long res;
    1654        1790 :   if (gl->den != gen_1) tlift = FpX_Fp_mul(tlift, gl->den, gl->Q);
    1655        1790 :   tlift = FpX_center(tlift, gl->Q, shifti(gl->Q,-1));
    1656        1790 :   res = poltopermtest(tlift, gl, frob);
    1657        1790 :   avma = av; return res;
    1658             : }
    1659             : 
    1660             : static GEN
    1661         511 : galoismakepsi(long g, GEN sg, GEN pf)
    1662             : {
    1663         511 :   GEN psi=cgetg(g+1,t_VECSMALL);
    1664             :   long i;
    1665         511 :   for (i = 1; i < g; i++) psi[i] = sg[pf[i]];
    1666         511 :   psi[g] = sg[1]; return psi;
    1667             : }
    1668             : 
    1669             : static GEN
    1670        1568 : galoisfrobeniuslift(GEN T, GEN den, GEN L,  GEN Lden,
    1671             :     struct galois_frobenius *gf,  struct galois_borne *gb)
    1672             : {
    1673        1568 :   pari_sp ltop=avma, av2;
    1674             :   struct galois_testlift gt;
    1675             :   struct galois_lift gl;
    1676        1568 :   long i, j, k, n = lg(L)-1, deg = 1, g = lg(gf->Tmod)-1;
    1677        1568 :   GEN F,Fp,Fe, aut, frob, ip = utoipos(gf->p), res = cgetg(lg(L), t_VECSMALL);
    1678        1568 :   gf->psi = const_vecsmall(g,1);
    1679        1568 :   av2 = avma;
    1680        1568 :   initlift(T, den, ip, L, Lden, gb, &gl);
    1681        1568 :   if (DEBUGLEVEL >= 4)
    1682           0 :     err_printf("GaloisConj: p=%ld e=%ld deg=%ld fp=%ld\n",
    1683             :                             gf->p, gl.e, deg, gf->fp);
    1684        1568 :   aut = galoisdolift(&gl, res);
    1685        1568 :   if (!aut || galoisfrobeniustest(aut,&gl,res))
    1686             :   {
    1687         980 :     avma = av2; gf->deg = gf->fp; return res;
    1688             :   }
    1689         588 :   inittestlift(aut,gf->Tmod, &gl, &gt);
    1690         588 :   gt.C = cgetg(gf->fp+1,t_VEC);
    1691         588 :   gt.Cd= cgetg(gf->fp+1,t_VEC);
    1692        4242 :   for (i = 1; i <= gf->fp; i++) {
    1693        3654 :     gel(gt.C,i)  = zero_zv(gt.g);
    1694        3654 :     gel(gt.Cd,i) = zero_zv(gt.g);
    1695             :   }
    1696             : 
    1697         588 :   F =factoru(gf->fp);
    1698         588 :   Fp = gel(F,1);
    1699         588 :   Fe = gel(F,2);
    1700         588 :   frob = cgetg(lg(L), t_VECSMALL);
    1701        1274 :   for(k=lg(Fp)-1;k>=1;k--)
    1702             :   {
    1703         686 :     pari_sp btop=avma;
    1704         686 :     GEN psi=NULL, fres=NULL, sg = identity_perm(1);
    1705         686 :     long el=gf->fp, dg=1, dgf=1, e, pr;
    1706        1561 :     for(e=1; e<=Fe[k]; e++)
    1707             :     {
    1708             :       GEN lo, pf;
    1709             :       long l;
    1710        1092 :       dg *= Fp[k]; el /= Fp[k];
    1711        1092 :       if (DEBUGLEVEL>=4) err_printf("Trying degre %d.\n",dg);
    1712        1092 :       if (galoisfrobeniustest(gel(gt.pauto,el+1),&gl,frob))
    1713             :       {
    1714         364 :         psi = const_vecsmall(g,1);
    1715         364 :         dgf = dg; fres = gcopy(frob); continue;
    1716             :       }
    1717         728 :       lo = listznstarelts(dg, n / gf->fp);
    1718         728 :       if (e!=1) lo = galoisfindgroups(lo, sg, dgf);
    1719         728 :       if (DEBUGLEVEL>=4) err_printf("Galoisconj:Subgroups list:%Ps\n", lo);
    1720        1533 :       for (l = 1; l < lg(lo); l++)
    1721        1316 :         if (lg(gel(lo,l))>2 && frobeniusliftall(gel(lo,l), el, &pf,&gl,&gt, frob))
    1722             :         {
    1723         511 :           sg  = gcopy(gel(lo,l));
    1724         511 :           psi = galoismakepsi(g,sg,pf);
    1725         511 :           dgf = dg; fres = gcopy(frob); break;
    1726             :         }
    1727         728 :       if (l == lg(lo)) break;
    1728             :     }
    1729         686 :     if (dgf == 1) { avma = btop; continue; }
    1730         553 :     pr = deg*dgf;
    1731         553 :     if (deg == 1)
    1732             :     {
    1733         490 :       for(i=1;i<lg(res);i++) res[i]=fres[i];
    1734         490 :       for(i=1;i<lg(psi);i++) gf->psi[i]=psi[i];
    1735             :     }
    1736             :     else
    1737             :     {
    1738          63 :       GEN cp = perm_mul(res,fres);
    1739          63 :       for(i=1;i<lg(res);i++) res[i] = cp[i];
    1740          63 :       for(i=1;i<lg(psi);i++) gf->psi[i] = (dgf*gf->psi[i] + deg*psi[i]) % pr;
    1741             :     }
    1742         553 :     deg = pr; avma = btop;
    1743             :   }
    1744        4242 :   for (i = 1; i <= gf->fp; i++)
    1745       18767 :     for (j = 1; j <= gt.g; j++)
    1746       15113 :       if (mael(gt.C,i,j)) gunclone(gmael(gt.C,i,j));
    1747         588 :   if (DEBUGLEVEL>=4 && res) err_printf("Best lift: %d\n",deg);
    1748         588 :   if (deg==1) { avma = ltop; return NULL; }
    1749             :   else
    1750             :   {
    1751             :     /* Normalize result so that psi[g]=1 */
    1752         490 :     long im = Fl_inv(gf->psi[g], deg);
    1753         490 :     GEN cp = perm_pow(res, im);
    1754         490 :     for(i=1;i<lg(res);i++) res[i] = cp[i];
    1755         490 :     for(i=1;i<lg(gf->psi);i++) gf->psi[i] = Fl_mul(im, gf->psi[i], deg);
    1756         490 :     avma = av2; gf->deg = deg; return res;
    1757             :   }
    1758             : }
    1759             : 
    1760             : /* return NULL if not Galois */
    1761             : static GEN
    1762        1470 : galoisfindfrobenius(GEN T, GEN L, GEN den, struct galois_frobenius *gf,
    1763             :     struct galois_borne *gb, const struct galois_analysis *ga)
    1764             : {
    1765        1470 :   pari_sp ltop = avma, av;
    1766        1470 :   long Try = 0, n = degpol(T), deg, gmask = (ga->group&ga_ext_2)? 3: 1;
    1767        1470 :   GEN frob, Lden = makeLden(L,den,gb);
    1768             :   forprime_t S;
    1769             : 
    1770        1470 :   u_forprime_init(&S, ga->p, ULONG_MAX);
    1771        1470 :   av = avma;
    1772        1470 :   deg = gf->deg = ga->deg;
    1773        3045 :   while ((gf->p = u_forprime_next(&S)))
    1774             :   {
    1775             :     pari_sp lbot;
    1776             :     GEN Ti, Tp;
    1777             :     long nb, d;
    1778        1575 :     avma = av;
    1779        1575 :     Tp = ZX_to_Flx(T, gf->p);
    1780        1575 :     if (!Flx_is_squarefree(Tp, gf->p)) continue;
    1781        1575 :     Ti = gel(Flx_factor(Tp, gf->p), 1);
    1782        1575 :     nb = lg(Ti)-1; d = degpol(gel(Ti,1));
    1783        1575 :     if (nb > 1 && degpol(gel(Ti,nb)) != d) { avma = ltop; return NULL; }
    1784        1575 :     if (((gmask&1)==0 || d % deg) && ((gmask&2)==0 || odd(d))) continue;
    1785        1568 :     if (DEBUGLEVEL >= 1) err_printf("GaloisConj: Trying p=%ld\n", gf->p);
    1786        1568 :     FlxV_to_ZXV_inplace(Ti);
    1787        1568 :     gf->fp = d;
    1788        1568 :     gf->Tmod = Ti; lbot = avma;
    1789        1568 :     frob = galoisfrobeniuslift(T, den, L, Lden, gf, gb);
    1790        1568 :     if (frob)
    1791             :     {
    1792             :       GEN *gptr[3];
    1793        1470 :       if (gf->deg < ga->mindeg)
    1794             :       {
    1795           0 :         if (DEBUGLEVEL >= 4)
    1796           0 :           err_printf("GaloisConj: lift degree too small %ld < %ld\n",
    1797             :                      gf->deg, ga->mindeg);
    1798           0 :         continue;
    1799             :       }
    1800        1470 :       gf->Tmod = gcopy(Ti);
    1801        1470 :       gptr[0]=&gf->Tmod; gptr[1]=&gf->psi; gptr[2]=&frob;
    1802        1470 :       gerepilemanysp(ltop,lbot,gptr,3); return frob;
    1803             :     }
    1804          98 :     if ((ga->group&ga_all_normal) && d % deg == 0) gmask &= ~1;
    1805             :     /* The first prime degree is always divisible by deg, so we don't
    1806             :      * have to worry about ext_2 being used before regular supersolvable*/
    1807          98 :     if (!gmask) { avma = ltop; return NULL; }
    1808          98 :     if ((ga->group&ga_non_wss) && ++Try > ((3*n)>>1))
    1809             :     {
    1810           0 :       pari_warn(warner,"Galois group probably not weakly super solvable");
    1811           0 :       return NULL;
    1812             :     }
    1813             :   }
    1814           0 :   if (!gf->p) pari_err_OVERFLOW("galoisfindfrobenius [ran out of primes]");
    1815           0 :   return NULL;
    1816             : }
    1817             : 
    1818             : /* compute g such that tau(Pmod[#])= tau(Pmod[g]) */
    1819             : 
    1820             : static long
    1821        1183 : get_image(GEN tau, GEN P, GEN Pmod, GEN p)
    1822             : {
    1823        1183 :   pari_sp av = avma;
    1824        1183 :   long g, gp = lg(Pmod)-1;
    1825        1183 :   tau = RgX_to_FpX(tau, p);
    1826        1183 :   tau = FpX_FpXQ_eval(gel(Pmod, gp), tau, P, p);
    1827        1183 :   tau = FpX_normalize(FpX_gcd(P, tau, p), p);
    1828        2506 :   for (g = 1; g <= gp; g++)
    1829        2506 :     if (ZX_equal(tau, gel(Pmod,g))) { avma = av; return g; }
    1830           0 :   avma = av; return 0;
    1831             : }
    1832             : 
    1833             : static GEN
    1834             : galoisgen(GEN T, GEN L, GEN M, GEN den, struct galois_borne *gb,
    1835             :           const struct galois_analysis *ga);
    1836             : static GEN
    1837         945 : galoisgenfixedfield(GEN Tp, GEN Pmod, GEN V, GEN ip, struct galois_borne *gb)
    1838             : {
    1839             :   GEN  P, PL, Pden, PM, Pp;
    1840             :   GEN  tau, PG, Pg;
    1841             :   long g, lP;
    1842         945 :   long x=varn(Tp);
    1843         945 :   P=gel(V,3);
    1844         945 :   PL=gel(V,2);
    1845         945 :   Pp = FpX_red(P,ip);
    1846         945 :   if (DEBUGLEVEL>=6)
    1847           0 :     err_printf("GaloisConj: Fixed field %Ps\n",P);
    1848         945 :   if (degpol(P)==2)
    1849             :   {
    1850         651 :     PG=cgetg(3,t_VEC);
    1851         651 :     gel(PG,1) = mkvec( mkvecsmall2(2,1) );
    1852         651 :     gel(PG,2) = mkvecsmall(2);
    1853         651 :     tau = deg1pol_shallow(gen_m1, negi(gel(P,3)), x);
    1854         651 :     g = get_image(tau, Pp, Pmod, ip);
    1855         651 :     if (!g) return NULL;
    1856         651 :     Pg = mkvecsmall(g);
    1857             :   }
    1858             :   else
    1859             :   {
    1860             :     struct galois_analysis Pga;
    1861             :     struct galois_borne Pgb;
    1862             :     GEN mod, mod2;
    1863             :     long j;
    1864         308 :     if (!galoisanalysis(P, &Pga, 0)) return NULL;
    1865         280 :     Pgb.l = gb->l;
    1866         280 :     Pden = galoisborne(P, NULL, &Pgb, degpol(P));
    1867             : 
    1868         280 :     if (Pgb.valabs > gb->valabs)
    1869             :     {
    1870          42 :       if (DEBUGLEVEL>=4)
    1871           0 :         err_printf("GaloisConj: increase prec of p-adic roots of %ld.\n"
    1872           0 :             ,Pgb.valabs-gb->valabs);
    1873          42 :       PL = ZpX_liftroots(P,PL,gb->l,Pgb.valabs);
    1874             :     }
    1875         238 :     else if (Pgb.valabs < gb->valabs)
    1876         238 :       PL = FpC_red(PL, Pgb.ladicabs);
    1877         280 :     PM = FpV_invVandermonde(PL, Pden, Pgb.ladicabs);
    1878         280 :     PG = galoisgen(P, PL, PM, Pden, &Pgb, &Pga);
    1879         280 :     if (PG == gen_0) return NULL;
    1880         280 :     lP = lg(gel(PG,1));
    1881         280 :     mod = Pgb.ladicabs; mod2 = shifti(mod, -1);
    1882         280 :     Pg = cgetg(lP, t_VECSMALL);
    1883         812 :     for (j = 1; j < lP; j++)
    1884             :     {
    1885         532 :       pari_sp btop=avma;
    1886         532 :       tau = permtopol(gmael(PG,1,j), PL, PM, Pden, mod, mod2, x);
    1887         532 :       g = get_image(tau, Pp, Pmod, ip);
    1888         532 :       if (!g) return NULL;
    1889         532 :       Pg[j] = g;
    1890         532 :       avma = btop;
    1891             :     }
    1892             :   }
    1893         931 :   return mkvec2(PG,Pg);
    1894             : }
    1895             : 
    1896             : /* Let sigma^m=1,  tau*sigma*tau^-1=sigma^s.
    1897             :  * Compute n so that (sigma*tau)^e = sigma^n*tau^e
    1898             :  * We have n = sum_{k=0}^{e-1} s^k mod m.
    1899             :  * so n*(1-s) = 1-s^e mod m
    1900             :  * Unfortunately (1-s) might not invertible mod m.
    1901             :  */
    1902             : 
    1903             : static long
    1904        2772 : stpow(long s, long e, long m)
    1905             : {
    1906             :   long i;
    1907        2772 :   long n = 1;
    1908        4480 :   for (i = 1; i < e; i++)
    1909        1708 :     n = (1 + n * s) % m;
    1910        2772 :   return n;
    1911             : }
    1912             : 
    1913             : static GEN
    1914        1183 : wpow(long s, long m, long e, long n)
    1915             : {
    1916        1183 :   GEN   w = cgetg(n+1,t_VECSMALL);
    1917        1183 :   long si = s;
    1918             :   long i;
    1919        1183 :   w[1] = 1;
    1920        1183 :   for(i=2; i<=n; i++) w[i] = w[i-1]*e;
    1921        2569 :   for(i=n; i>=1; i--)
    1922             :   {
    1923        1386 :     si = Fl_powu(si,e,m);
    1924        1386 :     w[i] = Fl_mul(s-1, stpow(si, w[i], m), m);
    1925             :   }
    1926        1183 :   return w;
    1927             : }
    1928             : 
    1929             : static GEN
    1930        1183 : galoisgenliftauto(GEN O, GEN gj, long s, long n, struct galois_test *td)
    1931             : {
    1932        1183 :   pari_sp av = avma;
    1933             :   long sr, k;
    1934        1183 :   long deg = lg(gel(O,1))-1;
    1935        1183 :   GEN  X  = cgetg(lg(O), t_VECSMALL);
    1936        1183 :   GEN  oX = cgetg(lg(O), t_VECSMALL);
    1937        1183 :   GEN  B  = perm_cycles(gj);
    1938        1183 :   long oj = lg(gel(B,1)) - 1;
    1939        1183 :   GEN  F  = factoru(oj);
    1940        1183 :   GEN  Fp = gel(F,1);
    1941        1183 :   GEN  Fe = gel(F,2);
    1942        1183 :   GEN  pf = identity_perm(n);
    1943        1183 :   if (DEBUGLEVEL >= 6)
    1944           0 :     err_printf("GaloisConj: %Ps of relative order %d\n", gj, oj);
    1945        2366 :   for (k=lg(Fp)-1; k>=1; k--)
    1946             :   {
    1947        1183 :     long f, dg = 1, el = oj, osel = 1, a = 0;
    1948        1183 :     long p  = Fp[k], e  = Fe[k], op = oj / upowuu(p,e);
    1949             :     long i;
    1950        1183 :     GEN  pf1 = NULL, w, wg, Be = cgetg(e+1,t_VEC);
    1951        1183 :     gel(Be,e) = cyc_pow(B, op);
    1952        1183 :     for(i=e-1; i>=1; i--) gel(Be,i) = cyc_pow(gel(Be,i+1), p);
    1953        1183 :     w = wpow(Fl_powu(s,op,deg),deg,p,e);
    1954        1183 :     wg = cgetg(e+2,t_VECSMALL);
    1955        1183 :     wg[e+1] = deg;
    1956        1183 :     for (i=e; i>=1; i--) wg[i] = ugcd(wg[i+1], w[i]);
    1957        1183 :     for (i=1; i<lg(O); i++) oX[i] = 0;
    1958        2569 :     for (f=1; f<=e; f++)
    1959             :     {
    1960             :       long sel, t;
    1961        1386 :       GEN Bel = gel(Be,f);
    1962        1386 :       dg *= p; el /= p;
    1963        1386 :       sel = Fl_powu(s,el,deg);
    1964        1386 :       if (DEBUGLEVEL >= 6) err_printf("GaloisConj: B=%Ps\n", Bel);
    1965        1386 :       sr  = cgcd(stpow(sel,p,deg),deg);
    1966        1386 :       if (DEBUGLEVEL >= 6)
    1967           0 :         err_printf("GaloisConj: exp %d: s=%ld [%ld] a=%ld w=%ld wg=%ld sr=%ld\n",
    1968           0 :             dg, sel, deg, a, w[f], wg[f+1], sr);
    1969        1561 :       for (t = 0; t < sr; t++)
    1970        1561 :         if ((a+t*w[f])%wg[f+1]==0)
    1971             :         {
    1972             :           long i, j, k, st;
    1973        1533 :           for (i = 1; i < lg(X); i++) X[i] = 0;
    1974        6748 :           for (i = 0; i < lg(X)-1; i+=dg)
    1975       11536 :             for (j = 1, k = p, st = t; k <= dg; j++, k += p)
    1976             :             {
    1977        6321 :               X[k+i] = (oX[j+i] + st)%deg;
    1978        6321 :               st = (t + st*osel)%deg;
    1979             :             }
    1980        1533 :           pf1 = testpermutation(O, Bel, X, sel, p, sr, td);
    1981        1533 :           if (pf1) break;
    1982             :         }
    1983        1386 :       if (!pf1) return NULL;
    1984        1386 :       for (i=1; i<lg(O); i++) oX[i] = X[i];
    1985        1386 :       osel = sel; a = (a+t*w[f])%deg;
    1986             :     }
    1987        1183 :     pf = perm_mul(pf, perm_pow(pf1, el));
    1988             :   }
    1989        1183 :   return gerepileuptoleaf(av, pf);
    1990             : }
    1991             : 
    1992             : static GEN
    1993        1512 : galoisgen(GEN T, GEN L, GEN M, GEN den, struct galois_borne *gb,
    1994             :           const struct galois_analysis *ga)
    1995             : {
    1996             :   struct galois_test td;
    1997             :   struct galois_frobenius gf;
    1998        1512 :   pari_sp ltop = avma;
    1999        1512 :   long p, deg, x, j, n = degpol(T), lP;
    2000             :   GEN sigma, Tmod, res, res1, ip, frob, O, PG, PG1, PG2, Pg;
    2001             : 
    2002        1512 :   if (!ga->deg) return gen_0;
    2003        1512 :   x = varn(T);
    2004        1512 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: denominator:%Ps\n", den);
    2005        1512 :   if (n == 12 && ga->ord==3 && !ga->p4)
    2006             :   { /* A4 is very probable: test it first */
    2007          21 :     pari_sp av = avma;
    2008          21 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Testing A4 first\n");
    2009          21 :     inittest(L, M, gb->bornesol, gb->ladicsol, &td);
    2010          21 :     PG = a4galoisgen(&td);
    2011          21 :     freetest(&td);
    2012          21 :     if (PG != gen_0) return gerepileupto(ltop, PG);
    2013           0 :     avma = av;
    2014             :   }
    2015        1491 :   if (n == 24 && ga->ord==3)
    2016             :   { /* S4 is very probable: test it first */
    2017          21 :     pari_sp av = avma;
    2018             :     struct galois_lift gl;
    2019          21 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Testing S4 first\n");
    2020          21 :     initlift(T, den, stoi(ga->p4), L, makeLden(L,den,gb), gb, &gl);
    2021          21 :     PG = s4galoisgen(&gl);
    2022          21 :     if (PG != gen_0) return gerepileupto(ltop, PG);
    2023           0 :     avma = av;
    2024             :   }
    2025        1470 :   frob = galoisfindfrobenius(T, L, den, &gf, gb, ga);
    2026        1470 :   if (!frob) { avma=ltop; return gen_0; }
    2027        1470 :   p = gf.p; ip = utoipos(p);
    2028        1470 :   Tmod = gf.Tmod;
    2029        1470 :   O = perm_cycles(frob);
    2030        1470 :   deg = lg(gel(O,1))-1;
    2031        1470 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: Orbit:%Ps\n", O);
    2032        1470 :   if (deg == n)        /* cyclic */
    2033         525 :     return gerepilecopy(ltop, mkvec2(mkvec(frob), mkvecsmall(deg)));
    2034         945 :   sigma = permtopol(frob, L, M, den, gb->ladicabs, shifti(gb->ladicabs,-1), x);
    2035         945 :   if (DEBUGLEVEL >= 9) err_printf("GaloisConj: Frobenius:%Ps\n", sigma);
    2036             :   {
    2037         945 :     pari_sp btop=avma;
    2038             :     GEN V, Tp, Sp, Pmod;
    2039         945 :     GEN OL = fixedfieldorbits(O,L);
    2040         945 :     V  = fixedfieldsympol(OL, gb->ladicabs, gb->l, ip, x);
    2041         945 :     Tp = FpX_red(T,ip);
    2042         945 :     Sp = sympol_aut_evalmod(gel(V,1),deg,sigma,Tp,ip);
    2043         945 :     Pmod = fixedfieldfactmod(Sp,ip,Tmod);
    2044         945 :     PG = galoisgenfixedfield(Tp, Pmod, V, ip, gb);
    2045         945 :     if (PG == NULL) { avma = ltop; return gen_0; }
    2046         931 :     if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Back to Earth:%Ps\n", PG);
    2047         931 :     PG=gerepilecopy(btop, PG);
    2048             :   }
    2049         931 :   inittest(L, M, gb->bornesol, gb->ladicsol, &td);
    2050         931 :   PG1 = gmael(PG, 1, 1); lP = lg(PG1);
    2051         931 :   PG2 = gmael(PG, 1, 2);
    2052         931 :   Pg = gel(PG, 2);
    2053         931 :   res = cgetg(3, t_VEC);
    2054         931 :   gel(res,1) = res1 = cgetg(lP + 1, t_VEC);
    2055         931 :   gel(res,2) = vecsmall_prepend(PG2, deg);
    2056         931 :   gel(res1, 1) = vecsmall_copy(frob);
    2057        2114 :   for (j = 1; j < lP; j++)
    2058             :   {
    2059        1183 :     GEN pf = galoisgenliftauto(O, gel(PG1, j), gf.psi[Pg[j]], n, &td);
    2060        1183 :     if (!pf) { freetest(&td); avma = ltop; return gen_0; }
    2061        1183 :     gel(res1, j+1) = pf;
    2062             :   }
    2063         931 :   if (DEBUGLEVEL >= 4) err_printf("GaloisConj: Fini!\n");
    2064         931 :   freetest(&td);
    2065         931 :   return gerepileupto(ltop, res);
    2066             : }
    2067             : 
    2068             : /* T = polcyclo(N) */
    2069             : static GEN
    2070          56 : conjcyclo(GEN T, long N)
    2071             : {
    2072          56 :   pari_sp av = avma;
    2073          56 :   long i, k = 1, d = eulerphiu(N), v = varn(T);
    2074          56 :   GEN L = cgetg(d+1,t_COL);
    2075         518 :   for (i=1; i<=N; i++)
    2076         462 :     if (ugcd(i, N)==1)
    2077             :     {
    2078         238 :       GEN s = pol_xn(i, v);
    2079         238 :       if (i >= d) s = ZX_rem(s, T);
    2080         238 :       gel(L,k++) = s;
    2081             :     }
    2082          56 :   return gerepileupto(av, gen_sort(L, (void*)&gcmp, &gen_cmp_RgX));
    2083             : }
    2084             : 
    2085             : /* T: polynomial or nf, den multiple of common denominator of solutions or
    2086             :  * NULL (unknown). If T is nf, and den unknown, use den = denom(nf.zk) */
    2087             : static GEN
    2088        2632 : galoisconj4_main(GEN T, GEN den, long flag)
    2089             : {
    2090        2632 :   pari_sp ltop = avma;
    2091             :   GEN nf, G, L, M, aut;
    2092             :   struct galois_analysis ga;
    2093             :   struct galois_borne gb;
    2094             :   long n;
    2095             :   pari_timer ti;
    2096             : 
    2097        2632 :   T = get_nfpol(T, &nf);
    2098        2632 :   n = poliscyclo(T);
    2099        2632 :   if (n) return flag? galoiscyclo(n, varn(T)): conjcyclo(T, n);
    2100        2422 :   n = degpol(T);
    2101        2422 :   if (nf)
    2102        1841 :   { if (!den) den = Q_denom(nf_get_zk(nf)); }
    2103             :   else
    2104             :   {
    2105         581 :     if (n <= 0) pari_err_IRREDPOL("galoisinit",T);
    2106         581 :     RgX_check_ZX(T, "galoisinit");
    2107         581 :     if (!ZX_is_squarefree(T))
    2108           7 :       pari_err_DOMAIN("galoisinit","issquarefree(pol)","=",gen_0,T);
    2109         574 :     if (!gequal1(gel(T,n+2))) pari_err_IMPL("galoisinit(non-monic)");
    2110             :   }
    2111        2408 :   if (n == 1)
    2112             :   {
    2113           7 :     if (!flag) { G = cgetg(2, t_COL); gel(G,1) = pol_x(varn(T)); return G;}
    2114           7 :     ga.l = 3;
    2115           7 :     ga.deg = 1;
    2116           7 :     den = gen_1;
    2117             :   }
    2118        2401 :   else if (!galoisanalysis(T, &ga, 1)) { avma = ltop; return utoipos(ga.p); }
    2119             : 
    2120        1239 :   if (den)
    2121             :   {
    2122         777 :     if (typ(den) != t_INT) pari_err_TYPE("galoisconj4 [2nd arg integer]", den);
    2123         777 :     den = absi(den);
    2124             :   }
    2125        1239 :   gb.l = utoipos(ga.l);
    2126        1239 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    2127        1239 :   den = galoisborne(T, den, &gb, degpol(T));
    2128        1239 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "galoisborne()");
    2129        1239 :   L = ZpX_roots(T, gb.l, gb.valabs);
    2130        1239 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "ZpX_roots");
    2131        1239 :   M = FpV_invVandermonde(L, den, gb.ladicabs);
    2132        1239 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "FpV_invVandermonde()");
    2133        1239 :   if (n == 1)
    2134             :   {
    2135           7 :     G = cgetg(3, t_VEC);
    2136           7 :     gel(G,1) = cgetg(1, t_VEC);
    2137           7 :     gel(G,2) = cgetg(1, t_VECSMALL);
    2138             :   }
    2139             :   else
    2140        1232 :     G = galoisgen(T, L, M, den, &gb, &ga);
    2141        1239 :   if (DEBUGLEVEL >= 6) err_printf("GaloisConj: %Ps\n", G);
    2142        1239 :   if (G == gen_0) { avma = ltop; return gen_0; }
    2143        1225 :   if (DEBUGLEVEL >= 1) timer_start(&ti);
    2144        1225 :   if (flag)
    2145             :   {
    2146         784 :     GEN grp = cgetg(9, t_VEC);
    2147         784 :     gel(grp,1) = ZX_copy(T);
    2148         784 :     gel(grp,2) = mkvec3(utoipos(ga.l), utoipos(gb.valabs), icopy(gb.ladicabs));
    2149         784 :     gel(grp,3) = ZC_copy(L);
    2150         784 :     gel(grp,4) = ZM_copy(M);
    2151         784 :     gel(grp,5) = icopy(den);
    2152         784 :     gel(grp,6) = group_elts(G,n);
    2153         784 :     gel(grp,7) = gcopy(gel(G,1));
    2154         784 :     gel(grp,8) = gcopy(gel(G,2)); return gerepileupto(ltop, grp);
    2155             :   }
    2156         441 :   aut = galoisgrouptopol(group_elts(G, n),L,M,den,gb.ladicsol, varn(T));
    2157         441 :   if (DEBUGLEVEL >= 1) timer_printf(&ti, "Computation of polynomials");
    2158         441 :   return gerepileupto(ltop, gen_sort(aut, (void*)&gcmp, &gen_cmp_RgX));
    2159             : }
    2160             : 
    2161             : /* Heuristic computation of #Aut(T), pinit = first prime to be tested */
    2162             : long
    2163         637 : numberofconjugates(GEN T, long pinit)
    2164             : {
    2165         637 :   pari_sp av = avma;
    2166         637 :   long c, nbtest, nbmax, n = degpol(T);
    2167             :   ulong p;
    2168             :   forprime_t S;
    2169             : 
    2170         637 :   if (n == 1) return 1;
    2171         637 :   nbmax = (n < 10)? 20: (n<<1) + 1;
    2172         637 :   nbtest = 0;
    2173             : #if 0
    2174             :   c = ZX_sturm(T); c = ugcd(c, n-c); /* too costly: finite primes are cheaper */
    2175             : #else
    2176         637 :   c = n;
    2177             : #endif
    2178         637 :   u_forprime_init(&S, pinit, ULONG_MAX);
    2179         637 :   while((p = u_forprime_next(&S)))
    2180             :   {
    2181        5326 :     GEN L, Tp = ZX_to_Flx(T,p);
    2182             :     long i, nb;
    2183        5326 :     if (!Flx_is_squarefree(Tp, p)) continue;
    2184             :     /* unramified */
    2185        4300 :     nbtest++;
    2186        4300 :     L = Flx_nbfact_by_degree(Tp, &nb, p); /* L[i] = #factors of degree i */
    2187        4300 :     if (L[n/nb] == nb) {
    2188        3538 :       if (c == n && nbtest > 10) break; /* probably Galois */
    2189             :     }
    2190             :     else
    2191             :     {
    2192        1399 :       c = ugcd(c, L[1]);
    2193        9148 :       for (i = 2; i <= n; i++)
    2194        8217 :         if (L[i]) { c = ugcd(c, L[i]*i); if (c == 1) break; }
    2195        1399 :       if (c == 1) break;
    2196             :     }
    2197        3817 :     if (nbtest == nbmax) break;
    2198        3663 :     if (DEBUGLEVEL >= 6)
    2199           0 :       err_printf("NumberOfConjugates [%ld]:c=%ld,p=%ld\n", nbtest,c,p);
    2200        3663 :     avma = av;
    2201             :   }
    2202         637 :   if (DEBUGLEVEL >= 2) err_printf("NumberOfConjugates:c=%ld,p=%ld\n", c, p);
    2203         637 :   avma = av; return c;
    2204             : }
    2205             : static GEN
    2206           0 : galoisconj4(GEN nf, GEN d)
    2207             : {
    2208           0 :   pari_sp av = avma;
    2209             :   GEN G, T;
    2210           0 :   G = galoisconj4_main(nf, d, 0);
    2211           0 :   if (typ(G) != t_INT) return G; /* Success */
    2212           0 :   avma = av; T = get_nfpol(nf, &nf);
    2213           0 :   G = cgetg(2, t_COL); gel(G,1) = pol_x(varn(T)); return G; /* Fail */
    2214             : 
    2215             : }
    2216             : 
    2217             : /* d multiplicative bound for the automorphism's denominators */
    2218             : GEN
    2219        2037 : galoisconj(GEN nf, GEN d)
    2220             : {
    2221        2037 :   pari_sp av = avma;
    2222        2037 :   GEN G, NF, T = get_nfpol(nf,&NF);
    2223        2037 :   if (degpol(T) == 2)
    2224             :   { /* fast shortcut */
    2225         910 :     GEN a = gel(T,4), b = gel(T,3);
    2226         910 :     long v = varn(T);
    2227         910 :     RgX_check_ZX(T, "galoisconj");
    2228         910 :     if (!gequal1(a)) pari_err_IMPL("galoisconj(non-monic)");
    2229         910 :     b = negi(b);
    2230         910 :     G = cgetg(3, t_COL);
    2231         910 :     gel(G,1) = pol_x(v);
    2232         910 :     gel(G,2) = deg1pol(gen_m1, b, v); return G;
    2233             :   }
    2234        1127 :   G = galoisconj4_main(nf, d, 0);
    2235        1127 :   if (typ(G) != t_INT) return G; /* Success */
    2236         630 :   avma = av; return galoisconj1(nf);
    2237             : }
    2238             : 
    2239             : /* FIXME: obsolete, use galoisconj(nf, d) directly */
    2240             : GEN
    2241          42 : galoisconj0(GEN nf, long flag, GEN d, long prec)
    2242             : {
    2243             :   (void)prec;
    2244          42 :   switch(flag) {
    2245             :     case 2:
    2246          35 :     case 0: return galoisconj(nf, d);
    2247           7 :     case 1: return galoisconj1(nf);
    2248           0 :     case 4: return galoisconj4(nf, d);
    2249             :   }
    2250           0 :   pari_err_FLAG("nfgaloisconj");
    2251           0 :   return NULL; /*not reached*/
    2252             : }
    2253             : 
    2254             : /******************************************************************************/
    2255             : /* Galois theory related algorithms                                           */
    2256             : /******************************************************************************/
    2257             : GEN
    2258        2429 : checkgal(GEN gal)
    2259             : {
    2260        2429 :   if (typ(gal) == t_POL) pari_err_TYPE("checkgal [apply galoisinit first]",gal);
    2261        2429 :   if (typ(gal) != t_VEC || lg(gal) != 9) pari_err_TYPE("checkgal",gal);
    2262        2422 :   return gal;
    2263             : }
    2264             : 
    2265             : GEN
    2266        1505 : galoisinit(GEN nf, GEN den)
    2267             : {
    2268        1505 :   GEN G = galoisconj4_main(nf, den, 1);
    2269        1491 :   return (typ(G) == t_INT)? gen_0: G;
    2270             : }
    2271             : 
    2272             : static GEN
    2273        1505 : galoispermtopol_i(GEN gal, GEN perm, GEN mod, GEN mod2)
    2274             : {
    2275        1505 :   switch (typ(perm))
    2276             :   {
    2277             :     case t_VECSMALL:
    2278        1267 :       return permtopol(perm, gal_get_roots(gal), gal_get_invvdm(gal),
    2279             :                              gal_get_den(gal), mod, mod2,
    2280        1267 :                              varn(gal_get_pol(gal)));
    2281             :     case t_VEC: case t_COL: case t_MAT:
    2282             :     {
    2283             :       long i, lv;
    2284         238 :       GEN v = cgetg_copy(perm, &lv);
    2285         238 :       if (DEBUGLEVEL>=4) err_printf("GaloisPermToPol:");
    2286         721 :       for (i = 1; i < lv; i++)
    2287             :       {
    2288         483 :         gel(v,i) = galoispermtopol_i(gal, gel(perm,i), mod, mod2);
    2289         483 :         if (DEBUGLEVEL>=4) err_printf("%ld ",i);
    2290             :       }
    2291         238 :       if (DEBUGLEVEL>=4) err_printf("\n");
    2292         238 :       return v;
    2293             :     }
    2294             :   }
    2295           0 :   pari_err_TYPE("galoispermtopol", perm);
    2296           0 :   return NULL; /* not reached */
    2297             : }
    2298             : 
    2299             : GEN
    2300        1022 : galoispermtopol(GEN gal, GEN perm)
    2301             : {
    2302        1022 :   pari_sp av = avma;
    2303             :   GEN mod, mod2;
    2304        1022 :   gal = checkgal(gal);
    2305        1022 :   mod = gal_get_mod(gal);
    2306        1022 :   mod2 = shifti(mod,-1);
    2307        1022 :   return gerepilecopy(av, galoispermtopol_i(gal, perm, mod, mod2));
    2308             : }
    2309             : 
    2310             : GEN
    2311          63 : galoiscosets(GEN O, GEN perm)
    2312             : {
    2313          63 :   long i, j, k, u, f, l = lg(O);
    2314          63 :   GEN RC, C = cgetg(l,t_VECSMALL), o = gel(O,1);
    2315          63 :   pari_sp av = avma;
    2316          63 :   f = lg(o); u = o[1]; RC = zero_zv(lg(perm)-1);
    2317         280 :   for(i=1,j=1; j<l; i++)
    2318             :   {
    2319         217 :     GEN p = gel(perm,i);
    2320         217 :     if (RC[ p[u] ]) continue;
    2321         133 :     for(k=1; k<f; k++) RC[ p[ o[k] ] ] = 1;
    2322         133 :     C[j++] = i;
    2323             :   }
    2324          63 :   avma = av; return C;
    2325             : }
    2326             : 
    2327             : static GEN
    2328          63 : fixedfieldfactor(GEN L, GEN O, GEN perm, GEN M, GEN den, GEN mod, GEN mod2,
    2329             :                  long x,long y)
    2330             : {
    2331          63 :   pari_sp ltop = avma;
    2332          63 :   long i, j, k, l = lg(O), lo = lg(gel(O,1));
    2333          63 :   GEN V, res, cosets = galoiscosets(O,perm), F = cgetg(lo+1,t_COL);
    2334             : 
    2335          63 :   gel(F, lo) = gen_1;
    2336          63 :   if (DEBUGLEVEL>=4) err_printf("GaloisFixedField:cosets=%Ps \n",cosets);
    2337          63 :   if (DEBUGLEVEL>=6) err_printf("GaloisFixedField:den=%Ps mod=%Ps \n",den,mod);
    2338          63 :   V = cgetg(l,t_COL); res = cgetg(l,t_VEC);
    2339         196 :   for (i = 1; i < l; i++)
    2340             :   {
    2341         133 :     pari_sp av = avma;
    2342         133 :     GEN G = cgetg(l,t_VEC), Lp = vecpermute(L, gel(perm, cosets[i]));
    2343         448 :     for (k = 1; k < l; k++)
    2344         315 :       gel(G,k) = FpV_roots_to_pol(vecpermute(Lp, gel(O,k)), mod, x);
    2345         483 :     for (j = 1; j < lo; j++)
    2346             :     {
    2347         350 :       for(k = 1; k < l; k++) gel(V,k) = gmael(G,k,j+1);
    2348         350 :       gel(F,j) = vectopol(V, M, den, mod, mod2, y);
    2349             :     }
    2350         133 :     gel(res,i) = gerepileupto(av,gtopolyrev(F,x));
    2351             :   }
    2352          63 :   return gerepileupto(ltop,res);
    2353             : }
    2354             : 
    2355             : static void
    2356        1036 : chk_perm(GEN perm, long n)
    2357             : {
    2358        1036 :   if (typ(perm) != t_VECSMALL || lg(perm)!=n+1)
    2359           0 :     pari_err_TYPE("galoisfixedfield", perm);
    2360        1036 : }
    2361             : 
    2362             : static int
    2363        1323 : is_group(GEN g)
    2364             : {
    2365        3962 :   return typ(g)==t_VEC && lg(g)==3 && typ(gel(g,1))==t_VEC
    2366        1701 :       && typ(gel(g,2))==t_VECSMALL;
    2367             : }
    2368             : 
    2369             : GEN
    2370         791 : galoisfixedfield(GEN gal, GEN perm, long flag, long y)
    2371             : {
    2372         791 :   pari_sp ltop = avma;
    2373             :   GEN T, L, P, S, PL, O, res, mod, mod2;
    2374             :   long vT, n, i;
    2375         791 :   if (flag<0 || flag>2) pari_err_FLAG("galoisfixedfield");
    2376         791 :   gal = checkgal(gal); T = gal_get_pol(gal);
    2377         791 :   vT = varn(T);
    2378         791 :   L = gal_get_roots(gal); n = lg(L)-1;
    2379         791 :   mod = gal_get_mod(gal);
    2380         791 :   if (typ(perm) == t_VEC)
    2381             :   {
    2382         546 :     if (is_group(perm)) perm = gel(perm, 1);
    2383         546 :     for (i = 1; i < lg(perm); i++) chk_perm(gel(perm,i), n);
    2384         546 :     O = vecperm_orbits(perm, n);
    2385             :   }
    2386             :   else
    2387             :   {
    2388         245 :     chk_perm(perm, n);
    2389         245 :     O = perm_cycles(perm);
    2390             :   }
    2391             : 
    2392             :   {
    2393         791 :     GEN OL= fixedfieldorbits(O,L);
    2394         791 :     GEN V = fixedfieldsympol(OL, mod, gal_get_p(gal), NULL, vT);
    2395         791 :     PL= gel(V,2);
    2396         791 :     P = gel(V,3);
    2397             :   }
    2398         791 :   if (flag==1) return gerepileupto(ltop,P);
    2399         560 :   mod2 = shifti(mod,-1);
    2400         560 :   S = fixedfieldinclusion(O, PL);
    2401         560 :   S = vectopol(S, gal_get_invvdm(gal), gal_get_den(gal), mod, mod2, vT);
    2402         560 :   if (flag==0)
    2403         497 :     res = cgetg(3, t_VEC);
    2404             :   else
    2405             :   {
    2406             :     GEN PM, Pden;
    2407             :     struct galois_borne Pgb;
    2408          63 :     long val = itos(gal_get_e(gal));
    2409          63 :     Pgb.l = gal_get_p(gal);
    2410          63 :     Pden = galoisborne(P, NULL, &Pgb, degpol(T)/degpol(P));
    2411          63 :     if (Pgb.valabs > val)
    2412             :     {
    2413          12 :       if (DEBUGLEVEL>=4)
    2414           0 :         err_printf("GaloisConj: increase p-adic prec by %ld.\n", Pgb.valabs-val);
    2415          12 :       PL = ZpX_liftroots(P, PL, Pgb.l, Pgb.valabs);
    2416          12 :       L  = ZpX_liftroots(T, L, Pgb.l, Pgb.valabs);
    2417          12 :       mod = Pgb.ladicabs; mod2 = shifti(mod,-1);
    2418             :     }
    2419          63 :     PM = FpV_invVandermonde(PL, Pden, mod);
    2420          63 :     if (y < 0) y = 1;
    2421          63 :     if (varncmp(y, vT) <= 0)
    2422           0 :       pari_err_PRIORITY("galoisfixedfield", T, "<=", y);
    2423          63 :     res = cgetg(4, t_VEC);
    2424          63 :     gel(res,3) = fixedfieldfactor(L,O,gal_get_group(gal), PM,Pden,mod,mod2,vT,y);
    2425             :   }
    2426         560 :   gel(res,1) = gcopy(P);
    2427         560 :   gel(res,2) = gmodulo(S, T);
    2428         560 :   return gerepileupto(ltop, res);
    2429             : }
    2430             : 
    2431             : /* gal a galois group output the underlying wss group */
    2432             : GEN
    2433         756 : galois_group(GEN gal) { return mkvec2(gal_get_gen(gal), gal_get_orders(gal)); }
    2434             : 
    2435             : GEN
    2436         777 : checkgroup(GEN g, GEN *S)
    2437             : {
    2438         777 :   if (is_group(g)) { *S = NULL; return g; }
    2439         420 :   g  = checkgal(g);
    2440         413 :   *S = gal_get_group(g); return galois_group(g);
    2441             : }
    2442             : 
    2443             : GEN
    2444         168 : galoisisabelian(GEN gal, long flag)
    2445             : {
    2446         168 :   pari_sp av = avma;
    2447         168 :   GEN S, G = checkgroup(gal,&S);
    2448         168 :   if (!group_isabelian(G)) { avma=av; return gen_0; }
    2449         147 :   switch(flag)
    2450             :   {
    2451          49 :     case 0: return gerepileupto(av, group_abelianHNF(G,S));
    2452          49 :     case 1: avma=av; return gen_1;
    2453          49 :     case 2: return gerepileupto(av, group_abelianSNF(G,S));
    2454           0 :     default: pari_err_FLAG("galoisisabelian");
    2455             :   }
    2456           0 :   return NULL; /* not reached */
    2457             : }
    2458             : 
    2459             : long
    2460          56 : galoisisnormal(GEN gal, GEN sub)
    2461             : {
    2462          56 :   pari_sp av = avma;
    2463          56 :   GEN S, G = checkgroup(gal, &S), H = checkgroup(sub, &S);
    2464          56 :   long res = group_subgroup_isnormal(G, H);
    2465          56 :   avma = av;
    2466          56 :   return res;
    2467             : }
    2468             : 
    2469             : GEN
    2470          56 : galoissubgroups(GEN gal)
    2471             : {
    2472          56 :   pari_sp av = avma;
    2473          56 :   GEN S, G = checkgroup(gal,&S);
    2474          56 :   return gerepileupto(av, group_subgroups(G));
    2475             : }
    2476             : 
    2477             : GEN
    2478          42 : galoissubfields(GEN G, long flag, long v)
    2479             : {
    2480          42 :   pari_sp av = avma;
    2481          42 :   GEN L = galoissubgroups(G);
    2482          42 :   long i, l = lg(L);
    2483          42 :   GEN S = cgetg(l, t_VEC);
    2484          42 :   for (i = 1; i < l; ++i) gel(S,i) = galoisfixedfield(G, gmael(L,i,1), flag, v);
    2485          42 :   return gerepileupto(av, S);
    2486             : }
    2487             : 
    2488             : GEN
    2489          28 : galoisexport(GEN gal, long format)
    2490             : {
    2491          28 :   pari_sp av = avma;
    2492          28 :   GEN S, G = checkgroup(gal,&S);
    2493          28 :   return gerepileupto(av, group_export(G,format));
    2494             : }
    2495             : 
    2496             : GEN
    2497         392 : galoisidentify(GEN gal)
    2498             : {
    2499         392 :   pari_sp av = avma;
    2500         392 :   GEN S, G = checkgroup(gal,&S);
    2501         385 :   long idx = group_ident(G,S), card = group_order(G);
    2502         385 :   avma = av; return mkvec2s(card, idx);
    2503             : }

Generated by: LCOV version 1.11