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 - basemath - modsym.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23036-b751c0af5) Lines: 2556 2681 95.3 %
Date: 2018-09-26 05:46:06 Functions: 274 279 98.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2011  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /* Adapted from shp_package/moments by Robert Pollack
      18             :  * http://www.math.mcgill.ca/darmon/programs/shp/shp.html */
      19             : static GEN mskinit(ulong N, long k, long sign);
      20             : static GEN mshecke_i(GEN W, ulong p);
      21             : static GEN ZSl2_star(GEN v);
      22             : static GEN getMorphism(GEN W1, GEN W2, GEN v);
      23             : static GEN voo_act_Gl2Q(GEN g, long k);
      24             : 
      25             : /* Input: P^1(Z/NZ) (formed by create_p1mod)
      26             :    Output: # P^1(Z/NZ) */
      27             : static long
      28        6748 : p1_size(GEN p1N) { return lg(gel(p1N,1)) - 1; }
      29             : static ulong
      30     6770288 : p1N_get_N(GEN p1N) { return gel(p1N,3)[2]; }
      31             : static GEN
      32     2800399 : p1N_get_hash(GEN p1N) { return gel(p1N,2); }
      33             : static GEN
      34        1813 : p1N_get_fa(GEN p1N) { return gel(p1N,4); }
      35             : static GEN
      36        1708 : p1N_get_div(GEN p1N) { return gel(p1N,5); }
      37             : static GEN
      38     2679838 : p1N_get_invsafe(GEN p1N) { return gel(p1N,6); }
      39             : static GEN
      40      905317 : p1N_get_inverse(GEN p1N) { return gel(p1N,7); }
      41             : 
      42             : /* ms-specific accessors */
      43             : /* W = msinit, return the output of msinit_N */
      44             : static GEN
      45      657741 : get_msN(GEN W) { return lg(W) == 4? gel(W,1): W; }
      46             : static GEN
      47      750995 : msN_get_p1N(GEN W) { return gel(W,1); }
      48             : static GEN
      49      208488 : msN_get_genindex(GEN W) { return gel(W,5); }
      50             : static GEN
      51     5382986 : msN_get_E2fromE1(GEN W) { return gel(W,7); }
      52             : static GEN
      53        1393 : msN_get_annT2(GEN W) { return gel(W,8); }
      54             : static GEN
      55        1393 : msN_get_annT31(GEN W) { return gel(W,9); }
      56             : static GEN
      57        1358 : msN_get_singlerel(GEN W) { return gel(W,10); }
      58             : static GEN
      59      677565 : msN_get_section(GEN W) { return gel(W,12); }
      60             : 
      61             : static GEN
      62       83377 : ms_get_p1N(GEN W) { return msN_get_p1N(get_msN(W)); }
      63             : static long
      64       73150 : ms_get_N(GEN W) { return p1N_get_N(ms_get_p1N(W)); }
      65             : static GEN
      66        1869 : ms_get_hashcusps(GEN W) { W = get_msN(W); return gel(W,16); }
      67             : static GEN
      68       14896 : ms_get_section(GEN W) { return msN_get_section(get_msN(W)); }
      69             : static GEN
      70      204400 : ms_get_genindex(GEN W) { return msN_get_genindex(get_msN(W)); }
      71             : static long
      72      199605 : ms_get_nbgen(GEN W) { return lg(ms_get_genindex(W))-1; }
      73             : static long
      74      162169 : ms_get_nbE1(GEN W)
      75             : {
      76             :   GEN W11;
      77      162169 :   W = get_msN(W); W11 = gel(W,11);
      78      162169 :   return W11[4] - W11[3];
      79             : }
      80             : 
      81             : /* msk-specific accessors */
      82             : static long
      83          84 : msk_get_dim(GEN W) { return gmael(W,3,2)[2]; }
      84             : static GEN
      85       82838 : msk_get_basis(GEN W) { return gmael(W,3,1); }
      86             : static long
      87       46081 : msk_get_weight(GEN W) { return gmael(W,3,2)[1]; }
      88             : static long
      89       20874 : msk_get_sign(GEN W)
      90             : {
      91       20874 :   GEN t = gel(W,2);
      92       20874 :   return typ(t)==t_INT? 0: itos(gel(t,1));
      93             : }
      94             : static GEN
      95         644 : msk_get_star(GEN W) { return gmael(W,2,2); }
      96             : static GEN
      97        3626 : msk_get_starproj(GEN W) { return gmael(W,2,3); }
      98             : 
      99             : static int
     100         322 : is_Qevproj(GEN x)
     101         322 : { return typ(x) == t_VEC && lg(x) == 5 && typ(gel(x,1)) == t_MAT; }
     102             : long
     103         112 : msdim(GEN W)
     104             : {
     105         112 :   if (is_Qevproj(W)) return lg(gel(W,1)) - 1;
     106          98 :   checkms(W);
     107          98 :   if (!msk_get_sign(W)) return msk_get_dim(W);
     108          28 :   return lg(gel(msk_get_starproj(W), 1)) - 1;
     109             : }
     110             : long
     111          14 : msgetlevel(GEN W) { checkms(W); return ms_get_N(W); }
     112             : long
     113          14 : msgetweight(GEN W) { checkms(W); return msk_get_weight(W); }
     114             : long
     115          28 : msgetsign(GEN W) { checkms(W); return msk_get_sign(W); }
     116             : 
     117             : void
     118       36113 : checkms(GEN W)
     119             : {
     120       36113 :   if (typ(W) != t_VEC || lg(W) != 4)
     121           0 :     pari_err_TYPE("checkms [please apply msinit]", W);
     122       36113 : }
     123             : 
     124             : /** MODULAR TO SYM **/
     125             : 
     126             : /* q a t_FRAC or t_INT */
     127             : static GEN
     128        7756 : Q_log_init(ulong N, GEN q)
     129             : {
     130             :   long l, n;
     131             :   GEN Q;
     132             : 
     133        7756 :   q = gboundcf(q, 0);
     134        7756 :   l = lg(q);
     135        7756 :   Q = cgetg(l, t_VECSMALL);
     136        7756 :   Q[1] = 1;
     137        7756 :   for (n=2; n <l; n++) Q[n] = umodiu(gel(q,n), N);
     138       17255 :   for (n=3; n < l; n++)
     139        9499 :     Q[n] = Fl_add(Fl_mul(Q[n], Q[n-1], N), Q[n-2], N);
     140        7756 :   return Q;
     141             : }
     142             : 
     143             : /** INIT MODSYM STRUCTURE, WEIGHT 2 **/
     144             : 
     145             : /* num = [Gamma : Gamma_0(N)] = N * Prod_{p|N} (1+p^-1) */
     146             : static ulong
     147        1708 : count_Manin_symbols(ulong N, GEN P)
     148             : {
     149        1708 :   long i, l = lg(P);
     150        1708 :   ulong num = N;
     151        1708 :   for (i = 1; i < l; i++) { ulong p = P[i]; num *= p+1; num /= p; }
     152        1708 :   return num;
     153             : }
     154             : /* returns the list of "Manin symbols" (c,d) in (Z/NZ)^2, (c,d,N) = 1
     155             :  * generating H^1(X_0(N), Z) */
     156             : static GEN
     157        1708 : generatemsymbols(ulong N, ulong num, GEN divN)
     158             : {
     159        1708 :   GEN ret = cgetg(num+1, t_VEC);
     160        1708 :   ulong c, d, curn = 0;
     161             :   long i, l;
     162             :   /* generate Manin-symbols in two lists: */
     163             :   /* list 1: (c:1) for 0 <= c < N */
     164        1708 :   for (c = 0; c < N; c++) gel(ret, ++curn) = mkvecsmall2(c, 1);
     165        1708 :   if (N == 1) return ret;
     166             :   /* list 2: (c:d) with 1 <= c < N, c | N, 0 <= d < N, gcd(d,N) > 1, gcd(c,d)=1.
     167             :    * Furthermore, d != d0 (mod N/c) with c,d0 already in the list */
     168        1687 :   l = lg(divN) - 1;
     169             :   /* c = 1 first */
     170        1687 :   gel(ret, ++curn) = mkvecsmall2(1,0);
     171      152593 :   for (d = 2; d < N; d++)
     172      150906 :     if (ugcd(d,N) != 1UL)
     173       54733 :       gel(ret, ++curn) = mkvecsmall2(1,d);
     174             :   /* omit c = 1 (first) and c = N (last) */
     175        4844 :   for (i=2; i < l; i++)
     176             :   {
     177             :     ulong Novc, d0;
     178        3157 :     c = divN[i];
     179        3157 :     Novc = N / c;
     180       84861 :     for (d0 = 2; d0 <= Novc; d0++)
     181             :     {
     182       81704 :       ulong k, d = d0;
     183       81704 :       if (ugcd(d, Novc) == 1UL) continue;
     184      131299 :       for (k = 0; k < c; k++, d += Novc)
     185      119805 :         if (ugcd(c,d) == 1UL)
     186             :         {
     187       18634 :           gel(ret, ++curn) = mkvecsmall2(c,d);
     188       18634 :           break;
     189             :         }
     190             :     }
     191             :   }
     192        1687 :   if (curn != num) pari_err_BUG("generatemsymbols [wrong number of symbols]");
     193        1687 :   return ret;
     194             : }
     195             : 
     196             : static GEN
     197        1708 : inithashmsymbols(ulong N, GEN symbols)
     198             : {
     199        1708 :   GEN H = zerovec(N);
     200        1708 :   long k, l = lg(symbols);
     201             :   /* skip the (c:1), 0 <= c < N and (1:0) */
     202       75075 :   for (k=N+2; k < l; k++)
     203             :   {
     204       73367 :     GEN s = gel(symbols, k);
     205       73367 :     ulong c = s[1], d = s[2], Novc = N/c;
     206       73367 :     if (gel(H,c) == gen_0) gel(H,c) = const_vecsmall(Novc+1,0);
     207       73367 :     if (c != 1) { d %= Novc; if (!d) d = Novc; }
     208       73367 :     mael(H, c, d) = k;
     209             :   }
     210        1708 :   return H;
     211             : }
     212             : 
     213             : /** Helper functions for Sl2(Z) / Gamma_0(N) **/
     214             : /* M a 2x2 ZM in SL2(Z) */
     215             : static GEN
     216     1207556 : SL2_inv(GEN M)
     217             : {
     218     1207556 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
     219     1207556 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
     220     1207556 :   return mkmat22(d,negi(b), negi(c),a);
     221             : }
     222             : /* SL2_inv(M)[2] */
     223             : static GEN
     224        3514 : SL2_inv2(GEN M)
     225             : {
     226        3514 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
     227        3514 :   return mkcol2(negi(b),a);
     228             : }
     229             : /* M a 2x2 mat2 in SL2(Z) */
     230             : static GEN
     231      661101 : sl2_inv(GEN M)
     232             : {
     233      661101 :   long a=coeff(M,1,1), b=coeff(M,1,2), c=coeff(M,2,1), d=coeff(M,2,2);
     234      661101 :   return mkvec2(mkvecsmall2(d, -c), mkvecsmall2(-b, a));
     235             : }
     236             : /* Return the mat2 [a,b; c,d], not a zm to avoid GP problems */
     237             : static GEN
     238     1309966 : mat2(long a, long b, long c, long d)
     239     1309966 : { return mkvec2(mkvecsmall2(a,c), mkvecsmall2(b,d)); }
     240             : static GEN
     241      332045 : mat2_to_ZM(GEN M)
     242             : {
     243      332045 :   GEN A = gel(M,1), B = gel(M,2);
     244      332045 :   retmkmat2(mkcol2s(A[1],A[2]), mkcol2s(B[1],B[2]));
     245             : }
     246             : 
     247             : /* Input: a = 2-vector = path = {r/s,x/y}
     248             :  * Output: either [r,x;s,y] or [-r,x;-s,y], whichever has determinant > 0 */
     249             : static GEN
     250       80388 : path_to_ZM(GEN a)
     251             : {
     252       80388 :   GEN v = gel(a,1), w = gel(a,2);
     253       80388 :   long r = v[1], s = v[2], x = w[1], y = w[2];
     254       80388 :   if (cmpii(mulss(r,y), mulss(x,s)) < 0) { r = -r; s = -s; }
     255       80388 :   return mkmat22(stoi(r),stoi(x),stoi(s),stoi(y));
     256             : }
     257             : static GEN
     258      807205 : path_to_zm(GEN a)
     259             : {
     260      807205 :   GEN v = gel(a,1), w = gel(a,2);
     261      807205 :   long r = v[1], s = v[2], x = w[1], y = w[2];
     262      807205 :   if (cmpii(mulss(r,y), mulss(x,s)) < 0) { r = -r; s = -s; }
     263      807205 :   return mat2(r,x,s,y);
     264             : }
     265             : /* path from c1 to c2 */
     266             : static GEN
     267      464688 : mkpath(GEN c1, GEN c2) { return mat2(c1[1], c2[1], c1[2], c2[2]); }
     268             : static long
     269      656488 : cc(GEN M) { GEN v = gel(M,1); return v[2]; }
     270             : static long
     271      656488 : dd(GEN M) { GEN v = gel(M,2); return v[2]; }
     272             : 
     273             : /*Input: a,b = 2 paths, N = integer
     274             :  *Output: 1 if the a,b are \Gamma_0(N)-equivalent; 0 otherwise */
     275             : static int
     276       75474 : gamma_equiv(GEN a, GEN b, ulong N)
     277             : {
     278       75474 :   pari_sp av = avma;
     279       75474 :   GEN m = path_to_zm(a);
     280       75474 :   GEN n = path_to_zm(b);
     281       75474 :   GEN d = subii(mulss(cc(m),dd(n)), mulss(dd(m),cc(n)));
     282       75474 :   return gc_bool(av, umodiu(d, N) == 0);
     283             : }
     284             : /* Input: a,b = 2 paths that are \Gamma_0(N)-equivalent, N = integer
     285             :  * Output: M in \Gamma_0(N) such that Mb=a */
     286             : static GEN
     287       39830 : gamma_equiv_matrix(GEN a, GEN b)
     288             : {
     289       39830 :   GEN m = path_to_ZM(a);
     290       39830 :   GEN n = path_to_ZM(b);
     291       39830 :   return ZM_mul(m, SL2_inv(n));
     292             : }
     293             : 
     294             : /*************/
     295             : /* P^1(Z/NZ) */
     296             : /*************/
     297             : /* a != 0 in Z/NZ. Return v in (Z/NZ)^* such that av = gcd(a, N) (mod N)*/
     298             : static ulong
     299      354438 : Fl_inverse(ulong a, ulong N) { ulong g; return Fl_invgen(a,N,&g); }
     300             : 
     301             : /* Input: N = integer
     302             :  * Output: creates P^1(Z/NZ) = [symbols, H, N]
     303             :  *   symbols: list of vectors [x,y] that give a set of representatives
     304             :  *            of P^1(Z/NZ)
     305             :  *   H: an M by M grid whose value at the r,c-th place is the index of the
     306             :  *      "standard representative" equivalent to [r,c] occuring in the first
     307             :  *      list. If gcd(r,c,N) > 1 the grid has value 0. */
     308             : static GEN
     309        1708 : create_p1mod(ulong N)
     310             : {
     311        1708 :   GEN fa = factoru(N), div = divisorsu_fact(fa);
     312        1708 :   ulong i, nsym = count_Manin_symbols(N, gel(fa,1));
     313        1708 :   GEN symbols = generatemsymbols(N, nsym, div);
     314        1708 :   GEN H = inithashmsymbols(N,symbols);
     315        1708 :   GEN invsafe = cgetg(N, t_VECSMALL), inverse = cgetg(N, t_VECSMALL);
     316      154301 :   for (i = 1; i < N; i++)
     317             :   {
     318      152593 :     invsafe[i] = Fl_invsafe(i,N);
     319      152593 :     inverse[i] = Fl_inverse(i,N);
     320             :   }
     321        1708 :   return mkvecn(7, symbols, H, utoipos(N), fa, div, invsafe, inverse);
     322             : }
     323             : 
     324             : /* Let (c : d) in P1(Z/NZ).
     325             :  * If c = 0 return (0:1). If d = 0 return (1:0).
     326             :  * Else replace by (cu : du), where u in (Z/NZ)^* such that C := cu = gcd(c,N).
     327             :  * In create_p1mod(), (c : d) is represented by (C:D) where D = du (mod N/c)
     328             :  * is smallest such that gcd(C,D) = 1. Return (C : du mod N/c), which need
     329             :  * not belong to P1(Z/NZ) ! A second component du mod N/c = 0 is replaced by
     330             :  * N/c in this case to avoid problems with array indices */
     331             : static void
     332     2800399 : p1_std_form(long *pc, long *pd, GEN p1N)
     333             : {
     334     2800399 :   ulong N = p1N_get_N(p1N);
     335             :   ulong u;
     336     2800399 :   *pc = smodss(*pc, N); if (!*pc) { *pd = 1; return; }
     337     2719605 :   *pd = smodss(*pd, N); if (!*pd) { *pc = 1; return; }
     338     2679838 :   u = p1N_get_invsafe(p1N)[*pd];
     339     2679838 :   if (u) { *pc = Fl_mul(*pc,u,N); *pd = 1; return; } /* (d,N) = 1 */
     340             : 
     341      905317 :   u = p1N_get_inverse(p1N)[*pc];
     342      905317 :   if (u > 1) { *pc = Fl_mul(*pc,u,N); *pd = Fl_mul(*pd,u,N); }
     343             :   /* c | N */
     344      905317 :   if (*pc != 1) *pd %= (N / *pc);
     345      905317 :   if (!*pd) *pd = N / *pc;
     346             : }
     347             : 
     348             : /* Input: v = [x,y] = elt of P^1(Z/NZ) = class in Gamma_0(N) \ PSL2(Z)
     349             :  * Output: returns the index of the standard rep equivalent to v */
     350             : static long
     351     2800399 : p1_index(long x, long y, GEN p1N)
     352             : {
     353     2800399 :   ulong N = p1N_get_N(p1N);
     354     2800399 :   GEN H = p1N_get_hash(p1N);
     355             : 
     356     2800399 :   p1_std_form(&x, &y, p1N);
     357     2800399 :   if (y == 1) return x+1;
     358      945084 :   if (y == 0) return N+1;
     359      905317 :   if (mael(H,x,y) == 0) pari_err_BUG("p1_index");
     360      905317 :   return mael(H,x,y);
     361             : }
     362             : 
     363             : /* Cusps for \Gamma_0(N) */
     364             : 
     365             : /* \sum_{d | N} \phi(gcd(d, N/d)), using multiplicativity. fa = factor(N) */
     366             : ulong
     367        1764 : mfnumcuspsu_fact(GEN fa)
     368             : {
     369        1764 :   GEN P = gel(fa,1), E = gel(fa,2);
     370        1764 :   long i, l = lg(P);
     371        1764 :   ulong T = 1;
     372        4445 :   for (i = 1; i < l; i++)
     373             :   {
     374        2681 :     long e = E[i], e2 = e >> 1; /* floor(E[i] / 2) */
     375        2681 :     ulong p = P[i];
     376        2681 :     if (odd(e))
     377        2331 :       T *= 2 * upowuu(p, e2);
     378             :     else
     379         350 :       T *= (p+1) * upowuu(p, e2-1);
     380             :   }
     381        1764 :   return T;
     382             : }
     383             : ulong
     384           7 : mfnumcuspsu(ulong n)
     385           7 : { pari_sp av = avma; return gc_ulong(av, mfnumcuspsu_fact( factoru(n) )); }
     386             : /* \sum_{d | N} \phi(gcd(d, N/d)), using multiplicativity. fa = factor(N) */
     387             : GEN
     388          14 : mfnumcusps_fact(GEN fa)
     389             : {
     390          14 :   GEN P = gel(fa,1), E = gel(fa,2), T = gen_1;
     391          14 :   long i, l = lg(P);
     392          35 :   for (i = 1; i < l; i++)
     393             :   {
     394          21 :     GEN p = gel(P,i), c;
     395          21 :     long e = itos(gel(E,i)), e2 = e >> 1; /* floor(E[i] / 2) */
     396          21 :     if (odd(e))
     397           0 :       c = shifti(powiu(p, e2), 1);
     398             :     else
     399          21 :       c = mulii(addiu(p,1), powiu(p, e2-1));
     400          21 :     T = T? mulii(T, c): c;
     401             :   }
     402          14 :   return T? T: gen_1;
     403             : }
     404             : GEN
     405          21 : mfnumcusps(GEN n)
     406             : {
     407          21 :   pari_sp av = avma;
     408          21 :   GEN F = check_arith_pos(n,"mfnumcusps");
     409          21 :   if (!F)
     410             :   {
     411          14 :     if (lgefint(n) == 3) return utoi( mfnumcuspsu(n[2]) );
     412           7 :     F = absZ_factor(n);
     413             :   }
     414          14 :   return gerepileuptoint(av, mfnumcusps_fact(F));
     415             : }
     416             : 
     417             : 
     418             : /* to each cusp in \Gamma_0(N) P1(Q), represented by p/q, we associate a
     419             :  * unique index. Canonical representative: (1:0) or (p:q) with q | N, q < N,
     420             :  * p defined modulo d := gcd(N/q,q), (p,d) = 1.
     421             :  * Return [[N, nbcusps], H, cusps]*/
     422             : static GEN
     423        1708 : inithashcusps(GEN p1N)
     424             : {
     425        1708 :   ulong N = p1N_get_N(p1N);
     426        1708 :   GEN div = p1N_get_div(p1N), H = zerovec(N+1);
     427        1708 :   long k, ind, l = lg(div), ncusp = mfnumcuspsu_fact(p1N_get_fa(p1N));
     428        1708 :   GEN cusps = cgetg(ncusp+1, t_VEC);
     429             : 
     430        1708 :   gel(H,1) = mkvecsmall2(0/*empty*/, 1/* first cusp: (1:0) */);
     431        1708 :   gel(cusps, 1) = mkvecsmall2(1,0);
     432        1708 :   ind = 2;
     433        6552 :   for (k=1; k < l-1; k++) /* l-1: remove q = N */
     434             :   {
     435        4844 :     ulong p, q = div[k], d = ugcd(q, N/q);
     436        4844 :     GEN h = const_vecsmall(d+1,0);
     437        4844 :     gel(H,q+1) = h ;
     438       12824 :     for (p = 0; p < d; p++)
     439        7980 :       if (ugcd(p,d) == 1)
     440             :       {
     441        6258 :         h[p+1] = ind;
     442        6258 :         gel(cusps, ind) = mkvecsmall2(p,q);
     443        6258 :         ind++;
     444             :       }
     445             :   }
     446        1708 :   return mkvec3(mkvecsmall2(N,ind-1), H, cusps);
     447             : }
     448             : /* c = [p,q], (p,q) = 1, return a canonical representative for
     449             :  * \Gamma_0(N)(p/q) */
     450             : static GEN
     451      203770 : cusp_std_form(GEN c, GEN S)
     452             : {
     453      203770 :   long p, N = gel(S,1)[1], q = smodss(c[2], N);
     454             :   ulong u, d;
     455      203770 :   if (q == 0) return mkvecsmall2(1, 0);
     456      201845 :   p = smodss(c[1], N);
     457      201845 :   u = Fl_inverse(q, N);
     458      201845 :   q = Fl_mul(q,u, N);
     459      201845 :   d = ugcd(q, N/q);
     460      201845 :   return mkvecsmall2(Fl_div(p % d,u % d, d), q);
     461             : }
     462             : /* c = [p,q], (p,q) = 1, return the index of the corresponding cusp.
     463             :  * S from inithashcusps */
     464             : static ulong
     465      203770 : cusp_index(GEN c, GEN S)
     466             : {
     467             :   long p, q;
     468      203770 :   GEN H = gel(S,2);
     469      203770 :   c = cusp_std_form(c, S);
     470      203770 :   p = c[1]; q = c[2];
     471      203770 :   if (!mael(H,q+1,p+1)) pari_err_BUG("cusp_index");
     472      203770 :   return mael(H,q+1,p+1);
     473             : }
     474             : 
     475             : /* M a square invertible ZM, return a ZM iM such that iM M = M iM = d.Id */
     476             : static GEN
     477        3052 : ZM_inv_denom(GEN M)
     478             : {
     479        3052 :   GEN diM, iM = ZM_inv(M, &diM);
     480        3052 :   return mkvec2(iM, diM);
     481             : }
     482             : /* return M^(-1) v, dinv = ZM_inv_denom(M) OR Qevproj_init(M) */
     483             : static GEN
     484      745115 : ZC_apply_dinv(GEN dinv, GEN v)
     485             : {
     486             :   GEN x, c, iM;
     487      745115 :   if (lg(dinv) == 3)
     488             :   {
     489      666421 :     iM = gel(dinv,1);
     490      666421 :     c = gel(dinv,2);
     491             :   }
     492             :   else
     493             :   { /* Qevproj_init */
     494       78694 :     iM = gel(dinv,2);
     495       78694 :     c = gel(dinv,3);
     496      157388 :     v = typ(v) == t_MAT? rowpermute(v, gel(dinv,4))
     497       78694 :                        : vecpermute(v, gel(dinv,4));
     498             :   }
     499      745115 :   x = RgM_RgC_mul(iM, v);
     500      745115 :   if (!isint1(c)) x = RgC_Rg_div(x, c);
     501      745115 :   return x;
     502             : }
     503             : 
     504             : /* M an n x d ZM of rank d (basis of a Q-subspace), n >= d.
     505             :  * Initialize a projector on M */
     506             : GEN
     507        4886 : Qevproj_init(GEN M)
     508             : {
     509             :   GEN v, perm, MM, iM, diM;
     510        4886 :   v = ZM_indexrank(M); perm = gel(v,1);
     511        4886 :   MM = rowpermute(M, perm); /* square invertible */
     512        4886 :   iM = ZM_inv(MM, &diM);
     513        4886 :   return mkvec4(M, iM, diM, perm);
     514             : }
     515             : 
     516             : /* same with typechecks */
     517             : static GEN
     518         714 : Qevproj_init0(GEN M)
     519             : {
     520         714 :   switch(typ(M))
     521             :   {
     522             :     case t_VEC:
     523         658 :       if (lg(M) == 5) return M;
     524           0 :       break;
     525             :     case t_COL:
     526          49 :       M = mkmat(M);/*fall through*/
     527             :     case t_MAT:
     528          56 :       M = Q_primpart(M);
     529          56 :       RgM_check_ZM(M,"Qevproj_init");
     530          56 :       return Qevproj_init(M);
     531             :   }
     532           0 :   pari_err_TYPE("Qevproj_init",M);
     533           0 :   return NULL;
     534             : }
     535             : 
     536             : /* T an n x n QM, pro = Qevproj_init(M), pro2 = Qevproj_init(M2); TM \subset M2.
     537             :  * Express these column vectors on M2's basis */
     538             : static GEN
     539        3724 : Qevproj_apply2(GEN T, GEN pro, GEN pro2)
     540             : {
     541        3724 :   GEN M = gel(pro,1), iM = gel(pro2,2), ciM = gel(pro2,3), perm = gel(pro2,4);
     542        3724 :   return RgM_Rg_div(RgM_mul(iM, RgM_mul(rowpermute(T,perm), M)), ciM);
     543             : }
     544             : /* T an n x n QM, stabilizing d-dimensional Q-vector space spanned by the
     545             :  * d columns of M, pro = Qevproj_init(M). Return dxd matrix of T acting on M */
     546             : GEN
     547        3122 : Qevproj_apply(GEN T, GEN pro) { return Qevproj_apply2(T, pro, pro); }
     548             : /* Qevproj_apply(T,pro)[,k] */
     549             : GEN
     550         819 : Qevproj_apply_vecei(GEN T, GEN pro, long k)
     551             : {
     552         819 :   GEN M = gel(pro,1), iM = gel(pro,2), ciM = gel(pro,3), perm = gel(pro,4);
     553         819 :   GEN v = RgM_RgC_mul(iM, RgM_RgC_mul(rowpermute(T,perm), gel(M,k)));
     554         819 :   return RgC_Rg_div(v, ciM);
     555             : }
     556             : 
     557             : static GEN
     558        1183 : QM_ker_r(GEN M) { return ZM_ker(Q_primpart(M)); }
     559             : static GEN
     560         959 : QM_image(GEN A)
     561             : {
     562         959 :   A = vec_Q_primpart(A);
     563         959 :   return vecpermute(A, ZM_indeximage(A));
     564             : }
     565             : 
     566             : static int
     567         420 : cmp_dim(void *E, GEN a, GEN b)
     568             : {
     569             :   long k;
     570             :   (void)E;
     571         420 :   a = gel(a,1);
     572         420 :   b = gel(b,1); k = lg(a)-lg(b);
     573         420 :   return k? ((k > 0)? 1: -1): 0;
     574             : }
     575             : 
     576             : /* FIXME: could use ZX_roots for deglim = 1 */
     577             : static GEN
     578         329 : ZX_factor_limit(GEN T, long deglim, long *pl)
     579             : {
     580         329 :   GEN fa = ZX_factor(T), P, E;
     581             :   long i, l;
     582         329 :   P = gel(fa,1); *pl = l = lg(P);
     583         329 :   if (deglim <= 0) return fa;
     584         224 :   E = gel(fa,2);
     585         567 :   for (i = 1; i < l; i++)
     586         406 :     if (degpol(gel(P,i)) > deglim) break;
     587         224 :   setlg(P,i);
     588         224 :   setlg(E,i); return fa;
     589             : }
     590             : 
     591             : /* Decompose the subspace H (Qevproj format) in simple subspaces.
     592             :  * Eg for H = msnew */
     593             : static GEN
     594         259 : mssplit_i(GEN W, GEN H, long deglim)
     595             : {
     596         259 :   ulong p, N = ms_get_N(W);
     597             :   long first, dim;
     598             :   forprime_t S;
     599         259 :   GEN T1 = NULL, T2 = NULL, V;
     600         259 :   dim = lg(gel(H,1))-1;
     601         259 :   V = vectrunc_init(dim+1);
     602         259 :   if (!dim) return V;
     603         252 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
     604         252 :   vectrunc_append(V, H);
     605         252 :   first = 1; /* V[1..first-1] contains simple subspaces */
     606         630 :   while ((p = u_forprime_next(&S)))
     607             :   {
     608             :     GEN T;
     609             :     long j, lV;
     610         378 :     if (N % p == 0) continue;
     611         322 :     if (T1 && T2) {
     612          21 :       T = RgM_add(T1,T2);
     613          21 :       T2 = NULL;
     614             :     } else {
     615         301 :       T2 = T1;
     616         301 :       T1 = T = mshecke(W, p, NULL);
     617             :     }
     618         322 :     lV = lg(V);
     619         651 :     for (j = first; j < lV; j++)
     620             :     {
     621         329 :       pari_sp av = avma;
     622             :       long lP;
     623         329 :       GEN Vj = gel(V,j), P = gel(Vj,1);
     624         329 :       GEN TVj = Qevproj_apply(T, Vj); /* c T | V_j */
     625         329 :       GEN ch = QM_charpoly_ZX(TVj), fa = ZX_factor_limit(ch,deglim, &lP);
     626         329 :       GEN F = gel(fa, 1), E = gel(fa, 2);
     627         329 :       long k, lF = lg(F);
     628         329 :       if (lF == 2 && lP == 2)
     629             :       {
     630         336 :         if (isint1(gel(E,1)))
     631             :         { /* simple subspace */
     632         168 :           swap(gel(V,first), gel(V,j));
     633         168 :           first++;
     634             :         }
     635             :         else
     636           0 :           set_avma(av);
     637             :       }
     638         161 :       else if (lF == 1) /* discard V[j] */
     639           7 :       { swap(gel(V,j), gel(V,lg(V)-1)); setlg(V, lg(V)-1); }
     640             :       else
     641             :       { /* can split Vj */
     642             :         GEN pows;
     643         154 :         long D = 1;
     644         616 :         for (k = 1; k < lF; k++)
     645             :         {
     646         462 :           long d = degpol(gel(F,k));
     647         462 :           if (d > D) D = d;
     648             :         }
     649             :         /* remove V[j] */
     650         154 :         swap(gel(V,j), gel(V,lg(V)-1)); setlg(V, lg(V)-1);
     651         154 :         pows = RgM_powers(TVj, minss((long)2*sqrt((double)D), D));
     652         616 :         for (k = 1; k < lF; k++)
     653             :         {
     654         462 :           GEN f = gel(F,k);
     655         462 :           GEN K = QM_ker_r( RgX_RgMV_eval(f, pows)) ; /* Ker f(TVj) */
     656         462 :           GEN p = vec_Q_primpart( RgM_mul(P, K) );
     657         462 :           vectrunc_append(V, Qevproj_init(p));
     658         462 :           if (lg(K) == 2 || isint1(gel(E,k)))
     659             :           { /* simple subspace */
     660         385 :             swap(gel(V,first), gel(V, lg(V)-1));
     661         385 :             first++;
     662             :           }
     663             :         }
     664         154 :         if (j < first) j = first;
     665             :       }
     666             :     }
     667         322 :     if (first >= lg(V)) {
     668         252 :       gen_sort_inplace(V, NULL, cmp_dim, NULL);
     669         252 :       return V;
     670             :     }
     671             :   }
     672           0 :   pari_err_BUG("subspaces not found");
     673           0 :   return NULL;
     674             : }
     675             : GEN
     676         259 : mssplit(GEN W, GEN H, long deglim)
     677             : {
     678         259 :   pari_sp av = avma;
     679         259 :   checkms(W);
     680         259 :   if (!msk_get_sign(W))
     681           0 :     pari_err_DOMAIN("mssplit","abs(sign)","!=",gen_1,gen_0);
     682         259 :   if (!H) H = msnew(W);
     683         259 :   H = Qevproj_init0(H);
     684         259 :   return gerepilecopy(av, mssplit_i(W,H,deglim));
     685             : }
     686             : 
     687             : /* proV = Qevproj_init of a Hecke simple subspace, return [ a_n, n <= B ] */
     688             : static GEN
     689         245 : msqexpansion_i(GEN W, GEN proV, ulong B)
     690             : {
     691         245 :   ulong p, N = ms_get_N(W), sqrtB;
     692         245 :   long i, d, k = msk_get_weight(W);
     693             :   forprime_t S;
     694         245 :   GEN T1=NULL, T2=NULL, TV=NULL, ch=NULL, v, dTiv, Tiv, diM, iM, L;
     695         245 :   switch(B)
     696             :   {
     697           0 :     case 0: return cgetg(1,t_VEC);
     698           0 :     case 1: return mkvec(gen_1);
     699             :   }
     700         245 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
     701         602 :   while ((p = u_forprime_next(&S)))
     702             :   {
     703             :     GEN T;
     704         357 :     if (N % p == 0) continue;
     705         266 :     if (T1 && T2)
     706             :     {
     707           0 :       T = RgM_add(T1,T2);
     708           0 :       T2 = NULL;
     709             :     }
     710             :     else
     711             :     {
     712         266 :       T2 = T1;
     713         266 :       T1 = T = mshecke(W, p, NULL);
     714             :     }
     715         266 :     TV = Qevproj_apply(T, proV); /* T | V */
     716         266 :     ch = QM_charpoly_ZX(TV);
     717         266 :     if (ZX_is_irred(ch)) break;
     718          21 :     ch = NULL;
     719             :   }
     720         245 :   if (!ch) pari_err_BUG("q-Expansion not found");
     721             :   /* T generates the Hecke algebra (acting on V) */
     722         245 :   d = degpol(ch);
     723         245 :   v = vec_ei(d, 1); /* take v = e_1 */
     724         245 :   Tiv = cgetg(d+1, t_MAT); /* Tiv[i] = T^(i-1)v */
     725         245 :   gel(Tiv, 1) = v;
     726         245 :   for (i = 2; i <= d; i++) gel(Tiv, i) = RgM_RgC_mul(TV, gel(Tiv,i-1));
     727         245 :   Tiv = Q_remove_denom(Tiv, &dTiv);
     728         245 :   iM = ZM_inv(Tiv, &diM);
     729         245 :   if (dTiv) diM = gdiv(diM, dTiv);
     730         245 :   L = const_vec(B,NULL);
     731         245 :   sqrtB = usqrt(B);
     732         245 :   gel(L,1) = d > 1? mkpolmod(gen_1,ch): gen_1;
     733        2471 :   for (p = 2; p <= B; p++)
     734             :   {
     735        2226 :     pari_sp av = avma;
     736             :     GEN T, u, Tv, ap, P;
     737             :     ulong m;
     738        2226 :     if (gel(L,p)) continue;  /* p not prime */
     739         819 :     T = mshecke(W, p, NULL);
     740         819 :     Tv = Qevproj_apply_vecei(T, proV, 1); /* Tp.v */
     741             :     /* Write Tp.v = \sum u_i T^i v */
     742         819 :     u = RgC_Rg_div(RgM_RgC_mul(iM, Tv), diM);
     743         819 :     ap = gerepilecopy(av, RgV_to_RgX(u, 0));
     744         819 :     if (d > 1)
     745         399 :       ap = mkpolmod(ap,ch);
     746             :     else
     747         420 :       ap = simplify_shallow(ap);
     748         819 :     gel(L,p) = ap;
     749         819 :     if (!(N % p))
     750             :     { /* p divides the level */
     751         147 :       ulong C = B/p;
     752         546 :       for (m=1; m<=C; m++)
     753         399 :         if (gel(L,m)) gel(L,m*p) = gmul(gel(L,m), ap);
     754         147 :       continue;
     755             :     }
     756         672 :     P = powuu(p,k-1);
     757         672 :     if (p <= sqrtB) {
     758         119 :       ulong pj, oldpj = 1;
     759         546 :       for (pj = p; pj <= B; oldpj=pj, pj *= p)
     760             :       {
     761         427 :         GEN apj = (pj==p)? ap
     762         427 :                          : gsub(gmul(ap,gel(L,oldpj)), gmul(P,gel(L,oldpj/p)));
     763         427 :         gel(L,pj) = apj;
     764        3136 :         for (m = B/pj; m > 1; m--)
     765        2709 :           if (gel(L,m) && m%p) gel(L,m*pj) = gmul(gel(L,m), apj);
     766             :       }
     767             :     } else {
     768         553 :       gel(L,p) = ap;
     769        1092 :       for (m = B/p; m > 1; m--)
     770         539 :         if (gel(L,m)) gel(L,m*p) = gmul(gel(L,m), ap);
     771             :     }
     772             :   }
     773         245 :   return L;
     774             : }
     775             : GEN
     776         245 : msqexpansion(GEN W, GEN proV, ulong B)
     777             : {
     778         245 :   pari_sp av = avma;
     779         245 :   checkms(W);
     780         245 :   proV = Qevproj_init0(proV);
     781         245 :   return gerepilecopy(av, msqexpansion_i(W,proV,B));
     782             : }
     783             : 
     784             : static GEN
     785         266 : Qevproj_apply0(GEN T, GEN pro)
     786             : {
     787         266 :   GEN iM = gel(pro,2), perm = gel(pro,4);
     788         266 :   return vec_Q_primpart(ZM_mul(iM, rowpermute(T,perm)));
     789             : }
     790             : /* T a ZC or ZM */
     791             : GEN
     792        4186 : Qevproj_down(GEN T, GEN pro)
     793             : {
     794        4186 :   GEN iM = gel(pro,2), ciM = gel(pro,3), perm = gel(pro,4);
     795        4186 :   if (typ(T) == t_COL)
     796        4186 :     return RgC_Rg_div(ZM_ZC_mul(iM, vecpermute(T,perm)), ciM);
     797             :   else
     798           0 :     return RgM_Rg_div(ZM_mul(iM, rowpermute(T,perm)), ciM);
     799             : }
     800             : 
     801             : static GEN
     802         357 : Qevproj_star(GEN W, GEN H)
     803             : {
     804         357 :   long s = msk_get_sign(W);
     805         357 :   if (s)
     806             :   { /* project on +/- component */
     807         266 :     GEN A = RgM_mul(msk_get_star(W), H);
     808         266 :     A = (s > 0)? gadd(A, H): gsub(A, H);
     809             :     /* Im(star + sign) = Ker(star - sign) */
     810         266 :     H = QM_image(A);
     811         266 :     H = Qevproj_apply0(H, msk_get_starproj(W));
     812             :   }
     813         357 :   return H;
     814             : }
     815             : 
     816             : static GEN
     817        2625 : Tp_matrices(ulong p)
     818             : {
     819        2625 :   GEN v = cgetg(p+2, t_VEC);
     820             :   ulong i;
     821        2625 :   for (i = 1; i <= p; i++) gel(v,i) = mat2(1, i-1, 0, p);
     822        2625 :   gel(v,i) = mat2(p, 0, 0, 1);
     823        2625 :   return v;
     824             : }
     825             : static GEN
     826         973 : Up_matrices(ulong p)
     827             : {
     828         973 :   GEN v = cgetg(p+1, t_VEC);
     829             :   ulong i;
     830         973 :   for (i = 1; i <= p; i++) gel(v,i) = mat2(1, i-1, 0, p);
     831         973 :   return v;
     832             : }
     833             : 
     834             : /* M = N/p. Classes of Gamma_0(M) / Gamma_O(N) when p | M */
     835             : static GEN
     836         168 : NP_matrices(ulong M, ulong p)
     837             : {
     838         168 :   GEN v = cgetg(p+1, t_VEC);
     839             :   ulong i;
     840         168 :   for (i = 1; i <= p; i++) gel(v,i) = mat2(1, 0, (i-1)*M, 1);
     841         168 :   return v;
     842             : }
     843             : /* M = N/p. Extra class of Gamma_0(M) / Gamma_O(N) when p \nmid M */
     844             : static GEN
     845          84 : NP_matrix_extra(ulong M, ulong p)
     846             : {
     847          84 :   long w,z, d = cbezout(p, -M, &w, &z);
     848          84 :   if (d != 1) return NULL;
     849          84 :   return mat2(w,z,M,p);
     850             : }
     851             : static GEN
     852          98 : WQ_matrix(long N, long Q)
     853             : {
     854          98 :   long w,z, d = cbezout(Q, N/Q, &w, &z);
     855          98 :   if (d != 1) return NULL;
     856          98 :   return mat2(Q,1,-N*z,Q*w);
     857             : }
     858             : 
     859             : GEN
     860         280 : msnew(GEN W)
     861             : {
     862         280 :   pari_sp av = avma;
     863         280 :   GEN S = mscuspidal(W, 0);
     864         280 :   ulong N = ms_get_N(W);
     865         280 :   long s = msk_get_sign(W), k = msk_get_weight(W);
     866         280 :   if (N > 1 && (!uisprime(N) || (k == 12 || k > 14)))
     867             :   {
     868         105 :     GEN p1N = ms_get_p1N(W), P = gel(p1N_get_fa(p1N), 1);
     869         105 :     long i, nP = lg(P)-1;
     870         105 :     GEN v = cgetg(2*nP + 1, t_COL);
     871         105 :     S = gel(S,1); /* Q basis */
     872         273 :     for (i = 1; i <= nP; i++)
     873             :     {
     874         168 :       pari_sp av = avma, av2;
     875         168 :       long M = N/P[i];
     876         168 :       GEN T1,Td, Wi = mskinit(M, k, s);
     877         168 :       GEN v1 = NP_matrices(M, P[i]);
     878         168 :       GEN vd = Up_matrices(P[i]);
     879             :       /* p^2 \nmid N */
     880         168 :       if (M % P[i])
     881             :       {
     882          84 :         v1 = shallowconcat(v1, mkvec(NP_matrix_extra(M,P[i])));
     883          84 :         vd = shallowconcat(vd, mkvec(WQ_matrix(N,P[i])));
     884             :       }
     885         168 :       T1 = getMorphism(W, Wi, v1);
     886         168 :       Td = getMorphism(W, Wi, vd);
     887         168 :       if (s)
     888             :       {
     889         154 :         T1 = Qevproj_apply2(T1, msk_get_starproj(W), msk_get_starproj(Wi));
     890         154 :         Td = Qevproj_apply2(Td, msk_get_starproj(W), msk_get_starproj(Wi));
     891             :       }
     892         168 :       av2 = avma;
     893         168 :       T1 = RgM_mul(T1,S);
     894         168 :       Td = RgM_mul(Td,S);  /* multiply by S = restrict to mscusp */
     895         168 :       gerepileallsp(av, av2, 2, &T1, &Td);
     896         168 :       gel(v,2*i-1) = T1;
     897         168 :       gel(v,2*i)   = Td;
     898             :     }
     899         105 :     S = ZM_mul(S, QM_ker_r(matconcat(v))); /* Snew */
     900         105 :     S = Qevproj_init(vec_Q_primpart(S));
     901             :   }
     902         280 :   return gerepilecopy(av, S);
     903             : }
     904             : 
     905             : /* Solve the Manin relations for a congruence subgroup \Gamma by constructing
     906             :  * a well-formed fundamental domain for the action of \Gamma on upper half
     907             :  * space. See
     908             :  * Pollack and Stevens, Overconvergent modular symbols and p-adic L-functions
     909             :  * Annales scientifiques de l'ENS 44, fascicule 1 (2011), 1-42
     910             :  * http://math.bu.edu/people/rpollack/Papers/Overconvergent_modular_symbols_and_padic_Lfunctions.pdf
     911             :  *
     912             :  * FIXME: Implemented for \Gamma = \Gamma_0(N) only. */
     913             : 
     914             : #if 0 /* Pollack-Stevens shift their paths so as to solve equations of the
     915             :          form f(z+1) - f(z) = g. We don't (to avoid mistakes) so we will
     916             :          have to solve eqs of the form f(z-1) - f(z) = g */
     917             : /* c = a/b; as a t_VECSMALL [a,b]; return c-1 as a t_VECSMALL */
     918             : static GEN
     919             : Shift_left_cusp(GEN c) { long a=c[1], b=c[2]; return mkvecsmall2(a - b, b); }
     920             : /* c = a/b; as a t_VECSMALL [a,b]; return c+1 as a t_VECSMALL */
     921             : static GEN
     922             : Shift_right_cusp(GEN c) { long a=c[1], b=c[2]; return mkvecsmall2(a + b, b); }
     923             : /*Input: path = [r,s] (thought of as a geodesic between these points)
     924             :  *Output: The path shifted by one to the left, i.e. [r-1,s-1] */
     925             : static GEN
     926             : Shift_left(GEN path)
     927             : {
     928             :   GEN r = gel(path,1), s = gel(path,2);
     929             :   return mkvec2(Shift_left_cusp(r), Shift_left_cusp(s)); }
     930             : /*Input: path = [r,s] (thought of as a geodesic between these points)
     931             :  *Output: The path shifted by one to the right, i.e. [r+1,s+1] */
     932             : GEN
     933             : Shift_right(GEN path)
     934             : {
     935             :   GEN r = gel(path,1), s = gel(path,2);
     936             :   return mkvec2(Shift_right_cusp(r), Shift_right_cusp(s)); }
     937             : #endif
     938             : 
     939             : /* linked lists */
     940             : typedef struct list_t { GEN data; struct list_t *next; } list_t;
     941             : static list_t *
     942       77889 : list_new(GEN x)
     943             : {
     944       77889 :   list_t *L = (list_t*)stack_malloc(sizeof(list_t));
     945       77889 :   L->data = x;
     946       77889 :   L->next = NULL; return L;
     947             : }
     948             : static void
     949       76202 : list_insert(list_t *L, GEN x)
     950             : {
     951       76202 :   list_t *l = list_new(x);
     952       76202 :   l->next = L->next;
     953       76202 :   L->next = l;
     954       76202 : }
     955             : 
     956             : /*Input: N > 1, p1N = P^1(Z/NZ)
     957             :  *Output: a connected fundamental domain for the action of \Gamma_0(N) on
     958             :  *  upper half space.  When \Gamma_0(N) is torsion free, the domain has the
     959             :  *  property that all of its vertices are cusps.  When \Gamma_0(N) has
     960             :  *  three-torsion, 2 extra triangles need to be added.
     961             :  *
     962             :  * The domain is constructed by beginning with the triangle with vertices 0,1
     963             :  * and oo.  Each adjacent triangle is successively tested to see if it contains
     964             :  * points not \Gamma_0(N) equivalent to some point in our region.  If a
     965             :  * triangle contains new points, it is added to the region.  This process is
     966             :  * continued until the region can no longer be extended (and still be a
     967             :  * fundamental domain) by added an adjacent triangle.  The list of cusps
     968             :  * between 0 and 1 are then returned
     969             :  *
     970             :  * Precisely, the function returns a list such that the elements of the list
     971             :  * with odd index are the cusps in increasing order.  The even elements of the
     972             :  * list are either an "x" or a "t".  A "t" represents that there is an element
     973             :  * of order three such that its fixed point is in the triangle directly
     974             :  * adjacent to the our region with vertices given by the cusp before and after
     975             :  * the "t".  The "x" represents that this is not the case. */
     976             : enum { type_X, type_DO /* ? */, type_T };
     977             : static GEN
     978        1687 : form_list_of_cusps(ulong N, GEN p1N)
     979             : {
     980        1687 :   pari_sp av = avma;
     981        1687 :   long i, position, nbC = 2;
     982             :   GEN v, L;
     983             :   list_t *C, *c;
     984             :   /* Let t be the index of a class in PSL2(Z) / \Gamma in our fixed enumeration
     985             :    * v[t] != 0 iff it is the class of z tau^r for z a previous alpha_i
     986             :    * or beta_i.
     987             :    * For \Gamma = \Gamma_0(N), the enumeration is given by p1_index.
     988             :    * We write cl(gamma) = the class of gamma mod \Gamma */
     989        1687 :   v = const_vecsmall(p1_size(p1N), 0);
     990        1687 :   i = p1_index( 0, 1, p1N); v[i] = 1;
     991        1687 :   i = p1_index( 1,-1, p1N); v[i] = 2;
     992        1687 :   i = p1_index(-1, 0, p1N); v[i] = 3;
     993             :   /* the value is unused [debugging]: what matters is whether it is != 0 */
     994        1687 :   position = 4;
     995             :   /* at this point, Fund = R, v contains the classes of Id, tau, tau^2 */
     996             : 
     997        1687 :   C  = list_new(mkvecsmall3(0,1, type_X));
     998        1687 :   list_insert(C, mkvecsmall3(1,1,type_DO));
     999             :   /* C is a list of triples[a,b,t], where c = a/b is a cusp, and t is the type
    1000             :    * of the path between c and the PREVIOUS cusp in the list, coded as
    1001             :    *   type_DO = "?", type_X = "x", type_T = "t"
    1002             :    * Initially, C = [0/1,"?",1/1]; */
    1003             : 
    1004             :   /* loop through the current set of cusps C and check to see if more cusps
    1005             :    * should be added */
    1006             :   for (;;)
    1007        7567 :   {
    1008        9254 :     int done = 1;
    1009      370076 :     for (c = C; c; c = c->next)
    1010             :     {
    1011             :       GEN cusp1, cusp2, gam;
    1012             :       long pos, b1, b2, b;
    1013             : 
    1014      370076 :       if (!c->next) break;
    1015      360822 :       cusp1 = c->data; /* = a1/b1 */
    1016      360822 :       cusp2 = (c->next)->data; /* = a2/b2 */
    1017      360822 :       if (cusp2[3] != type_DO) continue;
    1018             : 
    1019             :       /* gam (oo -> 0) = (cusp2 -> cusp1), gam in PSL2(Z) */
    1020      150717 :       gam = path_to_zm(mkpath(cusp2, cusp1)); /* = [a2,a1;b2,b1] */
    1021             :       /* we have normalized the cusp representation so that a1 b2 - a2 b1 = 1 */
    1022      150717 :       b1 = coeff(gam,2,1); b2 = coeff(gam,2,2);
    1023             :       /* gam.1  = (a1 + a2) / (b1 + b2) */
    1024      150717 :       b = b1 + b2;
    1025             :       /* Determine whether the adjacent triangle *below* (cusp1->cusp2)
    1026             :        * should be added */
    1027      150717 :       pos = p1_index(b1,b2, p1N); /* did we see cl(gam) before ? */
    1028      150717 :       if (v[pos])
    1029       75474 :         cusp2[3] = type_X; /* NO */
    1030             :       else
    1031             :       { /* YES */
    1032             :         ulong B1, B2;
    1033       75243 :         v[pos] = position;
    1034       75243 :         i = p1_index(-(b1+b2), b1, p1N); v[i] = position+1;
    1035       75243 :         i = p1_index(b2, -(b1+b2), p1N); v[i] = position+2;
    1036             :         /* add cl(gam), cl(gam*TAU), cl(gam*TAU^2) to v */
    1037       75243 :         position += 3;
    1038             :         /* gam tau gam^(-1) in \Gamma ? */
    1039       75243 :         B1 = smodss(b1, N);
    1040       75243 :         B2 = smodss(b2, N);
    1041       75243 :         if ((Fl_sqr(B2,N) + Fl_sqr(B1,N) + Fl_mul(B1,B2,N)) % N == 0)
    1042         728 :           cusp2[3] = type_T;
    1043             :         else
    1044             :         {
    1045       74515 :           long a1 = coeff(gam, 1,1), a2 = coeff(gam, 1,2);
    1046       74515 :           long a = a1 + a2; /* gcd(a,b) = 1 */
    1047       74515 :           list_insert(c, mkvecsmall3(a,b,type_DO));
    1048       74515 :           c = c->next;
    1049       74515 :           nbC++;
    1050       74515 :           done = 0;
    1051             :         }
    1052             :       }
    1053             :     }
    1054        9254 :     if (done) break;
    1055             :   }
    1056        1687 :   L = cgetg(nbC+1, t_VEC); i = 1;
    1057        1687 :   for (c = C; c; c = c->next) gel(L,i++) = c->data;
    1058        1687 :   return gerepilecopy(av, L);
    1059             : }
    1060             : 
    1061             : /* W an msN. M in PSL2(Z). Return index of M in P1^(Z/NZ) = Gamma0(N) \ PSL2(Z),
    1062             :  * and M0 in Gamma_0(N) such that M = M0 * M', where M' = chosen
    1063             :  * section( PSL2(Z) -> P1^(Z/NZ) ). */
    1064             : static GEN
    1065      499023 : Gamma0N_decompose(GEN W, GEN M, long *index)
    1066             : {
    1067      499023 :   GEN p1N = msN_get_p1N(W), W3 = gel(W,3), section = msN_get_section(W);
    1068             :   GEN A;
    1069      499023 :   ulong N = p1N_get_N(p1N);
    1070      499023 :   ulong c = umodiu(gcoeff(M,2,1), N);
    1071      499023 :   ulong d = umodiu(gcoeff(M,2,2), N);
    1072      499023 :   long s, ind = p1_index(c, d, p1N); /* as an elt of P1(Z/NZ) */
    1073      499023 :   *index = W3[ind]; /* as an elt of F, E2, ... */
    1074      499023 :   M = ZM_zm_mul(M, sl2_inv(gel(section,ind)));
    1075             :   /* normalize mod +/-Id */
    1076      499023 :   A = gcoeff(M,1,1);
    1077      499023 :   s = signe(A);
    1078      499023 :   if (s < 0)
    1079      237363 :     M = ZM_neg(M);
    1080      261660 :   else if (!s)
    1081             :   {
    1082         322 :     GEN C = gcoeff(M,2,1);
    1083         322 :     if (signe(C) < 0) M = ZM_neg(M);
    1084             :   }
    1085      499023 :   return M;
    1086             : }
    1087             : /* W an msN; as above for a path. Return [[ind], M] */
    1088             : static GEN
    1089      159152 : path_Gamma0N_decompose(GEN W, GEN path)
    1090             : {
    1091      159152 :   GEN p1N = msN_get_p1N(W);
    1092      159152 :   GEN p1index_to_ind = gel(W,3);
    1093      159152 :   GEN section = msN_get_section(W);
    1094      159152 :   GEN M = path_to_zm(path);
    1095      159152 :   long p1index = p1_index(cc(M), dd(M), p1N);
    1096      159152 :   long ind = p1index_to_ind[p1index];
    1097      159152 :   GEN M0 = ZM_zm_mul(mat2_to_ZM(M), sl2_inv(gel(section,p1index)));
    1098      159152 :   return mkvec2(mkvecsmall(ind), M0);
    1099             : }
    1100             : 
    1101             : /*Form generators of H_1(X_0(N),{cusps},Z)
    1102             : *
    1103             : *Input: N = integer > 1, p1N = P^1(Z/NZ)
    1104             : *Output: [cusp_list,E,F,T2,T3,E1] where
    1105             : *  cusps_list = list of cusps describing fundamental domain of
    1106             : *    \Gamma_0(N).
    1107             : *  E = list of paths in the boundary of the fundamental domains and oriented
    1108             : *    clockwise such that they do not contain a point
    1109             : *    fixed by an element of order 2 and they are not an edge of a
    1110             : *    triangle containing a fixed point of an element of order 3
    1111             : *  F = list of paths in the interior of the domain with each
    1112             : *    orientation appearing separately
    1113             : * T2 = list of paths in the boundary of domain containing a point fixed
    1114             : *    by an element of order 2 (oriented clockwise)
    1115             : * T3 = list of paths in the boundard of domain which are the edges of
    1116             : *    some triangle containing a fixed point of a matrix of order 3 (both
    1117             : *    orientations appear)
    1118             : * E1 = a sublist of E such that every path in E is \Gamma_0(N)-equivalent to
    1119             : *    either an element of E1 or the flip (reversed orientation) of an element
    1120             : *    of E1.
    1121             : * (Elements of T2 are \Gamma_0(N)-equivalent to their own flip.)
    1122             : *
    1123             : * sec = a list from 1..#p1N of matrices describing a section of the map
    1124             : *   SL_2(Z) to P^1(Z/NZ) given by [a,b;c,d]-->[c,d].
    1125             : *   Given our fixed enumeration of P^1(Z/NZ), the j-th element of the list
    1126             : *   represents the image of the j-th element of P^1(Z/NZ) under the section. */
    1127             : 
    1128             : /* insert path in set T */
    1129             : static void
    1130      229334 : set_insert(hashtable *T, GEN path)
    1131      229334 : { hash_insert(T, path,  (void*)(T->nb + 1)); }
    1132             : 
    1133             : static GEN
    1134       15183 : hash_to_vec(hashtable *h)
    1135             : {
    1136       15183 :   GEN v = cgetg(h->nb + 1, t_VEC);
    1137             :   ulong i;
    1138     1941604 :   for (i = 0; i < h->len; i++)
    1139             :   {
    1140     1926421 :     hashentry *e = h->table[i];
    1141     4232746 :     while (e)
    1142             :     {
    1143      379904 :       GEN key = (GEN)e->key;
    1144      379904 :       long index = (long)e->val;
    1145      379904 :       gel(v, index) = key;
    1146      379904 :       e = e->next;
    1147             :     }
    1148             :   }
    1149       15183 :   return v;
    1150             : }
    1151             : 
    1152             : static long
    1153      117054 : path_to_p1_index(GEN path, GEN p1N)
    1154             : {
    1155      117054 :   GEN M = path_to_zm(path);
    1156      117054 :   return p1_index(cc(M), dd(M), p1N);
    1157             : }
    1158             : 
    1159             : /* Pollack-Stevens sets */
    1160             : typedef struct PS_sets_t {
    1161             :   hashtable *F, *T2, *T31, *T32, *E1, *E2;
    1162             :   GEN E2fromE1, stdE1;
    1163             : } PS_sets_t;
    1164             : 
    1165             : static hashtable *
    1166       13860 : set_init(long max)
    1167       13860 : { return hash_create(max, (ulong(*)(void*))&hash_GEN,
    1168             :                           (int(*)(void*,void*))&gidentical, 1); }
    1169             : /* T = E2fromE1[i] = [c, gamma] */
    1170             : static ulong
    1171     5427443 : E2fromE1_c(GEN T) { return itou(gel(T,1)); }
    1172             : static GEN
    1173      580237 : E2fromE1_Zgamma(GEN T) { return gel(T,2); }
    1174             : static GEN
    1175       39018 : E2fromE1_gamma(GEN T) { return gcoeff(gel(T,2),1,1); }
    1176             : 
    1177             : static void
    1178       78036 : insert_E(GEN path, PS_sets_t *S, GEN p1N)
    1179             : {
    1180       78036 :   GEN rev = vecreverse(path);
    1181       78036 :   long std = path_to_p1_index(rev, p1N);
    1182       78036 :   GEN v = gel(S->stdE1, std);
    1183       78036 :   if (v)
    1184             :   { /* [s, p1], where E1[s] is the path p1 = vecreverse(path) mod \Gamma */
    1185       39018 :     GEN gamma, p1 = gel(v,2);
    1186       39018 :     long r, s = itou(gel(v,1));
    1187             : 
    1188       39018 :     set_insert(S->E2, path);
    1189       39018 :     r = S->E2->nb;
    1190       39018 :     if (gel(S->E2fromE1, r) != gen_0) pari_err_BUG("insert_E");
    1191             : 
    1192       39018 :     gamma = gamma_equiv_matrix(rev, p1);
    1193             :     /* reverse(E2[r]) = gamma * E1[s] */
    1194       39018 :     gel(S->E2fromE1, r) = mkvec2(utoipos(s), to_famat_shallow(gamma,gen_m1));
    1195             :   }
    1196             :   else
    1197             :   {
    1198       39018 :     set_insert(S->E1, path);
    1199       39018 :     std = path_to_p1_index(path, p1N);
    1200       39018 :     gel(S->stdE1, std) = mkvec2(utoipos(S->E1->nb), path);
    1201             :   }
    1202       78036 : }
    1203             : 
    1204             : static GEN
    1205        6748 : cusp_infinity(void) { return mkvecsmall2(1,0); }
    1206             : 
    1207             : static void
    1208        1687 : form_E_F_T(ulong N, GEN p1N, GEN *pC, PS_sets_t *S)
    1209             : {
    1210        1687 :   GEN C, cusp_list = form_list_of_cusps(N, p1N);
    1211        1687 :   long nbgen = lg(cusp_list)-1, nbmanin = p1_size(p1N), r, s, i;
    1212             :   hashtable *F, *T2, *T31, *T32, *E1, *E2;
    1213             : 
    1214        1687 :   *pC = C = cgetg(nbgen+1, t_VEC);
    1215       79576 :   for (i = 1; i <= nbgen; i++)
    1216             :   {
    1217       77889 :     GEN c = gel(cusp_list,i);
    1218       77889 :     gel(C,i) = mkvecsmall2(c[1], c[2]);
    1219             :   }
    1220        1687 :   S->F  = F  = set_init(nbmanin);
    1221        1687 :   S->E1 = E1 = set_init(nbgen);
    1222        1687 :   S->E2 = E2 = set_init(nbgen);
    1223        1687 :   S->T2 = T2 = set_init(nbgen);
    1224        1687 :   S->T31 = T31 = set_init(nbgen);
    1225        1687 :   S->T32 = T32 = set_init(nbgen);
    1226             : 
    1227             :   /* T31 represents the three torsion paths going from left to right */
    1228             :   /* T32 represents the three torsion paths going from right to left */
    1229       77889 :   for (r = 1; r < nbgen; r++)
    1230             :   {
    1231       76202 :     GEN c2 = gel(cusp_list,r+1);
    1232       76202 :     if (c2[3] == type_T)
    1233             :     {
    1234         728 :       GEN c1 = gel(cusp_list,r), path = mkpath(c1,c2), path2 = vecreverse(path);
    1235         728 :       set_insert(T31, path);
    1236         728 :       set_insert(T32, path2);
    1237             :     }
    1238             :   }
    1239             : 
    1240             :   /* to record relations between E2 and E1 */
    1241        1687 :   S->E2fromE1 = zerovec(nbgen);
    1242        1687 :   S->stdE1 = const_vec(nbmanin, NULL);
    1243             : 
    1244             :   /* Assumption later: path [oo,0] is E1[1], path [1,oo] is E2[1] */
    1245             :   {
    1246        1687 :     GEN oo = cusp_infinity();
    1247        1687 :     GEN p1 = mkpath(oo, mkvecsmall2(0,1)); /* [oo, 0] */
    1248        1687 :     GEN p2 = mkpath(mkvecsmall2(1,1), oo); /* [1, oo] */
    1249        1687 :     insert_E(p1, S, p1N);
    1250        1687 :     insert_E(p2, S, p1N);
    1251             :   }
    1252             : 
    1253       77889 :   for (r = 1; r < nbgen; r++)
    1254             :   {
    1255       76202 :     GEN c1 = gel(cusp_list,r);
    1256    18063864 :     for (s = r+1; s <= nbgen; s++)
    1257             :     {
    1258    17987662 :       pari_sp av = avma;
    1259    17987662 :       GEN c2 = gel(cusp_list,s), path;
    1260    17987662 :       GEN d = subii(mulss(c1[1],c2[2]), mulss(c1[2],c2[1]));
    1261    17987662 :       set_avma(av);
    1262    17987662 :       if (!is_pm1(d)) continue;
    1263             : 
    1264      150717 :       path = mkpath(c1,c2);
    1265      150717 :       if (r+1 == s)
    1266             :       {
    1267       76202 :         GEN w = path;
    1268       76202 :         ulong hash = T31->hash(w); /* T31, T32 use the same hash function */
    1269       76202 :         if (!hash_search2(T31, w, hash) && !hash_search2(T32, w, hash))
    1270             :         {
    1271       75474 :           if (gamma_equiv(path, vecreverse(path), N))
    1272         812 :             set_insert(T2, path);
    1273             :           else
    1274       74662 :             insert_E(path, S, p1N);
    1275             :         }
    1276             :       } else {
    1277       74515 :         set_insert(F, mkvec2(path, mkvecsmall2(r,s)));
    1278       74515 :         set_insert(F, mkvec2(vecreverse(path), mkvecsmall2(s,r)));
    1279             :       }
    1280             :     }
    1281             :   }
    1282        1687 :   setlg(S->E2fromE1, E2->nb+1);
    1283        1687 : }
    1284             : 
    1285             : /* v = \sum n_i g_i, g_i in Sl(2,Z), return \sum n_i g_i^(-1) */
    1286             : static GEN
    1287      846321 : ZSl2_star(GEN v)
    1288             : {
    1289             :   long i, l;
    1290             :   GEN w, G;
    1291      846321 :   if (typ(v) == t_INT) return v;
    1292      846321 :   G = gel(v,1);
    1293      846321 :   w = cgetg_copy(G, &l);
    1294     2016707 :   for (i = 1; i < l; i++)
    1295             :   {
    1296     1170386 :     GEN g = gel(G,i);
    1297     1170386 :     if (typ(g) == t_MAT) g = SL2_inv(g);
    1298     1170386 :     gel(w,i) = g;
    1299             :   }
    1300      846321 :   return ZG_normalize(mkmat2(w, gel(v,2)));
    1301             : }
    1302             : 
    1303             : /* Input: h = set of unimodular paths, p1N = P^1(Z/NZ) = Gamma_0(N)\PSL2(Z)
    1304             :  * Output: Each path is converted to a matrix and then an element of P^1(Z/NZ)
    1305             :  * Append the matrix to W[12], append the index that represents
    1306             :  * these elements of P^1 (the classes mod Gamma_0(N) via our fixed
    1307             :  * enumeration to W[2]. */
    1308             : static void
    1309       10122 : paths_decompose(GEN W, hashtable *h, int flag)
    1310             : {
    1311       10122 :   GEN p1N = ms_get_p1N(W), section = ms_get_section(W);
    1312       10122 :   GEN v = hash_to_vec(h);
    1313       10122 :   long i, l = lg(v);
    1314      239456 :   for (i = 1; i < l; i++)
    1315             :   {
    1316      229334 :     GEN e = gel(v,i);
    1317      229334 :     GEN M = path_to_zm(flag? gel(e,1): e);
    1318      229334 :     long index = p1_index(cc(M), dd(M), p1N);
    1319      229334 :     vecsmalltrunc_append(gel(W,2), index);
    1320      229334 :     gel(section, index) = M;
    1321             :   }
    1322       10122 : }
    1323             : static void
    1324        1687 : fill_W2_W12(GEN W, PS_sets_t *S)
    1325             : {
    1326        1687 :   GEN p1N = msN_get_p1N(W);
    1327        1687 :   long n = p1_size(p1N);
    1328        1687 :   gel(W, 2) = vecsmalltrunc_init(n+1);
    1329        1687 :   gel(W,12) = cgetg(n+1, t_VEC);
    1330             :   /* F contains [path, [index cusp1, index cusp2]]. Others contain paths only */
    1331        1687 :   paths_decompose(W, S->F, 1);
    1332        1687 :   paths_decompose(W, S->E2, 0);
    1333        1687 :   paths_decompose(W, S->T32, 0);
    1334        1687 :   paths_decompose(W, S->E1, 0);
    1335        1687 :   paths_decompose(W, S->T2, 0);
    1336        1687 :   paths_decompose(W, S->T31, 0);
    1337        1687 : }
    1338             : 
    1339             : /* x t_VECSMALL, corresponds to a map x(i) = j, where 1 <= j <= max for all i
    1340             :  * Return y s.t. y[j] = i or 0 (not in image) */
    1341             : static GEN
    1342        3374 : reverse_list(GEN x, long max)
    1343             : {
    1344        3374 :   GEN y = const_vecsmall(max, 0);
    1345        3374 :   long r, lx = lg(x);
    1346        3374 :   for (r = 1; r < lx; r++) y[ x[r] ] = r;
    1347        3374 :   return y;
    1348             : }
    1349             : 
    1350             : /* go from C[a] to C[b]; return the indices of paths
    1351             :  * E.g. if a < b
    1352             :  *   (C[a]->C[a+1], C[a+1]->C[a+2], ... C[b-1]->C[b])
    1353             :  * (else reverse direction)
    1354             :  * = b - a paths */
    1355             : static GEN
    1356      145152 : F_indices(GEN W, long a, long b)
    1357             : {
    1358      145152 :   GEN v = cgetg(labs(b-a) + 1, t_VEC);
    1359      145152 :   long s, k = 1;
    1360      145152 :   if (a < b) {
    1361       72576 :     GEN index_forward = gel(W,13);
    1362       72576 :     for (s = a; s < b; s++) gel(v,k++) = gel(index_forward,s);
    1363             :   } else {
    1364       72576 :     GEN index_backward = gel(W,14);
    1365       72576 :     for (s = a; s > b; s--) gel(v,k++) = gel(index_backward,s);
    1366             :   }
    1367      145152 :   return v;
    1368             : }
    1369             : /* go from C[a] to C[b] via oo; return the indices of paths
    1370             :  * E.g. if a < b
    1371             :  *   (C[a]->C[a-1], ... C[2]->C[1],
    1372             :  *    C[1]->oo, oo-> C[end],
    1373             :  *    C[end]->C[end-1], ... C[b+1]->C[b])
    1374             :  *  a-1 + 2 + end-(b+1)+1 = end - b + a + 1 paths  */
    1375             : static GEN
    1376        3878 : F_indices_oo(GEN W, long end, long a, long b)
    1377             : {
    1378        3878 :   GEN index_oo = gel(W,15);
    1379        3878 :   GEN v = cgetg(end-labs(b-a)+1 + 1, t_VEC);
    1380        3878 :   long s, k = 1;
    1381             : 
    1382        3878 :   if (a < b) {
    1383        1939 :     GEN index_backward = gel(W,14);
    1384        1939 :     for (s = a; s > 1; s--) gel(v,k++) = gel(index_backward,s);
    1385        1939 :     gel(v,k++) = gel(index_backward,1); /* C[1] -> oo */
    1386        1939 :     gel(v,k++) = gel(index_oo,2); /* oo -> C[end] */
    1387        1939 :     for (s = end; s > b; s--) gel(v,k++) = gel(index_backward,s);
    1388             :   } else {
    1389        1939 :     GEN index_forward = gel(W,13);
    1390        1939 :     for (s = a; s < end; s++) gel(v,k++) = gel(index_forward,s);
    1391        1939 :     gel(v,k++) = gel(index_forward,end); /* C[end] -> oo */
    1392        1939 :     gel(v,k++) = gel(index_oo,1); /* oo -> C[1] */
    1393        1939 :     for (s = 1; s < b; s++) gel(v,k++) = gel(index_forward,s);
    1394             :   }
    1395        3878 :   return v;
    1396             : }
    1397             : /* index of oo -> C[1], oo -> C[end] */
    1398             : static GEN
    1399        1687 : indices_oo(GEN W, GEN C)
    1400             : {
    1401        1687 :   long end = lg(C)-1;
    1402        1687 :   GEN w, v = cgetg(2+1, t_VEC), oo = cusp_infinity();
    1403        1687 :   w = mkpath(oo, gel(C,1)); /* oo -> C[1]=0 */
    1404        1687 :   gel(v,1) = path_Gamma0N_decompose(W, w);
    1405        1687 :   w = mkpath(oo, gel(C,end)); /* oo -> C[end]=1 */
    1406        1687 :   gel(v,2) = path_Gamma0N_decompose(W, w);
    1407        1687 :   return v;
    1408             : }
    1409             : 
    1410             : /* index of C[1]->C[2], C[2]->C[3], ... C[end-1]->C[end], C[end]->oo
    1411             :  * Recall that C[1] = 0, C[end] = 1 */
    1412             : static GEN
    1413        1687 : indices_forward(GEN W, GEN C)
    1414             : {
    1415        1687 :   long s, k = 1, end = lg(C)-1;
    1416        1687 :   GEN v = cgetg(end+1, t_VEC);
    1417       79576 :   for (s = 1; s <= end; s++)
    1418             :   {
    1419       77889 :     GEN w = mkpath(gel(C,s), s == end? cusp_infinity(): gel(C,s+1));
    1420       77889 :     gel(v,k++) = path_Gamma0N_decompose(W, w);
    1421             :   }
    1422        1687 :   return v;
    1423             : }
    1424             : /* index of C[1]->oo, C[2]->C[1], ... C[end]->C[end-1] */
    1425             : static GEN
    1426        1687 : indices_backward(GEN W, GEN C)
    1427             : {
    1428        1687 :   long s, k = 1, end = lg(C)-1;
    1429        1687 :   GEN v = cgetg(end+1, t_VEC);
    1430       79576 :   for (s = 1; s <= end; s++)
    1431             :   {
    1432       77889 :     GEN w = mkpath(gel(C,s), s == 1? cusp_infinity(): gel(C,s-1));
    1433       77889 :     gel(v,k++) = path_Gamma0N_decompose(W, w);
    1434             :   }
    1435        1687 :   return v;
    1436             : }
    1437             : 
    1438             : /*[0,-1;1,-1]*/
    1439             : static GEN
    1440        1729 : mkTAU()
    1441        1729 : { return mkmat22(gen_0,gen_m1, gen_1,gen_m1); }
    1442             : /* S */
    1443             : static GEN
    1444          42 : mkS()
    1445          42 : { return mkmat22(gen_0,gen_1, gen_m1,gen_0); }
    1446             : /* N = integer > 1. Returns data describing Delta_0 = Z[P^1(Q)]_0 seen as
    1447             :  * a Gamma_0(N) - module. */
    1448             : static GEN
    1449        1708 : msinit_N(ulong N)
    1450             : {
    1451             :   GEN p1N, C, vecF, vecT2, vecT31, TAU, W, W2, singlerel, annT2, annT31;
    1452             :   GEN F_index;
    1453             :   ulong r, s, width;
    1454             :   long nball, nbgen, nbp1N;
    1455             :   hashtable *F, *T2, *T31, *T32, *E1, *E2;
    1456             :   PS_sets_t S;
    1457             : 
    1458        1708 :   W = zerovec(16);
    1459        1708 :   gel(W,1) = p1N = create_p1mod(N);
    1460        1708 :   gel(W,16)= inithashcusps(p1N);
    1461        1708 :   TAU = mkTAU();
    1462        1708 :   if (N == 1)
    1463             :   {
    1464          21 :     gel(W,5) = mkvecsmall(1);
    1465             :     /* cheat because sets are not disjoint if N=1 */
    1466          21 :     gel(W,11) = mkvecsmall5(0, 0, 1, 1, 2);
    1467          21 :     gel(W,12) = mkvec(mat2(1,0,0,1));
    1468          21 :     gel(W,8) = mkvec( mkmat22(gen_1,gen_1, mkS(),gen_1) );
    1469          21 :     gel(W,9) = mkvec( mkmat2(mkcol3(gen_1,TAU,ZM_sqr(TAU)),
    1470             :                              mkcol3(gen_1,gen_1,gen_1)) );
    1471          21 :     return W;
    1472             :   }
    1473        1687 :   nbp1N = p1_size(p1N);
    1474        1687 :   form_E_F_T(N,p1N, &C, &S);
    1475        1687 :   E1  = S.E1;
    1476        1687 :   E2  = S.E2;
    1477        1687 :   T31 = S.T31;
    1478        1687 :   T32 = S.T32;
    1479        1687 :   F   = S.F;
    1480        1687 :   T2  = S.T2;
    1481        1687 :   nbgen = lg(C)-1;
    1482             : 
    1483             :  /* Put our paths in the order: F,E2,T32,E1,T2,T31
    1484             :   * W2[j] associates to the j-th element of this list its index in P1. */
    1485        1687 :   fill_W2_W12(W, &S);
    1486        1687 :   W2 = gel(W, 2);
    1487        1687 :   nball = lg(W2)-1;
    1488        1687 :   gel(W,3) = reverse_list(W2, nbp1N);
    1489        1687 :   gel(W,5) = vecslice(gel(W,2), F->nb + E2->nb + T32->nb + 1, nball);
    1490        1687 :   gel(W,4) = reverse_list(gel(W,5), nbp1N);
    1491        1687 :   gel(W,13) = indices_forward(W, C);
    1492        1687 :   gel(W,14) = indices_backward(W, C);
    1493        1687 :   gel(W,15) = indices_oo(W, C);
    1494        8435 :   gel(W,11) = mkvecsmall5(F->nb,
    1495        1687 :                           F->nb + E2->nb,
    1496        1687 :                           F->nb + E2->nb + T32->nb,
    1497        1687 :                           F->nb + E2->nb + T32->nb + E1->nb,
    1498        1687 :                           F->nb + E2->nb + T32->nb + E1->nb + T2->nb);
    1499             :   /* relations between T32 and T31 [not stored!]
    1500             :    * T32[i] = - T31[i] */
    1501             : 
    1502             :   /* relations of F */
    1503        1687 :   width = E1->nb + T2->nb + T31->nb;
    1504             :   /* F_index[r] = [index_1, ..., index_k], where index_i is the p1_index()
    1505             :    * of the elementary unimodular path between 2 consecutive cusps
    1506             :    * [in E1,E2,T2,T31 or T32] */
    1507        1687 :   F_index = cgetg(F->nb+1, t_VEC);
    1508        1687 :   vecF = hash_to_vec(F);
    1509      150717 :   for (r = 1; r <= F->nb; r++)
    1510             :   {
    1511      149030 :     GEN w = gel(gel(vecF,r), 2);
    1512      149030 :     long a = w[1], b = w[2], d = labs(b - a);
    1513             :     /* c1 = cusp_list[a],  c2 = cusp_list[b], ci != oo */
    1514      298060 :     gel(F_index,r) = (nbgen-d >= d-1)? F_indices(W, a,b)
    1515      149030 :                                      : F_indices_oo(W, lg(C)-1,a,b);
    1516             :   }
    1517             : 
    1518        1687 :   singlerel = cgetg(width+1, t_VEC);
    1519             :   /* form the single boundary relation */
    1520       40705 :   for (s = 1; s <= E2->nb; s++)
    1521             :   { /* reverse(E2[s]) = gamma * E1[c] */
    1522       39018 :     GEN T = gel(S.E2fromE1,s), gamma = E2fromE1_gamma(T);
    1523       39018 :     gel(singlerel, E2fromE1_c(T)) = mkmat22(gen_1,gen_1, gamma,gen_m1);
    1524             :   }
    1525        1687 :   for (r = E1->nb + 1; r <= width; r++) gel(singlerel, r) = gen_1;
    1526             : 
    1527             :   /* form the 2-torsion relations */
    1528        1687 :   annT2 = cgetg(T2->nb+1, t_VEC);
    1529        1687 :   vecT2 = hash_to_vec(T2);
    1530        2499 :   for (r = 1; r <= T2->nb; r++)
    1531             :   {
    1532         812 :     GEN w = gel(vecT2,r);
    1533         812 :     GEN gamma = gamma_equiv_matrix(vecreverse(w), w);
    1534         812 :     gel(annT2, r) = mkmat22(gen_1,gen_1, gamma,gen_1);
    1535             :   }
    1536             : 
    1537             :   /* form the 3-torsion relations */
    1538        1687 :   annT31 = cgetg(T31->nb+1, t_VEC);
    1539        1687 :   vecT31 = hash_to_vec(T31);
    1540        2415 :   for (r = 1; r <= T31->nb; r++)
    1541             :   {
    1542         728 :     GEN M = path_to_ZM( vecreverse(gel(vecT31,r)) );
    1543         728 :     GEN gamma = ZM_mul(ZM_mul(M, TAU), SL2_inv(M));
    1544         728 :     gel(annT31, r) = mkmat2(mkcol3(gen_1,gamma,ZM_sqr(gamma)),
    1545             :                             mkcol3(gen_1,gen_1,gen_1));
    1546             :   }
    1547        1687 :   gel(W,6) = F_index;
    1548        1687 :   gel(W,7) = S.E2fromE1;
    1549        1687 :   gel(W,8) = annT2;
    1550        1687 :   gel(W,9) = annT31;
    1551        1687 :   gel(W,10)= singlerel;
    1552        1687 :   return W;
    1553             : }
    1554             : static GEN
    1555         112 : cusp_to_P1Q(GEN c) { return c[2]? gdivgs(stoi(c[1]), c[2]): mkoo(); }
    1556             : static GEN
    1557          21 : mspathgens_i(GEN W)
    1558             : {
    1559             :   GEN R, r, g, section, gen, annT2, annT31;
    1560             :   long i, l;
    1561          21 :   checkms(W); W = get_msN(W);
    1562          21 :   section = msN_get_section(W);
    1563          21 :   gen = ms_get_genindex(W);
    1564          21 :   l = lg(gen);
    1565          21 :   g = cgetg(l,t_VEC);
    1566          77 :   for (i = 1; i < l; i++)
    1567             :   {
    1568          56 :     GEN p = gel(section,gen[i]);
    1569          56 :     gel(g,i) = mkvec2(cusp_to_P1Q(gel(p,1)), cusp_to_P1Q(gel(p,2)));
    1570             :   }
    1571          21 :   annT2 = msN_get_annT2(W);
    1572          21 :   annT31= msN_get_annT31(W);
    1573          21 :   if (ms_get_N(W) == 1)
    1574             :   {
    1575           7 :     R = cgetg(3, t_VEC);
    1576           7 :     gel(R,1) = mkvec( mkvec2(gel(annT2,1), gen_1) );
    1577           7 :     gel(R,2) = mkvec( mkvec2(gel(annT31,1), gen_1) );
    1578             :   }
    1579             :   else
    1580             :   {
    1581          14 :     GEN singlerel = msN_get_singlerel(W);
    1582          14 :     long j, nbT2 = lg(annT2)-1, nbT31 = lg(annT31)-1, nbE1 = ms_get_nbE1(W);
    1583          14 :     R = cgetg(nbT2+nbT31+2, t_VEC);
    1584          14 :     l = lg(singlerel);
    1585          14 :     r = cgetg(l, t_VEC);
    1586          42 :     for (i = 1; i <= nbE1; i++)
    1587          28 :       gel(r,i) = mkvec2(gel(singlerel, i), utoi(i));
    1588          35 :     for (; i < l; i++)
    1589          21 :       gel(r,i) = mkvec2(gen_1, utoi(i));
    1590          14 :     gel(R,1) = r; j = 2;
    1591          35 :     for (i = 1; i <= nbT2; i++,j++)
    1592          21 :       gel(R,j) = mkvec( mkvec2(gel(annT2,i), utoi(i + nbE1)) );
    1593          14 :     for (i = 1; i <= nbT31; i++,j++)
    1594           0 :       gel(R,j) = mkvec( mkvec2(gel(annT31,i), utoi(i + nbE1 + nbT2)) );
    1595             :   }
    1596          21 :   return mkvec2(g,R);
    1597             : }
    1598             : GEN
    1599          21 : mspathgens(GEN W)
    1600             : {
    1601          21 :   pari_sp av = avma;
    1602          21 :   return gerepilecopy(av, mspathgens_i(W));
    1603             : }
    1604             : /* Modular symbols in weight k: Hom_Gamma(Delta, Q[x,y]_{k-2}) */
    1605             : /* A symbol phi is represented by the {phi(g_i)}, {phi(g'_i)}, {phi(g''_i)}
    1606             :  * where the {g_i, g'_i, g''_i} are the Z[\Gamma]-generators of Delta,
    1607             :  * g_i corresponds to E1, g'_i to T2, g''_i to T31.
    1608             :  */
    1609             : 
    1610             : /* FIXME: export. T^1, ..., T^n */
    1611             : static GEN
    1612      702520 : RgX_powers(GEN T, long n)
    1613             : {
    1614      702520 :   GEN v = cgetg(n+1, t_VEC);
    1615             :   long i;
    1616      702520 :   gel(v, 1) = T;
    1617      702520 :   for (i = 1; i < n; i++) gel(v,i+1) = RgX_mul(gel(v,i), T);
    1618      702520 :   return v;
    1619             : }
    1620             : 
    1621             : /* g = [a,b;c,d] a mat2. Return (X^{k-2} | g)(X,Y)[X = 1]. */
    1622             : static GEN
    1623        2926 : voo_act_Gl2Q(GEN g, long k)
    1624             : {
    1625        2926 :   GEN mc = stoi(-coeff(g,2,1)), d = stoi(coeff(g,2,2));
    1626        2926 :   return RgX_to_RgC(gpowgs(deg1pol_shallow(mc, d, 0), k-2), k-1);
    1627             : }
    1628             : 
    1629             : struct m_act {
    1630             :   long dim, k, p;
    1631             :   GEN q;
    1632             :   GEN(*act)(struct m_act *,GEN);
    1633             : };
    1634             : 
    1635             : /* g = [a,b;c,d]. Return (P | g)(X,Y)[X = 1] = P(dX - cY, -b X + aY)[X = 1],
    1636             :  * for P = X^{k-2}, X^{k-3}Y, ..., Y^{k-2} */
    1637             : GEN
    1638      351260 : RgX_act_Gl2Q(GEN g, long k)
    1639             : {
    1640             :   GEN a,b,c,d, V1,V2,V;
    1641             :   long i;
    1642      351260 :   if (k == 2) return matid(1);
    1643      351260 :   a = gcoeff(g,1,1); b = gcoeff(g,1,2);
    1644      351260 :   c = gcoeff(g,2,1); d = gcoeff(g,2,2);
    1645      351260 :   V1 = RgX_powers(deg1pol_shallow(gneg(c), d, 0), k-2); /* d - c Y */
    1646      351260 :   V2 = RgX_powers(deg1pol_shallow(a, gneg(b), 0), k-2); /*-b + a Y */
    1647      351260 :   V = cgetg(k, t_MAT);
    1648      351260 :   gel(V,1)   = RgX_to_RgC(gel(V1, k-2), k-1);
    1649      820708 :   for (i = 1; i < k-2; i++)
    1650             :   {
    1651      469448 :     GEN v1 = gel(V1, k-2-i); /* (d-cY)^(k-2-i) */
    1652      469448 :     GEN v2 = gel(V2, i); /* (-b+aY)^i */
    1653      469448 :     gel(V,i+1) = RgX_to_RgC(RgX_mul(v1,v2), k-1);
    1654             :   }
    1655      351260 :   gel(V,k-1) = RgX_to_RgC(gel(V2, k-2), k-1);
    1656      351260 :   return V; /* V[i+1] = X^i | g */
    1657             : }
    1658             : /* z in Z[Gl2(Q)], return the matrix of z acting on V */
    1659             : static GEN
    1660      600824 : act_ZGl2Q(GEN z, struct m_act *T, hashtable *H)
    1661             : {
    1662      600824 :   GEN S = NULL, G, E;
    1663             :   pari_sp av;
    1664             :   long l, j;
    1665             :   /* paranoia: should not occur */
    1666      600824 :   if (typ(z) == t_INT) return scalarmat_shallow(z, T->dim);
    1667      600824 :   G = gel(z,1); l = lg(G);
    1668      600824 :   E = gel(z,2); av = avma;
    1669     1771210 :   for (j = 1; j < l; j++)
    1670             :   {
    1671     1170386 :     GEN M, g = gel(G,j), n = gel(E,j);
    1672     1170386 :     if (typ(g) == t_INT) /* = 1 */
    1673        3927 :       M = n; /* n*Id_dim */
    1674             :     else
    1675             :     { /*search in H succeeds because of preload*/
    1676     1166459 :       M = H? (GEN)hash_search(H,g)->val: T->act(T,g);
    1677     1166459 :       if (is_pm1(n))
    1678     1158934 :       { if (signe(n) < 0) M = RgM_neg(M); }
    1679             :       else
    1680        7525 :         M = RgM_Rg_mul(M, n);
    1681             :     }
    1682     1170386 :     if (!S) { S = M; continue; }
    1683      569562 :     S = gadd(S, M);
    1684      569562 :     if (gc_needed(av,1))
    1685             :     {
    1686           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"act_ZGl2Q, j = %ld",j);
    1687           0 :       S = gerepileupto(av, S);
    1688             :     }
    1689             :   }
    1690      600824 :   return gerepilecopy(av, S);
    1691             : }
    1692             : static GEN
    1693      351113 : _RgX_act_Gl2Q(struct m_act *S, GEN z) { return RgX_act_Gl2Q(z, S->k); }
    1694             : /* acting on (X^{k-2},...,Y^{k-2}) */
    1695             : GEN
    1696       60718 : RgX_act_ZGl2Q(GEN z, long k)
    1697             : {
    1698             :   struct m_act T;
    1699       60718 :   T.k = k;
    1700       60718 :   T.dim = k-1;
    1701       60718 :   T.act=&_RgX_act_Gl2Q;
    1702       60718 :   return act_ZGl2Q(z, &T, NULL);
    1703             : }
    1704             : 
    1705             : /* First pass, identify matrices in Sl_2 to convert to operators;
    1706             :  * insert operators in hashtable. This allows GC in act_ZGl2Q */
    1707             : static void
    1708     1070832 : hash_preload(GEN M, struct m_act *S, hashtable *H)
    1709             : {
    1710     1070832 :   if (typ(M) != t_INT)
    1711             :   {
    1712     1070832 :     ulong h = H->hash(M);
    1713     1070832 :     hashentry *e = hash_search2(H, M, h);
    1714     1070832 :     if (!e) hash_insert2(H, M, S->act(S,M), h);
    1715             :   }
    1716     1070832 : }
    1717             : /* z a sparse operator */
    1718             : static void
    1719      540092 : hash_vecpreload(GEN z, struct m_act *S, hashtable *H)
    1720             : {
    1721      540092 :   GEN G = gel(z,1);
    1722      540092 :   long i, l = lg(G);
    1723      540092 :   for (i = 1; i < l; i++) hash_preload(gel(G,i), S, H);
    1724      540092 : }
    1725             : static void
    1726       40817 : ZGl2QC_preload(struct m_act *S, GEN v, hashtable *H)
    1727             : {
    1728       40817 :   GEN val = gel(v,2);
    1729       40817 :   long i, l = lg(val);
    1730       40817 :   for (i = 1; i < l; i++) hash_vecpreload(gel(val,i), S, H);
    1731       40817 : }
    1732             : /* Given a sparse vector of elements in Z[G], convert it to a (sparse) vector
    1733             :  * of operators on V (given by t_MAT) */
    1734             : static void
    1735       40831 : ZGl2QC_to_act(struct m_act *S, GEN v, hashtable *H)
    1736             : {
    1737       40831 :   GEN val = gel(v,2);
    1738       40831 :   long i, l = lg(val);
    1739       40831 :   for (i = 1; i < l; i++) gel(val,i) = act_ZGl2Q(gel(val,i), S, H);
    1740       40831 : }
    1741             : 
    1742             : /* For all V[i] in Z[\Gamma], find the P such that  P . V[i]^* = 0;
    1743             :  * write P in basis X^{k-2}, ..., Y^{k-2} */
    1744             : static GEN
    1745        1246 : ZGV_tors(GEN V, long k)
    1746             : {
    1747        1246 :   long i, l = lg(V);
    1748        1246 :   GEN v = cgetg(l, t_VEC);
    1749        1750 :   for (i = 1; i < l; i++)
    1750             :   {
    1751         504 :     GEN a = ZSl2_star(gel(V,i));
    1752         504 :     gel(v,i) = ZM_ker(RgX_act_ZGl2Q(a,k));
    1753             :   }
    1754        1246 :   return v;
    1755             : }
    1756             : 
    1757             : static long
    1758    12400913 : set_from_index(GEN W11, long i)
    1759             : {
    1760    12400913 :   if (i <= W11[1]) return 1;
    1761    11196990 :   if (i <= W11[2]) return 2;
    1762     5814382 :   if (i <= W11[3]) return 3;
    1763     5805639 :   if (i <= W11[4]) return 4;
    1764       31367 :   if (i <= W11[5]) return 5;
    1765        8463 :   return 6;
    1766             : }
    1767             : 
    1768             : /* det M = 1 */
    1769             : static void
    1770     1536703 : treat_index(GEN W, GEN M, long index, GEN v)
    1771             : {
    1772     1536703 :   GEN W11 = gel(W,11);
    1773     1536703 :   long shift = W11[3]; /* #F + #E2 + T32 */
    1774     1536703 :   switch(set_from_index(W11, index))
    1775             :   {
    1776             :     case 1: /*F*/
    1777             :     {
    1778      251363 :       GEN F_index = gel(W,6), ind = gel(F_index, index);
    1779      251363 :       long j, l = lg(ind);
    1780     1289043 :       for (j = 1; j < l; j++)
    1781             :       {
    1782     1037680 :         GEN IND = gel(ind,j), M0 = gel(IND,2);
    1783     1037680 :         long index = mael(IND,1,1);
    1784     1037680 :         treat_index(W, ZM_mul(M,M0), index, v);
    1785             :       }
    1786      251363 :       break;
    1787             :     }
    1788             : 
    1789             :     case 2: /*E2, E2[r] + gamma * E1[s] = 0 */
    1790             :     {
    1791      580237 :       long r = index - W11[1];
    1792      580237 :       GEN z = gel(msN_get_E2fromE1(W), r);
    1793             : 
    1794      580237 :       index = E2fromE1_c(z);
    1795      580237 :       M = G_ZG_mul(M, E2fromE1_Zgamma(z)); /* M * (-gamma) */
    1796      580237 :       gel(v, index) = ZG_add(gel(v, index), M);
    1797      580237 :       break;
    1798             :     }
    1799             : 
    1800             :     case 3: /*T32, T32[i] = -T31[i] */
    1801             :     {
    1802        6006 :       long T3shift = W11[5] - W11[2]; /* #T32 + #E1 + #T2 */
    1803        6006 :       index += T3shift;
    1804        6006 :       index -= shift;
    1805        6006 :       gel(v, index) = ZG_add(gel(v, index), to_famat_shallow(M,gen_m1));
    1806        6006 :       break;
    1807             :     }
    1808             :     default: /*E1,T2,T31*/
    1809      699097 :       index -= shift;
    1810      699097 :       gel(v, index) = ZG_add(gel(v, index), to_famat_shallow(M,gen_1));
    1811      699097 :       break;
    1812             :   }
    1813     1536703 : }
    1814             : static void
    1815    10864210 : treat_index_trivial(GEN v, GEN W, long index)
    1816             : {
    1817    10864210 :   GEN W11 = gel(W,11);
    1818    10864210 :   long shift = W11[3]; /* #F + #E2 + T32 */
    1819    10864210 :   switch(set_from_index(W11, index))
    1820             :   {
    1821             :     case 1: /*F*/
    1822             :     {
    1823      952560 :       GEN F_index = gel(W,6), ind = gel(F_index, index);
    1824      952560 :       long j, l = lg(ind);
    1825    10327198 :       for (j = 1; j < l; j++)
    1826             :       {
    1827     9374638 :         GEN IND = gel(ind,j);
    1828     9374638 :         treat_index_trivial(v, W, mael(IND,1,1));
    1829             :       }
    1830      952560 :       break;
    1831             :     }
    1832             : 
    1833             :     case 2: /*E2, E2[r] + gamma * E1[s] = 0 */
    1834             :     {
    1835     4802371 :       long r = index - W11[1];
    1836     4802371 :       long s = E2fromE1_c(gel(msN_get_E2fromE1(W), r));
    1837     4802371 :       v[s]--;
    1838     4802371 :       break;
    1839             :     }
    1840             : 
    1841             :     case 3: case 5: case 6: /*T32,T2,T31*/
    1842       15463 :       break;
    1843             : 
    1844             :     case 4: /*E1*/
    1845     5093816 :       v[index-shift]++;
    1846     5093816 :       break;
    1847             :   }
    1848    10864210 : }
    1849             : 
    1850             : static GEN
    1851      178542 : M2_log(GEN W, GEN M)
    1852             : {
    1853      178542 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    1854      178542 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    1855             :   GEN  u, v, D, V;
    1856             :   long index, s;
    1857             : 
    1858      178542 :   W = get_msN(W);
    1859      178542 :   V = zerovec(ms_get_nbgen(W));
    1860             : 
    1861      178542 :   D = subii(mulii(a,d), mulii(b,c));
    1862      178542 :   s = signe(D);
    1863      178542 :   if (!s) return V;
    1864      177198 :   if (is_pm1(D))
    1865             :   { /* shortcut, no need to apply Manin's trick */
    1866       63532 :     if (s < 0) { b = negi(b); d = negi(d); }
    1867       63532 :     M = Gamma0N_decompose(W, mkmat22(a,b, c,d), &index);
    1868       63532 :     treat_index(W, M, index, V);
    1869             :   }
    1870             :   else
    1871             :   {
    1872             :     GEN U, B, P, Q, PQ, C1,C2;
    1873             :     long i, l;
    1874      113666 :     (void)bezout(a,c,&u,&v);
    1875      113666 :     B = addii(mulii(b,u), mulii(d,v));
    1876             :     /* [u,v;-c,a] [a,b; c,d] = [1,B; 0,D], i.e. M = U [1,B;0,D] */
    1877      113666 :     U = mkmat22(a,negi(v), c,u);
    1878             : 
    1879             :     /* {1/0 -> B/D} as \sum g_i, g_i unimodular paths */
    1880      113666 :     PQ = ZV_allpnqn( gboundcf(gdiv(B,D), 0) );
    1881      113666 :     P = gel(PQ,1); l = lg(P);
    1882      113666 :     Q = gel(PQ,2);
    1883      113666 :     C1 = gel(U,1);
    1884      549157 :     for (i = 1; i < l; i++, C1 = C2)
    1885             :     {
    1886             :       GEN M;
    1887      435491 :       C2 = ZM_ZC_mul(U, mkcol2(gel(P,i), gel(Q,i)));
    1888      435491 :       if (!odd(i)) C1 = ZC_neg(C1);
    1889      435491 :       M = Gamma0N_decompose(W, mkmat2(C1,C2), &index);
    1890      435491 :       treat_index(W, M, index, V);
    1891             :     }
    1892             :   }
    1893      177198 :   return V;
    1894             : }
    1895             : 
    1896             : /* express +oo->q=a/b in terms of the Z[G]-generators, trivial action */
    1897             : static void
    1898        7756 : Q_log_trivial(GEN v, GEN W, GEN q)
    1899             : {
    1900        7756 :   GEN Q, W3 = gel(W,3), p1N = msN_get_p1N(W);
    1901        7756 :   ulong c,d, N = p1N_get_N(p1N);
    1902             :   long i, lx;
    1903             : 
    1904        7756 :   Q = Q_log_init(N, q);
    1905        7756 :   lx = lg(Q);
    1906        7756 :   c = 0;
    1907       32284 :   for (i = 1; i < lx; i++, c = d)
    1908             :   {
    1909             :     long index;
    1910       24528 :     d = Q[i];
    1911       24528 :     if (c && !odd(i)) c = N - c;
    1912       24528 :     index = W3[ p1_index(c,d,p1N) ];
    1913       24528 :     treat_index_trivial(v, W, index);
    1914             :   }
    1915        7756 : }
    1916             : static void
    1917      587853 : M2_log_trivial(GEN V, GEN W, GEN M)
    1918             : {
    1919      587853 :   GEN p1N = gel(W,1), W3 = gel(W,3);
    1920      587853 :   ulong N = p1N_get_N(p1N);
    1921      587853 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    1922      587853 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    1923             :   GEN  u, v, D;
    1924             :   long index, s;
    1925             : 
    1926      587853 :   D = subii(mulii(a,d), mulii(b,c));
    1927      587853 :   s = signe(D);
    1928      593551 :   if (!s) return;
    1929      587846 :   if (is_pm1(D))
    1930             :   { /* shortcut, not need to apply Manin's trick */
    1931      215019 :     if (s < 0) d = negi(d);
    1932      215019 :     index = W3[ p1_index(umodiu(c,N),umodiu(d,N),p1N) ];
    1933      215019 :     treat_index_trivial(V, W, index);
    1934             :   }
    1935             :   else
    1936             :   {
    1937             :     GEN U, B, P, Q, PQ;
    1938             :     long i, l;
    1939      372827 :     if (!signe(c)) { Q_log_trivial(V,W,gdiv(b,d)); return; }
    1940      367136 :     (void)bezout(a,c,&u,&v);
    1941      367136 :     B = addii(mulii(b,u), mulii(d,v));
    1942             :     /* [u,v;-c,a] [a,b; c,d] = [1,B; 0,D], i.e. M = U [1,B;0,D] */
    1943      367136 :     U = mkvec2(c, u);
    1944             : 
    1945             :     /* {1/0 -> B/D} as \sum g_i, g_i unimodular paths */
    1946      367136 :     PQ = ZV_allpnqn( gboundcf(gdiv(B,D), 0) );
    1947      367136 :     P = gel(PQ,1); l = lg(P);
    1948      367136 :     Q = gel(PQ,2);
    1949     1617161 :     for (i = 1; i < l; i++, c = d)
    1950             :     {
    1951     1250025 :       d = addii(mulii(gel(U,1),gel(P,i)), mulii(gel(U,2),gel(Q,i)));
    1952     1250025 :       if (!odd(i)) c = negi(c);
    1953     1250025 :       index = W3[ p1_index(umodiu(c,N),umodiu(d,N),p1N) ];
    1954     1250025 :       treat_index_trivial(V, W, index);
    1955             :     }
    1956             :   }
    1957             : }
    1958             : 
    1959             : static GEN
    1960       16772 : cusp_to_ZC(GEN c)
    1961             : {
    1962       16772 :   switch(typ(c))
    1963             :   {
    1964             :     case t_INFINITY:
    1965          35 :       return mkcol2(gen_1,gen_0);
    1966             :     case t_INT:
    1967          91 :       return mkcol2(c,gen_1);
    1968             :     case t_FRAC:
    1969         140 :       return mkcol2(gel(c,1),gel(c,2));
    1970             :     case t_VECSMALL:
    1971       16506 :       return mkcol2(stoi(c[1]), stoi(c[2]));
    1972             :     default:
    1973           0 :       pari_err_TYPE("mspathlog",c);
    1974           0 :       return NULL;
    1975             :   }
    1976             : }
    1977             : static GEN
    1978        8386 : path2_to_M2(GEN p)
    1979        8386 : { return mkmat2(cusp_to_ZC(gel(p,1)), cusp_to_ZC(gel(p,2))); }
    1980             : static GEN
    1981       15981 : path_to_M2(GEN p)
    1982             : {
    1983       15981 :   if (lg(p) != 3) pari_err_TYPE("mspathlog",p);
    1984       15974 :   switch(typ(p))
    1985             :   {
    1986             :     case t_MAT:
    1987        9702 :       RgM_check_ZM(p,"mspathlog");
    1988        9702 :       break;
    1989             :     case t_VEC:
    1990        6272 :       p = path2_to_M2(p);
    1991        6272 :       break;
    1992           0 :     default: pari_err_TYPE("mspathlog",p);
    1993             :   }
    1994       15974 :   return p;
    1995             : }
    1996             : /* Expresses path p as \sum x_i g_i, where the g_i are our distinguished
    1997             :  * generators and x_i \in Z[\Gamma]. Returns [x_1,...,x_n] */
    1998             : GEN
    1999       12523 : mspathlog(GEN W, GEN p)
    2000             : {
    2001       12523 :   pari_sp av = avma;
    2002       12523 :   checkms(W);
    2003       12523 :   return gerepilecopy(av, M2_log(W, path_to_M2(p)));
    2004             : }
    2005             : 
    2006             : /** HECKE OPERATORS **/
    2007             : /* [a,b;c,d] * cusp */
    2008             : static GEN
    2009     1490734 : cusp_mul(long a, long b, long c, long d, GEN cusp)
    2010             : {
    2011     1490734 :   long x = cusp[1], y = cusp[2];
    2012     1490734 :   long A = a*x+b*y, B = c*x+d*y, u = cgcd(A,B);
    2013     1490734 :   if (u != 1) { A /= u; B /= u; }
    2014     1490734 :   return mkcol2s(A, B);
    2015             : }
    2016             : /* f in Gl2(Q), act on path (zm), return path_to_M2(f.path) */
    2017             : static GEN
    2018      745367 : Gl2Q_act_path(GEN f, GEN path)
    2019             : {
    2020      745367 :   long a = coeff(f,1,1), b = coeff(f,1,2);
    2021      745367 :   long c = coeff(f,2,1), d = coeff(f,2,2);
    2022      745367 :   GEN c1 = cusp_mul(a,b,c,d, gel(path,1));
    2023      745367 :   GEN c2 = cusp_mul(a,b,c,d, gel(path,2));
    2024      745367 :   return mkmat2(c1,c2);
    2025             : }
    2026             : 
    2027             : static GEN
    2028      152208 : init_act_trivial(GEN W) { return const_vecsmall(ms_get_nbE1(W), 0); }
    2029             : static GEN
    2030        3444 : mspathlog_trivial(GEN W, GEN p)
    2031             : {
    2032             :   GEN v;
    2033        3444 :   W = get_msN(W);
    2034        3444 :   v = init_act_trivial(W);
    2035        3444 :   M2_log_trivial(v, W, path_to_M2(p));
    2036        3437 :   return v;
    2037             : }
    2038             : 
    2039             : /* map from W1=Hom(Delta_0(N1),Q) -> W2=Hom(Delta_0(N2),Q), weight 2,
    2040             :  * trivial action. v a Gl2_Q or a t_VEC of Gl2_Q (\sum v[i] in Z[Gl2(Q)]).
    2041             :  * Return the matrix attached to the action of v. */
    2042             : static GEN
    2043        2744 : getMorphism_trivial(GEN WW1, GEN WW2, GEN v)
    2044             : {
    2045        2744 :   GEN T, section, gen, W1 = get_msN(WW1), W2 = get_msN(WW2);
    2046             :   long j, lv, d2;
    2047        2744 :   if (ms_get_N(W1) == 1) return cgetg(1,t_MAT);
    2048        2744 :   if (ms_get_N(W2) == 1) return zeromat(0, ms_get_nbE1(W1));
    2049        2744 :   section = msN_get_section(W2);
    2050        2744 :   gen = msN_get_genindex(W2);
    2051        2744 :   d2 = ms_get_nbE1(W2);
    2052        2744 :   T = cgetg(d2+1, t_MAT);
    2053        2744 :   lv = lg(v);
    2054      149443 :   for (j = 1; j <= d2; j++)
    2055             :   {
    2056      146699 :     GEN w = gel(section, gen[j]);
    2057      146699 :     GEN t = init_act_trivial(W1);
    2058      146699 :     pari_sp av = avma;
    2059             :     long l;
    2060      146699 :     for (l = 1; l < lv; l++) M2_log_trivial(t, W1, Gl2Q_act_path(gel(v,l), w));
    2061      146699 :     gel(T,j) = t; set_avma(av);
    2062             :   }
    2063        2744 :   return shallowtrans(zm_to_ZM(T));
    2064             : }
    2065             : 
    2066             : static GEN
    2067      166019 : RgV_sparse(GEN v, GEN *pind)
    2068             : {
    2069             :   long i, l, k;
    2070      166019 :   GEN w = cgetg_copy(v,&l), ind = cgetg(l, t_VECSMALL);
    2071    17145044 :   for (i = k = 1; i < l; i++)
    2072             :   {
    2073    16979025 :     GEN c = gel(v,i);
    2074    16979025 :     if (typ(c) == t_INT) continue;
    2075      785603 :     gel(w,k) = c; ind[k] = i; k++;
    2076             :   }
    2077      166019 :   setlg(w,k); setlg(ind,k);
    2078      166019 :   *pind = ind; return w;
    2079             : }
    2080             : 
    2081             : static int
    2082      163065 : mat2_isidentity(GEN M)
    2083             : {
    2084      163065 :   GEN A = gel(M,1), B = gel(M,2);
    2085      163065 :   return A[1] == 1 && A[2] == 0 && B[1] == 0 && B[2] == 1;
    2086             : }
    2087             : /* path a mat22/mat22s, return log(f.path)^* . f in sparse form */
    2088             : static GEN
    2089      166019 : M2_logf(GEN Wp, GEN path, GEN f)
    2090             : {
    2091      166019 :   pari_sp av = avma;
    2092             :   GEN ind, L;
    2093             :   long i, l;
    2094      166019 :   if (f)
    2095      160951 :     path = Gl2Q_act_path(f, path);
    2096        5068 :   else if (typ(gel(path,1)) == t_VECSMALL)
    2097        2114 :     path = path2_to_M2(path);
    2098      166019 :   L = M2_log(Wp, path);
    2099      166019 :   L = RgV_sparse(L,&ind); l = lg(L);
    2100      166019 :   for (i = 1; i < l; i++) gel(L,i) = ZSl2_star(gel(L,i));
    2101      166019 :   if (f) ZGC_G_mul_inplace(L, mat2_to_ZM(f));
    2102      166019 :   return gerepilecopy(av, mkvec2(ind,L));
    2103             : }
    2104             : 
    2105             : static hashtable *
    2106        3738 : Gl2act_cache(long dim) { return set_init(dim*10); }
    2107             : 
    2108             : /* f zm/ZM in Gl_2(Q), acts from the left on Delta, which is generated by
    2109             :  * (g_i) as Z[Gamma1]-module, and by (G_i) as Z[Gamma2]-module.
    2110             :  * We have f.G_j = \sum_i \lambda_{i,j} g_i,   \lambda_{i,j} in Z[Gamma1]
    2111             :  * For phi in Hom_Gamma1(D,V), g in D, phi | f is in Hom_Gamma2(D,V) and
    2112             :  *  (phi | f)(G_j) = phi(f.G_j) | f
    2113             :  *                 = phi( \sum_i \lambda_{i,j} g_i ) | f
    2114             :  *                 = \sum_i phi(g_i) | (\lambda_{i,j}^* f)
    2115             :  *                 = \sum_i phi(g_i) | \mu_{i,j}(f)
    2116             :  * More generally
    2117             :  *  (\sum_k (phi |v_k))(G_j) = \sum_i phi(g_i) | \Mu_{i,j}
    2118             :  * with \Mu_{i,j} = \sum_k \mu{i,j}(v_k)
    2119             :  * Return the \Mu_{i,j} matrix as vector of sparse columns of operators on V */
    2120             : static GEN
    2121        3262 : init_dual_act(GEN v, GEN W1, GEN W2, struct m_act *S)
    2122             : {
    2123        3262 :   GEN section = ms_get_section(W2), gen = ms_get_genindex(W2);
    2124             :   /* HACK: the actions we consider in dimension 1 are trivial and in
    2125             :    * characteristic != 2, 3 => torsion generators are 0
    2126             :    * [satisfy e.g. (1+gamma).g = 0 => \phi(g) | 1+gamma  = 0 => \phi(g) = 0 */
    2127        3262 :   long j, lv = lg(v), dim = S->dim == 1? ms_get_nbE1(W2): lg(gen)-1;
    2128        3262 :   GEN T = cgetg(dim+1, t_VEC);
    2129        3262 :   hashtable *H = Gl2act_cache(dim);
    2130       41139 :   for (j = 1; j <= dim; j++)
    2131             :   {
    2132       37877 :     pari_sp av = avma;
    2133       37877 :     GEN w = gel(section, gen[j]); /* path_to_zm( E1/T2/T3 element ) */
    2134       37877 :     GEN t = NULL;
    2135             :     long k;
    2136      200942 :     for (k = 1; k < lv; k++)
    2137             :     {
    2138      163065 :       GEN tk, f = gel(v,k);
    2139      163065 :       if (typ(gel(f,1)) != t_VECSMALL) f = ZM_to_zm(f);
    2140      163065 :       if (mat2_isidentity(f)) f = NULL;
    2141      163065 :       tk = M2_logf(W1, w, f); /* mu_{.,j}(v[k]) as sparse vector */
    2142      163065 :       t = t? ZGCs_add(t, tk): tk;
    2143             :     }
    2144       37877 :     gel(T,j) = gerepilecopy(av, t);
    2145             :   }
    2146       41139 :   for (j = 1; j <= dim; j++)
    2147             :   {
    2148       37877 :     ZGl2QC_preload(S, gel(T,j), H);
    2149       37877 :     ZGl2QC_to_act(S, gel(T,j), H);
    2150             :   }
    2151        3262 :   return T;
    2152             : }
    2153             : 
    2154             : /* modular symbol given by phi[j] = \phi(G_j)
    2155             :  * \sum L[i]*phi[i], L a sparse column of operators */
    2156             : static GEN
    2157      354354 : dense_act_col(GEN col, GEN phi)
    2158             : {
    2159      354354 :   GEN s = NULL, colind = gel(col,1), colval = gel(col,2);
    2160      354354 :   long i, l = lg(colind), lphi = lg(phi);
    2161     5630121 :   for (i = 1; i < l; i++)
    2162             :   {
    2163     5278490 :     long a = colind[i];
    2164             :     GEN t;
    2165     5278490 :     if (a >= lphi) break; /* happens if k=2: torsion generator t omitted */
    2166     5275767 :     t = gel(phi, a); /* phi(G_a) */
    2167     5275767 :     t = RgM_RgC_mul(gel(colval,i), t);
    2168     5275767 :     s = s? RgC_add(s, t): t;
    2169             :   }
    2170      354354 :   return s;
    2171             : }
    2172             : /* modular symbol given by \phi( G[ind[j]] ) = val[j]
    2173             :  * \sum L[i]*phi[i], L a sparse column of operators */
    2174             : static GEN
    2175      780283 : sparse_act_col(GEN col, GEN phi)
    2176             : {
    2177      780283 :   GEN s = NULL, colind = gel(col,1), colval = gel(col,2);
    2178      780283 :   GEN ind = gel(phi,2), val = gel(phi,3);
    2179      780283 :   long a, l = lg(ind);
    2180      780283 :   if (lg(gel(phi,1)) == 1) return RgM_RgC_mul(gel(colval,1), gel(val,1));
    2181     3036978 :   for (a = 1; a < l; a++)
    2182             :   {
    2183     2257094 :     GEN t = gel(val, a); /* phi(G_i) */
    2184     2257094 :     long i = zv_search(colind, ind[a]);
    2185     2257094 :     if (!i) continue;
    2186      543228 :     t = RgM_RgC_mul(gel(colval,i), t);
    2187      543228 :     s = s? RgC_add(s, t): t;
    2188             :   }
    2189      779884 :   return s;
    2190             : }
    2191             : static int
    2192       69545 : phi_sparse(GEN phi) { return typ(gel(phi,1)) == t_VECSMALL; }
    2193             : /* phi in Hom_Gamma1(Delta, V), return the matrix whose colums are the
    2194             :  *   \sum_i phi(g_i) | \mu_{i,j} = (phi|f)(G_j),
    2195             :  * see init_dual_act. */
    2196             : static GEN
    2197       69545 : dual_act(long dimV, GEN act, GEN phi)
    2198             : {
    2199       69545 :   long l = lg(act), j;
    2200       69545 :   GEN v = cgetg(l, t_MAT);
    2201       69545 :   GEN (*ACT)(GEN,GEN) = phi_sparse(phi)? sparse_act_col: dense_act_col;
    2202     1200850 :   for (j = 1; j < l; j++)
    2203             :   {
    2204     1131305 :     pari_sp av = avma;
    2205     1131305 :     GEN s = ACT(gel(act,j), phi);
    2206     1131305 :     gel(v,j) = s? gerepileupto(av,s): zerocol(dimV);
    2207             :   }
    2208       69545 :   return v;
    2209             : }
    2210             : 
    2211             : /* in level N > 1 */
    2212             : static void
    2213       59507 : msk_get_st(GEN W, long *s, long *t)
    2214       59507 : { GEN st = gmael(W,3,3); *s = st[1]; *t = st[2]; }
    2215             : static GEN
    2216       59507 : msk_get_link(GEN W) { return gmael(W,3,4); }
    2217             : static GEN
    2218       59948 : msk_get_inv(GEN W) { return gmael(W,3,5); }
    2219             : /* \phi in Hom(Delta, V), \phi(G_k) = phi[k]. Write \phi as
    2220             :  *   \sum_{i,j} mu_{i,j} phi_{i,j}, mu_{i,j} in Q */
    2221             : static GEN
    2222       59948 : getMorphism_basis(GEN W, GEN phi)
    2223             : {
    2224       59948 :   GEN R, Q, Ls, T0, T1, Ts, link, basis, inv = msk_get_inv(W);
    2225             :   long i, j, r, s, t, dim, lvecT;
    2226             : 
    2227       59948 :   if (ms_get_N(W) == 1) return ZC_apply_dinv(inv, gel(phi,1));
    2228       59507 :   lvecT = lg(phi);
    2229       59507 :   basis = msk_get_basis(W);
    2230       59507 :   dim = lg(basis)-1;
    2231       59507 :   R = zerocol(dim);
    2232       59507 :   msk_get_st(W, &s, &t);
    2233       59507 :   link = msk_get_link(W);
    2234      791350 :   for (r = 2; r < lvecT; r++)
    2235             :   {
    2236             :     GEN Tr, L;
    2237      731843 :     if (r == s) continue;
    2238      672336 :     Tr = gel(phi,r); /* Phi(G_r), r != 1,s */
    2239      672336 :     L = gel(link, r);
    2240      672336 :     Q = ZC_apply_dinv(gel(inv,r), Tr);
    2241             :     /* write Phi(G_r) as sum_{a,b} mu_{a,b} Phi_{a,b}(G_r) */
    2242      672336 :     for (j = 1; j < lg(L); j++) gel(R, L[j]) = gel(Q,j);
    2243             :   }
    2244       59507 :   Ls = gel(link, s);
    2245       59507 :   T1 = gel(phi,1); /* Phi(G_1) */
    2246       59507 :   gel(R, Ls[t]) = gel(T1, 1);
    2247             : 
    2248       59507 :   T0 = NULL;
    2249      791350 :   for (i = 2; i < lg(link); i++)
    2250             :   {
    2251             :     GEN L;
    2252      731843 :     if (i == s) continue;
    2253      672336 :     L = gel(link,i);
    2254     3514154 :     for (j =1 ; j < lg(L); j++)
    2255             :     {
    2256     2841818 :       long n = L[j]; /* phi_{i,j} = basis[n] */
    2257     2841818 :       GEN mu_ij = gel(R, n);
    2258     2841818 :       GEN phi_ij = gel(basis, n), pols = gel(phi_ij,3);
    2259     2841818 :       GEN z = RgC_Rg_mul(gel(pols, 3), mu_ij);
    2260     2841818 :       T0 = T0? RgC_add(T0, z): z; /* += mu_{i,j} Phi_{i,j} (G_s) */
    2261             :     }
    2262             :   }
    2263       59507 :   Ts = gel(phi,s); /* Phi(G_s) */
    2264       59507 :   if (T0) Ts = RgC_sub(Ts, T0);
    2265             :   /* solve \sum_{j!=t} mu_{s,j} Phi_{s,j}(G_s) = Ts */
    2266       59507 :   Q = ZC_apply_dinv(gel(inv,s), Ts);
    2267       59507 :   for (j = 1; j < t; j++) gel(R, Ls[j]) = gel(Q,j);
    2268             :   /* avoid mu_{s,t} */
    2269       59507 :   for (j = t; j < lg(Q); j++) gel(R, Ls[j+1]) = gel(Q,j);
    2270       59507 :   return R;
    2271             : }
    2272             : 
    2273             : /* a = s(g_i) for some modular symbol s; b in Z[G]
    2274             :  * return s(b.g_i) = b^* . s(g_i) */
    2275             : static GEN
    2276      115458 : ZGl2Q_act_s(GEN b, GEN a, long k)
    2277             : {
    2278      115458 :   if (typ(b) == t_INT)
    2279             :   {
    2280       58604 :     if (!signe(b)) return gen_0;
    2281          14 :     switch(typ(a))
    2282             :     {
    2283             :       case t_POL:
    2284          14 :         a = RgX_to_RgC(a, k-1); /*fall through*/
    2285             :       case t_COL:
    2286          14 :         a = RgC_Rg_mul(a,b);
    2287          14 :         break;
    2288           0 :       default: a = scalarcol_shallow(b,k-1);
    2289             :     }
    2290             :   }
    2291             :   else
    2292             :   {
    2293       56854 :     b = RgX_act_ZGl2Q(ZSl2_star(b), k);
    2294       56854 :     switch(typ(a))
    2295             :     {
    2296             :       case t_POL:
    2297          63 :         a = RgX_to_RgC(a, k-1); /*fall through*/
    2298             :       case t_COL:
    2299       45094 :         a = RgM_RgC_mul(b,a);
    2300       45094 :         break;
    2301       11760 :       default: a = RgC_Rg_mul(gel(b,1),a);
    2302             :     }
    2303             :   }
    2304       56868 :   return a;
    2305             : }
    2306             : 
    2307             : static int
    2308          21 : checksymbol(GEN W, GEN s)
    2309             : {
    2310             :   GEN t, annT2, annT31, singlerel;
    2311             :   long i, k, l, nbE1, nbT2, nbT31;
    2312          21 :   k = msk_get_weight(W);
    2313          21 :   W = get_msN(W);
    2314          21 :   nbE1 = ms_get_nbE1(W);
    2315          21 :   singlerel = gel(W,10);
    2316          21 :   l = lg(singlerel);
    2317          21 :   if (k == 2)
    2318             :   {
    2319           0 :     for (i = nbE1+1; i < l; i++)
    2320           0 :       if (!gequal0(gel(s,i))) return 0;
    2321           0 :     return 1;
    2322             :   }
    2323          21 :   annT2 = msN_get_annT2(W); nbT2 = lg(annT2)-1;
    2324          21 :   annT31 = msN_get_annT31(W);nbT31 = lg(annT31)-1;
    2325          21 :   t = NULL;
    2326          84 :   for (i = 1; i < l; i++)
    2327             :   {
    2328          63 :     GEN a = gel(s,i);
    2329          63 :     a = ZGl2Q_act_s(gel(singlerel,i), a, k);
    2330          63 :     t = t? gadd(t, a): a;
    2331             :   }
    2332          21 :   if (!gequal0(t)) return 0;
    2333          14 :   for (i = 1; i <= nbT2; i++)
    2334             :   {
    2335           0 :     GEN a = gel(s,i + nbE1);
    2336           0 :     a = ZGl2Q_act_s(gel(annT2,i), a, k);
    2337           0 :     if (!gequal0(a)) return 0;
    2338             :   }
    2339          28 :   for (i = 1; i <= nbT31; i++)
    2340             :   {
    2341          14 :     GEN a = gel(s,i + nbE1 + nbT2);
    2342          14 :     a = ZGl2Q_act_s(gel(annT31,i), a, k);
    2343          14 :     if (!gequal0(a)) return 0;
    2344             :   }
    2345          14 :   return 1;
    2346             : }
    2347             : GEN
    2348          56 : msissymbol(GEN W, GEN s)
    2349             : {
    2350             :   long k, nbgen;
    2351          56 :   checkms(W);
    2352          56 :   k = msk_get_weight(W);
    2353          56 :   nbgen = ms_get_nbgen(W);
    2354          56 :   switch(typ(s))
    2355             :   {
    2356             :     case t_VEC: /* values s(g_i) */
    2357          21 :       if (lg(s)-1 != nbgen) return gen_0;
    2358          21 :       break;
    2359             :     case t_COL:
    2360          28 :       if (msk_get_sign(W))
    2361             :       {
    2362           0 :         GEN star = gel(msk_get_starproj(W), 1);
    2363           0 :         if (lg(star) == lg(s)) return gen_1;
    2364             :       }
    2365          28 :       if (k == 2) /* on the dual basis of (g_i) */
    2366             :       {
    2367           0 :         if (lg(s)-1 != nbgen) return gen_0;
    2368             :       }
    2369             :       else
    2370             :       {
    2371          28 :         GEN basis = msk_get_basis(W);
    2372          28 :         return (lg(s) == lg(basis))? gen_1: gen_0;
    2373             :       }
    2374           0 :       break;
    2375             :     case t_MAT:
    2376             :     {
    2377           7 :       long i, l = lg(s);
    2378           7 :       GEN v = cgetg(l, t_VEC);
    2379           7 :       for (i = 1; i < l; i++) gel(v,i) = msissymbol(W,gel(s,i))? gen_1: gen_0;
    2380           7 :       return v;
    2381             :     }
    2382           0 :     default: return gen_0;
    2383             :   }
    2384          21 :   return checksymbol(W,s)? gen_1: gen_0;
    2385             : }
    2386             : #if DEBUG
    2387             : /* phi is a sparse symbol from msk_get_basis, return phi(G_j) */
    2388             : static GEN
    2389             : phi_Gj(GEN W, GEN phi, long j)
    2390             : {
    2391             :   GEN ind = gel(phi,2), pols = gel(phi,3);
    2392             :   long i = vecsmall_isin(ind,j);
    2393             :   return i? gel(pols,i): NULL;
    2394             : }
    2395             : /* check that \sum d_i phi_i(G_j)  = T_j for all j */
    2396             : static void
    2397             : checkdec(GEN W, GEN D, GEN T)
    2398             : {
    2399             :   GEN B = msk_get_basis(W);
    2400             :   long i, j;
    2401             :   if (!checksymbol(W,T)) pari_err_BUG("checkdec");
    2402             :   for (j = 1; j < lg(T); j++)
    2403             :   {
    2404             :     GEN S = gen_0;
    2405             :     for (i = 1; i < lg(D); i++)
    2406             :     {
    2407             :       GEN d = gel(D,i), v = phi_Gj(W, gel(B,i), j);
    2408             :       if (!v || gequal0(d)) continue;
    2409             :       S = gadd(S, gmul(d, v));
    2410             :     }
    2411             :     /* S = \sum_i d_i phi_i(G_j) */
    2412             :     if (!gequal(S, gel(T,j)))
    2413             :       pari_warn(warner, "checkdec j = %ld\n\tS = %Ps\n\tT = %Ps", j,S,gel(T,j));
    2414             :   }
    2415             : }
    2416             : #endif
    2417             : 
    2418             : /* map op: W1 = Hom(Delta_0(N1),V) -> W2 = Hom(Delta_0(N2),V), given by
    2419             :  * \sum v[i], v[i] in Gl2(Q) */
    2420             : static GEN
    2421        5523 : getMorphism(GEN W1, GEN W2, GEN v)
    2422             : {
    2423             :   struct m_act S;
    2424             :   GEN B1, M, act;
    2425        5523 :   long a, l, k = msk_get_weight(W1);
    2426        5523 :   if (k == 2) return getMorphism_trivial(W1,W2,v);
    2427        2779 :   S.k = k;
    2428        2779 :   S.dim = k-1;
    2429        2779 :   S.act = &_RgX_act_Gl2Q;
    2430        2779 :   act = init_dual_act(v,W1,W2,&S);
    2431        2779 :   B1 = msk_get_basis(W1);
    2432        2779 :   l = lg(B1); M = cgetg(l, t_MAT);
    2433       61656 :   for (a = 1; a < l; a++)
    2434             :   {
    2435       58877 :     pari_sp av = avma;
    2436       58877 :     GEN phi = dual_act(S.dim, act, gel(B1,a));
    2437       58877 :     GEN D = getMorphism_basis(W2, phi);
    2438             : #if DEBUG
    2439             :     checkdec(W2,D,T);
    2440             : #endif
    2441       58877 :     gel(M,a) = gerepilecopy(av, D);
    2442             :   }
    2443        2779 :   return M;
    2444             : }
    2445             : static GEN
    2446        4389 : msendo(GEN W, GEN v) { return getMorphism(W, W, v); }
    2447             : 
    2448             : static GEN
    2449        2583 : endo_project(GEN W, GEN e, GEN H)
    2450             : {
    2451        2583 :   if (msk_get_sign(W)) e = Qevproj_apply(e, msk_get_starproj(W));
    2452        2583 :   if (H) e = Qevproj_apply(e, Qevproj_init0(H));
    2453        2583 :   return e;
    2454             : }
    2455             : static GEN
    2456        2947 : mshecke_i(GEN W, ulong p)
    2457             : {
    2458        2947 :   GEN v = ms_get_N(W) % p? Tp_matrices(p): Up_matrices(p);
    2459        2947 :   return msendo(W,v);
    2460             : }
    2461             : GEN
    2462        2534 : mshecke(GEN W, long p, GEN H)
    2463             : {
    2464        2534 :   pari_sp av = avma;
    2465             :   GEN T;
    2466        2534 :   checkms(W);
    2467        2534 :   if (p <= 1) pari_err_PRIME("mshecke",stoi(p));
    2468        2534 :   T = mshecke_i(W,p);
    2469        2534 :   T = endo_project(W,T,H);
    2470        2534 :   return gerepilecopy(av, T);
    2471             : }
    2472             : 
    2473             : static GEN
    2474          42 : msatkinlehner_i(GEN W, long Q)
    2475             : {
    2476          42 :   long N = ms_get_N(W);
    2477             :   GEN v;
    2478          42 :   if (Q == 1) return matid(msk_get_dim(W));
    2479          28 :   if (Q == N) return msendo(W, mkvec(mat2(0,1,-N,0)));
    2480          21 :   if (N % Q) pari_err_DOMAIN("msatkinlehner","N % Q","!=",gen_0,stoi(Q));
    2481          14 :   v = WQ_matrix(N, Q);
    2482          14 :   if (!v) pari_err_DOMAIN("msatkinlehner","gcd(Q,N/Q)","!=",gen_1,stoi(Q));
    2483          14 :   return msendo(W,mkvec(v));
    2484             : }
    2485             : GEN
    2486          42 : msatkinlehner(GEN W, long Q, GEN H)
    2487             : {
    2488          42 :   pari_sp av = avma;
    2489             :   GEN w;
    2490             :   long k;
    2491          42 :   checkms(W);
    2492          42 :   k = msk_get_weight(W);
    2493          42 :   if (Q <= 0) pari_err_DOMAIN("msatkinlehner","Q","<=",gen_0,stoi(Q));
    2494          42 :   w = msatkinlehner_i(W,Q);
    2495          35 :   w = endo_project(W,w,H);
    2496          35 :   if (k > 2 && Q != 1) w = RgM_Rg_div(w, powuu(Q,(k-2)>>1));
    2497          35 :   return gerepilecopy(av, w);
    2498             : }
    2499             : 
    2500             : static GEN
    2501        1421 : msstar_i(GEN W) { return msendo(W, mkvec(mat2(-1,0,0,1))); }
    2502             : GEN
    2503          14 : msstar(GEN W, GEN H)
    2504             : {
    2505          14 :   pari_sp av = avma;
    2506             :   GEN s;
    2507          14 :   checkms(W);
    2508          14 :   s = msstar_i(W);
    2509          14 :   s = endo_project(W,s,H);
    2510          14 :   return gerepilecopy(av, s);
    2511             : }
    2512             : 
    2513             : #if 0
    2514             : /* is \Gamma_0(N) cusp1 = \Gamma_0(N) cusp2 ? */
    2515             : static int
    2516             : iscuspeq(ulong N, GEN cusp1, GEN cusp2)
    2517             : {
    2518             :   long p1, q1, p2, q2, s1, s2, d;
    2519             :   p1 = cusp1[1]; p2 = cusp2[1];
    2520             :   q1 = cusp1[2]; q2 = cusp2[2];
    2521             :   d = Fl_mul(smodss(q1,N),smodss(q2,N), N);
    2522             :   d = ugcd(d, N);
    2523             : 
    2524             :   s1 = q1 > 2? Fl_inv(smodss(p1,q1), q1): 1;
    2525             :   s2 = q2 > 2? Fl_inv(smodss(p2,q2), q2): 1;
    2526             :   return Fl_mul(s1,q2,d) == Fl_mul(s2,q1,d);
    2527             : }
    2528             : #endif
    2529             : 
    2530             : /* return E_c(r) */
    2531             : static GEN
    2532        2926 : get_Ec_r(GEN c, long k)
    2533             : {
    2534        2926 :   long p = c[1], q = c[2], u, v;
    2535             :   GEN gr;
    2536        2926 :   (void)cbezout(p, q, &u, &v);
    2537        2926 :   gr = mat2(p, -v, q, u); /* g . (1:0) = (p:q) */
    2538        2926 :   return voo_act_Gl2Q(sl2_inv(gr), k);
    2539             : }
    2540             : /* N > 1; returns the modular symbol attached to the cusp c := p/q via the rule
    2541             :  * E_c(path from a to b in Delta_0) := E_c(b) - E_c(a), where
    2542             :  * E_c(r) := 0 if r != c mod Gamma
    2543             :  *           v_oo | gamma_r^(-1)
    2544             :  * where v_oo is stable by T = [1,1;0,1] (i.e x^(k-2)) and
    2545             :  * gamma_r . (1:0) = r, for some gamma_r in SL_2(Z) * */
    2546             : static GEN
    2547         441 : msfromcusp_trivial(GEN W, GEN c)
    2548             : {
    2549         441 :   GEN section = ms_get_section(W), gen = ms_get_genindex(W);
    2550         441 :   GEN S = ms_get_hashcusps(W);
    2551         441 :   long j, ic = cusp_index(c, S), l = ms_get_nbE1(W)+1;
    2552         441 :   GEN phi = cgetg(l, t_COL);
    2553       90062 :   for (j = 1; j < l; j++)
    2554             :   {
    2555       89621 :     GEN vj, g = gel(section, gen[j]); /* path_to_zm(generator) */
    2556       89621 :     GEN c1 = gel(g,1), c2 = gel(g,2);
    2557       89621 :     long i1 = cusp_index(c1, S);
    2558       89621 :     long i2 = cusp_index(c2, S);
    2559       89621 :     if (i1 == ic)
    2560        3206 :       vj = (i2 == ic)?  gen_0: gen_1;
    2561             :     else
    2562       86415 :       vj = (i2 == ic)? gen_m1: gen_0;
    2563       89621 :     gel(phi, j) = vj;
    2564             :   }
    2565         441 :   return phi;
    2566             : }
    2567             : static GEN
    2568        1512 : msfromcusp_i(GEN W, GEN c)
    2569             : {
    2570             :   GEN section, gen, S, phi;
    2571        1512 :   long j, ic, l, k = msk_get_weight(W);
    2572        1512 :   if (k == 2)
    2573             :   {
    2574         441 :     long N = ms_get_N(W);
    2575         441 :     return N == 1? cgetg(1,t_COL): msfromcusp_trivial(W, c);
    2576             :   }
    2577        1071 :   k = msk_get_weight(W);
    2578        1071 :   section = ms_get_section(W);
    2579        1071 :   gen = ms_get_genindex(W);
    2580        1071 :   S = ms_get_hashcusps(W);
    2581        1071 :   ic = cusp_index(c, S);
    2582        1071 :   l = lg(gen);
    2583        1071 :   phi = cgetg(l, t_COL);
    2584       12579 :   for (j = 1; j < l; j++)
    2585             :   {
    2586       11508 :     GEN vj = NULL, g = gel(section, gen[j]); /* path_to_zm(generator) */
    2587       11508 :     GEN c1 = gel(g,1), c2 = gel(g,2);
    2588       11508 :     long i1 = cusp_index(c1, S);
    2589       11508 :     long i2 = cusp_index(c2, S);
    2590       11508 :     if (i1 == ic) vj = get_Ec_r(c1, k);
    2591       11508 :     if (i2 == ic)
    2592             :     {
    2593        1463 :       GEN s = get_Ec_r(c2, k);
    2594        1463 :       vj = vj? gsub(vj, s): gneg(s);
    2595             :     }
    2596       11508 :     if (!vj) vj = zerocol(k-1);
    2597       11508 :     gel(phi, j) = vj;
    2598             :   }
    2599        1071 :   return getMorphism_basis(W, phi);
    2600             : }
    2601             : GEN
    2602          28 : msfromcusp(GEN W, GEN c)
    2603             : {
    2604          28 :   pari_sp av = avma;
    2605             :   long N;
    2606          28 :   checkms(W);
    2607          28 :   N = ms_get_N(W);
    2608          28 :   switch(typ(c))
    2609             :   {
    2610             :     case t_INFINITY:
    2611           7 :       c = mkvecsmall2(1,0);
    2612           7 :       break;
    2613             :     case t_INT:
    2614          14 :       c = mkvecsmall2(smodis(c,N), 1);
    2615          14 :       break;
    2616             :     case t_FRAC:
    2617           7 :       c = mkvecsmall2(smodis(gel(c,1),N), smodis(gel(c,2),N));
    2618           7 :       break;
    2619             :     default:
    2620           0 :       pari_err_TYPE("msfromcusp",c);
    2621             :   }
    2622          28 :   return gerepilecopy(av, msfromcusp_i(W,c));
    2623             : }
    2624             : 
    2625             : static GEN
    2626         357 : mseisenstein_i(GEN W)
    2627             : {
    2628         357 :   GEN M, S = ms_get_hashcusps(W), cusps = gel(S,3);
    2629         357 :   long i, l = lg(cusps);
    2630         357 :   if (msk_get_weight(W)==2) l--;
    2631         357 :   M = cgetg(l, t_MAT);
    2632         357 :   for (i = 1; i < l; i++) gel(M,i) = msfromcusp_i(W, gel(cusps,i));
    2633         357 :   return Qevproj_star(W, QM_image(M));
    2634             : }
    2635             : GEN
    2636         357 : mseisenstein(GEN W)
    2637             : {
    2638         357 :   pari_sp av = avma;
    2639         357 :   checkms(W);
    2640         357 :   return gerepilecopy(av, Qevproj_init(mseisenstein_i(W)));
    2641             : }
    2642             : 
    2643             : /* upper bound for log_2 |charpoly(T_p|S)|, where S is a cuspidal subspace of
    2644             :  * dimension d, k is the weight */
    2645             : #if 0
    2646             : static long
    2647             : TpS_char_bound(ulong p, long k, long d)
    2648             : { /* |eigenvalue| <= 2 p^(k-1)/2 */
    2649             :   return d * (2 + (log2((double)p)*(k-1))/2);
    2650             : }
    2651             : #endif
    2652             : static long
    2653         336 : TpE_char_bound(ulong p, long k, long d)
    2654             : { /* |eigenvalue| <= 2 p^(k-1) */
    2655         336 :   return d * (2 + log2((double)p)*(k-1));
    2656             : }
    2657             : 
    2658             : GEN
    2659         336 : mscuspidal(GEN W, long flag)
    2660             : {
    2661         336 :   pari_sp av = avma;
    2662             :   GEN S, E, M, T, TE, chE;
    2663             :   long bit;
    2664             :   forprime_t F;
    2665             :   ulong p, N;
    2666             :   pari_timer ti;
    2667             : 
    2668         336 :   E = mseisenstein(W);
    2669         336 :   N = ms_get_N(W);
    2670         336 :   (void)u_forprime_init(&F, 2, ULONG_MAX);
    2671         336 :   while ((p = u_forprime_next(&F)))
    2672         469 :     if (N % p) break;
    2673         336 :   if (DEBUGLEVEL) timer_start(&ti);
    2674         336 :   T = mshecke(W, p, NULL);
    2675         336 :   if (DEBUGLEVEL) timer_printf(&ti,"Tp, p = %ld", p);
    2676         336 :   TE = Qevproj_apply(T, E); /* T_p | E */
    2677         336 :   if (DEBUGLEVEL) timer_printf(&ti,"Qevproj_init(E)");
    2678         336 :   bit = TpE_char_bound(p, msk_get_weight(W), lg(TE)-1);
    2679         336 :   chE = QM_charpoly_ZX_bound(TE, bit);
    2680         336 :   chE = ZX_radical(chE);
    2681         336 :   M = RgX_RgM_eval(chE, T);
    2682         336 :   S = Qevproj_init(QM_image(M));
    2683         336 :   return gerepilecopy(av, flag? mkvec2(S,E): S);
    2684             : }
    2685             : 
    2686             : /** INIT ELLSYM STRUCTURE **/
    2687             : /* V a vector of ZM. If all of them have 0 last row, return NULL.
    2688             :  * Otherwise return [m,i,j], where m = V[i][last,j] contains the value
    2689             :  * of smallest absolute value */
    2690             : static GEN
    2691         931 : RgMV_find_non_zero_last_row(long offset, GEN V)
    2692             : {
    2693         931 :   long i, lasti = 0, lastj = 0, lV = lg(V);
    2694         931 :   GEN m = NULL;
    2695        4074 :   for (i = 1; i < lV; i++)
    2696             :   {
    2697        3143 :     GEN M = gel(V,i);
    2698        3143 :     long j, n, l = lg(M);
    2699        3143 :     if (l == 1) continue;
    2700        2835 :     n = nbrows(M);
    2701       13804 :     for (j = 1; j < l; j++)
    2702             :     {
    2703       10969 :       GEN a = gcoeff(M, n, j);
    2704       10969 :       if (!gequal0(a) && (!m || abscmpii(a, m) < 0))
    2705             :       {
    2706        1582 :         m = a; lasti = i; lastj = j;
    2707        1582 :         if (is_pm1(m)) goto END;
    2708             :       }
    2709             :     }
    2710             :   }
    2711             : END:
    2712         931 :   if (!m) return NULL;
    2713         623 :   return mkvec2(m, mkvecsmall2(lasti+offset, lastj));
    2714             : }
    2715             : /* invert the d_oo := (\gamma_oo - 1) operator, acting on
    2716             :  * [x^(k-2), ..., y^(k-2)] */
    2717             : static GEN
    2718         623 : Delta_inv(GEN doo, long k)
    2719             : {
    2720         623 :   GEN M = RgX_act_ZGl2Q(doo, k);
    2721         623 :   M = RgM_minor(M, k-1, 1); /* 1st column and last row are 0 */
    2722         623 :   return ZM_inv_denom(M);
    2723             : }
    2724             : /* The ZX P = \sum a_i x^i y^{k-2-i} is given by the ZV [a_0, ..., a_k-2]~,
    2725             :  * return Q and d such that P = doo Q + d y^k-2, where d in Z and Q */
    2726             : static GEN
    2727       12831 : doo_decompose(GEN dinv, GEN P, GEN *pd)
    2728             : {
    2729       12831 :   long l = lg(P); *pd = gel(P, l-1);
    2730       12831 :   P = vecslice(P, 1, l-2);
    2731       12831 :   return shallowconcat(gen_0, ZC_apply_dinv(dinv, P));
    2732             : }
    2733             : 
    2734             : static GEN
    2735       12831 : get_phi_ij(long i,long j,long n, long s,long t,GEN P_st,GEN Q_st,GEN d_st,
    2736             :            GEN P_ij, GEN lP_ij, GEN dinv)
    2737             : {
    2738             :   GEN ind, pols;
    2739       12831 :   if (i == s && j == t)
    2740             :   {
    2741         623 :     ind = mkvecsmall(1);
    2742         623 :     pols = mkvec(scalarcol_shallow(gen_1, lg(P_st)-1)); /* x^{k-2} */
    2743             :   }
    2744             :   else
    2745             :   {
    2746       12208 :     GEN d_ij, Q_ij = doo_decompose(dinv, lP_ij, &d_ij);
    2747       12208 :     GEN a = ZC_Z_mul(P_ij, d_st);
    2748       12208 :     GEN b = ZC_Z_mul(P_st, negi(d_ij));
    2749       12208 :     GEN c = RgC_sub(RgC_Rg_mul(Q_ij, d_st), RgC_Rg_mul(Q_st, d_ij));
    2750       12208 :     if (i == s) { /* j != t */
    2751        1645 :       ind = mkvecsmall2(1, s);
    2752        1645 :       pols = mkvec2(c, ZC_add(a, b));
    2753             :     } else {
    2754       10563 :       ind = mkvecsmall3(1, i, s);
    2755       10563 :       pols = mkvec3(c, a, b); /* image of g_1, g_i, g_s */
    2756             :     }
    2757       12208 :     pols = Q_primpart(pols);
    2758             :   }
    2759       12831 :   return mkvec3(mkvecsmall3(i,j,n), ind, pols);
    2760             : }
    2761             : 
    2762             : static GEN
    2763         770 : mskinit_trivial(GEN WN)
    2764             : {
    2765         770 :   long dim = ms_get_nbE1(WN);
    2766         770 :   return mkvec3(WN, gen_0, mkvec2(gen_0,mkvecsmall2(2, dim)));
    2767             : }
    2768             : /* sum of #cols of the matrices contained in V */
    2769             : static long
    2770        1246 : RgMV_dim(GEN V)
    2771             : {
    2772        1246 :   long l = lg(V), d = 0, i;
    2773        1246 :   for (i = 1; i < l; i++) d += lg(gel(V,i)) - 1;
    2774        1246 :   return d;
    2775             : }
    2776             : static GEN
    2777         623 : mskinit_nontrivial(GEN WN, long k)
    2778             : {
    2779         623 :   GEN annT2 = gel(WN,8), annT31 = gel(WN,9), singlerel = gel(WN,10);
    2780             :   GEN link, basis, monomials, Inv;
    2781         623 :   long nbE1 = ms_get_nbE1(WN);
    2782         623 :   GEN dinv = Delta_inv(ZG_neg( ZSl2_star(gel(singlerel,1)) ), k);
    2783         623 :   GEN p1 = cgetg(nbE1+1, t_VEC), remove;
    2784         623 :   GEN p2 = ZGV_tors(annT2, k);
    2785         623 :   GEN p3 = ZGV_tors(annT31, k);
    2786         623 :   GEN gentor = shallowconcat(p2, p3);
    2787             :   GEN P_st, lP_st, Q_st, d_st;
    2788             :   long n, i, dim, s, t, u;
    2789         623 :   gel(p1, 1) = cgetg(1,t_MAT); /* dummy */
    2790        3360 :   for (i = 2; i <= nbE1; i++) /* skip 1st element = (\gamma_oo-1)g_oo */
    2791             :   {
    2792        2737 :     GEN z = gel(singlerel, i);
    2793        2737 :     gel(p1, i) = RgX_act_ZGl2Q(ZSl2_star(z), k);
    2794             :   }
    2795         623 :   remove = RgMV_find_non_zero_last_row(nbE1, gentor);
    2796         623 :   if (!remove) remove = RgMV_find_non_zero_last_row(0, p1);
    2797         623 :   if (!remove) pari_err_BUG("msinit [no y^k-2]");
    2798         623 :   remove = gel(remove,2); /* [s,t] */
    2799         623 :   s = remove[1];
    2800         623 :   t = remove[2];
    2801             :   /* +1 because of = x^(k-2), but -1 because of Manin relation */
    2802         623 :   dim = (k-1)*(nbE1-1) + RgMV_dim(p2) + RgMV_dim(p3);
    2803             :   /* Let (g_1,...,g_d) be the Gamma-generators of Delta, g_1 = g_oo.
    2804             :    * We describe modular symbols by the collection phi(g_1), ..., phi(g_d)
    2805             :    * \in V := Q[x,y]_{k-2}, with right Gamma action.
    2806             :    * For each i = 1, .., d, let V_i \subset V be the Q-vector space of
    2807             :    * allowed values for phi(g_i): with basis (P^{i,j}) given by the monomials
    2808             :    * x^(j-1) y^{k-2-(j-1)}, j = 1 .. k-1
    2809             :    * (g_i in E_1) or the solution of the torsion equations (1 + gamma)P = 0
    2810             :    * (g_i in T2) or (1 + gamma + gamma^2)P = 0 (g_i in T31). All such P
    2811             :    * are chosen in Z[x,y] with Q_content 1.
    2812             :    *
    2813             :    * The Manin relation (singlerel) is of the form \sum_i \lambda_i g_i = 0,
    2814             :    * where \lambda_i = 1 if g_i in T2 or T31, and \lambda_i = (1 - \gamma_i)
    2815             :    * for g_i in E1.
    2816             :    *
    2817             :    * If phi \in Hom_Gamma(Delta, V), it is defined by phi(g_i) := P_i in V
    2818             :    * with \sum_i P_i . \lambda_i^* = 0, where (\sum n_i g_i)^* :=
    2819             :    * \sum n_i \gamma_i^(-1).
    2820             :    *
    2821             :    * We single out gamma_1 / g_1 (g_oo in Pollack-Stevens paper) and
    2822             :    * write P_{i,j} \lambda_i^* =  Q_{i,j} (\gamma_1 - 1)^* + d_{i,j} y^{k-2}
    2823             :    * where d_{i,j} is a scalar and Q_{i,j} in V; we normalize Q_{i,j} to
    2824             :    * that the coefficient of x^{k-2} is 0.
    2825             :    *
    2826             :    * There exist (s,t) such that d_{s,t} != 0.
    2827             :    * A Q-basis of the (dual) space of modular symbols is given by the
    2828             :    * functions phi_{i,j}, 2 <= i <= d, 1 <= j <= k-1, mapping
    2829             :    *  g_1 -> d_{s,t} Q_{i,j} - d_{i,j} Q_{s,t} + [(i,j)=(s,t)] x^{k-2}
    2830             :    * If i != s
    2831             :    *   g_i -> d_{s,t} P_{i,j}
    2832             :    *   g_s -> - d_{i,j} P_{s,t}
    2833             :    * If i = s, j != t
    2834             :    *   g_i -> d_{s,t} P_{i,j} - d_{i,j} P_{s,t}
    2835             :    * And everything else to 0. Again we normalize the phi_{i,j} such that
    2836             :    * their image has content 1. */
    2837         623 :   monomials = matid(k-1); /* represent the monomials x^{k-2}, ... , y^{k-2} */
    2838         623 :   if (s <= nbE1) /* in E1 */
    2839             :   {
    2840         308 :     P_st = gel(monomials, t);
    2841         308 :     lP_st = gmael(p1, s, t); /* P_{s,t} lambda_s^* */
    2842             :   }
    2843             :   else /* in T2, T31 */
    2844             :   {
    2845         315 :     P_st = gmael(gentor, s - nbE1, t);
    2846         315 :     lP_st = P_st;
    2847             :   }
    2848         623 :   Q_st = doo_decompose(dinv, lP_st, &d_st);
    2849         623 :   basis = cgetg(dim+1, t_VEC);
    2850         623 :   link = cgetg(nbE1 + lg(gentor), t_VEC);
    2851         623 :   gel(link,1) = cgetg(1,t_VECSMALL); /* dummy */
    2852         623 :   n = 1;
    2853        3360 :   for (i = 2; i <= nbE1; i++)
    2854             :   {
    2855        2737 :     GEN L = cgetg(k, t_VECSMALL);
    2856             :     long j;
    2857             :     /* link[i][j] = n gives correspondance between phi_{i,j} and basis[n] */
    2858        2737 :     gel(link,i) = L;
    2859       14000 :     for (j = 1; j < k; j++)
    2860             :     {
    2861       11263 :       GEN lP_ij = gmael(p1, i, j); /* P_{i,j} lambda_i^* */
    2862       11263 :       GEN P_ij = gel(monomials,j);
    2863       11263 :       L[j] = n;
    2864       11263 :       gel(basis, n) = get_phi_ij(i,j,n, s,t, P_st, Q_st, d_st, P_ij, lP_ij, dinv);
    2865       11263 :       n++;
    2866             :     }
    2867             :   }
    2868        1127 :   for (u = 1; u < lg(gentor); u++,i++)
    2869             :   {
    2870         504 :     GEN V = gel(gentor,u);
    2871         504 :     long j, lV = lg(V);
    2872         504 :     GEN L = cgetg(lV, t_VECSMALL);
    2873         504 :     gel(link,i) = L;
    2874        2072 :     for (j = 1; j < lV; j++)
    2875             :     {
    2876        1568 :       GEN lP_ij = gel(V, j); /* P_{i,j} lambda_i^* = P_{i,j} */
    2877        1568 :       GEN P_ij = lP_ij;
    2878        1568 :       L[j] = n;
    2879        1568 :       gel(basis, n) = get_phi_ij(i,j,n, s,t, P_st, Q_st, d_st, P_ij, lP_ij, dinv);
    2880        1568 :       n++;
    2881             :     }
    2882             :   }
    2883         623 :   Inv = cgetg(lg(link), t_VEC);
    2884         623 :   gel(Inv,1) = cgetg(1, t_MAT); /* dummy */
    2885        3864 :   for (i = 2; i < lg(link); i++)
    2886             :   {
    2887        3241 :     GEN M, inv, B = gel(link,i);
    2888        3241 :     long j, lB = lg(B);
    2889        3241 :     if (i == s) { B = vecsplice(B, t); lB--; } /* remove phi_st */
    2890        3241 :     M = cgetg(lB, t_MAT);
    2891       15449 :     for (j = 1; j < lB; j++)
    2892             :     {
    2893       12208 :       GEN phi_ij = gel(basis, B[j]), pols = gel(phi_ij,3);
    2894       12208 :       gel(M, j) = gel(pols, 2); /* phi_ij(g_i) */
    2895             :     }
    2896        3241 :     if (i <= nbE1 && i != s) /* maximal rank k-1 */
    2897        2429 :       inv = ZM_inv_denom(M);
    2898             :     else /* i = s (rank k-2) or from torsion: rank k/3 or k/2 */
    2899         812 :       inv = Qevproj_init(M);
    2900        3241 :     gel(Inv,i) = inv;
    2901             :   }
    2902         623 :   return mkvec3(WN, gen_0, mkvec5(basis, mkvecsmall2(k, dim), mkvecsmall2(s,t),
    2903             :                                   link, Inv));
    2904             : }
    2905             : static GEN
    2906        1407 : add_star(GEN W, long sign)
    2907             : {
    2908        1407 :   GEN s = msstar_i(W);
    2909        1407 :   GEN K = sign? QM_ker_r(gsubgs(s, sign)): cgetg(1,t_MAT);
    2910        1407 :   gel(W,2) = mkvec3(stoi(sign), s, Qevproj_init(K));
    2911        1407 :   return W;
    2912             : }
    2913             : /* WN = msinit_N(N) */
    2914             : static GEN
    2915        1407 : mskinit(ulong N, long k, long sign)
    2916             : {
    2917        1407 :   GEN W, WN = msinit_N(N);
    2918        1407 :   if (N == 1)
    2919             :   {
    2920          14 :     GEN basis, M = RgXV_to_RgM(mfperiodpolbasis(k, 0), k-1);
    2921          14 :     GEN T = cgetg(1, t_VECSMALL), ind = mkvecsmall(1);
    2922          14 :     long i, l = lg(M);
    2923          14 :     basis = cgetg(l, t_VEC);
    2924          14 :     for (i = 1; i < l; i++) gel(basis,i) = mkvec3(T, ind, mkvec(gel(M,i)));
    2925          14 :     W = mkvec3(WN, gen_0, mkvec5(basis, mkvecsmall2(k, l-1), mkvecsmall2(0,0),
    2926             :                                  gen_0, Qevproj_init(M)));
    2927             :   }
    2928             :   else
    2929        1393 :     W = k == 2? mskinit_trivial(WN)
    2930        1393 :               : mskinit_nontrivial(WN, k);
    2931        1407 :   return add_star(W, sign);
    2932             : }
    2933             : GEN
    2934         462 : msinit(GEN N, GEN K, long sign)
    2935             : {
    2936         462 :   pari_sp av = avma;
    2937             :   GEN W;
    2938             :   long k;
    2939         462 :   if (typ(N) != t_INT) pari_err_TYPE("msinit", N);
    2940         462 :   if (typ(K) != t_INT) pari_err_TYPE("msinit", K);
    2941         462 :   k = itos(K);
    2942         462 :   if (k < 2) pari_err_DOMAIN("msinit","k", "<", gen_2,K);
    2943         462 :   if (odd(k)) pari_err_IMPL("msinit [odd weight]");
    2944         462 :   if (signe(N) <= 0) pari_err_DOMAIN("msinit","N", "<=", gen_0,N);
    2945         462 :   W = mskinit(itou(N), k, sign);
    2946         462 :   return gerepilecopy(av, W);
    2947             : }
    2948             : 
    2949             : /* W = msinit, xpm integral modular symbol of weight 2, c t_FRAC
    2950             :  * Return image of <oo->c> */
    2951             : static GEN
    2952        2065 : Q_xpm(GEN W, GEN xpm, GEN c)
    2953             : {
    2954        2065 :   pari_sp av = avma;
    2955             :   GEN v;
    2956        2065 :   W = get_msN(W);
    2957        2065 :   v = init_act_trivial(W);
    2958        2065 :   Q_log_trivial(v, W, c); /* oo -> (a:b), c = a/b */
    2959        2065 :   return gerepileuptoint(av, ZV_zc_mul(xpm, v));
    2960             : }
    2961             : 
    2962             : static GEN
    2963       20146 : eval_single(GEN s, long k, GEN B, long v)
    2964             : {
    2965             :   long i, l;
    2966       20146 :   GEN A = cgetg_copy(s,&l);
    2967       20146 :   for (i=1; i<l; i++) gel(A,i) = ZGl2Q_act_s(gel(B,i), gel(s,i), k);
    2968       20146 :   A = RgV_sum(A);
    2969       20146 :   if (is_vec_t(typ(A))) A = RgV_to_RgX(A, v);
    2970       20146 :   return A;
    2971             : }
    2972             : /* Evaluate symbol s on mspathlog B (= sum p_i g_i, p_i in Z[G]). Allow
    2973             :  * s = t_MAT [ collection of symbols, return a vector ]*/
    2974             : static GEN
    2975       15904 : mseval_by_values(GEN W, GEN s, GEN p, long v)
    2976             : {
    2977       15904 :   long i, l, k = msk_get_weight(W);
    2978             :   GEN A;
    2979       15904 :   if (k == 2)
    2980             :   { /* trivial represention: don't bother with Z[G] */
    2981        3444 :     GEN B = mspathlog_trivial(W,p);
    2982        3437 :     if (typ(s) != t_MAT) return RgV_zc_mul(s,B);
    2983        3367 :     l = lg(s); A = cgetg(l, t_VEC);
    2984        3367 :     for (i = 1; i < l; i++) gel(A,i) = RgV_zc_mul(gel(s,i), B);
    2985             :   }
    2986             :   else
    2987             :   {
    2988       12460 :     GEN B = mspathlog(W,p);
    2989       12460 :     if (typ(s) != t_MAT) return eval_single(s, k, B, v);
    2990         812 :     l = lg(s); A = cgetg(l, t_VEC);
    2991         812 :     for (i = 1; i < l; i++) gel(A,i) = eval_single(gel(s,i), k, B, v);
    2992             :   }
    2993        4179 :   return A;
    2994             : }
    2995             : 
    2996             : /* express symbol on the basis phi_{i,j} */
    2997             : static GEN
    2998       20524 : symtophi(GEN W, GEN s)
    2999             : {
    3000       20524 :   GEN e, basis = msk_get_basis(W);
    3001       20524 :   long i, l = lg(basis);
    3002       20524 :   if (lg(s) != l) pari_err_TYPE("mseval",s);
    3003       20524 :   e = zerovec(ms_get_nbgen(W));
    3004      312914 :   for (i=1; i<l; i++)
    3005             :   {
    3006      292390 :     GEN phi, ind, pols, c = gel(s,i);
    3007             :     long j, m;
    3008      292390 :     if (gequal0(c)) continue;
    3009      122528 :     phi = gel(basis,i);
    3010      122528 :     ind = gel(phi,2); m = lg(ind);
    3011      122528 :     pols = gel(phi,3);
    3012      470470 :     for (j=1; j<m; j++)
    3013             :     {
    3014      347942 :       long t = ind[j];
    3015      347942 :       gel(e,t) = gadd(gel(e,t), gmul(c, gel(pols,j)));
    3016             :     }
    3017             :   }
    3018       20524 :   return e;
    3019             : }
    3020             : /* evaluate symbol s on path p */
    3021             : GEN
    3022       16863 : mseval(GEN W, GEN s, GEN p)
    3023             : {
    3024       16863 :   pari_sp av = avma;
    3025       16863 :   long i, k, l, v = 0;
    3026       16863 :   checkms(W);
    3027       16863 :   k = msk_get_weight(W);
    3028       16863 :   switch(typ(s))
    3029             :   {
    3030             :     case t_VEC: /* values s(g_i) */
    3031           7 :       if (lg(s)-1 != ms_get_nbgen(W)) pari_err_TYPE("mseval",s);
    3032           7 :       if (!p) return gcopy(s);
    3033           0 :       v = gvar(s);
    3034           0 :       break;
    3035             :     case t_COL:
    3036       12663 :       if (msk_get_sign(W))
    3037             :       {
    3038         357 :         GEN star = gel(msk_get_starproj(W), 1);
    3039         357 :         if (lg(star) == lg(s)) s = RgM_RgC_mul(star, s);
    3040             :       }
    3041       12663 :       if (k == 2) /* on the dual basis of (g_i) */
    3042             :       {
    3043         637 :         if (lg(s)-1 != ms_get_nbE1(W)) pari_err_TYPE("mseval",s);
    3044         637 :         if (!p) return gtrans(s);
    3045             :       }
    3046             :       else
    3047       12026 :         s = symtophi(W,s);
    3048       12103 :       break;
    3049             :     case t_MAT:
    3050        4193 :       l = lg(s);
    3051        4193 :       if (!p)
    3052             :       {
    3053           7 :         GEN v = cgetg(l, t_VEC);
    3054           7 :         for (i = 1; i < l; i++) gel(v,i) = mseval(W, gel(s,i), NULL);
    3055           7 :         return v;
    3056             :       }
    3057        4186 :       if (l == 1) return cgetg(1, t_VEC);
    3058        4179 :       if (msk_get_sign(W))
    3059             :       {
    3060          84 :         GEN star = gel(msk_get_starproj(W), 1);
    3061          84 :         if (lg(star) == lgcols(s)) s = RgM_mul(star, s);
    3062             :       }
    3063        4179 :       if (k == 2)
    3064        3367 :       { if (nbrows(s) != ms_get_nbE1(W)) pari_err_TYPE("mseval",s); }
    3065             :       else
    3066             :       {
    3067         812 :         GEN t = cgetg(l, t_MAT);
    3068         812 :         for (i = 1; i < l; i++) gel(t,i) = symtophi(W,gel(s,i));
    3069         812 :         s = t;
    3070             :       }
    3071        4179 :       break;
    3072           0 :     default: pari_err_TYPE("mseval",s);
    3073             :   }
    3074       16282 :   if (p)
    3075       15904 :     s = mseval_by_values(W, s, p, v);
    3076             :   else
    3077             :   {
    3078         378 :     l = lg(s);
    3079        3675 :     for (i = 1; i < l; i++)
    3080             :     {
    3081        3297 :       GEN c = gel(s,i);
    3082        3297 :       if (!isintzero(c)) gel(s,i) = RgV_to_RgX(gel(s,i), v);
    3083             :     }
    3084             :   }
    3085       16275 :   return gerepilecopy(av, s);
    3086             : }
    3087             : 
    3088             : static GEN
    3089        1001 : allxpm(GEN W, GEN xpm, long f)
    3090             : {
    3091        1001 :   GEN v, L = coprimes_zv(f);
    3092        1001 :   long a, nonzero = 0;
    3093        1001 :   v = const_vec(f, NULL);
    3094        3815 :   for (a = 1; a <= f; a++)
    3095             :   {
    3096             :     GEN c;
    3097        2814 :     if (!L[a]) continue;
    3098        2065 :     c = Q_xpm(W, xpm, sstoQ(a, f));
    3099        2065 :     if (!gequal0(c)) { gel(v,a) = c; nonzero = 1; }
    3100             :   }
    3101        1001 :   return nonzero? v: NULL;
    3102             : }
    3103             : /* \sum_{a mod f} chi(a) x(a/f) */
    3104             : static GEN
    3105         483 : seval(GEN G, GEN chi, GEN vx)
    3106             : {
    3107         483 :   GEN vZ, T, s = gen_0, go = zncharorder(G,chi);
    3108         483 :   long i, l = lg(vx), o = itou(go);
    3109         483 :   T = polcyclo(o,0);
    3110         483 :   vZ = mkvec2(RgXQ_powers(RgX_rem(pol_x(0), T), o-1, T), go);
    3111        1939 :   for (i = 1; i < l; i++)
    3112             :   {
    3113        1456 :     GEN x = gel(vx,i);
    3114        1456 :     if (x) s = gadd(s, gmul(x, znchareval(G, chi, utoi(i), vZ)));
    3115             :   }
    3116         483 :   return gequal0(s)? NULL: poleval(s, rootsof1u_cx(o, DEFAULTPREC));
    3117             : }
    3118             : 
    3119             : static long
    3120         315 : nb_components(GEN E) { return signe(ell_get_disc(E)) > 0? 2: 1; }
    3121             : /* E minimal */
    3122             : static GEN
    3123         861 : ellperiod(GEN E, long s)
    3124             : {
    3125         861 :   GEN w = ellR_omega(E,DEFAULTPREC);
    3126         861 :   if (s == 1)
    3127         546 :     w = gel(w,1);
    3128         315 :   else if (nb_components(E) == 2)
    3129         140 :     w = gneg(gel(w,2));
    3130             :   else
    3131         175 :     w = mkcomplex(gen_0, gneg(gmul2n(imag_i(gel(w,2)), 1)));
    3132         861 :   return w;
    3133             : }
    3134             : 
    3135             : /* Let W = msinit(conductor(E), 2), xpm an integral modular symbol with the same
    3136             :  * eigenvalues as L_E. There exist a unique C such that
    3137             :  *   C*L(E,(D/.),1)_{xpm} = L(E,(D/.),1) / w1(E_D) != 0, for all D fundamental,
    3138             :  * sign(D) = s, and such that E_D has rank 0. Return C * ellperiod(E,s) */
    3139             : static GEN
    3140         483 : ell_get_Cw(GEN LE, GEN W, GEN xpm, long s)
    3141             : {
    3142         483 :   long f, NE = ms_get_N(W);
    3143         483 :   const long bit = 64;
    3144             : 
    3145        1456 :   for (f = 1;; f++)
    3146         973 :   { /* look for chi with conductor f coprime to N(E) and parity s
    3147             :      * such that L(E,chi,1) != 0 */
    3148        1456 :     pari_sp av = avma;
    3149             :     GEN vchi, vx, G;
    3150             :     long l, i;
    3151        1456 :     if ((f & 3) == 2 || ugcd(NE,f) != 1) continue;
    3152        1001 :     vx = allxpm(W, xpm, f); if (!vx) continue;
    3153         483 :     G = znstar0(utoipos(f),1);
    3154         483 :     vchi = chargalois(G,NULL); l = lg(vchi);
    3155         826 :     for (i = 1; i < l; i++)
    3156             :     {
    3157         826 :       pari_sp av2 = avma;
    3158         826 :       GEN tau, z, S, L, chi = gel(vchi,i);
    3159         826 :       long o = zncharisodd(G,chi);
    3160         826 :       if ((s > 0 && o) || (s < 0 && !o)
    3161         595 :           || itos(zncharconductor(G, chi)) != f) continue;
    3162         483 :       S = seval(G, chi, vx);
    3163         483 :       if (!S) { set_avma(av2); continue; }
    3164             : 
    3165         483 :       L = lfuntwist(LE, mkvec2(G, zncharconj(G,chi)));
    3166         483 :       z = lfun(L, gen_1, bit);
    3167         483 :       tau = znchargauss(G, chi, gen_1, bit);
    3168         966 :       return gdiv(gmul(z, tau), S); /* C * w */
    3169             :     }
    3170           0 :     set_avma(av);
    3171             :   }
    3172             : }
    3173             : static GEN
    3174         378 : ell_get_scale(GEN LE, GEN W, long sign, GEN x)
    3175             : {
    3176         378 :   if (sign)
    3177         273 :     return ell_get_Cw(LE, W, gel(x,1), sign);
    3178             :   else
    3179             :   {
    3180         105 :     GEN Cwp = ell_get_Cw(LE, W, gel(x,1), 1);
    3181         105 :     GEN Cwm = ell_get_Cw(LE, W, gel(x,2),-1);
    3182         105 :     return mkvec2(Cwp, Cwm);
    3183             :   }
    3184             : }
    3185             : /* E minimal */
    3186             : static GEN
    3187         567 : msfromell_scale(GEN x, GEN Cw, GEN E, long s)
    3188             : {
    3189         567 :   GEN B = int2n(32);
    3190         567 :   if (s)
    3191             :   {
    3192         273 :     GEN C = gdiv(Cw, ellperiod(E,s));
    3193         273 :     return ZC_Q_mul(gel(x,1), bestappr(C,B));
    3194             :   }
    3195             :   else
    3196             :   {
    3197         294 :     GEN xp = gel(x,1), Cp = gdiv(gel(Cw,1), ellperiod(E, 1)), L;
    3198         294 :     GEN xm = gel(x,2), Cm = gdiv(gel(Cw,2), ellperiod(E,-1));
    3199         294 :     xp = ZC_Q_mul(xp, bestappr(Cp,B));
    3200         294 :     xm = ZC_Q_mul(xm, bestappr(Cm,B));
    3201         294 :     if (signe(ell_get_disc(E)) > 0)
    3202         133 :       L = mkmat2(xp, xm); /* E(R) has 2 connected components */
    3203             :     else
    3204         161 :       L = mkmat2(gsub(xp,xm), gmul2n(xm,1));
    3205         294 :     return mkvec3(xp, xm, L);
    3206             :   }
    3207             : }
    3208             : /* v != 0 */
    3209             : static GEN
    3210         485 : Flc_normalize(GEN v, ulong p)
    3211             : {
    3212         485 :   long i, l = lg(v);
    3213         845 :   for (i = 1; i < l; i++)
    3214         845 :     if (v[i])
    3215             :     {
    3216         485 :       if (v[i] != 1) v = Flv_Fl_div(v, v[i], p);
    3217         485 :       return v;
    3218             :     }
    3219           0 :   return NULL;
    3220             : }
    3221             : /* K \cap Ker M  [F_l vector spaces]. K = NULL means full space */
    3222             : static GEN
    3223         415 : msfromell_ker(GEN K, GEN M, ulong l)
    3224             : {
    3225         415 :   GEN B, Ml = ZM_to_Flm(M, l);
    3226         415 :   if (K) Ml = Flm_mul(Ml, K, l);
    3227         415 :   B = Flm_ker(Ml, l);
    3228         415 :   if (!K) K = B;
    3229          36 :   else if (lg(B) < lg(K))
    3230          36 :     K = Flm_mul(K, B, l);
    3231         415 :   return K;
    3232             : }
    3233             : /* K = \cap_p Ker(T_p - a_p), 2-dimensional. Set *xl to the 1-dimensional
    3234             :  * Fl-basis  such that star . xl = sign . xl if sign != 0 and
    3235             :  * star * xl[1] = xl[1]; star * xl[2] = -xl[2] if sign = 0 */
    3236             : static void
    3237         379 : msfromell_l(GEN *pxl, GEN K, GEN star, long sign, ulong l)
    3238             : {
    3239         379 :   GEN s = ZM_to_Flm(star, l);
    3240         379 :   GEN a = gel(K,1), Sa = Flm_Flc_mul(s,a,l);
    3241         379 :   GEN b = gel(K,2);
    3242         379 :   GEN t = Flv_add(a,Sa,l), xp, xm;
    3243         379 :   if (zv_equal0(t))
    3244             :   {
    3245          14 :     xm = a;
    3246          14 :     xp = Flv_add(b,Flm_Flc_mul(s,b,l), l);
    3247             :   }
    3248             :   else
    3249             :   {
    3250         365 :     xp = t; t = Flv_sub(a, Sa, l);
    3251         365 :     xm = zv_equal0(t)? Flv_sub(b, Flm_Flc_mul(s,b,l), l): t;
    3252             :   }
    3253             :   /* xp = 0 on Im(S - 1), xm = 0 on Im(S + 1) */
    3254         379 :   if (sign > 0)
    3255         252 :     *pxl = mkmat(Flc_normalize(xp, l));
    3256         127 :   else if (sign < 0)
    3257          21 :     *pxl = mkmat(Flc_normalize(xm, l));
    3258             :   else
    3259         106 :     *pxl = mkmat2(Flc_normalize(xp, l), Flc_normalize(xm, l));
    3260         379 : }
    3261             : /* return a primitive symbol */
    3262             : static GEN
    3263         379 : msfromell_ratlift(GEN x, GEN q)
    3264             : {
    3265         379 :   GEN B = sqrti(shifti(q,-1));
    3266         379 :   GEN r = FpM_ratlift(x, q, B, B, NULL);
    3267         379 :   if (r) r = Q_primpart(r);
    3268         379 :   return r;
    3269             : }
    3270             : static int
    3271         379 : msfromell_check(GEN x, GEN vT, GEN star, long sign)
    3272             : {
    3273             :   long i, l;
    3274             :   GEN sx;
    3275         379 :   if (!x) return 0;
    3276         378 :   l = lg(vT);
    3277         791 :   for (i = 1; i < l; i++)
    3278             :   {
    3279         413 :     GEN T = gel(vT,i);
    3280         413 :     if (!gequal0(ZM_mul(T, x))) return 0; /* fail */
    3281             :   }
    3282         378 :   sx = ZM_mul(star,x);
    3283         378 :   if (sign)
    3284         273 :     return ZV_equal(gel(sx,1), sign > 0? gel(x,1): ZC_neg(gel(x,1)));
    3285             :   else
    3286         105 :     return ZV_equal(gel(sx,1),gel(x,1)) && ZV_equal(gel(sx,2),ZC_neg(gel(x,2)));
    3287             : }
    3288             : GEN
    3289         378 : msfromell(GEN E0, long sign)
    3290             : {
    3291         378 :   pari_sp av = avma, av2;
    3292         378 :   GEN T, Cw, E, NE, star, q, vT, xl, xr, W, x = NULL, K = NULL;
    3293             :   long lE, single;
    3294             :   ulong p, l, N;
    3295             :   forprime_t S, Sl;
    3296             : 
    3297         378 :   if (typ(E0) != t_VEC) pari_err_TYPE("msfromell",E0);
    3298         378 :   lE = lg(E0);
    3299         378 :   if (lE == 1) return cgetg(1,t_VEC);
    3300         378 :   single = (typ(gel(E0,1)) != t_VEC);
    3301         378 :   E = single ? E0: gel(E0,1);
    3302         378 :   NE = ellQ_get_N(E);
    3303             :   /* must make it integral for ellap; we have minimal model at hand */
    3304         378 :   T = obj_check(E, Q_MINIMALMODEL); if (lg(T) != 2) E = gel(T,3);
    3305         378 :   N = itou(NE); av2 = avma;
    3306         378 :   W = gerepilecopy(av2, mskinit(N,2,0));
    3307         378 :   star = msk_get_star(W);
    3308         378 :   init_modular_small(&Sl);
    3309             :   /* loop for p <= count_Manin_symbols(N) / 6 would be enough */
    3310         378 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
    3311         378 :   vT = cgetg(1, t_VEC);
    3312         378 :   l = u_forprime_next(&Sl);
    3313         378 :   while( (p = u_forprime_next(&S)) )
    3314             :   {
    3315             :     GEN M;
    3316         518 :     if (N % p == 0) continue;
    3317         413 :     av2 = avma;
    3318         413 :     M = RgM_Rg_sub_shallow(mshecke_i(W, p), ellap(E, utoipos(p)));
    3319         413 :     M = gerepilecopy(av2, M);
    3320         413 :     vT = shallowconcat(vT, mkvec(M)); /* for certification at the end */
    3321         413 :     K = msfromell_ker(K, M, l);
    3322         413 :     if (lg(K) == 3) break;
    3323             :   }
    3324         378 :   if (!p) pari_err_BUG("msfromell: ran out of primes");
    3325             : 
    3326             :   /* mod one l should be enough */
    3327         378 :   msfromell_l(&xl, K, star, sign, l);
    3328         378 :   x = ZM_init_CRT(xl, l);
    3329         378 :   q = utoipos(l);
    3330         378 :   xr = msfromell_ratlift(x, q);
    3331             :   /* paranoia */
    3332         757 :   while (!msfromell_check(xr, vT, star, sign) && (l = u_forprime_next(&Sl)) )
    3333             :   {
    3334           1 :     GEN K = NULL;
    3335           1 :     long i, lvT = lg(vT);
    3336           2 :     for (i = 1; i < lvT; i++)
    3337             :     {
    3338           2 :       K = msfromell_ker(K, gel(vT,i), l);
    3339           2 :       if (lg(K) == 3) break;
    3340             :     }
    3341           1 :     if (i >= lvT) { x = NULL; continue; }
    3342           1 :     msfromell_l(&xl, K, star, sign, l);
    3343           1 :     ZM_incremental_CRT(&x, xl, &q, l);
    3344           1 :     xr = msfromell_ratlift(x, q);
    3345             :   }
    3346             :   /* linear form = 0 on all Im(Tp - ap) and Im(S - sign) if sign != 0 */
    3347         378 :   Cw = ell_get_scale(lfuncreate(E), W, sign, xr);
    3348         378 :   if (single)
    3349         329 :     x = msfromell_scale(xr, Cw, E, sign);
    3350             :   else
    3351             :   { /* assume all E0[i] isogenous, given by minimal models */
    3352          49 :     GEN v = cgetg(lE, t_VEC);
    3353             :     long i;
    3354          49 :     for (i=1; i<lE; i++) gel(v,i) = msfromell_scale(xr, Cw, gel(E0,i), sign);
    3355          49 :     x = v;
    3356             :   }
    3357         378 :   return gerepilecopy(av, mkvec2(W, x));
    3358             : }
    3359             : 
    3360             : GEN
    3361          21 : msfromhecke(GEN W, GEN v, GEN H)
    3362             : {
    3363          21 :   pari_sp av = avma;
    3364          21 :   long i, l = lg(v);
    3365          21 :   GEN K = NULL;
    3366          21 :   checkms(W);
    3367          21 :   if (typ(v) != t_VEC) pari_err_TYPE("msfromhecke",v);
    3368          49 :   for (i = 1; i < l; i++)
    3369             :   {
    3370          28 :     GEN K2, T, p, P, c = gel(v,i);
    3371          28 :     if (typ(c) != t_VEC || lg(c) != 3) pari_err_TYPE("msfromhecke",v);
    3372          28 :     p = gel(c,1);
    3373          28 :     if (typ(p) != t_INT) pari_err_TYPE("msfromhecke",v);
    3374          28 :     P = gel(c,2);
    3375          28 :     switch(typ(P))
    3376             :     {
    3377             :       case t_INT:
    3378          21 :         P = deg1pol_shallow(gen_1, negi(P), 0);
    3379          21 :         break;
    3380             :       case t_POL:
    3381           7 :         if (RgX_is_ZX(P)) break;
    3382             :       default:
    3383           0 :         pari_err_TYPE("msfromhecke",v);
    3384             :     };
    3385          28 :     T = mshecke(W, itos(p), H);
    3386          28 :     T = Q_primpart(RgX_RgM_eval(P, T));
    3387          28 :     if (K) T = ZM_mul(T,K);
    3388          28 :     K2 = ZM_ker(T);
    3389          28 :     if (!K) K = K2;
    3390           7 :     else if (lg(K2) < lg(K)) K = ZM_mul(K,K2);
    3391             :   }
    3392          21 :   return gerepilecopy(av, K);
    3393             : }
    3394             : 
    3395             : /* OVERCONVERGENT MODULAR SYMBOLS */
    3396             : 
    3397             : static GEN
    3398        2933 : mspadic_get_Wp(GEN W) { return gel(W,1); }
    3399             : static GEN
    3400         483 : mspadic_get_Tp(GEN W) { return gel(W,2); }
    3401             : static GEN
    3402         483 : mspadic_get_bin(GEN W) { return gel(W,3); }
    3403             : static GEN
    3404         476 : mspadic_get_actUp(GEN W) { return gel(W,4); }
    3405             : static GEN
    3406         476 : mspadic_get_q(GEN W) { return gel(W,5); }
    3407             : static long
    3408        1456 : mspadic_get_p(GEN W) { return gel(W,6)[1]; }
    3409             : static long
    3410        1211 : mspadic_get_n(GEN W) { return gel(W,6)[2]; }
    3411             : static long
    3412         161 : mspadic_get_flag(GEN W) { return gel(W,6)[3]; }
    3413             : static GEN
    3414         483 : mspadic_get_M(GEN W) { return gel(W,7); }
    3415             : static GEN
    3416         483 : mspadic_get_C(GEN W) { return gel(W,8); }
    3417             : static long
    3418         973 : mspadic_get_weight(GEN W) { return msk_get_weight(mspadic_get_Wp(W)); }
    3419             : 
    3420             : void
    3421         980 : checkmspadic(GEN W)
    3422             : {
    3423         980 :   if (typ(W) != t_VEC || lg(W) != 9) pari_err_TYPE("checkmspadic",W);
    3424         980 :   checkms(mspadic_get_Wp(W));
    3425         980 : }
    3426             : 
    3427             : /* f in M_2(Z) \cap GL_2(Q), p \nmid a [ and for the result to mean anything
    3428             :  * p | c, but not needed here]. Return the matrix M in M_D(Z), D = M+k-1
    3429             :  * such that, if v = \int x^i d mu, i < D, is a vector of D moments of mu,
    3430             :  * then M * v is the vector of moments of mu | f  mod p^D */
    3431             : static GEN
    3432      276073 : moments_act_i(struct m_act *S, GEN f)
    3433             : {
    3434      276073 :   long j, k = S->k, D = S->dim;
    3435      276073 :   GEN a = gcoeff(f,1,1), b = gcoeff(f,1,2);
    3436      276073 :   GEN c = gcoeff(f,2,1), d = gcoeff(f,2,2);
    3437      276073 :   GEN u, z, q = S->q, mat = cgetg(D+1, t_MAT);
    3438             : 
    3439      276073 :   a = modii(a,q);
    3440      276073 :   c = modii(c,q);
    3441      276073 :   z = FpX_powu(deg1pol(c,a,0), k-2, q); /* (a+cx)^(k-2) */
    3442             :   /* u := (b+dx) / (a+cx) mod (q,x^D) = (b/a +d/a*x) / (1 - (-c/a)*x) */
    3443      276073 :   if (!equali1(a))
    3444             :   {
    3445      271229 :     GEN ai = Fp_inv(a,q);
    3446      271229 :     b = Fp_mul(b,ai,q);
    3447      271229 :     c = Fp_mul(c,ai,q);
    3448      271229 :     d = Fp_mul(d,ai,q);
    3449             :   }
    3450      276073 :   u = deg1pol_shallow(d, b, 0);
    3451             :   /* multiply by 1 / (1 - (-c/a)*x) */
    3452      276073 :   if (signe(c))
    3453             :   {
    3454      269640 :     GEN C = Fp_neg(c,q), v = cgetg(D+2,t_POL);
    3455      269640 :     v[1] = evalsigne(1)|evalvarn(0);
    3456      269640 :     gel(v, 2) = gen_1; gel(v, 3) = C;
    3457     1405138 :     for (j = 4; j < D+2; j++)
    3458             :     {
    3459     1329027 :       GEN t = Fp_mul(gel(v,j-1), C, q);
    3460     1329027 :       if (!signe(t)) { setlg(v,j); break; }
    3461     1135498 :       gel(v,j) = t;
    3462             :     }
    3463      269640 :     u = FpXn_mul(u, v, D, q);
    3464             :   }
    3465     2369024 :   for (j = 1; j <= D; j++)
    3466             :   {
    3467     2092951 :     gel(mat,j) = RgX_to_RgC(z, D); /* (a+cx)^(k-2) * ((b+dx)/(a+cx))^(j-1) */
    3468     2092951 :     if (j != D) z = FpXn_mul(z, u, D, q);
    3469             :   }
    3470      276073 :   return shallowtrans(mat);
    3471             : }
    3472             : static GEN
    3473      275611 : moments_act(struct m_act *S, GEN f)
    3474      275611 : { pari_sp av = avma; return gerepilecopy(av, moments_act_i(S,f)); }
    3475             : static GEN
    3476         483 : init_moments_act(GEN W, long p, long n, GEN q, GEN v)
    3477             : {
    3478             :   struct m_act S;
    3479         483 :   long k = msk_get_weight(W);
    3480         483 :   S.p = p;
    3481         483 :   S.k = k;
    3482         483 :   S.q = q;
    3483         483 :   S.dim = n+k-1;
    3484         483 :   S.act = &moments_act; return init_dual_act(v,W,W,&S);
    3485             : }
    3486             : 
    3487             : static void
    3488        6762 : clean_tail(GEN phi, long c, GEN q)
    3489             : {
    3490        6762 :   long a, l = lg(phi);
    3491      214438 :   for (a = 1; a < l; a++)
    3492             :   {
    3493      207676 :     GEN P = FpV_red(gel(phi,a), q); /* phi(G_a) = vector of moments */
    3494      207676 :     long j, lP = lg(P);
    3495      207676 :     for (j = c; j < lP; j++) gel(P,j) = gen_0; /* reset garbage to 0 */
    3496      207676 :     gel(phi,a) = P;
    3497             :   }
    3498        6762 : }
    3499             : /* concat z to all phi[i] */
    3500             : static GEN
    3501         630 : concat2(GEN phi, GEN z)
    3502             : {
    3503             :   long i, l;
    3504         630 :   GEN v = cgetg_copy(phi,&l);
    3505         630 :   for (i = 1; i < l; i++) gel(v,i) = shallowconcat(gel(phi,i), z);
    3506         630 :   return v;
    3507             : }
    3508             : static GEN
    3509         630 : red_mod_FilM(GEN phi, ulong p, long k, long flag)
    3510             : {
    3511             :   long a, l;
    3512         630 :   GEN den = gen_1, v = cgetg_copy(phi, &l);
    3513         630 :   if (flag)
    3514             :   {
    3515         343 :     phi = Q_remove_denom(phi, &den);
    3516         343 :     if (!den) { den = gen_1; flag = 0; }
    3517             :   }
    3518       29386 :   for (a = 1; a < l; a++)
    3519             :   {
    3520       28756 :     GEN P = gel(phi,a), q = den;
    3521             :     long j;
    3522      207676 :     for (j = lg(P)-1; j >= k+1; j--)
    3523             :     {
    3524      178920 :       q = muliu(q,p);
    3525      178920 :       gel(P,j) = modii(gel(P,j),q);
    3526             :     }
    3527       28756 :     q = muliu(q,p);
    3528       93380 :     for (     ; j >= 1; j--)
    3529       64624 :       gel(P,j) = modii(gel(P,j),q);
    3530       28756 :     gel(v,a) = P;
    3531             :   }
    3532         630 :   if (flag) v = gdiv(v, den);
    3533         630 :   return v;
    3534             : }
    3535             : 
    3536             : /* denom(C) | p^(2(k-1) - v_p(ap)) */
    3537             : static GEN
    3538         154 : oms_dim2(GEN W, GEN phi, GEN C, GEN ap)
    3539             : {
    3540         154 :   long t, i, k = mspadic_get_weight(W);
    3541         154 :   long p = mspadic_get_p(W), n = mspadic_get_n(W);
    3542         154 :   GEN phi1 = gel(phi,1), phi2 = gel(phi,2);
    3543         154 :   GEN v, q = mspadic_get_q(W);
    3544         154 :   GEN act = mspadic_get_actUp(W);
    3545             : 
    3546         154 :   t = signe(ap)? Z_lval(ap,p) : k-1;
    3547         154 :   phi1 = concat2(phi1, zerovec(n));
    3548         154 :   phi2 = concat2(phi2, zerovec(n));
    3549        2107 :   for (i = 1; i <= n; i++)
    3550             :   {
    3551        1953 :     phi1 = dual_act(k-1, act, phi1);
    3552        1953 :     phi1 = dual_act(k-1, act, phi1);
    3553        1953 :     clean_tail(phi1, k + i*t, q);
    3554             : 
    3555        1953 :     phi2 = dual_act(k-1, act, phi2);
    3556        1953 :     phi2 = dual_act(k-1, act, phi2);
    3557        1953 :     clean_tail(phi2, k + i*t, q);
    3558             :   }
    3559         154 :   C = gpowgs(C,n);
    3560         154 :   v = RgM_RgC_mul(C, mkcol2(phi1,phi2));
    3561         154 :   phi1 = red_mod_FilM(gel(v,1), p, k, 1);
    3562         154 :   phi2 = red_mod_FilM(gel(v,2), p, k, 1);
    3563         154 :   return mkvec2(phi1,phi2);
    3564             : }
    3565             : 
    3566             : /* flag = 0 iff alpha is a p-unit */
    3567             : static GEN
    3568         322 : oms_dim1(GEN W, GEN phi, GEN alpha, long flag)
    3569             : {
    3570         322 :   long i, k = mspadic_get_weight(W);
    3571         322 :   long p = mspadic_get_p(W), n = mspadic_get_n(W);
    3572         322 :   GEN q = mspadic_get_q(W);
    3573         322 :   GEN act = mspadic_get_actUp(W);
    3574         322 :   phi = concat2(phi, zerovec(n));
    3575        3178 :   for (i = 1; i <= n; i++)
    3576             :   {
    3577        2856 :     phi = dual_act(k-1, act, phi);
    3578        2856 :     clean_tail(phi, k + i, q);
    3579             :   }
    3580         322 :   phi = gmul(lift_shallow(gpowgs(alpha,n)), phi);
    3581         322 :   phi = red_mod_FilM(phi, p, k, flag);
    3582         322 :   return mkvec(phi);
    3583             : }
    3584             : 
    3585             : /* lift polynomial P in RgX[X,Y]_{k-2} to a distribution \mu such that
    3586             :  * \int (Y - X z)^(k-2) d\mu(z) = P(X,Y)
    3587             :  * Return the t_VEC of k-1 first moments of \mu: \int z^i d\mu(z), 0<= i < k-1.
    3588             :  *   \sum_j (-1)^(k-2-j) binomial(k-2,j) Y^j \int z^(k-2-j) d\mu(z) = P(1,Y)
    3589             :  * Input is P(1,Y), bin = vecbinomial(k-2): bin[j] = binomial(k-2,j-1) */
    3590             : static GEN
    3591       38626 : RgX_to_moments(GEN P, GEN bin)
    3592             : {
    3593       38626 :   long j, k = lg(bin);
    3594             :   GEN Pd, Bd;
    3595       38626 :   if (typ(P) != t_POL) P = scalarpol(P,0);
    3596       38626 :   P = RgX_to_RgC(P, k-1); /* deg <= k-2 */
    3597       38626 :   settyp(P, t_VEC);
    3598       38626 :   Pd = P+1;  /* Pd[i] = coeff(P,i) */
    3599       38626 :   Bd = bin+1;/* Bd[i] = binomial(k-2,i) */
    3600       46249 :   for (j = 1; j < k-2; j++)
    3601             :   {
    3602        7623 :     GEN c = gel(Pd,j);
    3603        7623 :     if (odd(j)) c = gneg(c);
    3604        7623 :     gel(Pd,j) = gdiv(c, gel(Bd,j));
    3605             :   }
    3606       38626 :   return vecreverse(P);
    3607             : }
    3608             : static GEN
    3609         882 : RgXC_to_moments(GEN v, GEN bin)
    3610             : {
    3611             :   long i, l;
    3612         882 :   GEN w = cgetg_copy(v,&l);
    3613         882 :   for (i=1; i<l; i++) gel(w,i) = RgX_to_moments(gel(v,i),bin);
    3614         882 :   return w;
    3615             : }
    3616             : 
    3617             : /* W an mspadic, assume O[2] is integral, den is the cancelled denominator
    3618             :  * or NULL, L = log(path)^* in sparse form */
    3619             : static GEN
    3620        2954 : omseval_int(struct m_act *S, GEN PHI, GEN L, hashtable *H)
    3621             : {
    3622             :   long i, l;
    3623        2954 :   GEN v = cgetg_copy(PHI, &l);
    3624        2954 :   ZGl2QC_to_act(S, L, H); /* as operators on V */
    3625        6286 :   for (i = 1; i < l; i++)
    3626             :   {
    3627        3332 :     GEN T = dense_act_col(L, gel(PHI,i));
    3628        3332 :     gel(v,i) = T? FpC_red(T,S->q): zerocol(S->dim);
    3629             :   }
    3630        2954 :   return v;
    3631             : }
    3632             : 
    3633             : GEN
    3634          14 : msomseval(GEN W, GEN phi, GEN path)
    3635             : {
    3636             :   struct m_act S;
    3637          14 :   pari_sp av = avma;
    3638             :   GEN v, Wp;
    3639             :   long n, vden;
    3640          14 :   checkmspadic(W);
    3641          14 :   if (typ(phi) != t_COL || lg(phi) != 4)  pari_err_TYPE("msomseval",phi);
    3642          14 :   vden = itos(gel(phi,2));
    3643          14 :   phi = gel(phi,1);
    3644          14 :   n = mspadic_get_n(W);
    3645          14 :   Wp= mspadic_get_Wp(W);
    3646          14 :   S.k = mspadic_get_weight(W);
    3647          14 :   S.p = mspadic_get_p(W);
    3648          14 :   S.q = powuu(S.p, n+vden);
    3649          14 :   S.dim = n + S.k - 1;
    3650          14 :   S.act = &moments_act;
    3651          14 :   path = path_to_M2(path);
    3652          14 :   v = omseval_int(&S, phi, M2_logf(Wp,path,NULL), NULL);
    3653          14 :   return gerepilecopy(av, v);
    3654             : }
    3655             : /* W = msinit(N,k,...); if flag < 0 or flag >= k-1, allow all symbols;
    3656             :  * else commit to v_p(a_p) <= flag (ordinary if flag = 0)*/
    3657             : GEN
    3658         490 : mspadicinit(GEN W, long p, long n, long flag)
    3659             : {
    3660         490 :   pari_sp av = avma;
    3661             :   long a, N, k;
    3662             :   GEN P, C, M, bin, Wp, Tp, q, pn, actUp, teich, pas;
    3663             : 
    3664         490 :   checkms(W);
    3665         490 :   N = ms_get_N(W);
    3666         490 :   k = msk_get_weight(W);
    3667         490 :   if (flag < 0) flag = 1; /* worst case */
    3668         357 :   else if (flag >= k) flag = k-1;
    3669             : 
    3670         490 :   bin = vecbinomial(k-2);
    3671         490 :   Tp = mshecke(W, p, NULL);
    3672         490 :   if (N % p == 0)
    3673             :   {
    3674          91 :     if ((N/p) % p == 0) pari_err_IMPL("mspadicinit when p^2 | N");
    3675             :     /* a_p != 0 */
    3676          84 :     Wp = W;
    3677          84 :     M = gen_0;
    3678          84 :     flag = (k-2) / 2; /* exact valuation */
    3679             :     /* will multiply by matrix with denominator p^(k-2)/2 in mspadicint.
    3680             :      * Except if p = 2 (multiply by alpha^2) */
    3681          84 :     if (p == 2) n += k-2; else n += (k-2)/2;
    3682          84 :     pn = powuu(p,n);
    3683             :     /* For accuracy mod p^n, oms_dim1 require p^(k/2*n) */
    3684          84 :     q = powiu(pn, k/2);
    3685             :   }
    3686             :   else
    3687             :   { /* p-stabilize */
    3688         399 :     long s = msk_get_sign(W);
    3689             :     GEN M1, M2;
    3690             : 
    3691         399 :     Wp = mskinit(N*p, k, s);
    3692         399 :     M1 = getMorphism(W, Wp, mkvec(mat2(1,0,0,1)));
    3693         399 :     M2 = getMorphism(W, Wp, mkvec(mat2(p,0,0,1)));
    3694         399 :     if (s)
    3695             :     {
    3696         147 :       GEN SW = msk_get_starproj(W), SWp = msk_get_starproj(Wp);
    3697         147 :       M1 = Qevproj_apply2(M1, SW, SWp);
    3698         147 :       M2 = Qevproj_apply2(M2, SW, SWp);
    3699             :     }
    3700         399 :     M = mkvec2(M1,M2);
    3701         399 :     n += Z_lval(Q_denom(M), p); /*den. introduced by p-stabilization*/
    3702             :     /* in supersingular case: will multiply by matrix with denominator p^k
    3703             :      * in mspadicint. Except if p = 2 (multiply by alpha^2) */
    3704         399 :     if (flag) { if (p == 2) n += 2*k-2; else n += k; }
    3705         399 :     pn = powuu(p,n);
    3706             :     /* For accuracy mod p^n, supersingular require p^((2k-1-v_p(a_p))*n) */
    3707         399 :     if (flag) /* k-1 also takes care of a_p = 0. Worst case v_p(a_p) = flag */
    3708         231 :       q = powiu(pn, 2*k-1 - flag);
    3709             :     else
    3710         168 :       q = pn;
    3711             :   }
    3712         483 :   actUp = init_moments_act(Wp, p, n, q, Up_matrices(p));
    3713             : 
    3714         483 :   if (p == 2) C = gen_0;
    3715             :   else
    3716             :   {
    3717         427 :     pas = matpascal(n);
    3718         427 :     teich = teichmullerinit(p, n+1);
    3719         427 :     P = gpowers(utoipos(p), n);
    3720         427 :     C = cgetg(p, t_VEC);
    3721        2317 :     for (a = 1; a < p; a++)
    3722             :     { /* powb[j+1] = ((a - w(a)) / p)^j mod p^n */
    3723        1890 :       GEN powb = Fp_powers(diviuexact(subui(a, gel(teich,a)), p), n, pn);
    3724        1890 :       GEN Ca = cgetg(n+2, t_VEC);
    3725        1890 :       long j, r, ai = Fl_inv(a, p); /* a^(-1) */
    3726        1890 :       gel(C,a) = Ca;
    3727       22134 :       for (j = 0; j <= n; j++)
    3728             :       {
    3729       20244 :         GEN Caj = cgetg(j+2, t_VEC);
    3730       20244 :         GEN atij = gel(teich, Fl_powu(ai,j,p));/* w(a)^(-j) = w(a^(-j) mod p) */
    3731       20244 :         gel(Ca,j+1) = Caj;
    3732      158200 :         for (r = 0; r <= j; r++)
    3733             :         {
    3734      137956 :           GEN c = Fp_mul(gcoeff(pas,j+1,r+1), gel(powb, j-r+1), pn);
    3735      137956 :           c = Fp_mul(c,atij,pn); /* binomial(j,r)*b^(j-r)*w(a)^(-j) mod p^n */
    3736      137956 :           gel(Caj,r+1) = mulii(c, gel(P,j+1)); /* p^j * c mod p^(n+j) */
    3737             :         }
    3738             :       }
    3739             :     }
    3740             :   }
    3741         483 :   return gerepilecopy(av, mkvecn(8, Wp,Tp, bin, actUp, q,
    3742             :                                  mkvecsmall3(p,n,flag), M, C));
    3743             : }
    3744             : 
    3745             : #if 0
    3746             : /* assume phi an ordinary OMS */
    3747             : static GEN
    3748             : omsactgl2(GEN W, GEN phi, GEN M)
    3749             : {
    3750             :   GEN q, Wp, act;
    3751             :   long p, k, n;
    3752             :   checkmspadic(W);
    3753             :   Wp = mspadic_get_Wp(W);
    3754             :   p = mspadic_get_p(W);
    3755             :   k = mspadic_get_weight(W);
    3756             :   n = mspadic_get_n(W);
    3757             :   q = mspadic_get_q(W);
    3758             :   act = init_moments_act(Wp, p, n, q, M);
    3759             :   phi = gel(phi,1);
    3760             :   return dual_act(k-1, act, gel(phi,1));
    3761             : }
    3762             : #endif
    3763             : 
    3764             : static GEN
    3765         483 : eigenvalue(GEN T, GEN x)
    3766             : {
    3767         483 :   long i, l = lg(x);
    3768         637 :   for (i = 1; i < l; i++)
    3769         637 :     if (!isintzero(gel(x,i))) break;
    3770         483 :   if (i == l) pari_err_DOMAIN("mstooms", "phi", "=", gen_0, x);
    3771         483 :   return gdiv(RgMrow_RgC_mul(T,x,i), gel(x,i));
    3772             : }
    3773             : 
    3774             : /* p coprime to ap, return unit root of x^2 - ap*x + p^(k-1), accuracy p^n */
    3775             : GEN
    3776         266 : mspadic_unit_eigenvalue(GEN ap, long k, GEN p, long n)
    3777             : {
    3778         266 :   GEN sqrtD, D = subii(sqri(ap), shifti(powiu(p,k-1),2));
    3779         266 :   if (absequaliu(p,2))
    3780             :   {
    3781           7 :     n++; sqrtD = Zp_sqrt(D, p, n);
    3782           7 :     if (mod4(sqrtD) != mod4(ap)) sqrtD = negi(sqrtD);
    3783             :   }
    3784             :   else
    3785         259 :     sqrtD = Zp_sqrtlift(D, ap, p, n);
    3786             :   /* sqrtD = ap (mod p) */
    3787         266 :   return gmul2n(gadd(ap, cvtop(sqrtD,p,n)), -1);
    3788             : }
    3789             : 
    3790             : /* W = msinit(N,k,...); phi = T_p/U_p - eigensymbol */
    3791             : GEN
    3792         483 : mstooms(GEN W, GEN phi)
    3793             : {
    3794         483 :   pari_sp av = avma;
    3795             :   GEN Wp, bin, Tp, c, alpha, ap, phi0, M;
    3796             :   long k, p, vden;
    3797             : 
    3798         483 :   checkmspadic(W);
    3799         483 :   if (typ(phi) != t_COL)
    3800             :   {
    3801         161 :     if (!is_Qevproj(phi)) pari_err_TYPE("mstooms",phi);
    3802         161 :     phi = gel(phi,1);
    3803         161 :     if (lg(phi) != 2) pari_err_TYPE("mstooms [dim_Q (eigenspace) > 1]",phi);
    3804         161 :     phi = gel(phi,1);
    3805             :   }
    3806             : 
    3807         483 :   Wp = mspadic_get_Wp(W);
    3808         483 :   Tp = mspadic_get_Tp(W);
    3809         483 :   bin = mspadic_get_bin(W);
    3810         483 :   k = msk_get_weight(Wp);
    3811         483 :   p = mspadic_get_p(W);
    3812         483 :   M = mspadic_get_M(W);
    3813             : 
    3814         483 :   phi = Q_remove_denom(phi, &c);
    3815         483 :   ap = eigenvalue(Tp, phi);
    3816         483 :   vden = c? Z_lvalrem(c, p, &c): 0;
    3817             : 
    3818         483 :   if (typ(M) == t_INT)
    3819             :   { /* p | N */
    3820             :     GEN c1;
    3821          84 :     alpha = ap;
    3822          84 :     alpha = ginv(alpha);
    3823          84 :     phi0 = mseval(Wp, phi, NULL);
    3824          84 :     phi0 = RgXC_to_moments(phi0, bin);
    3825          84 :     phi0 = Q_remove_denom(phi0, &c1);
    3826          84 :     if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3827          84 :     if (umodiu(ap,p)) /* p \nmid a_p */
    3828          49 :       phi = oms_dim1(W, phi0, alpha, 0);
    3829             :     else
    3830             :     {
    3831          35 :       phi = oms_dim1(W, phi0, alpha, 1);
    3832          35 :       phi = Q_remove_denom(phi, &c1);
    3833          35 :       if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3834             :     }
    3835             :   }
    3836             :   else
    3837             :   { /* p-stabilize */
    3838             :     GEN M1, M2, phi1, phi2, c1;
    3839         399 :     if (typ(M) != t_VEC || lg(M) != 3) pari_err_TYPE("mstooms",W);
    3840         399 :     M1 = gel(M,1);
    3841         399 :     M2 = gel(M,2);
    3842             : 
    3843         399 :     phi1 = RgM_RgC_mul(M1, phi);
    3844         399 :     phi2 = RgM_RgC_mul(M2, phi);
    3845         399 :     phi1 = mseval(Wp, phi1, NULL);
    3846         399 :     phi2 = mseval(Wp, phi2, NULL);
    3847             : 
    3848         399 :     phi1 = RgXC_to_moments(phi1, bin);
    3849         399 :     phi2 = RgXC_to_moments(phi2, bin);
    3850         399 :     phi = Q_remove_denom(mkvec2(phi1,phi2), &c1);
    3851         399 :     phi1 = gel(phi,1);
    3852         399 :     phi2 = gel(phi,2);
    3853         399 :     if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3854             :     /* all polynomials multiplied by c p^vden */
    3855         399 :     if (umodiu(ap, p))
    3856             :     {
    3857         238 :       alpha = mspadic_unit_eigenvalue(ap, k, utoipos(p), mspadic_get_n(W));
    3858         238 :       alpha = ginv(alpha);
    3859         238 :       phi0 = gsub(phi1, gmul(lift_shallow(alpha),phi2));
    3860         238 :       phi = oms_dim1(W, phi0, alpha, 0);
    3861             :     }
    3862             :     else
    3863             :     { /* p | ap, alpha = [a_p, -1; p^(k-1), 0] */
    3864         161 :       long flag = mspadic_get_flag(W);
    3865         161 :       if (!flag || (signe(ap) && Z_lval(ap,p) < flag))
    3866           7 :         pari_err_TYPE("mstooms [v_p(ap) > mspadicinit flag]", phi);
    3867         154 :       alpha = mkmat22(ap,gen_m1, powuu(p, k-1),gen_0);
    3868         154 :       alpha = ginv(alpha);
    3869         154 :       phi = oms_dim2(W, mkvec2(phi1,phi2), gsqr(alpha), ap);
    3870         154 :       phi = Q_remove_denom(phi, &c1);
    3871         154 :       if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3872             :     }
    3873             :   }
    3874         476 :   if (vden) c = mul_denom(c, powuu(p,vden));
    3875         476 :   if (p == 2) alpha = gsqr(alpha);
    3876         476 :   if (c) alpha = gdiv(alpha,c);
    3877         476 :   if (typ(alpha) == t_MAT)
    3878             :   { /* express in basis (omega,-p phi(omega)) */
    3879         154 :     gcoeff(alpha,2,1) = gdivgs(gcoeff(alpha,2,1), -p);
    3880         154 :     gcoeff(alpha,2,2) = gdivgs(gcoeff(alpha,2,2), -p);
    3881             :     /* at the end of mspadicint we shall multiply result by [1,0;0,-1/p]*alpha
    3882             :      * vden + k is the denominator of this matrix */
    3883             :   }
    3884             :   /* phi is integral-valued */
    3885         476 :   return gerepilecopy(av, mkcol3(phi, stoi(vden), alpha));
    3886             : }
    3887             : 
    3888             : /* HACK: the v[j] have different lengths */
    3889             : static GEN
    3890        2156 : FpVV_dotproduct(GEN v, GEN w, GEN p)
    3891             : {
    3892        2156 :   long j, l = lg(v);
    3893        2156 :   GEN T = cgetg(l, t_VEC);
    3894        2156 :   for (j = 1; j < l; j++) gel(T,j) = FpV_dotproduct(gel(v,j),w,p);
    3895        2156 :   return T;
    3896             : }
    3897             : 
    3898             : /* \int (-4z)^j given \int z^j */
    3899             : static GEN
    3900          98 : twistmoment_m4(GEN v)
    3901             : {
    3902             :   long i, l;
    3903          98 :   GEN w = cgetg_copy(v, &l);
    3904        2009 :   for (i = 1; i < l; i++)
    3905             :   {
    3906        1911 :     GEN c = gel(v,i);
    3907        1911 :     if (i > 1) c = gmul2n(c, (i-1)<<1);
    3908        1911 :     gel(w,i) = odd(i)? c: gneg(c);
    3909             :   }
    3910          98 :   return w;
    3911             : }
    3912             : /* \int (4z)^j given \int z^j */
    3913             : static GEN
    3914          98 : twistmoment_4(GEN v)
    3915             : {
    3916             :   long i, l;
    3917          98 :   GEN w = cgetg_copy(v, &l);
    3918        2009 :   for (i = 1; i < l; i++)
    3919             :   {
    3920        1911 :     GEN c = gel(v,i);
    3921        1911 :     if (i > 1) c = gmul2n(c, (i-1)<<1);
    3922        1911 :     gel(w,i) = c;
    3923             :   }
    3924          98 :   return w;
    3925             : }
    3926             : /* W an mspadic, phi eigensymbol, p \nmid D. Return C(x) mod FilM */
    3927             : GEN
    3928         483 : mspadicmoments(GEN W, GEN PHI, long D)
    3929             : {
    3930         483 :   pari_sp av = avma;
    3931         483 :   long na, ia, b, lphi, aD = labs(D), pp, p, k, n, vden;
    3932             :   GEN Wp, Dact, vL, v, C, pn, phi;
    3933             :   struct m_act S;
    3934             :   hashtable *H;
    3935             : 
    3936         483 :   checkmspadic(W);
    3937         483 :   Wp = mspadic_get_Wp(W);
    3938         483 :   p = mspadic_get_p(W);
    3939         483 :   k = mspadic_get_weight(W);
    3940         483 :   n = mspadic_get_n(W);
    3941         483 :   C = mspadic_get_C(W);
    3942         483 :   if (typ(PHI) != t_COL || lg(PHI) != 4 || typ(gel(PHI,1)) != t_VEC)
    3943         476 :     PHI = mstooms(W, PHI);
    3944         476 :   vden = itos( gel(PHI,2) );
    3945         476 :   phi = gel(PHI,1); lphi = lg(phi);
    3946         476 :   if (p == 2) { na = 2; pp = 4; }
    3947         420 :   else        { na = p-1; pp = p; }
    3948         476 :   pn = powuu(p, n + vden);
    3949             : 
    3950         476 :   S.p = p;
    3951         476 :   S.k = k;
    3952         476 :   S.q = pn;
    3953         476 :   S.dim = n+k-1;
    3954         476 :   S.act = &moments_act;
    3955         476 :   H = Gl2act_cache(ms_get_nbgen(Wp));
    3956         476 :   if (D == 1) Dact = NULL;
    3957             :   else
    3958             :   {
    3959          63 :     GEN gaD = utoi(aD), Dk = Fp_pows(stoi(D), 2-k, pn);
    3960          63 :     if (!sisfundamental(D)) pari_err_TYPE("mspadicmoments", stoi(D));
    3961          63 :     if (D % p == 0) pari_err_DOMAIN("mspadicmoments","p","|", stoi(D), utoi(p));
    3962          63 :     Dact = cgetg(aD, t_VEC);
    3963         532 :     for (b = 1; b < aD; b++)
    3964             :     {
    3965         469 :       GEN z = NULL;
    3966         469 :       long s = kross(D, b);
    3967         469 :       if (s)
    3968             :       {
    3969         462 :         pari_sp av2 = avma;
    3970             :         GEN d;
    3971         462 :         z = moments_act_i(&S, mkmat22(gaD,utoipos(b), gen_0,gaD));
    3972         462 :         d = s > 0? Dk: Fp_neg(Dk, pn);
    3973         924 :         z = equali1(d)? gerepilecopy(av2, z)
    3974         462 :                       : gerepileupto(av2, FpM_Fp_mul(z, d, pn));
    3975             :       }
    3976         469 :       gel(Dact,b) = z;
    3977             :     }
    3978             :   }
    3979         476 :   vL = cgetg(na+1,t_VEC);
    3980             :   /* first pass to precompute log(paths), preload matrices and allow GC later */
    3981        2464 :   for (ia = 1; ia <= na; ia++)
    3982             :   {
    3983             :     GEN path, La;
    3984        1988 :     long a = (p == 2 && ia == 2)? -1: ia;
    3985        1988 :     if (Dact)
    3986             :     { /* twist by D */
    3987         224 :       La = cgetg(aD, t_VEC);
    3988        1442 :       for (b = 1; b < aD; b++)
    3989             :       {
    3990        1218 :         GEN Actb = gel(Dact,b);
    3991        1218 :         if (!Actb) continue;
    3992             :         /* oo -> a/pp + b/|D|*/
    3993        1176 :         path = mkmat22(gen_1, addii(mulss(a, aD), muluu(pp, b)),
    3994             :                        gen_0, muluu(pp, aD));
    3995        1176 :         gel(La,b) = M2_logf(Wp,path,NULL);
    3996        1176 :         ZGl2QC_preload(&S, gel(La,b), H);
    3997             :       }
    3998             :     }
    3999             :     else
    4000             :     {
    4001        1764 :       path = mkmat22(gen_1,stoi(a), gen_0, utoipos(pp));
    4002        1764 :       La = M2_logf(Wp,path,NULL);
    4003        1764 :       ZGl2QC_preload(&S, La, H);
    4004             :     }
    4005        1988 :     gel(vL,ia) = La;
    4006             :   }
    4007         476 :   v = cgetg(na+1,t_VEC);
    4008             :   /* second pass, with GC */
    4009        2464 :   for (ia = 1; ia <= na; ia++)
    4010             :   {
    4011        1988 :     pari_sp av2 = avma;
    4012        1988 :     GEN vca, Ca = gel(C,ia), La = gel(vL,ia), va = cgetg(lphi, t_VEC);
    4013             :     long i;
    4014        1988 :     if (!Dact) vca = omseval_int(&S, phi, La, H);
    4015             :     else
    4016             :     { /* twist by D */
    4017         224 :       vca = cgetg(lphi,t_VEC);
    4018        1442 :       for (b = 1; b < aD; b++)
    4019             :       {
    4020        1218 :         GEN T, Actb = gel(Dact,b);
    4021        1218 :         if (!Actb) continue;
    4022        1176 :         T = omseval_int(&S, phi, gel(La,b), H);
    4023        2352 :         for (i = 1; i < lphi; i++)
    4024             :         {
    4025        1176 :           GEN z = FpM_FpC_mul(Actb, gel(T,i), pn);
    4026        1176 :           gel(vca,i) = b==1? z: ZC_add(gel(vca,i), z);
    4027             :         }
    4028             :       }
    4029             :     }
    4030        1988 :     if (p != 2)
    4031        1876 :     { for (i=1; i<lphi; i++) gel(va,i) = FpVV_dotproduct(Ca,gel(vca,i),pn); }
    4032         112 :     else if (ia == 1) /* \tilde{a} = 1 */
    4033          56 :     { for (i=1; i<lphi; i++) gel(va,i) = twistmoment_4(gel(vca,i)); }
    4034             :     else /* \tilde{a} = -1 */
    4035          56 :     { for (i=1; i<lphi; i++) gel(va,i) = twistmoment_m4(gel(vca,i)); }
    4036        1988 :     gel(v,ia) = gerepilecopy(av2, va);
    4037             :   }
    4038         476 :   return gerepilecopy(av, mkvec3(v, gel(PHI,3), mkvecsmall4(p,n+vden,n,D)));
    4039             : }
    4040             : static void
    4041        1918 : checkoms(GEN v)
    4042             : {
    4043        1918 :   if (typ(v) != t_VEC || lg(v) != 4 || typ(gel(v,1)) != t_VEC
    4044        1918 :       || typ(gel(v,3))!=t_VECSMALL)
    4045           0 :     pari_err_TYPE("checkoms [apply mspadicmoments]", v);
    4046        1918 : }
    4047             : static long
    4048        4284 : oms_get_p(GEN oms) { return gel(oms,3)[1]; }
    4049             : static long
    4050        4186 : oms_get_n(GEN oms) { return gel(oms,3)[2]; }
    4051             : static long
    4052        2464 : oms_get_n0(GEN oms) { return gel(oms,3)[3]; }
    4053             : static long
    4054        1918 : oms_get_D(GEN oms) { return gel(oms,3)[4]; }
    4055             : static int
    4056          98 : oms_is_supersingular(GEN oms) { GEN v = gel(oms,1); return lg(gel(v,1)) == 3; }
    4057             : 
    4058             : /* sum(j = 1, n, (-1)^(j+1)/j * x^j) */
    4059             : static GEN
    4060         784 : log1x(long n)
    4061             : {
    4062         784 :   long i, l = n+3;
    4063         784 :   GEN v = cgetg(l, t_POL);
    4064         784 :   v[1] = evalvarn(0)|evalsigne(1); gel(v,2) = gen_0;
    4065         784 :   for (i = 3; i < l; i++) gel(v,i) = ginv(stoi(odd(i)? i-2: 2-i));
    4066         784 :   return v;
    4067             : }
    4068             : 
    4069             : /* S = (1+x)^zk log(1+x)^logj (mod x^(n+1)) */
    4070             : static GEN
    4071        1820 : xlog1x(long n, long zk, long logj, long *pteich)
    4072             : {
    4073        1820 :   GEN S = logj? RgXn_powu_i(log1x(n), logj, n+1): NULL;
    4074        1820 :   if (zk)
    4075             :   {
    4076        1183 :     GEN L = deg1pol_shallow(gen_1, gen_1, 0); /* x+1 */
    4077        1183 :     *pteich += zk;
    4078        1183 :     if (zk < 0) { L = RgXn_inv(L,n+1); zk = -zk; }
    4079        1183 :     if (zk != 1) L = RgXn_powu_i(L, zk, n+1);
    4080        1183 :     S = S? RgXn_mul(S, L, n+1): L;
    4081             :   }
    4082        1820 :   return S;
    4083             : }
    4084             : 
    4085             : /* oms from mspadicmoments; integrate teichmuller^i * S(x) [S = NULL: 1]*/
    4086             : static GEN
    4087        2366 : mspadicint(GEN oms, long teichi, GEN S)
    4088             : {
    4089        2366 :   pari_sp av = avma;
    4090        2366 :   long p = oms_get_p(oms), n = oms_get_n(oms), n0 = oms_get_n0(oms);
    4091        2366 :   GEN vT = gel(oms,1), alpha = gel(oms,2), gp = utoipos(p);
    4092        2366 :   long loss = S? Z_lval(Q_denom(S), p): 0;
    4093        2366 :   long nfinal = minss(n-loss, n0);
    4094        2366 :   long i, la, l = lg(gel(vT,1));
    4095        2366 :   GEN res = cgetg(l, t_COL), teich = NULL;
    4096             : 
    4097        2366 :   if (S) S = RgX_to_RgC(S,lg(gmael(vT,1,1))-1);
    4098        2366 :   if (p == 2)
    4099             :   {
    4100         448 :     la = 3; /* corresponds to [1,-1] */
    4101         448 :     teichi &= 1;
    4102             :   }
    4103             :   else
    4104             :   {
    4105        1918 :     la = p; /* corresponds to [1,2,...,p-1] */
    4106        1918 :     teichi = smodss(teichi, p-1);
    4107        1918 :     if (teichi) teich = teichmullerinit(p, n);
    4108             :   }
    4109        5446 :   for (i=1; i<l; i++)
    4110             :   {
    4111        3080 :     pari_sp av2 = avma;
    4112        3080 :     GEN s = gen_0;
    4113             :     long ia;
    4114       14756 :     for (ia = 1; ia < la; ia++)
    4115             :     { /* Ta[j+1] correct mod p^n */
    4116       11676 :       GEN Ta = gmael(vT,ia,i), v = S? RgV_dotproduct(Ta, S): gel(Ta,1);
    4117       11676 :       if (teichi && ia != 1)
    4118             :       {
    4119        3843 :         if (p != 2)
    4120        3626 :           v = gmul(v, gel(teich, Fl_powu(ia,teichi,p)));
    4121             :         else
    4122         217 :           if (teichi) v = gneg(v);
    4123             :       }
    4124       11676 :       s = gadd(s, v);
    4125             :     }
    4126        3080 :     s = gadd(s, zeropadic(gp,nfinal));
    4127        3080 :     gel(res,i) = gerepileupto(av2, s);
    4128             :   }
    4129        2366 :   return gerepileupto(av, gmul(alpha, res));
    4130             : }
    4131             : /* integrate P = polynomial in log(x); vlog[j+1] = mspadicint(0,log(1+x)^j) */
    4132             : static GEN
    4133         539 : mspadicint_RgXlog(GEN P, GEN vlog)
    4134             : {
    4135         539 :   long i, d = degpol(P);
    4136         539 :   GEN s = gmul(gel(P,2), gel(vlog,1));
    4137         539 :   for (i = 1; i <= d; i++) s = gadd(s, gmul(gel(P,i+2), gel(vlog,i+1)));
    4138         539 :   return s;
    4139             : };
    4140             : 
    4141             : /* oms from mspadicmoments */
    4142             : GEN
    4143          98 : mspadicseries(GEN oms, long teichi)
    4144             : {
    4145          98 :   pari_sp av = avma;
    4146             :   GEN S, L, X, vlog, s, s2, u, logu, bin;
    4147             :   long j, p, m, n, step, stop;
    4148          98 :   checkoms(oms);
    4149          98 :   n = oms_get_n0(oms);
    4150          98 :   if (n < 1)
    4151             :   {
    4152           0 :     s = zeroser(0,0);
    4153           0 :     if (oms_is_supersingular(oms)) s = mkvec2(s,s);
    4154           0 :     return gerepilecopy(av, s);
    4155             :   }
    4156          98 :   p = oms_get_p(oms);
    4157          98 :   vlog = cgetg(n+1, t_VEC);
    4158          98 :   step = p == 2? 2: 1;
    4159          98 :   stop = 0;
    4160          98 :   S = NULL;
    4161          98 :   L = log1x(n);
    4162         644 :   for (j = 0; j < n; j++)
    4163             :   {
    4164         616 :     if (j) stop += step + u_lval(j,p); /* = step*j + v_p(j!) */
    4165         616 :     if (stop >= n) break;
    4166             :     /* S = log(1+x)^j */
    4167         546 :     gel(vlog,j+1) = mspadicint(oms,teichi,S);
    4168         546 :     S = S? RgXn_mul(S, L, n+1): L;
    4169             :   }
    4170          98 :   m = j;
    4171          98 :   u = utoipos(p == 2? 5: 1+p);
    4172          98 :   logu = glog(cvtop(u, utoipos(p), 4*m), 0);
    4173          98 :   X = gdiv(pol_x(0), logu);
    4174          98 :   s = cgetg(m+1, t_VEC);
    4175          98 :   s2 = oms_is_supersingular(oms)? cgetg(m+1, t_VEC): NULL;
    4176          98 :   bin = pol_1(0);
    4177         539 :   for (j = 0; j < m; j++)
    4178             :   { /* bin = binomial(x/log(1+p+O(p^(4*n))), j) mod x^m */
    4179         539 :     GEN a, v = mspadicint_RgXlog(bin, vlog);
    4180         539 :     int done = 1;
    4181         539 :     gel(s,j+1) = a = gel(v,1);
    4182         539 :     if (!gequal0(a) || valp(a) > 0) done = 0; else setlg(s,j+1);
    4183         539 :     if (s2)
    4184             :     {
    4185         119 :       gel(s2,j+1) = a = gel(v,2);
    4186         119 :       if (!gequal0(a) || valp(a) > 0) done = 0; else setlg(s2,j+1);
    4187             :     }
    4188         539 :     if (done || j == m-1) break;
    4189         441 :     bin = RgXn_mul(bin, gdivgs(gsubgs(X, j), j+1), m);
    4190             :   }
    4191          98 :   s = RgV_to_ser(s,0,lg(s)+1);
    4192          98 :   if (s2) { s2 = RgV_to_ser(s2,0,lg(s2)+1); s = mkvec2(s, s2); }
    4193          98 :   if (kross(oms_get_D(oms), p) >= 0) return gerepilecopy(av, s);
    4194           7 :   return gerepileupto(av, gneg(s));
    4195             : }
    4196             : void
    4197        1911 : mspadic_parse_chi(GEN s, GEN *s1, GEN *s2)
    4198             : {
    4199        1911 :   if (!s) *s1 = *s2 = gen_0;
    4200        1778 :   else switch(typ(s))
    4201             :   {
    4202        1274 :     case t_INT: *s1 = *s2 = s; break;
    4203             :     case t_VEC:
    4204         504 :       if (lg(s) == 3)
    4205             :       {
    4206         504 :         *s1 = gel(s,1);
    4207         504 :         *s2 = gel(s,2);
    4208         504 :         if (typ(*s1) == t_INT && typ(*s2) == t_INT) break;
    4209             :       }
    4210           0 :     default: pari_err_TYPE("mspadicL",s);
    4211           0 :              *s1 = *s2 = NULL;
    4212             :   }
    4213        1911 : }
    4214             : /* oms from mspadicmoments
    4215             :  * r-th derivative of L(f,chi^s,psi) in direction <chi>
    4216             :    - s \in Z_p \times \Z/(p-1)\Z, s-> chi^s=<\chi>^s_1 omega^s_2)
    4217             :    - Z -> Z_p \times \Z/(p-1)\Z par s-> (s, s mod p-1).
    4218             :  */
    4219             : GEN
    4220        1820 : mspadicL(GEN oms, GEN s, long r)
    4221             : {
    4222        1820 :   pari_sp av = avma;
    4223             :   GEN s1, s2, z, S;
    4224             :   long p, n, teich;
    4225        1820 :   checkoms(oms);
    4226        1820 :   p = oms_get_p(oms);
    4227        1820 :   n = oms_get_n(oms);
    4228        1820 :   mspadic_parse_chi(s, &s1,&s2);
    4229        1820 :   teich = umodiu(subii(s2,s1), p==2? 2: p-1);
    4230        1820 :   S = xlog1x(n, itos(s1), r, &teich);
    4231        1820 :   z = mspadicint(oms, teich, S);
    4232        1820 :   if (lg(z) == 2) z = gel(z,1);
    4233        1820 :   if (kross(oms_get_D(oms), p) < 0) z = gneg(z);
    4234        1820 :   return gerepilecopy(av, z);
    4235             : }
    4236             : 
    4237             : /****************************************************************************/
    4238             : 
    4239             : struct siegel
    4240             : {
    4241             :   GEN V, Ast;
    4242             :   long N; /* level */
    4243             :   long oo; /* index of the [oo,0] path */
    4244             :   long k1, k2; /* two distinguished indices */
    4245             :   long n; /* #W, W = initial segment [in siegelstepC] already normalized */
    4246             : };
    4247             : 
    4248             : static void
    4249         378 : siegel_init(struct siegel *C, GEN M)
    4250             : {
    4251             :   GEN CPI, CP, MM, V, W, Ast;
    4252         378 :   GEN m = gel(M,11), M2 = gel(M,2), S = msN_get_section(M);
    4253         378 :   GEN E2fromE1 = msN_get_E2fromE1(M);
    4254         378 :   long m0 = lg(M2)-1;
    4255         378 :   GEN E2  = vecslice(M2, m[1]+1, m[2]);/* E2 */
    4256         378 :   GEN E1T = vecslice(M2, m[3]+1, m0); /* E1,T2,T31 */
    4257         378 :   GEN L = shallowconcat(E1T, E2);
    4258         378 :   long i, l = lg(L), n = lg(E1T)-1, lE = lg(E2);
    4259             : 
    4260         378 :   Ast = cgetg(l, t_VECSMALL);
    4261        6195 :   for (i = 1; i < lE; ++i)
    4262             :   {
    4263        5817 :     long j = E2fromE1_c(gel(E2fromE1,i));
    4264        5817 :     Ast[n+i] = j;
    4265        5817 :     Ast[j] = n+i;
    4266             :   }
    4267         378 :   for (; i<=n; ++i) Ast[i] = i;
    4268         378 :   MM = cgetg (l,t_VEC);
    4269             : 
    4270       12320 :   for (i = 1; i < l; i++)
    4271             :   {
    4272       11942 :     GEN c = gel(S, L[i]);
    4273       11942 :     long c12, c22, c21 = ucoeff(c,2,1);
    4274       11942 :     if (!c21) { gel(MM,i) = gen_0; continue; }
    4275       11564 :     c22 = ucoeff(c,2,2);
    4276       11564 :     if (!c22) { gel(MM,i) = gen_m1; continue; }
    4277       11186 :     c12 = ucoeff(c,1,2);
    4278       11186 :     gel(MM,i) = gdivgs(stoi(c12), c22); /* right extremity > 0 */
    4279             :   }
    4280         378 :   CP = indexsort(MM);
    4281         378 :   CPI = cgetg(l, t_VECSMALL);
    4282         378 :   V = cgetg(l, t_VEC);
    4283         378 :   W = cgetg(l, t_VECSMALL);
    4284       12320 :   for (i = 1; i < l; ++i)
    4285             :   {
    4286       11942 :     gel(V,i) = mat2_to_ZM(gel(S, L[CP[i]]));
    4287       11942 :     CPI[CP[i]] = i;
    4288             :   }
    4289         378 :   for (i = 1; i < l; ++i) W[CPI[i]] = CPI[Ast[i]];
    4290         378 :   C->V = V;
    4291         378 :   C->Ast = W;
    4292         378 :   C->n = 0;
    4293         378 :   C->oo = 2;
    4294         378 :   C->N = ms_get_N(M);
    4295         378 : }
    4296             : 
    4297             : static double
    4298           0 : ZMV_size(GEN v)
    4299             : {
    4300           0 :   long i, l = lg(v);
    4301           0 :   GEN z = cgetg(l, t_VECSMALL);
    4302           0 :   for (i = 1; i < l; i++) z[i] = gexpo(gel(v,i));
    4303           0 :   return ((double)zv_sum(z)) / (4*(l-1));
    4304             : }
    4305             : 
    4306             : /* apply permutation perm to struct S. Don't follow k1,k2 */
    4307             : static void
    4308        5558 : siegel_perm0(struct siegel *S, GEN perm)
    4309             : {
    4310        5558 :   long i, l = lg(S->V);
    4311        5558 :   GEN V2 = cgetg(l, t_VEC), Ast2 = cgetg(l, t_VECSMALL);
    4312        5558 :   GEN V = S->V, Ast = S->Ast;
    4313             : 
    4314        5558 :   for (i = 1; i < l; i++) gel(V2,perm[i]) = gel(V,i);
    4315        5558 :   for (i = 1; i < l; i++) Ast2[perm[i]] = perm[Ast[i]];
    4316        5558 :   S->oo = perm[S->oo];
    4317        5558 :   S->Ast = Ast2;
    4318        5558 :   S->V = V2;
    4319        5558 : }
    4320             : /* apply permutation perm to full struct S */
    4321             : static void
    4322        5194 : siegel_perm(struct siegel *S, GEN perm)
    4323             : {
    4324        5194 :   siegel_perm0(S, perm);
    4325        5194 :   S->k1 = perm[S->k1];
    4326        5194 :   S->k2 = perm[S->k2];
    4327        5194 : }
    4328             : /* cyclic permutation of lg = l-1 moving a -> 1, a+1 -> 2, etc.  */
    4329             : static GEN
    4330        2884 : rotate_perm(long l, long a)
    4331             : {
    4332        2884 :   GEN p = cgetg(l, t_VECSMALL);
    4333        2884 :   long i, j = 1;
    4334        2884 :   for (i = a; i < l; i++) p[i] = j++;
    4335        2884 :   for (i = 1; i < a; i++) p[i] = j++;
    4336        2884 :   return p;
    4337             : }
    4338             : 
    4339             : /* a1 < c1 <= a2 < c2*/
    4340             : static GEN
    4341        2520 : basic_op_perm(long l, long a1, long a2, long c1, long c2)
    4342             : {
    4343        2520 :   GEN p = cgetg(l, t_VECSMALL);
    4344        2520 :   long i, j = 1;
    4345        2520 :   p[a1] = j++;
    4346        2520 :   for (i = c1; i < a2; i++)   p[i] = j++;
    4347        2520 :   for (i = a1+1; i < c1; i++) p[i] = j++;
    4348        2520 :   p[a2] = j++;
    4349        2520 :   for (i = c2; i < l; i++)    p[i] = j++;
    4350        2520 :   for (i = 1; i < a1; i++)    p[i] = j++;
    4351        2520 :   for (i = a2+1; i < c2; i++) p[i] = j++;
    4352        2520 :   return p;
    4353             : }
    4354             : static GEN
    4355         154 : basic_op_perm_elliptic(long l, long a1)
    4356             : {
    4357         154 :   GEN p = cgetg(l, t_VECSMALL);
    4358         154 :   long i, j = 1;
    4359         154 :   p[a1] = j++;
    4360         154 :   for (i = 1; i < a1; i++)   p[i] = j++;
    4361         154 :   for (i = a1+1; i < l; i++) p[i] = j++;
    4362         154 :   return p;
    4363             : }
    4364             : static GEN
    4365       14616 : ZM2_rev(GEN T) { return mkmat2(gel(T,2), ZC_neg(gel(T,1))); }
    4366             : 
    4367             : /* In place, V = vector of consecutive paths, between x <= y.
    4368             :  * V[x..y-1] <- g*V[x..y-1] */
    4369             : static void
    4370        5733 : path_vec_mul(GEN V, long x, long y, GEN g)
    4371             : {
    4372             :   long j;
    4373             :   GEN M;
    4374        5733 :   if (x == y) return;
    4375        3360 :   M = gel(V,x); gel(V,x) = ZM_mul(g,M);
    4376       37709 :   for (j = x+1; j < y; j++) /* V[j] <- g*V[j], optimized */
    4377             :   {
    4378       34349 :     GEN Mnext = gel(V,j); /* Mnext[,1] = M[,2] */
    4379       34349 :     GEN gM = gel(V,j-1), u = gel(gM,2);
    4380       34349 :     if (!ZV_equal(gel(M,2), gel(Mnext,1))) u = ZC_neg(u);
    4381       34349 :     gel(V,j) = mkmat2(u, ZM_ZC_mul(g,gel(Mnext,2)));
    4382       34349 :     M = Mnext;
    4383             :   }
    4384             : }
    4385             : 
    4386        4830 : static long prev(GEN V, long i) { return (i == 1)? lg(V)-1: i-1; }
    4387        4830 : static long next(GEN V, long i) { return (i == lg(V)-1)? 1: i+1; }
    4388             : static GEN
    4389       19810 : ZM_det2(GEN u, GEN v)
    4390             : {
    4391       19810 :   GEN a = gel(u,1), c = gel(u,2);
    4392       19810 :   GEN b = gel(v,1), d = gel(v,2); return subii(mulii(a,d), mulii(b,c));
    4393             : }
    4394             : static GEN
    4395       14616 : ZM2_det(GEN T) { return ZM_det2(gel(T,1),gel(T,2)); }
    4396             : static void
    4397        4466 : fill1(GEN V, long a)
    4398             : {
    4399        4466 :   long p = prev(V,a), n = next(V,a);
    4400        4466 :   GEN u = gmael(V,p,2), v = gmael(V,n,1);
    4401        4466 :   if (signe(ZM_det2(u,v)) < 0) v = ZC_neg(v);
    4402        4466 :   gel(V,a) = mkmat2(u, v);
    4403        4466 : }
    4404             : /* a1 < a2 */
    4405             : static void
    4406        2520 : fill2(GEN V, long a1, long a2)
    4407             : {
    4408        2520 :   if (a2 != a1+1) { fill1(V,a1); fill1(V,a2); } /* non adjacent, reconnect */
    4409             :   else /* parabolic */
    4410             :   {
    4411         364 :     long p = prev(V,a1), n = next(V,a2);
    4412         364 :     GEN u, v, C = gmael(V,a1,2), mC = ZC_neg(C); /* = \pm V[a2][1] */
    4413         364 :     u = gmael(V,p,2); v = (signe(ZM_det2(u,C)) < 0)? mC: C;
    4414         364 :     gel(V,a1) = mkmat2(u,v);
    4415         364 :     v = gmael(V,n,1); u = (signe(ZM_det2(C,v)) < 0)? mC: C;
    4416         364 :     gel(V,a2) = mkmat2(u,v);
    4417             :   }
    4418        2520 : }
    4419             : 
    4420             : /* DU = det(U), return g = T*U^(-1) or NULL if not in Gamma0(N); if N = 0,
    4421             :  * only test whether g is integral */
    4422             : static GEN
    4423       14903 : ZM2_div(GEN T, GEN U, GEN DU, long N)
    4424             : {
    4425       14903 :   GEN a=gcoeff(U,1,1), b=gcoeff(U,1,2), c=gcoeff(U,2,1), d=gcoeff(U,2,2);
    4426       14903 :   GEN e=gcoeff(T,1,1), f=gcoeff(T,1,2), g=gcoeff(T,2,1), h=gcoeff(T,2,2);
    4427             :   GEN A, B, C, D, r;
    4428             : 
    4429       14903 :   C = dvmdii(subii(mulii(d,g), mulii(c,h)), DU, &r);
    4430       14903 :   if (r != gen_0 || (N && smodis(C,N))) return NULL;
    4431       14616 :   A = dvmdii(subii(mulii(d,e), mulii(c,f)), DU, &r);
    4432       14616 :   if (r != gen_0) return NULL;
    4433       14616 :   B = dvmdii(subii(mulii(a,f), mulii(b,e)), DU, &r);
    4434       14616 :   if (r != gen_0) return NULL;
    4435       14616 :   D = dvmdii(subii(mulii(a,h), mulii(g,b)), DU, &r);
    4436       14616 :   if (r != gen_0) return NULL;
    4437       14616 :   return mkmat22(A,B,C,D);
    4438             : }
    4439             : 
    4440             : static GEN
    4441       14616 : get_g(struct siegel *S, long a1)
    4442             : {
    4443       14616 :   long a2 = S->Ast[a1];
    4444       14616 :   GEN a = gel(S->V,a1), ar = ZM2_rev(gel(S->V,a2)), Dar = ZM2_det(ar);
    4445       14616 :   GEN g = ZM2_div(a, ar, Dar, S->N);
    4446       14616 :   if (!g)
    4447             :   {
    4448         287 :     GEN tau = mkmat22(gen_0,gen_m1, gen_1,gen_m1); /*[0,-1;1,-1]*/
    4449         287 :     g = ZM2_div(ZM_mul(ar, tau), ar, Dar, 0);
    4450             :   }
    4451       14616 :   return g;
    4452             : }
    4453             : /* input V = (X1 a X2 | X3 a^* X4) + Ast
    4454             :  * a1 = index of a
    4455             :  * a2 = index of a^*, inferred from a1. We must have a != a^*
    4456             :  * c1 = first cut [ index of first path in X3 ]
    4457             :  * c2 = second cut [ either in X4 or X1, index of first path ]
    4458             :  * Assume a < a^* (cf Paranoia below): c1 or c2 must be in
    4459             :  *    ]a,a^*], and the other in the "complement" ]a^*,a] */
    4460             : static void
    4461        2520 : basic_op(struct siegel *S, long a1, long c1, long c2)
    4462             : {
    4463        2520 :   long l = lg(S->V), a2 = S->Ast[a1];
    4464             :   GEN g;
    4465             : 
    4466        2520 :   if (a1 == a2)
    4467             :   { /* a = a^* */
    4468           0 :     g = get_g(S, a1);
    4469           0 :     path_vec_mul(S->V, a1+1, l, g);
    4470           0 :     siegel_perm(S, basic_op_perm_elliptic(l, a1));
    4471             :     /* fill the hole left at a1, reconnect the path */
    4472           0 :     fill1(S->V, a1); return;
    4473             :   }
    4474             : 
    4475             :   /* Paranoia: (a,a^*) conjugate, call 'a' the first one */
    4476        2520 :   if (a2 < a1) lswap(a1,a2);
    4477             :   /* Now a1 < a2 */
    4478        2520 :   if (c1 <= a1 || c1 > a2) lswap(c1,c2); /* ensure a1 < c1 <= a2 */
    4479        2520 :   if (c2 < a1)
    4480             :   { /* if cut c2 is in X1 = X11|X12, rotate to obtain
    4481             :        (a X2 | X3 a^* X4 X11|X12): then a1 = 1 */
    4482        2520 :     GEN p = rotate_perm(l, a1);
    4483        2520 :     siegel_perm(S, p);
    4484        2520 :     a1 = 1; /* = p[a1] */
    4485        2520 :     a2 = S->Ast[1]; /* > a1 */
    4486        2520 :     c1 = p[c1];
    4487        2520 :     c2 = p[c2];
    4488             :   }
    4489             :   /* Now a1 < c1 <= a2 < c2; a != a^* */
    4490        2520 :   g = get_g(S, a1);
    4491        2520 :   if (S->oo >= c1 && S->oo < c2) /* W inside [c1..c2[ */
    4492         539 :   { /* c2 -> c1 excluding a1 */
    4493         539 :     GEN gi = SL2_inv(g); /* g a^* = a; gi a = a^* */
    4494         539 :     path_vec_mul(S->V, 1, a1, gi);
    4495         539 :     path_vec_mul(S->V, a1+1, c1, gi);
    4496         539 :     path_vec_mul(S->V, c2, l, gi);
    4497             :   }
    4498             :   else
    4499             :   { /* c1 -> c2 excluding a2 */
    4500        1981 :     path_vec_mul(S->V, c1, a2, g);
    4501        1981 :     path_vec_mul(S->V, a2+1, c2, g);
    4502             :   }
    4503        2520 :   siegel_perm(S, basic_op_perm(l, a1,a2, c1,c2));
    4504             :   /* fill the holes left at a1,a2, reconnect the path */
    4505        2520 :   fill2(S->V, a1, a2);
    4506             : }
    4507             : /* a = a^* (elliptic case) */
    4508             : static void
    4509         154 : basic_op_elliptic(struct siegel *S, long a1)
    4510             : {
    4511         154 :   long l = lg(S->V);
    4512         154 :   GEN g = get_g(S, a1);
    4513         154 :   path_vec_mul(S->V, a1+1, l, g);
    4514         154 :   siegel_perm(S, basic_op_perm_elliptic(l, a1));
    4515             :   /* fill the hole left at a1 (now at 1), reconnect the path */
    4516         154 :   fill1(S->V, 1);
    4517         154 : }
    4518             : 
    4519             : /* input V = W X a b Y a^* Z b^* T, W already normalized
    4520             :  * X = [n+1, k1-1], Y = [k2+1, Ast[k1]-1],
    4521             :  * Z = [Ast[k1]+1, Ast[k2]-1], T = [Ast[k2]+1, oo].
    4522             :  * Assume that X doesn't start by c c^* or a b a^* b^*. */
    4523             : static void
    4524        1057 : siegelstep(struct siegel *S)
    4525             : {
    4526        1057 :   if (S->Ast[S->k1] == S->k1)
    4527             :   {
    4528         154 :     basic_op_elliptic(S, S->k1);
    4529         154 :     S->n++;
    4530             :   }
    4531         903 :   else if (S->Ast[S->k1] == S->k1+1)
    4532             :   {
    4533         364 :     basic_op(S, S->k1, S->Ast[S->k1], 1); /* 1: W starts there */
    4534         364 :     S->n += 2;
    4535             :   }
    4536             :   else
    4537             :   {
    4538         539 :     basic_op(S, S->k2, S->Ast[S->k1], 1); /* 1: W starts there */
    4539         539 :     basic_op(S, S->k1, S->k2, S->Ast[S->k2]);
    4540         539 :     basic_op(S, S->Ast[S->k2], S->k2, S->Ast[S->k1]);
    4541         539 :     basic_op(S, S->k1, S->Ast[S->k1], S->Ast[S->k2]);
    4542         539 :     S->n += 4;
    4543             :   }
    4544        1057 : }
    4545             : 
    4546             : /* normalize hyperbolic polygon */
    4547             : static void
    4548         301 : mssiegel(struct siegel *S)
    4549             : {
    4550         301 :   pari_sp av = avma;
    4551             :   long k, t, nv;
    4552             : #ifdef COUNT
    4553             :   long countset[16];
    4554             :   for (k = 0; k < 16; k++) countset[k] = 0;
    4555             : #endif
    4556             : 
    4557         301 :   nv = lg(S->V)-1;
    4558         301 :   if (DEBUGLEVEL>1) err_printf("nv = %ld, expo = %.2f\n", nv,ZMV_size(S->V));
    4559         301 :   t = 0;
    4560        2506 :   while (S->n < nv)
    4561             :   {
    4562        1904 :     if (S->Ast[S->n+1] == S->n+1) { S->n++; continue; }
    4563        1778 :     if (S->Ast[S->n+1] == S->n+2) { S->n += 2; continue; }
    4564        1134 :     if (S->Ast[S->n+1] == S->n+3 && S->Ast[S->n+2] == S->n+4) { S->n += 4; continue; }
    4565        1057 :     k = nv;
    4566        2184 :     while (k > S->n)
    4567             :     {
    4568        1127 :       if (S->Ast[k] == k) { k--; continue; }
    4569        1099 :       if (S->Ast[k] == k-1) { k -= 2; continue; }
    4570        1057 :       if (S->Ast[k] == k-2 && S->Ast[k-1] == k-3) { k -= 4; continue; }
    4571        1057 :       break;
    4572             :     }
    4573        1057 :     if (k != nv) { siegel_perm0(S, rotate_perm(nv+1, k+1)); S->n += nv-k; }
    4574             : 
    4575        6223 :     for (k = S->n+1; k <= nv; k++)
    4576        6223 :       if (S->Ast[k] <= k) { t = S->Ast[k]; break; }
    4577        1057 :     S->k1 = t;
    4578        1057 :     S->k2 = t+1;
    4579             : #ifdef COUNT
    4580             :     countset[ ((S->k1-1 == S->n)
    4581             :               | ((S->k2 == S->Ast[S->k1]-1) << 1)
    4582             :               | ((S->Ast[S->k1] == S->Ast[S->k2]-1) << 2)
    4583             :               | ((S->Ast[S->k2] == nv) << 3)) ]++;
    4584             : #endif
    4585        1057 :     siegelstep(S);
    4586        1057 :     if (gc_needed(av,2))
    4587             :     {
    4588           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"mspolygon, n = %ld",S->n);
    4589           0 :       gerepileall(av, 2, &S->V, &S->Ast);
    4590             :     }
    4591             :   }
    4592         301 :   if (DEBUGLEVEL>1) err_printf("expo = %.2f\n", ZMV_size(S->V));
    4593             : #ifdef COUNT
    4594             :   for (k = 0; k < 16; k++)
    4595             :     err_printf("%3ld: %6ld\n", k, countset[k]);
    4596             : #endif
    4597         301 : }
    4598             : 
    4599             : /* return a vector of char */
    4600             : static GEN
    4601           0 : Ast2v(GEN Ast)
    4602             : {
    4603           0 :   long j = 0, k, l = lg(Ast);
    4604           0 :   GEN v = const_vec(l-1, NULL);
    4605           0 :   for (k=1; k < l; k++)
    4606             :   {
    4607             :     char *sj;
    4608           0 :     if (gel(v,k)) continue;
    4609           0 :     j++;
    4610           0 :     sj = stack_sprintf("$%ld$", j);
    4611           0 :     gel(v,k) = (GEN)sj;
    4612           0 :     if (Ast[k] != k) gel(v,Ast[k]) = (GEN)stack_sprintf("$%ld^*$", j);
    4613             :   }
    4614           0 :   return v;
    4615             : };
    4616             : static GEN
    4617           0 : M2Q(GEN p) { GEN c = gel(p,1); return gdiv(gel(c,1), gel(c,2)); }
    4618             : 
    4619             : static GEN
    4620           0 : polygon2tex(GEN V, GEN Ast)
    4621             : {
    4622           0 :   pari_sp av = avma;
    4623           0 :   GEN v = Ast2v(Ast);
    4624             :   pari_str s;
    4625           0 :   long j, l = lg(V), flag;
    4626             :   double c;
    4627           0 :   str_init(&s, 1);
    4628           0 :   flag = (l <= 16);
    4629           0 :   str_puts(&s, "\n\\begin{tikzpicture}[scale=10]\n");
    4630           0 :   str_puts(&s, "\\draw (0,0.5)--(0,0) node [very near start, right] {$1^*$} node [below] {$0$}");
    4631           0 :   for (j=4; j < l; j++)
    4632             :   {
    4633           0 :     GEN a = M2Q(gel(V,j-1)), b = M2Q(gel(V,j));
    4634           0 :     c = gtodouble(gsub(b,a)) / 2;
    4635             : 
    4636           0 :     str_printf(&s, "arc (180:0:%.4f)\n", c);
    4637           0 :     if (flag)
    4638             :     {
    4639           0 :       long sb = itos(numer_i(b));
    4640           0 :       long sa = itos(denom_i(a));
    4641           0 :       str_printf(&s,
    4642             :         "node [midway, above] {%s} node [below]{$\\frac{%ld}{%ld}$}\n",
    4643           0 :         (char*)gel(v,j-1), sb, sa);
    4644             :     }
    4645             :   }
    4646           0 :   c = (1- gtodouble(M2Q(gel(V,l-1)))) / 2;
    4647           0 :   str_printf(&s, "arc (180:0:%.4f)\n", c);
    4648           0 :   if (flag) str_printf(&s, "node [midway, above] {%s}", (char*)gel(v,l-1));
    4649           0 :   str_printf(&s,"node [below] {$1$} -- (1,0.5) node [very near end, left] {$1$};");
    4650           0 :   str_printf(&s, "\n\\end{tikzpicture}");
    4651           0 :   return gerepileuptoleaf(av, strtoGENstr(s.string));
    4652             : }
    4653             : 
    4654             : static GEN
    4655           0 : circle2tex(GEN Ast, GEN G)
    4656             : {
    4657           0 :   pari_sp av = avma;
    4658           0 :   GEN v = Ast2v(Ast);
    4659             :   pari_str s;
    4660           0 :   long u, n = lg(Ast)-1;
    4661           0 :   const double ang = 360./n;
    4662             : 
    4663           0 :   if (n > 30)
    4664             :   {
    4665           0 :     v = const_vec(n, (GEN)"");
    4666           0 :     gel(v,1) = (GEN)"$(1,\\infty)$";
    4667             :   }
    4668           0 :   str_init(&s, 1);
    4669           0 :   str_puts(&s, "\n\\begingroup\n\
    4670             :   \\def\\geo#1#2{(#2:1) arc (90+#2:270+#1:{tan((#2-#1)/2)})}\n\
    4671             :   \\def\\sgeo#1#2{(#2:1) -- (#1:1)}\n\
    4672             :   \\def\\unarc#1#2#3{({#1 * #3}:1.2) node {#2}}\n\
    4673             :   \\def\\cell#1#2{({#1 * #2}:0.95) circle(0.05)}\n\
    4674             :   \\def\\link#1#2#3#4#5{\\unarc{#1}{#2}{#5}\\geo{#1*#5}{#3*#5}\\unarc{#3}{#4}{#5}}\n\
    4675             :   \\def\\slink#1#2#3#4#5{\\unarc{#1}{#2}{#5}\\sgeo{#1*#5}{#3*#5}\\unarc{#3}{#4}{#5}}");
    4676             : 
    4677           0 :   str_puts(&s, "\n\\begin{tikzpicture}[scale=4]\n");
    4678           0 :   str_puts(&s, "\\draw (0, 0) circle(1);\n");
    4679           0 :   for (u=1; u <= n; u++)
    4680             :   {
    4681           0 :     if (Ast[u] == u)
    4682             :     {
    4683           0 :       str_printf(&s,"\\draw\\unarc{%ld}{%s}{%.4f}; \\draw\\cell{%ld}{%.4f};\n",
    4684           0 :                  u, v[u], ang, u, ang);
    4685           0 :       if (ZM_isscalar(gpowgs(gel(G,u),3), NULL))
    4686           0 :         str_printf(&s,"\\fill \\cell{%ld}{%.4f};\n", u, ang);
    4687             :     }
    4688           0 :     else if(Ast[u] > u)
    4689           0 :       str_printf(&s, "\\draw \\%slink {%ld}{%s}{%ld}{%s}{%.4f};\n",
    4690           0 :                      Ast[u]-u==n/2? "s": "", u, v[u], Ast[u], v[Ast[u]], ang);
    4691             :   }
    4692           0 :   str_printf(&s, "\\end{tikzpicture}\\endgroup");
    4693           0 :   return gerepileuptoleaf(av, strtoGENstr(s.string));
    4694             : }
    4695             : 
    4696             : GEN
    4697         399 : mspolygon(GEN M, long flag)
    4698             : {
    4699         399 :   pari_sp av = avma;
    4700             :   struct siegel T;
    4701             :   long i, l;
    4702             :   GEN v, G, msN;
    4703         399 :   if (typ(M) == t_INT)
    4704             :   {
    4705         301 :     long N = itos(M);
    4706         301 :     if (N <= 0) pari_err_DOMAIN("msinit","N", "<=", gen_0,M);
    4707         301 :     msN = msinit_N(N);
    4708             :   }
    4709          98 :   else { checkms(M); msN = get_msN(M); }
    4710         399 :   if (flag < 0 || flag > 3) pari_err_FLAG("mspolygon");
    4711         399 :   if (ms_get_N(msN) == 1)
    4712             :   {
    4713          21 :     GEN S = mkS();
    4714          21 :     T.V = mkvec2(matid(2), S);
    4715          21 :     T.Ast = mkvecsmall2(1,2);
    4716          21 :     G = mkvec2(S, mkTAU());
    4717             :   }
    4718             :   else
    4719             :   {
    4720         378 :     siegel_init(&T, msN);
    4721         378 :     l = lg(T.V);
    4722         378 :     if (flag & 1)
    4723             :     {
    4724         301 :       long oo2 = 0;
    4725         301 :       mssiegel(&T);
    4726        3451 :       for (i = 1; i < l; i++)
    4727             :       {
    4728        3451 :         GEN c = gel(T.V, i);
    4729        3451 :         GEN c22 = gcoeff(c,2,2); if (!signe(c22)) { oo2 = i; break; }
    4730             :       }
    4731         301 :       if (!oo2) pari_err_BUG("mspolygon");
    4732         301 :       siegel_perm0(&T, rotate_perm(l, oo2));
    4733             :     }
    4734         378 :     G = cgetg(l, t_VEC);
    4735         378 :     for (i = 1; i < l; i++) gel(G,i) = get_g(&T, i);
    4736             :   }
    4737         399 :   if (flag & 2)
    4738           0 :     v = mkvec5(T.V, T.Ast, G, polygon2tex(T.V,T.Ast), circle2tex(T.Ast,G));
    4739             :   else
    4740         399 :     v = mkvec3(T.V, T.Ast, G);
    4741         399 :   return gerepilecopy(av, v);
    4742             : }
    4743             : 
    4744             : #if 0
    4745             : static int
    4746             : iselliptic(GEN Ast, long i) { return i == Ast[i]; }
    4747             : static int
    4748             : isparabolic(GEN Ast, long i)
    4749             : { long i2 = Ast[i]; return (i2 == i+1 || i2 == i-1); }
    4750             : #endif
    4751             : 
    4752             : /* M from msinit, F QM maximal rank */
    4753             : GEN
    4754          77 : mslattice(GEN M, GEN F)
    4755             : {
    4756          77 :   pari_sp av = avma;
    4757             :   long i, ivB, j, k, l, lF;
    4758             :   GEN D, U, G, A, vB, m, d;
    4759             : 
    4760          77 :   checkms(M);
    4761          77 :   if (!F) F = gel(mscuspidal(M, 0), 1);
    4762             :   else
    4763             :   {
    4764          49 :     if (is_Qevproj(F)) F = gel(F,1);
    4765          49 :     if (typ(F) != t_MAT) pari_err_TYPE("mslattice",F);
    4766             :   }
    4767          77 :   lF = lg(F); if (lF == 1) return cgetg(1, t_MAT);
    4768          77 :   D = mspolygon(M,0);
    4769          77 :   k = msk_get_weight(M);
    4770          77 :   F = vec_Q_primpart(F);
    4771          77 :   if (typ(F)!=t_MAT || !RgM_is_ZM(F)) pari_err_TYPE("mslattice",F);
    4772          77 :   G = gel(D,3); l = lg(G);
    4773          77 :   A = gel(D,2);
    4774          77 :   vB = cgetg(l, t_COL);
    4775          77 :   d = mkcol2(gen_0,gen_1);
    4776          77 :   m = mkmat2(d, d);
    4777        7091 :   for (i = ivB = 1; i < l; i++)
    4778             :   {
    4779        7014 :     GEN B, vb, g = gel(G,i);
    4780        7014 :     if (A[i] < i) continue;
    4781        3514 :     gel(m,2) = SL2_inv2(g);
    4782        3514 :     vb = mseval(M, F, m);
    4783        3514 :     if (k == 2) B = vb;
    4784             :     else
    4785             :     {
    4786             :       long lB;
    4787         147 :       B = RgXV_to_RgM(vb, k-1);
    4788             :       /* add coboundaries */
    4789         147 :       B = shallowconcat(B, RgM_Rg_sub(RgX_act_Gl2Q(g, k), gen_1));
    4790             :       /* beware: the basis for RgX_act_Gl2Q is (X^(k-2),...,Y^(k-2)) */
    4791         147 :       lB = lg(B);
    4792         147 :       for (j = 1; j < lB; j++) gel(B,j) = vecreverse(gel(B,j));
    4793             :     }
    4794        3514 :     gel(vB, ivB++) = B;
    4795             :   }
    4796          77 :   setlg(vB, ivB);
    4797          77 :   vB = shallowmatconcat(vB);
    4798          77 :   if (ZM_equal0(vB)) return gerepilecopy(av, F);
    4799             : 
    4800          77 :   (void)QM_ImQ_hnfall(vB, &U, 0);
    4801          77 :   if (k > 2) U = rowslice(U, 1, lgcols(U)-k); /* remove coboundary part */
    4802          77 :   U = Q_remove_denom(U, &d);
    4803          77 :   F = ZM_hnf(ZM_mul(F, U));
    4804          77 :   if (d) F = RgM_Rg_div(F, d);
    4805          77 :   return gerepileupto(av, F);
    4806             : }
    4807             : 
    4808             : /**** Petersson scalar product ****/
    4809             : 
    4810             : /* oo -> g^(-1) oo */
    4811             : static GEN
    4812        6181 : cocycle(GEN g)
    4813        6181 : { return mkmat22(gen_1, gcoeff(g,2,2), gen_0, negi(gcoeff(g,2,1))); }
    4814             : 
    4815             : /* C = vecbinome(k-2) */
    4816             : static GEN
    4817       17983 : bil(GEN P, GEN Q, GEN C)
    4818             : {
    4819       17983 :   long i, n = lg(C)-2; /* k - 2 */
    4820             :   GEN s;
    4821       17983 :   if (!n) return gmul(P,Q);
    4822       17962 :   if (typ(P) != t_POL) P = scalarpol_shallow(P,0);
    4823       17962 :   if (typ(Q) != t_POL) Q = scalarpol_shallow(Q,0);
    4824       17962 :   s = gen_0;
    4825       71960 :   for (i = 0; i <= n; i++)
    4826             :   {
    4827       53998 :     GEN t = gdiv(gmul(RgX_coeff(P,i), RgX_coeff(Q, n-i)), gel(C,i+1));
    4828       53998 :     s = odd(i)? gsub(s, t): gadd(s, t);
    4829             :   }
    4830       17962 :   return s;
    4831             : }
    4832             : 
    4833             : static void
    4834        1351 : mspetersson_i(GEN W, GEN F, GEN G, GEN *pvf, GEN *pvg, GEN *pC)
    4835             : {
    4836        1351 :   GEN WN = get_msN(W), annT2, annT31, section, c, vf, vg;
    4837             :   long i, n1, n2, n3;
    4838             : 
    4839        1351 :   annT2 = msN_get_annT2(WN);
    4840        1351 :   annT31 = msN_get_annT31(WN);
    4841        1351 :   section = msN_get_section(WN);
    4842             : 
    4843        1351 :   if (ms_get_N(WN) == 1)
    4844             :   {
    4845           7 :     vf = cgetg(3, t_VEC);
    4846           7 :     vg = cgetg(3, t_VEC);
    4847           7 :     gel(vf,1) = mseval(W, F, gel(section,1));
    4848           7 :     gel(vf,2) = gneg(gel(vf,1));
    4849           7 :     n1 = 0;
    4850             :   }
    4851             :   else
    4852             :   {
    4853        1344 :     GEN singlerel = msN_get_singlerel(WN);
    4854        1344 :     GEN gen = msN_get_genindex(WN);
    4855        1344 :     long l = lg(gen);
    4856        1344 :     vf = cgetg(l, t_VEC);
    4857        1344 :     vg = cgetg(l, t_VEC); /* generators of Delta ordered as E1,T2,T31 */
    4858        1344 :     for (i = 1; i < l; i++) gel(vf, i) = mseval(W, F, gel(section,gen[i]));
    4859        1344 :     n1 = ms_get_nbE1(WN); /* E1 */
    4860        7420 :     for (i = 1; i <= n1; i++)
    4861             :     {
    4862        6076 :       c = cocycle(gcoeff(gel(singlerel,i),2,1));
    4863        6076 :       gel(vg, i) = mseval(W, G, c);
    4864             :     }
    4865             :   }
    4866        1351 :   n2 = lg(annT2)-1; /* T2 */
    4867        1386 :   for (i = 1; i <= n2; i++)
    4868             :   {
    4869          35 :     c = cocycle(gcoeff(gel(annT2,i), 2,1));
    4870          35 :     gel(vg, i+n1) = gmul2n(mseval(W, G, c), -1);
    4871             :   }
    4872        1351 :   n3 = lg(annT31)-1; /* T31 */
    4873        1386 :   for (i = 1; i <= n3; i++)
    4874             :   {
    4875             :     GEN f;
    4876          35 :     c = cocycle(gcoeff(gel(annT31,i), 2,1));
    4877          35 :     f = mseval(W, G, c);
    4878          35 :     c = cocycle(gcoeff(gel(annT31,i), 3,1));
    4879          35 :     gel(vg, i+n1+n2) = gdivgs(gadd(f, mseval(W, G, c)), 3);
    4880             :   }
    4881        1351 :   *pC = vecbinome(msk_get_weight(W) - 2);
    4882        1351 :   *pvf = vf;
    4883        1351 :   *pvg = vg;
    4884        1351 : }
    4885             : 
    4886             : /* Petersson product on Hom_G(Delta_0, V_k) */
    4887             : GEN
    4888        1351 : mspetersson(GEN W, GEN F, GEN G)
    4889             : {
    4890        1351 :   pari_sp av = avma;
    4891             :   GEN vf, vg, C;
    4892             :   long k, l, tG, tF;
    4893        1351 :   checkms(W);
    4894        1351 :   if (!F) F = matid(msdim(W));
    4895        1351 :   if (!G) G = F;
    4896        1351 :   tF = typ(F);
    4897        1351 :   tG = typ(G);
    4898        1351 :   if (tF == t_MAT && tG != t_MAT) pari_err_TYPE("mspetersson",G);
    4899        1351 :   if (tG == t_MAT && tF != t_MAT) pari_err_TYPE("mspetersson",F);
    4900        1351 :   mspetersson_i(W, F, G, &vf, &vg, &C);
    4901        1351 :   l = lg(vf);
    4902        1351 :   if (tF != t_MAT)
    4903             :   { /* <F,G>, two symbols */
    4904        1274 :     GEN s = gen_0;
    4905        1274 :     for (k = 1; k < l; k++) s = gadd(s, bil(gel(vf,k), gel(vg,k), C));
    4906        1274 :     return gerepileupto(av, s);
    4907             :   }
    4908          77 :   else if (F != G)
    4909             :   { /* <(f_1,...,f_m), (g_1,...,g_n)> */
    4910           0 :     long iF, iG, lF = lg(F), lG = lg(G);
    4911           0 :     GEN M = cgetg(lG, t_MAT);
    4912           0 :     for (iG = 1; iG < lG; iG++)
    4913             :     {
    4914           0 :       GEN c = cgetg(lF, t_COL);
    4915           0 :       gel(M,iG) = c;
    4916           0 :       for (iF = 1; iF < lF; iF++)
    4917             :       {
    4918           0 :         GEN s = gen_0;
    4919           0 :         for (k = 1; k < l; k++)
    4920           0 :           s = gadd(s, bil(gmael(vf,k,iF), gmael(vg,k,iG), C));
    4921           0 :         gel(c,iF) = s; /* M[iF,iG] = <F[iF], G[iG] > */
    4922             :       }
    4923             :     }
    4924           0 :     return gerepilecopy(av, M);
    4925             :   }
    4926             :   else
    4927             :   { /* <(f_1,...,f_n), (f_1,...,f_n)> */
    4928          77 :     long iF, iG, n = lg(F)-1;
    4929          77 :     GEN M = zeromatcopy(n,n);
    4930         693 :     for (iG = 1; iG <= n; iG++)
    4931        3192 :       for (iF = iG+1; iF <= n; iF++)
    4932             :       {
    4933        2576 :         GEN s = gen_0;
    4934       14728 :         for (k = 1; k < l; k++)
    4935       12152 :           s = gadd(s, bil(gmael(vf,k,iF), gmael(vg,k,iG), C));
    4936        2576 :         gcoeff(M,iF,iG) = s; /* <F[iF], F[iG] > */
    4937        2576 :         gcoeff(M,iG,iF) = gneg(s);
    4938             :       }
    4939          77 :     return gerepilecopy(av, M);
    4940             :   }
    4941             : }

Generated by: LCOV version 1.13