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.1 lcov report (development 24038-ebe36f6c4) Lines: 2569 2708 94.9 %
Date: 2019-07-23 05:53:17 Functions: 275 281 97.9 %
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        5110 : Qevproj_init(GEN M)
     508             : {
     509             :   GEN v, perm, MM, iM, diM;
     510        5110 :   v = ZM_indexrank(M); perm = gel(v,1);
     511        5110 :   MM = rowpermute(M, perm); /* square invertible */
     512        5110 :   iM = ZM_inv(MM, &diM);
     513        5110 :   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 = vec_append(v1, NP_matrix_extra(M,P[i]));
     883          84 :         vd = vec_append(vd, 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]? sstoQ(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             : 
    2387             : /* map op: W1 = Hom(Delta_0(N1),V) -> W2 = Hom(Delta_0(N2),V), given by
    2388             :  * \sum v[i], v[i] in Gl2(Q) */
    2389             : static GEN
    2390        5523 : getMorphism(GEN W1, GEN W2, GEN v)
    2391             : {
    2392             :   struct m_act S;
    2393             :   GEN B1, M, act;
    2394        5523 :   long a, l, k = msk_get_weight(W1);
    2395        5523 :   if (k == 2) return getMorphism_trivial(W1,W2,v);
    2396        2779 :   S.k = k;
    2397        2779 :   S.dim = k-1;
    2398        2779 :   S.act = &_RgX_act_Gl2Q;
    2399        2779 :   act = init_dual_act(v,W1,W2,&S);
    2400        2779 :   B1 = msk_get_basis(W1);
    2401        2779 :   l = lg(B1); M = cgetg(l, t_MAT);
    2402       61656 :   for (a = 1; a < l; a++)
    2403             :   {
    2404       58877 :     pari_sp av = avma;
    2405       58877 :     GEN phi = dual_act(S.dim, act, gel(B1,a));
    2406       58877 :     GEN D = getMorphism_basis(W2, phi);
    2407       58877 :     gel(M,a) = gerepilecopy(av, D);
    2408             :   }
    2409        2779 :   return M;
    2410             : }
    2411             : static GEN
    2412        4389 : msendo(GEN W, GEN v) { return getMorphism(W, W, v); }
    2413             : 
    2414             : static GEN
    2415        2583 : endo_project(GEN W, GEN e, GEN H)
    2416             : {
    2417        2583 :   if (msk_get_sign(W)) e = Qevproj_apply(e, msk_get_starproj(W));
    2418        2583 :   if (H) e = Qevproj_apply(e, Qevproj_init0(H));
    2419        2583 :   return e;
    2420             : }
    2421             : static GEN
    2422        2947 : mshecke_i(GEN W, ulong p)
    2423             : {
    2424        2947 :   GEN v = ms_get_N(W) % p? Tp_matrices(p): Up_matrices(p);
    2425        2947 :   return msendo(W,v);
    2426             : }
    2427             : GEN
    2428        2534 : mshecke(GEN W, long p, GEN H)
    2429             : {
    2430        2534 :   pari_sp av = avma;
    2431             :   GEN T;
    2432        2534 :   checkms(W);
    2433        2534 :   if (p <= 1) pari_err_PRIME("mshecke",stoi(p));
    2434        2534 :   T = mshecke_i(W,p);
    2435        2534 :   T = endo_project(W,T,H);
    2436        2534 :   return gerepilecopy(av, T);
    2437             : }
    2438             : 
    2439             : static GEN
    2440          42 : msatkinlehner_i(GEN W, long Q)
    2441             : {
    2442          42 :   long N = ms_get_N(W);
    2443             :   GEN v;
    2444          42 :   if (Q == 1) return matid(msk_get_dim(W));
    2445          28 :   if (Q == N) return msendo(W, mkvec(mat2(0,1,-N,0)));
    2446          21 :   if (N % Q) pari_err_DOMAIN("msatkinlehner","N % Q","!=",gen_0,stoi(Q));
    2447          14 :   v = WQ_matrix(N, Q);
    2448          14 :   if (!v) pari_err_DOMAIN("msatkinlehner","gcd(Q,N/Q)","!=",gen_1,stoi(Q));
    2449          14 :   return msendo(W,mkvec(v));
    2450             : }
    2451             : GEN
    2452          42 : msatkinlehner(GEN W, long Q, GEN H)
    2453             : {
    2454          42 :   pari_sp av = avma;
    2455             :   GEN w;
    2456             :   long k;
    2457          42 :   checkms(W);
    2458          42 :   k = msk_get_weight(W);
    2459          42 :   if (Q <= 0) pari_err_DOMAIN("msatkinlehner","Q","<=",gen_0,stoi(Q));
    2460          42 :   w = msatkinlehner_i(W,Q);
    2461          35 :   w = endo_project(W,w,H);
    2462          35 :   if (k > 2 && Q != 1) w = RgM_Rg_div(w, powuu(Q,(k-2)>>1));
    2463          35 :   return gerepilecopy(av, w);
    2464             : }
    2465             : 
    2466             : static GEN
    2467        1421 : msstar_i(GEN W) { return msendo(W, mkvec(mat2(-1,0,0,1))); }
    2468             : GEN
    2469          14 : msstar(GEN W, GEN H)
    2470             : {
    2471          14 :   pari_sp av = avma;
    2472             :   GEN s;
    2473          14 :   checkms(W);
    2474          14 :   s = msstar_i(W);
    2475          14 :   s = endo_project(W,s,H);
    2476          14 :   return gerepilecopy(av, s);
    2477             : }
    2478             : 
    2479             : #if 0
    2480             : /* is \Gamma_0(N) cusp1 = \Gamma_0(N) cusp2 ? */
    2481             : static int
    2482             : iscuspeq(ulong N, GEN cusp1, GEN cusp2)
    2483             : {
    2484             :   long p1, q1, p2, q2, s1, s2, d;
    2485             :   p1 = cusp1[1]; p2 = cusp2[1];
    2486             :   q1 = cusp1[2]; q2 = cusp2[2];
    2487             :   d = Fl_mul(smodss(q1,N),smodss(q2,N), N);
    2488             :   d = ugcd(d, N);
    2489             : 
    2490             :   s1 = q1 > 2? Fl_inv(smodss(p1,q1), q1): 1;
    2491             :   s2 = q2 > 2? Fl_inv(smodss(p2,q2), q2): 1;
    2492             :   return Fl_mul(s1,q2,d) == Fl_mul(s2,q1,d);
    2493             : }
    2494             : #endif
    2495             : 
    2496             : /* return E_c(r) */
    2497             : static GEN
    2498        2926 : get_Ec_r(GEN c, long k)
    2499             : {
    2500        2926 :   long p = c[1], q = c[2], u, v;
    2501             :   GEN gr;
    2502        2926 :   (void)cbezout(p, q, &u, &v);
    2503        2926 :   gr = mat2(p, -v, q, u); /* g . (1:0) = (p:q) */
    2504        2926 :   return voo_act_Gl2Q(sl2_inv(gr), k);
    2505             : }
    2506             : /* N > 1; returns the modular symbol attached to the cusp c := p/q via the rule
    2507             :  * E_c(path from a to b in Delta_0) := E_c(b) - E_c(a), where
    2508             :  * E_c(r) := 0 if r != c mod Gamma
    2509             :  *           v_oo | gamma_r^(-1)
    2510             :  * where v_oo is stable by T = [1,1;0,1] (i.e x^(k-2)) and
    2511             :  * gamma_r . (1:0) = r, for some gamma_r in SL_2(Z) * */
    2512             : static GEN
    2513         441 : msfromcusp_trivial(GEN W, GEN c)
    2514             : {
    2515         441 :   GEN section = ms_get_section(W), gen = ms_get_genindex(W);
    2516         441 :   GEN S = ms_get_hashcusps(W);
    2517         441 :   long j, ic = cusp_index(c, S), l = ms_get_nbE1(W)+1;
    2518         441 :   GEN phi = cgetg(l, t_COL);
    2519       90062 :   for (j = 1; j < l; j++)
    2520             :   {
    2521       89621 :     GEN vj, g = gel(section, gen[j]); /* path_to_zm(generator) */
    2522       89621 :     GEN c1 = gel(g,1), c2 = gel(g,2);
    2523       89621 :     long i1 = cusp_index(c1, S);
    2524       89621 :     long i2 = cusp_index(c2, S);
    2525       89621 :     if (i1 == ic)
    2526        3206 :       vj = (i2 == ic)?  gen_0: gen_1;
    2527             :     else
    2528       86415 :       vj = (i2 == ic)? gen_m1: gen_0;
    2529       89621 :     gel(phi, j) = vj;
    2530             :   }
    2531         441 :   return phi;
    2532             : }
    2533             : static GEN
    2534        1512 : msfromcusp_i(GEN W, GEN c)
    2535             : {
    2536             :   GEN section, gen, S, phi;
    2537        1512 :   long j, ic, l, k = msk_get_weight(W);
    2538        1512 :   if (k == 2)
    2539             :   {
    2540         441 :     long N = ms_get_N(W);
    2541         441 :     return N == 1? cgetg(1,t_COL): msfromcusp_trivial(W, c);
    2542             :   }
    2543        1071 :   k = msk_get_weight(W);
    2544        1071 :   section = ms_get_section(W);
    2545        1071 :   gen = ms_get_genindex(W);
    2546        1071 :   S = ms_get_hashcusps(W);
    2547        1071 :   ic = cusp_index(c, S);
    2548        1071 :   l = lg(gen);
    2549        1071 :   phi = cgetg(l, t_COL);
    2550       12579 :   for (j = 1; j < l; j++)
    2551             :   {
    2552       11508 :     GEN vj = NULL, g = gel(section, gen[j]); /* path_to_zm(generator) */
    2553       11508 :     GEN c1 = gel(g,1), c2 = gel(g,2);
    2554       11508 :     long i1 = cusp_index(c1, S);
    2555       11508 :     long i2 = cusp_index(c2, S);
    2556       11508 :     if (i1 == ic) vj = get_Ec_r(c1, k);
    2557       11508 :     if (i2 == ic)
    2558             :     {
    2559        1463 :       GEN s = get_Ec_r(c2, k);
    2560        1463 :       vj = vj? gsub(vj, s): gneg(s);
    2561             :     }
    2562       11508 :     if (!vj) vj = zerocol(k-1);
    2563       11508 :     gel(phi, j) = vj;
    2564             :   }
    2565        1071 :   return getMorphism_basis(W, phi);
    2566             : }
    2567             : GEN
    2568          28 : msfromcusp(GEN W, GEN c)
    2569             : {
    2570          28 :   pari_sp av = avma;
    2571             :   long N;
    2572          28 :   checkms(W);
    2573          28 :   N = ms_get_N(W);
    2574          28 :   switch(typ(c))
    2575             :   {
    2576             :     case t_INFINITY:
    2577           7 :       c = mkvecsmall2(1,0);
    2578           7 :       break;
    2579             :     case t_INT:
    2580          14 :       c = mkvecsmall2(smodis(c,N), 1);
    2581          14 :       break;
    2582             :     case t_FRAC:
    2583           7 :       c = mkvecsmall2(smodis(gel(c,1),N), smodis(gel(c,2),N));
    2584           7 :       break;
    2585             :     default:
    2586           0 :       pari_err_TYPE("msfromcusp",c);
    2587             :   }
    2588          28 :   return gerepilecopy(av, msfromcusp_i(W,c));
    2589             : }
    2590             : 
    2591             : static GEN
    2592         357 : mseisenstein_i(GEN W)
    2593             : {
    2594         357 :   GEN M, S = ms_get_hashcusps(W), cusps = gel(S,3);
    2595         357 :   long i, l = lg(cusps);
    2596         357 :   if (msk_get_weight(W)==2) l--;
    2597         357 :   M = cgetg(l, t_MAT);
    2598         357 :   for (i = 1; i < l; i++) gel(M,i) = msfromcusp_i(W, gel(cusps,i));
    2599         357 :   return Qevproj_star(W, QM_image(M));
    2600             : }
    2601             : GEN
    2602         357 : mseisenstein(GEN W)
    2603             : {
    2604         357 :   pari_sp av = avma;
    2605         357 :   checkms(W);
    2606         357 :   return gerepilecopy(av, Qevproj_init(mseisenstein_i(W)));
    2607             : }
    2608             : 
    2609             : /* upper bound for log_2 |charpoly(T_p|S)|, where S is a cuspidal subspace of
    2610             :  * dimension d, k is the weight */
    2611             : #if 0
    2612             : static long
    2613             : TpS_char_bound(ulong p, long k, long d)
    2614             : { /* |eigenvalue| <= 2 p^(k-1)/2 */
    2615             :   return d * (2 + (log2((double)p)*(k-1))/2);
    2616             : }
    2617             : #endif
    2618             : static long
    2619         336 : TpE_char_bound(ulong p, long k, long d)
    2620             : { /* |eigenvalue| <= 2 p^(k-1) */
    2621         336 :   return d * (2 + log2((double)p)*(k-1));
    2622             : }
    2623             : 
    2624             : GEN
    2625         336 : mscuspidal(GEN W, long flag)
    2626             : {
    2627         336 :   pari_sp av = avma;
    2628             :   GEN S, E, M, T, TE, chE;
    2629             :   long bit;
    2630             :   forprime_t F;
    2631             :   ulong p, N;
    2632             :   pari_timer ti;
    2633             : 
    2634         336 :   E = mseisenstein(W);
    2635         336 :   N = ms_get_N(W);
    2636         336 :   (void)u_forprime_init(&F, 2, ULONG_MAX);
    2637         336 :   while ((p = u_forprime_next(&F)))
    2638         469 :     if (N % p) break;
    2639         336 :   if (DEBUGLEVEL) timer_start(&ti);
    2640         336 :   T = mshecke(W, p, NULL);
    2641         336 :   if (DEBUGLEVEL) timer_printf(&ti,"Tp, p = %ld", p);
    2642         336 :   TE = Qevproj_apply(T, E); /* T_p | E */
    2643         336 :   if (DEBUGLEVEL) timer_printf(&ti,"Qevproj_init(E)");
    2644         336 :   bit = TpE_char_bound(p, msk_get_weight(W), lg(TE)-1);
    2645         336 :   chE = QM_charpoly_ZX_bound(TE, bit);
    2646         336 :   chE = ZX_radical(chE);
    2647         336 :   M = RgX_RgM_eval(chE, T);
    2648         336 :   S = Qevproj_init(QM_image(M));
    2649         336 :   return gerepilecopy(av, flag? mkvec2(S,E): S);
    2650             : }
    2651             : 
    2652             : /** INIT ELLSYM STRUCTURE **/
    2653             : /* V a vector of ZM. If all of them have 0 last row, return NULL.
    2654             :  * Otherwise return [m,i,j], where m = V[i][last,j] contains the value
    2655             :  * of smallest absolute value */
    2656             : static GEN
    2657         931 : RgMV_find_non_zero_last_row(long offset, GEN V)
    2658             : {
    2659         931 :   long i, lasti = 0, lastj = 0, lV = lg(V);
    2660         931 :   GEN m = NULL;
    2661        4074 :   for (i = 1; i < lV; i++)
    2662             :   {
    2663        3143 :     GEN M = gel(V,i);
    2664        3143 :     long j, n, l = lg(M);
    2665        3143 :     if (l == 1) continue;
    2666        2835 :     n = nbrows(M);
    2667       13804 :     for (j = 1; j < l; j++)
    2668             :     {
    2669       10969 :       GEN a = gcoeff(M, n, j);
    2670       10969 :       if (!gequal0(a) && (!m || abscmpii(a, m) < 0))
    2671             :       {
    2672        1582 :         m = a; lasti = i; lastj = j;
    2673        1582 :         if (is_pm1(m)) goto END;
    2674             :       }
    2675             :     }
    2676             :   }
    2677             : END:
    2678         931 :   if (!m) return NULL;
    2679         623 :   return mkvec2(m, mkvecsmall2(lasti+offset, lastj));
    2680             : }
    2681             : /* invert the d_oo := (\gamma_oo - 1) operator, acting on
    2682             :  * [x^(k-2), ..., y^(k-2)] */
    2683             : static GEN
    2684         623 : Delta_inv(GEN doo, long k)
    2685             : {
    2686         623 :   GEN M = RgX_act_ZGl2Q(doo, k);
    2687         623 :   M = RgM_minor(M, k-1, 1); /* 1st column and last row are 0 */
    2688         623 :   return ZM_inv_denom(M);
    2689             : }
    2690             : /* The ZX P = \sum a_i x^i y^{k-2-i} is given by the ZV [a_0, ..., a_k-2]~,
    2691             :  * return Q and d such that P = doo Q + d y^k-2, where d in Z and Q */
    2692             : static GEN
    2693       12831 : doo_decompose(GEN dinv, GEN P, GEN *pd)
    2694             : {
    2695       12831 :   long l = lg(P); *pd = gel(P, l-1);
    2696       12831 :   P = vecslice(P, 1, l-2);
    2697       12831 :   return shallowconcat(gen_0, ZC_apply_dinv(dinv, P));
    2698             : }
    2699             : 
    2700             : static GEN
    2701       12831 : get_phi_ij(long i,long j,long n, long s,long t,GEN P_st,GEN Q_st,GEN d_st,
    2702             :            GEN P_ij, GEN lP_ij, GEN dinv)
    2703             : {
    2704             :   GEN ind, pols;
    2705       12831 :   if (i == s && j == t)
    2706             :   {
    2707         623 :     ind = mkvecsmall(1);
    2708         623 :     pols = mkvec(scalarcol_shallow(gen_1, lg(P_st)-1)); /* x^{k-2} */
    2709             :   }
    2710             :   else
    2711             :   {
    2712       12208 :     GEN d_ij, Q_ij = doo_decompose(dinv, lP_ij, &d_ij);
    2713       12208 :     GEN a = ZC_Z_mul(P_ij, d_st);
    2714       12208 :     GEN b = ZC_Z_mul(P_st, negi(d_ij));
    2715       12208 :     GEN c = RgC_sub(RgC_Rg_mul(Q_ij, d_st), RgC_Rg_mul(Q_st, d_ij));
    2716       12208 :     if (i == s) { /* j != t */
    2717        1645 :       ind = mkvecsmall2(1, s);
    2718        1645 :       pols = mkvec2(c, ZC_add(a, b));
    2719             :     } else {
    2720       10563 :       ind = mkvecsmall3(1, i, s);
    2721       10563 :       pols = mkvec3(c, a, b); /* image of g_1, g_i, g_s */
    2722             :     }
    2723       12208 :     pols = Q_primpart(pols);
    2724             :   }
    2725       12831 :   return mkvec3(mkvecsmall3(i,j,n), ind, pols);
    2726             : }
    2727             : 
    2728             : static GEN
    2729         770 : mskinit_trivial(GEN WN)
    2730             : {
    2731         770 :   long dim = ms_get_nbE1(WN);
    2732         770 :   return mkvec3(WN, gen_0, mkvec2(gen_0,mkvecsmall2(2, dim)));
    2733             : }
    2734             : /* sum of #cols of the matrices contained in V */
    2735             : static long
    2736        1246 : RgMV_dim(GEN V)
    2737             : {
    2738        1246 :   long l = lg(V), d = 0, i;
    2739        1246 :   for (i = 1; i < l; i++) d += lg(gel(V,i)) - 1;
    2740        1246 :   return d;
    2741             : }
    2742             : static GEN
    2743         623 : mskinit_nontrivial(GEN WN, long k)
    2744             : {
    2745         623 :   GEN annT2 = gel(WN,8), annT31 = gel(WN,9), singlerel = gel(WN,10);
    2746             :   GEN link, basis, monomials, Inv;
    2747         623 :   long nbE1 = ms_get_nbE1(WN);
    2748         623 :   GEN dinv = Delta_inv(ZG_neg( ZSl2_star(gel(singlerel,1)) ), k);
    2749         623 :   GEN p1 = cgetg(nbE1+1, t_VEC), remove;
    2750         623 :   GEN p2 = ZGV_tors(annT2, k);
    2751         623 :   GEN p3 = ZGV_tors(annT31, k);
    2752         623 :   GEN gentor = shallowconcat(p2, p3);
    2753             :   GEN P_st, lP_st, Q_st, d_st;
    2754             :   long n, i, dim, s, t, u;
    2755         623 :   gel(p1, 1) = cgetg(1,t_MAT); /* dummy */
    2756        3360 :   for (i = 2; i <= nbE1; i++) /* skip 1st element = (\gamma_oo-1)g_oo */
    2757             :   {
    2758        2737 :     GEN z = gel(singlerel, i);
    2759        2737 :     gel(p1, i) = RgX_act_ZGl2Q(ZSl2_star(z), k);
    2760             :   }
    2761         623 :   remove = RgMV_find_non_zero_last_row(nbE1, gentor);
    2762         623 :   if (!remove) remove = RgMV_find_non_zero_last_row(0, p1);
    2763         623 :   if (!remove) pari_err_BUG("msinit [no y^k-2]");
    2764         623 :   remove = gel(remove,2); /* [s,t] */
    2765         623 :   s = remove[1];
    2766         623 :   t = remove[2];
    2767             :   /* +1 because of = x^(k-2), but -1 because of Manin relation */
    2768         623 :   dim = (k-1)*(nbE1-1) + RgMV_dim(p2) + RgMV_dim(p3);
    2769             :   /* Let (g_1,...,g_d) be the Gamma-generators of Delta, g_1 = g_oo.
    2770             :    * We describe modular symbols by the collection phi(g_1), ..., phi(g_d)
    2771             :    * \in V := Q[x,y]_{k-2}, with right Gamma action.
    2772             :    * For each i = 1, .., d, let V_i \subset V be the Q-vector space of
    2773             :    * allowed values for phi(g_i): with basis (P^{i,j}) given by the monomials
    2774             :    * x^(j-1) y^{k-2-(j-1)}, j = 1 .. k-1
    2775             :    * (g_i in E_1) or the solution of the torsion equations (1 + gamma)P = 0
    2776             :    * (g_i in T2) or (1 + gamma + gamma^2)P = 0 (g_i in T31). All such P
    2777             :    * are chosen in Z[x,y] with Q_content 1.
    2778             :    *
    2779             :    * The Manin relation (singlerel) is of the form \sum_i \lambda_i g_i = 0,
    2780             :    * where \lambda_i = 1 if g_i in T2 or T31, and \lambda_i = (1 - \gamma_i)
    2781             :    * for g_i in E1.
    2782             :    *
    2783             :    * If phi \in Hom_Gamma(Delta, V), it is defined by phi(g_i) := P_i in V
    2784             :    * with \sum_i P_i . \lambda_i^* = 0, where (\sum n_i g_i)^* :=
    2785             :    * \sum n_i \gamma_i^(-1).
    2786             :    *
    2787             :    * We single out gamma_1 / g_1 (g_oo in Pollack-Stevens paper) and
    2788             :    * write P_{i,j} \lambda_i^* =  Q_{i,j} (\gamma_1 - 1)^* + d_{i,j} y^{k-2}
    2789             :    * where d_{i,j} is a scalar and Q_{i,j} in V; we normalize Q_{i,j} to
    2790             :    * that the coefficient of x^{k-2} is 0.
    2791             :    *
    2792             :    * There exist (s,t) such that d_{s,t} != 0.
    2793             :    * A Q-basis of the (dual) space of modular symbols is given by the
    2794             :    * functions phi_{i,j}, 2 <= i <= d, 1 <= j <= k-1, mapping
    2795             :    *  g_1 -> d_{s,t} Q_{i,j} - d_{i,j} Q_{s,t} + [(i,j)=(s,t)] x^{k-2}
    2796             :    * If i != s
    2797             :    *   g_i -> d_{s,t} P_{i,j}
    2798             :    *   g_s -> - d_{i,j} P_{s,t}
    2799             :    * If i = s, j != t
    2800             :    *   g_i -> d_{s,t} P_{i,j} - d_{i,j} P_{s,t}
    2801             :    * And everything else to 0. Again we normalize the phi_{i,j} such that
    2802             :    * their image has content 1. */
    2803         623 :   monomials = matid(k-1); /* represent the monomials x^{k-2}, ... , y^{k-2} */
    2804         623 :   if (s <= nbE1) /* in E1 */
    2805             :   {
    2806         308 :     P_st = gel(monomials, t);
    2807         308 :     lP_st = gmael(p1, s, t); /* P_{s,t} lambda_s^* */
    2808             :   }
    2809             :   else /* in T2, T31 */
    2810             :   {
    2811         315 :     P_st = gmael(gentor, s - nbE1, t);
    2812         315 :     lP_st = P_st;
    2813             :   }
    2814         623 :   Q_st = doo_decompose(dinv, lP_st, &d_st);
    2815         623 :   basis = cgetg(dim+1, t_VEC);
    2816         623 :   link = cgetg(nbE1 + lg(gentor), t_VEC);
    2817         623 :   gel(link,1) = cgetg(1,t_VECSMALL); /* dummy */
    2818         623 :   n = 1;
    2819        3360 :   for (i = 2; i <= nbE1; i++)
    2820             :   {
    2821        2737 :     GEN L = cgetg(k, t_VECSMALL);
    2822             :     long j;
    2823             :     /* link[i][j] = n gives correspondance between phi_{i,j} and basis[n] */
    2824        2737 :     gel(link,i) = L;
    2825       14000 :     for (j = 1; j < k; j++)
    2826             :     {
    2827       11263 :       GEN lP_ij = gmael(p1, i, j); /* P_{i,j} lambda_i^* */
    2828       11263 :       GEN P_ij = gel(monomials,j);
    2829       11263 :       L[j] = n;
    2830       11263 :       gel(basis, n) = get_phi_ij(i,j,n, s,t, P_st, Q_st, d_st, P_ij, lP_ij, dinv);
    2831       11263 :       n++;
    2832             :     }
    2833             :   }
    2834        1127 :   for (u = 1; u < lg(gentor); u++,i++)
    2835             :   {
    2836         504 :     GEN V = gel(gentor,u);
    2837         504 :     long j, lV = lg(V);
    2838         504 :     GEN L = cgetg(lV, t_VECSMALL);
    2839         504 :     gel(link,i) = L;
    2840        2072 :     for (j = 1; j < lV; j++)
    2841             :     {
    2842        1568 :       GEN lP_ij = gel(V, j); /* P_{i,j} lambda_i^* = P_{i,j} */
    2843        1568 :       GEN P_ij = lP_ij;
    2844        1568 :       L[j] = n;
    2845        1568 :       gel(basis, n) = get_phi_ij(i,j,n, s,t, P_st, Q_st, d_st, P_ij, lP_ij, dinv);
    2846        1568 :       n++;
    2847             :     }
    2848             :   }
    2849         623 :   Inv = cgetg(lg(link), t_VEC);
    2850         623 :   gel(Inv,1) = cgetg(1, t_MAT); /* dummy */
    2851        3864 :   for (i = 2; i < lg(link); i++)
    2852             :   {
    2853        3241 :     GEN M, inv, B = gel(link,i);
    2854        3241 :     long j, lB = lg(B);
    2855        3241 :     if (i == s) { B = vecsplice(B, t); lB--; } /* remove phi_st */
    2856        3241 :     M = cgetg(lB, t_MAT);
    2857       15449 :     for (j = 1; j < lB; j++)
    2858             :     {
    2859       12208 :       GEN phi_ij = gel(basis, B[j]), pols = gel(phi_ij,3);
    2860       12208 :       gel(M, j) = gel(pols, 2); /* phi_ij(g_i) */
    2861             :     }
    2862        3241 :     if (i <= nbE1 && i != s) /* maximal rank k-1 */
    2863        2429 :       inv = ZM_inv_denom(M);
    2864             :     else /* i = s (rank k-2) or from torsion: rank k/3 or k/2 */
    2865         812 :       inv = Qevproj_init(M);
    2866        3241 :     gel(Inv,i) = inv;
    2867             :   }
    2868         623 :   return mkvec3(WN, gen_0, mkvec5(basis, mkvecsmall2(k, dim), mkvecsmall2(s,t),
    2869             :                                   link, Inv));
    2870             : }
    2871             : static GEN
    2872        1407 : add_star(GEN W, long sign)
    2873             : {
    2874        1407 :   GEN s = msstar_i(W);
    2875        1407 :   GEN K = sign? QM_ker_r(gsubgs(s, sign)): cgetg(1,t_MAT);
    2876        1407 :   gel(W,2) = mkvec3(stoi(sign), s, Qevproj_init(K));
    2877        1407 :   return W;
    2878             : }
    2879             : /* WN = msinit_N(N) */
    2880             : static GEN
    2881        1407 : mskinit(ulong N, long k, long sign)
    2882             : {
    2883        1407 :   GEN W, WN = msinit_N(N);
    2884        1407 :   if (N == 1)
    2885             :   {
    2886          14 :     GEN basis, M = RgXV_to_RgM(mfperiodpolbasis(k, 0), k-1);
    2887          14 :     GEN T = cgetg(1, t_VECSMALL), ind = mkvecsmall(1);
    2888          14 :     long i, l = lg(M);
    2889          14 :     basis = cgetg(l, t_VEC);
    2890          14 :     for (i = 1; i < l; i++) gel(basis,i) = mkvec3(T, ind, mkvec(gel(M,i)));
    2891          14 :     W = mkvec3(WN, gen_0, mkvec5(basis, mkvecsmall2(k, l-1), mkvecsmall2(0,0),
    2892             :                                  gen_0, Qevproj_init(M)));
    2893             :   }
    2894             :   else
    2895        1393 :     W = k == 2? mskinit_trivial(WN)
    2896        1393 :               : mskinit_nontrivial(WN, k);
    2897        1407 :   return add_star(W, sign);
    2898             : }
    2899             : GEN
    2900         462 : msinit(GEN N, GEN K, long sign)
    2901             : {
    2902         462 :   pari_sp av = avma;
    2903             :   GEN W;
    2904             :   long k;
    2905         462 :   if (typ(N) != t_INT) pari_err_TYPE("msinit", N);
    2906         462 :   if (typ(K) != t_INT) pari_err_TYPE("msinit", K);
    2907         462 :   k = itos(K);
    2908         462 :   if (k < 2) pari_err_DOMAIN("msinit","k", "<", gen_2,K);
    2909         462 :   if (odd(k)) pari_err_IMPL("msinit [odd weight]");
    2910         462 :   if (signe(N) <= 0) pari_err_DOMAIN("msinit","N", "<=", gen_0,N);
    2911         462 :   W = mskinit(itou(N), k, sign);
    2912         462 :   return gerepilecopy(av, W);
    2913             : }
    2914             : 
    2915             : /* W = msinit, xpm integral modular symbol of weight 2, c t_FRAC
    2916             :  * Return image of <oo->c> */
    2917             : static GEN
    2918        2065 : Q_xpm(GEN W, GEN xpm, GEN c)
    2919             : {
    2920        2065 :   pari_sp av = avma;
    2921             :   GEN v;
    2922        2065 :   W = get_msN(W);
    2923        2065 :   v = init_act_trivial(W);
    2924        2065 :   Q_log_trivial(v, W, c); /* oo -> (a:b), c = a/b */
    2925        2065 :   return gerepileuptoint(av, ZV_zc_mul(xpm, v));
    2926             : }
    2927             : 
    2928             : static GEN
    2929       20146 : eval_single(GEN s, long k, GEN B, long v)
    2930             : {
    2931             :   long i, l;
    2932       20146 :   GEN A = cgetg_copy(s,&l);
    2933       20146 :   for (i=1; i<l; i++) gel(A,i) = ZGl2Q_act_s(gel(B,i), gel(s,i), k);
    2934       20146 :   A = RgV_sum(A);
    2935       20146 :   if (is_vec_t(typ(A))) A = RgV_to_RgX(A, v);
    2936       20146 :   return A;
    2937             : }
    2938             : /* Evaluate symbol s on mspathlog B (= sum p_i g_i, p_i in Z[G]). Allow
    2939             :  * s = t_MAT [ collection of symbols, return a vector ]*/
    2940             : static GEN
    2941       15904 : mseval_by_values(GEN W, GEN s, GEN p, long v)
    2942             : {
    2943       15904 :   long i, l, k = msk_get_weight(W);
    2944             :   GEN A;
    2945       15904 :   if (k == 2)
    2946             :   { /* trivial represention: don't bother with Z[G] */
    2947        3444 :     GEN B = mspathlog_trivial(W,p);
    2948        3437 :     if (typ(s) != t_MAT) return RgV_zc_mul(s,B);
    2949        3367 :     l = lg(s); A = cgetg(l, t_VEC);
    2950        3367 :     for (i = 1; i < l; i++) gel(A,i) = RgV_zc_mul(gel(s,i), B);
    2951             :   }
    2952             :   else
    2953             :   {
    2954       12460 :     GEN B = mspathlog(W,p);
    2955       12460 :     if (typ(s) != t_MAT) return eval_single(s, k, B, v);
    2956         812 :     l = lg(s); A = cgetg(l, t_VEC);
    2957         812 :     for (i = 1; i < l; i++) gel(A,i) = eval_single(gel(s,i), k, B, v);
    2958             :   }
    2959        4179 :   return A;
    2960             : }
    2961             : 
    2962             : /* express symbol on the basis phi_{i,j} */
    2963             : static GEN
    2964       20524 : symtophi(GEN W, GEN s)
    2965             : {
    2966       20524 :   GEN e, basis = msk_get_basis(W);
    2967       20524 :   long i, l = lg(basis);
    2968       20524 :   if (lg(s) != l) pari_err_TYPE("mseval",s);
    2969       20524 :   e = zerovec(ms_get_nbgen(W));
    2970      312914 :   for (i=1; i<l; i++)
    2971             :   {
    2972      292390 :     GEN phi, ind, pols, c = gel(s,i);
    2973             :     long j, m;
    2974      292390 :     if (gequal0(c)) continue;
    2975      122528 :     phi = gel(basis,i);
    2976      122528 :     ind = gel(phi,2); m = lg(ind);
    2977      122528 :     pols = gel(phi,3);
    2978      470470 :     for (j=1; j<m; j++)
    2979             :     {
    2980      347942 :       long t = ind[j];
    2981      347942 :       gel(e,t) = gadd(gel(e,t), gmul(c, gel(pols,j)));
    2982             :     }
    2983             :   }
    2984       20524 :   return e;
    2985             : }
    2986             : /* evaluate symbol s on path p */
    2987             : GEN
    2988       16863 : mseval(GEN W, GEN s, GEN p)
    2989             : {
    2990       16863 :   pari_sp av = avma;
    2991       16863 :   long i, k, l, v = 0;
    2992       16863 :   checkms(W);
    2993       16863 :   k = msk_get_weight(W);
    2994       16863 :   switch(typ(s))
    2995             :   {
    2996             :     case t_VEC: /* values s(g_i) */
    2997           7 :       if (lg(s)-1 != ms_get_nbgen(W)) pari_err_TYPE("mseval",s);
    2998           7 :       if (!p) return gcopy(s);
    2999           0 :       v = gvar(s);
    3000           0 :       break;
    3001             :     case t_COL:
    3002       12663 :       if (msk_get_sign(W))
    3003             :       {
    3004         357 :         GEN star = gel(msk_get_starproj(W), 1);
    3005         357 :         if (lg(star) == lg(s)) s = RgM_RgC_mul(star, s);
    3006             :       }
    3007       12663 :       if (k == 2) /* on the dual basis of (g_i) */
    3008             :       {
    3009         637 :         if (lg(s)-1 != ms_get_nbE1(W)) pari_err_TYPE("mseval",s);
    3010         637 :         if (!p) return gtrans(s);
    3011             :       }
    3012             :       else
    3013       12026 :         s = symtophi(W,s);
    3014       12103 :       break;
    3015             :     case t_MAT:
    3016        4193 :       l = lg(s);
    3017        4193 :       if (!p)
    3018             :       {
    3019           7 :         GEN v = cgetg(l, t_VEC);
    3020           7 :         for (i = 1; i < l; i++) gel(v,i) = mseval(W, gel(s,i), NULL);
    3021           7 :         return v;
    3022             :       }
    3023        4186 :       if (l == 1) return cgetg(1, t_VEC);
    3024        4179 :       if (msk_get_sign(W))
    3025             :       {
    3026          84 :         GEN star = gel(msk_get_starproj(W), 1);
    3027          84 :         if (lg(star) == lgcols(s)) s = RgM_mul(star, s);
    3028             :       }
    3029        4179 :       if (k == 2)
    3030        3367 :       { if (nbrows(s) != ms_get_nbE1(W)) pari_err_TYPE("mseval",s); }
    3031             :       else
    3032             :       {
    3033         812 :         GEN t = cgetg(l, t_MAT);
    3034         812 :         for (i = 1; i < l; i++) gel(t,i) = symtophi(W,gel(s,i));
    3035         812 :         s = t;
    3036             :       }
    3037        4179 :       break;
    3038           0 :     default: pari_err_TYPE("mseval",s);
    3039             :   }
    3040       16282 :   if (p)
    3041       15904 :     s = mseval_by_values(W, s, p, v);
    3042             :   else
    3043             :   {
    3044         378 :     l = lg(s);
    3045        3675 :     for (i = 1; i < l; i++)
    3046             :     {
    3047        3297 :       GEN c = gel(s,i);
    3048        3297 :       if (!isintzero(c)) gel(s,i) = RgV_to_RgX(gel(s,i), v);
    3049             :     }
    3050             :   }
    3051       16275 :   return gerepilecopy(av, s);
    3052             : }
    3053             : 
    3054             : static GEN
    3055        1001 : allxpm(GEN W, GEN xpm, long f)
    3056             : {
    3057        1001 :   GEN v, L = coprimes_zv(f);
    3058        1001 :   long a, nonzero = 0;
    3059        1001 :   v = const_vec(f, NULL);
    3060        3815 :   for (a = 1; a <= f; a++)
    3061             :   {
    3062             :     GEN c;
    3063        2814 :     if (!L[a]) continue;
    3064        2065 :     c = Q_xpm(W, xpm, sstoQ(a, f));
    3065        2065 :     if (!gequal0(c)) { gel(v,a) = c; nonzero = 1; }
    3066             :   }
    3067        1001 :   return nonzero? v: NULL;
    3068             : }
    3069             : /* \sum_{a mod f} chi(a) x(a/f) */
    3070             : static GEN
    3071         483 : seval(GEN G, GEN chi, GEN vx)
    3072             : {
    3073         483 :   GEN vZ, T, s = gen_0, go = zncharorder(G,chi);
    3074         483 :   long i, l = lg(vx), o = itou(go);
    3075         483 :   T = polcyclo(o,0);
    3076         483 :   vZ = mkvec2(RgXQ_powers(RgX_rem(pol_x(0), T), o-1, T), go);
    3077        1939 :   for (i = 1; i < l; i++)
    3078             :   {
    3079        1456 :     GEN x = gel(vx,i);
    3080        1456 :     if (x) s = gadd(s, gmul(x, znchareval(G, chi, utoi(i), vZ)));
    3081             :   }
    3082         483 :   return gequal0(s)? NULL: poleval(s, rootsof1u_cx(o, DEFAULTPREC));
    3083             : }
    3084             : 
    3085             : static long
    3086         315 : nb_components(GEN E) { return signe(ell_get_disc(E)) > 0? 2: 1; }
    3087             : /* E minimal */
    3088             : static GEN
    3089         861 : ellperiod(GEN E, long s)
    3090             : {
    3091         861 :   GEN w = ellR_omega(E,DEFAULTPREC);
    3092         861 :   if (s == 1)
    3093         546 :     w = gel(w,1);
    3094         315 :   else if (nb_components(E) == 2)
    3095         140 :     w = gneg(gel(w,2));
    3096             :   else
    3097         175 :     w = mkcomplex(gen_0, gneg(gmul2n(imag_i(gel(w,2)), 1)));
    3098         861 :   return w;
    3099             : }
    3100             : 
    3101             : /* Let W = msinit(conductor(E), 2), xpm an integral modular symbol with the same
    3102             :  * eigenvalues as L_E. There exist a unique C such that
    3103             :  *   C*L(E,(D/.),1)_{xpm} = L(E,(D/.),1) / w1(E_D) != 0, for all D fundamental,
    3104             :  * sign(D) = s, and such that E_D has rank 0. Return C * ellperiod(E,s) */
    3105             : static GEN
    3106         483 : ell_get_Cw(GEN LE, GEN W, GEN xpm, long s)
    3107             : {
    3108         483 :   long f, NE = ms_get_N(W);
    3109         483 :   const long bit = 64;
    3110             : 
    3111        1456 :   for (f = 1;; f++)
    3112         973 :   { /* look for chi with conductor f coprime to N(E) and parity s
    3113             :      * such that L(E,chi,1) != 0 */
    3114        1456 :     pari_sp av = avma;
    3115             :     GEN vchi, vx, G;
    3116             :     long l, i;
    3117        1456 :     if ((f & 3) == 2 || ugcd(NE,f) != 1) continue;
    3118        1001 :     vx = allxpm(W, xpm, f); if (!vx) continue;
    3119         483 :     G = znstar0(utoipos(f),1);
    3120         483 :     vchi = chargalois(G,NULL); l = lg(vchi);
    3121         826 :     for (i = 1; i < l; i++)
    3122             :     {
    3123         826 :       pari_sp av2 = avma;
    3124         826 :       GEN tau, z, S, L, chi = gel(vchi,i);
    3125         826 :       long o = zncharisodd(G,chi);
    3126         826 :       if ((s > 0 && o) || (s < 0 && !o)
    3127         595 :           || itos(zncharconductor(G, chi)) != f) continue;
    3128         483 :       S = seval(G, chi, vx);
    3129         483 :       if (!S) { set_avma(av2); continue; }
    3130             : 
    3131         483 :       L = lfuntwist(LE, mkvec2(G, zncharconj(G,chi)));
    3132         483 :       z = lfun(L, gen_1, bit);
    3133         483 :       tau = znchargauss(G, chi, gen_1, bit);
    3134         966 :       return gdiv(gmul(z, tau), S); /* C * w */
    3135             :     }
    3136           0 :     set_avma(av);
    3137             :   }
    3138             : }
    3139             : static GEN
    3140         378 : ell_get_scale(GEN LE, GEN W, long sign, GEN x)
    3141             : {
    3142         378 :   if (sign)
    3143         273 :     return ell_get_Cw(LE, W, gel(x,1), sign);
    3144             :   else
    3145             :   {
    3146         105 :     GEN Cwp = ell_get_Cw(LE, W, gel(x,1), 1);
    3147         105 :     GEN Cwm = ell_get_Cw(LE, W, gel(x,2),-1);
    3148         105 :     return mkvec2(Cwp, Cwm);
    3149             :   }
    3150             : }
    3151             : /* E minimal */
    3152             : static GEN
    3153         567 : msfromell_scale(GEN x, GEN Cw, GEN E, long s)
    3154             : {
    3155         567 :   GEN B = int2n(32);
    3156         567 :   if (s)
    3157             :   {
    3158         273 :     GEN C = gdiv(Cw, ellperiod(E,s));
    3159         273 :     return ZC_Q_mul(gel(x,1), bestappr(C,B));
    3160             :   }
    3161             :   else
    3162             :   {
    3163         294 :     GEN xp = gel(x,1), Cp = gdiv(gel(Cw,1), ellperiod(E, 1)), L;
    3164         294 :     GEN xm = gel(x,2), Cm = gdiv(gel(Cw,2), ellperiod(E,-1));
    3165         294 :     xp = ZC_Q_mul(xp, bestappr(Cp,B));
    3166         294 :     xm = ZC_Q_mul(xm, bestappr(Cm,B));
    3167         294 :     if (signe(ell_get_disc(E)) > 0)
    3168         133 :       L = mkmat2(xp, xm); /* E(R) has 2 connected components */
    3169             :     else
    3170         161 :       L = mkmat2(gsub(xp,xm), gmul2n(xm,1));
    3171         294 :     return mkvec3(xp, xm, L);
    3172             :   }
    3173             : }
    3174             : /* v != 0 */
    3175             : static GEN
    3176         485 : Flc_normalize(GEN v, ulong p)
    3177             : {
    3178         485 :   long i, l = lg(v);
    3179         845 :   for (i = 1; i < l; i++)
    3180         845 :     if (v[i])
    3181             :     {
    3182         485 :       if (v[i] != 1) v = Flv_Fl_div(v, v[i], p);
    3183         485 :       return v;
    3184             :     }
    3185           0 :   return NULL;
    3186             : }
    3187             : /* K \cap Ker M  [F_l vector spaces]. K = NULL means full space */
    3188             : static GEN
    3189         415 : msfromell_ker(GEN K, GEN M, ulong l)
    3190             : {
    3191         415 :   GEN B, Ml = ZM_to_Flm(M, l);
    3192         415 :   if (K) Ml = Flm_mul(Ml, K, l);
    3193         415 :   B = Flm_ker(Ml, l);
    3194         415 :   if (!K) K = B;
    3195          36 :   else if (lg(B) < lg(K))
    3196          36 :     K = Flm_mul(K, B, l);
    3197         415 :   return K;
    3198             : }
    3199             : /* K = \cap_p Ker(T_p - a_p), 2-dimensional. Set *xl to the 1-dimensional
    3200             :  * Fl-basis  such that star . xl = sign . xl if sign != 0 and
    3201             :  * star * xl[1] = xl[1]; star * xl[2] = -xl[2] if sign = 0 */
    3202             : static void
    3203         379 : msfromell_l(GEN *pxl, GEN K, GEN star, long sign, ulong l)
    3204             : {
    3205         379 :   GEN s = ZM_to_Flm(star, l);
    3206         379 :   GEN a = gel(K,1), Sa = Flm_Flc_mul(s,a,l);
    3207         379 :   GEN b = gel(K,2);
    3208         379 :   GEN t = Flv_add(a,Sa,l), xp, xm;
    3209         379 :   if (zv_equal0(t))
    3210             :   {
    3211          14 :     xm = a;
    3212          14 :     xp = Flv_add(b,Flm_Flc_mul(s,b,l), l);
    3213             :   }
    3214             :   else
    3215             :   {
    3216         365 :     xp = t; t = Flv_sub(a, Sa, l);
    3217         365 :     xm = zv_equal0(t)? Flv_sub(b, Flm_Flc_mul(s,b,l), l): t;
    3218             :   }
    3219             :   /* xp = 0 on Im(S - 1), xm = 0 on Im(S + 1) */
    3220         379 :   if (sign > 0)
    3221         252 :     *pxl = mkmat(Flc_normalize(xp, l));
    3222         127 :   else if (sign < 0)
    3223          21 :     *pxl = mkmat(Flc_normalize(xm, l));
    3224             :   else
    3225         106 :     *pxl = mkmat2(Flc_normalize(xp, l), Flc_normalize(xm, l));
    3226         379 : }
    3227             : /* return a primitive symbol */
    3228             : static GEN
    3229         379 : msfromell_ratlift(GEN x, GEN q)
    3230             : {
    3231         379 :   GEN B = sqrti(shifti(q,-1));
    3232         379 :   GEN r = FpM_ratlift(x, q, B, B, NULL);
    3233         379 :   if (r) r = Q_primpart(r);
    3234         379 :   return r;
    3235             : }
    3236             : static int
    3237         379 : msfromell_check(GEN x, GEN vT, GEN star, long sign)
    3238             : {
    3239             :   long i, l;
    3240             :   GEN sx;
    3241         379 :   if (!x) return 0;
    3242         378 :   l = lg(vT);
    3243         791 :   for (i = 1; i < l; i++)
    3244             :   {
    3245         413 :     GEN T = gel(vT,i);
    3246         413 :     if (!gequal0(ZM_mul(T, x))) return 0; /* fail */
    3247             :   }
    3248         378 :   sx = ZM_mul(star,x);
    3249         378 :   if (sign)
    3250         273 :     return ZV_equal(gel(sx,1), sign > 0? gel(x,1): ZC_neg(gel(x,1)));
    3251             :   else
    3252         105 :     return ZV_equal(gel(sx,1),gel(x,1)) && ZV_equal(gel(sx,2),ZC_neg(gel(x,2)));
    3253             : }
    3254             : GEN
    3255         378 : msfromell(GEN E0, long sign)
    3256             : {
    3257         378 :   pari_sp av = avma, av2;
    3258         378 :   GEN T, Cw, E, NE, star, q, vT, xl, xr, W, x = NULL, K = NULL;
    3259             :   long lE, single;
    3260             :   ulong p, l, N;
    3261             :   forprime_t S, Sl;
    3262             : 
    3263         378 :   if (typ(E0) != t_VEC) pari_err_TYPE("msfromell",E0);
    3264         378 :   lE = lg(E0);
    3265         378 :   if (lE == 1) return cgetg(1,t_VEC);
    3266         378 :   single = (typ(gel(E0,1)) != t_VEC);
    3267         378 :   E = single ? E0: gel(E0,1);
    3268         378 :   NE = ellQ_get_N(E);
    3269             :   /* must make it integral for ellap; we have minimal model at hand */
    3270         378 :   T = obj_check(E, Q_MINIMALMODEL); if (lg(T) != 2) E = gel(T,3);
    3271         378 :   N = itou(NE); av2 = avma;
    3272         378 :   W = gerepilecopy(av2, mskinit(N,2,0));
    3273         378 :   star = msk_get_star(W);
    3274         378 :   init_modular_small(&Sl);
    3275             :   /* loop for p <= count_Manin_symbols(N) / 6 would be enough */
    3276         378 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
    3277         378 :   vT = cgetg(1, t_VEC);
    3278         378 :   l = u_forprime_next(&Sl);
    3279         378 :   while( (p = u_forprime_next(&S)) )
    3280             :   {
    3281             :     GEN M;
    3282         518 :     if (N % p == 0) continue;
    3283         413 :     av2 = avma;
    3284         413 :     M = RgM_Rg_sub_shallow(mshecke_i(W, p), ellap(E, utoipos(p)));
    3285         413 :     M = gerepilecopy(av2, M);
    3286         413 :     vT = vec_append(vT, M); /* for certification at the end */
    3287         413 :     K = msfromell_ker(K, M, l);
    3288         413 :     if (lg(K) == 3) break;
    3289             :   }
    3290         378 :   if (!p) pari_err_BUG("msfromell: ran out of primes");
    3291             : 
    3292             :   /* mod one l should be enough */
    3293         378 :   msfromell_l(&xl, K, star, sign, l);
    3294         378 :   x = ZM_init_CRT(xl, l);
    3295         378 :   q = utoipos(l);
    3296         378 :   xr = msfromell_ratlift(x, q);
    3297             :   /* paranoia */
    3298         757 :   while (!msfromell_check(xr, vT, star, sign) && (l = u_forprime_next(&Sl)) )
    3299             :   {
    3300           1 :     GEN K = NULL;
    3301           1 :     long i, lvT = lg(vT);
    3302           2 :     for (i = 1; i < lvT; i++)
    3303             :     {
    3304           2 :       K = msfromell_ker(K, gel(vT,i), l);
    3305           2 :       if (lg(K) == 3) break;
    3306             :     }
    3307           1 :     if (i >= lvT) { x = NULL; continue; }
    3308           1 :     msfromell_l(&xl, K, star, sign, l);
    3309           1 :     ZM_incremental_CRT(&x, xl, &q, l);
    3310           1 :     xr = msfromell_ratlift(x, q);
    3311             :   }
    3312             :   /* linear form = 0 on all Im(Tp - ap) and Im(S - sign) if sign != 0 */
    3313         378 :   Cw = ell_get_scale(lfuncreate(E), W, sign, xr);
    3314         378 :   if (single)
    3315         329 :     x = msfromell_scale(xr, Cw, E, sign);
    3316             :   else
    3317             :   { /* assume all E0[i] isogenous, given by minimal models */
    3318          49 :     GEN v = cgetg(lE, t_VEC);
    3319             :     long i;
    3320          49 :     for (i=1; i<lE; i++) gel(v,i) = msfromell_scale(xr, Cw, gel(E0,i), sign);
    3321          49 :     x = v;
    3322             :   }
    3323         378 :   return gerepilecopy(av, mkvec2(W, x));
    3324             : }
    3325             : 
    3326             : GEN
    3327          21 : msfromhecke(GEN W, GEN v, GEN H)
    3328             : {
    3329          21 :   pari_sp av = avma;
    3330          21 :   long i, l = lg(v);
    3331          21 :   GEN K = NULL;
    3332          21 :   checkms(W);
    3333          21 :   if (typ(v) != t_VEC) pari_err_TYPE("msfromhecke",v);
    3334          49 :   for (i = 1; i < l; i++)
    3335             :   {
    3336          28 :     GEN K2, T, p, P, c = gel(v,i);
    3337          28 :     if (typ(c) != t_VEC || lg(c) != 3) pari_err_TYPE("msfromhecke",v);
    3338          28 :     p = gel(c,1);
    3339          28 :     if (typ(p) != t_INT) pari_err_TYPE("msfromhecke",v);
    3340          28 :     P = gel(c,2);
    3341          28 :     switch(typ(P))
    3342             :     {
    3343             :       case t_INT:
    3344          21 :         P = deg1pol_shallow(gen_1, negi(P), 0);
    3345          21 :         break;
    3346             :       case t_POL:
    3347           7 :         if (RgX_is_ZX(P)) break;
    3348             :       default:
    3349           0 :         pari_err_TYPE("msfromhecke",v);
    3350             :     };
    3351          28 :     T = mshecke(W, itos(p), H);
    3352          28 :     T = Q_primpart(RgX_RgM_eval(P, T));
    3353          28 :     if (K) T = ZM_mul(T,K);
    3354          28 :     K2 = ZM_ker(T);
    3355          28 :     if (!K) K = K2;
    3356           7 :     else if (lg(K2) < lg(K)) K = ZM_mul(K,K2);
    3357             :   }
    3358          21 :   return gerepilecopy(av, K);
    3359             : }
    3360             : 
    3361             : /* OVERCONVERGENT MODULAR SYMBOLS */
    3362             : 
    3363             : static GEN
    3364        2933 : mspadic_get_Wp(GEN W) { return gel(W,1); }
    3365             : static GEN
    3366         483 : mspadic_get_Tp(GEN W) { return gel(W,2); }
    3367             : static GEN
    3368         483 : mspadic_get_bin(GEN W) { return gel(W,3); }
    3369             : static GEN
    3370         476 : mspadic_get_actUp(GEN W) { return gel(W,4); }
    3371             : static GEN
    3372         476 : mspadic_get_q(GEN W) { return gel(W,5); }
    3373             : static long
    3374        1456 : mspadic_get_p(GEN W) { return gel(W,6)[1]; }
    3375             : static long
    3376        1211 : mspadic_get_n(GEN W) { return gel(W,6)[2]; }
    3377             : static long
    3378         161 : mspadic_get_flag(GEN W) { return gel(W,6)[3]; }
    3379             : static GEN
    3380         483 : mspadic_get_M(GEN W) { return gel(W,7); }
    3381             : static GEN
    3382         483 : mspadic_get_C(GEN W) { return gel(W,8); }
    3383             : static long
    3384         973 : mspadic_get_weight(GEN W) { return msk_get_weight(mspadic_get_Wp(W)); }
    3385             : 
    3386             : void
    3387         980 : checkmspadic(GEN W)
    3388             : {
    3389         980 :   if (typ(W) != t_VEC || lg(W) != 9) pari_err_TYPE("checkmspadic",W);
    3390         980 :   checkms(mspadic_get_Wp(W));
    3391         980 : }
    3392             : 
    3393             : /* f in M_2(Z) \cap GL_2(Q), p \nmid a [ and for the result to mean anything
    3394             :  * p | c, but not needed here]. Return the matrix M in M_D(Z), D = M+k-1
    3395             :  * such that, if v = \int x^i d mu, i < D, is a vector of D moments of mu,
    3396             :  * then M * v is the vector of moments of mu | f  mod p^D */
    3397             : static GEN
    3398      276073 : moments_act_i(struct m_act *S, GEN f)
    3399             : {
    3400      276073 :   long j, k = S->k, D = S->dim;
    3401      276073 :   GEN a = gcoeff(f,1,1), b = gcoeff(f,1,2);
    3402      276073 :   GEN c = gcoeff(f,2,1), d = gcoeff(f,2,2);
    3403      276073 :   GEN u, z, q = S->q, mat = cgetg(D+1, t_MAT);
    3404             : 
    3405      276073 :   a = modii(a,q);
    3406      276073 :   c = modii(c,q);
    3407      276073 :   z = FpX_powu(deg1pol(c,a,0), k-2, q); /* (a+cx)^(k-2) */
    3408             :   /* u := (b+dx) / (a+cx) mod (q,x^D) = (b/a +d/a*x) / (1 - (-c/a)*x) */
    3409      276073 :   if (!equali1(a))
    3410             :   {
    3411      271229 :     GEN ai = Fp_inv(a,q);
    3412      271229 :     b = Fp_mul(b,ai,q);
    3413      271229 :     c = Fp_mul(c,ai,q);
    3414      271229 :     d = Fp_mul(d,ai,q);
    3415             :   }
    3416      276073 :   u = deg1pol_shallow(d, b, 0);
    3417             :   /* multiply by 1 / (1 - (-c/a)*x) */
    3418      276073 :   if (signe(c))
    3419             :   {
    3420      269640 :     GEN C = Fp_neg(c,q), v = cgetg(D+2,t_POL);
    3421      269640 :     v[1] = evalsigne(1)|evalvarn(0);
    3422      269640 :     gel(v, 2) = gen_1; gel(v, 3) = C;
    3423     1405138 :     for (j = 4; j < D+2; j++)
    3424             :     {
    3425     1329027 :       GEN t = Fp_mul(gel(v,j-1), C, q);
    3426     1329027 :       if (!signe(t)) { setlg(v,j); break; }
    3427     1135498 :       gel(v,j) = t;
    3428             :     }
    3429      269640 :     u = FpXn_mul(u, v, D, q);
    3430             :   }
    3431     2369024 :   for (j = 1; j <= D; j++)
    3432             :   {
    3433     2092951 :     gel(mat,j) = RgX_to_RgC(z, D); /* (a+cx)^(k-2) * ((b+dx)/(a+cx))^(j-1) */
    3434     2092951 :     if (j != D) z = FpXn_mul(z, u, D, q);
    3435             :   }
    3436      276073 :   return shallowtrans(mat);
    3437             : }
    3438             : static GEN
    3439      275611 : moments_act(struct m_act *S, GEN f)
    3440      275611 : { pari_sp av = avma; return gerepilecopy(av, moments_act_i(S,f)); }
    3441             : static GEN
    3442         483 : init_moments_act(GEN W, long p, long n, GEN q, GEN v)
    3443             : {
    3444             :   struct m_act S;
    3445         483 :   long k = msk_get_weight(W);
    3446         483 :   S.p = p;
    3447         483 :   S.k = k;
    3448         483 :   S.q = q;
    3449         483 :   S.dim = n+k-1;
    3450         483 :   S.act = &moments_act; return init_dual_act(v,W,W,&S);
    3451             : }
    3452             : 
    3453             : static void
    3454        6762 : clean_tail(GEN phi, long c, GEN q)
    3455             : {
    3456        6762 :   long a, l = lg(phi);
    3457      214438 :   for (a = 1; a < l; a++)
    3458             :   {
    3459      207676 :     GEN P = FpV_red(gel(phi,a), q); /* phi(G_a) = vector of moments */
    3460      207676 :     long j, lP = lg(P);
    3461      207676 :     for (j = c; j < lP; j++) gel(P,j) = gen_0; /* reset garbage to 0 */
    3462      207676 :     gel(phi,a) = P;
    3463             :   }
    3464        6762 : }
    3465             : /* concat z to all phi[i] */
    3466             : static GEN
    3467         630 : concat2(GEN phi, GEN z)
    3468             : {
    3469             :   long i, l;
    3470         630 :   GEN v = cgetg_copy(phi,&l);
    3471         630 :   for (i = 1; i < l; i++) gel(v,i) = shallowconcat(gel(phi,i), z);
    3472         630 :   return v;
    3473             : }
    3474             : static GEN
    3475         630 : red_mod_FilM(GEN phi, ulong p, long k, long flag)
    3476             : {
    3477             :   long a, l;
    3478         630 :   GEN den = gen_1, v = cgetg_copy(phi, &l);
    3479         630 :   if (flag)
    3480             :   {
    3481         343 :     phi = Q_remove_denom(phi, &den);
    3482         343 :     if (!den) { den = gen_1; flag = 0; }
    3483             :   }
    3484       29386 :   for (a = 1; a < l; a++)
    3485             :   {
    3486       28756 :     GEN P = gel(phi,a), q = den;
    3487             :     long j;
    3488      207676 :     for (j = lg(P)-1; j >= k+1; j--)
    3489             :     {
    3490      178920 :       q = muliu(q,p);
    3491      178920 :       gel(P,j) = modii(gel(P,j),q);
    3492             :     }
    3493       28756 :     q = muliu(q,p);
    3494       93380 :     for (     ; j >= 1; j--)
    3495       64624 :       gel(P,j) = modii(gel(P,j),q);
    3496       28756 :     gel(v,a) = P;
    3497             :   }
    3498         630 :   if (flag) v = gdiv(v, den);
    3499         630 :   return v;
    3500             : }
    3501             : 
    3502             : /* denom(C) | p^(2(k-1) - v_p(ap)) */
    3503             : static GEN
    3504         154 : oms_dim2(GEN W, GEN phi, GEN C, GEN ap)
    3505             : {
    3506         154 :   long t, i, k = mspadic_get_weight(W);
    3507         154 :   long p = mspadic_get_p(W), n = mspadic_get_n(W);
    3508         154 :   GEN phi1 = gel(phi,1), phi2 = gel(phi,2);
    3509         154 :   GEN v, q = mspadic_get_q(W);
    3510         154 :   GEN act = mspadic_get_actUp(W);
    3511             : 
    3512         154 :   t = signe(ap)? Z_lval(ap,p) : k-1;
    3513         154 :   phi1 = concat2(phi1, zerovec(n));
    3514         154 :   phi2 = concat2(phi2, zerovec(n));
    3515        2107 :   for (i = 1; i <= n; i++)
    3516             :   {
    3517        1953 :     phi1 = dual_act(k-1, act, phi1);
    3518        1953 :     phi1 = dual_act(k-1, act, phi1);
    3519        1953 :     clean_tail(phi1, k + i*t, q);
    3520             : 
    3521        1953 :     phi2 = dual_act(k-1, act, phi2);
    3522        1953 :     phi2 = dual_act(k-1, act, phi2);
    3523        1953 :     clean_tail(phi2, k + i*t, q);
    3524             :   }
    3525         154 :   C = gpowgs(C,n);
    3526         154 :   v = RgM_RgC_mul(C, mkcol2(phi1,phi2));
    3527         154 :   phi1 = red_mod_FilM(gel(v,1), p, k, 1);
    3528         154 :   phi2 = red_mod_FilM(gel(v,2), p, k, 1);
    3529         154 :   return mkvec2(phi1,phi2);
    3530             : }
    3531             : 
    3532             : /* flag = 0 iff alpha is a p-unit */
    3533             : static GEN
    3534         322 : oms_dim1(GEN W, GEN phi, GEN alpha, long flag)
    3535             : {
    3536         322 :   long i, k = mspadic_get_weight(W);
    3537         322 :   long p = mspadic_get_p(W), n = mspadic_get_n(W);
    3538         322 :   GEN q = mspadic_get_q(W);
    3539         322 :   GEN act = mspadic_get_actUp(W);
    3540         322 :   phi = concat2(phi, zerovec(n));
    3541        3178 :   for (i = 1; i <= n; i++)
    3542             :   {
    3543        2856 :     phi = dual_act(k-1, act, phi);
    3544        2856 :     clean_tail(phi, k + i, q);
    3545             :   }
    3546         322 :   phi = gmul(lift_shallow(gpowgs(alpha,n)), phi);
    3547         322 :   phi = red_mod_FilM(phi, p, k, flag);
    3548         322 :   return mkvec(phi);
    3549             : }
    3550             : 
    3551             : /* lift polynomial P in RgX[X,Y]_{k-2} to a distribution \mu such that
    3552             :  * \int (Y - X z)^(k-2) d\mu(z) = P(X,Y)
    3553             :  * Return the t_VEC of k-1 first moments of \mu: \int z^i d\mu(z), 0<= i < k-1.
    3554             :  *   \sum_j (-1)^(k-2-j) binomial(k-2,j) Y^j \int z^(k-2-j) d\mu(z) = P(1,Y)
    3555             :  * Input is P(1,Y), bin = vecbinomial(k-2): bin[j] = binomial(k-2,j-1) */
    3556             : static GEN
    3557       38626 : RgX_to_moments(GEN P, GEN bin)
    3558             : {
    3559       38626 :   long j, k = lg(bin);
    3560             :   GEN Pd, Bd;
    3561       38626 :   if (typ(P) != t_POL) P = scalarpol(P,0);
    3562       38626 :   P = RgX_to_RgC(P, k-1); /* deg <= k-2 */
    3563       38626 :   settyp(P, t_VEC);
    3564       38626 :   Pd = P+1;  /* Pd[i] = coeff(P,i) */
    3565       38626 :   Bd = bin+1;/* Bd[i] = binomial(k-2,i) */
    3566       46249 :   for (j = 1; j < k-2; j++)
    3567             :   {
    3568        7623 :     GEN c = gel(Pd,j);
    3569        7623 :     if (odd(j)) c = gneg(c);
    3570        7623 :     gel(Pd,j) = gdiv(c, gel(Bd,j));
    3571             :   }
    3572       38626 :   return vecreverse(P);
    3573             : }
    3574             : static GEN
    3575         882 : RgXC_to_moments(GEN v, GEN bin)
    3576             : {
    3577             :   long i, l;
    3578         882 :   GEN w = cgetg_copy(v,&l);
    3579         882 :   for (i=1; i<l; i++) gel(w,i) = RgX_to_moments(gel(v,i),bin);
    3580         882 :   return w;
    3581             : }
    3582             : 
    3583             : /* W an mspadic, assume O[2] is integral, den is the cancelled denominator
    3584             :  * or NULL, L = log(path)^* in sparse form */
    3585             : static GEN
    3586        2954 : omseval_int(struct m_act *S, GEN PHI, GEN L, hashtable *H)
    3587             : {
    3588             :   long i, l;
    3589        2954 :   GEN v = cgetg_copy(PHI, &l);
    3590        2954 :   ZGl2QC_to_act(S, L, H); /* as operators on V */
    3591        6286 :   for (i = 1; i < l; i++)
    3592             :   {
    3593        3332 :     GEN T = dense_act_col(L, gel(PHI,i));
    3594        3332 :     gel(v,i) = T? FpC_red(T,S->q): zerocol(S->dim);
    3595             :   }
    3596        2954 :   return v;
    3597             : }
    3598             : 
    3599             : GEN
    3600          14 : msomseval(GEN W, GEN phi, GEN path)
    3601             : {
    3602             :   struct m_act S;
    3603          14 :   pari_sp av = avma;
    3604             :   GEN v, Wp;
    3605             :   long n, vden;
    3606          14 :   checkmspadic(W);
    3607          14 :   if (typ(phi) != t_COL || lg(phi) != 4)  pari_err_TYPE("msomseval",phi);
    3608          14 :   vden = itos(gel(phi,2));
    3609          14 :   phi = gel(phi,1);
    3610          14 :   n = mspadic_get_n(W);
    3611          14 :   Wp= mspadic_get_Wp(W);
    3612          14 :   S.k = mspadic_get_weight(W);
    3613          14 :   S.p = mspadic_get_p(W);
    3614          14 :   S.q = powuu(S.p, n+vden);
    3615          14 :   S.dim = n + S.k - 1;
    3616          14 :   S.act = &moments_act;
    3617          14 :   path = path_to_M2(path);
    3618          14 :   v = omseval_int(&S, phi, M2_logf(Wp,path,NULL), NULL);
    3619          14 :   return gerepilecopy(av, v);
    3620             : }
    3621             : /* W = msinit(N,k,...); if flag < 0 or flag >= k-1, allow all symbols;
    3622             :  * else commit to v_p(a_p) <= flag (ordinary if flag = 0)*/
    3623             : GEN
    3624         490 : mspadicinit(GEN W, long p, long n, long flag)
    3625             : {
    3626         490 :   pari_sp av = avma;
    3627             :   long a, N, k;
    3628             :   GEN P, C, M, bin, Wp, Tp, q, pn, actUp, teich, pas;
    3629             : 
    3630         490 :   checkms(W);
    3631         490 :   N = ms_get_N(W);
    3632         490 :   k = msk_get_weight(W);
    3633         490 :   if (flag < 0) flag = 1; /* worst case */
    3634         357 :   else if (flag >= k) flag = k-1;
    3635             : 
    3636         490 :   bin = vecbinomial(k-2);
    3637         490 :   Tp = mshecke(W, p, NULL);
    3638         490 :   if (N % p == 0)
    3639             :   {
    3640          91 :     if ((N/p) % p == 0) pari_err_IMPL("mspadicinit when p^2 | N");
    3641             :     /* a_p != 0 */
    3642          84 :     Wp = W;
    3643          84 :     M = gen_0;
    3644          84 :     flag = (k-2) / 2; /* exact valuation */
    3645             :     /* will multiply by matrix with denominator p^(k-2)/2 in mspadicint.
    3646             :      * Except if p = 2 (multiply by alpha^2) */
    3647          84 :     if (p == 2) n += k-2; else n += (k-2)/2;
    3648          84 :     pn = powuu(p,n);
    3649             :     /* For accuracy mod p^n, oms_dim1 require p^(k/2*n) */
    3650          84 :     q = powiu(pn, k/2);
    3651             :   }
    3652             :   else
    3653             :   { /* p-stabilize */
    3654         399 :     long s = msk_get_sign(W);
    3655             :     GEN M1, M2;
    3656             : 
    3657         399 :     Wp = mskinit(N*p, k, s);
    3658         399 :     M1 = getMorphism(W, Wp, mkvec(mat2(1,0,0,1)));
    3659         399 :     M2 = getMorphism(W, Wp, mkvec(mat2(p,0,0,1)));
    3660         399 :     if (s)
    3661             :     {
    3662         147 :       GEN SW = msk_get_starproj(W), SWp = msk_get_starproj(Wp);
    3663         147 :       M1 = Qevproj_apply2(M1, SW, SWp);
    3664         147 :       M2 = Qevproj_apply2(M2, SW, SWp);
    3665             :     }
    3666         399 :     M = mkvec2(M1,M2);
    3667         399 :     n += Z_lval(Q_denom(M), p); /*den. introduced by p-stabilization*/
    3668             :     /* in supersingular case: will multiply by matrix with denominator p^k
    3669             :      * in mspadicint. Except if p = 2 (multiply by alpha^2) */
    3670         399 :     if (flag) { if (p == 2) n += 2*k-2; else n += k; }
    3671         399 :     pn = powuu(p,n);
    3672             :     /* For accuracy mod p^n, supersingular require p^((2k-1-v_p(a_p))*n) */
    3673         399 :     if (flag) /* k-1 also takes care of a_p = 0. Worst case v_p(a_p) = flag */
    3674         231 :       q = powiu(pn, 2*k-1 - flag);
    3675             :     else
    3676         168 :       q = pn;
    3677             :   }
    3678         483 :   actUp = init_moments_act(Wp, p, n, q, Up_matrices(p));
    3679             : 
    3680         483 :   if (p == 2) C = gen_0;
    3681             :   else
    3682             :   {
    3683         427 :     pas = matpascal(n);
    3684         427 :     teich = teichmullerinit(p, n+1);
    3685         427 :     P = gpowers(utoipos(p), n);
    3686         427 :     C = cgetg(p, t_VEC);
    3687        2317 :     for (a = 1; a < p; a++)
    3688             :     { /* powb[j+1] = ((a - w(a)) / p)^j mod p^n */
    3689        1890 :       GEN powb = Fp_powers(diviuexact(subui(a, gel(teich,a)), p), n, pn);
    3690        1890 :       GEN Ca = cgetg(n+2, t_VEC);
    3691        1890 :       long j, r, ai = Fl_inv(a, p); /* a^(-1) */
    3692        1890 :       gel(C,a) = Ca;
    3693       22134 :       for (j = 0; j <= n; j++)
    3694             :       {
    3695       20244 :         GEN Caj = cgetg(j+2, t_VEC);
    3696       20244 :         GEN atij = gel(teich, Fl_powu(ai,j,p));/* w(a)^(-j) = w(a^(-j) mod p) */
    3697       20244 :         gel(Ca,j+1) = Caj;
    3698      158200 :         for (r = 0; r <= j; r++)
    3699             :         {
    3700      137956 :           GEN c = Fp_mul(gcoeff(pas,j+1,r+1), gel(powb, j-r+1), pn);
    3701      137956 :           c = Fp_mul(c,atij,pn); /* binomial(j,r)*b^(j-r)*w(a)^(-j) mod p^n */
    3702      137956 :           gel(Caj,r+1) = mulii(c, gel(P,j+1)); /* p^j * c mod p^(n+j) */
    3703             :         }
    3704             :       }
    3705             :     }
    3706             :   }
    3707         483 :   return gerepilecopy(av, mkvecn(8, Wp,Tp, bin, actUp, q,
    3708             :                                  mkvecsmall3(p,n,flag), M, C));
    3709             : }
    3710             : 
    3711             : #if 0
    3712             : /* assume phi an ordinary OMS */
    3713             : static GEN
    3714             : omsactgl2(GEN W, GEN phi, GEN M)
    3715             : {
    3716             :   GEN q, Wp, act;
    3717             :   long p, k, n;
    3718             :   checkmspadic(W);
    3719             :   Wp = mspadic_get_Wp(W);
    3720             :   p = mspadic_get_p(W);
    3721             :   k = mspadic_get_weight(W);
    3722             :   n = mspadic_get_n(W);
    3723             :   q = mspadic_get_q(W);
    3724             :   act = init_moments_act(Wp, p, n, q, M);
    3725             :   phi = gel(phi,1);
    3726             :   return dual_act(k-1, act, gel(phi,1));
    3727             : }
    3728             : #endif
    3729             : 
    3730             : static GEN
    3731         483 : eigenvalue(GEN T, GEN x)
    3732             : {
    3733         483 :   long i, l = lg(x);
    3734         637 :   for (i = 1; i < l; i++)
    3735         637 :     if (!isintzero(gel(x,i))) break;
    3736         483 :   if (i == l) pari_err_DOMAIN("mstooms", "phi", "=", gen_0, x);
    3737         483 :   return gdiv(RgMrow_RgC_mul(T,x,i), gel(x,i));
    3738             : }
    3739             : 
    3740             : /* p coprime to ap, return unit root of x^2 - ap*x + p^(k-1), accuracy p^n */
    3741             : GEN
    3742         266 : mspadic_unit_eigenvalue(GEN ap, long k, GEN p, long n)
    3743             : {
    3744         266 :   GEN sqrtD, D = subii(sqri(ap), shifti(powiu(p,k-1),2));
    3745         266 :   if (absequaliu(p,2))
    3746             :   {
    3747           7 :     n++; sqrtD = Zp_sqrt(D, p, n);
    3748           7 :     if (mod4(sqrtD) != mod4(ap)) sqrtD = negi(sqrtD);
    3749             :   }
    3750             :   else
    3751         259 :     sqrtD = Zp_sqrtlift(D, ap, p, n);
    3752             :   /* sqrtD = ap (mod p) */
    3753         266 :   return gmul2n(gadd(ap, cvtop(sqrtD,p,n)), -1);
    3754             : }
    3755             : 
    3756             : /* W = msinit(N,k,...); phi = T_p/U_p - eigensymbol */
    3757             : GEN
    3758         483 : mstooms(GEN W, GEN phi)
    3759             : {
    3760         483 :   pari_sp av = avma;
    3761             :   GEN Wp, bin, Tp, c, alpha, ap, phi0, M;
    3762             :   long k, p, vden;
    3763             : 
    3764         483 :   checkmspadic(W);
    3765         483 :   if (typ(phi) != t_COL)
    3766             :   {
    3767         161 :     if (!is_Qevproj(phi)) pari_err_TYPE("mstooms",phi);
    3768         161 :     phi = gel(phi,1);
    3769         161 :     if (lg(phi) != 2) pari_err_TYPE("mstooms [dim_Q (eigenspace) > 1]",phi);
    3770         161 :     phi = gel(phi,1);
    3771             :   }
    3772             : 
    3773         483 :   Wp = mspadic_get_Wp(W);
    3774         483 :   Tp = mspadic_get_Tp(W);
    3775         483 :   bin = mspadic_get_bin(W);
    3776         483 :   k = msk_get_weight(Wp);
    3777         483 :   p = mspadic_get_p(W);
    3778         483 :   M = mspadic_get_M(W);
    3779             : 
    3780         483 :   phi = Q_remove_denom(phi, &c);
    3781         483 :   ap = eigenvalue(Tp, phi);
    3782         483 :   vden = c? Z_lvalrem(c, p, &c): 0;
    3783             : 
    3784         483 :   if (typ(M) == t_INT)
    3785             :   { /* p | N */
    3786             :     GEN c1;
    3787          84 :     alpha = ap;
    3788          84 :     alpha = ginv(alpha);
    3789          84 :     phi0 = mseval(Wp, phi, NULL);
    3790          84 :     phi0 = RgXC_to_moments(phi0, bin);
    3791          84 :     phi0 = Q_remove_denom(phi0, &c1);
    3792          84 :     if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3793          84 :     if (umodiu(ap,p)) /* p \nmid a_p */
    3794          49 :       phi = oms_dim1(W, phi0, alpha, 0);
    3795             :     else
    3796             :     {
    3797          35 :       phi = oms_dim1(W, phi0, alpha, 1);
    3798          35 :       phi = Q_remove_denom(phi, &c1);
    3799          35 :       if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3800             :     }
    3801             :   }
    3802             :   else
    3803             :   { /* p-stabilize */
    3804             :     GEN M1, M2, phi1, phi2, c1;
    3805         399 :     if (typ(M) != t_VEC || lg(M) != 3) pari_err_TYPE("mstooms",W);
    3806         399 :     M1 = gel(M,1);
    3807         399 :     M2 = gel(M,2);
    3808             : 
    3809         399 :     phi1 = RgM_RgC_mul(M1, phi);
    3810         399 :     phi2 = RgM_RgC_mul(M2, phi);
    3811         399 :     phi1 = mseval(Wp, phi1, NULL);
    3812         399 :     phi2 = mseval(Wp, phi2, NULL);
    3813             : 
    3814         399 :     phi1 = RgXC_to_moments(phi1, bin);
    3815         399 :     phi2 = RgXC_to_moments(phi2, bin);
    3816         399 :     phi = Q_remove_denom(mkvec2(phi1,phi2), &c1);
    3817         399 :     phi1 = gel(phi,1);
    3818         399 :     phi2 = gel(phi,2);
    3819         399 :     if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3820             :     /* all polynomials multiplied by c p^vden */
    3821         399 :     if (umodiu(ap, p))
    3822             :     {
    3823         238 :       alpha = mspadic_unit_eigenvalue(ap, k, utoipos(p), mspadic_get_n(W));
    3824         238 :       alpha = ginv(alpha);
    3825         238 :       phi0 = gsub(phi1, gmul(lift_shallow(alpha),phi2));
    3826         238 :       phi = oms_dim1(W, phi0, alpha, 0);
    3827             :     }
    3828             :     else
    3829             :     { /* p | ap, alpha = [a_p, -1; p^(k-1), 0] */
    3830         161 :       long flag = mspadic_get_flag(W);
    3831         161 :       if (!flag || (signe(ap) && Z_lval(ap,p) < flag))
    3832           7 :         pari_err_TYPE("mstooms [v_p(ap) > mspadicinit flag]", phi);
    3833         154 :       alpha = mkmat22(ap,gen_m1, powuu(p, k-1),gen_0);
    3834         154 :       alpha = ginv(alpha);
    3835         154 :       phi = oms_dim2(W, mkvec2(phi1,phi2), gsqr(alpha), ap);
    3836         154 :       phi = Q_remove_denom(phi, &c1);
    3837         154 :       if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3838             :     }
    3839             :   }
    3840         476 :   if (vden) c = mul_denom(c, powuu(p,vden));
    3841         476 :   if (p == 2) alpha = gsqr(alpha);
    3842         476 :   if (c) alpha = gdiv(alpha,c);
    3843         476 :   if (typ(alpha) == t_MAT)
    3844             :   { /* express in basis (omega,-p phi(omega)) */
    3845         154 :     gcoeff(alpha,2,1) = gdivgs(gcoeff(alpha,2,1), -p);
    3846         154 :     gcoeff(alpha,2,2) = gdivgs(gcoeff(alpha,2,2), -p);
    3847             :     /* at the end of mspadicint we shall multiply result by [1,0;0,-1/p]*alpha
    3848             :      * vden + k is the denominator of this matrix */
    3849             :   }
    3850             :   /* phi is integral-valued */
    3851         476 :   return gerepilecopy(av, mkcol3(phi, stoi(vden), alpha));
    3852             : }
    3853             : 
    3854             : /* HACK: the v[j] have different lengths */
    3855             : static GEN
    3856        2156 : FpVV_dotproduct(GEN v, GEN w, GEN p)
    3857             : {
    3858        2156 :   long j, l = lg(v);
    3859        2156 :   GEN T = cgetg(l, t_VEC);
    3860        2156 :   for (j = 1; j < l; j++) gel(T,j) = FpV_dotproduct(gel(v,j),w,p);
    3861        2156 :   return T;
    3862             : }
    3863             : 
    3864             : /* \int (-4z)^j given \int z^j */
    3865             : static GEN
    3866          98 : twistmoment_m4(GEN v)
    3867             : {
    3868             :   long i, l;
    3869          98 :   GEN w = cgetg_copy(v, &l);
    3870        2009 :   for (i = 1; i < l; i++)
    3871             :   {
    3872        1911 :     GEN c = gel(v,i);
    3873        1911 :     if (i > 1) c = gmul2n(c, (i-1)<<1);
    3874        1911 :     gel(w,i) = odd(i)? c: gneg(c);
    3875             :   }
    3876          98 :   return w;
    3877             : }
    3878             : /* \int (4z)^j given \int z^j */
    3879             : static GEN
    3880          98 : twistmoment_4(GEN v)
    3881             : {
    3882             :   long i, l;
    3883          98 :   GEN w = cgetg_copy(v, &l);
    3884        2009 :   for (i = 1; i < l; i++)
    3885             :   {
    3886        1911 :     GEN c = gel(v,i);
    3887        1911 :     if (i > 1) c = gmul2n(c, (i-1)<<1);
    3888        1911 :     gel(w,i) = c;
    3889             :   }
    3890          98 :   return w;
    3891             : }
    3892             : /* W an mspadic, phi eigensymbol, p \nmid D. Return C(x) mod FilM */
    3893             : GEN
    3894         483 : mspadicmoments(GEN W, GEN PHI, long D)
    3895             : {
    3896         483 :   pari_sp av = avma;
    3897         483 :   long na, ia, b, lphi, aD = labs(D), pp, p, k, n, vden;
    3898             :   GEN Wp, Dact, vL, v, C, pn, phi;
    3899             :   struct m_act S;
    3900             :   hashtable *H;
    3901             : 
    3902         483 :   checkmspadic(W);
    3903         483 :   Wp = mspadic_get_Wp(W);
    3904         483 :   p = mspadic_get_p(W);
    3905         483 :   k = mspadic_get_weight(W);
    3906         483 :   n = mspadic_get_n(W);
    3907         483 :   C = mspadic_get_C(W);
    3908         483 :   if (typ(PHI) != t_COL || lg(PHI) != 4 || typ(gel(PHI,1)) != t_VEC)
    3909         476 :     PHI = mstooms(W, PHI);
    3910         476 :   vden = itos( gel(PHI,2) );
    3911         476 :   phi = gel(PHI,1); lphi = lg(phi);
    3912         476 :   if (p == 2) { na = 2; pp = 4; }
    3913         420 :   else        { na = p-1; pp = p; }
    3914         476 :   pn = powuu(p, n + vden);
    3915             : 
    3916         476 :   S.p = p;
    3917         476 :   S.k = k;
    3918         476 :   S.q = pn;
    3919         476 :   S.dim = n+k-1;
    3920         476 :   S.act = &moments_act;
    3921         476 :   H = Gl2act_cache(ms_get_nbgen(Wp));
    3922         476 :   if (D == 1) Dact = NULL;
    3923             :   else
    3924             :   {
    3925          63 :     GEN gaD = utoi(aD), Dk = Fp_pows(stoi(D), 2-k, pn);
    3926          63 :     if (!sisfundamental(D)) pari_err_TYPE("mspadicmoments", stoi(D));
    3927          63 :     if (D % p == 0) pari_err_DOMAIN("mspadicmoments","p","|", stoi(D), utoi(p));
    3928          63 :     Dact = cgetg(aD, t_VEC);
    3929         532 :     for (b = 1; b < aD; b++)
    3930             :     {
    3931         469 :       GEN z = NULL;
    3932         469 :       long s = kross(D, b);
    3933         469 :       if (s)
    3934             :       {
    3935         462 :         pari_sp av2 = avma;
    3936             :         GEN d;
    3937         462 :         z = moments_act_i(&S, mkmat22(gaD,utoipos(b), gen_0,gaD));
    3938         462 :         d = s > 0? Dk: Fp_neg(Dk, pn);
    3939         924 :         z = equali1(d)? gerepilecopy(av2, z)
    3940         462 :                       : gerepileupto(av2, FpM_Fp_mul(z, d, pn));
    3941             :       }
    3942         469 :       gel(Dact,b) = z;
    3943             :     }
    3944             :   }
    3945         476 :   vL = cgetg(na+1,t_VEC);
    3946             :   /* first pass to precompute log(paths), preload matrices and allow GC later */
    3947        2464 :   for (ia = 1; ia <= na; ia++)
    3948             :   {
    3949             :     GEN path, La;
    3950        1988 :     long a = (p == 2 && ia == 2)? -1: ia;
    3951        1988 :     if (Dact)
    3952             :     { /* twist by D */
    3953         224 :       La = cgetg(aD, t_VEC);
    3954        1442 :       for (b = 1; b < aD; b++)
    3955             :       {
    3956        1218 :         GEN Actb = gel(Dact,b);
    3957        1218 :         if (!Actb) continue;
    3958             :         /* oo -> a/pp + b/|D|*/
    3959        1176 :         path = mkmat22(gen_1, addii(mulss(a, aD), muluu(pp, b)),
    3960             :                        gen_0, muluu(pp, aD));
    3961        1176 :         gel(La,b) = M2_logf(Wp,path,NULL);
    3962        1176 :         ZGl2QC_preload(&S, gel(La,b), H);
    3963             :       }
    3964             :     }
    3965             :     else
    3966             :     {
    3967        1764 :       path = mkmat22(gen_1,stoi(a), gen_0, utoipos(pp));
    3968        1764 :       La = M2_logf(Wp,path,NULL);
    3969        1764 :       ZGl2QC_preload(&S, La, H);
    3970             :     }
    3971        1988 :     gel(vL,ia) = La;
    3972             :   }
    3973         476 :   v = cgetg(na+1,t_VEC);
    3974             :   /* second pass, with GC */
    3975        2464 :   for (ia = 1; ia <= na; ia++)
    3976             :   {
    3977        1988 :     pari_sp av2 = avma;
    3978        1988 :     GEN vca, Ca = gel(C,ia), La = gel(vL,ia), va = cgetg(lphi, t_VEC);
    3979             :     long i;
    3980        1988 :     if (!Dact) vca = omseval_int(&S, phi, La, H);
    3981             :     else
    3982             :     { /* twist by D */
    3983         224 :       vca = cgetg(lphi,t_VEC);
    3984        1442 :       for (b = 1; b < aD; b++)
    3985             :       {
    3986        1218 :         GEN T, Actb = gel(Dact,b);
    3987        1218 :         if (!Actb) continue;
    3988        1176 :         T = omseval_int(&S, phi, gel(La,b), H);
    3989        2352 :         for (i = 1; i < lphi; i++)
    3990             :         {
    3991        1176 :           GEN z = FpM_FpC_mul(Actb, gel(T,i), pn);
    3992        1176 :           gel(vca,i) = b==1? z: ZC_add(gel(vca,i), z);
    3993             :         }
    3994             :       }
    3995             :     }
    3996        1988 :     if (p != 2)
    3997        1876 :     { for (i=1; i<lphi; i++) gel(va,i) = FpVV_dotproduct(Ca,gel(vca,i),pn); }
    3998         112 :     else if (ia == 1) /* \tilde{a} = 1 */
    3999          56 :     { for (i=1; i<lphi; i++) gel(va,i) = twistmoment_4(gel(vca,i)); }
    4000             :     else /* \tilde{a} = -1 */
    4001          56 :     { for (i=1; i<lphi; i++) gel(va,i) = twistmoment_m4(gel(vca,i)); }
    4002        1988 :     gel(v,ia) = gerepilecopy(av2, va);
    4003             :   }
    4004         476 :   return gerepilecopy(av, mkvec3(v, gel(PHI,3), mkvecsmall4(p,n+vden,n,D)));
    4005             : }
    4006             : static void
    4007        1918 : checkoms(GEN v)
    4008             : {
    4009        1918 :   if (typ(v) != t_VEC || lg(v) != 4 || typ(gel(v,1)) != t_VEC
    4010        1918 :       || typ(gel(v,3))!=t_VECSMALL)
    4011           0 :     pari_err_TYPE("checkoms [apply mspadicmoments]", v);
    4012        1918 : }
    4013             : static long
    4014        4284 : oms_get_p(GEN oms) { return gel(oms,3)[1]; }
    4015             : static long
    4016        4186 : oms_get_n(GEN oms) { return gel(oms,3)[2]; }
    4017             : static long
    4018        2464 : oms_get_n0(GEN oms) { return gel(oms,3)[3]; }
    4019             : static long
    4020        1918 : oms_get_D(GEN oms) { return gel(oms,3)[4]; }
    4021             : static int
    4022          98 : oms_is_supersingular(GEN oms) { GEN v = gel(oms,1); return lg(gel(v,1)) == 3; }
    4023             : 
    4024             : /* sum(j = 1, n, (-1)^(j+1)/j * x^j) */
    4025             : static GEN
    4026         784 : log1x(long n)
    4027             : {
    4028         784 :   long i, l = n+3;
    4029         784 :   GEN v = cgetg(l, t_POL);
    4030         784 :   v[1] = evalvarn(0)|evalsigne(1); gel(v,2) = gen_0;
    4031         784 :   for (i = 3; i < l; i++) gel(v,i) = ginv(stoi(odd(i)? i-2: 2-i));
    4032         784 :   return v;
    4033             : }
    4034             : 
    4035             : /* S = (1+x)^zk log(1+x)^logj (mod x^(n+1)) */
    4036             : static GEN
    4037        1820 : xlog1x(long n, long zk, long logj, long *pteich)
    4038             : {
    4039        1820 :   GEN S = logj? RgXn_powu_i(log1x(n), logj, n+1): NULL;
    4040        1820 :   if (zk)
    4041             :   {
    4042        1183 :     GEN L = deg1pol_shallow(gen_1, gen_1, 0); /* x+1 */
    4043        1183 :     *pteich += zk;
    4044        1183 :     if (zk < 0) { L = RgXn_inv(L,n+1); zk = -zk; }
    4045        1183 :     if (zk != 1) L = RgXn_powu_i(L, zk, n+1);
    4046        1183 :     S = S? RgXn_mul(S, L, n+1): L;
    4047             :   }
    4048        1820 :   return S;
    4049             : }
    4050             : 
    4051             : /* oms from mspadicmoments; integrate teichmuller^i * S(x) [S = NULL: 1]*/
    4052             : static GEN
    4053        2366 : mspadicint(GEN oms, long teichi, GEN S)
    4054             : {
    4055        2366 :   pari_sp av = avma;
    4056        2366 :   long p = oms_get_p(oms), n = oms_get_n(oms), n0 = oms_get_n0(oms);
    4057        2366 :   GEN vT = gel(oms,1), alpha = gel(oms,2), gp = utoipos(p);
    4058        2366 :   long loss = S? Z_lval(Q_denom(S), p): 0;
    4059        2366 :   long nfinal = minss(n-loss, n0);
    4060        2366 :   long i, la, l = lg(gel(vT,1));
    4061        2366 :   GEN res = cgetg(l, t_COL), teich = NULL;
    4062             : 
    4063        2366 :   if (S) S = RgX_to_RgC(S,lg(gmael(vT,1,1))-1);
    4064        2366 :   if (p == 2)
    4065             :   {
    4066         448 :     la = 3; /* corresponds to [1,-1] */
    4067         448 :     teichi &= 1;
    4068             :   }
    4069             :   else
    4070             :   {
    4071        1918 :     la = p; /* corresponds to [1,2,...,p-1] */
    4072        1918 :     teichi = smodss(teichi, p-1);
    4073        1918 :     if (teichi) teich = teichmullerinit(p, n);
    4074             :   }
    4075        5446 :   for (i=1; i<l; i++)
    4076             :   {
    4077        3080 :     pari_sp av2 = avma;
    4078        3080 :     GEN s = gen_0;
    4079             :     long ia;
    4080       14756 :     for (ia = 1; ia < la; ia++)
    4081             :     { /* Ta[j+1] correct mod p^n */
    4082       11676 :       GEN Ta = gmael(vT,ia,i), v = S? RgV_dotproduct(Ta, S): gel(Ta,1);
    4083       11676 :       if (teichi && ia != 1)
    4084             :       {
    4085        3843 :         if (p != 2)
    4086        3626 :           v = gmul(v, gel(teich, Fl_powu(ia,teichi,p)));
    4087             :         else
    4088         217 :           if (teichi) v = gneg(v);
    4089             :       }
    4090       11676 :       s = gadd(s, v);
    4091             :     }
    4092        3080 :     s = gadd(s, zeropadic(gp,nfinal));
    4093        3080 :     gel(res,i) = gerepileupto(av2, s);
    4094             :   }
    4095        2366 :   return gerepileupto(av, gmul(alpha, res));
    4096             : }
    4097             : /* integrate P = polynomial in log(x); vlog[j+1] = mspadicint(0,log(1+x)^j) */
    4098             : static GEN
    4099         539 : mspadicint_RgXlog(GEN P, GEN vlog)
    4100             : {
    4101         539 :   long i, d = degpol(P);
    4102         539 :   GEN s = gmul(gel(P,2), gel(vlog,1));
    4103         539 :   for (i = 1; i <= d; i++) s = gadd(s, gmul(gel(P,i+2), gel(vlog,i+1)));
    4104         539 :   return s;
    4105             : };
    4106             : 
    4107             : /* oms from mspadicmoments */
    4108             : GEN
    4109          98 : mspadicseries(GEN oms, long teichi)
    4110             : {
    4111          98 :   pari_sp av = avma;
    4112             :   GEN S, L, X, vlog, s, s2, u, logu, bin;
    4113             :   long j, p, m, n, step, stop;
    4114          98 :   checkoms(oms);
    4115          98 :   n = oms_get_n0(oms);
    4116          98 :   if (n < 1)
    4117             :   {
    4118           0 :     s = zeroser(0,0);
    4119           0 :     if (oms_is_supersingular(oms)) s = mkvec2(s,s);
    4120           0 :     return gerepilecopy(av, s);
    4121             :   }
    4122          98 :   p = oms_get_p(oms);
    4123          98 :   vlog = cgetg(n+1, t_VEC);
    4124          98 :   step = p == 2? 2: 1;
    4125          98 :   stop = 0;
    4126          98 :   S = NULL;
    4127          98 :   L = log1x(n);
    4128         644 :   for (j = 0; j < n; j++)
    4129             :   {
    4130         616 :     if (j) stop += step + u_lval(j,p); /* = step*j + v_p(j!) */
    4131         616 :     if (stop >= n) break;
    4132             :     /* S = log(1+x)^j */
    4133         546 :     gel(vlog,j+1) = mspadicint(oms,teichi,S);
    4134         546 :     S = S? RgXn_mul(S, L, n+1): L;
    4135             :   }
    4136          98 :   m = j;
    4137          98 :   u = utoipos(p == 2? 5: 1+p);
    4138          98 :   logu = glog(cvtop(u, utoipos(p), 4*m), 0);
    4139          98 :   X = gdiv(pol_x(0), logu);
    4140          98 :   s = cgetg(m+1, t_VEC);
    4141          98 :   s2 = oms_is_supersingular(oms)? cgetg(m+1, t_VEC): NULL;
    4142          98 :   bin = pol_1(0);
    4143         539 :   for (j = 0; j < m; j++)
    4144             :   { /* bin = binomial(x/log(1+p+O(p^(4*n))), j) mod x^m */
    4145         539 :     GEN a, v = mspadicint_RgXlog(bin, vlog);
    4146         539 :     int done = 1;
    4147         539 :     gel(s,j+1) = a = gel(v,1);
    4148         539 :     if (!gequal0(a) || valp(a) > 0) done = 0; else setlg(s,j+1);
    4149         539 :     if (s2)
    4150             :     {
    4151         119 :       gel(s2,j+1) = a = gel(v,2);
    4152         119 :       if (!gequal0(a) || valp(a) > 0) done = 0; else setlg(s2,j+1);
    4153             :     }
    4154         539 :     if (done || j == m-1) break;
    4155         441 :     bin = RgXn_mul(bin, gdivgs(gsubgs(X, j), j+1), m);
    4156             :   }
    4157          98 :   s = RgV_to_ser(s,0,lg(s)+1);
    4158          98 :   if (s2) { s2 = RgV_to_ser(s2,0,lg(s2)+1); s = mkvec2(s, s2); }
    4159          98 :   if (kross(oms_get_D(oms), p) >= 0) return gerepilecopy(av, s);
    4160           7 :   return gerepileupto(av, gneg(s));
    4161             : }
    4162             : void
    4163        1911 : mspadic_parse_chi(GEN s, GEN *s1, GEN *s2)
    4164             : {
    4165        1911 :   if (!s) *s1 = *s2 = gen_0;
    4166        1778 :   else switch(typ(s))
    4167             :   {
    4168        1274 :     case t_INT: *s1 = *s2 = s; break;
    4169             :     case t_VEC:
    4170         504 :       if (lg(s) == 3)
    4171             :       {
    4172         504 :         *s1 = gel(s,1);
    4173         504 :         *s2 = gel(s,2);
    4174         504 :         if (typ(*s1) == t_INT && typ(*s2) == t_INT) break;
    4175             :       }
    4176           0 :     default: pari_err_TYPE("mspadicL",s);
    4177           0 :              *s1 = *s2 = NULL;
    4178             :   }
    4179        1911 : }
    4180             : /* oms from mspadicmoments
    4181             :  * r-th derivative of L(f,chi^s,psi) in direction <chi>
    4182             :    - s \in Z_p \times \Z/(p-1)\Z, s-> chi^s=<\chi>^s_1 omega^s_2)
    4183             :    - Z -> Z_p \times \Z/(p-1)\Z par s-> (s, s mod p-1).
    4184             :  */
    4185             : GEN
    4186        1820 : mspadicL(GEN oms, GEN s, long r)
    4187             : {
    4188        1820 :   pari_sp av = avma;
    4189             :   GEN s1, s2, z, S;
    4190             :   long p, n, teich;
    4191        1820 :   checkoms(oms);
    4192        1820 :   p = oms_get_p(oms);
    4193        1820 :   n = oms_get_n(oms);
    4194        1820 :   mspadic_parse_chi(s, &s1,&s2);
    4195        1820 :   teich = umodiu(subii(s2,s1), p==2? 2: p-1);
    4196        1820 :   S = xlog1x(n, itos(s1), r, &teich);
    4197        1820 :   z = mspadicint(oms, teich, S);
    4198        1820 :   if (lg(z) == 2) z = gel(z,1);
    4199        1820 :   if (kross(oms_get_D(oms), p) < 0) z = gneg(z);
    4200        1820 :   return gerepilecopy(av, z);
    4201             : }
    4202             : 
    4203             : /****************************************************************************/
    4204             : 
    4205             : struct siegel
    4206             : {
    4207             :   GEN V, Ast;
    4208             :   long N; /* level */
    4209             :   long oo; /* index of the [oo,0] path */
    4210             :   long k1, k2; /* two distinguished indices */
    4211             :   long n; /* #W, W = initial segment [in siegelstepC] already normalized */
    4212             : };
    4213             : 
    4214             : static void
    4215         378 : siegel_init(struct siegel *C, GEN M)
    4216             : {
    4217             :   GEN CPI, CP, MM, V, W, Ast;
    4218         378 :   GEN m = gel(M,11), M2 = gel(M,2), S = msN_get_section(M);
    4219         378 :   GEN E2fromE1 = msN_get_E2fromE1(M);
    4220         378 :   long m0 = lg(M2)-1;
    4221         378 :   GEN E2  = vecslice(M2, m[1]+1, m[2]);/* E2 */
    4222         378 :   GEN E1T = vecslice(M2, m[3]+1, m0); /* E1,T2,T31 */
    4223         378 :   GEN L = shallowconcat(E1T, E2);
    4224         378 :   long i, l = lg(L), n = lg(E1T)-1, lE = lg(E2);
    4225             : 
    4226         378 :   Ast = cgetg(l, t_VECSMALL);
    4227        6195 :   for (i = 1; i < lE; ++i)
    4228             :   {
    4229        5817 :     long j = E2fromE1_c(gel(E2fromE1,i));
    4230        5817 :     Ast[n+i] = j;
    4231        5817 :     Ast[j] = n+i;
    4232             :   }
    4233         378 :   for (; i<=n; ++i) Ast[i] = i;
    4234         378 :   MM = cgetg (l,t_VEC);
    4235             : 
    4236       12320 :   for (i = 1; i < l; i++)
    4237             :   {
    4238       11942 :     GEN c = gel(S, L[i]);
    4239       11942 :     long c12, c22, c21 = ucoeff(c,2,1);
    4240       11942 :     if (!c21) { gel(MM,i) = gen_0; continue; }
    4241       11564 :     c22 = ucoeff(c,2,2);
    4242       11564 :     if (!c22) { gel(MM,i) = gen_m1; continue; }
    4243       11186 :     c12 = ucoeff(c,1,2);
    4244       11186 :     gel(MM,i) = sstoQ(c12, c22); /* right extremity > 0 */
    4245             :   }
    4246         378 :   CP = indexsort(MM);
    4247         378 :   CPI = cgetg(l, t_VECSMALL);
    4248         378 :   V = cgetg(l, t_VEC);
    4249         378 :   W = cgetg(l, t_VECSMALL);
    4250       12320 :   for (i = 1; i < l; ++i)
    4251             :   {
    4252       11942 :     gel(V,i) = mat2_to_ZM(gel(S, L[CP[i]]));
    4253       11942 :     CPI[CP[i]] = i;
    4254             :   }
    4255         378 :   for (i = 1; i < l; ++i) W[CPI[i]] = CPI[Ast[i]];
    4256         378 :   C->V = V;
    4257         378 :   C->Ast = W;
    4258         378 :   C->n = 0;
    4259         378 :   C->oo = 2;
    4260         378 :   C->N = ms_get_N(M);
    4261         378 : }
    4262             : 
    4263             : static double
    4264           0 : ZMV_size(GEN v)
    4265             : {
    4266           0 :   long i, l = lg(v);
    4267           0 :   GEN z = cgetg(l, t_VECSMALL);
    4268           0 :   for (i = 1; i < l; i++) z[i] = gexpo(gel(v,i));
    4269           0 :   return ((double)zv_sum(z)) / (4*(l-1));
    4270             : }
    4271             : 
    4272             : /* apply permutation perm to struct S. Don't follow k1,k2 */
    4273             : static void
    4274        5558 : siegel_perm0(struct siegel *S, GEN perm)
    4275             : {
    4276        5558 :   pari_sp av = avma;
    4277        5558 :   long i, l = lg(S->V);
    4278        5558 :   GEN V2 = cgetg(l, t_VEC), Ast2 = cgetg(l, t_VECSMALL);
    4279        5558 :   GEN V = S->V, Ast = S->Ast;
    4280             : 
    4281        5558 :   for (i = 1; i < l; i++) gel(V2,perm[i]) = gel(V,i);
    4282        5558 :   for (i = 1; i < l; i++) Ast2[perm[i]] = perm[Ast[i]];
    4283        5558 :   for (i = 1; i < l; i++) { S->Ast[i] = Ast2[i]; gel(V,i) = gel(V2,i); }
    4284        5558 :   set_avma(av); S->oo = perm[S->oo];
    4285        5558 : }
    4286             : /* apply permutation perm to full struct S */
    4287             : static void
    4288        5194 : siegel_perm(struct siegel *S, GEN perm)
    4289             : {
    4290        5194 :   siegel_perm0(S, perm);
    4291        5194 :   S->k1 = perm[S->k1];
    4292        5194 :   S->k2 = perm[S->k2];
    4293        5194 : }
    4294             : /* cyclic permutation of lg = l-1 moving a -> 1, a+1 -> 2, etc.  */
    4295             : static GEN
    4296        2884 : rotate_perm(long l, long a)
    4297             : {
    4298        2884 :   GEN p = cgetg(l, t_VECSMALL);
    4299        2884 :   long i, j = 1;
    4300        2884 :   for (i = a; i < l; i++) p[i] = j++;
    4301        2884 :   for (i = 1; i < a; i++) p[i] = j++;
    4302        2884 :   return p;
    4303             : }
    4304             : 
    4305             : /* a1 < c1 <= a2 < c2*/
    4306             : static GEN
    4307        2520 : basic_op_perm(long l, long a1, long a2, long c1, long c2)
    4308             : {
    4309        2520 :   GEN p = cgetg(l, t_VECSMALL);
    4310        2520 :   long i, j = 1;
    4311        2520 :   p[a1] = j++;
    4312        2520 :   for (i = c1; i < a2; i++)   p[i] = j++;
    4313        2520 :   for (i = a1+1; i < c1; i++) p[i] = j++;
    4314        2520 :   p[a2] = j++;
    4315        2520 :   for (i = c2; i < l; i++)    p[i] = j++;
    4316        2520 :   for (i = 1; i < a1; i++)    p[i] = j++;
    4317        2520 :   for (i = a2+1; i < c2; i++) p[i] = j++;
    4318        2520 :   return p;
    4319             : }
    4320             : static GEN
    4321         154 : basic_op_perm_elliptic(long l, long a1)
    4322             : {
    4323         154 :   GEN p = cgetg(l, t_VECSMALL);
    4324         154 :   long i, j = 1;
    4325         154 :   p[a1] = j++;
    4326         154 :   for (i = 1; i < a1; i++)   p[i] = j++;
    4327         154 :   for (i = a1+1; i < l; i++) p[i] = j++;
    4328         154 :   return p;
    4329             : }
    4330             : static GEN
    4331       14616 : ZM2_rev(GEN T) { return mkmat2(gel(T,2), ZC_neg(gel(T,1))); }
    4332             : 
    4333             : /* In place, V = vector of consecutive paths, between x <= y.
    4334             :  * V[x..y-1] <- g*V[x..y-1] */
    4335             : static void
    4336        5733 : path_vec_mul(GEN V, long x, long y, GEN g)
    4337             : {
    4338             :   long j;
    4339             :   GEN M;
    4340        5733 :   if (x == y) return;
    4341        3360 :   M = gel(V,x); gel(V,x) = ZM_mul(g,M);
    4342       37709 :   for (j = x+1; j < y; j++) /* V[j] <- g*V[j], optimized */
    4343             :   {
    4344       34349 :     GEN Mnext = gel(V,j); /* Mnext[,1] = M[,2] */
    4345       34349 :     GEN gM = gel(V,j-1), u = gel(gM,2);
    4346       34349 :     if (!ZV_equal(gel(M,2), gel(Mnext,1))) u = ZC_neg(u);
    4347       34349 :     gel(V,j) = mkmat2(u, ZM_ZC_mul(g,gel(Mnext,2)));
    4348       34349 :     M = Mnext;
    4349             :   }
    4350             : }
    4351             : 
    4352        4830 : static long prev(GEN V, long i) { return (i == 1)? lg(V)-1: i-1; }
    4353        4830 : static long next(GEN V, long i) { return (i == lg(V)-1)? 1: i+1; }
    4354             : static GEN
    4355       19810 : ZM_det2(GEN u, GEN v)
    4356             : {
    4357       19810 :   GEN a = gel(u,1), c = gel(u,2);
    4358       19810 :   GEN b = gel(v,1), d = gel(v,2); return subii(mulii(a,d), mulii(b,c));
    4359             : }
    4360             : static GEN
    4361       14616 : ZM2_det(GEN T) { return ZM_det2(gel(T,1),gel(T,2)); }
    4362             : static long
    4363        5194 : ZM_det2_sign(GEN u, GEN v)
    4364             : {
    4365        5194 :   pari_sp av = avma;
    4366        5194 :   long s = signe(ZM_det2(u, v));
    4367        5194 :   return gc_long(av, s);
    4368             : }
    4369             : static void
    4370        4466 : fill1(GEN V, long a)
    4371             : {
    4372        4466 :   long p = prev(V,a), n = next(V,a);
    4373        4466 :   GEN u = gmael(V,p,2), v = gmael(V,n,1);
    4374        4466 :   if (ZM_det2_sign(u,v) < 0) v = ZC_neg(v);
    4375        4466 :   gel(V,a) = mkmat2(u, v);
    4376        4466 : }
    4377             : /* a1 < a2 */
    4378             : static void
    4379        2520 : fill2(GEN V, long a1, long a2)
    4380             : {
    4381        2520 :   if (a2 != a1+1) { fill1(V,a1); fill1(V,a2); } /* non adjacent, reconnect */
    4382             :   else
    4383             :   { /* parabolic */
    4384         364 :     long p = prev(V,a1), n = next(V,a2);
    4385         364 :     GEN u, v, C = gmael(V,a1,2), mC = NULL; /* = \pm V[a2][1] */
    4386         364 :     u = gmael(V,p,2); v = C;
    4387         364 :     if (ZM_det2_sign(u,v) < 0) v = mC = ZC_neg(C);
    4388         364 :     gel(V,a1) = mkmat2(u,v);
    4389         364 :     v = gmael(V,n,1); u = C;
    4390         364 :     if (ZM_det2_sign(u,v) < 0) u = mC? mC: ZC_neg(C);
    4391         364 :     gel(V,a2) = mkmat2(u,v);
    4392             :   }
    4393        2520 : }
    4394             : 
    4395             : /* DU = det(U), return g = T*U^(-1) or NULL if not in Gamma0(N); if N = 0,
    4396             :  * only test whether g is integral */
    4397             : static GEN
    4398       14903 : ZM2_div(GEN T, GEN U, GEN DU, long N)
    4399             : {
    4400       14903 :   GEN a=gcoeff(U,1,1), b=gcoeff(U,1,2), c=gcoeff(U,2,1), d=gcoeff(U,2,2);
    4401       14903 :   GEN e=gcoeff(T,1,1), f=gcoeff(T,1,2), g=gcoeff(T,2,1), h=gcoeff(T,2,2);
    4402             :   GEN A, B, C, D, r;
    4403             : 
    4404       14903 :   C = dvmdii(subii(mulii(d,g), mulii(c,h)), DU, &r);
    4405       14903 :   if (r != gen_0 || (N && smodis(C,N))) return NULL;
    4406       14616 :   A = dvmdii(subii(mulii(d,e), mulii(c,f)), DU, &r);
    4407       14616 :   if (r != gen_0) return NULL;
    4408       14616 :   B = dvmdii(subii(mulii(a,f), mulii(b,e)), DU, &r);
    4409       14616 :   if (r != gen_0) return NULL;
    4410       14616 :   D = dvmdii(subii(mulii(a,h), mulii(g,b)), DU, &r);
    4411       14616 :   if (r != gen_0) return NULL;
    4412       14616 :   return mkmat22(A,B,C,D);
    4413             : }
    4414             : 
    4415             : static GEN
    4416       14616 : get_g(struct siegel *S, long a1)
    4417             : {
    4418       14616 :   pari_sp av = avma;
    4419       14616 :   long a2 = S->Ast[a1];
    4420       14616 :   GEN a = gel(S->V,a1), ar = ZM2_rev(gel(S->V,a2)), Dar = ZM2_det(ar);
    4421       14616 :   GEN g = ZM2_div(a, ar, Dar, S->N);
    4422       14616 :   if (!g)
    4423             :   {
    4424         287 :     GEN tau = mkmat22(gen_0,gen_m1, gen_1,gen_m1); /*[0,-1;1,-1]*/
    4425         287 :     g = ZM2_div(ZM_mul(ar, tau), ar, Dar, 0);
    4426             :   }
    4427       14616 :   return gerepilecopy(av, g);
    4428             : }
    4429             : /* input V = (X1 a X2 | X3 a^* X4) + Ast
    4430             :  * a1 = index of a
    4431             :  * a2 = index of a^*, inferred from a1. We must have a != a^*
    4432             :  * c1 = first cut [ index of first path in X3 ]
    4433             :  * c2 = second cut [ either in X4 or X1, index of first path ]
    4434             :  * Assume a < a^* (cf Paranoia below): c1 or c2 must be in
    4435             :  *    ]a,a^*], and the other in the "complement" ]a^*,a] */
    4436             : static void
    4437        2520 : basic_op(struct siegel *S, long a1, long c1, long c2)
    4438             : {
    4439             :   pari_sp av;
    4440        2520 :   long l = lg(S->V), a2 = S->Ast[a1];
    4441             :   GEN g;
    4442             : 
    4443        2520 :   if (a1 == a2)
    4444             :   { /* a = a^* */
    4445           0 :     g = get_g(S, a1);
    4446           0 :     path_vec_mul(S->V, a1+1, l, g);
    4447           0 :     av = avma;
    4448           0 :     siegel_perm(S, basic_op_perm_elliptic(l, a1));
    4449             :     /* fill the hole left at a1, reconnect the path */
    4450           0 :     set_avma(av); fill1(S->V, a1); return;
    4451             :   }
    4452             : 
    4453             :   /* Paranoia: (a,a^*) conjugate, call 'a' the first one */
    4454        2520 :   if (a2 < a1) lswap(a1,a2);
    4455             :   /* Now a1 < a2 */
    4456        2520 :   if (c1 <= a1 || c1 > a2) lswap(c1,c2); /* ensure a1 < c1 <= a2 */
    4457        2520 :   if (c2 < a1)
    4458             :   { /* if cut c2 is in X1 = X11|X12, rotate to obtain
    4459             :        (a X2 | X3 a^* X4 X11|X12): then a1 = 1 */
    4460             :     GEN p;
    4461        2520 :     av = avma; p = rotate_perm(l, a1);
    4462        2520 :     siegel_perm(S, p);
    4463        2520 :     a1 = 1; /* = p[a1] */
    4464        2520 :     a2 = S->Ast[1]; /* > a1 */
    4465        2520 :     c1 = p[c1];
    4466        2520 :     c2 = p[c2]; set_avma(av);
    4467             :   }
    4468             :   /* Now a1 < c1 <= a2 < c2; a != a^* */
    4469        2520 :   g = get_g(S, a1);
    4470        2520 :   if (S->oo >= c1 && S->oo < c2) /* W inside [c1..c2[ */
    4471         539 :   { /* c2 -> c1 excluding a1 */
    4472         539 :     GEN gi = SL2_inv(g); /* g a^* = a; gi a = a^* */
    4473         539 :     path_vec_mul(S->V, 1, a1, gi);
    4474         539 :     path_vec_mul(S->V, a1+1, c1, gi);
    4475         539 :     path_vec_mul(S->V, c2, l, gi);
    4476             :   }
    4477             :   else
    4478             :   { /* c1 -> c2 excluding a2 */
    4479        1981 :     path_vec_mul(S->V, c1, a2, g);
    4480        1981 :     path_vec_mul(S->V, a2+1, c2, g);
    4481             :   }
    4482        2520 :   av = avma;
    4483        2520 :   siegel_perm(S, basic_op_perm(l, a1,a2, c1,c2));
    4484        2520 :   set_avma(av);
    4485             :   /* fill the holes left at a1,a2, reconnect the path */
    4486        2520 :   fill2(S->V, a1, a2);
    4487             : }
    4488             : /* a = a^* (elliptic case) */
    4489             : static void
    4490         154 : basic_op_elliptic(struct siegel *S, long a1)
    4491             : {
    4492             :   pari_sp av;
    4493         154 :   long l = lg(S->V);
    4494         154 :   GEN g = get_g(S, a1);
    4495         154 :   path_vec_mul(S->V, a1+1, l, g);
    4496         154 :   av = avma; siegel_perm(S, basic_op_perm_elliptic(l, a1));
    4497             :   /* fill the hole left at a1 (now at 1), reconnect the path */
    4498         154 :   set_avma(av); fill1(S->V, 1);
    4499         154 : }
    4500             : 
    4501             : /* input V = W X a b Y a^* Z b^* T, W already normalized
    4502             :  * X = [n+1, k1-1], Y = [k2+1, Ast[k1]-1],
    4503             :  * Z = [Ast[k1]+1, Ast[k2]-1], T = [Ast[k2]+1, oo].
    4504             :  * Assume that X doesn't start by c c^* or a b a^* b^*. */
    4505             : static void
    4506        1057 : siegelstep(struct siegel *S)
    4507             : {
    4508        1057 :   if (S->Ast[S->k1] == S->k1)
    4509             :   {
    4510         154 :     basic_op_elliptic(S, S->k1);
    4511         154 :     S->n++;
    4512             :   }
    4513         903 :   else if (S->Ast[S->k1] == S->k1+1)
    4514             :   {
    4515         364 :     basic_op(S, S->k1, S->Ast[S->k1], 1); /* 1: W starts there */
    4516         364 :     S->n += 2;
    4517             :   }
    4518             :   else
    4519             :   {
    4520         539 :     basic_op(S, S->k2, S->Ast[S->k1], 1); /* 1: W starts there */
    4521         539 :     basic_op(S, S->k1, S->k2, S->Ast[S->k2]);
    4522         539 :     basic_op(S, S->Ast[S->k2], S->k2, S->Ast[S->k1]);
    4523         539 :     basic_op(S, S->k1, S->Ast[S->k1], S->Ast[S->k2]);
    4524         539 :     S->n += 4;
    4525             :   }
    4526        1057 : }
    4527             : 
    4528             : /* normalize hyperbolic polygon */
    4529             : static void
    4530         301 : mssiegel(struct siegel *S)
    4531             : {
    4532         301 :   pari_sp av = avma;
    4533             :   long k, t, nv;
    4534             : #ifdef COUNT
    4535             :   long countset[16];
    4536             :   for (k = 0; k < 16; k++) countset[k] = 0;
    4537             : #endif
    4538             : 
    4539         301 :   nv = lg(S->V)-1;
    4540         301 :   if (DEBUGLEVEL>1) err_printf("nv = %ld, expo = %.2f\n", nv,ZMV_size(S->V));
    4541         301 :   t = 0;
    4542        2506 :   while (S->n < nv)
    4543             :   {
    4544        1904 :     if (S->Ast[S->n+1] == S->n+1) { S->n++; continue; }
    4545        1778 :     if (S->Ast[S->n+1] == S->n+2) { S->n += 2; continue; }
    4546        1134 :     if (S->Ast[S->n+1] == S->n+3 && S->Ast[S->n+2] == S->n+4) { S->n += 4; continue; }
    4547        1057 :     k = nv;
    4548        2184 :     while (k > S->n)
    4549             :     {
    4550        1127 :       if (S->Ast[k] == k) { k--; continue; }
    4551        1099 :       if (S->Ast[k] == k-1) { k -= 2; continue; }
    4552        1057 :       if (S->Ast[k] == k-2 && S->Ast[k-1] == k-3) { k -= 4; continue; }
    4553        1057 :       break;
    4554             :     }
    4555        1057 :     if (k != nv)
    4556             :     {
    4557          63 :       pari_sp av2 = avma;
    4558          63 :       siegel_perm0(S, rotate_perm(nv+1, k+1));
    4559          63 :       set_avma(av2); S->n += nv-k;
    4560             :     }
    4561             : 
    4562        6223 :     for (k = S->n+1; k <= nv; k++)
    4563        6223 :       if (S->Ast[k] <= k) { t = S->Ast[k]; break; }
    4564        1057 :     S->k1 = t;
    4565        1057 :     S->k2 = t+1;
    4566             : #ifdef COUNT
    4567             :     countset[ ((S->k1-1 == S->n)
    4568             :               | ((S->k2 == S->Ast[S->k1]-1) << 1)
    4569             :               | ((S->Ast[S->k1] == S->Ast[S->k2]-1) << 2)
    4570             :               | ((S->Ast[S->k2] == nv) << 3)) ]++;
    4571             : #endif
    4572        1057 :     siegelstep(S);
    4573        1057 :     if (gc_needed(av,2))
    4574             :     {
    4575           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"mspolygon, n = %ld",S->n);
    4576           0 :       gerepileall(av, 2, &S->V, &S->Ast);
    4577             :     }
    4578             :   }
    4579         301 :   if (DEBUGLEVEL>1) err_printf("expo = %.2f\n", ZMV_size(S->V));
    4580             : #ifdef COUNT
    4581             :   for (k = 0; k < 16; k++)
    4582             :     err_printf("%3ld: %6ld\n", k, countset[k]);
    4583             : #endif
    4584         301 : }
    4585             : 
    4586             : /* return a vector of char */
    4587             : static GEN
    4588           0 : Ast2v(GEN Ast)
    4589             : {
    4590           0 :   long j = 0, k, l = lg(Ast);
    4591           0 :   GEN v = const_vec(l-1, NULL);
    4592           0 :   for (k=1; k < l; k++)
    4593             :   {
    4594             :     char *sj;
    4595           0 :     if (gel(v,k)) continue;
    4596           0 :     j++;
    4597           0 :     sj = stack_sprintf("$%ld$", j);
    4598           0 :     gel(v,k) = (GEN)sj;
    4599           0 :     if (Ast[k] != k) gel(v,Ast[k]) = (GEN)stack_sprintf("$%ld^*$", j);
    4600             :   }
    4601           0 :   return v;
    4602             : };
    4603             : static GEN
    4604           0 : M2Q(GEN p) { GEN c = gel(p,1); return gdiv(gel(c,1), gel(c,2)); }
    4605             : 
    4606             : static void
    4607           0 : decorate(pari_str *s, GEN g, GEN T1, GEN T2)
    4608             : {
    4609           0 :   double a = gtodouble(gcoeff(g,1,1)), t1 = gtodouble(T1), t2 = gtodouble(T2);
    4610           0 :   double c = gtodouble(gcoeff(g,2,1)), d = gtodouble(gcoeff(g,2,2));
    4611           0 :   if (a + d)
    4612             :   {
    4613           0 :     double x, y, u1, u2, C = 360 / (2*M_PI);
    4614           0 :     x = (a - d) / (2 * c);
    4615           0 :     y = 0.8660254 / fabs(c); /* sqrt(3)/ (2 |c|) > 0 */
    4616           0 :     u1 = (x*x + y*y - t1*t1)/(x-t1)/2;
    4617           0 :     u2 = (x*x + y*y - t2*t2)/(x-t2)/2;
    4618           0 :     str_printf(s, ";\n \\draw (start) arc (180:%.4f:%.4f) node {$\\bullet$} arc (%.4f:0:%.4f)", C*atan2(y,x-u1), fabs(t1-u1), C*atan2(y,x-u2), fabs(t2-u2));
    4619             :   }
    4620             :   else
    4621           0 :     str_printf(s, " node (start) {} (%.4f,%.4f) node {$\\circ$};\n \\draw (start) node{}",a/c,fabs(1/c));
    4622           0 : }
    4623             : 
    4624             : static GEN
    4625           0 : polygon2tex(GEN V, GEN Ast, GEN G)
    4626             : {
    4627           0 :   pari_sp av = avma;
    4628           0 :   GEN v = Ast2v(Ast), a, b;
    4629             :   pari_str s;
    4630           0 :   long j, k, l = lg(V), flag = (l <= 16);
    4631             :   double c;
    4632           0 :   str_init(&s, 1);
    4633           0 :   str_puts(&s, "\n\\begin{tikzpicture}[scale=10]\n");
    4634           0 :   str_puts(&s, "\\draw (0,0.5)--(0,0) node [very near start, right] {$1^*$} node [below] {$0$}");
    4635           0 :   k = minss(3, l-1); a = M2Q(gel(V,k));
    4636           0 :   for (j = 4; j < l; k = j, a = b, j++)
    4637             :   {
    4638           0 :     b = M2Q(gel(V,j)); /* next vertex */
    4639           0 :     c = gtodouble(gsub(b,a)) / 2;
    4640             : 
    4641           0 :     str_printf(&s, "node (start) {} arc (180:0:%.4f)\n", c);
    4642           0 :     if (flag)
    4643             :     {
    4644           0 :       long sb = itos(numer_i(b)), sa = itos(denom_i(b));
    4645           0 :       str_printf(&s, "node [midway, above] {%s} node [below]{$\\frac{%ld}{%ld}$}\n",
    4646           0 :                  (char*)gel(v,k), sb, sa);
    4647             :     }
    4648           0 :     if (Ast[k] == k) decorate(&s, gel(G,k), a, b);
    4649             :   }
    4650           0 :   c = (1 - gtodouble(a)) / 2;
    4651           0 :   str_printf(&s, "node (start) {} arc (180:0:%.4f)\n", c);
    4652           0 :   if (flag) str_printf(&s, "node [midway, above] {%s}", (char*)gel(v,l-1));
    4653           0 :   if (Ast[k] == k) decorate(&s, gel(G,k), a, gen_1);
    4654           0 :   str_printf(&s,"node [below] {$1$} -- (1,0.5) node [very near end, left] {$1$};");
    4655           0 :   str_printf(&s, "\n\\end{tikzpicture}");
    4656           0 :   return gerepileuptoleaf(av, strtoGENstr(s.string));
    4657             : }
    4658             : 
    4659             : static GEN
    4660           0 : circle2tex(GEN Ast, GEN G)
    4661             : {
    4662           0 :   pari_sp av = avma;
    4663           0 :   GEN v = Ast2v(Ast);
    4664             :   pari_str s;
    4665           0 :   long u, n = lg(Ast)-1;
    4666           0 :   const double ang = 360./n;
    4667             : 
    4668           0 :   if (n > 30)
    4669             :   {
    4670           0 :     v = const_vec(n, (GEN)"");
    4671           0 :     gel(v,1) = (GEN)"$(1,\\infty)$";
    4672             :   }
    4673           0 :   str_init(&s, 1);
    4674           0 :   str_puts(&s, "\n\\begingroup\n\
    4675             :   \\def\\geo#1#2{(#2:1) arc (90+#2:270+#1:{tan((#2-#1)/2)})}\n\
    4676             :   \\def\\sgeo#1#2{(#2:1) -- (#1:1)}\n\
    4677             :   \\def\\unarc#1#2#3{({#1 * #3}:1.2) node {#2}}\n\
    4678             :   \\def\\cell#1#2{({#1 * #2}:0.95) circle(0.05)}\n\
    4679             :   \\def\\link#1#2#3#4#5{\\unarc{#1}{#2}{#5}\\geo{#1*#5}{#3*#5}\\unarc{#3}{#4}{#5}}\n\
    4680             :   \\def\\slink#1#2#3#4#5{\\unarc{#1}{#2}{#5}\\sgeo{#1*#5}{#3*#5}\\unarc{#3}{#4}{#5}}");
    4681             : 
    4682           0 :   str_puts(&s, "\n\\begin{tikzpicture}[scale=4]\n");
    4683           0 :   str_puts(&s, "\\draw (0, 0) circle(1);\n");
    4684           0 :   for (u=1; u <= n; u++)
    4685             :   {
    4686           0 :     if (Ast[u] == u)
    4687             :     {
    4688           0 :       str_printf(&s,"\\draw\\unarc{%ld}{%s}{%.4f}; \\draw\\cell{%ld}{%.4f};\n",
    4689           0 :                  u, v[u], ang, u, ang);
    4690           0 :       if (ZM_isscalar(gpowgs(gel(G,u),3), NULL))
    4691           0 :         str_printf(&s,"\\fill \\cell{%ld}{%.4f};\n", u, ang);
    4692             :     }
    4693           0 :     else if(Ast[u] > u)
    4694           0 :       str_printf(&s, "\\draw \\%slink {%ld}{%s}{%ld}{%s}{%.4f};\n",
    4695           0 :                      Ast[u]-u==n/2? "s": "", u, v[u], Ast[u], v[Ast[u]], ang);
    4696             :   }
    4697           0 :   str_printf(&s, "\\end{tikzpicture}\\endgroup");
    4698           0 :   return gerepileuptoleaf(av, strtoGENstr(s.string));
    4699             : }
    4700             : 
    4701             : GEN
    4702         399 : mspolygon(GEN M, long flag)
    4703             : {
    4704         399 :   pari_sp av = avma;
    4705             :   struct siegel T;
    4706             :   long i, l;
    4707             :   GEN v, G, msN;
    4708         399 :   if (typ(M) == t_INT)
    4709             :   {
    4710         301 :     long N = itos(M);
    4711         301 :     if (N <= 0) pari_err_DOMAIN("msinit","N", "<=", gen_0,M);
    4712         301 :     msN = msinit_N(N);
    4713             :   }
    4714          98 :   else { checkms(M); msN = get_msN(M); }
    4715         399 :   if (flag < 0 || flag > 3) pari_err_FLAG("mspolygon");
    4716         399 :   if (ms_get_N(msN) == 1)
    4717             :   {
    4718          21 :     GEN S = mkS();
    4719          21 :     T.V = mkvec2(matid(2), S);
    4720          21 :     T.Ast = mkvecsmall2(1,2);
    4721          21 :     G = mkvec2(S, mkTAU());
    4722             :   }
    4723             :   else
    4724             :   {
    4725         378 :     siegel_init(&T, msN);
    4726         378 :     l = lg(T.V);
    4727         378 :     if (flag & 1)
    4728             :     {
    4729         301 :       long oo2 = 0;
    4730             :       pari_sp av;
    4731         301 :       mssiegel(&T);
    4732        3451 :       for (i = 1; i < l; i++)
    4733             :       {
    4734        3451 :         GEN c = gel(T.V, i);
    4735        3451 :         GEN c22 = gcoeff(c,2,2); if (!signe(c22)) { oo2 = i; break; }
    4736             :       }
    4737         301 :       if (!oo2) pari_err_BUG("mspolygon");
    4738         301 :       av = avma; siegel_perm0(&T, rotate_perm(l, oo2));
    4739         301 :       set_avma(av);
    4740             :     }
    4741         378 :     G = cgetg(l, t_VEC);
    4742         378 :     for (i = 1; i < l; i++) gel(G,i) = get_g(&T, i);
    4743             :   }
    4744         399 :   if (flag & 2)
    4745           0 :     v = mkvec5(T.V, T.Ast, G, polygon2tex(T.V,T.Ast,G), circle2tex(T.Ast,G));
    4746             :   else
    4747         399 :     v = mkvec3(T.V, T.Ast, G);
    4748         399 :   return gerepilecopy(av, v);
    4749             : }
    4750             : 
    4751             : #if 0
    4752             : static int
    4753             : iselliptic(GEN Ast, long i) { return i == Ast[i]; }
    4754             : static int
    4755             : isparabolic(GEN Ast, long i)
    4756             : { long i2 = Ast[i]; return (i2 == i+1 || i2 == i-1); }
    4757             : #endif
    4758             : 
    4759             : /* M from msinit, F QM maximal rank */
    4760             : GEN
    4761          77 : mslattice(GEN M, GEN F)
    4762             : {
    4763          77 :   pari_sp av = avma;
    4764             :   long i, ivB, j, k, l, lF;
    4765             :   GEN D, U, G, A, vB, m, d;
    4766             : 
    4767          77 :   checkms(M);
    4768          77 :   if (!F) F = gel(mscuspidal(M, 0), 1);
    4769             :   else
    4770             :   {
    4771          49 :     if (is_Qevproj(F)) F = gel(F,1);
    4772          49 :     if (typ(F) != t_MAT) pari_err_TYPE("mslattice",F);
    4773             :   }
    4774          77 :   lF = lg(F); if (lF == 1) return cgetg(1, t_MAT);
    4775          77 :   D = mspolygon(M,0);
    4776          77 :   k = msk_get_weight(M);
    4777          77 :   F = vec_Q_primpart(F);
    4778          77 :   if (typ(F)!=t_MAT || !RgM_is_ZM(F)) pari_err_TYPE("mslattice",F);
    4779          77 :   G = gel(D,3); l = lg(G);
    4780          77 :   A = gel(D,2);
    4781          77 :   vB = cgetg(l, t_COL);
    4782          77 :   d = mkcol2(gen_0,gen_1);
    4783          77 :   m = mkmat2(d, d);
    4784        7091 :   for (i = ivB = 1; i < l; i++)
    4785             :   {
    4786        7014 :     GEN B, vb, g = gel(G,i);
    4787        7014 :     if (A[i] < i) continue;
    4788        3514 :     gel(m,2) = SL2_inv2(g);
    4789        3514 :     vb = mseval(M, F, m);
    4790        3514 :     if (k == 2) B = vb;
    4791             :     else
    4792             :     {
    4793             :       long lB;
    4794         147 :       B = RgXV_to_RgM(vb, k-1);
    4795             :       /* add coboundaries */
    4796         147 :       B = shallowconcat(B, RgM_Rg_sub(RgX_act_Gl2Q(g, k), gen_1));
    4797             :       /* beware: the basis for RgX_act_Gl2Q is (X^(k-2),...,Y^(k-2)) */
    4798         147 :       lB = lg(B);
    4799         147 :       for (j = 1; j < lB; j++) gel(B,j) = vecreverse(gel(B,j));
    4800             :     }
    4801        3514 :     gel(vB, ivB++) = B;
    4802             :   }
    4803          77 :   setlg(vB, ivB);
    4804          77 :   vB = shallowmatconcat(vB);
    4805          77 :   if (ZM_equal0(vB)) return gerepilecopy(av, F);
    4806             : 
    4807          77 :   (void)QM_ImQ_hnfall(vB, &U, 0);
    4808          77 :   if (k > 2) U = rowslice(U, 1, lgcols(U)-k); /* remove coboundary part */
    4809          77 :   U = Q_remove_denom(U, &d);
    4810          77 :   F = ZM_hnf(ZM_mul(F, U));
    4811          77 :   if (d) F = RgM_Rg_div(F, d);
    4812          77 :   return gerepileupto(av, F);
    4813             : }
    4814             : 
    4815             : /**** Petersson scalar product ****/
    4816             : 
    4817             : /* oo -> g^(-1) oo */
    4818             : static GEN
    4819        6181 : cocycle(GEN g)
    4820        6181 : { return mkmat22(gen_1, gcoeff(g,2,2), gen_0, negi(gcoeff(g,2,1))); }
    4821             : 
    4822             : /* C = vecbinome(k-2) */
    4823             : static GEN
    4824       17983 : bil(GEN P, GEN Q, GEN C)
    4825             : {
    4826       17983 :   long i, n = lg(C)-2; /* k - 2 */
    4827             :   GEN s;
    4828       17983 :   if (!n) return gmul(P,Q);
    4829       17962 :   if (typ(P) != t_POL) P = scalarpol_shallow(P,0);
    4830       17962 :   if (typ(Q) != t_POL) Q = scalarpol_shallow(Q,0);
    4831       17962 :   s = gen_0;
    4832       71960 :   for (i = 0; i <= n; i++)
    4833             :   {
    4834       53998 :     GEN t = gdiv(gmul(RgX_coeff(P,i), RgX_coeff(Q, n-i)), gel(C,i+1));
    4835       53998 :     s = odd(i)? gsub(s, t): gadd(s, t);
    4836             :   }
    4837       17962 :   return s;
    4838             : }
    4839             : 
    4840             : static void
    4841        1351 : mspetersson_i(GEN W, GEN F, GEN G, GEN *pvf, GEN *pvg, GEN *pC)
    4842             : {
    4843        1351 :   GEN WN = get_msN(W), annT2, annT31, section, c, vf, vg;
    4844             :   long i, n1, n2, n3;
    4845             : 
    4846        1351 :   annT2 = msN_get_annT2(WN);
    4847        1351 :   annT31 = msN_get_annT31(WN);
    4848        1351 :   section = msN_get_section(WN);
    4849             : 
    4850        1351 :   if (ms_get_N(WN) == 1)
    4851             :   {
    4852           7 :     vf = cgetg(3, t_VEC);
    4853           7 :     vg = cgetg(3, t_VEC);
    4854           7 :     gel(vf,1) = mseval(W, F, gel(section,1));
    4855           7 :     gel(vf,2) = gneg(gel(vf,1));
    4856           7 :     n1 = 0;
    4857             :   }
    4858             :   else
    4859             :   {
    4860        1344 :     GEN singlerel = msN_get_singlerel(WN);
    4861        1344 :     GEN gen = msN_get_genindex(WN);
    4862        1344 :     long l = lg(gen);
    4863        1344 :     vf = cgetg(l, t_VEC);
    4864        1344 :     vg = cgetg(l, t_VEC); /* generators of Delta ordered as E1,T2,T31 */
    4865        1344 :     for (i = 1; i < l; i++) gel(vf, i) = mseval(W, F, gel(section,gen[i]));
    4866        1344 :     n1 = ms_get_nbE1(WN); /* E1 */
    4867        7420 :     for (i = 1; i <= n1; i++)
    4868             :     {
    4869        6076 :       c = cocycle(gcoeff(gel(singlerel,i),2,1));
    4870        6076 :       gel(vg, i) = mseval(W, G, c);
    4871             :     }
    4872             :   }
    4873        1351 :   n2 = lg(annT2)-1; /* T2 */
    4874        1386 :   for (i = 1; i <= n2; i++)
    4875             :   {
    4876          35 :     c = cocycle(gcoeff(gel(annT2,i), 2,1));
    4877          35 :     gel(vg, i+n1) = gmul2n(mseval(W, G, c), -1);
    4878             :   }
    4879        1351 :   n3 = lg(annT31)-1; /* T31 */
    4880        1386 :   for (i = 1; i <= n3; i++)
    4881             :   {
    4882             :     GEN f;
    4883          35 :     c = cocycle(gcoeff(gel(annT31,i), 2,1));
    4884          35 :     f = mseval(W, G, c);
    4885          35 :     c = cocycle(gcoeff(gel(annT31,i), 3,1));
    4886          35 :     gel(vg, i+n1+n2) = gdivgs(gadd(f, mseval(W, G, c)), 3);
    4887             :   }
    4888        1351 :   *pC = vecbinome(msk_get_weight(W) - 2);
    4889        1351 :   *pvf = vf;
    4890        1351 :   *pvg = vg;
    4891        1351 : }
    4892             : 
    4893             : /* Petersson product on Hom_G(Delta_0, V_k) */
    4894             : GEN
    4895        1351 : mspetersson(GEN W, GEN F, GEN G)
    4896             : {
    4897        1351 :   pari_sp av = avma;
    4898             :   GEN vf, vg, C;
    4899             :   long k, l, tG, tF;
    4900        1351 :   checkms(W);
    4901        1351 :   if (!F) F = matid(msdim(W));
    4902        1351 :   if (!G) G = F;
    4903        1351 :   tF = typ(F);
    4904        1351 :   tG = typ(G);
    4905        1351 :   if (tF == t_MAT && tG != t_MAT) pari_err_TYPE("mspetersson",G);
    4906        1351 :   if (tG == t_MAT && tF != t_MAT) pari_err_TYPE("mspetersson",F);
    4907        1351 :   mspetersson_i(W, F, G, &vf, &vg, &C);
    4908        1351 :   l = lg(vf);
    4909        1351 :   if (tF != t_MAT)
    4910             :   { /* <F,G>, two symbols */
    4911        1274 :     GEN s = gen_0;
    4912        1274 :     for (k = 1; k < l; k++) s = gadd(s, bil(gel(vf,k), gel(vg,k), C));
    4913        1274 :     return gerepileupto(av, s);
    4914             :   }
    4915          77 :   else if (F != G)
    4916             :   { /* <(f_1,...,f_m), (g_1,...,g_n)> */
    4917           0 :     long iF, iG, lF = lg(F), lG = lg(G);
    4918           0 :     GEN M = cgetg(lG, t_MAT);
    4919           0 :     for (iG = 1; iG < lG; iG++)
    4920             :     {
    4921           0 :       GEN c = cgetg(lF, t_COL);
    4922           0 :       gel(M,iG) = c;
    4923           0 :       for (iF = 1; iF < lF; iF++)
    4924             :       {
    4925           0 :         GEN s = gen_0;
    4926           0 :         for (k = 1; k < l; k++)
    4927           0 :           s = gadd(s, bil(gmael(vf,k,iF), gmael(vg,k,iG), C));
    4928           0 :         gel(c,iF) = s; /* M[iF,iG] = <F[iF], G[iG] > */
    4929             :       }
    4930             :     }
    4931           0 :     return gerepilecopy(av, M);
    4932             :   }
    4933             :   else
    4934             :   { /* <(f_1,...,f_n), (f_1,...,f_n)> */
    4935          77 :     long iF, iG, n = lg(F)-1;
    4936          77 :     GEN M = zeromatcopy(n,n);
    4937         693 :     for (iG = 1; iG <= n; iG++)
    4938        3192 :       for (iF = iG+1; iF <= n; iF++)
    4939             :       {
    4940        2576 :         GEN s = gen_0;
    4941       14728 :         for (k = 1; k < l; k++)
    4942       12152 :           s = gadd(s, bil(gmael(vf,k,iF), gmael(vg,k,iG), C));
    4943        2576 :         gcoeff(M,iF,iG) = s; /* <F[iF], F[iG] > */
    4944        2576 :         gcoeff(M,iG,iF) = gneg(s);
    4945             :       }
    4946          77 :     return gerepilecopy(av, M);
    4947             :   }
    4948             : }

Generated by: LCOV version 1.13