Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - modules - algebras.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23328-a3379c31c) Lines: 2976 3063 97.2 %
Date: 2018-12-09 05:41:42 Functions: 269 273 98.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2000  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. 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             : #include "pari.h"
      14             : #include "paripriv.h"
      15             : 
      16             : #define dbg_printf(lvl) if (DEBUGLEVEL >= (lvl) + 3) err_printf
      17             : 
      18             : /********************************************************************/
      19             : /**                                                                **/
      20             : /**           ASSOCIATIVE ALGEBRAS, CENTRAL SIMPLE ALGEBRAS        **/
      21             : /**                 contributed by Aurel Page (2014)               **/
      22             : /**                                                                **/
      23             : /********************************************************************/
      24             : static GEN alg_subalg(GEN al, GEN basis);
      25             : static GEN alg_maximal_primes(GEN al, GEN P);
      26             : static GEN algnatmultable(GEN al, long D);
      27             : static GEN _tablemul_ej(GEN mt, GEN x, long j);
      28             : static GEN _tablemul_ej_Fp(GEN mt, GEN x, long j, GEN p);
      29             : static GEN _tablemul_ej_Fl(GEN mt, GEN x, long j, ulong p);
      30             : static ulong algtracei(GEN mt, ulong p, ulong expo, ulong modu);
      31             : static GEN alg_pmaximal(GEN al, GEN p);
      32             : static GEN alg_maximal(GEN al);
      33             : static GEN algtracematrix(GEN al);
      34             : static GEN algtableinit_i(GEN mt0, GEN p);
      35             : static GEN algbasisrightmultable(GEN al, GEN x);
      36             : static GEN algabstrace(GEN al, GEN x);
      37             : static GEN algbasismul(GEN al, GEN x, GEN y);
      38             : static GEN algbasismultable(GEN al, GEN x);
      39             : static GEN algbasismultable_Flm(GEN mt, GEN x, ulong m);
      40             : 
      41             : 
      42             : static int
      43      817021 : checkalg_i(GEN al)
      44             : {
      45             :   GEN mt, rnf;
      46      817021 :   if (typ(al) != t_VEC || lg(al) != 12) return 0;
      47      816825 :   mt = alg_get_multable(al);
      48      816825 :   if (typ(mt) != t_VEC || lg(mt) == 1 || typ(gel(mt,1)) != t_MAT) return 0;
      49      816804 :   rnf = alg_get_splittingfield(al);
      50      816804 :   if (isintzero(rnf) || !gequal0(alg_get_char(al))) return 1;
      51      459697 :   if (typ(gel(al,2)) != t_VEC || lg(gel(al,2)) == 1) return 0;
      52             :   /* not checkrnf_i: beware placeholder from alg_csa_table */
      53      459690 :   return typ(rnf)==t_VEC && lg(rnf)==13;
      54             : }
      55             : void
      56      816349 : checkalg(GEN al)
      57      816349 : { if (!checkalg_i(al)) pari_err_TYPE("checkalg [please apply alginit()]",al); }
      58             : 
      59             : static int
      60      180992 : checklat_i(GEN al, GEN lat)
      61             : {
      62             :   long N,i,j;
      63             :   GEN m,t,c;
      64      180992 :   if (typ(lat)!=t_VEC || lg(lat) != 3) return 0;
      65      180992 :   t = gel(lat,2);
      66      180992 :   if (typ(t) != t_INT && typ(t) != t_FRAC) return 0;
      67      180992 :   if (gsigne(t)<=0) return 0;
      68      180992 :   m = gel(lat,1);
      69      180992 :   if (typ(m) != t_MAT) return 0;
      70      180992 :   N = alg_get_absdim(al);
      71      180992 :   if (lg(m)-1 != N || lg(gel(m,1))-1 != N) return 0;
      72     1628886 :   for (i=1; i<=N; i++)
      73    13031067 :     for (j=1; j<=N; j++) {
      74    11583173 :       c = gcoeff(m,i,j);
      75    11583173 :       if (typ(c) != t_INT) return 0;
      76    11583173 :       if (j<i && signe(gcoeff(m,i,j))) return 0;
      77    11583173 :       if (i==j && !signe(gcoeff(m,i,j))) return 0;
      78             :     }
      79      180985 :   return 1;
      80             : }
      81      180992 : void checklat(GEN al, GEN lat)
      82      180992 : { if (!checklat_i(al,lat)) pari_err_TYPE("checklat [please apply alglathnf()]", lat); }
      83             : 
      84             : 
      85             : /**  ACCESSORS  **/
      86             : long
      87     4828391 : alg_type(GEN al)
      88             : {
      89     4828391 :   if (isintzero(alg_get_splittingfield(al)) || !gequal0(alg_get_char(al))) return al_TABLE;
      90     3578484 :   switch(typ(gmael(al,2,1))) {
      91      895678 :     case t_MAT: return al_CSA;
      92             :     case t_INT:
      93             :     case t_FRAC:
      94             :     case t_POL:
      95     2682785 :     case t_POLMOD: return al_CYCLIC;
      96          21 :     default: return al_NULL;
      97             :   }
      98             :   return -1; /*LCOV_EXCL_LINE*/
      99             : }
     100             : long
     101         203 : algtype(GEN al)
     102         203 : { return checkalg_i(al)? alg_type(al): al_NULL; }
     103             : 
     104             : /* absdim == dim for al_TABLE. */
     105             : long
     106      224420 : alg_get_dim(GEN al)
     107             : {
     108             :   long d;
     109      224420 :   switch(alg_type(al)) {
     110       10549 :     case al_TABLE: return lg(alg_get_multable(al))-1;
     111      213794 :     case al_CSA: return lg(alg_get_relmultable(al))-1;
     112          77 :     case al_CYCLIC: d = alg_get_degree(al); return d*d;
     113           0 :     default: pari_err_TYPE("alg_get_dim", al);
     114             :   }
     115             :   return -1; /*LCOV_EXCL_LINE*/
     116             : }
     117             : 
     118             : long
     119     1548915 : alg_get_absdim(GEN al)
     120             : {
     121     1548915 :   switch(alg_type(al)) {
     122      660370 :     case al_TABLE: return lg(alg_get_multable(al))-1;
     123      113162 :     case al_CSA: return alg_get_dim(al)*nf_get_degree(alg_get_center(al));
     124             :     case al_CYCLIC:
     125      775383 :       return rnf_get_absdegree(alg_get_splittingfield(al))*alg_get_degree(al);
     126           0 :     default: pari_err_TYPE("alg_get_absdim", al);
     127             :   }
     128             :   return -1;/*LCOV_EXCL_LINE*/
     129             : }
     130             : 
     131             : long
     132        1715 : algdim(GEN al, long abs)
     133             : {
     134        1715 :   checkalg(al);
     135        1694 :   if (abs) return alg_get_absdim(al);
     136        1491 :   return alg_get_dim(al);
     137             : }
     138             : 
     139             : /* only cyclic */
     140             : GEN
     141       12936 : alg_get_auts(GEN al)
     142             : {
     143       12936 :   if (alg_type(al) != al_CYCLIC)
     144           0 :     pari_err_TYPE("alg_get_auts [non-cyclic algebra]", al);
     145       12936 :   return gel(al,2);
     146             : }
     147             : GEN
     148          91 : alg_get_aut(GEN al)
     149             : {
     150          91 :   if (alg_type(al) != al_CYCLIC)
     151           7 :     pari_err_TYPE("alg_get_aut [non-cyclic algebra]", al);
     152          84 :   return gel(alg_get_auts(al),1);
     153             : }
     154             : GEN
     155          21 : algaut(GEN al) { checkalg(al); return alg_get_aut(al); }
     156             : GEN
     157       12957 : alg_get_b(GEN al)
     158             : {
     159       12957 :   if (alg_type(al) != al_CYCLIC)
     160           7 :     pari_err_TYPE("alg_get_b [non-cyclic algebra]", al);
     161       12950 :   return gel(al,3);
     162             : }
     163             : GEN
     164          35 : algb(GEN al) { checkalg(al); return alg_get_b(al); }
     165             : 
     166             : /* only CSA */
     167             : GEN
     168      215831 : alg_get_relmultable(GEN al)
     169             : {
     170      215831 :   if (alg_type(al) != al_CSA)
     171           7 :     pari_err_TYPE("alg_get_relmultable [algebra not given via mult. table]", al);
     172      215824 :   return gel(al,2);
     173             : }
     174             : GEN
     175          42 : algrelmultable(GEN al) { checkalg(al); return alg_get_relmultable(al); }
     176             : GEN
     177          49 : alg_get_splittingdata(GEN al)
     178             : {
     179          49 :   if (alg_type(al) != al_CSA)
     180           7 :     pari_err_TYPE("alg_get_splittingdata [algebra not given via mult. table]",al);
     181          42 :   return gel(al,3);
     182             : }
     183             : GEN
     184          49 : algsplittingdata(GEN al) { checkalg(al); return alg_get_splittingdata(al); }
     185             : GEN
     186        4102 : alg_get_splittingbasis(GEN al)
     187             : {
     188        4102 :   if (alg_type(al) != al_CSA)
     189           0 :     pari_err_TYPE("alg_get_splittingbasis [algebra not given via mult. table]",al);
     190        4102 :   return gmael(al,3,2);
     191             : }
     192             : GEN
     193        4102 : alg_get_splittingbasisinv(GEN al)
     194             : {
     195        4102 :   if (alg_type(al) != al_CSA)
     196           0 :     pari_err_TYPE("alg_get_splittingbasisinv [algebra not given via mult. table]",al);
     197        4102 :   return gmael(al,3,3);
     198             : }
     199             : 
     200             : /* only cyclic and CSA */
     201             : GEN
     202     8096651 : alg_get_splittingfield(GEN al) { return gel(al,1); }
     203             : GEN
     204          91 : algsplittingfield(GEN al)
     205             : {
     206             :   long ta;
     207          91 :   checkalg(al);
     208          91 :   ta = alg_type(al);
     209          91 :   if (ta != al_CYCLIC && ta != al_CSA)
     210           7 :     pari_err_TYPE("alg_get_splittingfield [use alginit]",al);
     211          84 :   return alg_get_splittingfield(al);
     212             : }
     213             : long
     214     1228542 : alg_get_degree(GEN al)
     215             : {
     216             :   long ta;
     217     1228542 :   ta = alg_type(al);
     218     1228542 :   if (ta != al_CYCLIC && ta != al_CSA)
     219          21 :     pari_err_TYPE("alg_get_degree [use alginit]",al);
     220     1228521 :   return rnf_get_degree(alg_get_splittingfield(al));
     221             : }
     222             : long
     223         301 : algdegree(GEN al)
     224             : {
     225         301 :   checkalg(al);
     226         294 :   return alg_get_degree(al);
     227             : }
     228             : 
     229             : GEN
     230      294749 : alg_get_center(GEN al)
     231             : {
     232             :   long ta;
     233      294749 :   ta = alg_type(al);
     234      294749 :   if (ta != al_CSA && ta != al_CYCLIC)
     235           7 :     pari_err_TYPE("alg_get_center [use alginit]",al);
     236      294742 :   return rnf_get_nf(alg_get_splittingfield(al));
     237             : }
     238             : GEN
     239          70 : alg_get_splitpol(GEN al)
     240             : {
     241          70 :   long ta = alg_type(al);
     242          70 :   if (ta != al_CYCLIC && ta != al_CSA)
     243           0 :     pari_err_TYPE("alg_get_splitpol [use alginit]",al);
     244          70 :   return rnf_get_pol(alg_get_splittingfield(al));
     245             : }
     246             : GEN
     247       67312 : alg_get_abssplitting(GEN al)
     248             : {
     249       67312 :   long ta = alg_type(al), prec;
     250       67312 :   if (ta != al_CYCLIC && ta != al_CSA)
     251           0 :     pari_err_TYPE("alg_get_abssplitting [use alginit]",al);
     252       67312 :   prec = nf_get_prec(alg_get_center(al));
     253       67312 :   return rnf_build_nfabs(alg_get_splittingfield(al), prec);
     254             : }
     255             : GEN
     256        1134 : alg_get_hasse_i(GEN al)
     257             : {
     258        1134 :   long ta = alg_type(al);
     259        1134 :   if (ta != al_CYCLIC && ta != al_CSA)
     260           7 :     pari_err_TYPE("alg_get_hasse_i [use alginit]",al);
     261        1127 :   if (ta == al_CSA) pari_err_IMPL("computation of Hasse invariants over table CSA");
     262        1120 :   return gel(al,4);
     263             : }
     264             : GEN
     265         210 : alghassei(GEN al) { checkalg(al); return alg_get_hasse_i(al); }
     266             : GEN
     267        1883 : alg_get_hasse_f(GEN al)
     268             : {
     269        1883 :   long ta = alg_type(al);
     270        1883 :   if (ta != al_CYCLIC && ta != al_CSA)
     271           7 :     pari_err_TYPE("alg_get_hasse_f [use alginit]",al);
     272        1876 :   if (ta == al_CSA) pari_err_IMPL("computation of Hasse invariants over table CSA");
     273        1869 :   return gel(al,5);
     274             : }
     275             : GEN
     276         329 : alghassef(GEN al) { checkalg(al); return alg_get_hasse_f(al); }
     277             : 
     278             : /* all types */
     279             : GEN
     280        2695 : alg_get_basis(GEN al) { return gel(al,7); }
     281             : GEN
     282          49 : algbasis(GEN al) { checkalg(al); return alg_get_basis(al); }
     283             : GEN
     284       59661 : alg_get_invbasis(GEN al) { return gel(al,8); }
     285             : GEN
     286          49 : alginvbasis(GEN al) { checkalg(al); return alg_get_invbasis(al); }
     287             : GEN
     288     2228728 : alg_get_multable(GEN al) { return gel(al,9); }
     289             : GEN
     290         217 : algmultable(GEN al) { checkalg(al); return alg_get_multable(al); }
     291             : GEN
     292     5567745 : alg_get_char(GEN al) { return gel(al,10); }
     293             : GEN
     294          91 : algchar(GEN al) { checkalg(al); return alg_get_char(al); }
     295             : GEN
     296      236481 : alg_get_tracebasis(GEN al) { return gel(al,11); }
     297             : 
     298             : /* lattices */
     299             : GEN
     300      244314 : alglat_get_primbasis(GEN lat) { return gel(lat,1); }
     301             : GEN
     302      289905 : alglat_get_scalar(GEN lat) { return gel(lat,2); }
     303             : 
     304             : /** ADDITIONAL **/
     305             : 
     306             : static long
     307         721 : rnfrealdec(GEN rnf, long k)
     308             : {
     309         721 :   pari_sp av = avma;
     310         721 :   GEN r = nfpolsturm(rnf_get_nf(rnf), rnf_get_pol(rnf), stoi(k));
     311         721 :   return gc_long(av, itou(r));
     312             : }
     313             : 
     314             : /* no garbage collection */
     315             : static GEN
     316         777 : backtrackfacto(GEN y0, long n, GEN red, GEN pl, GEN nf, GEN data, int (*test)(GEN,GEN,GEN), GEN* fa, GEN N, GEN I)
     317             : {
     318             :   long b, i;
     319             :   GEN y1, y2, ny, fan;
     320         777 :   long *v = new_chunk(n+1);
     321         777 :   pari_sp av = avma;
     322         798 :   for (b = 0;; b = b+(2*b)/(3*n)+1)
     323             :   {
     324         819 :     set_avma(av);
     325         798 :     for (i=1; i<=n; i++) v[i] = -b;
     326         798 :     v[n]--;
     327             :     while (1) {
     328         966 :       i=n;
     329        1890 :       while (i>0) {
     330         987 :         if (v[i]==b) { v[i] = -b; i--; } else { v[i]++; break; }
     331             :       }
     332         882 :       if (i==0) break;
     333             : 
     334         861 :       y1 = y0;
     335         861 :       for (i=1; i<=n; i++) y1 = nfadd(nf, y1, ZC_z_mul(gel(red,i), v[i]));
     336         861 :       if (!nfchecksigns(nf, y1, pl)) continue;
     337             : 
     338         819 :       ny = absi_shallow(nfnorm(nf, y1));
     339         819 :       if (!signe(ny)) continue;
     340         819 :       ny = diviiexact(ny,gcdii(ny,N));
     341         819 :       fan = Z_factor_limit(ny,1<<17);
     342         819 :       if (lg(fan)>1 && nbrows(fan)>0 && !isprime(gcoeff(fan,nbrows(fan),1)))
     343           7 :         continue;
     344             : 
     345         812 :       y2 = idealdivexact(nf,y1,idealadd(nf,y1,I));
     346         812 :       *fa = idealfactor(nf, y2);
     347        1589 :       if (!data || test(data,y1,*fa)) return y1;
     348             :     }
     349             :   }
     350             : }
     351             : 
     352             : /* if data == NULL, the test is skipped */
     353             : /* in the test, the factorization does not contain the known factors */
     354             : static GEN
     355         777 : factoredextchinesetest(GEN nf, GEN x, GEN y, GEN pl, GEN* fa, GEN data, int (*test)(GEN,GEN,GEN))
     356             : {
     357         777 :   pari_sp av = avma;
     358             :   long n,i;
     359         777 :   GEN x1, y0, y1, red, N, I, P = gel(x,1), E = gel(x,2);
     360         777 :   n = nf_get_degree(nf);
     361         777 :   x = idealchineseinit(nf, mkvec2(x,pl));
     362         777 :   x1 = gel(x,1);
     363         777 :   red = lg(x1) == 1? matid(n): gel(x1,1);
     364         777 :   y0 = idealchinese(nf, x, y);
     365             : 
     366         777 :   E = shallowcopy(E);
     367         777 :   if (!gequal0(y0))
     368        1981 :     for (i=1; i<lg(E); i++)
     369             :     {
     370        1204 :       long v = nfval(nf,y0,gel(P,i));
     371        1204 :       if (cmpsi(v, gel(E,i)) < 0) gel(E,i) = stoi(v);
     372             :     }
     373             :   /* N and I : known factors */
     374         777 :   I = factorbackprime(nf, P, E);
     375         777 :   N = idealnorm(nf,I);
     376             : 
     377         777 :   y1 = backtrackfacto(y0, n, red, pl, nf, data, test, fa, N, I);
     378             : 
     379             :   /* restore known factors */
     380         777 :   for (i=1; i<lg(E); i++) gel(E,i) = stoi(nfval(nf,y1,gel(P,i)));
     381         777 :   *fa = famat_reduce(famat_mul_shallow(*fa, mkmat2(P, E)));
     382             : 
     383         777 :   gerepileall(av, 2, &y1, fa);
     384         777 :   return y1;
     385             : }
     386             : 
     387             : static GEN
     388         553 : factoredextchinese(GEN nf, GEN x, GEN y, GEN pl, GEN* fa)
     389         553 : { return factoredextchinesetest(nf,x,y,pl,fa,NULL,NULL); }
     390             : 
     391             : /** OPERATIONS ON ASSOCIATIVE ALGEBRAS algebras.c **/
     392             : 
     393             : /*
     394             : Convention:
     395             : (K/F,sigma,b) = sum_{i=0..n-1} u^i*K
     396             : t*u = u*sigma(t)
     397             : 
     398             : Natural basis:
     399             : 1<=i<=d*n^2
     400             : b_i = u^((i-1)/(dn))*ZKabs.((i-1)%(dn)+1)
     401             : 
     402             : Integral basis:
     403             : Basis of some order.
     404             : 
     405             : al:
     406             : 1- rnf of the cyclic splitting field of degree n over the center nf of degree d
     407             : 2- VEC of aut^i 1<=i<=n
     408             : 3- b in nf
     409             : 4- infinite hasse invariants (mod n) : VECSMALL of size r1, values only 0 or n/2 (if integral)
     410             : 5- finite hasse invariants (mod n) : VEC[VEC of primes, VECSMALL of hasse inv mod n]
     411             : 6- nf of the splitting field (absolute)
     412             : 7* dn^2*dn^2 matrix expressing the integral basis in terms of the natural basis
     413             : 8* dn^2*dn^2 matrix expressing the natural basis in terms of the integral basis
     414             : 9* VEC of dn^2 matrices giving the dn^2*dn^2 left multiplication tables of the integral basis
     415             : 10* characteristic of the base field (used only for algebras given by a multiplication table)
     416             : 11* trace of basis elements
     417             : 
     418             : If al is given by a multiplication table (al_TABLE), only the * fields are present.
     419             : */
     420             : 
     421             : /* assumes same center and same variable */
     422             : /* currently only works for coprime degrees */
     423             : GEN
     424          77 : algtensor(GEN al1, GEN al2, long maxord) {
     425          77 :   pari_sp av = avma;
     426             :   long v, k, d1, d2;
     427             :   GEN nf, P1, P2, aut1, aut2, b1, b2, C, rnf, aut, b, x1, x2, al;
     428             : 
     429          77 :   checkalg(al1);
     430          63 :   checkalg(al2);
     431          56 :   if (alg_type(al1) != al_CYCLIC  || alg_type(al2) != al_CYCLIC)
     432          14 :     pari_err_IMPL("tensor of non-cyclic algebras"); /* TODO: do it. */
     433             : 
     434          42 :   nf=alg_get_center(al1);
     435          42 :   if (!gequal(alg_get_center(al2),nf))
     436           7 :     pari_err_OP("tensor product [not the same center]", al1, al2);
     437             : 
     438          35 :   P1=alg_get_splitpol(al1); aut1=alg_get_aut(al1); b1=alg_get_b(al1);
     439          35 :   P2=alg_get_splitpol(al2); aut2=alg_get_aut(al2); b2=alg_get_b(al2);
     440          35 :   v=varn(P1);
     441             : 
     442          35 :   d1=alg_get_degree(al1);
     443          35 :   d2=alg_get_degree(al2);
     444          35 :   if (ugcd(d1,d2) != 1)
     445           7 :     pari_err_IMPL("tensor of cylic algebras of non-coprime degrees"); /* TODO */
     446             : 
     447          28 :   if (d1==1) return gcopy(al2);
     448          21 :   if (d2==1) return gcopy(al1);
     449             : 
     450          14 :   C = nfcompositum(nf, P1, P2, 3);
     451          14 :   rnf = rnfinit(nf,gel(C,1));
     452          14 :   x1 = gel(C,2);
     453          14 :   x2 = gel(C,3);
     454          14 :   k = itos(gel(C,4));
     455          14 :   aut = gadd(gsubst(aut2,v,x2),gmulsg(k,gsubst(aut1,v,x1)));
     456          14 :   b = nfmul(nf,nfpow_u(nf,b1,d2),nfpow_u(nf,b2,d1));
     457          14 :   al = alg_cyclic(rnf,aut,b,maxord);
     458          14 :   return gerepilecopy(av,al);
     459             : }
     460             : 
     461             : /* M an n x d Flm of rank d, n >= d. Initialize Mx = y solver */
     462             : static GEN
     463        4298 : Flm_invimage_init(GEN M, ulong p)
     464             : {
     465        4298 :   GEN v = Flm_indexrank(M, p), perm = gel(v,1);
     466        4298 :   GEN MM = rowpermute(M, perm); /* square invertible */
     467        4298 :   return mkvec2(Flm_inv(MM,p), perm);
     468             : }
     469             : /* assume Mx = y has a solution, v = Flm_invimage_init(M,p); return x */
     470             : static GEN
     471      244153 : Flm_invimage_pre(GEN v, GEN y, ulong p)
     472             : {
     473      244153 :   GEN inv = gel(v,1), perm = gel(v,2);
     474      244153 :   return Flm_Flc_mul(inv, vecsmallpermute(y, perm), p);
     475             : }
     476             : 
     477             : GEN
     478        5271 : algradical(GEN al)
     479             : {
     480        5271 :   pari_sp av = avma;
     481             :   GEN I, x, traces, K, MT, P, mt;
     482             :   long l,i,ni, n;
     483             :   ulong modu, expo, p;
     484        5271 :   checkalg(al);
     485        5271 :   P = alg_get_char(al);
     486        5271 :   mt = alg_get_multable(al);
     487        5271 :   n = alg_get_absdim(al);
     488        5271 :   dbg_printf(1)("algradical: char=%Ps, dim=%d\n", P, n);
     489        5271 :   traces = algtracematrix(al);
     490        5271 :   if (!signe(P))
     491             :   {
     492         567 :     dbg_printf(2)(" char 0, computing kernel...\n");
     493         567 :     K = ker(traces);
     494         567 :     dbg_printf(2)(" ...done.\n");
     495         567 :     ni = lg(K)-1; if (!ni) { set_avma(av); return gen_0; }
     496          70 :     return gerepileupto(av, K);
     497             :   }
     498        4704 :   dbg_printf(2)(" char>0, computing kernel...\n");
     499        4704 :   K = FpM_ker(traces, P);
     500        4704 :   dbg_printf(2)(" ...done.\n");
     501        4704 :   ni = lg(K)-1; if (!ni) { set_avma(av); return gen_0; }
     502        2828 :   if (abscmpiu(P,n)>0) return gerepileupto(av, K);
     503             : 
     504             :   /* tough case, p <= n. Ronyai's algorithm */
     505        2317 :   p = P[2]; l = 1;
     506        2317 :   expo = p; modu = p*p;
     507        2317 :   dbg_printf(2)(" char>0, hard case.\n");
     508        2317 :   while (modu<=(ulong)n) { l++; modu *= p; }
     509        2317 :   MT = ZMV_to_FlmV(mt, modu);
     510        2317 :   I = ZM_to_Flm(K,p); /* I_0 */
     511        6314 :   for (i=1; i<=l; i++) {/*compute I_i, expo = p^i, modu = p^(l+1) > n*/
     512             :     long j, lig,col;
     513        4298 :     GEN v = cgetg(ni+1, t_VECSMALL);
     514        4298 :     GEN invI = Flm_invimage_init(I, p);
     515        4298 :     dbg_printf(2)(" computing I_%d:\n", i);
     516        4298 :     traces = cgetg(ni+1,t_MAT);
     517       28826 :     for (j = 1; j <= ni; j++)
     518             :     {
     519       24528 :       GEN M = algbasismultable_Flm(MT, gel(I,j), modu);
     520       24528 :       uel(v,j) = algtracei(M, p,expo,modu);
     521             :     }
     522       28826 :     for (col=1; col<=ni; col++)
     523             :     {
     524       24528 :       GEN t = cgetg(n+1,t_VECSMALL); gel(traces,col) = t;
     525       24528 :       x = gel(I, col); /*col-th basis vector of I_{i-1}*/
     526      268681 :       for (lig=1; lig<=n; lig++)
     527             :       {
     528      244153 :         GEN y = _tablemul_ej_Fl(MT,x,lig,p);
     529      244153 :         GEN z = Flm_invimage_pre(invI, y, p);
     530      244153 :         uel(t,lig) = Flv_dotproduct(v, z, p);
     531             :       }
     532             :     }
     533        4298 :     dbg_printf(2)(" computing kernel...\n");
     534        4298 :     K = Flm_ker(traces, p);
     535        4298 :     dbg_printf(2)(" ...done.\n");
     536        4298 :     ni = lg(K)-1; if (!ni) { set_avma(av); return gen_0; }
     537        3997 :     I = Flm_mul(I,K,p);
     538        3997 :     expo *= p;
     539             :   }
     540        2016 :   return Flm_to_ZM(I);
     541             : }
     542             : 
     543             : /* compute the multiplication table of the element x, where mt is a
     544             :  * multiplication table in an arbitrary ring */
     545             : static GEN
     546         427 : Rgmultable(GEN mt, GEN x)
     547             : {
     548         427 :   long i, l = lg(x);
     549         427 :   GEN z = NULL;
     550        5796 :   for (i = 1; i < l; i++)
     551             :   {
     552        5369 :     GEN c = gel(x,i);
     553        5369 :     if (!gequal0(c))
     554             :     {
     555         644 :       GEN M = RgM_Rg_mul(gel(mt,i),c);
     556         644 :       z = z? RgM_add(z, M): M;
     557             :     }
     558             :   }
     559         427 :   return z;
     560             : }
     561             : 
     562             : static GEN
     563          49 : change_Rgmultable(GEN mt, GEN P, GEN Pi)
     564             : {
     565             :   GEN mt2;
     566          49 :   long lmt = lg(mt), i;
     567          49 :   mt2 = cgetg(lmt,t_VEC);
     568         476 :   for (i=1;i<lmt;i++) {
     569         427 :     GEN mti = Rgmultable(mt,gel(P,i));
     570         427 :     gel(mt2,i) = RgM_mul(Pi, RgM_mul(mti,P));
     571             :   }
     572          49 :   return mt2;
     573             : }
     574             : 
     575             : static GEN
     576       19915 : alg_quotient0(GEN al, GEN S, GEN Si, long nq, GEN p, long maps)
     577             : {
     578       19915 :   GEN mt = cgetg(nq+1,t_VEC), P, Pi, d;
     579             :   long i;
     580       19915 :   dbg_printf(3)("  alg_quotient0: char=%Ps, dim=%d, dim I=%d\n", p, alg_get_absdim(al), lg(S)-1);
     581       79307 :   for (i=1; i<=nq; i++) {
     582       59392 :     GEN mti = algbasismultable(al,gel(S,i));
     583       59392 :     if (signe(p)) gel(mt,i) = FpM_mul(Si, FpM_mul(mti,S,p), p);
     584        5397 :     else          gel(mt,i) = RgM_mul(Si, RgM_mul(mti,S));
     585             :   }
     586       19915 :   if (!signe(p) && !isint1(Q_denom(mt))) {
     587          35 :     dbg_printf(3)("  bad case: denominator=%Ps\n", Q_denom(mt));
     588          35 :     P = Q_remove_denom(Si,&d);
     589          35 :     P = ZM_hnf(P);
     590          35 :     P = RgM_Rg_div(P,d);
     591          35 :     Pi = RgM_inv(P);
     592          35 :     mt = change_Rgmultable(mt,P,Pi);
     593          35 :     Si = RgM_mul(P,Si);
     594          35 :     S = RgM_mul(S,Pi);
     595             :   }
     596       19915 :   al = algtableinit_i(mt,p);
     597       19915 :   if (maps) al = mkvec3(al,Si,S); /* algebra, proj, lift */
     598       19915 :   return al;
     599             : }
     600             : 
     601             : /* quotient of an algebra by a nontrivial two-sided ideal */
     602             : GEN
     603        2716 : alg_quotient(GEN al, GEN I, long maps)
     604             : {
     605        2716 :   pari_sp av = avma;
     606             :   GEN p, IS, ISi, S, Si;
     607             :   long n, ni;
     608             : 
     609        2716 :   checkalg(al);
     610        2716 :   p = alg_get_char(al);
     611        2716 :   n = alg_get_absdim(al);
     612        2716 :   ni = lg(I)-1;
     613             : 
     614             :   /* force first vector of complement to be the identity */
     615        2716 :   IS = shallowconcat(I, gcoeff(alg_get_multable(al),1,1));
     616        2716 :   if (signe(p)) {
     617        2688 :     IS = FpM_suppl(IS,p);
     618        2688 :     ISi = FpM_inv(IS,p);
     619             :   }
     620             :   else {
     621          28 :     IS = suppl(IS);
     622          28 :     ISi = RgM_inv(IS);
     623             :   }
     624        2716 :   S = vecslice(IS, ni+1, n);
     625        2716 :   Si = rowslice(ISi, ni+1, n);
     626        2716 :   return gerepilecopy(av, alg_quotient0(al, S, Si, n-ni, p, maps));
     627             : }
     628             : 
     629             : static GEN
     630       27279 : image_keep_first(GEN m, GEN p) /* assume first column is nonzero or m==0, no GC */
     631             : {
     632             :   GEN ir, icol, irow, M, c, x;
     633             :   long i;
     634       27279 :   if (gequal0(gel(m,1))) return zeromat(nbrows(m),0);
     635             : 
     636       27265 :   if (signe(p)) ir = FpM_indexrank(m,p);
     637        1498 :   else          ir = indexrank(m);
     638             : 
     639       27265 :   icol = gel(ir,2);
     640       27265 :   if (icol[1]==1) return extract0(m,icol,NULL);
     641             : 
     642          35 :   irow = gel(ir,1);
     643          35 :   M = extract0(m, irow, icol);
     644          35 :   c = extract0(gel(m,1), irow, NULL);
     645          35 :   if (signe(p)) x = FpM_FpC_invimage(M,c,p);
     646           0 :   else          x = inverseimage(M,c); /* TODO modulo a small prime */
     647             : 
     648          35 :   for (i=1; i<lg(x); i++)
     649             :   {
     650          35 :     if (!gequal0(gel(x,i)))
     651             :     {
     652          35 :       icol[i] = 1;
     653          35 :       vecsmall_sort(icol);
     654          35 :       return extract0(m,icol,NULL);
     655             :     }
     656             :   }
     657             : 
     658             :   return NULL; /* LCOV_EXCL_LINE */
     659             : }
     660             : 
     661             : /* z[1],...z[nz] central elements such that z[1]A + z[2]A + ... + z[nz]A = A
     662             :  * is a direct sum. idempotents ==> first basis element is identity */
     663             : GEN
     664        8120 : alg_centralproj(GEN al, GEN z, long maps)
     665             : {
     666        8120 :   pari_sp av = avma;
     667             :   GEN S, U, Ui, alq, p;
     668        8120 :   long i, iu, lz = lg(z);
     669             : 
     670        8120 :   checkalg(al);
     671        8120 :   if (typ(z) != t_VEC) pari_err_TYPE("alcentralproj",z);
     672        8113 :   p = alg_get_char(al);
     673        8113 :   dbg_printf(3)("  alg_centralproj: char=%Ps, dim=%d, #z=%d\n", p, alg_get_absdim(al), lz-1);
     674        8113 :   S = cgetg(lz,t_VEC); /* S[i] = Im(z_i) */
     675       25326 :   for (i=1; i<lz; i++)
     676             :   {
     677       17213 :     GEN mti = algbasismultable(al, gel(z,i));
     678       17213 :     gel(S,i) = image_keep_first(mti,p);
     679             :   }
     680        8113 :   U = shallowconcat1(S); /* U = [Im(z_1)|Im(z_2)|...|Im(z_nz)], n x n */
     681        8113 :   if (lg(U)-1 < alg_get_absdim(al)) pari_err_TYPE("alcentralproj [z[i]'s not surjective]",z);
     682        8106 :   if (signe(p)) Ui = FpM_inv(U,p);
     683         749 :   else          Ui = RgM_inv(U);
     684             :   if (!Ui) pari_err_BUG("alcentralproj"); /*LCOV_EXCL_LINE*/
     685             : 
     686        8106 :   alq = cgetg(lz,t_VEC);
     687       25305 :   for (iu=0,i=1; i<lz; i++)
     688             :   {
     689       17199 :     long nq = lg(gel(S,i))-1, ju = iu + nq;
     690       17199 :     GEN Si = rowslice(Ui, iu+1, ju);
     691       17199 :     gel(alq, i) = alg_quotient0(al,gel(S,i),Si,nq,p,maps);
     692       17199 :     iu = ju;
     693             :   }
     694        8106 :   return gerepilecopy(av, alq);
     695             : }
     696             : 
     697             : /* al is an al_TABLE */
     698             : static GEN
     699       18305 : algtablecenter(GEN al)
     700             : {
     701       18305 :   pari_sp av = avma;
     702             :   long n, i, j, k, ic;
     703             :   GEN C, cij, mt, p;
     704             : 
     705       18305 :   n = alg_get_absdim(al);
     706       18305 :   mt = alg_get_multable(al);
     707       18305 :   p = alg_get_char(al);
     708       18305 :   C = cgetg(n+1,t_MAT);
     709       89572 :   for (j=1; j<=n; j++)
     710             :   {
     711       71267 :     gel(C,j) = cgetg(n*n-n+1,t_COL);
     712       71267 :     ic = 1;
     713      586775 :     for (i=2; i<=n; i++) {
     714      515508 :       if (signe(p)) cij = FpC_sub(gmael(mt,i,j),gmael(mt,j,i),p);
     715       52318 :       else          cij = RgC_sub(gmael(mt,i,j),gmael(mt,j,i));
     716      515508 :       for (k=1; k<=n; k++, ic++) gcoeff(C,ic,j) = gel(cij, k);
     717             :     }
     718             :   }
     719       18305 :   if (signe(p)) return gerepileupto(av, FpM_ker(C,p));
     720        1645 :   else          return gerepileupto(av, ker(C));
     721             : }
     722             : 
     723             : GEN
     724        4865 : algcenter(GEN al)
     725             : {
     726        4865 :   checkalg(al);
     727        4865 :   if (alg_type(al)==al_TABLE) return algtablecenter(al);
     728          28 :   return alg_get_center(al);
     729             : }
     730             : 
     731             : /* Only in positive characteristic. Assumes that al is semisimple. */
     732             : GEN
     733        3969 : algprimesubalg(GEN al)
     734             : {
     735        3969 :   pari_sp av = avma;
     736             :   GEN p, Z, F, K;
     737             :   long nz, i;
     738        3969 :   checkalg(al);
     739        3969 :   p = alg_get_char(al);
     740        3969 :   if (!signe(p)) pari_err_DOMAIN("algprimesubalg","characteristic","=",gen_0,p);
     741             : 
     742        3955 :   Z = algtablecenter(al);
     743        3955 :   nz = lg(Z)-1;
     744        3955 :   if (nz==1) return Z;
     745             : 
     746        2653 :   F = cgetg(nz+1, t_MAT);
     747       14140 :   for (i=1; i<=nz; i++) {
     748       11487 :     GEN zi = gel(Z,i);
     749       11487 :     gel(F,i) = FpC_sub(algpow(al,zi,p),zi,p);
     750             :   }
     751        2653 :   K = FpM_ker(F,p);
     752        2653 :   return gerepileupto(av, FpM_mul(Z,K,p));
     753             : }
     754             : 
     755             : 
     756             : static GEN
     757        9935 : _FpX_mul(void* D, GEN x, GEN y) { return FpX_mul(x,y,(GEN)D); }
     758             : static GEN
     759       26168 : _FpX_pow(void* D, GEN x, GEN n) { return FpX_powu(x,itos(n),(GEN)D); }
     760             : static GEN
     761       16233 : FpX_factorback(GEN fa, GEN p)
     762             : {
     763       16233 :   return gen_factorback(gel(fa,1), zv_to_ZV(gel(fa,2)), &_FpX_mul, &_FpX_pow, (void*)p);
     764             : }
     765             : 
     766             : static GEN
     767       14546 : out_decompose(GEN t, GEN Z, GEN P, GEN p)
     768             : {
     769       14546 :   GEN ali = gel(t,1), projm = gel(t,2), liftm = gel(t,3), pZ;
     770       14546 :   if (signe(p)) pZ = FpM_image(FpM_mul(projm,Z,p),p);
     771        1407 :   else          pZ = image(RgM_mul(projm,Z));
     772       14546 :   return mkvec5(ali, projm, liftm, pZ, P);
     773             : }
     774             : /* fa factorization of charpol(x) */
     775             : static GEN
     776        7315 : alg_decompose_from_facto(GEN al, GEN x, GEN fa, GEN Z, long mini)
     777             : {
     778        7315 :   long k = lgcols(fa)-1, k2 = mini? 1: k/2;
     779        7315 :   GEN v1 = rowslice(fa,1,k2);
     780        7315 :   GEN v2 = rowslice(fa,k2+1,k);
     781        7315 :   GEN alq, P, Q, p = alg_get_char(al);
     782        7315 :   dbg_printf(3)("  alg_decompose_from_facto\n");
     783        7315 :   if (signe(p)) {
     784        6594 :     P = FpX_factorback(v1, p);
     785        6594 :     Q = FpX_factorback(v2, p);
     786        6594 :     P = FpX_mul(P, FpXQ_inv(P,Q,p), p);
     787             :   }
     788             :   else {
     789         721 :     P = factorback(v1);
     790         721 :     Q = factorback(v2);
     791         721 :     P = RgX_mul(P, RgXQ_inv(P,Q));
     792             :   }
     793        7315 :   P = algpoleval(al, P, x);
     794        7315 :   if (signe(p)) Q = FpC_sub(col_ei(lg(P)-1,1), P, p);
     795         721 :   else          Q = gsub(gen_1, P);
     796        7315 :   if (gequal0(P) || gequal0(Q)) return NULL;
     797        7315 :   alq = alg_centralproj(al, mkvec2(P,Q), 1);
     798             : 
     799        7315 :   P = out_decompose(gel(alq,1), Z, P, p); if (mini) return P;
     800        7231 :   Q = out_decompose(gel(alq,2), Z, Q, p);
     801        7231 :   return mkvec2(P,Q);
     802             : }
     803             : 
     804             : static GEN
     805       11774 : random_pm1(long n)
     806             : {
     807       11774 :   GEN z = cgetg(n+1,t_VECSMALL);
     808             :   long i;
     809       11774 :   for (i = 1; i <= n; i++) z[i] = random_bits(5)%3 - 1;
     810       11774 :   return z;
     811             : }
     812             : 
     813             : static GEN alg_decompose(GEN al, GEN Z, long mini, GEN* pt_primelt);
     814             : /* Try to split al using x's charpoly. Return gen_0 if simple, NULL if failure.
     815             :  * And a splitting otherwise
     816             :  * If pt_primelt!=NULL, compute a primitive element of the center when simple */
     817             : static GEN
     818       13762 : try_fact(GEN al, GEN x, GEN zx, GEN Z, GEN Zal, long mini, GEN* pt_primelt)
     819             : {
     820       13762 :   GEN z, dec0, dec1, cp = algcharpoly(Zal,zx,0,1), fa, p = alg_get_char(al);
     821             :   long nfa, e;
     822       13762 :   dbg_printf(3)("  try_fact: zx=%Ps\n", zx);
     823       13762 :   if (signe(p)) fa = FpX_factor(cp,p);
     824        1302 :   else          fa = factor(cp);
     825       13762 :   dbg_printf(3)("  charpoly=%Ps\n", fa);
     826       13762 :   nfa = nbrows(fa);
     827       13762 :   if (nfa == 1) {
     828        6447 :     if (signe(p)) e = gel(fa,2)[1];
     829         581 :     else          e = itos(gcoeff(fa,1,2));
     830        6447 :     if (e == 1) {
     831        3689 :       if (pt_primelt != NULL) *pt_primelt = mkvec2(x, cp);
     832        3689 :       return gen_0;
     833             :     }
     834        2758 :     else return NULL;
     835             :   }
     836        7315 :   dec0 = alg_decompose_from_facto(al, x, fa, Z, mini);
     837        7315 :   if (!dec0) return NULL;
     838        7315 :   if (!mini) return dec0;
     839          84 :   dec1 = alg_decompose(gel(dec0,1), gel(dec0,4), 1, pt_primelt);
     840          84 :   z = gel(dec0,5);
     841          84 :   if (!isintzero(dec1)) {
     842          14 :     if (signe(p)) z = FpM_FpC_mul(gel(dec0,3),dec1,p);
     843           7 :     else          z = RgM_RgC_mul(gel(dec0,3),dec1);
     844             :   }
     845          84 :   return z;
     846             : }
     847             : static GEN
     848           7 : randcol(long n, GEN b)
     849             : {
     850           7 :   GEN N = addiu(shifti(b,1), 1);
     851             :   long i;
     852           7 :   GEN res =  cgetg(n+1,t_COL);
     853          63 :   for (i=1; i<=n; i++)
     854             :   {
     855          56 :     pari_sp av = avma;
     856          56 :     gel(res,i) = gerepileuptoint(av, subii(randomi(N),b));
     857             :   }
     858           7 :   return res;
     859             : }
     860             : /* Return gen_0 if already simple. mini: only returns a central idempotent
     861             :  * corresponding to one simple factor
     862             :  * if pt_primelt!=NULL, sets it to a primitive element of the center when simple */
     863             : static GEN
     864       20013 : alg_decompose(GEN al, GEN Z, long mini, GEN* pt_primelt)
     865             : {
     866             :   pari_sp av;
     867             :   GEN Zal, x, zx, rand, dec0, B, p;
     868       20013 :   long i, nz = lg(Z)-1;
     869             : 
     870       20013 :   if (nz == 1) {
     871        9009 :     if (pt_primelt != 0) *pt_primelt = mkvec2(zerocol(alg_get_dim(al)), pol_x(0));
     872        9009 :     return gen_0;
     873             :   }
     874       11004 :   p = alg_get_char(al);
     875       11004 :   dbg_printf(2)(" alg_decompose: char=%Ps, dim=%d, dim Z=%d\n", p, alg_get_absdim(al), nz);
     876       11004 :   Zal = alg_subalg(al,Z);
     877       11004 :   Z = gel(Zal,2);
     878       11004 :   Zal = gel(Zal,1);
     879       11004 :   av = avma;
     880             : 
     881       11004 :   rand = random_pm1(nz);
     882       11004 :   zx = zc_to_ZC(rand);
     883       11004 :   if (signe(p)) {
     884       10031 :     zx = FpC_red(zx,p);
     885       10031 :     x = ZM_zc_mul(Z,rand);
     886       10031 :     x = FpC_red(x,p);
     887             :   }
     888         973 :   else x = RgM_zc_mul(Z,rand);
     889       11004 :   dec0 = try_fact(al,x,zx,Z,Zal,mini,pt_primelt);
     890       11004 :   if (dec0) return dec0;
     891        2702 :   set_avma(av);
     892             : 
     893        2758 :   for (i=2; i<=nz; i++)
     894             :   {
     895        2751 :     dec0 = try_fact(al,gel(Z,i),col_ei(nz,i),Z,Zal,mini,pt_primelt);
     896        2751 :     if (dec0) return dec0;
     897          56 :     set_avma(av);
     898             :   }
     899           7 :   B = int2n(10);
     900             :   for (;;)
     901           0 :   {
     902           7 :     GEN x = randcol(nz,B), zx = ZM_ZC_mul(Z,x);
     903           7 :     dec0 = try_fact(al,x,zx,Z,Zal,mini,pt_primelt);
     904           7 :     if (dec0) return dec0;
     905           0 :     set_avma(av);
     906             :   }
     907             : }
     908             : 
     909             : static GEN
     910       16408 : alg_decompose_total(GEN al, GEN Z, long maps)
     911             : {
     912             :   GEN dec, sc, p;
     913             :   long i;
     914             : 
     915       16408 :   dec = alg_decompose(al, Z, 0, NULL);
     916       16408 :   if (isintzero(dec))
     917             :   {
     918        9177 :     if (maps) {
     919        6545 :       long n = alg_get_absdim(al);
     920        6545 :       al = mkvec3(al, matid(n), matid(n));
     921             :     }
     922        9177 :     return mkvec(al);
     923             :   }
     924        7231 :   p = alg_get_char(al); if (!signe(p)) p = NULL;
     925        7231 :   sc = cgetg(lg(dec), t_VEC);
     926       21693 :   for (i=1; i<lg(sc); i++) {
     927       14462 :     GEN D = gel(dec,i), a = gel(D,1), Za = gel(D,4);
     928       14462 :     GEN S = alg_decompose_total(a, Za, maps);
     929       14462 :     gel(sc,i) = S;
     930       14462 :     if (maps)
     931             :     {
     932       10206 :       GEN projm = gel(D,2), liftm = gel(D,3);
     933       10206 :       long j, lS = lg(S);
     934       27857 :       for (j=1; j<lS; j++)
     935             :       {
     936       17651 :         GEN Sj = gel(S,j), p2 = gel(Sj,2), l2 = gel(Sj,3);
     937       17651 :         if (p) p2 = FpM_mul(p2, projm, p);
     938          49 :         else   p2 = RgM_mul(p2, projm);
     939       17651 :         if (p) l2 = FpM_mul(liftm, l2, p);
     940          49 :         else   l2 = RgM_mul(liftm, l2);
     941       17651 :         gel(Sj,2) = p2;
     942       17651 :         gel(Sj,3) = l2;
     943             :       }
     944             :     }
     945             :   }
     946        7231 :   return shallowconcat1(sc);
     947             : }
     948             : 
     949             : static GEN
     950       11060 : alg_subalg(GEN al, GEN basis)
     951             : {
     952       11060 :   GEN invbasis, mt, p = alg_get_char(al), al2;
     953       11060 :   long i, j, n = lg(basis)-1;
     954       11060 :   if (!signe(p)) p = NULL;
     955       11060 :   basis = shallowmatconcat(mkvec2(col_ei(n,1),basis));
     956       11060 :   if (p)
     957             :   {
     958       10066 :     basis = image_keep_first(basis,p);
     959       10066 :     invbasis = FpM_inv(basis,p);
     960             :   }
     961             :   else
     962             :   {
     963             :     /* FIXME use an integral variant of image_keep_first */
     964         994 :     basis = QM_ImQ_hnf(basis);
     965         994 :     invbasis = RgM_inv(basis);
     966             :   }
     967       11060 :   mt = cgetg(n+1,t_VEC);
     968       11060 :   gel(mt,1) = matid(n);
     969       37293 :   for (i=2; i<=n; i++) {
     970       26233 :     GEN mtx = cgetg(n+1,t_MAT), x = gel(basis,i);
     971       26233 :     gel(mtx,1) = col_ei(n,i);
     972      166550 :     for (j=2; j<=n; j++) {
     973      140317 :       GEN xy = algmul(al, x, gel(basis,j));
     974      140317 :       if (p) gel(mtx,j) = FpM_FpC_mul(invbasis, xy, p);
     975       29568 :       else   gel(mtx,j) = RgM_RgC_mul(invbasis, xy);
     976             :     }
     977       26233 :     gel(mt,i) = mtx;
     978             :   }
     979       11060 :   al2 = algtableinit_i(mt,p);
     980       11060 :   al2 = mkvec2(al2,basis);
     981       11060 :   return al2;
     982             : }
     983             : 
     984             : GEN
     985          63 : algsubalg(GEN al, GEN basis)
     986             : {
     987          63 :   pari_sp av = avma;
     988             :   GEN p;
     989          63 :   checkalg(al);
     990          63 :   if (typ(basis) != t_MAT) pari_err_TYPE("algsubalg",basis);
     991          56 :   p = alg_get_char(al);
     992          56 :   if (signe(p)) basis = RgM_to_FpM(basis,p);
     993          56 :   return gerepilecopy(av, alg_subalg(al,basis));
     994             : }
     995             : 
     996             : static int
     997       11802 : cmp_algebra(GEN x, GEN y)
     998             : {
     999             :   long d;
    1000       11802 :   d = gel(x,1)[1] - gel(y,1)[1]; if (d) return d < 0? -1: 1;
    1001       10605 :   d = gel(x,1)[2] - gel(y,1)[2]; if (d) return d < 0? -1: 1;
    1002       10605 :   return cmp_universal(gel(x,2), gel(y,2));
    1003             : }
    1004             : 
    1005             : GEN
    1006        4046 : algsimpledec_ss(GEN al, long maps)
    1007             : {
    1008        4046 :   pari_sp av = avma;
    1009             :   GEN Z, p, r, res, perm;
    1010             :   long i, l, n;
    1011        4046 :   checkalg(al);
    1012        4046 :   p = alg_get_char(al);
    1013        4046 :   dbg_printf(1)("algsimpledec_ss: char=%Ps, dim=%d\n", p, alg_get_absdim(al));
    1014        4046 :   if (signe(p)) Z = algprimesubalg(al);
    1015         245 :   else          Z = algtablecenter(al);
    1016             : 
    1017        4046 :   if (lg(Z) == 2) {/* dim Z = 1 */
    1018        2100 :     n = alg_get_absdim(al);
    1019        2100 :     set_avma(av);
    1020        2100 :     if (!maps) return mkveccopy(al);
    1021        1974 :     retmkvec(mkvec3(gcopy(al), matid(n), matid(n)));
    1022             :   }
    1023        1946 :   res = alg_decompose_total(al, Z, maps);
    1024        1946 :   l = lg(res); r = cgetg(l, t_VEC);
    1025       11123 :   for (i = 1; i < l; i++)
    1026             :   {
    1027        9177 :     GEN A = maps? gmael(res,i,1): gel(res,i);
    1028        9177 :     gel(r,i) = mkvec2(mkvecsmall2(alg_get_dim(A), lg(algtablecenter(A))),
    1029             :                       alg_get_multable(A));
    1030             :   }
    1031        1946 :   perm = gen_indexsort(r, (void*)cmp_algebra, &cmp_nodata);
    1032        1946 :   return gerepilecopy(av, vecpermute(res, perm));
    1033             : }
    1034             : 
    1035             : GEN
    1036         756 : algsimpledec(GEN al, long maps)
    1037             : {
    1038         756 :   pari_sp av = avma;
    1039             :   int ss;
    1040         756 :   GEN rad, dec, res, proj=NULL, lift=NULL;
    1041         756 :   rad = algradical(al);
    1042         756 :   ss = gequal0(rad);
    1043         756 :   if (!ss)
    1044             :   {
    1045          42 :     al = alg_quotient(al, rad, maps);
    1046          42 :     if (maps) {
    1047          14 :       proj = gel(al,2);
    1048          14 :       lift = gel(al,3);
    1049          14 :       al = gel(al,1);
    1050             :     }
    1051             :   }
    1052         756 :   dec = algsimpledec_ss(al, maps);
    1053         756 :   if (!ss && maps) /* update maps */
    1054             :   {
    1055          14 :     GEN p = alg_get_char(al);
    1056             :     long i;
    1057          42 :     for (i=1; i<lg(dec); i++)
    1058             :     {
    1059          28 :       if (signe(p))
    1060             :       {
    1061          14 :         gmael(dec,i,2) = FpM_mul(gmael(dec,i,2), proj, p);
    1062          14 :         gmael(dec,i,3) = FpM_mul(lift, gmael(dec,i,3), p);
    1063             :       }
    1064             :       else
    1065             :       {
    1066          14 :         gmael(dec,i,2) = RgM_mul(gmael(dec,i,2), proj);
    1067          14 :         gmael(dec,i,3) = RgM_mul(lift, gmael(dec,i,3));
    1068             :       }
    1069             :     }
    1070             :   }
    1071         756 :   res = mkvec2(rad, dec);
    1072         756 :   return gerepilecopy(av,res);
    1073             : }
    1074             : 
    1075             : static GEN alg_idempotent(GEN al, long n, long d);
    1076             : static GEN
    1077        6482 : try_split(GEN al, GEN x, long n, long d)
    1078             : {
    1079        6482 :   GEN cp, p = alg_get_char(al), fa, e, pol, exp, P, Q, U, u, mx, mte, ire;
    1080        6482 :   long nfa, i, smalldim = alg_get_absdim(al)+1, dim, smalli = 0;
    1081        6482 :   cp = algcharpoly(al,x,0,1);
    1082        6482 :   fa = FpX_factor(cp,p);
    1083        6482 :   nfa = nbrows(fa);
    1084        6482 :   if (nfa == 1) return NULL;
    1085        3052 :   pol = gel(fa,1);
    1086        3052 :   exp = gel(fa,2);
    1087             : 
    1088             :   /* charpoly is always a d-th power */
    1089        9254 :   for (i=1; i<lg(exp); i++) {
    1090        6209 :     if (exp[i]%d) pari_err(e_MISC, "the algebra must be simple (try_split 1)");
    1091        6202 :     exp[i] /= d;
    1092             :   }
    1093        3045 :   cp = FpX_factorback(fa,p);
    1094             : 
    1095             :   /* find smallest Fp-dimension of a characteristic space */
    1096        9247 :   for (i=1; i<lg(pol); i++) {
    1097        6202 :     dim = degree(gel(pol,i))*exp[i];
    1098        6202 :     if (dim < smalldim) {
    1099        3115 :       smalldim = dim;
    1100        3115 :       smalli = i;
    1101             :     }
    1102             :   }
    1103        3045 :   i = smalli;
    1104        3045 :   if (smalldim != n) return NULL;
    1105             :   /* We could also compute e*al*e and try again with this smaller algebra */
    1106             :   /* Fq-rank 1 = Fp-rank n idempotent: success */
    1107             : 
    1108             :   /* construct idempotent */
    1109        3031 :   mx = algbasismultable(al,x);
    1110        3031 :   P = gel(pol,i);
    1111        3031 :   P = FpX_powu(P, exp[i], p);
    1112        3031 :   Q = FpX_div(cp, P, p);
    1113        3031 :   e = algpoleval(al, Q, mkvec2(x,mx));
    1114        3031 :   U = FpXQ_inv(Q, P, p);
    1115        3031 :   u = algpoleval(al, U, mkvec2(x,mx));
    1116        3031 :   e = algbasismul(al, e, u);
    1117        3031 :   mte = algbasisrightmultable(al,e);
    1118        3031 :   ire = FpM_indexrank(mte,p);
    1119        3031 :   if (lg(gel(ire,1))-1 != smalldim*d) pari_err(e_MISC, "the algebra must be simple (try_split 2)");
    1120             : 
    1121        3024 :   return mkvec3(e,mte,ire);
    1122             : }
    1123             : 
    1124             : /*
    1125             :  * Given a simple algebra al of dimension d^2 over its center of degree n,
    1126             :  * find an idempotent e in al with rank n (which is minimal).
    1127             : */
    1128             : static GEN
    1129        3038 : alg_idempotent(GEN al, long n, long d)
    1130             : {
    1131        3038 :   pari_sp av = avma;
    1132        3038 :   long i, N = alg_get_absdim(al);
    1133        3038 :   GEN e, p = alg_get_char(al), x;
    1134        6377 :   for(i=2; i<=N; i++) {
    1135        6321 :     x = col_ei(N,i);
    1136        6321 :     e = try_split(al, x, n, d);
    1137        6307 :     if (e) return e;
    1138        3339 :     set_avma(av);
    1139             :   }
    1140             :   for(;;) {
    1141         266 :     x = random_FpC(N,p);
    1142         161 :     e = try_split(al, x, n, d);
    1143         161 :     if (e) return e;
    1144         105 :     set_avma(av);
    1145             :   }
    1146             : }
    1147             : 
    1148             : static GEN
    1149        3857 : try_descend(GEN M, GEN B, GEN p, long m, long n, long d)
    1150             : {
    1151        3857 :   GEN B2 = cgetg(m+1,t_MAT), b;
    1152        3857 :   long i, j, k=0;
    1153       11011 :   for (i=1; i<=d; i++)
    1154             :   {
    1155        7154 :     k++;
    1156        7154 :     b = gel(B,i);
    1157        7154 :     gel(B2,k) = b;
    1158       17248 :     for (j=1; j<n; j++)
    1159             :     {
    1160       10094 :       k++;
    1161       10094 :       b = FpM_FpC_mul(M,b,p);
    1162       10094 :       gel(B2,k) = b;
    1163             :     }
    1164             :   }
    1165        3857 :   if (!signe(FpM_det(B2,p))) return NULL;
    1166        3437 :   return FpM_inv(B2,p);
    1167             : }
    1168             : 
    1169             : /* Given an m*m matrix M with irreducible charpoly over F of degree n,
    1170             :  * let K = F(M), which is a field, and write m=d*n.
    1171             :  * Compute the d-dimensional K-vector space structure on V=F^m induced by M.
    1172             :  * Return [B,C] where:
    1173             :  *  - B is m*d matrix over F giving a K-basis b_1,...,b_d of V
    1174             :  *  - C is d*m matrix over F[x] expressing the canonical F-basis of V on the b_i
    1175             :  * Currently F = Fp TODO extend this. */
    1176             : static GEN
    1177        3437 : descend_i(GEN M, long n, GEN p)
    1178             : {
    1179             :   GEN B, C;
    1180             :   long m,d,i;
    1181             :   pari_sp av;
    1182        3437 :   m = lg(M)-1;
    1183        3437 :   d = m/n;
    1184        3437 :   B = cgetg(d+1,t_MAT);
    1185        3437 :   av = avma;
    1186             : 
    1187             :   /* try a subset of the canonical basis */
    1188        9751 :   for (i=1; i<=d; i++)
    1189        6314 :     gel(B,i) = col_ei(m,n*(i-1)+1);
    1190        3437 :   C = try_descend(M,B,p,m,n,d);
    1191        3437 :   if (C) return mkvec2(B,C);
    1192         385 :   set_avma(av);
    1193             : 
    1194             :   /* try smallish elements */
    1195        1155 :   for (i=1; i<=d; i++)
    1196         770 :     gel(B,i) = FpC_red(zc_to_ZC(random_pm1(m)),p);
    1197         385 :   C = try_descend(M,B,p,m,n,d);
    1198         385 :   if (C) return mkvec2(B,C);
    1199          35 :   set_avma(av);
    1200             : 
    1201             :   /* try random elements */
    1202             :   for (;;)
    1203             :   {
    1204         105 :     for (i=1; i<=d; i++)
    1205          70 :       gel(B,i) = random_FpC(m,p);
    1206          35 :     C = try_descend(M,B,p,m,n,d);
    1207          35 :     if (C) return mkvec2(B,C);
    1208           0 :     set_avma(av);
    1209             :   }
    1210             : }
    1211             : static GEN
    1212       15568 : RgC_contract(GEN C, long n, long v) /* n>1 */
    1213             : {
    1214             :   GEN C2, P;
    1215             :   long m, d, i, j;
    1216       15568 :   m = lg(C)-1;
    1217       15568 :   d = m/n;
    1218       15568 :   C2 = cgetg(d+1,t_COL);
    1219       43344 :   for (i=1; i<=d; i++)
    1220             :   {
    1221       27776 :     P = pol_xn(n-1,v);
    1222      105728 :     for (j=1; j<=n; j++)
    1223       77952 :       gel(P,j+1) = gel(C,n*(i-1)+j);
    1224       27776 :     P = normalizepol(P);
    1225       27776 :     gel(C2,i) = P;
    1226             :   }
    1227       15568 :   return C2;
    1228             : }
    1229             : static GEN
    1230        3437 : RgM_contract(GEN A, long n, long v) /* n>1 */
    1231             : {
    1232        3437 :   GEN A2 = cgetg(lg(A),t_MAT);
    1233             :   long i;
    1234       19005 :   for (i=1; i<lg(A2); i++)
    1235       15568 :     gel(A2,i) = RgC_contract(gel(A,i),n,v);
    1236        3437 :   return A2;
    1237             : }
    1238             : static GEN
    1239        3437 : descend(GEN M, long n, GEN p, long v)
    1240             : {
    1241        3437 :   GEN res = descend_i(M,n,p);
    1242        3437 :   gel(res,2) = RgM_contract(gel(res,2),n,v);
    1243        3437 :   return res;
    1244             : }
    1245             : 
    1246             : /* isomorphism of Fp-vector spaces M_d(F_p^n) -> (F_p)^(d^2*n) */
    1247             : static GEN
    1248       29939 : Fq_mat2col(GEN M, long d, long n)
    1249             : {
    1250       29939 :   long N = d*d*n, i, j, k;
    1251       29939 :   GEN C = cgetg(N+1, t_COL);
    1252       90160 :   for (i=1; i<=d; i++)
    1253      191632 :     for (j=1; j<=d; j++)
    1254      400526 :       for (k=0; k<n; k++)
    1255      269115 :         gel(C,n*(d*(i-1)+j-1)+k+1) = polcoef_i(gcoeff(M,i,j),k,-1);
    1256       29939 :   return C;
    1257             : }
    1258             : 
    1259             : static GEN
    1260        3752 : alg_finite_csa_split(GEN al, long v)
    1261             : {
    1262             :   GEN Z, e, mte, ire, primelt, b, T, M, proje, lifte, extre, p, B, C, mt, mx, map, mapi, T2, ro;
    1263        3752 :   long n, d, N = alg_get_absdim(al), i;
    1264        3752 :   p = alg_get_char(al);
    1265             :   /* compute the center */
    1266        3752 :   Z = algcenter(al);
    1267             :   /* TODO option to give the center as input instead of computing it */
    1268        3752 :   n = lg(Z)-1;
    1269             : 
    1270             :   /* compute a minimal rank idempotent e */
    1271        3752 :   if (n==N) {
    1272         707 :     d = 1;
    1273         707 :     e = col_ei(N,1);
    1274         707 :     mte = matid(N);
    1275         707 :     ire = mkvec2(identity_perm(n),identity_perm(n));
    1276             :   }
    1277             :   else {
    1278        3045 :     d = usqrt(N/n);
    1279        3045 :     if (d*d*n != N) pari_err(e_MISC, "the algebra must be simple (alg_finite_csa_split 1)");
    1280        3038 :     e = alg_idempotent(al,n,d);
    1281        3024 :     mte = gel(e,2);
    1282        3024 :     ire = gel(e,3);
    1283        3024 :     e = gel(e,1);
    1284             :   }
    1285             : 
    1286             :   /* identify the center */
    1287        3731 :   if (n==1)
    1288             :   {
    1289         287 :     T = pol_x(v);
    1290         287 :     primelt = gen_0;
    1291             :   }
    1292             :   else
    1293             :   {
    1294        3444 :     b = alg_decompose(al, Z, 1, &primelt);
    1295        3444 :     if (!gequal0(b)) pari_err(e_MISC, "the algebra must be simple (alg_finite_csa_split 2)");
    1296        3437 :     T = gel(primelt,2);
    1297        3437 :     primelt = gel(primelt,1);
    1298        3437 :     setvarn(T,v);
    1299             :   }
    1300             : 
    1301             :   /* use the ffinit polynomial */
    1302        3724 :   if (n>1)
    1303             :   {
    1304        3437 :     T2 = init_Fq(p,n,v);
    1305        3437 :     setvarn(T,fetch_var_higher());
    1306        3437 :     ro = FpXQX_roots(T2,T,p);
    1307        3437 :     ro = gel(ro,1);
    1308        3437 :     primelt = algpoleval(al,ro,primelt);
    1309        3437 :     T = T2;
    1310             :   }
    1311             : 
    1312             :   /* descend al*e to a vector space over the center */
    1313             :   /* lifte: al*e -> al ; proje: al*e -> al */
    1314        3724 :   lifte = shallowextract(mte,gel(ire,2));
    1315        3724 :   extre = shallowmatextract(mte,gel(ire,1),gel(ire,2));
    1316        3724 :   extre = FpM_inv(extre,p);
    1317        3724 :   proje = rowpermute(mte,gel(ire,1));
    1318        3724 :   proje = FpM_mul(extre,proje,p);
    1319        3724 :   if (n==1)
    1320             :   {
    1321         287 :     B = lifte;
    1322         287 :     C = proje;
    1323             :   }
    1324             :   else
    1325             :   {
    1326        3437 :     M = algbasismultable(al,primelt);
    1327        3437 :     M = FpM_mul(M,lifte,p);
    1328        3437 :     M = FpM_mul(proje,M,p);
    1329        3437 :     B = descend(M,n,p,v);
    1330        3437 :     C = gel(B,2);
    1331        3437 :     B = gel(B,1);
    1332        3437 :     B = FpM_mul(lifte,B,p);
    1333        3437 :     C = FqM_mul(C,proje,T,p);
    1334             :   }
    1335             : 
    1336             :   /* compute the isomorphism */
    1337        3724 :   mt = alg_get_multable(al);
    1338        3724 :   map = cgetg(N+1,t_VEC);
    1339        3724 :   M = cgetg(N+1,t_MAT);
    1340       33663 :   for (i=1; i<=N; i++)
    1341             :   {
    1342       29939 :     mx = gel(mt,i);
    1343       29939 :     mx = FpM_mul(mx,B,p);
    1344       29939 :     mx = FqM_mul(C,mx,T,p);
    1345       29939 :     gel(map,i) = mx;
    1346       29939 :     gel(M,i) = Fq_mat2col(mx,d,n);
    1347             :   }
    1348        3724 :   mapi = FpM_inv(M,p);
    1349        3724 :   if (!mapi) pari_err(e_MISC, "the algebra must be simple (alg_finite_csa_split 3)");
    1350        3717 :   return mkvec3(T,map,mapi);
    1351             : }
    1352             : 
    1353             : GEN
    1354        3766 : algsplit(GEN al, long v)
    1355             : {
    1356        3766 :   pari_sp av = avma;
    1357             :   GEN res, T, map, mapi, ff, p;
    1358             :   long i,j,k,li,lj;
    1359        3766 :   checkalg(al);
    1360        3759 :   p = alg_get_char(al);
    1361        3759 :   if (gequal0(p))
    1362           7 :     pari_err_IMPL("splitting a characteristic 0 algebra over its center");
    1363        3752 :   res = alg_finite_csa_split(al, v);
    1364        3717 :   T = gel(res,1);
    1365        3717 :   map = gel(res,2);
    1366        3717 :   mapi = gel(res,3);
    1367        3717 :   ff = Tp_to_FF(T,p);
    1368       33593 :   for (i=1; i<lg(map); i++)
    1369             :   {
    1370       29876 :     li = lg(gel(map,i));
    1371       89908 :     for (j=1; j<li; j++)
    1372             :     {
    1373       60032 :       lj = lg(gmael(map,i,j));
    1374      190876 :       for (k=1; k<lj; k++)
    1375      130844 :         gmael3(map,i,j,k) = Fq_to_FF(gmael3(map,i,j,k),ff);
    1376             :     }
    1377             :   }
    1378             : 
    1379        3717 :   return gerepilecopy(av, mkvec2(map,mapi));
    1380             : }
    1381             : 
    1382             : /* multiplication table sanity checks */
    1383             : static GEN
    1384       36785 : check_mt(GEN mt, GEN p)
    1385             : {
    1386             :   long i, l;
    1387       36785 :   GEN MT = cgetg_copy(mt, &l);
    1388       36785 :   if (typ(MT) != t_VEC || l == 1) return NULL;
    1389      179194 :   for (i = 1; i < l; i++)
    1390             :   {
    1391      142472 :     GEN M = gel(mt,i);
    1392      142472 :     if (typ(M) != t_MAT || lg(M) != l || lgcols(M) != l) return NULL;
    1393      142451 :     if (p) M = RgM_to_FpM(M,p);
    1394      142451 :     if (i > 1 && ZC_is_ei(gel(M,1)) != i) return NULL; /* i = 1 checked at end */
    1395      142430 :     gel(MT,i) = M;
    1396             :   }
    1397       36722 :   if (!ZM_isidentity(gel(MT,1))) return NULL;
    1398       36715 :   return MT;
    1399             : }
    1400             : 
    1401             : static GEN
    1402         161 : check_relmt(GEN nf, GEN mt)
    1403             : {
    1404         161 :   long i, l = lg(mt), j, k;
    1405         161 :   GEN MT = gcopy(mt), a, b, d;
    1406         161 :   if (typ(MT) != t_VEC || l == 1) return NULL;
    1407         623 :   for (i = 1; i < l; i++)
    1408             :   {
    1409         483 :     GEN M = gel(MT,i);
    1410         483 :     if (typ(M) != t_MAT || lg(M) != l || lgcols(M) != l) return NULL;
    1411        2478 :     for (k = 1; k < l; k++)
    1412       12523 :       for (j = 1; j < l; j++)
    1413             :       {
    1414       10528 :         a = gcoeff(M,j,k);
    1415       10528 :         if (typ(a)==t_INT) continue;
    1416        1771 :         b = algtobasis(nf,a);
    1417        1771 :         d = Q_denom(b);
    1418        1771 :         if (!isint1(d))
    1419          14 :           pari_err_DOMAIN("alg_csa_table", "denominator(mt)", "!=", gen_1, mt);
    1420        1757 :         gcoeff(M,j,k) = lift(basistoalg(nf,b));
    1421             :       }
    1422         469 :     if (i > 1 && RgC_is_ei(gel(M,1)) != i) return NULL; /* i = 1 checked at end */
    1423         462 :     gel(MT,i) = M;
    1424             :   }
    1425         140 :   if (!RgM_isidentity(gel(MT,1))) return NULL;
    1426         140 :   return MT;
    1427             : }
    1428             : 
    1429             : 
    1430             : int
    1431         469 : algisassociative(GEN mt0, GEN p)
    1432             : {
    1433         469 :   pari_sp av = avma;
    1434             :   long i, j, k, n;
    1435             :   GEN M, mt;
    1436             : 
    1437         469 :   if (checkalg_i(mt0)) { p = alg_get_char(mt0); mt0 = alg_get_multable(mt0); }
    1438         469 :   if (typ(p) != t_INT) pari_err_TYPE("algisassociative",p);
    1439         462 :   mt = check_mt(mt0, isintzero(p)? NULL: p);
    1440         462 :   if (!mt) pari_err_TYPE("algisassociative (mult. table)", mt0);
    1441         413 :   n = lg(mt)-1;
    1442         413 :   M = cgetg(n+1,t_MAT);
    1443         413 :   for (j=1; j<=n; j++) gel(M,j) = cgetg(n+1,t_COL);
    1444        3402 :   for (i=1; i<=n; i++)
    1445             :   {
    1446        2989 :     GEN mi = gel(mt,i);
    1447        2989 :     for (j=1; j<=n; j++) gcoeff(M,i,j) = gel(mi,j); /* ei.ej */
    1448             :   }
    1449        2975 :   for (i=2; i<=n; i++) {
    1450        2569 :     GEN mi = gel(mt,i);
    1451       28777 :     for (j=2; j<=n; j++) {
    1452      367759 :       for (k=2; k<=n; k++) {
    1453             :         GEN x, y;
    1454      341551 :         if (signe(p)) {
    1455      242039 :           x = _tablemul_ej_Fp(mt,gcoeff(M,i,j),k,p);
    1456      242039 :           y = FpM_FpC_mul(mi,gcoeff(M,j,k),p);
    1457             :         }
    1458             :         else {
    1459       99512 :           x = _tablemul_ej(mt,gcoeff(M,i,j),k);
    1460       99512 :           y = RgM_RgC_mul(mi,gcoeff(M,j,k));
    1461             :         }
    1462             :         /* not cmp_universal: must not fail on 0 == Mod(0,2) for instance */
    1463      341551 :         if (!gequal(x,y)) return gc_bool(av,0);
    1464             :       }
    1465             :     }
    1466             :   }
    1467         406 :   return gc_bool(av,1);
    1468             : }
    1469             : 
    1470             : int
    1471         350 : algiscommutative(GEN al) /* assumes e_1 = 1 */
    1472             : {
    1473             :   long i,j,k,N,sp;
    1474             :   GEN mt,a,b,p;
    1475         350 :   checkalg(al);
    1476         350 :   if (alg_type(al) != al_TABLE) return alg_get_degree(al)==1;
    1477         308 :   N = alg_get_absdim(al);
    1478         308 :   mt = alg_get_multable(al);
    1479         308 :   p = alg_get_char(al);
    1480         308 :   sp = signe(p);
    1481        1449 :   for (i=2; i<=N; i++)
    1482        9464 :     for (j=2; j<=N; j++)
    1483       85820 :       for (k=1; k<=N; k++) {
    1484       77553 :         a = gcoeff(gel(mt,i),k,j);
    1485       77553 :         b = gcoeff(gel(mt,j),k,i);
    1486       77553 :         if (sp) {
    1487       73423 :           if (cmpii(Fp_red(a,p), Fp_red(b,p))) return 0;
    1488             :         }
    1489        4130 :         else if (gcmp(a,b)) return 0;
    1490             :       }
    1491         252 :   return 1;
    1492             : }
    1493             : 
    1494             : int
    1495         350 : algissemisimple(GEN al)
    1496             : {
    1497         350 :   pari_sp av = avma;
    1498             :   GEN rad;
    1499         350 :   checkalg(al);
    1500         350 :   if (alg_type(al) != al_TABLE) return 1;
    1501         308 :   rad = algradical(al);
    1502         308 :   set_avma(av);
    1503         308 :   return gequal0(rad);
    1504             : }
    1505             : 
    1506             : /* ss : known to be semisimple */
    1507             : int
    1508         259 : algissimple(GEN al, long ss)
    1509             : {
    1510         259 :   pari_sp av = avma;
    1511             :   GEN Z, dec, p;
    1512         259 :   checkalg(al);
    1513         259 :   if (alg_type(al) != al_TABLE) return 1;
    1514         224 :   if (!ss && !algissemisimple(al)) return 0;
    1515             : 
    1516         182 :   p = alg_get_char(al);
    1517         182 :   if (signe(p)) Z = algprimesubalg(al);
    1518          91 :   else          Z = algtablecenter(al);
    1519             : 
    1520         182 :   if (lg(Z) == 2) {/* dim Z = 1 */
    1521         105 :     set_avma(av);
    1522         105 :     return 1;
    1523             :   }
    1524          77 :   dec = alg_decompose(al, Z, 1, NULL);
    1525          77 :   set_avma(av);
    1526          77 :   return gequal0(dec);
    1527             : }
    1528             : 
    1529             : static long
    1530         329 : is_place_emb(GEN nf, GEN pl)
    1531             : {
    1532             :   long r, r1, r2;
    1533         329 :   if (typ(pl) != t_INT) pari_err_TYPE("is_place_emb", pl);
    1534         315 :   if (signe(pl)<=0) pari_err_DOMAIN("is_place_emb", "pl", "<=", gen_0, pl);
    1535         308 :   nf_get_sign(nf,&r1,&r2); r = r1+r2;
    1536         308 :   if (cmpiu(pl,r)>0) pari_err_DOMAIN("is_place_emb", "pl", ">", utoi(r), pl);
    1537         294 :   return itou(pl);
    1538             : }
    1539             : 
    1540             : static long
    1541         294 : alghasse_emb(GEN al, long emb)
    1542             : {
    1543         294 :   GEN nf = alg_get_center(al);
    1544         294 :   long r1 = nf_get_r1(nf);
    1545         294 :   return (emb <= r1)? alg_get_hasse_i(al)[emb]: 0;
    1546             : }
    1547             : 
    1548             : static long
    1549         399 : alghasse_pr(GEN al, GEN pr)
    1550             : {
    1551         399 :   GEN hf = alg_get_hasse_f(al);
    1552         399 :   long i = tablesearch(gel(hf,1), pr, &cmp_prime_ideal);
    1553         399 :   return i? gel(hf,2)[i]: 0;
    1554             : }
    1555             : 
    1556             : static long
    1557         735 : alghasse_0(GEN al, GEN pl)
    1558             : {
    1559             :   GEN pr, nf;
    1560         735 :   if (alg_type(al)== al_CSA)
    1561           7 :     pari_err_IMPL("computation of Hasse invariants over table CSA");
    1562         728 :   if ((pr = get_prid(pl))) return alghasse_pr(al, pr);
    1563         329 :   nf = alg_get_center(al);
    1564         329 :   return alghasse_emb(al, is_place_emb(nf, pl));
    1565             : }
    1566             : GEN
    1567         210 : alghasse(GEN al, GEN pl)
    1568             : {
    1569             :   long h;
    1570         210 :   checkalg(al);
    1571         210 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("alghasse [use alginit]",al);
    1572         203 :   h = alghasse_0(al,pl);
    1573         161 :   return sstoQ(h, alg_get_degree(al));
    1574             : }
    1575             : 
    1576             : /* h >= 0, d >= 0 */
    1577             : static long
    1578         812 : indexfromhasse(long h, long d) { return d/ugcd(h,d); }
    1579             : 
    1580             : long
    1581         728 : algindex(GEN al, GEN pl)
    1582             : {
    1583             :   long d, res, i, l;
    1584             :   GEN hi, hf;
    1585             : 
    1586         728 :   checkalg(al);
    1587         721 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algindex [use alginit]",al);
    1588         714 :   d = alg_get_degree(al);
    1589         714 :   if (pl) return indexfromhasse(alghasse_0(al,pl), d);
    1590             : 
    1591             :   /* else : global index */
    1592         182 :   res = 1;
    1593         182 :   hi = alg_get_hasse_i(al); l = lg(hi);
    1594         182 :   for (i=1; i<l && res!=d; i++) res = ulcm(res, indexfromhasse(hi[i],d));
    1595         182 :   hf = gel(alg_get_hasse_f(al), 2); l = lg(hf);
    1596         182 :   for (i=1; i<l && res!=d; i++) res = ulcm(res, indexfromhasse(hf[i],d));
    1597         182 :   return res;
    1598             : }
    1599             : 
    1600             : int
    1601         203 : algisdivision(GEN al, GEN pl)
    1602             : {
    1603         203 :   checkalg(al);
    1604         203 :   if (alg_type(al) == al_TABLE) {
    1605          21 :     if (!algissimple(al,0)) return 0;
    1606          14 :     if (algiscommutative(al)) return 1;
    1607           7 :     pari_err_IMPL("algisdivision for table algebras");
    1608             :   }
    1609         182 :   return algindex(al,pl) == alg_get_degree(al);
    1610             : }
    1611             : 
    1612             : int
    1613         182 : algissplit(GEN al, GEN pl)
    1614             : {
    1615         182 :   checkalg(al);
    1616         182 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algissplit [use alginit]", al);
    1617         175 :   return algindex(al,pl) == 1;
    1618             : }
    1619             : 
    1620             : int
    1621         182 : algisramified(GEN al, GEN pl)
    1622             : {
    1623         182 :   checkalg(al);
    1624         182 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algisramified [use alginit]", al);
    1625         175 :   return algindex(al,pl) != 1;
    1626             : }
    1627             : 
    1628             : GEN
    1629          91 : algramifiedplaces(GEN al)
    1630             : {
    1631          91 :   pari_sp av = avma;
    1632             :   GEN ram, hf, hi, Lpr;
    1633             :   long r1, count, i;
    1634          91 :   checkalg(al);
    1635          91 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algramifiedplaces [use alginit]", al);
    1636          84 :   r1 = nf_get_r1(alg_get_center(al));
    1637          84 :   hi = alg_get_hasse_i(al);
    1638          84 :   hf = alg_get_hasse_f(al);
    1639          84 :   Lpr = gel(hf,1);
    1640          84 :   hf = gel(hf,2);
    1641          84 :   ram = cgetg(r1+lg(Lpr), t_VEC);
    1642          84 :   count = 0;
    1643         280 :   for (i=1; i<=r1; i++)
    1644         196 :     if (hi[i]) {
    1645          91 :       count++;
    1646          91 :       gel(ram,count) = stoi(i);
    1647             :     }
    1648         287 :   for (i=1; i<lg(Lpr); i++)
    1649         203 :     if (hf[i]) {
    1650          77 :       count++;
    1651          77 :       gel(ram,count) = gel(Lpr,i);
    1652             :     }
    1653          84 :   setlg(ram, count+1);
    1654          84 :   return gerepilecopy(av, ram);
    1655             : }
    1656             : 
    1657             : /** OPERATIONS ON ELEMENTS operations.c **/
    1658             : 
    1659             : static long
    1660     1045419 : alg_model0(GEN al, GEN x)
    1661             : {
    1662     1045419 :   long t, N = alg_get_absdim(al), lx = lg(x), d, n, D, i;
    1663     1045419 :   if (typ(x) == t_MAT) return al_MATRIX;
    1664      999464 :   if (typ(x) != t_COL) return al_INVALID;
    1665      999401 :   if (N == 1) {
    1666        2667 :     if (lx != 2) return al_INVALID;
    1667        2646 :     switch(typ(gel(x,1)))
    1668             :     {
    1669        1652 :       case t_INT: case t_FRAC: return al_TRIVIAL; /* cannot distinguish basis and alg from size */
    1670         994 :       case t_POL: case t_POLMOD: return al_ALGEBRAIC;
    1671           0 :       default: return al_INVALID;
    1672             :     }
    1673             :   }
    1674             : 
    1675      996734 :   switch(alg_type(al)) {
    1676             :     case al_TABLE:
    1677      552164 :       if (lx != N+1) return al_INVALID;
    1678      552143 :       return al_BASIS;
    1679             :     case al_CYCLIC:
    1680      358526 :       d = alg_get_degree(al);
    1681      358526 :       if (lx == N+1) return al_BASIS;
    1682      100639 :       if (lx == d+1) return al_ALGEBRAIC;
    1683          14 :       return al_INVALID;
    1684             :     case al_CSA:
    1685       86044 :       D = alg_get_dim(al);
    1686       86044 :       n = nf_get_degree(alg_get_center(al));
    1687       86044 :       if (n == 1) {
    1688        1302 :         if (lx != D+1) return al_INVALID;
    1689        3871 :         for (i=1; i<=D; i++) {
    1690        3227 :           t = typ(gel(x,i));
    1691        3227 :           if (t == t_POL || t == t_POLMOD)  return al_ALGEBRAIC;
    1692             :             /* TODO t_COL for coefficients in basis form ? */
    1693             :         }
    1694         644 :         return al_BASIS;
    1695             :       }
    1696             :       else {
    1697       84742 :         if (lx == N+1) return al_BASIS;
    1698       23135 :         if (lx == D+1) return al_ALGEBRAIC;
    1699           0 :         return al_INVALID;
    1700             :       }
    1701             :   }
    1702             :   return al_INVALID; /* LCOV_EXCL_LINE */
    1703             : }
    1704             : 
    1705             : static void
    1706     1045293 : checkalgx(GEN x, long model)
    1707             : {
    1708             :   long t, i;
    1709     1045293 :   switch(model) {
    1710             :     case al_BASIS:
    1711     9209911 :       for (i=1; i<lg(x); i++) {
    1712     8337637 :         t = typ(gel(x,i));
    1713     8337637 :         if (t != t_INT && t != t_FRAC)
    1714           7 :           pari_err_TYPE("checkalgx", gel(x,i));
    1715             :       }
    1716      872274 :       return;
    1717             :     case al_TRIVIAL:
    1718             :     case al_ALGEBRAIC:
    1719      444059 :       for (i=1; i<lg(x); i++) {
    1720      317009 :         t = typ(gel(x,i));
    1721      317009 :         if (t != t_INT && t != t_FRAC && t != t_POL && t != t_POLMOD)
    1722             :           /* TODO t_COL ? */
    1723           7 :           pari_err_TYPE("checkalgx", gel(x,i));
    1724             :       }
    1725      127050 :       return;
    1726             :   }
    1727             : }
    1728             : 
    1729             : long
    1730     1045419 : alg_model(GEN al, GEN x)
    1731             : {
    1732     1045419 :   long res = alg_model0(al, x);
    1733     1045419 :   if (res == al_INVALID) pari_err_TYPE("alg_model", x);
    1734     1045293 :   checkalgx(x, res); return res;
    1735             : }
    1736             : 
    1737             : static GEN
    1738         518 : alC_add_i(GEN al, GEN x, GEN y, long lx)
    1739             : {
    1740         518 :   GEN A = cgetg(lx, t_COL);
    1741             :   long i;
    1742         518 :   for (i=1; i<lx; i++) gel(A,i) = algadd(al, gel(x,i), gel(y,i));
    1743         518 :   return A;
    1744             : }
    1745             : static GEN
    1746         280 : alM_add(GEN al, GEN x, GEN y)
    1747             : {
    1748         280 :   long lx = lg(x), l, j;
    1749             :   GEN z;
    1750         280 :   if (lg(y) != lx) pari_err_DIM("alM_add (rows)");
    1751         273 :   if (lx == 1) return cgetg(1, t_MAT);
    1752         266 :   z = cgetg(lx, t_MAT); l = lgcols(x);
    1753         266 :   if (lgcols(y) != l) pari_err_DIM("alM_add (columns)");
    1754         259 :   for (j = 1; j < lx; j++) gel(z,j) = alC_add_i(al, gel(x,j), gel(y,j), l);
    1755         259 :   return z;
    1756             : }
    1757             : GEN
    1758       36974 : algadd(GEN al, GEN x, GEN y)
    1759             : {
    1760       36974 :   pari_sp av = avma;
    1761             :   long tx, ty;
    1762             :   GEN p;
    1763       36974 :   checkalg(al);
    1764       36974 :   tx = alg_model(al,x);
    1765       36967 :   ty = alg_model(al,y);
    1766       36967 :   p = alg_get_char(al);
    1767       36967 :   if (signe(p)) return FpC_add(x,y,p);
    1768       36834 :   if (tx==ty) {
    1769       36022 :     if (tx!=al_MATRIX) return gadd(x,y);
    1770         280 :     return gerepilecopy(av, alM_add(al,x,y));
    1771             :   }
    1772         812 :   if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    1773         812 :   if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
    1774         812 :   return gerepileupto(av, gadd(x,y));
    1775             : }
    1776             : 
    1777             : GEN
    1778         147 : algneg(GEN al, GEN x) { checkalg(al); (void)alg_model(al,x); return gneg(x); }
    1779             : 
    1780             : static GEN
    1781         210 : alC_sub_i(GEN al, GEN x, GEN y, long lx)
    1782             : {
    1783             :   long i;
    1784         210 :   GEN A = cgetg(lx, t_COL);
    1785         210 :   for (i=1; i<lx; i++) gel(A,i) = algsub(al, gel(x,i), gel(y,i));
    1786         210 :   return A;
    1787             : }
    1788             : static GEN
    1789         126 : alM_sub(GEN al, GEN x, GEN y)
    1790             : {
    1791         126 :   long lx = lg(x), l, j;
    1792             :   GEN z;
    1793         126 :   if (lg(y) != lx) pari_err_DIM("alM_sub (rows)");
    1794         119 :   if (lx == 1) return cgetg(1, t_MAT);
    1795         112 :   z = cgetg(lx, t_MAT); l = lgcols(x);
    1796         112 :   if (lgcols(y) != l) pari_err_DIM("alM_sub (columns)");
    1797         105 :   for (j = 1; j < lx; j++) gel(z,j) = alC_sub_i(al, gel(x,j), gel(y,j), l);
    1798         105 :   return z;
    1799             : }
    1800             : GEN
    1801         966 : algsub(GEN al, GEN x, GEN y)
    1802             : {
    1803             :   long tx, ty;
    1804         966 :   pari_sp av = avma;
    1805             :   GEN p;
    1806         966 :   checkalg(al);
    1807         966 :   tx = alg_model(al,x);
    1808         959 :   ty = alg_model(al,y);
    1809         959 :   p = alg_get_char(al);
    1810         959 :   if (signe(p)) return FpC_sub(x,y,p);
    1811         868 :   if (tx==ty) {
    1812         546 :     if (tx != al_MATRIX) return gsub(x,y);
    1813         126 :     return gerepilecopy(av, alM_sub(al,x,y));
    1814             :   }
    1815         322 :   if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    1816         322 :   if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
    1817         322 :   return gerepileupto(av, gsub(x,y));
    1818             : }
    1819             : 
    1820             : static GEN
    1821        1659 : algalgmul_cyc(GEN al, GEN x, GEN y)
    1822             : {
    1823        1659 :   pari_sp av = avma;
    1824        1659 :   long n = alg_get_degree(al), i, k;
    1825             :   GEN xalg, yalg, res, rnf, auts, sum, b, prod, autx;
    1826        1659 :   rnf = alg_get_splittingfield(al);
    1827        1659 :   auts = alg_get_auts(al);
    1828        1659 :   b = alg_get_b(al);
    1829             : 
    1830        1659 :   xalg = cgetg(n+1, t_COL);
    1831        4935 :   for (i=0; i<n; i++)
    1832        3276 :     gel(xalg,i+1) = lift_shallow(rnfbasistoalg(rnf,gel(x,i+1)));
    1833             : 
    1834        1659 :   yalg = cgetg(n+1, t_COL);
    1835        1659 :   for (i=0; i<n; i++) gel(yalg,i+1) = rnfbasistoalg(rnf,gel(y,i+1));
    1836             : 
    1837        1659 :   res = cgetg(n+1,t_COL);
    1838        4935 :   for (k=0; k<n; k++) {
    1839        3276 :     gel(res,k+1) = gmul(gel(xalg,k+1),gel(yalg,1));
    1840        5166 :     for (i=1; i<=k; i++) {
    1841        1890 :       autx = poleval(gel(xalg,k-i+1),gel(auts,i));
    1842        1890 :       prod = gmul(autx,gel(yalg,i+1));
    1843        1890 :       gel(res,k+1) = gadd(gel(res,k+1), prod);
    1844             :     }
    1845             : 
    1846        3276 :     sum = gen_0;
    1847        5166 :     for (; i<n; i++) {
    1848        1890 :       autx = poleval(gel(xalg,k+n-i+1),gel(auts,i));
    1849        1890 :       prod = gmul(autx,gel(yalg,i+1));
    1850        1890 :       sum = gadd(sum,prod);
    1851             :     }
    1852        3276 :     sum = gmul(b,sum);
    1853             : 
    1854        3276 :     gel(res,k+1) = gadd(gel(res,k+1),sum);
    1855             :   }
    1856             : 
    1857        1659 :   return gerepilecopy(av, res);
    1858             : }
    1859             : 
    1860             : static GEN
    1861      205261 : _tablemul(GEN mt, GEN x, GEN y)
    1862             : {
    1863      205261 :   pari_sp av = avma;
    1864      205261 :   long D = lg(mt)-1, i;
    1865      205261 :   GEN res = NULL;
    1866     1927261 :   for (i=1; i<=D; i++) {
    1867     1722000 :     GEN c = gel(x,i);
    1868     1722000 :     if (!gequal0(c)) {
    1869      949606 :       GEN My = RgM_RgC_mul(gel(mt,i),y);
    1870      949606 :       GEN t = RgC_Rg_mul(My,c);
    1871      949606 :       res = res? RgC_add(res,t): t;
    1872             :     }
    1873             :   }
    1874      205261 :   if (!res) { set_avma(av); return zerocol(D); }
    1875      204358 :   return gerepileupto(av, res);
    1876             : }
    1877             : 
    1878             : static GEN
    1879      189590 : _tablemul_Fp(GEN mt, GEN x, GEN y, GEN p)
    1880             : {
    1881      189590 :   pari_sp av = avma;
    1882      189590 :   long D = lg(mt)-1, i;
    1883      189590 :   GEN res = NULL;
    1884     2235190 :   for (i=1; i<=D; i++) {
    1885     2045600 :     GEN c = gel(x,i);
    1886     2045600 :     if (signe(c)) {
    1887      325873 :       GEN My = FpM_FpC_mul(gel(mt,i),y,p);
    1888      325873 :       GEN t = FpC_Fp_mul(My,c,p);
    1889      325873 :       res = res? FpC_add(res,t,p): t;
    1890             :     }
    1891             :   }
    1892      189590 :   if (!res) { set_avma(av); return zerocol(D); }
    1893      189051 :   return gerepileupto(av, res);
    1894             : }
    1895             : 
    1896             : /* x*ej */
    1897             : static GEN
    1898       99512 : _tablemul_ej(GEN mt, GEN x, long j)
    1899             : {
    1900       99512 :   pari_sp av = avma;
    1901       99512 :   long D = lg(mt)-1, i;
    1902       99512 :   GEN res = NULL;
    1903     1561861 :   for (i=1; i<=D; i++) {
    1904     1462349 :     GEN c = gel(x,i);
    1905     1462349 :     if (!gequal0(c)) {
    1906      114023 :       GEN My = gel(gel(mt,i),j);
    1907      114023 :       GEN t = RgC_Rg_mul(My,c);
    1908      114023 :       res = res? RgC_add(res,t): t;
    1909             :     }
    1910             :   }
    1911       99512 :   if (!res) { set_avma(av); return zerocol(D); }
    1912       99372 :   return gerepileupto(av, res);
    1913             : }
    1914             : static GEN
    1915      242039 : _tablemul_ej_Fp(GEN mt, GEN x, long j, GEN p)
    1916             : {
    1917      242039 :   pari_sp av = avma;
    1918      242039 :   long D = lg(mt)-1, i;
    1919      242039 :   GEN res = NULL;
    1920     4364787 :   for (i=1; i<=D; i++) {
    1921     4122748 :     GEN c = gel(x,i);
    1922     4122748 :     if (!gequal0(c)) {
    1923      289954 :       GEN My = gel(gel(mt,i),j);
    1924      289954 :       GEN t = FpC_Fp_mul(My,c,p);
    1925      289954 :       res = res? FpC_add(res,t,p): t;
    1926             :     }
    1927             :   }
    1928      242039 :   if (!res) { set_avma(av); return zerocol(D); }
    1929      241927 :   return gerepileupto(av, res);
    1930             : }
    1931             : 
    1932             : static GEN
    1933      244153 : _tablemul_ej_Fl(GEN mt, GEN x, long j, ulong p)
    1934             : {
    1935      244153 :   pari_sp av = avma;
    1936      244153 :   long D = lg(mt)-1, i;
    1937      244153 :   GEN res = NULL;
    1938     3943604 :   for (i=1; i<=D; i++) {
    1939     3699451 :     ulong c = x[i];
    1940     3699451 :     if (c) {
    1941      384202 :       GEN My = gel(gel(mt,i),j);
    1942      384202 :       GEN t = Flv_Fl_mul(My,c, p);
    1943      384202 :       res = res? Flv_add(res,t, p): t;
    1944             :     }
    1945             :   }
    1946      244153 :   if (!res) { set_avma(av); return zero_Flv(D); }
    1947      244153 :   return gerepileupto(av, res);
    1948             : }
    1949             : 
    1950             : static GEN
    1951         686 : algalgmul_csa(GEN al, GEN x, GEN y)
    1952             : {
    1953         686 :   GEN z, nf = alg_get_center(al);
    1954             :   long i;
    1955         686 :   z = _tablemul(alg_get_relmultable(al), x, y);
    1956        2485 :   for (i=1; i<lg(z); i++)
    1957        1799 :     gel(z,i) = basistoalg(nf,gel(z,i));
    1958         686 :   return z;
    1959             : }
    1960             : 
    1961             : /* assumes x and y in algebraic form */
    1962             : static GEN
    1963        2345 : algalgmul(GEN al, GEN x, GEN y)
    1964             : {
    1965        2345 :   switch(alg_type(al))
    1966             :   {
    1967        1659 :     case al_CYCLIC: return algalgmul_cyc(al, x, y);
    1968         686 :     case al_CSA: return algalgmul_csa(al, x, y);
    1969             :   }
    1970             :   return NULL; /*LCOV_EXCL_LINE*/
    1971             : }
    1972             : 
    1973             : static GEN
    1974      394165 : algbasismul(GEN al, GEN x, GEN y)
    1975             : {
    1976      394165 :   GEN mt = alg_get_multable(al), p = alg_get_char(al);
    1977      394165 :   if (signe(p)) return _tablemul_Fp(mt, x, y, p);
    1978      204575 :   return _tablemul(mt, x, y);
    1979             : }
    1980             : 
    1981             : /* x[i,]*y. Assume lg(x) > 1 and 0 < i < lgcols(x) */
    1982             : static GEN
    1983       85001 : alMrow_alC_mul_i(GEN al, GEN x, GEN y, long i, long lx)
    1984             : {
    1985       85001 :   pari_sp av = avma;
    1986       85001 :   GEN c = algmul(al,gcoeff(x,i,1),gel(y,1)), ZERO;
    1987             :   long k;
    1988       85001 :   ZERO = zerocol(alg_get_absdim(al));
    1989      170002 :   for (k = 2; k < lx; k++)
    1990             :   {
    1991       85001 :     GEN t = algmul(al, gcoeff(x,i,k), gel(y,k));
    1992       85001 :     if (!gequal(t,ZERO)) c = algadd(al, c, t);
    1993             :   }
    1994       85001 :   return gerepilecopy(av, c);
    1995             : }
    1996             : /* return x * y, 1 < lx = lg(x), l = lgcols(x) */
    1997             : static GEN
    1998       42518 : alM_alC_mul_i(GEN al, GEN x, GEN y, long lx, long l)
    1999             : {
    2000       42518 :   GEN z = cgetg(l,t_COL);
    2001             :   long i;
    2002       42518 :   for (i=1; i<l; i++) gel(z,i) = alMrow_alC_mul_i(al,x,y,i,lx);
    2003       42518 :   return z;
    2004             : }
    2005             : static GEN
    2006       21336 : alM_mul(GEN al, GEN x, GEN y)
    2007             : {
    2008       21336 :   long j, l, lx=lg(x), ly=lg(y);
    2009             :   GEN z;
    2010       21336 :   if (ly==1) return cgetg(1,t_MAT);
    2011       21287 :   if (lx != lgcols(y)) pari_err_DIM("alM_mul");
    2012       21266 :   if (lx==1) return zeromat(0, ly-1);
    2013       21259 :   l = lgcols(x); z = cgetg(ly,t_MAT);
    2014       21259 :   for (j=1; j<ly; j++) gel(z,j) = alM_alC_mul_i(al,x,gel(y,j),lx,l);
    2015       21259 :   return z;
    2016             : }
    2017             : 
    2018             : GEN
    2019      366228 : algmul(GEN al, GEN x, GEN y)
    2020             : {
    2021      366228 :   pari_sp av = avma;
    2022             :   long tx, ty;
    2023      366228 :   checkalg(al);
    2024      366228 :   tx = alg_model(al,x);
    2025      366214 :   ty = alg_model(al,y);
    2026      366214 :   if (tx==al_MATRIX) {
    2027       20832 :     if (ty==al_MATRIX) return alM_mul(al,x,y);
    2028           7 :     pari_err_TYPE("algmul", y);
    2029             :   }
    2030      345382 :   if (signe(alg_get_char(al))) return algbasismul(al,x,y);
    2031      205002 :   if (tx==al_TRIVIAL) retmkcol(gmul(gel(x,1),gel(y,1)));
    2032      204897 :   if (tx==al_ALGEBRAIC && ty==al_ALGEBRAIC) return algalgmul(al,x,y);
    2033      203371 :   if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2034      203371 :   if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
    2035      203371 :   return gerepileupto(av,algbasismul(al,x,y));
    2036             : }
    2037             : 
    2038             : GEN
    2039       48986 : algsqr(GEN al, GEN x)
    2040             : {
    2041       48986 :   pari_sp av = avma;
    2042             :   long tx;
    2043       48986 :   checkalg(al);
    2044       48951 :   tx = alg_model(al,x);
    2045       48895 :   if (tx==al_MATRIX) return gerepilecopy(av,alM_mul(al,x,x));
    2046       48384 :   if (signe(alg_get_char(al))) return algbasismul(al,x,x);
    2047        2205 :   if (tx==al_TRIVIAL) retmkcol(gsqr(gel(x,1)));
    2048        2023 :   if (tx==al_ALGEBRAIC) return algalgmul(al,x,x);
    2049        1204 :   return gerepileupto(av,algbasismul(al,x,x));
    2050             : }
    2051             : 
    2052             : static GEN
    2053        8099 : algmtK2Z_cyc(GEN al, GEN m)
    2054             : {
    2055        8099 :   pari_sp av = avma;
    2056        8099 :   GEN nf = alg_get_abssplitting(al), res, mt, rnf = alg_get_splittingfield(al), c, dc;
    2057        8099 :   long n = alg_get_degree(al), N = nf_get_degree(nf), Nn, i, j, i1, j1;
    2058        8099 :   Nn = N*n;
    2059        8099 :   res = zeromatcopy(Nn,Nn);
    2060       38150 :   for (i=0; i<n; i++)
    2061      186242 :   for (j=0; j<n; j++) {
    2062      156191 :     c = gcoeff(m,i+1,j+1);
    2063      156191 :     if (!gequal0(c)) {
    2064       30051 :       c = rnfeltreltoabs(rnf,c);
    2065       30051 :       c = algtobasis(nf,c);
    2066       30051 :       c = Q_remove_denom(c,&dc);
    2067       30051 :       mt = zk_multable(nf,c);
    2068       30051 :       if (dc) mt = ZM_Z_div(mt,dc);
    2069      270634 :       for (i1=1; i1<=N; i1++)
    2070     2529646 :       for (j1=1; j1<=N; j1++)
    2071     2289063 :         gcoeff(res,i*N+i1,j*N+j1) = gcoeff(mt,i1,j1);
    2072             :     }
    2073             :   }
    2074        8099 :   return gerepilecopy(av,res);
    2075             : }
    2076             : 
    2077             : static GEN
    2078         861 : algmtK2Z_csa(GEN al, GEN m)
    2079             : {
    2080         861 :   pari_sp av = avma;
    2081         861 :   GEN nf = alg_get_center(al), res, mt, c, dc;
    2082         861 :   long d2 = alg_get_dim(al), n = nf_get_degree(nf), D, i, j, i1, j1;
    2083         861 :   D = d2*n;
    2084         861 :   res = zeromatcopy(D,D);
    2085        5082 :   for (i=0; i<d2; i++)
    2086       29442 :   for (j=0; j<d2; j++) {
    2087       25221 :     c = gcoeff(m,i+1,j+1);
    2088       25221 :     if (!gequal0(c)) {
    2089        3360 :       c = algtobasis(nf,c);
    2090        3360 :       c = Q_remove_denom(c,&dc);
    2091        3360 :       mt = zk_multable(nf,c);
    2092        3360 :       if (dc) mt = ZM_Z_div(mt,dc);
    2093       11550 :       for (i1=1; i1<=n; i1++)
    2094       29736 :       for (j1=1; j1<=n; j1++)
    2095       21546 :         gcoeff(res,i*n+i1,j*n+j1) = gcoeff(mt,i1,j1);
    2096             :     }
    2097             :   }
    2098         861 :   return gerepilecopy(av,res);
    2099             : }
    2100             : 
    2101             : /* assumes al is a CSA or CYCLIC */
    2102             : static GEN
    2103        8960 : algmtK2Z(GEN al, GEN m)
    2104             : {
    2105        8960 :   switch(alg_type(al))
    2106             :   {
    2107        8099 :     case al_CYCLIC: return algmtK2Z_cyc(al, m);
    2108         861 :     case al_CSA: return algmtK2Z_csa(al, m);
    2109             :   }
    2110             :   return NULL; /*LCOV_EXCL_LINE*/
    2111             : }
    2112             : 
    2113             : /* left multiplication table, as a vector space of dimension n over the splitting field (by right multiplication) */
    2114             : static GEN
    2115       10717 : algalgmultable_cyc(GEN al, GEN x)
    2116             : {
    2117       10717 :   pari_sp av = avma;
    2118       10717 :   long n = alg_get_degree(al), i, j;
    2119             :   GEN res, rnf, auts, b, pol;
    2120       10717 :   rnf = alg_get_splittingfield(al);
    2121       10717 :   auts = alg_get_auts(al);
    2122       10717 :   b = alg_get_b(al);
    2123       10717 :   pol = rnf_get_pol(rnf);
    2124             : 
    2125       10717 :   res = zeromatcopy(n,n);
    2126       46074 :   for (i=0; i<n; i++)
    2127       35357 :     gcoeff(res,i+1,1) = lift_shallow(rnfbasistoalg(rnf,gel(x,i+1)));
    2128             : 
    2129       46074 :   for (i=0; i<n; i++) {
    2130      101423 :     for (j=1; j<=i; j++)
    2131       66066 :       gcoeff(res,i+1,j+1) = gmodulo(poleval(gcoeff(res,i-j+1,1),gel(auts,j)),pol);
    2132      101423 :     for (; j<n; j++)
    2133       66066 :       gcoeff(res,i+1,j+1) = gmodulo(gmul(b,poleval(gcoeff(res,n+i-j+1,1),gel(auts,j))), pol);
    2134             :   }
    2135             : 
    2136       46074 :   for (i=0; i<n; i++)
    2137       35357 :     gcoeff(res,i+1,1) = gmodulo(gcoeff(res,i+1,1),pol);
    2138             : 
    2139       10717 :   return gerepilecopy(av, res);
    2140             : }
    2141             : 
    2142             : static GEN
    2143        1309 : elementmultable(GEN mt, GEN x)
    2144             : {
    2145        1309 :   pari_sp av = avma;
    2146        1309 :   long D = lg(mt)-1, i;
    2147        1309 :   GEN z = NULL;
    2148        7028 :   for (i=1; i<=D; i++)
    2149             :   {
    2150        5719 :     GEN c = gel(x,i);
    2151        5719 :     if (!gequal0(c))
    2152             :     {
    2153        2079 :       GEN M = RgM_Rg_mul(gel(mt,i),c);
    2154        2079 :       z = z? RgM_add(z, M): M;
    2155             :     }
    2156             :   }
    2157        1309 :   if (!z) { set_avma(av); return zeromatcopy(D,D); }
    2158        1309 :   return gerepileupto(av, z);
    2159             : }
    2160             : /* mt a t_VEC of Flm modulo m */
    2161             : static GEN
    2162       24528 : algbasismultable_Flm(GEN mt, GEN x, ulong m)
    2163             : {
    2164       24528 :   pari_sp av = avma;
    2165       24528 :   long D = lg(gel(mt,1))-1, i;
    2166       24528 :   GEN z = NULL;
    2167      268681 :   for (i=1; i<=D; i++)
    2168             :   {
    2169      244153 :     ulong c = x[i];
    2170      244153 :     if (c)
    2171             :     {
    2172       33243 :       GEN M = Flm_Fl_mul(gel(mt,i),c, m);
    2173       33243 :       z = z? Flm_add(z, M, m): M;
    2174             :     }
    2175             :   }
    2176       24528 :   if (!z) { set_avma(av); return zero_Flm(D,D); }
    2177       24528 :   return gerepileupto(av, z);
    2178             : }
    2179             : static GEN
    2180      220476 : elementabsmultable_Z(GEN mt, GEN x)
    2181             : {
    2182      220476 :   long i, l = lg(x);
    2183      220476 :   GEN z = NULL;
    2184     2360819 :   for (i = 1; i < l; i++)
    2185             :   {
    2186     2140343 :     GEN c = gel(x,i);
    2187     2140343 :     if (signe(c))
    2188             :     {
    2189      802071 :       GEN M = ZM_Z_mul(gel(mt,i),c);
    2190      802071 :       z = z? ZM_add(z, M): M;
    2191             :     }
    2192             :   }
    2193      220476 :   return z;
    2194             : }
    2195             : static GEN
    2196      114786 : elementabsmultable(GEN mt, GEN x)
    2197             : {
    2198      114786 :   GEN d, z = elementabsmultable_Z(mt, Q_remove_denom(x,&d));
    2199      114786 :   return (z && d)? ZM_Z_div(z, d): z;
    2200             : }
    2201             : static GEN
    2202      105690 : elementabsmultable_Fp(GEN mt, GEN x, GEN p)
    2203             : {
    2204      105690 :   GEN z = elementabsmultable_Z(mt, x);
    2205      105690 :   return z? FpM_red(z, p): z;
    2206             : }
    2207             : static GEN
    2208      220476 : algbasismultable(GEN al, GEN x)
    2209             : {
    2210      220476 :   pari_sp av = avma;
    2211      220476 :   GEN z, p = alg_get_char(al), mt = alg_get_multable(al);
    2212      220476 :   z = signe(p)? elementabsmultable_Fp(mt, x, p): elementabsmultable(mt, x);
    2213      220476 :   if (!z)
    2214             :   {
    2215         693 :     long D = lg(mt)-1;
    2216         693 :     set_avma(av); return zeromat(D,D);
    2217             :   }
    2218      219783 :   return gerepileupto(av, z);
    2219             : }
    2220             : 
    2221             : static GEN
    2222        1309 : algalgmultable_csa(GEN al, GEN x)
    2223             : {
    2224        1309 :   GEN nf = alg_get_center(al), m;
    2225             :   long i,j;
    2226        1309 :   m = elementmultable(alg_get_relmultable(al), x);
    2227        7028 :   for (i=1; i<lg(m); i++)
    2228       36638 :     for(j=1; j<lg(m); j++)
    2229       30919 :       gcoeff(m,i,j) = basistoalg(nf,gcoeff(m,i,j));
    2230        1309 :   return m;
    2231             : }
    2232             : 
    2233             : /* assumes x in algebraic form */
    2234             : static GEN
    2235       11732 : algalgmultable(GEN al, GEN x)
    2236             : {
    2237       11732 :   switch(alg_type(al))
    2238             :   {
    2239       10717 :     case al_CYCLIC: return algalgmultable_cyc(al, x);
    2240        1015 :     case al_CSA: return algalgmultable_csa(al, x);
    2241             :   }
    2242             :   return NULL; /*LCOV_EXCL_LINE*/
    2243             : }
    2244             : 
    2245             : /* on the natural basis */
    2246             : /* assumes x in algebraic form */
    2247             : static GEN
    2248        8960 : algZmultable(GEN al, GEN x) {
    2249        8960 :   pari_sp av = avma;
    2250        8960 :   GEN res = NULL, x0;
    2251        8960 :   long tx = alg_model(al,x);
    2252        8960 :   switch(tx) {
    2253             :     case al_TRIVIAL:
    2254           0 :       x0 = gel(x,1);
    2255           0 :       if (typ(x0)==t_POLMOD) x0 = gel(x0,2);
    2256           0 :       if (typ(x0)==t_POL) x0 = constant_coeff(x0);
    2257           0 :       res = mkmatcopy(mkcol(x0));
    2258           0 :       break;
    2259        8960 :     case al_ALGEBRAIC: res = algmtK2Z(al,algalgmultable(al,x)); break;
    2260             :   }
    2261        8960 :   return gerepileupto(av,res);
    2262             : }
    2263             : 
    2264             : /* x integral */
    2265             : static GEN
    2266       36561 : algbasisrightmultable(GEN al, GEN x)
    2267             : {
    2268       36561 :   long N = alg_get_absdim(al), i,j,k;
    2269       36561 :   GEN res = zeromatcopy(N,N), c, mt = alg_get_multable(al), p = alg_get_char(al);
    2270       36561 :   if (gequal0(p)) p = NULL;
    2271      330862 :   for (i=1; i<=N; i++) {
    2272      294301 :     c = gel(x,i);
    2273      294301 :     if (!gequal0(c)) {
    2274      872200 :       for (j=1; j<=N; j++)
    2275     7417690 :       for(k=1; k<=N; k++) {
    2276     6639682 :         if (p) gcoeff(res,k,j) = Fp_add(gcoeff(res,k,j), Fp_mul(c, gcoeff(gel(mt,j),k,i), p), p);
    2277     5014814 :         else gcoeff(res,k,j) = addii(gcoeff(res,k,j), mulii(c, gcoeff(gel(mt,j),k,i)));
    2278             :       }
    2279             :     }
    2280             :   }
    2281       36561 :   return res;
    2282             : }
    2283             : 
    2284             : /* basis for matrices : 1, E_{i,j} for (i,j)!=(1,1) */
    2285             : /* index : ijk = ((i-1)*N+j-1)*n + k */
    2286             : /* square matrices only, coefficients in basis form, shallow function */
    2287             : static GEN
    2288       20097 : algmat2basis(GEN al, GEN M)
    2289             : {
    2290       20097 :   long n = alg_get_absdim(al), N = lg(M)-1, i, j, k, ij, ijk;
    2291             :   GEN res, x;
    2292       20097 :   res = zerocol(N*N*n);
    2293       60291 :   for (i=1; i<=N; i++) {
    2294      120582 :     for (j=1, ij=(i-1)*N+1; j<=N; j++, ij++) {
    2295       80388 :       x = gcoeff(M,i,j);
    2296      660772 :       for (k=1, ijk=(ij-1)*n+1; k<=n; k++, ijk++) {
    2297      580384 :         gel(res, ijk) = gel(x, k);
    2298      580384 :         if (i>1 && i==j) gel(res, ijk) = gsub(gel(res,ijk), gel(res,k));
    2299             :       }
    2300             :     }
    2301             :   }
    2302             : 
    2303       20097 :   return res;
    2304             : }
    2305             : 
    2306             : static GEN
    2307         294 : algbasis2mat(GEN al, GEN M, long N)
    2308             : {
    2309         294 :   long n = alg_get_absdim(al), i, j, k, ij, ijk;
    2310             :   GEN res, x;
    2311         294 :   res = zeromatcopy(N,N);
    2312         882 :   for (i=1; i<=N; i++)
    2313        1764 :   for (j=1; j<=N; j++)
    2314        1176 :     gcoeff(res,i,j) = zerocol(n);
    2315             : 
    2316         882 :   for (i=1; i<=N; i++) {
    2317        1764 :     for (j=1, ij=(i-1)*N+1; j<=N; j++, ij++) {
    2318        1176 :       x = gcoeff(res,i,j);
    2319        9240 :       for (k=1, ijk=(ij-1)*n+1; k<=n; k++, ijk++) {
    2320        8064 :         gel(x,k) = gel(M,ijk);
    2321        8064 :         if (i>1 && i==j) gel(x,k) = gadd(gel(x,k), gel(M,k));
    2322             :       }
    2323             :     }
    2324             :   }
    2325             : 
    2326         294 :   return res;
    2327             : }
    2328             : 
    2329             : static GEN
    2330       20020 : algmatbasis_ei(GEN al, long ijk, long N)
    2331             : {
    2332       20020 :   long n = alg_get_absdim(al), i, j, k, ij;
    2333             :   GEN res;
    2334             : 
    2335       20020 :   res = zeromatcopy(N,N);
    2336       60060 :   for (i=1; i<=N; i++)
    2337      120120 :   for (j=1; j<=N; j++)
    2338       80080 :     gcoeff(res,i,j) = zerocol(n);
    2339             : 
    2340       20020 :   k = ijk%n;
    2341       20020 :   if (k==0) k=n;
    2342       20020 :   ij = (ijk-k)/n+1;
    2343             : 
    2344       20020 :   if (ij==1) {
    2345       15015 :     for (i=1; i<=N; i++)
    2346       10010 :       gcoeff(res,i,i) = col_ei(n,k);
    2347        5005 :     return res;
    2348             :   }
    2349             : 
    2350       15015 :   j = ij%N;
    2351       15015 :   if (j==0) j=N;
    2352       15015 :   i = (ij-j)/N+1;
    2353             : 
    2354       15015 :   gcoeff(res,i,j) = col_ei(n,k);
    2355       15015 :   return res;
    2356             : }
    2357             : 
    2358             : /* FIXME lazy implementation! */
    2359             : static GEN
    2360         777 : algleftmultable_mat(GEN al, GEN M)
    2361             : {
    2362         777 :   long N = lg(M)-1, n = alg_get_absdim(al), D = N*N*n, j;
    2363             :   GEN res, x, Mx;
    2364         777 :   if (N == 0) return cgetg(1, t_MAT);
    2365         770 :   if (N != nbrows(M)) pari_err_DIM("algleftmultable_mat (nonsquare)");
    2366         749 :   res = cgetg(D+1, t_MAT);
    2367       20769 :   for (j=1; j<=D; j++) {
    2368       20020 :     x = algmatbasis_ei(al, j, N);
    2369       20020 :     Mx = algmul(al, M, x);
    2370       20020 :     gel(res, j) = algmat2basis(al, Mx);
    2371             :   }
    2372         749 :   return res;
    2373             : }
    2374             : 
    2375             : /* left multiplication table on integral basis */
    2376             : static GEN
    2377        6951 : algleftmultable(GEN al, GEN x)
    2378             : {
    2379        6951 :   pari_sp av = avma;
    2380             :   long tx;
    2381             :   GEN res;
    2382             : 
    2383        6951 :   checkalg(al);
    2384        6951 :   tx = alg_model(al,x);
    2385        6944 :   switch(tx) {
    2386          98 :     case al_TRIVIAL : res = mkmatcopy(mkcol(gel(x,1))); break;
    2387         196 :     case al_ALGEBRAIC : x = algalgtobasis(al,x);
    2388        6328 :     case al_BASIS : res = algbasismultable(al,x); break;
    2389         518 :     case al_MATRIX : res = algleftmultable_mat(al,x); break;
    2390             :     default : return NULL; /* LCOV_EXCL_LINE */
    2391             :   }
    2392        6937 :   return gerepileupto(av,res);
    2393             : }
    2394             : 
    2395             : static GEN
    2396        4102 : algbasissplittingmatrix_csa(GEN al, GEN x)
    2397             : {
    2398        4102 :   long d = alg_get_degree(al), i, j;
    2399        4102 :   GEN rnf = alg_get_splittingfield(al), splba = alg_get_splittingbasis(al), splbainv = alg_get_splittingbasisinv(al), M;
    2400        4102 :   M = algbasismultable(al,x);
    2401        4102 :   M = RgM_mul(M, splba); /* TODO best order ? big matrix /Q vs small matrix /nf */
    2402        4102 :   M = RgM_mul(splbainv, M);
    2403       12131 :   for (i=1; i<=d; i++)
    2404       23912 :   for (j=1; j<=d; j++)
    2405       15883 :     gcoeff(M,i,j) = rnfeltabstorel(rnf, gcoeff(M,i,j));
    2406        4102 :   return M;
    2407             : }
    2408             : 
    2409             : GEN
    2410        7399 : algtomatrix(GEN al, GEN x, long abs)
    2411             : {
    2412        7399 :   pari_sp av = avma;
    2413        7399 :   GEN res = NULL;
    2414             :   long ta, tx, i, j;
    2415        7399 :   checkalg(al);
    2416        7399 :   ta = alg_type(al);
    2417        7399 :   if (abs || ta==al_TABLE) return algleftmultable(al,x);
    2418        6622 :   tx = alg_model(al,x);
    2419        6622 :   if (tx==al_MATRIX) {
    2420         469 :     if (lg(x) == 1) return cgetg(1, t_MAT);
    2421         441 :     res = zeromatcopy(nbrows(x),lg(x)-1);
    2422        1323 :     for (j=1; j<lg(x); j++)
    2423        2618 :     for (i=1; i<lgcols(x); i++)
    2424        1736 :       gcoeff(res,i,j) = algtomatrix(al,gcoeff(x,i,j),0);
    2425         441 :     res = shallowmatconcat(res);
    2426             :   }
    2427        6153 :   else switch(alg_type(al))
    2428             :   {
    2429             :     case al_CYCLIC:
    2430        2051 :       if (tx==al_BASIS) x = algbasistoalg(al,x);
    2431        2051 :       res = algalgmultable(al,x);
    2432        2051 :       break;
    2433             :     case al_CSA:
    2434        4102 :       if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2435        4102 :       res = algbasissplittingmatrix_csa(al,x);
    2436        4102 :       break;
    2437             :     default:
    2438           0 :       pari_err_DOMAIN("algtomatrix", "alg_type(al)", "=", stoi(alg_type(al)), stoi(alg_type(al)));
    2439             :   }
    2440        6594 :   return gerepilecopy(av,res);
    2441             : }
    2442             : 
    2443             : /*  x^(-1)*y, NULL if no solution */
    2444             : static GEN
    2445        1715 : algdivl_i(GEN al, GEN x, GEN y, long tx, long ty) {
    2446        1715 :   pari_sp av = avma;
    2447        1715 :   GEN res, p = alg_get_char(al), mtx;
    2448        1715 :   if (tx != ty) {
    2449         343 :     if (tx==al_ALGEBRAIC) { x = algalgtobasis(al,x); tx=al_BASIS; }
    2450         343 :     if (ty==al_ALGEBRAIC) { y = algalgtobasis(al,y); ty=al_BASIS; }
    2451             :   }
    2452        1715 :   if (ty == al_MATRIX)
    2453             :   {
    2454          77 :     if (alg_type(al) != al_TABLE) y = algalgtobasis(al,y);
    2455          77 :     y = algmat2basis(al,y);
    2456             :   }
    2457        1715 :   if (signe(p)) res = FpM_FpC_invimage(algbasismultable(al,x),y,p);
    2458             :   else
    2459             :   {
    2460        1526 :     if (ty==al_ALGEBRAIC)   mtx = algalgmultable(al,x);
    2461         819 :     else                    mtx = algleftmultable(al,x);
    2462        1526 :     res = inverseimage(mtx,y);
    2463             :   }
    2464        1715 :   if (!res || lg(res)==1) return gc_NULL(av);
    2465        1687 :   if (tx == al_MATRIX) {
    2466         294 :     res = algbasis2mat(al, res, lg(x)-1);
    2467         294 :     return gerepilecopy(av,res);
    2468             :   }
    2469        1393 :   return gerepileupto(av,res);
    2470             : }
    2471             : static GEN
    2472         721 : algdivl_i2(GEN al, GEN x, GEN y)
    2473             : {
    2474             :   long tx, ty;
    2475         721 :   checkalg(al);
    2476         721 :   tx = alg_model(al,x);
    2477         714 :   ty = alg_model(al,y);
    2478         714 :   if (tx == al_MATRIX) {
    2479         119 :     if (ty != al_MATRIX) pari_err_TYPE2("\\", x, y);
    2480         112 :     if (lg(y) == 1) return cgetg(1, t_MAT);
    2481         105 :     if (lg(x) == 1) return NULL;
    2482          98 :     if (lgcols(x) != lgcols(y)) pari_err_DIM("algdivl");
    2483          91 :     if (lg(x) != lgcols(x) || lg(y) != lgcols(y))
    2484          14 :       pari_err_DIM("algdivl (nonsquare)");
    2485             :   }
    2486         672 :   return algdivl_i(al,x,y,tx,ty);
    2487             : }
    2488             : 
    2489         672 : GEN algdivl(GEN al, GEN x, GEN y)
    2490             : {
    2491             :   GEN z;
    2492         672 :   z = algdivl_i2(al,x,y);
    2493         637 :   if (!z) pari_err_INV("algdivl", x);
    2494         623 :   return z;
    2495             : }
    2496             : 
    2497             : int
    2498          49 : algisdivl(GEN al, GEN x, GEN y, GEN* ptz)
    2499             : {
    2500          49 :   pari_sp av = avma;
    2501          49 :   GEN z = algdivl_i2(al,x,y);
    2502          49 :   if (!z) return gc_bool(av,0);
    2503          42 :   if (ptz != NULL) *ptz = z;
    2504          42 :   return 1;
    2505             : }
    2506             : 
    2507             : static GEN
    2508        1148 : alginv_i(GEN al, GEN x)
    2509             : {
    2510        1148 :   pari_sp av = avma;
    2511        1148 :   GEN res = NULL, p = alg_get_char(al);
    2512        1148 :   long tx = alg_model(al,x), n;
    2513        1127 :   switch(tx) {
    2514             :     case al_TRIVIAL :
    2515          63 :       if (signe(p)) { res = mkcol(Fp_inv(gel(x,1),p)); break; }
    2516          49 :       else          { res = mkcol(ginv(gel(x,1))); break; }
    2517             :     case al_ALGEBRAIC :
    2518         455 :       switch(alg_type(al)) {
    2519         350 :         case al_CYCLIC: n = alg_get_degree(al); break;
    2520         105 :         case al_CSA: n = alg_get_dim(al); break;
    2521             :         default: return NULL; /* LCOV_EXCL_LINE */
    2522             :       }
    2523         455 :       res = algdivl_i(al, x, col_ei(n,1), tx, al_ALGEBRAIC); break;
    2524         371 :     case al_BASIS : res = algdivl_i(al, x, col_ei(alg_get_absdim(al),1), tx,
    2525         371 :                                                             al_BASIS); break;
    2526             :     case al_MATRIX :
    2527         238 :       n = lg(x)-1;
    2528         238 :       if (n==0) return cgetg(1, t_MAT);
    2529         224 :       if (n != nbrows(x)) pari_err_DIM("alginv_i (nonsquare)");
    2530         217 :       res = algdivl_i(al, x, col_ei(n*n*alg_get_absdim(al),1), tx, al_BASIS);
    2531             :         /* cheat on type because wrong dimension */
    2532             :   }
    2533        1106 :   if (!res) return gc_NULL(av);
    2534        1092 :   return gerepilecopy(av,res);
    2535             : }
    2536             : GEN
    2537        1078 : alginv(GEN al, GEN x)
    2538             : {
    2539             :   GEN z;
    2540        1078 :   checkalg(al);
    2541        1078 :   z = alginv_i(al,x);
    2542        1050 :   if (!z) pari_err_INV("alginv", x);
    2543        1043 :   return z;
    2544             : }
    2545             : 
    2546             : int
    2547          70 : algisinv(GEN al, GEN x, GEN* ptix)
    2548             : {
    2549          70 :   pari_sp av = avma;
    2550             :   GEN ix;
    2551          70 :   checkalg(al);
    2552          70 :   ix = alginv_i(al,x);
    2553          70 :   if (!ix) return gc_bool(av,0);
    2554          63 :   if (ptix != NULL) *ptix = ix;
    2555          63 :   return 1;
    2556             : }
    2557             : 
    2558             : /*  x*y^(-1)  */
    2559             : GEN
    2560         406 : algdivr(GEN al, GEN x, GEN y) { return algmul(al, x, alginv(al, y)); }
    2561             : 
    2562       25270 : static GEN _mul(void* data, GEN x, GEN y) { return algmul((GEN)data,x,y); }
    2563       47894 : static GEN _sqr(void* data, GEN x) { return algsqr((GEN)data,x); }
    2564             : 
    2565             : static GEN
    2566          21 : algmatid(GEN al, long N)
    2567             : {
    2568          21 :   long n = alg_get_absdim(al), i, j;
    2569             :   GEN res, one, zero;
    2570             : 
    2571          21 :   res = zeromatcopy(N,N);
    2572          21 :   one = col_ei(n,1);
    2573          21 :   zero = zerocol(n);
    2574          49 :   for (i=1; i<=N; i++)
    2575          84 :   for (j=1; j<=N; j++)
    2576          56 :     gcoeff(res,i,j) = i==j ? one : zero;
    2577          21 :   return res;
    2578             : }
    2579             : 
    2580             : GEN
    2581       12278 : algpow(GEN al, GEN x, GEN n)
    2582             : {
    2583       12278 :   pari_sp av = avma;
    2584             :   GEN res;
    2585       12278 :   checkalg(al);
    2586       12278 :   switch(signe(n)) {
    2587             :     case 0 :
    2588          28 :       if (alg_model(al,x) == al_MATRIX)
    2589          21 :                         res = algmatid(al,lg(x)-1);
    2590           7 :       else              res = col_ei(alg_get_absdim(al),1);
    2591          28 :       break;
    2592       12166 :     case 1 :            res = gen_pow(x, n, (void*)al, _sqr, _mul); break;
    2593          84 :     default : /* -1 */  res = gen_pow(alginv(al,x), gneg(n), (void*)al, _sqr, _mul);
    2594             :   }
    2595       12271 :   return gerepileupto(av,res);
    2596             : }
    2597             : 
    2598             : static GEN
    2599         378 : algredcharpoly_i(GEN al, GEN x, long v)
    2600             : {
    2601         378 :   GEN rnf = alg_get_splittingfield(al);
    2602         378 :   GEN cp = charpoly(algtomatrix(al,x,0),v);
    2603         371 :   long i, m = lg(cp);
    2604         371 :   for (i=2; i<m; i++) gel(cp,i) = rnfeltdown(rnf, gel(cp,i));
    2605         371 :   return cp;
    2606             : }
    2607             : 
    2608             : /* assumes al is CSA or CYCLIC */
    2609             : static GEN
    2610         385 : algredcharpoly(GEN al, GEN x, long v)
    2611             : {
    2612         385 :   pari_sp av = avma;
    2613         385 :   long w = gvar(rnf_get_pol(alg_get_center(al)));
    2614         385 :   if (varncmp(v,w)>=0) pari_err_PRIORITY("algredcharpoly",pol_x(v),">=",w);
    2615         378 :   switch(alg_type(al))
    2616             :   {
    2617             :     case al_CYCLIC:
    2618             :     case al_CSA:
    2619         378 :       return gerepileupto(av, algredcharpoly_i(al, x, v));
    2620             :   }
    2621             :   return NULL; /*LCOV_EXCL_LINE*/
    2622             : }
    2623             : 
    2624             : static GEN
    2625       20944 : algbasischarpoly(GEN al, GEN x, long v)
    2626             : {
    2627       20944 :   pari_sp av = avma;
    2628       20944 :   GEN p = alg_get_char(al), mx;
    2629       20944 :   if (alg_model(al,x) == al_MATRIX) mx = algleftmultable_mat(al,x);
    2630       20853 :   else                              mx = algbasismultable(al,x);
    2631       20937 :   if (signe(p)) {
    2632       19061 :     GEN res = FpM_charpoly(mx,p);
    2633       19061 :     setvarn(res,v);
    2634       19061 :     return gerepileupto(av, res);
    2635             :   }
    2636        1876 :   return gerepileupto(av, charpoly(mx,v));
    2637             : }
    2638             : 
    2639             : GEN
    2640       21014 : algcharpoly(GEN al, GEN x, long v, long abs)
    2641             : {
    2642       21014 :   checkalg(al);
    2643       21014 :   if (v<0) v=0;
    2644             : 
    2645             :   /* gneg(x[1]) left on stack */
    2646       21014 :   if (alg_model(al,x) == al_TRIVIAL) {
    2647          56 :     GEN p = alg_get_char(al);
    2648          56 :     if (signe(p)) return deg1pol(gen_1,Fp_neg(gel(x,1),p),v);
    2649          42 :     return deg1pol(gen_1,gneg(gel(x,1)),v);
    2650             :   }
    2651             : 
    2652       20951 :   switch(alg_type(al)) {
    2653             :     case al_CYCLIC: case al_CSA:
    2654         490 :       if (abs)
    2655             :       {
    2656         105 :         if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2657             :       }
    2658         385 :       else return algredcharpoly(al,x,v);
    2659       20566 :     case al_TABLE: return algbasischarpoly(al,x,v);
    2660             :     default : return NULL; /* LCOV_EXCL_LINE */
    2661             :   }
    2662             : }
    2663             : 
    2664             : /* assumes x in basis form */
    2665             : static GEN
    2666      236565 : algabstrace(GEN al, GEN x)
    2667             : {
    2668      236565 :   pari_sp av = avma;
    2669      236565 :   GEN res = NULL, p = alg_get_char(al);
    2670      236565 :   if (signe(p)) return FpV_dotproduct(x, alg_get_tracebasis(al), p);
    2671       42644 :   switch(alg_model(al,x)) {
    2672          84 :     case al_TRIVIAL: return gcopy(gel(x,1)); break;
    2673       42560 :     case al_BASIS: res = RgV_dotproduct(x, alg_get_tracebasis(al)); break;
    2674             :   }
    2675       42560 :   return gerepileupto(av,res);
    2676             : }
    2677             : 
    2678             : static GEN
    2679        1372 : algredtrace(GEN al, GEN x)
    2680             : {
    2681        1372 :   pari_sp av = avma;
    2682        1372 :   GEN res = NULL;
    2683        1372 :   switch(alg_model(al,x)) {
    2684          35 :     case al_TRIVIAL: return gcopy(gel(x,1)); break;
    2685         490 :     case al_BASIS: return algredtrace(al, algbasistoalg(al,x));
    2686             :                    /* TODO precompute too? */
    2687             :     case al_ALGEBRAIC:
    2688         847 :       switch(alg_type(al))
    2689             :       {
    2690             :         case al_CYCLIC:
    2691         553 :           res = rnfelttrace(alg_get_splittingfield(al),gel(x,1));
    2692         553 :           break;
    2693             :         case al_CSA:
    2694         294 :           res = gtrace(algalgmultable_csa(al,x));
    2695         294 :           res = gdiv(res, stoi(alg_get_degree(al)));
    2696         294 :           break;
    2697             :         default: return NULL; /* LCOV_EXCL_LINE */
    2698             :       }
    2699             :   }
    2700         847 :   return gerepileupto(av,res);
    2701             : }
    2702             : 
    2703             : static GEN
    2704         308 : algtrace_mat(GEN al, GEN M, long abs) {
    2705         308 :   pari_sp av = avma;
    2706         308 :   long N = lg(M)-1, i;
    2707         308 :   GEN res, p = alg_get_char(al);
    2708         308 :   if (N == 0) return gen_0;
    2709         294 :   if (N != nbrows(M)) pari_err_DIM("algtrace_mat (nonsquare)");
    2710             : 
    2711         287 :   if (!signe(p)) p = NULL;
    2712         287 :   res = algtrace(al, gcoeff(M,1,1), abs);
    2713         574 :   for (i=2; i<=N; i++) {
    2714         287 :     if (p)  res = Fp_add(res, algtrace(al,gcoeff(M,i,i),abs), p);
    2715         280 :     else    res = gadd(res, algtrace(al,gcoeff(M,i,i),abs));
    2716             :   }
    2717         287 :   if (abs || alg_type(al) == al_TABLE) res = gmulgs(res, N); /* absolute trace */
    2718         287 :   return gerepileupto(av, res);
    2719             : }
    2720             : 
    2721             : GEN
    2722        1519 : algtrace(GEN al, GEN x, long abs)
    2723             : {
    2724        1519 :   checkalg(al);
    2725        1519 :   if (alg_model(al,x) == al_MATRIX) return algtrace_mat(al,x,abs);
    2726        1211 :   switch(alg_type(al)) {
    2727             :     case al_CYCLIC: case al_CSA:
    2728        1078 :       if (!abs) return algredtrace(al,x);
    2729         196 :       if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2730         329 :     case al_TABLE: return algabstrace(al,x);
    2731             :     default : return NULL; /* LCOV_EXCL_LINE */
    2732             :   }
    2733             : }
    2734             : 
    2735             : static GEN
    2736       40971 : ZM_trace(GEN x)
    2737             : {
    2738       40971 :   long i, lx = lg(x);
    2739             :   GEN t;
    2740       40971 :   if (lx < 3) return lx == 1? gen_0: gcopy(gcoeff(x,1,1));
    2741       40173 :   t = gcoeff(x,1,1);
    2742       40173 :   for (i = 2; i < lx; i++) t = addii(t, gcoeff(x,i,i));
    2743       40173 :   return t;
    2744             : }
    2745             : static GEN
    2746      126708 : FpM_trace(GEN x, GEN p)
    2747             : {
    2748      126708 :   long i, lx = lg(x);
    2749             :   GEN t;
    2750      126708 :   if (lx < 3) return lx == 1? gen_0: gcopy(gcoeff(x,1,1));
    2751      119008 :   t = gcoeff(x,1,1);
    2752      119008 :   for (i = 2; i < lx; i++) t = Fp_add(t, gcoeff(x,i,i), p);
    2753      119008 :   return t;
    2754             : }
    2755             : 
    2756             : static GEN
    2757       38801 : algtracebasis(GEN al)
    2758             : {
    2759       38801 :   pari_sp av = avma;
    2760       38801 :   GEN mt = alg_get_multable(al), p = alg_get_char(al);
    2761       38801 :   long i, l = lg(mt);
    2762       38801 :   GEN v = cgetg(l, t_VEC);
    2763       38801 :   if (signe(p)) for (i=1; i < l; i++) gel(v,i) = FpM_trace(gel(mt,i), p);
    2764        5418 :   else          for (i=1; i < l; i++) gel(v,i) = ZM_trace(gel(mt,i));
    2765       38801 :   return gerepileupto(av,v);
    2766             : }
    2767             : 
    2768             : /* Assume: i > 0, expo := p^i <= absdim, x contained in I_{i-1} given by mult
    2769             :  * table modulo modu=p^(i+1). Return Tr(x^(p^i)) mod modu */
    2770             : static ulong
    2771       24528 : algtracei(GEN mt, ulong p, ulong expo, ulong modu)
    2772             : {
    2773       24528 :   pari_sp av = avma;
    2774       24528 :   long j, l = lg(mt);
    2775       24528 :   ulong tr = 0;
    2776       24528 :   mt = Flm_powu(mt,expo,modu);
    2777       24528 :   for (j=1; j<l; j++) tr += ucoeff(mt,j,j);
    2778       24528 :   return gc_ulong(av, (tr/expo) % p);
    2779             : }
    2780             : 
    2781             : GEN
    2782         952 : algnorm(GEN al, GEN x, long abs)
    2783             : {
    2784         952 :   pari_sp av = avma;
    2785             :   long tx;
    2786             :   GEN p, rnf, res, mx;
    2787         952 :   checkalg(al);
    2788         952 :   p = alg_get_char(al);
    2789         952 :   tx = alg_model(al,x);
    2790         952 :   if (signe(p)) {
    2791          21 :     if (tx == al_MATRIX)    mx = algleftmultable_mat(al,x);
    2792          14 :     else                    mx = algbasismultable(al,x);
    2793          21 :     return gerepileupto(av, FpM_det(mx,p));
    2794             :   }
    2795         931 :   if (tx == al_TRIVIAL) return gcopy(gel(x,1));
    2796             : 
    2797         889 :   switch(alg_type(al)) {
    2798             :     case al_CYCLIC: case al_CSA:
    2799         819 :       if (abs)
    2800             :       {
    2801         196 :         if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2802             :       }
    2803             :       else
    2804             :       {
    2805         623 :         rnf = alg_get_splittingfield(al);
    2806         623 :         res = rnfeltdown(rnf, det(algtomatrix(al,x,0)));
    2807         616 :         break;
    2808             :       }
    2809             :     case al_TABLE:
    2810         266 :       if (tx == al_MATRIX)  mx = algleftmultable_mat(al,x);
    2811         105 :       else                  mx = algbasismultable(al,x);
    2812         259 :       res = det(mx);
    2813         259 :       break;
    2814             :     default: return NULL; /* LCOV_EXCL_LINE */
    2815             :   }
    2816         875 :   return gerepileupto(av, res);
    2817             : }
    2818             : 
    2819             : static GEN
    2820       48356 : algalgtonat_cyc(GEN al, GEN x)
    2821             : {
    2822       48356 :   pari_sp av = avma;
    2823       48356 :   GEN nf = alg_get_abssplitting(al), rnf = alg_get_splittingfield(al), res, c;
    2824       48356 :   long n = alg_get_degree(al), N = nf_get_degree(nf), i, i1;
    2825       48356 :   res = zerocol(N*n);
    2826      148246 :   for (i=0; i<n; i++) {
    2827       99890 :     c = gel(x,i+1);
    2828       99890 :     c = rnfeltreltoabs(rnf,c);
    2829       99890 :     if (!gequal0(c)) {
    2830       81214 :       c = algtobasis(nf,c);
    2831       81214 :       for (i1=1; i1<=N; i1++) gel(res,i*N+i1) = gel(c,i1);
    2832             :     }
    2833             :   }
    2834       48356 :   return gerepilecopy(av, res);
    2835             : }
    2836             : 
    2837             : static GEN
    2838       11256 : algalgtonat_csa(GEN al, GEN x)
    2839             : {
    2840       11256 :   pari_sp av = avma;
    2841       11256 :   GEN nf = alg_get_center(al), res, c;
    2842       11256 :   long d2 = alg_get_dim(al), n = nf_get_degree(nf), i, i1;
    2843       11256 :   res = zerocol(d2*n);
    2844       56133 :   for (i=0; i<d2; i++) {
    2845       44877 :     c = gel(x,i+1);
    2846       44877 :     if (!gequal0(c)) {
    2847       31318 :       c = algtobasis(nf,c);
    2848       31318 :       for (i1=1; i1<=n; i1++) gel(res,i*n+i1) = gel(c,i1);
    2849             :     }
    2850             :   }
    2851       11256 :   return gerepilecopy(av, res);
    2852             : }
    2853             : 
    2854             : /* assumes al CSA or CYCLIC */
    2855             : static GEN
    2856       59612 : algalgtonat(GEN al, GEN x)
    2857             : {
    2858       59612 :   switch(alg_type(al))
    2859             :   {
    2860       48356 :     case al_CYCLIC: return algalgtonat_cyc(al, x);
    2861       11256 :     case al_CSA: return algalgtonat_csa(al, x);
    2862             :   }
    2863             :   return NULL; /*LCOV_EXCL_LINE*/
    2864             : }
    2865             : 
    2866             : static GEN
    2867       10381 : algnattoalg_cyc(GEN al, GEN x)
    2868             : {
    2869       10381 :   pari_sp av = avma;
    2870       10381 :   GEN nf = alg_get_abssplitting(al), rnf = alg_get_splittingfield(al), res, c;
    2871       10381 :   long n = alg_get_degree(al), N = nf_get_degree(nf), i, i1;
    2872       10381 :   res = zerocol(n);
    2873       10381 :   c = zerocol(N);
    2874       44926 :   for (i=0; i<n; i++) {
    2875       34545 :     for (i1=1; i1<=N; i1++) gel(c,i1) = gel(x,i*N+i1);
    2876       34545 :     gel(res,i+1) = rnfeltabstorel(rnf,basistoalg(nf,c));
    2877             :   }
    2878       10381 :   return gerepilecopy(av, res);
    2879             : }
    2880             : 
    2881             : static GEN
    2882        1225 : algnattoalg_csa(GEN al, GEN x)
    2883             : {
    2884        1225 :   pari_sp av = avma;
    2885        1225 :   GEN nf = alg_get_center(al), res, c;
    2886        1225 :   long d2 = alg_get_dim(al), n = nf_get_degree(nf), i, i1;
    2887        1225 :   res = zerocol(d2);
    2888        1225 :   c = zerocol(n);
    2889        6608 :   for (i=0; i<d2; i++) {
    2890        5383 :     for (i1=1; i1<=n; i1++) gel(c,i1) = gel(x,i*n+i1);
    2891        5383 :     gel(res,i+1) = basistoalg(nf,c);
    2892             :   }
    2893        1225 :   return gerepilecopy(av, res);
    2894             : }
    2895             : 
    2896             : /* assumes al CSA or CYCLIC */
    2897             : static GEN
    2898       11606 : algnattoalg(GEN al, GEN x)
    2899             : {
    2900       11606 :   switch(alg_type(al))
    2901             :   {
    2902       10381 :     case al_CYCLIC: return algnattoalg_cyc(al, x);
    2903        1225 :     case al_CSA: return algnattoalg_csa(al, x);
    2904             :   }
    2905             :   return NULL; /*LCOV_EXCL_LINE*/
    2906             : }
    2907             : 
    2908             : static GEN
    2909         182 : algalgtobasis_mat(GEN al, GEN x) /* componentwise */
    2910             : {
    2911         182 :   pari_sp av = avma;
    2912             :   long lx, lxj, i, j;
    2913             :   GEN res;
    2914         182 :   lx = lg(x);
    2915         182 :   res = cgetg(lx, t_MAT);
    2916         546 :   for (j=1; j<lx; j++) {
    2917         364 :     lxj = lg(gel(x,j));
    2918         364 :     gel(res,j) = cgetg(lxj, t_COL);
    2919        1092 :     for (i=1; i<lxj; i++)
    2920         728 :       gcoeff(res,i,j) = algalgtobasis(al,gcoeff(x,i,j));
    2921             :   }
    2922         182 :   return gerepilecopy(av,res);
    2923             : }
    2924             : GEN
    2925       60067 : algalgtobasis(GEN al, GEN x)
    2926             : {
    2927             :   pari_sp av;
    2928             :   long tx;
    2929       60067 :   checkalg(al);
    2930       60067 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algalgtobasis [use alginit]", al);
    2931       60053 :   tx = alg_model(al,x);
    2932       60053 :   if (tx==al_BASIS) return gcopy(x);
    2933       59794 :   if (tx==al_MATRIX) return algalgtobasis_mat(al,x);
    2934       59612 :   av = avma;
    2935       59612 :   x = algalgtonat(al,x);
    2936       59612 :   x = RgM_RgC_mul(alg_get_invbasis(al),x);
    2937       59612 :   return gerepileupto(av, x);
    2938             : }
    2939             : 
    2940             : static GEN
    2941         119 : algbasistoalg_mat(GEN al, GEN x) /* componentwise */
    2942             : {
    2943         119 :   long j, lx = lg(x);
    2944         119 :   GEN res = cgetg(lx, t_MAT);
    2945         357 :   for (j=1; j<lx; j++) {
    2946         238 :     long i, lxj = lg(gel(x,j));
    2947         238 :     gel(res,j) = cgetg(lxj, t_COL);
    2948         238 :     for (i=1; i<lxj; i++) gcoeff(res,i,j) = algbasistoalg(al,gcoeff(x,i,j));
    2949             :   }
    2950         119 :   return res;
    2951             : }
    2952             : GEN
    2953        2912 : algbasistoalg(GEN al, GEN x)
    2954             : {
    2955             :   pari_sp av;
    2956             :   long tx;
    2957        2912 :   checkalg(al);
    2958        2912 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algbasistoalg [use alginit]", al);
    2959        2898 :   tx = alg_model(al,x);
    2960        2898 :   if (tx==al_ALGEBRAIC) return gcopy(x);
    2961        2765 :   if (tx==al_MATRIX) return algbasistoalg_mat(al,x);
    2962        2646 :   av = avma;
    2963        2646 :   x = RgM_RgC_mul(alg_get_basis(al),x);
    2964        2646 :   x = algnattoalg(al,x);
    2965        2646 :   return gerepileupto(av, x);
    2966             : }
    2967             : 
    2968             : GEN
    2969       18305 : algrandom(GEN al, GEN b)
    2970             : {
    2971             :   GEN res, p, N;
    2972             :   long i, n;
    2973       18305 :   if (typ(b) != t_INT) pari_err_TYPE("algrandom",b);
    2974       18298 :   if (signe(b)<0) pari_err_DOMAIN("algrandom", "b", "<", gen_0, b);
    2975       18291 :   checkalg(al);
    2976       18284 :   n = alg_get_absdim(al);
    2977       18284 :   N = addiu(shifti(b,1), 1); /* left on stack */
    2978       18284 :   p = alg_get_char(al); if (!signe(p)) p = NULL;
    2979       18284 :   res = cgetg(n+1,t_COL);
    2980      163828 :   for (i=1; i<= n; i++)
    2981             :   {
    2982      145544 :     pari_sp av = avma;
    2983      145544 :     GEN t = subii(randomi(N),b);
    2984      145544 :     if (p) t = modii(t, p);
    2985      145544 :     gel(res,i) = gerepileuptoint(av, t);
    2986             :   }
    2987       18284 :   return res;
    2988             : }
    2989             : 
    2990             : /* Assumes pol has coefficients in the same ring as the COL x; x either
    2991             :  * in basis or algebraic form or [x,mx] where mx is the mult. table of x.
    2992             :  TODO more general version: pol with coeffs in center and x in basis form */
    2993             : GEN
    2994       17080 : algpoleval(GEN al, GEN pol, GEN x)
    2995             : {
    2996       17080 :   pari_sp av = avma;
    2997       17080 :   GEN p, mx = NULL, res;
    2998             :   long i;
    2999       17080 :   checkalg(al);
    3000       17080 :   p = alg_get_char(al);
    3001       17080 :   if (typ(pol) != t_POL) pari_err_TYPE("algpoleval", pol);
    3002       17073 :   if (typ(x) == t_VEC)
    3003             :   {
    3004        6097 :     if (lg(x) != 3) pari_err_TYPE("algpoleval [vector must be of length 2]", x);
    3005        6090 :     mx = gel(x,2);
    3006        6090 :     x = gel(x,1);
    3007        6090 :     if (typ(mx)!=t_MAT || !gequal(x,gel(mx,1)))
    3008          21 :       pari_err_TYPE("algpoleval [mx must be the multiplication table of x]", mx);
    3009             :   }
    3010             :   else
    3011             :   {
    3012       10976 :     switch(alg_model(al,x))
    3013             :     {
    3014          14 :       case al_ALGEBRAIC: mx = algalgmultable(al,x); break;
    3015       10934 :       case al_BASIS: if (!RgX_is_QX(pol))
    3016           7 :         pari_err_IMPL("algpoleval with x in basis form and pol not in Q[x]");
    3017       10941 :       case al_TRIVIAL: mx = algbasismultable(al,x); break;
    3018           7 :       default: pari_err_TYPE("algpoleval", x);
    3019             :     }
    3020             :   }
    3021       17024 :   res = zerocol(lg(mx)-1);
    3022       17024 :   if (signe(p)) {
    3023       63922 :     for (i=lg(pol)-1; i>1; i--)
    3024             :     {
    3025       47696 :       gel(res,1) = Fp_add(gel(res,1), gel(pol,i), p);
    3026       47696 :       if (i>2) res = FpM_FpC_mul(mx, res, p);
    3027             :     }
    3028             :   }
    3029             :   else {
    3030        4886 :     for (i=lg(pol)-1; i>1; i--)
    3031             :     {
    3032        4088 :       gel(res,1) = gadd(gel(res,1), gel(pol,i));
    3033        4088 :       if (i>2) res = RgM_RgC_mul(mx, res);
    3034             :     }
    3035             :   }
    3036       17024 :   return gerepileupto(av, res);
    3037             : }
    3038             : 
    3039             : /** GRUNWALD-WANG **/
    3040             : /*
    3041             : Song Wang's PhD thesis (pdf pages)
    3042             : p.25 definition of chi_b. K^Ker(chi_b) = K(b^(1/m))
    3043             : p.26 bound on the conductor (also Cohen adv. GTM 193 p.166)
    3044             : p.21 & p.34 description special case, also on wikipedia:
    3045             : http://en.wikipedia.org/wiki/Grunwald%E2%80%93Wang_theorem#Special_fields
    3046             : p.77 Kummer case
    3047             : */
    3048             : 
    3049             : /* n > 0. Is n = 2^k ? */
    3050             : static int
    3051         154 : uispow2(ulong n) { return !(n &(n-1)); }
    3052             : 
    3053             : static GEN
    3054         175 : get_phi0(GEN bnr, GEN Lpr, GEN Ld, GEN pl, long *pr, long *pn)
    3055             : {
    3056         175 :   const long NTRY = 10; /* FIXME: magic constant */
    3057         175 :   const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    3058         175 :   GEN S = bnr_get_cyc(bnr);
    3059             :   GEN Sst, G, globGmod, loc, X, Rglob, Rloc, H, U, Lconj;
    3060             :   long i, j, r, nbfrob, nbloc, nz, t;
    3061             : 
    3062         175 :   *pn = n;
    3063         175 :   *pr = r = lg(S)-1;
    3064         175 :   if (!r) return NULL;
    3065         154 :   Lconj = NULL;
    3066         154 :   nbloc = nbfrob = lg(Lpr)-1;
    3067         154 :   if (uispow2(n))
    3068             :   {
    3069          84 :     long l = lg(pl), k = 1;
    3070          84 :     GEN real = cgetg(l, t_VECSMALL);
    3071         210 :     for (i=1; i<l; i++)
    3072         126 :       if (pl[i]==-1) real[k++] = i;
    3073          84 :     if (k > 1)
    3074             :     {
    3075          84 :       GEN nf = bnr_get_nf(bnr), I = bid_get_fact(bnr_get_bid(bnr));
    3076          84 :       GEN v, y, C = idealchineseinit(bnr, I);
    3077          84 :       long r1 = nf_get_r1(nf), n = nbrows(I);
    3078          84 :       nbloc += k-1;
    3079          84 :       Lconj = cgetg(k, t_VEC);
    3080          84 :       v = const_vecsmall(r1,1);
    3081          84 :       y = const_vec(n, gen_1);
    3082         210 :       for (i = 1; i < k; i++)
    3083             :       {
    3084         126 :         v[i] = -1; gel(Lconj,i) = idealchinese(nf,mkvec2(C,v),y);
    3085         126 :         v[i] = 1;
    3086             :       }
    3087             :     }
    3088             :   }
    3089             : 
    3090             :   /* compute Z/n-dual */
    3091         154 :   Sst = cgetg(r+1, t_VECSMALL);
    3092         154 :   for (i=1; i<=r; i++) Sst[i] = ugcdiu(gel(S,i), n);
    3093         154 :   if (Sst[1] != n) return NULL;
    3094             : 
    3095         154 :   globGmod = cgetg(r+1,t_MAT);
    3096         154 :   G = cgetg(r+1,t_VECSMALL);
    3097         336 :   for (i=1; i<=r; i++)
    3098             :   {
    3099         182 :     G[i] = n / Sst[i]; /* pairing between S and Sst */
    3100         182 :     gel(globGmod,i) = cgetg(nbloc+1,t_VECSMALL);
    3101             :   }
    3102             : 
    3103             :   /* compute images of Frobenius elements (and complex conjugation) */
    3104         154 :   loc = cgetg(nbloc+1,t_VECSMALL);
    3105         490 :   for (i=1; i<=nbloc; i++) {
    3106             :     long L;
    3107         350 :     if (i<=nbfrob)
    3108             :     {
    3109         224 :       X = gel(Lpr,i);
    3110         224 :       L = Ld[i];
    3111             :     }
    3112             :     else
    3113             :     { /* X = 1 (mod f), sigma_i(x) < 0, positive at all other real places */
    3114         126 :       X = gel(Lconj,i-nbfrob);
    3115         126 :       L = 2;
    3116             :     }
    3117         350 :     X = ZV_to_Flv(isprincipalray(bnr,X), n);
    3118         868 :     for (nz=0,j=1; j<=r; j++)
    3119             :     {
    3120         518 :       ulong c = (X[j] * G[j]) % L;
    3121         518 :       ucoeff(globGmod,i,j) = c;
    3122         518 :       if (c) nz = 1;
    3123             :     }
    3124         350 :     if (!nz) return NULL;
    3125         336 :     loc[i] = L;
    3126             :   }
    3127             : 
    3128             :   /* try some random elements in the dual */
    3129         140 :   Rglob = cgetg(r+1,t_VECSMALL);
    3130         441 :   for (t=0; t<NTRY; t++) {
    3131         434 :     for (j=1; j<=r; j++) Rglob[j] = random_Fl(Sst[j]);
    3132         434 :     Rloc = zm_zc_mul(globGmod,Rglob);
    3133         917 :     for (i=1; i<=nbloc; i++)
    3134         784 :       if (Rloc[i] % loc[i] == 0) break;
    3135         434 :     if (i > nbloc)
    3136         133 :       return zv_to_ZV(Rglob);
    3137             :   }
    3138             : 
    3139             :   /* try to realize some random elements of the product of the local duals */
    3140           7 :   H = ZM_hnfall_i(shallowconcat(zm_to_ZM(globGmod),
    3141             :                                 diagonal_shallow(zv_to_ZV(loc))), &U, 2);
    3142             :   /* H,U nbloc x nbloc */
    3143           7 :   Rloc = cgetg(nbloc+1,t_COL);
    3144          77 :   for (t=0; t<NTRY; t++) {
    3145             :     /* nonzero random coordinate */ /* TODO add special case ? */
    3146          70 :     for (i=1; i<=nbloc; i++) gel(Rloc,i) = stoi(1 + random_Fl(loc[i]-1));
    3147          70 :     Rglob = hnf_invimage(H, Rloc);
    3148          70 :     if (Rglob)
    3149             :     {
    3150           0 :       Rglob = ZM_ZC_mul(U,Rglob);
    3151           0 :       return vecslice(Rglob,1,r);
    3152             :     }
    3153             :   }
    3154           7 :   return NULL;
    3155             : }
    3156             : 
    3157             : static GEN
    3158         175 : bnrgwsearch(GEN bnr, GEN Lpr, GEN Ld, GEN pl)
    3159             : {
    3160         175 :   pari_sp av = avma;
    3161             :   long n, r;
    3162         175 :   GEN phi0 = get_phi0(bnr,Lpr,Ld,pl, &r,&n), gn, v, H,U;
    3163         175 :   if (!phi0) { set_avma(av); return gen_0; }
    3164         133 :   gn = stoi(n);
    3165             :   /* compute kernel of phi0 */
    3166         133 :   v = ZV_extgcd(shallowconcat(phi0, gn));
    3167         133 :   U = vecslice(gel(v,2), 1,r);
    3168         133 :   H = ZM_hnfmodid(rowslice(U, 1,r), gn);
    3169         133 :   return gerepileupto(av, H);
    3170             : }
    3171             : 
    3172             : GEN
    3173         133 : bnfgwgeneric(GEN bnf, GEN Lpr, GEN Ld, GEN pl, long var)
    3174             : {
    3175         133 :   pari_sp av = avma;
    3176         133 :   const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    3177             :   forprime_t S;
    3178         133 :   GEN bnr = NULL, ideal = gen_1, nf, dec, H = gen_0, finf, pol;
    3179             :   ulong ell, p;
    3180             :   long deg, i, degell;
    3181         133 :   (void)uisprimepower(n, &ell);
    3182         133 :   nf = bnf_get_nf(bnf);
    3183         133 :   deg = nf_get_degree(nf);
    3184         133 :   degell = ugcd(deg,ell-1);
    3185         133 :   finf = cgetg(lg(pl),t_VEC);
    3186         133 :   for (i=1; i<lg(pl); i++) gel(finf,i) = pl[i]==-1 ? gen_1 : gen_0;
    3187             : 
    3188         133 :   u_forprime_init(&S, 2, ULONG_MAX);
    3189         665 :   while ((p = u_forprime_next(&S))) {
    3190         532 :     if (Fl_powu(p % ell, degell, ell) != 1) continue; /* ell | p^deg-1 ? */
    3191         238 :     dec = idealprimedec(nf, utoipos(p));
    3192         392 :     for (i=1; i<lg(dec); i++) {
    3193         287 :       GEN pp = gel(dec,i);
    3194         287 :       if (RgV_isin(Lpr,pp)) continue;
    3195             :         /* TODO also accept the prime ideals at which there is a condition
    3196             :          * (use local Artin)? */
    3197         231 :       if (smodis(idealnorm(nf,pp),ell) != 1) continue; /* ell | N(pp)-1 ? */
    3198         175 :       ideal = idealmul(bnf,ideal,pp);
    3199             :       /* TODO: give factorization ? */
    3200         175 :       bnr = Buchray(bnf, mkvec2(ideal,finf), nf_INIT);
    3201         175 :       H = bnrgwsearch(bnr,Lpr,Ld,pl);
    3202         175 :       if (H != gen_0)
    3203             :       {
    3204         133 :         pol = rnfkummer(bnr,H,0,nf_get_prec(nf));
    3205         133 :         setvarn(pol, var);
    3206         133 :         return gerepileupto(av,pol);
    3207             :       }
    3208             :     }
    3209             :   }
    3210             :   pari_err_BUG("bnfgwgeneric (no suitable p)"); /*LCOV_EXCL_LINE*/
    3211             :   return NULL;/*LCOV_EXCL_LINE*/
    3212             : }
    3213             : 
    3214             : /* no garbage collection */
    3215             : static GEN
    3216         231 : localextdeg(GEN nf, GEN pr, GEN cnd, long d, long ell, long n)
    3217             : {
    3218         231 :   long g = n/d;
    3219         231 :   GEN res, modpr, ppr = pr, T, p, gen, k;
    3220         231 :   if (d==1) return gen_1;
    3221         210 :   if (equalsi(ell,pr_get_p(pr))) { /* ell == p */
    3222          14 :     res = nfadd(nf, gen_1, pr_get_gen(pr));
    3223          14 :     res = nfpowmodideal(nf, res, stoi(g), cnd);
    3224             :   }
    3225             :   else { /* ell != p */
    3226         196 :     k = powis(stoi(ell),Z_lval(subiu(pr_norm(pr),1),ell));
    3227         196 :     k = divis(k,g);
    3228         196 :     modpr = nf_to_Fq_init(nf, &ppr, &T, &p);
    3229         196 :     (void)Fq_sqrtn(gen_1,k,T,p,&gen);
    3230         196 :     res = Fq_to_nf(gen, modpr);
    3231             :   }
    3232         210 :   return res;
    3233             : }
    3234             : 
    3235             : /* Ld[i] must be nontrivial powers of the same prime ell */
    3236             : /* pl : -1 at real places at which the extention must ramify, 0 elsewhere */
    3237             : GEN
    3238         161 : nfgwkummer(GEN nf, GEN Lpr, GEN Ld, GEN pl, long var)
    3239             : {
    3240         161 :   const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    3241         161 :   pari_sp av = avma;
    3242             :   ulong ell;
    3243             :   long i, v;
    3244             :   GEN cnd, y, x, pol;
    3245         161 :   v = uisprimepower(n, &ell);
    3246         161 :   cnd = zeromatcopy(lg(Lpr)-1,2);
    3247             : 
    3248         161 :   y = vec_ei(lg(Lpr)-1,1);
    3249         392 :   for (i=1; i<lg(Lpr); i++) {
    3250         231 :     GEN pr = gel(Lpr,i), p = pr_get_p(pr), E;
    3251         231 :     long e = pr_get_e(pr);
    3252         231 :     gcoeff(cnd,i,1) = pr;
    3253             : 
    3254         231 :     if (!absequalui(ell,p))
    3255         210 :       E = gen_1;
    3256             :     else
    3257          21 :       E = addui(1 + v*e, divsi(e,subiu(p,1)));
    3258         231 :     gcoeff(cnd,i,2) = E;
    3259         231 :     gel(y,i) = localextdeg(nf, pr, idealpow(nf,pr,E), Ld[i], ell, n);
    3260             :   }
    3261             : 
    3262             :   /* TODO use a factoredextchinese to ease computations afterwards ? */
    3263         161 :   x = idealchinese(nf, mkvec2(cnd,pl), y);
    3264         161 :   x = basistoalg(nf,x);
    3265         161 :   pol = gsub(gpowgs(pol_x(var),n),x);
    3266             : 
    3267         161 :   return gerepileupto(av,pol);
    3268             : }
    3269             : 
    3270             : static GEN
    3271         693 : get_vecsmall(GEN v)
    3272             : {
    3273         693 :   switch(typ(v))
    3274             :   {
    3275         581 :     case t_VECSMALL: return v;
    3276         105 :     case t_VEC: if (RgV_is_ZV(v)) return ZV_to_zv(v);
    3277             :   }
    3278           7 :   pari_err_TYPE("nfgrunwaldwang",v);
    3279             :   return NULL;/*LCOV_EXCL_LINE*/
    3280             : }
    3281             : GEN
    3282         392 : nfgrunwaldwang(GEN nf0, GEN Lpr, GEN Ld, GEN pl, long var)
    3283             : {
    3284             :   ulong n;
    3285         392 :   pari_sp av = avma;
    3286             :   GEN nf, bnf, pr;
    3287             :   long t, w, i, vnf;
    3288             :   ulong ell, ell2;
    3289         392 :   if (var < 0) var = 0;
    3290         392 :   nf = get_nf(nf0,&t);
    3291         392 :   if (!nf) pari_err_TYPE("nfgrunwaldwang",nf0);
    3292         392 :   vnf = nf_get_varn(nf);
    3293         392 :   if (varncmp(var, vnf) >= 0)
    3294           7 :     pari_err_PRIORITY("nfgrunwaldwang", pol_x(var), ">=", vnf);
    3295         385 :   if (typ(Lpr) != t_VEC) pari_err_TYPE("nfgrunwaldwang",Lpr);
    3296         371 :   if (lg(Lpr) != lg(Ld)) pari_err_DIM("nfgrunwaldwang [#Lpr != #Ld]");
    3297         833 :   for (i=1; i<lg(Lpr); i++) {
    3298         476 :     pr = gel(Lpr,i);
    3299         476 :     if (nf_get_degree(nf)==1 && typ(pr)==t_INT)
    3300          63 :       gel(Lpr,i) = gel(idealprimedec(nf,pr), 1);
    3301         413 :     else checkprid(pr);
    3302             :   }
    3303         357 :   if (lg(pl)-1 != nf_get_r1(nf))
    3304           7 :     pari_err_DOMAIN("nfgrunwaldwang [pl should have r1 components]", "#pl",
    3305           7 :         "!=", stoi(nf_get_r1(nf)), stoi(lg(pl)-1));
    3306             : 
    3307         350 :   Ld = get_vecsmall(Ld);
    3308         343 :   pl = get_vecsmall(pl);
    3309         343 :   bnf = get_bnf(nf0,&t);
    3310         343 :   n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    3311             : 
    3312         343 :   if (!uisprimepower(n, &ell))
    3313           7 :     pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (a)");
    3314         770 :   for (i=1; i<lg(Ld); i++)
    3315         441 :     if (Ld[i]!=1 && (!uisprimepower(Ld[i],&ell2) || ell2!=ell))
    3316           7 :       pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (b)");
    3317         770 :   for (i=1; i<lg(pl); i++)
    3318         448 :     if (pl[i]==-1 && ell%2)
    3319           7 :       pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (c)");
    3320             : 
    3321         322 :   w = bnf? bnf_get_tuN(bnf): itos(gel(rootsof1(nf),1));
    3322             : 
    3323             :   /* TODO choice between kummer and generic ? Let user choose between speed
    3324             :    * and size */
    3325         322 :   if (w%n==0 && lg(Ld)>1)
    3326         161 :     return gerepileupto(av,nfgwkummer(nf,Lpr,Ld,pl,var));
    3327         161 :   if (ell==n) {
    3328         133 :     if (!bnf) bnf = Buchall(nf,0,0);
    3329         133 :     return gerepileupto(av,bnfgwgeneric(bnf,Lpr,Ld,pl,var));
    3330             :   }
    3331             :   else {
    3332          28 :     pari_err_IMPL("nfgrunwaldwang for non-prime degree");
    3333             :     set_avma(av); return gen_0; /*LCOV_EXCL_LINE*/
    3334             :   }
    3335             : }
    3336             : 
    3337             : /** HASSE INVARIANTS **/
    3338             : 
    3339             : /* TODO long -> ulong + uel */
    3340             : static GEN
    3341         917 : hasseconvert(GEN H, long n)
    3342             : {
    3343             :   GEN h, c;
    3344             :   long i, l;
    3345         917 :   switch(typ(H)) {
    3346             :     case t_VEC:
    3347         847 :       l = lg(H); h = cgetg(l,t_VECSMALL);
    3348         847 :       if (l == 1) return h;
    3349         749 :       c = gel(H,1);
    3350         749 :       if (typ(c) == t_VEC && l == 3)
    3351         287 :         return mkvec2(gel(H,1),hasseconvert(gel(H,2),n));
    3352        1225 :       for (i=1; i<l; i++)
    3353             :       {
    3354         791 :         c = gel(H,i);
    3355         791 :         switch(typ(c)) {
    3356         567 :           case t_INT:  break;
    3357             :           case t_INTMOD:
    3358           7 :             c = gel(c,2); break;
    3359             :           case t_FRAC :
    3360         196 :             c = gmulgs(c,n);
    3361         196 :             if (typ(c) == t_INT) break;
    3362           7 :             pari_err_DOMAIN("hasseconvert [degree should be a denominator of the invariant]", "denom(h)", "ndiv", stoi(n), Q_denom(gel(H,i)));
    3363          21 :           default : pari_err_TYPE("Hasse invariant", c);
    3364             :         }
    3365         763 :         h[i] = smodis(c,n);
    3366             :       }
    3367         434 :       return h;
    3368          63 :     case t_VECSMALL: return H;
    3369             :   }
    3370           7 :   pari_err_TYPE("Hasse invariant", H); return NULL;
    3371             : }
    3372             : 
    3373             : /* assume f >= 2 */
    3374             : static long
    3375         385 : cyclicrelfrob0(GEN nf, GEN aut, GEN pr, GEN q, long f, long g)
    3376             : {
    3377         385 :   pari_sp av = avma;
    3378             :   long s;
    3379             :   GEN T, p, modpr, a, b;
    3380             : 
    3381         385 :   modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3382         385 :   a = pol_x(nf_get_varn(nf));
    3383         385 :   b = galoisapply(nf, aut, modpr_genFq(modpr));
    3384         385 :   b = nf_to_Fq(nf, b, modpr);
    3385         385 :   for (s=0; !ZX_equal(a, b); s++) a = Fq_pow(a, q, T, p);
    3386         385 :   set_avma(av);
    3387         385 :   return g*Fl_inv(s, f);/* <n */
    3388             : }
    3389             : 
    3390             : static GEN
    3391        1064 : rnfprimedec(GEN rnf, GEN pr)
    3392        1064 : { return idealfactor(obj_check(rnf,rnf_NFABS), rnfidealup0(rnf, pr, 1)); }
    3393             : 
    3394             : static long
    3395         952 : cyclicrelfrob(GEN rnf, GEN auts, GEN pr)
    3396             : {
    3397         952 :   pari_sp av = avma;
    3398         952 :   long f,g,frob, n = rnf_get_degree(rnf);
    3399         952 :   GEN fa = rnfprimedec(rnf, pr);
    3400             : 
    3401         952 :   if (cmpis(gcoeff(fa,1,2), 1) > 0)
    3402           0 :     pari_err_DOMAIN("cyclicrelfrob","e(PR/pr)",">",gen_1,pr);
    3403         952 :   g = nbrows(fa);
    3404         952 :   f = n/g;
    3405             : 
    3406         952 :   if (f <= 2) frob = g%n;
    3407             :   else {
    3408         385 :     GEN nf2, PR = gcoeff(fa,1,1);
    3409         385 :     GEN autabs = rnfeltreltoabs(rnf,gel(auts,g));
    3410         385 :     nf2 = obj_check(rnf,rnf_NFABS);
    3411         385 :     autabs = nfadd(nf2, autabs, gmul(rnf_get_k(rnf), rnf_get_alpha(rnf)));
    3412         385 :     frob = cyclicrelfrob0(nf2, autabs, PR, pr_norm(pr), f, g);
    3413             :   }
    3414         952 :   set_avma(av); return frob;
    3415             : }
    3416             : 
    3417             : static long
    3418         553 : localhasse(GEN rnf, GEN cnd, GEN pl, GEN auts, GEN b, long k)
    3419             : {
    3420         553 :   pari_sp av = avma;
    3421             :   long v, m, h, lfa, frob, n, i;
    3422             :   GEN previous, y, pr, nf, q, fa;
    3423         553 :   nf = rnf_get_nf(rnf);
    3424         553 :   n = rnf_get_degree(rnf);
    3425         553 :   pr = gcoeff(cnd,k,1);
    3426         553 :   v = nfval(nf, b, pr);
    3427         553 :   m = lg(cnd)>1 ? nbrows(cnd) : 0;
    3428             : 
    3429             :   /* add the valuation of b to the conductor... */
    3430         553 :   previous = gcoeff(cnd,k,2);
    3431         553 :   gcoeff(cnd,k,2) = addis(previous, v);
    3432             : 
    3433         553 :   y = const_vec(m, gen_1);
    3434         553 :   gel(y,k) = b;
    3435             :   /* find a factored element y congruent to b mod pr^(vpr(b)+vpr(cnd)) and to 1 mod the conductor. */
    3436         553 :   y = factoredextchinese(nf, cnd, y, pl, &fa);
    3437         553 :   h = 0;
    3438         553 :   lfa = nbrows(fa);
    3439             :   /* sum of all Hasse invariants of (rnf/nf,aut,y) is 0, Hasse invariants at q!=pr are easy, Hasse invariant at pr is the same as for al=(rnf/nf,aut,b). */
    3440        1057 :   for (i=1; i<=lfa; i++) {
    3441         504 :     q = gcoeff(fa,i,1);
    3442         504 :     if (cmp_prime_ideal(pr,q)) {
    3443         469 :       frob = cyclicrelfrob(rnf, auts, q);
    3444         469 :       frob = Fl_mul(frob,umodiu(gcoeff(fa,i,2),n),n);
    3445         469 :       h = Fl_add(h,frob,n);
    3446             :     }
    3447             :   }
    3448             :   /* ...then restore it. */
    3449         553 :   gcoeff(cnd,k,2) = previous;
    3450             : 
    3451         553 :   set_avma(av); return Fl_neg(h,n);
    3452             : }
    3453             : 
    3454             : static GEN
    3455         700 : allauts(GEN rnf, GEN aut)
    3456             : {
    3457         700 :   long n = rnf_get_degree(rnf), i;
    3458         700 :   GEN pol = rnf_get_pol(rnf), vaut;
    3459         700 :   if (n==1) n=2;
    3460         700 :   vaut = cgetg(n,t_VEC);
    3461         700 :   aut = lift_shallow(rnfbasistoalg(rnf,aut));
    3462         700 :   gel(vaut,1) = aut;
    3463        1008 :   for (i=1; i<n-1; i++)
    3464         308 :     gel(vaut,i+1) = RgX_rem(poleval(gel(vaut,i), aut), pol);
    3465         700 :   return vaut;
    3466             : }
    3467             : 
    3468             : static GEN
    3469         224 : clean_factor(GEN fa)
    3470             : {
    3471         224 :   GEN P2,E2, P = gel(fa,1), E = gel(fa,2);
    3472         224 :   long l = lg(P), i, j = 1;
    3473         224 :   P2 = cgetg(l, t_COL);
    3474         224 :   E2 = cgetg(l, t_COL);
    3475         609 :   for (i = 1;i < l; i++)
    3476         385 :     if (signe(gel(E,i))) {
    3477         252 :       gel(P2,j) = gel(P,i);
    3478         252 :       gel(E2,j) = gel(E,i); j++;
    3479             :     }
    3480         224 :   setlg(P2,j);
    3481         224 :   setlg(E2,j); return mkmat2(P2,E2);
    3482             : }
    3483             : 
    3484             : /* shallow concat x[1],...x[nx],y[1], ... y[ny], returning a t_COL. To be
    3485             :  * used when we do not know whether x,y are t_VEC or t_COL */
    3486             : static GEN
    3487         448 : colconcat(GEN x, GEN y)
    3488             : {
    3489         448 :   long i, lx = lg(x), ly = lg(y);
    3490         448 :   GEN z=cgetg(lx+ly-1, t_COL);
    3491         448 :   for (i=1; i<lx; i++) z[i]     = x[i];
    3492         448 :   for (i=1; i<ly; i++) z[lx+i-1]= y[i];
    3493         448 :   return z;
    3494             : }
    3495             : 
    3496             : /* return v(x) at all primes in listpr, replace x by cofactor */
    3497             : static GEN
    3498         924 : nfmakecoprime(GEN nf, GEN *px, GEN listpr)
    3499             : {
    3500         924 :   long j, l = lg(listpr);
    3501         924 :   GEN x1, x = *px, L = cgetg(l, t_COL);
    3502             : 
    3503         924 :   if (typ(x) != t_MAT)
    3504             :   { /* scalar, divide at the end (fast valuation) */
    3505         819 :     x1 = NULL;
    3506        1708 :     for (j=1; j<l; j++)
    3507             :     {
    3508         889 :       GEN pr = gel(listpr,j), e;
    3509         889 :       long v = nfval(nf, x, pr);
    3510         889 :       e = stoi(v); gel(L,j) = e;
    3511        1057 :       if (v) x1 = x1? idealmulpowprime(nf, x1, pr, e)
    3512         168 :                     : idealpow(nf, pr, e);
    3513             :     }
    3514         819 :     if (x1) x = idealdivexact(nf, idealhnf(nf,x), x1);
    3515             :   }
    3516             :   else
    3517             :   { /* HNF, divide as we proceed (reduce size) */
    3518         119 :     for (j=1; j<l; j++)
    3519             :     {
    3520          14 :       GEN pr = gel(listpr,j);
    3521          14 :       long v = idealval(nf, x, pr);
    3522          14 :       gel(L,j) = stoi(v);
    3523          14 :       if (v) x = idealmulpowprime(nf, x, pr, stoi(-v));
    3524             :     }
    3525             :   }
    3526         924 :   *px = x; return L;
    3527             : }
    3528             : 
    3529             : /* Caveat: factorizations are not sorted wrt cmp_prime_ideal: Lpr comes first */
    3530             : static GEN
    3531         224 : computecnd(GEN rnf, GEN Lpr)
    3532             : {
    3533             :   GEN id, nf, fa, Le, P,E;
    3534         224 :   long n = rnf_get_degree(rnf);
    3535             : 
    3536         224 :   nf = rnf_get_nf(rnf);
    3537         224 :   id = rnf_get_idealdisc(rnf);
    3538         224 :   Le = nfmakecoprime(nf, &id, Lpr);
    3539         224 :   fa = idealfactor(nf, id); /* part of D_{L/K} coprime with Lpr */
    3540         224 :   P =  colconcat(Lpr,gel(fa,1));
    3541         224 :   E =  colconcat(Le, gel(fa,2));
    3542         224 :   fa = mkmat2(P, gdiventgs(E, eulerphiu(n)));
    3543         224 :   return mkvec2(fa, clean_factor(fa));
    3544             : }
    3545             : 
    3546             : /* h >= 0 */
    3547             : static void
    3548           0 : nextgen(GEN gene, long h, GEN* gens, GEN* hgens, long* ngens, long* curgcd) {
    3549           0 :   long nextgcd = ugcd(h,*curgcd);
    3550           0 :   if (nextgcd == *curgcd) return;
    3551           0 :   (*ngens)++;
    3552           0 :   gel(*gens,*ngens) = gene;
    3553           0 :   gel(*hgens,*ngens) = utoi(h);
    3554           0 :   *curgcd = nextgcd;
    3555           0 :   return;
    3556             : }
    3557             : 
    3558             : static int
    3559           0 : dividesmod(long d, long h, long n) { return !(h%cgcd(d,n)); }
    3560             : 
    3561             : /* ramified prime with nontrivial Hasse invariant */
    3562             : static GEN
    3563           0 : localcomplete(GEN rnf, GEN pl, GEN cnd, GEN auts, long j, long n, long h, long* v)
    3564             : {
    3565             :   GEN nf, gens, hgens, pr, modpr, T, p, sol, U, D, b, gene, randg, pu;
    3566             :   long ngens, i, d, np, k, d1, d2, hg, dnf, vcnd, curgcd;
    3567           0 :   nf = rnf_get_nf(rnf);
    3568           0 :   pr = gcoeff(cnd,j,1);
    3569           0 :   np = umodiu(pr_norm(pr), n);
    3570           0 :   dnf = nf_get_degree(nf);
    3571           0 :   vcnd = itos(gcoeff(cnd,j,2));
    3572           0 :   ngens = 13+dnf;
    3573           0 :   gens = zerovec(ngens);
    3574           0 :   hgens = zerovec(ngens);
    3575           0 :   *v = 0;
    3576           0 :   curgcd = 0;
    3577           0 :   ngens = 0;
    3578             : 
    3579           0 :   if (!uisprime(n)) {
    3580           0 :     gene =  pr_get_gen(pr);
    3581           0 :     hg = localhasse(rnf, cnd, pl, auts, gene, j);
    3582           0 :     nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
    3583             :   }
    3584             : 
    3585           0 :   if (ugcd(np,n) != 1) { /* GCD(Np,n) != 1 */
    3586           0 :     pu = idealprincipalunits(nf,pr,vcnd);
    3587           0 :     pu = abgrp_get_gen(pu);
    3588           0 :     for (i=1; i<lg(pu) && !dividesmod(curgcd,h,n); i++) {
    3589           0 :       gene = gel(pu,i);
    3590           0 :       hg = localhasse(rnf, cnd, pl, auts, gene, j);
    3591           0 :       nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
    3592             :     }
    3593             :   }
    3594             : 
    3595           0 :   d = ugcd(np-1,n);
    3596           0 :   if (d != 1) { /* GCD(Np-1,n) != 1 */
    3597           0 :     modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    3598           0 :     while (!dividesmod(curgcd,h,n)) { /* TODO gener_FpXQ_local */
    3599           0 :       if (T==NULL) randg = randomi(p);
    3600           0 :       else randg = random_FpX(degpol(T), varn(T),p);
    3601             : 
    3602           0 :       if (!gequal0(randg) && !gequal1(randg)) {
    3603           0 :         gene = Fq_to_nf(randg, modpr);
    3604           0 :         hg = localhasse(rnf, cnd, pl, auts, gene, j);
    3605           0 :         nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
    3606             :       }
    3607             :     }
    3608             :   }
    3609             : 
    3610           0 :   setlg(gens,ngens+1);
    3611           0 :   setlg(hgens,ngens+1);
    3612             : 
    3613           0 :   sol = ZV_extgcd(hgens);
    3614           0 :   D = gel(sol,1);
    3615           0 :   U = gmael(sol,2,ngens);
    3616             : 
    3617           0 :   b = gen_1;
    3618           0 :   d = itou(D);
    3619           0 :   d1 = ugcd(d,n);
    3620           0 :   d2 = d/d1;
    3621           0 :   d = ((h/d1)*Fl_inv(d2,n))%n;
    3622           0 :   for (i=1; i<=ngens; i++) {
    3623           0 :     k = (itos(gel(U,i))*d)%n;
    3624           0 :     if (k<0) k = n-k;
    3625           0 :     if (k) b = nfmul(nf, b, nfpow_u(nf, gel(gens,i),k));
    3626           0 :     if (i==1) *v = k;
    3627             :   }
    3628           0 :   return b;
    3629             : }
    3630             : 
    3631             : static int
    3632         259 : testsplits(GEN data, GEN b, GEN fa)
    3633             : {
    3634             :   GEN rnf, fapr, forbid, P, E;
    3635             :   long i, n;
    3636         259 :   if (gequal0(b)) return 0;
    3637         259 :   P = gel(fa,1);
    3638         259 :   E = gel(fa,2);
    3639         259 :   rnf = gel(data,1);
    3640         259 :   forbid = gel(data,2);
    3641         259 :   n = rnf_get_degree(rnf);
    3642         350 :   for (i=1; i<lgcols(fa); i++) {
    3643         126 :     GEN pr = gel(P,i);
    3644             :     long g;
    3645         126 :     if (tablesearch(forbid, pr, &cmp_prime_ideal)) return 0;
    3646         112 :     fapr = rnfprimedec(rnf,pr);
    3647         112 :     g = nbrows(fapr);
    3648         112 :     if ((itos(gel(E,i))*g)%n) return 0;
    3649             :   }
    3650         224 :   return 1;
    3651             : }
    3652             : 
    3653             : /* remove entries with Hasse invariant 0 */
    3654             : static GEN
    3655         476 : hassereduce(GEN hf)
    3656             : {
    3657         476 :   GEN pr,h, PR = gel(hf,1), H = gel(hf,2);
    3658         476 :   long i, j, l = lg(PR);
    3659             : 
    3660         476 :   pr= cgetg(l, t_VEC);
    3661         476 :   h = cgetg(l, t_VECSMALL);
    3662        1099 :   for (i = j = 1; i < l; i++)
    3663         623 :     if (H[i]) {
    3664         294 :       gel(pr,j) = gel(PR,i);
    3665         294 :       h[j] = H[i]; j++;
    3666             :     }
    3667         476 :   setlg(pr,j);
    3668         476 :   setlg(h,j); return mkvec2(pr,h);
    3669             : }
    3670             : 
    3671             : /* v vector of prid. Return underlying list of rational primes */
    3672             : static GEN
    3673         623 : pr_primes(GEN v)
    3674             : {
    3675         623 :   long i, l = lg(v);
    3676         623 :   GEN w = cgetg(l,t_VEC);
    3677         623 :   for (i=1; i<l; i++) gel(w,i) = pr_get_p(gel(v,i));
    3678         623 :   return ZV_sort_uniq(w);
    3679             : }
    3680             : 
    3681             : /* rnf complete */
    3682             : static GEN
    3683         224 : alg_complete0(GEN rnf, GEN aut, GEN hf, GEN hi, long maxord)
    3684             : {
    3685         224 :   pari_sp av = avma;
    3686             :   GEN nf, pl, pl2, cnd, prcnd, cnds, y, Lpr, auts, b, fa, data, hfe;
    3687             :   GEN forbid, al;
    3688             :   long D, n, d, i, j;
    3689         224 :   nf = rnf_get_nf(rnf);
    3690         224 :   n = rnf_get_degree(rnf);
    3691         224 :   d = nf_get_degree(nf);
    3692         224 :   D = d*n*n;
    3693         224 :   checkhasse(nf,hf,hi,n);
    3694         224 :   hf = hassereduce(hf);
    3695         224 :   Lpr = gel(hf,1);
    3696         224 :   hfe = gel(hf,2);
    3697             : 
    3698         224 :   auts = allauts(rnf,aut);
    3699             : 
    3700         224 :   pl = gcopy(hi); /* conditions on the final b */
    3701         224 :   pl2 = gcopy(hi); /* conditions for computing local Hasse invariants */
    3702         497 :   for (i=1; i<lg(pl); i++) {
    3703         273 :     if (hi[i]) { pl[i] = -1; pl2[i] = 1; }
    3704         196 :     else if (!rnfrealdec(rnf,i)) { pl[i] = 1; pl2[i] = 1; }
    3705             :   }
    3706             : 
    3707         224 :   cnds = computecnd(rnf,Lpr);
    3708         224 :   prcnd = gel(cnds,1);
    3709         224 :   cnd = gel(cnds,2);
    3710         224 :   y = cgetg(lgcols(prcnd),t_VEC);
    3711         224 :   forbid = vectrunc_init(lg(Lpr));
    3712         357 :   for (i=j=1; i<lg(Lpr); i++)
    3713             :   {
    3714         133 :     GEN pr = gcoeff(prcnd,i,1), yi;
    3715         133 :     long v, e = itou( gcoeff(prcnd,i,2) );
    3716         133 :     if (!e) {
    3717         133 :       long frob = cyclicrelfrob(rnf,auts,pr), f1 = ugcd(frob,n);
    3718         133 :       vectrunc_append(forbid, pr);
    3719         133 :       yi = gen_0;
    3720         133 :       v = ((hfe[i]/f1) * Fl_inv(frob/f1,n)) % n;
    3721             :     }
    3722             :     else
    3723           0 :       yi = localcomplete(rnf, pl2, cnd, auts, j++, n, hfe[i], &v);
    3724         133 :     gel(y,i) = yi;
    3725         133 :     gcoeff(prcnd,i,2) = stoi(e + v);
    3726             :   }
    3727         224 :   for (; i<lgcols(prcnd); i++) gel(y,i) = gen_1;
    3728         224 :   gen_sort_inplace(forbid, (void*)&cmp_prime_ideal, &cmp_nodata, NULL);
    3729         224 :   data = mkvec2(rnf,forbid);
    3730         224 :   b = factoredextchinesetest(nf,prcnd,y,pl,&fa,data,testsplits);
    3731             : 
    3732         224 :   al = cgetg(12, t_VEC);
    3733         224 :   gel(al,10)= gen_0; /* must be set first */
    3734         224 :   gel(al,1) = rnf;
    3735         224 :   gel(al,2) = auts;
    3736         224 :   gel(al,3) = basistoalg(nf,b);
    3737         224 :   gel(al,4) = hi;
    3738             :   /* add primes | disc or b with trivial Hasse invariant to hf */
    3739         224 :   Lpr = gel(prcnd,1); y = b;
    3740         224 :   (void)nfmakecoprime(nf, &y, Lpr);
    3741         224 :   Lpr = shallowconcat(Lpr, gel(idealfactor(nf,y), 1));
    3742         224 :   settyp(Lpr,t_VEC);
    3743         224 :   hf = mkvec2(Lpr, shallowconcat(hfe, const_vecsmall(lg(Lpr)-lg(hfe), 0)));
    3744         224 :   gel(al,5) = hf;
    3745         224 :   gel(al,6) = gen_0;
    3746         224 :   gel(al,7) = matid(D);
    3747         224 :   gel(al,8) = matid(D); /* TODO modify 7, 8 et 9 once LLL added */
    3748         224 :   gel(al,9) = algnatmultable(al,D);
    3749         224 :   gel(al,11)= algtracebasis(al);
    3750         224 :   if (maxord) al = alg_maximal_primes(al, pr_primes(Lpr));
    3751         224 :   return gerepilecopy(av, al);
    3752             : }
    3753             : 
    3754             : GEN
    3755           0 : alg_complete(GEN rnf, GEN aut, GEN hf, GEN hi, long maxord)
    3756             : {
    3757           0 :   long n = rnf_get_degree(rnf);
    3758           0 :   rnfcomplete(rnf);
    3759           0 :   return alg_complete0(rnf,aut,hasseconvert(hf,n),hasseconvert(hi,n), maxord);
    3760             : }
    3761             : 
    3762             : void
    3763        1239 : checkhasse(GEN nf, GEN hf, GEN hi, long n)
    3764             : {
    3765             :   GEN Lpr, Lh;
    3766             :   long i, sum;
    3767        1239 :   if (typ(hf) != t_VEC || lg(hf) != 3) pari_err_TYPE("checkhasse [hf]", hf);
    3768        1232 :   Lpr = gel(hf,1);
    3769        1232 :   Lh = gel(hf,2);
    3770        1232 :   if (typ(Lpr) != t_VEC) pari_err_TYPE("checkhasse [Lpr]", Lpr);
    3771        1232 :   if (typ(Lh) != t_VECSMALL) pari_err_TYPE("checkhasse [Lh]", Lh);
    3772        1232 :   if (typ(hi) != t_VECSMALL) pari_err_TYPE("checkhasse [hi]", hi);
    3773        1232 :   if ((nf && lg(hi) != nf_get_r1(nf)+1))
    3774           7 :     pari_err_DOMAIN("checkhasse [hi should have r1 components]","#hi","!=",stoi(nf_get_r1(nf)),stoi(lg(hi)-1));
    3775        1225 :   if (lg(Lpr) != lg(Lh))
    3776           7 :     pari_err_DIM("checkhasse [Lpr and Lh should have same length]");
    3777        1218 :   for (i=1; i<lg(Lpr); i++) checkprid(gel(Lpr,i));
    3778        1218 :   if (lg(gen_sort_uniq(Lpr, (void*)cmp_prime_ideal, cmp_nodata)) < lg(Lpr))
    3779           7 :     pari_err(e_MISC, "error in checkhasse [duplicate prime ideal]");
    3780        1211 :   sum = 0;
    3781        1211 :   for (i=1; i<lg(Lh); i++) sum = (sum+Lh[i])%n;
    3782        2611 :   for (i=1; i<lg(hi); i++) {
    3783        1414 :       if (hi[i] && 2*hi[i] != n) pari_err_DOMAIN("checkhasse", "Hasse invariant at real place [must be 0 or 1/2]", "!=", n%2? gen_0 : stoi(n/2), stoi(hi[i]));
    3784        1400 :       sum = (sum+hi[i])%n;
    3785             :   }
    3786        1197 :   if (sum<0) sum = n+sum;
    3787        1197 :   if (sum != 0)
    3788           7 :     pari_err_DOMAIN("checkhasse","sum(Hasse invariants)","!=",gen_0,Lh);
    3789        1190 : }
    3790             : 
    3791             : static GEN
    3792         322 : hassecoprime(GEN hf, GEN hi, long n)
    3793             : {
    3794         322 :   pari_sp av = avma;
    3795             :   long l, i, j, lk, inv;
    3796             :   GEN fa, P,E, res, hil, hfl;
    3797         322 :   hi = hasseconvert(hi, n);
    3798         308 :   hf = hasseconvert(hf, n);
    3799         287 :   checkhasse(NULL,hf,hi,n);
    3800         245 :   fa = factoru(n);
    3801         245 :   P = gel(fa,1); l = lg(P);
    3802         245 :   E = gel(fa,2);
    3803         245 :   res = cgetg(l,t_VEC);
    3804         497 :   for (i=1; i<l; i++) {
    3805         252 :     lk = upowuu(P[i],E[i]);
    3806         252 :     inv = Fl_invsafe((n/lk)%lk, lk);
    3807         252 :     hil = gcopy(hi);
    3808         252 :     hfl = gcopy(hf);
    3809             : 
    3810         252 :     if (P[i] == 2)
    3811         210 :       for (j=1; j<lg(hil); j++) hil[j] = hi[j]==0 ? 0 : lk/2;
    3812             :     else
    3813          42 :       for (j=1; j<lg(hil); j++) hil[j] = 0;
    3814         252 :     for (j=1; j<lgcols(hfl); j++) gel(hfl,2)[j] = (gel(hf,2)[j]*inv)%lk;
    3815         252 :     hfl = hassereduce(hfl);
    3816         252 :     gel(res,i) = mkvec3(hfl,hil,utoi(lk));
    3817             :   }
    3818             : 
    3819         245 :   return gerepilecopy(av, res);
    3820             : }
    3821             : 
    3822             : /* no garbage collection */
    3823             : static GEN
    3824          70 : genefrob(GEN nf, GEN gal, GEN r)
    3825             : {
    3826             :   long i;
    3827          70 :   GEN g = identity_perm(nf_get_degree(nf)), fa = Z_factor(r), p, pr, frob;
    3828         119 :   for (i=1; i<lgcols(fa); i++) {
    3829          49 :     p = gcoeff(fa,i,1);
    3830          49 :     pr = idealprimedec(nf, p);
    3831          49 :     pr = gel(pr,1);
    3832          49 :     frob = idealfrobenius(nf, gal, pr);
    3833          49 :     g = perm_mul(g, perm_pow(frob, itos(gcoeff(fa,i,2))));
    3834             :   }
    3835          70 :   return g;
    3836             : }
    3837             : 
    3838             : static GEN
    3839         224 : rnfcycaut(GEN rnf)
    3840             : {
    3841         224 :   GEN nf2 = obj_check(rnf, rnf_NFABS);
    3842             :   GEN L, alpha, pol, salpha, s, sj, polabs, k, X, pol0, nf;
    3843             :   long i, d, j;
    3844         224 :   d = rnf_get_degree(rnf);
    3845         224 :   L = galoisconj(nf2,NULL);
    3846         224 :   alpha = lift_shallow(rnf_get_alpha(rnf));
    3847         224 :   pol = rnf_get_pol(rnf);
    3848         224 :   k = rnf_get_k(rnf);
    3849         224 :   polabs = rnf_get_polabs(rnf);
    3850         224 :   nf = rnf_get_nf(rnf);
    3851         224 :   pol0 = nf_get_pol(nf);
    3852         224 :   X = RgX_rem(pol_x(varn(pol0)), pol0);
    3853             : 
    3854             :   /* TODO check mod prime of degree 1 */
    3855         399 :   for (i=1; i<lg(L); i++) {
    3856         399 :     s = gel(L,i);
    3857         399 :     salpha = RgX_RgXQ_eval(alpha,s,polabs);
    3858         399 :     if (!gequal(alpha,salpha)) continue;
    3859             : 
    3860         357 :     s = lift_shallow(rnfeltabstorel(rnf,s));
    3861         357 :     sj = s = gsub(s, gmul(k,X));
    3862         616 :     for (j=1; !gequal0(gsub(sj,pol_x(varn(s)))); j++)
    3863         259 :       sj = RgX_RgXQ_eval(sj,s,pol);
    3864         357 :     if (j<d) continue;
    3865         224 :     return s;
    3866             :   }
    3867             :   return NULL; /*LCOV_EXCL_LINE*/
    3868             : }
    3869             : 
    3870             : /* returns Lpr augmented with an extra, distinct prime */
    3871             : /* TODO be less lazy and return a small prime */
    3872             : static GEN
    3873          84 : extraprime(GEN nf, GEN Lpr)
    3874             : {
    3875          84 :   GEN Lpr2, p = gen_2, pr;
    3876             :   long i;
    3877          84 :   Lpr2 = cgetg(lg(Lpr)+1,t_VEC);
    3878          98 :   for (i=1; i<lg(Lpr); i++)
    3879             :   {
    3880          14 :     gel(Lpr2,i) = gel(Lpr,i);
    3881          14 :     p = gmax_shallow(p, pr_get_p(gel(Lpr,i)));
    3882             :   }
    3883          84 :   p = nextprime(addis(p,1));
    3884          84 :   pr = gel(idealprimedec_limit_f(nf, p, 0), 1);
    3885          84 :   gel(Lpr2,lg(Lpr)) = pr;
    3886          84 :   return Lpr2;
    3887             : }
    3888             : 
    3889             : GEN
    3890         336 : alg_hasse(GEN nf, long n, GEN hf, GEN hi, long var, long maxord)
    3891             : {
    3892         336 :   pari_sp av = avma;
    3893         336 :   GEN primary, al = gen_0, al2, rnf, hil, hfl, Ld, pl, pol, Lpr, aut, Lpr2, Ld2;
    3894             :   long i, lk, j, maxdeg;
    3895         336 :   dbg_printf(1)("alg_hasse\n");
    3896         336 :   if (n<=1) pari_err_DOMAIN("alg_hasse", "degree", "<=", gen_1, stoi(n));
    3897         322 :   primary = hassecoprime(hf, hi, n);
    3898         476 :   for (i=1; i<lg(primary); i++) {
    3899         252 :     lk = itos(gmael(primary,i,3));
    3900         252 :     hfl = gmael(primary,i,1);
    3901         252 :     hil = gmael(primary,i,2);
    3902         252 :     checkhasse(nf, hfl, hil, lk);
    3903         245 :     dbg_printf(1)("alg_hasse: i=%d hf=%Ps hi=%Ps lk=%d\n", i, hfl, hil, lk);
    3904             : 
    3905         245 :     if (lg(gel(hfl,1))>1 || lk%2==0) {
    3906         238 :       maxdeg = 1;
    3907         238 :       Lpr = gel(hfl,1);
    3908         238 :       Ld = gcopy(gel(hfl,2));
    3909         385 :       for (j=1; j<lg(Ld); j++)
    3910             :       {
    3911         147 :         Ld[j] = lk/ugcd(lk,Ld[j]);
    3912         147 :         maxdeg = maxss(Ld[j],maxdeg);
    3913             :       }
    3914         238 :       pl = gcopy(hil);
    3915         525 :       for (j=1; j<lg(pl); j++) if(pl[j])
    3916             :       {
    3917          77 :         pl[j] = -1;
    3918          77 :         maxdeg = maxss(maxdeg,2);
    3919             :       }
    3920             : 
    3921         238 :       Lpr2 = Lpr;
    3922         238 :       Ld2 = Ld;
    3923         238 :       if (maxdeg<lk)
    3924             :       {
    3925         154 :         if (maxdeg==1 && lk==2 && lg(pl)>1) pl[1] = -1;
    3926             :         else
    3927             :         {
    3928          84 :           Lpr2 = extraprime(nf,Lpr);
    3929          84 :           Ld2 = cgetg(lg(Ld)+1, t_VECSMALL);
    3930          84 :           for (j=1; j<lg(Ld); j++) Ld2[j] = Ld[j];
    3931          84 :           Ld2[lg(Ld)] = lk;
    3932             :         }
    3933             :       }
    3934             : 
    3935         238 :       dbg_printf(2)("alg_hasse: calling nfgrunwaldwang Lpr=%Ps Pd=%Ps pl=%Ps\n",
    3936             :           Lpr, Ld, pl);
    3937         238 :       pol = nfgrunwaldwang(nf, Lpr2, Ld2, pl, var);
    3938         224 :       dbg_printf(2)("alg_hasse: calling rnfinit(%Ps)\n", pol);
    3939         224 :       rnf = rnfinit0(nf,pol,1);
    3940         224 :       dbg_printf(2)("alg_hasse: computing automorphism\n");
    3941         224 :       aut = rnfcycaut(rnf);
    3942         224 :       dbg_printf(2)("alg_hasse: calling alg_complete\n");
    3943         224 :       al2 = alg_complete0(rnf,aut,hfl,hil,maxord);
    3944             :     }
    3945           7 :     else al2 = alg_matrix(nf, lk, var, cgetg(1,t_VEC), maxord);
    3946             : 
    3947         231 :     if (i==1) al = al2;
    3948           7 :     else      al = algtensor(al,al2,maxord);
    3949             :   }
    3950         224 :   return gerepilecopy(av,al);
    3951             : }
    3952             : 
    3953             : /** CYCLIC ALGEBRA WITH GIVEN HASSE INVARIANTS **/
    3954             : 
    3955             : /* no garbage collection */
    3956             : static int
    3957          70 : linindep(GEN pol, GEN L)
    3958             : {
    3959             :   long i;
    3960             :   GEN fa;
    3961          70 :   for (i=1; i<lg(L); i++) {
    3962           0 :     fa = nffactor(gel(L,i),pol);
    3963           0 :     if (lgcols(fa)>2) return 0;
    3964             :   }
    3965          70 :   return 1;
    3966             : }
    3967             : 
    3968             : /* no garbage collection */
    3969             : static GEN
    3970          70 : subcycloindep(GEN nf, long n, long v, GEN L, GEN *pr)
    3971             : {
    3972             :   pari_sp av;
    3973             :   forprime_t S;
    3974             :   ulong p;
    3975          70 :   u_forprime_arith_init(&S, 1, ULONG_MAX, 1, n);
    3976          70 :   av = avma;
    3977         147 :   while ((p = u_forprime_next(&S)))
    3978             :   {
    3979          77 :     ulong r = pgener_Fl(p);
    3980          77 :     GEN pol = galoissubcyclo(utoipos(p), utoipos(Fl_powu(r,n,p)), 0, v);
    3981          77 :     GEN fa = nffactor(nf, pol);
    3982          77 :     if (lgcols(fa) == 2 && linindep(pol,L)) { *pr = utoipos(r); return pol; }
    3983           7 :     set_avma(av);
    3984             :   }
    3985             :   pari_err_BUG("subcycloindep (no suitable prime = 1(mod n))"); /*LCOV_EXCL_LINE*/
    3986             :   *pr = NULL; return NULL; /*LCOV_EXCL_LINE*/
    3987             : }
    3988             : 
    3989             : GEN
    3990          77 : alg_matrix(GEN nf, long n, long v, GEN L, long maxord)
    3991             : {
    3992          77 :   pari_sp av = avma;
    3993             :   GEN pol, gal, rnf, cyclo, g, r, aut;
    3994          77 :   dbg_printf(1)("alg_matrix\n");
    3995          77 :   if (n<=0) pari_err_DOMAIN("alg_matrix", "n", "<=", gen_0, stoi(n));
    3996          70 :   pol = subcycloindep(nf, n, v, L, &r);
    3997          70 :   rnf = rnfinit(nf, pol);
    3998          70 :   cyclo = nfinit(pol, nf_get_prec(nf));
    3999          70 :   gal = galoisinit(cyclo, NULL);
    4000          70 :   g = genefrob(cyclo,gal,r);
    4001          70 :   aut = galoispermtopol(gal,g);
    4002          70 :   return gerepileupto(av, alg_cyclic(rnf, aut, gen_1, maxord));
    4003             : }
    4004             : 
    4005             : GEN
    4006         273 : alg_hilbert(GEN nf, GEN a, GEN b, long v, long maxord)
    4007             : {
    4008         273 :   pari_sp av = avma;
    4009             :   GEN C, P, rnf, aut;
    4010         273 :   dbg_printf(1)("alg_hilbert\n");
    4011         273 :   checknf(nf);
    4012         273 :   if (!isint1(Q_denom(a)))
    4013           7 :     pari_err_DOMAIN("alg_hilbert", "denominator(a)", "!=", gen_1,a);
    4014         266 :   if (!isint1(Q_denom(b)))
    4015           7 :     pari_err_DOMAIN("alg_hilbert", "denominator(b)", "!=", gen_1,b);
    4016             : 
    4017         259 :   if (v < 0) v = 0;
    4018         259 :   C = Rg_col_ei(gneg(a), 3, 3);
    4019         259 :   gel(C,1) = gen_1;
    4020         259 :   P = gtopoly(C,v);
    4021         259 :   rnf = rnfinit(nf, P);
    4022         252 :   aut = gneg(pol_x(v));
    4023         252 :   return gerepileupto(av, alg_cyclic(rnf, aut, b, maxord));
    4024             : }
    4025             : 
    4026             : GEN
    4027        1043 : alginit(GEN A, GEN B, long v, long maxord)
    4028             : {
    4029             :   long w;
    4030        1043 :   switch(nftyp(A))
    4031             :   {
    4032             :     case typ_NF:
    4033         875 :       if (v<0) v=0;
    4034         875 :       w = gvar(nf_get_pol(A));
    4035         875 :       if (varncmp(v,w)>=0) pari_err_PRIORITY("alginit", pol_x(v), ">=", w);
    4036         861 :       switch(typ(B))
    4037             :       {
    4038             :         long nB;
    4039          70 :         case t_INT: return alg_matrix(A, itos(B), v, cgetg(1,t_VEC), maxord);
    4040             :         case t_VEC:
    4041         784 :           nB = lg(B)-1;
    4042         784 :           if (nB && typ(gel(B,1)) == t_MAT) return alg_csa_table(A,B,v,maxord);
    4043         623 :           switch(nB)
    4044             :           {
    4045         273 :             case 2: return alg_hilbert(A, gel(B,1), gel(B,2), v, maxord);
    4046             :             case 3:
    4047         343 :               if (typ(gel(B,1))!=t_INT)
    4048           7 :                   pari_err_TYPE("alginit [degree should be an integer]", gel(B,1));
    4049         336 :               return alg_hasse(A, itos(gel(B,1)), gel(B,2), gel(B,3), v,
    4050             :                                                                       maxord);
    4051             :           }
    4052             :       }
    4053          14 :       pari_err_TYPE("alginit", B); break;
    4054             : 
    4055             :     case typ_RNF:
    4056         161 :       if (typ(B) != t_VEC || lg(B) != 3) pari_err_TYPE("alginit", B);
    4057         147 :       return alg_cyclic(A, gel(B,1), gel(B,2), maxord);
    4058             :   }
    4059           7 :   pari_err_TYPE("alginit", A);
    4060             :   return NULL;/*LCOV_EXCL_LINE*/
    4061             : }
    4062             : 
    4063             : /* assumes al CSA or CYCLIC */
    4064             : static GEN
    4065         833 : algnatmultable(GEN al, long D)
    4066             : {
    4067             :   GEN res, x;
    4068             :   long i;
    4069         833 :   res = cgetg(D+1,t_VEC);
    4070        9793 :   for (i=1; i<=D; i++) {
    4071        8960 :     x = algnattoalg(al,col_ei(D,i));
    4072        8960 :     gel(res,i) = algZmultable(al,x);
    4073             :   }
    4074         833 :   return res;
    4075             : }
    4076             : 
    4077             : /* no garbage collection */
    4078             : static void
    4079         476 : algcomputehasse(GEN al)
    4080             : {
    4081             :   long r1, k, n, m, m1, m2, m3, i, m23, m123;
    4082             :   GEN rnf, nf, b, fab, disc2, cnd, fad, auts, pr, pl, perm;
    4083             :   GEN hi, PH, H, L;
    4084             : 
    4085         476 :   rnf = alg_get_splittingfield(al);
    4086         476 :   n = rnf_get_degree(rnf);
    4087         476 :   nf = rnf_get_nf(rnf);
    4088         476 :   b = alg_get_b(al);
    4089         476 :   r1 = nf_get_r1(nf);
    4090         476 :   auts = alg_get_auts(al);
    4091         476 :   (void)alg_get_abssplitting(al);
    4092             : 
    4093             :   /* real places where rnf/nf ramifies */
    4094         476 :   pl = cgetg(r1+1, t_VECSMALL);
    4095         476 :   for (k=1; k<=r1; k++) pl[k] = !rnfrealdec(rnf,k);
    4096             : 
    4097             :   /* infinite Hasse invariants */
    4098         476 :   if (odd(n)) hi = const_vecsmall(r1, 0);
    4099             :   else
    4100             :   {
    4101         406 :     GEN s = nfsign(nf, b);
    4102         406 :     hi = cgetg(r1+1, t_VECSMALL);
    4103         406 :     for (k = 1; k<=r1; k++) hi[k] = (s[k] && pl[k]) ? (n/2) : 0;
    4104             :   }
    4105             : 
    4106         476 :   fab = idealfactor(nf, b);
    4107         476 :   disc2 = rnf_get_idealdisc(rnf);
    4108         476 :   L = nfmakecoprime(nf, &disc2, gel(fab,1));
    4109         476 :   m = lg(L)-1;
    4110             :   /* m1 = #{pr|b: pr \nmid disc}, m3 = #{pr|b: pr | disc} */
    4111         476 :   perm = cgetg(m+1, t_VECSMALL);
    4112         861 :   for (i=1, m1=m, k=1; k<=m; k++)
    4113         385 :     if (signe(gel(L,k))) perm[m1--] = k; else perm[i++] = k;
    4114         476 :   m3 = m - m1;
    4115             : 
    4116             :   /* disc2 : factor of disc coprime to b */
    4117         476 :   fad = idealfactor(nf, disc2);
    4118             :   /* m2 : number of prime factors of disc not dividing b */
    4119         476 :   m2 = nbrows(fad);
    4120         476 :   m23 = m2+m3;
    4121         476 :   m123 = m1+m2+m3;
    4122             : 
    4123             :   /* initialize the possibly ramified primes (hasse) and the factored conductor of rnf/nf (cnd) */
    4124         476 :   cnd = zeromatcopy(m23,2);
    4125         476 :   PH = cgetg(m123+1, t_VEC); /* ramified primes */
    4126         476 :   H = cgetg(m123+1, t_VECSMALL); /* Hasse invariant */
    4127             :   /* compute Hasse invariant at primes that are unramified in rnf/nf */
    4128         826 :   for (k=1; k<=m1; k++) {/* pr | b, pr \nmid disc */
    4129         350 :     long frob, e, j = perm[k];
    4130         350 :     pr = gcoeff(fab,j,1);
    4131         350 :     e = itos(gcoeff(fab,j,2));
    4132         350 :     frob = cyclicrelfrob(rnf, auts, pr);
    4133         350 :     gel(PH,k) = pr;
    4134         350 :     H[k] = Fl_mul(frob, e, n);
    4135             :   }
    4136             :   /* compute Hasse invariant at primes that are ramified in rnf/nf */
    4137         994 :   for (k=1; k<=m2; k++) {/* pr \nmid b, pr | disc */
    4138         518 :     pr = gcoeff(fad,k,1);
    4139         518 :     gel(PH,k+m1) = pr;
    4140         518 :     gcoeff(cnd,k,1) = pr;
    4141         518 :     gcoeff(cnd,k,2) = gcoeff(fad,k,2);
    4142             :   }
    4143         511 :   for (k=1; k<=m3; k++) { /* pr | (b, disc) */
    4144          35 :     long j = perm[k+m1];
    4145          35 :     pr = gcoeff(fab,j,1);
    4146          35 :     gel(PH,k+m1+m2) = pr;
    4147          35 :     gcoeff(cnd,k+m2,1) = pr;
    4148          35 :     gcoeff(cnd,k+m2,2) = gel(L,j);
    4149             :   }
    4150         476 :   gel(cnd,2) = gdiventgs(gel(cnd,2), eulerphiu(n));
    4151         476 :   for (k=1; k<=m23; k++) H[k+m1] = localhasse(rnf, cnd, pl, auts, b, k);
    4152         476 :   gel(al,4) = hi;
    4153         476 :   perm = gen_indexsort(PH, (void*)&cmp_prime_ideal, &cmp_nodata);
    4154         476 :   gel(al,5) = mkvec2(vecpermute(PH,perm),vecsmallpermute(H,perm));
    4155         476 :   checkhasse(nf,alg_get_hasse_f(al),alg_get_hasse_i(al),n);
    4156         476 : }
    4157             : 
    4158             : static GEN
    4159         749 : alg_maximal_primes(GEN al, GEN P)
    4160             : {
    4161         749 :   pari_sp av = avma;
    4162         749 :   long l = lg(P), i;
    4163        1946 :   for (i=1; i<l; i++)
    4164             :   {
    4165        1197 :     if (i != 1) al = gerepilecopy(av, al);
    4166        1197 :     al = alg_pmaximal(al,gel(P,i));
    4167             :   }
    4168         749 :   return al;
    4169             : }
    4170             : 
    4171             : GEN
    4172         483 : alg_cyclic(GEN rnf, GEN aut, GEN b, long maxord)
    4173             : {
    4174         483 :   pari_sp av = avma;
    4175             :   GEN al, nf;
    4176             :   long D, n, d;
    4177         483 :   dbg_printf(1)("alg_cyclic\n");
    4178         483 :   checkrnf(rnf);
    4179         483 :   if (!isint1(Q_denom(b)))
    4180           7 :     pari_err_DOMAIN("alg_cyclic", "denominator(b)", "!=", gen_1,b);
    4181             : 
    4182         476 :   nf = rnf_get_nf(rnf);
    4183         476 :   n = rnf_get_degree(rnf);
    4184         476 :   d = nf_get_degree(nf);
    4185         476 :   D = d*n*n;
    4186             : 
    4187         476 :   al = cgetg(12,t_VEC);
    4188         476 :   gel(al,10)= gen_0; /* must be set first */
    4189         476 :   gel(al,1) = rnf;
    4190         476 :   gel(al,2) = allauts(rnf, aut);
    4191         476 :   gel(al,3) = basistoalg(nf,b);
    4192         476 :   rnf_build_nfabs(rnf, nf_get_prec(nf));
    4193         476 :   gel(al,6) = gen_0;
    4194         476 :   gel(al,7) = matid(D);
    4195         476 :   gel(al,8) = matid(D); /* TODO modify 7, 8 et 9 once LLL added */
    4196         476 :   gel(al,9) = algnatmultable(al,D);
    4197         476 :   gel(al,11)= algtracebasis(al);
    4198             : 
    4199         476 :   algcomputehasse(al);
    4200             : 
    4201         476 :   if (maxord) {
    4202         413 :     GEN hf = alg_get_hasse_f(al), pr = gel(hf,1);
    4203         413 :     al = alg_maximal_primes(al, pr_primes(pr));
    4204             :   }
    4205         476 :   return gerepilecopy(av, al);
    4206             : }
    4207             : 
    4208             : static int
    4209         378 : ismaximalsubfield(GEN al, GEN x, GEN d, long v, GEN *pt_minpol)
    4210             : {
    4211         378 :   GEN cp = algbasischarpoly(al, x, v), lead;
    4212         378 :   if (!ispower(cp, d, pt_minpol)) return 0;
    4213         378 :   lead = leading_coeff(*pt_minpol);
    4214         378 :   if (isintm1(lead)) *pt_minpol = gneg(*pt_minpol);
    4215         378 :   return ZX_is_irred(*pt_minpol);
    4216             : }
    4217             : 
    4218             : static GEN
    4219         133 : findmaximalsubfield(GEN al, GEN d, long v)
    4220             : {
    4221         133 :   long count, nb=2, i, N = alg_get_absdim(al), n = nf_get_degree(alg_get_center(al));
    4222         133 :   GEN x, minpol, maxc = gen_1;
    4223             : 
    4224         210 :   for (i=n+1; i<=N; i+=n) {
    4225         336 :     for (count=0; count<2 && i+count<=N; count++) {
    4226         259 :       x = col_ei(N,i+count);
    4227         259 :       if (ismaximalsubfield(al, x, d, v, &minpol)) return mkvec2(x,minpol);
    4228             :     }
    4229             :   }
    4230             : 
    4231             :   while(1) {
    4232         175 :     x = zerocol(N);
    4233         504 :     for (count=0; count<nb; count++)
    4234             :     {
    4235         385 :       i = random_Fl(N)+1;
    4236         385 :       gel(x,i) = addiu(randomi(maxc),1);
    4237         385 :       if (random_bits(1)) gel(x,i) = negi(gel(x,i));
    4238             :     }
    4239         119 :     if (ismaximalsubfield(al, x, d, v, &minpol)) return mkvec2(x,minpol);
    4240          56 :     if (!random_bits(3)) maxc = addiu(maxc,1);
    4241          56 :     if (nb<N) nb++;
    4242             :   }
    4243             : 
    4244             :   return NULL; /* LCOV_EXCL_LINE */
    4245             : }
    4246             : 
    4247             : static GEN
    4248         133 : frobeniusform(GEN al, GEN x)
    4249             : {
    4250             :   GEN M, FP, P, Pi;
    4251             : 
    4252             :   /* /!\ has to be the *right* multiplication table */
    4253         133 :   M = algbasisrightmultable(al, x);
    4254             : 
    4255         133 :   FP = matfrobenius(M,2,0); /* M = P^(-1)*F*P */
    4256         133 :   P = gel(FP,2);
    4257         133 :   Pi = RgM_inv(P);
    4258         133 :   return mkvec2(P, Pi);
    4259             : }
    4260             : 
    4261             : static void
    4262         133 : computesplitting(GEN al, long d, long v)
    4263             : {
    4264         133 :   GEN subf, x, pol, polabs, basis, P, Pi, nf = alg_get_center(al), rnf, Lbasis, Lbasisinv, Q, pows;
    4265         133 :   long i, n = nf_get_degree(nf), nd = n*d, N = alg_get_absdim(al), j, j2;
    4266             : 
    4267         133 :   subf = findmaximalsubfield(al, utoipos(d), v);
    4268         133 :   x = gel(subf, 1);
    4269         133 :   polabs = gel(subf, 2);
    4270             : 
    4271             :   /* Frobenius form to obtain L-vector space structure */
    4272         133 :   basis = frobeniusform(al, x);
    4273         133 :   P = gel(basis, 1);
    4274         133 :   Pi = gel(basis, 2);
    4275             : 
    4276             :   /* construct rnf of splitting field */
    4277         133 :   pol = nffactor(nf,polabs);
    4278         133 :   pol = gcoeff(pol,1,1);
    4279         133 :   gel(al,1) = rnf = rnfinit(nf, pol);
    4280             :   /* since pol is irreducible over Q, we have k=0 in rnf. */
    4281         133 :   if (!gequal0(rnf_get_k(rnf)))
    4282             :     pari_err_BUG("computesplitting (k!=0)"); /*LCOV_EXCL_LINE*/
    4283         133 :   gel(al,6) = gen_0;
    4284         133 :   rnf_build_nfabs(rnf, nf_get_prec(nf));
    4285             : 
    4286             :   /* construct splitting data */
    4287         133 :   Lbasis = cgetg(d+1, t_MAT);
    4288         357 :   for (j=j2=1; j<=d; j++, j2+=nd)
    4289         224 :     gel(Lbasis,j) = gel(Pi,j2);
    4290             : 
    4291         133 :   Q = zeromatcopy(d,N);
    4292         133 :   pows = pol_x_powers(nd,v);
    4293         357 :   for (i=j=1; j<=N; j+=nd, i++)
    4294        1085 :   for (j2=0; j2<nd; j2++)
    4295         861 :     gcoeff(Q,i,j+j2) = mkpolmod(gel(pows,j2+1),polabs);
    4296         133 :   Lbasisinv = RgM_mul(Q,P);
    4297             : 
    4298         133 :   gel(al,3) = mkvec3(x,Lbasis,Lbasisinv);
    4299         133 : }
    4300             : 
    4301             : /* assumes that mt defines a central simple algebra over nf */
    4302             : GEN
    4303         161 : alg_csa_table(GEN nf, GEN mt0, long v, long maxord)
    4304             : {
    4305         161 :   pari_sp av = avma;
    4306             :   GEN al, mt;
    4307         161 :   long n, D, d2 = lg(mt0)-1, d = usqrt(d2);
    4308         161 :   dbg_printf(1)("alg_csa_table\n");
    4309             : 
    4310         161 :   nf = checknf(nf);
    4311         161 :   mt = check_relmt(nf,mt0);
    4312         147 :   if (!mt) pari_err_TYPE("alg_csa_table", mt0);
    4313         140 :   n = nf_get_degree(nf);
    4314         140 :   D = n*d2;
    4315         140 :   if (d*d != d2)
    4316           7 :     pari_err_DOMAIN("alg_csa_table","(nonsquare) dimension","!=",stoi(d*d),mt);
    4317             : 
    4318         133 :   al = cgetg(12, t_VEC);
    4319         133 :   gel(al,10) = gen_0; /* must be set first */
    4320         133 :   gel(al,1) = zerovec(12); gmael(al,1,10) = nf;
    4321         133 :   gmael(al,1,1) = gpowgs(pol_x(0), d); /* placeholder before splitting field */
    4322         133 :   gel(al,2) = mt;
    4323         133 :   gel(al,3) = gen_0; /* placeholder */
    4324         133 :   gel(al,4) = gel(al,5) = gen_0; /* TODO Hasse invariants */
    4325         133 :   gel(al,5) = gel(al,6) = gen_0; /* placeholder */
    4326         133 :   gel(al,7) = matid(D);
    4327         133 :   gel(al,8) = matid(D);
    4328         133 :   gel(al,9) = algnatmultable(al,D);
    4329         133 :   gel(al,11)= algtracebasis(al);
    4330         133 :   if (maxord) al = alg_maximal(al);
    4331         133 :   computesplitting(al, d, v);
    4332         133 :   return gerepilecopy(av, al);
    4333             : }
    4334             : 
    4335             : static GEN
    4336       36281 : algtableinit_i(GEN mt0, GEN p)
    4337             : {
    4338             :   GEN al, mt;
    4339             :   long i, n;
    4340             : 
    4341       36281 :   if (p && !signe(p)) p = NULL;
    4342       36281 :   mt = check_mt(mt0,p);
    4343       36281 :   if (!mt) pari_err_TYPE("algtableinit", mt0);
    4344       36281 :   if (!p && !isint1(Q_denom(mt0)))
    4345           7 :     pari_err_DOMAIN("algtableinit", "denominator(mt)", "!=", gen_1, mt0);
    4346       36274 :   n = lg(mt)-1;
    4347       36274 :   al = cgetg(12, t_VEC);
    4348       36274 :   for (i=1; i<=6; i++) gel(al,i) = gen_0;
    4349       36274 :   gel(al,7) = matid(n);
    4350       36274 :   gel(al,8) = matid(n);
    4351       36274 :   gel(al,9) = mt;
    4352       36274 :   gel(al,10) = p? p: gen_0;
    4353       36274 :   gel(al,11)= algtracebasis(al);
    4354       36274 :   return al;
    4355             : }
    4356             : GEN
    4357        4193 : algtableinit(GEN mt0, GEN p)
    4358             : {
    4359        4193 :   pari_sp av = avma;
    4360        4193 :   if (p)
    4361             :   {
    4362        4074 :     if (typ(p) != t_INT) pari_err_TYPE("algtableinit",p);
    4363        4067 :     if (signe(p) && !BPSW_psp(p)) pari_err_PRIME("algtableinit",p);
    4364             :   }
    4365        4172 :   return gerepilecopy(av, algtableinit_i(mt0, p));
    4366             : }
    4367             : 
    4368             : /** REPRESENTATIONS OF GROUPS **/
    4369             : 
    4370             : static GEN
    4371         294 : list_to_regular_rep(GEN elts, long n)
    4372             : {
    4373             :   GEN reg, elts2, g;
    4374             :   long i,j;
    4375         294 :   elts = shallowcopy(elts);
    4376         294 :   gen_sort_inplace(elts, (void*)&vecsmall_lexcmp, &cmp_nodata, NULL);
    4377         294 :   reg = cgetg(n+1, t_VEC);
    4378         294 :   gel(reg,1) = identity_perm(n);
    4379        3857 :   for (i=2; i<=n; i++) {
    4380        3563 :     g = perm_inv(gel(elts,i));
    4381        3563 :     elts2 = cgetg(n+1, t_VEC);
    4382        3563 :     for (j=1; j<=n; j++) gel(elts2,j) = perm_mul(g,gel(elts,j));
    4383        3563 :     gen_sort_inplace(elts2, (void*)&vecsmall_lexcmp, &cmp_nodata, &gel(reg,i));
    4384             :   }
    4385         294 :   return reg;
    4386             : }
    4387             : 
    4388             : static GEN
    4389        3857 : matrix_perm(GEN perm, long n)
    4390             : {
    4391             :   GEN m;
    4392             :   long j;
    4393        3857 :   m = cgetg(n+1, t_MAT);
    4394       78694 :   for (j=1; j<=n; j++) {
    4395       74837 :     gel(m,j) = col_ei(n,perm[j]);
    4396             :   }
    4397        3857 :   return m;
    4398             : }
    4399             : 
    4400             : GEN
    4401         840 : conjclasses_algcenter(GEN cc, GEN p)
    4402             : {
    4403         840 :   GEN mt, elts = gel(cc,1), conjclass = gel(cc,2), rep = gel(cc,3);
    4404         840 :   long i, nbcl = lg(rep)-1, n = lg(elts)-1;
    4405             :   pari_sp av;
    4406             : 
    4407             :   /* multiplication table of the center of Z[G] (class functions) */
    4408         840 :   mt = cgetg(nbcl+1,t_VEC);
    4409         840 :   for (i=1;i<=nbcl;i++) gel(mt,i) = zero_Flm_copy(nbcl,nbcl);
    4410         840 :   av = avma;
    4411       14798 :   for (i=1;i<=n;i++)
    4412             :   {
    4413       13958 :     GEN xi = gel(elts,i), mi = gel(mt,conjclass[i]);
    4414             :     long j;
    4415      563500 :     for (j=1;j<=n;j++)
    4416             :     {
    4417      549542 :       GEN xj = gel(elts,j);
    4418      549542 :       long k = vecsearch(elts, perm_mul(xi,xj), NULL), ck = conjclass[k];
    4419      549542 :       if (rep[ck]==k) ucoeff(mi, ck, conjclass[j])++;
    4420             :     }
    4421       13958 :     set_avma(av);
    4422             :   }
    4423         840 :   for (i=1;i<=nbcl;i++) gel(mt,i) = Flm_to_ZM(gel(mt,i));
    4424         840 :   return algtableinit_i(mt,p);
    4425             : }
    4426             : 
    4427             : GEN
    4428         329 : alggroupcenter(GEN G, GEN p, GEN *pcc)
    4429             : {
    4430         329 :   pari_sp av = avma;
    4431         329 :   GEN cc = group_to_cc(G), al = conjclasses_algcenter(cc, p);
    4432         315 :   if (!pcc) al = gerepilecopy(av,al);
    4433             :   else
    4434           7 :   { *pcc = cc; gerepileall(av,2,&al,pcc); }
    4435         315 :   return al;
    4436             : }
    4437             : 
    4438             : static GEN
    4439         294 : groupelts_algebra(GEN elts, GEN p)
    4440             : {
    4441         294 :   pari_sp av = avma;
    4442             :   GEN mt;
    4443         294 :   long i, n = lg(elts)-1;
    4444         294 :   elts = list_to_regular_rep(elts,n);
    4445         294 :   mt = cgetg(n+1, t_VEC);
    4446         294 :   for (i=1; i<=n; i++) gel(mt,i) = matrix_perm(gel(elts,i),n);
    4447         294 :   return gerepilecopy(av, algtableinit_i(mt,p));
    4448             : }
    4449             : 
    4450             : GEN
    4451         329 : alggroup(GEN gal, GEN p)
    4452             : {
    4453         329 :   GEN elts = checkgroupelts(gal);
    4454         294 :   return groupelts_algebra(elts, p);
    4455             : }
    4456             : 
    4457             : /** MAXIMAL ORDER **/
    4458             : 
    4459             : static GEN
    4460       52381 : mattocol(GEN M, long n)
    4461             : {
    4462       52381 :   GEN C = cgetg(n*n+1, t_COL);
    4463             :   long i,j,ic;
    4464       52381 :   ic = 1;
    4465     1126916 :   for (i=1; i<=n; i++)
    4466     1074535 :   for (j=1; j<=n; j++, ic++) gel(C,ic) = gcoeff(M,i,j);
    4467       52381 :   return C;
    4468             : }
    4469             : 
    4470             : /* Ip is a lift of a left O/pO-ideal where O is the integral basis of al */
    4471             : static GEN
    4472        3745 : algleftordermodp(GEN al, GEN Ip, GEN p)
    4473             : {
    4474        3745 :   pari_sp av = avma;
    4475             :   GEN I, Ii, M, mt, K, imi, p2;
    4476             :   long n, i;
    4477        3745 :   n = alg_get_absdim(al);
    4478        3745 :   mt = alg_get_multable(al);
    4479        3745 :   p2 = sqri(p);
    4480             : 
    4481        3745 :   I = ZM_hnfmodid(Ip, p);
    4482        3745 :   Ii = ZM_inv(I,NULL);
    4483             : 
    4484        3745 :   M = cgetg(n+1, t_MAT);
    4485       56126 :   for (i=1; i<=n; i++) {
    4486       52381 :     imi = FpM_mul(Ii, FpM_mul(gel(mt,i), I, p2), p2);
    4487       52381 :     imi = ZM_Z_divexact(imi, p);
    4488       52381 :     gel(M,i) = mattocol(imi, n);
    4489             :   }
    4490        3745 :   K = FpM_ker(M, p);
    4491        3745 :   if (lg(K)==1) { set_avma(av); return matid(n); }
    4492        1694 :   K = ZM_hnfmodid(K,p);
    4493             : 
    4494        1694 :   return gerepileupto(av, ZM_Z_div(K,p));
    4495             : }
    4496             : 
    4497             : static GEN
    4498        4830 : alg_ordermodp(GEN al, GEN p)
    4499             : {
    4500             :   GEN alp;
    4501        4830 :   long i, N = alg_get_absdim(al);
    4502        4830 :   alp = cgetg(12, t_VEC);
    4503        4830 :   for (i=1; i<=8; i++) gel(alp,i) = gen_0;
    4504        4830 :   gel(alp,9) = cgetg(N+1, t_VEC);
    4505        4830 :   for (i=1; i<=N; i++) gmael(alp,9,i) = FpM_red(gmael(al,9,i), p);
    4506        4830 :   gel(alp,10) = p;
    4507        4830 :   gel(alp,11) = cgetg(N+1, t_VEC);
    4508        4830 :   for (i=1; i<=N; i++) gmael(alp,11,i) = Fp_red(gmael(al,11,i), p);
    4509             : 
    4510        4830 :   return alp;
    4511             : }
    4512             : 
    4513             : static GEN
    4514        2891 : algpradical_i(GEN al, GEN p, GEN zprad, GEN projs)
    4515             : {
    4516        2891 :   pari_sp av = avma;
    4517        2891 :   GEN alp = alg_ordermodp(al, p), liftrad, projrad, alq, alrad, res, Lalp, radq;
    4518             :   long i;
    4519        2891 :   if (lg(zprad)==1) {
    4520        1806 :     liftrad = NULL;
    4521        1806 :     projrad = NULL;
    4522             :   }
    4523             :   else {
    4524        1085 :     alq = alg_quotient(alp, zprad, 1);
    4525        1085 :     alp = gel(alq,1);
    4526        1085 :     projrad = gel(alq,2);
    4527        1085 :     liftrad = gel(alq,3);
    4528             :   }
    4529             : 
    4530        2891 :   if (projs) {
    4531         392 :     if (projrad) {
    4532          28 :       projs = gcopy(projs);
    4533          84 :       for (i=1; i<lg(projs); i++)
    4534          56 :         gel(projs,i) = FpM_FpC_mul(projrad, gel(projs,i), p);
    4535             :     }
    4536         392 :     Lalp = alg_centralproj(alp, projs, 1);
    4537             : 
    4538         392 :     alrad = cgetg(lg(Lalp),t_VEC);
    4539        1673 :     for (i=1; i<lg(Lalp); i++) {
    4540        1281 :       alq = gel(Lalp,i);
    4541        1281 :       radq = algradical(gel(alq,1));
    4542        1281 :       if (gequal0(radq))
    4543         812 :         gel(alrad,i) = cgetg(1,t_MAT);
    4544             :       else {
    4545         469 :         radq = FpM_mul(gel(alq,3),radq,p);
    4546         469 :         gel(alrad,i) = radq;
    4547             :       }
    4548             :     }
    4549         392 :     alrad = shallowmatconcat(alrad);
    4550         392 :     alrad = FpM_image(alrad,p);
    4551             :   }
    4552        2499 :   else alrad = algradical(alp);
    4553             : 
    4554        2891 :   if (!gequal0(alrad)) {
    4555        2212 :     if (liftrad) alrad = FpM_mul(liftrad, alrad, p);
    4556        2212 :     res = shallowmatconcat(mkvec2(alrad, zprad));
    4557        2212 :     res = FpM_image(res,p);
    4558             :   }
    4559         679 :   else res = lg(zprad)==1 ? gen_0 : zprad;
    4560        2891 :   return gerepilecopy(av, res);
    4561             : }
    4562             : 
    4563             : static GEN
    4564        1939 : algpdecompose0(GEN al, GEN prad, GEN p, GEN projs)
    4565             : {
    4566        1939 :   pari_sp av = avma;
    4567        1939 :   GEN alp, quo, ss, liftm = NULL, projm = NULL, dec, res, I, Lss, deci;
    4568             :   long i, j;
    4569             : 
    4570        1939 :   alp = alg_ordermodp(al, p);
    4571        1939 :   if (!gequal0(prad)) {
    4572        1526 :     quo = alg_quotient(alp, prad, 1);
    4573        1526 :     ss = gel(quo,1);
    4574        1526 :     projm = gel(quo,2);
    4575        1526 :     liftm = gel(quo,3);
    4576             :   }
    4577         413 :   else ss = alp;
    4578             : 
    4579        1939 :   if (projs) {
    4580         336 :     if (projm) {
    4581        1022 :       for (i=1; i<lg(projs); i++)
    4582         770 :         gel(projs,i) = FpM_FpC_mul(projm, gel(projs,i), p);
    4583             :     }
    4584         336 :     Lss = alg_centralproj(ss, projs, 1);
    4585             : 
    4586         336 :     dec = cgetg(lg(Lss),t_VEC);
    4587        1498 :     for (i=1; i<lg(Lss); i++) {
    4588        1162 :       gel(dec,i) = algsimpledec_ss(gmael(Lss,i,1), 1);
    4589        1162 :       deci = gel(dec,i);
    4590        2576 :       for (j=1; j<lg(deci); j++)
    4591        1414 :        gmael(deci,j,3) = FpM_mul(gmael(Lss,i,3), gmael(deci,j,3), p);
    4592             :     }
    4593         336 :     dec = shallowconcat1(dec);
    4594             :   }
    4595        1603 :   else dec = algsimpledec_ss(ss,1);
    4596             : 
    4597        1939 :   res = cgetg(lg(dec),t_VEC);
    4598        5663 :   for (i=1; i<lg(dec); i++) {
    4599        3724 :     I = gmael(dec,i,3);
    4600        3724 :     if (liftm) I = FpM_mul(liftm,I,p);
    4601        3724 :     I = shallowmatconcat(mkvec2(I,prad));
    4602        3724 :     gel(res,i) = I;
    4603             :   }
    4604             : 
    4605        1939 :   return gerepilecopy(av, res);
    4606             : }
    4607             : 
    4608             : /* finds a nontrivial ideal of O/prad or gen_0 if there is none. */
    4609             : static GEN
    4610         742 : algpdecompose_i(GEN al, GEN p, GEN zprad, GEN projs)
    4611             : {
    4612         742 :   pari_sp av = avma;
    4613         742 :   GEN prad = algpradical_i(al,p,zprad,projs);
    4614         742 :   return gerepileupto(av, algpdecompose0(al, prad, p, projs));
    4615             : }
    4616             : 
    4617             : /* ord is assumed to be in hnf wrt the integral basis of al. */
    4618             : /* assumes that alg_get_invbasis(al) is integral. */
    4619             : static GEN
    4620        1694 : alg_change_overorder_shallow(GEN al, GEN ord)
    4621             : {
    4622             :   GEN al2, mt, iord, mtx, den, den2, div;
    4623             :   long i, n;
    4624        1694 :   n = alg_get_absdim(al);
    4625             : 
    4626        1694 :   iord = QM_inv(ord);
    4627        1694 :   al2 = shallowcopy(al);
    4628        1694 :   ord = Q_remove_denom(ord,&den);
    4629             : 
    4630        1694 :   gel(al2,7) = Q_remove_denom(gel(al,7), &den2);
    4631        1694 :   if (den2) div = mulii(den,den2);
    4632         644 :   else      div = den;
    4633        1694 :   gel(al2,7) = ZM_Z_div(ZM_mul(gel(al2,7), ord), div);
    4634             : 
    4635        1694 :   gel(al2,8) = ZM_mul(iord, gel(al,8));
    4636             : 
    4637        1694 :   mt = cgetg(n+1,t_VEC);
    4638        1694 :   gel(mt,1) = matid(n);
    4639        1694 :   div = sqri(den);
    4640       19404 :   for (i=2; i<=n; i++) {
    4641       17710 :     mtx = algbasismultable(al,gel(ord,i));
    4642       17710 :     gel(mt,i) = ZM_mul(iord, ZM_mul(mtx, ord));
    4643       17710 :     gel(mt,i) = ZM_Z_divexact(gel(mt,i), div);
    4644             :   }
    4645        1694 :   gel(al2,9) = mt;
    4646             : 
    4647        1694 :   gel(al2,11) = algtracebasis(al2);
    4648             : 
    4649        1694 :   return al2;
    4650             : }
    4651             : 
    4652             : static GEN
    4653       10227 : algfromcenter(GEN al, GEN x)
    4654             : {
    4655       10227 :   GEN nf = alg_get_center(al);
    4656             :   long n;
    4657       10227 :   switch(alg_type(al)) {
    4658             :     case al_CYCLIC:
    4659        9135 :       n = alg_get_degree(al);
    4660        9135 :       break;
    4661             :     case al_CSA:
    4662        1092 :       n = alg_get_dim(al);
    4663        1092 :       break;
    4664             :     default:
    4665             :       return NULL; /*LCOV_EXCL_LINE*/
    4666             :   }
    4667       10227 :   return algalgtobasis(al, scalarcol(basistoalg(nf, x), n));
    4668             : }
    4669             : 
    4670             : /* x is an ideal of the center in hnf form */
    4671             : static GEN
    4672        2891 : algfromcenterhnf(GEN al, GEN x)
    4673             : {
    4674             :   GEN res;
    4675             :   long i;
    4676        2891 :   res = cgetg(lg(x), t_MAT);
    4677        2891 :   for (i=1; i<lg(x); i++) gel(res,i) = algfromcenter(al, gel(x,i));
    4678        2891 :   return res;
    4679             : }
    4680             : 
    4681             : /* assumes al is CSA or CYCLIC */
    4682             : static GEN
    4683        1197 : algcenter_precompute(GEN al, GEN p)
    4684             : {
    4685        1197 :   GEN fa, pdec, nfprad, projs, nf = alg_get_center(al);
    4686             :   long i, np;
    4687             : 
    4688        1197 :   pdec = idealprimedec(nf, p);
    4689        1197 :   settyp(pdec, t_COL);
    4690        1197 :   np = lg(pdec)-1;
    4691        1197 :   fa = mkmat2(pdec, const_col(np, gen_1));
    4692        1197 :   if (dvdii(nf_get_disc(nf), p))
    4693         329 :     nfprad = idealprodprime(nf, pdec);
    4694             :   else
    4695         868 :     nfprad = scalarmat_shallow(p, nf_get_degree(nf));
    4696        1197 :   fa = idealchineseinit(nf, fa);
    4697        1197 :   projs = cgetg(np+1, t_VEC);
    4698        1197 :   for (i=1; i<=np; i++) gel(projs, i) = idealchinese(nf, fa, vec_ei(np,i));
    4699        1197 :   return mkvec2(nfprad, projs);
    4700             : }
    4701             : 
    4702             : static GEN
    4703        2891 : algcenter_prad(GEN al, GEN p, GEN pre)
    4704             : {
    4705             :   GEN nfprad, zprad, mtprad;
    4706             :   long i;
    4707        2891 :   nfprad = gel(pre,1);
    4708        2891 :   zprad = algfromcenterhnf(al, nfprad);
    4709        2891 :   zprad = FpM_image(zprad, p);
    4710        2891 :   mtprad = cgetg(lg(zprad), t_VEC);
    4711        2891 :   for (i=1; i<lg(zprad); i++) gel(mtprad, i) = algbasismultable(al, gel(zprad,i));
    4712        2891 :   mtprad = shallowmatconcat(mtprad);
    4713        2891 :   zprad = FpM_image(mtprad, p);
    4714        2891 :   return zprad;
    4715             : }
    4716             : 
    4717             : static GEN
    4718        2891 : algcenter_p_projs(GEN al, GEN p, GEN pre)
    4719             : {
    4720             :   GEN projs, zprojs;
    4721             :   long i;
    4722        2891 :   projs = gel(pre,2);
    4723        2891 :   zprojs = cgetg(lg(projs), t_VEC);
    4724        2891 :   for (i=1; i<lg(projs); i++) gel(zprojs,i) = FpC_red(algfromcenter(al, gel(projs,i)),p);
    4725        2891 :   return zprojs;
    4726             : }
    4727             : 
    4728             : /* al is assumed to be simple */
    4729             : static GEN
    4730        1197 : alg_pmaximal(GEN al, GEN p)
    4731             : {
    4732        1197 :   GEN al2, prad, lord = gen_0, I, id, dec, zprad, projs, pre;
    4733             :   long n, i;
    4734        1197 :   n = alg_get_absdim(al);
    4735        1197 :   id = matid(n);
    4736        1197 :   al2 = al;
    4737             : 
    4738        1197 :   dbg_printf(0)("Round 2 (non-commutative) at p=%Ps, dim=%d\n", p, n);
    4739             : 
    4740        1197 :   pre = algcenter_precompute(al,p);
    4741             : 
    4742             :   while (1) {
    4743        3101 :     zprad = algcenter_prad(al2, p, pre);
    4744        2149 :     projs = algcenter_p_projs(al2, p, pre);
    4745        2149 :     if (lg(projs) == 2) projs = NULL;
    4746        2149 :     prad = algpradical_i(al2,p,zprad,projs);
    4747        2149 :     if (typ(prad) == t_INT) break;
    4748        2128 :     lord = algleftordermodp(al2,prad,p);
    4749        2128 :     if (!cmp_universal(lord,id)) break;
    4750         952 :     al2 = alg_change_overorder_shallow(al2,lord);
    4751             :   }
    4752             : 
    4753        1197 :   dec = algpdecompose0(al2,prad,p,projs);
    4754        3136 :   while (lg(dec)>2) {
    4755        1820 :     for (i=1; i<lg(dec); i++) {
    4756        1617 :       I = gel(dec,i);
    4757        1617 :       lord = algleftordermodp(al2,I,p);
    4758        1617 :       if (cmp_universal(lord,matid(n))) break;
    4759             :     }
    4760         945 :     if (i==lg(dec)) break;
    4761         742 :     al2 = alg_change_overorder_shallow(al2,lord);
    4762         742 :     zprad = algcenter_prad(al2, p, pre);
    4763         742 :     projs = algcenter_p_projs(al2, p, pre);
    4764         742 :     if (lg(projs) == 2) projs = NULL;
    4765         742 :     dec = algpdecompose_i(al2,p,zprad,projs);
    4766             :   }
    4767        1197 :   return al2;
    4768             : }
    4769             : 
    4770             : static GEN
    4771        5404 : algtracematrix(GEN al)
    4772             : {
    4773             :   GEN M, mt;
    4774             :   long n, i, j;
    4775        5404 :   n = alg_get_absdim(al);
    4776        5404 :   mt = alg_get_multable(al);
    4777        5404 :   M = cgetg(n+1, t_MAT);
    4778       43015 :   for (i=1; i<=n; i++)
    4779             :   {
    4780       37611 :     gel(M,i) = cgetg(n+1,t_MAT);
    4781      273847 :     for (j=1; j<=i; j++)
    4782      236236 :       gcoeff(M,j,i) = gcoeff(M,i,j) = algabstrace(al,gmael(mt,i,j));
    4783             :   }
    4784        5404 :   return M;
    4785             : }
    4786             : static GEN
    4787         133 : algdisc_i(GEN al) { return ZM_det(algtracematrix(al)); }
    4788             : GEN
    4789           7 : algdisc(GEN al)
    4790             : {
    4791           7 :   pari_sp av = avma;
    4792           7 :   checkalg(al); return gerepileuptoint(av, algdisc_i(al));
    4793             : }
    4794             : static GEN
    4795         126 : alg_maximal(GEN al)
    4796             : {
    4797         126 :   GEN fa = absZ_factor(algdisc_i(al));
    4798         126 :   return alg_maximal_primes(al, gel(fa,1));
    4799             : }
    4800             : 
    4801             : /** LATTICES **/
    4802             : 
    4803             : /*
    4804             :  Convention: lattice = [I,t] representing t*I, where
    4805             :  - I integral nonsingular upper-triangular matrix representing a lattice over
    4806             :    the integral basis of the algebra, and
    4807             :  - t>0 either an integer or a rational number.
    4808             : 
    4809             :  Recommended and returned by the functions below:
    4810             :  - I HNF and primitive
    4811             : */
    4812             : 
    4813             : /* TODO use hnfmodid whenever possible using a*O <= I <= O
    4814             :  * for instance a = ZM_det_triangular(I) */
    4815             : 
    4816             : static GEN
    4817       63343 : primlat(GEN lat)
    4818             : {
    4819             :   GEN m, t, c;
    4820       63343 :   m = alglat_get_primbasis(lat);
    4821       63343 :   t = alglat_get_scalar(lat);
    4822       63343 :   m = Q_primitive_part(m,&c);
    4823       63343 :   if (c) return mkvec2(m,gmul(t,c));
    4824       53760 :   return lat;
    4825             : }
    4826             : 
    4827             : /* assumes the lattice contains d * integral basis, d=0 allowed */
    4828             : GEN
    4829       51065 : alglathnf(GEN al, GEN m, GEN d)
    4830             : {
    4831       51065 :   pari_sp av = avma;
    4832             :   long N,i,j;
    4833             :   GEN m2, c;
    4834       51065 :   checkalg(al);
    4835       51065 :   N = alg_get_absdim(al);
    4836       51065 :   if (!d) d = gen_0;
    4837       51065 :   if (typ(m) == t_VEC) m = matconcat(m);
    4838       51065 :   if (typ(m) == t_COL) m = algleftmultable(al,m);
    4839       51065 :   if (typ(m) != t_MAT) pari_err_TYPE("alglathnf",m);
    4840       51058 :   if (typ(d) != t_FRAC && typ(d) != t_INT) pari_err_TYPE("alglathnf",d);
    4841       51058 :   if (lg(m)-1 < N || lg(gel(m,1))-1 != N) pari_err_DIM("alglathnf");
    4842      459242 :   for (i=1; i<=N; i++)
    4843     6820758 :     for (j=1; j<lg(m); j++)
    4844     6412546 :       if (typ(gcoeff(m,i,j)) != t_FRAC && typ(gcoeff(m,i,j)) != t_INT)
    4845           7 :         pari_err_TYPE("alglathnf", gcoeff(m,i,j));
    4846       51023 :   m2 = Q_primitive_part(m,&c);
    4847       51023 :   if (!c) c = gen_1;
    4848       51023 :   if (!signe(d)) d = detint(m2);
    4849       45593 :   else           d = gdiv(d,c); /* should be an integer */
    4850       51023 :   if (!signe(d)) pari_err_INV("alglathnf [m does not have full rank]", m2);
    4851       51009 :   m2 = ZM_hnfmodid(m2,d);
    4852       51009 :   return gerepilecopy(av, mkvec2(m2,c));
    4853             : }
    4854             : 
    4855             : static GEN
    4856       10689 : prepare_multipliers(GEN *a, GEN *b)
    4857             : {
    4858             :   GEN na, nb, da, db, d;
    4859       10689 :   na = numer_i(*a); da = denom_i(*a);
    4860       10689 :   nb = numer_i(*b); db = denom_i(*b);
    4861       10689 :   na = mulii(na,db);
    4862       10689 :   nb = mulii(nb,da);
    4863       10689 :   d = gcdii(na,nb);
    4864       10689 :   *a = diviiexact(na,d);
    4865       10689 :   *b = diviiexact(nb,d);
    4866       10689 :   return gdiv(d, mulii(da,db));
    4867             : }
    4868             : 
    4869             : static GEN
    4870       10689 : prepare_lat(GEN m1, GEN t1, GEN m2, GEN t2)
    4871             : {
    4872       10689 :   GEN d = prepare_multipliers(&t1, &t2);
    4873       10689 :   m1 = ZM_Z_mul(m1,t1);
    4874       10689 :   m2 = ZM_Z_mul(m2,t2);
    4875       10689 :   return mkvec3(m1,m2,d);
    4876             : }
    4877             : 
    4878             : static GEN
    4879       10689 : alglataddinter(GEN al, GEN lat1, GEN lat2, GEN *sum, GEN *inter)
    4880             : {
    4881             :   GEN d, m1, m2, t1, t2, M, prep, d1, d2, ds, di, K;
    4882       10689 :   checkalg(al);
    4883       10689 :   checklat(al,lat1);
    4884       10689 :   checklat(al,lat2);
    4885             : 
    4886       10689 :   m1 = alglat_get_primbasis(lat1);
    4887       10689 :   t1 = alglat_get_scalar(lat1);
    4888       10689 :   m2 = alglat_get_primbasis(lat2);
    4889       10689 :   t2 = alglat_get_scalar(lat2);
    4890       10689 :   prep = prepare_lat(m1, t1, m2, t2);
    4891       10689 :   m1 = gel(prep,1);
    4892       10689 :   m2 = gel(prep,2);
    4893       10689 :   d = gel(prep,3);
    4894       10689 :   M = matconcat(mkvec2(m1,m2));
    4895       10689 :   d1 = ZM_det_triangular(m1);
    4896       10689 :   d2 = ZM_det_triangular(m2);
    4897       10689 :   ds = gcdii(d1,d2);
    4898       10689 :   if (inter)
    4899             :   {
    4900        7112 :     di = diviiexact(mulii(d1,d2),ds);
    4901        7112 :     K = matkermod(M,di,sum);
    4902        7112 :     K = rowslice(K,1,lg(m1));
    4903        7112 :     *inter = hnfmodid(FpM_mul(m1,K,di),di);
    4904        7112 :     if (sum) *sum = hnfmodid(*sum,ds);
    4905             :   }
    4906        3577 :   else *sum = hnfmodid(M,ds);
    4907       10689 :   return d;
    4908             : }
    4909             : 
    4910             : GEN
    4911        3598 : alglatinter(GEN al, GEN lat1, GEN lat2, GEN* ptsum)
    4912             : {
    4913        3598 :   pari_sp av = avma;
    4914             :   GEN inter, d;
    4915        3598 :   d = alglataddinter(al, lat1, lat2, ptsum, &inter);
    4916        3598 :   inter = primlat(mkvec2(inter, d));
    4917        3598 :   if (ptsum)
    4918             :   {
    4919          14 :     *ptsum = primlat(mkvec2(*ptsum,d));
    4920          14 :     gerepileall(av, 2, &inter, ptsum);
    4921             :   }
    4922        3584 :   else inter = gerepilecopy(av, inter);
    4923        3598 :   return inter;
    4924             : }
    4925             : 
    4926             : GEN
    4927        7091 : alglatadd(GEN al, GEN lat1, GEN lat2, GEN* ptinter)
    4928             : {
    4929        7091 :   pari_sp av = avma;
    4930             :   GEN sum, d;
    4931        7091 :   d = alglataddinter(al, lat1, lat2, &sum, ptinter);
    4932        7091 :   sum = primlat(mkvec2(sum, d));
    4933        7091 :   if (ptinter)
    4934             :   {
    4935        3514 :     *ptinter = primlat(mkvec2(*ptinter,d));
    4936        3514 :     gerepileall(av, 2, &sum, ptinter);
    4937             :   }
    4938        3577 :   else sum = gerepilecopy(av, sum);
    4939        7091 :   return sum;
    4940             : }
    4941             : 
    4942             : int
    4943       31549 : alglatsubset(GEN al, GEN lat1, GEN lat2, GEN* ptindex)
    4944             : {
    4945             :   /* TODO version that returns the quotient as abelian group? */
    4946             :   /* return matrices to convert coordinates from one to other? */
    4947       31549 :   pari_sp av = avma;
    4948             :   int res;
    4949             :   GEN m1, m2, m2i, m, t;
    4950       31549 :   checkalg(al);
    4951       31549 :   checklat(al,lat1);
    4952       31549 :   checklat(al,lat2);
    4953       31549 :   m1 = alglat_get_primbasis(lat1);
    4954       31549 :   m2 = alglat_get_primbasis(lat2);
    4955       31549 :   m2i = RgM_inv_upper(m2);
    4956       31549 :   t = gdiv(alglat_get_scalar(lat1), alglat_get_scalar(lat2));
    4957       31549 :   m = RgM_Rg_mul(RgM_mul(m2i,m1), t);
    4958       31549 :   res = RgM_is_ZM(m);
    4959       31549 :   if (res && ptindex)
    4960             :   {
    4961        1757 :     *ptindex = mpabs(ZM_det_triangular(m));
    4962        1757 :     gerepileall(av,1,ptindex);
    4963             :   }
    4964       29792 :   else set_avma(av);
    4965       31549 :   return res;
    4966             : }
    4967             : 
    4968             : GEN
    4969        5264 : alglatindex(GEN al, GEN lat1, GEN lat2)
    4970             : {
    4971        5264 :   pari_sp av = avma;
    4972             :   long N;
    4973             :   GEN res;
    4974        5264 :   checkalg(al);
    4975        5264 :   checklat(al,lat1);
    4976        5264 :   checklat(al,lat2);
    4977        5264 :   N = alg_get_absdim(al);
    4978        5264 :   res = alglat_get_scalar(lat1);
    4979        5264 :   res = gdiv(res, alglat_get_scalar(lat2));
    4980        5264 :   res = gpowgs(res, N);
    4981        5264 :   res = gmul(res,RgM_det_triangular(alglat_get_primbasis(lat1)));
    4982        5264 :   res = gdiv(res, RgM_det_triangular(alglat_get_primbasis(lat2)));
    4983        5264 :   res = gabs(res,0);
    4984        5264 :   return gerepilecopy(av, res);
    4985             : }
    4986             : 
    4987             : GEN
    4988       45605 : alglatmul(GEN al, GEN lat1, GEN lat2)
    4989             : {
    4990       45605 :   pari_sp av = avma;
    4991             :   long N,i;
    4992             :   GEN m1, m2, m, V, lat, t, d, dp;
    4993       45605 :   checkalg(al);
    4994       45605 :   if (typ(lat1)==t_COL)
    4995             :   {
    4996       19292 :     if (typ(lat2)==t_COL)
    4997           7 :       pari_err_TYPE("alglatmul [one of lat1, lat2 has to be a lattice]", lat2);
    4998       19285 :     checklat(al,lat2);
    4999       19285 :     lat1 = Q_remove_denom(lat1,&d);
    5000       19285 :     m = algbasismultable(al,lat1);
    5001       19285 :     m2 = alglat_get_primbasis(lat2);
    5002       19285 :     dp = mulii(detint(m),ZM_det_triangular(m2));
    5003       19285 :     m = ZM_mul(m,m2);
    5004       19285 :     t = alglat_get_scalar(lat2);
    5005       19285 :     if (d) t = gdiv(t,d);
    5006             :   }
    5007             :   else /* typ(lat1)!=t_COL */
    5008             :   {
    5009       26313 :     checklat(al,lat1);
    5010       26313 :     if (typ(lat2)==t_COL)
    5011             :     {
    5012       19285 :       lat2 = Q_remove_denom(lat2,&d);
    5013       19285 :       m = algbasisrightmultable(al,lat2);
    5014       19285 :       m1 = alglat_get_primbasis(lat1);
    5015       19285 :       dp = mulii(detint(m),ZM_det_triangular(m1));
    5016       19285 :       m = ZM_mul(m,m1);
    5017       19285 :       t = alglat_get_scalar(lat1);
    5018       19285 :       if (d) t = gdiv(t,d);
    5019             :     }
    5020             :     else /* typ(lat2)!=t_COL */
    5021             :     {
    5022        7028 :       checklat(al,lat2);
    5023        7021 :       N = alg_get_absdim(al);
    5024        7021 :       m1 = alglat_get_primbasis(lat1);
    5025        7021 :       m2 = alglat_get_primbasis(lat2);
    5026        7021 :       dp = mulii(ZM_det_triangular(m1), ZM_det_triangular(m2));
    5027        7021 :       V = cgetg(N+1,t_VEC);
    5028       63189 :       for (i=1; i<=N; i++) {
    5029       56168 :         gel(V,i) = algbasismultable(al,gel(m1,i));
    5030       56168 :         gel(V,i) = ZM_mul(gel(V,i),m2);
    5031             :       }
    5032        7021 :       m = matconcat(V);
    5033        7021 :       t = gmul(alglat_get_scalar(lat1), alglat_get_scalar(lat2));
    5034             :     }
    5035             :   }
    5036             : 
    5037       45591 :   lat = alglathnf(al,m,dp);
    5038       45591 :   gel(lat,2) = gmul(alglat_get_scalar(lat), t);
    5039       45591 :   lat = primlat(lat);
    5040       45591 :   return gerepilecopy(av, lat);
    5041             : }
    5042             : 
    5043             : int
    5044       17521 : alglatcontains(GEN al, GEN lat, GEN x, GEN *ptc)
    5045             : {
    5046       17521 :   pari_sp av = avma;
    5047             :   GEN m, t, sol;
    5048       17521 :   checkalg(al);
    5049       17521 :   checklat(al,lat);
    5050       17521 :   m = alglat_get_primbasis(lat);
    5051       17521 :   t = alglat_get_scalar(lat);
    5052       17521 :   x = RgC_Rg_div(x,t);
    5053       17521 :   if (!RgV_is_ZV(x)) return gc_bool(av,0);
    5054       17521 :   sol = hnf_solve(m,x);
    5055       17521 :   if (!sol) return gc_bool(av,0);
    5056        8771 :   if (!ptc) return gc_bool(av,1);
    5057        8764 :   *ptc = sol; gerepileall(av,1,ptc); return 1;
    5058             : }
    5059             : 
    5060             : GEN
    5061        8771 : alglatelement(GEN al, GEN lat, GEN c)
    5062             : {
    5063        8771 :   pari_sp av = avma;
    5064             :   GEN res;
    5065        8771 :   checkalg(al);
    5066        8771 :   checklat(al,lat);
    5067        8771 :   if (typ(c)!=t_COL) pari_err_TYPE("alglatelement", c);
    5068        8764 :   res = ZM_ZC_mul(alglat_get_primbasis(lat),c);
    5069        8764 :   res = RgC_Rg_mul(res, alglat_get_scalar(lat));
    5070        8764 :   return gerepilecopy(av,res);
    5071             : }
    5072             : 
    5073             : /* idem QM_invimZ, knowing result is contained in 1/c*Z^n */
    5074             : static GEN
    5075        3535 : QM_invimZ_mod(GEN m, GEN c)
    5076             : {
    5077             :   GEN d, m0, K;
    5078        3535 :   m0 = Q_remove_denom(m, &d);
    5079        3535 :   if (d)    d = mulii(d,c);
    5080          21 :   else      d = c;
    5081        3535 :   K = matkermod(m0, d, NULL);
    5082        3535 :   if (lg(K)==1) K = scalarmat(d, lg(m)-1);
    5083        3521 :   else          K = hnfmodid(K, d);
    5084        3535 :   return RgM_Rg_div(K,c);
    5085             : }
    5086             : 
    5087             : /* If m is injective, computes a Z-basis of the submodule of elements whose
    5088             :  * image under m is integral */
    5089             : static GEN
    5090          14 : QM_invimZ(GEN m)
    5091             : {
    5092          14 :   return RgM_invimage(m, QM_ImQ_hnf(m));
    5093             : }
    5094             : 
    5095             : /* An isomorphism of R-modules M_{m,n}(R) -> R^{m*n} */
    5096             : static GEN
    5097       28322 : mat2col(GEN M, long m, long n)
    5098             : {
    5099             :   long i,j,k,p;
    5100             :   GEN C;
    5101       28322 :   p = m*n;
    5102       28322 :   C = cgetg(p+1,t_COL);
    5103      254702 :   for (i=1,k=1;i<=m;i++)
    5104     2036804 :     for (j=1;j<=n;j++,k++)
    5105     1810424 :       gel(C,k) = gcoeff(M,i,j);
    5106       28322 :   return C;
    5107             : }
    5108             : 
    5109             : static GEN
    5110        3535 : alglattransporter_i(GEN al, GEN lat1, GEN lat2, long right)
    5111             : {
    5112             :   GEN m1, m2, m2i, M, MT, mt, t1, t2, T, c;
    5113             :   long N, i;
    5114        3535 :   N = alg_get_absdim(al);
    5115        3535 :   m1 = alglat_get_primbasis(lat1);
    5116        3535 :   m2 = alglat_get_primbasis(lat2);
    5117        3535 :   m2i = RgM_inv_upper(m2);
    5118        3535 :   c = detint(m1);
    5119        3535 :   t1 = alglat_get_scalar(lat1);
    5120        3535 :   m1 = RgM_Rg_mul(m1,t1);
    5121        3535 :   t2 = alglat_get_scalar(lat2);
    5122        3535 :   m2i = RgM_Rg_div(m2i,t2);
    5123             : 
    5124        3535 :   MT = right? NULL: alg_get_multable(al);
    5125        3535 :   M = cgetg(N+1, t_MAT);
    5126       31815 :   for (i=1; i<=N; i++) {
    5127       28280 :     if (right) mt = algbasisrightmultable(al, vec_ei(N,i));
    5128       14168 :     else       mt = gel(MT,i);
    5129       28280 :     mt = RgM_mul(m2i,mt);
    5130       28280 :     mt = RgM_mul(mt,m1);
    5131       28280 :     gel(M,i) = mat2col(mt, N, N);
    5132             :   }
    5133             : 
    5134        3535 :   c = gdiv(t2,gmul(c,t1));
    5135        3535 :   c = denom_i(c);
    5136        3535 :   T = QM_invimZ_mod(M,c);
    5137        3535 :   return primlat(mkvec2(T,gen_1));
    5138             : }
    5139             : 
    5140             : /*
    5141             :    { x in al | x*lat1 subset lat2}
    5142             : */
    5143             : GEN
    5144        1771 : alglatlefttransporter(GEN al, GEN lat1, GEN lat2)
    5145             : {
    5146        1771 :   pari_sp av = avma;
    5147        1771 :   checkalg(al);
    5148        1771 :   checklat(al,lat1);
    5149        1771 :   checklat(al,lat2);
    5150        1771 :   return gerepilecopy(av, alglattransporter_i(al,lat1,lat2,0));
    5151             : }
    5152             : 
    5153             : /*
    5154             :    { x in al | lat1*x subset lat2}
    5155             : */
    5156             : GEN
    5157        1764 : alglatrighttransporter(GEN al, GEN lat1, GEN lat2)
    5158             : {
    5159        1764 :   pari_sp av = avma;
    5160        1764 :   checkalg(al);
    5161        1764 :   checklat(al,lat1);
    5162        1764 :   checklat(al,lat2);
    5163        1764 :   return gerepilecopy(av, alglattransporter_i(al,lat1,lat2,1));
    5164             : }
    5165             : 
    5166             : GEN
    5167          42 : algmakeintegral(GEN mt0, long maps)
    5168             : {
    5169          42 :   pari_sp av = avma;
    5170             :   long n,i;
    5171             :   GEN m,P,Pi,mt2,mt;
    5172          42 :   n = lg(mt0)-1;
    5173          42 :   mt = check_mt(mt0,NULL);
    5174          42 :   if (!mt) pari_err_TYPE("algmakeintegral", mt0);
    5175          21 :   if (isint1(Q_denom(mt0))) {
    5176           7 :     if (maps) mt = mkvec3(mt,matid(n),matid(n));
    5177           7 :     return gerepilecopy(av,mt);
    5178             :   }
    5179          14 :   dbg_printf(2)(" algmakeintegral: dim=%d, denom=%Ps\n", n, Q_denom(mt0));
    5180          14 :   m = cgetg(n+1,t_MAT);
    5181          56 :   for (i=1;i<=n;i++)
    5182          42 :     gel(m,i) = mat2col(gel(mt,i),n,n);
    5183          14 :   dbg_printf(2)(" computing order, dims m = %d x %d...\n", nbrows(m), lg(m)-1);
    5184          14 :   P = QM_invimZ(m);
    5185          14 :   dbg_printf(2)(" ...done.\n");
    5186          14 :   P = shallowmatconcat(mkvec2(col_ei(n,1),P));
    5187          14 :   P = hnf(P);
    5188          14 :   Pi = RgM_inv(P);
    5189          14 :   mt2 = change_Rgmultable(mt,P,Pi);
    5190          14 :   if (maps) mt2 = mkvec3(mt2,Pi,P); /* mt2, mt->mt2, mt2->mt */
    5191          14 :   return gerepilecopy(av,mt2);
    5192             : }
    5193             : 
    5194             : /** ORDERS **/
    5195             : 
    5196             : /** IDEALS **/
    5197             : 

Generated by: LCOV version 1.13