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

Generated by: LCOV version 1.13