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

Generated by: LCOV version 1.11