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

Generated by: LCOV version 1.11