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.1 lcov report (development 24782-f7724578b4) Lines: 2972 3068 96.9 %
Date: 2019-12-06 05:56:26 Functions: 270 274 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      813555 : checkalg_i(GEN al)
      44             : {
      45             :   GEN mt, rnf;
      46      813555 :   if (typ(al) != t_VEC || lg(al) != 12) return 0;
      47      813359 :   mt = alg_get_multable(al);
      48      813359 :   if (typ(mt) != t_VEC || lg(mt) == 1 || typ(gel(mt,1)) != t_MAT) return 0;
      49      813338 :   rnf = alg_get_splittingfield(al);
      50      813338 :   if (isintzero(rnf) || !gequal0(alg_get_char(al))) return 1;
      51      459060 :   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      459053 :   return typ(rnf)==t_VEC && lg(rnf)==13;
      54             : }
      55             : void
      56      812883 : checkalg(GEN al)
      57      812883 : { 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     4812425 : alg_type(GEN al)
      88             : {
      89     4812425 :   if (isintzero(alg_get_splittingfield(al)) || !gequal0(alg_get_char(al))) return al_TABLE;
      90     3569909 :   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     2674210 :     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      224392 : alg_get_dim(GEN al)
     107             : {
     108             :   long d;
     109      224392 :   switch(alg_type(al)) {
     110       10521 :     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     1543481 : alg_get_absdim(GEN al)
     120             : {
     121     1543481 :   switch(alg_type(al)) {
     122      656021 :     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      774298 :       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     8070555 : 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     1225546 : alg_get_degree(GEN al)
     215             : {
     216             :   long ta;
     217     1225546 :   ta = alg_type(al);
     218     1225546 :   if (ta != al_CYCLIC && ta != al_CSA)
     219          21 :     pari_err_TYPE("alg_get_degree [use alginit]",al);
     220     1225525 :   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      293440 : alg_get_center(GEN al)
     231             : {
     232             :   long ta;
     233      293440 :   ta = alg_type(al);
     234      293440 :   if (ta != al_CSA && ta != al_CYCLIC)
     235           7 :     pari_err_TYPE("alg_get_center [use alginit]",al);
     236      293433 :   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       66675 : alg_get_abssplitting(GEN al)
     248             : {
     249       66675 :   long ta = alg_type(al), prec;
     250       66675 :   if (ta != al_CYCLIC && ta != al_CSA)
     251           0 :     pari_err_TYPE("alg_get_abssplitting [use alginit]",al);
     252       66675 :   prec = nf_get_prec(alg_get_center(al));
     253       66675 :   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       59024 : 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     2212521 : 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     5543796 : 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      231917 : 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        1106 :       i=n;
     329        2058 :       while (i>0) {
     330        1085 :         if (v[i]==b) { v[i] = -b; i--; } else { v[i]++; break; }
     331             :       }
     332         952 :       if (i==0) break;
     333             : 
     334         931 :       y1 = y0;
     335         931 :       for (i=1; i<=n; i++) y1 = nfadd(nf, y1, ZC_z_mul(gel(red,i), v[i]));
     336         931 :       if (!nfchecksigns(nf, y1, pl)) continue;
     337             : 
     338         812 :       ny = absi_shallow(nfnorm(nf, y1));
     339         812 :       if (!signe(ny)) continue;
     340         812 :       ny = diviiexact(ny,gcdii(ny,N));
     341         812 :       fan = Z_factor_limit(ny,1<<17);
     342         812 :       if (lg(fan)>1 && nbrows(fan)>0 && !isprime(gcoeff(fan,nbrows(fan),1)))
     343           7 :         continue;
     344             : 
     345         805 :       y2 = idealdivexact(nf,y1,idealadd(nf,y1,I));
     346         805 :       *fa = idealfactor(nf, y2);
     347        1582 :       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        4312 : Flm_invimage_init(GEN M, ulong p)
     464             : {
     465        4312 :   GEN v = Flm_indexrank(M, p), perm = gel(v,1);
     466        4312 :   GEN MM = rowpermute(M, perm); /* square invertible */
     467        4312 :   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      244531 : Flm_invimage_pre(GEN v, GEN y, ulong p)
     472             : {
     473      244531 :   GEN inv = gel(v,1), perm = gel(v,2);
     474      244531 :   return Flm_Flc_mul(inv, vecsmallpermute(y, perm), p);
     475             : }
     476             : 
     477             : GEN
     478        4984 : algradical(GEN al)
     479             : {
     480        4984 :   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        4984 :   checkalg(al);
     485        4984 :   P = alg_get_char(al);
     486        4984 :   mt = alg_get_multable(al);
     487        4984 :   n = alg_get_absdim(al);
     488        4984 :   dbg_printf(1)("algradical: char=%Ps, dim=%d\n", P, n);
     489        4984 :   traces = algtracematrix(al);
     490        4984 :   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) return gc_const(av, gen_0);
     496          70 :     return gerepileupto(av, K);
     497             :   }
     498        4417 :   dbg_printf(2)(" char>0, computing kernel...\n");
     499        4417 :   K = FpM_ker(traces, P);
     500        4417 :   dbg_printf(2)(" ...done.\n");
     501        4417 :   ni = lg(K)-1; if (!ni) return gc_const(av, gen_0);
     502        2828 :   if (abscmpiu(P,n)>0) return gerepileupto(av, K);
     503             : 
     504             :   /* tough case, p <= n. Ronyai's algorithm */
     505        2324 :   p = P[2]; l = 1;
     506        2324 :   expo = p; modu = p*p;
     507        2324 :   dbg_printf(2)(" char>0, hard case.\n");
     508        2324 :   while (modu<=(ulong)n) { l++; modu *= p; }
     509        2324 :   MT = ZMV_to_FlmV(mt, modu);
     510        2324 :   I = ZM_to_Flm(K,p); /* I_0 */
     511        6335 :   for (i=1; i<=l; i++) {/*compute I_i, expo = p^i, modu = p^(l+1) > n*/
     512             :     long j, lig,col;
     513        4312 :     GEN v = cgetg(ni+1, t_VECSMALL);
     514        4312 :     GEN invI = Flm_invimage_init(I, p);
     515        4312 :     dbg_printf(2)(" computing I_%d:\n", i);
     516        4312 :     traces = cgetg(ni+1,t_MAT);
     517       28910 :     for (j = 1; j <= ni; j++)
     518             :     {
     519       24598 :       GEN M = algbasismultable_Flm(MT, gel(I,j), modu);
     520       24598 :       uel(v,j) = algtracei(M, p,expo,modu);
     521             :     }
     522       28910 :     for (col=1; col<=ni; col++)
     523             :     {
     524       24598 :       GEN t = cgetg(n+1,t_VECSMALL); gel(traces,col) = t;
     525       24598 :       x = gel(I, col); /*col-th basis vector of I_{i-1}*/
     526      269129 :       for (lig=1; lig<=n; lig++)
     527             :       {
     528      244531 :         GEN y = _tablemul_ej_Fl(MT,x,lig,p);
     529      244531 :         GEN z = Flm_invimage_pre(invI, y, p);
     530      244531 :         uel(t,lig) = Flv_dotproduct(v, z, p);
     531             :       }
     532             :     }
     533        4312 :     dbg_printf(2)(" computing kernel...\n");
     534        4312 :     K = Flm_ker(traces, p);
     535        4312 :     dbg_printf(2)(" ...done.\n");
     536        4312 :     ni = lg(K)-1; if (!ni) return gc_const(av, gen_0);
     537        4011 :     I = Flm_mul(I,K,p);
     538        4011 :     expo *= p;
     539             :   }
     540        2023 :   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       19250 : alg_quotient0(GEN al, GEN S, GEN Si, long nq, GEN p, long maps)
     577             : {
     578       19250 :   GEN mt = cgetg(nq+1,t_VEC), P, Pi, d;
     579             :   long i;
     580       19250 :   dbg_printf(3)("  alg_quotient0: char=%Ps, dim=%d, dim I=%d\n", p, alg_get_absdim(al), lg(S)-1);
     581       74843 :   for (i=1; i<=nq; i++) {
     582       55593 :     GEN mti = algbasismultable(al,gel(S,i));
     583       55593 :     if (signe(p)) gel(mt,i) = FpM_mul(Si, FpM_mul(mti,S,p), p);
     584        5257 :     else          gel(mt,i) = RgM_mul(Si, RgM_mul(mti,S));
     585             :   }
     586       19250 :   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       19250 :   al = algtableinit_i(mt,p);
     597       19250 :   if (maps) al = mkvec3(al,Si,S); /* algebra, proj, lift */
     598       19250 :   return al;
     599             : }
     600             : 
     601             : /* quotient of an algebra by a nontrivial two-sided ideal */
     602             : GEN
     603        2695 : alg_quotient(GEN al, GEN I, long maps)
     604             : {
     605        2695 :   pari_sp av = avma;
     606             :   GEN p, IS, ISi, S, Si;
     607             :   long n, ni;
     608             : 
     609        2695 :   checkalg(al);
     610        2695 :   p = alg_get_char(al);
     611        2695 :   n = alg_get_absdim(al);
     612        2695 :   ni = lg(I)-1;
     613             : 
     614             :   /* force first vector of complement to be the identity */
     615        2695 :   IS = shallowconcat(I, gcoeff(alg_get_multable(al),1,1));
     616        2695 :   if (signe(p)) {
     617        2667 :     IS = FpM_suppl(IS,p);
     618        2667 :     ISi = FpM_inv(IS,p);
     619             :   }
     620             :   else {
     621          28 :     IS = suppl(IS);
     622          28 :     ISi = RgM_inv(IS);
     623             :   }
     624        2695 :   S = vecslice(IS, ni+1, n);
     625        2695 :   Si = rowslice(ISi, ni+1, n);
     626        2695 :   return gerepilecopy(av, alg_quotient0(al, S, Si, n-ni, p, maps));
     627             : }
     628             : 
     629             : static GEN
     630       26621 : 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       26621 :   if (gequal0(gel(m,1))) return zeromat(nbrows(m),0);
     635             : 
     636       26607 :   if (signe(p)) ir = FpM_indexrank(m,p);
     637        1498 :   else          ir = indexrank(m);
     638             : 
     639       26607 :   icol = gel(ir,2);
     640       26607 :   if (icol[1]==1) return extract0(m,icol,NULL);
     641             : 
     642           0 :   irow = gel(ir,1);
     643           0 :   M = extract0(m, irow, icol);
     644           0 :   c = extract0(gel(m,1), irow, NULL);
     645           0 :   if (signe(p)) x = FpM_FpC_invimage(M,c,p);
     646           0 :   else          x = inverseimage(M,c); /* TODO modulo a small prime */
     647             : 
     648           0 :   for (i=1; i<lg(x); i++)
     649             :   {
     650           0 :     if (!gequal0(gel(x,i)))
     651             :     {
     652           0 :       icol[i] = 1;
     653           0 :       vecsmall_sort(icol);
     654           0 :       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        7980 : alg_centralproj(GEN al, GEN z, long maps)
     665             : {
     666        7980 :   pari_sp av = avma;
     667             :   GEN S, U, Ui, alq, p;
     668        7980 :   long i, iu, lz = lg(z);
     669             : 
     670        7980 :   checkalg(al);
     671        7980 :   if (typ(z) != t_VEC) pari_err_TYPE("alcentralproj",z);
     672        7973 :   p = alg_get_char(al);
     673        7973 :   dbg_printf(3)("  alg_centralproj: char=%Ps, dim=%d, #z=%d\n", p, alg_get_absdim(al), lz-1);
     674        7973 :   S = cgetg(lz,t_VEC); /* S[i] = Im(z_i) */
     675       24542 :   for (i=1; i<lz; i++)
     676             :   {
     677       16569 :     GEN mti = algbasismultable(al, gel(z,i));
     678       16569 :     gel(S,i) = image_keep_first(mti,p);
     679             :   }
     680        7973 :   U = shallowconcat1(S); /* U = [Im(z_1)|Im(z_2)|...|Im(z_nz)], n x n */
     681        7973 :   if (lg(U)-1 < alg_get_absdim(al)) pari_err_TYPE("alcentralproj [z[i]'s not surjective]",z);
     682        7966 :   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        7966 :   alq = cgetg(lz,t_VEC);
     687       24521 :   for (iu=0,i=1; i<lz; i++)
     688             :   {
     689       16555 :     long nq = lg(gel(S,i))-1, ju = iu + nq;
     690       16555 :     GEN Si = rowslice(Ui, iu+1, ju);
     691       16555 :     gel(alq, i) = alg_quotient0(al,gel(S,i),Si,nq,p,maps);
     692       16555 :     iu = ju;
     693             :   }
     694        7966 :   return gerepilecopy(av, alq);
     695             : }
     696             : 
     697             : /* al is an al_TABLE */
     698             : static GEN
     699       17976 : algtablecenter(GEN al)
     700             : {
     701       17976 :   pari_sp av = avma;
     702             :   long n, i, j, k, ic;
     703             :   GEN C, cij, mt, p;
     704             : 
     705       17976 :   n = alg_get_absdim(al);
     706       17976 :   mt = alg_get_multable(al);
     707       17976 :   p = alg_get_char(al);
     708       17976 :   C = cgetg(n+1,t_MAT);
     709       87703 :   for (j=1; j<=n; j++)
     710             :   {
     711       69727 :     gel(C,j) = cgetg(n*n-n+1,t_COL);
     712       69727 :     ic = 1;
     713      578487 :     for (i=2; i<=n; i++) {
     714      508760 :       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      508760 :       for (k=1; k<=n; k++, ic++) gcoeff(C,ic,j) = gel(cij, k);
     717             :     }
     718             :   }
     719       17976 :   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        3668 : algprimesubalg(GEN al)
     734             : {
     735        3668 :   pari_sp av = avma;
     736             :   GEN p, Z, F, K;
     737             :   long nz, i;
     738        3668 :   checkalg(al);
     739        3668 :   p = alg_get_char(al);
     740        3668 :   if (!signe(p)) pari_err_DOMAIN("algprimesubalg","characteristic","=",gen_0,p);
     741             : 
     742        3654 :   Z = algtablecenter(al);
     743        3654 :   nz = lg(Z)-1;
     744        3654 :   if (nz==1) return Z;
     745             : 
     746        2569 :   F = cgetg(nz+1, t_MAT);
     747       13860 :   for (i=1; i<=nz; i++) {
     748       11291 :     GEN zi = gel(Z,i);
     749       11291 :     gel(F,i) = FpC_sub(algpow(al,zi,p),zi,p);
     750             :   }
     751        2569 :   K = FpM_ker(F,p);
     752        2569 :   return gerepileupto(av, FpM_mul(Z,K,p));
     753             : }
     754             : 
     755             : 
     756             : static GEN
     757        9775 : _FpX_mul(void* D, GEN x, GEN y) { return FpX_mul(x,y,(GEN)D); }
     758             : static GEN
     759       25980 : _FpX_pow(void* D, GEN x, GEN n) { return FpX_powu(x,itos(n),(GEN)D); }
     760             : static GEN
     761       16205 : FpX_factorback(GEN fa, GEN p)
     762             : {
     763       16205 :   return gen_factorback(gel(fa,1), zv_to_ZV(gel(fa,2)), (void *)p, &_FpX_mul, &_FpX_pow);
     764             : }
     765             : 
     766             : static GEN
     767       14518 : out_decompose(GEN t, GEN Z, GEN P, GEN p)
     768             : {
     769       14518 :   GEN ali = gel(t,1), projm = gel(t,2), liftm = gel(t,3), pZ;
     770       14518 :   if (signe(p)) pZ = FpM_image(FpM_mul(projm,Z,p),p);
     771        1407 :   else          pZ = image(RgM_mul(projm,Z));
     772       14518 :   return mkvec5(ali, projm, liftm, pZ, P);
     773             : }
     774             : /* fa factorization of charpol(x) */
     775             : static GEN
     776        7301 : alg_decompose_from_facto(GEN al, GEN x, GEN fa, GEN Z, long mini)
     777             : {
     778        7301 :   long k = lgcols(fa)-1, k2 = mini? 1: k/2;
     779        7301 :   GEN v1 = rowslice(fa,1,k2);
     780        7301 :   GEN v2 = rowslice(fa,k2+1,k);
     781        7301 :   GEN alq, P, Q, p = alg_get_char(al);
     782        7301 :   dbg_printf(3)("  alg_decompose_from_facto\n");
     783        7301 :   if (signe(p)) {
     784        6580 :     P = FpX_factorback(v1, p);
     785        6580 :     Q = FpX_factorback(v2, p);
     786        6580 :     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        7301 :   P = algpoleval(al, P, x);
     794        7301 :   if (signe(p)) Q = FpC_sub(col_ei(lg(P)-1,1), P, p);
     795         721 :   else          Q = gsub(gen_1, P);
     796        7301 :   if (gequal0(P) || gequal0(Q)) return NULL;
     797        7301 :   alq = alg_centralproj(al, mkvec2(P,Q), 1);
     798             : 
     799        7301 :   P = out_decompose(gel(alq,1), Z, P, p); if (mini) return P;
     800        7217 :   Q = out_decompose(gel(alq,2), Z, Q, p);
     801        7217 :   return mkvec2(P,Q);
     802             : }
     803             : 
     804             : static GEN
     805       11760 : random_pm1(long n)
     806             : {
     807       11760 :   GEN z = cgetg(n+1,t_VECSMALL);
     808             :   long i;
     809       11760 :   for (i = 1; i <= n; i++) z[i] = random_bits(5)%3 - 1;
     810       11760 :   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       13718 : try_fact(GEN al, GEN x, GEN zx, GEN Z, GEN Zal, long mini, GEN* pt_primelt)
     819             : {
     820       13718 :   GEN z, dec0, dec1, cp = algcharpoly(Zal,zx,0,1), fa, p = alg_get_char(al);
     821             :   long nfa, e;
     822       13718 :   dbg_printf(3)("  try_fact: zx=%Ps\n", zx);
     823       13718 :   if (signe(p)) fa = FpX_factor(cp,p);
     824        1330 :   else          fa = factor(cp);
     825       13718 :   dbg_printf(3)("  charpoly=%Ps\n", fa);
     826       13718 :   nfa = nbrows(fa);
     827       13718 :   if (nfa == 1) {
     828        6417 :     if (signe(p)) e = gel(fa,2)[1];
     829         609 :     else          e = itos(gcoeff(fa,1,2));
     830        6417 :     if (e == 1) {
     831        3689 :       if (pt_primelt != NULL) *pt_primelt = mkvec2(x, cp);
     832        3689 :       return gen_0;
     833             :     }
     834        2728 :     else return NULL;
     835             :   }
     836        7301 :   dec0 = alg_decompose_from_facto(al, x, fa, Z, mini);
     837        7301 :   if (!dec0) return NULL;
     838        7301 :   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       19971 : 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       19971 :   long i, nz = lg(Z)-1;
     869             : 
     870       19971 :   if (nz == 1) {
     871        8981 :     if (pt_primelt != 0) *pt_primelt = mkvec2(zerocol(alg_get_dim(al)), pol_x(0));
     872        8981 :     return gen_0;
     873             :   }
     874       10990 :   p = alg_get_char(al);
     875       10990 :   dbg_printf(2)(" alg_decompose: char=%Ps, dim=%d, dim Z=%d\n", p, alg_get_absdim(al), nz);
     876       10990 :   Zal = alg_subalg(al,Z);
     877       10990 :   Z = gel(Zal,2);
     878       10990 :   Zal = gel(Zal,1);
     879       10990 :   av = avma;
     880             : 
     881       10990 :   rand = random_pm1(nz);
     882       10990 :   zx = zc_to_ZC(rand);
     883       10990 :   if (signe(p)) {
     884       10017 :     zx = FpC_red(zx,p);
     885       10017 :     x = ZM_zc_mul(Z,rand);
     886       10017 :     x = FpC_red(x,p);
     887             :   }
     888         973 :   else x = RgM_zc_mul(Z,rand);
     889       10990 :   dec0 = try_fact(al,x,zx,Z,Zal,mini,pt_primelt);
     890       10990 :   if (dec0) return dec0;
     891        2672 :   set_avma(av);
     892             : 
     893        2728 :   for (i=2; i<=nz; i++)
     894             :   {
     895        2721 :     dec0 = try_fact(al,gel(Z,i),col_ei(nz,i),Z,Zal,mini,pt_primelt);
     896        2721 :     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       16366 : alg_decompose_total(GEN al, GEN Z, long maps)
     911             : {
     912             :   GEN dec, sc, p;
     913             :   long i;
     914             : 
     915       16366 :   dec = alg_decompose(al, Z, 0, NULL);
     916       16366 :   if (isintzero(dec))
     917             :   {
     918        9149 :     if (maps) {
     919        6517 :       long n = alg_get_absdim(al);
     920        6517 :       al = mkvec3(al, matid(n), matid(n));
     921             :     }
     922        9149 :     return mkvec(al);
     923             :   }
     924        7217 :   p = alg_get_char(al); if (!signe(p)) p = NULL;
     925        7217 :   sc = cgetg(lg(dec), t_VEC);
     926       21651 :   for (i=1; i<lg(sc); i++) {
     927       14434 :     GEN D = gel(dec,i), a = gel(D,1), Za = gel(D,4);
     928       14434 :     GEN S = alg_decompose_total(a, Za, maps);
     929       14434 :     gel(sc,i) = S;
     930       14434 :     if (maps)
     931             :     {
     932       10178 :       GEN projm = gel(D,2), liftm = gel(D,3);
     933       10178 :       long j, lS = lg(S);
     934       27803 :       for (j=1; j<lS; j++)
     935             :       {
     936       17625 :         GEN Sj = gel(S,j), p2 = gel(Sj,2), l2 = gel(Sj,3);
     937       17625 :         if (p) p2 = FpM_mul(p2, projm, p);
     938          49 :         else   p2 = RgM_mul(p2, projm);
     939       17625 :         if (p) l2 = FpM_mul(liftm, l2, p);
     940          49 :         else   l2 = RgM_mul(liftm, l2);
     941       17625 :         gel(Sj,2) = p2;
     942       17625 :         gel(Sj,3) = l2;
     943             :       }
     944             :     }
     945             :   }
     946        7217 :   return shallowconcat1(sc);
     947             : }
     948             : 
     949             : static GEN
     950       11046 : alg_subalg(GEN al, GEN basis)
     951             : {
     952       11046 :   GEN invbasis, mt, p = alg_get_char(al), al2;
     953       11046 :   long i, j, n = lg(basis)-1;
     954       11046 :   if (!signe(p)) p = NULL;
     955       11046 :   basis = shallowmatconcat(mkvec2(col_ei(n,1),basis));
     956       11046 :   if (p)
     957             :   {
     958       10052 :     basis = image_keep_first(basis,p);
     959       10052 :     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       11046 :   mt = cgetg(n+1,t_VEC);
     968       11046 :   gel(mt,1) = matid(n);
     969       37162 :   for (i=2; i<=n; i++) {
     970       26116 :     GEN mtx = cgetg(n+1,t_MAT), x = gel(basis,i);
     971       26116 :     gel(mtx,1) = col_ei(n,i);
     972      165412 :     for (j=2; j<=n; j++) {
     973      139296 :       GEN xy = algmul(al, x, gel(basis,j));
     974      139296 :       if (p) gel(mtx,j) = FpM_FpC_mul(invbasis, xy, p);
     975       28070 :       else   gel(mtx,j) = RgM_RgC_mul(invbasis, xy);
     976             :     }
     977       26116 :     gel(mt,i) = mtx;
     978             :   }
     979       11046 :   al2 = algtableinit_i(mt,p);
     980       11046 :   al2 = mkvec2(al2,basis);
     981       11046 :   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       11788 : cmp_algebra(GEN x, GEN y)
     998             : {
     999             :   long d;
    1000       11788 :   d = gel(x,1)[1] - gel(y,1)[1]; if (d) return d < 0? -1: 1;
    1001       10591 :   d = gel(x,1)[2] - gel(y,1)[2]; if (d) return d < 0? -1: 1;
    1002       10591 :   return cmp_universal(gel(x,2), gel(y,2));
    1003             : }
    1004             : 
    1005             : GEN
    1006        3745 : algsimpledec_ss(GEN al, long maps)
    1007             : {
    1008        3745 :   pari_sp av = avma;
    1009             :   GEN Z, p, r, res, perm;
    1010             :   long i, l, n;
    1011        3745 :   checkalg(al);
    1012        3745 :   p = alg_get_char(al);
    1013        3745 :   dbg_printf(1)("algsimpledec_ss: char=%Ps, dim=%d\n", p, alg_get_absdim(al));
    1014        3745 :   if (signe(p)) Z = algprimesubalg(al);
    1015         245 :   else          Z = algtablecenter(al);
    1016             : 
    1017        3745 :   if (lg(Z) == 2) {/* dim Z = 1 */
    1018        1813 :     n = alg_get_absdim(al);
    1019        1813 :     set_avma(av);
    1020        1813 :     if (!maps) return mkveccopy(al);
    1021        1687 :     retmkvec(mkvec3(gcopy(al), matid(n), matid(n)));
    1022             :   }
    1023        1932 :   res = alg_decompose_total(al, Z, maps);
    1024        1932 :   l = lg(res); r = cgetg(l, t_VEC);
    1025       11081 :   for (i = 1; i < l; i++)
    1026             :   {
    1027        9149 :     GEN A = maps? gmael(res,i,1): gel(res,i);
    1028        9149 :     gel(r,i) = mkvec2(mkvecsmall2(alg_get_dim(A), lg(algtablecenter(A))),
    1029             :                       alg_get_multable(A));
    1030             :   }
    1031        1932 :   perm = gen_indexsort(r, (void*)cmp_algebra, &cmp_nodata);
    1032        1932 :   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       36113 : check_mt_noid(GEN mt, GEN p)
    1385             : {
    1386             :   long i, l;
    1387       36113 :   GEN MT = cgetg_copy(mt, &l);
    1388       36113 :   if (typ(MT) != t_VEC || l == 1) return NULL;
    1389      174627 :   for (i = 1; i < l; i++)
    1390             :   {
    1391      138556 :     GEN M = gel(mt,i);
    1392      138556 :     if (typ(M) != t_MAT || lg(M) != l || lgcols(M) != l) return NULL;
    1393      138535 :     if (p) M = RgM_to_FpM(M,p);
    1394      138535 :     gel(MT,i) = M;
    1395             :   }
    1396       36071 :   return MT;
    1397             : }
    1398             : static GEN
    1399       35651 : check_mt(GEN mt, GEN p)
    1400             : {
    1401             :   long i;
    1402             :   GEN MT;
    1403       35651 :   MT = check_mt_noid(mt, p);
    1404       35651 :   if (!MT || !ZM_isidentity(gel(MT,1))) return NULL;
    1405      135490 :   for (i=2; i<lg(MT); i++)
    1406       99860 :     if (ZC_is_ei(gmael(MT,i,1)) != i) return NULL;
    1407       35630 :   return MT;
    1408             : }
    1409             : 
    1410             : static GEN
    1411         161 : check_relmt(GEN nf, GEN mt)
    1412             : {
    1413         161 :   long i, l = lg(mt), j, k;
    1414         161 :   GEN MT = gcopy(mt), a, b, d;
    1415         161 :   if (typ(MT) != t_VEC || l == 1) return NULL;
    1416         623 :   for (i = 1; i < l; i++)
    1417             :   {
    1418         483 :     GEN M = gel(MT,i);
    1419         483 :     if (typ(M) != t_MAT || lg(M) != l || lgcols(M) != l) return NULL;
    1420        2478 :     for (k = 1; k < l; k++)
    1421       12523 :       for (j = 1; j < l; j++)
    1422             :       {
    1423       10528 :         a = gcoeff(M,j,k);
    1424       10528 :         if (typ(a)==t_INT) continue;
    1425        1771 :         b = algtobasis(nf,a);
    1426        1771 :         d = Q_denom(b);
    1427        1771 :         if (!isint1(d))
    1428          14 :           pari_err_DOMAIN("alg_csa_table", "denominator(mt)", "!=", gen_1, mt);
    1429        1757 :         gcoeff(M,j,k) = lift(basistoalg(nf,b));
    1430             :       }
    1431         469 :     if (i > 1 && RgC_is_ei(gel(M,1)) != i) return NULL; /* i = 1 checked at end */
    1432         462 :     gel(MT,i) = M;
    1433             :   }
    1434         140 :   if (!RgM_isidentity(gel(MT,1))) return NULL;
    1435         140 :   return MT;
    1436             : }
    1437             : 
    1438             : 
    1439             : int
    1440         469 : algisassociative(GEN mt0, GEN p)
    1441             : {
    1442         469 :   pari_sp av = avma;
    1443             :   long i, j, k, n;
    1444             :   GEN M, mt;
    1445             : 
    1446         469 :   if (checkalg_i(mt0)) { p = alg_get_char(mt0); mt0 = alg_get_multable(mt0); }
    1447         469 :   if (typ(p) != t_INT) pari_err_TYPE("algisassociative",p);
    1448         462 :   mt = check_mt_noid(mt0, isintzero(p)? NULL: p);
    1449         462 :   if (!mt) pari_err_TYPE("algisassociative (mult. table)", mt0);
    1450         427 :   if (!ZM_isidentity(gel(mt,1))) return gc_bool(av,0);
    1451         413 :   n = lg(mt)-1;
    1452         413 :   M = cgetg(n+1,t_MAT);
    1453         413 :   for (j=1; j<=n; j++) gel(M,j) = cgetg(n+1,t_COL);
    1454        3402 :   for (i=1; i<=n; i++)
    1455             :   {
    1456        2989 :     GEN mi = gel(mt,i);
    1457        2989 :     for (j=1; j<=n; j++) gcoeff(M,i,j) = gel(mi,j); /* ei.ej */
    1458             :   }
    1459        2975 :   for (i=2; i<=n; i++) {
    1460        2569 :     GEN mi = gel(mt,i);
    1461       28777 :     for (j=2; j<=n; j++) {
    1462      367759 :       for (k=2; k<=n; k++) {
    1463             :         GEN x, y;
    1464      341551 :         if (signe(p)) {
    1465      242039 :           x = _tablemul_ej_Fp(mt,gcoeff(M,i,j),k,p);
    1466      242039 :           y = FpM_FpC_mul(mi,gcoeff(M,j,k),p);
    1467             :         }
    1468             :         else {
    1469       99512 :           x = _tablemul_ej(mt,gcoeff(M,i,j),k);
    1470       99512 :           y = RgM_RgC_mul(mi,gcoeff(M,j,k));
    1471             :         }
    1472             :         /* not cmp_universal: must not fail on 0 == Mod(0,2) for instance */
    1473      341551 :         if (!gequal(x,y)) return gc_bool(av,0);
    1474             :       }
    1475             :     }
    1476             :   }
    1477         406 :   return gc_bool(av,1);
    1478             : }
    1479             : 
    1480             : int
    1481         350 : algiscommutative(GEN al) /* assumes e_1 = 1 */
    1482             : {
    1483             :   long i,j,k,N,sp;
    1484             :   GEN mt,a,b,p;
    1485         350 :   checkalg(al);
    1486         350 :   if (alg_type(al) != al_TABLE) return alg_get_degree(al)==1;
    1487         308 :   N = alg_get_absdim(al);
    1488         308 :   mt = alg_get_multable(al);
    1489         308 :   p = alg_get_char(al);
    1490         308 :   sp = signe(p);
    1491        1449 :   for (i=2; i<=N; i++)
    1492        9464 :     for (j=2; j<=N; j++)
    1493       85820 :       for (k=1; k<=N; k++) {
    1494       77553 :         a = gcoeff(gel(mt,i),k,j);
    1495       77553 :         b = gcoeff(gel(mt,j),k,i);
    1496       77553 :         if (sp) {
    1497       73423 :           if (cmpii(Fp_red(a,p), Fp_red(b,p))) return 0;
    1498             :         }
    1499        4130 :         else if (gcmp(a,b)) return 0;
    1500             :       }
    1501         252 :   return 1;
    1502             : }
    1503             : 
    1504             : int
    1505         350 : algissemisimple(GEN al)
    1506             : {
    1507         350 :   pari_sp av = avma;
    1508             :   GEN rad;
    1509         350 :   checkalg(al);
    1510         350 :   if (alg_type(al) != al_TABLE) return 1;
    1511         308 :   rad = algradical(al);
    1512         308 :   set_avma(av);
    1513         308 :   return gequal0(rad);
    1514             : }
    1515             : 
    1516             : /* ss : known to be semisimple */
    1517             : int
    1518         259 : algissimple(GEN al, long ss)
    1519             : {
    1520         259 :   pari_sp av = avma;
    1521             :   GEN Z, dec, p;
    1522         259 :   checkalg(al);
    1523         259 :   if (alg_type(al) != al_TABLE) return 1;
    1524         224 :   if (!ss && !algissemisimple(al)) return 0;
    1525             : 
    1526         182 :   p = alg_get_char(al);
    1527         182 :   if (signe(p)) Z = algprimesubalg(al);
    1528          91 :   else          Z = algtablecenter(al);
    1529             : 
    1530         182 :   if (lg(Z) == 2) {/* dim Z = 1 */
    1531         105 :     set_avma(av);
    1532         105 :     return 1;
    1533             :   }
    1534          77 :   dec = alg_decompose(al, Z, 1, NULL);
    1535          77 :   set_avma(av);
    1536          77 :   return gequal0(dec);
    1537             : }
    1538             : 
    1539             : static long
    1540         329 : is_place_emb(GEN nf, GEN pl)
    1541             : {
    1542             :   long r, r1, r2;
    1543         329 :   if (typ(pl) != t_INT) pari_err_TYPE("is_place_emb", pl);
    1544         315 :   if (signe(pl)<=0) pari_err_DOMAIN("is_place_emb", "pl", "<=", gen_0, pl);
    1545         308 :   nf_get_sign(nf,&r1,&r2); r = r1+r2;
    1546         308 :   if (cmpiu(pl,r)>0) pari_err_DOMAIN("is_place_emb", "pl", ">", utoi(r), pl);
    1547         294 :   return itou(pl);
    1548             : }
    1549             : 
    1550             : static long
    1551         294 : alghasse_emb(GEN al, long emb)
    1552             : {
    1553         294 :   GEN nf = alg_get_center(al);
    1554         294 :   long r1 = nf_get_r1(nf);
    1555         294 :   return (emb <= r1)? alg_get_hasse_i(al)[emb]: 0;
    1556             : }
    1557             : 
    1558             : static long
    1559         399 : alghasse_pr(GEN al, GEN pr)
    1560             : {
    1561         399 :   GEN hf = alg_get_hasse_f(al);
    1562         399 :   long i = tablesearch(gel(hf,1), pr, &cmp_prime_ideal);
    1563         399 :   return i? gel(hf,2)[i]: 0;
    1564             : }
    1565             : 
    1566             : static long
    1567         735 : alghasse_0(GEN al, GEN pl)
    1568             : {
    1569             :   GEN pr, nf;
    1570         735 :   if (alg_type(al)== al_CSA)
    1571           7 :     pari_err_IMPL("computation of Hasse invariants over table CSA");
    1572         728 :   if ((pr = get_prid(pl))) return alghasse_pr(al, pr);
    1573         329 :   nf = alg_get_center(al);
    1574         329 :   return alghasse_emb(al, is_place_emb(nf, pl));
    1575             : }
    1576             : GEN
    1577         210 : alghasse(GEN al, GEN pl)
    1578             : {
    1579             :   long h;
    1580         210 :   checkalg(al);
    1581         210 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("alghasse [use alginit]",al);
    1582         203 :   h = alghasse_0(al,pl);
    1583         161 :   return sstoQ(h, alg_get_degree(al));
    1584             : }
    1585             : 
    1586             : /* h >= 0, d >= 0 */
    1587             : static long
    1588         812 : indexfromhasse(long h, long d) { return d/ugcd(h,d); }
    1589             : 
    1590             : long
    1591         728 : algindex(GEN al, GEN pl)
    1592             : {
    1593             :   long d, res, i, l;
    1594             :   GEN hi, hf;
    1595             : 
    1596         728 :   checkalg(al);
    1597         721 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algindex [use alginit]",al);
    1598         714 :   d = alg_get_degree(al);
    1599         714 :   if (pl) return indexfromhasse(alghasse_0(al,pl), d);
    1600             : 
    1601             :   /* else : global index */
    1602         182 :   res = 1;
    1603         182 :   hi = alg_get_hasse_i(al); l = lg(hi);
    1604         182 :   for (i=1; i<l && res!=d; i++) res = ulcm(res, indexfromhasse(hi[i],d));
    1605         182 :   hf = gel(alg_get_hasse_f(al), 2); l = lg(hf);
    1606         182 :   for (i=1; i<l && res!=d; i++) res = ulcm(res, indexfromhasse(hf[i],d));
    1607         182 :   return res;
    1608             : }
    1609             : 
    1610             : int
    1611         203 : algisdivision(GEN al, GEN pl)
    1612             : {
    1613         203 :   checkalg(al);
    1614         203 :   if (alg_type(al) == al_TABLE) {
    1615          21 :     if (!algissimple(al,0)) return 0;
    1616          14 :     if (algiscommutative(al)) return 1;
    1617           7 :     pari_err_IMPL("algisdivision for table algebras");
    1618             :   }
    1619         182 :   return algindex(al,pl) == alg_get_degree(al);
    1620             : }
    1621             : 
    1622             : int
    1623         182 : algissplit(GEN al, GEN pl)
    1624             : {
    1625         182 :   checkalg(al);
    1626         182 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algissplit [use alginit]", al);
    1627         175 :   return algindex(al,pl) == 1;
    1628             : }
    1629             : 
    1630             : int
    1631         182 : algisramified(GEN al, GEN pl)
    1632             : {
    1633         182 :   checkalg(al);
    1634         182 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algisramified [use alginit]", al);
    1635         175 :   return algindex(al,pl) != 1;
    1636             : }
    1637             : 
    1638             : GEN
    1639          91 : algramifiedplaces(GEN al)
    1640             : {
    1641          91 :   pari_sp av = avma;
    1642             :   GEN ram, hf, hi, Lpr;
    1643             :   long r1, count, i;
    1644          91 :   checkalg(al);
    1645          91 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algramifiedplaces [use alginit]", al);
    1646          84 :   r1 = nf_get_r1(alg_get_center(al));
    1647          84 :   hi = alg_get_hasse_i(al);
    1648          84 :   hf = alg_get_hasse_f(al);
    1649          84 :   Lpr = gel(hf,1);
    1650          84 :   hf = gel(hf,2);
    1651          84 :   ram = cgetg(r1+lg(Lpr), t_VEC);
    1652          84 :   count = 0;
    1653         280 :   for (i=1; i<=r1; i++)
    1654         196 :     if (hi[i]) {
    1655          91 :       count++;
    1656          91 :       gel(ram,count) = stoi(i);
    1657             :     }
    1658         259 :   for (i=1; i<lg(Lpr); i++)
    1659         175 :     if (hf[i]) {
    1660          77 :       count++;
    1661          77 :       gel(ram,count) = gel(Lpr,i);
    1662             :     }
    1663          84 :   setlg(ram, count+1);
    1664          84 :   return gerepilecopy(av, ram);
    1665             : }
    1666             : 
    1667             : /** OPERATIONS ON ELEMENTS operations.c **/
    1668             : 
    1669             : static long
    1670     1041812 : alg_model0(GEN al, GEN x)
    1671             : {
    1672     1041812 :   long t, N = alg_get_absdim(al), lx = lg(x), d, n, D, i;
    1673     1041812 :   if (typ(x) == t_MAT) return al_MATRIX;
    1674      995857 :   if (typ(x) != t_COL) return al_INVALID;
    1675      995794 :   if (N == 1) {
    1676        2667 :     if (lx != 2) return al_INVALID;
    1677        2646 :     switch(typ(gel(x,1)))
    1678             :     {
    1679        1652 :       case t_INT: case t_FRAC: return al_TRIVIAL; /* cannot distinguish basis and alg from size */
    1680         994 :       case t_POL: case t_POLMOD: return al_ALGEBRAIC;
    1681           0 :       default: return al_INVALID;
    1682             :     }
    1683             :   }
    1684             : 
    1685      993127 :   switch(alg_type(al)) {
    1686             :     case al_TABLE:
    1687      549194 :       if (lx != N+1) return al_INVALID;
    1688      549173 :       return al_BASIS;
    1689             :     case al_CYCLIC:
    1690      357889 :       d = alg_get_degree(al);
    1691      357889 :       if (lx == N+1) return al_BASIS;
    1692      100002 :       if (lx == d+1) return al_ALGEBRAIC;
    1693          14 :       return al_INVALID;
    1694             :     case al_CSA:
    1695       86044 :       D = alg_get_dim(al);
    1696       86044 :       n = nf_get_degree(alg_get_center(al));
    1697       86044 :       if (n == 1) {
    1698        1302 :         if (lx != D+1) return al_INVALID;
    1699        3871 :         for (i=1; i<=D; i++) {
    1700        3227 :           t = typ(gel(x,i));
    1701        3227 :           if (t == t_POL || t == t_POLMOD)  return al_ALGEBRAIC;
    1702             :             /* TODO t_COL for coefficients in basis form ? */
    1703             :         }
    1704         644 :         return al_BASIS;
    1705             :       }
    1706             :       else {
    1707       84742 :         if (lx == N+1) return al_BASIS;
    1708       23135 :         if (lx == D+1) return al_ALGEBRAIC;
    1709           0 :         return al_INVALID;
    1710             :       }
    1711             :   }
    1712             :   return al_INVALID; /* LCOV_EXCL_LINE */
    1713             : }
    1714             : 
    1715             : static void
    1716     1041686 : checkalgx(GEN x, long model)
    1717             : {
    1718             :   long t, i;
    1719     1041686 :   switch(model) {
    1720             :     case al_BASIS:
    1721     9170628 :       for (i=1; i<lg(x); i++) {
    1722     8301324 :         t = typ(gel(x,i));
    1723     8301324 :         if (t != t_INT && t != t_FRAC)
    1724           7 :           pari_err_TYPE("checkalgx", gel(x,i));
    1725             :       }
    1726      869304 :       return;
    1727             :     case al_TRIVIAL:
    1728             :     case al_ALGEBRAIC:
    1729      442176 :       for (i=1; i<lg(x); i++) {
    1730      315763 :         t = typ(gel(x,i));
    1731      315763 :         if (t != t_INT && t != t_FRAC && t != t_POL && t != t_POLMOD)
    1732             :           /* TODO t_COL ? */
    1733           7 :           pari_err_TYPE("checkalgx", gel(x,i));
    1734             :       }
    1735      126413 :       return;
    1736             :   }
    1737             : }
    1738             : 
    1739             : long
    1740     1041812 : alg_model(GEN al, GEN x)
    1741             : {
    1742     1041812 :   long res = alg_model0(al, x);
    1743     1041812 :   if (res == al_INVALID) pari_err_TYPE("alg_model", x);
    1744     1041686 :   checkalgx(x, res); return res;
    1745             : }
    1746             : 
    1747             : static GEN
    1748         518 : alC_add_i(GEN al, GEN x, GEN y, long lx)
    1749             : {
    1750         518 :   GEN A = cgetg(lx, t_COL);
    1751             :   long i;
    1752         518 :   for (i=1; i<lx; i++) gel(A,i) = algadd(al, gel(x,i), gel(y,i));
    1753         518 :   return A;
    1754             : }
    1755             : static GEN
    1756         280 : alM_add(GEN al, GEN x, GEN y)
    1757             : {
    1758         280 :   long lx = lg(x), l, j;
    1759             :   GEN z;
    1760         280 :   if (lg(y) != lx) pari_err_DIM("alM_add (rows)");
    1761         273 :   if (lx == 1) return cgetg(1, t_MAT);
    1762         266 :   z = cgetg(lx, t_MAT); l = lgcols(x);
    1763         266 :   if (lgcols(y) != l) pari_err_DIM("alM_add (columns)");
    1764         259 :   for (j = 1; j < lx; j++) gel(z,j) = alC_add_i(al, gel(x,j), gel(y,j), l);
    1765         259 :   return z;
    1766             : }
    1767             : GEN
    1768       36974 : algadd(GEN al, GEN x, GEN y)
    1769             : {
    1770       36974 :   pari_sp av = avma;
    1771             :   long tx, ty;
    1772             :   GEN p;
    1773       36974 :   checkalg(al);
    1774       36974 :   tx = alg_model(al,x);
    1775       36967 :   ty = alg_model(al,y);
    1776       36967 :   p = alg_get_char(al);
    1777       36967 :   if (signe(p)) return FpC_add(x,y,p);
    1778       36834 :   if (tx==ty) {
    1779       36022 :     if (tx!=al_MATRIX) return gadd(x,y);
    1780         280 :     return gerepilecopy(av, alM_add(al,x,y));
    1781             :   }
    1782         812 :   if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    1783         812 :   if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
    1784         812 :   return gerepileupto(av, gadd(x,y));
    1785             : }
    1786             : 
    1787             : GEN
    1788         147 : algneg(GEN al, GEN x) { checkalg(al); (void)alg_model(al,x); return gneg(x); }
    1789             : 
    1790             : static GEN
    1791         210 : alC_sub_i(GEN al, GEN x, GEN y, long lx)
    1792             : {
    1793             :   long i;
    1794         210 :   GEN A = cgetg(lx, t_COL);
    1795         210 :   for (i=1; i<lx; i++) gel(A,i) = algsub(al, gel(x,i), gel(y,i));
    1796         210 :   return A;
    1797             : }
    1798             : static GEN
    1799         126 : alM_sub(GEN al, GEN x, GEN y)
    1800             : {
    1801         126 :   long lx = lg(x), l, j;
    1802             :   GEN z;
    1803         126 :   if (lg(y) != lx) pari_err_DIM("alM_sub (rows)");
    1804         119 :   if (lx == 1) return cgetg(1, t_MAT);
    1805         112 :   z = cgetg(lx, t_MAT); l = lgcols(x);
    1806         112 :   if (lgcols(y) != l) pari_err_DIM("alM_sub (columns)");
    1807         105 :   for (j = 1; j < lx; j++) gel(z,j) = alC_sub_i(al, gel(x,j), gel(y,j), l);
    1808         105 :   return z;
    1809             : }
    1810             : GEN
    1811         966 : algsub(GEN al, GEN x, GEN y)
    1812             : {
    1813             :   long tx, ty;
    1814         966 :   pari_sp av = avma;
    1815             :   GEN p;
    1816         966 :   checkalg(al);
    1817         966 :   tx = alg_model(al,x);
    1818         959 :   ty = alg_model(al,y);
    1819         959 :   p = alg_get_char(al);
    1820         959 :   if (signe(p)) return FpC_sub(x,y,p);
    1821         868 :   if (tx==ty) {
    1822         546 :     if (tx != al_MATRIX) return gsub(x,y);
    1823         126 :     return gerepilecopy(av, alM_sub(al,x,y));
    1824             :   }
    1825         322 :   if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    1826         322 :   if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
    1827         322 :   return gerepileupto(av, gsub(x,y));
    1828             : }
    1829             : 
    1830             : static GEN
    1831        1659 : algalgmul_cyc(GEN al, GEN x, GEN y)
    1832             : {
    1833        1659 :   pari_sp av = avma;
    1834        1659 :   long n = alg_get_degree(al), i, k;
    1835             :   GEN xalg, yalg, res, rnf, auts, sum, b, prod, autx;
    1836        1659 :   rnf = alg_get_splittingfield(al);
    1837        1659 :   auts = alg_get_auts(al);
    1838        1659 :   b = alg_get_b(al);
    1839             : 
    1840        1659 :   xalg = cgetg(n+1, t_COL);
    1841        4935 :   for (i=0; i<n; i++)
    1842        3276 :     gel(xalg,i+1) = lift_shallow(rnfbasistoalg(rnf,gel(x,i+1)));
    1843             : 
    1844        1659 :   yalg = cgetg(n+1, t_COL);
    1845        1659 :   for (i=0; i<n; i++) gel(yalg,i+1) = rnfbasistoalg(rnf,gel(y,i+1));
    1846             : 
    1847        1659 :   res = cgetg(n+1,t_COL);
    1848        4935 :   for (k=0; k<n; k++) {
    1849        3276 :     gel(res,k+1) = gmul(gel(xalg,k+1),gel(yalg,1));
    1850        5166 :     for (i=1; i<=k; i++) {
    1851        1890 :       autx = poleval(gel(xalg,k-i+1),gel(auts,i));
    1852        1890 :       prod = gmul(autx,gel(yalg,i+1));
    1853        1890 :       gel(res,k+1) = gadd(gel(res,k+1), prod);
    1854             :     }
    1855             : 
    1856        3276 :     sum = gen_0;
    1857        5166 :     for (; i<n; i++) {
    1858        1890 :       autx = poleval(gel(xalg,k+n-i+1),gel(auts,i));
    1859        1890 :       prod = gmul(autx,gel(yalg,i+1));
    1860        1890 :       sum = gadd(sum,prod);
    1861             :     }
    1862        3276 :     sum = gmul(b,sum);
    1863             : 
    1864        3276 :     gel(res,k+1) = gadd(gel(res,k+1),sum);
    1865             :   }
    1866             : 
    1867        1659 :   return gerepilecopy(av, res);
    1868             : }
    1869             : 
    1870             : static GEN
    1871      203763 : _tablemul(GEN mt, GEN x, GEN y)
    1872             : {
    1873      203763 :   pari_sp av = avma;
    1874      203763 :   long D = lg(mt)-1, i;
    1875      203763 :   GEN res = NULL;
    1876     1906905 :   for (i=1; i<=D; i++) {
    1877     1703142 :     GEN c = gel(x,i);
    1878     1703142 :     if (!gequal0(c)) {
    1879      988876 :       GEN My = RgM_RgC_mul(gel(mt,i),y);
    1880      988876 :       GEN t = RgC_Rg_mul(My,c);
    1881      988876 :       res = res? RgC_add(res,t): t;
    1882             :     }
    1883             :   }
    1884      203763 :   if (!res) { set_avma(av); return zerocol(D); }
    1885      202860 :   return gerepileupto(av, res);
    1886             : }
    1887             : 
    1888             : static GEN
    1889      189563 : _tablemul_Fp(GEN mt, GEN x, GEN y, GEN p)
    1890             : {
    1891      189563 :   pari_sp av = avma;
    1892      189563 :   long D = lg(mt)-1, i;
    1893      189563 :   GEN res = NULL;
    1894     2234831 :   for (i=1; i<=D; i++) {
    1895     2045268 :     GEN c = gel(x,i);
    1896     2045268 :     if (signe(c)) {
    1897      324936 :       GEN My = FpM_FpC_mul(gel(mt,i),y,p);
    1898      324936 :       GEN t = FpC_Fp_mul(My,c,p);
    1899      324936 :       res = res? FpC_add(res,t,p): t;
    1900             :     }
    1901             :   }
    1902      189563 :   if (!res) { set_avma(av); return zerocol(D); }
    1903      189024 :   return gerepileupto(av, res);
    1904             : }
    1905             : 
    1906             : /* x*ej */
    1907             : static GEN
    1908       99512 : _tablemul_ej(GEN mt, GEN x, long j)
    1909             : {
    1910       99512 :   pari_sp av = avma;
    1911       99512 :   long D = lg(mt)-1, i;
    1912       99512 :   GEN res = NULL;
    1913     1561861 :   for (i=1; i<=D; i++) {
    1914     1462349 :     GEN c = gel(x,i);
    1915     1462349 :     if (!gequal0(c)) {
    1916      114023 :       GEN My = gel(gel(mt,i),j);
    1917      114023 :       GEN t = RgC_Rg_mul(My,c);
    1918      114023 :       res = res? RgC_add(res,t): t;
    1919             :     }
    1920             :   }
    1921       99512 :   if (!res) { set_avma(av); return zerocol(D); }
    1922       99372 :   return gerepileupto(av, res);
    1923             : }
    1924             : static GEN
    1925      242039 : _tablemul_ej_Fp(GEN mt, GEN x, long j, GEN p)
    1926             : {
    1927      242039 :   pari_sp av = avma;
    1928      242039 :   long D = lg(mt)-1, i;
    1929      242039 :   GEN res = NULL;
    1930     4364787 :   for (i=1; i<=D; i++) {
    1931     4122748 :     GEN c = gel(x,i);
    1932     4122748 :     if (!gequal0(c)) {
    1933      289954 :       GEN My = gel(gel(mt,i),j);
    1934      289954 :       GEN t = FpC_Fp_mul(My,c,p);
    1935      289954 :       res = res? FpC_add(res,t,p): t;
    1936             :     }
    1937             :   }
    1938      242039 :   if (!res) { set_avma(av); return zerocol(D); }
    1939      241927 :   return gerepileupto(av, res);
    1940             : }
    1941             : 
    1942             : static GEN
    1943      244531 : _tablemul_ej_Fl(GEN mt, GEN x, long j, ulong p)
    1944             : {
    1945      244531 :   pari_sp av = avma;
    1946      244531 :   long D = lg(mt)-1, i;
    1947      244531 :   GEN res = NULL;
    1948     3946488 :   for (i=1; i<=D; i++) {
    1949     3701957 :     ulong c = x[i];
    1950     3701957 :     if (c) {
    1951      383908 :       GEN My = gel(gel(mt,i),j);
    1952      383908 :       GEN t = Flv_Fl_mul(My,c, p);
    1953      383908 :       res = res? Flv_add(res,t, p): t;
    1954             :     }
    1955             :   }
    1956      244531 :   if (!res) { set_avma(av); return zero_Flv(D); }
    1957      244531 :   return gerepileupto(av, res);
    1958             : }
    1959             : 
    1960             : static GEN
    1961         686 : algalgmul_csa(GEN al, GEN x, GEN y)
    1962             : {
    1963         686 :   GEN z, nf = alg_get_center(al);
    1964             :   long i;
    1965         686 :   z = _tablemul(alg_get_relmultable(al), x, y);
    1966        2485 :   for (i=1; i<lg(z); i++)
    1967        1799 :     gel(z,i) = basistoalg(nf,gel(z,i));
    1968         686 :   return z;
    1969             : }
    1970             : 
    1971             : /* assumes x and y in algebraic form */
    1972             : static GEN
    1973        2345 : algalgmul(GEN al, GEN x, GEN y)
    1974             : {
    1975        2345 :   switch(alg_type(al))
    1976             :   {
    1977        1659 :     case al_CYCLIC: return algalgmul_cyc(al, x, y);
    1978         686 :     case al_CSA: return algalgmul_csa(al, x, y);
    1979             :   }
    1980             :   return NULL; /*LCOV_EXCL_LINE*/
    1981             : }
    1982             : 
    1983             : static GEN
    1984      392640 : algbasismul(GEN al, GEN x, GEN y)
    1985             : {
    1986      392640 :   GEN mt = alg_get_multable(al), p = alg_get_char(al);
    1987      392640 :   if (signe(p)) return _tablemul_Fp(mt, x, y, p);
    1988      203077 :   return _tablemul(mt, x, y);
    1989             : }
    1990             : 
    1991             : /* x[i,]*y. Assume lg(x) > 1 and 0 < i < lgcols(x) */
    1992             : static GEN
    1993       85001 : alMrow_alC_mul_i(GEN al, GEN x, GEN y, long i, long lx)
    1994             : {
    1995       85001 :   pari_sp av = avma;
    1996       85001 :   GEN c = algmul(al,gcoeff(x,i,1),gel(y,1)), ZERO;
    1997             :   long k;
    1998       85001 :   ZERO = zerocol(alg_get_absdim(al));
    1999      170002 :   for (k = 2; k < lx; k++)
    2000             :   {
    2001       85001 :     GEN t = algmul(al, gcoeff(x,i,k), gel(y,k));
    2002       85001 :     if (!gequal(t,ZERO)) c = algadd(al, c, t);
    2003             :   }
    2004       85001 :   return gerepilecopy(av, c);
    2005             : }
    2006             : /* return x * y, 1 < lx = lg(x), l = lgcols(x) */
    2007             : static GEN
    2008       42518 : alM_alC_mul_i(GEN al, GEN x, GEN y, long lx, long l)
    2009             : {
    2010       42518 :   GEN z = cgetg(l,t_COL);
    2011             :   long i;
    2012       42518 :   for (i=1; i<l; i++) gel(z,i) = alMrow_alC_mul_i(al,x,y,i,lx);
    2013       42518 :   return z;
    2014             : }
    2015             : static GEN
    2016       21336 : alM_mul(GEN al, GEN x, GEN y)
    2017             : {
    2018       21336 :   long j, l, lx=lg(x), ly=lg(y);
    2019             :   GEN z;
    2020       21336 :   if (ly==1) return cgetg(1,t_MAT);
    2021       21287 :   if (lx != lgcols(y)) pari_err_DIM("alM_mul");
    2022       21266 :   if (lx==1) return zeromat(0, ly-1);
    2023       21259 :   l = lgcols(x); z = cgetg(ly,t_MAT);
    2024       21259 :   for (j=1; j<ly; j++) gel(z,j) = alM_alC_mul_i(al,x,gel(y,j),lx,l);
    2025       21259 :   return z;
    2026             : }
    2027             : 
    2028             : GEN
    2029      364885 : algmul(GEN al, GEN x, GEN y)
    2030             : {
    2031      364885 :   pari_sp av = avma;
    2032             :   long tx, ty;
    2033      364885 :   checkalg(al);
    2034      364885 :   tx = alg_model(al,x);
    2035      364871 :   ty = alg_model(al,y);
    2036      364871 :   if (tx==al_MATRIX) {
    2037       20832 :     if (ty==al_MATRIX) return alM_mul(al,x,y);
    2038           7 :     pari_err_TYPE("algmul", y);
    2039             :   }
    2040      344039 :   if (signe(alg_get_char(al))) return algbasismul(al,x,y);
    2041      203504 :   if (tx==al_TRIVIAL) retmkcol(gmul(gel(x,1),gel(y,1)));
    2042      203399 :   if (tx==al_ALGEBRAIC && ty==al_ALGEBRAIC) return algalgmul(al,x,y);
    2043      201873 :   if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2044      201873 :   if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
    2045      201873 :   return gerepileupto(av,algbasismul(al,x,y));
    2046             : }
    2047             : 
    2048             : GEN
    2049       48804 : algsqr(GEN al, GEN x)
    2050             : {
    2051       48804 :   pari_sp av = avma;
    2052             :   long tx;
    2053       48804 :   checkalg(al);
    2054       48769 :   tx = alg_model(al,x);
    2055       48713 :   if (tx==al_MATRIX) return gerepilecopy(av,alM_mul(al,x,x));
    2056       48202 :   if (signe(alg_get_char(al))) return algbasismul(al,x,x);
    2057        2205 :   if (tx==al_TRIVIAL) retmkcol(gsqr(gel(x,1)));
    2058        2023 :   if (tx==al_ALGEBRAIC) return algalgmul(al,x,x);
    2059        1204 :   return gerepileupto(av,algbasismul(al,x,x));
    2060             : }
    2061             : 
    2062             : static GEN
    2063        8099 : algmtK2Z_cyc(GEN al, GEN m)
    2064             : {
    2065        8099 :   pari_sp av = avma;
    2066        8099 :   GEN nf = alg_get_abssplitting(al), res, mt, rnf = alg_get_splittingfield(al), c, dc;
    2067        8099 :   long n = alg_get_degree(al), N = nf_get_degree(nf), Nn, i, j, i1, j1;
    2068        8099 :   Nn = N*n;
    2069        8099 :   res = zeromatcopy(Nn,Nn);
    2070       38150 :   for (i=0; i<n; i++)
    2071      186242 :   for (j=0; j<n; j++) {
    2072      156191 :     c = gcoeff(m,i+1,j+1);
    2073      156191 :     if (!gequal0(c)) {
    2074       30051 :       c = rnfeltreltoabs(rnf,c);
    2075       30051 :       c = algtobasis(nf,c);
    2076       30051 :       c = Q_remove_denom(c,&dc);
    2077       30051 :       mt = zk_multable(nf,c);
    2078       30051 :       if (dc) mt = ZM_Z_div(mt,dc);
    2079      270634 :       for (i1=1; i1<=N; i1++)
    2080     2529646 :       for (j1=1; j1<=N; j1++)
    2081     2289063 :         gcoeff(res,i*N+i1,j*N+j1) = gcoeff(mt,i1,j1);
    2082             :     }
    2083             :   }
    2084        8099 :   return gerepilecopy(av,res);
    2085             : }
    2086             : 
    2087             : static GEN
    2088         861 : algmtK2Z_csa(GEN al, GEN m)
    2089             : {
    2090         861 :   pari_sp av = avma;
    2091         861 :   GEN nf = alg_get_center(al), res, mt, c, dc;
    2092         861 :   long d2 = alg_get_dim(al), n = nf_get_degree(nf), D, i, j, i1, j1;
    2093         861 :   D = d2*n;
    2094         861 :   res = zeromatcopy(D,D);
    2095        5082 :   for (i=0; i<d2; i++)
    2096       29442 :   for (j=0; j<d2; j++) {
    2097       25221 :     c = gcoeff(m,i+1,j+1);
    2098       25221 :     if (!gequal0(c)) {
    2099        3360 :       c = algtobasis(nf,c);
    2100        3360 :       c = Q_remove_denom(c,&dc);
    2101        3360 :       mt = zk_multable(nf,c);
    2102        3360 :       if (dc) mt = ZM_Z_div(mt,dc);
    2103       11550 :       for (i1=1; i1<=n; i1++)
    2104       29736 :       for (j1=1; j1<=n; j1++)
    2105       21546 :         gcoeff(res,i*n+i1,j*n+j1) = gcoeff(mt,i1,j1);
    2106             :     }
    2107             :   }
    2108         861 :   return gerepilecopy(av,res);
    2109             : }
    2110             : 
    2111             : /* assumes al is a CSA or CYCLIC */
    2112             : static GEN
    2113        8960 : algmtK2Z(GEN al, GEN m)
    2114             : {
    2115        8960 :   switch(alg_type(al))
    2116             :   {
    2117        8099 :     case al_CYCLIC: return algmtK2Z_cyc(al, m);
    2118         861 :     case al_CSA: return algmtK2Z_csa(al, m);
    2119             :   }
    2120             :   return NULL; /*LCOV_EXCL_LINE*/
    2121             : }
    2122             : 
    2123             : /* left multiplication table, as a vector space of dimension n over the splitting field (by right multiplication) */
    2124             : static GEN
    2125       10717 : algalgmultable_cyc(GEN al, GEN x)
    2126             : {
    2127       10717 :   pari_sp av = avma;
    2128       10717 :   long n = alg_get_degree(al), i, j;
    2129             :   GEN res, rnf, auts, b, pol;
    2130       10717 :   rnf = alg_get_splittingfield(al);
    2131       10717 :   auts = alg_get_auts(al);
    2132       10717 :   b = alg_get_b(al);
    2133       10717 :   pol = rnf_get_pol(rnf);
    2134             : 
    2135       10717 :   res = zeromatcopy(n,n);
    2136       46074 :   for (i=0; i<n; i++)
    2137       35357 :     gcoeff(res,i+1,1) = lift_shallow(rnfbasistoalg(rnf,gel(x,i+1)));
    2138             : 
    2139       46074 :   for (i=0; i<n; i++) {
    2140      101423 :     for (j=1; j<=i; j++)
    2141       66066 :       gcoeff(res,i+1,j+1) = gmodulo(poleval(gcoeff(res,i-j+1,1),gel(auts,j)),pol);
    2142      101423 :     for (; j<n; j++)
    2143       66066 :       gcoeff(res,i+1,j+1) = gmodulo(gmul(b,poleval(gcoeff(res,n+i-j+1,1),gel(auts,j))), pol);
    2144             :   }
    2145             : 
    2146       46074 :   for (i=0; i<n; i++)
    2147       35357 :     gcoeff(res,i+1,1) = gmodulo(gcoeff(res,i+1,1),pol);
    2148             : 
    2149       10717 :   return gerepilecopy(av, res);
    2150             : }
    2151             : 
    2152             : static GEN
    2153        1309 : elementmultable(GEN mt, GEN x)
    2154             : {
    2155        1309 :   pari_sp av = avma;
    2156        1309 :   long D = lg(mt)-1, i;
    2157        1309 :   GEN z = NULL;
    2158        7028 :   for (i=1; i<=D; i++)
    2159             :   {
    2160        5719 :     GEN c = gel(x,i);
    2161        5719 :     if (!gequal0(c))
    2162             :     {
    2163        2079 :       GEN M = RgM_Rg_mul(gel(mt,i),c);
    2164        2079 :       z = z? RgM_add(z, M): M;
    2165             :     }
    2166             :   }
    2167        1309 :   if (!z) { set_avma(av); return zeromatcopy(D,D); }
    2168        1309 :   return gerepileupto(av, z);
    2169             : }
    2170             : /* mt a t_VEC of Flm modulo m */
    2171             : static GEN
    2172       24598 : algbasismultable_Flm(GEN mt, GEN x, ulong m)
    2173             : {
    2174       24598 :   pari_sp av = avma;
    2175       24598 :   long D = lg(gel(mt,1))-1, i;
    2176       24598 :   GEN z = NULL;
    2177      269129 :   for (i=1; i<=D; i++)
    2178             :   {
    2179      244531 :     ulong c = x[i];
    2180      244531 :     if (c)
    2181             :     {
    2182       33243 :       GEN M = Flm_Fl_mul(gel(mt,i),c, m);
    2183       33243 :       z = z? Flm_add(z, M, m): M;
    2184             :     }
    2185             :   }
    2186       24598 :   if (!z) { set_avma(av); return zero_Flm(D,D); }
    2187       24598 :   return gerepileupto(av, z);
    2188             : }
    2189             : static GEN
    2190      215569 : elementabsmultable_Z(GEN mt, GEN x)
    2191             : {
    2192      215569 :   long i, l = lg(x);
    2193      215569 :   GEN z = NULL;
    2194     2223819 :   for (i = 1; i < l; i++)
    2195             :   {
    2196     2008250 :     GEN c = gel(x,i);
    2197     2008250 :     if (signe(c))
    2198             :     {
    2199      744952 :       GEN M = ZM_Z_mul(gel(mt,i),c);
    2200      744952 :       z = z? ZM_add(z, M): M;
    2201             :     }
    2202             :   }
    2203      215569 :   return z;
    2204             : }
    2205             : static GEN
    2206      114268 : elementabsmultable(GEN mt, GEN x)
    2207             : {
    2208      114268 :   GEN d, z = elementabsmultable_Z(mt, Q_remove_denom(x,&d));
    2209      114268 :   return (z && d)? ZM_Z_div(z, d): z;
    2210             : }
    2211             : static GEN
    2212      101301 : elementabsmultable_Fp(GEN mt, GEN x, GEN p)
    2213             : {
    2214      101301 :   GEN z = elementabsmultable_Z(mt, x);
    2215      101301 :   return z? FpM_red(z, p): z;
    2216             : }
    2217             : static GEN
    2218      215569 : algbasismultable(GEN al, GEN x)
    2219             : {
    2220      215569 :   pari_sp av = avma;
    2221      215569 :   GEN z, p = alg_get_char(al), mt = alg_get_multable(al);
    2222      215569 :   z = signe(p)? elementabsmultable_Fp(mt, x, p): elementabsmultable(mt, x);
    2223      215569 :   if (!z)
    2224             :   {
    2225         754 :     long D = lg(mt)-1;
    2226         754 :     set_avma(av); return zeromat(D,D);
    2227             :   }
    2228      214815 :   return gerepileupto(av, z);
    2229             : }
    2230             : 
    2231             : static GEN
    2232        1309 : algalgmultable_csa(GEN al, GEN x)
    2233             : {
    2234        1309 :   GEN nf = alg_get_center(al), m;
    2235             :   long i,j;
    2236        1309 :   m = elementmultable(alg_get_relmultable(al), x);
    2237        7028 :   for (i=1; i<lg(m); i++)
    2238       36638 :     for(j=1; j<lg(m); j++)
    2239       30919 :       gcoeff(m,i,j) = basistoalg(nf,gcoeff(m,i,j));
    2240        1309 :   return m;
    2241             : }
    2242             : 
    2243             : /* assumes x in algebraic form */
    2244             : static GEN
    2245       11732 : algalgmultable(GEN al, GEN x)
    2246             : {
    2247       11732 :   switch(alg_type(al))
    2248             :   {
    2249       10717 :     case al_CYCLIC: return algalgmultable_cyc(al, x);
    2250        1015 :     case al_CSA: return algalgmultable_csa(al, x);
    2251             :   }
    2252             :   return NULL; /*LCOV_EXCL_LINE*/
    2253             : }
    2254             : 
    2255             : /* on the natural basis */
    2256             : /* assumes x in algebraic form */
    2257             : static GEN
    2258        8960 : algZmultable(GEN al, GEN x) {
    2259        8960 :   pari_sp av = avma;
    2260        8960 :   GEN res = NULL, x0;
    2261        8960 :   long tx = alg_model(al,x);
    2262        8960 :   switch(tx) {
    2263             :     case al_TRIVIAL:
    2264           0 :       x0 = gel(x,1);
    2265           0 :       if (typ(x0)==t_POLMOD) x0 = gel(x0,2);
    2266           0 :       if (typ(x0)==t_POL) x0 = constant_coeff(x0);
    2267           0 :       res = mkmatcopy(mkcol(x0));
    2268           0 :       break;
    2269        8960 :     case al_ALGEBRAIC: res = algmtK2Z(al,algalgmultable(al,x)); break;
    2270             :   }
    2271        8960 :   return gerepileupto(av,res);
    2272             : }
    2273             : 
    2274             : /* x integral */
    2275             : static GEN
    2276       36561 : algbasisrightmultable(GEN al, GEN x)
    2277             : {
    2278       36561 :   long N = alg_get_absdim(al), i,j,k;
    2279       36561 :   GEN res = zeromatcopy(N,N), c, mt = alg_get_multable(al), p = alg_get_char(al);
    2280       36561 :   if (gequal0(p)) p = NULL;
    2281      330862 :   for (i=1; i<=N; i++) {
    2282      294301 :     c = gel(x,i);
    2283      294301 :     if (!gequal0(c)) {
    2284      872200 :       for (j=1; j<=N; j++)
    2285     7417690 :       for(k=1; k<=N; k++) {
    2286     6639682 :         if (p) gcoeff(res,k,j) = Fp_add(gcoeff(res,k,j), Fp_mul(c, gcoeff(gel(mt,j),k,i), p), p);
    2287     5014814 :         else gcoeff(res,k,j) = addii(gcoeff(res,k,j), mulii(c, gcoeff(gel(mt,j),k,i)));
    2288             :       }
    2289             :     }
    2290             :   }
    2291       36561 :   return res;
    2292             : }
    2293             : 
    2294             : /* basis for matrices : 1, E_{i,j} for (i,j)!=(1,1) */
    2295             : /* index : ijk = ((i-1)*N+j-1)*n + k */
    2296             : /* square matrices only, coefficients in basis form, shallow function */
    2297             : static GEN
    2298       20097 : algmat2basis(GEN al, GEN M)
    2299             : {
    2300       20097 :   long n = alg_get_absdim(al), N = lg(M)-1, i, j, k, ij, ijk;
    2301             :   GEN res, x;
    2302       20097 :   res = zerocol(N*N*n);
    2303       60291 :   for (i=1; i<=N; i++) {
    2304      120582 :     for (j=1, ij=(i-1)*N+1; j<=N; j++, ij++) {
    2305       80388 :       x = gcoeff(M,i,j);
    2306      660772 :       for (k=1, ijk=(ij-1)*n+1; k<=n; k++, ijk++) {
    2307      580384 :         gel(res, ijk) = gel(x, k);
    2308      580384 :         if (i>1 && i==j) gel(res, ijk) = gsub(gel(res,ijk), gel(res,k));
    2309             :       }
    2310             :     }
    2311             :   }
    2312             : 
    2313       20097 :   return res;
    2314             : }
    2315             : 
    2316             : static GEN
    2317         294 : algbasis2mat(GEN al, GEN M, long N)
    2318             : {
    2319         294 :   long n = alg_get_absdim(al), i, j, k, ij, ijk;
    2320             :   GEN res, x;
    2321         294 :   res = zeromatcopy(N,N);
    2322         882 :   for (i=1; i<=N; i++)
    2323        1764 :   for (j=1; j<=N; j++)
    2324        1176 :     gcoeff(res,i,j) = zerocol(n);
    2325             : 
    2326         882 :   for (i=1; i<=N; i++) {
    2327        1764 :     for (j=1, ij=(i-1)*N+1; j<=N; j++, ij++) {
    2328        1176 :       x = gcoeff(res,i,j);
    2329        9240 :       for (k=1, ijk=(ij-1)*n+1; k<=n; k++, ijk++) {
    2330        8064 :         gel(x,k) = gel(M,ijk);
    2331        8064 :         if (i>1 && i==j) gel(x,k) = gadd(gel(x,k), gel(M,k));
    2332             :       }
    2333             :     }
    2334             :   }
    2335             : 
    2336         294 :   return res;
    2337             : }
    2338             : 
    2339             : static GEN
    2340       20020 : algmatbasis_ei(GEN al, long ijk, long N)
    2341             : {
    2342       20020 :   long n = alg_get_absdim(al), i, j, k, ij;
    2343             :   GEN res;
    2344             : 
    2345       20020 :   res = zeromatcopy(N,N);
    2346       60060 :   for (i=1; i<=N; i++)
    2347      120120 :   for (j=1; j<=N; j++)
    2348       80080 :     gcoeff(res,i,j) = zerocol(n);
    2349             : 
    2350       20020 :   k = ijk%n;
    2351       20020 :   if (k==0) k=n;
    2352       20020 :   ij = (ijk-k)/n+1;
    2353             : 
    2354       20020 :   if (ij==1) {
    2355       15015 :     for (i=1; i<=N; i++)
    2356       10010 :       gcoeff(res,i,i) = col_ei(n,k);
    2357        5005 :     return res;
    2358             :   }
    2359             : 
    2360       15015 :   j = ij%N;
    2361       15015 :   if (j==0) j=N;
    2362       15015 :   i = (ij-j)/N+1;
    2363             : 
    2364       15015 :   gcoeff(res,i,j) = col_ei(n,k);
    2365       15015 :   return res;
    2366             : }
    2367             : 
    2368             : /* FIXME lazy implementation! */
    2369             : static GEN
    2370         777 : algleftmultable_mat(GEN al, GEN M)
    2371             : {
    2372         777 :   long N = lg(M)-1, n = alg_get_absdim(al), D = N*N*n, j;
    2373             :   GEN res, x, Mx;
    2374         777 :   if (N == 0) return cgetg(1, t_MAT);
    2375         770 :   if (N != nbrows(M)) pari_err_DIM("algleftmultable_mat (nonsquare)");
    2376         749 :   res = cgetg(D+1, t_MAT);
    2377       20769 :   for (j=1; j<=D; j++) {
    2378       20020 :     x = algmatbasis_ei(al, j, N);
    2379       20020 :     Mx = algmul(al, M, x);
    2380       20020 :     gel(res, j) = algmat2basis(al, Mx);
    2381             :   }
    2382         749 :   return res;
    2383             : }
    2384             : 
    2385             : /* left multiplication table on integral basis */
    2386             : static GEN
    2387        6951 : algleftmultable(GEN al, GEN x)
    2388             : {
    2389        6951 :   pari_sp av = avma;
    2390             :   long tx;
    2391             :   GEN res;
    2392             : 
    2393        6951 :   checkalg(al);
    2394        6951 :   tx = alg_model(al,x);
    2395        6944 :   switch(tx) {
    2396          98 :     case al_TRIVIAL : res = mkmatcopy(mkcol(gel(x,1))); break;
    2397         196 :     case al_ALGEBRAIC : x = algalgtobasis(al,x);
    2398        6328 :     case al_BASIS : res = algbasismultable(al,x); break;
    2399         518 :     case al_MATRIX : res = algleftmultable_mat(al,x); break;
    2400             :     default : return NULL; /* LCOV_EXCL_LINE */
    2401             :   }
    2402        6937 :   return gerepileupto(av,res);
    2403             : }
    2404             : 
    2405             : static GEN
    2406        4102 : algbasissplittingmatrix_csa(GEN al, GEN x)
    2407             : {
    2408        4102 :   long d = alg_get_degree(al), i, j;
    2409        4102 :   GEN rnf = alg_get_splittingfield(al), splba = alg_get_splittingbasis(al), splbainv = alg_get_splittingbasisinv(al), M;
    2410        4102 :   M = algbasismultable(al,x);
    2411        4102 :   M = RgM_mul(M, splba); /* TODO best order ? big matrix /Q vs small matrix /nf */
    2412        4102 :   M = RgM_mul(splbainv, M);
    2413       12131 :   for (i=1; i<=d; i++)
    2414       23912 :   for (j=1; j<=d; j++)
    2415       15883 :     gcoeff(M,i,j) = rnfeltabstorel(rnf, gcoeff(M,i,j));
    2416        4102 :   return M;
    2417             : }
    2418             : 
    2419             : GEN
    2420        7399 : algtomatrix(GEN al, GEN x, long abs)
    2421             : {
    2422        7399 :   pari_sp av = avma;
    2423        7399 :   GEN res = NULL;
    2424             :   long ta, tx, i, j;
    2425        7399 :   checkalg(al);
    2426        7399 :   ta = alg_type(al);
    2427        7399 :   if (abs || ta==al_TABLE) return algleftmultable(al,x);
    2428        6622 :   tx = alg_model(al,x);
    2429        6622 :   if (tx==al_MATRIX) {
    2430         469 :     if (lg(x) == 1) return cgetg(1, t_MAT);
    2431         441 :     res = zeromatcopy(nbrows(x),lg(x)-1);
    2432        1323 :     for (j=1; j<lg(x); j++)
    2433        2618 :     for (i=1; i<lgcols(x); i++)
    2434        1736 :       gcoeff(res,i,j) = algtomatrix(al,gcoeff(x,i,j),0);
    2435         441 :     res = shallowmatconcat(res);
    2436             :   }
    2437        6153 :   else switch(alg_type(al))
    2438             :   {
    2439             :     case al_CYCLIC:
    2440        2051 :       if (tx==al_BASIS) x = algbasistoalg(al,x);
    2441        2051 :       res = algalgmultable(al,x);
    2442        2051 :       break;
    2443             :     case al_CSA:
    2444        4102 :       if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2445        4102 :       res = algbasissplittingmatrix_csa(al,x);
    2446        4102 :       break;
    2447             :     default:
    2448           0 :       pari_err_DOMAIN("algtomatrix", "alg_type(al)", "=", stoi(alg_type(al)), stoi(alg_type(al)));
    2449             :   }
    2450        6594 :   return gerepilecopy(av,res);
    2451             : }
    2452             : 
    2453             : /*  x^(-1)*y, NULL if no solution */
    2454             : static GEN
    2455        1715 : algdivl_i(GEN al, GEN x, GEN y, long tx, long ty) {
    2456        1715 :   pari_sp av = avma;
    2457        1715 :   GEN res, p = alg_get_char(al), mtx;
    2458        1715 :   if (tx != ty) {
    2459         343 :     if (tx==al_ALGEBRAIC) { x = algalgtobasis(al,x); tx=al_BASIS; }
    2460         343 :     if (ty==al_ALGEBRAIC) { y = algalgtobasis(al,y); ty=al_BASIS; }
    2461             :   }
    2462        1715 :   if (ty == al_MATRIX)
    2463             :   {
    2464          77 :     if (alg_type(al) != al_TABLE) y = algalgtobasis(al,y);
    2465          77 :     y = algmat2basis(al,y);
    2466             :   }
    2467        1715 :   if (signe(p)) res = FpM_FpC_invimage(algbasismultable(al,x),y,p);
    2468             :   else
    2469             :   {
    2470        1526 :     if (ty==al_ALGEBRAIC)   mtx = algalgmultable(al,x);
    2471         819 :     else                    mtx = algleftmultable(al,x);
    2472        1526 :     res = inverseimage(mtx,y);
    2473             :   }
    2474        1715 :   if (!res || lg(res)==1) return gc_NULL(av);
    2475        1687 :   if (tx == al_MATRIX) {
    2476         294 :     res = algbasis2mat(al, res, lg(x)-1);
    2477         294 :     return gerepilecopy(av,res);
    2478             :   }
    2479        1393 :   return gerepileupto(av,res);
    2480             : }
    2481             : static GEN
    2482         721 : algdivl_i2(GEN al, GEN x, GEN y)
    2483             : {
    2484             :   long tx, ty;
    2485         721 :   checkalg(al);
    2486         721 :   tx = alg_model(al,x);
    2487         714 :   ty = alg_model(al,y);
    2488         714 :   if (tx == al_MATRIX) {
    2489         119 :     if (ty != al_MATRIX) pari_err_TYPE2("\\", x, y);
    2490         112 :     if (lg(y) == 1) return cgetg(1, t_MAT);
    2491         105 :     if (lg(x) == 1) return NULL;
    2492          98 :     if (lgcols(x) != lgcols(y)) pari_err_DIM("algdivl");
    2493          91 :     if (lg(x) != lgcols(x) || lg(y) != lgcols(y))
    2494          14 :       pari_err_DIM("algdivl (nonsquare)");
    2495             :   }
    2496         672 :   return algdivl_i(al,x,y,tx,ty);
    2497             : }
    2498             : 
    2499         672 : GEN algdivl(GEN al, GEN x, GEN y)
    2500             : {
    2501             :   GEN z;
    2502         672 :   z = algdivl_i2(al,x,y);
    2503         637 :   if (!z) pari_err_INV("algdivl", x);
    2504         623 :   return z;
    2505             : }
    2506             : 
    2507             : int
    2508          49 : algisdivl(GEN al, GEN x, GEN y, GEN* ptz)
    2509             : {
    2510          49 :   pari_sp av = avma;
    2511          49 :   GEN z = algdivl_i2(al,x,y);
    2512          49 :   if (!z) return gc_bool(av,0);
    2513          42 :   if (ptz != NULL) *ptz = z;
    2514          42 :   return 1;
    2515             : }
    2516             : 
    2517             : static GEN
    2518        1148 : alginv_i(GEN al, GEN x)
    2519             : {
    2520        1148 :   pari_sp av = avma;
    2521        1148 :   GEN res = NULL, p = alg_get_char(al);
    2522        1148 :   long tx = alg_model(al,x), n;
    2523        1127 :   switch(tx) {
    2524             :     case al_TRIVIAL :
    2525          63 :       if (signe(p)) { res = mkcol(Fp_inv(gel(x,1),p)); break; }
    2526          49 :       else          { res = mkcol(ginv(gel(x,1))); break; }
    2527             :     case al_ALGEBRAIC :
    2528         455 :       switch(alg_type(al)) {
    2529         350 :         case al_CYCLIC: n = alg_get_degree(al); break;
    2530         105 :         case al_CSA: n = alg_get_dim(al); break;
    2531             :         default: return NULL; /* LCOV_EXCL_LINE */
    2532             :       }
    2533         455 :       res = algdivl_i(al, x, col_ei(n,1), tx, al_ALGEBRAIC); break;
    2534         371 :     case al_BASIS : res = algdivl_i(al, x, col_ei(alg_get_absdim(al),1), tx,
    2535         371 :                                                             al_BASIS); break;
    2536             :     case al_MATRIX :
    2537         238 :       n = lg(x)-1;
    2538         238 :       if (n==0) return cgetg(1, t_MAT);
    2539         224 :       if (n != nbrows(x)) pari_err_DIM("alginv_i (nonsquare)");
    2540         217 :       res = algdivl_i(al, x, col_ei(n*n*alg_get_absdim(al),1), tx, al_BASIS);
    2541             :         /* cheat on type because wrong dimension */
    2542             :   }
    2543        1106 :   if (!res) return gc_NULL(av);
    2544        1092 :   return gerepilecopy(av,res);
    2545             : }
    2546             : GEN
    2547        1078 : alginv(GEN al, GEN x)
    2548             : {
    2549             :   GEN z;
    2550        1078 :   checkalg(al);
    2551        1078 :   z = alginv_i(al,x);
    2552        1050 :   if (!z) pari_err_INV("alginv", x);
    2553        1043 :   return z;
    2554             : }
    2555             : 
    2556             : int
    2557          70 : algisinv(GEN al, GEN x, GEN* ptix)
    2558             : {
    2559          70 :   pari_sp av = avma;
    2560             :   GEN ix;
    2561          70 :   checkalg(al);
    2562          70 :   ix = alginv_i(al,x);
    2563          70 :   if (!ix) return gc_bool(av,0);
    2564          63 :   if (ptix != NULL) *ptix = ix;
    2565          63 :   return 1;
    2566             : }
    2567             : 
    2568             : /*  x*y^(-1)  */
    2569             : GEN
    2570         406 : algdivr(GEN al, GEN x, GEN y) { return algmul(al, x, alginv(al, y)); }
    2571             : 
    2572       24948 : static GEN _mul(void* data, GEN x, GEN y) { return algmul((GEN)data,x,y); }
    2573       47712 : static GEN _sqr(void* data, GEN x) { return algsqr((GEN)data,x); }
    2574             : 
    2575             : static GEN
    2576          21 : algmatid(GEN al, long N)
    2577             : {
    2578          21 :   long n = alg_get_absdim(al), i, j;
    2579             :   GEN res, one, zero;
    2580             : 
    2581          21 :   res = zeromatcopy(N,N);
    2582          21 :   one = col_ei(n,1);
    2583          21 :   zero = zerocol(n);
    2584          49 :   for (i=1; i<=N; i++)
    2585          84 :   for (j=1; j<=N; j++)
    2586          56 :     gcoeff(res,i,j) = i==j ? one : zero;
    2587          21 :   return res;
    2588             : }
    2589             : 
    2590             : GEN
    2591       12082 : algpow(GEN al, GEN x, GEN n)
    2592             : {
    2593       12082 :   pari_sp av = avma;
    2594             :   GEN res;
    2595       12082 :   checkalg(al);
    2596       12082 :   switch(signe(n)) {
    2597             :     case 0:
    2598          28 :       if (alg_model(al,x) == al_MATRIX)
    2599          21 :         res = algmatid(al,lg(x)-1);
    2600             :       else
    2601           7 :         res = col_ei(alg_get_absdim(al),1);
    2602          28 :       return res;
    2603             :     case 1:
    2604       11970 :       res = gen_pow_i(x, n, (void*)al, _sqr, _mul); break;
    2605             :     default: /* -1 */
    2606          84 :       res = gen_pow_i(alginv(al,x), gneg(n), (void*)al, _sqr, _mul);
    2607             :   }
    2608       12047 :   return gerepilecopy(av,res);
    2609             : }
    2610             : 
    2611             : static GEN
    2612         378 : algredcharpoly_i(GEN al, GEN x, long v)
    2613             : {
    2614         378 :   GEN rnf = alg_get_splittingfield(al);
    2615         378 :   GEN cp = charpoly(algtomatrix(al,x,0),v);
    2616         371 :   long i, m = lg(cp);
    2617         371 :   for (i=2; i<m; i++) gel(cp,i) = rnfeltdown(rnf, gel(cp,i));
    2618         371 :   return cp;
    2619             : }
    2620             : 
    2621             : /* assumes al is CSA or CYCLIC */
    2622             : static GEN
    2623         385 : algredcharpoly(GEN al, GEN x, long v)
    2624             : {
    2625         385 :   pari_sp av = avma;
    2626         385 :   long w = gvar(rnf_get_pol(alg_get_center(al)));
    2627         385 :   if (varncmp(v,w)>=0) pari_err_PRIORITY("algredcharpoly",pol_x(v),">=",w);
    2628         378 :   switch(alg_type(al))
    2629             :   {
    2630             :     case al_CYCLIC:
    2631             :     case al_CSA:
    2632         378 :       return gerepileupto(av, algredcharpoly_i(al, x, v));
    2633             :   }
    2634             :   return NULL; /*LCOV_EXCL_LINE*/
    2635             : }
    2636             : 
    2637             : static GEN
    2638       20900 : algbasischarpoly(GEN al, GEN x, long v)
    2639             : {
    2640       20900 :   pari_sp av = avma;
    2641       20900 :   GEN p = alg_get_char(al), mx;
    2642       20900 :   if (alg_model(al,x) == al_MATRIX) mx = algleftmultable_mat(al,x);
    2643       20809 :   else                              mx = algbasismultable(al,x);
    2644       20893 :   if (signe(p)) {
    2645       18989 :     GEN res = FpM_charpoly(mx,p);
    2646       18989 :     setvarn(res,v);
    2647       18989 :     return gerepileupto(av, res);
    2648             :   }
    2649        1904 :   return gerepileupto(av, charpoly(mx,v));
    2650             : }
    2651             : 
    2652             : GEN
    2653       20970 : algcharpoly(GEN al, GEN x, long v, long abs)
    2654             : {
    2655       20970 :   checkalg(al);
    2656       20970 :   if (v<0) v=0;
    2657             : 
    2658             :   /* gneg(x[1]) left on stack */
    2659       20970 :   if (alg_model(al,x) == al_TRIVIAL) {
    2660          56 :     GEN p = alg_get_char(al);
    2661          56 :     if (signe(p)) return deg1pol(gen_1,Fp_neg(gel(x,1),p),v);
    2662          42 :     return deg1pol(gen_1,gneg(gel(x,1)),v);
    2663             :   }
    2664             : 
    2665       20907 :   switch(alg_type(al)) {
    2666             :     case al_CYCLIC: case al_CSA:
    2667         490 :       if (abs)
    2668             :       {
    2669         105 :         if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2670             :       }
    2671         385 :       else return algredcharpoly(al,x,v);
    2672       20522 :     case al_TABLE: return algbasischarpoly(al,x,v);
    2673             :     default : return NULL; /* LCOV_EXCL_LINE */
    2674             :   }
    2675             : }
    2676             : 
    2677             : /* assumes x in basis form */
    2678             : static GEN
    2679      232001 : algabstrace(GEN al, GEN x)
    2680             : {
    2681      232001 :   pari_sp av = avma;
    2682      232001 :   GEN res = NULL, p = alg_get_char(al);
    2683      232001 :   if (signe(p)) return FpV_dotproduct(x, alg_get_tracebasis(al), p);
    2684       42644 :   switch(alg_model(al,x)) {
    2685          84 :     case al_TRIVIAL: return gcopy(gel(x,1)); break;
    2686       42560 :     case al_BASIS: res = RgV_dotproduct(x, alg_get_tracebasis(al)); break;
    2687             :   }
    2688       42560 :   return gerepileupto(av,res);
    2689             : }
    2690             : 
    2691             : static GEN
    2692        1372 : algredtrace(GEN al, GEN x)
    2693             : {
    2694        1372 :   pari_sp av = avma;
    2695        1372 :   GEN res = NULL;
    2696        1372 :   switch(alg_model(al,x)) {
    2697          35 :     case al_TRIVIAL: return gcopy(gel(x,1)); break;
    2698         490 :     case al_BASIS: return algredtrace(al, algbasistoalg(al,x));
    2699             :                    /* TODO precompute too? */
    2700             :     case al_ALGEBRAIC:
    2701         847 :       switch(alg_type(al))
    2702             :       {
    2703             :         case al_CYCLIC:
    2704         553 :           res = rnfelttrace(alg_get_splittingfield(al),gel(x,1));
    2705         553 :           break;
    2706             :         case al_CSA:
    2707         294 :           res = gtrace(algalgmultable_csa(al,x));
    2708         294 :           res = gdiv(res, stoi(alg_get_degree(al)));
    2709         294 :           break;
    2710             :         default: return NULL; /* LCOV_EXCL_LINE */
    2711             :       }
    2712             :   }
    2713         847 :   return gerepileupto(av,res);
    2714             : }
    2715             : 
    2716             : static GEN
    2717         308 : algtrace_mat(GEN al, GEN M, long abs) {
    2718         308 :   pari_sp av = avma;
    2719         308 :   long N = lg(M)-1, i;
    2720         308 :   GEN res, p = alg_get_char(al);
    2721         308 :   if (N == 0) return gen_0;
    2722         294 :   if (N != nbrows(M)) pari_err_DIM("algtrace_mat (nonsquare)");
    2723             : 
    2724         287 :   if (!signe(p)) p = NULL;
    2725         287 :   res = algtrace(al, gcoeff(M,1,1), abs);
    2726         574 :   for (i=2; i<=N; i++) {
    2727         287 :     if (p)  res = Fp_add(res, algtrace(al,gcoeff(M,i,i),abs), p);
    2728         280 :     else    res = gadd(res, algtrace(al,gcoeff(M,i,i),abs));
    2729             :   }
    2730         287 :   if (abs || alg_type(al) == al_TABLE) res = gmulgs(res, N); /* absolute trace */
    2731         287 :   return gerepileupto(av, res);
    2732             : }
    2733             : 
    2734             : GEN
    2735        1519 : algtrace(GEN al, GEN x, long abs)
    2736             : {
    2737        1519 :   checkalg(al);
    2738        1519 :   if (alg_model(al,x) == al_MATRIX) return algtrace_mat(al,x,abs);
    2739        1211 :   switch(alg_type(al)) {
    2740             :     case al_CYCLIC: case al_CSA:
    2741        1078 :       if (!abs) return algredtrace(al,x);
    2742         196 :       if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2743         329 :     case al_TABLE: return algabstrace(al,x);
    2744             :     default : return NULL; /* LCOV_EXCL_LINE */
    2745             :   }
    2746             : }
    2747             : 
    2748             : static GEN
    2749       40257 : ZM_trace(GEN x)
    2750             : {
    2751       40257 :   long i, lx = lg(x);
    2752             :   GEN t;
    2753       40257 :   if (lx < 3) return lx == 1? gen_0: gcopy(gcoeff(x,1,1));
    2754       39459 :   t = gcoeff(x,1,1);
    2755       39459 :   for (i = 2; i < lx; i++) t = addii(t, gcoeff(x,i,i));
    2756       39459 :   return t;
    2757             : }
    2758             : static GEN
    2759      123072 : FpM_trace(GEN x, GEN p)
    2760             : {
    2761      123072 :   long i, lx = lg(x);
    2762             :   GEN t;
    2763      123072 :   if (lx < 3) return lx == 1? gen_0: gcopy(gcoeff(x,1,1));
    2764      115372 :   t = gcoeff(x,1,1);
    2765      115372 :   for (i = 2; i < lx; i++) t = Fp_add(t, gcoeff(x,i,i), p);
    2766      115372 :   return t;
    2767             : }
    2768             : 
    2769             : static GEN
    2770       38122 : algtracebasis(GEN al)
    2771             : {
    2772       38122 :   pari_sp av = avma;
    2773       38122 :   GEN mt = alg_get_multable(al), p = alg_get_char(al);
    2774       38122 :   long i, l = lg(mt);
    2775       38122 :   GEN v = cgetg(l, t_VEC);
    2776       38122 :   if (signe(p)) for (i=1; i < l; i++) gel(v,i) = FpM_trace(gel(mt,i), p);
    2777        5411 :   else          for (i=1; i < l; i++) gel(v,i) = ZM_trace(gel(mt,i));
    2778       38122 :   return gerepileupto(av,v);
    2779             : }
    2780             : 
    2781             : /* Assume: i > 0, expo := p^i <= absdim, x contained in I_{i-1} given by mult
    2782             :  * table modulo modu=p^(i+1). Return Tr(x^(p^i)) mod modu */
    2783             : static ulong
    2784       24598 : algtracei(GEN mt, ulong p, ulong expo, ulong modu)
    2785             : {
    2786       24598 :   pari_sp av = avma;
    2787       24598 :   long j, l = lg(mt);
    2788       24598 :   ulong tr = 0;
    2789       24598 :   mt = Flm_powu(mt,expo,modu);
    2790       24598 :   for (j=1; j<l; j++) tr += ucoeff(mt,j,j);
    2791       24598 :   return gc_ulong(av, (tr/expo) % p);
    2792             : }
    2793             : 
    2794             : GEN
    2795         952 : algnorm(GEN al, GEN x, long abs)
    2796             : {
    2797         952 :   pari_sp av = avma;
    2798             :   long tx;
    2799             :   GEN p, rnf, res, mx;
    2800         952 :   checkalg(al);
    2801         952 :   p = alg_get_char(al);
    2802         952 :   tx = alg_model(al,x);
    2803         952 :   if (signe(p)) {
    2804          21 :     if (tx == al_MATRIX)    mx = algleftmultable_mat(al,x);
    2805          14 :     else                    mx = algbasismultable(al,x);
    2806          21 :     return gerepileupto(av, FpM_det(mx,p));
    2807             :   }
    2808         931 :   if (tx == al_TRIVIAL) return gcopy(gel(x,1));
    2809             : 
    2810         889 :   switch(alg_type(al)) {
    2811             :     case al_CYCLIC: case al_CSA:
    2812         819 :       if (abs)
    2813             :       {
    2814         196 :         if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2815             :       }
    2816             :       else
    2817             :       {
    2818         623 :         rnf = alg_get_splittingfield(al);
    2819         623 :         res = rnfeltdown(rnf, det(algtomatrix(al,x,0)));
    2820         616 :         break;
    2821             :       }
    2822             :     case al_TABLE:
    2823         266 :       if (tx == al_MATRIX)  mx = algleftmultable_mat(al,x);
    2824         105 :       else                  mx = algbasismultable(al,x);
    2825         259 :       res = det(mx);
    2826         259 :       break;
    2827             :     default: return NULL; /* LCOV_EXCL_LINE */
    2828             :   }
    2829         875 :   return gerepileupto(av, res);
    2830             : }
    2831             : 
    2832             : static GEN
    2833       47719 : algalgtonat_cyc(GEN al, GEN x)
    2834             : {
    2835       47719 :   pari_sp av = avma;
    2836       47719 :   GEN nf = alg_get_abssplitting(al), rnf = alg_get_splittingfield(al), res, c;
    2837       47719 :   long n = alg_get_degree(al), N = nf_get_degree(nf), i, i1;
    2838       47719 :   res = zerocol(N*n);
    2839      146363 :   for (i=0; i<n; i++) {
    2840       98644 :     c = gel(x,i+1);
    2841       98644 :     c = rnfeltreltoabs(rnf,c);
    2842       98644 :     if (!gequal0(c)) {
    2843       75341 :       c = algtobasis(nf,c);
    2844       75341 :       for (i1=1; i1<=N; i1++) gel(res,i*N+i1) = gel(c,i1);
    2845             :     }
    2846             :   }
    2847       47719 :   return gerepilecopy(av, res);
    2848             : }
    2849             : 
    2850             : static GEN
    2851       11256 : algalgtonat_csa(GEN al, GEN x)
    2852             : {
    2853       11256 :   pari_sp av = avma;
    2854       11256 :   GEN nf = alg_get_center(al), res, c;
    2855       11256 :   long d2 = alg_get_dim(al), n = nf_get_degree(nf), i, i1;
    2856       11256 :   res = zerocol(d2*n);
    2857       56133 :   for (i=0; i<d2; i++) {
    2858       44877 :     c = gel(x,i+1);
    2859       44877 :     if (!gequal0(c)) {
    2860       31318 :       c = algtobasis(nf,c);
    2861       31318 :       for (i1=1; i1<=n; i1++) gel(res,i*n+i1) = gel(c,i1);
    2862             :     }
    2863             :   }
    2864       11256 :   return gerepilecopy(av, res);
    2865             : }
    2866             : 
    2867             : /* assumes al CSA or CYCLIC */
    2868             : static GEN
    2869       58975 : algalgtonat(GEN al, GEN x)
    2870             : {
    2871       58975 :   switch(alg_type(al))
    2872             :   {
    2873       47719 :     case al_CYCLIC: return algalgtonat_cyc(al, x);
    2874       11256 :     case al_CSA: return algalgtonat_csa(al, x);
    2875             :   }
    2876             :   return NULL; /*LCOV_EXCL_LINE*/
    2877             : }
    2878             : 
    2879             : static GEN
    2880       10381 : algnattoalg_cyc(GEN al, GEN x)
    2881             : {
    2882       10381 :   pari_sp av = avma;
    2883       10381 :   GEN nf = alg_get_abssplitting(al), rnf = alg_get_splittingfield(al), res, c;
    2884       10381 :   long n = alg_get_degree(al), N = nf_get_degree(nf), i, i1;
    2885       10381 :   res = zerocol(n);
    2886       10381 :   c = zerocol(N);
    2887       44926 :   for (i=0; i<n; i++) {
    2888       34545 :     for (i1=1; i1<=N; i1++) gel(c,i1) = gel(x,i*N+i1);
    2889       34545 :     gel(res,i+1) = rnfeltabstorel(rnf,basistoalg(nf,c));
    2890             :   }
    2891       10381 :   return gerepilecopy(av, res);
    2892             : }
    2893             : 
    2894             : static GEN
    2895        1225 : algnattoalg_csa(GEN al, GEN x)
    2896             : {
    2897        1225 :   pari_sp av = avma;
    2898        1225 :   GEN nf = alg_get_center(al), res, c;
    2899        1225 :   long d2 = alg_get_dim(al), n = nf_get_degree(nf), i, i1;
    2900        1225 :   res = zerocol(d2);
    2901        1225 :   c = zerocol(n);
    2902        6608 :   for (i=0; i<d2; i++) {
    2903        5383 :     for (i1=1; i1<=n; i1++) gel(c,i1) = gel(x,i*n+i1);
    2904        5383 :     gel(res,i+1) = basistoalg(nf,c);
    2905             :   }
    2906        1225 :   return gerepilecopy(av, res);
    2907             : }
    2908             : 
    2909             : /* assumes al CSA or CYCLIC */
    2910             : static GEN
    2911       11606 : algnattoalg(GEN al, GEN x)
    2912             : {
    2913       11606 :   switch(alg_type(al))
    2914             :   {
    2915       10381 :     case al_CYCLIC: return algnattoalg_cyc(al, x);
    2916        1225 :     case al_CSA: return algnattoalg_csa(al, x);
    2917             :   }
    2918             :   return NULL; /*LCOV_EXCL_LINE*/
    2919             : }
    2920             : 
    2921             : static GEN
    2922         182 : algalgtobasis_mat(GEN al, GEN x) /* componentwise */
    2923             : {
    2924         182 :   pari_sp av = avma;
    2925             :   long lx, lxj, i, j;
    2926             :   GEN res;
    2927         182 :   lx = lg(x);
    2928         182 :   res = cgetg(lx, t_MAT);
    2929         546 :   for (j=1; j<lx; j++) {
    2930         364 :     lxj = lg(gel(x,j));
    2931         364 :     gel(res,j) = cgetg(lxj, t_COL);
    2932        1092 :     for (i=1; i<lxj; i++)
    2933         728 :       gcoeff(res,i,j) = algalgtobasis(al,gcoeff(x,i,j));
    2934             :   }
    2935         182 :   return gerepilecopy(av,res);
    2936             : }
    2937             : GEN
    2938       59430 : algalgtobasis(GEN al, GEN x)
    2939             : {
    2940             :   pari_sp av;
    2941             :   long tx;
    2942       59430 :   checkalg(al);
    2943       59430 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algalgtobasis [use alginit]", al);
    2944       59416 :   tx = alg_model(al,x);
    2945       59416 :   if (tx==al_BASIS) return gcopy(x);
    2946       59157 :   if (tx==al_MATRIX) return algalgtobasis_mat(al,x);
    2947       58975 :   av = avma;
    2948       58975 :   x = algalgtonat(al,x);
    2949       58975 :   x = RgM_RgC_mul(alg_get_invbasis(al),x);
    2950       58975 :   return gerepileupto(av, x);
    2951             : }
    2952             : 
    2953             : static GEN
    2954         119 : algbasistoalg_mat(GEN al, GEN x) /* componentwise */
    2955             : {
    2956         119 :   long j, lx = lg(x);
    2957         119 :   GEN res = cgetg(lx, t_MAT);
    2958         357 :   for (j=1; j<lx; j++) {
    2959         238 :     long i, lxj = lg(gel(x,j));
    2960         238 :     gel(res,j) = cgetg(lxj, t_COL);
    2961         238 :     for (i=1; i<lxj; i++) gcoeff(res,i,j) = algbasistoalg(al,gcoeff(x,i,j));
    2962             :   }
    2963         119 :   return res;
    2964             : }
    2965             : GEN
    2966        2912 : algbasistoalg(GEN al, GEN x)
    2967             : {
    2968             :   pari_sp av;
    2969             :   long tx;
    2970        2912 :   checkalg(al);
    2971        2912 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algbasistoalg [use alginit]", al);
    2972        2898 :   tx = alg_model(al,x);
    2973        2898 :   if (tx==al_ALGEBRAIC) return gcopy(x);
    2974        2765 :   if (tx==al_MATRIX) return algbasistoalg_mat(al,x);
    2975        2646 :   av = avma;
    2976        2646 :   x = RgM_RgC_mul(alg_get_basis(al),x);
    2977        2646 :   x = algnattoalg(al,x);
    2978        2646 :   return gerepileupto(av, x);
    2979             : }
    2980             : 
    2981             : GEN
    2982       18305 : algrandom(GEN al, GEN b)
    2983             : {
    2984             :   GEN res, p, N;
    2985             :   long i, n;
    2986       18305 :   if (typ(b) != t_INT) pari_err_TYPE("algrandom",b);
    2987       18298 :   if (signe(b)<0) pari_err_DOMAIN("algrandom", "b", "<", gen_0, b);
    2988       18291 :   checkalg(al);
    2989       18284 :   n = alg_get_absdim(al);
    2990       18284 :   N = addiu(shifti(b,1), 1); /* left on stack */
    2991       18284 :   p = alg_get_char(al); if (!signe(p)) p = NULL;
    2992       18284 :   res = cgetg(n+1,t_COL);
    2993      163828 :   for (i=1; i<= n; i++)
    2994             :   {
    2995      145544 :     pari_sp av = avma;
    2996      145544 :     GEN t = subii(randomi(N),b);
    2997      145544 :     if (p) t = modii(t, p);
    2998      145544 :     gel(res,i) = gerepileuptoint(av, t);
    2999             :   }
    3000       18284 :   return res;
    3001             : }
    3002             : 
    3003             : /* Assumes pol has coefficients in the same ring as the COL x; x either
    3004             :  * in basis or algebraic form or [x,mx] where mx is the mult. table of x.
    3005             :  TODO more general version: pol with coeffs in center and x in basis form */
    3006             : GEN
    3007       17066 : algpoleval(GEN al, GEN pol, GEN x)
    3008             : {
    3009       17066 :   pari_sp av = avma;
    3010       17066 :   GEN p, mx = NULL, res;
    3011             :   long i;
    3012       17066 :   checkalg(al);
    3013       17066 :   p = alg_get_char(al);
    3014       17066 :   if (typ(pol) != t_POL) pari_err_TYPE("algpoleval", pol);
    3015       17059 :   if (typ(x) == t_VEC)
    3016             :   {
    3017        6097 :     if (lg(x) != 3) pari_err_TYPE("algpoleval [vector must be of length 2]", x);
    3018        6090 :     mx = gel(x,2);
    3019        6090 :     x = gel(x,1);
    3020        6090 :     if (typ(mx)!=t_MAT || !gequal(x,gel(mx,1)))
    3021          21 :       pari_err_TYPE("algpoleval [mx must be the multiplication table of x]", mx);
    3022             :   }
    3023             :   else
    3024             :   {
    3025       10962 :     switch(alg_model(al,x))
    3026             :     {
    3027          14 :       case al_ALGEBRAIC: mx = algalgmultable(al,x); break;
    3028       10920 :       case al_BASIS: if (!RgX_is_QX(pol))
    3029           7 :         pari_err_IMPL("algpoleval with x in basis form and pol not in Q[x]");
    3030       10927 :       case al_TRIVIAL: mx = algbasismultable(al,x); break;
    3031           7 :       default: pari_err_TYPE("algpoleval", x);
    3032             :     }
    3033             :   }
    3034       17010 :   res = zerocol(lg(mx)-1);
    3035       17010 :   if (signe(p)) {
    3036       63929 :     for (i=lg(pol)-1; i>1; i--)
    3037             :     {
    3038       47717 :       gel(res,1) = Fp_add(gel(res,1), gel(pol,i), p);
    3039       47717 :       if (i>2) res = FpM_FpC_mul(mx, res, p);
    3040             :     }
    3041             :   }
    3042             :   else {
    3043        4746 :     for (i=lg(pol)-1; i>1; i--)
    3044             :     {
    3045        3948 :       gel(res,1) = gadd(gel(res,1), gel(pol,i));
    3046        3948 :       if (i>2) res = RgM_RgC_mul(mx, res);
    3047             :     }
    3048             :   }
    3049       17010 :   return gerepileupto(av, res);
    3050             : }
    3051             : 
    3052             : /** GRUNWALD-WANG **/
    3053             : /*
    3054             : Song Wang's PhD thesis (pdf pages)
    3055             : p.25 definition of chi_b. K^Ker(chi_b) = K(b^(1/m))
    3056             : p.26 bound on the conductor (also Cohen adv. GTM 193 p.166)
    3057             : p.21 & p.34 description special case, also on wikipedia:
    3058             : http://en.wikipedia.org/wiki/Grunwald%E2%80%93Wang_theorem#Special_fields
    3059             : p.77 Kummer case
    3060             : */
    3061             : 
    3062             : /* n > 0. Is n = 2^k ? */
    3063             : static int
    3064         154 : uispow2(ulong n) { return !(n &(n-1)); }
    3065             : 
    3066             : static GEN
    3067         175 : get_phi0(GEN bnr, GEN Lpr, GEN Ld, GEN pl, long *pr, long *pn)
    3068             : {
    3069         175 :   const long NTRY = 10; /* FIXME: magic constant */
    3070         175 :   const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    3071         175 :   GEN S = bnr_get_cyc(bnr);
    3072             :   GEN Sst, G, globGmod, loc, X, Rglob, Rloc, H, U, Lconj;
    3073             :   long i, j, r, nbfrob, nbloc, nz, t;
    3074             : 
    3075         175 :   *pn = n;
    3076         175 :   *pr = r = lg(S)-1;
    3077         175 :   if (!r) return NULL;
    3078         154 :   Lconj = NULL;
    3079         154 :   nbloc = nbfrob = lg(Lpr)-1;
    3080         154 :   if (uispow2(n))
    3081             :   {
    3082          84 :     long l = lg(pl), k = 1;
    3083          84 :     GEN real = cgetg(l, t_VECSMALL);
    3084         210 :     for (i=1; i<l; i++)
    3085         126 :       if (pl[i]==-1) real[k++] = i;
    3086          84 :     if (k > 1)
    3087             :     {
    3088          84 :       GEN nf = bnr_get_nf(bnr), I = bid_get_fact(bnr_get_bid(bnr));
    3089          84 :       GEN v, y, C = idealchineseinit(bnr, I);
    3090          84 :       long r1 = nf_get_r1(nf), n = nbrows(I);
    3091          84 :       nbloc += k-1;
    3092          84 :       Lconj = cgetg(k, t_VEC);
    3093          84 :       v = const_vecsmall(r1,1);
    3094          84 :       y = const_vec(n, gen_1);
    3095         210 :       for (i = 1; i < k; i++)
    3096             :       {
    3097         126 :         v[i] = -1; gel(Lconj,i) = idealchinese(nf,mkvec2(C,v),y);
    3098         126 :         v[i] = 1;
    3099             :       }
    3100             :     }
    3101             :   }
    3102             : 
    3103             :   /* compute Z/n-dual */
    3104         154 :   Sst = cgetg(r+1, t_VECSMALL);
    3105         154 :   for (i=1; i<=r; i++) Sst[i] = ugcdiu(gel(S,i), n);
    3106         154 :   if (Sst[1] != n) return NULL;
    3107             : 
    3108         154 :   globGmod = cgetg(r+1,t_MAT);
    3109         154 :   G = cgetg(r+1,t_VECSMALL);
    3110         336 :   for (i=1; i<=r; i++)
    3111             :   {
    3112         182 :     G[i] = n / Sst[i]; /* pairing between S and Sst */
    3113         182 :     gel(globGmod,i) = cgetg(nbloc+1,t_VECSMALL);
    3114             :   }
    3115             : 
    3116             :   /* compute images of Frobenius elements (and complex conjugation) */
    3117         154 :   loc = cgetg(nbloc+1,t_VECSMALL);
    3118         490 :   for (i=1; i<=nbloc; i++) {
    3119             :     long L;
    3120         350 :     if (i<=nbfrob)
    3121             :     {
    3122         224 :       X = gel(Lpr,i);
    3123         224 :       L = Ld[i];
    3124             :     }
    3125             :     else
    3126             :     { /* X = 1 (mod f), sigma_i(x) < 0, positive at all other real places */
    3127         126 :       X = gel(Lconj,i-nbfrob);
    3128         126 :       L = 2;
    3129             :     }
    3130         350 :     X = ZV_to_Flv(isprincipalray(bnr,X), n);
    3131         868 :     for (nz=0,j=1; j<=r; j++)
    3132             :     {
    3133         518 :       ulong c = (X[j] * G[j]) % L;
    3134         518 :       ucoeff(globGmod,i,j) = c;
    3135         518 :       if (c) nz = 1;
    3136             :     }
    3137         350 :     if (!nz) return NULL;
    3138         336 :     loc[i] = L;
    3139             :   }
    3140             : 
    3141             :   /* try some random elements in the dual */
    3142         140 :   Rglob = cgetg(r+1,t_VECSMALL);
    3143         469 :   for (t=0; t<NTRY; t++) {
    3144         462 :     for (j=1; j<=r; j++) Rglob[j] = random_Fl(Sst[j]);
    3145         462 :     Rloc = zm_zc_mul(globGmod,Rglob);
    3146         945 :     for (i=1; i<=nbloc; i++)
    3147         812 :       if (Rloc[i] % loc[i] == 0) break;
    3148         462 :     if (i > nbloc)
    3149         133 :       return zv_to_ZV(Rglob);
    3150             :   }
    3151             : 
    3152             :   /* try to realize some random elements of the product of the local duals */
    3153           7 :   H = ZM_hnfall_i(shallowconcat(zm_to_ZM(globGmod),
    3154             :                                 diagonal_shallow(zv_to_ZV(loc))), &U, 2);
    3155             :   /* H,U nbloc x nbloc */
    3156           7 :   Rloc = cgetg(nbloc+1,t_COL);
    3157          77 :   for (t=0; t<NTRY; t++) {
    3158             :     /* nonzero random coordinate */ /* TODO add special case ? */
    3159          70 :     for (i=1; i<=nbloc; i++) gel(Rloc,i) = stoi(1 + random_Fl(loc[i]-1));
    3160          70 :     Rglob = hnf_invimage(H, Rloc);
    3161          70 :     if (Rglob)
    3162             :     {
    3163           0 :       Rglob = ZM_ZC_mul(U,Rglob);
    3164           0 :       return vecslice(Rglob,1,r);
    3165             :     }
    3166             :   }
    3167           7 :   return NULL;
    3168             : }
    3169             : 
    3170             : static GEN
    3171         175 : bnrgwsearch(GEN bnr, GEN Lpr, GEN Ld, GEN pl)
    3172             : {
    3173         175 :   pari_sp av = avma;
    3174             :   long n, r;
    3175         175 :   GEN phi0 = get_phi0(bnr,Lpr,Ld,pl, &r,&n), gn, v, H,U;
    3176         175 :   if (!phi0) return gc_const(av, gen_0);
    3177         133 :   gn = stoi(n);
    3178             :   /* compute kernel of phi0 */
    3179         133 :   v = ZV_extgcd(shallowconcat(phi0, gn));
    3180         133 :   U = vecslice(gel(v,2), 1,r);
    3181         133 :   H = ZM_hnfmodid(rowslice(U, 1,r), gn);
    3182         133 :   return gerepileupto(av, H);
    3183             : }
    3184             : 
    3185             : GEN
    3186         133 : bnfgwgeneric(GEN bnf, GEN Lpr, GEN Ld, GEN pl, long var)
    3187             : {
    3188         133 :   pari_sp av = avma;
    3189         133 :   const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    3190             :   forprime_t S;
    3191         133 :   GEN bnr = NULL, ideal = gen_1, nf, dec, H = gen_0, finf, pol;
    3192             :   ulong ell, p;
    3193             :   long deg, i, degell;
    3194         133 :   (void)uisprimepower(n, &ell);
    3195         133 :   nf = bnf_get_nf(bnf);
    3196         133 :   deg = nf_get_degree(nf);
    3197         133 :   degell = ugcd(deg,ell-1);
    3198         133 :   finf = cgetg(lg(pl),t_VEC);
    3199         133 :   for (i=1; i<lg(pl); i++) gel(finf,i) = pl[i]==-1 ? gen_1 : gen_0;
    3200             : 
    3201         133 :   u_forprime_init(&S, 2, ULONG_MAX);
    3202         665 :   while ((p = u_forprime_next(&S))) {
    3203         532 :     if (Fl_powu(p % ell, degell, ell) != 1) continue; /* ell | p^deg-1 ? */
    3204         238 :     dec = idealprimedec(nf, utoipos(p));
    3205         392 :     for (i=1; i<lg(dec); i++) {
    3206         287 :       GEN pp = gel(dec,i);
    3207         287 :       if (RgV_isin(Lpr,pp)) continue;
    3208             :         /* TODO also accept the prime ideals at which there is a condition
    3209             :          * (use local Artin)? */
    3210         231 :       if (smodis(idealnorm(nf,pp),ell) != 1) continue; /* ell | N(pp)-1 ? */
    3211         175 :       ideal = idealmul(bnf,ideal,pp);
    3212             :       /* TODO: give factorization ? */
    3213         175 :       bnr = Buchray(bnf, mkvec2(ideal,finf), nf_INIT);
    3214         175 :       H = bnrgwsearch(bnr,Lpr,Ld,pl);
    3215         175 :       if (H != gen_0)
    3216             :       {
    3217         133 :         pol = rnfkummer(bnr,H,0,nf_get_prec(nf));
    3218         133 :         setvarn(pol, var);
    3219         133 :         return gerepileupto(av,pol);
    3220             :       }
    3221             :     }
    3222             :   }
    3223             :   pari_err_BUG("bnfgwgeneric (no suitable p)"); /*LCOV_EXCL_LINE*/
    3224             :   return NULL;/*LCOV_EXCL_LINE*/
    3225             : }
    3226             : 
    3227             : /* no garbage collection */
    3228             : static GEN
    3229         231 : localextdeg(GEN nf, GEN pr, GEN cnd, long d, long ell, long n)
    3230             : {
    3231         231 :   long g = n/d;
    3232         231 :   GEN res, modpr, ppr = pr, T, p, gen, k;
    3233         231 :   if (d==1) return gen_1;
    3234         210 :   if (equalsi(ell,pr_get_p(pr))) { /* ell == p */
    3235          14 :     res = nfadd(nf, gen_1, pr_get_gen(pr));
    3236          14 :     res = nfpowmodideal(nf, res, stoi(g), cnd);
    3237             :   }
    3238             :   else { /* ell != p */
    3239         196 :     k = powis(stoi(ell),Z_lval(subiu(pr_norm(pr),1),ell));
    3240         196 :     k = divis(k,g);
    3241         196 :     modpr = nf_to_Fq_init(nf, &ppr, &T, &p);
    3242         196 :     (void)Fq_sqrtn(gen_1,k,T,p,&gen);
    3243         196 :     res = Fq_to_nf(gen, modpr);
    3244             :   }
    3245         210 :   return res;
    3246             : }
    3247             : 
    3248             : /* Ld[i] must be nontrivial powers of the same prime ell */
    3249             : /* pl : -1 at real places at which the extention must ramify, 0 elsewhere */
    3250             : GEN
    3251         161 : nfgwkummer(GEN nf, GEN Lpr, GEN Ld, GEN pl, long var)
    3252             : {
    3253         161 :   const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    3254         161 :   pari_sp av = avma;
    3255             :   ulong ell;
    3256             :   long i, v;
    3257             :   GEN cnd, y, x, pol;
    3258         161 :   v = uisprimepower(n, &ell);
    3259         161 :   cnd = zeromatcopy(lg(Lpr)-1,2);
    3260             : 
    3261         161 :   y = vec_ei(lg(Lpr)-1,1);
    3262         392 :   for (i=1; i<lg(Lpr); i++) {
    3263         231 :     GEN pr = gel(Lpr,i), p = pr_get_p(pr), E;
    3264         231 :     long e = pr_get_e(pr);
    3265         231 :     gcoeff(cnd,i,1) = pr;
    3266             : 
    3267         231 :     if (!absequalui(ell,p))
    3268         210 :       E = gen_1;
    3269             :     else
    3270          21 :       E = addui(1 + v*e, divsi(e,subiu(p,1)));
    3271         231 :     gcoeff(cnd,i,2) = E;
    3272         231 :     gel(y,i) = localextdeg(nf, pr, idealpow(nf,pr,E), Ld[i], ell, n);
    3273             :   }
    3274             : 
    3275             :   /* TODO use a factoredextchinese to ease computations afterwards ? */
    3276         161 :   x = idealchinese(nf, mkvec2(cnd,pl), y);
    3277         161 :   x = basistoalg(nf,x);
    3278         161 :   pol = gsub(gpowgs(pol_x(var),n),x);
    3279             : 
    3280         161 :   return gerepileupto(av,pol);
    3281             : }
    3282             : 
    3283             : static GEN
    3284         693 : get_vecsmall(GEN v)
    3285             : {
    3286         693 :   switch(typ(v))
    3287             :   {
    3288         581 :     case t_VECSMALL: return v;
    3289         105 :     case t_VEC: if (RgV_is_ZV(v)) return ZV_to_zv(v);
    3290             :   }
    3291           7 :   pari_err_TYPE("nfgrunwaldwang",v);
    3292             :   return NULL;/*LCOV_EXCL_LINE*/
    3293             : }
    3294             : GEN
    3295         392 : nfgrunwaldwang(GEN nf0, GEN Lpr, GEN Ld, GEN pl, long var)
    3296             : {
    3297             :   ulong n;
    3298         392 :   pari_sp av = avma;
    3299             :   GEN nf, bnf, pr;
    3300             :   long t, w, i, vnf;
    3301             :   ulong ell, ell2;
    3302         392 :   if (var < 0) var = 0;
    3303         392 :   nf = get_nf(nf0,&t);
    3304         392 :   if (!nf) pari_err_TYPE("nfgrunwaldwang",nf0);
    3305         392 :   vnf = nf_get_varn(nf);
    3306         392 :   if (varncmp(var, vnf) >= 0)
    3307           7 :     pari_err_PRIORITY("nfgrunwaldwang", pol_x(var), ">=", vnf);
    3308         385 :   if (typ(Lpr) != t_VEC) pari_err_TYPE("nfgrunwaldwang",Lpr);
    3309         371 :   if (lg(Lpr) != lg(Ld)) pari_err_DIM("nfgrunwaldwang [#Lpr != #Ld]");
    3310         833 :   for (i=1; i<lg(Lpr); i++) {
    3311         476 :     pr = gel(Lpr,i);
    3312         476 :     if (nf_get_degree(nf)==1 && typ(pr)==t_INT)
    3313          63 :       gel(Lpr,i) = gel(idealprimedec(nf,pr), 1);
    3314         413 :     else checkprid(pr);
    3315             :   }
    3316         357 :   if (lg(pl)-1 != nf_get_r1(nf))
    3317           7 :     pari_err_DOMAIN("nfgrunwaldwang [pl should have r1 components]", "#pl",
    3318           7 :         "!=", stoi(nf_get_r1(nf)), stoi(lg(pl)-1));
    3319             : 
    3320         350 :   Ld = get_vecsmall(Ld);
    3321         343 :   pl = get_vecsmall(pl);
    3322         343 :   bnf = get_bnf(nf0,&t);
    3323         343 :   n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    3324             : 
    3325         343 :   if (!uisprimepower(n, &ell))
    3326           7 :     pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (a)");
    3327         770 :   for (i=1; i<lg(Ld); i++)
    3328         441 :     if (Ld[i]!=1 && (!uisprimepower(Ld[i],&ell2) || ell2!=ell))
    3329           7 :       pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (b)");
    3330         770 :   for (i=1; i<lg(pl); i++)
    3331         448 :     if (pl[i]==-1 && ell%2)
    3332           7 :       pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (c)");
    3333             : 
    3334         322 :   w = bnf? bnf_get_tuN(bnf): itos(gel(nfrootsof1(nf),1));
    3335             : 
    3336             :   /* TODO choice between kummer and generic ? Let user choose between speed
    3337             :    * and size */
    3338         322 :   if (w%n==0 && lg(Ld)>1)
    3339         161 :     return gerepileupto(av,nfgwkummer(nf,Lpr,Ld,pl,var));
    3340         161 :   if (ell==n) {
    3341         133 :     if (!bnf) bnf = Buchall(nf,0,0);
    3342         133 :     return gerepileupto(av,bnfgwgeneric(bnf,Lpr,Ld,pl,var));
    3343             :   }
    3344          28 :   pari_err_IMPL("nfgrunwaldwang for non-prime degree");
    3345             :   return NULL; /*LCOV_EXCL_LINE*/
    3346             : }
    3347             : 
    3348             : /** HASSE INVARIANTS **/
    3349             : 
    3350             : /* TODO long -> ulong + uel */
    3351             : static GEN
    3352         917 : hasseconvert(GEN H, long n)
    3353             : {
    3354             :   GEN h, c;
    3355             :   long i, l;
    3356         917 :   switch(typ(H)) {
    3357             :     case t_VEC:
    3358         847 :       l = lg(H); h = cgetg(l,t_VECSMALL);
    3359         847 :       if (l == 1) return h;
    3360         749 :       c = gel(H,1);
    3361         749 :       if (typ(c) == t_VEC && l == 3)
    3362         287 :         return mkvec2(gel(H,1),hasseconvert(gel(H,2),n));
    3363        1225 :       for (i=1; i<l; i++)
    3364             :       {
    3365         791 :         c = gel(H,i);
    3366         791 :         switch(typ(c)) {
    3367         567 :           case t_INT:  break;
    3368             :           case t_INTMOD:
    3369           7 :             c = gel(c,2); break;
    3370             :           case t_FRAC :
    3371         196 :             c = gmulgs(c,n);
    3372         196 :             if (typ(c) == t_INT) break;
    3373           7 :             pari_err_DOMAIN("hasseconvert [degree should be a denominator of the invariant]", "denom(h)", "ndiv", stoi(n), Q_denom(gel(H,i)));
    3374          21 :           default : pari_err_TYPE("Hasse invariant", c);
    3375             :         }
    3376         763 :         h[i] = smodis(c,n);
    3377             :       }
    3378         434 :       return h;
    3379          63 :     case t_VECSMALL: return H;
    3380             :   }
    3381           7 :   pari_err_TYPE("Hasse invariant", H); return NULL;
    3382             : }
    3383             : 
    3384             : /* assume f >= 2 */
    3385             : static long
    3386         392 : cyclicrelfrob0(GEN nf, GEN aut, GEN pr, GEN q, long f, long g)
    3387             : {
    3388         392 :   pari_sp av = avma;
    3389             :   long s;
    3390             :   GEN T, p, modpr, a, b;
    3391             : 
    3392         392 :   modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    3393         392 :   a = pol_x(nf_get_varn(nf));
    3394         392 :   b = galoisapply(nf, aut, modpr_genFq(modpr));
    3395         392 :   b = nf_to_Fq(nf, b, modpr);
    3396         392 :   for (s=0; !ZX_equal(a, b); s++) a = Fq_pow(a, q, T, p);
    3397         392 :   set_avma(av);
    3398         392 :   return g*Fl_inv(s, f);/* <n */
    3399             : }
    3400             : 
    3401             : static GEN
    3402        1008 : rnfprimedec(GEN rnf, GEN pr)
    3403        1008 : { return idealfactor(obj_check(rnf,rnf_NFABS), rnfidealup0(rnf, pr, 1)); }
    3404             : 
    3405             : static long
    3406         938 : cyclicrelfrob(GEN rnf, GEN auts, GEN pr)
    3407             : {
    3408         938 :   pari_sp av = avma;
    3409         938 :   long f,g,frob, n = rnf_get_degree(rnf);
    3410         938 :   GEN fa = rnfprimedec(rnf, pr);
    3411             : 
    3412         938 :   if (cmpis(gcoeff(fa,1,2), 1) > 0)
    3413           0 :     pari_err_DOMAIN("cyclicrelfrob","e(PR/pr)",">",gen_1,pr);
    3414         938 :   g = nbrows(fa);
    3415         938 :   f = n/g;
    3416             : 
    3417         938 :   if (f <= 2) frob = g%n;
    3418             :   else {
    3419         392 :     GEN nf2, PR = gcoeff(fa,1,1);
    3420         392 :     GEN autabs = rnfeltreltoabs(rnf,gel(auts,g));
    3421         392 :     nf2 = obj_check(rnf,rnf_NFABS);
    3422         392 :     autabs = nfadd(nf2, autabs, gmul(rnf_get_k(rnf), rnf_get_alpha(rnf)));
    3423         392 :     frob = cyclicrelfrob0(nf2, autabs, PR, pr_norm(pr), f, g);
    3424             :   }
    3425         938 :   return gc_long(av, frob);
    3426             : }
    3427             : 
    3428             : static long
    3429         553 : localhasse(GEN rnf, GEN cnd, GEN pl, GEN auts, GEN b, long k)
    3430             : {
    3431         553 :   pari_sp av = avma;
    3432             :   long v, m, h, lfa, frob, n, i;
    3433             :   GEN previous, y, pr, nf, q, fa;
    3434         553 :   nf = rnf_get_nf(rnf);
    3435         553 :   n = rnf_get_degree(rnf);
    3436         553 :   pr = gcoeff(cnd,k,1);
    3437         553 :   v = nfval(nf, b, pr);
    3438         553 :   m = lg(cnd)>1 ? nbrows(cnd) : 0;
    3439             : 
    3440             :   /* add the valuation of b to the conductor... */
    3441         553 :   previous = gcoeff(cnd,k,2);
    3442         553 :   gcoeff(cnd,k,2) = addis(previous, v);
    3443             : 
    3444         553 :   y = const_vec(m, gen_1);
    3445         553 :   gel(y,k) = b;
    3446             :   /* find a factored element y congruent to b mod pr^(vpr(b)+vpr(cnd)) and to 1 mod the conductor. */
    3447         553 :   y = factoredextchinese(nf, cnd, y, pl, &fa);
    3448         553 :   h = 0;
    3449         553 :   lfa = nbrows(fa);
    3450             :   /* 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). */
    3451        1043 :   for (i=1; i<=lfa; i++) {
    3452         490 :     q = gcoeff(fa,i,1);
    3453         490 :     if (cmp_prime_ideal(pr,q)) {
    3454         455 :       frob = cyclicrelfrob(rnf, auts, q);
    3455         455 :       frob = Fl_mul(frob,umodiu(gcoeff(fa,i,2),n),n);
    3456         455 :       h = Fl_add(h,frob,n);
    3457             :     }
    3458             :   }
    3459             :   /* ...then restore it. */
    3460         553 :   gcoeff(cnd,k,2) = previous;
    3461         553 :   return gc_long(av, Fl_neg(h,n));
    3462             : }
    3463             : 
    3464             : static GEN
    3465         700 : allauts(GEN rnf, GEN aut)
    3466             : {
    3467         700 :   long n = rnf_get_degree(rnf), i;
    3468         700 :   GEN pol = rnf_get_pol(rnf), vaut;
    3469         700 :   if (n==1) n=2;
    3470         700 :   vaut = cgetg(n,t_VEC);
    3471         700 :   aut = lift_shallow(rnfbasistoalg(rnf,aut));
    3472         700 :   gel(vaut,1) = aut;
    3473        1008 :   for (i=1; i<n-1; i++)
    3474         308 :     gel(vaut,i+1) = RgX_rem(poleval(gel(vaut,i), aut), pol);
    3475         700 :   return vaut;
    3476             : }
    3477             : 
    3478             : static GEN
    3479         224 : clean_factor(GEN fa)
    3480             : {
    3481         224 :   GEN P2,E2, P = gel(fa,1), E = gel(fa,2);
    3482         224 :   long l = lg(P), i, j = 1;
    3483         224 :   P2 = cgetg(l, t_COL);
    3484         224 :   E2 = cgetg(l, t_COL);
    3485         609 :   for (i = 1;i < l; i++)
    3486         385 :     if (signe(gel(E,i))) {
    3487         252 :       gel(P2,j) = gel(P,i);
    3488         252 :       gel(E2,j) = gel(E,i); j++;
    3489             :     }
    3490         224 :   setlg(P2,j);
    3491         224 :   setlg(E2,j); return mkmat2(P2,E2);
    3492             : }
    3493             : 
    3494             : /* shallow concat x[1],...x[nx],y[1], ... y[ny], returning a t_COL. To be
    3495             :  * used when we do not know whether x,y are t_VEC or t_COL */
    3496             : static GEN
    3497         448 : colconcat(GEN x, GEN y)
    3498             : {
    3499         448 :   long i, lx = lg(x), ly = lg(y);
    3500         448 :   GEN z=cgetg(lx+ly-1, t_COL);
    3501         448 :   for (i=1; i<lx; i++) z[i]     = x[i];
    3502         448 :   for (i=1; i<ly; i++) z[lx+i-1]= y[i];
    3503         448 :   return z;
    3504             : }
    3505             : 
    3506             : /* return v(x) at all primes in listpr, replace x by cofactor */
    3507             : static GEN
    3508         924 : nfmakecoprime(GEN nf, GEN *px, GEN listpr)
    3509             : {
    3510         924 :   long j, l = lg(listpr);
    3511         924 :   GEN x1, x = *px, L = cgetg(l, t_COL);
    3512             : 
    3513         924 :   if (typ(x) != t_MAT)
    3514             :   { /* scalar, divide at the end (fast valuation) */
    3515         819 :     x1 = NULL;
    3516        1708 :     for (j=1; j<l; j++)
    3517             :     {
    3518         889 :       GEN pr = gel(listpr,j), e;
    3519         889 :       long v = nfval(nf, x, pr);
    3520         889 :       e = stoi(v); gel(L,j) = e;
    3521        1057 :       if (v) x1 = x1? idealmulpowprime(nf, x1, pr, e)
    3522         168 :                     : idealpow(nf, pr, e);
    3523             :     }
    3524         819 :     if (x1) x = idealdivexact(nf, idealhnf(nf,x), x1);
    3525             :   }
    3526             :   else
    3527             :   { /* HNF, divide as we proceed (reduce size) */
    3528         119 :     for (j=1; j<l; j++)
    3529             :     {
    3530          14 :       GEN pr = gel(listpr,j);
    3531          14 :       long v = idealval(nf, x, pr);
    3532          14 :       gel(L,j) = stoi(v);
    3533          14 :       if (v) x = idealmulpowprime(nf, x, pr, stoi(-v));
    3534             :     }
    3535             :   }
    3536         924 :   *px = x; return L;
    3537             : }
    3538             : 
    3539             : /* Caveat: factorizations are not sorted wrt cmp_prime_ideal: Lpr comes first */
    3540             : static GEN
    3541         224 : computecnd(GEN rnf, GEN Lpr)
    3542             : {
    3543             :   GEN id, nf, fa, Le, P,E;
    3544         224 :   long n = rnf_get_degree(rnf);
    3545             : 
    3546         224 :   nf = rnf_get_nf(rnf);
    3547         224 :   id = rnf_get_idealdisc(rnf);
    3548         224 :   Le = nfmakecoprime(nf, &id, Lpr);
    3549         224 :   fa = idealfactor(nf, id); /* part of D_{L/K} coprime with Lpr */
    3550         224 :   P =  colconcat(Lpr,gel(fa,1));
    3551         224 :   E =  colconcat(Le, gel(fa,2));
    3552         224 :   fa = mkmat2(P, gdiventgs(E, eulerphiu(n)));
    3553         224 :   return mkvec2(fa, clean_factor(fa));
    3554             : }
    3555             : 
    3556             : /* h >= 0 */
    3557             : static void
    3558           0 : nextgen(GEN gene, long h, GEN* gens, GEN* hgens, long* ngens, long* curgcd) {
    3559           0 :   long nextgcd = ugcd(h,*curgcd);
    3560           0 :   if (nextgcd == *curgcd) return;
    3561           0 :   (*ngens)++;
    3562           0 :   gel(*gens,*ngens) = gene;
    3563           0 :   gel(*hgens,*ngens) = utoi(h);
    3564           0 :   *curgcd = nextgcd;
    3565           0 :   return;
    3566             : }
    3567             : 
    3568             : static int
    3569           0 : dividesmod(long d, long h, long n) { return !(h%cgcd(d,n)); }
    3570             : 
    3571             : /* ramified prime with nontrivial Hasse invariant */
    3572             : static GEN
    3573           0 : localcomplete(GEN rnf, GEN pl, GEN cnd, GEN auts, long j, long n, long h, long* v)
    3574             : {
    3575             :   GEN nf, gens, hgens, pr, modpr, T, p, sol, U, D, b, gene, randg, pu;
    3576             :   long ngens, i, d, np, k, d1, d2, hg, dnf, vcnd, curgcd;
    3577           0 :   nf = rnf_get_nf(rnf);
    3578           0 :   pr = gcoeff(cnd,j,1);
    3579           0 :   np = umodiu(pr_norm(pr), n);
    3580           0 :   dnf = nf_get_degree(nf);
    3581           0 :   vcnd = itos(gcoeff(cnd,j,2));
    3582           0 :   ngens = 13+dnf;
    3583           0 :   gens = zerovec(ngens);
    3584           0 :   hgens = zerovec(ngens);
    3585           0 :   *v = 0;
    3586           0 :   curgcd = 0;
    3587           0 :   ngens = 0;
    3588             : 
    3589           0 :   if (!uisprime(n)) {
    3590           0 :     gene =  pr_get_gen(pr);
    3591           0 :     hg = localhasse(rnf, cnd, pl, auts, gene, j);
    3592           0 :     nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
    3593             :   }
    3594             : 
    3595           0 :   if (ugcd(np,n) != 1) { /* GCD(Np,n) != 1 */
    3596           0 :     pu = idealprincipalunits(nf,pr,vcnd);
    3597           0 :     pu = abgrp_get_gen(pu);
    3598           0 :     for (i=1; i<lg(pu) && !dividesmod(curgcd,h,n); i++) {
    3599           0 :       gene = gel(pu,i);
    3600           0 :       hg = localhasse(rnf, cnd, pl, auts, gene, j);
    3601           0 :       nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
    3602             :     }
    3603             :   }
    3604             : 
    3605           0 :   d = ugcd(np-1,n);
    3606           0 :   if (d != 1) { /* GCD(Np-1,n) != 1 */
    3607           0 :     modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    3608           0 :     while (!dividesmod(curgcd,h,n)) { /* TODO gener_FpXQ_local */
    3609           0 :       if (T==NULL) randg = randomi(p);
    3610           0 :       else randg = random_FpX(degpol(T), varn(T),p);
    3611             : 
    3612           0 :       if (!gequal0(randg) && !gequal1(randg)) {
    3613           0 :         gene = Fq_to_nf(randg, modpr);
    3614           0 :         hg = localhasse(rnf, cnd, pl, auts, gene, j);
    3615           0 :         nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
    3616             :       }
    3617             :     }
    3618             :   }
    3619             : 
    3620           0 :   setlg(gens,ngens+1);
    3621           0 :   setlg(hgens,ngens+1);
    3622             : 
    3623           0 :   sol = ZV_extgcd(hgens);
    3624           0 :   D = gel(sol,1);
    3625           0 :   U = gmael(sol,2,ngens);
    3626             : 
    3627           0 :   b = gen_1;
    3628           0 :   d = itou(D);
    3629           0 :   d1 = ugcd(d,n);
    3630           0 :   d2 = d/d1;
    3631           0 :   d = ((h/d1)*Fl_inv(d2,n))%n;
    3632           0 :   for (i=1; i<=ngens; i++) {
    3633           0 :     k = (itos(gel(U,i))*d)%n;
    3634           0 :     if (k<0) k = n-k;
    3635           0 :     if (k) b = nfmul(nf, b, nfpow_u(nf, gel(gens,i),k));
    3636           0 :     if (i==1) *v = k;
    3637             :   }
    3638           0 :   return b;
    3639             : }
    3640             : 
    3641             : static int
    3642         252 : testsplits(GEN data, GEN b, GEN fa)
    3643             : {
    3644             :   GEN rnf, fapr, forbid, P, E;
    3645             :   long i, n;
    3646         252 :   if (gequal0(b)) return 0;
    3647         252 :   P = gel(fa,1);
    3648         252 :   E = gel(fa,2);
    3649         252 :   rnf = gel(data,1);
    3650         252 :   forbid = gel(data,2);
    3651         252 :   n = rnf_get_degree(rnf);
    3652         308 :   for (i=1; i<lgcols(fa); i++) {
    3653          84 :     GEN pr = gel(P,i);
    3654             :     long g;
    3655          84 :     if (tablesearch(forbid, pr, &cmp_prime_ideal)) return 0;
    3656          70 :     fapr = rnfprimedec(rnf,pr);
    3657          70 :     g = nbrows(fapr);
    3658          70 :     if ((itos(gel(E,i))*g)%n) return 0;
    3659             :   }
    3660         224 :   return 1;
    3661             : }
    3662             : 
    3663             : /* remove entries with Hasse invariant 0 */
    3664             : static GEN
    3665         476 : hassereduce(GEN hf)
    3666             : {
    3667         476 :   GEN pr,h, PR = gel(hf,1), H = gel(hf,2);
    3668         476 :   long i, j, l = lg(PR);
    3669             : 
    3670         476 :   pr= cgetg(l, t_VEC);
    3671         476 :   h = cgetg(l, t_VECSMALL);
    3672        1099 :   for (i = j = 1; i < l; i++)
    3673         623 :     if (H[i]) {
    3674         294 :       gel(pr,j) = gel(PR,i);
    3675         294 :       h[j] = H[i]; j++;
    3676             :     }
    3677         476 :   setlg(pr,j);
    3678         476 :   setlg(h,j); return mkvec2(pr,h);
    3679             : }
    3680             : 
    3681             : /* v vector of prid. Return underlying list of rational primes */
    3682             : static GEN
    3683         623 : pr_primes(GEN v)
    3684             : {
    3685         623 :   long i, l = lg(v);
    3686         623 :   GEN w = cgetg(l,t_VEC);
    3687         623 :   for (i=1; i<l; i++) gel(w,i) = pr_get_p(gel(v,i));
    3688         623 :   return ZV_sort_uniq(w);
    3689             : }
    3690             : 
    3691             : /* rnf complete */
    3692             : static GEN
    3693         224 : alg_complete0(GEN rnf, GEN aut, GEN hf, GEN hi, long maxord)
    3694             : {
    3695         224 :   pari_sp av = avma;
    3696             :   GEN nf, pl, pl2, cnd, prcnd, cnds, y, Lpr, auts, b, fa, data, hfe;
    3697             :   GEN forbid, al;
    3698             :   long D, n, d, i, j;
    3699         224 :   nf = rnf_get_nf(rnf);
    3700         224 :   n = rnf_get_degree(rnf);
    3701         224 :   d = nf_get_degree(nf);
    3702         224 :   D = d*n*n;
    3703         224 :   checkhasse(nf,hf,hi,n);
    3704         224 :   hf = hassereduce(hf);
    3705         224 :   Lpr = gel(hf,1);
    3706         224 :   hfe = gel(hf,2);
    3707             : 
    3708         224 :   auts = allauts(rnf,aut);
    3709             : 
    3710         224 :   pl = gcopy(hi); /* conditions on the final b */
    3711         224 :   pl2 = gcopy(hi); /* conditions for computing local Hasse invariants */
    3712         497 :   for (i=1; i<lg(pl); i++) {
    3713         273 :     if (hi[i]) { pl[i] = -1; pl2[i] = 1; }
    3714         196 :     else if (!rnfrealdec(rnf,i)) { pl[i] = 1; pl2[i] = 1; }
    3715             :   }
    3716             : 
    3717         224 :   cnds = computecnd(rnf,Lpr);
    3718         224 :   prcnd = gel(cnds,1);
    3719         224 :   cnd = gel(cnds,2);
    3720         224 :   y = cgetg(lgcols(prcnd),t_VEC);
    3721         224 :   forbid = vectrunc_init(lg(Lpr));
    3722         357 :   for (i=j=1; i<lg(Lpr); i++)
    3723             :   {
    3724         133 :     GEN pr = gcoeff(prcnd,i,1), yi;
    3725         133 :     long v, e = itou( gcoeff(prcnd,i,2) );
    3726         133 :     if (!e) {
    3727         133 :       long frob = cyclicrelfrob(rnf,auts,pr), f1 = ugcd(frob,n);
    3728         133 :       vectrunc_append(forbid, pr);
    3729         133 :       yi = gen_0;
    3730         133 :       v = ((hfe[i]/f1) * Fl_inv(frob/f1,n)) % n;
    3731             :     }
    3732             :     else
    3733           0 :       yi = localcomplete(rnf, pl2, cnd, auts, j++, n, hfe[i], &v);
    3734         133 :     gel(y,i) = yi;
    3735         133 :     gcoeff(prcnd,i,2) = stoi(e + v);
    3736             :   }
    3737         224 :   for (; i<lgcols(prcnd); i++) gel(y,i) = gen_1;
    3738         224 :   gen_sort_inplace(forbid, (void*)&cmp_prime_ideal, &cmp_nodata, NULL);
    3739         224 :   data = mkvec2(rnf,forbid);
    3740         224 :   b = factoredextchinesetest(nf,prcnd,y,pl,&fa,data,testsplits);
    3741             : 
    3742         224 :   al = cgetg(12, t_VEC);
    3743         224 :   gel(al,10)= gen_0; /* must be set first */
    3744         224 :   gel(al,1) = rnf;
    3745         224 :   gel(al,2) = auts;
    3746         224 :   gel(al,3) = basistoalg(nf,b);
    3747         224 :   gel(al,4) = hi;
    3748             :   /* add primes | disc or b with trivial Hasse invariant to hf */
    3749         224 :   Lpr = gel(prcnd,1); y = b;
    3750         224 :   (void)nfmakecoprime(nf, &y, Lpr);
    3751         224 :   Lpr = shallowconcat(Lpr, gel(idealfactor(nf,y), 1));
    3752         224 :   settyp(Lpr,t_VEC);
    3753         224 :   hf = mkvec2(Lpr, shallowconcat(hfe, const_vecsmall(lg(Lpr)-lg(hfe), 0)));
    3754         224 :   gel(al,5) = hf;
    3755         224 :   gel(al,6) = gen_0;
    3756         224 :   gel(al,7) = matid(D);
    3757         224 :   gel(al,8) = matid(D); /* TODO modify 7, 8 et 9 once LLL added */
    3758         224 :   gel(al,9) = algnatmultable(al,D);
    3759         224 :   gel(al,11)= algtracebasis(al);
    3760         224 :   if (maxord) al = alg_maximal_primes(al, pr_primes(Lpr));
    3761         224 :   return gerepilecopy(av, al);
    3762             : }
    3763             : 
    3764             : GEN
    3765           0 : alg_complete(GEN rnf, GEN aut, GEN hf, GEN hi, long maxord)
    3766             : {
    3767           0 :   long n = rnf_get_degree(rnf);
    3768           0 :   rnfcomplete(rnf);
    3769           0 :   return alg_complete0(rnf,aut,hasseconvert(hf,n),hasseconvert(hi,n), maxord);
    3770             : }
    3771             : 
    3772             : void
    3773        1239 : checkhasse(GEN nf, GEN hf, GEN hi, long n)
    3774             : {
    3775             :   GEN Lpr, Lh;
    3776             :   long i, sum;
    3777        1239 :   if (typ(hf) != t_VEC || lg(hf) != 3) pari_err_TYPE("checkhasse [hf]", hf);
    3778        1232 :   Lpr = gel(hf,1);
    3779        1232 :   Lh = gel(hf,2);
    3780        1232 :   if (typ(Lpr) != t_VEC) pari_err_TYPE("checkhasse [Lpr]", Lpr);
    3781        1232 :   if (typ(Lh) != t_VECSMALL) pari_err_TYPE("checkhasse [Lh]", Lh);
    3782        1232 :   if (typ(hi) != t_VECSMALL) pari_err_TYPE("checkhasse [hi]", hi);
    3783        1232 :   if ((nf && lg(hi) != nf_get_r1(nf)+1))
    3784           7 :     pari_err_DOMAIN("checkhasse [hi should have r1 components]","#hi","!=",stoi(nf_get_r1(nf)),stoi(lg(hi)-1));
    3785        1225 :   if (lg(Lpr) != lg(Lh))
    3786           7 :     pari_err_DIM("checkhasse [Lpr and Lh should have same length]");
    3787        1218 :   for (i=1; i<lg(Lpr); i++) checkprid(gel(Lpr,i));
    3788        1218 :   if (lg(gen_sort_uniq(Lpr, (void*)cmp_prime_ideal, cmp_nodata)) < lg(Lpr))
    3789           7 :     pari_err(e_MISC, "error in checkhasse [duplicate prime ideal]");
    3790        1211 :   sum = 0;
    3791        1211 :   for (i=1; i<lg(Lh); i++) sum = (sum+Lh[i])%n;
    3792        2611 :   for (i=1; i<lg(hi); i++) {
    3793        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]));
    3794        1400 :       sum = (sum+hi[i])%n;
    3795             :   }
    3796        1197 :   if (sum<0) sum = n+sum;
    3797        1197 :   if (sum != 0)
    3798           7 :     pari_err_DOMAIN("checkhasse","sum(Hasse invariants)","!=",gen_0,Lh);
    3799        1190 : }
    3800             : 
    3801             : static GEN
    3802         322 : hassecoprime(GEN hf, GEN hi, long n)
    3803             : {
    3804         322 :   pari_sp av = avma;
    3805             :   long l, i, j, lk, inv;
    3806             :   GEN fa, P,E, res, hil, hfl;
    3807         322 :   hi = hasseconvert(hi, n);
    3808         308 :   hf = hasseconvert(hf, n);
    3809         287 :   checkhasse(NULL,hf,hi,n);
    3810         245 :   fa = factoru(n);
    3811         245 :   P = gel(fa,1); l = lg(P);
    3812         245 :   E = gel(fa,2);
    3813         245 :   res = cgetg(l,t_VEC);
    3814         497 :   for (i=1; i<l; i++) {
    3815         252 :     lk = upowuu(P[i],E[i]);
    3816         252 :     inv = Fl_invsafe((n/lk)%lk, lk);
    3817         252 :     hil = gcopy(hi);
    3818         252 :     hfl = gcopy(hf);
    3819             : 
    3820         252 :     if (P[i] == 2)
    3821         210 :       for (j=1; j<lg(hil); j++) hil[j] = hi[j]==0 ? 0 : lk/2;
    3822             :     else
    3823          42 :       for (j=1; j<lg(hil); j++) hil[j] = 0;
    3824         252 :     for (j=1; j<lgcols(hfl); j++) gel(hfl,2)[j] = (gel(hf,2)[j]*inv)%lk;
    3825         252 :     hfl = hassereduce(hfl);
    3826         252 :     gel(res,i) = mkvec3(hfl,hil,utoi(lk));
    3827             :   }
    3828             : 
    3829         245 :   return gerepilecopy(av, res);
    3830             : }
    3831             : 
    3832             : /* no garbage collection */
    3833             : static GEN
    3834          70 : genefrob(GEN nf, GEN gal, GEN r)
    3835             : {
    3836             :   long i;
    3837          70 :   GEN g = identity_perm(nf_get_degree(nf)), fa = Z_factor(r), p, pr, frob;
    3838         119 :   for (i=1; i<lgcols(fa); i++) {
    3839          49 :     p = gcoeff(fa,i,1);
    3840          49 :     pr = idealprimedec(nf, p);
    3841          49 :     pr = gel(pr,1);
    3842          49 :     frob = idealfrobenius(nf, gal, pr);
    3843          49 :     g = perm_mul(g, perm_pow(frob, itos(gcoeff(fa,i,2))));
    3844             :   }
    3845          70 :   return g;
    3846             : }
    3847             : 
    3848             : static GEN
    3849         224 : rnfcycaut(GEN rnf)
    3850             : {
    3851         224 :   GEN nf2 = obj_check(rnf, rnf_NFABS);
    3852             :   GEN L, alpha, pol, salpha, s, sj, polabs, k, X, pol0, nf;
    3853             :   long i, d, j;
    3854         224 :   d = rnf_get_degree(rnf);
    3855         224 :   L = galoisconj(nf2,NULL);
    3856         224 :   alpha = lift_shallow(rnf_get_alpha(rnf));
    3857         224 :   pol = rnf_get_pol(rnf);
    3858         224 :   k = rnf_get_k(rnf);
    3859         224 :   polabs = rnf_get_polabs(rnf);
    3860         224 :   nf = rnf_get_nf(rnf);
    3861         224 :   pol0 = nf_get_pol(nf);
    3862         224 :   X = RgX_rem(pol_x(varn(pol0)), pol0);
    3863             : 
    3864             :   /* TODO check mod prime of degree 1 */
    3865         406 :   for (i=1; i<lg(L); i++) {
    3866         406 :     s = gel(L,i);
    3867         406 :     salpha = RgX_RgXQ_eval(alpha,s,polabs);
    3868         406 :     if (!gequal(alpha,salpha)) continue;
    3869             : 
    3870         364 :     s = lift_shallow(rnfeltabstorel(rnf,s));
    3871         364 :     sj = s = gsub(s, gmul(k,X));
    3872         623 :     for (j=1; !gequal0(gsub(sj,pol_x(varn(s)))); j++)
    3873         259 :       sj = RgX_RgXQ_eval(sj,s,pol);
    3874         364 :     if (j<d) continue;
    3875         224 :     return s;
    3876             :   }
    3877             :   return NULL; /*LCOV_EXCL_LINE*/
    3878             : }
    3879             : 
    3880             : /* returns Lpr augmented with an extra, distinct prime */
    3881             : /* TODO be less lazy and return a small prime */
    3882             : static GEN
    3883          84 : extraprime(GEN nf, GEN Lpr)
    3884             : {
    3885          84 :   GEN Lpr2, p = gen_2, pr;
    3886             :   long i;
    3887          84 :   Lpr2 = cgetg(lg(Lpr)+1,t_VEC);
    3888          98 :   for (i=1; i<lg(Lpr); i++)
    3889             :   {
    3890          14 :     gel(Lpr2,i) = gel(Lpr,i);
    3891          14 :     p = gmax_shallow(p, pr_get_p(gel(Lpr,i)));
    3892             :   }
    3893          84 :   p = nextprime(addis(p,1));
    3894          84 :   pr = gel(idealprimedec_limit_f(nf, p, 0), 1);
    3895          84 :   gel(Lpr2,lg(Lpr)) = pr;
    3896          84 :   return Lpr2;
    3897             : }
    3898             : 
    3899             : GEN
    3900         336 : alg_hasse(GEN nf, long n, GEN hf, GEN hi, long var, long maxord)
    3901             : {
    3902         336 :   pari_sp av = avma;
    3903         336 :   GEN primary, al = gen_0, al2, rnf, hil, hfl, Ld, pl, pol, Lpr, aut, Lpr2, Ld2;
    3904             :   long i, lk, j, maxdeg;
    3905         336 :   dbg_printf(1)("alg_hasse\n");
    3906         336 :   if (n<=1) pari_err_DOMAIN("alg_hasse", "degree", "<=", gen_1, stoi(n));
    3907         322 :   primary = hassecoprime(hf, hi, n);
    3908         476 :   for (i=1; i<lg(primary); i++) {
    3909         252 :     lk = itos(gmael(primary,i,3));
    3910         252 :     hfl = gmael(primary,i,1);
    3911         252 :     hil = gmael(primary,i,2);
    3912         252 :     checkhasse(nf, hfl, hil, lk);
    3913         245 :     dbg_printf(1)("alg_hasse: i=%d hf=%Ps hi=%Ps lk=%d\n", i, hfl, hil, lk);
    3914             : 
    3915         245 :     if (lg(gel(hfl,1))>1 || lk%2==0) {
    3916         238 :       maxdeg = 1;
    3917         238 :       Lpr = gel(hfl,1);
    3918         238 :       Ld = gcopy(gel(hfl,2));
    3919         385 :       for (j=1; j<lg(Ld); j++)
    3920             :       {
    3921         147 :         Ld[j] = lk/ugcd(lk,Ld[j]);
    3922         147 :         maxdeg = maxss(Ld[j],maxdeg);
    3923             :       }
    3924         238 :       pl = gcopy(hil);
    3925         525 :       for (j=1; j<lg(pl); j++) if(pl[j])
    3926             :       {
    3927          77 :         pl[j] = -1;
    3928          77 :         maxdeg = maxss(maxdeg,2);
    3929             :       }
    3930             : 
    3931         238 :       Lpr2 = Lpr;
    3932         238 :       Ld2 = Ld;
    3933         238 :       if (maxdeg<lk)
    3934             :       {
    3935         154 :         if (maxdeg==1 && lk==2 && lg(pl)>1) pl[1] = -1;
    3936             :         else
    3937             :         {
    3938          84 :           Lpr2 = extraprime(nf,Lpr);
    3939          84 :           Ld2 = cgetg(lg(Ld)+1, t_VECSMALL);
    3940          84 :           for (j=1; j<lg(Ld); j++) Ld2[j] = Ld[j];
    3941          84 :           Ld2[lg(Ld)] = lk;
    3942             :         }
    3943             :       }
    3944             : 
    3945         238 :       dbg_printf(2)("alg_hasse: calling nfgrunwaldwang Lpr=%Ps Pd=%Ps pl=%Ps\n",
    3946             :           Lpr, Ld, pl);
    3947         238 :       pol = nfgrunwaldwang(nf, Lpr2, Ld2, pl, var);
    3948         224 :       dbg_printf(2)("alg_hasse: calling rnfinit(%Ps)\n", pol);
    3949         224 :       rnf = rnfinit0(nf,pol,1);
    3950         224 :       dbg_printf(2)("alg_hasse: computing automorphism\n");
    3951         224 :       aut = rnfcycaut(rnf);
    3952         224 :       dbg_printf(2)("alg_hasse: calling alg_complete\n");
    3953         224 :       al2 = alg_complete0(rnf,aut,hfl,hil,maxord);
    3954             :     }
    3955           7 :     else al2 = alg_matrix(nf, lk, var, cgetg(1,t_VEC), maxord);
    3956             : 
    3957         231 :     if (i==1) al = al2;
    3958           7 :     else      al = algtensor(al,al2,maxord);
    3959             :   }
    3960         224 :   return gerepilecopy(av,al);
    3961             : }
    3962             : 
    3963             : /** CYCLIC ALGEBRA WITH GIVEN HASSE INVARIANTS **/
    3964             : 
    3965             : /* no garbage collection */
    3966             : static int
    3967          70 : linindep(GEN pol, GEN L)
    3968             : {
    3969             :   long i;
    3970             :   GEN fa;
    3971          70 :   for (i=1; i<lg(L); i++) {
    3972           0 :     fa = nffactor(gel(L,i),pol);
    3973           0 :     if (lgcols(fa)>2) return 0;
    3974             :   }
    3975          70 :   return 1;
    3976             : }
    3977             : 
    3978             : /* no garbage collection */
    3979             : static GEN
    3980          70 : subcycloindep(GEN nf, long n, long v, GEN L, GEN *pr)
    3981             : {
    3982             :   pari_sp av;
    3983             :   forprime_t S;
    3984             :   ulong p;
    3985          70 :   u_forprime_arith_init(&S, 1, ULONG_MAX, 1, n);
    3986          70 :   av = avma;
    3987         147 :   while ((p = u_forprime_next(&S)))
    3988             :   {
    3989          77 :     ulong r = pgener_Fl(p);
    3990          77 :     GEN pol = galoissubcyclo(utoipos(p), utoipos(Fl_powu(r,n,p)), 0, v);
    3991          77 :     GEN fa = nffactor(nf, pol);
    3992          77 :     if (lgcols(fa) == 2 && linindep(pol,L)) { *pr = utoipos(r); return pol; }
    3993           7 :     set_avma(av);
    3994             :   }
    3995             :   pari_err_BUG("subcycloindep (no suitable prime = 1(mod n))"); /*LCOV_EXCL_LINE*/
    3996             :   *pr = NULL; return NULL; /*LCOV_EXCL_LINE*/
    3997             : }
    3998             : 
    3999             : GEN
    4000          77 : alg_matrix(GEN nf, long n, long v, GEN L, long maxord)
    4001             : {
    4002          77 :   pari_sp av = avma;
    4003             :   GEN pol, gal, rnf, cyclo, g, r, aut;
    4004          77 :   dbg_printf(1)("alg_matrix\n");
    4005          77 :   if (n<=0) pari_err_DOMAIN("alg_matrix", "n", "<=", gen_0, stoi(n));
    4006          70 :   pol = subcycloindep(nf, n, v, L, &r);
    4007          70 :   rnf = rnfinit(nf, pol);
    4008          70 :   cyclo = nfinit(pol, nf_get_prec(nf));
    4009          70 :   gal = galoisinit(cyclo, NULL);
    4010          70 :   g = genefrob(cyclo,gal,r);
    4011          70 :   aut = galoispermtopol(gal,g);
    4012          70 :   return gerepileupto(av, alg_cyclic(rnf, aut, gen_1, maxord));
    4013             : }
    4014             : 
    4015             : GEN
    4016         273 : alg_hilbert(GEN nf, GEN a, GEN b, long v, long maxord)
    4017             : {
    4018         273 :   pari_sp av = avma;
    4019             :   GEN C, P, rnf, aut;
    4020         273 :   dbg_printf(1)("alg_hilbert\n");
    4021         273 :   checknf(nf);
    4022         273 :   if (!isint1(Q_denom(a)))
    4023           7 :     pari_err_DOMAIN("alg_hilbert", "denominator(a)", "!=", gen_1,a);
    4024         266 :   if (!isint1(Q_denom(b)))
    4025           7 :     pari_err_DOMAIN("alg_hilbert", "denominator(b)", "!=", gen_1,b);
    4026             : 
    4027         259 :   if (v < 0) v = 0;
    4028         259 :   C = Rg_col_ei(gneg(a), 3, 3);
    4029         259 :   gel(C,1) = gen_1;
    4030         259 :   P = gtopoly(C,v);
    4031         259 :   rnf = rnfinit(nf, P);
    4032         252 :   aut = gneg(pol_x(v));
    4033         252 :   return gerepileupto(av, alg_cyclic(rnf, aut, b, maxord));
    4034             : }
    4035             : 
    4036             : GEN
    4037        1043 : alginit(GEN A, GEN B, long v, long maxord)
    4038             : {
    4039             :   long w;
    4040        1043 :   switch(nftyp(A))
    4041             :   {
    4042             :     case typ_NF:
    4043         875 :       if (v<0) v=0;
    4044         875 :       w = gvar(nf_get_pol(A));
    4045         875 :       if (varncmp(v,w)>=0) pari_err_PRIORITY("alginit", pol_x(v), ">=", w);
    4046         861 :       switch(typ(B))
    4047             :       {
    4048             :         long nB;
    4049          70 :         case t_INT: return alg_matrix(A, itos(B), v, cgetg(1,t_VEC), maxord);
    4050             :         case t_VEC:
    4051         784 :           nB = lg(B)-1;
    4052         784 :           if (nB && typ(gel(B,1)) == t_MAT) return alg_csa_table(A,B,v,maxord);
    4053         623 :           switch(nB)
    4054             :           {
    4055         273 :             case 2: return alg_hilbert(A, gel(B,1), gel(B,2), v, maxord);
    4056             :             case 3:
    4057         343 :               if (typ(gel(B,1))!=t_INT)
    4058           7 :                   pari_err_TYPE("alginit [degree should be an integer]", gel(B,1));
    4059         336 :               return alg_hasse(A, itos(gel(B,1)), gel(B,2), gel(B,3), v,
    4060             :                                                                       maxord);
    4061             :           }
    4062             :       }
    4063          14 :       pari_err_TYPE("alginit", B); break;
    4064             : 
    4065             :     case typ_RNF:
    4066         161 :       if (typ(B) != t_VEC || lg(B) != 3) pari_err_TYPE("alginit", B);
    4067         147 :       return alg_cyclic(A, gel(B,1), gel(B,2), maxord);
    4068             :   }
    4069           7 :   pari_err_TYPE("alginit", A);
    4070             :   return NULL;/*LCOV_EXCL_LINE*/
    4071             : }
    4072             : 
    4073             : /* assumes al CSA or CYCLIC */
    4074             : static GEN
    4075         833 : algnatmultable(GEN al, long D)
    4076             : {
    4077             :   GEN res, x;
    4078             :   long i;
    4079         833 :   res = cgetg(D+1,t_VEC);
    4080        9793 :   for (i=1; i<=D; i++) {
    4081        8960 :     x = algnattoalg(al,col_ei(D,i));
    4082        8960 :     gel(res,i) = algZmultable(al,x);
    4083             :   }
    4084         833 :   return res;
    4085             : }
    4086             : 
    4087             : /* no garbage collection */
    4088             : static void
    4089         476 : algcomputehasse(GEN al)
    4090             : {
    4091             :   long r1, k, n, m, m1, m2, m3, i, m23, m123;
    4092             :   GEN rnf, nf, b, fab, disc2, cnd, fad, auts, pr, pl, perm;
    4093             :   GEN hi, PH, H, L;
    4094             : 
    4095         476 :   rnf = alg_get_splittingfield(al);
    4096         476 :   n = rnf_get_degree(rnf);
    4097         476 :   nf = rnf_get_nf(rnf);
    4098         476 :   b = alg_get_b(al);
    4099         476 :   r1 = nf_get_r1(nf);
    4100         476 :   auts = alg_get_auts(al);
    4101         476 :   (void)alg_get_abssplitting(al);
    4102             : 
    4103             :   /* real places where rnf/nf ramifies */
    4104         476 :   pl = cgetg(r1+1, t_VECSMALL);
    4105         476 :   for (k=1; k<=r1; k++) pl[k] = !rnfrealdec(rnf,k);
    4106             : 
    4107             :   /* infinite Hasse invariants */
    4108         476 :   if (odd(n)) hi = const_vecsmall(r1, 0);
    4109             :   else
    4110             :   {
    4111         406 :     GEN s = nfsign(nf, b);
    4112         406 :     hi = cgetg(r1+1, t_VECSMALL);
    4113         406 :     for (k = 1; k<=r1; k++) hi[k] = (s[k] && pl[k]) ? (n/2) : 0;
    4114             :   }
    4115             : 
    4116         476 :   fab = idealfactor(nf, b);
    4117         476 :   disc2 = rnf_get_idealdisc(rnf);
    4118         476 :   L = nfmakecoprime(nf, &disc2, gel(fab,1));
    4119         476 :   m = lg(L)-1;
    4120             :   /* m1 = #{pr|b: pr \nmid disc}, m3 = #{pr|b: pr | disc} */
    4121         476 :   perm = cgetg(m+1, t_VECSMALL);
    4122         861 :   for (i=1, m1=m, k=1; k<=m; k++)
    4123         385 :     if (signe(gel(L,k))) perm[m1--] = k; else perm[i++] = k;
    4124         476 :   m3 = m - m1;
    4125             : 
    4126             :   /* disc2 : factor of disc coprime to b */
    4127         476 :   fad = idealfactor(nf, disc2);
    4128             :   /* m2 : number of prime factors of disc not dividing b */
    4129         476 :   m2 = nbrows(fad);
    4130         476 :   m23 = m2+m3;
    4131         476 :   m123 = m1+m2+m3;
    4132             : 
    4133             :   /* initialize the possibly ramified primes (hasse) and the factored conductor of rnf/nf (cnd) */
    4134         476 :   cnd = zeromatcopy(m23,2);
    4135         476 :   PH = cgetg(m123+1, t_VEC); /* ramified primes */
    4136         476 :   H = cgetg(m123+1, t_VECSMALL); /* Hasse invariant */
    4137             :   /* compute Hasse invariant at primes that are unramified in rnf/nf */
    4138         826 :   for (k=1; k<=m1; k++) {/* pr | b, pr \nmid disc */
    4139         350 :     long frob, e, j = perm[k];
    4140         350 :     pr = gcoeff(fab,j,1);
    4141         350 :     e = itos(gcoeff(fab,j,2));
    4142         350 :     frob = cyclicrelfrob(rnf, auts, pr);
    4143         350 :     gel(PH,k) = pr;
    4144         350 :     H[k] = Fl_mul(frob, e, n);
    4145             :   }
    4146             :   /* compute Hasse invariant at primes that are ramified in rnf/nf */
    4147         994 :   for (k=1; k<=m2; k++) {/* pr \nmid b, pr | disc */
    4148         518 :     pr = gcoeff(fad,k,1);
    4149         518 :     gel(PH,k+m1) = pr;
    4150         518 :     gcoeff(cnd,k,1) = pr;
    4151         518 :     gcoeff(cnd,k,2) = gcoeff(fad,k,2);
    4152             :   }
    4153         511 :   for (k=1; k<=m3; k++) { /* pr | (b, disc) */
    4154          35 :     long j = perm[k+m1];
    4155          35 :     pr = gcoeff(fab,j,1);
    4156          35 :     gel(PH,k+m1+m2) = pr;
    4157          35 :     gcoeff(cnd,k+m2,1) = pr;
    4158          35 :     gcoeff(cnd,k+m2,2) = gel(L,j);
    4159             :   }
    4160         476 :   gel(cnd,2) = gdiventgs(gel(cnd,2), eulerphiu(n));
    4161         476 :   for (k=1; k<=m23; k++) H[k+m1] = localhasse(rnf, cnd, pl, auts, b, k);
    4162         476 :   gel(al,4) = hi;
    4163         476 :   perm = gen_indexsort(PH, (void*)&cmp_prime_ideal, &cmp_nodata);
    4164         476 :   gel(al,5) = mkvec2(vecpermute(PH,perm),vecsmallpermute(H,perm));
    4165         476 :   checkhasse(nf,alg_get_hasse_f(al),alg_get_hasse_i(al),n);
    4166         476 : }
    4167             : 
    4168             : static GEN
    4169         749 : alg_maximal_primes(GEN al, GEN P)
    4170             : {
    4171         749 :   pari_sp av = avma;
    4172         749 :   long l = lg(P), i;
    4173        1911 :   for (i=1; i<l; i++)
    4174             :   {
    4175        1162 :     if (i != 1) al = gerepilecopy(av, al);
    4176        1162 :     al = alg_pmaximal(al,gel(P,i));
    4177             :   }
    4178         749 :   return al;
    4179             : }
    4180             : 
    4181             : GEN
    4182         483 : alg_cyclic(GEN rnf, GEN aut, GEN b, long maxord)
    4183             : {
    4184         483 :   pari_sp av = avma;
    4185             :   GEN al, nf;
    4186             :   long D, n, d;
    4187         483 :   dbg_printf(1)("alg_cyclic\n");
    4188         483 :   checkrnf(rnf);
    4189         483 :   if (!isint1(Q_denom(b)))
    4190           7 :     pari_err_DOMAIN("alg_cyclic", "denominator(b)", "!=", gen_1,b);
    4191             : 
    4192         476 :   nf = rnf_get_nf(rnf);
    4193         476 :   n = rnf_get_degree(rnf);
    4194         476 :   d = nf_get_degree(nf);
    4195         476 :   D = d*n*n;
    4196             : 
    4197         476 :   al = cgetg(12,t_VEC);
    4198         476 :   gel(al,10)= gen_0; /* must be set first */
    4199         476 :   gel(al,1) = rnf;
    4200         476 :   gel(al,2) = allauts(rnf, aut);
    4201         476 :   gel(al,3) = basistoalg(nf,b);
    4202         476 :   rnf_build_nfabs(rnf, nf_get_prec(nf));
    4203         476 :   gel(al,6) = gen_0;
    4204         476 :   gel(al,7) = matid(D);
    4205         476 :   gel(al,8) = matid(D); /* TODO modify 7, 8 et 9 once LLL added */
    4206         476 :   gel(al,9) = algnatmultable(al,D);
    4207         476 :   gel(al,11)= algtracebasis(al);
    4208             : 
    4209         476 :   algcomputehasse(al);
    4210             : 
    4211         476 :   if (maxord) {
    4212         413 :     GEN hf = alg_get_hasse_f(al), pr = gel(hf,1);
    4213         413 :     al = alg_maximal_primes(al, pr_primes(pr));
    4214             :   }
    4215         476 :   return gerepilecopy(av, al);
    4216             : }
    4217             : 
    4218             : static int
    4219         378 : ismaximalsubfield(GEN al, GEN x, GEN d, long v, GEN *pt_minpol)
    4220             : {
    4221         378 :   GEN cp = algbasischarpoly(al, x, v), lead;
    4222         378 :   if (!ispower(cp, d, pt_minpol)) return 0;
    4223         378 :   lead = leading_coeff(*pt_minpol);
    4224         378 :   if (isintm1(lead)) *pt_minpol = gneg(*pt_minpol);
    4225         378 :   return ZX_is_irred(*pt_minpol);
    4226             : }
    4227             : 
    4228             : static GEN
    4229         133 : findmaximalsubfield(GEN al, GEN d, long v)
    4230             : {
    4231         133 :   long count, nb=2, i, N = alg_get_absdim(al), n = nf_get_degree(alg_get_center(al));
    4232         133 :   GEN x, minpol, maxc = gen_1;
    4233             : 
    4234         210 :   for (i=n+1; i<=N; i+=n) {
    4235         336 :     for (count=0; count<2 && i+count<=N; count++) {
    4236         259 :       x = col_ei(N,i+count);
    4237         259 :       if (ismaximalsubfield(al, x, d, v, &minpol)) return mkvec2(x,minpol);
    4238             :     }
    4239             :   }
    4240             : 
    4241             :   while(1) {
    4242         175 :     x = zerocol(N);
    4243         504 :     for (count=0; count<nb; count++)
    4244             :     {
    4245         385 :       i = random_Fl(N)+1;
    4246         385 :       gel(x,i) = addiu(randomi(maxc),1);
    4247         385 :       if (random_bits(1)) gel(x,i) = negi(gel(x,i));
    4248             :     }
    4249         119 :     if (ismaximalsubfield(al, x, d, v, &minpol)) return mkvec2(x,minpol);
    4250          56 :     if (!random_bits(3)) maxc = addiu(maxc,1);
    4251          56 :     if (nb<N) nb++;
    4252             :   }
    4253             : 
    4254             :   return NULL; /* LCOV_EXCL_LINE */
    4255             : }
    4256             : 
    4257             : static GEN
    4258         133 : frobeniusform(GEN al, GEN x)
    4259             : {
    4260             :   GEN M, FP, P, Pi;
    4261             : 
    4262             :   /* /!\ has to be the *right* multiplication table */
    4263         133 :   M = algbasisrightmultable(al, x);
    4264             : 
    4265         133 :   FP = matfrobenius(M,2,0); /* M = P^(-1)*F*P */
    4266         133 :   P = gel(FP,2);
    4267         133 :   Pi = RgM_inv(P);
    4268         133 :   return mkvec2(P, Pi);
    4269             : }
    4270             : 
    4271             : static void
    4272         133 : computesplitting(GEN al, long d, long v)
    4273             : {
    4274         133 :   GEN subf, x, pol, polabs, basis, P, Pi, nf = alg_get_center(al), rnf, Lbasis, Lbasisinv, Q, pows;
    4275         133 :   long i, n = nf_get_degree(nf), nd = n*d, N = alg_get_absdim(al), j, j2;
    4276             : 
    4277         133 :   subf = findmaximalsubfield(al, utoipos(d), v);
    4278         133 :   x = gel(subf, 1);
    4279         133 :   polabs = gel(subf, 2);
    4280             : 
    4281             :   /* Frobenius form to obtain L-vector space structure */
    4282         133 :   basis = frobeniusform(al, x);
    4283         133 :   P = gel(basis, 1);
    4284         133 :   Pi = gel(basis, 2);
    4285             : 
    4286             :   /* construct rnf of splitting field */
    4287         133 :   pol = nffactor(nf,polabs);
    4288         133 :   pol = gcoeff(pol,1,1);
    4289         133 :   gel(al,1) = rnf = rnfinit(nf, pol);
    4290             :   /* since pol is irreducible over Q, we have k=0 in rnf. */
    4291         133 :   if (!gequal0(rnf_get_k(rnf)))
    4292             :     pari_err_BUG("computesplitting (k!=0)"); /*LCOV_EXCL_LINE*/
    4293         133 :   gel(al,6) = gen_0;
    4294         133 :   rnf_build_nfabs(rnf, nf_get_prec(nf));
    4295             : 
    4296             :   /* construct splitting data */
    4297         133 :   Lbasis = cgetg(d+1, t_MAT);
    4298         357 :   for (j=j2=1; j<=d; j++, j2+=nd)
    4299         224 :     gel(Lbasis,j) = gel(Pi,j2);
    4300             : 
    4301         133 :   Q = zeromatcopy(d,N);
    4302         133 :   pows = pol_x_powers(nd,v);
    4303         357 :   for (i=j=1; j<=N; j+=nd, i++)
    4304        1085 :   for (j2=0; j2<nd; j2++)
    4305         861 :     gcoeff(Q,i,j+j2) = mkpolmod(gel(pows,j2+1),polabs);
    4306         133 :   Lbasisinv = RgM_mul(Q,P);
    4307             : 
    4308         133 :   gel(al,3) = mkvec3(x,Lbasis,Lbasisinv);
    4309         133 : }
    4310             : 
    4311             : /* assumes that mt defines a central simple algebra over nf */
    4312             : GEN
    4313         161 : alg_csa_table(GEN nf, GEN mt0, long v, long maxord)
    4314             : {
    4315         161 :   pari_sp av = avma;
    4316             :   GEN al, mt;
    4317         161 :   long n, D, d2 = lg(mt0)-1, d = usqrt(d2);
    4318         161 :   dbg_printf(1)("alg_csa_table\n");
    4319             : 
    4320         161 :   nf = checknf(nf);
    4321         161 :   mt = check_relmt(nf,mt0);
    4322         147 :   if (!mt) pari_err_TYPE("alg_csa_table", mt0);
    4323         140 :   n = nf_get_degree(nf);
    4324         140 :   D = n*d2;
    4325         140 :   if (d*d != d2)
    4326           7 :     pari_err_DOMAIN("alg_csa_table","(nonsquare) dimension","!=",stoi(d*d),mt);
    4327             : 
    4328         133 :   al = cgetg(12, t_VEC);
    4329         133 :   gel(al,10) = gen_0; /* must be set first */
    4330         133 :   gel(al,1) = zerovec(12); gmael(al,1,10) = nf;
    4331         133 :   gmael(al,1,1) = gpowgs(pol_x(0), d); /* placeholder before splitting field */
    4332         133 :   gel(al,2) = mt;
    4333         133 :   gel(al,3) = gen_0; /* placeholder */
    4334         133 :   gel(al,4) = gel(al,5) = gen_0; /* TODO Hasse invariants */
    4335         133 :   gel(al,5) = gel(al,6) = gen_0; /* placeholder */
    4336         133 :   gel(al,7) = matid(D);
    4337         133 :   gel(al,8) = matid(D);
    4338         133 :   gel(al,9) = algnatmultable(al,D);
    4339         133 :   gel(al,11)= algtracebasis(al);
    4340         133 :   if (maxord) al = alg_maximal(al);
    4341         133 :   computesplitting(al, d, v);
    4342         133 :   return gerepilecopy(av, al);
    4343             : }
    4344             : 
    4345             : static GEN
    4346       35609 : algtableinit_i(GEN mt0, GEN p)
    4347             : {
    4348             :   GEN al, mt;
    4349             :   long i, n;
    4350             : 
    4351       35609 :   if (p && !signe(p)) p = NULL;
    4352       35609 :   mt = check_mt(mt0,p);
    4353       35609 :   if (!mt) pari_err_TYPE("algtableinit", mt0);
    4354       35609 :   if (!p && !isint1(Q_denom(mt0)))
    4355           7 :     pari_err_DOMAIN("algtableinit", "denominator(mt)", "!=", gen_1, mt0);
    4356       35602 :   n = lg(mt)-1;
    4357       35602 :   al = cgetg(12, t_VEC);
    4358       35602 :   for (i=1; i<=6; i++) gel(al,i) = gen_0;
    4359       35602 :   gel(al,7) = matid(n);
    4360       35602 :   gel(al,8) = matid(n);
    4361       35602 :   gel(al,9) = mt;
    4362       35602 :   gel(al,10) = p? p: gen_0;
    4363       35602 :   gel(al,11)= algtracebasis(al);
    4364       35602 :   return al;
    4365             : }
    4366             : GEN
    4367        4193 : algtableinit(GEN mt0, GEN p)
    4368             : {
    4369        4193 :   pari_sp av = avma;
    4370        4193 :   if (p)
    4371             :   {
    4372        4074 :     if (typ(p) != t_INT) pari_err_TYPE("algtableinit",p);
    4373        4067 :     if (signe(p) && !BPSW_psp(p)) pari_err_PRIME("algtableinit",p);
    4374             :   }
    4375        4172 :   return gerepilecopy(av, algtableinit_i(mt0, p));
    4376             : }
    4377             : 
    4378             : /** REPRESENTATIONS OF GROUPS **/
    4379             : 
    4380             : static GEN
    4381         294 : list_to_regular_rep(GEN elts, long n)
    4382             : {
    4383             :   GEN reg, elts2, g;
    4384             :   long i,j;
    4385         294 :   elts = shallowcopy(elts);
    4386         294 :   gen_sort_inplace(elts, (void*)&vecsmall_lexcmp, &cmp_nodata, NULL);
    4387         294 :   reg = cgetg(n+1, t_VEC);
    4388         294 :   gel(reg,1) = identity_perm(n);
    4389        3857 :   for (i=2; i<=n; i++) {
    4390        3563 :     g = perm_inv(gel(elts,i));
    4391        3563 :     elts2 = cgetg(n+1, t_VEC);
    4392        3563 :     for (j=1; j<=n; j++) gel(elts2,j) = perm_mul(g,gel(elts,j));
    4393        3563 :     gen_sort_inplace(elts2, (void*)&vecsmall_lexcmp, &cmp_nodata, &gel(reg,i));
    4394             :   }
    4395         294 :   return reg;
    4396             : }
    4397             : 
    4398             : static GEN
    4399        3857 : matrix_perm(GEN perm, long n)
    4400             : {
    4401             :   GEN m;
    4402             :   long j;
    4403        3857 :   m = cgetg(n+1, t_MAT);
    4404       78694 :   for (j=1; j<=n; j++) {
    4405       74837 :     gel(m,j) = col_ei(n,perm[j]);
    4406             :   }
    4407        3857 :   return m;
    4408             : }
    4409             : 
    4410             : GEN
    4411         847 : conjclasses_algcenter(GEN cc, GEN p)
    4412             : {
    4413         847 :   GEN mt, elts = gel(cc,1), conjclass = gel(cc,2), rep = gel(cc,3);
    4414         847 :   long i, nbcl = lg(rep)-1, n = lg(elts)-1;
    4415             :   pari_sp av;
    4416             : 
    4417             :   /* multiplication table of the center of Z[G] (class functions) */
    4418         847 :   mt = cgetg(nbcl+1,t_VEC);
    4419         847 :   for (i=1;i<=nbcl;i++) gel(mt,i) = zero_Flm_copy(nbcl,nbcl);
    4420         847 :   av = avma;
    4421       14819 :   for (i=1;i<=n;i++)
    4422             :   {
    4423       13972 :     GEN xi = gel(elts,i), mi = gel(mt,conjclass[i]);
    4424             :     long j;
    4425      563542 :     for (j=1;j<=n;j++)
    4426             :     {
    4427      549570 :       GEN xj = gel(elts,j);
    4428      549570 :       long k = vecsearch(elts, perm_mul(xi,xj), NULL), ck = conjclass[k];
    4429      549570 :       if (rep[ck]==k) ucoeff(mi, ck, conjclass[j])++;
    4430             :     }
    4431       13972 :     set_avma(av);
    4432             :   }
    4433         847 :   for (i=1;i<=nbcl;i++) gel(mt,i) = Flm_to_ZM(gel(mt,i));
    4434         847 :   return algtableinit_i(mt,p);
    4435             : }
    4436             : 
    4437             : GEN
    4438         329 : alggroupcenter(GEN G, GEN p, GEN *pcc)
    4439             : {
    4440         329 :   pari_sp av = avma;
    4441         329 :   GEN cc = group_to_cc(G), al = conjclasses_algcenter(cc, p);
    4442         315 :   if (!pcc) al = gerepilecopy(av,al);
    4443             :   else
    4444           7 :   { *pcc = cc; gerepileall(av,2,&al,pcc); }
    4445         315 :   return al;
    4446             : }
    4447             : 
    4448             : static GEN
    4449         294 : groupelts_algebra(GEN elts, GEN p)
    4450             : {
    4451         294 :   pari_sp av = avma;
    4452             :   GEN mt;
    4453         294 :   long i, n = lg(elts)-1;
    4454         294 :   elts = list_to_regular_rep(elts,n);
    4455         294 :   mt = cgetg(n+1, t_VEC);
    4456         294 :   for (i=1; i<=n; i++) gel(mt,i) = matrix_perm(gel(elts,i),n);
    4457         294 :   return gerepilecopy(av, algtableinit_i(mt,p));
    4458             : }
    4459             : 
    4460             : GEN
    4461         329 : alggroup(GEN gal, GEN p)
    4462             : {
    4463         329 :   GEN elts = checkgroupelts(gal);
    4464         294 :   return groupelts_algebra(elts, p);
    4465             : }
    4466             : 
    4467             : /** MAXIMAL ORDER **/
    4468             : 
    4469             : static GEN
    4470       43547 : mattocol(GEN M, long n)
    4471             : {
    4472       43547 :   GEN C = cgetg(n*n+1, t_COL);
    4473             :   long i,j,ic;
    4474       43547 :   ic = 1;
    4475      850416 :   for (i=1; i<=n; i++)
    4476      806869 :   for (j=1; j<=n; j++, ic++) gel(C,ic) = gcoeff(M,i,j);
    4477       43547 :   return C;
    4478             : }
    4479             : 
    4480             : /* Ip is a lift of a left O/pO-ideal where O is the integral basis of al */
    4481             : static GEN
    4482        3444 : algleftordermodp(GEN al, GEN Ip, GEN p)
    4483             : {
    4484        3444 :   pari_sp av = avma;
    4485             :   GEN I, Ii, M, mt, K, imi, p2;
    4486             :   long n, i;
    4487        3444 :   n = alg_get_absdim(al);
    4488        3444 :   mt = alg_get_multable(al);
    4489        3444 :   p2 = sqri(p);
    4490             : 
    4491        3444 :   I = ZM_hnfmodid(Ip, p);
    4492        3444 :   Ii = ZM_inv(I,NULL);
    4493             : 
    4494        3444 :   M = cgetg(n+1, t_MAT);
    4495       46991 :   for (i=1; i<=n; i++) {
    4496       43547 :     imi = FpM_mul(Ii, FpM_mul(gel(mt,i), I, p2), p2);
    4497       43547 :     imi = ZM_Z_divexact(imi, p);
    4498       43547 :     gel(M,i) = mattocol(imi, n);
    4499             :   }
    4500        3444 :   K = FpM_ker(M, p);
    4501        3444 :   if (lg(K)==1) { set_avma(av); return matid(n); }
    4502        1687 :   K = ZM_hnfmodid(K,p);
    4503             : 
    4504        1687 :   return gerepileupto(av, ZM_Z_div(K,p));
    4505             : }
    4506             : 
    4507             : static GEN
    4508        4725 : alg_ordermodp(GEN al, GEN p)
    4509             : {
    4510             :   GEN alp;
    4511        4725 :   long i, N = alg_get_absdim(al);
    4512        4725 :   alp = cgetg(12, t_VEC);
    4513        4725 :   for (i=1; i<=8; i++) gel(alp,i) = gen_0;
    4514        4725 :   gel(alp,9) = cgetg(N+1, t_VEC);
    4515        4725 :   for (i=1; i<=N; i++) gmael(alp,9,i) = FpM_red(gmael(al,9,i), p);
    4516        4725 :   gel(alp,10) = p;
    4517        4725 :   gel(alp,11) = cgetg(N+1, t_VEC);
    4518        4725 :   for (i=1; i<=N; i++) gmael(alp,11,i) = Fp_red(gmael(al,11,i), p);
    4519             : 
    4520        4725 :   return alp;
    4521             : }
    4522             : 
    4523             : static GEN
    4524        2849 : algpradical_i(GEN al, GEN p, GEN zprad, GEN projs)
    4525             : {
    4526        2849 :   pari_sp av = avma;
    4527        2849 :   GEN alp = alg_ordermodp(al, p), liftrad, projrad, alq, alrad, res, Lalp, radq;
    4528             :   long i;
    4529        2849 :   if (lg(zprad)==1) {
    4530        1757 :     liftrad = NULL;
    4531        1757 :     projrad = NULL;
    4532             :   }
    4533             :   else {
    4534        1092 :     alq = alg_quotient(alp, zprad, 1);
    4535        1092 :     alp = gel(alq,1);
    4536        1092 :     projrad = gel(alq,2);
    4537        1092 :     liftrad = gel(alq,3);
    4538             :   }
    4539             : 
    4540        2849 :   if (projs) {
    4541         329 :     if (projrad) {
    4542          28 :       projs = gcopy(projs);
    4543          84 :       for (i=1; i<lg(projs); i++)
    4544          56 :         gel(projs,i) = FpM_FpC_mul(projrad, gel(projs,i), p);
    4545             :     }
    4546         329 :     Lalp = alg_centralproj(alp, projs, 1);
    4547             : 
    4548         329 :     alrad = cgetg(lg(Lalp),t_VEC);
    4549        1302 :     for (i=1; i<lg(Lalp); i++) {
    4550         973 :       alq = gel(Lalp,i);
    4551         973 :       radq = algradical(gel(alq,1));
    4552         973 :       if (gequal0(radq))
    4553         525 :         gel(alrad,i) = cgetg(1,t_MAT);
    4554             :       else {
    4555         448 :         radq = FpM_mul(gel(alq,3),radq,p);
    4556         448 :         gel(alrad,i) = radq;
    4557             :       }
    4558             :     }
    4559         329 :     alrad = shallowmatconcat(alrad);
    4560         329 :     alrad = FpM_image(alrad,p);
    4561             :   }
    4562        2520 :   else alrad = algradical(alp);
    4563             : 
    4564        2849 :   if (!gequal0(alrad)) {
    4565        2205 :     if (liftrad) alrad = FpM_mul(liftrad, alrad, p);
    4566        2205 :     res = shallowmatconcat(mkvec2(alrad, zprad));
    4567        2205 :     res = FpM_image(res,p);
    4568             :   }
    4569         644 :   else res = lg(zprad)==1 ? gen_0 : zprad;
    4570        2849 :   return gerepilecopy(av, res);
    4571             : }
    4572             : 
    4573             : static GEN
    4574        1876 : algpdecompose0(GEN al, GEN prad, GEN p, GEN projs)
    4575             : {
    4576        1876 :   pari_sp av = avma;
    4577        1876 :   GEN alp, quo, ss, liftm = NULL, projm = NULL, dec, res, I, Lss, deci;
    4578             :   long i, j;
    4579             : 
    4580        1876 :   alp = alg_ordermodp(al, p);
    4581        1876 :   if (!gequal0(prad)) {
    4582        1498 :     quo = alg_quotient(alp, prad, 1);
    4583        1498 :     ss = gel(quo,1);
    4584        1498 :     projm = gel(quo,2);
    4585        1498 :     liftm = gel(quo,3);
    4586             :   }
    4587         378 :   else ss = alp;
    4588             : 
    4589        1876 :   if (projs) {
    4590         273 :     if (projm) {
    4591         868 :       for (i=1; i<lg(projs); i++)
    4592         644 :         gel(projs,i) = FpM_FpC_mul(projm, gel(projs,i), p);
    4593             :     }
    4594         273 :     Lss = alg_centralproj(ss, projs, 1);
    4595             : 
    4596         273 :     dec = cgetg(lg(Lss),t_VEC);
    4597        1127 :     for (i=1; i<lg(Lss); i++) {
    4598         854 :       gel(dec,i) = algsimpledec_ss(gmael(Lss,i,1), 1);
    4599         854 :       deci = gel(dec,i);
    4600        1939 :       for (j=1; j<lg(deci); j++)
    4601        1085 :        gmael(deci,j,3) = FpM_mul(gmael(Lss,i,3), gmael(deci,j,3), p);
    4602             :     }
    4603         273 :     dec = shallowconcat1(dec);
    4604             :   }
    4605        1603 :   else dec = algsimpledec_ss(ss,1);
    4606             : 
    4607        1876 :   res = cgetg(lg(dec),t_VEC);
    4608        5271 :   for (i=1; i<lg(dec); i++) {
    4609        3395 :     I = gmael(dec,i,3);
    4610        3395 :     if (liftm) I = FpM_mul(liftm,I,p);
    4611        3395 :     I = shallowmatconcat(mkvec2(I,prad));
    4612        3395 :     gel(res,i) = I;
    4613             :   }
    4614             : 
    4615        1876 :   return gerepilecopy(av, res);
    4616             : }
    4617             : 
    4618             : /* finds a nontrivial ideal of O/prad or gen_0 if there is none. */
    4619             : static GEN
    4620         714 : algpdecompose_i(GEN al, GEN p, GEN zprad, GEN projs)
    4621             : {
    4622         714 :   pari_sp av = avma;
    4623         714 :   GEN prad = algpradical_i(al,p,zprad,projs);
    4624         714 :   return gerepileupto(av, algpdecompose0(al, prad, p, projs));
    4625             : }
    4626             : 
    4627             : /* ord is assumed to be in hnf wrt the integral basis of al. */
    4628             : /* assumes that alg_get_invbasis(al) is integral. */
    4629             : static GEN
    4630        1687 : alg_change_overorder_shallow(GEN al, GEN ord)
    4631             : {
    4632             :   GEN al2, mt, iord, mtx, den, den2, div;
    4633             :   long i, n;
    4634        1687 :   n = alg_get_absdim(al);
    4635             : 
    4636        1687 :   iord = QM_inv(ord);
    4637        1687 :   al2 = shallowcopy(al);
    4638        1687 :   ord = Q_remove_denom(ord,&den);
    4639             : 
    4640        1687 :   gel(al2,7) = Q_remove_denom(gel(al,7), &den2);
    4641        1687 :   if (den2) div = mulii(den,den2);
    4642         644 :   else      div = den;
    4643        1687 :   gel(al2,7) = ZM_Z_div(ZM_mul(gel(al2,7), ord), div);
    4644             : 
    4645        1687 :   gel(al2,8) = ZM_mul(iord, gel(al,8));
    4646             : 
    4647        1687 :   mt = cgetg(n+1,t_VEC);
    4648        1687 :   gel(mt,1) = matid(n);
    4649        1687 :   div = sqri(den);
    4650       18970 :   for (i=2; i<=n; i++) {
    4651       17283 :     mtx = algbasismultable(al,gel(ord,i));
    4652       17283 :     gel(mt,i) = ZM_mul(iord, ZM_mul(mtx, ord));
    4653       17283 :     gel(mt,i) = ZM_Z_divexact(gel(mt,i), div);
    4654             :   }
    4655        1687 :   gel(al2,9) = mt;
    4656             : 
    4657        1687 :   gel(al2,11) = algtracebasis(al2);
    4658             : 
    4659        1687 :   return al2;
    4660             : }
    4661             : 
    4662             : static GEN
    4663        9590 : algfromcenter(GEN al, GEN x)
    4664             : {
    4665        9590 :   GEN nf = alg_get_center(al);
    4666             :   long n;
    4667        9590 :   switch(alg_type(al)) {
    4668             :     case al_CYCLIC:
    4669        8498 :       n = alg_get_degree(al);
    4670        8498 :       break;
    4671             :     case al_CSA:
    4672        1092 :       n = alg_get_dim(al);
    4673        1092 :       break;
    4674             :     default:
    4675             :       return NULL; /*LCOV_EXCL_LINE*/
    4676             :   }
    4677        9590 :   return algalgtobasis(al, scalarcol(basistoalg(nf, x), n));
    4678             : }
    4679             : 
    4680             : /* x is an ideal of the center in hnf form */
    4681             : static GEN
    4682        2849 : algfromcenterhnf(GEN al, GEN x)
    4683             : {
    4684             :   GEN res;
    4685             :   long i;
    4686        2849 :   res = cgetg(lg(x), t_MAT);
    4687        2849 :   for (i=1; i<lg(x); i++) gel(res,i) = algfromcenter(al, gel(x,i));
    4688        2849 :   return res;
    4689             : }
    4690             : 
    4691             : /* assumes al is CSA or CYCLIC */
    4692             : static GEN
    4693        1162 : algcenter_precompute(GEN al, GEN p)
    4694             : {
    4695        1162 :   GEN fa, pdec, nfprad, projs, nf = alg_get_center(al);
    4696             :   long i, np;
    4697             : 
    4698        1162 :   pdec = idealprimedec(nf, p);
    4699        1162 :   settyp(pdec, t_COL);
    4700        1162 :   np = lg(pdec)-1;
    4701        1162 :   fa = mkmat2(pdec, const_col(np, gen_1));
    4702        1162 :   if (dvdii(nf_get_disc(nf), p))
    4703         329 :     nfprad = idealprodprime(nf, pdec);
    4704             :   else
    4705         833 :     nfprad = scalarmat_shallow(p, nf_get_degree(nf));
    4706        1162 :   fa = idealchineseinit(nf, fa);
    4707        1162 :   projs = cgetg(np+1, t_VEC);
    4708        1162 :   for (i=1; i<=np; i++) gel(projs, i) = idealchinese(nf, fa, vec_ei(np,i));
    4709        1162 :   return mkvec2(nfprad, projs);
    4710             : }
    4711             : 
    4712             : static GEN
    4713        2849 : algcenter_prad(GEN al, GEN p, GEN pre)
    4714             : {
    4715             :   GEN nfprad, zprad, mtprad;
    4716             :   long i;
    4717        2849 :   nfprad = gel(pre,1);
    4718        2849 :   zprad = algfromcenterhnf(al, nfprad);
    4719        2849 :   zprad = FpM_image(zprad, p);
    4720        2849 :   mtprad = cgetg(lg(zprad), t_VEC);
    4721        2849 :   for (i=1; i<lg(zprad); i++) gel(mtprad, i) = algbasismultable(al, gel(zprad,i));
    4722        2849 :   mtprad = shallowmatconcat(mtprad);
    4723        2849 :   zprad = FpM_image(mtprad, p);
    4724        2849 :   return zprad;
    4725             : }
    4726             : 
    4727             : static GEN
    4728        2849 : algcenter_p_projs(GEN al, GEN p, GEN pre)
    4729             : {
    4730             :   GEN projs, zprojs;
    4731             :   long i;
    4732        2849 :   projs = gel(pre,2);
    4733        2849 :   zprojs = cgetg(lg(projs), t_VEC);
    4734        2849 :   for (i=1; i<lg(projs); i++) gel(zprojs,i) = FpC_red(algfromcenter(al, gel(projs,i)),p);
    4735        2849 :   return zprojs;
    4736             : }
    4737             : 
    4738             : /* al is assumed to be simple */
    4739             : static GEN
    4740        1162 : alg_pmaximal(GEN al, GEN p)
    4741             : {
    4742        1162 :   GEN al2, prad, lord = gen_0, I, id, dec, zprad, projs, pre;
    4743             :   long n, i;
    4744        1162 :   n = alg_get_absdim(al);
    4745        1162 :   id = matid(n);
    4746        1162 :   al2 = al;
    4747             : 
    4748        1162 :   dbg_printf(0)("Round 2 (non-commutative) at p=%Ps, dim=%d\n", p, n);
    4749             : 
    4750        1162 :   pre = algcenter_precompute(al,p);
    4751             : 
    4752             :   while (1) {
    4753        3108 :     zprad = algcenter_prad(al2, p, pre);
    4754        2135 :     projs = algcenter_p_projs(al2, p, pre);
    4755        2135 :     if (lg(projs) == 2) projs = NULL;
    4756        2135 :     prad = algpradical_i(al2,p,zprad,projs);
    4757        2135 :     if (typ(prad) == t_INT) break;
    4758        2114 :     lord = algleftordermodp(al2,prad,p);
    4759        2114 :     if (!cmp_universal(lord,id)) break;
    4760         973 :     al2 = alg_change_overorder_shallow(al2,lord);
    4761             :   }
    4762             : 
    4763        1162 :   dec = algpdecompose0(al2,prad,p,projs);
    4764        3038 :   while (lg(dec)>2) {
    4765        1498 :     for (i=1; i<lg(dec); i++) {
    4766        1330 :       I = gel(dec,i);
    4767        1330 :       lord = algleftordermodp(al2,I,p);
    4768        1330 :       if (cmp_universal(lord,matid(n))) break;
    4769             :     }
    4770         882 :     if (i==lg(dec)) break;
    4771         714 :     al2 = alg_change_overorder_shallow(al2,lord);
    4772         714 :     zprad = algcenter_prad(al2, p, pre);
    4773         714 :     projs = algcenter_p_projs(al2, p, pre);
    4774         714 :     if (lg(projs) == 2) projs = NULL;
    4775         714 :     dec = algpdecompose_i(al2,p,zprad,projs);
    4776             :   }
    4777        1162 :   return al2;
    4778             : }
    4779             : 
    4780             : static GEN
    4781        5117 : algtracematrix(GEN al)
    4782             : {
    4783             :   GEN M, mt;
    4784             :   long n, i, j;
    4785        5117 :   n = alg_get_absdim(al);
    4786        5117 :   mt = alg_get_multable(al);
    4787        5117 :   M = cgetg(n+1, t_MAT);
    4788       41314 :   for (i=1; i<=n; i++)
    4789             :   {
    4790       36197 :     gel(M,i) = cgetg(n+1,t_MAT);
    4791      267869 :     for (j=1; j<=i; j++)
    4792      231672 :       gcoeff(M,j,i) = gcoeff(M,i,j) = algabstrace(al,gmael(mt,i,j));
    4793             :   }
    4794        5117 :   return M;
    4795             : }
    4796             : static GEN
    4797         133 : algdisc_i(GEN al) { return ZM_det(algtracematrix(al)); }
    4798             : GEN
    4799           7 : algdisc(GEN al)
    4800             : {
    4801           7 :   pari_sp av = avma;
    4802           7 :   checkalg(al); return gerepileuptoint(av, algdisc_i(al));
    4803             : }
    4804             : static GEN
    4805         126 : alg_maximal(GEN al)
    4806             : {
    4807         126 :   GEN fa = absZ_factor(algdisc_i(al));
    4808         126 :   return alg_maximal_primes(al, gel(fa,1));
    4809             : }
    4810             : 
    4811             : /** LATTICES **/
    4812             : 
    4813             : /*
    4814             :  Convention: lattice = [I,t] representing t*I, where
    4815             :  - I integral nonsingular upper-triangular matrix representing a lattice over
    4816             :    the integral basis of the algebra, and
    4817             :  - t>0 either an integer or a rational number.
    4818             : 
    4819             :  Recommended and returned by the functions below:
    4820             :  - I HNF and primitive
    4821             : */
    4822             : 
    4823             : /* TODO use hnfmodid whenever possible using a*O <= I <= O
    4824             :  * for instance a = ZM_det_triangular(I) */
    4825             : 
    4826             : static GEN
    4827       63343 : primlat(GEN lat)
    4828             : {
    4829             :   GEN m, t, c;
    4830       63343 :   m = alglat_get_primbasis(lat);
    4831       63343 :   t = alglat_get_scalar(lat);
    4832       63343 :   m = Q_primitive_part(m,&c);
    4833       63343 :   if (c) return mkvec2(m,gmul(t,c));
    4834       53760 :   return lat;
    4835             : }
    4836             : 
    4837             : /* assumes the lattice contains d * integral basis, d=0 allowed */
    4838             : GEN
    4839       51065 : alglathnf(GEN al, GEN m, GEN d)
    4840             : {
    4841       51065 :   pari_sp av = avma;
    4842             :   long N,i,j;
    4843             :   GEN m2, c;
    4844       51065 :   checkalg(al);
    4845       51065 :   N = alg_get_absdim(al);
    4846       51065 :   if (!d) d = gen_0;
    4847       51065 :   if (typ(m) == t_VEC) m = matconcat(m);
    4848       51065 :   if (typ(m) == t_COL) m = algleftmultable(al,m);
    4849       51065 :   if (typ(m) != t_MAT) pari_err_TYPE("alglathnf",m);
    4850       51058 :   if (typ(d) != t_FRAC && typ(d) != t_INT) pari_err_TYPE("alglathnf",d);
    4851       51058 :   if (lg(m)-1 < N || lg(gel(m,1))-1 != N) pari_err_DIM("alglathnf");
    4852      459242 :   for (i=1; i<=N; i++)
    4853     6820758 :     for (j=1; j<lg(m); j++)
    4854     6412546 :       if (typ(gcoeff(m,i,j)) != t_FRAC && typ(gcoeff(m,i,j)) != t_INT)
    4855           7 :         pari_err_TYPE("alglathnf", gcoeff(m,i,j));
    4856       51023 :   m2 = Q_primitive_part(m,&c);
    4857       51023 :   if (!c) c = gen_1;
    4858       51023 :   if (!signe(d)) d = detint(m2);
    4859       45593 :   else           d = gdiv(d,c); /* should be an integer */
    4860       51023 :   if (!signe(d)) pari_err_INV("alglathnf [m does not have full rank]", m2);
    4861       51009 :   m2 = ZM_hnfmodid(m2,d);
    4862       51009 :   return gerepilecopy(av, mkvec2(m2,c));
    4863             : }
    4864             : 
    4865             : static GEN
    4866       10689 : prepare_multipliers(GEN *a, GEN *b)
    4867             : {
    4868             :   GEN na, nb, da, db, d;
    4869       10689 :   na = numer_i(*a); da = denom_i(*a);
    4870       10689 :   nb = numer_i(*b); db = denom_i(*b);
    4871       10689 :   na = mulii(na,db);
    4872       10689 :   nb = mulii(nb,da);
    4873       10689 :   d = gcdii(na,nb);
    4874       10689 :   *a = diviiexact(na,d);
    4875       10689 :   *b = diviiexact(nb,d);
    4876       10689 :   return gdiv(d, mulii(da,db));
    4877             : }
    4878             : 
    4879             : static GEN
    4880       10689 : prepare_lat(GEN m1, GEN t1, GEN m2, GEN t2)
    4881             : {
    4882       10689 :   GEN d = prepare_multipliers(&t1, &t2);
    4883       10689 :   m1 = ZM_Z_mul(m1,t1);
    4884       10689 :   m2 = ZM_Z_mul(m2,t2);
    4885       10689 :   return mkvec3(m1,m2,d);
    4886             : }
    4887             : 
    4888             : static GEN
    4889       10689 : alglataddinter(GEN al, GEN lat1, GEN lat2, GEN *sum, GEN *inter)
    4890             : {
    4891             :   GEN d, m1, m2, t1, t2, M, prep, d1, d2, ds, di, K;
    4892       10689 :   checkalg(al);
    4893       10689 :   checklat(al,lat1);
    4894       10689 :   checklat(al,lat2);
    4895             : 
    4896       10689 :   m1 = alglat_get_primbasis(lat1);
    4897       10689 :   t1 = alglat_get_scalar(lat1);
    4898       10689 :   m2 = alglat_get_primbasis(lat2);
    4899       10689 :   t2 = alglat_get_scalar(lat2);
    4900       10689 :   prep = prepare_lat(m1, t1, m2, t2);
    4901       10689 :   m1 = gel(prep,1);
    4902       10689 :   m2 = gel(prep,2);
    4903       10689 :   d = gel(prep,3);
    4904       10689 :   M = matconcat(mkvec2(m1,m2));
    4905       10689 :   d1 = ZM_det_triangular(m1);
    4906       10689 :   d2 = ZM_det_triangular(m2);
    4907       10689 :   ds = gcdii(d1,d2);
    4908       10689 :   if (inter)
    4909             :   {
    4910        7112 :     di = diviiexact(mulii(d1,d2),ds);
    4911        7112 :     K = matkermod(M,di,sum);
    4912        7112 :     K = rowslice(K,1,lg(m1));
    4913        7112 :     *inter = hnfmodid(FpM_mul(m1,K,di),di);
    4914        7112 :     if (sum) *sum = hnfmodid(*sum,ds);
    4915             :   }
    4916        3577 :   else *sum = hnfmodid(M,ds);
    4917       10689 :   return d;
    4918             : }
    4919             : 
    4920             : GEN
    4921        3598 : alglatinter(GEN al, GEN lat1, GEN lat2, GEN* ptsum)
    4922             : {
    4923        3598 :   pari_sp av = avma;
    4924             :   GEN inter, d;
    4925        3598 :   d = alglataddinter(al, lat1, lat2, ptsum, &inter);
    4926        3598 :   inter = primlat(mkvec2(inter, d));
    4927        3598 :   if (ptsum)
    4928             :   {
    4929          14 :     *ptsum = primlat(mkvec2(*ptsum,d));
    4930          14 :     gerepileall(av, 2, &inter, ptsum);
    4931             :   }
    4932        3584 :   else inter = gerepilecopy(av, inter);
    4933        3598 :   return inter;
    4934             : }
    4935             : 
    4936             : GEN
    4937        7091 : alglatadd(GEN al, GEN lat1, GEN lat2, GEN* ptinter)
    4938             : {
    4939        7091 :   pari_sp av = avma;
    4940             :   GEN sum, d;
    4941        7091 :   d = alglataddinter(al, lat1, lat2, &sum, ptinter);
    4942        7091 :   sum = primlat(mkvec2(sum, d));
    4943        7091 :   if (ptinter)
    4944             :   {
    4945        3514 :     *ptinter = primlat(mkvec2(*ptinter,d));
    4946        3514 :     gerepileall(av, 2, &sum, ptinter);
    4947             :   }
    4948        3577 :   else sum = gerepilecopy(av, sum);
    4949        7091 :   return sum;
    4950             : }
    4951             : 
    4952             : int
    4953       31549 : alglatsubset(GEN al, GEN lat1, GEN lat2, GEN* ptindex)
    4954             : {
    4955             :   /* TODO version that returns the quotient as abelian group? */
    4956             :   /* return matrices to convert coordinates from one to other? */
    4957       31549 :   pari_sp av = avma;
    4958             :   int res;
    4959             :   GEN m1, m2, m2i, m, t;
    4960       31549 :   checkalg(al);
    4961       31549 :   checklat(al,lat1);
    4962       31549 :   checklat(al,lat2);
    4963       31549 :   m1 = alglat_get_primbasis(lat1);
    4964       31549 :   m2 = alglat_get_primbasis(lat2);
    4965       31549 :   m2i = RgM_inv_upper(m2);
    4966       31549 :   t = gdiv(alglat_get_scalar(lat1), alglat_get_scalar(lat2));
    4967       31549 :   m = RgM_Rg_mul(RgM_mul(m2i,m1), t);
    4968       31549 :   res = RgM_is_ZM(m);
    4969       31549 :   if (res && ptindex)
    4970             :   {
    4971        1757 :     *ptindex = mpabs(ZM_det_triangular(m));
    4972        1757 :     gerepileall(av,1,ptindex);
    4973             :   }
    4974       29792 :   else set_avma(av);
    4975       31549 :   return res;
    4976             : }
    4977             : 
    4978             : GEN
    4979        5264 : alglatindex(GEN al, GEN lat1, GEN lat2)
    4980             : {
    4981        5264 :   pari_sp av = avma;
    4982             :   long N;
    4983             :   GEN res;
    4984        5264 :   checkalg(al);
    4985        5264 :   checklat(al,lat1);
    4986        5264 :   checklat(al,lat2);
    4987        5264 :   N = alg_get_absdim(al);
    4988        5264 :   res = alglat_get_scalar(lat1);
    4989        5264 :   res = gdiv(res, alglat_get_scalar(lat2));
    4990        5264 :   res = gpowgs(res, N);
    4991        5264 :   res = gmul(res,RgM_det_triangular(alglat_get_primbasis(lat1)));
    4992        5264 :   res = gdiv(res, RgM_det_triangular(alglat_get_primbasis(lat2)));
    4993        5264 :   res = gabs(res,0);
    4994        5264 :   return gerepilecopy(av, res);
    4995             : }
    4996             : 
    4997             : GEN
    4998       45605 : alglatmul(GEN al, GEN lat1, GEN lat2)
    4999             : {
    5000       45605 :   pari_sp av = avma;
    5001             :   long N,i;
    5002             :   GEN m1, m2, m, V, lat, t, d, dp;
    5003       45605 :   checkalg(al);
    5004       45605 :   if (typ(lat1)==t_COL)
    5005             :   {
    5006       19292 :     if (typ(lat2)==t_COL)
    5007           7 :       pari_err_TYPE("alglatmul [one of lat1, lat2 has to be a lattice]", lat2);
    5008       19285 :     checklat(al,lat2);
    5009       19285 :     lat1 = Q_remove_denom(lat1,&d);
    5010       19285 :     m = algbasismultable(al,lat1);
    5011       19285 :     m2 = alglat_get_primbasis(lat2);
    5012       19285 :     dp = mulii(detint(m),ZM_det_triangular(m2));
    5013       19285 :     m = ZM_mul(m,m2);
    5014       19285 :     t = alglat_get_scalar(lat2);
    5015       19285 :     if (d) t = gdiv(t,d);
    5016             :   }
    5017             :   else /* typ(lat1)!=t_COL */
    5018             :   {
    5019       26313 :     checklat(al,lat1);
    5020       26313 :     if (typ(lat2)==t_COL)
    5021             :     {
    5022       19285 :       lat2 = Q_remove_denom(lat2,&d);
    5023       19285 :       m = algbasisrightmultable(al,lat2);
    5024       19285 :       m1 = alglat_get_primbasis(lat1);
    5025       19285 :       dp = mulii(detint(m),ZM_det_triangular(m1));
    5026       19285 :       m = ZM_mul(m,m1);
    5027       19285 :       t = alglat_get_scalar(lat1);
    5028       19285 :       if (d) t = gdiv(t,d);
    5029             :     }
    5030             :     else /* typ(lat2)!=t_COL */
    5031             :     {
    5032        7028 :       checklat(al,lat2);
    5033        7021 :       N = alg_get_absdim(al);
    5034        7021 :       m1 = alglat_get_primbasis(lat1);
    5035        7021 :       m2 = alglat_get_primbasis(lat2);
    5036        7021 :       dp = mulii(ZM_det_triangular(m1), ZM_det_triangular(m2));
    5037        7021 :       V = cgetg(N+1,t_VEC);
    5038       63189 :       for (i=1; i<=N; i++) {
    5039       56168 :         gel(V,i) = algbasismultable(al,gel(m1,i));
    5040       56168 :         gel(V,i) = ZM_mul(gel(V,i),m2);
    5041             :       }
    5042        7021 :       m = matconcat(V);
    5043        7021 :       t = gmul(alglat_get_scalar(lat1), alglat_get_scalar(lat2));
    5044             :     }
    5045             :   }
    5046             : 
    5047       45591 :   lat = alglathnf(al,m,dp);
    5048       45591 :   gel(lat,2) = gmul(alglat_get_scalar(lat), t);
    5049       45591 :   lat = primlat(lat);
    5050       45591 :   return gerepilecopy(av, lat);
    5051             : }
    5052             : 
    5053             : int
    5054       17521 : alglatcontains(GEN al, GEN lat, GEN x, GEN *ptc)
    5055             : {
    5056       17521 :   pari_sp av = avma;
    5057             :   GEN m, t, sol;
    5058       17521 :   checkalg(al);
    5059       17521 :   checklat(al,lat);
    5060       17521 :   m = alglat_get_primbasis(lat);
    5061       17521 :   t = alglat_get_scalar(lat);
    5062       17521 :   x = RgC_Rg_div(x,t);
    5063       17521 :   if (!RgV_is_ZV(x)) return gc_bool(av,0);
    5064       17521 :   sol = hnf_solve(m,x);
    5065       17521 :   if (!sol) return gc_bool(av,0);
    5066        8771 :   if (!ptc) return gc_bool(av,1);
    5067        8764 :   *ptc = sol; gerepileall(av,1,ptc); return 1;
    5068             : }
    5069             : 
    5070             : GEN
    5071        8771 : alglatelement(GEN al, GEN lat, GEN c)
    5072             : {
    5073        8771 :   pari_sp av = avma;
    5074             :   GEN res;
    5075        8771 :   checkalg(al);
    5076        8771 :   checklat(al,lat);
    5077        8771 :   if (typ(c)!=t_COL) pari_err_TYPE("alglatelement", c);
    5078        8764 :   res = ZM_ZC_mul(alglat_get_primbasis(lat),c);
    5079        8764 :   res = RgC_Rg_mul(res, alglat_get_scalar(lat));
    5080        8764 :   return gerepilecopy(av,res);
    5081             : }
    5082             : 
    5083             : /* idem QM_invimZ, knowing result is contained in 1/c*Z^n */
    5084             : static GEN
    5085        3535 : QM_invimZ_mod(GEN m, GEN c)
    5086             : {
    5087             :   GEN d, m0, K;
    5088        3535 :   m0 = Q_remove_denom(m, &d);
    5089        3535 :   if (d)    d = mulii(d,c);
    5090          21 :   else      d = c;
    5091        3535 :   K = matkermod(m0, d, NULL);
    5092        3535 :   if (lg(K)==1) K = scalarmat(d, lg(m)-1);
    5093        3521 :   else          K = hnfmodid(K, d);
    5094        3535 :   return RgM_Rg_div(K,c);
    5095             : }
    5096             : 
    5097             : /* If m is injective, computes a Z-basis of the submodule of elements whose
    5098             :  * image under m is integral */
    5099             : static GEN
    5100          14 : QM_invimZ(GEN m)
    5101             : {
    5102          14 :   return RgM_invimage(m, QM_ImQ_hnf(m));
    5103             : }
    5104             : 
    5105             : /* An isomorphism of R-modules M_{m,n}(R) -> R^{m*n} */
    5106             : static GEN
    5107       28322 : mat2col(GEN M, long m, long n)
    5108             : {
    5109             :   long i,j,k,p;
    5110             :   GEN C;
    5111       28322 :   p = m*n;
    5112       28322 :   C = cgetg(p+1,t_COL);
    5113      254702 :   for (i=1,k=1;i<=m;i++)
    5114     2036804 :     for (j=1;j<=n;j++,k++)
    5115     1810424 :       gel(C,k) = gcoeff(M,i,j);
    5116       28322 :   return C;
    5117             : }
    5118             : 
    5119             : static GEN
    5120        3535 : alglattransporter_i(GEN al, GEN lat1, GEN lat2, long right)
    5121             : {
    5122             :   GEN m1, m2, m2i, M, MT, mt, t1, t2, T, c;
    5123             :   long N, i;
    5124        3535 :   N = alg_get_absdim(al);
    5125        3535 :   m1 = alglat_get_primbasis(lat1);
    5126        3535 :   m2 = alglat_get_primbasis(lat2);
    5127        3535 :   m2i = RgM_inv_upper(m2);
    5128        3535 :   c = detint(m1);
    5129        3535 :   t1 = alglat_get_scalar(lat1);
    5130        3535 :   m1 = RgM_Rg_mul(m1,t1);
    5131        3535 :   t2 = alglat_get_scalar(lat2);
    5132        3535 :   m2i = RgM_Rg_div(m2i,t2);
    5133             : 
    5134        3535 :   MT = right? NULL: alg_get_multable(al);
    5135        3535 :   M = cgetg(N+1, t_MAT);
    5136       31815 :   for (i=1; i<=N; i++) {
    5137       28280 :     if (right) mt = algbasisrightmultable(al, vec_ei(N,i));
    5138       14168 :     else       mt = gel(MT,i);
    5139       28280 :     mt = RgM_mul(m2i,mt);
    5140       28280 :     mt = RgM_mul(mt,m1);
    5141       28280 :     gel(M,i) = mat2col(mt, N, N);
    5142             :   }
    5143             : 
    5144        3535 :   c = gdiv(t2,gmul(c,t1));
    5145        3535 :   c = denom_i(c);
    5146        3535 :   T = QM_invimZ_mod(M,c);
    5147        3535 :   return primlat(mkvec2(T,gen_1));
    5148             : }
    5149             : 
    5150             : /*
    5151             :    { x in al | x*lat1 subset lat2}
    5152             : */
    5153             : GEN
    5154        1771 : alglatlefttransporter(GEN al, GEN lat1, GEN lat2)
    5155             : {
    5156        1771 :   pari_sp av = avma;
    5157        1771 :   checkalg(al);
    5158        1771 :   checklat(al,lat1);
    5159        1771 :   checklat(al,lat2);
    5160        1771 :   return gerepilecopy(av, alglattransporter_i(al,lat1,lat2,0));
    5161             : }
    5162             : 
    5163             : /*
    5164             :    { x in al | lat1*x subset lat2}
    5165             : */
    5166             : GEN
    5167        1764 : alglatrighttransporter(GEN al, GEN lat1, GEN lat2)
    5168             : {
    5169        1764 :   pari_sp av = avma;
    5170        1764 :   checkalg(al);
    5171        1764 :   checklat(al,lat1);
    5172        1764 :   checklat(al,lat2);
    5173        1764 :   return gerepilecopy(av, alglattransporter_i(al,lat1,lat2,1));
    5174             : }
    5175             : 
    5176             : GEN
    5177          42 : algmakeintegral(GEN mt0, long maps)
    5178             : {
    5179          42 :   pari_sp av = avma;
    5180             :   long n,i;
    5181             :   GEN m,P,Pi,mt2,mt;
    5182          42 :   n = lg(mt0)-1;
    5183          42 :   mt = check_mt(mt0,NULL);
    5184          42 :   if (!mt) pari_err_TYPE("algmakeintegral", mt0);
    5185          21 :   if (isint1(Q_denom(mt0))) {
    5186           7 :     if (maps) mt = mkvec3(mt,matid(n),matid(n));
    5187           7 :     return gerepilecopy(av,mt);
    5188             :   }
    5189          14 :   dbg_printf(2)(" algmakeintegral: dim=%d, denom=%Ps\n", n, Q_denom(mt0));
    5190          14 :   m = cgetg(n+1,t_MAT);
    5191          56 :   for (i=1;i<=n;i++)
    5192          42 :     gel(m,i) = mat2col(gel(mt,i),n,n);
    5193          14 :   dbg_printf(2)(" computing order, dims m = %d x %d...\n", nbrows(m), lg(m)-1);
    5194          14 :   P = QM_invimZ(m);
    5195          14 :   dbg_printf(2)(" ...done.\n");
    5196          14 :   P = shallowmatconcat(mkvec2(col_ei(n,1),P));
    5197          14 :   P = hnf(P);
    5198          14 :   Pi = RgM_inv(P);
    5199          14 :   mt2 = change_Rgmultable(mt,P,Pi);
    5200          14 :   if (maps) mt2 = mkvec3(mt2,Pi,P); /* mt2, mt->mt2, mt2->mt */
    5201          14 :   return gerepilecopy(av,mt2);
    5202             : }
    5203             : 
    5204             : /** ORDERS **/
    5205             : 
    5206             : /** IDEALS **/
    5207             : 

Generated by: LCOV version 1.13