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.1 lcov report (development 24782-f7724578b4) Lines: 7384 7561 97.7 %
Date: 2019-12-06 05:56:26 Functions: 756 760 99.5 %
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 Qab_Czeta(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       29414 : mkgNK(GEN N, GEN k, GEN CHI, GEN P) { return mkvec4(N, k, CHI, P); }
      79             : static GEN
      80       13013 : mkNK(long N, long k, GEN CHI) { return mkgNK(stoi(N), stoi(k), CHI, pol_x(1)); }
      81             : GEN
      82        7420 : MF_get_CHI(GEN mf) { return gmael(mf,1,3); }
      83             : GEN
      84       18277 : MF_get_gN(GEN mf) { return gmael(mf,1,1); }
      85             : long
      86       17430 : MF_get_N(GEN mf) { return itou(MF_get_gN(mf)); }
      87             : GEN
      88       12558 : MF_get_gk(GEN mf) { return gmael(mf,1,2); }
      89             : long
      90        6216 : MF_get_k(GEN mf)
      91             : {
      92        6216 :   GEN gk = MF_get_gk(mf);
      93        6216 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
      94        6216 :   return itou(gk);
      95             : }
      96             : long
      97         210 : MF_get_r(GEN mf)
      98             : {
      99         210 :   GEN gk = MF_get_gk(mf);
     100         210 :   if (typ(gk) == t_INT) pari_err_IMPL("integral weight");
     101         210 :   return itou(gel(gk, 1)) >> 1;
     102             : }
     103             : long
     104       12656 : MF_get_space(GEN mf) { return itos(gmael(mf,1,4)); }
     105             : GEN
     106        3794 : MF_get_E(GEN mf) { return gel(mf,2); }
     107             : GEN
     108       18732 : MF_get_S(GEN mf) { return gel(mf,3); }
     109             : GEN
     110        1274 : MF_get_basis(GEN mf) { return shallowconcat(gel(mf,2), gel(mf,3)); }
     111             : long
     112        4648 : MF_get_dim(GEN mf)
     113             : {
     114        4648 :   switch(MF_get_space(mf))
     115             :   {
     116             :     case mf_FULL:
     117         658 :       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        3850 :       return lg(MF_get_S(mf)) - 1;
     122             :   }
     123             : }
     124             : GEN
     125        6727 : MFnew_get_vj(GEN mf) { return gel(mf,4); }
     126             : GEN
     127         497 : MFcusp_get_vMjd(GEN mf) { return gel(mf,4); }
     128             : GEN
     129        6202 : MF_get_M(GEN mf) { return gmael(mf,5,3); }
     130             : GEN
     131        2506 : MF_get_Minv(GEN mf) { return gmael(mf,5,2); }
     132             : GEN
     133        8043 : MF_get_Mindex(GEN mf) { return gmael(mf,5,1); }
     134             : 
     135             : /* ordinary gtocol forgets about initial 0s */
     136             : GEN
     137        1603 : 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         763 : QabM_init(long n, ulong *p)
     144             : {
     145         763 :   ulong pinit = 1000000007;
     146             :   forprime_t T;
     147         763 :   if (n <= 1) { *p = pinit; return 0; }
     148         756 :   u_forprime_arith_init(&T, pinit, ULONG_MAX, 1, n);
     149         756 :   *p = u_forprime_next(&T);
     150         756 :   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         966 : ZabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *den, int ratlift)
     194             : {
     195         966 :   GEN v = ZabM_indexrank(M, P, n);
     196         966 :   if (pv) *pv = v;
     197         966 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
     198         966 :   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        1659 : QabM_ker(GEN M, GEN P, long n)
     205             : {
     206             :   GEN B;
     207        1659 :   if (n <= 2)
     208         924 :     B = ZM_ker(Q_primpart(M));
     209             :   else
     210         735 :     B = ZabM_ker(Q_primpart(liftpol_shallow(M)), P, n);
     211        1659 :   return B;
     212             : }
     213             : /* pseudo-inverse of M. FIXME: should replace QabM_pseudoinv */
     214             : static GEN
     215        1204 : QabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     216             : {
     217             :   GEN cM, Mi;
     218        1204 :   if (n <= 2)
     219             :   {
     220         917 :     M = Q_primitive_part(M, &cM);
     221         917 :     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        1204 :   *pden = mul_content(*pden, cM);
     229        1204 :   return Mi;
     230             : }
     231             : /* FIXME: delete */
     232             : static GEN
     233         994 : QabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     234             : {
     235         994 :   GEN Mi = QabM_pseudoinv_i(M, P, n, pv, pden);
     236         994 :   return P? gmodulo(Mi, P): Mi;
     237             : }
     238             : 
     239             : static GEN
     240        9632 : QabM_indexrank(GEN M, GEN P, long n)
     241             : {
     242             :   GEN z;
     243        9632 :   if (n <= 2)
     244             :   {
     245        8519 :     M = vec_Q_primpart(M);
     246        8519 :     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        9632 :   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       98077 : myeulerphiu(ulong n)
     263             : {
     264             :   pari_sp av;
     265       98077 :   if (n == 1) return 1;
     266       82453 :   av = avma; return gc_ulong(av, eulerphiu_fact(myfactoru(n)));
     267             : }
     268             : static long
     269       65562 : mymoebiusu(ulong n)
     270             : {
     271             :   pari_sp av;
     272       65562 :   if (n == 1) return 1;
     273       54068 :   av = avma; return gc_long(av, moebiusu_fact(myfactoru(n)));
     274             : }
     275             : 
     276             : static long
     277        2828 : mynumdivu(long N)
     278             : {
     279             :   pari_sp av;
     280        2828 :   if (N == 1) return 1;
     281        2723 :   av = avma; return gc_long(av, numdivu_fact(myfactoru(N)));
     282             : }
     283             : 
     284             : /* N\prod_{p|N} (1+1/p) */
     285             : static long
     286      339164 : mypsiu(ulong N)
     287             : {
     288      339164 :   pari_sp av = avma;
     289      339164 :   GEN P = gel(myfactoru(N), 1);
     290      339164 :   long j, l = lg(P), res = N;
     291      339164 :   for (j = 1; j < l; j++) res += res/P[j];
     292      339164 :   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     8253259 : corediscs_fact(GEN fa)
     313             : {
     314     8253259 :   GEN P = gel(fa,1), E = gel(fa,2);
     315     8253259 :   long i, l = lg(P), m = 1;
     316    27307672 :   for (i = 1; i < l; i++)
     317             :   {
     318    19054413 :     long p = P[i], e = E[i];
     319    19054413 :     if (e & 1) m *= p;
     320             :   }
     321     8253259 :   if ((m&3L) != 3) m <<= 2;
     322     8253259 :   return m;
     323             : }
     324             : static long
     325        6335 : mubeta(long n)
     326             : {
     327        6335 :   pari_sp av = avma;
     328        6335 :   GEN E = gel(myfactoru(n), 2);
     329        6335 :   long i, s = 1, l = lg(E);
     330       13132 :   for (i = 1; i < l; i++)
     331             :   {
     332        6797 :     long e = E[i];
     333        6797 :     if (e >= 3) return gc_long(av,0);
     334        6797 :     if (e == 1) s *= -2;
     335             :   }
     336        6335 :   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     5967745 : mubeta2(long n, long m)
     343             : {
     344     5967745 :   pari_sp av = avma;
     345     5967745 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     346     5967745 :   long i, s = 1, l = lg(P);
     347    11944408 :   for (i = 1; i < l; i++)
     348             :   {
     349     5976663 :     long p = P[i], e = E[i];
     350     5976663 :     if (m % p)
     351             :     { /* p^e in n1 */
     352     4881499 :       if (e >= 3) return gc_long(av,0);
     353     4881499 :       if (e == 1) s *= -2;
     354             :     }
     355             :     else
     356             :     { /* in n2 */
     357     1095164 :       if (e >= 2) return gc_long(av,0);
     358     1095164 :       s = -s;
     359             :     }
     360             :   }
     361     5967745 :   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     1440334 : newt_params(long N, long n, long FC, long *pg, long *pN2)
     369             : {
     370     1440334 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     371     1440334 :   long i, g = 1, N2 = 1, l = lg(P);
     372     3809372 :   for (i = 1; i < l; i++)
     373             :   {
     374     2369038 :     long p = P[i], e = E[i];
     375     2369038 :     if (e == 1)
     376     2038435 :     { if (FC % p && n % (p*p) == 0) g *= p; }
     377             :     else
     378      330603 :       N2 *= upowuu(p,(n % p)? e-2: e-1);
     379             :   }
     380     1440334 :   *pg = g; *pN2 = N2;
     381     1440334 : }
     382             : /* simplified version of newt_params for n = 1 (newdim) */
     383             : static void
     384       36813 : newd_params(long N, long *pN2)
     385             : {
     386       36813 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     387       36813 :   long i, N2 = 1, l = lg(P);
     388       93576 :   for (i = 1; i < l; i++)
     389             :   {
     390       56763 :     long p = P[i], e = E[i];
     391       56763 :     if (e > 2) N2 *= upowuu(p, e-2);
     392             :   }
     393       36813 :   *pN2 = N2;
     394       36813 : }
     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       36834 : phipart(long g, long q)
     415             : {
     416       36834 :   if (g > 1)
     417             :   {
     418       19642 :     GEN P = gel(myfactoru(g), 1);
     419       19642 :     long i, l = lg(P);
     420       19642 :     for (i = 1; i < l; i++) { long p = P[i]; if (q % p) g -= g / p; }
     421             :   }
     422       36834 :   return g;
     423             : }
     424             : /* Set s,v s.t. Trace(zeta_N^k) from Q(zeta_N) to Q(\zeta_N) = s * zeta_M^v
     425             :  * With k > 0, N = M*d and N, M != 2 mod 4 */
     426             : static long
     427       84609 : tracerelz(long *pv, long d, long M, long k)
     428             : {
     429             :   long s, g, q, muq;
     430       84609 :   if (d == 1) { *pv = k; return 1; }
     431       65471 :   *pv = 0; g = ugcd(k, d); q = d / g;
     432       65471 :   muq = mymoebiusu(q); if (!muq) return 0;
     433       47096 :   if (M != 1)
     434             :   {
     435       37758 :     long v = Fl_invsafe(q % M, M);
     436       37758 :     if (!v) return 0;
     437       27496 :     *pv = (v * (k/g)) % M;
     438             :   }
     439       36834 :   s = phipart(g, M*q); if (muq < 0) s = -s;
     440       36834 :   return s;
     441             : }
     442             : /* Pi = polcyclo(i), i = m or n. Let Ki = Q(zeta_i), initialize Tr_{Kn/Km} */
     443             : GEN
     444       33362 : Qab_trace_init(long n, long m, GEN Pn, GEN Pm)
     445             : {
     446             :   long a, i, j, N, M, vt, d, D;
     447             :   GEN T, G;
     448             : 
     449       33362 :   if (m == n || n <= 2) return mkvec(Pm);
     450       16485 :   vt = varn(Pn);
     451       16485 :   d = degpol(Pn);
     452             :   /* if (N != n) zeta_N = zeta_n^2 and zeta_n = - zeta_N^{(N+1)/2} */
     453       16485 :   N = ((n & 3) == 2)? n >> 1: n;
     454       16485 :   M = ((m & 3) == 2)? m >> 1: m; /* M | N | n */
     455       16485 :   a = N / M;
     456       16485 :   T = const_vec(d, NULL);
     457       16485 :   D = d / degpol(Pm); /* relative degree */
     458       16485 :   if (D == 1) G = NULL;
     459             :   else
     460             :   { /* zeta_M = zeta_n^A; s_j(zeta_M) = zeta_M <=> j = 1 (mod J) */
     461       15211 :     long lG, A = (N == n)? a: (a << 1), J = n / ugcd(n, A);
     462       15211 :     G = coprimes_zv(n);
     463      149912 :     for (j = lG = 1; j < n; j += J)
     464      134701 :       if (G[j]) G[lG++] = j;
     465       15211 :     setlg(G, lG); /* Gal(Q(zeta_n) / Q(zeta_m)) */
     466             :   }
     467       16485 :   T = const_vec(d, NULL);
     468       16485 :   gel(T,1) = utoipos(D); /* Tr 1 */
     469      139902 :   for (i = 1; i < d; i++)
     470             :   { /* if n = 2N, zeta_n^i = (-1)^i zeta_N^k */
     471             :     long s, v, k;
     472             :     GEN t;
     473             : 
     474      123417 :     if (gel(T, i+1)) continue;
     475       84609 :     k = (N == n)? i: ((odd(i)? i + N: i) >> 1);
     476       84609 :     if ((s = tracerelz(&v, a, M, k)))
     477             :     {
     478       55972 :       if (m != M) v *= 2;/* Tr = s * zeta_m^v */
     479       55972 :       if (n != N && odd(i)) s = -s;
     480       55972 :       t = Qab_Czeta(v, m, stoi(s), vt);
     481             :     }
     482             :     else
     483       28637 :       t = gen_0;
     484             :     /* t = Tr_{Kn/Km} zeta_n^i; fill using Galois action */
     485       84609 :     if (!G)
     486       19138 :       gel(T, i + 1) = t;
     487             :     else
     488      370349 :       for (j = 1; j <= D; j++)
     489             :       {
     490      304878 :         long z = Fl_mul(i,G[j], n);
     491      304878 :         if (z < d) gel(T, z + 1) = t;
     492             :       }
     493             :   }
     494       16485 :   return mkvec3(Pm, Pn, T);
     495             : }
     496             : /* x a t_POL modulo Phi_n */
     497             : static GEN
     498       61698 : tracerel_i(GEN T, GEN x)
     499             : {
     500       61698 :   long k, l = lg(x);
     501             :   GEN S;
     502       61698 :   if (l == 2) return gen_0;
     503       61698 :   S = gmul(gel(T,1), gel(x,2));
     504       61698 :   for (k = 3; k < l; k++) S = gadd(S, gmul(gel(T,k-1), gel(x,k)));
     505       61698 :   return S;
     506             : }
     507             : static GEN
     508      131740 : tracerel(GEN a, GEN v, GEN z)
     509             : {
     510      131740 :   a = liftpol_shallow(a);
     511      131740 :   a = simplify_shallow(z? gmul(z,a): a);
     512      131740 :   if (typ(a) == t_POL)
     513             :   {
     514       61698 :     GEN T = gel(v,3);
     515       61698 :     long degrel = itou(gel(T,1));
     516       61698 :     a = tracerel_i(T, RgX_rem(a, gel(v,2)));
     517       61698 :     if (degrel != 1) a = gdivgs(a, degrel);
     518       61698 :     if (typ(a) == t_POL) a = RgX_rem(a, gel(v,1));
     519             :   }
     520      131740 :   return a;
     521             : }
     522             : static GEN
     523        5362 : tracerel_z(GEN v, long t)
     524             : {
     525        5362 :   GEN Pn = gel(v,2);
     526        5362 :   return t? pol_xn(t, varn(Pn)): NULL;
     527             : }
     528             : /* v = Qab_trace_init(n,m); x is a t_VEC of polmodulo Phi_n; Kn = Q(zeta_n)
     529             :  * [Kn:Km]^(-1) Tr_{Kn/Km} (zeta_n^t * x); 0 <= t < [Kn:Km] */
     530             : GEN
     531           0 : Qab_tracerel(GEN v, long t, GEN a)
     532             : {
     533           0 :   if (lg(v) != 4) return a; /* => t = 0 */
     534           0 :   return tracerel(a, v, tracerel_z(v, t));
     535             : }
     536             : GEN
     537       10710 : QabV_tracerel(GEN v, long t, GEN x)
     538             : {
     539             :   GEN z;
     540       10710 :   if (lg(v) != 4) return x; /* => t = 0 */
     541        5362 :   z = tracerel_z(v, t);
     542        5362 :   pari_APPLY_same(tracerel(gel(x,i), v, z));
     543             : }
     544             : GEN
     545         133 : QabM_tracerel(GEN v, long t, GEN x)
     546             : {
     547         133 :   if (lg(v) != 4) return x;
     548          21 :   pari_APPLY_same(QabV_tracerel(v, t, gel(x,i)));
     549             : }
     550             : 
     551             : /* C*zeta_o^k mod X^o - 1 */
     552             : static GEN
     553      928655 : Qab_Czeta(long k, long o, GEN C, long vt)
     554             : {
     555      928655 :   if (!k) return C;
     556      529466 :   if (!odd(o))
     557             :   { /* optimization: reduce max degree by a factor 2 for free */
     558      467572 :     o >>= 1;
     559      467572 :     if (k >= o) { k -= o; C = gneg(C); if (!k) return C; }
     560             :   }
     561      381962 :   return monomial(C, k, vt);
     562             : }
     563             : /* zeta_o^k */
     564             : static GEN
     565      157465 : Qab_zeta(long k, long o, long vt) { return Qab_Czeta(k, o, gen_1, vt); }
     566             : 
     567             : /*              Operations on Dirichlet characters                       */
     568             : 
     569             : /* A Dirichlet character can be given in GP in different formats, but in this
     570             :  * package, it will be a vector CHI=[G,chi,ord], where G is the (Z/MZ)^* to
     571             :  * which the character belongs, chi is the character in Conrey format, ord is
     572             :  * the order */
     573             : 
     574             : static GEN
     575     1460319 : gmfcharorder(GEN CHI) { return gel(CHI, 3); }
     576             : long
     577     1425844 : mfcharorder(GEN CHI) { return itou(gmfcharorder(CHI)); }
     578             : static long
     579        9471 : mfcharistrivial(GEN CHI) { return !CHI || mfcharorder(CHI) == 1; }
     580             : static GEN
     581      762230 : gmfcharmodulus(GEN CHI) { return gmael3(CHI, 1, 1, 1); }
     582             : long
     583      762230 : mfcharmodulus(GEN CHI) { return itou(gmfcharmodulus(CHI)); }
     584             : GEN
     585      266021 : mfcharpol(GEN CHI) { return gel(CHI,4); }
     586             : 
     587             : /* vz[i+1] = image of (zeta_o)^i in Fp */
     588             : static ulong
     589      228858 : Qab_Czeta_Fl(long k, GEN vz, ulong C, ulong p)
     590             : {
     591             :   long o;
     592      228858 :   if (!k) return C;
     593      153811 :   o = lg(vz)-2;
     594      153811 :   if ((k << 1) == o) return Fl_neg(C,p);
     595      127792 :   return Fl_mul(C, vz[k+1], p);
     596             : }
     597             : 
     598             : static long
     599      738710 : znchareval_i(GEN CHI, long n, GEN ord)
     600      738710 : { return itos(znchareval(gel(CHI,1), gel(CHI,2), stoi(n), ord)); }
     601             : 
     602             : /* n coprime with the modulus of CHI */
     603             : static GEN
     604       11669 : mfchareval(GEN CHI, long n)
     605             : {
     606       11669 :   GEN Pn, C, go = gmfcharorder(CHI);
     607       11669 :   long k, o = go[2];
     608       11669 :   if (o == 1) return gen_1;
     609        5999 :   k = znchareval_i(CHI, n, go);
     610        5999 :   Pn = mfcharpol(CHI);
     611        5999 :   C = Qab_zeta(k, o, varn(Pn));
     612        5999 :   if (typ(C) != t_POL) return C;
     613        4872 :   return gmodulo(C, Pn);
     614             : }
     615             : /* d a multiple of ord(CHI); n coprime with char modulus;
     616             :  * return x s.t. CHI(n) = \zeta_d^x] */
     617             : static long
     618     1314544 : mfcharevalord(GEN CHI, long n, long d)
     619             : {
     620     1314544 :   if (mfcharorder(CHI) == 1) return 0;
     621      729015 :   return znchareval_i(CHI, n, utoi(d));
     622             : }
     623             : 
     624             : /* G a znstar, L a Conrey log: return a 'mfchar' */
     625             : static GEN
     626      370160 : mfcharGL(GEN G, GEN L)
     627             : {
     628      370160 :   GEN o = zncharorder(G,L);
     629      370160 :   long ord = itou(o), vt = fetch_user_var("t");
     630      370160 :   return mkvec4(G, L, o, polcyclo(ord,vt));
     631             : }
     632             : static GEN
     633        4942 : mfchartrivial()
     634        4942 : { return mfcharGL(znstar0(gen_1,1), cgetg(1,t_COL)); }
     635             : /* convert a generic character into an 'mfchar' */
     636             : static GEN
     637        3899 : get_mfchar(GEN CHI)
     638             : {
     639             :   GEN G, L;
     640        3899 :   if (typ(CHI) != t_VEC) CHI = znchar(CHI);
     641             :   else
     642             :   {
     643         875 :     long l = lg(CHI);
     644         875 :     if ((l != 3 && l != 5) || !checkznstar_i(gel(CHI,1)))
     645           7 :       pari_err_TYPE("checkNF [chi]", CHI);
     646         868 :     if (l == 5) return CHI;
     647             :   }
     648        3829 :   G = gel(CHI,1);
     649        3829 :   L = gel(CHI,2); if (typ(L) != t_COL) L = znconreylog(G,L);
     650        3829 :   return mfcharGL(G, L);
     651             : }
     652             : 
     653             : /* parse [N], [N,k], [N,k,CHI]. If 'joker' is set, allow wildcard for CHI */
     654             : static GEN
     655        9058 : checkCHI(GEN NK, long N, int joker)
     656             : {
     657             :   GEN CHI;
     658        9058 :   if (lg(NK) == 3)
     659         623 :     CHI = mfchartrivial();
     660             :   else
     661             :   {
     662             :     long i, l;
     663        8435 :     CHI = gel(NK,3); l = lg(CHI);
     664        8435 :     if (isintzero(CHI) && joker)
     665        4095 :       CHI = NULL; /* all character orbits */
     666        4340 :     else if (isintm1(CHI) && joker > 1)
     667        2373 :       CHI = gen_m1; /* sum over all character orbits */
     668        2100 :     else if ((typ(CHI) == t_VEC &&
     669         196 :              (l == 1 || l != 3 || !checkznstar_i(gel(CHI,1)))) && joker)
     670             :     {
     671         133 :       CHI = shallowtrans(CHI); /* list of characters */
     672         133 :       for (i = 1; i < l; i++) gel(CHI,i) = get_mfchar(gel(CHI,i));
     673             :     }
     674             :     else
     675             :     {
     676        1834 :       CHI = get_mfchar(CHI); /* single char */
     677        1834 :       if (N % mfcharmodulus(CHI)) pari_err_TYPE("checkNF [chi]", NK);
     678             :     }
     679             :   }
     680        9044 :   return CHI;
     681             : }
     682             : /* support half-integral weight */
     683             : static void
     684        9065 : checkNK2(GEN NK, long *N, long *nk, long *dk, GEN *CHI, int joker)
     685             : {
     686        9065 :   long l = lg(NK);
     687             :   GEN T;
     688        9065 :   if (typ(NK) != t_VEC || l < 3 || l > 4) pari_err_TYPE("checkNK", NK);
     689        9065 :   T = gel(NK,1); if (typ(T) != t_INT) pari_err_TYPE("checkNF [N]", NK);
     690        9065 :   *N = itos(T); if (*N <= 0) pari_err_TYPE("checkNF [N <= 0]", NK);
     691        9065 :   T = gel(NK,2);
     692        9065 :   switch(typ(T))
     693             :   {
     694        5705 :     case t_INT:  *nk = itos(T); *dk = 1; break;
     695             :     case t_FRAC:
     696        3353 :       *nk = itos(gel(T,1));
     697        3353 :       *dk = itou(gel(T,2)); if (*dk == 2) break;
     698           7 :     default: pari_err_TYPE("checkNF [k]", NK);
     699             :   }
     700        9058 :   *CHI = checkCHI(NK, *N, joker);
     701        9044 : }
     702             : /* don't support half-integral weight */
     703             : static void
     704         126 : checkNK(GEN NK, long *N, long *k, GEN *CHI, int joker)
     705             : {
     706             :   long d;
     707         126 :   checkNK2(NK, N, k, &d, CHI, joker);
     708         126 :   if (d != 1) pari_err_TYPE("checkNF [k]", NK);
     709         126 : }
     710             : 
     711             : static GEN
     712        4851 : mfchargalois(long N, int odd, GEN flagorder)
     713             : {
     714        4851 :   GEN G = znstar0(utoi(N), 1), L = chargalois(G, flagorder);
     715        4851 :   long l = lg(L), i, j;
     716      112735 :   for (i = j = 1; i < l; i++)
     717             :   {
     718      107884 :     GEN chi = znconreyfromchar(G, gel(L,i));
     719      107884 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
     720             :   }
     721        4851 :   setlg(L, j); return L;
     722             : }
     723             : /* possible characters for non-trivial S_1(N, chi) */
     724             : static GEN
     725        1708 : mfwt1chars(long N, GEN vCHI)
     726             : {
     727        1708 :   if (vCHI) return vCHI; /*do not filter, user knows best*/
     728             :   /* Tate's theorem */
     729        1638 :   return mfchargalois(N, 1, uisprime(N)? mkvecsmall2(2,4): NULL);
     730             : }
     731             : static GEN
     732        3255 : mfchars(long N, long k, long dk, GEN vCHI)
     733        3255 : { return vCHI? vCHI: mfchargalois(N, (dk == 2)? 0: (k & 1), NULL); }
     734             : 
     735             : /* wrappers from mfchar to znchar */
     736             : static long
     737       66073 : mfcharparity(GEN CHI)
     738             : {
     739       66073 :   if (!CHI) return 1;
     740       66073 :   return zncharisodd(gel(CHI,1), gel(CHI,2)) ? -1 : 1;
     741             : }
     742             : /* if CHI is primitive, return CHI itself, not a copy */
     743             : static GEN
     744       68971 : mfchartoprimitive(GEN CHI, long *pF)
     745             : {
     746             :   pari_sp av;
     747             :   GEN chi, F;
     748       68971 :   if (!CHI) { if (pF) *pF = 1; return mfchartrivial(); }
     749       68971 :   av = avma; F = znconreyconductor(gel(CHI,1), gel(CHI,2), &chi);
     750       68971 :   if (typ(F) == t_INT) set_avma(av);
     751             :   else
     752             :   {
     753        7357 :     CHI = leafcopy(CHI);
     754        7357 :     gel(CHI,1) = znstar0(F, 1);
     755        7357 :     gel(CHI,2) = chi;
     756             :   }
     757       68971 :   if (pF) *pF = mfcharmodulus(CHI);
     758       68971 :   return CHI;
     759             : }
     760             : static long
     761      393309 : mfcharconductor(GEN CHI)
     762             : {
     763      393309 :   pari_sp av = avma;
     764      393309 :   GEN res = znconreyconductor(gel(CHI,1), gel(CHI,2), NULL);
     765      393309 :   if (typ(res) == t_VEC) res = gel(res, 1);
     766      393309 :   return gc_long(av, itos(res));
     767             : }
     768             : 
     769             : /*                      Operations on mf closures                    */
     770             : static GEN
     771       50946 : tagparams(long t, GEN NK) { return mkvec2(mkvecsmall(t), NK); }
     772             : static GEN
     773        1078 : lfuntag(long t, GEN x) { return mkvec2(mkvecsmall(t), x); }
     774             : static GEN
     775          56 : tag0(long t, GEN NK) { retmkvec(tagparams(t,NK)); }
     776             : static GEN
     777        9520 : tag(long t, GEN NK, GEN x) { retmkvec2(tagparams(t,NK), x); }
     778             : static GEN
     779       28511 : tag2(long t, GEN NK, GEN x, GEN y) { retmkvec3(tagparams(t,NK), x,y); }
     780             : static GEN
     781       12747 : tag3(long t, GEN NK, GEN x,GEN y,GEN z) { retmkvec4(tagparams(t,NK), x,y,z); }
     782             : static GEN
     783           0 : tag4(long t, GEN NK, GEN x,GEN y,GEN z,GEN a)
     784           0 : { retmkvec5(tagparams(t,NK), x,y,z,a); }
     785             : /* is F a "modular form" ? */
     786             : int
     787       16415 : checkmf_i(GEN F)
     788       16415 : { return typ(F) == t_VEC
     789       15869 :     && lg(F) > 1 && typ(gel(F,1)) == t_VEC
     790       11697 :     && lg(gel(F,1)) == 3
     791       11536 :     && typ(gmael(F,1,1)) == t_VECSMALL
     792       27951 :     && typ(gmael(F,1,2)) == t_VEC; }
     793      171255 : long mf_get_type(GEN F) { return gmael(F,1,1)[1]; }
     794      125307 : GEN mf_get_gN(GEN F) { return gmael3(F,1,2,1); }
     795      103390 : GEN mf_get_gk(GEN F) { return gmael3(F,1,2,2); }
     796             : /* k - 1/2, assume k in 1/2 + Z */
     797         441 : long mf_get_r(GEN F) { return itou(gel(mf_get_gk(F),1)) >> 1; }
     798       90440 : long mf_get_N(GEN F) { return itou(mf_get_gN(F)); }
     799       57435 : long mf_get_k(GEN F)
     800             : {
     801       57435 :   GEN gk = mf_get_gk(F);
     802       57435 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
     803       57435 :   return itou(gk);
     804             : }
     805       38584 : GEN mf_get_CHI(GEN F) { return gmael3(F,1,2,3); }
     806       18284 : GEN mf_get_field(GEN F) { return gmael3(F,1,2,4); }
     807       16492 : GEN mf_get_NK(GEN F) { return gmael(F,1,2); }
     808             : static void
     809         504 : mf_setfield(GEN f, GEN P)
     810             : {
     811         504 :   gel(f,1) = leafcopy(gel(f,1));
     812         504 :   gmael(f,1,2) = leafcopy(gmael(f,1,2));
     813         504 :   gmael3(f,1,2,4) = P;
     814         504 : }
     815             : 
     816             : /* UTILITY FUNCTIONS */
     817             : GEN
     818        4438 : mftocol(GEN F, long lim, long d)
     819        4438 : { GEN c = mfcoefs_i(F, lim, d); settyp(c,t_COL); return c; }
     820             : GEN
     821        1351 : mfvectomat(GEN vF, long lim, long d)
     822             : {
     823        1351 :   long j, l = lg(vF);
     824        1351 :   GEN M = cgetg(l, t_MAT);
     825        1351 :   for (j = 1; j < l; j++) gel(M,j) = mftocol(gel(vF,j), lim, d);
     826        1351 :   return M;
     827             : }
     828             : 
     829             : static GEN
     830        3633 : RgV_to_ser_full(GEN x) { return RgV_to_ser(x, 0, lg(x)+1); }
     831             : /* TODO: delete */
     832             : static GEN
     833         539 : mfcoefsser(GEN F, long n) { return RgV_to_ser_full(mfcoefs_i(F,n,1)); }
     834             : static GEN
     835         777 : sertovecslice(GEN S, long n)
     836             : {
     837         777 :   GEN v = gtovec0(S, -(lg(S) - 2 + valp(S)));
     838         777 :   long l = lg(v), n2 = n + 2;
     839         777 :   if (l < n2) pari_err_BUG("sertovecslice [n too large]");
     840         777 :   return (l == n2)? v: vecslice(v, 1, n2-1);
     841             : }
     842             : 
     843             : /* a, b two RgV of the same length, multiply as truncated power series */
     844             : static GEN
     845        3283 : RgV_mul_RgXn(GEN a, GEN b)
     846             : {
     847        3283 :   long n = lg(a)-1;
     848             :   GEN c;
     849        3283 :   a = RgV_to_RgX(a,0);
     850        3283 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, b, n);
     851        3283 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     852             : }
     853             : /* divide as truncated power series */
     854             : static GEN
     855         357 : RgV_div_RgXn(GEN a, GEN b)
     856             : {
     857         357 :   long n = lg(a)-1;
     858             :   GEN c;
     859         357 :   a = RgV_to_RgX(a,0);
     860         357 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, RgXn_inv(b,n), n);
     861         357 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     862             : }
     863             : /* a^b */
     864             : static GEN
     865          84 : RgV_pows_RgXn(GEN a, long b)
     866             : {
     867          84 :   long n = lg(a)-1;
     868             :   GEN c;
     869          84 :   a = RgV_to_RgX(a,0);
     870          84 :   if (b < 0) { a = RgXn_inv(a, n); b = -b; }
     871          84 :   c = RgXn_powu_i(a,b,n);
     872          84 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     873             : }
     874             : 
     875             : /* assume lg(V) >= n*d + 2 */
     876             : static GEN
     877        6699 : c_deflate(long n, long d, GEN v)
     878             : {
     879        6699 :   long i, id, l = n+2;
     880             :   GEN w;
     881        6699 :   if (d == 1) return lg(v) == l ? v: vecslice(v, 1, l-1);
     882         378 :   w = cgetg(l, typ(v));
     883         378 :   for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
     884         378 :   return w;
     885             : }
     886             : 
     887             : static void
     888          14 : err_cyclo(void)
     889          14 : { pari_err_IMPL("changing cyclotomic fields in mf"); }
     890             : /* Q(zeta_a) = Q(zeta_b) ? */
     891             : static int
     892         567 : same_cyc(long a, long b)
     893         567 : { return (a == b) || (odd(a) && b == (a<<1)) || (odd(b) && a == (b<<1)); }
     894             : /* need to combine elements in Q(CHI1) and Q(CHI2) with result in Q(CHI),
     895             :  * CHI = CHI1 * CHI2 or CHI / CHI2 times some character of order 2 */
     896             : static GEN
     897        2023 : chicompat(GEN CHI, GEN CHI1, GEN CHI2)
     898             : {
     899        2023 :   long o1 = mfcharorder(CHI1);
     900        2023 :   long o2 = mfcharorder(CHI2), O, o;
     901             :   GEN T1, T2, P, Po;
     902        2023 :   if (o1 <= 2 && o2 <= 2) return NULL;
     903         574 :   o = mfcharorder(CHI);
     904         574 :   Po = mfcharpol(CHI);
     905         574 :   P = mfcharpol(CHI1);
     906         574 :   if (o1 == o2)
     907             :   {
     908          21 :     if (o1 == o) return NULL;
     909          14 :     if (!same_cyc(o1,o)) err_cyclo();
     910           0 :     return mkvec4(P, gen_1,gen_1, Qab_trace_init(o1, o, P, Po));
     911             :   }
     912         553 :   O = ulcm(o1, o2);
     913         553 :   if (!same_cyc(O,o)) err_cyclo();
     914         553 :   if (O != o1) P = (O == o2)? mfcharpol(CHI2): polcyclo(O, varn(P));
     915         553 :   T1 = o1 <= 2? gen_1: utoipos(O / o1);
     916         553 :   T2 = o2 <= 2? gen_1: utoipos(O / o2);
     917         553 :   return mkvec4(P, T1, T2, O == o? gen_1: Qab_trace_init(O, o, P, Po));
     918             : }
     919             : /* *F a vector of cyclotomic numbers */
     920             : static void
     921           7 : compatlift(GEN *F, long o, GEN P)
     922             : {
     923             :   long i, l;
     924           7 :   GEN f = *F, g = cgetg_copy(f,&l);
     925          56 :   for (i = 1; i < l; i++)
     926             :   {
     927          49 :     GEN fi = lift_shallow(gel(f,i));
     928          49 :     gel(g,i) = gmodulo(typ(fi)==t_POL? RgX_inflate(fi,o): fi, P);
     929             :   }
     930           7 :   *F = g;
     931           7 : }
     932             : static void
     933         651 : chicompatlift(GEN T, GEN *F, GEN *G)
     934             : {
     935         651 :   long o1 = itou(gel(T,2)), o2 = itou(gel(T,3));
     936         651 :   GEN P = gel(T,1);
     937         651 :   if (o1 != 1) compatlift(F, o1, P);
     938         651 :   if (o2 != 1 && G) compatlift(G, o2, P);
     939         651 : }
     940             : static GEN
     941         651 : chicompatfix(GEN T, GEN F)
     942             : {
     943         651 :   GEN V = gel(T,4);
     944         651 :   if (typ(V) == t_VEC) F = gmodulo(QabV_tracerel(V, 0, F), gel(V,1));
     945         651 :   return F;
     946             : }
     947             : 
     948             : static GEN
     949         623 : c_mul(long n, long d, GEN S)
     950             : {
     951         623 :   pari_sp av = avma;
     952         623 :   long nd = n*d;
     953         623 :   GEN F = gel(S,2), G = gel(S,3);
     954         623 :   F = mfcoefs_i(F, nd, 1);
     955         623 :   G = mfcoefs_i(G, nd, 1);
     956         623 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F,&G);
     957         623 :   F = c_deflate(n, d, RgV_mul_RgXn(F,G));
     958         623 :   if (lg(S) == 5) F = chicompatfix(gel(S,4), F);
     959         623 :   return gerepilecopy(av, F);
     960             : }
     961             : static GEN
     962          84 : c_pow(long n, long d, GEN S)
     963             : {
     964          84 :   pari_sp av = avma;
     965          84 :   long nd = n*d;
     966          84 :   GEN F = gel(S,2), a = gel(S,3), f = mfcoefs_i(F,nd,1);
     967          84 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F, NULL);
     968          84 :   f = RgV_pows_RgXn(f, itos(a));
     969          84 :   f = c_deflate(n, d, f);
     970          84 :   if (lg(S) == 5) f = chicompatfix(gel(S,4), f);
     971          84 :   return gerepilecopy(av, f);
     972             : }
     973             : 
     974             : /* F * Theta */
     975             : static GEN
     976         399 : mfmultheta(GEN F)
     977             : {
     978         399 :   if (typ(mf_get_gk(F)) == t_FRAC && mf_get_type(F) == t_MF_DIV)
     979             :   {
     980         112 :     GEN T = gel(F,3); /* hopefully mfTheta() */
     981         112 :     if (mf_get_type(T) == t_MF_THETA && mf_get_N(T) == 4) return gel(F,2);
     982             :   }
     983         287 :   return mfmul(F, mfTheta(NULL));
     984             : }
     985             : 
     986             : static GEN
     987          21 : c_bracket(long n, long d, GEN S)
     988             : {
     989          21 :   pari_sp av = avma;
     990          21 :   long i, nd = n*d;
     991          21 :   GEN F = gel(S,2), G = gel(S,3);
     992          21 :   GEN VF = mfcoefs_i(F, nd, 1), tF = cgetg(nd+2, t_VEC);
     993          21 :   GEN VG = mfcoefs_i(G, nd, 1), tG = cgetg(nd+2, t_VEC);
     994          21 :   GEN C, mpow, res = NULL, gk = mf_get_gk(F), gl = mf_get_gk(G);
     995          21 :   ulong j, m = itou(gel(S,4));
     996             :   /* pow[i,j+1] = i^j */
     997          21 :   if (lg(S) == 6) chicompatlift(gel(S,5),&VF,&VG);
     998          21 :   mpow = cgetg(m+2, t_MAT);
     999          21 :   gel(mpow,1) = const_col(nd, gen_1);
    1000          49 :   for (j = 1; j <= m; j++)
    1001             :   {
    1002          28 :     GEN c = cgetg(nd+1, t_COL);
    1003          28 :     gel(mpow,j+1) = c;
    1004          28 :     for (i = 1; i <= nd; i++) gel(c,i) = muliu(gcoeff(mpow,i,j), i);
    1005             :   }
    1006          21 :   C = binomial(gaddgs(gk, m-1), m);
    1007          21 :   if (odd(m)) C = gneg(C);
    1008          70 :   for (j = 0; j <= m; j++)
    1009             :   { /* C = (-1)^(m-j) binom(m+l-1, j) binom(m+k-1,m-j) */
    1010             :     GEN c;
    1011          49 :     gel(tF,1) = j == 0? gel(VF,1): gen_0;
    1012          49 :     gel(tG,1) = j == m? gel(VG,1): gen_0;
    1013          49 :     gel(tF,2) = gel(VF,2);
    1014          49 :     gel(tG,2) = gel(VG,2);
    1015         413 :     for (i = 2; i <= nd; i++)
    1016             :     {
    1017         364 :       gel(tF, i+1) = gmul(gcoeff(mpow,i,j+1),   gel(VF, i+1));
    1018         364 :       gel(tG, i+1) = gmul(gcoeff(mpow,i,m-j+1), gel(VG, i+1));
    1019             :     }
    1020          49 :     c = gmul(C, c_deflate(n, d, RgV_mul_RgXn(tF, tG)));
    1021          49 :     res = res? gadd(res, c): c;
    1022          49 :     if (j < m)
    1023          56 :       C = gdiv(gmul(C, gmulsg(m-j, gaddgs(gl,m-j-1))),
    1024          28 :                gmulsg(-(j+1), gaddgs(gk,j)));
    1025             :   }
    1026          21 :   if (lg(S) == 6) res = chicompatfix(gel(S,5), res);
    1027          21 :   return gerepileupto(av, res);
    1028             : }
    1029             : /* linear combination \sum L[j] vecF[j] */
    1030             : static GEN
    1031        2674 : c_linear(long n, long d, GEN F, GEN L, GEN dL)
    1032             : {
    1033        2674 :   pari_sp av = avma;
    1034        2674 :   long j, l = lg(L);
    1035        2674 :   GEN S = NULL;
    1036        8477 :   for (j = 1; j < l; j++)
    1037             :   {
    1038        5803 :     GEN c = gel(L,j);
    1039        5803 :     if (gequal0(c)) continue;
    1040        5166 :     c = gmul(c, mfcoefs_i(gel(F,j), n, d));
    1041        5166 :     S = S? gadd(S,c): c;
    1042             :   }
    1043        2674 :   if (!S) return zerovec(n+1);
    1044        2674 :   if (!is_pm1(dL)) S = gdiv(S, dL);
    1045        2674 :   return gerepileupto(av, S);
    1046             : }
    1047             : 
    1048             : /* B_d(T_j Trace^new) as t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)) or
    1049             :  * t_MF_HECKE(t_MF_NEWTRACE)
    1050             :  * or t_MF_NEWTRACE in level N. Set d and j, return t_MF_NEWTRACE component*/
    1051             : static GEN
    1052       68082 : bhn_parse(GEN f, long *d, long *j)
    1053             : {
    1054       68082 :   long t = mf_get_type(f);
    1055       68082 :   *d = *j = 1;
    1056       68082 :   if (t == t_MF_BD) { *d = itos(gel(f,3)); f = gel(f,2); t = mf_get_type(f); }
    1057       68082 :   if (t == t_MF_HECKE) { *j = gel(f,2)[1]; f = gel(f,3); }
    1058       68082 :   return f;
    1059             : }
    1060             : /* f as above, return the t_MF_NEWTRACE component */
    1061             : static GEN
    1062       21448 : bhn_newtrace(GEN f)
    1063             : {
    1064       21448 :   long t = mf_get_type(f);
    1065       21448 :   if (t == t_MF_BD) { f = gel(f,2); t = mf_get_type(f); }
    1066       21448 :   if (t == t_MF_HECKE) f = gel(f,3);
    1067       21448 :   return f;
    1068             : }
    1069             : static int
    1070        2884 : ok_bhn_linear(GEN vf)
    1071             : {
    1072        2884 :   long i, N0 = 0, l = lg(vf);
    1073             :   GEN CHI, gk;
    1074        2884 :   if (l == 1) return 1;
    1075        2884 :   gk = mf_get_gk(gel(vf,1));
    1076        2884 :   CHI = mf_get_CHI(gel(vf,1));
    1077       16835 :   for (i = 1; i < l; i++)
    1078             :   {
    1079       15484 :     GEN f = bhn_newtrace(gel(vf,i));
    1080       15484 :     long N = mf_get_N(f);
    1081       15484 :     if (mf_get_type(f) != t_MF_NEWTRACE) return 0;
    1082       13951 :     if (N < N0) return 0; /* largest level must come last */
    1083       13951 :     N0 = N;
    1084       13951 :     if (!gequal(gk,mf_get_gk(f))) return 0; /* same k */
    1085       13951 :     if (!gequal(gel(mf_get_CHI(f),2), gel(CHI,2))) return 0; /* same CHI */
    1086             :   }
    1087        1351 :   return 1;
    1088             : }
    1089             : 
    1090             : /* vF not empty, same hypotheses as bhnmat_extend */
    1091             : static GEN
    1092        6048 : bhnmat_extend_nocache(GEN M, long N, long n, long d, GEN vF)
    1093             : {
    1094             :   cachenew_t cache;
    1095        6048 :   long l = lg(vF);
    1096             :   GEN f;
    1097        6048 :   if (l == 1) return M? M: cgetg(1, t_MAT);
    1098        5964 :   f = bhn_newtrace(gel(vF,1)); /* N.B. mf_get_N(f) divides N */
    1099        5964 :   init_cachenew(&cache, n*d, N, f);
    1100        5964 :   M = bhnmat_extend(M, n, d, vF, &cache);
    1101        5964 :   dbg_cachenew(&cache); return M;
    1102             : }
    1103             : /* c_linear of "bhn" mf closures, same hypotheses as bhnmat_extend */
    1104             : static GEN
    1105        1624 : c_linear_bhn(long n, long d, GEN F)
    1106             : {
    1107             :   pari_sp av;
    1108        1624 :   GEN M, v, vF = gel(F,2), L = gel(F,3), dL = gel(F,4);
    1109        1624 :   if (lg(L) == 1) return zerovec(n+1);
    1110        1624 :   av = avma;
    1111        1624 :   M = bhnmat_extend_nocache(NULL, mf_get_N(F), n, d, vF);
    1112        1624 :   v = RgM_RgC_mul(M,L); settyp(v, t_VEC);
    1113        1624 :   if (!is_pm1(dL)) v = gdiv(v, dL);
    1114        1624 :   return gerepileupto(av, v);
    1115             : }
    1116             : 
    1117             : /* c in K, K := Q[X]/(T) vz = vector of consecutive powers of root z of T
    1118             :  * attached to an embedding s: K -> C. Return s(c) in C */
    1119             : static GEN
    1120       75565 : Rg_embed1(GEN c, GEN vz)
    1121             : {
    1122       75565 :   long t = typ(c);
    1123       75565 :   if (t == t_POLMOD) { c = gel(c,2); t = typ(c); }
    1124       75565 :   if (t == t_POL) c = RgX_RgV_eval(c, vz);
    1125       75565 :   return c;
    1126             : }
    1127             : /* return s(P) in C[X] */
    1128             : static GEN
    1129         882 : RgX_embed1(GEN P, GEN vz)
    1130             : {
    1131             :   long i, l;
    1132         882 :   GEN Q = cgetg_copy(P, &l);
    1133         882 :   Q[1] = P[1];
    1134         882 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1135         882 :   return normalizepol_lg(Q,l); /* normally a no-op */
    1136             : }
    1137             : /* return s(P) in C^n */
    1138             : static GEN
    1139         728 : vecembed1(GEN P, GEN vz)
    1140             : {
    1141             :   long i, l;
    1142         728 :   GEN Q = cgetg_copy(P, &l);
    1143         728 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1144         728 :   return Q;
    1145             : }
    1146             : /* P in L = K[X]/(U), K = Q[t]/T; s an embedding of K -> C attached
    1147             :  * to a root of T, extended to an embedding of L -> C attached to a root
    1148             :  * of s(U); vT powers of the root of T, vU powers of the root of s(U).
    1149             :  * Return s(P) in C^n */
    1150             : static GEN
    1151       13314 : Rg_embed2(GEN P, long vt, GEN vT, GEN vU)
    1152             : {
    1153             :   long i, l;
    1154             :   GEN Q;
    1155       13314 :   P = liftpol_shallow(P);
    1156       13314 :   if (typ(P) != t_POL) return P;
    1157       13300 :   if (varn(P) == vt) return Rg_embed1(P, vT);
    1158             :   /* varn(P) == vx */
    1159       13293 :   Q = cgetg_copy(P, &l); Q[1] = P[1];
    1160       13293 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vT);
    1161       13293 :   return Rg_embed1(Q, vU);
    1162             : }
    1163             : static GEN
    1164          42 : vecembed2(GEN P, long vt, GEN vT, GEN vU)
    1165             : {
    1166             :   long i, l;
    1167          42 :   GEN Q = cgetg_copy(P, &l);
    1168          42 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1169          42 :   return Q;
    1170             : }
    1171             : static GEN
    1172         532 : RgX_embed2(GEN P, long vt, GEN vT, GEN vU)
    1173             : {
    1174             :   long i, l;
    1175         532 :   GEN Q = cgetg_copy(P, &l);
    1176         532 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1177         532 :   Q[1] = P[1]; return normalizepol_lg(Q,l);
    1178             : }
    1179             : /* embed polynomial f in variable vx [ may be a scalar ], E from getembed */
    1180             : static GEN
    1181        1596 : RgX_embed(GEN f, long vx, GEN E)
    1182             : {
    1183             :   GEN vT;
    1184        1596 :   if (typ(f) != t_POL || varn(f) != vx) return mfembed(E, f);
    1185        1575 :   if (lg(E) == 1) return f;
    1186        1379 :   vT = gel(E,2);
    1187        1379 :   if (lg(E) == 3)
    1188         847 :     f = RgX_embed1(f, vT);
    1189             :   else
    1190         532 :     f = RgX_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1191        1379 :   return f;
    1192             : }
    1193             : /* embed vector, E from getembed */
    1194             : GEN
    1195        1680 : mfvecembed(GEN E, GEN v)
    1196             : {
    1197             :   GEN vT;
    1198        1680 :   if (lg(E) == 1) return v;
    1199         770 :   vT = gel(E,2);
    1200         770 :   if (lg(E) == 3)
    1201         728 :     v = vecembed1(v, vT);
    1202             :   else
    1203          42 :     v = vecembed2(v, varn(gel(E,1)), vT, gel(E,3));
    1204         770 :   return v;
    1205             : }
    1206             : GEN
    1207          63 : mfmatembed(GEN E, GEN f)
    1208             : {
    1209             :   long i, l;
    1210             :   GEN g;
    1211          63 :   if (lg(E) == 1) return f;
    1212          42 :   g = cgetg_copy(f, &l);
    1213          42 :   for (i = 1; i < l; i++) gel(g,i) = mfvecembed(E, gel(f,i));
    1214          42 :   return g;
    1215             : }
    1216             : /* embed vector of polynomials in var vx */
    1217             : static GEN
    1218          98 : RgXV_embed(GEN f, long vx, GEN E)
    1219             : {
    1220             :   long i, l;
    1221             :   GEN v;
    1222          98 :   if (lg(E) == 1) return f;
    1223          70 :   v = cgetg_copy(f, &l);
    1224          70 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(gel(f,i), vx, E);
    1225          70 :   return v;
    1226             : }
    1227             : 
    1228             : /* embed scalar */
    1229             : GEN
    1230       96580 : mfembed(GEN E, GEN f)
    1231             : {
    1232             :   GEN vT;
    1233       96580 :   if (lg(E) == 1) return f;
    1234       13538 :   vT = gel(E,2);
    1235       13538 :   if (lg(E) == 3)
    1236        4424 :     f = Rg_embed1(f, vT);
    1237             :   else
    1238        9114 :     f = Rg_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1239       13538 :   return f;
    1240             : }
    1241             : /* vector of the sigma(f), sigma in vE */
    1242             : static GEN
    1243         294 : RgX_embedall(GEN f, long vx, GEN vE)
    1244             : {
    1245         294 :   long i, l = lg(vE);
    1246         294 :   GEN v = cgetg(l, t_VEC);
    1247         294 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(f, vx, gel(vE,i));
    1248         294 :   return l == 2? gel(v,1): v;
    1249             : }
    1250             : /* matrix whose colums are the sigma(v), sigma in vE */
    1251             : static GEN
    1252         329 : RgC_embedall(GEN v, GEN vE)
    1253             : {
    1254         329 :   long j, l = lg(vE);
    1255         329 :   GEN M = cgetg(l, t_MAT);
    1256         329 :   for (j = 1; j < l; j++) gel(M,j) = mfvecembed(gel(vE,j), v);
    1257         329 :   return M;
    1258             : }
    1259             : /* vector of the sigma(v), sigma in vE */
    1260             : static GEN
    1261        4907 : Rg_embedall_i(GEN v, GEN vE)
    1262             : {
    1263        4907 :   long j, l = lg(vE);
    1264        4907 :   GEN M = cgetg(l, t_VEC);
    1265        4907 :   for (j = 1; j < l; j++) gel(M,j) = mfembed(gel(vE,j), v);
    1266        4907 :   return M;
    1267             : }
    1268             : /* vector of the sigma(v), sigma in vE; if #vE == 1, return v */
    1269             : static GEN
    1270       90980 : Rg_embedall(GEN v, GEN vE)
    1271       90980 : { return (lg(vE) == 2)? mfembed(gel(vE,1), v): Rg_embedall_i(v, vE); }
    1272             : 
    1273             : static GEN
    1274         777 : c_div_i(long n, GEN S)
    1275             : {
    1276         777 :   GEN F = gel(S,2), G = gel(S,3);
    1277             :   GEN a0, a0i, H;
    1278         777 :   F = mfcoefs_i(F, n, 1);
    1279         777 :   G = mfcoefs_i(G, n, 1);
    1280         777 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F,&G);
    1281         777 :   F = RgV_to_ser_full(F);
    1282         777 :   G = RgV_to_ser_full(G);
    1283         777 :   a0 = polcoef_i(G, 0, -1); /* != 0 */
    1284         777 :   if (gequal1(a0)) a0 = a0i = NULL;
    1285             :   else
    1286             :   {
    1287         602 :     a0i = ginv(a0);
    1288         602 :     G = gmul(ser_unscale(G,a0), a0i);
    1289         602 :     F = gmul(ser_unscale(F,a0), a0i);
    1290             :   }
    1291         777 :   H = gdiv(F, G);
    1292         777 :   if (a0) H = ser_unscale(H,a0i);
    1293         777 :   H = sertovecslice(H, n);
    1294         777 :   if (lg(S) == 5) H = chicompatfix(gel(S,4), H);
    1295         777 :   return H;
    1296             : }
    1297             : static GEN
    1298         777 : c_div(long n, long d, GEN S)
    1299             : {
    1300         777 :   pari_sp av = avma;
    1301         777 :   GEN D = (d==1)? c_div_i(n, S): c_deflate(n, d, c_div_i(n*d, S));
    1302         777 :   return gerepilecopy(av, D);
    1303             : }
    1304             : 
    1305             : static GEN
    1306          35 : c_shift(long n, long d, GEN F, GEN gsh)
    1307             : {
    1308          35 :   pari_sp av = avma;
    1309             :   GEN vF;
    1310          35 :   long sh = itos(gsh), n1 = n*d + sh;
    1311          35 :   if (n1 < 0) return zerovec(n+1);
    1312          35 :   vF = mfcoefs_i(F, n1, 1);
    1313          35 :   if (sh < 0) vF = shallowconcat(zerovec(-sh), vF);
    1314          35 :   else vF = vecslice(vF, sh+1, n1+1);
    1315          35 :   return gerepilecopy(av, c_deflate(n, d, vF));
    1316             : }
    1317             : 
    1318             : static GEN
    1319         147 : c_deriv(long n, long d, GEN F, GEN gm)
    1320             : {
    1321         147 :   pari_sp av = avma;
    1322         147 :   GEN V = mfcoefs_i(F, n, d), res;
    1323         147 :   long i, m = itos(gm);
    1324         147 :   if (!m) return V;
    1325         147 :   res = cgetg(n+2, t_VEC); gel(res,1) = gen_0;
    1326         147 :   if (m < 0)
    1327           7 :   { for (i=1; i <= n; i++) gel(res, i+1) = gdiv(gel(V, i+1), powuu(i,-m)); }
    1328             :   else
    1329         140 :   { for (i=1; i <= n; i++) gel(res, i+1) = gmul(gel(V,i+1), powuu(i,m)); }
    1330         147 :   return gerepileupto(av, res);
    1331             : }
    1332             : 
    1333             : static GEN
    1334          14 : c_derivE2(long n, long d, GEN F, GEN gm)
    1335             : {
    1336          14 :   pari_sp av = avma;
    1337             :   GEN VF, VE, res, tmp, gk;
    1338          14 :   long i, m = itos(gm), nd;
    1339          14 :   if (m == 0) return mfcoefs_i(F, n, d);
    1340          14 :   nd = n*d;
    1341          14 :   VF = mfcoefs_i(F, nd, 1); VE = mfcoefs_i(mfEk(2), nd, 1);
    1342          14 :   gk = mf_get_gk(F);
    1343          14 :   if (m == 1)
    1344             :   {
    1345           7 :     res = cgetg(n+2, t_VEC);
    1346           7 :     for (i = 0; i <= n; i++) gel(res, i+1) = gmulsg(i, gel(VF, i*d+1));
    1347           7 :     tmp = c_deflate(n, d, RgV_mul_RgXn(VF, VE));
    1348           7 :     return gerepileupto(av, gsub(res, gmul(gdivgs(gk, 12), tmp)));
    1349             :   }
    1350             :   else
    1351             :   {
    1352             :     long j;
    1353          35 :     for (j = 1; j <= m; j++)
    1354             :     {
    1355          28 :       tmp = RgV_mul_RgXn(VF, VE);
    1356          28 :       for (i = 0; i <= nd; i++) gel(VF, i+1) = gmulsg(i, gel(VF, i+1));
    1357          28 :       VF = gsub(VF, gmul(gdivgs(gaddgs(gk, 2*(j-1)), 12), tmp));
    1358             :     }
    1359           7 :     return gerepilecopy(av, c_deflate(n, d, VF));
    1360             :   }
    1361             : }
    1362             : 
    1363             : /* Twist by the character (D/.) */
    1364             : static GEN
    1365           7 : c_twist(long n, long d, GEN F, GEN D)
    1366             : {
    1367           7 :   pari_sp av = avma;
    1368           7 :   GEN V = mfcoefs_i(F, n, d), res = cgetg(n+2, t_VEC);
    1369             :   long i;
    1370         119 :   for (i = 0; i <= n; i++)
    1371         112 :     gel(res, i + 1) = gmulsg(krois(D, i), gel(V, i+1));
    1372           7 :   return gerepileupto(av, res);
    1373             : }
    1374             : 
    1375             : /* form F given by closure, compute T(n)(F) as closure */
    1376             : static GEN
    1377         434 : c_hecke(long m, long l, GEN DATA, GEN F)
    1378             : {
    1379         434 :   pari_sp av = avma;
    1380         434 :   return gerepilecopy(av, hecke_i(m, l, NULL, F, DATA));
    1381             : }
    1382             : static GEN
    1383         140 : c_const(long n, long d, GEN C)
    1384             : {
    1385         140 :   GEN V = zerovec(n+1);
    1386         140 :   long i, j, l = lg(C);
    1387         140 :   if (l > d*n+2) l = d*n+2;
    1388         140 :   for (i = j = 1; i < l; i+=d, j++) gel(V, j) = gcopy(gel(C,i));
    1389         140 :   return V;
    1390             : }
    1391             : 
    1392             : /* m > 0 */
    1393             : static GEN
    1394         462 : eta3_ZXn(long m)
    1395             : {
    1396         462 :   long l = m+2, n, k;
    1397         462 :   GEN P = cgetg(l,t_POL);
    1398         462 :   P[1] = evalsigne(1)|evalvarn(0);
    1399         462 :   for (n = 2; n < l; n++) gel(P,n) = gen_0;
    1400        2534 :   for (n = k = 0;; n++)
    1401             :   {
    1402        4606 :     if (k + n >= m) { setlg(P, k+3); return P; }
    1403        2072 :     k += n;
    1404             :     /* now k = n(n+1) / 2 */
    1405        2072 :     gel(P, k+2) = odd(n)? utoineg(2*n+1): utoipos(2*n+1);
    1406             :   }
    1407             : }
    1408             : 
    1409             : static GEN
    1410         469 : c_delta(long n, long d)
    1411             : {
    1412         469 :   pari_sp ltop = avma;
    1413         469 :   long N = n*d;
    1414             :   GEN e;
    1415         469 :   if (!N) return mkvec(gen_0);
    1416         462 :   e = eta3_ZXn(N);
    1417         462 :   e = ZXn_sqr(e,N);
    1418         462 :   e = ZXn_sqr(e,N);
    1419         462 :   e = ZXn_sqr(e,N); /* eta(x)^24 */
    1420         462 :   settyp(e, t_VEC);
    1421         462 :   gel(e,1) = gen_0; /* Delta(x) = x*eta(x)^24 as a t_VEC */
    1422         462 :   return gerepilecopy(ltop, c_deflate(n, d, e));
    1423             : }
    1424             : 
    1425             : /* return s(d) such that s|f <=> d | f^2 */
    1426             : static long
    1427          49 : mysqrtu(ulong d)
    1428             : {
    1429          49 :   GEN fa = myfactoru(d), P = gel(fa,1), E = gel(fa,2);
    1430          49 :   long l = lg(P), i, s = 1;
    1431          49 :   for (i = 1; i < l; i++) s *= upowuu(P[i], (E[i]+1)>>1);
    1432          49 :   return s;
    1433             : }
    1434             : static GEN
    1435        1652 : c_theta(long n, long d, GEN psi)
    1436             : {
    1437        1652 :   long lim = usqrt(n*d), F = mfcharmodulus(psi), par = mfcharparity(psi);
    1438        1652 :   long f, d2 = d == 1? 1: mysqrtu(d);
    1439        1652 :   GEN V = zerovec(n + 1);
    1440        7014 :   for (f = d2; f <= lim; f += d2)
    1441        5362 :     if (ugcd(F, f) == 1)
    1442             :     {
    1443        5355 :       pari_sp av = avma;
    1444        5355 :       GEN c = mfchareval(psi, f);
    1445        5355 :       gel(V, f*f/d + 1) = gerepileupto(av, par < 0 ? gmulgs(c,2*f) : gmul2n(c,1));
    1446             :     }
    1447        1652 :   if (F == 1) gel(V, 1) = gen_1;
    1448        1652 :   return V; /* no gerepile needed */
    1449             : }
    1450             : 
    1451             : static GEN
    1452         182 : c_etaquo(long n, long d, GEN eta, GEN gs)
    1453             : {
    1454         182 :   pari_sp av = avma;
    1455         182 :   long s = itos(gs), nd = n*d, nds = nd - s + 1;
    1456             :   GEN c;
    1457         182 :   if (nds <= 0) return zerovec(n+1);
    1458         161 :   c = RgX_to_RgC(eta_product_ZXn(eta, nds), nds); settyp(c, t_VEC);
    1459         161 :   if (s > 0) c = shallowconcat(zerovec(s), c);
    1460         161 :   return gerepilecopy(av, c_deflate(n, d, c));
    1461             : }
    1462             : 
    1463             : static GEN
    1464          77 : c_ell(long n, long d, GEN E)
    1465             : {
    1466          77 :   pari_sp av = avma;
    1467             :   GEN v;
    1468          77 :   if (d == 1) return concat(gen_0, anell(E, n));
    1469           7 :   v = shallowconcat(gen_0, anell(E, n*d));
    1470           7 :   return gerepilecopy(av, c_deflate(n, d, v));
    1471             : }
    1472             : 
    1473             : static GEN
    1474          21 : c_cusptrace(long n, long d, GEN F)
    1475             : {
    1476          21 :   pari_sp av = avma;
    1477          21 :   GEN D = gel(F,2), res = cgetg(n+2, t_VEC);
    1478          21 :   long i, N = mf_get_N(F), k = mf_get_k(F);
    1479          21 :   gel(res, 1) = gen_0;
    1480         140 :   for (i = 1; i <= n; i++)
    1481         119 :     gel(res, i+1) = mfcusptrace_i(N, k, i*d, mydivisorsu(i*d), D);
    1482          21 :   return gerepilecopy(av, res);
    1483             : }
    1484             : 
    1485             : static GEN
    1486         749 : c_newtrace(long n, long d, GEN F)
    1487             : {
    1488         749 :   pari_sp av = avma;
    1489             :   cachenew_t cache;
    1490         749 :   long N = mf_get_N(F);
    1491             :   GEN v;
    1492         749 :   init_cachenew(&cache, n*d, N, F);
    1493         749 :   v = colnewtrace(0, n, d, N, mf_get_k(F), &cache);
    1494         749 :   settyp(v, t_VEC); return gerepilecopy(av, v);
    1495             : }
    1496             : 
    1497             : static GEN
    1498        3668 : c_Bd(long n, long d, GEN F, GEN A)
    1499             : {
    1500        3668 :   pari_sp av = avma;
    1501        3668 :   long a = itou(A), ad = ugcd(a,d), aad = a/ad, i, j;
    1502        3668 :   GEN w, v = mfcoefs_i(F, n/aad, d/ad);
    1503        3668 :   if (a == 1) return v;
    1504        3668 :   n++; w = zerovec(n);
    1505        3668 :   for (i = j = 1; j <= n; i++, j += aad) gel(w,j) = gcopy(gel(v,i));
    1506        3668 :   return gerepileupto(av, w);
    1507             : }
    1508             : 
    1509             : static GEN
    1510        3997 : c_dihedral(long n, long d, GEN bnr, GEN w, GEN k0j)
    1511             : {
    1512        3997 :   pari_sp av = avma;
    1513        3997 :   GEN V = dihan(bnr, w, k0j, n*d);
    1514        3997 :   GEN Tinit = gel(w,3), Pm = gel(Tinit,1);
    1515        3997 :   GEN A = c_deflate(n, d, V);
    1516        3997 :   if (degpol(Pm) == 1 || RgV_is_ZV(A)) return gerepilecopy(av, A);
    1517         749 :   return gerepileupto(av, gmodulo(A, Pm));
    1518             : }
    1519             : 
    1520             : static GEN
    1521         315 : c_mfEH(long n, long d, GEN F)
    1522             : {
    1523         315 :   pari_sp av = avma;
    1524             :   GEN v, M, A;
    1525         315 :   long i, r = mf_get_r(F);
    1526         315 :   if (n == 1)
    1527          14 :     return gerepilecopy(av, mkvec2(mfEHcoef(r,0),mfEHcoef(r,d)));
    1528             :   /* speedup mfcoef */
    1529         301 :   if (r == 1)
    1530             :   {
    1531          70 :     v = cgetg(n+2, t_VEC);
    1532          70 :     gel(v,1) = sstoQ(-1,12);
    1533       83258 :     for (i = 1; i <= n; i++)
    1534             :     {
    1535       83188 :       long id = i*d, a = id & 3;
    1536       83188 :       gel(v,i+1) = (a==1 || a==2)? gen_0: sstoQ(hclassno6u(id), 6);
    1537             :     }
    1538          70 :     return v; /* no gerepile needed */
    1539             :   }
    1540         231 :   M = mfEHmat(n*d+1,r);
    1541         231 :   if (d > 1)
    1542             :   {
    1543          35 :     long l = lg(M);
    1544          35 :     for (i = 1; i < l; i++) gel(M,i) = c_deflate(n, d, gel(M,i));
    1545             :   }
    1546         231 :   A = gel(F,2); /* [num(B), den(B)] */
    1547         231 :   v = RgC_Rg_div(RgM_RgC_mul(M, gel(A,1)), gel(A,2));
    1548         231 :   settyp(v,t_VEC); return gerepileupto(av, v);
    1549             : }
    1550             : 
    1551             : static GEN
    1552        7147 : c_mfeisen(long n, long d, GEN F)
    1553             : {
    1554        7147 :   pari_sp av = avma;
    1555        7147 :   GEN v, vchi, E0, P, T, CHI, gk = mf_get_gk(F);
    1556             :   long i, k;
    1557        7147 :   if (typ(gk) != t_INT) return c_mfEH(n, d, F);
    1558        6832 :   k = itou(gk);
    1559        6832 :   vchi = gel(F,2);
    1560        6832 :   E0 = gel(vchi,1);
    1561        6832 :   T = gel(vchi,2);
    1562        6832 :   P = gel(T,1);
    1563        6832 :   CHI = gel(vchi,3);
    1564        6832 :   v = cgetg(n+2, t_VEC);
    1565        6832 :   gel(v, 1) = gcopy(E0); /* E(0) */
    1566        6832 :   if (lg(vchi) == 5)
    1567             :   { /* E_k(chi1,chi2) */
    1568        5026 :     GEN CHI2 = gel(vchi,4), F3 = gel(F,3);
    1569        5026 :     long ord = F3[1], j = F3[2];
    1570        5026 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi2(k, CHI, CHI2, i*d, ord);
    1571        5026 :     v = QabV_tracerel(T, j, v);
    1572             :   }
    1573             :   else
    1574             :   { /* E_k(chi) */
    1575        1806 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi(k, CHI, i*d);
    1576             :   }
    1577        6832 :   if (degpol(P) != 1 && !RgV_is_QV(v)) return gerepileupto(av, gmodulo(v, P));
    1578        5145 :   return gerepilecopy(av, v);
    1579             : }
    1580             : 
    1581             : /* L(chi_D, 1-k) */
    1582             : static GEN
    1583          28 : lfunquadneg_naive(long D, long k)
    1584             : {
    1585          28 :   GEN B, dS, S = gen_0;
    1586          28 :   long r, N = labs(D);
    1587             :   pari_sp av;
    1588          28 :   if (k == 1 && N == 1) return gneg(ghalf);
    1589             :   /* B = N^k * denom(B) * B(x/N) */
    1590          28 :   B = ZX_rescale(Q_remove_denom(bernpol(k, 0), &dS), utoi(N));
    1591          28 :   dS = mul_denom(dS, stoi(-N*k));
    1592          28 :   av = avma;
    1593        7175 :   for (r = 0; r < N; r++)
    1594             :   {
    1595        7147 :     long c = kross(D, r);
    1596        7147 :     if (c)
    1597             :     {
    1598        5152 :       GEN tmp = poleval(B, utoi(r));
    1599        5152 :       S = c > 0 ? addii(S, tmp) : subii(S, tmp);
    1600        5152 :       S = gerepileuptoint(av, S);
    1601             :     }
    1602             :   }
    1603          28 :   return gdiv(S, dS);
    1604             : }
    1605             : 
    1606             : /* Returns vector of coeffs from F[0], F[d], ..., F[d*n] */
    1607             : static GEN
    1608       25137 : mfcoefs_i(GEN F, long n, long d)
    1609             : {
    1610       25137 :   if (n < 0) return gen_0;
    1611       25137 :   switch(mf_get_type(F))
    1612             :   {
    1613         140 :     case t_MF_CONST: return c_const(n, d, gel(F,2));
    1614        7147 :     case t_MF_EISEN: return c_mfeisen(n, d, F);
    1615         833 :     case t_MF_Ek: return c_Ek(n, d, F);
    1616         469 :     case t_MF_DELTA: return c_delta(n, d);
    1617        1414 :     case t_MF_THETA: return c_theta(n, d, gel(F,2));
    1618         182 :     case t_MF_ETAQUO: return c_etaquo(n, d, gel(F,2), gel(F,3));
    1619          77 :     case t_MF_ELL: return c_ell(n, d, gel(F,2));
    1620         623 :     case t_MF_MUL: return c_mul(n, d, F);
    1621          84 :     case t_MF_POW: return c_pow(n, d, F);
    1622          21 :     case t_MF_BRACKET: return c_bracket(n, d, F);
    1623        2674 :     case t_MF_LINEAR: return c_linear(n, d, gel(F,2), gel(F,3), gel(F,4));
    1624        1624 :     case t_MF_LINEAR_BHN: return c_linear_bhn(n, d, F);
    1625         777 :     case t_MF_DIV: return c_div(n, d, F);
    1626          35 :     case t_MF_SHIFT: return c_shift(n, d, gel(F,2), gel(F,3));
    1627         147 :     case t_MF_DERIV: return c_deriv(n, d, gel(F,2), gel(F,3));
    1628          14 :     case t_MF_DERIVE2: return c_derivE2(n, d, gel(F,2), gel(F,3));
    1629           7 :     case t_MF_TWIST: return c_twist(n, d, gel(F,2), gel(F,3));
    1630         434 :     case t_MF_HECKE: return c_hecke(n, d, gel(F,2), gel(F,3));
    1631        3668 :     case t_MF_BD: return c_Bd(n, d, gel(F,2), gel(F,3));
    1632          21 :     case t_MF_TRACE: return c_cusptrace(n, d, F);
    1633         749 :     case t_MF_NEWTRACE: return c_newtrace(n, d, F);
    1634        3997 :     case t_MF_DIHEDRAL: return c_dihedral(n, d, gel(F,2), gel(F,3), gel(F,4));
    1635             :     default: pari_err_TYPE("mfcoefs",F); return NULL;/*LCOV_EXCL_LINE*/
    1636             :   }
    1637             : }
    1638             : 
    1639             : static GEN
    1640         308 : matdeflate(long n, long d, GEN M)
    1641             : {
    1642             :   long i, l;
    1643             :   GEN A;
    1644             :   /*  if (d == 1) return M; */
    1645         308 :   A = cgetg_copy(M,&l);
    1646         308 :   for (i = 1; i < l; i++) gel(A,i) = c_deflate(n,d,gel(M,i));
    1647         308 :   return A;
    1648             : }
    1649             : static int
    1650        5460 : space_is_cusp(long space) { return space != mf_FULL && space != mf_EISEN; }
    1651             : /* safe with flraw mf */
    1652             : static GEN
    1653        2212 : mfcoefs_mf(GEN mf, long n, long d)
    1654             : {
    1655        2212 :   GEN MS, ME, E = MF_get_E(mf), S = MF_get_S(mf), M = MF_get_M(mf);
    1656        2212 :   long lE = lg(E), lS = lg(S), l = lE+lS-1;
    1657             : 
    1658        2212 :   if (l == 1) return cgetg(1, t_MAT);
    1659        2100 :   if (typ(M) == t_MAT && lg(M) != 1 && (n+1)*d < nbrows(M))
    1660          21 :     return matdeflate(n, d, M); /*cached; lg = 1 is possible from mfinit */
    1661        2079 :   ME = (lE == 1)? cgetg(1, t_MAT): mfvectomat(E, n, d);
    1662        2079 :   if (lS == 1)
    1663         399 :     MS = cgetg(1, t_MAT);
    1664        1680 :   else if (mf_get_type(gel(S,1)) == t_MF_DIV) /*k 1/2-integer or k=1 (exotic)*/
    1665         287 :     MS = matdeflate(n,d, mflineardivtomat(MF_get_N(mf), S, n*d));
    1666        1393 :   else if (MF_get_k(mf) == 1) /* k = 1 (dihedral) */
    1667             :   {
    1668         140 :     GEN M = mfvectomat(gmael(S,1,2), n, d);
    1669             :     long i;
    1670         140 :     MS = cgetg(lS, t_MAT);
    1671         448 :     for (i = 1; i < lS; i++)
    1672             :     {
    1673         308 :       GEN f = gel(S,i), dc = gel(f,4), c = RgM_RgC_mul(M, gel(f,3));
    1674         308 :       if (!equali1(dc)) c = RgC_Rg_div(c,dc);
    1675         308 :       gel(MS,i) = c;
    1676             :     }
    1677             :   }
    1678             :   else /* k >= 2 integer */
    1679        1253 :     MS = bhnmat_extend_nocache(NULL, MF_get_N(mf), n, d, S);
    1680        2079 :   return shallowconcat(ME,MS);
    1681             : }
    1682             : GEN
    1683        3759 : mfcoefs(GEN F, long n, long d)
    1684             : {
    1685        3759 :   if (!checkmf_i(F))
    1686             :   {
    1687          42 :     pari_sp av = avma;
    1688          42 :     GEN mf = checkMF_i(F); if (!mf) pari_err_TYPE("mfcoefs", F);
    1689          42 :     return gerepilecopy(av, mfcoefs_mf(mf,n,d));
    1690             :   }
    1691        3717 :   if (d <= 0) pari_err_DOMAIN("mfcoefs", "d", "<=", gen_0, stoi(d));
    1692        3717 :   if (n < 0) return cgetg(1, t_VEC);
    1693        3717 :   return mfcoefs_i(F, n, d);
    1694             : }
    1695             : 
    1696             : /* assume k >= 0 */
    1697             : static GEN
    1698         280 : mfak_i(GEN F, long k)
    1699             : {
    1700         280 :   if (!k) return gel(mfcoefs_i(F,0,1), 1);
    1701         154 :   return gel(mfcoefs_i(F,1,k), 2);
    1702             : }
    1703             : GEN
    1704         140 : mfcoef(GEN F, long n)
    1705             : {
    1706         140 :   pari_sp av = avma;
    1707         140 :   if (!checkmf_i(F)) pari_err_TYPE("mfcoef",F);
    1708         140 :   return n < 0? gen_0: gerepilecopy(av, mfak_i(F, n));
    1709             : }
    1710             : 
    1711             : static GEN
    1712         112 : paramconst() { return tagparams(t_MF_CONST, mkNK(1,0,mfchartrivial())); }
    1713             : static GEN
    1714          70 : mftrivial(void) { retmkvec2(paramconst(), cgetg(1,t_VEC)); }
    1715             : static GEN
    1716          42 : mf1(void) { retmkvec2(paramconst(), mkvec(gen_1)); }
    1717             : 
    1718             : /* induce mfchar CHI to G */
    1719             : static GEN
    1720      306691 : induce(GEN G, GEN CHI)
    1721             : {
    1722             :   GEN o, chi;
    1723      306691 :   if (typ(CHI) == t_INT) /* Kronecker */
    1724             :   {
    1725      300671 :     chi = znchar_quad(G, CHI);
    1726      300671 :     o = ZV_equal0(chi)? gen_1: gen_2;
    1727      300671 :     CHI = mkvec4(G,chi,o,cgetg(1,t_VEC));
    1728             :   }
    1729             :   else
    1730             :   {
    1731        6020 :     if (mfcharmodulus(CHI) == itos(znstar_get_N(G))) return CHI;
    1732        5467 :     CHI = leafcopy(CHI);
    1733        5467 :     chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    1734        5467 :     gel(CHI,1) = G;
    1735        5467 :     gel(CHI,2) = chi;
    1736             :   }
    1737      306138 :   return CHI;
    1738             : }
    1739             : /* induce mfchar CHI to znstar(G) */
    1740             : static GEN
    1741       42252 : induceN(long N, GEN CHI)
    1742             : {
    1743       42252 :   if (mfcharmodulus(CHI) != N) CHI = induce(znstar0(utoipos(N),1), CHI);
    1744       42252 :   return CHI;
    1745             : }
    1746             : /* *pCHI1 and *pCHI2 are mfchar, induce to common modulus */
    1747             : static void
    1748        5530 : char2(GEN *pCHI1, GEN *pCHI2)
    1749             : {
    1750        5530 :   GEN CHI1 = *pCHI1, G1 = gel(CHI1,1), N1 = znstar_get_N(G1);
    1751        5530 :   GEN CHI2 = *pCHI2, G2 = gel(CHI2,1), N2 = znstar_get_N(G2);
    1752        5530 :   if (!equalii(N1,N2))
    1753             :   {
    1754        4032 :     GEN G, d = gcdii(N1,N2);
    1755        4032 :     if      (equalii(N2,d)) *pCHI2 = induce(G1, CHI2);
    1756        1386 :     else if (equalii(N1,d)) *pCHI1 = induce(G2, CHI1);
    1757             :     else
    1758             :     {
    1759         154 :       if (!equali1(d)) N2 = diviiexact(N2,d);
    1760         154 :       G = znstar0(mulii(N1,N2), 1);
    1761         154 :       *pCHI1 = induce(G, CHI1);
    1762         154 :       *pCHI2 = induce(G, CHI2);
    1763             :     }
    1764             :   }
    1765        5530 : }
    1766             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1767             : static GEN
    1768      301665 : mfcharmul_i(GEN CHI1, GEN CHI2)
    1769             : {
    1770      301665 :   GEN G = gel(CHI1,1), chi3 = zncharmul(G, gel(CHI1,2), gel(CHI2,2));
    1771      301665 :   return mfcharGL(G, chi3);
    1772             : }
    1773             : /* mfchar or charinit; outputs a mfchar */
    1774             : static GEN
    1775        1001 : mfcharmul(GEN CHI1, GEN CHI2)
    1776             : {
    1777        1001 :   char2(&CHI1, &CHI2); return mfcharmul_i(CHI1,CHI2);
    1778             : }
    1779             : /* mfchar or charinit; outputs a mfchar */
    1780             : static GEN
    1781         119 : mfcharpow(GEN CHI, GEN n)
    1782             : {
    1783             :   GEN G, chi;
    1784         119 :   G = gel(CHI,1); chi = zncharpow(G, gel(CHI,2), n);
    1785         119 :   return mfcharGL(G, chi);
    1786             : }
    1787             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1788             : static GEN
    1789        4529 : mfchardiv_i(GEN CHI1, GEN CHI2)
    1790             : {
    1791        4529 :   GEN G = gel(CHI1,1), chi3 = znchardiv(G, gel(CHI1,2), gel(CHI2,2));
    1792        4529 :   return mfcharGL(G, chi3);
    1793             : }
    1794             : /* mfchar or charinit; outputs a mfchar */
    1795             : static GEN
    1796        4529 : mfchardiv(GEN CHI1, GEN CHI2)
    1797             : {
    1798        4529 :   char2(&CHI1, &CHI2); return mfchardiv_i(CHI1,CHI2);
    1799             : }
    1800             : static GEN
    1801          49 : mfcharconj(GEN CHI)
    1802             : {
    1803          49 :   CHI = leafcopy(CHI);
    1804          49 :   gel(CHI,2) = zncharconj(gel(CHI,1), gel(CHI,2));
    1805          49 :   return CHI;
    1806             : }
    1807             : 
    1808             : /* CHI mfchar, assume 4 | N. Multiply CHI by \chi_{-4} */
    1809             : static GEN
    1810         882 : mfchilift(GEN CHI, long N)
    1811             : {
    1812         882 :   CHI = induceN(N, CHI);
    1813         882 :   return mfcharmul_i(CHI, induce(gel(CHI,1), stoi(-4)));
    1814             : }
    1815             : /* CHI defined mod N, N4 = N/4;
    1816             :  * if CHI is defined mod N4 return CHI;
    1817             :  * else if CHI' = CHI*(-4,.) is defined mod N4, return CHI' (primitive)
    1818             :  * else return NULL */
    1819             : static GEN
    1820          70 : mfcharchiliftprim(GEN CHI, long N4)
    1821             : {
    1822          70 :   long FC = mfcharconductor(CHI);
    1823          70 :   if (N4 % FC == 0) return CHI;
    1824          14 :   CHI = mfchilift(CHI, N4 << 2);
    1825          14 :   CHI = mfchartoprimitive(CHI, &FC);
    1826          14 :   return (N4 % FC == 0)? CHI: NULL;
    1827             : }
    1828             : static GEN
    1829        2100 : mfchiadjust(GEN CHI, GEN gk, long N)
    1830             : {
    1831        2100 :   long par = mfcharparity(CHI);
    1832        2100 :   if (typ(gk) == t_INT &&  mpodd(gk)) par = -par;
    1833        2100 :   return par == 1 ? CHI : mfchilift(CHI, N);
    1834             : }
    1835             : 
    1836             : static GEN
    1837        3143 : mfsamefield(GEN T, GEN P, GEN Q)
    1838             : {
    1839        3143 :   if (degpol(P) == 1) return Q;
    1840         602 :   if (degpol(Q) == 1) return P;
    1841         511 :   if (!gequal(P,Q)) pari_err_TYPE("mfsamefield [different fields]",mkvec2(P,Q));
    1842         504 :   if (T) err_cyclo();
    1843         504 :   return P;
    1844             : }
    1845             : 
    1846             : GEN
    1847         448 : mfmul(GEN f, GEN g)
    1848             : {
    1849         448 :   pari_sp av = avma;
    1850             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    1851         448 :   if (!checkmf_i(f)) pari_err_TYPE("mfmul",f);
    1852         448 :   if (!checkmf_i(g)) pari_err_TYPE("mfmul",g);
    1853         448 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1854         448 :   K = gadd(mf_get_gk(f), mf_get_gk(g));
    1855         448 :   CHIf = mf_get_CHI(f);
    1856         448 :   CHIg = mf_get_CHI(g);
    1857         448 :   CHI = mfchiadjust(mfcharmul(CHIf,CHIg), K, itos(N));
    1858         448 :   T = chicompat(CHI, CHIf, CHIg);
    1859         448 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    1860         441 :   return gerepilecopy(av, T? tag3(t_MF_MUL,NK,f,g,T): tag2(t_MF_MUL,NK,f,g));
    1861             : }
    1862             : GEN
    1863          63 : mfpow(GEN f, long n)
    1864             : {
    1865          63 :   pari_sp av = avma;
    1866             :   GEN T, KK, NK, gn, CHI, CHIf;
    1867          63 :   if (!checkmf_i(f)) pari_err_TYPE("mfpow",f);
    1868          63 :   if (!n) return mf1();
    1869          63 :   if (n == 1) return gcopy(f);
    1870          63 :   KK = gmulsg(n,mf_get_gk(f));
    1871          63 :   gn = stoi(n);
    1872          63 :   CHIf = mf_get_CHI(f);
    1873          63 :   CHI = mfchiadjust(mfcharpow(CHIf,gn), KK, mf_get_N(f));
    1874          63 :   T = chicompat(CHI, CHIf, CHIf);
    1875          56 :   NK = mkgNK(mf_get_gN(f), KK, CHI, mf_get_field(f));
    1876          56 :   return gerepilecopy(av, T? tag3(t_MF_POW,NK,f,gn,T): tag2(t_MF_POW,NK,f,gn));
    1877             : }
    1878             : GEN
    1879          21 : mfbracket(GEN f, GEN g, long m)
    1880             : {
    1881          21 :   pari_sp av = avma;
    1882             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    1883          21 :   if (!checkmf_i(f)) pari_err_TYPE("mfbracket",f);
    1884          21 :   if (!checkmf_i(g)) pari_err_TYPE("mfbracket",g);
    1885          21 :   if (m < 0) pari_err_TYPE("mfbracket [m<0]",stoi(m));
    1886          21 :   K = gaddgs(gadd(mf_get_gk(f), mf_get_gk(g)), 2*m);
    1887          21 :   if (gsigne(K) < 0) pari_err_IMPL("mfbracket for this form");
    1888          21 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1889          21 :   CHIf = mf_get_CHI(f);
    1890          21 :   CHIg = mf_get_CHI(g);
    1891          21 :   CHI = mfcharmul(CHIf, CHIg);
    1892          21 :   CHI = mfchiadjust(CHI, K, itou(N));
    1893          21 :   T = chicompat(CHI, CHIf, CHIg);
    1894          21 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    1895          42 :   return gerepilecopy(av, T? tag4(t_MF_BRACKET, NK, f, g, utoi(m), T)
    1896          21 :                            : tag3(t_MF_BRACKET, NK, f, g, utoi(m)));
    1897             : }
    1898             : 
    1899             : /* remove 0 entries in L */
    1900             : static int
    1901        1155 : mflinear_strip(GEN *pF, GEN *pL)
    1902             : {
    1903        1155 :   pari_sp av = avma;
    1904        1155 :   GEN F = *pF, L = *pL;
    1905        1155 :   long i, j, l = lg(L);
    1906        1155 :   GEN F2 = cgetg(l, t_VEC), L2 = cgetg(l, t_VEC);
    1907        6860 :   for (i = j = 1; i < l; i++)
    1908             :   {
    1909        5705 :     if (gequal0(gel(L,i))) continue;
    1910        3227 :     gel(F2,j) = gel(F,i);
    1911        3227 :     gel(L2,j) = gel(L,i); j++;
    1912             :   }
    1913        1155 :   if (j == l) set_avma(av);
    1914             :   else
    1915             :   {
    1916         322 :     setlg(F2,j); *pF = F2;
    1917         322 :     setlg(L2,j); *pL = L2;
    1918             :   }
    1919        1155 :   return (j > 1);
    1920             : }
    1921             : static GEN
    1922        4634 : taglinear_i(long t, GEN NK, GEN F, GEN L)
    1923             : {
    1924             :   GEN dL;
    1925        4634 :   L = Q_remove_denom(L, &dL); if (!dL) dL = gen_1;
    1926        4634 :   return tag3(t, NK, F, L, dL);
    1927             : }
    1928             : static GEN
    1929        1806 : taglinear(GEN NK, GEN F, GEN L)
    1930             : {
    1931        1806 :   long t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1932        1806 :    return taglinear_i(t, NK, F, L);
    1933             : }
    1934             : /* assume F has parameters NK = [N,K,CHI] */
    1935             : static GEN
    1936         301 : mflinear_i(GEN NK, GEN F, GEN L)
    1937             : {
    1938         301 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1939         301 :   return taglinear(NK, F,L);
    1940             : }
    1941             : static GEN
    1942         483 : mflinear_bhn(GEN mf, GEN L)
    1943             : {
    1944             :   long i, l;
    1945         483 :   GEN P, NK, F = MF_get_S(mf);
    1946         483 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1947         476 :   l = lg(L); P = pol_x(1);
    1948        2597 :   for (i = 1; i < l; i++)
    1949             :   {
    1950        2121 :     GEN c = gel(L,i);
    1951        2121 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1)
    1952         518 :       P = mfsamefield(NULL, P, gel(c,1));
    1953             :   }
    1954         476 :   NK = mkgNK(MF_get_gN(mf), MF_get_gk(mf), MF_get_CHI(mf), P);
    1955         476 :   return taglinear_i(t_MF_LINEAR_BHN,  NK, F,L);
    1956             : }
    1957             : 
    1958             : /* F vector of forms with same weight and character but varying level, return
    1959             :  * global [N,k,chi,P] */
    1960             : static GEN
    1961        2366 : vecmfNK(GEN F)
    1962             : {
    1963        2366 :   long i, l = lg(F);
    1964             :   GEN N, f;
    1965        2366 :   if (l == 1) return mkNK(1, 0, mfchartrivial());
    1966        2366 :   f = gel(F,1); N = mf_get_gN(f);
    1967        2366 :   for (i = 2; i < l; i++) N = lcmii(N, mf_get_gN(gel(F,i)));
    1968        2366 :   return mkgNK(N, mf_get_gk(f), mf_get_CHI(f), mf_get_field(f));
    1969             : }
    1970             : /* do not use mflinear: mflineardivtomat rely on F being constant across the
    1971             :  * basis where mflinear strips the ones matched by 0 coeffs. Assume k and CHI
    1972             :  * constant, N is allowed to vary. */
    1973             : static GEN
    1974        1078 : vecmflinear(GEN F, GEN C)
    1975             : {
    1976        1078 :   long i, t, l = lg(C);
    1977        1078 :   GEN NK, v = cgetg(l, t_VEC);
    1978        1078 :   if (l == 1) return v;
    1979        1078 :   t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1980        1078 :   NK = vecmfNK(F);
    1981        1078 :   for (i = 1; i < l; i++) gel(v,i) = taglinear_i(t, NK, F, gel(C,i));
    1982        1078 :   return v;
    1983             : }
    1984             : /* vecmflinear(F,C), then divide everything by E, which has valuation 0 */
    1985             : static GEN
    1986         350 : vecmflineardiv0(GEN F, GEN C, GEN E)
    1987             : {
    1988         350 :   GEN v = vecmflinear(F, C);
    1989         350 :   long i, l = lg(v);
    1990         350 :   if (l == 1) return v;
    1991         350 :   gel(v,1) = mfdiv_val(gel(v,1), E, 0);
    1992        1036 :   for (i = 2; i < l; i++)
    1993             :   { /* v[i] /= E */
    1994         686 :     GEN f = shallowcopy(gel(v,1));
    1995         686 :     gel(f,2) = gel(v,i);
    1996         686 :     gel(v,i) = f;
    1997             :   }
    1998         350 :   return v;
    1999             : }
    2000             : 
    2001             : /* Non empty linear combination of linear combinations of same
    2002             :  * F_j=\sum_i \mu_{i,j}G_i so R = \sum_i (\sum_j(\la_j\mu_{i,j})) G_i */
    2003             : static GEN
    2004        1288 : mflinear_linear(GEN F, GEN L, int strip)
    2005             : {
    2006        1288 :   long l = lg(F), j;
    2007        1288 :   GEN vF, M = cgetg(l, t_MAT);
    2008        1288 :   L = shallowcopy(L);
    2009        7728 :   for (j = 1; j < l; j++)
    2010             :   {
    2011        6440 :     GEN f = gel(F,j), c = gel(f,3), d = gel(f,4);
    2012        6440 :     if (typ(c) == t_VEC) c = shallowtrans(c);
    2013        6440 :     if (!isint1(d)) gel(L,j) = gdiv(gel(L,j),d);
    2014        6440 :     gel(M,j) = c;
    2015             :   }
    2016        1288 :   vF = gmael(F,1,2); L = RgM_RgC_mul(M,L);
    2017        1288 :   if (strip && !mflinear_strip(&vF,&L)) return mftrivial();
    2018        1288 :   return taglinear(vecmfNK(vF), vF, L);
    2019             : }
    2020             : /* F non-empty vector of forms of the form mfdiv(mflinear(B,v), E) where E
    2021             :  * does not vanish at oo, or mflinear(B,v). Apply mflinear(F, L) */
    2022             : static GEN
    2023        1288 : mflineardiv_linear(GEN F, GEN L, int strip)
    2024             : {
    2025        1288 :   long l = lg(F), j;
    2026             :   GEN v, E, f;
    2027        1288 :   if (lg(L) != l) pari_err_DIM("mflineardiv_linear");
    2028        1288 :   f = gel(F,1); /* l > 1 */
    2029        1288 :   if (mf_get_type(f) != t_MF_DIV) return mflinear_linear(F,L,strip);
    2030        1113 :   E = gel(f,3);
    2031        1113 :   v = cgetg(l, t_VEC);
    2032        1113 :   for (j = 1; j < l; j++) { GEN f = gel(F,j); gel(v,j) = gel(f,2); }
    2033        1113 :   return mfdiv_val(mflinear_linear(v,L,strip), E, 0);
    2034             : }
    2035             : static GEN
    2036         413 : vecmflineardiv_linear(GEN F, GEN M)
    2037             : {
    2038         413 :   long i, l = lg(M);
    2039         413 :   GEN v = cgetg(l, t_VEC);
    2040         413 :   for (i = 1; i < l; i++) gel(v,i) = mflineardiv_linear(F, gel(M,i), 0);
    2041         413 :   return v;
    2042             : }
    2043             : 
    2044             : static GEN
    2045         532 : tobasis(GEN mf, GEN F, GEN L)
    2046             : {
    2047         532 :   if (checkmf_i(L) && mf) return mftobasis(mf, L, 0);
    2048         525 :   if (typ(F) != t_VEC) pari_err_TYPE("mflinear",F);
    2049         525 :   if (!is_vec_t(typ(L))) pari_err_TYPE("mflinear",L);
    2050         525 :   if (lg(L) != lg(F)) pari_err_DIM("mflinear");
    2051         525 :   return L;
    2052             : }
    2053             : GEN
    2054         560 : mflinear(GEN F, GEN L)
    2055             : {
    2056         560 :   pari_sp av = avma;
    2057         560 :   GEN G, NK, P, mf = checkMF_i(F), N = NULL, K = NULL, CHI = NULL;
    2058             :   long i, l;
    2059         560 :   if (mf)
    2060             :   {
    2061         434 :     GEN gk = MF_get_gk(mf);
    2062         434 :     F = MF_get_basis(F);
    2063         434 :     if (typ(gk) != t_INT)
    2064          28 :       return gerepilecopy(av, mflineardiv_linear(F, L, 1));
    2065         406 :     if (itou(gk) > 1 && space_is_cusp(MF_get_space(mf)))
    2066             :     {
    2067         238 :       L = tobasis(mf, F, L);
    2068         238 :       return gerepilecopy(av, mflinear_bhn(mf, L));
    2069             :     }
    2070             :   }
    2071         294 :   L = tobasis(mf, F, L);
    2072         294 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    2073             : 
    2074         287 :   l = lg(F);
    2075         287 :   if (l == 2 && gequal1(gel(L,1))) return gerepilecopy(av, gel(F,1));
    2076         231 :   P = pol_x(1);
    2077         777 :   for (i = 1; i < l; i++)
    2078             :   {
    2079         553 :     GEN f = gel(F,i), c = gel(L,i), Ni, Ki;
    2080         553 :     if (!checkmf_i(f)) pari_err_TYPE("mflinear", f);
    2081         553 :     Ni = mf_get_gN(f); N = N? lcmii(N, Ni): Ni;
    2082         553 :     Ki = mf_get_gk(f);
    2083         553 :     if (!K) K = Ki;
    2084         322 :     else if (!gequal(K, Ki))
    2085           7 :       pari_err_TYPE("mflinear [different weights]", mkvec2(K,Ki));
    2086         546 :     P = mfsamefield(NULL, P, mf_get_field(f));
    2087         546 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1)
    2088         126 :       P = mfsamefield(NULL, P, gel(c,1));
    2089             :   }
    2090         224 :   G = znstar0(N,1);
    2091         756 :   for (i = 1; i < l; i++)
    2092             :   {
    2093         539 :     GEN CHI2 = mf_get_CHI(gel(F,i));
    2094         539 :     CHI2 = induce(G, CHI2);
    2095         539 :     if (!CHI) CHI = CHI2;
    2096         315 :     else if (!gequal(CHI, CHI2))
    2097           7 :       pari_err_TYPE("mflinear [different characters]", mkvec2(CHI,CHI2));
    2098             :   }
    2099         217 :   NK = mkgNK(N, K, CHI, P);
    2100         217 :   return gerepilecopy(av, taglinear(NK,F,L));
    2101             : }
    2102             : 
    2103             : GEN
    2104          42 : mfshift(GEN F, long sh)
    2105             : {
    2106          42 :   pari_sp av = avma;
    2107          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfshift",F);
    2108          42 :   return gerepilecopy(av, tag2(t_MF_SHIFT, mf_get_NK(F), F, stoi(sh)));
    2109             : }
    2110             : static long
    2111          49 : mfval(GEN F)
    2112             : {
    2113          49 :   pari_sp av = avma;
    2114          49 :   long i = 0, n, sb;
    2115             :   GEN gk, gN;
    2116          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfval", F);
    2117          49 :   gN = mf_get_gN(F);
    2118          49 :   gk = mf_get_gk(F);
    2119          49 :   sb = mfsturmNgk(itou(gN), gk);
    2120         119 :   for (n = 1; n <= sb;)
    2121             :   {
    2122             :     GEN v;
    2123          63 :     if (n > 0.5*sb) n = sb+1;
    2124          63 :     v = mfcoefs_i(F, n, 1);
    2125         119 :     for (; i <= n; i++)
    2126          98 :       if (!gequal0(gel(v, i+1))) return gc_long(av,i);
    2127          21 :     n <<= 1;
    2128             :   }
    2129           7 :   return gc_long(av,-1);
    2130             : }
    2131             : 
    2132             : GEN
    2133        1491 : mfdiv_val(GEN f, GEN g, long vg)
    2134             : {
    2135             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    2136        1491 :   if (vg) { f = mfshift(f,vg); g = mfshift(g,vg); }
    2137        1491 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    2138        1491 :   K = gsub(mf_get_gk(f), mf_get_gk(g));
    2139        1491 :   CHIf = mf_get_CHI(f);
    2140        1491 :   CHIg = mf_get_CHI(g);
    2141        1491 :   CHI = mfchiadjust(mfchardiv(CHIf, CHIg), K, itos(N));
    2142        1491 :   T = chicompat(CHI, CHIf, CHIg);
    2143        1484 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    2144        1484 :   return T? tag3(t_MF_DIV, NK, f, g, T): tag2(t_MF_DIV, NK, f, g);
    2145             : }
    2146             : GEN
    2147          49 : mfdiv(GEN F, GEN G)
    2148             : {
    2149          49 :   pari_sp av = avma;
    2150          49 :   long v = mfval(G);
    2151          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfdiv", F);
    2152          42 :   if (v < 0 || (v && !gequal0(mfcoefs(F, v-1, 1))))
    2153          14 :     pari_err_DOMAIN("mfdiv", "ord(G)", ">", strtoGENstr("ord(F)"),
    2154             :                     mkvec2(F, G));
    2155          28 :   return gerepilecopy(av, mfdiv_val(F, G, v));
    2156             : }
    2157             : GEN
    2158         154 : mfderiv(GEN F, long m)
    2159             : {
    2160         154 :   pari_sp av = avma;
    2161             :   GEN NK, gk;
    2162         154 :   if (!checkmf_i(F)) pari_err_TYPE("mfderiv",F);
    2163         154 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2164         154 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2165         154 :   return gerepilecopy(av, tag2(t_MF_DERIV, NK, F, stoi(m)));
    2166             : }
    2167             : GEN
    2168          21 : mfderivE2(GEN F, long m)
    2169             : {
    2170          21 :   pari_sp av = avma;
    2171             :   GEN NK, gk;
    2172          21 :   if (!checkmf_i(F)) pari_err_TYPE("mfderivE2",F);
    2173          21 :   if (m < 0) pari_err_DOMAIN("mfderivE2","m","<",gen_0,stoi(m));
    2174          21 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2175          21 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2176          21 :   return gerepilecopy(av, tag2(t_MF_DERIVE2, NK, F, stoi(m)));
    2177             : }
    2178             : 
    2179             : GEN
    2180          14 : mftwist(GEN F, GEN D)
    2181             : {
    2182          14 :   pari_sp av = avma;
    2183             :   GEN NK, CHI, NT, Da;
    2184             :   long q;
    2185          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftwist", F);
    2186          14 :   if (typ(D) != t_INT) pari_err_TYPE("mftwist", D);
    2187          14 :   Da = mpabs_shallow(D);
    2188          14 :   CHI = mf_get_CHI(F); q = mfcharconductor(CHI);
    2189          14 :   NT = glcm(glcm(mf_get_gN(F), mulsi(q, Da)), sqri(Da));
    2190          14 :   NK = mkgNK(NT, mf_get_gk(F), CHI, mf_get_field(F));
    2191          14 :   return gerepilecopy(av, tag2(t_MF_TWIST, NK, F, D));
    2192             : }
    2193             : 
    2194             : /***************************************************************/
    2195             : /*                 Generic cache handling                      */
    2196             : /***************************************************************/
    2197             : enum { cache_FACT, cache_DIV, cache_H, cache_D, cache_DIH };
    2198             : typedef struct {
    2199             :   const char *name;
    2200             :   GEN cache;
    2201             :   ulong minself, maxself;
    2202             :   void (*init)(long);
    2203             :   ulong miss, maxmiss;
    2204             :   long compressed;
    2205             : } cache;
    2206             : 
    2207             : static void constfact(long lim);
    2208             : static void constdiv(long lim);
    2209             : static void consttabh(long lim);
    2210             : static void consttabdihedral(long lim);
    2211             : static void constcoredisc(long lim);
    2212             : static THREAD cache caches[] = {
    2213             : { "Factors",  NULL,  50000,    50000, &constfact, 0, 0, 0 },
    2214             : { "Divisors", NULL,  50000,    50000, &constdiv, 0, 0, 0 },
    2215             : { "H",        NULL, 100000, 10000000, &consttabh, 0, 0, 1 },
    2216             : { "CorediscF",NULL, 100000, 10000000, &constcoredisc, 0, 0, 0 },
    2217             : { "Dihedral", NULL,   1000,     3000, &consttabdihedral, 0, 0, 0 },
    2218             : };
    2219             : 
    2220             : static void
    2221         399 : cache_reset(long id) { caches[id].miss = caches[id].maxmiss = 0; }
    2222             : static void
    2223        8095 : cache_delete(long id) { guncloneNULL(caches[id].cache); }
    2224             : static void
    2225         413 : cache_set(long id, GEN S)
    2226             : {
    2227         413 :   GEN old = caches[id].cache;
    2228         413 :   caches[id].cache = gclone(S);
    2229         413 :   guncloneNULL(old);
    2230         413 : }
    2231             : 
    2232             : /* handle a cache miss: store stats, possibly reset table; return value
    2233             :  * if (now) cached; return NULL on failure. HACK: some caches contain an
    2234             :  * ulong where the 0 value is impossible, and return it (typecast to GEN) */
    2235             : static GEN
    2236   234402044 : cache_get(long id, ulong D)
    2237             : {
    2238   234402044 :   cache *S = &caches[id];
    2239   234402044 :   const ulong d = S->compressed? D>>1: D;
    2240             :   ulong max, l;
    2241             : 
    2242   234402044 :   if (!S->cache)
    2243             :   {
    2244         266 :     max = maxuu(minuu(D, S->maxself), S->minself);
    2245         266 :     S->init(max);
    2246         266 :     l = lg(S->cache);
    2247             :   }
    2248             :   else
    2249             :   {
    2250   234401778 :     l = lg(S->cache);
    2251   234401778 :     if (l <= d)
    2252             :     {
    2253         994 :       if (D > S->maxmiss) S->maxmiss = D;
    2254         994 :       if (DEBUGLEVEL >= 3)
    2255           0 :         err_printf("miss in cache %s: %lu, max = %lu\n",
    2256             :                    S->name, D, S->maxmiss);
    2257         994 :       if (S->miss++ >= 5 && D < S->maxself)
    2258             :       {
    2259          84 :         max = minuu(S->maxself, (long)(S->maxmiss * 1.2));
    2260          84 :         if (max <= S->maxself)
    2261             :         {
    2262          84 :           if (DEBUGLEVEL >= 3)
    2263           0 :             err_printf("resetting cache %s to %lu\n", S->name, max);
    2264          84 :           S->init(max); l = lg(S->cache);
    2265             :         }
    2266             :       }
    2267             :     }
    2268             :   }
    2269   234402044 :   return (l <= d)? NULL: gel(S->cache, d);
    2270             : }
    2271             : static GEN
    2272          70 : cache_report(long id)
    2273             : {
    2274          70 :   cache *S = &caches[id];
    2275          70 :   GEN v = zerocol(5);
    2276          70 :   gel(v,1) = strtoGENstr(S->name);
    2277          70 :   if (S->cache)
    2278             :   {
    2279          35 :     gel(v,2) = utoi(lg(S->cache)-1);
    2280          35 :     gel(v,3) = utoi(S->miss);
    2281          35 :     gel(v,4) = utoi(S->maxmiss);
    2282          35 :     gel(v,5) = utoi(gsizebyte(S->cache));
    2283             :   }
    2284          70 :   return v;
    2285             : }
    2286             : GEN
    2287          14 : getcache(void)
    2288             : {
    2289          14 :   pari_sp av = avma;
    2290          14 :   GEN M = cgetg(6, t_MAT);
    2291          14 :   gel(M,1) = cache_report(cache_FACT);
    2292          14 :   gel(M,2) = cache_report(cache_DIV);
    2293          14 :   gel(M,3) = cache_report(cache_H);
    2294          14 :   gel(M,4) = cache_report(cache_D);
    2295          14 :   gel(M,5) = cache_report(cache_DIH);
    2296          14 :   return gerepilecopy(av, shallowtrans(M));
    2297             : }
    2298             : 
    2299             : void
    2300        1619 : pari_close_mf(void)
    2301             : {
    2302        1619 :   cache_delete(cache_FACT);
    2303        1619 :   cache_delete(cache_DIV);
    2304        1619 :   cache_delete(cache_H);
    2305        1619 :   cache_delete(cache_D);
    2306        1619 :   cache_delete(cache_DIH);
    2307        1619 : }
    2308             : 
    2309             : /*************************************************************************/
    2310             : /* a odd, update local cache (recycle memory) */
    2311             : static GEN
    2312        2032 : update_factor_cache(long a, long lim, long *pb)
    2313             : {
    2314        2032 :   const long step = 16000; /* even; don't increase this: RAM cache thrashing */
    2315        2032 :   if (a + 2*step > lim)
    2316         224 :     *pb = lim; /* fuse last 2 chunks */
    2317             :   else
    2318        1808 :     *pb = a + step;
    2319        2032 :   return vecfactoroddu_i(a, *pb);
    2320             : }
    2321             : /* assume lim < MAX_LONG/8 */
    2322             : static void
    2323          77 : constcoredisc(long lim)
    2324             : {
    2325          77 :   pari_sp av2, av = avma;
    2326          77 :   GEN D = caches[cache_D].cache, CACHE = NULL;
    2327          77 :   long cachea, cacheb, N, LIM = !D ? 4 : lg(D)-1;
    2328          77 :   if (lim <= 0) lim = 5;
    2329          77 :   if (lim <= LIM) return;
    2330          77 :   cache_reset(cache_D);
    2331          77 :   D = zero_zv(lim);
    2332          77 :   av2 = avma;
    2333          77 :   cachea = cacheb = 0;
    2334     8253336 :   for (N = 1; N <= lim; N+=2)
    2335             :   { /* N odd */
    2336             :     long i, d, d2;
    2337             :     GEN F;
    2338     8253259 :     if (N > cacheb)
    2339             :     {
    2340        1008 :       set_avma(av2); cachea = N;
    2341        1008 :       CACHE = update_factor_cache(N, lim, &cacheb);
    2342             :     }
    2343     8253259 :     F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2344     8253259 :     D[N] = d = corediscs_fact(F); /* = 3 mod 4 or 4 mod 16 */
    2345     8253259 :     d2 = odd(d)? d<<3: d<<1;
    2346     8253259 :     for (i = 1;;)
    2347             :     {
    2348    13755399 :       if ((N << i) > lim) break;
    2349     5502160 :       D[N<<i] = d2; i++;
    2350     5502160 :       if ((N << i) > lim) break;
    2351     2751070 :       D[N<<i] = d; i++;
    2352             :     }
    2353             :   }
    2354          77 :   cache_set(cache_D, D);
    2355          77 :   set_avma(av);
    2356             : }
    2357             : 
    2358             : static void
    2359         112 : constfact(long lim)
    2360             : {
    2361             :   pari_sp av;
    2362         112 :   GEN VFACT = caches[cache_FACT].cache;
    2363         112 :   long LIM = VFACT? lg(VFACT)-1: 4;
    2364         112 :   if (lim <= 0) lim = 5;
    2365         112 :   if (lim <= LIM) return;
    2366          91 :   cache_reset(cache_FACT); av = avma;
    2367          91 :   cache_set(cache_FACT, vecfactoru_i(1,lim)); set_avma(av);
    2368             : }
    2369             : static void
    2370          84 : constdiv(long lim)
    2371             : {
    2372             :   pari_sp av;
    2373          84 :   GEN VFACT, VDIV = caches[cache_DIV].cache;
    2374          84 :   long N, LIM = VDIV? lg(VDIV)-1: 4;
    2375          84 :   if (lim <= 0) lim = 5;
    2376          84 :   if (lim <= LIM) return;
    2377          84 :   constfact(lim);
    2378          84 :   VFACT = caches[cache_FACT].cache;
    2379          84 :   cache_reset(cache_DIV); av = avma;
    2380          84 :   VDIV  = cgetg(lim+1, t_VEC);
    2381          84 :   for (N = 1; N <= lim; N++) gel(VDIV,N) = divisorsu_fact(gel(VFACT,N));
    2382          84 :   cache_set(cache_DIV, VDIV); set_avma(av);
    2383             : }
    2384             : 
    2385             : /* n > 1, D = divisors(n); sets L = 2*lambda(n), S = sigma(n) */
    2386             : static void
    2387    10567586 : lamsig(GEN D, long *pL, long *pS)
    2388             : {
    2389    10567586 :   pari_sp av = avma;
    2390    10567586 :   long i, l = lg(D), L = 1, S = D[l-1]+1;
    2391    37807024 :   for (i = 2; i < l; i++) /* skip d = 1 */
    2392             :   {
    2393    37807024 :     long d = D[i], nd = D[l-i]; /* nd = n/d */
    2394    37807024 :     if (d < nd) { L += d; S += d + nd; }
    2395             :     else
    2396             :     {
    2397    10567586 :       L <<= 1; if (d == nd) { L += d; S += d; }
    2398    10567586 :       break;
    2399             :     }
    2400             :   }
    2401    10567586 :   set_avma(av); *pL = L; *pS = S;
    2402    10567586 : }
    2403             : /* table of 6 * Hurwitz class numbers D <= lim */
    2404             : static void
    2405         147 : consttabh(long lim)
    2406             : {
    2407         147 :   pari_sp av = avma, av2;
    2408         147 :   GEN VHDH0, VDIV, CACHE = NULL;
    2409         147 :   GEN VHDH = caches[cache_H].cache;
    2410         147 :   long r, N, cachea, cacheb, lim0 = VHDH? lg(VHDH)-1: 2, LIM = lim0 << 1;
    2411             : 
    2412         147 :   if (lim <= 0) lim = 5;
    2413         147 :   if (lim <= LIM) return;
    2414         147 :   cache_reset(cache_H);
    2415         147 :   r = lim&3L; if (r) lim += 4-r;
    2416         147 :   cache_get(cache_DIV, lim);
    2417         147 :   VDIV = caches[cache_DIV].cache;
    2418         147 :   VHDH0 = cgetg(lim/2 + 1, t_VECSMALL);
    2419         147 :   VHDH0[1] = 2;
    2420         147 :   VHDH0[2] = 3;
    2421         147 :   for (N = 3; N <= lim0; N++) VHDH0[N] = VHDH[N];
    2422         147 :   av2 = avma;
    2423         147 :   cachea = cacheb = 0;
    2424     5283940 :   for (N = LIM + 3; N <= lim; N += 4)
    2425             :   {
    2426     5283793 :     long s = 0, limt = usqrt(N>>2), flsq = 0, ind, t, L, S;
    2427             :     GEN DN, DN2;
    2428     5283793 :     if (N + 2 >= lg(VDIV))
    2429             :     { /* use local cache */
    2430             :       GEN F;
    2431     4233961 :       if (N + 2 > cacheb)
    2432             :       {
    2433        1024 :         set_avma(av2); cachea = N;
    2434        1024 :         CACHE = update_factor_cache(N, lim+2, &cacheb);
    2435             :       }
    2436     4233961 :       F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2437     4233961 :       DN = divisorsu_fact(F);
    2438     4233961 :       F = gel(CACHE, ((N-cachea)>>1)+2); /* factoru(N+2) */
    2439     4233961 :       DN2 = divisorsu_fact(F);
    2440             :     }
    2441             :     else
    2442             :     { /* use global cache */
    2443     1049832 :       DN = gel(VDIV,N);
    2444     1049832 :       DN2 = gel(VDIV,N+2);
    2445             :     }
    2446     5283793 :     ind = N >> 1;
    2447  1115411521 :     for (t = 1; t <= limt; t++)
    2448             :     {
    2449  1110127728 :       ind -= (t<<2)-2; /* N/2 - 2t^2 */
    2450  1110127728 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2451             :     }
    2452     5283793 :     lamsig(DN, &L,&S);
    2453     5283793 :     VHDH0[N >> 1] = 2*S - 3*L - 2*s + flsq;
    2454     5283793 :     s = 0; flsq = 0; limt = (usqrt(N+2) - 1) >> 1;
    2455     5283793 :     ind = (N+1) >> 1;
    2456  1112795533 :     for (t = 1; t <= limt; t++)
    2457             :     {
    2458  1107511740 :       ind -= t<<2; /* (N+1)/2 - 2t(t+1) */
    2459  1107511740 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2460             :     }
    2461     5283793 :     lamsig(DN2, &L,&S);
    2462     5283793 :     VHDH0[(N+1) >> 1] = S - 3*(L >> 1) - s - flsq;
    2463             :   }
    2464         147 :   cache_set(cache_H, VHDH0); set_avma(av);
    2465             : }
    2466             : 
    2467             : /*************************************************************************/
    2468             : /* Core functions using factorizations, divisors of class numbers caches */
    2469             : /* TODO: myfactoru and factorization cache should be exported */
    2470             : static GEN
    2471    21662669 : myfactoru(long N)
    2472             : {
    2473    21662669 :   GEN z = cache_get(cache_FACT, N);
    2474    21662669 :   return z? gcopy(z): factoru(N);
    2475             : }
    2476             : static GEN
    2477    50605191 : mydivisorsu(long N)
    2478             : {
    2479    50605191 :   GEN z = cache_get(cache_DIV, N);
    2480    50605191 :   return z? leafcopy(z): divisorsu(N);
    2481             : }
    2482             : /* write -n = Df^2, D < 0 fundamental discriminant. Return D, set f. */
    2483             : static long
    2484    85547378 : mycoredisc2neg(ulong n, long *pf)
    2485             : {
    2486    85547378 :   ulong m, D = (ulong)cache_get(cache_D, n);
    2487    85547378 :   if (D) { *pf = usqrt(n/D); return -(long)D; }
    2488         196 :   m = mycore(n, pf);
    2489         196 :   if ((m&3) != 3) { m <<= 2; *pf >>= 1; }
    2490         196 :   return (long)-m;
    2491             : }
    2492             : /* write n = Df^2, D > 0 fundamental discriminant. Return D, set f. */
    2493             : static long
    2494          14 : mycoredisc2pos(ulong n, long *pf)
    2495             : {
    2496          14 :   ulong m = mycore(n, pf);
    2497          14 :   if ((m&3) != 1) { m <<= 2; *pf >>= 1; }
    2498          14 :   return (long)m;
    2499             : }
    2500             : 
    2501             : /* 1+p+...+p^e, e >= 1 */
    2502             : static ulong
    2503          49 : usumpow(ulong p, long e)
    2504             : {
    2505          49 :   ulong q = 1+p;
    2506             :   long i;
    2507          49 :   for (i = 1; i < e; i++) q = p*q + 1;
    2508          49 :   return q;
    2509             : }
    2510             : /* Hurwitz(D0 F^2)/ Hurwitz(D0)
    2511             :  * = \sum_{f|F}  f \prod_{p|f} (1-kro(D0/p)/p)
    2512             :  * = \prod_{p^e || F} (1 + (p^e-1) / (p-1) * (p-kro(D0/p))) */
    2513             : static long
    2514         294 : get_sh(long F, long D0)
    2515             : {
    2516         294 :   GEN fa = myfactoru(F), P = gel(fa,1), E = gel(fa,2);
    2517         294 :   long i, l = lg(P), t = 1;
    2518         794 :   for (i = 1; i < l; i++)
    2519             :   {
    2520         500 :     long p = P[i], e = E[i], s = kross(D0,p);
    2521         500 :     if (e == 1) { t *= 1 + p - s; continue; }
    2522         153 :     if (s == 1) { t *= upowuu(p,e); continue; }
    2523          49 :     t *= 1 + usumpow(p,e-1)*(p-s);
    2524             :   }
    2525         294 :   return t;
    2526             : }
    2527             : /* d > 0, d = 0,3 (mod 4). Return 6*hclassno(d); -d must be fundamental
    2528             :  * Faster than quadclassunit up to 5*10^5 or so */
    2529             : static ulong
    2530          42 : hclassno6u_count(ulong d)
    2531             : {
    2532          42 :   ulong a, b, b2, h = 0;
    2533          42 :   int f = 0;
    2534             : 
    2535          42 :   if (d > 500000)
    2536           7 :     return 6 * itou(gel(quadclassunit0(utoineg(d), 0, NULL, 0), 1));
    2537             : 
    2538             :   /* this part would work with -d non fundamental */
    2539          35 :   b = d&1; b2 = (1+d)>>2;
    2540          35 :   if (!b)
    2541             :   {
    2542           0 :     for (a=1; a*a<b2; a++)
    2543           0 :       if (b2%a == 0) h++;
    2544           0 :     f = (a*a==b2); b=2; b2=(4+d)>>2;
    2545             :   }
    2546        7168 :   while (b2*3 < d)
    2547             :   {
    2548        7098 :     if (b2%b == 0) h++;
    2549     1188551 :     for (a=b+1; a*a < b2; a++)
    2550     1181453 :       if (b2%a == 0) h += 2;
    2551        7098 :     if (a*a == b2) h++;
    2552        7098 :     b += 2; b2 = (b*b+d)>>2;
    2553             :   }
    2554          35 :   if (b2*3 == d) return 6*h+2;
    2555          35 :   if (f) return 6*h+3;
    2556          35 :   return 6*h;
    2557             : }
    2558             : /* D > 0; 6 * hclassno(D), using D = D0*F^2 */
    2559             : static long
    2560         336 : hclassno6u_2(ulong D, long D0, long F)
    2561             : {
    2562             :   long h;
    2563         336 :   if (F == 1) h = hclassno6u_count(D);
    2564             :   else
    2565             :   { /* second chance */
    2566         294 :     h = (ulong)cache_get(cache_H, -D0);
    2567         294 :     if (!h) h = hclassno6u_count(-D0);
    2568         294 :     h *= get_sh(F,D0);
    2569             :   }
    2570         336 :   return h;
    2571             : }
    2572             : /* D > 0; 6 * hclassno(D) (6*Hurwitz). Beware, cached value for D (=0,3 mod 4)
    2573             :  * is stored at D>>1 */
    2574             : ulong
    2575      155780 : hclassno6u(ulong D)
    2576             : {
    2577      155780 :   ulong z = (ulong)cache_get(cache_H, D);
    2578             :   long D0, F;
    2579      155780 :   if (z) return z;
    2580         336 :   D0 = mycoredisc2neg(D, &F);
    2581         336 :   return hclassno6u_2(D,D0,F);
    2582             : }
    2583             : /* same, where the decomposition D = D0*F^2 is already known */
    2584             : static ulong
    2585    69751395 : hclassno6u_i(ulong D, long D0, long F)
    2586             : {
    2587    69751395 :   ulong z = (ulong)cache_get(cache_H, D);
    2588    69751395 :   if (z) return z;
    2589           0 :   return hclassno6u_2(D,D0,F);
    2590             : }
    2591             : 
    2592             : #if 0
    2593             : /* D > 0, return h(-D) [ordinary class number].
    2594             :  * Assume consttabh(D or more) was previously called */
    2595             : static long
    2596             : hfromH(long D)
    2597             : {
    2598             :   pari_sp ltop = avma;
    2599             :   GEN m, d, fa = myfactoru(D), P = gel(fa,1), E = gel(fa,2);
    2600             :   GEN VH = caches[cache_H].cache;
    2601             :   long i, nd, S, l = lg(P);
    2602             : 
    2603             :   /* n = d[i] loops through squarefree divisors of f, where f^2 = largest square
    2604             :    * divisor of N = |D|; m[i] = moebius(n) */
    2605             :   nd = 1 << (l-1);
    2606             :   d = cgetg(nd+1, t_VECSMALL);
    2607             :   m = cgetg(nd+1, t_VECSMALL);
    2608             :   d[1] = 1; S = VH[D >> 1]; /* 6 hclassno(-D) */
    2609             :   m[1] = 1; nd = 1;
    2610             :   i = 1;
    2611             :   if (P[1] == 2 && E[1] <= 3) /* need D/n^2 to be a discriminant */
    2612             :   { if (odd(E[1]) || (E[1] == 2 && (D & 15) == 4)) i = 2; }
    2613             :   for (; i<l; i++)
    2614             :   {
    2615             :     long j, p = P[i];
    2616             :     if (E[i] == 1) continue;
    2617             :     for (j=1; j<=nd; j++)
    2618             :     {
    2619             :       long n, s, hn;
    2620             :       d[nd+j] = n = d[j] * p;
    2621             :       m[nd+j] = s = - m[j]; /* moebius(n) */
    2622             :       hn = VH[(D/(n*n)) >> 1]; /* 6 hclassno(-D/n^2) */
    2623             :       if (s > 0) S += hn; else S -= hn;
    2624             :     }
    2625             :     nd <<= 1;
    2626             :   }
    2627             :   return gc_long(ltop, S/6);
    2628             : }
    2629             : #endif
    2630             : /* D < -4 fundamental, h(D), ordinary class number */
    2631             : static long
    2632     6655411 : myh(long D)
    2633             : {
    2634     6655411 :   ulong z = (ulong)cache_get(cache_H, -D);
    2635     6655411 :   if (z) return z/6; /* should be hfromH(-D) if D non-fundamental */
    2636           0 :   return itou(quadclassno(stoi(D)));
    2637             : }
    2638             : 
    2639             : /*************************************************************************/
    2640             : /*                          TRACE FORMULAS                               */
    2641             : /* CHIP primitive, initialize for t_POLMOD output */
    2642             : static GEN
    2643       28630 : mfcharinit(GEN CHIP)
    2644             : {
    2645       28630 :   long n, o, l, vt, N = mfcharmodulus(CHIP);
    2646             :   GEN c, v, V, G, Pn;
    2647       28630 :   if (N == 1) return mkvec2(mkvec(gen_1), pol_x(0));
    2648        4151 :   G = gel(CHIP,1);
    2649        4151 :   v = ncharvecexpo(G, znconrey_normalized(G, gel(CHIP,2)));
    2650        4151 :   l = lg(v); V = cgetg(l, t_VEC);
    2651        4151 :   o = mfcharorder(CHIP);
    2652        4151 :   Pn = mfcharpol(CHIP); vt = varn(Pn);
    2653        4151 :   if (o <= 2)
    2654             :   {
    2655       29974 :     for (n = 1; n < l; n++)
    2656             :     {
    2657       26775 :       if (v[n] < 0) c = gen_0; else c = v[n]? gen_m1: gen_1;
    2658       26775 :       gel(V,n) = c;
    2659             :     }
    2660             :   }
    2661             :   else
    2662             :   {
    2663       16835 :     for (n = 1; n < l; n++)
    2664             :     {
    2665       15883 :       if (v[n] < 0) c = gen_0;
    2666             :       else
    2667             :       {
    2668        8890 :         c = Qab_zeta(v[n], o, vt);
    2669        8890 :         if (typ(c) == t_POL && lg(c) >= lg(Pn)) c = RgX_rem(c, Pn);
    2670             :       }
    2671       15883 :       gel(V,n) = c;
    2672             :     }
    2673             :   }
    2674        4151 :   return mkvec2(V, Pn);
    2675             : }
    2676             : static GEN
    2677      421526 : vchip_lift(GEN VCHI, long x, GEN C)
    2678             : {
    2679      421526 :   GEN V = gel(VCHI,1);
    2680      421526 :   long F = lg(V)-1;
    2681      421526 :   if (F == 1) return C;
    2682       27440 :   x %= F;
    2683       27440 :   if (!x) return C;
    2684       27440 :   if (x <= 0) x += F;
    2685       27440 :   return gmul(C, gel(V, x));
    2686             : }
    2687             : static long
    2688   129646622 : vchip_FC(GEN VCHI) { return lg(gel(VCHI,1))-1; }
    2689             : static GEN
    2690     4741548 : vchip_mod(GEN VCHI, GEN S)
    2691     4741548 : { return (typ(S) == t_POL)? RgX_rem(S, gel(VCHI,2)): S; }
    2692             : static GEN
    2693     1480675 : vchip_polmod(GEN VCHI, GEN S)
    2694     1480675 : { return (typ(S) == t_POL)? mkpolmod(S, gel(VCHI,2)): S; }
    2695             : 
    2696             : /* ceil(m/d) */
    2697             : static long
    2698      135646 : ceildiv(long m, long d)
    2699             : {
    2700             :   long q;
    2701      135646 :   if (!m) return 0;
    2702       40376 :   q = m/d; return m%d? q+1: q;
    2703             : }
    2704             : 
    2705             : /* contribution of scalar matrices in dimension formula */
    2706             : static GEN
    2707      299019 : A1(long N, long k)
    2708      299019 : { return sstoQ(mypsiu(N)*(k-1), 12); }
    2709             : static long
    2710        7497 : ceilA1(long N, long k)
    2711        7497 : { return ceildiv(mypsiu(N) * (k-1), 12); }
    2712             : 
    2713             : /* sturm bound, slightly larger than dimension */
    2714             : long
    2715       28315 : mfsturmNk(long N, long k) { return 1 + (mypsiu(N)*k)/12; }
    2716             : long
    2717        2345 : mfsturmNgk(long N, GEN k)
    2718             : {
    2719        2345 :   long n,d; Qtoss(k,&n,&d);
    2720        2345 :   return 1 + (mypsiu(N)*n)/(d == 1? 12: 24);
    2721             : }
    2722             : static long
    2723          42 : mfsturmmf(GEN F) { return mfsturmNgk(mf_get_N(F), mf_get_gk(F)); }
    2724             : 
    2725             : /* List of all solutions of x^2 + x + 1 = 0 modulo N, x modulo N */
    2726             : static GEN
    2727         539 : sqrtm3modN(long N)
    2728             : {
    2729             :   pari_sp av;
    2730             :   GEN fa, P, E, B, mB, A, Q, T, R, v, gen_m3;
    2731         539 :   long l, i, n, ct, fl3 = 0, Ninit;
    2732         539 :   if (!odd(N) || (N%9) == 0) return cgetg(1,t_VECSMALL);
    2733         511 :   Ninit = N;
    2734         511 :   if ((N%3) == 0) { N /= 3; fl3 = 1; }
    2735         511 :   fa = myfactoru(N); P = gel(fa, 1); E = gel(fa, 2);
    2736         511 :   l = lg(P);
    2737         707 :   for (i = 1; i < l; i++)
    2738         518 :     if ((P[i]%3) == 2) return cgetg(1,t_VECSMALL);
    2739         189 :   A = cgetg(l, t_VECSMALL);
    2740         189 :   B = cgetg(l, t_VECSMALL);
    2741         189 :   mB= cgetg(l, t_VECSMALL);
    2742         189 :   Q = cgetg(l, t_VECSMALL); gen_m3 = utoineg(3);
    2743         385 :   for (i = 1; i < l; i++)
    2744             :   {
    2745         196 :     long p = P[i], e = E[i];
    2746         196 :     Q[i] = upowuu(p,e);
    2747         196 :     B[i] = itou( Zp_sqrt(gen_m3, utoipos(p), e) );
    2748         196 :     mB[i]= Q[i] - B[i];
    2749             :   }
    2750         189 :   ct = 1 << (l-1);
    2751         189 :   T = ZV_producttree(Q);
    2752         189 :   R = ZV_chinesetree(Q,T);
    2753         189 :   v = cgetg(ct+1, t_VECSMALL);
    2754         189 :   av = avma;
    2755         581 :   for (n = 1; n <= ct; n++)
    2756             :   {
    2757         392 :     long m = n-1, r;
    2758         812 :     for (i = 1; i < l; i++)
    2759             :     {
    2760         420 :       A[i] = (m&1L)? mB[i]: B[i];
    2761         420 :       m >>= 1;
    2762             :     }
    2763         392 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2764         392 :     if (fl3) while (r%3) r += N;
    2765         392 :     set_avma(av); v[n] = odd(r) ? (r-1) >> 1 : (r+Ninit-1) >> 1;
    2766             :   }
    2767         189 :   return v;
    2768             : }
    2769             : 
    2770             : /* number of elliptic points of order 3 in X0(N) */
    2771             : static long
    2772        9667 : nu3(long N)
    2773             : {
    2774             :   long i, l;
    2775             :   GEN P;
    2776        9667 :   if (!odd(N) || (N%9) == 0) return 0;
    2777        8617 :   if ((N%3) == 0) N /= 3;
    2778        8617 :   P = gel(myfactoru(N), 1); l = lg(P);
    2779        8617 :   for (i = 1; i < l; i++) if ((P[i]%3) == 2) return 0;
    2780        3864 :   return 1L<<(l-1);
    2781             : }
    2782             : /* number of elliptic points of order 2 in X0(N) */
    2783             : static long
    2784       16695 : nu2(long N)
    2785             : {
    2786             :   long i, l;
    2787             :   GEN P;
    2788       16695 :   if ((N&3L) == 0) return 0;
    2789       16695 :   if (!odd(N)) N >>= 1;
    2790       16695 :   P = gel(myfactoru(N), 1); l = lg(P);
    2791       16695 :   for (i = 1; i < l; i++) if ((P[i]&3L) == 3) return 0;
    2792        3822 :   return 1L<<(l-1);
    2793             : }
    2794             : 
    2795             : /* contribution of elliptic matrices of order 3 in dimension formula
    2796             :  * Only depends on CHIP the primitive char attached to CHI */
    2797             : static GEN
    2798       41027 : A21(long N, long k, GEN CHI)
    2799             : {
    2800             :   GEN res, G, chi, o;
    2801             :   long a21, i, limx, S;
    2802       41027 :   if ((N&1L) == 0) return gen_0;
    2803       20118 :   a21 = k%3 - 1;
    2804       20118 :   if (!a21) return gen_0;
    2805       19474 :   if (N <= 3) return sstoQ(a21, 3);
    2806       10206 :   if (!CHI) return sstoQ(nu3(N) * a21, 3);
    2807         539 :   res = sqrtm3modN(N); limx = (N - 1) >> 1;
    2808         539 :   G = gel(CHI,1); chi = gel(CHI,2);
    2809         539 :   o = gmfcharorder(CHI);
    2810         931 :   for (S = 0, i = 1; i < lg(res); i++)
    2811             :   { /* (x,N) = 1; S += chi(x) + chi(x^2) */
    2812         392 :     long x = res[i];
    2813         392 :     if (x <= limx)
    2814             :     { /* CHI(x)=e(c/o), 3rd-root of 1 */
    2815         196 :       GEN c = znchareval(G, chi, utoi(x), o);
    2816         196 :       if (!signe(c)) S += 2; else S--;
    2817             :     }
    2818             :   }
    2819         539 :   return sstoQ(a21 * S, 3);
    2820             : }
    2821             : 
    2822             : /* List of all square roots of -1 modulo N */
    2823             : static GEN
    2824         595 : sqrtm1modN(long N)
    2825             : {
    2826             :   pari_sp av;
    2827             :   GEN fa, P, E, B, mB, A, Q, T, R, v;
    2828         595 :   long l, i, n, ct, fleven = 0;
    2829         595 :   if ((N&3L) == 0) return cgetg(1,t_VECSMALL);
    2830         595 :   if ((N&1L) == 0) { N >>= 1; fleven = 1; }
    2831         595 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    2832         595 :   l = lg(P);
    2833         945 :   for (i = 1; i < l; i++)
    2834         665 :     if ((P[i]&3L) == 3) return cgetg(1,t_VECSMALL);
    2835         280 :   A = cgetg(l, t_VECSMALL);
    2836         280 :   B = cgetg(l, t_VECSMALL);
    2837         280 :   mB= cgetg(l, t_VECSMALL);
    2838         280 :   Q = cgetg(l, t_VECSMALL);
    2839         574 :   for (i = 1; i < l; i++)
    2840             :   {
    2841         294 :     long p = P[i], e = E[i];
    2842         294 :     Q[i] = upowuu(p,e);
    2843         294 :     B[i] = itou( Zp_sqrt(gen_m1, utoipos(p), e) );
    2844         294 :     mB[i]= Q[i] - B[i];
    2845             :   }
    2846         280 :   ct = 1 << (l-1);
    2847         280 :   T = ZV_producttree(Q);
    2848         280 :   R = ZV_chinesetree(Q,T);
    2849         280 :   v = cgetg(ct+1, t_VECSMALL);
    2850         280 :   av = avma;
    2851         868 :   for (n = 1; n <= ct; n++)
    2852             :   {
    2853         588 :     long m = n-1, r;
    2854        1232 :     for (i = 1; i < l; i++)
    2855             :     {
    2856         644 :       A[i] = (m&1L)? mB[i]: B[i];
    2857         644 :       m >>= 1;
    2858             :     }
    2859         588 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2860         588 :     if (fleven && !odd(r)) r += N;
    2861         588 :     set_avma(av); v[n] = r;
    2862             :   }
    2863         280 :   return v;
    2864             : }
    2865             : 
    2866             : /* contribution of elliptic matrices of order 4 in dimension formula.
    2867             :  * Only depends on CHIP the primitive char attached to CHI */
    2868             : static GEN
    2869       41027 : A22(long N, long k, GEN CHI)
    2870             : {
    2871             :   GEN G, chi, o, res;
    2872             :   long S, a22, i, limx, o2;
    2873       41027 :   if ((N&3L) == 0) return gen_0;
    2874       28756 :   a22 = (k & 3L) - 1; /* (k % 4) - 1 */
    2875       28756 :   if (!a22) return gen_0;
    2876       28756 :   if (N <= 2) return sstoQ(a22, 4);
    2877       17500 :   if (!CHI) return sstoQ(nu2(N)*a22, 4);
    2878         805 :   if (mfcharparity(CHI) == -1) return gen_0;
    2879         595 :   res = sqrtm1modN(N); limx = (N - 1) >> 1;
    2880         595 :   G = gel(CHI,1); chi = gel(CHI,2);
    2881         595 :   o = gmfcharorder(CHI);
    2882         595 :   o2 = itou(o)>>1;
    2883        1183 :   for (S = 0, i = 1; i < lg(res); i++)
    2884             :   { /* (x,N) = 1, S += real(chi(x)) */
    2885         588 :     long x = res[i];
    2886         588 :     if (x <= limx)
    2887             :     { /* CHI(x)=e(c/o), 4th-root of 1 */
    2888         294 :       long c = itou( znchareval(G, chi, utoi(x), o) );
    2889         294 :       if (!c) S++; else if (c == o2) S--;
    2890             :     }
    2891             :   }
    2892         595 :   return sstoQ(a22 * S, 2);
    2893             : }
    2894             : 
    2895             : /* sumdiv(N,d,eulerphi(gcd(d,N/d))) */
    2896             : static long
    2897       37044 : nuinf(long N)
    2898             : {
    2899       37044 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    2900       37044 :   long i, t = 1, l = lg(P);
    2901       78687 :   for (i=1; i<l; i++)
    2902             :   {
    2903       41643 :     long p = P[i], e = E[i];
    2904       41643 :     if (odd(e))
    2905       33418 :       t *= upowuu(p,e>>1) << 1;
    2906             :     else
    2907        8225 :       t *= upowuu(p,(e>>1)-1) * (p+1);
    2908             :   }
    2909       37044 :   return t;
    2910             : }
    2911             : 
    2912             : /* contribution of hyperbolic matrices in dimension formula */
    2913             : static GEN
    2914       41475 : A3(long N, long FC)
    2915             : {
    2916             :   long i, S, NF, l;
    2917             :   GEN D;
    2918       41475 :   if (FC == 1) return sstoQ(nuinf(N),2);
    2919        4431 :   D = mydivisorsu(N); l = lg(D);
    2920        4431 :   S = 0; NF = N/FC;
    2921       33495 :   for (i = 1; i < l; i++)
    2922             :   {
    2923       29064 :     long g = ugcd(D[i], D[l-i]);
    2924       29064 :     if (NF%g == 0) S += myeulerphiu(g);
    2925             :   }
    2926        4431 :   return sstoQ(S, 2);
    2927             : }
    2928             : 
    2929             : /* special contribution in weight 2 in dimension formula */
    2930             : static long
    2931       40642 : A4(long k, long FC)
    2932       40642 : { return (k==2 && FC==1)? 1: 0; }
    2933             : /* gcd(x,N) */
    2934             : static long
    2935   154807842 : myugcd(GEN GCD, ulong x)
    2936             : {
    2937   154807842 :   ulong N = lg(GCD)-1;
    2938   154807842 :   if (x >= N) x %= N;
    2939   154807842 :   return GCD[x+1];
    2940             : }
    2941             : /* 1_{gcd(x,N) = 1} * chi(x), return NULL if 0 */
    2942             : static GEN
    2943   203009541 : mychicgcd(GEN GCD, GEN VCHI, long x)
    2944             : {
    2945   203009541 :   long N = lg(GCD)-1;
    2946   203009541 :   if (N == 1) return gen_1;
    2947   166755330 :   x = umodsu(x, N);
    2948   166755330 :   if (GCD[x+1] != 1) return NULL;
    2949   124226172 :   x %= vchip_FC(VCHI); if (!x) return gen_1;
    2950     6854813 :   return gel(gel(VCHI,1), x);
    2951             : }
    2952             : 
    2953             : /* contribution of scalar matrices to trace formula */
    2954             : static GEN
    2955     4647104 : TA1(long N, long k, GEN VCHI, GEN GCD, long n)
    2956             : {
    2957             :   GEN S;
    2958             :   ulong m;
    2959     4647104 :   if (!uissquareall(n, &m)) return gen_0;
    2960      323302 :   if (m == 1) return A1(N,k); /* common */
    2961      288512 :   S = mychicgcd(GCD, VCHI, m);
    2962      288512 :   return S? gmul(gmul(powuu(m, k-2), A1(N,k)), S): gen_0;
    2963             : }
    2964             : 
    2965             : /* All square roots modulo 4N, x modulo 2N, precomputed to accelerate TA2 */
    2966             : static GEN
    2967      113680 : mksqr(long N)
    2968             : {
    2969      113680 :   pari_sp av = avma;
    2970      113680 :   long x, N2 = N << 1, N4 = N << 2;
    2971      113680 :   GEN v = const_vec(N2, cgetg(1, t_VECSMALL));
    2972      113680 :   gel(v, N2) = mkvecsmall(0); /* x = 0 */
    2973     2869559 :   for (x = 1; x <= N; x++)
    2974             :   {
    2975     2755879 :     long r = (((x*x - 1)%N4) >> 1) + 1;
    2976     2755879 :     gel(v,r) = vecsmall_append(gel(v,r), x);
    2977             :   }
    2978      113680 :   return gerepilecopy(av, v);
    2979             : }
    2980             : 
    2981             : static GEN
    2982      113680 : mkgcd(long N)
    2983             : {
    2984             :   GEN GCD, d;
    2985             :   long i, N2;
    2986      113680 :   if (N == 1) return mkvecsmall(N);
    2987       93415 :   GCD = cgetg(N + 1, t_VECSMALL);
    2988       93415 :   d = GCD+1; /* GCD[i+1] = d[i] = gcd(i,N) = gcd(N-i,N), i = 0..N-1 */
    2989       93415 :   d[0] = N; d[1] = d[N-1] = 1; N2 = N>>1;
    2990       93415 :   for (i = 2; i <= N2; i++) d[i] = d[N-i] = ugcd(N, i);
    2991       93415 :   return GCD;
    2992             : }
    2993             : 
    2994             : /* Table of \sum_{x^2-tx+n=0 mod Ng}chi(x) for all g dividing gcd(N,F),
    2995             :  * F^2 largest such that (t^2-4n)/F^2=0 or 1 mod 4; t >= 0 */
    2996             : static GEN
    2997    12550678 : mutglistall(long t, long N, long NF, GEN VCHI, long n, GEN MUP, GEN li, GEN GCD)
    2998             : {
    2999    12550678 :   long i, lx = lg(li);
    3000    12550678 :   GEN DNF = mydivisorsu(NF), v = zerovec(NF);
    3001    12550678 :   long j, g, lDNF = lg(DNF);
    3002    33276453 :   for (i = 1; i < lx; i++)
    3003             :   {
    3004    20725775 :     long x = (li[i] + t) >> 1, y, lD;
    3005    20725775 :     GEN D, c = mychicgcd(GCD, VCHI, x);
    3006    20725775 :     if (li[i] && li[i] != N)
    3007             :     {
    3008    13053124 :       GEN c2 = mychicgcd(GCD, VCHI, t - x);
    3009    13053124 :       if (c2) c = c? gadd(c, c2): c2;
    3010             :     }
    3011    20725775 :     if (!c) continue;
    3012    12903303 :     y = (x*(x - t) + n) / N; /* exact division */
    3013    12903303 :     D = mydivisorsu(ugcd(labs(y), NF)); lD = lg(D);
    3014    12903303 :     for (j=1; j < lD; j++) { g = D[j]; gel(v,g) = gadd(gel(v,g), c); }
    3015             :   }
    3016             :   /* j = 1 corresponds to g = 1, and MUP[1] = 1 */
    3017    12550678 :   for (j=2; j < lDNF; j++) { g = DNF[j]; gel(v,g) = gmulsg(MUP[g], gel(v,g)); }
    3018    12550678 :   return v;
    3019             : }
    3020             : 
    3021             : /* special case (N,F) = 1: easier */
    3022             : static GEN
    3023    72996350 : mutg1(long t, long N, GEN VCHI, GEN li, GEN GCD)
    3024             : { /* (N,F) = 1 */
    3025    72996350 :   GEN S = NULL;
    3026    72996350 :   long i, lx = lg(li);
    3027   152584957 :   for (i = 1; i < lx; i++)
    3028             :   {
    3029    79588607 :     long x = (li[i] + t) >> 1;
    3030    79588607 :     GEN c = mychicgcd(GCD, VCHI, x);
    3031    79588607 :     if (c) S = S? gadd(S, c): c;
    3032    79588607 :     if (li[i] && li[i] != N)
    3033             :     {
    3034    42181825 :       c = mychicgcd(GCD, VCHI, t - x);
    3035    42181825 :       if (c) S = S? gadd(S, c): c;
    3036             :     }
    3037    79588607 :     if (S && !signe(S)) S = NULL; /* strive hard to add gen_0 */
    3038             :   }
    3039    72996350 :   return S; /* single value */
    3040             : }
    3041             : 
    3042             : /* Gegenbauer pol; n > 2, P = \sum_{0<=j<=n/2} (-1)^j (n-j)!/j!(n-2*j)! X^j */
    3043             : static GEN
    3044      350322 : mfrhopol(long n)
    3045             : {
    3046             : #ifdef LONG_IS_64BIT
    3047      300276 :   const long M = 2642249;
    3048             : #else
    3049       50046 :   const long M = 1629;
    3050             : #endif
    3051      350322 :   long j, d = n >> 1; /* >= 1 */
    3052      350322 :   GEN P = cgetg(d + 3, t_POL);
    3053             : 
    3054      350322 :   if (n > M) pari_err_IMPL("mfrhopol for large weight"); /* avoid overflow */
    3055      350322 :   P[1] = evalvarn(0)|evalsigne(1);
    3056      350322 :   gel(P,2) = gen_1;
    3057      350322 :   gel(P,3) = utoineg(n-1); /* j = 1 */
    3058      350322 :   if (d > 1) gel(P,4) = utoipos(((n-3)*(n-2)) >> 1); /* j = 2 */
    3059      350322 :   if (d > 2) gel(P,5) = utoineg(((n-5)*(n-4)*(n-3)) / 6); /* j = 3 */
    3060     1289155 :   for (j = 4; j <= d; j++)
    3061      938833 :     gel(P,j+2) = divis(mulis(gel(P,j+1), (n-2*j+1)*(n-2*j+2)), (n-j+1)*(-j));
    3062      350322 :   return P;
    3063             : }
    3064             : 
    3065             : /* polrecip(Q)(t2), assume Q(0) = 1 */
    3066             : static GEN
    3067     2985598 : ZXrecip_u_eval(GEN Q, ulong t2)
    3068             : {
    3069     2985598 :   GEN T = addiu(gel(Q,3), t2);
    3070     2985598 :   long l = lg(Q), j;
    3071     2985598 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mului(t2, T));
    3072     2985598 :   return T;
    3073             : }
    3074             : /* return sh * sqrt(n)^nu * G_nu(t/(2*sqrt(n))) for t != 0
    3075             :  * else (sh/2) * sqrt(n)^nu * G_nu(0) [ implies nu is even ]
    3076             :  * G_nu(z) = \sum_{0<=j<=nu/2} (-1)^j (nu-j)!/j!(nu-2*j)! * (2z)^(nu-2*j)) */
    3077             : static GEN
    3078    77134624 : mfrhopowsimp(GEN Q, GEN sh, long nu, long t, long t2, long n)
    3079             : {
    3080             :   GEN T;
    3081    77134624 :   switch (nu)
    3082             :   {
    3083    69252169 :     case 0: return t? sh: gmul2n(sh,-1);
    3084     3464468 :     case 1: return gmulsg(t, sh);
    3085     1394197 :     case 2: return t? gmulsg(t2 - n, sh): gmul(gmul2n(stoi(-n), -1), sh);
    3086         469 :     case 3: return gmul(mulss(t, t2 - 2*n), sh);
    3087             :     default:
    3088     3023321 :       if (!t) return gmul(gmul2n(gel(Q, lg(Q) - 1), -1), sh);
    3089     2985598 :       T = ZXrecip_u_eval(Q, t2); if (odd(nu)) T = mulsi(t, T);
    3090     2985598 :       return gmul(T, sh);
    3091             :   }
    3092             : }
    3093             : 
    3094             : /* contribution of elliptic matrices to trace formula */
    3095             : static GEN
    3096     4647104 : TA2(long N, long k, GEN VCHI, long n, GEN SQRTS, GEN MUP, GEN GCD)
    3097             : {
    3098     4647104 :   const long n4 = n << 2, N4 = N << 2, nu = k - 2;
    3099     4647104 :   const long st = (!odd(N) && odd(n)) ? 2 : 1;
    3100             :   long limt, t;
    3101             :   GEN S, Q;
    3102             : 
    3103     4647104 :   limt = usqrt(n4);
    3104     4647104 :   if (limt*limt == n4) limt--;
    3105     4647104 :   Q = nu > 3 ? ZX_z_unscale(mfrhopol(nu), n) : NULL;
    3106     4647104 :   S = gen_0;
    3107   146860763 :   for (t = odd(k)? st: 0; t <= limt; t += st) /* t^2 < 4n */
    3108             :   {
    3109   142213659 :     pari_sp av = avma;
    3110   142213659 :     long t2 = t*t, D = n4 - t2, F, D0, NF;
    3111             :     GEN sh, li;
    3112             : 
    3113   142213659 :     li = gel(SQRTS, (umodsu(-D - 1, N4) >> 1) + 1);
    3114   207292694 :     if (lg(li) == 1) continue;
    3115    85547028 :     D0 = mycoredisc2neg(D, &F);
    3116    85547028 :     NF = myugcd(GCD, F);
    3117    85547028 :     if (NF == 1)
    3118             :     { /* (N,F) = 1 => single value in mutglistall */
    3119    72996350 :       GEN mut = mutg1(t, N, VCHI, li, GCD);
    3120    72996350 :       if (!mut) { set_avma(av); continue; }
    3121    69751395 :       sh = gmul(sstoQ(hclassno6u_i(D,D0,F),6), mut);
    3122             :     }
    3123             :     else
    3124             :     {
    3125    12550678 :       GEN v = mutglistall(t, N, NF, VCHI, n, MUP, li, GCD);
    3126    12550678 :       GEN DF = mydivisorsu(F);
    3127    12550678 :       long i, lDF = lg(DF);
    3128    12550678 :       sh = gen_0;
    3129    48515523 :       for (i = 1; i < lDF; i++)
    3130             :       {
    3131    35964845 :         long Ff, f = DF[i], g = myugcd(GCD, f);
    3132    35964845 :         GEN mut = gel(v, g);
    3133    35964845 :         if (gequal0(mut)) continue;
    3134    18294549 :         Ff = DF[lDF-i]; /* F/f */
    3135    18294549 :         if (Ff == 1) sh = gadd(sh, mut);
    3136             :         else
    3137             :         {
    3138    12947501 :           GEN P = gel(myfactoru(Ff), 1);
    3139    12947501 :           long j, lP = lg(P);
    3140    12947501 :           for (j = 1; j < lP; j++) { long p = P[j]; Ff -= kross(D0, p)*Ff/p; }
    3141    12947501 :           sh = gadd(sh, gmulsg(Ff, mut));
    3142             :         }
    3143             :       }
    3144    12550678 :       if (gequal0(sh)) { set_avma(av); continue; }
    3145     7383229 :       if (D0 == -3) sh = gdivgs(sh, 3);
    3146     7005411 :       else if (D0 == -4) sh = gdivgs(sh, 2);
    3147     6655411 :       else sh = gmulgs(sh, myh(D0));
    3148             :     }
    3149    77134624 :     S = gerepileupto(av, gadd(S, mfrhopowsimp(Q,sh,nu,t,t2,n)));
    3150             :   }
    3151     4647104 :   return S;
    3152             : }
    3153             : 
    3154             : /* compute global auxiliary data for TA3 */
    3155             : static GEN
    3156      113680 : mkbez(long N, long FC)
    3157             : {
    3158      113680 :   long ct, i, NF = N/FC;
    3159      113680 :   GEN w, D = mydivisorsu(N);
    3160      113680 :   long l = lg(D);
    3161             : 
    3162      113680 :   w = cgetg(l, t_VEC);
    3163      324660 :   for (i = ct = 1; i < l; i++)
    3164             :   {
    3165      304395 :     long u, v, h, c = D[i], Nc = D[l-i];
    3166      304395 :     if (c > Nc) break;
    3167      210980 :     h = cbezout(c, Nc, &u, &v);
    3168      210980 :     if (h == 1) /* shortcut */
    3169      155267 :       gel(w, ct++) = mkvecsmall4(1,u*c,1,i);
    3170       55713 :     else if (!(NF%h))
    3171       49987 :       gel(w, ct++) = mkvecsmall4(h,u*(c/h),myeulerphiu(h),i);
    3172             :   }
    3173      113680 :   setlg(w,ct); stackdummy((pari_sp)(w+ct),(pari_sp)(w+l));
    3174      113680 :   return w;
    3175             : }
    3176             : 
    3177             : /* contribution of hyperbolic matrices to trace formula, d * nd = n,
    3178             :  * DN = divisorsu(N) */
    3179             : static GEN
    3180    19771024 : auxsum(GEN VCHI, GEN GCD, long d, long nd, GEN DN, GEN BEZ)
    3181             : {
    3182    19771024 :   GEN S = gen_0;
    3183    19771024 :   long ct, g = nd - d, lDN = lg(DN), lBEZ = lg(BEZ);
    3184    51968658 :   for (ct = 1; ct < lBEZ; ct++)
    3185             :   {
    3186    32197634 :     GEN y, B = gel(BEZ, ct);
    3187    32197634 :     long ic, c, Nc, uch, h = B[1];
    3188    32197634 :     if (g%h) continue;
    3189    31516961 :     uch = B[2];
    3190    31516961 :     ic  = B[4];
    3191    31516961 :     c = DN[ic];
    3192    31516961 :     Nc= DN[lDN - ic]; /* Nc = N/c */
    3193    31516961 :     if (ugcd(Nc, nd) == 1)
    3194    24943191 :       y = mychicgcd(GCD, VCHI, d + uch*g); /* 0 if (c,d) > 1 */
    3195             :     else
    3196     6573770 :       y = NULL;
    3197    31516961 :     if (c != Nc && ugcd(Nc, d) == 1)
    3198             :     {
    3199    22228507 :       GEN y2 = mychicgcd(GCD, VCHI, nd - uch*g); /* 0 if (c,nd) > 1 */
    3200    22228507 :       if (y2) y = y? gadd(y, y2): y2;
    3201             :     }
    3202    31516961 :     if (y) S = gadd(S, gmulsg(B[3], y));
    3203             :   }
    3204    19771024 :   return S;
    3205             : }
    3206             : 
    3207             : static GEN
    3208     4647104 : TA3(long N, long k, GEN VCHI, GEN GCD, GEN Dn, GEN BEZ)
    3209             : {
    3210     4647104 :   GEN S = gen_0, DN = mydivisorsu(N);
    3211     4647104 :   long i, l = lg(Dn);
    3212    24418128 :   for (i = 1; i < l; i++)
    3213             :   {
    3214    24383338 :     long d = Dn[i], nd = Dn[l-i]; /* = n/d */
    3215             :     GEN t, u;
    3216    24383338 :     if (d > nd) break;
    3217    19771024 :     t = auxsum(VCHI, GCD, d, nd, DN, BEZ);
    3218    19771024 :     if (isintzero(t)) continue;
    3219    18466448 :     u = powuu(d,k-1); if (d == nd) u = gmul2n(u,-1);
    3220    18466448 :     S = gadd(S, gmul(u,t));
    3221             :   }
    3222     4647104 :   return S;
    3223             : }
    3224             : 
    3225             : /* special contribution in weight 2 in trace formula */
    3226             : static long
    3227     4647104 : TA4(long k, GEN VCHIP, GEN Dn, GEN GCD)
    3228             : {
    3229             :   long i, l, S;
    3230     4647104 :   if (k != 2 || vchip_FC(VCHIP) != 1) return 0;
    3231     3961174 :   l = lg(Dn); S = 0;
    3232    37257143 :   for (i = 1; i < l; i++)
    3233             :   {
    3234    33295969 :     long d = Dn[i]; /* gcd(N,n/d) == 1? */
    3235    33295969 :     if (myugcd(GCD, Dn[l-i]) == 1) S += d;
    3236             :   }
    3237     3961174 :   return S;
    3238             : }
    3239             : 
    3240             : /* precomputation of products occurring im mutg, again to accelerate TA2 */
    3241             : static GEN
    3242      113680 : mkmup(long N)
    3243             : {
    3244      113680 :   GEN fa = myfactoru(N), P = gel(fa,1), D = divisorsu_fact(fa);
    3245      113680 :   long i, lP = lg(P), lD = lg(D);
    3246      113680 :   GEN MUP = zero_zv(N);
    3247      113680 :   MUP[1] = 1;
    3248      388430 :   for (i = 2; i < lD; i++)
    3249             :   {
    3250      274750 :     long j, g = D[i], Ng = D[lD-i]; /*  N/g */
    3251      274750 :     for (j = 1; j < lP; j++) { long p = P[j]; if (Ng%p) g += g/p; }
    3252      274750 :     MUP[D[i]] = g;
    3253             :   }
    3254      113680 :   return MUP;
    3255             : }
    3256             : 
    3257             : /* quadratic non-residues mod p; p odd prime, p^2 fits in a long */
    3258             : static GEN
    3259        1456 : non_residues(long p)
    3260             : {
    3261        1456 :   long i, j, p2 = p >> 1;
    3262        1456 :   GEN v = cgetg(p2+1, t_VECSMALL), w = const_vecsmall(p-1, 1);
    3263        1456 :   for (i = 2; i <= p2; i++) w[(i*i) % p] = 0; /* no need to check 1 */
    3264        1456 :   for (i = 2, j = 1; i < p; i++) if (w[i]) v[j++] = i;
    3265        1456 :   return v;
    3266             : }
    3267             : 
    3268             : /* CHIP primitive. Return t_VECSMALL v of length q such that
    3269             :  * Tr^new_{N,CHIP}(n) = 0 whenever v[(n%q) + 1] is non-zero */
    3270             : static GEN
    3271       28658 : mfnewzerodata(long N, GEN CHIP)
    3272             : {
    3273       28658 :   GEN V, M, L, faN = myfactoru(N), PN = gel(faN,1), EN = gel(faN,2);
    3274       28658 :   GEN G = gel(CHIP,1), chi = gel(CHIP,2);
    3275       28658 :   GEN fa = znstar_get_faN(G), P = ZV_to_zv(gel(fa,1)), E = gel(fa,2);
    3276       28658 :   long i, mod, j = 1, l = lg(PN);
    3277             : 
    3278       28658 :   M = cgetg(l, t_VECSMALL); M[1] = 0;
    3279       28658 :   V = cgetg(l, t_VEC);
    3280             :   /* Tr^new(n) = 0 if (n mod M[i]) in V[i]  */
    3281       28658 :   if ((N & 3) == 0)
    3282             :   {
    3283       10738 :     long e = EN[1];
    3284       10738 :     long c = (lg(P) > 1 && P[1] == 2)? E[1]: 0; /* c = v_2(FC) */
    3285             :     /* e >= 2 */
    3286       10738 :     if (c == e-1) return NULL; /* Tr^new = 0 */
    3287       10703 :     if (c == e)
    3288             :     {
    3289        2492 :       if (e == 2)
    3290             :       { /* sc: -4 */
    3291        1722 :         gel(V,1) = mkvecsmall(3);
    3292        1722 :         M[1] = 4;
    3293             :       }
    3294         770 :       else if (e == 3)
    3295             :       { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3296         770 :         long t = signe(gel(chi,1))? 7: 3;
    3297         770 :         gel(V,1) = mkvecsmall2(5, t);
    3298         770 :         M[1] = 8;
    3299             :       }
    3300             :     }
    3301        8211 :     else if (e == 5 && c == 3)
    3302         154 :     { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3303         154 :       long t = signe(gel(chi,1))? 7: 3;
    3304         154 :       gel(V,1) = mkvecsmalln(6, 2L,4L,5L,6L,8L,t);
    3305         154 :       M[1] = 8;
    3306             :     }
    3307        8057 :     else if ((e == 4 && c == 2) || (e == 5 && c <= 2) || (e == 6 && c <= 2)
    3308        6629 :          || (e >= 7 && c == e - 3))
    3309             :     { /* sc: 4 */
    3310        1428 :       gel(V,1) = mkvecsmall3(0,2,3);
    3311        1428 :       M[1] = 4;
    3312             :     }
    3313        6629 :     else if ((e <= 4 && c == 0) || (e >= 5 && c == e - 2))
    3314             :     { /* sc: 2 */
    3315        6363 :       gel(V,1) = mkvecsmall(0);
    3316        6363 :       M[1] = 2;
    3317             :     }
    3318         266 :     else if ((e == 6 && c == 3) || (e >= 7 && c <= e - 4))
    3319             :     { /* sc: -2 */
    3320         266 :       gel(V,1) = mkvecsmalln(7, 0L,2L,3L,4L,5L,6L,7L);
    3321         266 :       M[1] = 8;
    3322             :     }
    3323             :   }
    3324       28623 :   j = M[1]? 2: 1;
    3325       61621 :   for (i = odd(N)? 1: 2; i < l; i++) /* skip p=2, done above */
    3326             :   {
    3327       32998 :     long p = PN[i], e = EN[i];
    3328       32998 :     long z = zv_search(P, p), c = z? E[z]: 0; /* c = v_p(FC) */
    3329       32998 :     if ((e <= 2 && c == 1 && itos(gel(chi,z)) == (p>>1)) /* ord(CHI_p)=2 */
    3330       32004 :         || (e >= 3 && c <= e - 2))
    3331        1456 :     { /* sc: -p */
    3332        1456 :       GEN v = non_residues(p);
    3333        1456 :       if (e != 1) v = vecsmall_prepend(v, 0);
    3334        1456 :       gel(V,j) = v;
    3335        1456 :       M[j] = p; j++;
    3336             :     }
    3337       31542 :     else if (e >= 2 && c < e)
    3338             :     { /* sc: p */
    3339        2107 :       gel(V,j) = mkvecsmall(0);
    3340        2107 :       M[j] = p; j++;
    3341             :     }
    3342             :   }
    3343       28623 :   if (j == 1) return cgetg(1, t_VECSMALL);
    3344       12740 :   setlg(V,j); setlg(M,j); mod = zv_prod(M);
    3345       12740 :   L = zero_zv(mod);
    3346       27006 :   for (i = 1; i < j; i++)
    3347             :   {
    3348       14266 :     GEN v = gel(V,i);
    3349       14266 :     long s, m = M[i], lv = lg(v);
    3350       36764 :     for (s = 1; s < lv; s++)
    3351             :     {
    3352       22498 :       long a = v[s] + 1;
    3353       32284 :       do { L[a] = 1; a += m; } while (a <= mod);
    3354             :     }
    3355             :   }
    3356       12740 :   return L;
    3357             : }
    3358             : /* v=mfnewzerodata(N,CHI); returns TRUE if newtrace(n) must be zero,
    3359             :  * (but newtrace(n) may still be zero if we return FALSE) */
    3360             : static long
    3361     1866921 : mfnewchkzero(GEN v, long n) { long q = lg(v)-1; return q && v[(n%q) + 1]; }
    3362             : 
    3363             : /* if (!VCHIP): from mftraceform_cusp;
    3364             :  * else from initnewtrace and CHI is known to be primitive */
    3365             : static GEN
    3366      113680 : inittrace(long N, GEN CHI, GEN VCHIP)
    3367             : {
    3368             :   long FC;
    3369      113680 :   if (VCHIP)
    3370      113673 :     FC = mfcharmodulus(CHI);
    3371             :   else
    3372           7 :     VCHIP = mfcharinit(mfchartoprimitive(CHI, &FC));
    3373      113680 :   return mkvecn(5, mksqr(N), mkmup(N), mkgcd(N), VCHIP, mkbez(N, FC));
    3374             : }
    3375             : 
    3376             : /* p > 2 prime; return a sorted t_VECSMALL of primes s.t Tr^new(p) = 0 for all
    3377             :  * weights > 2 */
    3378             : static GEN
    3379       28623 : inittrconj(long N, long FC)
    3380             : {
    3381             :   GEN fa, P, E, v;
    3382             :   long i, k, l;
    3383             : 
    3384       28623 :   if (FC != 1) return cgetg(1,t_VECSMALL);
    3385             : 
    3386       24472 :   fa = myfactoru(N >> vals(N));
    3387       24472 :   P = gel(fa,1); l = lg(P);
    3388       24472 :   E = gel(fa,2);
    3389       24472 :   v = cgetg(l, t_VECSMALL);
    3390       53725 :   for (i = k = 1; i < l; i++)
    3391             :   {
    3392       29253 :     long j, p = P[i]; /* > 2 */
    3393       70966 :     for (j = 1; j < l; j++)
    3394       41713 :       if (j != i && E[j] == 1 && kross(-p, P[j]) == 1) v[k++] = p;
    3395             :   }
    3396       24472 :   setlg(v,k); return v;
    3397             : }
    3398             : 
    3399             : /* assume CHIP primitive, f(CHIP) | N; NZ = mfnewzerodata(N,CHIP) */
    3400             : static GEN
    3401       28623 : initnewtrace_i(long N, GEN CHIP, GEN NZ)
    3402             : {
    3403       28623 :   GEN T = const_vec(N, cgetg(1,t_VEC)), D, VCHIP;
    3404       28623 :   long FC = mfcharmodulus(CHIP), N1, N2, i, l;
    3405             : 
    3406       28623 :   if (!NZ) NZ = mkvecsmall(1); /*Tr^new = 0; initialize data nevertheless*/
    3407       28623 :   VCHIP = mfcharinit(CHIP);
    3408       28623 :   N1 = N/FC; newd_params(N1, &N2);
    3409       28623 :   D = mydivisorsu(N1/N2); l = lg(D);
    3410       28623 :   N2 *= FC;
    3411      142296 :   for (i = 1; i < l; i++)
    3412             :   {
    3413      113673 :     long M = D[i]*N2;
    3414      113673 :     gel(T,M) = inittrace(M, CHIP, VCHIP);
    3415             :   }
    3416       28623 :   gel(T,N) = shallowconcat(gel(T,N), mkvec2(NZ, inittrconj(N,FC)));
    3417       28623 :   return T;
    3418             : }
    3419             : /* don't initialize if Tr^new = 0, return NULL */
    3420             : static GEN
    3421       28658 : initnewtrace(long N, GEN CHI)
    3422             : {
    3423       28658 :   GEN CHIP = mfchartoprimitive(CHI, NULL), NZ = mfnewzerodata(N,CHIP);
    3424       28658 :   return NZ? initnewtrace_i(N, CHIP, NZ): NULL;
    3425             : }
    3426             : 
    3427             : /* (-1)^k */
    3428             : static long
    3429        7329 : m1pk(long k) { return odd(k)? -1 : 1; }
    3430             : static long
    3431        7007 : badchar(long N, long k, GEN CHI)
    3432        7007 : { return mfcharparity(CHI) != m1pk(k) || (CHI && N % mfcharconductor(CHI)); }
    3433             : 
    3434             : /* dimension of space of cusp forms S_k(\G_0(N),CHI)
    3435             :  * Only depends on CHIP the primitive char attached to CHI */
    3436             : long
    3437       40649 : mfcuspdim(long N, long k, GEN CHI)
    3438             : {
    3439       40649 :   pari_sp av = avma;
    3440             :   long FC;
    3441             :   GEN s;
    3442       40649 :   if (k <= 0) return 0;
    3443       40649 :   if (k == 1) return mfwt1cuspdim(N, CHI);
    3444       40460 :   FC = CHI? mfcharconductor(CHI): 1;
    3445       40460 :   if (FC == 1) CHI = NULL;
    3446       40460 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3447       40460 :   s = gadd(s, gsubsg(A4(k, FC), A3(N, FC)));
    3448       40460 :   return gc_long(av, itos(s));
    3449             : }
    3450             : 
    3451             : /* dimension of whole space M_k(\G_0(N),CHI)
    3452             :  * Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3453             : long
    3454         784 : mffulldim(long N, long k, GEN CHI)
    3455             : {
    3456         784 :   pari_sp av = avma;
    3457         784 :   long FC = CHI? mfcharconductor(CHI): 1;
    3458             :   GEN s;
    3459         784 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3460         784 :   if (k == 1) return gc_long(av, itos(A3(N, FC)) + mfwt1cuspdim(N, CHI));
    3461         567 :   if (FC == 1) CHI = NULL;
    3462         567 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3463         567 :   s = gadd(s, A3(N, FC));
    3464         567 :   return gc_long(av, itos(s));
    3465             : }
    3466             : 
    3467             : /* Dimension of the space of Eisenstein series */
    3468             : long
    3469         231 : mfeisensteindim(long N, long k, GEN CHI)
    3470             : {
    3471         231 :   pari_sp av = avma;
    3472         231 :   long s, FC = CHI? mfcharconductor(CHI): 1;
    3473         231 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3474         231 :   s = itos(gmul2n(A3(N, FC), 1));
    3475         231 :   if (k > 1) s -= A4(k, FC); else s >>= 1;
    3476         231 :   return gc_long(av,s);
    3477             : }
    3478             : 
    3479             : enum { _SQRTS = 1, _MUP, _GCD, _VCHIP, _BEZ, _NEWLZ, _TRCONJ };
    3480             : /* Trace of T(n) on space of cuspforms; only depends on CHIP the primitive char
    3481             :  * attached to CHI */
    3482             : static GEN
    3483     4647104 : mfcusptrace_i(long N, long k, long n, GEN Dn, GEN S)
    3484             : {
    3485     4647104 :   pari_sp av = avma;
    3486             :   GEN a, b, VCHIP, GCD;
    3487             :   long t;
    3488     4647104 :   if (!n) return gen_0;
    3489     4647104 :   VCHIP = gel(S,_VCHIP);
    3490     4647104 :   GCD = gel(S,_GCD);
    3491     4647104 :   t = TA4(k, VCHIP, Dn, GCD);
    3492     4647104 :   a = TA1(N, k, VCHIP, GCD, n); if (t) a = gaddgs(a,t);
    3493     4647104 :   b = TA2(N, k, VCHIP, n, gel(S,_SQRTS), gel(S,_MUP), GCD);
    3494     4647104 :   b = gadd(b, TA3(N, k, VCHIP, GCD, Dn, gel(S,_BEZ)));
    3495     4647104 :   b = gsub(a,b);
    3496     4647104 :   if (typ(b) != t_POL) return gerepileupto(av, b);
    3497       40341 :   return gerepilecopy(av, vchip_polmod(VCHIP, b));
    3498             : }
    3499             : 
    3500             : static GEN
    3501     5967745 : mfcusptracecache(long N, long k, long n, GEN Dn, GEN S, cachenew_t *cache)
    3502             : {
    3503     5967745 :   GEN C = NULL, T = gel(cache->vfull,N);
    3504     5967745 :   long lcache = lg(T);
    3505     5967745 :   if (n < lcache) C = gel(T, n);
    3506     5967745 :   if (C) cache->cuspHIT++; else C = mfcusptrace_i(N, k, n, Dn, S);
    3507     5967745 :   cache->cuspTOTAL++;
    3508     5967745 :   if (n < lcache) gel(T,n) = C;
    3509     5967745 :   return C;
    3510             : }
    3511             : 
    3512             : /* return the divisors of n, known to be among the elements of D */
    3513             : static GEN
    3514      361459 : div_restrict(GEN D, ulong n)
    3515             : {
    3516             :   long i, j, l;
    3517      361459 :   GEN v, VDIV = caches[cache_DIV].cache;
    3518      361459 :   if (lg(VDIV) > n) return gel(VDIV,n);
    3519           0 :   l = lg(D);
    3520           0 :   v = cgetg(l, t_VECSMALL);
    3521           0 :   for (i = j = 1; i < l; i++)
    3522             :   {
    3523           0 :     ulong d = D[i];
    3524           0 :     if (n % d == 0) v[j++] = d;
    3525             :   }
    3526           0 :   setlg(v,j); return v;
    3527             : }
    3528             : 
    3529             : /* for some prime divisors of N, Tr^new(p) = 0 */
    3530             : static int
    3531      225190 : trconj(GEN T, long N, long n)
    3532      225190 : { return (lg(T) > 1 && N % n == 0 && zv_search(T, n)); }
    3533             : 
    3534             : /* n > 0; trace formula on new space */
    3535             : static GEN
    3536     1866921 : mfnewtrace_i(long N, long k, long n, cachenew_t *cache)
    3537             : {
    3538     1866921 :   GEN VCHIP, s, Dn, DN1, SN, S = cache->DATA;
    3539             :   long FC, N1, N2, N1N2, g, i, j, lDN1;
    3540             : 
    3541     1866921 :   if (!S) return gen_0;
    3542     1866921 :   SN = gel(S,N);
    3543     1866921 :   if (mfnewchkzero(gel(SN,_NEWLZ), n)) return gen_0;
    3544     1440369 :   if (k > 2 && trconj(gel(SN,_TRCONJ), N, n)) return gen_0;
    3545     1440334 :   VCHIP = gel(SN, _VCHIP); FC = vchip_FC(VCHIP);
    3546     1440334 :   N1 = N/FC; newt_params(N1, n, FC, &g, &N2);
    3547     1440334 :   N1N2 = N1/N2;
    3548     1440334 :   DN1 = mydivisorsu(N1N2); lDN1 = lg(DN1);
    3549     1440334 :   N2 *= FC;
    3550     1440334 :   Dn = mydivisorsu(n); /* this one is probably out of cache */
    3551     1440334 :   s = gmulsg(mubeta2(N1N2,n), mfcusptracecache(N2, k, n, Dn, gel(S,N2), cache));
    3552     5606286 :   for (i = 2; i < lDN1; i++)
    3553             :   { /* skip M1 = 1, done above */
    3554     4165952 :     long M1 = DN1[i], N1M1 = DN1[lDN1-i];
    3555     4165952 :     GEN Dg = mydivisorsu(ugcd(M1, g));
    3556     4165952 :     M1 *= N2;
    3557     4165952 :     s = gadd(s, gmulsg(mubeta2(N1M1,n),
    3558     4165952 :                        mfcusptracecache(M1, k, n, Dn, gel(S,M1), cache)));
    3559     4527411 :     for (j = 2; j < lg(Dg); j++) /* skip d = 1, done above */
    3560             :     {
    3561      361459 :       long d = Dg[j], ndd = n/(d*d), M = M1/d;
    3562      361459 :       GEN z = mulsi(mubeta2(N1M1,ndd), powuu(d,k-1)), C = vchip_lift(VCHIP,d,z);
    3563      361459 :       GEN Dndd = div_restrict(Dn, ndd);
    3564      361459 :       s = gadd(s, gmul(C, mfcusptracecache(M, k, ndd, Dndd, gel(S,M), cache)));
    3565             :     }
    3566     4165952 :     s = vchip_mod(VCHIP, s);
    3567             :   }
    3568     1440334 :   return vchip_polmod(VCHIP, s);
    3569             : }
    3570             : 
    3571             : /* mfcuspdim(N,k,CHI) - mfnewdim(N,k,CHI); CHIP primitive (for efficiency) */
    3572             : static long
    3573        7721 : mfolddim_i(long N, long k, GEN CHIP)
    3574             : {
    3575        7721 :   long S, i, l, FC = mfcharmodulus(CHIP), N1 = N/FC, N2;
    3576             :   GEN D;
    3577        7721 :   newd_params(N1, &N2); /* will ensure mubeta != 0 */
    3578        7721 :   D = mydivisorsu(N1/N2); l = lg(D);
    3579        7721 :   N2 *= FC; S = 0;
    3580       30289 :   for (i = 2; i < l; i++)
    3581             :   {
    3582       22568 :     long M = D[l-i]*N2, d = mfcuspdim(M, k, CHIP);
    3583       22568 :     if (d) S -= mubeta(D[i]) * d;
    3584             :   }
    3585        7721 :   return S;
    3586             : }
    3587             : long
    3588         399 : mfolddim(long N, long k, GEN CHI)
    3589             : {
    3590         399 :   pari_sp av = avma;
    3591         399 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3592         399 :   return gc_long(av, mfolddim_i(N, k, CHIP));
    3593             : }
    3594             : /* Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3595             : long
    3596       14910 : mfnewdim(long N, long k, GEN CHI)
    3597             : {
    3598             :   pari_sp av;
    3599             :   long S;
    3600       14910 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3601       14910 :   S = mfcuspdim(N, k, CHIP); if (!S) return 0;
    3602        7308 :   av = avma; return gc_long(av, S - mfolddim_i(N, k, CHIP));
    3603             : }
    3604             : 
    3605             : /* trace form, given as closure */
    3606             : static GEN
    3607         924 : mftraceform_new(long N, long k, GEN CHI)
    3608             : {
    3609             :   GEN T;
    3610         924 :   if (k == 1) return initwt1newtrace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3611         903 :   T = initnewtrace(N,CHI); if (!T) return mftrivial();
    3612         903 :   return tag(t_MF_NEWTRACE, mkNK(N,k,CHI), T);
    3613             : }
    3614             : static GEN
    3615          14 : mftraceform_cusp(long N, long k, GEN CHI)
    3616             : {
    3617          14 :   if (k == 1) return initwt1trace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3618           7 :   return tag(t_MF_TRACE, mkNK(N,k,CHI), inittrace(N,CHI,NULL));
    3619             : }
    3620             : static GEN
    3621          91 : mftraceform_i(GEN NK, long space)
    3622             : {
    3623             :   GEN CHI;
    3624             :   long N, k;
    3625          91 :   checkNK(NK, &N, &k, &CHI, 0);
    3626          91 :   if (!mfdim_Nkchi(N, k, CHI, space)) return mftrivial();
    3627          70 :   switch(space)
    3628             :   {
    3629          49 :     case mf_NEW: return mftraceform_new(N, k, CHI);
    3630          14 :     case mf_CUSP:return mftraceform_cusp(N, k, CHI);
    3631             :   }
    3632           7 :   pari_err_DOMAIN("mftraceform", "space", "=", utoi(space), NK);
    3633             :   return NULL;/*LCOV_EXCL_LINE*/
    3634             : }
    3635             : GEN
    3636          91 : mftraceform(GEN NK, long space)
    3637          91 : { pari_sp av = avma; return gerepilecopy(av, mftraceform_i(NK,space)); }
    3638             : 
    3639             : static GEN
    3640       15281 : hecke_data(long N, long n)
    3641       15281 : { return mkvecsmall3(n, u_ppo(n, N), N); }
    3642             : /* 1/2-integral weight */
    3643             : static GEN
    3644          84 : heckef2_data(long N, long n)
    3645             : {
    3646             :   ulong f, fN, fN2;
    3647          84 :   if (!uissquareall(n, &f)) return NULL;
    3648          77 :   fN = u_ppo(f, N); fN2 = fN*fN;
    3649          77 :   return mkvec2(myfactoru(fN), mkvecsmall4(n, N, fN2, n/fN2));
    3650             : }
    3651             : /* N = mf_get_N(F) or a multiple */
    3652             : static GEN
    3653       22057 : mfhecke_i(long n, long N, GEN F)
    3654             : {
    3655       22057 :   if (n == 1) return F;
    3656       15071 :   return tag2(t_MF_HECKE, mf_get_NK(F), hecke_data(N,n), F);
    3657             : }
    3658             : 
    3659             : GEN
    3660         105 : mfhecke(GEN mf, GEN F, long n)
    3661             : {
    3662         105 :   pari_sp av = avma;
    3663             :   GEN NK, CHI, gk, DATA;
    3664             :   long N, nk, dk;
    3665         105 :   mf = checkMF(mf);
    3666         105 :   if (!checkmf_i(F)) pari_err_TYPE("mfhecke",F);
    3667         105 :   if (n <= 0) pari_err_TYPE("mfhecke [n <= 0]", stoi(n));
    3668         105 :   if (n == 1) return gcopy(F);
    3669         105 :   gk = mf_get_gk(F);
    3670         105 :   Qtoss(gk,&nk,&dk);
    3671         105 :   CHI = mf_get_CHI(F);
    3672         105 :   N = MF_get_N(mf);
    3673         105 :   if (dk == 2)
    3674             :   {
    3675          77 :     DATA = heckef2_data(N,n);
    3676          77 :     if (!DATA) return mftrivial();
    3677             :   }
    3678             :   else
    3679          28 :     DATA = hecke_data(N,n);
    3680          98 :   NK = mkgNK(lcmii(stoi(N), mf_get_gN(F)), gk, CHI, mf_get_field(F));
    3681          98 :   return gerepilecopy(av, tag2(t_MF_HECKE, NK, DATA, F));
    3682             : }
    3683             : 
    3684             : /* form F given by closure, compute B(d)(F) as closure (q -> q^d) */
    3685             : static GEN
    3686       28336 : mfbd_i(GEN F, long d)
    3687             : {
    3688             :   GEN D, NK, gk, CHI;
    3689       28336 :   if (d == 1) return F;
    3690        9933 :   if (d <= 0) pari_err_TYPE("mfbd [d <= 0]", stoi(d));
    3691        9933 :   if (mf_get_type(F) != t_MF_BD) D = utoi(d);
    3692           7 :   else { D = mului(d, gel(F,3)); F = gel(F,2); }
    3693        9933 :   gk = mf_get_gk(F); CHI = mf_get_CHI(F);
    3694        9933 :   if (typ(gk) != t_INT) CHI = mfcharmul(CHI, get_mfchar(utoi(d << 2)));
    3695        9933 :   NK = mkgNK(muliu(mf_get_gN(F), d), gk, CHI, mf_get_field(F));
    3696        9933 :   return tag2(t_MF_BD, NK, F, D);
    3697             : }
    3698             : GEN
    3699          35 : mfbd(GEN F, long d)
    3700             : {
    3701          35 :   pari_sp av = avma;
    3702          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfbd",F);
    3703          35 :   return gerepilecopy(av, mfbd_i(F, d));
    3704             : }
    3705             : 
    3706             : /* CHI is a character defined modulo N4 */
    3707             : static GEN
    3708          98 : RgV_shimura(GEN V, long n, long D, long N4, long r, GEN CHI)
    3709             : {
    3710          98 :   GEN R, a0, Pn = mfcharpol(CHI);
    3711          98 :   long m, Da, ND, ord = mfcharorder(CHI), vt = varn(Pn), d4 = D & 3L;
    3712             : 
    3713          98 :   if (d4 == 2 || d4 == 3) D *= 4;
    3714          98 :   Da = labs(D); ND = N4*Da;
    3715          98 :   R = cgetg(n + 2, t_VEC);
    3716          98 :   a0 = gel(V, 1);
    3717          98 :   if (!gequal0(a0))
    3718             :   {
    3719           7 :     long D4 = D << 2;
    3720           7 :     GEN CHID = induceN(ulcm(mfcharmodulus(CHI), labs(D4)), CHI);
    3721           7 :     CHID = mfcharmul_i(CHID, induce(gel(CHID,1), stoi(D4)));
    3722           7 :     a0 = gmul(a0, charLFwtk(r, CHID, mfcharorder(CHID)));
    3723             :   }
    3724          98 :   if (odd(ND) && !odd(mfcharmodulus(CHI))) ND <<= 1;
    3725          98 :   gel(R, 1) = a0;
    3726         567 :   for (m = 1; m <= n; m++)
    3727             :   {
    3728         469 :     GEN Dm = mydivisorsu(u_ppo(m, ND)), S = gel(V, m*m + 1);
    3729         469 :     long i, l = lg(Dm);
    3730         770 :     for (i = 2; i < l; i++)
    3731             :     { /* (e,ND) = 1; skip i = 1: e = 1, done above */
    3732         301 :       long e = Dm[i], me = m / e;
    3733         301 :       long a = mfcharevalord(CHI, e, ord);
    3734         301 :       GEN c, C = powuu(e, r - 1);
    3735         301 :       if (kross(D, e) == -1) C = negi(C);
    3736         301 :       c = Qab_Czeta(a, ord, C, vt);
    3737         301 :       S = gadd(S, gmul(c, gel(V, me*me + 1)));
    3738             :     }
    3739         469 :     gel(R, m+1) = S;
    3740             :   }
    3741          98 :   return degpol(Pn) > 1? gmodulo(R, Pn): R;
    3742             : }
    3743             : static GEN
    3744          28 : c_shimura(long n, GEN F, long D, GEN CHI)
    3745             : {
    3746          28 :   GEN v = mfcoefs_i(F, n*n, labs(D));
    3747          28 :   return RgV_shimura(v, n, D, mf_get_N(F)>>2, mf_get_r(F), CHI);
    3748             : }
    3749             : 
    3750             : static long
    3751          14 : mfisinkohnen(GEN mf, GEN F)
    3752             : {
    3753          14 :   GEN v, gk = MF_get_gk(mf), CHI = MF_get_CHI(mf);
    3754          14 :   long i, sb, eps, N4 = MF_get_N(mf) >> 2, r = MF_get_r(mf);
    3755          14 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
    3756          14 :   eps = N4 % mfcharconductor(CHI)? -1 : 1;
    3757          14 :   if (odd(r)) eps = -eps;
    3758          14 :   v = mfcoefs(F, sb, 1);
    3759         896 :   for (i = 0; i <= sb; i++)
    3760             :   {
    3761         882 :     long j = i & 3L;
    3762         882 :     if ((j == 2 || j == 2 + eps) && !gequal0(gel(v,i+1))) return 0;
    3763             :   }
    3764          14 :   return 1;
    3765             : }
    3766             : 
    3767             : static long
    3768          35 : mfshimura_space_cusp(GEN mf)
    3769             : {
    3770          35 :   long fl = 1, r = MF_get_r(mf), M = MF_get_N(mf) >> 2;
    3771          35 :   if (r == 1 && M >= 4)
    3772             :   {
    3773          14 :     GEN E = gel(myfactoru(M), 2);
    3774          14 :     long ma = vecsmall_max(E);
    3775          14 :     if (ma > 2 || (ma == 2 && !mfcharistrivial(MF_get_CHI(mf)))) fl = 0;
    3776             :   }
    3777          35 :   return fl;
    3778             : }
    3779             : 
    3780             : /* D is either a discriminant (not necessarily fundamental) with
    3781             :    sign(D)=(-1)^{k-1/2}*eps, or a positive squarefree integer t, which is then
    3782             :    transformed into a fundamental discriminant of the correct sign. */
    3783             : GEN
    3784          35 : mfshimura(GEN mf, GEN F, long D)
    3785             : {
    3786          35 :   pari_sp av = avma;
    3787             :   GEN gk, G, res, mf2, CHI, CHIP;
    3788          35 :   long M, r, space, cusp, N4, flagdisc = 0;
    3789          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfshimura",F);
    3790          35 :   gk = mf_get_gk(F);
    3791          35 :   if (typ(gk) != t_FRAC) pari_err_TYPE("mfshimura [integral weight]", F);
    3792          35 :   r = MF_get_r(mf);
    3793          35 :   if (r <= 0) pari_err_DOMAIN("mfshimura", "weight", "<=", ghalf, gk);
    3794          35 :   N4 = MF_get_N(mf) >> 2; CHI = MF_get_CHI(mf);
    3795          35 :   CHIP = mfcharchiliftprim(CHI, N4);
    3796          35 :   if (!CHIP) CHIP = CHI;
    3797             :   else
    3798             :   {
    3799          35 :     long epsD = CHI == CHIP? D: -D, rd = D & 3L;
    3800          35 :     if (odd(r)) epsD = -epsD;
    3801          35 :     if (epsD > 0 && (rd == 0 || rd == 1)) flagdisc = 1;
    3802             :     else
    3803             :     {
    3804          14 :       if (D < 0 || !uissquarefree(D))
    3805           7 :         pari_err_TYPE("shimura [incorrect D]", stoi(D));
    3806           7 :       D = epsD;
    3807             :     }
    3808             :   }
    3809          28 :   M = N4;
    3810          28 :   cusp = mfiscuspidal(mf,F);
    3811          28 :   space = cusp && mfshimura_space_cusp(mf)? mf_CUSP : mf_FULL;
    3812          28 :   if (!cusp || !flagdisc || !mfisinkohnen(mf,F)) M <<= 1;
    3813          28 :   mf2 = mfinit_Nkchi(M, r << 1, mfcharpow(CHI, gen_2), space, 0);
    3814          28 :   G = c_shimura(mfsturm(mf2), F, D, CHIP);
    3815          28 :   res = mftobasis_i(mf2, G);
    3816             :   /* not mflinear(mf2,): we want lowest possible level */
    3817          28 :   G = mflinear(MF_get_basis(mf2), res);
    3818          28 :   return gerepilecopy(av, mkvec3(mf2, G, res));
    3819             : }
    3820             : 
    3821             : /* W ZabM (ZM if n = 1), a t_INT or NULL, b t_INT, ZXQ mod P or NULL.
    3822             :  * Write a/b = A/d with d t_INT and A Zab return [W,d,A,P] */
    3823             : static GEN
    3824        7105 : mkMinv(GEN W, GEN a, GEN b, GEN P)
    3825             : {
    3826        7105 :   GEN A = (b && typ(b) == t_POL)? Q_remove_denom(QXQ_inv(b,P), &b): NULL;
    3827        7105 :   if (a && b)
    3828             :   {
    3829        1127 :     a = Qdivii(a,b);
    3830        1127 :     if (typ(a) == t_INT) b = gen_1; else { b = gel(a,2); a = gel(a,1); }
    3831        1127 :     if (is_pm1(a)) a = NULL;
    3832             :   }
    3833        7105 :   if (a) A = A? ZX_Z_mul(A,a): a; else if (!A) A = gen_1;
    3834        7105 :   if (!b) b = gen_1;
    3835        7105 :   if (!P) P = gen_0;
    3836        7105 :   return mkvec4(W,b,A,P);
    3837             : }
    3838             : /* M square invertible QabM, return [M',d], M*M' = d*Id */
    3839             : static GEN
    3840         525 : QabM_Minv(GEN M, GEN P, long n)
    3841             : {
    3842             :   GEN dW, W, dM;
    3843         525 :   M = Q_remove_denom(M, &dM);
    3844         525 :   W = P? ZabM_inv(liftpol_shallow(M), P, n, &dW): ZM_inv(M, &dW);
    3845         525 :   return mkMinv(W, dM, dW, P);
    3846             : }
    3847             : /* Simplified form of mfclean, after a QabM_indexrank: M a ZabM with full
    3848             :  * column rank and z = indexrank(M) is known */
    3849             : static GEN
    3850         826 : mfclean2(GEN M, GEN z, GEN P, long n)
    3851             : {
    3852         826 :   GEN d, Minv, y = gel(z,1), W = rowpermute(M, y);
    3853         826 :   W = P? ZabM_inv(liftpol_shallow(W), P, n, &d): ZM_inv(W, &d);
    3854         826 :   M = rowslice(M, 1, y[lg(y)-1]);
    3855         826 :   Minv = mkMinv(W, NULL, d, P);
    3856         826 :   return mkvec3(y, Minv, M);
    3857             : }
    3858             : /* M QabM, lg(M)>1 and [y,z] its rank profile. Let Minv be the inverse of the
    3859             :  * invertible square matrix in mkMinv format. Return [y,Minv, M[..y[#y],]]
    3860             :  * P cyclotomic polynomial of order n > 2 or NULL */
    3861             : static GEN
    3862        4564 : mfclean(GEN M, GEN P, long n, int ratlift)
    3863             : {
    3864        4564 :   GEN W, v, y, z, d, Minv, dM, MdM = Q_remove_denom(M, &dM);
    3865        4564 :   if (n <= 2)
    3866        3598 :     W = ZM_pseudoinv(MdM, &v, &d);
    3867             :   else
    3868         966 :     W = ZabM_pseudoinv_i(liftpol_shallow(MdM), P, n, &v, &d, ratlift);
    3869        4564 :   y = gel(v,1);
    3870        4564 :   z = gel(v,2);
    3871        4564 :   if (lg(z) != lg(MdM)) M = vecpermute(M,z);
    3872        4564 :   M = rowslice(M, 1, y[lg(y)-1]);
    3873        4564 :   Minv = mkMinv(W, dM, d, P);
    3874        4564 :   return mkvec3(y, Minv, M);
    3875             : }
    3876             : /* call mfclean using only CHI */
    3877             : static GEN
    3878        3696 : mfcleanCHI(GEN M, GEN CHI, int ratlift)
    3879             : {
    3880        3696 :   long n = mfcharorder(CHI);
    3881        3696 :   GEN P = (n <= 2)? NULL: mfcharpol(CHI);
    3882        3696 :   return mfclean(M, P, n, ratlift);
    3883             : }
    3884             : 
    3885             : /* DATA component of a t_MF_NEWTRACE. Was it stripped to save memory ? */
    3886             : static int
    3887       29547 : newtrace_stripped(GEN DATA)
    3888       29547 : { return DATA && (lg(DATA) == 5 && typ(gel(DATA,3)) == t_INT); }
    3889             : /* f a t_MF_NEWTRACE */
    3890             : static GEN
    3891       29547 : newtrace_DATA(long N, GEN f)
    3892             : {
    3893       29547 :   GEN DATA = gel(f,2);
    3894       29547 :   return newtrace_stripped(DATA)? initnewtrace(N, DATA): DATA;
    3895             : }
    3896             : /* reset cachenew for new level incorporating new DATA, tf a t_MF_NEWTRACE
    3897             :  * (+ possibly initialize 'full' for new allowed levels) */
    3898             : static void
    3899       29547 : reset_cachenew(cachenew_t *cache, long N, GEN tf)
    3900             : {
    3901             :   long i, n, l;
    3902       29547 :   GEN v, DATA = newtrace_DATA(N,tf);
    3903       29547 :   cache->DATA = DATA;
    3904       29547 :   if (!DATA) return;
    3905       29512 :   n = cache->n;
    3906       29512 :   v = cache->vfull; l = N+1; /* = lg(DATA) */
    3907     1777524 :   for (i = 1; i < l; i++)
    3908     1748012 :     if (typ(gel(v,i)) == t_INT && lg(gel(DATA,i)) != 1)
    3909       45871 :       gel(v,i) = const_vec(n, NULL);
    3910       29512 :   cache->VCHIP = gel(gel(DATA,N),_VCHIP);
    3911             : }
    3912             : /* initialize a cache of newtrace / cusptrace up to index n and level | N;
    3913             :  * DATA may be NULL (<=> Tr^new = 0). tf a t_MF_NEWTRACE */
    3914             : static void
    3915       10857 : init_cachenew(cachenew_t *cache, long n, long N, GEN tf)
    3916             : {
    3917       10857 :   long i, l = N+1; /* = lg(tf.DATA) when DATA != NULL */
    3918             :   GEN v;
    3919       10857 :   cache->n = n;
    3920       10857 :   cache->vnew = v = cgetg(l, t_VEC);
    3921       10857 :   for (i = 1; i < l; i++) gel(v,i) = (N % i)? gen_0: const_vec(n, NULL);
    3922       10857 :   cache->newHIT = cache->newTOTAL = cache->cuspHIT = cache->cuspTOTAL = 0;
    3923       10857 :   cache->vfull = v = zerovec(N);
    3924       10857 :   reset_cachenew(cache, N, tf);
    3925       10857 : }
    3926             : static void
    3927       15841 : dbg_cachenew(cachenew_t *C)
    3928             : {
    3929       15841 :   if (DEBUGLEVEL >= 2 && C)
    3930           0 :     err_printf("newtrace cache hits: new = %ld/%ld, cusp = %ld/%ld\n",
    3931             :                     C->newHIT, C->newTOTAL, C->cuspHIT, C->cuspTOTAL);
    3932       15841 : }
    3933             : 
    3934             : /* newtrace_{N,k}(d*i), i = n0, ..., n */
    3935             : static GEN
    3936      135310 : colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *cache)
    3937             : {
    3938      135310 :   GEN v = cgetg(n-n0+2, t_COL);
    3939             :   long i;
    3940      135310 :   for (i = n0; i <= n; i++) gel(v, i-n0+1) = mfnewtracecache(N, k, i*d, cache);
    3941      135310 :   return v;
    3942             : }
    3943             : /* T_n(l*m0, l*(m0+1), ..., l*m) F, F = t_MF_NEWTRACE [N,k],DATA, cache
    3944             :  * contains DATA != NULL as well as cached values of F */
    3945             : static GEN
    3946       74494 : heckenewtrace(long m0, long m, long l, long N, long NBIG, long k, long n, cachenew_t *cache)
    3947             : {
    3948       74494 :   long lD, a, k1, nl = n*l;
    3949       74494 :   GEN D, V, v = colnewtrace(m0, m, nl, N, k, cache); /* d=1 */
    3950             :   GEN VCHIP;
    3951       74494 :   if (n == 1) return v;
    3952       48895 :   VCHIP = cache->VCHIP;
    3953       48895 :   D = mydivisorsu(u_ppo(n, NBIG)); lD = lg(D);
    3954       48895 :   k1 = k - 1;
    3955      108962 :   for (a = 2; a < lD; a++)
    3956             :   { /* d > 1, (d,NBIG) = 1 */
    3957       60067 :     long i, j, d = D[a], c = ugcd(l, d), dl = d/c, m0d = ceildiv(m0, dl);
    3958       60067 :     GEN C = vchip_lift(VCHIP, d, powuu(d, k1));
    3959             :     /* m0=0: i = 1 => skip F(0) = 0 */
    3960       60067 :     if (!m0) { i = 1; j = dl; } else { i = 0; j = m0d*dl; }
    3961       60067 :     V = colnewtrace(m0d, m/dl, nl/(d*c), N, k, cache);
    3962             :     /* C = chi(d) d^(k-1) */
    3963      635663 :     for (; j <= m; i++, j += dl)
    3964      575596 :       gel(v,j-m0+1) = gadd(gel(v,j-m0+1), vchip_mod(VCHIP, gmul(C,gel(V,i+1))));
    3965             :   }
    3966       48895 :   return v;
    3967             : }
    3968             : 
    3969             : /* Given v = an[i], return an[d*i] */
    3970             : static GEN
    3971         658 : anextract(GEN v, long n, long d)
    3972             : {
    3973         658 :   GEN w = cgetg(n+2, t_VEC);
    3974             :   long i;
    3975         658 :   for (i = 0; i <= n; i++) gel(w, i+1) = gel(v, i*d+1);
    3976         658 :   return w;
    3977             : }
    3978             : /* T_n(F)(0, l, ..., l*m) */
    3979             : static GEN
    3980         854 : hecke_i(long m, long l, GEN V, GEN F, GEN DATA)
    3981             : {
    3982             :   long k, n, nNBIG, NBIG, lD, M, a, t, nl;
    3983             :   GEN D, v, CHI;
    3984         854 :   if (typ(DATA) == t_VEC)
    3985             :   { /* 1/2-integral k */
    3986          98 :     if (!V) { GEN S = gel(DATA,2); V = mfcoefs_i(F, m*l*S[3], S[4]); }
    3987          98 :     return RgV_heckef2(m, l, V, F, DATA);
    3988             :   }
    3989         756 :   k = mf_get_k(F);
    3990         756 :   n = DATA[1]; nl = n*l;
    3991         756 :   nNBIG = DATA[2];
    3992         756 :   NBIG = DATA[3];
    3993         756 :   if (nNBIG == 1) return V? V: mfcoefs_i(F,m,nl);
    3994         539 :   if (!V && mf_get_type(F) == t_MF_NEWTRACE)
    3995             :   { /* inline F to allow cache, T_n at level NBIG acting on Tr^new(N,k,CHI) */
    3996             :     cachenew_t cache;
    3997         210 :     long N = mf_get_N(F);
    3998         210 :     init_cachenew(&cache, m*nl, N, F);
    3999         210 :     v = heckenewtrace(0, m, l, N, NBIG, k, n, &cache);
    4000         210 :     dbg_cachenew(&cache);
    4001         210 :     settyp(v, t_VEC); return v;
    4002             :   }
    4003         329 :   CHI = mf_get_CHI(F);
    4004         329 :   D = mydivisorsu(nNBIG); lD = lg(D);
    4005         329 :   M = m + 1;
    4006         329 :   t = nNBIG * ugcd(nNBIG, l);
    4007         329 :   if (!V) V = mfcoefs_i(F, m * t, nl / t); /* usually nl = t */
    4008         329 :   v = anextract(V, m, t); /* mfcoefs(F, m, nl); d = 1 */
    4009         658 :   for (a = 2; a < lD; a++)
    4010             :   { /* d > 1, (d, NBIG) = 1 */
    4011         329 :     long d = D[a], c = ugcd(l, d), dl = d/c, i, idl;
    4012         329 :     GEN C = gmul(mfchareval(CHI, d), powuu(d, k-1));
    4013         329 :     GEN w = anextract(V, m/dl, t/(d*c)); /* mfcoefs(F, m/dl, nl/(d*c)) */
    4014        1008 :     for (i = idl = 1; idl <= M; i++, idl += dl)
    4015         679 :       gel(v,idl) = gadd(gel(v,idl), gmul(C, gel(w,i)));
    4016             :   }
    4017         329 :   return v;
    4018             : }
    4019             : 
    4020             : static GEN
    4021       11508 : mkmf(GEN x1, GEN x2, GEN x3, GEN x4, GEN x5)
    4022             : {
    4023       11508 :   GEN MF = obj_init(5, MF_SPLITN);
    4024       11508 :   gel(MF,1) = x1;
    4025       11508 :   gel(MF,2) = x2;
    4026       11508 :   gel(MF,3) = x3;
    4027       11508 :   gel(MF,4) = x4;
    4028       11508 :   gel(MF,5) = x5; return MF;
    4029             : }
    4030             : 
    4031             : /* return an integer b such that p | b => T_p^k Tr^new = 0, for all k > 0 */
    4032             : static long
    4033        7105 : get_badj(long N, long FC)
    4034             : {
    4035        7105 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    4036        7105 :   long i, b = 1, l = lg(P);
    4037       18893 :   for (i = 1; i < l; i++)
    4038       11788 :     if (E[i] > 1 && u_lval(FC, P[i]) < E[i]) b *= P[i];
    4039        7105 :   return b;
    4040             : }
    4041             : /* in place, assume perm strictly increasing */
    4042             : static void
    4043        1162 : vecpermute_inplace(GEN v, GEN perm)
    4044             : {
    4045        1162 :   long i, l = lg(perm);
    4046        1162 :   for (i = 1; i < l; i++) gel(v,i) = gel(v,perm[i]);
    4047        1162 : }
    4048             : 
    4049             : /* Find basis of newspace using closures; assume k >= 2 and !badchar.
    4050             :  * Return NULL if space is empty, else
    4051             :  * [mf1, list of closures T(j)traceform, list of corresponding j, matrix] */
    4052             : static GEN
    4053       14672 : mfnewinit(long N, long k, GEN CHI, cachenew_t *cache, long init)
    4054             : {
    4055             :   GEN S, vj, M, CHIP, mf1, listj, P, tf;
    4056             :   long j, ct, ctlj, dim, jin, SB, sb, two, ord, FC, badj;
    4057             : 
    4058       14672 :   dim = mfnewdim(N, k, CHI);
    4059       14672 :   if (!dim && !init) return NULL;
    4060        7105 :   sb = mfsturmNk(N, k);
    4061        7105 :   CHIP = mfchartoprimitive(CHI, &FC);
    4062             :   /* remove newtrace data from S to save space in output: negligible slowdown */
    4063        7105 :   tf = tag(t_MF_NEWTRACE, mkNK(N,k,CHIP), CHIP);
    4064        7105 :   badj = get_badj(N, FC);
    4065             :   /* try sbsmall first: Sturm bound not sharp for new space */
    4066        7105 :   SB = ceilA1(N, k);
    4067        7105 :   listj = cgetg(2*sb + 3, t_VECSMALL);
    4068      330848 :   for (j = ctlj = 1; ctlj < 2*sb + 3; j++)
    4069      323743 :     if (ugcd(j, badj) == 1) listj[ctlj++] = j;
    4070        7105 :   if (init)
    4071             :   {
    4072        3934 :     init_cachenew(cache, (SB+1)*listj[dim+1], N, tf);
    4073        3934 :     if (init == -1 || !dim) return NULL; /* old space or dim = 0 */
    4074             :   }
    4075             :   else
    4076        3171 :     reset_cachenew(cache, N, tf);
    4077             :   /* cache.DATA is not NULL */
    4078        6678 :   ord = mfcharorder(CHIP);
    4079        6678 :   P = ord <= 2? NULL: mfcharpol(CHIP);
    4080        6678 :   vj = cgetg(dim+1, t_VECSMALL);
    4081        6678 :   M = cgetg(dim+1, t_MAT);
    4082        6685 :   for (two = 1, ct = 0, jin = 1; two <= 2; two++)
    4083             :   {
    4084        6685 :     long a, jlim = jin + sb;
    4085       19334 :     for (a = jin; a <= jlim; a++)
    4086             :     {
    4087             :       GEN z, vecz;
    4088       19327 :       ct++; vj[ct] = listj[a];
    4089       19327 :       gel(M, ct) = heckenewtrace(0, SB, 1, N, N, k, vj[ct], cache);
    4090       19327 :       if (ct < dim) continue;
    4091             : 
    4092        7259 :       z = QabM_indexrank(M, P, ord);
    4093        7259 :       vecz = gel(z, 2); ct = lg(vecz) - 1;
    4094        7259 :       if (ct == dim) { M = mkvec3(z, gen_0, M); break; } /*maximal rank, done*/
    4095         581 :       vecpermute_inplace(M, vecz);
    4096         581 :       vecpermute_inplace(vj, vecz);
    4097             :     }
    4098        6685 :     if (a <= jlim) break;
    4099             :     /* sbsmall was not sufficient, use Sturm bound: must extend M */
    4100          70 :     for (j = 1; j <= ct; j++)
    4101             :     {
    4102          63 :       GEN t = heckenewtrace(SB + 1, sb, 1, N, N, k, vj[j], cache);
    4103          63 :       gel(M,j) = shallowconcat(gel(M, j), t);
    4104             :     }
    4105           7 :     jin = jlim + 1; SB = sb;
    4106             :   }
    4107        6678 :   S = cgetg(dim + 1, t_VEC);
    4108        6678 :   for (j = 1; j <= dim; j++) gel(S, j) = mfhecke_i(vj[j], N, tf);
    4109        6678 :   dbg_cachenew(cache);
    4110        6678 :   mf1 = mkvec4(utoipos(N), utoipos(k), CHI, utoi(mf_NEW));
    4111        6678 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    4112             : }
    4113             : /* k > 1 integral, mf space is mf_CUSP or mf_FULL */
    4114             : static GEN
    4115          42 : mfinittonew(GEN mf)
    4116             : {
    4117          42 :   GEN CHI = MF_get_CHI(mf), S = MF_get_S(mf), vMjd = MFcusp_get_vMjd(mf);
    4118          42 :   GEN M = MF_get_M(mf), vj, mf1;
    4119          42 :   long i, j, l, l0 = lg(S), N0 = MF_get_N(mf);
    4120         203 :   for (i = l0-1; i > 0; i--)
    4121             :   {
    4122         189 :     long N = gel(vMjd,i)[1];
    4123         189 :     if (N != N0) break;
    4124             :   }
    4125          42 :   if (i == l0-1) return NULL;
    4126          35 :   S = vecslice(S, i+1, l0-1); /* forms of conductor N0 */
    4127          35 :   l = lg(S); vj = cgetg(l, t_VECSMALL);
    4128          35 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd,j+i)[2];
    4129          35 :   M = vecslice(M, lg(M)-lg(S)+1, lg(M)-1); /* their coefficients */
    4130          35 :   M = mfcleanCHI(M, CHI, 0);
    4131          35 :   mf1 = mkvec4(utoipos(N0), MF_get_gk(mf), CHI, utoi(mf_NEW));
    4132          35 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    4133             : }
    4134             : 
    4135             : /* Bd(f)[m0..m], v = f[ceil(m0/d)..floor(m/d)], m0d = ceil(m0/d) */
    4136             : static GEN
    4137       68082 : RgC_Bd_expand(long m0, long m, GEN v, long d, long m0d)
    4138             : {
    4139             :   long i, j;
    4140             :   GEN w;
    4141       68082 :   if (d == 1) return v;
    4142       19530 :   w = zerocol(m-m0+1);
    4143       19530 :   if (!m0) { i = 1; j = d; } else { i = 0; j = m0d*d; }
    4144       19530 :   for (; j <= m; i++, j += d) gel(w,j-m0+1) = gel(v,i+1);
    4145       19530 :   return w;
    4146             : }
    4147             : /* S a non-empty vector of t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)); M the matrix
    4148             :  * of their coefficients r*0, r*1, ..., r*m0 (~ mfvectomat) or NULL (empty),
    4149             :  * extend it to coeffs up to m > m0. The forms B_d(T_j(tf_N))in S should be
    4150             :  * sorted by level N, then j, then increasing d. No reordering here. */
    4151             : static GEN
    4152        8113 : bhnmat_extend(GEN M, long m, long r, GEN S, cachenew_t *cache)
    4153             : {
    4154        8113 :   long i, mr, m0, m0r, Nold = 0, jold = 0, l = lg(S);
    4155        8113 :   GEN MAT = cgetg(l, t_MAT), v = NULL;
    4156        8113 :   if (M) { m0 = nbrows(M); m0r = m0 * r; } else m0 = m0r = 0;
    4157        8113 :   mr = m*r;
    4158       76195 :   for (i = 1; i < l; i++)
    4159             :   {
    4160             :     long d, j, md, N;
    4161       68082 :     GEN c, f = bhn_parse(gel(S,i), &d,&j); /* t_MF_NEWTRACE */
    4162       68082 :     N = mf_get_N(f);
    4163       68082 :     md = ceildiv(m0r,d);
    4164       68082 :     if (N != Nold) { reset_cachenew(cache, N, f); Nold = N; jold = 0; }
    4165       68082 :     if (!cache->DATA) { gel(MAT,i) = zerocol(m+1); continue; }
    4166       68082 :     if (j != jold || md)
    4167       54894 :     { v = heckenewtrace(md, mr/d, 1, N, N, mf_get_k(f), j,cache); jold=j; }
    4168       68082 :     c = RgC_Bd_expand(m0r, mr, v, d, md);
    4169       68082 :     if (r > 1) c = c_deflate(m-m0, r, c);
    4170       68082 :     if (M) c = shallowconcat(gel(M,i), c);
    4171       68082 :     gel(MAT,i) = c;
    4172             :   }
    4173        8113 :   return MAT;
    4174             : }
    4175             : 
    4176             : static GEN
    4177        2989 : mfinitcusp(long N, long k, GEN CHI, cachenew_t *cache, long space)
    4178             : {
    4179             :   long L, l, lDN1, FC, N1, d1, i, init;
    4180        2989 :   GEN vS, vMjd, DN1, vmf, CHIP = mfchartoprimitive(CHI, &FC);
    4181             : 
    4182        2989 :   d1 = (space == mf_OLD)? mfolddim_i(N, k, CHIP): mfcuspdim(N, k, CHIP);
    4183        2989 :   if (!d1) return NULL;
    4184        2737 :   N1 = N/FC; DN1 = mydivisorsu(N1); lDN1 = lg(DN1);
    4185        2737 :   init = (space == mf_OLD)? -1: 1;
    4186        2737 :   vmf = cgetg(lDN1, t_VEC);
    4187       16212 :   for (i = lDN1 - 1, l = 1; i; i--)
    4188             :   { /* by decreasing level to allow cache */
    4189       13475 :     GEN mf = mfnewinit(FC*DN1[i], k, CHIP, cache, init);
    4190       13475 :     if (mf) gel(vmf, l++) = mf;
    4191       13475 :     init = 0;
    4192             :   }
    4193        2737 :   setlg(vmf,l); vmf = vecreverse(vmf); /* reorder by increasing level */
    4194             : 
    4195        2737 :   L = mfsturmNk(N, k)+1;
    4196        2737 :   vS = vectrunc_init(L);
    4197        2737 :   vMjd = vectrunc_init(L);
    4198        8589 :   for (i = 1; i < l; i++)
    4199             :   {
    4200        5852 :     GEN DNM, mf = gel(vmf,i), S = MF_get_S(mf), vj = MFnew_get_vj(mf);
    4201        5852 :     long a, lDNM, lS = lg(S), M = MF_get_N(mf);
    4202        5852 :     DNM = mydivisorsu(N / M); lDNM = lg(DNM);
    4203       22547 :     for (a = 1; a < lS; a++)
    4204             :     {
    4205       16695 :       GEN tf = gel(S,a);
    4206       16695 :       long b, j = vj[a];
    4207       41139 :       for (b = 1; b < lDNM; b++)
    4208             :       {
    4209       24444 :         long d = DNM[b];
    4210       24444 :         vectrunc_append(vS, mfbd_i(tf, d));
    4211       24444 :         vectrunc_append(vMjd, mkvecsmall3(M, j, d));
    4212             :       }
    4213             :     }
    4214             :   }
    4215        2737 :   return mkmf(NULL, cgetg(1, t_VEC), vS, vMjd, NULL);
    4216             : }
    4217             : 
    4218             : long
    4219        3493 : mfsturm_mf(GEN mf)
    4220             : {
    4221        3493 :   GEN Mindex = MF_get_Mindex(mf);
    4222        3493 :   long n = lg(Mindex)-1;
    4223        3493 :   return n? Mindex[n]: 0;
    4224             : }
    4225             : 
    4226             : long
    4227         581 : mfsturm(GEN T)
    4228             : {
    4229             :   long N, nk, dk;
    4230         581 :   GEN CHI, mf = checkMF_i(T);
    4231         581 :   if (mf) return mfsturm_mf(mf);
    4232           7 :   checkNK2(T, &N, &nk, &dk, &CHI, 0);
    4233           7 :   return dk == 1 ? mfsturmNk(N, nk) : mfsturmNk(N, (nk + 1) >> 1);
    4234             : }
    4235             : long
    4236           7 : mfisequal(GEN F, GEN G, long lim)
    4237             : {
    4238           7 :   pari_sp av = avma;
    4239             :   long b;
    4240           7 :   if (!checkmf_i(F)) pari_err_TYPE("mfisequal",F);
    4241           7 :   if (!checkmf_i(G)) pari_err_TYPE("mfisequal",G);
    4242           7 :   b = lim? lim: maxss(mfsturmmf(F), mfsturmmf(G));
    4243           7 :   return gc_long(av, gequal(mfcoefs_i(F, b+1, 1), mfcoefs_i(G, b+1, 1)));
    4244             : }
    4245             : 
    4246             : GEN
    4247          35 : mffields(GEN mf)
    4248             : {
    4249          35 :   if (checkmf_i(mf)) return gcopy(mf_get_field(mf));
    4250          35 :   mf = checkMF(mf); return gcopy(MF_get_fields(mf));
    4251             : }
    4252             : 
    4253             : GEN
    4254         322 : mfeigenbasis(GEN mf)
    4255             : {
    4256         322 :   pari_sp ltop = avma;
    4257             :   GEN F, S, v, vP;
    4258             :   long i, l, k, dS;
    4259             : 
    4260         322 :   mf = checkMF(mf);
    4261         322 :   k = MF_get_k(mf);
    4262         322 :   S = MF_get_S(mf); dS = lg(S)-1;
    4263         322 :   if (!dS) return cgetg(1, t_VEC);
    4264         315 :   F = MF_get_newforms(mf);
    4265         315 :   vP = MF_get_fields(mf);
    4266         315 :   if (k == 1)
    4267             :   {
    4268         196 :     if (MF_get_space(mf) == mf_FULL)
    4269             :     {
    4270          14 :       long dE = lg(MF_get_E(mf)) - 1;
    4271          14 :       if (dE) F = rowslice(F, dE+1, dE+dS);
    4272             :     }
    4273         196 :     v = vecmflineardiv_linear(S, F);
    4274         196 :     l = lg(v);
    4275             :   }
    4276             :   else
    4277             :   {
    4278         119 :     GEN (*L)(GEN, GEN) = (MF_get_space(mf) == mf_FULL)? mflinear: mflinear_bhn;
    4279         119 :     l = lg(F); v = cgetg(l, t_VEC);
    4280         119 :     for (i = 1; i < l; i++) gel(v,i) = L(mf, gel(F,i));
    4281             :   }
    4282         315 :   for (i = 1; i < l; i++) mf_setfield(gel(v,i), gel(vP,i));
    4283         315 :   return gerepilecopy(ltop, v);
    4284             : }
    4285             : 
    4286             : /* Minv = [M, d, A], v a t_COL; A a Zab, d a t_INT; return (A/d) * M*v */
    4287             : static GEN
    4288        5859 : Minv_RgC_mul(GEN Minv, GEN v)
    4289             : {
    4290        5859 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4291        5859 :   v = RgM_RgC_mul(M, v);
    4292        5859 :   if (!equali1(A))
    4293             :   {
    4294        1568 :     if (typ(A) == t_POL && degpol(A) > 0) A = mkpolmod(A, gel(Minv,4));
    4295        1568 :     v = RgC_Rg_mul(v, A);
    4296             :   }
    4297        5859 :   if (!equali1(d)) v = RgC_Rg_div(v, d);
    4298        5859 :   return v;
    4299             : }
    4300             : static GEN
    4301        1099 : Minv_RgM_mul(GEN Minv, GEN B)
    4302             : {
    4303        1099 :   long j, l = lg(B);
    4304        1099 :   GEN M = cgetg(l, t_MAT);
    4305        1099 :   for (j = 1; j < l; j++) gel(M,j) = Minv_RgC_mul(Minv, gel(B,j));
    4306        1099 :   return M;
    4307             : }
    4308             : /* B * Minv; allow B = NULL for Id */
    4309             : static GEN
    4310        2170 : RgM_Minv_mul(GEN B, GEN Minv)
    4311             : {
    4312        2170 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4313        2170 :   if (B) M = RgM_mul(B, M);
    4314        2170 :   if (!equali1(A))
    4315             :   {
    4316         882 :     if (typ(A) == t_POL) A = mkpolmod(A, gel(Minv,4));
    4317         882 :     M = RgM_Rg_mul(M, A);
    4318             :   }
    4319        2170 :   if (!equali1(d)) M = RgM_Rg_div(M,d);
    4320        2170 :   return M;
    4321             : }
    4322             : 
    4323             : /* perm vector of strictly increasing indices, v a vector or arbitrary length;
    4324             :  * the last r entries of perm fall beyond v.
    4325             :  * Return v o perm[1..(-r)], discarding the last r entries of v */
    4326             : static GEN
    4327        1106 : vecpermute_partial(GEN v, GEN perm, long *r)
    4328             : {
    4329        1106 :   long i, n = lg(v)-1, l = lg(perm);
    4330             :   GEN w;
    4331        1106 :   if (perm[l-1] <= n) { *r = 0; return vecpermute(v,perm); }
    4332          63 :   for (i = 1; i < l; i++)
    4333          63 :     if (perm[i] > n) break;
    4334          21 :   *r = l - i; l = i;
    4335          21 :   w = cgetg(l, typ(v));
    4336          21 :   for (i = 1; i < l; i++) gel(w,i) = gel(v,perm[i]);
    4337          21 :   return w;
    4338             : }
    4339             : 
    4340             : /* given form F, find coeffs of F on mfbasis(mf). If power series, not
    4341             :  * guaranteed correct if precision less than Sturm bound */
    4342             : static GEN
    4343        1162 : mftobasis_i(GEN mf, GEN F)
    4344             : {
    4345             :   GEN v, Mindex, Minv;
    4346        1162 :   if (!MF_get_dim(mf)) return cgetg(1, t_COL);
    4347        1162 :   Mindex = MF_get_Mindex(mf);
    4348        1162 :   Minv = MF_get_Minv(mf);
    4349        1162 :   if (checkmf_i(F))
    4350             :   {
    4351         252 :     long n = Mindex[lg(Mindex)-1];
    4352         252 :     v = vecpermute(mfcoefs_i(F, n, 1), Mindex);
    4353         252 :     return Minv_RgC_mul(Minv, v);
    4354             :   }
    4355             :   else
    4356             :   {
    4357         910 :     GEN A = gel(Minv,1), d = gel(Minv,2);
    4358             :     long r;
    4359         910 :     v = F;
    4360         910 :     switch(typ(F))
    4361             :     {
    4362           0 :       case t_SER: v = sertocol(v);
    4363         910 :       case t_VEC: case t_COL: break;
    4364           0 :       default: pari_err_TYPE("mftobasis", F);
    4365             :     }
    4366         910 :     if (lg(v) == 1) pari_err_TYPE("mftobasis",v);
    4367         910 :     v = vecpermute_partial(v, Mindex, &r);
    4368         910 :     if (!r) return Minv_RgC_mul(Minv, v); /* single solution */
    4369             :     /* affine space of dimension r */
    4370          21 :     v = RgM_RgC_mul(vecslice(A, 1, lg(v)-1), v);
    4371          21 :     if (!equali1(d)) v = RgC_Rg_div(v,d);
    4372          21 :     return mkvec2(v, vecslice(A, lg(A)-r, lg(A)-1));
    4373             :   }
    4374             : }
    4375             : 
    4376             : static GEN
    4377         560 : const_mat(long n, GEN x)
    4378             : {
    4379         560 :   long j, l = n+1;
    4380         560 :   GEN A = cgetg(l,t_MAT);
    4381         560 :   for (j = 1; j < l; j++) gel(A,j) = const_col(n, x);
    4382         560 :   return A;
    4383             : }
    4384             : 
    4385             : /* L is the mftobasis of a form on CUSP space. We allow mf_FULL or mf_CUSP */
    4386             : static GEN
    4387         280 : mftonew_i(GEN mf, GEN L, long *plevel)
    4388             : {
    4389             :   GEN S, listMjd, CHI, res, Aclos, Acoef, D, perm;
    4390         280 :   long N1, LC, lD, i, l, t, level, N = MF_get_N(mf);
    4391             : 
    4392         280 :   if (MF_get_k(mf) == 1) pari_err_IMPL("mftonew in weight 1");
    4393         280 :   listMjd = MFcusp_get_vMjd(mf);
    4394         280 :   CHI = MF_get_CHI(mf); LC = mfcharconductor(CHI);
    4395         280 :   S = MF_get_S(mf);
    4396             : 
    4397         280 :   N1 = N/LC;
    4398         280 :   D = mydivisorsu(N1); lD = lg(D);
    4399         280 :   perm = cgetg(N1+1, t_VECSMALL);
    4400         280 :   for (i = 1; i < lD; i++) perm[D[i]] = i;
    4401         280 :   Aclos = const_mat(lD-1, cgetg(1,t_VEC));
    4402         280 :   Acoef = const_mat(lD-1, cgetg(1,t_VEC));
    4403         280 :   l = lg(listMjd);
    4404        2877 :   for (i = 1; i < l; i++)
    4405             :   {
    4406             :     long M, d;
    4407             :     GEN v;
    4408        2597 :     if (gequal0(gel(L,i))) continue;
    4409         273 :     v = gel(listMjd, i);
    4410         273 :     M = perm[ v[1]/LC ];
    4411         273 :     d = perm[ v[3] ];
    4412         273 :     gcoeff(Aclos,M,d) = vec_append(gcoeff(Aclos,M,d), gel(S,i));
    4413         273 :     gcoeff(Acoef,M,d) = shallowconcat(gcoeff(Acoef,M,d), gel(L,i));
    4414             :   }
    4415         280 :   res = cgetg(l, t_VEC); level = 1;
    4416        2009 :   for (i = t = 1; i < lD; i++)
    4417             :   {
    4418        1729 :     long j, M = D[i]*LC;
    4419        1729 :     GEN gM = utoipos(M);
    4420       15134 :     for (j = 1; j < lD; j++)
    4421             :     {
    4422       13405 :       GEN f = gcoeff(Aclos,i,j), C, NK;
    4423             :       long d;
    4424       13405 :       if (lg(f) == 1) continue;
    4425         245 :       NK = mf_get_NK(gel(f,1));
    4426         245 :       d = D[j];
    4427         245 :       C = gcoeff(Acoef,i,j);
    4428         245 :       level = ulcm(level, M*d);
    4429         245 :       gel(res,t++) = mkvec3(gM, utoipos(d), mflinear_i(NK,f,C));
    4430             :     }
    4431             :   }
    4432         280 :   if (plevel) *plevel = level;
    4433         280 :   setlg(res, t); return res;
    4434             : }
    4435             : GEN
    4436          35 : mftonew(GEN mf, GEN F)
    4437             : {
    4438          35 :   pari_sp av = avma;
    4439             :   GEN ES;
    4440             :   long s;
    4441          35 :   mf = checkMF(mf);
    4442          35 :   s = MF_get_space(mf);
    4443          35 :   if (s != mf_FULL && s != mf_CUSP)
    4444           7 :     pari_err_TYPE("mftonew [not a full or cuspidal space]", mf);
    4445          28 :   ES = mftobasisES(mf,F);
    4446          21 :   if (!gequal0(gel(ES,1)))
    4447           0 :     pari_err_TYPE("mftonew [not a cuspidal form]", F);
    4448          21 :   F = gel(ES,2);
    4449          21 :   return gerepilecopy(av, mftonew_i(mf,F, NULL));
    4450             : }
    4451             : 
    4452             : static GEN mfeisenstein_i(long k, GEN CHI1, GEN CHI2);
    4453             : 
    4454             : /* mfinit(F * Theta) */
    4455             : static GEN
    4456          77 : mf2init(GEN mf)
    4457             : {
    4458          77 :   GEN CHI = MF_get_CHI(mf), gk = gadd(MF_get_gk(mf), ghalf);
    4459          77 :   long N = MF_get_N(mf);
    4460          77 :   return mfinit_Nkchi(N, itou(gk), mfchiadjust(CHI, gk, N), mf_FULL, 0);
    4461             : }
    4462             : 
    4463             : static long
    4464         497 : mfvec_first_cusp(GEN v)
    4465             : {
    4466         497 :   long i, l = lg(v);
    4467         973 :   for (i = 1; i < l; i++)
    4468             :   {
    4469         889 :     GEN F = gel(v,i);
    4470         889 :     long t = mf_get_type(F);
    4471         889 :     if (t == t_MF_BD) { F = gel(F,2); t = mf_get_type(F); }
    4472         889 :     if (t == t_MF_HECKE) { F = gel(F,3); t = mf_get_type(F); }
    4473         889 :     if (t == t_MF_NEWTRACE) break;
    4474             :   }
    4475         497 :   return i;
    4476             : }
    4477             : /* vF a vector of mf F of type DIV(LINEAR(BAS,L), f) in (lcm) level N,
    4478             :  * F[2]=LINEAR(BAS,L), F[2][2]=BAS=fixed basis (Eisenstein or bhn type),
    4479             :  * F[2][3]=L, F[3]=f; mfvectomat(vF, n) */
    4480             : static GEN
    4481         504 : mflineardivtomat(long N, GEN vF, long n)
    4482             : {
    4483         504 :   GEN F, M, f, fc, ME, dB, B, a0, V = NULL;
    4484         504 :   long lM, lF = lg(vF), j;
    4485             : 
    4486         504 :   if (lF == 1) return cgetg(1,t_MAT);
    4487         497 :   F = gel(vF,1);
    4488         497 :   if (lg(F) == 5)
    4489             :   { /* chicompat */
    4490         238 :     V = gmael(F,4,4);
    4491         238 :     if (typ(V) == t_INT) V = NULL;
    4492             :   }
    4493         497 :   M = gmael(F,2,2); /* BAS */
    4494         497 :   lM = lg(M);
    4495         497 :   j = mfvec_first_cusp(M);
    4496         497 :   if (j == 1) ME = NULL;
    4497             :   else
    4498             :   { /* BAS starts by Eisenstein */
    4499         112 :     ME = mfvectomat(vecslice(M,1,j-1), n, 1);
    4500         112 :     M = vecslice(M, j,lM-1);
    4501             :   }
    4502         497 :   M = bhnmat_extend_nocache(NULL, N, n, 1, M);
    4503         497 :   if (ME) M = shallowconcat(ME,M);
    4504             :   /* M = mfcoefs of BAS */
    4505         497 :   B = cgetg(lF, t_MAT);
    4506         497 :   dB= cgetg(lF, t_VEC);
    4507        2037 :   for (j = 1; j < lF; j++)
    4508             :   {
    4509        1540 :     GEN g = gel(vF, j); /* t_MF_DIV */
    4510        1540 :     gel(B,j) = RgM_RgC_mul(M, gmael(g,2,3));
    4511        1540 :     gel(dB,j)= gmael(g,2,4);
    4512             :   }
    4513         497 :   f = mfcoefsser(gel(F,3),n);
    4514         497 :   a0 = polcoef_i(f, 0, -1);
    4515         497 :   if (gequal0(a0) || gequal1(a0))
    4516         273 :     a0 = NULL;
    4517             :   else
    4518         224 :     f = gdiv(ser_unscale(f, a0), a0);
    4519         497 :   fc = ginv(f);
    4520        2037 :   for (j = 1; j < lF; j++)
    4521             :   {
    4522        1540 :     pari_sp av = avma;
    4523        1540 :     GEN LISer = RgV_to_ser_full(gel(B,j)), f;
    4524        1540 :     if (a0) LISer = gdiv(ser_unscale(LISer, a0), a0);
    4525        1540 :     f = gmul(LISer, fc);
    4526        1540 :     if (a0) f = ser_unscale(f, ginv(a0));
    4527        1540 :     f = sertocol(f); setlg(f, n+2);
    4528        1540 :     if (!gequal1(gel(dB,j))) f = RgC_Rg_div(f, gel(dB,j));
    4529        1540 :     gel(B,j) = gerepileupto(av,f);
    4530             :   }
    4531         497 :   if (V) B = gmodulo(QabM_tracerel(V, 0, B), gel(V,1));
    4532         497 :   return B;
    4533             : }
    4534             : 
    4535             : static GEN
    4536         189 : mfheckemat_mfcoefs(GEN mf, GEN B, GEN DATA)
    4537             : {
    4538         189 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4539         189 :   long j, l = lg(B), sb = mfsturm_mf(mf)-1;
    4540         189 :   GEN b = MF_get_basis(mf), Q = cgetg(l, t_VEC);
    4541         609 :   for (j = 1; j < l; j++)
    4542             :   {
    4543         420 :     GEN v = hecke_i(sb, 1, gel(B,j), gel(b,j), DATA); /* Tn b[j] */
    4544         420 :     settyp(v,t_COL); gel(Q,j) = vecpermute(v, Mindex);
    4545             :   }
    4546         189 :   return Minv_RgM_mul(Minv,Q);
    4547             : }
    4548             : /* T_p^2, p prime, 1/2-integral weight; B = mfcoefs(mf,sb*p^2,1) or (mf,sb,p^2)
    4549             :  * if p|N */
    4550             : static GEN
    4551           7 : mfheckemat_mfcoefs_p2(GEN mf, long p, GEN B)
    4552             : {
    4553           7 :   pari_sp av = avma;
    4554           7 :   GEN DATA = heckef2_data(MF_get_N(mf), p*p);
    4555           7 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, DATA));
    4556             : }
    4557             : /* convert Mindex from row-index to mfcoef indexation: a(n) is stored in
    4558             :  * mfcoefs()[n+1], so subtract 1 from all indices */
    4559             : static GEN
    4560          49 : Mindex_as_coef(GEN mf)
    4561             : {
    4562          49 :   GEN v, Mindex = MF_get_Mindex(mf);
    4563          49 :   long i, l = lg(Mindex);
    4564          49 :   v = cgetg(l, t_VECSMALL);
    4565          49 :   for (i = 1; i < l; i++) v[i] = Mindex[i]-1;
    4566          49 :   return v;
    4567             : }
    4568             : /* T_p, p prime; B = mfcoefs(mf,sb*p,1) or (mf,sb,p) if p|N; integral weight */
    4569             : static GEN
    4570          35 : mfheckemat_mfcoefs_p(GEN mf, long p, GEN B)
    4571             : {
    4572          35 :   pari_sp av = avma;
    4573          35 :   GEN vm, Q, C, Minv = MF_get_Minv(mf);
    4574          35 :   long lm, k, i, j, l = lg(B), N = MF_get_N(mf);
    4575             : 
    4576          35 :   if (N % p == 0) return Minv_RgM_mul(Minv, rowpermute(B, MF_get_Mindex(mf)));
    4577          21 :   k = MF_get_k(mf);
    4578          21 :   C = gmul(mfchareval(MF_get_CHI(mf), p), powuu(p, k-1));
    4579          21 :   vm = Mindex_as_coef(mf); lm = lg(vm);
    4580          21 :   Q = cgetg(l, t_MAT);
    4581          21 :   for (j = 1; j < l; j++) gel(Q,j) = cgetg(lm, t_COL);
    4582         147 :   for (i = 1; i < lm; i++)
    4583             :   {
    4584         126 :     long m = vm[i], mp = m*p;
    4585         126 :     GEN Cm = (m % p) == 0? C : NULL;
    4586        1260 :     for (j = 1; j < l; j++)
    4587             :     {
    4588        1134 :       GEN S = gel(B,j), s = gel(S, mp + 1);
    4589        1134 :       if (Cm) s = gadd(s, gmul(C, gel(S, m/p + 1)));
    4590        1134 :       gcoeff(Q, i, j) = s;
    4591             :     }
    4592             :   }
    4593          21 :   return gerepileupto(av, Minv_RgM_mul(Minv,Q));
    4594             : }
    4595             : /* Matrix of T(p), p prime, dim(mf) > 0 and integral weight */
    4596             : static GEN
    4597         182 : mfheckemat_p(GEN mf, long p)
    4598             : {
    4599         182 :   pari_sp av = avma;
    4600         182 :   long N = MF_get_N(mf), sb = mfsturm_mf(mf)-1;
    4601         182 :   GEN B = (N % p)? mfcoefs_mf(mf, sb * p, 1): mfcoefs_mf(mf, sb, p);
    4602         182 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, hecke_data(N,p)));
    4603             : }
    4604             : 
    4605             : /* mf_NEW != (0), weight > 1, p prime. Use
    4606             :  * T(p) T(j) = T(j*p) + p^{k-1} \chi(p) 1_{p | j, p \nmid N} T(j/p) */
    4607             : static GEN
    4608         875 : mfnewmathecke_p(GEN mf, long p)
    4609             : {
    4610         875 :   pari_sp av = avma;
    4611         875 :   GEN tf, vj = MFnew_get_vj(mf), CHI = MF_get_CHI(mf);
    4612         875 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4613         875 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4614         875 :   long i, j, lvj = lg(vj), lim = vj[lvj-1] * p;
    4615         875 :   GEN M, perm, V, need = zero_zv(lim);
    4616         875 :   GEN C = (N % p)? gmul(mfchareval(CHI,p), powuu(p,k-1)): NULL;
    4617         875 :   tf = mftraceform_new(N, k, CHI);
    4618        3759 :   for (i = 1; i < lvj; i++)
    4619             :   {
    4620        2884 :     j = vj[i]; need[j*p] = 1;
    4621        2884 :     if (N % p && j % p == 0) need[j/p] = 1;
    4622             :   }
    4623         875 :   perm = zero_zv(lim);
    4624         875 :   V = cgetg(lim+1, t_VEC);
    4625       12047 :   for (i = j = 1; i <= lim; i++)
    4626       11172 :     if (need[i]) { gel(V,j) = mfhecke_i(i, N, tf); perm[i] = j; j++; }
    4627         875 :   setlg(V, j);
    4628         875 :   V = bhnmat_extend_nocache(NULL, N, mfsturm_mf(mf)-1, 1, V);
    4629         875 :   V = rowpermute(V, Mindex); /* V[perm[i]] = coeffs(T_i newtrace) */
    4630         875 :   M = cgetg(lvj, t_MAT);
    4631        3759 :   for (i = 1; i < lvj; i++)
    4632             :   {
    4633             :     GEN t;
    4634        2884 :     j = vj[i]; t = gel(V, perm[j*p]);
    4635        2884 :     if (C && j % p == 0) t = RgC_add(t, RgC_Rg_mul(gel(V, perm[j/p]),C));
    4636        2884 :     gel(M,i) = t;
    4637             :   }
    4638         875 :   return gerepileupto(av, Minv_RgM_mul(Minv, M));
    4639             : }
    4640             : 
    4641             : GEN
    4642          77 : mfheckemat(GEN mf, GEN vn)
    4643             : {
    4644          77 :   pari_sp av = avma;
    4645          77 :   long lv, lvP, i, N, dim, nk, dk, p, sb, flint = (typ(vn)==t_INT);
    4646             :   GEN CHI, res, vT, FA, B, vP;
    4647             : 
    4648          77 :   mf = checkMF(mf);
    4649          77 :   if (typ(vn) != t_VECSMALL) vn = gtovecsmall(vn);
    4650          77 :   N = MF_get_N(mf); CHI = MF_get_CHI(mf); Qtoss(MF_get_gk(mf), &nk, &dk);
    4651          77 :   dim = MF_get_dim(mf);
    4652          77 :   lv = lg(vn);
    4653          77 :   res = cgetg(lv, t_VEC);
    4654          77 :   FA = cgetg(lv, t_VEC);
    4655          77 :   vP = cgetg(lv, t_VEC);
    4656          77 :   vT = const_vec(vecsmall_max(vn), NULL);
    4657         182 :   for (i = 1; i < lv; i++)
    4658             :   {
    4659         105 :     ulong n = (ulong)labs(vn[i]);
    4660             :     GEN fa;
    4661         105 :     if (!n) pari_err_TYPE("mfheckemat", vn);
    4662         105 :     if (dk == 1 || uissquareall(n, &n)) fa = myfactoru(n);
    4663           0 :     else { n = 0; fa = myfactoru(1); } /* dummy: T_{vn[i]} = 0 */
    4664         105 :     vn[i] = n;
    4665         105 :     gel(FA,i) = fa;
    4666         105 :     gel(vP,i) = gel(fa,1);
    4667             :   }
    4668          77 :   vP = shallowconcat1(vP); vecsmall_sort(vP);
    4669          77 :   vP = vecsmall_uniq_sorted(vP); /* all primes occurring in vn */
    4670          77 :   lvP = lg(vP); if (lvP == 1) goto END;
    4671          56 :   p = vP[lvP-1];
    4672          56 :   sb = mfsturm_mf(mf)-1;
    4673          56 :   if (dk == 1 && nk != 1 && MF_get_space(mf) == mf_NEW)
    4674          21 :     B = NULL; /* special purpose mfnewmathecke_p is faster */
    4675          35 :   else if (lvP == 2 && N % p == 0)
    4676          21 :     B = mfcoefs_mf(mf, sb, dk==2? p*p: p); /* single prime | N, can optimize */
    4677             :   else
    4678          14 :     B = mfcoefs_mf(mf, sb * (dk==2? p*p: p), 1); /* general initialization */
    4679         126 :   for (i = 1; i < lvP; i++)
    4680             :   {
    4681          70 :     long j, l, q, e = 1;
    4682             :     GEN C, Tp, u1, u0;
    4683          70 :     p = vP[i];
    4684          70 :     for (j = 1; j < lv; j++) e = maxss(e, z_lval(vn[j], p));
    4685          70 :     if (!B)
    4686          28 :       Tp = mfnewmathecke_p(mf, p);
    4687          42 :     else if (dk == 2)
    4688           7 :       Tp = mfheckemat_mfcoefs_p2(mf,p, (lvP==2||N%p)? B: matdeflate(sb,p*p,B));
    4689             :     else
    4690          35 :       Tp = mfheckemat_mfcoefs_p(mf, p, (lvP==2||N%p)? B: matdeflate(sb,p,B));
    4691          70 :     gel(vT, p) = Tp;
    4692          70 :     if (e == 1) continue;
    4693          14 :     u0 = gen_1;
    4694          14 :     if (dk == 2)
    4695             :     {
    4696           0 :       C = N % p? gmul(mfchareval(CHI,p*p), powuu(p, nk-2)): NULL;
    4697           0 :       if (e == 2) u0 = sstoQ(p+1,p); /* special case T_{p^4} */
    4698             :     }
    4699             :     else
    4700          14 :       C = N % p? gmul(mfchareval(CHI,p),   powuu(p, nk-1)): NULL;
    4701          28 :     for (u1=Tp, q=p, l=2; l <= e; l++)
    4702             :     { /* u0 = T_{p^{l-2}}, u1 = T_{p^{l-1}} for l > 2 */
    4703          14 :       GEN v = gmul(Tp, u1);
    4704          14 :       if (C) v = gsub(v, gmul(C, u0));
    4705             :       /* q = p^l, vT[q] = T_q for k integer else T_{q^2} */
    4706          14 :       q *= p; u0 = u1; gel(vT, q) = u1 = v;
    4707             :     }
    4708             :   }
    4709             : END:
    4710             :   /* vT[p^e] = T_{p^e} for all p^e occurring below */
    4711         182 :   for (i = 1; i < lv; i++)
    4712             :   {
    4713         105 :     long n = vn[i], j, lP;
    4714             :     GEN fa, P, E, M;
    4715         105 :     if (n == 0) { gel(res,i) = zeromat(dim,dim); continue; }
    4716         105 :     if (n == 1) { gel(res,i) = matid(dim); continue; }
    4717          77 :     fa = gel(FA,i);
    4718          77 :     P = gel(fa,1); lP = lg(P);
    4719          77 :     E = gel(fa,2); M = gel(vT, upowuu(P[1], E[1]));
    4720          77 :     for (j = 2; j < lP; j++) M = RgM_mul(M, gel(vT, upowuu(P[j], E[j])));
    4721          77 :     gel(res,i) = M;
    4722             :   }
    4723          77 :   if (flint) res = gel(res,1);
    4724          77 :   return gerepilecopy(av, res);
    4725             : }
    4726             : 
    4727             : 
    4728             : /* f = \sum_i v[i] T_listj[i] (Trace Form) attached to v; replace by f/a_1(f) */
    4729             : static GEN
    4730        1323 : mf_normalize(GEN mf, GEN v)
    4731             : {
    4732        1323 :   GEN c, dc = NULL, M = MF_get_M(mf), Mindex = MF_get_Mindex(mf);
    4733        1323 :   v = Q_primpart(v);
    4734        1323 :   c = RgMrow_RgC_mul(M, v, 2); /* a_1(f) */
    4735        1323 :   if (gequal1(c)) return v;
    4736         791 :   if (typ(c) == t_POL) c = gmodulo(c, mfcharpol(MF_get_CHI(mf)));
    4737         791 :   if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1 && degpol(gel(c,1)) >= 40
    4738           7 :                          && Mindex[1] == 2
    4739           7 :                          && mfcharorder(MF_get_CHI(mf)) <= 2)
    4740           7 :   { /* normalize using expansion at infinity (small coefficients) */
    4741           7 :     GEN w, P = gel(c,1), a1 = gel(c,2);
    4742           7 :     long i, l = lg(Mindex);
    4743           7 :     w = cgetg(l, t_COL);
    4744           7 :     gel(w,1) = gen_1;
    4745         280 :     for (i = 2; i < l; i++)
    4746             :     {
    4747         273 :       c = liftpol_shallow(RgMrow_RgC_mul(M, v, Mindex[i]));
    4748         273 :       gel(w,i) = QXQ_div(c, a1, P);
    4749             :     }
    4750             :     /* w = expansion at oo of normalized form */
    4751           7 :     v = Minv_RgC_mul(MF_get_Minv(mf), Q_remove_denom(w, &dc));
    4752           7 :     v = gmodulo(v, P); /* back to mfbasis coefficients */
    4753             :   }
    4754             :   else
    4755             :   {
    4756         784 :     c = ginv(c);
    4757         784 :     if (typ(c) == t_POLMOD) c = Q_remove_denom(c, &dc);
    4758         784 :     v = RgC_Rg_mul(v, c);
    4759             :   }
    4760         791 :   if (dc) v = RgC_Rg_div(v, dc);
    4761         791 :   return v;
    4762             : }
    4763             : static void
    4764         343 : pol_red(GEN NF, GEN *pP, GEN *pa, long flag)
    4765             : {
    4766         343 :   GEN dP, a, P = *pP;
    4767         343 :   long d = degpol(P);
    4768             : 
    4769         343 :   *pa = a = pol_x(varn(P));
    4770         343 :   if (d > 30) return;
    4771             : 
    4772         336 :   dP = RgX_disc(P);
    4773         336 :   if (typ(dP) != t_INT)
    4774          84 :   { dP = gnorm(dP); if (typ(dP) != t_INT) pari_err_BUG("mfnewsplit"); }
    4775         336 :   if (d == 2 || expi(dP) < 62)
    4776             :   {
    4777         308 :     if (expi(dP) < 31)
    4778         308 :       P = NF? rnfpolredabs(NF, P,flag): polredabs0(P,flag);
    4779             :     else
    4780           0 :       P = NF? rnfpolredbest(NF,P,flag): polredbest(P,flag);
    4781         308 :     if (flag)
    4782             :     {
    4783         280 :       a = gel(P,2); if (typ(a) == t_POLMOD) a = gel(a,2);
    4784         280 :       P = gel(P,1);
    4785             :     }
    4786             :   }
    4787         336 :   *pP = P;
    4788         336 :   *pa = a;
    4789             : }
    4790             : 
    4791             : /* Diagonalize and normalize. See mfsplit for meaning of flag. */
    4792             : static GEN
    4793         966 : mfspclean(GEN mf, GEN mf0, GEN NF, long ord, GEN simplesp, long flag)
    4794             : {
    4795         966 :   const long vz = 1;
    4796         966 :   long i, l = lg(simplesp), dim = MF_get_dim(mf);
    4797         966 :   GEN res = cgetg(l, t_MAT), pols = cgetg(l, t_VEC);
    4798         966 :   GEN zeros = (mf == mf0)? NULL: zerocol(dim - MF_get_dim(mf0));
    4799        2317 :   for (i = 1; i < l; i++)
    4800             :   {
    4801        1351 :     GEN ATP = gel(simplesp, i), A = gel(ATP,1), P = gel(ATP,3);
    4802        1351 :     long d = degpol(P);
    4803        1351 :     GEN a, v = (flag && d > flag)? NULL: gel(A,1);
    4804        1351 :     if (d == 1) P = pol_x(vz);
    4805             :     else
    4806             :     {
    4807         343 :       pol_red(NF, &P, &a, !!v);
    4808         343 :       if (v)
    4809             :       { /* Mod(a,P) root of charpoly(T), K*gpowers(a) = eigenvector of T */
    4810         315 :         GEN K, den, M = cgetg(d+1, t_MAT), T = gel(ATP,2);
    4811             :         long j;
    4812         315 :         T = shallowtrans(T);
    4813         315 :         gel(M,1) = vec_ei(d,1); /* basis of cyclic vectors */
    4814         315 :         for (j = 2; j <= d; j++) gel(M,j) = RgM_RgC_mul(T, gel(M,j-1));
    4815         315 :         M = Q_primpart(M);
    4816         427 :         K = NF? ZabM_inv(liftpol_shallow(M), nf_get_pol(NF), ord, &den)
    4817         427 :               : ZM_inv(M,&den);
    4818         315 :         K = shallowtrans(K);
    4819         315 :         v = gequalX(a)? pol_x_powers(d, vz): RgXQ_powers(a, d-1, P);
    4820         315 :         v = gmodulo(RgM_RgC_mul(A, RgM_RgC_mul(K,v)), P);
    4821             :       }
    4822             :     }
    4823        1351 :     if (v)
    4824             :     {
    4825        1323 :       v = mf_normalize(mf0, v); if (zeros) v = shallowconcat(zeros,v);
    4826        1323 :       gel(res,i) = v; if (flag) setlg(res,i+1);
    4827             :     }
    4828             :     else
    4829          28 :       gel(res,i) = zerocol(dim);
    4830        1351 :     gel(pols,i) = P;
    4831             :   }
    4832         966 :   return mkvec2(res, pols);
    4833             : }
    4834             : 
    4835             : /* return v = v_{X-r}(P), and set Z = P / (X-r)^v */
    4836             : static long
    4837          70 : RgX_valrem_root(GEN P, GEN r, GEN *Z)
    4838             : {
    4839             :   long v;
    4840         140 :   for (v = 0; degpol(P); v++)
    4841             :   {
    4842         140 :     GEN t, Q = RgX_div_by_X_x(P, r, &t);
    4843         140 :     if (!gequal0(t)) break;
    4844          70 :     P = Q;
    4845             :   }
    4846          70 :   *Z = P; return v;
    4847             : }
    4848             : static GEN
    4849        1071 : mynffactor(GEN NF, GEN P, long dimlim)
    4850             : {
    4851             :   long i, l, v;
    4852             :   GEN R, E;
    4853        1071 :   if (dimlim != 1)
    4854             :   {
    4855         511 :     R = NF? nffactor(NF, P): QX_factor(P);
    4856         511 :     if (!dimlim) return R;
    4857          21 :     E = gel(R,2);
    4858          21 :     R = gel(R,1); l = lg(R);
    4859          98 :     for (i = 1; i < l; i++)
    4860          91 :       if (degpol(gel(R,i)) > dimlim) break;
    4861          21 :     if (i == 1) return NULL;
    4862          21 :     setlg(E,i);
    4863          21 :     setlg(R,i); return mkmat2(R, E);
    4864             :   }
    4865             :   /* dimlim = 1 */
    4866         560 :   R = nfroots(NF, P); l = lg(R);
    4867         560 :   if (l == 1) return NULL;
    4868         497 :   v = varn(P);
    4869         497 :   settyp(R, t_COL);
    4870         497 :   if (degpol(P) == l-1)
    4871         441 :     E = const_col(l-1, gen_1);
    4872             :   else
    4873             :   {
    4874          56 :     E = cgetg(l, t_COL);
    4875          56 :     for (i = 1; i < l; i++) gel(E,i) = utoi(RgX_valrem_root(P, gel(R,i), &P));
    4876             :   }
    4877         497 :   R = deg1_from_roots(R, v);
    4878         497 :   return mkmat2(R, E);
    4879             : }
    4880             : 
    4881             : /* Let K be a number field attached to NF (Q if NF = NULL). A K-vector
    4882             :  * space of dimension d > 0 is given by a t_MAT A (n x d, full column rank)
    4883             :  * giving a K-basis, X a section (d x n: left pseudo-inverse of A). Return a
    4884             :  * pair (T, fa), where T is an element of the Hecke algebra (a sum of Tp taken
    4885             :  * from vector vTp) acting on A (a d x d t_MAT) and fa is the factorization of
    4886             :  * its characteristic polynomial, limited to factors of degree <= dimlim if
    4887             :  * dimlim != 0 (return NULL if there are no factors of degree <= dimlim) */
    4888             : static GEN
    4889        1064 : findbestsplit(GEN NF, GEN vTp, GEN A, GEN X, long dimlim, long vz)
    4890             : {
    4891        1064 :   GEN T = NULL, Tkeep = NULL, fakeep = NULL;
    4892        1064 :   long lmax = 0, i, lT = lg(vTp);
    4893        2296 :   for (i = 1; i < lT; i++)
    4894             :   {
    4895        1148 :     GEN D, P, E, fa, TpA = gel(vTp,i);
    4896             :     long l;
    4897        2149 :     if (typ(TpA) == t_INT) break;
    4898        1071 :     if (lg(TpA) > lg(A)) TpA = RgM_mul(X, RgM_mul(TpA, A)); /* Tp | A */
    4899        1071 :     T = T ? RgM_add(T, TpA) : TpA;
    4900        1071 :     if (!NF) { P = QM_charpoly_ZX(T); setvarn(P, vz); }
    4901             :     else
    4902             :     {
    4903         203 :       P = charpoly(Q_remove_denom(T, &D), vz);
    4904         203 :       if (D) P = gdiv(RgX_unscale(P, D), powiu(D, degpol(P)));
    4905             :     }
    4906        1071 :     fa = mynffactor(NF, P, dimlim);
    4907        1071 :     if (!fa) return NULL;
    4908        1008 :     E = gel(fa, 2);
    4909             :     /* characteristic polynomial is separable ? */
    4910        1008 :     if (isint1(vecmax(E))) { Tkeep = T; fakeep = fa; break; }
    4911          84 :     l = lg(E);
    4912             :     /* characteristic polynomial has more factors than before ? */
    4913          84 :     if (l > lmax) { lmax = l; Tkeep = T; fakeep = fa; }
    4914             :   }
    4915        1001 :   return mkvec2(Tkeep, fakeep);
    4916             : }
    4917             : 
    4918             : static GEN
    4919         161 : nfcontent(GEN nf, GEN v)
    4920             : {
    4921         161 :   long i, l = lg(v);
    4922         161 :   GEN c = gel(v,1);
    4923         161 :   for (i = 2; i < l; i++) c = idealadd(nf, c, gel(v,i));
    4924         161 :   if (typ(c) == t_MAT && gequal1(gcoeff(c,1,1))) c = gen_1;
    4925         161 :   return c;
    4926             : }
    4927             : static GEN
    4928         252 : nf_primpart(GEN nf, GEN B)
    4929             : {
    4930         252 :   switch(typ(B))
    4931             :   {
    4932             :     case t_COL:
    4933             :     {
    4934         161 :       GEN A = matalgtobasis(nf, B), c = nfcontent(nf, A);
    4935         161 :       if (typ(c) == t_INT) return B;
    4936          21 :       c = idealred_elt(nf,c);
    4937          21 :       A = Q_primpart( nfC_nf_mul(nf, A, Q_primpart(nfinv(nf,c))) );
    4938          21 :       A = liftpol_shallow( matbasistoalg(nf, A) );
    4939          21 :       if (gexpo(A) > gexpo(B)) A = B;
    4940          21 :       return A;
    4941             :     }
    4942             :     case t_MAT:
    4943             :     {
    4944             :       long i, l;
    4945          91 :       GEN A = cgetg_copy(B, &l);
    4946          91 :       for (i = 1; i < l; i++) gel(A,i) = nf_primpart(nf, gel(B,i));
    4947          91 :       return A;
    4948             :     }
    4949             :     default:
    4950           0 :       pari_err_TYPE("nf_primpart", B);
    4951             :       return NULL; /*LCOV_EXCL_LINE*/
    4952             :   }
    4953             : }
    4954             : 
    4955             : /* rotate entries of v to accomodate new entry 'x' (push out oldest entry) */
    4956             : static void
    4957        1029 : vecpush(GEN v, GEN x)
    4958             : {
    4959             :   long i;
    4960        1029 :   for (i = lg(v)-1; i > 1; i--) gel(v,i) = gel(v,i-1);
    4961        1029 :   gel(v,1) = x;
    4962        1029 : }
    4963             : 
    4964             : /* sort t_VEC of vector spaces by increasing dimension */
    4965             : static GEN
    4966         966 : sort_by_dim(GEN v)
    4967             : {
    4968         966 :   long i, l = lg(v);
    4969         966 :   GEN D = cgetg(l, t_VECSMALL);
    4970         966 :   for (i = 1; i < l; i++) D[i] = lg(gmael(v,i,2));
    4971         966 :   return vecpermute(v , vecsmall_indexsort(D));
    4972             : }
    4973             : static GEN
    4974         966 : split_starting_space(GEN mf)
    4975             : {
    4976         966 :   long d = MF_get_dim(mf), d2;
    4977         966 :   GEN id = matid(d);
    4978         966 :   switch(MF_get_space(mf))
    4979             :   {
    4980             :     case mf_NEW:
    4981         959 :     case mf_CUSP: return mkvec2(id, id);
    4982             :   }
    4983           7 :   d2 = lg(MF_get_S(mf))-1;
    4984           7 :   return mkvec2(vecslice(id, d-d2+1,d),
    4985             :                 shallowconcat(zeromat(d2,d-d2),matid(d2)));
    4986             : }
    4987             : /* If dimlim > 0, keep only the dimension <= dimlim eigenspaces.
    4988             :  * See mfsplit for the meaning of flag. */
    4989             : static GEN
    4990        1365 : split_ii(GEN mf, long dimlim, long flag, long *pnewd)
    4991             : {
    4992             :   forprime_t iter;
    4993        1365 :   GEN CHI = MF_get_CHI(mf), empty = cgetg(1, t_VEC), mf0 = mf;
    4994             :   GEN NF, POLCYC, todosp, Tpbigvec, simplesp;
    4995        1365 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4996        1365 :   long ord, FC, NEWT, dimsimple = 0, newd = -1;
    4997        1365 :   const long NBH = 5, vz = 1;
    4998             :   ulong p;
    4999             : 
    5000        1365 :   switch(MF_get_space(mf))
    5001             :   {
    5002        1162 :     case mf_NEW: break;
    5003             :     case mf_CUSP:
    5004             :     case mf_FULL:
    5005         196 :       if (k > 1) { mf0 = mfinittonew(mf); break; }
    5006         175 :       newd = lg(MF_get_S(mf))-1 - mfolddim(N, k, CHI);
    5007         175 :       break;
    5008           7 :     default: pari_err_TYPE("mfsplit [space does not contain newspace]", mf);
    5009             :       return NULL; /*LCOV_EXCL_LINE*/
    5010             :   }
    5011        1358 :   if (newd < 0) newd = mf0? MF_get_dim(mf0): 0;
    5012        1358 :   *pnewd = newd;
    5013        1358 :   if (!newd) return mkvec2(cgetg(1, t_MAT), empty);
    5014             : 
    5015         966 :   NEWT = (k > 1 && MF_get_space(mf0) == mf_NEW);
    5016         966 :   todosp = mkvec( split_starting_space(mf0) );
    5017         966 :   simplesp = empty;
    5018         966 :   FC = mfcharconductor(CHI);
    5019         966 :   ord = mfcharorder(CHI);
    5020         966 :   if (ord <= 2) NF = POLCYC = NULL;
    5021             :   else
    5022             :   {
    5023         161 :     POLCYC = mfcharpol(CHI);
    5024         161 :     NF = nfinit(POLCYC,DEFAULTPREC);
    5025             :   }
    5026         966 :   Tpbigvec = zerovec(NBH);
    5027         966 :   u_forprime_init(&iter, 2, ULONG_MAX);
    5028         966 :   while (dimsimple < newd && (p = u_forprime_next(&iter)))
    5029             :   {
    5030             :     GEN nextsp;
    5031             :     long ind;
    5032        1274 :     if (N % (p*p) == 0 && N/p % FC == 0) continue; /* T_p = 0 in this case */
    5033        1029 :     vecpush(Tpbigvec, NEWT? mfnewmathecke_p(mf0,p): mfheckemat_p(mf0,p));
    5034        1029 :     nextsp = empty;
    5035        2093 :     for (ind = 1; ind < lg(todosp); ind++)
    5036             :     {
    5037        1064 :       GEN tmp = gel(todosp, ind), fa, P, E, D, Tp, DTp;
    5038        1064 :       GEN A = gel(tmp, 1);
    5039        1064 :       GEN X = gel(tmp, 2);
    5040             :       long lP, i;
    5041        1064 :       tmp = findbestsplit(NF, Tpbigvec, A, X, dimlim, vz);
    5042        1785 :       if (!tmp) continue; /* nothing there */
    5043        1001 :       Tp = gel(tmp, 1);
    5044        1001 :       fa = gel(tmp, 2);
    5045        1001 :       P = gel(fa, 1);
    5046        1001 :       E = gel(fa, 2); lP = lg(P);
    5047             :       /* lP > 1 */
    5048        1001 :       if (DEBUGLEVEL) err_printf("Exponents = %Ps\n", E);
    5049        1001 :       if (lP == 2)
    5050             :       {
    5051         707 :         GEN P1 = gel(P,1);
    5052         707 :         long e1 = itos(gel(E,1)), d1 = degpol(P1);
    5053         707 :         if (e1 * d1 == lg(Tp)-1)
    5054             :         {
    5055         658 :           if (e1 > 1) nextsp = vec_append(nextsp, mkvec2(A,X));
    5056             :           else
    5057             :           { /* simple module */
    5058         644 :             simplesp = vec_append(simplesp, mkvec3(A,Tp,P1));
    5059         644 :             dimsimple += d1;
    5060             :           }
    5061         658 :           continue;
    5062             :         }
    5063             :       }
    5064             :       /* Found splitting */
    5065         343 :       DTp = Q_remove_denom(Tp, &D);
    5066        1148 :       for (i = 1; i < lP; i++)
    5067             :       {
    5068         805 :         GEN Ai, Xi, dXi, AAi, v, y, Pi = gel(P,i);
    5069         805 :         Ai = RgX_RgM_eval(D? RgX_rescale(Pi,D): Pi, DTp);
    5070         805 :         Ai = QabM_ker(Ai, POLCYC, ord);
    5071         805 :         if (NF) Ai = nf_primpart(NF, Ai);
    5072             : 
    5073         805 :         AAi = RgM_mul(A, Ai);
    5074             :         /* gives section, works on nonsquare matrices */
    5075         805 :         Xi = QabM_pseudoinv(Ai, POLCYC, ord, &v, &dXi);
    5076         805 :         Xi = RgM_Rg_div(Xi, dXi);
    5077         805 :         y = gel(v,1);
    5078         805 :         if (isint1(gel(E,i)))
    5079             :         {
    5080         707 :           GEN Tpi = RgM_mul(Xi, RgM_mul(rowpermute(Tp,y), Ai));
    5081         707 :           simplesp = vec_append(simplesp, mkvec3(AAi, Tpi, Pi));
    5082         707 :           dimsimple += degpol(Pi);
    5083             :         }
    5084             :         else
    5085             :         {
    5086          98 :           Xi = RgM_mul(Xi, rowpermute(X,y));
    5087          98 :           nextsp = vec_append(nextsp, mkvec2(AAi, Xi));
    5088             :         }
    5089             :       }
    5090             :     }
    5091        1029 :     todosp = nextsp; if (lg(todosp) == 1) break;
    5092             :   }
    5093         966 :   if (DEBUGLEVEL) err_printf("end split, need to clean\n");
    5094         966 :   return mfspclean(mf, mf0, NF, ord, sort_by_dim(simplesp), flag);
    5095             : }
    5096             : static GEN
    5097          28 : dim_filter(GEN v, long dim)
    5098             : {
    5099          28 :   GEN P = gel(v,2);
    5100          28 :   long j, l = lg(P);
    5101         140 :   for (j = 1; j < l; j++)
    5102         126 :     if (degpol(gel(P,j)) > dim)
    5103             :     {
    5104          14 :       v = mkvec2(vecslice(gel(v,1),1,j-1), vecslice(P,1,j-1));
    5105          14 :       break;
    5106             :     }
    5107          28 :   return v;
    5108             : }
    5109             : static long
    5110         203 : dim_sum(GEN v)
    5111             : {
    5112         203 :   GEN P = gel(v,2);
    5113         203 :   long j, l = lg(P), d = 0;
    5114         203 :   for (j = 1; j < l; j++) d += degpol(gel(P,j));
    5115         203 :   return d;
    5116             : }
    5117             : static GEN
    5118        1295 : split_i(GEN mf, long dimlim, long flag)
    5119        1295 : { long junk; return split_ii(mf, dimlim, flag, &junk); }
    5120             : /* mf is either already split or output by mfinit. Splitting is done only for
    5121             :  * newspace except in weight 1. If flag = 0 (default) split completely.
    5122             :  * If flag = d > 0, only give the Galois polynomials in degree > d
    5123             :  * Flag is ignored if dimlim = 1. */
    5124             : GEN
    5125          98 : mfsplit(GEN mf0, long dimlim, long flag)
    5126             : {
    5127          98 :   pari_sp av = avma;
    5128          98 :   GEN v, mf = checkMF_i(mf0);
    5129          98 :   if (!mf) pari_err_TYPE("mfsplit", mf0);
    5130          98 :   if ((v = obj_check(mf, MF_SPLIT)))
    5131          28 :   { if (dimlim) v = dim_filter(v, dimlim); }
    5132          70 :   else if (dimlim && (v = obj_check(mf, MF_SPLITN)))
    5133          21 :   { v = (itos(gel(v,1)) >= dimlim)? dim_filter(gel(v,2), dimlim): NULL; }
    5134          98 :   if (!v)
    5135             :   {
    5136             :     long newd;
    5137          70 :     v = split_ii(mf, dimlim, flag, &newd);
    5138          70 :     if (lg(v) == 1) obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5139          70 :     else if (!flag)
    5140             :     {
    5141          49 :       if (dim_sum(v) == newd) obj_insert(mf, MF_SPLIT,v);
    5142          21 :       else obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5143             :     }
    5144             :   }
    5145          98 :   return gerepilecopy(av, v);
    5146             : }
    5147             : static GEN
    5148         385 : split(GEN mf) { return split_i(mf,0,0); }
    5149             : GEN
    5150         735 : MF_get_newforms(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),1); }
    5151             : GEN
    5152         560 : MF_get_fields(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),2); }
    5153             : 
    5154             : /*************************************************************************/
    5155             : /*                     Modular forms of Weight 1                         */
    5156             : /*************************************************************************/
    5157             : /* S_1(G_0(N)), small N. Return 1 if definitely empty; return 0 if maybe
    5158             :  * non-empty  */
    5159             : static int
    5160       16142 : wt1empty(long N)
    5161             : {
    5162       16142 :   if (N <= 100) switch (N)
    5163             :   { /* non-empty [32/100] */
    5164             :     case 23: case 31: case 39: case 44: case 46:
    5165             :     case 47: case 52: case 55: case 56: case 57:
    5166             :     case 59: case 62: case 63: case 68: case 69:
    5167             :     case 71: case 72: case 76: case 77: case 78:
    5168             :     case 79: case 80: case 83: case 84: case 87:
    5169             :     case 88: case 92: case 93: case 94: case 95:
    5170        5446 :     case 99: case 100: return 0;
    5171        3542 :     default: return 1;
    5172             :   }
    5173        7154 :   if (N <= 600) switch(N)
    5174             :   { /* empty [111/500] */
    5175             :     case 101: case 102: case 105: case 106: case 109:
    5176             :     case 113: case 121: case 122: case 123: case 125:
    5177             :     case 130: case 134: case 137: case 146: case 149:
    5178             :     case 150: case 153: case 157: case 162: case 163:
    5179             :     case 169: case 170: case 173: case 178: case 181:
    5180             :     case 182: case 185: case 187: case 193: case 194:
    5181             :     case 197: case 202: case 205: case 210: case 218:
    5182             :     case 221: case 226: case 233: case 241: case 242:
    5183             :     case 245: case 246: case 250: case 257: case 265:
    5184             :     case 267: case 269: case 274: case 277: case 281:
    5185             :     case 289: case 293: case 298: case 305: case 306:
    5186             :     case 313: case 314: case 317: case 326: case 337:
    5187             :     case 338: case 346: case 349: case 353: case 361:
    5188             :     case 362: case 365: case 369: case 370: case 373:
    5189             :     case 374: case 377: case 386: case 389: case 394:
    5190             :     case 397: case 401: case 409: case 410: case 421:
    5191             :     case 425: case 427: case 433: case 442: case 449:
    5192             :     case 457: case 461: case 466: case 481: case 482:
    5193             :     case 485: case 490: case 493: case 509: case 514:
    5194             :     case 521: case 530: case 533: case 534: case 538:
    5195             :     case 541: case 545: case 554: case 557: case 562:
    5196             :     case 565: case 569: case 577: case 578: case 586:
    5197         336 :     case 593: return 1;
    5198        6804 :     default: return 0;
    5199             :   }
    5200          14 :   return 0;
    5201             : }
    5202             : 
    5203             : static GEN
    5204          28 : initwt1trace(GEN mf)
    5205             : {
    5206          28 :   GEN S = MF_get_S(mf), v, H;
    5207             :   long l, i;
    5208          28 :   if (lg(S) == 1) return mftrivial();
    5209          28 :   H = mfheckemat(mf, Mindex_as_coef(mf));
    5210          28 :   l = lg(H); v = cgetg(l, t_VEC);
    5211          28 :   for (i = 1; i < l; i++) gel(v,i) = gtrace(gel(H,i));
    5212          28 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5213          28 :   return mflineardiv_linear(S, v, 1);
    5214             : }
    5215             : static GEN
    5216          21 : initwt1newtrace(GEN mf)
    5217             : {
    5218          21 :   GEN v, D, S, Mindex, CHI = MF_get_CHI(mf);
    5219          21 :   long FC, lD, i, sb, N1, N2, lM, N = MF_get_N(mf);
    5220          21 :   CHI = mfchartoprimitive(CHI, &FC);
    5221          21 :   if (N % FC || mfcharparity(CHI) == 1) return mftrivial();
    5222          21 :   D = mydivisorsu(N/FC); lD = lg(D);
    5223          21 :   S = MF_get_S(mf);
    5224          21 :   if (lg(S) == 1) return mftrivial();
    5225          21 :   N2 = newd_params2(N);
    5226          21 :   N1 = N / N2;
    5227          21 :   Mindex = MF_get_Mindex(mf);
    5228          21 :   lM = lg(Mindex);
    5229          21 :   sb = Mindex[lM-1];
    5230          21 :   v = zerovec(sb+1);
    5231          42 :   for (i = 1; i < lD; i++)
    5232             :   {
    5233          21 :     long M = FC*D[i], j;
    5234          21 :     GEN tf = initwt1trace(M == N? mf: mfinit_Nkchi(M, 1, CHI, mf_CUSP, 0));
    5235             :     GEN listd, w;
    5236          21 :     if (mf_get_type(tf) == t_MF_CONST) continue;
    5237          21 :     w = mfcoefs_i(tf, sb, 1);
    5238          21 :     if (M == N) { v = gadd(v, w); continue; }
    5239           0 :     listd = mydivisorsu(u_ppo(ugcd(N/M, N1), FC));
    5240           0 :     for (j = 1; j < lg(listd); j++)
    5241             :     {
    5242           0 :       long d = listd[j], d2 = d*d; /* coprime to FC */
    5243           0 :       GEN dk = mfchareval(CHI, d);
    5244           0 :       long NMd = N/(M*d), m;
    5245           0 :       for (m = 1; m <= sb/d2; m++)
    5246             :       {
    5247           0 :         long be = mubeta2(NMd, m);
    5248           0 :         if (be)
    5249             :         {
    5250           0 :           GEN c = gmul(dk, gmulsg(be, gel(w, m+1)));
    5251           0 :           long n = m*d2;
    5252           0 :           gel(v, n+1) = gadd(gel(v, n+1), c);
    5253             :         }
    5254             :       }
    5255             :     }
    5256             :   }
    5257          21 :   if (gequal0(gel(v,2))) return mftrivial();
    5258          21 :   v = vecpermute(v,Mindex);
    5259          21 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5260          21 :   return mflineardiv_linear(S, v, 1);
    5261             : }
    5262             : 
    5263             : /* Matrix of T(p), p \nmid N */
    5264             : static GEN
    5265         196 : Tpmat(long p, long lim, GEN CHI)
    5266             : {
    5267         196 :   GEN M = zeromatcopy(lim, p*lim), chip = mfchareval(CHI, p); /* != 0 */
    5268             :   long i, j, pi, pj;
    5269         196 :   gcoeff(M, 1, 1) = gaddsg(1, chip);
    5270         196 :   for (i = 1, pi = p; i < lim; i++,  pi += p) gcoeff(M, i+1, pi+1) = gen_1;
    5271         196 :   for (j = 1, pj = p; pj < lim; j++, pj += p) gcoeff(M, pj+1, j+1) = chip;
    5272         196 :   return M;
    5273             : }
    5274             : 
    5275             : /* assume !wt1empty(N), in particular N>25 */
    5276             : /* Returns [[lim,p], mf (weight 2), p*lim x dim matrix] */
    5277             : static GEN
    5278        1799 : mfwt1_pre(long N)
    5279             : {
    5280        1799 :   GEN M, mf = mfinit_Nkchi(N, 2, mfchartrivial(), mf_CUSP, 0);
    5281             :   /*not empty for N>25*/
    5282             :   long p, lim;
    5283        1799 :   if (uisprime(N))
    5284             :   {
    5285         392 :     p = 2; /*N>25 is not 2 */
    5286         392 :     lim = ceilA1(N, 3);
    5287             :   }
    5288             :   else
    5289             :   {
    5290             :     forprime_t S;
    5291        1407 :     u_forprime_init(&S, 2, N);
    5292        1407 :     while ((p = u_forprime_next(&S)))
    5293        2527 :       if (N % p) break;
    5294        1407 :     lim = mfsturm_mf(mf) + 1;
    5295             :   }
    5296             :   /* p = smalllest prime not dividing N */
    5297        1799 :   M = bhnmat_extend_nocache(MF_get_M(mf), N, p*lim-1, 1, MF_get_S(mf));
    5298        1799 :   return mkvec3(mkvecsmall2(lim, p), mf, M);
    5299             : }
    5300             : 
    5301             : /* lg(A) > 1, E a t_POL */
    5302             : static GEN
    5303        1134 : mfmatsermul(GEN A, GEN E)
    5304             : {
    5305        1134 :   long j, l = lg(A), r = nbrows(A);
    5306        1134 :   GEN M = cgetg(l, t_MAT);
    5307        1134 :   E = RgXn_red_shallow(E, r+1);
    5308       12509 :   for (j = 1; j < l; j++)
    5309             :   {
    5310       11375 :     GEN c = RgV_to_RgX(gel(A,j), 0);
    5311       11375 :     gel(M, j) = RgX_to_RgC(RgXn_mul(c, E, r+1), r);
    5312             :   }
    5313        1134 :   return M;
    5314             : }
    5315             : /* lg(Ap) > 1, Ep an Flxn */
    5316             : static GEN
    5317         728 : mfmatsermul_Fl(GEN Ap, GEN Ep, ulong p)
    5318             : {
    5319         728 :   long j, l = lg(Ap), r = nbrows(Ap);
    5320         728 :   GEN M = cgetg(l, t_MAT);
    5321        9590 :   for (j = 1; j < l; j++)
    5322             :   {
    5323        8862 :     GEN c = Flv_to_Flx(gel(Ap,j), 0);
    5324        8862 :     gel(M,j) = Flx_to_Flv(Flxn_mul(c, Ep, r+1, p), r);
    5325             :   }
    5326         728 :   return M;
    5327             : }
    5328             : 
    5329             : /* CHI mod F | N, return mfchar of modulus N.
    5330             :  * FIXME: wasteful, G should be precomputed  */
    5331             : static GEN
    5332       16555 : mfcharinduce(GEN CHI, long N)
    5333             : {
    5334             :   GEN G, chi;
    5335       16555 :   if (mfcharmodulus(CHI) == N) return CHI;
    5336        2940 :   G = znstar0(utoipos(N), 1);
    5337        2940 :   chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    5338        2940 :   CHI = leafcopy(CHI);
    5339        2940 :   gel(CHI,1) = G;
    5340        2940 :   gel(CHI,2) = chi; return CHI;
    5341             : }
    5342             : 
    5343             : static GEN
    5344        3983 : gmfcharno(GEN CHI)
    5345             : {
    5346        3983 :   GEN G = gel(CHI,1), chi = gel(CHI,2);
    5347        3983 :   return mkintmod(znconreyexp(G, chi), znstar_get_N(G));
    5348             : }
    5349             : static long
    5350       12831 : mfcharno(GEN CHI)
    5351             : {
    5352       12831 :   GEN n = znconreyexp(gel(CHI,1), gel(CHI,2));
    5353       12831 :   return itou(n);
    5354             : }
    5355             : 
    5356             : /* return k such that minimal mfcharacter in Galois orbit of CHI is CHI^k */
    5357             : static long
    5358       11354 : mfconreyminimize(GEN CHI)
    5359             : {
    5360       11354 :   GEN G = gel(CHI,1), cyc, chi;
    5361       11354 :   cyc = ZV_to_zv(znstar_get_cyc(G));
    5362       11354 :   chi = ZV_to_zv(znconreychar(G, gel(CHI,2)));
    5363       11354 :   return zv_cyc_minimize(cyc, chi, coprimes_zv(mfcharorder(CHI)));
    5364             : }
    5365             : 
    5366             : /* find scalar c such that first non-0 entry of c*v is 1; return c*v
    5367             :  * (set c = NULL for 1) */
    5368             : static GEN
    5369        1701 : RgV_normalize(GEN v, GEN *pc)
    5370             : {
    5371        1701 :   long i, l = lg(v);
    5372        1701 :   *pc = NULL;
    5373        3948 :   for (i = 1; i < l; i++)
    5374             :   {
    5375        3948 :     GEN c = gel(v,i);
    5376        3948 :     if (!gequal0(c))
    5377             :     {
    5378        1701 :       if (gequal1(c)) { *pc = gen_1; return v; }
    5379         595 :       *pc = ginv(c); return RgV_Rg_mul(v, *pc);
    5380             :     }
    5381             :   }
    5382           0 :   return v;
    5383             : }
    5384             : static GEN
    5385        2289 : mftreatdihedral(GEN DIH, GEN POLCYC, long ordchi, long biglim, GEN *pS)
    5386             : {
    5387             :   GEN M, Minv, C;
    5388             :   long l, i;
    5389        2289 :   l = lg(DIH); if (l == 1) return NULL;
    5390        2289 :   if (!pS) return DIH;
    5391         728 :   C = cgetg(l, t_VEC);
    5392         728 :   M = cgetg(l, t_MAT);
    5393        2044 :   for (i = 1; i < l; i++)
    5394             :   {
    5395        1316 :     GEN c, v = mfcoefs_i(gel(DIH,i), biglim, 1);
    5396        1316 :     gel(M,i) = RgV_normalize(v, &c);
    5397        1316 :     gel(C,i) = Rg_col_ei(c, l-1, i);
    5398             :   }
    5399         728 :   Minv = gel(mfclean(M,POLCYC,ordchi,0),2);
    5400         728 :   M = RgM_Minv_mul(M, Minv);
    5401         728 :   C = RgM_Minv_mul(C, Minv);
    5402         728 :   *pS = vecmflinear(DIH, C);
    5403         728 :   return M;
    5404             : }
    5405             : 
    5406             : static GEN
    5407         189 : mfstabiter(GEN M, GEN A2, GEN E1inv, long lim, GEN P, long ordchi)
    5408             : {
    5409             :   GEN A, VC, con;
    5410         189 :   E1inv = primitive_part(E1inv, &con);
    5411         189 :   VC = con? ginv(con): gen_1;
    5412         189 :   A = mfmatsermul(A2, E1inv);
    5413             :   while(1)
    5414         105 :   {
    5415         294 :     GEN R = shallowconcat(RgM_mul(M,A), rowslice(A,1,lim));
    5416         294 :     GEN B = QabM_ker(R, P, ordchi);
    5417         294 :     long lA = lg(A), lB = lg(B);
    5418         294 :     if (lB == 1) return NULL;
    5419         294 :     if (lB == lA) return mkvec2(A, VC);
    5420         105 :     B = rowslice(B, 1, lA-1);
    5421         105 :     if (ordchi > 2) B = gmodulo(B, P);
    5422         105 :     A = Q_primitive_part(RgM_mul(A,B), &con);
    5423         105 :     VC = gmul(VC,B); /* first VC is a scalar, then a RgM */
    5424         105 :     if (con) VC = RgM_Rg_div(VC, con);
    5425             :   }
    5426             : }
    5427             : static long
    5428         189 : mfstabitermodp(GEN Mp, GEN Ap, long p, long lim)
    5429             : {
    5430         189 :   GEN VC = NULL;
    5431             :   while (1)
    5432          21 :   {
    5433         210 :     GEN Rp = shallowconcat(Flm_mul(Mp,Ap,p), rowslice(Ap,1,lim));
    5434         210 :     GEN Bp = Flm_ker(Rp, p);
    5435         210 :     long lA = lg(Ap), lB = lg(Bp);
    5436         210 :     if (lB == 1) return 0;
    5437         210 :     if (lB == lA) return lA-1;
    5438          21 :     Bp = rowslice(Bp, 1, lA-1);
    5439          21 :     Ap = Flm_mul(Ap, Bp, p);
    5440          21 :     VC = VC? Flm_mul(VC, Bp, p): Bp;
    5441             :   }
    5442             : }
    5443             : 
    5444             : static GEN
    5445         350 : mfintereis(GEN A, GEN M2, GEN y, GEN den, GEN E2, GEN P, long ordchi)
    5446             : {
    5447         350 :   GEN z, M1 = mfmatsermul(A,E2), M1den = isint1(den)? M1: RgM_Rg_mul(M1,den);
    5448         350 :   M2 = RgM_mul(M2, rowpermute(M1, y));
    5449         350 :   z = QabM_ker(RgM_sub(M2,M1den), P, ordchi);
    5450         350 :   if (ordchi > 2) z = gmodulo(z, P);
    5451         350 :   return mkvec2(RgM_mul(A,z), z);
    5452             : }
    5453             : static GEN
    5454         357 : mfintereismodp(GEN A, GEN M2, GEN E2, ulong p)
    5455             : {
    5456         357 :   GEN M1 = mfmatsermul_Fl(A, E2, p), z;
    5457         357 :   long j, lx = lg(A);
    5458         357 :   z = Flm_ker(shallowconcat(M1, M2), p);
    5459         357 :   for (j = lg(z) - 1; j; j--) setlg(z[j], lx);
    5460         357 :   return mkvec2(Flm_mul(A,z,p), z);
    5461             : }
    5462             : 
    5463             : static GEN
    5464         196 : mfcharinv_i(GEN CHI)
    5465             : {
    5466         196 :   GEN G = gel(CHI,1);
    5467         196 :   CHI = leafcopy(CHI); gel(CHI,2) =  zncharconj(G, gel(CHI,2)); return CHI;
    5468             : }
    5469             : 
    5470             : /* upper bound dim S_1(Gamma_0(N),chi) performing the linear algebra mod p */
    5471             : static long
    5472         196 : mfwt1dimmodp(GEN A, GEN ES, GEN M, long ordchi, long dih, long lim)
    5473             : {
    5474             :   GEN Ap, ApF, ES1p, VC;
    5475         196 :   ulong p, r = QabM_init(ordchi, &p);
    5476             : 
    5477         196 :   ApF = Ap = QabM_to_Flm(A, r, p);
    5478         196 :   VC = NULL;
    5479         196 :   ES1p = QabX_to_Flx(gel(ES,1), r, p);
    5480         196 :   if (lg(ES) >= 3)
    5481             :   {
    5482         182 :     GEN M2 = mfmatsermul_Fl(ApF, ES1p, p);
    5483         182 :     pari_sp av = avma;
    5484             :     long i;
    5485         532 :     for (i = 2; i < lg(ES); i++)
    5486             :     {
    5487         357 :       GEN ESip = QabX_to_Flx(gel(ES,i), r, p);
    5488         357 :       GEN C, ApC = mfintereismodp(Ap, M2, ESip, p);
    5489         357 :       Ap = gel(ApC,1);
    5490         357 :       if (lg(Ap)-1 == dih) return dih;
    5491         350 :       C = gel(ApC,2); VC = VC? Flm_mul(VC, C, p): C;
    5492         350 :       gerepileall(av, 2, &Ap,&VC);
    5493             :     }
    5494             :   }
    5495             :   /* intersection of Eisenstein series quotients non empty: use Schaeffer */
    5496         189 :   Ap = mfmatsermul_Fl(Ap, Flxn_inv(ES1p,nbrows(Ap),p), p);
    5497         189 :   return mfstabitermodp(QabM_to_Flm(M,r,p), Ap, p, lim);
    5498             : }
    5499             : 
    5500             : /* Compute the full S_1(\G_0(N),\chi). If pS is NULL, only the dimension
    5501             :  * dim, in the form of a vector having dim components. Otherwise output
    5502             :  * a basis: ptvf contains a pointer to the vector of forms, and the
    5503             :  * program returns the corresponding matrix of Fourier expansions.
    5504             :  * ptdimdih gives the dimension of the subspace generated by dihedral forms;
    5505             :  * TMP is from mfwt1_pre or NULL. */
    5506             : static GEN
    5507       10815 : mfwt1basis(long N, GEN CHI, GEN TMP, GEN *pS, long *ptdimdih)
    5508             : {
    5509             :   GEN ES, mf, A, M, Tp, tmp1, tmp2, den;
    5510             :   GEN S, ESA, VC, C, POLCYC, ES1, ES1INV, DIH, a0, a0i;
    5511             :   long plim, lim, biglim, i, p, dA, dimp, ordchi, dih;
    5512             : 
    5513       10815 :   if (ptdimdih) *ptdimdih = 0;
    5514       10815 :   if (pS) *pS = NULL;
    5515       10815 :   if (wt1empty(N) || mfcharparity(CHI) != -1) return NULL;
    5516       10528 :   ordchi = mfcharorder(CHI);
    5517       10528 :   if (uisprime(N) && ordchi > 4) return NULL;
    5518       10500 :   if (!pS)
    5519             :   {
    5520        7042 :     dih = mfdihedralcuspdim(N, CHI);
    5521        7042 :     DIH = zerovec(dih);
    5522             :   }
    5523             :   else
    5524             :   {
    5525        3458 :     DIH = mfdihedralcusp(N, CHI);
    5526        3458 :     dih = lg(DIH) - 1;
    5527             :   }
    5528       10500 :   POLCYC = (ordchi <= 2)? NULL: mfcharpol(CHI);
    5529       10500 :   if (ptdimdih) *ptdimdih = dih;
    5530       10500 :   biglim = mfsturmNk(N, 2);
    5531       10500 :   if (N <= 600) switch(N)
    5532             :   {
    5533             :     long m;
    5534             :     case 219: case 273: case 283: case 331: case 333: case 344: case 416:
    5535             :     case 438: case 468: case 491: case 504: case 546: case 553: case 563:
    5536             :     case 566: case 581: case 592:
    5537          14 :       break; /* one chi with both exotic and dihedral forms */
    5538             :     default: /* only dihedral forms */
    5539        9436 :       if (!dih) return NULL;
    5540             :       /* fall through */
    5541             :     case 124: case 133: case 148: case 171: case 201: case 209: case 224:
    5542             :     case 229: case 248: case 261: case 266: case 288: case 296: case 301:
    5543             :     case 309: case 325: case 342: case 371: case 372: case 380: case 399:
    5544             :     case 402: case 403: case 404: case 408: case 418: case 432: case 444:
    5545             :     case 448: case 451: case 453: case 458: case 496: case 497: case 513:
    5546             :     case 522: case 527: case 532: case 576: case 579:
    5547             :       /* no chi with both exotic and dihedral; one chi with exotic forms */
    5548        3192 :       if (dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5549         910 :       CHI = mfcharinduce(CHI,N);
    5550         910 :       m = mfcharno(CHI);
    5551         910 :       if (N == 124 && (m != 67 && m != 87)) return NULL;
    5552         784 :       if (N == 133 && (m != 83 && m !=125)) return NULL;
    5553         490 :       if (N == 148 && (m !=105 && m !=117)) return NULL;
    5554         364 :       if (N == 171 && (m != 94 && m !=151)) return NULL;
    5555         364 :       if (N == 201 && (m != 29 && m !=104)) return NULL;
    5556         364 :       if (N == 209 && (m != 87 && m !=197)) return NULL;
    5557         364 :       if (N == 224 && (m != 95 && m !=191)) return NULL;
    5558         364 :       if (N == 229 && (m !=107 && m !=122)) return NULL;
    5559         364 :       if (N == 248 && (m != 87 && m !=191)) return NULL;
    5560         273 :       if (N == 261 && (m != 46 && m !=244)) return NULL;
    5561         273 :       if (N == 266 && (m != 83 && m !=125)) return NULL;
    5562         273 :       if (N == 288 && (m != 31 && m !=223)) return NULL;
    5563         273 :       if (N == 296 && (m !=105 && m !=265)) return NULL;
    5564             :   }
    5565         196 :   if (!TMP) TMP = mfwt1_pre(N);
    5566         196 :   tmp1= gel(TMP,1); lim = tmp1[1]; p = tmp1[2]; plim = p*lim;
    5567         196 :   mf  = gel(TMP,2);
    5568         196 :   A   = gel(TMP,3); /* p*lim x dim matrix */
    5569         196 :   S = MF_get_S(mf);
    5570         196 :   ESA = mfeisensteinbasis(N, 1, mfcharinv_i(CHI));
    5571         196 :   ES = RgM_to_RgXV(mfvectomat(ESA, plim+1, 1), 0);
    5572         196 :   ES1 = gel(ES,1); /* does not vanish at oo */
    5573         196 :   Tp = Tpmat(p, lim, CHI);
    5574         196 :   dimp = mfwt1dimmodp(A, ES, Tp, ordchi, dih, lim);
    5575         196 :   if (!dimp) return NULL;
    5576         196 :   if (dimp == dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5577         189 :   VC = gen_1;
    5578         189 :   if (lg(ES) >= 3)
    5579             :   {
    5580             :     pari_sp btop;
    5581         175 :     long lim2 = (3*lim)/2 + 1;
    5582         175 :     GEN Ash = rowslice(A, 1, lim2), M2 = mfmatsermul(Ash, ES1);
    5583             :     GEN v, y, M2M2I, M2I;
    5584         175 :     M2I = QabM_pseudoinv(M2, POLCYC, ordchi, &v, &den);
    5585         175 :     y = gel(v,1);
    5586         175 :     M2M2I = RgM_mul(M2,M2I);
    5587         175 :     btop = avma;
    5588         525 :     for (i = 2; i < lg(ES); i++)
    5589             :     {
    5590         350 :       GEN APC = mfintereis(Ash, M2M2I, y, den, gel(ES,i), POLCYC,ordchi);
    5591         350 :       Ash = gel(APC,1); if (lg(Ash) == 1) return NULL;
    5592         350 :       VC = gmul(VC, gel(APC,2));
    5593         350 :       if (gc_needed(btop, 1))
    5594             :       {
    5595           0 :         if (DEBUGMEM > 1) pari_warn(warnmem,"mfwt1basis i = %ld", i);
    5596           0 :         gerepileall(btop, 2, &Ash, &VC);
    5597             :       }
    5598             :     }
    5599         175 :     A = RgM_mul(A, vecslice(VC,1, lg(Ash)-1));
    5600             :   }
    5601         189 :   a0 = gel(ES1,2); /* non-zero */
    5602         189 :   if (gequal1(a0)) a0 = a0i = NULL;
    5603             :   else
    5604             :   {
    5605         189 :     a0i = ginv(a0);
    5606         189 :     ES1 = RgX_Rg_mul(RgX_unscale(ES1,a0), a0i);
    5607             :   }
    5608         189 :   ES1INV = RgXn_inv(ES1, plim-1);
    5609         189 :   if (a0) ES1INV = RgX_Rg_mul(RgX_unscale(ES1INV, a0i), a0i);
    5610         189 :   tmp2 = mfstabiter(Tp, A, ES1INV, lim, POLCYC, ordchi);
    5611         189 :   if (!tmp2) return NULL;
    5612         189 :   A = gel(tmp2,1); dA = lg(A);
    5613         189 :   VC = gmul(VC, gel(tmp2,2));
    5614         189 :   C = cgetg(dA, t_VEC);
    5615         189 :   M = cgetg(dA, t_MAT);
    5616         574 :   for (i = 1; i < dA; i++)
    5617             :   {
    5618         385 :     GEN c, v = gel(A,i);
    5619         385 :     gel(M,i) = RgV_normalize(v, &c);
    5620         385 :     gel(C,i) = RgC_Rg_mul(gel(VC,i), c);
    5621             :   }
    5622         189 :   if (pS)
    5623             :   {
    5624         140 :     GEN Minv = gel(mfclean(M, POLCYC, ordchi, 0), 2);
    5625         140 :     M = RgM_Minv_mul(M, Minv);
    5626         140 :     C = RgM_Minv_mul(C, Minv);
    5627         140 :     *pS = vecmflineardiv0(S, C, gel(ESA,1));
    5628             :   }
    5629         189 :   return M;
    5630             : }
    5631             : 
    5632             : static void
    5633         322 : MF_set_space(GEN mf, long x) { gmael(mf,1,4) = utoi(x); }
    5634             : static GEN
    5635         168 : mfwt1_cusptonew(GEN mf)
    5636             : {
    5637         168 :   const long vy = 1;
    5638         168 :   GEN vP, F, S, Snew, vF, v = split(mf);
    5639             :   long i, lP, dSnew, ct;
    5640             : 
    5641         168 :   F = gel(v,1);
    5642         168 :   vP= gel(v,2); lP = lg(vP);
    5643         168 :   if (lP == 1) { obj_insert(mf, MF_SPLIT, v); return NULL; }
    5644         154 :   MF_set_space(mf, mf_NEW);
    5645         154 :   S = MF_get_S(mf);
    5646         154 :   dSnew = dim_sum(v);
    5647         154 :   Snew = cgetg(dSnew + 1, t_VEC); ct = 0;
    5648         154 :   vF = cgetg(lP, t_MAT);
    5649         329 :   for (i = 1; i < lP; i++)
    5650             :   {
    5651         175 :     GEN V, P = gel(vP,i), f = liftpol_shallow(gel(F,i));
    5652         175 :     long j, d = degpol(P);
    5653         175 :     gel(vF,i) = V = zerocol(dSnew);
    5654         175 :     if (d == 1)
    5655             :     {
    5656          84 :       gel(Snew, ct+1) = mflineardiv_linear(S, f, 0);
    5657          84 :       gel(V, ct+1) = gen_1;
    5658             :     }
    5659             :     else
    5660             :     {
    5661          91 :       f = RgXV_to_RgM(f,d);
    5662         280 :       for (j = 1; j <= d; j++)
    5663             :       {
    5664         189 :         gel(Snew, ct+j) = mflineardiv_linear(S, row(f,j), 0);
    5665         189 :         gel(V, ct+j) = mkpolmod(pol_xn(j-1,vy), P);
    5666             :       }
    5667             :     }
    5668         175 :     ct += d;
    5669             :   }
    5670         154 :   obj_insert(mf, MF_SPLIT, mkvec2(vF, vP));
    5671         154 :   gel(mf,3) = Snew; return mf;
    5672             : }
    5673             : static GEN
    5674        3570 : mfwt1init(long N, GEN CHI, GEN TMP, long space, long flraw)
    5675             : {
    5676        3570 :   GEN mf, mf1, S, M = mfwt1basis(N, CHI, TMP, &S, NULL);
    5677        3570 :   if (!M) return NULL;
    5678         868 :   mf1 = mkvec4(stoi(N), gen_1, CHI, utoi(mf_CUSP));
    5679         868 :   mf = mkmf(mf1, cgetg(1,t_VEC), S, gen_0, NULL);
    5680         868 :   if (space == mf_NEW)
    5681             :   {
    5682         168 :     gel(mf,5) = mfcleanCHI(M,CHI, 0);
    5683         168 :     mf = mfwt1_cusptonew(mf); if (!mf) return NULL;
    5684         154 :     if (!flraw) M = mfcoefs_mf(mf, mfsturmNk(N,1)+1, 1);
    5685             :   }
    5686         854 :   gel(mf,5) = flraw? zerovec(3): mfcleanCHI(M, CHI, 0);
    5687         854 :   return mf;
    5688             : }
    5689             : 
    5690             : static GEN
    5691         973 : mfEMPTY(GEN mf1)
    5692             : {
    5693         973 :   GEN Minv = mkMinv(cgetg(1,t_MAT), NULL,NULL,NULL);
    5694         973 :   GEN M = mkvec3(cgetg(1,t_VECSMALL), Minv, cgetg(1,t_MAT));
    5695         973 :   return mkmf(mf1, cgetg(1,t_VEC), cgetg(1,t_VEC), cgetg(1,t_VEC), M);
    5696             : }
    5697             : static GEN
    5698         616 : mfEMPTYall(long N, GEN gk, GEN vCHI, long space)
    5699             : {
    5700             :   long i, l;
    5701             :   GEN v, gN, gs;
    5702         616 :   if (!vCHI) return cgetg(1, t_VEC);
    5703          14 :   gN = utoipos(N); gs = utoi(space);
    5704          14 :   l = lg(vCHI); v = cgetg(l, t_VEC);
    5705          14 :   for (i = 1; i < l; i++) gel(v,i) = mfEMPTY(mkvec4(gN,gk,gel(vCHI,i),gs));
    5706          14 :   return v;
    5707             : }
    5708             : 
    5709             : static GEN
    5710        3983 : fmt_dim(GEN CHI, long d, long dih)
    5711        3983 : { return mkvec4(gmfcharorder(CHI), gmfcharno(CHI), utoi(d), stoi(dih)); }
    5712             : /* merge two vector of fmt_dim's for the same vector of characters. If CHI
    5713             :  * is not NULL, remove dim-0 spaces and add character from CHI */
    5714             : static GEN
    5715           7 : merge_dims(GEN V, GEN W, GEN CHI)
    5716             : {
    5717           7 :   long i, j, id, l = lg(V);
    5718           7 :   GEN A = cgetg(l, t_VEC);
    5719           7 :   if (l == 1) return A;
    5720           7 :   id = CHI? 1: 3;
    5721          21 :   for (i = j = 1; i < l; i++)
    5722             :   {
    5723          14 :     GEN v = gel(V,i), w = gel(W,i);
    5724          14 :     long dv = itou(gel(v,id)), dvh = itou(gel(v,id+1)), d;
    5725          14 :     long dw = itou(gel(w,id)), dwh = itou(gel(w,id+1));
    5726          14 :     d = dv + dw;
    5727          14 :     if (d || CHI)
    5728          42 :       gel(A,j++) = CHI? fmt_dim(gel(CHI,i),d, dvh+dwh)
    5729          28 :                       : mkvec2s(d,dvh+dwh);
    5730             :   }
    5731           7 :   setlg(A, j); return A;
    5732             : }
    5733             : static GEN
    5734        3010 : mfdim0all(GEN w)
    5735             : {
    5736        3010 :   if (w) retconst_vec(lg(w)-1, zerovec(2));
    5737        3003 :   return cgetg(1,t_VEC);
    5738             : }
    5739             : static long
    5740        7245 : mfwt1cuspdim_i(long N, GEN CHI, GEN TMP, long *dih)
    5741             : {
    5742        7245 :   pari_sp av = avma;
    5743        7245 :   GEN b = mfwt1basis(N, CHI, TMP, NULL, dih);
    5744        7245 :   return gc_long(av, b? lg(b)-1: 0);
    5745             : }
    5746             : static long
    5747         406 : mfwt1cuspdim(long N, GEN CHI) { return mfwt1cuspdim_i(N, CHI, NULL, NULL); }
    5748             : static GEN
    5749        4144 : mfwt1cuspdimall(long N, GEN vCHI)
    5750             : {
    5751             :   GEN z, TMP, w;
    5752             :   long i, j, l;
    5753        4144 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5754        1141 :   w = mfwt1chars(N,vCHI);
    5755        1141 :   l = lg(w); if (l == 1) return cgetg(1,t_VEC);
    5756        1141 :   z = cgetg(l, t_VEC);
    5757        1141 :   TMP = mfwt1_pre(N);
    5758        7861 :   for (i = j = 1; i < l; i++)
    5759             :   {
    5760        6720 :     GEN CHI = gel(w,i);
    5761        6720 :     long dih, d = mfwt1cuspdim_i(N, CHI, TMP, &dih);
    5762        6720 :     if (vCHI)
    5763          42 :       gel(z,j++) = mkvec2s(d, dih);
    5764        6678 :     else if (d)
    5765        1428 :       gel(z,j++) = fmt_dim(CHI, d, dih);
    5766             :   }
    5767        1141 :   setlg(z,j); return z;
    5768             : }
    5769             : 
    5770             : /* dimension of S_1(Gamma_1(N)) */
    5771             : static long
    5772        4123 : mfwt1cuspdimsum(long N)
    5773             : {
    5774        4123 :   pari_sp av = avma;
    5775        4123 :   GEN v = mfwt1cuspdimall(N, NULL);
    5776        4123 :   long i, ct = 0, l = lg(v);
    5777        5544 :   for (i = 1; i < l; i++)
    5778             :   {
    5779        1421 :     GEN w = gel(v,i); /* [ord(CHI),*,dim,*] */
    5780        1421 :     ct += itou(gel(w,3))*myeulerphiu(itou(gel(w,1)));
    5781             :   }
    5782        4123 :   return gc_long(av,ct);
    5783             : }
    5784             : 
    5785             : static GEN
    5786          56 : mfwt1newdimall(long N, GEN vCHI)
    5787             : {
    5788             :   GEN z, w, vTMP, fa, P, E;
    5789             :   long i, c, l, lw, P1;
    5790          56 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5791          56 :   w = mfwt1chars(N,vCHI);
    5792          56 :   lw = lg(w); if (lw == 1) return cgetg(1,t_VEC);
    5793          56 :   vTMP = const_vec(N, NULL);
    5794          56 :   gel(vTMP,N) = mfwt1_pre(N);
    5795             :   /* if p || N and p \nmid F(CHI), S_1^new(G0(N),chi) = 0 */
    5796          56 :   fa = znstar_get_faN(gmael(w,1,1));
    5797          56 :   P = gel(fa,1); l = lg(P);
    5798          56 :   E = gel(fa,2);
    5799         154 :   for (i = P1 = 1; i < l; i++)
    5800          98 :     if (E[i] == 1) P1 *= itou(gel(P,i));
    5801             :   /* P1 = \prod_{v_p(N) = 1} p */
    5802          56 :   z = cgetg(lw, t_VEC);
    5803         182 :   for (i = c = 1; i < lw; i++)
    5804             :   {
    5805             :     long S, j, l, F, dihnew;
    5806         126 :     GEN D, CHI = gel(w,i), CHIP = mfchartoprimitive(CHI,&F);
    5807             : 
    5808         126 :     S = F % P1? 0: mfwt1cuspdim_i(N, CHI, gel(vTMP,N), &dihnew);
    5809         126 :     if (!S)
    5810             :     {
    5811          56 :       if (vCHI) gel(z, c++) = zerovec(2);
    5812          56 :       continue;
    5813             :     }
    5814          70 :     D = mydivisorsu(N/F); l = lg(D);
    5815          77 :     for (j = l-2; j > 0; j--) /* skip last M = N */
    5816             :     {
    5817           7 :       long M = D[j]*F, m, s, dih;
    5818           7 :       GEN TMP = gel(vTMP,M);
    5819           7 :       if (wt1empty(M) || !(m = mubeta(D[l-j]))) continue; /*m = mubeta(N/M)*/
    5820           7 :       if (!TMP) gel(vTMP,M) = TMP = mfwt1_pre(M);
    5821           7 :       s = mfwt1cuspdim_i(M, CHIP, TMP, &dih);
    5822           7 :       if (s) { S += m * s; dihnew += m * dih; }
    5823             :     }
    5824          70 :     if (vCHI)
    5825          63 :       gel(z,c++) = mkvec2s(S, dihnew);
    5826           7 :     else if (S)
    5827           7 :       gel(z, c++) = fmt_dim(CHI, S, dihnew);
    5828             :   }
    5829          56 :   setlg(z,c); return z;
    5830             : }
    5831             : 
    5832             : static GEN
    5833          28 : mfwt1olddimall(long N, GEN vCHI)
    5834             : {
    5835             :   long i, j, l;
    5836             :   GEN z, w;
    5837          28 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5838          28 :   w = mfwt1chars(N,vCHI);
    5839          28 :   l = lg(w); z = cgetg(l, t_VEC);
    5840          84 :   for (i = j = 1; i < l; i++)
    5841             :   {
    5842          56 :     GEN CHI = gel(w,i);
    5843          56 :     long d = mfolddim(N, 1, CHI);
    5844          56 :     if (vCHI)
    5845          28 :       gel(z,j++) = mkvec2s(d,d?-1:0);
    5846          28 :     else if (d)
    5847           7 :       gel(z, j++) = fmt_dim(CHI, d, -1);
    5848             :   }
    5849          28 :   setlg(z,j); return z;
    5850             : }
    5851             : 
    5852             : static long
    5853         469 : mfwt1olddimsum(long N)
    5854             : {
    5855             :   GEN D;
    5856         469 :   long N2, i, l, S = 0;
    5857         469 :   newd_params(N, &N2); /* will ensure mubeta != 0 */
    5858         469 :   D = mydivisorsu(N/N2); l = lg(D);
    5859        2485 :   for (i = 2; i < l; i++)
    5860             :   {
    5861        2016 :     long M = D[l-i]*N2, d = mfwt1cuspdimsum(M);
    5862        2016 :     if (d) S -= mubeta(D[i]) * d;
    5863             :   }
    5864         469 :   return S;
    5865             : }
    5866             : static long
    5867        1050 : mfwt1newdimsum(long N)
    5868             : {
    5869        1050 :   long S = mfwt1cuspdimsum(N);
    5870        1050 :   return S? S - mfwt1olddimsum(N): 0;
    5871             : }
    5872             : 
    5873             : static long
    5874         210 : mfisdihedral(GEN vF, GEN DIH)
    5875             : {
    5876         210 :   GEN vG = gel(DIH,1), M = gel(DIH,2), v, G, bnr, w, gen, cyc, D, f, nf, con;
    5877             :   GEN f0, f0b, xin;
    5878             :   long i, l, e, j, L, n;
    5879         210 :   if (lg(M) == 1) return 0;
    5880          28 :   v = RgM_RgC_invimage(M, vF);
    5881          28 :   if (!v) return 0;
    5882          28 :   l = lg(v);
    5883          28 :   for (i = 1; i < l; i++)
    5884          28 :     if (!gequal0(gel(v,i))) break;
    5885          28 :   if (i == l) return 0;
    5886          28 :   G = gel(vG,i);
    5887          28 :   bnr = gel(G,2); cyc = bnr_get_cyc(bnr); D = gel(cyc,1);
    5888          28 :   w = gel(G,3);
    5889          28 :   f = bnr_get_mod(bnr);
    5890          28 :   nf = bnr_get_nf(bnr);
    5891          28 :   con = gel(galoisconj(nf,gen_1), 2);
    5892          28 :   f0 = gel(f,1); f0b = galoisapply(nf, con, f0);
    5893          28 :   xin = zv_to_ZV(gel(w,2)); /* xi(bnr.gen[i]) = e(xin[i] / D) */
    5894          28 :   if (!gequal(f0,f0b))
    5895             :   { /* finite part of conductor not ambiguous */
    5896          14 :     GEN a = idealmul(nf, f0, idealdivexact(nf, f0b, idealadd(nf, f0, f0b)));
    5897          14 :     GEN bnr0 = bnr;
    5898          14 :     bnr = Buchray(bnr_get_bnf(bnr), mkvec2(a, gel(f,2)), nf_INIT | nf_GEN);
    5899          14 :     xin = RgV_RgM_mul(xin, bnrsurjection(bnr, bnr0));
    5900             :     /* still xi(gen[i]) = e(xin[i] / D), for the new generators */
    5901             :   }
    5902          28 :   gen = bnr_get_gen(bnr); L = lg(gen);
    5903          42 :   for (j = 1, e = itou(D); j < L; j++)
    5904             :   {
    5905          35 :     GEN Ng = idealnorm(nf, gel(gen,j));
    5906          35 :     GEN a = shifti(gel(xin,j), 1); /* xi(g_j^2) = e(a/D) */
    5907          35 :     GEN b = FpV_dotproduct(xin, isprincipalray(bnr,Ng), D);
    5908          35 :     GEN m = Fp_sub(a, b, D); /* xi(g_j/\bar{g_j}) = e(m/D) */
    5909          35 :     e = ugcd(e, itou(m)); if (e == 1) break;
    5910             :   }
    5911          28 :   n = itou(D) / e;
    5912          28 :   return n == 1? 4: 2*n;
    5913             : }
    5914             : 
    5915             : static ulong
    5916         119 : myradicalu(ulong n) { return zv_prod(gel(myfactoru(n),1)); }
    5917             : 
    5918             : /* list of fundamental discriminants unramified outside N, with sign s
    5919             :  * [s = 0 => no sign condition] */
    5920             : static GEN
    5921         119 : mfunram(long N, long s)
    5922             : {
    5923         119 :   long cN = myradicalu(N >> vals(N)), p = 1, m = 1, l, c, i;
    5924         119 :   GEN D = mydivisorsu(cN), res;
    5925         119 :   l = lg(D);
    5926         119 :   if (s == 1) m = 0; else if (s == -1) p = 0;
    5927         119 :   res = cgetg(6*l - 5, t_VECSMALL);
    5928         119 :   c = 1;
    5929         119 :   if (!odd(N))
    5930             :   { /* d = 1 */
    5931          56 :     if (p) res[c++] = 8;
    5932          56 :     if (m) { res[c++] =-8; res[c++] =-4; }
    5933             :   }
    5934         364 :   for (i = 2; i < l; i++)
    5935             :   { /* skip d = 1, done above */
    5936         245 :     long d = D[i], d4 = d & 3L; /* d odd, squarefree, d4 = 1 or 3 */
    5937         245 :     if (d4 == 1) { if (p) res[c++] = d; }
    5938         182 :     else         { if (m) res[c++] =-d; }
    5939         245 :     if (!odd(N))
    5940             :     {
    5941          56 :       if (p) { res[c++] = 8*d; if (d4 == 3) res[c++] = 4*d; }
    5942          56 :       if (m) { res[c++] =-8*d; if (d4 == 1) res[c++] =-4*d; }
    5943             :     }
    5944             :   }
    5945         119 :   setlg(res, c); return res;
    5946             : }
    5947             : 
    5948             : /* Return 1 if F is definitely not S4 type; return 0 on failure. */
    5949             : static long
    5950         105 : mfisnotS4(long N, GEN w)
    5951             : {
    5952         105 :   GEN D = mfunram(N, 0);
    5953         105 :   long i, lD = lg(D), lw = lg(w);
    5954         616 :   for (i = 1; i < lD; i++)
    5955             :   {
    5956         511 :     long p, d = D[i], ok = 0;
    5957        1442 :     for (p = 2; p < lw; p++)
    5958        1442 :       if (w[p] && kross(d,p) == -1) { ok = 1; break; }
    5959         511 :     if (!ok) return 0;
    5960             :   }
    5961         105 :   return 1;
    5962             : }
    5963             : 
    5964             : /* Return 1 if Q(sqrt(5)) \not\subset Q(F), i.e. F is definitely not A5 type;
    5965             :  * return 0 on failure. */
    5966             : static long
    5967         105 : mfisnotA5(GEN F)
    5968             : {
    5969         105 :   GEN CHI = mf_get_CHI(F), P = mfcharpol(CHI), T, Q;
    5970             : 
    5971         105 :   if (mfcharorder(CHI) % 5 == 0) return 0;
    5972         105 :   T = mf_get_field(F); if (degpol(T) == 1) return 1;
    5973         105 :   if (degpol(P) > 1) T = rnfequation(P,T);
    5974         105 :   Q = gsubgs(pol_xn(2,varn(T)), 5);
    5975         105 :   return (typ(nfisincl(Q, T)) == t_INT);
    5976             : }
    5977             : 
    5978             : /* v[p+1]^2 / chi(p) - 2 = z + 1/z with z primitive root of unity of order n,
    5979             :  * return n */
    5980             : static long
    5981        6741 : mffindrootof1(GEN v, long p, GEN CHI)
    5982             : {
    5983        6741 :   GEN ap = gel(v,p+1), u0, u1, u1k, u2;
    5984        6741 :   long c = 1;
    5985        6741 :   if (gequal0(ap)) return 2;
    5986        5033 :   u0 = gen_2; u1k = u1 = gsubgs(gdiv(gsqr(ap), mfchareval(CHI, p)), 2);
    5987       19845 :   while (!gequalsg(2, liftpol_shallow(u1))) /* u1 = z^c + z^-c */
    5988             :   {
    5989        9779 :     u2 = gsub(gmul(u1k, u1), u0);
    5990        9779 :     u0 = u1; u1 = u2; c++;
    5991             :   }
    5992        5033 :   return c;
    5993             : }
    5994             : 
    5995             : /* we known that F is not dihedral */
    5996             : static long
    5997         182 : mfgaloistype_i(long N, GEN CHI, GEN F, GEN v)
    5998             : {
    5999             :   forprime_t iter;
    6000         182 :   long lim = lg(v)-2;
    6001         182 :   GEN w = zero_zv(lim);
    6002             :   pari_sp av;
    6003             :   ulong p;
    6004         182 :   u_forprime_init(&iter, 2, lim);
    6005         182 :   av = avma;
    6006        5474 :   while((p = u_forprime_next(&iter))) if (N%p) switch(mffindrootof1(v, p, CHI))
    6007             :   {
    6008        1400 :     case 1: case 2: continue;
    6009        3451 :     case 3: w[p] = 1; break;
    6010          70 :     case 4: return -24; /* S4 */
    6011           0 :     case 5: return -60; /* A5 */
    6012           7 :     default: pari_err_DOMAIN("mfgaloistype", "form", "not a",
    6013             :                              strtoGENstr("cuspidal eigenform"), F);
    6014           0 :     set_avma(av);
    6015             :   }
    6016         105 :   if (mfisnotS4(N,w) && mfisnotA5(F)) return -12; /* A4 */
    6017           0 :   return 0; /* FAILURE */
    6018             : }
    6019             : 
    6020             : static GEN
    6021         210 : mfgaloistype0(long N, GEN CHI, GEN F, GEN DIH, long lim)
    6022             : {
    6023         210 :   pari_sp av = avma;
    6024         210 :   GEN vF = mftocol(F, lim, 1);
    6025         210 :   long t = mfisdihedral(vF, DIH);
    6026         210 :   if (t) { set_avma(av); return stoi(t); }
    6027             :   for(;;)
    6028             :   {
    6029           0 :     t = mfgaloistype_i(N, CHI, F, vF);
    6030         175 :     set_avma(av); if (t) return stoi(t);
    6031           0 :     lim += lim >> 1; vF = mfcoefs_i(F,lim,1);
    6032             :   }
    6033             : }
    6034             : 
    6035             : /* If f is NULL, give all the galoistypes, otherwise just for f */
    6036             : GEN
    6037         217 : mfgaloistype(GEN NK, GEN f)
    6038             : {
    6039         217 :   pari_sp av = avma;
    6040         217 :   GEN CHI, T, F, DIH, mf = checkMF_i(NK);
    6041             :   long N, k, lL, i, lim, SB;
    6042             : 
    6043         217 :   if (f && !checkmf_i(f)) pari_err_TYPE("mfgaloistype", f);
    6044         210 :   if (mf)
    6045             :   {
    6046         175 :     N = MF_get_N(mf);
    6047         175 :     k = MF_get_k(mf);
    6048         175 :     CHI = MF_get_CHI(mf);
    6049             :   }
    6050             :   else
    6051             :   {
    6052          35 :     checkNK(NK, &N, &k, &CHI, 0);
    6053          35 :     mf = f? NULL: mfinit_i(NK, mf_NEW);
    6054             :   }
    6055         210 :   if (k != 1) pari_err_DOMAIN("mfgaloistype", "k", "!=", gen_1, stoi(k));
    6056         210 :   SB = mf? mfsturm_mf(mf): mfsturmNk(N,1);
    6057         210 :   DIH = mfdihedralnew(N,CHI);
    6058         210 :   lim = lg(DIH) == 1? 200: SB;
    6059         210 :   DIH = mkvec2(DIH, mfvectomat(DIH,SB,1));
    6060         210 :   if (f) return gerepileuptoint(av, mfgaloistype0(N,CHI, f, DIH, lim));
    6061         112 :   F = mfeigenbasis(mf); lL = lg(F);
    6062         112 :   T = cgetg(lL, t_VEC);
    6063         112 :   for (i=1; i < lL; i++) gel(T,i) = mfgaloistype0(N, CHI, gel(F,i), DIH, lim);
    6064         112 :   return gerepileupto(av, T);
    6065             : }
    6066             : 
    6067             : /******************************************************************/
    6068             : /*                   Find all dihedral forms.                     */
    6069             : /******************************************************************/
    6070             : /* lim >= 2 */
    6071             : static void
    6072          14 : consttabdihedral(long lim)
    6073          14 : { cache_set(cache_DIH, mfdihedralall(mkvecsmall2(1,lim))); }
    6074             : 
    6075             : /* a ideal coprime to bnr modulus */
    6076             : static long
    6077       78099 : mfdiheval(GEN bnr, GEN w, GEN a)
    6078             : {
    6079       78099 :   GEN L, cycn = gel(w,1), chin = gel(w,2);
    6080       78099 :   long ordmax = cycn[1];
    6081       78099 :   L = ZV_to_Flv(isprincipalray(bnr,a), ordmax);
    6082       78099 :   return Flv_dotproduct(chin, L, ordmax);
    6083             : }
    6084             : 
    6085             : /* A(x^k) mod T */
    6086             : static GEN
    6087       28049 : Galois(GEN A, long k, GEN T)
    6088             : {
    6089       28049 :   if (typ(A) != t_POL) return A;
    6090       10283 :   return gmod(RgX_inflate(A, k), T);
    6091             : }
    6092             : static GEN
    6093         791 : vecGalois(GEN v, long k, GEN T)
    6094             : {
    6095             :   long i, l;
    6096         791 :   GEN w = cgetg_copy(v,&l);
    6097         791 :   for (i = 1; i < l; i++) gel(w,i) = Galois(gel(v,i), k, T);
    6098         791 :   return w;
    6099             : }
    6100             : 
    6101             : static GEN
    6102      154567 : fix_pol(GEN S, GEN Pn, int *trace)
    6103             : {
    6104      154567 :   if (typ(S) != t_POL) return S;
    6105      108185 :   S = RgX_rem(S, Pn);
    6106      108185 :   if (typ(S) == t_POL)
    6107             :   {
    6108      108185 :     switch(lg(S))
    6109             :     {
    6110       37765 :       case 2: return gen_0;
    6111       17080 :       case 3: return gel(S,2);
    6112             :     }
    6113       53340 :     *trace = 1;
    6114             :   }
    6115       53340 :   return S;
    6116             : }
    6117             : 
    6118             : static GEN
    6119       10913 : dihan(GEN bnr, GEN w, GEN k0j, ulong lim)
    6120             : {
    6121       10913 :   GEN nf = bnr_get_nf(bnr), f = bid_get_ideal(bnr_get_bid(bnr));
    6122       10913 :   GEN v = zerovec(lim+1), cycn = gel(w,1), Tinit = gel(w,3);
    6123       10913 :   GEN Pn = gel(Tinit,lg(Tinit)==4? 2: 1);
    6124       10913 :   long j, ordmax = cycn[1], k0 = k0j[1], jdeg = k0j[2];
    6125       10913 :   long D = itos(nf_get_disc(nf)), vt = varn(Pn);
    6126       10913 :   int trace = 0;
    6127             :   ulong p, n;
    6128             :   forprime_t T;
    6129             : 
    6130       10913 :   if (!lim) return v;
    6131       10703 :   gel(v,2) = gen_1;
    6132       10703 :   u_forprime_init(&T, 2, lim);
    6133             :   /* fill in prime powers first */
    6134       10703 :   while ((p = u_forprime_next(&T)))
    6135             :   {
    6136             :     GEN vP, vchiP, S;
    6137             :     long k, lP;
    6138             :     ulong q, qk;
    6139       71183 :     if (kross(D,p) >= 0) q = p;
    6140       29092 :     else if (!(q = umuluu_le(p,p,lim))) continue;
    6141             :     /* q = Norm P */
    6142       47705 :     vP = idealprimedec(nf, utoipos(p));
    6143       47705 :     lP = lg(vP);
    6144       47705 :     vchiP = cgetg(lP, t_VECSMALL);
    6145      129675 :     for (j = k = 1; j < lP; j++)
    6146             :     {
    6147       81970 :       GEN P = gel(vP,j);
    6148       81970 :       if (!idealval(nf, f, P)) vchiP[k++] = mfdiheval(bnr,w,P);
    6149             :     }
    6150       47705 :     if (k == 1) continue;
    6151       46018 :     setlg(vchiP, k); lP = k;
    6152       46018 :     if (lP == 2)
    6153             :     { /* one prime above p not dividing f */
    6154       13937 :       long s, s0 = vchiP[1];
    6155       24668 :       for (qk=q, s = s0;; s = Fl_add(s,s0,ordmax))
    6156             :       {
    6157       35399 :         S = Qab_zeta(s, ordmax, vt);
    6158       24668 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6159       24668 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6160             :       }
    6161             :     }
    6162             :     else /* two primes above p not dividing f */
    6163             :     {
    6164       32081 :       long s, s0 = vchiP[1], s1 = vchiP[2];
    6165       47446 :       for (qk=q, k = 1;; k++)
    6166       15365 :       { /* sum over a,b s.t. Norm( P1^a P2^b ) = q^k, i.e. a+b = k */
    6167             :         long a;
    6168       47446 :         GEN S = gen_0;
    6169      165354 :         for (a = 0; a <= k; a++)
    6170             :         {
    6171      117908 :           s = Fl_add(Fl_mul(a, s0, ordmax), Fl_mul(k-a, s1, ordmax), ordmax);
    6172      117908 :           S = gadd(S, Qab_zeta(s, ordmax, vt));
    6173             :         }
    6174       47446 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6175       47446 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6176             :       }
    6177             :     }
    6178             :   }
    6179             :   /* complete with non-prime powers */
    6180      200319 :   for (n = 2; n <= lim; n++)
    6181             :   {
    6182      189616 :     GEN S, fa = myfactoru(n), P = gel(fa, 1), E = gel(fa, 2);
    6183             :     long q;
    6184      189616 :     if (lg(P) == 2) continue;
    6185             :     /* not a prime power */
    6186       82453 :     q = upowuu(P[1],E[1]);
    6187       82453 :     S = gmul(gel(v, q + 1), gel(v, n/q + 1));
    6188       82453 :     gel(v, n+1) = fix_pol(S, Pn, &trace);
    6189             :   }
    6190       10703 :   if (trace)
    6191             :   {
    6192        5572 :     v = QabV_tracerel(Tinit, jdeg, v);
    6193             :     /* Apply Galois Mod(k0, ordw) */
    6194        5572 :     if (k0 > 1) { GEN Pm = gel(Tinit,1); v = vecGalois(v, k0, Pm); }
    6195             :   }
    6196       10703 :   return v;
    6197             : }
    6198             : 
    6199             : /* as cyc_normalize for t_VECSMALL cyc */
    6200             : static GEN
    6201       26782 : cyc_normalize_zv(GEN cyc)
    6202             : {
    6203       26782 :   long i, o = cyc[1], l = lg(cyc); /* > 1 */
    6204       26782 :   GEN D = cgetg(l, t_VECSMALL);
    6205       26782 :   D[1] = o; for (i = 2; i < l; i++) D[i] = o / cyc[i];
    6206       26782 :   return D;
    6207             : }
    6208             : /* as char_normalize for t_VECSMALLs */
    6209             : static GEN
    6210      117950 : char_normalize_zv(GEN chi, GEN ncyc)
    6211             : {
    6212      117950 :   long i, l = lg(chi);
    6213      117950 :   GEN c = cgetg(l, t_VECSMALL);
    6214      117950 :   if (l > 1) {
    6215      117950 :     c[1] = chi[1];
    6216      117950 :     for (i = 2; i < l; i++) c[i] = chi[i] * ncyc[i];
    6217             :   }
    6218      117950 :   return c;
    6219             : }
    6220             : 
    6221             : static GEN
    6222        8946 : dihan_bnf(long D)
    6223        8946 : { setrand(gen_1); return Buchall(quadpoly(stoi(D)), 0, LOWDEFAULTPREC); }
    6224             : static GEN
    6225       37233 : dihan_bnr(GEN bnf, GEN A)
    6226       37233 : { setrand(gen_1); return Buchray(bnf, A, nf_INIT|nf_GEN); }
    6227             : 
    6228             : /* Hecke xi * (D/.) = Dirichlet chi, return v in Q^r st chi(g_i) = e(v[i]).
    6229             :  * cycn = cyc_normalize_zv(bnr.cyc), chin = char_normalize_zv(chi,cyc) */
    6230             : static GEN
    6231       34412 : bnrchartwist2conrey(GEN chin, GEN cycn, GEN bnrconreyN, GEN kroconreyN)
    6232             : {
    6233       34412 :   long l = lg(bnrconreyN), c1 = cycn[1], i;
    6234       34412 :   GEN v = cgetg(l, t_COL);
    6235      125132 :   for (i = 1; i < l; i++)
    6236             :   {
    6237       90720 :     GEN d = sstoQ(zv_dotproduct(chin, gel(bnrconreyN,i)), c1);
    6238       90720 :     if (kroconreyN[i] < 0) d = gadd(d, ghalf);
    6239       90720 :     gel(v,i) = d;
    6240             :   }
    6241       34412 :   return v;
    6242             : }
    6243             : 
    6244             : /* chi(g_i) = e(v[i]) denormalize wrt Conrey generators orders */
    6245             : static GEN
    6246       34412 : conreydenormalize(GEN znN, GEN v)
    6247             : {
    6248       34412 :   GEN gcyc = znstar_get_conreycyc(znN), w;
    6249       34412 :   long l = lg(v), i;
    6250       34412 :   w = cgetg(l, t_COL);
    6251      125132 :   for (i = 1; i < l; i++)
    6252       90720 :     gel(w,i) = modii(gmul(gel(v,i), gel(gcyc,i)), gel(gcyc,i));
    6253       34412 :   return w;
    6254             : }
    6255             : 
    6256             : static long
    6257       83538 : Miyake(GEN vchi, GEN gb, GEN cycn)
    6258             : {
    6259       83538 :   long i, e = cycn[1], lb = lg(gb);
    6260       83538 :   GEN v = char_normalize_zv(vchi, cycn);
    6261      124264 :   for (i = 1; i < lb; i++)
    6262       99666 :     if ((zv_dotproduct(v, gel(gb,i)) -  v[i]) % e) return 1;
    6263       24598 :   return 0;
    6264             : }
    6265             : 
    6266             : /* list of Hecke characters not induced by a Dirichlet character up to Galois
    6267             :  * conjugation, whose conductor is bnr.cond; cycn = cyc_normalize(bnr.cyc)*/
    6268             : static GEN
    6269       26782 : mklvchi(GEN bnr, GEN cycn, GEN gb)
    6270             : {
    6271       26782 :   GEN cyc = bnr_get_cyc(bnr), cycsmall = ZV_to_zv(cyc);
    6272       26782 :   GEN vchi = cyc2elts(cycsmall);
    6273       26782 :   long ordmax = cycsmall[1], c, i, l;
    6274       26782 :   l = lg(vchi);
    6275      303450 :   for (i = c = 1; i < l; i++)
    6276             :   {
    6277      276668 :     GEN chi = gel(vchi,i);
    6278      276668 :     if (!gb || Miyake(chi, gb, cycn)) gel(vchi, c++) = Flv_to_ZV(chi);
    6279             :   }
    6280       26782 :   setlg(vchi, c); l = c;
    6281      278852 :   for (i = 1; i < l; i++)
    6282             :   {
    6283      252070 :     GEN chi = gel(vchi,i);
    6284             :     long n;
    6285      252070 :     if (!chi) continue;
    6286     1054578 :     for (n = 2; n < ordmax; n++)
    6287      965496 :       if (ugcd(n, ordmax) == 1)
    6288             :       {
    6289      397194 :         GEN tmp = vecmodii(gmulsg(n, chi), cyc);
    6290             :         long j;
    6291     7616616 :         for (j = i+1; j < l; j++)
    6292     7219422 :           if (gel(vchi,j) && gequal(gel(vchi,j), tmp)) gel(vchi,j) = NULL;
    6293             :       }
    6294             :   }
    6295      278852 :   for (i = c = 1; i < l; i++)
    6296             :   {
    6297      252070 :     GEN chi = gel(vchi,i);
    6298      252070 :     if (chi && bnrisconductor(bnr, chi)) gel(vchi, c++) = chi;
    6299             :   }
    6300       26782 :   setlg(vchi, c); return vchi;
    6301             : }
    6302             : 
    6303             : static GEN
    6304        7784 : get_gb(GEN bnr, GEN con)
    6305             : {
    6306        7784 :   GEN gb, g = bnr_get_gen(bnr), nf = bnr_get_nf(bnr);
    6307        7784 :   long i, l = lg(g);
    6308        7784 :   gb = cgetg(l, t_VEC);
    6309       18270 :   for (i = 1; i < l; i++)
    6310       10486 :     gel(gb,i) = ZV_to_zv(isprincipalray(bnr, galoisapply(nf, con, gel(g,i))));
    6311        7784 :   return gb;
    6312             : }
    6313             : static GEN
    6314       15834 : get_bnrconreyN(GEN bnr, GEN znN)
    6315             : {
    6316       15834 :   GEN z, g = znstar_get_conreygen(znN);
    6317       15834 :   long i, l = lg(g);
    6318       15834 :   z = cgetg(l, t_VEC);
    6319       15834 :   for (i = 1; i < l; i++) gel(z,i) = ZV_to_zv(isprincipalray(bnr,gel(g,i)));
    6320       15834 :   return z;
    6321             : }
    6322             : /* con = NULL if D > 0 or if D < 0 and id != idcon. */
    6323             : static GEN
    6324       33670 : mfdihedralcommon(GEN bnf, GEN id, GEN znN, GEN kroconreyN, long N, long D, GEN con)
    6325             : {
    6326       33670 :   GEN bnr = dihan_bnr(bnf, id), cyc = ZV_to_zv( bnr_get_cyc(bnr) );
    6327             :   GEN bnrconreyN, cycn, cycN, Lvchi, res, P, vT;
    6328             :   long j, ordmax, l, lc, deghecke, degrel, vt;
    6329             : 
    6330       33670 :   lc = lg(cyc); if (lc == 1) return NULL;
    6331       26782 :   cycn = cyc_normalize_zv(cyc);
    6332       26782 :   Lvchi = mklvchi(bnr, cycn, con? get_gb(bnr, con): NULL);
    6333       26782 :   l = lg(Lvchi);
    6334       26782 :   if (l == 1) return NULL;
    6335             : 
    6336       15834 :   bnrconreyN = get_bnrconreyN(bnr, znN);
    6337       15834 :   cycN = ZV_to_zv(znstar_get_cyc(znN));
    6338       15834 :   ordmax = cyc[1];
    6339       15834 :   vT = const_vec(odd(ordmax)? ordmax << 1: ordmax, NULL);
    6340       15834 :   vt = fetch_user_var("t");
    6341       15834 :   P = polcyclo(ordmax, vt);
    6342       15834 :   gel(vT,ordmax) = Qab_trace_init(ordmax, ordmax, P, P);
    6343       15834 :   deghecke = myeulerphiu(ordmax);
    6344       15834 :   res = cgetg(l, t_VEC);
    6345       50246 :   for (j = 1; j < l; j++)
    6346             :   {
    6347       34412 :     GEN T, v, vchi = ZV_to_zv(gel(Lvchi,j));
    6348       34412 :     GEN chi, chin = char_normalize_zv(vchi, cycn);
    6349             :     long o, vnum, k0;
    6350       34412 :     v = bnrchartwist2conrey(chin, cycn, bnrconreyN, kroconreyN);
    6351       34412 :     o = itou(Q_denom(v));
    6352       34412 :     T = gel(vT, o);
    6353       34412 :     if (!T) gel(vT,o) = T = Qab_trace_init(ordmax, o, P, polcyclo(o,vt));
    6354       34412 :     chi = conreydenormalize(znN, v);
    6355       34412 :     vnum = itou(znconreyexp(znN, chi));
    6356       34412 :     chi = ZV_to_zv(znconreychar(znN,chi));
    6357       34412 :     degrel = deghecke / degpol(gel(T,1));
    6358       34412 :     k0 = zv_cyc_minimize(cycN, chi, coprimes_zv(o));
    6359       34412 :     vnum = Fl_powu(vnum, k0, N);
    6360             :     /* encodes degrel forms: jdeg = 0..degrel-1 */
    6361       34412 :     gel(res,j) = mkvec3(mkvecsmalln(5, N, k0, vnum, D, degrel),
    6362             :                         id, mkvec3(cycn,chin,T));
    6363             :   }
    6364       15834 :   return res;
    6365             : }
    6366             : 
    6367             : static long
    6368       49322 : not_cond(long D, long n)
    6369             : {
    6370       49322 :   if (D > 0) return n == 4 && (D&7L) != 1;
    6371       30086 :   return n == 2 || n == 3 || (n == 4 && (D&7L)==1);
    6372             : }
    6373             : /* Append to v all dihedral weight 1 forms coming from D, if fundamental.
    6374             :  * level in [l1, l2] */
    6375             : static void
    6376       18578 : append_dihedral(GEN v, long D, long l1, long l2)
    6377             : {
    6378       18578 :   long Da = labs(D), no, i, numi, ct, min, max;
    6379             :   GEN bnf, con, LI, resall, arch1, arch2;
    6380             :   pari_sp av;
    6381             : 
    6382             :   /* min <= Nf <= max */
    6383       18578 :   max = l2 / Da;
    6384       18578 :   if (l1 == l2)
    6385             :   { /* assume Da | l2 */
    6386           0 :     min = max;
    6387           0 :     if (D > 0 && min < 3) return;
    6388             :   }
    6389             :   else /* assume l1 < l2 */
    6390       18578 :     min = (l1 + Da-1)/Da;
    6391       18578 :   if (!sisfundamental(D)) return;
    6392             : 
    6393        5684 :   av = avma;
    6394        5684 :   bnf = dihan_bnf(D);
    6395        5684 :   con = gel(galoisconj(bnf,gen_1), 2);
    6396        5684 :   LI = ideallist(bnf, max);
    6397        5684 :   numi = 0; for (i = min; i <= max; i++) numi += lg(gel(LI, i)) - 1;
    6398        5684 :   if (D > 0)
    6399             :   {
    6400        1414 :     numi <<= 1;
    6401        1414 :     arch1 = mkvec2(gen_1,gen_0);
    6402        1414 :     arch2 = mkvec2(gen_0,gen_1);
    6403             :   }
    6404             :   else
    6405        4270 :     arch1 = arch2 = NULL;
    6406        5684 :   resall = cgetg(numi+1, t_VEC); ct = 1;
    6407       55006 :   for (no = min; no <= max; no++) if (!not_cond(D, no))
    6408             :   {
    6409       44604 :     long N = Da*no, lgc, lglis;
    6410       44604 :     GEN LIs = gel(LI, no), znN = znstar0(utoipos(N), 1), conreyN, kroconreyN;
    6411             : 
    6412       44604 :     conreyN = znstar_get_conreygen(znN); lgc = lg(conreyN);
    6413       44604 :     kroconreyN = cgetg(lgc, t_VECSMALL);
    6414       44604 :     for (i = 1; i < lgc; i++) kroconreyN[i] = krosi(D, gel(conreyN, i));
    6415       44604 :     lglis = lg(LIs);
    6416       87752 :     for (i = 1; i < lglis; i++)
    6417             :     {
    6418       43148 :       GEN id = gel(LIs, i), idcon, z;
    6419             :       long j;
    6420       43148 :       if (typ(id) == t_INT) continue;
    6421       28154 :       idcon = galoisapply(bnf, con, id);
    6422       51380 :       for (j = i; j < lglis; j++)
    6423       51380 :         if (gequal(idcon, gel(LIs, j))) { gel(LIs, j) = gen_0; break; }
    6424       28154 :       if (D < 0)
    6425             :       {
    6426       17458 :         GEN conk = i == j ? con : NULL;
    6427       17458 :         z = mfdihedralcommon(bnf, id, znN, kroconreyN, N, D, conk);
    6428       17458 :         if (z) gel(resall, ct++) = z;
    6429             :       }
    6430             :       else
    6431             :       {
    6432             :         GEN ide;
    6433       10696 :         ide = mkvec2(id, arch1);
    6434       10696 :         z = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, NULL);
    6435       10696 :         if (z) gel(resall, ct++) = z;
    6436       10696 :         if (gequal(idcon,id)) continue;
    6437        5516 :         ide = mkvec2(id, arch2);
    6438        5516 :         z = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, NULL);
    6439        5516 :         if (z) gel(resall, ct++) = z;
    6440             :       }
    6441             :     }
    6442             :   }
    6443        5684 :   if (ct == 1) set_avma(av);
    6444             :   else
    6445             :   {
    6446        4788 :     setlg(resall, ct);
    6447        4788 :     vectrunc_append(v, gerepilecopy(av, shallowconcat1(resall)));
    6448             :   }
    6449             : }
    6450             : 
    6451             : static long
    6452       42042 : di_N(GEN a) { return gel(a,1)[1]; }
    6453             : /* All primitive dihedral wt1 forms: LIM a t_VECSMALL with a single component
    6454             :  * (only level LIM) or 2 components [m,M], m < M (between m and M) */
    6455             : static GEN
    6456          14 : mfdihedralall(GEN LIM)
    6457             : {
    6458             :   GEN res, z;
    6459             :   long limD, ct, i, l1, l2;
    6460             : 
    6461          14 :   if (lg(LIM) == 2) l1 = l2 = LIM[1]; else { l1 = LIM[1]; l2 = LIM[2]; }
    6462          14 :   limD = l2;
    6463          14 :   res = vectrunc_init(2*limD);
    6464          14 :   if (l1 == l2)
    6465             :   {
    6466           0 :     GEN D = mydivisorsu(l1);
    6467           0 :     long l = lg(D), j;
    6468           0 :     for (j = 2; j < l; j++)
    6469             :     { /* skip d = 1 */
    6470           0 :       long d = D[j];
    6471           0 :       if (d == 2) continue;
    6472           0 :       append_dihedral(res, -d, l1,l2);
    6473           0 :       if (d >= 5 && D[l-j] >= 3) append_dihedral(res, d, l1,l2); /* Nf >= 3 */
    6474             :     }
    6475             :   }
    6476             :   else
    6477             :   {
    6478             :     long D;
    6479          14 :     for (D = -3; D >= -limD; D--) append_dihedral(res, D, l1,l2);
    6480          14 :     limD /= 3; /* Nf >= 3 (GTM 193, prop 3.3.18) */
    6481          14 :     for (D = 5; D <= limD;   D++) append_dihedral(res, D, l1,l2);
    6482             :   }
    6483          14 :   ct = lg(res);
    6484          14 :   if (ct > 1) res = shallowconcat1(res);
    6485          14 :   if (l1 == l2) return res; /* single level */
    6486          14 :   if (ct > 1)
    6487             :   { /* sort wrt N */
    6488          14 :     res = vecpermute(res, indexvecsort(res, mkvecsmall(1)));
    6489          14 :     ct = lg(res);
    6490             :   }
    6491          14 :   z = const_vec(l2-l1+1, cgetg(1,t_VEC));
    6492        7672 :   for (i = 1; i < ct;)
    6493             :   { /* regroup result sharing the same N */
    6494        7644 :     long n = di_N(gel(res,i)), j = i+1, k;
    6495             :     GEN v;
    6496        7644 :     while (j < ct && di_N(gel(res,j)) == n) j++;
    6497        7644 :     n -= l1-1;
    6498        7644 :     gel(z, n) = v = cgetg(j-i+1, t_VEC);
    6499        7644 :     for (k = 1; i < j; k++,i++) gel(v,k) = gel(res,i);
    6500             :   }
    6501          14 :   return z;
    6502             : }
    6503             : 
    6504             : /* return [vF, index], where vecpermute(vF,index) generates dihedral forms
    6505             :  * for character CHI */
    6506             : static GEN
    6507       23779 : mfdihedralnew_i(long N, GEN CHI)
    6508             : {
    6509             :   GEN bnf, Tinit, Pm, vf, M, V, NK, SP;
    6510             :   long Dold, d, ordw, i, SB, c, l, k0, k1, chino, chinoorig, lv;
    6511             : 
    6512       23779 :   SP = cache_get(cache_DIH, N);
    6513       23779 :   if (!SP) SP = mfdihedralall(mkvecsmall(N));
    6514       23779 :   lv = lg(SP); if (lv == 1) return NULL;
    6515       11354 :   CHI = mfcharinduce(CHI,N);
    6516       11354 :   ordw = mfcharorder(CHI);
    6517       11354 :   chinoorig = mfcharno(CHI);
    6518       11354 :   k0 = mfconreyminimize(CHI);
    6519       11354 :   chino = Fl_powu(chinoorig, k0, N);
    6520       11354 :   k1 = Fl_inv(k0 % ordw, ordw);
    6521       11354 :   V = cgetg(lv, t_VEC);
    6522       11354 :   d = 0;
    6523       35245 :   for (i = l = 1; i < lv; i++)
    6524             :   {
    6525       23891 :     GEN sp = gel(SP,i), T = gel(sp,1);
    6526       23891 :     if (T[3] != chino) continue;
    6527        3563 :     d += T[5];
    6528        3563 :     if (k1 != 1)
    6529             :     {
    6530          77 :       GEN t = leafcopy(T);
    6531          77 :       t[3] = chinoorig;
    6532          77 :       t[2] = (t[2]*k1) % ordw;
    6533          77 :       sp = mkvec4(t, gel(sp,2), gel(sp,3), gel(sp,4));
    6534             :     }
    6535        3563 :     gel(V, l++) = sp;
    6536             :   }
    6537       11354 :   setlg(V, l); /* dihedral forms of level N and character CHI */
    6538       11354 :   if (l == 1) return NULL;
    6539             : 
    6540        2338 :   SB = myeulerphiu(ordw) * mfsturmNk(N,1) + 1;
    6541        2338 :   M = cgetg(d+1, t_MAT);
    6542        2338 :   vf = cgetg(d+1, t_VEC);
    6543        2338 :   NK = mkNK(N, 1, CHI);
    6544        2338 :   bnf = NULL; Dold = 0;
    6545        5901 :   for (i = c = 1; i < l; i++)
    6546             :   { /* T = [N, k0, conreyno, D, degrel] */
    6547        3563 :     GEN bnr, Vi = gel(V,i), T = gel(Vi,1), id = gel(Vi,2), w = gel(Vi,3);
    6548        3563 :     long jdeg, k0i = T[2], D = T[4], degrel = T[5];
    6549             : 
    6550        3563 :     if (D != Dold) { Dold = D; bnf = dihan_bnf(D); }
    6551        3563 :     bnr = dihan_bnr(bnf, id);
    6552       10479 :     for (jdeg = 0; jdeg < degrel; jdeg++,c++)
    6553             :     {
    6554        6916 :       GEN k0j = mkvecsmall2(k0i, jdeg), an = dihan(bnr, w, k0j, SB);
    6555        6916 :       settyp(an, t_COL); gel(M,c) = Q_primpart(an);
    6556        6916 :       gel(vf,c) = tag3(t_MF_DIHEDRAL, NK, bnr, w, k0j);
    6557             :     }
    6558             :   }
    6559        2338 :   Tinit = gmael3(V,1,3,3); Pm = gel(Tinit,1);
    6560        2338 :   V = QabM_indexrank(M, degpol(Pm)==1? NULL: Pm, ordw);
    6561        2338 :   return mkvec2(vf,gel(V,2));
    6562             : }
    6563             : static long
    6564       15820 : mfdihedralnewdim(long N, GEN CHI)
    6565             : {
    6566       15820 :   pari_sp av = avma;
    6567       15820 :   GEN S = mfdihedralnew_i(N, CHI);
    6568       15820 :   return gc_long(av, S? lg(gel(S,2))-1: 0);
    6569             : }
    6570             : static GEN
    6571        7959 : mfdihedralnew(long N, GEN CHI)
    6572             : {
    6573        7959 :   pari_sp av = avma;
    6574        7959 :   GEN S = mfdihedralnew_i(N, CHI);
    6575        7959 :   if (!S) { set_avma(av); return cgetg(1, t_VEC); }
    6576         777 :   return vecpermute(gel(S,1), gel(S,2));
    6577             : }
    6578             : 
    6579             : static long
    6580        7042 : mfdihedralcuspdim(long N, GEN CHI)
    6581             : {
    6582        7042 :   pari_sp av = avma;
    6583             :   GEN D, CHIP;
    6584             :   long F, i, lD, dim;
    6585             : 
    6586        7042 :   CHIP = mfchartoprimitive(CHI, &F);
    6587        7042 :   D = mydivisorsu(N/F); lD = lg(D);
    6588        7042 :   dim = mfdihedralnewdim(N, CHI); /* d = 1 */
    6589       15820 :   for (i = 2; i < lD; i++)
    6590             :   {
    6591        8778 :     long d = D[i], M = N/d, a = mfdihedralnewdim(M, CHIP);
    6592        8778 :     if (a) dim += a * mynumdivu(d);
    6593             :   }
    6594        7042 :   return gc_long(av,dim);
    6595             : }
    6596             : 
    6597             : static GEN
    6598        5698 : mfbdall(GEN E, long N)
    6599             : {
    6600        5698 :   GEN v, D = mydivisorsu(N);
    6601        5698 :   long i, j, nD = lg(D) - 1, nE = lg(E) - 1;
    6602        5698 :   v = cgetg(nD*nE + 1, t_VEC);
    6603        7406 :   for (j = 1; j <= nE; j++)
    6604             :   {
    6605        1708 :     GEN Ej = gel(E, j);
    6606        1708 :     for (i = 0; i < nD; i++) gel(v, i*nE + j) = mfbd_i(Ej, D[i+1]);
    6607             :   }
    6608        5698 :   return v;
    6609             : }
    6610             : static GEN
    6611        3458 : mfdihedralcusp(long N, GEN CHI)
    6612             : {
    6613        3458 :   pari_sp av = avma;
    6614             :   GEN D, CHIP, z;
    6615             :   long F, i, lD;
    6616             : 
    6617        3458 :   CHIP = mfchartoprimitive(CHI, &F);
    6618        3458 :   D = mydivisorsu(N/F); lD = lg(D);
    6619        3458 :   z = cgetg(lD, t_VEC);
    6620        3458 :   gel(z,1) = mfdihedralnew(N, CHI);
    6621        7749 :   for (i = 2; i < lD; i++) /* skip 1 */
    6622             :   {
    6623        4291 :     long d = D[i], M = N / d;
    6624        4291 :     GEN LF = mfdihedralnew(M, mfcharinduce(CHIP, M));
    6625        4291 :     gel(z,i) = mfbdall(LF, d);
    6626             :   }
    6627        3458 :   return gerepilecopy(av, shallowconcat1(z));
    6628             : }
    6629             : 
    6630             : /* used to decide between ratlift and comatrix for ZM_inv; ratlift is better
    6631             :  * when N has many divisors */
    6632             : static int
    6633        2436 : abundant(ulong N) { return mynumdivu(N) >= 8; }
    6634             : 
    6635             : /* CHI an mfchar */
    6636             : static int
    6637         294 : cmp_ord(void *E, GEN a, GEN b)
    6638             : {
    6639         294 :   GEN chia = MF_get_CHI(a), chib = MF_get_CHI(b);
    6640         294 :   (void)E; return cmpii(gmfcharorder(chia), gmfcharorder(chib));
    6641             : }
    6642             : /* mfinit structure.
    6643             : -- mf[1] contains [N,k,CHI,space],
    6644             : -- mf[2] contains vector of closures of Eisenstein series, empty if not
    6645             :    full space.
    6646             : -- mf[3] contains vector of closures, so #mf[3] = dimension of cusp/new space.
    6647             : -- mf[4] contains the corresponding indices: either j for T(j)tf if newspace,
    6648             :    or [M,j,d] for B(d)T(j)tf_M if cuspspace or oldspace.
    6649             : -- mf[5] contains the matrix M of first coefficients of basis, never cleaned.
    6650             :  * NK is either [N,k] or [N,k,CHI].
    6651             :  * mfinit does not do the splitting, only the basis generation. */
    6652             : 
    6653             : /* Set flraw to 1 if do not need mf[5]: no mftobasis etc..., only the
    6654             :    expansions of the basis elements are needed. */
    6655             : 
    6656             : static GEN
    6657        4739 : mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw)
    6658             : {
    6659        4739 :   GEN M = NULL, mf = NULL, mf1 = mkvec4(utoi(N), stoi(k), CHI, utoi(space));
    6660        4739 :   long sb = mfsturmNk(N, k);
    6661             :   cachenew_t cache;
    6662        4739 :   if (k < 0 || badchar(N, k, CHI)) return mfEMPTY(mf1);
    6663        4704 :   if (k == 0) /*nothing*/;
    6664        4662 :   else if (k == 1)
    6665             :   {
    6666         364 :     switch (space)
    6667             :     {
    6668             :       case mf_NEW:
    6669             :       case mf_FULL:
    6670         336 :       case mf_CUSP: mf = mfwt1init(N, CHI, NULL, space, flraw); break;
    6671          14 :       case mf_EISEN:break;
    6672           7 :       case mf_OLD: pari_err_IMPL("mfinit in weight 1 for old space");
    6673           7 :       default: pari_err_FLAG("mfinit");
    6674             :     }
    6675             :   }
    6676             :   else /* k >= 2 */
    6677             :   {
    6678        4298 :     long ord = mfcharorder(CHI);
    6679        4298 :     GEN z = NULL, P = (ord <= 2)? NULL: mfcharpol(CHI);
    6680        4298 :     switch(space)
    6681             :     {
    6682             :       case mf_EISEN:
    6683         105 :         break;
    6684             :       case mf_NEW:
    6685        1197 :         mf = mfnewinit(N, k, CHI, &cache, 1);
    6686        1197 :         if (mf && !flraw) { M = MF_get_M(mf); z = MF_get_Mindex(mf); }
    6687        1197 :         break;
    6688             :       case mf_OLD:
    6689             :       case mf_CUSP:
    6690             :       case mf_FULL:
    6691        2989 :         mf = mfinitcusp(N, k, CHI, &cache, space);
    6692        2989 :         if (mf && !flraw)
    6693             :         {
    6694        2149 :           GEN S = MF_get_S(mf);
    6695        2149 :           M = bhnmat_extend(M, sb+1, 1, S, &cache);
    6696        2149 :           if (space != mf_FULL) gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6697             :         }
    6698        2989 :         dbg_cachenew(&cache);
    6699        2989 :         break;
    6700           7 :       default: pari_err_FLAG("mfinit");
    6701             :     }
    6702        4291 :     if (z) gel(mf,5) = mfclean2(M, z, P, ord);
    6703             :   }
    6704        4683 :   if (!mf) mf = mfEMPTY(mf1);
    6705             :   else
    6706             :   {
    6707        3773 :     gel(mf,1) = mf1;
    6708        3773 :     if (flraw) gel(mf,5) = zerovec(3);
    6709             :   }
    6710        4683 :   if (!space_is_cusp(space))
    6711             :   {
    6712         686 :     GEN E = mfeisensteinbasis(N, k, CHI);
    6713         686 :     gel(mf,2) = E;
    6714         686 :     if (!flraw)
    6715             :     {
    6716         462 :       if (M)
    6717         175 :         M = shallowconcat(mfvectomat(E, sb+1, 1), M);
    6718             :       else
    6719         287 :         M = mfcoefs_mf(mf, sb+1, 1);
    6720         462 :       gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6721             :     }
    6722             :   }
    6723        4683 :   return mf;
    6724             : }
    6725             : 
    6726             : /* mfinit for k = nk/dk */
    6727             : static GEN
    6728        2569 : mfinit_Nndkchi(long N, long nk, long dk, GEN CHI, long space, long flraw)
    6729         217 : { return (dk == 2)? mf2init_Nkchi(N, nk >> 1, CHI, space, flraw)
    6730        2786 :                   : mfinit_Nkchi(N, nk, CHI, space, flraw); }
    6731             : static GEN
    6732        3227 : mfinit_i(GEN NK, long space)
    6733             : {
    6734             :   GEN CHI, mf;
    6735             :   long N, k, dk, joker;
    6736        3227 :   if (checkmf_i(NK))
    6737             :   {
    6738         133 :     N = mf_get_N(NK);
    6739         133 :     Qtoss(mf_get_gk(NK), &k, &dk);
    6740         133 :     CHI = mf_get_CHI(NK);
    6741             :   }
    6742        3094 :   else if ((mf = checkMF_i(NK)))
    6743             :   {
    6744          21 :     long s = MF_get_space(mf);
    6745          21 :     if (s == space) return mf;
    6746          21 :     Qtoss(MF_get_gk(mf), &k, &dk);
    6747          21 :     if (dk == 1 && k > 1 && space == mf_NEW && (s == mf_CUSP || s == mf_FULL))
    6748          21 :       return mfinittonew(mf);
    6749           0 :     N = MF_get_N(mf);
    6750           0 :     CHI = MF_get_CHI(mf);
    6751             :   }
    6752             :   else
    6753        3073 :     checkNK2(NK, &N, &k, &dk, &CHI, 1);
    6754        3185 :   joker = !CHI || typ(CHI) == t_COL;
    6755        3185 :   if (joker)
    6756             :   {
    6757        1141 :     GEN mf, vCHI = CHI;
    6758             :     long i, j, l;
    6759        1141 :     if (CHI && lg(CHI) == 1) return cgetg(1,t_VEC);
    6760        1134 :     if (k < 0) return mfEMPTYall(N, sstoQ(k,dk), CHI, space);
    6761        1120 :     if (k == 1 && dk == 1 && space != mf_EISEN)
    6762         483 :     {
    6763             :       GEN TMP, gN, gs;
    6764        1085 :       if (space != mf_CUSP && space != mf_NEW)
    6765           0 :         pari_err_IMPL("mfinit([N,1,wildcard], space != cusp or new space)");
    6766        1085 :       if (wt1empty(N)) return mfEMPTYall(N, gen_1, CHI, space);
    6767         483 :       vCHI = mfwt1chars(N,vCHI);
    6768         483 :       l = lg(vCHI); mf = cgetg(l, t_VEC); if (l == 1) return mf;
    6769         483 :       TMP = mfwt1_pre(N); gN = utoipos(N); gs = utoi(space);
    6770        3717 :       for (i = j = 1; i < l; i++)
    6771             :       {
    6772        3234 :         pari_sp av = avma;
    6773        3234 :         GEN c = gel(vCHI,i), z = mfwt1init(N, c, TMP, space, 0);
    6774        3234 :         if (!z) {
    6775        2590 :           set_avma(av);
    6776        2590 :           if (CHI) z = mfEMPTY(mkvec4(gN,gen_1,c,gs));
    6777             :         }
    6778        3234 :         if (z) gel(mf, j++) = z;
    6779             :       }
    6780             :     }
    6781             :     else
    6782             :     {
    6783          35 :       vCHI = mfchars(N,k,dk,vCHI);
    6784          35 :       l = lg(vCHI); mf = cgetg(l, t_VEC);
    6785         119 :       for (i = j = 1; i < l; i++)
    6786             :       {
    6787          84 :         pari_sp av = avma;
    6788          84 :         GEN v = mfinit_Nndkchi(N, k, dk, gel(vCHI,i), space, 0);
    6789          84 :         if (MF_get_dim(v) || CHI) gel(mf, j++) = v; else set_avma(av);
    6790             :       }
    6791             :     }
    6792         518 :     setlg(mf,j);
    6793         518 :     if (!CHI) gen_sort_inplace(mf, NULL, &cmp_ord, NULL);
    6794         518 :     return mf;
    6795             :   }
    6796        2044 :   return mfinit_Nndkchi(N, k, dk, CHI, space, 0);
    6797             : }
    6798             : GEN
    6799        2268 : mfinit(GEN NK, long space)
    6800             : {
    6801        2268 :   pari_sp av = avma;
    6802        2268 :   return gerepilecopy(av, mfinit_i(NK, space));
    6803             : }
    6804             : 
    6805             : /* UTILITY FUNCTIONS */
    6806             : static void
    6807         357 : cusp_canon(GEN cusp, long N, long *pA, long *pC)
    6808             : {
    6809         357 :   pari_sp av = avma;
    6810             :   long A, C, tc, cg;
    6811         357 :   if (N <= 0) pari_err_DOMAIN("mfcuspwidth","N","<=",gen_0,stoi(N));
    6812         350 :   if (!cusp || (tc = typ(cusp)) == t_INFINITY) { *pA = 1; *pC = N; return; }
    6813         343 :   if (tc != t_INT && tc != t_FRAC) pari_err_TYPE("checkcusp", cusp);
    6814         343 :   Qtoss(cusp, &A,&C);
    6815         343 :   if (N % C)
    6816             :   {
    6817             :     ulong uC;
    6818          14 :     long u = Fl_invgen((C-1)%N + 1, N, &uC);
    6819          14 :     A = Fl_mul(A, u, N);
    6820          14 :     C = (long)uC;
    6821             :   }
    6822         343 :   cg = ugcd(C, N/C);
    6823         343 :   while (ugcd(A, N) > 1) A += cg;
    6824         343 :   *pA = A % N; *pC = C; set_avma(av);
    6825             : }
    6826             : static long
    6827         896 : mfcuspcanon_width(long N, long C)
    6828         896 : { return (!C || C == N)? 1 : N / ugcd(N, Fl_sqr(umodsu(C,N),N)); }
    6829             : /* v = [a,c] a ZC, width of cusp (a:c) */
    6830             : static long
    6831        8743 : mfZC_width(long N, GEN v)
    6832             : {
    6833        8743 :   ulong C = umodiu(gel(v,2), N);
    6834        8743 :   return (C == 0)? 1: N / ugcd(N, Fl_sqr(C,N));
    6835             : }
    6836             : long
    6837         161 : mfcuspwidth(GEN gN, GEN cusp)
    6838             : {
    6839         161 :   long N = 0, A, C;
    6840             :   GEN mf;
    6841         161 :   if (typ(gN) == t_INT) N = itos(gN);
    6842          42 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    6843           0 :   else pari_err_TYPE("mfcuspwidth", gN);
    6844         161 :   cusp_canon(cusp, N, &A, &C);
    6845         154 :   return mfcuspcanon_width(N, C);
    6846             : }
    6847             : 
    6848             : /* Q a t_INT */
    6849             : static GEN
    6850          14 : findq(GEN al, GEN Q)
    6851             : {
    6852             :   long n;
    6853          14 :   if (typ(al) == t_FRAC && cmpii(gel(al,2), Q) <= 0)
    6854           0 :     return mkvec(mkvec2(gel(al,1), gel(al,2)));
    6855          14 :   n = 1 + (long)ceil(2.0781*gtodouble(glog(Q, LOWDEFAULTPREC)));
    6856          14 :   return contfracpnqn(gboundcf(al,n), n);
    6857             : }
    6858             : static GEN
    6859          91 : findqga(long N, GEN z)
    6860             : {
    6861          91 :   GEN Q, LDC, CK = NULL, DK = NULL, ma, x, y = imag_i(z);
    6862             :   long j, l;
    6863          91 :   if (gcmpgs(gmulsg(2*N, y), 1) >= 0) return NULL;
    6864          14 :   x = real_i(z);
    6865          14 :   Q = ground(ginv(gsqrt(gmulsg(N, y), LOWDEFAULTPREC)));
    6866          14 :   LDC = findq(gmulsg(-N,x), Q);
    6867          14 :   ma = gen_1; l = lg(LDC);
    6868          35 :   for (j = 1; j < l; j++)
    6869             :   {
    6870          21 :     GEN D, DC = gel(LDC,j), C1 = gel(DC,2);
    6871          21 :     if (cmpii(C1,Q) > 0) break;
    6872          21 :     D = gel(DC,1);
    6873          21 :     if (ugcdiu(D,N) == 1)
    6874             :     {
    6875           7 :       GEN C = mului(N, C1), den;
    6876           7 :       den = gadd(gsqr(gmul(C,y)), gsqr(gadd(D, gmul(C,x))));
    6877           7 :       if (gcmp(den, ma) < 0) { ma = den; CK = C; DK = D; }
    6878             :     }
    6879             :   }
    6880          14 :   return DK? mkvec2(CK, DK): NULL;
    6881             : }
    6882             : 
    6883             : static long
    6884         154 : valNC2(GEN P, GEN E, long e)
    6885             : {
    6886         154 :   long i, d = 1, l = lg(P);
    6887         476 :   for (i = 1; i < l; i++)
    6888             :   {
    6889         322 :     long v = u_lval(e, P[i]) << 1;
    6890         322 :     if (v == E[i] + 1) v--;
    6891         322 :     d *= upowuu(P[i], v);
    6892             :   }
    6893         154 :   return d;
    6894             : }
    6895             : 
    6896             : static GEN
    6897          42 : findqganew(long N, GEN z)
    6898             : {
    6899          42 :   GEN MI, DI, x = real_i(z), y = imag_i(z), Ck = gen_0, Dk = gen_1, fa, P, E;
    6900             :   long i;
    6901          42 :   MI = ginv(utoi(N));
    6902          42 :   DI = mydivisorsu(mysqrtu(N));
    6903          42 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    6904         196 :   for (i = 1; i < lg(DI); i++)
    6905             :   {
    6906         154 :     long e = DI[i], g;
    6907             :     GEN U, C, D, m;
    6908         154 :     (void)cxredsl2(gmulsg(e, z), &U);
    6909         154 :     C = gcoeff(U,2,1); if (!signe(C)) continue;
    6910         154 :     D = gcoeff(U,2,2);
    6911         154 :     g = ugcdiu(D,e);
    6912         154 :     if (g > 1) { C = muliu(C,e/g); D = diviuexact(D,g); } else C = muliu(C,e);
    6913         154 :     m = gadd(gsqr(gadd(gmul(C, x), D)), gsqr(gmul(C, y)));
    6914         154 :     m = gdivgs(m, valNC2(P, E, e));
    6915         154 :     if (gcmp(m, MI) < 0) { MI = m; Ck = C; Dk = D; }
    6916             :   }
    6917          42 :   return signe(Ck)? mkvec2(Ck, Dk): NULL;
    6918             : }
    6919             : 
    6920             : /* Return z' and U = [a,b;c,d] \in SL_2(Z), z' = U*z,
    6921             :  * Im(z')/width(U.oo) > sqrt(3)/(2N). Set *pczd = c*z+d */
    6922             : static GEN
    6923         168 : cxredga0N(long N, GEN z, GEN *pU, GEN *pczd, long flag)
    6924             : {
    6925         168 :   GEN v = NULL, A, B, C, D;
    6926             :   long e;
    6927         168 :   if (N == 1) return cxredsl2_i(z, pU, pczd);
    6928         133 :   e = gexpo(gel(z,2));
    6929         133 :   if (e < 0) z = gprec_wensure(z, precision(z) + nbits2extraprec(-e));
    6930         133 :   v = flag? findqganew(N,z): findqga(N,z);
    6931         133 :   if (!v) { *pU = matid(2); *pczd = gen_1; return z; }
    6932          49 :   C = gel(v,1);
    6933          49 :   D = gel(v,2);
    6934          49 :   if (!is_pm1(bezout(C,D, &B,&A))) pari_err_BUG("cxredga0N [gcd > 1]");
    6935          49 :   B = negi(B);
    6936          49 :   *pU = mkmat2(mkcol2(A,C), mkcol2(B,D));
    6937          49 :   *pczd = gadd(gmul(C,z), D);
    6938          49 :   return gdiv(gadd(gmul(A,z), B), *pczd);
    6939             : }
    6940             : 
    6941             : static GEN
    6942         154 : lfunthetaall(GEN b, GEN vL, GEN t, long bitprec)
    6943             : {
    6944         154 :   long i, l = lg(vL);
    6945         154 :   GEN v = cgetg(l, t_VEC);
    6946         336 :   for (i = 1; i < l; i++)
    6947             :   {
    6948         182 :     GEN T, L = gel(vL,i), a0 = gel(L,1), ldata = gel(L,2);
    6949         182 :     GEN van = gel(ldata_get_an(ldata),2);
    6950         182 :     if (lg(van) == 1)
    6951             :     {
    6952           0 :       T = gmul(b, a0);
    6953           0 :       if (isexactzero(T)) { GEN z = real_0_bit(-bitprec); T = mkcomplex(z,z); }
    6954             :     }
    6955             :     else
    6956             :     {
    6957         182 :       T = gmul2n(lfuntheta(ldata, t, 0, bitprec), -1);
    6958         182 :       T = gmul(b, gadd(a0, T));
    6959             :     }
    6960         182 :     gel(v,i) = T;
    6961             :   }
    6962         154 :   return l == 2? gel(v,1): v;
    6963             : }
    6964             : 
    6965             : /* P in ZX, irreducible */
    6966             : static GEN
    6967         175 : ZX_roots(GEN P, long prec)
    6968             : {
    6969         175 :   long d = degpol(P);
    6970         175 :   if (d == 1) return mkvec(gen_0);
    6971         175 :   if (d == 2 && isint1(gel(P,2)) && isintzero(gel(P,3)) && isint1(gel(P,4)))
    6972           7 :     return mkvec2(powIs(3), gen_I()); /* order as polroots */
    6973         168 :   return (ZX_sturm_irred(P) == d)? ZX_realroots_irred(P, prec)
    6974         168 :                                  : QX_complex_roots(P, prec);
    6975             : }
    6976             : /* initializations for RgX_RgV_eval / RgC_embed */
    6977             : static GEN
    6978         210 : rootspowers(GEN v)
    6979             : {
    6980         210 :   long i, l = lg(v);
    6981         210 :   GEN w = cgetg(l, t_VEC);
    6982         210 :   for (i = 1; i < l; i++) gel(w,i) = gpowers(gel(v,i), l-2);
    6983         210 :   return w;
    6984             : }
    6985             : /* mf embeddings attached to Q(chi)/(T), chi attached to cyclotomic P */
    6986             : static GEN
    6987         868 : getembed(GEN P, GEN T, GEN zcyclo, long prec)
    6988             : {
    6989             :   long i, l;
    6990             :   GEN v;
    6991         868 :   if (degpol(P) == 1) P = NULL; /* mfcharpol for quadratic char */
    6992         868 :   if (degpol(T) == 1) T = NULL; /* dim 1 orbit */
    6993         868 :   if (T && P)
    6994          35 :   { /* K(y) / (T(y)), K = Q(t)/(P) cyclotomic */
    6995          35 :     GEN vr = RgX_is_ZX(T)? ZX_roots(T,prec): roots(RgX_embed1(T,zcyclo), prec);
    6996          35 :     v = rootspowers(vr); l = lg(v);
    6997          35 :     for (i = 1; i < l; i++) gel(v,i) = mkcol3(P,zcyclo,gel(v,i));
    6998             :   }
    6999         833 :   else if (T)
    7000             :   { /* Q(y) / (T(y)), T non-cyclotomic */
    7001         175 :     GEN vr = ZX_roots(T, prec);
    7002         175 :     v = rootspowers(vr); l = lg(v);
    7003         175 :     for (i = 1; i < l; i++) gel(v,i) = mkcol2(T, gel(v,i));
    7004             :   }
    7005             :   else /* cyclotomic or rational */
    7006         658 :     v = mkvec(P? mkvec2(P, zcyclo): cgetg(1,t_VEC));
    7007         868 :   return v;
    7008             : }
    7009             : static GEN
    7010         721 : grootsof1_CHI(GEN CHI, long prec)
    7011         721 : { return grootsof1(mfcharorder(CHI), prec); }
    7012             : /* return the [Q(F):Q(chi)] embeddings of F */
    7013             : static GEN
    7014         567 : mfgetembed(GEN F, long prec)
    7015             : {
    7016         567 :   GEN T = mf_get_field(F), CHI = mf_get_CHI(F), P = mfcharpol(CHI);
    7017         567 :   return getembed(P, T, grootsof1_CHI(CHI, prec), prec);
    7018             : }
    7019             : static GEN
    7020           7 : mfchiembed(GEN mf, long prec)
    7021             : {
    7022           7 :   GEN CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    7023           7 :   return getembed(P, pol_x(0), grootsof1_CHI(CHI, prec), prec);
    7024             : }
    7025             : /* mfgetembed for the successive eigenforms in MF_get_newforms */
    7026             : static GEN
    7027         147 : mfeigenembed(GEN mf, long prec)
    7028             : {
    7029         147 :   GEN vP = MF_get_fields(mf), vF = MF_get_newforms(mf);
    7030         147 :   GEN zcyclo, vE, CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    7031         147 :   long i, l = lg(vP);
    7032         147 :   vF = Q_remove_denom(liftpol_shallow(vF), NULL);
    7033         147 :   prec += nbits2extraprec(gexpo(vF));
    7034         147 :   zcyclo = grootsof1_CHI(CHI, prec);
    7035         147 :   vE = cgetg(l, t_VEC);
    7036         147 :   for (i = 1; i < l; i++) gel(vE,i) = getembed(P, gel(vP,i), zcyclo, prec);
    7037         147 :   return vE;
    7038             : }
    7039             : 
    7040             : static int
    7041          28 : checkPv(GEN P, GEN v)
    7042          28 : { return typ(P) == t_POL && is_vec_t(typ(v)) && lg(v)-1 >= degpol(P); }
    7043             : static int
    7044          28 : checkemb_i(GEN E)
    7045             : {
    7046          28 :   long t = typ(E), l = lg(E);
    7047          28 :   if (t == t_VEC) return l == 1 || (l == 3 && checkPv(gel(E,1), gel(E,2)));
    7048          21 :   if (t != t_COL) return 0;
    7049          21 :   if (l == 3) return checkPv(gel(E,1), gel(E,2));
    7050          21 :   return l == 4 && is_vec_t(typ(gel(E,2))) && checkPv(gel(E,1), gel(E,3));
    7051             : }
    7052             : static GEN
    7053          28 : anyembed(GEN v, GEN E)
    7054             : {
    7055          28 :   switch(typ(v))
    7056             :   {
    7057          21 :     case t_VEC: case t_COL: return mfvecembed(E, v);
    7058           7 :     case t_MAT: return mfmatembed(E, v);
    7059             :   }
    7060           0 :   return mfembed(E, v);
    7061             : }
    7062             : GEN
    7063          49 : mfembed0(GEN E, GEN v, long prec)
    7064             : {
    7065          49 :   pari_sp av = avma;
    7066          49 :   GEN mf, vE = NULL;
    7067          49 :   if (checkmf_i(E)) vE = mfgetembed(E, prec);
    7068          35 :   else if ((mf = checkMF_i(E))) vE = mfchiembed(mf, prec);
    7069          49 :   if (vE)
    7070             :   {
    7071          21 :     long i, l = lg(vE);
    7072             :     GEN w;
    7073          21 :     if (!v) return gerepilecopy(av, l == 2? gel(vE,1): vE);
    7074           0 :     w = cgetg(l, t_VEC);
    7075           0 :     for (i = 1; i < l; i++) gel(w,i) = anyembed(v, gel(vE,i));
    7076           0 :     return gerepilecopy(av, l == 2? gel(w,1): w);
    7077             :   }
    7078          28 :   if (!checkemb_i(E) || !v) pari_err_TYPE("mfembed", E);
    7079          28 :   return gerepilecopy(av, anyembed(v,E));
    7080             : }
    7081             : 
    7082             : /* dummy lfun create for theta evaluation */
    7083             : static GEN
    7084         882 : mfthetaancreate(GEN van, GEN N, GEN k)
    7085             : {
    7086         882 :   GEN L = zerovec(6);
    7087         882 :   gel(L,1) = lfuntag(t_LFUN_GENERIC, van);
    7088         882 :   gel(L,3) = mkvec2(gen_0, gen_1);
    7089         882 :   gel(L,4) = k;
    7090         882 :   gel(L,5) = N; return L;
    7091             : }
    7092             : /* destroy van and prepare to evaluate theta(sigma(van)), for all sigma in
    7093             :  * embeddings vector vE */
    7094             : static GEN
    7095         322 : van_embedall(GEN van, GEN vE, GEN gN, GEN gk)
    7096             : {
    7097         322 :   GEN a0 = gel(van,1), vL;
    7098         322 :   long i, lE = lg(vE), l = lg(van);
    7099         322 :   van++; van[0] = evaltyp(t_VEC) | evallg(l-1); /* remove a0 */
    7100         322 :   vL = cgetg(lE, t_VEC);
    7101         847 :   for (i = 1; i < lE; i++)
    7102             :   {
    7103         525 :     GEN E = gel(vE,i), v = mfvecembed(E, van);
    7104         525 :     gel(vL,i) = mkvec2(mfembed(E,a0), mfthetaancreate(v, gN, gk));
    7105             :   }
    7106         322 :   return vL;
    7107             : }
    7108             : 
    7109             : static int
    7110        1022 : cusp_AC(GEN cusp, long *A, long *C)
    7111             : {
    7112        1022 :   switch(typ(cusp))
    7113             :   {
    7114         105 :     case t_INFINITY: *A = 1; *C = 0; break;
    7115         273 :     case t_INT:  *A = itos(cusp); *C = 1; break;
    7116         427 :     case t_FRAC: *A = itos(gel(cusp, 1)); *C = itos(gel(cusp, 2)); break;
    7117             :     case t_REAL: case t_COMPLEX:
    7118         217 :       *A = 0; *C = 0;
    7119         217 :       if (gsigne(imag_i(cusp)) <= 0)
    7120           7 :         pari_err_DOMAIN("mfeval","imag(tau)","<=",gen_0,cusp);
    7121         210 :       return 0;
    7122           0 :     default: pari_err_TYPE("cusp_AC", cusp);
    7123             :   }
    7124         805 :   return 1;
    7125             : }
    7126             : static GEN
    7127         511 : cusp2mat(long A, long C)
    7128             : { long B, D;
    7129         511 :   cbezout(A, C, &D, &B);
    7130         511 :   return mkmat22s(A, -B, C, D);
    7131             : }
    7132             : static GEN
    7133          21 : mkS(void) { return mkmat22s(0,-1,1,0); }
    7134             : 
    7135             : /* if t is a cusp, return F(t), else NULL */
    7136             : static GEN
    7137         350 : evalcusp(GEN mf, GEN F, GEN t, long prec)
    7138             : {
    7139             :   long A, C;
    7140             :   GEN R;
    7141         350 :   if (!cusp_AC(t, &A,&C)) return NULL;
    7142         189 :   if (C % mf_get_N(F) == 0) return gel(mfcoefs_i(F, 0, 1), 1);
    7143         175 :   R = mfgaexpansion(mf, F, cusp2mat(A,C), 0, prec);
    7144         175 :   return gequal0(gel(R,1))? gmael(R,3,1): gen_0;
    7145             : }
    7146             : /* Evaluate an mf closure numerically, i.e., in the usual sense, either for a
    7147             :  * single tau or a vector of tau; for each, return a vector of results
    7148             :  * corresponding to all complex embeddings of F. If flag is non-zero, allow
    7149             :  * replacing F by F | gamma to increase imag(gamma^(-1).tau) [ expensive if
    7150             :  * MF_EISENSPACE not present ] */
    7151             : static GEN
    7152         161 : mfeval_i(GEN mf, GEN F, GEN vtau, long flag, long bitprec)
    7153             : {
    7154             :   GEN L0, vL, vb, sqN, vczd, vTAU, vs, van, vE;
    7155         161 :   long N = MF_get_N(mf), N0, ta, lv, i, prec = nbits2prec(bitprec);
    7156         161 :   GEN gN = utoipos(N), gk = mf_get_gk(F), gk1 = gsubgs(gk,1), vgk;
    7157         161 :   long flscal = 0;
    7158             : 
    7159             :   /* gen_0 is ignored, second component assumes Ramanujan-Petersson in
    7160             :    * 1/2-integer weight */
    7161         161 :   vgk = mkvec2(gen_0, mfiscuspidal(mf,F)? gmul2n(gk1,-1): gk1);
    7162         161 :   ta = typ(vtau);
    7163         161 :   if (!is_vec_t(ta)) { flscal = 1; vtau = mkvec(vtau); ta = t_VEC; }
    7164         161 :   lv = lg(vtau);
    7165         161 :   sqN = sqrtr_abs(utor(N, prec));
    7166         161 :   vs = const_vec(lv-1, NULL);
    7167         161 :   vb = const_vec(lv-1, NULL);
    7168         161 :   vL = cgetg(lv, t_VEC);
    7169         161 :   vTAU = cgetg(lv, t_VEC);
    7170         161 :   vczd = cgetg(lv, t_VEC);
    7171         161 :   L0 = mfthetaancreate(NULL, gN, vgk); /* only for thetacost */
    7172         161 :   vE = mfgetembed(F, prec);
    7173         161 :   N0 = 0;
    7174         343 :   for (i = 1; i < lv; i++)
    7175             :   {
    7176         189 :     GEN z = gel(vtau,i), tau, U;
    7177             :     long w, n;
    7178             : 
    7179         189 :     gel(vs,i) = evalcusp(mf, F, z, prec);
    7180         182 :     if (gel(vs,i)) continue;
    7181         154 :     tau = cxredga0N(N, z, &U, &gel(vczd,i), flag);
    7182         154 :     if (!flag) w = 0; else { w = mfZC_width(N, gel(U,1)); tau = gdivgs(tau,w); }
    7183         154 :     gel(vTAU,i) = mulcxmI(gmul(tau, sqN));
    7184         154 :     n = lfunthetacost(L0, real_i(gel(vTAU,i)), 0, bitprec);
    7185         154 :     if (N0 < n) N0 = n;
    7186         154 :     if (flag)
    7187             :     {
    7188          42 :       GEN A, al, v = mfslashexpansion(mf, F, ZM_inv(U,NULL), n, 0, &A, prec);
    7189          42 :       gel(vL,i) = van_embedall(v, vE, gN, vgk);
    7190          42 :       al = gel(A,1);
    7191          42 :       if (!gequal0(al))
    7192           7 :         gel(vb,i) = gexp(gmul(gmul(gmulsg(w,al),PiI2(prec)), tau), prec);
    7193             :     }
    7194             :   }
    7195         154 :   if (!flag)
    7196             :   {
    7197         112 :     van = mfcoefs_i(F, N0, 1);
    7198         112 :     vL = const_vec(lv-1, van_embedall(van, vE, gN, vgk));
    7199             :   }
    7200         336 :   for (i = 1; i < lv; i++)
    7201             :   {
    7202             :     GEN T;
    7203         182 :     if (gel(vs,i)) continue;
    7204         154 :     T = gpow(gel(vczd,i), gneg(gk), prec);
    7205         154 :     if (flag && gel(vb,i)) T = gmul(T, gel(vb,i));
    7206         154 :     gel(vs,i) = lfunthetaall(T, gel(vL,i), gel(vTAU,i), bitprec);
    7207             :   }
    7208         154 :   return flscal? gel(vs,1): vs;
    7209             : }
    7210             : 
    7211             : static long
    7212        1127 : mfistrivial(GEN F)
    7213             : {
    7214        1127 :   switch(mf_get_type(F))
    7215             :   {
    7216           7 :     case t_MF_CONST: return lg(gel(F,2)) == 1;
    7217         259 :     case t_MF_LINEAR: case t_MF_LINEAR_BHN: return gequal0(gel(F,3));
    7218         861 :     default: return 0;
    7219             :   }
    7220             : }
    7221             : 
    7222             : static long
    7223         945 : mf_same_k(GEN mf, GEN f) { return gequal(MF_get_gk(mf), mf_get_gk(f)); }
    7224             : static long
    7225         903 : mf_same_CHI(GEN mf, GEN f)
    7226             : {
    7227         903 :   GEN F1, F2, chi1, chi2, CHI1 = MF_get_CHI(mf), CHI2 = mf_get_CHI(f);
    7228             :   /* are the primitive chars attached to CHI1 and CHI2 equal ? */
    7229         903 :   F1 = znconreyconductor(gel(CHI1,1), gel(CHI1,2), &chi1);
    7230         903 :   if (typ(F1) == t_VEC) F1 = gel(F1,1);
    7231         903 :   F2 = znconreyconductor(gel(CHI2,1), gel(CHI2,2), &chi2);
    7232         903 :   if (typ(F2) == t_VEC) F2 = gel(F2,1);
    7233         903 :   return equalii(F1,F2) && ZV_equal(chi1,chi2);
    7234             : }
    7235             : /* check k and CHI rigorously, but not coefficients nor N */
    7236             : static long
    7237         231 : mfisinspace_i(GEN mf, GEN F)
    7238             : {
    7239         231 :   return mfistrivial(F) || (mf_same_k(mf,F) && mf_same_CHI(mf,F));
    7240             : }
    7241             : static void
    7242           7 : err_space(GEN F)
    7243           7 : { pari_err_DOMAIN("mftobasis", "form", "does not belong to",
    7244           0 :                   strtoGENstr("space"), F); }
    7245             : 
    7246             : static long
    7247         147 : mfcheapeisen(GEN mf)
    7248             : {
    7249         147 :   long k, L, N = MF_get_N(mf);
    7250             :   GEN P;
    7251         147 :   if (N <= 70) return 1;
    7252          84 :   k = itos(gceil(MF_get_gk(mf)));
    7253          84 :   if (odd(k)) k--;
    7254          84 :   switch (k)
    7255             :   {
    7256           0 :     case 2:  L = 190; break;
    7257          14 :     case 4:  L = 162; break;
    7258             :     case 6:
    7259          70 :     case 8:  L = 88; break;
    7260           0 :     case 10: L = 78; break;
    7261           0 :     default: L = 66; break;
    7262             :   }
    7263          84 :   P = gel(myfactoru(N), 1);
    7264          84 :   return P[lg(P)-1] <= L;
    7265             : }
    7266             : 
    7267             : static GEN
    7268         182 : myimag_i(GEN tau)
    7269             : {
    7270         182 :   long tc = typ(tau);
    7271         182 :   if (tc == t_INFINITY || tc == t_INT || tc == t_FRAC)
    7272          28 :     return gen_1;
    7273         154 :   if (tc == t_VEC)
    7274             :   {
    7275             :     long ltau, i;
    7276           7 :     GEN z = cgetg_copy(tau, &ltau);
    7277           7 :     for (i=1; i<ltau; i++) gel(z,i) = myimag_i(gel(tau,i));
    7278           7 :     return z;
    7279             :   }
    7280         147 :   return imag_i(tau);
    7281             : }
    7282             : 
    7283             : static GEN
    7284         147 : mintau(GEN vtau)
    7285             : {
    7286         147 :   if (!is_vec_t(typ(vtau))) return myimag_i(vtau);
    7287           7 :   return (lg(vtau) == 1)? gen_1: vecmin(myimag_i(vtau));
    7288             : }
    7289             : 
    7290             : /* initialization for mfgaexpansion: what does not depend on cusp */
    7291             : static GEN
    7292         938 : mf_eisendec(GEN mf, GEN F, long prec)
    7293             : {
    7294         938 :   GEN B = liftpol_shallow(mfeisensteindec(mf, F)), v = variables_vecsmall(B);
    7295         938 :   GEN Mvecj = obj_check(mf, MF_EISENSPACE);
    7296         938 :   long l = lg(v), i, ord;
    7297         938 :   if (lg(Mvecj) < 5) Mvecj = gel(Mvecj,1);
    7298         938 :   ord = itou(gel(Mvecj,4));
    7299         994 :   for (i = 1; i < l; i++)
    7300         707 :     if (v[i] != 1)
    7301             :     {
    7302             :       GEN d;
    7303             :       long e;
    7304         651 :       B = Q_remove_denom(B, &d);
    7305         651 :       e = gexpo(B);
    7306         651 :       if (e > 0) prec += nbits2prec(e);
    7307         651 :       B = gsubst(B, v[i], rootsof1u_cx(ord, prec));
    7308         651 :       if (d) B = gdiv(B, d);
    7309         651 :       break;
    7310             :     }
    7311         938 :   return B;
    7312             : }
    7313             : 
    7314             : GEN
    7315         161 : mfeval(GEN mf0, GEN F, GEN vtau, long bitprec)
    7316             : {
    7317         161 :   pari_sp av = avma;
    7318         161 :   long flnew = 1;
    7319         161 :   GEN mf = checkMF_i(mf0);
    7320         161 :   if (!mf) pari_err_TYPE("mfeval", mf0);
    7321         161 :   if (!checkmf_i(F)) pari_err_TYPE("mfeval", F);
    7322         161 :   if (!mfisinspace_i(mf, F)) err_space(F);
    7323         161 :   if (!obj_check(mf, MF_EISENSPACE)) flnew = mfcheapeisen(mf);
    7324         161 :   if (flnew && gcmpgs(gmulsg(2*MF_get_N(mf), mintau(vtau)), 1) >= 0) flnew = 0;
    7325         161 :   return gerepilecopy(av, mfeval_i(mf, F, vtau, flnew, bitprec));
    7326             : }
    7327             : 
    7328             : static long
    7329         182 : val(GEN v, long bit)
    7330             : {
    7331         182 :   long c, l = lg(v);
    7332         399 :   for (c = 1; c < l; c++)
    7333         385 :     if (gexpo(gel(v,c)) > -bit) return c-1;
    7334          14 :   return -1;
    7335             : }
    7336             : GEN
    7337         196 : mfcuspval(GEN mf, GEN F, GEN cusp, long bitprec)
    7338             : {
    7339         196 :   pari_sp av = avma;
    7340         196 :   long lvE, w, N, sb, n, A, C, prec = nbits2prec(bitprec);
    7341             :   GEN ga, gk, vE;
    7342         196 :   mf = checkMF(mf);
    7343         196 :   if (!checkmf_i(F)) pari_err_TYPE("mfcuspval",F);
    7344         196 :   N = MF_get_N(mf);
    7345         196 :   cusp_canon(cusp, N, &A, &C);
    7346         196 :   gk = mf_get_gk(F);
    7347         196 :   if (typ(gk) != t_INT)
    7348             :   {
    7349          42 :     GEN FT = mfmultheta(F), mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7350          42 :     GEN r = mfcuspval(mf2, FT, cusp, bitprec);
    7351          42 :     if ((C & 3L) == 2)
    7352             :     {
    7353          14 :       GEN z = sstoQ(1,4);
    7354          14 :       r = gsub(r, typ(r) == t_VEC? const_vec(lg(r)-1, z): z);
    7355             :     }
    7356          42 :     return gerepileupto(av, r);
    7357             :   }
    7358         154 :   vE = mfgetembed(F, prec);
    7359         154 :   lvE = lg(vE);
    7360         154 :   w = mfcuspcanon_width(N, C);
    7361         154 :   sb = w * mfsturmNk(N, itos(gk));
    7362         154 :   ga = cusp2mat(A,C);
    7363         161 :   for (n = 8;; n = minss(sb, n << 1))
    7364           7 :   {
    7365         161 :     GEN R = mfgaexpansion(mf, F, ga, n, prec), res = liftpol_shallow(gel(R,3));
    7366         161 :     GEN v = cgetg(lvE-1, t_VECSMALL);
    7367         161 :     long j, ok = 1;
    7368         161 :     res = RgC_embedall(res, vE);
    7369         343 :     for (j = 1; j < lvE; j++)
    7370             :     {
    7371         182 :       v[j] = val(gel(res,j), bitprec/2);
    7372         182 :       if (v[j] < 0) ok = 0;
    7373             :     }
    7374         161 :     if (ok)
    7375             :     {
    7376         147 :       res = cgetg(lvE, t_VEC);
    7377         147 :       for (j = 1; j < lvE; j++) gel(res,j) = gadd(gel(R,1), sstoQ(v[j], w));
    7378         147 :       return gerepilecopy(av, lvE==2? gel(res,1): res);
    7379             :     }
    7380          14 :     if (n == sb) return lvE==2? mkoo(): const_vec(lvE-1, mkoo()); /* 0 */
    7381             :   }
    7382             : }
    7383             : 
    7384             : long
    7385         203 : mfiscuspidal(GEN mf, GEN F)
    7386             : {
    7387         203 :   pari_sp av = avma;
    7388             :   GEN mf2;
    7389         203 :   if (space_is_cusp(MF_get_space(mf))) return 1;
    7390          77 :   if (typ(mf_get_gk(F)) == t_INT)
    7391             :   {
    7392          49 :     GEN v = mftobasis(mf,F,0), vE = vecslice(v, 1, lg(MF_get_E(mf))-1);
    7393          49 :     return gc_long(av, gequal0(vE));
    7394             :   }
    7395          28 :   if (!gequal0(mfak_i(F, 0))) return 0;
    7396          14 :   mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7397          14 :   return mfiscuspidal(mf2, mfmultheta(F));
    7398             : }
    7399             : 
    7400             : /* F = vector of newforms in mftobasis format */
    7401             : static GEN
    7402          91 : mffrickeeigen_i(GEN mf, GEN F, GEN vE, long prec)
    7403             : {
    7404          91 :   GEN M, Z, L0, gN = MF_get_gN(mf), gk = MF_get_gk(mf);
    7405          91 :   long N0, i, lM, bit = prec2nbits(prec), k = itou(gk);
    7406          91 :   long LIM = 5; /* Sturm bound is enough */
    7407             : 
    7408          91 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7409             : START:
    7410          91 :   N0 = lfunthetacost(L0, gen_1, LIM, bit);
    7411          91 :   M = mfcoefs_mf(mf, N0, 1);
    7412          91 :   lM = lg(F);
    7413          91 :   Z = cgetg(lM, t_VEC);
    7414         259 :   for (i = 1; i < lM; i++)
    7415             :   { /* expansion of D * F[i] */
    7416         168 :     GEN D, z, van = RgM_RgC_mul(M, Q_remove_denom(gel(F,i), &D));
    7417         168 :     GEN L = van_embedall(van, gel(vE,i), gN, gk);
    7418         168 :     long l = lg(L), j, bit_add = D? expi(D): 0;
    7419         168 :     gel(Z,i) = z = cgetg(l, t_VEC);
    7420         511 :     for (j = 1; j < l; j++)
    7421             :     {
    7422             :       GEN v, C, C0;
    7423             :       long m, e;
    7424         476 :       for (m = 0; m <= LIM; m++)
    7425             :       {
    7426         476 :         v = lfuntheta(gmael(L,j,2), gen_1, m, bit);
    7427         476 :         if (gexpo(v) > bit_add - bit/2) break;
    7428             :       }
    7429         343 :       if (m > LIM) { LIM <<= 1; goto START; }
    7430         343 :       C = mulcxpowIs(gdiv(v,conj_i(v)), 2*m - k);
    7431         343 :       C0 = grndtoi(C, &e); if (e < 5-bit_accuracy(precision(C))) C = C0;
    7432         343 :       gel(z,j) = C;
    7433             :     }
    7434             :   }
    7435          91 :   return Z;
    7436             : }
    7437             : static GEN
    7438          70 : mffrickeeigen(GEN mf, GEN vE, long prec)
    7439             : {
    7440          70 :   GEN D = obj_check(mf, MF_FRICKE);
    7441          70 :   if (D) { long p = gprecision(D); if (!p || p >= prec) return D; }
    7442          63 :   D = mffrickeeigen_i(mf, MF_get_newforms(mf), vE, prec);
    7443          63 :   return obj_insert(mf, MF_FRICKE, D);
    7444             : }
    7445             : 
    7446             : /* integral weight, new space for primitive quadratic character CHIP;
    7447             :  * MF = vector of embedded eigenforms coefs on mfbasis, by orbit.
    7448             :  * Assume N > Q > 1 and (Q,f(CHIP)) = 1 */
    7449             : static GEN
    7450          56 : mfatkineigenquad(GEN mf, GEN CHIP, long Q, GEN MF, long bitprec)
    7451             : {
    7452             :   GEN L0, la2, S, F, vP, tau, wtau, Z, va, vb, den, coe, sqrtQ, sqrtN;
    7453          56 :   GEN M, gN, gk = MF_get_gk(mf);
    7454          56 :   long N0, x, yq, i, j, lF, dim, muQ, prec = nbits2prec(bitprec);
    7455          56 :   long N = MF_get_N(mf), k = itos(gk), NQ = N / Q;
    7456             : 
    7457             :   /* Q coprime to FC */
    7458          56 :   F = MF_get_newforms(mf);
    7459          56 :   vP = MF_get_fields(mf);
    7460          56 :   lF = lg(F);
    7461          56 :   Z = cgetg(lF, t_VEC);
    7462          56 :   S = MF_get_S(mf); dim = lg(S) - 1;
    7463          56 :   muQ = mymoebiusu(Q);
    7464          56 :   if (muQ)
    7465             :   {
    7466          42 :     GEN SQ = cgetg(dim+1,t_VEC), Qk = gpow(stoi(Q), sstoQ(k-2, 2), prec);
    7467          42 :     long i, bit2 = bitprec >> 1;
    7468          42 :     for (j = 1; j <= dim; j++) gel(SQ,j) = mfak_i(gel(S,j), Q);
    7469          84 :     for (i = 1; i < lF; i++)
    7470             :     {
    7471          42 :       GEN S = RgV_dotproduct(gel(F,i), SQ), T = gel(vP,i);
    7472             :       long e;
    7473          42 :       if (degpol(T) > 1 && typ(S) != t_POLMOD) S = gmodulo(S, T);
    7474          42 :       S = grndtoi(gdiv(conjvec(S, prec), Qk), &e);
    7475          42 :       if (e > -bit2) pari_err_PREC("mfatkineigenquad");
    7476          42 :       if (muQ == -1) S = gneg(S);
    7477          42 :       gel(Z,i) = S;
    7478             :     }
    7479          42 :     return Z;
    7480             :   }
    7481          14 :   la2 = mfchareval(CHIP, Q); /* 1 or -1 */
    7482          14 :   (void)cbezout(Q, NQ, &x, &yq);
    7483          14 :   sqrtQ = sqrtr_abs(utor(Q,prec));
    7484          14 :   tau = mkcomplex(gadd(sstoQ(-1, NQ), ginv(utoi(1000))),
    7485             :                   divru(sqrtQ, N));
    7486          14 :   den = gaddgs(gmulsg(NQ, tau), 1);
    7487          14 :   wtau = gdiv(gsub(gmulsg(x, tau), sstoQ(yq, Q)), den);
    7488          14 :   coe = gpowgs(gmul(sqrtQ, den), k);
    7489             : 
    7490          14 :   sqrtN = sqrtr_abs(utor(N,prec));
    7491          14 :   tau  = mulcxmI(gmul(tau,  sqrtN));
    7492          14 :   wtau = mulcxmI(gmul(wtau, sqrtN));
    7493          14 :   gN = utoipos(N);
    7494          14 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7495          14 :   N0 = maxss(lfunthetacost(L0,real_i(tau), 0,bitprec),
    7496             :              lfunthetacost(L0,real_i(wtau),0,bitprec));
    7497          14 :   M = mfcoefs_mf(mf, N0, 1);
    7498          14 :   va = cgetg(dim+1, t_VEC);
    7499          14 :   vb = cgetg(dim+1, t_VEC);
    7500         105 :   for (j = 1; j <= dim; j++)
    7501             :   {
    7502          91 :     GEN L, v = vecslice(gel(M,j), 2, N0+1); /* remove a0 */
    7503          91 :     settyp(v, t_VEC); L = mfthetaancreate(v, gN, gk);
    7504          91 :     gel(va,j) = lfuntheta(L, tau,0,bitprec);
    7505          91 :     gel(vb,j) = lfuntheta(L,wtau,0,bitprec);
    7506             :   }
    7507          84 :   for (i = 1; i < lF; i++)
    7508             :   {
    7509          70 :     GEN z, FE = gel(MF,i);
    7510          70 :     long l = lg(FE);
    7511          70 :     z = cgetg(l, t_VEC);
    7512          70 :     for (j = 1; j < l; j++)
    7513             :     {
    7514          70 :       GEN f = gel(FE,j), a = RgV_dotproduct(va,f), b = RgV_dotproduct(vb,f);
    7515          70 :       GEN la = ground( gdiv(b, gmul(a,coe)) );
    7516          70 :       if (!gequal(gsqr(la), la2)) pari_err_PREC("mfatkineigenquad");
    7517          70 :       if (typ(la) == t_INT)
    7518             :       {
    7519          70 :         if (j != 1) pari_err_BUG("mfatkineigenquad");
    7520          70 :         z = const_vec(l-1, la); break;
    7521             :       }
    7522           0 :       gel(z,j) = la;
    7523             :     }
    7524          70 :     gel(Z,i) = z;
    7525             :   }
    7526          14 :   return Z;
    7527             : }
    7528             : 
    7529             : static GEN
    7530          84 : myusqrt(ulong a, long prec)
    7531             : {
    7532          84 :   if (a == 1UL) return gen_1;
    7533          70 :   if (uissquareall(a, &a)) return utoipos(a);
    7534          49 :   return sqrtr_abs(utor(a, prec));
    7535             : }
    7536             : /* Assume mf is a non-trivial new space, rational primitive character CHIP
    7537             :  * and (Q,FC) = 1 */
    7538             : static GEN
    7539          98 : mfatkinmatnewquad(GEN mf, GEN CHIP, long Q, long flag, long PREC)
    7540             : {
    7541          98 :   GEN cM, M, D, MF, den, vE, F = MF_get_newforms(mf);
    7542          98 :   long i, c, e, prec, bitprec, lF = lg(F), N = MF_get_N(mf), k = MF_get_k(mf);
    7543             : 
    7544          98 :   if (Q == 1) return mkvec4(gen_0, matid(MF_get_dim(mf)), gen_1, mf);
    7545          98 :   den = gel(MF_get_Minv(mf), 2);
    7546          98 :   bitprec = expi(den) + 64;
    7547          98 :   if (!flag) bitprec = maxss(bitprec, prec2nbits(PREC));
    7548             : 
    7549             : START:
    7550          98 :   prec = nbits2prec(bitprec);
    7551          98 :   vE = mfeigenembed(mf, prec);
    7552          98 :   M = cgetg(lF, t_VEC);
    7553          98 :   for (i = 1; i < lF; i++) gel(M,i) = RgC_embedall(gel(F,i), gel(vE,i));
    7554          98 :   if (Q != N)
    7555             :   {
    7556          56 :     D = mfatkineigenquad(mf, CHIP, Q, M, bitprec);
    7557          56 :     c = odd(k)? Q: 1;
    7558             :   }
    7559             :   else
    7560             :   {
    7561          42 :     D = mffrickeeigen(mf, vE, DEFAULTPREC);
    7562          42 :     c = mfcharmodulus(CHIP); if (odd(k)) c = -Q/c;
    7563             :   }
    7564          98 :   D = shallowconcat1(D);
    7565          98 :   if (vec_isconst(D)) { MF = diagonal_shallow(D); flag = 0; }
    7566             :   else
    7567             :   {
    7568          63 :     M = shallowconcat1(M);
    7569          63 :     MF = RgM_mul(matmuldiagonal(M,D), ginv(M));
    7570             :   }
    7571          98 :   if (!flag) return mkvec4(gen_0, MF, gen_1, mf);
    7572             : 
    7573          21 :   if (c > 0)
    7574          21 :     cM = myusqrt(c, PREC);
    7575             :   else
    7576             :   {
    7577           0 :     MF = imag_i(MF); c = -c;
    7578           0 :     cM = mkcomplex(gen_0, myusqrt(c,PREC));
    7579             :   }
    7580          21 :   if (c != 1) MF = RgM_Rg_mul(MF, myusqrt(c,prec));
    7581          21 :   MF = grndtoi(RgM_Rg_mul(MF,den), &e);
    7582          21 :   if (e > -32) { bitprec <<= 1; goto START; }
    7583          21 :   MF = RgM_Rg_div(MF, den);
    7584          21 :   if (is_rational_t(typ(cM)) && !isint1(cM))
    7585           0 :   { MF = RgM_Rg_div(MF, cM); cM = gen_1; }
    7586          21 :   return mkvec4(gen_0, MF, cM, mf);
    7587             : }
    7588             : 
    7589             : /* let CHI mod N, Q || N, return \bar{CHI_Q} * CHI_{N/Q} */
    7590             : static GEN
    7591          91 : mfcharAL(GEN CHI, long Q)
    7592             : {
    7593          91 :   GEN G = gel(CHI,1), c = gel(CHI,2), cycc, d, P, E, F;
    7594          91 :   long l = lg(c), N = mfcharmodulus(CHI), i;
    7595          91 :   if (N == Q) return mfcharconj(CHI);
    7596          42 :   if (N == 1) return CHI;
    7597          42 :   CHI = leafcopy(CHI);
    7598          42 :   gel(CHI,2) = d = leafcopy(c);
    7599          42 :   F = znstar_get_faN(G);
    7600          42 :   P = gel(F,1);
    7601          42 :   E = gel(F,2);
    7602          42 :   cycc = znstar_get_conreycyc(G);
    7603          42 :   if (!odd(Q) && equaliu(gel(P,1), 2) && E[1] >= 3)
    7604          14 :     gel(d,2) = Fp_neg(gel(d,2), gel(cycc,2));
    7605          56 :   else for (i = 1; i < l; i++)
    7606          28 :     if (!umodui(Q, gel(P,i))) gel(d,i) = Fp_neg(gel(d,i), gel(cycc,i));
    7607          42 :   return CHI;
    7608             : }
    7609             : static long
    7610         210 : atkin_get_NQ(long N, long Q, const char *f)
    7611             : {
    7612         210 :   long NQ = N / Q;
    7613         210 :   if (N % Q) pari_err_DOMAIN(f,"N % Q","!=",gen_0,utoi(Q));
    7614         210 :   if (ugcd(NQ, Q) > 1) pari_err_DOMAIN(f,"gcd(Q,N/Q)","!=",gen_1,utoi(Q));
    7615         210 :   return NQ;
    7616             : }
    7617             : 
    7618             : /* transform mf to new_NEW if possible */
    7619             : static GEN
    7620        1260 : MF_set_new(GEN mf)
    7621             : {
    7622        1260 :   GEN vMjd, vj, gk = MF_get_gk(mf);
    7623             :   long l, j;
    7624        1260 :   if (MF_get_space(mf) != mf_CUSP
    7625         189 :       || typ(gk) != t_INT || itou(gk) == 1) return mf;
    7626         175 :   vMjd = MFcusp_get_vMjd(mf); l = lg(vMjd);
    7627         175 :   if (l > 1 && gel(vMjd,1)[1] != MF_get_N(mf)) return mf; /* oldspace != 0 */
    7628         168 :   mf = shallowcopy(mf);
    7629         168 :   gel(mf,1) = shallowcopy(gel(mf,1));
    7630         168 :   MF_set_space(mf, mf_NEW);
    7631         168 :   vj = cgetg(l, t_VECSMALL);
    7632         168 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd, j)[2];
    7633         168 :   gel(mf,4) = vj; return mf;
    7634             : }
    7635             : 
    7636             : /* if flag = 1, rationalize, else don't */
    7637             : static GEN
    7638         189 : mfatkininit_i(GEN mf, long Q, long flag, long prec)
    7639             : {
    7640             :   GEN M, B, C, CHI, CHIAL, G, chi, P, z, g, mfB, s, Mindex, Minv;
    7641         189 :   long j, l, lim, ord, FC, NQ, cQ, nk, dk, N = MF_get_N(mf);
    7642             : 
    7643         189 :   B = MF_get_basis(mf); l = lg(B);
    7644         189 :   M = cgetg(l, t_MAT); if (l == 1) return mkvec4(gen_0,M,gen_1,mf);
    7645         189 :   Qtoss(MF_get_gk(mf), &nk,&dk);
    7646         189 :   Q = labs(Q);
    7647         189 :   NQ = atkin_get_NQ(N, Q, "mfatkininit");
    7648         189 :   CHI = MF_get_CHI(mf);
    7649         189 :   CHI = mfchartoprimitive(CHI, &FC);
    7650         189 :   ord = mfcharorder(CHI);
    7651         189 :   mf = MF_set_new(mf);
    7652         189 :   if (MF_get_space(mf) == mf_NEW && ord <= 2 && NQ % FC == 0 && dk == 1)
    7653          98 :     return mfatkinmatnewquad(mf, CHI, Q, flag, prec);
    7654             :   /* now flag != 0 */
    7655          91 :   G   = gel(CHI,1);
    7656          91 :   chi = gel(CHI,2);
    7657          91 :   if (Q == N) { g = mkmat22s(0, -1, N, 0); cQ = NQ; } /* Fricke */
    7658             :   else
    7659             :   {
    7660          28 :     GEN F, gQP = utoi(ugcd(Q, FC));
    7661             :     long t, v;
    7662          28 :     chi = znchardecompose(G, chi, gQP);
    7663          28 :     F = znconreyconductor(G, chi, &chi);
    7664          28 :     G = znstar0(F,1);
    7665          28 :     (void)cbezout(Q, NQ, &t, &v);
    7666          28 :     g = mkmat22s(Q*t, 1, -N*v, Q);
    7667          28 :     cQ = -NQ*v;
    7668             :   }
    7669          91 :   C = s = gen_1;
    7670             :   /* N.B. G,chi are G_Q,chi_Q [primitive] at this point */
    7671          91 :   if (lg(chi) != 1) C = ginv( znchargauss(G, chi, gen_1, prec2nbits(prec)) );
    7672          91 :   if (dk == 1)
    7673          84 :   { if (odd(nk)) s = myusqrt(Q,prec); }
    7674             :   else
    7675             :   {
    7676           7 :     long r = nk >> 1; /* k-1/2 */
    7677           7 :     s = gpow(utoipos(Q), mkfracss(odd(r)? 1: 3, 4), prec);
    7678           7 :     if (odd(cQ))
    7679             :     {
    7680           7 :       long t = r + ((cQ-1) >> 1);
    7681           7 :       s = mkcomplex(s, odd(t)? gneg(s): s);
    7682             :     }
    7683             :   }
    7684          91 :   if (!isint1(s)) C = gmul(C, s);
    7685          91 :   CHIAL = mfcharAL(CHI, Q);
    7686          91 :   if (dk == 2)
    7687           7 :     CHIAL = mfcharmul(CHIAL, induce(gel(CHIAL,1), utoipos(odd(Q) ? Q<<2 : Q)));
    7688          91 :   CHIAL = mfchartoprimitive(CHIAL,NULL);
    7689          91 :   mfB = gequal(CHIAL,CHI)? mf: mfinit_Nndkchi(N,nk,dk,CHIAL,MF_get_space(mf),0);
    7690          91 :   Mindex = MF_get_Mindex(mfB);
    7691          91 :   Minv = MF_get_Minv(mfB);
    7692          91 :   P = z = NULL;
    7693          91 :   if (ord > 2) { P = mfcharpol(CHI); z = rootsof1u_cx(ord, prec); }
    7694          91 :   lim = maxss(mfsturm(mfB), mfsturm(mf)) + 1;
    7695         287 :   for (j = 1; j < l; j++)
    7696             :   {
    7697         196 :     GEN v = mfslashexpansion(mf, gel(B,j), g, lim, 0, NULL, prec+EXTRAPREC);
    7698             :     long junk;
    7699         196 :     if (!isint1(C)) v = RgV_Rg_mul(v, C);
    7700         196 :     v = bestapprnf(v, P, z, prec);
    7701         196 :     v = vecpermute_partial(v, Mindex, &junk);
    7702         196 :     v = Minv_RgC_mul(Minv, v); /* cf mftobasis_i */
    7703         196 :     gel(M, j) = v;
    7704             :   }
    7705          91 :   if (is_rational_t(typ(C)) && !gequal1(C)) { M = gdiv(M, C); C = gen_1; }
    7706          91 :   if (mfB == mf) mfB = gen_0;
    7707          91 :   return mkvec4(mfB, M, C, mf);
    7708             : }
    7709             : GEN
    7710          77 : mfatkininit(GEN mf, long Q, long prec)
    7711             : {
    7712          77 :   pari_sp av = avma;
    7713          77 :   mf = checkMF(mf); return gerepilecopy(av, mfatkininit_i(mf, Q, 1, prec));
    7714             : }
    7715             : static void
    7716          56 : checkmfa(GEN z)
    7717             : {
    7718          56 :   if (typ(z) != t_VEC || lg(z) != 5 || typ(gel(z,2)) != t_MAT
    7719          56 :       || !checkMF_i(gel(z,4))
    7720          56 :       || (!isintzero(gel(z,1)) && !checkMF_i(gel(z,1))))
    7721           0 :     pari_err_TYPE("mfatkin [please apply mfatkininit()]",z);
    7722          56 : }
    7723             : 
    7724             : /* Apply atkin Q to closure F */
    7725             : GEN
    7726          56 : mfatkin(GEN mfa, GEN F)
    7727             : {
    7728          56 :   pari_sp av = avma;
    7729             :   GEN z, mfB, MQ, mf;
    7730          56 :   checkmfa(mfa);
    7731          56 :   mfB= gel(mfa,1);
    7732          56 :   MQ = gel(mfa,2);
    7733          56 :   mf = gel(mfa,4);
    7734          56 :   if (typ(mfB) == t_INT) mfB = mf;
    7735          56 :   z = RgM_RgC_mul(MQ, mftobasis_i(mf,F));
    7736          56 :   return gerepileupto(av, mflinear(mfB, z));
    7737             : }
    7738             : 
    7739             : GEN
    7740          49 : mfatkineigenvalues(GEN mf, long Q, long prec)
    7741             : {
    7742          49 :   pari_sp av = avma;
    7743             :   GEN vF, L, CHI, M, mfatk, C, MQ, vE, mfB;
    7744             :   long N, NQ, l, i;
    7745             : 
    7746          49 :   mf = checkMF(mf); N = MF_get_N(mf);
    7747          49 :   vF = MF_get_newforms(mf); l = lg(vF);
    7748             :   /* N.B. k is integral */
    7749          49 :   if (l == 1) { set_avma(av); return cgetg(1, t_VEC); }
    7750          49 :   L = cgetg(l, t_VEC);
    7751          49 :   if (Q == 1)
    7752             :   {
    7753           7 :     GEN vP = MF_get_fields(mf);
    7754           7 :     for (i = 1; i < l; i++) gel(L,i) = const_vec(degpol(gel(vP,i)), gen_1);
    7755           7 :     return L;
    7756             :   }
    7757          42 :   vE = mfeigenembed(mf,prec);
    7758          42 :   if (Q == N) return gerepileupto(av, mffrickeeigen(mf, vE, prec));
    7759          21 :   Q = labs(Q);
    7760          21 :   NQ = atkin_get_NQ(N, Q, "mfatkineigenvalues"); /* != 1 */
    7761          21 :   mfatk = mfatkininit(mf, Q, prec);
    7762          21 :   mfB= gel(mfatk,1); if (typ(mfB) != t_VEC) mfB = mf;
    7763          21 :   MQ = gel(mfatk,2);
    7764          21 :   C  = gel(mfatk,3);
    7765          21 :   M = row(mfcoefs_mf(mfB,1,1), 2); /* vec of a_1(b_i) for mfbasis functions */
    7766          56 :   for (i = 1; i < l; i++)
    7767             :   {
    7768          35 :     GEN c = RgV_dotproduct(RgM_RgC_mul(MQ,gel(vF,i)), M); /* C * eigen_i */
    7769          35 :     gel(L,i) = Rg_embedall_i(c, gel(vE,i));
    7770             :   }
    7771          21 :   if (!gequal1(C)) L = gdiv(L, C);
    7772          21 :   CHI = MF_get_CHI(mf);
    7773          21 :   if (mfcharorder(CHI) <= 2 && NQ % mfcharconductor(CHI) == 0) L = ground(L);
    7774          21 :   return gerepilecopy(av, L);
    7775             : }
    7776             : 
    7777             : /* expand B_d V, keeping same length */
    7778             : static GEN
    7779        5796 : bdexpand(GEN V, long d)
    7780             : {
    7781             :   GEN W;
    7782             :   long N, n;
    7783        5796 :   if (d == 1) return V;
    7784        2079 :   N = lg(V)-1; W = zerovec(N);
    7785        2079 :   for (n = 0; n <= (N-1)/d; n++) gel(W, n*d+1) = gel(V, n+1);
    7786        2079 :   return W;
    7787             : }
    7788             : /* expand B_d V, increasing length up to lim */
    7789             : static GEN
    7790         287 : bdexpandall(GEN V, long d, long lim)
    7791             : {
    7792             :   GEN W;
    7793             :   long N, n;
    7794         287 :   if (d == 1) return V;
    7795          35 :   N = lg(V)-1; W = zerovec(lim);
    7796          35 :   for (n = 0; n <= N-1 && n*d <= lim; n++) gel(W, n*d+1) = gel(V, n+1);
    7797          35 :   return W;
    7798             : }
    7799             : 
    7800             : static void
    7801        8631 : parse_vecj(GEN T, GEN *E1, GEN *E2)
    7802             : {
    7803        8631 :   if (lg(T)==3) { *E1 = gel(T,1); *E2 = gel(T,2); }
    7804        4613 :   else { *E1 = T; *E2 = NULL; }
    7805        8631 : }
    7806             : 
    7807             : /* g in M_2(Z) ? */
    7808             : static int
    7809        2737 : check_M2Z(GEN g)
    7810        2737 : {  return typ(g) == t_MAT && lg(g) == 3 && lgcols(g) == 3 && RgM_is_ZM(g); }
    7811             : /* g in SL_2(Z) ? */
    7812             : static int
    7813        1666 : check_SL2Z(GEN g) { return check_M2Z(g) && equali1(ZM_det(g)); }
    7814             : 
    7815             : static GEN
    7816        9023 : mfcharcxeval(GEN CHI, long n, long prec)
    7817             : {
    7818        9023 :   ulong ord, N = mfcharmodulus(CHI);
    7819             :   GEN ordg;
    7820        9023 :   if (N == 1) return gen_1;
    7821        3696 :   if (ugcd(N, labs(n)) > 1) return gen_0;
    7822        3696 :   ordg = gmfcharorder(CHI);
    7823        3696 :   ord = itou(ordg);
    7824        3696 :   return rootsof1q_cx(znchareval_i(CHI,n,ordg), ord, prec);
    7825             : }
    7826             : 
    7827             : static GEN
    7828        4795 : RgV_shift(GEN V, GEN gn)
    7829             : {
    7830             :   long i, n, l;
    7831             :   GEN W;
    7832        4795 :   if (typ(gn) != t_INT) pari_err_BUG("RgV_shift [n not integral]");
    7833        4795 :   n = itos(gn);
    7834        4795 :   if (n < 0) pari_err_BUG("RgV_shift [n negative]");
    7835        4795 :   if (!n) return V;
    7836         112 :   W = cgetg_copy(V, &l); if (n > l-1) n = l-1;
    7837         112 :   for (i=1; i <= n; i++) gel(W,i) = gen_0;
    7838         112 :   for (    ; i < l; i++) gel(W,i) = gel(V, i-n);
    7839         112 :   return W;
    7840             : }
    7841             : static GEN
    7842        7427 : hash_eisengacx(hashtable *H, void *E, long w, GEN ga, long n, long prec)
    7843             : {
    7844        7427 :   ulong h = H->hash(E);
    7845        7427 :   hashentry *e = hash_search2(H, E, h);
    7846             :   GEN v;
    7847        7427 :   if (e) v = (GEN)e->val;
    7848             :   else
    7849             :   {
    7850        4956 :     v = mfeisensteingacx((GEN)E, w, ga, n, prec);
    7851        4956 :     hash_insert2(H, E, (void*)v, h);
    7852             :   }
    7853        7427 :   return v;
    7854             : }
    7855             : static GEN
    7856        4795 : vecj_expand(GEN B, hashtable *H, long w, GEN ga, long n, long prec)
    7857             : {
    7858             :   GEN E1, E2, v;
    7859        4795 :   parse_vecj(B, &E1, &E2);
    7860        4795 :   v = hash_eisengacx(H, (void*)E1, w, ga, n, prec);
    7861        4795 :   if (E2)
    7862             :   {
    7863        2576 :     GEN u = hash_eisengacx(H, (void*)E2, w, ga, n, prec);
    7864        2576 :     GEN a = gadd(gel(v,1), gel(u,1));
    7865        2576 :     GEN b = RgV_mul_RgXn(gel(v,2), gel(u,2));
    7866        2576 :     v = mkvec2(a,b);
    7867             :   }
    7868        4795 :   return v;
    7869             : }
    7870             : static GEN
    7871        1001 : shift_M(GEN M, GEN Valpha, long w)
    7872             : {
    7873        1001 :   long i, l = lg(Valpha);
    7874        1001 :   GEN almin = vecmin(Valpha);
    7875        5796 :   for (i = 1; i < l; i++)
    7876             :   {
    7877        4795 :     GEN alpha = gel(Valpha, i), gsh = gmulsg(w, gsub(alpha,almin));
    7878        4795 :     gel(M,i) = RgV_shift(gel(M,i), gsh);
    7879             :   }
    7880        1001 :   return almin;
    7881             : }
    7882             : static GEN mfeisensteinspaceinit(GEN NK);
    7883             : #if 0
    7884             : /* ga in M_2^+(Z)), n >= 0 */
    7885             : static GEN
    7886             : mfgaexpansion_init(GEN mf, GEN ga, long n, long prec)
    7887             : {
    7888             :   GEN M, Mvecj, vecj, almin, Valpha;
    7889             :   long i, w, l, N = MF_get_N(mf), c = itos(gcoeff(ga,2,1));
    7890             :   hashtable *H;
    7891             : 
    7892             :   if (c % N == 0)
    7893             :   { /* ga in G_0(N), trivial case; w = 1 */
    7894             :     GEN chid = mfcharcxeval(MF_get_CHI(mf), itos(gcoeff(ga,2,2)), prec);
    7895             :     return mkvec2(chid, utoi(n));
    7896             :   }
    7897             : 
    7898             :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7899             :   if (lg(Mvecj) < 5) pari_err_IMPL("mfgaexpansion_init in this case");
    7900             :   w = mfcuspcanon_width(N, c);
    7901             :   vecj = gel(Mvecj, 3);
    7902             :   l = lg(vecj);
    7903             :   M = cgetg(l, t_VEC);
    7904             :   Valpha = cgetg(l, t_VEC);
    7905             :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7906             :                      (int(*)(void*,void*))&gidentical, 1);
    7907             :   for (i = 1; i < l; i++)
    7908             :   {
    7909             :     GEN v = vecj_expand(gel(vecj,i), H, w, ga, n, prec);
    7910             :     gel(Valpha,i) = gel(v,1);
    7911             :     gel(M,i) = gel(v,2);
    7912             :   }
    7913             :   almin = shift_M(M, Valpha, w);
    7914             :   return mkvec3(almin, utoi(w), M);
    7915             : }
    7916             : /* half-integer weight not supported; vF = [F,eisendec(F)].
    7917             :  * Minit = mfgaexpansion_init(mf, ga, n, prec) */
    7918             : static GEN
    7919             : mfgaexpansion_with_init(GEN Minit, GEN vF)
    7920             : {
    7921             :   GEN v;
    7922             :   if (lg(Minit) == 3)
    7923             :   { /* ga in G_0(N) */
    7924             :     GEN chid = gel(Minit,1), gn = gel(Minit,2);
    7925             :     v = mfcoefs_i(gel(vF,1), itou(gn), 1);
    7926             :     v = mkvec3(gen_0, gen_1, RgV_Rg_mul(v,chid));
    7927             :   }
    7928             :   else
    7929             :   {
    7930             :     GEN V = RgM_RgC_mul(gel(Minit,3), gel(vF,2));
    7931             :     v = mkvec3(gel(Minit,1), gel(Minit,2), V);
    7932             :   }
    7933             :   return v;
    7934             : }
    7935             : #endif
    7936             : 
    7937             : /* B = mfeisensteindec(F) already embedded, ga in M_2^+(Z)), n >= 0 */
    7938             : static GEN
    7939        1001 : mfgaexpansion_i(GEN mf, GEN B0, GEN ga, long n, long prec)
    7940             : {
    7941        1001 :   GEN M, Mvecj, vecj, almin, Valpha, B, E = NULL;
    7942        1001 :   long i, j, w, nw, l, N = MF_get_N(mf), bit = prec2nbits(prec) / 2;
    7943             :   hashtable *H;
    7944             : 
    7945        1001 :   Mvecj = obj_check(mf, MF_EISENSPACE);
    7946        1001 :   if (lg(Mvecj) < 5) { E = gel(Mvecj, 2); Mvecj = gel(Mvecj, 1); }
    7947        1001 :   vecj = gel(Mvecj, 3);
    7948        1001 :   l = lg(vecj);
    7949        1001 :   B = cgetg(l, t_COL);
    7950        1001 :   M = cgetg(l, t_VEC);
    7951        1001 :   Valpha = cgetg(l, t_VEC);
    7952        1001 :   w = mfZC_width(N, gel(ga,1));
    7953        1001 :   nw = E ? n + w : n;
    7954        1001 :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7955             :                      (int(*)(void*,void*))&gidentical, 1);
    7956        8596 :   for (i = j = 1; i < l; i++)
    7957             :   {
    7958             :     GEN v;
    7959        7595 :     if (gequal0(gel(B0,i))) continue;
    7960        4795 :     v = vecj_expand(gel(vecj,i), H, w, ga, nw, prec);
    7961        4795 :     gel(B,j) = gel(B0,i);
    7962        4795 :     gel(Valpha,j) = gel(v,1);
    7963        4795 :     gel(M,j) = gel(v,2); j++;
    7964             :   }
    7965        1001 :   setlg(Valpha, j);
    7966        1001 :   setlg(B, j);
    7967        1001 :   setlg(M, j); l = j;
    7968        1001 :   if (l == 1) return mkvec3(gen_0, utoi(w), zerovec(n+1));
    7969        1001 :   almin = shift_M(M, Valpha, w);
    7970        1001 :   B = RgM_RgC_mul(M, B); l = lg(B);
    7971      147714 :   for (i = 1; i < l; i++)
    7972      146713 :     if (gexpo(gel(B,i)) < -bit) gel(B,i) = gen_0;
    7973        1001 :   settyp(B, t_VEC);
    7974        1001 :   if (E)
    7975             :   {
    7976             :     GEN v, e;
    7977          56 :     long ell = 0, vB, ve;
    7978         126 :     for (i = 1; i < l; i++)
    7979         126 :       if (!gequal0(gel(B,i))) break;
    7980          56 :     vB = i-1;
    7981          56 :     v = hash_eisengacx(H, (void*)E, w, ga, n + vB, prec);
    7982          56 :     e = gel(v,2); l = lg(e);
    7983          56 :     for (i = 1; i < l; i++)
    7984          56 :       if (!gequal0(gel(e,i))) break;
    7985          56 :     ve = i-1;
    7986          56 :     almin = gsub(almin, gel(v,1));
    7987          56 :     if (gsigne(almin) < 0)
    7988             :     {
    7989           0 :       GEN gell = gceil(gmulsg(-w, almin));
    7990           0 :       ell = itos(gell);
    7991           0 :       almin = gadd(almin, gdivgs(gell, w));
    7992           0 :       if (nw < ell) pari_err_IMPL("alpha < 0 in mfgaexpansion");
    7993             :     }
    7994          56 :     if (ve) { ell += ve; e = vecslice(e, ve+1, l-1); }
    7995          56 :     B = vecslice(B, ell + 1, minss(n + ell + 1, lg(B)-1));
    7996          56 :     B = RgV_div_RgXn(B, e);
    7997             :   }
    7998        1001 :   return mkvec3(almin, utoi(w), B);
    7999             : }
    8000             : 
    8001             : /* Theta multiplier: assume 4 | C, (C,D)=1 */
    8002             : static GEN
    8003         301 : mfthetamultiplier(GEN C, GEN D)
    8004             : {
    8005         301 :   long s = kronecker(C, D);
    8006         301 :   if (Mod4(D) == 1) return s > 0 ? gen_1: gen_m1;
    8007          84 :   return s > 0? powIs(3): gen_I();
    8008             : }
    8009             : /* theta | [*,*;C,D] defined over Q(i) [else over Q] */
    8010             : static int
    8011          56 : mfthetaI(long C, long D) { return odd(C) || (D & 3) == 3; }
    8012             : /* (theta | M) [0..n], assume (C,D) = 1 */
    8013             : static GEN
    8014         301 : mfthetaexpansion(GEN M, long n)
    8015             : {
    8016         301 :   GEN w, s, al, sla, E, V = zerovec(n+1), C = gcoeff(M,2,1), D = gcoeff(M,2,2);
    8017         301 :   long lim, la, f, C4 = Mod4(C);
    8018         301 :   switch (C4)
    8019             :   {
    8020          70 :     case 0: al = gen_0; w = gen_1;
    8021          70 :       s = mfthetamultiplier(C,D);
    8022          70 :       lim = usqrt(n); gel(V, 1) = s;
    8023          70 :       s = gmul2n(s, 1);
    8024          70 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = s;
    8025          70 :       break;
    8026         105 :     case 2: al = sstoQ(1,4); w = gen_1;
    8027         105 :       E = subii(C, shifti(D,1)); /* (E, D) = 1 */
    8028         105 :       s = gmul2n(mfthetamultiplier(E, D), 1);
    8029         105 :       if ((!signe(E) && equalim1(D)) || (signe(E) > 0 && signe(C) < 0))
    8030          14 :         s = gneg(s);
    8031         105 :       lim = (usqrt(n << 2) - 1) >> 1;
    8032         105 :       for (f = 0; f <= lim; f++) gel(V, f*(f+1) + 1) = s;
    8033         105 :       break;
    8034         126 :     default: al = gen_0; w = utoipos(4);
    8035         126 :       la = (-Mod4(D)*C4) & 3L;
    8036         126 :       E = negi(addii(D, mului(la, C)));
    8037         126 :       s = mfthetamultiplier(E, C); /* (E,C) = 1 */
    8038         126 :       if (signe(C) < 0 && signe(E) >= 0) s = gneg(s);
    8039         126 :       s = gsub(s, mulcxI(s));
    8040         126 :       sla = gmul(s, powIs(-la));
    8041         126 :       lim = usqrt(n); gel(V, 1) = gmul2n(s, -1);
    8042         126 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = odd(f) ? sla : s;
    8043         126 :       break;
    8044             :   }
    8045         301 :   return mkvec3(al, w, V);
    8046             : }
    8047             : 
    8048             : /* F 1/2 integral weight */
    8049             : static GEN
    8050         301 : mf2gaexpansion(GEN mf2, GEN F, GEN ga, long n, long prec)
    8051             : {
    8052         301 :   GEN FT = mfmultheta(F), mf = obj_checkbuild(mf2, MF_MF2INIT, &mf2init);
    8053         301 :   GEN res, V1, Tres, V2, al, V, gsh, C = gcoeff(ga,2,1);
    8054         301 :   long w2, N = MF_get_N(mf), w = mfcuspcanon_width(N, umodiu(C,N));
    8055         301 :   long ext = (Mod4(C) != 2)? 0: (w+3) >> 2;
    8056         301 :   long prec2 = prec + nbits2extraprec((long)M_PI/(2*M_LN2)*sqrt(n + ext));
    8057         301 :   res = mfgaexpansion(mf, FT, ga, n + ext, prec2);
    8058         301 :   Tres = mfthetaexpansion(ga, n + ext);
    8059         301 :   V1 = gel(res,3);
    8060         301 :   V2 = gel(Tres,3);
    8061         301 :   al = gsub(gel(res,1), gel(Tres,1));
    8062         301 :   w2 = itos(gel(Tres,2));
    8063         301 :   if (w != itos(gel(res,2)) || w % w2)
    8064           0 :     pari_err_BUG("mf2gaexpansion [incorrect w2 or w]");
    8065         301 :   if (w2 != w) V2 = bdexpand(V2, w/w2);
    8066         301 :   V = RgV_div_RgXn(V1, V2);
    8067         301 :   gsh = gfloor(gmulsg(w, al));
    8068         301 :   if (!gequal0(gsh))
    8069             :   {
    8070          35 :     al = gsub(al, gdivgs(gsh, w));
    8071          35 :     if (gsigne(gsh) > 0)
    8072             :     {
    8073           0 :       V = RgV_shift(V, gsh);
    8074           0 :       V = vecslice(V, 1, n + 1);
    8075             :     }
    8076             :     else
    8077             :     {
    8078          35 :       long sh = -itos(gsh), i;
    8079          35 :       if (sh > ext) pari_err_BUG("mf2gaexpansion [incorrect sh]");
    8080         154 :       for (i = 1; i <= sh; i++)
    8081         119 :         if (!gequal0(gel(V,i))) pari_err_BUG("mf2gaexpansion [sh too large]");
    8082          35 :       V = vecslice(V, sh+1, n + sh+1);
    8083             :     }
    8084             :   }
    8085         301 :   obj_free(mf); return mkvec3(al, stoi(w), gprec_wtrunc(V, prec));
    8086             : }
    8087             : 
    8088             : static GEN
    8089          70 : mfgaexpansionatkin(GEN mf, GEN F, GEN C, GEN D, long Q, long n, long prec)
    8090             : {
    8091          70 :   GEN mfa = mfatkininit_i(mf, Q, 0, prec), MQ = gel(mfa,2);
    8092          70 :   long i, FC, k = MF_get_k(mf);
    8093          70 :   GEN x, v, V, z, s, CHI = mfchartoprimitive(MF_get_CHI(mf), &FC);
    8094             : 
    8095             :   /* V = mfcoefs(F | w_Q, n), can't use mfatkin because MQ non-rational */
    8096          70 :   V = RgM_RgC_mul(mfcoefs_mf(mf,n,1), RgM_RgC_mul(MQ, mftobasis_i(mf,F)));
    8097          70 :   (void)bezout(utoipos(Q), C, &x, &v);
    8098          70 :   s = mfchareval(CHI, (umodiu(x, FC) * umodiu(D, FC)) % FC);
    8099          70 :   s = gdiv(s, gpow(utoipos(Q), sstoQ(k,2), prec));
    8100          70 :   V = RgV_Rg_mul(V, s);
    8101          70 :   z = rootsof1powinit(umodiu(D,Q)*umodiu(v,Q) % Q, Q, prec);
    8102          70 :   for (i = 1; i <= n+1; i++) gel(V,i) = gmul(gel(V,i), rootsof1pow(z, i-1));
    8103          70 :   return mkvec3(gen_0, utoipos(Q), V);
    8104             : }
    8105             : 
    8106             : static long
    8107          70 : inveis_extraprec(long N, GEN ga, GEN Mvecj, long n)
    8108             : {
    8109          70 :   long e, w = mfZC_width(N, gel(ga,1));
    8110          70 :   GEN f, E = gel(Mvecj,2), v = mfeisensteingacx(E, w, ga, n, DEFAULTPREC);
    8111          70 :   v = gel(v,2);
    8112          70 :   f = RgV_to_RgX(v,0); n -= RgX_valrem(f, &f);
    8113          70 :   e = gexpo(RgXn_inv(f, n+1));
    8114          70 :   return (e > 0)? nbits2extraprec(e): 0;
    8115             : }
    8116             : /* allow F of the form [F, mf_eisendec(F)]~ */
    8117             : static GEN
    8118        1659 : mfgaexpansion(GEN mf, GEN F, GEN ga, long n, long prec)
    8119             : {
    8120        1659 :   GEN v, EF = NULL, res, Mvecj, c, d;
    8121             :   long precnew, N;
    8122             : 
    8123        1659 :   if (n < 0) pari_err_DOMAIN("mfgaexpansion", "n", "<", gen_0, stoi(n));
    8124        1659 :   if (typ(F) == t_COL && lg(F) == 3) { EF = gel(F,2); F = gel(F,1); }
    8125        1659 :   if (!checkmf_i(F)) pari_err_TYPE("mfgaexpansion", F);
    8126        1659 :   if (!check_SL2Z(ga)) pari_err_TYPE("mfgaexpansion",ga);
    8127        1659 :   if (typ(mf_get_gk(F)) != t_INT) return mf2gaexpansion(mf, F, ga, n, prec);
    8128        1358 :   c = gcoeff(ga,2,1);
    8129        1358 :   d = gcoeff(ga,2,2);
    8130        1358 :   N = MF_get_N(mf);
    8131        1358 :   if (!umodiu(c, mf_get_N(F)))
    8132             :   { /* trivial case: ga in Gamma_0(N) */
    8133         287 :     long w = mfcuspcanon_width(N, umodiu(c,N));
    8134         287 :     GEN CHI = mf_get_CHI(F);
    8135         287 :     GEN chid = mfcharcxeval(CHI, umodiu(d,mfcharmodulus(CHI)), prec);
    8136         287 :     v = mfcoefs_i(F, n/w, 1); if (!isint1(chid)) v = RgV_Rg_mul(v,chid);
    8137         287 :     return mkvec3(gen_0, stoi(w), bdexpandall(v,w,n+1));
    8138             :   }
    8139        1071 :   mf = MF_set_new(mf);
    8140        1071 :   if (MF_get_space(mf) == mf_NEW)
    8141             :   {
    8142         441 :     long cN = umodiu(c,N), g = ugcd(cN,N), Q = N/g;
    8143         441 :     GEN CHI = MF_get_CHI(mf);
    8144         441 :     if (ugcd(cN, Q)==1 && mfcharorder(CHI) <= 2
    8145         217 :                        && g % mfcharconductor(CHI) == 0
    8146         112 :                        && degpol(mf_get_field(F)) == 1)
    8147          70 :       return mfgaexpansionatkin(mf, F, c, d, Q, n, prec);
    8148             :   }
    8149        1001 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    8150        1001 :   precnew = prec;
    8151        1001 :   if (lg(Mvecj) < 5) precnew += inveis_extraprec(N, ga, Mvecj, n);
    8152        1001 :   if (!EF) EF = mf_eisendec(mf, F, precnew);
    8153        1001 :   res = mfgaexpansion_i(mf, EF, ga, n, precnew);
    8154        1001 :   return precnew == prec ? res : gprec_wtrunc(res, prec);
    8155             : }
    8156             : 
    8157             : /* parity = -1 or +1 */
    8158             : static GEN
    8159         217 : findd(long N, long parity)
    8160             : {
    8161         217 :   GEN L, D = mydivisorsu(N);
    8162         217 :   long i, j, l = lg(D);
    8163         217 :   L = cgetg(l, t_VEC);
    8164        1218 :   for (i = j = 1; i < l; i++)
    8165             :   {
    8166        1001 :     long d = D[i];
    8167        1001 :     if (parity == -1) d = -d;
    8168        1001 :     if (sisfundamental(d)) gel(L,j++) = stoi(d);
    8169             :   }
    8170         217 :   setlg(L,j); return L;
    8171             : }
    8172             : /* does ND contain a divisor of N ? */
    8173             : static int
    8174         413 : seenD(long N, GEN ND)
    8175             : {
    8176         413 :   long j, l = lg(ND);
    8177         427 :   for (j = 1; j < l; j++)
    8178          14 :     if (N % ND[j] == 0) return 1;
    8179         413 :   return 0;
    8180             : }
    8181             : static GEN
    8182          42 : search_levels(GEN vN, const char *f)
    8183             : {
    8184          42 :   switch(typ(vN))
    8185             :   {
    8186           7 :     case t_INT: vN = mkvecsmall(itos(vN)); break;
    8187          35 :     case t_VEC: case t_COL: vN = ZV_to_zv(vN); break;
    8188           0 :     case t_VECSMALL: vN = leafcopy(vN); break;
    8189           0 :     default: pari_err_TYPE(f, vN);
    8190             :   }
    8191          42 :   vecsmall_sort(vN); return vN;
    8192             : }
    8193             : GEN
    8194          14 : mfsearch(GEN NK, GEN V, long space)
    8195             : {
    8196          14 :   pari_sp av = avma;
    8197             :   GEN F, gk, NbyD, vN;
    8198             :   long n, nk, dk, parity, nV, i, lvN;
    8199             : 
    8200          14 :   if (typ(NK) != t_VEC || lg(NK) != 3) pari_err_TYPE("mfsearch", NK);
    8201          14 :   gk = gel(NK,2);
    8202          14 :   if (typ(gmul2n(gk, 1)) != t_INT) pari_err_TYPE("mfsearch [k]", gk);
    8203          14 :   switch(typ(V))
    8204             :   {
    8205          14 :     case t_VEC: V = shallowtrans(V);
    8206          14 :     case t_COL: break;
    8207           0 :     default: pari_err_TYPE("mfsearch [V]", V);
    8208             :   }
    8209          14 :   vN = search_levels(gel(NK,1), "mfsearch [N]");
    8210          14 :   lvN = lg(vN);
    8211             : 
    8212          14 :   Qtoss(gk, &nk,&dk);
    8213          14 :   parity = (dk == 1 && odd(nk)) ? -1 : 1;
    8214          14 :   nV = lg(V)-2;
    8215          14 :   F = cgetg(1, t_VEC);
    8216          14 :   NbyD = const_vec(vN[lvN-1], cgetg(1,t_VECSMALL));
    8217         231 :   for (n = 1; n < lvN; n++)
    8218             :   {
    8219         217 :     long N = vN[n];
    8220             :     GEN L;
    8221         217 :     if (N <= 0 || (dk == 2 && (N & 3))) continue;
    8222         217 :     L = findd(N, parity);
    8223         630 :     for (i = 1; i < lg(L); i++)
    8224             :     {
    8225         413 :       GEN mf, M, CO, gD = gel(L,i);
    8226         413 :       GEN *ND = (GEN*)NbyD + itou(gD); /* points to NbyD[|D|] */
    8227             : 
    8228         413 :       if (seenD(N, *ND)) continue;
    8229         413 :       mf = mfinit_Nndkchi(N, nk, dk, get_mfchar(gD), space, 1);
    8230         413 :       M = mfcoefs_mf(mf, nV, 1);
    8231         413 :       CO = inverseimage(M, V); if (lg(CO) == 1) continue;
    8232             : 
    8233          42 :       F = vec_append(F, mflinear(mf,CO));
    8234          42 :       *ND = vecsmall_append(*ND, N); /* add to NbyD[|D|] */
    8235             :     }
    8236             :   }
    8237          14 :   return gerepilecopy(av, F);
    8238             : }
    8239             : 
    8240             : static GEN
    8241         882 : search_from_split(GEN mf, GEN vap, GEN vlp)
    8242             : {
    8243         882 :   pari_sp av = avma;
    8244         882 :   long lvlp = lg(vlp), j, jv, l1;
    8245         882 :   GEN v, NK, S1, S, M = NULL;
    8246             : 
    8247         882 :   S1 = gel(split_i(mf, 1, 0), 1); /* rational newforms */
    8248         882 :   l1 = lg(S1);
    8249         882 :   if (l1 == 1) return gc_NULL(av);
    8250         448 :   v = cgetg(l1, t_VEC);
    8251         448 :   S = MF_get_S(mf);
    8252         448 :   NK = mf_get_NK(gel(S,1));
    8253         448 :   if (lvlp > 1) M = rowpermute(mfcoefs_mf(mf, vlp[lvlp-1], 1), vlp);
    8254         966 :   for (j = jv = 1; j < l1; j++)
    8255             :   {
    8256         518 :     GEN vF = gel(S1,j);
    8257             :     long t;
    8258         651 :     for (t = lvlp-1; t > 0; t--)
    8259             :     { /* lhs = vlp[j]-th coefficient of eigenform */
    8260         595 :       GEN rhs = gel(vap,t), lhs = RgMrow_RgC_mul(M, vF, t);
    8261         595 :       if (!gequal(lhs, rhs)) break;
    8262             :     }
    8263         518 :     if (!t) gel(v,jv++) = mflinear_i(NK,S,vF);
    8264             :   }
    8265         448 :   if (jv == 1) return gc_NULL(av);
    8266          56 :   setlg(v,jv); return v;
    8267             : }
    8268             : GEN
    8269          28 : mfeigensearch(GEN NK, GEN AP)
    8270             : {
    8271          28 :   pari_sp av = avma;
    8272          28 :   GEN k, vN, vap, vlp, vres = cgetg(1, t_VEC), D;
    8273             :   long n, lvN, i, l, even;
    8274             : 
    8275          28 :   if (!AP) l = 1;
    8276             :   else
    8277             :   {
    8278          28 :     l = lg(AP);
    8279          28 :     if (typ(AP) != t_VEC) pari_err_TYPE("mfeigensearch",AP);
    8280             :   }
    8281          28 :   vap = cgetg(l, t_VEC);
    8282          28 :   vlp = cgetg(l, t_VECSMALL);
    8283          28 :   if (l > 1)
    8284             :   {
    8285          28 :     GEN perm = indexvecsort(AP, mkvecsmall(1));
    8286          77 :     for (i = 1; i < l; i++)
    8287             :     {
    8288          49 :       GEN v = gel(AP,perm[i]), gp, ap;
    8289          49 :       if (typ(v) != t_VEC || lg(v) != 3) pari_err_TYPE("mfeigensearch", AP);
    8290          49 :       gp = gel(v,1);
    8291          49 :       ap = gel(v,2);
    8292          49 :       if (typ(gp) != t_INT || (typ(ap) != t_INT && typ(ap) != t_INTMOD))
    8293           0 :         pari_err_TYPE("mfeigensearch", AP);
    8294          49 :       gel(vap,i) = ap;
    8295          49 :       vlp[i] = itos(gp)+1; if (vlp[i] < 0) pari_err_TYPE("mfeigensearch", AP);
    8296             :     }
    8297             :   }
    8298          28 :   l = lg(NK);
    8299          28 :   if (typ(NK) != t_VEC || l != 3) pari_err_TYPE("mfeigensearch",NK);
    8300          28 :   k = gel(NK,2);
    8301          28 :   vN = search_levels(gel(NK,1), "mfeigensearch [N]");
    8302          28 :   lvN = lg(vN);
    8303          28 :   vecsmall_sort(vlp);
    8304          28 :   even = !mpodd(k);
    8305         966 :   for (n = 1; n < lvN; n++)
    8306             :   {
    8307         938 :     pari_sp av2 = avma;
    8308             :     GEN mf, L;
    8309         938 :     long N = vN[n];
    8310         938 :     if (even) D = gen_1;
    8311             :     else
    8312             :     {
    8313         112 :       long r = (N&3L);
    8314         112 :       if (r == 1 || r == 2) continue;
    8315          56 :       D = stoi( corediscs(-N, NULL) ); /* < 0 */
    8316             :     }
    8317         882 :     mf = mfinit_i(mkvec3(utoipos(N), k, D), mf_NEW);
    8318         882 :     L = search_from_split(mf, vap, vlp);
    8319         882 :     if (L) vres = shallowconcat(vres, L); else set_avma(av2);
    8320             :   }
    8321          28 :   return gerepilecopy(av, vres);
    8322             : }
    8323             : 
    8324             : /* tf_{N,k}(n) */
    8325             : static GEN
    8326     3155873 : mfnewtracecache(long N, long k, long n, cachenew_t *cache)
    8327             : {
    8328     3155873 :   GEN C = NULL, S;
    8329             :   long lcache;
    8330     3155873 :   if (!n) return gen_0;
    8331     3053505 :   S = gel(cache->vnew,N);
    8332     3053505 :   lcache = lg(S);
    8333     3053505 :   if (n < lcache) C = gel(S, n);
    8334     3053505 :   if (C) cache->newHIT++;
    8335     1866921 :   else C = mfnewtrace_i(N,k,n,cache);
    8336     3053505 :   cache->newTOTAL++;
    8337     3053505 :   if (n < lcache) gel(S,n) = C;
    8338     3053505 :   return C;
    8339             : }
    8340             : 
    8341             : static long
    8342        1386 : mfdim_Nkchi(long N, long k, GEN CHI, long space)
    8343             : {
    8344        1386 :   if (k < 0 || badchar(N,k,CHI)) return 0;
    8345        1085 :   if (k == 0)
    8346          35 :     return mfcharistrivial(CHI) && !space_is_cusp(space)? 1: 0;
    8347        1050 :   switch(space)
    8348             :   {
    8349         238 :     case mf_NEW: return mfnewdim(N,k,CHI);
    8350         196 :     case mf_CUSP:return mfcuspdim(N,k,CHI);
    8351         168 :     case mf_OLD: return mfolddim(N,k,CHI);
    8352         217 :     case mf_FULL:return mffulldim(N,k,CHI);
    8353         231 :     case mf_EISEN: return mfeisensteindim(N,k,CHI);
    8354           0 :     default: pari_err_FLAG("mfdim");
    8355             :   }
    8356             :   return 0;/*LCOV_EXCL_LINE*/
    8357             : }
    8358             : static long
    8359        2114 : mfwt1dimsum(long N, long space)
    8360             : {
    8361        2114 :   switch(space)
    8362             :   {
    8363        1050 :     case mf_NEW:  return mfwt1newdimsum(N);
    8364        1057 :     case mf_CUSP: return mfwt1cuspdimsum(N);
    8365           7 :     case mf_OLD:  return mfwt1olddimsum(N);
    8366             :   }
    8367           0 :   pari_err_FLAG("mfdim");
    8368             :   return 0; /*LCOV_EXCL_LINE*/
    8369             : }
    8370             : /* mfdim for k = nk/dk */
    8371             : static long
    8372       44744 : mfdim_Nndkchi(long N, long nk, long dk, GEN CHI, long space)
    8373       43463 : { return (dk == 2)? mf2dim_Nkchi(N, nk >> 1, CHI, space)
    8374       88207 :                   : mfdim_Nkchi(N, nk, CHI, space); }
    8375             : /* FIXME: use direct dim Gamma1(N) formula, don't compute individual spaces */
    8376             : static long
    8377         252 : mfwtkdimsum(long N, long k, long dk, long space)
    8378             : {
    8379         252 :   GEN w = mfchars(N, k, dk, NULL);
    8380         252 :   long i, j, D = 0, l = lg(w);
    8381        1239 :   for (i = j = 1; i < l; i++)
    8382             :   {
    8383         987 :     GEN CHI = gel(w,i);
    8384         987 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8385         987 :     if (d) D += d * myeulerphiu(mfcharorder(CHI));
    8386             :   }
    8387         252 :   return D;
    8388             : }
    8389             : static GEN
    8390         105 : mfwt1dims(long N, GEN vCHI, long space)
    8391             : {
    8392         105 :   GEN D = NULL;
    8393         105 :   switch(space)
    8394             :   {
    8395          56 :     case mf_NEW: D = mfwt1newdimall(N, vCHI); break;
    8396          21 :     case mf_CUSP:D = mfwt1cuspdimall(N, vCHI); break;
    8397          28 :     case mf_OLD: D = mfwt1olddimall(N, vCHI); break;
    8398           0 :     default: pari_err_FLAG("mfdim");
    8399             :   }
    8400         105 :   return D;
    8401             : }
    8402             : static GEN
    8403        2961 : mfwtkdims(long N, long k, long dk, GEN vCHI, long space)
    8404             : {
    8405        2961 :   GEN D, w = mfchars(N, k, dk, vCHI);
    8406        2961 :   long i, j, l = lg(w);
    8407        2961 :   D = cgetg(l, t_VEC);
    8408       46592 :   for (i = j = 1; i < l; i++)
    8409             :   {
    8410       43631 :     GEN CHI = gel(w,i);
    8411       43631 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8412       43631 :     if (vCHI)
    8413         574 :       gel(D, j++) = mkvec2s(d, 0);
    8414       43057 :     else if (d)
    8415        2520 :       gel(D, j++) = fmt_dim(CHI, d, 0);
    8416             :   }
    8417        2961 :   setlg(D,j); return D;
    8418             : }
    8419             : GEN
    8420        5719 : mfdim(GEN NK, long space)
    8421             : {
    8422        5719 :   pari_sp av = avma;
    8423             :   long N, k, dk, joker;
    8424             :   GEN CHI, mf;
    8425        5719 :   if ((mf = checkMF_i(NK))) return utoi(MF_get_dim(mf));
    8426        5586 :   checkNK2(NK, &N, &k, &dk, &CHI, 2);
    8427        5586 :   if (!CHI) joker = 1;
    8428             :   else
    8429        2611 :     switch(typ(CHI))
    8430             :     {
    8431        2373 :       case t_INT: joker = 2; break;
    8432         112 :       case t_COL: joker = 3; break;
    8433         126 :       default: joker = 0; break;
    8434             :     }
    8435        5586 :   if (joker)
    8436             :   {
    8437             :     long d;
    8438             :     GEN D;
    8439        5460 :     if (k < 0) switch(joker)
    8440             :     {
    8441           0 :       case 1: return cgetg(1,t_VEC);
    8442           7 :       case 2: return gen_0;
    8443           0 :       case 3: return mfdim0all(CHI);
    8444             :     }
    8445        5453 :     if (k == 0)
    8446             :     {
    8447          28 :       if (space_is_cusp(space)) switch(joker)
    8448             :       {
    8449           7 :         case 1: return cgetg(1,t_VEC);
    8450           0 :         case 2: return gen_0;
    8451           7 :         case 3: return mfdim0all(CHI);
    8452             :       }
    8453          14 :       switch(joker)
    8454             :       {
    8455             :         long i, l;
    8456           7 :         case 1: retmkvec(fmt_dim(mfchartrivial(),0,0));
    8457           0 :         case 2: return gen_1;
    8458           7 :         case 3: l = lg(CHI); D = cgetg(l,t_VEC);
    8459          35 :                 for (i = 1; i < l; i++)
    8460             :                 {
    8461          28 :                   long t = mfcharistrivial(gel(CHI,i));
    8462          28 :                   gel(D,i) = mkvec2(t? gen_1: gen_0, gen_0);
    8463             :                 }
    8464           7 :                 return D;
    8465             :       }
    8466             :     }
    8467        5425 :     if (dk == 1 && k == 1 && space != mf_EISEN)
    8468         105 :     {
    8469        2219 :       long fix = 0, space0 = space;
    8470        2219 :       if (space == mf_FULL) space = mf_CUSP; /* remove Eisenstein part */
    8471        2219 :       if (joker == 2)
    8472             :       {
    8473        2114 :         d = mfwt1dimsum(N, space);
    8474        2114 :         if (space0 == mf_FULL) d += mfwtkdimsum(N,k,dk,mf_EISEN);/*add it back*/
    8475        2114 :         set_avma(av); return utoi(d);
    8476             :       }
    8477             :       /* must initialize explicitly: trivial spaces for E_k/S_k differ */
    8478         105 :       if (space0 == mf_FULL)
    8479             :       {
    8480           7 :         if (!CHI) fix = 1; /* must remove 0 spaces */
    8481           7 :         CHI = mfchars(N, k, dk, CHI);
    8482             :       }
    8483         105 :       D = mfwt1dims(N, CHI, space);
    8484         105 :       if (space0 == mf_FULL)
    8485             :       {
    8486           7 :         GEN D2 = mfwtkdims(N, k, dk, CHI, mf_EISEN);
    8487           7 :         D = merge_dims(D, D2, fix? CHI: NULL);
    8488             :       }
    8489             :     }
    8490             :     else
    8491             :     {
    8492        3206 :       if (joker==2) { d = mfwtkdimsum(N,k,dk,space); set_avma(av); return utoi(d); }
    8493        2954 :       D = mfwtkdims(N, k, dk, CHI, space);
    8494             :     }
    8495        3059 :     if (!CHI) return gerepileupto(av, vecsort(D, mkvecsmall(1)));
    8496         105 :     return gerepilecopy(av, D);
    8497             :   }
    8498         126 :   return utoi( mfdim_Nndkchi(N, k, dk, CHI, space) );
    8499             : }
    8500             : 
    8501             : GEN
    8502         315 : mfbasis(GEN NK, long space)
    8503             : {
    8504         315 :   pari_sp av = avma;
    8505             :   long N, k, dk;
    8506             :   GEN mf, CHI;
    8507         315 :   if ((mf = checkMF_i(NK))) return concat(gel(mf,2), gel(mf,3));
    8508           7 :   checkNK2(NK, &N, &k, &dk, &CHI, 0);
    8509           7 :   if (dk == 2) return gerepilecopy(av, mf2basis(N, k>>1, CHI, NULL, space));
    8510           7 :   mf = mfinit_Nkchi(N, k, CHI, space, 1);
    8511           7 :   return gerepilecopy(av, MF_get_basis(mf));
    8512             : }
    8513             : 
    8514             : static GEN
    8515          49 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
    8516          49 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
    8517             : /* r / x + O(1) */
    8518             : static GEN
    8519          49 : simple_pole(GEN r)
    8520             : {
    8521          49 :   GEN S = deg1ser_shallow(gen_0, r, 0, 1);
    8522          49 :   setvalp(S, -1); return S;
    8523             : }
    8524             : 
    8525             : /* F form, E embedding; mfa = mfatkininit or root number (eigenform case) */
    8526             : static GEN
    8527         154 : mflfuncreate(GEN mfa, GEN F, GEN E, GEN N, GEN gk)
    8528             : {
    8529         154 :   GEN LF = cgetg(8,t_VEC), polar = cgetg(1,t_COL), eps;
    8530         154 :   long k = itou(gk);
    8531         154 :   gel(LF,1) = lfuntag(t_LFUN_MFCLOS, mkvec3(F,E,gen_1));
    8532         154 :   if (typ(mfa) != t_VEC)
    8533          98 :     eps = mfa; /* cuspidal eigenform: root number; no poles */
    8534             :   else
    8535             :   { /* mfatkininit */
    8536          56 :     GEN a0, b0, vF, vG, G = NULL;
    8537          56 :     GEN M = gel(mfa,2), C = gel(mfa,3), mf = gel(mfa,4);
    8538          56 :     M = gdiv(mfmatembed(E, M), C);
    8539          56 :     vF = mfvecembed(E, mftobasis_i(mf, F));
    8540          56 :     vG = RgM_RgC_mul(M, vF);
    8541          56 :     if (gequal(vF,vG)) eps = gen_1;
    8542          42 :     else if (gequal(vF,gneg(vG))) eps = gen_m1;
    8543             :     else
    8544             :     { /* not self-dual */
    8545          42 :       eps = NULL;
    8546          42 :       G = mfatkin(mfa, F);
    8547          42 :       gel(LF,2) = lfuntag(t_LFUN_MFCLOS, mkvec3(G,E,ginv(C)));
    8548          42 :       gel(LF,6) = powIs(k);
    8549             :     }
    8550             :     /* polar part */
    8551          56 :     a0 = mfembed(E, mfcoef(F,0));
    8552          56 :     b0 = eps? gmul(eps,a0): gdiv(mfembed(E, mfcoef(G,0)), C);
    8553          56 :     if (!gequal0(b0))
    8554             :     {
    8555          28 :       b0 = mulcxpowIs(gmul2n(b0,1), k);
    8556          28 :       polar = vec_append(polar, mkvec2(gk, simple_pole(b0)));
    8557             :     }
    8558          56 :     if (!gequal0(a0))
    8559             :     {
    8560          21 :       a0 = gneg(gmul2n(a0,1));
    8561          21 :       polar = vec_append(polar, mkvec2(gen_0, simple_pole(a0)));
    8562             :     }
    8563             :   }
    8564         154 :   if (eps) /* self-dual */
    8565             :   {
    8566         112 :     gel(LF,2) = mfcharorder(mf_get_CHI(F)) <= 2? gen_0: gen_1;
    8567         112 :     gel(LF,6) = mulcxpowIs(eps,k);
    8568             :   }
    8569         154 :   gel(LF,3) = mkvec2(gen_0, gen_1);
    8570         154 :   gel(LF,4) = gk;
    8571         154 :   gel(LF,5) = N;
    8572         154 :   if (lg(polar) == 1) setlg(LF,7); else gel(LF,7) = polar;
    8573         154 :   return LF;
    8574             : }
    8575             : static GEN
    8576         126 : mflfuncreateall(long sd, GEN mfa, GEN F, GEN vE, GEN gN, GEN gk)
    8577             : {
    8578         126 :   long i, l = lg(vE);
    8579         126 :   GEN L = cgetg(l, t_VEC);
    8580         280 :   for (i = 1; i < l; i++)
    8581         154 :     gel(L,i) = mflfuncreate(sd? gel(mfa,i): mfa, F, gel(vE,i), gN, gk);
    8582         126 :   return L;
    8583             : }
    8584             : GEN
    8585          77 : lfunmf(GEN mf, GEN F, long bitprec)
    8586             : {
    8587          77 :   pari_sp av = avma;
    8588          77 :   long i, l, prec = nbits2prec(bitprec);
    8589             :   GEN L, gk, gN;
    8590          77 :   mf = checkMF(mf);
    8591          77 :   gk = MF_get_gk(mf);
    8592          77 :   gN = MF_get_gN(mf);
    8593          77 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
    8594          77 :   if (F)
    8595             :   {
    8596             :     GEN v;
    8597          70 :     long s = MF_get_space(mf);
    8598          70 :     if (!checkmf_i(F)) pari_err_TYPE("lfunmf", F);
    8599          70 :     if (!mfisinspace_i(mf, F)) err_space(F);
    8600          70 :     L = NULL;
    8601          70 :     if ((s == mf_NEW || s == mf_CUSP || s == mf_FULL)
    8602          56 :         && gequal(mfcoefs_i(F,1,1), mkvec2(gen_0,gen_1)))
    8603             :     { /* check if eigenform */
    8604          35 :       GEN vP, vF, b = mftobasis_i(mf, F);
    8605          35 :       long lF, d = degpol(mf_get_field(F));
    8606          35 :       v = mfsplit(mf, d, 0);
    8607          35 :       vF = gel(v,1);
    8608          35 :       vP = gel(v,2); lF = lg(vF);
    8609          35 :       for (i = 1; i < lF; i++)
    8610          28 :         if (degpol(gel(vP,i)) == d && gequal(gel(vF,i), b))
    8611             :         {
    8612          28 :           GEN vE = mfgetembed(F, prec);
    8613          28 :           GEN Z = mffrickeeigen_i(mf, mkvec(b), mkvec(vE), prec);
    8614          28 :           L = mflfuncreateall(1, gel(Z,1), F, vE, gN, gk);
    8615          28 :           break;
    8616             :         }
    8617             :     }
    8618          70 :     if (!L)
    8619             :     { /* not an eigenform: costly general case */
    8620          42 :       GEN mfa = mfatkininit_i(mf, itou(gN), 1, prec);
    8621          42 :       L = mflfuncreateall(0,mfa, F, mfgetembed(F,prec), gN, gk);
    8622             :     }
    8623          70 :     if (lg(L) == 2) L = gel(L,1);
    8624             :   }
    8625             :   else
    8626             :   {
    8627           7 :     GEN M = mfeigenbasis(mf), vE = mfeigenembed(mf, prec);
    8628           7 :     GEN v = mffrickeeigen(mf, vE, prec);
    8629           7 :     l = lg(vE); L = cgetg(l, t_VEC);
    8630          63 :     for (i = 1; i < l; i++)
    8631          56 :       gel(L,i) = mflfuncreateall(1,gel(v,i), gel(M,i), gel(vE,i), gN, gk);
    8632             :   }
    8633          77 :   return gerepilecopy(av, L);
    8634             : }
    8635             : 
    8636             : GEN
    8637          28 : mffromell(GEN E)
    8638             : {
    8639          28 :   pari_sp av = avma;
    8640             :   GEN mf, F, z, v, S;
    8641             :   long N, i, l;
    8642             : 
    8643          28 :   checkell(E);
    8644          28 :   if (ell_get_type(E) != t_ELL_Q) pari_err_TYPE("mfffromell [E not over Q]", E);
    8645          28 :   N = itos(ellQ_get_N(E));
    8646          28 :   mf = mfinit_i(mkvec2(utoi(N), gen_2), mf_NEW);
    8647          28 :   v = split_i(mf, 1, 0);
    8648          28 :   S = gel(v,1); l = lg(S); /* rational newforms */
    8649          28 :   F = tag(t_MF_ELL, mkNK(N,2,mfchartrivial()), E);
    8650          28 :   z = mftobasis_i(mf, F);
    8651          28 :   for(i = 1; i < l; i++)
    8652          28 :     if (gequal(z, gel(S,i))) break;
    8653          28 :   if (i == l) pari_err_BUG("mffromell [E is not modular]");
    8654          28 :   return gerepilecopy(av, mkvec3(mf, F, z));
    8655             : }
    8656             : 
    8657             : /* returns -1 if not, degree otherwise */
    8658             : long
    8659          98 : polishomogeneous(GEN P)
    8660             : {
    8661             :   long i, D, l;
    8662          98 :   if (typ(P) != t_POL) return 0;
    8663          49 :   D = -1; l = lg(P);
    8664         231 :   for (i = 2; i < l; i++)
    8665             :   {
    8666         182 :     GEN c = gel(P,i);
    8667             :     long d;
    8668         182 :     if (gequal0(c)) continue;
    8669          84 :     d = polishomogeneous(c);
    8670          84 :     if (d < 0) return -1;
    8671          84 :     if (D < 0) D = d + i-2; else if (D != d + i-2) return -1;
    8672             :   }
    8673          49 :   return D;
    8674             : }
    8675             : 
    8676             : /* P a t_POL, 1 if spherical, 0 otherwise */
    8677             : static int
    8678          14 : RgX_isspherical(GEN Qi, GEN P)
    8679             : {
    8680          14 :   pari_sp av = avma;
    8681             :   GEN va, S;
    8682             :   long lva, i, j;
    8683          14 :   if (degpol(P) <= 1) return 1;
    8684          14 :   va = variables_vecsmall(P); lva = lg(va);
    8685          14 :   if (lva > lg(Qi)) pari_err(e_MISC, "too many variables in mffromqf");
    8686          14 :   S = gen_0;
    8687          42 :   for (j = 1; j < lva; j++)
    8688             :   {
    8689          28 :     GEN col = gel(Qi, j), Pj = deriv(P, va[j]);
    8690          70 :     for (i = 1; i <= j; i++)
    8691             :     {
    8692          42 :       GEN coe = gel(col, i);
    8693          42 :       if (i != j) coe = gmul2n(coe, 1);
    8694          42 :       if (!gequal0(coe)) S = gadd(S, gmul(coe, deriv(Pj, va[i])));
    8695             :     }
    8696             :   }
    8697          14 :   return gc_bool(av, gequal0(S));
    8698             : }
    8699             : 
    8700             : static GEN
    8701          35 : c_QFsimple_i(long n, GEN Q, GEN P)
    8702             : {
    8703          35 :   GEN V, v = qfrep0(Q, utoi(n), 1);
    8704          35 :   long i, l = lg(v);
    8705          35 :   V = cgetg(l+1, t_VEC);
    8706          63 :   if (!P || equali1(P))
    8707             :   {
    8708          28 :     gel(V,1) = gen_1;
    8709          28 :     for (i = 2; i <= l; i++) gel(V,i) = utoi(v[i-1] << 1);
    8710             :   }
    8711             :   else
    8712             :   {
    8713           7 :     gel(V,1) = gcopy(P);
    8714           7 :     for (i = 2; i <= l; i++) gel(V,i) = gmulgs(P, v[i-1] << 1);
    8715             :   }
    8716          35 :   return V;
    8717             : }
    8718             : 
    8719             : /* v a t_VECSMALL of variable numbers, lg(r) >= lg(v), r is a vector of
    8720             :  * scalars [not involving any variable in v] */
    8721             : static GEN
    8722          28 : gsubstvec_i(GEN e, GEN v, GEN r)
    8723             : {
    8724          28 :   long i, l = lg(v);
    8725          28 :   for(i = 1; i < l; i++) e = gsubst(e, v[i], gel(r,i));
    8726          28 :   return e;
    8727             : }
    8728             : static GEN
    8729          42 : c_QF_i(long n, GEN Q, GEN P)
    8730             : {
    8731          42 :   pari_sp av = avma;
    8732             :   GEN V, v, va;
    8733             :   long i, l;
    8734          42 :   if (!P || typ(P) != t_POL) return gerepileupto(av, c_QFsimple_i(n, Q, P));
    8735           7 :   v = gel(minim(Q, utoi(2*n), NULL), 3);
    8736           7 :   va = variables_vecsmall(P);
    8737           7 :   V = zerovec(n + 1); l = lg(v);
    8738          35 :   for (i = 1; i < l; i++)
    8739             :   {
    8740          28 :     pari_sp av = avma;
    8741          28 :     GEN X = gel(v,i);
    8742          28 :     long c = (itos(qfeval(Q, X)) >> 1) + 1;
    8743          28 :     gel(V, c) = gerepileupto(av, gadd(gel(V, c), gsubstvec_i(P, va, X)));
    8744             :   }
    8745           7 :   return gmul2n(V, 1);
    8746             : }
    8747             : 
    8748             : GEN
    8749          49 : mffromqf(GEN Q, GEN P)
    8750             : {
    8751          49 :   pari_sp av = avma;
    8752             :   GEN G, Qi, F, D, N, mf, v, gk, chi;
    8753             :   long m, d, space;
    8754          49 :   if (typ(Q) != t_MAT) pari_err_TYPE("mffromqf", Q);
    8755          49 :   if (!RgM_is_ZM(Q) || !qfiseven(Q))
    8756           0 :     pari_err_TYPE("mffromqf [not integral or even]", Q);
    8757          49 :   m = lg(Q)-1;
    8758          49 :   Qi = ZM_inv(Q, &N);
    8759          49 :   if (!qfiseven(Qi)) N = shifti(N, 1);
    8760          49 :   d = 0;
    8761          49 :   if (!P || gequal1(P)) P = NULL;
    8762             :   else
    8763             :   {
    8764          21 :     P = simplify_shallow(P);
    8765          21 :     if (typ(P) == t_POL)
    8766             :     {
    8767          14 :       d = polishomogeneous(P);
    8768          14 :       if (d < 0) pari_err_TYPE("mffromqf [not homogeneous t_POL]", P);
    8769          14 :       if (!RgX_isspherical(Qi, P))
    8770           0 :         pari_err_TYPE("mffromqf [not a spherical t_POL]", P);
    8771             :     }
    8772             :   }
    8773          49 :   gk = sstoQ(m + 2*d, 2);
    8774          49 :   D = ZM_det(Q);
    8775          49 :   if (!odd(m)) { if ((m & 3) == 2) D = negi(D); } else D = shifti(D, 1);
    8776          49 :   space = d > 0 ? mf_CUSP : mf_FULL;
    8777          49 :   G = znstar0(N,1);
    8778          49 :   chi = mkvec2(G, znchar_quad(G,D));
    8779          49 :   mf = mfinit(mkvec3(N, gk, chi), space);
    8780          49 :   if (odd(d))
    8781             :   {
    8782           7 :     F = mftrivial();
    8783           7 :     v = zerocol(MF_get_dim(mf));
    8784             :   }
    8785             :   else
    8786             :   {
    8787          42 :     F = c_QF_i(mfsturm(mf), Q, P);
    8788          42 :     v = mftobasis_i(mf, F);
    8789          42 :     F = mflinear(mf, v);
    8790             :   }
    8791          49 :   return gerepilecopy(av, mkvec3(mf, F, v));
    8792             : }
    8793             : 
    8794             : /***********************************************************************/
    8795             : /*                          Eisenstein Series                          */
    8796             : /***********************************************************************/
    8797             : /* \sigma_{k-1}(\chi,n) */
    8798             : static GEN
    8799       22169 : sigchi(long k, GEN CHI, long n)
    8800             : {
    8801       22169 :   pari_sp av = avma;
    8802       22169 :   GEN S = gen_1, D = mydivisorsu(u_ppo(n,mfcharmodulus(CHI)));
    8803       22169 :   long i, l = lg(D), ord = mfcharorder(CHI), vt = varn(mfcharpol(CHI));
    8804       77980 :   for (i = 2; i < l; i++) /* skip D[1] = 1 */
    8805             :   {
    8806       55811 :     long d = D[i], a = mfcharevalord(CHI, d, ord);
    8807       55811 :     S = gadd(S, Qab_Czeta(a, ord, powuu(d, k-1), vt));
    8808             :   }
    8809       22169 :   return gerepileupto(av,S);
    8810             : }
    8811             : 
    8812             : /* write n = n0*n1*n2, (n0,N1*N2) = 1, n1 | N1^oo, n2 | N2^oo;
    8813             :  * return NULL if (n,N1,N2) > 1, else return factoru(n0) */
    8814             : static GEN
    8815      279713 : sigchi2_dec(long n, long N1, long N2, long *pn1, long *pn2)
    8816             : {
    8817      279713 :   GEN P0, E0, P, E, fa = myfactoru(n);
    8818             :   long i, j, l;
    8819      279713 :   *pn1 = 1;
    8820      279713 :   *pn2 = 1;
    8821      279713 :   if (N1 == 1 && N2 == 1) return fa;
    8822      265587 :   P = gel(fa,1); l = lg(P);
    8823      265587 :   E = gel(fa,2);
    8824      265587 :   P0 = cgetg(l, t_VECSMALL);
    8825      265587 :   E0 = cgetg(l, t_VECSMALL);
    8826      611394 :   for (i = j = 1; i < l; i++)
    8827             :   {
    8828      369019 :     long p = P[i], e = E[i];
    8829      369019 :     if (N1 % p == 0)
    8830             :     {
    8831       38815 :       if (N2 % p == 0) return NULL;
    8832       15603 :       *pn1 *= upowuu(p,e);
    8833             :     }
    8834      330204 :     else if (N2 % p == 0)
    8835       61208 :       *pn2 *= upowuu(p,e);
    8836      268996 :     else { P0[j] = p; E0[j] = e; j++; }
    8837             :   }
    8838      242375 :   setlg(P0, j);
    8839      242375 :   setlg(E0, j); return mkvec2(P0,E0);
    8840             : }
    8841             : 
    8842             : /* sigma_{k-1}(\chi_1,\chi_2,n), ord multiple of lcm(ord(CHI1),ord(CHI2)) */
    8843             : static GEN
    8844      221879 : sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord)
    8845             : {
    8846      221879 :   pari_sp av = avma;
    8847      221879 :   GEN S = gen_0, D;
    8848      221879 :   long i, l, n1, n2, vt, N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    8849      221879 :   D = sigchi2_dec(n, N1, N2, &n1, &n2); if (!D) { set_avma(av); return S; }
    8850      203539 :   D = divisorsu_fact(D); l = lg(D);
    8851      203539 :   vt = varn(mfcharpol(CHI1));
    8852      802865 :   for (i = 1; i < l; i++)
    8853             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8854      599326 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1; (n/d,N2) = 1 */
    8855      599326 :     a = mfcharevalord(CHI1, d, ord) + mfcharevalord(CHI2, nd, ord);
    8856      599326 :     if (a >= ord) a -= ord;
    8857      599326 :     S = gadd(S, Qab_Czeta(a, ord, powuu(d, k-1), vt));
    8858             :   }
    8859      203539 :   return gerepileupto(av, S);
    8860             : }
    8861             : 
    8862             : /**************************************************************************/
    8863             : /**           Dirichlet characters with precomputed values               **/
    8864             : /**************************************************************************/
    8865             : /* CHI mfchar */
    8866             : static GEN
    8867       13090 : mfcharcxinit(GEN CHI, long prec)
    8868             : {
    8869       13090 :   GEN G = gel(CHI,1), chi = gel(CHI,2), z, V;
    8870       13090 :   GEN v = ncharvecexpo(G, znconrey_normalized(G,chi));
    8871       13090 :   long n, l = lg(v), o = mfcharorder(CHI);
    8872       13090 :   V = cgetg(l, t_VEC);
    8873       13090 :   z = grootsof1(o, prec); /* Mod(t, Phi_o(t)) -> e(1/o) */
    8874       13090 :   for (n = 1; n < l; n++) gel(V,n) = v[n] < 0? gen_0: gel(z, v[n]+1);
    8875       13090 :   return mkvecn(6, G, chi, gmfcharorder(CHI), v, V, mfcharpol(CHI));
    8876             : }
    8877             : /* v a "CHIvec" */
    8878             : static long
    8879    24014445 : CHIvec_N(GEN v) { return itou(znstar_get_N(gel(v,1))); }
    8880             : static GEN
    8881       14252 : CHIvec_CHI(GEN v)
    8882       14252 : { return mkvec4(gel(v,1), gel(v,2), gel(v,3), gel(v,6)); }
    8883             : /* character order */
    8884             : static long
    8885       38045 : CHIvec_ord(GEN v) { return itou(gel(v,3)); }
    8886             : /* character exponents, i.e. t such that chi(n) = e(t) */
    8887             : static GEN
    8888      434945 : CHIvec_expo(GEN v) { return gel(v,4); }
    8889             : /* character values chi(n) */
    8890             : static GEN
    8891    23359357 : CHIvec_val(GEN v) { return gel(v,5); }
    8892             : /* CHI(n) */
    8893             : static GEN
    8894    23350096 : mychareval(GEN v, long n)
    8895             : {
    8896    23350096 :   long N = CHIvec_N(v), ind = n%N;
    8897    23350096 :   if (ind <= 0) ind += N;
    8898    23350096 :   return gel(CHIvec_val(v), ind);
    8899             : }
    8900             : /* return c such that CHI(n) = e(c / ordz) or -1 if (n,N) > 1 */
    8901             : static long
    8902      434945 : mycharexpo(GEN v, long n)
    8903             : {
    8904      434945 :   long N = CHIvec_N(v), ind = n%N;
    8905      434945 :   if (ind <= 0) ind += N;
    8906      434945 :   return CHIvec_expo(v)[ind];
    8907             : }
    8908             : /* faster than mfcharparity */
    8909             : static long
    8910       53613 : CHIvec_parity(GEN v) { return mycharexpo(v,-1) ? -1: 1; }
    8911             : /**************************************************************************/
    8912             : 
    8913             : static ulong
    8914       57834 : sigchi2_Fl(long k, GEN CHI1vec, GEN CHI2vec, long n, GEN vz, ulong p)
    8915             : {
    8916       57834 :   pari_sp av = avma;
    8917       57834 :   long ordz = lg(vz)-2, i, l, n1, n2;
    8918       57834 :   ulong S = 0;
    8919       57834 :   GEN D = sigchi2_dec(n, CHIvec_N(CHI1vec), CHIvec_N(CHI2vec), &n1, &n2);
    8920       57834 :   if (!D) return gc_ulong(av,S);
    8921       52962 :   D = divisorsu_fact(D);
    8922       52962 :   l = lg(D);
    8923      181916 :   for (i = 1; i < l; i++)
    8924             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8925      128954 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1, (n/d,N2)=1 */
    8926      128954 :     a = mycharexpo(CHI2vec, nd) + mycharexpo(CHI1vec, d);
    8927      128954 :     if (a >= ordz) a -= ordz;
    8928      128954 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, Fl_powu(d,k-1,p), p), p);
    8929             :   }
    8930       52962 :   return gc_ulong(av,S);
    8931             : }
    8932             : 
    8933             : /**********************************************************************/
    8934             : /* Fourier expansions of Eisenstein series                            */
    8935             : /**********************************************************************/
    8936             : /* L(CHI,0) / 2, order(CHI) | ord != 0 */
    8937             : static GEN
    8938        2072 : charLFwt1(GEN CHI, long ord)
    8939             : {
    8940             :   GEN S;
    8941        2072 :   long r, vt, m = mfcharmodulus(CHI);
    8942             : 
    8943        2072 :   if (m == 1) return mkfrac(gen_m1,stoi(4));
    8944        2072 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8945       64120 :   for (r = 1; r < m; r++)
    8946             :   { /* S += r*chi(r) */
    8947             :     long a;
    8948       62048 :     if (ugcd(m,r) != 1) continue;
    8949       50008 :     a = mfcharevalord(CHI,r,ord);
    8950       50008 :     S = gadd(S, Qab_Czeta(a, ord, utoi(r), vt));
    8951             :   }
    8952        2072 :   return gdivgs(S, -2*m);
    8953             : }
    8954             : /* L(CHI,0) / 2, mod p */
    8955             : static ulong
    8956        1939 : charLFwt1_Fl(GEN CHIvec, GEN vz, ulong p)
    8957             : {
    8958        1939 :   long r, m = CHIvec_N(CHIvec);
    8959             :   ulong S;
    8960        1939 :   if (m == 1) return Rg_to_Fl(mkfrac(gen_m1,stoi(4)), p);
    8961        1939 :   S = 0;
    8962       95683 :   for (r = 1; r < m; r++)
    8963             :   { /* S += r*chi(r) */
    8964       93744 :     long a = mycharexpo(CHIvec,r);
    8965       93744 :     if (a < 0) continue;
    8966       91448 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, r, p), p);
    8967             :   }
    8968        1939 :   return Fl_div(Fl_neg(S,p), 2*m, p);
    8969             : }
    8970             : /* L(CHI,1-k) / 2, order(CHI) | ord != 0 */
    8971             : static GEN
    8972        1470 : charLFwtk(long k, GEN CHI, long ord)
    8973             : {
    8974             :   GEN S, P, dS;
    8975             :   long r, m, vt;
    8976             : 
    8977        1470 :   if (k == 1) return charLFwt1(CHI, ord);
    8978        1470 :   m = mfcharmodulus(CHI);
    8979        1470 :   if (m == 1) return gdivgs(bernfrac(k),-2*k);
    8980         868 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8981         868 :   P = ZX_rescale(Q_remove_denom(bernpol(k,0), &dS), utoi(m));
    8982         868 :   dS = mul_denom(dS, stoi(-2*m*k));
    8983       12418 :   for (r = 1; r < m; r++)
    8984             :   { /* S += P(r)*chi(r) */
    8985             :     long a;
    8986       11550 :     if (ugcd(r,m) != 1) continue;
    8987        9772 :     a = mfcharevalord(CHI,r,ord);
    8988        9772 :     S = gadd(S, Qab_Czeta(a, ord, poleval(P, utoi(r)), vt));
    8989             :   }
    8990         868 :   return gdiv(S, dS);
    8991             : }
    8992             : /* L(CHI,1-k) / 2, mod p */
    8993             : static ulong
    8994        2688 : charLFwtk_Fl(long k, GEN CHIvec, GEN vz, ulong p)
    8995             : {
    8996             :   GEN P;
    8997             :   long r, m;
    8998             :   ulong S;
    8999        2688 :   if (k == 1) return charLFwt1_Fl(CHIvec, vz, p);
    9000         749 :   m = CHIvec_N(CHIvec);
    9001         749 :   if (m == 1) return Rg_to_Fl(gdivgs(bernfrac(k),-2*k), p);
    9002         441 :   S = 0;
    9003         441 :   P = RgX_to_Flx(RgX_rescale(bernpol(k,0), utoi(m)), p);
    9004       10017 :   for (r = 1; r < m; r++)
    9005             :   { /* S += P(r)*chi(r) */
    9006        9576 :     long a = mycharexpo(CHIvec,r);
    9007        9576 :     if (a < 0) continue;
    9008        8456 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, Flx_eval(P,r,p), p), p);
    9009             :   }
    9010         441 :   return Fl_div(Fl_neg(S,p), 2*k*m, p);
    9011             : }
    9012             : 
    9013             : static GEN
    9014        6300 : mfeisenstein2_0(long k, GEN CHI1, GEN CHI2, long ord)
    9015             : {
    9016        6300 :   if (k == 1 && mfcharistrivial(CHI1))
    9017        2072 :     return charLFwt1(CHI2, ord);
    9018        4228 :   else if (mfcharistrivial(CHI2))
    9019        1288 :     return charLFwtk(k, CHI1, ord);
    9020        2940 :   else return gen_0;
    9021             : }
    9022             : static ulong
    9023        4088 : mfeisenstein2_0_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p)
    9024             : {
    9025        4088 :   if (k == 1 && CHIvec_ord(CHI1vec) == 1)
    9026        1939 :     return charLFwtk_Fl(k, CHI2vec, vz, p);
    9027        2149 :   else if (CHIvec_ord(CHI2vec) == 1)
    9028         749 :     return charLFwtk_Fl(k, CHI1vec, vz, p);
    9029        1400 :   else return 0;
    9030             : }
    9031             : static GEN
    9032         126 : NK_eisen2(long k, GEN CHI1, GEN CHI2, long ord)
    9033             : {
    9034         126 :   long o, N = mfcharmodulus(CHI1)*mfcharmodulus(CHI2);
    9035         126 :   GEN CHI = mfcharmul(CHI1, CHI2);
    9036         126 :   o = mfcharorder(CHI);
    9037         126 :   if ((ord & 3) == 2) ord >>= 1;
    9038         126 :   if ((o & 3) == 2) o >>= 1;
    9039         126 :   if (ord != o) pari_err_IMPL("mfeisenstein for these characters");
    9040         119 :   return mkNK(N, k, CHI);
    9041             : }
    9042             : static GEN
    9043         336 : mfeisenstein_i(long k, GEN CHI1, GEN CHI2)
    9044             : {
    9045         336 :   long s = 1, ord, vt;
    9046             :   GEN E0, NK, vchi, T;
    9047         336 :   if (CHI2) { CHI2 = get_mfchar(CHI2); if (mfcharparity(CHI2) < 0) s = -s; }
    9048         336 :   if (CHI1) { CHI1 = get_mfchar(CHI1); if (mfcharparity(CHI1) < 0) s = -s; }
    9049         322 :   if (s != m1pk(k)) return mftrivial();
    9050         301 :   if (!CHI1) CHI1 = mfchartrivial();
    9051         301 :   if (!CHI2)
    9052             :   { /* E_k(chi1) */
    9053         175 :     vt = varn(mfcharpol(CHI1));
    9054         175 :     ord = mfcharorder(CHI1);
    9055         175 :     NK = mkNK(mfcharmodulus(CHI1), k, CHI1);
    9056         175 :     E0 = charLFwtk(k, CHI1, ord);
    9057         175 :     vchi = mkvec3(E0, mkvec(mfcharpol(CHI1)), CHI1);
    9058         175 :     return tag(t_MF_EISEN, NK, vchi);
    9059             :   }
    9060             :   /* E_k(chi1,chi2) */
    9061         126 :   vt = varn(mfcharpol(CHI1));
    9062         126 :   ord = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    9063         126 :   NK = NK_eisen2(k, CHI1, CHI2, ord);
    9064         119 :   E0 = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9065         119 :   T = mkvec(polcyclo(ord, vt));
    9066         119 :   vchi = mkvec4(E0, T, CHI1, CHI2);
    9067         119 :   return tag2(t_MF_EISEN, NK, vchi, mkvecsmall2(ord,0));
    9068             : }
    9069             : GEN
    9070         336 : mfeisenstein(long k, GEN CHI1, GEN CHI2)
    9071             : {
    9072         336 :   pari_sp av = avma;
    9073         336 :   if (k < 1) pari_err_DOMAIN("mfeisenstein", "k", "<", gen_1, stoi(k));
    9074         336 :   return gerepilecopy(av, mfeisenstein_i(k, CHI1, CHI2));
    9075             : }
    9076             : 
    9077             : static GEN
    9078        1407 : mfeisenstein2all(long N0, GEN NK, long k, GEN CHI1, GEN CHI2, GEN T, long o)
    9079             : {
    9080        1407 :   GEN E, E0 = mfeisenstein2_0(k, CHI1,CHI2, o), vchi = mkvec4(E0, T, CHI1,CHI2);
    9081        1407 :   long j, d = (lg(T)==4)? itou(gmael(T,3,1)): 1;
    9082        1407 :   E = cgetg(d+1, t_VEC);
    9083        1407 :   for (j=1; j<=d; j++) gel(E,j) = tag2(t_MF_EISEN, NK,vchi,mkvecsmall2(o,j-1));
    9084        1407 :   return mfbdall(E, N0 / mf_get_N(gel(E,1)));
    9085             : }
    9086             : 
    9087             : static GEN
    9088         651 : zncharsG(GEN G)
    9089             : {
    9090         651 :   long i, l, N = itou(znstar_get_N(G));
    9091             :   GEN vCHI, V;
    9092         651 :   if (N == 1) return mkvec2(gen_1,cgetg(1,t_COL));
    9093         651 :   vCHI = const_vec(N,NULL);
    9094         651 :   V = cyc2elts(znstar_get_conreycyc(G));
    9095         651 :   l = lg(V);
    9096       23289 :   for (i = 1; i < l; i++)
    9097             :   {
    9098       22638 :     GEN chi0, chi = zc_to_ZC(gel(V,i)), n, F;
    9099       22638 :     F = znconreyconductor(G, chi, &chi0);
    9100       22638 :     if (typ(F) != t_INT) F = gel(F,1);
    9101       22638 :     n = znconreyexp(G, chi);
    9102       22638 :     gel(vCHI, itos(n)) = mkvec2(F, chi0);
    9103             :   }
    9104         651 :   return vCHI;
    9105             : }
    9106             : 
    9107             : /* CHI primitive, f(CHI) | N. Return pairs (CHI1,CHI2) both primitive
    9108             :  * such that f(CHI1)*f(CHI2) | N and CHI1 * CHI2 = CHI;
    9109             :  * if k = 1, CHI1 is even; if k = 2, omit (1,1) if CHI = 1 */
    9110             : static GEN
    9111         854 : mfeisensteinbasis_i(long N0, long k, GEN CHI)
    9112             : {
    9113         854 :   GEN G = gel(CHI,1), chi = gel(CHI,2), vT = const_vec(myeulerphiu(N0), NULL);
    9114         854 :   GEN CHI0, GN, chiN, Lchi, LG, V, RES, NK, T, C = mfcharpol(CHI);
    9115         854 :   long i, j, l, n, n1, N, ord = mfcharorder(CHI);
    9116         854 :   long F = mfcharmodulus(CHI), vt = varn(mfcharpol(CHI));
    9117             : 
    9118         854 :   CHI0 = (F == 1)? CHI: mfchartrivial();
    9119         854 :   j = 1; RES = cgetg(N0+1, t_VEC);
    9120         854 :   T = gel(vT,ord) = Qab_trace_init(ord, ord, C, C);
    9121         854 :   if (F != 1 || k != 2)
    9122             :   { /* N1 = 1 */
    9123         721 :     NK = mkNK(F, k, CHI);
    9124         721 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI0, CHI, T, ord);
    9125         721 :     if (F != 1 && k != 1)
    9126         217 :       gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI, CHI0, T, ord);
    9127             :   }
    9128         854 :   if (N0 == 1) { setlg(RES,j); return RES; }
    9129         784 :   GN = G; chiN = chi;
    9130         784 :   if (F == N0) N = N0;
    9131             :   else
    9132             :   {
    9133         448 :     GEN faN = myfactoru(N0), P = gel(faN,1), E = gel(faN,2);
    9134         448 :     long lP = lg(P);
    9135        1148 :     for (i = N = 1; i < lP; i++)
    9136             :     {
    9137         700 :       long p = P[i];
    9138         700 :       N *= upowuu(p, maxuu(E[i]/2, z_lval(F,p)));
    9139             :     }
    9140         448 :     if ((N & 3) == 2) N >>= 1;
    9141         448 :     if (N == 1) { setlg(RES,j); return RES; }
    9142         315 :     if (F != N)
    9143             :     {
    9144          98 :       GN = znstar0(utoipos(N),1);
    9145          98 :       chiN = zncharinduce(G, chi, GN);
    9146             :     }
    9147             :   }
    9148         651 :   LG = const_vec(N, NULL); /* LG[d] = znstar(d,1) or NULL */
    9149         651 :   gel(LG,1) = gel(CHI0,1);
    9150         651 :   gel(LG,F) = G;
    9151         651 :   gel(LG,N) = GN;
    9152         651 :   Lchi = coprimes_zv(N);
    9153         651 :   n = itou(znconreyexp(GN,chiN));
    9154         651 :   V = zncharsG(GN); l = lg(V);
    9155       30296 :   for (n1 = 2; n1 < l; n1++) /* skip 1 (trivial char) */
    9156             :   {
    9157       29645 :     GEN v = gel(V,n1), w, chi1, chi2, G1, G2, CHI1, CHI2;
    9158             :     long N12, N1, N2, no, o12, t, m;
    9159       29645 :     if (!Lchi[n1]) continue;
    9160       21938 :     chi1 = gel(v,2); N1 = itou(gel(v,1)); /* conductor of chi1 */
    9161       21938 :     w = gel(V, Fl_div(n,n1,N));
    9162       21938 :     chi2 = gel(w,2); N2 = itou(gel(w,1)); /* conductor of chi2 */
    9163       21938 :     N12 = N1 * N2;
    9164       21938 :     if (N2 == 1 || N0 % N12) continue;
    9165             : 
    9166         658 :     G1 = gel(LG,N1); if (!G1) gel(LG,N1) = G1 = znstar0(utoipos(N1), 1);
    9167         658 :     if (k == 1 && zncharisodd(G1,chi1)) continue;
    9168         469 :     G2 = gel(LG,N2); if (!G2) gel(LG,N2) = G2 = znstar0(utoipos(N2), 1);
    9169         469 :     CHI1 = mfcharGL(G1, chi1);
    9170         469 :     CHI2 = mfcharGL(G2, chi2);
    9171         469 :     o12 = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    9172             :     /* remove Galois orbit: same trace */
    9173         469 :     no = Fl_powu(n1, ord, N);
    9174         763 :     for (t = 1+ord, m = n1; t <= o12; t += ord)
    9175             :     { /* m <-> CHI1^t, if t in Gal(Q(chi1,chi2)/Q), omit (CHI1^t,CHI2^t) */
    9176         294 :       m = Fl_mul(m, no, N); if (!m) break;
    9177         294 :       if (ugcd(t, o12) == 1) Lchi[m] = 0;
    9178             :     }
    9179         469 :     T = gel(vT,o12);
    9180         469 :     if (!T) T = gel(vT,o12) = Qab_trace_init(o12, ord, polcyclo(o12,vt), C);
    9181         469 :     NK = mkNK(N12, k, CHI);
    9182         469 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI1, CHI2, T, o12);
    9183             :   }
    9184         651 :   setlg(RES,j); return RES;
    9185             : }
    9186             : 
    9187             : static GEN
    9188         623 : mfbd_E2(GEN E2, long d, GEN CHI)
    9189             : {
    9190         623 :   GEN E2d = mfbd_i(E2, d);
    9191         623 :   GEN F = mkvec2(E2, E2d), L = mkvec2(gen_1, utoineg(d));
    9192             :   /* cannot use mflinear_i: E2 and E2d do not have the same level */
    9193         623 :   return tag3(t_MF_LINEAR, mkNK(d,2,CHI), F, L, gen_1);
    9194             : }
    9195             : /* C-basis of E_k(Gamma_0(N),chi). If k = 1, the first basis element must not
    9196             :  * vanish at oo [used in mfwt1basis]. Here E_1(CHI), whose q^0 coefficient
    9197             :  * does not vanish (since L(CHI,0) does not) *if* CHI is not trivial; which
    9198             :  * must be the case in weight 1.
    9199             :  *
    9200             :  * (k>=3): In weight k >= 3, basis is B(d) E(CHI1,(CHI/CHI1)_prim), where
    9201             :  * CHI1 is primitive modulo N1, and if N2 is the conductor of CHI/CHI1
    9202             :  * then d*N1*N2 | N.
    9203             :  * (k=2): In weight k=2, same if CHI is nontrivial. If CHI is trivial, must
    9204             :  * not take CHI1 trivial, and must add E_2(tau)-dE_2(d tau)), where
    9205             :  * d|N, d > 1.
    9206             :  * (k=1): In weight k=1, same as k >= 3 except that we restrict to CHI1 even */
    9207             : static GEN
    9208         882 : mfeisensteinbasis(long N, long k, GEN CHI)
    9209             : {
    9210             :   long i, F;
    9211             :   GEN L;
    9212         882 :   if (badchar(N, k, CHI)) return cgetg(1, t_VEC);
    9213         882 :   if (k == 0) return mfcharistrivial(CHI)? mkvec(mf1()): cgetg(1, t_VEC);
    9214         854 :   CHI = mfchartoprimitive(CHI, &F);
    9215         854 :   L = mfeisensteinbasis_i(N, k, CHI);
    9216         854 :   if (F == 1 && k == 2)
    9217             :   {
    9218         133 :     GEN v, E2 = mfeisenstein(2, NULL, NULL), D = mydivisorsu(N);
    9219         133 :     long nD = lg(D)-1;
    9220         133 :     v = cgetg(nD, t_VEC); L = vec_append(L,v);
    9221         133 :     for (i = 1; i < nD; i++) gel(v,i) = mfbd_E2(E2, D[i+1], CHI);
    9222             :   }
    9223         854 :   return lg(L) == 1? L: shallowconcat1(L);
    9224             : }
    9225             : 
    9226             : static GEN
    9227          70 : not_in_space(GEN F, long flag)
    9228             : {
    9229          70 :   if (!flag) err_space(F);
    9230          63 :   return cgetg(1, t_COL);
    9231             : }
    9232             : /* when flag set, no error */
    9233             : GEN
    9234         812 : mftobasis(GEN mf, GEN F, long flag)
    9235             : {
    9236         812 :   pari_sp av2, av = avma;
    9237             :   GEN G, v, y, gk;
    9238         812 :   long N, B, ismf = checkmf_i(F);
    9239             : 
    9240         812 :   mf = checkMF(mf);
    9241         812 :   if (ismf)
    9242             :   {
    9243         721 :     if (mfistrivial(F)) return zerocol(MF_get_dim(mf));
    9244         714 :     if (!mf_same_k(mf, F) || !mf_same_CHI(mf, F)) return not_in_space(F, flag);
    9245             :   }
    9246         763 :   N = MF_get_N(mf);
    9247         763 :   gk = MF_get_gk(mf);
    9248         763 :   if (ismf)
    9249             :   {
    9250         672 :     long NF = mf_get_N(F);
    9251         672 :     B = maxuu(mfsturmNgk(NF,gk), mfsturmNgk(N,gk)) + 1;
    9252         672 :     v = mfcoefs_i(F,B,1);
    9253             :   }
    9254             :   else
    9255             :   {
    9256          91 :     B = mfsturmNgk(N, gk) + 1;
    9257          91 :     switch(typ(F))
    9258             :     { /* F(0),...,F(lg(v)-2) */
    9259          63 :       case t_SER: v = sertocol(F); settyp(v,t_VEC); break;
    9260          14 :       case t_VEC: v = F; break;
    9261           7 :       case t_COL: v = shallowtrans(F); break;
    9262           7 :       default: pari_err_TYPE("mftobasis",F);
    9263             :                v = NULL;/*LCOV_EXCL_LINE*/
    9264             :     }
    9265          84 :     if (flag) B = minss(B, lg(v)-2);
    9266             :   }
    9267         756 :   y = mftobasis_i(mf, v);
    9268         756 :   if (typ(y) == t_VEC)
    9269             :   {
    9270          21 :     if (flag) return gerepilecopy(av, y);
    9271           0 :     pari_err(e_MISC, "not enough coefficients in mftobasis");
    9272             :   }
    9273         735 :   av2 = avma;
    9274         735 :   if (MF_get_space(mf) == mf_FULL || mfsturm(mf)+1 == B) return y;
    9275         210 :   G = mflinear(mf, y);
    9276         210 :   if (!gequal(v, mfcoefs_i(G, lg(v)-2,1))) y = NULL;
    9277         210 :   if (!y) { set_avma(av); return not_in_space(F, flag); }
    9278         182 :   set_avma(av2); return gerepileupto(av, y);
    9279             : }
    9280             : 
    9281             : /* assume N > 0; first cusp is always 0 */
    9282             : static GEN
    9283          49 : mfcusps_i(long N)
    9284             : {
    9285             :   long i, c, l;
    9286             :   GEN D, v;
    9287             : 
    9288          49 :   if (N == 1) return mkvec(gen_0);
    9289          49 :   D = mydivisorsu(N); l = lg(D); /* left on stack */
    9290          49 :   c = mfnumcuspsu_fact(myfactoru(N));
    9291          49 :   v = cgetg(c + 1, t_VEC);
    9292         350 :   for (i = c = 1; i < l; i++)
    9293             :   {
    9294         301 :     long C = D[i], NC = D[l-i], lima = ugcd(C, NC), A0, A;
    9295         889 :     for (A0 = 0; A0 < lima; A0++)
    9296         588 :       if (ugcd(A0, lima) == 1)
    9297             :       {
    9298         392 :         A = A0; while (ugcd(A,C) > 1) A += lima;
    9299         392 :         gel(v, c++) = sstoQ(A, C);
    9300             :       }
    9301             :   }
    9302          49 :   return v;
    9303             : }
    9304             : /* List of cusps of Gamma_0(N) */
    9305             : GEN
    9306          28 : mfcusps(GEN gN)
    9307             : {
    9308             :   long N;
    9309             :   GEN mf;
    9310          28 :   if (typ(gN) == t_INT) N = itos(gN);
    9311          14 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    9312           0 :   else { pari_err_TYPE("mfcusps", gN); N = 0; }
    9313          28 :   if (N <= 0) pari_err_DOMAIN("mfcusps", "N", "<=", gen_0, stoi(N));
    9314          28 :   return mfcusps_i(N);
    9315             : }
    9316             : 
    9317             : long
    9318         315 : mfcuspisregular(GEN NK, GEN cusp)
    9319             : {
    9320             :   long v, N, dk, nk, t, o;
    9321             :   GEN mf, CHI, go, A, C, g, c, d;
    9322         315 :   if ((mf = checkMF_i(NK)))
    9323             :   {
    9324          49 :     GEN gk = MF_get_gk(mf);
    9325          49 :     N = MF_get_N(mf);
    9326          49 :     CHI = MF_get_CHI(mf);
    9327          49 :     Qtoss(gk, &nk, &dk);
    9328             :   }
    9329             :   else
    9330         266 :     checkNK2(NK, &N, &nk, &dk, &CHI, 0);
    9331         315 :   if (typ(cusp) == t_INFINITY) return 1;
    9332         315 :   if (typ(cusp) == t_FRAC) { A = gel(cusp,1); C = gel(cusp,2); }
    9333          28 :   else { A = cusp; C = gen_1; }
    9334         315 :   g = diviuexact(mului(N,C), ugcd(N, Fl_sqr(umodiu(C,N), N)));
    9335         315 :   c = mulii(negi(C),g);
    9336         315 :   d = addiu(mulii(A,g), 1);
    9337         315 :   if (!CHI) return 1;
    9338         315 :   go = gmfcharorder(CHI);
    9339         315 :   v = vali(go); if (v < 2) go = shifti(go, 2-v);
    9340         315 :   t = itou( znchareval(gel(CHI,1), gel(CHI,2), d, go) );
    9341         315 :   if (dk == 1) return t == 0;
    9342         154 :   o = itou(go);
    9343         154 :   if (kronecker(c,d) < 0) t = Fl_add(t, o/2, o);
    9344         154 :   if (Mod4(d) == 1) return t == 0;
    9345          14 :   t = Fl_sub(t, Fl_mul(o/4, nk, o), o);
    9346          14 :   return t == 0;
    9347             : }
    9348             : 
    9349             : /* Some useful closures */
    9350             : 
    9351             : /* sum_{d|n} d^k */
    9352             : static GEN
    9353       37058 : mysumdivku(ulong n, ulong k)
    9354             : {
    9355       37058 :   GEN fa = myfactoru(n);
    9356       37058 :   return k == 1? usumdiv_fact(fa): usumdivk_fact(fa,k);
    9357             : }
    9358             : static GEN
    9359         833 : c_Ek(long n, long d, GEN F)
    9360             : {
    9361         833 :   GEN E = cgetg(n + 2, t_VEC), C = gel(F,2);
    9362         833 :   long i, k = mf_get_k(F);
    9363         833 :   gel (E, 1) = gen_1;
    9364       26117 :   for (i = 1; i <= n; i++)
    9365             :   {
    9366       25284 :     pari_sp av = avma;
    9367       25284 :     gel(E, i+1) = gerepileupto(av, gmul(C, mysumdivku(i*d, k-1)));
    9368             :   }
    9369         833 :   return E;
    9370             : }
    9371             : 
    9372             : GEN
    9373         364 : mfEk(long k)
    9374             : {
    9375         364 :   pari_sp av = avma;
    9376             :   GEN E0, NK;
    9377         364 :   if (k < 0 || odd(k)) pari_err_TYPE("mfEk [incorrect k]", stoi(k));
    9378         364 :   if (!k) return mf1();
    9379         357 :   E0 = gdivsg(-2*k, bernfrac(k));
    9380         357 :   NK = mkNK(1,k,mfchartrivial());
    9381         357 :   return gerepilecopy(av, tag(t_MF_Ek, NK, E0));
    9382             : }
    9383             : 
    9384             : GEN
    9385          56 : mfDelta(void)
    9386             : {
    9387          56 :   pari_sp av = avma;
    9388          56 :   return gerepilecopy(av, tag0(t_MF_DELTA, mkNK(1,12,mfchartrivial())));
    9389             : }
    9390             : 
    9391             : GEN
    9392         707 : mfTheta(GEN psi)
    9393             : {
    9394         707 :   pari_sp av = avma;
    9395             :   GEN N, gk, psi2;
    9396             :   long par;
    9397         707 :   if (!psi) { psi = mfchartrivial(); N = utoipos(4); par = 1; }
    9398             :   else
    9399             :   {
    9400             :     long FC;
    9401          21 :     psi = get_mfchar(psi);
    9402          21 :     FC = mfcharconductor(psi);
    9403          21 :     if (mfcharmodulus(psi) != FC)
    9404           0 :       pari_err_TYPE("mfTheta [nonprimitive character]", psi);
    9405          21 :     par = mfcharparity(psi);
    9406          21 :     N = shifti(sqru(FC),2);
    9407             :   }
    9408         707 :   if (par > 0) { gk = ghalf; psi2 = psi; }
    9409           7 :   else { gk = gsubsg(2, ghalf); psi2 = mfcharmul(psi, get_mfchar(stoi(-4))); }
    9410         707 :   return gerepilecopy(av, tag(t_MF_THETA, mkgNK(N, gk, psi2, pol_x(1)), psi));
    9411             : }
    9412             : 
    9413             : /* Output 0 if not desired eta product: if flag=0 (default) require
    9414             :  * holomorphic at cusps. If flag set, accept meromorphic, but sill in some
    9415             :  * modular function space */
    9416             : GEN
    9417         196 : mffrometaquo(GEN eta, long flag)
    9418             : {
    9419         196 :   pari_sp av = avma;
    9420             :   GEN NK, N, k, BR, P;
    9421         196 :   long v, cusp = 0;
    9422         196 :   if (!etaquotype(&eta, &N,&k,&P, &v, NULL, flag? NULL: &cusp) || cusp < 0)
    9423             :   {
    9424          14 :     set_avma(av); return gen_0;
    9425             :   }
    9426         182 :   if (lg(gel(eta,1)) == 1) { set_avma(av); return mf1(); }
    9427         175 :   BR = mkvec2(ZV_to_zv(gel(eta,1)), ZV_to_zv(gel(eta,2)));
    9428         175 :   if (v < 0) v = 0;
    9429         175 :   NK = mkgNK(N, k, get_mfchar(P), pol_x(1));
    9430         175 :   return gerepilecopy(av, tag2(t_MF_ETAQUO, NK, BR, utoi(v)));
    9431             : }
    9432             : 
    9433             : /* Q^(-r) */
    9434             : static GEN
    9435         361 : RgXn_negpow(GEN Q, long r, long L)
    9436             : {
    9437         361 :   if (r < 0) r = -r; else Q = RgXn_inv_i(Q, L);
    9438         361 :   if (r != 1) Q = RgXn_powu_i(Q, r, L);
    9439         361 :   return Q;
    9440             : }
    9441             : /* flag same as in mffrometaquo: if set, accept meromorphic. */
    9442             : static GEN
    9443          42 : mfisetaquo_i(GEN F, long flag)
    9444             : {
    9445             :   GEN gk, P, E, M, S, G, CHI, v, w;
    9446             :   long b, l, L, N, vS, m, j;
    9447          42 :   const long bextra = 10;
    9448             : 
    9449          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfisetaquo",F);
    9450          42 :   CHI = mf_get_CHI(F); if (mfcharorder(CHI) > 2) return NULL;
    9451          42 :   N = mf_get_N(F);
    9452          42 :   gk = mf_get_gk(F);
    9453          42 :   b = mfsturmNgk(N, gk);
    9454          42 :   L = maxss(N, b) + bextra;
    9455          42 :   S = mfcoefs_i(F, L, 1);
    9456          42 :   if (!RgV_is_ZV(S)) return NULL;
    9457         203 :   for (vS = 1; vS <= L+1; vS++)
    9458         203 :     if (signe(gel(S,vS))) break;
    9459          42 :   vS--; if (vS) { S = vecslice(S, vS+1, L+1); L -= vS; }
    9460          42 :   S = RgV_to_RgX(S, 0); l = lg(S)-2;
    9461          42 :   P = cgetg(l, t_COL);
    9462          42 :   E = cgetg(l, t_COL); w = v = gen_0; /* w = weight, v = valuation */
    9463         550 :   for (m = j = 1; m+2 < lg(S); m++)
    9464             :   {
    9465         515 :     GEN c = gel(S,m+2);
    9466             :     long r;
    9467         515 :     if (is_bigint(c)) return NULL;
    9468         508 :     r = -itos(c);
    9469         508 :     if (r)
    9470             :     {
    9471         361 :       S = ZXn_mul(S, RgXn_negpow(eta_ZXn(m, L), r, L), L);
    9472         361 :       gel(P,j) = utoipos(m);
    9473         361 :       gel(E,j) = stoi(r);
    9474         361 :       v = addmuliu(v, gel(E,j), m);
    9475         361 :       w = addis(w, r);
    9476         361 :       j++;
    9477             :     }
    9478             :   }
    9479          35 :   if (!equalii(w, gmul2n(gk, 1)) || (!flag && !equalii(v, muluu(24,vS))))
    9480           7 :     return NULL;
    9481          28 :   setlg(P, j);
    9482          28 :   setlg(E, j); M = mkmat2(P, E); G = mffrometaquo(M, flag);
    9483          28 :   return (typ(G) != t_INT
    9484          28 :           && (mfsturmmf(G) <= b + bextra || mfisequal(F, G, b)))? M: NULL;
    9485             : }
    9486             : GEN
    9487          42 : mfisetaquo(GEN F, long flag)
    9488             : {
    9489          42 :   pari_sp av = avma;
    9490          42 :   GEN M = mfisetaquo_i(F, flag);
    9491          42 :   if (!M) { set_avma(av); return gen_0; }
    9492          28 :   return gerepilecopy(av, M);
    9493             : }
    9494             : 
    9495             : #if 0
    9496             : /* number of primitive characters modulo N */
    9497             : static ulong
    9498             : numprimchars(ulong N)
    9499             : {
    9500             :   GEN fa, P, E;
    9501             :   long i, l;
    9502             :   ulong n;
    9503             :   if ((N & 3) == 2) return 0;
    9504             :   fa = myfactoru(N);
    9505             :   P = gel(fa,1); l = lg(P);
    9506             :   E = gel(fa,2);
    9507             :   for (i = n = 1; i < l; i++)
    9508             :   {
    9509             :     ulong p = P[i], e = E[i];
    9510             :     if (e == 2) n *= p-2; else n *= (p-1)*(p-1)*upowuu(p,e-2);
    9511             :   }
    9512             :   return n;
    9513             : }
    9514             : #endif
    9515             : 
    9516             : /* Space generated by products of two Eisenstein series */
    9517             : 
    9518             : INLINE int
    9519      134946 : cmp_small(long a, long b) { return a>b? 1: (a<b? -1: 0); }
    9520             : static int
    9521       72205 : cmp_small_priority(void *E, GEN a, GEN b)
    9522             : {
    9523       72205 :   GEN prio = (GEN)E;
    9524       72205 :   return cmp_small(prio[(long)a], prio[(long)b]);
    9525             : }
    9526             : static long
    9527        1134 : znstar_get_expo(GEN G)
    9528             : {
    9529        1134 :   GEN cyc = znstar_get_cyc(G);
    9530        1134 :   return (lg(cyc) == 1)? 1: itou(gel(cyc,1));
    9531             : }
    9532             : 
    9533             : /* Return [vchi, bymod, vG]:
    9534             :  * vG[f] = znstar(f,1) for f a conductor of (at least) a char mod N; else NULL
    9535             :  * bymod[f] = vecsmall of conrey indexes of chars modulo f | N; else NULL
    9536             :  * vchi[n] = a list of CHIvec [G0,chi0,o,ncharvecexpo(G0,nchi0),...]:
    9537             :  *   chi0 = primitive char attached to Conrey Mod(n,N)
    9538             :  * (resp. NULL if (n,N) > 1) */
    9539             : static GEN
    9540         567 : charsmodN(long N)
    9541             : {
    9542         567 :   GEN D, G, prio, phio, dummy = cgetg(1,t_VEC);
    9543         567 :   GEN vP, vG = const_vec(N,NULL), vCHI  = const_vec(N,NULL);
    9544         567 :   GEN bymod = const_vec(N,NULL);
    9545         567 :   long pn, i, l, vt = fetch_user_var("t");
    9546         567 :   D = mydivisorsu(N); l = lg(D);
    9547        3507 :   for (i = 1; i < l; i++)
    9548        2940 :     gel(bymod, D[i]) = vecsmalltrunc_init(myeulerphiu(D[i])+1);
    9549         567 :   gel(vG,N) = G = znstar0(utoipos(N),1);
    9550         567 :   pn = znstar_get_expo(G);  /* exponent(Z/NZ)^* */
    9551         567 :   vP = const_vec(pn,NULL);
    9552       25641 :   for (i = 1; i <= N; i++)
    9553             :   {
    9554             :     GEN P, gF, G0, chi0, nchi0, chi, v, go;
    9555             :     long j, F, o;
    9556       25074 :     if (ugcd(i,N) != 1) continue;
    9557       13433 :     chi = znconreylog(G, utoipos(i));
    9558       13433 :     gF = znconreyconductor(G, chi, &chi0);
    9559       13433 :     F = (typ(gF) == t_INT)? itou(gF): itou(gel(gF,1));
    9560       13433 :     G0 = gel(vG, F); if (!G0) G0 = gel(vG,F) = znstar0(gF, 1);
    9561       13433 :     nchi0 = znconreylog_normalize(G0,chi0);
    9562       13433 :     go = gel(nchi0,1); o = itou(go); /* order(chi0) */
    9563       13433 :     v = ncharvecexpo(G0, nchi0);
    9564       13433 :     if (!equaliu(go, pn)) v = zv_z_mul(v, pn / o);
    9565       13433 :     P = gel(vP, o); if (!P) P = gel(vP,o) = polcyclo(o,vt);
    9566             :     /* mfcharcxinit with dummy complex powers */
    9567       13433 :     gel(vCHI,i) = mkvecn(6, G0, chi0, go, v, dummy, P);
    9568       13433 :     D = mydivisorsu(N / F); l = lg(D);
    9569       13433 :     for (j = 1; j < l; j++) vecsmalltrunc_append(gel(bymod, F*D[j]), i);
    9570             :   }
    9571         567 :   phio = zero_zv(pn); l = lg(vCHI); prio = cgetg(l, t_VEC);
    9572       25641 :   for (i = 1; i < l; i++)
    9573             :   {
    9574       25074 :     GEN CHI = gel(vCHI,i);
    9575             :     long o;
    9576       25074 :     if (!CHI) continue;
    9577       13433 :     o = CHIvec_ord(CHI);
    9578       13433 :     if (!phio[o]) phio[o] = myeulerphiu(o);
    9579       13433 :     prio[i] = phio[o];
    9580             :   }
    9581         567 :   l = lg(bymod);
    9582             :   /* sort characters by increasing value of phi(order) */
    9583       25641 :   for (i = 1; i < l; i++)
    9584             :   {
    9585       25074 :     GEN z = gel(bymod,i);
    9586       25074 :     if (z) gen_sort_inplace(z, (void*)prio, &cmp_small_priority, NULL);
    9587             :   }
    9588         567 :   return mkvec3(vCHI, bymod, vG);
    9589             : }
    9590             : 
    9591             : static GEN
    9592        4774 : mfeisenstein2pure(long k, GEN CHI1, GEN CHI2, long ord, GEN P, long lim)
    9593             : {
    9594        4774 :   GEN c, V = cgetg(lim+2, t_COL);
    9595             :   long n;
    9596        4774 :   c = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9597        4774 :   if (P) c = grem(c, P);
    9598        4774 :   gel(V,1) = c;
    9599       99428 :   for (n=1; n <= lim; n++)
    9600             :   {
    9601       94654 :     c = sigchi2(k, CHI1, CHI2, n, ord);
    9602       94654 :     if (P) c = grem(c, P);
    9603       94654 :     gel(V,n+1) = c;
    9604             :   }
    9605        4774 :   return V;
    9606             : }
    9607             : static GEN
    9608        4088 : mfeisenstein2pure_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p, long lim)
    9609             : {
    9610        4088 :   GEN V = cgetg(lim+2, t_VECSMALL);
    9611             :   long n;
    9612        4088 :   V[1] = mfeisenstein2_0_Fl(k, CHI1vec, CHI2vec, vz, p);
    9613        4088 :   for (n=1; n <= lim; n++) V[n+1] = sigchi2_Fl(k, CHI1vec, CHI2vec, n, vz, p);
    9614        4088 :   return V;
    9615             : }
    9616             : 
    9617             : static GEN
    9618         217 : getcolswt2(GEN M, GEN D, ulong p)
    9619             : {
    9620         217 :   GEN R, v = gel(M,1);
    9621         217 :   long i, l = lg(M) - 1;
    9622         217 :   R = cgetg(l, t_MAT); /* skip D[1] = 1 */
    9623         812 :   for (i = 1; i < l; i++)
    9624             :   {
    9625         595 :     GEN w = Flv_Fl_mul(gel(M,i+1), D[i+1], p);
    9626         595 :     gel(R,i) = Flv_sub(v, w, p);
    9627             :   }
    9628         217 :   return R;
    9629             : }
    9630             : static GEN
    9631        4984 : expandbd(GEN V, long d)
    9632             : {
    9633             :   long L, n, nd;
    9634             :   GEN W;
    9635        4984 :   if (d == 1) return V;
    9636        1897 :   L = lg(V)-1; W = zerocol(L); /* nd = n/d */
    9637        1897 :   for (n = nd = 0; n < L; n += d, nd++) gel(W, n+1) = gel(V, nd+1);
    9638        1897 :   return W;
    9639             : }
    9640             : static GEN
    9641        6342 : expandbd_Fl(GEN V, long d)
    9642             : {
    9643             :   long L, n, nd;
    9644             :   GEN W;
    9645        6342 :   if (d == 1) return V;
    9646        2254 :   L = lg(V)-1; W = zero_Flv(L); /* nd = n/d */
    9647        2254 :   for (n = nd = 0; n < L; n += d, nd++) W[n+1] = V[nd+1];
    9648        2254 :   return W;
    9649             : }
    9650             : static void
    9651        4088 : getcols_i(GEN *pM, GEN *pvj, GEN gk, GEN CHI1vec, GEN CHI2vec, long NN1, GEN vz,
    9652             :           ulong p, long lim)
    9653             : {
    9654        4088 :   GEN CHI1 = CHIvec_CHI(CHI1vec), CHI2 = CHIvec_CHI(CHI2vec);
    9655        4088 :   long N2 = CHIvec_N(CHI2vec);
    9656        4088 :   GEN vj, M, D = mydivisorsu(NN1/N2);
    9657        4088 :   long i, l = lg(D), k = gk[2];
    9658        4088 :   GEN V = mfeisenstein2pure_Fl(k, CHI1vec, CHI2vec, vz, p, lim);
    9659        4088 :   M = cgetg(l, t_MAT);
    9660        4088 :   for (i = 1; i < l; i++) gel(M,i) = expandbd_Fl(V, D[i]);
    9661        4088 :   if (k == 2 && N2 == 1 && CHIvec_N(CHI1vec) == 1)
    9662             :   {
    9663         217 :     M = getcolswt2(M, D, p); l--;
    9664         217 :     D = vecslice(D, 2, l);
    9665             :   }
    9666        4088 :   *pM = M;
    9667        4088 :   *pvj = vj = cgetg(l, t_VEC);
    9668        4088 :   for (i = 1; i < l; i++) gel(vj,i) = mkvec4(gk, CHI1, CHI2, utoipos(D[i]));
    9669        4088 : }
    9670             : 
    9671             : /* find all CHI1, CHI2 mod N such that CHI1*CHI2 = CHI, f(CHI1)*f(CHI2) | N.
    9672             :  * set M = mfcoefs(B_e E(CHI1,CHI2), lim), vj = [e,i1,i2] */
    9673             : static void
    9674        1666 : getcols(GEN *pM, GEN *pv, long k, long nCHI, GEN allN, GEN vz, ulong p,
    9675             :         long lim)
    9676             : {
    9677        1666 :   GEN vCHI = gel(allN,1), gk = utoi(k);
    9678        1666 :   GEN M = cgetg(1,t_MAT), v = cgetg(1,t_VEC);
    9679        1666 :   long i1, N = lg(vCHI)-1;
    9680       83888 :   for (i1 = 1; i1 <= N; i1++)
    9681             :   {
    9682       82222 :     GEN CHI1vec = gel(vCHI, i1), CHI2vec, M1, v1;
    9683             :     long NN1, i2;
    9684      161455 :     if (!CHI1vec) continue;
    9685       65282 :     if (k == 1 && CHIvec_parity(CHI1vec) == -1) continue;
    9686       40670 :     NN1 = N/CHIvec_N(CHI1vec); /* N/f(chi1) */;
    9687       40670 :     i2 = Fl_div(nCHI,i1, N);
    9688       40670 :     if (!i2) i2 = 1;
    9689       40670 :     CHI2vec = gel(vCHI,i2);
    9690       40670 :     if (NN1 % CHIvec_N(CHI2vec)) continue; /* f(chi1)f(chi2) | N ? */
    9691        2989 :     getcols_i(&M1, &v1, gk, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9692        2989 :     M = shallowconcat(M, M1);
    9693        2989 :     v = shallowconcat(v, v1);
    9694             :   }
    9695        1666 :   *pM = M;
    9696        1666 :   *pv = v;
    9697        1666 : }
    9698             : 
    9699             : static void
    9700        1106 : update_Mj(GEN *M, GEN *vecj, GEN *pz, ulong p)
    9701             : {
    9702             :   GEN perm;
    9703        1106 :   *pz = Flm_indexrank(*M, p); perm = gel(*pz,2);
    9704        1106 :   *M = vecpermute(*M, perm);
    9705        1106 :   *vecj = vecpermute(*vecj, perm);
    9706        1106 : }
    9707             : static int
    9708         350 : getcolsgen(long dim, GEN *pM, GEN *pvj, GEN *pz, long k, long ell, long nCHI,
    9709             :            GEN allN, GEN vz, ulong p, long lim)
    9710             : {
    9711         350 :   GEN vCHI = gel(allN,1), bymod = gel(allN,2), gell = utoi(ell);
    9712         350 :   long i1, N = lg(vCHI)-1;
    9713         350 :   long L = lim+1;
    9714         350 :   if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9715         350 :   if (lg(*pvj)-1 == dim) return 1;
    9716        1512 :   for (i1 = 1; i1 <= N; i1++)
    9717             :   {
    9718        1491 :     GEN CHI1vec = gel(vCHI, i1), T;
    9719             :     long par1, j, l, N1, NN1;
    9720             : 
    9721        1491 :     if (!CHI1vec) continue;
    9722        1470 :     par1 = CHIvec_parity(CHI1vec);
    9723        1470 :     if (ell == 1 && par1 == -1) continue;
    9724         889 :     if (odd(ell)) par1 = -par1;
    9725         889 :     N1 = CHIvec_N(CHI1vec);
    9726         889 :     NN1 = N/N1;
    9727         889 :     T = gel(bymod, NN1); l = lg(T);
    9728        3500 :     for (j = 1; j < l; j++)
    9729             :     {
    9730        2919 :       long i2 = T[j], l1, l2, j1, s, nC;
    9731        2919 :       GEN M, M1, M2, vj, vj1, vj2, CHI2vec = gel(vCHI, i2);
    9732        4739 :       if (CHIvec_parity(CHI2vec) != par1) continue;
    9733        1099 :       nC = Fl_div(nCHI, Fl_mul(i1,i2,N), N);
    9734        1099 :       getcols(&M2, &vj2, k-ell, nC, allN, vz, p, lim);
    9735        1099 :       l2 = lg(M2); if (l2 == 1) continue;
    9736        1099 :       getcols_i(&M1, &vj1, gell, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9737        1099 :       l1 = lg(M1);
    9738        1099 :       M1 = Flm_to_FlxV(M1, 0);
    9739        1099 :       M2 = Flm_to_FlxV(M2, 0);
    9740        1099 :       M  = cgetg((l1-1)*(l2-1) + 1, t_MAT);
    9741        1099 :       vj = cgetg((l1-1)*(l2-1) + 1, t_VEC);
    9742        2660 :       for (j1 = s = 1; j1 < l1; j1++)
    9743             :       {
    9744        1561 :         GEN E = gel(M1,j1), v = gel(vj1,j1);
    9745             :         long j2;
    9746        6286 :         for (j2 = 1; j2 < l2; j2++, s++)
    9747             :         {
    9748        4725 :           GEN c = Flx_to_Flv(Flxn_mul(E, gel(M2,j2), L, p), L);
    9749        4725 :           gel(M,s) = c;
    9750        4725 :           gel(vj,s) = mkvec2(v, gel(vj2,j2));
    9751             :         }
    9752             :       }
    9753        1099 :       *pM = shallowconcat(*pM, M);
    9754        1099 :       *pvj = shallowconcat(*pvj, vj);
    9755        1099 :       if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9756        1099 :       if (lg(*pvj)-1 == dim) return 1;
    9757             :     }
    9758             :   }
    9759          21 :   if (ell == 1)
    9760             :   {
    9761          21 :     update_Mj(pM, pvj, pz, p);
    9762          21 :     return (lg(*pvj)-1 == dim);
    9763             :   }
    9764           0 :   return 0;
    9765             : }
    9766             : 
    9767             : static GEN
    9768        1316 : mkF2bd(long d, long lim)
    9769             : {
    9770        1316 :   GEN V = zerovec(lim + 1);
    9771             :   long n;
    9772        1316 :   gel(V, 1) = ginv(stoi(-24));
    9773        1316 :   for (n = 1; n <= lim/d; n++) gel(V, n*d + 1) = mysumdivku(n, 1);
    9774        1316 :   return V;
    9775             : }
    9776             : 
    9777             : static GEN
    9778        5278 : mkeisen(GEN E, long ord, GEN P, long lim)
    9779             : {
    9780        5278 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9781        5278 :   GEN CHI1 = gel(E,2), CHI2 = gel(E,3);
    9782        5278 :   if (k == 2 && mfcharistrivial(CHI1) && mfcharistrivial(CHI2))
    9783         504 :     return gsub(mkF2bd(1,lim), gmulgs(mkF2bd(e,lim), e));
    9784             :   else
    9785             :   {
    9786        4774 :     GEN V = mfeisenstein2pure(k, CHI1, CHI2, ord, P, lim);
    9787        4774 :     return expandbd(V, e);
    9788             :   }
    9789             : }
    9790             : static GEN
    9791         525 : mkM(GEN vj, long pn, GEN P, long lim)
    9792             : {
    9793         525 :   long j, l = lg(vj), L = lim+1;
    9794         525 :   GEN M = cgetg(l, t_MAT);
    9795        4361 :   for (j = 1; j < l; j++)
    9796             :   {
    9797             :     GEN E1, E2;
    9798        3836 :     parse_vecj(gel(vj,j), &E1,&E2);
    9799        3836 :     E1 = RgV_to_RgX(mkeisen(E1, pn, P, lim), 0);
    9800        3836 :     if (E2)
    9801             :     {
    9802        1442 :       E2 = RgV_to_RgX(mkeisen(E2, pn, P, lim), 0);
    9803        1442 :       E1 = RgXn_mul(E1, E2, L);
    9804             :     }
    9805        3836 :     E1 = RgX_to_RgC(E1, L);
    9806        3836 :     if (P && E2) E1 = RgXQV_red(E1, P);
    9807        3836 :     gel(M,j) = E1;
    9808             :   }
    9809         525 :   return M;
    9810             : }
    9811             : 
    9812             : /* assume N > 2 */
    9813             : static GEN
    9814          35 : mffindeisen1(long N)
    9815             : {
    9816          35 :   GEN G = znstar0(utoipos(N), 1), L = chargalois(G, NULL), chi0 = NULL;
    9817          35 :   long j, m = N, l = lg(L);
    9818         259 :   for (j = 1; j < l; j++)
    9819             :   {
    9820         245 :     GEN chi = gel(L,j);
    9821         245 :     long r = myeulerphiu(itou(zncharorder(G,chi)));
    9822         245 :     if (r >= m) continue;
    9823         182 :     chi = znconreyfromchar(G, chi);
    9824         182 :     if (zncharisodd(G,chi)) { m = r; chi0 = chi; if (r == 1) break; }
    9825             :   }
    9826          35 :   if (!chi0) pari_err_BUG("mffindeisen1 [no Eisenstein series found]");
    9827          35 :   chi0 = znchartoprimitive(G,chi0);
    9828          35 :   return mfcharGL(gel(chi0,1), gel(chi0,2));
    9829             : }
    9830             : 
    9831             : static GEN
    9832         567 : mfeisensteinspaceinit_i(long N, long k, GEN CHI)
    9833             : {
    9834         567 :   GEN M, Minv, vj, vG, GN, allN, P, vz, z = NULL;
    9835         567 :   long nCHI, lim, ell, ord, dim = mffulldim(N, k, CHI);
    9836             :   ulong r, p;
    9837             : 
    9838         567 :   if (!dim) retmkvec3(cgetg(1,t_VECSMALL),
    9839             :                       mkvec2(cgetg(1,t_MAT),gen_1),cgetg(1,t_VEC));
    9840         567 :   lim = mfsturmNk(N, k) + 1;
    9841         567 :   allN = charsmodN(N);
    9842         567 :   vG = gel(allN,3);
    9843         567 :   GN = gel(vG,N);
    9844         567 :   ord = znstar_get_expo(GN);
    9845         567 :   P = ord <= 2? NULL: polcyclo(ord, varn(mfcharpol(CHI)));
    9846         567 :   CHI = induce(GN, CHI); /* lift CHI mod N before mfcharno*/
    9847         567 :   nCHI = mfcharno(CHI);
    9848         567 :   r = QabM_init(ord, &p);
    9849         567 :   vz = Fl_powers(r, ord, p);
    9850         567 :   getcols(&M, &vj, k, nCHI, allN, vz, p, lim);
    9851         588 :   for (ell = k>>1; ell >= 1; ell--)
    9852         350 :     if (getcolsgen(dim, &M, &vj, &z, k, ell, nCHI, allN, vz, p, lim)) break;
    9853         567 :   if (!z) update_Mj(&M, &vj, &z, p);
    9854         567 :   if (lg(vj) - 1 < dim) return NULL;
    9855         525 :   M = mkM(vj, ord, P, lim);
    9856         525 :   Minv = QabM_Minv(rowpermute(M, gel(z,1)), P, ord);
    9857         525 :   return mkvec4(gel(z,1), Minv, vj, utoi(ord));
    9858             : }
    9859             : /* true mf */
    9860             : static GEN
    9861         525 : mfeisensteinspaceinit(GEN mf)
    9862             : {
    9863         525 :   pari_sp av = avma;
    9864         525 :   GEN z, CHI = MF_get_CHI(mf);
    9865         525 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    9866         525 :   if (!CHI) CHI = mfchartrivial();
    9867         525 :   z = mfeisensteinspaceinit_i(N, k, CHI);
    9868         525 :   if (!z)
    9869             :   {
    9870          35 :     GEN E, CHIN = mffindeisen1(N), CHI0 = mfchartrivial();
    9871          35 :     z = mfeisensteinspaceinit_i(N, k+1, mfcharmul(CHI, CHIN));
    9872          35 :     if (z) E = mkvec4(gen_1, CHI0, CHIN, gen_1);
    9873             :     else
    9874             :     {
    9875           7 :       z = mfeisensteinspaceinit_i(N, k+2, CHI);
    9876           7 :       E = mkvec4(gen_2, CHI0, CHI0, utoipos(N));
    9877             :     }
    9878          35 :     z = mkvec2(z, E);
    9879             :   }
    9880         525 :   return gerepilecopy(av, z);
    9881             : }
    9882             : 
    9883             : /* decomposition of modular form on eisenspace */
    9884             : static GEN
    9885         938 : mfeisensteindec(GEN mf, GEN F)
    9886             : {
    9887         938 :   pari_sp av = avma;
    9888             :   GEN M, Mindex, Mvecj, V, B, CHI;
    9889             :   long o, ord;
    9890             : 
    9891         938 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    9892         938 :   if (lg(Mvecj) < 5)
    9893             :   {
    9894          56 :     GEN E, e = gel(Mvecj,2), gkE = gel(e,1);
    9895          56 :     long dE = itou(gel(e,4));
    9896          56 :     Mvecj = gel(Mvecj,1);
    9897          56 :     E = mfeisenstein(itou(gkE), NULL, gel(e,3));
    9898          56 :     if (dE != 1) E = mfbd_E2(E, dE, gel(e,2)); /* here k = 2 */
    9899          56 :     F = mfmul(F, E);
    9900             :   }
    9901         938 :   M = gel(Mvecj, 2);
    9902         938 :   if (lg(M) == 1) return cgetg(1, t_VEC);
    9903         938 :   Mindex = gel(Mvecj, 1);
    9904         938 :   ord = itou(gel(Mvecj,4));
    9905         938 :   V = mfcoefs(F, Mindex[lg(Mindex)-1]-1, 1); settyp(V, t_COL);
    9906         938 :   CHI = mf_get_CHI(F);
    9907         938 :   o = mfcharorder(CHI);
    9908         938 :   if (o > 2 && o != ord)
    9909             :   { /* convert Mod(.,polcyclo(o)) to Mod(., polcyclo(N)) for o | N,
    9910             :      * o and N both != 2 (mod 4) */
    9911          84 :     GEN z, P = gel(M,4); /* polcyclo(ord) */
    9912          84 :     long vt = varn(P);
    9913          84 :     z = gmodulo(pol_xn(ord/o, vt), P);
    9914          84 :     if (ord % o) pari_err_TYPE("mfeisensteindec", V);
    9915          84 :     V = gsubst(liftpol_shallow(V), vt, z);
    9916             :   }
    9917         938 :   B = Minv_RgC_mul(M, vecpermute(V, Mindex));
    9918         938 :   return gerepileupto(av, B);
    9919             : }
    9920             : 
    9921             : /*********************************************************************/
    9922             : /*                        END EISENSPACE                             */
    9923             : /*********************************************************************/
    9924             : 
    9925             : static GEN
    9926          70 : sertocol2(GEN S, long l)
    9927             : {
    9928          70 :   GEN C = cgetg(l + 2, t_COL);
    9929             :   long i;
    9930          70 :   for (i = 0; i <= l; i++) gel(C, i+1) = polcoef_i(S, i, -1);
    9931          70 :   return C;
    9932             : }
    9933             : 
    9934             : /* Compute polynomial P0 such that F=E4^(k/4)P0(E6/E4^(3/2)). */
    9935             : static GEN
    9936          14 : mfcanfindp0(GEN F, long k)
    9937             : {
    9938          14 :   pari_sp ltop = avma;
    9939             :   GEN E4, E6, V, V1, Q, W, res, M, B;
    9940             :   long l, j;
    9941          14 :   l = k/6 + 2;
    9942          14 :   V = mfcoefsser(F,l);
    9943          14 :   E4 = mfcoefsser(mfEk(4),l);
    9944          14 :   E6 = mfcoefsser(mfEk(6),l);
    9945          14 :   V1 = gdiv(V, gpow(E4, sstoQ(k,4), 0));
    9946          14 :   Q = gdiv(E6, gpow(E4, sstoQ(3,2), 0));
    9947          14 :   W = gpowers(Q, l - 1);
    9948          14 :   M = cgetg(l + 1, t_MAT);
    9949          14 :   for (j = 1; j <= l; j++) gel(M,j) = sertocol2(gel(W,j), l);
    9950          14 :   B = sertocol2(V1, l);
    9951          14 :   res = inverseimage(M, B);
    9952          14 :   if (lg(res) == 1) err_space(F);
    9953          14 :   return gerepilecopy(ltop, gtopolyrev(res, 0));
    9954             : }
    9955             : 
    9956             : /* Compute the first n+1 Taylor coeffs at tau=I of a modular form
    9957             :  * on SL_2(Z). */
    9958             : GEN
    9959          14 : mftaylor(GEN F, long n, long flreal, long prec)
    9960             : {
    9961          14 :   pari_sp ltop = avma;
    9962          14 :   GEN P0, Pm1 = gen_0, v;
    9963          14 :   GEN X2 = mkpoln(3, ghalf,gen_0,gneg(ghalf)); /* (x^2-1) / 2 */
    9964             :   long k, m;
    9965          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftaylor",F);
    9966          14 :   k = mf_get_k(F);
    9967          14 :   if (mf_get_N(F) != 1 || k < 0) pari_err_IMPL("mftaylor for this form");
    9968          14 :   P0 = mfcanfindp0(F, k);
    9969          14 :   v = cgetg(n+2, t_VEC); gel(v, 1) = RgX_coeff(P0,0);
    9970         154 :   for (m = 0; m < n; m++)
    9971             :   {
    9972         140 :     GEN P1 = gdivgs(gmulsg(-(k + 2*m), RgX_shift(P0,1)), 12);
    9973         140 :     P1 = gadd(P1, gmul(X2, RgX_deriv(P0)));
    9974         140 :     if (m) P1 = gsub(P1, gdivgs(gmulsg(m*(m+k-1), Pm1), 144));
    9975         140 :     Pm1 = P0; P0 = P1;
    9976         140 :     gel(v, m+2) = RgX_coeff(P0, 0);
    9977             :   }
    9978          14 :   if (flreal)
    9979             :   {
    9980           7 :     GEN pi2 = Pi2n(1, prec), pim4 = gmulsg(-2, pi2), VPC;
    9981           7 :     GEN C = gmulsg(3, gdiv(gpowgs(ggamma(ginv(utoi(4)), prec), 8), gpowgs(pi2, 6)));
    9982             :     /* E_4(i): */
    9983           7 :     GEN facn = gen_1;
    9984           7 :     VPC = gpowers(gmul(pim4, gsqrt(C, prec)), n);
    9985           7 :     C = gpow(C, sstoQ(k,4), prec);
    9986          84 :     for (m = 0; m <= n; m++)
    9987             :     {
    9988          77 :       gel(v, m+1) = gdiv(gmul(C, gmul(gel(v, m+1), gel(VPC, m+1))), facn);
    9989          77 :       facn = gmulgs(facn, m+1);
    9990             :     }
    9991             :   }
    9992          14 :   return gerepilecopy(ltop, v);
    9993             : }
    9994             : 
    9995             : #if 0
    9996             : /* To be used in mfeigensearch() */
    9997             : GEN
    9998             : mfreadratfile()
    9999             : {
   10000             :   GEN eqn;
   10001             :   pariFILE *F = pari_fopengz("rateigen300.gp");
   10002             :   eqn = gp_readvec_stream(F->file);
   10003             :   pari_fclose(F);
   10004             :   return eqn;
   10005             : }
   10006             : #endif
   10007             :  /*****************************************************************/
   10008             : /*           EISENSTEIN CUSPS: COMPLEX DIRECTLY: one F_k         */
   10009             : /*****************************************************************/
   10010             : 
   10011             : /* CHIvec = charinit(CHI); data = [N1g/g1,N2g/g2,g1/g,g2/g,C/g1,C/g2,
   10012             :  * (N1g/g1)^{-1},(N2g/g2)^{-1}] */
   10013             : 
   10014             : /* nm = n/m;
   10015             :  * z1 = powers of \z_{C/g}^{(Ae/g)^{-1}},
   10016             :  * z2 = powers of \z_N^{A^{-1}(g1g2/C)}]
   10017             :  * N.B. : we compute value and conjugate at the end, so it is (Ae/g)^{-1}
   10018             :  * and not -(Ae/g)^{-1} */
   10019             : static GEN
   10020     7649684 : eiscnm(long nm, long m, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1)
   10021             : {
   10022     7649684 :   long Cg1 = data[5], s10 = (nm*data[7]) % Cg1, r10 = (nm - data[1]*s10) / Cg1;
   10023     7649684 :   long Cg2 = data[6], s20 = (m *data[8]) % Cg2, r20 = (m  - data[2]*s20) / Cg2;
   10024             :   long j1, r1, s1;
   10025     7649684 :   GEN T = gen_0;
   10026    18420640 :   for (j1 = 0, r1 = r10, s1 = s10; j1 < data[3]; j1++, r1 -= data[1], s1 += Cg1)
   10027             :   {
   10028    10770956 :     GEN c1 = mychareval(CHI1vec, r1);
   10029    10770956 :     if (!gequal0(c1))
   10030             :     {
   10031             :       long j2, r2, s2;
   10032     7916678 :       GEN S = gen_0;
   10033    20492654 :       for (j2 = 0, r2 = r20, s2 = s20; j2 < data[4]; j2++, r2 -= data[2], s2 += Cg2)
   10034             :       {
   10035    12575976 :         GEN c2 = mychareval(CHI2vec, r2);
   10036    12575976 :         if (!gequal0(c2)) S = gadd(S, gmul(c2, rootsof1pow(z1, s1*s2)));
   10037             :       }
   10038     7916678 :       T = gadd(T, gmul(c1, S));
   10039             :     }
   10040             :   }
   10041     7649684 :   return conj_i(T);
   10042             : }
   10043             : 
   10044             : static GEN
   10045      594566 : fg1g2n(long n, long k, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1, GEN z2)
   10046             : {
   10047      594566 :   pari_sp av = avma;
   10048      594566 :   GEN S = gen_0, D = mydivisorsu(n);
   10049      594566 :   long i, l = lg(D);
   10050     4419408 :   for (i = 1; i < l; i++)
   10051             :   {
   10052     3824842 :     long m = D[i], nm = D[l-i]; /* n/m */
   10053     3824842 :     GEN u = eiscnm( nm,  m, CHI1vec, CHI2vec, data, z1);
   10054     3824842 :     GEN v = eiscnm(-nm, -m, CHI1vec, CHI2vec, data, z1);
   10055     3824842 :     GEN w = odd(k) ? gsub(u, v) : gadd(u, v);
   10056     3824842 :     S = gadd(S, gmul(powuu(m, k-1), w));
   10057             :   }
   10058      594566 :   return gerepileupto(av, gmul(S, rootsof1pow(z2, n)));
   10059             : }
   10060             : 
   10061             : static GEN
   10062       13090 : gausssumcx(GEN CHIvec, long prec)
   10063             : {
   10064             :   GEN z, S, V;
   10065       13090 :   long m, N = CHIvec_N(CHIvec);
   10066       13090 :   if (N == 1) return gen_1;
   10067        7098 :   V = CHIvec_val(CHIvec);
   10068        7098 :   z = rootsof1u_cx(N, prec);
   10069        7098 :   S = gmul(z, gel(V, N));
   10070        7098 :   for (m = N-1; m >= 1; m--) S = gmul(z, gadd(gel(V, m), S));
   10071        7098 :   return S;
   10072             : }
   10073             : 
   10074             : /* Computation of Q_k(\z_N^s) as a polynomial in \z_N^s. FIXME: explicit
   10075             :  * formula ? */
   10076             : static GEN
   10077        2163 : mfqk(long k, long N)
   10078             : {
   10079             :   GEN X, P, ZI, Q, Xm1, invden;
   10080             :   long i;
   10081        2163 :   ZI = gdivgs(RgX_shift_shallow(RgV_to_RgX(identity_ZV(N-1), 0), 1), N);
   10082        2163 :   if (k == 1) return ZI;
   10083        1113 :   P = gsubgs(pol_xn(N,0), 1);
   10084        1113 :   invden = RgXQ_powu(ZI, k, P);
   10085        1113 :   X = pol_x(0); Q = gneg(X); Xm1 = gsubgs(X, 1);
   10086        2765 :   for (i = 2; i < k; i++)
   10087        1652 :     Q = RgX_shift_shallow(ZX_add(gmul(Xm1, ZX_deriv(Q)), gmulsg(-i, Q)), 1);
   10088        1113 :   return RgXQ_mul(Q, invden, P);
   10089             : }
   10090             : 
   10091             : /* CHI mfchar; M is a multiple of the conductor of CHI, but is NOT
   10092             :  * necessarily its modulus */
   10093             : static GEN
   10094        3038 : mfskcx(long k, GEN CHI, long M, long prec)
   10095             : {
   10096             :   GEN S, CHIvec, P;
   10097             :   long F, m, i, l;
   10098        3038 :   CHI = mfchartoprimitive(CHI, &F);
   10099        3038 :   CHIvec = mfcharcxinit(CHI, prec);
   10100        3038 :   if (F == 1) S = gdivgs(bernfrac(k), k);
   10101             :   else
   10102             :   {
   10103        2163 :     GEN Q = mfqk(k, F), V = CHIvec_val(CHIvec);
   10104        2163 :     S = gmul(gel(V, F), RgX_coeff(Q, 0));
   10105        2163 :     for (m = 1; m < F; m++) S = gadd(S, gmul(gel(V, m), RgX_coeff(Q, m)));
   10106        2163 :     S = conj_i(S);
   10107             :   }
   10108             :   /* prime divisors of M not dividing f(chi) */
   10109        3038 :   P = gel(myfactoru(u_ppo(M/F,F)), 1); l = lg(P);
   10110        3164 :   for (i = 1; i < l; i++)
   10111             :   {
   10112         126 :     long p = P[i];
   10113         126 :     S = gmul(S, gsubsg(1, gdiv(mychareval(CHIvec, p), powuu(p, k))));
   10114             :   }
   10115        3038 :   return gmul(gmul(gausssumcx(CHIvec, prec), S), powuu(M/F, k));
   10116             : }
   10117             : 
   10118             : static GEN
   10119        5600 : f00_i(long k, GEN CHI1vec, GEN CHI2vec, GEN G2, GEN S, long prec)
   10120             : {
   10121             :   GEN c, a;
   10122        5600 :   long N1 = CHIvec_N(CHI1vec), N2 = CHIvec_N(CHI2vec);
   10123        5600 :   if (S[2] != N1) return gen_0;
   10124        3038 :   c = mychareval(CHI1vec, S[3]);
   10125        3038 :   if (isintzero(c)) return gen_0;
   10126        3038 :   a = mfskcx(k, mfchardiv(CHIvec_CHI(CHI2vec), CHIvec_CHI(CHI1vec)), N1*N2, prec);
   10127        3038 :   a = gmul(a, conj_i(gmul(c,G2)));
   10128        3038 :   return gdiv(a, mulsi(-N2, powuu(S[1], k-1)));
   10129             : }
   10130             : 
   10131             : static GEN
   10132        4431 : f00(long k, GEN CHI1vec,GEN CHI2vec, GEN G1,GEN G2, GEN data, long prec)
   10133             : {
   10134             :   GEN T1, T2;
   10135        4431 :   T2 = f00_i(k, CHI1vec, CHI2vec, G2, data, prec);
   10136        4431 :   if (k > 1) return T2;
   10137        1169 :   T1 = f00_i(k, CHI2vec, CHI1vec, G1, data, prec);
   10138        1169 :   return gadd(T1, T2);
   10139             : }
   10140             : 
   10141             : /* ga in SL_2(Z), find beta [a,b;c,d] in Gamma_0(N) and mu in Z such that
   10142             :  * beta * ga * T^u = [A',B';C',D'] with C' | N and N | B', C' > 0 */
   10143             : static void
   10144        5026 : mfgatogap(GEN ga, long N, long *pA, long *pC, long *pD, long *pd, long *pmu)
   10145             : {
   10146        5026 :   GEN A = gcoeff(ga,1,1), B = gcoeff(ga,1,2);
   10147        5026 :   GEN C = gcoeff(ga,2,1), D = gcoeff(ga,2,2), a, b, c, d;
   10148             :   long t, Ap, Cp, B1, D1, mu;
   10149        5026 :   Cp = itou(bezout(muliu(A,N), C, &c, &d)); /* divides N */
   10150        5026 :   t = 0;
   10151        5026 :   if (Cp > 1)
   10152             :   { /* (d, N/Cp) = 1, find t such that (d - t*(A*N/Cp), N) = 1 */
   10153        2408 :     long dN = umodiu(d,Cp), Q = (N/Cp * umodiu(A,Cp)) % Cp;
   10154        2408 :     while (ugcd(dN, Cp) > 1) { t++; dN = Fl_sub(dN, Q, Cp); }
   10155             :   }
   10156        5026 :   if (t)
   10157             :   {
   10158         371 :     c = addii(c, mului(t, diviuexact(C,Cp)));
   10159         371 :     d = subii(d, mului(t, muliu(A, N/Cp))); /* (d,N) = 1 */
   10160             :   }
   10161        5026 :   D1 = umodiu(mulii(d,D), N);
   10162        5026 :   (void)bezout(d, mulis(c,-N), &a, &b); /* = 1 */
   10163        5026 :   t = 0; Ap = umodiu(addii(mulii(a,A), mulii(b,C)), N); /* (Ap,Cp) = 1 */
   10164        5026 :   while (ugcd(Ap, N) > 1) { t++; Ap = Fl_add(Ap, Cp, N); }
   10165        5026 :   B1 = umodiu(a,N)*umodiu(B,N) + umodiu(b,N)*umodiu(D,N) + t*D1;
   10166        5026 :   B1 %= N;
   10167        5026 :   *pmu = mu = Fl_neg(Fl_div(B1, Ap, N), N);
   10168             :   /* A', D' and d only needed modulo N */
   10169        5026 :   *pd = umodiu(d, N);
   10170        5026 :   *pA = Ap;
   10171        5026 :   *pC = Cp; *pD = (D1 + Cp*mu) % N;
   10172        5026 : }
   10173             : 
   10174             : #if 0
   10175             : /* CHI is a mfchar, return alpha(CHI) */
   10176             : static long
   10177             : mfalchi(GEN CHI, long AN, long cg)
   10178             : {
   10179             :   GEN G = gel(CHI,1), chi = gel(CHI,2), go = gmfcharorder(CHI);
   10180             :   long o = itou(go), a = itos( znchareval(G, chi, stoi(1 + AN/cg), go) );
   10181             :   if (a < 0 || (cg * a) % o) pari_err_BUG("mfalchi");
   10182             :   return (cg * a) / o;
   10183             : }
   10184             : #endif
   10185             : /* return A such that CHI1(t) * CHI2(t) = e(A) or NULL if (t,N1*N2) > 1 */
   10186             : static GEN
   10187       10052 : mfcharmuleval(GEN CHI1vec, GEN CHI2vec, long t)
   10188             : {
   10189       10052 :   long a1 = mycharexpo(CHI1vec, t), o1 = CHIvec_ord(CHI1vec);
   10190       10052 :   long a2 = mycharexpo(CHI2vec, t), o2 = CHIvec_ord(CHI2vec);;
   10191       10052 :   if (a1 < 0 || a2 < 0) return NULL;
   10192       10052 :   return sstoQ(a1*o2 + a2*o1, o1*o2);
   10193             : }
   10194             : static GEN
   10195        5026 : mfcharmulcxeval(GEN CHI1vec, GEN CHI2vec, long t, long prec)
   10196             : {
   10197        5026 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, t);
   10198             :   long n, d;
   10199        5026 :   if (!A) return gen_0;
   10200        5026 :   Qtoss(A, &n,&d); return rootsof1q_cx(n, d, prec);
   10201             : }
   10202             : /* alpha(CHI1 * CHI2) */
   10203             : static long
   10204        5026 : mfalchi2(GEN CHI1vec, GEN CHI2vec, long AN, long cg)
   10205             : {
   10206        5026 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, 1 + AN/cg);
   10207             :   long a;
   10208        5026 :   if (!A) pari_err_BUG("mfalchi2");
   10209        5026 :   A = gmulsg(cg, A);
   10210        5026 :   if (typ(A) != t_INT) pari_err_BUG("mfalchi2");
   10211        5026 :   a = itos(A) % cg; if (a < 0) a += cg;
   10212        5026 :   return a;
   10213             : }
   10214             : 
   10215             : /* return g = (a,b), set u >= 0 s.t. g = a * u (mod b) */
   10216             : static long
   10217       20104 : mybezout(long a, long b, long *pu)
   10218             : {
   10219       20104 :   long junk, g = cbezout(a, b, pu, &junk);
   10220       20104 :   if (*pu < 0) *pu += b/g;
   10221       20104 :   return g;
   10222             : }
   10223             : 
   10224             : /* E = [k, CHI1,CHI2, e], CHI1 and CHI2 primitive mfchars such that,
   10225             :  * CHI1(-1)*CHI2(-1) = (-1)^k; expansion of (B_e (E_k(CHI1,CHI2))) | ga.
   10226             :  * w is the width for the space of the calling function. */
   10227             : static GEN
   10228        5026 : mfeisensteingacx(GEN E, long w, GEN ga, long lim, long prec)
   10229             : {
   10230        5026 :   GEN CHI1vec, CHI2vec, CHI1 = gel(E,2), CHI2 = gel(E,3), v, S, ALPHA;
   10231             :   GEN G1, G2, z1, z2, data;
   10232        5026 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
   10233        5026 :   long N1 = mfcharmodulus(CHI1);
   10234        5026 :   long N2 = mfcharmodulus(CHI2), N = e * N1 * N2;
   10235             :   long NsurC, cg, wN, A, C, Ai, d, mu, alchi, na, da;
   10236             :   long eg, g, gH, U, u0, u1, u2, Aig, H, m, n, t, Cg, NC1, NC2;
   10237             : 
   10238        5026 :   mfgatogap(ga, N, &A, &C, &Ai, &d, &mu);
   10239        5026 :   CHI1vec = mfcharcxinit(CHI1, prec);
   10240        5026 :   CHI2vec = mfcharcxinit(CHI2, prec);
   10241        5026 :   NsurC = N/C; cg  = ugcd(C, NsurC); wN = NsurC / cg;
   10242        5026 :   if (w%wN) pari_err_BUG("mfeisensteingacx [wN does not divide w]");
   10243        5026 :   alchi = mfalchi2(CHI1vec, CHI2vec, A*N, cg);
   10244        5026 :   ALPHA = sstoQ(alchi, NsurC);
   10245             : 
   10246        5026 :   g = mybezout(A*e, C, &u0); Cg = C/g; eg = e/g;
   10247        5026 :   NC1 = mybezout(N1, Cg, &u1);
   10248        5026 :   NC2 = mybezout(N2, Cg, &u2);
   10249        5026 :   H = (NC1*NC2*g)/Cg;
   10250        5026 :   Aig = (Ai*H)%N; if (Aig < 0) Aig += N;
   10251        5026 :   z1 = rootsof1powinit(u0, Cg, prec);
   10252        5026 :   z2 = rootsof1powinit(Aig, N, prec);
   10253        5026 :   data = mkvecsmalln(8, N1/NC1, N2/NC2, NC1, NC2, Cg/NC1, Cg/NC2, u1, u2);
   10254        5026 :   v = zerovec(lim + 1);
   10255             :   /* need n*H = alchi (mod cg) */
   10256        5026 :   gH = mybezout(H, cg, &U);
   10257        5026 :   if (gH > 1)
   10258             :   {
   10259         399 :     if (alchi % gH) return mkvec2(gen_0, v);
   10260         399 :     alchi /= gH; cg /= gH; H /= gH;
   10261             :   }
   10262        5026 :   G1 = gausssumcx(CHI1vec, prec);
   10263        5026 :   G2 = gausssumcx(CHI2vec, prec);
   10264        5026 :   if (!alchi)
   10265        4431 :     gel(v,1) = f00(k, CHI1vec,CHI2vec,G1,G2, mkvecsmall3(NC2,Cg,A*eg), prec);
   10266        5026 :   n = Fl_mul(alchi,U,cg); if (!n) n = cg;
   10267        5026 :   m = (n*H - alchi) / cg; /* positive, exact division */
   10268      599592 :   for (; m <= lim; n+=cg, m+=H)
   10269      594566 :     gel(v, m+1) = fg1g2n(n, k, CHI1vec, CHI2vec, data, z1,z2);
   10270        5026 :   t = (2*e)/g; if (odd(k)) t = -t;
   10271        5026 :   v = gdiv(v, gmul(conj_i(gmul(G1,G2)), mulsi(t, powuu(eg*N2/NC2, k-1))));
   10272        5026 :   if (k == 2 && N1 == 1 && N2 == 1) v = gsub(mkF2bd(wN,lim), gmulsg(e,v));
   10273             : 
   10274        5026 :   Qtoss(ALPHA, &na,&da);
   10275        5026 :   S = conj_i( mfcharmulcxeval(CHI1vec,CHI2vec,d,prec) ); /* CHI(1/d) */
   10276        5026 :   if (wN > 1)
   10277             :   {
   10278        3871 :     GEN z = rootsof1powinit(-mu, wN, prec);
   10279        3871 :     long i, l = lg(v);
   10280        3871 :     for (i = 1; i < l; i++) gel(v,i) = gmul(gel(v,i), rootsof1pow(z,i-1));
   10281             :   }
   10282        5026 :   v = RgV_Rg_mul(v, gmul(S, rootsof1q_cx(-mu*na, da, prec)));
   10283        5026 :   return mkvec2(ALPHA, bdexpand(v, w/wN));
   10284             : }
   10285             : 
   10286             : /*****************************************************************/
   10287             : /*                       END EISENSTEIN CUSPS                    */
   10288             : /*****************************************************************/
   10289             : 
   10290             : static GEN
   10291        1589 : mfchisimpl(GEN CHI)
   10292             : {
   10293             :   GEN G, chi;
   10294        1589 :   if (typ(CHI) == t_INT) return CHI;
   10295        1589 :   G = gel(CHI, 1); chi = gel(CHI, 2);
   10296        1589 :   switch(mfcharorder(CHI))
   10297             :   {
   10298        1141 :     case 1: chi = gen_1; break;
   10299         427 :     case 2: chi = znchartokronecker(G,chi,1); break;
   10300          21 :     default:chi = mkintmod(znconreyexp(G,chi), znstar_get_N(G)); break;
   10301             :   }
   10302        1589 :   return chi;
   10303             : }
   10304             : 
   10305             : GEN
   10306         700 : mfparams(GEN F)
   10307             : {
   10308         700 :   pari_sp av = avma;
   10309             :   GEN z, mf, CHI;
   10310         700 :   if ((mf = checkMF_i(F)))
   10311             :   {
   10312          14 :     long N = MF_get_N(mf);
   10313          14 :     GEN gk = MF_get_gk(mf);
   10314          14 :     CHI = MF_get_CHI(mf);
   10315          14 :     z = mkvec5(utoi(N), gk, CHI, utoi(MF_get_space(mf)), mfcharpol(CHI));
   10316             :   }
   10317             :   else
   10318             :   {
   10319         686 :     if (!checkmf_i(F)) pari_err_TYPE("mfparams", F);
   10320         686 :     z = vec_append(mf_get_NK(F), mfcharpol(mf_get_CHI(F)));
   10321             :   }
   10322         700 :   gel(z,3) = mfchisimpl(gel(z,3));
   10323         700 :   return gerepilecopy(av, z);
   10324             : }
   10325             : 
   10326             : GEN
   10327          14 : mfisCM(GEN F)
   10328             : {
   10329          14 :   pari_sp av = avma;
   10330             :   forprime_t S;
   10331             :   GEN D, v;
   10332             :   long N, k, lD, sb, p, i;
   10333          14 :   if (!checkmf_i(F)) pari_err_TYPE("mfisCM", F);
   10334          14 :   N = mf_get_N(F);
   10335          14 :   k = mf_get_k(F); if (N < 0 || k < 0) pari_err_IMPL("mfisCM for this F");
   10336          14 :   D = mfunram(N, -1);
   10337          14 :   lD = lg(D);
   10338          14 :   sb = maxss(mfsturmNk(N, k), 4*N);
   10339          14 :   v = mfcoefs_i(F, sb, 1);
   10340          14 :   u_forprime_init(&S, 2, sb);
   10341         518 :   while ((p = u_forprime_next(&S)))
   10342             :   {
   10343         490 :     GEN ap = gel(v, p+1);
   10344         490 :     if (!gequal0(ap))
   10345         406 :       for (i = 1; i < lD; i++)
   10346         245 :         if (kross(D[i], p) == -1) { D = vecsplice(D, i); lD--; }
   10347             :   }
   10348          14 :   if (lD == 1) { set_avma(av); return gen_0; }
   10349          14 :   if (lD == 2) { set_avma(av); return stoi(D[1]); }
   10350           7 :   if (k > 1) pari_err_BUG("mfisCM");
   10351           7 :   return gerepileupto(av, zv_to_ZV(D));
   10352             : }
   10353             : 
   10354             : static long
   10355         287 : mfspace_i(GEN mf, GEN F)
   10356             : {
   10357             :   GEN v, vF, gk;
   10358             :   long n, nE, i, l, s, N;
   10359             : 
   10360         287 :   mf = checkMF(mf); s = MF_get_space(mf);
   10361         287 :   if (!F) return s;
   10362         287 :   if (!checkmf_i(F)) pari_err_TYPE("mfspace",F);
   10363         287 :   v = mftobasis(mf, F, 1);
   10364         287 :   n = lg(v)-1; if (!n) return -1;
   10365         231 :   nE = lg(MF_get_E(mf))-1;
   10366         231 :   switch(s)
   10367             :   {
   10368          56 :     case mf_NEW: case mf_OLD: case mf_EISEN: return s;
   10369             :     case mf_FULL:
   10370         140 :       if (mf_get_type(F) == t_MF_THETA) return mf_EISEN;
   10371         133 :       if (!gequal0(vecslice(v,1,nE)))
   10372          63 :         return gequal0(vecslice(v,nE+1,n))? mf_EISEN: mf_FULL;
   10373             :   }
   10374             :   /* mf is mf_CUSP or mf_FULL, F a cusp form */
   10375         105 :   gk = mf_get_gk(F);
   10376         105 :   if (typ(gk) == t_FRAC || equali1(gk)) return mf_CUSP;
   10377          91 :   vF = mftonew_i(mf, vecslice(v, nE+1, n), &N);
   10378          91 :   if (N != MF_get_N(mf)) return mf_OLD;
   10379          63 :   l = lg(vF);
   10380         105 :   for (i = 1; i < l; i++)
   10381          63 :     if (itos(gmael(vF,i,1)) != N) return mf_CUSP;
   10382          42 :   return mf_NEW;
   10383             : }
   10384             : long
   10385         287 : mfspace(GEN mf, GEN F)
   10386         287 : { pari_sp av = avma; return gc_long(av, mfspace_i(mf,F)); }
   10387             : static GEN
   10388          14 : lfunfindchi(GEN ldata, GEN van, long prec)
   10389             : {
   10390          14 :   GEN gN = ldata_get_conductor(ldata), gk = ldata_get_k(ldata);
   10391          14 :   GEN G = znstar0(gN,1), L, go, vz;
   10392          14 :   long N = itou(gN), odd = typ(gk) == t_INT && mpodd(gk);
   10393          14 :   long i, j, o, l, B0 = 2, B = lg(van)-1, bit = 10 - prec2nbits(prec);
   10394             : 
   10395          14 :   van = shallowcopy(van);
   10396          14 :   L = cyc2elts(znstar_get_conreycyc(G));
   10397          14 :   l = lg(L);
   10398          42 :   for (i = j = 1; i < l; i++)
   10399             :   {
   10400          28 :     GEN chi = zc_to_ZC(gel(L,i));
   10401          28 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
   10402             :   }
   10403          14 :   setlg(L,j); l = j;
   10404          14 :   if (l <= 2) return gel(L,1);
   10405           0 :   o = znstar_get_expo(G); go = utoi(o);
   10406           0 :   vz = grootsof1(o, prec);
   10407             :   for (;;)
   10408           0 :   {
   10409             :     long n;
   10410           0 :     for (n = B0; n <= B; n++)
   10411             :     {
   10412           0 :       GEN an = gel(van,n), r;
   10413             :       long j;
   10414           0 :       if (ugcd(n, N) != 1 || gexpo(an) < bit) continue;
   10415           0 :       r = gdiv(an, conj_i(an));
   10416           0 :       for (i = 1; i < l; i++)
   10417             :       {
   10418           0 :         GEN CHI = gel(L,i);
   10419           0 :         if (gexpo(gsub(r, gel(vz, znchareval_i(CHI,n,go)+1))) > bit)
   10420           0 :           gel(L,i) = NULL;
   10421             :       }
   10422           0 :       for (i = j = 1; i < l; i++)
   10423           0 :         if (gel(L,i)) gel(L,j++) = gel(L,i);
   10424           0 :       l = j; setlg(L,l);
   10425           0 :       if (l == 2) return gel(L,1);
   10426             :     }
   10427           0 :     B0 = B+1; B <<= 1;
   10428           0 :     van = ldata_vecan(ldata_get_an(ldata), B, prec);
   10429             :   }
   10430             : }
   10431             : 
   10432             : GEN
   10433          14 : mffromlfun(GEN L, long prec)
   10434             : {
   10435          14 :   pari_sp av = avma;
   10436          14 :   GEN ldata = lfunmisc_to_ldata_shallow(L), Vga = ldata_get_gammavec(ldata);
   10437          14 :   GEN van, a0, CHI, NK, gk = ldata_get_k(ldata);
   10438             :   long N, space;
   10439          14 :   if (!gequal(Vga, mkvec2(gen_0, gen_1))) pari_err_TYPE("mffromlfun", L);
   10440          14 :   N = itou(ldata_get_conductor(ldata));
   10441          14 :   van = ldata_vecan(ldata_get_an(ldata), mfsturmNgk(N,gk) + 2, prec);
   10442          14 :   CHI = lfunfindchi(ldata, van, prec);
   10443          14 :   space = (lg(ldata) == 7)? mf_CUSP: mf_FULL;
   10444          14 :   a0 = (space == mf_CUSP)? gen_0: gneg(lfun(L, gen_0, prec2nbits(prec)));
   10445          14 :   NK = mkvec3(utoi(N), gk, mfchisimpl(CHI));
   10446          14 :   return gerepilecopy(av, mkvec3(NK, utoi(space), shallowconcat(a0, van)));
   10447             : }
   10448             : /*******************************************************************/
   10449             : /*                                                                 */
   10450             : /*                       HALF-INTEGRAL WEIGHT                      */
   10451             : /*                                                                 */
   10452             : /*******************************************************************/
   10453             : /* We use the prefix mf2; k represents the weight -1/2, so e.g.
   10454             :    k = 2 is weight 5/2. N is the level, so 4\mid N, and CHI is the
   10455             :    character, always even. */
   10456             : 
   10457             : static long
   10458        3360 : lamCO(long r, long s, long p)
   10459             : {
   10460        3360 :   if ((s << 1) <= r)
   10461             :   {
   10462        1232 :     long rp = r >> 1;
   10463        1232 :     if (odd(r)) return upowuu(p, rp) << 1;
   10464         336 :     else return (p + 1)*upowuu(p, rp - 1);
   10465             :   }
   10466        2128 :   else return upowuu(p, r - s) << 1;
   10467             : }
   10468             : 
   10469             : static int
   10470        1568 : condC(GEN faN, GEN valF)
   10471             : {
   10472        1568 :   GEN P = gel(faN, 1), E = gel(faN, 2);
   10473        1568 :   long l = lg(P), i;
   10474        3696 :   for (i = 1; i < l; i++)
   10475        3024 :     if ((P[i] & 3L) == 3)
   10476             :     {
   10477        1120 :       long r = E[i];
   10478        1120 :       if (odd(r) || r < (valF[i] << 1)) return 1;
   10479             :     }
   10480         672 :   return 0;
   10481             : }
   10482             : 
   10483             : /* returns 2*zetaCO; weight is k + 1/2 */
   10484             : static long
   10485        3696 : zeta2CO(GEN faN, GEN valF, long r2, long s2, long k)
   10486             : {
   10487        3696 :   if (r2 >= 4) return lamCO(r2, s2, 2) << 1;
   10488        2912 :   if (r2 == 3) return 6;
   10489        1568 :   if (condC(faN, valF)) return 4;
   10490         672 :   if (odd(k)) return s2 ? 3 : 5; else return s2 ? 5: 3;
   10491             : }
   10492             : 
   10493             : /* returns 4 times last term in formula */
   10494             : static long
   10495        3696 : dim22(long N, long F, long k)
   10496             : {
   10497        3696 :   pari_sp av = avma;
   10498        3696 :   GEN vF, faN = myfactoru(N), P = gel(faN, 1), E = gel(faN, 2);
   10499        3696 :   long i, D, l = lg(P);
   10500        3696 :   vF = cgetg(l, t_VECSMALL);
   10501        3696 :   for (i = 1; i < l; i++) vF[i] = u_lval(F, P[i]);
   10502        3696 :   D = zeta2CO(faN, vF, E[1], vF[1], k);
   10503        3696 :   for (i = 2; i < l; i++) D *= lamCO(E[i], vF[i], P[i]);
   10504        3696 :   return gc_long(av,D);
   10505             : }
   10506             : 
   10507             : /* PSI not necessarily primitive, of conductor F */
   10508             : static int
   10509       13846 : charistotallyeven(GEN PSI, long F)
   10510             : {
   10511       13846 :   pari_sp av = avma;
   10512       13846 :   GEN P = gel(myfactoru(F), 1);
   10513       13846 :   GEN G = gel(PSI,1), psi = gel(PSI,2);
   10514             :   long i;
   10515       14350 :   for (i = 1; i < lg(P); i++)
   10516             :   {
   10517         532 :     GEN psip = znchardecompose(G, psi, utoipos(P[i]));
   10518         532 :     if (zncharisodd(G, psip)) return gc_bool(av,0);
   10519             :   }
   10520       13818 :   return gc_bool(av,1);
   10521             : }
   10522             : 
   10523             : static GEN
   10524      299775 : get_PSI(GEN CHI, long t)
   10525             : {
   10526      299775 :   long r = t & 3L, t2 = (r == 2 || r == 3) ? t << 2 : t;
   10527      299775 :   return mfcharmul_i(CHI, induce(gel(CHI,1), utoipos(t2)));
   10528             : }
   10529             : /* space = mf_CUSP, mf_EISEN or mf_FULL, weight k + 1/2 */
   10530             : static long
   10531       41363 : mf2dimwt12(long N, GEN CHI, long space)
   10532             : {
   10533       41363 :   pari_sp av = avma;
   10534       41363 :   GEN D = mydivisorsu(N >> 2);
   10535       41363 :   long i, l = lg(D), dim3 = 0, dim4 = 0;
   10536             : 
   10537       41363 :   CHI = induceN(N, CHI);
   10538      341138 :   for (i = 1; i < l; i++)
   10539             :   {
   10540      299775 :     long rp, t = D[i], Mt = D[l-i];
   10541      299775 :     GEN PSI = get_PSI(CHI,t);
   10542      299775 :     rp = mfcharconductor(PSI);
   10543      299775 :     if (Mt % (rp*rp) == 0) { dim4++; if (charistotallyeven(PSI,rp)) dim3++; }
   10544             :   }
   10545       41363 :   set_avma(av);
   10546       41363 :   switch (space)
   10547             :   {
   10548       40439 :     case mf_CUSP: return dim4 - dim3;
   10549         462 :     case mf_EISEN:return dim3;
   10550         462 :     case mf_FULL: return dim4;
   10551             :   }
   10552             :   return 0; /*LCOV_EXCL_LINE*/
   10553             : }
   10554             : 
   10555             : static long
   10556         693 : mf2dimwt32(long N, GEN CHI, long F, long space)
   10557             : {
   10558             :   long D;
   10559         693 :   switch(space)
   10560             :   {
   10561         231 :     case mf_CUSP: D = mypsiu(N) - 6*dim22(N, F, 1);
   10562         231 :       if (D%24) pari_err_BUG("mfdim");
   10563         231 :       return D/24 + mf2dimwt12(N, CHI, 4);
   10564         231 :     case mf_FULL: D = mypsiu(N) + 6*dim22(N, F, 0);
   10565         231 :       if (D%24) pari_err_BUG("mfdim");
   10566         231 :       return D/24 + mf2dimwt12(N, CHI, 1);
   10567         231 :     case mf_EISEN: D = dim22(N, F, 0) + dim22(N, F, 1);
   10568         231 :       if (D & 3L) pari_err_BUG("mfdim");
   10569         231 :       return (D >> 2) - mf2dimwt12(N, CHI, 3);
   10570             :   }
   10571             :   return 0; /*LCOV_EXCL_LINE*/
   10572             : }
   10573             : 
   10574             : /* F = conductor(CHI), weight k = r+1/2 */
   10575             : static long
   10576       43680 : checkmf2(long N, long r, GEN CHI, long F, long space)
   10577             : {
   10578       43680 :   switch(space)
   10579             :   {
   10580       43659 :     case mf_FULL: case mf_CUSP: case mf_EISEN: break;
   10581             :     case mf_NEW: case mf_OLD:
   10582          14 :       pari_err_TYPE("half-integral weight [new/old spaces]", utoi(space));
   10583             :     default:
   10584           7 :       pari_err_TYPE("half-integral weight [incorrect space]",utoi(space));
   10585             :   }
   10586       43659 :   if (N & 3L)
   10587           0 :     pari_err_DOMAIN("half-integral weight", "N % 4", "!=", gen_0, stoi(N));
   10588       43659 :   return r >= 0 && mfcharparity(CHI) == 1 && N % F == 0;
   10589             : }
   10590             : 
   10591             : /* weight k = r + 1/2 */
   10592             : static long
   10593       43463 : mf2dim_Nkchi(long N, long r, GEN CHI, ulong space)
   10594             : {
   10595       43463 :   long D, D2, F = mfcharconductor(CHI);
   10596       43463 :   if (!checkmf2(N, r, CHI, F, space)) return 0;
   10597