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.18.1 lcov report (development 30288-703288f808) Lines: 3960 3986 99.3 %
Date: 2025-05-19 09:23:07 Functions: 344 345 99.7 %
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 algbasismul(GEN al, GEN x, GEN y);
      40             : static GEN algbasismultable(GEN al, GEN x);
      41             : static GEN algbasismultable_Flm(GEN mt, GEN x, ulong m);
      42             : static GEN algeltfromnf_i(GEN al, GEN x);
      43             : static void computesplitting(GEN al, long d, long v, long flag);
      44             : static GEN alg_change_overorder_shallow(GEN al, GEN ord);
      45             : 
      46             : static GEN H_inv(GEN x);
      47             : static GEN H_norm(GEN x, long abs);
      48             : static GEN H_trace(GEN x, long abs);
      49             : 
      50             : static int
      51     1411465 : checkalg_i(GEN al)
      52             : {
      53             :   GEN mt, rnf;
      54             :   long t;
      55     1411465 :   if (typ(al) != t_VEC || lg(al) != 12) return 0;
      56     1411136 :   mt = alg_get_multable(al);
      57     1411136 :   if (typ(mt) != t_VEC || lg(mt) == 1 || typ(gel(mt,1)) != t_MAT) return 0;
      58     1411115 :   rnf = alg_get_splittingfield(al);
      59     1411115 :   if (isintzero(rnf) || !gequal0(alg_get_char(al)))
      60      543933 :     return 1;
      61      867182 :   if (typ(gel(al,2)) != t_VEC || lg(gel(al,2)) == 1) return 0;
      62             :   /* not checkrnf_i: beware placeholder from alg_csa_table */
      63      867175 :   t = typ(rnf);
      64      867175 :   return t==t_COMPLEX || t==t_REAL || (t==t_VEC && lg(rnf)==13);
      65             : }
      66             : void
      67     1538809 : checkalg(GEN al)
      68             : {
      69     1538809 :   if (al && !checkalg_i(al))
      70         203 :     pari_err_TYPE("checkalg [please apply alginit()]",al);
      71     1538606 : }
      72             : 
      73             : static int
      74      186858 : checklat_i(GEN al, GEN lat)
      75             : {
      76             :   long N,i,j;
      77             :   GEN m,t,c;
      78      186858 :   if (typ(lat)!=t_VEC || lg(lat) != 3) return 0;
      79      186858 :   t = gel(lat,2);
      80      186858 :   if (typ(t) != t_INT && typ(t) != t_FRAC) return 0;
      81      186858 :   if (gsigne(t)<=0) return 0;
      82      186858 :   m = gel(lat,1);
      83      186858 :   if (typ(m) != t_MAT) return 0;
      84      186858 :   N = alg_get_absdim(al);
      85      186858 :   if (lg(m)-1 != N || lg(gel(m,1))-1 != N) return 0;
      86     1686020 :   for (i=1; i<=N; i++)
      87    14154399 :     for (j=1; j<=N; j++) {
      88    12655237 :       c = gcoeff(m,i,j);
      89    12655237 :       if (typ(c) != t_INT) return 0;
      90    12655237 :       if (j<i && signe(gcoeff(m,i,j))) return 0;
      91    12655237 :       if (i==j && !signe(gcoeff(m,i,j))) return 0;
      92             :     }
      93      186851 :   return 1;
      94             : }
      95      186858 : void checklat(GEN al, GEN lat)
      96      186858 : { if (!checklat_i(al,lat)) pari_err_TYPE("checklat [please apply alglathnf()]", lat); }
      97             : 
      98             : /**  ACCESSORS  **/
      99             : long
     100    10007257 : alg_type(GEN al)
     101             : {
     102             :   long t;
     103    10007257 :   if (!al) return al_REAL;
     104     9877064 :   t = typ(alg_get_splittingfield(al));
     105     9877064 :   if (t==t_REAL || t==t_COMPLEX) return al_REAL;
     106     9873543 :   if (isintzero(alg_get_splittingfield(al)) || !gequal0(alg_get_char(al))) return al_TABLE;
     107     7704166 :   switch(typ(gmael(al,2,1))) {
     108     1691116 :     case t_MAT: return al_CSA;
     109     6013015 :     case t_INT:
     110             :     case t_FRAC:
     111             :     case t_POL:
     112     6013015 :     case t_POLMOD: return al_CYCLIC;
     113          35 :     default: return al_NULL;
     114             :   }
     115             :   return -1; /*LCOV_EXCL_LINE*/
     116             : }
     117             : long
     118        2254 : algtype(GEN al)
     119        2254 : { return checkalg_i(al)? alg_type(al): al_NULL; }
     120             : 
     121             : static long /* is a square special case? */
     122        1183 : alg_is_asq(GEN al) { return typ(gmael(al,6,1)) == t_VEC; }
     123             : 
     124             : /* absdim == dim for al_TABLE. */
     125             : static long
     126         259 : algreal_dim(GEN al)
     127             : {
     128         259 :   switch(lg(alg_get_multable(al))) {
     129         161 :     case 2: case 3: return 1;
     130          91 :     case 5: return 4;
     131           7 :     default: pari_err_TYPE("algreal_dim", al);
     132             :   }
     133             :   return -1; /*LCOV_EXCL_LINE*/
     134             : }
     135             : long
     136      410673 : alg_get_dim(GEN al)
     137             : {
     138             :   long d;
     139      410673 :   if (!al) return 4;
     140      410673 :   switch(alg_type(al)) {
     141       19478 :     case al_TABLE: return lg(alg_get_multable(al))-1;
     142      391083 :     case al_CSA: return lg(alg_get_relmultable(al))-1;
     143          77 :     case al_CYCLIC: d = alg_get_degree(al); return d*d;
     144          28 :     case al_REAL: return algreal_dim(al);
     145           7 :     default: pari_err_TYPE("alg_get_dim", al);
     146             :   }
     147             :   return -1; /*LCOV_EXCL_LINE*/
     148             : }
     149             : 
     150             : long
     151     2562139 : alg_get_absdim(GEN al)
     152             : {
     153     2562139 :   if (!al) return 4;
     154     2515421 :   switch(alg_type(al)) {
     155      924099 :     case al_TABLE: case al_REAL: return lg(alg_get_multable(al))-1;
     156      198884 :     case al_CSA: return alg_get_dim(al)*nf_get_degree(alg_get_center(al));
     157     1392431 :     case al_CYCLIC:
     158     1392431 :       return rnf_get_absdegree(alg_get_splittingfield(al))*alg_get_degree(al);
     159           7 :     default: pari_err_TYPE("alg_get_absdim", al);
     160             :   }
     161             :   return -1;/*LCOV_EXCL_LINE*/
     162             : }
     163             : 
     164             : long
     165        6216 : algdim(GEN al, long abs)
     166             : {
     167        6216 :   checkalg(al);
     168        6195 :   if (abs) return alg_get_absdim(al);
     169        5551 :   return alg_get_dim(al);
     170             : }
     171             : 
     172             : /* only cyclic */
     173             : GEN
     174       20937 : alg_get_auts(GEN al)
     175             : {
     176       20937 :   long ta = alg_type(al);
     177       20937 :   if (ta != al_CYCLIC && ta != al_REAL)
     178           0 :     pari_err_TYPE("alg_get_auts [noncyclic algebra]", al);
     179       20937 :   return gel(al,2);
     180             : }
     181             : GEN
     182        1169 : alg_get_aut(GEN al)
     183             : {
     184        1169 :   long ta = alg_type(al);
     185        1169 :   if (ta != al_CYCLIC && ta != al_REAL)
     186          28 :     pari_err_TYPE("alg_get_aut [noncyclic algebra]", al);
     187        1141 :   return gel(alg_get_auts(al),1);
     188             : }
     189             : GEN
     190          63 : algaut(GEN al) { checkalg(al); return alg_get_aut(al); }
     191             : GEN
     192       19992 : alg_get_b(GEN al)
     193             : {
     194       19992 :   long ta = alg_type(al);
     195       19992 :   if (ta != al_CYCLIC && ta != al_REAL)
     196          28 :     pari_err_TYPE("alg_get_b [noncyclic algebra]", al);
     197       19964 :   return gel(al,3);
     198             : }
     199             : GEN
     200          91 : algb(GEN al) { checkalg(al); return alg_get_b(al); }
     201             : 
     202             : /* only CSA */
     203             : GEN
     204      394009 : alg_get_relmultable(GEN al)
     205             : {
     206      394009 :   if (alg_type(al) != al_CSA)
     207          14 :     pari_err_TYPE("alg_get_relmultable [algebra not given via mult. table]", al);
     208      393995 :   return gel(al,2);
     209             : }
     210             : GEN
     211          70 : algrelmultable(GEN al) { checkalg(al); return alg_get_relmultable(al); }
     212             : GEN
     213          84 : alg_get_splittingdata(GEN al)
     214             : {
     215          84 :   if (alg_type(al) != al_CSA)
     216          14 :     pari_err_TYPE("alg_get_splittingdata [algebra not given via mult. table]",al);
     217          70 :   return gel(al,3);
     218             : }
     219             : GEN
     220          84 : algsplittingdata(GEN al) { checkalg(al); return alg_get_splittingdata(al); }
     221             : GEN
     222        4347 : alg_get_splittingbasis(GEN al)
     223             : {
     224        4347 :   if (alg_type(al) != al_CSA)
     225           0 :     pari_err_TYPE("alg_get_splittingbasis [algebra not given via mult. table]",al);
     226        4347 :   return gmael(al,3,2);
     227             : }
     228             : GEN
     229        4347 : alg_get_splittingbasisinv(GEN al)
     230             : {
     231        4347 :   if (alg_type(al) != al_CSA)
     232           0 :     pari_err_TYPE("alg_get_splittingbasisinv [algebra not given via mult. table]",al);
     233        4347 :   return gmael(al,3,3);
     234             : }
     235             : 
     236             : /* only cyclic and CSA */
     237             : GEN
     238    25824049 : alg_get_splittingfield(GEN al) { return gel(al,1); }
     239             : GEN
     240         161 : algsplittingfield(GEN al)
     241             : {
     242             :   long ta;
     243         161 :   checkalg(al);
     244         161 :   ta = alg_type(al);
     245         161 :   if (ta != al_CYCLIC && ta != al_CSA && ta != al_REAL)
     246           7 :     pari_err_TYPE("alg_get_splittingfield [use alginit]",al);
     247         154 :   return alg_get_splittingfield(al);
     248             : }
     249             : long
     250     2495165 : alg_get_degree(GEN al)
     251             : {
     252             :   long ta;
     253     2495165 :   ta = alg_type(al);
     254     2495165 :   if (ta == al_REAL) return algreal_dim(al)==1? 1 : 2;
     255     2495081 :   if (ta != al_CYCLIC && ta != al_CSA)
     256          21 :     pari_err_TYPE("alg_get_degree [use alginit]",al);
     257     2495060 :   return rnf_get_degree(alg_get_splittingfield(al));
     258             : }
     259             : long
     260         679 : algdegree(GEN al)
     261             : {
     262         679 :   checkalg(al);
     263         672 :   return alg_get_degree(al);
     264             : }
     265             : 
     266             : GEN
     267      542462 : alg_get_center(GEN al)
     268             : {
     269             :   long ta;
     270      542462 :   ta = alg_type(al);
     271      542462 :   if (ta == al_REAL)
     272             :   {
     273          28 :     if (algreal_dim(al) != 4) return alg_get_splittingfield(al);
     274          14 :     return stor(1, LOWDEFAULTPREC);
     275             :   }
     276      542434 :   if (ta != al_CSA && ta != al_CYCLIC)
     277          14 :     pari_err_TYPE("alg_get_center [use alginit]",al);
     278      542420 :   return rnf_get_nf(alg_get_splittingfield(al));
     279             : }
     280             : GEN
     281         315 : alg_get_splitpol(GEN al)
     282             : {
     283         315 :   long ta = alg_type(al);
     284         315 :   if (ta != al_CYCLIC && ta != al_CSA)
     285           0 :     pari_err_TYPE("alg_get_splitpol [use alginit]",al);
     286         315 :   return rnf_get_pol(alg_get_splittingfield(al));
     287             : }
     288             : GEN
     289      102957 : alg_get_abssplitting(GEN al)
     290             : {
     291      102957 :   long ta = alg_type(al), prec;
     292      102957 :   if (ta != al_CYCLIC && ta != al_CSA)
     293           0 :     pari_err_TYPE("alg_get_abssplitting [use alginit]",al);
     294      102957 :   prec = nf_get_prec(alg_get_center(al));
     295      102957 :   return rnf_build_nfabs(alg_get_splittingfield(al), prec);
     296             : }
     297             : GEN
     298        2198 : alg_get_hasse_i(GEN al)
     299             : {
     300        2198 :   long ta = alg_type(al);
     301        2198 :   if (ta != al_CYCLIC && ta != al_CSA && ta != al_REAL)
     302           7 :     pari_err_TYPE("alg_get_hasse_i [use alginit]",al);
     303        2191 :   if (ta == al_CSA && !alg_is_asq(al))
     304          21 :     pari_err_IMPL("computation of Hasse invariants over table CSA");
     305        2170 :   return gel(al,4);
     306             : }
     307             : GEN
     308         266 : alghassei(GEN al) { checkalg(al); return alg_get_hasse_i(al); }
     309             : GEN
     310        4550 : alg_get_hasse_f(GEN al)
     311             : {
     312        4550 :   long ta = alg_type(al);
     313             :   GEN hf;
     314        4550 :   if (ta != al_CYCLIC && ta != al_CSA)
     315           7 :     pari_err_TYPE("alg_get_hasse_f [use alginit]",al);
     316        4543 :   if (ta == al_CSA && !alg_is_asq(al))
     317           7 :     pari_err_IMPL("computation of Hasse invariants over table CSA");
     318        4536 :   hf = gel(al,5);
     319        4536 :   if (typ(hf) == t_INT) /* could be computed on the fly */
     320          35 :     pari_err(e_MISC, "Hasse invariants were not computed for this algebra");
     321        4501 :   return hf;
     322             : }
     323             : GEN
     324         364 : alghassef(GEN al) { checkalg(al); return alg_get_hasse_f(al); }
     325             : 
     326             : /* all types */
     327             : GEN
     328        3297 : alg_get_basis(GEN al) { return gel(al,7); }
     329             : GEN
     330         154 : algbasis(GEN al) { checkalg(al); return alg_get_basis(al); }
     331             : GEN
     332       86738 : alg_get_invbasis(GEN al) { return gel(al,8); }
     333             : GEN
     334          84 : alginvbasis(GEN al) { checkalg(al); return alg_get_invbasis(al); }
     335             : GEN
     336     3757027 : alg_get_multable(GEN al) { return gel(al,9); }
     337             : GEN
     338         308 : algmultable(GEN al) { checkalg(al); return alg_get_multable(al); }
     339             : GEN
     340    11651584 : alg_get_char(GEN al) { if (!al) return gen_0; return gel(al,10); }
     341             : GEN
     342         112 : algchar(GEN al) { checkalg(al); return alg_get_char(al); }
     343             : GEN
     344      652287 : alg_get_tracebasis(GEN al) { return gel(al,11); }
     345             : GEN
     346        3774 : alg_get_invol(GEN al) { return gmael(al,6,2); }
     347             : 
     348             : /* lattices */
     349             : GEN
     350      251188 : alglat_get_primbasis(GEN lat) { return gel(lat,1); }
     351             : GEN
     352      296779 : alglat_get_scalar(GEN lat) { return gel(lat,2); }
     353             : 
     354             : /* algmodpr */
     355             : GEN
     356        3948 : algmodpr_get_pr(GEN data) { return gel(data,1); }
     357             : long
     358        5005 : algmodpr_get_k(GEN data) { return gel(data,2)[1]; } /* target M_k(F_p^m) */
     359             : long
     360        2219 : algmodpr_get_m(GEN data) { return gel(data,2)[2]; } /* target M_k(F_p^m) */
     361             : GEN
     362        1715 : algmodpr_get_ff(GEN data) { return gel(data,3); }
     363             : GEN
     364        1708 : algmodpr_get_proj(GEN data) { return gel(data,4); }
     365             : GEN
     366        3003 : algmodpr_get_lift(GEN data) { return gel(data,5); }
     367             : GEN
     368        1729 : algmodpr_get_tau(GEN data) { return gel(data,6); }
     369             : GEN
     370        3948 : algmodpr_get_p(GEN data) { return pr_get_p(algmodpr_get_pr(data)); }
     371             : GEN
     372        3801 : algmodpr_get_T(GEN data) { return gel(data,2)[2]==1 ? NULL : gel(data,7); }
     373             : 
     374             : /** ADDITIONAL **/
     375             : 
     376             : /* is N=smooth*prime? */
     377       12126 : static int Z_easyfactor(GEN N, ulong lim)
     378             : {
     379             :   GEN fa;
     380       12126 :   if (lgefint(N) <= 3) return 1;
     381       10324 :   fa = absZ_factor_limit(N, lim);
     382       10324 :   return BPSW_psp(veclast(gel(fa,1)));
     383             : }
     384             : 
     385             : /* no garbage collection */
     386             : static GEN
     387        1820 : backtrackfacto(GEN y0, long n, GEN red, GEN pl, GEN nf, GEN data, int (*test)(GEN,GEN), GEN* fa, GEN N, GEN I)
     388             : {
     389             :   long b, i;
     390        1820 :   ulong lim = 1UL << 17;
     391        1820 :   long *v = new_chunk(n+1);
     392        1820 :   pari_sp av = avma;
     393        1820 :   for (b = 0;; b += (2*b)/(3*n) + 1)
     394         377 :   {
     395             :     GEN ny, y1, y2;
     396        2197 :     set_avma(av);
     397        6423 :     for (i = 1; i <= n; i++) v[i] = -b;
     398        2197 :     v[n]--;
     399             :     for(;;)
     400             :     {
     401       14876 :       i = n;
     402       15568 :       while (i > 0)
     403       15191 :       { if (v[i] == b) v[i--] = -b; else { v[i]++; break; } }
     404       14876 :       if (i==0) break;
     405             : 
     406       14499 :       y1 = y0;
     407       32222 :       for (i = 1; i <= n; i++) y1 = nfadd(nf, y1, ZC_z_mul(gel(red,i), v[i]));
     408       14499 :       if (!nfchecksigns(nf, y1, pl)) continue;
     409             : 
     410       12126 :       ny = absi_shallow(nfnorm(nf, y1));
     411       12126 :       if (!signe(ny)) continue;
     412       12126 :       ny = diviiexact(ny, gcdii(ny, N));
     413       12126 :       if (!Z_easyfactor(ny, lim)) continue;
     414             : 
     415        2316 :       y2 = idealdivexact(nf, y1, idealadd(nf,y1,I));
     416        2316 :       *fa = idealfactor(nf, y2);
     417        2316 :       if (!data || test(data,*fa)) return y1;
     418             :     }
     419             :   }
     420             : }
     421             : 
     422             : /* if data == NULL, the test is skipped */
     423             : /* in the test, the factorization does not contain the known factors */
     424             : static GEN
     425        1827 : factoredextchinesetest(GEN nf, GEN x, GEN y, GEN pl, GEN* fa, GEN data, int (*test)(GEN,GEN))
     426             : {
     427        1827 :   pari_sp av = avma;
     428             :   long n,i;
     429        1827 :   GEN x1, y0, y1, red, N, I, P = gel(x,1), E = gel(x,2);
     430        1827 :   n = nf_get_degree(nf);
     431        1827 :   x = idealchineseinit(nf, mkvec2(x,pl));
     432        1827 :   x1 = gel(x,1);
     433        1827 :   red = lg(x1) == 1? matid(n): gmael(x1,1,1);
     434        1827 :   y0 = idealchinese(nf, x, y);
     435             : 
     436        1820 :   E = shallowcopy(E);
     437        1820 :   if (!gequal0(y0))
     438        7776 :     for (i=1; i<lg(E); i++)
     439             :     {
     440        5956 :       long v = nfval(nf,y0,gel(P,i));
     441        5956 :       if (cmpsi(v, gel(E,i)) < 0) gel(E,i) = stoi(v);
     442             :     }
     443             :   /* N and I : known factors */
     444        1820 :   I = factorbackprime(nf, P, E);
     445        1820 :   N = idealnorm(nf,I);
     446             : 
     447        1820 :   y1 = backtrackfacto(y0, n, red, pl, nf, data, test, fa, N, I);
     448             : 
     449             :   /* restore known factors */
     450        7776 :   for (i=1; i<lg(E); i++) gel(E,i) = stoi(nfval(nf,y1,gel(P,i)));
     451        1820 :   *fa = famat_reduce(famat_mul_shallow(*fa, mkmat2(P, E)));
     452        1820 :   return gc_all(av, 2, &y1, fa);
     453             : }
     454             : 
     455             : static GEN
     456        1414 : factoredextchinese(GEN nf, GEN x, GEN y, GEN pl, GEN* fa)
     457        1414 : { return factoredextchinesetest(nf,x,y,pl,fa,NULL,NULL); }
     458             : 
     459             : /** OPERATIONS ON ASSOCIATIVE ALGEBRAS algebras.c **/
     460             : 
     461             : /*
     462             : Convention:
     463             : (K/F,sigma,b) = sum_{i=0..n-1} u^i*K
     464             : t*u = u*sigma(t)
     465             : 
     466             : Natural basis:
     467             : 1<=i<=d*n^2
     468             : b_i = u^((i-1)/(dn))*ZKabs.((i-1)%(dn)+1)
     469             : 
     470             : Integral basis:
     471             : Basis of some order.
     472             : 
     473             : al structure:
     474             : 1- rnf of the cyclic splitting field of degree n over the center nf of degree d
     475             : 2- VEC of aut^i 1<=i<=n if n>1, or i=0 if n=1
     476             : 3- b in nf
     477             : 4- infinite hasse invariants (mod n) : VECSMALL of size r1, values only 0 or n/2 (if integral)
     478             : 5- finite hasse invariants (mod n) : VEC[sorted VEC of primes, VECSMALL of hasse inv mod n]
     479             : 6- VEC
     480             :   6.1- 0, or [a,b,sa] where sa^2=a if al is quaternion algebra (a,b)
     481             :   6.2- dn^2*dn^2 matrix of stored involution
     482             : 7* dn^2*dn^2 matrix expressing the integral basis in terms of the natural basis
     483             : 8* dn^2*dn^2 matrix expressing the natural basis in terms of the integral basis
     484             : 9* VEC of dn^2 matrices giving the dn^2*dn^2 left multiplication tables of the integral basis
     485             : 10* characteristic of the base field (used only for algebras given by a multiplication table)
     486             : 11* trace of basis elements
     487             : 
     488             : If al is given by a multiplication table (al_TABLE), only the * fields are present.
     489             : The other ones are filled with gen_0 placeholders.
     490             : */
     491             : 
     492             : /* assumes same center and same variable */
     493             : /* currently only works for coprime degrees */
     494             : GEN
     495          84 : algtensor(GEN al1, GEN al2, long flag) {
     496          84 :   pari_sp av = avma;
     497             :   long v, k, d1, d2;
     498             :   GEN nf, P1, P2, aut1, aut2, b1, b2, C, rnf, aut, b, x1, x2, al, rnfpol;
     499             : 
     500          84 :   checkalg(al1);
     501          70 :   checkalg(al2);
     502          63 :   if (alg_type(al1) != al_CYCLIC  || alg_type(al2) != al_CYCLIC)
     503          21 :     pari_err_IMPL("tensor of noncyclic algebras"); /* TODO: do it. */
     504             : 
     505          42 :   nf = alg_get_center(al1);
     506          42 :   if (!gequal(alg_get_center(al2),nf))
     507           7 :     pari_err_OP("tensor product [not the same center]", al1, al2);
     508             : 
     509          35 :   P1=alg_get_splitpol(al1); aut1=alg_get_aut(al1); b1=alg_get_b(al1);
     510          35 :   P2=alg_get_splitpol(al2); aut2=alg_get_aut(al2); b2=alg_get_b(al2);
     511          35 :   v=varn(P1);
     512             : 
     513          35 :   d1=alg_get_degree(al1);
     514          35 :   d2=alg_get_degree(al2);
     515          35 :   if (ugcd(d1,d2) != 1)
     516           7 :     pari_err_IMPL("tensor of cyclic algebras of noncoprime degrees"); /* TODO */
     517             : 
     518          28 :   if (d1==1) return gcopy(al2);
     519          21 :   if (d2==1) return gcopy(al1);
     520             : 
     521          14 :   C = nfcompositum(nf, P1, P2, 3);
     522          14 :   rnfpol = gel(C,1);
     523          14 :   if (!(flag & al_FACTOR)) rnfpol = mkvec2(rnfpol, stoi(1<<20));
     524          14 :   rnf = rnfinit(nf, rnfpol);
     525             :   /* TODO use integral basis of P1 and P2 to get that of C */
     526          14 :   x1 = gel(C,2);
     527          14 :   x2 = gel(C,3);
     528          14 :   k = itos(gel(C,4));
     529          14 :   aut = gadd(gsubst(aut2,v,x2),gmulsg(k,gsubst(aut1,v,x1)));
     530          14 :   b = nfmul(nf,nfpow_u(nf,b1,d2),nfpow_u(nf,b2,d1));
     531          14 :   al = alg_cyclic(rnf, aut, b, flag);
     532          14 :   return gc_GEN(av,al);
     533             : }
     534             : 
     535             : /* M an n x d Flm of rank d, n >= d. Initialize Mx = y solver */
     536             : static GEN
     537        8913 : Flm_invimage_init(GEN M, ulong p)
     538             : {
     539        8913 :   GEN v = Flm_indexrank(M, p), perm = gel(v,1);
     540        8913 :   GEN MM = rowpermute(M, perm); /* square invertible */
     541        8913 :   return mkvec2(Flm_inv(MM,p), perm);
     542             : }
     543             : /* assume Mx = y has a solution, v = Flm_invimage_init(M,p); return x */
     544             : static GEN
     545      619387 : Flm_invimage_pre(GEN v, GEN y, ulong p)
     546             : {
     547      619387 :   GEN inv = gel(v,1), perm = gel(v,2);
     548      619387 :   return Flm_Flc_mul(inv, vecsmallpermute(y, perm), p);
     549             : }
     550             : 
     551             : GEN
     552       14760 : algradical(GEN al)
     553             : {
     554       14760 :   pari_sp av = avma;
     555             :   GEN I, x, traces, K, MT, P, mt;
     556             :   long l,i,ni, n;
     557             :   ulong modu, expo, p;
     558       14760 :   checkalg(al);
     559       14760 :   if (alg_type(al) != al_TABLE) return gen_0;
     560       14669 :   P = alg_get_char(al);
     561       14669 :   mt = alg_get_multable(al);
     562       14669 :   n = alg_get_absdim(al);
     563       14669 :   dbg_printf(1)("algradical: char=%Ps, dim=%d\n", P, n);
     564       14669 :   traces = algtracematrix(al);
     565       14669 :   if (!signe(P))
     566             :   {
     567         546 :     dbg_printf(2)(" char 0, computing kernel...\n");
     568         546 :     K = ker(traces);
     569         546 :     dbg_printf(2)(" ...done.\n");
     570         546 :     ni = lg(K)-1; if (!ni) return gc_const(av, gen_0);
     571          70 :     return gc_upto(av, K);
     572             :   }
     573       14123 :   dbg_printf(2)(" char>0, computing kernel...\n");
     574       14123 :   K = FpM_ker(traces, P);
     575       14123 :   dbg_printf(2)(" ...done.\n");
     576       14123 :   ni = lg(K)-1; if (!ni) return gc_const(av, gen_0);
     577        7815 :   if (abscmpiu(P,n)>0) return gc_upto(av, K);
     578             : 
     579             :   /* tough case, p <= n. Ronyai's algorithm */
     580        4940 :   p = P[2]; l = 1;
     581        4940 :   expo = p; modu = p*p;
     582        4940 :   dbg_printf(2)(" char>0, hard case.\n");
     583        9601 :   while (modu<=(ulong)n) { l++; modu *= p; }
     584        4940 :   MT = ZMV_to_FlmV(mt, modu);
     585        4940 :   I = ZM_to_Flm(K,p); /* I_0 */
     586       13306 :   for (i=1; i<=l; i++) {/*compute I_i, expo = p^i, modu = p^(l+1) > n*/
     587             :     long j, lig,col;
     588        8913 :     GEN v = cgetg(ni+1, t_VECSMALL);
     589        8913 :     GEN invI = Flm_invimage_init(I, p);
     590        8913 :     dbg_printf(2)(" computing I_%d:\n", i);
     591        8913 :     traces = cgetg(ni+1,t_MAT);
     592       60273 :     for (j = 1; j <= ni; j++)
     593             :     {
     594       51360 :       GEN M = algbasismultable_Flm(MT, gel(I,j), modu);
     595       51360 :       uel(v,j) = algtracei(M, p,expo,modu);
     596             :     }
     597       60273 :     for (col=1; col<=ni; col++)
     598             :     {
     599       51360 :       GEN t = cgetg(n+1,t_VECSMALL); gel(traces,col) = t;
     600       51360 :       x = gel(I, col); /*col-th basis vector of I_{i-1}*/
     601      670747 :       for (lig=1; lig<=n; lig++)
     602             :       {
     603      619387 :         GEN y = _tablemul_ej_Fl(MT,x,lig,p);
     604      619387 :         GEN z = Flm_invimage_pre(invI, y, p);
     605      619387 :         uel(t,lig) = Flv_dotproduct(v, z, p);
     606             :       }
     607             :     }
     608        8913 :     dbg_printf(2)(" computing kernel...\n");
     609        8913 :     K = Flm_ker(traces, p);
     610        8913 :     dbg_printf(2)(" ...done.\n");
     611        8913 :     ni = lg(K)-1; if (!ni) return gc_const(av, gen_0);
     612        8366 :     I = Flm_mul(I,K,p);
     613        8366 :     expo *= p;
     614             :   }
     615        4393 :   return Flm_to_ZM(I);
     616             : }
     617             : 
     618             : /* compute the multiplication table of the element x, where mt is a
     619             :  * multiplication table in an arbitrary ring */
     620             : static GEN
     621         476 : Rgmultable(GEN mt, GEN x)
     622             : {
     623         476 :   long i, l = lg(x);
     624         476 :   GEN z = NULL;
     625        6188 :   for (i = 1; i < l; i++)
     626             :   {
     627        5712 :     GEN c = gel(x,i);
     628        5712 :     if (!gequal0(c))
     629             :     {
     630         714 :       GEN M = RgM_Rg_mul(gel(mt,i),c);
     631         714 :       z = z? RgM_add(z, M): M;
     632             :     }
     633             :   }
     634         476 :   return z;
     635             : }
     636             : 
     637             : static GEN
     638          56 : change_Rgmultable(GEN mt, GEN P, GEN Pi)
     639             : {
     640             :   GEN mt2;
     641          56 :   long lmt = lg(mt), i;
     642          56 :   mt2 = cgetg(lmt,t_VEC);
     643         532 :   for (i=1;i<lmt;i++) {
     644         476 :     GEN mti = Rgmultable(mt,gel(P,i));
     645         476 :     gel(mt2,i) = RgM_mul(Pi, RgM_mul(mti,P));
     646             :   }
     647          56 :   return mt2;
     648             : }
     649             : 
     650             : /* S: lift (basis of quotient) ; Si: proj */
     651             : static GEN
     652       37977 : alg_quotient0(GEN al, GEN S, GEN Si, long nq, GEN p, long maps)
     653             : {
     654       37977 :   GEN mt = cgetg(nq+1,t_VEC), P, Pi, d;
     655             :   long i;
     656       37977 :   dbg_printf(3)("  alg_quotient0: char=%Ps, dim=%d, dim I=%d\n", p, alg_get_absdim(al), lg(S)-1);
     657      173582 :   for (i=1; i<=nq; i++) {
     658      135605 :     GEN mti = algbasismultable(al,gel(S,i));
     659      135605 :     if (signe(p)) gel(mt,i) = FpM_mul(Si, FpM_mul(mti,S,p), p);
     660        6076 :     else          gel(mt,i) = RgM_mul(Si, RgM_mul(mti,S));
     661             :   }
     662       37977 :   if (!signe(p) && !isint1(Q_denom(mt))) {
     663          42 :     dbg_printf(3)("  bad case: denominator=%Ps\n", Q_denom(mt));
     664          42 :     P = Q_remove_denom(Si,&d);
     665          42 :     P = ZM_hnf(P);
     666          42 :     P = RgM_Rg_div(P,d); /* P: new basis (Z-basis of image of order in al) */
     667          42 :     Pi = RgM_inv(P);
     668          42 :     mt = change_Rgmultable(mt,P,Pi);
     669          42 :     Si = RgM_mul(Pi,Si);
     670          42 :     S = RgM_mul(S,P);
     671             :   }
     672       37977 :   al = algtableinit_i(mt,p);
     673       37977 :   if (maps) al = mkvec3(al,Si,S); /* algebra, proj, lift */
     674       37977 :   return al;
     675             : }
     676             : 
     677             : /* quotient of an algebra by a nontrivial two-sided ideal */
     678             : GEN
     679       12235 : alg_quotient(GEN al, GEN I, long maps)
     680             : {
     681       12235 :   pari_sp av = avma;
     682             :   GEN p, IS, ISi, S, Si;
     683             :   long n, ni;
     684             : 
     685       12235 :   checkalg(al);
     686       12235 :   if (alg_type(al) != al_TABLE) pari_err_TYPE("alg_quotient [not a table algebra]", al);
     687       12228 :   p = alg_get_char(al);
     688       12228 :   n = alg_get_absdim(al);
     689       12228 :   ni = lg(I)-1;
     690             : 
     691             :   /* force first vector of complement to be the identity */
     692       12228 :   IS = shallowconcat(I, gcoeff(alg_get_multable(al),1,1));
     693       12228 :   if (signe(p)) {
     694       12200 :     IS = FpM_suppl(IS,p);
     695       12200 :     ISi = FpM_inv(IS,p);
     696             :   }
     697             :   else {
     698          28 :     IS = suppl(IS);
     699          28 :     ISi = RgM_inv(IS);
     700             :   }
     701       12228 :   S = vecslice(IS, ni+1, n);
     702       12228 :   Si = rowslice(ISi, ni+1, n);
     703       12228 :   return gc_GEN(av, alg_quotient0(al, S, Si, n-ni, p, maps));
     704             : }
     705             : 
     706             : static GEN
     707       39079 : image_keep_first(GEN m, GEN p) /* assume first column is nonzero or m==0, no GC */
     708             : {
     709             :   GEN ir, icol, irow, M, c, x;
     710             :   long i;
     711       39079 :   if (gequal0(gel(m,1))) return zeromat(nbrows(m),0);
     712             : 
     713       39065 :   if (signe(p)) ir = FpM_indexrank(m,p);
     714        1708 :   else          ir = indexrank(m);
     715             : 
     716       39065 :   icol = gel(ir,2);
     717       39065 :   if (icol[1]==1) return extract0(m,icol,NULL);
     718             : 
     719          15 :   irow = gel(ir,1);
     720          15 :   M = extract0(m, irow, icol);
     721          15 :   c = extract0(gel(m,1), irow, NULL);
     722          15 :   if (signe(p)) x = FpM_FpC_invimage(M,c,p);
     723           0 :   else          x = inverseimage(M,c); /* TODO modulo a small prime */
     724             : 
     725          22 :   for (i=1; i<lg(x); i++)
     726             :   {
     727          22 :     if (!gequal0(gel(x,i)))
     728             :     {
     729          15 :       icol[i] = 1;
     730          15 :       vecsmall_sort(icol);
     731          15 :       return extract0(m,icol,NULL);
     732             :     }
     733             :   }
     734             : 
     735             :   return NULL; /* LCOV_EXCL_LINE */
     736             : }
     737             : 
     738             : /* z[1],...z[nz] central elements such that z[1]A + z[2]A + ... + z[nz]A = A
     739             :  * is a direct sum. idempotents ==> first basis element is identity */
     740             : GEN
     741       12516 : alg_centralproj(GEN al, GEN z, long maps)
     742             : {
     743       12516 :   pari_sp av = avma;
     744             :   GEN S, U, Ui, alq, p;
     745       12516 :   long i, iu, lz = lg(z), ta;
     746             : 
     747       12516 :   checkalg(al);
     748       12516 :   ta = alg_type(al);
     749       12516 :   if (ta != al_TABLE) pari_err_TYPE("algcentralproj [not a table algebra]", al);
     750       12509 :   if (typ(z) != t_VEC) pari_err_TYPE("alcentralproj",z);
     751       12502 :   p = alg_get_char(al);
     752       12502 :   dbg_printf(3)("  alg_centralproj: char=%Ps, dim=%d, #z=%d\n", p, alg_get_absdim(al), lz-1);
     753       12502 :   S = cgetg(lz,t_VEC); /* S[i] = Im(z_i) */
     754       38265 :   for (i=1; i<lz; i++)
     755             :   {
     756       25763 :     GEN mti = algbasismultable(al, gel(z,i));
     757       25763 :     gel(S,i) = image_keep_first(mti,p);
     758             :   }
     759       12502 :   U = shallowconcat1(S); /* U = [Im(z_1)|Im(z_2)|...|Im(z_nz)], n x n */
     760       12502 :   if (lg(U)-1 < alg_get_absdim(al)) pari_err_TYPE("alcentralproj [z[i]'s not surjective]",z);
     761       12495 :   if (signe(p)) Ui = FpM_inv(U,p);
     762         854 :   else          Ui = RgM_inv(U);
     763             :   if (!Ui) pari_err_BUG("alcentralproj"); /*LCOV_EXCL_LINE*/
     764             : 
     765       12495 :   alq = cgetg(lz,t_VEC);
     766       38244 :   for (iu=0,i=1; i<lz; i++)
     767             :   {
     768       25749 :     long nq = lg(gel(S,i))-1, ju = iu + nq;
     769       25749 :     GEN Si = rowslice(Ui, iu+1, ju);
     770       25749 :     gel(alq, i) = alg_quotient0(al,gel(S,i),Si,nq,p,maps);
     771       25749 :     iu = ju;
     772             :   }
     773       12495 :   return gc_GEN(av, alq);
     774             : }
     775             : 
     776             : /* al is an al_TABLE */
     777             : static GEN
     778       32999 : algtablecenter(GEN al)
     779             : {
     780       32999 :   pari_sp av = avma;
     781             :   long n, i, j, k, ic;
     782             :   GEN C, cij, mt, p;
     783             : 
     784       32999 :   n = alg_get_absdim(al);
     785       32999 :   mt = alg_get_multable(al);
     786       32999 :   p = alg_get_char(al);
     787       32999 :   C = cgetg(n+1,t_MAT);
     788      157861 :   for (j=1; j<=n; j++)
     789             :   {
     790      124862 :     gel(C,j) = cgetg(n*n-n+1,t_COL);
     791      124862 :     ic = 1;
     792     1117184 :     for (i=2; i<=n; i++) {
     793      992322 :       if (signe(p)) cij = FpC_sub(gmael(mt,i,j),gmael(mt,j,i),p);
     794       57694 :       else          cij = RgC_sub(gmael(mt,i,j),gmael(mt,j,i));
     795    19692678 :       for (k=1; k<=n; k++, ic++) gcoeff(C,ic,j) = gel(cij, k);
     796             :     }
     797             :   }
     798       32999 :   if (signe(p)) return gc_upto(av, FpM_ker(C,p));
     799        1785 :   else          return gc_upto(av, ker(C));
     800             : }
     801             : 
     802             : GEN
     803       11795 : algcenter(GEN al)
     804             : {
     805       11795 :   checkalg(al);
     806       11795 :   if (alg_type(al)==al_TABLE) return algtablecenter(al);
     807        2821 :   return alg_get_center(al);
     808             : }
     809             : 
     810             : /* Only in positive characteristic. Assumes that al is semisimple. */
     811             : GEN
     812        9573 : algprimesubalg(GEN al)
     813             : {
     814        9573 :   pari_sp av = avma;
     815             :   GEN p, Z, F, K;
     816             :   long nz, i;
     817        9573 :   checkalg(al);
     818        9573 :   p = alg_get_char(al);
     819        9573 :   if (!signe(p)) pari_err_DOMAIN("algprimesubalg","characteristic","=",gen_0,p);
     820             : 
     821        9559 :   Z = algtablecenter(al);
     822        9559 :   nz = lg(Z)-1;
     823        9559 :   if (nz==1) return Z;
     824             : 
     825        6716 :   F = cgetg(nz+1, t_MAT);
     826       28235 :   for (i=1; i<=nz; i++) {
     827       21519 :     GEN zi = gel(Z,i);
     828       21519 :     gel(F,i) = FpC_sub(algpow(al,zi,p),zi,p);
     829             :   }
     830        6716 :   K = FpM_ker(F,p);
     831        6716 :   return gc_upto(av, FpM_mul(Z,K,p));
     832             : }
     833             : 
     834             : static GEN
     835       19779 : out_decompose(GEN t, GEN Z, GEN P, GEN p)
     836             : {
     837       19779 :   GEN ali = gel(t,1), projm = gel(t,2), liftm = gel(t,3), pZ;
     838       19779 :   if (signe(p)) pZ = FpM_image(FpM_mul(projm,Z,p),p);
     839        1617 :   else          pZ = image(RgM_mul(projm,Z));
     840       19779 :   return mkvec5(ali, projm, liftm, pZ, P);
     841             : }
     842             : /* fa factorization of charpol(x) */
     843             : static GEN
     844        9928 : alg_decompose_from_facto(GEN al, GEN x, GEN fa, GEN Z, long mini)
     845             : {
     846        9928 :   long k = lgcols(fa)-1, k2 = mini? 1: k/2;
     847        9928 :   GEN v1 = rowslice(fa,1,k2);
     848        9928 :   GEN v2 = rowslice(fa,k2+1,k);
     849        9928 :   GEN alq, P, Q, p = alg_get_char(al);
     850        9928 :   dbg_printf(3)("  alg_decompose_from_facto\n");
     851        9928 :   if (signe(p)) {
     852        9102 :     P = FpXV_factorback(gel(v1,1), gel(v1,2), p, 0);
     853        9102 :     Q = FpXV_factorback(gel(v2,1), gel(v2,2), p, 0);
     854        9102 :     P = FpX_mul(P, FpXQ_inv(P,Q,p), p);
     855             :   }
     856             :   else {
     857         826 :     P = factorback(v1);
     858         826 :     Q = factorback(v2);
     859         826 :     P = RgX_mul(P, RgXQ_inv(P,Q));
     860             :   }
     861        9928 :   P = algpoleval(al, P, x);
     862        9928 :   if (signe(p)) Q = FpC_sub(col_ei(lg(P)-1,1), P, p);
     863         826 :   else          Q = gsub(gen_1, P);
     864        9928 :   if (gequal0(P) || gequal0(Q)) return NULL;
     865        9928 :   alq = alg_centralproj(al, mkvec2(P,Q), 1);
     866             : 
     867        9928 :   P = out_decompose(gel(alq,1), Z, P, p); if (mini) return P;
     868        9851 :   Q = out_decompose(gel(alq,2), Z, Q, p);
     869        9851 :   return mkvec2(P,Q);
     870             : }
     871             : 
     872             : static GEN
     873       15199 : random_pm1(long n)
     874             : {
     875       15199 :   GEN z = cgetg(n+1,t_VECSMALL);
     876             :   long i;
     877       62851 :   for (i = 1; i <= n; i++) z[i] = random_bits(5)%3 - 1;
     878       15199 :   return z;
     879             : }
     880             : 
     881             : static GEN alg_decompose(GEN al, GEN Z, long mini, GEN* pt_primelt);
     882             : /* Try to split al using x's charpoly. Return gen_0 if simple, NULL if failure.
     883             :  * And a splitting otherwise
     884             :  * If pt_primelt!=NULL, compute a primitive element of the center when simple */
     885             : static GEN
     886       18237 : try_fact(GEN al, GEN x, GEN zx, GEN Z, GEN Zal, long mini, GEN* pt_primelt)
     887             : {
     888       18237 :   GEN z, dec0, dec1, cp = algcharpoly(Zal,zx,0,1), fa, p = alg_get_char(al);
     889             :   long nfa, e;
     890       18237 :   dbg_printf(3)("  try_fact: zx=%Ps\n", zx);
     891       18237 :   if (signe(p)) fa = FpX_factor(cp,p);
     892        1519 :   else          fa = factor(cp);
     893       18237 :   dbg_printf(3)("  charpoly=%Ps\n", fa);
     894       18237 :   nfa = nbrows(fa);
     895       18237 :   if (nfa == 1) {
     896        8309 :     if (signe(p)) e = gel(fa,2)[1];
     897         693 :     else          e = itos(gcoeff(fa,1,2));
     898        8309 :     if (e == 1) {
     899        4487 :       if (pt_primelt != NULL) *pt_primelt = mkvec2(x, cp);
     900        4487 :       return gen_0;
     901             :     }
     902        3822 :     else return NULL;
     903             :   }
     904        9928 :   dec0 = alg_decompose_from_facto(al, x, fa, Z, mini);
     905        9928 :   if (!dec0) return NULL;
     906        9928 :   if (!mini) return dec0;
     907          77 :   dec1 = alg_decompose(gel(dec0,1), gel(dec0,4), 1, pt_primelt);
     908          77 :   z = gel(dec0,5);
     909          77 :   if (!isintzero(dec1)) {
     910           7 :     if (signe(p)) z = FpM_FpC_mul(gel(dec0,3),dec1,p);
     911           7 :     else          z = RgM_RgC_mul(gel(dec0,3),dec1);
     912             :   }
     913          77 :   return z;
     914             : }
     915             : static GEN
     916           7 : randcol(long n, GEN b)
     917             : {
     918           7 :   GEN N = addiu(shifti(b,1), 1);
     919             :   long i;
     920           7 :   GEN res =  cgetg(n+1,t_COL);
     921          63 :   for (i=1; i<=n; i++)
     922             :   {
     923          56 :     pari_sp av = avma;
     924          56 :     gel(res,i) = gc_INT(av, subii(randomi(N),b));
     925             :   }
     926           7 :   return res;
     927             : }
     928             : /* Return gen_0 if already simple. mini: only returns a central idempotent
     929             :  * corresponding to one simple factor
     930             :  * if pt_primelt!=NULL, sets it to a primitive element of the center when simple */
     931             : static GEN
     932       28293 : alg_decompose(GEN al, GEN Z, long mini, GEN* pt_primelt)
     933             : {
     934             :   pari_sp av;
     935             :   GEN Zal, x, zx, rand, dec0, B, p;
     936       28293 :   long i, nz = lg(Z)-1;
     937             : 
     938       28293 :   if (nz == 1) {
     939       13878 :     if (pt_primelt != 0) *pt_primelt = mkvec2(zerocol(alg_get_dim(al)), pol_x(0));
     940       13878 :     return gen_0;
     941             :   }
     942       14415 :   p = alg_get_char(al);
     943       14415 :   dbg_printf(2)(" alg_decompose: char=%Ps, dim=%d, dim Z=%d\n", p, alg_get_absdim(al), nz);
     944       14415 :   Zal = alg_subalg(al,Z);
     945       14415 :   Z = gel(Zal,2);
     946       14415 :   Zal = gel(Zal,1);
     947       14415 :   av = avma;
     948             : 
     949       14415 :   rand = random_pm1(nz);
     950       14415 :   zx = zc_to_ZC(rand);
     951       14415 :   if (signe(p)) {
     952       13267 :     zx = FpC_red(zx,p);
     953       13267 :     x = ZM_zc_mul(Z,rand);
     954       13267 :     x = FpC_red(x,p);
     955             :   }
     956        1148 :   else x = RgM_zc_mul(Z,rand);
     957       14415 :   dec0 = try_fact(al,x,zx,Z,Zal,mini,pt_primelt);
     958       14415 :   if (dec0) return dec0;
     959        3752 :   set_avma(av);
     960             : 
     961        3822 :   for (i=2; i<=nz; i++)
     962             :   {
     963        3815 :     dec0 = try_fact(al,gel(Z,i),col_ei(nz,i),Z,Zal,mini,pt_primelt);
     964        3815 :     if (dec0) return dec0;
     965          70 :     set_avma(av);
     966             :   }
     967           7 :   B = int2n(10);
     968             :   for (;;)
     969           0 :   {
     970           7 :     GEN x = randcol(nz,B), zx = ZM_ZC_mul(Z,x);
     971           7 :     dec0 = try_fact(al,x,zx,Z,Zal,mini,pt_primelt);
     972           7 :     if (dec0) return dec0;
     973           0 :     set_avma(av);
     974             :   }
     975             : }
     976             : 
     977             : static GEN
     978       23953 : alg_decompose_total(GEN al, GEN Z, long maps)
     979             : {
     980             :   GEN dec, sc, p;
     981             :   long i;
     982             : 
     983       23953 :   dec = alg_decompose(al, Z, 0, NULL);
     984       23953 :   if (isintzero(dec))
     985             :   {
     986       14102 :     if (maps) {
     987        8922 :       long n = alg_get_absdim(al);
     988        8922 :       al = mkvec3(al, matid(n), matid(n));
     989             :     }
     990       14102 :     return mkvec(al);
     991             :   }
     992        9851 :   p = alg_get_char(al); if (!signe(p)) p = NULL;
     993        9851 :   sc = cgetg(lg(dec), t_VEC);
     994       29553 :   for (i=1; i<lg(sc); i++) {
     995       19702 :     GEN D = gel(dec,i), a = gel(D,1), Za = gel(D,4);
     996       19702 :     GEN S = alg_decompose_total(a, Za, maps);
     997       19702 :     gel(sc,i) = S;
     998       19702 :     if (maps)
     999             :     {
    1000       13094 :       GEN projm = gel(D,2), liftm = gel(D,3);
    1001       13094 :       long j, lS = lg(S);
    1002       34636 :       for (j=1; j<lS; j++)
    1003             :       {
    1004       21542 :         GEN Sj = gel(S,j), p2 = gel(Sj,2), l2 = gel(Sj,3);
    1005       21542 :         if (p) p2 = FpM_mul(p2, projm, p);
    1006        1449 :         else   p2 = RgM_mul(p2, projm);
    1007       21542 :         if (p) l2 = FpM_mul(liftm, l2, p);
    1008        1449 :         else   l2 = RgM_mul(liftm, l2);
    1009       21542 :         gel(Sj,2) = p2;
    1010       21542 :         gel(Sj,3) = l2;
    1011             :       }
    1012             :     }
    1013             :   }
    1014        9851 :   return shallowconcat1(sc);
    1015             : }
    1016             : 
    1017             : static GEN
    1018       14485 : alg_subalg(GEN al, GEN basis)
    1019             : {
    1020       14485 :   GEN invbasis, mt, p = alg_get_char(al);
    1021             :   long i, j, n;
    1022             : 
    1023       14485 :   if (!signe(p)) p = NULL;
    1024       14485 :   basis = shallowmatconcat(mkvec2(col_ei(alg_get_absdim(al),1), basis));
    1025       14485 :   if (p)
    1026             :   {
    1027       13316 :     basis = image_keep_first(basis,p);
    1028       13316 :     invbasis = FpM_inv(basis,p);
    1029             :   }
    1030             :   else
    1031             :   { /* FIXME use an integral variant of image_keep_first */
    1032        1169 :     basis = QM_ImQ_hnf(basis);
    1033        1169 :     invbasis = RgM_inv(basis);
    1034             :   }
    1035       14485 :   n = lg(basis)-1;
    1036       14485 :   mt = cgetg(n+1,t_VEC);
    1037       14485 :   gel(mt,1) = matid(n);
    1038       44929 :   for (i = 2; i <= n; i++)
    1039             :   {
    1040       30444 :     GEN mtx = cgetg(n+1,t_MAT), x = gel(basis,i);
    1041       30444 :     gel(mtx,1) = col_ei(n,i);
    1042      182258 :     for (j = 2; j <= n; j++)
    1043             :     {
    1044      151814 :       GEN xy = algmul(al, x, gel(basis,j));
    1045      151814 :       if (p) gel(mtx,j) = FpM_FpC_mul(invbasis, xy, p);
    1046       36253 :       else   gel(mtx,j) = RgM_RgC_mul(invbasis, xy);
    1047             :     }
    1048       30444 :     gel(mt,i) = mtx;
    1049             :   }
    1050       14485 :   return mkvec2(algtableinit_i(mt,p), basis);
    1051             : }
    1052             : 
    1053             : GEN
    1054          84 : algsubalg(GEN al, GEN basis)
    1055             : {
    1056          84 :   pari_sp av = avma;
    1057             :   GEN p;
    1058          84 :   checkalg(al);
    1059          84 :   if (alg_type(al) == al_REAL) pari_err_TYPE("algsubalg [real algebra]", al);
    1060          77 :   if (typ(basis) != t_MAT) pari_err_TYPE("algsubalg",basis);
    1061          70 :   p = alg_get_char(al);
    1062          70 :   if (signe(p)) basis = RgM_to_FpM(basis,p);
    1063          70 :   return gc_GEN(av, alg_subalg(al,basis));
    1064             : }
    1065             : 
    1066             : static int
    1067       14603 : cmp_algebra(GEN x, GEN y)
    1068             : {
    1069             :   long d;
    1070       14603 :   d = gel(x,1)[1] - gel(y,1)[1]; if (d) return d < 0? -1: 1;
    1071       12687 :   d = gel(x,1)[2] - gel(y,1)[2]; if (d) return d < 0? -1: 1;
    1072       12687 :   return cmp_universal(gel(x,2), gel(y,2));
    1073             : }
    1074             : 
    1075             : GEN
    1076        9678 : algsimpledec_ss(GEN al, long maps)
    1077             : {
    1078        9678 :   pari_sp av = avma;
    1079             :   GEN Z, p, r, res, perm;
    1080             :   long i, l, n;
    1081        9678 :   checkalg(al);
    1082        9678 :   p = alg_get_char(al);
    1083        9678 :   dbg_printf(1)("algsimpledec_ss: char=%Ps, dim=%d\n", p, alg_get_absdim(al));
    1084        9678 :   if (signe(p))                     Z = algprimesubalg(al);
    1085         273 :   else if (alg_type(al)!=al_TABLE)  Z = gen_0;
    1086         252 :   else                              Z = algtablecenter(al);
    1087             : 
    1088        9678 :   if (lg(Z) == 2) {/* dim Z = 1 */
    1089        5427 :     n = alg_get_absdim(al);
    1090        5427 :     set_avma(av);
    1091        5427 :     if (!maps) return mkveccopy(al);
    1092        4720 :     retmkvec(mkvec3(gcopy(al), matid(n), matid(n)));
    1093             :   }
    1094        4251 :   res = alg_decompose_total(al, Z, maps);
    1095        4251 :   l = lg(res); r = cgetg(l, t_VEC);
    1096       18353 :   for (i = 1; i < l; i++)
    1097             :   {
    1098       14102 :     GEN A = maps? gmael(res,i,1): gel(res,i);
    1099       14102 :     gel(r,i) = mkvec2(mkvecsmall2(alg_get_dim(A), lg(algtablecenter(A))),
    1100             :                       alg_get_multable(A));
    1101             :   }
    1102        4251 :   perm = gen_indexsort(r, (void*)cmp_algebra, &cmp_nodata);
    1103        4251 :   return gc_GEN(av, vecpermute(res, perm));
    1104             : }
    1105             : 
    1106             : GEN
    1107        2730 : algsimpledec(GEN al, long maps)
    1108             : {
    1109        2730 :   pari_sp av = avma;
    1110             :   int ss;
    1111        2730 :   GEN rad, dec, res, proj=NULL, lift=NULL;
    1112        2730 :   rad = algradical(al);
    1113        2730 :   ss = gequal0(rad);
    1114        2730 :   if (!ss)
    1115             :   {
    1116        1428 :     al = alg_quotient(al, rad, maps);
    1117        1428 :     if (maps) {
    1118          14 :       proj = gel(al,2);
    1119          14 :       lift = gel(al,3);
    1120          14 :       al = gel(al,1);
    1121             :     }
    1122             :   }
    1123        2730 :   dec = algsimpledec_ss(al, maps);
    1124        2730 :   if (!ss && maps) /* update maps */
    1125             :   {
    1126          14 :     GEN p = alg_get_char(al);
    1127             :     long i;
    1128          42 :     for (i=1; i<lg(dec); i++)
    1129             :     {
    1130          28 :       if (signe(p))
    1131             :       {
    1132          14 :         gmael(dec,i,2) = FpM_mul(gmael(dec,i,2), proj, p);
    1133          14 :         gmael(dec,i,3) = FpM_mul(lift, gmael(dec,i,3), p);
    1134             :       }
    1135             :       else
    1136             :       {
    1137          14 :         gmael(dec,i,2) = RgM_mul(gmael(dec,i,2), proj);
    1138          14 :         gmael(dec,i,3) = RgM_mul(lift, gmael(dec,i,3));
    1139             :       }
    1140             :     }
    1141             :   }
    1142        2730 :   res = mkvec2(rad, dec);
    1143        2730 :   return gc_GEN(av,res);
    1144             : }
    1145             : 
    1146             : static GEN alg_idempotent(GEN al, long n, long d);
    1147             : static GEN
    1148       12733 : try_split(GEN al, GEN x, long n, long d)
    1149             : {
    1150       12733 :   GEN cp, p = alg_get_char(al), fa, e, pol, exp, P, Q, U, u, mx, mte, ire;
    1151       12733 :   long nfa, i, smalldim = alg_get_absdim(al)+1, dim, smalli = 0;
    1152       12733 :   cp = algcharpoly(al,x,0,1);
    1153       12733 :   fa = FpX_factor(cp,p);
    1154       12733 :   nfa = nbrows(fa);
    1155       12733 :   if (nfa == 1) return NULL;
    1156        5061 :   pol = gel(fa,1);
    1157        5061 :   exp = gel(fa,2);
    1158             : 
    1159             :   /* charpoly is always a d-th power */
    1160       15563 :   for (i=1; i<lg(exp); i++) {
    1161       10509 :     if (exp[i]%d) pari_err(e_MISC, "the algebra must be simple (try_split 1)");
    1162       10502 :     exp[i] /= d;
    1163             :   }
    1164        5054 :   cp = FpXV_factorback(gel(fa,1), gel(fa,2), p, 0);
    1165             : 
    1166             :   /* find smallest Fp-dimension of a characteristic space */
    1167       15556 :   for (i=1; i<lg(pol); i++) {
    1168       10502 :     dim = degree(gel(pol,i))*exp[i];
    1169       10502 :     if (dim < smalldim) {
    1170        5126 :       smalldim = dim;
    1171        5126 :       smalli = i;
    1172             :     }
    1173             :   }
    1174        5054 :   i = smalli;
    1175        5054 :   if (smalldim != n) return NULL;
    1176             :   /* We could also compute e*al*e and try again with this smaller algebra */
    1177             :   /* Fq-rank 1 = Fp-rank n idempotent: success */
    1178             : 
    1179             :   /* construct idempotent */
    1180        5040 :   mx = algbasismultable(al,x);
    1181        5040 :   P = gel(pol,i);
    1182        5040 :   P = FpX_powu(P, exp[i], p);
    1183        5040 :   Q = FpX_div(cp, P, p);
    1184        5040 :   e = algpoleval(al, Q, mkvec2(x,mx));
    1185        5040 :   U = FpXQ_inv(Q, P, p);
    1186        5040 :   u = algpoleval(al, U, mkvec2(x,mx));
    1187        5040 :   e = algbasismul(al, e, u);
    1188        5040 :   mte = algbasisrightmultable(al,e);
    1189        5040 :   ire = FpM_indexrank(mte,p);
    1190        5040 :   if (lg(gel(ire,1))-1 != smalldim*d) pari_err(e_MISC, "the algebra must be simple (try_split 2)");
    1191             : 
    1192        5033 :   return mkvec3(e,mte,ire);
    1193             : }
    1194             : 
    1195             : /*
    1196             :  * Given a simple algebra al of dimension d^2 over its center of degree n,
    1197             :  * find an idempotent e in al with rank n (which is minimal).
    1198             : */
    1199             : static GEN
    1200        5047 : alg_idempotent(GEN al, long n, long d)
    1201             : {
    1202        5047 :   pari_sp av = avma;
    1203        5047 :   long i, N = alg_get_absdim(al);
    1204        5047 :   GEN e, p = alg_get_char(al), x;
    1205       12404 :   for(i=2; i<=N; i++) {
    1206       12061 :     x = col_ei(N,i);
    1207       12061 :     e = try_split(al, x, n, d);
    1208       12047 :     if (e) return e;
    1209        7357 :     set_avma(av);
    1210             :   }
    1211             :   for(;;) {
    1212         672 :     x = random_FpC(N,p);
    1213         672 :     e = try_split(al, x, n, d);
    1214         672 :     if (e) return e;
    1215         329 :     set_avma(av);
    1216             :   }
    1217             : }
    1218             : 
    1219             : static GEN
    1220        4592 : try_descend(GEN M, GEN B, GEN p, long m, long n, long d)
    1221             : {
    1222        4592 :   GEN B2 = cgetg(m+1,t_MAT), b;
    1223        4592 :   long i, j, k=0;
    1224       13342 :   for (i=1; i<=d; i++)
    1225             :   {
    1226        8750 :     k++;
    1227        8750 :     b = gel(B,i);
    1228        8750 :     gel(B2,k) = b;
    1229       20468 :     for (j=1; j<n; j++)
    1230             :     {
    1231       11718 :       k++;
    1232       11718 :       b = FpM_FpC_mul(M,b,p);
    1233       11718 :       gel(B2,k) = b;
    1234             :     }
    1235             :   }
    1236        4592 :   if (!signe(FpM_det(B2,p))) return NULL;
    1237        4165 :   return FpM_inv(B2,p);
    1238             : }
    1239             : 
    1240             : /* Given an m*m matrix M with irreducible charpoly over F of degree n,
    1241             :  * let K = F(M), which is a field, and write m=d*n.
    1242             :  * Compute the d-dimensional K-vector space structure on V=F^m induced by M.
    1243             :  * Return [B,C] where:
    1244             :  *  - B is m*d matrix over F giving a K-basis b_1,...,b_d of V
    1245             :  *  - C is d*m matrix over F[x] expressing the canonical F-basis of V on the b_i
    1246             :  * Currently F = Fp TODO extend this. */
    1247             : static GEN
    1248        4165 : descend_i(GEN M, long n, GEN p)
    1249             : {
    1250             :   GEN B, C;
    1251             :   long m,d,i;
    1252             :   pari_sp av;
    1253        4165 :   m = lg(M)-1;
    1254        4165 :   d = m/n;
    1255        4165 :   B = cgetg(d+1,t_MAT);
    1256        4165 :   av = avma;
    1257             : 
    1258             :   /* try a subset of the canonical basis */
    1259       12061 :   for (i=1; i<=d; i++)
    1260        7896 :     gel(B,i) = col_ei(m,n*(i-1)+1);
    1261        4165 :   C = try_descend(M,B,p,m,n,d);
    1262        4165 :   if (C) return mkvec2(B,C);
    1263         392 :   set_avma(av);
    1264             : 
    1265             :   /* try smallish elements */
    1266        1176 :   for (i=1; i<=d; i++)
    1267         784 :     gel(B,i) = FpC_red(zc_to_ZC(random_pm1(m)),p);
    1268         392 :   C = try_descend(M,B,p,m,n,d);
    1269         392 :   if (C) return mkvec2(B,C);
    1270          35 :   set_avma(av);
    1271             : 
    1272             :   /* try random elements */
    1273             :   for (;;)
    1274             :   {
    1275         105 :     for (i=1; i<=d; i++)
    1276          70 :       gel(B,i) = random_FpC(m,p);
    1277          35 :     C = try_descend(M,B,p,m,n,d);
    1278          35 :     if (C) return mkvec2(B,C);
    1279           0 :     set_avma(av);
    1280             :   }
    1281             : }
    1282             : static GEN
    1283       18746 : RgC_contract(GEN C, long n, long v) /* n>1 */
    1284             : {
    1285             :   GEN C2, P;
    1286             :   long m, d, i, j;
    1287       18746 :   m = lg(C)-1;
    1288       18746 :   d = m/n;
    1289       18746 :   C2 = cgetg(d+1,t_COL);
    1290       55034 :   for (i=1; i<=d; i++)
    1291             :   {
    1292       36288 :     P = pol_xn(n-1,v);
    1293      131348 :     for (j=1; j<=n; j++)
    1294       95060 :       gel(P,j+1) = gel(C,n*(i-1)+j);
    1295       36288 :     P = normalizepol(P);
    1296       36288 :     gel(C2,i) = P;
    1297             :   }
    1298       18746 :   return C2;
    1299             : }
    1300             : static GEN
    1301        4165 : RgM_contract(GEN A, long n, long v) /* n>1 */
    1302             : {
    1303        4165 :   GEN A2 = cgetg(lg(A),t_MAT);
    1304             :   long i;
    1305       22911 :   for (i=1; i<lg(A2); i++)
    1306       18746 :     gel(A2,i) = RgC_contract(gel(A,i),n,v);
    1307        4165 :   return A2;
    1308             : }
    1309             : static GEN
    1310        4165 : descend(GEN M, long n, GEN p, long v)
    1311             : {
    1312        4165 :   GEN res = descend_i(M,n,p);
    1313        4165 :   gel(res,2) = RgM_contract(gel(res,2),n,v);
    1314        4165 :   return res;
    1315             : }
    1316             : 
    1317             : /* isomorphism of Fp-vector spaces M_d(F_p^n) -> (F_p)^(d^2*n) */
    1318             : static GEN
    1319       49343 : RgM_mat2col(GEN M, long d, long n)
    1320             : {
    1321       49343 :   long nd = d*n,  N = d*nd, i, j, ni, nj;
    1322       49343 :   GEN C = cgetg(N+1, t_COL);
    1323      168504 :   for (i=1, ni = 0; i<=d; i++, ni += nd)
    1324      459508 :     for (j=1, nj = 0; j<=d; j++, nj += n)
    1325             :     {
    1326      340347 :       GEN P = gcoeff(M,i,j);
    1327      340347 :       long k, e = ni + nj + 1;
    1328      340347 :       if (typ(P)==t_POL)
    1329             :       {
    1330      339731 :         long dP = degpol(P);
    1331      708139 :         for (k = 0; k <= dP; k++)
    1332      368408 :           gel(C,e+k) = gel(P,k+2);
    1333             :       } else
    1334             :       {
    1335         616 :         gel(C,e) = P;
    1336         616 :         k = 1;
    1337             :       }
    1338      552946 :       for (  ; k < n; k++)
    1339      212599 :         gel(C,e+k) = gen_0;
    1340             :     }
    1341       49343 :   return C;
    1342             : }
    1343             : /* inverse isomorphism */
    1344             : static GEN
    1345        1708 : RgC_col2mat(GEN C, long d, long n, long v)
    1346             : {
    1347             :   long i, j, start;
    1348        1708 :   GEN M = cgetg(d+1, t_MAT), cM;
    1349        5432 :   for (j=1; j<=d; j++)
    1350             :   {
    1351        3724 :     cM = cgetg(d+1, t_COL);
    1352       14420 :     for (i=1; i<=d; i++)
    1353             :     {
    1354       10696 :       start = n*(d*(i-1)+j-1)+1;
    1355       10696 :       if (n==1) gel(cM,i) = gel(C, start);
    1356        4564 :       else gel(cM,i) = RgV_to_RgX(vecslice(C, start, start+n-1), v);
    1357             :     }
    1358        3724 :     gel(M,j) = cM;
    1359             :   }
    1360        1708 :   return M;
    1361             : }
    1362             : 
    1363             : static GEN
    1364        6510 : alg_finite_csa_split(GEN al, long v)
    1365             : {
    1366             :   GEN Z, e, mte, ire, primelt, b, T, M, proje, lifte, extre, p, B, C, mt, mx, map, mapi, T2, ro;
    1367        6510 :   long n, d, N = alg_get_absdim(al), i;
    1368        6510 :   p = alg_get_char(al);
    1369             :   /* compute the center */
    1370        6510 :   Z = algcenter(al);
    1371             :   /* TODO option to give the center as input instead of computing it */
    1372        6510 :   n = lg(Z)-1;
    1373             : 
    1374             :   /* compute a minimal rank idempotent e */
    1375        6510 :   if (n==N) {
    1376        1456 :     d = 1;
    1377        1456 :     e = col_ei(N,1);
    1378        1456 :     mte = matid(N);
    1379        1456 :     ire = mkvec2(identity_perm(n),identity_perm(n));
    1380             :   }
    1381             :   else {
    1382        5054 :     d = usqrt(N/n);
    1383        5054 :     if (d*d*n != N) pari_err(e_MISC, "the algebra must be simple (alg_finite_csa_split 1)");
    1384        5047 :     e = alg_idempotent(al,n,d);
    1385        5033 :     mte = gel(e,2);
    1386        5033 :     ire = gel(e,3);
    1387        5033 :     e = gel(e,1);
    1388             :   }
    1389             : 
    1390             :   /* identify the center */
    1391        6489 :   if (n==1)
    1392             :   {
    1393        2317 :     T = pol_x(v);
    1394        2317 :     primelt = gen_0;
    1395             :   }
    1396             :   else
    1397             :   {
    1398        4172 :     b = alg_decompose(al, Z, 1, &primelt);
    1399        4172 :     if (!gequal0(b)) pari_err(e_MISC, "the algebra must be simple (alg_finite_csa_split 2)");
    1400        4165 :     T = gel(primelt,2);
    1401        4165 :     primelt = gel(primelt,1);
    1402        4165 :     setvarn(T,v);
    1403             :   }
    1404             : 
    1405             :   /* use the ffinit polynomial */
    1406        6482 :   if (n>1)
    1407             :   {
    1408        4165 :     T2 = init_Fq(p,n,v);
    1409        4165 :     setvarn(T,fetch_var_higher());
    1410        4165 :     ro = FpXQX_roots(T2,T,p);
    1411        4165 :     ro = gel(ro,1);
    1412        4165 :     primelt = algpoleval(al,ro,primelt);
    1413        4165 :     T = T2;
    1414        4165 :     delete_var();
    1415             :   }
    1416             : 
    1417             :   /* descend al*e to a vector space over the center */
    1418             :   /* lifte: al*e -> al ; proje: al*e -> al */
    1419        6482 :   lifte = shallowextract(mte,gel(ire,2));
    1420        6482 :   extre = shallowmatextract(mte,gel(ire,1),gel(ire,2));
    1421        6482 :   extre = FpM_inv(extre,p);
    1422        6482 :   proje = rowpermute(mte,gel(ire,1));
    1423        6482 :   proje = FpM_mul(extre,proje,p);
    1424        6482 :   if (n==1)
    1425             :   {
    1426        2317 :     B = lifte;
    1427        2317 :     C = proje;
    1428             :   }
    1429             :   else
    1430             :   {
    1431        4165 :     M = algbasismultable(al,primelt);
    1432        4165 :     M = FpM_mul(M,lifte,p);
    1433        4165 :     M = FpM_mul(proje,M,p);
    1434        4165 :     B = descend(M,n,p,v);
    1435        4165 :     C = gel(B,2);
    1436        4165 :     B = gel(B,1);
    1437        4165 :     B = FpM_mul(lifte,B,p);
    1438        4165 :     C = FqM_mul(C,proje,T,p);
    1439             :   }
    1440             : 
    1441             :   /* compute the isomorphism */
    1442        6482 :   mt = alg_get_multable(al);
    1443        6482 :   map = cgetg(N+1,t_VEC);
    1444        6482 :   M = cgetg(N+1,t_MAT);
    1445       55321 :   for (i=1; i<=N; i++)
    1446             :   {
    1447       48839 :     mx = gel(mt,i);
    1448       48839 :     mx = FpM_mul(mx,B,p);
    1449       48839 :     mx = FqM_mul(C,mx,T,p);
    1450       48839 :     gel(map,i) = mx;
    1451       48839 :     gel(M,i) = RgM_mat2col(mx,d,n);
    1452             :   }
    1453        6482 :   mapi = FpM_inv(M,p);
    1454        6482 :   if (!mapi) pari_err(e_MISC, "the algebra must be simple (alg_finite_csa_split 3)");
    1455        6475 :   return mkvec4(T,map,mapi,M);
    1456             : }
    1457             : 
    1458             : GEN
    1459        3766 : algsplit(GEN al, long v)
    1460             : {
    1461        3766 :   pari_sp av = avma;
    1462             :   GEN res, T, map, mapi, ff, p;
    1463             :   long i,j,k,li,lj;
    1464        3766 :   checkalg(al);
    1465        3759 :   p = alg_get_char(al);
    1466        3759 :   if (gequal0(p))
    1467           7 :     pari_err_IMPL("splitting a characteristic 0 algebra over its center");
    1468        3752 :   res = alg_finite_csa_split(al, v);
    1469        3717 :   T = gel(res,1);
    1470        3717 :   map = gel(res,2);
    1471        3717 :   mapi = gel(res,3);
    1472        3717 :   ff = Tp_to_FF(T,p);
    1473       33593 :   for (i=1; i<lg(map); i++)
    1474             :   {
    1475       29876 :     li = lg(gel(map,i));
    1476       89908 :     for (j=1; j<li; j++)
    1477             :     {
    1478       60032 :       lj = lg(gmael(map,i,j));
    1479      190876 :       for (k=1; k<lj; k++)
    1480      130844 :         gmael3(map,i,j,k) = Fq_to_FF(gmael3(map,i,j,k),ff);
    1481             :     }
    1482             :   }
    1483             : 
    1484        3717 :   return gc_GEN(av, mkvec2(map,mapi));
    1485             : }
    1486             : 
    1487             : /* multiplication table sanity checks */
    1488             : static GEN
    1489       60372 : check_mt_noid(GEN mt, GEN p)
    1490             : {
    1491             :   long i, l;
    1492       60372 :   GEN MT = cgetg_copy(mt, &l);
    1493       60372 :   if (typ(MT) != t_VEC || l == 1) return NULL;
    1494      297879 :   for (i = 1; i < l; i++)
    1495             :   {
    1496      237556 :     GEN M = gel(mt,i);
    1497      237556 :     if (typ(M) != t_MAT || lg(M) != l || lgcols(M) != l) return NULL;
    1498      237528 :     if (p) M = RgM_to_FpM(M,p);
    1499      237528 :     gel(MT,i) = M;
    1500             :   }
    1501       60323 :   return MT;
    1502             : }
    1503             : static GEN
    1504       59847 : check_mt(GEN mt, GEN p)
    1505             : {
    1506             :   long i;
    1507             :   GEN MT;
    1508       59847 :   MT = check_mt_noid(mt, p);
    1509       59847 :   if (!MT || !ZM_isidentity(gel(MT,1))) return NULL;
    1510      234217 :   for (i=2; i<lg(MT); i++)
    1511      174398 :     if (ZC_is_ei(gmael(MT,i,1)) != i) return NULL;
    1512       59819 :   return MT;
    1513             : }
    1514             : 
    1515             : static GEN
    1516         294 : check_relmt(GEN nf, GEN mt)
    1517             : {
    1518         294 :   long i, l = lg(mt), j, k;
    1519         294 :   GEN MT = gcopy(mt), a, b, d;
    1520         294 :   if (typ(MT) != t_VEC || l == 1) return NULL;
    1521        1225 :   for (i = 1; i < l; i++)
    1522             :   {
    1523         952 :     GEN M = gel(MT,i);
    1524         952 :     if (typ(M) != t_MAT || lg(M) != l || lgcols(M) != l) return NULL;
    1525        4760 :     for (k = 1; k < l; k++)
    1526       21525 :       for (j = 1; j < l; j++)
    1527             :       {
    1528       17717 :         a = gcoeff(M,j,k);
    1529       17717 :         if (typ(a)==t_INT) continue;
    1530        2247 :         b = algtobasis(nf,a);
    1531        2247 :         d = Q_denom(b);
    1532        2247 :         if (!isint1(d))
    1533          14 :           pari_err_DOMAIN("alg_csa_table", "denominator(mt)", "!=", gen_1, mt);
    1534        2233 :         gcoeff(M,j,k) = lift(basistoalg(nf,b));
    1535             :       }
    1536         938 :     if (i > 1 && RgC_is_ei(gel(M,1)) != i) return NULL; /* i = 1 checked at end */
    1537         931 :     gel(MT,i) = M;
    1538             :   }
    1539         273 :   if (!RgM_isidentity(gel(MT,1))) return NULL;
    1540         273 :   return MT;
    1541             : }
    1542             : 
    1543             : int
    1544         532 : algisassociative(GEN mt0, GEN p)
    1545             : {
    1546         532 :   pari_sp av = avma;
    1547             :   long i, j, k, n;
    1548             :   GEN M, mt;
    1549             : 
    1550         532 :   if (checkalg_i(mt0)) { p = alg_get_char(mt0); mt0 = alg_get_multable(mt0); }
    1551         532 :   if (!p) p = gen_0;
    1552         532 :   if (typ(p) != t_INT) pari_err_TYPE("algisassociative",p);
    1553         525 :   mt = check_mt_noid(mt0, isintzero(p)? NULL: p);
    1554         525 :   if (!mt) pari_err_TYPE("algisassociative (mult. table)", mt0);
    1555         490 :   if (!ZM_isidentity(gel(mt,1))) return gc_bool(av,0);
    1556         476 :   n = lg(mt)-1;
    1557         476 :   M = cgetg(n+1,t_MAT);
    1558        3731 :   for (j=1; j<=n; j++) gel(M,j) = cgetg(n+1,t_COL);
    1559        3731 :   for (i=1; i<=n; i++)
    1560             :   {
    1561        3255 :     GEN mi = gel(mt,i);
    1562       36918 :     for (j=1; j<=n; j++) gcoeff(M,i,j) = gel(mi,j); /* ei.ej */
    1563             :   }
    1564        3241 :   for (i=2; i<=n; i++) {
    1565        2772 :     GEN mi = gel(mt,i);
    1566       30373 :     for (j=2; j<=n; j++) {
    1567      381451 :       for (k=2; k<=n; k++) {
    1568             :         GEN x, y;
    1569      353850 :         if (signe(p)) {
    1570      242039 :           x = _tablemul_ej_Fp(mt,gcoeff(M,i,j),k,p);
    1571      242039 :           y = FpM_FpC_mul(mi,gcoeff(M,j,k),p);
    1572             :         }
    1573             :         else {
    1574      111811 :           x = _tablemul_ej(mt,gcoeff(M,i,j),k);
    1575      111811 :           y = RgM_RgC_mul(mi,gcoeff(M,j,k));
    1576             :         }
    1577             :         /* not cmp_universal: must not fail on 0 == Mod(0,2) for instance */
    1578      353850 :         if (!gequal(x,y)) return gc_bool(av,0);
    1579             :       }
    1580             :     }
    1581             :   }
    1582         469 :   return gc_bool(av,1);
    1583             : }
    1584             : 
    1585             : int
    1586         392 : algiscommutative(GEN al) /* assumes e_1 = 1 */
    1587             : {
    1588             :   long i,j,k,N,sp;
    1589             :   GEN mt,a,b,p;
    1590         392 :   checkalg(al);
    1591         392 :   if (alg_type(al) != al_TABLE) return alg_get_degree(al)==1;
    1592         329 :   N = alg_get_absdim(al);
    1593         329 :   mt = alg_get_multable(al);
    1594         329 :   p = alg_get_char(al);
    1595         329 :   sp = signe(p);
    1596        1491 :   for (i=2; i<=N; i++)
    1597        9772 :     for (j=2; j<=N; j++)
    1598       89047 :       for (k=1; k<=N; k++) {
    1599       80514 :         a = gcoeff(gel(mt,i),k,j);
    1600       80514 :         b = gcoeff(gel(mt,j),k,i);
    1601       80514 :         if (sp) {
    1602       73423 :           if (cmpii(Fp_red(a,p), Fp_red(b,p))) return 0;
    1603             :         }
    1604        7091 :         else if (gcmp(a,b)) return 0;
    1605             :       }
    1606         252 :   return 1;
    1607             : }
    1608             : 
    1609             : int
    1610         392 : algissemisimple(GEN al)
    1611             : {
    1612         392 :   pari_sp av = avma;
    1613             :   GEN rad;
    1614         392 :   checkalg(al);
    1615         392 :   if (alg_type(al) != al_TABLE) return 1;
    1616         329 :   rad = algradical(al);
    1617         329 :   set_avma(av);
    1618         329 :   return gequal0(rad);
    1619             : }
    1620             : 
    1621             : /* ss : known to be semisimple */
    1622             : int
    1623         301 : algissimple(GEN al, long ss)
    1624             : {
    1625         301 :   pari_sp av = avma;
    1626             :   GEN Z, dec, p;
    1627         301 :   checkalg(al);
    1628         301 :   if (alg_type(al) != al_TABLE) return 1;
    1629         245 :   if (!ss && !algissemisimple(al)) return 0;
    1630             : 
    1631         203 :   p = alg_get_char(al);
    1632         203 :   if (signe(p)) Z = algprimesubalg(al);
    1633         112 :   else          Z = algtablecenter(al);
    1634             : 
    1635         203 :   if (lg(Z) == 2) {/* dim Z = 1 */
    1636         112 :     set_avma(av);
    1637         112 :     return 1;
    1638             :   }
    1639          91 :   dec = alg_decompose(al, Z, 1, NULL);
    1640          91 :   set_avma(av);
    1641          91 :   return gequal0(dec);
    1642             : }
    1643             : 
    1644             : static long
    1645         546 : is_place_emb(GEN nf, GEN pl)
    1646             : {
    1647             :   long r, r1, r2;
    1648         546 :   if (typ(pl) != t_INT) pari_err_TYPE("is_place_emb", pl);
    1649         525 :   if (signe(pl)<=0) pari_err_DOMAIN("is_place_emb", "pl", "<=", gen_0, pl);
    1650         518 :   nf_get_sign(nf,&r1,&r2); r = r1+r2;
    1651         518 :   if (cmpiu(pl,r)>0) pari_err_DOMAIN("is_place_emb", "pl", ">", utoi(r), pl);
    1652         497 :   return itou(pl);
    1653             : }
    1654             : 
    1655             : static long
    1656         497 : alghasse_emb(GEN al, long emb)
    1657             : {
    1658         497 :   GEN nf = alg_get_center(al);
    1659         497 :   long r1 = nf_get_r1(nf);
    1660         497 :   return (emb <= r1)? alg_get_hasse_i(al)[emb]: 0;
    1661             : }
    1662             : 
    1663             : static long
    1664        1799 : alghasse_pr(GEN al, GEN pr)
    1665             : {
    1666        1799 :   GEN hf = alg_get_hasse_f(al);
    1667        1792 :   long i = tablesearch(gel(hf,1), pr, &cmp_prime_ideal);
    1668        1792 :   return i? gel(hf,2)[i]: 0;
    1669             : }
    1670             : 
    1671             : static long
    1672        2380 : alghasse_0(GEN al, GEN pl)
    1673             : {
    1674             :   long ta;
    1675             :   GEN pr, nf;
    1676        2380 :   ta = alg_type(al);
    1677        2380 :   if (ta == al_REAL) return algreal_dim(al)!=1;
    1678        2359 :   if (!pl)
    1679           7 :     pari_err(e_MISC, "must provide a place pl");
    1680        2352 :   if (ta == al_CSA && !alg_is_asq(al))
    1681           7 :     pari_err_IMPL("computation of Hasse invariants over table CSA");
    1682        2345 :   if ((pr = get_prid(pl))) return alghasse_pr(al, pr);
    1683         546 :   nf = alg_get_center(al);
    1684         546 :   return alghasse_emb(al, is_place_emb(nf, pl));
    1685             : }
    1686             : GEN
    1687         343 : alghasse(GEN al, GEN pl)
    1688             : {
    1689             :   long h;
    1690         343 :   checkalg(al);
    1691         343 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("alghasse [use alginit]",al);
    1692         336 :   h = alghasse_0(al,pl);
    1693         280 :   return sstoQ(h, alg_get_degree(al));
    1694             : }
    1695             : 
    1696             : /* h >= 0, d >= 0 */
    1697             : static long
    1698        2219 : indexfromhasse(long h, long d) { return d/ugcd(h,d); }
    1699             : 
    1700             : long
    1701        2191 : algindex(GEN al, GEN pl)
    1702             : {
    1703             :   long d, res, i, l, ta;
    1704             :   GEN hi, hf;
    1705             : 
    1706        2191 :   checkalg(al);
    1707        2184 :   ta = alg_type(al);
    1708        2184 :   if (ta == al_TABLE) pari_err_TYPE("algindex [use alginit]",al);
    1709        2177 :   if (ta == al_REAL) return algreal_dim(al)==1 ? 1 : 2;
    1710        2093 :   d = alg_get_degree(al);
    1711        2093 :   if (pl) return indexfromhasse(alghasse_0(al,pl), d);
    1712             : 
    1713             :   /* else : global index */
    1714         273 :   res = 1;
    1715         273 :   hi = alg_get_hasse_i(al); l = lg(hi);
    1716         518 :   for (i=1; i<l && res!=d; i++) res = ulcm(res, indexfromhasse(hi[i],d));
    1717         273 :   hf = gel(alg_get_hasse_f(al), 2); l = lg(hf);
    1718         420 :   for (i=1; i<l && res!=d; i++) res = ulcm(res, indexfromhasse(hf[i],d));
    1719         266 :   return res;
    1720             : }
    1721             : 
    1722             : int
    1723         287 : algisdivision(GEN al, GEN pl)
    1724             : {
    1725         287 :   checkalg(al);
    1726         287 :   if (alg_type(al) == al_TABLE) {
    1727          21 :     if (!algissimple(al,0)) return 0;
    1728          14 :     if (algiscommutative(al)) return 1;
    1729           7 :     pari_err_IMPL("algisdivision for table algebras");
    1730             :   }
    1731         266 :   return algindex(al,pl) == alg_get_degree(al);
    1732             : }
    1733             : 
    1734             : int
    1735        1652 : algissplit(GEN al, GEN pl)
    1736             : {
    1737        1652 :   checkalg(al);
    1738        1652 :   if (alg_type(al) == al_TABLE) pari_err_TYPE("algissplit [use alginit]", al);
    1739        1638 :   return algindex(al,pl) == 1;
    1740             : }
    1741             : 
    1742             : int
    1743        1386 : algisramified(GEN al, GEN pl) { return !algissplit(al,pl); }
    1744             : 
    1745             : /* sorted; infinite places first */
    1746             : GEN
    1747         364 : algramifiedplaces(GEN al)
    1748             : {
    1749         364 :   pari_sp av = avma;
    1750             :   GEN ram, hf, hi, Lpr;
    1751             :   long r1, count, i, ta;
    1752         364 :   checkalg(al);
    1753         364 :   ta = alg_type(al);
    1754         364 :   if (ta != al_CSA && ta != al_CYCLIC)
    1755          14 :     pari_err_TYPE("algramifiedplaces [not a central simple algebra"
    1756             :         " over a number field]", al);
    1757         350 :   r1 = nf_get_r1(alg_get_center(al));
    1758         350 :   hi = alg_get_hasse_i(al);
    1759         336 :   hf = alg_get_hasse_f(al);
    1760         322 :   Lpr = gel(hf,1);
    1761         322 :   hf = gel(hf,2);
    1762         322 :   ram = cgetg(r1+lg(Lpr), t_VEC);
    1763         322 :   count = 0;
    1764         889 :   for (i=1; i<=r1; i++)
    1765         567 :     if (hi[i]) {
    1766         224 :       count++;
    1767         224 :       gel(ram,count) = stoi(i);
    1768             :     }
    1769        1059 :   for (i=1; i<lg(Lpr); i++)
    1770         737 :     if (hf[i]) {
    1771         322 :       count++;
    1772         322 :       gel(ram,count) = gel(Lpr,i);
    1773             :     }
    1774         322 :   setlg(ram, count+1);
    1775         322 :   return gc_GEN(av, ram);
    1776             : }
    1777             : 
    1778             : /* assume same degree and al_CYCLIC or al_CSA */
    1779             : static int
    1780         154 : algissimilar_i(GEN al, GEN al2, GEN pl)
    1781             : {
    1782             :   GEN ram, ram2;
    1783             :   long i, h;
    1784         154 :   if (pl)
    1785             :   {
    1786          70 :     h = alghasse_0(al2,pl);
    1787          56 :     return alghasse_0(al,pl) == h;
    1788             :   }
    1789          84 :   ram = algramifiedplaces(al);
    1790          63 :   ram2 = algramifiedplaces(al2);
    1791          63 :   if (!gequal(ram, ram2)) return 0;
    1792          84 :   for (i = 1; i < lg(ram); i++)
    1793             :   {
    1794          49 :     h = alghasse_0(al2,gel(ram,i));
    1795          49 :     if (alghasse_0(al,gel(ram,i)) != h) return 0;
    1796             :   }
    1797          35 :   return 1;
    1798             : }
    1799             : 
    1800             : int
    1801         245 : algisisom(GEN al, GEN al2, GEN pl)
    1802             : {
    1803         245 :   pari_sp av = avma;
    1804             :   long t, d;
    1805         245 :   checkalg(al);
    1806         238 :   checkalg(al2);
    1807         231 :   t = alg_type(al);
    1808         231 :   if (t != al_CYCLIC && t != al_CSA)
    1809          14 :     pari_err_TYPE("algisisom [al: apply alginit()]", al);
    1810         217 :   t = alg_type(al2);
    1811         217 :   if (t != al_CYCLIC && t != al_CSA)
    1812          14 :     pari_err_TYPE("algisisom [al2: apply alginit()]", al2);
    1813         203 :   if (!gequal(nf_get_pol(alg_get_center(al)), nf_get_pol(alg_get_center(al2))))
    1814           7 :     pari_err(e_MISC, "base fields must be identical in algisisom");
    1815         196 :   d = alg_get_degree(al);
    1816         196 :   if (d != alg_get_degree(al2)) return gc_int(av, 0);
    1817         189 :   if (d == 1) return gc_int(av, 1);
    1818         154 :   return gc_int(av, algissimilar_i(al,al2,pl));
    1819             : }
    1820             : 
    1821             : GEN
    1822          84 : algnewprec_shallow(GEN al, long prec)
    1823             : {
    1824             :   GEN al2;
    1825          84 :   long t = algtype(al);
    1826          84 :   if (t != al_CYCLIC && t != al_CSA) return al;
    1827          56 :   al2 = shallowcopy(al);
    1828          56 :   gel(al2,1) = rnfnewprec_shallow(gel(al2,1), prec);
    1829          56 :   return al2;
    1830             : };
    1831             : 
    1832             : GEN
    1833          84 : algnewprec(GEN al, long prec)
    1834             : {
    1835          84 :   pari_sp av = avma;
    1836          84 :   GEN al2 = algnewprec_shallow(al, prec);
    1837          84 :   return gc_GEN(av, al2);
    1838             : }
    1839             : 
    1840             : /** OPERATIONS ON ELEMENTS operations.c **/
    1841             : 
    1842             : static long
    1843     1896132 : alg_model0(GEN al, GEN x)
    1844             : {
    1845     1896132 :   long t, N = alg_get_absdim(al), lx = lg(x), d, n, D, i;
    1846     1896132 :   if (typ(x) == t_MAT) return al_MATRIX;
    1847     1850009 :   if (typ(x) != t_COL) return al_INVALID;
    1848     1849932 :   if (N == 1) {
    1849        7679 :     if (lx != 2) return al_INVALID;
    1850        7658 :     switch(typ(gel(x,1)))
    1851             :     {
    1852        4928 :       case t_INT: case t_FRAC: return al_TRIVIAL; /* cannot distinguish basis and alg from size */
    1853        2723 :       case t_POL: case t_POLMOD: return al_ALGEBRAIC;
    1854           7 :       default: return al_INVALID;
    1855             :     }
    1856             :   }
    1857             : 
    1858     1842253 :   switch(alg_type(al)) {
    1859      733858 :     case al_TABLE:
    1860      733858 :       if (lx != N+1) return al_INVALID;
    1861      733837 :       return al_BASIS;
    1862      942054 :     case al_CYCLIC:
    1863      942054 :       d = alg_get_degree(al);
    1864      942054 :       if (lx == N+1) return al_BASIS;
    1865      113135 :       if (lx == d+1) return al_ALGEBRAIC;
    1866          49 :       return al_INVALID;
    1867      166341 :     case al_CSA:
    1868      166341 :       D = alg_get_dim(al);
    1869      166341 :       n = nf_get_degree(alg_get_center(al));
    1870      166341 :       if (n == 1) {
    1871       22652 :         if (lx != D+1) return al_INVALID;
    1872      104433 :         for (i=1; i<=D; i++) {
    1873       84007 :           t = typ(gel(x,i));
    1874       84007 :           if (t == t_POL || t == t_POLMOD)  return al_ALGEBRAIC;
    1875             :             /* TODO t_COL for coefficients in basis form ? */
    1876             :         }
    1877       20426 :         return al_BASIS;
    1878             :       }
    1879             :       else {
    1880      143689 :         if (lx == N+1) return al_BASIS;
    1881       25809 :         if (lx == D+1) return al_ALGEBRAIC;
    1882           7 :         return al_INVALID;
    1883             :       }
    1884             :   }
    1885             :   return al_INVALID; /* LCOV_EXCL_LINE */
    1886             : }
    1887             : 
    1888             : static void
    1889     1895943 : checkalgx(GEN x, long model)
    1890             : {
    1891             :   long t, i;
    1892     1895943 :   switch(model) {
    1893     1701062 :     case al_BASIS:
    1894    22644279 :       for (i=1; i<lg(x); i++) {
    1895    20943224 :         t = typ(gel(x,i));
    1896    20943224 :         if (t != t_INT && t != t_FRAC)
    1897           7 :           pari_err_TYPE("checkalgx", gel(x,i));
    1898             :       }
    1899     1701055 :       return;
    1900      148758 :     case al_TRIVIAL:
    1901             :     case al_ALGEBRAIC:
    1902      503591 :       for (i=1; i<lg(x); i++) {
    1903      354840 :         t = typ(gel(x,i));
    1904      354840 :         if (t != t_INT && t != t_FRAC && t != t_POL && t != t_POLMOD)
    1905             :           /* TODO t_COL ? */
    1906           7 :           pari_err_TYPE("checkalgx", gel(x,i));
    1907             :       }
    1908      148751 :       return;
    1909             :   }
    1910             : }
    1911             : 
    1912             : long
    1913     1896132 : alg_model(GEN al, GEN x)
    1914             : {
    1915     1896132 :   long res = alg_model0(al, x);
    1916     1896132 :   if (res == al_INVALID) pari_err_TYPE("alg_model", x);
    1917     1895943 :   checkalgx(x, res); return res;
    1918             : }
    1919             : 
    1920             : static long
    1921      462910 : H_model0(GEN x)
    1922             : {
    1923             :   long i;
    1924      462910 :   switch(typ(x))
    1925             :   {
    1926       15274 :     case t_INT:
    1927             :     case t_FRAC:
    1928             :     case t_REAL:
    1929             :     case t_COMPLEX:
    1930       15274 :       return H_SCALAR;
    1931       10157 :     case t_MAT:
    1932       10157 :       return H_MATRIX;
    1933      437367 :     case t_COL:
    1934      437367 :       if (lg(x)!=5) return H_INVALID;
    1935     2186688 :       for (i=1; i<=4; i++) if (!is_real_t(typ(gel(x,i)))) return H_INVALID;
    1936      437332 :       return H_QUATERNION;
    1937         112 :     default:
    1938         112 :       return al_INVALID;
    1939             :   }
    1940             : }
    1941             : 
    1942             : static long
    1943      462910 : H_model(GEN x)
    1944             : {
    1945      462910 :   long res = H_model0(x);
    1946      462910 :   if (res == H_INVALID) pari_err_TYPE("H_model", x);
    1947      462763 :   return res;
    1948             : }
    1949             : 
    1950             : static GEN
    1951         756 : alC_add_i(GEN al, GEN x, GEN y, long lx)
    1952             : {
    1953         756 :   GEN A = cgetg(lx, t_COL);
    1954             :   long i;
    1955        2296 :   for (i=1; i<lx; i++) gel(A,i) = algadd(al, gel(x,i), gel(y,i));
    1956         749 :   return A;
    1957             : }
    1958             : static GEN
    1959         406 : alM_add(GEN al, GEN x, GEN y)
    1960             : {
    1961         406 :   long lx = lg(x), l, j;
    1962             :   GEN z;
    1963         406 :   if (lg(y) != lx) pari_err_DIM("alM_add (rows)");
    1964         392 :   if (lx == 1) return cgetg(1, t_MAT);
    1965         385 :   z = cgetg(lx, t_MAT); l = lgcols(x);
    1966         385 :   if (lgcols(y) != l) pari_err_DIM("alM_add (columns)");
    1967        1127 :   for (j = 1; j < lx; j++) gel(z,j) = alC_add_i(al, gel(x,j), gel(y,j), l);
    1968         371 :   return z;
    1969             : }
    1970             : static GEN
    1971       17745 : H_add(GEN x, GEN y)
    1972             : {
    1973       17745 :   long tx = H_model(x), ty = H_model(y);
    1974       17724 :   if ((tx==H_MATRIX) ^ (ty==H_MATRIX)) pari_err_TYPE2("H_add", x, y);
    1975       17710 :   if (tx>ty) { swap(x,y); lswap(tx,ty); }
    1976       17710 :   switch (tx)
    1977             :   {
    1978         105 :     case H_MATRIX: /* both H_MATRIX */ return alM_add(NULL, x, y);
    1979       16681 :     case H_QUATERNION: /* both H_QUATERNION */ return gadd(x,y);
    1980         924 :     case H_SCALAR:
    1981         924 :       if (ty == H_SCALAR) return gadd(x,y);
    1982             :       else /* ty == H_QUATERNION */
    1983             :       {
    1984         217 :         pari_sp av = avma;
    1985         217 :         GEN res = gcopy(y), im;
    1986         217 :         gel(res,1) = gadd(gel(res,1), real_i(x));
    1987         217 :         im = imag_i(x);
    1988         217 :         if (im != gen_0) gel(res,2) = gadd(gel(res,2), im);
    1989         217 :         return gc_upto(av, res);
    1990             :       }
    1991             :   }
    1992             :   return NULL; /*LCOV_EXCL_LINE*/
    1993             : }
    1994             : GEN
    1995       54999 : algadd(GEN al, GEN x, GEN y)
    1996             : {
    1997       54999 :   pari_sp av = avma;
    1998             :   long tx, ty;
    1999             :   GEN p;
    2000       54999 :   checkalg(al);
    2001       54999 :   if (alg_type(al)==al_REAL) return H_add(x,y);
    2002       37254 :   tx = alg_model(al,x);
    2003       37247 :   ty = alg_model(al,y);
    2004       37247 :   p = alg_get_char(al);
    2005       37247 :   if (signe(p)) return FpC_add(x,y,p);
    2006       37114 :   if (tx==ty) {
    2007       36232 :     if (tx!=al_MATRIX) return gadd(x,y);
    2008         301 :     return gc_GEN(av, alM_add(al,x,y));
    2009             :   }
    2010         882 :   if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2011         882 :   if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
    2012         882 :   return gc_upto(av, gadd(x,y));
    2013             : }
    2014             : 
    2015             : static GEN
    2016          98 : H_neg(GEN x)
    2017             : {
    2018          98 :   (void)H_model(x);
    2019          70 :   return gneg(x);
    2020             : }
    2021             : 
    2022             : GEN
    2023         245 : algneg(GEN al, GEN x)
    2024             : {
    2025         245 :   checkalg(al);
    2026         245 :   if (alg_type(al)==al_REAL) return H_neg(x);
    2027         147 :   (void)alg_model(al,x);
    2028         140 :   return gneg(x);
    2029             : }
    2030             : 
    2031             : static GEN
    2032         210 : alC_sub_i(GEN al, GEN x, GEN y, long lx)
    2033             : {
    2034             :   long i;
    2035         210 :   GEN A = cgetg(lx, t_COL);
    2036         630 :   for (i=1; i<lx; i++) gel(A,i) = algsub(al, gel(x,i), gel(y,i));
    2037         210 :   return A;
    2038             : }
    2039             : static GEN
    2040         126 : alM_sub(GEN al, GEN x, GEN y)
    2041             : {
    2042         126 :   long lx = lg(x), l, j;
    2043             :   GEN z;
    2044         126 :   if (lg(y) != lx) pari_err_DIM("alM_sub (rows)");
    2045         119 :   if (lx == 1) return cgetg(1, t_MAT);
    2046         112 :   z = cgetg(lx, t_MAT); l = lgcols(x);
    2047         112 :   if (lgcols(y) != l) pari_err_DIM("alM_sub (columns)");
    2048         315 :   for (j = 1; j < lx; j++) gel(z,j) = alC_sub_i(al, gel(x,j), gel(y,j), l);
    2049         105 :   return z;
    2050             : }
    2051             : GEN
    2052        1127 : algsub(GEN al, GEN x, GEN y)
    2053             : {
    2054             :   long tx, ty;
    2055        1127 :   pari_sp av = avma;
    2056             :   GEN p;
    2057        1127 :   checkalg(al);
    2058        1127 :   if (alg_type(al)==al_REAL) return gc_upto(av, algadd(NULL,x,gneg(y)));
    2059         973 :   tx = alg_model(al,x);
    2060         966 :   ty = alg_model(al,y);
    2061         966 :   p = alg_get_char(al);
    2062         966 :   if (signe(p)) return FpC_sub(x,y,p);
    2063         875 :   if (tx==ty) {
    2064         553 :     if (tx != al_MATRIX) return gsub(x,y);
    2065         126 :     return gc_GEN(av, alM_sub(al,x,y));
    2066             :   }
    2067         322 :   if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2068         322 :   if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
    2069         322 :   return gc_upto(av, gsub(x,y));
    2070             : }
    2071             : 
    2072             : static GEN
    2073        1659 : algalgmul_cyc(GEN al, GEN x, GEN y)
    2074             : {
    2075        1659 :   pari_sp av = avma;
    2076        1659 :   long n = alg_get_degree(al), i, k;
    2077             :   GEN xalg, yalg, res, rnf, auts, sum, b, prod, autx;
    2078        1659 :   rnf = alg_get_splittingfield(al);
    2079        1659 :   auts = alg_get_auts(al);
    2080        1659 :   b = alg_get_b(al);
    2081             : 
    2082        1659 :   xalg = cgetg(n+1, t_COL);
    2083        4935 :   for (i=0; i<n; i++)
    2084        3276 :     gel(xalg,i+1) = lift_shallow(rnfbasistoalg(rnf,gel(x,i+1)));
    2085             : 
    2086        1659 :   yalg = cgetg(n+1, t_COL);
    2087        4935 :   for (i=0; i<n; i++) gel(yalg,i+1) = rnfbasistoalg(rnf,gel(y,i+1));
    2088             : 
    2089        1659 :   res = cgetg(n+1,t_COL);
    2090        4935 :   for (k=0; k<n; k++) {
    2091        3276 :     gel(res,k+1) = gmul(gel(xalg,k+1),gel(yalg,1));
    2092        5166 :     for (i=1; i<=k; i++) {
    2093        1890 :       autx = poleval(gel(xalg,k-i+1),gel(auts,i));
    2094        1890 :       prod = gmul(autx,gel(yalg,i+1));
    2095        1890 :       gel(res,k+1) = gadd(gel(res,k+1), prod);
    2096             :     }
    2097             : 
    2098        3276 :     sum = gen_0;
    2099        5166 :     for (; i<n; i++) {
    2100        1890 :       autx = poleval(gel(xalg,k+n-i+1),gel(auts,i));
    2101        1890 :       prod = gmul(autx,gel(yalg,i+1));
    2102        1890 :       sum = gadd(sum,prod);
    2103             :     }
    2104        3276 :     sum = gmul(b,sum);
    2105             : 
    2106        3276 :     gel(res,k+1) = gadd(gel(res,k+1),sum);
    2107             :   }
    2108             : 
    2109        1659 :   return gc_GEN(av, res);
    2110             : }
    2111             : 
    2112             : static GEN
    2113      521822 : _tablemul(GEN mt, GEN x, GEN y)
    2114             : {
    2115      521822 :   pari_sp av = avma;
    2116      521822 :   long D = lg(mt)-1, i;
    2117      521822 :   GEN res = NULL;
    2118     8016946 :   for (i=1; i<=D; i++) {
    2119     7495124 :     GEN c = gel(x,i);
    2120     7495124 :     if (!gequal0(c)) {
    2121     1770670 :       GEN My = RgM_RgC_mul(gel(mt,i),y);
    2122     1770670 :       GEN t = RgC_Rg_mul(My,c);
    2123     1770670 :       res = res? RgC_add(res,t): t;
    2124             :     }
    2125             :   }
    2126      521822 :   if (!res) { set_avma(av); return zerocol(D); }
    2127      520912 :   return gc_upto(av, res);
    2128             : }
    2129             : 
    2130             : static GEN
    2131      299736 : _tablemul_Fp(GEN mt, GEN x, GEN y, GEN p)
    2132             : {
    2133      299736 :   pari_sp av = avma;
    2134      299736 :   long D = lg(mt)-1, i;
    2135      299736 :   GEN res = NULL;
    2136     2866309 :   for (i=1; i<=D; i++) {
    2137     2566573 :     GEN c = gel(x,i);
    2138     2566573 :     if (signe(c)) {
    2139      533518 :       GEN My = FpM_FpC_mul(gel(mt,i),y,p);
    2140      533518 :       GEN t = FpC_Fp_mul(My,c,p);
    2141      533518 :       res = res? FpC_add(res,t,p): t;
    2142             :     }
    2143             :   }
    2144      299736 :   if (!res) { set_avma(av); return zerocol(D); }
    2145      299197 :   return gc_upto(av, res);
    2146             : }
    2147             : 
    2148             : /* x*ej */
    2149             : static GEN
    2150      111811 : _tablemul_ej(GEN mt, GEN x, long j)
    2151             : {
    2152      111811 :   pari_sp av = avma;
    2153      111811 :   long D = lg(mt)-1, i;
    2154      111811 :   GEN res = NULL;
    2155     1707468 :   for (i=1; i<=D; i++) {
    2156     1595657 :     GEN c = gel(x,i);
    2157     1595657 :     if (!gequal0(c)) {
    2158      162302 :       GEN My = gel(gel(mt,i),j);
    2159      162302 :       GEN t = RgC_Rg_mul(My,c);
    2160      162302 :       res = res? RgC_add(res,t): t;
    2161             :     }
    2162             :   }
    2163      111811 :   if (!res) { set_avma(av); return zerocol(D); }
    2164      111629 :   return gc_upto(av, res);
    2165             : }
    2166             : static GEN
    2167      242039 : _tablemul_ej_Fp(GEN mt, GEN x, long j, GEN p)
    2168             : {
    2169      242039 :   pari_sp av = avma;
    2170      242039 :   long D = lg(mt)-1, i;
    2171      242039 :   GEN res = NULL;
    2172     4364787 :   for (i=1; i<=D; i++) {
    2173     4122748 :     GEN c = gel(x,i);
    2174     4122748 :     if (!gequal0(c)) {
    2175      289954 :       GEN My = gel(gel(mt,i),j);
    2176      289954 :       GEN t = FpC_Fp_mul(My,c,p);
    2177      289954 :       res = res? FpC_add(res,t,p): t;
    2178             :     }
    2179             :   }
    2180      242039 :   if (!res) { set_avma(av); return zerocol(D); }
    2181      241927 :   return gc_upto(av, res);
    2182             : }
    2183             : 
    2184             : static GEN
    2185      619387 : _tablemul_ej_Fl(GEN mt, GEN x, long j, ulong p)
    2186             : {
    2187      619387 :   pari_sp av = avma;
    2188      619387 :   long D = lg(mt)-1, i;
    2189      619387 :   GEN res = NULL;
    2190    13999108 :   for (i=1; i<=D; i++) {
    2191    13379721 :     ulong c = x[i];
    2192    13379721 :     if (c) {
    2193     1286016 :       GEN My = gel(gel(mt,i),j);
    2194     1286016 :       GEN t = Flv_Fl_mul(My,c, p);
    2195     1286016 :       res = res? Flv_add(res,t, p): t;
    2196             :     }
    2197             :   }
    2198      619387 :   if (!res) { set_avma(av); return zero_Flv(D); }
    2199      619387 :   return gc_upto(av, res);
    2200             : }
    2201             : 
    2202             : static GEN
    2203         686 : algalgmul_csa(GEN al, GEN x, GEN y)
    2204             : {
    2205         686 :   GEN z, nf = alg_get_center(al);
    2206             :   long i;
    2207         686 :   z = _tablemul(alg_get_relmultable(al), x, y);
    2208        2485 :   for (i=1; i<lg(z); i++)
    2209        1799 :     gel(z,i) = basistoalg(nf,gel(z,i));
    2210         686 :   return z;
    2211             : }
    2212             : 
    2213             : /* assumes x and y in algebraic form */
    2214             : static GEN
    2215        2345 : algalgmul(GEN al, GEN x, GEN y)
    2216             : {
    2217        2345 :   switch(alg_type(al))
    2218             :   {
    2219        1659 :     case al_CYCLIC: return algalgmul_cyc(al, x, y);
    2220         686 :     case al_CSA: return algalgmul_csa(al, x, y);
    2221             :   }
    2222             :   return NULL; /*LCOV_EXCL_LINE*/
    2223             : }
    2224             : 
    2225             : static GEN
    2226      820872 : algbasismul(GEN al, GEN x, GEN y)
    2227             : {
    2228      820872 :   GEN mt = alg_get_multable(al), p = alg_get_char(al);
    2229      820872 :   if (signe(p)) return _tablemul_Fp(mt, x, y, p);
    2230      521136 :   return _tablemul(mt, x, y);
    2231             : }
    2232             : 
    2233             : /* x[i,]*y. Assume lg(x) > 1 and 0 < i < lgcols(x) */
    2234             : static GEN
    2235      119651 : alMrow_alC_mul_i(GEN al, GEN x, GEN y, long i, long lx)
    2236             : {
    2237      119651 :   pari_sp av = avma;
    2238      119651 :   GEN c = algmul(al,gcoeff(x,i,1),gel(y,1)), ZERO;
    2239             :   long k;
    2240      119651 :   ZERO = zerocol(alg_get_absdim(al));
    2241      273308 :   for (k = 2; k < lx; k++)
    2242             :   {
    2243      153657 :     GEN t = algmul(al, gcoeff(x,i,k), gel(y,k));
    2244      153657 :     if (!gequal(t,ZERO)) c = algadd(al, c, t);
    2245             :   }
    2246      119651 :   return gc_GEN(av, c);
    2247             : }
    2248             : /* return x * y, 1 < lx = lg(x), l = lgcols(x) */
    2249             : static GEN
    2250       54502 : alM_alC_mul_i(GEN al, GEN x, GEN y, long lx, long l)
    2251             : {
    2252       54502 :   GEN z = cgetg(l,t_COL);
    2253             :   long i;
    2254      174153 :   for (i=1; i<l; i++) gel(z,i) = alMrow_alC_mul_i(al,x,y,i,lx);
    2255       54502 :   return z;
    2256             : }
    2257             : static GEN
    2258       25627 : alM_mul(GEN al, GEN x, GEN y)
    2259             : {
    2260       25627 :   long j, l, lx=lg(x), ly=lg(y);
    2261             :   GEN z;
    2262       25627 :   if (ly==1) return cgetg(1,t_MAT);
    2263       25529 :   if (lx != lgcols(y)) pari_err_DIM("alM_mul");
    2264       25508 :   if (lx==1) return zeromat(0, ly-1);
    2265       25501 :   l = lgcols(x); z = cgetg(ly,t_MAT);
    2266       80003 :   for (j=1; j<ly; j++) gel(z,j) = alM_alC_mul_i(al,x,gel(y,j),lx,l);
    2267       25501 :   return z;
    2268             : }
    2269             : 
    2270             : static void
    2271      205639 : H_compo(GEN x, GEN* a, GEN* b, GEN* c, GEN* d)
    2272             : {
    2273      205639 :   switch(H_model(x))
    2274             :   {
    2275        5173 :     case H_SCALAR:
    2276        5173 :       *a = real_i(x);
    2277        5173 :       *b = imag_i(x);
    2278        5173 :       *c = gen_0;
    2279        5173 :       *d = gen_0;
    2280        5173 :       return;
    2281      200466 :     case H_QUATERNION:
    2282      200466 :       *a = gel(x,1);
    2283      200466 :       *b = gel(x,2);
    2284      200466 :       *c = gel(x,3);
    2285      200466 :       *d = gel(x,4);
    2286      200466 :       return;
    2287             :     default: *a = *b = *c = *d = NULL; return; /*LCOV_EXCL_LINE*/
    2288             :   }
    2289             : }
    2290             : static GEN
    2291      108129 : H_mul(GEN x, GEN y)
    2292             : {
    2293      108129 :   pari_sp av = avma;
    2294             :   GEN a,b,c,d,u,v,w,z;
    2295      108129 :   long tx = H_model(x), ty = H_model(y);
    2296      108115 :   if ((tx==H_MATRIX) ^ (ty==H_MATRIX)) pari_err_TYPE2("H_mul", x, y);
    2297      108108 :   if (tx == H_MATRIX) /* both H_MATRIX */ return alM_mul(NULL, x, y);
    2298      103817 :   if (tx == H_SCALAR && ty == H_SCALAR) return gmul(x,y);
    2299      102620 :   H_compo(x,&a,&b,&c,&d);
    2300      102620 :   H_compo(y,&u,&v,&w,&z);
    2301      102620 :   return gc_GEN(av,mkcol4(
    2302             :         gsub(gmul(a,u), gadd(gadd(gmul(b,v),gmul(c,w)),gmul(d,z))),
    2303             :         gsub(gadd(gmul(a,v),gadd(gmul(b,u),gmul(c,z))), gmul(d,w)),
    2304             :         gsub(gadd(gmul(a,w),gadd(gmul(c,u),gmul(d,v))), gmul(b,z)),
    2305             :         gsub(gadd(gmul(a,z),gadd(gmul(b,w),gmul(d,u))), gmul(c,v))
    2306             :         ));
    2307             : }
    2308             : 
    2309             : GEN
    2310      821811 : algmul(GEN al, GEN x, GEN y)
    2311             : {
    2312      821811 :   pari_sp av = avma;
    2313             :   long tx, ty;
    2314      821811 :   checkalg(al);
    2315      821811 :   if (alg_type(al)==al_REAL) return H_mul(x,y);
    2316      713962 :   tx = alg_model(al,x);
    2317      713948 :   ty = alg_model(al,y);
    2318      713948 :   if (tx==al_MATRIX) {
    2319       20832 :     if (ty==al_MATRIX) return alM_mul(al,x,y);
    2320           7 :     pari_err_TYPE("algmul", y);
    2321             :   }
    2322      693116 :   if (signe(alg_get_char(al))) return algbasismul(al,x,y);
    2323      521150 :   if (tx==al_TRIVIAL) retmkcol(gmul(gel(x,1),gel(y,1)));
    2324      520450 :   if (tx==al_ALGEBRAIC && ty==al_ALGEBRAIC) return algalgmul(al,x,y);
    2325      518924 :   if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2326      518924 :   if (ty==al_ALGEBRAIC) y = algalgtobasis(al,y);
    2327      518924 :   return gc_upto(av,algbasismul(al,x,y));
    2328             : }
    2329             : 
    2330             : static GEN
    2331         329 : H_sqr(GEN x)
    2332             : {
    2333         329 :   pari_sp av = avma;
    2334         329 :   long tx = H_model(x);
    2335             :   GEN a,b,c,d;
    2336         308 :   if (tx == H_SCALAR) return gsqr(x);
    2337         224 :   if (tx == H_MATRIX) return H_mul(x,x);
    2338         119 :   H_compo(x,&a,&b,&c,&d);
    2339         119 :   return gc_GEN(av, mkcol4(
    2340             :         gsub(gsqr(a), gadd(gsqr(b),gadd(gsqr(c),gsqr(d)))),
    2341             :         gshift(gmul(a,b),1),
    2342             :         gshift(gmul(a,c),1),
    2343             :         gshift(gmul(a,d),1)
    2344             :         ));
    2345             : }
    2346             : 
    2347             : GEN
    2348      127056 : algsqr(GEN al, GEN x)
    2349             : {
    2350      127056 :   pari_sp av = avma;
    2351             :   long tx;
    2352      127056 :   checkalg(al);
    2353      127021 :   if (alg_type(al)==al_REAL) return H_sqr(x);
    2354      126692 :   tx = alg_model(al,x);
    2355      126622 :   if (tx==al_MATRIX) return gc_GEN(av,alM_mul(al,x,x));
    2356      126111 :   if (signe(alg_get_char(al))) return algbasismul(al,x,x);
    2357        3381 :   if (tx==al_TRIVIAL) retmkcol(gsqr(gel(x,1)));
    2358        3031 :   if (tx==al_ALGEBRAIC) return algalgmul(al,x,x);
    2359        2212 :   return gc_upto(av,algbasismul(al,x,x));
    2360             : }
    2361             : 
    2362             : static GEN
    2363       14399 : algmtK2Z_cyc(GEN al, GEN m)
    2364             : {
    2365       14399 :   pari_sp av = avma;
    2366       14399 :   GEN nf = alg_get_abssplitting(al), res, mt, rnf = alg_get_splittingfield(al), c, dc;
    2367       14399 :   long n = alg_get_degree(al), N = nf_get_degree(nf), Nn, i, j, i1, j1;
    2368       14399 :   Nn = N*n;
    2369       14399 :   res = zeromatcopy(Nn,Nn);
    2370       60312 :   for (i=0; i<n; i++)
    2371      247380 :   for (j=0; j<n; j++) {
    2372      201467 :     c = gcoeff(m,i+1,j+1);
    2373      201467 :     if (!gequal0(c)) {
    2374       45913 :       c = rnfeltreltoabs(rnf,c);
    2375       45913 :       c = algtobasis(nf,c);
    2376       45913 :       c = Q_remove_denom(c,&dc);
    2377       45913 :       mt = zk_multable(nf,c);
    2378       45913 :       if (dc) mt = ZM_Z_div(mt,dc);
    2379      384860 :       for (i1=1; i1<=N; i1++)
    2380     3464636 :       for (j1=1; j1<=N; j1++)
    2381     3125689 :         gcoeff(res,i*N+i1,j*N+j1) = gcoeff(mt,i1,j1);
    2382             :     }
    2383             :   }
    2384       14399 :   return gc_GEN(av,res);
    2385             : }
    2386             : 
    2387             : static GEN
    2388        1687 : algmtK2Z_csa(GEN al, GEN m)
    2389             : {
    2390        1687 :   pari_sp av = avma;
    2391        1687 :   GEN nf = alg_get_center(al), res, mt, c, dc;
    2392        1687 :   long d2 = alg_get_dim(al), n = nf_get_degree(nf), D, i, j, i1, j1;
    2393        1687 :   D = d2*n;
    2394        1687 :   res = zeromatcopy(D,D);
    2395        9086 :   for (i=0; i<d2; i++)
    2396       45206 :   for (j=0; j<d2; j++) {
    2397       37807 :     c = gcoeff(m,i+1,j+1);
    2398       37807 :     if (!gequal0(c)) {
    2399        7035 :       c = algtobasis(nf,c);
    2400        7035 :       c = Q_remove_denom(c,&dc);
    2401        7035 :       mt = zk_multable(nf,c);
    2402        7035 :       if (dc) mt = ZM_Z_div(mt,dc);
    2403       22064 :       for (i1=1; i1<=n; i1++)
    2404       50414 :       for (j1=1; j1<=n; j1++)
    2405       35385 :         gcoeff(res,i*n+i1,j*n+j1) = gcoeff(mt,i1,j1);
    2406             :     }
    2407             :   }
    2408        1687 :   return gc_GEN(av,res);
    2409             : }
    2410             : 
    2411             : /* assumes al is a CSA or CYCLIC */
    2412             : static GEN
    2413       16086 : algmtK2Z(GEN al, GEN m)
    2414             : {
    2415       16086 :   switch(alg_type(al))
    2416             :   {
    2417       14399 :     case al_CYCLIC: return algmtK2Z_cyc(al, m);
    2418        1687 :     case al_CSA: return algmtK2Z_csa(al, m);
    2419             :   }
    2420             :   return NULL; /*LCOV_EXCL_LINE*/
    2421             : }
    2422             : 
    2423             : /* left multiplication table, as a vector space of dimension n over the splitting field (by right multiplication) */
    2424             : static GEN
    2425       17164 : algalgmultable_cyc(GEN al, GEN x)
    2426             : {
    2427       17164 :   pari_sp av = avma;
    2428       17164 :   long n = alg_get_degree(al), i, j;
    2429             :   GEN res, rnf, auts, b, pol;
    2430       17164 :   rnf = alg_get_splittingfield(al);
    2431       17164 :   auts = alg_get_auts(al);
    2432       17164 :   b = alg_get_b(al);
    2433       17164 :   pol = rnf_get_pol(rnf);
    2434             : 
    2435       17164 :   res = zeromatcopy(n,n);
    2436       68663 :   for (i=0; i<n; i++)
    2437       51499 :     gcoeff(res,i+1,1) = lift_shallow(rnfbasistoalg(rnf,gel(x,i+1)));
    2438             : 
    2439       68663 :   for (i=0; i<n; i++) {
    2440      132405 :     for (j=1; j<=i; j++)
    2441       80906 :       gcoeff(res,i+1,j+1) = gmodulo(poleval(gcoeff(res,i-j+1,1),gel(auts,j)),pol);
    2442      132405 :     for (; j<n; j++)
    2443       80906 :       gcoeff(res,i+1,j+1) = gmodulo(gmul(b,poleval(gcoeff(res,n+i-j+1,1),gel(auts,j))), pol);
    2444             :   }
    2445             : 
    2446       68663 :   for (i=0; i<n; i++)
    2447       51499 :     gcoeff(res,i+1,1) = gmodulo(gcoeff(res,i+1,1),pol);
    2448             : 
    2449       17164 :   return gc_GEN(av, res);
    2450             : }
    2451             : 
    2452             : static GEN
    2453        2170 : elementmultable(GEN mt, GEN x)
    2454             : {
    2455        2170 :   pari_sp av = avma;
    2456        2170 :   long D = lg(mt)-1, i;
    2457        2170 :   GEN z = NULL;
    2458       11207 :   for (i=1; i<=D; i++)
    2459             :   {
    2460        9037 :     GEN c = gel(x,i);
    2461        9037 :     if (!gequal0(c))
    2462             :     {
    2463        2961 :       GEN M = RgM_Rg_mul(gel(mt,i),c);
    2464        2961 :       z = z? RgM_add(z, M): M;
    2465             :     }
    2466             :   }
    2467        2170 :   if (!z) { set_avma(av); return zeromatcopy(D,D); }
    2468        2170 :   return gc_upto(av, z);
    2469             : }
    2470             : /* mt a t_VEC of Flm modulo m */
    2471             : static GEN
    2472       51360 : algbasismultable_Flm(GEN mt, GEN x, ulong m)
    2473             : {
    2474       51360 :   pari_sp av = avma;
    2475       51360 :   long D = lg(gel(mt,1))-1, i;
    2476       51360 :   GEN z = NULL;
    2477      670747 :   for (i=1; i<=D; i++)
    2478             :   {
    2479      619387 :     ulong c = x[i];
    2480      619387 :     if (c)
    2481             :     {
    2482       80682 :       GEN M = Flm_Fl_mul(gel(mt,i),c, m);
    2483       80682 :       z = z? Flm_add(z, M, m): M;
    2484             :     }
    2485             :   }
    2486       51360 :   if (!z) { set_avma(av); return zero_Flm(D,D); }
    2487       51360 :   return gc_upto(av, z);
    2488             : }
    2489             : static GEN
    2490      367854 : elementabsmultable_Z(GEN mt, GEN x)
    2491             : {
    2492      367854 :   long i, l = lg(x);
    2493      367854 :   GEN z = NULL;
    2494     4271365 :   for (i = 1; i < l; i++)
    2495             :   {
    2496     3903511 :     GEN c = gel(x,i);
    2497     3903511 :     if (signe(c))
    2498             :     {
    2499     1113760 :       GEN M = ZM_Z_mul(gel(mt,i),c);
    2500     1113760 :       z = z? ZM_add(z, M): M;
    2501             :     }
    2502             :   }
    2503      367854 :   return z;
    2504             : }
    2505             : static GEN
    2506      159050 : elementabsmultable(GEN mt, GEN x)
    2507             : {
    2508      159050 :   GEN d, z = elementabsmultable_Z(mt, Q_remove_denom(x,&d));
    2509      159050 :   return (z && d)? ZM_Z_div(z, d): z;
    2510             : }
    2511             : static GEN
    2512      208804 : elementabsmultable_Fp(GEN mt, GEN x, GEN p)
    2513             : {
    2514      208804 :   GEN z = elementabsmultable_Z(mt, x);
    2515      208804 :   return z? FpM_red(z, p): z;
    2516             : }
    2517             : static GEN
    2518      367854 : algbasismultable(GEN al, GEN x)
    2519             : {
    2520      367854 :   pari_sp av = avma;
    2521      367854 :   GEN z, p = alg_get_char(al), mt = alg_get_multable(al);
    2522      367854 :   z = signe(p)? elementabsmultable_Fp(mt, x, p): elementabsmultable(mt, x);
    2523      367854 :   if (!z)
    2524             :   {
    2525        4318 :     long D = lg(mt)-1;
    2526        4318 :     set_avma(av); return zeromat(D,D);
    2527             :   }
    2528      363536 :   return gc_upto(av, z);
    2529             : }
    2530             : 
    2531             : static GEN
    2532        2170 : algalgmultable_csa(GEN al, GEN x)
    2533             : {
    2534        2170 :   GEN nf = alg_get_center(al), m;
    2535             :   long i,j;
    2536        2170 :   m = elementmultable(alg_get_relmultable(al), x);
    2537       11207 :   for (i=1; i<lg(m); i++)
    2538       53102 :     for(j=1; j<lg(m); j++)
    2539       44065 :       gcoeff(m,i,j) = basistoalg(nf,gcoeff(m,i,j));
    2540        2170 :   return m;
    2541             : }
    2542             : 
    2543             : /* assumes x in algebraic form */
    2544             : static GEN
    2545       19005 : algalgmultable(GEN al, GEN x)
    2546             : {
    2547       19005 :   switch(alg_type(al))
    2548             :   {
    2549       17164 :     case al_CYCLIC: return algalgmultable_cyc(al, x);
    2550        1841 :     case al_CSA: return algalgmultable_csa(al, x);
    2551             :   }
    2552             :   return NULL; /*LCOV_EXCL_LINE*/
    2553             : }
    2554             : 
    2555             : /* on the natural basis */
    2556             : /* assumes x in algebraic form */
    2557             : static GEN
    2558       16086 : algZmultable(GEN al, GEN x) {
    2559       16086 :   pari_sp av = avma;
    2560       16086 :   return gc_upto(av, algmtK2Z(al,algalgmultable(al,x)));
    2561             : }
    2562             : 
    2563             : /* x integral */
    2564             : static GEN
    2565       41265 : algbasisrightmultable(GEN al, GEN x)
    2566             : {
    2567       41265 :   long N = alg_get_absdim(al), i,j,k;
    2568       41265 :   GEN res = zeromatcopy(N,N), c, mt = alg_get_multable(al), p = alg_get_char(al);
    2569       41265 :   if (gequal0(p)) p = NULL;
    2570      374591 :   for (i=1; i<=N; i++) {
    2571      333326 :     c = gel(x,i);
    2572      333326 :     if (!gequal0(c)) {
    2573     1320097 :       for (j=1; j<=N; j++)
    2574    20118318 :       for(k=1; k<=N; k++) {
    2575    18918764 :         if (p) gcoeff(res,k,j) = Fp_add(gcoeff(res,k,j), Fp_mul(c, gcoeff(gel(mt,j),k,i), p), p);
    2576    14262724 :         else gcoeff(res,k,j) = addii(gcoeff(res,k,j), mulii(c, gcoeff(gel(mt,j),k,i)));
    2577             :       }
    2578             :     }
    2579             :   }
    2580       41265 :   return res;
    2581             : }
    2582             : 
    2583             : /* central simple algebra al from alginit */
    2584             : /* right multiplication table on integral basis; no checks no GC */
    2585             : static GEN
    2586          77 : algrightmultable(GEN al, GEN x)
    2587             : {
    2588             :   GEN d, M;
    2589          77 :   x = algalgtobasis(al, x);
    2590          70 :   x = Q_remove_denom(x, &d);
    2591          70 :   M = algbasisrightmultable(al,x);
    2592          70 :   return d ? ZM_Z_div(M,d) : M;
    2593             : }
    2594             : 
    2595             : /* basis for matrices : 1, E_{i,j} for (i,j)!=(1,1) */
    2596             : /* index : ijk = ((i-1)*N+j-1)*n + k */
    2597             : /* square matrices only, coefficients in basis form, shallow function */
    2598             : static GEN
    2599       23961 : algmat2basis(GEN al, GEN M)
    2600             : {
    2601       23961 :   long n = alg_get_absdim(al), N = lg(M)-1, i, j, k, ij, ijk;
    2602             :   GEN res, x;
    2603       23961 :   res = zerocol(N*N*n);
    2604       75131 :   for (i=1; i<=N; i++) {
    2605      163310 :     for (j=1, ij=(i-1)*N+1; j<=N; j++, ij++) {
    2606      112140 :       x = gcoeff(M,i,j);
    2607      819532 :       for (k=1, ijk=(ij-1)*n+1; k<=n; k++, ijk++) {
    2608      707392 :         gel(res, ijk) = gel(x, k);
    2609      707392 :         if (i>1 && i==j) gel(res, ijk) = gsub(gel(res,ijk), gel(res,k));
    2610             :       }
    2611             :     }
    2612             :   }
    2613             : 
    2614       23961 :   return res;
    2615             : }
    2616             : 
    2617             : static GEN
    2618         294 : algbasis2mat(GEN al, GEN M, long N)
    2619             : {
    2620         294 :   long n = alg_get_absdim(al), i, j, k, ij, ijk;
    2621             :   GEN res, x;
    2622         294 :   res = zeromatcopy(N,N);
    2623         882 :   for (i=1; i<=N; i++)
    2624        1764 :   for (j=1; j<=N; j++)
    2625        1176 :     gcoeff(res,i,j) = zerocol(n);
    2626             : 
    2627         882 :   for (i=1; i<=N; i++) {
    2628        1764 :     for (j=1, ij=(i-1)*N+1; j<=N; j++, ij++) {
    2629        1176 :       x = gcoeff(res,i,j);
    2630        9240 :       for (k=1, ijk=(ij-1)*n+1; k<=n; k++, ijk++) {
    2631        8064 :         gel(x,k) = gel(M,ijk);
    2632        8064 :         if (i>1 && i==j) gel(x,k) = gadd(gel(x,k), gel(M,k));
    2633             :       }
    2634             :     }
    2635             :   }
    2636             : 
    2637         294 :   return res;
    2638             : }
    2639             : 
    2640             : static GEN
    2641       23884 : algmatbasis_ei(GEN al, long ijk, long N)
    2642             : {
    2643       23884 :   long n = alg_get_absdim(al), i, j, k, ij;
    2644             :   GEN res;
    2645             : 
    2646       23884 :   res = zeromatcopy(N,N);
    2647       74900 :   for (i=1; i<=N; i++)
    2648      162848 :   for (j=1; j<=N; j++)
    2649      111832 :     gcoeff(res,i,j) = zerocol(n);
    2650             : 
    2651       23884 :   k = ijk%n;
    2652       23884 :   if (k==0) k=n;
    2653       23884 :   ij = (ijk-k)/n+1;
    2654             : 
    2655       23884 :   if (ij==1) {
    2656       16947 :     for (i=1; i<=N; i++)
    2657       11410 :       gcoeff(res,i,i) = col_ei(n,k);
    2658        5537 :     return res;
    2659             :   }
    2660             : 
    2661       18347 :   j = ij%N;
    2662       18347 :   if (j==0) j=N;
    2663       18347 :   i = (ij-j)/N+1;
    2664             : 
    2665       18347 :   gcoeff(res,i,j) = col_ei(n,k);
    2666       18347 :   return res;
    2667             : }
    2668             : 
    2669             : /* FIXME lazy implementation! */
    2670             : static GEN
    2671         910 : algleftmultable_mat(GEN al, GEN M)
    2672             : {
    2673         910 :   long N = lg(M)-1, n = alg_get_absdim(al), D = N*N*n, j;
    2674             :   GEN res, x, Mx;
    2675         910 :   if (N == 0) return cgetg(1, t_MAT);
    2676         903 :   if (N != nbrows(M)) pari_err_DIM("algleftmultable_mat (nonsquare)");
    2677         882 :   res = cgetg(D+1, t_MAT);
    2678       24766 :   for (j=1; j<=D; j++) {
    2679       23884 :     x = algmatbasis_ei(al, j, N);
    2680       23884 :     Mx = algmul(al, M, x);
    2681       23884 :     gel(res, j) = algmat2basis(al, Mx);
    2682             :   }
    2683         882 :   return res;
    2684             : }
    2685             : 
    2686             : /* left multiplication table on integral basis */
    2687             : static GEN
    2688       23660 : algleftmultable(GEN al, GEN x)
    2689             : {
    2690       23660 :   pari_sp av = avma;
    2691             :   long tx;
    2692             :   GEN res;
    2693             : 
    2694       23660 :   checkalg(al);
    2695       23660 :   tx = alg_model(al,x);
    2696       23639 :   switch(tx) {
    2697         994 :     case al_TRIVIAL : res = mkmatcopy(mkcol(gel(x,1))); break;
    2698         280 :     case al_ALGEBRAIC : x = algalgtobasis(al,x);
    2699       22127 :     case al_BASIS : res = algbasismultable(al,x); break;
    2700         518 :     case al_MATRIX : res = algleftmultable_mat(al,x); break;
    2701             :     default : return NULL; /* LCOV_EXCL_LINE */
    2702             :   }
    2703       23632 :   return gc_upto(av,res);
    2704             : }
    2705             : 
    2706             : static GEN
    2707        4347 : algbasissplittingmatrix_csa(GEN al, GEN x)
    2708             : {
    2709        4347 :   long d = alg_get_degree(al), i, j;
    2710        4347 :   GEN rnf = alg_get_splittingfield(al), splba = alg_get_splittingbasis(al), splbainv = alg_get_splittingbasisinv(al), M;
    2711        4347 :   M = algbasismultable(al,x);
    2712        4347 :   M = RgM_mul(M, splba); /* TODO best order ? big matrix /Q vs small matrix /nf */
    2713        4347 :   M = RgM_mul(splbainv, M);
    2714       12852 :   for (i=1; i<=d; i++)
    2715       25326 :   for (j=1; j<=d; j++)
    2716       16821 :     gcoeff(M,i,j) = rnfeltabstorel(rnf, gcoeff(M,i,j));
    2717        4347 :   return M;
    2718             : }
    2719             : 
    2720             : static GEN
    2721         728 : algmat_tomatrix(GEN al, GEN x) /* abs = 0 */
    2722             : {
    2723             :   GEN res;
    2724             :   long i,j;
    2725         728 :   if (lg(x) == 1) return cgetg(1, t_MAT);
    2726         700 :   res = zeromatcopy(nbrows(x),lg(x)-1);
    2727        2212 :   for (j=1; j<lg(x); j++)
    2728        4879 :   for (i=1; i<lgcols(x); i++)
    2729        3367 :     gcoeff(res,i,j) = algtomatrix(al,gcoeff(x,i,j),0);
    2730         700 :   return shallowmatconcat(res);
    2731             : }
    2732             : 
    2733             : static GEN
    2734          42 : R_tomatrix(GEN x)
    2735             : {
    2736          42 :   long t = H_model(x);
    2737          42 :   if (t == H_QUATERNION) pari_err_TYPE("R_tomatrix", x);
    2738          35 :   if (t == H_MATRIX) return x;
    2739          21 :   return mkmat(mkcol(x));
    2740             : }
    2741             : static GEN
    2742          84 : C_tomatrix(GEN z, long abs)
    2743             : {
    2744             :   GEN x,y;
    2745          84 :   long t = H_model(z), nrows, ncols;
    2746          84 :   if (t == H_QUATERNION) pari_err_TYPE("C_tomatrix", z);
    2747          77 :   if (!abs)
    2748             :   {
    2749          14 :     if (t == H_MATRIX) return z;
    2750           7 :     return mkmat(mkcol(z));
    2751             :   }
    2752          63 :   if (t == H_MATRIX)
    2753             :   {
    2754             :     /* Warning: this is not the same choice of basis as for other algebras */
    2755             :     GEN res, a, b;
    2756             :     long i,j;
    2757          56 :     RgM_dimensions(z,&nrows,&ncols);
    2758          56 :     res = zeromatcopy(2*nrows,2*ncols);
    2759         168 :     for (i=1; i<=nrows; i++)
    2760         336 :       for (j=1; j<=ncols; j++)
    2761             :       {
    2762         224 :         a = real_i(gcoeff(z,i,j));
    2763         224 :         b = imag_i(gcoeff(z,i,j));
    2764         224 :         gcoeff(res,2*i-1,2*j-1) = a;
    2765         224 :         gcoeff(res,2*i,2*j) = a;
    2766         224 :         gcoeff(res,2*i-1,2*j) = gneg(b);
    2767         224 :         gcoeff(res,2*i,2*j-1) = b;
    2768             :       }
    2769          56 :     return res;
    2770             :   }
    2771           7 :   x = real_i(z);
    2772           7 :   y = imag_i(z);
    2773           7 :   return mkmat22(x,gneg(y),y,x);
    2774             : }
    2775             : static GEN
    2776        2457 : H_tomatrix(GEN x, long abs)
    2777             : {
    2778        2457 :   long tx = H_model(x);
    2779        2450 :   GEN a = NULL, b =NULL, c = NULL, d = NULL, md = NULL, M = NULL;
    2780        2450 :   if (abs) {
    2781         413 :     if (tx == H_MATRIX) return algleftmultable_mat(NULL,x);
    2782         280 :     switch(tx)
    2783             :     {
    2784          77 :       case H_SCALAR:
    2785          77 :         a = real_i(x);
    2786          77 :         b = imag_i(x);
    2787          77 :         c = gen_0;
    2788          77 :         d = gen_0;
    2789          77 :         break;
    2790         203 :       case H_QUATERNION:
    2791         203 :         a = gel(x,1);
    2792         203 :         b = gel(x,2);
    2793         203 :         c = gel(x,3);
    2794         203 :         d = gel(x,4);
    2795         203 :         break;
    2796             :     }
    2797         280 :     M = scalarmat(a,4);
    2798         280 :     gcoeff(M,2,1) = gcoeff(M,4,3) = b;
    2799         280 :     gcoeff(M,1,2) = gcoeff(M,3,4) = gneg(b);
    2800         280 :     gcoeff(M,3,1) = gcoeff(M,2,4) = c;
    2801         280 :     gcoeff(M,4,2) = gcoeff(M,1,3) = gneg(c);
    2802         280 :     gcoeff(M,4,1) = gcoeff(M,3,2) = d;
    2803         280 :     gcoeff(M,2,3) = gcoeff(M,1,4) = gneg(d);
    2804             :   }
    2805             :   else /* abs == 0 */
    2806             :   {
    2807        2037 :     if (tx == H_MATRIX) return algmat_tomatrix(NULL,x);
    2808        1778 :     switch(tx)
    2809             :     {
    2810         273 :       case H_SCALAR:
    2811         273 :         M = mkmat22(
    2812             :             x,      gen_0,
    2813             :             gen_0,  conj_i(x)
    2814             :             );
    2815         273 :         break;
    2816        1505 :       case H_QUATERNION:
    2817        1505 :         a = gel(x,1);
    2818        1505 :         b = gel(x,2);
    2819        1505 :         c = gel(x,3);
    2820        1505 :         md = gneg(gel(x,4));
    2821        1505 :         M = mkmat22(
    2822             :             mkcomplex(a,b),     mkcomplex(gneg(c),md),
    2823             :             mkcomplex(c,md),    mkcomplex(a,gneg(b))
    2824             :             );
    2825             :     }
    2826             :   }
    2827        2058 :   return M;
    2828             : }
    2829             : 
    2830             : GEN
    2831       25109 : algtomatrix(GEN al, GEN x, long abs)
    2832             : {
    2833       25109 :   pari_sp av = avma;
    2834       25109 :   GEN res = NULL;
    2835             :   long ta, tx;
    2836       25109 :   checkalg(al);
    2837       25109 :   ta = alg_type(al);
    2838       25109 :   if (ta==al_REAL)
    2839             :   {
    2840        2268 :     switch(alg_get_absdim(al)) {
    2841          42 :       case 1: res = R_tomatrix(x); break;
    2842          84 :       case 2: res = C_tomatrix(x,abs); break;
    2843        2135 :       case 4: res = H_tomatrix(x,abs); break;
    2844           7 :       default: pari_err_TYPE("algtomatrix [apply alginit]", al);
    2845             :     }
    2846        2240 :     return gc_GEN(av, res);
    2847             :   }
    2848       22841 :   if (abs || ta==al_TABLE) return algleftmultable(al,x);
    2849        7014 :   tx = alg_model(al,x);
    2850        7014 :   if (tx == al_MATRIX) res = algmat_tomatrix(al,x);
    2851        6545 :   else switch (alg_type(al))
    2852             :   {
    2853        2198 :     case al_CYCLIC:
    2854        2198 :       if (tx==al_BASIS) x = algbasistoalg(al,x);
    2855        2198 :       res = algalgmultable(al,x);
    2856        2198 :       break;
    2857        4347 :     case al_CSA:
    2858        4347 :       if (tx==al_ALGEBRAIC) x = algalgtobasis(al,x);
    2859        4347 :       res = algbasissplittingmatrix_csa(al,x);
    2860        4347 :       break;
    2861             :     default: return NULL; /*LCOV_EXCL_LINE*/
    2862             :   }
    2863        7014 :   return gc_GEN(av,res);
    2864             : }
    2865             : 
    2866             : /*  x^(-1)*y, NULL if no solution */
    2867             : static GEN
    2868         112 : C_divl_i(GEN x, GEN y)
    2869             : {
    2870         112 :   long tx = H_model(x), ty = H_model(y);
    2871         112 :   if (tx != ty) pari_err_TYPE2("C_divl", x, y);
    2872         105 :   switch (tx) {
    2873          42 :     case H_SCALAR:
    2874          42 :       if (gequal0(x)) return gequal0(y) ? gen_0 : NULL;
    2875          14 :       else return gdiv(y,x);
    2876          56 :     case H_MATRIX:
    2877          56 :       if ((lg(x)>1 && lg(x) != lgcols(x)) || (lg(y)>1 && lg(y) != lgcols(y)))
    2878           7 :         pari_err_DIM("C_divl (nonsquare)");
    2879          49 :       if (lg(x) != lg(y)) pari_err_DIM("C_divl");
    2880          42 :       if (lg(y) == 1) return cgetg(1, t_MAT);
    2881          42 :       return RgM_invimage(x, y);
    2882           7 :     default: pari_err_TYPE("C_divl", x); return NULL;
    2883             :   }
    2884             : }
    2885             : /* H^k -> C^2k */
    2886             : static GEN
    2887         140 : HC_to_CC(GEN v)
    2888             : {
    2889         140 :   long l = lg(v), i;
    2890         140 :   GEN w = cgetg(2*l-1, t_COL), a, b, c, d;
    2891         420 :   for (i=1; i<l; i++)
    2892             :   {
    2893         280 :     H_compo(gel(v,i),&a,&b,&c,&d);
    2894         280 :     gel(w,2*i-1) = mkcomplex(a,b);
    2895         280 :     gel(w,2*i) = mkcomplex(c,gneg(d));
    2896             :   }
    2897         140 :   return w;
    2898             : }
    2899             : /* C^2k -> H^k */
    2900             : static GEN
    2901          98 : CC_to_HC(GEN w)
    2902             : {
    2903          98 :   long l = lg(w), i, lv = (l+1)/2;
    2904          98 :   GEN v = cgetg(lv, t_COL), ab, cd;
    2905         294 :   for (i=1; i<lv; i++)
    2906             :   {
    2907         196 :     ab = gel(w,2*i-1);
    2908         196 :     cd = gel(w,2*i);
    2909         196 :     gel(v,i) = mkcol4(real_i(ab),imag_i(ab),real_i(cd),gneg(imag_i(cd)));
    2910             :   }
    2911          98 :   return v;
    2912             : }
    2913             : /* M_{k,n}(H) -> M_{2k,n}(C) */
    2914             : static GEN
    2915         210 : HM_to_CM(GEN x) pari_APPLY_same(HC_to_CC(gel(x,i)));
    2916             : /* M_{2k,n}(C) -> M_{k,n}(H) */
    2917             : static GEN
    2918         147 : CM_to_HM(GEN x) pari_APPLY_same(CC_to_HC(gel(x,i)));
    2919             : /*  x^(-1)*y, NULL if no solution */
    2920             : static GEN
    2921         203 : H_divl_i(GEN x, GEN y)
    2922             : {
    2923         203 :   pari_sp av = avma;
    2924         203 :   long tx = H_model(x), ty = H_model(y);
    2925         189 :   if ((tx==H_MATRIX) ^ (ty==H_MATRIX)) pari_err_TYPE2("H_divl", x, y);
    2926         168 :   if (tx==H_MATRIX)
    2927             :   {
    2928             :     GEN mx, my, mxdivy;
    2929          98 :     if ((lg(x)>1 && lg(x) != lgcols(x)) || (lg(y)>1 && lg(y) != lgcols(y)))
    2930          14 :       pari_err_DIM("H_divl (nonsquare)");
    2931          84 :     if (lg(x) != lg(y)) pari_err_DIM("H_divl");
    2932          77 :     if (lg(y) == 1) return cgetg(1, t_MAT);
    2933          70 :     mx = H_tomatrix(x,0);
    2934          70 :     my = HM_to_CM(y);
    2935          70 :     mxdivy = RgM_invimage(mx, my);
    2936          70 :     if (!mxdivy) return gc_NULL(av);
    2937          49 :     return gc_GEN(av,CM_to_HM(mxdivy));
    2938             :   }
    2939          70 :   if (gequal0(y)) return gen_0;
    2940          56 :   if (gequal0(x)) return NULL;
    2941          42 :   return gc_GEN(av,H_mul(H_inv(x),y));
    2942             : }
    2943             : /*  x^(-1)*y, NULL if no solution */
    2944             : static GEN
    2945        3199 : algdivl_i(GEN al, GEN x, GEN y, long tx, long ty) {
    2946        3199 :   pari_sp av = avma;
    2947        3199 :   GEN res, p = alg_get_char(al), mtx;
    2948        3199 :   if (tx != ty) {
    2949         343 :     if (tx==al_ALGEBRAIC) { x = algalgtobasis(al,x); tx=al_BASIS; }
    2950         343 :     if (ty==al_ALGEBRAIC) { y = algalgtobasis(al,y); ty=al_BASIS; }
    2951             :   }
    2952        3199 :   if (ty == al_MATRIX)
    2953             :   {
    2954          77 :     if (alg_type(al) != al_TABLE) y = algalgtobasis(al,y);
    2955          77 :     y = algmat2basis(al,y);
    2956             :   }
    2957        3199 :   if (signe(p)) res = FpM_FpC_invimage(algbasismultable(al,x),y,p);
    2958             :   else
    2959             :   {
    2960        3010 :     if (ty==al_ALGEBRAIC)   mtx = algalgmultable(al,x);
    2961        2303 :     else                    mtx = algleftmultable(al,x);
    2962        3010 :     res = inverseimage(mtx,y);
    2963             :   }
    2964        3199 :   if (!res || lg(res)==1) return gc_NULL(av);
    2965        1764 :   if (tx == al_MATRIX) {
    2966         294 :     res = algbasis2mat(al, res, lg(x)-1);
    2967         294 :     return gc_GEN(av,res);
    2968             :   }
    2969        1470 :   return gc_upto(av,res);
    2970             : }
    2971             : static GEN
    2972        1015 : algdivl_i2(GEN al, GEN x, GEN y)
    2973             : {
    2974             :   long tx, ty;
    2975        1015 :   checkalg(al);
    2976        1015 :   if (alg_type(al)==al_REAL) switch(alg_get_absdim(al)) {
    2977         112 :     case 1: case 2: return C_divl_i(x,y);
    2978         147 :     case 4: return H_divl_i(x,y);
    2979             :   }
    2980         756 :   tx = alg_model(al,x);
    2981         749 :   ty = alg_model(al,y);
    2982         749 :   if (tx == al_MATRIX) {
    2983         140 :     if (ty != al_MATRIX) pari_err_TYPE2("\\", x, y);
    2984         133 :     if ((lg(x)>1 && lg(x) != lgcols(x)) || (lg(y)>1 && lg(y) != lgcols(y)))
    2985          28 :       pari_err_DIM("algdivl (nonsquare)");
    2986         105 :     if (lg(x) != lg(y)) pari_err_DIM("algdivl");
    2987          84 :     if (lg(y) == 1) return cgetg(1, t_MAT);
    2988             :   }
    2989         686 :   return algdivl_i(al,x,y,tx,ty);
    2990             : }
    2991             : 
    2992         889 : GEN algdivl(GEN al, GEN x, GEN y)
    2993             : {
    2994             :   GEN z;
    2995         889 :   z = algdivl_i2(al,x,y);
    2996         742 :   if (!z) pari_err_INV("algdivl", x);
    2997         728 :   return z;
    2998             : }
    2999             : 
    3000             : int
    3001         126 : algisdivl(GEN al, GEN x, GEN y, GEN* ptz)
    3002             : {
    3003         126 :   pari_sp av = avma;
    3004         126 :   GEN z = algdivl_i2(al,x,y);
    3005         126 :   if (!z) return gc_bool(av,0);
    3006          84 :   if (ptz != NULL) *ptz = z;
    3007          84 :   return 1;
    3008             : }
    3009             : 
    3010             : static GEN
    3011         140 : C_inv(GEN x)
    3012             : {
    3013         140 :   switch (H_model(x))
    3014             :   {
    3015          63 :     case H_SCALAR: return gequal0(x) ? NULL : ginv(x);
    3016          70 :     case H_MATRIX: return RgM_inv(x);
    3017           7 :     default: pari_err_TYPE("alginv_i", x);
    3018             :   }
    3019             :   return NULL; /*LCOV_EXCL_LINE*/
    3020             : }
    3021             : static GEN
    3022         259 : H_inv(GEN x)
    3023             : {
    3024         259 :   pari_sp av = avma;
    3025             :   GEN nm, xi;
    3026             :   long i;
    3027         259 :   switch (H_model(x))
    3028             :   {
    3029          28 :     case H_SCALAR:
    3030          28 :       if (gequal0(x)) return NULL;
    3031          14 :       return ginv(x);
    3032         161 :     case H_QUATERNION:
    3033         161 :       if (gequal0(x)) return NULL;
    3034         154 :       nm = H_norm(x, 0);
    3035         154 :       xi = gdiv(x,nm);
    3036         616 :       for(i=2; i<=4; i++) gel(xi,i) = gneg(gel(xi,i));
    3037         154 :       return gc_GEN(av,xi);
    3038          63 :     case H_MATRIX:
    3039          63 :       if (lg(x)==1) return cgetg(1,t_MAT);
    3040          56 :       return H_divl_i(x, matid(lg(x)-1));
    3041             :   }
    3042             :   return NULL; /*LCOV_EXCL_LINE*/
    3043             : }
    3044             : static GEN
    3045        2989 : alginv_i(GEN al, GEN x)
    3046             : {
    3047        2989 :   pari_sp av = avma;
    3048        2989 :   GEN res = NULL, p = alg_get_char(al);
    3049             :   long tx, n, ta;
    3050        2989 :   ta = alg_type(al);
    3051        2989 :   if (ta==al_REAL) switch(alg_get_absdim(al)) {
    3052         140 :     case 1: case 2: return C_inv(x);
    3053         217 :     case 4: return H_inv(x);
    3054           7 :     default: pari_err_TYPE("alginv_i [apply alginit]", al);
    3055             :   }
    3056        2625 :   tx = alg_model(al,x);
    3057        2604 :   switch(tx) {
    3058          70 :     case al_TRIVIAL :
    3059          70 :       if (signe(p)) { res = mkcol(Fp_inv(gel(x,1),p)); break; }
    3060          56 :       else          { res = mkcol(ginv(gel(x,1))); break; }
    3061         455 :     case al_ALGEBRAIC :
    3062             :       switch(ta) {
    3063         350 :         case al_CYCLIC: n = alg_get_degree(al); break;
    3064         105 :         case al_CSA: n = alg_get_dim(al); break;
    3065             :         default: return NULL; /* LCOV_EXCL_LINE */
    3066             :       }
    3067         455 :       res = algdivl_i(al, x, col_ei(n,1), tx, al_ALGEBRAIC); break;
    3068        1841 :     case al_BASIS : res = algdivl_i(al, x, col_ei(alg_get_absdim(al),1), tx,
    3069        1841 :                                                             al_BASIS); break;
    3070         238 :     case al_MATRIX :
    3071         238 :       n = lg(x)-1;
    3072         238 :       if (n==0) return cgetg(1, t_MAT);
    3073         224 :       if (n != nbrows(x)) pari_err_DIM("alginv_i (nonsquare)");
    3074         217 :       res = algdivl_i(al, x, col_ei(n*n*alg_get_absdim(al),1), tx, al_BASIS);
    3075             :         /* cheat on type because wrong dimension */
    3076             :   }
    3077        2583 :   if (!res) return gc_NULL(av);
    3078        1162 :   return gc_GEN(av,res);
    3079             : }
    3080             : GEN
    3081        1330 : alginv(GEN al, GEN x)
    3082             : {
    3083             :   GEN z;
    3084        1330 :   checkalg(al);
    3085        1330 :   z = alginv_i(al,x);
    3086        1281 :   if (!z) pari_err_INV("alginv", x);
    3087        1246 :   return z;
    3088             : }
    3089             : 
    3090             : int
    3091        1659 : algisinv(GEN al, GEN x, GEN* ptix)
    3092             : {
    3093        1659 :   pari_sp av = avma;
    3094             :   GEN ix;
    3095        1659 :   if (al) checkalg(al);
    3096        1659 :   ix = alginv_i(al,x);
    3097        1659 :   if (!ix) return gc_bool(av,0);
    3098         196 :   if (ptix != NULL) *ptix = ix;
    3099         196 :   return 1;
    3100             : }
    3101             : 
    3102             : /*  x*y^(-1)  */
    3103             : GEN
    3104         469 : algdivr(GEN al, GEN x, GEN y) { return algmul(al, x, alginv(al, y)); }
    3105             : 
    3106       52177 : static GEN _mul(void* data, GEN x, GEN y) { return algmul((GEN)data,x,y); }
    3107      124662 : static GEN _sqr(void* data, GEN x) { return algsqr((GEN)data,x); }
    3108             : 
    3109             : static GEN
    3110          21 : algmatid(GEN al, long N)
    3111             : {
    3112          21 :   long n = alg_get_absdim(al), i, j;
    3113             :   GEN res, one, zero;
    3114             : 
    3115          21 :   res = zeromatcopy(N,N);
    3116          21 :   one = col_ei(n,1);
    3117          21 :   zero = zerocol(n);
    3118          49 :   for (i=1; i<=N; i++)
    3119          84 :   for (j=1; j<=N; j++)
    3120          56 :     gcoeff(res,i,j) = i==j ? one : zero;
    3121          21 :   return res;
    3122             : }
    3123             : 
    3124             : GEN
    3125       22520 : algpow(GEN al, GEN x, GEN n)
    3126             : {
    3127       22520 :   pari_sp av = avma;
    3128             :   GEN res;
    3129       22520 :   long s = signe(n);
    3130       22520 :   checkalg(al);
    3131       22520 :   if (!s && alg_type(al)==al_REAL)
    3132             :   {
    3133          63 :     if (H_model(x) == H_MATRIX) return matid(lg(x)-1);
    3134          35 :     else                        return gen_1;
    3135             :   }
    3136       22457 :   switch (s) {
    3137          28 :     case 0:
    3138          28 :       if (alg_model(al,x) == al_MATRIX)
    3139          21 :         res = algmatid(al,lg(x)-1);
    3140             :       else
    3141           7 :         res = col_ei(alg_get_absdim(al),1);
    3142          28 :       return res;
    3143       22282 :     case 1:
    3144       22282 :       res = gen_pow_i(x, n, (void*)al, _sqr, _mul); break;
    3145         147 :     default: /* -1 */
    3146         147 :       res = gen_pow_i(alginv(al,x), gneg(n), (void*)al, _sqr, _mul);
    3147             :   }
    3148       22415 :   return gc_GEN(av,res);
    3149             : }
    3150             : 
    3151             : static GEN
    3152         546 : algredcharpoly_i(GEN al, GEN x, long v)
    3153             : {
    3154         546 :   GEN rnf = alg_get_splittingfield(al);
    3155         546 :   GEN cp = charpoly(algtomatrix(al,x,0),v);
    3156         539 :   long i, m = lg(cp);
    3157        2184 :   for (i=2; i<m; i++) gel(cp,i) = rnfeltdown(rnf, gel(cp,i));
    3158         539 :   return cp;
    3159             : }
    3160             : 
    3161             : /* assumes al is CSA or CYCLIC */
    3162             : static GEN
    3163         553 : algredcharpoly(GEN al, GEN x, long v)
    3164             : {
    3165         553 :   pari_sp av = avma;
    3166         553 :   long w = gvar(rnf_get_pol(alg_get_center(al)));
    3167         553 :   if (varncmp(v,w)>=0) pari_err_PRIORITY("algredcharpoly",pol_x(v),">=",w);
    3168         546 :   switch(alg_type(al))
    3169             :   {
    3170         546 :     case al_CYCLIC:
    3171             :     case al_CSA:
    3172         546 :       return gc_upto(av, algredcharpoly_i(al, x, v));
    3173             :   }
    3174             :   return NULL; /*LCOV_EXCL_LINE*/
    3175             : }
    3176             : 
    3177             : static GEN
    3178       31929 : algbasischarpoly(GEN al, GEN x, long v)
    3179             : {
    3180       31929 :   pari_sp av = avma;
    3181       31929 :   GEN p = alg_get_char(al), mx;
    3182       31929 :   if (alg_model(al,x) == al_MATRIX) mx = algleftmultable_mat(al,x);
    3183       31838 :   else                              mx = algbasismultable(al,x);
    3184       31922 :   if (signe(p)) {
    3185       29570 :     GEN res = FpM_charpoly(mx,p);
    3186       29570 :     setvarn(res,v);
    3187       29570 :     return gc_upto(av, res);
    3188             :   }
    3189        2352 :   return gc_upto(av, charpoly(mx,v));
    3190             : }
    3191             : 
    3192             : static GEN
    3193          35 : R_charpoly(GEN x, long v, long abs)
    3194             : {
    3195          35 :   pari_sp av = avma;
    3196          35 :   GEN res = NULL;
    3197          35 :   switch (H_model(x))
    3198             :   {
    3199          14 :     case H_SCALAR: res = mkpoln(2, gen_1, gneg(x)); break;
    3200          14 :     case H_MATRIX:
    3201          14 :       res = charpoly(x,v);
    3202          14 :       if (abs) res = gpowgs(res,nbrows(x));
    3203          14 :       break;
    3204           7 :     default: pari_err_TYPE("R_charpoly", x);
    3205             :   }
    3206          28 :   if (v) setvarn(res, v);
    3207          28 :   return gc_GEN(av, res);
    3208             : }
    3209             : static GEN
    3210          35 : C_charpoly(GEN x, long v, long abs)
    3211             : {
    3212          35 :   pari_sp av = avma;
    3213          35 :   GEN res = NULL;
    3214          35 :   switch (H_model(x))
    3215             :   {
    3216          14 :     case H_SCALAR:
    3217          14 :       if (abs)  res = mkpoln(3, gen_1, gneg(gshift(real_i(x),1)), cxnorm(x));
    3218           7 :       else      res = mkpoln(2, gen_1, gneg(x));
    3219          14 :       break;
    3220          14 :     case H_MATRIX:
    3221          14 :       res = charpoly(x,v);
    3222          14 :       if (abs) res = gpowgs(real_i(gmul(res,gconj(res))),nbrows(x));
    3223          14 :       break;
    3224           7 :     default: pari_err_TYPE("C_charpoly", x);
    3225             :   }
    3226          28 :   if (v) setvarn(res, v);
    3227          28 :   return gc_GEN(av, res);
    3228             : }
    3229             : static GEN
    3230          98 : H_charpoly(GEN x, long v, long abs)
    3231             : {
    3232          98 :   pari_sp av = avma;
    3233             :   GEN res;
    3234          98 :   if (H_model(x) == H_MATRIX) return greal(charpoly(H_tomatrix(x,abs),v));
    3235          70 :   res = mkpoln(3, gen_1, gneg(H_trace(x,0)), H_norm(x,0));
    3236          70 :   if (v) setvarn(res, v);
    3237          70 :   if (abs) res = gsqr(res);
    3238          70 :   return gc_GEN(av, res);
    3239             : }
    3240             : 
    3241             : GEN
    3242       32118 : algcharpoly(GEN al, GEN x, long v, long abs)
    3243             : {
    3244             :   long ta;
    3245       32118 :   if (v<0) v=0;
    3246       32118 :   checkalg(al);
    3247       32118 :   ta = alg_type(al);
    3248       32118 :   if (ta == al_REAL) switch (alg_get_absdim(al)) {
    3249          35 :     case 1: return R_charpoly(x, v, abs);
    3250          35 :     case 2: return C_charpoly(x, v, abs);
    3251          98 :     case 4: return H_charpoly(x, v, abs);
    3252           7 :     default: pari_err_TYPE("algcharpoly [apply alginit]", al);
    3253             :   }
    3254             : 
    3255             :   /* gneg(x[1]) left on stack */
    3256       31943 :   if (alg_model(al,x) == al_TRIVIAL) {
    3257          84 :     GEN p = alg_get_char(al);
    3258          84 :     if (signe(p)) return deg1pol(gen_1,Fp_neg(gel(x,1),p),v);
    3259          70 :     return deg1pol(gen_1,gneg(gel(x,1)),v);
    3260             :   }
    3261             : 
    3262       31852 :   switch(ta) {
    3263         665 :     case al_CYCLIC: case al_CSA:
    3264         665 :       if (abs)
    3265             :       {
    3266         112 :         if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
    3267             :       }
    3268         553 :       else return algredcharpoly(al,x,v);
    3269       31299 :     case al_TABLE: return algbasischarpoly(al,x,v);
    3270             :     default : return NULL; /* LCOV_EXCL_LINE */
    3271             :   }
    3272             : }
    3273             : 
    3274             : /* assumes x in basis form */
    3275             : static GEN
    3276      652441 : algabstrace(GEN al, GEN x)
    3277             : {
    3278      652441 :   pari_sp av = avma;
    3279      652441 :   GEN res = NULL, p = alg_get_char(al);
    3280      652441 :   if (signe(p)) return FpV_dotproduct(x, alg_get_tracebasis(al), p);
    3281       49462 :   switch(alg_model(al,x)) {
    3282         154 :     case al_TRIVIAL: return gcopy(gel(x,1)); break;
    3283       49308 :     case al_BASIS: res = RgV_dotproduct(x, alg_get_tracebasis(al)); break;
    3284             :   }
    3285       49308 :   return gc_upto(av,res);
    3286             : }
    3287             : 
    3288             : static GEN
    3289        1512 : algredtrace(GEN al, GEN x)
    3290             : {
    3291        1512 :   pari_sp av = avma;
    3292        1512 :   GEN res = NULL;
    3293        1512 :   switch(alg_model(al,x)) {
    3294          35 :     case al_TRIVIAL: return gcopy(gel(x,1)); break;
    3295         560 :     case al_BASIS: return algredtrace(al, algbasistoalg(al,x));
    3296             :                    /* TODO precompute too? */
    3297         917 :     case al_ALGEBRAIC:
    3298         917 :       switch(alg_type(al))
    3299             :       {
    3300         588 :         case al_CYCLIC:
    3301         588 :           res = rnfelttrace(alg_get_splittingfield(al),gel(x,1));
    3302         588 :           break;
    3303         329 :         case al_CSA:
    3304         329 :           res = gtrace(algalgmultable_csa(al,x));
    3305         329 :           res = gdiv(res, stoi(alg_get_degree(al)));
    3306         329 :           break;
    3307             :         default: return NULL; /* LCOV_EXCL_LINE */
    3308             :       }
    3309             :   }
    3310         917 :   return gc_upto(av,res);
    3311             : }
    3312             : 
    3313             : static GEN
    3314         469 : algtrace_mat(GEN al, GEN M, long abs) {
    3315         469 :   pari_sp av = avma;
    3316         469 :   long N = lg(M)-1, i;
    3317         469 :   GEN res, p = alg_get_char(al);
    3318         469 :   if (N == 0) return gen_0;
    3319         448 :   if (N != nbrows(M)) pari_err_DIM("algtrace_mat (nonsquare)");
    3320             : 
    3321         434 :   if (!signe(p)) p = NULL;
    3322         434 :   if (alg_type(al) == al_TABLE) abs = 1;
    3323         434 :   res = algtrace(al, gcoeff(M,1,1), abs);
    3324         896 :   for (i=2; i<=N; i++) {
    3325         462 :     if (p)  res = Fp_add(res, algtrace(al,gcoeff(M,i,i),abs), p);
    3326         455 :     else    res = gadd(res, algtrace(al,gcoeff(M,i,i),abs));
    3327             :   }
    3328         434 :   if (abs) res = gmulgu(res, N); /* absolute trace */
    3329         434 :   return gc_upto(av, res);
    3330             : }
    3331             : 
    3332             : static GEN
    3333          35 : R_trace(GEN x, long abs)
    3334             : {
    3335          35 :   pari_sp av = avma;
    3336          35 :   GEN res = NULL;
    3337          35 :   switch (H_model(x))
    3338             :   {
    3339          14 :     case H_SCALAR: res = gcopy(x); break;
    3340          14 :     case H_MATRIX: res = abs? mulrs(gtrace(x),nbrows(x)) : gtrace(x); break;
    3341           7 :     default: pari_err_TYPE("R_trace", x);
    3342             :   }
    3343          28 :   return gc_GEN(av, res);
    3344             : }
    3345             : static GEN
    3346          35 : C_trace(GEN x, long abs)
    3347             : {
    3348          35 :   pari_sp av = avma;
    3349          35 :   GEN res = NULL;
    3350          35 :   switch (H_model(x))
    3351             :   {
    3352          14 :     case H_SCALAR: res = abs ? gshift(real_i(x),1) : x; break;
    3353          14 :     case H_MATRIX:
    3354          14 :       res = abs ? mulrs(real_i(gtrace(x)),2*nbrows(x)) : gtrace(x); break;
    3355           7 :     default: pari_err_TYPE("C_trace", x);
    3356             :   }
    3357          28 :   return gc_GEN(av, res);
    3358             : }
    3359             : static GEN
    3360         567 : H_trace(GEN x, long abs)
    3361             : {
    3362         567 :   long s = abs? 2 : 1;
    3363         567 :   switch (H_model(x))
    3364             :   {
    3365         154 :     case H_SCALAR: return gshift(real_i(x),s);
    3366         329 :     case H_QUATERNION: return gshift(gel(x,1),s);
    3367          77 :     case H_MATRIX:
    3368          77 :       return algtrace_mat(NULL, x, abs);
    3369             :   }
    3370             :   return NULL; /*LCOV_EXCL_LINE*/
    3371             : }
    3372             : 
    3373             : GEN
    3374        2702 : algtrace(GEN al, GEN x, long abs)
    3375             : {
    3376             :   long ta;
    3377        2702 :   checkalg(al);
    3378        2702 :   ta = alg_type(al);
    3379        2702 :   if (ta==al_REAL) switch (alg_get_absdim(al)) {
    3380          35 :     case 1: return R_trace(x,abs);
    3381          35 :     case 2: return C_trace(x,abs);
    3382         497 :     case 4: return H_trace(x,abs);
    3383           7 :     default: pari_err_TYPE("algtrace [apply alginit]", al);
    3384             :   }
    3385        2128 :   if (alg_model(al,x) == al_MATRIX) return algtrace_mat(al,x,abs);
    3386        1736 :   switch(ta) {
    3387        1596 :     case al_CYCLIC: case al_CSA:
    3388        1596 :       if (!abs) return algredtrace(al,x);
    3389         644 :       if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
    3390         784 :     case al_TABLE: return algabstrace(al,x);
    3391             :     default : return NULL; /* LCOV_EXCL_LINE */
    3392             :   }
    3393             : }
    3394             : 
    3395             : static GEN
    3396       64972 : algtracebasis(GEN al)
    3397             : {
    3398       64972 :   pari_sp av = avma;
    3399       64972 :   GEN mt = alg_get_multable(al), p = alg_get_char(al);
    3400       64972 :   long i, l = lg(mt);
    3401       64972 :   GEN v = cgetg(l, t_VEC);
    3402      284657 :   if (signe(p)) for (i=1; i < l; i++) gel(v,i) = FpM_trace(gel(mt,i), p);
    3403       78261 :   else          for (i=1; i < l; i++) gel(v,i) = ZM_trace(gel(mt,i));
    3404       64972 :   return gc_upto(av,v);
    3405             : }
    3406             : 
    3407             : /* Assume: i > 0, expo := p^i <= absdim, x contained in I_{i-1} given by mult
    3408             :  * table modulo modu=p^(i+1). Return Tr(x^(p^i)) mod modu */
    3409             : static ulong
    3410       51360 : algtracei(GEN mt, ulong p, ulong expo, ulong modu)
    3411             : {
    3412       51360 :   pari_sp av = avma;
    3413       51360 :   long j, l = lg(mt);
    3414       51360 :   ulong tr = 0;
    3415       51360 :   mt = Flm_powu(mt,expo,modu);
    3416      670747 :   for (j=1; j<l; j++) tr += ucoeff(mt,j,j);
    3417       51360 :   return gc_ulong(av, (tr/expo) % p);
    3418             : }
    3419             : 
    3420             : static GEN
    3421          42 : R_norm(GEN x, long abs)
    3422             : {
    3423          42 :   pari_sp av = avma;
    3424          42 :   GEN res = NULL;
    3425          42 :   switch (H_model(x))
    3426             :   {
    3427          14 :     case H_SCALAR: res = gcopy(x); break;
    3428          21 :     case H_MATRIX: res = abs ? powrs(det(x),nbrows(x)) : det(x); break;
    3429           7 :     default: pari_err_TYPE("R_norm", x);
    3430             :   }
    3431          35 :   return gc_GEN(av,res);
    3432             : }
    3433             : static GEN
    3434          42 : C_norm(GEN x, long abs)
    3435             : {
    3436          42 :   pari_sp av = avma;
    3437          42 :   GEN res = NULL;
    3438          42 :   switch (H_model(x))
    3439             :   {
    3440          14 :     case H_SCALAR: res = abs ? cxnorm(x) : x; break;
    3441          21 :     case H_MATRIX: res = abs ? powrs(cxnorm(det(x)),nbrows(x)) : det(x); break;
    3442           7 :     default: pari_err_TYPE("C_norm", x);
    3443             :   }
    3444          35 :   return gc_GEN(av,res);
    3445             : }
    3446             : static GEN
    3447         434 : H_norm(GEN x, long abs)
    3448             : {
    3449         434 :   pari_sp av = avma;
    3450         434 :   switch (H_model(x))
    3451             :   {
    3452          42 :     case H_SCALAR:
    3453          42 :       if (abs)  return gc_GEN(av,gsqr(gnorm(x)));
    3454          35 :       else      return gnorm(x);
    3455         322 :     case H_QUATERNION:
    3456         322 :       if (abs)  return gc_GEN(av,gsqr(gnorml2(x)));
    3457         294 :       else      return gnorml2(x);
    3458          63 :     case H_MATRIX:
    3459          63 :       return gc_GEN(av,real_i(det(H_tomatrix(x,abs))));
    3460             :   }
    3461             :   return NULL; /*LCOV_EXCL_LINE*/
    3462             : }
    3463             : 
    3464             : GEN
    3465        1309 : algnorm(GEN al, GEN x, long abs)
    3466             : {
    3467        1309 :   pari_sp av = avma;
    3468             :   long tx, ta;
    3469             :   GEN p, rnf, res, mx;
    3470        1309 :   checkalg(al);
    3471        1309 :   ta = alg_type(al);
    3472        1309 :   if (ta==al_REAL) switch (alg_get_absdim(al)) {
    3473          42 :     case 1: return R_norm(x,abs);
    3474          42 :     case 2: return C_norm(x,abs);
    3475         210 :     case 4: return H_norm(x,abs);
    3476           7 :     default: pari_err_TYPE("algnorm [apply alginit]", al);
    3477             :   }
    3478        1008 :   p = alg_get_char(al);
    3479        1008 :   tx = alg_model(al,x);
    3480        1008 :   if (signe(p)) {
    3481          21 :     if (tx == al_MATRIX)    mx = algleftmultable_mat(al,x);
    3482          14 :     else                    mx = algbasismultable(al,x);
    3483          21 :     return gc_upto(av, FpM_det(mx,p));
    3484             :   }
    3485         987 :   if (tx == al_TRIVIAL) return gcopy(gel(x,1));
    3486             : 
    3487         945 :   switch(ta) {
    3488         875 :     case al_CYCLIC: case al_CSA:
    3489         875 :       if (abs)
    3490             :       {
    3491         196 :         if (alg_model(al,x)==al_ALGEBRAIC) x = algalgtobasis(al,x);
    3492             :       }
    3493             :       else
    3494             :       {
    3495         679 :         rnf = alg_get_splittingfield(al);
    3496         679 :         res = rnfeltdown(rnf, det(algtomatrix(al,x,0)));
    3497         672 :         break;
    3498             :       }
    3499             :     case al_TABLE:
    3500         266 :       if (tx == al_MATRIX)  mx = algleftmultable_mat(al,x);
    3501         105 :       else                  mx = algbasismultable(al,x);
    3502         259 :       res = det(mx);
    3503         259 :       break;
    3504             :     default: return NULL; /* LCOV_EXCL_LINE */
    3505             :   }
    3506         931 :   return gc_upto(av, res);
    3507             : }
    3508             : 
    3509             : static GEN
    3510       70491 : algalgtonat_cyc(GEN al, GEN x)
    3511             : {
    3512       70491 :   pari_sp av = avma;
    3513       70491 :   GEN nf = alg_get_abssplitting(al), rnf = alg_get_splittingfield(al), res, c;
    3514       70491 :   long n = alg_get_degree(al), N = nf_get_degree(nf), i, i1;
    3515       70491 :   res = zerocol(N*n);
    3516      219132 :   for (i=0; i<n; i++) {
    3517      148641 :     c = gel(x,i+1);
    3518      148641 :     c = rnfeltreltoabs(rnf,c);
    3519      148641 :     if (!gequal0(c)) {
    3520       96622 :       c = algtobasis(nf,c);
    3521      502115 :       for (i1=1; i1<=N; i1++) gel(res,i*N+i1) = gel(c,i1);
    3522             :     }
    3523             :   }
    3524       70491 :   return gc_GEN(av, res);
    3525             : }
    3526             : 
    3527             : static GEN
    3528       16163 : algalgtonat_csa(GEN al, GEN x)
    3529             : {
    3530       16163 :   pari_sp av = avma;
    3531       16163 :   GEN nf = alg_get_center(al), res, c;
    3532       16163 :   long d2 = alg_get_dim(al), n = nf_get_degree(nf), i, i1;
    3533       16163 :   res = zerocol(d2*n);
    3534       80129 :   for (i=0; i<d2; i++) {
    3535       63966 :     c = gel(x,i+1);
    3536       63966 :     if (!gequal0(c)) {
    3537       35980 :       c = algtobasis(nf,c);
    3538      107415 :       for (i1=1; i1<=n; i1++) gel(res,i*n+i1) = gel(c,i1);
    3539             :     }
    3540             :   }
    3541       16163 :   return gc_GEN(av, res);
    3542             : }
    3543             : 
    3544             : /* assumes al CSA or CYCLIC */
    3545             : static GEN
    3546       86654 : algalgtonat(GEN al, GEN x)
    3547             : {
    3548       86654 :   switch(alg_type(al))
    3549             :   {
    3550       70491 :     case al_CYCLIC: return algalgtonat_cyc(al, x);
    3551       16163 :     case al_CSA: return algalgtonat_csa(al, x);
    3552             :   }
    3553             :   return NULL; /*LCOV_EXCL_LINE*/
    3554             : }
    3555             : 
    3556             : static GEN
    3557       17094 : algnattoalg_cyc(GEN al, GEN x)
    3558             : {
    3559       17094 :   pari_sp av = avma;
    3560       17094 :   GEN nf = alg_get_abssplitting(al), rnf = alg_get_splittingfield(al), res, c;
    3561       17094 :   long n = alg_get_degree(al), N = nf_get_degree(nf), i, i1;
    3562       17094 :   res = zerocol(n);
    3563       17094 :   c = zerocol(N);
    3564       68390 :   for (i=0; i<n; i++) {
    3565      411523 :     for (i1=1; i1<=N; i1++) gel(c,i1) = gel(x,i*N+i1);
    3566       51296 :     gel(res,i+1) = rnfeltabstorel(rnf,basistoalg(nf,c));
    3567             :   }
    3568       17094 :   return gc_GEN(av, res);
    3569             : }
    3570             : 
    3571             : static GEN
    3572        2135 : algnattoalg_csa(GEN al, GEN x)
    3573             : {
    3574        2135 :   pari_sp av = avma;
    3575        2135 :   GEN nf = alg_get_center(al), res, c;
    3576        2135 :   long d2 = alg_get_dim(al), n = nf_get_degree(nf), i, i1;
    3577        2135 :   res = zerocol(d2);
    3578        2135 :   c = zerocol(n);
    3579       11032 :   for (i=0; i<d2; i++) {
    3580       28728 :     for (i1=1; i1<=n; i1++) gel(c,i1) = gel(x,i*n+i1);
    3581        8897 :     gel(res,i+1) = basistoalg(nf,c);
    3582             :   }
    3583        2135 :   return gc_GEN(av, res);
    3584             : }
    3585             : 
    3586             : /* assumes al CSA or CYCLIC */
    3587             : static GEN
    3588       19229 : algnattoalg(GEN al, GEN x)
    3589             : {
    3590       19229 :   switch(alg_type(al))
    3591             :   {
    3592       17094 :     case al_CYCLIC: return algnattoalg_cyc(al, x);
    3593        2135 :     case al_CSA: return algnattoalg_csa(al, x);
    3594             :   }
    3595             :   return NULL; /*LCOV_EXCL_LINE*/
    3596             : }
    3597             : 
    3598             : static GEN
    3599         182 : algalgtobasis_mat(GEN al, GEN x) /* componentwise */
    3600             : {
    3601         182 :   pari_sp av = avma;
    3602             :   long lx, lxj, i, j;
    3603             :   GEN res;
    3604         182 :   lx = lg(x);
    3605         182 :   res = cgetg(lx, t_MAT);
    3606         546 :   for (j=1; j<lx; j++) {
    3607         364 :     lxj = lg(gel(x,j));
    3608         364 :     gel(res,j) = cgetg(lxj, t_COL);
    3609        1092 :     for (i=1; i<lxj; i++)
    3610         728 :       gcoeff(res,i,j) = algalgtobasis(al,gcoeff(x,i,j));
    3611             :   }
    3612         182 :   return gc_GEN(av,res);
    3613             : }
    3614             : GEN
    3615       88656 : algalgtobasis(GEN al, GEN x)
    3616             : {
    3617             :   pari_sp av;
    3618             :   long tx, ta;
    3619       88656 :   checkalg(al);
    3620       88649 :   ta = alg_type(al);
    3621       88649 :   if (ta != al_CYCLIC && ta != al_CSA) pari_err_TYPE("algalgtobasis [use alginit]", al);
    3622       88614 :   tx = alg_model(al,x);
    3623       88593 :   if (tx==al_BASIS) return gcopy(x);
    3624       86752 :   if (tx==al_MATRIX) return algalgtobasis_mat(al,x);
    3625       86570 :   av = avma;
    3626       86570 :   x = algalgtonat(al,x);
    3627       86570 :   x = RgM_RgC_mul(alg_get_invbasis(al),x);
    3628       86570 :   return gc_upto(av, x);
    3629             : }
    3630             : 
    3631             : /*
    3632             :  Quaternion algebras special case:
    3633             :  al = (L/F, sigma, b) with L quadratic
    3634             :  > v^2-a: i = v
    3635             :  > v^2+A*v+B: i = 2*v+A: i^2 = a = A^2-4*B
    3636             :  al ~ (a,b)_F
    3637             : */
    3638             : /* We could improve efficiency, but these functions are just for convenience. */
    3639             : GEN
    3640         280 : algquattobasis(GEN al, GEN x)
    3641             : {
    3642         280 :   pari_sp av = avma;
    3643             :   GEN L1, L2, pol, A, x2, nf;
    3644             :   long v, i, ta;
    3645         280 :   checkalg(al);
    3646         273 :   if (alg_is_asq(al))
    3647             :   {
    3648          84 :     x = algalgtonat(al,x);
    3649          84 :     x = RgM_RgC_mul(alg_get_invbasis(al),x);
    3650          84 :     return gc_upto(av,x);
    3651             :   }
    3652         189 :   ta = alg_type(al);
    3653         189 :   if (ta != al_CYCLIC || alg_get_degree(al)!=2)
    3654          28 :     pari_err_TYPE("algquattobasis [not a quaternion algebra]", al);
    3655         161 :   if (typ(x)!=t_COL && typ(x)!=t_VEC) pari_err_TYPE("algquattobasis", x);
    3656         154 :   if (lg(x)!=5) pari_err_DIM("algquattobasis [quaternions have 4 components]");
    3657         147 :   nf = alg_get_center(al);
    3658         147 :   x2 = cgetg(5, t_COL);
    3659         707 :   for (i=1; i<=4; i++) gel(x2,i) = basistoalg(nf, gel(x,i));
    3660         140 :   gel(x2,4) = gneg(gel(x2,4));
    3661         140 :   pol = alg_get_splitpol(al);
    3662         140 :   v = varn(pol);
    3663         140 :   A = gel(pol,3); /* coeff of v^1 */
    3664         140 :   if (gequal0(A))
    3665             :   {
    3666             :     /* i = v */
    3667          91 :     L1 = deg1pol_shallow(gel(x2,2), gel(x2,1), v);
    3668          91 :     L2 = deg1pol_shallow(gel(x2,4), gel(x2,3), v);
    3669             :   }
    3670             :   else
    3671             :   {
    3672             :     /* i = 2*v+A */
    3673          49 :     L1 = deg1pol_shallow(gshift(gel(x2,2),1),
    3674          49 :         gadd(gel(x2,1),gmul(A,gel(x2,2))), v);
    3675          49 :     L2 = deg1pol_shallow(gshift(gel(x2,4),1),
    3676          49 :         gadd(gel(x2,3),gmul(A,gel(x2,4))), v);
    3677             :   }
    3678         140 :   return gc_upto(av, algalgtobasis(al,mkcol2(L1,L2)));
    3679             : }
    3680             : GEN
    3681         126 : algbasistoquat(GEN al, GEN x)
    3682             : {
    3683         126 :   pari_sp av = avma;
    3684             :   GEN pol, A, x2, q;
    3685             :   long v, ta;
    3686         126 :   checkalg(al);
    3687         119 :   if (alg_is_asq(al))
    3688             :   {
    3689          21 :     x = RgM_RgC_mul(alg_get_basis(al),x);
    3690          21 :     x = algnattoalg(al,x);
    3691          21 :     return gc_upto(av, x);
    3692             :   }
    3693          98 :   ta = alg_type(al);
    3694          98 :   if (ta != al_CYCLIC || alg_get_degree(al)!=2)
    3695          28 :     pari_err_TYPE("algbasistoquat [not a quaternion algebra]", al);
    3696          70 :   pol = alg_get_splitpol(al);
    3697          70 :   v = varn(pol);
    3698          70 :   A = gel(pol,3); /* coeff of v^1 */
    3699          70 :   x2 = algbasistoalg(al, x);
    3700          56 :   x2 = lift0(x2, v);
    3701          56 :   q = cgetg(5, t_COL);
    3702          56 :   if (gequal0(A))
    3703             :   {
    3704             :     /* v = i */
    3705          42 :     gel(q,1) = polcoef_i(gel(x2,1),0,v);
    3706          42 :     gel(q,2) = polcoef_i(gel(x2,1),1,v);
    3707          42 :     gel(q,3) = polcoef_i(gel(x2,2),0,v);
    3708          42 :     gel(q,4) = polcoef_i(gel(x2,2),1,v);
    3709          42 :     gel(q,4) = gneg(gel(q,4));
    3710             :   }
    3711             :   else
    3712             :   {
    3713             :     /* v = (i-A)/2 */
    3714          14 :     gel(q,2) = gshift(polcoef_i(gel(x2,1),1,v),-1);
    3715          14 :     gel(q,1) = gsub(polcoef_i(gel(x2,1),0,v), gmul(A,gel(q,2)));
    3716          14 :     gel(q,4) = gneg(gshift(polcoef_i(gel(x2,2),1,v),-1));
    3717          14 :     gel(q,3) = gadd(polcoef_i(gel(x2,2),0,v),gmul(A,gel(q,4)));
    3718             :   }
    3719          56 :   return gc_GEN(av, q);
    3720             : }
    3721             : GEN
    3722          98 : algisquatalg(GEN al)
    3723             : {
    3724          98 :   pari_sp av = avma;
    3725             :   GEN pol, a;
    3726             :   long ta;
    3727          98 :   checkalg(al);
    3728          91 :   ta = alg_type(al);
    3729          91 :   if (ta == al_REAL && algreal_dim(al)==4)
    3730           7 :     return gc_GEN(av, mkvec2(gen_m1,gen_m1));
    3731          84 :   if (alg_is_asq(al))
    3732          21 :     return gc_GEN(av, mkvec2(gmael3(al,6,1,1),gmael3(al,6,1,2)));
    3733          63 :   if (ta != al_CYCLIC || alg_get_degree(al)!=2) return gc_const(av, gen_0);
    3734          35 :   pol = alg_get_splitpol(al);
    3735          35 :   if (gequal0(gel(pol,3))) a = gneg(gel(pol,2)); /* coeffs of v^1 and v^0 */
    3736           7 :   else a = RgX_disc(pol);
    3737          35 :   return gc_GEN(av, mkvec2(a,lift_shallow(alg_get_b(al))));
    3738             : }
    3739             : 
    3740             : static GEN
    3741         119 : algbasistoalg_mat(GEN al, GEN x) /* componentwise */
    3742             : {
    3743         119 :   long j, lx = lg(x);
    3744         119 :   GEN res = cgetg(lx, t_MAT);
    3745         357 :   for (j=1; j<lx; j++) {
    3746         238 :     long i, lxj = lg(gel(x,j));
    3747         238 :     gel(res,j) = cgetg(lxj, t_COL);
    3748         714 :     for (i=1; i<lxj; i++) gcoeff(res,i,j) = algbasistoalg(al,gcoeff(x,i,j));
    3749             :   }
    3750         119 :   return res;
    3751             : }
    3752             : GEN
    3753        3409 : algbasistoalg(GEN al, GEN x)
    3754             : {
    3755             :   pari_sp av;
    3756             :   long tx, ta;
    3757        3409 :   checkalg(al);
    3758        3409 :   ta = alg_type(al);
    3759        3409 :   if (ta != al_CYCLIC && ta != al_CSA) pari_err_TYPE("algbasistoalg [use alginit]", al);
    3760        3388 :   tx = alg_model(al,x);
    3761        3374 :   if (tx==al_ALGEBRAIC) return gcopy(x);
    3762        3241 :   if (tx==al_MATRIX) return algbasistoalg_mat(al,x);
    3763        3122 :   av = avma;
    3764        3122 :   x = RgM_RgC_mul(alg_get_basis(al),x);
    3765        3122 :   x = algnattoalg(al,x);
    3766        3122 :   return gc_upto(av, x);
    3767             : }
    3768             : 
    3769             : static GEN
    3770        4466 : R_random(GEN b)
    3771             : {
    3772        4466 :   pari_sp av = avma;
    3773        4466 :   long prec = realprec(b);
    3774        4466 :   GEN z = randomr(prec); shiftr_inplace(z, 1);
    3775        4466 :   return gc_uptoleaf(av, mulrr(b,addsr(-1, z)));
    3776             : }
    3777             : static GEN
    3778         182 : C_random(GEN b)
    3779             : {
    3780         182 :   retmkcomplex(R_random(b), R_random(b));
    3781             : }
    3782             : static GEN
    3783         980 : H_random(GEN b)
    3784             : {
    3785         980 :   GEN res = cgetg(5, t_COL);
    3786             :   long i;
    3787        4900 :   for (i=1; i<=4; i++) gel(res,i) = R_random(b);
    3788         980 :   return res;
    3789             : }
    3790             : GEN
    3791       20104 : algrandom(GEN al, GEN b)
    3792             : {
    3793       20104 :   GEN res = NULL, p, N;
    3794             :   long i, n;
    3795       20104 :   checkalg(al);
    3796       20090 :   if (alg_type(al)==al_REAL)
    3797             :   {
    3798        1365 :     if (typ(b) != t_REAL) pari_err_TYPE("algrandom",b);
    3799        1358 :     if (signe(b) < 0) pari_err_DOMAIN("algrandom", "b", "<", gen_0, b);
    3800        1351 :     switch(alg_get_absdim(al))
    3801             :     {
    3802         182 :       case 1: res = R_random(b); break;
    3803         182 :       case 2: res = C_random(b); break;
    3804         980 :       case 4: res = H_random(b); break;
    3805           7 :       default: pari_err_TYPE("algrandom [apply alginit]", al);
    3806             :     }
    3807        1344 :     return res;
    3808             :   }
    3809       18725 :   if (typ(b) != t_INT) pari_err_TYPE("algrandom",b);
    3810       18718 :   if (signe(b) < 0) pari_err_DOMAIN("algrandom", "b", "<", gen_0, b);
    3811       18711 :   n = alg_get_absdim(al);
    3812       18711 :   N = addiu(shifti(b,1), 1); /* left on stack */
    3813       18711 :   p = alg_get_char(al); if (!signe(p)) p = NULL;
    3814       18711 :   res = cgetg(n+1,t_COL);
    3815      168385 :   for (i = 1; i <= n; i++)
    3816             :   {
    3817      149674 :     pari_sp av = avma;
    3818      149674 :     GEN t = subii(randomi(N),b);
    3819      149674 :     if (p) t = modii(t, p);
    3820      149674 :     gel(res,i) = gc_INT(av, t);
    3821             :   }
    3822       18711 :   return res;
    3823             : }
    3824             : 
    3825             : static GEN
    3826          84 : H_poleval(GEN pol, GEN x)
    3827             : {
    3828          84 :   pari_sp av = avma;
    3829             :   GEN res;
    3830             :   long i;
    3831          84 :   switch (H_model(x))
    3832             :   {
    3833          21 :     case H_SCALAR: return RgX_cxeval(pol, x, NULL);
    3834          49 :     case H_QUATERNION: break;
    3835           7 :     default: pari_err_TYPE("H_poleval", x);
    3836             :   }
    3837             : 
    3838          49 :   res = zerocol(4);
    3839         231 :   for (i=lg(pol)-1; i>1; i--)
    3840             :   {
    3841         182 :     gel(res,1) = gadd(gel(res,1), gel(pol,i));
    3842         182 :     if (i>2) res = H_mul(x, res);
    3843             :   }
    3844             : 
    3845          49 :   return gc_GEN(av,res);
    3846             : }
    3847             : 
    3848             : /* Assumes pol has coefficients in the same ring as the COL x; x either
    3849             :  * in basis or algebraic form or [x,mx] where mx is the mult. table of x.
    3850             :  TODO more general version: pol with coeffs in center and x in basis form */
    3851             : GEN
    3852       29395 : algpoleval(GEN al, GEN pol, GEN x)
    3853             : {
    3854       29395 :   pari_sp av = avma;
    3855       29395 :   GEN p, mx = NULL, res, c;
    3856       29395 :   long i, xalg = 0;
    3857       29395 :   if (typ(pol) != t_POL) pari_err_TYPE("algpoleval", pol);
    3858       29381 :   checkalg(al);
    3859       29381 :   if (alg_type(al)==al_REAL) return H_poleval(pol,x);
    3860       29297 :   p = alg_get_char(al);
    3861       29297 :   if (typ(x) == t_VEC)
    3862             :   {
    3863       10122 :     if (lg(x) != 3) pari_err_TYPE("algpoleval [vector must be of length 2]", x);
    3864       10115 :     mx = gel(x,2);
    3865       10115 :     x = gel(x,1);
    3866       10115 :     if (typ(mx)!=t_MAT || !gequal(x,gel(mx,1)))
    3867          21 :       pari_err_TYPE("algpoleval [mx must be the multiplication table of x]", mx);
    3868             :   }
    3869             :   else
    3870             :   {
    3871       19175 :     switch(alg_model(al,x))
    3872             :     {
    3873          14 :       case al_ALGEBRAIC: mx = algalgmultable(al,x); xalg=1; break;
    3874       19147 :       case al_BASIS:
    3875       19147 :       case al_TRIVIAL: mx = algbasismultable(al,x); break;
    3876           7 :       default: pari_err_TYPE("algpoleval", x);
    3877             :     }
    3878             :   }
    3879       29255 :   res = zerocol(lg(mx)-1);
    3880       29255 :   if (signe(p)) {
    3881       86287 :     for (i=lg(pol)-1; i>1; i--)
    3882             :     {
    3883       62807 :       gel(res,1) = Fp_add(gel(res,1), gel(pol,i), p);
    3884       62807 :       if (i>2) res = FpM_FpC_mul(mx, res, p);
    3885             :     }
    3886             :   }
    3887             :   else {
    3888       29338 :     for (i=lg(pol)-1; i>1; i--)
    3889             :     {
    3890       23563 :       c = gel(pol,i);
    3891       23563 :       if (xalg || is_rational_t(typ(c))) gel(res,1) = gadd(gel(res,1), c);
    3892         434 :       else res = RgC_add(res, algeltfromnf_i(al,c));
    3893       23563 :       if (i>2) res = RgM_RgC_mul(mx, res);
    3894             :     }
    3895             :   }
    3896       29255 :   return gc_upto(av, res);
    3897             : }
    3898             : 
    3899             : static GEN
    3900          98 : H_invol(GEN x)
    3901             : {
    3902          98 :   pari_sp av = avma;
    3903             :   long tx;
    3904             :   GEN cx;
    3905          98 :   if (!x) return gc_upto(av,diagonal(mkvec4(gen_1,gen_m1,gen_m1,gen_m1)));
    3906          35 :   tx = H_model(x);
    3907          28 :   if (tx == H_SCALAR) return gconj(x);
    3908          14 :   cx = gneg(x);
    3909          14 :   gel(cx,1) = gcopy(gel(x,1));
    3910          14 :   return gc_upto(av, cx);
    3911             : }
    3912             : 
    3913             : GEN
    3914         308 : alginvol(GEN al, GEN x)
    3915             : {
    3916         308 :   pari_sp av = avma;
    3917             :   GEN invol;
    3918         308 :   checkalg(al);
    3919         294 :   if (!x && al) return gc_upto(av, alg_get_invol(al));
    3920         175 :   if (alg_type(al)==al_REAL) return H_invol(x);
    3921         133 :   x = algalgtobasis(al, x);
    3922         126 :   invol = alg_get_invol(al);
    3923         126 :   if (typ(invol)!=t_MAT)
    3924           7 :     pari_err_DOMAIN("alginvol [al does not contain an involution]", "invol", "=", gen_0, invol);
    3925         119 :   return gc_upto(av, RgM_RgC_mul(invol,x));
    3926             : }
    3927             : 
    3928             : GEN
    3929         112 : algskolemnoether(GEN al, GEN a, GEN fa)
    3930             : {
    3931         112 :   pari_sp av = avma;
    3932         112 :   long c = 0, i, ta;
    3933             :   GEN M, K, b;
    3934         112 :   checkalg(al);
    3935         105 :   ta = alg_type(al);
    3936         105 :   if (ta!=al_CYCLIC && ta!=al_CSA) pari_err_TYPE("algskolemnoether"
    3937             :       " [al: apply alginit()]", al);
    3938          91 :   if (typ(a) != t_VEC) a = mkvec(a);
    3939          91 :   if (typ(fa) != t_VEC) fa = mkvec(fa);
    3940          91 :   if (lg(a) != lg(fa)) pari_err_DIM("algskolemnoether [lg(a) != lg(fa)]");
    3941          84 :   if (lg(a) == 1) return gc_upto(av, col_ei(alg_get_absdim(al),1));
    3942             : 
    3943             :   /* compute space K of b s.t. b*a_i == fa_i*b for all i */
    3944          77 :   M = cgetg(lg(a),t_COL);
    3945         154 :   for (i=1; i<lg(a); i++) gel(M,i) = RgM_sub(algrightmultable(al,gel(a,i)),
    3946          91 :                                       algleftmultable(al,gel(fa,i)));
    3947          56 :   M = shallowmatconcat(M);
    3948          56 :   K = QM_ker(M);
    3949             : 
    3950             :   /* find invertible element in K */
    3951          56 :   if (lg(K)==1) pari_err(e_MISC, "no solution in algskolemnoether"
    3952             :       " [check simplicity and homomorphism assumptions]");
    3953          49 :   b = gel(K,1);
    3954        1449 :   while (!algisinv(al, b, NULL))
    3955             :   {
    3956        1407 :     b = gadd(b, gel(K,1+random_Fl(lg(K)-1)));
    3957        1407 :     c++;
    3958        1407 :     if (c > 200) pari_err(e_MISC, "probable infinite loop in algskolemnoether"
    3959             :         " (the subalgebra is probably not simple)");
    3960             :   }
    3961          42 :   return gc_upto(av, b);
    3962             : }
    3963             : 
    3964             : /** GRUNWALD-WANG **/
    3965             : /*
    3966             : Song Wang's PhD thesis (pdf pages)
    3967             : p.25 definition of chi_b. K^Ker(chi_b) = K(b^(1/m))
    3968             : p.26 bound on the conductor (also Cohen adv. GTM 193 p.166)
    3969             : p.21 & p.34 description special case, also on wikipedia:
    3970             : http://en.wikipedia.org/wiki/Grunwald%E2%80%93Wang_theorem#Special_fields
    3971             : p.77 Kummer case
    3972             : */
    3973             : 
    3974             : /* n > 0. Is n = 2^k ? */
    3975             : static int
    3976         385 : uispow2(ulong n) { return !(n &(n-1)); }
    3977             : 
    3978             : static GEN
    3979         441 : get_phi0(GEN bnr, GEN Lpr, GEN Ld, GEN pl, long *pr, long *pn)
    3980             : {
    3981         441 :   const long NTRY = 10; /* FIXME: magic constant */
    3982         441 :   const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    3983         441 :   GEN S = bnr_get_cyc(bnr);
    3984             :   GEN Sst, G, globGmod, loc, X, Rglob, Rloc, H, U, Lconj;
    3985             :   long i, j, r, nbfrob, nbloc, nz, t;
    3986             : 
    3987         441 :   *pn = n;
    3988         441 :   *pr = r = lg(S)-1;
    3989         441 :   if (!r) return NULL;
    3990         392 :   Sst = cgetg(r+1, t_VECSMALL); /* Z/n-dual */
    3991        1715 :   for (i=1; i<=r; i++) Sst[i] = ugcdiu(gel(S,i), n);
    3992         392 :   if (Sst[1] != n) return NULL;
    3993         385 :   Lconj = NULL;
    3994         385 :   nbloc = nbfrob = lg(Lpr)-1;
    3995         385 :   if (uispow2(n))
    3996             :   {
    3997         266 :     long l = lg(pl), k = 0;
    3998         266 :     GEN real = cgetg(l, t_VECSMALL);
    3999         994 :     for (i = 1; i < l; i++)
    4000         728 :       if (pl[i] == -1) real[++k] = i;
    4001         266 :     if (k)
    4002             :     {
    4003         266 :       GEN nf = bnr_get_nf(bnr), I = bid_get_fact(bnr_get_bid(bnr));
    4004         266 :       GEN v, y, C = idealchineseinit(bnr, I);
    4005         266 :       long r1 = nf_get_r1(nf), n = nbrows(I);
    4006         266 :       nbloc += k;
    4007         266 :       Lconj = cgetg(k+1, t_VEC);
    4008         266 :       v = const_vecsmall(r1, 1);
    4009         266 :       y = const_vec(n, gen_1);
    4010         728 :       for (i = 1; i <= k; i++)
    4011             :       {
    4012         462 :         v[real[i]] = -1; gel(Lconj,i) = idealchinese(nf, mkvec2(C,v), y);
    4013         462 :         v[real[i]] = 1;
    4014             :       }
    4015             :     }
    4016             :   }
    4017         385 :   globGmod = cgetg(r+1,t_MAT);
    4018         385 :   G = cgetg(r+1,t_VECSMALL);
    4019        1701 :   for (i = 1; i <= r; i++)
    4020             :   {
    4021        1316 :     G[i] = n / Sst[i]; /* pairing between S and Sst */
    4022        1316 :     gel(globGmod,i) = cgetg(nbloc+1,t_VECSMALL);
    4023             :   }
    4024             : 
    4025             :   /* compute images of Frobenius elements (and complex conjugation) */
    4026         385 :   loc = cgetg(nbloc+1,t_VECSMALL);
    4027         868 :   for (i = 1; i <= nbloc; i++)
    4028             :   {
    4029             :     long L;
    4030         651 :     if (i <= nbfrob)
    4031             :     {
    4032         322 :       X = gel(Lpr, i);
    4033         322 :       L = Ld[i];
    4034             :     }
    4035             :     else
    4036             :     { /* X = 1 (mod f), sigma_i(x) < 0, positive at all other real places */
    4037         329 :       X = gel(Lconj, i-nbfrob);
    4038         329 :       L = 2;
    4039             :     }
    4040         651 :     X = ZV_to_Flv(isprincipalray(bnr,X), n);
    4041        2499 :     for (nz=0,j=1; j<=r; j++)
    4042             :     {
    4043        1848 :       ulong c = (X[j] * G[j]) % L;
    4044        1848 :       ucoeff(globGmod,i,j) = c;
    4045        1848 :       if (c) nz = 1;
    4046             :     }
    4047         651 :     if (!nz) return NULL;
    4048         483 :     loc[i] = L;
    4049             :   }
    4050             : 
    4051             :   /* try some random elements in the dual */
    4052         217 :   Rglob = cgetg(r+1,t_VECSMALL);
    4053         506 :   for (t=0; t<NTRY; t++) {
    4054        1775 :     for (j = 1; j <= r; j++) Rglob[j] = random_Fl(Sst[j]);
    4055         499 :     Rloc = zm_zc_mul(globGmod,Rglob);
    4056        1164 :     for (i = 1; i <= nbloc; i++)
    4057         954 :       if (Rloc[i] % loc[i] == 0) break;
    4058         499 :     if (i > nbloc) return zv_to_ZV(Rglob);
    4059             :   }
    4060             : 
    4061             :   /* try to realize some random elements of the product of the local duals */
    4062           7 :   H = ZM_hnfall_i(shallowconcat(zm_to_ZM(globGmod),
    4063             :                                 diagonal_shallow(zv_to_ZV(loc))), &U, 2);
    4064             :   /* H,U nbloc x nbloc */
    4065           7 :   Rloc = cgetg(nbloc+1,t_COL);
    4066          77 :   for (t = 0; t < NTRY; t++)
    4067             :   { /* nonzero random coordinate */ /* TODO add special case ? */
    4068         560 :     for (i = 1; i <= nbloc; i++) gel(Rloc,i) = stoi(1 + random_Fl(loc[i]-1));
    4069          70 :     Rglob = hnf_invimage(H, Rloc);
    4070          70 :     if (Rglob)
    4071             :     {
    4072           0 :       Rglob = ZM_ZC_mul(U,Rglob);
    4073           0 :       return vecslice(Rglob,1,r);
    4074             :     }
    4075             :   }
    4076           7 :   return NULL;
    4077             : }
    4078             : 
    4079             : static GEN
    4080         441 : bnrgwsearch(GEN bnr, GEN Lpr, GEN Ld, GEN pl)
    4081             : {
    4082         441 :   pari_sp av = avma;
    4083             :   long n, r;
    4084         441 :   GEN phi0 = get_phi0(bnr,Lpr,Ld,pl, &r,&n), gn, v, H,U;
    4085         441 :   if (!phi0) return gc_const(av, gen_0);
    4086         210 :   gn = stoi(n);
    4087             :   /* compute kernel of phi0 */
    4088         210 :   v = ZV_extgcd(vec_append(phi0, gn));
    4089         210 :   U = vecslice(gel(v,2), 1,r);
    4090         210 :   H = ZM_hnfmodid(rowslice(U, 1,r), gn);
    4091         210 :   return gc_upto(av, H);
    4092             : }
    4093             : 
    4094             : GEN
    4095         210 : bnfgwgeneric(GEN bnf, GEN Lpr, GEN Ld, GEN pl, long var)
    4096             : {
    4097         210 :   pari_sp av = avma;
    4098         210 :   const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    4099             :   forprime_t S;
    4100         210 :   GEN bnr = NULL, ideal = gen_1, nf, dec, H = gen_0, finf, pol;
    4101             :   ulong ell, p;
    4102             :   long deg, i, degell;
    4103         210 :   (void)uisprimepower(n, &ell);
    4104         210 :   nf = bnf_get_nf(bnf);
    4105         210 :   deg = nf_get_degree(nf);
    4106         210 :   degell = ugcd(deg,ell-1);
    4107         210 :   finf = cgetg(lg(pl),t_VEC);
    4108         546 :   for (i=1; i<lg(pl); i++) gel(finf,i) = pl[i]==-1 ? gen_1 : gen_0;
    4109             : 
    4110         210 :   u_forprime_init(&S, 2, ULONG_MAX);
    4111         903 :   while ((p = u_forprime_next(&S))) {
    4112         903 :     if (Fl_powu(p % ell, degell, ell) != 1) continue; /* ell | p^deg-1 ? */
    4113         434 :     dec = idealprimedec(nf, utoipos(p));
    4114         784 :     for (i=1; i<lg(dec); i++) {
    4115         560 :       GEN pp = gel(dec,i);
    4116         560 :       if (RgV_isin(Lpr,pp)) continue;
    4117             :         /* TODO also accept the prime ideals at which there is a condition
    4118             :          * (use local Artin)? */
    4119         497 :       if (smodis(idealnorm(nf,pp),ell) != 1) continue; /* ell | N(pp)-1 ? */
    4120         441 :       ideal = idealmul(bnf,ideal,pp);
    4121             :       /* TODO: give factorization ? */
    4122         441 :       bnr = Buchray(bnf, mkvec2(ideal,finf), nf_INIT);
    4123         441 :       H = bnrgwsearch(bnr,Lpr,Ld,pl);
    4124         441 :       if (H != gen_0)
    4125             :       {
    4126         210 :         pol = rnfkummer(bnr,H,nf_get_prec(nf));
    4127         210 :         setvarn(pol, var);
    4128         210 :         return gc_upto(av,pol);
    4129             :       }
    4130             :     }
    4131             :   }
    4132             :   pari_err_BUG("bnfgwgeneric (no suitable p)"); /*LCOV_EXCL_LINE*/
    4133             :   return NULL;/*LCOV_EXCL_LINE*/
    4134             : }
    4135             : 
    4136             : /* pr.p != ell */
    4137             : static GEN
    4138        1624 : localextdeg(GEN nf, GEN pr, long d, ulong ell, long n)
    4139             : {
    4140             :   GEN modpr, T, p, gen, k;
    4141        1624 :   if (d == 1) return gen_1;
    4142        1610 :   k = powuu(ell, Z_lval(subiu(pr_norm(pr),1), ell));
    4143        1610 :   k = divis(k, n / d);
    4144        1610 :   modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    4145        1610 :   (void)Fq_sqrtn(gen_1, k, T, p, &gen);
    4146        1610 :   return Fq_to_nf(gen, modpr);
    4147             : }
    4148             : /* pr.p = ell */
    4149             : static GEN
    4150         175 : localextdegell(GEN nf, GEN pr, GEN E, long d, long n)
    4151             : {
    4152             :   GEN x;
    4153         175 :   if (d == 1) return gen_1;
    4154         168 :   x = nfadd(nf, gen_1, pr_get_gen(pr));
    4155         168 :   return nfpowmodideal(nf, x, stoi(n / d), idealpow(nf, pr, E));
    4156             : }
    4157             : 
    4158             : /* Ld[i] must be nontrivial powers of the same prime ell */
    4159             : /* pl : -1 at real places at which the extension must ramify, 0 elsewhere */
    4160             : GEN
    4161         294 : nfgwkummer(GEN nf, GEN Lpr, GEN Ld, GEN pl, long var)
    4162             : {
    4163         294 :   const long n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    4164             :   ulong ell;
    4165         294 :   long i, l = lg(Lpr), v = uisprimepower(n, &ell);
    4166         294 :   GEN E = cgetg(l, t_COL), y = cgetg(l, t_VEC), fa;
    4167             : 
    4168        2093 :   for (i = 1; i < l; i++)
    4169             :   {
    4170        1799 :     GEN pr = gel(Lpr,i), p = pr_get_p(pr);
    4171        1799 :     if (!absequalui(ell, p))
    4172             :     {
    4173        1624 :       gel(E, i) = gen_1;
    4174        1624 :       gel(y, i) = localextdeg(nf, pr, Ld[i], ell, n);
    4175             :     }
    4176             :     else
    4177             :     {
    4178         175 :       long e = pr_get_e(pr);
    4179         175 :       gel(E, i) = addui(1 + v*e, divsi(e, subiu(p,1)));
    4180         175 :       gel(y, i) = localextdegell(nf, pr, gel(E,i), Ld[i], n);
    4181             :     }
    4182             :   }
    4183         294 :   y = factoredextchinese(nf, mkmat2(shallowtrans(Lpr),E), y, pl, &fa);
    4184         287 :   return gsub(gpowgs(pol_x(var),n), basistoalg(nf, y));
    4185             : }
    4186             : 
    4187             : static GEN
    4188        1113 : get_vecsmall(GEN v)
    4189             : {
    4190        1113 :   switch(typ(v))
    4191             :   {
    4192         987 :     case t_VECSMALL: return v;
    4193         119 :     case t_VEC: if (RgV_is_ZV(v)) return ZV_to_zv(v);
    4194             :   }
    4195           7 :   pari_err_TYPE("nfgrunwaldwang",v);
    4196             :   return NULL;/*LCOV_EXCL_LINE*/
    4197             : }
    4198             : GEN
    4199         602 : nfgrunwaldwang(GEN nf0, GEN Lpr, GEN Ld, GEN pl, long var)
    4200             : {
    4201             :   ulong n, ell, ell2;
    4202         602 :   pari_sp av = avma;
    4203             :   GEN nf, bnf;
    4204             :   long t, w, i, vnf;
    4205             : 
    4206         602 :   if (var < 0) var = 0;
    4207         602 :   nf = get_nf(nf0,&t);
    4208         602 :   if (!nf) pari_err_TYPE("nfgrunwaldwang",nf0);
    4209         602 :   vnf = nf_get_varn(nf);
    4210         602 :   if (varncmp(var, vnf) >= 0)
    4211           7 :     pari_err_PRIORITY("nfgrunwaldwang", pol_x(var), ">=", vnf);
    4212         595 :   if (typ(Lpr) != t_VEC) pari_err_TYPE("nfgrunwaldwang",Lpr);
    4213         581 :   if (lg(Lpr) != lg(Ld)) pari_err_DIM("nfgrunwaldwang [#Lpr != #Ld]");
    4214         574 :   if (nf_get_degree(nf)==1) Lpr = shallowcopy(Lpr);
    4215        2709 :   for (i=1; i<lg(Lpr); i++) {
    4216        2142 :     GEN pr = gel(Lpr,i);
    4217        2142 :     if (nf_get_degree(nf)==1 && typ(pr)==t_INT)
    4218          77 :       gel(Lpr,i) = gel(idealprimedec(nf,pr), 1);
    4219        2065 :     else checkprid(pr);
    4220             :   }
    4221         567 :   if (lg(pl)-1 != nf_get_r1(nf))
    4222           7 :     pari_err_DOMAIN("nfgrunwaldwang [pl should have r1 components]", "#pl",
    4223           7 :         "!=", stoi(nf_get_r1(nf)), stoi(lg(pl)-1));
    4224             : 
    4225         560 :   Ld = get_vecsmall(Ld);
    4226         553 :   pl = get_vecsmall(pl);
    4227         553 :   bnf = get_bnf(nf0,&t);
    4228         553 :   n = (lg(Ld)==1)? 2: vecsmall_max(Ld);
    4229             : 
    4230         553 :   if (!uisprimepower(n, &ell))
    4231           7 :     pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (a)");
    4232        2646 :   for (i=1; i<lg(Ld); i++)
    4233        2107 :     if (Ld[i]!=1 && (!uisprimepower(Ld[i],&ell2) || ell2!=ell))
    4234           7 :       pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (b)");
    4235        1393 :   for (i=1; i<lg(pl); i++)
    4236         861 :     if (pl[i]==-1 && ell%2)
    4237           7 :       pari_err_IMPL("nfgrunwaldwang for non prime-power local degrees (c)");
    4238             : 
    4239         532 :   w = bnf? bnf_get_tuN(bnf): itos(gel(nfrootsof1(nf),1));
    4240             : 
    4241             :   /* TODO choice between kummer and generic ? Let user choose between speed
    4242             :    * and size */
    4243         532 :   if (w%n==0 && lg(Ld)>1)
    4244         294 :     return gc_upto(av, nfgwkummer(nf,Lpr,Ld,pl,var));
    4245         238 :   if (ell==n)
    4246             :   {
    4247         210 :     if (!bnf) bnf = Buchall(nf, nf_FORCE, 0);
    4248         210 :     return gc_upto(av, bnfgwgeneric(bnf,Lpr,Ld,pl,var));
    4249             :   }
    4250          28 :   pari_err_IMPL("nfgrunwaldwang for nonprime degree");
    4251             :   return NULL; /*LCOV_EXCL_LINE*/
    4252             : }
    4253             : 
    4254             : /** HASSE INVARIANTS **/
    4255             : 
    4256             : /* TODO long -> ulong + uel */
    4257             : static GEN
    4258        1484 : hasseconvert(GEN H, long n)
    4259             : {
    4260             :   GEN h, c;
    4261             :   long i, l;
    4262        1484 :   switch(typ(H)) {
    4263        1323 :     case t_VEC:
    4264        1323 :       l = lg(H); h = cgetg(l,t_VECSMALL);
    4265        1323 :       if (l == 1) return h;
    4266        1197 :       c = gel(H,1);
    4267        1197 :       if (typ(c) == t_VEC && l == 3)
    4268         476 :         return mkvec2(gel(H,1),hasseconvert(gel(H,2),n));
    4269        3374 :       for (i=1; i<l; i++)
    4270             :       {
    4271        2681 :         c = gel(H,i);
    4272        2681 :         switch(typ(c)) {
    4273         910 :           case t_INT:  break;
    4274           7 :           case t_INTMOD:
    4275           7 :             c = gel(c,2); break;
    4276        1743 :           case t_FRAC :
    4277        1743 :             c = gmulgs(c,n);
    4278        1743 :             if (typ(c) == t_INT) break;
    4279           7 :             pari_err_DOMAIN("hasseconvert [degree should be a denominator of the invariant]", "denom(h)", "ndiv", stoi(n), Q_denom(gel(H,i)));
    4280          21 :           default : pari_err_TYPE("Hasse invariant", c);
    4281             :         }
    4282        2653 :         h[i] = smodis(c,n);
    4283             :       }
    4284         693 :       return h;
    4285         154 :     case t_VECSMALL: return H;
    4286             :   }
    4287           7 :   pari_err_TYPE("Hasse invariant", H);
    4288             :   return NULL;/*LCOV_EXCL_LINE*/
    4289             : }
    4290             : 
    4291             : /* assume f >= 2 */
    4292             : static long
    4293         546 : cyclicrelfrob0(GEN nf, GEN aut, GEN pr, GEN q, long f, long g)
    4294             : {
    4295         546 :   GEN T, p, a, b, modpr = nf_to_Fq_init(nf,&pr,&T,&p);
    4296             :   long s;
    4297             : 
    4298         546 :   a = pol_x(nf_get_varn(nf));
    4299         546 :   b = galoisapply(nf, aut, modpr_genFq(modpr));
    4300         546 :   b = nf_to_Fq(nf, b, modpr);
    4301        1582 :   for (s = 0; !ZX_equal(a, b); s++) a = Fq_pow(a, q, T, p);
    4302         546 :   return g * Fl_inv(s, f); /* < n */
    4303             : }
    4304             : 
    4305             : static long
    4306        3310 : cyclicrelfrob(GEN rnf, GEN auts, GEN pr)
    4307             : {
    4308        3310 :   pari_sp av = avma;
    4309        3310 :   long f,g,frob, n = rnf_get_degree(rnf);
    4310        3310 :   GEN P = rnfidealprimedec(rnf, pr);
    4311             : 
    4312        3310 :   if (pr_get_e(gel(P,1)) > pr_get_e(pr))
    4313           0 :     pari_err_DOMAIN("cyclicrelfrob","e(PR/pr)",">",gen_1,pr);
    4314        3310 :   g = lg(P) - 1;
    4315        3310 :   f = n / g;
    4316             : 
    4317        3310 :   if (f <= 2) frob = g % n;
    4318             :   else {
    4319         546 :     GEN nf2, PR = gel(P,1);
    4320         546 :     GEN autabs = rnfeltreltoabs(rnf,gel(auts,g));
    4321         546 :     nf2 = obj_check(rnf,rnf_NFABS);
    4322         546 :     autabs = nfadd(nf2, autabs, gmul(rnf_get_k(rnf), rnf_get_alpha(rnf)));
    4323         546 :     frob = cyclicrelfrob0(nf2, autabs, PR, pr_norm(pr), f, g);
    4324             :   }
    4325        3310 :   return gc_long(av, frob);
    4326             : }
    4327             : 
    4328             : static long
    4329        1120 : localhasse(GEN rnf, GEN cnd, GEN pl, GEN auts, GEN b, long k)
    4330             : {
    4331        1120 :   pari_sp av = avma;
    4332             :   long v, m, h, lfa, frob, n, i;
    4333             :   GEN previous, y, pr, nf, q, fa;
    4334        1120 :   nf = rnf_get_nf(rnf);
    4335        1120 :   n = rnf_get_degree(rnf);
    4336        1120 :   pr = gcoeff(cnd,k,1);
    4337        1120 :   v = nfval(nf, b, pr);
    4338        1120 :   m = lg(cnd)>1 ? nbrows(cnd) : 0;
    4339             : 
    4340             :   /* add the valuation of b to the conductor... */
    4341        1120 :   previous = gcoeff(cnd,k,2);
    4342        1120 :   gcoeff(cnd,k,2) = addis(previous, v);
    4343             : 
    4344        1120 :   y = const_vec(m, gen_1);
    4345        1120 :   gel(y,k) = b;
    4346             :   /* find a factored element y congruent to b mod pr^(vpr(b)+vpr(cnd)) and to 1 mod the conductor. */
    4347        1120 :   y = factoredextchinese(nf, cnd, y, pl, &fa);
    4348        1120 :   h = 0;
    4349        1120 :   lfa = nbrows(fa);
    4350             :   /* 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). */
    4351        2169 :   for (i=1; i<=lfa; i++) {
    4352        1049 :     q = gcoeff(fa,i,1);
    4353        1049 :     if (cmp_prime_ideal(pr,q)) {
    4354         972 :       frob = cyclicrelfrob(rnf, auts, q);
    4355         972 :       frob = Fl_mul(frob,umodiu(gcoeff(fa,i,2),n),n);
    4356         972 :       h = Fl_add(h,frob,n);
    4357             :     }
    4358             :   }
    4359             :   /* ...then restore it. */
    4360        1120 :   gcoeff(cnd,k,2) = previous;
    4361        1120 :   return gc_long(av, Fl_neg(h,n));
    4362             : }
    4363             : 
    4364             : static GEN
    4365        1386 : allauts(GEN rnf, GEN aut)
    4366             : {
    4367        1386 :   long n = rnf_get_degree(rnf), i;
    4368        1386 :   GEN pol = rnf_get_pol(rnf), vaut;
    4369        1386 :   if (n==1) n=2;
    4370        1386 :   vaut = cgetg(n,t_VEC);
    4371        1386 :   aut = lift_shallow(rnfbasistoalg(rnf,aut));
    4372        1386 :   if (typ(aut) != t_POL || varn(pol) != varn(aut))
    4373           0 :     pari_err_TYPE("alg_cyclic", aut);
    4374        1386 :   gel(vaut,1) = aut;
    4375        1841 :   for (i=1; i<n-1; i++)
    4376         455 :     gel(vaut,i+1) = RgX_rem(poleval(gel(vaut,i), aut), pol);
    4377        1386 :   return vaut;
    4378             : }
    4379             : 
    4380             : static GEN
    4381         413 : clean_factor(GEN fa)
    4382             : {
    4383         413 :   GEN P2,E2, P = gel(fa,1), E = gel(fa,2);
    4384         413 :   long l = lg(P), i, j = 1;
    4385         413 :   P2 = cgetg(l, t_COL);
    4386         413 :   E2 = cgetg(l, t_COL);
    4387        2842 :   for (i = 1;i < l; i++)
    4388        2429 :     if (signe(gel(E,i))) {
    4389         721 :       gel(P2,j) = gel(P,i);
    4390         721 :       gel(E2,j) = gel(E,i); j++;
    4391             :     }
    4392         413 :   setlg(P2,j);
    4393         413 :   setlg(E2,j); return mkmat2(P2,E2);
    4394             : }
    4395             : 
    4396             : /* shallow concat x[1],...x[nx],y[1], ... y[ny], returning a t_COL. To be
    4397             :  * used when we do not know whether x,y are t_VEC or t_COL */
    4398             : static GEN
    4399         826 : colconcat(GEN x, GEN y)
    4400             : {
    4401         826 :   long i, lx = lg(x), ly = lg(y);
    4402         826 :   GEN z=cgetg(lx+ly-1, t_COL);
    4403        4396 :   for (i=1; i<lx; i++) z[i]     = x[i];
    4404        2114 :   for (i=1; i<ly; i++) z[lx+i-1]= y[i];
    4405         826 :   return z;
    4406             : }
    4407             : 
    4408             : /* return v(x) at all primes in listpr, replace x by cofactor */
    4409             : static GEN
    4410        1799 : nfmakecoprime(GEN nf, GEN *px, GEN listpr)
    4411             : {
    4412        1799 :   long j, l = lg(listpr);
    4413        1799 :   GEN x1, x = *px, L = cgetg(l, t_COL);
    4414             : 
    4415        1799 :   if (typ(x) != t_MAT)
    4416             :   { /* scalar, divide at the end (fast valuation) */
    4417        1554 :     x1 = NULL;
    4418        6307 :     for (j=1; j<l; j++)
    4419             :     {
    4420        4753 :       GEN pr = gel(listpr,j), e;
    4421        4753 :       long v = nfval(nf, x, pr);
    4422        4753 :       e = stoi(v); gel(L,j) = e;
    4423        6552 :       if (v) x1 = x1? idealmulpowprime(nf, x1, pr, e)
    4424        1799 :                     : idealpow(nf, pr, e);
    4425             :     }
    4426        1554 :     if (x1) x = idealdivexact(nf, idealhnf(nf,x), x1);
    4427             :   }
    4428             :   else
    4429             :   { /* HNF, divide as we proceed (reduce size) */
    4430         497 :     for (j=1; j<l; j++)
    4431             :     {
    4432         252 :       GEN pr = gel(listpr,j);
    4433         252 :       long v = idealval(nf, x, pr);
    4434         252 :       gel(L,j) = stoi(v);
    4435         252 :       if (v) x = idealmulpowprime(nf, x, pr, stoi(-v));
    4436             :     }
    4437             :   }
    4438        1799 :   *px = x; return L;
    4439             : }
    4440             : 
    4441             : /* Caveat: factorizations are not sorted wrt cmp_prime_ideal: Lpr comes first */
    4442             : static GEN
    4443         413 : computecnd(GEN rnf, GEN Lpr)
    4444             : {
    4445             :   GEN id, nf, fa, Le, P,E;
    4446         413 :   long n = rnf_get_degree(rnf);
    4447             : 
    4448         413 :   nf = rnf_get_nf(rnf);
    4449         413 :   id = rnf_get_idealdisc(rnf);
    4450         413 :   Le = nfmakecoprime(nf, &id, Lpr);
    4451         413 :   fa = idealfactor(nf, id); /* part of D_{L/K} coprime with Lpr */
    4452         413 :   P =  colconcat(Lpr,gel(fa,1));
    4453         413 :   E =  colconcat(Le, gel(fa,2));
    4454         413 :   fa = mkmat2(P, gdiventgs(E, eulerphiu(n)));
    4455         413 :   return mkvec2(fa, clean_factor(fa));
    4456             : }
    4457             : 
    4458             : /* h >= 0 */
    4459             : static void
    4460          77 : nextgen(GEN gene, long h, GEN* gens, GEN* hgens, long* ngens, long* curgcd) {
    4461          77 :   long nextgcd = ugcd(h,*curgcd);
    4462          77 :   if (nextgcd == *curgcd) return;
    4463          77 :   (*ngens)++;
    4464          77 :   gel(*gens,*ngens) = gene;
    4465          77 :   gel(*hgens,*ngens) = utoi(h);
    4466          77 :   *curgcd = nextgcd;
    4467          77 :   return;
    4468             : }
    4469             : 
    4470             : static int
    4471         140 : dividesmod(long d, long h, long n) { return !(h%cgcd(d,n)); }
    4472             : 
    4473             : /* ramified prime with nontrivial Hasse invariant */
    4474             : static GEN
    4475          77 : localcomplete(GEN rnf, GEN pl, GEN cnd, GEN auts, long j, long n, long h, long* v)
    4476             : {
    4477             :   GEN nf, gens, hgens, pr, modpr, T, p, sol, U, b, gene, randg, pu;
    4478             :   long ngens, i, d, np, d1, d2, hg, dnf, vcnd, curgcd;
    4479          77 :   nf = rnf_get_nf(rnf);
    4480          77 :   pr = gcoeff(cnd,j,1);
    4481          77 :   np = umodiu(pr_norm(pr), n);
    4482          77 :   dnf = nf_get_degree(nf);
    4483          77 :   vcnd = itos(gcoeff(cnd,j,2));
    4484          77 :   ngens = 13+dnf;
    4485          77 :   gens = zerovec(ngens);
    4486          77 :   hgens = zerovec(ngens);
    4487          77 :   *v = 0;
    4488          77 :   curgcd = 0;
    4489          77 :   ngens = 0;
    4490             : 
    4491          77 :   if (!uisprime(n)) {
    4492           0 :     gene =  pr_get_gen(pr);
    4493           0 :     hg = localhasse(rnf, cnd, pl, auts, gene, j);
    4494           0 :     nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
    4495             :   }
    4496             : 
    4497          77 :   if (ugcd(np,n) != 1) { /* GCD(Np,n) != 1 */
    4498          77 :     pu = idealprincipalunits(nf,pr,vcnd);
    4499          77 :     pu = abgrp_get_gen(pu);
    4500         154 :     for (i=1; i<lg(pu) && !dividesmod(curgcd,h,n); i++) {
    4501          77 :       gene = gel(pu,i);
    4502          77 :       hg = localhasse(rnf, cnd, pl, auts, gene, j);
    4503          77 :       nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
    4504             :     }
    4505             :   }
    4506             : 
    4507          77 :   d = ugcd(np-1,n);
    4508          77 :   if (d != 1) { /* GCD(Np-1,n) != 1 */
    4509          14 :     modpr = nf_to_Fq_init(nf, &pr, &T, &p);
    4510          14 :     while (!dividesmod(curgcd,h,n)) { /* TODO gener_FpXQ_local */
    4511           0 :       if (T==NULL) randg = randomi(p);
    4512           0 :       else randg = random_FpX(degpol(T), varn(T),p);
    4513             : 
    4514           0 :       if (!gequal0(randg) && !gequal1(randg)) {
    4515           0 :         gene = Fq_to_nf(randg, modpr);
    4516           0 :         hg = localhasse(rnf, cnd, pl, auts, gene, j);
    4517           0 :         nextgen(gene, hg, &gens, &hgens, &ngens, &curgcd);
    4518             :       }
    4519             :     }
    4520             :   }
    4521             : 
    4522          77 :   setlg(gens,ngens+1);
    4523          77 :   setlg(hgens,ngens+1);
    4524             : 
    4525          77 :   sol = ZV_extgcd(hgens);
    4526          77 :   U = ZV_to_Flv(gmael(sol,2,ngens), n);
    4527          77 :   d = itou(gel(sol,1));
    4528          77 :   d1 = ugcd(d, n);
    4529          77 :   d2 = d / d1;
    4530          77 :   d = Fl_mul(h / d1, Fl_inv(d2,n), n);
    4531          77 :   if (d != 1) U = Flv_Fl_mul(U, d, n);
    4532         154 :   for (i = 1, b = gen_1; i <= ngens; i++)
    4533          77 :     if (U[i]) b = nfmul(nf, b, nfpow_u(nf, gel(gens,i), U[i]));
    4534          77 :   *v = U[1]; return b;
    4535             : }
    4536             : 
    4537             : static int
    4538         909 : testsplits(GEN data, GEN fa)
    4539             : {
    4540         909 :   GEN rnf = gel(data,1), forbid = gel(data,2), P = gel(fa,1), E = gel(fa,2);
    4541         909 :   long i, n, l = lg(P);
    4542             : 
    4543        1318 :   for (i = 1; i < l; i++)
    4544             :   {
    4545         822 :     GEN pr = gel(P,i);
    4546         822 :     if (tablesearch(forbid, pr, &cmp_prime_ideal)) return 0;
    4547             :   }
    4548         496 :   n = rnf_get_degree(rnf);
    4549         704 :   for (i = 1; i < l; i++)
    4550             :   {
    4551         291 :     long e = itos(gel(E,i)) % n;
    4552         291 :     if (e)
    4553             :     {
    4554         276 :       GEN L = rnfidealprimedec(rnf, gel(P,i));
    4555         276 :       long g = lg(L) - 1;
    4556         276 :       if ((e * g) % n) return 0;
    4557             :     }
    4558             :   }
    4559         413 :   return 1;
    4560             : }
    4561             : 
    4562             : /* remove entries with Hasse invariant 0 */
    4563             : static GEN
    4564         854 : hassereduce(GEN hf)
    4565             : {
    4566         854 :   GEN pr,h, PR = gel(hf,1), H = gel(hf,2);
    4567         854 :   long i, j, l = lg(PR);
    4568             : 
    4569         854 :   pr= cgetg(l, t_VEC);
    4570         854 :   h = cgetg(l, t_VECSMALL);
    4571        4788 :   for (i = j = 1; i < l; i++)
    4572        3934 :     if (H[i]) {
    4573        3598 :       gel(pr,j) = gel(PR,i);
    4574        3598 :       h[j] = H[i]; j++;
    4575             :     }
    4576         854 :   setlg(pr,j);
    4577         854 :   setlg(h,j); return mkvec2(pr,h);
    4578             : }
    4579             : 
    4580             : static void
    4581        1036 : alg_insert_quatconj(GEN al)
    4582             : {
    4583             :   GEN aut, nf, rnf, nfabs, gene, absaut;
    4584             :   long d;
    4585        1036 :   aut = alg_get_aut(al);
    4586        1036 :   d = alg_get_absdim(al) / 4;
    4587        1036 :   nf = alg_get_center(al);
    4588        1036 :   rnf = alg_get_splittingfield(al);
    4589        1036 :   nfabs = rnf_build_nfabs(rnf, nf_get_prec(nf));
    4590        1036 :   gene = lift_shallow(rnfeltabstorel(rnf,pol_x(nf_get_varn(nfabs))));
    4591        1036 :   absaut = rnfeltreltoabs(rnf,poleval(gene,aut));
    4592        1036 :   gmael(al,6,2) = shallowmatconcat(mkmat22(
    4593             :     nfgaloismatrix(nfabs,absaut),
    4594             :     gen_0,
    4595             :     gen_0,
    4596             :     gneg(matid(2*d))
    4597             :     ));
    4598        1036 : }
    4599             : 
    4600             : /* rnf complete */
    4601             : static GEN
    4602         413 : alg_complete0(GEN rnf, GEN aut, GEN hf, GEN hi, long flag)
    4603             : {
    4604         413 :   pari_sp av = avma;
    4605             :   GEN nf, pl, pl2, cnd, prcnd, cnds, y, Lpr, auts, b, fa, data, hfe;
    4606             :   GEN forbid, al, ind, perm;
    4607             :   long D, n, d, i, j, l;
    4608         413 :   nf = rnf_get_nf(rnf);
    4609         413 :   n = rnf_get_degree(rnf);
    4610         413 :   d = nf_get_degree(nf);
    4611         413 :   D = d*n*n;
    4612         413 :   checkhasse(nf,hf,hi,n);
    4613         413 :   hf = hassereduce(hf);
    4614         413 :   Lpr = gel(hf,1);
    4615         413 :   hfe = gel(hf,2);
    4616             : 
    4617         413 :   auts = allauts(rnf,aut);
    4618             : 
    4619         413 :   pl = leafcopy(hi); /* conditions on the final b */
    4620         413 :   pl2 = leafcopy(hi); /* conditions for computing local Hasse invariants */
    4621         413 :   l = lg(pl); ind = cgetg(l, t_VECSMALL);
    4622        1036 :   for (i = j = 1; i < l; i++)
    4623         623 :     if (hi[i]) { pl[i] = -1; pl2[i] = 1; } else ind[j++] = i;
    4624         413 :   setlg(ind, j);
    4625         413 :   y = nfpolsturm(nf, rnf_get_pol(rnf), ind);
    4626         777 :   for (i = 1; i < j; i++)
    4627         364 :     if (!signe(gel(y,i))) { pl[ind[i]] = 1; pl2[ind[i]] = 1; }
    4628             : 
    4629         413 :   cnds = computecnd(rnf,Lpr);
    4630         413 :   prcnd = gel(cnds,1);
    4631         413 :   cnd = gel(cnds,2);
    4632         413 :   y = cgetg(lgcols(prcnd),t_VEC);
    4633         413 :   forbid = vectrunc_init(lg(Lpr));
    4634        2198 :   for (i=j=1; i<lg(Lpr); i++)
    4635             :   {
    4636        1785 :     GEN pr = gcoeff(prcnd,i,1), yi;
    4637        1785 :     long v, e = itou( gcoeff(prcnd,i,2) );
    4638        1785 :     if (!e) {
    4639        1708 :       long frob = cyclicrelfrob(rnf,auts,pr), f1 = ugcd(frob,n);
    4640        1708 :       vectrunc_append(forbid, pr);
    4641        1708 :       yi = gen_0;
    4642        1708 :       v = ((hfe[i]/f1) * Fl_inv(frob/f1,n)) % n;
    4643             :     }
    4644             :     else
    4645          77 :       yi = localcomplete(rnf, pl2, cnd, auts, j++, n, hfe[i], &v);
    4646        1785 :     gel(y,i) = yi;
    4647        1785 :     gcoeff(prcnd,i,2) = stoi(e + v);
    4648             :   }
    4649        1057 :   for (; i<lgcols(prcnd); i++) gel(y,i) = gen_1;
    4650         413 :   gen_sort_inplace(forbid, (void*)&cmp_prime_ideal, &cmp_nodata, NULL);
    4651         413 :   data = mkvec2(rnf,forbid);
    4652         413 :   b = factoredextchinesetest(nf,prcnd,y,pl,&fa,data,testsplits);
    4653             : 
    4654         413 :   al = cgetg(12, t_VEC);
    4655         413 :   gel(al,10)= gen_0; /* must be set first */
    4656         413 :   gel(al,1) = rnf;
    4657         413 :   gel(al,2) = auts;
    4658         413 :   gel(al,3) = basistoalg(nf,b);
    4659         413 :   gel(al,4) = hi;
    4660             :   /* add primes | disc or b with trivial Hasse invariant to hf */
    4661         413 :   Lpr = gel(prcnd,1); y = b;
    4662         413 :   (void)nfmakecoprime(nf, &y, Lpr);
    4663         413 :   Lpr = shallowconcat(Lpr, gel(idealfactor(nf,y), 1));
    4664         413 :   settyp(Lpr,t_VEC);
    4665         413 :   hf = shallowconcat(hfe, const_vecsmall(lg(Lpr)-lg(hfe), 0));
    4666         413 :   perm = gen_indexsort(Lpr, (void*)&cmp_prime_ideal, &cmp_nodata);
    4667         413 :   gel(al,5) = mkvec2(vecpermute(Lpr,perm), vecsmallpermute(hf,perm));
    4668         413 :   gel(al,6) = mkvec2(gen_0,gen_0);
    4669         413 :   gel(al,7) = matid(D);
    4670         413 :   gel(al,8) = matid(D); /* TODO modify 7, 8 et 9 once LLL added */
    4671         413 :   gel(al,9) = algnatmultable(al,D);
    4672         413 :   gel(al,11)= algtracebasis(al);
    4673             : 
    4674         413 :   if (n==2) alg_insert_quatconj(al);
    4675         413 :   if (flag & al_MAXORD) al = alg_maximal_primes(al, prV_primes(Lpr));
    4676         413 :   return gc_GEN(av, al);
    4677             : }
    4678             : 
    4679             : GEN
    4680           0 : alg_complete(GEN rnf, GEN aut, GEN hf, GEN hi, long flag)
    4681             : {
    4682           0 :   long n = rnf_get_degree(rnf);
    4683           0 :   rnfcomplete(rnf);
    4684           0 :   return alg_complete0(rnf, aut, hasseconvert(hf,n), hasseconvert(hi,n), flag);
    4685             : }
    4686             : 
    4687             : void
    4688        2254 : checkhasse(GEN nf, GEN hf, GEN hi, long n)
    4689             : {
    4690             :   GEN Lpr, Lh;
    4691             :   long i, sum;
    4692        2254 :   if (typ(hf) != t_VEC || lg(hf) != 3) pari_err_TYPE("checkhasse [hf]", hf);
    4693        2247 :   Lpr = gel(hf,1);
    4694        2247 :   Lh = gel(hf,2);
    4695        2247 :   if (typ(Lpr) != t_VEC) pari_err_TYPE("checkhasse [Lpr]", Lpr);
    4696        2247 :   if (typ(Lh) != t_VECSMALL) pari_err_TYPE("checkhasse [Lh]", Lh);
    4697        2247 :   if (typ(hi) != t_VECSMALL) pari_err_TYPE("checkhasse [hi]", hi);
    4698        2247 :   if ((nf && lg(hi) != nf_get_r1(nf)+1))
    4699           7 :     pari_err_DOMAIN("checkhasse [hi should have r1 components]","#hi","!=",stoi(nf_get_r1(nf)),stoi(lg(hi)-1));
    4700        2240 :   if (lg(Lpr) != lg(Lh))
    4701           7 :     pari_err_DIM("checkhasse [Lpr and Lh should have same length]");
    4702        9646 :   for (i=1; i<lg(Lpr); i++) checkprid(gel(Lpr,i));
    4703        2233 :   if (lg(gen_sort_uniq(Lpr, (void*)cmp_prime_ideal, cmp_nodata)) < lg(Lpr))
    4704           7 :     pari_err(e_MISC, "error in checkhasse [duplicate prime ideal]");
    4705        2226 :   sum = 0;
    4706        9625 :   for (i=1; i<lg(Lh); i++) sum = (sum+Lh[i])%n;
    4707        5278 :   for (i=1; i<lg(hi); i++) {
    4708        3066 :       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]));
    4709        3052 :       sum = (sum+hi[i])%n;
    4710             :   }
    4711        2212 :   if (sum<0) sum = n+sum;
    4712        2212 :   if (sum != 0)
    4713           7 :     pari_err_DOMAIN("checkhasse","sum(Hasse invariants)","!=",gen_0,Lh);
    4714        2205 : }
    4715             : 
    4716             : static GEN
    4717         511 : hassecoprime(GEN hf, GEN hi, long n)
    4718             : {
    4719         511 :   pari_sp av = avma;
    4720             :   long l, i, j, lk, inv;
    4721             :   GEN fa, P,E, res, hil, hfl;
    4722         511 :   hi = hasseconvert(hi, n);
    4723         497 :   hf = hasseconvert(hf, n);
    4724         476 :   checkhasse(NULL,hf,hi,n);
    4725         434 :   fa = factoru(n);
    4726         434 :   P = gel(fa,1); l = lg(P);
    4727         434 :   E = gel(fa,2);
    4728         434 :   res = cgetg(l,t_VEC);
    4729         875 :   for (i=1; i<l; i++) {
    4730         441 :     lk = upowuu(P[i],E[i]);
    4731         441 :     inv = Fl_invsafe((n/lk)%lk, lk);
    4732         441 :     hil = gcopy(hi);
    4733         441 :     hfl = gcopy(hf);
    4734             : 
    4735         441 :     if (P[i] == 2)
    4736         896 :       for (j=1; j<lg(hil); j++) hil[j] = hi[j]==0 ? 0 : lk/2;
    4737             :     else
    4738         203 :       for (j=1; j<lg(hil); j++) hil[j] = 0;
    4739        2590 :     for (j=1; j<lgcols(hfl); j++) gel(hfl,2)[j] = (gel(hf,2)[j]*inv)%lk;
    4740         441 :     hfl = hassereduce(hfl);
    4741         441 :     gel(res,i) = mkvec3(hfl,hil,utoi(lk));
    4742             :   }
    4743             : 
    4744         434 :   return gc_GEN(av, res);
    4745             : }
    4746             : 
    4747             : /* no garbage collection */
    4748             : static GEN
    4749         140 : genefrob(GEN nf, GEN gal, GEN r)
    4750             : {
    4751             :   long i;
    4752         140 :   GEN g = identity_perm(nf_get_degree(nf)), fa = Z_factor(r), p, pr, frob;
    4753         217 :   for (i=1; i<lgcols(fa); i++) {
    4754          77 :     p = gcoeff(fa,i,1);
    4755          77 :     pr = idealprimedec(nf, p);
    4756          77 :     pr = gel(pr,1);
    4757          77 :     frob = idealfrobenius(nf, gal, pr);
    4758          77 :     g = perm_mul(g, perm_pow(frob, gcoeff(fa,i,2)));
    4759             :   }
    4760         140 :   return g;
    4761             : }
    4762             : 
    4763             : static GEN
    4764         413 : rnfcycaut(GEN rnf)
    4765             : {
    4766         413 :   GEN nf2 = obj_check(rnf, rnf_NFABS);
    4767             :   GEN L, alpha, pol, salpha, s, sj, polabs, k, X, pol0, nf;
    4768             :   long i, d, j;
    4769         413 :   d = rnf_get_degree(rnf);
    4770         413 :   L = galoisconj(nf2,NULL);
    4771         413 :   alpha = lift_shallow(rnf_get_alpha(rnf));
    4772         413 :   pol = rnf_get_pol(rnf);
    4773         413 :   k = rnf_get_k(rnf);
    4774         413 :   polabs = rnf_get_polabs(rnf);
    4775         413 :   nf = rnf_get_nf(rnf);
    4776         413 :   pol0 = nf_get_pol(nf);
    4777         413 :   X = RgX_rem(pol_x(varn(pol0)), pol0);
    4778             : 
    4779             :   /* TODO check mod prime of degree 1 */
    4780         630 :   for (i=1; i<lg(L); i++) {
    4781         630 :     s = gel(L,i);
    4782         630 :     salpha = RgX_RgXQ_eval(alpha,s,polabs);
    4783         630 :     if (!gequal(alpha,salpha)) continue;
    4784             : 
    4785         553 :     s = lift_shallow(rnfeltabstorel(rnf,s));
    4786         553 :     sj = s = gsub(s, gmul(k,X));
    4787        1092 :     for (j=1; !gequal0(gsub(sj,pol_x(varn(s)))); j++)
    4788         539 :       sj = RgX_RgXQ_eval(sj,s,pol);
    4789         553 :     if (j<d) continue;
    4790         413 :     return s;
    4791             :   }
    4792             :   return NULL; /*LCOV_EXCL_LINE*/
    4793             : }
    4794             : 
    4795             : /* returns the smallest prime not in P */
    4796             : static GEN
    4797          84 : extraprime(GEN P)
    4798             : {
    4799             :   forprime_t T;
    4800             :   GEN p;
    4801          84 :   forprime_init(&T, gen_2, NULL);
    4802          98 :   while ((p = forprime_next(&T))) if (!ZV_search(P, p)) break;
    4803          84 :   return p;
    4804             : }
    4805             : 
    4806             : /* true nf */
    4807             : GEN
    4808         525 : alg_hasse(GEN nf, long n, GEN hf, GEN hi, long var, long flag)
    4809             : {
    4810         525 :   pari_sp av = avma;
    4811         525 :   GEN primary, al = gen_0, al2, rnf, hil, hfl, Ld, pl, pol, Lpr, aut, Lpr2, Ld2;
    4812             :   long i, lk, j, maxdeg;
    4813         525 :   dbg_printf(1)("alg_hasse\n");
    4814         525 :   if (n<=1) pari_err_DOMAIN("alg_hasse", "degree", "<=", gen_1, stoi(n));
    4815         511 :   primary = hassecoprime(hf, hi, n);
    4816         854 :   for (i=1; i<lg(primary); i++) {
    4817         441 :     lk = itos(gmael(primary,i,3));
    4818         441 :     hfl = gmael(primary,i,1);
    4819         441 :     hil = gmael(primary,i,2);
    4820         441 :     checkhasse(nf, hfl, hil, lk);
    4821         434 :     dbg_printf(1)("alg_hasse: i=%d hf=%Ps hi=%Ps lk=%d\n", i, hfl, hil, lk);
    4822             : 
    4823         434 :     if (lg(gel(hfl,1))>1 || lk%2==0) {
    4824         427 :       maxdeg = 1;
    4825         427 :       Lpr = gel(hfl,1);
    4826         427 :       Ld = gcopy(gel(hfl,2));
    4827        2226 :       for (j=1; j<lg(Ld); j++)
    4828             :       {
    4829        1799 :         Ld[j] = lk/ugcd(lk,Ld[j]);
    4830        1799 :         maxdeg = maxss(Ld[j],maxdeg);
    4831             :       }
    4832         427 :       pl = leafcopy(hil);
    4833        1064 :       for (j=1; j<lg(pl); j++) if(pl[j])
    4834             :       {
    4835         259 :         pl[j] = -1;
    4836         259 :         maxdeg = maxss(maxdeg,2);
    4837             :       }
    4838             : 
    4839         427 :       Lpr2 = Lpr;
    4840         427 :       Ld2 = Ld;
    4841         427 :       if (maxdeg<lk)
    4842             :       {
    4843         154 :         if (maxdeg==1 && lk==2 && lg(pl)>1) pl[1] = -1;
    4844             :         else
    4845             :         {
    4846          84 :           GEN p = extraprime(prV_primes(Lpr));
    4847          84 :           Lpr2 = vec_append(Lpr2, idealprimedec_galois(nf, p));
    4848          84 :           Ld2 = vecsmall_append(Ld2, lk);
    4849             :         }
    4850             :       }
    4851             : 
    4852         427 :       dbg_printf(2)("alg_hasse: calling nfgrunwaldwang Lpr=%Ps Pd=%Ps pl=%Ps\n",
    4853             :           Lpr, Ld, pl);
    4854         427 :       pol = nfgrunwaldwang(nf, Lpr2, Ld2, pl, var);
    4855         413 :       dbg_printf(2)("alg_hasse: calling rnfinit(%Ps)\n", pol);
    4856         413 :       rnf = rnfinit0(nf,pol,1);
    4857         413 :       dbg_printf(2)("alg_hasse: computing automorphism\n");
    4858         413 :       aut = rnfcycaut(rnf);
    4859         413 :       dbg_printf(2)("alg_hasse: calling alg_complete\n");
    4860         413 :       al2 = alg_complete0(rnf, aut, hfl, hil, flag);
    4861             :     }
    4862           7 :     else al2 = alg_matrix(nf, lk, var, flag);
    4863             : 
    4864         420 :     if (i==1) al = al2;
    4865           7 :     else      al = algtensor(al,al2,flag);
    4866             :   }
    4867         413 :   return gc_GEN(av,al);
    4868             : }
    4869             : 
    4870             : /** CYCLIC ALGEBRA WITH GIVEN HASSE INVARIANTS **/
    4871             : 
    4872             : /* no garbage collection */
    4873             : static GEN
    4874         140 : subcycloindep(GEN nf, long n, long v, GEN *pr)
    4875             : {
    4876             :   pari_sp av;
    4877             :   forprime_t S;
    4878             :   ulong p;
    4879         140 :   u_forprime_arith_init(&S, 1, ULONG_MAX, 1, n);
    4880         140 :   av = avma;
    4881         147 :   while ((p = u_forprime_next(&S)))
    4882             :   {
    4883         147 :     ulong r = pgener_Fl(p);
    4884         147 :     GEN pol = galoissubcyclo(utoipos(p), utoipos(Fl_powu(r,n,p)), 0, v);
    4885         147 :     GEN fa = nffactor(nf, pol);
    4886         147 :     if (lgcols(fa) == 2) { *pr = utoipos(r); return pol; }
    4887           7 :     set_avma(av);
    4888             :   }
    4889             :   pari_err_BUG("subcycloindep (no suitable prime = 1(mod n))"); /*LCOV_EXCL_LINE*/
    4890             :   *pr = NULL; return NULL; /*LCOV_EXCL_LINE*/
    4891             : }
    4892             : 
    4893             : GEN
    4894         147 : alg_matrix(GEN nf, long n, long v, long flag)
    4895             : {
    4896         147 :   pari_sp av = avma;
    4897             :   GEN pol, gal, rnf, cyclo, g, r, aut;
    4898         147 :   dbg_printf(1)("alg_matrix\n");
    4899         147 :   if (n<=0) pari_err_DOMAIN("alg_matrix", "n", "<=", gen_0, stoi(n));
    4900         140 :   pol = subcycloindep(nf, n, v, &r);
    4901         140 :   rnf = rnfinit(nf, pol);
    4902         140 :   cyclo = nfinit(pol, nf_get_prec(nf));
    4903         140 :   gal = galoisinit(cyclo, NULL);
    4904         140 :   g = genefrob(cyclo,gal,r);
    4905         140 :   aut = galoispermtopol(gal,g);
    4906         140 :   return gc_upto(av, alg_cyclic(rnf, aut, gen_1, flag));
    4907             : }
    4908             : 
    4909             : static GEN
    4910          28 : alg_hilbert_asquare(GEN nf, GEN a, GEN sa, GEN b, long v, long flag)
    4911             : {
    4912             :   GEN mt, al, ord, z1, z2, den, invol;
    4913          28 :   long d = nf_get_degree(nf), i;
    4914          28 :   mt = mkvec4(
    4915             :       matid(4),
    4916             :       mkmat4(
    4917             :         mkcol4(gen_0,gen_1,gen_0,gen_0),
    4918             :         mkcol4(a,gen_0,gen_0,gen_0),
    4919             :         mkcol4(gen_0,gen_0,gen_0,gen_1),
    4920             :         mkcol4(gen_0,gen_0,a,gen_0)
    4921             :       ),
    4922             :       mkmat4(
    4923             :         mkcol4(gen_0,gen_0,gen_1,gen_0),
    4924             :         mkcol4(gen_0,gen_0,gen_0,gen_m1),
    4925             :         mkcol4(b,gen_0,gen_0,gen_0),
    4926             :         mkcol4(gen_0,gneg(b),gen_0,gen_0)
    4927             :       ),
    4928             :       mkmat4(
    4929             :         mkcol4(gen_0,gen_0,gen_0,gen_1),
    4930             :         mkcol4(gen_0,gen_0,gneg(a),gen_0),
    4931             :         mkcol4(gen_0,b,gen_0,gen_0),
    4932             :         mkcol4(gneg(gmul(a,b)),gen_0,gen_0,gen_0)
    4933             :       )
    4934             :   );
    4935          28 :   al = alg_csa_table(nf, mt, v, al_NOSPLITTING);
    4936             : 
    4937             :   /* set trivial Hasse invariants */
    4938          28 :   gel(al,4) = zero_zv(nf_get_r1(nf));
    4939          28 :   gel(al,5) = mkvec2(cgetg(1,t_VEC),cgetg(1,t_VECSMALL));
    4940             : 
    4941             :   /* remember special case */
    4942          28 :   sa = basistoalg(nf,sa);
    4943          28 :   gmael(al,6,1) = mkvec3(a,b,sa);
    4944          28 :   invol = matid(4*d);
    4945         196 :   for (i=d+1; i<lg(invol); i++) gcoeff(invol,i,i) = gen_m1;
    4946          28 :   gmael(al,6,2) = invol;
    4947             : 
    4948          28 :   if (flag & al_MAXORD)
    4949             :   {
    4950          28 :     ord = cgetg(4,t_VEC);
    4951             : 
    4952          28 :     z1 = mkfracss(1,2); /* 1/2 */
    4953          28 :     z2 = gmul2n(ginv(sa),-1); /* 1/(2*sa) */
    4954             :     /* (1+i/sa)/2 */
    4955          28 :     gel(ord,1) = algleftmultable(al,mkcol4(z1,z2,gen_0,gen_0));
    4956             :     /* (j-ij/sa)/2 */
    4957          28 :     gel(ord,2) = algleftmultable(al,mkcol4(gen_0,gen_0,z1,gneg(z2)));
    4958          28 :     z1 = basistoalg(nf,nfdiv(nf,z1,b));
    4959          28 :     z2 = basistoalg(nf,nfdiv(nf,z2,b));
    4960             :     /* (j/b + ij/(b*sa))/2 */
    4961          28 :     gel(ord,3) = algleftmultable(al,mkcol4(gen_0,gen_0,z1,z2));
    4962             : 
    4963             :     /* multiply by nf.zk == d first vectors of natural basis */
    4964         112 :     for (i=1; i<=3; i++) gel(ord,i) = vecslice(gel(ord,i),1,d);
    4965             : 
    4966          28 :     ord = shallowmatconcat(ord);
    4967          28 :     ord = Q_remove_denom(ord, &den);
    4968          28 :     ord = hnfmodid(ord, den);
    4969          28 :     ord = ZM_Z_div(ord, den);
    4970          28 :     al = alg_change_overorder_shallow(al, ord);
    4971             :   }
    4972             :   /* could take splitting field == nf */
    4973          28 :   computesplitting(al, 2, v, flag);
    4974          28 :   return al;
    4975             : }
    4976             : 
    4977             : GEN
    4978         658 : alg_hilbert(GEN nf, GEN a, GEN b, long v, long flag)
    4979             : {
    4980         658 :   pari_sp av = avma;
    4981             :   GEN rnf, aut, rnfpol, sa;
    4982         658 :   dbg_printf(1)("alg_hilbert\n");
    4983         658 :   if (gequal0(a)) pari_err_DOMAIN("alg_hilbert", "a", "=", gen_0, a);
    4984         651 :   if (gequal0(b)) pari_err_DOMAIN("alg_hilbert", "b", "=", gen_0, b);
    4985         644 :   if (!isint1(Q_denom(algtobasis(nf,a))))
    4986           7 :     pari_err_DOMAIN("alg_hilbert", "denominator(a)", "!=", gen_1,a);
    4987         637 :   if (!isint1(Q_denom(algtobasis(nf,b))))
    4988           7 :     pari_err_DOMAIN("alg_hilbert", "denominator(b)", "!=", gen_1,b);
    4989         630 :   if (nfissquare(nf,a,&sa))
    4990          28 :     return gc_GEN(av, alg_hilbert_asquare(nf,a,sa,b,v,flag));
    4991             : 
    4992         602 :   if (v < 0) v = 0;
    4993         602 :   rnfpol = deg2pol_shallow(gen_1, gen_0, gneg(basistoalg(nf,a)), v);
    4994         602 :   if (!(flag & al_FACTOR)) rnfpol = mkvec2(rnfpol, stoi(1<<20));
    4995         602 :   rnf = rnfinit(nf, rnfpol);
    4996         602 :   aut = gneg(pol_x(v));
    4997         602 :   return gc_upto(av, alg_cyclic(rnf, aut, b, flag));
    4998             : }
    4999             : 
    5000             : /* shortcut for alg_hasse in quaternion case */
    5001             : static GEN
    5002          77 : alg_quatramif(GEN nf, GEN Lpr, GEN hi, long var, long flag)
    5003             : {
    5004          77 :   pari_sp av = avma;
    5005          77 :   GEN hf = mkvec2(Lpr, const_vecsmall(lg(Lpr)-1,1));
    5006          77 :   return gc_upto(av, alg_hasse(nf, 2, hf, hi, var, flag));
    5007             : }
    5008             : 
    5009             : /* return a structure representing the algebra of real numbers */
    5010             : static GEN
    5011          28 : mk_R()
    5012             : {
    5013          28 :   pari_sp av = avma;
    5014             :   GEN al;
    5015          28 :   al = zerovec(11);
    5016          28 :   gel(al,1) = stor(1, LOWDEFAULTPREC);
    5017          28 :   gel(al,2) = mkvec(gel(al,1));
    5018          28 :   gel(al,3) = gen_1;
    5019          28 :   gel(al,4) = mkvecsmall(0);
    5020          28 :   gel(al,6) = mkvec2(gen_0,matid(1));
    5021          28 :   gel(al,8) = gel(al,7) = matid(1);
    5022          28 :   gel(al,9) = mkvec(matid(1));
    5023          28 :   return gc_GEN(av,al);
    5024             : }
    5025             : /* return a structure representing the algebra of complex numbers */
    5026             : static GEN
    5027          21 : mk_C()
    5028             : {
    5029          21 :   pari_sp av = avma;
    5030             :   GEN al, I;
    5031          21 :   al = zerovec(11);
    5032          21 :   I = gen_I();
    5033          21 :   gel(al,1) = I;
    5034          21 :   gel(al,2) = mkvec(I);
    5035          21 :   gel(al,3) = gen_1;
    5036          21 :   gel(al,4) = cgetg(1,t_VECSMALL);
    5037          21 :   gel(al,6) = mkvec2(gen_0,mkmat22(gen_1,gen_0,gen_0,gen_m1));
    5038          21 :   gel(al,8) = gel(al,7) = matid(2);
    5039          21 :   gel(al,9) = mkvec2(
    5040             :     matid(2),
    5041             :     mkmat22(gen_0,gen_m1,gen_1,gen_0)
    5042             :   );
    5043          21 :   return gc_GEN(av,al);
    5044             : }
    5045             : /* return a structure representing the Hamilton quaternion algebra */
    5046             : static GEN
    5047          56 : mk_H()
    5048             : {
    5049          56 :   pari_sp av = avma;
    5050             :   GEN al, I;
    5051          56 :   al = zerovec(11);
    5052          56 :   I = gen_I();
    5053          56 :   gel(al,1) = I;
    5054          56 :   gel(al,2) = mkvec(gconj(I));
    5055          56 :   gel(al,3) = gen_m1;
    5056          56 :   gel(al,4) = mkvecsmall(1);
    5057          56 :   gel(al,6) = mkvec2(gen_0, H_invol(NULL));
    5058          56 :   gel(al,8) = gel(al,7) = matid(4);
    5059          56 :   gel(al,9) = mkvec4(
    5060             :     matid(4),
    5061             :     H_tomatrix(I,1),
    5062             :     H_tomatrix(mkcol4(gen_0,gen_0,gen_1,gen_0),1),
    5063             :     H_tomatrix(mkcol4(gen_0,gen_0,gen_0,gen_1),1)
    5064             :   );
    5065          56 :   return gc_GEN(av,al);
    5066             : }
    5067             : 
    5068             : GEN
    5069        1995 : alginit(GEN A, GEN B, long v, long flag)
    5070             : {
    5071             :   long w;
    5072        1995 :   if (typ(A) == t_COMPLEX) return mk_C();
    5073        1974 :   if (typ(A) == t_REAL)
    5074             :   {
    5075          91 :     if (is_scalar_t(typ(B)) && gequal0(B)) return mk_R();
    5076          63 :     if (typ(B) == t_FRAC && gequal(B, mkfrac(gen_1,gen_2))) return mk_H();
    5077           7 :     pari_err_DOMAIN("alginit", "real Hasse invariant [must be 0 or 1/2]", "", NULL, B);
    5078             :   }
    5079        1883 :   switch(nftyp(A))
    5080             :   {
    5081        1624 :     case typ_NF:
    5082        1624 :       if (v<0) v=0;
    5083        1624 :       w = gvar(nf_get_pol(A));
    5084        1624 :       if (varncmp(v,w)>=0) pari_err_PRIORITY("alginit", pol_x(v), ">=", w);
    5085        1610 :       switch(typ(B))
    5086             :       {
    5087             :         long nB;
    5088         140 :         case t_INT: return alg_matrix(A, itos(B), v, flag);
    5089        1463 :         case t_VEC:
    5090        1463 :           nB = lg(B)-1;
    5091        1463 :           if (nB && typ(gel(B,1)) == t_MAT) return alg_csa_table(A,B,v,flag);
    5092             :           switch(nB)
    5093             :           {
    5094         735 :             case 2:
    5095         735 :               if (typ(gel(B,1)) == t_VEC)
    5096          77 :                 return alg_quatramif(A, gel(B,1), gel(B,2), v, flag);
    5097         658 :               return alg_hilbert(A, gel(B,1), gel(B,2), v, flag);
    5098         455 :             case 3:
    5099         455 :               if (typ(gel(B,1))!=t_INT)
    5100           7 :                   pari_err_TYPE("alginit [degree should be an integer]", gel(B,1));
    5101         448 :               return alg_hasse(A, itos(gel(B,1)), gel(B,2), gel(B,3), v,
    5102             :                                                                       flag);
    5103             :           }
    5104             :       }
    5105          14 :       pari_err_TYPE("alginit", B); break;
    5106             : 
    5107         245 :     case typ_RNF:
    5108         245 :       if (typ(B) != t_VEC || lg(B) != 3) pari_err_TYPE("alginit", B);
    5109         231 :       return alg_cyclic(A, gel(B,1), gel(B,2), flag);
    5110             :   }
    5111          14 :   pari_err_TYPE("alginit", A);
    5112             :   return NULL;/*LCOV_EXCL_LINE*/
    5113             : }
    5114             : 
    5115             : /* assumes al CSA or CYCLIC */
    5116             : static GEN
    5117        1652 : algnatmultable(GEN al, long D)
    5118             : {
    5119             :   GEN res, x;
    5120             :   long i;
    5121        1652 :   res = cgetg(D+1,t_VEC);
    5122       17738 :   for (i=1; i<=D; i++) {
    5123       16086 :     x = algnattoalg(al,col_ei(D,i));
    5124       16086 :     gel(res,i) = algZmultable(al,x);
    5125             :   }
    5126        1652 :   return res;
    5127             : }
    5128             : 
    5129         168 : static int normfact_is_partial(GEN nf, GEN x, GEN fax)
    5130             : {
    5131             :   long i;
    5132             :   GEN nfx;
    5133         168 :   nfx = RgM_shallowcopy(fax);
    5134         434 :   for (i=1; i<lg(gel(nfx,1)); i++)
    5135         266 :     gcoeff(nfx,i,1) = idealnorm(nf, gcoeff(nfx,i,1));
    5136         168 :   nfx = factorback(nfx);
    5137         168 :   return !gequal(idealnorm(nf, x), nfx);
    5138             : }
    5139             : /* no garbage collection */
    5140             : static void
    5141         973 : algcomputehasse(GEN al, long flag)
    5142             : {
    5143             :   int partialfact;
    5144             :   long r1, k, n, m, m1, m2, m3, i, m23, m123;
    5145             :   GEN rnf, nf, b, fab, disc2, cnd, fad, auts, pr, pl, perm, y, hi, PH, H, L;
    5146             : 
    5147         973 :   rnf = alg_get_splittingfield(al);
    5148         973 :   n = rnf_get_degree(rnf);
    5149         973 :   nf = rnf_get_nf(rnf);
    5150         973 :   b = alg_get_b(al);
    5151         973 :   r1 = nf_get_r1(nf);
    5152         973 :   auts = alg_get_auts(al);
    5153         973 :   (void)alg_get_abssplitting(al);
    5154             : 
    5155         973 :   y = nfpolsturm(nf, rnf_get_pol(rnf), NULL);
    5156         973 :   pl = cgetg(r1+1, t_VECSMALL);
    5157             :   /* real places where rnf/nf ramifies */
    5158        2170 :   for (k = 1; k <= r1; k++) pl[k] = !signe(gel(y,k));
    5159             : 
    5160             :   /* infinite Hasse invariants */
    5161         973 :   if (odd(n)) hi = const_vecsmall(r1, 0);
    5162             :   else
    5163             :   {
    5164         805 :     GEN s = nfsign(nf, b);
    5165         805 :     hi = cgetg(r1+1, t_VECSMALL);
    5166        1834 :     for (k = 1; k<=r1; k++) hi[k] = (s[k] && pl[k]) ? (n/2) : 0;
    5167             :   }
    5168         973 :   gel(al,4) = hi;
    5169             : 
    5170         973 :   partialfact = 0;
    5171         973 :   if (flag & al_FACTOR)
    5172         868 :     fab = idealfactor(nf, b);
    5173             :   else {
    5174         105 :     fab = idealfactor_limit(nf, b, 1<<20);
    5175             :     /* does not report whether factorisation was partial; check it */
    5176         105 :     partialfact = normfact_is_partial(nf, b, fab);
    5177             :   }
    5178             : 
    5179         973 :   disc2 = rnf_get_idealdisc(rnf);
    5180         973 :   L = nfmakecoprime(nf, &disc2, gel(fab,1));
    5181         973 :   m = lg(L)-1;
    5182             :   /* m1 = #{pr|b: pr \nmid disc}, m3 = #{pr|b: pr | disc} */
    5183         973 :   perm = cgetg(m+1, t_VECSMALL);
    5184        1764 :   for (i=1, m1=m, k=1; k<=m; k++)
    5185         791 :     if (signe(gel(L,k))) perm[m1--] = k; else perm[i++] = k;
    5186         973 :   m3 = m - m1;
    5187             : 
    5188             :   /* disc2 : factor of disc coprime to b */
    5189         973 :   if (flag & al_FACTOR)
    5190         868 :     fad = idealfactor(nf, disc2);
    5191             :   else {
    5192         105 :     fad = idealfactor_limit(nf, disc2, 1<<20);
    5193         105 :     partialfact = partialfact || normfact_is_partial(nf, disc2, fad);
    5194             :   }
    5195             : 
    5196             :   /* if factorisation is partial, do not compute Hasse invariants */
    5197             :   /* we could compute their sum at composite factors */
    5198         973 :   if (partialfact)
    5199             :   {
    5200          49 :     if (!(flag & al_MAXORD))
    5201             :     {
    5202          42 :       gel(al,5) = gen_0;
    5203          49 :       return;
    5204             :     }
    5205             :     /* but transmit list of factors found for computation of maximal order */
    5206           7 :     PH = prV_primes(shallowconcat(gel(fab,1), gel(fad,1)));
    5207           7 :     gel(al,5) = mkvec2(PH, gen_0);;
    5208           7 :     return;
    5209             :   }
    5210             : 
    5211             :   /* m2 : number of prime factors of disc not dividing b */
    5212         924 :   m2 = nbrows(fad);
    5213         924 :   m23 = m2+m3;
    5214         924 :   m123 = m1+m2+m3;
    5215             : 
    5216             :   /* initialize the possibly ramified primes (hasse) and the factored conductor of rnf/nf (cnd) */
    5217         924 :   cnd = zeromatcopy(m23,2);
    5218         924 :   PH = cgetg(m123+1, t_VEC); /* ramified primes */
    5219         924 :   H = cgetg(m123+1, t_VECSMALL); /* Hasse invariant */
    5220             :   /* compute Hasse invariant at primes that are unramified in rnf/nf */
    5221        1554 :   for (k=1; k<=m1; k++) {/* pr | b, pr \nmid disc */
    5222         630 :     long frob, e, j = perm[k];
    5223         630 :     pr = gcoeff(fab,j,1);
    5224         630 :     e = itos(gcoeff(fab,j,2));
    5225         630 :     frob = cyclicrelfrob(rnf, auts, pr);
    5226         630 :     gel(PH,k) = pr;
    5227         630 :     H[k] = Fl_mul(frob, e, n);
    5228             :   }
    5229             :   /* compute Hasse invariant at primes that are ramified in rnf/nf */
    5230        1890 :   for (k=1; k<=m2; k++) {/* pr \nmid b, pr | disc */
    5231         966 :     pr = gcoeff(fad,k,1);
    5232         966 :     gel(PH,k+m1) = pr;
    5233         966 :     gcoeff(cnd,k,1) = pr;
    5234         966 :     gcoeff(cnd,k,2) = gcoeff(fad,k,2);
    5235             :   }
    5236        1001 :   for (k=1; k<=m3; k++) { /* pr | (b, disc) */
    5237          77 :     long j = perm[k+m1];
    5238          77 :     pr = gcoeff(fab,j,1);
    5239          77 :     gel(PH,k+m1+m2) = pr;
    5240          77 :     gcoeff(cnd,k+m2,1) = pr;
    5241          77 :     gcoeff(cnd,k+m2,2) = gel(L,j);
    5242             :   }
    5243         924 :   gel(cnd,2) = gdiventgs(gel(cnd,2), eulerphiu(n));
    5244        1967 :   for (k=1; k<=m23; k++) H[k+m1] = localhasse(rnf, cnd, pl, auts, b, k);
    5245         924 :   perm = gen_indexsort(PH, (void*)&cmp_prime_ideal, &cmp_nodata);
    5246         924 :   gel(al,5) = mkvec2(vecpermute(PH,perm),vecsmallpermute(H,perm));
    5247         924 :   checkhasse(nf, alg_get_hasse_f(al), alg_get_hasse_i(al), n);
    5248             : }
    5249             : 
    5250             : static GEN
    5251        1442 : alg_maximal_primes(GEN al, GEN P)
    5252             : {
    5253        1442 :   pari_sp av = avma;
    5254        1442 :   long l = lg(P), i;
    5255        4713 :   for (i=1; i<l; i++)
    5256             :   {
    5257        3271 :     if (i != 1) al = gc_GEN(av, al);
    5258        3271 :     al = alg_pmaximal(al,gel(P,i));
    5259             :   }
    5260        1442 :   return al;
    5261             : }
    5262             : 
    5263             : GEN
    5264         987 : alg_cyclic(GEN rnf, GEN aut, GEN b, long flag)
    5265             : {
    5266         987 :   pari_sp av = avma;
    5267             :   GEN al, nf;
    5268             :   long D, n, d;
    5269         987 :   dbg_printf(1)("alg_cyclic\n");
    5270         987 :   checkrnf(rnf); nf = rnf_get_nf(rnf);
    5271         987 :   b = nf_to_scalar_or_basis(nf, b);
    5272         980 :   if (typ(b) == t_FRAC || (typ(b) == t_COL && !RgV_is_ZV(b)))
    5273           7 :     pari_err_DOMAIN("alg_cyclic", "denominator(b)", "!=", gen_1,b);
    5274             : 
    5275         973 :   n = rnf_get_degree(rnf);
    5276         973 :   d = nf_get_degree(nf);
    5277         973 :   D = d*n*n;
    5278             : 
    5279         973 :   al = cgetg(12,t_VEC);
    5280         973 :   gel(al,10)= gen_0; /* must be set first */
    5281         973 :   gel(al,1) = rnf;
    5282         973 :   gel(al,2) = allauts(rnf, aut);
    5283         973 :   gel(al,3) = basistoalg(nf,b);
    5284         973 :   rnf_build_nfabs(rnf, nf_get_prec(nf));
    5285         973 :   gel(al,6) = mkvec2(gen_0,gen_0);
    5286         973 :   gel(al,7) = matid(D);
    5287         973 :   gel(al,8) = matid(D); /* TODO modify 7, 8 et 9 once LLL added */
    5288         973 :   gel(al,9) = algnatmultable(al,D);
    5289         973 :   gel(al,11)= algtracebasis(al);
    5290             : 
    5291         973 :   if (n==2) alg_insert_quatconj(al);
    5292             : 
    5293         973 :   algcomputehasse(al, flag);
    5294             : 
    5295         973 :   if (flag & al_MAXORD) {
    5296         854 :     GEN hf = alg_get_hasse_f(al), pr = gel(hf,1);
    5297         854 :     if (typ(gel(hf,2)) == t_INT) /* factorisation was partial */
    5298           7 :       gel(al,5) = gen_0;
    5299         847 :     else pr = prV_primes(pr);
    5300         854 :     al = alg_maximal_primes(al, pr);
    5301             :   }
    5302         973 :   return gc_GEN(av, al);
    5303             : }
    5304             : 
    5305             : static int
    5306         630 : ismaximalsubfield(GEN al, GEN x, GEN d, long v, GEN *pt_minpol)
    5307             : {
    5308         630 :   GEN cp = algbasischarpoly(al, x, v), lead;
    5309         630 :   if (!ispower(cp, d, pt_minpol)) return 0;
    5310         630 :   lead = leading_coeff(*pt_minpol);
    5311         630 :   if (isintm1(lead)) *pt_minpol = gneg(*pt_minpol);
    5312         630 :   return ZX_is_irred(*pt_minpol);
    5313             : }
    5314             : 
    5315             : static GEN
    5316         266 : findmaximalsubfield(GEN al, GEN d, long v)
    5317             : {
    5318         266 :   long count, nb=2, i, N = alg_get_absdim(al), n = nf_get_degree(alg_get_center(al));
    5319         266 :   GEN x, minpol, maxc = gen_1;
    5320             : 
    5321         385 :   for (i=n+1; i<=N; i+=n) {
    5322         595 :     for (count=0; count<2 && i+count<=N; count++) {
    5323         476 :       x = col_ei(N,i+count);
    5324         476 :       if (ismaximalsubfield(al, x, d, v, &minpol)) return mkvec2(x,minpol);
    5325             :     }
    5326             :   }
    5327             : 
    5328             :   while(1) {
    5329         154 :     x = zerocol(N);
    5330         609 :     for (count=0; count<nb; count++)
    5331             :     {
    5332         455 :       i = random_Fl(N)+1;
    5333         455 :       gel(x,i) = addiu(randomi(maxc),1);
    5334         455 :       if (random_bits(1)) gel(x,i) = negi(gel(x,i));
    5335             :     }
    5336         154 :     if (ismaximalsubfield(al, x, d, v, &minpol)) return mkvec2(x,minpol);
    5337          70 :     if (!random_bits(3)) maxc = addiu(maxc,1);
    5338          70 :     if (nb<N) nb++;
    5339             :   }
    5340             : 
    5341             :   return NULL; /* LCOV_EXCL_LINE */
    5342             : }
    5343             : 
    5344             : static GEN
    5345         266 : frobeniusform(GEN al, GEN x)
    5346             : {
    5347             :   GEN M, FP, P, Pi;
    5348             : 
    5349             :   /* /!\ has to be the *right* multiplication table */
    5350         266 :   M = algbasisrightmultable(al, x);
    5351             : 
    5352         266 :   FP = matfrobenius(M,2,0); /* M = P^(-1)*F*P */
    5353         266 :   P = gel(FP,2);
    5354         266 :   Pi = RgM_inv(P);
    5355         266 :   return mkvec2(P, Pi);
    5356             : }
    5357             : 
    5358             : static void
    5359         266 : computesplitting(GEN al, long d, long v, long flag)
    5360             : {
    5361         266 :   GEN subf, x, pol, polabs, basis, P, Pi, nf = alg_get_center(al), rnf, Lbasis, Lbasisinv, Q, pows;
    5362         266 :   long i, n = nf_get_degree(nf), nd = n*d, N = alg_get_absdim(al), j, j2;
    5363             : 
    5364         266 :   subf = findmaximalsubfield(al, utoipos(d), v);
    5365         266 :   x = gel(subf, 1);
    5366         266 :   polabs = gel(subf, 2);
    5367             : 
    5368             :   /* Frobenius form to obtain L-vector space structure */
    5369         266 :   basis = frobeniusform(al, x);
    5370         266 :   P = gel(basis, 1);
    5371         266 :   Pi = gel(basis, 2);
    5372             : 
    5373             :   /* construct rnf of splitting field */
    5374         266 :   pol = gel(nffactor(nf,polabs),1);
    5375         308 :   for (i=1; i<lg(pol); i++)
    5376             :     /* select relative factor that vanishes on x */
    5377         308 :     if (gequal0(algpoleval(al, gel(pol,i), x)))
    5378             :     {
    5379         266 :       pol = gel(pol,i);
    5380         266 :       break;
    5381             :     }
    5382         266 :   if (typ(pol) != t_POL) pari_err_BUG("computesplitting (no valid factor)");
    5383         266 :   if (!(flag & al_FACTOR)) pol = mkvec2(pol, stoi(1<<20));
    5384         266 :   gel(al,1) = rnf = rnfinit(nf, pol);
    5385             :   /* since pol is irreducible over Q, we have k=0 in rnf. */
    5386         266 :   if (!gequal0(rnf_get_k(rnf)))
    5387             :     pari_err_BUG("computesplitting (k!=0)"); /*LCOV_EXCL_LINE*/
    5388         266 :   rnf_build_nfabs(rnf, nf_get_prec(nf));
    5389             : 
    5390             :   /* construct splitting data */
    5391         266 :   Lbasis = cgetg(d+1, t_MAT);
    5392         735 :   for (j=j2=1; j<=d; j++, j2+=nd)
    5393         469 :     gel(Lbasis,j) = gel(Pi,j2);
    5394             : 
    5395         266 :   Q = zeromatcopy(d,N);
    5396         266 :   pows = pol_x_powers(nd,v);
    5397         735 :   for (i=j=1; j<=N; j+=nd, i++)
    5398        2156 :   for (j2=0; j2<nd; j2++)
    5399        1687 :     gcoeff(Q,i,j+j2) = mkpolmod(gel(pows,j2+1),polabs);
    5400         266 :   Lbasisinv = RgM_mul(Q,P);
    5401             : 
    5402         266 :   gel(al,3) = mkvec3(x,Lbasis,Lbasisinv);
    5403         266 : }
    5404             : 
    5405             : /* assumes that mt defines a central simple algebra over nf */
    5406             : GEN
    5407         294 : alg_csa_table(GEN nf, GEN mt0, long v, long flag)
    5408             : {
    5409         294 :   pari_sp av = avma;
    5410             :   GEN al, mt;
    5411         294 :   long n, D, d2 = lg(mt0)-1, d = usqrt(d2);
    5412         294 :   dbg_printf(1)("alg_csa_table\n");
    5413             : 
    5414         294 :   mt = check_relmt(nf,mt0);
    5415         280 :   if (!mt) pari_err_TYPE("alg_csa_table", mt0);
    5416         273 :   n = nf_get_degree(nf);
    5417         273 :   D = n*d2;
    5418         273 :   if (d*d != d2)
    5419           7 :     pari_err_DOMAIN("alg_csa_table","(nonsquare) dimension","!=",stoi(d*d),mt);
    5420             : 
    5421         266 :   al = cgetg(12, t_VEC);
    5422         266 :   gel(al,10) = gen_0; /* must be set first */
    5423         266 :   gel(al,1) = zerovec(12); gmael(al,1,10) = nf;
    5424         266 :   gmael(al,1,1) = gpowgs(pol_x(0), d); /* placeholder before splitting field */
    5425         266 :   gel(al,2) = mt;
    5426         266 :   gel(al,3) = gen_0; /* placeholder */
    5427         266 :   gel(al,4) = gel(al,5) = gen_0; /* TODO Hasse invariants if flag&al_FACTOR */
    5428         266 :   gel(al,6) = mkvec2(gen_0,gen_0);
    5429         266 :   gel(al,7) = matid(D);
    5430         266 :   gel(al,8) = matid(D);
    5431         266 :   gel(al,9) = algnatmultable(al,D);
    5432         266 :   gel(al,11)= algtracebasis(al);
    5433         266 :   if (flag & al_MAXORD) al = alg_maximal(al);
    5434         266 :   if (!(flag & al_NOSPLITTING)) computesplitting(al, d, v, flag);
    5435         266 :   return gc_GEN(av, al);
    5436             : }
    5437             : 
    5438             : static GEN
    5439       59805 : algtableinit_i(GEN mt0, GEN p)
    5440             : {
    5441             :   GEN al, mt;
    5442             :   long i, n;
    5443             : 
    5444       59805 :   if (p && !signe(p)) p = NULL;
    5445       59805 :   mt = check_mt(mt0,p);
    5446       59805 :   if (!mt) pari_err_TYPE("algtableinit", mt0);
    5447       59798 :   if (!p && !isint1(Q_denom(mt0)))
    5448           7 :     pari_err_DOMAIN("algtableinit", "denominator(mt)", "!=", gen_1, mt0);
    5449       59791 :   n = lg(mt)-1;
    5450       59791 :   al = cgetg(12, t_VEC);
    5451      358746 :   for (i=1; i<=5; i++) gel(al,i) = gen_0;
    5452       59791 :   gel(al,6) = mkvec2(gen_0, gen_0);
    5453       59791 :   gel(al,7) = matid(n);
    5454       59791 :   gel(al,8) = matid(n);
    5455       59791 :   gel(al,9) = mt;
    5456       59791 :   gel(al,10) = p? p: gen_0;
    5457       59791 :   gel(al,11) = algtracebasis(al);
    5458       59791 :   return al;
    5459             : }
    5460             : GEN
    5461        6223 : algtableinit(GEN mt0, GEN p)
    5462             : {
    5463        6223 :   pari_sp av = avma;
    5464        6223 :   if (p)
    5465             :   {
    5466        6041 :     if (typ(p) != t_INT) pari_err_TYPE("algtableinit",p);
    5467        6034 :     if (signe(p) && !BPSW_psp(p)) pari_err_PRIME("algtableinit",p);
    5468             :   }
    5469        6202 :   return gc_GEN(av, algtableinit_i(mt0, p));
    5470             : }
    5471             : 
    5472             : /** REPRESENTATIONS OF GROUPS **/
    5473             : 
    5474             : static GEN
    5475         294 : list_to_regular_rep(GEN elts, long n)
    5476             : {
    5477             :   GEN reg, elts2, g;
    5478             :   long i,j;
    5479         294 :   elts = shallowcopy(elts);
    5480         294 :   gen_sort_inplace(elts, (void*)&vecsmall_lexcmp, &cmp_nodata, NULL);
    5481         294 :   reg = cgetg(n+1, t_VEC);
    5482         294 :   gel(reg,1) = identity_perm(n);
    5483        3857 :   for (i=2; i<=n; i++) {
    5484        3563 :     g = perm_inv(gel(elts,i));
    5485        3563 :     elts2 = cgetg(n+1, t_VEC);
    5486       74543 :     for (j=1; j<=n; j++) gel(elts2,j) = perm_mul(g,gel(elts,j));
    5487        3563 :     gen_sort_inplace(elts2, (void*)&vecsmall_lexcmp, &cmp_nodata, &gel(reg,i));
    5488             :   }
    5489         294 :   return reg;
    5490             : }
    5491             : 
    5492             : static GEN
    5493        3857 : matrix_perm(GEN perm, long n)
    5494             : {
    5495             :   GEN m;
    5496             :   long j;
    5497        3857 :   m = cgetg(n+1, t_MAT);
    5498       78694 :   for (j=1; j<=n; j++) {
    5499       74837 :     gel(m,j) = col_ei(n,perm[j]);
    5500             :   }
    5501        3857 :   return m;
    5502             : }
    5503             : 
    5504             : GEN
    5505         847 : conjclasses_algcenter(GEN cc, GEN p)
    5506             : {
    5507         847 :   GEN mt, elts = gel(cc,1), conjclass = gel(cc,2), rep = gel(cc,3), card;
    5508         847 :   long i, nbcl = lg(rep)-1, n = lg(elts)-1;
    5509             :   pari_sp av;
    5510             : 
    5511         847 :   card = zero_Flv(nbcl);
    5512       14819 :   for (i=1; i<=n; i++) card[conjclass[i]]++;
    5513             : 
    5514             :   /* multiplication table of the center of Z[G] (class functions) */
    5515         847 :   mt = cgetg(nbcl+1,t_VEC);
    5516        7217 :   for (i=1;i<=nbcl;i++) gel(mt,i) = zero_Flm_copy(nbcl,nbcl);
    5517         847 :   av = avma;
    5518        7217 :   for (i=1;i<=nbcl;i++)
    5519             :   {
    5520        6370 :     GEN xi = gel(elts,rep[i]), mi = gel(mt,i);
    5521             :     long j,k;
    5522      132244 :     for (j=1;j<=n;j++)
    5523             :     {
    5524      125874 :       GEN xj = gel(elts,j);
    5525      125874 :       k = vecsearch(elts, perm_mul(xi,xj), NULL);
    5526      125874 :       ucoeff(mi, conjclass[k], conjclass[j])++;
    5527             :     }
    5528       70238 :     for (k=1; k<=nbcl; k++)
    5529      852362 :       for (j=1; j<=nbcl; j++)
    5530             :       {
    5531      788494 :         ucoeff(mi,k,j) *= card[i];
    5532      788494 :         ucoeff(mi,k,j) /= card[k];
    5533             :       }
    5534        6370 :     set_avma(av);
    5535             :   }
    5536        7217 :   for (i=1;i<=nbcl;i++) gel(mt,i) = Flm_to_ZM(gel(mt,i));
    5537         847 :   return algtableinit_i(mt,p);
    5538             : }
    5539             : 
    5540             : GEN
    5541         329 : alggroupcenter(GEN G, GEN p, GEN *pcc)
    5542             : {
    5543         329 :   pari_sp av = avma;
    5544         329 :   GEN cc = group_to_cc(G), al = conjclasses_algcenter(cc, p);
    5545         315 :   if (!pcc) return gc_GEN(av,al);
    5546           7 :   *pcc = cc; return gc_all(av, 2, &al, pcc);
    5547             : }
    5548             : 
    5549             : static GEN
    5550         294 : groupelts_algebra(GEN elts, GEN p)
    5551             : {
    5552         294 :   pari_sp av = avma;
    5553             :   GEN mt;
    5554         294 :   long i, n = lg(elts)-1;
    5555         294 :   elts = list_to_regular_rep(elts,n);
    5556         294 :   mt = cgetg(n+1, t_VEC);
    5557        4151 :   for (i=1; i<=n; i++) gel(mt,i) = matrix_perm(gel(elts,i),n);
    5558         294 :   return gc_GEN(av, algtableinit_i(mt,p));
    5559             : }
    5560             : 
    5561             : GEN
    5562         329 : alggroup(GEN gal, GEN p)
    5563             : {
    5564         329 :   GEN elts = checkgroupelts(gal);
    5565         294 :   return groupelts_algebra(elts, p);
    5566             : }
    5567             : 
    5568             : /** MAXIMAL ORDER **/
    5569             : 
    5570             : static GEN
    5571       93201 : mattocol(GEN M, long n)
    5572             : {
    5573       93201 :   GEN C = cgetg(n*n+1, t_COL);
    5574             :   long i,j,ic;
    5575       93201 :   ic = 1;
    5576     1650060 :   for (i=1; i<=n; i++)
    5577    40690752 :   for (j=1; j<=n; j++, ic++) gel(C,ic) = gcoeff(M,i,j);
    5578       93201 :   return C;
    5579             : }
    5580             : 
    5581             : /* Ip is a lift of a left O/pO-ideal where O is the integral basis of al */
    5582             : static GEN
    5583        8604 : algleftordermodp(GEN al, GEN Ip, GEN p)
    5584             : {
    5585        8604 :   pari_sp av = avma;
    5586             :   GEN I, Ii, M, mt, K, imi, p2;
    5587             :   long n, i;
    5588        8604 :   n = alg_get_absdim(al);
    5589        8604 :   mt = alg_get_multable(al);
    5590        8604 :   p2 = sqri(p);
    5591             : 
    5592        8604 :   I = ZM_hnfmodid(Ip, p);
    5593        8604 :   Ii = ZM_inv(I,NULL);
    5594             : 
    5595        8604 :   M = cgetg(n+1, t_MAT);
    5596      101805 :   for (i=1; i<=n; i++) {
    5597       93201 :     imi = FpM_mul(Ii, FpM_mul(gel(mt,i), I, p2), p2);
    5598       93201 :     imi = ZM_Z_divexact(imi, p);
    5599       93201 :     gel(M,i) = mattocol(imi, n);
    5600             :   }
    5601        8604 :   K = FpM_ker(M, p);
    5602        8604 :   if (lg(K)==1) { set_avma(av); return matid(n); }
    5603        3501 :   K = ZM_hnfmodid(K,p);
    5604             : 
    5605        3501 :   return gc_upto(av, ZM_Z_div(K,p));
    5606             : }
    5607             : 
    5608             : static GEN
    5609       14406 : alg_ordermodp(GEN al, GEN p)
    5610             : {
    5611             :   GEN alp;
    5612       14406 :   long i, N = alg_get_absdim(al);
    5613       14406 :   alp = cgetg(12, t_VEC);
    5614      129654 :   for (i=1; i<=8; i++) gel(alp,i) = gen_0;
    5615       14406 :   gel(alp,9) = cgetg(N+1, t_VEC);
    5616      151931 :   for (i=1; i<=N; i++) gmael(alp,9,i) = FpM_red(gmael(al,9,i), p);
    5617       14406 :   gel(alp,10) = p;
    5618       14406 :   gel(alp,11) = cgetg(N+1, t_VEC);
    5619      151931 :   for (i=1; i<=N; i++) gmael(alp,11,i) = Fp_red(gmael(al,11,i), p);
    5620             : 
    5621       14406 :   return alp;
    5622             : }
    5623             : 
    5624             : static GEN
    5625        6772 : algpradical_i(GEN al, GEN p, GEN zprad, GEN projs)
    5626             : {
    5627        6772 :   pari_sp av = avma;
    5628        6772 :   GEN alp = alg_ordermodp(al, p), liftrad, projrad, alq, alrad, res, Lalp, radq;
    5629             :   long i;
    5630        6772 :   if (lg(zprad)==1) {
    5631        4704 :     liftrad = NULL;
    5632        4704 :     projrad = NULL;
    5633             :   }
    5634             :   else {
    5635        2068 :     alq = alg_quotient(alp, zprad, 1);
    5636        2068 :     alp = gel(alq,1);
    5637        2068 :     projrad = gel(alq,2);
    5638        2068 :     liftrad = gel(alq,3);
    5639             :   }
    5640             : 
    5641        6772 :   if (projs) {
    5642        1333 :     if (projrad) {
    5643          28 :       projs = gcopy(projs);
    5644          84 :       for (i=1; i<lg(projs); i++)
    5645          56 :         gel(projs,i) = FpM_FpC_mul(projrad, gel(projs,i), p);
    5646             :     }
    5647        1333 :     Lalp = alg_centralproj(alp, projs, 1);
    5648             : 
    5649        1333 :     alrad = cgetg(lg(Lalp),t_VEC);
    5650        4389 :     for (i=1; i<lg(Lalp); i++) {
    5651        3056 :       alq = gel(Lalp,i);
    5652        3056 :       radq = algradical(gel(alq,1));
    5653        3056 :       if (gequal0(radq))
    5654        1744 :         gel(alrad,i) = cgetg(1,t_MAT);
    5655             :       else {
    5656        1312 :         radq = FpM_mul(gel(alq,3),radq,p);
    5657        1312 :         gel(alrad,i) = radq;
    5658             :       }
    5659             :     }
    5660        1333 :     alrad = shallowmatconcat(alrad);
    5661        1333 :     alrad = FpM_image(alrad,p);
    5662             :   }
    5663        5439 :   else alrad = algradical(alp);
    5664             : 
    5665        6772 :   if (!gequal0(alrad)) {
    5666        5377 :     if (liftrad) alrad = FpM_mul(liftrad, alrad, p);
    5667        5377 :     res = shallowmatconcat(mkvec2(alrad, zprad));
    5668        5377 :     res = FpM_image(res,p);
    5669             :   }
    5670        1395 :   else res = lg(zprad)==1 ? gen_0 : zprad;
    5671        6772 :   return gc_GEN(av, res);
    5672             : }
    5673             : 
    5674             : static GEN
    5675        4876 : algpdecompose0(GEN al, GEN prad, GEN p, GEN projs)
    5676             : {
    5677        4876 :   pari_sp av = avma;
    5678        4876 :   GEN alp, quo, ss, liftm = NULL, projm = NULL, dec, res, I, Lss, deci;
    5679             :   long i, j;
    5680             : 
    5681        4876 :   alp = alg_ordermodp(al, p);
    5682        4876 :   if (!gequal0(prad)) {
    5683        3951 :     quo = alg_quotient(alp, prad, 1);
    5684        3951 :     ss = gel(quo,1);
    5685        3951 :     projm = gel(quo,2);
    5686        3951 :     liftm = gel(quo,3);
    5687             :   }
    5688         925 :   else ss = alp;
    5689             : 
    5690        4876 :   if (projs) {
    5691        1171 :     if (projm) {
    5692        2676 :       for (i=1; i<lg(projs); i++)
    5693        1856 :         gel(projs,i) = FpM_FpC_mul(projm, gel(projs,i), p);
    5694             :     }
    5695        1171 :     Lss = alg_centralproj(ss, projs, 1);
    5696             : 
    5697        1171 :     dec = cgetg(lg(Lss),t_VEC);
    5698        3882 :     for (i=1; i<lg(Lss); i++) {
    5699        2711 :       gel(dec,i) = algsimpledec_ss(gmael(Lss,i,1), 1);
    5700        2711 :       deci = gel(dec,i);
    5701        6144 :       for (j=1; j<lg(deci); j++)
    5702        3433 :        gmael(deci,j,3) = FpM_mul(gmael(Lss,i,3), gmael(deci,j,3), p);
    5703             :     }
    5704        1171 :     dec = shallowconcat1(dec);
    5705             :   }
    5706        3705 :   else dec = algsimpledec_ss(ss,1);
    5707             : 
    5708        4876 :   res = cgetg(lg(dec),t_VEC);
    5709       13373 :   for (i=1; i<lg(dec); i++) {
    5710        8497 :     I = gmael(dec,i,3);
    5711        8497 :     if (liftm) I = FpM_mul(liftm,I,p);
    5712        8497 :     I = shallowmatconcat(mkvec2(I,prad));
    5713        8497 :     gel(res,i) = I;
    5714             :   }
    5715             : 
    5716        4876 :   return gc_GEN(av, res);
    5717             : }
    5718             : 
    5719             : /* finds a nontrivial ideal of O/prad or gen_0 if there is none. */
    5720             : static GEN
    5721        1605 : algpdecompose_i(GEN al, GEN p, GEN zprad, GEN projs)
    5722             : {
    5723        1605 :   pari_sp av = avma;
    5724        1605 :   GEN prad = algpradical_i(al,p,zprad,projs);
    5725        1605 :   return gc_upto(av, algpdecompose0(al, prad, p, projs));
    5726             : }
    5727             : 
    5728             : /* ord is assumed to be in hnf wrt the integral basis of al. */
    5729             : /* assumes that alg_get_invbasis(al) is integral. */
    5730             : static GEN
    5731        3529 : alg_change_overorder_shallow(GEN al, GEN ord)
    5732             : {
    5733             :   GEN al2, mt, iord, mtx, den, den2, div, invol;
    5734             :   long i, n;
    5735        3529 :   n = alg_get_absdim(al);
    5736             : 
    5737        3529 :   iord = QM_inv(ord);
    5738        3529 :   al2 = shallowcopy(al);
    5739             : 
    5740        3529 :   invol = alg_get_invol(al);
    5741        3529 :   if (typ(invol) == t_MAT) gmael(al2,6,2) = QM_mul(iord, QM_mul(invol,ord));
    5742             : 
    5743        3529 :   ord = Q_remove_denom(ord,&den);
    5744             : 
    5745        3529 :   gel(al2,7) = Q_remove_denom(gel(al,7), &den2);
    5746        3529 :   if (den2) div = mulii(den,den2);
    5747        1253 :   else      div = den;
    5748        3529 :   gel(al2,7) = ZM_Z_div(ZM_mul(gel(al2,7), ord), div);
    5749             : 
    5750        3529 :   gel(al2,8) = ZM_mul(iord, gel(al,8));
    5751             : 
    5752        3529 :   mt = cgetg(n+1,t_VEC);
    5753        3529 :   gel(mt,1) = matid(n);
    5754        3529 :   div = sqri(den);
    5755       39207 :   for (i=2; i<=n; i++) {
    5756       35678 :     mtx = algbasismultable(al,gel(ord,i));
    5757       35678 :     gel(mt,i) = ZM_mul(iord, ZM_mul(mtx, ord));
    5758       35678 :     gel(mt,i) = ZM_Z_divexact(gel(mt,i), div);
    5759             :   }
    5760        3529 :   gel(al2,9) = mt;
    5761             : 
    5762        3529 :   gel(al2,11) = algtracebasis(al2);
    5763             : 
    5764        3529 :   return al2;
    5765             : }
    5766             : 
    5767             : static GEN
    5768       36345 : algeltfromnf_i(GEN al, GEN x)
    5769             : {
    5770       36345 :   GEN nf = alg_get_center(al);
    5771             :   long n;
    5772       36345 :   switch(alg_type(al)) {
    5773       30647 :     case al_CYCLIC:
    5774       30647 :       n = alg_get_degree(al);
    5775       30647 :       break;
    5776        5698 :     case al_CSA:
    5777        5698 :       n = alg_get_dim(al);
    5778        5698 :       break;
    5779             :     default: return NULL; /*LCOV_EXCL_LINE*/
    5780             :   }
    5781       36345 :   return algalgtobasis(al, scalarcol(basistoalg(nf, x), n));
    5782             : }
    5783             : 
    5784             : GEN
    5785        5138 : algeltfromnf(GEN al, GEN x)
    5786             : {
    5787        5138 :   pari_sp av = avma;
    5788        5138 :   checkalg(al);
    5789        5131 :   return gc_upto(av, algeltfromnf_i(al,x));
    5790             : }
    5791             : 
    5792             : /* x is an ideal of the center in hnf form */
    5793             : static GEN
    5794        6772 : algeltfromnf_hnf(GEN al, GEN x)
    5795             : {
    5796             :   GEN res;
    5797             :   long i;
    5798        6772 :   res = cgetg(lg(x), t_MAT);
    5799       19327 :   for (i=1; i<lg(x); i++) gel(res,i) = algeltfromnf_i(al, gel(x,i));
    5800        6772 :   return res;
    5801             : }
    5802             : 
    5803             : /* assumes al is CSA or CYCLIC */
    5804             : static GEN
    5805        3271 : algcenter_precompute(GEN al, GEN p)
    5806             : {
    5807        3271 :   GEN fa, pdec, nfprad, projs, nf = alg_get_center(al);
    5808             :   long i, np;
    5809             : 
    5810        3271 :   pdec = idealprimedec(nf, p);
    5811        3271 :   settyp(pdec, t_COL);
    5812        3271 :   np = lg(pdec)-1;
    5813        3271 :   fa = mkmat2(pdec, const_col(np, gen_1));
    5814        3271 :   if (dvdii(nf_get_disc(nf), p))
    5815         666 :     nfprad = idealprodprime(nf, pdec);
    5816             :   else
    5817        2605 :     nfprad = scalarmat_shallow(p, nf_get_degree(nf));
    5818        3271 :   fa = idealchineseinit(nf, fa);
    5819        3271 :   projs = cgetg(np+1, t_VEC);
    5820        7375 :   for (i=1; i<=np; i++) gel(projs, i) = idealchinese(nf, fa, vec_ei(np,i));
    5821        3271 :   return mkvec2(nfprad, projs);
    5822             : }
    5823             : 
    5824             : static GEN
    5825        6772 : algcenter_prad(GEN al, GEN p, GEN pre)
    5826             : {
    5827             :   GEN nfprad, zprad, mtprad;
    5828             :   long i;
    5829        6772 :   nfprad = gel(pre,1);
    5830        6772 :   zprad = algeltfromnf_hnf(al, nfprad);
    5831        6772 :   zprad = FpM_image(zprad, p);
    5832        6772 :   mtprad = cgetg(lg(zprad), t_VEC);
    5833        9177 :   for (i=1; i<lg(zprad); i++) gel(mtprad, i) = algbasismultable(al, gel(zprad,i));
    5834        6772 :   mtprad = shallowmatconcat(mtprad);
    5835        6772 :   zprad = FpM_image(mtprad, p);
    5836        6772 :   return zprad;
    5837             : }
    5838             : 
    5839             : static GEN
    5840        6772 : algcenter_p_projs(GEN al, GEN p, GEN pre)
    5841             : {
    5842             :   GEN projs, zprojs;
    5843             :   long i;
    5844        6772 :   projs = gel(pre,2);
    5845        6772 :   zprojs = cgetg(lg(projs), t_VEC);
    5846       15267 :   for (i=1; i<lg(projs); i++) gel(zprojs,i) = FpC_red(algeltfromnf_i(al, gel(projs,i)),p);
    5847        6772 :   return zprojs;
    5848             : }
    5849             : 
    5850             : /* al is assumed to be simple */
    5851             : static GEN
    5852        3271 : alg_pmaximal(GEN al, GEN p)
    5853             : {
    5854             :   pari_sp av;
    5855        3271 :   long n = alg_get_absdim(al);
    5856        3271 :   GEN id = matid(n), al2 = al, prad, lord = gen_0, dec, zprad, projs, pre;
    5857             : 
    5858        3271 :   dbg_printf(0)("Round 2 (noncommutative) at p=%Ps, dim=%d\n", p, n);
    5859        3271 :   pre = algcenter_precompute(al,p); av = avma;
    5860             :   while (1) {
    5861        5167 :     zprad = algcenter_prad(al2, p, pre);
    5862        5167 :     projs = algcenter_p_projs(al2, p, pre);
    5863        5167 :     if (lg(projs) == 2) projs = NULL;
    5864        5167 :     prad = algpradical_i(al2,p,zprad,projs);
    5865        5167 :     if (typ(prad) == t_INT) break;
    5866        5125 :     lord = algleftordermodp(al2,prad,p);
    5867        5125 :     if (!cmp_universal(lord,id)) break;
    5868        1896 :     al2 = gc_GEN(av, alg_change_overorder_shallow(al2,lord));
    5869             :   }
    5870             : 
    5871        3271 :   dec = algpdecompose0(al2,prad,p,projs); av = avma;
    5872        4876 :   while (lg(dec) > 2) {
    5873             :     long i;
    5874        4145 :     for (i = 1; i < lg(dec); i++) {
    5875        3479 :       GEN I = gel(dec,i);
    5876        3479 :       lord = algleftordermodp(al2,I,p);
    5877        3479 :       if (cmp_universal(lord,id)) break;
    5878             :     }
    5879        2271 :     if (i==lg(dec)) break;
    5880        1605 :     al2 = gc_GEN(av, alg_change_overorder_shallow(al2,lord));
    5881        1605 :     zprad = algcenter_prad(al2, p, pre);
    5882        1605 :     projs = algcenter_p_projs(al2, p, pre);
    5883        1605 :     if (lg(projs) == 2) projs = NULL;
    5884        1605 :     dec = algpdecompose_i(al2,p,zprad,projs);
    5885             :   }
    5886        3271 :   return al2;
    5887             : }
    5888             : 
    5889             : static GEN
    5890       15236 : algtracematrix(GEN al)
    5891             : {
    5892             :   GEN M, mt;
    5893             :   long n, i, j;
    5894       15236 :   n = alg_get_absdim(al);
    5895       15236 :   mt = alg_get_multable(al);
    5896       15236 :   M = cgetg(n+1, t_MAT);
    5897      114068 :   for (i=1; i<=n; i++)
    5898             :   {
    5899       98832 :     gel(M,i) = cgetg(n+1,t_MAT);
    5900      750489 :     for (j=1; j<=i; j++)
    5901      651657 :       gcoeff(M,j,i) = gcoeff(M,i,j) = algabstrace(al,gmael(mt,i,j));
    5902             :   }
    5903       15236 :   return M;
    5904             : }
    5905             : static GEN
    5906         567 : algdisc_i(GEN al) { return ZM_det(algtracematrix(al)); }
    5907             : GEN
    5908         364 : algdisc(GEN al)
    5909             : {
    5910         364 :   pari_sp av = avma;
    5911         364 :   checkalg(al);
    5912         364 :   if (alg_type(al) == al_REAL) pari_err_TYPE("algdisc [real algebra]", al);
    5913         343 :   return gc_INT(av, algdisc_i(al));
    5914             : }
    5915             : static GEN
    5916         224 : alg_maximal(GEN al)
    5917             : {
    5918         224 :   GEN fa = absZ_factor(algdisc_i(al));
    5919         224 :   return alg_maximal_primes(al, gel(fa,1));
    5920             : }
    5921             : 
    5922             : /** LATTICES **/
    5923             : 
    5924             : /*
    5925             :  Convention: lattice = [I,t] representing t*I, where
    5926             :  - I integral nonsingular upper-triangular matrix representing a lattice over
    5927             :    the integral basis of the algebra, and
    5928             :  - t>0 either an integer or a rational number.
    5929             : 
    5930             :  Recommended and returned by the functions below:
    5931             :  - I HNF and primitive
    5932             : */
    5933             : 
    5934             : /* TODO use hnfmodid whenever possible using a*O <= I <= O
    5935             :  * for instance a = ZM_det_triangular(I) */
    5936             : 
    5937             : static GEN
    5938       64351 : primlat(GEN lat)
    5939             : {
    5940             :   GEN m, t, c;
    5941       64351 :   m = alglat_get_primbasis(lat);
    5942       64351 :   t = alglat_get_scalar(lat);
    5943       64351 :   m = Q_primitive_part(m,&c);
    5944       64351 :   if (c) return mkvec2(m,gmul(t,c));
    5945       54817 :   return lat;
    5946             : }
    5947             : 
    5948             : /* assumes the lattice contains d * integral basis, d=0 allowed */
    5949             : GEN
    5950       53487 : alglathnf(GEN al, GEN m, GEN d)
    5951             : {
    5952       53487 :   pari_sp av = avma;
    5953             :   long N,i,j;
    5954             :   GEN m2, c;
    5955       53487 :   if (!d) d = gen_0;
    5956       53487 :   checkalg(al);
    5957       53487 :   if (alg_type(al) == al_REAL) pari_err_TYPE("alglathnf [real algebra]", al);
    5958       53480 :   N = alg_get_absdim(al);
    5959       53480 :   if (!d) d = gen_0;
    5960       53480 :   if (typ(m) == t_VEC) m = matconcat(m);
    5961       53480 :   if (typ(m) == t_COL) m = algleftmultable(al,m);
    5962       53480 :   if (typ(m) != t_MAT) pari_err_TYPE("alglathnf",m);
    5963       53473 :   if (typ(d) != t_FRAC && typ(d) != t_INT) pari_err_TYPE("alglathnf",d);
    5964       53473 :   if (lg(m)-1 < N || lg(gel(m,1))-1 != N) pari_err_DIM("alglathnf");
    5965      480935 :   for (i=1; i<=N; i++)
    5966     7185192 :     for (j=1; j<lg(m); j++)
    5967     6757702 :       if (typ(gcoeff(m,i,j)) != t_FRAC && typ(gcoeff(m,i,j)) != t_INT)
    5968           7 :         pari_err_TYPE("alglathnf", gcoeff(m,i,j));
    5969       53438 :   m2 = Q_primitive_part(m,&c);
    5970       53438 :   if (!c) c = gen_1;
    5971       53438 :   if (!signe(d)) d = detint(m2);
    5972       45593 :   else           d = gdiv(d,c); /* should be an integer */
    5973       53438 :   if (!signe(d)) pari_err_INV("alglathnf [m does not have full rank]", m2);
    5974       53424 :   m2 = ZM_hnfmodid(m2,d);
    5975       53424 :   return gc_GEN(av, mkvec2(m2,c));
    5976             : }
    5977             : 
    5978             : static GEN
    5979       11683 : prepare_multipliers(GEN *a, GEN *b)
    5980             : {
    5981             :   GEN na, nb, da, db, d;
    5982       11683 :   na = numer_i(*a); da = denom_i(*a);
    5983       11683 :   nb = numer_i(*b); db = denom_i(*b);
    5984       11683 :   na = mulii(na,db);
    5985       11683 :   nb = mulii(nb,da);
    5986       11683 :   d = gcdii(na,nb);
    5987       11683 :   *a = diviiexact(na,d);
    5988       11683 :   *b = diviiexact(nb,d);
    5989       11683 :   return gdiv(d, mulii(da,db));
    5990             : }
    5991             : 
    5992             : static GEN
    5993       11683 : prepare_lat(GEN m1, GEN t1, GEN m2, GEN t2)
    5994             : {
    5995       11683 :   GEN d = prepare_multipliers(&t1, &t2);
    5996       11683 :   m1 = ZM_Z_mul(m1,t1);
    5997       11683 :   m2 = ZM_Z_mul(m2,t2);
    5998       11683 :   return mkvec3(m1,m2,d);
    5999             : }
    6000             : 
    6001             : static GEN
    6002       11697 : alglataddinter(GEN al, GEN lat1, GEN lat2, GEN *sum, GEN *inter)
    6003             : {
    6004             :   GEN d, m1, m2, t1, t2, M, prep, d1, d2, ds, di, K;
    6005       11697 :   checkalg(al);
    6006       11697 :   if (alg_type(al) == al_REAL)
    6007          14 :     pari_err_TYPE("alglataddinter [real algebra]", al);
    6008       11683 :   checklat(al,lat1);
    6009       11683 :   checklat(al,lat2);
    6010             : 
    6011       11683 :   m1 = alglat_get_primbasis(lat1);
    6012       11683 :   t1 = alglat_get_scalar(lat1);
    6013       11683 :   m2 = alglat_get_primbasis(lat2);
    6014       11683 :   t2 = alglat_get_scalar(lat2);
    6015       11683 :   prep = prepare_lat(m1, t1, m2, t2);
    6016       11683 :   m1 = gel(prep,1);
    6017       11683 :   m2 = gel(prep,2);
    6018       11683 :   d = gel(prep,3);
    6019       11683 :   M = matconcat(mkvec2(m1,m2));
    6020       11683 :   d1 = ZM_det_triangular(m1);
    6021       11683 :   d2 = ZM_det_triangular(m2);
    6022       11683 :   ds = gcdii(d1,d2);
    6023       11683 :   if (inter)
    6024             :   {
    6025        7616 :     di = diviiexact(mulii(d1,d2),ds);
    6026        7616 :     if (equali1(di))
    6027             :     {
    6028         140 :       *inter = matid(lg(m1)-1);
    6029         140 :       if (sum) *sum = matid(lg(m1)-1);
    6030             :     }
    6031             :     else
    6032             :     {
    6033        7476 :       K = matkermod(M,di,sum);
    6034        7476 :       K = rowslice(K,1,lg(m1));
    6035        7476 :       *inter = hnfmodid(FpM_mul(m1,K,di),di);
    6036        7476 :       if (sum) *sum = hnfmodid(*sum,ds);
    6037             :     }
    6038             :   }
    6039        4067 :   else *sum = hnfmodid(M,ds);
    6040       11683 :   return d;
    6041             : }
    6042             : 
    6043             : GEN
    6044        4109 : alglatinter(GEN al, GEN lat1, GEN lat2, GEN* psum)
    6045             : {
    6046        4109 :   pari_sp av = avma;
    6047             :   GEN inter, d;
    6048        4109 :   d = alglataddinter(al, lat1, lat2, psum, &inter);
    6049        4102 :   inter = primlat(mkvec2(inter, d));
    6050        4102 :   if (!psum) return gc_GEN(av, inter);
    6051          28 :   *psum = primlat(mkvec2(*psum,d));
    6052          28 :   return gc_all(av, 2, &inter, psum);
    6053             : }
    6054             : 
    6055             : GEN
    6056        7588 : alglatadd(GEN al, GEN lat1, GEN lat2, GEN* pinter)
    6057             : {
    6058        7588 :   pari_sp av = avma;
    6059             :   GEN sum, d;
    6060        7588 :   d = alglataddinter(al, lat1, lat2, &sum, pinter);
    6061        7581 :   sum = primlat(mkvec2(sum, d));
    6062        7581 :   if (!pinter) return gc_GEN(av, sum);
    6063        3514 :   *pinter = primlat(mkvec2(*pinter,d));
    6064        3514 :   return gc_all(av, 2, &sum, pinter);
    6065             : }
    6066             : 
    6067             : /* TODO version that returns the quotient as abelian group? */
    6068             : /* return matrices to convert coordinates from one to other? */
    6069             : int
    6070       33495 : alglatsubset(GEN al, GEN lat1, GEN lat2, GEN* pindex)
    6071             : {
    6072       33495 :   pari_sp av = avma;
    6073             :   int res;
    6074             :   GEN m1, m2, m2i, m, t;
    6075       33495 :   checkalg(al);
    6076       33495 :   if (alg_type(al) == al_REAL) pari_err_TYPE("alglatsubset [real algebra]", al);
    6077       33488 :   checklat(al,lat1);
    6078       33488 :   checklat(al,lat2);
    6079       33488 :   m1 = alglat_get_primbasis(lat1);
    6080       33488 :   m2 = alglat_get_primbasis(lat2);
    6081       33488 :   m2i = RgM_inv_upper(m2);
    6082       33488 :   t = gdiv(alglat_get_scalar(lat1), alglat_get_scalar(lat2));
    6083       33488 :   m = RgM_Rg_mul(RgM_mul(m2i,m1), t);
    6084       33488 :   res = RgM_is_ZM(m);
    6085       33488 :   if (!res || !pindex) return gc_int(av, res);
    6086        1757 :   *pindex = gc_INT(av, mpabs(ZM_det_triangular(m)));
    6087        1757 :   return 1;
    6088             : }
    6089             : 
    6090             : GEN
    6091        5271 : alglatindex(GEN al, GEN lat1, GEN lat2)
    6092             : {
    6093        5271 :   pari_sp av = avma;
    6094             :   long N;
    6095             :   GEN res;
    6096        5271 :   checkalg(al);
    6097        5271 :   if (alg_type(al) == al_REAL) pari_err_TYPE("alglatindex [real algebra]", al);
    6098        5264 :   checklat(al,lat1);
    6099        5264 :   checklat(al,lat2);
    6100        5264 :   N = alg_get_absdim(al);
    6101        5264 :   res = alglat_get_scalar(lat1);
    6102        5264 :   res = gdiv(res, alglat_get_scalar(lat2));
    6103        5264 :   res = gpowgs(res, N);
    6104        5264 :   res = gmul(res,RgM_det_triangular(alglat_get_primbasis(lat1)));
    6105        5264 :   res = gdiv(res, RgM_det_triangular(alglat_get_primbasis(lat2)));
    6106        5264 :   res = gabs(res,0);
    6107        5264 :   return gc_GEN(av, res);
    6108             : }
    6109             : 
    6110             : GEN
    6111       45612 : alglatmul(GEN al, GEN lat1, GEN lat2)
    6112             : {
    6113       45612 :   pari_sp av = avma;
    6114             :   long N,i;
    6115             :   GEN m1, m2, m, V, lat, t, d, dp;
    6116       45612 :   checkalg(al);
    6117       45612 :   if (alg_type(al) == al_REAL) pari_err_TYPE("alglatmul [real algebra]", al);
    6118       45605 :   if (typ(lat1)==t_COL)
    6119             :   {
    6120       19292 :     if (typ(lat2)==t_COL)
    6121           7 :       pari_err_TYPE("alglatmul [one of lat1, lat2 has to be a lattice]", lat2);
    6122       19285 :     checklat(al,lat2);
    6123       19285 :     lat1 = Q_remove_denom(lat1,&d);
    6124       19285 :     m = algbasismultable(al,lat1);
    6125       19285 :     m2 = alglat_get_primbasis(lat2);
    6126       19285 :     dp = mulii(detint(m),ZM_det_triangular(m2));
    6127       19285 :     m = ZM_mul(m,m2);
    6128       19285 :     t = alglat_get_scalar(lat2);
    6129       19285 :     if (d) t = gdiv(t,d);
    6130             :   }
    6131             :   else /* typ(lat1)!=t_COL */
    6132             :   {
    6133       26313 :     checklat(al,lat1);
    6134       26313 :     if (typ(lat2)==t_COL)
    6135             :     {
    6136       19285 :       lat2 = Q_remove_denom(lat2,&d);
    6137       19285 :       m = algbasisrightmultable(al,lat2);
    6138       19285 :       m1 = alglat_get_primbasis(lat1);
    6139       19285 :       dp = mulii(detint(m),ZM_det_triangular(m1));
    6140       19285 :       m = ZM_mul(m,m1);
    6141       19285 :       t = alglat_get_scalar(lat1);
    6142       19285 :       if (d) t = gdiv(t,d);
    6143             :     }
    6144             :     else /* typ(lat2)!=t_COL */
    6145             :     {
    6146        7028 :       checklat(al,lat2);
    6147        7021 :       N = alg_get_absdim(al);
    6148        7021 :       m1 = alglat_get_primbasis(lat1);
    6149        7021 :       m2 = alglat_get_primbasis(lat2);
    6150        7021 :       dp = mulii(ZM_det_triangular(m1), ZM_det_triangular(m2));
    6151        7021 :       V = cgetg(N+1,t_VEC);
    6152       63189 :       for (i=1; i<=N; i++) {
    6153       56168 :         gel(V,i) = algbasismultable(al,gel(m1,i));
    6154       56168 :         gel(V,i) = ZM_mul(gel(V,i),m2);
    6155             :       }
    6156        7021 :       m = matconcat(V);
    6157        7021 :       t = gmul(alglat_get_scalar(lat1), alglat_get_scalar(lat2));
    6158             :     }
    6159             :   }
    6160             : 
    6161       45591 :   lat = alglathnf(al,m,dp);
    6162       45591 :   gel(lat,2) = gmul(alglat_get_scalar(lat), t);
    6163       45591 :   lat = primlat(lat);
    6164       45591 :   return gc_GEN(av, lat);
    6165             : }
    6166             : 
    6167             : int
    6168       17528 : alglatcontains(GEN al, GEN lat, GEN x, GEN *ptc)
    6169             : {
    6170       17528 :   pari_sp av = avma;
    6171             :   GEN m, t, sol;
    6172       17528 :   checkalg(al);
    6173       17528 :   if (alg_type(al) == al_REAL)
    6174           7 :     pari_err_TYPE("alglatcontains [real algebra]", al);
    6175       17521 :   checklat(al,lat);
    6176       17521 :   m = alglat_get_primbasis(lat);
    6177       17521 :   t = alglat_get_scalar(lat);
    6178       17521 :   x = RgC_Rg_div(x,t);
    6179       17521 :   if (!RgV_is_ZV(x)) return gc_bool(av,0);
    6180       17521 :   sol = hnf_solve(m,x);
    6181       17521 :   if (!sol) return gc_bool(av,0);
    6182        8771 :   if (!ptc) return gc_bool(av,1);
    6183        8764 :   *ptc = gc_GEN(av, sol); return 1;
    6184             : }
    6185             : 
    6186             : GEN
    6187        8778 : alglatelement(GEN al, GEN lat, GEN c)
    6188             : {
    6189        8778 :   pari_sp av = avma;
    6190             :   GEN res;
    6191        8778 :   checkalg(al);
    6192        8778 :   if (alg_type(al) == al_REAL)
    6193           7 :     pari_err_TYPE("alglatelement [real algebra]", al);
    6194        8771 :   checklat(al,lat);
    6195        8771 :   if (typ(c)!=t_COL) pari_err_TYPE("alglatelement", c);
    6196        8764 :   res = ZM_ZC_mul(alglat_get_primbasis(lat),c);
    6197        8764 :   res = RgC_Rg_mul(res, alglat_get_scalar(lat));
    6198        8764 :   return gc_GEN(av,res);
    6199             : }
    6200             : 
    6201             : /* idem QM_invimZ, knowing result is contained in 1/c*Z^n */
    6202             : static GEN
    6203        3535 : QM_invimZ_mod(GEN m, GEN c)
    6204             : {
    6205             :   GEN d, m0, K;
    6206        3535 :   m0 = Q_remove_denom(m, &d);
    6207        3535 :   if (d)    d = mulii(d,c);
    6208          35 :   else      d = c;
    6209        3535 :   K = matkermod(m0, d, NULL);
    6210        3535 :   if (lg(K)==1) K = scalarmat(d, lg(m)-1);
    6211        3493 :   else          K = hnfmodid(K, d);
    6212        3535 :   return RgM_Rg_div(K,c);
    6213             : }
    6214             : 
    6215             : /* If m is injective, computes a Z-basis of the submodule of elements whose
    6216             :  * image under m is integral */
    6217             : static GEN
    6218          14 : QM_invimZ(GEN m)
    6219             : {
    6220          14 :   return RgM_invimage(m, QM_ImQ_hnf(m));
    6221             : }
    6222             : 
    6223             : /* An isomorphism of R-modules M_{m,n}(R) -> R^{m*n} */
    6224             : static GEN
    6225       28322 : mat2col(GEN M, long m, long n)
    6226             : {
    6227             :   long i,j,k,p;
    6228             :   GEN C;
    6229       28322 :   p = m*n;
    6230       28322 :   C = cgetg(p+1,t_COL);
    6231      254702 :   for (i=1,k=1;i<=m;i++)
    6232     2036804 :     for (j=1;j<=n;j++,k++)
    6233     1810424 :       gel(C,k) = gcoeff(M,i,j);
    6234       28322 :   return C;
    6235             : }
    6236             : 
    6237             : static GEN
    6238        3535 : alglattransporter_i(GEN al, GEN lat1, GEN lat2, long right)
    6239             : {
    6240             :   GEN m1, m2, m2i, M, MT, mt, t1, t2, T, c;
    6241             :   long N, i;
    6242        3535 :   N = alg_get_absdim(al);
    6243        3535 :   m1 = alglat_get_primbasis(lat1);
    6244        3535 :   m2 = alglat_get_primbasis(lat2);
    6245        3535 :   m2i = RgM_inv_upper(m2);
    6246        3535 :   c = detint(m1);
    6247        3535 :   t1 = alglat_get_scalar(lat1);
    6248        3535 :   m1 = RgM_Rg_mul(m1,t1);
    6249        3535 :   t2 = alglat_get_scalar(lat2);
    6250        3535 :   m2i = RgM_Rg_div(m2i,t2);
    6251             : 
    6252        3535 :   MT = right? NULL: alg_get_multable(al);
    6253        3535 :   M = cgetg(N+1, t_MAT);
    6254       31815 :   for (i=1; i<=N; i++) {
    6255       28280 :     if (right) mt = algbasisrightmultable(al, vec_ei(N,i));
    6256       14168 :     else       mt = gel(MT,i);
    6257       28280 :     mt = RgM_mul(m2i,mt);
    6258       28280 :     mt = RgM_mul(mt,m1);
    6259       28280 :     gel(M,i) = mat2col(mt, N, N);
    6260             :   }
    6261             : 
    6262        3535 :   c = gdiv(t2,gmul(c,t1));
    6263        3535 :   c = denom_i(c);
    6264        3535 :   T = QM_invimZ_mod(M,c);
    6265        3535 :   return primlat(mkvec2(T,gen_1));
    6266             : }
    6267             : 
    6268             : /*
    6269             :    { x in al | x*lat1 subset lat2}
    6270             : */
    6271             : GEN
    6272        1778 : alglatlefttransporter(GEN al, GEN lat1, GEN lat2)
    6273             : {
    6274        1778 :   pari_sp av = avma;
    6275        1778 :   checkalg(al);
    6276        1778 :   if (alg_type(al) == al_REAL)
    6277           7 :     pari_err_TYPE("alglatlefttransporter [real algebra]", al);
    6278        1771 :   checklat(al,lat1);
    6279        1771 :   checklat(al,lat2);
    6280        1771 :   return gc_GEN(av, alglattransporter_i(al,lat1,lat2,0));
    6281             : }
    6282             : 
    6283             : /*
    6284             :    { x in al | lat1*x subset lat2}
    6285             : */
    6286             : GEN
    6287        1771 : alglatrighttransporter(GEN al, GEN lat1, GEN lat2)
    6288             : {
    6289        1771 :   pari_sp av = avma;
    6290        1771 :   checkalg(al);
    6291        1771 :   if (alg_type(al) == al_REAL)
    6292           7 :     pari_err_TYPE("alglatrighttransporter [real algebra]", al);
    6293        1764 :   checklat(al,lat1);
    6294        1764 :   checklat(al,lat2);
    6295        1764 :   return gc_GEN(av, alglattransporter_i(al,lat1,lat2,1));
    6296             : }
    6297             : 
    6298             : GEN
    6299          42 : algmakeintegral(GEN mt0, long maps)
    6300             : {
    6301          42 :   pari_sp av = avma;
    6302             :   long n,i;
    6303             :   GEN m,P,Pi,mt2,mt;
    6304          42 :   n = lg(mt0)-1;
    6305          42 :   mt = check_mt(mt0,NULL);
    6306          42 :   if (!mt) pari_err_TYPE("algmakeintegral", mt0);
    6307          21 :   if (isint1(Q_denom(mt0))) {
    6308           7 :     if (maps) mt = mkvec3(mt,matid(n),matid(n));
    6309           7 :     return gc_GEN(av,mt);
    6310             :   }
    6311          14 :   dbg_printf(2)(" algmakeintegral: dim=%d, denom=%Ps\n", n, Q_denom(mt0));
    6312          14 :   m = cgetg(n+1,t_MAT);
    6313          56 :   for (i=1;i<=n;i++)
    6314          42 :     gel(m,i) = mat2col(gel(mt,i),n,n);
    6315          14 :   dbg_printf(2)(" computing order, dims m = %d x %d...\n", nbrows(m), lg(m)-1);
    6316          14 :   P = QM_invimZ(m);
    6317          14 :   dbg_printf(2)(" ...done.\n");
    6318          14 :   P = shallowmatconcat(mkvec2(col_ei(n,1),P));
    6319          14 :   P = hnf(P);
    6320          14 :   Pi = RgM_inv(P);
    6321          14 :   mt2 = change_Rgmultable(mt,P,Pi);
    6322          14 :   if (maps) mt2 = mkvec3(mt2,Pi,P); /* mt2, mt->mt2, mt2->mt */
    6323          14 :   return gc_GEN(av,mt2);
    6324             : }
    6325             : 
    6326             : /** ORDERS **/
    6327             : 
    6328             : /*
    6329             :  * algmodpr data:
    6330             :  * 1. pr
    6331             :  * 2. Vecsmall([k,m]) s.t. target is M_k(F_p^m). /!\ m can differ from pr.f
    6332             :  * 3. t_FFELT 1 representing the finite field F_q
    6333             :  * 4. proj: O -> M_k(F_q)
    6334             :  * 5. lift: M_k(F_q) -> O
    6335             :  * 6. tau: anti uniformizer (left multiplication matrix)
    6336             :  * 7. T s.t. F_q = F_p[x]/T
    6337             :  */
    6338             : GEN
    6339        2793 : algmodprinit(GEN al, GEN pr, long v)
    6340             : {
    6341        2793 :   pari_sp av = avma;
    6342             :   GEN p, alp, g, Q, pro, lif, map, mapi, alpr, spl, data, nf, T, J, tau;
    6343             :   long tal, k, m;
    6344        2793 :   checkalg(al); checkprid(pr);
    6345        2779 :   tal = alg_type(al);
    6346        2779 :   if (tal!=al_CYCLIC && tal!=al_CSA)
    6347          21 :     pari_err_TYPE("algmodprinit [use alginit]", al);
    6348        2758 :   nf = alg_get_center(al);
    6349        2758 :   p = pr_get_p(pr);
    6350        2758 :   alp = alg_ordermodp(al, p);
    6351        2758 :   g = algeltfromnf_i(al, pr_get_gen(pr));
    6352        2758 :   g = algbasismultable(alp, g);
    6353        2758 :   g = FpM_image(g, p);
    6354        2758 :   alpr = alg_quotient(alp, g, 1);
    6355        2758 :   Q = gel(alpr, 1);
    6356        2758 :   pro = gel(alpr, 2);
    6357        2758 :   lif = gel(alpr, 3);
    6358        2758 :   J = algradical(Q); /* could skip if we knew the order is maximal at unramified pr */
    6359        2758 :   if (!gequal0(J))
    6360             :   {
    6361          21 :     Q = alg_quotient(Q, J, 1);
    6362          21 :     pro = ZM_mul(gel(Q,2), pro);
    6363          21 :     lif = ZM_mul(lif, gel(Q,3));
    6364          21 :     Q = gel(Q,1);
    6365             :   }
    6366        2758 :   spl = alg_finite_csa_split(Q, v);
    6367        2758 :   T = gel(spl, 1); /* t_POL, possibly of degree 1 */
    6368        2758 :   mapi = gel(spl, 3);
    6369        2758 :   map = gel(spl, 4);
    6370        2758 :   tau = pr_anti_uniformizer(nf, pr);
    6371        2758 :   m = degpol(T);
    6372        2758 :   k = lg(gmael(spl,2,1)) - 1;
    6373        2758 :   if (typ(tau) != t_INT) tau = algbasismultable(al,algeltfromnf_i(al,tau));
    6374        2758 :   data = mkvecn(7,
    6375             :     pr,
    6376             :     mkvecsmall2(k, m),
    6377             :     Tp_to_FF(T,p),
    6378             :     FpM_mul(map, pro, p),
    6379             :     FpM_mul(lif, mapi, p),
    6380             :     tau,
    6381             :     T
    6382             :   );
    6383        2758 :   return gc_GEN(av, data);
    6384             : }
    6385             : 
    6386             : static int
    6387        2135 : checkalgmodpr_i(GEN data)
    6388             : {
    6389             :   GEN compo;
    6390        2135 :   if (typ(data)!=t_VEC || lg(data)!=8) return 0;
    6391        2121 :   checkprid(gel(data,1));
    6392        2114 :   compo = gel(data,2);
    6393        2114 :   if (typ(compo)!=t_VECSMALL || lg(compo)!=3) return 0;
    6394        2107 :   if (typ(gel(data,3))!=t_FFELT) return 0;
    6395        2100 :   if (typ(gel(data,4))!=t_MAT) return 0;
    6396        2093 :   if (typ(gel(data,5))!=t_MAT) return 0;
    6397        2086 :   compo = gel(data,6);
    6398        2086 :   if (typ(compo)!=t_MAT && (typ(compo)!=t_INT || !equali1(compo))) return 0;
    6399        2079 :   if (typ(gel(data,7))!=t_POL) return 0;
    6400        2072 :   return 1;
    6401             : }
    6402             : static void
    6403        2135 : checkalgmodpr(GEN data)
    6404             : {
    6405        2135 :   if(!checkalgmodpr_i(data))
    6406          56 :     pari_err_TYPE("checkalgmodpr [use algmodprinit()]", data);
    6407        2072 : }
    6408             : 
    6409             : /* x belongs to the stored order of al, no GC */
    6410             : static GEN
    6411        1708 : algmodpr_integral(GEN x, GEN data, long reduce)
    6412             : {
    6413             :   GEN res, T, p;
    6414        1708 :   long k, m, v = -1;
    6415        1708 :   T = algmodpr_get_T(data);
    6416        1708 :   if (T) v = varn(T);
    6417        1708 :   p = algmodpr_get_p(data);
    6418        1708 :   k = algmodpr_get_k(data);
    6419        1708 :   m = algmodpr_get_m(data);
    6420        1708 :   res = ZM_ZC_mul(algmodpr_get_proj(data), x);
    6421        1708 :   res = RgC_col2mat(res, k, m, v);
    6422        1708 :   return reduce? FqM_red(res, T, p) : res;
    6423             : }
    6424             : 
    6425             : /* x in basis form */
    6426             : static GEN
    6427        1729 : algmodpr_i(GEN x, GEN data)
    6428             : {
    6429             :   GEN T, p, res, den, tau;
    6430             :   long v, i, j;
    6431        1729 :   x = Q_remove_denom(x, &den);
    6432        1729 :   T = algmodpr_get_T(data);
    6433        1729 :   p = algmodpr_get_p(data);
    6434        1729 :   tau = algmodpr_get_tau(data);
    6435        1729 :   if (den)
    6436             :   {
    6437          35 :     v = Z_pvalrem(den, p, &den);
    6438          35 :     if (v && typ(tau)!=t_INT)
    6439             :     {
    6440             :       /* TODO not always better to exponentiate the matrix */
    6441          21 :       x = ZM_ZC_mul(ZM_powu(tau, v), x);
    6442          21 :       v -= ZV_pvalrem(x, p, &x);
    6443             :     }
    6444          35 :     if (v>0) pari_err_INV("algmodpr", mkintmod(gen_0,p));
    6445          21 :     if (v<0)
    6446             :     {
    6447           7 :       long k = algmodpr_get_k(data);
    6448           7 :       return zeromatcopy(k,k);
    6449             :     }
    6450          14 :     if (equali1(den)) den = NULL;
    6451             :   }
    6452        1708 :   res = algmodpr_integral(x, data, 0);
    6453        1708 :   if (den)
    6454             :   {
    6455           7 :     GEN d = Fp_inv(den, p);
    6456          21 :     for (j=1; j<lg(res); j++)
    6457          42 :       for (i=1; i<lg(res); i++)
    6458          28 :         gcoeff(res,i,j) = Fq_Fp_mul(gcoeff(res,i,j), d, T, p);
    6459             :   }
    6460        1701 :   else res = FqM_red(res, T, p);
    6461        1708 :   return res;
    6462             : }
    6463             : 
    6464             : static GEN
    6465          28 : algmodpr_mat(GEN al, GEN x, GEN data)
    6466             : {
    6467             :   GEN res, cx, c;
    6468             :   long i, j;
    6469          28 :   res = cgetg(lg(x),t_MAT);
    6470         133 :   for (j=1; j<lg(x); j++)
    6471             :   {
    6472         105 :     cx = gel(x,j);
    6473         105 :     c = cgetg(lg(cx), t_COL);
    6474         525 :     for (i=1; i<lg(cx); i++) gel(c,i) = algmodpr(al, gel(cx,i), data);
    6475         105 :     gel(res, j) = c;
    6476             :   }
    6477          28 :   return shallowmatconcat(res);
    6478             : }
    6479             : 
    6480             : GEN
    6481        1841 : algmodpr(GEN al, GEN x, GEN data)
    6482             : {
    6483        1841 :   pari_sp av = avma;
    6484             :   GEN res, ff;
    6485        1841 :   checkalgmodpr(data);
    6486        1785 :   if (typ(x) == t_MAT) return gc_GEN(av, algmodpr_mat(al,x,data));
    6487        1757 :   x = algalgtobasis(al, x);
    6488        1729 :   res = algmodpr_i(x, data);
    6489        1715 :   ff = algmodpr_get_ff(data);
    6490        1715 :   return gc_GEN(av, FqM_to_FFM(res,ff));
    6491             : }
    6492             : 
    6493             : static GEN
    6494         511 : algmodprlift_i(GEN x, GEN data)
    6495             : {
    6496         511 :   GEN lift, C, p, c, T = NULL;
    6497             :   long i, j, k, m;
    6498         511 :   lift = algmodpr_get_lift(data);
    6499         511 :   p = algmodpr_get_p(data);
    6500         511 :   k = algmodpr_get_k(data);
    6501         511 :   m = algmodpr_get_m(data); /* M_k(F_p^m) */
    6502         511 :   if (m > 1) T = algmodpr_get_T(data);
    6503         511 :   x = gcopy(x);
    6504        1561 :   for (i=1; i<=k; i++)
    6505        3689 :     for (j=1; j<=k; j++)
    6506             :     {
    6507        2639 :       c = gcoeff(x,i,j);
    6508        2639 :       if (typ(c) == t_FFELT)    gcoeff(x,i,j) = FF_to_FpXQ(c);
    6509         119 :       else if (m == 1)          gcoeff(x,i,j) = scalarpol(Rg_to_Fp(c,p), -1);
    6510          91 :       else                      gcoeff(x,i,j) = Rg_to_FpXQ(c, T, p);
    6511             :     }
    6512         504 :   C = RgM_mat2col(x, k, m);
    6513         504 :   return FpM_FpC_mul(lift, C, p);
    6514             : }
    6515             : 
    6516             : GEN
    6517         301 : algmodprlift(GEN al, GEN x, GEN data)
    6518             : {
    6519         301 :   pari_sp av = avma;
    6520             :   GEN res, blk;
    6521             :   long k, nc, nr, i, j;
    6522         301 :   checkalg(al);
    6523         294 :   checkalgmodpr(data);
    6524         287 :   k = algmodpr_get_k(data); /* M_k(F_p^m) */
    6525         287 :   if (typ(x) != t_MAT) pari_err_TYPE("algmodprlift [matrix x]",x);
    6526         280 :   if ((lg(x)-1)%k) pari_err_DIM("algmodprlift [matrix x, nb cols]");
    6527         273 :   nc = (lg(x)-1)/k;
    6528         273 :   if (!nc) return gc_upto(av, zeromat(0,0));
    6529         266 :   if ((lgcols(x)-1)%k) pari_err_DIM("algmodprlift [matrix x, nb rows]");
    6530         259 :   nr = nbrows(x)/k;
    6531         259 :   if (nr==1 && nc==1) res = algmodprlift_i(x, data);
    6532             :   else
    6533             :   {
    6534          28 :     res = zeromatcopy(nr, nc);
    6535         119 :     for (i=1; i<=nr; i++)
    6536         371 :       for(j=1; j<=nc; j++)
    6537             :       {
    6538         280 :         blk = matslice(x, (i-1)*k+1, i*k, (j-1)*k+1, j*k);
    6539         280 :         gcoeff(res,i,j) = algmodprlift_i(blk, data);
    6540             :       }
    6541             :   }
    6542         252 :   return gc_GEN(av, res);
    6543             : }
    6544             : 
    6545             : /* e in al such that e mod pr is a non-invertible idempotent of maximal rank */
    6546             : static GEN
    6547        2499 : eichleridempotent(GEN al, GEN pr)
    6548             : {
    6549             :   long i, k, n, nk, j;
    6550             :   GEN data, mapi, e;
    6551        2499 :   data = algmodprinit(al, pr, -1);
    6552        2492 :   mapi = algmodpr_get_lift(data);
    6553        2492 :   k = algmodpr_get_k(data);
    6554        2492 :   n = pr_get_f(pr);
    6555        2492 :   nk = n*(k+1);
    6556        2492 :   if (k==1) return zerocol(alg_get_absdim(al));
    6557        1820 :   e = gel(mapi,1+nk);
    6558        2681 :   for (i = 2, j = 1+2*nk; i < k; i++, j += nk) e = ZC_add(e,gel(mapi,j));
    6559        1820 :   return e;
    6560             : }
    6561             : 
    6562             : static GEN
    6563        2492 : mat_algeltfromnf(GEN al, GEN x)
    6564             : {
    6565        6244 :   pari_APPLY_type(t_MAT, algeltfromnf_i(al, gel(x,i)));
    6566             : }
    6567             : static GEN
    6568        2499 : eichlerprimepower_i(GEN al, GEN pr, long m, GEN prm)
    6569             : {
    6570             :   GEN p, e, polidem, Me, Mzk, nf, Mprm;
    6571             :   long ep, i;
    6572             :   ulong mask;
    6573        2499 :   polidem = mkpoln(4, gen_m2, utoi(3), gen_0, gen_0);
    6574        2499 :   p = pr_get_p(pr); ep = pr_get_e(pr);
    6575        2499 :   e = eichleridempotent(al, pr); /* ZC */
    6576        2492 :   mask = quadratic_prec_mask(m);
    6577        2492 :   i = 1;
    6578        6769 :   while (mask > 1)
    6579             :   {
    6580        4277 :     i *=2;
    6581        4277 :     if (mask & 1UL) i--;
    6582        4277 :     mask >>= 1;
    6583        4277 :     e = algpoleval(al, polidem, e);
    6584        4277 :     e = FpC_red(e, powiu(p,(i+ep-1)/ep));
    6585             :   }
    6586        2492 :   Me = algbasisrightmultable(al, e);
    6587        2492 :   nf = algcenter(al);
    6588        2492 :   Mzk = mat_algeltfromnf(al, nf_get_zk(nf));
    6589        2492 :   prm = idealtwoelt(nf, prm);
    6590        2492 :   Mprm = algbasismultable(al, algeltfromnf_i(al,gel(prm,2)));
    6591        2492 :   return hnfmodid(shallowmatconcat(mkvec3(Me,Mzk,Mprm)), gel(prm,1));
    6592             : }
    6593             : static GEN
    6594         546 : eichlerprimepower(GEN al, GEN pr, long m, GEN prm)
    6595             : {
    6596         546 :   pari_sp av = avma;
    6597         546 :   return gc_upto(av, eichlerprimepower_i(al, pr, m, prm));
    6598             : }
    6599             : 
    6600             : GEN
    6601        2100 : algeichlerbasis(GEN al, GEN N)
    6602             : {
    6603        2100 :   pari_sp av = avma;
    6604        2100 :   GEN nf, faN, LH = NULL, Cpr = NULL, Cm = NULL, Lpp, M, H, pp, LH2;
    6605             :   long k, n, ih, lh, np;
    6606             : 
    6607        2100 :   checkalg(al);
    6608        2093 :   nf = alg_get_center(al);
    6609        2086 :   if (checkprid_i(N)) return eichlerprimepower(al,N,1,N);
    6610        2065 :   if (is_nf_factor(N))
    6611             :   {
    6612        2037 :     faN = sort_factor(shallowcopy(N), (void*)&cmp_prime_ideal, &cmp_nodata);
    6613        2037 :     N = factorbackprime(nf, gel(faN,1), gel(faN,2));
    6614             :   }
    6615          28 :   else faN = idealfactor(nf, N);
    6616        2051 :   n = nbrows(faN);
    6617        2051 :   if (!n) { set_avma(av); return matid(alg_get_absdim(al)); }
    6618        2044 :   if (n==1)
    6619             :   {
    6620        1953 :     GEN pr = gcoeff(faN,1,1), mZ = gcoeff(faN,1,2);
    6621        1953 :     long m = itos(mZ);
    6622        1953 :     return gc_upto(av, eichlerprimepower_i(al, pr, m, N));
    6623             :   }
    6624             : 
    6625             :   /* collect prime power Eichler orders */
    6626          91 :   Lpp = cgetg(n+1,t_VEC);
    6627          91 :   LH2 = cgetg(n+1, t_VEC);
    6628          91 :   np = 0;
    6629          91 :   ih = 1;
    6630          91 :   lh = 1;
    6631         616 :   for (k = 1; k <= n; k++)
    6632             :   {
    6633         525 :     GEN pr = gcoeff(faN,k,1), mZ = gcoeff(faN,k,2), prm;
    6634         525 :     long m = itos(mZ);
    6635             : 
    6636         525 :     if (ih == lh) /* done with previous p, prepare next */
    6637             :     {
    6638         462 :       GEN p = pr_get_p(pr);
    6639         462 :       long k2 = k + 1;
    6640         462 :       np++;
    6641         462 :       gel(Lpp,np) = gen_0;
    6642         462 :       lh = 2;
    6643             :       /* count the pr|p in faN */
    6644         525 :       while (k2<=n && equalii(p,pr_get_p(gcoeff(faN,k2,1)))) { lh++; k2++; }
    6645         462 :       LH = cgetg(lh, t_VEC);
    6646         462 :       Cpr = cgetg(lh, t_VEC);
    6647         462 :       Cm = cgetg(lh, t_VEC);
    6648         462 :       ih = 1;
    6649             :     }
    6650         525 :     prm = idealpow(nf, pr, mZ);
    6651         525 :     H = eichlerprimepower(al, pr, m, prm);
    6652         525 :     pp = gcoeff(prm,1,1);
    6653         525 :     if (cmpii(pp,gel(Lpp,np)) > 0) gel(Lpp,np) = pp;
    6654         525 :     gel(LH,ih) = H;
    6655         525 :     gel(Cpr,ih) = pr;
    6656         525 :     gel(Cm,ih) = mZ;
    6657         525 :     ih++;
    6658             : 
    6659         525 :     if (ih == lh) /* done with this p */
    6660             :     {
    6661         462 :       if (lh == 2) gel(LH2,np) = gel(LH,1);
    6662             :       else
    6663             :       { /* put together the pr|p */
    6664          63 :         GEN U = gmael(idealchineseinit(nf, mkmat2(Cpr,Cm)),1,2);
    6665             :         long i;
    6666         189 :         for (i = 1; i < lh; i++)
    6667             :         {
    6668         126 :           GEN e = algeltfromnf_i(al, gel(U,i));
    6669         126 :           e = algbasismultable(al, e);
    6670         126 :           gel(LH,i) = ZM_mul(e, gel(LH,i));
    6671             :         }
    6672          63 :         gel(LH2,np) = hnfmodid(shallowmatconcat(LH), gel(Lpp,np));
    6673             :       }
    6674             :     }
    6675             :   }
    6676          91 :   if (np == 1) return gc_GEN(av, gel(LH2,1));
    6677             :   /* put together all p */
    6678          84 :   setlg(Lpp,np+1);
    6679          84 :   setlg(LH2,np+1);
    6680          84 :   H = nmV_chinese_center(LH2, Lpp, &M);
    6681          84 :   return gc_upto(av, hnfmodid(H, M));
    6682             : }
    6683             : 
    6684             : /** IDEALS **/

Generated by: LCOV version 1.16