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

Generated by: LCOV version 1.13