Code coverage tests

This page documents the degree to which the PARI/GP source code is tested by our public test suite, distributed with the source distribution in directory src/test/. This is measured by the gcov utility; we then process gcov output using the lcov frond-end.

We test a few variants depending on Configure flags on the pari.math.u-bordeaux.fr machine (x86_64 architecture), and agregate them in the final report:

The target is 90% coverage for all mathematical modules (given that branches depending on DEBUGLEVEL or DEBUGMEM are not covered). This script is run to produce the results below.

LCOV - code coverage report
Current view: top level - basemath - modsym.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.10.0 lcov report (development 21741-70cf009) Lines: 2552 2618 97.5 %
Date: 2018-01-21 06:18:30 Functions: 274 275 99.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2011  The PARI group.
       2             : 
       3             : This file is part of the PARI/GP package.
       4             : 
       5             : PARI/GP is free software; you can redistribute it and/or modify it under the
       6             : terms of the GNU General Public License as published by the Free Software
       7             : Foundation. It is distributed in the hope that it will be useful, but WITHOUT
       8             : ANY WARRANTY WHATSOEVER.
       9             : 
      10             : Check the License for details. You should have received a copy of it, along
      11             : with the package; see the file 'COPYING'. If not, write to the Free Software
      12             : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
      13             : 
      14             : #include "pari.h"
      15             : #include "paripriv.h"
      16             : 
      17             : /* Adapted from shp_package/moments by Robert Pollack
      18             :  * http://www.math.mcgill.ca/darmon/programs/shp/shp.html */
      19             : static GEN mskinit(ulong N, long k, long sign);
      20             : static GEN mshecke_i(GEN W, ulong p);
      21             : static GEN ZSl2_star(GEN v);
      22             : static GEN getMorphism(GEN W1, GEN W2, GEN v);
      23             : static GEN voo_act_Gl2Q(GEN g, long k);
      24             : 
      25             : /* Input: P^1(Z/NZ) (formed by create_p1mod)
      26             :    Output: # P^1(Z/NZ) */
      27             : static long
      28        6664 : p1_size(GEN p1N) { return lg(gel(p1N,1)) - 1; }
      29             : static ulong
      30     6775160 : p1N_get_N(GEN p1N) { return gel(p1N,3)[2]; }
      31             : static GEN
      32     2803255 : p1N_get_hash(GEN p1N) { return gel(p1N,2); }
      33             : static GEN
      34        1792 : p1N_get_fa(GEN p1N) { return gel(p1N,4); }
      35             : static GEN
      36        1687 : p1N_get_div(GEN p1N) { return gel(p1N,5); }
      37             : static GEN
      38     2676408 : p1N_get_invsafe(GEN p1N) { return gel(p1N,6); }
      39             : static GEN
      40      902097 : p1N_get_inverse(GEN p1N) { return gel(p1N,7); }
      41             : 
      42             : /* ms-specific accessors */
      43             : /* W = msinit, return the output of msinit_N */
      44             : static GEN
      45      656439 : get_msN(GEN W) { return lg(W) == 4? gel(W,1): W; }
      46             : static GEN
      47      750239 : msN_get_p1N(GEN W) { return gel(W,1); }
      48             : static GEN
      49      208446 : msN_get_genindex(GEN W) { return gel(W,5); }
      50             : static GEN
      51     4375959 : msN_get_E2fromE1(GEN W) { return gel(W,7); }
      52             : static GEN
      53        1393 : msN_get_annT2(GEN W) { return gel(W,8); }
      54             : static GEN
      55        1393 : msN_get_annT31(GEN W) { return gel(W,9); }
      56             : static GEN
      57        1358 : msN_get_singlerel(GEN W) { return gel(W,10); }
      58             : static GEN
      59      677082 : msN_get_section(GEN W) { return gel(W,12); }
      60             : 
      61             : static GEN
      62       83062 : ms_get_p1N(GEN W) { return msN_get_p1N(get_msN(W)); }
      63             : static long
      64       72961 : ms_get_N(GEN W) { return p1N_get_N(ms_get_p1N(W)); }
      65             : static GEN
      66        1869 : ms_get_hashcusps(GEN W) { W = get_msN(W); return gel(W,16); }
      67             : static GEN
      68       14770 : ms_get_section(GEN W) { return msN_get_section(get_msN(W)); }
      69             : static GEN
      70      204400 : ms_get_genindex(GEN W) { return msN_get_genindex(get_msN(W)); }
      71             : static long
      72      199605 : ms_get_nbgen(GEN W) { return lg(ms_get_genindex(W))-1; }
      73             : static long
      74      161602 : ms_get_nbE1(GEN W)
      75             : {
      76             :   GEN W11;
      77      161602 :   W = get_msN(W); W11 = gel(W,11);
      78      161602 :   return W11[4] - W11[3];
      79             : }
      80             : 
      81             : /* msk-specific accessors */
      82             : static long
      83          77 : msk_get_dim(GEN W) { return gmael(W,3,2)[2]; }
      84             : static GEN
      85       82838 : msk_get_basis(GEN W) { return gmael(W,3,1); }
      86             : static long
      87       45808 : msk_get_weight(GEN W) { return gmael(W,3,2)[1]; }
      88             : static long
      89       20762 : msk_get_sign(GEN W)
      90             : {
      91       20762 :   GEN t = gel(W,2);
      92       20762 :   return typ(t)==t_INT? 0: itos(gel(t,1));
      93             : }
      94             : static GEN
      95         623 : msk_get_star(GEN W) { return gmael(W,2,2); }
      96             : static GEN
      97        3626 : msk_get_starproj(GEN W) { return gmael(W,2,3); }
      98             : 
      99             : static int
     100         294 : is_Qevproj(GEN x)
     101         294 : { return typ(x) == t_VEC && lg(x) == 5 && typ(gel(x,1)) == t_MAT; }
     102             : long
     103         105 : msdim(GEN W)
     104             : {
     105         105 :   if (is_Qevproj(W)) return lg(gel(W,1)) - 1;
     106          91 :   checkms(W);
     107          91 :   if (!msk_get_sign(W)) return msk_get_dim(W);
     108          28 :   return lg(gel(msk_get_starproj(W), 1)) - 1;
     109             : }
     110             : long
     111          14 : msgetlevel(GEN W) { checkms(W); return ms_get_N(W); }
     112             : long
     113          14 : msgetweight(GEN W) { checkms(W); return msk_get_weight(W); }
     114             : long
     115          28 : msgetsign(GEN W) { checkms(W); return msk_get_sign(W); }
     116             : 
     117             : void
     118       35959 : checkms(GEN W)
     119             : {
     120       35959 :   if (typ(W) != t_VEC || lg(W) != 4)
     121           0 :     pari_err_TYPE("checkms [please apply msinit]", W);
     122       35959 : }
     123             : 
     124             : /** MODULAR TO SYM **/
     125             : 
     126             : /* q a t_FRAC or t_INT */
     127             : static GEN
     128        7630 : Q_log_init(ulong N, GEN q)
     129             : {
     130             :   long l, n;
     131             :   GEN Q;
     132             : 
     133        7630 :   q = gboundcf(q, 0);
     134        7630 :   l = lg(q);
     135        7630 :   Q = cgetg(l, t_VECSMALL);
     136        7630 :   Q[1] = 1;
     137        7630 :   for (n=2; n <l; n++) Q[n] = umodiu(gel(q,n), N);
     138       17087 :   for (n=3; n < l; n++)
     139        9457 :     Q[n] = Fl_add(Fl_mul(Q[n], Q[n-1], N), Q[n-2], N);
     140        7630 :   return Q;
     141             : }
     142             : 
     143             : /** INIT MODSYM STRUCTURE, WEIGHT 2 **/
     144             : 
     145             : /* num = [Gamma : Gamma_0(N)] = N * Prod_{p|N} (1+p^-1) */
     146             : static ulong
     147        1687 : count_Manin_symbols(ulong N, GEN P)
     148             : {
     149        1687 :   long i, l = lg(P);
     150        1687 :   ulong num = N;
     151        1687 :   for (i = 1; i < l; i++) { ulong p = P[i]; num *= p+1; num /= p; }
     152        1687 :   return num;
     153             : }
     154             : /* returns the list of "Manin symbols" (c,d) in (Z/NZ)^2, (c,d,N) = 1
     155             :  * generating H^1(X_0(N), Z) */
     156             : static GEN
     157        1687 : generatemsymbols(ulong N, ulong num, GEN divN)
     158             : {
     159        1687 :   GEN ret = cgetg(num+1, t_VEC);
     160        1687 :   ulong c, d, curn = 0;
     161             :   long i, l;
     162             :   /* generate Manin-symbols in two lists: */
     163             :   /* list 1: (c:1) for 0 <= c < N */
     164        1687 :   for (c = 0; c < N; c++) gel(ret, ++curn) = mkvecsmall2(c, 1);
     165        1687 :   if (N == 1) return ret;
     166             :   /* list 2: (c:d) with 1 <= c < N, c | N, 0 <= d < N, gcd(d,N) > 1, gcd(c,d)=1.
     167             :    * Furthermore, d != d0 (mod N/c) with c,d0 already in the list */
     168        1666 :   l = lg(divN) - 1;
     169             :   /* c = 1 first */
     170        1666 :   gel(ret, ++curn) = mkvecsmall2(1,0);
     171      152320 :   for (d = 2; d < N; d++)
     172      150654 :     if (ugcd(d,N) != 1UL)
     173       54586 :       gel(ret, ++curn) = mkvecsmall2(1,d);
     174             :   /* omit c = 1 (first) and c = N (last) */
     175        4781 :   for (i=2; i < l; i++)
     176             :   {
     177             :     ulong Novc, d0;
     178        3115 :     c = divN[i];
     179        3115 :     Novc = N / c;
     180       84672 :     for (d0 = 2; d0 <= Novc; d0++)
     181             :     {
     182       81557 :       ulong k, d = d0;
     183       81557 :       if (ugcd(d, Novc) == 1UL) continue;
     184      131257 :       for (k = 0; k < c; k++, d += Novc)
     185      119763 :         if (ugcd(c,d) == 1UL)
     186             :         {
     187       18592 :           gel(ret, ++curn) = mkvecsmall2(c,d);
     188       18592 :           break;
     189             :         }
     190             :     }
     191             :   }
     192        1666 :   if (curn != num) pari_err_BUG("generatemsymbols [wrong number of symbols]");
     193        1666 :   return ret;
     194             : }
     195             : 
     196             : static GEN
     197        1687 : inithashmsymbols(ulong N, GEN symbols)
     198             : {
     199        1687 :   GEN H = zerovec(N);
     200        1687 :   long k, l = lg(symbols);
     201             :   /* skip the (c:1), 0 <= c < N and (1:0) */
     202       74865 :   for (k=N+2; k < l; k++)
     203             :   {
     204       73178 :     GEN s = gel(symbols, k);
     205       73178 :     ulong c = s[1], d = s[2], Novc = N/c;
     206       73178 :     if (gel(H,c) == gen_0) gel(H,c) = const_vecsmall(Novc+1,0);
     207       73178 :     if (c != 1) { d %= Novc; if (!d) d = Novc; }
     208       73178 :     mael(H, c, d) = k;
     209             :   }
     210        1687 :   return H;
     211             : }
     212             : 
     213             : /** Helper functions for Sl2(Z) / Gamma_0(N) **/
     214             : /* M a 2x2 ZM in SL2(Z) */
     215             : static GEN
     216     1199639 : SL2_inv(GEN M)
     217             : {
     218     1199639 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
     219     1199639 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
     220     1199639 :   return mkmat22(d,negi(b), negi(c),a);
     221             : }
     222             : /* SL2_inv(M)[2] */
     223             : static GEN
     224        3409 : SL2_inv2(GEN M)
     225             : {
     226        3409 :   GEN a = gcoeff(M,1,1), c = gcoeff(M,2,1);
     227        3409 :   return mkcol2(negi(c),a);
     228             : }
     229             : /* M a 2x2 mat2 in SL2(Z) */
     230             : static GEN
     231      660807 : sl2_inv(GEN M)
     232             : {
     233      660807 :   long a=coeff(M,1,1), b=coeff(M,1,2), c=coeff(M,2,1), d=coeff(M,2,2);
     234      660807 :   return mkvec2(mkvecsmall2(d, -c), mkvecsmall2(-b, a));
     235             : }
     236             : /* Return the mat2 [a,b; c,d], not a zm to avoid GP problems */
     237             : static GEN
     238     1306879 : mat2(long a, long b, long c, long d)
     239     1306879 : { return mkvec2(mkvecsmall2(a,c), mkvecsmall2(b,d)); }
     240             : static GEN
     241      319683 : mat2_to_ZM(GEN M)
     242             : {
     243      319683 :   GEN A = gel(M,1), B = gel(M,2);
     244      319683 :   retmkmat2(mkcol2s(A[1],A[2]), mkcol2s(B[1],B[2]));
     245             : }
     246             : 
     247             : /* Input: a = 2-vector = path = {r/s,x/y}
     248             :  * Output: either [r,x;s,y] or [-r,x;-s,y], whichever has determinant > 0 */
     249             : static GEN
     250       80178 : path_to_ZM(GEN a)
     251             : {
     252       80178 :   GEN v = gel(a,1), w = gel(a,2);
     253       80178 :   long r = v[1], s = v[2], x = w[1], y = w[2];
     254       80178 :   if (cmpii(mulss(r,y), mulss(x,s)) < 0) { r = -r; s = -s; }
     255       80178 :   return mkmat22(stoi(r),stoi(x),stoi(s),stoi(y));
     256             : }
     257             : static GEN
     258      805315 : path_to_zm(GEN a)
     259             : {
     260      805315 :   GEN v = gel(a,1), w = gel(a,2);
     261      805315 :   long r = v[1], s = v[2], x = w[1], y = w[2];
     262      805315 :   if (cmpii(mulss(r,y), mulss(x,s)) < 0) { r = -r; s = -s; }
     263      805315 :   return mat2(r,x,s,y);
     264             : }
     265             : /* path from c1 to c2 */
     266             : static GEN
     267      463596 : mkpath(GEN c1, GEN c2) { return mat2(c1[1], c2[1], c1[2], c2[2]); }
     268             : static long
     269      654913 : cc(GEN M) { GEN v = gel(M,1); return v[2]; }
     270             : static long
     271      654913 : dd(GEN M) { GEN v = gel(M,2); return v[2]; }
     272             : 
     273             : /*Input: a,b = 2 paths, N = integer
     274             :  *Output: 1 if the a,b are \Gamma_0(N)-equivalent; 0 otherwise */
     275             : static int
     276       75306 : gamma_equiv(GEN a, GEN b, ulong N)
     277             : {
     278       75306 :   pari_sp av = avma;
     279       75306 :   GEN m = path_to_zm(a);
     280       75306 :   GEN n = path_to_zm(b);
     281       75306 :   GEN d = subii(mulss(cc(m),dd(n)), mulss(dd(m),cc(n)));
     282       75306 :   ulong res = umodiu(d, N);
     283       75306 :   avma = av; return res == 0;
     284             : }
     285             : /* Input: a,b = 2 paths that are \Gamma_0(N)-equivalent, N = integer
     286             :  * Output: M in \Gamma_0(N) such that Mb=a */
     287             : static GEN
     288       39725 : gamma_equiv_matrix(GEN a, GEN b)
     289             : {
     290       39725 :   GEN m = path_to_ZM(a);
     291       39725 :   GEN n = path_to_ZM(b);
     292       39725 :   return ZM_mul(m, SL2_inv(n));
     293             : }
     294             : 
     295             : /*************/
     296             : /* P^1(Z/NZ) */
     297             : /*************/
     298             : /* a != 0 in Z/NZ. Return v in (Z/NZ)^* such that av = gcd(a, N) (mod N)*/
     299             : static ulong
     300      354165 : Fl_inverse(ulong a, ulong N) { ulong g; return Fl_invgen(a,N,&g); }
     301             : 
     302             : /* Input: N = integer
     303             :  * Output: creates P^1(Z/NZ) = [symbols, H, N]
     304             :  *   symbols: list of vectors [x,y] that give a set of representatives
     305             :  *            of P^1(Z/NZ)
     306             :  *   H: an M by M grid whose value at the r,c-th place is the index of the
     307             :  *      "standard representative" equivalent to [r,c] occuring in the first
     308             :  *      list. If gcd(r,c,N) > 1 the grid has value 0. */
     309             : static GEN
     310        1687 : create_p1mod(ulong N)
     311             : {
     312        1687 :   GEN fa = factoru(N), div = divisorsu_fact(fa);
     313        1687 :   ulong i, nsym = count_Manin_symbols(N, gel(fa,1));
     314        1687 :   GEN symbols = generatemsymbols(N, nsym, div);
     315        1687 :   GEN H = inithashmsymbols(N,symbols);
     316        1687 :   GEN invsafe = cgetg(N, t_VECSMALL), inverse = cgetg(N, t_VECSMALL);
     317      154007 :   for (i = 1; i < N; i++)
     318             :   {
     319      152320 :     invsafe[i] = Fl_invsafe(i,N);
     320      152320 :     inverse[i] = Fl_inverse(i,N);
     321             :   }
     322        1687 :   return mkvecn(7, symbols, H, utoipos(N), fa, div, invsafe, inverse);
     323             : }
     324             : 
     325             : /* Let (c : d) in P1(Z/NZ).
     326             :  * If c = 0 return (0:1). If d = 0 return (1:0).
     327             :  * Else replace by (cu : du), where u in (Z/NZ)^* such that C := cu = gcd(c,N).
     328             :  * In create_p1mod(), (c : d) is represented by (C:D) where D = du (mod N/c)
     329             :  * is smallest such that gcd(C,D) = 1. Return (C : du mod N/c), which need
     330             :  * not belong to P1(Z/NZ) ! A second component du mod N/c = 0 is replaced by
     331             :  * N/c in this case to avoid problems with array indices */
     332             : static void
     333     2803255 : p1_std_form(long *pc, long *pd, GEN p1N)
     334             : {
     335     2803255 :   ulong N = p1N_get_N(p1N);
     336             :   ulong u;
     337     2803255 :   *pc = smodss(*pc, N); if (!*pc) { *pd = 1; return; }
     338     2719423 :   *pd = smodss(*pd, N); if (!*pd) { *pc = 1; return; }
     339     2676408 :   u = p1N_get_invsafe(p1N)[*pd];
     340     2676408 :   if (u) { *pc = Fl_mul(*pc,u,N); *pd = 1; return; } /* (d,N) = 1 */
     341             : 
     342      902097 :   u = p1N_get_inverse(p1N)[*pc];
     343      902097 :   if (u > 1) { *pc = Fl_mul(*pc,u,N); *pd = Fl_mul(*pd,u,N); }
     344             :   /* c | N */
     345      902097 :   if (*pc != 1) *pd %= (N / *pc);
     346      902097 :   if (!*pd) *pd = N / *pc;
     347             : }
     348             : 
     349             : /* Input: v = [x,y] = elt of P^1(Z/NZ) = class in Gamma_0(N) \ PSL2(Z)
     350             :  * Output: returns the index of the standard rep equivalent to v */
     351             : static long
     352     2803255 : p1_index(long x, long y, GEN p1N)
     353             : {
     354     2803255 :   ulong N = p1N_get_N(p1N);
     355     2803255 :   GEN H = p1N_get_hash(p1N);
     356             : 
     357     2803255 :   p1_std_form(&x, &y, p1N);
     358     2803255 :   if (y == 1) return x+1;
     359      945112 :   if (y == 0) return N+1;
     360      902097 :   if (mael(H,x,y) == 0) pari_err_BUG("p1_index");
     361      902097 :   return mael(H,x,y);
     362             : }
     363             : 
     364             : /* Cusps for \Gamma_0(N) */
     365             : 
     366             : /* \sum_{d | N} \phi(gcd(d, N/d)), using multiplicativity. fa = factor(N) */
     367             : ulong
     368        1722 : mfnumcuspsu_fact(GEN fa)
     369             : {
     370        1722 :   GEN P = gel(fa,1), E = gel(fa,2);
     371        1722 :   long i, l = lg(P);
     372        1722 :   ulong T = 1;
     373        4326 :   for (i = 1; i < l; i++)
     374             :   {
     375        2604 :     long e = E[i], e2 = e >> 1; /* floor(E[i] / 2) */
     376        2604 :     ulong p = P[i];
     377        2604 :     if (odd(e))
     378        2275 :       T *= 2 * upowuu(p, e2);
     379             :     else
     380         329 :       T *= (p+1) * upowuu(p, e2-1);
     381             :   }
     382        1722 :   return T;
     383             : }
     384             : ulong
     385           7 : mfnumcuspsu(ulong n)
     386             : {
     387           7 :   pari_sp av = avma;
     388           7 :   ulong t = mfnumcuspsu_fact( factoru(n) );
     389           7 :   avma = av; return t;
     390             : }
     391             : /* \sum_{d | N} \phi(gcd(d, N/d)), using multiplicativity. fa = factor(N) */
     392             : GEN
     393          14 : mfnumcusps_fact(GEN fa)
     394             : {
     395          14 :   GEN P = gel(fa,1), E = gel(fa,2), T = gen_1;
     396          14 :   long i, l = lg(P);
     397          35 :   for (i = 1; i < l; i++)
     398             :   {
     399          21 :     GEN p = gel(P,i), c;
     400          21 :     long e = itos(gel(E,i)), e2 = e >> 1; /* floor(E[i] / 2) */
     401          21 :     if (odd(e))
     402           0 :       c = shifti(powiu(p, e2), 1);
     403             :     else
     404          21 :       c = mulii(addiu(p,1), powiu(p, e2-1));
     405          21 :     T = T? mulii(T, c): c;
     406             :   }
     407          14 :   return T? T: gen_1;
     408             : }
     409             : GEN
     410          21 : mfnumcusps(GEN n)
     411             : {
     412          21 :   pari_sp av = avma;
     413          21 :   GEN F = check_arith_pos(n,"mfnumcusps");
     414          21 :   if (!F)
     415             :   {
     416          14 :     if (lgefint(n) == 3) return utoi( mfnumcuspsu(n[2]) );
     417           7 :     F = absZ_factor(n);
     418             :   }
     419          14 :   return gerepileuptoint(av, mfnumcusps_fact(F));
     420             : }
     421             : 
     422             : 
     423             : /* to each cusp in \Gamma_0(N) P1(Q), represented by p/q, we associate a
     424             :  * unique index. Canonical representative: (1:0) or (p:q) with q | N, q < N,
     425             :  * p defined modulo d := gcd(N/q,q), (p,d) = 1.
     426             :  * Return [[N, nbcusps], H, cusps]*/
     427             : static GEN
     428        1687 : inithashcusps(GEN p1N)
     429             : {
     430        1687 :   ulong N = p1N_get_N(p1N);
     431        1687 :   GEN div = p1N_get_div(p1N), H = zerovec(N+1);
     432        1687 :   long k, ind, l = lg(div), ncusp = mfnumcuspsu_fact(p1N_get_fa(p1N));
     433        1687 :   GEN cusps = cgetg(ncusp+1, t_VEC);
     434             : 
     435        1687 :   gel(H,1) = mkvecsmall2(0/*empty*/, 1/* first cusp: (1:0) */);
     436        1687 :   gel(cusps, 1) = mkvecsmall2(1,0);
     437        1687 :   ind = 2;
     438        6468 :   for (k=1; k < l-1; k++) /* l-1: remove q = N */
     439             :   {
     440        4781 :     ulong p, q = div[k], d = ugcd(q, N/q);
     441        4781 :     GEN h = const_vecsmall(d+1,0);
     442        4781 :     gel(H,q+1) = h ;
     443       12698 :     for (p = 0; p < d; p++)
     444        7917 :       if (ugcd(p,d) == 1)
     445             :       {
     446        6195 :         h[p+1] = ind;
     447        6195 :         gel(cusps, ind) = mkvecsmall2(p,q);
     448        6195 :         ind++;
     449             :       }
     450             :   }
     451        1687 :   return mkvec3(mkvecsmall2(N,ind-1), H, cusps);
     452             : }
     453             : /* c = [p,q], (p,q) = 1, return a canonical representative for
     454             :  * \Gamma_0(N)(p/q) */
     455             : static GEN
     456      203770 : cusp_std_form(GEN c, GEN S)
     457             : {
     458      203770 :   long p, N = gel(S,1)[1], q = smodss(c[2], N);
     459             :   ulong u, d;
     460      203770 :   if (q == 0) return mkvecsmall2(1, 0);
     461      201845 :   p = smodss(c[1], N);
     462      201845 :   u = Fl_inverse(q, N);
     463      201845 :   q = Fl_mul(q,u, N);
     464      201845 :   d = ugcd(q, N/q);
     465      201845 :   return mkvecsmall2(Fl_div(p % d,u % d, d), q);
     466             : }
     467             : /* c = [p,q], (p,q) = 1, return the index of the corresponding cusp.
     468             :  * S from inithashcusps */
     469             : static ulong
     470      203770 : cusp_index(GEN c, GEN S)
     471             : {
     472             :   long p, q;
     473      203770 :   GEN H = gel(S,2);
     474      203770 :   c = cusp_std_form(c, S);
     475      203770 :   p = c[1]; q = c[2];
     476      203770 :   if (!mael(H,q+1,p+1)) pari_err_BUG("cusp_index");
     477      203770 :   return mael(H,q+1,p+1);
     478             : }
     479             : 
     480             : /* M a square invertible ZM, return a ZM iM such that iM M = M iM = d.Id */
     481             : static GEN
     482        3052 : ZM_inv_denom(GEN M)
     483             : {
     484        3052 :   GEN diM, iM = ZM_inv_ratlift(M, &diM);
     485        3052 :   return mkvec2(iM, diM);
     486             : }
     487             : /* return M^(-1) v, dinv = ZM_inv_denom(M) OR Qevproj_init(M) */
     488             : static GEN
     489      745115 : ZC_apply_dinv(GEN dinv, GEN v)
     490             : {
     491             :   GEN x, c, iM;
     492      745115 :   if (lg(dinv) == 3)
     493             :   {
     494      666421 :     iM = gel(dinv,1);
     495      666421 :     c = gel(dinv,2);
     496             :   }
     497             :   else
     498             :   { /* Qevproj_init */
     499       78694 :     iM = gel(dinv,2);
     500       78694 :     c = gel(dinv,3);
     501      157388 :     v = typ(v) == t_MAT? rowpermute(v, gel(dinv,4))
     502       78694 :                        : vecpermute(v, gel(dinv,4));
     503             :   }
     504      745115 :   x = RgM_RgC_mul(iM, v);
     505      745115 :   if (!isint1(c)) x = RgC_Rg_div(x, c);
     506      745115 :   return x;
     507             : }
     508             : 
     509             : /* M an n x d ZM of rank d (basis of a Q-subspace), n >= d.
     510             :  * Initialize a projector on M */
     511             : GEN
     512        4571 : Qevproj_init(GEN M)
     513             : {
     514             :   GEN v, perm, MM, iM, diM;
     515        4571 :   v = ZM_indexrank(M); perm = gel(v,1);
     516        4571 :   MM = rowpermute(M, perm); /* square invertible */
     517        4571 :   iM = ZM_inv_ratlift(MM, &diM);
     518        4571 :   return mkvec4(M, iM, diM, perm);
     519             : }
     520             : 
     521             : /* same with typechecks */
     522             : static GEN
     523         714 : Qevproj_init0(GEN M)
     524             : {
     525         714 :   switch(typ(M))
     526             :   {
     527             :     case t_VEC:
     528         658 :       if (lg(M) == 5) return M;
     529           0 :       break;
     530             :     case t_COL:
     531          49 :       M = mkmat(M);/*fall through*/
     532             :     case t_MAT:
     533          56 :       M = Q_primpart(M);
     534          56 :       RgM_check_ZM(M,"Qevproj_init");
     535          56 :       return Qevproj_init(M);
     536             :   }
     537           0 :   pari_err_TYPE("Qevproj_init",M);
     538           0 :   return NULL;
     539             : }
     540             : 
     541             : /* T an n x n QM, stabilizing d-dimensional Q-vector space spanned by the
     542             :  * columns of M, pro = Qevproj_init(M). Return d x d matrix of T acting
     543             :  * on M */
     544             : GEN
     545        3122 : Qevproj_apply(GEN T, GEN pro)
     546             : {
     547        3122 :   GEN M = gel(pro,1), iM = gel(pro,2), ciM = gel(pro,3), perm = gel(pro,4);
     548        3122 :   return RgM_Rg_div(RgM_mul(iM, RgM_mul(rowpermute(T,perm), M)), ciM);
     549             : }
     550             : /* Qevproj_apply(T,pro)[,k] */
     551             : GEN
     552         819 : Qevproj_apply_vecei(GEN T, GEN pro, long k)
     553             : {
     554         819 :   GEN M = gel(pro,1), iM = gel(pro,2), ciM = gel(pro,3), perm = gel(pro,4);
     555         819 :   GEN v = RgM_RgC_mul(iM, RgM_RgC_mul(rowpermute(T,perm), gel(M,k)));
     556         819 :   return RgC_Rg_div(v, ciM);
     557             : }
     558             : 
     559             : static GEN
     560        1183 : QM_ker_r(GEN M) { return ZM_ker(Q_primpart(M)); }
     561             : static GEN
     562         959 : QM_image(GEN A)
     563             : {
     564         959 :   A = vec_Q_primpart(A);
     565         959 :   return vecpermute(A, ZM_indeximage(A));
     566             : }
     567             : 
     568             : static int
     569         420 : cmp_dim(void *E, GEN a, GEN b)
     570             : {
     571             :   long k;
     572             :   (void)E;
     573         420 :   a = gel(a,1);
     574         420 :   b = gel(b,1); k = lg(a)-lg(b);
     575         420 :   return k? ((k > 0)? 1: -1): 0;
     576             : }
     577             : 
     578             : /* FIXME: could use ZX_roots for deglim = 1 */
     579             : static GEN
     580         329 : ZX_factor_limit(GEN T, long deglim, long *pl)
     581             : {
     582         329 :   GEN fa = ZX_factor(T), P, E;
     583             :   long i, l;
     584         329 :   P = gel(fa,1); *pl = l = lg(P);
     585         329 :   if (deglim <= 0) return fa;
     586         224 :   E = gel(fa,2);
     587         567 :   for (i = 1; i < l; i++)
     588         406 :     if (degpol(gel(P,i)) > deglim) break;
     589         224 :   setlg(P,i);
     590         224 :   setlg(E,i); return fa;
     591             : }
     592             : 
     593             : /* Decompose the subspace H (Qevproj format) in simple subspaces.
     594             :  * Eg for H = msnew */
     595             : static GEN
     596         259 : mssplit_i(GEN W, GEN H, long deglim)
     597             : {
     598         259 :   ulong p, N = ms_get_N(W);
     599             :   long first, dim;
     600             :   forprime_t S;
     601         259 :   GEN T1 = NULL, T2 = NULL, V;
     602         259 :   dim = lg(gel(H,1))-1;
     603         259 :   V = vectrunc_init(dim+1);
     604         259 :   if (!dim) return V;
     605         252 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
     606         252 :   vectrunc_append(V, H);
     607         252 :   first = 1; /* V[1..first-1] contains simple subspaces */
     608         630 :   while ((p = u_forprime_next(&S)))
     609             :   {
     610             :     GEN T;
     611             :     long j, lV;
     612         378 :     if (N % p == 0) continue;
     613         322 :     if (T1 && T2) {
     614          21 :       T = RgM_add(T1,T2);
     615          21 :       T2 = NULL;
     616             :     } else {
     617         301 :       T2 = T1;
     618         301 :       T1 = T = mshecke(W, p, NULL);
     619             :     }
     620         322 :     lV = lg(V);
     621         651 :     for (j = first; j < lV; j++)
     622             :     {
     623         329 :       pari_sp av = avma;
     624             :       long lP;
     625         329 :       GEN Vj = gel(V,j), P = gel(Vj,1);
     626         329 :       GEN TVj = Qevproj_apply(T, Vj); /* c T | V_j */
     627         329 :       GEN ch = QM_charpoly_ZX(TVj), fa = ZX_factor_limit(ch,deglim, &lP);
     628         329 :       GEN F = gel(fa, 1), E = gel(fa, 2);
     629         329 :       long k, lF = lg(F);
     630         329 :       if (lF == 2 && lP == 2)
     631             :       {
     632         336 :         if (isint1(gel(E,1)))
     633             :         { /* simple subspace */
     634         168 :           swap(gel(V,first), gel(V,j));
     635         168 :           first++;
     636             :         }
     637             :         else
     638           0 :           avma = av;
     639             :       }
     640         161 :       else if (lF == 1) /* discard V[j] */
     641           7 :       { swap(gel(V,j), gel(V,lg(V)-1)); setlg(V, lg(V)-1); }
     642             :       else
     643             :       { /* can split Vj */
     644             :         GEN pows;
     645         154 :         long D = 1;
     646         616 :         for (k = 1; k < lF; k++)
     647             :         {
     648         462 :           long d = degpol(gel(F,k));
     649         462 :           if (d > D) D = d;
     650             :         }
     651             :         /* remove V[j] */
     652         154 :         swap(gel(V,j), gel(V,lg(V)-1)); setlg(V, lg(V)-1);
     653         154 :         pows = RgM_powers(TVj, minss((long)2*sqrt((double)D), D));
     654         616 :         for (k = 1; k < lF; k++)
     655             :         {
     656         462 :           GEN f = gel(F,k);
     657         462 :           GEN K = QM_ker_r( RgX_RgMV_eval(f, pows)) ; /* Ker f(TVj) */
     658         462 :           GEN p = vec_Q_primpart( RgM_mul(P, K) );
     659         462 :           vectrunc_append(V, Qevproj_init(p));
     660         462 :           if (lg(K) == 2 || isint1(gel(E,k)))
     661             :           { /* simple subspace */
     662         385 :             swap(gel(V,first), gel(V, lg(V)-1));
     663         385 :             first++;
     664             :           }
     665             :         }
     666         154 :         if (j < first) j = first;
     667             :       }
     668             :     }
     669         322 :     if (first >= lg(V)) {
     670         252 :       gen_sort_inplace(V, NULL, cmp_dim, NULL);
     671         252 :       return V;
     672             :     }
     673             :   }
     674           0 :   pari_err_BUG("subspaces not found");
     675           0 :   return NULL;
     676             : }
     677             : GEN
     678         259 : mssplit(GEN W, GEN H, long deglim)
     679             : {
     680         259 :   pari_sp av = avma;
     681         259 :   checkms(W);
     682         259 :   if (!msk_get_sign(W))
     683           0 :     pari_err_DOMAIN("mssplit","abs(sign)","!=",gen_1,gen_0);
     684         259 :   if (!H) H = msnew(W);
     685         259 :   H = Qevproj_init0(H);
     686         259 :   return gerepilecopy(av, mssplit_i(W,H,deglim));
     687             : }
     688             : 
     689             : /* proV = Qevproj_init of a Hecke simple subspace, return [ a_n, n <= B ] */
     690             : static GEN
     691         245 : msqexpansion_i(GEN W, GEN proV, ulong B)
     692             : {
     693         245 :   ulong p, N = ms_get_N(W), sqrtB;
     694         245 :   long i, d, k = msk_get_weight(W);
     695             :   forprime_t S;
     696         245 :   GEN T1=NULL, T2=NULL, TV=NULL, ch=NULL, v, dTiv, Tiv, diM, iM, L;
     697         245 :   switch(B)
     698             :   {
     699           0 :     case 0: return cgetg(1,t_VEC);
     700           0 :     case 1: return mkvec(gen_1);
     701             :   }
     702         245 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
     703         602 :   while ((p = u_forprime_next(&S)))
     704             :   {
     705             :     GEN T;
     706         357 :     if (N % p == 0) continue;
     707         266 :     if (T1 && T2)
     708             :     {
     709           0 :       T = RgM_add(T1,T2);
     710           0 :       T2 = NULL;
     711             :     }
     712             :     else
     713             :     {
     714         266 :       T2 = T1;
     715         266 :       T1 = T = mshecke(W, p, NULL);
     716             :     }
     717         266 :     TV = Qevproj_apply(T, proV); /* T | V */
     718         266 :     ch = QM_charpoly_ZX(TV);
     719         266 :     if (ZX_is_irred(ch)) break;
     720          21 :     ch = NULL;
     721             :   }
     722         245 :   if (!ch) pari_err_BUG("q-Expansion not found");
     723             :   /* T generates the Hecke algebra (acting on V) */
     724         245 :   d = degpol(ch);
     725         245 :   v = vec_ei(d, 1); /* take v = e_1 */
     726         245 :   Tiv = cgetg(d+1, t_MAT); /* Tiv[i] = T^(i-1)v */
     727         245 :   gel(Tiv, 1) = v;
     728         245 :   for (i = 2; i <= d; i++) gel(Tiv, i) = RgM_RgC_mul(TV, gel(Tiv,i-1));
     729         245 :   Tiv = Q_remove_denom(Tiv, &dTiv);
     730         245 :   iM = ZM_inv_ratlift(Tiv, &diM);
     731         245 :   if (dTiv) diM = gdiv(diM, dTiv);
     732         245 :   L = const_vec(B,NULL);
     733         245 :   sqrtB = usqrt(B);
     734         245 :   gel(L,1) = d > 1? mkpolmod(gen_1,ch): gen_1;
     735        2471 :   for (p = 2; p <= B; p++)
     736             :   {
     737        2226 :     pari_sp av = avma;
     738             :     GEN T, u, Tv, ap, P;
     739             :     ulong m;
     740        2226 :     if (gel(L,p)) continue;  /* p not prime */
     741         819 :     T = mshecke(W, p, NULL);
     742         819 :     Tv = Qevproj_apply_vecei(T, proV, 1); /* Tp.v */
     743             :     /* Write Tp.v = \sum u_i T^i v */
     744         819 :     u = RgC_Rg_div(RgM_RgC_mul(iM, Tv), diM);
     745         819 :     ap = gerepilecopy(av, RgV_to_RgX(u, 0));
     746         819 :     if (d > 1)
     747         399 :       ap = mkpolmod(ap,ch);
     748             :     else
     749         420 :       ap = simplify_shallow(ap);
     750         819 :     gel(L,p) = ap;
     751         819 :     if (!(N % p))
     752             :     { /* p divides the level */
     753         147 :       ulong C = B/p;
     754         546 :       for (m=1; m<=C; m++)
     755         399 :         if (gel(L,m)) gel(L,m*p) = gmul(gel(L,m), ap);
     756         147 :       continue;
     757             :     }
     758         672 :     P = powuu(p,k-1);
     759         672 :     if (p <= sqrtB) {
     760         119 :       ulong pj, oldpj = 1;
     761         546 :       for (pj = p; pj <= B; oldpj=pj, pj *= p)
     762             :       {
     763         427 :         GEN apj = (pj==p)? ap
     764         427 :                          : gsub(gmul(ap,gel(L,oldpj)), gmul(P,gel(L,oldpj/p)));
     765         427 :         gel(L,pj) = apj;
     766        3136 :         for (m = B/pj; m > 1; m--)
     767        2709 :           if (gel(L,m) && m%p) gel(L,m*pj) = gmul(gel(L,m), apj);
     768             :       }
     769             :     } else {
     770         553 :       gel(L,p) = ap;
     771        1092 :       for (m = B/p; m > 1; m--)
     772         539 :         if (gel(L,m)) gel(L,m*p) = gmul(gel(L,m), ap);
     773             :     }
     774             :   }
     775         245 :   return L;
     776             : }
     777             : GEN
     778         245 : msqexpansion(GEN W, GEN proV, ulong B)
     779             : {
     780         245 :   pari_sp av = avma;
     781         245 :   checkms(W);
     782         245 :   proV = Qevproj_init0(proV);
     783         245 :   return gerepilecopy(av, msqexpansion_i(W,proV,B));
     784             : }
     785             : 
     786             : static GEN
     787         602 : Qevproj_apply2(GEN T, GEN pro1, GEN pro2)
     788             : {
     789         602 :   GEN M = gel(pro1,1), iM = gel(pro2,2), ciM = gel(pro2,3), perm = gel(pro2,4);
     790         602 :   return RgM_Rg_div(RgM_mul(iM, RgM_mul(rowpermute(T,perm), M)), ciM);
     791             : }
     792             : static GEN
     793         266 : Qevproj_apply0(GEN T, GEN pro)
     794             : {
     795         266 :   GEN iM = gel(pro,2), perm = gel(pro,4);
     796         266 :   return vec_Q_primpart(ZM_mul(iM, rowpermute(T,perm)));
     797             : }
     798             : /* T a ZC or ZM */
     799             : GEN
     800        1057 : Qevproj_down(GEN T, GEN pro)
     801             : {
     802        1057 :   GEN iM = gel(pro,2), ciM = gel(pro,3), perm = gel(pro,4);
     803        1057 :   if (typ(T) == t_COL)
     804        1057 :     return RgC_Rg_div(ZM_ZC_mul(iM, vecpermute(T,perm)), ciM);
     805             :   else
     806           0 :     return RgM_Rg_div(ZM_mul(iM, rowpermute(T,perm)), ciM);
     807             : }
     808             : 
     809             : static GEN
     810         357 : Qevproj_star(GEN W, GEN H)
     811             : {
     812         357 :   long s = msk_get_sign(W);
     813         357 :   if (s)
     814             :   { /* project on +/- component */
     815         266 :     GEN A = RgM_mul(msk_get_star(W), H);
     816         266 :     A = (s > 0)? gadd(A, H): gsub(A, H);
     817             :     /* Im(star + sign) = Ker(star - sign) */
     818         266 :     H = QM_image(A);
     819         266 :     H = Qevproj_apply0(H, msk_get_starproj(W));
     820             :   }
     821         357 :   return H;
     822             : }
     823             : 
     824             : static GEN
     825        2604 : Tp_matrices(ulong p)
     826             : {
     827        2604 :   GEN v = cgetg(p+2, t_VEC);
     828             :   ulong i;
     829        2604 :   for (i = 1; i <= p; i++) gel(v,i) = mat2(1, i-1, 0, p);
     830        2604 :   gel(v,i) = mat2(p, 0, 0, 1);
     831        2604 :   return v;
     832             : }
     833             : static GEN
     834         973 : Up_matrices(ulong p)
     835             : {
     836         973 :   GEN v = cgetg(p+1, t_VEC);
     837             :   ulong i;
     838         973 :   for (i = 1; i <= p; i++) gel(v,i) = mat2(1, i-1, 0, p);
     839         973 :   return v;
     840             : }
     841             : 
     842             : /* M = N/p. Classes of Gamma_0(M) / Gamma_O(N) when p | M */
     843             : static GEN
     844         168 : NP_matrices(ulong M, ulong p)
     845             : {
     846         168 :   GEN v = cgetg(p+1, t_VEC);
     847             :   ulong i;
     848         168 :   for (i = 1; i <= p; i++) gel(v,i) = mat2(1, 0, (i-1)*M, 1);
     849         168 :   return v;
     850             : }
     851             : /* M = N/p. Extra class of Gamma_0(M) / Gamma_O(N) when p \nmid M */
     852             : static GEN
     853          84 : NP_matrix_extra(ulong M, ulong p)
     854             : {
     855          84 :   long w,z, d = cbezout(p, -M, &w, &z);
     856          84 :   if (d != 1) return NULL;
     857          84 :   return mat2(w,z,M,p);
     858             : }
     859             : static GEN
     860          98 : WQ_matrix(long N, long Q)
     861             : {
     862          98 :   long w,z, d = cbezout(Q, N/Q, &w, &z);
     863          98 :   if (d != 1) return NULL;
     864          98 :   return mat2(Q,1,-N*z,Q*w);
     865             : }
     866             : 
     867             : GEN
     868         280 : msnew(GEN W)
     869             : {
     870         280 :   pari_sp av = avma;
     871         280 :   GEN S = mscuspidal(W, 0);
     872         280 :   ulong N = ms_get_N(W);
     873         280 :   long s = msk_get_sign(W), k = msk_get_weight(W);
     874         280 :   if (N > 1 && (!uisprime(N) || (k == 12 || k > 14)))
     875             :   {
     876         105 :     GEN p1N = ms_get_p1N(W), P = gel(p1N_get_fa(p1N), 1);
     877         105 :     long i, nP = lg(P)-1;
     878         105 :     GEN v = cgetg(2*nP + 1, t_COL);
     879         105 :     S = gel(S,1); /* Q basis */
     880         273 :     for (i = 1; i <= nP; i++)
     881             :     {
     882         168 :       pari_sp av = avma, av2;
     883         168 :       long M = N/P[i];
     884         168 :       GEN T1,Td, Wi = mskinit(M, k, s);
     885         168 :       GEN v1 = NP_matrices(M, P[i]);
     886         168 :       GEN vd = Up_matrices(P[i]);
     887             :       /* p^2 \nmid N */
     888         168 :       if (M % P[i])
     889             :       {
     890          84 :         v1 = shallowconcat(v1, mkvec(NP_matrix_extra(M,P[i])));
     891          84 :         vd = shallowconcat(vd, mkvec(WQ_matrix(N,P[i])));
     892             :       }
     893         168 :       T1 = getMorphism(W, Wi, v1);
     894         168 :       Td = getMorphism(W, Wi, vd);
     895         168 :       if (s)
     896             :       {
     897         154 :         T1 = Qevproj_apply2(T1, msk_get_starproj(W), msk_get_starproj(Wi));
     898         154 :         Td = Qevproj_apply2(Td, msk_get_starproj(W), msk_get_starproj(Wi));
     899             :       }
     900         168 :       av2 = avma;
     901         168 :       T1 = RgM_mul(T1,S);
     902         168 :       Td = RgM_mul(Td,S);  /* multiply by S = restrict to mscusp */
     903         168 :       gerepileallsp(av, av2, 2, &T1, &Td);
     904         168 :       gel(v,2*i-1) = T1;
     905         168 :       gel(v,2*i)   = Td;
     906             :     }
     907         105 :     S = ZM_mul(S, QM_ker_r(matconcat(v))); /* Snew */
     908         105 :     S = Qevproj_init(vec_Q_primpart(S));
     909             :   }
     910         280 :   return gerepilecopy(av, S);
     911             : }
     912             : 
     913             : /* Solve the Manin relations for a congruence subgroup \Gamma by constructing
     914             :  * a well-formed fundamental domain for the action of \Gamma on upper half
     915             :  * space. See
     916             :  * Pollack and Stevens, Overconvergent modular symbols and p-adic L-functions
     917             :  * Annales scientifiques de l'ENS 44, fascicule 1 (2011), 1-42
     918             :  * http://math.bu.edu/people/rpollack/Papers/Overconvergent_modular_symbols_and_padic_Lfunctions.pdf
     919             :  *
     920             :  * FIXME: Implemented for \Gamma = \Gamma_0(N) only. */
     921             : 
     922             : #if 0 /* Pollack-Stevens shift their paths so as to solve equations of the
     923             :          form f(z+1) - f(z) = g. We don't (to avoid mistakes) so we will
     924             :          have to solve eqs of the form f(z-1) - f(z) = g */
     925             : /* c = a/b; as a t_VECSMALL [a,b]; return c-1 as a t_VECSMALL */
     926             : static GEN
     927             : Shift_left_cusp(GEN c) { long a=c[1], b=c[2]; return mkvecsmall2(a - b, b); }
     928             : /* c = a/b; as a t_VECSMALL [a,b]; return c+1 as a t_VECSMALL */
     929             : static GEN
     930             : Shift_right_cusp(GEN c) { long a=c[1], b=c[2]; return mkvecsmall2(a + b, b); }
     931             : /*Input: path = [r,s] (thought of as a geodesic between these points)
     932             :  *Output: The path shifted by one to the left, i.e. [r-1,s-1] */
     933             : static GEN
     934             : Shift_left(GEN path)
     935             : {
     936             :   GEN r = gel(path,1), s = gel(path,2);
     937             :   return mkvec2(Shift_left_cusp(r), Shift_left_cusp(s)); }
     938             : /*Input: path = [r,s] (thought of as a geodesic between these points)
     939             :  *Output: The path shifted by one to the right, i.e. [r+1,s+1] */
     940             : GEN
     941             : Shift_right(GEN path)
     942             : {
     943             :   GEN r = gel(path,1), s = gel(path,2);
     944             :   return mkvec2(Shift_right_cusp(r), Shift_right_cusp(s)); }
     945             : #endif
     946             : 
     947             : /* linked lists */
     948             : typedef struct list_t { GEN data; struct list_t *next; } list_t;
     949             : static list_t *
     950       77700 : list_new(GEN x)
     951             : {
     952       77700 :   list_t *L = (list_t*)stack_malloc(sizeof(list_t));
     953       77700 :   L->data = x;
     954       77700 :   L->next = NULL; return L;
     955             : }
     956             : static void
     957       76034 : list_insert(list_t *L, GEN x)
     958             : {
     959       76034 :   list_t *l = list_new(x);
     960       76034 :   l->next = L->next;
     961       76034 :   L->next = l;
     962       76034 : }
     963             : 
     964             : /*Input: N > 1, p1N = P^1(Z/NZ)
     965             :  *Output: a connected fundamental domain for the action of \Gamma_0(N) on
     966             :  *  upper half space.  When \Gamma_0(N) is torsion free, the domain has the
     967             :  *  property that all of its vertices are cusps.  When \Gamma_0(N) has
     968             :  *  three-torsion, 2 extra triangles need to be added.
     969             :  *
     970             :  * The domain is constructed by beginning with the triangle with vertices 0,1
     971             :  * and oo.  Each adjacent triangle is successively tested to see if it contains
     972             :  * points not \Gamma_0(N) equivalent to some point in our region.  If a
     973             :  * triangle contains new points, it is added to the region.  This process is
     974             :  * continued until the region can no longer be extended (and still be a
     975             :  * fundamental domain) by added an adjacent triangle.  The list of cusps
     976             :  * between 0 and 1 are then returned
     977             :  *
     978             :  * Precisely, the function returns a list such that the elements of the list
     979             :  * with odd index are the cusps in increasing order.  The even elements of the
     980             :  * list are either an "x" or a "t".  A "t" represents that there is an element
     981             :  * of order three such that its fixed point is in the triangle directly
     982             :  * adjacent to the our region with vertices given by the cusp before and after
     983             :  * the "t".  The "x" represents that this is not the case. */
     984             : enum { type_X, type_DO /* ? */, type_T };
     985             : static GEN
     986        1666 : form_list_of_cusps(ulong N, GEN p1N)
     987             : {
     988        1666 :   pari_sp av = avma;
     989        1666 :   long i, position, nbC = 2;
     990             :   GEN v, L;
     991             :   list_t *C, *c;
     992             :   /* Let t be the index of a class in PSL2(Z) / \Gamma in our fixed enumeration
     993             :    * v[t] != 0 iff it is the class of z tau^r for z a previous alpha_i
     994             :    * or beta_i.
     995             :    * For \Gamma = \Gamma_0(N), the enumeration is given by p1_index.
     996             :    * We write cl(gamma) = the class of gamma mod \Gamma */
     997        1666 :   v = const_vecsmall(p1_size(p1N), 0);
     998        1666 :   i = p1_index( 0, 1, p1N); v[i] = 1;
     999        1666 :   i = p1_index( 1,-1, p1N); v[i] = 2;
    1000        1666 :   i = p1_index(-1, 0, p1N); v[i] = 3;
    1001             :   /* the value is unused [debugging]: what matters is whether it is != 0 */
    1002        1666 :   position = 4;
    1003             :   /* at this point, Fund = R, v contains the classes of Id, tau, tau^2 */
    1004             : 
    1005        1666 :   C  = list_new(mkvecsmall3(0,1, type_X));
    1006        1666 :   list_insert(C, mkvecsmall3(1,1,type_DO));
    1007             :   /* C is a list of triples[a,b,t], where c = a/b is a cusp, and t is the type
    1008             :    * of the path between c and the PREVIOUS cusp in the list, coded as
    1009             :    *   type_DO = "?", type_X = "x", type_T = "t"
    1010             :    * Initially, C = [0/1,"?",1/1]; */
    1011             : 
    1012             :   /* loop through the current set of cusps C and check to see if more cusps
    1013             :    * should be added */
    1014             :   for (;;)
    1015             :   {
    1016        9149 :     int done = 1;
    1017      369530 :     for (c = C; c; c = c->next)
    1018             :     {
    1019             :       GEN cusp1, cusp2, gam;
    1020             :       long pos, b1, b2, b;
    1021             : 
    1022      369530 :       if (!c->next) break;
    1023      360381 :       cusp1 = c->data; /* = a1/b1 */
    1024      360381 :       cusp2 = (c->next)->data; /* = a2/b2 */
    1025      360381 :       if (cusp2[3] != type_DO) continue;
    1026             : 
    1027             :       /* gam (oo -> 0) = (cusp2 -> cusp1), gam in PSL2(Z) */
    1028      150402 :       gam = path_to_zm(mkpath(cusp2, cusp1)); /* = [a2,a1;b2,b1] */
    1029             :       /* we have normalized the cusp representation so that a1 b2 - a2 b1 = 1 */
    1030      150402 :       b1 = coeff(gam,2,1); b2 = coeff(gam,2,2);
    1031             :       /* gam.1  = (a1 + a2) / (b1 + b2) */
    1032      150402 :       b = b1 + b2;
    1033             :       /* Determine whether the adjacent triangle *below* (cusp1->cusp2)
    1034             :        * should be added */
    1035      150402 :       pos = p1_index(b1,b2, p1N); /* did we see cl(gam) before ? */
    1036      150402 :       if (v[pos])
    1037       75306 :         cusp2[3] = type_X; /* NO */
    1038             :       else
    1039             :       { /* YES */
    1040             :         ulong B1, B2;
    1041       75096 :         v[pos] = position;
    1042       75096 :         i = p1_index(-(b1+b2), b1, p1N); v[i] = position+1;
    1043       75096 :         i = p1_index(b2, -(b1+b2), p1N); v[i] = position+2;
    1044             :         /* add cl(gam), cl(gam*TAU), cl(gam*TAU^2) to v */
    1045       75096 :         position += 3;
    1046             :         /* gam tau gam^(-1) in \Gamma ? */
    1047       75096 :         B1 = smodss(b1, N);
    1048       75096 :         B2 = smodss(b2, N);
    1049       75096 :         if ((Fl_sqr(B2,N) + Fl_sqr(B1,N) + Fl_mul(B1,B2,N)) % N == 0)
    1050         728 :           cusp2[3] = type_T;
    1051             :         else
    1052             :         {
    1053       74368 :           long a1 = coeff(gam, 1,1), a2 = coeff(gam, 1,2);
    1054       74368 :           long a = a1 + a2; /* gcd(a,b) = 1 */
    1055       74368 :           list_insert(c, mkvecsmall3(a,b,type_DO));
    1056       74368 :           c = c->next;
    1057       74368 :           nbC++;
    1058       74368 :           done = 0;
    1059             :         }
    1060             :       }
    1061             :     }
    1062        9149 :     if (done) break;
    1063        7483 :   }
    1064        1666 :   L = cgetg(nbC+1, t_VEC); i = 1;
    1065        1666 :   for (c = C; c; c = c->next) gel(L,i++) = c->data;
    1066        1666 :   return gerepilecopy(av, L);
    1067             : }
    1068             : 
    1069             : /* W an msN. M in PSL2(Z). Return index of M in P1^(Z/NZ) = Gamma0(N) \ PSL2(Z),
    1070             :  * and M0 in Gamma_0(N) such that M = M0 * M', where M' = chosen
    1071             :  * section( PSL2(Z) -> P1^(Z/NZ) ). */
    1072             : static GEN
    1073      499149 : Gamma0N_decompose(GEN W, GEN M, long *index)
    1074             : {
    1075      499149 :   GEN p1N = msN_get_p1N(W), W3 = gel(W,3), section = msN_get_section(W);
    1076             :   GEN A;
    1077      499149 :   ulong N = p1N_get_N(p1N);
    1078      499149 :   ulong c = umodiu(gcoeff(M,2,1), N);
    1079      499149 :   ulong d = umodiu(gcoeff(M,2,2), N);
    1080      499149 :   long s, ind = p1_index(c, d, p1N); /* as an elt of P1(Z/NZ) */
    1081      499149 :   *index = W3[ind]; /* as an elt of F, E2, ... */
    1082      499149 :   M = ZM_zm_mul(M, sl2_inv(gel(section,ind)));
    1083             :   /* normalize mod +/-Id */
    1084      499149 :   A = gcoeff(M,1,1);
    1085      499149 :   s = signe(A);
    1086      499149 :   if (s < 0)
    1087      237181 :     M = ZM_neg(M);
    1088      261968 :   else if (!s)
    1089             :   {
    1090         322 :     GEN C = gcoeff(M,2,1);
    1091         322 :     if (signe(C) < 0) M = ZM_neg(M);
    1092             :   }
    1093      499149 :   return M;
    1094             : }
    1095             : /* W an msN; as above for a path. Return [[ind], M] */
    1096             : static GEN
    1097      158732 : path_Gamma0N_decompose(GEN W, GEN path)
    1098             : {
    1099      158732 :   GEN p1N = msN_get_p1N(W);
    1100      158732 :   GEN p1index_to_ind = gel(W,3);
    1101      158732 :   GEN section = msN_get_section(W);
    1102      158732 :   GEN M = path_to_zm(path);
    1103      158732 :   long p1index = p1_index(cc(M), dd(M), p1N);
    1104      158732 :   long ind = p1index_to_ind[p1index];
    1105      158732 :   GEN M0 = ZM_zm_mul(mat2_to_ZM(M), sl2_inv(gel(section,p1index)));
    1106      158732 :   return mkvec2(mkvecsmall(ind), M0);
    1107             : }
    1108             : 
    1109             : /*Form generators of H_1(X_0(N),{cusps},Z)
    1110             : *
    1111             : *Input: N = integer > 1, p1N = P^1(Z/NZ)
    1112             : *Output: [cusp_list,E,F,T2,T3,E1] where
    1113             : *  cusps_list = list of cusps describing fundamental domain of
    1114             : *    \Gamma_0(N).
    1115             : *  E = list of paths in the boundary of the fundamental domains and oriented
    1116             : *    clockwise such that they do not contain a point
    1117             : *    fixed by an element of order 2 and they are not an edge of a
    1118             : *    triangle containing a fixed point of an element of order 3
    1119             : *  F = list of paths in the interior of the domain with each
    1120             : *    orientation appearing separately
    1121             : * T2 = list of paths in the boundary of domain containing a point fixed
    1122             : *    by an element of order 2 (oriented clockwise)
    1123             : * T3 = list of paths in the boundard of domain which are the edges of
    1124             : *    some triangle containing a fixed point of a matrix of order 3 (both
    1125             : *    orientations appear)
    1126             : * E1 = a sublist of E such that every path in E is \Gamma_0(N)-equivalent to
    1127             : *    either an element of E1 or the flip (reversed orientation) of an element
    1128             : *    of E1.
    1129             : * (Elements of T2 are \Gamma_0(N)-equivalent to their own flip.)
    1130             : *
    1131             : * sec = a list from 1..#p1N of matrices describing a section of the map
    1132             : *   SL_2(Z) to P^1(Z/NZ) given by [a,b;c,d]-->[c,d].
    1133             : *   Given our fixed enumeration of P^1(Z/NZ), the j-th element of the list
    1134             : *   represents the image of the j-th element of P^1(Z/NZ) under the section. */
    1135             : 
    1136             : /* insert path in set T */
    1137             : static void
    1138      228830 : set_insert(hashtable *T, GEN path)
    1139      228830 : { hash_insert(T, path,  (void*)(T->nb + 1)); }
    1140             : 
    1141             : static GEN
    1142       14994 : hash_to_vec(hashtable *h)
    1143             : {
    1144       14994 :   GEN v = cgetg(h->nb + 1, t_VEC);
    1145             :   ulong i;
    1146     1931398 :   for (i = 0; i < h->len; i++)
    1147             :   {
    1148     1916404 :     hashentry *e = h->table[i];
    1149     4211914 :     while (e)
    1150             :     {
    1151      379106 :       GEN key = (GEN)e->key;
    1152      379106 :       long index = (long)e->val;
    1153      379106 :       gel(v, index) = key;
    1154      379106 :       e = e->next;
    1155             :     }
    1156             :   }
    1157       14994 :   return v;
    1158             : }
    1159             : 
    1160             : static long
    1161      116739 : path_to_p1_index(GEN path, GEN p1N)
    1162             : {
    1163      116739 :   GEN M = path_to_zm(path);
    1164      116739 :   return p1_index(cc(M), dd(M), p1N);
    1165             : }
    1166             : 
    1167             : /* Pollack-Stevens sets */
    1168             : typedef struct PS_sets_t {
    1169             :   hashtable *F, *T2, *T31, *T32, *E1, *E2;
    1170             :   GEN E2fromE1, stdE1;
    1171             : } PS_sets_t;
    1172             : 
    1173             : static hashtable *
    1174       13734 : set_init(long max)
    1175       13734 : { return hash_create(max, (ulong(*)(void*))&hash_GEN,
    1176             :                           (int(*)(void*,void*))&gidentical, 1); }
    1177             : /* T = E2fromE1[i] = [c, gamma] */
    1178             : static ulong
    1179     4420227 : E2fromE1_c(GEN T) { return itou(gel(T,1)); }
    1180             : static GEN
    1181      579572 : E2fromE1_Zgamma(GEN T) { return gel(T,2); }
    1182             : static GEN
    1183       38913 : E2fromE1_gamma(GEN T) { return gcoeff(gel(T,2),1,1); }
    1184             : 
    1185             : static void
    1186       77826 : insert_E(GEN path, PS_sets_t *S, GEN p1N)
    1187             : {
    1188       77826 :   GEN rev = vecreverse(path);
    1189       77826 :   long std = path_to_p1_index(rev, p1N);
    1190       77826 :   GEN v = gel(S->stdE1, std);
    1191       77826 :   if (v)
    1192             :   { /* [s, p1], where E1[s] is the path p1 = vecreverse(path) mod \Gamma */
    1193       38913 :     GEN gamma, p1 = gel(v,2);
    1194       38913 :     long r, s = itou(gel(v,1));
    1195             : 
    1196       38913 :     set_insert(S->E2, path);
    1197       38913 :     r = S->E2->nb;
    1198       38913 :     if (gel(S->E2fromE1, r) != gen_0) pari_err_BUG("insert_E");
    1199             : 
    1200       38913 :     gamma = gamma_equiv_matrix(rev, p1);
    1201             :     /* reverse(E2[r]) = gamma * E1[s] */
    1202       38913 :     gel(S->E2fromE1, r) = mkvec2(utoipos(s), to_famat_shallow(gamma,gen_m1));
    1203             :   }
    1204             :   else
    1205             :   {
    1206       38913 :     set_insert(S->E1, path);
    1207       38913 :     std = path_to_p1_index(path, p1N);
    1208       38913 :     gel(S->stdE1, std) = mkvec2(utoipos(S->E1->nb), path);
    1209             :   }
    1210       77826 : }
    1211             : 
    1212             : static GEN
    1213        6664 : cusp_infinity(void) { return mkvecsmall2(1,0); }
    1214             : 
    1215             : static void
    1216        1666 : form_E_F_T(ulong N, GEN p1N, GEN *pC, PS_sets_t *S)
    1217             : {
    1218        1666 :   GEN C, cusp_list = form_list_of_cusps(N, p1N);
    1219        1666 :   long nbgen = lg(cusp_list)-1, nbmanin = p1_size(p1N), r, s, i;
    1220             :   hashtable *F, *T2, *T31, *T32, *E1, *E2;
    1221             : 
    1222        1666 :   *pC = C = cgetg(nbgen+1, t_VEC);
    1223       79366 :   for (i = 1; i <= nbgen; i++)
    1224             :   {
    1225       77700 :     GEN c = gel(cusp_list,i);
    1226       77700 :     gel(C,i) = mkvecsmall2(c[1], c[2]);
    1227             :   }
    1228        1666 :   S->F  = F  = set_init(nbmanin);
    1229        1666 :   S->E1 = E1 = set_init(nbgen);
    1230        1666 :   S->E2 = E2 = set_init(nbgen);
    1231        1666 :   S->T2 = T2 = set_init(nbgen);
    1232        1666 :   S->T31 = T31 = set_init(nbgen);
    1233        1666 :   S->T32 = T32 = set_init(nbgen);
    1234             : 
    1235             :   /* T31 represents the three torsion paths going from left to right */
    1236             :   /* T32 represents the three torsion paths going from right to left */
    1237       77700 :   for (r = 1; r < nbgen; r++)
    1238             :   {
    1239       76034 :     GEN c2 = gel(cusp_list,r+1);
    1240       76034 :     if (c2[3] == type_T)
    1241             :     {
    1242         728 :       GEN c1 = gel(cusp_list,r), path = mkpath(c1,c2), path2 = vecreverse(path);
    1243         728 :       set_insert(T31, path);
    1244         728 :       set_insert(T32, path2);
    1245             :     }
    1246             :   }
    1247             : 
    1248             :   /* to record relations between E2 and E1 */
    1249        1666 :   S->E2fromE1 = zerovec(nbgen);
    1250        1666 :   S->stdE1 = const_vec(nbmanin, NULL);
    1251             : 
    1252             :   /* Assumption later: path [oo,0] is E1[1], path [1,oo] is E2[1] */
    1253             :   {
    1254        1666 :     GEN oo = cusp_infinity();
    1255        1666 :     GEN p1 = mkpath(oo, mkvecsmall2(0,1)); /* [oo, 0] */
    1256        1666 :     GEN p2 = mkpath(mkvecsmall2(1,1), oo); /* [1, oo] */
    1257        1666 :     insert_E(p1, S, p1N);
    1258        1666 :     insert_E(p2, S, p1N);
    1259             :   }
    1260             : 
    1261       77700 :   for (r = 1; r < nbgen; r++)
    1262             :   {
    1263       76034 :     GEN c1 = gel(cusp_list,r);
    1264    18062940 :     for (s = r+1; s <= nbgen; s++)
    1265             :     {
    1266    17986906 :       pari_sp av = avma;
    1267    17986906 :       GEN c2 = gel(cusp_list,s), path;
    1268    17986906 :       GEN d = subii(mulss(c1[1],c2[2]), mulss(c1[2],c2[1]));
    1269    17986906 :       avma = av;
    1270    17986906 :       if (!is_pm1(d)) continue;
    1271             : 
    1272      150402 :       path = mkpath(c1,c2);
    1273      150402 :       if (r+1 == s)
    1274             :       {
    1275       76034 :         GEN w = path;
    1276       76034 :         ulong hash = T31->hash(w); /* T31, T32 use the same hash function */
    1277       76034 :         if (!hash_search2(T31, w, hash) && !hash_search2(T32, w, hash))
    1278             :         {
    1279       75306 :           if (gamma_equiv(path, vecreverse(path), N))
    1280         812 :             set_insert(T2, path);
    1281             :           else
    1282       74494 :             insert_E(path, S, p1N);
    1283             :         }
    1284             :       } else {
    1285       74368 :         set_insert(F, mkvec2(path, mkvecsmall2(r,s)));
    1286       74368 :         set_insert(F, mkvec2(vecreverse(path), mkvecsmall2(s,r)));
    1287             :       }
    1288             :     }
    1289             :   }
    1290        1666 :   setlg(S->E2fromE1, E2->nb+1);
    1291        1666 : }
    1292             : 
    1293             : /* v = \sum n_i g_i, g_i in Sl(2,Z), return \sum n_i g_i^(-1) */
    1294             : static GEN
    1295      841001 : ZSl2_star(GEN v)
    1296             : {
    1297             :   long i, l;
    1298             :   GEN w, G;
    1299      841001 :   if (typ(v) == t_INT) return v;
    1300      841001 :   G = gel(v,1);
    1301      841001 :   w = cgetg_copy(G, &l);
    1302     2003575 :   for (i = 1; i < l; i++)
    1303             :   {
    1304     1162574 :     GEN g = gel(G,i);
    1305     1162574 :     if (typ(g) == t_MAT) g = SL2_inv(g);
    1306     1162574 :     gel(w,i) = g;
    1307             :   }
    1308      841001 :   return ZG_normalize(mkmat2(w, gel(v,2)));
    1309             : }
    1310             : 
    1311             : /* Input: h = set of unimodular paths, p1N = P^1(Z/NZ) = Gamma_0(N)\PSL2(Z)
    1312             :  * Output: Each path is converted to a matrix and then an element of P^1(Z/NZ)
    1313             :  * Append the matrix to W[12], append the index that represents
    1314             :  * these elements of P^1 (the classes mod Gamma_0(N) via our fixed
    1315             :  * enumeration to W[2]. */
    1316             : static void
    1317        9996 : paths_decompose(GEN W, hashtable *h, int flag)
    1318             : {
    1319        9996 :   GEN p1N = ms_get_p1N(W), section = ms_get_section(W);
    1320        9996 :   GEN v = hash_to_vec(h);
    1321        9996 :   long i, l = lg(v);
    1322      238826 :   for (i = 1; i < l; i++)
    1323             :   {
    1324      228830 :     GEN e = gel(v,i);
    1325      228830 :     GEN M = path_to_zm(flag? gel(e,1): e);
    1326      228830 :     long index = p1_index(cc(M), dd(M), p1N);
    1327      228830 :     vecsmalltrunc_append(gel(W,2), index);
    1328      228830 :     gel(section, index) = M;
    1329             :   }
    1330        9996 : }
    1331             : static void
    1332        1666 : fill_W2_W12(GEN W, PS_sets_t *S)
    1333             : {
    1334        1666 :   GEN p1N = msN_get_p1N(W);
    1335        1666 :   long n = p1_size(p1N);
    1336        1666 :   gel(W, 2) = vecsmalltrunc_init(n+1);
    1337        1666 :   gel(W,12) = cgetg(n+1, t_VEC);
    1338             :   /* F contains [path, [index cusp1, index cusp2]]. Others contain paths only */
    1339        1666 :   paths_decompose(W, S->F, 1);
    1340        1666 :   paths_decompose(W, S->E2, 0);
    1341        1666 :   paths_decompose(W, S->T32, 0);
    1342        1666 :   paths_decompose(W, S->E1, 0);
    1343        1666 :   paths_decompose(W, S->T2, 0);
    1344        1666 :   paths_decompose(W, S->T31, 0);
    1345        1666 : }
    1346             : 
    1347             : /* x t_VECSMALL, corresponds to a map x(i) = j, where 1 <= j <= max for all i
    1348             :  * Return y s.t. y[j] = i or 0 (not in image) */
    1349             : static GEN
    1350        3332 : reverse_list(GEN x, long max)
    1351             : {
    1352        3332 :   GEN y = const_vecsmall(max, 0);
    1353        3332 :   long r, lx = lg(x);
    1354        3332 :   for (r = 1; r < lx; r++) y[ x[r] ] = r;
    1355        3332 :   return y;
    1356             : }
    1357             : 
    1358             : /* go from C[a] to C[b]; return the indices of paths
    1359             :  * E.g. if a < b
    1360             :  *   (C[a]->C[a+1], C[a+1]->C[a+2], ... C[b-1]->C[b])
    1361             :  * (else reverse direction)
    1362             :  * = b - a paths */
    1363             : static GEN
    1364      144942 : F_indices(GEN W, long a, long b)
    1365             : {
    1366      144942 :   GEN v = cgetg(labs(b-a) + 1, t_VEC);
    1367      144942 :   long s, k = 1;
    1368      144942 :   if (a < b) {
    1369       72471 :     GEN index_forward = gel(W,13);
    1370       72471 :     for (s = a; s < b; s++) gel(v,k++) = gel(index_forward,s);
    1371             :   } else {
    1372       72471 :     GEN index_backward = gel(W,14);
    1373       72471 :     for (s = a; s > b; s--) gel(v,k++) = gel(index_backward,s);
    1374             :   }
    1375      144942 :   return v;
    1376             : }
    1377             : /* go from C[a] to C[b] via oo; return the indices of paths
    1378             :  * E.g. if a < b
    1379             :  *   (C[a]->C[a-1], ... C[2]->C[1],
    1380             :  *    C[1]->oo, oo-> C[end],
    1381             :  *    C[end]->C[end-1], ... C[b+1]->C[b])
    1382             :  *  a-1 + 2 + end-(b+1)+1 = end - b + a + 1 paths  */
    1383             : static GEN
    1384        3794 : F_indices_oo(GEN W, long end, long a, long b)
    1385             : {
    1386        3794 :   GEN index_oo = gel(W,15);
    1387        3794 :   GEN v = cgetg(end-labs(b-a)+1 + 1, t_VEC);
    1388        3794 :   long s, k = 1;
    1389             : 
    1390        3794 :   if (a < b) {
    1391        1897 :     GEN index_backward = gel(W,14);
    1392        1897 :     for (s = a; s > 1; s--) gel(v,k++) = gel(index_backward,s);
    1393        1897 :     gel(v,k++) = gel(index_backward,1); /* C[1] -> oo */
    1394        1897 :     gel(v,k++) = gel(index_oo,2); /* oo -> C[end] */
    1395        1897 :     for (s = end; s > b; s--) gel(v,k++) = gel(index_backward,s);
    1396             :   } else {
    1397        1897 :     GEN index_forward = gel(W,13);
    1398        1897 :     for (s = a; s < end; s++) gel(v,k++) = gel(index_forward,s);
    1399        1897 :     gel(v,k++) = gel(index_forward,end); /* C[end] -> oo */
    1400        1897 :     gel(v,k++) = gel(index_oo,1); /* oo -> C[1] */
    1401        1897 :     for (s = 1; s < b; s++) gel(v,k++) = gel(index_forward,s);
    1402             :   }
    1403        3794 :   return v;
    1404             : }
    1405             : /* index of oo -> C[1], oo -> C[end] */
    1406             : static GEN
    1407        1666 : indices_oo(GEN W, GEN C)
    1408             : {
    1409        1666 :   long end = lg(C)-1;
    1410        1666 :   GEN w, v = cgetg(2+1, t_VEC), oo = cusp_infinity();
    1411        1666 :   w = mkpath(oo, gel(C,1)); /* oo -> C[1]=0 */
    1412        1666 :   gel(v,1) = path_Gamma0N_decompose(W, w);
    1413        1666 :   w = mkpath(oo, gel(C,end)); /* oo -> C[end]=1 */
    1414        1666 :   gel(v,2) = path_Gamma0N_decompose(W, w);
    1415        1666 :   return v;
    1416             : }
    1417             : 
    1418             : /* index of C[1]->C[2], C[2]->C[3], ... C[end-1]->C[end], C[end]->oo
    1419             :  * Recall that C[1] = 0, C[end] = 1 */
    1420             : static GEN
    1421        1666 : indices_forward(GEN W, GEN C)
    1422             : {
    1423        1666 :   long s, k = 1, end = lg(C)-1;
    1424        1666 :   GEN v = cgetg(end+1, t_VEC);
    1425       79366 :   for (s = 1; s <= end; s++)
    1426             :   {
    1427       77700 :     GEN w = mkpath(gel(C,s), s == end? cusp_infinity(): gel(C,s+1));
    1428       77700 :     gel(v,k++) = path_Gamma0N_decompose(W, w);
    1429             :   }
    1430        1666 :   return v;
    1431             : }
    1432             : /* index of C[1]->oo, C[2]->C[1], ... C[end]->C[end-1] */
    1433             : static GEN
    1434        1666 : indices_backward(GEN W, GEN C)
    1435             : {
    1436        1666 :   long s, k = 1, end = lg(C)-1;
    1437        1666 :   GEN v = cgetg(end+1, t_VEC);
    1438       79366 :   for (s = 1; s <= end; s++)
    1439             :   {
    1440       77700 :     GEN w = mkpath(gel(C,s), s == 1? cusp_infinity(): gel(C,s-1));
    1441       77700 :     gel(v,k++) = path_Gamma0N_decompose(W, w);
    1442             :   }
    1443        1666 :   return v;
    1444             : }
    1445             : 
    1446             : /*[0,-1;1,-1]*/
    1447             : static GEN
    1448        1708 : mkTAU()
    1449        1708 : { return mkmat22(gen_0,gen_m1, gen_1,gen_m1); }
    1450             : /* S */
    1451             : static GEN
    1452          42 : mkS()
    1453          42 : { return mkmat22(gen_0,gen_1, gen_m1,gen_0); }
    1454             : /* N = integer > 1. Returns data describing Delta_0 = Z[P^1(Q)]_0 seen as
    1455             :  * a Gamma_0(N) - module. */
    1456             : static GEN
    1457        1687 : msinit_N(ulong N)
    1458             : {
    1459             :   GEN p1N, C, vecF, vecT2, vecT31, TAU, W, W2, singlerel, annT2, annT31;
    1460             :   GEN F_index;
    1461             :   ulong r, s, width;
    1462             :   long nball, nbgen, nbp1N;
    1463             :   hashtable *F, *T2, *T31, *T32, *E1, *E2;
    1464             :   PS_sets_t S;
    1465             : 
    1466        1687 :   W = zerovec(16);
    1467        1687 :   gel(W,1) = p1N = create_p1mod(N);
    1468        1687 :   gel(W,16)= inithashcusps(p1N);
    1469        1687 :   TAU = mkTAU();
    1470        1687 :   if (N == 1)
    1471             :   {
    1472          21 :     gel(W,5) = mkvecsmall(1);
    1473             :     /* cheat because sets are not disjoint if N=1 */
    1474          21 :     gel(W,11) = mkvecsmall5(0, 0, 1, 1, 2);
    1475          21 :     gel(W,12) = mkvec(mat2(1,0,0,1));
    1476          21 :     gel(W,8) = mkvec( mkmat22(gen_1,gen_1, mkS(),gen_1) );
    1477          21 :     gel(W,9) = mkvec( mkmat2(mkcol3(gen_1,TAU,ZM_sqr(TAU)),
    1478             :                              mkcol3(gen_1,gen_1,gen_1)) );
    1479          21 :     return W;
    1480             :   }
    1481        1666 :   nbp1N = p1_size(p1N);
    1482        1666 :   form_E_F_T(N,p1N, &C, &S);
    1483        1666 :   E1  = S.E1;
    1484        1666 :   E2  = S.E2;
    1485        1666 :   T31 = S.T31;
    1486        1666 :   T32 = S.T32;
    1487        1666 :   F   = S.F;
    1488        1666 :   T2  = S.T2;
    1489        1666 :   nbgen = lg(C)-1;
    1490             : 
    1491             :  /* Put our paths in the order: F,E2,T32,E1,T2,T31
    1492             :   * W2[j] associates to the j-th element of this list its index in P1. */
    1493        1666 :   fill_W2_W12(W, &S);
    1494        1666 :   W2 = gel(W, 2);
    1495        1666 :   nball = lg(W2)-1;
    1496        1666 :   gel(W,3) = reverse_list(W2, nbp1N);
    1497        1666 :   gel(W,5) = vecslice(gel(W,2), F->nb + E2->nb + T32->nb + 1, nball);
    1498        1666 :   gel(W,4) = reverse_list(gel(W,5), nbp1N);
    1499        1666 :   gel(W,13) = indices_forward(W, C);
    1500        1666 :   gel(W,14) = indices_backward(W, C);
    1501        1666 :   gel(W,15) = indices_oo(W, C);
    1502        8330 :   gel(W,11) = mkvecsmall5(F->nb,
    1503        1666 :                           F->nb + E2->nb,
    1504        1666 :                           F->nb + E2->nb + T32->nb,
    1505        1666 :                           F->nb + E2->nb + T32->nb + E1->nb,
    1506        1666 :                           F->nb + E2->nb + T32->nb + E1->nb + T2->nb);
    1507             :   /* relations between T32 and T31 [not stored!]
    1508             :    * T32[i] = - T31[i] */
    1509             : 
    1510             :   /* relations of F */
    1511        1666 :   width = E1->nb + T2->nb + T31->nb;
    1512             :   /* F_index[r] = [index_1, ..., index_k], where index_i is the p1_index()
    1513             :    * of the elementary unimodular path between 2 consecutive cusps
    1514             :    * [in E1,E2,T2,T31 or T32] */
    1515        1666 :   F_index = cgetg(F->nb+1, t_VEC);
    1516        1666 :   vecF = hash_to_vec(F);
    1517      150402 :   for (r = 1; r <= F->nb; r++)
    1518             :   {
    1519      148736 :     GEN w = gel(gel(vecF,r), 2);
    1520      148736 :     long a = w[1], b = w[2], d = labs(b - a);
    1521             :     /* c1 = cusp_list[a],  c2 = cusp_list[b], ci != oo */
    1522      297472 :     gel(F_index,r) = (nbgen-d >= d-1)? F_indices(W, a,b)
    1523      148736 :                                      : F_indices_oo(W, lg(C)-1,a,b);
    1524             :   }
    1525             : 
    1526        1666 :   singlerel = cgetg(width+1, t_VEC);
    1527             :   /* form the single boundary relation */
    1528       40579 :   for (s = 1; s <= E2->nb; s++)
    1529             :   { /* reverse(E2[s]) = gamma * E1[c] */
    1530       38913 :     GEN T = gel(S.E2fromE1,s), gamma = E2fromE1_gamma(T);
    1531       38913 :     gel(singlerel, E2fromE1_c(T)) = mkmat22(gen_1,gen_1, gamma,gen_m1);
    1532             :   }
    1533        1666 :   for (r = E1->nb + 1; r <= width; r++) gel(singlerel, r) = gen_1;
    1534             : 
    1535             :   /* form the 2-torsion relations */
    1536        1666 :   annT2 = cgetg(T2->nb+1, t_VEC);
    1537        1666 :   vecT2 = hash_to_vec(T2);
    1538        2478 :   for (r = 1; r <= T2->nb; r++)
    1539             :   {
    1540         812 :     GEN w = gel(vecT2,r);
    1541         812 :     GEN gamma = gamma_equiv_matrix(vecreverse(w), w);
    1542         812 :     gel(annT2, r) = mkmat22(gen_1,gen_1, gamma,gen_1);
    1543             :   }
    1544             : 
    1545             :   /* form the 3-torsion relations */
    1546        1666 :   annT31 = cgetg(T31->nb+1, t_VEC);
    1547        1666 :   vecT31 = hash_to_vec(T31);
    1548        2394 :   for (r = 1; r <= T31->nb; r++)
    1549             :   {
    1550         728 :     GEN M = path_to_ZM( vecreverse(gel(vecT31,r)) );
    1551         728 :     GEN gamma = ZM_mul(ZM_mul(M, TAU), SL2_inv(M));
    1552         728 :     gel(annT31, r) = mkmat2(mkcol3(gen_1,gamma,ZM_sqr(gamma)),
    1553             :                             mkcol3(gen_1,gen_1,gen_1));
    1554             :   }
    1555        1666 :   gel(W,6) = F_index;
    1556        1666 :   gel(W,7) = S.E2fromE1;
    1557        1666 :   gel(W,8) = annT2;
    1558        1666 :   gel(W,9) = annT31;
    1559        1666 :   gel(W,10)= singlerel;
    1560        1666 :   return W;
    1561             : }
    1562             : static GEN
    1563         112 : cusp_to_P1Q(GEN c) { return c[2]? gdivgs(stoi(c[1]), c[2]): mkoo(); }
    1564             : static GEN
    1565          21 : mspathgens_i(GEN W)
    1566             : {
    1567             :   GEN R, r, g, section, gen, annT2, annT31;
    1568             :   long i, l;
    1569          21 :   checkms(W); W = get_msN(W);
    1570          21 :   section = msN_get_section(W);
    1571          21 :   gen = ms_get_genindex(W);
    1572          21 :   l = lg(gen);
    1573          21 :   g = cgetg(l,t_VEC);
    1574          77 :   for (i = 1; i < l; i++)
    1575             :   {
    1576          56 :     GEN p = gel(section,gen[i]);
    1577          56 :     gel(g,i) = mkvec2(cusp_to_P1Q(gel(p,1)), cusp_to_P1Q(gel(p,2)));
    1578             :   }
    1579          21 :   annT2 = msN_get_annT2(W);
    1580          21 :   annT31= msN_get_annT31(W);
    1581          21 :   if (ms_get_N(W) == 1)
    1582             :   {
    1583           7 :     R = cgetg(3, t_VEC);
    1584           7 :     gel(R,1) = mkvec( mkvec2(gel(annT2,1), gen_1) );
    1585           7 :     gel(R,2) = mkvec( mkvec2(gel(annT31,1), gen_1) );
    1586             :   }
    1587             :   else
    1588             :   {
    1589          14 :     GEN singlerel = msN_get_singlerel(W);
    1590          14 :     long j, nbT2 = lg(annT2)-1, nbT31 = lg(annT31)-1, nbE1 = ms_get_nbE1(W);
    1591          14 :     R = cgetg(nbT2+nbT31+2, t_VEC);
    1592          14 :     l = lg(singlerel);
    1593          14 :     r = cgetg(l, t_VEC);
    1594          42 :     for (i = 1; i <= nbE1; i++)
    1595          28 :       gel(r,i) = mkvec2(gel(singlerel, i), utoi(i));
    1596          35 :     for (; i < l; i++)
    1597          21 :       gel(r,i) = mkvec2(gen_1, utoi(i));
    1598          14 :     gel(R,1) = r; j = 2;
    1599          35 :     for (i = 1; i <= nbT2; i++,j++)
    1600          21 :       gel(R,j) = mkvec( mkvec2(gel(annT2,i), utoi(i + nbE1)) );
    1601          14 :     for (i = 1; i <= nbT31; i++,j++)
    1602           0 :       gel(R,j) = mkvec( mkvec2(gel(annT31,i), utoi(i + nbE1 + nbT2)) );
    1603             :   }
    1604          21 :   return mkvec2(g,R);
    1605             : }
    1606             : GEN
    1607          21 : mspathgens(GEN W)
    1608             : {
    1609          21 :   pari_sp av = avma;
    1610          21 :   return gerepilecopy(av, mspathgens_i(W));
    1611             : }
    1612             : /* Modular symbols in weight k: Hom_Gamma(Delta, Q[x,y]_{k-2}) */
    1613             : /* A symbol phi is represented by the {phi(g_i)}, {phi(g'_i)}, {phi(g''_i)}
    1614             :  * where the {g_i, g'_i, g''_i} are the Z[\Gamma]-generators of Delta,
    1615             :  * g_i corresponds to E1, g'_i to T2, g''_i to T31.
    1616             :  */
    1617             : 
    1618             : /* FIXME: export. T^1, ..., T^n */
    1619             : static GEN
    1620      686896 : RgX_powers(GEN T, long n)
    1621             : {
    1622      686896 :   GEN v = cgetg(n+1, t_VEC);
    1623             :   long i;
    1624      686896 :   gel(v, 1) = T;
    1625      686896 :   for (i = 1; i < n; i++) gel(v,i+1) = RgX_mul(gel(v,i), T);
    1626      686896 :   return v;
    1627             : }
    1628             : 
    1629             : /* g = [a,b;c,d] a mat2. Return (X^{k-2} | g)(X,Y)[X = 1]. */
    1630             : static GEN
    1631        2926 : voo_act_Gl2Q(GEN g, long k)
    1632             : {
    1633        2926 :   GEN mc = stoi(-coeff(g,2,1)), d = stoi(coeff(g,2,2));
    1634        2926 :   return RgX_to_RgC(gpowgs(deg1pol_shallow(mc, d, 0), k-2), k-1);
    1635             : }
    1636             : 
    1637             : struct m_act {
    1638             :   long dim, k, p;
    1639             :   GEN q;
    1640             :   GEN(*act)(struct m_act *,GEN);
    1641             : };
    1642             : 
    1643             : /* g = [a,b;c,d]. Return (P | g)(X,Y)[X = 1] = P(dX - cY, -b X + aY)[X = 1],
    1644             :  * for P = X^{k-2}, X^{k-3}Y, ..., Y^{k-2} */
    1645             : GEN
    1646      343448 : RgX_act_Gl2Q(GEN g, long k)
    1647             : {
    1648             :   GEN a,b,c,d, V1,V2,V;
    1649             :   long i;
    1650      343448 :   if (k == 2) return matid(1);
    1651      343448 :   a = gcoeff(g,1,1); b = gcoeff(g,1,2);
    1652      343448 :   c = gcoeff(g,2,1); d = gcoeff(g,2,2);
    1653      343448 :   V1 = RgX_powers(deg1pol_shallow(gneg(c), d, 0), k-2); /* d - c Y */
    1654      343448 :   V2 = RgX_powers(deg1pol_shallow(a, gneg(b), 0), k-2); /*-b + a Y */
    1655      343448 :   V = cgetg(k, t_MAT);
    1656      343448 :   gel(V,1)   = RgX_to_RgC(gel(V1, k-2), k-1);
    1657      805084 :   for (i = 1; i < k-2; i++)
    1658             :   {
    1659      461636 :     GEN v1 = gel(V1, k-2-i); /* (d-cY)^(k-2-i) */
    1660      461636 :     GEN v2 = gel(V2, i); /* (-b+aY)^i */
    1661      461636 :     gel(V,i+1) = RgX_to_RgC(RgX_mul(v1,v2), k-1);
    1662             :   }
    1663      343448 :   gel(V,k-1) = RgX_to_RgC(gel(V2, k-2), k-1);
    1664      343448 :   return V; /* V[i+1] = X^i | g */
    1665             : }
    1666             : /* z in Z[Gl2(Q)], return the matrix of z acting on V */
    1667             : static GEN
    1668      595504 : act_ZGl2Q(GEN z, struct m_act *T, hashtable *H)
    1669             : {
    1670      595504 :   GEN S = NULL, G, E;
    1671             :   pari_sp av;
    1672             :   long l, j;
    1673             :   /* paranoia: should not occur */
    1674      595504 :   if (typ(z) == t_INT) return scalarmat_shallow(z, T->dim);
    1675      595504 :   G = gel(z,1); l = lg(G);
    1676      595504 :   E = gel(z,2); av = avma;
    1677     1758078 :   for (j = 1; j < l; j++)
    1678             :   {
    1679     1162574 :     GEN M, g = gel(G,j), n = gel(E,j);
    1680     1162574 :     if (typ(g) == t_INT) /* = 1 */
    1681        3927 :       M = n; /* n*Id_dim */
    1682             :     else
    1683             :     { /*search in H succeeds because of preload*/
    1684     1158647 :       M = H? (GEN)hash_search(H,g)->val: T->act(T,g);
    1685     1158647 :       if (is_pm1(n))
    1686     1151710 :       { if (signe(n) < 0) M = RgM_neg(M); }
    1687             :       else
    1688        6937 :         M = RgM_Rg_mul(M, n);
    1689             :     }
    1690     1162574 :     if (!S) { S = M; continue; }
    1691      567070 :     S = gadd(S, M);
    1692      567070 :     if (gc_needed(av,1))
    1693             :     {
    1694           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"act_ZGl2Q, j = %ld",j);
    1695           0 :       S = gerepileupto(av, S);
    1696             :     }
    1697             :   }
    1698      595504 :   return gerepilecopy(av, S);
    1699             : }
    1700             : static GEN
    1701      343301 : _RgX_act_Gl2Q(struct m_act *S, GEN z) { return RgX_act_Gl2Q(z, S->k); }
    1702             : /* acting on (X^{k-2},...,Y^{k-2}) */
    1703             : GEN
    1704       55398 : RgX_act_ZGl2Q(GEN z, long k)
    1705             : {
    1706             :   struct m_act T;
    1707       55398 :   T.k = k;
    1708       55398 :   T.dim = k-1;
    1709       55398 :   T.act=&_RgX_act_Gl2Q;
    1710       55398 :   return act_ZGl2Q(z, &T, NULL);
    1711             : }
    1712             : 
    1713             : /* First pass, identify matrices in Sl_2 to convert to operators;
    1714             :  * insert operators in hashtable. This allows GC in act_ZGl2Q */
    1715             : static void
    1716     1070832 : hash_preload(GEN M, struct m_act *S, hashtable *H)
    1717             : {
    1718     1070832 :   if (typ(M) != t_INT)
    1719             :   {
    1720     1070832 :     ulong h = H->hash(M);
    1721     1070832 :     hashentry *e = hash_search2(H, M, h);
    1722     1070832 :     if (!e) hash_insert2(H, M, S->act(S,M), h);
    1723             :   }
    1724     1070832 : }
    1725             : /* z a sparse operator */
    1726             : static void
    1727      540092 : hash_vecpreload(GEN z, struct m_act *S, hashtable *H)
    1728             : {
    1729      540092 :   GEN G = gel(z,1);
    1730      540092 :   long i, l = lg(G);
    1731      540092 :   for (i = 1; i < l; i++) hash_preload(gel(G,i), S, H);
    1732      540092 : }
    1733             : static void
    1734       40817 : ZGl2QC_preload(struct m_act *S, GEN v, hashtable *H)
    1735             : {
    1736       40817 :   GEN val = gel(v,2);
    1737       40817 :   long i, l = lg(val);
    1738       40817 :   for (i = 1; i < l; i++) hash_vecpreload(gel(val,i), S, H);
    1739       40817 : }
    1740             : /* Given a sparse vector of elements in Z[G], convert it to a (sparse) vector
    1741             :  * of operators on V (given by t_MAT) */
    1742             : static void
    1743       40831 : ZGl2QC_to_act(struct m_act *S, GEN v, hashtable *H)
    1744             : {
    1745       40831 :   GEN val = gel(v,2);
    1746       40831 :   long i, l = lg(val);
    1747       40831 :   for (i = 1; i < l; i++) gel(val,i) = act_ZGl2Q(gel(val,i), S, H);
    1748       40831 : }
    1749             : 
    1750             : /* For all V[i] in Z[\Gamma], find the P such that  P . V[i]^* = 0;
    1751             :  * write P in basis X^{k-2}, ..., Y^{k-2} */
    1752             : static GEN
    1753        1246 : ZGV_tors(GEN V, long k)
    1754             : {
    1755        1246 :   long i, l = lg(V);
    1756        1246 :   GEN v = cgetg(l, t_VEC);
    1757        1750 :   for (i = 1; i < l; i++)
    1758             :   {
    1759         504 :     GEN a = ZSl2_star(gel(V,i));
    1760         504 :     gel(v,i) = ZM_ker(RgX_act_ZGl2Q(a,k));
    1761             :   }
    1762        1246 :   return v;
    1763             : }
    1764             : 
    1765             : static long
    1766    10801707 : set_from_index(GEN W11, long i)
    1767             : {
    1768    10801707 :   if (i <= W11[1]) return 1;
    1769     9604014 :   if (i <= W11[2]) return 2;
    1770     5228412 :   if (i <= W11[3]) return 3;
    1771     5219669 :   if (i <= W11[4]) return 4;
    1772       31367 :   if (i <= W11[5]) return 5;
    1773        8463 :   return 6;
    1774             : }
    1775             : 
    1776             : /* det M = 1 */
    1777             : static void
    1778     1535625 : treat_index(GEN W, GEN M, long index, GEN v)
    1779             : {
    1780     1535625 :   GEN W11 = gel(W,11);
    1781     1535625 :   long shift = W11[3]; /* #F + #E2 + T32 */
    1782     1535625 :   switch(set_from_index(W11, index))
    1783             :   {
    1784             :     case 1: /*F*/
    1785             :     {
    1786      251237 :       GEN F_index = gel(W,6), ind = gel(F_index, index);
    1787      251237 :       long j, l = lg(ind);
    1788     1287713 :       for (j = 1; j < l; j++)
    1789             :       {
    1790     1036476 :         GEN IND = gel(ind,j), M0 = gel(IND,2);
    1791     1036476 :         long index = mael(IND,1,1);
    1792     1036476 :         treat_index(W, ZM_mul(M,M0), index, v);
    1793             :       }
    1794      251237 :       break;
    1795             :     }
    1796             : 
    1797             :     case 2: /*E2, E2[r] + gamma * E1[s] = 0 */
    1798             :     {
    1799      579572 :       long r = index - W11[1];
    1800      579572 :       GEN z = gel(msN_get_E2fromE1(W), r);
    1801             : 
    1802      579572 :       index = E2fromE1_c(z);
    1803      579572 :       M = G_ZG_mul(M, E2fromE1_Zgamma(z)); /* M * (-gamma) */
    1804      579572 :       gel(v, index) = ZG_add(gel(v, index), M);
    1805      579572 :       break;
    1806             :     }
    1807             : 
    1808             :     case 3: /*T32, T32[i] = -T31[i] */
    1809             :     {
    1810        6006 :       long T3shift = W11[5] - W11[2]; /* #T32 + #E1 + #T2 */
    1811        6006 :       index += T3shift;
    1812        6006 :       index -= shift;
    1813        6006 :       gel(v, index) = ZG_add(gel(v, index), to_famat_shallow(M,gen_m1));
    1814        6006 :       break;
    1815             :     }
    1816             :     default: /*E1,T2,T31*/
    1817      698810 :       index -= shift;
    1818      698810 :       gel(v, index) = ZG_add(gel(v, index), to_famat_shallow(M,gen_1));
    1819      698810 :       break;
    1820             :   }
    1821     1535625 : }
    1822             : static void
    1823     9266082 : treat_index_trivial(GEN v, GEN W, long index)
    1824             : {
    1825     9266082 :   GEN W11 = gel(W,11);
    1826     9266082 :   long shift = W11[3]; /* #F + #E2 + T32 */
    1827     9266082 :   switch(set_from_index(W11, index))
    1828             :   {
    1829             :     case 1: /*F*/
    1830             :     {
    1831      946456 :       GEN F_index = gel(W,6), ind = gel(F_index, index);
    1832      946456 :       long j, l = lg(ind);
    1833     8718325 :       for (j = 1; j < l; j++)
    1834             :       {
    1835     7771869 :         GEN IND = gel(ind,j);
    1836     7771869 :         treat_index_trivial(v, W, mael(IND,1,1));
    1837             :       }
    1838      946456 :       break;
    1839             :     }
    1840             : 
    1841             :     case 2: /*E2, E2[r] + gamma * E1[s] = 0 */
    1842             :     {
    1843     3796030 :       long r = index - W11[1];
    1844     3796030 :       long s = E2fromE1_c(gel(msN_get_E2fromE1(W), r));
    1845     3796030 :       v[s]--;
    1846     3796030 :       break;
    1847             :     }
    1848             : 
    1849             :     case 3: case 5: case 6: /*T32,T2,T31*/
    1850       15463 :       break;
    1851             : 
    1852             :     case 4: /*E1*/
    1853     4508133 :       v[index-shift]++;
    1854     4508133 :       break;
    1855             :   }
    1856     9266082 : }
    1857             : 
    1858             : static GEN
    1859      178542 : M2_log(GEN W, GEN M)
    1860             : {
    1861      178542 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    1862      178542 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    1863             :   GEN  u, v, D, V;
    1864             :   long index, s;
    1865             : 
    1866      178542 :   W = get_msN(W);
    1867      178542 :   V = zerovec(ms_get_nbgen(W));
    1868             : 
    1869      178542 :   D = subii(mulii(a,d), mulii(b,c));
    1870      178542 :   s = signe(D);
    1871      178542 :   if (!s) return V;
    1872      177177 :   if (is_pm1(D))
    1873             :   { /* shortcut, no need to apply Manin's trick */
    1874       63504 :     if (s < 0) { b = negi(b); d = negi(d); }
    1875       63504 :     M = Gamma0N_decompose(W, mkmat22(a,b, c,d), &index);
    1876       63504 :     treat_index(W, M, index, V);
    1877             :   }
    1878             :   else
    1879             :   {
    1880             :     GEN U, B, P, Q, PQ, C1,C2;
    1881             :     long i, l;
    1882      113673 :     (void)bezout(a,c,&u,&v);
    1883      113673 :     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      113673 :     U = mkmat22(a,negi(v), c,u);
    1886             : 
    1887             :     /* {1/0 -> B/D} as \sum g_i, g_i unimodular paths */
    1888      113673 :     PQ = ZV_allpnqn( gboundcf(gdiv(B,D), 0) );
    1889      113673 :     P = gel(PQ,1); l = lg(P);
    1890      113673 :     Q = gel(PQ,2);
    1891      113673 :     C1 = gel(U,1);
    1892      549318 :     for (i = 1; i < l; i++, C1 = C2)
    1893             :     {
    1894             :       GEN M;
    1895      435645 :       C2 = ZM_ZC_mul(U, mkcol2(gel(P,i), gel(Q,i)));
    1896      435645 :       if (!odd(i)) C1 = ZC_neg(C1);
    1897      435645 :       M = Gamma0N_decompose(W, mkmat2(C1,C2), &index);
    1898      435645 :       treat_index(W, M, index, V);
    1899             :     }
    1900             :   }
    1901      177177 :   return V;
    1902             : }
    1903             : 
    1904             : /* express +oo->q=a/b in terms of the Z[G]-generators, trivial action */
    1905             : static void
    1906        7630 : Q_log_trivial(GEN v, GEN W, GEN q)
    1907             : {
    1908        7630 :   GEN Q, W3 = gel(W,3), p1N = msN_get_p1N(W);
    1909        7630 :   ulong c,d, N = p1N_get_N(p1N);
    1910             :   long i, lx;
    1911             : 
    1912        7630 :   Q = Q_log_init(N, q);
    1913        7630 :   lx = lg(Q);
    1914        7630 :   c = 0;
    1915       31906 :   for (i = 1; i < lx; i++, c = d)
    1916             :   {
    1917             :     long index;
    1918       24276 :     d = Q[i];
    1919       24276 :     if (c && !odd(i)) c = N - c;
    1920       24276 :     index = W3[ p1_index(c,d,p1N) ];
    1921       24276 :     treat_index_trivial(v, W, index);
    1922             :   }
    1923        7630 : }
    1924             : static void
    1925      587223 : M2_log_trivial(GEN V, GEN W, GEN M)
    1926             : {
    1927      587223 :   GEN p1N = gel(W,1), W3 = gel(W,3);
    1928      587223 :   ulong N = p1N_get_N(p1N);
    1929      587223 :   GEN a = gcoeff(M,1,1), b = gcoeff(M,1,2);
    1930      587223 :   GEN c = gcoeff(M,2,1), d = gcoeff(M,2,2);
    1931             :   GEN  u, v, D;
    1932             :   long index, s;
    1933             : 
    1934      587223 :   D = subii(mulii(a,d), mulii(b,c));
    1935      587223 :   s = signe(D);
    1936      592907 :   if (!s) return;
    1937      587188 :   if (is_pm1(D))
    1938             :   { /* shortcut, not need to apply Manin's trick */
    1939      214655 :     if (s < 0) d = negi(d);
    1940      214655 :     index = W3[ p1_index(umodiu(c,N),umodiu(d,N),p1N) ];
    1941      214655 :     treat_index_trivial(V, W, index);
    1942             :   }
    1943             :   else
    1944             :   {
    1945             :     GEN U, B, P, Q, PQ;
    1946             :     long i, l;
    1947      372533 :     if (!signe(c)) { Q_log_trivial(V,W,gdiv(b,d)); return; }
    1948      366884 :     (void)bezout(a,c,&u,&v);
    1949      366884 :     B = addii(mulii(b,u), mulii(d,v));
    1950             :     /* [u,v;-c,a] [a,b; c,d] = [1,B; 0,D], i.e. M = U [1,B;0,D] */
    1951      366884 :     U = mkvec2(c, u);
    1952             : 
    1953             :     /* {1/0 -> B/D} as \sum g_i, g_i unimodular paths */
    1954      366884 :     PQ = ZV_allpnqn( gboundcf(gdiv(B,D), 0) );
    1955      366884 :     P = gel(PQ,1); l = lg(P);
    1956      366884 :     Q = gel(PQ,2);
    1957     1622166 :     for (i = 1; i < l; i++, c = d)
    1958             :     {
    1959     1255282 :       d = addii(mulii(gel(U,1),gel(P,i)), mulii(gel(U,2),gel(Q,i)));
    1960     1255282 :       if (!odd(i)) c = negi(c);
    1961     1255282 :       index = W3[ p1_index(umodiu(c,N),umodiu(d,N),p1N) ];
    1962     1255282 :       treat_index_trivial(V, W, index);
    1963             :     }
    1964             :   }
    1965             : }
    1966             : 
    1967             : static GEN
    1968       16772 : cusp_to_ZC(GEN c)
    1969             : {
    1970       16772 :   switch(typ(c))
    1971             :   {
    1972             :     case t_INFINITY:
    1973          35 :       return mkcol2(gen_1,gen_0);
    1974             :     case t_INT:
    1975          91 :       return mkcol2(c,gen_1);
    1976             :     case t_FRAC:
    1977         140 :       return mkcol2(gel(c,1),gel(c,2));
    1978             :     case t_VECSMALL:
    1979       16506 :       return mkcol2(stoi(c[1]), stoi(c[2]));
    1980             :     default:
    1981           0 :       pari_err_TYPE("mspathlog",c);
    1982           0 :       return NULL;
    1983             :   }
    1984             : }
    1985             : static GEN
    1986        8386 : path2_to_M2(GEN p)
    1987        8386 : { return mkmat2(cusp_to_ZC(gel(p,1)), cusp_to_ZC(gel(p,2))); }
    1988             : static GEN
    1989       15876 : path_to_M2(GEN p)
    1990             : {
    1991       15876 :   if (lg(p) != 3) pari_err_TYPE("mspathlog",p);
    1992       15869 :   switch(typ(p))
    1993             :   {
    1994             :     case t_MAT:
    1995        9597 :       RgM_check_ZM(p,"mspathlog");
    1996        9597 :       break;
    1997             :     case t_VEC:
    1998        6272 :       p = path2_to_M2(p);
    1999        6272 :       break;
    2000           0 :     default: pari_err_TYPE("mspathlog",p);
    2001             :   }
    2002       15869 :   return p;
    2003             : }
    2004             : /* Expresses path p as \sum x_i g_i, where the g_i are our distinguished
    2005             :  * generators and x_i \in Z[\Gamma]. Returns [x_1,...,x_n] */
    2006             : GEN
    2007       12523 : mspathlog(GEN W, GEN p)
    2008             : {
    2009       12523 :   pari_sp av = avma;
    2010       12523 :   checkms(W);
    2011       12523 :   return gerepilecopy(av, M2_log(W, path_to_M2(p)));
    2012             : }
    2013             : 
    2014             : /** HECKE OPERATORS **/
    2015             : /* [a,b;c,d] * cusp */
    2016             : static GEN
    2017     1489684 : cusp_mul(long a, long b, long c, long d, GEN cusp)
    2018             : {
    2019     1489684 :   long x = cusp[1], y = cusp[2];
    2020     1489684 :   long A = a*x+b*y, B = c*x+d*y, u = cgcd(A,B);
    2021     1489684 :   if (u != 1) { A /= u; B /= u; }
    2022     1489684 :   return mkcol2s(A, B);
    2023             : }
    2024             : /* f in Gl2(Q), act on path (zm), return path_to_M2(f.path) */
    2025             : static GEN
    2026      744842 : Gl2Q_act_path(GEN f, GEN path)
    2027             : {
    2028      744842 :   long a = coeff(f,1,1), b = coeff(f,1,2);
    2029      744842 :   long c = coeff(f,2,1), d = coeff(f,2,2);
    2030      744842 :   GEN c1 = cusp_mul(a,b,c,d, gel(path,1));
    2031      744842 :   GEN c2 = cusp_mul(a,b,c,d, gel(path,2));
    2032      744842 :   return mkmat2(c1,c2);
    2033             : }
    2034             : 
    2035             : static GEN
    2036      151809 : init_act_trivial(GEN W) { return const_vecsmall(ms_get_nbE1(W), 0); }
    2037             : static GEN
    2038        3339 : mspathlog_trivial(GEN W, GEN p)
    2039             : {
    2040             :   GEN v;
    2041        3339 :   W = get_msN(W);
    2042        3339 :   v = init_act_trivial(W);
    2043        3339 :   M2_log_trivial(v, W, path_to_M2(p));
    2044        3332 :   return v;
    2045             : }
    2046             : 
    2047             : /* map from W1=Hom(Delta_0(N1),Q) -> W2=Hom(Delta_0(N2),Q), weight 2,
    2048             :  * trivial action. v a Gl2_Q or a t_VEC of Gl2_Q (\sum v[i] in Z[Gl2(Q)]).
    2049             :  * Return the matrix attached to the action of v. */
    2050             : static GEN
    2051        2702 : getMorphism_trivial(GEN WW1, GEN WW2, GEN v)
    2052             : {
    2053        2702 :   GEN T, section, gen, W1 = get_msN(WW1), W2 = get_msN(WW2);
    2054             :   long j, lv, d2;
    2055        2702 :   if (ms_get_N(W1) == 1) return cgetg(1,t_MAT);
    2056        2702 :   if (ms_get_N(W2) == 1) return zeromat(0, ms_get_nbE1(W1));
    2057        2702 :   section = msN_get_section(W2);
    2058        2702 :   gen = msN_get_genindex(W2);
    2059        2702 :   d2 = ms_get_nbE1(W2);
    2060        2702 :   T = cgetg(d2+1, t_MAT);
    2061        2702 :   lv = lg(v);
    2062      149191 :   for (j = 1; j <= d2; j++)
    2063             :   {
    2064      146489 :     GEN w = gel(section, gen[j]);
    2065      146489 :     GEN t = init_act_trivial(W1);
    2066             :     long l;
    2067      146489 :     for (l = 1; l < lv; l++) M2_log_trivial(t, W1, Gl2Q_act_path(gel(v,l), w));
    2068      146489 :     gel(T,j) = t;
    2069             :   }
    2070        2702 :   return shallowtrans(zm_to_ZM(T));
    2071             : }
    2072             : 
    2073             : static GEN
    2074      166019 : RgV_sparse(GEN v, GEN *pind)
    2075             : {
    2076             :   long i, l, k;
    2077      166019 :   GEN w = cgetg_copy(v,&l), ind = cgetg(l, t_VECSMALL);
    2078    17145044 :   for (i = k = 1; i < l; i++)
    2079             :   {
    2080    16979025 :     GEN c = gel(v,i);
    2081    16979025 :     if (typ(c) == t_INT) continue;
    2082      785603 :     gel(w,k) = c; ind[k] = i; k++;
    2083             :   }
    2084      166019 :   setlg(w,k); setlg(ind,k);
    2085      166019 :   *pind = ind; return w;
    2086             : }
    2087             : 
    2088             : static int
    2089      163065 : mat2_isidentity(GEN M)
    2090             : {
    2091      163065 :   GEN A = gel(M,1), B = gel(M,2);
    2092      163065 :   return A[1] == 1 && A[2] == 0 && B[1] == 0 && B[2] == 1;
    2093             : }
    2094             : /* path a mat22/mat22s, return log(f.path)^* . f in sparse form */
    2095             : static GEN
    2096      166019 : M2_logf(GEN Wp, GEN path, GEN f)
    2097             : {
    2098      166019 :   pari_sp av = avma;
    2099             :   GEN ind, L;
    2100             :   long i, l;
    2101      166019 :   if (f)
    2102      160951 :     path = Gl2Q_act_path(f, path);
    2103        5068 :   else if (typ(gel(path,1)) == t_VECSMALL)
    2104        2114 :     path = path2_to_M2(path);
    2105      166019 :   L = M2_log(Wp, path);
    2106      166019 :   L = RgV_sparse(L,&ind); l = lg(L);
    2107      166019 :   for (i = 1; i < l; i++) gel(L,i) = ZSl2_star(gel(L,i));
    2108      166019 :   if (f) ZGC_G_mul_inplace(L, mat2_to_ZM(f));
    2109      166019 :   return gerepilecopy(av, mkvec2(ind,L));
    2110             : }
    2111             : 
    2112             : static hashtable *
    2113        3738 : Gl2act_cache(long dim) { return set_init(dim*10); }
    2114             : 
    2115             : /* f zm/ZM in Gl_2(Q), acts from the left on Delta, which is generated by
    2116             :  * (g_i) as Z[Gamma1]-module, and by (G_i) as Z[Gamma2]-module.
    2117             :  * We have f.G_j = \sum_i \lambda_{i,j} g_i,   \lambda_{i,j} in Z[Gamma1]
    2118             :  * For phi in Hom_Gamma1(D,V), g in D, phi | f is in Hom_Gamma2(D,V) and
    2119             :  *  (phi | f)(G_j) = phi(f.G_j) | f
    2120             :  *                 = phi( \sum_i \lambda_{i,j} g_i ) | f
    2121             :  *                 = \sum_i phi(g_i) | (\lambda_{i,j}^* f)
    2122             :  *                 = \sum_i phi(g_i) | \mu_{i,j}(f)
    2123             :  * More generally
    2124             :  *  (\sum_k (phi |v_k))(G_j) = \sum_i phi(g_i) | \Mu_{i,j}
    2125             :  * with \Mu_{i,j} = \sum_k \mu{i,j}(v_k)
    2126             :  * Return the \Mu_{i,j} matrix as vector of sparse columns of operators on V */
    2127             : static GEN
    2128        3262 : init_dual_act(GEN v, GEN W1, GEN W2, struct m_act *S)
    2129             : {
    2130        3262 :   GEN section = ms_get_section(W2), gen = ms_get_genindex(W2);
    2131             :   /* HACK: the actions we consider in dimension 1 are trivial and in
    2132             :    * characteristic != 2, 3 => torsion generators are 0
    2133             :    * [satisfy e.g. (1+gamma).g = 0 => \phi(g) | 1+gamma  = 0 => \phi(g) = 0 */
    2134        3262 :   long j, lv = lg(v), dim = S->dim == 1? ms_get_nbE1(W2): lg(gen)-1;
    2135        3262 :   GEN T = cgetg(dim+1, t_VEC);
    2136        3262 :   hashtable *H = Gl2act_cache(dim);
    2137       41139 :   for (j = 1; j <= dim; j++)
    2138             :   {
    2139       37877 :     pari_sp av = avma;
    2140       37877 :     GEN w = gel(section, gen[j]); /* path_to_zm( E1/T2/T3 element ) */
    2141       37877 :     GEN t = NULL;
    2142             :     long k;
    2143      200942 :     for (k = 1; k < lv; k++)
    2144             :     {
    2145      163065 :       GEN tk, f = gel(v,k);
    2146      163065 :       if (typ(gel(f,1)) != t_VECSMALL) f = ZM_to_zm(f);
    2147      163065 :       if (mat2_isidentity(f)) f = NULL;
    2148      163065 :       tk = M2_logf(W1, w, f); /* mu_{.,j}(v[k]) as sparse vector */
    2149      163065 :       t = t? ZGCs_add(t, tk): tk;
    2150             :     }
    2151       37877 :     gel(T,j) = gerepilecopy(av, t);
    2152             :   }
    2153       41139 :   for (j = 1; j <= dim; j++)
    2154             :   {
    2155       37877 :     ZGl2QC_preload(S, gel(T,j), H);
    2156       37877 :     ZGl2QC_to_act(S, gel(T,j), H);
    2157             :   }
    2158        3262 :   return T;
    2159             : }
    2160             : 
    2161             : /* modular symbol given by phi[j] = \phi(G_j)
    2162             :  * \sum L[i]*phi[i], L a sparse column of operators */
    2163             : static GEN
    2164      354354 : dense_act_col(GEN col, GEN phi)
    2165             : {
    2166      354354 :   GEN s = NULL, colind = gel(col,1), colval = gel(col,2);
    2167      354354 :   long i, l = lg(colind), lphi = lg(phi);
    2168     5630121 :   for (i = 1; i < l; i++)
    2169             :   {
    2170     5278490 :     long a = colind[i];
    2171             :     GEN t;
    2172     5278490 :     if (a >= lphi) break; /* happens if k=2: torsion generator t omitted */
    2173     5275767 :     t = gel(phi, a); /* phi(G_a) */
    2174     5275767 :     t = RgM_RgC_mul(gel(colval,i), t);
    2175     5275767 :     s = s? RgC_add(s, t): t;
    2176             :   }
    2177      354354 :   return s;
    2178             : }
    2179             : /* modular symbol given by \phi( G[ind[j]] ) = val[j]
    2180             :  * \sum L[i]*phi[i], L a sparse column of operators */
    2181             : static GEN
    2182      780283 : sparse_act_col(GEN col, GEN phi)
    2183             : {
    2184      780283 :   GEN s = NULL, colind = gel(col,1), colval = gel(col,2);
    2185      780283 :   GEN ind = gel(phi,2), val = gel(phi,3);
    2186      780283 :   long a, l = lg(ind);
    2187      780283 :   if (lg(gel(phi,1)) == 1) return RgM_RgC_mul(gel(colval,1), gel(val,1));
    2188     3036978 :   for (a = 1; a < l; a++)
    2189             :   {
    2190     2257094 :     GEN t = gel(val, a); /* phi(G_i) */
    2191     2257094 :     long i = zv_search(colind, ind[a]);
    2192     2257094 :     if (!i) continue;
    2193      543228 :     t = RgM_RgC_mul(gel(colval,i), t);
    2194      543228 :     s = s? RgC_add(s, t): t;
    2195             :   }
    2196      779884 :   return s;
    2197             : }
    2198             : static int
    2199       69545 : phi_sparse(GEN phi) { return typ(gel(phi,1)) == t_VECSMALL; }
    2200             : /* phi in Hom_Gamma1(Delta, V), return the matrix whose colums are the
    2201             :  *   \sum_i phi(g_i) | \mu_{i,j} = (phi|f)(G_j),
    2202             :  * see init_dual_act. */
    2203             : static GEN
    2204       69545 : dual_act(long dimV, GEN act, GEN phi)
    2205             : {
    2206       69545 :   long l = lg(act), j;
    2207       69545 :   GEN v = cgetg(l, t_MAT);
    2208       69545 :   GEN (*ACT)(GEN,GEN) = phi_sparse(phi)? sparse_act_col: dense_act_col;
    2209     1200850 :   for (j = 1; j < l; j++)
    2210             :   {
    2211     1131305 :     pari_sp av = avma;
    2212     1131305 :     GEN s = ACT(gel(act,j), phi);
    2213     1131305 :     gel(v,j) = s? gerepileupto(av,s): zerocol(dimV);
    2214             :   }
    2215       69545 :   return v;
    2216             : }
    2217             : 
    2218             : /* in level N > 1 */
    2219             : static void
    2220       59507 : msk_get_st(GEN W, long *s, long *t)
    2221       59507 : { GEN st = gmael(W,3,3); *s = st[1]; *t = st[2]; }
    2222             : static GEN
    2223       59507 : msk_get_link(GEN W) { return gmael(W,3,4); }
    2224             : static GEN
    2225       59948 : msk_get_inv(GEN W) { return gmael(W,3,5); }
    2226             : /* \phi in Hom(Delta, V), \phi(G_k) = phi[k]. Write \phi as
    2227             :  *   \sum_{i,j} mu_{i,j} phi_{i,j}, mu_{i,j} in Q */
    2228             : static GEN
    2229       59948 : getMorphism_basis(GEN W, GEN phi)
    2230             : {
    2231       59948 :   GEN R, Q, Ls, T0, T1, Ts, link, basis, inv = msk_get_inv(W);
    2232             :   long i, j, r, s, t, dim, lvecT;
    2233             : 
    2234       59948 :   if (ms_get_N(W) == 1) return ZC_apply_dinv(inv, gel(phi,1));
    2235       59507 :   lvecT = lg(phi);
    2236       59507 :   basis = msk_get_basis(W);
    2237       59507 :   dim = lg(basis)-1;
    2238       59507 :   R = zerocol(dim);
    2239       59507 :   msk_get_st(W, &s, &t);
    2240       59507 :   link = msk_get_link(W);
    2241      791350 :   for (r = 2; r < lvecT; r++)
    2242             :   {
    2243             :     GEN Tr, L;
    2244      731843 :     if (r == s) continue;
    2245      672336 :     Tr = gel(phi,r); /* Phi(G_r), r != 1,s */
    2246      672336 :     L = gel(link, r);
    2247      672336 :     Q = ZC_apply_dinv(gel(inv,r), Tr);
    2248             :     /* write Phi(G_r) as sum_{a,b} mu_{a,b} Phi_{a,b}(G_r) */
    2249      672336 :     for (j = 1; j < lg(L); j++) gel(R, L[j]) = gel(Q,j);
    2250             :   }
    2251       59507 :   Ls = gel(link, s);
    2252       59507 :   T1 = gel(phi,1); /* Phi(G_1) */
    2253       59507 :   gel(R, Ls[t]) = gel(T1, 1);
    2254             : 
    2255       59507 :   T0 = NULL;
    2256      791350 :   for (i = 2; i < lg(link); i++)
    2257             :   {
    2258             :     GEN L;
    2259      731843 :     if (i == s) continue;
    2260      672336 :     L = gel(link,i);
    2261     3514154 :     for (j =1 ; j < lg(L); j++)
    2262             :     {
    2263     2841818 :       long n = L[j]; /* phi_{i,j} = basis[n] */
    2264     2841818 :       GEN mu_ij = gel(R, n);
    2265     2841818 :       GEN phi_ij = gel(basis, n), pols = gel(phi_ij,3);
    2266     2841818 :       GEN z = RgC_Rg_mul(gel(pols, 3), mu_ij);
    2267     2841818 :       T0 = T0? RgC_add(T0, z): z; /* += mu_{i,j} Phi_{i,j} (G_s) */
    2268             :     }
    2269             :   }
    2270       59507 :   Ts = gel(phi,s); /* Phi(G_s) */
    2271       59507 :   if (T0) Ts = RgC_sub(Ts, T0);
    2272             :   /* solve \sum_{j!=t} mu_{s,j} Phi_{s,j}(G_s) = Ts */
    2273       59507 :   Q = ZC_apply_dinv(gel(inv,s), Ts);
    2274       59507 :   for (j = 1; j < t; j++) gel(R, Ls[j]) = gel(Q,j);
    2275             :   /* avoid mu_{s,t} */
    2276       59507 :   for (j = t; j < lg(Q); j++) gel(R, Ls[j+1]) = gel(Q,j);
    2277       59507 :   return R;
    2278             : }
    2279             : 
    2280             : /* a = s(g_i) for some modular symbol s; b in Z[G]
    2281             :  * return s(b.g_i) = b^* . s(g_i) */
    2282             : static GEN
    2283      115458 : ZGl2Q_act_s(GEN b, GEN a, long k)
    2284             : {
    2285      115458 :   if (typ(b) == t_INT)
    2286             :   {
    2287       63924 :     if (!signe(b)) return gen_0;
    2288          14 :     switch(typ(a))
    2289             :     {
    2290             :       case t_POL:
    2291          14 :         a = RgX_to_RgC(a, k-1); /*fall through*/
    2292             :       case t_COL:
    2293          14 :         a = RgC_Rg_mul(a,b);
    2294          14 :         break;
    2295           0 :       default: a = scalarcol_shallow(b,k-1);
    2296             :     }
    2297             :   }
    2298             :   else
    2299             :   {
    2300       51534 :     b = RgX_act_ZGl2Q(ZSl2_star(b), k);
    2301       51534 :     switch(typ(a))
    2302             :     {
    2303             :       case t_POL:
    2304          63 :         a = RgX_to_RgC(a, k-1); /*fall through*/
    2305             :       case t_COL:
    2306       39788 :         a = RgM_RgC_mul(b,a);
    2307       39788 :         break;
    2308       11746 :       default: a = RgC_Rg_mul(gel(b,1),a);
    2309             :     }
    2310             :   }
    2311       51548 :   return a;
    2312             : }
    2313             : 
    2314             : static int
    2315          21 : checksymbol(GEN W, GEN s)
    2316             : {
    2317             :   GEN t, annT2, annT31, singlerel;
    2318             :   long i, k, l, nbE1, nbT2, nbT31;
    2319          21 :   k = msk_get_weight(W);
    2320          21 :   W = get_msN(W);
    2321          21 :   nbE1 = ms_get_nbE1(W);
    2322          21 :   singlerel = gel(W,10);
    2323          21 :   l = lg(singlerel);
    2324          21 :   if (k == 2)
    2325             :   {
    2326           0 :     for (i = nbE1+1; i < l; i++)
    2327           0 :       if (!gequal0(gel(s,i))) return 0;
    2328           0 :     return 1;
    2329             :   }
    2330          21 :   annT2 = msN_get_annT2(W); nbT2 = lg(annT2)-1;
    2331          21 :   annT31 = msN_get_annT31(W);nbT31 = lg(annT31)-1;
    2332          21 :   t = NULL;
    2333          84 :   for (i = 1; i < l; i++)
    2334             :   {
    2335          63 :     GEN a = gel(s,i);
    2336          63 :     a = ZGl2Q_act_s(gel(singlerel,i), a, k);
    2337          63 :     t = t? gadd(t, a): a;
    2338             :   }
    2339          21 :   if (!gequal0(t)) return 0;
    2340          14 :   for (i = 1; i <= nbT2; i++)
    2341             :   {
    2342           0 :     GEN a = gel(s,i + nbE1);
    2343           0 :     a = ZGl2Q_act_s(gel(annT2,i), a, k);
    2344           0 :     if (!gequal0(a)) return 0;
    2345             :   }
    2346          28 :   for (i = 1; i <= nbT31; i++)
    2347             :   {
    2348          14 :     GEN a = gel(s,i + nbE1 + nbT2);
    2349          14 :     a = ZGl2Q_act_s(gel(annT31,i), a, k);
    2350          14 :     if (!gequal0(a)) return 0;
    2351             :   }
    2352          14 :   return 1;
    2353             : }
    2354             : GEN
    2355          56 : msissymbol(GEN W, GEN s)
    2356             : {
    2357             :   long k, nbgen;
    2358          56 :   checkms(W);
    2359          56 :   k = msk_get_weight(W);
    2360          56 :   nbgen = ms_get_nbgen(W);
    2361          56 :   switch(typ(s))
    2362             :   {
    2363             :     case t_VEC: /* values s(g_i) */
    2364          21 :       if (lg(s)-1 != nbgen) return gen_0;
    2365          21 :       break;
    2366             :     case t_COL:
    2367          28 :       if (msk_get_sign(W))
    2368             :       {
    2369           0 :         GEN star = gel(msk_get_starproj(W), 1);
    2370           0 :         if (lg(star) == lg(s)) return gen_1;
    2371             :       }
    2372          28 :       if (k == 2) /* on the dual basis of (g_i) */
    2373             :       {
    2374           0 :         if (lg(s)-1 != nbgen) return gen_0;
    2375             :       }
    2376             :       else
    2377             :       {
    2378          28 :         GEN basis = msk_get_basis(W);
    2379          28 :         return (lg(s) == lg(basis))? gen_1: gen_0;
    2380             :       }
    2381           0 :       break;
    2382             :     case t_MAT:
    2383             :     {
    2384           7 :       long i, l = lg(s);
    2385           7 :       GEN v = cgetg(l, t_VEC);
    2386           7 :       for (i = 1; i < l; i++) gel(v,i) = msissymbol(W,gel(s,i))? gen_1: gen_0;
    2387           7 :       return v;
    2388             :     }
    2389           0 :     default: return gen_0;
    2390             :   }
    2391          21 :   return checksymbol(W,s)? gen_1: gen_0;
    2392             : }
    2393             : #if DEBUG
    2394             : /* phi is a sparse symbol from msk_get_basis, return phi(G_j) */
    2395             : static GEN
    2396             : phi_Gj(GEN W, GEN phi, long j)
    2397             : {
    2398             :   GEN ind = gel(phi,2), pols = gel(phi,3);
    2399             :   long i = vecsmall_isin(ind,j);
    2400             :   return i? gel(pols,i): NULL;
    2401             : }
    2402             : /* check that \sum d_i phi_i(G_j)  = T_j for all j */
    2403             : static void
    2404             : checkdec(GEN W, GEN D, GEN T)
    2405             : {
    2406             :   GEN B = msk_get_basis(W);
    2407             :   long i, j;
    2408             :   if (!checksymbol(W,T)) pari_err_BUG("checkdec");
    2409             :   for (j = 1; j < lg(T); j++)
    2410             :   {
    2411             :     GEN S = gen_0;
    2412             :     for (i = 1; i < lg(D); i++)
    2413             :     {
    2414             :       GEN d = gel(D,i), v = phi_Gj(W, gel(B,i), j);
    2415             :       if (!v || gequal0(d)) continue;
    2416             :       S = gadd(S, gmul(d, v));
    2417             :     }
    2418             :     /* S = \sum_i d_i phi_i(G_j) */
    2419             :     if (!gequal(S, gel(T,j)))
    2420             :       pari_warn(warner, "checkdec j = %ld\n\tS = %Ps\n\tT = %Ps", j,S,gel(T,j));
    2421             :   }
    2422             : }
    2423             : #endif
    2424             : 
    2425             : /* map op: W1 = Hom(Delta_0(N1),V) -> W2 = Hom(Delta_0(N2),V), given by
    2426             :  * \sum v[i], v[i] in Gl2(Q) */
    2427             : static GEN
    2428        5481 : getMorphism(GEN W1, GEN W2, GEN v)
    2429             : {
    2430             :   struct m_act S;
    2431             :   GEN B1, M, act;
    2432        5481 :   long a, l, k = msk_get_weight(W1);
    2433        5481 :   if (k == 2) return getMorphism_trivial(W1,W2,v);
    2434        2779 :   S.k = k;
    2435        2779 :   S.dim = k-1;
    2436        2779 :   S.act = &_RgX_act_Gl2Q;
    2437        2779 :   act = init_dual_act(v,W1,W2,&S);
    2438        2779 :   B1 = msk_get_basis(W1);
    2439        2779 :   l = lg(B1); M = cgetg(l, t_MAT);
    2440       61656 :   for (a = 1; a < l; a++)
    2441             :   {
    2442       58877 :     pari_sp av = avma;
    2443       58877 :     GEN phi = dual_act(S.dim, act, gel(B1,a));
    2444       58877 :     GEN D = getMorphism_basis(W2, phi);
    2445             : #if DEBUG
    2446             :     checkdec(W2,D,T);
    2447             : #endif
    2448       58877 :     gel(M,a) = gerepilecopy(av, D);
    2449             :   }
    2450        2779 :   return M;
    2451             : }
    2452             : static GEN
    2453        4347 : msendo(GEN W, GEN v) { return getMorphism(W, W, v); }
    2454             : 
    2455             : static GEN
    2456        2583 : endo_project(GEN W, GEN e, GEN H)
    2457             : {
    2458        2583 :   if (msk_get_sign(W)) e = Qevproj_apply(e, msk_get_starproj(W));
    2459        2583 :   if (H) e = Qevproj_apply(e, Qevproj_init0(H));
    2460        2583 :   return e;
    2461             : }
    2462             : static GEN
    2463        2926 : mshecke_i(GEN W, ulong p)
    2464             : {
    2465        2926 :   GEN v = ms_get_N(W) % p? Tp_matrices(p): Up_matrices(p);
    2466        2926 :   return msendo(W,v);
    2467             : }
    2468             : GEN
    2469        2534 : mshecke(GEN W, long p, GEN H)
    2470             : {
    2471        2534 :   pari_sp av = avma;
    2472             :   GEN T;
    2473        2534 :   checkms(W);
    2474        2534 :   if (p <= 1) pari_err_PRIME("mshecke",stoi(p));
    2475        2534 :   T = mshecke_i(W,p);
    2476        2534 :   T = endo_project(W,T,H);
    2477        2534 :   return gerepilecopy(av, T);
    2478             : }
    2479             : 
    2480             : static GEN
    2481          42 : msatkinlehner_i(GEN W, long Q)
    2482             : {
    2483          42 :   long N = ms_get_N(W);
    2484             :   GEN v;
    2485          42 :   if (Q == 1) return matid(msk_get_dim(W));
    2486          28 :   if (Q == N) return msendo(W, mkvec(mat2(0,1,-N,0)));
    2487          21 :   if (N % Q) pari_err_DOMAIN("msatkinlehner","N % Q","!=",gen_0,stoi(Q));
    2488          14 :   v = WQ_matrix(N, Q);
    2489          14 :   if (!v) pari_err_DOMAIN("msatkinlehner","gcd(Q,N/Q)","!=",gen_1,stoi(Q));
    2490          14 :   return msendo(W,mkvec(v));
    2491             : }
    2492             : GEN
    2493          42 : msatkinlehner(GEN W, long Q, GEN H)
    2494             : {
    2495          42 :   pari_sp av = avma;
    2496             :   GEN w;
    2497             :   long k;
    2498          42 :   checkms(W);
    2499          42 :   k = msk_get_weight(W);
    2500          42 :   if (Q <= 0) pari_err_DOMAIN("msatkinlehner","Q","<=",gen_0,stoi(Q));
    2501          42 :   w = msatkinlehner_i(W,Q);
    2502          35 :   w = endo_project(W,w,H);
    2503          35 :   if (k > 2 && Q != 1) w = RgM_Rg_div(w, powuu(Q,(k-2)>>1));
    2504          35 :   return gerepilecopy(av, w);
    2505             : }
    2506             : 
    2507             : static GEN
    2508        1400 : msstar_i(GEN W) { return msendo(W, mkvec(mat2(-1,0,0,1))); }
    2509             : GEN
    2510          14 : msstar(GEN W, GEN H)
    2511             : {
    2512          14 :   pari_sp av = avma;
    2513             :   GEN s;
    2514          14 :   checkms(W);
    2515          14 :   s = msstar_i(W);
    2516          14 :   s = endo_project(W,s,H);
    2517          14 :   return gerepilecopy(av, s);
    2518             : }
    2519             : 
    2520             : #if 0
    2521             : /* is \Gamma_0(N) cusp1 = \Gamma_0(N) cusp2 ? */
    2522             : static int
    2523             : iscuspeq(ulong N, GEN cusp1, GEN cusp2)
    2524             : {
    2525             :   long p1, q1, p2, q2, s1, s2, d;
    2526             :   p1 = cusp1[1]; p2 = cusp2[1];
    2527             :   q1 = cusp1[2]; q2 = cusp2[2];
    2528             :   d = Fl_mul(smodss(q1,N),smodss(q2,N), N);
    2529             :   d = ugcd(d, N);
    2530             : 
    2531             :   s1 = q1 > 2? Fl_inv(smodss(p1,q1), q1): 1;
    2532             :   s2 = q2 > 2? Fl_inv(smodss(p2,q2), q2): 1;
    2533             :   return Fl_mul(s1,q2,d) == Fl_mul(s2,q1,d);
    2534             : }
    2535             : #endif
    2536             : 
    2537             : /* return E_c(r) */
    2538             : static GEN
    2539        2926 : get_Ec_r(GEN c, long k)
    2540             : {
    2541        2926 :   long p = c[1], q = c[2], u, v;
    2542             :   GEN gr;
    2543        2926 :   (void)cbezout(p, q, &u, &v);
    2544        2926 :   gr = mat2(p, -v, q, u); /* g . (1:0) = (p:q) */
    2545        2926 :   return voo_act_Gl2Q(sl2_inv(gr), k);
    2546             : }
    2547             : /* N > 1; returns the modular symbol attached to the cusp c := p/q via the rule
    2548             :  * E_c(path from a to b in Delta_0) := E_c(b) - E_c(a), where
    2549             :  * E_c(r) := 0 if r != c mod Gamma
    2550             :  *           v_oo | gamma_r^(-1)
    2551             :  * where v_oo is stable by T = [1,1;0,1] (i.e x^(k-2)) and
    2552             :  * gamma_r . (1:0) = r, for some gamma_r in SL_2(Z) * */
    2553             : static GEN
    2554         441 : msfromcusp_trivial(GEN W, GEN c)
    2555             : {
    2556         441 :   GEN section = ms_get_section(W), gen = ms_get_genindex(W);
    2557         441 :   GEN S = ms_get_hashcusps(W);
    2558         441 :   long j, ic = cusp_index(c, S), l = ms_get_nbE1(W)+1;
    2559         441 :   GEN phi = cgetg(l, t_COL);
    2560       90062 :   for (j = 1; j < l; j++)
    2561             :   {
    2562       89621 :     GEN vj, g = gel(section, gen[j]); /* path_to_zm(generator) */
    2563       89621 :     GEN c1 = gel(g,1), c2 = gel(g,2);
    2564       89621 :     long i1 = cusp_index(c1, S);
    2565       89621 :     long i2 = cusp_index(c2, S);
    2566       89621 :     if (i1 == ic)
    2567        3206 :       vj = (i2 == ic)?  gen_0: gen_1;
    2568             :     else
    2569       86415 :       vj = (i2 == ic)? gen_m1: gen_0;
    2570       89621 :     gel(phi, j) = vj;
    2571             :   }
    2572         441 :   return phi;
    2573             : }
    2574             : static GEN
    2575        1512 : msfromcusp_i(GEN W, GEN c)
    2576             : {
    2577             :   GEN section, gen, S, phi;
    2578        1512 :   long j, ic, l, k = msk_get_weight(W);
    2579        1512 :   if (k == 2)
    2580             :   {
    2581         441 :     long N = ms_get_N(W);
    2582         441 :     return N == 1? cgetg(1,t_COL): msfromcusp_trivial(W, c);
    2583             :   }
    2584        1071 :   k = msk_get_weight(W);
    2585        1071 :   section = ms_get_section(W);
    2586        1071 :   gen = ms_get_genindex(W);
    2587        1071 :   S = ms_get_hashcusps(W);
    2588        1071 :   ic = cusp_index(c, S);
    2589        1071 :   l = lg(gen);
    2590        1071 :   phi = cgetg(l, t_COL);
    2591       12579 :   for (j = 1; j < l; j++)
    2592             :   {
    2593       11508 :     GEN vj = NULL, g = gel(section, gen[j]); /* path_to_zm(generator) */
    2594       11508 :     GEN c1 = gel(g,1), c2 = gel(g,2);
    2595       11508 :     long i1 = cusp_index(c1, S);
    2596       11508 :     long i2 = cusp_index(c2, S);
    2597       11508 :     if (i1 == ic) vj = get_Ec_r(c1, k);
    2598       11508 :     if (i2 == ic)
    2599             :     {
    2600        1463 :       GEN s = get_Ec_r(c2, k);
    2601        1463 :       vj = vj? gsub(vj, s): gneg(s);
    2602             :     }
    2603       11508 :     if (!vj) vj = zerocol(k-1);
    2604       11508 :     gel(phi, j) = vj;
    2605             :   }
    2606        1071 :   return getMorphism_basis(W, phi);
    2607             : }
    2608             : GEN
    2609          28 : msfromcusp(GEN W, GEN c)
    2610             : {
    2611          28 :   pari_sp av = avma;
    2612             :   long N;
    2613          28 :   checkms(W);
    2614          28 :   N = ms_get_N(W);
    2615          28 :   switch(typ(c))
    2616             :   {
    2617             :     case t_INFINITY:
    2618           7 :       c = mkvecsmall2(1,0);
    2619           7 :       break;
    2620             :     case t_INT:
    2621          14 :       c = mkvecsmall2(smodis(c,N), 1);
    2622          14 :       break;
    2623             :     case t_FRAC:
    2624           7 :       c = mkvecsmall2(smodis(gel(c,1),N), smodis(gel(c,2),N));
    2625           7 :       break;
    2626             :     default:
    2627           0 :       pari_err_TYPE("msfromcusp",c);
    2628             :   }
    2629          28 :   return gerepilecopy(av, msfromcusp_i(W,c));
    2630             : }
    2631             : 
    2632             : static GEN
    2633         357 : mseisenstein_i(GEN W)
    2634             : {
    2635         357 :   GEN M, S = ms_get_hashcusps(W), cusps = gel(S,3);
    2636         357 :   long i, l = lg(cusps);
    2637         357 :   if (msk_get_weight(W)==2) l--;
    2638         357 :   M = cgetg(l, t_MAT);
    2639         357 :   for (i = 1; i < l; i++) gel(M,i) = msfromcusp_i(W, gel(cusps,i));
    2640         357 :   return Qevproj_star(W, QM_image(M));
    2641             : }
    2642             : GEN
    2643         357 : mseisenstein(GEN W)
    2644             : {
    2645         357 :   pari_sp av = avma;
    2646         357 :   checkms(W);
    2647         357 :   return gerepilecopy(av, Qevproj_init(mseisenstein_i(W)));
    2648             : }
    2649             : 
    2650             : /* upper bound for log_2 |charpoly(T_p|S)|, where S is a cuspidal subspace of
    2651             :  * dimension d, k is the weight */
    2652             : #if 0
    2653             : static long
    2654             : TpS_char_bound(ulong p, long k, long d)
    2655             : { /* |eigenvalue| <= 2 p^(k-1)/2 */
    2656             :   return d * (2 + (log2((double)p)*(k-1))/2);
    2657             : }
    2658             : #endif
    2659             : static long
    2660         336 : TpE_char_bound(ulong p, long k, long d)
    2661             : { /* |eigenvalue| <= 2 p^(k-1) */
    2662         336 :   return d * (2 + log2((double)p)*(k-1));
    2663             : }
    2664             : 
    2665             : GEN
    2666         336 : mscuspidal(GEN W, long flag)
    2667             : {
    2668         336 :   pari_sp av = avma;
    2669             :   GEN S, E, M, T, TE, chE;
    2670             :   long bit;
    2671             :   forprime_t F;
    2672             :   ulong p, N;
    2673             :   pari_timer ti;
    2674             : 
    2675         336 :   E = mseisenstein(W);
    2676         336 :   N = ms_get_N(W);
    2677         336 :   (void)u_forprime_init(&F, 2, ULONG_MAX);
    2678         336 :   while ((p = u_forprime_next(&F)))
    2679         469 :     if (N % p) break;
    2680         336 :   if (DEBUGLEVEL) timer_start(&ti);
    2681         336 :   T = mshecke(W, p, NULL);
    2682         336 :   if (DEBUGLEVEL) timer_printf(&ti,"Tp, p = %ld", p);
    2683         336 :   TE = Qevproj_apply(T, E); /* T_p | E */
    2684         336 :   if (DEBUGLEVEL) timer_printf(&ti,"Qevproj_init(E)");
    2685         336 :   bit = TpE_char_bound(p, msk_get_weight(W), lg(TE)-1);
    2686         336 :   chE = QM_charpoly_ZX_bound(TE, bit);
    2687         336 :   chE = ZX_radical(chE);
    2688         336 :   M = RgX_RgM_eval(chE, T);
    2689         336 :   S = Qevproj_init(QM_image(M));
    2690         336 :   return gerepilecopy(av, flag? mkvec2(S,E): S);
    2691             : }
    2692             : 
    2693             : /** INIT ELLSYM STRUCTURE **/
    2694             : /* V a vector of ZM. If all of them have 0 last row, return NULL.
    2695             :  * Otherwise return [m,i,j], where m = V[i][last,j] contains the value
    2696             :  * of smallest absolute value */
    2697             : static GEN
    2698         931 : RgMV_find_non_zero_last_row(long offset, GEN V)
    2699             : {
    2700         931 :   long i, lasti = 0, lastj = 0, lV = lg(V);
    2701         931 :   GEN m = NULL;
    2702        4074 :   for (i = 1; i < lV; i++)
    2703             :   {
    2704        3143 :     GEN M = gel(V,i);
    2705        3143 :     long j, n, l = lg(M);
    2706        3143 :     if (l == 1) continue;
    2707        2835 :     n = nbrows(M);
    2708       13804 :     for (j = 1; j < l; j++)
    2709             :     {
    2710       10969 :       GEN a = gcoeff(M, n, j);
    2711       10969 :       if (!gequal0(a) && (!m || abscmpii(a, m) < 0))
    2712             :       {
    2713        1582 :         m = a; lasti = i; lastj = j;
    2714        1582 :         if (is_pm1(m)) goto END;
    2715             :       }
    2716             :     }
    2717             :   }
    2718             : END:
    2719         931 :   if (!m) return NULL;
    2720         623 :   return mkvec2(m, mkvecsmall2(lasti+offset, lastj));
    2721             : }
    2722             : /* invert the d_oo := (\gamma_oo - 1) operator, acting on
    2723             :  * [x^(k-2), ..., y^(k-2)] */
    2724             : static GEN
    2725         623 : Delta_inv(GEN doo, long k)
    2726             : {
    2727         623 :   GEN M = RgX_act_ZGl2Q(doo, k);
    2728         623 :   M = RgM_minor(M, k-1, 1); /* 1st column and last row are 0 */
    2729         623 :   return ZM_inv_denom(M);
    2730             : }
    2731             : /* The ZX P = \sum a_i x^i y^{k-2-i} is given by the ZV [a_0, ..., a_k-2]~,
    2732             :  * return Q and d such that P = doo Q + d y^k-2, where d in Z and Q */
    2733             : static GEN
    2734       12831 : doo_decompose(GEN dinv, GEN P, GEN *pd)
    2735             : {
    2736       12831 :   long l = lg(P); *pd = gel(P, l-1);
    2737       12831 :   P = vecslice(P, 1, l-2);
    2738       12831 :   return shallowconcat(gen_0, ZC_apply_dinv(dinv, P));
    2739             : }
    2740             : 
    2741             : static GEN
    2742       12831 : get_phi_ij(long i,long j,long n, long s,long t,GEN P_st,GEN Q_st,GEN d_st,
    2743             :            GEN P_ij, GEN lP_ij, GEN dinv)
    2744             : {
    2745             :   GEN ind, pols;
    2746       12831 :   if (i == s && j == t)
    2747             :   {
    2748         623 :     ind = mkvecsmall(1);
    2749         623 :     pols = mkvec(scalarcol_shallow(gen_1, lg(P_st)-1)); /* x^{k-2} */
    2750             :   }
    2751             :   else
    2752             :   {
    2753       12208 :     GEN d_ij, Q_ij = doo_decompose(dinv, lP_ij, &d_ij);
    2754       12208 :     GEN a = ZC_Z_mul(P_ij, d_st);
    2755       12208 :     GEN b = ZC_Z_mul(P_st, negi(d_ij));
    2756       12208 :     GEN c = RgC_sub(RgC_Rg_mul(Q_ij, d_st), RgC_Rg_mul(Q_st, d_ij));
    2757       12208 :     if (i == s) { /* j != t */
    2758        1645 :       ind = mkvecsmall2(1, s);
    2759        1645 :       pols = mkvec2(c, ZC_add(a, b));
    2760             :     } else {
    2761       10563 :       ind = mkvecsmall3(1, i, s);
    2762       10563 :       pols = mkvec3(c, a, b); /* image of g_1, g_i, g_s */
    2763             :     }
    2764       12208 :     pols = Q_primpart(pols);
    2765             :   }
    2766       12831 :   return mkvec3(mkvecsmall3(i,j,n), ind, pols);
    2767             : }
    2768             : 
    2769             : static GEN
    2770         749 : mskinit_trivial(GEN WN)
    2771             : {
    2772         749 :   long dim = ms_get_nbE1(WN);
    2773         749 :   return mkvec3(WN, gen_0, mkvec2(gen_0,mkvecsmall2(2, dim)));
    2774             : }
    2775             : /* sum of #cols of the matrices contained in V */
    2776             : static long
    2777        1246 : RgMV_dim(GEN V)
    2778             : {
    2779        1246 :   long l = lg(V), d = 0, i;
    2780        1246 :   for (i = 1; i < l; i++) d += lg(gel(V,i)) - 1;
    2781        1246 :   return d;
    2782             : }
    2783             : static GEN
    2784         623 : mskinit_nontrivial(GEN WN, long k)
    2785             : {
    2786         623 :   GEN annT2 = gel(WN,8), annT31 = gel(WN,9), singlerel = gel(WN,10);
    2787             :   GEN link, basis, monomials, Inv;
    2788         623 :   long nbE1 = ms_get_nbE1(WN);
    2789         623 :   GEN dinv = Delta_inv(ZG_neg( ZSl2_star(gel(singlerel,1)) ), k);
    2790         623 :   GEN p1 = cgetg(nbE1+1, t_VEC), remove;
    2791         623 :   GEN p2 = ZGV_tors(annT2, k);
    2792         623 :   GEN p3 = ZGV_tors(annT31, k);
    2793         623 :   GEN gentor = shallowconcat(p2, p3);
    2794             :   GEN P_st, lP_st, Q_st, d_st;
    2795             :   long n, i, dim, s, t, u;
    2796         623 :   gel(p1, 1) = cgetg(1,t_MAT); /* dummy */
    2797        3360 :   for (i = 2; i <= nbE1; i++) /* skip 1st element = (\gamma_oo-1)g_oo */
    2798             :   {
    2799        2737 :     GEN z = gel(singlerel, i);
    2800        2737 :     gel(p1, i) = RgX_act_ZGl2Q(ZSl2_star(z), k);
    2801             :   }
    2802         623 :   remove = RgMV_find_non_zero_last_row(nbE1, gentor);
    2803         623 :   if (!remove) remove = RgMV_find_non_zero_last_row(0, p1);
    2804         623 :   if (!remove) pari_err_BUG("msinit [no y^k-2]");
    2805         623 :   remove = gel(remove,2); /* [s,t] */
    2806         623 :   s = remove[1];
    2807         623 :   t = remove[2];
    2808             :   /* +1 because of = x^(k-2), but -1 because of Manin relation */
    2809         623 :   dim = (k-1)*(nbE1-1) + RgMV_dim(p2) + RgMV_dim(p3);
    2810             :   /* Let (g_1,...,g_d) be the Gamma-generators of Delta, g_1 = g_oo.
    2811             :    * We describe modular symbols by the collection phi(g_1), ..., phi(g_d)
    2812             :    * \in V := Q[x,y]_{k-2}, with right Gamma action.
    2813             :    * For each i = 1, .., d, let V_i \subset V be the Q-vector space of
    2814             :    * allowed values for phi(g_i): with basis (P^{i,j}) given by the monomials
    2815             :    * x^(j-1) y^{k-2-(j-1)}, j = 1 .. k-1
    2816             :    * (g_i in E_1) or the solution of the torsion equations (1 + gamma)P = 0
    2817             :    * (g_i in T2) or (1 + gamma + gamma^2)P = 0 (g_i in T31). All such P
    2818             :    * are chosen in Z[x,y] with Q_content 1.
    2819             :    *
    2820             :    * The Manin relation (singlerel) is of the form \sum_i \lambda_i g_i = 0,
    2821             :    * where \lambda_i = 1 if g_i in T2 or T31, and \lambda_i = (1 - \gamma_i)
    2822             :    * for g_i in E1.
    2823             :    *
    2824             :    * If phi \in Hom_Gamma(Delta, V), it is defined by phi(g_i) := P_i in V
    2825             :    * with \sum_i P_i . \lambda_i^* = 0, where (\sum n_i g_i)^* :=
    2826             :    * \sum n_i \gamma_i^(-1).
    2827             :    *
    2828             :    * We single out gamma_1 / g_1 (g_oo in Pollack-Stevens paper) and
    2829             :    * write P_{i,j} \lambda_i^* =  Q_{i,j} (\gamma_1 - 1)^* + d_{i,j} y^{k-2}
    2830             :    * where d_{i,j} is a scalar and Q_{i,j} in V; we normalize Q_{i,j} to
    2831             :    * that the coefficient of x^{k-2} is 0.
    2832             :    *
    2833             :    * There exist (s,t) such that d_{s,t} != 0.
    2834             :    * A Q-basis of the (dual) space of modular symbols is given by the
    2835             :    * functions phi_{i,j}, 2 <= i <= d, 1 <= j <= k-1, mapping
    2836             :    *  g_1 -> d_{s,t} Q_{i,j} - d_{i,j} Q_{s,t} + [(i,j)=(s,t)] x^{k-2}
    2837             :    * If i != s
    2838             :    *   g_i -> d_{s,t} P_{i,j}
    2839             :    *   g_s -> - d_{i,j} P_{s,t}
    2840             :    * If i = s, j != t
    2841             :    *   g_i -> d_{s,t} P_{i,j} - d_{i,j} P_{s,t}
    2842             :    * And everything else to 0. Again we normalize the phi_{i,j} such that
    2843             :    * their image has content 1. */
    2844         623 :   monomials = matid(k-1); /* represent the monomials x^{k-2}, ... , y^{k-2} */
    2845         623 :   if (s <= nbE1) /* in E1 */
    2846             :   {
    2847         308 :     P_st = gel(monomials, t);
    2848         308 :     lP_st = gmael(p1, s, t); /* P_{s,t} lambda_s^* */
    2849             :   }
    2850             :   else /* in T2, T31 */
    2851             :   {
    2852         315 :     P_st = gmael(gentor, s - nbE1, t);
    2853         315 :     lP_st = P_st;
    2854             :   }
    2855         623 :   Q_st = doo_decompose(dinv, lP_st, &d_st);
    2856         623 :   basis = cgetg(dim+1, t_VEC);
    2857         623 :   link = cgetg(nbE1 + lg(gentor), t_VEC);
    2858         623 :   gel(link,1) = cgetg(1,t_VECSMALL); /* dummy */
    2859         623 :   n = 1;
    2860        3360 :   for (i = 2; i <= nbE1; i++)
    2861             :   {
    2862        2737 :     GEN L = cgetg(k, t_VECSMALL);
    2863             :     long j;
    2864             :     /* link[i][j] = n gives correspondance between phi_{i,j} and basis[n] */
    2865        2737 :     gel(link,i) = L;
    2866       14000 :     for (j = 1; j < k; j++)
    2867             :     {
    2868       11263 :       GEN lP_ij = gmael(p1, i, j); /* P_{i,j} lambda_i^* */
    2869       11263 :       GEN P_ij = gel(monomials,j);
    2870       11263 :       L[j] = n;
    2871       11263 :       gel(basis, n) = get_phi_ij(i,j,n, s,t, P_st, Q_st, d_st, P_ij, lP_ij, dinv);
    2872       11263 :       n++;
    2873             :     }
    2874             :   }
    2875        1127 :   for (u = 1; u < lg(gentor); u++,i++)
    2876             :   {
    2877         504 :     GEN V = gel(gentor,u);
    2878         504 :     long j, lV = lg(V);
    2879         504 :     GEN L = cgetg(lV, t_VECSMALL);
    2880         504 :     gel(link,i) = L;
    2881        2072 :     for (j = 1; j < lV; j++)
    2882             :     {
    2883        1568 :       GEN lP_ij = gel(V, j); /* P_{i,j} lambda_i^* = P_{i,j} */
    2884        1568 :       GEN P_ij = lP_ij;
    2885        1568 :       L[j] = n;
    2886        1568 :       gel(basis, n) = get_phi_ij(i,j,n, s,t, P_st, Q_st, d_st, P_ij, lP_ij, dinv);
    2887        1568 :       n++;
    2888             :     }
    2889             :   }
    2890         623 :   Inv = cgetg(lg(link), t_VEC);
    2891         623 :   gel(Inv,1) = cgetg(1, t_MAT); /* dummy */
    2892        3864 :   for (i = 2; i < lg(link); i++)
    2893             :   {
    2894        3241 :     GEN M, inv, B = gel(link,i);
    2895        3241 :     long j, lB = lg(B);
    2896        3241 :     if (i == s) { B = vecsplice(B, t); lB--; } /* remove phi_st */
    2897        3241 :     M = cgetg(lB, t_MAT);
    2898       15449 :     for (j = 1; j < lB; j++)
    2899             :     {
    2900       12208 :       GEN phi_ij = gel(basis, B[j]), pols = gel(phi_ij,3);
    2901       12208 :       gel(M, j) = gel(pols, 2); /* phi_ij(g_i) */
    2902             :     }
    2903        3241 :     if (i <= nbE1 && i != s) /* maximal rank k-1 */
    2904        2429 :       inv = ZM_inv_denom(M);
    2905             :     else /* i = s (rank k-2) or from torsion: rank k/3 or k/2 */
    2906         812 :       inv = Qevproj_init(M);
    2907        3241 :     gel(Inv,i) = inv;
    2908             :   }
    2909         623 :   return mkvec3(WN, gen_0, mkvec5(basis, mkvecsmall2(k, dim), mkvecsmall2(s,t),
    2910             :                                   link, Inv));
    2911             : }
    2912             : static GEN
    2913        1386 : add_star(GEN W, long sign)
    2914             : {
    2915        1386 :   GEN s = msstar_i(W);
    2916        1386 :   GEN K = sign? QM_ker_r(gsubgs(s, sign)): cgetg(1,t_MAT);
    2917        1386 :   gel(W,2) = mkvec3(stoi(sign), s, Qevproj_init(K));
    2918        1386 :   return W;
    2919             : }
    2920             : /* WN = msinit_N(N) */
    2921             : static GEN
    2922        1386 : mskinit(ulong N, long k, long sign)
    2923             : {
    2924        1386 :   GEN W, WN = msinit_N(N);
    2925        1386 :   if (N == 1)
    2926             :   {
    2927          14 :     GEN basis, M = RgXV_to_RgM(mfperiodpolbasis(k, 0), k-1);
    2928          14 :     GEN T = cgetg(1, t_VECSMALL), ind = mkvecsmall(1);
    2929          14 :     long i, l = lg(M);
    2930          14 :     basis = cgetg(l, t_VEC);
    2931          14 :     for (i = 1; i < l; i++) gel(basis,i) = mkvec3(T, ind, mkvec(gel(M,i)));
    2932          14 :     W = mkvec3(WN, gen_0, mkvec5(basis, mkvecsmall2(k, l-1), mkvecsmall2(0,0),
    2933             :                                  gen_0, Qevproj_init(M)));
    2934             :   }
    2935             :   else
    2936        1372 :     W = k == 2? mskinit_trivial(WN)
    2937        1372 :               : mskinit_nontrivial(WN, k);
    2938        1386 :   return add_star(W, sign);
    2939             : }
    2940             : GEN
    2941         462 : msinit(GEN N, GEN K, long sign)
    2942             : {
    2943         462 :   pari_sp av = avma;
    2944             :   GEN W;
    2945             :   long k;
    2946         462 :   if (typ(N) != t_INT) pari_err_TYPE("msinit", N);
    2947         462 :   if (typ(K) != t_INT) pari_err_TYPE("msinit", K);
    2948         462 :   k = itos(K);
    2949         462 :   if (k < 2) pari_err_DOMAIN("msinit","k", "<", gen_2,K);
    2950         462 :   if (odd(k)) pari_err_IMPL("msinit [odd weight]");
    2951         462 :   if (signe(N) <= 0) pari_err_DOMAIN("msinit","N", "<=", gen_0,N);
    2952         462 :   W = mskinit(itou(N), k, sign);
    2953         462 :   return gerepilecopy(av, W);
    2954             : }
    2955             : 
    2956             : /* W = msinit, xpm integral modular symbol of weight 2, c t_FRAC
    2957             :  * Return image of <oo->c> */
    2958             : static GEN
    2959        1981 : Q_xpm(GEN W, GEN xpm, GEN c)
    2960             : {
    2961        1981 :   pari_sp av = avma;
    2962             :   GEN v;
    2963        1981 :   W = get_msN(W);
    2964        1981 :   v = init_act_trivial(W);
    2965        1981 :   Q_log_trivial(v, W, c); /* oo -> (a:b), c = a/b */
    2966        1981 :   return gerepileuptoint(av, ZV_zc_mul(xpm, v));
    2967             : }
    2968             : 
    2969             : static GEN
    2970       20146 : eval_single(GEN s, long k, GEN B, long v)
    2971             : {
    2972             :   long i, l;
    2973       20146 :   GEN A = cgetg_copy(s,&l);
    2974       20146 :   for (i=1; i<l; i++) gel(A,i) = ZGl2Q_act_s(gel(B,i), gel(s,i), k);
    2975       20146 :   A = RgV_sum(A);
    2976       20146 :   if (is_vec_t(typ(A))) A = RgV_to_RgX(A, v);
    2977       20146 :   return A;
    2978             : }
    2979             : /* Evaluate symbol s on mspathlog B (= sum p_i g_i, p_i in Z[G]). Allow
    2980             :  * s = t_MAT [ collection of symbols, return a vector ]*/
    2981             : static GEN
    2982       15799 : mseval_by_values(GEN W, GEN s, GEN p, long v)
    2983             : {
    2984       15799 :   long i, l, k = msk_get_weight(W);
    2985             :   GEN A;
    2986       15799 :   if (k == 2)
    2987             :   { /* trivial represention: don't bother with Z[G] */
    2988        3339 :     GEN B = mspathlog_trivial(W,p);
    2989        3332 :     if (typ(s) != t_MAT) return RgV_zc_mul(s,B);
    2990        3262 :     l = lg(s); A = cgetg(l, t_VEC);
    2991        3262 :     for (i = 1; i < l; i++) gel(A,i) = RgV_zc_mul(gel(s,i), B);
    2992             :   }
    2993             :   else
    2994             :   {
    2995       12460 :     GEN B = mspathlog(W,p);
    2996       12460 :     if (typ(s) != t_MAT) return eval_single(s, k, B, v);
    2997         812 :     l = lg(s); A = cgetg(l, t_VEC);
    2998         812 :     for (i = 1; i < l; i++) gel(A,i) = eval_single(gel(s,i), k, B, v);
    2999             :   }
    3000        4074 :   return A;
    3001             : }
    3002             : 
    3003             : /* express symbol on the basis phi_{i,j} */
    3004             : static GEN
    3005       20524 : symtophi(GEN W, GEN s)
    3006             : {
    3007       20524 :   GEN e, basis = msk_get_basis(W);
    3008       20524 :   long i, l = lg(basis);
    3009       20524 :   if (lg(s) != l) pari_err_TYPE("mseval",s);
    3010       20524 :   e = zerovec(ms_get_nbgen(W));
    3011      312914 :   for (i=1; i<l; i++)
    3012             :   {
    3013      292390 :     GEN phi, ind, pols, c = gel(s,i);
    3014             :     long j, m;
    3015      292390 :     if (gequal0(c)) continue;
    3016      122528 :     phi = gel(basis,i);
    3017      122528 :     ind = gel(phi,2); m = lg(ind);
    3018      122528 :     pols = gel(phi,3);
    3019      470470 :     for (j=1; j<m; j++)
    3020             :     {
    3021      347942 :       long t = ind[j];
    3022      347942 :       gel(e,t) = gadd(gel(e,t), gmul(c, gel(pols,j)));
    3023             :     }
    3024             :   }
    3025       20524 :   return e;
    3026             : }
    3027             : /* evaluate symbol s on path p */
    3028             : GEN
    3029       16758 : mseval(GEN W, GEN s, GEN p)
    3030             : {
    3031       16758 :   pari_sp av = avma;
    3032       16758 :   long i, k, l, v = 0;
    3033       16758 :   checkms(W);
    3034       16758 :   k = msk_get_weight(W);
    3035       16758 :   switch(typ(s))
    3036             :   {
    3037             :     case t_VEC: /* values s(g_i) */
    3038           7 :       if (lg(s)-1 != ms_get_nbgen(W)) pari_err_TYPE("mseval",s);
    3039           7 :       if (!p) return gcopy(s);
    3040           0 :       v = gvar(s);
    3041           0 :       break;
    3042             :     case t_COL:
    3043       12663 :       if (msk_get_sign(W))
    3044             :       {
    3045         357 :         GEN star = gel(msk_get_starproj(W), 1);
    3046         357 :         if (lg(star) == lg(s)) s = RgM_RgC_mul(star, s);
    3047             :       }
    3048       12663 :       if (k == 2) /* on the dual basis of (g_i) */
    3049             :       {
    3050         637 :         if (lg(s)-1 != ms_get_nbE1(W)) pari_err_TYPE("mseval",s);
    3051         637 :         if (!p) return gtrans(s);
    3052             :       }
    3053             :       else
    3054       12026 :         s = symtophi(W,s);
    3055       12103 :       break;
    3056             :     case t_MAT:
    3057        4088 :       l = lg(s);
    3058        4088 :       if (!p)
    3059             :       {
    3060           7 :         GEN v = cgetg(l, t_VEC);
    3061           7 :         for (i = 1; i < l; i++) gel(v,i) = mseval(W, gel(s,i), NULL);
    3062           7 :         return v;
    3063             :       }
    3064        4081 :       if (l == 1) return cgetg(1, t_VEC);
    3065        4074 :       if (msk_get_sign(W))
    3066             :       {
    3067          84 :         GEN star = gel(msk_get_starproj(W), 1);
    3068          84 :         if (lg(star) == lgcols(s)) s = RgM_mul(star, s);
    3069             :       }
    3070        4074 :       if (k == 2)
    3071        3262 :       { if (nbrows(s) != ms_get_nbE1(W)) pari_err_TYPE("mseval",s); }
    3072             :       else
    3073             :       {
    3074         812 :         GEN t = cgetg(l, t_MAT);
    3075         812 :         for (i = 1; i < l; i++) gel(t,i) = symtophi(W,gel(s,i));
    3076         812 :         s = t;
    3077             :       }
    3078        4074 :       break;
    3079           0 :     default: pari_err_TYPE("mseval",s);
    3080             :   }
    3081       16177 :   if (p)
    3082       15799 :     s = mseval_by_values(W, s, p, v);
    3083             :   else
    3084             :   {
    3085         378 :     l = lg(s);
    3086        3675 :     for (i = 1; i < l; i++)
    3087             :     {
    3088        3297 :       GEN c = gel(s,i);
    3089        3297 :       if (!isintzero(c)) gel(s,i) = RgV_to_RgX(gel(s,i), v);
    3090             :     }
    3091             :   }
    3092       16170 :   return gerepilecopy(av, s);
    3093             : }
    3094             : 
    3095             : static GEN
    3096         938 : allxpm(GEN W, GEN xpm, long f)
    3097             : {
    3098         938 :   GEN v, L = coprimes_zv(f);
    3099         938 :   long a, nonzero = 0;
    3100         938 :   v = const_vec(f, NULL);
    3101        3647 :   for (a = 1; a <= f; a++)
    3102             :   {
    3103             :     GEN c;
    3104        2709 :     if (!L[a]) continue;
    3105        1981 :     c = Q_xpm(W, xpm, sstoQ(a, f));
    3106        1981 :     if (!gequal0(c)) { gel(v,a) = c; nonzero = 1; }
    3107             :   }
    3108         938 :   return nonzero? v: NULL;
    3109             : }
    3110             : /* \sum_{a mod f} chi(a) x(a/f) */
    3111             : static GEN
    3112         441 : seval(GEN G, GEN chi, GEN vx)
    3113             : {
    3114         441 :   GEN vZ, T, s = gen_0, go = zncharorder(G,chi);
    3115         441 :   long i, l = lg(vx), o = itou(go);
    3116         441 :   T = polcyclo(o,0);
    3117         441 :   vZ = mkvec2(RgXQ_powers(RgX_rem(pol_x(0), T), o-1, T), go);
    3118        1813 :   for (i = 1; i < l; i++)
    3119             :   {
    3120        1372 :     GEN x = gel(vx,i);
    3121        1372 :     if (x) s = gadd(s, gmul(x, znchareval(G, chi, utoi(i), vZ)));
    3122             :   }
    3123         441 :   return gequal0(s)? NULL: poleval(s, rootsof1u_cx(o, DEFAULTPREC));
    3124             : }
    3125             : 
    3126             : static long
    3127         189 : nb_components(GEN E) { return signe(ell_get_disc(E)) > 0? 2: 1; }
    3128             : static GEN
    3129         609 : ellperiod(GEN E, long s)
    3130             : {
    3131         609 :   GEN E0, u, v, w = ellR_omega(E,DEFAULTPREC);
    3132         609 :   if (s == 1)
    3133         420 :     w = gel(w,1);
    3134         189 :   else if (nb_components(E) == 2)
    3135          77 :     w = gneg(gel(w,2));
    3136             :   else
    3137         112 :     w = mkcomplex(gen_0, gneg(gmul2n(imag_i(gel(w,2)), 1)));
    3138         609 :   E0 = ellminimalmodel(E,&v);
    3139         609 :   u = gel(v,1); if (!is_pm1(u)) w = gdiv(w,u);
    3140         609 :   obj_free(E0); return w;
    3141             : }
    3142             : 
    3143             : /* Let W = msinit(conductor(E), 2), xpm an integral modular symbol with the same
    3144             :  * eigenvalues as L_E. There exist a unique C such that
    3145             :  *   C*L(E,(D/.),1)_{xpm} = L(E,(D/.),1) / w1(E_D) != 0, for all D fundamental,
    3146             :  * sign(D) = s, and such that E_D has rank 0. Return C * ellperiod(E,s) */
    3147             : static GEN
    3148         441 : ell_get_Cw(GEN LE, GEN W, GEN xpm, long s)
    3149             : {
    3150         441 :   long f, NE = ms_get_N(W);
    3151         441 :   const long bit = 64;
    3152             : 
    3153        1372 :   for (f = 1;; f++)
    3154             :   { /* look for chi with conductor f coprime to N(E) and parity s
    3155             :      * such that L(E,chi,1) != 0 */
    3156        1372 :     pari_sp av = avma;
    3157             :     GEN vchi, vx, G;
    3158             :     long l, i;
    3159        1372 :     if ((f & 3) == 2 || cgcd(NE,f) != 1) continue;
    3160         938 :     vx = allxpm(W, xpm, f); if (!vx) continue;
    3161         441 :     G = znstar0(utoipos(f),1);
    3162         441 :     vchi = chargalois(G,NULL); l = lg(vchi);
    3163         763 :     for (i = 1; i < l; i++)
    3164             :     {
    3165         763 :       pari_sp av2 = avma;
    3166         763 :       GEN tau, z, S, L, chi = gel(vchi,i);
    3167         763 :       long o = zncharisodd(G,chi);
    3168         763 :       if ((s > 0 && o) || (s < 0 && !o)
    3169         553 :           || itos(zncharconductor(G, chi)) != f) continue;
    3170         441 :       S = seval(G, chi, vx);
    3171         441 :       if (!S) { avma = av2; continue; }
    3172             : 
    3173         441 :       L = lfuntwist(LE, mkvec2(G, zncharconj(G,chi)));
    3174         441 :       z = lfun(L, gen_1, bit);
    3175         441 :       tau = znchargauss(G, chi, gen_1, bit);
    3176         882 :       return gdiv(gmul(z, tau), S); /* C * w */
    3177             :     }
    3178           0 :     avma = av;
    3179         931 :   }
    3180             : }
    3181             : static GEN
    3182         357 : ell_get_scale(GEN LE, GEN W, long sign, GEN x)
    3183             : {
    3184         357 :   if (sign)
    3185         273 :     return ell_get_Cw(LE, W, gel(x,1), sign);
    3186             :   else
    3187             :   {
    3188          84 :     GEN Cwp = ell_get_Cw(LE, W, gel(x,1), 1);
    3189          84 :     GEN Cwm = ell_get_Cw(LE, W, gel(x,2),-1);
    3190          84 :     return mkvec2(Cwp, Cwm);
    3191             :   }
    3192             : }
    3193             : static GEN
    3194         441 : msfromell_scale(GEN x, GEN Cw, GEN E, long s)
    3195             : {
    3196         441 :   GEN B = int2n(32);
    3197         441 :   if (s)
    3198             :   {
    3199         273 :     GEN C = gdiv(Cw, ellperiod(E,s));
    3200         273 :     return ZC_Q_mul(gel(x,1), bestappr(C,B));
    3201             :   }
    3202             :   else
    3203             :   {
    3204         168 :     GEN xp = gel(x,1), Cp = gdiv(gel(Cw,1), ellperiod(E, 1)), L;
    3205         168 :     GEN xm = gel(x,2), Cm = gdiv(gel(Cw,2), ellperiod(E,-1));
    3206         168 :     xp = ZC_Q_mul(xp, bestappr(Cp,B));
    3207         168 :     xm = ZC_Q_mul(xm, bestappr(Cm,B));
    3208         168 :     if (signe(ell_get_disc(E)) > 0)
    3209          70 :       L = mkmat2(xp, xm); /* E(R) has 2 connected components */
    3210             :     else
    3211          98 :       L = mkmat2(gsub(xp,xm), gmul2n(xm,1));
    3212         168 :     return mkvec3(xp, xm, L);
    3213             :   }
    3214             : }
    3215             : /* v != 0 */
    3216             : static GEN
    3217         443 : Flc_normalize(GEN v, ulong p)
    3218             : {
    3219         443 :   long i, l = lg(v);
    3220         782 :   for (i = 1; i < l; i++)
    3221         782 :     if (v[i])
    3222             :     {
    3223         443 :       if (v[i] != 1) v = Flv_Fl_div(v, v[i], p);
    3224         443 :       return v;
    3225             :     }
    3226           0 :   return NULL;
    3227             : }
    3228             : /* K \cap Ker M  [F_l vector spaces]. K = NULL means full space */
    3229             : static GEN
    3230         394 : msfromell_ker(GEN K, GEN M, ulong l)
    3231             : {
    3232         394 :   GEN B, Ml = ZM_to_Flm(M, l);
    3233         394 :   if (K) Ml = Flm_mul(Ml, K, l);
    3234         394 :   B = Flm_ker(Ml, l);
    3235         394 :   if (!K) K = B;
    3236          36 :   else if (lg(B) < lg(K))
    3237          36 :     K = Flm_mul(K, B, l);
    3238         394 :   return K;
    3239             : }
    3240             : /* K = \cap_p Ker(T_p - a_p), 2-dimensional. Set *xl to the 1-dimensional
    3241             :  * Fl-basis  such that star . xl = sign . xl if sign != 0 and
    3242             :  * star * xl[1] = xl[1]; star * xl[2] = -xl[2] if sign = 0 */
    3243             : static void
    3244         358 : msfromell_l(GEN *pxl, GEN K, GEN star, long sign, ulong l)
    3245             : {
    3246         358 :   GEN s = ZM_to_Flm(star, l);
    3247         358 :   GEN a = gel(K,1), Sa = Flm_Flc_mul(s,a,l);
    3248         358 :   GEN b = gel(K,2);
    3249         358 :   GEN t = Flv_add(a,Sa,l), xp, xm;
    3250         358 :   if (zv_equal0(t))
    3251             :   {
    3252          14 :     xm = a;
    3253          14 :     xp = Flv_add(b,Flm_Flc_mul(s,b,l), l);
    3254             :   }
    3255             :   else
    3256             :   {
    3257         344 :     xp = t; t = Flv_sub(a, Sa, l);
    3258         344 :     xm = zv_equal0(t)? Flv_sub(b, Flm_Flc_mul(s,b,l), l): t;
    3259             :   }
    3260             :   /* xp = 0 on Im(S - 1), xm = 0 on Im(S + 1) */
    3261         358 :   if (sign > 0)
    3262         252 :     *pxl = mkmat(Flc_normalize(xp, l));
    3263         106 :   else if (sign < 0)
    3264          21 :     *pxl = mkmat(Flc_normalize(xm, l));
    3265             :   else
    3266          85 :     *pxl = mkmat2(Flc_normalize(xp, l), Flc_normalize(xm, l));
    3267         358 : }
    3268             : /* return a primitive symbol */
    3269             : static GEN
    3270         358 : msfromell_ratlift(GEN x, GEN q)
    3271             : {
    3272         358 :   GEN B = sqrti(shifti(q,-1));
    3273         358 :   GEN r = FpM_ratlift(x, q, B, B, NULL);
    3274         358 :   if (r) r = Q_primpart(r);
    3275         358 :   return r;
    3276             : }
    3277             : static int
    3278         358 : msfromell_check(GEN x, GEN vT, GEN star, long sign)
    3279             : {
    3280             :   long i, l;
    3281             :   GEN sx;
    3282         358 :   if (!x) return 0;
    3283         357 :   l = lg(vT);
    3284         749 :   for (i = 1; i < l; i++)
    3285             :   {
    3286         392 :     GEN T = gel(vT,i);
    3287         392 :     if (!gequal0(ZM_mul(T, x))) return 0; /* fail */
    3288             :   }
    3289         357 :   sx = ZM_mul(star,x);
    3290         357 :   if (sign)
    3291         273 :     return ZV_equal(gel(sx,1), sign > 0? gel(x,1): ZC_neg(gel(x,1)));
    3292             :   else
    3293          84 :     return ZV_equal(gel(sx,1),gel(x,1)) && ZV_equal(gel(sx,2),ZC_neg(gel(x,2)));
    3294             : }
    3295             : GEN
    3296         357 : msfromell(GEN E0, long sign)
    3297             : {
    3298         357 :   pari_sp av = avma, av2;
    3299         357 :   GEN T, Cw, E, NE, star, q, vT, xl, xr, W, x = NULL, K = NULL;
    3300             :   long lE, single;
    3301             :   ulong p, l, N;
    3302             :   forprime_t S, Sl;
    3303             : 
    3304         357 :   if (typ(E0) != t_VEC) pari_err_TYPE("msfromell",E0);
    3305         357 :   lE = lg(E0);
    3306         357 :   if (lE == 1) return cgetg(1,t_VEC);
    3307         357 :   single = (typ(gel(E0,1)) != t_VEC);
    3308         357 :   E = single ? E0: gel(E0,1);
    3309         357 :   NE = ellQ_get_N(E);
    3310             :   /* must make it integral for ellap; we have minimal model at hand */
    3311         357 :   T = obj_check(E, Q_MINIMALMODEL); if (lg(T) != 2) E = gel(T,3);
    3312         357 :   N = itou(NE); av2 = avma;
    3313         357 :   W = gerepilecopy(av2, mskinit(N,2,0));
    3314         357 :   star = msk_get_star(W);
    3315         357 :   init_modular_small(&Sl);
    3316             :   /* loop for p <= count_Manin_symbols(N) / 6 would be enough */
    3317         357 :   (void)u_forprime_init(&S, 2, ULONG_MAX);
    3318         357 :   vT = cgetg(1, t_VEC);
    3319         357 :   l = u_forprime_next(&Sl);
    3320         357 :   while( (p = u_forprime_next(&S)) )
    3321             :   {
    3322             :     GEN M;
    3323         476 :     if (N % p == 0) continue;
    3324         392 :     av2 = avma;
    3325         392 :     M = RgM_Rg_sub_shallow(mshecke_i(W, p), ellap(E, utoipos(p)));
    3326         392 :     M = gerepilecopy(av2, M);
    3327         392 :     vT = shallowconcat(vT, mkvec(M)); /* for certification at the end */
    3328         392 :     K = msfromell_ker(K, M, l);
    3329         392 :     if (lg(K) == 3) break;
    3330             :   }
    3331         357 :   if (!p) pari_err_BUG("msfromell: ran out of primes");
    3332             : 
    3333             :   /* mod one l should be enough */
    3334         357 :   msfromell_l(&xl, K, star, sign, l);
    3335         357 :   x = ZM_init_CRT(xl, l);
    3336         357 :   q = utoipos(l);
    3337         357 :   xr = msfromell_ratlift(x, q);
    3338             :   /* paranoia */
    3339         715 :   while (!msfromell_check(xr, vT, star, sign) && (l = u_forprime_next(&Sl)) )
    3340             :   {
    3341           1 :     GEN K = NULL;
    3342           1 :     long i, lvT = lg(vT);
    3343           2 :     for (i = 1; i < lvT; i++)
    3344             :     {
    3345           2 :       K = msfromell_ker(K, gel(vT,i), l);
    3346           2 :       if (lg(K) == 3) break;
    3347             :     }
    3348           1 :     if (i >= lvT) { x = NULL; continue; }
    3349           1 :     msfromell_l(&xl, K, star, sign, l);
    3350           1 :     ZM_incremental_CRT(&x, xl, &q, l);
    3351           1 :     xr = msfromell_ratlift(x, q);
    3352             :   }
    3353             :   /* linear form = 0 on all Im(Tp - ap) and Im(S - sign) if sign != 0 */
    3354         357 :   Cw = ell_get_scale(lfuncreate(E), W, sign, xr);
    3355         357 :   if (single)
    3356         329 :     x = msfromell_scale(xr, Cw, E, sign);
    3357             :   else
    3358             :   {
    3359          28 :     GEN v = cgetg(lE, t_VEC);
    3360             :     long i;
    3361          28 :     for (i=1; i<lE; i++) gel(v,i) = msfromell_scale(xr, Cw, gel(E0,i), sign);
    3362          28 :     x = v;
    3363             :   }
    3364         357 :   return gerepilecopy(av, mkvec2(W, x));
    3365             : }
    3366             : 
    3367             : GEN
    3368          21 : msfromhecke(GEN W, GEN v, GEN H)
    3369             : {
    3370          21 :   pari_sp av = avma;
    3371          21 :   long i, l = lg(v);
    3372          21 :   GEN K = NULL;
    3373          21 :   checkms(W);
    3374          21 :   if (typ(v) != t_VEC) pari_err_TYPE("msfromhecke",v);
    3375          49 :   for (i = 1; i < l; i++)
    3376             :   {
    3377          28 :     GEN K2, T, p, P, c = gel(v,i);
    3378          28 :     if (typ(c) != t_VEC || lg(c) != 3) pari_err_TYPE("msfromhecke",v);
    3379          28 :     p = gel(c,1);
    3380          28 :     if (typ(p) != t_INT) pari_err_TYPE("msfromhecke",v);
    3381          28 :     P = gel(c,2);
    3382          28 :     switch(typ(P))
    3383             :     {
    3384             :       case t_INT:
    3385          21 :         P = deg1pol_shallow(gen_1, negi(P), 0);
    3386          21 :         break;
    3387             :       case t_POL:
    3388           7 :         if (RgX_is_ZX(P)) break;
    3389             :       default:
    3390           0 :         pari_err_TYPE("msfromhecke",v);
    3391             :     };
    3392          28 :     T = mshecke(W, itos(p), H);
    3393          28 :     T = Q_primpart(RgX_RgM_eval(P, T));
    3394          28 :     if (K) T = ZM_mul(T,K);
    3395          28 :     K2 = ZM_ker(T);
    3396          28 :     if (!K) K = K2;
    3397           7 :     else if (lg(K2) < lg(K)) K = ZM_mul(K,K2);
    3398             :   }
    3399          21 :   return gerepilecopy(av, K);
    3400             : }
    3401             : 
    3402             : /* OVERCONVERGENT MODULAR SYMBOLS */
    3403             : 
    3404             : static GEN
    3405        2933 : mspadic_get_Wp(GEN W) { return gel(W,1); }
    3406             : static GEN
    3407         483 : mspadic_get_Tp(GEN W) { return gel(W,2); }
    3408             : static GEN
    3409         483 : mspadic_get_bin(GEN W) { return gel(W,3); }
    3410             : static GEN
    3411         476 : mspadic_get_actUp(GEN W) { return gel(W,4); }
    3412             : static GEN
    3413         476 : mspadic_get_q(GEN W) { return gel(W,5); }
    3414             : static long
    3415        1456 : mspadic_get_p(GEN W) { return gel(W,6)[1]; }
    3416             : static long
    3417        1211 : mspadic_get_n(GEN W) { return gel(W,6)[2]; }
    3418             : static long
    3419         161 : mspadic_get_flag(GEN W) { return gel(W,6)[3]; }
    3420             : static GEN
    3421         483 : mspadic_get_M(GEN W) { return gel(W,7); }
    3422             : static GEN
    3423         483 : mspadic_get_C(GEN W) { return gel(W,8); }
    3424             : static long
    3425         973 : mspadic_get_weight(GEN W) { return msk_get_weight(mspadic_get_Wp(W)); }
    3426             : 
    3427             : void
    3428         980 : checkmspadic(GEN W)
    3429             : {
    3430         980 :   if (typ(W) != t_VEC || lg(W) != 9) pari_err_TYPE("checkmspadic",W);
    3431         980 :   checkms(mspadic_get_Wp(W));
    3432         980 : }
    3433             : 
    3434             : /* f in M_2(Z) \cap GL_2(Q), p \nmid a [ and for the result to mean anything
    3435             :  * p | c, but not needed here]. Return the matrix M in M_D(Z), D = M+k-1
    3436             :  * such that, if v = \int x^i d mu, i < D, is a vector of D moments of mu,
    3437             :  * then M * v is the vector of moments of mu | f  mod p^D */
    3438             : static GEN
    3439      276073 : moments_act_i(struct m_act *S, GEN f)
    3440             : {
    3441      276073 :   long j, k = S->k, D = S->dim;
    3442      276073 :   GEN a = gcoeff(f,1,1), b = gcoeff(f,1,2);
    3443      276073 :   GEN c = gcoeff(f,2,1), d = gcoeff(f,2,2);
    3444      276073 :   GEN u,z,C, q = S->q, mat = cgetg(D+1, t_MAT);
    3445             : 
    3446      276073 :   a = modii(a,q);
    3447      276073 :   z = FpX_powu(deg1pol(c,a,0), k-2, q); /* (a+cx)^(k-2) */
    3448             :   /* u := (b+dx) / (a+cx) mod (q,x^D) = (b/a +d/a*x) / (1 - (-c/a)*x) */
    3449      276073 :   if (!equali1(a))
    3450             :   {
    3451      271229 :     GEN ai = Fp_inv(a,q);
    3452      271229 :     b = Fp_mul(b,ai,q);
    3453      271229 :     c = Fp_mul(c,ai,q);
    3454      271229 :     d = Fp_mul(d,ai,q);
    3455             :   }
    3456      276073 :   u = cgetg(D+2,t_POL); u[1] = evalsigne(1)|evalvarn(0);
    3457      276073 :   gel(u, 2) = gen_1;
    3458      276073 :   gel(u, 3) = C = Fp_neg(c,q);
    3459      276073 :   for (j = 4; j < D+2; j++) gel(u,j) = Fp_mul(gel(u,j-1), C, q);
    3460      276073 :   u = FpX_red(RgXn_mul(deg1pol(d,b,0), u, D), q);
    3461     2369024 :   for (j = 1; j <= D; j++)
    3462             :   {
    3463     2092951 :     gel(mat,j) = RgX_to_RgC(z, D); /* (a+cx)^(k-2) * ((b+dx)/(a+cx))^(j-1) */
    3464     2092951 :     if (j != D) z = FpX_red(RgXn_mul(z, u, D), q);
    3465             :   }
    3466      276073 :   return shallowtrans(mat);
    3467             : }
    3468             : static GEN
    3469      275611 : moments_act(struct m_act *S, GEN f)
    3470      275611 : { pari_sp av = avma; return gerepilecopy(av, moments_act_i(S,f)); }
    3471             : static GEN
    3472         483 : init_moments_act(GEN W, long p, long n, GEN q, GEN v)
    3473             : {
    3474             :   struct m_act S;
    3475         483 :   long k = msk_get_weight(W);
    3476         483 :   S.p = p;
    3477         483 :   S.k = k;
    3478         483 :   S.q = q;
    3479         483 :   S.dim = n+k-1;
    3480         483 :   S.act = &moments_act; return init_dual_act(v,W,W,&S);
    3481             : }
    3482             : 
    3483             : static void
    3484        6762 : clean_tail(GEN phi, long c, GEN q)
    3485             : {
    3486        6762 :   long a, l = lg(phi);
    3487      214438 :   for (a = 1; a < l; a++)
    3488             :   {
    3489      207676 :     GEN P = FpV_red(gel(phi,a), q); /* phi(G_a) = vector of moments */
    3490      207676 :     long j, lP = lg(P);
    3491      207676 :     for (j = c; j < lP; j++) gel(P,j) = gen_0; /* reset garbage to 0 */
    3492      207676 :     gel(phi,a) = P;
    3493             :   }
    3494        6762 : }
    3495             : /* concat z to all phi[i] */
    3496             : static GEN
    3497         630 : concat2(GEN phi, GEN z)
    3498             : {
    3499             :   long i, l;
    3500         630 :   GEN v = cgetg_copy(phi,&l);
    3501         630 :   for (i = 1; i < l; i++) gel(v,i) = shallowconcat(gel(phi,i), z);
    3502         630 :   return v;
    3503             : }
    3504             : static GEN
    3505         630 : red_mod_FilM(GEN phi, ulong p, long k, long flag)
    3506             : {
    3507             :   long a, l;
    3508         630 :   GEN den = gen_1, v = cgetg_copy(phi, &l);
    3509         630 :   if (flag)
    3510             :   {
    3511         343 :     phi = Q_remove_denom(phi, &den);
    3512         343 :     if (!den) { den = gen_1; flag = 0; }
    3513             :   }
    3514       29386 :   for (a = 1; a < l; a++)
    3515             :   {
    3516       28756 :     GEN P = gel(phi,a), q = den;
    3517             :     long j;
    3518      207676 :     for (j = lg(P)-1; j >= k+1; j--)
    3519             :     {
    3520      178920 :       q = muliu(q,p);
    3521      178920 :       gel(P,j) = modii(gel(P,j),q);
    3522             :     }
    3523       28756 :     q = muliu(q,p);
    3524       93380 :     for (     ; j >= 1; j--)
    3525       64624 :       gel(P,j) = modii(gel(P,j),q);
    3526       28756 :     gel(v,a) = P;
    3527             :   }
    3528         630 :   if (flag) v = gdiv(v, den);
    3529         630 :   return v;
    3530             : }
    3531             : 
    3532             : /* denom(C) | p^(2(k-1) - v_p(ap)) */
    3533             : static GEN
    3534         154 : oms_dim2(GEN W, GEN phi, GEN C, GEN ap)
    3535             : {
    3536         154 :   long t, i, k = mspadic_get_weight(W);
    3537         154 :   long p = mspadic_get_p(W), n = mspadic_get_n(W);
    3538         154 :   GEN phi1 = gel(phi,1), phi2 = gel(phi,2);
    3539         154 :   GEN v, q = mspadic_get_q(W);
    3540         154 :   GEN act = mspadic_get_actUp(W);
    3541             : 
    3542         154 :   t = signe(ap)? Z_lval(ap,p) : k-1;
    3543         154 :   phi1 = concat2(phi1, zerovec(n));
    3544         154 :   phi2 = concat2(phi2, zerovec(n));
    3545        2107 :   for (i = 1; i <= n; i++)
    3546             :   {
    3547        1953 :     phi1 = dual_act(k-1, act, phi1);
    3548        1953 :     phi1 = dual_act(k-1, act, phi1);
    3549        1953 :     clean_tail(phi1, k + i*t, q);
    3550             : 
    3551        1953 :     phi2 = dual_act(k-1, act, phi2);
    3552        1953 :     phi2 = dual_act(k-1, act, phi2);
    3553        1953 :     clean_tail(phi2, k + i*t, q);
    3554             :   }
    3555         154 :   C = gpowgs(C,n);
    3556         154 :   v = RgM_RgC_mul(C, mkcol2(phi1,phi2));
    3557         154 :   phi1 = red_mod_FilM(gel(v,1), p, k, 1);
    3558         154 :   phi2 = red_mod_FilM(gel(v,2), p, k, 1);
    3559         154 :   return mkvec2(phi1,phi2);
    3560             : }
    3561             : 
    3562             : /* flag = 0 iff alpha is a p-unit */
    3563             : static GEN
    3564         322 : oms_dim1(GEN W, GEN phi, GEN alpha, long flag)
    3565             : {
    3566         322 :   long i, k = mspadic_get_weight(W);
    3567         322 :   long p = mspadic_get_p(W), n = mspadic_get_n(W);
    3568         322 :   GEN q = mspadic_get_q(W);
    3569         322 :   GEN act = mspadic_get_actUp(W);
    3570         322 :   phi = concat2(phi, zerovec(n));
    3571        3178 :   for (i = 1; i <= n; i++)
    3572             :   {
    3573        2856 :     phi = dual_act(k-1, act, phi);
    3574        2856 :     clean_tail(phi, k + i, q);
    3575             :   }
    3576         322 :   phi = gmul(lift_shallow(gpowgs(alpha,n)), phi);
    3577         322 :   phi = red_mod_FilM(phi, p, k, flag);
    3578         322 :   return mkvec(phi);
    3579             : }
    3580             : 
    3581             : /* lift polynomial P in RgX[X,Y]_{k-2} to a distribution \mu such that
    3582             :  * \int (Y - X z)^(k-2) d\mu(z) = P(X,Y)
    3583             :  * Return the t_VEC of k-1 first moments of \mu: \int z^i d\mu(z), 0<= i < k-1.
    3584             :  *   \sum_j (-1)^(k-2-j) binomial(k-2,j) Y^j \int z^(k-2-j) d\mu(z) = P(1,Y)
    3585             :  * Input is P(1,Y), bin = vecbinomial(k-2): bin[j] = binomial(k-2,j-1) */
    3586             : static GEN
    3587       38626 : RgX_to_moments(GEN P, GEN bin)
    3588             : {
    3589       38626 :   long j, k = lg(bin);
    3590             :   GEN Pd, Bd;
    3591       38626 :   if (typ(P) != t_POL) P = scalarpol(P,0);
    3592       38626 :   P = RgX_to_RgC(P, k-1); /* deg <= k-2 */
    3593       38626 :   settyp(P, t_VEC);
    3594       38626 :   Pd = P+1;  /* Pd[i] = coeff(P,i) */
    3595       38626 :   Bd = bin+1;/* Bd[i] = binomial(k-2,i) */
    3596       46249 :   for (j = 1; j < k-2; j++)
    3597             :   {
    3598        7623 :     GEN c = gel(Pd,j);
    3599        7623 :     if (odd(j)) c = gneg(c);
    3600        7623 :     gel(Pd,j) = gdiv(c, gel(Bd,j));
    3601             :   }
    3602       38626 :   return vecreverse(P);
    3603             : }
    3604             : static GEN
    3605         882 : RgXC_to_moments(GEN v, GEN bin)
    3606             : {
    3607             :   long i, l;
    3608         882 :   GEN w = cgetg_copy(v,&l);
    3609         882 :   for (i=1; i<l; i++) gel(w,i) = RgX_to_moments(gel(v,i),bin);
    3610         882 :   return w;
    3611             : }
    3612             : 
    3613             : /* W an mspadic, assume O[2] is integral, den is the cancelled denominator
    3614             :  * or NULL, L = log(path)^* in sparse form */
    3615             : static GEN
    3616        2954 : omseval_int(struct m_act *S, GEN PHI, GEN L, hashtable *H)
    3617             : {
    3618             :   long i, l;
    3619        2954 :   GEN v = cgetg_copy(PHI, &l);
    3620        2954 :   ZGl2QC_to_act(S, L, H); /* as operators on V */
    3621        6286 :   for (i = 1; i < l; i++)
    3622             :   {
    3623        3332 :     GEN T = dense_act_col(L, gel(PHI,i));
    3624        3332 :     gel(v,i) = T? FpC_red(T,S->q): zerocol(S->dim);
    3625             :   }
    3626        2954 :   return v;
    3627             : }
    3628             : 
    3629             : GEN
    3630          14 : msomseval(GEN W, GEN phi, GEN path)
    3631             : {
    3632             :   struct m_act S;
    3633          14 :   pari_sp av = avma;
    3634             :   GEN v, Wp;
    3635             :   long n, vden;
    3636          14 :   checkmspadic(W);
    3637          14 :   if (typ(phi) != t_COL || lg(phi) != 4)  pari_err_TYPE("msomseval",phi);
    3638          14 :   vden = itos(gel(phi,2));
    3639          14 :   phi = gel(phi,1);
    3640          14 :   n = mspadic_get_n(W);
    3641          14 :   Wp= mspadic_get_Wp(W);
    3642          14 :   S.k = mspadic_get_weight(W);
    3643          14 :   S.p = mspadic_get_p(W);
    3644          14 :   S.q = powuu(S.p, n+vden);
    3645          14 :   S.dim = n + S.k - 1;
    3646          14 :   S.act = &moments_act;
    3647          14 :   path = path_to_M2(path);
    3648          14 :   v = omseval_int(&S, phi, M2_logf(Wp,path,NULL), NULL);
    3649          14 :   return gerepilecopy(av, v);
    3650             : }
    3651             : /* W = msinit(N,k,...); if flag < 0 or flag >= k-1, allow all symbols;
    3652             :  * else commit to v_p(a_p) <= flag (ordinary if flag = 0)*/
    3653             : GEN
    3654         490 : mspadicinit(GEN W, long p, long n, long flag)
    3655             : {
    3656         490 :   pari_sp av = avma;
    3657             :   long a, N, k;
    3658             :   GEN P, C, M, bin, Wp, Tp, q, pn, actUp, teich, pas;
    3659             : 
    3660         490 :   checkms(W);
    3661         490 :   N = ms_get_N(W);
    3662         490 :   k = msk_get_weight(W);
    3663         490 :   if (flag < 0) flag = 1; /* worst case */
    3664         357 :   else if (flag >= k) flag = k-1;
    3665             : 
    3666         490 :   bin = vecbinomial(k-2);
    3667         490 :   Tp = mshecke(W, p, NULL);
    3668         490 :   if (N % p == 0)
    3669             :   {
    3670          91 :     if ((N/p) % p == 0) pari_err_IMPL("mspadicinit when p^2 | N");
    3671             :     /* a_p != 0 */
    3672          84 :     Wp = W;
    3673          84 :     M = gen_0;
    3674          84 :     flag = (k-2) / 2; /* exact valuation */
    3675             :     /* will multiply by matrix with denominator p^(k-2)/2 in mspadicint.
    3676             :      * Except if p = 2 (multiply by alpha^2) */
    3677          84 :     if (p == 2) n += k-2; else n += (k-2)/2;
    3678          84 :     pn = powuu(p,n);
    3679             :     /* For accuracy mod p^n, oms_dim1 require p^(k/2*n) */
    3680          84 :     q = powiu(pn, k/2);
    3681             :   }
    3682             :   else
    3683             :   { /* p-stabilize */
    3684         399 :     long s = msk_get_sign(W);
    3685             :     GEN M1, M2;
    3686             : 
    3687         399 :     Wp = mskinit(N*p, k, s);
    3688         399 :     M1 = getMorphism(W, Wp, mkvec(mat2(1,0,0,1)));
    3689         399 :     M2 = getMorphism(W, Wp, mkvec(mat2(p,0,0,1)));
    3690         399 :     if (s)
    3691             :     {
    3692         147 :       GEN SW = msk_get_starproj(W), SWp = msk_get_starproj(Wp);
    3693         147 :       M1 = Qevproj_apply2(M1, SW, SWp);
    3694         147 :       M2 = Qevproj_apply2(M2, SW, SWp);
    3695             :     }
    3696         399 :     M = mkvec2(M1,M2);
    3697         399 :     n += Z_lval(Q_denom(M), p); /*den. introduced by p-stabilization*/
    3698             :     /* in supersingular case: will multiply by matrix with denominator p^k
    3699             :      * in mspadicint. Except if p = 2 (multiply by alpha^2) */
    3700         399 :     if (flag) { if (p == 2) n += 2*k-2; else n += k; }
    3701         399 :     pn = powuu(p,n);
    3702             :     /* For accuracy mod p^n, supersingular require p^((2k-1-v_p(a_p))*n) */
    3703         399 :     if (flag) /* k-1 also takes care of a_p = 0. Worst case v_p(a_p) = flag */
    3704         231 :       q = powiu(pn, 2*k-1 - flag);
    3705             :     else
    3706         168 :       q = pn;
    3707             :   }
    3708         483 :   actUp = init_moments_act(Wp, p, n, q, Up_matrices(p));
    3709             : 
    3710         483 :   if (p == 2) C = gen_0;
    3711             :   else
    3712             :   {
    3713         427 :     pas = matpascal(n);
    3714         427 :     teich = teichmullerinit(p, n+1);
    3715         427 :     P = gpowers(utoipos(p), n);
    3716         427 :     C = cgetg(p, t_VEC);
    3717        2317 :     for (a = 1; a < p; a++)
    3718             :     { /* powb[j+1] = ((a - w(a)) / p)^j mod p^n */
    3719        1890 :       GEN powb = Fp_powers(diviuexact(subui(a, gel(teich,a)), p), n, pn);
    3720        1890 :       GEN Ca = cgetg(n+2, t_VEC);
    3721        1890 :       long j, r, ai = Fl_inv(a, p); /* a^(-1) */
    3722        1890 :       gel(C,a) = Ca;
    3723       22134 :       for (j = 0; j <= n; j++)
    3724             :       {
    3725       20244 :         GEN Caj = cgetg(j+2, t_VEC);
    3726       20244 :         GEN atij = gel(teich, Fl_powu(ai,j,p));/* w(a)^(-j) = w(a^(-j) mod p) */
    3727       20244 :         gel(Ca,j+1) = Caj;
    3728      158200 :         for (r = 0; r <= j; r++)
    3729             :         {
    3730      137956 :           GEN c = Fp_mul(gcoeff(pas,j+1,r+1), gel(powb, j-r+1), pn);
    3731      137956 :           c = Fp_mul(c,atij,pn); /* binomial(j,r)*b^(j-r)*w(a)^(-j) mod p^n */
    3732      137956 :           gel(Caj,r+1) = mulii(c, gel(P,j+1)); /* p^j * c mod p^(n+j) */
    3733             :         }
    3734             :       }
    3735             :     }
    3736             :   }
    3737         483 :   return gerepilecopy(av, mkvecn(8, Wp,Tp, bin, actUp, q,
    3738             :                                  mkvecsmall3(p,n,flag), M, C));
    3739             : }
    3740             : 
    3741             : #if 0
    3742             : /* assume phi an ordinary OMS */
    3743             : static GEN
    3744             : omsactgl2(GEN W, GEN phi, GEN M)
    3745             : {
    3746             :   GEN q, Wp, act;
    3747             :   long p, k, n;
    3748             :   checkmspadic(W);
    3749             :   Wp = mspadic_get_Wp(W);
    3750             :   p = mspadic_get_p(W);
    3751             :   k = mspadic_get_weight(W);
    3752             :   n = mspadic_get_n(W);
    3753             :   q = mspadic_get_q(W);
    3754             :   act = init_moments_act(Wp, p, n, q, M);
    3755             :   phi = gel(phi,1);
    3756             :   return dual_act(k-1, act, gel(phi,1));
    3757             : }
    3758             : #endif
    3759             : 
    3760             : static GEN
    3761         483 : eigenvalue(GEN T, GEN x)
    3762             : {
    3763         483 :   long i, l = lg(x);
    3764         637 :   for (i = 1; i < l; i++)
    3765         637 :     if (!isintzero(gel(x,i))) break;
    3766         483 :   if (i == l) pari_err_DOMAIN("mstooms", "phi", "=", gen_0, x);
    3767         483 :   return gdiv(RgMrow_RgC_mul(T,x,i), gel(x,i));
    3768             : }
    3769             : 
    3770             : /* p coprime to ap, return unit root of x^2 - ap*x + p^(k-1), accuracy p^n */
    3771             : GEN
    3772         266 : mspadic_unit_eigenvalue(GEN ap, long k, GEN p, long n)
    3773             : {
    3774         266 :   GEN sqrtD, D = subii(sqri(ap), shifti(powiu(p,k-1),2));
    3775         266 :   if (absequaliu(p,2))
    3776             :   {
    3777           7 :     n++; sqrtD = Zp_sqrt(D, p, n);
    3778           7 :     if (mod4(sqrtD) != mod4(ap)) sqrtD = negi(sqrtD);
    3779             :   }
    3780             :   else
    3781         259 :     sqrtD = Zp_sqrtlift(D, ap, p, n);
    3782             :   /* sqrtD = ap (mod p) */
    3783         266 :   return gmul2n(gadd(ap, cvtop(sqrtD,p,n)), -1);
    3784             : }
    3785             : 
    3786             : /* W = msinit(N,k,...); phi = T_p/U_p - eigensymbol */
    3787             : GEN
    3788         483 : mstooms(GEN W, GEN phi)
    3789             : {
    3790         483 :   pari_sp av = avma;
    3791             :   GEN Wp, bin, Tp, c, alpha, ap, phi0, M;
    3792             :   long k, p, vden;
    3793             : 
    3794         483 :   checkmspadic(W);
    3795         483 :   if (typ(phi) != t_COL)
    3796             :   {
    3797         161 :     if (!is_Qevproj(phi)) pari_err_TYPE("mstooms",phi);
    3798         161 :     phi = gel(phi,1);
    3799         161 :     if (lg(phi) != 2) pari_err_TYPE("mstooms [dim_Q (eigenspace) > 1]",phi);
    3800         161 :     phi = gel(phi,1);
    3801             :   }
    3802             : 
    3803         483 :   Wp = mspadic_get_Wp(W);
    3804         483 :   Tp = mspadic_get_Tp(W);
    3805         483 :   bin = mspadic_get_bin(W);
    3806         483 :   k = msk_get_weight(Wp);
    3807         483 :   p = mspadic_get_p(W);
    3808         483 :   M = mspadic_get_M(W);
    3809             : 
    3810         483 :   phi = Q_remove_denom(phi, &c);
    3811         483 :   ap = eigenvalue(Tp, phi);
    3812         483 :   vden = c? Z_lvalrem(c, p, &c): 0;
    3813             : 
    3814         483 :   if (typ(M) == t_INT)
    3815             :   { /* p | N */
    3816             :     GEN c1;
    3817          84 :     alpha = ap;
    3818          84 :     alpha = ginv(alpha);
    3819          84 :     phi0 = mseval(Wp, phi, NULL);
    3820          84 :     phi0 = RgXC_to_moments(phi0, bin);
    3821          84 :     phi0 = Q_remove_denom(phi0, &c1);
    3822          84 :     if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3823          84 :     if (umodiu(ap,p)) /* p \nmid a_p */
    3824          49 :       phi = oms_dim1(W, phi0, alpha, 0);
    3825             :     else
    3826             :     {
    3827          35 :       phi = oms_dim1(W, phi0, alpha, 1);
    3828          35 :       phi = Q_remove_denom(phi, &c1);
    3829          35 :       if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3830             :     }
    3831             :   }
    3832             :   else
    3833             :   { /* p-stabilize */
    3834             :     GEN M1, M2, phi1, phi2, c1;
    3835         399 :     if (typ(M) != t_VEC || lg(M) != 3) pari_err_TYPE("mstooms",W);
    3836         399 :     M1 = gel(M,1);
    3837         399 :     M2 = gel(M,2);
    3838             : 
    3839         399 :     phi1 = RgM_RgC_mul(M1, phi);
    3840         399 :     phi2 = RgM_RgC_mul(M2, phi);
    3841         399 :     phi1 = mseval(Wp, phi1, NULL);
    3842         399 :     phi2 = mseval(Wp, phi2, NULL);
    3843             : 
    3844         399 :     phi1 = RgXC_to_moments(phi1, bin);
    3845         399 :     phi2 = RgXC_to_moments(phi2, bin);
    3846         399 :     phi = Q_remove_denom(mkvec2(phi1,phi2), &c1);
    3847         399 :     phi1 = gel(phi,1);
    3848         399 :     phi2 = gel(phi,2);
    3849         399 :     if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3850             :     /* all polynomials multiplied by c p^vden */
    3851         399 :     if (umodiu(ap, p))
    3852             :     {
    3853         238 :       alpha = mspadic_unit_eigenvalue(ap, k, utoipos(p), mspadic_get_n(W));
    3854         238 :       alpha = ginv(alpha);
    3855         238 :       phi0 = gsub(phi1, gmul(lift_shallow(alpha),phi2));
    3856         238 :       phi = oms_dim1(W, phi0, alpha, 0);
    3857             :     }
    3858             :     else
    3859             :     { /* p | ap, alpha = [a_p, -1; p^(k-1), 0] */
    3860         161 :       long flag = mspadic_get_flag(W);
    3861         161 :       if (!flag || (signe(ap) && Z_lval(ap,p) < flag))
    3862           7 :         pari_err_TYPE("mstooms [v_p(ap) > mspadicinit flag]", phi);
    3863         154 :       alpha = mkmat22(ap,gen_m1, powuu(p, k-1),gen_0);
    3864         154 :       alpha = ginv(alpha);
    3865         154 :       phi = oms_dim2(W, mkvec2(phi1,phi2), gsqr(alpha), ap);
    3866         154 :       phi = Q_remove_denom(phi, &c1);
    3867         154 :       if (c1) { vden += Z_lvalrem(c1, p, &c1); c = mul_denom(c,c1); }
    3868             :     }
    3869             :   }
    3870         476 :   if (vden) c = mul_denom(c, powuu(p,vden));
    3871         476 :   if (p == 2) alpha = gsqr(alpha);
    3872         476 :   if (c) alpha = gdiv(alpha,c);
    3873         476 :   if (typ(alpha) == t_MAT)
    3874             :   { /* express in basis (omega,-p phi(omega)) */
    3875         154 :     gcoeff(alpha,2,1) = gdivgs(gcoeff(alpha,2,1), -p);
    3876         154 :     gcoeff(alpha,2,2) = gdivgs(gcoeff(alpha,2,2), -p);
    3877             :     /* at the end of mspadicint we shall multiply result by [1,0;0,-1/p]*alpha
    3878             :      * vden + k is the denominator of this matrix */
    3879             :   }
    3880             :   /* phi is integral-valued */
    3881         476 :   return gerepilecopy(av, mkcol3(phi, stoi(vden), alpha));
    3882             : }
    3883             : 
    3884             : /* HACK: the v[j] have different lengths */
    3885             : static GEN
    3886        2156 : FpVV_dotproduct(GEN v, GEN w, GEN p)
    3887             : {
    3888        2156 :   long j, l = lg(v);
    3889        2156 :   GEN T = cgetg(l, t_VEC);
    3890        2156 :   for (j = 1; j < l; j++) gel(T,j) = FpV_dotproduct(gel(v,j),w,p);
    3891        2156 :   return T;
    3892             : }
    3893             : 
    3894             : /* \int (-4z)^j given \int z^j */
    3895             : static GEN
    3896          98 : twistmoment_m4(GEN v)
    3897             : {
    3898             :   long i, l;
    3899          98 :   GEN w = cgetg_copy(v, &l);
    3900        2009 :   for (i = 1; i < l; i++)
    3901             :   {
    3902        1911 :     GEN c = gel(v,i);
    3903        1911 :     if (i > 1) c = gmul2n(c, (i-1)<<1);
    3904        1911 :     gel(w,i) = odd(i)? c: gneg(c);
    3905             :   }
    3906          98 :   return w;
    3907             : }
    3908             : /* \int (4z)^j given \int z^j */
    3909             : static GEN
    3910          98 : twistmoment_4(GEN v)
    3911             : {
    3912             :   long i, l;
    3913          98 :   GEN w = cgetg_copy(v, &l);
    3914        2009 :   for (i = 1; i < l; i++)
    3915             :   {
    3916        1911 :     GEN c = gel(v,i);
    3917        1911 :     if (i > 1) c = gmul2n(c, (i-1)<<1);
    3918        1911 :     gel(w,i) = c;
    3919             :   }
    3920          98 :   return w;
    3921             : }
    3922             : /* W an mspadic, phi eigensymbol, p \nmid D. Return C(x) mod FilM */
    3923             : GEN
    3924         483 : mspadicmoments(GEN W, GEN PHI, long D)
    3925             : {
    3926         483 :   pari_sp av = avma;
    3927         483 :   long na, ia, b, lphi, aD = labs(D), pp, p, k, n, vden;
    3928             :   GEN Wp, Dact, vL, v, C, pn, phi;
    3929             :   struct m_act S;
    3930             :   hashtable *H;
    3931             : 
    3932         483 :   checkmspadic(W);
    3933         483 :   Wp = mspadic_get_Wp(W);
    3934         483 :   p = mspadic_get_p(W);
    3935         483 :   k = mspadic_get_weight(W);
    3936         483 :   n = mspadic_get_n(W);
    3937         483 :   C = mspadic_get_C(W);
    3938         483 :   if (typ(PHI) != t_COL || lg(PHI) != 4 || typ(gel(PHI,1)) != t_VEC)
    3939         476 :     PHI = mstooms(W, PHI);
    3940         476 :   vden = itos( gel(PHI,2) );
    3941         476 :   phi = gel(PHI,1); lphi = lg(phi);
    3942         476 :   if (p == 2) { na = 2; pp = 4; }
    3943         420 :   else        { na = p-1; pp = p; }
    3944         476 :   pn = powuu(p, n + vden);
    3945             : 
    3946         476 :   S.p = p;
    3947         476 :   S.k = k;
    3948         476 :   S.q = pn;
    3949         476 :   S.dim = n+k-1;
    3950         476 :   S.act = &moments_act;
    3951         476 :   H = Gl2act_cache(ms_get_nbgen(Wp));
    3952         476 :   if (D == 1) Dact = NULL;
    3953             :   else
    3954             :   {
    3955          63 :     GEN gaD = utoi(aD), Dk = Fp_pows(stoi(D), 2-k, pn);
    3956          63 :     if (!sisfundamental(D)) pari_err_TYPE("mspadicmoments", stoi(D));
    3957          63 :     if (D % p == 0) pari_err_DOMAIN("mspadicmoments","p","|", stoi(D), utoi(p));
    3958          63 :     Dact = cgetg(aD, t_VEC);
    3959         532 :     for (b = 1; b < aD; b++)
    3960             :     {
    3961         469 :       GEN z = NULL;
    3962         469 :       long s = kross(D, b);
    3963         469 :       if (s)
    3964             :       {
    3965         462 :         pari_sp av2 = avma;
    3966             :         GEN d;
    3967         462 :         z = moments_act_i(&S, mkmat22(gaD,utoipos(b), gen_0,gaD));
    3968         462 :         d = s > 0? Dk: Fp_neg(Dk, pn);
    3969         924 :         z = equali1(d)? gerepilecopy(av2, z)
    3970         462 :                       : gerepileupto(av2, FpM_Fp_mul(z, d, pn));
    3971             :       }
    3972         469 :       gel(Dact,b) = z;
    3973             :     }
    3974             :   }
    3975         476 :   vL = cgetg(na+1,t_VEC);
    3976             :   /* first pass to precompute log(paths), preload matrices and allow GC later */
    3977        2464 :   for (ia = 1; ia <= na; ia++)
    3978             :   {
    3979             :     GEN path, La;
    3980        1988 :     long a = (p == 2 && ia == 2)? -1: ia;
    3981        1988 :     if (Dact)
    3982             :     { /* twist by D */
    3983         224 :       La = cgetg(aD, t_VEC);
    3984        1442 :       for (b = 1; b < aD; b++)
    3985             :       {
    3986        1218 :         GEN Actb = gel(Dact,b);
    3987        1218 :         if (!Actb) continue;
    3988             :         /* oo -> a/pp + b/|D|*/
    3989        1176 :         path = mkmat22(gen_1, addii(mulss(a, aD), muluu(pp, b)),
    3990             :                        gen_0, muluu(pp, aD));
    3991        1176 :         gel(La,b) = M2_logf(Wp,path,NULL);
    3992        1176 :         ZGl2QC_preload(&S, gel(La,b), H);
    3993             :       }
    3994             :     }
    3995             :     else
    3996             :     {
    3997        1764 :       path = mkmat22(gen_1,stoi(a), gen_0, utoipos(pp));
    3998        1764 :       La = M2_logf(Wp,path,NULL);
    3999        1764 :       ZGl2QC_preload(&S, La, H);
    4000             :     }
    4001        1988 :     gel(vL,ia) = La;
    4002             :   }
    4003         476 :   v = cgetg(na+1,t_VEC);
    4004             :   /* second pass, with GC */
    4005        2464 :   for (ia = 1; ia <= na; ia++)
    4006             :   {
    4007        1988 :     pari_sp av2 = avma;
    4008        1988 :     GEN vca, Ca = gel(C,ia), La = gel(vL,ia), va = cgetg(lphi, t_VEC);
    4009             :     long i;
    4010        1988 :     if (!Dact) vca = omseval_int(&S, phi, La, H);
    4011             :     else
    4012             :     { /* twist by D */
    4013         224 :       vca = cgetg(lphi,t_VEC);
    4014        1442 :       for (b = 1; b < aD; b++)
    4015             :       {
    4016        1218 :         GEN T, Actb = gel(Dact,b);
    4017        1218 :         if (!Actb) continue;
    4018        1176 :         T = omseval_int(&S, phi, gel(La,b), H);
    4019        2352 :         for (i = 1; i < lphi; i++)
    4020             :         {
    4021        1176 :           GEN z = FpM_FpC_mul(Actb, gel(T,i), pn);
    4022        1176 :           gel(vca,i) = b==1? z: ZC_add(gel(vca,i), z);
    4023             :         }
    4024             :       }
    4025             :     }
    4026        1988 :     if (p != 2)
    4027        1876 :     { for (i=1; i<lphi; i++) gel(va,i) = FpVV_dotproduct(Ca,gel(vca,i),pn); }
    4028         112 :     else if (ia == 1) /* \tilde{a} = 1 */
    4029          56 :     { for (i=1; i<lphi; i++) gel(va,i) = twistmoment_4(gel(vca,i)); }
    4030             :     else /* \tilde{a} = -1 */
    4031          56 :     { for (i=1; i<lphi; i++) gel(va,i) = twistmoment_m4(gel(vca,i)); }
    4032        1988 :     gel(v,ia) = gerepilecopy(av2, va);
    4033             :   }
    4034         476 :   return gerepilecopy(av, mkvec3(v, gel(PHI,3), mkvecsmall4(p,n+vden,n,D)));
    4035             : }
    4036             : static void
    4037        1918 : checkoms(GEN v)
    4038             : {
    4039        1918 :   if (typ(v) != t_VEC || lg(v) != 4 || typ(gel(v,1)) != t_VEC
    4040        1918 :       || typ(gel(v,3))!=t_VECSMALL)
    4041           0 :     pari_err_TYPE("checkoms [apply mspadicmoments]", v);
    4042        1918 : }
    4043             : static long
    4044        4284 : oms_get_p(GEN oms) { return gel(oms,3)[1]; }
    4045             : static long
    4046        4186 : oms_get_n(GEN oms) { return gel(oms,3)[2]; }
    4047             : static long
    4048        2464 : oms_get_n0(GEN oms) { return gel(oms,3)[3]; }
    4049             : static long
    4050        1918 : oms_get_D(GEN oms) { return gel(oms,3)[4]; }
    4051             : static int
    4052          98 : oms_is_supersingular(GEN oms) { GEN v = gel(oms,1); return lg(gel(v,1)) == 3; }
    4053             : 
    4054             : /* sum(j = 1, n, (-1)^(j+1)/j * x^j) */
    4055             : static GEN
    4056         784 : log1x(long n)
    4057             : {
    4058         784 :   long i, l = n+3;
    4059         784 :   GEN v = cgetg(l, t_POL);
    4060         784 :   v[1] = evalvarn(0)|evalsigne(1); gel(v,2) = gen_0;
    4061         784 :   for (i = 3; i < l; i++) gel(v,i) = ginv(stoi(odd(i)? i-2: 2-i));
    4062         784 :   return v;
    4063             : }
    4064             : 
    4065             : /* S = (1+x)^zk log(1+x)^logj (mod x^(n+1)) */
    4066             : static GEN
    4067        1820 : xlog1x(long n, long zk, long logj, long *pteich)
    4068             : {
    4069        1820 :   GEN S = logj? RgXn_powu_i(log1x(n), logj, n+1): NULL;
    4070        1820 :   if (zk)
    4071             :   {
    4072        1183 :     GEN L = deg1pol_shallow(gen_1, gen_1, 0); /* x+1 */
    4073        1183 :     *pteich += zk;
    4074        1183 :     if (zk < 0) { L = RgXn_inv(L,n+1); zk = -zk; }
    4075        1183 :     if (zk != 1) L = RgXn_powu_i(L, zk, n+1);
    4076        1183 :     S = S? RgXn_mul(S, L, n+1): L;
    4077             :   }
    4078        1820 :   return S;
    4079             : }
    4080             : 
    4081             : /* oms from mspadicmoments; integrate teichmuller^i * S(x) [S = NULL: 1]*/
    4082             : static GEN
    4083        2366 : mspadicint(GEN oms, long teichi, GEN S)
    4084             : {
    4085        2366 :   pari_sp av = avma;
    4086        2366 :   long p = oms_get_p(oms), n = oms_get_n(oms), n0 = oms_get_n0(oms);
    4087        2366 :   GEN vT = gel(oms,1), alpha = gel(oms,2), gp = utoipos(p);
    4088        2366 :   long loss = S? Z_lval(Q_denom(S), p): 0;
    4089        2366 :   long nfinal = minss(n-loss, n0);
    4090        2366 :   long i, la, l = lg(gel(vT,1));
    4091        2366 :   GEN res = cgetg(l, t_COL), teich = NULL;
    4092             : 
    4093        2366 :   if (S) S = RgX_to_RgC(S,lg(gmael(vT,1,1))-1);
    4094        2366 :   if (p == 2)
    4095             :   {
    4096         448 :     la = 3; /* corresponds to [1,-1] */
    4097         448 :     teichi &= 1;
    4098             :   }
    4099             :   else
    4100             :   {
    4101        1918 :     la = p; /* corresponds to [1,2,...,p-1] */
    4102        1918 :     teichi = smodss(teichi, p-1);
    4103        1918 :     if (teichi) teich = teichmullerinit(p, n);
    4104             :   }
    4105        5446 :   for (i=1; i<l; i++)
    4106             :   {
    4107        3080 :     pari_sp av2 = avma;
    4108        3080 :     GEN s = gen_0;
    4109             :     long ia;
    4110       14756 :     for (ia = 1; ia < la; ia++)
    4111             :     { /* Ta[j+1] correct mod p^n */
    4112       11676 :       GEN Ta = gmael(vT,ia,i), v = S? RgV_dotproduct(Ta, S): gel(Ta,1);
    4113       11676 :       if (teichi && ia != 1)
    4114             :       {
    4115        3843 :         if (p != 2)
    4116        3626 :           v = gmul(v, gel(teich, Fl_powu(ia,teichi,p)));
    4117             :         else
    4118         217 :           if (teichi) v = gneg(v);
    4119             :       }
    4120       11676 :       s = gadd(s, v);
    4121             :     }
    4122        3080 :     s = gadd(s, zeropadic(gp,nfinal));
    4123        3080 :     gel(res,i) = gerepileupto(av2, s);
    4124             :   }
    4125        2366 :   return gerepileupto(av, gmul(alpha, res));
    4126             : }
    4127             : /* integrate P = polynomial in log(x); vlog[j+1] = mspadicint(0,log(1+x)^j) */
    4128             : static GEN
    4129         539 : mspadicint_RgXlog(GEN P, GEN vlog)
    4130             : {
    4131         539 :   long i, d = degpol(P);
    4132         539 :   GEN s = gmul(gel(P,2), gel(vlog,1));
    4133         539 :   for (i = 1; i <= d; i++) s = gadd(s, gmul(gel(P,i+2), gel(vlog,i+1)));
    4134         539 :   return s;
    4135             : };
    4136             : 
    4137             : /* oms from mspadicmoments */
    4138             : GEN
    4139          98 : mspadicseries(GEN oms, long teichi)
    4140             : {
    4141          98 :   pari_sp av = avma;
    4142             :   GEN S, L, X, vlog, s, s2, u, logu, bin;
    4143             :   long j, p, m, n, step, stop;
    4144          98 :   checkoms(oms);
    4145          98 :   n = oms_get_n0(oms);
    4146          98 :   if (n < 1)
    4147             :   {
    4148           0 :     s = zeroser(0,0);
    4149           0 :     if (oms_is_supersingular(oms)) s = mkvec2(s,s);
    4150           0 :     return gerepilecopy(av, s);
    4151             :   }
    4152          98 :   p = oms_get_p(oms);
    4153          98 :   vlog = cgetg(n+1, t_VEC);
    4154          98 :   step = p == 2? 2: 1;
    4155          98 :   stop = 0;
    4156          98 :   S = NULL;
    4157          98 :   L = log1x(n);
    4158         644 :   for (j = 0; j < n; j++)
    4159             :   {
    4160         616 :     if (j) stop += step + u_lval(j,p); /* = step*j + v_p(j!) */
    4161         616 :     if (stop >= n) break;
    4162             :     /* S = log(1+x)^j */
    4163         546 :     gel(vlog,j+1) = mspadicint(oms,teichi,S);
    4164         546 :     S = S? RgXn_mul(S, L, n+1): L;
    4165             :   }
    4166          98 :   m = j;
    4167          98 :   u = utoipos(p == 2? 5: 1+p);
    4168          98 :   logu = glog(cvtop(u, utoipos(p), 4*m), 0);
    4169          98 :   X = gdiv(pol_x(0), logu);
    4170          98 :   s = cgetg(m+1, t_VEC);
    4171          98 :   s2 = oms_is_supersingular(oms)? cgetg(m+1, t_VEC): NULL;
    4172          98 :   bin = pol_1(0);
    4173         539 :   for (j = 0; j < m; j++)
    4174             :   { /* bin = binomial(x/log(1+p+O(p^(4*n))), j) mod x^m */
    4175         539 :     GEN a, v = mspadicint_RgXlog(bin, vlog);
    4176         539 :     int done = 1;
    4177         539 :     gel(s,j+1) = a = gel(v,1);
    4178         539 :     if (!gequal0(a) || valp(a) > 0) done = 0; else setlg(s,j+1);
    4179         539 :     if (s2)
    4180             :     {
    4181         119 :       gel(s2,j+1) = a = gel(v,2);
    4182         119 :       if (!gequal0(a) || valp(a) > 0) done = 0; else setlg(s2,j+1);
    4183             :     }
    4184         539 :     if (done || j == m-1) break;
    4185         441 :     bin = RgXn_mul(bin, gdivgs(gsubgs(X, j), j+1), m);
    4186             :   }
    4187          98 :   s = gtoser(s,0,lg(s)-1);
    4188          98 :   if (s2) { s2 = gtoser(s2,0,lg(s2)-1); s = mkvec2(s, s2); }
    4189          98 :   if (kross(oms_get_D(oms), p) >= 0) return gerepilecopy(av, s);
    4190           7 :   return gerepileupto(av, gneg(s));
    4191             : }
    4192             : void
    4193        1911 : mspadic_parse_chi(GEN s, GEN *s1, GEN *s2)
    4194             : {
    4195        1911 :   if (!s) *s1 = *s2 = gen_0;
    4196        1778 :   else switch(typ(s))
    4197             :   {
    4198        1274 :     case t_INT: *s1 = *s2 = s; break;
    4199             :     case t_VEC:
    4200         504 :       if (lg(s) == 3)
    4201             :       {
    4202         504 :         *s1 = gel(s,1);
    4203         504 :         *s2 = gel(s,2);
    4204         504 :         if (typ(*s1) == t_INT && typ(*s2) == t_INT) break;
    4205             :       }
    4206           0 :     default: pari_err_TYPE("mspadicL",s);
    4207           0 :              *s1 = *s2 = NULL;
    4208             :   }
    4209        1911 : }
    4210             : /* oms from mspadicmoments
    4211             :  * r-th derivative of L(f,chi^s,psi) in direction <chi>
    4212             :    - s \in Z_p \times \Z/(p-1)\Z, s-> chi^s=<\chi>^s_1 omega^s_2)
    4213             :    - Z -> Z_p \times \Z/(p-1)\Z par s-> (s, s mod p-1).
    4214             :  */
    4215             : GEN
    4216        1820 : mspadicL(GEN oms, GEN s, long r)
    4217             : {
    4218        1820 :   pari_sp av = avma;
    4219             :   GEN s1, s2, z, S;
    4220             :   long p, n, teich;
    4221        1820 :   checkoms(oms);
    4222        1820 :   p = oms_get_p(oms);
    4223        1820 :   n = oms_get_n(oms);
    4224        1820 :   mspadic_parse_chi(s, &s1,&s2);
    4225        1820 :   teich = umodiu(subii(s2,s1), p==2? 2: p-1);
    4226        1820 :   S = xlog1x(n, itos(s1), r, &teich);
    4227        1820 :   z = mspadicint(oms, teich, S);
    4228        1820 :   if (lg(z) == 2) z = gel(z,1);
    4229        1820 :   if (kross(oms_get_D(oms), p) < 0) z = gneg(z);
    4230        1820 :   return gerepilecopy(av, z);
    4231             : }
    4232             : 
    4233             : /****************************************************************************/
    4234             : 
    4235             : struct siegel
    4236             : {
    4237             :   GEN V, Ast;
    4238             :   long N; /* level */
    4239             :   long oo; /* index of the [oo,0] path */
    4240             :   long k1, k2; /* two distinguished indices */
    4241             :   long n; /* #W, W = initial segment [in siegelstepC] already normalized */
    4242             : };
    4243             : 
    4244             : static void
    4245         357 : siegel_init(struct siegel *C, GEN M)
    4246             : {
    4247             :   GEN CPI, CP, MM, V, W, Ast;
    4248         357 :   GEN m = gel(M,11), M2 = gel(M,2), S = msN_get_section(M);
    4249         357 :   GEN E2fromE1 = msN_get_E2fromE1(M);
    4250         357 :   long m0 = lg(M2)-1;
    4251         357 :   GEN E2  = vecslice(M2, m[1]+1, m[2]);/* E2 */
    4252         357 :   GEN E1T = vecslice(M2, m[3]+1, m0); /* E1,T2,T31 */
    4253         357 :   GEN L = shallowconcat(E1T, E2);
    4254         357 :   long i, l = lg(L), n = lg(E1T)-1, lE = lg(E2);
    4255             : 
    4256         357 :   Ast = cgetg(l, t_VECSMALL);
    4257        6069 :   for (i = 1; i < lE; ++i)
    4258             :   {
    4259        5712 :     long j = E2fromE1_c(gel(E2fromE1,i));
    4260        5712 :     Ast[n+i] = j;
    4261        5712 :     Ast[j] = n+i;
    4262             :   }
    4263         357 :   for (; i<=n; ++i) Ast[i] = i;
    4264         357 :   MM = cgetg (l,t_VEC);
    4265             : 
    4266       12089 :   for (i = 1; i < l; i++)
    4267             :   {
    4268       11732 :     GEN c = gel(S, L[i]);
    4269       11732 :     long c12, c22, c21 = ucoeff(c,2,1);
    4270       11732 :     if (!c21) { gel(MM,i) = gen_0; continue; }
    4271       11375 :     c22 = ucoeff(c,2,2);
    4272       11375 :     if (!c22) { gel(MM,i) = gen_m1; continue; }
    4273       11018 :     c12 = ucoeff(c,1,2);
    4274       11018 :     gel(MM,i) = gdivgs(stoi(c12), c22); /* right extremity > 0 */
    4275             :   }
    4276         357 :   CP = indexsort(MM);
    4277         357 :   CPI = cgetg(l, t_VECSMALL);
    4278         357 :   V = cgetg(l, t_VEC);
    4279         357 :   W = cgetg(l, t_VECSMALL);
    4280       12089 :   for (i = 1; i < l; ++i)
    4281             :   {
    4282       11732 :     gel(V,i) = zm_to_ZM(gel(S, L[CP[i]]));
    4283       11732 :     CPI[CP[i]] = i;
    4284             :   }
    4285         357 :   for (i = 1; i < l; ++i) W[CPI[i]] = CPI[Ast[i]];
    4286         357 :   C->V = V;
    4287         357 :   C->Ast = W;
    4288         357 :   C->n = 0;
    4289         357 :   C->oo = 2;
    4290         357 :   C->N = ms_get_N(M);
    4291         357 : }
    4292             : 
    4293             : static double
    4294           0 : ZMV_size(GEN v)
    4295             : {
    4296           0 :   long i, l = lg(v);
    4297           0 :   GEN z = cgetg(l, t_VECSMALL);
    4298           0 :   for (i = 1; i < l; i++) z[i] = gexpo(gel(v,i));
    4299           0 :   return ((double)zv_sum(z)) / (4*(l-1));
    4300             : }
    4301             : 
    4302             : /* apply permutation perm to struct S. Don't follow k1,k2 */
    4303             : static void
    4304        5558 : siegel_perm0(struct siegel *S, GEN perm)
    4305             : {
    4306        5558 :   long i, l = lg(S->V);
    4307        5558 :   GEN V2 = cgetg(l, t_VEC), Ast2 = cgetg(l, t_VECSMALL);
    4308        5558 :   GEN V = S->V, Ast = S->Ast;
    4309             : 
    4310        5558 :   for (i = 1; i < l; i++) gel(V2,perm[i]) = gel(V,i);
    4311        5558 :   for (i = 1; i < l; i++) Ast2[perm[i]] = perm[Ast[i]];
    4312        5558 :   S->oo = perm[S->oo];
    4313        5558 :   S->Ast = Ast2;
    4314        5558 :   S->V = V2;
    4315        5558 : }
    4316             : /* apply permutation perm to full struct S */
    4317             : static void
    4318        5194 : siegel_perm(struct siegel *S, GEN perm)
    4319             : {
    4320        5194 :   siegel_perm0(S, perm);
    4321        5194 :   S->k1 = perm[S->k1];
    4322        5194 :   S->k2 = perm[S->k2];
    4323        5194 : }
    4324             : /* cyclic permutation of lg = l-1 moving a -> 1, a+1 -> 2, etc.  */
    4325             : static GEN
    4326        2884 : rotate_perm(long l, long a)
    4327             : {
    4328        2884 :   GEN p = cgetg(l, t_VECSMALL);
    4329        2884 :   long i, j = 1;
    4330        2884 :   for (i = a; i < l; i++) p[i] = j++;
    4331        2884 :   for (i = 1; i < a; i++) p[i] = j++;
    4332        2884 :   return p;
    4333             : }
    4334             : 
    4335             : /* a1 < c1 <= a2 < c2*/
    4336             : static GEN
    4337        2520 : basic_op_perm(long l, long a1, long a2, long c1, long c2)
    4338             : {
    4339        2520 :   GEN p = cgetg(l, t_VECSMALL);
    4340        2520 :   long i, j = 1;
    4341        2520 :   p[a1] = j++;
    4342        2520 :   for (i = c1; i < a2; i++)   p[i] = j++;
    4343        2520 :   for (i = a1+1; i < c1; i++) p[i] = j++;
    4344        2520 :   p[a2] = j++;
    4345        2520 :   for (i = c2; i < l; i++)    p[i] = j++;
    4346        2520 :   for (i = 1; i < a1; i++)    p[i] = j++;
    4347        2520 :   for (i = a2+1; i < c2; i++) p[i] = j++;
    4348        2520 :   return p;
    4349             : }
    4350             : static GEN
    4351         154 : basic_op_perm_elliptic(long l, long a1)
    4352             : {
    4353         154 :   GEN p = cgetg(l, t_VECSMALL);
    4354         154 :   long i, j = 1;
    4355         154 :   p[a1] = j++;
    4356         154 :   for (i = 1; i < a1; i++)   p[i] = j++;
    4357         154 :   for (i = a1+1; i < l; i++) p[i] = j++;
    4358         154 :   return p;
    4359             : }
    4360             : static GEN
    4361       14406 : ZM2_rev(GEN T) { return mkmat2(gel(T,2), ZC_neg(gel(T,1))); }
    4362             : 
    4363             : /* In place, V = vector of consecutive paths, between x <= y.
    4364             :  * V[x..y-1] <- g*V[x..y-1] */
    4365             : static void
    4366        5733 : path_vec_mul(GEN V, long x, long y, GEN g)
    4367             : {
    4368             :   long j;
    4369             :   GEN M;
    4370       11466 :   if (x == y) return;
    4371        3360 :   M = gel(V,x); gel(V,x) = ZM_mul(g,M);
    4372       37709 :   for (j = x+1; j < y; j++) /* V[j] <- g*V[j], optimized */
    4373             :   {
    4374       34349 :     GEN Mnext = gel(V,j); /* Mnext[,1] = M[,2] */
    4375       34349 :     GEN gM = gel(V,j-1), u = gel(gM,2);
    4376       34349 :     if (!ZV_equal(gel(M,2), gel(Mnext,1))) u = ZC_neg(u);
    4377       34349 :     gel(V,j) = mkmat2(u, ZM_ZC_mul(g,gel(Mnext,2)));
    4378       34349 :     M = Mnext;
    4379             :   }
    4380             : }
    4381             : 
    4382        4830 : static long prev(GEN V, long i) { return (i == 1)? lg(V)-1: i-1; }
    4383        4830 : static long next(GEN V, long i) { return (i == lg(V)-1)? 1: i+1; }
    4384             : static GEN
    4385       19600 : ZM_det2(GEN u, GEN v)
    4386             : {
    4387       19600 :   GEN a = gel(u,1), c = gel(u,2);
    4388       19600 :   GEN b = gel(v,1), d = gel(v,2); return subii(mulii(a,d), mulii(b,c));
    4389             : }
    4390             : static GEN
    4391       14406 : ZM2_det(GEN T) { return ZM_det2(gel(T,1),gel(T,2)); }
    4392             : static void
    4393        4466 : fill1(GEN V, long a)
    4394             : {
    4395        4466 :   long p = prev(V,a), n = next(V,a);
    4396        4466 :   GEN u = gmael(V,p,2), v = gmael(V,n,1);
    4397        4466 :   if (signe(ZM_det2(u,v)) < 0) v = ZC_neg(v);
    4398        4466 :   gel(V,a) = mkmat2(u, v);
    4399        4466 : }
    4400             : /* a1 < a2 */
    4401             : static void
    4402        2520 : fill2(GEN V, long a1, long a2)
    4403             : {
    4404        2520 :   if (a2 != a1+1) { fill1(V,a1); fill1(V,a2); } /* non adjacent, reconnect */
    4405             :   else /* parabolic */
    4406             :   {
    4407         364 :     long p = prev(V,a1), n = next(V,a2);
    4408         364 :     GEN u, v, C = gmael(V,a1,2), mC = ZC_neg(C); /* = \pm V[a2][1] */
    4409         364 :     u = gmael(V,p,2); v = (signe(ZM_det2(u,C)) < 0)? mC: C;
    4410         364 :     gel(V,a1) = mkmat2(u,v);
    4411         364 :     v = gmael(V,n,1); u = (signe(ZM_det2(C,v)) < 0)? mC: C;
    4412         364 :     gel(V,a2) = mkmat2(u,v);
    4413             :   }
    4414        2520 : }
    4415             : 
    4416             : /* DU = det(U), return g = T*U^(-1) or NULL if not in Gamma0(N); if N = 0,
    4417             :  * only test whether g is integral */
    4418             : static GEN
    4419       14693 : ZM2_div(GEN T, GEN U, GEN DU, long N)
    4420             : {
    4421       14693 :   GEN a=gcoeff(U,1,1), b=gcoeff(U,1,2), c=gcoeff(U,2,1), d=gcoeff(U,2,2);
    4422       14693 :   GEN e=gcoeff(T,1,1), f=gcoeff(T,1,2), g=gcoeff(T,2,1), h=gcoeff(T,2,2);
    4423             :   GEN A, B, C, D, r;
    4424             : 
    4425       14693 :   C = dvmdii(subii(mulii(d,g), mulii(c,h)), DU, &r);
    4426       14693 :   if (r != gen_0 || (N && smodis(C,N))) return NULL;
    4427       14406 :   A = dvmdii(subii(mulii(d,e), mulii(c,f)), DU, &r);
    4428       14406 :   if (r != gen_0) return NULL;
    4429       14406 :   B = dvmdii(subii(mulii(a,f), mulii(b,e)), DU, &r);
    4430       14406 :   if (r != gen_0) return NULL;
    4431       14406 :   D = dvmdii(subii(mulii(a,h), mulii(g,b)), DU, &r);
    4432       14406 :   if (r != gen_0) return NULL;
    4433       14406 :   return mkmat22(A,B,C,D);
    4434             : }
    4435             : 
    4436             : static GEN
    4437       14406 : get_g(struct siegel *S, long a1)
    4438             : {
    4439       14406 :   long a2 = S->Ast[a1];
    4440       14406 :   GEN a = gel(S->V,a1), ar = ZM2_rev(gel(S->V,a2)), Dar = ZM2_det(ar);
    4441       14406 :   GEN g = ZM2_div(a, ar, Dar, S->N);
    4442       14406 :   if (!g)
    4443             :   {
    4444         287 :     GEN tau = mkmat22(gen_0,gen_m1, gen_1,gen_m1); /*[0,-1;1,-1]*/
    4445         287 :     g = ZM2_div(ZM_mul(ar, tau), ar, Dar, 0);
    4446             :   }
    4447       14406 :   return g;
    4448             : }
    4449             : /* input V = (X1 a X2 | X3 a^* X4) + Ast
    4450             :  * a1 = index of a
    4451             :  * a2 = index of a^*, inferred from a1. We must have a != a^*
    4452             :  * c1 = first cut [ index of first path in X3 ]
    4453             :  * c2 = second cut [ either in X4 or X1, index of first path ]
    4454             :  * Assume a < a^* (cf Paranoia below): c1 or c2 must be in
    4455             :  *    ]a,a^*], and the other in the "complement" ]a^*,a] */
    4456             : static void
    4457        2520 : basic_op(struct siegel *S, long a1, long c1, long c2)
    4458             : {
    4459        2520 :   long l = lg(S->V), a2 = S->Ast[a1];
    4460             :   GEN g;
    4461             : 
    4462        2520 :   if (a1 == a2)
    4463             :   { /* a = a^* */
    4464           0 :     g = get_g(S, a1);
    4465           0 :     path_vec_mul(S->V, a1+1, l, g);
    4466           0 :     siegel_perm(S, basic_op_perm_elliptic(l, a1));
    4467             :     /* fill the hole left at a1, reconnect the path */
    4468        2520 :     fill1(S->V, a1); return;
    4469             :   }
    4470             : 
    4471             :   /* Paranoia: (a,a^*) conjugate, call 'a' the first one */
    4472        2520 :   if (a2 < a1) lswap(a1,a2);
    4473             :   /* Now a1 < a2 */
    4474        2520 :   if (c1 <= a1 || c1 > a2) lswap(c1,c2); /* ensure a1 < c1 <= a2 */
    4475        2520 :   if (c2 < a1)
    4476             :   { /* if cut c2 is in X1 = X11|X12, rotate to obtain
    4477             :        (a X2 | X3 a^* X4 X11|X12): then a1 = 1 */
    4478        2520 :     GEN p = rotate_perm(l, a1);
    4479        2520 :     siegel_perm(S, p);
    4480        2520 :     a1 = 1; /* = p[a1] */
    4481        2520 :     a2 = S->Ast[1]; /* > a1 */
    4482        2520 :     c1 = p[c1];
    4483        2520 :     c2 = p[c2];
    4484             :   }
    4485             :   /* Now a1 < c1 <= a2 < c2; a != a^* */
    4486        2520 :   g = get_g(S, a1);
    4487        2520 :   if (S->oo >= c1 && S->oo < c2) /* W inside [c1..c2[ */
    4488         539 :   { /* c2 -> c1 excluding a1 */
    4489         539 :     GEN gi = SL2_inv(g); /* g a^* = a; gi a = a^* */
    4490         539 :     path_vec_mul(S->V, 1, a1, gi);
    4491         539 :     path_vec_mul(S->V, a1+1, c1, gi);
    4492         539 :     path_vec_mul(S->V, c2, l, gi);
    4493             :   }
    4494             :   else
    4495             :   { /* c1 -> c2 excluding a2 */
    4496        1981 :     path_vec_mul(S->V, c1, a2, g);
    4497        1981 :     path_vec_mul(S->V, a2+1, c2, g);
    4498             :   }
    4499        2520 :   siegel_perm(S, basic_op_perm(l, a1,a2, c1,c2));
    4500             :   /* fill the holes left at a1,a2, reconnect the path */
    4501        2520 :   fill2(S->V, a1, a2);
    4502             : }
    4503             : /* a = a^* (elliptic case) */
    4504             : static void
    4505         154 : basic_op_elliptic(struct siegel *S, long a1)
    4506             : {
    4507         154 :   long l = lg(S->V);
    4508         154 :   GEN g = get_g(S, a1);
    4509         154 :   path_vec_mul(S->V, a1+1, l, g);
    4510         154 :   siegel_perm(S, basic_op_perm_elliptic(l, a1));
    4511             :   /* fill the hole left at a1 (now at 1), reconnect the path */
    4512         154 :   fill1(S->V, 1);
    4513         154 : }
    4514             : 
    4515             : /* input V = W X a b Y a^* Z b^* T, W already normalized
    4516             :  * X = [n+1, k1-1], Y = [k2+1, Ast[k1]-1],
    4517             :  * Z = [Ast[k1]+1, Ast[k2]-1], T = [Ast[k2]+1, oo].
    4518             :  * Assume that X doesn't start by c c^* or a b a^* b^*. */
    4519             : static void
    4520        1057 : siegelstep(struct siegel *S)
    4521             : {
    4522        1057 :   if (S->Ast[S->k1] == S->k1)
    4523             :   {
    4524         154 :     basic_op_elliptic(S, S->k1);
    4525         154 :     S->n++;
    4526             :   }
    4527         903 :   else if (S->Ast[S->k1] == S->k1+1)
    4528             :   {
    4529         364 :     basic_op(S, S->k1, S->Ast[S->k1], 1); /* 1: W starts there */
    4530         364 :     S->n += 2;
    4531             :   }
    4532             :   else
    4533             :   {
    4534         539 :     basic_op(S, S->k2, S->Ast[S->k1], 1); /* 1: W starts there */
    4535         539 :     basic_op(S, S->k1, S->k2, S->Ast[S->k2]);
    4536         539 :     basic_op(S, S->Ast[S->k2], S->k2, S->Ast[S->k1]);
    4537         539 :     basic_op(S, S->k1, S->Ast[S->k1], S->Ast[S->k2]);
    4538         539 :     S->n += 4;
    4539             :   }
    4540        1057 : }
    4541             : 
    4542             : /* normalize hyperbolic polygon */
    4543             : static void
    4544         301 : mssiegel(struct siegel *S)
    4545             : {
    4546         301 :   pari_sp av = avma;
    4547             :   long k, t, nv;
    4548             : #ifdef COUNT
    4549             :   long countset[16];
    4550             :   for (k = 0; k < 16; k++) countset[k] = 0;
    4551             : #endif
    4552             : 
    4553         301 :   nv = lg(S->V)-1;
    4554         301 :   if (DEBUGLEVEL>1) err_printf("nv = %ld, expo = %.2f\n", nv,ZMV_size(S->V));
    4555         301 :   t = 0;
    4556        2506 :   while (S->n < nv)
    4557             :   {
    4558        1904 :     if (S->Ast[S->n+1] == S->n+1) { S->n++; continue; }
    4559        1778 :     if (S->Ast[S->n+1] == S->n+2) { S->n += 2; continue; }
    4560        1134 :     if (S->Ast[S->n+1] == S->n+3 && S->Ast[S->n+2] == S->n+4) { S->n += 4; continue; }
    4561        1057 :     k = nv;
    4562        2184 :     while (k > S->n)
    4563             :     {
    4564        1127 :       if (S->Ast[k] == k) { k--; continue; }
    4565        1099 :       if (S->Ast[k] == k-1) { k -= 2; continue; }
    4566        1057 :       if (S->Ast[k] == k-2 && S->Ast[k-1] == k-3) { k -= 4; continue; }
    4567        1057 :       break;
    4568             :     }
    4569        1057 :     if (k != nv) { siegel_perm0(S, rotate_perm(nv+1, k+1)); S->n += nv-k; }
    4570             : 
    4571        6223 :     for (k = S->n+1; k <= nv; k++)
    4572        6223 :       if (S->Ast[k] <= k) { t = S->Ast[k]; break; }
    4573        1057 :     S->k1 = t;
    4574        1057 :     S->k2 = t+1;
    4575             : #ifdef COUNT
    4576             :     countset[ ((S->k1-1 == S->n)
    4577             :               | ((S->k2 == S->Ast[S->k1]-1) << 1)
    4578             :               | ((S->Ast[S->k1] == S->Ast[S->k2]-1) << 2)
    4579             :               | ((S->Ast[S->k2] == nv) << 3)) ]++;
    4580             : #endif
    4581        1057 :     siegelstep(S);
    4582        1057 :     if (gc_needed(av,2))
    4583             :     {
    4584           0 :       if(DEBUGMEM>1) pari_warn(warnmem,"mspolygon, n = %ld",S->n);
    4585           0 :       gerepileall(av, 2, &S->V, &S->Ast);
    4586             :     }
    4587             :   }
    4588         301 :   if (DEBUGLEVEL>1) err_printf("expo = %.2f\n", ZMV_size(S->V));
    4589             : #ifdef COUNT
    4590             :   for (k = 0; k < 16; k++)
    4591             :     err_printf("%3ld: %6ld\n", k, countset[k]);
    4592             : #endif
    4593         301 : }
    4594             : GEN
    4595         378 : mspolygon(GEN M, long flag)
    4596             : {
    4597         378 :   pari_sp av = avma;
    4598             :   struct siegel S;
    4599             :   long i, l;
    4600             :   GEN G, msN;
    4601         378 :   if (typ(M) == t_INT)
    4602             :   {
    4603         301 :     long N = itos(M);
    4604         301 :     if (N <= 0) pari_err_DOMAIN("msinit","N", "<=", gen_0,M);
    4605         301 :     msN = msinit_N(N);
    4606             :   }
    4607          77 :   else { checkms(M); msN = get_msN(M); }
    4608         378 :   if (flag < 0 || flag > 1) pari_err_FLAG("mspolygon");
    4609         378 :   if (ms_get_N(msN) == 1)
    4610             :   {
    4611          21 :     GEN S = mkS(), V = mkvec2(matid(2), S);
    4612          21 :     return gerepilecopy(av, mkvec3(V, mkvecsmall2(1,2), mkvec2(S, mkTAU())));
    4613             :   }
    4614         357 :   siegel_init(&S, msN);
    4615         357 :   l = lg(S.V);
    4616         357 :   if (flag)
    4617             :   {
    4618         301 :     long oo2 = 0;
    4619         301 :     mssiegel(&S);
    4620        3451 :     for (i = 1; i < l; i++)
    4621             :     {
    4622        3451 :       GEN c = gel(S.V, i);
    4623        3451 :       GEN c22 = gcoeff(c,2,2); if (!signe(c22)) { oo2 = i; break; }
    4624             :     }
    4625         301 :     if (!oo2) pari_err_BUG("mspolygon");
    4626         301 :     siegel_perm0(&S, rotate_perm(l, oo2));
    4627             :   }
    4628         357 :   G = cgetg(l, t_VEC);
    4629         357 :   for (i = 1; i < l; i++) gel(G,i) = get_g(&S, i);
    4630         357 :   return gerepilecopy(av, mkvec3(S.V, S.Ast, G));
    4631             : }
    4632             : 
    4633             : #if 0
    4634             : static int
    4635             : iselliptic(GEN Ast, long i) { return i == Ast[i]; }
    4636             : static int
    4637             : isparabolic(GEN Ast, long i)
    4638             : { long i2 = Ast[i]; return (i2 == i+1 || i2 == i-1); }
    4639             : #endif
    4640             : 
    4641             : /* M from msinit, F QM maximal rank */
    4642             : GEN
    4643          56 : mslattice(GEN M, GEN F)
    4644             : {
    4645          56 :   pari_sp av = avma;
    4646             :   long i, ivB, j, k, l, lF;
    4647             :   GEN D, U, G, A, vB, m, d;
    4648             : 
    4649          56 :   checkms(M);
    4650          56 :   if (!F) F = gel(mscuspidal(M, 0), 1);
    4651             :   else
    4652             :   {
    4653          28 :     if (is_Qevproj(F)) F = gel(F,1);
    4654          28 :     if (typ(F) != t_MAT) pari_err_TYPE("mslattice",F);
    4655             :   }
    4656          56 :   lF = lg(F); if (lF == 1) return cgetg(1, t_MAT);
    4657          56 :   D = mspolygon(M,0);
    4658          56 :   k = msk_get_weight(M);
    4659          56 :   F = vec_Q_primpart(F);
    4660          56 :   if (typ(F)!=t_MAT || !RgM_is_ZM(F)) pari_err_TYPE("mslattice",F);
    4661          56 :   G = gel(D,3); l = lg(G);
    4662          56 :   A = gel(D,2);
    4663          56 :   vB = cgetg(l, t_COL);
    4664          56 :   m = mkmat2(mkcol2(gen_0,gen_1), NULL);
    4665        6860 :   for (i = ivB = 1; i < l; i++)
    4666             :   {
    4667        6804 :     GEN B, vb, g = gel(G,i);
    4668        6804 :     if (A[i] < i) continue;
    4669        3409 :     gel(m,2) = SL2_inv2(g);
    4670        3409 :     vb = mseval(M, F, m);
    4671        3409 :     if (k == 2) B = vb;
    4672             :     else
    4673             :     {
    4674             :       long lB;
    4675         147 :       B = RgXV_to_RgM(vb, k-1);
    4676             :       /* add coboundaries */
    4677         147 :       B = shallowconcat(B, RgM_Rg_sub(RgX_act_Gl2Q(g, k), gen_1));
    4678             :       /* beware: the basis for RgX_act_Gl2Q is (X^(k-2),...,Y^(k-2)) */
    4679         147 :       lB = lg(B);
    4680         147 :       for (j = 1; j < lB; j++) gel(B,j) = vecreverse(gel(B,j));
    4681             :     }
    4682        3409 :     gel(vB, ivB++) = B;
    4683             :   }
    4684          56 :   setlg(vB, ivB);
    4685          56 :   vB = shallowmatconcat(vB);
    4686          56 :   if (ZM_equal0(vB)) return gerepilecopy(av, F);
    4687             : 
    4688          56 :   (void)QM_ImQ_hnfall(vB, &U, 0);
    4689          56 :   if (k > 2) U = rowslice(U, 1, lgcols(U)-k); /* remove coboundary part */
    4690          56 :   U = Q_remove_denom(U, &d);
    4691          56 :   F = ZM_hnf(ZM_mul(F, U));
    4692          56 :   if (d) F = RgM_Rg_div(F, d);
    4693          56 :   return gerepileupto(av, F);
    4694             : }
    4695             : 
    4696             : /**** Petersson scalar product ****/
    4697             : 
    4698             : /* oo -> g^(-1) oo */
    4699             : static GEN
    4700        6181 : cocycle(GEN g)
    4701        6181 : { return mkmat22(gen_1, gcoeff(g,2,2), gen_0, negi(gcoeff(g,2,1))); }
    4702             : 
    4703             : /* C = vecbinome(k-2) */
    4704             : static GEN
    4705       17983 : bil(GEN P, GEN Q, GEN C)
    4706             : {
    4707       17983 :   long i, n = lg(C)-2; /* k - 2 */
    4708             :   GEN s;
    4709       17983 :   if (!n) return gmul(P,Q);
    4710       17962 :   if (typ(P) != t_POL) P = scalarpol_shallow(P,0);
    4711       17962 :   if (typ(Q) != t_POL) Q = scalarpol_shallow(Q,0);
    4712       17962 :   s = gen_0;
    4713       71960 :   for (i = 0; i <= n; i++)
    4714             :   {
    4715       53998 :     GEN t = gdiv(gmul(RgX_coeff(P,i), RgX_coeff(Q, n-i)), gel(C,i+1));
    4716       53998 :     s = odd(i)? gsub(s, t): gadd(s, t);
    4717             :   }
    4718       17962 :   return s;
    4719             : }
    4720             : 
    4721             : static void
    4722        1351 : mspetersson_i(GEN W, GEN F, GEN G, GEN *pvf, GEN *pvg, GEN *pC)
    4723             : {
    4724        1351 :   GEN WN = get_msN(W), annT2, annT31, section, c, vf, vg;
    4725             :   long i, n1, n2, n3;
    4726             : 
    4727        1351 :   annT2 = msN_get_annT2(WN);
    4728        1351 :   annT31 = msN_get_annT31(WN);
    4729        1351 :   section = msN_get_section(WN);
    4730             : 
    4731        1351 :   if (ms_get_N(WN) == 1)
    4732             :   {
    4733           7 :     vf = cgetg(3, t_VEC);
    4734           7 :     vg = cgetg(3, t_VEC);
    4735           7 :     gel(vf,1) = mseval(W, F, gel(section,1));
    4736           7 :     gel(vf,2) = gneg(gel(vf,1));
    4737           7 :     n1 = 0;
    4738             :   }
    4739             :   else
    4740             :   {
    4741        1344 :     GEN singlerel = msN_get_singlerel(WN);
    4742        1344 :     GEN gen = msN_get_genindex(WN);
    4743        1344 :     long l = lg(gen);
    4744        1344 :     vf = cgetg(l, t_VEC);
    4745        1344 :     vg = cgetg(l, t_VEC); /* generators of Delta ordered as E1,T2,T31 */
    4746        1344 :     for (i = 1; i < l; i++) gel(vf, i) = mseval(W, F, gel(section,gen[i]));
    4747        1344 :     n1 = ms_get_nbE1(WN); /* E1 */
    4748        7420 :     for (i = 1; i <= n1; i++)
    4749             :     {
    4750        6076 :       c = cocycle(gcoeff(gel(singlerel,i),2,1));
    4751        6076 :       gel(vg, i) = mseval(W, G, c);
    4752             :     }
    4753             :   }
    4754        1351 :   n2 = lg(annT2)-1; /* T2 */
    4755        1386 :   for (i = 1; i <= n2; i++)
    4756             :   {
    4757          35 :     c = cocycle(gcoeff(gel(annT2,i), 2,1));
    4758          35 :     gel(vg, i+n1) = gmul2n(mseval(W, G, c), -1);
    4759             :   }
    4760        1351 :   n3 = lg(annT31)-1; /* T31 */
    4761        1386 :   for (i = 1; i <= n3; i++)
    4762             :   {
    4763             :     GEN f;
    4764          35 :     c = cocycle(gcoeff(gel(annT31,i), 2,1));
    4765          35 :     f = mseval(W, G, c);
    4766          35 :     c = cocycle(gcoeff(gel(annT31,i), 3,1));
    4767          35 :     gel(vg, i+n1+n2) = gdivgs(gadd(f, mseval(W, G, c)), 3);
    4768             :   }
    4769        1351 :   *pC = vecbinome(msk_get_weight(W) - 2);
    4770        1351 :   *pvf = vf;
    4771        1351 :   *pvg = vg;
    4772        1351 : }
    4773             : 
    4774             : /* Petersson product on Hom_G(Delta_0, V_k) */
    4775             : GEN
    4776        1351 : mspetersson(GEN W, GEN F, GEN G)
    4777             : {
    4778        1351 :   pari_sp av = avma;
    4779             :   GEN vf, vg, C;
    4780             :   long k, l, tG, tF;
    4781        1351 :   checkms(W);
    4782        1351 :   if (!F) F = matid(msdim(W));
    4783        1351 :   if (!G) G = F;
    4784        1351 :   tF = typ(F);
    4785        1351 :   tG = typ(G);
    4786        1351 :   if (tF == t_MAT && tG != t_MAT) pari_err_TYPE("mspetersson",G);
    4787        1351 :   if (tG == t_MAT && tF != t_MAT) pari_err_TYPE("mspetersson",F);
    4788        1351 :   mspetersson_i(W, F, G, &vf, &vg, &C);
    4789        1351 :   l = lg(vf);
    4790        1351 :   if (tF != t_MAT)
    4791             :   { /* <F,G>, two symbols */
    4792        1274 :     GEN s = gen_0;
    4793        1274 :     for (k = 1; k < l; k++) s = gadd(s, bil(gel(vf,k), gel(vg,k), C));
    4794        1274 :     return gerepileupto(av, s);
    4795             :   }
    4796          77 :   else if (F != G)
    4797             :   { /* <(f_1,...,f_m), (g_1,...,g_n)> */
    4798           0 :     long iF, iG, lF = lg(F), lG = lg(G);
    4799           0 :     GEN M = cgetg(lG, t_MAT);
    4800           0 :     for (iG = 1; iG < lG; iG++)
    4801             :     {
    4802           0 :       GEN c = cgetg(lF, t_COL);
    4803           0 :       gel(M,iG) = c;
    4804           0 :       for (iF = 1; iF < lF; iF++)
    4805             :       {
    4806           0 :         GEN s = gen_0;
    4807           0 :         for (k = 1; k < l; k++)
    4808           0 :           s = gadd(s, bil(gmael(vf,k,iF), gmael(vg,k,iG), C));
    4809           0 :         gel(c,iF) = s; /* M[iF,iG] = <F[iF], G[iG] > */
    4810             :       }
    4811             :     }
    4812           0 :     return gerepilecopy(av, M);
    4813             :   }
    4814             :   else
    4815             :   { /* <(f_1,...,f_n), (f_1,...,f_n)> */
    4816          77 :     long iF, iG, n = lg(F)-1;
    4817          77 :     GEN M = zeromatcopy(n,n);
    4818         693 :     for (iG = 1; iG <= n; iG++)
    4819        3192 :       for (iF = iG+1; iF <= n; iF++)
    4820             :       {
    4821        2576 :         GEN s = gen_0;
    4822       14728 :         for (k = 1; k < l; k++)
    4823       12152 :           s = gadd(s, bil(gmael(vf,k,iF), gmael(vg,k,iG), C));
    4824        2576 :         gcoeff(M,iF,iG) = s; /* <F[iF], F[iG] > */
    4825        2576 :         gcoeff(M,iG,iF) = gneg(s);
    4826             :       }
    4827          77 :     return gerepilecopy(av, M);
    4828             :   }
    4829             : }

Generated by: LCOV version 1.11