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 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.10.0 lcov report (development 19825-b77c7f8) Lines: 2121 2187 97.0 %
Date: 2016-12-06 05:49:02 Functions: 225 225 100.0 %
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        4872 : p1_size(GEN p1N) { return lg(gel(p1N,1)) - 1; }
      29             : static ulong
      30     5085696 : p1N_get_N(GEN p1N) { return gel(p1N,3)[2]; }
      31             : static GEN
      32     2095345 : p1N_get_hash(GEN p1N) { return gel(p1N,2); }
      33             : static GEN
      34        1323 : p1N_get_fa(GEN p1N) { return gel(p1N,4); }
      35             : static GEN
      36        1218 : p1N_get_div(GEN p1N) { return gel(p1N,5); }
      37             : static GEN
      38     2002630 : p1N_get_invsafe(GEN p1N) { return gel(p1N,6); }
      39             : static GEN
      40      543676 : p1N_get_inverse(GEN p1N) { return gel(p1N,7); }
      41             : /* ms-specific accessors */
      42             : /* W = msinit or msfromell */
      43             : static GEN
      44     1058827 : get_ms(GEN W) { return lg(W) == 4? gel(W,1): W; }
      45             : static GEN
      46       11669 : ms_get_p1N(GEN W) { W = get_ms(W); return gel(W,1); }
      47             : static long
      48        4256 : ms_get_N(GEN W) { return p1N_get_N(ms_get_p1N(W)); }
      49             : static GEN
      50        1659 : ms_get_hashcusps(GEN W) { W = get_ms(W); return gel(W,16); }
      51             : static GEN
      52      584766 : ms_get_section(GEN W) { W = get_ms(W); return gel(W,12); }
      53             : static GEN
      54      164808 : ms_get_genindex(GEN W) { W = get_ms(W); return gel(W,5); }
      55             : static long
      56      157920 : ms_get_nbgen(GEN W) { return lg(ms_get_genindex(W))-1; }
      57             : static long
      58      132202 : ms_get_nbE1(GEN W)
      59             : {
      60             :   GEN W11;
      61      132202 :   W = get_ms(W); W11 = gel(W,11);
      62      132202 :   return W11[4] - W11[3];
      63             : }
      64             : /* msk-specific accessors */
      65             : static long
      66           7 : msk_get_dim(GEN W) { return gmael(W,3,2)[2]; }
      67             : static GEN
      68       61341 : msk_get_basis(GEN W) { return gmael(W,3,1); }
      69             : static long
      70       11669 : msk_get_weight(GEN W) { return gmael(W,3,2)[1]; }
      71             : static GEN
      72       58373 : msk_get_st(GEN W) { return gmael(W,3,3); }
      73             : static GEN
      74       58373 : msk_get_link(GEN W) { return gmael(W,3,4); }
      75             : static GEN
      76       58373 : msk_get_invphiblock(GEN W) { return gmael(W,3,5); }
      77             : static long
      78        4592 : msk_get_sign(GEN W)
      79             : {
      80        4592 :   GEN t = gel(W,2);
      81        4592 :   return typ(t)==t_INT? 0: itos(gel(t,1));
      82             : }
      83             : static GEN
      84         553 : msk_get_star(GEN W) { return gmael(W,2,2); }
      85             : static GEN
      86        3472 : msk_get_starproj(GEN W) { return gmael(W,2,3); }
      87             : 
      88             : long
      89           7 : msgetlevel(GEN W) { checkms(W); return ms_get_N(W); }
      90             : long
      91           7 : msgetweight(GEN W) { checkms(W); return msk_get_weight(W); }
      92             : long
      93          21 : msgetsign(GEN W) { checkms(W); return msk_get_sign(W); }
      94             : 
      95             : void
      96        5761 : checkms(GEN W)
      97             : {
      98        5761 :   if (typ(W) != t_VEC || lg(W) != 4)
      99           0 :     pari_err_TYPE("checkms [please apply msinit]", W);
     100        5761 : }
     101             : 
     102             : /** MODULAR TO SYM **/
     103             : 
     104             : /* q a t_FRAC or t_INT */
     105             : static GEN
     106        6552 : Q_log_init(ulong N, GEN q)
     107             : {
     108             :   long l, n;
     109             :   GEN Q;
     110             : 
     111        6552 :   q = gboundcf(q, 0);
     112        6552 :   l = lg(q);
     113        6552 :   Q = cgetg(l, t_VECSMALL);
     114        6552 :   Q[1] = 1;
     115        6552 :   for (n=2; n <l; n++) Q[n] = umodiu(gel(q,n), N);
     116       15547 :   for (n=3; n < l; n++)
     117        8995 :     Q[n] = Fl_add(Fl_mul(Q[n], Q[n-1], N), Q[n-2], N);
     118        6552 :   return Q;
     119             : }
     120             : 
     121             : /** INIT MODSYM STRUCTURE, WEIGHT 2 **/
     122             : 
     123             : /* num = [Gamma : Gamma_0(N)] = N * Prod_{p|N} (1+p^-1) */
     124             : static ulong
     125        1218 : count_Manin_symbols(ulong N, GEN P)
     126             : {
     127        1218 :   long i, l = lg(P);
     128        1218 :   ulong num = N;
     129        1218 :   for (i = 1; i < l; i++) { ulong p = P[i]; num *= p+1; num /= p; }
     130        1218 :   return num;
     131             : }
     132             : /* returns the list of "Manin symbols" (c,d) in (Z/NZ)^2, (c,d,N) = 1
     133             :  * generating H^1(X_0(N), Z) */
     134             : static GEN
     135        1218 : generatemsymbols(ulong N, ulong num, GEN divN)
     136             : {
     137        1218 :   GEN ret = cgetg(num+1, t_VEC);
     138        1218 :   ulong c, d, curn = 0;
     139             :   long i, l;
     140             :   /* generate Manin-symbols in two lists: */
     141             :   /* list 1: (c:1) for 0 <= c < N */
     142        1218 :   for (c = 0; c < N; c++) gel(ret, ++curn) = mkvecsmall2(c, 1);
     143        1218 :   if (N == 1) return ret;
     144             :   /* list 2: (c:d) with 1 <= c < N, c | N, 0 <= d < N, gcd(d,N) > 1, gcd(c,d)=1.
     145             :    * Furthermore, d != d0 (mod N/c) with c,d0 already in the list */
     146        1218 :   l = lg(divN) - 1;
     147             :   /* c = 1 first */
     148        1218 :   gel(ret, ++curn) = mkvecsmall2(1,0);
     149      122304 :   for (d = 2; d < N; d++)
     150      121086 :     if (ugcd(d,N) != 1UL)
     151       42987 :       gel(ret, ++curn) = mkvecsmall2(1,d);
     152             :   /* omit c = 1 (first) and c = N (last) */
     153        3346 :   for (i=2; i < l; i++)
     154             :   {
     155             :     ulong Novc, d0;
     156        2128 :     c = divN[i];
     157        2128 :     Novc = N / c;
     158       62664 :     for (d0 = 2; d0 <= Novc; d0++)
     159             :     {
     160       60536 :       ulong k, d = d0;
     161       60536 :       if (ugcd(d, Novc) == 1UL) continue;
     162       88067 :       for (k = 0; k < c; k++, d += Novc)
     163       79576 :         if (ugcd(c,d) == 1UL)
     164             :         {
     165       11186 :           gel(ret, ++curn) = mkvecsmall2(c,d);
     166       11186 :           break;
     167             :         }
     168             :     }
     169             :   }
     170        1218 :   if (curn != num) pari_err_BUG("generatemsymbols [wrong number of symbols]");
     171        1218 :   return ret;
     172             : }
     173             : 
     174             : static GEN
     175        1218 : inithashmsymbols(ulong N, GEN symbols)
     176             : {
     177        1218 :   GEN H = zerovec(N);
     178        1218 :   long k, l = lg(symbols);
     179             :   /* skip the (c:1), 0 <= c < N and (1:0) */
     180       55391 :   for (k=N+2; k < l; k++)
     181             :   {
     182       54173 :     GEN s = gel(symbols, k);
     183       54173 :     ulong c = s[1], d = s[2], Novc = N/c;
     184       54173 :     if (gel(H,c) == gen_0) gel(H,c) = const_vecsmall(Novc+1,0);
     185       54173 :     if (c != 1) { d %= Novc; if (!d) d = Novc; }
     186       54173 :     mael(H, c, d) = k;
     187             :   }
     188        1218 :   return H;
     189             : }
     190             : 
     191             : /** Helper functions for Sl2(Z) / Gamma_0(N) **/
     192             : /* [a,b;c,d] */
     193             : static GEN
     194     1238951 : mkmat22(GEN a, GEN b, GEN c, GEN d) { retmkmat2(mkcol2(a,c),mkcol2(b,d)); }
     195             : /* M a 2x2 ZM in SL2(Z) */
     196             : static GEN
     197     1046493 : SL2_inv(GEN M)
     198             : {
     199     1046493 :   GEN a=gcoeff(M,1,1), b=gcoeff(M,1,2), c=gcoeff(M,2,1), d=gcoeff(M,2,2);
     200     1046493 :   return mkmat22(d,negi(b), negi(c),a);
     201             : }
     202             : /* M a 2x2 zm in SL2(Z) */
     203             : static GEN
     204      573146 : sl2_inv(GEN M)
     205             : {
     206      573146 :   long a=coeff(M,1,1), b=coeff(M,1,2), c=coeff(M,2,1), d=coeff(M,2,2);
     207      573146 :   return mkvec2(mkvecsmall2(d, -c), mkvecsmall2(-b, a));
     208             : }
     209             : /* Return the zm [a,b; c,d] */
     210             : static GEN
     211     1090404 : mat2(long a, long b, long c, long d)
     212     1090404 : { return mkvec2(mkvecsmall2(a,c), mkvecsmall2(b,d)); }
     213             : 
     214             : /* Input: a = 2-vector = path = {r/s,x/y}
     215             :  * Output: either [r,x;s,y] or [-r,x;-s,y], whichever has determinant > 0 */
     216             : static GEN
     217      692650 : path_to_zm(GEN a)
     218             : {
     219      692650 :   GEN v = gel(a,1), w = gel(a,2);
     220      692650 :   long r = v[1], s = v[2], x = w[1], y = w[2];
     221      692650 :   if (cmpii(mulss(r,y), mulss(x,s)) < 0) { r = -r; s = -s; }
     222      692650 :   return mat2(r,x,s,y);
     223             : }
     224             : /* path from c1 to c2 */
     225             : static GEN
     226      362285 : mkpath(GEN c1, GEN c2) { return mat2(c1[1], c2[1], c1[2], c2[2]); }
     227             : static long
     228      512309 : cc(GEN M) { GEN v = gel(M,1); return v[2]; }
     229             : static long
     230      512309 : dd(GEN M) { GEN v = gel(M,2); return v[2]; }
     231             : 
     232             : /*Input: a,b = 2 paths, N = integer
     233             :  *Output: 1 if the a,b are \Gamma_0(N)-equivalent; 0 otherwise */
     234             : static int
     235       59087 : gamma_equiv(GEN a, GEN b, ulong N)
     236             : {
     237       59087 :   pari_sp av = avma;
     238       59087 :   GEN m = path_to_zm(a);
     239       59087 :   GEN n = path_to_zm(b);
     240       59087 :   GEN d = subii(mulss(cc(m),dd(n)), mulss(dd(m),cc(n)));
     241       59087 :   ulong res = umodiu(d, N);
     242       59087 :   avma = av; return res == 0;
     243             : }
     244             : /* Input: a,b = 2 paths that are \Gamma_0(N)-equivalent, N = integer
     245             :  * Output: M in \Gamma_0(N) such that Mb=a */
     246             : static GEN
     247       31073 : gamma_equiv_matrix(GEN a, GEN b)
     248             : {
     249       31073 :   GEN m = zm_to_ZM( path_to_zm(a) );
     250       31073 :   GEN n = zm_to_ZM( path_to_zm(b) );
     251       31073 :   return ZM_mul(m, SL2_inv(n));
     252             : }
     253             : 
     254             : /*************/
     255             : /* P^1(Z/NZ) */
     256             : /*************/
     257             : /* result is known to be representable as an ulong */
     258             : static ulong
     259       57414 : lcmuu(ulong a, ulong b) { ulong d = ugcd(a,b); return (a/d) * b; }
     260             : /* a != 0 in Z/NZ. Return u in (Z/NZ)^* such that au = gcd(a, N) (mod N)*/
     261             : static ulong
     262      322427 : Fl_inverse(ulong a, ulong N)
     263             : {
     264      322427 :   ulong d, d0, e, u = Fl_invgen(a, N, &d);
     265      322427 :   if (d == 1) return u;
     266      129913 :   e = N / d;
     267      129913 :   d0 = u_ppo(d, e); /* d = d0 d1, d0 coprime to N/d, core(d1) | N/d */
     268      129913 :   if (d0 == 1) return u;
     269       57414 :   e = lcmuu(e, d / d0);
     270       57414 :   return u_chinese_coprime(u, 1, e, d0, e*d0);
     271             : }
     272             : 
     273             : /* Input: N = integer
     274             :  * Output: creates P^1(Z/NZ) = [symbols, H, N]
     275             :  *   symbols: list of vectors [x,y] that give a set of representatives
     276             :  *            of P^1(Z/NZ)
     277             :  *   H: an M by M grid whose value at the r,c-th place is the index of the
     278             :  *      "standard representative" equivalent to [r,c] occuring in the first
     279             :  *      list. If gcd(r,c,N) > 1 the grid has value 0. */
     280             : static GEN
     281        1218 : create_p1mod(ulong N)
     282             : {
     283        1218 :   GEN fa = factoru(N), div = divisorsu_fact(gel(fa,1), gel(fa,2));
     284        1218 :   ulong i, nsym = count_Manin_symbols(N, gel(fa,1));
     285        1218 :   GEN symbols = generatemsymbols(N, nsym, div);
     286        1218 :   GEN H = inithashmsymbols(N,symbols);
     287        1218 :   GEN invsafe = cgetg(N, t_VECSMALL), inverse = cgetg(N, t_VECSMALL);
     288      123522 :   for (i = 1; i < N; i++)
     289             :   {
     290      122304 :     invsafe[i] = Fl_invsafe(i,N);
     291      122304 :     inverse[i] = Fl_inverse(i,N);
     292             :   }
     293        1218 :   return mkvecn(7, symbols, H, utoipos(N), fa, div, invsafe, inverse);
     294             : }
     295             : 
     296             : /* Let (c : d) in P1(Z/NZ).
     297             :  * If c = 0 return (0:1). If d = 0 return (1:0).
     298             :  * Else replace by (cu : du), where u in (Z/NZ)^* such that C := cu = gcd(c,N).
     299             :  * In create_p1mod(), (c : d) is represented by (C:D) where D = du (mod N/c)
     300             :  * is smallest such that gcd(C,D) = 1. Return (C : du mod N/c), which need
     301             :  * not belong to P1(Z/NZ) ! A second component du mod N/c = 0 is replaced by
     302             :  * N/c in this case to avoid problems with array indices */
     303             : static void
     304     2095345 : p1_std_form(long *pc, long *pd, GEN p1N)
     305             : {
     306     2095345 :   ulong N = p1N_get_N(p1N);
     307             :   ulong u;
     308     2095345 :   *pc = smodss(*pc, N); if (!*pc) { *pd = 1; return; }
     309     2030917 :   *pd = smodss(*pd, N); if (!*pd) { *pc = 1; return; }
     310     2002630 :   u = p1N_get_invsafe(p1N)[*pd];
     311     2002630 :   if (u) { *pc = Fl_mul(*pc,u,N); *pd = 1; return; } /* (d,N) = 1 */
     312             : 
     313      543676 :   u = p1N_get_inverse(p1N)[*pc];
     314      543676 :   if (u > 1) { *pc = Fl_mul(*pc,u,N); *pd = Fl_mul(*pd,u,N); }
     315             :   /* c | N */
     316      543676 :   if (*pc != 1) *pd %= (N / *pc);
     317      543676 :   if (!*pd) *pd = N / *pc;
     318             : }
     319             : 
     320             : /* Input: v = [x,y] = elt of P^1(Z/NZ) = class in Gamma_0(N) \ PSL2(Z)
     321             :  * Output: returns the index of the standard rep equivalent to v */
     322             : static long
     323     2095345 : p1_index(long x, long y, GEN p1N)
     324             : {
     325     2095345 :   ulong N = p1N_get_N(p1N);
     326     2095345 :   GEN H = p1N_get_hash(p1N);
     327             : 
     328     2095345 :   p1_std_form(&x, &y, p1N);
     329     2095345 :   if (y == 1) return x+1;
     330      571963 :   if (y == 0) return N+1;
     331      543676 :   if (mael(H,x,y) == 0) pari_err_BUG("p1_index");
     332      543676 :   return mael(H,x,y);
     333             : }
     334             : 
     335             : /* Cusps for \Gamma_0(N) */
     336             : 
     337             : /* \sum_{d | N} \phi(gcd(d, N/d)), using multiplicativity. fa = factor(N) */
     338             : static ulong
     339        1218 : nbcusp(GEN fa)
     340             : {
     341        1218 :   GEN P = gel(fa,1), E = gel(fa,2);
     342        1218 :   long i, l = lg(P);
     343        1218 :   ulong T = 1;
     344        3059 :   for (i = 1; i < l; i++)
     345             :   {
     346        1841 :     long e = E[i] >> 1; /* floor(E[i] / 2) */
     347        1841 :     ulong p = P[i];
     348        1841 :     if (odd(E[i]))
     349        1645 :       T *= 2 * upowuu(p, e);
     350             :     else
     351         196 :       T *= (p+1) * upowuu(p, e - 1);
     352             :   }
     353        1218 :   return T;
     354             : }
     355             : 
     356             : /* to each cusp in \Gamma_0(N) P1(Q), represented by p/q, we associate a
     357             :  * unique index. Canonical representative: (1:0) or (p:q) with q | N, q < N,
     358             :  * p defined modulo d := gcd(N/q,q), (p,d) = 1.
     359             :  * Return [[N, nbcusps], H, cusps]*/
     360             : static GEN
     361        1218 : inithashcusps(GEN p1N)
     362             : {
     363        1218 :   ulong N = p1N_get_N(p1N);
     364        1218 :   GEN div = p1N_get_div(p1N), H = zerovec(N+1);
     365        1218 :   long k, ind, l = lg(div), ncusp = nbcusp(p1N_get_fa(p1N));
     366        1218 :   GEN cusps = cgetg(ncusp+1, t_VEC);
     367             : 
     368        1218 :   gel(H,1) = mkvecsmall2(0/*empty*/, 1/* first cusp: (1:0) */);
     369        1218 :   gel(cusps, 1) = mkvecsmall2(1,0);
     370        1218 :   ind = 2;
     371        4564 :   for (k=1; k < l-1; k++) /* l-1: remove q = N */
     372             :   {
     373        3346 :     ulong p, q = div[k], d = ugcd(q, N/q);
     374        3346 :     GEN h = const_vecsmall(d+1,0);
     375        3346 :     gel(H,q+1) = h ;
     376        8841 :     for (p = 0; p < d; p++)
     377        5495 :       if (ugcd(p,d) == 1)
     378             :       {
     379        4284 :         h[p+1] = ind;
     380        4284 :         gel(cusps, ind) = mkvecsmall2(p,q);
     381        4284 :         ind++;
     382             :       }
     383             :   }
     384        1218 :   return mkvec3(mkvecsmall2(N,ind-1), H, cusps);
     385             : }
     386             : /* c = [p,q], (p,q) = 1, return a canonical representative for
     387             :  * \Gamma_0(N)(p/q) */
     388             : static GEN
     389      201789 : cusp_std_form(GEN c, GEN S)
     390             : {
     391      201789 :   long p, N = gel(S,1)[1], q = smodss(c[2], N);
     392             :   ulong u, d;
     393      201789 :   if (q == 0) return mkvecsmall2(1, 0);
     394      200123 :   p = smodss(c[1], N);
     395      200123 :   u = Fl_inverse(q, N);
     396      200123 :   q = Fl_mul(q,u, N);
     397      200123 :   d = ugcd(q, N/q);
     398      200123 :   return mkvecsmall2(Fl_div(p % d,u % d, d), q);
     399             : }
     400             : /* c = [p,q], (p,q) = 1, return the index of the corresponding cusp.
     401             :  * S from inithashcusps */
     402             : static ulong
     403      201789 : cusp_index(GEN c, GEN S)
     404             : {
     405             :   long p, q;
     406      201789 :   GEN H = gel(S,2);
     407      201789 :   c = cusp_std_form(c, S);
     408      201789 :   p = c[1]; q = c[2];
     409      201789 :   if (!mael(H,q+1,p+1)) pari_err_BUG("cusp_index");
     410      201789 :   return mael(H,q+1,p+1);
     411             : }
     412             : 
     413             : /* M a square invertible ZM, return a ZM iM such that iM M = M iM = d.Id */
     414             : static GEN
     415        2821 : ZM_inv_denom(GEN M)
     416             : {
     417        2821 :   GEN diM, iM = ZM_inv_ratlift(M, &diM);
     418        2821 :   return mkvec2(iM, diM);
     419             : }
     420             : /* return M^(-1) v, dinv = ZM_inv_denom(M) OR Qevproj_init(M) */
     421             : static GEN
     422      735504 : ZC_apply_dinv(GEN dinv, GEN v)
     423             : {
     424             :   GEN x, c, iM;
     425      735504 :   if (lg(dinv) == 3)
     426             :   {
     427      658693 :     iM = gel(dinv,1);
     428      658693 :     c = gel(dinv,2);
     429             :   }
     430             :   else
     431             :   { /* Qevproj_init */
     432       76811 :     iM = gel(dinv,2);
     433       76811 :     c = gel(dinv,3);
     434      153622 :     v = typ(v) == t_MAT? rowpermute(v, gel(dinv,4))
     435       76811 :                        : vecpermute(v, gel(dinv,4));
     436             :   }
     437      735504 :   x = RgM_RgC_mul(iM, v);
     438      735504 :   if (!isint1(c)) x = RgC_Rg_div(x, c);
     439      735504 :   return x;
     440             : }
     441             : 
     442             : /* M an n x d ZM of rank d (basis of a Q-subspace), n >= d.
     443             :  * Initialize a projector on M */
     444             : GEN
     445        3647 : Qevproj_init(GEN M)
     446             : {
     447             :   GEN v, perm, MM, iM, diM;
     448        3647 :   v = ZM_indexrank(M); perm = gel(v,1);
     449        3647 :   MM = rowpermute(M, perm); /* square invertible */
     450        3647 :   iM = ZM_inv_ratlift(MM, &diM);
     451        3647 :   return mkvec4(M, iM, diM, perm);
     452             : }
     453             : static int
     454         161 : is_Qevproj(GEN x)
     455         161 : { return typ(x) == t_VEC && lg(x) == 5 && typ(gel(x,1)) == t_MAT; }
     456             : 
     457             : /* same with typechecks */
     458             : static GEN
     459         700 : Qevproj_init0(GEN M)
     460             : {
     461         700 :   switch(typ(M))
     462             :   {
     463             :     case t_VEC:
     464         651 :       if (lg(M) == 5) return M;
     465           0 :       break;
     466             :     case t_COL:
     467          42 :       M = mkmat(M);/*fall through*/
     468             :     case t_MAT:
     469          49 :       M = Q_primpart(M);
     470          49 :       RgM_check_ZM(M,"Qevproj_init");
     471          49 :       return Qevproj_init(M);
     472             :   }
     473           0 :   pari_err_TYPE("Qevproj_init",M);
     474           0 :   return NULL;
     475             : }
     476             : 
     477             : /* T an n x n QM, stabilizing d-dimensional Q-vector space spanned by the
     478             :  * columns of M, pro = Qevproj_init(M). Return d x d matrix of T acting
     479             :  * on M */
     480             : GEN
     481        3045 : Qevproj_apply(GEN T, GEN pro)
     482             : {
     483        3045 :   GEN M = gel(pro,1), iM = gel(pro,2), ciM = gel(pro,3), perm = gel(pro,4);
     484        3045 :   return RgM_Rg_div(RgM_mul(iM, RgM_mul(rowpermute(T,perm), M)), ciM);
     485             : }
     486             : /* Qevproj_apply(T,pro)[,k] */
     487             : GEN
     488         777 : Qevproj_apply_vecei(GEN T, GEN pro, long k)
     489             : {
     490         777 :   GEN M = gel(pro,1), iM = gel(pro,2), ciM = gel(pro,3), perm = gel(pro,4);
     491         777 :   GEN v = RgM_RgC_mul(iM, RgM_RgC_mul(rowpermute(T,perm), gel(M,k)));
     492         777 :   return RgC_Rg_div(v, ciM);
     493             : }
     494             : 
     495             : static GEN
     496        1631 : ZM_ker(GEN M) { return ZM_ker_ratlift(M); }
     497             : static GEN
     498        1162 : QM_ker(GEN M) { return ZM_ker(Q_primpart(M)); }
     499             : static GEN
     500         840 : QM_image(GEN A)
     501             : {
     502         840 :   A = vec_Q_primpart(A);
     503         840 :   return vecpermute(A, ZM_indeximage(A));
     504             : }
     505             : 
     506             : static int
     507         420 : cmp_dim(void *E, GEN a, GEN b)
     508             : {
     509             :   long k;
     510             :   (void)E;
     511         420 :   a = gel(a,1);
     512         420 :   b = gel(b,1); k = lg(a)-lg(b);
     513         420 :   return k? ((k > 0)? 1: -1): 0;
     514             : }
     515             : 
     516             : /* FIXME: could use ZX_roots for deglim = 1 */
     517             : static GEN
     518         322 : ZX_factor_limit(GEN T, long deglim, long *pl)
     519             : {
     520         322 :   GEN fa = ZX_factor(T), P, E;
     521             :   long i, l;
     522         322 :   P = gel(fa,1); *pl = l = lg(P);
     523         322 :   if (deglim <= 0) return fa;
     524         224 :   E = gel(fa,2);
     525         567 :   for (i = 1; i < l; i++)
     526         406 :     if (degpol(gel(P,i)) > deglim) break;
     527         224 :   setlg(P,i);
     528         224 :   setlg(E,i); return fa;
     529             : }
     530             : 
     531             : /* Decompose the subspace H (Qevproj format) in simple subspaces.
     532             :  * Eg for H = msnew */
     533             : static GEN
     534         252 : mssplit_i(GEN W, GEN H, long deglim)
     535             : {
     536         252 :   ulong p, N = ms_get_N(W);
     537             :   long first, dim;
     538             :   forprime_t S;
     539         252 :   GEN T1 = NULL, T2 = NULL, V;
     540         252 :   dim = lg(gel(H,1))-1;
     541         252 :   V = vectrunc_init(dim+1);
     542         252 :   if (!dim) return V;
     543         245 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
     544         245 :   vectrunc_append(V, H);
     545         245 :   first = 1; /* V[1..first-1] contains simple subspaces */
     546         616 :   while ((p = u_forprime_next(&S)))
     547             :   {
     548             :     GEN T;
     549             :     long j, lV;
     550         371 :     if (N % p == 0) continue;
     551         315 :     if (T1 && T2) {
     552          21 :       T = RgM_add(T1,T2);
     553          21 :       T2 = NULL;
     554             :     } else {
     555         294 :       T2 = T1;
     556         294 :       T1 = T = mshecke(W, p, NULL);
     557             :     }
     558         315 :     lV = lg(V);
     559         637 :     for (j = first; j < lV; j++)
     560             :     {
     561         322 :       pari_sp av = avma;
     562             :       long lP;
     563         322 :       GEN Vj = gel(V,j), P = gel(Vj,1);
     564         322 :       GEN TVj = Qevproj_apply(T, Vj); /* c T | V_j */
     565         322 :       GEN ch = QM_charpoly_ZX(TVj), fa = ZX_factor_limit(ch,deglim, &lP);
     566         322 :       GEN F = gel(fa, 1), E = gel(fa, 2);
     567         322 :       long k, lF = lg(F);
     568         322 :       if (lF == 2 && lP == 2)
     569             :       {
     570         322 :         if (isint1(gel(E,1)))
     571             :         { /* simple subspace */
     572         161 :           swap(gel(V,first), gel(V,j));
     573         161 :           first++;
     574             :         }
     575             :         else
     576           0 :           avma = av;
     577             :       }
     578         161 :       else if (lF == 1) /* discard V[j] */
     579           7 :       { swap(gel(V,j), gel(V,lg(V)-1)); setlg(V, lg(V)-1); }
     580             :       else
     581             :       { /* can split Vj */
     582             :         GEN pows;
     583         154 :         long D = 1;
     584         616 :         for (k = 1; k < lF; k++)
     585             :         {
     586         462 :           long d = degpol(gel(F,k));
     587         462 :           if (d > D) D = d;
     588             :         }
     589             :         /* remove V[j] */
     590         154 :         swap(gel(V,j), gel(V,lg(V)-1)); setlg(V, lg(V)-1);
     591         154 :         pows = RgM_powers(TVj, minss((long)2*sqrt((double)D), D));
     592         616 :         for (k = 1; k < lF; k++)
     593             :         {
     594         462 :           GEN f = gel(F,k);
     595         462 :           GEN K = QM_ker( RgX_RgMV_eval(f, pows)) ; /* Ker f(TVj) */
     596         462 :           GEN p = vec_Q_primpart( RgM_mul(P, K) );
     597         462 :           vectrunc_append(V, Qevproj_init(p));
     598         462 :           if (lg(K) == 2 || isint1(gel(E,k)))
     599             :           { /* simple subspace */
     600         385 :             swap(gel(V,first), gel(V, lg(V)-1));
     601         385 :             first++;
     602             :           }
     603             :         }
     604         154 :         if (j < first) j = first;
     605             :       }
     606             :     }
     607         315 :     if (first >= lg(V)) {
     608         245 :       gen_sort_inplace(V, NULL, cmp_dim, NULL);
     609         245 :       return V;
     610             :     }
     611             :   }
     612           0 :   pari_err_BUG("subspaces not found");
     613           0 :   return NULL;
     614             : }
     615             : GEN
     616         252 : mssplit(GEN W, GEN H, long deglim)
     617             : {
     618         252 :   pari_sp av = avma;
     619         252 :   checkms(W);
     620         252 :   if (!msk_get_sign(W))
     621           0 :     pari_err_DOMAIN("mssplit","abs(sign)","!=",gen_1,gen_0);
     622         252 :   H = Qevproj_init0(H);
     623         252 :   return gerepilecopy(av, mssplit_i(W,H,deglim));
     624             : }
     625             : 
     626             : /* proV = Qevproj_init of a Hecke simple subspace, return [ a_n, n <= B ] */
     627             : static GEN
     628         238 : msqexpansion_i(GEN W, GEN proV, ulong B)
     629             : {
     630         238 :   ulong p, N = ms_get_N(W), sqrtB;
     631         238 :   long i, d, k = msk_get_weight(W);
     632             :   forprime_t S;
     633         238 :   GEN T1=NULL, T2=NULL, TV=NULL, ch=NULL, v, dTiv, Tiv, diM, iM, L;
     634         238 :   switch(B)
     635             :   {
     636           0 :     case 0: return cgetg(1,t_VEC);
     637           0 :     case 1: return mkvec(gen_1);
     638             :   }
     639         238 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
     640         588 :   while ((p = u_forprime_next(&S)))
     641             :   {
     642             :     GEN T;
     643         350 :     if (N % p == 0) continue;
     644         259 :     if (T1 && T2)
     645             :     {
     646           0 :       T = RgM_add(T1,T2);
     647           0 :       T2 = NULL;
     648             :     }
     649             :     else
     650             :     {
     651         259 :       T2 = T1;
     652         259 :       T1 = T = mshecke(W, p, NULL);
     653             :     }
     654         259 :     TV = Qevproj_apply(T, proV); /* T | V */
     655         259 :     ch = QM_charpoly_ZX(TV);
     656         259 :     if (ZX_is_irred(ch)) break;
     657          21 :     ch = NULL;
     658             :   }
     659         238 :   if (!ch) pari_err_BUG("q-Expansion not found");
     660             :   /* T generates the Hecke algebra */
     661         238 :   d = degpol(ch);
     662         238 :   v = vec_ei(d, 1); /* take v = e_1 */
     663         238 :   Tiv = cgetg(d+1, t_MAT); /* Tiv[i] = T^(i-1)v */
     664         238 :   gel(Tiv, 1) = v;
     665         238 :   for (i = 2; i <= d; i++) gel(Tiv, i) = RgM_RgC_mul(TV, gel(Tiv,i-1));
     666         238 :   Tiv = Q_remove_denom(Tiv, &dTiv);
     667         238 :   iM = ZM_inv_ratlift(Tiv, &diM);
     668         238 :   if (dTiv) diM = gdiv(diM, dTiv);
     669         238 :   L = const_vec(B,NULL);
     670         238 :   sqrtB = usqrt(B);
     671         238 :   gel(L,1) = d > 1? mkpolmod(gen_1,ch): gen_1;
     672        2359 :   for (p = 2; p <= B; p++)
     673             :   {
     674        2121 :     pari_sp av = avma;
     675             :     GEN T, u, Tv, ap, P;
     676             :     ulong m;
     677        2121 :     if (gel(L,p)) continue;  /* p not prime */
     678         777 :     T = mshecke(W, p, NULL);
     679         777 :     Tv = Qevproj_apply_vecei(T, proV, 1); /* Tp.v */
     680             :     /* Write Tp.v = \sum u_i T^i v */
     681         777 :     u = RgC_Rg_div(RgM_RgC_mul(iM, Tv), diM);
     682         777 :     ap = gerepilecopy(av, RgV_to_RgX(u, 0));
     683         777 :     if (d > 1)
     684         399 :       ap = mkpolmod(ap,ch);
     685             :     else
     686         378 :       ap = simplify_shallow(ap);
     687         777 :     gel(L,p) = ap;
     688         777 :     if (!(N % p))
     689             :     { /* p divides the level */
     690         147 :       ulong C = B/p;
     691         546 :       for (m=1; m<=C; m++)
     692         399 :         if (gel(L,m)) gel(L,m*p) = gmul(gel(L,m), ap);
     693         147 :       continue;
     694             :     }
     695         630 :     P = powuu(p,k-1);
     696         630 :     if (p <= sqrtB) {
     697         105 :       ulong pj, oldpj = 1;
     698         490 :       for (pj = p; pj <= B; oldpj=pj, pj *= p)
     699             :       {
     700         385 :         GEN apj = (pj==p)? ap
     701         385 :                          : gsub(gmul(ap,gel(L,oldpj)), gmul(P,gel(L,oldpj/p)));
     702         385 :         gel(L,pj) = apj;
     703        2989 :         for (m = B/pj; m > 1; m--)
     704        2604 :           if (gel(L,m) && m%p) gel(L,m*pj) = gmul(gel(L,m), apj);
     705             :       }
     706             :     } else {
     707         525 :       gel(L,p) = ap;
     708        1043 :       for (m = B/p; m > 1; m--)
     709         518 :         if (gel(L,m)) gel(L,m*p) = gmul(gel(L,m), ap);
     710             :     }
     711             :   }
     712         238 :   return L;
     713             : }
     714             : GEN
     715         238 : msqexpansion(GEN W, GEN proV, ulong B)
     716             : {
     717         238 :   pari_sp av = avma;
     718         238 :   checkms(W);
     719         238 :   proV = Qevproj_init0(proV);
     720         238 :   return gerepilecopy(av, msqexpansion_i(W,proV,B));
     721             : }
     722             : 
     723             : static GEN
     724         602 : Qevproj_apply2(GEN T, GEN pro1, GEN pro2)
     725             : {
     726         602 :   GEN M = gel(pro1,1), iM = gel(pro2,2), ciM = gel(pro2,3), perm = gel(pro2,4);
     727         602 :   return RgM_Rg_div(RgM_mul(iM, RgM_mul(rowpermute(T,perm), M)), ciM);
     728             : }
     729             : static GEN
     730         259 : Qevproj_apply0(GEN T, GEN pro)
     731             : {
     732         259 :   GEN iM = gel(pro,2), perm = gel(pro,4);
     733         259 :   return vec_Q_primpart(ZM_mul(iM, rowpermute(T,perm)));
     734             : }
     735             : 
     736             : static GEN
     737         294 : Qevproj_star(GEN W, GEN H)
     738             : {
     739         294 :   long s = msk_get_sign(W);
     740         294 :   if (s)
     741             :   { /* project on +/- component */
     742         259 :     GEN A = RgM_mul(msk_get_star(W), H);
     743         259 :     A = (s > 0)? gadd(A, H): gsub(A, H);
     744             :     /* Im(star + sign) = Ker(star - sign) */
     745         259 :     H = QM_image(A);
     746         259 :     H = Qevproj_apply0(H, msk_get_starproj(W));
     747             :   }
     748         294 :   return H;
     749             : }
     750             : 
     751             : static GEN
     752        2387 : Tp_matrices(ulong p)
     753             : {
     754        2387 :   GEN v = cgetg(p+2, t_VEC);
     755             :   ulong i;
     756        2387 :   for (i = 1; i <= p; i++) gel(v,i) = mat2(1, i-1, 0, p);
     757        2387 :   gel(v,i) = mat2(p, 0, 0, 1);
     758        2387 :   return v;
     759             : }
     760             : static GEN
     761         924 : Up_matrices(ulong p)
     762             : {
     763         924 :   GEN v = cgetg(p+1, t_VEC);
     764             :   ulong i;
     765         924 :   for (i = 1; i <= p; i++) gel(v,i) = mat2(1, i-1, 0, p);
     766         924 :   return v;
     767             : }
     768             : 
     769             : /* M = N/p. Classes of Gamma_0(M) / Gamma_O(N) when p | M */
     770             : static GEN
     771         168 : NP_matrices(ulong M, ulong p)
     772             : {
     773         168 :   GEN v = cgetg(p+1, t_VEC);
     774             :   ulong i;
     775         168 :   for (i = 1; i <= p; i++) gel(v,i) = mat2(1, 0, (i-1)*M, 1);
     776         168 :   return v;
     777             : }
     778             : /* M = N/p. Extra class of Gamma_0(M) / Gamma_O(N) when p \nmid M */
     779             : static GEN
     780          84 : NP_matrix_extra(ulong M, ulong p)
     781             : {
     782          84 :   long w,z, d = cbezout(p, -M, &w, &z);
     783          84 :   if (d != 1) return NULL;
     784          84 :   return mat2(w,z,M,p);
     785             : }
     786             : static GEN
     787          98 : WQ_matrix(long N, long Q)
     788             : {
     789          98 :   long w,z, d = cbezout(Q, N/Q, &w, &z);
     790          98 :   if (d != 1) return NULL;
     791          98 :   return mat2(Q,1,-N*z,Q*w);
     792             : }
     793             : 
     794             : GEN
     795         266 : msnew(GEN W)
     796             : {
     797         266 :   pari_sp av = avma;
     798         266 :   GEN S = mscuspidal(W, 0);
     799         266 :   ulong N = ms_get_N(W);
     800         266 :   long s = msk_get_sign(W);
     801         266 :   if (!uisprime(N))
     802             :   {
     803         105 :     GEN p1N = ms_get_p1N(W), P = gel(p1N_get_fa(p1N), 1);
     804         105 :     long i, nP = lg(P)-1, k = msk_get_weight(W);
     805         105 :     GEN v = cgetg(2*nP + 1, t_COL);
     806         105 :     S = gel(S,1); /* Q basis */
     807         273 :     for (i = 1; i <= nP; i++)
     808             :     {
     809         168 :       pari_sp av = avma, av2;
     810         168 :       long M = N/P[i];
     811         168 :       GEN T1,Td, Wi = mskinit(M, k, s);
     812         168 :       GEN v1 = NP_matrices(M, P[i]);
     813         168 :       GEN vd = Up_matrices(P[i]);
     814             :       /* p^2 \nmid N */
     815         168 :       if (M % P[i])
     816             :       {
     817          84 :         v1 = shallowconcat(v1, mkvec(NP_matrix_extra(M,P[i])));
     818          84 :         vd = shallowconcat(vd, mkvec(WQ_matrix(N,P[i])));
     819             :       }
     820         168 :       T1 = getMorphism(W, Wi, v1);
     821         168 :       Td = getMorphism(W, Wi, vd);
     822         168 :       if (s)
     823             :       {
     824         154 :         T1 = Qevproj_apply2(T1, msk_get_starproj(W), msk_get_starproj(Wi));
     825         154 :         Td = Qevproj_apply2(Td, msk_get_starproj(W), msk_get_starproj(Wi));
     826             :       }
     827         168 :       av2 = avma;
     828         168 :       T1 = RgM_mul(T1,S);
     829         168 :       Td = RgM_mul(Td,S);  /* multiply by S = restrict to mscusp */
     830         168 :       gerepileallsp(av, av2, 2, &T1, &Td);
     831         168 :       gel(v,2*i-1) = T1;
     832         168 :       gel(v,2*i)   = Td;
     833             :     }
     834         105 :     S = ZM_mul(S, QM_ker(matconcat(v))); /* Snew */
     835         105 :     S = Qevproj_init(vec_Q_primpart(S));
     836             :   }
     837         266 :   return gerepilecopy(av, S);
     838             : }
     839             : 
     840             : /* Solve the Manin relations for a congruence subgroup \Gamma by constructing
     841             :  * a well-formed fundamental domain for the action of \Gamma on upper half
     842             :  * space. See
     843             :  * Pollack and Stevens, Overconvergent modular symbols and p-adic L-functions
     844             :  * Annales scientifiques de l'ENS 44, fascicule 1 (2011), 1-42
     845             :  * http://math.bu.edu/people/rpollack/Papers/Overconvergent_modular_symbols_and_padic_Lfunctions.pdf
     846             :  *
     847             :  * FIXME: Implemented for \Gamma = \Gamma_0(N) only. */
     848             : 
     849             : #if 0 /* Pollack-Stevens shift their paths so as to solve equations of the
     850             :          form f(z+1) - f(z) = g. We don't (to avoid mistakes) so we will
     851             :          have to solve eqs of the form f(z-1) - f(z) = g */
     852             : /* c = a/b; as a t_VECSMALL [a,b]; return c-1 as a t_VECSMALL */
     853             : static GEN
     854             : Shift_left_cusp(GEN c) { long a=c[1], b=c[2]; return mkvecsmall2(a - b, b); }
     855             : /* c = a/b; as a t_VECSMALL [a,b]; return c+1 as a t_VECSMALL */
     856             : static GEN
     857             : Shift_right_cusp(GEN c) { long a=c[1], b=c[2]; return mkvecsmall2(a + b, b); }
     858             : /*Input: path = [r,s] (thought of as a geodesic between these points)
     859             :  *Output: The path shifted by one to the left, i.e. [r-1,s-1] */
     860             : static GEN
     861             : Shift_left(GEN path)
     862             : {
     863             :   GEN r = gel(path,1), s = gel(path,2);
     864             :   return mkvec2(Shift_left_cusp(r), Shift_left_cusp(s)); }
     865             : /*Input: path = [r,s] (thought of as a geodesic between these points)
     866             :  *Output: The path shifted by one to the right, i.e. [r+1,s+1] */
     867             : GEN
     868             : Shift_right(GEN path)
     869             : {
     870             :   GEN r = gel(path,1), s = gel(path,2);
     871             :   return mkvec2(Shift_right_cusp(r), Shift_right_cusp(s)); }
     872             : #endif
     873             : 
     874             : /* linked lists */
     875             : typedef struct list_t { GEN data; struct list_t *next; } list_t;
     876             : static list_t *
     877       60718 : list_new(GEN x)
     878             : {
     879       60718 :   list_t *L = (list_t*)stack_malloc(sizeof(list_t));
     880       60718 :   L->data = x;
     881       60718 :   L->next = NULL; return L;
     882             : }
     883             : static void
     884       59500 : list_insert(list_t *L, GEN x)
     885             : {
     886       59500 :   list_t *l = list_new(x);
     887       59500 :   l->next = L->next;
     888       59500 :   L->next = l;
     889       59500 : }
     890             : 
     891             : /*Input: N > 1, p1N = P^1(Z/NZ)
     892             :  *Output: a connected fundamental domain for the action of \Gamma_0(N) on
     893             :  *  upper half space.  When \Gamma_0(N) is torsion free, the domain has the
     894             :  *  property that all of its vertices are cusps.  When \Gamma_0(N) has
     895             :  *  three-torsion, 2 extra triangles need to be added.
     896             :  *
     897             :  * The domain is constructed by beginning with the triangle with vertices 0,1
     898             :  * and oo.  Each adjacent triangle is successively tested to see if it contains
     899             :  * points not \Gamma_0(N) equivalent to some point in our region.  If a
     900             :  * triangle contains new points, it is added to the region.  This process is
     901             :  * continued until the region can no longer be extended (and still be a
     902             :  * fundamental domain) by added an adjacent triangle.  The list of cusps
     903             :  * between 0 and 1 are then returned
     904             :  *
     905             :  * Precisely, the function returns a list such that the elements of the list
     906             :  * with odd index are the cusps in increasing order.  The even elements of the
     907             :  * list are either an "x" or a "t".  A "t" represents that there is an element
     908             :  * of order three such that its fixed point is in the triangle directly
     909             :  * adjacent to the our region with vertices given by the cusp before and after
     910             :  * the "t".  The "x" represents that this is not the case. */
     911             : enum { type_X, type_DO /* ? */, type_T };
     912             : static GEN
     913        1218 : form_list_of_cusps(ulong N, GEN p1N)
     914             : {
     915        1218 :   pari_sp av = avma;
     916        1218 :   long i, position, nbC = 2;
     917             :   GEN v, L;
     918             :   list_t *C, *c;
     919             :   /* Let t be the index of a class in PSL2(Z) / \Gamma in our fixed enumeration
     920             :    * v[t] != 0 iff it is the class of z tau^r for z a previous alpha_i
     921             :    * or beta_i.
     922             :    * For \Gamma = \Gamma_0(N), the enumeration is given by p1_index.
     923             :    * We write cl(gamma) = the class of gamma mod \Gamma */
     924        1218 :   v = const_vecsmall(p1_size(p1N), 0);
     925        1218 :   i = p1_index( 0, 1, p1N); v[i] = 1;
     926        1218 :   i = p1_index( 1,-1, p1N); v[i] = 2;
     927        1218 :   i = p1_index(-1, 0, p1N); v[i] = 3;
     928             :   /* the value is unused [debugging]: what matters is whether it is != 0 */
     929        1218 :   position = 4;
     930             :   /* at this point, Fund = R, v contains the classes of Id, tau, tau^2 */
     931             : 
     932        1218 :   C  = list_new(mkvecsmall3(0,1, type_X));
     933        1218 :   list_insert(C, mkvecsmall3(1,1,type_DO));
     934             :   /* C is a list of triples[a,b,t], where c = a/b is a cusp, and t is the type
     935             :    * of the path between c and the PREVIOUS cusp in the list, coded as
     936             :    *   type_DO = "?", type_X = "x", type_T = "t"
     937             :    * Initially, C = [0/1,"?",1/1]; */
     938             : 
     939             :   /* loop through the current set of cusps C and check to see if more cusps
     940             :    * should be added */
     941             :   for (;;)
     942             :   {
     943        6727 :     int done = 1;
     944      296884 :     for (c = C; c; c = c->next)
     945             :     {
     946             :       GEN cusp1, cusp2, gam;
     947             :       long pos, b1, b2, b;
     948             : 
     949      296884 :       if (!c->next) break;
     950      290157 :       cusp1 = c->data; /* = a1/b1 */
     951      290157 :       cusp2 = (c->next)->data; /* = a2/b2 */
     952      290157 :       if (cusp2[3] != type_DO) continue;
     953             : 
     954             :       /* gam (oo -> 0) = (cusp2 -> cusp1), gam in PSL2(Z) */
     955      117782 :       gam = path_to_zm(mkpath(cusp2, cusp1)); /* = [a2,a1;b2,b1] */
     956             :       /* we have normalized the cusp representation so that a1 b2 - a2 b1 = 1 */
     957      117782 :       b1 = coeff(gam,2,1); b2 = coeff(gam,2,2);
     958             :       /* gam.1  = (a1 + a2) / (b1 + b2) */
     959      117782 :       b = b1 + b2;
     960             :       /* Determine whether the adjacent triangle *below* (cusp1->cusp2)
     961             :        * should be added */
     962      117782 :       pos = p1_index(b1,b2, p1N); /* did we see cl(gam) before ? */
     963      117782 :       if (v[pos])
     964       59087 :         cusp2[3] = type_X; /* NO */
     965             :       else
     966             :       { /* YES */
     967             :         ulong B1, B2;
     968       58695 :         v[pos] = position;
     969       58695 :         i = p1_index(-(b1+b2), b1, p1N); v[i] = position+1;
     970       58695 :         i = p1_index(b2, -(b1+b2), p1N); v[i] = position+2;
     971             :         /* add cl(gam), cl(gam*TAU), cl(gam*TAU^2) to v */
     972       58695 :         position += 3;
     973             :         /* gam tau gam^(-1) in \Gamma ? */
     974       58695 :         B1 = smodss(b1, N);
     975       58695 :         B2 = smodss(b2, N);
     976       58695 :         if ((Fl_sqr(B2,N) + Fl_sqr(B1,N) + Fl_mul(B1,B2,N)) % N == 0)
     977         413 :           cusp2[3] = type_T;
     978             :         else
     979             :         {
     980       58282 :           long a1 = coeff(gam, 1,1), a2 = coeff(gam, 1,2);
     981       58282 :           long a = a1 + a2; /* gcd(a,b) = 1 */
     982       58282 :           list_insert(c, mkvecsmall3(a,b,type_DO));
     983       58282 :           c = c->next;
     984       58282 :           nbC++;
     985       58282 :           done = 0;
     986             :         }
     987             :       }
     988             :     }
     989        6727 :     if (done) break;
     990        5509 :   }
     991        1218 :   L = cgetg(nbC+1, t_VEC); i = 1;
     992        1218 :   for (c = C; c; c = c->next) gel(L,i++) = c->data;
     993        1218 :   return gerepilecopy(av, L);
     994             : }
     995             : 
     996             : /* M in PSL2(Z). Return index of M in P1^(Z/NZ) = Gamma0(N) \ PSL2(Z),
     997             :  * and M0 in Gamma_0(N) such that M = M0 * M', where M' = chosen
     998             :  * section( PSL2(Z) -> P1^(Z/NZ) ). */
     999             : static GEN
    1000      446698 : Gamma0N_decompose(GEN W, GEN M, long *index)
    1001             : {
    1002      446698 :   GEN p1N = gel(W,1), W3 = gel(W,3), section = ms_get_section(W);
    1003             :   GEN A;
    1004      446698 :   ulong N = p1N_get_N(p1N);
    1005      446698 :   ulong c = umodiu(gcoeff(M,2,1), N);
    1006      446698 :   ulong d = umodiu(gcoeff(M,2,2), N);
    1007      446698 :   long s, ind = p1_index(c, d, p1N); /* as an elt of P1(Z/NZ) */
    1008      446698 :   *index = W3[ind]; /* as an elt of F, E2, ... */
    1009      446698 :   M = ZM_zm_mul(M, sl2_inv(gel(section,ind)));
    1010             :   /* normalize mod +/-Id */
    1011      446698 :   A = gcoeff(M,1,1);
    1012      446698 :   s = signe(A);
    1013      446698 :   if (s < 0)
    1014      221459 :     M = ZM_neg(M);
    1015      225239 :   else if (!s)
    1016             :   {
    1017           0 :     GEN C = gcoeff(M,2,1);
    1018           0 :     if (signe(C) < 0) M = ZM_neg(M);
    1019             :   }
    1020      446698 :   return M;
    1021             : }
    1022             : /* same for a path. Return [[ind], M] */
    1023             : static GEN
    1024      123872 : path_Gamma0N_decompose(GEN W, GEN path)
    1025             : {
    1026      123872 :   GEN p1N = gel(W,1);
    1027      123872 :   GEN p1index_to_ind = gel(W,3);
    1028      123872 :   GEN section = ms_get_section(W);
    1029      123872 :   GEN M = path_to_zm(path);
    1030      123872 :   long p1index = p1_index(cc(M), dd(M), p1N);
    1031      123872 :   long ind = p1index_to_ind[p1index];
    1032      123872 :   GEN M0 = ZM_zm_mul(zm_to_ZM(M), sl2_inv(gel(section,p1index)));
    1033      123872 :   return mkvec2(mkvecsmall(ind), M0);
    1034             : }
    1035             : 
    1036             : /*Form generators of H_1(X_0(N),{cusps},Z)
    1037             : *
    1038             : *Input: N = integer > 1, p1N = P^1(Z/NZ)
    1039             : *Output: [cusp_list,E,F,T2,T3,E1] where
    1040             : *  cusps_list = list of cusps describing fundamental domain of
    1041             : *    \Gamma_0(N).
    1042             : *  E = list of paths in the boundary of the fundamental domains and oriented
    1043             : *    clockwise such that they do not contain a point
    1044             : *    fixed by an element of order 2 and they are not an edge of a
    1045             : *    triangle containing a fixed point of an element of order 3
    1046             : *  F = list of paths in the interior of the domain with each
    1047             : *    orientation appearing separately
    1048             : * T2 = list of paths in the boundary of domain containing a point fixed
    1049             : *    by an element of order 2 (oriented clockwise)
    1050             : * T3 = list of paths in the boundard of domain which are the edges of
    1051             : *    some triangle containing a fixed point of a matrix of order 3 (both
    1052             : *    orientations appear)
    1053             : * E1 = a sublist of E such that every path in E is \Gamma_0(N)-equivalent to
    1054             : *    either an element of E1 or the flip (reversed orientation) of an element
    1055             : *    of E1.
    1056             : * (Elements of T2 are \Gamma_0(N)-equivalent to their own flip.)
    1057             : *
    1058             : * sec = a list from 1..#p1N of matrices describing a section of the map
    1059             : *   SL_2(Z) to P^1(Z/NZ) given by [a,b;c,d]-->[c,d].
    1060             : *   Given our fixed enumeration of P^1(Z/NZ), the j-th element of the list
    1061             : *   represents the image of the j-th element of P^1(Z/NZ) under the section. */
    1062             : 
    1063             : /* insert path in set T */
    1064             : static void
    1065      178913 : set_insert(hashtable *T, GEN path)
    1066      178913 : { hash_insert(T, path,  (void*)(T->nb + 1)); }
    1067             : 
    1068             : static GEN
    1069       10962 : hash_to_vec(hashtable *h)
    1070             : {
    1071       10962 :   GEN v = cgetg(h->nb + 1, t_VEC);
    1072             :   ulong i;
    1073     1484756 :   for (i = 0; i < h->len; i++)
    1074             :   {
    1075     1473794 :     hashentry *e = h->table[i];
    1076     3244101 :     while (e)
    1077             :     {
    1078      296513 :       GEN key = (GEN)e->key;
    1079      296513 :       long index = (long)e->val;
    1080      296513 :       gel(v, index) = key;
    1081      296513 :       e = e->next;
    1082             :     }
    1083             :   }
    1084       10962 :   return v;
    1085             : }
    1086             : 
    1087             : static long
    1088       91350 : path_to_p1_index(GEN path, GEN p1N)
    1089             : {
    1090       91350 :   GEN M = path_to_zm(path);
    1091       91350 :   return p1_index(cc(M), dd(M), p1N);
    1092             : }
    1093             : 
    1094             : /* Pollack-Stevens sets */
    1095             : typedef struct PS_sets_t {
    1096             :   hashtable *F, *T2, *T31, *T32, *E1, *E2;
    1097             :   GEN E2_in_terms_of_E1, stdE1;
    1098             : } PS_sets_t;
    1099             : 
    1100             : static hashtable *
    1101       10787 : set_init(long max)
    1102       10787 : { return hash_create(max, (ulong(*)(void*))&hash_GEN,
    1103             :                           (int(*)(void*,void*))&gidentical, 1); }
    1104             : static void
    1105       60900 : insert_E(GEN path, PS_sets_t *S, GEN p1N)
    1106             : {
    1107       60900 :   GEN rev = vecreverse(path);
    1108       60900 :   long std = path_to_p1_index(rev, p1N);
    1109       60900 :   GEN v = gel(S->stdE1, std);
    1110       60900 :   if (v)
    1111             :   { /* [s, p1], where E1[s] = the path p1 \equiv vecreverse(path) mod \Gamma */
    1112       30450 :     GEN gamma, p1 = gel(v,2);
    1113       30450 :     long r, s = itos(gel(v,1));
    1114             : 
    1115       30450 :     set_insert(S->E2, path);
    1116       30450 :     r = S->E2->nb;
    1117       30450 :     if (gel(S->E2_in_terms_of_E1, r) != gen_0) pari_err_BUG("insert_E");
    1118             : 
    1119       30450 :     gamma = gamma_equiv_matrix(rev, p1);
    1120             :     /* E2[r] + gamma * E1[s] = 0 */
    1121       30450 :     gel(S->E2_in_terms_of_E1, r) = mkvec2(utoipos(s),
    1122             :                                           to_famat_shallow(gamma,gen_m1));
    1123             :   }
    1124             :   else
    1125             :   {
    1126       30450 :     set_insert(S->E1, path);
    1127       30450 :     std = path_to_p1_index(path, p1N);
    1128       30450 :     gel(S->stdE1, std) = mkvec2(utoipos(S->E1->nb), path);
    1129             :   }
    1130       60900 : }
    1131             : 
    1132             : static GEN
    1133        4872 : cusp_infinity(void) { return mkvecsmall2(1,0); }
    1134             : 
    1135             : static void
    1136        1218 : form_E_F_T(ulong N, GEN p1N, GEN *pC, PS_sets_t *S)
    1137             : {
    1138        1218 :   GEN C, cusp_list = form_list_of_cusps(N, p1N);
    1139        1218 :   long nbgen = lg(cusp_list)-1, nbmanin = p1_size(p1N), r, s, i;
    1140             :   hashtable *F, *T2, *T31, *T32, *E1, *E2;
    1141             : 
    1142        1218 :   *pC = C = cgetg(nbgen+1, t_VEC);
    1143       61936 :   for (i = 1; i <= nbgen; i++)
    1144             :   {
    1145       60718 :     GEN c = gel(cusp_list,i);
    1146       60718 :     gel(C,i) = mkvecsmall2(c[1], c[2]);
    1147             :   }
    1148        1218 :   S->F  = F  = set_init(nbmanin);
    1149        1218 :   S->E1 = E1 = set_init(nbgen);
    1150        1218 :   S->E2 = E2 = set_init(nbgen);
    1151        1218 :   S->T2 = T2 = set_init(nbgen);
    1152        1218 :   S->T31 = T31 = set_init(nbgen);
    1153        1218 :   S->T32 = T32 = set_init(nbgen);
    1154             : 
    1155             :   /* T31 represents the three torsion paths going from left to right */
    1156             :   /* T32 represents the three torsion paths going from right to left */
    1157       60718 :   for (r = 1; r < nbgen; r++)
    1158             :   {
    1159       59500 :     GEN c2 = gel(cusp_list,r+1);
    1160       59500 :     if (c2[3] == type_T)
    1161             :     {
    1162         413 :       GEN c1 = gel(cusp_list,r), path = mkpath(c1,c2), path2 = vecreverse(path);
    1163         413 :       set_insert(T31, path);
    1164         413 :       set_insert(T32, path2);
    1165             :     }
    1166             :   }
    1167             : 
    1168             :   /* to record relations between E2 and E1 */
    1169        1218 :   S->E2_in_terms_of_E1 = zerovec(nbgen);
    1170        1218 :   S->stdE1 = const_vec(nbmanin, NULL);
    1171             : 
    1172             :   /* Assumption later: path [oo,0] is E1[1], path [1,oo] is E2[1] */
    1173             :   {
    1174        1218 :     GEN oo = cusp_infinity();
    1175        1218 :     GEN p1 = mkpath(oo, mkvecsmall2(0,1)); /* [oo, 0] */
    1176        1218 :     GEN p2 = mkpath(mkvecsmall2(1,1), oo); /* [1, oo] */
    1177        1218 :     insert_E(p1, S, p1N);
    1178        1218 :     insert_E(p2, S, p1N);
    1179             :   }
    1180             : 
    1181       60718 :   for (r = 1; r < nbgen; r++)
    1182             :   {
    1183       59500 :     GEN c1 = gel(cusp_list,r);
    1184    14195188 :     for (s = r+1; s <= nbgen; s++)
    1185             :     {
    1186    14135688 :       pari_sp av = avma;
    1187    14135688 :       GEN c2 = gel(cusp_list,s), path;
    1188    14135688 :       GEN d = subii(mulss(c1[1],c2[2]), mulss(c1[2],c2[1]));
    1189    14135688 :       avma = av;
    1190    14135688 :       if (!is_pm1(d)) continue;
    1191             : 
    1192      117782 :       path = mkpath(c1,c2);
    1193      117782 :       if (r+1 == s)
    1194             :       {
    1195       59500 :         GEN w = path;
    1196       59500 :         ulong hash = T31->hash(w); /* T31, T32 use the same hash function */
    1197       59500 :         if (!hash_search2(T31, w, hash) && !hash_search2(T32, w, hash))
    1198             :         {
    1199       59087 :           if (gamma_equiv(path, vecreverse(path), N))
    1200         623 :             set_insert(T2, path);
    1201             :           else
    1202       58464 :             insert_E(path, S, p1N);
    1203             :         }
    1204             :       } else {
    1205       58282 :         set_insert(F, mkvec2(path, mkvecsmall2(r,s)));
    1206       58282 :         set_insert(F, mkvec2(vecreverse(path), mkvecsmall2(s,r)));
    1207             :       }
    1208             :     }
    1209             :   }
    1210        1218 :   setlg(S->E2_in_terms_of_E1, E2->nb+1);
    1211        1218 : }
    1212             : 
    1213             : /* v = \sum n_i g_i, g_i in Sl(2,Z), return \sum n_i g_i^(-1) */
    1214             : static GEN
    1215      742483 : ZSl2_star(GEN v)
    1216             : {
    1217             :   long i, l;
    1218             :   GEN w, G;
    1219      742483 :   if (typ(v) == t_INT) return v;
    1220      742483 :   G = gel(v,1);
    1221      742483 :   w = cgetg_copy(G, &l);
    1222     1761081 :   for (i = 1; i < l; i++)
    1223             :   {
    1224     1018598 :     GEN g = gel(G,i);
    1225     1018598 :     if (typ(g) == t_MAT) g = SL2_inv(g);
    1226     1018598 :     gel(w,i) = g;
    1227             :   }
    1228      742483 :   return ZG_normalize(mkmat2(w, gel(v,2)));
    1229             : }
    1230             : static void
    1231      156968 : ZSl2C_star_inplace(GEN v)
    1232             : {
    1233      156968 :   long i, l = lg(v);
    1234      156968 :   for (i = 1; i < l; i++) gel(v,i) = ZSl2_star(gel(v,i));
    1235      156968 : }
    1236             : 
    1237             : /* Input: h = set of unimodular paths, p1N = P^1(Z/NZ) = Gamma_0(N)\PSL2(Z)
    1238             :  * Output: Each path is converted to a matrix and then an element of P^1(Z/NZ)
    1239             :  * Append the matrix to W[12], append the index that represents
    1240             :  * these elements of P^1 (the classes mod Gamma_0(N) via our fixed
    1241             :  * enumeration to W[2]. */
    1242             : static void
    1243        7308 : paths_decompose(GEN W, hashtable *h, int flag)
    1244             : {
    1245        7308 :   GEN p1N = ms_get_p1N(W), section = ms_get_section(W);
    1246        7308 :   GEN v = hash_to_vec(h);
    1247        7308 :   long i, l = lg(v);
    1248      186221 :   for (i = 1; i < l; i++)
    1249             :   {
    1250      178913 :     GEN e = gel(v,i);
    1251      178913 :     GEN M = path_to_zm(flag? gel(e,1): e);
    1252      178913 :     long index = p1_index(cc(M), dd(M), p1N);
    1253      178913 :     vecsmalltrunc_append(gel(W,2), index);
    1254      178913 :     gel(section, index) = M;
    1255             :   }
    1256        7308 : }
    1257             : static void
    1258        1218 : fill_W2_W12(GEN W, PS_sets_t *S)
    1259             : {
    1260        1218 :   GEN p1N = gel(W,1);
    1261        1218 :   long n = p1_size(p1N);
    1262        1218 :   gel(W, 2) = vecsmalltrunc_init(n+1);
    1263        1218 :   gel(W,12) = cgetg(n+1, t_VEC);
    1264             :   /* F contains [path, [index cusp1, index cusp2]]. Others contain paths only */
    1265        1218 :   paths_decompose(W, S->F, 1);
    1266        1218 :   paths_decompose(W, S->E2, 0);
    1267        1218 :   paths_decompose(W, S->T32, 0);
    1268        1218 :   paths_decompose(W, S->E1, 0);
    1269        1218 :   paths_decompose(W, S->T2, 0);
    1270        1218 :   paths_decompose(W, S->T31, 0);
    1271        1218 : }
    1272             : 
    1273             : /* x t_VECSMALL, corresponds to a map x(i) = j, where 1 <= j <= max for all i
    1274             :  * Return y s.t. y[j] = i or 0 (not in image) */
    1275             : static GEN
    1276        2436 : reverse_list(GEN x, long max)
    1277             : {
    1278        2436 :   GEN y = const_vecsmall(max, 0);
    1279        2436 :   long r, lx = lg(x);
    1280        2436 :   for (r = 1; r < lx; r++) y[ x[r] ] = r;
    1281        2436 :   return y;
    1282             : }
    1283             : 
    1284             : /* go from C[a] to C[b]; return the indices of paths
    1285             :  * E.g. if a < b
    1286             :  *   (C[a]->C[a+1], C[a+1]->C[a+2], ... C[b-1]->C[b])
    1287             :  * (else reverse direction)
    1288             :  * = b - a paths */
    1289             : static GEN
    1290      113876 : F_indices(GEN W, long a, long b)
    1291             : {
    1292      113876 :   GEN v = cgetg(labs(b-a) + 1, t_VEC);
    1293      113876 :   long s, k = 1;
    1294      113876 :   if (a < b) {
    1295       56938 :     GEN index_forward = gel(W,13);
    1296       56938 :     for (s = a; s < b; s++) gel(v,k++) = gel(index_forward,s);
    1297             :   } else {
    1298       56938 :     GEN index_backward = gel(W,14);
    1299       56938 :     for (s = a; s > b; s--) gel(v,k++) = gel(index_backward,s);
    1300             :   }
    1301      113876 :   return v;
    1302             : }
    1303             : /* go from C[a] to C[b] via oo; return the indices of paths
    1304             :  * E.g. if a < b
    1305             :  *   (C[a]->C[a-1], ... C[2]->C[1],
    1306             :  *    C[1]->oo, oo-> C[end],
    1307             :  *    C[end]->C[end-1], ... C[b+1]->C[b])
    1308             :  *  a-1 + 2 + end-(b+1)+1 = end - b + a + 1 paths  */
    1309             : static GEN
    1310        2688 : F_indices_oo(GEN W, long end, long a, long b)
    1311             : {
    1312        2688 :   GEN index_oo = gel(W,15);
    1313        2688 :   GEN v = cgetg(end-labs(b-a)+1 + 1, t_VEC);
    1314        2688 :   long s, k = 1;
    1315             : 
    1316        2688 :   if (a < b) {
    1317        1344 :     GEN index_backward = gel(W,14);
    1318        1344 :     for (s = a; s > 1; s--) gel(v,k++) = gel(index_backward,s);
    1319        1344 :     gel(v,k++) = gel(index_backward,1); /* C[1] -> oo */
    1320        1344 :     gel(v,k++) = gel(index_oo,2); /* oo -> C[end] */
    1321        1344 :     for (s = end; s > b; s--) gel(v,k++) = gel(index_backward,s);
    1322             :   } else {
    1323        1344 :     GEN index_forward = gel(W,13);
    1324        1344 :     for (s = a; s < end; s++) gel(v,k++) = gel(index_forward,s);
    1325        1344 :     gel(v,k++) = gel(index_forward,end); /* C[end] -> oo */
    1326        1344 :     gel(v,k++) = gel(index_oo,1); /* oo -> C[1] */
    1327        1344 :     for (s = 1; s < b; s++) gel(v,k++) = gel(index_forward,s);
    1328             :   }
    1329        2688 :   return v;
    1330             : }
    1331             : /* index of oo -> C[1], oo -> C[end] */
    1332             : static GEN
    1333        1218 : indices_oo(GEN W, GEN C)
    1334             : {
    1335        1218 :   long end = lg(C)-1;
    1336        1218 :   GEN w, v = cgetg(2+1, t_VEC), oo = cusp_infinity();
    1337        1218 :   w = mkpath(oo, gel(C,1)); /* oo -> C[1]=0 */
    1338        1218 :   gel(v,1) = path_Gamma0N_decompose(W, w);
    1339        1218 :   w = mkpath(oo, gel(C,end)); /* oo -> C[end]=1 */
    1340        1218 :   gel(v,2) = path_Gamma0N_decompose(W, w);
    1341        1218 :   return v;
    1342             : }
    1343             : 
    1344             : /* index of C[1]->C[2], C[2]->C[3], ... C[end-1]->C[end], C[end]->oo
    1345             :  * Recall that C[1] = 0, C[end] = 1 */
    1346             : static GEN
    1347        1218 : indices_forward(GEN W, GEN C)
    1348             : {
    1349        1218 :   long s, k = 1, end = lg(C)-1;
    1350        1218 :   GEN v = cgetg(end+1, t_VEC);
    1351       61936 :   for (s = 1; s <= end; s++)
    1352             :   {
    1353       60718 :     GEN w = mkpath(gel(C,s), s == end? cusp_infinity(): gel(C,s+1));
    1354       60718 :     gel(v,k++) = path_Gamma0N_decompose(W, w);
    1355             :   }
    1356        1218 :   return v;
    1357             : }
    1358             : /* index of C[1]->oo, C[2]->C[1], ... C[end]->C[end-1] */
    1359             : static GEN
    1360        1218 : indices_backward(GEN W, GEN C)
    1361             : {
    1362        1218 :   long s, k = 1, end = lg(C)-1;
    1363        1218 :   GEN v = cgetg(end+1, t_VEC);
    1364       61936 :   for (s = 1; s <= end; s++)
    1365             :   {
    1366       60718 :     GEN w = mkpath(gel(C,s), s == 1? cusp_infinity(): gel(C,s-1));
    1367       60718 :     gel(v,k++) = path_Gamma0N_decompose(W, w);
    1368             :   }
    1369        1218 :   return v;
    1370             : }
    1371             : 
    1372             : /* N = integer > 1. Returns data describing Delta_0 = Z[P^1(Q)]_0 seen as
    1373             :  * a Gamma_0(N) - module. */
    1374             : static GEN
    1375        1218 : msinit_N(ulong N)
    1376             : {
    1377        1218 :   GEN p1N = create_p1mod(N);
    1378             :   GEN C, vecF, vecT2, vecT31;
    1379             :   ulong r, s, width;
    1380        1218 :   long nball, nbgen, nbp1N = p1_size(p1N);
    1381        1218 :   GEN TAU = mkmat22(gen_0,gen_m1, gen_1,gen_m1); /*[0,-1;1,-1]*/
    1382             :   GEN W, W2, singlerel, annT2, annT31;
    1383             :   GEN F_index;
    1384             :   hashtable *F, *T2, *T31, *T32, *E1, *E2;
    1385             :   PS_sets_t S;
    1386             : 
    1387        1218 :   form_E_F_T(N,p1N, &C, &S);
    1388        1218 :   E1  = S.E1;
    1389        1218 :   E2  = S.E2;
    1390        1218 :   T31 = S.T31;
    1391        1218 :   T32 = S.T32;
    1392        1218 :   F   = S.F;
    1393        1218 :   T2  = S.T2;
    1394        1218 :   nbgen = lg(C)-1;
    1395             : 
    1396        1218 :   W = cgetg(17, t_VEC);
    1397        1218 :   gel(W,1) = p1N;
    1398             : 
    1399             :  /* Put our paths in the order: F,E2,T32,E1,T2,T31
    1400             :   * W2[j] associates to the j-th element of this list its index in P1. */
    1401        1218 :   fill_W2_W12(W, &S);
    1402        1218 :   W2 = gel(W, 2);
    1403        1218 :   nball = lg(W2)-1;
    1404        1218 :   gel(W,3) = reverse_list(W2, nbp1N);
    1405             : 
    1406        1218 :   gel(W,5) = vecslice(gel(W,2), F->nb + E2->nb + T32->nb + 1, nball);
    1407        1218 :   gel(W,4) = reverse_list(gel(W,5), nbp1N);
    1408        1218 :   gel(W,13) = indices_forward(W, C);
    1409        1218 :   gel(W,14) = indices_backward(W, C);
    1410        1218 :   gel(W,15) = indices_oo(W, C);
    1411        6090 :   gel(W,11) = mkvecsmall5(F->nb,
    1412        1218 :                           F->nb + E2->nb,
    1413        1218 :                           F->nb + E2->nb + T32->nb,
    1414        1218 :                           F->nb + E2->nb + T32->nb + E1->nb,
    1415        1218 :                           F->nb + E2->nb + T32->nb + E1->nb + T2->nb);
    1416             : 
    1417             :   /* relations between T32 and T31 [not stored!]
    1418             :    * T32[i] = - T31[i] */
    1419             : 
    1420             :   /* relations of F */
    1421        1218 :   width = E1->nb + T2->nb + T31->nb;
    1422             :   /* F_index[r] = [index_1, ..., index_k], where index_i is the p1_index()
    1423             :    * of the elementary unimodular path between 2 consecutive cusps
    1424             :    * [in E1,E2,T2,T31 or T32] */
    1425        1218 :   F_index = cgetg(F->nb+1, t_VEC);
    1426        1218 :   vecF = hash_to_vec(F);
    1427      117782 :   for (r = 1; r <= F->nb; r++)
    1428             :   {
    1429      116564 :     GEN w = gel(gel(vecF,r), 2);
    1430      116564 :     long a = w[1], b = w[2], d = labs(b - a);
    1431             :     /* c1 = cusp_list[a],  c2 = cusp_list[b], ci != oo */
    1432      233128 :     gel(F_index,r) = (nbgen-d >= d-1)? F_indices(W, a,b)
    1433      116564 :                                      : F_indices_oo(W, lg(C)-1,a,b);
    1434             :   }
    1435             : 
    1436        1218 :   singlerel = cgetg(width+1, t_VEC);
    1437             :   /* form the single boundary relation */
    1438       31668 :   for (s = 1; s <= E2->nb; s++)
    1439             :   {
    1440       30450 :     GEN data = gel(S.E2_in_terms_of_E1,s);
    1441       30450 :     long c = itos(gel(data,1));
    1442       30450 :     GEN u = gel(data,2); /* E2[s] = u * E1[c], u = - [gamma] */
    1443       30450 :     GEN gamma = gcoeff(u,1,1);
    1444       30450 :     gel(singlerel, c) = mkmat22(gen_1,gen_1, gamma,gen_m1);
    1445             :   }
    1446        1218 :   for (r = E1->nb + 1; r <= width; r++) gel(singlerel, r) = gen_1;
    1447             : 
    1448             :   /* form the 2-torsion relations */
    1449        1218 :   annT2 = cgetg(T2->nb+1, t_VEC);
    1450        1218 :   vecT2 = hash_to_vec(T2);
    1451        1841 :   for (r = 1; r <= T2->nb; r++)
    1452             :   {
    1453         623 :     GEN w = gel(vecT2,r);
    1454         623 :     GEN gamma = gamma_equiv_matrix(vecreverse(w), w);
    1455         623 :     gel(annT2, r) = mkmat22(gen_1,gen_1, gamma,gen_1);
    1456             :   }
    1457             : 
    1458             :   /* form the 3-torsion relations */
    1459        1218 :   annT31 = cgetg(T31->nb+1, t_VEC);
    1460        1218 :   vecT31 = hash_to_vec(T31);
    1461        1631 :   for (r = 1; r <= T31->nb; r++)
    1462             :   {
    1463         413 :     GEN M = zm_to_ZM( path_to_zm( vecreverse(gel(vecT31,r)) ) );
    1464         413 :     GEN gamma = ZM_mul(ZM_mul(M, TAU), SL2_inv(M));
    1465         413 :     gel(annT31, r) = mkmat2(mkcol3(gen_1,gamma,ZM_sqr(gamma)),
    1466             :                             mkcol3(gen_1,gen_1,gen_1));
    1467             :   }
    1468        1218 :   gel(W,6) = F_index;
    1469        1218 :   gel(W,7) = S.E2_in_terms_of_E1;
    1470        1218 :   gel(W,8) = annT2;
    1471        1218 :   gel(W,9) = annT31;
    1472        1218 :   gel(W,10)= singlerel;
    1473        1218 :   gel(W,16)= inithashcusps(p1N);
    1474        1218 :   return W;
    1475             : }
    1476             : static GEN
    1477          98 : cusp_to_P1Q(GEN c) { return c[2]? gdivgs(stoi(c[1]), c[2]): mkoo(); }
    1478             : GEN
    1479          14 : mspathgens(GEN W)
    1480             : {
    1481          14 :   pari_sp av = avma;
    1482             :   long i,j, l, nbE1, nbT2, nbT31;
    1483             :   GEN R, r, g, section, gen, annT2, annT31, singlerel;
    1484          14 :   checkms(W); W = get_ms(W);
    1485          14 :   section = ms_get_section(W);
    1486          14 :   gen = ms_get_genindex(W);
    1487          14 :   l = lg(gen);
    1488          14 :   g = cgetg(l,t_VEC);
    1489          63 :   for (i=1; i<l; i++)
    1490             :   {
    1491          49 :     GEN p = gel(section,gen[i]);
    1492          49 :     gel(g,i) = mkvec2(cusp_to_P1Q(gel(p,1)), cusp_to_P1Q(gel(p,2)));
    1493             :   }
    1494          14 :   nbE1 = ms_get_nbE1(W);
    1495          14 :   annT2 = gel(W,8); nbT2 = lg(annT2)-1;
    1496          14 :   annT31 = gel(W,9);nbT31 = lg(annT31)-1;
    1497          14 :   singlerel = gel(W,10);
    1498          14 :   R = cgetg(nbT2+nbT31+2, t_VEC);
    1499          14 :   l = lg(singlerel);
    1500          14 :   r = cgetg(l, t_VEC);
    1501          42 :   for (i = 1; i <= nbE1; i++)
    1502          28 :     gel(r,i) = mkvec2(gel(singlerel, i), stoi(i));
    1503          35 :   for (; i < l; i++)
    1504          21 :     gel(r,i) = mkvec2(gen_1, stoi(i));
    1505          14 :   gel(R,1) = r; j = 2;
    1506          35 :   for (i = 1; i <= nbT2; i++,j++)
    1507          21 :     gel(R,j) = mkvec( mkvec2(gel(annT2,i), stoi(i + nbE1)) );
    1508          14 :   for (i = 1; i <= nbT31; i++,j++)
    1509           0 :     gel(R,j) = mkvec( mkvec2(gel(annT31,i), stoi(i + nbE1 + nbT2)) );
    1510          14 :   return gerepilecopy(av, mkvec2(g,R));
    1511             : }
    1512             : 
    1513             : /* Modular symbols in weight k: Hom_Gamma(Delta, Q[x,y]_{k-2}) */
    1514             : /* A symbol phi is represented by the {phi(g_i)}, {phi(g'_i)}, {phi(g''_i)}
    1515             :  * where the {g_i, g'_i, g''_i} are the Z[\Gamma]-generators of Delta,
    1516             :  * g_i corresponds to E1, g'_i to T2, g''_i to T31.
    1517             :  */
    1518             : 
    1519             : /* FIXME: export. T^1, ..., T^n */
    1520             : static GEN
    1521      510706 : RgX_powers(GEN T, long n)
    1522             : {
    1523      510706 :   GEN v = cgetg(n+1, t_VEC);
    1524             :   long i;
    1525      510706 :   gel(v, 1) = T;
    1526      510706 :   for (i = 1; i < n; i++) gel(v,i+1) = RgX_mul(gel(v,i), T);
    1527      510706 :   return v;
    1528             : }
    1529             : 
    1530             : /* g = [a,b;c,d]. Return (X^{k-2} | g)(X,Y)[X = 1]. */
    1531             : static GEN
    1532        2576 : voo_act_Gl2Q(GEN g, long k)
    1533             : {
    1534        2576 :   GEN c = gcoeff(g,2,1), d = gcoeff(g,2,2);
    1535        2576 :   return RgX_to_RgC(gpowgs(deg1pol_shallow(gneg(c), d, 0), k-2), k-1);
    1536             : }
    1537             : 
    1538             : struct m_act {
    1539             :   long dim, k, p;
    1540             :   GEN q;
    1541             : };
    1542             : 
    1543             : /* g = [a,b;c,d]. Return (P | g)(X,Y)[X = 1] = P(dX - cY, -b X + aY)[X = 1],
    1544             :  * for P = X^{k-2}, X_^{k-3}Y, ..., Y^{k-2} */
    1545             : GEN
    1546      255353 : RgX_act_Gl2Q(GEN g, long k)
    1547             : {
    1548             :   GEN a,b,c,d, V1,V2,V;
    1549             :   long i;
    1550      255353 :   if (k == 2) return matid(1);
    1551      255353 :   a = gcoeff(g,1,1); b = gcoeff(g,1,2);
    1552      255353 :   c = gcoeff(g,2,1); d = gcoeff(g,2,2);
    1553      255353 :   V1 = RgX_powers(deg1pol_shallow(gneg(c), d, 0), k-2); /* d - c Y */
    1554      255353 :   V2 = RgX_powers(deg1pol_shallow(a, gneg(b), 0), k-2); /*-b + a Y */
    1555      255353 :   V = cgetg(k, t_MAT);
    1556      255353 :   gel(V,1)   = RgX_to_RgC(gel(V1, k-2), k-1);
    1557      617722 :   for (i = 1; i < k-2; i++)
    1558             :   {
    1559      362369 :     GEN v1 = gel(V1, k-2-i); /* (d-cY)^(k-2-i) */
    1560      362369 :     GEN v2 = gel(V2, i); /* (-b+aY)^i */
    1561      362369 :     gel(V,i+1) = RgX_to_RgC(RgX_mul(v1,v2), k-1);
    1562             :   }
    1563      255353 :   gel(V,k-1) = RgX_to_RgC(gel(V2, k-2), k-1);
    1564      255353 :   return V; /* V[i+1] = X^i | g */
    1565             : }
    1566             : /* z in Z[Gl2(Q)], return the matrix of z acting on V */
    1567             : static GEN
    1568      521409 : act_ZGl2Q(GEN z, struct m_act *T, GEN(*act)(struct m_act*,GEN), hashtable *H)
    1569             : {
    1570      521409 :   GEN S = NULL, G, E;
    1571             :   pari_sp av;
    1572             :   long l, j;
    1573             :   /* paranoia: should'n t occur */
    1574      521409 :   if (typ(z) == t_INT) return scalarmat_shallow(z, T->dim);
    1575      521409 :   G = gel(z,1); l = lg(G);
    1576      521409 :   E = gel(z,2);
    1577      521409 :   if (H)
    1578             :   { /* First pass, identify matrices in Sl_2 to convert to operators;
    1579             :      * insert operators in hashtable. This allows GC in 2nd pass */
    1580     1528905 :     for (j = 1; j < l; j++)
    1581             :     {
    1582     1011129 :       GEN g = gel(G,j);
    1583     1011129 :       if (typ(g) != t_INT)
    1584             :       {
    1585     1011129 :         ulong hash = H->hash(g);
    1586     1011129 :         hashentry *e = hash_search2(H,g,hash);
    1587     1011129 :         if (!e) hash_insert2(H,g,act(T,g),hash);
    1588             :       }
    1589             :     }
    1590             :   }
    1591      521409 :   av = avma;
    1592     1540007 :   for (j = 1; j < l; j++)
    1593             :   {
    1594     1018598 :     GEN M, g = gel(G,j), n = gel(E,j);
    1595     1018598 :     if (typ(g) == t_INT) /* = 1 */
    1596        3591 :       M = n; /* n*Id_dim */
    1597             :     else
    1598             :     {
    1599     1015007 :       if (H)
    1600     1011129 :         M = (GEN)hash_search(H,g)->val; /*search succeeds because of 1st pass*/
    1601             :       else
    1602        3878 :         M = act(T,g);
    1603     1015007 :       if (is_pm1(n))
    1604     1009337 :       { if (signe(n) < 0) M = RgM_neg(M); }
    1605             :       else
    1606        5670 :         M = RgM_Rg_mul(M, n);
    1607             :     }
    1608     1018598 :     if (!S) { S = M; continue; }
    1609      497189 :     S = gadd(S, M);
    1610      497189 :     if (gc_needed(av,1))
    1611             :     {
    1612           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"act_ZGl2Q, j = %ld",j);
    1613           0 :       S = gerepileupto(av, S);
    1614             :     }
    1615             :   }
    1616      521409 :   return gerepilecopy(av, S);
    1617             : }
    1618             : static GEN
    1619      255353 : _RgX_act_Gl2Q(struct m_act *S, GEN z) { return RgX_act_Gl2Q(z, S->k); }
    1620             : /* acting on (X^{k-2},...,Y^{k-2}) */
    1621             : GEN
    1622        3619 : RgX_act_ZGl2Q(GEN z, long k)
    1623             : {
    1624             :   struct m_act T;
    1625        3619 :   T.k = k;
    1626        3619 :   T.dim = k-1;
    1627        3619 :   return act_ZGl2Q(z, &T, _RgX_act_Gl2Q, NULL);
    1628             : }
    1629             : 
    1630             : /* Given a sparse vector of elements in Z[G], convert it to a (sparse) vector
    1631             :  * of operators on V (given by t_MAT) */
    1632             : static void
    1633       39116 : ZGl2QC_to_act(struct m_act *S, GEN(*act)(struct m_act*,GEN), GEN v, hashtable *H)
    1634             : {
    1635       39116 :   GEN val = gel(v,2);
    1636       39116 :   long i, l = lg(val);
    1637       39116 :   for (i = 1; i < l; i++) gel(val,i) = act_ZGl2Q(gel(val,i), S, act, H);
    1638       39116 : }
    1639             : 
    1640             : /* For all V[i] in Z[\Gamma], find the P such that  P . V[i]^* = 0;
    1641             :  * write P in basis X^{k-2}, ..., Y^{k-2} */
    1642             : static GEN
    1643        1106 : ZGV_tors(GEN V, long k)
    1644             : {
    1645        1106 :   long i, l = lg(V);
    1646        1106 :   GEN v = cgetg(l, t_VEC);
    1647        1554 :   for (i = 1; i < l; i++)
    1648             :   {
    1649         448 :     GEN a = ZSl2_star(gel(V,i));
    1650         448 :     gel(v,i) = ZM_ker(RgX_act_ZGl2Q(a,k));
    1651             :   }
    1652        1106 :   return v;
    1653             : }
    1654             : 
    1655             : static long
    1656     6560057 : set_from_index(GEN W11, long i)
    1657             : {
    1658     6560057 :   if (i <= W11[1]) return 1;
    1659     5689432 :   if (i <= W11[2]) return 2;
    1660     3007557 :   if (i <= W11[3]) return 3;
    1661     3002762 :   if (i <= W11[4]) return 4;
    1662       21203 :   if (i <= W11[5]) return 5;
    1663        4550 :   return 6;
    1664             : }
    1665             : 
    1666             : /* det M = 1 */
    1667             : static void
    1668     1401323 : treat_index(GEN W, GEN M, long index, GEN v)
    1669             : {
    1670     1401323 :   GEN W11 = gel(W,11);
    1671     1401323 :   long shift = W11[3]; /* #F + #E2 + T32 */
    1672     1401323 :   switch(set_from_index(W11, index))
    1673             :   {
    1674             :     case 1: /*F*/
    1675             :     {
    1676      230349 :       GEN F_index = gel(W,6), ind = gel(F_index, index);
    1677      230349 :       long j, l = lg(ind);
    1678     1184974 :       for (j = 1; j < l; j++)
    1679             :       {
    1680      954625 :         GEN IND = gel(ind,j), M0 = gel(IND,2);
    1681      954625 :         long index = mael(IND,1,1);
    1682      954625 :         treat_index(W, ZM_mul(M,M0), index, v);
    1683             :       }
    1684      230349 :       break;
    1685             :     }
    1686             : 
    1687             :     case 2: /*E2, E2[r] + gamma * E1[s] = 0 */
    1688             :     {
    1689      532420 :       long r = index - W11[1];
    1690      532420 :       GEN E2_in_terms_of_E1= gel(W,7), z = gel(E2_in_terms_of_E1, r);
    1691      532420 :       long s = itou(gel(z,1));
    1692             : 
    1693      532420 :       index = s;
    1694      532420 :       M = G_ZG_mul(M, gel(z,2)); /* M * (-gamma) */
    1695      532420 :       gel(v, index) = ZG_add(gel(v, index), M);
    1696      532420 :       break;
    1697             :     }
    1698             : 
    1699             :     case 3: /*T32, T32[i] = -T31[i] */
    1700             :     {
    1701        3675 :       long T3shift = W11[5] - W11[2]; /* #T32 + #E1 + #T2 */
    1702        3675 :       index += T3shift;
    1703        3675 :       index -= shift;
    1704        3675 :       gel(v, index) = ZG_add(gel(v, index), to_famat_shallow(M,gen_m1));
    1705        3675 :       break;
    1706             :     }
    1707             :     default: /*E1,T2,T31*/
    1708      634879 :       index -= shift;
    1709      634879 :       gel(v, index) = ZG_add(gel(v, index), to_famat_shallow(M,gen_1));
    1710      634879 :       break;
    1711             :   }
    1712     1401323 : }
    1713             : static void
    1714     5158734 : treat_index_trivial(GEN v, GEN W, long index)
    1715             : {
    1716     5158734 :   GEN W11 = gel(W,11);
    1717     5158734 :   long shift = W11[3]; /* #F + #E2 + T32 */
    1718     5158734 :   switch(set_from_index(W11, index))
    1719             :   {
    1720             :     case 1: /*F*/
    1721             :     {
    1722      640276 :       GEN F_index = gel(W,6), ind = gel(F_index, index);
    1723      640276 :       long j, l = lg(ind);
    1724     4783324 :       for (j = 1; j < l; j++)
    1725             :       {
    1726     4143048 :         GEN IND = gel(ind,j);
    1727     4143048 :         treat_index_trivial(v, W, mael(IND,1,1));
    1728             :       }
    1729      640276 :       break;
    1730             :     }
    1731             : 
    1732             :     case 2: /*E2, E2[r] + gamma * E1[s] = 0 */
    1733             :     {
    1734     2149455 :       long r = index - W11[1];
    1735     2149455 :       GEN E2_in_terms_of_E1 = gel(W,7), z = gel(E2_in_terms_of_E1, r);
    1736     2149455 :       long s = itou(gel(z,1));
    1737     2149455 :       v[s]--;
    1738     2149455 :       break;
    1739             :     }
    1740             : 
    1741             :     case 3: case 5: case 6: /*T32,T2,T31*/
    1742        9989 :       break;
    1743             : 
    1744             :     case 4: /*E1*/
    1745     2359014 :       v[index-shift]++;
    1746     2359014 :       break;
    1747             :   }
    1748     5158734 : }
    1749             : 
    1750             : static GEN
    1751      157052 : M2_log(GEN W, GEN M)
    1752             : {
    1753      157052 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    1754      157052 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    1755             :   GEN  u, v, D, V;
    1756             :   long index, s;
    1757             : 
    1758      157052 :   W = get_ms(W);
    1759      157052 :   V = zerovec(ms_get_nbgen(W));
    1760             : 
    1761      157052 :   D = subii(mulii(a,d), mulii(b,c));
    1762      157052 :   s = signe(D);
    1763      157052 :   if (!s) return V;
    1764      157045 :   if (is_pm1(D))
    1765             :   { /* shortcut, no need to apply Manin's trick */
    1766       55314 :     if (s < 0) {
    1767        3612 :       b = negi(b);
    1768        3612 :       d = negi(d);
    1769             :     }
    1770       55314 :     M = Gamma0N_decompose(W, mkmat22(a,b, c,d), &index);
    1771       55314 :     treat_index(W, M, index, V);
    1772             :   }
    1773             :   else
    1774             :   {
    1775             :     GEN U, B, P, Q, PQ, C1,C2;
    1776             :     long i, l;
    1777      101731 :     (void)bezout(a,c,&u,&v);
    1778      101731 :     B = addii(mulii(b,u), mulii(d,v));
    1779             :     /* [u,v;-c,a] [a,b; c,d] = [1,B; 0,D], i.e. M = U [1,B;0,D] */
    1780      101731 :     U = mkmat22(a,negi(v), c,u);
    1781             : 
    1782             :     /* {1/0 -> B/D} as \sum g_i, g_i unimodular paths */
    1783      101731 :     PQ = ZV_allpnqn( gboundcf(gdiv(B,D), 0) );
    1784      101731 :     P = gel(PQ,1); l = lg(P);
    1785      101731 :     Q = gel(PQ,2);
    1786      101731 :     C1 = gel(U,1);
    1787      493115 :     for (i = 1; i < l; i++, C1 = C2)
    1788             :     {
    1789             :       GEN M;
    1790      391384 :       C2 = ZM_ZC_mul(U, mkcol2(gel(P,i), gel(Q,i)));
    1791      391384 :       if (!odd(i)) C1 = ZC_neg(C1);
    1792      391384 :       M = Gamma0N_decompose(W, mkmat2(C1,C2), &index);
    1793      391384 :       treat_index(W, M, index, V);
    1794             :     }
    1795             :   }
    1796      157045 :   return V;
    1797             : }
    1798             : 
    1799             : /* express +oo->q=a/b in terms of the Z[G]-generators, trivial action */
    1800             : static void
    1801        6552 : Q_log_trivial(GEN v, GEN W, GEN q)
    1802             : {
    1803        6552 :   GEN Q, W3 = gel(W,3), p1N = gel(W,1);
    1804        6552 :   ulong c,d, N = p1N_get_N(p1N);
    1805             :   long i, lx;
    1806             : 
    1807        6552 :   Q = Q_log_init(N, q);
    1808        6552 :   lx = lg(Q);
    1809        6552 :   c = 0;
    1810       28350 :   for (i = 1; i < lx; i++, c = d)
    1811             :   {
    1812             :     long index;
    1813       21798 :     d = Q[i];
    1814       21798 :     if (c && !odd(i)) c = N - c;
    1815       21798 :     index = W3[ p1_index(c,d,p1N) ];
    1816       21798 :     treat_index_trivial(v, W, index);
    1817             :   }
    1818        6552 : }
    1819             : static void
    1820      436282 : M2_log_trivial(GEN V, GEN W, GEN M)
    1821             : {
    1822      436282 :   GEN p1N = gel(W,1), W3 = gel(W,3);
    1823      436282 :   ulong N = p1N_get_N(p1N);
    1824      436282 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    1825      436282 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    1826             :   GEN  u, v, D;
    1827             :   long index, s;
    1828             : 
    1829      436282 :   D = subii(mulii(a,d), mulii(b,c));
    1830      436282 :   s = signe(D);
    1831      441189 :   if (!s) return;
    1832      436282 :   if (is_pm1(D))
    1833             :   { /* shortcut, not need to apply Manin's trick */
    1834      180243 :     if (s < 0) d = negi(d);
    1835      180243 :     index = W3[ p1_index(umodiu(c,N),umodiu(d,N),p1N) ];
    1836      180243 :     treat_index_trivial(V, W, index);
    1837             :   }
    1838             :   else
    1839             :   {
    1840             :     GEN U, B, P, Q, PQ;
    1841             :     long i, l;
    1842      256039 :     if (!signe(c)) { Q_log_trivial(V,W,gdiv(b,d)); return; }
    1843      251132 :     (void)bezout(a,c,&u,&v);
    1844      251132 :     B = addii(mulii(b,u), mulii(d,v));
    1845             :     /* [u,v;-c,a] [a,b; c,d] = [1,B; 0,D], i.e. M = U [1,B;0,D] */
    1846      251132 :     U = mkvec2(c, u);
    1847             : 
    1848             :     /* {1/0 -> B/D} as \sum g_i, g_i unimodular paths */
    1849      251132 :     PQ = ZV_allpnqn( gboundcf(gdiv(B,D), 0) );
    1850      251132 :     P = gel(PQ,1); l = lg(P);
    1851      251132 :     Q = gel(PQ,2);
    1852     1064777 :     for (i = 1; i < l; i++, c = d)
    1853             :     {
    1854      813645 :       d = addii(mulii(gel(U,1),gel(P,i)), mulii(gel(U,2),gel(Q,i)));
    1855      813645 :       if (!odd(i)) c = negi(c);
    1856      813645 :       index = W3[ p1_index(umodiu(c,N),umodiu(d,N),p1N) ];
    1857      813645 :       treat_index_trivial(V, W, index);
    1858             :     }
    1859             :   }
    1860             : }
    1861             : 
    1862             : static GEN
    1863         238 : cusp_to_ZC(GEN c)
    1864             : {
    1865         238 :   switch(typ(c))
    1866             :   {
    1867             :     case t_INFINITY:
    1868          28 :       return mkcol2(gen_1,gen_0);
    1869             :     case t_INT:
    1870          84 :       return mkcol2(c,gen_1);
    1871             :     case t_FRAC:
    1872         126 :       return mkcol2(gel(c,1),gel(c,2));
    1873             :     case t_VECSMALL:
    1874           0 :       return mkcol2(stoi(c[1]), stoi(c[2]));
    1875             :     default:
    1876           0 :       pari_err_TYPE("mspathlog",c);
    1877           0 :       return NULL;
    1878             :   }
    1879             : }
    1880             : static GEN
    1881         119 : path2_to_M2(GEN p)
    1882         119 : { return mkmat2(cusp_to_ZC(gel(p,1)), cusp_to_ZC(gel(p,2))); }
    1883             : static GEN
    1884         133 : path_to_M2(GEN p)
    1885             : {
    1886         133 :   if (lg(p) != 3) pari_err_TYPE("mspathlog",p);
    1887         126 :   switch(typ(p))
    1888             :   {
    1889             :     case t_MAT:
    1890           7 :       RgM_check_ZM(p,"mspathlog");
    1891           7 :       break;
    1892             :     case t_VEC:
    1893         119 :       p = path2_to_M2(p);
    1894         119 :       break;
    1895           0 :     default: pari_err_TYPE("mspathlog",p);
    1896             :   }
    1897         126 :   return p;
    1898             : }
    1899             : /* Expresses path p as \sum x_i g_i, where the g_i are our distinguished
    1900             :  * generators and x_i \in Z[\Gamma]. Returns [x_1,...,x_n] */
    1901             : GEN
    1902          98 : mspathlog(GEN W, GEN p)
    1903             : {
    1904          98 :   pari_sp av = avma;
    1905          98 :   checkms(W);
    1906          98 :   return gerepilecopy(av, M2_log(W, path_to_M2(p)));
    1907             : }
    1908             : 
    1909             : /** HECKE OPERATORS **/
    1910             : /* [a,b;c,d] * cusp */
    1911             : static GEN
    1912     1181376 : cusp_mul(long a, long b, long c, long d, GEN cusp)
    1913             : {
    1914     1181376 :   long x = cusp[1], y = cusp[2];
    1915     1181376 :   long A = a*x+b*y, B = c*x+d*y, u = cgcd(A,B);
    1916     1181376 :   if (u != 1) { A /= u; B /= u; }
    1917     1181376 :   return mkcol2s(A, B);
    1918             : }
    1919             : /* f in Gl2(Q), act on path (zm), return path_to_M2(f.path) */
    1920             : static GEN
    1921      590688 : Gl2Q_act_path(GEN f, GEN path)
    1922             : {
    1923      590688 :   long a = coeff(f,1,1), b = coeff(f,1,2);
    1924      590688 :   long c = coeff(f,2,1), d = coeff(f,2,2);
    1925      590688 :   GEN c1 = cusp_mul(a,b,c,d, gel(path,1));
    1926      590688 :   GEN c2 = cusp_mul(a,b,c,d, gel(path,2));
    1927      590688 :   return mkmat2(c1,c2);
    1928             : }
    1929             : 
    1930             : static GEN
    1931      127477 : init_act_trivial(GEN W) { return const_vecsmall(ms_get_nbE1(W), 0); }
    1932             : static GEN
    1933          35 : mspathlog_trivial(GEN W, GEN p)
    1934             : {
    1935             :   GEN v;
    1936          35 :   W = get_ms(W);
    1937          35 :   v = init_act_trivial(W);
    1938          35 :   M2_log_trivial(v, W, path_to_M2(p));
    1939          28 :   return v;
    1940             : }
    1941             : 
    1942             : /* map from W1=Hom(Delta_0(N1),Q) -> W2=Hom(Delta_0(N2),Q), weight 2,
    1943             :  * trivial action. v a Gl2_Q or a t_VEC of Gl2_Q (\sum v[i] in Z[Gl2(Q)]).
    1944             :  * Return the matrix attached to the action of v. */
    1945             : static GEN
    1946        2478 : getMorphism_trivial(GEN WW1, GEN WW2, GEN v)
    1947             : {
    1948        2478 :   GEN W1 = get_ms(WW1), W2 = get_ms(WW2);
    1949        2478 :   GEN section = ms_get_section(W2), gen = ms_get_genindex(W2);
    1950        2478 :   long j, lv, d2 = ms_get_nbE1(W2);
    1951        2478 :   GEN T = cgetg(d2+1, t_MAT);
    1952        2478 :   lv = lg(v);
    1953      128275 :   for (j = 1; j <= d2; j++)
    1954             :   {
    1955      125797 :     GEN w = gel(section, gen[j]);
    1956      125797 :     GEN t = init_act_trivial(W1);
    1957             :     long l;
    1958      125797 :     for (l = 1; l < lv; l++) M2_log_trivial(t, W1, Gl2Q_act_path(gel(v,l), w));
    1959      125797 :     gel(T,j) = t;
    1960             :   }
    1961        2478 :   return shallowtrans(zm_to_ZM(T));
    1962             : }
    1963             : 
    1964             : static GEN
    1965      156968 : RgV_sparse(GEN v, GEN *pind)
    1966             : {
    1967             :   long i, l, k;
    1968      156968 :   GEN w = cgetg_copy(v,&l), ind = cgetg(l, t_VECSMALL);
    1969    16914646 :   for (i = k = 1; i < l; i++)
    1970             :   {
    1971    16757678 :     GEN c = gel(v,i);
    1972    16757678 :     if (typ(c) == t_INT) continue;
    1973      738864 :     gel(w,k) = c; ind[k] = i; k++;
    1974             :   }
    1975      156968 :   setlg(w,k); setlg(ind,k);
    1976      156968 :   *pind = ind; return w;
    1977             : }
    1978             : 
    1979             : static hashtable *
    1980        3479 : Gl2act_cache(long dim) { return set_init(dim*10); }
    1981             : 
    1982             : /* f zm/ZM in Gl_2(Q), acts from the left on Delta, which is generated by
    1983             :  * (g_i) as Z[Gamma1]-module, and by (G_i) as Z[Gamma2]-module.
    1984             :  * We have f.G_j = \sum_i \lambda_{i,j} g_i,   \lambda_{i,j} in Z[Gamma1]
    1985             :  * For phi in Hom_Gamma1(D,V), g in D, phi | f is in Hom_Gamma2(D,V) and
    1986             :  *  (phi | f)(G_j) = phi(f.G_j) | f
    1987             :  *                 = phi( \sum_i \lambda_{i,j} g_i ) | f
    1988             :  *                 = \sum_i phi(g_i) | (\lambda_{i,j}^* f)
    1989             :  *                 = \sum_i phi(g_i) | \mu_{i,j}(f)
    1990             :  * More generally
    1991             :  *  (\sum_k (phi |v_k))(G_j) = \sum_i phi(g_i) | \Mu_{i,j}
    1992             :  * with \Mu_{i,j} = \sum_k \mu{i,j}(v_k)
    1993             :  * Return the \Mu_{i,j} matrix as vector of sparse columns of operators on V */
    1994             : static GEN
    1995        3031 : init_dual_act(GEN v, GEN W1, GEN W2, struct m_act *S,
    1996             :               GEN(*act)(struct m_act *,GEN))
    1997             : {
    1998        3031 :   GEN section = ms_get_section(W2), gen = ms_get_genindex(W2);
    1999             :   /* HACK: the actions we consider in dimension 1 are trivial and in
    2000             :    * characteristic != 2, 3 => torsion generators are 0
    2001             :    * [satisfy e.g. (1+gamma).g = 0 => \phi(g) | 1+gamma  = 0 => \phi(g) = 0 */
    2002        3031 :   long j, lv = lg(v), dim = S->dim == 1? ms_get_nbE1(W2): lg(gen)-1;
    2003        3031 :   GEN T = cgetg(dim+1, t_VEC);
    2004        3031 :   hashtable *H = Gl2act_cache(dim);
    2005             : 
    2006       39613 :   for (j = 1; j <= dim; j++)
    2007             :   {
    2008       36582 :     pari_sp av = avma;
    2009       36582 :     GEN w = gel(section, gen[j]); /* path_to_zm( E1/T2/T3 element ) */
    2010       36582 :     GEN t = NULL;
    2011             :     long k;
    2012      191016 :     for (k = 1; k < lv; k++)
    2013             :     {
    2014      154434 :       GEN ind, L, F, tk, f = gel(v,k);
    2015      154434 :       if (typ(gel(f,1)) == t_VECSMALL) F = zm_to_ZM(f);
    2016           0 :       else { F = f; f = ZM_to_zm(F); }
    2017             :       /* f zm = F ZM */
    2018      154434 :       L = M2_log(W1, Gl2Q_act_path(f,w)); /* L[i] = lambda_{i,j} */
    2019      154434 :       L = RgV_sparse(L,&ind);
    2020      154434 :       ZSl2C_star_inplace(L); /* L[i] = lambda_{i,j}^* */
    2021      154434 :       if (!ZM_isidentity(F)) ZGC_G_mul_inplace(L, F);
    2022      154434 :       tk = mkvec2(ind,L); /* L[i] = mu_{i,j}(v[k]) */
    2023      154434 :       t = t? ZGCs_add(t, tk): tk;
    2024             :     }
    2025       36582 :     gel(T,j) = gerepilecopy(av, t);
    2026             :   }
    2027        3031 :   for(j = 1; j <= dim; j++) ZGl2QC_to_act(S, act, gel(T,j), H);
    2028        3031 :   return T;
    2029             : }
    2030             : 
    2031             : /* modular symbol given by phi[j] = \phi(G_j)
    2032             :  * \sum L[i]*phi[i], L a sparse column of operators */
    2033             : static GEN
    2034      348124 : dense_act_col(GEN col, GEN phi)
    2035             : {
    2036      348124 :   GEN s = NULL, colind = gel(col,1), colval = gel(col,2);
    2037      348124 :   long i, l = lg(colind), lphi = lg(phi);
    2038     5472061 :   for (i = 1; i < l; i++)
    2039             :   {
    2040     5126023 :     long a = colind[i];
    2041             :     GEN t;
    2042     5126023 :     if (a >= lphi) break; /* happens if k=2: torsion generator t omitted */
    2043     5123937 :     t = gel(phi, a); /* phi(G_a) */
    2044     5123937 :     t = RgM_RgC_mul(gel(colval,i), t);
    2045     5123937 :     s = s? RgC_add(s, t): t;
    2046             :   }
    2047      348124 :   return s;
    2048             : }
    2049             : /* modular symbol given by \phi( G[ind[j]] ) = val[j]
    2050             :  * \sum L[i]*phi[i], L a sparse column of operators */
    2051             : static GEN
    2052      771176 : sparse_act_col(GEN col, GEN phi)
    2053             : {
    2054      771176 :   GEN s = NULL, colind = gel(col,1), colval = gel(col,2);
    2055      771176 :   GEN ind = gel(phi,2), val = gel(phi,3);
    2056      771176 :   long a, l = lg(ind);
    2057     3003833 :   for (a = 1; a < l; a++)
    2058             :   {
    2059     2232657 :     GEN t = gel(val, a); /* phi(G_i) */
    2060     2232657 :     long i = zv_search(colind, ind[a]);
    2061     2232657 :     if (!i) continue;
    2062      531804 :     t = RgM_RgC_mul(gel(colval,i), t);
    2063      531804 :     s = s? RgC_add(s, t): t;
    2064             :   }
    2065      771176 :   return s;
    2066             : }
    2067             : static int
    2068       67900 : phi_sparse(GEN phi) { return typ(gel(phi,1)) == t_VECSMALL; }
    2069             : /* phi in Hom_Gamma1(Delta, V), return the matrix whose colums are the
    2070             :  *   \sum_i phi(g_i) | \mu_{i,j} = (phi|f)(G_j),
    2071             :  * see init_dual_act. */
    2072             : static GEN
    2073       67900 : dual_act(long dimV, GEN act, GEN phi)
    2074             : {
    2075       67900 :   long l = lg(act), j;
    2076       67900 :   GEN v = cgetg(l, t_MAT);
    2077       67900 :   GEN (*ACT)(GEN,GEN) = phi_sparse(phi)? sparse_act_col: dense_act_col;
    2078     1184288 :   for (j = 1; j < l; j++)
    2079             :   {
    2080     1116388 :     pari_sp av = avma;
    2081     1116388 :     GEN s = ACT(gel(act,j), phi);
    2082     1116388 :     gel(v,j) = s? gerepileupto(av,s): zerocol(dimV);
    2083             :   }
    2084       67900 :   return v;
    2085             : }
    2086             : 
    2087             : /* \phi in Hom(Delta, V), \phi(G_k) = phi[k]. Write \phi as
    2088             :  *   \sum_{i,j} mu_{i,j} phi_{i,j}, mu_{i,j} in Q */
    2089             : static GEN
    2090       58373 : getMorphism_basis(GEN W, GEN phi)
    2091             : {
    2092       58373 :   GEN basis = msk_get_basis(W);
    2093       58373 :   long i, j, r, lvecT = lg(phi), dim = lg(basis)-1;
    2094       58373 :   GEN st = msk_get_st(W);
    2095       58373 :   GEN link = msk_get_link(W);
    2096       58373 :   GEN invphiblock = msk_get_invphiblock(W);
    2097       58373 :   long s = st[1], t = st[2];
    2098       58373 :   GEN R = zerocol(dim), Q, Ls, T0, T1, Ts;
    2099      781788 :   for (r = 2; r < lvecT; r++)
    2100             :   {
    2101             :     GEN Tr, L;
    2102      723415 :     if (r == s) continue;
    2103      665042 :     Tr = gel(phi,r); /* Phi(G_r), r != 1,s */
    2104      665042 :     L = gel(link, r);
    2105      665042 :     Q = ZC_apply_dinv(gel(invphiblock,r), Tr);
    2106             :     /* write Phi(G_r) as sum_{a,b} mu_{a,b} Phi_{a,b}(G_r) */
    2107      665042 :     for (j = 1; j < lg(L); j++) gel(R, L[j]) = gel(Q,j);
    2108             :   }
    2109       58373 :   Ls = gel(link, s);
    2110       58373 :   T1 = gel(phi,1); /* Phi(G_1) */
    2111       58373 :   gel(R, Ls[t]) = gel(T1, 1);
    2112             : 
    2113       58373 :   T0 = NULL;
    2114      781788 :   for (i = 2; i < lg(link); i++)
    2115             :   {
    2116             :     GEN L;
    2117      723415 :     if (i == s) continue;
    2118      665042 :     L = gel(link,i);
    2119     3485286 :     for (j =1 ; j < lg(L); j++)
    2120             :     {
    2121     2820244 :       long n = L[j]; /* phi_{i,j} = basis[n] */
    2122     2820244 :       GEN mu_ij = gel(R, n);
    2123     2820244 :       GEN phi_ij = gel(basis, n), pols = gel(phi_ij,3);
    2124     2820244 :       GEN z = RgC_Rg_mul(gel(pols, 3), mu_ij);
    2125     2820244 :       T0 = T0? RgC_add(T0, z): z; /* += mu_{i,j} Phi_{i,j} (G_s) */
    2126             :     }
    2127             :   }
    2128       58373 :   Ts = gel(phi,s); /* Phi(G_s) */
    2129       58373 :   if (T0) Ts = RgC_sub(Ts, T0);
    2130             :   /* solve \sum_{j!=t} mu_{s,j} Phi_{s,j}(G_s) = Ts */
    2131       58373 :   Q = ZC_apply_dinv(gel(invphiblock,s), Ts);
    2132       58373 :   for (j = 1; j < t; j++) gel(R, Ls[j]) = gel(Q,j);
    2133             :   /* avoid mu_{s,t} */
    2134       58373 :   for (j = t; j < lg(Q); j++) gel(R, Ls[j+1]) = gel(Q,j);
    2135       58373 :   return R;
    2136             : }
    2137             : 
    2138             : /* a = s(g_i) for some modular symbol s; b in Z[G]
    2139             :  * return s(b.g_i) = b^* . s(g_i) */
    2140             : static GEN
    2141         147 : ZGl2Q_act_s(GEN b, GEN a, long k)
    2142             : {
    2143         147 :   if (typ(b) == t_INT)
    2144             :   {
    2145          56 :     if (!signe(b)) return gen_0;
    2146          14 :     switch(typ(a))
    2147             :     {
    2148             :       case t_POL:
    2149          14 :         a = RgX_to_RgC(a, k-1); /*fall through*/
    2150             :       case t_COL:
    2151          14 :         a = RgC_Rg_mul(a,b);
    2152          14 :         break;
    2153           0 :       default: a = scalarcol_shallow(b,k-1);
    2154             :     }
    2155             :   }
    2156             :   else
    2157             :   {
    2158          91 :     b = RgX_act_ZGl2Q(ZSl2_star(b), k);
    2159          91 :     switch(typ(a))
    2160             :     {
    2161             :       case t_POL:
    2162          63 :         a = RgX_to_RgC(a, k-1); /*fall through*/
    2163             :       case t_COL:
    2164          91 :         a = RgM_RgC_mul(b,a);
    2165          91 :         break;
    2166           0 :       default: a = RgC_Rg_mul(gel(b,1),a);
    2167             :     }
    2168             :   }
    2169         105 :   return a;
    2170             : }
    2171             : 
    2172             : static int
    2173          21 : checksymbol(GEN W, GEN s)
    2174             : {
    2175             :   GEN t, annT2, annT31, singlerel;
    2176             :   long i, k, l, nbE1, nbT2, nbT31;
    2177          21 :   k = msk_get_weight(W);
    2178          21 :   W = get_ms(W);
    2179          21 :   nbE1 = ms_get_nbE1(W);
    2180          21 :   singlerel = gel(W,10);
    2181          21 :   l = lg(singlerel);
    2182          21 :   if (k == 2)
    2183             :   {
    2184           0 :     for (i = nbE1+1; i < l; i++)
    2185           0 :       if (!gequal0(gel(s,i))) return 0;
    2186           0 :     return 1;
    2187             :   }
    2188          21 :   annT2 = gel(W,8); nbT2 = lg(annT2)-1;
    2189          21 :   annT31 = gel(W,9);nbT31 = lg(annT31)-1;
    2190          21 :   t = NULL;
    2191          84 :   for (i = 1; i < l; i++)
    2192             :   {
    2193          63 :     GEN a = gel(s,i);
    2194          63 :     a = ZGl2Q_act_s(gel(singlerel,i), a, k);
    2195          63 :     t = t? gadd(t, a): a;
    2196             :   }
    2197          21 :   if (!gequal0(t)) return 0;
    2198          14 :   for (i = 1; i <= nbT2; i++)
    2199             :   {
    2200           0 :     GEN a = gel(s,i + nbE1);
    2201           0 :     a = ZGl2Q_act_s(gel(annT2,i), a, k);
    2202           0 :     if (!gequal0(a)) return 0;
    2203             :   }
    2204          28 :   for (i = 1; i <= nbT31; i++)
    2205             :   {
    2206          14 :     GEN a = gel(s,i + nbE1 + nbT2);
    2207          14 :     a = ZGl2Q_act_s(gel(annT31,i), a, k);
    2208          14 :     if (!gequal0(a)) return 0;
    2209             :   }
    2210          14 :   return 1;
    2211             : }
    2212             : long
    2213          28 : msissymbol(GEN W, GEN s)
    2214             : {
    2215             :   long k, nbgen;
    2216          28 :   checkms(W);
    2217          28 :   k = msk_get_weight(W);
    2218          28 :   nbgen = ms_get_nbgen(W);
    2219          28 :   switch(typ(s))
    2220             :   {
    2221             :     case t_VEC: /* values s(g_i) */
    2222          21 :       if (lg(s)-1 != nbgen) return 0;
    2223          21 :       break;
    2224             :     case t_COL:
    2225           7 :       if (msk_get_sign(W))
    2226             :       {
    2227           0 :         GEN star = gel(msk_get_starproj(W), 1);
    2228           0 :         if (lg(star) == lg(s)) return 1;
    2229             :       }
    2230           7 :       if (k == 2) /* on the dual basis of (g_i) */
    2231             :       {
    2232           0 :         if (lg(s)-1 != nbgen) return 0;
    2233             :       }
    2234             :       else
    2235             :       {
    2236           7 :         GEN basis = msk_get_basis(W);
    2237           7 :         return (lg(s) == lg(basis));
    2238             :       }
    2239           0 :       break;
    2240           0 :     default: return 0;
    2241             :   }
    2242          21 :   return checksymbol(W,s);
    2243             : }
    2244             : #if DEBUG
    2245             : /* phi is a sparse symbol from msk_get_basis, return phi(G_j) */
    2246             : static GEN
    2247             : phi_Gj(GEN W, GEN phi, long j)
    2248             : {
    2249             :   GEN ind = gel(phi,2), pols = gel(phi,3);
    2250             :   long i = vecsmall_isin(ind,j);
    2251             :   return i? gel(pols,i): NULL;
    2252             : }
    2253             : /* check that \sum d_i phi_i(G_j)  = T_j for all j */
    2254             : static void
    2255             : checkdec(GEN W, GEN D, GEN T)
    2256             : {
    2257             :   GEN B = msk_get_basis(W);
    2258             :   long i, j;
    2259             :   if (!checksymbol(W,T)) pari_err_BUG("checkdec");
    2260             :   for (j = 1; j < lg(T); j++)
    2261             :   {
    2262             :     GEN S = gen_0;
    2263             :     for (i = 1; i < lg(D); i++)
    2264             :     {
    2265             :       GEN d = gel(D,i), v = phi_Gj(W, gel(B,i), j);
    2266             :       if (!v || gequal0(d)) continue;
    2267             :       S = gadd(S, gmul(d, v));
    2268             :     }
    2269             :     /* S = \sum_i d_i phi_i(G_j) */
    2270             :     if (!gequal(S, gel(T,j)))
    2271             :       pari_warn(warner, "checkdec j = %ld\n\tS = %Ps\n\tT = %Ps", j,S,gel(T,j));
    2272             :   }
    2273             : }
    2274             : #endif
    2275             : 
    2276             : /* map op: W1 = Hom(Delta_0(N1),V) -> W2 = Hom(Delta_0(N2),V), given by
    2277             :  * \sum v[i], v[i] in Gl2(Q) */
    2278             : static GEN
    2279        5054 : getMorphism(GEN W1, GEN W2, GEN v)
    2280             : {
    2281             :   struct m_act S;
    2282             :   GEN B1, M, act;
    2283        5054 :   long a, l, k = msk_get_weight(W1);
    2284        5054 :   if (k == 2) return getMorphism_trivial(W1,W2,v);
    2285        2576 :   S.k = k;
    2286        2576 :   S.dim = k-1;
    2287        2576 :   act = init_dual_act(v,W1,W2,&S, _RgX_act_Gl2Q);
    2288        2576 :   B1 = msk_get_basis(W1);
    2289        2576 :   l = lg(B1); M = cgetg(l, t_MAT);
    2290       60018 :   for (a = 1; a < l; a++)
    2291             :   {
    2292       57442 :     pari_sp av = avma;
    2293       57442 :     GEN phi = dual_act(S.dim, act, gel(B1,a));
    2294       57442 :     GEN D = getMorphism_basis(W2, phi);
    2295             : #if DEBUG
    2296             :     checkdec(W2,D,T);
    2297             : #endif
    2298       57442 :     gel(M,a) = gerepilecopy(av, D);
    2299             :   }
    2300        2576 :   return M;
    2301             : }
    2302             : static GEN
    2303        3934 : msendo(GEN W, GEN v) { return getMorphism(W, W, v); }
    2304             : 
    2305             : static GEN
    2306        2422 : endo_project(GEN W, GEN e, GEN H)
    2307             : {
    2308        2422 :   if (msk_get_sign(W)) e = Qevproj_apply(e, msk_get_starproj(W));
    2309        2422 :   if (H) e = Qevproj_apply(e, Qevproj_init0(H));
    2310        2422 :   return e;
    2311             : }
    2312             : static GEN
    2313        2688 : mshecke_i(GEN W, ulong p)
    2314             : {
    2315        2688 :   GEN v = ms_get_N(W) % p? Tp_matrices(p): Up_matrices(p);
    2316        2688 :   return msendo(W,v);
    2317             : }
    2318             : GEN
    2319        2387 : mshecke(GEN W, long p, GEN H)
    2320             : {
    2321        2387 :   pari_sp av = avma;
    2322             :   GEN T;
    2323        2387 :   checkms(W);
    2324        2387 :   if (p <= 1) pari_err_PRIME("mshecke",stoi(p));
    2325        2387 :   T = mshecke_i(W,p);
    2326        2387 :   T = endo_project(W,T,H);
    2327        2387 :   return gerepilecopy(av, T);
    2328             : }
    2329             : 
    2330             : static GEN
    2331          35 : msatkinlehner_i(GEN W, long Q)
    2332             : {
    2333          35 :   long N = ms_get_N(W);
    2334             :   GEN v;
    2335          35 :   if (Q == 1) return matid(msk_get_dim(W));
    2336          28 :   if (Q == N) return msendo(W, mkvec(mat2(0,1,-N,0)));
    2337          21 :   if (N % Q) pari_err_DOMAIN("msatkinlehner","N % Q","!=",gen_0,stoi(Q));
    2338          14 :   v = WQ_matrix(N, Q);
    2339          14 :   if (!v) pari_err_DOMAIN("msatkinlehner","gcd(Q,N/Q)","!=",gen_1,stoi(Q));
    2340          14 :   return msendo(W,mkvec(v));
    2341             : }
    2342             : GEN
    2343          35 : msatkinlehner(GEN W, long Q, GEN H)
    2344             : {
    2345          35 :   pari_sp av = avma;
    2346             :   GEN w;
    2347             :   long k;
    2348          35 :   checkms(W);
    2349          35 :   k = msk_get_weight(W);
    2350          35 :   if (Q <= 0) pari_err_DOMAIN("msatkinlehner","Q","<=",gen_0,stoi(Q));
    2351          35 :   w = msatkinlehner_i(W,Q);
    2352          28 :   w = endo_project(W,w,H);
    2353          28 :   if (k > 2 && Q != 1) w = RgM_Rg_div(w, powuu(Q,(k-2)>>1));
    2354          28 :   return gerepilecopy(av, w);
    2355             : }
    2356             : 
    2357             : static GEN
    2358        1225 : msstar_i(GEN W) { return msendo(W, mkvec(mat2(-1,0,0,1))); }
    2359             : GEN
    2360           7 : msstar(GEN W, GEN H)
    2361             : {
    2362           7 :   pari_sp av = avma;
    2363             :   GEN s;
    2364           7 :   checkms(W);
    2365           7 :   s = msstar_i(W);
    2366           7 :   s = endo_project(W,s,H);
    2367           7 :   return gerepilecopy(av, s);
    2368             : }
    2369             : 
    2370             : #if 0
    2371             : /* is \Gamma_0(N) cusp1 = \Gamma_0(N) cusp2 ? */
    2372             : static int
    2373             : iscuspeq(ulong N, GEN cusp1, GEN cusp2)
    2374             : {
    2375             :   long p1, q1, p2, q2, s1, s2, d;
    2376             :   p1 = cusp1[1]; p2 = cusp2[1];
    2377             :   q1 = cusp1[2]; q2 = cusp2[2];
    2378             :   d = Fl_mul(smodss(q1,N),smodss(q2,N), N);
    2379             :   d = ugcd(d, N);
    2380             : 
    2381             :   s1 = q1 > 2? Fl_inv(smodss(p1,q1), q1): 1;
    2382             :   s2 = q2 > 2? Fl_inv(smodss(p2,q2), q2): 1;
    2383             :   return Fl_mul(s1,q2,d) == Fl_mul(s2,q1,d);
    2384             : }
    2385             : #endif
    2386             : 
    2387             : /* return E_c(r) */
    2388             : static GEN
    2389        2576 : get_Ec_r(GEN c, long k)
    2390             : {
    2391        2576 :   long p = c[1], q = c[2], u, v;
    2392             :   GEN gr;
    2393        2576 :   (void)cbezout(p, q, &u, &v);
    2394        2576 :   gr = mat2(p, -v, q, u); /* g . (1:0) = (p:q) */
    2395        2576 :   return voo_act_Gl2Q(zm_to_ZM(sl2_inv(gr)), k);
    2396             : }
    2397             : /* returns the modular symbol attached to the cusp c := p/q via the rule
    2398             :  * E_c(path from a to b in Delta_0) := E_c(b) - E_c(a), where
    2399             :  * E_c(r) := 0 if r != c mod Gamma
    2400             :  *           v_oo | gamma_r^(-1)
    2401             :  * where v_oo is stable by T = [1,1;0,1] (i.e x^(k-2)) and
    2402             :  * gamma_r . (1:0) = r, for some gamma_r in SL_2(Z) * */
    2403             : static GEN
    2404         434 : msfromcusp_trivial(GEN W, GEN c)
    2405             : {
    2406         434 :   GEN section = ms_get_section(W), gen = ms_get_genindex(W);
    2407         434 :   GEN S = ms_get_hashcusps(W);
    2408         434 :   long j, ic = cusp_index(c, S), l = ms_get_nbE1(W)+1;
    2409         434 :   GEN phi = cgetg(l, t_COL);
    2410       90034 :   for (j = 1; j < l; j++)
    2411             :   {
    2412       89600 :     GEN vj, g = gel(section, gen[j]); /* path_to_zm(generator) */
    2413       89600 :     GEN c1 = gel(g,1), c2 = gel(g,2);
    2414       89600 :     long i1 = cusp_index(c1, S);
    2415       89600 :     long i2 = cusp_index(c2, S);
    2416       89600 :     if (i1 == ic)
    2417        3199 :       vj = (i2 == ic)?  gen_0: gen_1;
    2418             :     else
    2419       86401 :       vj = (i2 == ic)? gen_m1: gen_0;
    2420       89600 :     gel(phi, j) = vj;
    2421             :   }
    2422         434 :   return phi;
    2423             : }
    2424             : static GEN
    2425        1365 : msfromcusp_i(GEN W, GEN c)
    2426             : {
    2427             :   GEN section, gen, S, phi;
    2428        1365 :   long j, ic, l, k = msk_get_weight(W);
    2429        1365 :   if (k == 2) return msfromcusp_trivial(W, c);
    2430         931 :   k = msk_get_weight(W);
    2431         931 :   section = ms_get_section(W);
    2432         931 :   gen = ms_get_genindex(W);
    2433         931 :   S = ms_get_hashcusps(W);
    2434         931 :   ic = cusp_index(c, S);
    2435         931 :   l = lg(gen);
    2436         931 :   phi = cgetg(l, t_COL);
    2437       11543 :   for (j = 1; j < l; j++)
    2438             :   {
    2439       10612 :     GEN vj = NULL, g = gel(section, gen[j]); /* path_to_zm(generator) */
    2440       10612 :     GEN c1 = gel(g,1), c2 = gel(g,2);
    2441       10612 :     long i1 = cusp_index(c1, S);
    2442       10612 :     long i2 = cusp_index(c2, S);
    2443       10612 :     if (i1 == ic) vj = get_Ec_r(c1, k);
    2444       10612 :     if (i2 == ic)
    2445             :     {
    2446        1288 :       GEN s = get_Ec_r(c2, k);
    2447        1288 :       vj = vj? gsub(vj, s): gneg(s);
    2448             :     }
    2449       10612 :     if (!vj) vj = zerocol(k-1);
    2450       10612 :     gel(phi, j) = vj;
    2451             :   }
    2452         931 :   return getMorphism_basis(W, phi);
    2453             : }
    2454             : GEN
    2455          21 : msfromcusp(GEN W, GEN c)
    2456             : {
    2457          21 :   pari_sp av = avma;
    2458             :   long N;
    2459          21 :   checkms(W);
    2460          21 :   N = ms_get_N(W);
    2461          21 :   switch(typ(c))
    2462             :   {
    2463             :     case t_INFINITY:
    2464           7 :       c = mkvecsmall2(1,0);
    2465           7 :       break;
    2466             :     case t_INT:
    2467           7 :       c = mkvecsmall2(smodis(c,N), 1);
    2468           7 :       break;
    2469             :     case t_FRAC:
    2470           7 :       c = mkvecsmall2(smodis(gel(c,1),N), smodis(gel(c,2),N));
    2471           7 :       break;
    2472             :     default:
    2473           0 :       pari_err_TYPE("msfromcusp",c);
    2474             :   }
    2475          21 :   return gerepilecopy(av, msfromcusp_i(W,c));
    2476             : }
    2477             : 
    2478             : static GEN
    2479         294 : mseisenstein_i(GEN W)
    2480             : {
    2481         294 :   GEN M, S = ms_get_hashcusps(W), cusps = gel(S,3);
    2482         294 :   long i, l = lg(cusps);
    2483         294 :   if (msk_get_weight(W)==2) l--;
    2484         294 :   M = cgetg(l, t_MAT);
    2485         294 :   for (i = 1; i < l; i++) gel(M,i) = msfromcusp_i(W, gel(cusps,i));
    2486         294 :   return Qevproj_star(W, QM_image(M));
    2487             : }
    2488             : GEN
    2489         294 : mseisenstein(GEN W)
    2490             : {
    2491         294 :   pari_sp av = avma;
    2492         294 :   checkms(W);
    2493         294 :   return gerepilecopy(av, Qevproj_init(mseisenstein_i(W)));
    2494             : }
    2495             : 
    2496             : /* upper bound for log_2 |charpoly(T_p|S)|, where S is a cuspidal subspace of
    2497             :  * dimension d, k is the weight */
    2498             : #if 0
    2499             : static long
    2500             : TpS_char_bound(ulong p, long k, long d)
    2501             : { /* |eigenvalue| <= 2 p^(k-1)/2 */
    2502             :   return d * (2 + (log2((double)p)*(k-1))/2);
    2503             : }
    2504             : #endif
    2505             : static long
    2506         287 : TpE_char_bound(ulong p, long k, long d)
    2507             : { /* |eigenvalue| <= 2 p^(k-1) */
    2508         287 :   return d * (2 + log2((double)p)*(k-1));
    2509             : }
    2510             : 
    2511             : GEN
    2512         287 : mscuspidal(GEN W, long flag)
    2513             : {
    2514         287 :   pari_sp av = avma;
    2515             :   GEN S, E, M, T, TE, chE;
    2516             :   long bit;
    2517             :   forprime_t F;
    2518             :   ulong p, N;
    2519             :   pari_timer ti;
    2520             : 
    2521         287 :   E = mseisenstein(W);
    2522         287 :   N = ms_get_N(W);
    2523         287 :   (void)u_forprime_init(&F, 2, ULONG_MAX);
    2524         287 :   while ((p = u_forprime_next(&F)))
    2525         399 :     if (N % p) break;
    2526         287 :   if (DEBUGLEVEL) timer_start(&ti);
    2527         287 :   T = mshecke(W, p, NULL);
    2528         287 :   if (DEBUGLEVEL) timer_printf(&ti,"Tp, p = %ld", p);
    2529         287 :   TE = Qevproj_apply(T, E); /* T_p | E */
    2530         287 :   if (DEBUGLEVEL) timer_printf(&ti,"Qevproj_init(E)");
    2531         287 :   bit = TpE_char_bound(p, msk_get_weight(W), lg(TE)-1);
    2532         287 :   chE = QM_charpoly_ZX_bound(TE, bit);
    2533         287 :   chE = ZX_radical(chE);
    2534         287 :   M = RgX_RgM_eval(chE, T);
    2535         287 :   S = Qevproj_init(QM_image(M));
    2536         287 :   return gerepilecopy(av, flag? mkvec2(S,E): S);
    2537             : }
    2538             : 
    2539             : /** INIT ELLSYM STRUCTURE **/
    2540             : /* V a vector of ZM. If all of them have 0 last row, return NULL.
    2541             :  * Otherwise return [m,i,j], where m = V[i][last,j] contains the value
    2542             :  * of smallest absolute value */
    2543             : static GEN
    2544         812 : RgMV_find_non_zero_last_row(long offset, GEN V)
    2545             : {
    2546         812 :   long i, lasti = 0, lastj = 0, lV = lg(V);
    2547         812 :   GEN m = NULL;
    2548        3668 :   for (i = 1; i < lV; i++)
    2549             :   {
    2550        2856 :     GEN M = gel(V,i);
    2551        2856 :     long j, n, l = lg(M);
    2552        2856 :     if (l == 1) continue;
    2553        2597 :     n = nbrows(M);
    2554       12908 :     for (j = 1; j < l; j++)
    2555             :     {
    2556       10311 :       GEN a = gcoeff(M, n, j);
    2557       10311 :       if (!gequal0(a) && (!m || abscmpii(a, m) < 0))
    2558             :       {
    2559        1414 :         m = a; lasti = i; lastj = j;
    2560        1414 :         if (is_pm1(m)) goto END;
    2561             :       }
    2562             :     }
    2563             :   }
    2564             : END:
    2565         812 :   if (!m) return NULL;
    2566         553 :   return mkvec2(m, mkvecsmall2(lasti+offset, lastj));
    2567             : }
    2568             : /* invert the d_oo := (\gamma_oo - 1) operator, acting on
    2569             :  * [x^(k-2), ..., y^(k-2)] */
    2570             : static GEN
    2571         553 : Delta_inv(GEN doo, long k)
    2572             : {
    2573         553 :   GEN M = RgX_act_ZGl2Q(doo, k);
    2574         553 :   M = RgM_minor(M, k-1, 1); /* 1st column and last row are 0 */
    2575         553 :   return ZM_inv_denom(M);
    2576             : }
    2577             : /* The ZX P = \sum a_i x^i y^{k-2-i} is given by the ZV [a_0, ..., a_k-2]~,
    2578             :  * return Q and d such that P = doo Q + d y^k-2, where d in Z and Q */
    2579             : static GEN
    2580       12089 : doo_decompose(GEN dinv, GEN P, GEN *pd)
    2581             : {
    2582       12089 :   long l = lg(P); *pd = gel(P, l-1);
    2583       12089 :   P = vecslice(P, 1, l-2);
    2584       12089 :   return shallowconcat(gen_0, ZC_apply_dinv(dinv, P));
    2585             : }
    2586             : 
    2587             : static GEN
    2588       12089 : get_phi_ij(long i,long j,long n, long s,long t,GEN P_st,GEN Q_st,GEN d_st,
    2589             :            GEN P_ij, GEN lP_ij, GEN dinv)
    2590             : {
    2591             :   GEN ind, pols;
    2592       12089 :   if (i == s && j == t)
    2593             :   {
    2594         553 :     ind = mkvecsmall(1);
    2595         553 :     pols = mkvec(scalarcol_shallow(gen_1, lg(P_st)-1)); /* x^{k-2} */
    2596             :   }
    2597             :   else
    2598             :   {
    2599       11536 :     GEN d_ij, Q_ij = doo_decompose(dinv, lP_ij, &d_ij);
    2600       11536 :     GEN a = ZC_Z_mul(P_ij, d_st);
    2601       11536 :     GEN b = ZC_Z_mul(P_st, negi(d_ij));
    2602       11536 :     GEN c = RgC_sub(RgC_Rg_mul(Q_ij, d_st), RgC_Rg_mul(Q_st, d_ij));
    2603       11536 :     if (i == s) { /* j != t */
    2604        1526 :       ind = mkvecsmall2(1, s);
    2605        1526 :       pols = mkvec2(c, ZC_add(a, b));
    2606             :     } else {
    2607       10010 :       ind = mkvecsmall3(1, i, s);
    2608       10010 :       pols = mkvec3(c, a, b); /* image of g_1, g_i, g_s */
    2609             :     }
    2610       11536 :     pols = Q_primpart(pols);
    2611             :   }
    2612       12089 :   return mkvec3(mkvecsmall3(i,j,n), ind, pols);
    2613             : }
    2614             : 
    2615             : static GEN
    2616         665 : mskinit_trivial(GEN WN)
    2617             : {
    2618         665 :   long dim = ms_get_nbE1(WN);
    2619         665 :   return mkvec3(WN, gen_0, mkvec2(gen_0,mkvecsmall2(2, dim)));
    2620             : }
    2621             : /* sum of #cols of the matrices contained in V */
    2622             : static long
    2623        1106 : RgMV_dim(GEN V)
    2624             : {
    2625        1106 :   long l = lg(V), d = 0, i;
    2626        1106 :   for (i = 1; i < l; i++) d += lg(gel(V,i)) - 1;
    2627        1106 :   return d;
    2628             : }
    2629             : static GEN
    2630         553 : mskinit_nontrivial(GEN WN, long k)
    2631             : {
    2632         553 :   GEN annT2 = gel(WN,8), annT31 = gel(WN,9), singlerel = gel(WN,10);
    2633             :   GEN link, basis, monomials, invphiblock;
    2634         553 :   long nbE1 = ms_get_nbE1(WN);
    2635         553 :   GEN dinv = Delta_inv(ZG_neg( ZSl2_star(gel(singlerel,1)) ), k);
    2636         553 :   GEN p1 = cgetg(nbE1+1, t_VEC), remove;
    2637         553 :   GEN p2 = ZGV_tors(annT2, k);
    2638         553 :   GEN p3 = ZGV_tors(annT31, k);
    2639         553 :   GEN gentor = shallowconcat(p2, p3);
    2640             :   GEN P_st, lP_st, Q_st, d_st;
    2641             :   long n, i, dim, s, t, u;
    2642         553 :   gel(p1, 1) = cgetg(1,t_MAT); /* dummy */
    2643        3080 :   for (i = 2; i <= nbE1; i++) /* skip 1st element = (\gamma_oo-1)g_oo */
    2644             :   {
    2645        2527 :     GEN z = gel(singlerel, i);
    2646        2527 :     gel(p1, i) = RgX_act_ZGl2Q(ZSl2_star(z), k);
    2647             :   }
    2648         553 :   remove = RgMV_find_non_zero_last_row(nbE1, gentor);
    2649         553 :   if (!remove) remove = RgMV_find_non_zero_last_row(0, p1);
    2650         553 :   if (!remove) pari_err_BUG("msinit [no y^k-2]");
    2651         553 :   remove = gel(remove,2); /* [s,t] */
    2652         553 :   s = remove[1];
    2653         553 :   t = remove[2];
    2654             :   /* +1 because of = x^(k-2), but -1 because of Manin relation */
    2655         553 :   dim = (k-1)*(nbE1-1) + RgMV_dim(p2) + RgMV_dim(p3);
    2656             :   /* Let (g_1,...,g_d) be the Gamma-generators of Delta, g_1 = g_oo.
    2657             :    * We describe modular symbols by the collection phi(g_1), ..., phi(g_d)
    2658             :    * \in V := Q[x,y]_{k-2}, with right Gamma action.
    2659             :    * For each i = 1, .., d, let V_i \subset V be the Q-vector space of
    2660             :    * allowed values for phi(g_i): with basis (P^{i,j}) given by the monomials
    2661             :    * x^(j-1) y^{k-2-(j-1)}, j = 1 .. k-1
    2662             :    * (g_i in E_1) or the solution of the torsion equations (1 + gamma)P = 0
    2663             :    * (g_i in T2) or (1 + gamma + gamma^2)P = 0 (g_i in T31). All such P
    2664             :    * are chosen in Z[x,y] with Q_content 1.
    2665             :    *
    2666             :    * The Manin relation (singlerel) is of the form \sum_i \lambda_i g_i = 0,
    2667             :    * where \lambda_i = 1 if g_i in T2 or T31, and \lambda_i = (1 - \gamma_i)
    2668             :    * for g_i in E1.
    2669             :    *
    2670             :    * If phi \in Hom_Gamma(Delta, V), it is defined by phi(g_i) := P_i in V
    2671             :    * with \sum_i P_i . \lambda_i^* = 0, where (\sum n_i g_i)^* :=
    2672             :    * \sum n_i \gamma_i^(-1).
    2673             :    *
    2674             :    * We single out gamma_1 / g_1 (g_oo in Pollack-Stevens paper) and
    2675             :    * write P_{i,j} \lambda_i^* =  Q_{i,j} (\gamma_1 - 1)^* + d_{i,j} y^{k-2}
    2676             :    * where d_{i,j} is a scalar and Q_{i,j} in V; we normalize Q_{i,j} to
    2677             :    * that the coefficient of x^{k-2} is 0.
    2678             :    *
    2679             :    * There exist (s,t) such that d_{s,t} != 0.
    2680             :    * A Q-basis of the (dual) space of modular symbols is given by the
    2681             :    * functions phi_{i,j}, 2 <= i <= d, 1 <= j <= k-1, mapping
    2682             :    *  g_1 -> d_{s,t} Q_{i,j} - d_{i,j} Q_{s,t} + [(i,j)=(s,t)] x^{k-2}
    2683             :    * If i != s
    2684             :    *   g_i -> d_{s,t} P_{i,j}
    2685             :    *   g_s -> - d_{i,j} P_{s,t}
    2686             :    * If i = s, j != t
    2687             :    *   g_i -> d_{s,t} P_{i,j} - d_{i,j} P_{s,t}
    2688             :    * And everything else to 0. Again we normalize the phi_{i,j} such that
    2689             :    * their image has content 1. */
    2690         553 :   monomials = matid(k-1); /* represent the monomials x^{k-2}, ... , y^{k-2} */
    2691         553 :   if (s <= nbE1) /* in E1 */
    2692             :   {
    2693         259 :     P_st = gel(monomials, t);
    2694         259 :     lP_st = gmael(p1, s, t); /* P_{s,t} lambda_s^* */
    2695             :   }
    2696             :   else /* in T2, T31 */
    2697             :   {
    2698         294 :     P_st = gmael(gentor, s - nbE1, t);
    2699         294 :     lP_st = P_st;
    2700             :   }
    2701         553 :   Q_st = doo_decompose(dinv, lP_st, &d_st);
    2702         553 :   basis = cgetg(dim+1, t_VEC);
    2703         553 :   link = cgetg(nbE1 + lg(gentor), t_VEC);
    2704         553 :   gel(link,1) = cgetg(1,t_VECSMALL); /* dummy */
    2705         553 :   n = 1;
    2706        3080 :   for (i = 2; i <= nbE1; i++)
    2707             :   {
    2708        2527 :     GEN L = cgetg(k, t_VECSMALL);
    2709             :     long j;
    2710             :     /* link[i][j] = n gives correspondance between phi_{i,j} and basis[n] */
    2711        2527 :     gel(link,i) = L;
    2712       13160 :     for (j = 1; j < k; j++)
    2713             :     {
    2714       10633 :       GEN lP_ij = gmael(p1, i, j); /* P_{i,j} lambda_i^* */
    2715       10633 :       GEN P_ij = gel(monomials,j);
    2716       10633 :       L[j] = n;
    2717       10633 :       gel(basis, n) = get_phi_ij(i,j,n, s,t, P_st, Q_st, d_st, P_ij, lP_ij, dinv);
    2718       10633 :       n++;
    2719             :     }
    2720             :   }
    2721        1001 :   for (u = 1; u < lg(gentor); u++,i++)
    2722             :   {
    2723         448 :     GEN V = gel(gentor,u);
    2724         448 :     long j, lV = lg(V);
    2725         448 :     GEN L = cgetg(lV, t_VECSMALL);
    2726         448 :     gel(link,i) = L;
    2727        1904 :     for (j = 1; j < lV; j++)
    2728             :     {
    2729        1456 :       GEN lP_ij = gel(V, j); /* P_{i,j} lambda_i^* = P_{i,j} */
    2730        1456 :       GEN P_ij = lP_ij;
    2731        1456 :       L[j] = n;
    2732        1456 :       gel(basis, n) = get_phi_ij(i,j,n, s,t, P_st, Q_st, d_st, P_ij, lP_ij, dinv);
    2733        1456 :       n++;
    2734             :     }
    2735             :   }
    2736         553 :   invphiblock = cgetg(lg(link), t_VEC);
    2737         553 :   gel(invphiblock,1) = cgetg(1, t_MAT); /* dummy */
    2738        3528 :   for (i = 2; i < lg(link); i++)
    2739             :   {
    2740        2975 :     GEN M, inv, B = gel(link,i);
    2741        2975 :     long j, lB = lg(B);
    2742        2975 :     if (i == s) { B = vecsplice(B, t); lB--; } /* remove phi_st */
    2743        2975 :     M = cgetg(lB, t_MAT);
    2744       14511 :     for (j = 1; j < lB; j++)
    2745             :     {
    2746       11536 :       GEN phi_ij = gel(basis, B[j]), pols = gel(phi_ij,3);
    2747       11536 :       gel(M, j) = gel(pols, 2); /* phi_ij(g_i) */
    2748             :     }
    2749        2975 :     if (i <= nbE1 && i != s) /* maximal rank k-1 */
    2750        2268 :       inv = ZM_inv_denom(M);
    2751             :     else /* i = s (rank k-2) or from torsion: rank k/3 or k/2 */
    2752         707 :       inv = Qevproj_init(M);
    2753        2975 :     gel(invphiblock,i) = inv;
    2754             :   }
    2755         553 :   return mkvec3(WN, gen_0, mkvec5(basis, mkvecsmall2(k, dim), mkvecsmall2(s,t),
    2756             :                                   link, invphiblock));
    2757             : }
    2758             : static GEN
    2759        1218 : add_star(GEN W, long sign)
    2760             : {
    2761        1218 :   GEN s = msstar_i(W);
    2762        1218 :   GEN K = sign? QM_ker(gsubgs(s, sign)): cgetg(1,t_MAT);
    2763        1218 :   gel(W,2) = mkvec3(stoi(sign), s, Qevproj_init(K));
    2764        1218 :   return W;
    2765             : }
    2766             : /* WN = msinit_N(N) */
    2767             : static GEN
    2768        1218 : mskinit(ulong N, long k, long sign)
    2769             : {
    2770        1218 :   GEN WN = msinit_N(N);
    2771        1218 :   GEN W = k == 2? mskinit_trivial(WN)
    2772        1218 :                 : mskinit_nontrivial(WN, k);
    2773        1218 :   return add_star(W, sign);
    2774             : }
    2775             : GEN
    2776         364 : msinit(GEN N, GEN K, long sign)
    2777             : {
    2778         364 :   pari_sp av = avma;
    2779             :   GEN W;
    2780             :   long k;
    2781         364 :   if (typ(N) != t_INT) pari_err_TYPE("msinit", N);
    2782         364 :   if (typ(K) != t_INT) pari_err_TYPE("msinit", K);
    2783         364 :   k = itos(K);
    2784         364 :   if (k < 2) pari_err_DOMAIN("msinit","k", "<", gen_2,K);
    2785         364 :   if (odd(k)) pari_err_IMPL("msinit [odd weight]");
    2786         364 :   if (signe(N) <= 0) pari_err_DOMAIN("msinit","N", "<=", gen_0,N);
    2787         364 :   if (equali1(N)) pari_err_IMPL("msinit [ N = 1 ]");
    2788         364 :   W = mskinit(itou(N), k, sign);
    2789         364 :   return gerepilecopy(av, W);
    2790             : }
    2791             : 
    2792             : /* W = msinit, xpm integral modular symbol of weight 2, c t_FRAC
    2793             :  * Return image of <oo->c> */
    2794             : static GEN
    2795        1645 : Q_xpm(GEN W, GEN xpm, GEN c)
    2796             : {
    2797        1645 :   pari_sp av = avma;
    2798             :   GEN v;
    2799        1645 :   W = get_ms(W);
    2800        1645 :   v = init_act_trivial(W);
    2801        1645 :   Q_log_trivial(v, W, c); /* oo -> (a:b), c = a/b */
    2802        1645 :   return gerepileuptoint(av, ZV_zc_mul(xpm, v));
    2803             : }
    2804             : 
    2805             : static GEN
    2806          35 : eval_single(GEN s, long k, GEN B, long v)
    2807             : {
    2808             :   long i, l;
    2809          35 :   GEN A = cgetg_copy(s,&l);
    2810          35 :   for (i=1; i<l; i++) gel(A,i) = ZGl2Q_act_s(gel(B,i), gel(s,i), k);
    2811          35 :   A = RgV_sum(A);
    2812          35 :   if (is_vec_t(typ(A))) A = RgV_to_RgX(A, v);
    2813          35 :   return A;
    2814             : }
    2815             : /* Evaluate symbol s on mspathlog B (= sum p_i g_i, p_i in Z[G]). Allow
    2816             :  * s = t_MAT [ collection of symbols, return a vector ]*/
    2817             : static GEN
    2818          63 : mseval_by_values(GEN W, GEN s, GEN p, long v)
    2819             : {
    2820          63 :   long i, l, k = msk_get_weight(W);
    2821             :   GEN A;
    2822          63 :   if (k == 2)
    2823             :   { /* trivial represention: don't bother with Z[G] */
    2824          35 :     GEN B = mspathlog_trivial(W,p);
    2825          28 :     if (typ(s) != t_MAT) return RgV_zc_mul(s,B);
    2826           0 :     l = lg(s); A = cgetg(l, t_VEC);
    2827           0 :     for (i = 1; i < l; i++) gel(A,i) = RgV_zc_mul(gel(s,i), B);
    2828             :   }
    2829             :   else
    2830             :   {
    2831          28 :     GEN B = mspathlog(W,p);
    2832          28 :     if (typ(s) != t_MAT) return eval_single(s, k, B, v);
    2833           7 :     l = lg(s); A = cgetg(l, t_VEC);
    2834           7 :     for (i = 1; i < l; i++) gel(A,i) = eval_single(gel(s,i), k, B, v);
    2835             :   }
    2836           7 :   return A;
    2837             : }
    2838             : 
    2839             : /* express symbol on the basis phi_{i,j} */
    2840             : static GEN
    2841         385 : symtophi(GEN W, GEN s)
    2842             : {
    2843         385 :   GEN e, basis = msk_get_basis(W);
    2844         385 :   long i, l = lg(basis);
    2845         385 :   if (lg(s) != l) pari_err_TYPE("mseval",s);
    2846         385 :   e = const_vec(ms_get_nbgen(W), gen_0);
    2847       12565 :   for (i=1; i<l; i++)
    2848             :   {
    2849       12180 :     GEN phi, ind, pols, c = gel(s,i);
    2850             :     long j, m;
    2851       12180 :     if (gequal0(c)) continue;
    2852       12019 :     phi = gel(basis,i);
    2853       12019 :     ind = gel(phi,2); m = lg(ind);
    2854       12019 :     pols = gel(phi,3);
    2855       46249 :     for (j=1; j<m; j++)
    2856             :     {
    2857       34230 :       long t = ind[j];
    2858       34230 :       gel(e,t) = gadd(gel(e,t), gmul(c, gel(pols,j)));
    2859             :     }
    2860             :   }
    2861         385 :   return e;
    2862             : }
    2863             : /* evaluate symbol s on path p */
    2864             : GEN
    2865         952 : mseval(GEN W, GEN s, GEN p)
    2866             : {
    2867         952 :   pari_sp av = avma;
    2868         952 :   long i, k, l, v = 0;
    2869         952 :   checkms(W);
    2870         952 :   k = msk_get_weight(W);
    2871         952 :   switch(typ(s))
    2872             :   {
    2873             :     case t_VEC: /* values s(g_i) */
    2874           7 :       if (lg(s)-1 != ms_get_nbgen(W)) pari_err_TYPE("mseval",s);
    2875           7 :       if (!p) return gcopy(s);
    2876           0 :       v = gvar(s);
    2877           0 :       break;
    2878             :     case t_COL:
    2879         931 :       if (msk_get_sign(W))
    2880             :       {
    2881         336 :         GEN star = gel(msk_get_starproj(W), 1);
    2882         336 :         if (lg(star) == lg(s)) s = RgM_RgC_mul(star, s);
    2883             :       }
    2884         931 :       if (k == 2) /* on the dual basis of (g_i) */
    2885             :       {
    2886         560 :         if (lg(s)-1 != ms_get_nbE1(W)) pari_err_TYPE("mseval",s);
    2887         560 :         if (!p) return gtrans(s);
    2888             :       }
    2889             :       else
    2890         371 :         s = symtophi(W,s);
    2891         406 :       break;
    2892             :     case t_MAT:
    2893          14 :       if (!p) pari_err_TYPE("mseval",s);
    2894          14 :       l = lg(s);
    2895          14 :       if (l == 1) return cgetg(1, t_VEC);
    2896           7 :       if (msk_get_sign(W))
    2897             :       {
    2898           0 :         GEN star = gel(msk_get_starproj(W), 1);
    2899           0 :         if (lg(star) == lgcols(s)) s = RgM_mul(star, s);
    2900             :       }
    2901           7 :       if (k == 2)
    2902           0 :       { if (nbrows(s) != ms_get_nbE1(W)) pari_err_TYPE("mseval",s); }
    2903             :       else
    2904             :       {
    2905           7 :         GEN t = cgetg(l, t_MAT);
    2906           7 :         for (i = 1; i < l; i++) gel(t,i) = symtophi(W,gel(s,i));
    2907           7 :         s = t;
    2908             :       }
    2909           7 :       break;
    2910           0 :     default: pari_err_TYPE("mseval",s);
    2911             :   }
    2912         413 :   if (p)
    2913          63 :     s = mseval_by_values(W, s, p, v);
    2914             :   else
    2915             :   {
    2916         350 :     l = lg(s);
    2917        3577 :     for (i = 1; i < l; i++)
    2918             :     {
    2919        3227 :       GEN c = gel(s,i);
    2920        3227 :       if (!isintzero(c)) gel(s,i) = RgV_to_RgX(gel(s,i), v);
    2921             :     }
    2922             :   }
    2923         406 :   return gerepilecopy(av, s);
    2924             : }
    2925             : 
    2926             : /* sum_{a <= |D|} (D/a)*xpm(E,a/|D|) */
    2927             : static GEN
    2928         539 : get_X(GEN W, GEN xpm, long D)
    2929             : {
    2930         539 :   ulong a, d = (ulong)labs(D);
    2931         539 :   GEN t = gen_0;
    2932             :   GEN nc, c;
    2933         539 :   if (d == 1) return Q_xpm(W, xpm, gen_0);
    2934         238 :   nc = icopy(gen_1);
    2935         238 :   c = mkfrac(nc, utoipos(d));
    2936        2072 :   for (a=1; a < d; a++)
    2937             :   {
    2938        1834 :     long s = kross(D,a);
    2939             :     GEN x;
    2940        1834 :     if (!s) continue;
    2941        1344 :     nc[2] = a; x = Q_xpm(W, xpm, c);
    2942        1344 :     t = (s > 0)? addii(t, x): subii(t, x);
    2943             :   }
    2944         238 :   return t;
    2945             : }
    2946             : /* E of rank 0, minimal model; write L(E,1) = Q*w1(E) != 0 and return the
    2947             :  * rational Q; tam = product of all Tamagawa (incl. c_oo(E)). */
    2948             : static GEN
    2949         385 : get_Q(GEN E, GEN tam)
    2950             : {
    2951         385 :   GEN L, T, sha, w1 = gel(ellR_omega(E,DEFAULTPREC), 1);
    2952             :   long ex, t, t2;
    2953             : 
    2954         385 :   T = elltors(E); t = itos(gel(T,1)); t2 = t*t;
    2955         385 :   L = ellL1(E, 0, DEFAULTPREC);
    2956         385 :   sha = divrr(mulru(L, t2), mulri(w1,tam)); /* integral = |Sha| by BSD */
    2957         385 :   sha = sqri( grndtoi(sqrtr(sha), &ex) ); /* |Sha| is a square */
    2958         385 :   if (ex > -5) pari_err_BUG("msfromell (can't compute analytic |Sha|)");
    2959         385 :   return gdivgs(mulii(tam,sha), t2);
    2960             : }
    2961             : 
    2962             : /* E given by a minimal model; D != 0. Compare Euler factor of L(E,(D/.),1)
    2963             :  * with L(E^D,1). Return
    2964             :  *   \prod_{p|D} (p-a_p(E)+eps_{E}(p)) / p,
    2965             :  * where eps(p) = 0 if p | N_E and 1 otherwise */
    2966             : static GEN
    2967         385 : get_Euler(GEN E, long D)
    2968             : {
    2969         385 :   GEN t = gen_1, P = gel(factoru(labs(D)), 1);
    2970         385 :   GEN Delta = ell_get_disc(E); /* same prime divisors as N_E */
    2971         385 :   long i, l = lg(P);
    2972         560 :   for (i=1; i<l; i++)
    2973             :   {
    2974         175 :     long p = P[i];
    2975         175 :     long b = p - itos(ellap(E,utoipos(p))) + (dvdiu(Delta,p)?0L:1L);
    2976         175 :     t = gdivgs(gmulgs(t, b), p);
    2977             :   }
    2978         385 :   return t;
    2979             : }
    2980             : 
    2981             : /* E given by a minimal model, xpm in the sign(D) part with the same
    2982             :  * eigenvalues as E (unique up to multiplication with a rational).
    2983             :  * Let X(D) = \sum_{a <= |D|} (D/a) * xpm(E, a/|D|)
    2984             :  * Return the rational correction factor A such that
    2985             :  *   A * X(D) = L(E, (D/.), 1) / \Omega(E^D)
    2986             :  * for fundamental D (such that E^D has rank 0 otherwise both sides vanish). */
    2987             : static GEN
    2988         539 : ell_get_scale_d(GEN E, GEN W, GEN xpm, long D)
    2989             : {
    2990         539 :   GEN cb, N, Q, tam, u, Ed, X = get_X(W, xpm, D);
    2991             : 
    2992         539 :   if (!signe(X)) return NULL;
    2993         385 :   if (D == 1)
    2994         224 :     Ed = E;
    2995             :   else
    2996         161 :     Ed = ellinit(elltwist(E, stoi(D)), NULL, DEFAULTPREC);
    2997         385 :   Ed = ellanal_globalred_all(Ed, &cb, &N, &tam);
    2998         385 :   Q =  get_Q(Ed, tam);
    2999         385 :   if (cb)
    3000             :   { /* \tilde{u} in Pal's "Periods of quadratic twists of elliptic curves" */
    3001         182 :     u = gel(cb,1); /* Omega(E^D_min) = u * Omega(E^D) */
    3002         182 :     if (abscmpiu(Q_denom(u), 2) > 0) pari_err_BUG("msfromell [ell_get_scale]");
    3003         182 :     Q = gmul(Q,u);
    3004             :   }
    3005             :   /* L(E^D,1) = Q * w1(E^D_min) */
    3006         385 :   Q = gmul(Q, get_Euler(Ed, D));
    3007         385 :   if (D != 1) obj_free(Ed);
    3008             :   /* L(E^D,1) / Omega(E^D) = Q. Divide by X to get A */
    3009         385 :   return gdiv(Q, X);
    3010             : }
    3011             : 
    3012             : /* Let W = msinit(conductor(E), 2), xpm an integral modular symbol with the same
    3013             :  * eigenvalues as L_E. There exist a unique C such that
    3014             :  *   C*L(E,(D/.),1)_{xpm} = L(E,(D/.),1) / w1(E_D) != 0, for all D fundamental,
    3015             :  * sign(D) = s, and such that E_D has rank 0. Return the normalized symbol
    3016             :  * C * xpm */
    3017             : static GEN
    3018         385 : ell_get_scale(GEN E, GEN W, GEN xpm, long s)
    3019             : {
    3020             :   long d;
    3021             :   /* find D = s*d such that twist by D has rank 0 */
    3022        1106 :   for (d = 1; d < LONG_MAX; d++)
    3023             :   {
    3024        1106 :     pari_sp av = avma;
    3025             :     GEN C;
    3026        1106 :     long D = s > 0? d: -d;
    3027        1106 :     if (!sisfundamental(D)) continue;
    3028         539 :     C = ell_get_scale_d(E, W, xpm, D);
    3029         539 :     if (C) return RgC_Rg_mul(xpm, C);
    3030         154 :     avma = av;
    3031             :   }
    3032           0 :   pari_err_BUG("msfromell (no suitable twist)");
    3033           0 :   return NULL;
    3034             : }
    3035             : 
    3036             : /* v != 0 */
    3037             : static GEN
    3038         343 : Flc_normalize(GEN v, ulong p)
    3039             : {
    3040         343 :   long i, l = lg(v);
    3041         525 :   for (i = 1; i < l; i++)
    3042         525 :     if (v[i])
    3043             :     {
    3044         343 :       if (v[i] != 1) v = Flv_Fl_div(v, v[i], p);
    3045         343 :       return v;
    3046             :     }
    3047           0 :   return NULL;
    3048             : }
    3049             : 
    3050             : /* K \cap Ker M  [F_l vector spaces]. K = NULL means full space */
    3051             : static GEN
    3052         301 : msfromell_ker(GEN K, GEN M, ulong l)
    3053             : {
    3054         301 :   GEN B, Ml = ZM_to_Flm(M, l);
    3055         301 :   if (K) Ml = Flm_mul(Ml, K, l);
    3056         301 :   B = Flm_ker(Ml, l);
    3057         301 :   if (!K) K = B;
    3058           7 :   else if (lg(B) < lg(K))
    3059           7 :     K = Flm_mul(K, B, l);
    3060         301 :   return K;
    3061             : }
    3062             : /* K = \cap_p Ker(T_p - a_p), 2-dimensional. Set *xl to the 1-dimensional
    3063             :  * Fl-basis  such that star . xl = sign . xl if sign != 0 and
    3064             :  * star * xl[1] = xl[1]; star * xl[2] = -xl[2] if sign = 0 */
    3065             : static void
    3066         294 : msfromell_l(GEN *pxl, GEN K, GEN star, long sign, ulong l)
    3067             : {
    3068         294 :   GEN s = ZM_to_Flm(star, l);
    3069         294 :   GEN a = gel(K,1), Sa = Flm_Flc_mul(s,a,l);
    3070         294 :   GEN b = gel(K,2);
    3071         294 :   GEN t = Flv_add(a,Sa,l), xp, xm;
    3072         294 :   if (zv_equal0(t))
    3073             :   {
    3074          14 :     xm = a;
    3075          14 :     xp = Flv_add(b,Flm_Flc_mul(s,b,l), l);
    3076             :   }
    3077             :   else
    3078             :   {
    3079         280 :     xp = t; t = Flv_sub(a, Sa, l);
    3080         280 :     xm = zv_equal0(t)? Flv_sub(b, Flm_Flc_mul(s,b,l), l): t;
    3081             :   }
    3082             :   /* xp = 0 on Im(S - 1), xm = 0 on Im(S + 1) */
    3083         294 :   if (sign > 0)
    3084         231 :     *pxl = mkmat(Flc_normalize(xp, l));
    3085          63 :   else if (sign < 0)
    3086          14 :     *pxl = mkmat(Flc_normalize(xm, l));
    3087             :   else
    3088          49 :     *pxl = mkmat2(Flc_normalize(xp, l), Flc_normalize(xm, l));
    3089         294 : }
    3090             : static GEN
    3091         294 : msfromell_ratlift(GEN x, GEN q)
    3092             : {
    3093         294 :   GEN B = sqrti(shifti(q,-1));
    3094         294 :   GEN r = FpM_ratlift(x, q, B, B, NULL);
    3095         294 :   if (r) r = Q_primpart(r);
    3096         294 :   return r;
    3097             : }
    3098             : static int
    3099         294 : msfromell_check(GEN x, GEN vT, GEN star, long sign)
    3100             : {
    3101             :   long i, l;
    3102             :   GEN sx;
    3103         294 :   if (!x) return 0;
    3104         294 :   l = lg(vT);
    3105         595 :   for (i = 1; i < l; i++)
    3106             :   {
    3107         301 :     GEN T = gel(vT,i);
    3108         301 :     if (!gequal0(ZM_mul(T, x))) return 0; /* fail */
    3109             :   }
    3110         294 :   sx = ZM_mul(star,x);
    3111         294 :   if (sign)
    3112         245 :     return ZV_equal(gel(sx,1), sign > 0? gel(x,1): ZC_neg(gel(x,1)));
    3113             :   else
    3114          49 :     return ZV_equal(gel(sx,1),gel(x,1)) && ZV_equal(gel(sx,2),ZC_neg(gel(x,2)));
    3115             : }
    3116             : static GEN
    3117         315 : msfromell_scale(GEN E, GEN W, long sign, GEN x)
    3118             : {
    3119         315 :   if (sign)
    3120         245 :     x = ell_get_scale(E, W, gel(x,1), sign);
    3121             :   else
    3122             :   {
    3123          70 :     GEN p = ell_get_scale(E, W, gel(x,1), 1);
    3124          70 :     GEN m = ell_get_scale(E, W, gel(x,2),-1);
    3125          70 :     x = mkvec2(p,m);
    3126             :   }
    3127         315 :   return x;
    3128             : }
    3129             : GEN
    3130         294 : msfromell(GEN E0, long sign)
    3131             : {
    3132         294 :   pari_sp av = avma;
    3133         294 :   GEN E, cond, W, x = NULL, K = NULL, star, q, vT, xl, xr;
    3134             :   long lE, single;
    3135             :   ulong p, l, N;
    3136             :   forprime_t S, Sl;
    3137             : 
    3138         294 :   if (typ(E0) != t_VEC) pari_err_TYPE("msfromell",E0);
    3139         294 :   lE = lg(E0);
    3140         294 :   if (lE == 1) return cgetg(1,t_VEC);
    3141         294 :   single = (typ(gel(E0,1)) != t_VEC);
    3142         294 :   E = single ? E0: gel(E0,1);
    3143             : 
    3144         294 :   E = ellminimalmodel(E, NULL);
    3145         294 :   cond = ellQ_get_N(E);
    3146         294 :   N = itou(cond);
    3147         294 :   W = mskinit(N, 2, 0);
    3148         294 :   star = msk_get_star(W);
    3149         294 :   init_modular_small(&Sl);
    3150             :   /* loop for p <= count_Manin_symbols(N) / 6 would be enough */
    3151         294 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
    3152         294 :   vT = cgetg(1, t_VEC);
    3153         294 :   l = u_forprime_next(&Sl);
    3154         294 :   while( (p = u_forprime_next(&S)) )
    3155             :   {
    3156             :     GEN M;
    3157         343 :     if (N % p == 0) continue;
    3158         301 :     M = RgM_Rg_sub_shallow(mshecke_i(W, p), ellap(E, utoipos(p)));
    3159         301 :     vT = shallowconcat(vT, mkvec(M)); /* for certification at the end */
    3160         301 :     K = msfromell_ker(K, M, l);
    3161         301 :     if (lg(K) == 3) break;
    3162             :   }
    3163         294 :   if (!p) pari_err_BUG("msfromell: ran out of primes");
    3164             : 
    3165             :   /* mod one l should be enough */
    3166         294 :   msfromell_l(&xl, K, star, sign, l);
    3167         294 :   x = ZM_init_CRT(xl, l);
    3168         294 :   q = utoipos(l);
    3169         294 :   xr = msfromell_ratlift(x, q);
    3170             :   /* paranoia */
    3171         588 :   while (!msfromell_check(xr, vT, star, sign) && (l = u_forprime_next(&Sl)) )
    3172             :   {
    3173           0 :     GEN K = NULL;
    3174           0 :     long i, lvT = lg(vT);
    3175           0 :     for (i = 1; i < lvT; i++)
    3176             :     {
    3177           0 :       K = msfromell_ker(K, gel(vT,i), l);
    3178           0 :       if (lg(K) == 3) break;
    3179             :     }
    3180           0 :     if (i >= lvT) { x = NULL; continue; }
    3181           0 :     msfromell_l(&xl, K, star, sign, l);
    3182           0 :     ZM_incremental_CRT(&x, xl, &q, l);
    3183           0 :     xr = msfromell_ratlift(x, q);
    3184             :   }
    3185             :   /* linear form = 0 on all Im(Tp - ap) and Im(S - sign) if sign != 0 */
    3186             : 
    3187         294 :   if (single)
    3188         287 :     x = msfromell_scale(E, W, sign, xr);
    3189             :   else
    3190             :   {
    3191           7 :     GEN v = cgetg(lE, t_VEC);
    3192             :     long i;
    3193           7 :     for (i=1; i < lE; i++) gel(v,i) = msfromell_scale(gel(E0,i), W, sign, xr);
    3194           7 :     x = v;
    3195             :   }
    3196         294 :   return gerepilecopy(av, mkvec2(W, x));
    3197             : }
    3198             : 
    3199             : GEN
    3200          14 : msfromhecke(GEN W, GEN v, GEN H)
    3201             : {
    3202          14 :   pari_sp av = avma;
    3203          14 :   long i, l = lg(v);
    3204          14 :   GEN K = NULL;
    3205          14 :   checkms(W);
    3206          14 :   if (typ(v) != t_VEC) pari_err_TYPE("msfromhecke",v);
    3207          35 :   for (i = 1; i < l; i++)
    3208             :   {
    3209          21 :     GEN K2, T, p, P, c = gel(v,i);
    3210          21 :     if (typ(c) != t_VEC || lg(c) != 3) pari_err_TYPE("msfromhecke",v);
    3211          21 :     p = gel(c,1);
    3212          21 :     if (typ(p) != t_INT) pari_err_TYPE("msfromhecke",v);
    3213          21 :     P = gel(c,2);
    3214          21 :     switch(typ(P))
    3215             :     {
    3216             :       case t_INT:
    3217          14 :         P = deg1pol_shallow(gen_1, negi(P), 0);
    3218          14 :         break;
    3219             :       case t_POL:
    3220           7 :         if (RgX_is_ZX(P)) break;
    3221             :       default:
    3222           0 :         pari_err_TYPE("msfromhecke",v);
    3223             :     };
    3224          21 :     T = mshecke(W, itos(p), H);
    3225          21 :     T = Q_primpart(RgX_RgM_eval(P, T));
    3226          21 :     if (K) T = ZM_mul(T,K);
    3227          21 :     K2 = ZM_ker(T);
    3228          21 :     if (!K) K = K2;
    3229           7 :     else if (lg(K2) < lg(K)) K = ZM_mul(K,K2);
    3230             :   }
    3231          14 :   return gerepilecopy(av, K);
    3232             : }
    3233             : 
    3234             : /* OVERCONVERGENT MODULAR SYMBOLS */
    3235             : 
    3236             : static GEN
    3237        2765 : mspadic_get_Wp(GEN W) { return gel(W,1); }
    3238             : static GEN
    3239         455 : mspadic_get_Tp(GEN W) { return gel(W,2); }
    3240             : static GEN
    3241         455 : mspadic_get_bin(GEN W) { return gel(W,3); }
    3242             : static GEN
    3243         448 : mspadic_get_actUp(GEN W) { return gel(W,4); }
    3244             : static GEN
    3245         448 : mspadic_get_q(GEN W) { return gel(W,5); }
    3246             : static long
    3247        1372 : mspadic_get_p(GEN W) { return gel(W,6)[1]; }
    3248             : static long
    3249        1148 : mspadic_get_n(GEN W) { return gel(W,6)[2]; }
    3250             : static long
    3251         161 : mspadic_get_flag(GEN W) { return gel(W,6)[3]; }
    3252             : static GEN
    3253         455 : mspadic_get_M(GEN W) { return gel(W,7); }
    3254             : static GEN
    3255         455 : mspadic_get_C(GEN W) { return gel(W,8); }
    3256             : static long
    3257         917 : mspadic_get_weight(GEN W) { return msk_get_weight(mspadic_get_Wp(W)); }
    3258             : 
    3259             : void
    3260         924 : checkmspadic(GEN W)
    3261             : {
    3262         924 :   if (typ(W) != t_VEC || lg(W) != 9) pari_err_TYPE("checkmspadic",W);
    3263         924 :   checkms(mspadic_get_Wp(W));
    3264         924 : }
    3265             : 
    3266             : /* f in M_2(Z) \cap GL_2(Q), p \nmid a [ and for the result to mean anything
    3267             :  * p | c, but not needed here]. Return the matrix M in M_D(Z), D = M+k-1
    3268             :  * such that, if v = \int x^i d mu, i < D, is a vector of D moments of mu,
    3269             :  * then M * v is the vector of moments of mu | f  mod p^D */
    3270             : static GEN
    3271      252917 : moments_act(struct m_act *S, GEN f)
    3272             : {
    3273      252917 :   pari_sp av = avma;
    3274      252917 :   long j, k = S->k, D = S->dim;
    3275      252917 :   GEN a = gcoeff(f,1,1), b = gcoeff(f,1,2);
    3276      252917 :   GEN c = gcoeff(f,2,1), d = gcoeff(f,2,2);
    3277      252917 :   GEN u,z,C, q = S->q, mat = cgetg(D+1, t_MAT);
    3278             : 
    3279      252917 :   a = modii(a,q);
    3280      252917 :   z = FpX_powu(deg1pol(c,a,0), k-2, q); /* (a+cx)^(k-2) */
    3281             :   /* u := (b+dx) / (a+cx) mod (q,x^D) = (b/a +d/a*x) / (1 - (-c/a)*x) */
    3282      252917 :   if (!equali1(a))
    3283             :   {
    3284      248696 :     GEN ai = Fp_inv(a,q);
    3285      248696 :     b = Fp_mul(b,ai,q);
    3286      248696 :     c = Fp_mul(c,ai,q);
    3287      248696 :     d = Fp_mul(d,ai,q);
    3288             :   }
    3289      252917 :   u = cgetg(D+2,t_POL); u[1] = evalsigne(1)|evalvarn(0);
    3290      252917 :   gel(u, 2) = gen_1;
    3291      252917 :   gel(u, 3) = C = Fp_neg(c,q);
    3292      252917 :   for (j = 4; j < D+2; j++) gel(u,j) = Fp_mul(gel(u,j-1), C, q);
    3293      252917 :   u = FpX_red(RgXn_mul(deg1pol(d,b,0), u, D), q);
    3294     2120272 :   for (j = 1; j <= D; j++)
    3295             :   {
    3296     1867355 :     gel(mat,j) = RgX_to_RgC(z, D); /* (a+cx)^(k-2) * ((b+dx)/(a+cx))^(j-1) */
    3297     1867355 :     if (j != D) z = FpX_red(RgXn_mul(z, u, D), q);
    3298             :   }
    3299      252917 :   return gerepilecopy(av, shallowtrans(mat));
    3300             : }
    3301             : 
    3302             : static GEN
    3303         455 : init_moments_act(GEN W, long p, long n, GEN q, GEN v)
    3304             : {
    3305             :   struct m_act S;
    3306         455 :   long k = msk_get_weight(W);
    3307         455 :   S.p = p;
    3308         455 :   S.k = k;
    3309         455 :   S.q = q;
    3310         455 :   S.dim = n+k-1;
    3311         455 :   return init_dual_act(v,W,W,&S, moments_act);
    3312             : }
    3313             : 
    3314             : static void
    3315        6552 : clean_tail(GEN phi, long c, GEN q)
    3316             : {
    3317        6552 :   long a, l = lg(phi);
    3318      208418 :   for (a = 1; a < l; a++)
    3319             :   {
    3320      201866 :     GEN P = FpV_red(gel(phi,a), q); /* phi(G_a) = vector of moments */
    3321      201866 :     long j, lP = lg(P);
    3322      201866 :     for (j = c; j < lP; j++) gel(P,j) = gen_0; /* reset garbage to 0 */
    3323      201866 :     gel(phi,a) = P;
    3324             :   }
    3325        6552 : }
    3326             : /* concat z to all phi[i] */
    3327             : static GEN
    3328         602 : concat2(GEN phi, GEN z)
    3329             : {
    3330             :   long i, l;
    3331         602 :   GEN v = cgetg_copy(phi,&l);
    3332         602 :   for (i = 1; i < l; i++) gel(v,i) = shallowconcat(gel(phi,i), z);
    3333         602 :   return v;
    3334             : }
    3335             : static GEN
    3336         602 : red_mod_FilM(GEN phi, ulong p, long k, long flag)
    3337             : {
    3338             :   long a, l;
    3339         602 :   GEN den = gen_1, v = cgetg_copy(phi, &l);
    3340         602 :   if (flag)
    3341             :   {
    3342         343 :     phi = Q_remove_denom(phi, &den);
    3343         343 :     if (!den) { den = gen_1; flag = 0; }
    3344             :   }
    3345       28630 :   for (a = 1; a < l; a++)
    3346             :   {
    3347       28028 :     GEN P = gel(phi,a), q = den;
    3348             :     long j;
    3349      201866 :     for (j = lg(P)-1; j >= k+1; j--)
    3350             :     {
    3351      173838 :       q = muliu(q,p);
    3352      173838 :       gel(P,j) = modii(gel(P,j),q);
    3353             :     }
    3354       28028 :     q = muliu(q,p);
    3355       91196 :     for (     ; j >= 1; j--)
    3356       63168 :       gel(P,j) = modii(gel(P,j),q);
    3357       28028 :     gel(v,a) = P;
    3358             :   }
    3359         602 :   if (flag) v = gdiv(v, den);
    3360         602 :   return v;
    3361             : }
    3362             : 
    3363             : /* denom(C) | p^(2(k-1) - v_p(ap)) */
    3364             : static GEN
    3365         154 : oms_dim2(GEN W, GEN phi, GEN C, GEN ap)
    3366             : {
    3367         154 :   long t, i, k = mspadic_get_weight(W);
    3368         154 :   long p = mspadic_get_p(W), n = mspadic_get_n(W);
    3369         154 :   GEN phi1 = gel(phi,1), phi2 = gel(phi,2);
    3370         154 :   GEN v, q = mspadic_get_q(W);
    3371         154 :   GEN act = mspadic_get_actUp(W);
    3372             : 
    3373         154 :   t = signe(ap)? Z_lval(ap,p) : k-1;
    3374         154 :   phi1 = concat2(phi1, zerovec(n));
    3375         154 :   phi2 = concat2(phi2, zerovec(n));
    3376        2107 :   for (i = 1; i <= n; i++)
    3377             :   {
    3378        1953 :     phi1 = dual_act(k-1, act, phi1);
    3379        1953 :     phi1 = dual_act(k-1, act, phi1);
    3380        1953 :     clean_tail(phi1, k + i*t, q);
    3381             : 
    3382        1953 :     phi2 = dual_act(k-1, act, phi2);
    3383        1953 :     phi2 = dual_act(k-1, act, phi2);
    3384        1953 :     clean_tail(phi2, k + i*t, q);
    3385             :   }
    3386         154 :   C = gpowgs(C,n);
    3387         154 :   v = RgM_RgC_mul(C, mkcol2(phi1,phi2));
    3388         154 :   phi1 = red_mod_FilM(gel(v,1), p, k, 1);
    3389         154 :   phi2 = red_mod_FilM(gel(v,2), p, k, 1);
    3390         154 :   return mkvec2(phi1,phi2);
    3391             : }
    3392             : 
    3393             : /* flag = 0 iff alpha is a p-unit */
    3394             : static GEN
    3395         294 : oms_dim1(GEN W, GEN phi, GEN alpha, long flag)
    3396             : {
    3397         294 :   long i, k = mspadic_get_weight(W);
    3398         294 :   long p = mspadic_get_p(W), n = mspadic_get_n(W);
    3399         294 :   GEN q = mspadic_get_q(W);
    3400         294 :   GEN act = mspadic_get_actUp(W);
    3401         294 :   phi = concat2(phi, zerovec(n));
    3402        2940 :   for (i = 1; i <= n; i++)
    3403             :   {
    3404        2646 :     phi = dual_act(k-1, act, phi);
    3405        2646 :     clean_tail(phi, k + i, q);
    3406             :   }
    3407         294 :   phi = gmul(lift_shallow(gpowgs(alpha,n)), phi);
    3408         294 :   phi = red_mod_FilM(phi, p, k, flag);
    3409         294 :   return mkvec(phi);
    3410             : }
    3411             : 
    3412             : /* lift polynomial P in RgX[X,Y]_{k-2} to a distribution \mu such that
    3413             :  * \int (Y - X z)^(k-2) d\mu(z) = P(X,Y)
    3414             :  * Return the t_VEC of k-1 first moments of \mu: \int z^i d\mu(z), 0<= i < k-1.
    3415             :  *   \sum_j (-1)^(k-2-j) binomial(k-2,j) Y^j \int z^(k-2-j) d\mu(z) = P(1,Y)
    3416             :  * Input is P(1,Y), bin = vecbinome(k-2): bin[j] = binomial(k-2,j-1) */
    3417             : static GEN
    3418       37667 : RgX_to_moments(GEN P, GEN bin)
    3419             : {
    3420       37667 :   long j, k = lg(bin);
    3421             :   GEN Pd, Bd;
    3422       37667 :   if (typ(P) != t_POL) P = scalarpol(P,0);
    3423       37667 :   P = RgX_to_RgC(P, k-1); /* deg <= k-2 */
    3424       37667 :   settyp(P, t_VEC);
    3425       37667 :   Pd = P+1;  /* Pd[i] = coeff(P,i) */
    3426       37667 :   Bd = bin+1;/* Bd[i] = binomial(k-2,i) */
    3427       45290 :   for (j = 1; j < k-2; j++)
    3428             :   {
    3429        7623 :     GEN c = gel(Pd,j);
    3430        7623 :     if (odd(j)) c = gneg(c);
    3431        7623 :     gel(Pd,j) = gdiv(c, gel(Bd,j));
    3432             :   }
    3433       37667 :   return vecreverse(P);
    3434             : }
    3435             : static GEN
    3436         847 : RgXC_to_moments(GEN v, GEN bin)
    3437             : {
    3438             :   long i, l;
    3439         847 :   GEN w = cgetg_copy(v,&l);
    3440         847 :   for (i=1; i<l; i++) gel(w,i) = RgX_to_moments(gel(v,i),bin);
    3441         847 :   return w;
    3442             : }
    3443             : 
    3444             : /* W an mspadic, assume O[2] is integral, den is the cancelled denominator
    3445             :  * or NULL, L = log(path) */
    3446             : static GEN
    3447        2534 : omseval_int(struct m_act *S, GEN PHI, GEN L, hashtable *H)
    3448             : {
    3449             :   long a, lphi;
    3450        2534 :   GEN ind, v = cgetg_copy(PHI, &lphi);
    3451             : 
    3452        2534 :   L = RgV_sparse(L,&ind);
    3453        2534 :   ZSl2C_star_inplace(L); /* lambda_{i,j}^* */
    3454        2534 :   L = mkvec2(ind,L);
    3455        2534 :   ZGl2QC_to_act(S, moments_act, L, H); /* as operators on V */
    3456        5446 :   for (a = 1; a < lphi; a++)
    3457             :   {
    3458        2912 :     GEN T = dense_act_col(L, gel(PHI,a));
    3459        2912 :     if (T) T = FpC_red(T,S->q); else T = zerocol(S->dim);
    3460        2912 :     gel(v,a) = T;
    3461             :   }
    3462        2534 :   return v;
    3463             : }
    3464             : 
    3465             : GEN
    3466          14 : msomseval(GEN W, GEN phi, GEN path)
    3467             : {
    3468             :   struct m_act S;
    3469          14 :   pari_sp av = avma;
    3470             :   GEN v, Wp;
    3471             :   long n, vden;
    3472          14 :   checkmspadic(W);
    3473          14 :   if (typ(phi) != t_COL || lg(phi) != 4)  pari_err_TYPE("msomseval",phi);
    3474          14 :   vden = itos(gel(phi,2));
    3475          14 :   phi = gel(phi,1);
    3476          14 :   n = mspadic_get_n(W);
    3477          14 :   Wp= mspadic_get_Wp(W);
    3478          14 :   S.k = mspadic_get_weight(W);
    3479          14 :   S.p = mspadic_get_p(W);
    3480          14 :   S.q = powuu(S.p, n+vden);
    3481          14 :   S.dim = n + S.k - 1;
    3482          14 :   v = omseval_int(&S, phi, mspathlog(Wp,path), NULL);
    3483          14 :   return gerepilecopy(av, v);
    3484             : }
    3485             : /* W = msinit(N,k,...); if flag < 0 or flag >= k-1, allow all symbols;
    3486             :  * else commit to v_p(a_p) <= flag (ordinary if flag = 0)*/
    3487             : GEN
    3488         462 : mspadicinit(GEN W, long p, long n, long flag)
    3489             : {
    3490         462 :   pari_sp av = avma;
    3491             :   long a, N, k;
    3492             :   GEN P, C, M, bin, Wp, Tp, q, pn, actUp, teich, pas;
    3493             : 
    3494         462 :   checkms(W);
    3495         462 :   N = ms_get_N(W);
    3496         462 :   k = msk_get_weight(W);
    3497         462 :   if (flag < 0) flag = 1; /* worst case */
    3498         343 :   else if (flag >= k) flag = k-1;
    3499             : 
    3500         462 :   bin = vecbinome(k-2);
    3501         462 :   Tp = mshecke(W, p, NULL);
    3502         462 :   if (N % p == 0)
    3503             :   {
    3504          70 :     if ((N/p) % p == 0) pari_err_IMPL("mspadicinit when p^2 | N");
    3505             :     /* a_p != 0 */
    3506          63 :     Wp = W;
    3507          63 :     M = gen_0;
    3508          63 :     flag = (k-2) / 2; /* exact valuation */
    3509             :     /* will multiply by matrix with denominator p^(k-2)/2 in mspadicint.
    3510             :      * Except if p = 2 (multiply by alpha^2) */
    3511          63 :     if (p == 2) n += k-2; else n += (k-2)/2;
    3512          63 :     pn = powuu(p,n);
    3513             :     /* For accuracy mod p^n, oms_dim1 require p^(k/2*n) */
    3514          63 :     q = powiu(pn, k/2);
    3515             :   }
    3516             :   else
    3517             :   { /* p-stabilize */
    3518         392 :     long s = msk_get_sign(W);
    3519             :     GEN M1, M2;
    3520             : 
    3521         392 :     Wp = mskinit(N*p, k, s);
    3522         392 :     M1 = getMorphism(W, Wp, mkvec(mat2(1,0,0,1)));
    3523         392 :     M2 = getMorphism(W, Wp, mkvec(mat2(p,0,0,1)));
    3524         392 :     if (s)
    3525             :     {
    3526         147 :       GEN SW = msk_get_starproj(W), SWp = msk_get_starproj(Wp);
    3527         147 :       M1 = Qevproj_apply2(M1, SW, SWp);
    3528         147 :       M2 = Qevproj_apply2(M2, SW, SWp);
    3529             :     }
    3530         392 :     M = mkvec2(M1,M2);
    3531         392 :     n += Z_lval(Q_denom(M), p); /*den. introduced by p-stabilization*/
    3532             :     /* in supersingular case: will multiply by matrix with denominator p^k
    3533             :      * in mspadicint. Except if p = 2 (multiply by alpha^2) */
    3534         392 :     if (flag) { if (p == 2) n += 2*k-2; else n += k; }
    3535         392 :     pn = powuu(p,n);
    3536             :     /* For accuracy mod p^n, supersingular require p^((2k-1-v_p(a_p))*n) */
    3537         392 :     if (flag) /* k-1 also takes care of a_p = 0. Worst case v_p(a_p) = flag */
    3538         231 :       q = powiu(pn, 2*k-1 - flag);
    3539             :     else
    3540         161 :       q = pn;
    3541             :   }
    3542         455 :   actUp = init_moments_act(Wp, p, n, q, Up_matrices(p));
    3543             : 
    3544         455 :   if (p == 2) C = gen_0;
    3545             :   else
    3546             :   {
    3547         399 :     pas = matpascal(n);
    3548         399 :     teich = teichmullerinit(p, n+1);
    3549         399 :     P = gpowers(utoipos(p), n);
    3550         399 :     C = cgetg(p, t_VEC);
    3551        1911 :     for (a = 1; a < p; a++)
    3552             :     { /* powb[j+1] = ((a - w(a)) / p)^j mod p^n */
    3553        1512 :       GEN powb = Fp_powers(diviuexact(subui(a, gel(teich,a)), p), n, pn);
    3554        1512 :       GEN Ca = cgetg(n+2, t_VEC);
    3555        1512 :       long j, r, ai = Fl_inv(a, p); /* a^(-1) */
    3556        1512 :       gel(C,a) = Ca;
    3557       18018 :       for (j = 0; j <= n; j++)
    3558             :       {
    3559       16506 :         GEN Caj = cgetg(j+2, t_VEC);
    3560       16506 :         GEN atij = gel(teich, Fl_powu(ai,j,p));/* w(a)^(-j) = w(a^(-j) mod p) */
    3561       16506 :         gel(Ca,j+1) = Caj;
    3562      133294 :         for (r = 0; r <= j; r++)
    3563             :         {
    3564      116788 :           GEN c = Fp_mul(gcoeff(pas,j+1,r+1), gel(powb, j-r+1), pn);
    3565      116788 :           c = Fp_mul(c,atij,pn); /* binomial(j,r)*b^(j-r)*w(a)^(-j) mod p^n */
    3566      116788 :           gel(Caj,r+1) = mulii(c, gel(P,j+1)); /* p^j * c mod p^(n+j) */
    3567             :         }
    3568             :       }
    3569             :     }
    3570             :   }
    3571         455 :   return gerepilecopy(av, mkvecn(8, Wp,Tp, bin, actUp, q,
    3572             :                                  mkvecsmall3(p,n,flag), M, C));
    3573             : }
    3574             : 
    3575             : #if 0
    3576             : /* assume phi an ordinary OMS */
    3577             : static GEN
    3578             : omsactgl2(GEN W, GEN phi, GEN M)
    3579             : {
    3580             :   GEN q, Wp, act;
    3581             :   long p, k, n;
    3582             :   checkmspadic(W);
    3583             :   Wp = mspadic_get_Wp(W);
    3584             :   p = mspadic_get_p(W);
    3585             :   k = mspadic_get_weight(W);
    3586             :   n = mspadic_get_n(W);
    3587             :   q = mspadic_get_q(W);
    3588             :   act = init_moments_act(Wp, p, n, q, M);
    3589             :   phi = gel(phi,1);
    3590             :   return dual_act(k-1, act, gel(phi,1));
    3591             : }
    3592             : #endif
    3593             : 
    3594             : static GEN
    3595         455 : eigenvalue(GEN T, GEN x)
    3596             : {
    3597         455 :   long i, l = lg(x);
    3598         581 :   for (i = 1; i < l; i++)
    3599         581 :     if (!isintzero(gel(x,i))) break;
    3600         455 :   if (i == l) pari_err_DOMAIN("mstooms", "phi", "=", gen_0, x);
    3601         455 :   return gdiv(RgMrow_RgC_mul(T,x,i), gel(x,i));
    3602             : }
    3603             : 
    3604             : /* p coprime to ap, return unit root of x^2 - ap*x + p^(k-1), accuracy p^n */
    3605             : static GEN
    3606         231 : ms_unit_eigenvalue(GEN ap, long k, GEN p, long n)
    3607             : {
    3608         231 :   GEN sqrtD, D = subii(sqri(ap), shifti(powiu(p,k-1),2));
    3609         231 :   if (absequaliu(p,2))
    3610             :   {
    3611           7 :     n++; sqrtD = Zp_sqrt(D, p, n);
    3612           7 :     if (mod4(sqrtD) != mod4(ap)) sqrtD = negi(sqrtD);
    3613             :   }
    3614             :   else
    3615         224 :     sqrtD = Zp_sqrtlift(D, ap, p, n);
    3616             :   /* sqrtD = ap (mod p) */
    3617         231 :   return gmul2n(gadd(ap, cvtop(sqrtD,p,n)), -1);
    3618             : }
    3619             : 
    3620             : /* W = msinit(N,k,...); phi = T_p/U_p - eigensymbol */
    3621             : GEN
    3622         455 : mstooms(GEN W, GEN phi)
    3623             : {
    3624         455 :   pari_sp av = avma;
    3625             :   GEN Wp, bin, Tp, c, alpha, ap, phi0, M;
    3626             :   long k, p, vden;
    3627             : 
    3628         455 :   checkmspadic(W);
    3629         455 :   if (typ(phi) != t_COL)
    3630             :   {
    3631         161 :     if (!is_Qevproj(phi)) pari_err_TYPE("mstooms",phi);
    3632         161 :     phi = gel(phi,1);
    3633         161 :     if (lg(phi) != 2) pari_err_TYPE("mstooms [dim_Q (eigenspace) > 1]",phi);
    3634         161 :     phi = gel(phi,1);
    3635             :   }
    3636             : 
    3637         455 :   Wp = mspadic_get_Wp(W);
    3638         455 :   Tp = mspadic_get_Tp(W);
    3639         455 :   bin = mspadic_get_bin(W);
    3640         455 :   k = msk_get_weight(Wp);
    3641         455 :   p = mspadic_get_p(W);
    3642         455 :   M = mspadic_get_M(W);
    3643             : 
    3644         455 :   phi = Q_remove_denom(phi, &c);
    3645         455 :   ap = eigenvalue(Tp, phi);
    3646         455 :   vden = c? Z_lvalrem(c, p, &c): 0;
    3647             : 
    3648         455 :   if (typ(M) == t_INT)
    3649             :   { /* p | N */
    3650             :     GEN c1;
    3651          63 :     alpha = ap;
    3652          63 :     alpha = ginv(alpha);
    3653          63 :     phi0 = mseval(Wp, phi, NULL);
    3654          63 :     phi0 = RgXC_to_moments(phi0, bin);
    3655          63 :     phi0 = Q_remove_denom(phi0, &c1);
    3656          63 :     if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3657          63 :     if (umodiu(ap,p)) /* p \nmid a_p */
    3658          28 :       phi = oms_dim1(W, phi0, alpha, 0);
    3659             :     else
    3660             :     {
    3661          35 :       phi = oms_dim1(W, phi0, alpha, 1);
    3662          35 :       phi = Q_remove_denom(phi, &c1);
    3663          35 :       if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3664             :     }
    3665             :   }
    3666             :   else
    3667             :   { /* p-stabilize */
    3668             :     GEN M1, M2, phi1, phi2, c1;
    3669         392 :     if (typ(M) != t_VEC || lg(M) != 3) pari_err_TYPE("mstooms",W);
    3670         392 :     M1 = gel(M,1);
    3671         392 :     M2 = gel(M,2);
    3672             : 
    3673         392 :     phi1 = RgM_RgC_mul(M1, phi);
    3674         392 :     phi2 = RgM_RgC_mul(M2, phi);
    3675         392 :     phi1 = mseval(Wp, phi1, NULL);
    3676         392 :     phi2 = mseval(Wp, phi2, NULL);
    3677             : 
    3678         392 :     phi1 = RgXC_to_moments(phi1, bin);
    3679         392 :     phi2 = RgXC_to_moments(phi2, bin);
    3680         392 :     phi = Q_remove_denom(mkvec2(phi1,phi2), &c1);
    3681         392 :     phi1 = gel(phi,1);
    3682         392 :     phi2 = gel(phi,2);
    3683         392 :     if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3684             :     /* all polynomials multiplied by c p^vden */
    3685         392 :     if (umodiu(ap, p))
    3686             :     {
    3687         231 :       alpha = ms_unit_eigenvalue(ap, k, utoipos(p), mspadic_get_n(W));
    3688         231 :       alpha = ginv(alpha);
    3689         231 :       phi0 = gsub(phi1, gmul(lift_shallow(alpha),phi2));
    3690         231 :       phi = oms_dim1(W, phi0, alpha, 0);
    3691             :     }
    3692             :     else
    3693             :     { /* p | ap, alpha = [a_p, -1; p^(k-1), 0] */
    3694         161 :       long flag = mspadic_get_flag(W);
    3695         161 :       if (!flag || (signe(ap) && Z_lval(ap,p) < flag))
    3696           7 :         pari_err_TYPE("mstooms [v_p(ap) > mspadicinit flag]", phi);
    3697         154 :       alpha = mkmat22(ap,gen_m1, powuu(p, k-1),gen_0);
    3698         154 :       alpha = ginv(alpha);
    3699         154 :       phi = oms_dim2(W, mkvec2(phi1,phi2), gsqr(alpha), ap);
    3700         154 :       phi = Q_remove_denom(phi, &c1);
    3701         154 :       if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3702             :     }
    3703             :   }
    3704         448 :   if (vden) c = mul_denom(c, powuu(p,vden));
    3705         448 :   if (p == 2) alpha = gsqr(alpha);
    3706         448 :   if (c) alpha = gdiv(alpha,c);
    3707         448 :   if (typ(alpha) == t_MAT)
    3708             :   { /* express in basis (omega,-p phi(omega)) */
    3709         154 :     gcoeff(alpha,2,1) = gdivgs(gcoeff(alpha,2,1), -p);
    3710         154 :     gcoeff(alpha,2,2) = gdivgs(gcoeff(alpha,2,2), -p);
    3711             :     /* at the end of mspadicint we shall multiply result by [1,0;0,-1/p]*alpha
    3712             :      * vden + k is the denominator of this matrix */
    3713             :   }
    3714             :   /* phi is integral-valued */
    3715         448 :   return gerepilecopy(av, mkcol3(phi, stoi(vden), alpha));
    3716             : }
    3717             : 
    3718             : /* HACK: the v[j] have different lengths */
    3719             : static GEN
    3720        1778 : FpVV_dotproduct(GEN v, GEN w, GEN p)
    3721             : {
    3722        1778 :   long j, l = lg(v);
    3723        1778 :   GEN T = cgetg(l, t_VEC);
    3724        1778 :   for (j = 1; j < l; j++) gel(T,j) = FpV_dotproduct(gel(v,j),w,p);
    3725        1778 :   return T;
    3726             : }
    3727             : 
    3728             : /* \int (-4z)^j given \int z^j */
    3729             : static GEN
    3730          98 : twistmoment_minus(GEN v)
    3731             : {
    3732             :   long i, l;
    3733          98 :   GEN w = cgetg_copy(v, &l);
    3734        2009 :   for (i = 1; i < l; i++)
    3735             :   {
    3736        1911 :     GEN c = gel(v,i);
    3737        1911 :     if (i > 1) c = gmul2n(c, (i-1)<<1);
    3738        1911 :     gel(w,i) = odd(i)? c: gneg(c);
    3739             :   }
    3740          98 :   return w;
    3741             : }
    3742             : /* \int (4z)^j given \int z^j */
    3743             : static GEN
    3744          98 : twistmoment_plus(GEN v)
    3745             : {
    3746             :   long i, l;
    3747          98 :   GEN w = cgetg_copy(v, &l);
    3748        2009 :   for (i = 1; i < l; i++)
    3749             :   {
    3750        1911 :     GEN c = gel(v,i);
    3751        1911 :     if (i > 1) c = gmul2n(c, (i-1)<<1);
    3752        1911 :     gel(w,i) = c;
    3753             :   }
    3754          98 :   return w;
    3755             : }
    3756             : /* W an mspadic, phi eigensymbol, p \nmid D. Return C(x) mod FilM */
    3757             : GEN
    3758         455 : mspadicmoments(GEN W, GEN PHI, long D)
    3759             : {
    3760         455 :   pari_sp av = avma;
    3761         455 :   long la, ia, b, lphi, aD = labs(D), pp, p, k, n, vden;
    3762             :   GEN Wp, Dact, Dk, v, C, gp, pn, phi;
    3763             :   struct m_act S;
    3764             :   hashtable *H;
    3765             : 
    3766         455 :   checkmspadic(W);
    3767         455 :   Wp = mspadic_get_Wp(W);
    3768         455 :   p = mspadic_get_p(W);
    3769         455 :   k = mspadic_get_weight(W);
    3770         455 :   n = mspadic_get_n(W);
    3771         455 :   C = mspadic_get_C(W);
    3772         455 :   if (typ(PHI) != t_COL || lg(PHI) != 4 || typ(gel(PHI,1)) != t_VEC)
    3773         448 :     PHI = mstooms(W, PHI);
    3774         448 :   vden = itos( gel(PHI,2) );
    3775         448 :   phi = gel(PHI,1);
    3776         448 :   if (p == 2)
    3777          56 :   { la = 3; pp = 4; }
    3778             :   else
    3779         392 :   { la = p; pp = p; }
    3780         448 :   v = cgetg_copy(phi, &lphi);
    3781         448 :   for (b = 1; b < lphi; b++) gel(v,b) = cgetg(la, t_VEC);
    3782         448 :   pn = powuu(p, n + vden);
    3783         448 :   gp = utoipos(p);
    3784             : 
    3785         448 :   S.p = p;
    3786         448 :   S.k = k;
    3787         448 :   S.q = pn;
    3788         448 :   S.dim = n+k-1;
    3789             : 
    3790         448 :   Dact = NULL;
    3791         448 :   Dk = NULL;
    3792         448 :   if (D != 1)
    3793             :   {
    3794          56 :     GEN gaD = utoi(aD);
    3795          56 :     if (!sisfundamental(D)) pari_err_TYPE("mspadicmoments", stoi(D));
    3796          56 :     if (D % p == 0) pari_err_DOMAIN("mspadicmoments", "p","|", stoi(D), gp);
    3797          56 :     Dact = cgetg(aD, t_VEC);
    3798         504 :     for (b = 1; b < aD; b++)
    3799             :     {
    3800         448 :       GEN z = NULL;
    3801         448 :       if (ugcd(b,aD) == 1)
    3802         448 :         z = moments_act(&S, mkmat22(gaD,utoipos(b), gen_0,gaD));
    3803         448 :       gel(Dact,b) = z;
    3804             :     }
    3805          56 :     if (k != 2) Dk = Fp_pows(stoi(D), 2-k, pn);
    3806             :   }
    3807             : 
    3808         448 :   H = Gl2act_cache(ms_get_nbgen(Wp));
    3809             : 
    3810        2058 :   for (ia = 1; ia < la; ia++)
    3811             :   {
    3812             :     GEN path, vca;
    3813        1610 :     long i, a = ia;
    3814        1610 :     if (p == 2 && a == 2) a = -1;
    3815        1610 :     if (Dact) /* twist by D */
    3816             :     {
    3817             :       long c;
    3818         182 :       vca = const_vec(lphi-1,NULL);
    3819        1274 :       for (b = 1; b < aD; b++)
    3820             :       {
    3821        1092 :         long s = kross(D, b);
    3822             :         GEN z, T;
    3823        1092 :         if (!s) continue;
    3824        1092 :         z = addii(mulss(a, aD), muluu(pp, b));
    3825             :         /* oo -> a/pp + pp/|D|*/
    3826        1092 :         path = mkmat22(gen_1,z, gen_0,muluu(pp, aD));
    3827        1092 :         T = omseval_int(&S, phi, M2_log(Wp,path), H);
    3828        2184 :         for (c = 1; c < lphi; c++)
    3829             :         {
    3830        1092 :           z = FpM_FpC_mul(gel(Dact,b), gel(T,c), pn);
    3831        1092 :           if (s < 0) ZV_neg_inplace(z);
    3832        1092 :           gel(vca, c) = gel(vca,c)? ZC_add(gel(vca,c), z): z;
    3833             :         }
    3834             :       }
    3835         252 :       if (Dk) for(c = 1; c < lphi; c++)
    3836          70 :         gel(vca,c) = FpC_Fp_mul(gel(vca,c), Dk, pn);
    3837             :     }
    3838             :     else
    3839             :     {
    3840        1428 :       path = mkmat22(gen_1,stoi(a), gen_0, utoipos(pp));
    3841        1428 :       vca = omseval_int(&S, phi, M2_log(Wp,path), H);
    3842             :     }
    3843        1610 :     if (p != 2)
    3844             :     {
    3845        1498 :       GEN Ca = gel(C,a);
    3846        3276 :       for (i = 1; i < lphi; i++)
    3847        1778 :         gmael(v,i,a) = FpVV_dotproduct(Ca, gel(vca,i), pn);
    3848             :     }
    3849             :     else
    3850             :     {
    3851         112 :       if (ia == 1) /* \tilde{a} = 1 */
    3852          56 :       { for (i = 1; i < lphi; i++) gel(vca,i) = twistmoment_plus(gel(vca,i)); }
    3853             :       else /* \tilde{a} = -1 */
    3854          56 :       { for (i = 1; i < lphi; i++) gel(vca,i) = twistmoment_minus(gel(vca,i)); }
    3855         112 :       for (i = 1; i < lphi; i++) gmael(v,i,ia) = gel(vca,i);
    3856             :     }
    3857             :   }
    3858         448 :   return gerepilecopy(av, mkvec3(v, gel(PHI,3), mkvecsmall4(p,n+vden,n,D)));
    3859             : }
    3860             : static void
    3861        1855 : checkoms(GEN v)
    3862             : {
    3863        1855 :   if (typ(v) != t_VEC || lg(v) != 4 || typ(gel(v,1)) != t_VEC
    3864        1855 :       || typ(gel(v,3))!=t_VECSMALL)
    3865           0 :     pari_err_TYPE("checkoms [apply mspadicmoments]", v);
    3866        1855 : }
    3867             : static long
    3868        4158 : oms_get_p(GEN oms) { return gel(oms,3)[1]; }
    3869             : static long
    3870        4060 : oms_get_n(GEN oms) { return gel(oms,3)[2]; }
    3871             : static long
    3872        2401 : oms_get_n0(GEN oms) { return gel(oms,3)[3]; }
    3873             : static long
    3874        1855 : oms_get_D(GEN oms) { return gel(oms,3)[4]; }
    3875             : static int
    3876          98 : oms_is_supersingular(GEN oms) { GEN v = gel(oms,1); return lg(v) == 3; }
    3877             : 
    3878             : /* sum(j = 1, n, (-1)^(j+1)/j * x^j) */
    3879             : static GEN
    3880         749 : log1x(long n)
    3881             : {
    3882         749 :   long i, l = n+3;
    3883         749 :   GEN v = cgetg(l, t_POL);
    3884         749 :   v[1] = evalvarn(0)|evalsigne(1); gel(v,2) = gen_0;
    3885         749 :   for (i = 3; i < l; i++) gel(v,i) = ginv(stoi(odd(i)? i-2: 2-i));
    3886         749 :   return v;
    3887             : }
    3888             : 
    3889             : /* S = (1+x)^zk log(1+x)^logj (mod x^(n+1)) */
    3890             : static GEN
    3891        1757 : xlog1x(long n, long zk, long logj, long *pteich)
    3892             : {
    3893        1757 :   GEN S = logj? RgXn_powu_i(log1x(n), logj, n+1): NULL;
    3894        1757 :   if (zk)
    3895             :   {
    3896        1183 :     GEN L = deg1pol_shallow(gen_1, gen_1, 0); /* x+1 */
    3897        1183 :     *pteich += zk;
    3898        1183 :     if (zk < 0) { L = RgXn_inv(L,n+1); zk = -zk; }
    3899        1183 :     if (zk != 1) L = RgXn_powu_i(L, zk, n+1);
    3900        1183 :     S = S? RgXn_mul(S, L, n+1): L;
    3901             :   }
    3902        1757 :   return S;
    3903             : }
    3904             : 
    3905             : /* oms from mspadicmoments; integrate teichmuller^i * S(x) [S = NULL: 1]*/
    3906             : static GEN
    3907        2303 : mspadicint(GEN oms, long teichi, GEN S)
    3908             : {
    3909        2303 :   pari_sp av = avma;
    3910        2303 :   long p = oms_get_p(oms), n = oms_get_n(oms), n0 = oms_get_n0(oms);
    3911        2303 :   GEN vT = gel(oms,1), alpha = gel(oms,2), gp = utoipos(p);
    3912        2303 :   long loss = S? Z_lval(Q_denom(S), p): 0;
    3913        2303 :   long nfinal = minss(n-loss, n0);
    3914        2303 :   long i, la, l = lg(vT);
    3915        2303 :   GEN res = cgetg(l, t_COL), teich = NULL;
    3916             : 
    3917        2303 :   if (S) S = RgX_to_RgC(S,lg(gmael(vT,1,1))-1);
    3918        2303 :   if (p == 2)
    3919             :   {
    3920         448 :     la = 3; /* corresponds to [1,-1] */
    3921         448 :     teichi &= 1;
    3922             :   }
    3923             :   else
    3924             :   {
    3925        1855 :     la = p; /* corresponds to [1,2,...,p-1] */
    3926        1855 :     teichi = smodss(teichi, p-1);
    3927        1855 :     if (teichi) teich = teichmullerinit(p, n);
    3928             :   }
    3929        5320 :   for (i=1; i<l; i++)
    3930             :   {
    3931        3017 :     pari_sp av2 = avma;
    3932        3017 :     GEN s = gen_0, T = gel(vT,i);
    3933             :     long ia;
    3934       13895 :     for (ia = 1; ia < la; ia++)
    3935             :     { /* Ta[j+1] correct mod p^n */
    3936       10878 :       GEN Ta = gel(T,ia), v = S? RgV_dotproduct(Ta, S): gel(Ta,1);
    3937       10878 :       if (teichi && ia != 1)
    3938             :       {
    3939        3843 :         if (p != 2)
    3940        3626 :           v = gmul(v, gel(teich, Fl_powu(ia,teichi,p)));
    3941             :         else
    3942         217 :           if (teichi) v = gneg(v);
    3943             :       }
    3944       10878 :       s = gadd(s, v);
    3945             :     }
    3946        3017 :     s = gadd(s, zeropadic(gp,nfinal));
    3947        3017 :     gel(res,i) = gerepileupto(av2, s);
    3948             :   }
    3949        2303 :   return gerepileupto(av, gmul(alpha, res));
    3950             : }
    3951             : /* integrate P = polynomial in log(x); vlog[j+1] = mspadicint(0,log(1+x)^j) */
    3952             : static GEN
    3953         539 : mspadicint_RgXlog(GEN P, GEN vlog)
    3954             : {
    3955         539 :   long i, d = degpol(P);
    3956         539 :   GEN s = gmul(gel(P,2), gel(vlog,1));
    3957         539 :   for (i = 1; i <= d; i++) s = gadd(s, gmul(gel(P,i+2), gel(vlog,i+1)));
    3958         539 :   return s;
    3959             : };
    3960             : 
    3961             : /* oms from mspadicmoments */
    3962             : GEN
    3963          98 : mspadicseries(GEN oms, long teichi)
    3964             : {
    3965          98 :   pari_sp av = avma;
    3966             :   GEN S, L, X, vlog, s, s2, u, logu, bin;
    3967             :   long j, p, m, n, step, stop;
    3968          98 :   checkoms(oms);
    3969          98 :   n = oms_get_n0(oms);
    3970          98 :   if (n < 1)
    3971             :   {
    3972           0 :     s = zeroser(0,0);
    3973           0 :     if (oms_is_supersingular(oms)) s = mkvec2(s,s);
    3974           0 :     return gerepilecopy(av, s);
    3975             :   }
    3976          98 :   p = oms_get_p(oms);
    3977          98 :   vlog = cgetg(n+1, t_VEC);
    3978          98 :   step = p == 2? 2: 1;
    3979          98 :   stop = 0;
    3980          98 :   S = NULL;
    3981          98 :   L = log1x(n);
    3982         644 :   for (j = 0; j < n; j++)
    3983             :   {
    3984         616 :     if (j) stop += step + u_lval(j,p); /* = step*j + v_p(j!) */
    3985         616 :     if (stop >= n) break;
    3986             :     /* S = log(1+x)^j */
    3987         546 :     gel(vlog,j+1) = mspadicint(oms,teichi,S);
    3988         546 :     S = S? RgXn_mul(S, L, n+1): L;
    3989             :   }
    3990          98 :   m = j;
    3991          98 :   u = utoipos(p == 2? 5: 1+p);
    3992          98 :   logu = glog(cvtop(u, utoipos(p), 4*m), 0);
    3993          98 :   X = gdiv(pol_x(0), logu);
    3994          98 :   s = cgetg(m+1, t_VEC);
    3995          98 :   s2 = oms_is_supersingular(oms)? cgetg(m+1, t_VEC): NULL;
    3996          98 :   bin = pol_1(0);
    3997         539 :   for (j = 0; j < m; j++)
    3998             :   { /* bin = binomial(x/log(1+p+O(p^(4*n))), j) mod x^m */
    3999         539 :     GEN a, v = mspadicint_RgXlog(bin, vlog);
    4000         539 :     int done = 1;
    4001         539 :     gel(s,j+1) = a = gel(v,1);
    4002         539 :     if (!gequal0(a) || valp(a) > 0) done = 0; else setlg(s,j+1);
    4003         539 :     if (s2)
    4004             :     {
    4005         119 :       gel(s2,j+1) = a = gel(v,2);
    4006         119 :       if (!gequal0(a) || valp(a) > 0) done = 0; else setlg(s2,j+1);
    4007             :     }
    4008         539 :     if (done || j == m-1) break;
    4009         441 :     bin = RgXn_mul(bin, gdivgs(gsubgs(X, j), j+1), m);
    4010             :   }
    4011          98 :   s = gtoser(s,0,lg(s)-1);
    4012          98 :   if (s2) { s2 = gtoser(s2,0,lg(s2)-1); s = mkvec2(s, s2); }
    4013          98 :   if (kross(oms_get_D(oms), p) >= 0) return gerepilecopy(av, s);
    4014           7 :   return gerepileupto(av, gneg(s));
    4015             : }
    4016             : static void
    4017        1820 : parse_chi(GEN s, GEN *s1, GEN *s2)
    4018             : {
    4019        1820 :   if (!s) *s1 = *s2 = gen_0;
    4020        1687 :   else switch(typ(s))
    4021             :   {
    4022        1183 :     case t_INT: *s1 = *s2 = s; break;
    4023             :     case t_VEC:
    4024         504 :       if (lg(s) == 3)
    4025             :       {
    4026         504 :         *s1 = gel(s,1);
    4027         504 :         *s2 = gel(s,2);
    4028         504 :         if (typ(*s1) == t_INT && typ(*s2) == t_INT) break;
    4029             :       }
    4030           0 :     default: pari_err_TYPE("mspadicL",s);
    4031           0 :              *s1 = *s2 = NULL;
    4032             :   }
    4033        1820 : }
    4034             : /* oms from mspadicmoments
    4035             :  * r-th derivative of L(f,chi^s,psi) in direction <chi>
    4036             :    - s \in Z_p \times \Z/(p-1)\Z, s-> chi^s=<\chi>^s_1 omega^s_2)
    4037             :    - Z -> Z_p \times \Z/(p-1)\Z par s-> (s, s mod p-1).
    4038             :  */
    4039             : GEN
    4040        1757 : mspadicL(GEN oms, GEN s, long r)
    4041             : {
    4042        1757 :   pari_sp av = avma;
    4043             :   GEN s1, s2, z, S;
    4044             :   long p, n, teich;
    4045        1757 :   checkoms(oms);
    4046        1757 :   p = oms_get_p(oms);
    4047        1757 :   n = oms_get_n(oms);
    4048        1757 :   parse_chi(s, &s1,&s2);
    4049        1757 :   teich = umodiu(subii(s2,s1), p==2? 2: p-1);
    4050        1757 :   S = xlog1x(n, itos(s1), r, &teich);
    4051        1757 :   z = mspadicint(oms, teich, S);
    4052        1757 :   if (lg(z) == 2) z = gel(z,1);
    4053        1757 :   if (kross(oms_get_D(oms), p) < 0) z = gneg(z);
    4054        1757 :   return gerepilecopy(av, z);
    4055             : }
    4056             : 
    4057             : GEN
    4058          63 : ellpadicL(GEN E, GEN pp, long n, GEN s, long r, GEN DD)
    4059             : {
    4060          63 :   pari_sp av = avma;
    4061             :   GEN L, W, Wp, xpm, NE, s1,s2, oms, den;
    4062             :   long sign, D;
    4063             :   ulong p;
    4064             : 
    4065          63 :   if (DD && !Z_isfundamental(DD))
    4066           0 :     pari_err_DOMAIN("ellpadicL", "isfundamental(D)", "=", gen_0, DD);
    4067          63 :   if (typ(pp) != t_INT) pari_err_TYPE("ellpadicL",pp);
    4068          63 :   if (cmpis(pp,2) < 0) pari_err_PRIME("ellpadicL",pp);
    4069          63 :   if (n <= 0) pari_err_DOMAIN("ellpadicL","precision","<=",gen_0,stoi(n));
    4070          63 :   if (r < 0) pari_err_DOMAIN("ellpadicL","r","<",gen_0,stoi(r));
    4071          63 :   parse_chi(s, &s1,&s2);
    4072          63 :   if (!DD) { sign = 1; D = 1; }
    4073             :   else
    4074             :   {
    4075           0 :     sign = signe(DD); D = itos(DD);
    4076           0 :     if (!sign) pari_err_DOMAIN("ellpadicL", "D", "=", gen_0, DD);
    4077             :   }
    4078          63 :   if (mpodd(s2)) sign = -sign;
    4079          63 :   W = msfromell(E, sign);
    4080          63 :   xpm = gel(W,2);
    4081          63 :   W = gel(W,1);
    4082             : 
    4083          63 :   p = itou(pp);
    4084          63 :   NE = ellQ_get_N(E);
    4085          63 :   if (dvdii(NE, sqri(pp))) pari_err_IMPL("additive reduction in ellpadicL");
    4086             : 
    4087          63 :   xpm = Q_remove_denom(xpm,&den);
    4088          63 :   if (!den) den = gen_1;
    4089          63 :   n += Z_lval(den, p);
    4090             : 
    4091          63 :   Wp = mspadicinit(W, p, n, umodiu(ellap(E,pp),p)? 0: 1);
    4092          63 :   oms = mspadicmoments(Wp, xpm, D);
    4093          63 :   L = mspadicL(oms, s, r);
    4094          63 :   return gerepileupto(av, gdiv(L,den));
    4095             : }

Generated by: LCOV version 1.11