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.8.0 lcov report (development 19352-1b11b25) Lines: 2061 2116 97.4 %
Date: 2016-08-25 06:11:27 Functions: 215 215 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.11