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.14.0 lcov report (development 27775-aca467eab2) Lines: 2982 3085 96.7 %
Date: 2022-07-03 07:33:15 Functions: 266 271 98.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13