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 to exceed 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 - mftrace.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23332-367b47754) Lines: 7294 7462 97.7 %
Date: 2018-12-10 05:41:52 Functions: 744 747 99.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Copyright (C) 2016  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
      13             : 
      14             : /*************************************************************************/
      15             : /*                                                                       */
      16             : /*              Modular forms package based on trace formulas            */
      17             : /*                                                                       */
      18             : /*************************************************************************/
      19             : #include "pari.h"
      20             : #include "paripriv.h"
      21             : 
      22             : static const long EXTRAPREC = DEFAULTPREC-2;
      23             : 
      24             : enum {
      25             :   MF_SPLIT = 1,
      26             :   MF_EISENSPACE,
      27             :   MF_FRICKE,
      28             :   MF_MF2INIT,
      29             :   MF_SPLITN
      30             : };
      31             : 
      32             : typedef struct {
      33             :   GEN vnew, vfull, DATA, VCHIP;
      34             :   long n, newHIT, newTOTAL, cuspHIT, cuspTOTAL;
      35             : } cachenew_t;
      36             : 
      37             : static void init_cachenew(cachenew_t *c, long n, long N, GEN f);
      38             : static GEN mfinit_i(GEN NK, long space);
      39             : static GEN mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw);
      40             : static GEN mf2init_Nkchi(long N, long k, GEN CHI, long space, long flraw);
      41             : static GEN mf2basis(long N, long r, GEN CHI, GEN *pCHI1, long space);
      42             : static GEN mfeisensteinbasis(long N, long k, GEN CHI);
      43             : static GEN mfeisensteindec(GEN mf, GEN F);
      44             : static GEN initwt1newtrace(GEN mf);
      45             : static GEN initwt1trace(GEN mf);
      46             : static GEN myfactoru(long N);
      47             : static GEN mydivisorsu(long N);
      48             : static GEN mygmodulo_lift(long k, long ord, GEN C, long vt);
      49             : static GEN mfcoefs_i(GEN F, long n, long d);
      50             : static GEN bhnmat_extend(GEN M, long m,long l, GEN S, cachenew_t *cache);
      51             : static GEN initnewtrace(long N, GEN CHI);
      52             : static void dbg_cachenew(cachenew_t *C);
      53             : static GEN hecke_i(long m, long l, GEN V, GEN F, GEN DATA);
      54             : static GEN c_Ek(long n, long d, GEN F);
      55             : static GEN RgV_heckef2(long n, long d, GEN V, GEN F, GEN DATA);
      56             : static GEN mfcusptrace_i(long N, long k, long n, GEN Dn, GEN TDATA);
      57             : static GEN mfnewtracecache(long N, long k, long n, cachenew_t *cache);
      58             : static GEN colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *c);
      59             : static GEN dihan(GEN bnr, GEN w, GEN k0j, ulong n);
      60             : static GEN sigchi(long k, GEN CHI, long n);
      61             : static GEN sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord);
      62             : static GEN mflineardivtomat(long N, GEN vF, long n);
      63             : static GEN mfdihedralcusp(long N, GEN CHI);
      64             : static long mfdihedralcuspdim(long N, GEN CHI);
      65             : static GEN mfdihedralnew(long N, GEN CHI);
      66             : static GEN mfdihedralall(GEN LIM);
      67             : static long mfwt1cuspdim(long N, GEN CHI);
      68             : static long mf2dim_Nkchi(long N, long k, GEN CHI, ulong space);
      69             : static long mfdim_Nkchi(long N, long k, GEN CHI, long space);
      70             : static GEN charLFwtk(long k, GEN CHI, long ord);
      71             : static GEN mfeisensteingacx(GEN E,long w,GEN ga,long n,long prec);
      72             : static GEN mfgaexpansion(GEN mf, GEN F, GEN gamma, long n, long prec);
      73             : static GEN mfEHmat(long n, long r);
      74             : static GEN mfEHcoef(long r, long N);
      75             : static GEN mftobasis_i(GEN mf, GEN F);
      76             : 
      77             : static GEN
      78       28287 : mkgNK(GEN N, GEN k, GEN CHI, GEN P) { return mkvec4(N, k, CHI, P); }
      79             : static GEN
      80       12670 : mkNK(long N, long k, GEN CHI) { return mkgNK(stoi(N), stoi(k), CHI, pol_x(1)); }
      81             : GEN
      82        7098 : MF_get_CHI(GEN mf) { return gmael(mf,1,3); }
      83             : GEN
      84       17402 : MF_get_gN(GEN mf) { return gmael(mf,1,1); }
      85             : long
      86       16625 : MF_get_N(GEN mf) { return itou(MF_get_gN(mf)); }
      87             : GEN
      88       11802 : MF_get_gk(GEN mf) { return gmael(mf,1,2); }
      89             : long
      90        6013 : MF_get_k(GEN mf)
      91             : {
      92        6013 :   GEN gk = MF_get_gk(mf);
      93        6013 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
      94        6013 :   return itou(gk);
      95             : }
      96             : long
      97         147 : MF_get_r(GEN mf)
      98             : {
      99         147 :   GEN gk = MF_get_gk(mf);
     100         147 :   if (typ(gk) == t_INT) pari_err_IMPL("integral weight");
     101         147 :   return itou(gel(gk, 1)) >> 1;
     102             : }
     103             : long
     104       12040 : MF_get_space(GEN mf) { return itos(gmael(mf,1,4)); }
     105             : GEN
     106        3626 : MF_get_E(GEN mf) { return gel(mf,2); }
     107             : GEN
     108       18410 : MF_get_S(GEN mf) { return gel(mf,3); }
     109             : GEN
     110        1183 : MF_get_basis(GEN mf) { return shallowconcat(gel(mf,2), gel(mf,3)); }
     111             : long
     112        4466 : MF_get_dim(GEN mf)
     113             : {
     114        4466 :   switch(MF_get_space(mf))
     115             :   {
     116             :     case mf_FULL:
     117         560 :       return lg(MF_get_S(mf)) - 1 + lg(MF_get_E(mf))-1;
     118             :     case mf_EISEN:
     119         140 :       return lg(MF_get_E(mf))-1;
     120             :     default: /* mf_NEW, mf_CUSP, mf_OLD */
     121        3766 :       return lg(MF_get_S(mf)) - 1;
     122             :   }
     123             : }
     124             : GEN
     125        6685 : MFnew_get_vj(GEN mf) { return gel(mf,4); }
     126             : GEN
     127         476 : MFcusp_get_vMjd(GEN mf) { return gel(mf,4); }
     128             : GEN
     129        6083 : MF_get_M(GEN mf) { return gmael(mf,5,3); }
     130             : GEN
     131        2352 : MF_get_Minv(GEN mf) { return gmael(mf,5,2); }
     132             : GEN
     133        7770 : MF_get_Mindex(GEN mf) { return gmael(mf,5,1); }
     134             : 
     135             : /* ordinary gtocol forgets about initial 0s */
     136             : GEN
     137        1596 : sertocol(GEN S) { return gtocol0(S, -(lg(S) - 2 + valp(S))); }
     138             : /*******************************************************************/
     139             : /*     Linear algebra in cyclotomic fields (TODO: export this)     */
     140             : /*******************************************************************/
     141             : /* return r and split prime p giving projection Q(zeta_n) -> Fp, zeta -> r */
     142             : static ulong
     143         665 : QabM_init(long n, ulong *p)
     144             : {
     145         665 :   ulong pinit = 1000000007;
     146             :   forprime_t T;
     147         665 :   if (n <= 1) { *p = pinit; return 0; }
     148         658 :   u_forprime_arith_init(&T, pinit, ULONG_MAX, 1, n);
     149         658 :   *p = u_forprime_next(&T);
     150         658 :   return Flx_oneroot(ZX_to_Flx(polcyclo(n, 0), *p), *p);
     151             : }
     152             : static ulong
     153      676956 : Qab_to_Fl(GEN P, ulong r, ulong p)
     154             : {
     155             :   ulong t;
     156             :   GEN den;
     157      676956 :   P = Q_remove_denom(liftpol_shallow(P), &den);
     158      676956 :   if (typ(P) == t_POL) { GEN Pp = ZX_to_Flx(P, p); t = Flx_eval(Pp, r, p); }
     159      654591 :   else t = umodiu(P, p);
     160      676956 :   if (den) t = Fl_div(t, umodiu(den, p), p);
     161      676956 :   return t;
     162             : }
     163             : static GEN
     164       15386 : QabC_to_Flc(GEN C, ulong r, ulong p)
     165             : {
     166       15386 :   long i, l = lg(C);
     167       15386 :   GEN A = cgetg(l, t_VECSMALL);
     168       15386 :   for (i = 1; i < l; i++) uel(A,i) = Qab_to_Fl(gel(C,i), r, p);
     169       15386 :   return A;
     170             : }
     171             : static GEN
     172         385 : QabM_to_Flm(GEN M, ulong r, ulong p)
     173             : {
     174             :   long i, l;
     175         385 :   GEN A = cgetg_copy(M, &l);
     176       15771 :   for (i = 1; i < l; i++)
     177       15386 :     gel(A, i) = QabC_to_Flc(gel(M, i), r, p);
     178         385 :   return A;
     179             : }
     180             : /* A a t_POL */
     181             : static GEN
     182         553 : QabX_to_Flx(GEN A, ulong r, ulong p)
     183             : {
     184         553 :   long i, l = lg(A);
     185         553 :   GEN a = cgetg(l, t_VECSMALL);
     186         553 :   a[1] = ((ulong)A[1])&VARNBITS;
     187         553 :   for (i = 2; i < l; i++) uel(a,i) = Qab_to_Fl(gel(A,i), r, p);
     188         553 :   return Flx_renormalize(a, l);
     189             : }
     190             : 
     191             : /* FIXME: remove */
     192             : static GEN
     193         952 : ZabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *den, int ratlift)
     194             : {
     195         952 :   GEN v = ZabM_indexrank(M, P, n);
     196         952 :   if (pv) *pv = v;
     197         952 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
     198         952 :   return ratlift? ZabM_inv_ratlift(M, P, n, den): ZabM_inv(M, P, n, den);
     199             : }
     200             : 
     201             : /* M matrix with coeff in Q(\chi)), where Q(\chi) = Q(X)/(P) for
     202             :  * P = cyclotomic Phi_n. Assume M rational if n <= 2 */
     203             : static GEN
     204        1617 : QabM_ker(GEN M, GEN P, long n)
     205             : {
     206             :   GEN B;
     207        1617 :   if (n <= 2)
     208         882 :     B = ZM_ker(Q_primpart(M));
     209             :   else
     210         735 :     B = ZabM_ker(Q_primpart(liftpol_shallow(M)), P, n);
     211        1617 :   return B;
     212             : }
     213             : /* pseudo-inverse of M. FIXME: should replace QabM_pseudoinv */
     214             : static GEN
     215        1162 : QabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     216             : {
     217             :   GEN cM, Mi;
     218        1162 :   if (n <= 2)
     219             :   {
     220         875 :     M = Q_primitive_part(M, &cM);
     221         875 :     Mi = ZM_pseudoinv(M, pv, pden); /* M^(-1) = Mi / (cM * den) */
     222             :   }
     223             :   else
     224             :   {
     225         287 :     M = Q_primitive_part(liftpol_shallow(M), &cM);
     226         287 :     Mi = ZabM_pseudoinv(M, P, n, pv, pden);
     227             :   }
     228        1162 :   *pden = mul_content(*pden, cM);
     229        1162 :   return Mi;
     230             : }
     231             : /* FIXME: delete */
     232             : static GEN
     233         959 : QabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     234             : {
     235         959 :   GEN Mi = QabM_pseudoinv_i(M, P, n, pv, pden);
     236         959 :   return P? gmodulo(Mi, P): Mi;
     237             : }
     238             : 
     239             : static GEN
     240        9590 : QabM_indexrank(GEN M, GEN P, long n)
     241             : {
     242             :   GEN z;
     243        9590 :   if (n <= 2)
     244             :   {
     245        8477 :     M = vec_Q_primpart(M);
     246        8477 :     z = ZM_indexrank(M); /* M^(-1) = Mi / (cM * den) */
     247             :   }
     248             :   else
     249             :   {
     250        1113 :     M = vec_Q_primpart(liftpol_shallow(M));
     251        1113 :     z = ZabM_indexrank(M, P, n);
     252             :   }
     253        9590 :   return z;
     254             : }
     255             : 
     256             : /*********************************************************************/
     257             : /*                    Simple arithmetic functions                    */
     258             : /*********************************************************************/
     259             : /* TODO: most of these should be exported and used in ifactor1.c */
     260             : /* phi(n) */
     261             : static ulong
     262      142366 : myeulerphiu(ulong n)
     263             : {
     264             :   pari_sp av;
     265      142366 :   if (n == 1) return 1;
     266      127120 :   av = avma; return gc_ulong(av, eulerphiu_fact(myfactoru(n)));
     267             : }
     268             : static long
     269      154658 : mymoebiusu(ulong n)
     270             : {
     271             :   pari_sp av;
     272      154658 :   if (n == 1) return 1;
     273      140168 :   av = avma; return gc_long(av, moebiusu_fact(myfactoru(n)));
     274             : }
     275             : 
     276             : static long
     277        2786 : mynumdivu(long N)
     278             : {
     279             :   pari_sp av;
     280        2786 :   if (N == 1) return 1;
     281        2681 :   av = avma; return gc_long(av, numdivu_fact(myfactoru(N)));
     282             : }
     283             : 
     284             : /* N\prod_{p|N} (1+1/p) */
     285             : static long
     286      337225 : mypsiu(ulong N)
     287             : {
     288      337225 :   pari_sp av = avma;
     289      337225 :   GEN P = gel(myfactoru(N), 1);
     290      337225 :   long j, l = lg(P), res = N;
     291      337225 :   for (j = 1; j < l; j++) res += res/P[j];
     292      337225 :   return gc_long(av,res);
     293             : }
     294             : /* write n = mf^2. Return m, set f. */
     295             : static ulong
     296         210 : mycore(ulong n, long *pf)
     297             : {
     298         210 :   pari_sp av = avma;
     299         210 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     300         210 :   long i, l = lg(P), m = 1, f = 1;
     301         850 :   for (i = 1; i < l; i++)
     302             :   {
     303         640 :     long j, p = P[i], e = E[i];
     304         640 :     if (e & 1) m *= p;
     305         640 :     for (j = 2; j <= e; j+=2) f *= p;
     306             :   }
     307         210 :   *pf = f; return gc_long(av,m);
     308             : }
     309             : 
     310             : /* fa = factorization of -D > 0, return -D0 > 0 (where D0 is fundamental) */
     311             : static long
     312     7903259 : corediscs_fact(GEN fa)
     313             : {
     314     7903259 :   GEN P = gel(fa,1), E = gel(fa,2);
     315     7903259 :   long i, l = lg(P), m = 1;
     316    26177550 :   for (i = 1; i < l; i++)
     317             :   {
     318    18274291 :     long p = P[i], e = E[i];
     319    18274291 :     if (e & 1) m *= p;
     320             :   }
     321     7903259 :   if ((m&3L) != 3) m <<= 2;
     322     7903259 :   return m;
     323             : }
     324             : static long
     325        6328 : mubeta(long n)
     326             : {
     327        6328 :   pari_sp av = avma;
     328        6328 :   GEN E = gel(myfactoru(n), 2);
     329        6328 :   long i, s = 1, l = lg(E);
     330       13118 :   for (i = 1; i < l; i++)
     331             :   {
     332        6790 :     long e = E[i];
     333        6790 :     if (e >= 3) return gc_long(av,0);
     334        6790 :     if (e == 1) s *= -2;
     335             :   }
     336        6328 :   return gc_long(av,s);
     337             : }
     338             : 
     339             : /* n = n1*n2, n1 = ppo(n, m); return mubeta(n1)*moebiusu(n2).
     340             :  * N.B. If n from newt_params we, in fact, never return 0 */
     341             : static long
     342     5957427 : mubeta2(long n, long m)
     343             : {
     344     5957427 :   pari_sp av = avma;
     345     5957427 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     346     5957427 :   long i, s = 1, l = lg(P);
     347    11928833 :   for (i = 1; i < l; i++)
     348             :   {
     349     5971406 :     long p = P[i], e = E[i];
     350     5971406 :     if (m % p)
     351             :     { /* p^e in n1 */
     352     4876753 :       if (e >= 3) return gc_long(av,0);
     353     4876753 :       if (e == 1) s *= -2;
     354             :     }
     355             :     else
     356             :     { /* in n2 */
     357     1094653 :       if (e >= 2) return gc_long(av,0);
     358     1094653 :       s = -s;
     359             :     }
     360             :   }
     361     5957427 :   return gc_long(av,s);
     362             : }
     363             : 
     364             : /* write N = prod p^{ep} and n = df^2, d squarefree.
     365             :  * set g  = ppo(gcd(sqfpart(N), f), FC)
     366             :  *     N2 = prod p^if(e==1 || p|n, ep-1, ep-2) */
     367             : static void
     368     1434692 : newt_params(long N, long n, long FC, long *pg, long *pN2)
     369             : {
     370     1434692 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     371     1434692 :   long i, g = 1, N2 = 1, l = lg(P);
     372     3799670 :   for (i = 1; i < l; i++)
     373             :   {
     374     2364978 :     long p = P[i], e = E[i];
     375     2364978 :     if (e == 1)
     376     2034375 :     { if (FC % p && n % (p*p) == 0) g *= p; }
     377             :     else
     378      330603 :       N2 *= upowuu(p,(n % p)? e-2: e-1);
     379             :   }
     380     1434692 :   *pg = g; *pN2 = N2;
     381     1434692 : }
     382             : /* simplified version of newt_params for n = 1 (newdim) */
     383             : static void
     384       36547 : newd_params(long N, long *pN2)
     385             : {
     386       36547 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     387       36547 :   long i, N2 = 1, l = lg(P);
     388       93114 :   for (i = 1; i < l; i++)
     389             :   {
     390       56567 :     long p = P[i], e = E[i];
     391       56567 :     if (e > 2) N2 *= upowuu(p, e-2);
     392             :   }
     393       36547 :   *pN2 = N2;
     394       36547 : }
     395             : 
     396             : static long
     397          21 : newd_params2(long N)
     398             : {
     399          21 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     400          21 :   long i, N2 = 1, l = lg(P);
     401          56 :   for (i = 1; i < l; i++)
     402             :   {
     403          35 :     long p = P[i], e = E[i];
     404          35 :     if (e >= 2) N2 *= upowuu(p, e);
     405             :   }
     406          21 :   return N2;
     407             : }
     408             : 
     409             : /*******************************************************************/
     410             : /*   Relative trace between cyclotomic fields (TODO: export this)  */
     411             : /*******************************************************************/
     412             : /* g>=1; return g * prod_{p | g, (p,q) = 1} (1-1/p) */
     413             : static long
     414       85134 : phipart(long g, long q)
     415             : {
     416       85134 :   if (g > 1)
     417             :   {
     418       33180 :     GEN P = gel(myfactoru(g), 1);
     419       33180 :     long i, l = lg(P);
     420       33180 :     for (i = 1; i < l; i++) { long p = P[i]; if (q % p) g -= g / p; }
     421             :   }
     422       85134 :   return g;
     423             : }
     424             : /* Trace(zeta_N^k) from Q(zeta_N) to Q(\zeta_N), reduced mod t^m-1.
     425             :  * With k > 0, N = M*d and N, M != 2 mod 4 and m = M or 2M. */
     426             : static GEN
     427      174125 : tracerelz(long d, long m, long M, long k, long vt)
     428             : {
     429             :   long s, v, g, q, muq;
     430      174125 :   if (d == 1)
     431             :   {
     432       19558 :     v = k; if (m != M) v *= 2;
     433       19558 :     return mygmodulo_lift(v, m, gen_1, vt);
     434             :   }
     435      154567 :   g = ugcd(k, d); q = d / g; muq = mymoebiusu(q);
     436      154567 :   if (!muq) return gen_0;
     437      100534 :   if (M == 1)
     438             :   {
     439       35686 :     s = phipart(g, q); if (muq < 0) s = -s;
     440       35686 :     return stoi(s);
     441             :   }
     442       64848 :   if (ugcd(q, M) > 1) return gen_0;
     443       49448 :   s = phipart(g, M*q); if (muq < 0) s = -s;
     444       49448 :   v = Fl_inv(q % M, M);
     445       49448 :   v = (v*(k/g)) % M; /* Tr = s * zeta_M^v */
     446       49448 :   if (m != M) v *= 2;/* Tr = s * zeta_m^v */
     447       49448 :   return mygmodulo_lift(v, m, stoi(s), vt);
     448             : }
     449             : /* Pn = polcyclo(n) */
     450             : GEN
     451       35518 : Qab_trace_init(GEN Pn, long n, long m)
     452             : {
     453             :   GEN T, Pm;
     454             :   long a, i, d, vt, N, M;
     455       35518 :   if (m == n) return mkvec(Pn);
     456       26789 :   d = degpol(Pn);
     457       26789 :   vt = varn(Pn);
     458       26789 :   Pm = polcyclo(m, vt);
     459       26789 :   T = cgetg(d+1, t_VEC);
     460       26789 :   gel(T,1) = utoipos(d / degpol(Pm)); /* Tr 1 */
     461             :   /* if (N != n) zeta_N = zeta_n^2 and zeta_n = - zeta_N^{(N+1)/2} */
     462       26789 :   N = ((n & 3) == 2)? n >> 1: n;
     463       26789 :   M = ((m & 3) == 2)? m >> 1: m;
     464       26789 :   a = N / M;
     465      200914 :   for (i = 1; i < d; i++)
     466             :   { /* if n = 2N, zeta_n^i = (-1)^i zeta_N^k */
     467      174125 :     long k = (N == n)? i: ((odd(i)? i + N: i) >> 1);
     468      174125 :     GEN t = tracerelz(a, m, M, k, vt);
     469      174125 :     if (N != n && odd(i)) t = gneg(t);
     470      174125 :     gel(T,i+1) = t;
     471             :   }
     472       26789 :   return mkvec3(Pm, Pn, T);
     473             : }
     474             : /* x a t_POL modulo Phi_n */
     475             : static GEN
     476       61068 : tracerel_i(GEN T, GEN x)
     477             : {
     478       61068 :   long k, l = lg(x);
     479       61068 :   GEN S = gen_0;
     480       61068 :   for (k = 2; k < l; k++) S = gadd(S, gmul(gel(T,k-1), gel(x,k)));
     481       61068 :   return S;
     482             : }
     483             : /* v = Qab_trace_init(n,m); x is a t_VEC of polmodulo Phi_n
     484             :  * Tr_{Q(zeta_n)/Q(zeta_m)} (zeta_n^t * x) */
     485             : GEN
     486        5915 : QabV_tracerel(GEN v, long t, GEN x)
     487             : {
     488             :   long l, j, degrel;
     489             :   GEN y, z, Pm, Pn, T;
     490        5915 :   if (lg(v) != 4) return x;
     491        5915 :   y = cgetg_copy(x, &l);
     492        5915 :   Pm = gel(v,1);
     493        5915 :   Pn = gel(v,2);
     494        5915 :   T  = gel(v,3);
     495        5915 :   degrel = degpol(Pn) / degpol(Pm);
     496        5915 :   z = RgX_rem(pol_xn(t, varn(Pn)), Pn);
     497      148701 :   for (j = 1; j < l; j++)
     498             :   {
     499      142786 :     GEN a = liftpol_shallow(gel(x,j));
     500      142786 :     a = simplify_shallow( gmul(a, z) );
     501      142786 :     if (typ(a) == t_POL)
     502             :     {
     503       61068 :       a = gdivgs(tracerel_i(T, RgX_rem(a, Pn)), degrel);
     504       61068 :       if (typ(a) == t_POL) a = RgX_rem(a, Pm);
     505             :     }
     506      142786 :     gel(y,j) = a;
     507             :   }
     508        5915 :   return y;
     509             : }
     510             : GEN
     511         126 : QabM_tracerel(GEN v, long t, GEN x)
     512             : {
     513             :   long j, l;
     514             :   GEN y;
     515         126 :   if (lg(v) != 4) return x;
     516         126 :   y = cgetg_copy(x, &l);
     517         126 :   for (j = 1; j < l; j++) gel(y,j) = QabV_tracerel(v, t, gel(x,j));
     518         126 :   return y;
     519             : }
     520             : 
     521             : /*              Operations on Dirichlet characters                       */
     522             : 
     523             : /* A Dirichlet character can be given in GP in different formats, but in this
     524             :  * package, it will be a vector CHI=[G,chi,ord], where G is the (Z/MZ)^* to
     525             :  * which the character belongs, chi is the character in Conrey format, ord is
     526             :  * the order */
     527             : 
     528             : static GEN
     529     1398019 : gmfcharorder(GEN CHI) { return gel(CHI, 3); }
     530             : long
     531     1367072 : mfcharorder(GEN CHI) { return itou(gmfcharorder(CHI)); }
     532             : static long
     533        8463 : mfcharistrivial(GEN CHI) { return !CHI || mfcharorder(CHI) == 1; }
     534             : static GEN
     535      736526 : gmfcharmodulus(GEN CHI) { return gmael3(CHI, 1, 1, 1); }
     536             : long
     537      736526 : mfcharmodulus(GEN CHI) { return itou(gmfcharmodulus(CHI)); }
     538             : GEN
     539      251111 : mfcharpol(GEN CHI) { return gel(CHI,4); }
     540             : 
     541             : /* t^k mod polcyclo(ord), ord = order(CHI) > 1 */
     542             : static GEN
     543        5999 : mygmodulo(GEN CHI, long k)
     544             : {
     545             :   GEN Pn;
     546             :   long ord;
     547        5999 :   if (!k) return gen_1;
     548        5495 :   ord = mfcharorder(CHI);
     549        5495 :   if ((k << 1) == ord) return gen_m1;
     550        4872 :   Pn = mfcharpol(CHI);
     551        4872 :   return gmodulo(monomial(gen_1, k, varn(Pn)), Pn);
     552             : }
     553             : /* C*zeta_ord^k */
     554             : static GEN
     555      896938 : mygmodulo_lift(long k, long ord, GEN C, long vt)
     556             : {
     557      896938 :   if (!k) return C;
     558      520268 :   if ((k << 1) == ord) return gneg(C);
     559      378924 :   return monomial(C, k, vt);
     560             : }
     561             : /* vz[i+1] = image of (zeta_ord)^i in Fp */
     562             : static ulong
     563      171437 : mygmodulo_Fl(long k, GEN vz, ulong C, ulong p)
     564             : {
     565             :   long ord;
     566      171437 :   if (!k) return C;
     567      111069 :   ord = lg(vz)-2;
     568      111069 :   if ((k << 1) == ord) return Fl_neg(C,p);
     569       89446 :   return Fl_mul(C, vz[k+1], p);
     570             : }
     571             : 
     572             : static long
     573      700532 : znchareval_i(GEN CHI, long n, GEN ord)
     574      700532 : { return itos(znchareval(gel(CHI,1), gel(CHI,2), stoi(n), ord)); }
     575             : 
     576             : /* G a znstar, L a Conrey log: return a 'mfchar' */
     577             : static GEN
     578      368382 : mfcharGL(GEN G, GEN L)
     579             : {
     580      368382 :   GEN o = zncharorder(G,L);
     581      368382 :   long ord = itou(o), vt = fetch_user_var("t");
     582      368382 :   return mkvec4(G, L, o, polcyclo(ord,vt));
     583             : }
     584             : static GEN
     585        4081 : mfchartrivial()
     586        4081 : { return mfcharGL(znstar0(gen_1,1), cgetg(1,t_COL)); }
     587             : /* convert a generic character into an 'mfchar' */
     588             : static GEN
     589        3759 : get_mfchar(GEN CHI)
     590             : {
     591             :   GEN G, L;
     592        3759 :   if (typ(CHI) != t_VEC) CHI = znchar(CHI);
     593             :   else
     594             :   {
     595         833 :     long l = lg(CHI);
     596         833 :     if ((l != 3 && l != 5) || !checkznstar_i(gel(CHI,1)))
     597           7 :       pari_err_TYPE("checkNF [chi]", CHI);
     598         826 :     if (l == 5) return CHI;
     599             :   }
     600        3724 :   G = gel(CHI,1);
     601        3724 :   L = gel(CHI,2); if (typ(L) != t_COL) L = znconreylog(G,L);
     602        3724 :   return mfcharGL(G, L);
     603             : }
     604             : 
     605             : /* parse [N], [N,k], [N,k,CHI]. If 'joker' is set, allow wildcard for CHI */
     606             : static GEN
     607        9016 : checkCHI(GEN NK, long N, int joker)
     608             : {
     609             :   GEN CHI;
     610        9016 :   if (lg(NK) == 3)
     611         602 :     CHI = mfchartrivial();
     612             :   else
     613             :   {
     614             :     long i, l;
     615        8414 :     CHI = gel(NK,3); l = lg(CHI);
     616        8414 :     if (isintzero(CHI) && joker)
     617        4095 :       CHI = NULL; /* all character orbits */
     618        4319 :     else if (isintm1(CHI) && joker > 1)
     619        2373 :       CHI = gen_m1; /* sum over all character orbits */
     620        2079 :     else if ((typ(CHI) == t_VEC &&
     621         189 :              (l == 1 || l != 3 || !checkznstar_i(gel(CHI,1)))) && joker)
     622             :     {
     623         133 :       CHI = shallowtrans(CHI); /* list of characters */
     624         133 :       for (i = 1; i < l; i++) gel(CHI,i) = get_mfchar(gel(CHI,i));
     625             :     }
     626             :     else
     627             :     {
     628        1813 :       CHI = get_mfchar(CHI); /* single char */
     629        1813 :       if (N % mfcharmodulus(CHI)) pari_err_TYPE("checkNF [chi]", NK);
     630             :     }
     631             :   }
     632        9002 :   return CHI;
     633             : }
     634             : /* support half-integral weight */
     635             : static void
     636        9023 : checkNK2(GEN NK, long *N, long *nk, long *dk, GEN *CHI, int joker)
     637             : {
     638        9023 :   long l = lg(NK);
     639             :   GEN T;
     640        9023 :   if (typ(NK) != t_VEC || l < 3 || l > 4) pari_err_TYPE("checkNK", NK);
     641        9023 :   T = gel(NK,1); if (typ(T) != t_INT) pari_err_TYPE("checkNF [N]", NK);
     642        9023 :   *N = itos(T); if (*N <= 0) pari_err_TYPE("checkNF [N <= 0]", NK);
     643        9023 :   T = gel(NK,2);
     644        9023 :   switch(typ(T))
     645             :   {
     646        5663 :     case t_INT:  *nk = itos(T); *dk = 1; break;
     647             :     case t_FRAC:
     648        3353 :       *nk = itos(gel(T,1));
     649        3353 :       *dk = itou(gel(T,2)); if (*dk == 2) break;
     650           7 :     default: pari_err_TYPE("checkNF [k]", NK);
     651             :   }
     652        9016 :   *CHI = checkCHI(NK, *N, joker);
     653        9002 : }
     654             : /* don't support half-integral weight */
     655             : static void
     656         126 : checkNK(GEN NK, long *N, long *k, GEN *CHI, int joker)
     657             : {
     658             :   long d;
     659         126 :   checkNK2(NK, N, k, &d, CHI, joker);
     660         126 :   if (d != 1) pari_err_TYPE("checkNF [k]", NK);
     661         126 : }
     662             : 
     663             : static GEN
     664        4851 : mfchargalois(long N, int odd, GEN flagorder)
     665             : {
     666        4851 :   GEN G = znstar0(utoi(N), 1), L = chargalois(G, flagorder);
     667        4851 :   long l = lg(L), i, j;
     668      112735 :   for (i = j = 1; i < l; i++)
     669             :   {
     670      107884 :     GEN chi = znconreyfromchar(G, gel(L,i));
     671      107884 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
     672             :   }
     673        4851 :   setlg(L, j); return L;
     674             : }
     675             : /* possible characters for non-trivial S_1(N, chi) */
     676             : static GEN
     677        1708 : mfwt1chars(long N, GEN vCHI)
     678             : {
     679        1708 :   if (vCHI) return vCHI; /*do not filter, user knows best*/
     680             :   /* Tate's theorem */
     681        1638 :   return mfchargalois(N, 1, uisprime(N)? mkvecsmall2(2,4): NULL);
     682             : }
     683             : static GEN
     684        3255 : mfchars(long N, long k, long dk, GEN vCHI)
     685        3255 : { return vCHI? vCHI: mfchargalois(N, (dk == 2)? 0: (k & 1), NULL); }
     686             : 
     687             : /* wrappers from mfchar to znchar */
     688             : static long
     689       65310 : mfcharparity(GEN CHI)
     690             : {
     691       65310 :   if (!CHI) return 1;
     692       65310 :   return zncharisodd(gel(CHI,1), gel(CHI,2)) ? -1 : 1;
     693             : }
     694             : /* if CHI is primitive, return CHI itself, not a copy */
     695             : static GEN
     696       67963 : mfchartoprimitive(GEN CHI, long *pF)
     697             : {
     698             :   pari_sp av;
     699             :   GEN chi, F;
     700       67963 :   if (!CHI) { if (pF) *pF = 1; return mfchartrivial(); }
     701       67963 :   av = avma; F = znconreyconductor(gel(CHI,1), gel(CHI,2), &chi);
     702       67963 :   if (typ(F) == t_INT) set_avma(av);
     703             :   else
     704             :   {
     705        7357 :     CHI = leafcopy(CHI);
     706        7357 :     gel(CHI,1) = znstar0(F, 1);
     707        7357 :     gel(CHI,2) = chi;
     708             :   }
     709       67963 :   if (pF) *pF = mfcharmodulus(CHI);
     710       67963 :   return CHI;
     711             : }
     712             : static long
     713      392805 : mfcharconductor(GEN CHI)
     714             : {
     715      392805 :   pari_sp av = avma;
     716      392805 :   GEN res = znconreyconductor(gel(CHI,1), gel(CHI,2), NULL);
     717      392805 :   if (typ(res) == t_VEC) res = gel(res, 1);
     718      392805 :   return gc_long(av, itos(res));
     719             : }
     720             : 
     721             : /* n coprime with the modulus of CHI */
     722             : static GEN
     723        9947 : mfchareval_i(GEN CHI, long n)
     724             : {
     725        9947 :   GEN ord = gmfcharorder(CHI);
     726        9947 :   if (equali1(ord)) return gen_1;
     727        5999 :   return mygmodulo(CHI, znchareval_i(CHI, n, ord));
     728             : }
     729             : /* d a multiple of ord(CHI); n coprime with char modulus;
     730             :  * return x s.t. CHI(n) = \zeta_d^x] */
     731             : static long
     732     1254260 : mfcharevalord(GEN CHI, long n, long d)
     733             : {
     734     1254260 :   if (mfcharorder(CHI) == 1) return 0;
     735      690858 :   return znchareval_i(CHI, n, utoi(d));
     736             : }
     737             : 
     738             : /*                      Operations on mf closures                    */
     739             : static GEN
     740       49651 : tagparams(long t, GEN NK) { return mkvec2(mkvecsmall(t), NK); }
     741             : static GEN
     742         952 : lfuntag(long t, GEN x) { return mkvec2(mkvecsmall(t), x); }
     743             : static GEN
     744          49 : tag0(long t, GEN NK) { retmkvec(tagparams(t,NK)); }
     745             : static GEN
     746        8897 : tag(long t, GEN NK, GEN x) { retmkvec2(tagparams(t,NK), x); }
     747             : static GEN
     748       27993 : tag2(long t, GEN NK, GEN x, GEN y) { retmkvec3(tagparams(t,NK), x,y); }
     749             : static GEN
     750       12600 : tag3(long t, GEN NK, GEN x,GEN y,GEN z) { retmkvec4(tagparams(t,NK), x,y,z); }
     751             : static GEN
     752           0 : tag4(long t, GEN NK, GEN x,GEN y,GEN z,GEN a)
     753           0 : { retmkvec5(tagparams(t,NK), x,y,z,a); }
     754             : /* is F a "modular form" ? */
     755             : int
     756       14868 : checkmf_i(GEN F)
     757       14868 : { return typ(F) == t_VEC
     758       14378 :     && lg(F) > 1 && typ(gel(F,1)) == t_VEC
     759       10262 :     && lg(gel(F,1)) == 3
     760       10101 :     && typ(gmael(F,1,1)) == t_VECSMALL
     761       24969 :     && typ(gmael(F,1,2)) == t_VEC; }
     762      168469 : long mf_get_type(GEN F) { return gmael(F,1,1)[1]; }
     763      124124 : GEN mf_get_gN(GEN F) { return gmael3(F,1,2,1); }
     764      101199 : GEN mf_get_gk(GEN F) { return gmael3(F,1,2,2); }
     765             : /* k - 1/2, assume k in 1/2 + Z */
     766         266 : long mf_get_r(GEN F) { return itou(gel(mf_get_gk(F),1)) >> 1; }
     767       89789 : long mf_get_N(GEN F) { return itou(mf_get_gN(F)); }
     768       56938 : long mf_get_k(GEN F)
     769             : {
     770       56938 :   GEN gk = mf_get_gk(F);
     771       56938 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
     772       56938 :   return itou(gk);
     773             : }
     774       37611 : GEN mf_get_CHI(GEN F) { return gmael3(F,1,2,3); }
     775       17668 : GEN mf_get_field(GEN F) { return gmael3(F,1,2,4); }
     776       16373 : GEN mf_get_NK(GEN F) { return gmael(F,1,2); }
     777             : static void
     778         469 : mf_setfield(GEN f, GEN P)
     779             : {
     780         469 :   gel(f,1) = leafcopy(gel(f,1));
     781         469 :   gmael(f,1,2) = leafcopy(gmael(f,1,2));
     782         469 :   gmael3(f,1,2,4) = P;
     783         469 : }
     784             : 
     785             : /* UTILITY FUNCTIONS */
     786             : GEN
     787        4368 : mftocol(GEN F, long lim, long d)
     788        4368 : { GEN c = mfcoefs_i(F, lim, d); settyp(c,t_COL); return c; }
     789             : GEN
     790        1295 : mfvectomat(GEN vF, long lim, long d)
     791             : {
     792        1295 :   long j, l = lg(vF);
     793        1295 :   GEN M = cgetg(l, t_MAT);
     794        1295 :   for (j = 1; j < l; j++) gel(M,j) = mftocol(gel(vF,j), lim, d);
     795        1295 :   return M;
     796             : }
     797             : 
     798             : static GEN
     799        3619 : RgV_to_ser_full(GEN x) { return RgV_to_ser(x, 0, lg(x)+1); }
     800             : /* TODO: delete */
     801             : static GEN
     802         532 : mfcoefsser(GEN F, long n) { return RgV_to_ser_full(mfcoefs_i(F,n,1)); }
     803             : static GEN
     804         777 : sertovecslice(GEN S, long n)
     805             : {
     806         777 :   GEN v = gtovec0(S, -(lg(S) - 2 + valp(S)));
     807         777 :   long l = lg(v), n2 = n + 2;
     808         777 :   if (l < n2) pari_err_BUG("sertovecslice [n too large]");
     809         777 :   return (l == n2)? v: vecslice(v, 1, n2-1);
     810             : }
     811             : 
     812             : /* a, b two RgV of the same length, multiply as truncated power series */
     813             : static GEN
     814        2961 : RgV_mul_RgXn(GEN a, GEN b)
     815             : {
     816        2961 :   long n = lg(a)-1;
     817             :   GEN c;
     818        2961 :   a = RgV_to_RgX(a,0);
     819        2961 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, b, n);
     820        2961 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     821             : }
     822             : /* divide as truncated power series */
     823             : static GEN
     824         259 : RgV_div_RgXn(GEN a, GEN b)
     825             : {
     826         259 :   long n = lg(a)-1;
     827             :   GEN c;
     828         259 :   a = RgV_to_RgX(a,0);
     829         259 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, RgXn_inv(b,n), n);
     830         259 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     831             : }
     832             : /* a^b */
     833             : static GEN
     834          77 : RgV_pows_RgXn(GEN a, long b)
     835             : {
     836          77 :   long n = lg(a)-1;
     837             :   GEN c;
     838          77 :   a = RgV_to_RgX(a,0);
     839          77 :   if (b < 0) { a = RgXn_inv(a, n); b = -b; }
     840          77 :   c = RgXn_powu_i(a,b,n);
     841          77 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     842             : }
     843             : 
     844             : /* assume lg(V) >= n*d + 2 */
     845             : static GEN
     846        6076 : c_deflate(long n, long d, GEN v)
     847             : {
     848        6076 :   long i, id, l = n+2;
     849             :   GEN w;
     850        6076 :   if (d == 1) return lg(v) == l ? v: vecslice(v, 1, l-1);
     851         322 :   w = cgetg(l, typ(v));
     852         322 :   for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
     853         322 :   return w;
     854             : }
     855             : 
     856             : static void
     857          14 : err_cyclo(void)
     858          14 : { pari_err_IMPL("changing cyclotomic fields in mf"); }
     859             : /* Q(zeta_a) = Q(zeta_b) ? */
     860             : static int
     861         560 : same_cyc(long a, long b)
     862         560 : { return (a == b) || (odd(a) && b == (a<<1)) || (odd(b) && a == (b<<1)); }
     863             : /* need to combine elements in Q(CHI1) and Q(CHI2) with result in Q(CHI),
     864             :  * CHI = CHI1 * CHI2 or CHI / CHI2 times some character of order 2 */
     865             : static GEN
     866        1904 : chicompat(GEN CHI, GEN CHI1, GEN CHI2)
     867             : {
     868        1904 :   long o1 = mfcharorder(CHI1);
     869        1904 :   long o2 = mfcharorder(CHI2), O, o;
     870             :   GEN T1, T2, P;
     871        1904 :   if (o1 <= 2 && o2 <= 2) return NULL;
     872         567 :   o = mfcharorder(CHI);
     873         567 :   P = mfcharpol(CHI1);
     874         567 :   if (o1 == o2)
     875             :   {
     876          21 :     if (o1 == o) return NULL;
     877          14 :     if (!same_cyc(o1,o)) err_cyclo();
     878           0 :     return mkvec4(P, gen_1,gen_1, Qab_trace_init(P, o1, o));
     879             :   }
     880         546 :   O = ulcm(o1, o2);
     881         546 :   if (!same_cyc(O,o)) err_cyclo();
     882         546 :   if (O != o1) P = polcyclo(O, varn(P));
     883         546 :   T1 = o1 <= 2? gen_1: utoipos(O / o1);
     884         546 :   T2 = o2 <= 2? gen_1: utoipos(O / o2);
     885         546 :   return mkvec4(P, T1, T2, O == o? gen_1: Qab_trace_init(P, O, o));
     886             : }
     887             : /* *F a vector of cyclotomic numbers */
     888             : static void
     889           7 : compatlift(GEN *F, long o, GEN P)
     890             : {
     891             :   long i, l;
     892           7 :   GEN f = *F, g = cgetg_copy(f,&l);
     893          56 :   for (i = 1; i < l; i++)
     894          49 :     gel(g,i) = gmodulo(RgX_inflate(lift_shallow(gel(f,i)), o), P);
     895           7 :   *F = g;
     896           7 : }
     897             : static void
     898         644 : chicompatlift(GEN T, GEN *F, GEN *G)
     899             : {
     900         644 :   long o1 = itou(gel(T,2)), o2 = itou(gel(T,3));
     901         644 :   GEN P = gel(T,1);
     902         644 :   if (o1 != 1) compatlift(F, o1, P);
     903         644 :   if (o2 != 1 && G) compatlift(G, o2, P);
     904         644 : }
     905             : static GEN
     906         644 : chicompatfix(GEN T, GEN F)
     907             : {
     908         644 :   GEN V = gel(T,4);
     909         644 :   if (typ(V) == t_VEC) F = gmodulo(QabV_tracerel(V, 0, F), gel(V,1));
     910         644 :   return F;
     911             : }
     912             : 
     913             : static GEN
     914         525 : c_mul(long n, long d, GEN S)
     915             : {
     916         525 :   pari_sp av = avma;
     917         525 :   long nd = n*d;
     918         525 :   GEN F = gel(S,2), G = gel(S,3);
     919         525 :   F = mfcoefs_i(F, nd, 1);
     920         525 :   G = mfcoefs_i(G, nd, 1);
     921         525 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F,&G);
     922         525 :   F = c_deflate(n, d, RgV_mul_RgXn(F,G));
     923         525 :   if (lg(S) == 5) F = chicompatfix(gel(S,4), F);
     924         525 :   return gerepilecopy(av, F);
     925             : }
     926             : static GEN
     927          77 : c_pow(long n, long d, GEN S)
     928             : {
     929          77 :   pari_sp av = avma;
     930          77 :   long nd = n*d;
     931          77 :   GEN F = gel(S,2), a = gel(S,3), f = mfcoefs_i(F,nd,1);
     932          77 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F, NULL);
     933          77 :   f = RgV_pows_RgXn(f, itos(a));
     934          77 :   f = c_deflate(n, d, f);
     935          77 :   if (lg(S) == 5) f = chicompatfix(gel(S,4), f);
     936          77 :   return gerepilecopy(av, f);
     937             : }
     938             : 
     939             : /* F * Theta */
     940             : static GEN
     941         336 : mfmultheta(GEN F)
     942             : {
     943         336 :   if (typ(mf_get_gk(F)) == t_FRAC && mf_get_type(F) == t_MF_DIV)
     944             :   {
     945         112 :     GEN T = gel(F,3); /* hopefully mfTheta() */
     946         112 :     if (mf_get_type(T) == t_MF_THETA && mf_get_N(T) == 4) return gel(F,2);
     947             :   }
     948         224 :   return mfmul(F, mfTheta(NULL));
     949             : }
     950             : 
     951             : static GEN
     952          21 : c_bracket(long n, long d, GEN S)
     953             : {
     954          21 :   pari_sp av = avma;
     955          21 :   long i, nd = n*d;
     956          21 :   GEN F = gel(S,2), G = gel(S,3);
     957          21 :   GEN VF = mfcoefs_i(F, nd, 1), tF = cgetg(nd+2, t_VEC);
     958          21 :   GEN VG = mfcoefs_i(G, nd, 1), tG = cgetg(nd+2, t_VEC);
     959          21 :   GEN C, mpow, res = NULL, gk = mf_get_gk(F), gl = mf_get_gk(G);
     960          21 :   ulong j, m = itou(gel(S,4));
     961             :   /* pow[i,j+1] = i^j */
     962          21 :   if (lg(S) == 6) chicompatlift(gel(S,5),&VF,&VG);
     963          21 :   mpow = cgetg(m+2, t_MAT);
     964          21 :   gel(mpow,1) = const_col(nd, gen_1);
     965          49 :   for (j = 1; j <= m; j++)
     966             :   {
     967          28 :     GEN c = cgetg(nd+1, t_COL);
     968          28 :     gel(mpow,j+1) = c;
     969          28 :     for (i = 1; i <= nd; i++) gel(c,i) = muliu(gcoeff(mpow,i,j), i);
     970             :   }
     971          21 :   C = binomial(gaddgs(gk, m-1), m);
     972          21 :   if (odd(m)) C = gneg(C);
     973          70 :   for (j = 0; j <= m; j++)
     974             :   { /* C = (-1)^(m-j) binom(m+l-1, j) binom(m+k-1,m-j) */
     975             :     GEN c;
     976          49 :     gel(tF,1) = j == 0? gel(VF,1): gen_0;
     977          49 :     gel(tG,1) = j == m? gel(VG,1): gen_0;
     978          49 :     gel(tF,2) = gel(VF,2);
     979          49 :     gel(tG,2) = gel(VG,2);
     980         413 :     for (i = 2; i <= nd; i++)
     981             :     {
     982         364 :       gel(tF, i+1) = gmul(gcoeff(mpow,i,j+1),   gel(VF, i+1));
     983         364 :       gel(tG, i+1) = gmul(gcoeff(mpow,i,m-j+1), gel(VG, i+1));
     984             :     }
     985          49 :     c = gmul(C, c_deflate(n, d, RgV_mul_RgXn(tF, tG)));
     986          49 :     res = res? gadd(res, c): c;
     987          49 :     if (j < m)
     988          56 :       C = gdiv(gmul(C, gmulsg(m-j, gaddgs(gl,m-j-1))),
     989          28 :                gmulsg(-(j+1), gaddgs(gk,j)));
     990             :   }
     991          21 :   if (lg(S) == 6) res = chicompatfix(gel(S,5), res);
     992          21 :   return gerepileupto(av, res);
     993             : }
     994             : /* linear combination \sum L[j] vecF[j] */
     995             : static GEN
     996        2387 : c_linear(long n, long d, GEN F, GEN L, GEN dL)
     997             : {
     998        2387 :   pari_sp av = avma;
     999        2387 :   long j, l = lg(L);
    1000        2387 :   GEN S = NULL;
    1001        7546 :   for (j = 1; j < l; j++)
    1002             :   {
    1003        5159 :     GEN c = gel(L,j);
    1004        5159 :     if (gequal0(c)) continue;
    1005        4578 :     c = gmul(c, mfcoefs_i(gel(F,j), n, d));
    1006        4578 :     S = S? gadd(S,c): c;
    1007             :   }
    1008        2387 :   if (!S) return zerovec(n+1);
    1009        2387 :   if (!is_pm1(dL)) S = gdiv(S, dL);
    1010        2387 :   return gerepileupto(av, S);
    1011             : }
    1012             : 
    1013             : /* B_d(T_j Trace^new) as t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)) or
    1014             :  * t_MF_HECKE(t_MF_NEWTRACE)
    1015             :  * or t_MF_NEWTRACE in level N. Set d and j, return t_MF_NEWTRACE component*/
    1016             : static GEN
    1017       67753 : bhn_parse(GEN f, long *d, long *j)
    1018             : {
    1019       67753 :   long t = mf_get_type(f);
    1020       67753 :   *d = *j = 1;
    1021       67753 :   if (t == t_MF_BD) { *d = itos(gel(f,3)); f = gel(f,2); t = mf_get_type(f); }
    1022       67753 :   if (t == t_MF_HECKE) { *j = gel(f,2)[1]; f = gel(f,3); }
    1023       67753 :   return f;
    1024             : }
    1025             : /* f as above, return the t_MF_NEWTRACE component */
    1026             : static GEN
    1027       21231 : bhn_newtrace(GEN f)
    1028             : {
    1029       21231 :   long t = mf_get_type(f);
    1030       21231 :   if (t == t_MF_BD) { f = gel(f,2); t = mf_get_type(f); }
    1031       21231 :   if (t == t_MF_HECKE) f = gel(f,3);
    1032       21231 :   return f;
    1033             : }
    1034             : static int
    1035        2807 : ok_bhn_linear(GEN vf)
    1036             : {
    1037        2807 :   long i, N0 = 0, l = lg(vf);
    1038             :   GEN CHI, gk;
    1039        2807 :   if (l == 1) return 1;
    1040        2807 :   gk = mf_get_gk(gel(vf,1));
    1041        2807 :   CHI = mf_get_CHI(gel(vf,1));
    1042       16702 :   for (i = 1; i < l; i++)
    1043             :   {
    1044       15365 :     GEN f = bhn_newtrace(gel(vf,i));
    1045       15365 :     long N = mf_get_N(f);
    1046       15365 :     if (mf_get_type(f) != t_MF_NEWTRACE) return 0;
    1047       13895 :     if (N < N0) return 0; /* largest level must come last */
    1048       13895 :     N0 = N;
    1049       13895 :     if (!gequal(gk,mf_get_gk(f))) return 0; /* same k */
    1050       13895 :     if (!gequal(gel(mf_get_CHI(f),2), gel(CHI,2))) return 0; /* same CHI */
    1051             :   }
    1052        1337 :   return 1;
    1053             : }
    1054             : 
    1055             : /* vF not empty, same hypotheses as bhnmat_extend */
    1056             : static GEN
    1057        5943 : bhnmat_extend_nocache(GEN M, long N, long n, long d, GEN vF)
    1058             : {
    1059             :   cachenew_t cache;
    1060        5943 :   long l = lg(vF);
    1061             :   GEN f;
    1062        5943 :   if (l == 1) return M? M: cgetg(1, t_MAT);
    1063        5866 :   f = bhn_newtrace(gel(vF,1)); /* N.B. mf_get_N(f) divides N */
    1064        5866 :   init_cachenew(&cache, n*d, N, f);
    1065        5866 :   M = bhnmat_extend(M, n, d, vF, &cache);
    1066        5866 :   dbg_cachenew(&cache); return M;
    1067             : }
    1068             : /* c_linear of "bhn" mf closures, same hypotheses as bhnmat_extend */
    1069             : static GEN
    1070        1561 : c_linear_bhn(long n, long d, GEN F)
    1071             : {
    1072             :   pari_sp av;
    1073        1561 :   GEN M, v, vF = gel(F,2), L = gel(F,3), dL = gel(F,4);
    1074        1561 :   if (lg(L) == 1) return zerovec(n+1);
    1075        1561 :   av = avma;
    1076        1561 :   M = bhnmat_extend_nocache(NULL, mf_get_N(F), n, d, vF);
    1077        1561 :   v = RgM_RgC_mul(M,L); settyp(v, t_VEC);
    1078        1561 :   if (!is_pm1(dL)) v = gdiv(v, dL);
    1079        1561 :   return gerepileupto(av, v);
    1080             : }
    1081             : 
    1082             : /* c in K, K := Q[X]/(T) vz = vector of consecutive powers of root z of T
    1083             :  * attached to an embedding s: K -> C. Return s(c) in C */
    1084             : static GEN
    1085       74235 : Rg_embed1(GEN c, GEN vz)
    1086             : {
    1087       74235 :   long t = typ(c);
    1088       74235 :   if (t == t_POLMOD) { c = gel(c,2); t = typ(c); }
    1089       74235 :   if (t == t_POL) c = RgX_RgV_eval(c, vz);
    1090       74235 :   return c;
    1091             : }
    1092             : /* return s(P) in C[X] */
    1093             : static GEN
    1094         882 : RgX_embed1(GEN P, GEN vz)
    1095             : {
    1096             :   long i, l;
    1097         882 :   GEN Q = cgetg_copy(P, &l);
    1098         882 :   Q[1] = P[1];
    1099         882 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1100         882 :   return normalizepol_lg(Q,l); /* normally a no-op */
    1101             : }
    1102             : /* return s(P) in C^n */
    1103             : static GEN
    1104         567 : vecembed1(GEN P, GEN vz)
    1105             : {
    1106             :   long i, l;
    1107         567 :   GEN Q = cgetg_copy(P, &l);
    1108         567 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1109         567 :   return Q;
    1110             : }
    1111             : /* P in L = K[X]/(U), K = Q[t]/T; s an embedding of K -> C attached
    1112             :  * to a root of T, extended to an embedding of L -> C attached to a root
    1113             :  * of s(U); vT powers of the root of T, vU powers of the root of s(U).
    1114             :  * Return s(P) in C^n */
    1115             : static GEN
    1116       13314 : Rg_embed2(GEN P, long vt, GEN vT, GEN vU)
    1117             : {
    1118             :   long i, l;
    1119             :   GEN Q;
    1120       13314 :   P = liftpol_shallow(P);
    1121       13314 :   if (typ(P) != t_POL) return P;
    1122       13300 :   if (varn(P) == vt) return Rg_embed1(P, vT);
    1123             :   /* varn(P) == vx */
    1124       13293 :   Q = cgetg_copy(P, &l); Q[1] = P[1];
    1125       13293 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vT);
    1126       13293 :   return Rg_embed1(Q, vU);
    1127             : }
    1128             : static GEN
    1129          42 : vecembed2(GEN P, long vt, GEN vT, GEN vU)
    1130             : {
    1131             :   long i, l;
    1132          42 :   GEN Q = cgetg_copy(P, &l);
    1133          42 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1134          42 :   return Q;
    1135             : }
    1136             : static GEN
    1137         532 : RgX_embed2(GEN P, long vt, GEN vT, GEN vU)
    1138             : {
    1139             :   long i, l;
    1140         532 :   GEN Q = cgetg_copy(P, &l);
    1141         532 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1142         532 :   Q[1] = P[1]; return normalizepol_lg(Q,l);
    1143             : }
    1144             : /* embed polynomial f in variable vx [ may be a scalar ], E from getembed */
    1145             : static GEN
    1146        1596 : RgX_embed(GEN f, long vx, GEN E)
    1147             : {
    1148             :   GEN vT;
    1149        1596 :   if (typ(f) != t_POL || varn(f) != vx) return mfembed(E, f);
    1150        1575 :   if (lg(E) == 1) return f;
    1151        1379 :   vT = gel(E,2);
    1152        1379 :   if (lg(E) == 3)
    1153         847 :     f = RgX_embed1(f, vT);
    1154             :   else
    1155         532 :     f = RgX_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1156        1379 :   return f;
    1157             : }
    1158             : /* embed vector, E from getembed */
    1159             : GEN
    1160        1407 : mfvecembed(GEN E, GEN v)
    1161             : {
    1162             :   GEN vT;
    1163        1407 :   if (lg(E) == 1) return v;
    1164         609 :   vT = gel(E,2);
    1165         609 :   if (lg(E) == 3)
    1166         567 :     v = vecembed1(v, vT);
    1167             :   else
    1168          42 :     v = vecembed2(v, varn(gel(E,1)), vT, gel(E,3));
    1169         609 :   return v;
    1170             : }
    1171             : GEN
    1172           7 : mfmatembed(GEN E, GEN f)
    1173             : {
    1174             :   long i, l;
    1175             :   GEN g;
    1176           7 :   if (lg(E) == 1) return f;
    1177           7 :   g = cgetg_copy(f, &l);
    1178           7 :   for (i = 1; i < l; i++) gel(g,i) = mfvecembed(E, gel(f,i));
    1179           7 :   return g;
    1180             : }
    1181             : /* embed vector of polynomials in var vx */
    1182             : static GEN
    1183          98 : RgXV_embed(GEN f, long vx, GEN E)
    1184             : {
    1185             :   long i, l;
    1186             :   GEN v;
    1187          98 :   if (lg(E) == 1) return f;
    1188          70 :   v = cgetg_copy(f, &l);
    1189          70 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(gel(f,i), vx, E);
    1190          70 :   return v;
    1191             : }
    1192             : 
    1193             : /* embed scalar */
    1194             : GEN
    1195       95711 : mfembed(GEN E, GEN f)
    1196             : {
    1197             :   GEN vT;
    1198       95711 :   if (lg(E) == 1) return f;
    1199       13468 :   vT = gel(E,2);
    1200       13468 :   if (lg(E) == 3)
    1201        4354 :     f = Rg_embed1(f, vT);
    1202             :   else
    1203        9114 :     f = Rg_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1204       13468 :   return f;
    1205             : }
    1206             : /* vector of the sigma(f), sigma in vE */
    1207             : static GEN
    1208         294 : RgX_embedall(GEN f, long vx, GEN vE)
    1209             : {
    1210         294 :   long i, l = lg(vE);
    1211         294 :   GEN v = cgetg(l, t_VEC);
    1212         294 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(f, vx, gel(vE,i));
    1213         294 :   return l == 2? gel(v,1): v;
    1214             : }
    1215             : /* matrix whose colums are the sigma(v), sigma in vE */
    1216             : static GEN
    1217         329 : RgC_embedall(GEN v, GEN vE)
    1218             : {
    1219         329 :   long j, l = lg(vE);
    1220         329 :   GEN M = cgetg(l, t_MAT);
    1221         329 :   for (j = 1; j < l; j++) gel(M,j) = mfvecembed(gel(vE,j), v);
    1222         329 :   return M;
    1223             : }
    1224             : /* vector of the sigma(v), sigma in vE */
    1225             : static GEN
    1226        4907 : Rg_embedall_i(GEN v, GEN vE)
    1227             : {
    1228        4907 :   long j, l = lg(vE);
    1229        4907 :   GEN M = cgetg(l, t_VEC);
    1230        4907 :   for (j = 1; j < l; j++) gel(M,j) = mfembed(gel(vE,j), v);
    1231        4907 :   return M;
    1232             : }
    1233             : /* vector of the sigma(v), sigma in vE; if #vE == 1, return v */
    1234             : static GEN
    1235       90230 : Rg_embedall(GEN v, GEN vE)
    1236       90230 : { return (lg(vE) == 2)? mfembed(gel(vE,1), v): Rg_embedall_i(v, vE); }
    1237             : 
    1238             : static GEN
    1239         777 : c_div_i(long n, GEN S)
    1240             : {
    1241         777 :   GEN F = gel(S,2), G = gel(S,3);
    1242             :   GEN a0, a0i, H;
    1243         777 :   F = mfcoefs_i(F, n, 1);
    1244         777 :   G = mfcoefs_i(G, n, 1);
    1245         777 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F,&G);
    1246         777 :   F = RgV_to_ser_full(F);
    1247         777 :   G = RgV_to_ser_full(G);
    1248         777 :   a0 = polcoef_i(G, 0, -1); /* != 0 */
    1249         777 :   if (gequal1(a0)) a0 = a0i = NULL;
    1250             :   else
    1251             :   {
    1252         602 :     a0i = ginv(a0);
    1253         602 :     G = gmul(ser_unscale(G,a0), a0i);
    1254         602 :     F = gmul(ser_unscale(F,a0), a0i);
    1255             :   }
    1256         777 :   H = gdiv(F, G);
    1257         777 :   if (a0) H = ser_unscale(H,a0i);
    1258         777 :   H = sertovecslice(H, n);
    1259         777 :   if (lg(S) == 5) H = chicompatfix(gel(S,4), H);
    1260         777 :   return H;
    1261             : }
    1262             : static GEN
    1263         777 : c_div(long n, long d, GEN S)
    1264             : {
    1265         777 :   pari_sp av = avma;
    1266         777 :   GEN D = (d==1)? c_div_i(n, S): c_deflate(n, d, c_div_i(n*d, S));
    1267         777 :   return gerepilecopy(av, D);
    1268             : }
    1269             : 
    1270             : static GEN
    1271          35 : c_shift(long n, long d, GEN F, GEN gsh)
    1272             : {
    1273          35 :   pari_sp av = avma;
    1274             :   GEN vF;
    1275          35 :   long sh = itos(gsh), n1 = n*d + sh;
    1276          35 :   if (n1 < 0) return zerovec(n+1);
    1277          35 :   vF = mfcoefs_i(F, n1, 1);
    1278          35 :   if (sh < 0) vF = shallowconcat(zerovec(-sh), vF);
    1279          35 :   else vF = vecslice(vF, sh+1, n1+1);
    1280          35 :   return gerepilecopy(av, c_deflate(n, d, vF));
    1281             : }
    1282             : 
    1283             : static GEN
    1284          21 : c_deriv(long n, long d, GEN F, GEN gm)
    1285             : {
    1286          21 :   pari_sp av = avma;
    1287          21 :   GEN V = mfcoefs_i(F, n, d), res;
    1288          21 :   long i, m = itos(gm);
    1289          21 :   if (!m) return V;
    1290          21 :   res = cgetg(n+2, t_VEC); gel(res,1) = gen_0;
    1291          21 :   if (m < 0)
    1292           7 :   { for (i=1; i <= n; i++) gel(res, i+1) = gdiv(gel(V, i+1), powuu(i,-m)); }
    1293             :   else
    1294          14 :   { for (i=1; i <= n; i++) gel(res, i+1) = gmul(gel(V,i+1), powuu(i,m)); }
    1295          21 :   return gerepileupto(av, res);
    1296             : }
    1297             : 
    1298             : static GEN
    1299          14 : c_derivE2(long n, long d, GEN F, GEN gm)
    1300             : {
    1301          14 :   pari_sp av = avma;
    1302             :   GEN VF, VE, res, tmp, gk;
    1303          14 :   long i, m = itos(gm), nd;
    1304          14 :   if (m == 0) return mfcoefs_i(F, n, d);
    1305          14 :   nd = n*d;
    1306          14 :   VF = mfcoefs_i(F, nd, 1); VE = mfcoefs_i(mfEk(2), nd, 1);
    1307          14 :   gk = mf_get_gk(F);
    1308          14 :   if (m == 1)
    1309             :   {
    1310           7 :     res = cgetg(n+2, t_VEC);
    1311           7 :     for (i = 0; i <= n; i++) gel(res, i+1) = gmulsg(i, gel(VF, i*d+1));
    1312           7 :     tmp = c_deflate(n, d, RgV_mul_RgXn(VF, VE));
    1313           7 :     return gerepileupto(av, gsub(res, gmul(gdivgs(gk, 12), tmp)));
    1314             :   }
    1315             :   else
    1316             :   {
    1317             :     long j;
    1318          35 :     for (j = 1; j <= m; j++)
    1319             :     {
    1320          28 :       tmp = RgV_mul_RgXn(VF, VE);
    1321          28 :       for (i = 0; i <= nd; i++) gel(VF, i+1) = gmulsg(i, gel(VF, i+1));
    1322          28 :       VF = gsub(VF, gmul(gdivgs(gaddgs(gk, 2*(j-1)), 12), tmp));
    1323             :     }
    1324           7 :     return gerepilecopy(av, c_deflate(n, d, VF));
    1325             :   }
    1326             : }
    1327             : 
    1328             : /* Twist by the character (D/.) */
    1329             : static GEN
    1330           7 : c_twist(long n, long d, GEN F, GEN D)
    1331             : {
    1332           7 :   pari_sp av = avma;
    1333           7 :   GEN V = mfcoefs_i(F, n, d), res = cgetg(n+2, t_VEC);
    1334             :   long i;
    1335         119 :   for (i = 0; i <= n; i++)
    1336         112 :     gel(res, i + 1) = gmulsg(krois(D, i), gel(V, i+1));
    1337           7 :   return gerepileupto(av, res);
    1338             : }
    1339             : 
    1340             : /* form F given by closure, compute T(n)(F) as closure */
    1341             : static GEN
    1342         434 : c_hecke(long m, long l, GEN DATA, GEN F)
    1343             : {
    1344         434 :   pari_sp av = avma;
    1345         434 :   return gerepilecopy(av, hecke_i(m, l, NULL, F, DATA));
    1346             : }
    1347             : static GEN
    1348         140 : c_const(long n, long d, GEN C)
    1349             : {
    1350         140 :   GEN V = zerovec(n+1);
    1351         140 :   long i, j, l = lg(C);
    1352         140 :   if (l > d*n+2) l = d*n+2;
    1353         140 :   for (i = j = 1; i < l; i+=d, j++) gel(V, j) = gcopy(gel(C,i));
    1354         140 :   return V;
    1355             : }
    1356             : 
    1357             : /* m > 0 */
    1358             : static GEN
    1359         455 : eta3_ZXn(long m)
    1360             : {
    1361         455 :   long l = m+2, n, k;
    1362         455 :   GEN P = cgetg(l,t_POL);
    1363         455 :   P[1] = evalsigne(1)|evalvarn(0);
    1364         455 :   for (n = 2; n < l; n++) gel(P,n) = gen_0;
    1365        2471 :   for (n = k = 0;; n++)
    1366             :   {
    1367        4487 :     if (k + n >= m) { setlg(P, k+3); return P; }
    1368        2016 :     k += n;
    1369             :     /* now k = n(n+1) / 2 */
    1370        2016 :     gel(P, k+2) = odd(n)? utoineg(2*n+1): utoipos(2*n+1);
    1371             :   }
    1372             : }
    1373             : 
    1374             : static GEN
    1375         462 : c_delta(long n, long d)
    1376             : {
    1377         462 :   pari_sp ltop = avma;
    1378         462 :   long N = n*d;
    1379             :   GEN e;
    1380         462 :   if (!N) return mkvec(gen_0);
    1381         455 :   e = eta3_ZXn(N);
    1382         455 :   e = ZXn_sqr(e,N);
    1383         455 :   e = ZXn_sqr(e,N);
    1384         455 :   e = ZXn_sqr(e,N); /* eta(x)^24 */
    1385         455 :   settyp(e, t_VEC);
    1386         455 :   gel(e,1) = gen_0; /* Delta(x) = x*eta(x)^24 as a t_VEC */
    1387         455 :   return gerepilecopy(ltop, c_deflate(n, d, e));
    1388             : }
    1389             : 
    1390             : /* return s(d) such that s|f <=> d | f^2 */
    1391             : static long
    1392          42 : mysqrtu(ulong d)
    1393             : {
    1394          42 :   GEN fa = myfactoru(d), P = gel(fa,1), E = gel(fa,2);
    1395          42 :   long l = lg(P), i, s = 1;
    1396          42 :   for (i = 1; i < l; i++) s *= upowuu(P[i], (E[i]+1)>>1);
    1397          42 :   return s;
    1398             : }
    1399             : static GEN
    1400        1225 : c_theta(long n, long d, GEN psi)
    1401             : {
    1402        1225 :   long lim = usqrt(n*d), F = mfcharmodulus(psi), par = mfcharparity(psi);
    1403        1225 :   long f, d2 = d == 1? 1: mysqrtu(d);
    1404        1225 :   GEN V = zerovec(n + 1);
    1405        4879 :   for (f = d2; f <= lim; f += d2)
    1406        3654 :     if (ugcd(F, f) == 1)
    1407             :     {
    1408        3647 :       pari_sp av = avma;
    1409        3647 :       GEN c = mfchareval_i(psi, f);
    1410        3647 :       gel(V, f*f/d + 1) = gerepileupto(av, par < 0 ? gmulgs(c,2*f) : gmul2n(c,1));
    1411             :     }
    1412        1225 :   if (F == 1) gel(V, 1) = gen_1;
    1413        1225 :   return V; /* no gerepile needed */
    1414             : }
    1415             : 
    1416             : static GEN
    1417         133 : c_etaquo(long n, long d, GEN eta, GEN gs)
    1418             : {
    1419         133 :   pari_sp av = avma;
    1420         133 :   long s = itos(gs), nd = n*d, nds = nd - s + 1;
    1421             :   GEN c;
    1422         133 :   if (nds <= 0) return zerovec(n+1);
    1423         112 :   c = RgX_to_RgC(eta_product_ZXn(eta, nds), nds); settyp(c, t_VEC);
    1424         112 :   if (s > 0) c = shallowconcat(zerovec(s), c);
    1425         112 :   return gerepilecopy(av, c_deflate(n, d, c));
    1426             : }
    1427             : 
    1428             : static GEN
    1429          63 : c_ell(long n, long d, GEN E)
    1430             : {
    1431          63 :   pari_sp av = avma;
    1432             :   GEN v;
    1433          63 :   if (d == 1) return concat(gen_0, anell(E, n));
    1434           7 :   v = shallowconcat(gen_0, anell(E, n*d));
    1435           7 :   return gerepilecopy(av, c_deflate(n, d, v));
    1436             : }
    1437             : 
    1438             : static GEN
    1439          21 : c_cusptrace(long n, long d, GEN F)
    1440             : {
    1441          21 :   pari_sp av = avma;
    1442          21 :   GEN D = gel(F,2), res = cgetg(n+2, t_VEC);
    1443          21 :   long i, N = mf_get_N(F), k = mf_get_k(F);
    1444          21 :   gel(res, 1) = gen_0;
    1445         140 :   for (i = 1; i <= n; i++)
    1446         119 :     gel(res, i+1) = mfcusptrace_i(N, k, i*d, mydivisorsu(i*d), D);
    1447          21 :   return gerepilecopy(av, res);
    1448             : }
    1449             : 
    1450             : static GEN
    1451         749 : c_newtrace(long n, long d, GEN F)
    1452             : {
    1453         749 :   pari_sp av = avma;
    1454             :   cachenew_t cache;
    1455         749 :   long N = mf_get_N(F);
    1456             :   GEN v;
    1457         749 :   init_cachenew(&cache, n*d, N, F);
    1458         749 :   v = colnewtrace(0, n, d, N, mf_get_k(F), &cache);
    1459         749 :   settyp(v, t_VEC); return gerepilecopy(av, v);
    1460             : }
    1461             : 
    1462             : static GEN
    1463        3661 : c_Bd(long n, long d, GEN F, GEN A)
    1464             : {
    1465        3661 :   pari_sp av = avma;
    1466        3661 :   long a = itou(A), ad = ugcd(a,d), aad = a/ad, i, j;
    1467        3661 :   GEN w, v = mfcoefs_i(F, n/aad, d/ad);
    1468        3661 :   if (a == 1) return v;
    1469        3661 :   n++; w = zerovec(n);
    1470        3661 :   for (i = j = 1; j <= n; i++, j += aad) gel(w,j) = gcopy(gel(v,i));
    1471        3661 :   return gerepileupto(av, w);
    1472             : }
    1473             : 
    1474             : static GEN
    1475        3591 : c_dihedral(long n, long d, GEN bnr, GEN w, GEN k0j)
    1476             : {
    1477        3591 :   pari_sp av = avma;
    1478        3591 :   GEN V = dihan(bnr, w, k0j, n*d);
    1479        3591 :   GEN Tinit = gel(w,3), Pm = gel(Tinit,1);
    1480        3591 :   GEN A = c_deflate(n, d, V);
    1481        3591 :   if (degpol(Pm) == 1 || RgV_is_ZV(A)) return gerepilecopy(av, A);
    1482         749 :   return gerepileupto(av, gmodulo(A, Pm));
    1483             : }
    1484             : 
    1485             : static GEN
    1486         140 : c_mfEH(long n, long d, GEN F)
    1487             : {
    1488         140 :   pari_sp av = avma;
    1489             :   GEN v, M, A;
    1490         140 :   long i, r = mf_get_r(F);
    1491         140 :   if (n == 1)
    1492          14 :     return gerepilecopy(av, mkvec2(mfEHcoef(r,0),mfEHcoef(r,d)));
    1493             :   /* speedup mfcoef */
    1494         126 :   if (r == 1)
    1495             :   {
    1496          70 :     v = cgetg(n+2, t_VEC);
    1497          70 :     gel(v,1) = sstoQ(-1,12);
    1498       83258 :     for (i = 1; i <= n; i++)
    1499             :     {
    1500       83188 :       long id = i*d, a = id & 3;
    1501       83188 :       gel(v,i+1) = (a==1 || a==2)? gen_0: sstoQ(hclassno6u(id), 6);
    1502             :     }
    1503          70 :     return v; /* no gerepile needed */
    1504             :   }
    1505          56 :   M = mfEHmat(n*d+1,r);
    1506          56 :   if (d > 1)
    1507             :   {
    1508           7 :     long l = lg(M);
    1509           7 :     for (i = 1; i < l; i++) gel(M,i) = c_deflate(n, d, gel(M,i));
    1510             :   }
    1511          56 :   A = gel(F,2); /* [num(B), den(B)] */
    1512          56 :   v = RgC_Rg_div(RgM_RgC_mul(M, gel(A,1)), gel(A,2));
    1513          56 :   settyp(v,t_VEC); return gerepileupto(av, v);
    1514             : }
    1515             : 
    1516             : static GEN
    1517        6685 : c_mfeisen(long n, long d, GEN F)
    1518             : {
    1519        6685 :   pari_sp av = avma;
    1520        6685 :   GEN v, vchi, E0, P, T, CHI, gk = mf_get_gk(F);
    1521             :   long i, k;
    1522        6685 :   if (typ(gk) != t_INT) return c_mfEH(n, d, F);
    1523        6545 :   k = itou(gk);
    1524        6545 :   vchi = gel(F,2);
    1525        6545 :   E0 = gel(vchi,1);
    1526        6545 :   T = gel(vchi,2);
    1527        6545 :   P = gel(T,1);
    1528        6545 :   CHI = gel(vchi,3);
    1529        6545 :   v = cgetg(n+2, t_VEC);
    1530        6545 :   gel(v, 1) = gcopy(E0); /* E(0) */
    1531        6545 :   if (lg(vchi) == 5)
    1532             :   { /* E_k(chi1,chi2) */
    1533        4753 :     GEN CHI2 = gel(vchi,4), F3 = gel(F,3);
    1534        4753 :     long ord = F3[1], j = F3[2];
    1535        4753 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi2(k, CHI, CHI2, i*d, ord);
    1536        4753 :     if (lg(T) == 4) v = QabV_tracerel(T, j, v);
    1537             :   }
    1538             :   else
    1539             :   { /* E_k(chi) */
    1540        1792 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi(k, CHI, i*d);
    1541             :   }
    1542        6545 :   if (degpol(P) != 1 && !RgV_is_QV(v)) return gerepileupto(av, gmodulo(v, P));
    1543        4942 :   return gerepilecopy(av, v);
    1544             : }
    1545             : 
    1546             : /* L(chi_D, 1-k) */
    1547             : static GEN
    1548          28 : lfunquadneg(long D, long k)
    1549             : {
    1550          28 :   GEN B, dS, S = gen_0;
    1551          28 :   long r, N = labs(D);
    1552             :   pari_sp av;
    1553          28 :   if (k == 1 && N == 1) return gneg(ghalf);
    1554             :   /* B = N^k * denom(B) * B(x/N) */
    1555          28 :   B = ZX_rescale(Q_remove_denom(bernpol(k, 0), &dS), utoi(N));
    1556          28 :   dS = mul_denom(dS, stoi(-N*k));
    1557          28 :   av = avma;
    1558        7175 :   for (r = 0; r < N; r++)
    1559             :   {
    1560        7147 :     long c = kross(D, r);
    1561        7147 :     if (c)
    1562             :     {
    1563        5152 :       GEN tmp = poleval(B, utoi(r));
    1564        5152 :       S = c > 0 ? addii(S, tmp) : subii(S, tmp);
    1565        5152 :       S = gerepileuptoint(av, S);
    1566             :     }
    1567             :   }
    1568          28 :   return gdiv(S, dS);
    1569             : }
    1570             : 
    1571             : /* Returns vector of coeffs from F[0], F[d], ..., F[d*n] */
    1572             : static GEN
    1573       23184 : mfcoefs_i(GEN F, long n, long d)
    1574             : {
    1575       23184 :   if (n < 0) return gen_0;
    1576       23184 :   switch(mf_get_type(F))
    1577             :   {
    1578         140 :     case t_MF_CONST: return c_const(n, d, gel(F,2));
    1579        6685 :     case t_MF_EISEN: return c_mfeisen(n, d, F);
    1580         658 :     case t_MF_Ek: return c_Ek(n, d, F);
    1581         462 :     case t_MF_DELTA: return c_delta(n, d);
    1582        1162 :     case t_MF_THETA: return c_theta(n, d, gel(F,2));
    1583         133 :     case t_MF_ETAQUO: return c_etaquo(n, d, gel(F,2), gel(F,3));
    1584          63 :     case t_MF_ELL: return c_ell(n, d, gel(F,2));
    1585         525 :     case t_MF_MUL: return c_mul(n, d, F);
    1586          77 :     case t_MF_POW: return c_pow(n, d, F);
    1587          21 :     case t_MF_BRACKET: return c_bracket(n, d, F);
    1588        2387 :     case t_MF_LINEAR: return c_linear(n, d, gel(F,2), gel(F,3), gel(F,4));
    1589        1561 :     case t_MF_LINEAR_BHN: return c_linear_bhn(n, d, F);
    1590         777 :     case t_MF_DIV: return c_div(n, d, F);
    1591          35 :     case t_MF_SHIFT: return c_shift(n, d, gel(F,2), gel(F,3));
    1592          21 :     case t_MF_DERIV: return c_deriv(n, d, gel(F,2), gel(F,3));
    1593          14 :     case t_MF_DERIVE2: return c_derivE2(n, d, gel(F,2), gel(F,3));
    1594           7 :     case t_MF_TWIST: return c_twist(n, d, gel(F,2), gel(F,3));
    1595         434 :     case t_MF_HECKE: return c_hecke(n, d, gel(F,2), gel(F,3));
    1596        3661 :     case t_MF_BD: return c_Bd(n, d, gel(F,2), gel(F,3));
    1597          21 :     case t_MF_TRACE: return c_cusptrace(n, d, F);
    1598         749 :     case t_MF_NEWTRACE: return c_newtrace(n, d, F);
    1599        3591 :     case t_MF_DIHEDRAL: return c_dihedral(n, d, gel(F,2), gel(F,3), gel(F,4));
    1600             :     default: pari_err_TYPE("mfcoefs",F); return NULL;/*LCOV_EXCL_LINE*/
    1601             :   }
    1602             : }
    1603             : 
    1604             : static GEN
    1605         308 : matdeflate(long n, long d, GEN M)
    1606             : {
    1607             :   long i, l;
    1608             :   GEN A;
    1609             :   /*  if (d == 1) return M; */
    1610         308 :   A = cgetg_copy(M,&l);
    1611         308 :   for (i = 1; i < l; i++) gel(A,i) = c_deflate(n,d,gel(M,i));
    1612         308 :   return A;
    1613             : }
    1614             : static int
    1615        5362 : space_is_cusp(long space) { return space != mf_FULL && space != mf_EISEN; }
    1616             : /* safe with flraw mf */
    1617             : static GEN
    1618        2156 : mfcoefs_mf(GEN mf, long n, long d)
    1619             : {
    1620        2156 :   GEN MS, ME, E = MF_get_E(mf), S = MF_get_S(mf), M = MF_get_M(mf);
    1621        2156 :   long lE = lg(E), lS = lg(S), l = lE+lS-1;
    1622             : 
    1623        2156 :   if (l == 1) return cgetg(1, t_MAT);
    1624        2044 :   if (typ(M) == t_MAT && lg(M) != 1 && (n+1)*d < nbrows(M))
    1625          21 :     return matdeflate(n, d, M); /*cached; lg = 1 is possible from mfinit */
    1626        2023 :   ME = (lE == 1)? cgetg(1, t_MAT): mfvectomat(E, n, d);
    1627        2023 :   if (lS == 1)
    1628         357 :     MS = cgetg(1, t_MAT);
    1629        1666 :   else if (mf_get_type(gel(S,1)) == t_MF_DIV) /*k 1/2-integer or k=1 (exotic)*/
    1630         287 :     MS = matdeflate(n,d, mflineardivtomat(MF_get_N(mf), S, n*d));
    1631        1379 :   else if (MF_get_k(mf) == 1) /* k = 1 (dihedral) */
    1632             :   {
    1633         140 :     GEN M = mfvectomat(gmael(S,1,2), n, d);
    1634             :     long i;
    1635         140 :     MS = cgetg(lS, t_MAT);
    1636         448 :     for (i = 1; i < lS; i++)
    1637             :     {
    1638         308 :       GEN f = gel(S,i), dc = gel(f,4), c = RgM_RgC_mul(M, gel(f,3));
    1639         308 :       if (!equali1(dc)) c = RgC_Rg_div(c,dc);
    1640         308 :       gel(MS,i) = c;
    1641             :     }
    1642             :   }
    1643             :   else /* k >= 2 integer */
    1644        1239 :     MS = bhnmat_extend_nocache(NULL, MF_get_N(mf), n, d, S);
    1645        2023 :   return shallowconcat(ME,MS);
    1646             : }
    1647             : GEN
    1648        3262 : mfcoefs(GEN F, long n, long d)
    1649             : {
    1650        3262 :   if (!checkmf_i(F))
    1651             :   {
    1652          42 :     pari_sp av = avma;
    1653          42 :     GEN mf = checkMF_i(F); if (!mf) pari_err_TYPE("mfcoefs", F);
    1654          42 :     return gerepilecopy(av, mfcoefs_mf(mf,n,d));
    1655             :   }
    1656        3220 :   if (d <= 0) pari_err_DOMAIN("mfcoefs", "d", "<=", gen_0, stoi(d));
    1657        3220 :   if (n < 0) return cgetg(1, t_VEC);
    1658        3220 :   return mfcoefs_i(F, n, d);
    1659             : }
    1660             : 
    1661             : /* assume k >= 0 */
    1662             : static GEN
    1663         210 : mfak_i(GEN F, long k)
    1664             : {
    1665         210 :   if (!k) return gel(mfcoefs_i(F,0,1), 1);
    1666         154 :   return gel(mfcoefs_i(F,1,k), 2);
    1667             : }
    1668             : GEN
    1669          70 : mfcoef(GEN F, long n)
    1670             : {
    1671          70 :   pari_sp av = avma;
    1672          70 :   if (!checkmf_i(F)) pari_err_TYPE("mfcoef",F);
    1673          70 :   return n < 0? gen_0: gerepilecopy(av, mfak_i(F, n));
    1674             : }
    1675             : 
    1676             : static GEN
    1677         112 : paramconst() { return tagparams(t_MF_CONST, mkNK(1,0,mfchartrivial())); }
    1678             : static GEN
    1679          70 : mftrivial(void) { retmkvec2(paramconst(), cgetg(1,t_VEC)); }
    1680             : static GEN
    1681          42 : mf1(void) { retmkvec2(paramconst(), mkvec(gen_1)); }
    1682             : 
    1683             : /* induce mfchar CHI to G */
    1684             : static GEN
    1685      305816 : induce(GEN G, GEN CHI)
    1686             : {
    1687             :   GEN o, chi;
    1688      305816 :   if (typ(CHI) == t_INT) /* Kronecker */
    1689             :   {
    1690      300601 :     chi = znchar_quad(G, CHI);
    1691      300601 :     o = ZV_equal0(chi)? gen_1: gen_2;
    1692      300601 :     CHI = mkvec4(G,chi,o,cgetg(1,t_VEC));
    1693             :   }
    1694             :   else
    1695             :   {
    1696        5215 :     if (mfcharmodulus(CHI) == itos(znstar_get_N(G))) return CHI;
    1697        4816 :     CHI = leafcopy(CHI);
    1698        4816 :     chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    1699        4816 :     gel(CHI,1) = G;
    1700        4816 :     gel(CHI,2) = chi;
    1701             :   }
    1702      305417 :   return CHI;
    1703             : }
    1704             : /* induce mfchar CHI to znstar(G) */
    1705             : static GEN
    1706       42182 : induceN(long N, GEN CHI)
    1707             : {
    1708       42182 :   if (mfcharmodulus(CHI) != N) CHI = induce(znstar0(utoipos(N),1), CHI);
    1709       42182 :   return CHI;
    1710             : }
    1711             : /* *pCHI1 and *pCHI2 are mfchar, induce to common modulus */
    1712             : static void
    1713        4816 : char2(GEN *pCHI1, GEN *pCHI2)
    1714             : {
    1715        4816 :   GEN CHI1 = *pCHI1, G1 = gel(CHI1,1), N1 = znstar_get_N(G1);
    1716        4816 :   GEN CHI2 = *pCHI2, G2 = gel(CHI2,1), N2 = znstar_get_N(G2);
    1717        4816 :   if (!equalii(N1,N2))
    1718             :   {
    1719        3521 :     GEN G, d = gcdii(N1,N2);
    1720        3521 :     if      (equalii(N2,d)) *pCHI2 = induce(G1, CHI2);
    1721        1330 :     else if (equalii(N1,d)) *pCHI1 = induce(G2, CHI1);
    1722             :     else
    1723             :     {
    1724         154 :       if (!equali1(d)) N2 = diviiexact(N2,d);
    1725         154 :       G = znstar0(mulii(N1,N2), 1);
    1726         154 :       *pCHI1 = induce(G, CHI1);
    1727         154 :       *pCHI2 = induce(G, CHI2);
    1728             :     }
    1729             :   }
    1730        4816 : }
    1731             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1732             : static GEN
    1733      301427 : mfcharmul_i(GEN CHI1, GEN CHI2)
    1734             : {
    1735      301427 :   GEN G = gel(CHI1,1), chi3 = zncharmul(G, gel(CHI1,2), gel(CHI2,2));
    1736      301427 :   return mfcharGL(G, chi3);
    1737             : }
    1738             : /* mfchar or charinit; outputs a mfchar */
    1739             : static GEN
    1740         833 : mfcharmul(GEN CHI1, GEN CHI2)
    1741             : {
    1742         833 :   char2(&CHI1, &CHI2); return mfcharmul_i(CHI1,CHI2);
    1743             : }
    1744             : /* mfchar or charinit; outputs a mfchar */
    1745             : static GEN
    1746         112 : mfcharpow(GEN CHI, GEN n)
    1747             : {
    1748             :   GEN G, chi;
    1749         112 :   G = gel(CHI,1); chi = zncharpow(G, gel(CHI,2), n);
    1750         112 :   return mfcharGL(G, chi);
    1751             : }
    1752             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1753             : static GEN
    1754        3983 : mfchardiv_i(GEN CHI1, GEN CHI2)
    1755             : {
    1756        3983 :   GEN G = gel(CHI1,1), chi3 = znchardiv(G, gel(CHI1,2), gel(CHI2,2));
    1757        3983 :   return mfcharGL(G, chi3);
    1758             : }
    1759             : /* mfchar or charinit; outputs a mfchar */
    1760             : static GEN
    1761        3983 : mfchardiv(GEN CHI1, GEN CHI2)
    1762             : {
    1763        3983 :   char2(&CHI1, &CHI2); return mfchardiv_i(CHI1,CHI2);
    1764             : }
    1765             : static GEN
    1766          28 : mfcharconj(GEN CHI)
    1767             : {
    1768          28 :   CHI = leafcopy(CHI);
    1769          28 :   gel(CHI,2) = zncharconj(gel(CHI,1), gel(CHI,2));
    1770          28 :   return CHI;
    1771             : }
    1772             : 
    1773             : /* CHI mfchar, assume 4 | N. Multiply CHI by \chi_{-4} */
    1774             : static GEN
    1775         812 : mfchilift(GEN CHI, long N)
    1776             : {
    1777         812 :   CHI = induceN(N, CHI);
    1778         812 :   return mfcharmul_i(CHI, induce(gel(CHI,1), stoi(-4)));
    1779             : }
    1780             : /* CHI defined mod N, N4 = N/4;
    1781             :  * if CHI is defined mod N4 return CHI;
    1782             :  * else if CHI' = CHI*(-4,.) is defined mod N4, return CHI' (primitive)
    1783             :  * else return NULL */
    1784             : static GEN
    1785          70 : mfcharchiliftprim(GEN CHI, long N4)
    1786             : {
    1787          70 :   long FC = mfcharconductor(CHI);
    1788          70 :   if (N4 % FC == 0) return CHI;
    1789          14 :   CHI = mfchilift(CHI, N4 << 2);
    1790          14 :   CHI = mfchartoprimitive(CHI, &FC);
    1791          14 :   return (N4 % FC == 0)? CHI: NULL;
    1792             : }
    1793             : static GEN
    1794        1974 : mfchiadjust(GEN CHI, GEN gk, long N)
    1795             : {
    1796        1974 :   long par = mfcharparity(CHI);
    1797        1974 :   if (typ(gk) == t_INT &&  mpodd(gk)) par = -par;
    1798        1974 :   return par == 1 ? CHI : mfchilift(CHI, N);
    1799             : }
    1800             : 
    1801             : static GEN
    1802        2779 : mfsamefield(GEN T, GEN P, GEN Q)
    1803             : {
    1804        2779 :   if (degpol(P) == 1) return Q;
    1805         462 :   if (degpol(Q) == 1) return P;
    1806         434 :   if (!gequal(P,Q)) pari_err_TYPE("mfsamefield [different fields]",mkvec2(P,Q));
    1807         427 :   if (T) err_cyclo();
    1808         427 :   return P;
    1809             : }
    1810             : 
    1811             : GEN
    1812         350 : mfmul(GEN f, GEN g)
    1813             : {
    1814         350 :   pari_sp av = avma;
    1815             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    1816         350 :   if (!checkmf_i(f)) pari_err_TYPE("mfmul",f);
    1817         350 :   if (!checkmf_i(g)) pari_err_TYPE("mfmul",g);
    1818         350 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1819         350 :   K = gadd(mf_get_gk(f), mf_get_gk(g));
    1820         350 :   CHIf = mf_get_CHI(f);
    1821         350 :   CHIg = mf_get_CHI(g);
    1822         350 :   CHI = mfchiadjust(mfcharmul(CHIf,CHIg), K, itos(N));
    1823         350 :   T = chicompat(CHI, CHIf, CHIg);
    1824         350 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    1825         343 :   return gerepilecopy(av, T? tag3(t_MF_MUL,NK,f,g,T): tag2(t_MF_MUL,NK,f,g));
    1826             : }
    1827             : GEN
    1828          56 : mfpow(GEN f, long n)
    1829             : {
    1830          56 :   pari_sp av = avma;
    1831             :   GEN T, KK, NK, gn, CHI, CHIf;
    1832          56 :   if (!checkmf_i(f)) pari_err_TYPE("mfpow",f);
    1833          56 :   if (!n) return mf1();
    1834          56 :   if (n == 1) return gcopy(f);
    1835          56 :   KK = gmulsg(n,mf_get_gk(f));
    1836          56 :   gn = stoi(n);
    1837          56 :   CHIf = mf_get_CHI(f);
    1838          56 :   CHI = mfchiadjust(mfcharpow(CHIf,gn), KK, mf_get_N(f));
    1839          56 :   T = chicompat(CHI, CHIf, CHIf);
    1840          49 :   NK = mkgNK(mf_get_gN(f), KK, CHI, mf_get_field(f));
    1841          49 :   return gerepilecopy(av, T? tag3(t_MF_POW,NK,f,gn,T): tag2(t_MF_POW,NK,f,gn));
    1842             : }
    1843             : GEN
    1844          21 : mfbracket(GEN f, GEN g, long m)
    1845             : {
    1846          21 :   pari_sp av = avma;
    1847             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    1848          21 :   if (!checkmf_i(f)) pari_err_TYPE("mfbracket",f);
    1849          21 :   if (!checkmf_i(g)) pari_err_TYPE("mfbracket",g);
    1850          21 :   if (m < 0) pari_err_TYPE("mfbracket [m<0]",stoi(m));
    1851          21 :   K = gaddgs(gadd(mf_get_gk(f), mf_get_gk(g)), 2*m);
    1852          21 :   if (gsigne(K) < 0) pari_err_IMPL("mfbracket for this form");
    1853          21 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1854          21 :   CHIf = mf_get_CHI(f);
    1855          21 :   CHIg = mf_get_CHI(g);
    1856          21 :   CHI = mfcharmul(CHIf, CHIg);
    1857          21 :   CHI = mfchiadjust(CHI, K, itou(N));
    1858          21 :   T = chicompat(CHI, CHIf, CHIg);
    1859          21 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    1860          42 :   return gerepilecopy(av, T? tag4(t_MF_BRACKET, NK, f, g, utoi(m), T)
    1861          21 :                            : tag3(t_MF_BRACKET, NK, f, g, utoi(m)));
    1862             : }
    1863             : 
    1864             : /* remove 0 entries in L */
    1865             : static int
    1866        1085 : mflinear_strip(GEN *pF, GEN *pL)
    1867             : {
    1868        1085 :   pari_sp av = avma;
    1869        1085 :   GEN F = *pF, L = *pL;
    1870        1085 :   long i, j, l = lg(L);
    1871        1085 :   GEN F2 = cgetg(l, t_VEC), L2 = cgetg(l, t_VEC);
    1872        6566 :   for (i = j = 1; i < l; i++)
    1873             :   {
    1874        5481 :     if (gequal0(gel(L,i))) continue;
    1875        3045 :     gel(F2,j) = gel(F,i);
    1876        3045 :     gel(L2,j) = gel(L,i); j++;
    1877             :   }
    1878        1085 :   if (j == l) set_avma(av);
    1879             :   else
    1880             :   {
    1881         280 :     setlg(F2,j); *pF = F2;
    1882         280 :     setlg(L2,j); *pL = L2;
    1883             :   }
    1884        1085 :   return (j > 1);
    1885             : }
    1886             : static GEN
    1887        4543 : taglinear_i(long t, GEN NK, GEN F, GEN L)
    1888             : {
    1889             :   GEN dL;
    1890        4543 :   L = Q_remove_denom(L, &dL); if (!dL) dL = gen_1;
    1891        4543 :   return tag3(t, NK, F, L, dL);
    1892             : }
    1893             : static GEN
    1894        1736 : taglinear(GEN NK, GEN F, GEN L)
    1895             : {
    1896        1736 :   long t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1897        1736 :    return taglinear_i(t, NK, F, L);
    1898             : }
    1899             : /* assume F has parameters NK = [N,K,CHI] */
    1900             : static GEN
    1901         301 : mflinear_i(GEN NK, GEN F, GEN L)
    1902             : {
    1903         301 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1904         301 :   return taglinear(NK, F,L);
    1905             : }
    1906             : static GEN
    1907         469 : mflinear_bhn(GEN mf, GEN L)
    1908             : {
    1909             :   long i, l;
    1910         469 :   GEN P, NK, F = MF_get_S(mf);
    1911         469 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1912         462 :   l = lg(L); P = pol_x(1);
    1913        2541 :   for (i = 1; i < l; i++)
    1914             :   {
    1915        2079 :     GEN c = gel(L,i);
    1916        2079 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1)
    1917         497 :       P = mfsamefield(NULL, P, gel(c,1));
    1918             :   }
    1919         462 :   NK = mkgNK(MF_get_gN(mf), MF_get_gk(mf), MF_get_CHI(mf), P);
    1920         462 :   return taglinear_i(t_MF_LINEAR_BHN,  NK, F,L);
    1921             : }
    1922             : 
    1923             : /* F vector of forms with same weight and character but varying level, return
    1924             :  * global [N,k,chi,P] */
    1925             : static GEN
    1926        2345 : vecmfNK(GEN F)
    1927             : {
    1928        2345 :   long i, l = lg(F);
    1929             :   GEN N, f;
    1930        2345 :   if (l == 1) return mkNK(1, 0, mfchartrivial());
    1931        2345 :   f = gel(F,1); N = mf_get_gN(f);
    1932        2345 :   for (i = 2; i < l; i++) N = lcmii(N, mf_get_gN(gel(F,i)));
    1933        2345 :   return mkgNK(N, mf_get_gk(f), mf_get_CHI(f), mf_get_field(f));
    1934             : }
    1935             : /* do not use mflinear: mflineardivtomat rely on F being constant across the
    1936             :  * basis where mflinear strips the ones matched by 0 coeffs. Assume k and CHI
    1937             :  * constant, N is allowed to vary. */
    1938             : static GEN
    1939        1071 : vecmflinear(GEN F, GEN C)
    1940             : {
    1941        1071 :   long i, t, l = lg(C);
    1942        1071 :   GEN NK, v = cgetg(l, t_VEC);
    1943        1071 :   if (l == 1) return v;
    1944        1071 :   t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1945        1071 :   NK = vecmfNK(F);
    1946        1071 :   for (i = 1; i < l; i++) gel(v,i) = taglinear_i(t, NK, F, gel(C,i));
    1947        1071 :   return v;
    1948             : }
    1949             : /* vecmflinear(F,C), then divide everything by E, which has valuation 0 */
    1950             : static GEN
    1951         343 : vecmflineardiv0(GEN F, GEN C, GEN E)
    1952             : {
    1953         343 :   GEN v = vecmflinear(F, C);
    1954         343 :   long i, l = lg(v);
    1955         343 :   if (l == 1) return v;
    1956         343 :   gel(v,1) = mfdiv_val(gel(v,1), E, 0);
    1957        1029 :   for (i = 2; i < l; i++)
    1958             :   { /* v[i] /= E */
    1959         686 :     GEN f = shallowcopy(gel(v,1));
    1960         686 :     gel(f,2) = gel(v,i);
    1961         686 :     gel(v,i) = f;
    1962             :   }
    1963         343 :   return v;
    1964             : }
    1965             : 
    1966             : /* Non empty linear combination of linear combinations of same
    1967             :  * F_j=\sum_i \mu_{i,j}G_i so R = \sum_i (\sum_j(\la_j\mu_{i,j})) G_i */
    1968             : static GEN
    1969        1274 : mflinear_linear(GEN F, GEN L, int strip)
    1970             : {
    1971        1274 :   long l = lg(F), j;
    1972        1274 :   GEN vF, M = cgetg(l, t_MAT);
    1973        1274 :   L = shallowcopy(L);
    1974        7686 :   for (j = 1; j < l; j++)
    1975             :   {
    1976        6412 :     GEN f = gel(F,j), c = gel(f,3), d = gel(f,4);
    1977        6412 :     if (typ(c) == t_VEC) c = shallowtrans(c);
    1978        6412 :     if (!isint1(d)) gel(L,j) = gdiv(gel(L,j),d);
    1979        6412 :     gel(M,j) = c;
    1980             :   }
    1981        1274 :   vF = gmael(F,1,2); L = RgM_RgC_mul(M,L);
    1982        1274 :   if (strip && !mflinear_strip(&vF,&L)) return mftrivial();
    1983        1274 :   return taglinear(vecmfNK(vF), vF, L);
    1984             : }
    1985             : /* F non-empty vector of forms of the form mfdiv(mflinear(B,v), E) where E
    1986             :  * does not vanish at oo, or mflinear(B,v). Apply mflinear(F, L) */
    1987             : static GEN
    1988        1274 : mflineardiv_linear(GEN F, GEN L, int strip)
    1989             : {
    1990        1274 :   long l = lg(F), j;
    1991             :   GEN v, E, f;
    1992        1274 :   if (lg(L) != l) pari_err_DIM("mflineardiv_linear");
    1993        1274 :   f = gel(F,1); /* l > 1 */
    1994        1274 :   if (mf_get_type(f) != t_MF_DIV) return mflinear_linear(F,L,strip);
    1995        1106 :   E = gel(f,3);
    1996        1106 :   v = cgetg(l, t_VEC);
    1997        1106 :   for (j = 1; j < l; j++) { GEN f = gel(F,j); gel(v,j) = gel(f,2); }
    1998        1106 :   return mfdiv_val(mflinear_linear(v,L,strip), E, 0);
    1999             : }
    2000             : static GEN
    2001         399 : vecmflineardiv_linear(GEN F, GEN M)
    2002             : {
    2003         399 :   long i, l = lg(M);
    2004         399 :   GEN v = cgetg(l, t_VEC);
    2005         399 :   for (i = 1; i < l; i++) gel(v,i) = mflineardiv_linear(F, gel(M,i), 0);
    2006         399 :   return v;
    2007             : }
    2008             : 
    2009             : static GEN
    2010         476 : tobasis(GEN mf, GEN F, GEN L)
    2011             : {
    2012         476 :   if (checkmf_i(L) && mf) return mftobasis(mf, L, 0);
    2013         469 :   if (typ(F) != t_VEC) pari_err_TYPE("mflinear",F);
    2014         469 :   if (!is_vec_t(typ(L))) pari_err_TYPE("mflinear",L);
    2015         469 :   if (lg(L) != lg(F)) pari_err_DIM("mflinear");
    2016         469 :   return L;
    2017             : }
    2018             : GEN
    2019         504 : mflinear(GEN F, GEN L)
    2020             : {
    2021         504 :   pari_sp av = avma;
    2022         504 :   GEN G, NK, P, mf = checkMF_i(F), N = NULL, K = NULL, CHI = NULL;
    2023             :   long i, l;
    2024         504 :   if (mf)
    2025             :   {
    2026         378 :     GEN gk = MF_get_gk(mf);
    2027         378 :     F = MF_get_basis(F);
    2028         378 :     if (typ(gk) != t_INT)
    2029          28 :       return gerepilecopy(av, mflineardiv_linear(F, L, 1));
    2030         350 :     if (itou(gk) > 1 && space_is_cusp(MF_get_space(mf)))
    2031             :     {
    2032         238 :       L = tobasis(mf, F, L);
    2033         238 :       return gerepilecopy(av, mflinear_bhn(mf, L));
    2034             :     }
    2035             :   }
    2036         238 :   L = tobasis(mf, F, L);
    2037         238 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    2038             : 
    2039         231 :   l = lg(F);
    2040         231 :   if (l == 2 && gequal1(gel(L,1))) return gerepilecopy(av, gel(F,1));
    2041         175 :   P = pol_x(1);
    2042         581 :   for (i = 1; i < l; i++)
    2043             :   {
    2044         413 :     GEN f = gel(F,i), c = gel(L,i), Ni, Ki;
    2045         413 :     if (!checkmf_i(f)) pari_err_TYPE("mflinear", f);
    2046         413 :     Ni = mf_get_gN(f); N = N? lcmii(N, Ni): Ni;
    2047         413 :     Ki = mf_get_gk(f);
    2048         413 :     if (!K) K = Ki;
    2049         238 :     else if (!gequal(K, Ki))
    2050           7 :       pari_err_TYPE("mflinear [different weights]", mkvec2(K,Ki));
    2051         406 :     P = mfsamefield(NULL, P, mf_get_field(f));
    2052         406 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1)
    2053          35 :       P = mfsamefield(NULL, P, gel(c,1));
    2054             :   }
    2055         168 :   G = znstar0(N,1);
    2056         560 :   for (i = 1; i < l; i++)
    2057             :   {
    2058         399 :     GEN CHI2 = mf_get_CHI(gel(F,i));
    2059         399 :     CHI2 = induce(G, CHI2);
    2060         399 :     if (!CHI) CHI = CHI2;
    2061         231 :     else if (!gequal(CHI, CHI2))
    2062           7 :       pari_err_TYPE("mflinear [different characters]", mkvec2(CHI,CHI2));
    2063             :   }
    2064         161 :   NK = mkgNK(N, K, CHI, P);
    2065         161 :   return gerepilecopy(av, taglinear(NK,F,L));
    2066             : }
    2067             : 
    2068             : GEN
    2069          42 : mfshift(GEN F, long sh)
    2070             : {
    2071          42 :   pari_sp av = avma;
    2072          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfshift",F);
    2073          42 :   return gerepilecopy(av, tag2(t_MF_SHIFT, mf_get_NK(F), F, stoi(sh)));
    2074             : }
    2075             : static long
    2076          49 : mfval(GEN F)
    2077             : {
    2078          49 :   pari_sp av = avma;
    2079          49 :   long i = 0, n, sb;
    2080             :   GEN gk, gN;
    2081          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfval", F);
    2082          49 :   gN = mf_get_gN(F);
    2083          49 :   gk = mf_get_gk(F);
    2084          49 :   sb = mfsturmNgk(itou(gN), gk);
    2085         119 :   for (n = 1; n <= sb;)
    2086             :   {
    2087             :     GEN v;
    2088          63 :     if (n > 0.5*sb) n = sb+1;
    2089          63 :     v = mfcoefs_i(F, n, 1);
    2090         119 :     for (; i <= n; i++)
    2091          98 :       if (!gequal0(gel(v, i+1))) return gc_long(av,i);
    2092          21 :     n <<= 1;
    2093             :   }
    2094           7 :   return gc_long(av,-1);
    2095             : }
    2096             : 
    2097             : GEN
    2098        1477 : mfdiv_val(GEN f, GEN g, long vg)
    2099             : {
    2100             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    2101        1477 :   if (vg) { f = mfshift(f,vg); g = mfshift(g,vg); }
    2102        1477 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    2103        1477 :   K = gsub(mf_get_gk(f), mf_get_gk(g));
    2104        1477 :   CHIf = mf_get_CHI(f);
    2105        1477 :   CHIg = mf_get_CHI(g);
    2106        1477 :   CHI = mfchiadjust(mfchardiv(CHIf, CHIg), K, itos(N));
    2107        1477 :   T = chicompat(CHI, CHIf, CHIg);
    2108        1470 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    2109        1470 :   return T? tag3(t_MF_DIV, NK, f, g, T): tag2(t_MF_DIV, NK, f, g);
    2110             : }
    2111             : GEN
    2112          49 : mfdiv(GEN F, GEN G)
    2113             : {
    2114          49 :   pari_sp av = avma;
    2115          49 :   long v = mfval(G);
    2116          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfdiv", F);
    2117          42 :   if (v < 0 || (v && !gequal0(mfcoefs(F, v-1, 1))))
    2118          14 :     pari_err_DOMAIN("mfdiv", "ord(G)", ">", strtoGENstr("ord(F)"),
    2119             :                     mkvec2(F, G));
    2120          28 :   return gerepilecopy(av, mfdiv_val(F, G, v));
    2121             : }
    2122             : GEN
    2123          28 : mfderiv(GEN F, long m)
    2124             : {
    2125          28 :   pari_sp av = avma;
    2126             :   GEN NK, gk;
    2127          28 :   if (!checkmf_i(F)) pari_err_TYPE("mfderiv",F);
    2128          28 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2129          28 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2130          28 :   return gerepilecopy(av, tag2(t_MF_DERIV, NK, F, stoi(m)));
    2131             : }
    2132             : GEN
    2133          21 : mfderivE2(GEN F, long m)
    2134             : {
    2135          21 :   pari_sp av = avma;
    2136             :   GEN NK, gk;
    2137          21 :   if (!checkmf_i(F)) pari_err_TYPE("mfderivE2",F);
    2138          21 :   if (m < 0) pari_err_DOMAIN("mfderivE2","m","<",gen_0,stoi(m));
    2139          21 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2140          21 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2141          21 :   return gerepilecopy(av, tag2(t_MF_DERIVE2, NK, F, stoi(m)));
    2142             : }
    2143             : 
    2144             : GEN
    2145          14 : mftwist(GEN F, GEN D)
    2146             : {
    2147          14 :   pari_sp av = avma;
    2148             :   GEN NK, CHI, NT, Da;
    2149             :   long q;
    2150          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftwist", F);
    2151          14 :   if (typ(D) != t_INT) pari_err_TYPE("mftwist", D);
    2152          14 :   Da = mpabs_shallow(D);
    2153          14 :   CHI = mf_get_CHI(F); q = mfcharconductor(CHI);
    2154          14 :   NT = glcm(glcm(mf_get_gN(F), mulsi(q, Da)), sqri(Da));
    2155          14 :   NK = mkgNK(NT, mf_get_gk(F), CHI, mf_get_field(F));
    2156          14 :   return gerepilecopy(av, tag2(t_MF_TWIST, NK, F, D));
    2157             : }
    2158             : 
    2159             : /***************************************************************/
    2160             : /*                 Generic cache handling                      */
    2161             : /***************************************************************/
    2162             : enum { cache_FACT, cache_DIV, cache_H, cache_D, cache_DIH };
    2163             : typedef struct {
    2164             :   const char *name;
    2165             :   GEN cache;
    2166             :   ulong minself;
    2167             :   ulong maxself;
    2168             :   void (*init)(long);
    2169             :   ulong miss;
    2170             :   ulong maxmiss;
    2171             : } cache;
    2172             : 
    2173             : static void constfact(long lim);
    2174             : static void constdiv(long lim);
    2175             : static void consttabh(long lim);
    2176             : static void consttabdihedral(long lim);
    2177             : static void constcoredisc(long lim);
    2178             : static THREAD cache caches[] = {
    2179             : { "Factors",  NULL,  50000,    50000, &constfact, 0, 0 },
    2180             : { "Divisors", NULL,  50000,    50000, &constdiv, 0, 0 },
    2181             : { "H",        NULL, 100000, 10000000, &consttabh, 0, 0 },
    2182             : { "CorediscF",NULL, 100000, 10000000, &constcoredisc, 0, 0 },
    2183             : { "Dihedral", NULL,   1000,     3000, &consttabdihedral, 0, 0 },
    2184             : };
    2185             : 
    2186             : static void
    2187         343 : cache_reset(long id) { caches[id].miss = caches[id].maxmiss = 0; }
    2188             : static void
    2189        6196 : cache_delete(long id) { if (caches[id].cache) gunclone(caches[id].cache); }
    2190             : static void
    2191         357 : cache_set(long id, GEN S)
    2192             : {
    2193         357 :   GEN old = caches[id].cache;
    2194         357 :   caches[id].cache = gclone(S);
    2195         357 :   if (old) gunclone(old);
    2196         357 : }
    2197             : 
    2198             : /* handle a cache miss: store stats, possibly reset table; return value
    2199             :  * if (now) cached; return NULL on failure. HACK: some caches contain an
    2200             :  * ulong where the 0 value is impossible, and return it (typecase to GEN) */
    2201             : static GEN
    2202   234033683 : cache_get(long id, ulong D)
    2203             : {
    2204   234033683 :   cache *S = &caches[id];
    2205             :   /* cache_H is compressed: D=0,1 mod 4 */
    2206   234033683 :   const ulong d = (id == cache_H)? D>>1: D;
    2207             :   ulong max, l;
    2208             : 
    2209   234033683 :   if (!S->cache)
    2210             :   {
    2211         217 :     max = maxuu(minuu(D, S->maxself), S->minself);
    2212         217 :     S->init(max);
    2213         217 :     l = lg(S->cache);
    2214             :   }
    2215             :   else
    2216             :   {
    2217   234033466 :     l = lg(S->cache);
    2218   234033466 :     if (l <= d)
    2219             :     {
    2220         994 :       if (D > S->maxmiss) S->maxmiss = D;
    2221         994 :       if (DEBUGLEVEL >= 3)
    2222           0 :         err_printf("miss in cache %s: %lu, max = %lu\n",
    2223             :                    S->name, D, S->maxmiss);
    2224         994 :       if (S->miss++ >= 5 && D < S->maxself)
    2225             :       {
    2226          84 :         max = minuu(S->maxself, (long)(S->maxmiss * 1.2));
    2227          84 :         if (max <= S->maxself)
    2228             :         {
    2229          84 :           if (DEBUGLEVEL >= 3)
    2230           0 :             err_printf("resetting cache %s to %lu\n", S->name, max);
    2231          84 :           S->init(max); l = lg(S->cache);
    2232             :         }
    2233             :       }
    2234             :     }
    2235             :   }
    2236   234033683 :   return (l <= d)? NULL: gel(S->cache, d);
    2237             : }
    2238             : static GEN
    2239          70 : cache_report(long id)
    2240             : {
    2241          70 :   cache *S = &caches[id];
    2242          70 :   GEN v = zerocol(5);
    2243          70 :   gel(v,1) = strtoGENstr(S->name);
    2244          70 :   if (S->cache)
    2245             :   {
    2246          35 :     gel(v,2) = utoi(lg(S->cache)-1);
    2247          35 :     gel(v,3) = utoi(S->miss);
    2248          35 :     gel(v,4) = utoi(S->maxmiss);
    2249          35 :     gel(v,5) = utoi(gsizebyte(S->cache));
    2250             :   }
    2251          70 :   return v;
    2252             : }
    2253             : GEN
    2254          14 : getcache(void)
    2255             : {
    2256          14 :   pari_sp av = avma;
    2257          14 :   GEN M = cgetg(6, t_MAT);
    2258          14 :   gel(M,1) = cache_report(cache_FACT);
    2259          14 :   gel(M,2) = cache_report(cache_DIV);
    2260          14 :   gel(M,3) = cache_report(cache_H);
    2261          14 :   gel(M,4) = cache_report(cache_D);
    2262          14 :   gel(M,5) = cache_report(cache_DIH);
    2263          14 :   return gerepilecopy(av, shallowtrans(M));
    2264             : }
    2265             : 
    2266             : void
    2267        1549 : pari_close_mf(void)
    2268             : {
    2269        1549 :   cache_delete(cache_DIH);
    2270        1549 :   cache_delete(cache_DIV);
    2271        1549 :   cache_delete(cache_FACT);
    2272        1549 :   cache_delete(cache_H);
    2273        1549 : }
    2274             : 
    2275             : /*************************************************************************/
    2276             : /* a odd, update local cache (recycle memory) */
    2277             : static GEN
    2278        1948 : update_factor_cache(long a, long lim, long *pb)
    2279             : {
    2280        1948 :   const long step = 16000; /* even; don't increase this: RAM cache thrashing */
    2281        1948 :   if (a + 2*step > lim)
    2282         203 :     *pb = lim; /* fuse last 2 chunks */
    2283             :   else
    2284        1745 :     *pb = a + step;
    2285        1948 :   return vecfactoroddu_i(a, *pb);
    2286             : }
    2287             : /* assume lim < MAX_LONG/8 */
    2288             : static void
    2289          70 : constcoredisc(long lim)
    2290             : {
    2291          70 :   pari_sp av2, av = avma;
    2292          70 :   GEN D = caches[cache_D].cache, CACHE = NULL;
    2293          70 :   long cachea, cacheb, N, LIM = !D ? 4 : lg(D)-1;
    2294          70 :   if (lim <= 0) lim = 5;
    2295          70 :   if (lim <= LIM) return;
    2296          70 :   cache_reset(cache_D);
    2297          70 :   D = zero_zv(lim);
    2298          70 :   av2 = avma;
    2299          70 :   cachea = cacheb = 0;
    2300     7903329 :   for (N = 1; N <= lim; N+=2)
    2301             :   { /* N odd */
    2302             :     long i, d, d2;
    2303             :     GEN F;
    2304     7903259 :     if (N > cacheb)
    2305             :     {
    2306         966 :       set_avma(av2); cachea = N;
    2307         966 :       CACHE = update_factor_cache(N, lim, &cacheb);
    2308             :     }
    2309     7903259 :     F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2310     7903259 :     D[N] = d = corediscs_fact(F); /* = 3 mod 4 or 4 mod 16 */
    2311     7903259 :     d2 = odd(d)? d<<3: d<<1;
    2312     7903259 :     for (i = 1;;)
    2313             :     {
    2314    13172075 :       if ((N << i) > lim) break;
    2315     5268822 :       D[N<<i] = d2; i++;
    2316     5268822 :       if ((N << i) > lim) break;
    2317     2634408 :       D[N<<i] = d; i++;
    2318             :     }
    2319             :   }
    2320          70 :   cache_set(cache_D, D);
    2321          70 :   set_avma(av);
    2322             : }
    2323             : 
    2324             : static void
    2325          84 : constfact(long lim)
    2326             : {
    2327             :   pari_sp av;
    2328          84 :   GEN VFACT = caches[cache_FACT].cache;
    2329          84 :   long LIM = VFACT? lg(VFACT)-1: 4;
    2330          84 :   if (lim <= 0) lim = 5;
    2331          84 :   if (lim <= LIM) return;
    2332          70 :   cache_reset(cache_FACT); av = avma;
    2333          70 :   cache_set(cache_FACT, vecfactoru_i(1,lim)); set_avma(av);
    2334             : }
    2335             : static void
    2336          70 : constdiv(long lim)
    2337             : {
    2338             :   pari_sp av;
    2339          70 :   GEN VFACT, VDIV = caches[cache_DIV].cache;
    2340          70 :   long N, LIM = VDIV? lg(VDIV)-1: 4;
    2341          70 :   if (lim <= 0) lim = 5;
    2342          70 :   if (lim <= LIM) return;
    2343          70 :   constfact(lim);
    2344          70 :   VFACT = caches[cache_FACT].cache;
    2345          70 :   cache_reset(cache_DIV); av = avma;
    2346          70 :   VDIV  = cgetg(lim+1, t_VEC);
    2347          70 :   for (N = 1; N <= lim; N++) gel(VDIV,N) = divisorsu_fact(gel(VFACT,N));
    2348          70 :   cache_set(cache_DIV, VDIV); set_avma(av);
    2349             : }
    2350             : 
    2351             : /* n > 1, D = divisors(n); sets L = 2*lambda(n), S = sigma(n) */
    2352             : static void
    2353     9867614 : lamsig(GEN D, long *pL, long *pS)
    2354             : {
    2355     9867614 :   pari_sp av = avma;
    2356     9867614 :   long i, l = lg(D), L = 1, S = D[l-1]+1;
    2357    35523778 :   for (i = 2; i < l; i++) /* skip d = 1 */
    2358             :   {
    2359    35523778 :     long d = D[i], nd = D[l-i]; /* nd = n/d */
    2360    35523778 :     if (d < nd) { L += d; S += d + nd; }
    2361             :     else
    2362             :     {
    2363     9867614 :       L <<= 1; if (d == nd) { L += d; S += d; }
    2364     9867614 :       break;
    2365             :     }
    2366             :   }
    2367     9867614 :   set_avma(av); *pL = L; *pS = S;
    2368     9867614 : }
    2369             : /* table of 6 * Hurwitz class numbers D <= lim */
    2370             : static void
    2371         133 : consttabh(long lim)
    2372             : {
    2373         133 :   pari_sp av = avma, av2;
    2374         133 :   GEN VHDH0, VDIV, CACHE = NULL;
    2375         133 :   GEN VHDH = caches[cache_H].cache;
    2376         133 :   long r, N, cachea, cacheb, lim0 = VHDH? lg(VHDH)-1: 2, LIM = lim0 << 1;
    2377             : 
    2378         133 :   if (lim <= 0) lim = 5;
    2379         133 :   if (lim <= LIM) return;
    2380         133 :   cache_reset(cache_H);
    2381         133 :   r = lim&3L; if (r) lim += 4-r;
    2382         133 :   cache_get(cache_DIV, lim);
    2383         133 :   VDIV = caches[cache_DIV].cache;
    2384         133 :   VHDH0 = cgetg(lim/2 + 1, t_VECSMALL);
    2385         133 :   VHDH0[1] = 2;
    2386         133 :   VHDH0[2] = 3;
    2387         133 :   for (N = 3; N <= lim0; N++) VHDH0[N] = VHDH[N];
    2388         133 :   av2 = avma;
    2389         133 :   cachea = cacheb = 0;
    2390     4933940 :   for (N = LIM + 3; N <= lim; N += 4)
    2391             :   {
    2392     4933807 :     long s = 0, limt = usqrt(N>>2), flsq = 0, ind, t, L, S;
    2393             :     GEN DN, DN2;
    2394     4933807 :     if (N + 2 >= lg(VDIV))
    2395             :     { /* use local cache */
    2396             :       GEN F;
    2397     4058947 :       if (N + 2 > cacheb)
    2398             :       {
    2399         982 :         set_avma(av2); cachea = N;
    2400         982 :         CACHE = update_factor_cache(N, lim+2, &cacheb);
    2401             :       }
    2402     4058947 :       F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2403     4058947 :       DN = divisorsu_fact(F);
    2404     4058947 :       F = gel(CACHE, ((N-cachea)>>1)+2); /* factoru(N+2) */
    2405     4058947 :       DN2 = divisorsu_fact(F);
    2406             :     }
    2407             :     else
    2408             :     { /* use global cache */
    2409      874860 :       DN = gel(VDIV,N);
    2410      874860 :       DN2 = gel(VDIV,N+2);
    2411             :     }
    2412     4933807 :     ind = N >> 1;
    2413  1078343441 :     for (t = 1; t <= limt; t++)
    2414             :     {
    2415  1073409634 :       ind -= (t<<2)-2; /* N/2 - 2t^2 */
    2416  1073409634 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2417             :     }
    2418     4933807 :     lamsig(DN, &L,&S);
    2419     4933807 :     VHDH0[N >> 1] = 2*S - 3*L - 2*s + flsq;
    2420     4933807 :     s = 0; flsq = 0; limt = (usqrt(N+2) - 1) >> 1;
    2421     4933807 :     ind = (N+1) >> 1;
    2422  1075899401 :     for (t = 1; t <= limt; t++)
    2423             :     {
    2424  1070965594 :       ind -= t<<2; /* (N+1)/2 - 2t(t+1) */
    2425  1070965594 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2426             :     }
    2427     4933807 :     lamsig(DN2, &L,&S);
    2428     4933807 :     VHDH0[(N+1) >> 1] = S - 3*(L >> 1) - s - flsq;
    2429             :   }
    2430         133 :   cache_set(cache_H, VHDH0); set_avma(av);
    2431             : }
    2432             : 
    2433             : /*************************************************************************/
    2434             : /* Core functions using factorizations, divisors of class numbers caches */
    2435             : /* TODO: myfactoru and factorization cache should be exported */
    2436             : static GEN
    2437    21744877 : myfactoru(long N)
    2438             : {
    2439    21744877 :   GEN z = cache_get(cache_FACT, N);
    2440    21744877 :   return z? gcopy(z): factoru(N);
    2441             : }
    2442             : static GEN
    2443    50419474 : mydivisorsu(long N)
    2444             : {
    2445    50419474 :   GEN z = cache_get(cache_DIV, N);
    2446    50419474 :   return z? leafcopy(z): divisorsu(N);
    2447             : }
    2448             : /* write -n = Df^2, D < 0 fundamental discriminant. Return D, set f. */
    2449             : static long
    2450    85412572 : mycoredisc2neg(ulong n, long *pf)
    2451             : {
    2452    85412572 :   ulong m, D = (ulong)cache_get(cache_D, n);
    2453    85412572 :   if (D) { *pf = usqrt(n/D); return -(long)D; }
    2454         196 :   m = mycore(n, pf);
    2455         196 :   if ((m&3) != 3) { m <<= 2; *pf >>= 1; }
    2456         196 :   return (long)-m;
    2457             : }
    2458             : /* write n = Df^2, D > 0 fundamental discriminant. Return D, set f. */
    2459             : static long
    2460          14 : mycoredisc2pos(ulong n, long *pf)
    2461             : {
    2462          14 :   ulong m = mycore(n, pf);
    2463          14 :   if ((m&3) != 1) { m <<= 2; *pf >>= 1; }
    2464          14 :   return (long)m;
    2465             : }
    2466             : 
    2467             : /* 1+p+...+p^e, e >= 1 */
    2468             : static ulong
    2469          49 : usumpow(ulong p, long e)
    2470             : {
    2471          49 :   ulong q = 1+p;
    2472             :   long i;
    2473          49 :   for (i = 1; i < e; i++) q = p*q + 1;
    2474          49 :   return q;
    2475             : }
    2476             : /* Hurwitz(D0 F^2)/ Hurwitz(D0)
    2477             :  * = \sum_{f|F}  f \prod_{p|f} (1-kro(D0/p)/p)
    2478             :  * = \prod_{p^e || F} (1 + (p^e-1) / (p-1) * (p-kro(D0/p))) */
    2479             : static long
    2480         294 : get_sh(long F, long D0)
    2481             : {
    2482         294 :   GEN fa = myfactoru(F), P = gel(fa,1), E = gel(fa,2);
    2483         294 :   long i, l = lg(P), t = 1;
    2484         794 :   for (i = 1; i < l; i++)
    2485             :   {
    2486         500 :     long p = P[i], e = E[i], s = kross(D0,p);
    2487         500 :     if (e == 1) { t *= 1 + p - s; continue; }
    2488         153 :     if (s == 1) { t *= upowuu(p,e); continue; }
    2489          49 :     t *= 1 + usumpow(p,e-1)*(p-s);
    2490             :   }
    2491         294 :   return t;
    2492             : }
    2493             : /* d > 0, d = 0,3 (mod 4). Return 6*hclassno(d); -d must be fundamental
    2494             :  * Faster than quadclassunit up to 5*10^5 or so */
    2495             : static ulong
    2496          42 : hclassno6u_count(ulong d)
    2497             : {
    2498          42 :   ulong a, b, b2, h = 0;
    2499          42 :   int f = 0;
    2500             : 
    2501          42 :   if (d > 500000)
    2502           7 :     return 6 * itou(gel(quadclassunit0(utoineg(d), 0, NULL, 0), 1));
    2503             : 
    2504             :   /* this part would work with -d non fundamental */
    2505          35 :   b = d&1; b2 = (1+d)>>2;
    2506          35 :   if (!b)
    2507             :   {
    2508           0 :     for (a=1; a*a<b2; a++)
    2509           0 :       if (b2%a == 0) h++;
    2510           0 :     f = (a*a==b2); b=2; b2=(4+d)>>2;
    2511             :   }
    2512        7168 :   while (b2*3 < d)
    2513             :   {
    2514        7098 :     if (b2%b == 0) h++;
    2515     1188551 :     for (a=b+1; a*a < b2; a++)
    2516     1181453 :       if (b2%a == 0) h += 2;
    2517        7098 :     if (a*a == b2) h++;
    2518        7098 :     b += 2; b2 = (b*b+d)>>2;
    2519             :   }
    2520          35 :   if (b2*3 == d) return 6*h+2;
    2521          35 :   if (f) return 6*h+3;
    2522          35 :   return 6*h;
    2523             : }
    2524             : /* D > 0; 6 * hclassno(D), using D = D0*F^2 */
    2525             : static long
    2526         336 : hclassno6u_2(ulong D, long D0, long F)
    2527             : {
    2528             :   long h;
    2529         336 :   if (F == 1) h = hclassno6u_count(D);
    2530             :   else
    2531             :   { /* second chance */
    2532         294 :     h = (ulong)cache_get(cache_H, -D0);
    2533         294 :     if (!h) h = hclassno6u_count(-D0);
    2534         294 :     h *= get_sh(F,D0);
    2535             :   }
    2536         336 :   return h;
    2537             : }
    2538             : /* D > 0; 6 * hclassno(D) (6*Hurwitz). Beware, cached value for D (=0,3 mod 4)
    2539             :  * is stored at D>>1 */
    2540             : ulong
    2541      155745 : hclassno6u(ulong D)
    2542             : {
    2543      155745 :   ulong z = (ulong)cache_get(cache_H, D);
    2544             :   long D0, F;
    2545      155745 :   if (z) return z;
    2546         336 :   D0 = mycoredisc2neg(D, &F);
    2547         336 :   return hclassno6u_2(D,D0,F);
    2548             : }
    2549             : /* same, where the decomposition D = D0*F^2 is already known */
    2550             : static ulong
    2551    69622273 : hclassno6u_i(ulong D, long D0, long F)
    2552             : {
    2553    69622273 :   ulong z = (ulong)cache_get(cache_H, D);
    2554    69622273 :   if (z) return z;
    2555           0 :   return hclassno6u_2(D,D0,F);
    2556             : }
    2557             : 
    2558             : #if 0
    2559             : /* D > 0, return h(-D) [ordinary class number].
    2560             :  * Assume consttabh(D or more) was previously called */
    2561             : static long
    2562             : hfromH(long D)
    2563             : {
    2564             :   pari_sp ltop = avma;
    2565             :   GEN m, d, fa = myfactoru(D), P = gel(fa,1), E = gel(fa,2);
    2566             :   GEN VH = caches[cache_H].cache;
    2567             :   long i, nd, S, l = lg(P);
    2568             : 
    2569             :   /* n = d[i] loops through squarefree divisors of f, where f^2 = largest square
    2570             :    * divisor of N = |D|; m[i] = moebius(n) */
    2571             :   nd = 1 << (l-1);
    2572             :   d = cgetg(nd+1, t_VECSMALL);
    2573             :   m = cgetg(nd+1, t_VECSMALL);
    2574             :   d[1] = 1; S = VH[D >> 1]; /* 6 hclassno(-D) */
    2575             :   m[1] = 1; nd = 1;
    2576             :   i = 1;
    2577             :   if (P[1] == 2 && E[1] <= 3) /* need D/n^2 to be a discriminant */
    2578             :   { if (odd(E[1]) || (E[1] == 2 && (D & 15) == 4)) i = 2; }
    2579             :   for (; i<l; i++)
    2580             :   {
    2581             :     long j, p = P[i];
    2582             :     if (E[i] == 1) continue;
    2583             :     for (j=1; j<=nd; j++)
    2584             :     {
    2585             :       long n, s, hn;
    2586             :       d[nd+j] = n = d[j] * p;
    2587             :       m[nd+j] = s = - m[j]; /* moebius(n) */
    2588             :       hn = VH[(D/(n*n)) >> 1]; /* 6 hclassno(-D/n^2) */
    2589             :       if (s > 0) S += hn; else S -= hn;
    2590             :     }
    2591             :     nd <<= 1;
    2592             :   }
    2593             :   return gc_long(ltop, S/6);
    2594             : }
    2595             : #endif
    2596             : /* D < -4 fundamental, h(D), ordinary class number */
    2597             : static long
    2598     6654543 : myh(long D)
    2599             : {
    2600     6654543 :   ulong z = (ulong)cache_get(cache_H, -D);
    2601     6654543 :   if (z) return z/6; /* should be hfromH(-D) if D non-fundamental */
    2602           0 :   return itou(quadclassno(stoi(D)));
    2603             : }
    2604             : 
    2605             : /*************************************************************************/
    2606             : /*                          TRACE FORMULAS                               */
    2607             : /* CHIP primitive, initialize for t_POLMOD output */
    2608             : static GEN
    2609       28399 : mfcharinit(GEN CHIP)
    2610             : {
    2611       28399 :   long n, o, l, vt, N = mfcharmodulus(CHIP);
    2612             :   GEN c, v, V, G, Pn;
    2613       28399 :   if (N == 1) return mkvec2(mkvec(gen_1), pol_x(0));
    2614        4025 :   G = gel(CHIP,1);
    2615        4025 :   v = ncharvecexpo(G, znconrey_normalized(G, gel(CHIP,2)));
    2616        4025 :   l = lg(v); V = cgetg(l, t_VEC);
    2617        4025 :   o = mfcharorder(CHIP);
    2618        4025 :   Pn = mfcharpol(CHIP); vt = varn(Pn);
    2619        4025 :   if (o <= 2)
    2620             :   {
    2621       29176 :     for (n = 1; n < l; n++)
    2622             :     {
    2623       26103 :       if (v[n] < 0) c = gen_0; else c = v[n]? gen_m1: gen_1;
    2624       26103 :       gel(V,n) = c;
    2625             :     }
    2626             :   }
    2627             :   else
    2628             :   {
    2629       16835 :     for (n = 1; n < l; n++)
    2630             :     {
    2631       15883 :       if (v[n] < 0) c = gen_0;
    2632             :       else
    2633             :       {
    2634        8890 :         c = mygmodulo_lift(v[n], o, gen_1, vt);
    2635        8890 :         if (typ(c) == t_POL && lg(c) >= lg(Pn)) c = RgX_rem(c, Pn);
    2636             :       }
    2637       15883 :       gel(V,n) = c;
    2638             :     }
    2639             :   }
    2640        4025 :   return mkvec2(V, Pn);
    2641             : }
    2642             : static GEN
    2643      421099 : vchip_lift(GEN VCHI, long x, GEN C)
    2644             : {
    2645      421099 :   GEN V = gel(VCHI,1);
    2646      421099 :   long F = lg(V)-1;
    2647      421099 :   if (F == 1) return C;
    2648       27398 :   x %= F;
    2649       27398 :   if (!x) return C;
    2650       27398 :   if (x <= 0) x += F;
    2651       27398 :   return gmul(C, gel(V, x));
    2652             : }
    2653             : static long
    2654   129507168 : vchip_FC(GEN VCHI) { return lg(gel(VCHI,1))-1; }
    2655             : static GEN
    2656     4734814 : vchip_mod(GEN VCHI, GEN S)
    2657     4734814 : { return (typ(S) == t_POL)? RgX_rem(S, gel(VCHI,2)): S; }
    2658             : static GEN
    2659     1475033 : vchip_polmod(GEN VCHI, GEN S)
    2660     1475033 : { return (typ(S) == t_POL)? mkpolmod(S, gel(VCHI,2)): S; }
    2661             : 
    2662             : /* ceil(m/d) */
    2663             : static long
    2664      134981 : ceildiv(long m, long d)
    2665             : {
    2666             :   long q;
    2667      134981 :   if (!m) return 0;
    2668       40341 :   q = m/d; return m%d? q+1: q;
    2669             : }
    2670             : 
    2671             : /* contribution of scalar matrices in dimension formula */
    2672             : static GEN
    2673      297759 : A1(long N, long k)
    2674      297759 : { return sstoQ(mypsiu(N)*(k-1), 12); }
    2675             : static long
    2676        7462 : ceilA1(long N, long k)
    2677        7462 : { return ceildiv(mypsiu(N) * (k-1), 12); }
    2678             : 
    2679             : /* sturm bound, slightly larger than dimension */
    2680             : long
    2681       29512 : mfsturmNk(long N, long k) { return 1 + (mypsiu(N)*k)/12; }
    2682             : long
    2683        1932 : mfsturmNgk(long N, GEN k)
    2684             : {
    2685        1932 :   long n,d; Qtoss(k,&n,&d);
    2686        1932 :   return (d == 1)? mfsturmNk(N,n): 1 + (mypsiu(N)*n)/24;
    2687             : }
    2688             : 
    2689             : /* List of all solutions of x^2 + x + 1 = 0 modulo N, x modulo N */
    2690             : static GEN
    2691         511 : sqrtm3modN(long N)
    2692             : {
    2693             :   pari_sp av;
    2694             :   GEN fa, P, E, B, mB, A, Q, T, R, v, gen_m3;
    2695         511 :   long l, i, n, ct, fl3 = 0, Ninit;
    2696         511 :   if (!odd(N) || (N%9) == 0) return cgetg(1,t_VECSMALL);
    2697         483 :   Ninit = N;
    2698         483 :   if ((N%3) == 0) { N /= 3; fl3 = 1; }
    2699         483 :   fa = myfactoru(N); P = gel(fa, 1); E = gel(fa, 2);
    2700         483 :   l = lg(P);
    2701         679 :   for (i = 1; i < l; i++)
    2702         490 :     if ((P[i]%3) == 2) return cgetg(1,t_VECSMALL);
    2703         189 :   A = cgetg(l, t_VECSMALL);
    2704         189 :   B = cgetg(l, t_VECSMALL);
    2705         189 :   mB= cgetg(l, t_VECSMALL);
    2706         189 :   Q = cgetg(l, t_VECSMALL); gen_m3 = utoineg(3);
    2707         385 :   for (i = 1; i < l; i++)
    2708             :   {
    2709         196 :     long p = P[i], e = E[i];
    2710         196 :     Q[i] = upowuu(p,e);
    2711         196 :     B[i] = itou( Zp_sqrt(gen_m3, utoipos(p), e) );
    2712         196 :     mB[i]= Q[i] - B[i];
    2713             :   }
    2714         189 :   ct = 1 << (l-1);
    2715         189 :   T = ZV_producttree(Q);
    2716         189 :   R = ZV_chinesetree(Q,T);
    2717         189 :   v = cgetg(ct+1, t_VECSMALL);
    2718         189 :   av = avma;
    2719         581 :   for (n = 1; n <= ct; n++)
    2720             :   {
    2721         392 :     long m = n-1, r;
    2722         812 :     for (i = 1; i < l; i++)
    2723             :     {
    2724         420 :       A[i] = (m&1L)? mB[i]: B[i];
    2725         420 :       m >>= 1;
    2726             :     }
    2727         392 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2728         392 :     if (fl3) while (r%3) r += N;
    2729         392 :     set_avma(av); v[n] = odd(r) ? (r-1) >> 1 : (r+Ninit-1) >> 1;
    2730             :   }
    2731         189 :   return v;
    2732             : }
    2733             : 
    2734             : /* number of elliptic points of order 3 in X0(N) */
    2735             : static long
    2736        9597 : nu3(long N)
    2737             : {
    2738             :   long i, l;
    2739             :   GEN P;
    2740        9597 :   if (!odd(N) || (N%9) == 0) return 0;
    2741        8547 :   if ((N%3) == 0) N /= 3;
    2742        8547 :   P = gel(myfactoru(N), 1); l = lg(P);
    2743        8547 :   for (i = 1; i < l; i++) if ((P[i]%3) == 2) return 0;
    2744        3857 :   return 1L<<(l-1);
    2745             : }
    2746             : /* number of elliptic points of order 2 in X0(N) */
    2747             : static long
    2748       16555 : nu2(long N)
    2749             : {
    2750             :   long i, l;
    2751             :   GEN P;
    2752       16555 :   if ((N&3L) == 0) return 0;
    2753       16555 :   if (!odd(N)) N >>= 1;
    2754       16555 :   P = gel(myfactoru(N), 1); l = lg(P);
    2755       16555 :   for (i = 1; i < l; i++) if ((P[i]&3L) == 3) return 0;
    2756        3766 :   return 1L<<(l-1);
    2757             : }
    2758             : 
    2759             : /* contribution of elliptic matrices of order 3 in dimension formula
    2760             :  * Only depends on CHIP the primitive char attached to CHI */
    2761             : static GEN
    2762       40782 : A21(long N, long k, GEN CHI)
    2763             : {
    2764             :   GEN res, G, chi, o;
    2765             :   long a21, i, limx, S;
    2766       40782 :   if ((N&1L) == 0) return gen_0;
    2767       19957 :   a21 = k%3 - 1;
    2768       19957 :   if (!a21) return gen_0;
    2769       19313 :   if (N <= 3) return sstoQ(a21, 3);
    2770       10108 :   if (!CHI) return sstoQ(nu3(N) * a21, 3);
    2771         511 :   res = sqrtm3modN(N); limx = (N - 1) >> 1;
    2772         511 :   G = gel(CHI,1); chi = gel(CHI,2);
    2773         511 :   o = gmfcharorder(CHI);
    2774         903 :   for (S = 0, i = 1; i < lg(res); i++)
    2775             :   { /* (x,N) = 1; S += chi(x) + chi(x^2) */
    2776         392 :     long x = res[i];
    2777         392 :     if (x <= limx)
    2778             :     { /* CHI(x)=e(c/o), 3rd-root of 1 */
    2779         196 :       GEN c = znchareval(G, chi, utoi(x), o);
    2780         196 :       if (!signe(c)) S += 2; else S--;
    2781             :     }
    2782             :   }
    2783         511 :   return sstoQ(a21 * S, 3);
    2784             : }
    2785             : 
    2786             : /* List of all square roots of -1 modulo N */
    2787             : static GEN
    2788         574 : sqrtm1modN(long N)
    2789             : {
    2790             :   pari_sp av;
    2791             :   GEN fa, P, E, B, mB, A, Q, T, R, v;
    2792         574 :   long l, i, n, ct, fleven = 0;
    2793         574 :   if ((N&3L) == 0) return cgetg(1,t_VECSMALL);
    2794         574 :   if ((N&1L) == 0) { N >>= 1; fleven = 1; }
    2795         574 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    2796         574 :   l = lg(P);
    2797         924 :   for (i = 1; i < l; i++)
    2798         644 :     if ((P[i]&3L) == 3) return cgetg(1,t_VECSMALL);
    2799         280 :   A = cgetg(l, t_VECSMALL);
    2800         280 :   B = cgetg(l, t_VECSMALL);
    2801         280 :   mB= cgetg(l, t_VECSMALL);
    2802         280 :   Q = cgetg(l, t_VECSMALL);
    2803         574 :   for (i = 1; i < l; i++)
    2804             :   {
    2805         294 :     long p = P[i], e = E[i];
    2806         294 :     Q[i] = upowuu(p,e);
    2807         294 :     B[i] = itou( Zp_sqrt(gen_m1, utoipos(p), e) );
    2808         294 :     mB[i]= Q[i] - B[i];
    2809             :   }
    2810         280 :   ct = 1 << (l-1);
    2811         280 :   T = ZV_producttree(Q);
    2812         280 :   R = ZV_chinesetree(Q,T);
    2813         280 :   v = cgetg(ct+1, t_VECSMALL);
    2814         280 :   av = avma;
    2815         868 :   for (n = 1; n <= ct; n++)
    2816             :   {
    2817         588 :     long m = n-1, r;
    2818        1232 :     for (i = 1; i < l; i++)
    2819             :     {
    2820         644 :       A[i] = (m&1L)? mB[i]: B[i];
    2821         644 :       m >>= 1;
    2822             :     }
    2823         588 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2824         588 :     if (fleven && !odd(r)) r += N;
    2825         588 :     set_avma(av); v[n] = r;
    2826             :   }
    2827         280 :   return v;
    2828             : }
    2829             : 
    2830             : /* contribution of elliptic matrices of order 4 in dimension formula.
    2831             :  * Only depends on CHIP the primitive char attached to CHI */
    2832             : static GEN
    2833       40782 : A22(long N, long k, GEN CHI)
    2834             : {
    2835             :   GEN G, chi, o, res;
    2836             :   long S, a22, i, limx, o2;
    2837       40782 :   if ((N&3L) == 0) return gen_0;
    2838       28532 :   a22 = (k & 3L) - 1; /* (k % 4) - 1 */
    2839       28532 :   if (!a22) return gen_0;
    2840       28532 :   if (N <= 2) return sstoQ(a22, 4);
    2841       17332 :   if (!CHI) return sstoQ(nu2(N)*a22, 4);
    2842         777 :   if (mfcharparity(CHI) == -1) return gen_0;
    2843         574 :   res = sqrtm1modN(N); limx = (N - 1) >> 1;
    2844         574 :   G = gel(CHI,1); chi = gel(CHI,2);
    2845         574 :   o = gmfcharorder(CHI);
    2846         574 :   o2 = itou(o)>>1;
    2847        1162 :   for (S = 0, i = 1; i < lg(res); i++)
    2848             :   { /* (x,N) = 1, S += real(chi(x)) */
    2849         588 :     long x = res[i];
    2850         588 :     if (x <= limx)
    2851             :     { /* CHI(x)=e(c/o), 4th-root of 1 */
    2852         294 :       long c = itou( znchareval(G, chi, utoi(x), o) );
    2853         294 :       if (!c) S++; else if (c == o2) S--;
    2854             :     }
    2855             :   }
    2856         574 :   return sstoQ(a22 * S, 2);
    2857             : }
    2858             : 
    2859             : /* sumdiv(N,d,eulerphi(gcd(d,N/d))) */
    2860             : static long
    2861       36834 : nuinf(long N)
    2862             : {
    2863       36834 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    2864       36834 :   long i, t = 1, l = lg(P);
    2865       78225 :   for (i=1; i<l; i++)
    2866             :   {
    2867       41391 :     long p = P[i], e = E[i];
    2868       41391 :     if (odd(e))
    2869       33180 :       t *= upowuu(p,e>>1) << 1;
    2870             :     else
    2871        8211 :       t *= upowuu(p,(e>>1)-1) * (p+1);
    2872             :   }
    2873       36834 :   return t;
    2874             : }
    2875             : 
    2876             : /* contribution of hyperbolic matrices in dimension formula */
    2877             : static GEN
    2878       41181 : A3(long N, long FC)
    2879             : {
    2880             :   long i, S, NF, l;
    2881             :   GEN D;
    2882       41181 :   if (FC == 1) return sstoQ(nuinf(N),2);
    2883        4347 :   D = mydivisorsu(N); l = lg(D);
    2884        4347 :   S = 0; NF = N/FC;
    2885       33194 :   for (i = 1; i < l; i++)
    2886             :   {
    2887       28847 :     long g = ugcd(D[i], D[l-i]);
    2888       28847 :     if (NF%g == 0) S += myeulerphiu(g);
    2889             :   }
    2890        4347 :   return sstoQ(S, 2);
    2891             : }
    2892             : 
    2893             : /* special contribution in weight 2 in dimension formula */
    2894             : static long
    2895       40446 : A4(long k, long FC)
    2896       40446 : { return (k==2 && FC==1)? 1: 0; }
    2897             : /* gcd(x,N) */
    2898             : static long
    2899   154604443 : myugcd(GEN GCD, ulong x)
    2900             : {
    2901   154604443 :   ulong N = lg(GCD)-1;
    2902   154604443 :   if (x >= N) x %= N;
    2903   154604443 :   return GCD[x+1];
    2904             : }
    2905             : /* 1_{gcd(x,N) = 1} * chi(x), return NULL if 0 */
    2906             : static GEN
    2907   202771128 : mychicgcd(GEN GCD, GEN VCHI, long x)
    2908             : {
    2909   202771128 :   long N = lg(GCD)-1;
    2910   202771128 :   if (N == 1) return gen_1;
    2911   166609821 :   x = smodss(x, N);
    2912   166609821 :   if (GCD[x+1] != 1) return NULL;
    2913   124100284 :   x %= vchip_FC(VCHI); if (!x) return gen_1;
    2914     6835934 :   return gel(gel(VCHI,1), x);
    2915             : }
    2916             : 
    2917             : /* contribution of scalar matrices to trace formula */
    2918             : static GEN
    2919     4637304 : TA1(long N, long k, GEN VCHI, GEN GCD, long n)
    2920             : {
    2921             :   GEN S;
    2922             :   ulong m;
    2923     4637304 :   if (!uissquareall(n, &m)) return gen_0;
    2924      322119 :   if (m == 1) return A1(N,k); /* common */
    2925      287546 :   S = mychicgcd(GCD, VCHI, m);
    2926      287546 :   return S? gmul(gmul(powuu(m, k-2), A1(N,k)), S): gen_0;
    2927             : }
    2928             : 
    2929             : /* All square roots modulo 4N, x modulo 2N, precomputed to accelerate TA2 */
    2930             : static GEN
    2931      113232 : mksqr(long N)
    2932             : {
    2933      113232 :   pari_sp av = avma;
    2934      113232 :   long x, N2 = N << 1, N4 = N << 2;
    2935      113232 :   GEN v = const_vec(N2, cgetg(1, t_VECSMALL));
    2936      113232 :   gel(v, N2) = mkvecsmall(0); /* x = 0 */
    2937     2863077 :   for (x = 1; x <= N; x++)
    2938             :   {
    2939     2749845 :     long r = (((x*x - 1)%N4) >> 1) + 1;
    2940     2749845 :     gel(v,r) = vecsmall_append(gel(v,r), x);
    2941             :   }
    2942      113232 :   return gerepilecopy(av, v);
    2943             : }
    2944             : 
    2945             : static GEN
    2946      113232 : mkgcd(long N)
    2947             : {
    2948             :   GEN GCD, d;
    2949             :   long i, N2;
    2950      113232 :   if (N == 1) return mkvecsmall(N);
    2951       93072 :   GCD = cgetg(N + 1, t_VECSMALL);
    2952       93072 :   d = GCD+1; /* GCD[i+1] = d[i] = gcd(i,N) = gcd(N-i,N), i = 0..N-1 */
    2953       93072 :   d[0] = N; d[1] = d[N-1] = 1; N2 = N>>1;
    2954       93072 :   for (i = 2; i <= N2; i++) d[i] = d[N-i] = ugcd(N, i);
    2955       93072 :   return GCD;
    2956             : }
    2957             : 
    2958             : /* Table of \sum_{x^2-tx+n=0 mod Ng}chi(x) for all g dividing gcd(N,F),
    2959             :  * F^2 largest such that (t^2-4n)/F^2=0 or 1 mod 4; t >= 0 */
    2960             : static GEN
    2961    12545512 : mutglistall(long t, long N, long NF, GEN VCHI, long n, GEN MUP, GEN li, GEN GCD)
    2962             : {
    2963    12545512 :   long i, lx = lg(li);
    2964    12545512 :   GEN DNF = mydivisorsu(NF), v = zerovec(NF);
    2965    12545512 :   long j, g, lDNF = lg(DNF);
    2966    33263363 :   for (i = 1; i < lx; i++)
    2967             :   {
    2968    20717851 :     long x = (li[i] + t) >> 1, y, lD;
    2969    20717851 :     GEN D, c = mychicgcd(GCD, VCHI, x);
    2970    20717851 :     if (li[i] && li[i] != N)
    2971             :     {
    2972    13050968 :       GEN c2 = mychicgcd(GCD, VCHI, t - x);
    2973    13050968 :       if (c2) c = c? gadd(c, c2): c2;
    2974             :     }
    2975    20717851 :     if (!c) continue;
    2976    12900951 :     y = (x*(x - t) + n) / N; /* exact division */
    2977    12900951 :     D = mydivisorsu(ugcd(labs(y), NF)); lD = lg(D);
    2978    12900951 :     for (j=1; j < lD; j++) { g = D[j]; gel(v,g) = gadd(gel(v,g), c); }
    2979             :   }
    2980             :   /* j = 1 corresponds to g = 1, and MUP[1] = 1 */
    2981    12545512 :   for (j=2; j < lDNF; j++) { g = DNF[j]; gel(v,g) = gmulsg(MUP[g], gel(v,g)); }
    2982    12545512 :   return v;
    2983             : }
    2984             : 
    2985             : /* special case (N,F) = 1: easier */
    2986             : static GEN
    2987    72866710 : mutg1(long t, long N, GEN VCHI, GEN li, GEN GCD)
    2988             : { /* (N,F) = 1 */
    2989    72866710 :   GEN S = NULL;
    2990    72866710 :   long i, lx = lg(li);
    2991   152325544 :   for (i = 1; i < lx; i++)
    2992             :   {
    2993    79458834 :     long x = (li[i] + t) >> 1;
    2994    79458834 :     GEN c = mychicgcd(GCD, VCHI, x);
    2995    79458834 :     if (c) S = S? gadd(S, c): c;
    2996    79458834 :     if (li[i] && li[i] != N)
    2997             :     {
    2998    42133455 :       c = mychicgcd(GCD, VCHI, t - x);
    2999    42133455 :       if (c) S = S? gadd(S, c): c;
    3000             :     }
    3001    79458834 :     if (S && !signe(S)) S = NULL; /* strive hard to add gen_0 */
    3002             :   }
    3003    72866710 :   return S; /* single value */
    3004             : }
    3005             : 
    3006             : /* Gegenbauer pol; n > 2, P = \sum_{0<=j<=n/2} (-1)^j (n-j)!/j!(n-2*j)! X^j */
    3007             : static GEN
    3008      348838 : mfrhopol(long n)
    3009             : {
    3010             : #ifdef LONG_IS_64BIT
    3011      299004 :   const long M = 2642249;
    3012             : #else
    3013       49834 :   const long M = 1629;
    3014             : #endif
    3015      348838 :   long j, d = n >> 1; /* >= 1 */
    3016      348838 :   GEN P = cgetg(d + 3, t_POL);
    3017             : 
    3018      348838 :   if (n > M) pari_err_IMPL("mfrhopol for large weight"); /* avoid overflow */
    3019      348838 :   P[1] = evalvarn(0)|evalsigne(1);
    3020      348838 :   gel(P,2) = gen_1;
    3021      348838 :   gel(P,3) = utoineg(n-1); /* j = 1 */
    3022      348838 :   if (d > 1) gel(P,4) = utoipos(((n-3)*(n-2)) >> 1); /* j = 2 */
    3023      348838 :   if (d > 2) gel(P,5) = utoineg(((n-5)*(n-4)*(n-3)) / 6); /* j = 3 */
    3024     1287671 :   for (j = 4; j <= d; j++)
    3025      938833 :     gel(P,j+2) = divis(mulis(gel(P,j+1), (n-2*j+1)*(n-2*j+2)), (n-j+1)*(-j));
    3026      348838 :   return P;
    3027             : }
    3028             : 
    3029             : /* polrecip(Q)(t2), assume Q(0) = 1 */
    3030             : static GEN
    3031     2976596 : ZXrecip_u_eval(GEN Q, ulong t2)
    3032             : {
    3033     2976596 :   GEN T = addiu(gel(Q,3), t2);
    3034     2976596 :   long l = lg(Q), j;
    3035     2976596 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mului(t2, T));
    3036     2976596 :   return T;
    3037             : }
    3038             : /* return sh * sqrt(n)^nu * G_nu(t/(2*sqrt(n))) for t != 0
    3039             :  * else (sh/2) * sqrt(n)^nu * G_nu(0) [ implies nu is even ]
    3040             :  * G_nu(z) = \sum_{0<=j<=nu/2} (-1)^j (nu-j)!/j!(nu-2*j)! * (2z)^(nu-2*j)) */
    3041             : static GEN
    3042    77004221 : mfrhopowsimp(GEN Q, GEN sh, long nu, long t, long t2, long n)
    3043             : {
    3044             :   GEN T;
    3045    77004221 :   switch (nu)
    3046             :   {
    3047    69132910 :     case 0: return t? sh: gmul2n(sh,-1);
    3048     3462326 :     case 1: return gmulsg(t, sh);
    3049     1394197 :     case 2: return t? gmulsg(t2 - n, sh): gmul(gmul2n(stoi(-n), -1), sh);
    3050         469 :     case 3: return gmul(mulss(t, t2 - 2*n), sh);
    3051             :     default:
    3052     3014319 :       if (!t) return gmul(gmul2n(gel(Q, lg(Q) - 1), -1), sh);
    3053     2976596 :       T = ZXrecip_u_eval(Q, t2); if (odd(nu)) T = mulsi(t, T);
    3054     2976596 :       return gmul(T, sh);
    3055             :   }
    3056             : }
    3057             : 
    3058             : /* contribution of elliptic matrices to trace formula */
    3059             : static GEN
    3060     4637304 : TA2(long N, long k, GEN VCHI, long n, GEN SQRTS, GEN MUP, GEN GCD)
    3061             : {
    3062     4637304 :   const long n4 = n << 2, N4 = N << 2, nu = k - 2;
    3063     4637304 :   const long st = (!odd(N) && odd(n)) ? 2 : 1;
    3064             :   long limt, t;
    3065             :   GEN S, Q;
    3066             : 
    3067     4637304 :   limt = usqrt(n4);
    3068     4637304 :   if (limt*limt == n4) limt--;
    3069     4637304 :   Q = nu > 3 ? ZX_z_unscale(mfrhopol(nu), n) : NULL;
    3070     4637304 :   S = gen_0;
    3071   146669852 :   for (t = odd(k)? st: 0; t <= limt; t += st) /* t^2 < 4n */
    3072             :   {
    3073   142032548 :     pari_sp av = avma;
    3074   142032548 :     long t2 = t*t, D = n4 - t2, F, D0, NF;
    3075             :     GEN sh, li;
    3076             : 
    3077   142032548 :     li = gel(SQRTS, (smodss(-D - 1, N4) >> 1) + 1);
    3078   207060875 :     if (lg(li) == 1) continue;
    3079    85412222 :     D0 = mycoredisc2neg(D, &F);
    3080    85412222 :     NF = myugcd(GCD, F);
    3081    85412222 :     if (NF == 1)
    3082             :     { /* (N,F) = 1 => single value in mutglistall */
    3083    72866710 :       GEN mut = mutg1(t, N, VCHI, li, GCD);
    3084    72866710 :       if (!mut) { set_avma(av); continue; }
    3085    69622273 :       sh = gmul(sstoQ(hclassno6u_i(D,D0,F),6), mut);
    3086             :     }
    3087             :     else
    3088             :     {
    3089    12545512 :       GEN v = mutglistall(t, N, NF, VCHI, n, MUP, li, GCD);
    3090    12545512 :       GEN DF = mydivisorsu(F);
    3091    12545512 :       long i, lDF = lg(DF);
    3092    12545512 :       sh = gen_0;
    3093    48497540 :       for (i = 1; i < lDF; i++)
    3094             :       {
    3095    35952028 :         long Ff, f = DF[i], g = myugcd(GCD, f);
    3096    35952028 :         GEN mut = gel(v, g);
    3097    35952028 :         if (gequal0(mut)) continue;
    3098    18292379 :         Ff = DF[lDF-i]; /* F/f */
    3099    18292379 :         if (Ff == 1) sh = gadd(sh, mut);
    3100             :         else
    3101             :         {
    3102    12946612 :           GEN P = gel(myfactoru(Ff), 1);
    3103    12946612 :           long j, lP = lg(P);
    3104    12946612 :           for (j = 1; j < lP; j++) { long p = P[j]; Ff -= kross(D0, p)*Ff/p; }
    3105    12946612 :           sh = gadd(sh, gmulsg(Ff, mut));
    3106             :         }
    3107             :       }
    3108    12545512 :       if (gequal0(sh)) { set_avma(av); continue; }
    3109     7381948 :       if (D0 == -3) sh = gdivgs(sh, 3);
    3110     7004326 :       else if (D0 == -4) sh = gdivgs(sh, 2);
    3111     6654543 :       else sh = gmulgs(sh, myh(D0));
    3112             :     }
    3113    77004221 :     S = gerepileupto(av, gadd(S, mfrhopowsimp(Q,sh,nu,t,t2,n)));
    3114             :   }
    3115     4637304 :   return S;
    3116             : }
    3117             : 
    3118             : /* compute global auxiliary data for TA3 */
    3119             : static GEN
    3120      113232 : mkbez(long N, long FC)
    3121             : {
    3122      113232 :   long ct, i, NF = N/FC;
    3123      113232 :   GEN w, D = mydivisorsu(N);
    3124      113232 :   long l = lg(D);
    3125             : 
    3126      113232 :   w = cgetg(l, t_VEC);
    3127      323596 :   for (i = ct = 1; i < l; i++)
    3128             :   {
    3129      303436 :     long u, v, h, c = D[i], Nc = D[l-i];
    3130      303436 :     if (c > Nc) break;
    3131      210364 :     h = cbezout(c, Nc, &u, &v);
    3132      210364 :     if (h == 1) /* shortcut */
    3133      154721 :       gel(w, ct++) = mkvecsmall4(1,u*c,1,i);
    3134       55643 :     else if (!(NF%h))
    3135       49987 :       gel(w, ct++) = mkvecsmall4(h,u*(c/h),myeulerphiu(h),i);
    3136             :   }
    3137      113232 :   setlg(w,ct); stackdummy((pari_sp)(w+ct),(pari_sp)(w+l));
    3138      113232 :   return w;
    3139             : }
    3140             : 
    3141             : /* contribution of hyperbolic matrices to trace formula, d * nd = n,
    3142             :  * DN = divisorsu(N) */
    3143             : static GEN
    3144    19737053 : auxsum(GEN VCHI, GEN GCD, long d, long nd, GEN DN, GEN BEZ)
    3145             : {
    3146    19737053 :   GEN S = gen_0;
    3147    19737053 :   long ct, g = nd - d, lDN = lg(DN), lBEZ = lg(BEZ);
    3148    51899792 :   for (ct = 1; ct < lBEZ; ct++)
    3149             :   {
    3150    32162739 :     GEN y, B = gel(BEZ, ct);
    3151    32162739 :     long ic, c, Nc, uch, h = B[1];
    3152    32162739 :     if (g%h) continue;
    3153    31482066 :     uch = B[2];
    3154    31482066 :     ic  = B[4];
    3155    31482066 :     c = DN[ic];
    3156    31482066 :     Nc= DN[lDN - ic]; /* Nc = N/c */
    3157    31482066 :     if (ugcd(Nc, nd) == 1)
    3158    24912636 :       y = mychicgcd(GCD, VCHI, d + uch*g); /* 0 if (c,d) > 1 */
    3159             :     else
    3160     6569430 :       y = NULL;
    3161    31482066 :     if (c != Nc && ugcd(Nc, d) == 1)
    3162             :     {
    3163    22209838 :       GEN y2 = mychicgcd(GCD, VCHI, nd - uch*g); /* 0 if (c,nd) > 1 */
    3164    22209838 :       if (y2) y = y? gadd(y, y2): y2;
    3165             :     }
    3166    31482066 :     if (y) S = gadd(S, gmulsg(B[3], y));
    3167             :   }
    3168    19737053 :   return S;
    3169             : }
    3170             : 
    3171             : static GEN
    3172     4637304 : TA3(long N, long k, GEN VCHI, GEN GCD, GEN Dn, GEN BEZ)
    3173             : {
    3174     4637304 :   GEN S = gen_0, DN = mydivisorsu(N);
    3175     4637304 :   long i, l = lg(Dn);
    3176    24374357 :   for (i = 1; i < l; i++)
    3177             :   {
    3178    24339784 :     long d = Dn[i], nd = Dn[l-i]; /* = n/d */
    3179             :     GEN t, u;
    3180    24339784 :     if (d > nd) break;
    3181    19737053 :     t = auxsum(VCHI, GCD, d, nd, DN, BEZ);
    3182    19737053 :     if (isintzero(t)) continue;
    3183    18433730 :     u = powuu(d,k-1); if (d == nd) u = gmul2n(u,-1);
    3184    18433730 :     S = gadd(S, gmul(u,t));
    3185             :   }
    3186     4637304 :   return S;
    3187             : }
    3188             : 
    3189             : /* special contribution in weight 2 in trace formula */
    3190             : static long
    3191     4637304 : TA4(long k, GEN VCHIP, GEN Dn, GEN GCD)
    3192             : {
    3193             :   long i, l, S;
    3194     4637304 :   if (k != 2 || vchip_FC(VCHIP) != 1) return 0;
    3195     3953250 :   l = lg(Dn); S = 0;
    3196    37193443 :   for (i = 1; i < l; i++)
    3197             :   {
    3198    33240193 :     long d = Dn[i]; /* gcd(N,n/d) == 1? */
    3199    33240193 :     if (myugcd(GCD, Dn[l-i]) == 1) S += d;
    3200             :   }
    3201     3953250 :   return S;
    3202             : }
    3203             : 
    3204             : /* precomputation of products occurring im mutg, again to accelerate TA2 */
    3205             : static GEN
    3206      113232 : mkmup(long N)
    3207             : {
    3208      113232 :   GEN fa = myfactoru(N), P = gel(fa,1), D = divisorsu_fact(fa);
    3209      113232 :   long i, lP = lg(P), lD = lg(D);
    3210      113232 :   GEN MUP = zero_zv(N);
    3211      113232 :   MUP[1] = 1;
    3212      387373 :   for (i = 2; i < lD; i++)
    3213             :   {
    3214      274141 :     long j, g = D[i], Ng = D[lD-i]; /*  N/g */
    3215      274141 :     for (j = 1; j < lP; j++) { long p = P[j]; if (Ng%p) g += g/p; }
    3216      274141 :     MUP[D[i]] = g;
    3217             :   }
    3218      113232 :   return MUP;
    3219             : }
    3220             : 
    3221             : /* quadratic non-residues mod p; p odd prime, p^2 fits in a long */
    3222             : static GEN
    3223        1400 : non_residues(long p)
    3224             : {
    3225        1400 :   long i, j, p2 = p >> 1;
    3226        1400 :   GEN v = cgetg(p2+1, t_VECSMALL), w = const_vecsmall(p-1, 1);
    3227        1400 :   for (i = 2; i <= p2; i++) w[(i*i) % p] = 0; /* no need to check 1 */
    3228        1400 :   for (i = 2, j = 1; i < p; i++) if (w[i]) v[j++] = i;
    3229        1400 :   return v;
    3230             : }
    3231             : 
    3232             : /* CHIP primitive. Return t_VECSMALL v of length q such that
    3233             :  * Tr^new_{N,CHIP}(n) = 0 whenever v[(n%q) + 1] is non-zero */
    3234             : static GEN
    3235       28427 : mfnewzerodata(long N, GEN CHIP)
    3236             : {
    3237       28427 :   GEN V, M, L, faN = myfactoru(N), PN = gel(faN,1), EN = gel(faN,2);
    3238       28427 :   GEN G = gel(CHIP,1), chi = gel(CHIP,2);
    3239       28427 :   GEN fa = znstar_get_faN(G), P = ZV_to_zv(gel(fa,1)), E = gel(fa,2);
    3240       28427 :   long i, mod, j = 1, l = lg(PN);
    3241             : 
    3242       28427 :   M = cgetg(l, t_VECSMALL); M[1] = 0;
    3243       28427 :   V = cgetg(l, t_VEC);
    3244             :   /* Tr^new(n) = 0 if (n mod M[i]) in V[i]  */
    3245       28427 :   if ((N & 3) == 0)
    3246             :   {
    3247       10668 :     long e = EN[1];
    3248       10668 :     long c = (lg(P) > 1 && P[1] == 2)? E[1]: 0; /* c = v_2(FC) */
    3249             :     /* e >= 2 */
    3250       10668 :     if (c == e-1) return NULL; /* Tr^new = 0 */
    3251       10633 :     if (c == e)
    3252             :     {
    3253        2422 :       if (e == 2)
    3254             :       { /* sc: -4 */
    3255        1652 :         gel(V,1) = mkvecsmall(3);
    3256        1652 :         M[1] = 4;
    3257             :       }
    3258         770 :       else if (e == 3)
    3259             :       { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3260         770 :         long t = signe(gel(chi,1))? 7: 3;
    3261         770 :         gel(V,1) = mkvecsmall2(5, t);
    3262         770 :         M[1] = 8;
    3263             :       }
    3264             :     }
    3265        8211 :     else if (e == 5 && c == 3)
    3266         154 :     { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3267         154 :       long t = signe(gel(chi,1))? 7: 3;
    3268         154 :       gel(V,1) = mkvecsmalln(6, 2L,4L,5L,6L,8L,t);
    3269         154 :       M[1] = 8;
    3270             :     }
    3271        8057 :     else if ((e == 4 && c == 2) || (e == 5 && c <= 2) || (e == 6 && c <= 2)
    3272        6629 :          || (e >= 7 && c == e - 3))
    3273             :     { /* sc: 4 */
    3274        1428 :       gel(V,1) = mkvecsmall3(0,2,3);
    3275        1428 :       M[1] = 4;
    3276             :     }
    3277        6629 :     else if ((e <= 4 && c == 0) || (e >= 5 && c == e - 2))
    3278             :     { /* sc: 2 */
    3279        6363 :       gel(V,1) = mkvecsmall(0);
    3280        6363 :       M[1] = 2;
    3281             :     }
    3282         266 :     else if ((e == 6 && c == 3) || (e >= 7 && c <= e - 4))
    3283             :     { /* sc: -2 */
    3284         266 :       gel(V,1) = mkvecsmalln(7, 0L,2L,3L,4L,5L,6L,7L);
    3285         266 :       M[1] = 8;
    3286             :     }
    3287             :   }
    3288       28392 :   j = M[1]? 2: 1;
    3289       61201 :   for (i = odd(N)? 1: 2; i < l; i++) /* skip p=2, done above */
    3290             :   {
    3291       32809 :     long p = PN[i], e = EN[i];
    3292       32809 :     long z = zv_search(P, p), c = z? E[z]: 0; /* c = v_p(FC) */
    3293       32809 :     if ((e <= 2 && c == 1 && itos(gel(chi,z)) == (p>>1)) /* ord(CHI_p)=2 */
    3294       31871 :         || (e >= 3 && c <= e - 2))
    3295        1400 :     { /* sc: -p */
    3296        1400 :       GEN v = non_residues(p);
    3297        1400 :       if (e != 1) v = vecsmall_prepend(v, 0);
    3298        1400 :       gel(V,j) = v;
    3299        1400 :       M[j] = p; j++;
    3300             :     }
    3301       31409 :     else if (e >= 2 && c < e)
    3302             :     { /* sc: p */
    3303        2107 :       gel(V,j) = mkvecsmall(0);
    3304        2107 :       M[j] = p; j++;
    3305             :     }
    3306             :   }
    3307       28392 :   if (j == 1) return cgetg(1, t_VECSMALL);
    3308       12614 :   setlg(V,j); setlg(M,j); mod = zv_prod(M);
    3309       12614 :   L = zero_zv(mod);
    3310       26754 :   for (i = 1; i < j; i++)
    3311             :   {
    3312       14140 :     GEN v = gel(V,i);
    3313       14140 :     long s, m = M[i], lv = lg(v);
    3314       36400 :     for (s = 1; s < lv; s++)
    3315             :     {
    3316       22260 :       long a = v[s] + 1;
    3317       32046 :       do { L[a] = 1; a += m; } while (a <= mod);
    3318             :     }
    3319             :   }
    3320       12614 :   return L;
    3321             : }
    3322             : /* v=mfnewzerodata(N,CHI); returns TRUE if newtrace(n) must be zero,
    3323             :  * (but newtrace(n) may still be zero if we return FALSE) */
    3324             : static long
    3325     1860670 : mfnewchkzero(GEN v, long n) { long q = lg(v)-1; return q && v[(n%q) + 1]; }
    3326             : 
    3327             : /* if (!VCHIP): from mftraceform_cusp;
    3328             :  * else from initnewtrace and CHI is known to be primitive */
    3329             : static GEN
    3330      113232 : inittrace(long N, GEN CHI, GEN VCHIP)
    3331             : {
    3332             :   long FC;
    3333      113232 :   if (VCHIP)
    3334      113225 :     FC = mfcharmodulus(CHI);
    3335             :   else
    3336           7 :     VCHIP = mfcharinit(mfchartoprimitive(CHI, &FC));
    3337      113232 :   return mkvecn(5, mksqr(N), mkmup(N), mkgcd(N), VCHIP, mkbez(N, FC));
    3338             : }
    3339             : 
    3340             : /* p > 2 prime; return a sorted t_VECSMALL of primes s.t Tr^new(p) = 0 for all
    3341             :  * weights > 2 */
    3342             : static GEN
    3343       28392 : inittrconj(long N, long FC)
    3344             : {
    3345             :   GEN fa, P, E, v;
    3346             :   long i, k, l;
    3347             : 
    3348       28392 :   if (FC != 1) return cgetg(1,t_VECSMALL);
    3349             : 
    3350       24367 :   fa = myfactoru(N >> vals(N));
    3351       24367 :   P = gel(fa,1); l = lg(P);
    3352       24367 :   E = gel(fa,2);
    3353       24367 :   v = cgetg(l, t_VECSMALL);
    3354       53487 :   for (i = k = 1; i < l; i++)
    3355             :   {
    3356       29120 :     long j, p = P[i]; /* > 2 */
    3357       70644 :     for (j = 1; j < l; j++)
    3358       41524 :       if (j != i && E[j] == 1 && kross(-p, P[j]) == 1) v[k++] = p;
    3359             :   }
    3360       24367 :   setlg(v,k); return v;
    3361             : }
    3362             : 
    3363             : /* assume CHIP primitive, f(CHIP) | N; NZ = mfnewzerodata(N,CHIP) */
    3364             : static GEN
    3365       28392 : initnewtrace_i(long N, GEN CHIP, GEN NZ)
    3366             : {
    3367       28392 :   GEN T = const_vec(N, cgetg(1,t_VEC)), D, VCHIP;
    3368       28392 :   long FC = mfcharmodulus(CHIP), N1, N2, i, l;
    3369             : 
    3370       28392 :   if (!NZ) NZ = mkvecsmall(1); /*Tr^new = 0; initialize data nevertheless*/
    3371       28392 :   VCHIP = mfcharinit(CHIP);
    3372       28392 :   N1 = N/FC; newd_params(N1, &N2);
    3373       28392 :   D = mydivisorsu(N1/N2); l = lg(D);
    3374       28392 :   N2 *= FC;
    3375      141617 :   for (i = 1; i < l; i++)
    3376             :   {
    3377      113225 :     long M = D[i]*N2;
    3378      113225 :     gel(T,M) = inittrace(M, CHIP, VCHIP);
    3379             :   }
    3380       28392 :   gel(T,N) = shallowconcat(gel(T,N), mkvec2(NZ, inittrconj(N,FC)));
    3381       28392 :   return T;
    3382             : }
    3383             : /* don't initialize if Tr^new = 0, return NULL */
    3384             : static GEN
    3385       28427 : initnewtrace(long N, GEN CHI)
    3386             : {
    3387       28427 :   GEN CHIP = mfchartoprimitive(CHI, NULL), NZ = mfnewzerodata(N,CHIP);
    3388       28427 :   return NZ? initnewtrace_i(N, CHIP, NZ): NULL;
    3389             : }
    3390             : 
    3391             : /* (-1)^k */
    3392             : static long
    3393        7161 : m1pk(long k) { return odd(k)? -1 : 1; }
    3394             : static long
    3395        6888 : badchar(long N, long k, GEN CHI)
    3396        6888 : { return mfcharparity(CHI) != m1pk(k) || (CHI && N % mfcharconductor(CHI)); }
    3397             : 
    3398             : /* dimension of space of cusp forms S_k(\G_0(N),CHI)
    3399             :  * Only depends on CHIP the primitive char attached to CHI */
    3400             : long
    3401       40453 : mfcuspdim(long N, long k, GEN CHI)
    3402             : {
    3403       40453 :   pari_sp av = avma;
    3404             :   long FC;
    3405             :   GEN s;
    3406       40453 :   if (k <= 0) return 0;
    3407       40453 :   if (k == 1) return mfwt1cuspdim(N, CHI);
    3408       40264 :   FC = CHI? mfcharconductor(CHI): 1;
    3409       40264 :   if (FC == 1) CHI = NULL;
    3410       40264 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3411       40264 :   s = gadd(s, gsubsg(A4(k, FC), A3(N, FC)));
    3412       40264 :   return gc_long(av, itos(s));
    3413             : }
    3414             : 
    3415             : /* dimension of whole space M_k(\G_0(N),CHI)
    3416             :  * Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3417             : long
    3418         686 : mffulldim(long N, long k, GEN CHI)
    3419             : {
    3420         686 :   pari_sp av = avma;
    3421         686 :   long FC = CHI? mfcharconductor(CHI): 1;
    3422             :   GEN s;
    3423         686 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3424         686 :   if (k == 1) return gc_long(av, itos(A3(N, FC)) + mfwt1cuspdim(N, CHI));
    3425         518 :   if (FC == 1) CHI = NULL;
    3426         518 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3427         518 :   s = gadd(s, A3(N, FC));
    3428         518 :   return gc_long(av, itos(s));
    3429             : }
    3430             : 
    3431             : /* Dimension of the space of Eisenstein series */
    3432             : long
    3433         231 : mfeisensteindim(long N, long k, GEN CHI)
    3434             : {
    3435         231 :   pari_sp av = avma;
    3436         231 :   long s, FC = CHI? mfcharconductor(CHI): 1;
    3437         231 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3438         231 :   s = itos(gmul2n(A3(N, FC), 1));
    3439         231 :   if (k > 1) s -= A4(k, FC); else s >>= 1;
    3440         231 :   return gc_long(av,s);
    3441             : }
    3442             : 
    3443             : enum { _SQRTS = 1, _MUP, _GCD, _VCHIP, _BEZ, _NEWLZ, _TRCONJ };
    3444             : /* Trace of T(n) on space of cuspforms; only depends on CHIP the primitive char
    3445             :  * attached to CHI */
    3446             : static GEN
    3447     4637304 : mfcusptrace_i(long N, long k, long n, GEN Dn, GEN S)
    3448             : {
    3449     4637304 :   pari_sp av = avma;
    3450             :   GEN a, b, VCHIP, GCD;
    3451             :   long t;
    3452     4637304 :   if (!n) return gen_0;
    3453     4637304 :   VCHIP = gel(S,_VCHIP);
    3454     4637304 :   GCD = gel(S,_GCD);
    3455     4637304 :   t = TA4(k, VCHIP, Dn, GCD);
    3456     4637304 :   a = TA1(N, k, VCHIP, GCD, n); if (t) a = gaddgs(a,t);
    3457     4637304 :   b = TA2(N, k, VCHIP, n, gel(S,_SQRTS), gel(S,_MUP), GCD);
    3458     4637304 :   b = gadd(b, TA3(N, k, VCHIP, GCD, Dn, gel(S,_BEZ)));
    3459     4637304 :   b = gsub(a,b);
    3460     4637304 :   if (typ(b) != t_POL) return gerepileupto(av, b);
    3461       40341 :   return gerepilecopy(av, vchip_polmod(VCHIP, b));
    3462             : }
    3463             : 
    3464             : static GEN
    3465     5957427 : mfcusptracecache(long N, long k, long n, GEN Dn, GEN S, cachenew_t *cache)
    3466             : {
    3467     5957427 :   GEN C = NULL, T = gel(cache->vfull,N);
    3468     5957427 :   long lcache = lg(T);
    3469     5957427 :   if (n < lcache) C = gel(T, n);
    3470     5957427 :   if (C) cache->cuspHIT++; else C = mfcusptrace_i(N, k, n, Dn, S);
    3471     5957427 :   cache->cuspTOTAL++;
    3472     5957427 :   if (n < lcache) gel(T,n) = C;
    3473     5957427 :   return C;
    3474             : }
    3475             : 
    3476             : /* return the divisors of n, known to be among the elements of D */
    3477             : static GEN
    3478      361333 : div_restrict(GEN D, ulong n)
    3479             : {
    3480             :   long i, j, l;
    3481      361333 :   GEN v, VDIV = caches[cache_DIV].cache;
    3482      361333 :   if (lg(VDIV) > n) return gel(VDIV,n);
    3483           0 :   l = lg(D);
    3484           0 :   v = cgetg(l, t_VECSMALL);
    3485           0 :   for (i = j = 1; i < l; i++)
    3486             :   {
    3487           0 :     ulong d = D[i];
    3488           0 :     if (n % d == 0) v[j++] = d;
    3489             :   }
    3490           0 :   setlg(v,j); return v;
    3491             : }
    3492             : 
    3493             : /* for some prime divisors of N, Tr^new(p) = 0 */
    3494             : static int
    3495      223314 : trconj(GEN T, long N, long n)
    3496      223314 : { return (lg(T) > 1 && N % n == 0 && zv_search(T, n)); }
    3497             : 
    3498             : /* n > 0; trace formula on new space */
    3499             : static GEN
    3500     1860670 : mfnewtrace_i(long N, long k, long n, cachenew_t *cache)
    3501             : {
    3502     1860670 :   GEN VCHIP, s, Dn, DN1, SN, S = cache->DATA;
    3503             :   long FC, N1, N2, N1N2, g, i, j, lDN1;
    3504             : 
    3505     1860670 :   if (!S) return gen_0;
    3506     1860670 :   SN = gel(S,N);
    3507     1860670 :   if (mfnewchkzero(gel(SN,_NEWLZ), n)) return gen_0;
    3508     1434727 :   if (k > 2 && trconj(gel(SN,_TRCONJ), N, n)) return gen_0;
    3509     1434692 :   VCHIP = gel(SN, _VCHIP); FC = vchip_FC(VCHIP);
    3510     1434692 :   N1 = N/FC; newt_params(N1, n, FC, &g, &N2);
    3511     1434692 :   N1N2 = N1/N2;
    3512     1434692 :   DN1 = mydivisorsu(N1N2); lDN1 = lg(DN1);
    3513     1434692 :   N2 *= FC;
    3514     1434692 :   Dn = mydivisorsu(n); /* this one is probably out of cache */
    3515     1434692 :   s = gmulsg(mubeta2(N1N2,n), mfcusptracecache(N2, k, n, Dn, gel(S,N2), cache));
    3516     5596094 :   for (i = 2; i < lDN1; i++)
    3517             :   { /* skip M1 = 1, done above */
    3518     4161402 :     long M1 = DN1[i], N1M1 = DN1[lDN1-i];
    3519     4161402 :     GEN Dg = mydivisorsu(ugcd(M1, g));
    3520     4161402 :     M1 *= N2;
    3521     4161402 :     s = gadd(s, gmulsg(mubeta2(N1M1,n),
    3522     4161402 :                        mfcusptracecache(M1, k, n, Dn, gel(S,M1), cache)));
    3523     4522735 :     for (j = 2; j < lg(Dg); j++) /* skip d = 1, done above */
    3524             :     {
    3525      361333 :       long d = Dg[j], ndd = n/(d*d), M = M1/d;
    3526      361333 :       GEN z = mulsi(mubeta2(N1M1,ndd), powuu(d,k-1)), C = vchip_lift(VCHIP,d,z);
    3527      361333 :       GEN Dndd = div_restrict(Dn, ndd);
    3528      361333 :       s = gadd(s, gmul(C, mfcusptracecache(M, k, ndd, Dndd, gel(S,M), cache)));
    3529             :     }
    3530     4161402 :     s = vchip_mod(VCHIP, s);
    3531             :   }
    3532     1434692 :   return vchip_polmod(VCHIP, s);
    3533             : }
    3534             : 
    3535             : /* mfcuspdim(N,k,CHI) - mfnewdim(N,k,CHI); CHIP primitive (for efficiency) */
    3536             : static long
    3537        7686 : mfolddim_i(long N, long k, GEN CHIP)
    3538             : {
    3539        7686 :   long S, i, l, FC = mfcharmodulus(CHIP), N1 = N/FC, N2;
    3540             :   GEN D;
    3541        7686 :   newd_params(N1, &N2); /* will ensure mubeta != 0 */
    3542        7686 :   D = mydivisorsu(N1/N2); l = lg(D);
    3543        7686 :   N2 *= FC; S = 0;
    3544       30170 :   for (i = 2; i < l; i++)
    3545             :   {
    3546       22484 :     long M = D[l-i]*N2, d = mfcuspdim(M, k, CHIP);
    3547       22484 :     if (d) S -= mubeta(D[i]) * d;
    3548             :   }
    3549        7686 :   return S;
    3550             : }
    3551             : long
    3552         399 : mfolddim(long N, long k, GEN CHI)
    3553             : {
    3554         399 :   pari_sp av = avma;
    3555         399 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3556         399 :   return gc_long(av, mfolddim_i(N, k, CHIP));
    3557             : }
    3558             : /* Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3559             : long
    3560       14826 : mfnewdim(long N, long k, GEN CHI)
    3561             : {
    3562             :   pari_sp av;
    3563             :   long S;
    3564       14826 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3565       14826 :   S = mfcuspdim(N, k, CHIP); if (!S) return 0;
    3566        7273 :   av = avma; return gc_long(av, S - mfolddim_i(N, k, CHIP));
    3567             : }
    3568             : 
    3569             : /* trace form, given as closure */
    3570             : static GEN
    3571         903 : mftraceform_new(long N, long k, GEN CHI)
    3572             : {
    3573             :   GEN T;
    3574         903 :   if (k == 1) return initwt1newtrace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3575         882 :   T = initnewtrace(N,CHI); if (!T) return mftrivial();
    3576         882 :   return tag(t_MF_NEWTRACE, mkNK(N,k,CHI), T);
    3577             : }
    3578             : static GEN
    3579          14 : mftraceform_cusp(long N, long k, GEN CHI)
    3580             : {
    3581          14 :   if (k == 1) return initwt1trace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3582           7 :   return tag(t_MF_TRACE, mkNK(N,k,CHI), inittrace(N,CHI,NULL));
    3583             : }
    3584             : static GEN
    3585          91 : mftraceform_i(GEN NK, long space)
    3586             : {
    3587             :   GEN CHI;
    3588             :   long N, k;
    3589          91 :   checkNK(NK, &N, &k, &CHI, 0);
    3590          91 :   if (!mfdim_Nkchi(N, k, CHI, space)) return mftrivial();
    3591          70 :   switch(space)
    3592             :   {
    3593          49 :     case mf_NEW: return mftraceform_new(N, k, CHI);
    3594          14 :     case mf_CUSP:return mftraceform_cusp(N, k, CHI);
    3595             :   }
    3596           7 :   pari_err_DOMAIN("mftraceform", "space", "=", utoi(space), NK);
    3597             :   return NULL;/*LCOV_EXCL_LINE*/
    3598             : }
    3599             : GEN
    3600          91 : mftraceform(GEN NK, long space)
    3601          91 : { pari_sp av = avma; return gerepilecopy(av, mftraceform_i(NK,space)); }
    3602             : 
    3603             : static GEN
    3604       15162 : hecke_data(long N, long n)
    3605       15162 : { return mkvecsmall3(n, u_ppo(n, N), N); }
    3606             : /* 1/2-integral weight */
    3607             : static GEN
    3608          84 : heckef2_data(long N, long n)
    3609             : {
    3610             :   ulong f, fN, fN2;
    3611          84 :   if (!uissquareall(n, &f)) return NULL;
    3612          77 :   fN = u_ppo(f, N); fN2 = fN*fN;
    3613          77 :   return mkvec2(myfactoru(fN), mkvecsmall4(n, N, fN2, n/fN2));
    3614             : }
    3615             : /* N = mf_get_N(F) or a multiple */
    3616             : static GEN
    3617       21889 : mfhecke_i(long n, long N, GEN F)
    3618             : {
    3619       21889 :   if (n == 1) return F;
    3620       14952 :   return tag2(t_MF_HECKE, mf_get_NK(F), hecke_data(N,n), F);
    3621             : }
    3622             : 
    3623             : GEN
    3624         105 : mfhecke(GEN mf, GEN F, long n)
    3625             : {
    3626         105 :   pari_sp av = avma;
    3627             :   GEN NK, CHI, gk, DATA;
    3628             :   long N, nk, dk;
    3629         105 :   mf = checkMF(mf);
    3630         105 :   if (!checkmf_i(F)) pari_err_TYPE("mfhecke",F);
    3631         105 :   if (n <= 0) pari_err_TYPE("mfhecke [n <= 0]", stoi(n));
    3632         105 :   if (n == 1) return gcopy(F);
    3633         105 :   gk = mf_get_gk(F);
    3634         105 :   Qtoss(gk,&nk,&dk);
    3635         105 :   CHI = mf_get_CHI(F);
    3636         105 :   N = MF_get_N(mf);
    3637         105 :   if (dk == 2)
    3638             :   {
    3639          77 :     DATA = heckef2_data(N,n);
    3640          77 :     if (!DATA) return mftrivial();
    3641             :   }
    3642             :   else
    3643          28 :     DATA = hecke_data(N,n);
    3644          98 :   NK = mkgNK(lcmii(stoi(N), mf_get_gN(F)), gk, CHI, mf_get_field(F));
    3645          98 :   return gerepilecopy(av, tag2(t_MF_HECKE, NK, DATA, F));
    3646             : }
    3647             : 
    3648             : /* form F given by closure, compute B(d)(F) as closure (q -> q^d) */
    3649             : static GEN
    3650       28224 : mfbd_i(GEN F, long d)
    3651             : {
    3652             :   GEN D, NK, gk, CHI;
    3653       28224 :   if (d == 1) return F;
    3654        9919 :   if (d <= 0) pari_err_TYPE("mfbd [d <= 0]", stoi(d));
    3655        9919 :   if (mf_get_type(F) != t_MF_BD) D = utoi(d);
    3656           7 :   else { D = mului(d, gel(F,3)); F = gel(F,2); }
    3657        9919 :   gk = mf_get_gk(F); CHI = mf_get_CHI(F);
    3658        9919 :   if (typ(gk) != t_INT) CHI = mfcharmul(CHI, get_mfchar(utoi(d << 2)));
    3659        9919 :   NK = mkgNK(muliu(mf_get_gN(F), d), gk, CHI, mf_get_field(F));
    3660        9919 :   return tag2(t_MF_BD, NK, F, D);
    3661             : }
    3662             : GEN
    3663          35 : mfbd(GEN F, long d)
    3664             : {
    3665          35 :   pari_sp av = avma;
    3666          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfbd",F);
    3667          35 :   return gerepilecopy(av, mfbd_i(F, d));
    3668             : }
    3669             : 
    3670             : /* CHI is a character defined modulo N4 */
    3671             : static GEN
    3672          98 : RgV_shimura(GEN V, long n, long D, long N4, long r, GEN CHI)
    3673             : {
    3674          98 :   GEN R, a0, Pn = mfcharpol(CHI);
    3675          98 :   long m, Da, ND, ord = mfcharorder(CHI), vt = varn(Pn), d4 = D & 3L;
    3676             : 
    3677          98 :   if (d4 == 2 || d4 == 3) D *= 4;
    3678          98 :   Da = labs(D); ND = N4*Da;
    3679          98 :   R = cgetg(n + 2, t_VEC);
    3680          98 :   a0 = gel(V, 1);
    3681          98 :   if (!gequal0(a0))
    3682             :   {
    3683           7 :     long D4 = D << 2;
    3684           7 :     GEN CHID = induceN(ulcm(mfcharmodulus(CHI), labs(D4)), CHI);
    3685           7 :     CHID = mfcharmul_i(CHID, induce(gel(CHID,1), stoi(D4)));
    3686           7 :     a0 = gmul(a0, charLFwtk(r, CHID, mfcharorder(CHID)));
    3687             :   }
    3688          98 :   if (odd(ND) && !odd(mfcharmodulus(CHI))) ND <<= 1;
    3689          98 :   gel(R, 1) = a0;
    3690         567 :   for (m = 1; m <= n; m++)
    3691             :   {
    3692         469 :     GEN Dm = mydivisorsu(u_ppo(m, ND)), S = gel(V, m*m + 1);
    3693         469 :     long i, l = lg(Dm);
    3694         770 :     for (i = 2; i < l; i++)
    3695             :     { /* (e,ND) = 1; skip i = 1: e = 1, done above */
    3696         301 :       long e = Dm[i], me = m / e;
    3697         301 :       long a = mfcharevalord(CHI, e, ord);
    3698         301 :       GEN c, C = powuu(e, r - 1);
    3699         301 :       if (kross(D, e) == -1) C = negi(C);
    3700         301 :       c = mygmodulo_lift(a, ord, C, vt);
    3701         301 :       S = gadd(S, gmul(c, gel(V, me*me + 1)));
    3702             :     }
    3703         469 :     gel(R, m+1) = S;
    3704             :   }
    3705          98 :   return degpol(Pn) > 1? gmodulo(R, Pn): R;
    3706             : }
    3707             : static GEN
    3708          28 : c_shimura(long n, GEN F, long D, GEN CHI)
    3709             : {
    3710          28 :   GEN v = mfcoefs_i(F, n*n, labs(D));
    3711          28 :   return RgV_shimura(v, n, D, mf_get_N(F)>>2, mf_get_r(F), CHI);
    3712             : }
    3713             : 
    3714             : static long
    3715          14 : mfisinkohnen(GEN mf, GEN F)
    3716             : {
    3717          14 :   GEN v, gk = MF_get_gk(mf), CHI = MF_get_CHI(mf);
    3718          14 :   long i, sb, eps, N4 = MF_get_N(mf) >> 2, r = MF_get_r(mf);
    3719          14 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
    3720          14 :   eps = N4 % mfcharconductor(CHI)? -1 : 1;
    3721          14 :   if (odd(r)) eps = -eps;
    3722          14 :   v = mfcoefs(F, sb, 1);
    3723         896 :   for (i = 0; i <= sb; i++)
    3724             :   {
    3725         882 :     long j = i & 3L;
    3726         882 :     if ((j == 2 || j == 2 + eps) && !gequal0(gel(v,i+1))) return 0;
    3727             :   }
    3728          14 :   return 1;
    3729             : }
    3730             : 
    3731             : static long
    3732          35 : mfshimura_space_cusp(GEN mf)
    3733             : {
    3734          35 :   long fl = 1, r = MF_get_r(mf), M = MF_get_N(mf) >> 2;
    3735          35 :   if (r == 1 && M >= 4)
    3736             :   {
    3737          14 :     GEN E = gel(myfactoru(M), 2);
    3738          14 :     long ma = vecsmall_max(E);
    3739          14 :     if (ma > 2 || (ma == 2 && !mfcharistrivial(MF_get_CHI(mf)))) fl = 0;
    3740             :   }
    3741          35 :   return fl;
    3742             : }
    3743             : 
    3744             : /* D is either a discriminant (not necessarily fundamental) with
    3745             :    sign(D)=(-1)^{k-1/2}*eps, or a positive squarefree integer t, which is then
    3746             :    transformed into a fundamental discriminant of the correct sign. */
    3747             : GEN
    3748          35 : mfshimura(GEN mf, GEN F, long D)
    3749             : {
    3750          35 :   pari_sp av = avma;
    3751             :   GEN gk, G, res, mf2, CHI, CHIP;
    3752          35 :   long M, r, space, cusp, N4, flagdisc = 0;
    3753          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfshimura",F);
    3754          35 :   gk = mf_get_gk(F);
    3755          35 :   if (typ(gk) != t_FRAC) pari_err_TYPE("mfshimura [integral weight]", F);
    3756          35 :   r = MF_get_r(mf);
    3757          35 :   if (r <= 0) pari_err_DOMAIN("mfshimura", "weight", "<=", ghalf, gk);
    3758          35 :   N4 = MF_get_N(mf) >> 2; CHI = MF_get_CHI(mf);
    3759          35 :   CHIP = mfcharchiliftprim(CHI, N4);
    3760          35 :   if (!CHIP) CHIP = CHI;
    3761             :   else
    3762             :   {
    3763          35 :     long epsD = CHI == CHIP? D: -D, rd = D & 3L;
    3764          35 :     if (odd(r)) epsD = -epsD;
    3765          35 :     if (epsD > 0 && (rd == 0 || rd == 1)) flagdisc = 1;
    3766             :     else
    3767             :     {
    3768          14 :       if (D < 0 || !uissquarefree(D))
    3769           7 :         pari_err_TYPE("shimura [incorrect D]", stoi(D));
    3770           7 :       D = epsD;
    3771             :     }
    3772             :   }
    3773          28 :   M = N4;
    3774          28 :   cusp = mfiscuspidal(mf,F);
    3775          28 :   space = cusp && mfshimura_space_cusp(mf)? mf_CUSP : mf_FULL;
    3776          28 :   if (!cusp || !flagdisc || !mfisinkohnen(mf,F)) M <<= 1;
    3777          28 :   mf2 = mfinit_Nkchi(M, r << 1, mfcharpow(CHI, gen_2), space, 0);
    3778          28 :   G = c_shimura(mfsturm(mf2), F, D, CHIP);
    3779          28 :   res = mftobasis_i(mf2, G);
    3780             :   /* not mflinear(mf2,): we want lowest possible level */
    3781          28 :   G = mflinear(MF_get_basis(mf2), res);
    3782          28 :   return gerepilecopy(av, mkvec3(mf2, G, res));
    3783             : }
    3784             : 
    3785             : /* W ZabM (ZM if n = 1), a t_INT or NULL, b t_INT, ZXQ mod P or NULL.
    3786             :  * Write a/b = A/d with d t_INT and A Zab return [W,d,A,P] */
    3787             : static GEN
    3788        6902 : mkMinv(GEN W, GEN a, GEN b, GEN P)
    3789             : {
    3790        6902 :   GEN A = (b && typ(b) == t_POL)? Q_remove_denom(QXQ_inv(b,P), &b): NULL;
    3791        6902 :   if (a && b)
    3792             :   {
    3793        1001 :     a = Qdivii(a,b);
    3794        1001 :     if (typ(a) == t_INT) b = gen_1; else { b = gel(a,2); a = gel(a,1); }
    3795        1001 :     if (is_pm1(a)) a = NULL;
    3796             :   }
    3797        6902 :   if (a) A = A? ZX_Z_mul(A,a): a; else if (!A) A = gen_1;
    3798        6902 :   if (!b) b = gen_1;
    3799        6902 :   if (!P) P = gen_0;
    3800        6902 :   return mkvec4(W,b,A,P);
    3801             : }
    3802             : /* M square invertible QabM, return [M',d], M*M' = d*Id */
    3803             : static GEN
    3804         441 : QabM_Minv(GEN M, GEN P, long n)
    3805             : {
    3806             :   GEN dW, W, dM;
    3807         441 :   M = Q_remove_denom(M, &dM);
    3808         441 :   W = P? ZabM_inv(liftpol_shallow(M), P, n, &dW): ZM_inv(M, &dW);
    3809         441 :   return mkMinv(W, dM, dW, P);
    3810             : }
    3811             : /* Simplified form of mfclean, after a QabM_indexrank: M a ZabM with full
    3812             :  * column rank and z = indexrank(M) is known */
    3813             : static GEN
    3814         812 : mfclean2(GEN M, GEN z, GEN P, long n)
    3815             : {
    3816         812 :   GEN d, Minv, y = gel(z,1), W = rowpermute(M, y);
    3817         812 :   W = P? ZabM_inv(liftpol_shallow(W), P, n, &d): ZM_inv(W, &d);
    3818         812 :   M = rowslice(M, 1, y[lg(y)-1]);
    3819         812 :   Minv = mkMinv(W, NULL, d, P);
    3820         812 :   return mkvec3(y, Minv, M);
    3821             : }
    3822             : /* M QabM, lg(M)>1 and [y,z] its rank profile. Let Minv be the inverse of the
    3823             :  * invertible square matrix in mkMinv format. Return [y,Minv, M[..y[#y],]]
    3824             :  * P cyclotomic polynomial of order n > 2 or NULL */
    3825             : static GEN
    3826        4508 : mfclean(GEN M, GEN P, long n, int ratlift)
    3827             : {
    3828        4508 :   GEN W, v, y, z, d, Minv, dM, MdM = Q_remove_denom(M, &dM);
    3829        4508 :   if (n <= 2)
    3830        3556 :     W = ZM_pseudoinv(MdM, &v, &d);
    3831             :   else
    3832         952 :     W = ZabM_pseudoinv_i(liftpol_shallow(MdM), P, n, &v, &d, ratlift);
    3833        4508 :   y = gel(v,1);
    3834        4508 :   z = gel(v,2);
    3835        4508 :   if (lg(z) != lg(MdM)) M = vecpermute(M,z);
    3836        4508 :   M = rowslice(M, 1, y[lg(y)-1]);
    3837        4508 :   Minv = mkMinv(W, dM, d, P);
    3838        4508 :   return mkvec3(y, Minv, M);
    3839             : }
    3840             : /* call mfclean using only CHI */
    3841             : static GEN
    3842        3640 : mfcleanCHI(GEN M, GEN CHI, int ratlift)
    3843             : {
    3844        3640 :   long n = mfcharorder(CHI);
    3845        3640 :   GEN P = (n <= 2)? NULL: mfcharpol(CHI);
    3846        3640 :   return mfclean(M, P, n, ratlift);
    3847             : }
    3848             : 
    3849             : /* DATA component of a t_MF_NEWTRACE. Was it stripped to save memory ? */
    3850             : static int
    3851       29295 : newtrace_stripped(GEN DATA)
    3852       29295 : { return DATA && (lg(DATA) == 5 && typ(gel(DATA,3)) == t_INT); }
    3853             : /* f a t_MF_NEWTRACE */
    3854             : static GEN
    3855       29295 : newtrace_DATA(long N, GEN f)
    3856             : {
    3857       29295 :   GEN DATA = gel(f,2);
    3858       29295 :   return newtrace_stripped(DATA)? initnewtrace(N, DATA): DATA;
    3859             : }
    3860             : /* reset cachenew for new level incorporating new DATA, tf a t_MF_NEWTRACE
    3861             :  * (+ possibly initialize 'full' for new allowed levels) */
    3862             : static void
    3863       29295 : reset_cachenew(cachenew_t *cache, long N, GEN tf)
    3864             : {
    3865             :   long i, n, l;
    3866       29295 :   GEN v, DATA = newtrace_DATA(N,tf);
    3867       29295 :   cache->DATA = DATA;
    3868       29295 :   if (!DATA) return;
    3869       29260 :   n = cache->n;
    3870       29260 :   v = cache->vfull; l = N+1; /* = lg(DATA) */
    3871     1771329 :   for (i = 1; i < l; i++)
    3872     1742069 :     if (typ(gel(v,i)) == t_INT && lg(gel(DATA,i)) != 1)
    3873       45647 :       gel(v,i) = const_vec(n, NULL);
    3874       29260 :   cache->VCHIP = gel(gel(DATA,N),_VCHIP);
    3875             : }
    3876             : /* initialize a cache of newtrace / cusptrace up to index n and level | N;
    3877             :  * DATA may be NULL (<=> Tr^new = 0). tf a t_MF_NEWTRACE */
    3878             : static void
    3879       10731 : init_cachenew(cachenew_t *cache, long n, long N, GEN tf)
    3880             : {
    3881       10731 :   long i, l = N+1; /* = lg(tf.DATA) when DATA != NULL */
    3882             :   GEN v;
    3883       10731 :   cache->n = n;
    3884       10731 :   cache->vnew = v = cgetg(l, t_VEC);
    3885       10731 :   for (i = 1; i < l; i++) gel(v,i) = (N % i)? gen_0: const_vec(n, NULL);
    3886       10731 :   cache->newHIT = cache->newTOTAL = cache->cuspHIT = cache->cuspTOTAL = 0;
    3887       10731 :   cache->vfull = v = zerovec(N);
    3888       10731 :   reset_cachenew(cache, N, tf);
    3889       10731 : }
    3890             : static void
    3891       15680 : dbg_cachenew(cachenew_t *C)
    3892             : {
    3893       15680 :   if (DEBUGLEVEL >= 2 && C)
    3894           0 :     err_printf("newtrace cache hits: new = %ld/%ld, cusp = %ld/%ld\n",
    3895             :                     C->newHIT, C->newTOTAL, C->cuspHIT, C->cuspTOTAL);
    3896       15680 : }
    3897             : 
    3898             : /* newtrace_{N,k}(d*i), i = n0, ..., n */
    3899             : static GEN
    3900      134603 : colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *cache)
    3901             : {
    3902      134603 :   GEN v = cgetg(n-n0+2, t_COL);
    3903             :   long i;
    3904      134603 :   for (i = n0; i <= n; i++) gel(v, i-n0+1) = mfnewtracecache(N, k, i*d, cache);
    3905      134603 :   return v;
    3906             : }
    3907             : /* T_n(l*m0, l*(m0+1), ..., l*m) F, F = t_MF_NEWTRACE [N,k],DATA, cache
    3908             :  * contains DATA != NULL as well as cached values of F */
    3909             : static GEN
    3910       74088 : heckenewtrace(long m0, long m, long l, long N, long NBIG, long k, long n, cachenew_t *cache)
    3911             : {
    3912       74088 :   long lD, a, k1, nl = n*l;
    3913       74088 :   GEN D, V, v = colnewtrace(m0, m, nl, N, k, cache); /* d=1 */
    3914             :   GEN VCHIP;
    3915       74088 :   if (n == 1) return v;
    3916       48636 :   VCHIP = cache->VCHIP;
    3917       48636 :   D = mydivisorsu(u_ppo(n, NBIG)); lD = lg(D);
    3918       48636 :   k1 = k - 1;
    3919      108402 :   for (a = 2; a < lD; a++)
    3920             :   { /* d > 1, (d,NBIG) = 1 */
    3921       59766 :     long i, j, d = D[a], c = ugcd(l, d), dl = d/c, m0d = ceildiv(m0, dl);
    3922       59766 :     GEN C = vchip_lift(VCHIP, d, powuu(d, k1));
    3923             :     /* m0=0: i = 1 => skip F(0) = 0 */
    3924       59766 :     if (!m0) { i = 1; j = dl; } else { i = 0; j = m0d*dl; }
    3925       59766 :     V = colnewtrace(m0d, m/dl, nl/(d*c), N, k, cache);
    3926             :     /* C = chi(d) d^(k-1) */
    3927      633178 :     for (; j <= m; i++, j += dl)
    3928      573412 :       gel(v,j-m0+1) = gadd(gel(v,j-m0+1), vchip_mod(VCHIP, gmul(C,gel(V,i+1))));
    3929             :   }
    3930       48636 :   return v;
    3931             : }
    3932             : 
    3933             : /* Given v = an[i], return an[d*i] */
    3934             : static GEN
    3935         658 : anextract(GEN v, long n, long d)
    3936             : {
    3937         658 :   GEN w = cgetg(n+2, t_VEC);
    3938             :   long i;
    3939         658 :   for (i = 0; i <= n; i++) gel(w, i+1) = gel(v, i*d+1);
    3940         658 :   return w;
    3941             : }
    3942             : /* T_n(F)(0, l, ..., l*m) */
    3943             : static GEN
    3944         854 : hecke_i(long m, long l, GEN V, GEN F, GEN DATA)
    3945             : {
    3946             :   long k, n, nNBIG, NBIG, lD, M, a, t, nl;
    3947             :   GEN D, v, CHI;
    3948         854 :   if (typ(DATA) == t_VEC)
    3949             :   { /* 1/2-integral k */
    3950          98 :     if (!V) { GEN S = gel(DATA,2); V = mfcoefs_i(F, m*l*S[3], S[4]); }
    3951          98 :     return RgV_heckef2(m, l, V, F, DATA);
    3952             :   }
    3953         756 :   k = mf_get_k(F);
    3954         756 :   n = DATA[1]; nl = n*l;
    3955         756 :   nNBIG = DATA[2];
    3956         756 :   NBIG = DATA[3];
    3957         756 :   if (nNBIG == 1) return V? V: mfcoefs_i(F,m,nl);
    3958         539 :   if (!V && mf_get_type(F) == t_MF_NEWTRACE)
    3959             :   { /* inline F to allow cache, T_n at level NBIG acting on Tr^new(N,k,CHI) */
    3960             :     cachenew_t cache;
    3961         210 :     long N = mf_get_N(F);
    3962         210 :     init_cachenew(&cache, m*nl, N, F);
    3963         210 :     v = heckenewtrace(0, m, l, N, NBIG, k, n, &cache);
    3964         210 :     dbg_cachenew(&cache);
    3965         210 :     settyp(v, t_VEC); return v;
    3966             :   }
    3967         329 :   CHI = mf_get_CHI(F);
    3968         329 :   D = mydivisorsu(nNBIG); lD = lg(D);
    3969         329 :   M = m + 1;
    3970         329 :   t = nNBIG * ugcd(nNBIG, l);
    3971         329 :   if (!V) V = mfcoefs_i(F, m * t, nl / t); /* usually nl = t */
    3972         329 :   v = anextract(V, m, t); /* mfcoefs(F, m, nl); d = 1 */
    3973         658 :   for (a = 2; a < lD; a++)
    3974             :   { /* d > 1, (d, NBIG) = 1 */
    3975         329 :     long d = D[a], c = ugcd(l, d), dl = d/c, i, idl;
    3976         329 :     GEN C = gmul(mfchareval_i(CHI, d), powuu(d, k-1));
    3977         329 :     GEN w = anextract(V, m/dl, t/(d*c)); /* mfcoefs(F, m/dl, nl/(d*c)) */
    3978        1008 :     for (i = idl = 1; idl <= M; i++, idl += dl)
    3979         679 :       gel(v,idl) = gadd(gel(v,idl), gmul(C, gel(w,i)));
    3980             :   }
    3981         329 :   return v;
    3982             : }
    3983             : 
    3984             : static GEN
    3985       11403 : mkmf(GEN x1, GEN x2, GEN x3, GEN x4, GEN x5)
    3986             : {
    3987       11403 :   GEN MF = obj_init(5, MF_SPLITN);
    3988       11403 :   gel(MF,1) = x1;
    3989       11403 :   gel(MF,2) = x2;
    3990       11403 :   gel(MF,3) = x3;
    3991       11403 :   gel(MF,4) = x4;
    3992       11403 :   gel(MF,5) = x5; return MF;
    3993             : }
    3994             : 
    3995             : /* return an integer b such that p | b => T_p^k Tr^new = 0, for all k > 0 */
    3996             : static long
    3997        7070 : get_badj(long N, long FC)
    3998             : {
    3999        7070 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    4000        7070 :   long i, b = 1, l = lg(P);
    4001       18802 :   for (i = 1; i < l; i++)
    4002       11732 :     if (E[i] > 1 && u_lval(FC, P[i]) < E[i]) b *= P[i];
    4003        7070 :   return b;
    4004             : }
    4005             : /* in place, assume perm strictly increasing */
    4006             : static void
    4007        1162 : vecpermute_inplace(GEN v, GEN perm)
    4008             : {
    4009        1162 :   long i, l = lg(perm);
    4010        1162 :   for (i = 1; i < l; i++) gel(v,i) = gel(v,perm[i]);
    4011        1162 : }
    4012             : 
    4013             : /* Find basis of newspace using closures; assume k >= 2 and !badchar.
    4014             :  * Return NULL if space is empty, else
    4015             :  * [mf1, list of closures T(j)traceform, list of corresponding j, matrix] */
    4016             : static GEN
    4017       14588 : mfnewinit(long N, long k, GEN CHI, cachenew_t *cache, long init)
    4018             : {
    4019             :   GEN S, vj, M, CHIP, mf1, listj, P, tf;
    4020             :   long j, ct, ctlj, dim, jin, SB, sb, two, ord, FC, badj;
    4021             : 
    4022       14588 :   dim = mfnewdim(N, k, CHI);
    4023       14588 :   if (!dim && !init) return NULL;
    4024        7070 :   sb = mfsturmNk(N, k);
    4025        7070 :   CHIP = mfchartoprimitive(CHI, &FC);
    4026             :   /* remove newtrace data from S to save space in output: negligible slowdown */
    4027        7070 :   tf = tag(t_MF_NEWTRACE, mkNK(N,k,CHIP), CHIP);
    4028        7070 :   badj = get_badj(N, FC);
    4029             :   /* try sbsmall first: Sturm bound not sharp for new space */
    4030        7070 :   SB = ceilA1(N, k);
    4031        7070 :   listj = cgetg(2*sb + 3, t_VECSMALL);
    4032      330169 :   for (j = ctlj = 1; ctlj < 2*sb + 3; j++)
    4033      323099 :     if (ugcd(j, badj) == 1) listj[ctlj++] = j;
    4034        7070 :   if (init)
    4035             :   {
    4036        3906 :     init_cachenew(cache, (SB+1)*listj[dim+1], N, tf);
    4037        3906 :     if (init == -1 || !dim) return NULL; /* old space or dim = 0 */
    4038             :   }
    4039             :   else
    4040        3164 :     reset_cachenew(cache, N, tf);
    4041             :   /* cache.DATA is not NULL */
    4042        6643 :   ord = mfcharorder(CHIP);
    4043        6643 :   P = ord <= 2? NULL: mfcharpol(CHIP);
    4044        6643 :   vj = cgetg(dim+1, t_VECSMALL);
    4045        6643 :   M = cgetg(dim+1, t_MAT);
    4046        6650 :   for (two = 1, ct = 0, jin = 1; two <= 2; two++)
    4047             :   {
    4048        6650 :     long a, jlim = jin + sb;
    4049       19250 :     for (a = jin; a <= jlim; a++)
    4050             :     {
    4051             :       GEN z, vecz;
    4052       19243 :       ct++; vj[ct] = listj[a];
    4053       19243 :       gel(M, ct) = heckenewtrace(0, SB, 1, N, N, k, vj[ct], cache);
    4054       19243 :       if (ct < dim) continue;
    4055             : 
    4056        7224 :       z = QabM_indexrank(M, P, ord);
    4057        7224 :       vecz = gel(z, 2); ct = lg(vecz) - 1;
    4058        7224 :       if (ct == dim) { M = mkvec3(z, gen_0, M); break; } /*maximal rank, done*/
    4059         581 :       vecpermute_inplace(M, vecz);
    4060         581 :       vecpermute_inplace(vj, vecz);
    4061             :     }
    4062        6650 :     if (a <= jlim) break;
    4063             :     /* sbsmall was not sufficient, use Sturm bound: must extend M */
    4064          70 :     for (j = 1; j <= ct; j++)
    4065             :     {
    4066          63 :       GEN t = heckenewtrace(SB + 1, sb, 1, N, N, k, vj[j], cache);
    4067          63 :       gel(M,j) = shallowconcat(gel(M, j), t);
    4068             :     }
    4069           7 :     jin = jlim + 1; SB = sb;
    4070             :   }
    4071        6643 :   S = cgetg(dim + 1, t_VEC);
    4072        6643 :   for (j = 1; j <= dim; j++) gel(S, j) = mfhecke_i(vj[j], N, tf);
    4073        6643 :   dbg_cachenew(cache);
    4074        6643 :   mf1 = mkvec4(utoipos(N), utoipos(k), CHI, utoi(mf_NEW));
    4075        6643 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    4076             : }
    4077             : /* k > 1 integral, mf space is mf_CUSP or mf_FULL */
    4078             : static GEN
    4079          28 : mfinittonew(GEN mf)
    4080             : {
    4081          28 :   GEN CHI = MF_get_CHI(mf), S = MF_get_S(mf), vMjd = MFcusp_get_vMjd(mf);
    4082          28 :   GEN M = MF_get_M(mf), vj, mf1;
    4083          28 :   long i, j, l, l0 = lg(S), N0 = MF_get_N(mf);
    4084         161 :   for (i = l0-1; i > 0; i--)
    4085             :   {
    4086         161 :     long N = gel(vMjd,i)[1];
    4087         161 :     if (N != N0) break;
    4088             :   }
    4089          28 :   if (i == l0-1) return NULL;
    4090          28 :   S = vecslice(S, i+1, l0-1); /* forms of conductor N0 */
    4091          28 :   l = lg(S); vj = cgetg(l, t_VECSMALL);
    4092          28 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd,j+i)[2];
    4093          28 :   M = vecslice(M, lg(M)-lg(S)+1, lg(M)-1); /* their coefficients */
    4094          28 :   M = mfcleanCHI(M, CHI, 0);
    4095          28 :   mf1 = mkvec4(utoipos(N0), MF_get_gk(mf), CHI, utoi(mf_NEW));
    4096          28 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    4097             : }
    4098             : 
    4099             : /* Bd(f)[m0..m], v = f[ceil(m0/d)..floor(m/d)], m0d = ceil(m0/d) */
    4100             : static GEN
    4101       67753 : RgC_Bd_expand(long m0, long m, GEN v, long d, long m0d)
    4102             : {
    4103             :   long i, j;
    4104             :   GEN w;
    4105       67753 :   if (d == 1) return v;
    4106       19523 :   w = zerocol(m-m0+1);
    4107       19523 :   if (!m0) { i = 1; j = d; } else { i = 0; j = m0d*d; }
    4108       19523 :   for (; j <= m; i++, j += d) gel(w,j-m0+1) = gel(v,i+1);
    4109       19523 :   return w;
    4110             : }
    4111             : /* S a non-empty vector of t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)); M the matrix
    4112             :  * of their coefficients r*0, r*1, ..., r*m0 (~ mfvectomat) or NULL (empty),
    4113             :  * extend it to coeffs up to m > m0. The forms B_d(T_j(tf_N))in S should be
    4114             :  * sorted by level N, then j, then increasing d. No reordering here. */
    4115             : static GEN
    4116        8001 : bhnmat_extend(GEN M, long m, long r, GEN S, cachenew_t *cache)
    4117             : {
    4118        8001 :   long i, mr, m0, m0r, Nold = 0, jold = 0, l = lg(S);
    4119        8001 :   GEN MAT = cgetg(l, t_MAT), v = NULL;
    4120        8001 :   if (M) { m0 = nbrows(M); m0r = m0 * r; } else m0 = m0r = 0;
    4121        8001 :   mr = m*r;
    4122       75754 :   for (i = 1; i < l; i++)
    4123             :   {
    4124             :     long d, j, md, N;
    4125       67753 :     GEN c, f = bhn_parse(gel(S,i), &d,&j); /* t_MF_NEWTRACE */
    4126       67753 :     N = mf_get_N(f);
    4127       67753 :     md = ceildiv(m0r,d);
    4128       67753 :     if (N != Nold) { reset_cachenew(cache, N, f); Nold = N; jold = 0; }
    4129       67753 :     if (!cache->DATA) { gel(MAT,i) = zerocol(m+1); continue; }
    4130       67753 :     if (j != jold || md)
    4131       54572 :     { v = heckenewtrace(md, mr/d, 1, N, N, mf_get_k(f), j,cache); jold=j; }
    4132       67753 :     c = RgC_Bd_expand(m0r, mr, v, d, md);
    4133       67753 :     if (r > 1) c = c_deflate(m-m0, r, c);
    4134       67753 :     if (M) c = shallowconcat(gel(M,i), c);
    4135       67753 :     gel(MAT,i) = c;
    4136             :   }
    4137        8001 :   return MAT;
    4138             : }
    4139             : 
    4140             : static GEN
    4141        2961 : mfinitcusp(long N, long k, GEN CHI, cachenew_t *cache, long space)
    4142             : {
    4143             :   long L, l, lDN1, FC, N1, d1, i, init;
    4144        2961 :   GEN vS, vMjd, DN1, vmf, CHIP = mfchartoprimitive(CHI, &FC);
    4145             : 
    4146        2961 :   d1 = (space == mf_OLD)? mfolddim_i(N, k, CHIP): mfcuspdim(N, k, CHIP);
    4147        2961 :   if (!d1) return NULL;
    4148        2723 :   N1 = N/FC; DN1 = mydivisorsu(N1); lDN1 = lg(DN1);
    4149        2723 :   init = (space == mf_OLD)? -1: 1;
    4150        2723 :   vmf = cgetg(lDN1, t_VEC);
    4151       16128 :   for (i = lDN1 - 1, l = 1; i; i--)
    4152             :   { /* by decreasing level to allow cache */
    4153       13405 :     GEN mf = mfnewinit(FC*DN1[i], k, CHIP, cache, init);
    4154       13405 :     if (mf) gel(vmf, l++) = mf;
    4155       13405 :     init = 0;
    4156             :   }
    4157        2723 :   setlg(vmf,l); vmf = vecreverse(vmf); /* reorder by increasing level */
    4158             : 
    4159        2723 :   L = mfsturmNk(N, k)+1;
    4160        2723 :   vS = vectrunc_init(L);
    4161        2723 :   vMjd = vectrunc_init(L);
    4162        8554 :   for (i = 1; i < l; i++)
    4163             :   {
    4164        5831 :     GEN DNM, mf = gel(vmf,i), S = MF_get_S(mf), vj = MFnew_get_vj(mf);
    4165        5831 :     long a, lDNM, lS = lg(S), M = MF_get_N(mf);
    4166        5831 :     DNM = mydivisorsu(N / M); lDNM = lg(DNM);
    4167       22484 :     for (a = 1; a < lS; a++)
    4168             :     {
    4169       16653 :       GEN tf = gel(S,a);
    4170       16653 :       long b, j = vj[a];
    4171       41048 :       for (b = 1; b < lDNM; b++)
    4172             :       {
    4173       24395 :         long d = DNM[b];
    4174       24395 :         vectrunc_append(vS, mfbd_i(tf, d));
    4175       24395 :         vectrunc_append(vMjd, mkvecsmall3(M, j, d));
    4176             :       }
    4177             :     }
    4178             :   }
    4179        2723 :   return mkmf(NULL, cgetg(1, t_VEC), vS, vMjd, NULL);
    4180             : }
    4181             : 
    4182             : long
    4183        3423 : mfsturm_mf(GEN mf)
    4184             : {
    4185        3423 :   GEN Mindex = MF_get_Mindex(mf);
    4186        3423 :   long n = lg(Mindex)-1;
    4187        3423 :   return n? Mindex[n]: 0;
    4188             : }
    4189             : 
    4190             : long
    4191         532 : mfsturm(GEN T)
    4192             : {
    4193             :   long N, nk, dk;
    4194         532 :   GEN CHI, mf = checkMF_i(T);
    4195         532 :   if (mf) return mfsturm_mf(mf);
    4196           7 :   checkNK2(T, &N, &nk, &dk, &CHI, 0);
    4197           7 :   return dk == 1 ? mfsturmNk(N, nk) : mfsturmNk(N, (nk + 1) >> 1);
    4198             : }
    4199             : 
    4200             : long
    4201           7 : mfisequal(GEN F, GEN G, long lim)
    4202             : {
    4203           7 :   pari_sp av = avma;
    4204             :   long sb;
    4205           7 :   if (!checkmf_i(F)) pari_err_TYPE("mfisequal",F);
    4206           7 :   if (!checkmf_i(G)) pari_err_TYPE("mfisequal",G);
    4207           7 :   if (lim) sb = lim;
    4208             :   else
    4209             :   {
    4210             :     GEN gN, gk;
    4211           7 :     gN = mf_get_gN(F); gk = mf_get_gk(F);
    4212           7 :     sb = mfsturmNgk(itou(gN), gk);
    4213           7 :     gN = mf_get_gN(G); gk = mf_get_gk(G);
    4214           7 :     sb = maxss(sb, mfsturmNgk(itou(gN), gk));
    4215             :   }
    4216           7 :   return gc_long(av, gequal(mfcoefs_i(F, sb+1, 1), mfcoefs_i(G, sb+1, 1)));
    4217             : }
    4218             : 
    4219             : GEN
    4220          35 : mffields(GEN mf)
    4221             : {
    4222          35 :   if (checkmf_i(mf)) return gcopy(mf_get_field(mf));
    4223          35 :   mf = checkMF(mf); return gcopy(MF_get_fields(mf));
    4224             : }
    4225             : 
    4226             : GEN
    4227         301 : mfeigenbasis(GEN mf)
    4228             : {
    4229         301 :   pari_sp ltop = avma;
    4230             :   GEN F, S, v, vP;
    4231             :   long i, l, k, dS;
    4232             : 
    4233         301 :   mf = checkMF(mf);
    4234         301 :   k = MF_get_k(mf);
    4235         301 :   S = MF_get_S(mf); dS = lg(S)-1;
    4236         301 :   if (!dS) return cgetg(1, t_VEC);
    4237         294 :   F = MF_get_newforms(mf);
    4238         294 :   vP = MF_get_fields(mf);
    4239         294 :   if (k == 1)
    4240             :   {
    4241         189 :     if (MF_get_space(mf) == mf_FULL)
    4242             :     {
    4243           7 :       long dE = lg(MF_get_E(mf)) - 1;
    4244           7 :       if (dE) F = rowslice(F, dE+1, dE+dS);
    4245             :     }
    4246         189 :     v = vecmflineardiv_linear(S, F);
    4247         189 :     l = lg(v);
    4248             :   }
    4249             :   else
    4250             :   {
    4251         105 :     GEN (*L)(GEN, GEN) = (MF_get_space(mf) == mf_FULL)? mflinear: mflinear_bhn;
    4252         105 :     l = lg(F); v = cgetg(l, t_VEC);
    4253         105 :     for (i = 1; i < l; i++) gel(v,i) = L(mf, gel(F,i));
    4254             :   }
    4255         294 :   for (i = 1; i < l; i++) mf_setfield(gel(v,i), gel(vP,i));
    4256         294 :   return gerepilecopy(ltop, v);
    4257             : }
    4258             : 
    4259             : /* Minv = [M, d, A], v a t_COL; A a Zab, d a t_INT; return (A/d) * M*v */
    4260             : static GEN
    4261        5516 : Minv_RgC_mul(GEN Minv, GEN v)
    4262             : {
    4263        5516 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4264        5516 :   v = RgM_RgC_mul(M, v);
    4265        5516 :   if (!equali1(A))
    4266             :   {
    4267        1393 :     if (typ(A) == t_POL && degpol(A) > 0) A = mkpolmod(A, gel(Minv,4));
    4268        1393 :     v = RgC_Rg_mul(v, A);
    4269             :   }
    4270        5516 :   if (!equali1(d)) v = RgC_Rg_div(v, d);
    4271        5516 :   return v;
    4272             : }
    4273             : static GEN
    4274        1078 : Minv_RgM_mul(GEN Minv, GEN B)
    4275             : {
    4276        1078 :   long j, l = lg(B);
    4277        1078 :   GEN M = cgetg(l, t_MAT);
    4278        1078 :   for (j = 1; j < l; j++) gel(M,j) = Minv_RgC_mul(Minv, gel(B,j));
    4279        1078 :   return M;
    4280             : }
    4281             : /* B * Minv; allow B = NULL for Id */
    4282             : static GEN
    4283        2156 : RgM_Minv_mul(GEN B, GEN Minv)
    4284             : {
    4285        2156 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4286        2156 :   if (B) M = RgM_mul(B, M);
    4287        2156 :   if (!equali1(A))
    4288             :   {
    4289         868 :     if (typ(A) == t_POL) A = mkpolmod(A, gel(Minv,4));
    4290         868 :     M = RgM_Rg_mul(M, A);
    4291             :   }
    4292        2156 :   if (!equali1(d)) M = RgM_Rg_div(M,d);
    4293        2156 :   return M;
    4294             : }
    4295             : 
    4296             : /* perm vector of strictly increasing indices, v a vector or arbitrary length;
    4297             :  * the last r entries of perm fall beyond v.
    4298             :  * Return v o perm[1..(-r)], discarding the last r entries of v */
    4299             : static GEN
    4300        1043 : vecpermute_partial(GEN v, GEN perm, long *r)
    4301             : {
    4302        1043 :   long i, n = lg(v)-1, l = lg(perm);
    4303             :   GEN w;
    4304        1043 :   if (perm[l-1] <= n) { *r = 0; return vecpermute(v,perm); }
    4305          63 :   for (i = 1; i < l; i++)
    4306          63 :     if (perm[i] > n) break;
    4307          21 :   *r = l - i; l = i;
    4308          21 :   w = cgetg(l, typ(v));
    4309          21 :   for (i = 1; i < l; i++) gel(w,i) = gel(v,perm[i]);
    4310          21 :   return w;
    4311             : }
    4312             : 
    4313             : /* given form F, find coeffs of F on mfbasis(mf). If power series, not
    4314             :  * guaranteed correct if precision less than Sturm bound */
    4315             : static GEN
    4316        1050 : mftobasis_i(GEN mf, GEN F)
    4317             : {
    4318             :   GEN v, Mindex, Minv;
    4319        1050 :   if (!MF_get_dim(mf)) return cgetg(1, t_COL);
    4320        1050 :   Mindex = MF_get_Mindex(mf);
    4321        1050 :   Minv = MF_get_Minv(mf);
    4322        1050 :   if (checkmf_i(F))
    4323             :   {
    4324         154 :     long n = Mindex[lg(Mindex)-1];
    4325         154 :     v = vecpermute(mfcoefs_i(F, n, 1), Mindex);
    4326         154 :     return Minv_RgC_mul(Minv, v);
    4327             :   }
    4328             :   else
    4329             :   {
    4330         896 :     GEN A = gel(Minv,1), d = gel(Minv,2);
    4331             :     long r;
    4332         896 :     v = F;
    4333         896 :     switch(typ(F))
    4334             :     {
    4335           0 :       case t_SER: v = sertocol(v);
    4336         896 :       case t_VEC: case t_COL: break;
    4337           0 :       default: pari_err_TYPE("mftobasis", F);
    4338             :     }
    4339         896 :     if (lg(v) == 1) pari_err_TYPE("mftobasis",v);
    4340         896 :     v = vecpermute_partial(v, Mindex, &r);
    4341         896 :     if (!r) return Minv_RgC_mul(Minv, v); /* single solution */
    4342             :     /* affine space of dimension r */
    4343          21 :     v = RgM_RgC_mul(vecslice(A, 1, lg(v)-1), v);
    4344          21 :     if (!equali1(d)) v = RgC_Rg_div(v,d);
    4345          21 :     return mkvec2(v, vecslice(A, lg(A)-r, lg(A)-1));
    4346             :   }
    4347             : }
    4348             : 
    4349             : static GEN
    4350         560 : const_mat(long n, GEN x)
    4351             : {
    4352         560 :   long j, l = n+1;
    4353         560 :   GEN A = cgetg(l,t_MAT);
    4354         560 :   for (j = 1; j < l; j++) gel(A,j) = const_col(n, x);
    4355         560 :   return A;
    4356             : }
    4357             : 
    4358             : /* L is the mftobasis of a form on CUSP space. We allow mf_FULL or mf_CUSP */
    4359             : static GEN
    4360         280 : mftonew_i(GEN mf, GEN L, long *plevel)
    4361             : {
    4362             :   GEN S, listMjd, CHI, res, Aclos, Acoef, D, perm;
    4363         280 :   long N1, LC, lD, i, l, t, level, N = MF_get_N(mf);
    4364             : 
    4365         280 :   if (MF_get_k(mf) == 1) pari_err_IMPL("mftonew in weight 1");
    4366         280 :   listMjd = MFcusp_get_vMjd(mf);
    4367         280 :   CHI = MF_get_CHI(mf); LC = mfcharconductor(CHI);
    4368         280 :   S = MF_get_S(mf);
    4369             : 
    4370         280 :   N1 = N/LC;
    4371         280 :   D = mydivisorsu(N1); lD = lg(D);
    4372         280 :   perm = cgetg(N1+1, t_VECSMALL);
    4373         280 :   for (i = 1; i < lD; i++) perm[D[i]] = i;
    4374         280 :   Aclos = const_mat(lD-1, cgetg(1,t_VEC));
    4375         280 :   Acoef = const_mat(lD-1, cgetg(1,t_VEC));
    4376         280 :   l = lg(listMjd);
    4377        2877 :   for (i = 1; i < l; i++)
    4378             :   {
    4379             :     long M, d;
    4380             :     GEN v;
    4381        2597 :     if (gequal0(gel(L,i))) continue;
    4382         273 :     v = gel(listMjd, i);
    4383         273 :     M = perm[ v[1]/LC ];
    4384         273 :     d = perm[ v[3] ];
    4385         273 :     gcoeff(Aclos,M,d) = vec_append(gcoeff(Aclos,M,d), gel(S,i));
    4386         273 :     gcoeff(Acoef,M,d) = shallowconcat(gcoeff(Acoef,M,d), gel(L,i));
    4387             :   }
    4388         280 :   res = cgetg(l, t_VEC); level = 1;
    4389        2009 :   for (i = t = 1; i < lD; i++)
    4390             :   {
    4391        1729 :     long j, M = D[i]*LC;
    4392        1729 :     GEN gM = utoipos(M);
    4393       15134 :     for (j = 1; j < lD; j++)
    4394             :     {
    4395       13405 :       GEN f = gcoeff(Aclos,i,j), C, NK;
    4396             :       long d;
    4397       13405 :       if (lg(f) == 1) continue;
    4398         245 :       NK = mf_get_NK(gel(f,1));
    4399         245 :       d = D[j];
    4400         245 :       C = gcoeff(Acoef,i,j);
    4401         245 :       level = ulcm(level, M*d);
    4402         245 :       gel(res,t++) = mkvec3(gM, utoipos(d), mflinear_i(NK,f,C));
    4403             :     }
    4404             :   }
    4405         280 :   if (plevel) *plevel = level;
    4406         280 :   setlg(res, t); return res;
    4407             : }
    4408             : GEN
    4409          35 : mftonew(GEN mf, GEN F)
    4410             : {
    4411          35 :   pari_sp av = avma;
    4412             :   GEN ES;
    4413             :   long s;
    4414          35 :   mf = checkMF(mf);
    4415          35 :   s = MF_get_space(mf);
    4416          35 :   if (s != mf_FULL && s != mf_CUSP)
    4417           7 :     pari_err_TYPE("mftonew [not a full or cuspidal space]", mf);
    4418          28 :   ES = mftobasisES(mf,F);
    4419          21 :   if (!gequal0(gel(ES,1)))
    4420           0 :     pari_err_TYPE("mftonew [not a cuspidal form]", F);
    4421          21 :   F = gel(ES,2);
    4422          21 :   return gerepilecopy(av, mftonew_i(mf,F, NULL));
    4423             : }
    4424             : 
    4425             : static GEN mfeisenstein_i(long k, GEN CHI1, GEN CHI2);
    4426             : 
    4427             : /* mfinit(F * Theta) */
    4428             : static GEN
    4429          70 : mf2init(GEN mf)
    4430             : {
    4431          70 :   GEN CHI = MF_get_CHI(mf), gk = gadd(MF_get_gk(mf), ghalf);
    4432          70 :   long N = MF_get_N(mf);
    4433          70 :   return mfinit_Nkchi(N, itou(gk), mfchiadjust(CHI, gk, N), mf_FULL, 0);
    4434             : }
    4435             : 
    4436             : static long
    4437         490 : mfvec_first_cusp(GEN v)
    4438             : {
    4439         490 :   long i, l = lg(v);
    4440         959 :   for (i = 1; i < l; i++)
    4441             :   {
    4442         882 :     GEN F = gel(v,i);
    4443         882 :     long t = mf_get_type(F);
    4444         882 :     if (t == t_MF_BD) { F = gel(F,2); t = mf_get_type(F); }
    4445         882 :     if (t == t_MF_HECKE) { F = gel(F,3); t = mf_get_type(F); }
    4446         882 :     if (t == t_MF_NEWTRACE) break;
    4447             :   }
    4448         490 :   return i;
    4449             : }
    4450             : /* vF a vector of mf F of type DIV(LINEAR(BAS,L), f) in (lcm) level N,
    4451             :  * F[2]=LINEAR(BAS,L), F[2][2]=BAS=fixed basis (Eisenstein or bhn type),
    4452             :  * F[2][3]=L, F[3]=f; mfvectomat(vF, n) */
    4453             : static GEN
    4454         497 : mflineardivtomat(long N, GEN vF, long n)
    4455             : {
    4456         497 :   GEN F, M, f, fc, ME, dB, B, a0, V = NULL;
    4457         497 :   long lM, lF = lg(vF), j;
    4458             : 
    4459         497 :   if (lF == 1) return cgetg(1,t_MAT);
    4460         490 :   F = gel(vF,1);
    4461         490 :   if (lg(F) == 5)
    4462             :   { /* chicompat */
    4463         238 :     V = gmael(F,4,4);
    4464         238 :     if (typ(V) == t_INT) V = NULL;
    4465             :   }
    4466         490 :   M = gmael(F,2,2); /* BAS */
    4467         490 :   lM = lg(M);
    4468         490 :   j = mfvec_first_cusp(M);
    4469         490 :   if (j == 1) ME = NULL;
    4470             :   else
    4471             :   { /* BAS starts by Eisenstein */
    4472         105 :     ME = mfvectomat(vecslice(M,1,j-1), n, 1);
    4473         105 :     M = vecslice(M, j,lM-1);
    4474             :   }
    4475         490 :   M = bhnmat_extend_nocache(NULL, N, n, 1, M);
    4476         490 :   if (ME) M = shallowconcat(ME,M);
    4477             :   /* M = mfcoefs of BAS */
    4478         490 :   B = cgetg(lF, t_MAT);
    4479         490 :   dB= cgetg(lF, t_VEC);
    4480        2023 :   for (j = 1; j < lF; j++)
    4481             :   {
    4482        1533 :     GEN g = gel(vF, j); /* t_MF_DIV */
    4483        1533 :     gel(B,j) = RgM_RgC_mul(M, gmael(g,2,3));
    4484        1533 :     gel(dB,j)= gmael(g,2,4);
    4485             :   }
    4486         490 :   f = mfcoefsser(gel(F,3),n);
    4487         490 :   a0 = polcoef_i(f, 0, -1);
    4488         490 :   if (gequal0(a0) || gequal1(a0))
    4489         266 :     a0 = NULL;
    4490             :   else
    4491         224 :     f = gdiv(ser_unscale(f, a0), a0);
    4492         490 :   fc = ginv(f);
    4493        2023 :   for (j = 1; j < lF; j++)
    4494             :   {
    4495        1533 :     pari_sp av = avma;
    4496        1533 :     GEN LISer = RgV_to_ser_full(gel(B,j)), f;
    4497        1533 :     if (a0) LISer = gdiv(ser_unscale(LISer, a0), a0);
    4498        1533 :     f = gmul(LISer, fc);
    4499        1533 :     if (a0) f = ser_unscale(f, ginv(a0));
    4500        1533 :     f = sertocol(f); setlg(f, n+2);
    4501        1533 :     if (!gequal1(gel(dB,j))) f = RgC_Rg_div(f, gel(dB,j));
    4502        1533 :     gel(B,j) = gerepileupto(av,f);
    4503             :   }
    4504         490 :   if (V) B = gmodulo(QabM_tracerel(V, 0, B), gel(V,1));
    4505         490 :   return B;
    4506             : }
    4507             : 
    4508             : static GEN
    4509         189 : mfheckemat_mfcoefs(GEN mf, GEN B, GEN DATA)
    4510             : {
    4511         189 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4512         189 :   long j, l = lg(B), sb = mfsturm_mf(mf)-1;
    4513         189 :   GEN b = MF_get_basis(mf), Q = cgetg(l, t_VEC);
    4514         609 :   for (j = 1; j < l; j++)
    4515             :   {
    4516         420 :     GEN v = hecke_i(sb, 1, gel(B,j), gel(b,j), DATA); /* Tn b[j] */
    4517         420 :     settyp(v,t_COL); gel(Q,j) = vecpermute(v, Mindex);
    4518             :   }
    4519         189 :   return Minv_RgM_mul(Minv,Q);
    4520             : }
    4521             : /* T_p^2, p prime, 1/2-integral weight; B = mfcoefs(mf,sb*p^2,1) or (mf,sb,p^2)
    4522             :  * if p|N */
    4523             : static GEN
    4524           7 : mfheckemat_mfcoefs_p2(GEN mf, long p, GEN B)
    4525             : {
    4526           7 :   pari_sp av = avma;
    4527           7 :   GEN DATA = heckef2_data(MF_get_N(mf), p*p);
    4528           7 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, DATA));
    4529             : }
    4530             : /* convert Mindex from row-index to mfcoef indexation: a(n) is stored in
    4531             :  * mfcoefs()[n+1], so subtract 1 from all indices */
    4532             : static GEN
    4533          49 : Mindex_as_coef(GEN mf)
    4534             : {
    4535          49 :   GEN v, Mindex = MF_get_Mindex(mf);
    4536          49 :   long i, l = lg(Mindex);
    4537          49 :   v = cgetg(l, t_VECSMALL);
    4538          49 :   for (i = 1; i < l; i++) v[i] = Mindex[i]-1;
    4539          49 :   return v;
    4540             : }
    4541             : /* T_p, p prime; B = mfcoefs(mf,sb*p,1) or (mf,sb,p) if p|N; integral weight */
    4542             : static GEN
    4543          35 : mfheckemat_mfcoefs_p(GEN mf, long p, GEN B)
    4544             : {
    4545          35 :   pari_sp av = avma;
    4546          35 :   GEN vm, Q, C, Minv = MF_get_Minv(mf);
    4547          35 :   long lm, k, i, j, l = lg(B), N = MF_get_N(mf);
    4548             : 
    4549          35 :   if (N % p == 0) return Minv_RgM_mul(Minv, rowpermute(B, MF_get_Mindex(mf)));
    4550          21 :   k = MF_get_k(mf);
    4551          21 :   C = gmul(mfchareval_i(MF_get_CHI(mf), p), powuu(p, k-1));
    4552          21 :   vm = Mindex_as_coef(mf); lm = lg(vm);
    4553          21 :   Q = cgetg(l, t_MAT);
    4554          21 :   for (j = 1; j < l; j++) gel(Q,j) = cgetg(lm, t_COL);
    4555         147 :   for (i = 1; i < lm; i++)
    4556             :   {
    4557         126 :     long m = vm[i], mp = m*p;
    4558         126 :     GEN Cm = (m % p) == 0? C : NULL;
    4559        1260 :     for (j = 1; j < l; j++)
    4560             :     {
    4561        1134 :       GEN S = gel(B,j), s = gel(S, mp + 1);
    4562        1134 :       if (Cm) s = gadd(s, gmul(C, gel(S, m/p + 1)));
    4563        1134 :       gcoeff(Q, i, j) = s;
    4564             :     }
    4565             :   }
    4566          21 :   return gerepileupto(av, Minv_RgM_mul(Minv,Q));
    4567             : }
    4568             : /* Matrix of T(p), p prime, dim(mf) > 0 and integral weight */
    4569             : static GEN
    4570         182 : mfheckemat_p(GEN mf, long p)
    4571             : {
    4572         182 :   pari_sp av = avma;
    4573         182 :   long N = MF_get_N(mf), sb = mfsturm_mf(mf)-1;
    4574         182 :   GEN B = (N % p)? mfcoefs_mf(mf, sb * p, 1): mfcoefs_mf(mf, sb, p);
    4575         182 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, hecke_data(N,p)));
    4576             : }
    4577             : 
    4578             : /* mf_NEW != (0), weight > 1, p prime. Use
    4579             :  * T(p) T(j) = T(j*p) + p^{k-1} \chi(p) 1_{p | j, p \nmid N} T(j/p) */
    4580             : static GEN
    4581         854 : mfnewmathecke_p(GEN mf, long p)
    4582             : {
    4583         854 :   pari_sp av = avma;
    4584         854 :   GEN tf, vj = MFnew_get_vj(mf), CHI = MF_get_CHI(mf);
    4585         854 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4586         854 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4587         854 :   long i, j, lvj = lg(vj), lim = vj[lvj-1] * p;
    4588         854 :   GEN M, perm, V, need = zero_zv(lim);
    4589         854 :   GEN C = (N % p)? gmul(mfchareval_i(CHI,p), powuu(p,k-1)): NULL;
    4590         854 :   tf = mftraceform_new(N, k, CHI);
    4591        3668 :   for (i = 1; i < lvj; i++)
    4592             :   {
    4593        2814 :     j = vj[i]; need[j*p] = 1;
    4594        2814 :     if (N % p && j % p == 0) need[j/p] = 1;
    4595             :   }
    4596         854 :   perm = zero_zv(lim);
    4597         854 :   V = cgetg(lim+1, t_VEC);
    4598       11886 :   for (i = j = 1; i <= lim; i++)
    4599       11032 :     if (need[i]) { gel(V,j) = mfhecke_i(i, N, tf); perm[i] = j; j++; }
    4600         854 :   setlg(V, j);
    4601         854 :   V = bhnmat_extend_nocache(NULL, N, mfsturm_mf(mf)-1, 1, V);
    4602         854 :   V = rowpermute(V, Mindex); /* V[perm[i]] = coeffs(T_i newtrace) */
    4603         854 :   M = cgetg(lvj, t_MAT);
    4604        3668 :   for (i = 1; i < lvj; i++)
    4605             :   {
    4606             :     GEN t;
    4607        2814 :     j = vj[i]; t = gel(V, perm[j*p]);
    4608        2814 :     if (C && j % p == 0) t = RgC_add(t, RgC_Rg_mul(gel(V, perm[j/p]),C));
    4609        2814 :     gel(M,i) = t;
    4610             :   }
    4611         854 :   return gerepileupto(av, Minv_RgM_mul(Minv, M));
    4612             : }
    4613             : 
    4614             : GEN
    4615          77 : mfheckemat(GEN mf, GEN vn)
    4616             : {
    4617          77 :   pari_sp av = avma;
    4618          77 :   long lv, lvP, i, N, dim, nk, dk, p, sb, flint = (typ(vn)==t_INT);
    4619             :   GEN CHI, res, vT, FA, B, vP;
    4620             : 
    4621          77 :   mf = checkMF(mf);
    4622          77 :   if (typ(vn) != t_VECSMALL) vn = gtovecsmall(vn);
    4623          77 :   N = MF_get_N(mf); CHI = MF_get_CHI(mf); Qtoss(MF_get_gk(mf), &nk, &dk);
    4624          77 :   dim = MF_get_dim(mf);
    4625          77 :   lv = lg(vn);
    4626          77 :   res = cgetg(lv, t_VEC);
    4627          77 :   FA = cgetg(lv, t_VEC);
    4628          77 :   vP = cgetg(lv, t_VEC);
    4629          77 :   vT = const_vec(vecsmall_max(vn), NULL);
    4630         182 :   for (i = 1; i < lv; i++)
    4631             :   {
    4632         105 :     ulong n = (ulong)labs(vn[i]);
    4633             :     GEN fa;
    4634         105 :     if (!n) pari_err_TYPE("mfheckemat", vn);
    4635         105 :     if (dk == 1 || uissquareall(n, &n)) fa = myfactoru(n);
    4636           0 :     else { n = 0; fa = myfactoru(1); } /* dummy: T_{vn[i]} = 0 */
    4637         105 :     vn[i] = n;
    4638         105 :     gel(FA,i) = fa;
    4639         105 :     gel(vP,i) = gel(fa,1);
    4640             :   }
    4641          77 :   vP = shallowconcat1(vP); vecsmall_sort(vP);
    4642          77 :   vP = vecsmall_uniq_sorted(vP); /* all primes occurring in vn */
    4643          77 :   lvP = lg(vP); if (lvP == 1) goto END;
    4644          56 :   p = vP[lvP-1];
    4645          56 :   sb = mfsturm_mf(mf)-1;
    4646          56 :   if (dk == 1 && nk != 1 && MF_get_space(mf) == mf_NEW)
    4647          21 :     B = NULL; /* special purpose mfnewmathecke_p is faster */
    4648          35 :   else if (lvP == 2 && N % p == 0)
    4649          21 :     B = mfcoefs_mf(mf, sb, dk==2? p*p: p); /* single prime | N, can optimize */
    4650             :   else
    4651          14 :     B = mfcoefs_mf(mf, sb * (dk==2? p*p: p), 1); /* general initialization */
    4652         126 :   for (i = 1; i < lvP; i++)
    4653             :   {
    4654          70 :     long j, l, q, e = 1;
    4655             :     GEN C, Tp, u1, u0;
    4656          70 :     p = vP[i];
    4657          70 :     for (j = 1; j < lv; j++) e = maxss(e, z_lval(vn[j], p));
    4658          70 :     if (!B)
    4659          28 :       Tp = mfnewmathecke_p(mf, p);
    4660          42 :     else if (dk == 2)
    4661           7 :       Tp = mfheckemat_mfcoefs_p2(mf,p, (lvP==2||N%p)? B: matdeflate(sb,p*p,B));
    4662             :     else
    4663          35 :       Tp = mfheckemat_mfcoefs_p(mf, p, (lvP==2||N%p)? B: matdeflate(sb,p,B));
    4664          70 :     gel(vT, p) = Tp;
    4665          70 :     if (e == 1) continue;
    4666          14 :     u0 = gen_1;
    4667          14 :     if (dk == 2)
    4668             :     {
    4669           0 :       C = N % p? gmul(mfchareval_i(CHI,p*p), powuu(p, nk-2)): NULL;
    4670           0 :       if (e == 2) u0 = sstoQ(p+1,p); /* special case T_{p^4} */
    4671             :     }
    4672             :     else
    4673          14 :       C = N % p? gmul(mfchareval_i(CHI,p),   powuu(p, nk-1)): NULL;
    4674          28 :     for (u1=Tp, q=p, l=2; l <= e; l++)
    4675             :     { /* u0 = T_{p^{l-2}}, u1 = T_{p^{l-1}} for l > 2 */
    4676          14 :       GEN v = gmul(Tp, u1);
    4677          14 :       if (C) v = gsub(v, gmul(C, u0));
    4678             :       /* q = p^l, vT[q] = T_q for k integer else T_{q^2} */
    4679          14 :       q *= p; u0 = u1; gel(vT, q) = u1 = v;
    4680             :     }
    4681             :   }
    4682             : END:
    4683             :   /* vT[p^e] = T_{p^e} for all p^e occurring below */
    4684         182 :   for (i = 1; i < lv; i++)
    4685             :   {
    4686         105 :     long n = vn[i], j, lP;
    4687             :     GEN fa, P, E, M;
    4688         105 :     if (n == 0) { gel(res,i) = zeromat(dim,dim); continue; }
    4689         105 :     if (n == 1) { gel(res,i) = matid(dim); continue; }
    4690          77 :     fa = gel(FA,i);
    4691          77 :     P = gel(fa,1); lP = lg(P);
    4692          77 :     E = gel(fa,2); M = gel(vT, upowuu(P[1], E[1]));
    4693          77 :     for (j = 2; j < lP; j++) M = RgM_mul(M, gel(vT, upowuu(P[j], E[j])));
    4694          77 :     gel(res,i) = M;
    4695             :   }
    4696          77 :   if (flint) res = gel(res,1);
    4697          77 :   return gerepilecopy(av, res);
    4698             : }
    4699             : 
    4700             : 
    4701             : /* f = \sum_i v[i] T_listj[i] (Trace Form) attached to v; replace by f/a_1(f) */
    4702             : static GEN
    4703        1288 : mf_normalize(GEN mf, GEN v)
    4704             : {
    4705        1288 :   GEN c, dc = NULL, M = MF_get_M(mf), Mindex = MF_get_Mindex(mf);
    4706        1288 :   v = Q_primpart(v);
    4707        1288 :   c = RgMrow_RgC_mul(M, v, 2); /* a_1(f) */
    4708        1288 :   if (gequal1(c)) return v;
    4709         756 :   if (typ(c) == t_POL) c = gmodulo(c, mfcharpol(MF_get_CHI(mf)));
    4710         756 :   if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1 && degpol(gel(c,1)) >= 40
    4711           7 :                          && Mindex[1] == 2
    4712           7 :                          && mfcharorder(MF_get_CHI(mf)) <= 2)
    4713           7 :   { /* normalize using expansion at infinity (small coefficients) */
    4714           7 :     GEN w, P = gel(c,1), a1 = gel(c,2);
    4715           7 :     long i, l = lg(Mindex);
    4716           7 :     w = cgetg(l, t_COL);
    4717           7 :     gel(w,1) = gen_1;
    4718         280 :     for (i = 2; i < l; i++)
    4719             :     {
    4720         273 :       c = liftpol_shallow(RgMrow_RgC_mul(M, v, Mindex[i]));
    4721         273 :       gel(w,i) = QXQ_div_ratlift(c, a1, P);
    4722             :     }
    4723             :     /* w = expansion at oo of normalized form */
    4724           7 :     v = Minv_RgC_mul(MF_get_Minv(mf), Q_remove_denom(w, &dc));
    4725           7 :     v = gmodulo(v, P); /* back to mfbasis coefficients */
    4726             :   }
    4727             :   else
    4728             :   {
    4729         749 :     c = ginv(c);
    4730         749 :     if (typ(c) == t_POLMOD) c = Q_remove_denom(c, &dc);
    4731         749 :     v = RgC_Rg_mul(v, c);
    4732             :   }
    4733         756 :   if (dc) v = RgC_Rg_div(v, dc);
    4734         756 :   return v;
    4735             : }
    4736             : static void
    4737         329 : pol_red(GEN NF, GEN *pP, GEN *pa, long flag)
    4738             : {
    4739         329 :   GEN dP, a, P = *pP;
    4740         329 :   long d = degpol(P);
    4741             : 
    4742         329 :   *pa = a = pol_x(varn(P));
    4743         329 :   if (d > 30) return;
    4744             : 
    4745         322 :   dP = RgX_disc(P);
    4746         322 :   if (typ(dP) != t_INT)
    4747          84 :   { dP = gnorm(dP); if (typ(dP) != t_INT) pari_err_BUG("mfnewsplit"); }
    4748         322 :   if (d == 2 || expi(dP) < 62)
    4749             :   {
    4750         294 :     if (expi(dP) < 31)
    4751         294 :       P = NF? rnfpolredabs(NF, P,flag): polredabs0(P,flag);
    4752             :     else
    4753           0 :       P = NF? rnfpolredbest(NF,P,flag): polredbest(P,flag);
    4754         294 :     if (flag)
    4755             :     {
    4756         266 :       a = gel(P,2); if (typ(a) == t_POLMOD) a = gel(a,2);
    4757         266 :       P = gel(P,1);
    4758             :     }
    4759             :   }
    4760         322 :   *pP = P;
    4761         322 :   *pa = a;
    4762             : }
    4763             : 
    4764             : /* Diagonalize and normalize. See mfsplit for meaning of flag. */
    4765             : static GEN
    4766         945 : mfspclean(GEN mf, GEN mf0, GEN NF, long ord, GEN simplesp, long flag)
    4767             : {
    4768         945 :   const long vz = 1;
    4769         945 :   long i, l = lg(simplesp), dim = MF_get_dim(mf);
    4770         945 :   GEN res = cgetg(l, t_MAT), pols = cgetg(l, t_VEC);
    4771         945 :   GEN zeros = (mf == mf0)? NULL: zerocol(dim - MF_get_dim(mf0));
    4772        2261 :   for (i = 1; i < l; i++)
    4773             :   {
    4774        1316 :     GEN ATP = gel(simplesp, i), A = gel(ATP,1), P = gel(ATP,3);
    4775        1316 :     long d = degpol(P);
    4776        1316 :     GEN a, v = (flag && d > flag)? NULL: gel(A,1);
    4777        1316 :     if (d == 1) P = pol_x(vz);
    4778             :     else
    4779             :     {
    4780         329 :       pol_red(NF, &P, &a, !!v);
    4781         329 :       if (v)
    4782             :       { /* Mod(a,P) root of charpoly(T), K*gpowers(a) = eigenvector of T */
    4783         301 :         GEN K, den, M = cgetg(d+1, t_MAT), T = gel(ATP,2);
    4784             :         long j;
    4785         301 :         T = shallowtrans(T);
    4786         301 :         gel(M,1) = vec_ei(d,1); /* basis of cyclic vectors */
    4787         301 :         for (j = 2; j <= d; j++) gel(M,j) = RgM_RgC_mul(T, gel(M,j-1));
    4788         301 :         M = Q_primpart(M);
    4789         413 :         K = NF? ZabM_inv(liftpol_shallow(M), nf_get_pol(NF), ord, &den)
    4790         413 :               : ZM_inv(M,&den);
    4791         301 :         K = shallowtrans(K);
    4792         301 :         v = gequalX(a)? pol_x_powers(d, vz): RgXQ_powers(a, d-1, P);
    4793         301 :         v = gmodulo(RgM_RgC_mul(A, RgM_RgC_mul(K,v)), P);
    4794             :       }
    4795             :     }
    4796        1316 :     if (v)
    4797             :     {
    4798        1288 :       v = mf_normalize(mf0, v); if (zeros) v = shallowconcat(zeros,v);
    4799        1288 :       gel(res,i) = v; if (flag) setlg(res,i+1);
    4800             :     }
    4801             :     else
    4802          28 :       gel(res,i) = zerocol(dim);
    4803        1316 :     gel(pols,i) = P;
    4804             :   }
    4805         945 :   return mkvec2(res, pols);
    4806             : }
    4807             : 
    4808             : /* return v = v_{X-r}(P), and set Z = P / (X-r)^v */
    4809             : static long
    4810          63 : RgX_valrem_root(GEN P, GEN r, GEN *Z)
    4811             : {
    4812             :   long v;
    4813         126 :   for (v = 0; degpol(P); v++)
    4814             :   {
    4815         126 :     GEN t, Q = RgX_div_by_X_x(P, r, &t);
    4816         126 :     if (!gequal0(t)) break;
    4817          63 :     P = Q;
    4818             :   }
    4819          63 :   *Z = P; return v;
    4820             : }
    4821             : static GEN
    4822        1050 : mynffactor(GEN NF, GEN P, long dimlim)
    4823             : {
    4824             :   long i, l, v;
    4825             :   GEN R, E;
    4826        1050 :   if (dimlim != 1)
    4827             :   {
    4828         497 :     R = NF? nffactor(NF, P): QX_factor(P);
    4829         497 :     if (!dimlim) return R;
    4830          21 :     E = gel(R,2);
    4831          21 :     R = gel(R,1); l = lg(R);
    4832          98 :     for (i = 1; i < l; i++)
    4833          91 :       if (degpol(gel(R,i)) > dimlim) break;
    4834          21 :     if (i == 1) return NULL;
    4835          21 :     setlg(E,i);
    4836          21 :     setlg(R,i); return mkmat2(R, E);
    4837             :   }
    4838             :   /* dimlim = 1 */
    4839         553 :   R = nfroots(NF, P); l = lg(R);
    4840         553 :   if (l == 1) return NULL;
    4841         490 :   v = varn(P);
    4842         490 :   settyp(R, t_COL);
    4843         490 :   if (degpol(P) == l-1)
    4844         441 :     E = const_col(l-1, gen_1);
    4845             :   else
    4846             :   {
    4847          49 :     E = cgetg(l, t_COL);
    4848          49 :     for (i = 1; i < l; i++) gel(E,i) = utoi(RgX_valrem_root(P, gel(R,i), &P));
    4849             :   }
    4850         490 :   R = deg1_from_roots(R, v);
    4851         490 :   return mkmat2(R, E);
    4852             : }
    4853             : 
    4854             : /* Let K be a number field attached to NF (Q if NF = NULL). A K-vector
    4855             :  * space of dimension d > 0 is given by a t_MAT A (n x d, full column rank)
    4856             :  * giving a K-basis, X a section (d x n: left pseudo-inverse of A). Return a
    4857             :  * pair (T, fa), where T is an element of the Hecke algebra (a sum of Tp taken
    4858             :  * from vector vTp) acting on A (a d x d t_MAT) and fa is the factorization of
    4859             :  * its characteristic polynomial, limited to factors of degree <= dimlim if
    4860             :  * dimlim != 0 (return NULL if there are no factors of degree <= dimlim) */
    4861             : static GEN
    4862        1043 : findbestsplit(GEN NF, GEN vTp, GEN A, GEN X, long dimlim, long vz)
    4863             : {
    4864        1043 :   GEN T = NULL, Tkeep = NULL, fakeep = NULL;
    4865        1043 :   long lmax = 0, i, lT = lg(vTp);
    4866        2254 :   for (i = 1; i < lT; i++)
    4867             :   {
    4868        1127 :     GEN D, P, E, fa, TpA = gel(vTp,i);
    4869             :     long l;
    4870        2107 :     if (typ(TpA) == t_INT) break;
    4871        1050 :     if (lg(TpA) > lg(A)) TpA = RgM_mul(X, RgM_mul(TpA, A)); /* Tp | A */
    4872        1050 :     T = T ? RgM_add(T, TpA) : TpA;
    4873        1050 :     if (!NF) { P = QM_charpoly_ZX(T); setvarn(P, vz); }
    4874             :     else
    4875             :     {
    4876         203 :       P = charpoly(Q_remove_denom(T, &D), vz);
    4877         203 :       if (D) P = gdiv(RgX_unscale(P, D), powiu(D, degpol(P)));
    4878             :     }
    4879        1050 :     fa = mynffactor(NF, P, dimlim);
    4880        1050 :     if (!fa) return NULL;
    4881         987 :     E = gel(fa, 2);
    4882             :     /* characteristic polynomial is separable ? */
    4883         987 :     if (isint1(vecmax(E))) { Tkeep = T; fakeep = fa; break; }
    4884          84 :     l = lg(E);
    4885             :     /* characteristic polynomial has more factors than before ? */
    4886          84 :     if (l > lmax) { lmax = l; Tkeep = T; fakeep = fa; }
    4887             :   }
    4888         980 :   return mkvec2(Tkeep, fakeep);
    4889             : }
    4890             : 
    4891             : static GEN
    4892         161 : nfcontent(GEN nf, GEN v)
    4893             : {
    4894         161 :   long i, l = lg(v);
    4895         161 :   GEN c = gel(v,1);
    4896         161 :   for (i = 2; i < l; i++) c = idealadd(nf, c, gel(v,i));
    4897         161 :   if (typ(c) == t_MAT && gequal1(gcoeff(c,1,1))) c = gen_1;
    4898         161 :   return c;
    4899             : }
    4900             : static GEN
    4901         252 : nf_primpart(GEN nf, GEN B)
    4902             : {
    4903         252 :   switch(typ(B))
    4904             :   {
    4905             :     case t_COL:
    4906             :     {
    4907         161 :       GEN A = matalgtobasis(nf, B), c = nfcontent(nf, A);
    4908         161 :       if (typ(c) == t_INT) return B;
    4909          21 :       c = idealred_elt(nf,c);
    4910          21 :       A = Q_primpart( nfC_nf_mul(nf, A, Q_primpart(nfinv(nf,c))) );
    4911          21 :       A = liftpol_shallow( matbasistoalg(nf, A) );
    4912          21 :       if (gexpo(A) > gexpo(B)) A = B;
    4913          21 :       return A;
    4914             :     }
    4915             :     case t_MAT:
    4916             :     {
    4917             :       long i, l;
    4918          91 :       GEN A = cgetg_copy(B, &l);
    4919          91 :       for (i = 1; i < l; i++) gel(A,i) = nf_primpart(nf, gel(B,i));
    4920          91 :       return A;
    4921             :     }
    4922             :     default:
    4923           0 :       pari_err_TYPE("nf_primpart", B);
    4924             :       return NULL; /*LCOV_EXCL_LINE*/
    4925             :   }
    4926             : }
    4927             : 
    4928             : /* rotate entries of v to accomodate new entry 'x' (push out oldest entry) */
    4929             : static void
    4930        1008 : vecpush(GEN v, GEN x)
    4931             : {
    4932             :   long i;
    4933        1008 :   for (i = lg(v)-1; i > 1; i--) gel(v,i) = gel(v,i-1);
    4934        1008 :   gel(v,1) = x;
    4935        1008 : }
    4936             : 
    4937             : /* sort t_VEC of vector spaces by increasing dimension */
    4938             : static GEN
    4939         945 : sort_by_dim(GEN v)
    4940             : {
    4941         945 :   long i, l = lg(v);
    4942         945 :   GEN D = cgetg(l, t_VECSMALL);
    4943         945 :   for (i = 1; i < l; i++) D[i] = lg(gmael(v,i,2));
    4944         945 :   return vecpermute(v , vecsmall_indexsort(D));
    4945             : }
    4946             : static GEN
    4947         945 : split_starting_space(GEN mf)
    4948             : {
    4949         945 :   long d = MF_get_dim(mf), d2;
    4950         945 :   GEN id = matid(d);
    4951         945 :   switch(MF_get_space(mf))
    4952             :   {
    4953             :     case mf_NEW:
    4954         938 :     case mf_CUSP: return mkvec2(id, id);
    4955             :   }
    4956           7 :   d2 = lg(MF_get_S(mf))-1;
    4957           7 :   return mkvec2(vecslice(id, d-d2+1,d),
    4958             :                 shallowconcat(zeromat(d2,d-d2),matid(d2)));
    4959             : }
    4960             : /* If dimlim > 0, keep only the dimension <= dimlim eigenspaces.
    4961             :  * See mfsplit for the meaning of flag. */
    4962             : static GEN
    4963        1337 : split_ii(GEN mf, long dimlim, long flag, long *pnewd)
    4964             : {
    4965             :   forprime_t iter;
    4966        1337 :   GEN CHI = MF_get_CHI(mf), empty = cgetg(1, t_VEC), mf0 = mf;
    4967             :   GEN NF, POLCYC, todosp, Tpbigvec, simplesp;
    4968        1337 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4969        1337 :   long ord, FC, NEWT, dimsimple = 0, newd = -1;
    4970        1337 :   const long NBH = 5, vz = 1;
    4971             :   ulong p;
    4972             : 
    4973        1337 :   switch(MF_get_space(mf))
    4974             :   {
    4975        1148 :     case mf_NEW: break;
    4976             :     case mf_CUSP:
    4977             :     case mf_FULL:
    4978         182 :       if (k > 1) { mf0 = mfinittonew(mf); break; }
    4979         175 :       newd = lg(MF_get_S(mf))-1 - mfolddim(N, k, CHI);
    4980         175 :       break;
    4981           7 :     default: pari_err_TYPE("mfsplit [space does not contain newspace]", mf);
    4982             :       return NULL; /*LCOV_EXCL_LINE*/
    4983             :   }
    4984        1330 :   if (newd < 0) newd = mf0? MF_get_dim(mf0): 0;
    4985        1330 :   *pnewd = newd;
    4986        1330 :   if (!newd) return mkvec2(cgetg(1, t_MAT), empty);
    4987             : 
    4988         945 :   NEWT = (k > 1 && MF_get_space(mf0) == mf_NEW);
    4989         945 :   todosp = mkvec( split_starting_space(mf0) );
    4990         945 :   simplesp = empty;
    4991         945 :   FC = mfcharconductor(CHI);
    4992         945 :   ord = mfcharorder(CHI);
    4993         945 :   if (ord <= 2) NF = POLCYC = NULL;
    4994             :   else
    4995             :   {
    4996         161 :     POLCYC = mfcharpol(CHI);
    4997         161 :     NF = nfinit(POLCYC,DEFAULTPREC);
    4998             :   }
    4999         945 :   Tpbigvec = zerovec(NBH);
    5000         945 :   u_forprime_init(&iter, 2, ULONG_MAX);
    5001         945 :   while (dimsimple < newd && (p = u_forprime_next(&iter)))
    5002             :   {
    5003             :     GEN nextsp;
    5004             :     long ind;
    5005        1253 :     if (N % (p*p) == 0 && N/p % FC == 0) continue; /* T_p = 0 in this case */
    5006        1008 :     vecpush(Tpbigvec, NEWT? mfnewmathecke_p(mf0,p): mfheckemat_p(mf0,p));
    5007        1008 :     nextsp = empty;
    5008        2051 :     for (ind = 1; ind < lg(todosp); ind++)
    5009             :     {
    5010        1043 :       GEN tmp = gel(todosp, ind), fa, P, E, D, Tp, DTp;
    5011        1043 :       GEN A = gel(tmp, 1);
    5012        1043 :       GEN X = gel(tmp, 2);
    5013             :       long lP, i;
    5014        1043 :       tmp = findbestsplit(NF, Tpbigvec, A, X, dimlim, vz);
    5015        1764 :       if (!tmp) continue; /* nothing there */
    5016         980 :       Tp = gel(tmp, 1);
    5017         980 :       fa = gel(tmp, 2);
    5018         980 :       P = gel(fa, 1);
    5019         980 :       E = gel(fa, 2); lP = lg(P);
    5020             :       /* lP > 1 */
    5021         980 :       if (DEBUGLEVEL) err_printf("Exponents = %Ps\n", E);
    5022         980 :       if (lP == 2)
    5023             :       {
    5024         700 :         GEN P1 = gel(P,1);
    5025         700 :         long e1 = itos(gel(E,1)), d1 = degpol(P1);
    5026         700 :         if (e1 * d1 == lg(Tp)-1)
    5027             :         {
    5028         658 :           if (e1 > 1) nextsp = vec_append(nextsp, mkvec2(A,X));
    5029             :           else
    5030             :           { /* simple module */
    5031         644 :             simplesp = vec_append(simplesp, mkvec3(A,Tp,P1));
    5032         644 :             dimsimple += d1;
    5033             :           }
    5034         658 :           continue;
    5035             :         }
    5036             :       }
    5037             :       /* Found splitting */
    5038         322 :       DTp = Q_remove_denom(Tp, &D);
    5039        1092 :       for (i = 1; i < lP; i++)
    5040             :       {
    5041         770 :         GEN Ai, Xi, dXi, AAi, v, y, Pi = gel(P,i);
    5042         770 :         Ai = RgX_RgM_eval(D? RgX_rescale(Pi,D): Pi, DTp);
    5043         770 :         Ai = QabM_ker(Ai, POLCYC, ord);
    5044         770 :         if (NF) Ai = nf_primpart(NF, Ai);
    5045             : 
    5046         770 :         AAi = RgM_mul(A, Ai);
    5047             :         /* gives section, works on nonsquare matrices */
    5048         770 :         Xi = QabM_pseudoinv(Ai, POLCYC, ord, &v, &dXi);
    5049         770 :         Xi = RgM_Rg_div(Xi, dXi);
    5050         770 :         y = gel(v,1);
    5051         770 :         if (isint1(gel(E,i)))
    5052             :         {
    5053         672 :           GEN Tpi = RgM_mul(Xi, RgM_mul(rowpermute(Tp,y), Ai));
    5054         672 :           simplesp = vec_append(simplesp, mkvec3(AAi, Tpi, Pi));
    5055         672 :           dimsimple += degpol(Pi);
    5056             :         }
    5057             :         else
    5058             :         {
    5059          98 :           Xi = RgM_mul(Xi, rowpermute(X,y));
    5060          98 :           nextsp = vec_append(nextsp, mkvec2(AAi, Xi));
    5061             :         }
    5062             :       }
    5063             :     }
    5064        1008 :     todosp = nextsp; if (lg(todosp) == 1) break;
    5065             :   }
    5066         945 :   if (DEBUGLEVEL) err_printf("end split, need to clean\n");
    5067         945 :   return mfspclean(mf, mf0, NF, ord, sort_by_dim(simplesp), flag);
    5068             : }
    5069             : static GEN
    5070          14 : dim_filter(GEN v, long dim)
    5071             : {
    5072          14 :   GEN P = gel(v,2);
    5073          14 :   long j, l = lg(P);
    5074         112 :   for (j = 1; j < l; j++)
    5075         105 :     if (degpol(gel(P,j)) > dim)
    5076             :     {
    5077           7 :       v = mkvec2(vecslice(gel(v,1),1,j-1), vecslice(P,1,j-1));
    5078           7 :       break;
    5079             :     }
    5080          14 :   return v;
    5081             : }
    5082             : static long
    5083         196 : dim_sum(GEN v)
    5084             : {
    5085         196 :   GEN P = gel(v,2);
    5086         196 :   long j, l = lg(P), d = 0;
    5087         196 :   for (j = 1; j < l; j++) d += degpol(gel(P,j));
    5088         196 :   return d;
    5089             : }
    5090             : static GEN
    5091        1274 : split_i(GEN mf, long dimlim, long flag)
    5092        1274 : { long junk; return split_ii(mf, dimlim, flag, &junk); }
    5093             : /* mf is either already split or output by mfinit. Splitting is done only for
    5094             :  * newspace except in weight 1. If flag = 0 (default) split completely.
    5095             :  * If flag = d > 0, only give the Galois polynomials in degree > d
    5096             :  * Flag is ignored if dimlim = 1. */
    5097             : GEN
    5098          77 : mfsplit(GEN mf0, long dimlim, long flag)
    5099             : {
    5100          77 :   pari_sp av = avma;
    5101          77 :   GEN v, mf = checkMF_i(mf0);
    5102          77 :   if (!mf) pari_err_TYPE("mfsplit", mf0);
    5103          77 :   if ((v = obj_check(mf, MF_SPLIT)))
    5104          14 :   { if (dimlim) v = dim_filter(v, dimlim); }
    5105          63 :   else if (dimlim && (v = obj_check(mf, MF_SPLITN)))
    5106          21 :   { v = (itos(gel(v,1)) >= dimlim)? dim_filter(gel(v,2), dimlim): NULL; }
    5107          77 :   if (!v)
    5108             :   {
    5109             :     long newd;
    5110          63 :     v = split_ii(mf, dimlim, flag, &newd);
    5111          63 :     if (lg(v) == 1) obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5112          63 :     else if (!flag)
    5113             :     {
    5114          42 :       if (dim_sum(v) == newd) obj_insert(mf, MF_SPLIT,v);
    5115          21 :       else obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5116             :     }
    5117             :   }
    5118          77 :   return gerepilecopy(av, v);
    5119             : }
    5120             : static GEN
    5121         371 : split(GEN mf) { return split_i(mf,0,0); }
    5122             : GEN
    5123         714 : MF_get_newforms(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),1); }
    5124             : GEN
    5125         539 : MF_get_fields(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),2); }
    5126             : 
    5127             : /*************************************************************************/
    5128             : /*                     Modular forms of Weight 1                         */
    5129             : /*************************************************************************/
    5130             : /* S_1(G_0(N)), small N. Return 1 if definitely empty; return 0 if maybe
    5131             :  * non-empty  */
    5132             : static int
    5133       16065 : wt1empty(long N)
    5134             : {
    5135       16065 :   if (N <= 100) switch (N)
    5136             :   { /* non-empty [32/100] */
    5137             :     case 23: case 31: case 39: case 44: case 46:
    5138             :     case 47: case 52: case 55: case 56: case 57:
    5139             :     case 59: case 62: case 63: case 68: case 69:
    5140             :     case 71: case 72: case 76: case 77: case 78:
    5141             :     case 79: case 80: case 83: case 84: case 87:
    5142             :     case 88: case 92: case 93: case 94: case 95:
    5143        5439 :     case 99: case 100: return 0;
    5144        3472 :     default: return 1;
    5145             :   }
    5146        7154 :   if (N <= 600) switch(N)
    5147             :   { /* empty [111/500] */
    5148             :     case 101: case 102: case 105: case 106: case 109:
    5149             :     case 113: case 121: case 122: case 123: case 125:
    5150             :     case 130: case 134: case 137: case 146: case 149:
    5151             :     case 150: case 153: case 157: case 162: case 163:
    5152             :     case 169: case 170: case 173: case 178: case 181:
    5153             :     case 182: case 185: case 187: case 193: case 194:
    5154             :     case 197: case 202: case 205: case 210: case 218:
    5155             :     case 221: case 226: case 233: case 241: case 242:
    5156             :     case 245: case 246: case 250: case 257: case 265:
    5157             :     case 267: case 269: case 274: case 277: case 281:
    5158             :     case 289: case 293: case 298: case 305: case 306:
    5159             :     case 313: case 314: case 317: case 326: case 337:
    5160             :     case 338: case 346: case 349: case 353: case 361:
    5161             :     case 362: case 365: case 369: case 370: case 373:
    5162             :     case 374: case 377: case 386: case 389: case 394:
    5163             :     case 397: case 401: case 409: case 410: case 421:
    5164             :     case 425: case 427: case 433: case 442: case 449:
    5165             :     case 457: case 461: case 466: case 481: case 482:
    5166             :     case 485: case 490: case 493: case 509: case 514:
    5167             :     case 521: case 530: case 533: case 534: case 538:
    5168             :     case 541: case 545: case 554: case 557: case 562:
    5169             :     case 565: case 569: case 577: case 578: case 586:
    5170         336 :     case 593: return 1;
    5171        6804 :     default: return 0;
    5172             :   }
    5173          14 :   return 0;
    5174             : }
    5175             : 
    5176             : static GEN
    5177          28 : initwt1trace(GEN mf)
    5178             : {
    5179          28 :   GEN S = MF_get_S(mf), v, H;
    5180             :   long l, i;
    5181          28 :   if (lg(S) == 1) return mftrivial();
    5182          28 :   H = mfheckemat(mf, Mindex_as_coef(mf));
    5183          28 :   l = lg(H); v = cgetg(l, t_VEC);
    5184          28 :   for (i = 1; i < l; i++) gel(v,i) = gtrace(gel(H,i));
    5185          28 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5186          28 :   return mflineardiv_linear(S, v, 1);
    5187             : }
    5188             : static GEN
    5189          21 : initwt1newtrace(GEN mf)
    5190             : {
    5191          21 :   GEN v, D, S, Mindex, CHI = MF_get_CHI(mf);
    5192          21 :   long FC, lD, i, sb, N1, N2, lM, N = MF_get_N(mf);
    5193          21 :   CHI = mfchartoprimitive(CHI, &FC);
    5194          21 :   if (N % FC || mfcharparity(CHI) == 1) return mftrivial();
    5195          21 :   D = mydivisorsu(N/FC); lD = lg(D);
    5196          21 :   S = MF_get_S(mf);
    5197          21 :   if (lg(S) == 1) return mftrivial();
    5198          21 :   N2 = newd_params2(N);
    5199          21 :   N1 = N / N2;
    5200          21 :   Mindex = MF_get_Mindex(mf);
    5201          21 :   lM = lg(Mindex);
    5202          21 :   sb = Mindex[lM-1];
    5203          21 :   v = zerovec(sb+1);
    5204          42 :   for (i = 1; i < lD; i++)
    5205             :   {
    5206          21 :     long M = FC*D[i], j;
    5207          21 :     GEN tf = initwt1trace(M == N? mf: mfinit_Nkchi(M, 1, CHI, mf_CUSP, 0));
    5208             :     GEN listd, w;
    5209          21 :     if (mf_get_type(tf) == t_MF_CONST) continue;
    5210          21 :     w = mfcoefs_i(tf, sb, 1);
    5211          21 :     if (M == N) { v = gadd(v, w); continue; }
    5212           0 :     listd = mydivisorsu(u_ppo(ugcd(N/M, N1), FC));
    5213           0 :     for (j = 1; j < lg(listd); j++)
    5214             :     {
    5215           0 :       long d = listd[j], d2 = d*d; /* coprime to FC */
    5216           0 :       GEN dk = mfchareval_i(CHI, d);
    5217           0 :       long NMd = N/(M*d), m;
    5218           0 :       for (m = 1; m <= sb/d2; m++)
    5219             :       {
    5220           0 :         long be = mubeta2(NMd, m);
    5221           0 :         if (be)
    5222             :         {
    5223           0 :           GEN c = gmul(dk, gmulsg(be, gel(w, m+1)));
    5224           0 :           long n = m*d2;
    5225           0 :           gel(v, n+1) = gadd(gel(v, n+1), c);
    5226             :         }
    5227             :       }
    5228             :     }
    5229             :   }
    5230          21 :   if (gequal0(gel(v,2))) return mftrivial();
    5231          21 :   v = vecpermute(v,Mindex);
    5232          21 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5233          21 :   return mflineardiv_linear(S, v, 1);
    5234             : }
    5235             : 
    5236             : /* Matrix of T(p), p \nmid N */
    5237             : static GEN
    5238         196 : Tpmat(long p, long lim, GEN CHI)
    5239             : {
    5240         196 :   GEN M = zeromatcopy(lim, p*lim), chip = mfchareval_i(CHI, p); /* != 0 */
    5241             :   long i, j, pi, pj;
    5242         196 :   gcoeff(M, 1, 1) = gaddsg(1, chip);
    5243         196 :   for (i = 1, pi = p; i < lim; i++,  pi += p) gcoeff(M, i+1, pi+1) = gen_1;
    5244         196 :   for (j = 1, pj = p; pj < lim; j++, pj += p) gcoeff(M, pj+1, j+1) = chip;
    5245         196 :   return M;
    5246             : }
    5247             : 
    5248             : /* assume !wt1empty(N), in particular N>25 */
    5249             : /* Returns [[lim,p], mf (weight 2), p*lim x dim matrix] */
    5250             : static GEN
    5251        1799 : mfwt1_pre(long N)
    5252             : {
    5253        1799 :   GEN M, mf = mfinit_Nkchi(N, 2, mfchartrivial(), mf_CUSP, 0);
    5254             :   /*not empty for N>25*/
    5255             :   long p, lim;
    5256        1799 :   if (uisprime(N))
    5257             :   {
    5258         392 :     p = 2; /*N>25 is not 2 */
    5259         392 :     lim = ceilA1(N, 3);
    5260             :   }
    5261             :   else
    5262             :   {
    5263             :     forprime_t S;
    5264        1407 :     u_forprime_init(&S, 2, N);
    5265        1407 :     while ((p = u_forprime_next(&S)))
    5266        2527 :       if (N % p) break;
    5267        1407 :     lim = mfsturm_mf(mf) + 1;
    5268             :   }
    5269             :   /* p = smalllest prime not dividing N */
    5270        1799 :   M = bhnmat_extend_nocache(MF_get_M(mf), N, p*lim-1, 1, MF_get_S(mf));
    5271        1799 :   return mkvec3(mkvecsmall2(lim, p), mf, M);
    5272             : }
    5273             : 
    5274             : /* lg(A) > 1, E a t_POL */
    5275             : static GEN
    5276        1120 : mfmatsermul(GEN A, GEN E)
    5277             : {
    5278        1120 :   long j, l = lg(A), r = nbrows(A);
    5279        1120 :   GEN M = cgetg(l, t_MAT);
    5280        1120 :   E = RgXn_red_shallow(E, r+1);
    5281       12481 :   for (j = 1; j < l; j++)
    5282             :   {
    5283       11361 :     GEN c = RgV_to_RgX(gel(A,j), 0);
    5284       11361 :     gel(M, j) = RgX_to_RgC(RgXn_mul(c, E, r+1), r);
    5285             :   }
    5286        1120 :   return M;
    5287             : }
    5288             : /* lg(Ap) > 1, Ep an Flxn */
    5289             : static GEN
    5290         728 : mfmatsermul_Fl(GEN Ap, GEN Ep, ulong p)
    5291             : {
    5292         728 :   long j, l = lg(Ap), r = nbrows(Ap);
    5293         728 :   GEN M = cgetg(l, t_MAT);
    5294        9590 :   for (j = 1; j < l; j++)
    5295             :   {
    5296        8862 :     GEN c = Flv_to_Flx(gel(Ap,j), 0);
    5297        8862 :     gel(M,j) = Flx_to_Flv(Flxn_mul(c, Ep, r+1, p), r);
    5298             :   }
    5299         728 :   return M;
    5300             : }
    5301             : 
    5302             : /* CHI mod F | N, return mfchar of modulus N.
    5303             :  * FIXME: wasteful, G should be precomputed  */
    5304             : static GEN
    5305       16548 : mfcharinduce(GEN CHI, long N)
    5306             : {
    5307             :   GEN G, chi;
    5308       16548 :   if (mfcharmodulus(CHI) == N) return CHI;
    5309        2940 :   G = znstar0(utoipos(N), 1);
    5310        2940 :   chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    5311        2940 :   CHI = leafcopy(CHI);
    5312        2940 :   gel(CHI,1) = G;
    5313        2940 :   gel(CHI,2) = chi; return CHI;
    5314             : }
    5315             : 
    5316             : static GEN
    5317        3983 : gmfcharno(GEN CHI)
    5318             : {
    5319        3983 :   GEN G = gel(CHI,1), chi = gel(CHI,2);
    5320        3983 :   return mkintmod(znconreyexp(G, chi), znstar_get_N(G));
    5321             : }
    5322             : static long
    5323       12726 : mfcharno(GEN CHI)
    5324             : {
    5325       12726 :   GEN n = znconreyexp(gel(CHI,1), gel(CHI,2));
    5326       12726 :   return itou(n);
    5327             : }
    5328             : 
    5329             : /* return k such that minimal mfcharacter in Galois orbit of CHI is CHI^k */
    5330             : static long
    5331       11347 : mfconreyminimize(GEN CHI)
    5332             : {
    5333       11347 :   GEN G = gel(CHI,1), cyc, chi;
    5334       11347 :   cyc = ZV_to_zv(znstar_get_cyc(G));
    5335       11347 :   chi = ZV_to_zv(znconreychar(G, gel(CHI,2)));
    5336       11347 :   return zv_cyc_minimize(cyc, chi, coprimes_zv(mfcharorder(CHI)));
    5337             : }
    5338             : 
    5339             : /* find scalar c such that first non-0 entry of c*v is 1; return c*v
    5340             :  * (set c = NULL for 1) */
    5341             : static GEN
    5342        1701 : RgV_normalize(GEN v, GEN *pc)
    5343             : {
    5344        1701 :   long i, l = lg(v);
    5345        1701 :   *pc = NULL;
    5346        3948 :   for (i = 1; i < l; i++)
    5347             :   {
    5348        3948 :     GEN c = gel(v,i);
    5349        3948 :     if (!gequal0(c))
    5350             :     {
    5351        1701 :       if (gequal1(c)) { *pc = gen_1; return v; }
    5352         595 :       *pc = ginv(c); return RgV_Rg_mul(v, *pc);
    5353             :     }
    5354             :   }
    5355           0 :   return v;
    5356             : }
    5357             : static GEN
    5358        2282 : mftreatdihedral(GEN DIH, GEN POLCYC, long ordchi, long biglim, GEN *pS)
    5359             : {
    5360             :   GEN M, Minv, C;
    5361             :   long l, i;
    5362        2282 :   l = lg(DIH); if (l == 1) return NULL;
    5363        2282 :   if (!pS) return DIH;
    5364         728 :   C = cgetg(l, t_VEC);
    5365         728 :   M = cgetg(l, t_MAT);
    5366        2044 :   for (i = 1; i < l; i++)
    5367             :   {
    5368        1316 :     GEN c, v = mfcoefs_i(gel(DIH,i), biglim, 1);
    5369        1316 :     gel(M,i) = RgV_normalize(v, &c);
    5370        1316 :     gel(C,i) = Rg_col_ei(c, l-1, i);
    5371             :   }
    5372         728 :   Minv = gel(mfclean(M,POLCYC,ordchi,0),2);
    5373         728 :   M = RgM_Minv_mul(M, Minv);
    5374         728 :   C = RgM_Minv_mul(C, Minv);
    5375         728 :   *pS = vecmflinear(DIH, C);
    5376         728 :   return M;
    5377             : }
    5378             : 
    5379             : static GEN
    5380         189 : mfstabiter(GEN M, GEN A2, GEN E1inv, long lim, GEN P, long ordchi)
    5381             : {
    5382             :   GEN A, VC, con;
    5383         189 :   E1inv = primitive_part(E1inv, &con);
    5384         189 :   VC = con? ginv(con): gen_1;
    5385         189 :   A = mfmatsermul(A2, E1inv);
    5386             :   while(1)
    5387         105 :   {
    5388         294 :     GEN R = shallowconcat(RgM_mul(M,A), rowslice(A,1,lim));
    5389         294 :     GEN B = QabM_ker(R, P, ordchi);
    5390         294 :     long lA = lg(A), lB = lg(B);
    5391         294 :     if (lB == 1) return NULL;
    5392         294 :     if (lB == lA) return mkvec2(A, VC);
    5393         105 :     B = rowslice(B, 1, lA-1);
    5394         105 :     if (ordchi > 2) B = gmodulo(B, P);
    5395         105 :     A = Q_primitive_part(RgM_mul(A,B), &con);
    5396         105 :     VC = gmul(VC,B); /* first VC is a scalar, then a RgM */
    5397         105 :     if (con) VC = RgM_Rg_div(VC, con);
    5398             :   }
    5399             : }
    5400             : static long
    5401         189 : mfstabitermodp(GEN Mp, GEN Ap, long p, long lim)
    5402             : {
    5403         189 :   GEN VC = NULL;
    5404             :   while (1)
    5405          21 :   {
    5406         210 :     GEN Rp = shallowconcat(Flm_mul(Mp,Ap,p), rowslice(Ap,1,lim));
    5407         210 :     GEN Bp = Flm_ker(Rp, p);
    5408         210 :     long lA = lg(Ap), lB = lg(Bp);
    5409         210 :     if (lB == 1) return 0;
    5410         210 :     if (lB == lA) return lA-1;
    5411          21 :     Bp = rowslice(Bp, 1, lA-1);
    5412          21 :     Ap = Flm_mul(Ap, Bp, p);
    5413          21 :     VC = VC? Flm_mul(VC, Bp, p): Bp;
    5414             :   }
    5415             : }
    5416             : 
    5417             : static GEN
    5418         350 : mfintereis(GEN A, GEN M2, GEN y, GEN den, GEN E2, GEN P, long ordchi)
    5419             : {
    5420         350 :   GEN z, M1 = mfmatsermul(A,E2), M1den = is_pm1(den)? M1: RgM_Rg_mul(M1,den);
    5421         350 :   M2 = RgM_mul(M2, rowpermute(M1, y));
    5422         350 :   z = QabM_ker(RgM_sub(M2,M1den), P, ordchi);
    5423         350 :   if (ordchi > 2) z = gmodulo(z, P);
    5424         350 :   return mkvec2(RgM_mul(A,z), z);
    5425             : }
    5426             : static GEN
    5427         357 : mfintereismodp(GEN A, GEN M2, GEN E2, ulong p)
    5428             : {
    5429         357 :   GEN M1 = mfmatsermul_Fl(A, E2, p), z;
    5430         357 :   long j, lx = lg(A);
    5431         357 :   z = Flm_ker(shallowconcat(M1, M2), p);
    5432         357 :   for (j = lg(z) - 1; j; j--) setlg(z[j], lx);
    5433         357 :   return mkvec2(Flm_mul(A,z,p), z);
    5434             : }
    5435             : 
    5436             : static GEN
    5437         196 : mfcharinv_i(GEN CHI)
    5438             : {
    5439         196 :   GEN G = gel(CHI,1);
    5440         196 :   CHI = leafcopy(CHI); gel(CHI,2) =  zncharconj(G, gel(CHI,2)); return CHI;
    5441             : }
    5442             : 
    5443             : /* upper bound dim S_1(Gamma_0(N),chi) performing the linear algebra mod p */
    5444             : static long
    5445         196 : mfwt1dimmodp(GEN A, GEN ES, GEN M, long ordchi, long dih, long lim)
    5446             : {
    5447             :   GEN Ap, ApF, ES1p, VC;
    5448         196 :   ulong p, r = QabM_init(ordchi, &p);
    5449             : 
    5450         196 :   ApF = Ap = QabM_to_Flm(A, r, p);
    5451         196 :   VC = NULL;
    5452         196 :   ES1p = QabX_to_Flx(gel(ES,1), r, p);
    5453         196 :   if (lg(ES) >= 3)
    5454             :   {
    5455         182 :     GEN M2 = mfmatsermul_Fl(ApF, ES1p, p);
    5456         182 :     pari_sp av = avma;
    5457             :     long i;
    5458         532 :     for (i = 2; i < lg(ES); i++)
    5459             :     {
    5460         357 :       GEN ESip = QabX_to_Flx(gel(ES,i), r, p);
    5461         357 :       GEN C, ApC = mfintereismodp(Ap, M2, ESip, p);
    5462         357 :       Ap = gel(ApC,1);
    5463         357 :       if (lg(Ap)-1 == dih) return dih;
    5464         350 :       C = gel(ApC,2); VC = VC? Flm_mul(VC, C, p): C;
    5465         350 :       gerepileall(av, 2, &Ap,&VC);
    5466             :     }
    5467             :   }
    5468             :   /* intersection of Eisenstein series quotients non empty: use Schaeffer */
    5469         189 :   Ap = mfmatsermul_Fl(Ap, Flxn_inv(ES1p,nbrows(Ap),p), p);
    5470         189 :   return mfstabitermodp(QabM_to_Flm(M,r,p), Ap, p, lim);
    5471             : }
    5472             : 
    5473             : /* Compute the full S_1(\G_0(N),\chi). If pS is NULL, only the dimension
    5474             :  * dim, in the form of a vector having dim components. Otherwise output
    5475             :  * a basis: ptvf contains a pointer to the vector of forms, and the
    5476             :  * program returns the corresponding matrix of Fourier expansions.
    5477             :  * ptdimdih gives the dimension of the subspace generated by dihedral forms;
    5478             :  * TMP is from mfwt1_pre or NULL. */
    5479             : static GEN
    5480       10738 : mfwt1basis(long N, GEN CHI, GEN TMP, GEN *pS, long *ptdimdih)
    5481             : {
    5482             :   GEN ES, mf, A, M, Tp, tmp1, tmp2, den;
    5483             :   GEN S, ESA, VC, C, POLCYC, ES1, ES1INV, DIH, a0, a0i;
    5484             :   long plim, lim, biglim, i, p, dA, dimp, ordchi, dih;
    5485             : 
    5486       10738 :   if (ptdimdih) *ptdimdih = 0;
    5487       10738 :   if (pS) *pS = NULL;
    5488       10738 :   if (wt1empty(N) || mfcharparity(CHI) != -1) return NULL;
    5489       10521 :   ordchi = mfcharorder(CHI);
    5490       10521 :   if (uisprime(N) && ordchi > 4) return NULL;
    5491       10493 :   if (!pS)
    5492             :   {
    5493        7035 :     dih = mfdihedralcuspdim(N, CHI);
    5494        7035 :     DIH = zerovec(dih);
    5495             :   }
    5496             :   else
    5497             :   {
    5498        3458 :     DIH = mfdihedralcusp(N, CHI);
    5499        3458 :     dih = lg(DIH) - 1;
    5500             :   }
    5501       10493 :   POLCYC = (ordchi <= 2)? NULL: mfcharpol(CHI);
    5502       10493 :   if (ptdimdih) *ptdimdih = dih;
    5503       10493 :   biglim = mfsturmNk(N, 2);
    5504       10493 :   if (N <= 600) switch(N)
    5505             :   {
    5506             :     long m;
    5507             :     case 219: case 273: case 283: case 331: case 333: case 344: case 416:
    5508             :     case 438: case 468: case 491: case 504: case 546: case 553: case 563:
    5509             :     case 566: case 581: case 592:
    5510          14 :       break; /* one chi with both exotic and dihedral forms */
    5511             :     default: /* only dihedral forms */
    5512        9429 :       if (!dih) return NULL;
    5513             :       /* fall through */
    5514             :     case 124: case 133: case 148: case 171: case 201: case 209: case 224:
    5515             :     case 229: case 248: case 261: case 266: case 288: case 296: case 301:
    5516             :     case 309: case 325: case 342: case 371: case 372: case 380: case 399:
    5517             :     case 402: case 403: case 404: case 408: case 418: case 432: case 444:
    5518             :     case 448: case 451: case 453: case 458: case 496: case 497: case 513:
    5519             :     case 522: case 527: case 532: case 576: case 579:
    5520             :       /* no chi with both exotic and dihedral; one chi with exotic forms */
    5521        3185 :       if (dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5522         910 :       CHI = mfcharinduce(CHI,N);
    5523         910 :       m = mfcharno(CHI);
    5524         910 :       if (N == 124 && (m != 67 && m != 87)) return NULL;
    5525         784 :       if (N == 133 && (m != 83 && m !=125)) return NULL;
    5526         490 :       if (N == 148 && (m !=105 && m !=117)) return NULL;
    5527         364 :       if (N == 171 && (m != 94 && m !=151)) return NULL;
    5528         364 :       if (N == 201 && (m != 29 && m !=104)) return NULL;
    5529         364 :       if (N == 209 && (m != 87 && m !=197)) return NULL;
    5530         364 :       if (N == 224 && (m != 95 && m !=191)) return NULL;
    5531         364 :       if (N == 229 && (m !=107 && m !=122)) return NULL;
    5532         364 :       if (N == 248 && (m != 87 && m !=191)) return NULL;
    5533         273 :       if (N == 261 && (m != 46 && m !=244)) return NULL;
    5534         273 :       if (N == 266 && (m != 83 && m !=125)) return NULL;
    5535         273 :       if (N == 288 && (m != 31 && m !=223)) return NULL;
    5536         273 :       if (N == 296 && (m !=105 && m !=265)) return NULL;
    5537             :   }
    5538         196 :   if (!TMP) TMP = mfwt1_pre(N);
    5539         196 :   tmp1= gel(TMP,1); lim = tmp1[1]; p = tmp1[2]; plim = p*lim;
    5540         196 :   mf  = gel(TMP,2);
    5541         196 :   A   = gel(TMP,3); /* p*lim x dim matrix */
    5542         196 :   S = MF_get_S(mf);
    5543         196 :   ESA = mfeisensteinbasis(N, 1, mfcharinv_i(CHI));
    5544         196 :   ES = RgM_to_RgXV(mfvectomat(ESA, plim+1, 1), 0);
    5545         196 :   ES1 = gel(ES,1); /* does not vanish at oo */
    5546         196 :   Tp = Tpmat(p, lim, CHI);
    5547         196 :   dimp = mfwt1dimmodp(A, ES, Tp, ordchi, dih, lim);
    5548         196 :   if (!dimp) return NULL;
    5549         196 :   if (dimp == dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5550         189 :   VC = gen_1;
    5551         189 :   if (lg(ES) >= 3)
    5552             :   {
    5553             :     pari_sp btop;
    5554         175 :     long lim2 = (3*lim)/2 + 1;
    5555         175 :     GEN Ash = rowslice(A, 1, lim2), M2 = mfmatsermul(Ash, ES1);
    5556             :     GEN v, y, M2M2I, M2I;
    5557         175 :     M2I = QabM_pseudoinv(M2, POLCYC, ordchi, &v, &den);
    5558         175 :     y = gel(v,1);
    5559         175 :     M2M2I = RgM_mul(M2,M2I);
    5560         175 :     btop = avma;
    5561         525 :     for (i = 2; i < lg(ES); i++)
    5562             :     {
    5563         350 :       GEN APC = mfintereis(Ash, M2M2I, y, den, gel(ES,i), POLCYC,ordchi);
    5564         350 :       Ash = gel(APC,1); if (lg(Ash) == 1) return NULL;
    5565         350 :       VC = gmul(VC, gel(APC,2));
    5566         350 :       if (gc_needed(btop, 1))
    5567             :       {
    5568           6 :         if (DEBUGMEM > 1) pari_warn(warnmem,"mfwt1basis i = %ld", i);
    5569           6 :         gerepileall(btop, 2, &Ash, &VC);
    5570             :       }
    5571             :     }
    5572         175 :     A = RgM_mul(A, vecslice(VC,1, lg(Ash)-1));
    5573             :   }
    5574         189 :   a0 = gel(ES1,2); /* non-zero */
    5575         189 :   if (gequal1(a0)) a0 = a0i = NULL;
    5576             :   else
    5577             :   {
    5578         189 :     a0i = ginv(a0);
    5579         189 :     ES1 = RgX_Rg_mul(RgX_unscale(ES1,a0), a0i);
    5580             :   }
    5581         189 :   ES1INV = RgXn_inv(ES1, plim-1);
    5582         189 :   if (a0) ES1INV = RgX_Rg_mul(RgX_unscale(ES1INV, a0i), a0i);
    5583         189 :   tmp2 = mfstabiter(Tp, A, ES1INV, lim, POLCYC, ordchi);
    5584         189 :   if (!tmp2) return NULL;
    5585         189 :   A = gel(tmp2,1); dA = lg(A);
    5586         189 :   VC = gmul(VC, gel(tmp2,2));
    5587         189 :   C = cgetg(dA, t_VEC);
    5588         189 :   M = cgetg(dA, t_MAT);
    5589         574 :   for (i = 1; i < dA; i++)
    5590             :   {
    5591         385 :     GEN c, v = gel(A,i);
    5592         385 :     gel(M,i) = RgV_normalize(v, &c);
    5593         385 :     gel(C,i) = RgC_Rg_mul(gel(VC,i), c);
    5594             :   }
    5595         189 :   if (pS)
    5596             :   {
    5597         140 :     GEN Minv = gel(mfclean(M, POLCYC, ordchi, 0), 2);
    5598         140 :     M = RgM_Minv_mul(M, Minv);
    5599         140 :     C = RgM_Minv_mul(C, Minv);
    5600         140 :     *pS = vecmflineardiv0(S, C, gel(ESA,1));
    5601             :   }
    5602         189 :   return M;
    5603             : }
    5604             : 
    5605             : static void
    5606         322 : MF_set_space(GEN mf, long x) { gmael(mf,1,4) = utoi(x); }
    5607             : static GEN
    5608         168 : mfwt1_cusptonew(GEN mf)
    5609             : {
    5610         168 :   const long vy = 1;
    5611         168 :   GEN vP, F, S, Snew, vF, v = split(mf);
    5612             :   long i, lP, dSnew, ct;
    5613             : 
    5614         168 :   F = gel(v,1);
    5615         168 :   vP= gel(v,2); lP = lg(vP);
    5616         168 :   if (lP == 1) { obj_insert(mf, MF_SPLIT, v); return NULL; }
    5617         154 :   MF_set_space(mf, mf_NEW);
    5618         154 :   S = MF_get_S(mf);
    5619         154 :   dSnew = dim_sum(v);
    5620         154 :   Snew = cgetg(dSnew + 1, t_VEC); ct = 0;
    5621         154 :   vF = cgetg(lP, t_MAT);
    5622         329 :   for (i = 1; i < lP; i++)
    5623             :   {
    5624         175 :     GEN V, P = gel(vP,i), f = liftpol_shallow(gel(F,i));
    5625         175 :     long j, d = degpol(P);
    5626         175 :     gel(vF,i) = V = zerocol(dSnew);
    5627         175 :     if (d == 1)
    5628             :     {
    5629          84 :       gel(Snew, ct+1) = mflineardiv_linear(S, f, 0);
    5630          84 :       gel(V, ct+1) = gen_1;
    5631             :     }
    5632             :     else
    5633             :     {
    5634          91 :       f = RgXV_to_RgM(f,d);
    5635         280 :       for (j = 1; j <= d; j++)
    5636             :       {
    5637         189 :         gel(Snew, ct+j) = mflineardiv_linear(S, row(f,j), 0);
    5638         189 :         gel(V, ct+j) = mkpolmod(pol_xn(j-1,vy), P);
    5639             :       }
    5640             :     }
    5641         175 :     ct += d;
    5642             :   }
    5643         154 :   obj_insert(mf, MF_SPLIT, mkvec2(vF, vP));
    5644         154 :   gel(mf,3) = Snew; return mf;
    5645             : }
    5646             : static GEN
    5647        3542 : mfwt1init(long N, GEN CHI, GEN TMP, long space, long flraw)
    5648             : {
    5649        3542 :   GEN mf, mf1, S, M = mfwt1basis(N, CHI, TMP, &S, NULL);
    5650        3542 :   if (!M) return NULL;
    5651         868 :   mf1 = mkvec4(stoi(N), gen_1, CHI, utoi(mf_CUSP));
    5652         868 :   mf = mkmf(mf1, cgetg(1,t_VEC), S, gen_0, NULL);
    5653         868 :   if (space == mf_NEW)
    5654             :   {
    5655         168 :     gel(mf,5) = mfcleanCHI(M,CHI, 0);
    5656         168 :     mf = mfwt1_cusptonew(mf); if (!mf) return NULL;
    5657         154 :     if (!flraw) M = mfcoefs_mf(mf, mfsturmNk(N,1)+1, 1);
    5658             :   }
    5659         854 :   gel(mf,5) = flraw? zerovec(3): mfcleanCHI(M, CHI, 0);
    5660         854 :   return mf;
    5661             : }
    5662             : 
    5663             : static GEN
    5664         931 : mfEMPTY(GEN mf1)
    5665             : {
    5666         931 :   GEN Minv = mkMinv(cgetg(1,t_MAT), NULL,NULL,NULL);
    5667         931 :   GEN M = mkvec3(cgetg(1,t_VECSMALL), Minv, cgetg(1,t_MAT));
    5668         931 :   return mkmf(mf1, cgetg(1,t_VEC), cgetg(1,t_VEC), cgetg(1,t_VEC), M);
    5669             : }
    5670             : static GEN
    5671         616 : mfEMPTYall(long N, GEN gk, GEN vCHI, long space)
    5672             : {
    5673             :   long i, l;
    5674             :   GEN v, gN, gs;
    5675         616 :   if (!vCHI) return cgetg(1, t_VEC);
    5676          14 :   gN = utoipos(N); gs = utoi(space);
    5677          14 :   l = lg(vCHI); v = cgetg(l, t_VEC);
    5678          14 :   for (i = 1; i < l; i++) gel(v,i) = mfEMPTY(mkvec4(gN,gk,gel(vCHI,i),gs));
    5679          14 :   return v;
    5680             : }
    5681             : 
    5682             : static GEN
    5683        3983 : fmt_dim(GEN CHI, long d, long dih)
    5684        3983 : { return mkvec4(gmfcharorder(CHI), gmfcharno(CHI), utoi(d), stoi(dih)); }
    5685             : /* merge two vector of fmt_dim's for the same vector of characters. If CHI
    5686             :  * is not NULL, remove dim-0 spaces and add character from CHI */
    5687             : static GEN
    5688           7 : merge_dims(GEN V, GEN W, GEN CHI)
    5689             : {
    5690           7 :   long i, j, id, l = lg(V);
    5691           7 :   GEN A = cgetg(l, t_VEC);
    5692           7 :   if (l == 1) return A;
    5693           7 :   id = CHI? 1: 3;
    5694          21 :   for (i = j = 1; i < l; i++)
    5695             :   {
    5696          14 :     GEN v = gel(V,i), w = gel(W,i);
    5697          14 :     long dv = itou(gel(v,id)), dvh = itou(gel(v,id+1)), d;
    5698          14 :     long dw = itou(gel(w,id)), dwh = itou(gel(w,id+1));
    5699          14 :     d = dv + dw;
    5700          14 :     if (d || CHI)
    5701          42 :       gel(A,j++) = CHI? fmt_dim(gel(CHI,i),d, dvh+dwh)
    5702          28 :                       : mkvec2s(d,dvh+dwh);
    5703             :   }
    5704           7 :   setlg(A, j); return A;
    5705             : }
    5706             : static GEN
    5707        3010 : mfdim0all(GEN w)
    5708             : {
    5709        3010 :   if (w) retconst_vec(lg(w)-1, zerovec(2));
    5710        3003 :   return cgetg(1,t_VEC);
    5711             : }
    5712             : static long
    5713        7196 : mfwt1cuspdim_i(long N, GEN CHI, GEN TMP, long *dih)
    5714             : {
    5715        7196 :   pari_sp av = avma;
    5716        7196 :   GEN b = mfwt1basis(N, CHI, TMP, NULL, dih);
    5717        7196 :   return gc_long(av, b? lg(b)-1: 0);
    5718             : }
    5719             : static long
    5720         357 : mfwt1cuspdim(long N, GEN CHI) { return mfwt1cuspdim_i(N, CHI, NULL, NULL); }
    5721             : static GEN
    5722        4144 : mfwt1cuspdimall(long N, GEN vCHI)
    5723             : {
    5724             :   GEN z, TMP, w;
    5725             :   long i, j, l;
    5726        4144 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5727        1141 :   w = mfwt1chars(N,vCHI);
    5728        1141 :   l = lg(w); if (l == 1) return cgetg(1,t_VEC);
    5729        1141 :   z = cgetg(l, t_VEC);
    5730        1141 :   TMP = mfwt1_pre(N);
    5731        7861 :   for (i = j = 1; i < l; i++)
    5732             :   {
    5733        6720 :     GEN CHI = gel(w,i);
    5734        6720 :     long dih, d = mfwt1cuspdim_i(N, CHI, TMP, &dih);
    5735        6720 :     if (vCHI)
    5736          42 :       gel(z,j++) = mkvec2s(d, dih);
    5737        6678 :     else if (d)
    5738        1428 :       gel(z,j++) = fmt_dim(CHI, d, dih);
    5739             :   }
    5740        1141 :   setlg(z,j); return z;
    5741             : }
    5742             : 
    5743             : /* dimension of S_1(Gamma_1(N)) */
    5744             : static long
    5745        4123 : mfwt1cuspdimsum(long N)
    5746             : {
    5747        4123 :   pari_sp av = avma;
    5748        4123 :   GEN v = mfwt1cuspdimall(N, NULL);
    5749        4123 :   long i, ct = 0, l = lg(v);
    5750        5544 :   for (i = 1; i < l; i++)
    5751             :   {
    5752        1421 :     GEN w = gel(v,i); /* [ord(CHI),*,dim,*] */
    5753        1421 :     ct += itou(gel(w,3))*myeulerphiu(itou(gel(w,1)));
    5754             :   }
    5755        4123 :   return gc_long(av,ct);
    5756             : }
    5757             : 
    5758             : static GEN
    5759          56 : mfwt1newdimall(long N, GEN vCHI)
    5760             : {
    5761             :   GEN z, w, vTMP, fa, P, E;
    5762             :   long i, c, l, lw, P1;
    5763          56 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5764          56 :   w = mfwt1chars(N,vCHI);
    5765          56 :   lw = lg(w); if (lw == 1) return cgetg(1,t_VEC);
    5766          56 :   vTMP = const_vec(N, NULL);
    5767          56 :   gel(vTMP,N) = mfwt1_pre(N);
    5768             :   /* if p || N and p \nmid F(CHI), S_1^new(G0(N),chi) = 0 */
    5769          56 :   fa = znstar_get_faN(gmael(w,1,1));
    5770          56 :   P = gel(fa,1); l = lg(P);
    5771          56 :   E = gel(fa,2);
    5772         154 :   for (i = P1 = 1; i < l; i++)
    5773          98 :     if (E[i] == 1) P1 *= itou(gel(P,i));
    5774             :   /* P1 = \prod_{v_p(N) = 1} p */
    5775          56 :   z = cgetg(lw, t_VEC);
    5776         182 :   for (i = c = 1; i < lw; i++)
    5777             :   {
    5778             :     long S, j, l, F, dihnew;
    5779         126 :     GEN D, CHI = gel(w,i), CHIP = mfchartoprimitive(CHI,&F);
    5780             : 
    5781         126 :     S = F % P1? 0: mfwt1cuspdim_i(N, CHI, gel(vTMP,N), &dihnew);
    5782         126 :     if (!S)
    5783             :     {
    5784          56 :       if (vCHI) gel(z, c++) = zerovec(2);
    5785          56 :       continue;
    5786             :     }
    5787          70 :     D = mydivisorsu(N/F); l = lg(D);
    5788          77 :     for (j = l-2; j > 0; j--) /* skip last M = N */
    5789             :     {
    5790           7 :       long M = D[j]*F, m, s, dih;
    5791           7 :       GEN TMP = gel(vTMP,M);
    5792           7 :       if (wt1empty(M) || !(m = mubeta(D[l-j]))) continue; /*m = mubeta(N/M)*/
    5793           7 :       if (!TMP) gel(vTMP,M) = TMP = mfwt1_pre(M);
    5794           7 :       s = mfwt1cuspdim_i(M, CHIP, TMP, &dih);
    5795           7 :       if (s) { S += m * s; dihnew += m * dih; }
    5796             :     }
    5797          70 :     if (vCHI)
    5798          63 :       gel(z,c++) = mkvec2s(S, dihnew);
    5799           7 :     else if (S)
    5800           7 :       gel(z, c++) = fmt_dim(CHI, S, dihnew);
    5801             :   }
    5802          56 :   setlg(z,c); return z;
    5803             : }
    5804             : 
    5805             : static GEN
    5806          28 : mfwt1olddimall(long N, GEN vCHI)
    5807             : {
    5808             :   long i, j, l;
    5809             :   GEN z, w;
    5810          28 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5811          28 :   w = mfwt1chars(N,vCHI);
    5812          28 :   l = lg(w); z = cgetg(l, t_VEC);
    5813          84 :   for (i = j = 1; i < l; i++)
    5814             :   {
    5815          56 :     GEN CHI = gel(w,i);
    5816          56 :     long d = mfolddim(N, 1, CHI);
    5817          56 :     if (vCHI)
    5818          28 :       gel(z,j++) = mkvec2s(d,d?-1:0);
    5819          28 :     else if (d)
    5820           7 :       gel(z, j++) = fmt_dim(CHI, d, -1);
    5821             :   }
    5822          28 :   setlg(z,j); return z;
    5823             : }
    5824             : 
    5825             : static long
    5826         469 : mfwt1olddimsum(long N)
    5827             : {
    5828             :   GEN D;
    5829         469 :   long N2, i, l, S = 0;
    5830         469 :   newd_params(N, &N2); /* will ensure mubeta != 0 */
    5831         469 :   D = mydivisorsu(N/N2); l = lg(D);
    5832        2485 :   for (i = 2; i < l; i++)
    5833             :   {
    5834        2016 :     long M = D[l-i]*N2, d = mfwt1cuspdimsum(M);
    5835        2016 :     if (d) S -= mubeta(D[i]) * d;
    5836             :   }
    5837         469 :   return S;
    5838             : }
    5839             : static long
    5840        1050 : mfwt1newdimsum(long N)
    5841             : {
    5842        1050 :   long S = mfwt1cuspdimsum(N);
    5843        1050 :   return S? S - mfwt1olddimsum(N): 0;
    5844             : }
    5845             : 
    5846             : static long
    5847         210 : mfisdihedral(GEN vF, GEN DIH)
    5848             : {
    5849         210 :   GEN vG = gel(DIH,1), M = gel(DIH,2), v, G, bnr, w, gen, cyc, D, f, nf, con;
    5850             :   GEN f0, f0b, xin;
    5851             :   long i, l, e, j, L, n;
    5852         210 :   if (lg(M) == 1) return 0;
    5853          28 :   v = RgM_RgC_invimage(M, vF);
    5854          28 :   if (!v) return 0;
    5855          28 :   l = lg(v);
    5856          28 :   for (i = 1; i < l; i++)
    5857          28 :     if (!gequal0(gel(v,i))) break;
    5858          28 :   if (i == l) return 0;
    5859          28 :   G = gel(vG,i);
    5860          28 :   bnr = gel(G,2); cyc = bnr_get_cyc(bnr); D = gel(cyc,1);
    5861          28 :   w = gel(G,3);
    5862          28 :   f = bnr_get_mod(bnr);
    5863          28 :   nf = bnr_get_nf(bnr);
    5864          28 :   con = gel(galoisconj(nf,gen_1), 2);
    5865          28 :   f0 = gel(f,1); f0b = galoisapply(nf, con, f0);
    5866          28 :   xin = zv_to_ZV(gel(w,2)); /* xi(bnr.gen[i]) = e(xin[i] / D) */
    5867          28 :   if (!gequal(f0,f0b))
    5868             :   { /* finite part of conductor not ambiguous */
    5869          14 :     GEN a = idealmul(nf, f0, idealdivexact(nf, f0b, idealadd(nf, f0, f0b)));
    5870          14 :     GEN bnr0 = bnr;
    5871          14 :     bnr = bnrinit0(bnr_get_bnf(bnr), mkvec2(a, gel(f,2)), 1);
    5872          14 :     xin = RgV_RgM_mul(xin, bnrsurjection(bnr, bnr0));
    5873             :     /* still xi(gen[i]) = e(xin[i] / D), for the new generators */
    5874             :   }
    5875          28 :   gen = bnr_get_gen(bnr); L = lg(gen);
    5876          42 :   for (j = 1, e = itou(D); j < L; j++)
    5877             :   {
    5878          35 :     GEN Ng = idealnorm(nf, gel(gen,j));
    5879          35 :     GEN a = shifti(gel(xin,j), 1); /* xi(g_j^2) = e(a/D) */
    5880          35 :     GEN b = FpV_dotproduct(xin, isprincipalray(bnr,Ng), D);
    5881          35 :     GEN m = Fp_sub(a, b, D); /* xi(g_j/\bar{g_j}) = e(m/D) */
    5882          35 :     e = ugcd(e, itou(m)); if (e == 1) break;
    5883             :   }
    5884          28 :   n = itou(D) / e;
    5885          28 :   return n == 1? 4: 2*n;
    5886             : }
    5887             : 
    5888             : static ulong
    5889         119 : radical_u(ulong n)
    5890         119 : { return zv_prod(gel(myfactoru(n),1)); }
    5891             : 
    5892             : /* list of fundamental discriminants unramified outside N, with sign s
    5893             :  * [s = 0 => no sign condition] */
    5894             : static GEN
    5895         119 : mfunram(long N, long s)
    5896             : {
    5897         119 :   long cN = radical_u(N >> vals(N)), p = 1, m = 1, l, c, i;
    5898         119 :   GEN D = mydivisorsu(cN), res;
    5899         119 :   l = lg(D);
    5900         119 :   if (s == 1) m = 0; else if (s == -1) p = 0;
    5901         119 :   res = cgetg(6*l - 5, t_VECSMALL);
    5902         119 :   c = 1;
    5903         119 :   if (!odd(N))
    5904             :   { /* d = 1 */
    5905          56 :     if (p) res[c++] = 8;
    5906          56 :     if (m) { res[c++] =-8; res[c++] =-4; }
    5907             :   }
    5908         364 :   for (i = 2; i < l; i++)
    5909             :   { /* skip d = 1, done above */
    5910         245 :     long d = D[i], d4 = d & 3L; /* d odd, squarefree, d4 = 1 or 3 */
    5911         245 :     if (d4 == 1) { if (p) res[c++] = d; }
    5912         182 :     else         { if (m) res[c++] =-d; }
    5913         245 :     if (!odd(N))
    5914             :     {
    5915          56 :       if (p) { res[c++] = 8*d; if (d4 == 3) res[c++] = 4*d; }
    5916          56 :       if (m) { res[c++] =-8*d; if (d4 == 1) res[c++] =-4*d; }
    5917             :     }
    5918             :   }
    5919         119 :   setlg(res, c); return res;
    5920             : }
    5921             : 
    5922             : /* Return 1 if F is definitely not S4 type; return 0 on failure. */
    5923             : static long
    5924         105 : mfisnotS4(long N, GEN w)
    5925             : {
    5926         105 :   GEN D = mfunram(N, 0);
    5927         105 :   long i, lD = lg(D), lw = lg(w);
    5928         616 :   for (i = 1; i < lD; i++)
    5929             :   {
    5930         511 :     long p, d = D[i], ok = 0;
    5931        1442 :     for (p = 2; p < lw; p++)
    5932        1442 :       if (w[p] && kross(d,p) == -1) { ok = 1; break; }
    5933         511 :     if (!ok) return 0;
    5934             :   }
    5935         105 :   return 1;
    5936             : }
    5937             : 
    5938             : /* Return 1 if Q(sqrt(5)) \not\subset Q(F), i.e. F is definitely not A5 type;
    5939             :  * return 0 on failure. */
    5940             : static long
    5941         105 : mfisnotA5(GEN F)
    5942             : {
    5943         105 :   GEN CHI = mf_get_CHI(F), P = mfcharpol(CHI), T, Q;
    5944             : 
    5945         105 :   if (mfcharorder(CHI) % 5 == 0) return 0;
    5946         105 :   T = mf_get_field(F); if (degpol(T) == 1) return 1;
    5947         105 :   if (degpol(P) > 1) T = rnfequation(P,T);
    5948         105 :   Q = gsubgs(pol_xn(2,varn(T)), 5);
    5949         105 :   return (typ(nfisincl(Q, T)) == t_INT);
    5950             : }
    5951             : 
    5952             : /* Given v[p+1]^2 / chi(p) - 2 = z + 1/z with z primitive root of unity of order n,
    5953             :  * return n */
    5954             : static long
    5955        6741 : mffindrootof1(GEN v, long p, GEN CHI)
    5956             : {
    5957        6741 :   GEN ap = gel(v,p+1), u0, u1, u1k, u2;
    5958        6741 :   long c = 1;
    5959        6741 :   if (gequal0(ap)) return 2;
    5960        5033 :   u0 = gen_2; u1k = u1 = gsubgs(gdiv(gsqr(ap), mfchareval_i(CHI, p)), 2);
    5961       19845 :   while (!gequalsg(2, liftpol_shallow(u1))) /* u1 = z^c + z^-c */
    5962             :   {
    5963        9779 :     u2 = gsub(gmul(u1k, u1), u0);
    5964        9779 :     u0 = u1; u1 = u2; c++;
    5965             :   }
    5966        5033 :   return c;
    5967             : }
    5968             : 
    5969             : /* we known that F is not dihedral */
    5970             : static long
    5971         182 : mfgaloistype_i(long N, GEN CHI, GEN F, GEN v)
    5972             : {
    5973             :   forprime_t iter;
    5974         182 :   long lim = lg(v)-2;
    5975         182 :   GEN w = zero_zv(lim);
    5976             :   pari_sp av;
    5977             :   ulong p;
    5978         182 :   u_forprime_init(&iter, 2, lim);
    5979         182 :   av = avma;
    5980        5474 :   while((p = u_forprime_next(&iter))) if (N%p) switch(mffindrootof1(v, p, CHI))
    5981             :   {
    5982        1400 :     case 1: case 2: continue;
    5983        3451 :     case 3: w[p] = 1; break;
    5984          70 :     case 4: return -24; /* S4 */
    5985           0 :     case 5: return -60; /* A5 */
    5986           7 :     default: pari_err_DOMAIN("mfgaloistype", "form", "not a",
    5987             :                              strtoGENstr("cuspidal eigenform"), F);
    5988           0 :     set_avma(av);
    5989             :   }
    5990         105 :   if (mfisnotS4(N,w) && mfisnotA5(F)) return -12; /* A4 */
    5991           0 :   return 0; /* FAILURE */
    5992             : }
    5993             : 
    5994             : static GEN
    5995         210 : mfgaloistype0(long N, GEN CHI, GEN F, GEN DIH, long lim)
    5996             : {
    5997         210 :   pari_sp av = avma;
    5998         210 :   GEN vF = mftocol(F, lim, 1);
    5999         210 :   long t = mfisdihedral(vF, DIH);
    6000         210 :   if (t) { set_avma(av); return stoi(t); }
    6001             :   for(;;)
    6002             :   {
    6003           0 :     t = mfgaloistype_i(N, CHI, F, vF);
    6004         175 :     set_avma(av); if (t) return stoi(t);
    6005           0 :     lim += lim >> 1; vF = mfcoefs_i(F,lim,1);
    6006             :   }
    6007             : }
    6008             : 
    6009             : /* If f is NULL, give all the galoistypes, otherwise just for f */
    6010             : GEN
    6011         217 : mfgaloistype(GEN NK, GEN f)
    6012             : {
    6013         217 :   pari_sp av = avma;
    6014         217 :   GEN CHI, T, F, DIH, mf = checkMF_i(NK);
    6015             :   long N, k, lL, i, lim, SB;
    6016             : 
    6017         217 :   if (f && !checkmf_i(f)) pari_err_TYPE("mfgaloistype", f);
    6018         210 :   if (mf)
    6019             :   {
    6020         175 :     N = MF_get_N(mf);
    6021         175 :     k = MF_get_k(mf);
    6022         175 :     CHI = MF_get_CHI(mf);
    6023             :   }
    6024             :   else
    6025             :   {
    6026          35 :     checkNK(NK, &N, &k, &CHI, 0);
    6027          35 :     mf = f? NULL: mfinit_i(NK, mf_NEW);
    6028             :   }
    6029         210 :   if (k != 1) pari_err_DOMAIN("mfgaloistype", "k", "!=", gen_1, stoi(k));
    6030         210 :   SB = mf? mfsturm_mf(mf): mfsturmNk(N,1);
    6031         210 :   DIH = mfdihedralnew(N,CHI);
    6032         210 :   lim = lg(DIH) == 1? 200: SB;
    6033         210 :   DIH = mkvec2(DIH, mfvectomat(DIH,SB,1));
    6034         210 :   if (f) return gerepileuptoint(av, mfgaloistype0(N,CHI, f, DIH, lim));
    6035         112 :   F = mfeigenbasis(mf); lL = lg(F);
    6036         112 :   T = cgetg(lL, t_VEC);
    6037         112 :   for (i=1; i < lL; i++) gel(T,i) = mfgaloistype0(N, CHI, gel(F,i), DIH, lim);
    6038         112 :   return gerepileupto(av, T);
    6039             : }
    6040             : 
    6041             : /******************************************************************/
    6042             : /*                   Find all dihedral forms.                     */
    6043             : /******************************************************************/
    6044             : /* lim >= 2 */
    6045             : static void
    6046          14 : consttabdihedral(long lim)
    6047          14 : { cache_set(cache_DIH, mfdihedralall(mkvecsmall2(1,lim))); }
    6048             : 
    6049             : /* a ideal coprime to bnr modulus */
    6050             : static long
    6051       77049 : mfdiheval(GEN bnr, GEN w, GEN a)
    6052             : {
    6053       77049 :   GEN L, cycn = gel(w,1), chin = gel(w,2);
    6054       77049 :   long ordmax = cycn[1];
    6055       77049 :   L = ZV_to_Flv(isprincipalray(bnr,a), ordmax);
    6056       77049 :   return Flv_dotproduct(chin, L, ordmax);
    6057             : }
    6058             : 
    6059             : /* A(x^k) mod T */
    6060             : static GEN
    6061       25599 : Galois(GEN A, long k, GEN T)
    6062             : {
    6063       25599 :   if (typ(A) != t_POL) return A;
    6064        9674 :   return gmod(RgX_inflate(A, k), T);
    6065             : }
    6066             : static GEN
    6067         609 : vecGalois(GEN v, long k, GEN T)
    6068             : {
    6069             :   long i, l;
    6070         609 :   GEN w = cgetg_copy(v,&l);
    6071         609 :   for (i = 1; i < l; i++) gel(w,i) = Galois(gel(v,i), k, T);
    6072         609 :   return w;
    6073             : }
    6074             : 
    6075             : static GEN
    6076      153692 : fix_pol(GEN S, GEN Pn, int *trace)
    6077             : {
    6078      153692 :   if (typ(S) != t_POL) return S;
    6079      107310 :   S = RgX_rem(S, Pn);
    6080      107310 :   if (typ(S) == t_POL)
    6081             :   {
    6082      107310 :     switch(lg(S))
    6083             :     {
    6084       37765 :       case 2: return gen_0;
    6085       17080 :       case 3: return gel(S,2);
    6086             :     }
    6087       52465 :     *trace = 1;
    6088             :   }
    6089       52465 :   return S;
    6090             : }
    6091             : 
    6092             : static GEN
    6093       10465 : dihan(GEN bnr, GEN w, GEN k0j, ulong lim)
    6094             : {
    6095       10465 :   GEN nf = bnr_get_nf(bnr), f = bid_get_ideal(bnr_get_bid(bnr));
    6096       10465 :   GEN v = zerovec(lim+1), cycn = gel(w,1), Tinit = gel(w,3);
    6097       10465 :   GEN Pn = gel(Tinit,lg(Tinit)==4? 2: 1);
    6098       10465 :   long j, ordmax = cycn[1], k0 = k0j[1], jdeg = k0j[2];
    6099       10465 :   long D = itos(nf_get_disc(nf)), vt = varn(Pn);
    6100       10465 :   int trace = 0;
    6101             :   ulong p, n;
    6102             :   forprime_t T;
    6103             : 
    6104       10465 :   if (!lim) return v;
    6105       10465 :   gel(v,2) = gen_1;
    6106       10465 :   u_forprime_init(&T, 2, lim);
    6107             :   /* fill in prime powers first */
    6108       10465 :   while ((p = u_forprime_next(&T)))
    6109             :   {
    6110             :     GEN vP, vchiP, S;
    6111             :     long k, lP;
    6112             :     ulong q, qk;
    6113       70616 :     if (kross(D,p) >= 0) q = p;
    6114       29050 :     else if (!(q = umuluu_le(p,p,lim))) continue;
    6115             :     /* q = Norm P */
    6116       47180 :     vP = idealprimedec(nf, utoipos(p));
    6117       47180 :     lP = lg(vP);
    6118       47180 :     vchiP = cgetg(lP, t_VECSMALL);
    6119      128100 :     for (j = k = 1; j < lP; j++)
    6120             :     {
    6121       80920 :       GEN P = gel(vP,j);
    6122       80920 :       if (!idealval(nf, f, P)) vchiP[k++] = mfdiheval(bnr,w,P);
    6123             :     }
    6124       47180 :     if (k == 1) continue;
    6125       45493 :     setlg(vchiP, k); lP = k;
    6126       45493 :     if (lP == 2)
    6127             :     { /* one prime above p not dividing f */
    6128       13937 :       long s, s0 = vchiP[1];
    6129       24668 :       for (qk=q, s = s0;; s = Fl_add(s,s0,ordmax))
    6130             :       {
    6131       35399 :         S = mygmodulo_lift(s, ordmax, gen_1, vt);
    6132       24668 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6133       24668 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6134             :       }
    6135             :     }
    6136             :     else /* two primes above p not dividing f */
    6137             :     {
    6138       31556 :       long s, s0 = vchiP[1], s1 = vchiP[2];
    6139       46662 :       for (qk=q, k = 1;; k++)
    6140       15106 :       { /* sum over a,b s.t. Norm( P1^a P2^b ) = q^k, i.e. a+b = k */
    6141             :         long a;
    6142       46662 :         GEN S = gen_0;
    6143      162701 :         for (a = 0; a <= k; a++)
    6144             :         {
    6145      116039 :           s = Fl_add(Fl_mul(a, s0, ordmax), Fl_mul(k-a, s1, ordmax), ordmax);
    6146      116039 :           S = gadd(S, mygmodulo_lift(s, ordmax, gen_1, vt));
    6147             :         }
    6148       46662 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6149       46662 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6150             :       }
    6151             :     }
    6152             :   }
    6153             :   /* complete with non-prime powers */
    6154      199164 :   for (n = 2; n <= lim; n++)
    6155             :   {
    6156      188699 :     GEN S, fa = myfactoru(n), P = gel(fa, 1), E = gel(fa, 2);
    6157             :     long q;
    6158      188699 :     if (lg(P) == 2) continue;
    6159             :     /* not a prime power */
    6160       82362 :     q = upowuu(P[1],E[1]);
    6161       82362 :     S = gmul(gel(v, q + 1), gel(v, n/q + 1));
    6162       82362 :     gel(v, n+1) = fix_pol(S, Pn, &trace);
    6163             :   }
    6164       10465 :   if (trace)
    6165             :   {
    6166        5355 :     if (lg(Tinit) == 4) v = QabV_tracerel(Tinit, jdeg, v);
    6167             :     /* Apply Galois Mod(k0, ordw) */
    6168        5355 :     if (k0 > 1) { GEN Pm = gel(Tinit,1); v = vecGalois(v, k0, Pm); }
    6169             :   }
    6170       10465 :   return v;
    6171             : }
    6172             : 
    6173             : /* as cyc_normalize for t_VECSMALL cyc */
    6174             : static GEN
    6175       26782 : cyc_normalize_zv(GEN cyc)
    6176             : {
    6177       26782 :   long i, o = cyc[1], l = lg(cyc); /* > 1 */
    6178       26782 :   GEN D = cgetg(l, t_VECSMALL);
    6179       26782 :   D[1] = o; for (i = 2; i < l; i++) D[i] = o / cyc[i];
    6180       26782 :   return D;
    6181             : }
    6182             : /* as char_normalize for t_VECSMALLs */
    6183             : static GEN
    6184      117950 : char_normalize_zv(GEN chi, GEN ncyc)
    6185             : {
    6186      117950 :   long i, l = lg(chi);
    6187      117950 :   GEN c = cgetg(l, t_VECSMALL);
    6188      117950 :   if (l > 1) {
    6189      117950 :     c[1] = chi[1];
    6190      117950 :     for (i = 2; i < l; i++) c[i] = chi[i] * ncyc[i];
    6191             :   }
    6192      117950 :   return c;
    6193             : }
    6194             : 
    6195             : static GEN
    6196        8939 : dihan_bnf(long D)
    6197        8939 : { setrand(gen_1); return Buchall(quadpoly(stoi(D)), 0, LOWDEFAULTPREC); }
    6198             : static GEN
    6199       37226 : dihan_bnr(GEN bnf, GEN A)
    6200       37226 : { setrand(gen_1); return bnrinit0(bnf, A, 1); }
    6201             : 
    6202             : /* Hecke xi * (D/.) = Dirichlet chi, return v in Q^r st chi(g_i) = e(v[i]).
    6203             :  * cycn = cyc_normalize_zv(bnr.cyc), chin = char_normalize_zv(chi,cyc) */
    6204             : static GEN
    6205       34412 : bnrchartwist2conrey(GEN chin, GEN cycn, GEN bnrconreyN, GEN kroconreyN)
    6206             : {
    6207       34412 :   long l = lg(bnrconreyN), c1 = cycn[1], i;
    6208       34412 :   GEN v = cgetg(l, t_COL);
    6209      125132 :   for (i = 1; i < l; i++)
    6210             :   {
    6211       90720 :     GEN d = sstoQ(zv_dotproduct(chin, gel(bnrconreyN,i)), c1);
    6212       90720 :     if (kroconreyN[i] < 0) d = gadd(d, ghalf);
    6213       90720 :     gel(v,i) = d;
    6214             :   }
    6215       34412 :   return v;
    6216             : }
    6217             : 
    6218             : /* chi(g_i) = e(v[i]) denormalize wrt Conrey generators orders */
    6219             : static GEN
    6220       34412 : conreydenormalize(GEN znN, GEN v)
    6221             : {
    6222       34412 :   GEN gcyc = znstar_get_conreycyc(znN), w;
    6223       34412 :   long l = lg(v), i;
    6224       34412 :   w = cgetg(l, t_COL);
    6225      125132 :   for (i = 1; i < l; i++)
    6226       90720 :     gel(w,i) = modii(gmul(gel(v,i), gel(gcyc,i)), gel(gcyc,i));
    6227       34412 :   return w;
    6228             : }
    6229             : 
    6230             : static long
    6231       83538 : Miyake(GEN vchi, GEN gb, GEN cycn)
    6232             : {
    6233       83538 :   long i, e = cycn[1], lb = lg(gb);
    6234       83538 :   GEN v = char_normalize_zv(vchi, cycn);
    6235      124264 :   for (i = 1; i < lb; i++)
    6236       99666 :     if ((zv_dotproduct(v, gel(gb,i)) -  v[i]) % e) return 1;
    6237       24598 :   return 0;
    6238             : }
    6239             : 
    6240             : /* list of Hecke characters not induced by a Dirichlet character up to Galois
    6241             :  * conjugation, whose conductor is bnr.cond; cycn = cyc_normalize(bnr.cyc)*/
    6242             : static GEN
    6243       26782 : mklvchi(GEN bnr, GEN con, GEN cycn)
    6244             : {
    6245       26782 :   GEN gb = NULL, cyc = bnr_get_cyc(bnr), cycsmall = ZV_to_zv(cyc);
    6246       26782 :   GEN vchi = cyc2elts(cycsmall);
    6247       26782 :   long ordmax = cycsmall[1], c, i, l;
    6248       26782 :   if (con)
    6249             :   {
    6250        7784 :     GEN g = bnr_get_gen(bnr), nf = bnr_get_nf(bnr);
    6251        7784 :     long lg = lg(g);
    6252        7784 :     gb = cgetg(lg, t_VEC);
    6253       18270 :     for (i = 1; i < lg; i++)
    6254       10486 :       gel(gb,i) = ZV_to_zv(isprincipalray(bnr, galoisapply(nf, con, gel(g,i))));
    6255             :   }
    6256       26782 :   l = lg(vchi);
    6257      303450 :   for (i = c = 1; i < l; i++)
    6258             :   {
    6259      276668 :     GEN chi = gel(vchi,i);
    6260      276668 :     if (!con || Miyake(chi, gb, cycn)) gel(vchi, c++) = Flv_to_ZV(chi);
    6261             :   }
    6262       26782 :   setlg(vchi, c); l = c;
    6263      278852 :   for (i = 1; i < l; i++)
    6264             :   {
    6265      252070 :     GEN chi = gel(vchi,i);
    6266             :     long n;
    6267      252070 :     if (!chi) continue;
    6268     1054578 :     for (n = 2; n < ordmax; n++)
    6269      965496 :       if (ugcd(n, ordmax) == 1)
    6270             :       {
    6271      397194 :         GEN tmp = vecmodii(gmulsg(n, chi), cyc);
    6272             :         long j;
    6273     7618100 :         for (j = i+1; j < l; j++)
    6274     7220906 :           if (gel(vchi,j) && gequal(gel(vchi,j), tmp)) gel(vchi,j) = NULL;
    6275             :       }
    6276             :   }
    6277      278852 :   for (i = c = 1; i < l; i++)
    6278             :   {
    6279      252070 :     GEN chi = gel(vchi,i);
    6280      252070 :     if (chi && bnrisconductor(bnr, chi)) gel(vchi, c++) = chi;
    6281             :   }
    6282       26782 :   setlg(vchi, c); return vchi;
    6283             : }
    6284             : 
    6285             : /* con = NULL if D > 0 or if D < 0 and id != idcon. */
    6286             : static GEN
    6287       33670 : mfdihedralcommon(GEN bnf, GEN id, GEN znN, GEN kroconreyN, long N, long D, GEN con)
    6288             : {
    6289             :   GEN bnr, bnrconreyN, cyc, cycn, cycN, Lvchi, res, g, P;
    6290             :   long i, j, ordmax, l, lc, deghecke, degrel;
    6291             : 
    6292       33670 :   bnr = dihan_bnr(bnf, id);
    6293       33670 :   cyc = ZV_to_zv( bnr_get_cyc(bnr) );
    6294       33670 :   lc = lg(cyc); if (lc == 1) return NULL;
    6295             : 
    6296       26782 :   g = znstar_get_conreygen(znN); l = lg(g);
    6297       26782 :   bnrconreyN = cgetg(l, t_VEC);
    6298      100576 :   for (i = 1; i < l; i++)
    6299       73794 :     gel(bnrconreyN,i) = ZV_to_zv(isprincipalray(bnr,gel(g,i)));
    6300             : 
    6301       26782 :   cycn = cyc_normalize_zv(cyc);
    6302       26782 :   cycN = ZV_to_zv(znstar_get_cyc(znN));
    6303       26782 :   ordmax = cyc[1];
    6304       26782 :   P = polcyclo(ordmax, fetch_user_var("t"));
    6305       26782 :   deghecke = myeulerphiu(ordmax);
    6306       26782 :   Lvchi = mklvchi(bnr, con, cycn); l = lg(Lvchi);
    6307       26782 :   if (l == 1) return NULL;
    6308       15834 :   res = cgetg(l, t_VEC);
    6309       50246 :   for (j = 1; j < l; j++)
    6310             :   {
    6311       34412 :     GEN T, Tinit, v, vchi = ZV_to_zv(gel(Lvchi,j));
    6312       34412 :     GEN chi, chin = char_normalize_zv(vchi, cycn);
    6313             :     long ordw, vnum, k0;
    6314       34412 :     v = bnrchartwist2conrey(chin, cycn, bnrconreyN, kroconreyN);
    6315       34412 :     ordw = itou(Q_denom(v));
    6316       34412 :     Tinit = Qab_trace_init(P, ordmax, ordw);
    6317       34412 :     chi = conreydenormalize(znN, v);
    6318       34412 :     vnum = itou(znconreyexp(znN, chi));
    6319       34412 :     chi = ZV_to_zv(znconreychar(znN,chi));
    6320       34412 :     degrel = deghecke / myeulerphiu(ordw);
    6321       34412 :     k0 = zv_cyc_minimize(cycN, chi, coprimes_zv(ordw));
    6322       34412 :     vnum = Fl_powu(vnum, k0, N);
    6323             :     /* encodes degrel forms: jdeg = 0..degrel-1 */
    6324       34412 :     T = mkvecsmalln(6, N, k0, vnum, D, ordmax, degrel);
    6325       34412 :     gel(res,j) = mkvec3(T, id, mkvec3(cycn,chin,Tinit));
    6326             :   }
    6327       15834 :   return res;
    6328             : }
    6329             : 
    6330             : /* Append to v all dihedral weight 1 forms coming from D, if fundamental.
    6331             :  * level in [l1, l2] */
    6332             : static void
    6333       18578 : append_dihedral(GEN v, long D, long l1, long l2)
    6334             : {
    6335       18578 :   long Da = labs(D), no, N, i, numi, ct, min, max;
    6336             :   GEN bnf, con, LI, resall, varch;
    6337             :   pari_sp av;
    6338             : 
    6339             :   /* min <= Nf <= max */
    6340       18578 :   max = l2 / Da;
    6341       18578 :   if (l1 == l2)
    6342             :   { /* assume Da | l2 */
    6343           0 :     min = max;
    6344           0 :     if (D > 0 && min < 3) return;
    6345             :   }
    6346             :   else /* assume l1 < l2 */
    6347       18578 :     min = (l1 + Da-1)/Da;
    6348       18578 :   if (!sisfundamental(D)) return;
    6349             : 
    6350        5684 :   av = avma;
    6351        5684 :   bnf = dihan_bnf(D);
    6352        5684 :   con = gel(galoisconj(bnf,gen_1), 2);
    6353        5684 :   LI = ideallist(bnf, max);
    6354        5684 :   numi = 0; for (i = min; i <= max; i++) numi += lg(gel(LI, i)) - 1;
    6355        5684 :   if (D > 0)
    6356             :   {
    6357        1414 :     numi <<= 1;
    6358        1414 :     varch = mkvec2(mkvec2(gen_1,gen_0), mkvec2(gen_0,gen_1));
    6359             :   }
    6360             :   else
    6361        4270 :     varch = NULL;
    6362        5684 :   resall = cgetg(numi+1, t_VEC); ct = 1;
    6363       55006 :   for (no = min; no <= max; no++)
    6364             :   {
    6365             :     GEN LIs, znN, conreyN, kroconreyN;
    6366             :     long flcond, lgc, lglis;
    6367       49322 :     if (D < 0)
    6368       30086 :       flcond = (no == 2 || no == 3 || (no == 4 && (D&7L)==1));
    6369             :     else
    6370       19236 :       flcond = (no == 4 && (D&7L) != 1);
    6371       49322 :     if (flcond) continue;
    6372       44604 :     LIs = gel(LI, no);
    6373       44604 :     N = Da*no;
    6374       44604 :     znN = znstar0(utoi(N), 1);
    6375       44604 :     conreyN = znstar_get_conreygen(znN); lgc = lg(conreyN);
    6376       44604 :     kroconreyN = cgetg(lgc, t_VECSMALL);
    6377       44604 :     for (i = 1; i < lgc; i++) kroconreyN[i] = krosi(D, gel(conreyN, i));
    6378       44604 :     lglis = lg(LIs);
    6379       87752 :     for (i = 1; i < lglis; i++)
    6380             :     {
    6381       43148 :       GEN id = gel(LIs, i), idcon, conk;
    6382             :       long j, inf, maxinf;
    6383       43148 :       if (typ(id) == t_INT) continue;
    6384       28154 :       idcon = galoisapply(bnf, con, id);
    6385       28154 :       conk = (D < 0 && gequal(idcon, id)) ? con : NULL;
    6386       51380 :       for (j = i; j < lglis; j++)
    6387       51380 :         if (gequal(idcon, gel(LIs, j))) { gel(LIs, j) = gen_0; break; }
    6388       28154 :       maxinf = (D < 0 || gequal(idcon,id))? 1: 2;
    6389       61824 :       for (inf = 1; inf <= maxinf; inf++)
    6390             :       {
    6391       33670 :         GEN ide = (D > 0)? mkvec2(id, gel(varch,inf)): id;
    6392       33670 :         GEN res = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, conk);
    6393       33670 :         if (res) gel(resall, ct++) = res;
    6394             :       }
    6395             :     }
    6396             :   }
    6397        5684 :   if (ct == 1) set_avma(av);
    6398             :   else
    6399             :   {
    6400        4788 :     setlg(resall, ct);
    6401        4788 :     vectrunc_append(v, gerepilecopy(av, shallowconcat1(resall)));
    6402             :   }
    6403             : }
    6404             : 
    6405             : static long
    6406       42042 : di_N(GEN a) { return gel(a,1)[1]; }
    6407             : /* All primitive dihedral wt1 forms: LIM a t_VECSMALL with a single component
    6408             :  * (only level LIM) or 2 components [m,M], m < M (between m and M) */
    6409             : static GEN
    6410          14 : mfdihedralall(GEN LIM)
    6411             : {
    6412             :   GEN res, z;
    6413             :   long limD, ct, i, l1, l2;
    6414             : 
    6415          14 :   if (lg(LIM) == 2) l1 = l2 = LIM[1]; else { l1 = LIM[1]; l2 = LIM[2]; }
    6416          14 :   limD = l2;
    6417          14 :   res = vectrunc_init(2*limD);
    6418          14 :   if (l1 == l2)
    6419             :   {
    6420           0 :     GEN D = mydivisorsu(l1);
    6421           0 :     long l = lg(D), j;
    6422           0 :     for (j = 2; j < l; j++)
    6423             :     { /* skip d = 1 */
    6424           0 :       long d = D[j];
    6425           0 :       if (d == 2) continue;
    6426           0 :       append_dihedral(res, -d, l1,l2);
    6427           0 :       if (d >= 5 && D[l-j] >= 3) append_dihedral(res, d, l1,l2); /* Nf >= 3 */
    6428             :     }
    6429             :   }
    6430             :   else
    6431             :   {
    6432             :     long D;
    6433          14 :     for (D = -3; D >= -limD; D--) append_dihedral(res, D, l1,l2);
    6434          14 :     limD /= 3; /* Nf >= 3 (GTM 193, prop 3.3.18) */
    6435          14 :     for (D = 5; D <= limD;   D++) append_dihedral(res, D, l1,l2);
    6436             :   }
    6437          14 :   ct = lg(res);
    6438          14 :   if (ct > 1) res = shallowconcat1(res);
    6439          14 :   if (l1 == l2) return res; /* single level */
    6440          14 :   if (ct > 1)
    6441             :   { /* sort wrt N */
    6442          14 :     res = vecpermute(res, indexvecsort(res, mkvecsmall(1)));
    6443          14 :     ct = lg(res);
    6444             :   }
    6445          14 :   z = const_vec(l2-l1+1, cgetg(1,t_VEC));
    6446        7672 :   for (i = 1; i < ct;)
    6447             :   { /* regroup result sharing the same N */
    6448        7644 :     long n = di_N(gel(res,i)), j = i+1, k;
    6449             :     GEN v;
    6450        7644 :     while (j < ct && di_N(gel(res,j)) == n) j++;
    6451        7644 :     n -= l1-1;
    6452        7644 :     gel(z, n) = v = cgetg(j-i+1, t_VEC);
    6453        7644 :     for (k = 1; i < j; k++,i++) gel(v,k) = gel(res,i);
    6454             :   }
    6455          14 :   return z;
    6456             : }
    6457             : 
    6458             : /* return [vF, index], where vecpermute(vF,index) generates dihedral forms
    6459             :  * for character CHI */
    6460             : static GEN
    6461       23772 : mfdihedralnew_i(long N, GEN CHI)
    6462             : {
    6463             :   GEN bnf, Tinit, Pm, vf, M, V, NK, SP;
    6464             :   long Dold, d, ordw, i, SB, c, l, k0, k1, chino, chinoorig, lv;
    6465             : 
    6466       23772 :   SP = cache_get(cache_DIH, N);
    6467       23772 :   if (!SP) SP = mfdihedralall(mkvecsmall(N));
    6468       23772 :   lv = lg(SP); if (lv == 1) return NULL;
    6469       11347 :   CHI = mfcharinduce(CHI,N);
    6470       11347 :   ordw = mfcharorder(CHI);
    6471       11347 :   chinoorig = mfcharno(CHI);
    6472       11347 :   k0 = mfconreyminimize(CHI);
    6473       11347 :   chino = Fl_powu(chinoorig, k0, N);
    6474       11347 :   k1 = Fl_inv(k0 % ordw, ordw);
    6475       11347 :   V = cgetg(lv, t_VEC);
    6476       11347 :   d = 0;
    6477       35231 :   for (i = l = 1; i < lv; i++)
    6478             :   {
    6479       23884 :     GEN sp = gel(SP,i), T = gel(sp,1);
    6480       23884 :     if (T[3] != chino) continue;
    6481        3556 :     d += T[6];
    6482        3556 :     if (k1 != 1)
    6483             :     {
    6484          77 :       GEN t = leafcopy(T);
    6485          77 :       t[3] = chinoorig;
    6486          77 :       t[2] = (t[2]*k1)%ordw;
    6487          77 :       sp = mkvec4(t, gel(sp,2), gel(sp,3), gel(sp,4));
    6488             :     }
    6489        3556 :     gel(V, l++) = sp;
    6490             :   }
    6491       11347 :   setlg(V, l); /* dihedral forms of level N and character CHI */
    6492       11347 :   if (l == 1) return NULL;
    6493             : 
    6494        2331 :   SB = myeulerphiu(ordw) * mfsturmNk(N,1) + 1;
    6495        2331 :   M = cgetg(d+1, t_MAT);
    6496        2331 :   vf = cgetg(d+1, t_VEC);
    6497        2331 :   NK = mkNK(N, 1, CHI);
    6498        2331 :   bnf = NULL; Dold = 0;
    6499        5887 :   for (i = c = 1; i < l; i++)
    6500             :   { /* T = [N, k0, conreyno, D, ordmax, degrel] */
    6501        3556 :     GEN bnr, Vi = gel(V,i), T = gel(Vi,1), id = gel(Vi,2), w = gel(Vi,3);
    6502        3556 :     long jdeg, k0i = T[2], D = T[4], degrel = T[6];
    6503             : 
    6504        3556 :     if (D != Dold) { Dold = D; bnf = dihan_bnf(D); }
    6505        3556 :     bnr = dihan_bnr(bnf, id);
    6506       10430 :     for (jdeg = 0; jdeg < degrel; jdeg++,c++)
    6507             :     {
    6508        6874 :       GEN k0j = mkvecsmall2(k0i, jdeg), an = dihan(bnr, w, k0j, SB);
    6509        6874 :       settyp(an, t_COL); gel(M,c) = Q_primpart(an);
    6510        6874 :       gel(vf,c) = tag3(t_MF_DIHEDRAL, NK, bnr, w, k0j);
    6511             :     }
    6512             :   }
    6513        2331 :   Tinit = gmael3(V,1,3,3); Pm = gel(Tinit,1);
    6514        2331 :   V = QabM_indexrank(M, degpol(Pm)==1? NULL: Pm, ordw);
    6515        2331 :   return mkvec2(vf,gel(V,2));
    6516             : }
    6517             : static long
    6518       15813 : mfdihedralnewdim(long N, GEN CHI)
    6519             : {
    6520       15813 :   pari_sp av = avma;
    6521       15813 :   GEN S = mfdihedralnew_i(N, CHI);
    6522       15813 :   return gc_long(av, S? lg(gel(S,2))-1: 0);
    6523             : }
    6524             : static GEN
    6525        7959 : mfdihedralnew(long N, GEN CHI)
    6526             : {
    6527        7959 :   pari_sp av = avma;
    6528        7959 :   GEN S = mfdihedralnew_i(N, CHI);
    6529        7959 :   if (!S) { set_avma(av); return cgetg(1, t_VEC); }
    6530         777 :   return vecpermute(gel(S,1), gel(S,2));
    6531             : }
    6532             : 
    6533             : static long
    6534        7035 : mfdihedralcuspdim(long N, GEN CHI)
    6535             : {
    6536        7035 :   pari_sp av = avma;
    6537             :   GEN D, CHIP;
    6538             :   long F, i, lD, dim;
    6539             : 
    6540        7035 :   CHIP = mfchartoprimitive(CHI, &F);
    6541        7035 :   D = mydivisorsu(N/F); lD = lg(D);
    6542        7035 :   dim = mfdihedralnewdim(N, CHI); /* d = 1 */
    6543       15813 :   for (i = 2; i < lD; i++)
    6544             :   {
    6545        8778 :     long d = D[i], M = N/d, a = mfdihedralnewdim(M, CHIP);
    6546        8778 :     if (a) dim += a * mynumdivu(d);
    6547             :   }
    6548        7035 :   return gc_long(av,dim);
    6549             : }
    6550             : 
    6551             : static GEN
    6552        5642 : mfbdall(GEN E, long N)
    6553             : {
    6554        5642 :   GEN v, D = mydivisorsu(N);
    6555        5642 :   long i, j, nD = lg(D) - 1, nE = lg(E) - 1;
    6556        5642 :   v = cgetg(nD*nE + 1, t_VEC);
    6557        7294 :   for (j = 1; j <= nE; j++)
    6558             :   {
    6559        1652 :     GEN Ej = gel(E, j);
    6560        1652 :     for (i = 0; i < nD; i++) gel(v, i*nE + j) = mfbd_i(Ej, D[i+1]);
    6561             :   }
    6562        5642 :   return v;
    6563             : }
    6564             : static GEN
    6565        3458 : mfdihedralcusp(long N, GEN CHI)
    6566             : {
    6567        3458 :   pari_sp av = avma;
    6568             :   GEN D, CHIP, z;
    6569             :   long F, i, lD;
    6570             : 
    6571        3458 :   CHIP = mfchartoprimitive(CHI, &F);
    6572        3458 :   D = mydivisorsu(N/F); lD = lg(D);
    6573        3458 :   z = cgetg(lD, t_VEC);
    6574        3458 :   gel(z,1) = mfdihedralnew(N, CHI);
    6575        7749 :   for (i = 2; i < lD; i++) /* skip 1 */
    6576             :   {
    6577        4291 :     long d = D[i], M = N / d;
    6578        4291 :     GEN LF = mfdihedralnew(M, mfcharinduce(CHIP, M));
    6579        4291 :     gel(z,i) = mfbdall(LF, d);
    6580             :   }
    6581        3458 :   return gerepilecopy(av, shallowconcat1(z));
    6582             : }
    6583             : 
    6584             : /* used to decide between ratlift and comatrix for ZM_inv; ratlift is better
    6585             :  * when N has many divisors */
    6586             : static int
    6587        2394 : abundant(ulong N) { return mynumdivu(N) >= 8; }
    6588             : 
    6589             : /* CHI an mfchar */
    6590             : static int
    6591         294 : cmp_ord(void *E, GEN a, GEN b)
    6592             : {
    6593         294 :   GEN chia = MF_get_CHI(a), chib = MF_get_CHI(b);
    6594         294 :   (void)E; return cmpii(gmfcharorder(chia), gmfcharorder(chib));
    6595             : }
    6596             : /* mfinit structure.
    6597             : -- mf[1] contains [N,k,CHI,space],
    6598             : -- mf[2] contains vector of closures of Eisenstein series, empty if not
    6599             :    full space.
    6600             : -- mf[3] contains vector of closures, so #mf[3] = dimension of cusp/new space.
    6601             : -- mf[4] contains the corresponding indices: either j for T(j)tf if newspace,
    6602             :    or [M,j,d] for B(d)T(j)tf_M if cuspspace or oldspace.
    6603             : -- mf[5] contains the matrix M of first coefficients of basis, never cleaned.
    6604             :  * NK is either [N,k] or [N,k,CHI].
    6605             :  * mfinit does not do the splitting, only the basis generation. */
    6606             : 
    6607             : /* Set flraw to 1 if do not need mf[5]: no mftobasis etc..., only the
    6608             :    expansions of the basis elements are needed. */
    6609             : 
    6610             : static GEN
    6611        4669 : mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw)
    6612             : {
    6613        4669 :   GEN M = NULL, mf = NULL, mf1 = mkvec4(utoi(N), stoi(k), CHI, utoi(space));
    6614        4669 :   long sb = mfsturmNk(N, k);
    6615             :   cachenew_t cache;
    6616        4669 :   if (k < 0 || badchar(N, k, CHI)) return mfEMPTY(mf1);
    6617        4634 :   if (k == 0) /*nothing*/;
    6618        4592 :   else if (k == 1)
    6619             :   {
    6620         336 :     switch (space)
    6621             :     {
    6622             :       case mf_NEW:
    6623             :       case mf_FULL:
    6624         308 :       case mf_CUSP: mf = mfwt1init(N, CHI, NULL, space, flraw); break;
    6625          14 :       case mf_EISEN:break;
    6626           7 :       case mf_OLD: pari_err_IMPL("mfinit in weight 1 for old space");
    6627           7 :       default: pari_err_FLAG("mfinit");
    6628             :     }
    6629             :   }
    6630             :   else /* k >= 2 */
    6631             :   {
    6632        4256 :     long ord = mfcharorder(CHI);
    6633        4256 :     GEN z = NULL, P = (ord <= 2)? NULL: mfcharpol(CHI);
    6634        4256 :     switch(space)
    6635             :     {
    6636             :       case mf_EISEN:
    6637         105 :         break;
    6638             :       case mf_NEW:
    6639        1183 :         mf = mfnewinit(N, k, CHI, &cache, 1);
    6640        1183 :         if (mf && !flraw) { M = MF_get_M(mf); z = MF_get_Mindex(mf); }
    6641        1183 :         break;
    6642             :       case mf_OLD:
    6643             :       case mf_CUSP:
    6644             :       case mf_FULL:
    6645        2961 :         mf = mfinitcusp(N, k, CHI, &cache, space);
    6646        2961 :         if (mf && !flraw)
    6647             :         {
    6648        2135 :           GEN S = MF_get_S(mf);
    6649        2135 :           M = bhnmat_extend(M, sb+1, 1, S, &cache);
    6650        2135 :           if (space != mf_FULL) gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6651             :         }
    6652        2961 :         dbg_cachenew(&cache);
    6653        2961 :         break;
    6654           7 :       default: pari_err_FLAG("mfinit");
    6655             :     }
    6656        4249 :     if (z) gel(mf,5) = mfclean2(M, z, P, ord);
    6657             :   }
    6658        4613 :   if (!mf) mf = mfEMPTY(mf1);
    6659             :   else
    6660             :   {
    6661        3745 :     gel(mf,1) = mf1;
    6662        3745 :     if (flraw) gel(mf,5) = zerovec(3);
    6663             :   }
    6664        4613 :   if (!space_is_cusp(space))
    6665             :   {
    6666         637 :     GEN E = mfeisensteinbasis(N, k, CHI);
    6667         637 :     gel(mf,2) = E;
    6668         637 :     if (!flraw)
    6669             :     {
    6670         427 :       if (M)
    6671         168 :         M = shallowconcat(mfvectomat(E, sb+1, 1), M);
    6672             :       else
    6673         259 :         M = mfcoefs_mf(mf, sb+1, 1);
    6674         427 :       gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6675             :     }
    6676             :   }
    6677        4613 :   return mf;
    6678             : }
    6679             : 
    6680             : /* mfinit for k = nk/dk */
    6681             : static GEN
    6682        2513 : mfinit_Nndkchi(long N, long nk, long dk, GEN CHI, long space, long flraw)
    6683         210 : { return (dk == 2)? mf2init_Nkchi(N, nk >> 1, CHI, space, flraw)
    6684        2723 :                   : mfinit_Nkchi(N, nk, CHI, space, flraw); }
    6685             : static GEN
    6686        3178 : mfinit_i(GEN NK, long space)
    6687             : {
    6688             :   GEN CHI, mf;
    6689             :   long N, k, dk, joker;
    6690        3178 :   if (checkmf_i(NK))
    6691             :   {
    6692         126 :     N = mf_get_N(NK);
    6693         126 :     Qtoss(mf_get_gk(NK), &k, &dk);
    6694         126 :     CHI = mf_get_CHI(NK);
    6695             :   }
    6696        3052 :   else if ((mf = checkMF_i(NK)))
    6697             :   {
    6698          21 :     long s = MF_get_space(mf);
    6699          21 :     if (s == space) return mf;
    6700          21 :     Qtoss(MF_get_gk(mf), &k, &dk);
    6701          21 :     if (dk == 1 && k > 1 && space == mf_NEW && (s == mf_CUSP || s == mf_FULL))
    6702          21 :       return mfinittonew(mf);
    6703           0 :     N = MF_get_N(mf);
    6704           0 :     CHI = MF_get_CHI(mf);
    6705             :   }
    6706             :   else
    6707        3031 :     checkNK2(NK, &N, &k, &dk, &CHI, 1);
    6708        3136 :   joker = !CHI || typ(CHI) == t_COL;
    6709        3136 :   if (joker)
    6710             :   {
    6711        1141 :     GEN mf, vCHI = CHI;
    6712             :     long i, j, l;
    6713        1141 :     if (CHI && lg(CHI) == 1) return cgetg(1,t_VEC);
    6714        1134 :     if (k < 0) return mfEMPTYall(N, sstoQ(k,dk), CHI, space);
    6715        1120 :     if (k == 1 && dk == 1 && space != mf_EISEN)
    6716         483 :     {
    6717             :       GEN TMP, gN, gs;
    6718        1085 :       if (space != mf_CUSP && space != mf_NEW)
    6719           0 :         pari_err_IMPL("mfinit([N,1,wildcard], space != cusp or new space)");
    6720        1085 :       if (wt1empty(N)) return mfEMPTYall(N, gen_1, CHI, space);
    6721         483 :       vCHI = mfwt1chars(N,vCHI);
    6722         483 :       l = lg(vCHI); mf = cgetg(l, t_VEC); if (l == 1) return mf;
    6723         483 :       TMP = mfwt1_pre(N); gN = utoipos(N); gs = utoi(space);
    6724        3717 :       for (i = j = 1; i < l; i++)
    6725             :       {
    6726        3234 :         pari_sp av = avma;
    6727        3234 :         GEN c = gel(vCHI,i), z = mfwt1init(N, c, TMP, space, 0);
    6728        3234 :         if (!z) {
    6729        2590 :           set_avma(av);
    6730        2590 :           if (CHI) z = mfEMPTY(mkvec4(gN,gen_1,c,gs));
    6731             :         }
    6732        3234 :         if (z) gel(mf, j++) = z;
    6733             :       }
    6734             :     }
    6735             :     else
    6736             :     {
    6737          35 :       vCHI = mfchars(N,k,dk,vCHI);
    6738          35 :       l = lg(vCHI); mf = cgetg(l, t_VEC);
    6739         119 :       for (i = j = 1; i < l; i++)
    6740             :       {
    6741          84 :         pari_sp av = avma;
    6742          84 :         GEN v = mfinit_Nndkchi(N, k, dk, gel(vCHI,i), space, 0);
    6743          84 :         if (MF_get_dim(v) || CHI) gel(mf, j++) = v; else set_avma(av);
    6744             :       }
    6745             :     }
    6746         518 :     setlg(mf,j);
    6747         518 :     if (!CHI) gen_sort_inplace(mf, NULL, &cmp_ord, NULL);
    6748         518 :     return mf;
    6749             :   }
    6750        1995 :   return mfinit_Nndkchi(N, k, dk, CHI, space, 0);
    6751             : }
    6752             : GEN
    6753        2226 : mfinit(GEN NK, long space)
    6754             : {
    6755        2226 :   pari_sp av = avma;
    6756        2226 :   return gerepilecopy(av, mfinit_i(NK, space));
    6757             : }
    6758             : 
    6759             : /* UTILITY FUNCTIONS */
    6760             : static void
    6761         357 : cusp_canon(GEN cusp, long N, long *pA, long *pC)
    6762             : {
    6763         357 :   pari_sp av = avma;
    6764             :   long A, C, tc, cg;
    6765         357 :   if (N <= 0) pari_err_DOMAIN("mfcuspwidth","N","<=",gen_0,stoi(N));
    6766         350 :   if (!cusp || (tc = typ(cusp)) == t_INFINITY) { *pA = 1; *pC = N; return; }
    6767         343 :   if (tc != t_INT && tc != t_FRAC) pari_err_TYPE("checkcusp", cusp);
    6768         343 :   Qtoss(cusp, &A,&C);
    6769         343 :   if (N % C)
    6770             :   {
    6771             :     ulong uC;
    6772          14 :     long u = Fl_invgen((C-1)%N + 1, N, &uC);
    6773          14 :     A = Fl_mul(A, u, N);
    6774          14 :     C = (long)uC;
    6775             :   }
    6776         343 :   cg = ugcd(C, N/C);
    6777         343 :   while (ugcd(A, N) > 1) A += cg;
    6778         343 :   *pA = A % N; *pC = C; set_avma(av);
    6779             : }
    6780             : static long
    6781         812 : mfcuspcanon_width(long N, long C)
    6782         812 : { return (!C || C == N)? 1 : N / ugcd(N, Fl_sqr(umodsu(C,N),N)); }
    6783             : /* v = [a,c] a ZC, width of cusp (a:c) */
    6784             : static long
    6785        7427 : mfZC_width(long N, GEN v)
    6786             : {
    6787        7427 :   ulong C = umodiu(gel(v,2), N);
    6788        7427 :   return (C == 0)? 1: N / ugcd(N, Fl_sqr(C,N));
    6789             : }
    6790             : long
    6791         161 : mfcuspwidth(GEN gN, GEN cusp)
    6792             : {
    6793         161 :   long N = 0, A, C;
    6794             :   GEN mf;
    6795         161 :   if (typ(gN) == t_INT) N = itos(gN);
    6796          42 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    6797           0 :   else pari_err_TYPE("mfcuspwidth", gN);
    6798         161 :   cusp_canon(cusp, N, &A, &C);
    6799         154 :   return mfcuspcanon_width(N, C);
    6800             : }
    6801             : 
    6802             : /* Q a t_INT */
    6803             : static GEN
    6804          14 : findq(GEN al, GEN Q)
    6805             : {
    6806             :   long n;
    6807          14 :   if (typ(al) == t_FRAC && cmpii(gel(al,2), Q) <= 0)
    6808           0 :     return mkvec(mkvec2(gel(al,1), gel(al,2)));
    6809          14 :   n = 1 + (long)ceil(2.0781*gtodouble(glog(Q, LOWDEFAULTPREC)));
    6810          14 :   return contfracpnqn(gboundcf(al,n), n);
    6811             : }
    6812             : static GEN
    6813          91 : findqga(long N, GEN z)
    6814             : {
    6815          91 :   GEN Q, LDC, CK = NULL, DK = NULL, ma, x, y = imag_i(z);
    6816             :   long j, l;
    6817          91 :   if (gcmpgs(gmulsg(2*N, y), 1) >= 0) return NULL;
    6818          14 :   x = real_i(z);
    6819          14 :   Q = ground(ginv(gsqrt(gmulsg(N, y), LOWDEFAULTPREC)));
    6820          14 :   LDC = findq(gmulsg(-N,x), Q);
    6821          14 :   ma = gen_1; l = lg(LDC);
    6822          35 :   for (j = 1; j < l; j++)
    6823             :   {
    6824          21 :     GEN D, DC = gel(LDC,j), C1 = gel(DC,2);
    6825          21 :     if (cmpii(C1,Q) > 0) break;
    6826          21 :     D = gel(DC,1);
    6827          21 :     if (ugcdiu(D,N) == 1)
    6828             :     {
    6829           7 :       GEN C = mului(N, C1), den;
    6830           7 :       den = gadd(gsqr(gmul(C,y)), gsqr(gadd(D, gmul(C,x))));
    6831           7 :       if (gcmp(den, ma) < 0) { ma = den; CK = C; DK = D; }
    6832             :     }
    6833             :   }
    6834          14 :   return DK? mkvec2(CK, DK): NULL;
    6835             : }
    6836             : 
    6837             : static long
    6838          98 : valNC2(GEN P, GEN E, long e)
    6839             : {
    6840          98 :   long i, d = 1, l = lg(P);
    6841         252 :   for (i = 1; i < l; i++)
    6842             :   {
    6843         154 :     long v = u_lval(e, P[i]) << 1;
    6844         154 :     if (v == E[i] + 1) v--;
    6845         154 :     d *= upowuu(P[i], v);
    6846             :   }
    6847          98 :   return d;
    6848             : }
    6849             : 
    6850             : static GEN
    6851          35 : findqganew(long N, GEN z)
    6852             : {
    6853          35 :   GEN MI, DI, x = real_i(z), y = imag_i(z), Ck = gen_0, Dk = gen_1, fa, P, E;
    6854             :   long i;
    6855          35 :   MI = ginv(utoi(N));
    6856          35 :   DI = mydivisorsu(mysqrtu(N));
    6857          35 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    6858         133 :   for (i = 1; i < lg(DI); i++)
    6859             :   {
    6860          98 :     long e = DI[i], g;
    6861             :     GEN U, C, D, m;
    6862          98 :     (void)cxredsl2(gmulsg(e, z), &U);
    6863          98 :     C = gcoeff(U,2,1); if (!signe(C)) continue;
    6864          98 :     D = gcoeff(U,2,2);
    6865          98 :     g = ugcdiu(D,e);
    6866          98 :     if (g > 1) { C = muliu(C,e/g); D = diviuexact(D,g); } else C = muliu(C,e);
    6867          98 :     m = gadd(gsqr(gadd(gmul(C, x), D)), gsqr(gmul(C, y)));
    6868          98 :     m = gdivgs(m, valNC2(P, E, e));
    6869          98 :     if (gcmp(m, MI) < 0) { MI = m; Ck = C; Dk = D; }
    6870             :   }
    6871          35 :   return signe(Ck)? mkvec2(Ck, Dk): NULL;
    6872             : }
    6873             : 
    6874             : /* Return z' and U = [a,b;c,d] \in SL_2(Z), z' = U*z,
    6875             :  * Im(z')/width(U.oo) > sqrt(3)/(2N). Set *pczd = c*z+d */
    6876             : static GEN
    6877         161 : cxredga0N(long N, GEN z, GEN *pU, GEN *pczd, long flag)
    6878             : {
    6879         161 :   GEN v = NULL, A, B, C, D;
    6880             :   long e;
    6881         161 :   if (N == 1) return cxredsl2_i(z, pU, pczd);
    6882         126 :   e = gexpo(gel(z,2));
    6883         126 :   if (e < 0) z = gprec_wensure(z, precision(z) + nbits2extraprec(-e));
    6884         126 :   v = flag? findqganew(N,z): findqga(N,z);
    6885         126 :   if (!v) { *pU = matid(2); *pczd = gen_1; return z; }
    6886          42 :   C = gel(v,1);
    6887          42 :   D = gel(v,2);
    6888          42 :   if (!is_pm1(bezout(C,D, &B,&A))) pari_err_BUG("cxredga0N [gcd > 1]");
    6889          42 :   B = negi(B);
    6890          42 :   *pU = mkmat2(mkcol2(A,C), mkcol2(B,D));
    6891          42 :   *pczd = gadd(gmul(C,z), D);
    6892          42 :   return gdiv(gadd(gmul(A,z), B), *pczd);
    6893             : }
    6894             : 
    6895             : static GEN
    6896         147 : lfunthetaall(GEN b, GEN vL, GEN t, long bitprec)
    6897             : {
    6898         147 :   long i, l = lg(vL);
    6899         147 :   GEN v = cgetg(l, t_VEC);
    6900         322 :   for (i = 1; i < l; i++)
    6901             :   {
    6902         175 :     GEN T, L = gel(vL,i), a0 = gel(L,1), ldata = gel(L,2);
    6903         175 :     GEN van = gel(ldata_get_an(ldata),2);
    6904         175 :     if (lg(van) == 1)
    6905             :     {
    6906           0 :       T = gmul(b, a0);
    6907           0 :       if (isexactzero(T)) { GEN z = real_0_bit(-bitprec); T = mkcomplex(z,z); }
    6908             :     }
    6909             :     else
    6910             :     {
    6911         175 :       T = gmul2n(lfuntheta(ldata, t, 0, bitprec), -1);
    6912         175 :       T = gmul(b, gadd(a0, T));
    6913             :     }
    6914         175 :     gel(v,i) = T;
    6915             :   }
    6916         147 :   return l == 2? gel(v,1): v;
    6917             : }
    6918             : 
    6919             : /* P in ZX */
    6920             : static GEN
    6921         168 : ZX_roots(GEN P, long prec)
    6922             : {
    6923         168 :   long d = degpol(P);
    6924         168 :   if (d == 1) return mkvec(gen_0);
    6925         168 :   if (d == 2 && isint1(gel(P,2)) && isintzero(gel(P,3)) && isint1(gel(P,4)))
    6926           7 :     return mkvec2(powIs(3), gen_I()); /* order as polroots */
    6927         161 :   return (ZX_sturm(P) == d)? realroots(P,NULL,prec): QX_complex_roots(P,prec);
    6928             : }
    6929             : /* initializations for RgX_RgV_eval / RgC_embed */
    6930             : static GEN
    6931         203 : rootspowers(GEN v)
    6932             : {
    6933         203 :   long i, l = lg(v);
    6934         203 :   GEN w = cgetg(l, t_VEC);
    6935         203 :   for (i = 1; i < l; i++) gel(w,i) = gpowers(gel(v,i), l-2);
    6936         203 :   return w;
    6937             : }
    6938             : /* mf embeddings attached to Q(chi)/(T), chi attached to cyclotomic P */
    6939             : static GEN
    6940         819 : getembed(GEN P, GEN T, GEN zcyclo, long prec)
    6941             : {
    6942             :   long i, l;
    6943             :   GEN v;
    6944         819 :   if (degpol(P) == 1) P = NULL; /* mfcharpol for quadratic char */
    6945         819 :   if (degpol(T) == 1) T = NULL; /* dim 1 orbit */
    6946         819 :   if (T && P)
    6947          35 :   { /* K(y) / (T(y)), K = Q(t)/(P) cyclotomic */
    6948          35 :     GEN vr = RgX_is_ZX(T)? ZX_roots(T,prec): roots(RgX_embed1(T,zcyclo), prec);
    6949          35 :     v = rootspowers(vr); l = lg(v);
    6950          35 :     for (i = 1; i < l; i++) gel(v,i) = mkcol3(P,zcyclo,gel(v,i));
    6951             :   }
    6952         784 :   else if (T)
    6953             :   { /* Q(y) / (T(y)), T non-cyclotomic */
    6954         168 :     GEN vr = ZX_roots(T, prec);
    6955         168 :     v = rootspowers(vr); l = lg(v);
    6956         168 :     for (i = 1; i < l; i++) gel(v,i) = mkcol2(T, gel(v,i));
    6957             :   }
    6958             :   else /* cyclotomic or rational */
    6959         616 :     v = mkvec(P? mkvec2(P, zcyclo): cgetg(1,t_VEC));
    6960         819 :   return v;
    6961             : }
    6962             : static GEN
    6963         672 : grootsof1_CHI(GEN CHI, long prec)
    6964         672 : { return grootsof1(mfcharorder(CHI), prec); }
    6965             : /* return the [Q(F):Q(chi)] embeddings of F */
    6966             : static GEN
    6967         518 : mfgetembed(GEN F, long prec)
    6968             : {
    6969         518 :   GEN T = mf_get_field(F), CHI = mf_get_CHI(F), P = mfcharpol(CHI);
    6970         518 :   return getembed(P, T, grootsof1_CHI(CHI, prec), prec);
    6971             : }
    6972             : static GEN
    6973           7 : mfchiembed(GEN mf, long prec)
    6974             : {
    6975           7 :   GEN CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    6976           7 :   return getembed(P, pol_x(0), grootsof1_CHI(CHI, prec), prec);
    6977             : }
    6978             : /* mfgetembed for the successive eigenforms in MF_get_newforms */
    6979             : static GEN
    6980         147 : mfeigenembed(GEN mf, long prec)
    6981             : {
    6982         147 :   GEN vP = MF_get_fields(mf), vF = MF_get_newforms(mf);
    6983         147 :   GEN zcyclo, vE, CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    6984         147 :   long i, l = lg(vP);
    6985         147 :   vF = Q_remove_denom(liftpol_shallow(vF), NULL);
    6986         147 :   prec += nbits2extraprec(gexpo(vF));
    6987         147 :   zcyclo = grootsof1_CHI(CHI, prec);
    6988         147 :   vE = cgetg(l, t_VEC);
    6989         147 :   for (i = 1; i < l; i++) gel(vE,i) = getembed(P, gel(vP,i), zcyclo, prec);
    6990         147 :   return vE;
    6991             : }
    6992             : 
    6993             : static int
    6994          28 : checkPv(GEN P, GEN v)
    6995          28 : { return typ(P) == t_POL && typ(v) == t_VEC && lg(v)-1 >= degpol(P); }
    6996             : static int
    6997          28 : checkemb_i(GEN E)
    6998             : {
    6999          28 :   long t = typ(E), l = lg(E);
    7000          28 :   if (t == t_VEC) return l == 1 || (l == 3 && checkPv(gel(E,1), gel(E,2)));
    7001          21 :   if (t != t_COL) return 0;
    7002          21 :   if (l == 3) return checkPv(gel(E,1), gel(E,2));
    7003          21 :   return l == 4 && typ(gel(E,2)) == t_VEC && checkPv(gel(E,1), gel(E,3));
    7004             : }
    7005             : static GEN
    7006          28 : anyembed(GEN v, GEN E)
    7007             : {
    7008          28 :   switch(typ(v))
    7009             :   {
    7010          21 :     case t_VEC: case t_COL: return mfvecembed(E, v);
    7011           7 :     case t_MAT: return mfmatembed(E, v);
    7012             :   }
    7013           0 :   return mfembed(E, v);
    7014             : }
    7015             : GEN
    7016          49 : mfembed0(GEN E, GEN v, long prec)
    7017             : {
    7018          49 :   pari_sp av = avma;
    7019          49 :   GEN mf, vE = NULL;
    7020          49 :   if (checkmf_i(E)) vE = mfgetembed(E, prec);
    7021          35 :   else if ((mf = checkMF_i(E))) vE = mfchiembed(mf, prec);
    7022          49 :   if (vE)
    7023             :   {
    7024          21 :     long i, l = lg(vE);
    7025             :     GEN w;
    7026          21 :     if (!v) return gerepilecopy(av, l == 2? gel(vE,1): vE);
    7027           0 :     w = cgetg(l, t_VEC);
    7028           0 :     for (i = 1; i < l; i++) gel(w,i) = anyembed(v, gel(vE,i));
    7029           0 :     return gerepilecopy(av, l == 2? gel(w,1): w);
    7030             :   }
    7031          28 :   if (!checkemb_i(E) || !v) pari_err_TYPE("mfembed", E);
    7032          28 :   return gerepilecopy(av, anyembed(v,E));
    7033             : }
    7034             : 
    7035             : /* dummy lfun create for theta evaluation */
    7036             : static GEN
    7037         840 : mfthetaancreate(GEN van, GEN N, GEN k)
    7038             : {
    7039         840 :   GEN L = zerovec(6);
    7040         840 :   gel(L,1) = lfuntag(t_LFUN_GENERIC, van);
    7041         840 :   gel(L,3) = mkvec2(gen_0, gen_1);
    7042         840 :   gel(L,4) = k;
    7043         840 :   gel(L,5) = N; return L;
    7044             : }
    7045             : /* destroy van and prepare to evaluate theta(sigma(van)), for all sigma in
    7046             :  * embeddings vector vE */
    7047             : static GEN
    7048         301 : van_embedall(GEN van, GEN vE, GEN gN, GEN gk)
    7049             : {
    7050         301 :   GEN a0 = gel(van,1), vL;
    7051         301 :   long i, lE = lg(vE), l = lg(van);
    7052         301 :   van++; van[0] = evaltyp(t_VEC) | evallg(l-1); /* remove a0 */
    7053         301 :   vL = cgetg(lE, t_VEC);
    7054         805 :   for (i = 1; i < lE; i++)
    7055             :   {
    7056         504 :     GEN E = gel(vE,i), v = mfvecembed(E, van);
    7057         504 :     gel(vL,i) = mkvec2(mfembed(E,a0), mfthetaancreate(v, gN, gk));
    7058             :   }
    7059         301 :   return vL;
    7060             : }
    7061             : 
    7062             : static int
    7063        1015 : cusp_AC(GEN cusp, long *A, long *C)
    7064             : {
    7065        1015 :   switch(typ(cusp))
    7066             :   {
    7067         105 :     case t_INFINITY: *A = 1; *C = 0; break;
    7068         273 :     case t_INT:  *A = itos(cusp); *C = 1; break;
    7069         427 :     case t_FRAC: *A = itos(gel(cusp, 1)); *C = itos(gel(cusp, 2)); break;
    7070             :     case t_REAL: case t_COMPLEX:
    7071         210 :       *A = 0; *C = 0;
    7072         210 :       if (gsigne(imag_i(cusp)) <= 0)
    7073           7 :         pari_err_DOMAIN("mfeval","imag(tau)","<=",gen_0,cusp);
    7074         203 :       return 0;
    7075           0 :     default: pari_err_TYPE("cusp_AC", cusp);
    7076             :   }
    7077         805 :   return 1;
    7078             : }
    7079             : static GEN
    7080         511 : cusp2mat(long A, long C)
    7081             : { long B, D;
    7082         511 :   cbezout(A, C, &D, &B);
    7083         511 :   return mkmat22s(A, -B, C, D);
    7084             : }
    7085             : static GEN
    7086           7 : mkS(void) { return mkmat22s(0,-1,1,0); }
    7087             : 
    7088             : /* if t is a cusp, return F(t), else NULL */
    7089             : static GEN
    7090         343 : evalcusp(GEN mf, GEN F, GEN t, long prec)
    7091             : {
    7092             :   long A, C;
    7093             :   GEN R;
    7094         343 :   if (!cusp_AC(t, &A,&C)) return NULL;
    7095         189 :   if (C % mf_get_N(F) == 0) return gel(mfcoefs_i(F, 0, 1), 1);
    7096         175 :   R = mfgaexpansion(mf, F, cusp2mat(A,C), 0, prec);
    7097         175 :   return gequal0(gel(R,1))? gmael(R,3,1): gen_0;
    7098             : }
    7099             : /* Evaluate an mf closure numerically, i.e., in the usual sense, either for a
    7100             :  * single tau or a vector of tau; for each, return a vector of results
    7101             :  * corresponding to all complex embeddings of F. If flag is non-zero, allow
    7102             :  * replacing F by F | gamma to increase imag(gamma^(-1).tau) [ expensive if
    7103             :  * MF_EISENSPACE not present ] */
    7104             : static GEN
    7105         154 : mfeval_i(GEN mf, GEN F, GEN vtau, long flag, long bitprec)
    7106             : {
    7107             :   GEN L0, vL, vb, sqN, vczd, vTAU, vs, van, vE;
    7108         154 :   long N = mf_get_N(F), N0, ta, lv, i, prec = nbits2prec(bitprec);
    7109         154 :   GEN gN = utoipos(N), gk = mf_get_gk(F), gk1 = gsubgs(gk,1), vgk;
    7110         154 :   long flscal = 0;
    7111             : 
    7112             :   /* gen_0 is ignored, second component assumes Ramanujan-Petersson in
    7113             :    * 1/2-integer weight */
    7114         154 :   vgk = mkvec2(gen_0, mfiscuspidal(mf,F)? gmul2n(gk1,-1): gk1);
    7115         154 :   ta = typ(vtau);
    7116         154 :   if (!is_vec_t(ta)) { flscal = 1; vtau = mkvec(vtau); ta = t_VEC; }
    7117         154 :   lv = lg(vtau);
    7118         154 :   sqN = sqrtr_abs(utor(N, prec));
    7119         154 :   vs = const_vec(lv-1, NULL);
    7120         154 :   vb = const_vec(lv-1, NULL);
    7121         154 :   vL = cgetg(lv, t_VEC);
    7122         154 :   vTAU = cgetg(lv, t_VEC);
    7123         154 :   vczd = cgetg(lv, t_VEC);
    7124         154 :   L0 = mfthetaancreate(NULL, gN, vgk); /* only for thetacost */
    7125         154 :   vE = mfgetembed(F, prec);
    7126         154 :   N0 = 0;
    7127         329 :   for (i = 1; i < lv; i++)
    7128             :   {
    7129         182 :     GEN z = gel(vtau,i), tau, U;
    7130             :     long w, n;
    7131             : 
    7132         182 :     gel(vs,i) = evalcusp(mf, F, z, prec);
    7133         175 :     if (gel(vs,i)) continue;
    7134         147 :     tau = cxredga0N(N, z, &U, &gel(vczd,i), flag);
    7135         147 :     if (!flag) w = 0; else { w = mfZC_width(N, gel(U,1)); tau = gdivgs(tau,w); }
    7136         147 :     gel(vTAU,i) = mulcxmI(gmul(tau, sqN));
    7137         147 :     n = lfunthetacost(L0, real_i(gel(vTAU,i)), 0, bitprec);
    7138         147 :     if (N0 < n) N0 = n;
    7139         147 :     if (flag)
    7140             :     {
    7141          35 :       GEN A, al, v = mfslashexpansion(mf, F, ZM_inv(U,NULL), N0, 0, &A, prec);
    7142          35 :       gel(vL,i) = van_embedall(v, vE, gN, vgk);
    7143          35 :       al = gel(A,1);
    7144          35 :       if (!gequal0(al))
    7145           7 :         gel(vb,i) = gexp(gmul(gmul(gmulsg(w,al),PiI2(prec)), tau), prec);
    7146             :     }
    7147             :   }
    7148         147 :   if (!flag)
    7149             :   {
    7150         112 :     van = mfcoefs_i(F, N0, 1);
    7151         112 :     vL = const_vec(lv-1, van_embedall(van, vE, gN, vgk));
    7152             :   }
    7153         322 :   for (i = 1; i < lv; i++)
    7154             :   {
    7155             :     GEN T;
    7156         175 :     if (gel(vs,i)) continue;
    7157         147 :     T = gpow(gel(vczd,i), gneg(gk), prec);
    7158         147 :     if (flag && gel(vb,i)) T = gmul(T, gel(vb,i));
    7159         147 :     gel(vs,i) = lfunthetaall(T, gel(vL,i), gel(vTAU,i), bitprec);
    7160             :   }
    7161         147 :   return flscal? gel(vs,1): vs;
    7162             : }
    7163             : 
    7164             : static long
    7165        1078 : mfistrivial(GEN F)
    7166             : {
    7167        1078 :   switch(mf_get_type(F))
    7168             :   {
    7169           7 :     case t_MF_CONST: return lg(gel(F,2)) == 1;
    7170         224 :     case t_MF_LINEAR: case t_MF_LINEAR_BHN: return gequal0(gel(F,3));
    7171         847 :     default: return 0;
    7172             :   }
    7173             : }
    7174             : 
    7175             : static long
    7176         896 : mf_same_k(GEN mf, GEN f) { return gequal(MF_get_gk(mf), mf_get_gk(f)); }
    7177             : static long
    7178         854 : mf_same_CHI(GEN mf, GEN f)
    7179             : {
    7180         854 :   GEN F1, F2, chi1, chi2, CHI1 = MF_get_CHI(mf), CHI2 = mf_get_CHI(f);
    7181             :   /* are the primitive chars attached to CHI1 and CHI2 equal ? */
    7182         854 :   F1 = znconreyconductor(gel(CHI1,1), gel(CHI1,2), &chi1);
    7183         854 :   if (typ(F1) == t_VEC) F1 = gel(F1,1);
    7184         854 :   F2 = znconreyconductor(gel(CHI2,1), gel(CHI2,2), &chi2);
    7185         854 :   if (typ(F2) == t_VEC) F2 = gel(F2,1);
    7186         854 :   return equalii(F1,F2) && ZV_equal(chi1,chi2);
    7187             : }
    7188             : /* check k and CHI rigorously, but not coefficients nor N */
    7189             : static long
    7190         189 : mfisinspace_i(GEN mf, GEN F)
    7191             : {
    7192         189 :   return mfistrivial(F) || (mf_same_k(mf,F) && mf_same_CHI(mf,F));
    7193             : }
    7194             : static void
    7195           7 : err_space(GEN F)
    7196           7 : { pari_err_DOMAIN("mftobasis", "form", "does not belong to",
    7197           0 :                   strtoGENstr("space"), F); }
    7198             : 
    7199             : static long
    7200         140 : mfcheapeisen(GEN mf)
    7201             : {
    7202         140 :   long k, L, N = MF_get_N(mf);
    7203             :   GEN P;
    7204         140 :   if (N <= 70) return 1;
    7205          84 :   k = itos(gceil(MF_get_gk(mf)));
    7206          84 :   if (odd(k)) k--;
    7207          84 :   switch (k)
    7208             :   {
    7209           0 :     case 2:  L = 190; break;
    7210          14 :     case 4:  L = 162; break;
    7211             :     case 6:
    7212          70 :     case 8:  L = 88; break;
    7213           0 :     case 10: L = 78; break;
    7214           0 :     default: L = 66; break;
    7215             :   }
    7216          84 :   P = gel(myfactoru(N), 1);
    7217          84 :   return P[lg(P)-1] <= L;
    7218             : }
    7219             : 
    7220             : static GEN
    7221         175 : myimag_i(GEN tau)
    7222             : {
    7223         175 :   long tc = typ(tau);
    7224         175 :   if (tc == t_INFINITY || tc == t_INT || tc == t_FRAC)
    7225          28 :     return gen_1;
    7226         147 :   if (tc == t_VEC)
    7227             :   {
    7228             :     long ltau, i;
    7229           7 :     GEN z = cgetg_copy(tau, &ltau);
    7230           7 :     for (i=1; i<ltau; i++) gel(z,i) = myimag_i(gel(tau,i));
    7231           7 :     return z;
    7232             :   }
    7233         140 :   return imag_i(tau);
    7234             : }
    7235             : 
    7236             : static GEN
    7237         140 : mintau(GEN vtau)
    7238             : {
    7239         140 :   if (!is_vec_t(typ(vtau))) return myimag_i(vtau);
    7240           7 :   return (lg(vtau) == 1)? gen_1: vecmin(myimag_i(vtau));
    7241             : }
    7242             : 
    7243             : /* initialization for mfgaexpansion: what does not depend on cusp */
    7244             : static GEN
    7245         826 : mf_eisendec(GEN mf, GEN F, long prec)
    7246             : {
    7247         826 :   GEN B = liftpol_shallow(mfeisensteindec(mf, F)), v = variables_vecsmall(B);
    7248         826 :   GEN Mvecj = obj_check(mf, MF_EISENSPACE);
    7249         826 :   long l = lg(v), i, ord;
    7250         826 :   if (lg(Mvecj) < 5) Mvecj = gel(Mvecj,1);
    7251         826 :   ord = itou(gel(Mvecj,4));
    7252         882 :   for (i = 1; i < l; i++)
    7253         637 :     if (v[i] != 1)
    7254             :     {
    7255         581 :       long e = gexpo(B);
    7256         581 :       if (e > 0) prec += nbits2prec(e);
    7257         581 :       B = gsubst(B, v[i], rootsof1u_cx(ord, prec)); break;
    7258             :     }
    7259         826 :   return B;
    7260             : }
    7261             : 
    7262             : GEN
    7263         154 : mfeval(GEN mf0, GEN F, GEN vtau, long bitprec)
    7264             : {
    7265         154 :   pari_sp av = avma;
    7266         154 :   long flnew = 1;
    7267         154 :   GEN mf = checkMF_i(mf0);
    7268         154 :   if (!mf) pari_err_TYPE("mfeval", mf0);
    7269         154 :   if (!checkmf_i(F)) pari_err_TYPE("mfeval", F);
    7270         154 :   if (!mfisinspace_i(mf, F)) err_space(F);
    7271         154 :   if (!obj_check(mf, MF_EISENSPACE)) flnew = mfcheapeisen(mf);
    7272         154 :   if (flnew && gcmpgs(gmulsg(2*MF_get_N(mf), mintau(vtau)), 1) >= 0) flnew = 0;
    7273         154 :   return gerepilecopy(av, mfeval_i(mf, F, vtau, flnew, bitprec));
    7274             : }
    7275             : 
    7276             : static long
    7277         182 : val(GEN v, long bit)
    7278             : {
    7279         182 :   long c, l = lg(v);
    7280         399 :   for (c = 1; c < l; c++)
    7281         385 :     if (gexpo(gel(v,c)) > -bit) return c-1;
    7282          14 :   return -1;
    7283             : }
    7284             : GEN
    7285         196 : mfcuspval(GEN mf, GEN F, GEN cusp, long bitprec)
    7286             : {
    7287         196 :   pari_sp av = avma;
    7288         196 :   long lvE, w, N, sb, n, A, C, prec = nbits2prec(bitprec);
    7289             :   GEN ga, gk, vE;
    7290         196 :   mf = checkMF(mf);
    7291         196 :   if (!checkmf_i(F)) pari_err_TYPE("mfcuspval",F);
    7292         196 :   N = MF_get_N(mf);
    7293         196 :   cusp_canon(cusp, N, &A, &C);
    7294         196 :   gk = mf_get_gk(F);
    7295         196 :   if (typ(gk) != t_INT)
    7296             :   {
    7297          42 :     GEN FT = mfmultheta(F), mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7298          42 :     GEN r = mfcuspval(mf2, FT, cusp, bitprec);
    7299          42 :     if ((C & 3L) == 2)
    7300             :     {
    7301          14 :       GEN z = sstoQ(1,4);
    7302          14 :       r = gsub(r, typ(r) == t_VEC? const_vec(lg(r)-1, z): z);
    7303             :     }
    7304          42 :     return gerepileupto(av, r);
    7305             :   }
    7306         154 :   vE = mfgetembed(F, prec);
    7307         154 :   lvE = lg(vE);
    7308         154 :   w = mfcuspcanon_width(N, C);
    7309         154 :   sb = w * mfsturmNk(N, itos(gk));
    7310         154 :   ga = cusp2mat(A,C);
    7311         161 :   for (n = 8;; n = minss(sb, n << 1))
    7312           7 :   {
    7313         161 :     GEN R = mfgaexpansion(mf, F, ga, n, prec), res = liftpol_shallow(gel(R,3));
    7314         161 :     GEN v = cgetg(lvE-1, t_VECSMALL);
    7315         161 :     long j, ok = 1;
    7316         161 :     res = RgC_embedall(res, vE);
    7317         343 :     for (j = 1; j < lvE; j++)
    7318             :     {
    7319         182 :       v[j] = val(gel(res,j), bitprec/2);
    7320         182 :       if (v[j] < 0) ok = 0;
    7321             :     }
    7322         161 :     if (ok)
    7323             :     {
    7324         147 :       res = cgetg(lvE, t_VEC);
    7325         147 :       for (j = 1; j < lvE; j++) gel(res,j) = gadd(gel(R,1), sstoQ(v[j], w));
    7326         147 :       return gerepilecopy(av, lvE==2? gel(res,1): res);
    7327             :     }
    7328          14 :     if (n == sb) return lvE==2? mkoo(): const_vec(lvE-1, mkoo()); /* 0 */
    7329             :   }
    7330             : }
    7331             : 
    7332             : long
    7333         196 : mfiscuspidal(GEN mf, GEN F)
    7334             : {
    7335         196 :   pari_sp av = avma;
    7336             :   GEN mf2;
    7337         196 :   if (space_is_cusp(MF_get_space(mf))) return 1;
    7338          77 :   if (typ(mf_get_gk(F)) == t_INT)
    7339             :   {
    7340          49 :     GEN v = mftobasis(mf,F,0), vE = vecslice(v, 1, lg(MF_get_E(mf))-1);
    7341          49 :     return gc_long(av, gequal0(vE));
    7342             :   }
    7343          28 :   if (!gequal0(mfak_i(F, 0))) return 0;
    7344          14 :   mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7345          14 :   return mfiscuspidal(mf2, mfmultheta(F));
    7346             : }
    7347             : 
    7348             : /* F = vector of newforms in mftobasis format */
    7349             : static GEN
    7350          77 : mffrickeeigen_i(GEN mf, GEN F, GEN vE, long prec)
    7351             : {
    7352          77 :   GEN M, Z, L0, gN = MF_get_gN(mf), gk = MF_get_gk(mf);
    7353          77 :   long N0, i, lM, bit = prec2nbits(prec), k = itou(gk);
    7354          77 :   long LIM = 5; /* Sturm bound is enough */
    7355             : 
    7356          77 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7357             : START:
    7358          77 :   N0 = lfunthetacost(L0, gen_1, LIM, bit);
    7359          77 :   M = mfcoefs_mf(mf, N0, 1);
    7360          77 :   lM = lg(F);
    7361          77 :   Z = cgetg(lM, t_VEC);
    7362         231 :   for (i = 1; i < lM; i++)
    7363             :   { /* expansion of D * F[i] */
    7364         154 :     GEN D, z, van = RgM_RgC_mul(M, Q_remove_denom(gel(F,i), &D));
    7365         154 :     GEN L = van_embedall(van, gel(vE,i), gN, gk);
    7366         154 :     long l = lg(L), j, bit_add = D? expi(D): 0;
    7367         154 :     gel(Z,i) = z = cgetg(l, t_VEC);
    7368         483 :     for (j = 1; j < l; j++)
    7369             :     {
    7370             :       GEN v, C, C0;
    7371             :       long m, e;
    7372         462 :       for (m = 0; m <= LIM; m++)
    7373             :       {
    7374         462 :         v = lfuntheta(gmael(L,j,2), gen_1, m, bit);
    7375         462 :         if (gexpo(v) > bit_add - bit/2) break;
    7376             :       }
    7377         329 :       if (m > LIM) { LIM <<= 1; goto START; }
    7378         329 :       C = mulcxpowIs(gdiv(v,conj_i(v)), 2*m - k);
    7379         329 :       C0 = grndtoi(C, &e); if (e < 5-bit_accuracy(precision(C))) C = C0;
    7380         329 :       gel(z,j) = C;
    7381             :     }
    7382             :   }
    7383          77 :   return Z;
    7384             : }
    7385             : static GEN
    7386          70 : mffrickeeigen(GEN mf, GEN vE, long prec)
    7387             : {
    7388          70 :   GEN D = obj_check(mf, MF_FRICKE);
    7389          70 :   if (D) { long p = gprecision(D); if (!p || p >= prec) return D; }
    7390          63 :   D = mffrickeeigen_i(mf, MF_get_newforms(mf), vE, prec);
    7391          63 :   return obj_insert(mf, MF_FRICKE, D);
    7392             : }
    7393             : 
    7394             : /* integral weight, new space for primitive quadratic character CHIP;
    7395             :  * MF = vector of embedded eigenforms coefs on mfbasis, by orbit.
    7396             :  * Assume N > Q > 1 and (Q,f(CHIP)) = 1 */
    7397             : static GEN
    7398          56 : mfatkineigenquad(GEN mf, GEN CHIP, long Q, GEN MF, long bitprec)
    7399             : {
    7400             :   GEN L0, la2, S, F, vP, tau, wtau, Z, va, vb, den, coe, sqrtQ, sqrtN;
    7401          56 :   GEN M, gN, gk = MF_get_gk(mf);
    7402          56 :   long N0, x, yq, i, j, lF, dim, muQ, prec = nbits2prec(bitprec);
    7403          56 :   long N = MF_get_N(mf), k = itos(gk), NQ = N / Q;
    7404             : 
    7405             :   /* Q coprime to FC */
    7406          56 :   F = MF_get_newforms(mf);
    7407          56 :   vP = MF_get_fields(mf);
    7408          56 :   lF = lg(F);
    7409          56 :   Z = cgetg(lF, t_VEC);
    7410          56 :   S = MF_get_S(mf); dim = lg(S) - 1;
    7411          56 :   muQ = mymoebiusu(Q);
    7412          56 :   if (muQ)
    7413             :   {
    7414          42 :     GEN SQ = cgetg(dim+1,t_VEC), Qk = gpow(stoi(Q), sstoQ(k-2, 2), prec);
    7415          42 :     long i, bit2 = bitprec >> 1;
    7416          42 :     for (j = 1; j <= dim; j++) gel(SQ,j) = mfak_i(gel(S,j), Q);
    7417          84 :     for (i = 1; i < lF; i++)
    7418             :     {
    7419          42 :       GEN S = RgV_dotproduct(gel(F,i), SQ), T = gel(vP,i);
    7420             :       long e;
    7421          42 :       if (degpol(T) > 1 && typ(S) != t_POLMOD) S = gmodulo(S, T);
    7422          42 :       S = grndtoi(gdiv(conjvec(S, prec), Qk), &e);
    7423          42 :       if (e > -bit2) pari_err_PREC("mfatkineigenquad");
    7424          42 :       if (muQ == -1) S = gneg(S);
    7425          42 :       gel(Z,i) = S;
    7426             :     }
    7427          42 :     return Z;
    7428             :   }
    7429          14 :   la2 = mfchareval_i(CHIP, Q); /* 1 or -1 */
    7430          14 :   (void)cbezout(Q, NQ, &x, &yq);
    7431          14 :   sqrtQ = sqrtr_abs(utor(Q,prec));
    7432          14 :   tau = mkcomplex(gadd(sstoQ(-1, NQ), ginv(utoi(1000))),
    7433             :                   divru(sqrtQ, N));
    7434          14 :   den = gaddgs(gmulsg(NQ, tau), 1);
    7435          14 :   wtau = gdiv(gsub(gmulsg(x, tau), sstoQ(yq, Q)), den);
    7436          14 :   coe = gpowgs(gmul(sqrtQ, den), k);
    7437             : 
    7438          14 :   sqrtN = sqrtr_abs(utor(N,prec));
    7439          14 :   tau  = mulcxmI(gmul(tau,  sqrtN));
    7440          14 :   wtau = mulcxmI(gmul(wtau, sqrtN));
    7441          14 :   gN = utoipos(N);
    7442          14 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7443          14 :   N0 = maxss(lfunthetacost(L0,real_i(tau), 0,bitprec),
    7444             :              lfunthetacost(L0,real_i(wtau),0,bitprec));
    7445          14 :   M = mfcoefs_mf(mf, N0, 1);
    7446          14 :   va = cgetg(dim+1, t_VEC);
    7447          14 :   vb = cgetg(dim+1, t_VEC);
    7448         105 :   for (j = 1; j <= dim; j++)
    7449             :   {
    7450          91 :     GEN L, v = vecslice(gel(M,j), 2, N0+1); /* remove a0 */
    7451          91 :     settyp(v, t_VEC); L = mfthetaancreate(v, gN, gk);
    7452          91 :     gel(va,j) = lfuntheta(L, tau,0,bitprec);
    7453          91 :     gel(vb,j) = lfuntheta(L,wtau,0,bitprec);
    7454             :   }
    7455          84 :   for (i = 1; i < lF; i++)
    7456             :   {
    7457          70 :     GEN z, FE = gel(MF,i);
    7458          70 :     long l = lg(FE);
    7459          70 :     z = cgetg(l, t_VEC);
    7460          70 :     for (j = 1; j < l; j++)
    7461             :     {
    7462          70 :       GEN f = gel(FE,j), a = RgV_dotproduct(va,f), b = RgV_dotproduct(vb,f);
    7463          70 :       GEN la = ground( gdiv(b, gmul(a,coe)) );
    7464          70 :       if (!gequal(gsqr(la), la2)) pari_err_PREC("mfatkineigenquad");
    7465          70 :       if (typ(la) == t_INT)
    7466             :       {
    7467          70 :         if (j != 1) pari_err_BUG("mfatkineigenquad");
    7468          70 :         z = const_vec(l-1, la); break;
    7469             :       }
    7470           0 :       gel(z,j) = la;
    7471             :     }
    7472          70 :     gel(Z,i) = z;
    7473             :   }
    7474          14 :   return Z;
    7475             : }
    7476             : 
    7477             : static GEN
    7478          70 : myusqrt(ulong a, long prec)
    7479             : {
    7480          70 :   if (a == 1UL) return gen_1;
    7481          56 :   if (uissquareall(a, &a)) return utoipos(a);
    7482          42 :   return sqrtr_abs(utor(a, prec));
    7483             : }
    7484             : /* Assume mf is a non-trivial new space, rational primitive character CHIP
    7485             :  * and (Q,FC) = 1 */
    7486             : static GEN
    7487          98 : mfatkinmatnewquad(GEN mf, GEN CHIP, long Q, long flag, long PREC)
    7488             : {
    7489          98 :   GEN cM, M, D, MF, den, vE, F = MF_get_newforms(mf);
    7490          98 :   long i, c, e, prec, bitprec, lF = lg(F), N = MF_get_N(mf), k = MF_get_k(mf);
    7491             : 
    7492          98 :   if (Q == 1) return mkvec4(gen_0, matid(MF_get_dim(mf)), gen_1, mf);
    7493          98 :   den = gel(MF_get_Minv(mf), 2);
    7494          98 :   bitprec = expi(den) + 64;
    7495          98 :   if (!flag) bitprec = maxss(bitprec, prec2nbits(PREC));
    7496             : 
    7497             : START:
    7498          98 :   prec = nbits2prec(bitprec);
    7499          98 :   vE = mfeigenembed(mf, prec);
    7500          98 :   M = cgetg(lF, t_VEC);
    7501          98 :   for (i = 1; i < lF; i++) gel(M,i) = RgC_embedall(gel(F,i), gel(vE,i));
    7502          98 :   if (Q != N)
    7503             :   {
    7504          56 :     D = mfatkineigenquad(mf, CHIP, Q, M, bitprec);
    7505          56 :     c = odd(k)? Q: 1;
    7506             :   }
    7507             :   else
    7508             :   {
    7509          42 :     D = mffrickeeigen(mf, vE, DEFAULTPREC);
    7510          42 :     c = mfcharmodulus(CHIP); if (odd(k)) c = -Q/c;
    7511             :   }
    7512          98 :   D = shallowconcat1(D);
    7513          98 :   if (vec_isconst(D)) { MF = diagonal_shallow(D); flag = 0; }
    7514             :   else
    7515             :   {
    7516          63 :     M = shallowconcat1(M);
    7517          63 :     MF = RgM_mul(matmuldiagonal(M,D), ginv(M));
    7518             :   }
    7519          98 :   if (!flag) return mkvec4(gen_0, MF, gen_1, mf);
    7520             : 
    7521          21 :   if (c > 0)
    7522          21 :     cM = myusqrt(c, PREC);
    7523             :   else
    7524             :   {
    7525           0 :     MF = imag_i(MF); c = -c;
    7526           0 :     cM = mkcomplex(gen_0, myusqrt(c,PREC));
    7527             :   }
    7528          21 :   if (c != 1) MF = RgM_Rg_mul(MF, myusqrt(c,prec));
    7529          21 :   MF = grndtoi(RgM_Rg_mul(MF,den), &e);
    7530          21 :   if (e > -32) { bitprec <<= 1; goto START; }
    7531          21 :   MF = RgM_Rg_div(MF, den);
    7532          21 :   if (is_rational_t(typ(cM)) && !isint1(cM))
    7533           0 :   { MF = RgM_Rg_div(MF, cM); cM = gen_1; }
    7534          21 :   return mkvec4(gen_0, MF, cM, mf);
    7535             : }
    7536             : 
    7537             : /* let CHI mod N, Q || N, return \bar{CHI_Q} * CHI_{N/Q} */
    7538             : static GEN
    7539          70 : mfcharAL(GEN CHI, long Q)
    7540             : {
    7541          70 :   GEN G = gel(CHI,1), c = gel(CHI,2), cycc, d, P, E, F;
    7542          70 :   long l = lg(c), N = mfcharmodulus(CHI), i;
    7543          70 :   if (N == Q) return mfcharconj(CHI);
    7544          42 :   if (N == 1) return CHI;
    7545          42 :   CHI = leafcopy(CHI);
    7546          42 :   gel(CHI,2) = d = leafcopy(c);
    7547          42 :   F = znstar_get_faN(G);
    7548          42 :   P = gel(F,1);
    7549          42 :   E = gel(F,2);
    7550          42 :   cycc = znstar_get_conreycyc(G);
    7551          42 :   if (!odd(Q) && equaliu(gel(P,1), 2) && E[1] >= 3)
    7552          14 :     gel(d,2) = Fp_neg(gel(d,2), gel(cycc,2));
    7553          56 :   else for (i = 1; i < l; i++)
    7554          28 :     if (!umodui(Q, gel(P,i))) gel(d,i) = Fp_neg(gel(d,i), gel(cycc,i));
    7555          42 :   return CHI;
    7556             : }
    7557             : static long
    7558         189 : atkin_get_NQ(long N, long Q, const char *f)
    7559             : {
    7560         189 :   long NQ = N / Q;
    7561         189 :   if (N % Q) pari_err_DOMAIN(f,"N % Q","!=",gen_0,utoi(Q));
    7562         189 :   if (ugcd(NQ, Q) > 1) pari_err_DOMAIN(f,"gcd(Q,N/Q)","!=",gen_1,utoi(Q));
    7563         189 :   return NQ;
    7564             : }
    7565             : 
    7566             : /* transform mf to new_NEW if possible */
    7567             : static GEN
    7568        1127 : MF_set_new(GEN mf)
    7569             : {
    7570        1127 :   GEN vMjd, vj, gk = MF_get_gk(mf);
    7571             :   long l, j;
    7572        1127 :   if (MF_get_space(mf) != mf_CUSP
    7573         182 :       || typ(gk) != t_INT || itou(gk) == 1) return mf;
    7574         168 :   vMjd = MFcusp_get_vMjd(mf); l = lg(vMjd);
    7575         168 :   if (l > 1 && gel(vMjd,1)[1] != MF_get_N(mf)) return mf; /* oldspace != 0 */
    7576         168 :   mf = shallowcopy(mf);
    7577         168 :   gel(mf,1) = shallowcopy(gel(mf,1));
    7578         168 :   MF_set_space(mf, mf_NEW);
    7579         168 :   vj = cgetg(l, t_VECSMALL);
    7580         168 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd, j)[2];
    7581         168 :   gel(mf,4) = vj; return mf;
    7582             : }
    7583             : 
    7584             : /* if flag = 1, rationalize, else don't */
    7585             : static GEN
    7586         168 : mfatkininit_i(GEN mf, long Q, long flag, long prec)
    7587             : {
    7588             :   GEN M, B, C, CHI, CHIAL, G, chi, P, z, g, mfB, s, Mindex, Minv;
    7589         168 :   long j, l, lim, ord, FC, NQ, cQ, nk, dk, N = MF_get_N(mf);
    7590             : 
    7591         168 :   B = MF_get_basis(mf); l = lg(B);
    7592         168 :   M = cgetg(l, t_MAT); if (l == 1) return mkvec4(gen_0,M,gen_1,mf);
    7593         168 :   Qtoss(MF_get_gk(mf), &nk,&dk);
    7594         168 :   Q = labs(Q);
    7595         168 :   NQ = atkin_get_NQ(N, Q, "mfatkininit");
    7596         168 :   CHI = MF_get_CHI(mf);
    7597         168 :   CHI = mfchartoprimitive(CHI, &FC);
    7598         168 :   ord = mfcharorder(CHI);
    7599         168 :   mf = MF_set_new(mf);
    7600         168 :   if (MF_get_space(mf) == mf_NEW && ord <= 2 && NQ % FC == 0 && dk == 1)
    7601          98 :     return mfatkinmatnewquad(mf, CHI, Q, flag, prec);
    7602             :   /* now flag != 0 */
    7603          70 :   G   = gel(CHI,1);
    7604          70 :   chi = gel(CHI,2);
    7605          70 :   if (Q == N) { g = mkmat22s(0, -1, N, 0); cQ = NQ; } /* Fricke */
    7606             :   else
    7607             :   {
    7608          28 :     GEN F, gQP = utoi(ugcd(Q, FC));
    7609             :     long t, v;
    7610          28 :     chi = znchardecompose(G, chi, gQP);
    7611          28 :     F = znconreyconductor(G, chi, &chi);
    7612          28 :     G = znstar0(F,1);
    7613          28 :     (void)cbezout(Q, NQ, &t, &v);
    7614          28 :     g = mkmat22s(Q*t, 1, -N*v, Q);
    7615          28 :     cQ = -NQ*v;
    7616             :   }
    7617          70 :   C = s = gen_1;
    7618             :   /* N.B. G,chi are G_Q,chi_Q [primitive] at this point */
    7619          70 :   if (lg(chi) != 1) C = ginv( znchargauss(G, chi, gen_1, prec2nbits(prec)) );
    7620          70 :   if (dk == 1)
    7621          63 :   { if (odd(nk)) s = myusqrt(Q,prec); }
    7622             :   else
    7623             :   {
    7624           7 :     long r = nk >> 1; /* k-1/2 */
    7625           7 :     s = gpow(utoipos(Q), mkfracss(odd(r)? 1: 3, 4), prec);
    7626           7 :     if (odd(cQ))
    7627             :     {
    7628           7 :       long t = r + ((cQ-1) >> 1);
    7629           7 :       s = mkcomplex(s, odd(t)? gneg(s): s);
    7630             :     }
    7631             :   }
    7632          70 :   if (!isint1(s)) C = gmul(C, s);
    7633          70 :   CHIAL = mfcharAL(CHI, Q);
    7634          70 :   if (dk == 2)
    7635           7 :     CHIAL = mfcharmul(CHIAL, induce(gel(CHIAL,1), utoipos(odd(Q) ? Q<<2 : Q)));
    7636          70 :   CHIAL = mfchartoprimitive(CHIAL,NULL);
    7637          70 :   mfB = gequal(CHIAL,CHI)? mf: mfinit_Nndkchi(N,nk,dk,CHIAL,MF_get_space(mf),0);
    7638          70 :   Mindex = MF_get_Mindex(mfB);
    7639          70 :   Minv = MF_get_Minv(mfB);
    7640          70 :   P = z = NULL;
    7641          70 :   if (ord > 2) { P = mfcharpol(CHI); z = rootsof1u_cx(ord, prec); }
    7642          70 :   lim = maxss(mfsturm(mfB), mfsturm(mf)) + 1;
    7643         217 :   for (j = 1; j < l; j++)
    7644             :   {
    7645         147 :     GEN v = mfslashexpansion(mf, gel(B,j), g, lim, 0, NULL, prec+EXTRAPREC);
    7646             :     long junk;
    7647         147 :     if (!isint1(C)) v = RgV_Rg_mul(v, C);
    7648         147 :     v = bestapprnf(v, P, z, prec);
    7649         147 :     v = vecpermute_partial(v, Mindex, &junk);
    7650         147 :     v = Minv_RgC_mul(Minv, v); /* cf mftobasis_i */
    7651         147 :     gel(M, j) = v;
    7652             :   }
    7653          70 :   if (is_rational_t(typ(C)) && !gequal1(C)) { M = gdiv(M, C); C = gen_1; }
    7654          70 :   if (mfB == mf) mfB = gen_0;
    7655          70 :   return mkvec4(mfB, M, C, mf);
    7656             : }
    7657             : GEN
    7658          77 : mfatkininit(GEN mf, long Q, long prec)
    7659             : {
    7660          77 :   pari_sp av = avma;
    7661          77 :   mf = checkMF(mf); return gerepilecopy(av, mfatkininit_i(mf, Q, 1, prec));
    7662             : }
    7663             : static void
    7664          21 : checkmfa(GEN z)
    7665             : {
    7666          21 :   if (typ(z) != t_VEC || lg(z) != 5 || typ(gel(z,2)) != t_MAT
    7667          21 :       || !checkMF_i(gel(z,4))
    7668          21 :       || (!isintzero(gel(z,1)) && !checkMF_i(gel(z,1))))
    7669           0 :     pari_err_TYPE("mfatkin [please apply mfatkininit()]",z);
    7670          21 : }
    7671             : 
    7672             : /* Apply atkin Q to closure F */
    7673             : GEN
    7674          21 : mfatkin(GEN mfa, GEN F)
    7675             : {
    7676          21 :   pari_sp av = avma;
    7677             :   GEN z, mfB, MQ, mf;
    7678          21 :   checkmfa(mfa);
    7679          21 :   mfB= gel(mfa,1);
    7680          21 :   MQ = gel(mfa,2);
    7681          21 :   mf = gel(mfa,4);
    7682          21 :   if (typ(mfB) == t_INT) mfB = mf;
    7683          21 :   z = RgM_RgC_mul(MQ, mftobasis_i(mf,F));
    7684          21 :   return gerepileupto(av, mflinear(mfB, z));
    7685             : }
    7686             : 
    7687             : GEN
    7688          49 : mfatkineigenvalues(GEN mf, long Q, long prec)
    7689             : {
    7690          49 :   pari_sp av = avma;
    7691             :   GEN vF, L, CHI, M, mfatk, C, MQ, vE, mfB;
    7692             :   long N, NQ, l, i;
    7693             : 
    7694          49 :   mf = checkMF(mf); N = MF_get_N(mf);
    7695          49 :   vF = MF_get_newforms(mf); l = lg(vF);
    7696             :   /* N.B. k is integral */
    7697          49 :   if (l == 1) { set_avma(av); return cgetg(1, t_VEC); }
    7698          49 :   L = cgetg(l, t_VEC);
    7699          49 :   if (Q == 1)
    7700             :   {
    7701           7 :     GEN vP = MF_get_fields(mf);
    7702           7 :     for (i = 1; i < l; i++) gel(L,i) = const_vec(degpol(gel(vP,i)), gen_1);
    7703           7 :     return L;
    7704             :   }
    7705          42 :   vE = mfeigenembed(mf,prec);
    7706          42 :   if (Q == N) return gerepileupto(av, mffrickeeigen(mf, vE, prec));
    7707          21 :   Q = labs(Q);
    7708          21 :   NQ = atkin_get_NQ(N, Q, "mfatkineigenvalues"); /* != 1 */
    7709          21 :   mfatk = mfatkininit(mf, Q, prec);
    7710          21 :   mfB= gel(mfatk,1); if (typ(mfB) != t_VEC) mfB = mf;
    7711          21 :   MQ = gel(mfatk,2);
    7712          21 :   C  = gel(mfatk,3);
    7713          21 :   M = row(mfcoefs_mf(mfB,1,1), 2); /* vec of a_1(b_i) for mfbasis functions */
    7714          56 :   for (i = 1; i < l; i++)
    7715             :   {
    7716          35 :     GEN c = RgV_dotproduct(RgM_RgC_mul(MQ,gel(vF,i)), M); /* C * eigen_i */
    7717          35 :     gel(L,i) = Rg_embedall_i(c, gel(vE,i));
    7718             :   }
    7719          21 :   if (!gequal1(C)) L = gdiv(L, C);
    7720          21 :   CHI = MF_get_CHI(mf);
    7721          21 :   if (mfcharorder(CHI) <= 2 && NQ % mfcharconductor(CHI) == 0) L = ground(L);
    7722          21 :   return gerepilecopy(av, L);
    7723             : }
    7724             : 
    7725             : /* expand B_d V, keeping same length */
    7726             : static GEN
    7727        4781 : bdexpand(GEN V, long d)
    7728             : {
    7729             :   GEN W;
    7730             :   long N, n;
    7731        4781 :   if (d == 1) return V;
    7732        1624 :   N = lg(V)-1; W = zerovec(N);
    7733        1624 :   for (n = 0; n <= (N-1)/d; n++) gel(W, n*d+1) = gel(V, n+1);
    7734        1624 :   return W;
    7735             : }
    7736             : /* expand B_d V, increasing length up to lim */
    7737             : static GEN
    7738         266 : bdexpandall(GEN V, long d, long lim)
    7739             : {
    7740             :   GEN W;
    7741             :   long N, n;
    7742         266 :   if (d == 1) return V;
    7743          35 :   N = lg(V)-1; W = zerovec(lim);
    7744          35 :   for (n = 0; n <= N-1 && n*d <= lim; n++) gel(W, n*d+1) = gel(V, n+1);
    7745          35 :   return W;
    7746             : }
    7747             : 
    7748             : static void
    7749        7798 : parse_vecj(GEN T, GEN *E1, GEN *E2)
    7750             : {
    7751        7798 :   if (lg(T)==3) { *E1 = gel(T,1); *E2 = gel(T,2); }
    7752        4165 :   else { *E1 = T; *E2 = NULL; }
    7753        7798 : }
    7754             : 
    7755             : /* g in M_2(Z) ? */
    7756             : static int
    7757        2401 : check_M2Z(GEN g)
    7758        2401 : {  return typ(g) == t_MAT && lg(g) == 3 && lgcols(g) == 3 && RgM_is_ZM(g); }
    7759             : /* g in SL_2(Z) ? */
    7760             : static int
    7761        1470 : check_SL2Z(GEN g) { return check_M2Z(g) && equali1(ZM_det(g)); }
    7762             : 
    7763             : static GEN
    7764        7686 : mfcharcxeval(GEN CHI, long n, long prec)
    7765             : {
    7766        7686 :   ulong ord, N = mfcharmodulus(CHI);
    7767             :   GEN ordg;
    7768        7686 :   if (N == 1) return gen_1;
    7769        3675 :   if (ugcd(N, labs(n)) > 1) return gen_0;
    7770        3675 :   ordg = gmfcharorder(CHI);
    7771        3675 :   ord = itou(ordg);
    7772        3675 :   return rootsof1q_cx(znchareval_i(CHI,n,ordg), ord, prec);
    7773             : }
    7774             : 
    7775             : static GEN
    7776        4403 : RgV_shift(GEN V, GEN gn)
    7777             : {
    7778             :   long i, n, l;
    7779             :   GEN W;
    7780        4403 :   if (typ(gn) != t_INT) pari_err_BUG("RgV_shift [n not integral]");
    7781        4403 :   n = itos(gn);
    7782        4403 :   if (n < 0) pari_err_BUG("RgV_shift [n negative]");
    7783        4403 :   if (!n) return V;
    7784          98 :   W = cgetg_copy(V, &l); if (n > l-1) n = l-1;
    7785          98 :   for (i=1; i <= n; i++) gel(W,i) = gen_0;
    7786          98 :   for (    ; i < l; i++) gel(W,i) = gel(V, i-n);
    7787          98 :   return W;
    7788             : }
    7789             : static GEN
    7790        6776 : hash_eisengacx(hashtable *H, void *E, long w, GEN ga, long n, long prec)
    7791             : {
    7792        6776 :   ulong h = H->hash(E);
    7793        6776 :   hashentry *e = hash_search2(H, E, h);
    7794             :   GEN v;
    7795        6776 :   if (e) v = (GEN)e->val;
    7796             :   else
    7797             :   {
    7798        4403 :     v = mfeisensteingacx((GEN)E, w, ga, n, prec);
    7799        4403 :     hash_insert2(H, E, (void*)v, h);
    7800             :   }
    7801        6776 :   return v;
    7802             : }
    7803             : static GEN
    7804        4403 : vecj_expand(GEN B, hashtable *H, long w, GEN ga, long n, long prec)
    7805             : {
    7806             :   GEN E1, E2, v;
    7807        4403 :   parse_vecj(B, &E1, &E2);
    7808        4403 :   v = hash_eisengacx(H, (void*)E1, w, ga, n, prec);
    7809        4403 :   if (E2)
    7810             :   {
    7811        2352 :     GEN u = hash_eisengacx(H, (void*)E2, w, ga, n, prec);
    7812        2352 :     GEN a = gadd(gel(v,1), gel(u,1));
    7813        2352 :     GEN b = RgV_mul_RgXn(gel(v,2), gel(u,2));
    7814        2352 :     v = mkvec2(a,b);
    7815             :   }
    7816        4403 :   return v;
    7817             : }
    7818             : static GEN
    7819         889 : shift_M(GEN M, GEN Valpha, long w)
    7820             : {
    7821         889 :   long i, l = lg(Valpha);
    7822         889 :   GEN almin = vecmin(Valpha);
    7823        5292 :   for (i = 1; i < l; i++)
    7824             :   {
    7825        4403 :     GEN alpha = gel(Valpha, i), gsh = gmulsg(w, gsub(alpha,almin));
    7826        4403 :     gel(M,i) = RgV_shift(gel(M,i), gsh);
    7827             :   }
    7828         889 :   return almin;
    7829             : }
    7830             : static GEN mfeisensteinspaceinit(GEN NK);
    7831             : #if 0
    7832             : /* ga in M_2^+(Z)), n >= 0 */
    7833             : static GEN
    7834             : mfgaexpansion_init(GEN mf, GEN ga, long n, long prec)
    7835             : {
    7836             :   GEN M, Mvecj, vecj, almin, Valpha;
    7837             :   long i, w, l, N = MF_get_N(mf), c = itos(gcoeff(ga,2,1));
    7838             :   hashtable *H;
    7839             : 
    7840             :   if (c % N == 0)
    7841             :   { /* ga in G_0(N), trivial case; w = 1 */
    7842             :     GEN chid = mfcharcxeval(MF_get_CHI(mf), itos(gcoeff(ga,2,2)), prec);
    7843             :     return mkvec2(chid, utoi(n));
    7844             :   }
    7845             : 
    7846             :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7847             :   if (lg(Mvecj) < 5) pari_err_IMPL("mfgaexpansion_init in this case");
    7848             :   w = mfcuspcanon_width(N, c);
    7849             :   vecj = gel(Mvecj, 3);
    7850             :   l = lg(vecj);
    7851             :   M = cgetg(l, t_VEC);
    7852             :   Valpha = cgetg(l, t_VEC);
    7853             :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7854             :                      (int(*)(void*,void*))&gidentical, 1);
    7855             :   for (i = 1; i < l; i++)
    7856             :   {
    7857             :     GEN v = vecj_expand(gel(vecj,i), H, w, ga, n, prec);
    7858             :     gel(Valpha,i) = gel(v,1);
    7859             :     gel(M,i) = gel(v,2);
    7860             :   }
    7861             :   almin = shift_M(M, Valpha, w);
    7862             :   return mkvec3(almin, utoi(w), M);
    7863             : }
    7864             : /* half-integer weight not supported; vF = [F,eisendec(F)].
    7865             :  * Minit = mfgaexpansion_init(mf, ga, n, prec) */
    7866             : static GEN
    7867             : mfgaexpansion_with_init(GEN Minit, GEN vF)
    7868             : {
    7869             :   GEN v;
    7870             :   if (lg(Minit) == 3)
    7871             :   { /* ga in G_0(N) */
    7872             :     GEN chid = gel(Minit,1), gn = gel(Minit,2);
    7873             :     v = mfcoefs_i(gel(vF,1), itou(gn), 1);
    7874             :     v = mkvec3(gen_0, gen_1, RgV_Rg_mul(v,chid));
    7875             :   }
    7876             :   else
    7877             :   {
    7878             :     GEN V = RgM_RgC_mul(gel(Minit,3), gel(vF,2));
    7879             :     v = mkvec3(gel(Minit,1), gel(Minit,2), V);
    7880             :   }
    7881             :   return v;
    7882             : }
    7883             : #endif
    7884             : 
    7885             : /* B = mfeisensteindec(F) already embedded, ga in M_2^+(Z)), n >= 0 */
    7886             : static GEN
    7887         889 : mfgaexpansion_i(GEN mf, GEN B0, GEN ga, long n, long prec)
    7888             : {
    7889         889 :   GEN M, Mvecj, vecj, almin, Valpha, B, E = NULL;
    7890         889 :   long i, j, w, nw, l, N = MF_get_N(mf), bit = prec2nbits(prec) / 2;
    7891             :   hashtable *H;
    7892             : 
    7893         889 :   Mvecj = obj_check(mf, MF_EISENSPACE);
    7894         889 :   if (lg(Mvecj) < 5) { E = gel(Mvecj, 2); Mvecj = gel(Mvecj, 1); }
    7895         889 :   vecj = gel(Mvecj, 3);
    7896         889 :   l = lg(vecj);
    7897         889 :   B = cgetg(l, t_COL);
    7898         889 :   M = cgetg(l, t_VEC);
    7899         889 :   Valpha = cgetg(l, t_VEC);
    7900         889 :   w = mfZC_width(N, gel(ga,1));
    7901         889 :   nw = E ? n + w : n;
    7902         889 :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7903             :                      (int(*)(void*,void*))&gidentical, 1);
    7904        7882 :   for (i = j = 1; i < l; i++)
    7905             :   {
    7906             :     GEN v;
    7907        6993 :     if (gequal0(gel(B0,i))) continue;
    7908        4403 :     v = vecj_expand(gel(vecj,i), H, w, ga, nw, prec);
    7909        4403 :     gel(B,j) = gel(B0,i);
    7910        4403 :     gel(Valpha,j) = gel(v,1);
    7911        4403 :     gel(M,j) = gel(v,2); j++;
    7912             :   }
    7913         889 :   setlg(Valpha, j);
    7914         889 :   setlg(B, j);
    7915         889 :   setlg(M, j); l = j;
    7916         889 :   if (l == 1) return mkvec3(gen_0, utoi(w), zerovec(n+1));
    7917         889 :   almin = shift_M(M, Valpha, w);
    7918         889 :   B = RgM_RgC_mul(M, B); l = lg(B);
    7919      138110 :   for (i = 1; i < l; i++)
    7920      137221 :     if (gexpo(gel(B,i)) < -bit) gel(B,i) = gen_0;
    7921         889 :   settyp(B, t_VEC);
    7922         889 :   if (E)
    7923             :   {
    7924          21 :     GEN v = hash_eisengacx(H, (void*)E, w, ga, n, prec);
    7925          21 :     long ell = 0;
    7926          21 :     almin = gsub(almin, gel(v,1));
    7927          21 :     if (gsigne(almin) < 0)
    7928             :     {
    7929           0 :       GEN gell = gceil(gmulsg(-w, almin));
    7930           0 :       ell = itos(gell);
    7931           0 :       almin = gadd(almin, gdivgs(gell, w));
    7932           0 :       if (nw < ell) pari_err_IMPL("alpha < 0 in mfgaexpansion");
    7933             :     }
    7934          21 :     B = vecslice(B, ell + 1, n + ell + 1);
    7935          21 :     B = RgV_div_RgXn(B, gel(v,2));
    7936             :   }
    7937         889 :   return mkvec3(almin, utoi(w), B);
    7938             : }
    7939             : 
    7940             : /* Theta multiplier: assume 4 | C, (C,D)=1 */
    7941             : static GEN
    7942         238 : mfthetamultiplier(long C, long D)
    7943             : {
    7944         238 :   long s = kross(C, D);
    7945         238 :   if ((D&3L) == 1) return stoi(s);
    7946          49 :   return s > 0 ? powIs(3) : gen_I();
    7947             : }
    7948             : static GEN
    7949         238 : mfthetaexpansion(GEN M, long n)
    7950             : {
    7951         238 :   GEN s, al, sla, V = zerovec(n + 1);
    7952         238 :   long w, lim, la, f, C = itos(gcoeff(M, 2, 1)), D = itos(gcoeff(M, 2, 2));
    7953         238 :   switch (C & 3L)
    7954             :   {
    7955          56 :     case 0: al = gen_0; w = 1;
    7956          56 :       s = mfthetamultiplier(C,D);
    7957          56 :       lim = usqrt(n); gel(V, 1) = s;
    7958          56 :       s = gmul2n(s, 1);
    7959          56 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = s;
    7960          56 :       break;
    7961          84 :     case 2: al = sstoQ(1,4); w = 1;
    7962          84 :       s = gmul2n(mfthetamultiplier(C - 2*D, D), 1);
    7963          84 :       lim = (usqrt(n << 2) - 1) >> 1;
    7964          84 :       for (f = 0; f <= lim; f++) gel(V, f*(f+1) + 1) = s;
    7965          84 :       break;
    7966          98 :     default: al = gen_0; w = 4; la = (-D*C) & 3L;
    7967          98 :       s = mfthetamultiplier(-(D + la*C), C);
    7968          98 :       s = gsub(s, mulcxI(s));
    7969          98 :       sla = gmul(s, powIs(-la));
    7970          98 :       lim = usqrt(n); gel(V, 1) = gmul2n(s, -1);
    7971          98 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = odd(f) ? sla : s;
    7972          98 :       break;
    7973             :   }
    7974         238 :   return mkvec3(al, stoi(w), V);
    7975             : }
    7976             : 
    7977             : /* F 1/2 integral weight */
    7978             : static GEN
    7979         238 : mf2gaexpansion(GEN mf2, GEN F, GEN ga, long n, long prec)
    7980             : {
    7981         238 :   GEN FT = mfmultheta(F), mf = obj_checkbuild(mf2, MF_MF2INIT, &mf2init);
    7982             :   GEN res, V1, Tres, V2, al, V, gsh;
    7983         238 :   long w2, C = itos(gcoeff(ga,2,1)), w = mfcuspcanon_width(MF_get_N(mf), C);
    7984         238 :   long ext = ((C & 3L) != 2)? 0: (w+3) >> 2;
    7985         238 :   long prec2 = prec + nbits2extraprec((long)M_PI/(2*M_LN2)*sqrt(n + ext));
    7986         238 :   res = mfgaexpansion(mf, FT, ga, n + ext, prec2);
    7987         238 :   Tres = mfthetaexpansion(ga, n + ext);
    7988         238 :   V1 = gel(res,3);
    7989         238 :   V2 = gel(Tres,3);
    7990         238 :   al = gsub(gel(res,1), gel(Tres,1));
    7991         238 :   w2 = itos(gel(Tres,2));
    7992         238 :   if (w != itos(gel(res,2)) || w % w2)
    7993           0 :     pari_err_BUG("mf2gaexpansion [incorrect w2 or w]");
    7994         238 :   if (w2 != w) V2 = bdexpand(V2, w/w2);
    7995         238 :   V = RgV_div_RgXn(V1, V2);
    7996         238 :   gsh = gfloor(gmulsg(w, al));
    7997         238 :   if (!gequal0(gsh))
    7998             :   {
    7999          28 :     al = gsub(al, gdivgs(gsh, w));
    8000          28 :     if (gsigne(gsh) > 0)
    8001             :     {
    8002           0 :       V = RgV_shift(V, gsh);
    8003           0 :       V = vecslice(V, 1, n + 1);
    8004             :     }
    8005             :     else
    8006             :     {
    8007          28 :       long sh = -itos(gsh), i;
    8008          28 :       if (sh > ext) pari_err_BUG("mf2gaexpansion [incorrect sh]");
    8009         119 :       for (i = 1; i <= sh; i++)
    8010          91 :         if (!gequal0(gel(V,i))) pari_err_BUG("mf2gaexpansion [sh too large]");
    8011          28 :       V = vecslice(V, sh+1, n + sh+1);
    8012             :     }
    8013             :   }
    8014         238 :   obj_free(mf); return mkvec3(al, stoi(w), gprec_wtrunc(V, prec));
    8015             : }
    8016             : 
    8017             : static GEN
    8018          70 : mfgaexpansionatkin(GEN mf, GEN F, GEN C, GEN D, long Q, long n, long prec)
    8019             : {
    8020          70 :   GEN mfa = mfatkininit_i(mf, Q, 0, prec), MQ = gel(mfa,2);
    8021          70 :   long i, FC, k = MF_get_k(mf);
    8022          70 :   GEN x, v, V, z, s, CHI = mfchartoprimitive(MF_get_CHI(mf), &FC);
    8023             : 
    8024             :   /* V = mfcoefs(F | w_Q, n), can't use mfatkin because MQ non-rational */
    8025          70 :   V = RgM_RgC_mul(mfcoefs_mf(mf,n,1), RgM_RgC_mul(MQ, mftobasis_i(mf,F)));
    8026          70 :   (void)bezout(utoipos(Q), C, &x, &v);
    8027          70 :   s = mfchareval_i(CHI, (umodiu(x, FC) * umodiu(D, FC)) % FC);
    8028          70 :   s = gdiv(s, gpow(utoipos(Q), sstoQ(k,2), prec));
    8029          70 :   V = RgV_Rg_mul(V, s);
    8030          70 :   z = rootsof1powinit(umodiu(D,Q)*umodiu(v,Q) % Q, Q, prec);
    8031          70 :   for (i = 1; i <= n+1; i++) gel(V,i) = gmul(gel(V,i), rootsof1pow(z, i-1));
    8032          70 :   return mkvec3(gen_0, utoipos(Q), V);
    8033             : }
    8034             : 
    8035             : /* allow F of the form [F, mf_eisendec(F)]~ */
    8036             : static GEN
    8037        1463 : mfgaexpansion(GEN mf, GEN F, GEN ga, long n, long prec)
    8038             : {
    8039        1463 :   GEN v, EF = NULL, res, Mvecj, c, d;
    8040             :   long precnew, N;
    8041             : 
    8042        1463 :   if (n < 0) pari_err_DOMAIN("mfgaexpansion", "n", "<", gen_0, stoi(n));
    8043        1463 :   if (typ(F) == t_COL && lg(F) == 3) { EF = gel(F,2); F = gel(F,1); }
    8044        1463 :   if (!checkmf_i(F)) pari_err_TYPE("mfgaexpansion", F);
    8045        1463 :   if (!check_SL2Z(ga)) pari_err_TYPE("mfgaexpansion",ga);
    8046        1463 :   if (typ(mf_get_gk(F)) != t_INT) return mf2gaexpansion(mf, F, ga, n, prec);
    8047        1225 :   c = gcoeff(ga,2,1);
    8048        1225 :   d = gcoeff(ga,2,2);
    8049        1225 :   N = MF_get_N(mf);
    8050        1225 :   if (!umodiu(c, mf_get_N(F)))
    8051             :   { /* trivial case: ga in Gamma_0(N) */
    8052         266 :     long w = mfcuspcanon_width(N, umodiu(c,N));
    8053         266 :     GEN CHI = mf_get_CHI(F);
    8054         266 :     GEN chid = mfcharcxeval(CHI, umodiu(d,mfcharmodulus(CHI)), prec);
    8055         266 :     v = mfcoefs_i(F, n/w, 1); if (!isint1(chid)) v = RgV_Rg_mul(v,chid);
    8056         266 :     return mkvec3(gen_0, stoi(w), bdexpandall(v,w,n+1));
    8057             :   }
    8058         959 :   mf = MF_set_new(mf);
    8059         959 :   if (MF_get_space(mf) == mf_NEW)
    8060             :   {
    8061         441 :     long cN = umodiu(c,N), g = ugcd(cN,N), Q = N/g;
    8062         441 :     GEN CHI = MF_get_CHI(mf);
    8063         441 :     if (ugcd(cN, Q)==1 && mfcharorder(CHI) <= 2
    8064         217 :                        && g % mfcharconductor(CHI) == 0
    8065         112 :                        && degpol(mf_get_field(F)) == 1)
    8066          70 :       return mfgaexpansionatkin(mf, F, c, d, Q, n, prec);
    8067             :   }
    8068         889 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    8069         889 :   precnew = prec;
    8070         889 :   if (lg(Mvecj) < 5)
    8071             :   {
    8072          21 :     long e, w = mfZC_width(N, gel(ga,1));
    8073          21 :     GEN v, E = gel(Mvecj,2);
    8074          21 :     v = mfeisensteingacx(E, w, ga, n, LOWDEFAULTPREC);
    8075          21 :     v = gel(v,2);
    8076          21 :     e = gexpo(RgXn_inv(RgV_to_RgX(v,0), n+1));
    8077          21 :     if (e > 0) precnew += nbits2extraprec(e);
    8078             :   }
    8079         889 :   if (!EF) EF = mf_eisendec(mf, F, precnew);
    8080         889 :   res = mfgaexpansion_i(mf, EF, ga, n, precnew);
    8081         889 :   return precnew == prec ? res : gprec_wtrunc(res, prec);
    8082             : }
    8083             : 
    8084             : /* parity = -1 or +1 */
    8085             : static GEN
    8086         217 : findd(long N, long parity)
    8087             : {
    8088         217 :   GEN L, D = mydivisorsu(N);
    8089         217 :   long i, j, l = lg(D);
    8090         217 :   L = cgetg(l, t_VEC);
    8091        1218 :   for (i = j = 1; i < l; i++)
    8092             :   {
    8093        1001 :     long d = D[i];
    8094        1001 :     if (parity == -1) d = -d;
    8095        1001 :     if (sisfundamental(d)) gel(L,j++) = stoi(d);
    8096             :   }
    8097         217 :   setlg(L,j); return L;
    8098             : }
    8099             : /* does ND contain a divisor of N ? */
    8100             : static int
    8101         413 : seenD(long N, GEN ND)
    8102             : {
    8103         413 :   long j, l = lg(ND);
    8104         427 :   for (j = 1; j < l; j++)
    8105          14 :     if (N % ND[j] == 0) return 1;
    8106         413 :   return 0;
    8107             : }
    8108             : static GEN
    8109          42 : search_levels(GEN vN, const char *f)
    8110             : {
    8111          42 :   switch(typ(vN))
    8112             :   {
    8113           7 :     case t_INT: vN = mkvecsmall(itos(vN)); break;
    8114          35 :     case t_VEC: case t_COL: vN = ZV_to_zv(vN); break;
    8115           0 :     case t_VECSMALL: vN = leafcopy(vN); break;
    8116           0 :     default: pari_err_TYPE(f, vN);
    8117             :   }
    8118          42 :   vecsmall_sort(vN); return vN;
    8119             : }
    8120             : GEN
    8121          14 : mfsearch(GEN NK, GEN V, long space)
    8122             : {
    8123          14 :   pari_sp av = avma;
    8124             :   GEN F, gk, NbyD, vN;
    8125             :   long n, nk, dk, parity, nV, i, lvN;
    8126             : 
    8127          14 :   if (typ(NK) != t_VEC || lg(NK) != 3) pari_err_TYPE("mfsearch", NK);
    8128          14 :   gk = gel(NK,2);
    8129          14 :   if (typ(gmul2n(gk, 1)) != t_INT) pari_err_TYPE("mfsearch [k]", gk);
    8130          14 :   switch(typ(V))
    8131             :   {
    8132          14 :     case t_VEC: V = shallowtrans(V);
    8133          14 :     case t_COL: break;
    8134           0 :     default: pari_err_TYPE("mfsearch [V]", V);
    8135             :   }
    8136          14 :   vN = search_levels(gel(NK,1), "mfsearch [N]");
    8137          14 :   lvN = lg(vN);
    8138             : 
    8139          14 :   Qtoss(gk, &nk,&dk);
    8140          14 :   parity = (dk == 1 && odd(nk)) ? -1 : 1;
    8141          14 :   nV = lg(V)-2;
    8142          14 :   F = cgetg(1, t_VEC);
    8143          14 :   NbyD = const_vec(vN[lvN-1], cgetg(1,t_VECSMALL));
    8144         231 :   for (n = 1; n < lvN; n++)
    8145             :   {
    8146         217 :     long N = vN[n];
    8147             :     GEN L;
    8148         217 :     if (N <= 0 || (dk == 2 && (N & 3))) continue;
    8149         217 :     L = findd(N, parity);
    8150         630 :     for (i = 1; i < lg(L); i++)
    8151             :     {
    8152         413 :       GEN mf, M, CO, gD = gel(L,i);
    8153         413 :       GEN *ND = (GEN*)NbyD + itou(gD); /* points to NbyD[|D|] */
    8154             : 
    8155         413 :       if (seenD(N, *ND)) continue;
    8156         413 :       mf = mfinit_Nndkchi(N, nk, dk, get_mfchar(gD), space, 1);
    8157         413 :       M = mfcoefs_mf(mf, nV, 1);
    8158         413 :       CO = inverseimage(M, V); if (lg(CO) == 1) continue;
    8159             : 
    8160          42 :       F = vec_append(F, mflinear(mf,CO));
    8161          42 :       *ND = vecsmall_append(*ND, N); /* add to NbyD[|D|] */
    8162             :     }
    8163             :   }
    8164          14 :   return gerepilecopy(av, F);
    8165             : }
    8166             : 
    8167             : static GEN
    8168         882 : search_from_split(GEN mf, GEN vap, GEN vlp)
    8169             : {
    8170         882 :   pari_sp av = avma;
    8171         882 :   long lvlp = lg(vlp), j, jv, l1;
    8172         882 :   GEN v, NK, S1, S, M = NULL;
    8173             : 
    8174         882 :   S1 = gel(split_i(mf, 1, 0), 1); /* rational newforms */
    8175         882 :   l1 = lg(S1);
    8176         882 :   if (l1 == 1) return gc_NULL(av);
    8177         448 :   v = cgetg(l1, t_VEC);
    8178         448 :   S = MF_get_S(mf);
    8179         448 :   NK = mf_get_NK(gel(S,1));
    8180         448 :   if (lvlp > 1) M = rowpermute(mfcoefs_mf(mf, vlp[lvlp-1], 1), vlp);
    8181         966 :   for (j = jv = 1; j < l1; j++)
    8182             :   {
    8183         518 :     GEN vF = gel(S1,j);
    8184             :     long t;
    8185         651 :     for (t = lvlp-1; t > 0; t--)
    8186             :     { /* lhs = vlp[j]-th coefficient of eigenform */
    8187         595 :       GEN rhs = gel(vap,t), lhs = RgMrow_RgC_mul(M, vF, t);
    8188         595 :       if (!gequal(lhs, rhs)) break;
    8189             :     }
    8190         518 :     if (!t) gel(v,jv++) = mflinear_i(NK,S,vF);
    8191             :   }
    8192         448 :   if (jv == 1) return gc_NULL(av);
    8193          56 :   setlg(v,jv); return v;
    8194             : }
    8195             : GEN
    8196          28 : mfeigensearch(GEN NK, GEN AP)
    8197             : {
    8198          28 :   pari_sp av = avma;
    8199          28 :   GEN k, vN, vap, vlp, vres = cgetg(1, t_VEC), D;
    8200             :   long n, lvN, i, l, even;
    8201             : 
    8202          28 :   if (!AP) l = 1;
    8203             :   else
    8204             :   {
    8205          28 :     l = lg(AP);
    8206          28 :     if (typ(AP) != t_VEC) pari_err_TYPE("mfeigensearch",AP);
    8207             :   }
    8208          28 :   vap = cgetg(l, t_VEC);
    8209          28 :   vlp = cgetg(l, t_VECSMALL);
    8210          28 :   if (l > 1)
    8211             :   {
    8212          28 :     GEN perm = indexvecsort(AP, mkvecsmall(1));
    8213          77 :     for (i = 1; i < l; i++)
    8214             :     {
    8215          49 :       GEN v = gel(AP,perm[i]), gp, ap;
    8216          49 :       if (typ(v) != t_VEC || lg(v) != 3) pari_err_TYPE("mfeigensearch", AP);
    8217          49 :       gp = gel(v,1);
    8218          49 :       ap = gel(v,2);
    8219          49 :       if (typ(gp) != t_INT || (typ(ap) != t_INT && typ(ap) != t_INTMOD))
    8220           0 :         pari_err_TYPE("mfeigensearch", AP);
    8221          49 :       gel(vap,i) = ap;
    8222          49 :       vlp[i] = itos(gp)+1; if (vlp[i] < 0) pari_err_TYPE("mfeigensearch", AP);
    8223             :     }
    8224             :   }
    8225          28 :   l = lg(NK);
    8226          28 :   if (typ(NK) != t_VEC || l != 3) pari_err_TYPE("mfeigensearch",NK);
    8227          28 :   k = gel(NK,2);
    8228          28 :   vN = search_levels(gel(NK,1), "mfeigensearch [N]");
    8229          28 :   lvN = lg(vN);
    8230          28 :   vecsmall_sort(vlp);
    8231          28 :   even = !mpodd(k);
    8232         966 :   for (n = 1; n < lvN; n++)
    8233             :   {
    8234         938 :     pari_sp av2 = avma;
    8235             :     GEN mf, L;
    8236         938 :     long N = vN[n];
    8237         938 :     if (even) D = gen_1;
    8238             :     else
    8239             :     {
    8240         112 :       long r = (N&3L);
    8241         112 :       if (r == 1 || r == 2) continue;
    8242          56 :       D = stoi( corediscs(-N, NULL) ); /* < 0 */
    8243             :     }
    8244         882 :     mf = mfinit_i(mkvec3(utoipos(N), k, D), mf_NEW);
    8245         882 :     L = search_from_split(mf, vap, vlp);
    8246         882 :     if (L) vres = shallowconcat(vres, L); else set_avma(av2);
    8247             :   }
    8248          28 :   return gerepilecopy(av, vres);
    8249             : }
    8250             : 
    8251             : /* tf_{N,k}(n) */
    8252             : static GEN
    8253     3144603 : mfnewtracecache(long N, long k, long n, cachenew_t *cache)
    8254             : {
    8255     3144603 :   GEN C = NULL, S;
    8256             :   long lcache;
    8257     3144603 :   if (!n) return gen_0;
    8258     3042942 :   S = gel(cache->vnew,N);
    8259     3042942 :   lcache = lg(S);
    8260     3042942 :   if (n < lcache) C = gel(S, n);
    8261     3042942 :   if (C) cache->newHIT++;
    8262     1860670 :   else C = mfnewtrace_i(N,k,n,cache);
    8263     3042942 :   cache->newTOTAL++;
    8264     3042942 :   if (n < lcache) gel(S,n) = C;
    8265     3042942 :   return C;
    8266             : }
    8267             : 
    8268             : static long
    8269        1386 : mfdim_Nkchi(long N, long k, GEN CHI, long space)
    8270             : {
    8271        1386 :   if (k < 0 || badchar(N,k,CHI)) return 0;
    8272        1085 :   if (k == 0)
    8273          35 :     return mfcharistrivial(CHI) && !space_is_cusp(space)? 1: 0;
    8274        1050 :   switch(space)
    8275             :   {
    8276         238 :     case mf_NEW: return mfnewdim(N,k,CHI);
    8277         196 :     case mf_CUSP:return mfcuspdim(N,k,CHI);
    8278         168 :     case mf_OLD: return mfolddim(N,k,CHI);
    8279         217 :     case mf_FULL:return mffulldim(N,k,CHI);
    8280         231 :     case mf_EISEN: return mfeisensteindim(N,k,CHI);
    8281           0 :     default: pari_err_FLAG("mfdim");
    8282             :   }
    8283             :   return 0;/*LCOV_EXCL_LINE*/
    8284             : }
    8285             : static long
    8286        2114 : mfwt1dimsum(long N, long space)
    8287             : {
    8288        2114 :   switch(space)
    8289             :   {
    8290        1050 :     case mf_NEW:  return mfwt1newdimsum(N);
    8291        1057 :     case mf_CUSP: return mfwt1cuspdimsum(N);
    8292           7 :     case mf_OLD:  return mfwt1olddimsum(N);
    8293             :   }
    8294           0 :   pari_err_FLAG("mfdim");
    8295             :   return 0; /*LCOV_EXCL_LINE*/
    8296             : }
    8297             : /* mfdim for k = nk/dk */
    8298             : static long
    8299       44744 : mfdim_Nndkchi(long N, long nk, long dk, GEN CHI, long space)
    8300       43463 : { return (dk == 2)? mf2dim_Nkchi(N, nk >> 1, CHI, space)
    8301       88207 :                   : mfdim_Nkchi(N, nk, CHI, space); }
    8302             : /* FIXME: use direct dim Gamma1(N) formula, don't compute individual spaces */
    8303             : static long
    8304         252 : mfwtkdimsum(long N, long k, long dk, long space)
    8305             : {
    8306         252 :   GEN w = mfchars(N, k, dk, NULL);
    8307         252 :   long i, j, D = 0, l = lg(w);
    8308        1239 :   for (i = j = 1; i < l; i++)
    8309             :   {
    8310         987 :     GEN CHI = gel(w,i);
    8311         987 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8312         987 :     if (d) D += d * myeulerphiu(mfcharorder(CHI));
    8313             :   }
    8314         252 :   return D;
    8315             : }
    8316             : static GEN
    8317         105 : mfwt1dims(long N, GEN vCHI, long space)
    8318             : {
    8319         105 :   GEN D = NULL;
    8320         105 :   switch(space)
    8321             :   {
    8322          56 :     case mf_NEW: D = mfwt1newdimall(N, vCHI); break;
    8323          21 :     case mf_CUSP:D = mfwt1cuspdimall(N, vCHI); break;
    8324          28 :     case mf_OLD: D = mfwt1olddimall(N, vCHI); break;
    8325           0 :     default: pari_err_FLAG("mfdim");
    8326             :   }
    8327         105 :   return D;
    8328             : }
    8329             : static GEN
    8330        2961 : mfwtkdims(long N, long k, long dk, GEN vCHI, long space)
    8331             : {
    8332        2961 :   GEN D, w = mfchars(N, k, dk, vCHI);
    8333        2961 :   long i, j, l = lg(w);
    8334        2961 :   D = cgetg(l, t_VEC);
    8335       46592 :   for (i = j = 1; i < l; i++)
    8336             :   {
    8337       43631 :     GEN CHI = gel(w,i);
    8338       43631 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8339       43631 :     if (vCHI)
    8340         574 :       gel(D, j++) = mkvec2s(d, 0);
    8341       43057 :     else if (d)
    8342        2520 :       gel(D, j++) = fmt_dim(CHI, d, 0);
    8343             :   }
    8344        2961 :   setlg(D,j); return D;
    8345             : }
    8346             : GEN
    8347        5719 : mfdim(GEN NK, long space)
    8348             : {
    8349        5719 :   pari_sp av = avma;
    8350             :   long N, k, dk, joker;
    8351             :   GEN CHI, mf;
    8352        5719 :   if ((mf = checkMF_i(NK))) return utoi(MF_get_dim(mf));
    8353        5586 :   checkNK2(NK, &N, &k, &dk, &CHI, 2);
    8354        5586 :   if (!CHI) joker = 1;
    8355             :   else
    8356        2611 :     switch(typ(CHI))
    8357             :     {
    8358        2373 :       case t_INT: joker = 2; break;
    8359         112 :       case t_COL: joker = 3; break;
    8360         126 :       default: joker = 0; break;
    8361             :     }
    8362        5586 :   if (joker)
    8363             :   {
    8364             :     long d;
    8365             :     GEN D;
    8366        5460 :     if (k < 0) switch(joker)
    8367             :     {
    8368           0 :       case 1: return cgetg(1,t_VEC);
    8369           7 :       case 2: return gen_0;
    8370           0 :       case 3: return mfdim0all(CHI);
    8371             :     }
    8372        5453 :     if (k == 0)
    8373             :     {
    8374          28 :       if (space_is_cusp(space)) switch(joker)
    8375             :       {
    8376           7 :         case 1: return cgetg(1,t_VEC);
    8377           0 :         case 2: return gen_0;
    8378           7 :         case 3: return mfdim0all(CHI);
    8379             :       }
    8380          14 :       switch(joker)
    8381             :       {
    8382             :         long i, l;
    8383           7 :         case 1: retmkvec(fmt_dim(mfchartrivial(),0,0));
    8384           0 :         case 2: return gen_1;
    8385           7 :         case 3: l = lg(CHI); D = cgetg(l,t_VEC);
    8386          35 :                 for (i = 1; i < l; i++)
    8387             :                 {
    8388          28 :                   long t = mfcharistrivial(gel(CHI,i));
    8389          28 :                   gel(D,i) = mkvec2(t? gen_1: gen_0, gen_0);
    8390             :                 }
    8391           7 :                 return D;
    8392             :       }
    8393             :     }
    8394        5425 :     if (dk == 1 && k == 1 && space != mf_EISEN)
    8395         105 :     {
    8396        2219 :       long fix = 0, space0 = space;
    8397        2219 :       if (space == mf_FULL) space = mf_CUSP; /* remove Eisenstein part */
    8398        2219 :       if (joker == 2)
    8399             :       {
    8400        2114 :         d = mfwt1dimsum(N, space);
    8401        2114 :         if (space0 == mf_FULL) d += mfwtkdimsum(N,k,dk,mf_EISEN);/*add it back*/
    8402        2114 :         set_avma(av); return utoi(d);
    8403             :       }
    8404             :       /* must initialize explicitly: trivial spaces for E_k/S_k differ */
    8405         105 :       if (space0 == mf_FULL)
    8406             :       {
    8407           7 :         if (!CHI) fix = 1; /* must remove 0 spaces */
    8408           7 :         CHI = mfchars(N, k, dk, CHI);
    8409             :       }
    8410         105 :       D = mfwt1dims(N, CHI, space);
    8411         105 :       if (space0 == mf_FULL)
    8412             :       {
    8413           7 :         GEN D2 = mfwtkdims(N, k, dk, CHI, mf_EISEN);
    8414           7 :         D = merge_dims(D, D2, fix? CHI: NULL);
    8415             :       }
    8416             :     }
    8417             :     else
    8418             :     {
    8419        3206 :       if (joker==2) { d = mfwtkdimsum(N,k,dk,space); set_avma(av); return utoi(d); }
    8420        2954 :       D = mfwtkdims(N, k, dk, CHI, space);
    8421             :     }
    8422        3059 :     if (!CHI) return gerepileupto(av, vecsort(D, mkvecsmall(1)));
    8423         105 :     return gerepilecopy(av, D);
    8424             :   }
    8425         126 :   return utoi( mfdim_Nndkchi(N, k, dk, CHI, space) );
    8426             : }
    8427             : 
    8428             : GEN
    8429         308 : mfbasis(GEN NK, long space)
    8430             : {
    8431         308 :   pari_sp av = avma;
    8432             :   long N, k, dk;
    8433             :   GEN mf, CHI;
    8434         308 :   if ((mf = checkMF_i(NK))) return concat(gel(mf,2), gel(mf,3));
    8435           7 :   checkNK2(NK, &N, &k, &dk, &CHI, 0);
    8436           7 :   if (dk == 2) return gerepilecopy(av, mf2basis(N, k>>1, CHI, NULL, space));
    8437           7 :   mf = mfinit_Nkchi(N, k, CHI, space, 1);
    8438           7 :   return gerepilecopy(av, MF_get_basis(mf));
    8439             : }
    8440             : 
    8441             : static GEN
    8442          28 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
    8443          28 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
    8444             : /* r / x + O(1) */
    8445             : static GEN
    8446          28 : simple_pole(GEN r)
    8447             : {
    8448          28 :   GEN S = deg1ser_shallow(gen_0, r, 0, 1);
    8449          28 :   setvalp(S, -1); return S;
    8450             : }
    8451             : 
    8452             : /* F form, E embedding; mfa = mfatkininit or root number (eigenform case) */
    8453             : static GEN
    8454         105 : mflfuncreate(GEN mfa, GEN F, GEN E, GEN N, GEN gk)
    8455             : {
    8456         105 :   GEN LF = cgetg(8,t_VEC), polar = cgetg(1,t_COL), eps;
    8457         105 :   long k = itou(gk);
    8458         105 :   gel(LF,1) = lfuntag(t_LFUN_MFCLOS, mkvec3(F,E,gen_1));
    8459         105 :   if (typ(mfa) != t_VEC)
    8460          84 :     eps = mfa; /* cuspidal eigenform: root number; no poles */
    8461             :   else
    8462             :   { /* mfatkininit */
    8463          21 :     GEN a0, b0, vF, vG, G = NULL, M = gdiv(gel(mfa,2), gel(mfa,3)), mf = gel(mfa,4);
    8464          21 :     vF = mftobasis_i(mf, F);
    8465          21 :     vG = RgM_RgC_mul(M, vF);
    8466          21 :     if (gequal(vF,vG)) eps = gen_1;
    8467           7 :     else if (gequal(vF,gneg(vG))) eps = gen_m1;
    8468             :     else
    8469             :     { /* not self-dual */
    8470           7 :       eps = NULL;
    8471           7 :       G = mfatkin(mfa, F);
    8472           7 :       gel(LF,2) = lfuntag(t_LFUN_MFCLOS, mkvec3(G,E,ginv(gel(mfa,3))));
    8473           7 :       gel(LF,6) = powIs(k);
    8474             :     }
    8475             :     /* polar part */
    8476          21 :     a0 = mfcoef(F,0);
    8477          21 :     b0 = eps? gmul(eps,a0): mfcoef(G,0);
    8478          21 :     if (!gequal0(b0))
    8479             :     {
    8480          14 :       b0 = mulcxpowIs(gmul2n(b0,1), k);
    8481          14 :       polar = vec_append(polar, mkvec2(gk, simple_pole(b0)));
    8482             :     }
    8483          21 :     if (!gequal0(a0))
    8484             :     {
    8485          14 :       a0 = gneg(gmul2n(a0,1));
    8486          14 :       polar = vec_append(polar, mkvec2(gen_0, simple_pole(a0)));
    8487             :     }
    8488             :   }
    8489         105 :   if (eps) /* self-dual */
    8490             :   {
    8491          98 :     gel(LF,2) = mfcharorder(mf_get_CHI(F)) <= 2? gen_0: gen_1;
    8492          98 :     gel(LF,6) = mulcxpowIs(eps,k);
    8493             :   }
    8494         105 :   gel(LF,3) = mkvec2(gen_0, gen_1);
    8495         105 :   gel(LF,4) = gk;
    8496         105 :   gel(LF,5) = N;
    8497         105 :   if (lg(polar) == 1) setlg(LF,7); else gel(LF,7) = polar;
    8498         105 :   return LF;
    8499             : }
    8500             : static GEN
    8501          91 : mflfuncreateall(long sd, GEN mfa, GEN F, GEN vE, GEN gN, GEN gk)
    8502             : {
    8503          91 :   long i, l = lg(vE);
    8504          91 :   GEN L = cgetg(l, t_VEC);
    8505         196 :   for (i = 1; i < l; i++)
    8506         105 :     gel(L,i) = mflfuncreate(sd? gel(mfa,i): mfa, F, gel(vE,i), gN, gk);
    8507          91 :   return L;
    8508             : }
    8509             : GEN
    8510          42 : lfunmf(GEN mf, GEN F, long bitprec)
    8511             : {
    8512          42 :   pari_sp av = avma;
    8513          42 :   long i, l, prec = nbits2prec(bitprec);
    8514             :   GEN L, gk, gN;
    8515          42 :   mf = checkMF(mf);
    8516          42 :   gk = MF_get_gk(mf);
    8517          42 :   gN = MF_get_gN(mf);
    8518          42 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
    8519          42 :   if (F)
    8520             :   {
    8521             :     GEN v;
    8522          35 :     long s = MF_get_space(mf);
    8523          35 :     if (!checkmf_i(F)) pari_err_TYPE("lfunmf", F);
    8524          35 :     if (!mfisinspace_i(mf, F)) err_space(F);
    8525          35 :     L = NULL;
    8526          35 :     if ((s == mf_NEW || s == mf_CUSP || s == mf_FULL)
    8527          21 :         && gequal(mfcoefs_i(F,1,1), mkvec2(gen_0,gen_1)))
    8528             :     { /* check if eigenform */
    8529          14 :       GEN vP, vF, b = mftobasis_i(mf, F);
    8530          14 :       long lF, d = degpol(mf_get_field(F));
    8531          14 :       v = mfsplit(mf, d, 0);
    8532          14 :       vF = gel(v,1);
    8533          14 :       vP = gel(v,2); lF = lg(vF);
    8534          14 :       for (i = 1; i < lF; i++)
    8535          14 :         if (degpol(gel(vP,i)) == d && gequal(gel(vF,i), b))
    8536             :         {
    8537          14 :           GEN vE = mfgetembed(F, prec);
    8538          14 :           GEN Z = mffrickeeigen_i(mf, mkvec(b), mkvec(vE), prec);
    8539          14 :           L = mflfuncreateall(1, gel(Z,1), F, vE, gN, gk);
    8540          14 :           break;
    8541             :         }
    8542             :     }
    8543          35 :     if (!L)
    8544             :     { /* not an eigenform: costly general case */
    8545          21 :       GEN mfa = mfatkininit_i(mf, itou(gN), 1, prec);
    8546          21 :       L = mflfuncreateall(0,mfa, F, mfgetembed(F,prec), gN, gk);
    8547             :     }
    8548          35 :     if (lg(L) == 2) L = gel(L,1);
    8549             :   }
    8550             :   else
    8551             :   {
    8552           7 :     GEN M = mfeigenbasis(mf), vE = mfeigenembed(mf, prec);
    8553           7 :     GEN v = mffrickeeigen(mf, vE, prec);
    8554           7 :     l = lg(vE); L = cgetg(l, t_VEC);
    8555          63 :     for (i = 1; i < l; i++)
    8556          56 :       gel(L,i) = mflfuncreateall(1,gel(v,i), gel(M,i), gel(vE,i), gN, gk);
    8557             :   }
    8558          42 :   return gerepilecopy(av, L);
    8559             : }
    8560             : 
    8561             : GEN
    8562          21 : mffromell(GEN E)
    8563             : {
    8564          21 :   pari_sp av = avma;
    8565             :   GEN mf, F, z, v, S;
    8566             :   long N, i, l;
    8567             : 
    8568          21 :   checkell(E);
    8569          21 :   if (ell_get_type(E) != t_ELL_Q) pari_err_TYPE("mfffromell [E not over Q]", E);
    8570          21 :   N = itos(ellQ_get_N(E));
    8571          21 :   mf = mfinit_i(mkvec2(utoi(N), gen_2), mf_NEW);
    8572          21 :   v = split_i(mf, 1, 0);
    8573          21 :   S = gel(v,1); l = lg(S); /* rational newforms */
    8574          21 :   F = tag(t_MF_ELL, mkNK(N,2,mfchartrivial()), E);
    8575          21 :   z = mftobasis_i(mf, F);
    8576          21 :   for(i = 1; i < l; i++)
    8577          21 :     if (gequal(z, gel(S,i))) break;
    8578          21 :   if (i == l) pari_err_BUG("mffromell [E is not modular]");
    8579          21 :   return gerepilecopy(av, mkvec3(mf, F, z));
    8580             : }
    8581             : 
    8582             : /* returns -1 if not, degree otherwise */
    8583             : long
    8584          98 : polishomogeneous(GEN P)
    8585             : {
    8586             :   long i, D, l;
    8587          98 :   if (typ(P) != t_POL) return 0;
    8588          49 :   D = -1; l = lg(P);
    8589         231 :   for (i = 2; i < l; i++)
    8590             :   {
    8591         182 :     GEN c = gel(P,i);
    8592             :     long d;
    8593         182 :     if (gequal0(c)) continue;
    8594          84 :     d = polishomogeneous(c);
    8595          84 :     if (d < 0) return -1;
    8596          84 :     if (D < 0) D = d + i-2; else if (D != d + i-2) return -1;
    8597             :   }
    8598          49 :   return D;
    8599             : }
    8600             : 
    8601             : /* P a t_POL, 1 if spherical, 0 otherwise */
    8602             : static int
    8603          14 : RgX_isspherical(GEN Qi, GEN P)
    8604             : {
    8605          14 :   pari_sp av = avma;
    8606             :   GEN va, S;
    8607             :   long lva, i, j;
    8608          14 :   if (degpol(P) <= 1) return 1;
    8609          14 :   va = variables_vecsmall(P); lva = lg(va);
    8610          14 :   if (lva > lg(Qi)) pari_err(e_MISC, "too many variables in mffromqf");
    8611          14 :   S = gen_0;
    8612          42 :   for (j = 1; j < lva; j++)
    8613             :   {
    8614          28 :     GEN col = gel(Qi, j), Pj = deriv(P, va[j]);
    8615          70 :     for (i = 1; i <= j; i++)
    8616             :     {
    8617          42 :       GEN coe = gel(col, i);
    8618          42 :       if (i != j) coe = gmul2n(coe, 1);
    8619          42 :       if (!gequal0(coe)) S = gadd(S, gmul(coe, deriv(Pj, va[i])));
    8620             :     }
    8621             :   }
    8622          14 :   return gc_bool(av, gequal0(S));
    8623             : }
    8624             : 
    8625             : static GEN
    8626          28 : c_QFsimple_i(long n, GEN Q, GEN P)
    8627             : {
    8628          28 :   pari_sp av = avma;
    8629          28 :   GEN V, v = qfrep0(Q, utoi(n), 1);
    8630          28 :   long i, l = lg(v);
    8631          28 :   V = cgetg(l+1, t_VEC);
    8632          49 :   if (!P || equali1(P))
    8633             :   {
    8634          21 :     gel(V,1) = gen_1;
    8635          21 :     for (i = 2; i <= l; i++) gel(V,i) = utoi(v[i-1] << 1);
    8636             :   }
    8637             :   else
    8638             :   {
    8639           7 :     gel(V,1) = gcopy(P);
    8640           7 :     for (i = 2; i <= l; i++) gel(V,i) = gmulgs(P, v[i-1] << 1);
    8641             :   }
    8642          28 :   return gerepileupto(av, V);
    8643             : }
    8644             : static GEN
    8645          35 : c_QF_i(long n, GEN Q, GEN P)
    8646             : {
    8647          35 :   pari_sp av = avma;
    8648             :   GEN V, v, va;
    8649             :   long i, lva, lq, l;
    8650          35 :   if (!P || typ(P) != t_POL) return c_QFsimple_i(n, Q, P);
    8651           7 :   v = gel(minim(Q, utoi(2*n), NULL), 3);
    8652           7 :   va = variables_vec(P); lq = lg(Q) - 1; lva = lg(va) - 1;
    8653           7 :   V = zerovec(n + 1); l = lg(v);
    8654          35 :   for (i = 1; i < l; i++)
    8655             :   {
    8656          28 :     GEN X = gel(v,i);
    8657          28 :     long ind = (itos(qfeval0(Q, X, NULL)) >> 1) + 1;
    8658          28 :     if (lq > lva) X = vecslice(X, 1, lva);
    8659          28 :     gel(V, ind) = gadd(gel(V, ind), gsubstvec(P, va, X));
    8660             :   }
    8661           7 :   return gerepilecopy(av, gmul2n(V, 1));
    8662             : }
    8663             : 
    8664             : GEN
    8665          42 : mffromqf(GEN Q, GEN P)
    8666             : {
    8667          42 :   pari_sp av = avma;
    8668             :   GEN G, Qi, F, D, N, mf, v, gk, gwt, chi;
    8669             :   long m, d, space;
    8670          42 :   if (typ(Q) != t_MAT) pari_err_TYPE("mffromqf", Q);
    8671          42 :   if (!RgM_is_ZM(Q) || !qfiseven(Q))
    8672           0 :     pari_err_TYPE("mffromqf [not integral or even]", Q);
    8673          42 :   m = lg(Q)-1;
    8674          42 :   gk = sstoQ(m, 2);
    8675          42 :   Qi = ZM_inv(Q, &N);
    8676          42 :   if (!qfiseven(Qi)) N = shifti(N, 1);
    8677          42 :   d = 0;
    8678          42 :   if (!P || gequal1(P)) P = NULL;
    8679             :   else
    8680             :   {
    8681          21 :     P = simplify_shallow(P);
    8682          21 :     if (typ(P) == t_POL)
    8683             :     {
    8684          14 :       d = polishomogeneous(P);
    8685          14 :       if (d < 0) pari_err_TYPE("mffromqf [not homogeneous t_POL]", P);
    8686          14 :       if (!RgX_isspherical(Qi, P))
    8687           0 :         pari_err_TYPE("mffromqf [not a spherical t_POL]", P);
    8688             :     }
    8689             :   }
    8690          42 :   D = ZM_det(Q);
    8691          42 :   if (typ(gk) == t_INT) { if (mpodd(gk)) D = negi(D); } else D = shifti(D, 1);
    8692          42 :   space = d > 0 ? mf_CUSP : mf_FULL;
    8693          42 :   G = znstar0(N,1);
    8694          42 :   chi = mkvec2(G, znchar_quad(G,D));
    8695          42 :   gwt = gaddgs(gk, d);
    8696          42 :   mf = mfinit(mkvec3(N, gwt, chi), space);
    8697          42 :   if (odd(d))
    8698             :   {
    8699           7 :     F = mftrivial();
    8700           7 :     v = zerocol(MF_get_dim(mf));
    8701             :   }
    8702             :   else
    8703             :   {
    8704          35 :     F = c_QF_i(mfsturm(mf), Q, P);
    8705          35 :     v = mftobasis_i(mf, F);
    8706          35 :     F = mflinear(mf, v);
    8707             :   }
    8708          42 :   return gerepilecopy(av, mkvec3(mf, F, v));
    8709             : }
    8710             : 
    8711             : /***********************************************************************/
    8712             : /*                          Eisenstein Series                          */
    8713             : /***********************************************************************/
    8714             : /* \sigma_{k-1}(\chi,n) */
    8715             : static GEN
    8716       22092 : sigchi(long k, GEN CHI, long n)
    8717             : {
    8718       22092 :   pari_sp av = avma;
    8719       22092 :   GEN S = gen_1, D = mydivisorsu(u_ppo(n,mfcharmodulus(CHI)));
    8720       22092 :   long i, l = lg(D), ord = mfcharorder(CHI), vt = varn(mfcharpol(CHI));
    8721       77777 :   for (i = 2; i < l; i++) /* skip D[1] = 1 */
    8722             :   {
    8723       55685 :     long d = D[i], a = mfcharevalord(CHI, d, ord);
    8724       55685 :     S = gadd(S, mygmodulo_lift(a, ord, powuu(d, k-1), vt));
    8725             :   }
    8726       22092 :   return gerepileupto(av,S);
    8727             : }
    8728             : 
    8729             : /* write n = n0*n1*n2, (n0,N1*N2) = 1, n1 | N1^oo, n2 | N2^oo;
    8730             :  * return NULL if (n,N1,N2) > 1, else return factoru(n0) */
    8731             : static GEN
    8732      260071 : sigchi2_dec(long n, long N1, long N2, long *pn1, long *pn2)
    8733             : {
    8734      260071 :   GEN P0, E0, P, E, fa = myfactoru(n);
    8735             :   long i, j, l;
    8736      260071 :   *pn1 = 1;
    8737      260071 :   *pn2 = 1;
    8738      260071 :   if (N1 == 1 && N2 == 1) return fa;
    8739      246575 :   P = gel(fa,1); l = lg(P);
    8740      246575 :   E = gel(fa,2);
    8741      246575 :   P0 = cgetg(l, t_VECSMALL);
    8742      246575 :   E0 = cgetg(l, t_VECSMALL);
    8743      570745 :   for (i = j = 1; i < l; i++)
    8744             :   {
    8745      346248 :     long p = P[i], e = E[i];
    8746      346248 :     if (N1 % p == 0)
    8747             :     {
    8748       37653 :       if (N2 % p == 0) return NULL;
    8749       15575 :       *pn1 *= upowuu(p,e);
    8750             :     }
    8751      308595 :     else if (N2 % p == 0)
    8752       58835 :       *pn2 *= upowuu(p,e);
    8753      249760 :     else { P0[j] = p; E0[j] = e; j++; }
    8754             :   }
    8755      224497 :   setlg(P0, j);
    8756      224497 :   setlg(E0, j); return mkvec2(P0,E0);
    8757             : }
    8758             : 
    8759             : /* sigma_{k-1}(\chi_1,\chi_2,n), ord multiple of lcm(ord(CHI1),ord(CHI2)) */
    8760             : static GEN
    8761      212114 : sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord)
    8762             : {
    8763      212114 :   pari_sp av = avma;
    8764      212114 :   GEN S = gen_0, D;
    8765      212114 :   long i, l, n1, n2, vt, N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    8766      212114 :   D = sigchi2_dec(n, N1, N2, &n1, &n2); if (!D) { set_avma(av); return S; }
    8767      194404 :   D = divisorsu_fact(D); l = lg(D);
    8768      194404 :   vt = varn(mfcharpol(CHI1));
    8769      770329 :   for (i = 1; i < l; i++)
    8770             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8771      575925 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1; (n/d,N2) = 1 */
    8772      575925 :     a = mfcharevalord(CHI1, d, ord) + mfcharevalord(CHI2, nd, ord);
    8773      575925 :     if (a >= ord) a -= ord;
    8774      575925 :     S = gadd(S, mygmodulo_lift(a, ord, powuu(d, k-1), vt));
    8775             :   }
    8776      194404 :   return gerepileupto(av, S);
    8777             : }
    8778             : 
    8779             : /**************************************************************************/
    8780             : /**           Dirichlet characters with precomputed values               **/
    8781             : /**************************************************************************/
    8782             : /* CHI mfchar */
    8783             : static GEN
    8784       11354 : mfcharcxinit(GEN CHI, long prec)
    8785             : {
    8786       11354 :   GEN G = gel(CHI,1), chi = gel(CHI,2), z, V;
    8787       11354 :   GEN v = ncharvecexpo(G, znconrey_normalized(G,chi));
    8788       11354 :   long n, l = lg(v), o = mfcharorder(CHI);
    8789       11354 :   V = cgetg(l, t_VEC);
    8790       11354 :   z = grootsof1(o, prec); /* Mod(t, Phi_o(t)) -> e(1/o) */
    8791       11354 :   for (n = 1; n < l; n++) gel(V,n) = v[n] < 0? gen_0: gel(z, v[n]+1);
    8792       11354 :   return mkvecn(6, G, chi, gmfcharorder(CHI), v, V, mfcharpol(CHI));
    8793             : }
    8794             : /* v a "CHIvec" */
    8795             : static long
    8796    20066046 : CHIvec_N(GEN v) { return itou(znstar_get_N(gel(v,1))); }
    8797             : static GEN
    8798       11592 : CHIvec_CHI(GEN v)
    8799       11592 : { return mkvec4(gel(v,1), gel(v,2), gel(v,3), gel(v,6)); }
    8800             : /* character order */
    8801             : static long
    8802       32459 : CHIvec_ord(GEN v) { return itou(gel(v,3)); }
    8803             : /* character exponents, i.e. t such that chi(n) = e(t) */
    8804             : static GEN
    8805      333466 : CHIvec_expo(GEN v) { return gel(v,4); }
    8806             : /* character values chi(n) */
    8807             : static GEN
    8808    19558448 : CHIvec_val(GEN v) { return gel(v,5); }
    8809             : /* CHI(n) */
    8810             : static GEN
    8811    19550594 : mychareval(GEN v, long n)
    8812             : {
    8813    19550594 :   long N = CHIvec_N(v), ind = n%N;
    8814    19550594 :   if (ind <= 0) ind += N;
    8815    19550594 :   return gel(CHIvec_val(v), ind);
    8816             : }
    8817             : /* return c such that CHI(n) = e(c / ordz) or -1 if (n,N) > 1 */
    8818             : static long
    8819      333466 : mycharexpo(GEN v, long n)
    8820             : {
    8821      333466 :   long N = CHIvec_N(v), ind = n%N;
    8822      333466 :   if (ind <= 0) ind += N;
    8823      333466 :   return CHIvec_expo(v)[ind];
    8824             : }
    8825             : /* faster than mfcharparity */
    8826             : static long
    8827       37338 : CHIvec_parity(GEN v) { return mycharexpo(v,-1) ? -1: 1; }
    8828             : /**************************************************************************/
    8829             : 
    8830             : static ulong
    8831       47957 : sigchi2_Fl(long k, GEN CHI1vec, GEN CHI2vec, long n, GEN vz, ulong p)
    8832             : {
    8833       47957 :   pari_sp av = avma;
    8834       47957 :   long ordz = lg(vz)-2, i, l, n1, n2;
    8835       47957 :   ulong S = 0;
    8836       47957 :   GEN D = sigchi2_dec(n, CHIvec_N(CHI1vec), CHIvec_N(CHI2vec), &n1, &n2);
    8837       47957 :   if (!D) return gc_ulong(av,S);
    8838       43589 :   D = divisorsu_fact(D);
    8839       43589 :   l = lg(D);
    8840      147294 :   for (i = 1; i < l; i++)
    8841             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8842      103705 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1, (n/d,N2)=1 */
    8843      103705 :     a = mycharexpo(CHI2vec, nd) + mycharexpo(CHI1vec, d);
    8844      103705 :     if (a >= ordz) a -= ordz;
    8845      103705 :     S = Fl_add(S, mygmodulo_Fl(a, vz, Fl_powu(d,k-1,p), p), p);
    8846             :   }
    8847       43589 :   return gc_ulong(av,S);
    8848             : }
    8849             : 
    8850             : /**********************************************************************/
    8851             : /* Fourier expansions of Eisenstein series                            */
    8852             : /**********************************************************************/
    8853             : /* L(CHI,0) / 2, order(CHI) | ord != 0 */
    8854             : static GEN
    8855        1701 : charLFwt1(GEN CHI, long ord)
    8856             : {
    8857             :   GEN S;
    8858        1701 :   long r, vt, m = mfcharmodulus(CHI);
    8859             : 
    8860        1701 :   if (m == 1) return mkfrac(gen_m1,stoi(4));
    8861        1701 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8862       51723 :   for (r = 1; r < m; r++)
    8863             :   { /* S += r*chi(r) */
    8864             :     long a;
    8865       50022 :     if (ugcd(m,r) != 1) continue;
    8866       38318 :     a = mfcharevalord(CHI,r,ord);
    8867       38318 :     S = gadd(S, mygmodulo_lift(a, ord, utoi(r), vt));
    8868             :   }
    8869        1701 :   return gdivgs(S, -2*m);
    8870             : }
    8871             : /* L(CHI,0) / 2, mod p */
    8872             : static ulong
    8873        1323 : charLFwt1_Fl(GEN CHIvec, GEN vz, ulong p)
    8874             : {
    8875        1323 :   long r, m = CHIvec_N(CHIvec);
    8876             :   ulong S;
    8877        1323 :   if (m == 1) return Rg_to_Fl(mkfrac(gen_m1,stoi(4)), p);
    8878        1323 :   S = 0;
    8879       64659 :   for (r = 1; r < m; r++)
    8880             :   { /* S += r*chi(r) */
    8881       63336 :     long a = mycharexpo(CHIvec,r);
    8882       63336 :     if (a < 0) continue;
    8883       61166 :     S = Fl_add(S, mygmodulo_Fl(a, vz, r, p), p);
    8884             :   }
    8885        1323 :   return Fl_div(Fl_neg(S,p), 2*m, p);
    8886             : }
    8887             : /* L(CHI,1-k) / 2, order(CHI) | ord != 0 */
    8888             : static GEN
    8889        1414 : charLFwtk(long k, GEN CHI, long ord)
    8890             : {
    8891             :   GEN S, P, dS;
    8892             :   long r, m, vt;
    8893             : 
    8894        1414 :   if (k == 1) return charLFwt1(CHI, ord);
    8895        1414 :   m = mfcharmodulus(CHI);
    8896        1414 :   if (m == 1) return gdivgs(bernfrac(k),-2*k);
    8897         819 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8898         819 :   P = ZX_rescale(Q_remove_denom(bernpol(k,0), &dS), utoi(m));
    8899         819 :   dS = mul_denom(dS, stoi(-2*m*k));
    8900       10703 :   for (r = 1; r < m; r++)
    8901             :   { /* S += P(r)*chi(r) */
    8902             :     long a;
    8903        9884 :     if (ugcd(r,m) != 1) continue;
    8904        8106 :     a = mfcharevalord(CHI,r,ord);
    8905        8106 :     S = gadd(S, mygmodulo_lift(a, ord, poleval(P, utoi(r)), vt));
    8906             :   }
    8907         819 :   return gdiv(S, dS);
    8908             : }
    8909             : /* L(CHI,1-k) / 2, mod p */
    8910             : static ulong
    8911        1988 : charLFwtk_Fl(long k, GEN CHIvec, GEN vz, ulong p)
    8912             : {
    8913             :   GEN P;
    8914             :   long r, m;
    8915             :   ulong S;
    8916        1988 :   if (k == 1) return charLFwt1_Fl(CHIvec, vz, p);
    8917         665 :   m = CHIvec_N(CHIvec);
    8918         665 :   if (m == 1) return Rg_to_Fl(gdivgs(bernfrac(k),-2*k), p);
    8919         399 :   S = 0;
    8920         399 :   P = RgX_to_Flx(RgX_rescale(bernpol(k,0), utoi(m)), p);
    8921        8085 :   for (r = 1; r < m; r++)
    8922             :   { /* S += P(r)*chi(r) */
    8923        7686 :     long a = mycharexpo(CHIvec,r);
    8924        7686 :     if (a < 0) continue;
    8925        6566 :     S = Fl_add(S, mygmodulo_Fl(a, vz, Flx_eval(P,r,p), p), p);
    8926             :   }
    8927         399 :   return Fl_div(Fl_neg(S,p), 2*k*m, p);
    8928             : }
    8929             : 
    8930             : static GEN
    8931        5754 : mfeisenstein2_0(long k, GEN CHI1, GEN CHI2, long ord)
    8932             : {
    8933        5754 :   if (k == 1 && mfcharistrivial(CHI1))
    8934        1701 :     return charLFwt1(CHI2, ord);
    8935        4053 :   else if (mfcharistrivial(CHI2))
    8936        1239 :     return charLFwtk(k, CHI1, ord);
    8937        2814 :   else return gen_0;
    8938             : }
    8939             : static ulong
    8940        3290 : mfeisenstein2_0_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p)
    8941             : {
    8942        3290 :   if (k == 1 && CHIvec_ord(CHI1vec) == 1)
    8943        1323 :     return charLFwtk_Fl(k, CHI2vec, vz, p);
    8944        1967 :   else if (CHIvec_ord(CHI2vec) == 1)
    8945         665 :     return charLFwtk_Fl(k, CHI1vec, vz, p);
    8946        1302 :   else return 0;
    8947             : }
    8948             : static GEN
    8949          84 : NK_eisen2(long k, GEN CHI1, GEN CHI2)
    8950             : {
    8951          84 :   long N = mfcharmodulus(CHI1)*mfcharmodulus(CHI2);
    8952          84 :   return mkNK(N, k, mfcharmul(CHI1,CHI2));
    8953             : }
    8954             : static GEN
    8955         287 : mfeisenstein_i(long k, GEN CHI1, GEN CHI2)
    8956             : {
    8957         287 :   long s = 1, ord, vt;
    8958             :   GEN E0, NK, vchi, T;
    8959         287 :   if (CHI2) { CHI2 = get_mfchar(CHI2); if (mfcharparity(CHI2) < 0) s = -s; }
    8960         287 :   if (CHI1) { CHI1 = get_mfchar(CHI1); if (mfcharparity(CHI1) < 0) s = -s; }
    8961         273 :   if (s != m1pk(k)) return mftrivial();
    8962         252 :   if (!CHI1) CHI1 = mfchartrivial();
    8963         252 :   if (!CHI2)
    8964             :   { /* E_k(chi1) */
    8965         168 :     vt = varn(mfcharpol(CHI1));
    8966         168 :     ord = mfcharorder(CHI1);
    8967         168 :     NK = mkNK(mfcharmodulus(CHI1), k, CHI1);
    8968         168 :     E0 = charLFwtk(k, CHI1, ord);
    8969         168 :     vchi = mkvec3(E0, mkvec(mfcharpol(CHI1)), CHI1);
    8970         168 :     return tag(t_MF_EISEN, NK, vchi);
    8971             :   }
    8972             :   /* E_k(chi1,chi2) */
    8973          84 :   vt = varn(mfcharpol(CHI1));
    8974          84 :   NK = NK_eisen2(k, CHI1, CHI2);
    8975          84 :   ord = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    8976          84 :   E0 = mfeisenstein2_0(k, CHI1, CHI2, ord);
    8977          84 :   T = mkvec(polcyclo(ord, vt));
    8978          84 :   vchi = mkvec4(E0, T, CHI1, CHI2);
    8979          84 :   return tag2(t_MF_EISEN, NK, vchi, mkvecsmall2(ord,0));
    8980             : }
    8981             : GEN
    8982         287 : mfeisenstein(long k, GEN CHI1, GEN CHI2)
    8983             : {
    8984         287 :   pari_sp av = avma;
    8985         287 :   if (k < 1) pari_err_DOMAIN("mfeisenstein", "k", "<", gen_1, stoi(k));
    8986         287 :   return gerepilecopy(av, mfeisenstein_i(k, CHI1, CHI2));
    8987             : }
    8988             : 
    8989             : static GEN
    8990        1351 : mfeisenstein2all(long N0, GEN NK, long k, GEN CHI1, GEN CHI2, GEN T, long o)
    8991             : {
    8992        1351 :   GEN E, E0 = mfeisenstein2_0(k, CHI1,CHI2, o), vchi = mkvec4(E0, T, CHI1,CHI2);
    8993        1351 :   long j, d = (lg(T)==4)? itou(gmael(T,3,1)): 1;
    8994        1351 :   E = cgetg(d+1, t_VEC);
    8995        1351 :   for (j=1; j<=d; j++) gel(E,j) = tag2(t_MF_EISEN, NK,vchi,mkvecsmall2(o,j-1));
    8996        1351 :   return mfbdall(E, N0 / mf_get_N(gel(E,1)));
    8997             : }
    8998             : 
    8999             : static GEN
    9000         609 : zncharsG(GEN G)
    9001             : {
    9002         609 :   long i, l, N = itou(znstar_get_N(G));
    9003             :   GEN vCHI, V;
    9004         609 :   if (N == 1) return mkvec2(gen_1,cgetg(1,t_COL));
    9005         609 :   vCHI = const_vec(N,NULL);
    9006         609 :   V = cyc2elts(znstar_get_conreycyc(G));
    9007         609 :   l = lg(V);
    9008       23037 :   for (i = 1; i < l; i++)
    9009             :   {
    9010       22428 :     GEN chi0, chi = zc_to_ZC(gel(V,i)), n, F;
    9011       22428 :     F = znconreyconductor(G, chi, &chi0);
    9012       22428 :     if (typ(F) != t_INT) F = gel(F,1);
    9013       22428 :     n = znconreyexp(G, chi);
    9014       22428 :     gel(vCHI, itos(n)) = mkvec2(F, chi0);
    9015             :   }
    9016         609 :   return vCHI;
    9017             : }
    9018             : 
    9019             : /* CHI primitive, f(CHI) | N. Return pairs (CHI1,CHI2) both primitive
    9020             :  * such that f(CHI1)*f(CHI2) | N and CHI1 * CHI2 = CHI;
    9021             :  * if k = 1, CHI1 is even; if k = 2, omit (1,1) if CHI = 1 */
    9022             : static GEN
    9023         805 : mfeisensteinbasis_i(long N0, long k, GEN CHI)
    9024             : {
    9025         805 :   GEN G = gel(CHI,1), chi = gel(CHI,2), vT = const_vec(myeulerphiu(N0), NULL);
    9026             :   GEN CHI0, GN, chiN, Lchi, LG, V, RES, NK, T;
    9027         805 :   long i, j, l, n, n1, N, ord = mfcharorder(CHI);
    9028         805 :   long F = mfcharmodulus(CHI), vt = varn(mfcharpol(CHI));
    9029             : 
    9030         805 :   CHI0 = (F == 1)? CHI: mfchartrivial();
    9031         805 :   j = 1; RES = cgetg(N0+1, t_VEC);
    9032         805 :   T = gel(vT,ord) = Qab_trace_init(polcyclo(ord,vt), ord, ord);
    9033         805 :   if (F != 1 || k != 2)
    9034             :   { /* N1 = 1 */
    9035         679 :     NK = mkNK(F, k, CHI);
    9036         679 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI0, CHI, T, ord);
    9037         679 :     if (F != 1 && k != 1)
    9038         203 :       gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI, CHI0, T, ord);
    9039             :   }
    9040         805 :   if (N0 == 1) { setlg(RES,j); return RES; }
    9041         735 :   GN = G; chiN = chi;
    9042         735 :   if (F == N0) N = N0;
    9043             :   else
    9044             :   {
    9045         441 :     GEN faN = myfactoru(N0), P = gel(faN,1), E = gel(faN,2);
    9046         441 :     long lP = lg(P);
    9047        1134 :     for (i = N = 1; i < lP; i++)
    9048             :     {
    9049         693 :       long p = P[i];
    9050         693 :       N *= upowuu(p, maxuu(E[i]/2, z_lval(F,p)));
    9051             :     }
    9052         441 :     if ((N & 3) == 2) N >>= 1;
    9053         441 :     if (N == 1) { setlg(RES,j); return RES; }
    9054         315 :     if (F != N)
    9055             :     {
    9056          98 :       GN = znstar0(utoipos(N),1);
    9057          98 :       chiN = zncharinduce(G, chi, GN);
    9058             :     }
    9059             :   }
    9060         609 :   LG = const_vec(N, NULL); /* LG[d] = znstar(d,1) or NULL */
    9061         609 :   gel(LG,1) = gel(CHI0,1);
    9062         609 :   gel(LG,F) = G;
    9063         609 :   gel(LG,N) = GN;
    9064         609 :   Lchi = coprimes_zv(N);
    9065         609 :   n = itou(znconreyexp(GN,chiN));
    9066         609 :   V = zncharsG(GN); l = lg(V);
    9067       30002 :   for (n1 = 2; n1 < l; n1++) /* skip 1 (trivial char) */
    9068             :   {
    9069       29393 :     GEN v = gel(V,n1), w, chi1, chi2, G1, G2, CHI1, CHI2;
    9070             :     long N12, N1, N2, no, o12, t, m;
    9071       29393 :     if (!Lchi[n1]) continue;
    9072       21770 :     chi1 = gel(v,2); N1 = itou(gel(v,1)); /* conductor of chi1 */
    9073       21770 :     w = gel(V, Fl_div(n,n1,N));
    9074       21770 :     chi2 = gel(w,2); N2 = itou(gel(w,1)); /* conductor of chi2 */
    9075       21770 :     N12 = N1 * N2;
    9076       21770 :     if (N2 == 1 || N0 % N12) continue;
    9077             : 
    9078         658 :     G1 = gel(LG,N1); if (!G1) gel(LG,N1) = G1 = znstar0(utoipos(N1), 1);
    9079         658 :     if (k == 1 && zncharisodd(G1,chi1)) continue;
    9080         469 :     G2 = gel(LG,N2); if (!G2) gel(LG,N2) = G2 = znstar0(utoipos(N2), 1);
    9081         469 :     CHI1 = mfcharGL(G1, chi1);
    9082         469 :     CHI2 = mfcharGL(G2, chi2);
    9083         469 :     o12 = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    9084             :     /* remove Galois orbit: same trace */
    9085         469 :     no = Fl_powu(n1, ord, N);
    9086         763 :     for (t = 1+ord, m = n1; t <= o12; t += ord)
    9087             :     { /* m <-> CHI1^t, if t in Gal(Q(chi1,chi2)/Q), omit (CHI1^t,CHI2^t) */
    9088         294 :       m = Fl_mul(m, no, N); if (!m) break;
    9089         294 :       if (ugcd(t, o12) == 1) Lchi[m] = 0;
    9090             :     }
    9091         469 :     T = gel(vT,o12);
    9092         469 :     if (!T) T = gel(vT,o12) = Qab_trace_init(polcyclo(o12,vt), o12, ord);
    9093         469 :     NK = mkNK(N12, k, CHI);
    9094         469 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI1, CHI2, T, o12);
    9095             :   }
    9096         609 :   setlg(RES,j); return RES;
    9097             : }
    9098             : 
    9099             : static GEN
    9100         616 : mfbd_E2(GEN E2, long d, GEN CHI)
    9101             : {
    9102         616 :   GEN E2d = mfbd_i(E2, d);
    9103         616 :   GEN F = mkvec2(E2, E2d), L = mkvec2(gen_1, utoineg(d));
    9104             :   /* cannot use mflinear_i: E2 and E2d do not have the same level */
    9105         616 :   return tag3(t_MF_LINEAR, mkNK(d,2,CHI), F, L, gen_1);
    9106             : }
    9107             : /* C-basis of E_k(Gamma_0(N),chi). If k = 1, the first basis element must not
    9108             :  * vanish at oo [used in mfwt1basis]. Here E_1(CHI), whose q^0 coefficient
    9109             :  * does not vanish (since L(CHI,0) does not) *if* CHI is not trivial; which
    9110             :  * must be the case in weight 1.
    9111             :  *
    9112             :  * (k>=3): In weight k >= 3, basis is B(d) E(CHI1,(CHI/CHI1)_prim), where
    9113             :  * CHI1 is primitive modulo N1, and if N2 is the conductor of CHI/CHI1
    9114             :  * then d*N1*N2 | N.
    9115             :  * (k=2): In weight k=2, same if CHI is nontrivial. If CHI is trivial, must
    9116             :  * not take CHI1 trivial, and must add E_2(tau)-dE_2(d tau)), where
    9117             :  * d|N, d > 1.
    9118             :  * (k=1): In weight k=1, same as k >= 3 except that we restrict to CHI1 even */
    9119             : static GEN
    9120         833 : mfeisensteinbasis(long N, long k, GEN CHI)
    9121             : {
    9122             :   long i, F;
    9123             :   GEN L;
    9124         833 :   if (badchar(N, k, CHI)) return cgetg(1, t_VEC);
    9125         833 :   if (k == 0) return mfcharistrivial(CHI)? mkvec(mf1()): cgetg(1, t_VEC);
    9126         805 :   CHI = mfchartoprimitive(CHI, &F);
    9127         805 :   L = mfeisensteinbasis_i(N, k, CHI);
    9128         805 :   if (F == 1 && k == 2)
    9129             :   {
    9130         126 :     GEN v, E2 = mfeisenstein(2, NULL, NULL), D = mydivisorsu(N);
    9131         126 :     long nD = lg(D)-1;
    9132         126 :     v = cgetg(nD, t_VEC); L = vec_append(L,v);
    9133         126 :     for (i = 1; i < nD; i++) gel(v,i) = mfbd_E2(E2, D[i+1], CHI);
    9134             :   }
    9135         805 :   return lg(L) == 1? L: shallowconcat1(L);
    9136             : }
    9137             : 
    9138             : static GEN
    9139          70 : not_in_space(GEN F, long flag)
    9140             : {
    9141          70 :   if (!flag) err_space(F);
    9142          63 :   return cgetg(1, t_COL);
    9143             : }
    9144             : /* when flag set, no error */
    9145             : GEN
    9146         805 : mftobasis(GEN mf, GEN F, long flag)
    9147             : {
    9148         805 :   pari_sp av2, av = avma;
    9149             :   GEN G, v, y, gk;
    9150         805 :   long N, B, ismf = checkmf_i(F);
    9151             : 
    9152         805 :   mf = checkMF(mf);
    9153         805 :   if (ismf)
    9154             :   {
    9155         714 :     if (mfistrivial(F)) return zerocol(MF_get_dim(mf));
    9156         707 :     if (!mf_same_k(mf, F) || !mf_same_CHI(mf, F)) return not_in_space(F, flag);
    9157             :   }
    9158         756 :   N = MF_get_N(mf);
    9159         756 :   gk = MF_get_gk(mf);
    9160         756 :   if (ismf)
    9161             :   {
    9162         665 :     long NF = mf_get_N(F);
    9163         665 :     B = maxuu(mfsturmNgk(NF,gk), mfsturmNgk(N,gk)) + 1;
    9164         665 :     v = mfcoefs_i(F,B,1);
    9165             :   }
    9166             :   else
    9167             :   {
    9168          91 :     B = mfsturmNgk(N, gk) + 1;
    9169          91 :     switch(typ(F))
    9170             :     { /* F(0),...,F(lg(v)-2) */
    9171          63 :       case t_SER: v = sertocol(F); settyp(v,t_VEC); break;
    9172          14 :       case t_VEC: v = F; break;
    9173           7 :       case t_COL: v = shallowtrans(F); break;
    9174           7 :       default: pari_err_TYPE("mftobasis",F);
    9175             :                v = NULL;/*LCOV_EXCL_LINE*/
    9176             :     }
    9177          84 :     if (flag) B = minss(B, lg(v)-2);
    9178             :   }
    9179         749 :   y = mftobasis_i(mf, v);
    9180         749 :   if (typ(y) == t_VEC)
    9181             :   {
    9182          21 :     if (flag) return gerepilecopy(av, y);
    9183           0 :     pari_err(e_MISC, "not enough coefficients in mftobasis");
    9184             :   }
    9185         728 :   av2 = avma;
    9186         728 :   if (MF_get_space(mf) == mf_FULL || mfsturm(mf)+1 == B) return y;
    9187         210 :   G = mflinear(mf, y);
    9188         210 :   if (!gequal(v, mfcoefs_i(G, lg(v)-2,1))) y = NULL;
    9189         210 :   if (!y) { set_avma(av); return not_in_space(F, flag); }
    9190         182 :   set_avma(av2); return gerepileupto(av, y);
    9191             : }
    9192             : 
    9193             : /* assume N > 0; first cusp is always 0 */
    9194             : static GEN
    9195          49 : mfcusps_i(long N)
    9196             : {
    9197             :   long i, c, l;
    9198             :   GEN D, v;
    9199             : 
    9200          49 :   if (N == 1) return mkvec(gen_0);
    9201          49 :   D = mydivisorsu(N); l = lg(D); /* left on stack */
    9202          49 :   c = mfnumcuspsu_fact(myfactoru(N));
    9203          49 :   v = cgetg(c + 1, t_VEC);
    9204         350 :   for (i = c = 1; i < l; i++)
    9205             :   {
    9206         301 :     long C = D[i], NC = D[l-i], lima = ugcd(C, NC), A0, A;
    9207         889 :     for (A0 = 0; A0 < lima; A0++)
    9208         588 :       if (ugcd(A0, lima) == 1)
    9209             :       {
    9210         392 :         A = A0; while (ugcd(A,C) > 1) A += lima;
    9211         392 :         gel(v, c++) = sstoQ(A, C);
    9212             :       }
    9213             :   }
    9214          49 :   return v;
    9215             : }
    9216             : /* List of cusps of Gamma_0(N) */
    9217             : GEN
    9218          28 : mfcusps(GEN gN)
    9219             : {
    9220             :   long N;
    9221             :   GEN mf;
    9222          28 :   if (typ(gN) == t_INT) N = itos(gN);
    9223          14 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    9224           0 :   else { pari_err_TYPE("mfcusps", gN); N = 0; }
    9225          28 :   if (N <= 0) pari_err_DOMAIN("mfcusps", "N", "<=", gen_0, stoi(N));
    9226          28 :   return mfcusps_i(N);
    9227             : }
    9228             : 
    9229             : long
    9230         315 : mfcuspisregular(GEN NK, GEN cusp)
    9231             : {
    9232             :   long v, N, dk, nk, t, o;
    9233             :   GEN mf, CHI, go, A, C, g, c, d;
    9234         315 :   if ((mf = checkMF_i(NK)))
    9235             :   {
    9236          49 :     GEN gk = MF_get_gk(mf);
    9237          49 :     N = MF_get_N(mf);
    9238          49 :     CHI = MF_get_CHI(mf);
    9239          49 :     Qtoss(gk, &nk, &dk);
    9240             :   }
    9241             :   else
    9242         266 :     checkNK2(NK, &N, &nk, &dk, &CHI, 0);
    9243         315 :   if (typ(cusp) == t_INFINITY) return 1;
    9244         315 :   if (typ(cusp) == t_FRAC) { A = gel(cusp,1); C = gel(cusp,2); }
    9245          28 :   else { A = cusp; C = gen_1; }
    9246         315 :   g = diviuexact(mului(N,C), ugcd(N, Fl_sqr(umodiu(C,N), N)));
    9247         315 :   c = mulii(negi(C),g);
    9248         315 :   d = addiu(mulii(A,g), 1);
    9249         315 :   if (!CHI) return 1;
    9250         315 :   go = gmfcharorder(CHI);
    9251         315 :   v = vali(go); if (v < 2) go = shifti(go, 2-v);
    9252         315 :   t = itou( znchareval(gel(CHI,1), gel(CHI,2), d, go) );
    9253         315 :   if (dk == 1) return t == 0;
    9254         154 :   o = itou(go);
    9255         154 :   if (kronecker(c,d) < 0) t = Fl_add(t, o/2, o);
    9256         154 :   if (Mod4(d) == 1) return t == 0;
    9257          14 :   t = Fl_sub(t, Fl_mul(o/4, nk, o), o);
    9258          14 :   return t == 0;
    9259             : }
    9260             : 
    9261             : /* Some useful closures */
    9262             : 
    9263             : /* sum_{d|n} d^k */
    9264             : static GEN
    9265       16464 : mysumdivku(ulong n, ulong k)
    9266             : {
    9267       16464 :   GEN fa = myfactoru(n);
    9268       16464 :   return k == 1? usumdiv_fact(fa): usumdivk_fact(fa,k);
    9269             : }
    9270             : static GEN
    9271         658 : c_Ek(long n, long d, GEN F)
    9272             : {
    9273         658 :   GEN E = cgetg(n + 2, t_VEC), C = gel(F,2);
    9274         658 :   long i, k = mf_get_k(F);
    9275         658 :   gel (E, 1) = gen_1;
    9276        8260 :   for (i = 1; i <= n; i++)
    9277             :   {
    9278        7602 :     pari_sp av = avma;
    9279        7602 :     gel(E, i+1) = gerepileupto(av, gmul(C, mysumdivku(i*d, k-1)));
    9280             :   }
    9281         658 :   return E;
    9282             : }
    9283             : 
    9284             : GEN
    9285         189 : mfEk(long k)
    9286             : {
    9287         189 :   pari_sp av = avma;
    9288             :   GEN E0, NK;
    9289         189 :   if (k < 0 || odd(k)) pari_err_TYPE("mfEk [incorrect k]", stoi(k));
    9290         189 :   if (!k) return mf1();
    9291         182 :   E0 = gdivsg(-2*k, bernfrac(k));
    9292         182 :   NK = mkNK(1,k,mfchartrivial());
    9293         182 :   return gerepilecopy(av, tag(t_MF_Ek, NK, E0));
    9294             : }
    9295             : 
    9296             : GEN
    9297          49 : mfDelta(void)
    9298             : {
    9299          49 :   pari_sp av = avma;
    9300          49 :   return gerepilecopy(av, tag0(t_MF_DELTA, mkNK(1,12,mfchartrivial())));
    9301             : }
    9302             : 
    9303             : GEN
    9304         504 : mfTheta(GEN psi)
    9305             : {
    9306         504 :   pari_sp av = avma;
    9307             :   GEN N, gk, psi2;
    9308             :   long par;
    9309         504 :   if (!psi) { psi = mfchartrivial(); N = utoipos(4); par = 1; }
    9310             :   else
    9311             :   {
    9312             :     long FC;
    9313          21 :     psi = get_mfchar(psi);
    9314          21 :     FC = mfcharconductor(psi);
    9315          21 :     if (mfcharmodulus(psi) != FC)
    9316           0 :       pari_err_TYPE("mfTheta [nonprimitive character]", psi);
    9317          21 :     par = mfcharparity(psi);
    9318          21 :     N = shifti(sqru(FC),2);
    9319             :   }
    9320         504 :   if (par > 0) { gk = ghalf; psi2 = psi; }
    9321           7 :   else { gk = gsubsg(2, ghalf); psi2 = mfcharmul(psi, get_mfchar(stoi(-4))); }
    9322         504 :   return gerepilecopy(av, tag(t_MF_THETA, mkgNK(N, gk, psi2, pol_x(1)), psi));
    9323             : }
    9324             : 
    9325             : /* Output 0 if not desired eta product: if flag=0 (default) require
    9326             :  * holomorphic at cusps. If flag set, accept meromorphic, but sill in some
    9327             :  * modular function space */
    9328             : GEN
    9329         140 : mffrometaquo(GEN eta, long flag)
    9330             : {
    9331         140 :   pari_sp av = avma;
    9332             :   GEN NK, N, k, BR, P;
    9333         140 :   long v, cusp = 0;
    9334         140 :   if (!etaquotype(&eta, &N,&k,&P, &v, NULL, flag? NULL: &cusp) || cusp < 0)
    9335             :   {
    9336          14 :     set_avma(av); return gen_0;
    9337             :   }
    9338         126 :   if (lg(gel(eta,1)) == 1) { set_avma(av); return mf1(); }
    9339         119 :   BR = mkvec2(ZV_to_zv(gel(eta,1)), ZV_to_zv(gel(eta,2)));
    9340         119 :   if (v < 0) v = 0;
    9341         119 :   NK = mkgNK(N, k, get_mfchar(P), pol_x(1));
    9342         119 :   return gerepilecopy(av, tag2(t_MF_ETAQUO, NK, BR, utoi(v)));
    9343             : }
    9344             : 
    9345             : #if 0
    9346             : /* number of primitive characters modulo N */
    9347             : static ulong
    9348             : numprimchars(ulong N)
    9349             : {
    9350             :   GEN fa, P, E;
    9351             :   long i, l;
    9352             :   ulong n;
    9353             :   if ((N & 3) == 2) return 0;
    9354             :   fa = myfactoru(N);
    9355             :   P = gel(fa,1); l = lg(P);
    9356             :   E = gel(fa,2);
    9357             :   for (i = n = 1; i < l; i++)
    9358             :   {
    9359             :     ulong p = P[i], e = E[i];
    9360             :     if (e == 2) n *= p-2; else n *= (p-1)*(p-1)*upowuu(p,e-2);
    9361             :   }
    9362             :   return n;
    9363             : }
    9364             : #endif
    9365             : 
    9366             : /* Space generated by products of two Eisenstein series */
    9367             : 
    9368             : INLINE int
    9369      112483 : cmp_small(long a, long b) { return a>b? 1: (a<b? -1: 0); }
    9370             : static int
    9371       62657 : cmp_small_priority(void *E, GEN a, GEN b)
    9372             : {
    9373       62657 :   GEN prio = (GEN)E;
    9374       62657 :   return cmp_small(prio[(long)a], prio[(long)b]);
    9375             : }
    9376             : static long
    9377         938 : znstar_get_expo(GEN G)
    9378             : {
    9379         938 :   GEN cyc = znstar_get_cyc(G);
    9380         938 :   return (lg(cyc) == 1)? 1: itou(gel(cyc,1));
    9381             : }
    9382             : 
    9383             : /* Return [vchi, bymod, vG]:
    9384             :  * vG[f] = znstar(f,1) for f a conductor of (at least) a char mod N; else NULL
    9385             :  * bymod[f] = vecsmall of conrey indexes of chars modulo f | N; else NULL
    9386             :  * vchi[n] = a list of CHIvec [G0,chi0,o,ncharvecexpo(G0,nchi0),...]:
    9387             :  *   chi0 = primitive char attached to Conrey Mod(n,N)
    9388             :  * (resp. NULL if (n,N) > 1) */
    9389             : static GEN
    9390         469 : charsmodN(long N)
    9391             : {
    9392         469 :   GEN D, G, prio, phio, dummy = cgetg(1,t_VEC);
    9393         469 :   GEN vP, vG = const_vec(N,NULL), vCHI  = const_vec(N,NULL);
    9394         469 :   GEN bymod = const_vec(N,NULL);
    9395         469 :   long pn, i, l, vt = fetch_user_var("t");
    9396         469 :   D = mydivisorsu(N); l = lg(D);
    9397        3059 :   for (i = 1; i < l; i++)
    9398        2590 :     gel(bymod, D[i]) = vecsmalltrunc_init(myeulerphiu(D[i])+1);
    9399         469 :   gel(vG,N) = G = znstar0(utoipos(N),1);
    9400         469 :   pn = znstar_get_expo(G);  /* exponent(Z/NZ)^* */
    9401         469 :   vP = const_vec(pn,NULL);
    9402       22456 :   for (i = 1; i <= N; i++)
    9403             :   {
    9404             :     GEN P, gF, G0, chi0, nchi0, chi, v, go;
    9405             :     long j, F, o;
    9406       21987 :     if (ugcd(i,N) != 1) continue;
    9407       11067 :     chi = znconreylog(G, utoipos(i));
    9408       11067 :     gF = znconreyconductor(G, chi, &chi0);
    9409       11067 :     F = (typ(gF) == t_INT)? itou(gF): itou(gel(gF,1));
    9410       11067 :     G0 = gel(vG, F); if (!G0) G0 = gel(vG,F) = znstar0(gF, 1);
    9411       11067 :     nchi0 = znconreylog_normalize(G0,chi0);
    9412       11067 :     go = gel(nchi0,1); o = itou(go); /* order(chi0) */
    9413       11067 :     v = ncharvecexpo(G0, nchi0);
    9414       11067 :     if (!equaliu(go, pn)) v = zv_z_mul(v, pn / o);
    9415       11067 :     P = gel(vP, o); if (!P) P = gel(vP,o) = polcyclo(o,vt);
    9416             :     /* mfcharcxinit with dummy complex powers */
    9417       11067 :     gel(vCHI,i) = mkvecn(6, G0, chi0, go, v, dummy, P);
    9418       11067 :     D = mydivisorsu(N / F); l = lg(D);
    9419       11067 :     for (j = 1; j < l; j++) vecsmalltrunc_append(gel(bymod, F*D[j]), i);
    9420             :   }
    9421         469 :   phio = zero_zv(pn); l = lg(vCHI); prio = cgetg(l, t_VEC);
    9422       22456 :   for (i = 1; i < l; i++)
    9423             :   {
    9424       21987 :     GEN CHI = gel(vCHI,i);
    9425             :     long o;
    9426       21987 :     if (!CHI) continue;
    9427       11067 :     o = CHIvec_ord(CHI);
    9428       11067 :     if (!phio[o]) phio[o] = myeulerphiu(o);
    9429       11067 :     prio[i] = phio[o];
    9430             :   }
    9431         469 :   l = lg(bymod);
    9432             :   /* sort characters by increasing value of phi(order) */
    9433       22456 :   for (i = 1; i < l; i++)
    9434             :   {
    9435       21987 :     GEN z = gel(bymod,i);
    9436       21987 :     if (z) gen_sort_inplace(z, (void*)prio, &cmp_small_priority, NULL);
    9437             :   }
    9438         469 :   return mkvec3(vCHI, bymod, vG);
    9439             : }
    9440             : 
    9441             : static GEN
    9442        4319 : mfeisenstein2pure(long k, GEN CHI1, GEN CHI2, long ord, GEN P, long lim)
    9443             : {
    9444        4319 :   GEN c, V = cgetg(lim+2, t_COL);
    9445             :   long n;
    9446        4319 :   c = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9447        4319 :   if (P) c = grem(c, P);
    9448        4319 :   gel(V,1) = c;
    9449       92512 :   for (n=1; n <= lim; n++)
    9450             :   {
    9451       88193 :     c = sigchi2(k, CHI1, CHI2, n, ord);
    9452       88193 :     if (P) c = grem(c, P);
    9453       88193 :     gel(V,n+1) = c;
    9454             :   }
    9455        4319 :   return V;
    9456             : }
    9457             : static GEN
    9458        3290 : mfeisenstein2pure_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p, long lim)
    9459             : {
    9460        3290 :   GEN V = cgetg(lim+2, t_VECSMALL);
    9461             :   long n;
    9462        3290 :   V[1] = mfeisenstein2_0_Fl(k, CHI1vec, CHI2vec, vz, p);
    9463        3290 :   for (n=1; n <= lim; n++) V[n+1] = sigchi2_Fl(k, CHI1vec, CHI2vec, n, vz, p);
    9464        3290 :   return V;
    9465             : }
    9466             : 
    9467             : static GEN
    9468         175 : getcolswt2(GEN M, GEN D, ulong p)
    9469             : {
    9470         175 :   GEN R, v = gel(M,1);
    9471         175 :   long i, l = lg(M) - 1;
    9472         175 :   R = cgetg(l, t_MAT); /* skip D[1] = 1 */
    9473         616 :   for (i = 1; i < l; i++)
    9474             :   {
    9475         441 :     GEN w = Flv_Fl_mul(gel(M,i+1), D[i+1], p);
    9476         441 :     gel(R,i) = Flv_sub(v, w, p);
    9477             :   }
    9478         175 :   return R;
    9479             : }
    9480             : static GEN
    9481        4522 : expandbd(GEN V, long d)
    9482             : {
    9483             :   long L, n, nd;
    9484             :   GEN W;
    9485        4522 :   if (d == 1) return V;
    9486        1778 :   L = lg(V)-1; W = zerocol(L); /* nd = n/d */
    9487        1778 :   for (n = nd = 0; n < L; n += d, nd++) gel(W, n+1) = gel(V, nd+1);
    9488        1778 :   return W;
    9489             : }
    9490             : static GEN
    9491        5222 : expandbd_Fl(GEN V, long d)
    9492             : {
    9493             :   long L, n, nd;
    9494             :   GEN W;
    9495        5222 :   if (d == 1) return V;
    9496        1932 :   L = lg(V)-1; W = zero_Flv(L); /* nd = n/d */
    9497        1932 :   for (n = nd = 0; n < L; n += d, nd++) W[n+1] = V[nd+1];
    9498        1932 :   return W;
    9499             : }
    9500             : static void
    9501        3290 : getcols_i(GEN *pM, GEN *pvj, GEN gk, GEN CHI1vec, GEN CHI2vec, long NN1, GEN vz,
    9502             :           ulong p, long lim)
    9503             : {
    9504        3290 :   GEN CHI1 = CHIvec_CHI(CHI1vec), CHI2 = CHIvec_CHI(CHI2vec);
    9505        3290 :   long N2 = CHIvec_N(CHI2vec);
    9506        3290 :   GEN vj, M, D = mydivisorsu(NN1/N2);
    9507        3290 :   long i, l = lg(D), k = gk[2];
    9508        3290 :   GEN V = mfeisenstein2pure_Fl(k, CHI1vec, CHI2vec, vz, p, lim);
    9509        3290 :   M = cgetg(l, t_MAT);
    9510        3290 :   for (i = 1; i < l; i++) gel(M,i) = expandbd_Fl(V, D[i]);
    9511        3290 :   if (k == 2 && N2 == 1 && CHIvec_N(CHI1vec) == 1)
    9512             :   {
    9513         175 :     M = getcolswt2(M, D, p); l--;
    9514         175 :     D = vecslice(D, 2, l);
    9515             :   }
    9516        3290 :   *pM = M;
    9517        3290 :   *pvj = vj = cgetg(l, t_VEC);
    9518        3290 :   for (i = 1; i < l; i++) gel(vj,i) = mkvec4(gk, CHI1, CHI2, utoipos(D[i]));
    9519        3290 : }
    9520             : 
    9521             : /* find all CHI1, CHI2 mod N such that CHI1*CHI2 = CHI, f(CHI1)*f(CHI2) | N.
    9522             :  * set M = mfcoefs(B_e E(CHI1,CHI2), lim), vj = [e,i1,i2] */
    9523             : static void
    9524        1267 : getcols(GEN *pM, GEN *pv, long k, long nCHI, GEN allN, GEN vz, ulong p,
    9525             :         long lim)
    9526             : {
    9527        1267 :   GEN vCHI = gel(allN,1), gk = utoi(k);
    9528        1267 :   GEN M = cgetg(1,t_MAT), v = cgetg(1,t_VEC);
    9529        1267 :   long i1, N = lg(vCHI)-1;
    9530       63322 :   for (i1 = 1; i1 <= N; i1++)
    9531             :   {
    9532       62055 :     GEN CHI1vec = gel(vCHI, i1), CHI2vec, M1, v1;
    9533             :     long NN1, i2;
    9534      121618 :     if (!CHI1vec) continue;
    9535       46718 :     if (k == 1 && CHIvec_parity(CHI1vec) == -1) continue;
    9536       29582 :     NN1 = N/CHIvec_N(CHI1vec); /* N/f(chi1) */;
    9537       29582 :     i2 = Fl_div(nCHI,i1, N);
    9538       29582 :     if (!i2) i2 = 1;
    9539       29582 :     CHI2vec = gel(vCHI,i2);
    9540       29582 :     if (NN1 % CHIvec_N(CHI2vec)) continue; /* f(chi1)f(chi2) | N ? */
    9541        2492 :     getcols_i(&M1, &v1, gk, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9542        2492 :     M = shallowconcat(M, M1);
    9543        2492 :     v = shallowconcat(v, v1);
    9544             :   }
    9545        1267 :   *pM = M;
    9546        1267 :   *pv = v;
    9547        1267 : }
    9548             : 
    9549             : static void
    9550         833 : update_Mj(GEN *M, GEN *vecj, GEN *pz, ulong p)
    9551             : {
    9552             :   GEN perm;
    9553         833 :   *pz = Flm_indexrank(*M, p); perm = gel(*pz,2);
    9554         833 :   *M = vecpermute(*M, perm);
    9555         833 :   *vecj = vecpermute(*vecj, perm);
    9556         833 : }
    9557             : static int
    9558         301 : getcolsgen(long dim, GEN *pM, GEN *pvj, GEN *pz, long k, long ell, long nCHI,
    9559             :            GEN allN, GEN vz, ulong p, long lim)
    9560             : {
    9561         301 :   GEN vCHI = gel(allN,1), bymod = gel(allN,2), gell = utoi(ell);
    9562         301 :   long i1, N = lg(vCHI)-1;
    9563         301 :   long L = lim+1;
    9564         301 :   if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9565         301 :   if (lg(*pvj)-1 == dim) return 1;
    9566        1099 :   for (i1 = 1; i1 <= N; i1++)
    9567             :   {
    9568        1085 :     GEN CHI1vec = gel(vCHI, i1), T;
    9569             :     long par1, j, l, N1, NN1;
    9570             : 
    9571        1085 :     if (!CHI1vec) continue;
    9572        1071 :     par1 = CHIvec_parity(CHI1vec);
    9573        1071 :     if (ell == 1 && par1 == -1) continue;
    9574         672 :     if (odd(ell)) par1 = -par1;
    9575         672 :     N1 = CHIvec_N(CHI1vec);
    9576         672 :     NN1 = N/N1;
    9577         672 :     T = gel(bymod, NN1); l = lg(T);
    9578        2394 :     for (j = 1; j < l; j++)
    9579             :     {
    9580        1995 :       long i2 = T[j], l1, l2, j1, s, nC;
    9581        1995 :       GEN M, M1, M2, vj, vj1, vj2, CHI2vec = gel(vCHI, i2);
    9582        3192 :       if (CHIvec_parity(CHI2vec) != par1) continue;
    9583         798 :       nC = Fl_div(nCHI, Fl_mul(i1,i2,N), N);
    9584         798 :       getcols(&M2, &vj2, k-ell, nC, allN, vz, p, lim);
    9585         798 :       l2 = lg(M2); if (l2 == 1) continue;
    9586         798 :       getcols_i(&M1, &vj1, gell, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9587         798 :       l1 = lg(M1);
    9588         798 :       M1 = Flm_to_FlxV(M1, 0);
    9589         798 :       M2 = Flm_to_FlxV(M2, 0);
    9590         798 :       M  = cgetg((l1-1)*(l2-1) + 1, t_MAT);
    9591         798 :       vj = cgetg((l1-1)*(l2-1) + 1, t_VEC);
    9592        1995 :       for (j1 = s = 1; j1 < l1; j1++)
    9593             :       {
    9594        1197 :         GEN E = gel(M1,j1), v = gel(vj1,j1);
    9595             :         long j2;
    9596        5166 :         for (j2 = 1; j2 < l2; j2++, s++)
    9597             :         {
    9598        3969 :           GEN c = Flx_to_Flv(Flxn_mul(E, gel(M2,j2), L, p), L);
    9599        3969 :           gel(M,s) = c;
    9600        3969 :           gel(vj,s) = mkvec2(v, gel(vj2,j2));
    9601             :         }
    9602             :       }
    9603         798 :       *pM = shallowconcat(*pM, M);
    9604         798 :       *pvj = shallowconcat(*pvj, vj);
    9605         798 :       if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9606         798 :       if (lg(*pvj)-1 == dim) return 1;
    9607             :     }
    9608             :   }
    9609          14 :   if (ell == 1)
    9610             :   {
    9611          14 :     update_Mj(pM, pvj, pz, p);
    9612          14 :     return (lg(*pvj)-1 == dim);
    9613             :   }
    9614           0 :   return 0;
    9615             : }
    9616             : 
    9617             : static GEN
    9618         931 : mkF2bd(long d, long lim)
    9619             : {
    9620         931 :   GEN V = zerovec(lim + 1);
    9621             :   long n;
    9622         931 :   gel(V, 1) = ginv(stoi(-24));
    9623         931 :   for (n = 1; n <= lim/d; n++) gel(V, n*d + 1) = mysumdivku(n, 1);
    9624         931 :   return V;
    9625             : }
    9626             : 
    9627             : static GEN
    9628        4676 : mkeisen(GEN E, long ord, GEN P, long lim)
    9629             : {
    9630        4676 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9631        4676 :   GEN CHI1 = gel(E,2), CHI2 = gel(E,3);
    9632        4676 :   if (k == 2 && mfcharistrivial(CHI1) && mfcharistrivial(CHI2))
    9633         357 :     return gsub(mkF2bd(1,lim), gmulgs(mkF2bd(e,lim), e));
    9634             :   else
    9635             :   {
    9636        4319 :     GEN V = mfeisenstein2pure(k, CHI1, CHI2, ord, P, lim);
    9637        4319 :     return expandbd(V, e);
    9638             :   }
    9639             : }
    9640             : static GEN
    9641         441 : mkM(GEN vj, long pn, GEN P, long lim)
    9642             : {
    9643         441 :   long j, l = lg(vj), L = lim+1;
    9644         441 :   GEN M = cgetg(l, t_MAT);
    9645        3836 :   for (j = 1; j < l; j++)
    9646             :   {
    9647             :     GEN E1, E2;
    9648        3395 :     parse_vecj(gel(vj,j), &E1,&E2);
    9649        3395 :     E1 = RgV_to_RgX(mkeisen(E1, pn, P, lim), 0);
    9650        3395 :     if (E2)
    9651             :     {
    9652        1281 :       E2 = RgV_to_RgX(mkeisen(E2, pn, P, lim), 0);
    9653        1281 :       E1 = RgXn_mul(E1, E2, L);
    9654             :     }
    9655        3395 :     E1 = RgX_to_RgC(E1, L);
    9656        3395 :     if (P && E2) E1 = RgXQV_red(E1, P);
    9657        3395 :     gel(M,j) = E1;
    9658             :   }
    9659         441 :   return M;
    9660             : }
    9661             : 
    9662             : /* assume N > 2 */
    9663             : static GEN
    9664          21 : mffindeisen1(long N)
    9665             : {
    9666          21 :   GEN G = znstar0(utoipos(N), 1), L = chargalois(G, NULL), chi0 = NULL;
    9667          21 :   long j, m = N, l = lg(L);
    9668         154 :   for (j = 1; j < l; j++)
    9669             :   {
    9670         147 :     GEN chi = gel(L,j);
    9671         147 :     long r = myeulerphiu(itou(zncharorder(G,chi)));
    9672         147 :     if (r >= m) continue;
    9673         105 :     chi = znconreyfromchar(G, chi);
    9674         105 :     if (zncharisodd(G,chi)) { m = r; chi0 = chi; if (r == 1) break; }
    9675             :   }
    9676          21 :   if (!chi0) pari_err_BUG("mffindeisen1 [no Eisenstein series found]");
    9677          21 :   chi0 = znchartoprimitive(G,chi0);
    9678          21 :   return mfcharGL(gel(chi0,1), gel(chi0,2));
    9679             : }
    9680             : 
    9681             : static GEN
    9682         469 : mfeisensteinspaceinit_i(long N, long k, GEN CHI)
    9683             : {
    9684         469 :   GEN M, Minv, vj, vG, GN, allN, P, vz, z = NULL;
    9685         469 :   long nCHI, lim, ell, ord, dim = mffulldim(N, k, CHI);
    9686             :   ulong r, p;
    9687             : 
    9688         469 :   if (!dim) retmkvec3(cgetg(1,t_VECSMALL),
    9689             :                       mkvec2(cgetg(1,t_MAT),gen_1),cgetg(1,t_VEC));
    9690         469 :   lim = mfsturmNk(N, k) + 1;
    9691         469 :   allN = charsmodN(N);
    9692         469 :   vG = gel(allN,3);
    9693         469 :   GN = gel(vG,N);
    9694         469 :   ord = znstar_get_expo(GN);
    9695         469 :   P = ord <= 2? NULL: polcyclo(ord, varn(mfcharpol(CHI)));
    9696         469 :   CHI = induce(GN, CHI); /* lift CHI mod N before mfcharno*/
    9697         469 :   nCHI = mfcharno(CHI);
    9698         469 :   r = QabM_init(ord, &p);
    9699         469 :   vz = Fl_powers(r, ord, p);
    9700         469 :   getcols(&M, &vj, k, nCHI, allN, vz, p, lim);
    9701         483 :   for (ell = k>>1; ell >= 1; ell--)
    9702         301 :     if (getcolsgen(dim, &M, &vj, &z, k, ell, nCHI, allN, vz, p, lim)) break;
    9703         469 :   if (!z) update_Mj(&M, &vj, &z, p);
    9704         469 :   if (lg(vj) - 1 < dim) return NULL;
    9705         441 :   M = mkM(vj, ord, P, lim);
    9706         441 :   Minv = QabM_Minv(rowpermute(M, gel(z,1)), P, ord);
    9707         441 :   return mkvec4(gel(z,1), Minv, vj, utoi(ord));
    9708             : }
    9709             : /* true mf */
    9710             : static GEN
    9711         441 : mfeisensteinspaceinit(GEN mf)
    9712             : {
    9713         441 :   pari_sp av = avma;
    9714         441 :   GEN z, CHI = MF_get_CHI(mf);
    9715         441 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    9716         441 :   if (!CHI) CHI = mfchartrivial();
    9717         441 :   z = mfeisensteinspaceinit_i(N, k, CHI);
    9718         441 :   if (!z)
    9719             :   {
    9720          21 :     GEN E, CHIN = mffindeisen1(N), CHI0 = mfchartrivial();
    9721          21 :     z = mfeisensteinspaceinit_i(N, k+1, mfcharmul(CHI, CHIN));
    9722          21 :     if (z) E = mkvec4(gen_1, CHI0, CHIN, gen_1);
    9723             :     else
    9724             :     {
    9725           7 :       z = mfeisensteinspaceinit_i(N, k+2, CHI);
    9726           7 :       E = mkvec4(gen_2, CHI0, CHI0, utoipos(N));
    9727             :     }
    9728          21 :     z = mkvec2(z, E);
    9729             :   }
    9730         441 :   return gerepilecopy(av, z);
    9731             : }
    9732             : 
    9733             : /* decomposition of modular form on eisenspace */
    9734             : static GEN
    9735         826 : mfeisensteindec(GEN mf, GEN F)
    9736             : {
    9737         826 :   pari_sp av = avma;
    9738             :   GEN M, Mindex, Mvecj, V, B, CHI;
    9739             :   long o, ord;
    9740             : 
    9741         826 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    9742         826 :   if (lg(Mvecj) < 5)
    9743             :   {
    9744          21 :     GEN E, e = gel(Mvecj,2), gkE = gel(e,1);
    9745          21 :     long dE = itou(gel(e,4));
    9746          21 :     Mvecj = gel(Mvecj,1);
    9747          21 :     E = mfeisenstein(itou(gkE), NULL, gel(e,3));
    9748          21 :     if (dE != 1) E = mfbd_E2(E, dE, gel(e,2)); /* here k = 2 */
    9749          21 :     F = mfmul(F, E);
    9750             :   }
    9751         826 :   M = gel(Mvecj, 2);
    9752         826 :   if (lg(M) == 1) return cgetg(1, t_VEC);
    9753         826 :   Mindex = gel(Mvecj, 1);
    9754         826 :   ord = itou(gel(Mvecj,4));
    9755         826 :   V = mfcoefs(F, Mindex[lg(Mindex)-1]-1, 1); settyp(V, t_COL);
    9756         826 :   CHI = mf_get_CHI(F);
    9757         826 :   o = mfcharorder(CHI);
    9758         826 :   if (o > 2 && o != ord)
    9759             :   { /* convert Mod(.,polcyclo(o)) to Mod(., polcyclo(N)) for o | N,
    9760             :      * o and N both != 2 (mod 4) */
    9761          63 :     GEN z, P = gel(M,4); /* polcyclo(ord) */
    9762          63 :     long vt = varn(P);
    9763          63 :     z = gmodulo(pol_xn(ord/o, vt), P);
    9764          63 :     if (ord % o) pari_err_TYPE("mfeisensteindec", V);
    9765          63 :     V = gsubst(liftpol_shallow(V), vt, z);
    9766             :   }
    9767         826 :   B = Minv_RgC_mul(M, vecpermute(V, Mindex));
    9768         826 :   return gerepileupto(av, B);
    9769             : }
    9770             : 
    9771             : /*********************************************************************/
    9772             : /*                        END EISENSPACE                             */
    9773             : /*********************************************************************/
    9774             : 
    9775             : static GEN
    9776          70 : sertocol2(GEN S, long l)
    9777             : {
    9778          70 :   GEN C = cgetg(l + 2, t_COL);
    9779             :   long i;
    9780          70 :   for (i = 0; i <= l; i++) gel(C, i+1) = polcoef_i(S, i, -1);
    9781          70 :   return C;
    9782             : }
    9783             : 
    9784             : /* Compute polynomial P0 such that F=E4^(k/4)P0(E6/E4^(3/2)). */
    9785             : static GEN
    9786          14 : mfcanfindp0(GEN F, long k)
    9787             : {
    9788          14 :   pari_sp ltop = avma;
    9789             :   GEN E4, E6, V, V1, Q, W, res, M, B;
    9790             :   long l, j;
    9791          14 :   l = k/6 + 2;
    9792          14 :   V = mfcoefsser(F,l);
    9793          14 :   E4 = mfcoefsser(mfEk(4),l);
    9794          14 :   E6 = mfcoefsser(mfEk(6),l);
    9795          14 :   V1 = gdiv(V, gpow(E4, sstoQ(k,4), 0));
    9796          14 :   Q = gdiv(E6, gpow(E4, sstoQ(3,2), 0));
    9797          14 :   W = gpowers(Q, l - 1);
    9798          14 :   M = cgetg(l + 1, t_MAT);
    9799          14 :   for (j = 1; j <= l; j++) gel(M,j) = sertocol2(gel(W,j), l);
    9800          14 :   B = sertocol2(V1, l);
    9801          14 :   res = inverseimage(M, B);
    9802          14 :   if (lg(res) == 1) err_space(F);
    9803          14 :   return gerepilecopy(ltop, gtopolyrev(res, 0));
    9804             : }
    9805             : 
    9806             : /* Compute the first n+1 Taylor coeffs at tau=I of a modular form
    9807             :  * on SL_2(Z). */
    9808             : GEN
    9809          14 : mftaylor(GEN F, long n, long flreal, long prec)
    9810             : {
    9811          14 :   pari_sp ltop = avma;
    9812          14 :   GEN P0, Pm1 = gen_0, v;
    9813          14 :   GEN X2 = mkpoln(3, ghalf,gen_0,gneg(ghalf)); /* (x^2-1) / 2 */
    9814             :   long k, m;
    9815          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftaylor",F);
    9816          14 :   k = mf_get_k(F);
    9817          14 :   if (mf_get_N(F) != 1 || k < 0) pari_err_IMPL("mftaylor for this form");
    9818          14 :   P0 = mfcanfindp0(F, k);
    9819          14 :   v = cgetg(n+2, t_VEC); gel(v, 1) = RgX_coeff(P0,0);
    9820         154 :   for (m = 0; m < n; m++)
    9821             :   {
    9822         140 :     GEN P1 = gdivgs(gmulsg(-(k + 2*m), RgX_shift(P0,1)), 12);
    9823         140 :     P1 = gadd(P1, gmul(X2, RgX_deriv(P0)));
    9824         140 :     if (m) P1 = gsub(P1, gdivgs(gmulsg(m*(m+k-1), Pm1), 144));
    9825         140 :     Pm1 = P0; P0 = P1;
    9826         140 :     gel(v, m+2) = RgX_coeff(P0, 0);
    9827             :   }
    9828          14 :   if (flreal)
    9829             :   {
    9830           7 :     GEN pi2 = Pi2n(1, prec), pim4 = gmulsg(-2, pi2), VPC;
    9831           7 :     GEN C = gmulsg(3, gdiv(gpowgs(ggamma(ginv(utoi(4)), prec), 8), gpowgs(pi2, 6)));
    9832             :     /* E_4(i): */
    9833           7 :     GEN facn = gen_1;
    9834           7 :     VPC = gpowers(gmul(pim4, gsqrt(C, prec)), n);
    9835           7 :     C = gpow(C, sstoQ(k,4), prec);
    9836          84 :     for (m = 0; m <= n; m++)
    9837             :     {
    9838          77 :       gel(v, m+1) = gdiv(gmul(C, gmul(gel(v, m+1), gel(VPC, m+1))), facn);
    9839          77 :       facn = gmulgs(facn, m+1);
    9840             :     }
    9841             :   }
    9842          14 :   return gerepilecopy(ltop, v);
    9843             : }
    9844             : 
    9845             : #if 0
    9846             : /* To be used in mfeigensearch() */
    9847             : GEN
    9848             : mfreadratfile()
    9849             : {
    9850             :   GEN eqn;
    9851             :   pariFILE *F = pari_fopengz("rateigen300.gp");
    9852             :   eqn = gp_readvec_stream(F->file);
    9853             :   pari_fclose(F);
    9854             :   return eqn;
    9855             : }
    9856             : #endif
    9857             :  /*****************************************************************/
    9858             : /*           EISENSTEIN CUSPS: COMPLEX DIRECTLY: one F_k         */
    9859             : /*****************************************************************/
    9860             : 
    9861             : /* CHIvec = charinit(CHI); data = [N1g/g1,N2g/g2,g1/g,g2/g,C/g1,C/g2,
    9862             :  * (N1g/g1)^{-1},(N2g/g2)^{-1}] */
    9863             : 
    9864             : /* nm = n/m;
    9865             :  * z1 = powers of \z_{C/g}^{(Ae/g)^{-1}},
    9866             :  * z2 = powers of \z_N^{A^{-1}(g1g2/C)}]
    9867             :  * N.B. : we compute value and conjugate at the end, so it is (Ae/g)^{-1}
    9868             :  * and not -(Ae/g)^{-1} */
    9869             : static GEN
    9870     5749562 : eiscnm(long nm, long m, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1)
    9871             : {
    9872     5749562 :   long Cg1 = data[5], s10 = (nm*data[7]) % Cg1, r10 = (nm - data[1]*s10) / Cg1;
    9873     5749562 :   long Cg2 = data[6], s20 = (m *data[8]) % Cg2, r20 = (m  - data[2]*s20) / Cg2;
    9874             :   long j1, r1, s1;
    9875     5749562 :   GEN T = gen_0;
    9876    14619822 :   for (j1 = 0, r1 = r10, s1 = s10; j1 < data[3]; j1++, r1 -= data[1], s1 += Cg1)
    9877             :   {
    9878     8870260 :     GEN c1 = mychareval(CHI1vec, r1);
    9879     8870260 :     if (!gequal0(c1))
    9880             :     {
    9881             :       long j2, r2, s2;
    9882     6019818 :       GEN S = gen_0;
    9883    16697520 :       for (j2 = 0, r2 = r20, s2 = s20; j2 < data[4]; j2++, r2 -= data[2], s2 += Cg2)
    9884             :       {
    9885    10677702 :         GEN c2 = mychareval(CHI2vec, r2);
    9886    10677702 :         if (!gequal0(c2)) S = gadd(S, gmul(c2, rootsof1pow(z1, s1*s2)));
    9887             :       }
    9888     6019818 :       T = gadd(T, gmul(c1, S));
    9889             :     }
    9890             :   }
    9891     5749562 :   return conj_i(T);
    9892             : }
    9893             : 
    9894             : static GEN
    9895      451682 : fg1g2n(long n, long k, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1, GEN z2)
    9896             : {
    9897      451682 :   pari_sp av = avma;
    9898      451682 :   GEN S = gen_0, D = mydivisorsu(n);
    9899      451682 :   long i, l = lg(D);
    9900     3326463 :   for (i = 1; i < l; i++)
    9901             :   {
    9902     2874781 :     long m = D[i], nm = D[l-i]; /* n/m */
    9903     2874781 :     GEN u = eiscnm( nm,  m, CHI1vec, CHI2vec, data, z1);
    9904     2874781 :     GEN v = eiscnm(-nm, -m, CHI1vec, CHI2vec, data, z1);
    9905     2874781 :     GEN w = odd(k) ? gsub(u, v) : gadd(u, v);
    9906     2874781 :     S = gadd(S, gmul(powuu(m, k-1), w));
    9907             :   }
    9908      451682 :   return gerepileupto(av, gmul(S, rootsof1pow(z2, n)));
    9909             : }
    9910             : 
    9911             : static GEN
    9912       11354 : gausssumcx(GEN CHIvec, long prec)
    9913             : {
    9914             :   GEN z, S, V;
    9915       11354 :   long m, N = CHIvec_N(CHIvec);
    9916       11354 :   if (N == 1) return gen_1;
    9917        6132 :   V = CHIvec_val(CHIvec);
    9918        6132 :   z = rootsof1u_cx(N, prec);
    9919        6132 :   S = gmul(z, gel(V, N));
    9920        6132 :   for (m = N-1; m >= 1; m--) S = gmul(z, gadd(gel(V, m), S));
    9921        6132 :   return S;
    9922             : }
    9923             : 
    9924             : /* Computation of Q_k(\z_N^s) as a polynomial in \z_N^s. FIXME: explicit
    9925             :  * formula ? */
    9926             : static GEN
    9927        1722 : mfqk(long k, long N)
    9928             : {
    9929        1722 :   GEN X = pol_x(0), P = gsubgs(gpowgs(X,N), 1), ZI, Q, Xm1, invden;
    9930             :   long i;
    9931        1722 :   ZI = cgetg(N, t_VEC);
    9932        1722 :   for (i = 1; i < N; i++) gel(ZI, i) = utoi(i);
    9933        1722 :   ZI = gdivgs(gmul(X, gtopolyrev(ZI, 0)), N);
    9934        1722 :   if (k == 1) return ZI;
    9935        1071 :   invden = RgXQ_powu(ZI, k, P);
    9936        1071 :   Q = gneg(X); Xm1 = gsubgs(X, 1);
    9937        2716 :   for (i = 2; i < k; i++)
    9938        1645 :     Q = gmul(X, ZX_add(gmul(Xm1, ZX_deriv(Q)), gmulsg(-i, Q)));
    9939        1071 :   return RgXQ_mul(Q, invden, P);
    9940             : }
    9941             : /* CHI mfchar */
    9942             : /* Warning: M is a multiple of the conductor of CHI, but is NOT
    9943             :    necessarily its modulus */
    9944             : 
    9945             : static GEN
    9946        2506 : mfskcx(long k, GEN CHI, long M, long prec)
    9947             : {
    9948             :   GEN S, CHIvec, P;
    9949             :   long F, m, i, l;
    9950        2506 :   CHI = mfchartoprimitive(CHI, &F);
    9951        2506 :   CHIvec = mfcharcxinit(CHI, prec);
    9952        2506 :   if (F == 1) S = gdivgs(bernfrac(k), k);
    9953             :   else
    9954             :   {
    9955        1722 :     GEN Q = mfqk(k, F), V = CHIvec_val(CHIvec);
    9956        1722 :     S = gmul(gel(V, F), RgX_coeff(Q, 0));
    9957        1722 :     for (m = 1; m < F; m++) S = gadd(S, gmul(gel(V, m), RgX_coeff(Q, m)));
    9958        1722 :     S = conj_i(S);
    9959             :   }
    9960             :   /* prime divisors of M not dividing f(chi) */
    9961        2506 :   P = gel(myfactoru(u_ppo(M/F,F)), 1); l = lg(P);
    9962        2632 :   for (i = 1; i < l; i++)
    9963             :   {
    9964         126 :     long p = P[i];
    9965         126 :     S = gmul(S, gsubsg(1, gdiv(mychareval(CHIvec, p), powuu(p, k))));
    9966             :   }
    9967        2506 :   return gmul(gmul(gausssumcx(CHIvec, prec), S), powuu(M/F, k));
    9968             : }
    9969             : 
    9970             : static GEN
    9971        4620 : f00_i(long k, GEN CHI1vec, GEN CHI2vec, GEN G2, GEN S, long prec)
    9972             : {
    9973             :   GEN c, a;
    9974        4620 :   long N1 = CHIvec_N(CHI1vec), N2 = CHIvec_N(CHI2vec);
    9975        4620 :   if (S[2] != N1) return gen_0;
    9976        2506 :   c = mychareval(CHI1vec, S[3]);
    9977        2506 :   if (isintzero(c)) return gen_0;
    9978        2506 :   a = mfskcx(k, mfchardiv(CHIvec_CHI(CHI2vec), CHIvec_CHI(CHI1vec)), N1*N2, prec);
    9979        2506 :   a = gmul(a, conj_i(gmul(c,G2)));
    9980        2506 :   return gdiv(a, mulsi(-N2, powuu(S[1], k-1)));
    9981             : }
    9982             : 
    9983             : static GEN
    9984        3850 : f00(long k, GEN CHI1vec,GEN CHI2vec, GEN G1,GEN G2, GEN data, long prec)
    9985             : {
    9986             :   GEN T1, T2;
    9987        3850 :   T2 = f00_i(k, CHI1vec, CHI2vec, G2, data, prec);
    9988        3850 :   if (k > 1) return T2;
    9989         770 :   T1 = f00_i(k, CHI2vec, CHI1vec, G1, data, prec);
    9990         770 :   return gadd(T1, T2);
    9991             : }
    9992             : 
    9993             : /* ga in SL_2(Z), find beta [a,b;c,d] in Gamma_0(N) and mu in Z such that
    9994             :  * beta * ga * T^u = [A',B';C',D'] with C' | N and N | B', C' > 0 */
    9995             : static void
    9996        4424 : mfgatogap(GEN ga, long N, long *pA, long *pC, long *pD, long *pd, long *pmu)
    9997             : {
    9998        4424 :   GEN A = gcoeff(ga,1,1), B = gcoeff(ga,1,2);
    9999        4424 :   GEN C = gcoeff(ga,2,1), D = gcoeff(ga,2,2), a, b, c, d;
   10000             :   long t, Ap, Cp, B1, D1, mu;
   10001        4424 :   Cp = itou(bezout(muliu(A,N), C, &c, &d)); /* divides N */
   10002        4424 :   t = 0;
   10003        4424 :   if (Cp > 1)
   10004             :   { /* (d, N/Cp) = 1, find t such that (d - t*(A*N/Cp), N) = 1 */
   10005        2345 :     long dN = umodiu(d,Cp), Q = (N/Cp * umodiu(A,Cp)) % Cp;
   10006        2345 :     while (ugcd(dN, Cp) > 1) { t++; dN = Fl_sub(dN, Q, Cp); }
   10007             :   }
   10008        4424 :   if (t)
   10009             :   {
   10010         371 :     c = addii(c, mului(t, diviuexact(C,Cp)));
   10011         371 :     d = subii(d, mului(t, muliu(A, N/Cp))); /* (d,N) = 1 */
   10012             :   }
   10013        4424 :   D1 = umodiu(mulii(d,D), N);
   10014        4424 :   (void)bezout(d, mulis(c,-N), &a, &b); /* = 1 */
   10015        4424 :   t = 0; Ap = umodiu(addii(mulii(a,A), mulii(b,C)), N); /* (Ap,Cp) = 1 */
   10016        4424 :   while (ugcd(Ap, N) > 1) { t++; Ap = Fl_add(Ap, Cp, N); }
   10017        4424 :   B1 = umodiu(a,N)*umodiu(B,N) + umodiu(b,N)*umodiu(D,N) + t*D1;
   10018        4424 :   B1 %= N;
   10019        4424 :   *pmu = mu = Fl_neg(Fl_div(B1, Ap, N), N);
   10020             :   /* A', D' and d only needed modulo N */
   10021        4424 :   *pd = umodiu(d, N);
   10022        4424 :   *pA = Ap;
   10023        4424 :   *pC = Cp; *pD = (D1 + Cp*mu) % N;
   10024        4424 : }
   10025             : 
   10026             : #if 0
   10027             : /* CHI is a mfchar, return alpha(CHI) */
   10028             : static long
   10029             : mfalchi(GEN CHI, long AN, long cg)
   10030             : {
   10031             :   GEN G = gel(CHI,1), chi = gel(CHI,2), go = gmfcharorder(CHI);
   10032             :   long o = itou(go), a = itos( znchareval(G, chi, stoi(1 + AN/cg), go) );
   10033             :   if (a < 0 || (cg * a) % o) pari_err_BUG("mfalchi");
   10034             :   return (cg * a) / o;
   10035             : }
   10036             : #endif
   10037             : /* return A such that CHI1(t) * CHI2(t) = e(A) or NULL if (t,N1*N2) > 1 */
   10038             : static GEN
   10039        8848 : mfcharmuleval(GEN CHI1vec, GEN CHI2vec, long t)
   10040             : {
   10041        8848 :   long a1 = mycharexpo(CHI1vec, t), o1 = CHIvec_ord(CHI1vec);
   10042        8848 :   long a2 = mycharexpo(CHI2vec, t), o2 = CHIvec_ord(CHI2vec);;
   10043        8848 :   if (a1 < 0 || a2 < 0) return NULL;
   10044        8848 :   return sstoQ(a1*o2 + a2*o1, o1*o2);
   10045             : }
   10046             : static GEN
   10047        4424 : mfcharmulcxeval(GEN CHI1vec, GEN CHI2vec, long t, long prec)
   10048             : {
   10049        4424 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, t);
   10050             :   long n, d;
   10051        4424 :   if (!A) return gen_0;
   10052        4424 :   Qtoss(A, &n,&d); return rootsof1q_cx(n, d, prec);
   10053             : }
   10054             : /* alpha(CHI1 * CHI2) */
   10055             : static long
   10056        4424 : mfalchi2(GEN CHI1vec, GEN CHI2vec, long AN, long cg)
   10057             : {
   10058        4424 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, 1 + AN/cg);
   10059             :   long a;
   10060        4424 :   if (!A) pari_err_BUG("mfalchi2");
   10061        4424 :   A = gmulsg(cg, A);
   10062        4424 :   if (typ(A) != t_INT) pari_err_BUG("mfalchi2");
   10063        4424 :   a = itos(A) % cg; if (a < 0) a += cg;
   10064        4424 :   return a;
   10065             : }
   10066             : 
   10067             : /* return g = (a,b), set u >= 0 s.t. g = a * u (mod b) */
   10068             : static long
   10069       17696 : mybezout(long a, long b, long *pu)
   10070             : {
   10071       17696 :   long junk, g = cbezout(a, b, pu, &junk);
   10072       17696 :   if (*pu < 0) *pu += b/g;
   10073       17696 :   return g;
   10074             : }
   10075             : 
   10076             : /* E = [k, CHI1,CHI2, e], CHI1 and CHI2 primitive mfchars such that,
   10077             :  * CHI1(-1)*CHI2(-1) = (-1)^k; expansion of (B_e (E_k(CHI1,CHI2))) | ga.
   10078             :  * w is the width for the space of the calling function. */
   10079             : static GEN
   10080        4424 : mfeisensteingacx(GEN E, long w, GEN ga, long lim, long prec)
   10081             : {
   10082        4424 :   GEN CHI1vec, CHI2vec, CHI1 = gel(E,2), CHI2 = gel(E,3), v, S, ALPHA;
   10083             :   GEN G1, G2, z1, z2, data;
   10084        4424 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
   10085        4424 :   long N1 = mfcharmodulus(CHI1);
   10086        4424 :   long N2 = mfcharmodulus(CHI2), N = e * N1 * N2;
   10087             :   long NsurC, cg, wN, A, C, Ai, d, mu, alchi, na, da;
   10088             :   long eg, g, gH, U, u0, u1, u2, Aig, H, m, n, t, Cg, NC1, NC2;
   10089             : 
   10090        4424 :   mfgatogap(ga, N, &A, &C, &Ai, &d, &mu);
   10091        4424 :   CHI1vec = mfcharcxinit(CHI1, prec);
   10092        4424 :   CHI2vec = mfcharcxinit(CHI2, prec);
   10093        4424 :   NsurC = N/C; cg  = ugcd(C, NsurC); wN = NsurC / cg;
   10094        4424 :   if (w%wN) pari_err_BUG("mfeisensteingacx [wN does not divide w]");
   10095        4424 :   alchi = mfalchi2(CHI1vec, CHI2vec, A*N, cg);
   10096        4424 :   ALPHA = sstoQ(alchi, NsurC);
   10097             : 
   10098        4424 :   g = mybezout(A*e, C, &u0); Cg = C/g; eg = e/g;
   10099        4424 :   NC1 = mybezout(N1, Cg, &u1);
   10100        4424 :   NC2 = mybezout(N2, Cg, &u2);
   10101        4424 :   H = (NC1*NC2*g)/Cg;
   10102        4424 :   Aig = (Ai*H)%N; if (Aig < 0) Aig += N;
   10103        4424 :   z1 = rootsof1powinit(u0, Cg, prec);
   10104        4424 :   z2 = rootsof1powinit(Aig, N, prec);
   10105        4424 :   data = mkvecsmalln(8, N1/NC1, N2/NC2, NC1, NC2, Cg/NC1, Cg/NC2, u1, u2);
   10106        4424 :   v = zerovec(lim + 1);
   10107             :   /* need n*H = alchi (mod cg) */
   10108        4424 :   gH = mybezout(H, cg, &U);
   10109        4424 :   if (gH > 1)
   10110             :   {
   10111         357 :     if (alchi % gH) return mkvec2(gen_0, v);
   10112         357 :     alchi /= gH; cg /= gH; H /= gH;
   10113             :   }
   10114        4424 :   G1 = gausssumcx(CHI1vec, prec);
   10115        4424 :   G2 = gausssumcx(CHI2vec, prec);
   10116        4424 :   if (!alchi)
   10117        3850 :     gel(v,1) = f00(k, CHI1vec,CHI2vec,G1,G2, mkvecsmall3(NC2,Cg,A*eg), prec);
   10118        4424 :   n = Fl_mul(alchi,U,cg); if (!n) n = cg;
   10119        4424 :   m = (n*H - alchi) / cg; /* positive, exact division */
   10120      456106 :   for (; m <= lim; n+=cg, m+=H)
   10121      451682 :     gel(v, m+1) = fg1g2n(n, k, CHI1vec, CHI2vec, data, z1,z2);
   10122        4424 :   t = (2*e)/g; if (odd(k)) t = -t;
   10123        4424 :   v = gdiv(v, gmul(conj_i(gmul(G1,G2)), mulsi(t, powuu(eg*N2/NC2, k-1))));
   10124        4424 :   if (k == 2 && N1 == 1 && N2 == 1) v = gsub(mkF2bd(wN,lim), gmulsg(e,v));
   10125             : 
   10126        4424 :   Qtoss(ALPHA, &na,&da);
   10127        4424 :   S = conj_i( mfcharmulcxeval(CHI1vec,CHI2vec,d,prec) ); /* CHI(1/d) */
   10128        4424 :   if (wN > 1)
   10129             :   {
   10130        3283 :     GEN z = rootsof1powinit(-mu, wN, prec);
   10131        3283 :     long i, l = lg(v);
   10132        3283 :     for (i = 1; i < l; i++) gel(v,i) = gmul(gel(v,i), rootsof1pow(z,i-1));
   10133             :   }
   10134        4424 :   v = RgV_Rg_mul(v, gmul(S, rootsof1q_cx(-mu*na, da, prec)));
   10135        4424 :   return mkvec2(ALPHA, bdexpand(v, w/wN));
   10136             : }
   10137             : 
   10138             : /*****************************************************************/
   10139             : /*                       END EISENSTEIN CUSPS                    */
   10140             : /*****************************************************************/
   10141             : 
   10142             : static GEN
   10143        1582 : mfchisimpl(GEN CHI)
   10144             : {
   10145             :   GEN G, chi;
   10146        1582 :   if (typ(CHI) == t_INT) return CHI;
   10147        1582 :   G = gel(CHI, 1); chi = gel(CHI, 2);
   10148        1582 :   switch(mfcharorder(CHI))
   10149             :   {
   10150        1134 :     case 1: chi = gen_1; break;
   10151         427 :     case 2: chi = znchartokronecker(G,chi,1); break;
   10152          21 :     default:chi = mkintmod(znconreyexp(G,chi), znstar_get_N(G)); break;
   10153             :   }
   10154        1582 :   return chi;
   10155             : }
   10156             : 
   10157             : GEN
   10158         700 : mfparams(GEN F)
   10159             : {
   10160         700 :   pari_sp av = avma;
   10161             :   GEN z, mf, CHI;
   10162         700 :   if ((mf = checkMF_i(F)))
   10163             :   {
   10164          14 :     long N = MF_get_N(mf);
   10165          14 :     GEN gk = MF_get_gk(mf);
   10166          14 :     CHI = MF_get_CHI(mf);
   10167          14 :     z = mkvec5(utoi(N), gk, CHI, utoi(MF_get_space(mf)), mfcharpol(CHI));
   10168             :   }
   10169             :   else
   10170             :   {
   10171         686 :     if (!checkmf_i(F)) pari_err_TYPE("mfparams", F);
   10172         686 :     z = vec_append(mf_get_NK(F), mfcharpol(mf_get_CHI(F)));
   10173             :   }
   10174         700 :   gel(z,3) = mfchisimpl(gel(z,3));
   10175         700 :   return gerepilecopy(av, z);
   10176             : }
   10177             : 
   10178             : GEN
   10179          14 : mfisCM(GEN F)
   10180             : {
   10181          14 :   pari_sp av = avma;
   10182             :   forprime_t S;
   10183             :   GEN D, v;
   10184             :   long N, k, lD, sb, p, i;
   10185          14 :   if (!checkmf_i(F)) pari_err_TYPE("mfisCM", F);
   10186          14 :   N = mf_get_N(F);
   10187          14 :   k = mf_get_k(F); if (N < 0 || k < 0) pari_err_IMPL("mfisCM for this F");
   10188          14 :   D = mfunram(N, -1);
   10189          14 :   lD = lg(D);
   10190          14 :   sb = maxss(mfsturmNk(N, k), 4*N);
   10191          14 :   v = mfcoefs_i(F, sb, 1);
   10192          14 :   u_forprime_init(&S, 2, sb);
   10193         518 :   while ((p = u_forprime_next(&S)))
   10194             :   {
   10195         490 :     GEN ap = gel(v, p+1);
   10196         490 :     if (!gequal0(ap))
   10197         406 :       for (i = 1; i < lD; i++)
   10198         245 :         if (kross(D[i], p) == -1) { D = vecsplice(D, i); lD--; }
   10199             :   }
   10200          14 :   if (lD == 1) { set_avma(av); return gen_0; }
   10201          14 :   if (lD == 2) { set_avma(av); return stoi(D[1]); }
   10202           7 :   if (k > 1) pari_err_BUG("mfisCM");
   10203           7 :   return gerepileupto(av, zv_to_ZV(D));
   10204             : }
   10205             : 
   10206             : static long
   10207         287 : mfspace_i(GEN mf, GEN F)
   10208             : {
   10209             :   GEN v, vF, gk;
   10210             :   long n, nE, i, l, s, N;
   10211             : 
   10212         287 :   mf = checkMF(mf); s = MF_get_space(mf);
   10213         287 :   if (!F) return s;
   10214         287 :   if (!checkmf_i(F)) pari_err_TYPE("mfspace",F);
   10215         287 :   v = mftobasis(mf, F, 1);
   10216         287 :   n = lg(v)-1; if (!n) return -1;
   10217         231 :   nE = lg(MF_get_E(mf))-1;
   10218         231 :   switch(s)
   10219             :   {
   10220          56 :     case mf_NEW: case mf_OLD: case mf_EISEN: return s;
   10221             :     case mf_FULL:
   10222         140 :       if (mf_get_type(F) == t_MF_THETA) return mf_EISEN;
   10223         133 :       if (!gequal0(vecslice(v,1,nE)))
   10224          63 :         return gequal0(vecslice(v,nE+1,n))? mf_EISEN: mf_FULL;
   10225             :   }
   10226             :   /* mf is mf_CUSP or mf_FULL, F a cusp form */
   10227         105 :   gk = mf_get_gk(F);
   10228         105 :   if (typ(gk) == t_FRAC || equali1(gk)) return mf_CUSP;
   10229          91 :   vF = mftonew_i(mf, vecslice(v, nE+1, n), &N);
   10230          91 :   if (N != MF_get_N(mf)) return mf_OLD;
   10231          63 :   l = lg(vF);
   10232         105 :   for (i = 1; i < l; i++)
   10233          63 :     if (itos(gmael(vF,i,1)) != N) return mf_CUSP;
   10234          42 :   return mf_NEW;
   10235             : }
   10236             : long
   10237         287 : mfspace(GEN mf, GEN F)
   10238         287 : { pari_sp av = avma; return gc_long(av, mfspace_i(mf,F)); }
   10239             : static GEN
   10240           7 : lfunfindchi(GEN ldata, GEN van, long prec)
   10241             : {
   10242           7 :   GEN gN = ldata_get_conductor(ldata), G = znstar0(gN,1), L, go, vz;
   10243           7 :   long k = ldata_get_k(ldata), N = itou(gN), bit = 10 - prec2nbits(prec);
   10244           7 :   long i, j, o, l, odd = k & 1, B0 = 2, B = lg(van)-1;
   10245             : 
   10246           7 :   van = shallowcopy(van);
   10247           7 :   L = cyc2elts(znstar_get_conreycyc(G));
   10248           7 :   l = lg(L);
   10249          21 :   for (i = j = 1; i < l; i++)
   10250             :   {
   10251          14 :     GEN chi = zc_to_ZC(gel(L,i));
   10252          14 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
   10253             :   }
   10254           7 :   setlg(L,j); l = j;
   10255           7 :   if (l <= 2) return gel(L,1);
   10256           0 :   o = znstar_get_expo(G); go = utoi(o);
   10257           0 :   vz = grootsof1(o, prec);
   10258             :   for (;;)
   10259           0 :   {
   10260             :     long n;
   10261           0 :     for (n = B0; n <= B; n++)
   10262             :     {
   10263           0 :       GEN an = gel(van,n), r;
   10264             :       long j;
   10265           0 :       if (ugcd(n, N) != 1 || gexpo(an) < bit) continue;
   10266           0 :       r = gdiv(an, conj_i(an));
   10267           0 :       for (i = 1; i < l; i++)
   10268             :       {
   10269           0 :         GEN CHI = gel(L,i);
   10270           0 :         if (gexpo(gsub(r, gel(vz, znchareval_i(CHI,n,go)+1))) > bit)
   10271           0 :           gel(L,i) = NULL;
   10272             :       }
   10273           0 :       for (i = j = 1; i < l; i++)
   10274           0 :         if (gel(L,i)) gel(L,j++) = gel(L,i);
   10275           0 :       l = j; setlg(L,l);
   10276           0 :       if (l == 2) return gel(L,1);
   10277             :     }
   10278           0 :     B0 = B+1; B <<= 1;
   10279           0 :     van = ldata_vecan(ldata_get_an(ldata), B, prec);
   10280             :   }
   10281             : }
   10282             : 
   10283             : GEN
   10284           7 : mffromlfun(GEN L, long prec)
   10285             : {
   10286           7 :   pari_sp av = avma;
   10287           7 :   GEN ldata = lfunmisc_to_ldata_shallow(L), Vga = ldata_get_gammavec(ldata);
   10288             :   GEN van, a0, CHI, NK;
   10289             :   long k, N, space;
   10290           7 :   if (!gequal(Vga, mkvec2(gen_0, gen_1))) pari_err_TYPE("mffromlfun", L);
   10291           7 :   k = ldata_get_k(ldata);
   10292           7 :   N = itou(ldata_get_conductor(ldata));
   10293           7 :   van = ldata_vecan(ldata_get_an(ldata), mfsturmNk(N,k) + 2, prec);
   10294           7 :   CHI = lfunfindchi(ldata, van, prec);
   10295           7 :   space = (lg(ldata) == 7)? mf_CUSP: mf_FULL;
   10296           7 :   a0 = (space == mf_CUSP)? gen_0: gneg(lfun(L, gen_0, prec2nbits(prec)));
   10297           7 :   NK = mkvec3(utoi(N), utoi(k), mfchisimpl(CHI));
   10298           7 :   return gerepilecopy(av, mkvec3(NK, utoi(space), shallowconcat(a0, van)));
   10299             : }
   10300             : /*******************************************************************/
   10301             : /*                                                                 */
   10302             : /*                       HALF-INTEGRAL WEIGHT                      */
   10303             : /*                                                                 */
   10304             : /*******************************************************************/
   10305             : /* We use the prefix mf2; k represents the weight -1/2, so e.g.
   10306             :    k = 2 is weight 5/2. N is the level, so 4\mid N, and CHI is the
   10307             :    character, always even. */
   10308             : 
   10309             : static long
   10310        3360 : lamCO(long r, long s, long p)
   10311             : {
   10312        3360 :   if ((s << 1) <= r)
   10313             :   {
   10314        1232 :     long rp = r >> 1;
   10315        1232 :     if (odd(r)) return upowuu(p, rp) << 1;
   10316         336 :     else return (p + 1)*upowuu(p, rp - 1);
   10317             :   }
   10318        2128 :   else return upowuu(p, r - s) << 1;
   10319             : }
   10320             : 
   10321             : static int
   10322        1568 : condC(GEN faN, GEN valF)
   10323             : {
   10324        1568 :   GEN P = gel(faN, 1), E = gel(faN, 2);
   10325        1568 :   long l = lg(P), i;
   10326        3696 :   for (i = 1; i < l; i++)
   10327        3024 :     if ((P[i] & 3L) == 3)
   10328             :     {
   10329        1120 :       long r = E[i];
   10330        1120 :       if (odd(r) || r < (valF[i] << 1)) return 1;
   10331             :     }
   10332         672 :   return 0;
   10333             : }
   10334             : 
   10335             : /* returns 2*zetaCO; weight is k + 1/2 */
   10336             : static long
   10337        3696 : zeta2CO(GEN faN, GEN valF, long r2, long s2, long k)
   10338             : {
   10339        3696 :   if (r2 >= 4) return lamCO(r2, s2, 2) << 1;
   10340        2912 :   if (r2 == 3) return 6;
   10341        1568 :   if (condC(faN, valF)) return 4;
   10342         672 :   if (odd(k)) return s2 ? 3 : 5; else return s2 ? 5: 3;
   10343             : }
   10344             : 
   10345             : /* returns 4 times last term in formula */
   10346             : static long
   10347        3696 : dim22(long N, long F, long k)
   10348             : {
   10349        3696 :   pari_sp av = avma;
   10350        3696 :   GEN vF, faN = myfactoru(N), P = gel(faN, 1), E = gel(faN, 2);
   10351        3696 :   long i, D, l = lg(P);
   10352        3696 :   vF = cgetg(l, t_VECSMALL);
   10353        3696 :   for (i = 1; i < l; i++) vF[i] = u_lval(F, P[i]);
   10354        3696 :   D = zeta2CO(faN, vF, E[1], vF[1], k);
   10355        3696 :   for (i = 2; i < l; i++) D *= lamCO(E[i], vF[i], P[i]);
   10356        3696 :   return gc_long(av,D);
   10357             : }
   10358             : 
   10359             : /* PSI not necessarily primitive, of conductor F */
   10360             : static int
   10361       13846 : charistotallyeven(GEN PSI, long F)
   10362             : {
   10363       13846 :   pari_sp av = avma;
   10364       13846 :   GEN P = gel(myfactoru(F), 1);
   10365       13846 :   GEN G = gel(PSI,1), psi = gel(PSI,2);
   10366             :   long i;
   10367       14350 :   for (i = 1; i < lg(P); i++)
   10368             :   {
   10369         532 :     GEN psip = znchardecompose(G, psi, utoipos(P[i]));
   10370         532 :     if (zncharisodd(G, psip)) return gc_bool(av,0);
   10371             :   }
   10372       13818 :   return gc_bool(av,1);
   10373             : }
   10374             : 
   10375             : static GEN
   10376      299775 : get_PSI(GEN CHI, long t)
   10377             : {
   10378      299775 :   long r = t & 3L, t2 = (r == 2 || r == 3) ? t << 2 : t;
   10379      299775 :   return mfcharmul_i(CHI, induce(gel(CHI,1), utoipos(t2)));
   10380             : }
   10381             : /* space = mf_CUSP, mf_EISEN or mf_FULL, weight k + 1/2 */
   10382             : static long
   10383       41363 : mf2dimwt12(long N, GEN CHI, long space)
   10384             : {
   10385       41363 :   pari_sp av = avma;
   10386       41363 :   GEN D = mydivisorsu(N >> 2);
   10387       41363 :   long i, l = lg(D), dim3 = 0, dim4 = 0;
   10388             : 
   10389       41363 :   CHI = induceN(N, CHI);
   10390      341138 :   for (i = 1; i < l; i++)
   10391             :   {
   10392      299775 :     long rp, t = D[i], Mt = D[l-i];
   10393      299775 :     GEN PSI = get_PSI(CHI,t);
   10394      299775 :     rp = mfcharconductor(PSI);
   10395      299775 :     if (Mt % (rp*rp) == 0) { dim4++; if (charistotallyeven(PSI,rp)) dim3++; }
   10396             :   }
   10397       41363 :   set_avma(av);
   10398       41363 :   switch (space)
   10399             :   {
   10400       40439 :     case mf_CUSP: return dim4 - dim3;
   10401         462 :     case mf_EISEN:return dim3;
   10402         462 :     case mf_FULL: return dim4;
   10403             :   }
   10404             :   return 0; /*LCOV_EXCL_LINE*/
   10405             : }
   10406             : 
   10407             : static long
   10408         693 : mf2dimwt32(long N, GEN CHI, long F, long space)
   10409             : {
   10410             :   long D;
   10411         693 :   switch(space)
   10412             :   {
   10413         231 :     case mf_CUSP: D = mypsiu(N) - 6*dim22(N, F, 1);
   10414         231 :       if (D%24) pari_err_BUG("mfdim");
   10415         231 :       return D/24 + mf2dimwt12(N, CHI, 4);
   10416         231 :     case mf_FULL: D = mypsiu(N) + 6*dim22(N, F, 0);
   10417         231 :       if (D%24) pari_err_BUG("mfdim");
   10418         231 :       return D/24 + mf2dimwt12(N, CHI, 1);
   10419         231 :     case mf_EISEN: D = dim22(N, F, 0) + dim22(N, F, 1);
   10420         231 :       if (D & 3L) pari_err_BUG("mfdim");
   10421         231 :       return (D >> 2) - mf2dimwt12(N, CHI, 3);
   10422             :   }
   10423             :   return 0; /*LCOV_EXCL_LINE*/
   10424             : }
   10425             : 
   10426             : /* F = conductor(CHI), weight k = r+1/2 */
   10427             : static long
   10428       43673 : checkmf2(long N, long r, GEN CHI, long F, long space)
   10429             : {
   10430       43673 :   switch(space)
   10431             :   {
   10432       43652 :     case mf_FULL: case mf_CUSP: case mf_EISEN: break;
   10433             :     case mf_NEW: case mf_OLD:
   10434          14 :       pari_err_TYPE("half-integral weight [new/old spaces]", utoi(space));
   10435             :     default:
   10436           7 :       pari_err_TYPE("half-integral weight [incorrect space]",utoi(space));
   10437             :   }
   10438       43652 :   if (N & 3L)
   10439           0 :     pari_err_DOMAIN("half-integral weight", "N % 4", "!=", gen_0, stoi(N));
   10440       43652 :   return r >= 0 && mfcharparity(CHI) == 1 && N % F == 0;
   10441             : }
   10442             : 
   10443             : /* weight k = r + 1/2 */
   10444             : static long
   10445       43463 : mf2dim_Nkchi(long N, long r, GEN CHI, ulong space)
   10446             : {
   10447       43463 :   long D, D2, F = mfcharconductor(CHI);
   10448       43463 :   if (!checkmf2(N, r, CHI, F, space)) return 0;
   10449       43442 :   if (r == 0) return mf2dimwt12(N, CHI, space);
   10450        2772 :   if (r == 1) return mf2dimwt32(N, CHI, F, space);
   10451        2079 :   if (space == mf_EISEN)
   10452             :   {
   10453         693 :     D = dim22(N, F, r) + dim22(N, F, 1-r);
   10454         693 :     if (D & 3L) pari_err_BUG("mfdim");
   10455         693 :     return D >> 2;
   10456             :   }
   10457        1386 :   D2 = space == mf_FULL? dim22(N, F, 1-r): -dim22(N, F, r);
   10458        1386 :   D = (2*r-1)*mypsiu(N) + 6*D2;
   10459        1386 :   if (D%24) pari_err_BUG("mfdim");
   10460        1386 :   return D/24;
   10461             : }
   10462             : 
   10463             : /* weight k=r+1/2 */
   10464             : static GEN
   10465         210 : mf2init_Nkchi(long N, long r, GEN CHI, long space, long flraw)
   10466             : {
   10467         210 :   GEN CHI1, Minv, Minvmat, B, M, gk = gaddsg(r,ghalf);
   10468         210 :   GEN mf1 = mkvec4(utoi(N),gk,CHI,utoi(space));
   10469             :   long L;
   10470         210 :   if (!checkmf2(N, r, CHI, mfcharconductor(CHI), space)) return mfEMPTY(mf1);
   10471         210 :   if (space==mf_EISEN) pari_err_IMPL("half-integral weight Eisenstein space");
   10472         210 :   L = mfsturmNgk(N, gk) + 1;
   10473         210 :   B = mf2basis(N, r, CHI, &CHI1, space);
   10474         210 :   M = mflineardivtomat(N,B,L); /* defined modulo T = charpol(CHI) */
   10475         210 :   if (flraw) M = mkvec3(gen_0,gen_0,M);
   10476             :   else
   10477             :   {
   10478         210 :     long o1 = mfcharorder(CHI1), o = mfcharorder(CHI);
   10479         210 :     M = mfcleanCHI(M, CHI, 0);
   10480         210 :     Minv = gel(M,2);
   10481         210 :     Minvmat = RgM_Minv_mul(NULL, Minv); /* mod T */
   10482         210 :     if (o1 != o)
   10483             :     {
   10484         112 :       GEN tr = Qab_trace_init(mfcharpol(CHI), o, o1);
   10485         112 :       Minvmat = QabM_tracerel(tr, 0, Minvmat);
   10486             :     }
   10487             :     /* Minvmat mod T1 = charpol(CHI1) */
   10488         210 :     B = vecmflineardiv_linear(B, Minvmat);
   10489         210 :     gel(M,3) = RgM_Minv_mul(gel(M,3), Minv);
   10490         210 :     gel(M,2) = mkMinv(matid(lg(B)-1), NULL,NULL,NULL);
   10491             :   }
   10492         210 :   return mkmf(mf1, cgetg(1,t_VEC), B, gen_0, M);
   10493             : }
   10494             : 
   10495             : /**************************************************************************/
   10496             : /*                          Kohnen + space                                */
   10497             : /**************************************************************************/
   10498             : 
   10499             : static GEN
   10500          21 : mfkohnenbasis_i(GEN mf, GEN CHIP, long eps, long sb)
   10501             : {
   10502          21 :   GEN M = shallowtrans(mfcoefs_mf(mf, sb, 1)), ME;
   10503             :   long c, i, n;
   10504          21 :   ME = cgetg(sb + 2, t_MAT);
   10505         784 :   for (i = 0, c = 1; i <= sb; i++)
   10506             :   {
   10507         763 :     long j = i & 3L;
   10508         763 :     if (j == 2 || j == 2 + eps) gel(ME, c++) = gel(M, i+1);
   10509             :   }
   10510          21 :   setlg(ME, c); ME = shallowtrans(Q_primpart(ME));
   10511          21 :   n = mfcharorder(CHIP);
   10512          21 :   return n <= 2? ZM_ker(ME): ZabM_ker(liftpol_shallow(ME), mfcharpol(CHIP), n);
   10513             : }
   10514             : GEN
   10515          21 : mfkohnenbasis(GEN mf)
   10516             : {
   10517          21 :   pari_sp av = avma;
   10518             :   GEN gk, CHI, CHIP, K;
   10519             :   long N4, r, eps, sb;
   10520          21 :   mf = checkMF(mf);
   10521          21 :   if (MF_get_space(mf) != mf_CUSP)
   10522           0 :     pari_err_TYPE("mfkohnenbasis [not a cuspidal space", mf);
   10523          21 :   if (!MF_get_dim(mf)) return cgetg(1, t_MAT);
   10524          21 :   N4 = MF_get_N(mf) >> 2; gk = MF_get_gk(mf); CHI = MF_get_CHI(mf);
   10525          21 :   if (typ(gk) == t_INT) pari_err_TYPE("mfkohnenbasis", gk);
   10526          21 :   r = MF_get_r(mf);
   10527          21 :   CHIP = mfcharchiliftprim(CHI, N4);
   10528          21 :   eps = CHIP==CHI? 1: -1;
   10529          21 :   if (!CHIP) pari_err_TYPE("mfkohnenbasis [incorrect CHI]", CHI);
   10530          21 :   if (odd(r)) eps = -eps;
   10531          21 :   if (uissquarefree(N4))
   10532             :   {
   10533          14 :     long d = mfdim_Nkchi(N4, 2*r, mfcharpow(CHI, gen_2), mf_CUSP);
   10534          14 :     sb = mfsturmNgk(N4 << 2, gk) + 1;
   10535          14 :     K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10536          14 :     if (lg(K) - 1 == d) return gerepilecopy(av, K);
   10537             :   }
   10538           7 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
   10539           7 :   K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10540           7 :   return gerepilecopy(av, K);
   10541             : }
   10542             : 
   10543             : /* return [mf3, bijection, mfkohnenbasis, codeshi] */
   10544             : static GEN
   10545          14 : mfkohnenbijection_i(GEN mf)
   10546             : {
   10547          14 :   GEN vB, mf3, K, SHI, P, CHI = MF_get_CHI(mf);
   10548          14 :   long n, lK, i, dim, m, lw, sb3, N4 = MF_get_N(mf)>>2, r = MF_get_r(mf);
   10549          14 :   long Dp[] = {1, 5, 8, 12, 13, 17, 21, 24};
   10550          14 :   long Dm[] = {-3, -4, -7, -8, -11, -15, -19, -20}, *D = odd(r)? Dm: Dp;
   10551          14 :   const long nbD = 8, MAXm = 6560; /* #D, 3^#D - 1 */
   10552             : 
   10553          14 :   K = mfkohnenbasis(mf); lK = lg(K);
   10554          14 :   mf3 = mfinit_Nkchi(N4, r<<1, mfcharpow(CHI,gen_2), mf_CUSP, 0);
   10555          14 :   if (MF_get_dim(mf3) != lK - 1)
   10556           0 :     pari_err_BUG("mfkohnenbijection [different dimensions]");
   10557          14 :   if (lK == 1) return mkvec4(mf3, cgetg(1, t_MAT), K, cgetg(1, t_VEC));
   10558          14 :   CHI = mfcharchiliftprim(CHI, N4);
   10559          14 :   if (!CHI) pari_err_TYPE("mfkohnenbijection [incorrect CHI]", CHI);
   10560          14 :   n = mfcharorder(CHI);
   10561          14 :   P = n<=2? NULL: mfcharpol(CHI);
   10562          14 :   SHI = cgetg(nbD+1, t_VEC);
   10563          14 :   sb3 = mfsturm(mf3);
   10564          14 :   vB = RgM_mul(mfcoefs_mf(mf, labs(D[nbD-1])*sb3*sb3, 1), K);
   10565          14 :   dim = MF_get_dim(mf3);
   10566          35 :   for (m = 1, lw = 0; m <= MAXm; m += (m%3)? 2: 1)
   10567             :   {
   10568             :     pari_sp av;
   10569          35 :     ulong m1, y, v = u_lvalrem(m, 3, &y);
   10570             :     GEN z, M;
   10571             :     long j;
   10572          35 :     if (y == 1)
   10573             :     {
   10574          28 :       long d = D[v];
   10575          28 :       GEN a = cgetg(lK, t_MAT);
   10576          98 :       for (i = 1; i < lK; i++)
   10577             :       {
   10578          70 :         pari_sp av2 = avma;
   10579          70 :         GEN f = c_deflate(sb3*sb3, labs(d), gel(vB,i));
   10580          70 :         f = mftobasis_i(mf3, RgV_shimura(f, sb3, d, N4, r, CHI));
   10581          70 :         gel(a,i) = gerepileupto(av2, f);
   10582             :       }
   10583          28 :       lw++; gel(SHI,v+1) = a;
   10584             :     }
   10585          35 :     av = avma; M = NULL;
   10586          91 :     for (j = 1, m1 = m; j <= lw; j++, m1/=3)
   10587             :     {
   10588          56 :       long s = m1%3;
   10589          56 :       if (s)
   10590             :       {
   10591          42 :         GEN t = gel(SHI,j);
   10592          42 :         if (M) M = (s == 2)? Rg