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.13.0 lcov report (development 25825-edc109b529) Lines: 7409 7598 97.5 %
Date: 2020-09-21 06:08:33 Functions: 757 762 99.3 %
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             : enum {
      23             :   MF_SPLIT = 1,
      24             :   MF_EISENSPACE,
      25             :   MF_FRICKE,
      26             :   MF_MF2INIT,
      27             :   MF_SPLITN
      28             : };
      29             : 
      30             : typedef struct {
      31             :   GEN vnew, vfull, DATA, VCHIP;
      32             :   long n, newHIT, newTOTAL, cuspHIT, cuspTOTAL;
      33             : } cachenew_t;
      34             : 
      35             : static void init_cachenew(cachenew_t *c, long n, long N, GEN f);
      36             : static GEN mfinit_i(GEN NK, long space);
      37             : static GEN mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw);
      38             : static GEN mf2init_Nkchi(long N, long k, GEN CHI, long space, long flraw);
      39             : static GEN mf2basis(long N, long r, GEN CHI, GEN *pCHI1, long space);
      40             : static GEN mfeisensteinbasis(long N, long k, GEN CHI);
      41             : static GEN mfeisensteindec(GEN mf, GEN F);
      42             : static GEN initwt1newtrace(GEN mf);
      43             : static GEN initwt1trace(GEN mf);
      44             : static GEN myfactoru(long N);
      45             : static GEN mydivisorsu(long N);
      46             : static GEN Qab_Czeta(long k, long ord, GEN C, long vt);
      47             : static GEN mfcoefs_i(GEN F, long n, long d);
      48             : static GEN bhnmat_extend(GEN M, long m,long l, GEN S, cachenew_t *cache);
      49             : static GEN initnewtrace(long N, GEN CHI);
      50             : static void dbg_cachenew(cachenew_t *C);
      51             : static GEN hecke_i(long m, long l, GEN V, GEN F, GEN DATA);
      52             : static GEN c_Ek(long n, long d, GEN F);
      53             : static GEN RgV_heckef2(long n, long d, GEN V, GEN F, GEN DATA);
      54             : static GEN mfcusptrace_i(long N, long k, long n, GEN Dn, GEN TDATA);
      55             : static GEN mfnewtracecache(long N, long k, long n, cachenew_t *cache);
      56             : static GEN colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *c);
      57             : static GEN dihan(GEN bnr, GEN w, GEN k0j, ulong n);
      58             : static GEN sigchi(long k, GEN CHI, long n);
      59             : static GEN sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord);
      60             : static GEN mflineardivtomat(long N, GEN vF, long n);
      61             : static GEN mfdihedralcusp(long N, GEN CHI);
      62             : static long mfdihedralcuspdim(long N, GEN CHI);
      63             : static GEN mfdihedralnew(long N, GEN CHI);
      64             : static GEN mfdihedralall(GEN LIM);
      65             : static long mfwt1cuspdim(long N, GEN CHI);
      66             : static long mf2dim_Nkchi(long N, long k, GEN CHI, ulong space);
      67             : static long mfdim_Nkchi(long N, long k, GEN CHI, long space);
      68             : static GEN charLFwtk(long N, long k, GEN CHI, long ord, long t);
      69             : static GEN mfeisensteingacx(GEN E,long w,GEN ga,long n,long prec);
      70             : static GEN mfgaexpansion(GEN mf, GEN F, GEN gamma, long n, long prec);
      71             : static GEN mfEHmat(long n, long r);
      72             : static GEN mfEHcoef(long r, long N);
      73             : static GEN mftobasis_i(GEN mf, GEN F);
      74             : 
      75             : static GEN
      76       32123 : mkgNK(GEN N, GEN k, GEN CHI, GEN P) { return mkvec4(N, k, CHI, P); }
      77             : static GEN
      78       13419 : mkNK(long N, long k, GEN CHI) { return mkgNK(stoi(N), stoi(k), CHI, pol_x(1)); }
      79             : GEN
      80        7483 : MF_get_CHI(GEN mf) { return gmael(mf,1,3); }
      81             : GEN
      82       18536 : MF_get_gN(GEN mf) { return gmael(mf,1,1); }
      83             : long
      84       17661 : MF_get_N(GEN mf) { return itou(MF_get_gN(mf)); }
      85             : GEN
      86       12810 : MF_get_gk(GEN mf) { return gmael(mf,1,2); }
      87             : long
      88        6279 : MF_get_k(GEN mf)
      89             : {
      90        6279 :   GEN gk = MF_get_gk(mf);
      91        6279 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
      92        6279 :   return itou(gk);
      93             : }
      94             : long
      95         238 : MF_get_r(GEN mf)
      96             : {
      97         238 :   GEN gk = MF_get_gk(mf);
      98         238 :   if (typ(gk) == t_INT) pari_err_IMPL("integral weight");
      99         238 :   return itou(gel(gk, 1)) >> 1;
     100             : }
     101             : long
     102       12796 : MF_get_space(GEN mf) { return itos(gmael(mf,1,4)); }
     103             : GEN
     104        3864 : MF_get_E(GEN mf) { return gel(mf,2); }
     105             : GEN
     106       18921 : MF_get_S(GEN mf) { return gel(mf,3); }
     107             : GEN
     108        1414 : MF_get_basis(GEN mf) { return shallowconcat(gel(mf,2), gel(mf,3)); }
     109             : long
     110        4676 : MF_get_dim(GEN mf)
     111             : {
     112        4676 :   switch(MF_get_space(mf))
     113             :   {
     114         686 :     case mf_FULL:
     115         686 :       return lg(MF_get_S(mf)) - 1 + lg(MF_get_E(mf))-1;
     116         140 :     case mf_EISEN:
     117         140 :       return lg(MF_get_E(mf))-1;
     118        3850 :     default: /* mf_NEW, mf_CUSP, mf_OLD */
     119        3850 :       return lg(MF_get_S(mf)) - 1;
     120             :   }
     121             : }
     122             : GEN
     123        6867 : MFnew_get_vj(GEN mf) { return gel(mf,4); }
     124             : GEN
     125         490 : MFcusp_get_vMjd(GEN mf) { return gel(mf,4); }
     126             : GEN
     127        6244 : MF_get_M(GEN mf) { return gmael(mf,5,3); }
     128             : GEN
     129        2527 : MF_get_Minv(GEN mf) { return gmael(mf,5,2); }
     130             : GEN
     131        8106 : MF_get_Mindex(GEN mf) { return gmael(mf,5,1); }
     132             : 
     133             : /* ordinary gtocol forgets about initial 0s */
     134             : GEN
     135        2023 : sertocol(GEN S) { return gtocol0(S, -(lg(S) - 2 + valp(S))); }
     136             : /*******************************************************************/
     137             : /*     Linear algebra in cyclotomic fields (TODO: export this)     */
     138             : /*******************************************************************/
     139             : /* return r and split prime p giving projection Q(zeta_n) -> Fp, zeta -> r */
     140             : static ulong
     141         770 : QabM_init(long n, ulong *p)
     142             : {
     143         770 :   ulong pinit = 1000000007;
     144             :   forprime_t T;
     145         770 :   if (n <= 1) { *p = pinit; return 0; }
     146         763 :   u_forprime_arith_init(&T, pinit, ULONG_MAX, 1, n);
     147         763 :   *p = u_forprime_next(&T);
     148         763 :   return Flx_oneroot(ZX_to_Flx(polcyclo(n, 0), *p), *p);
     149             : }
     150             : static ulong
     151      645414 : Qab_to_Fl(GEN P, ulong r, ulong p)
     152             : {
     153             :   ulong t;
     154             :   GEN den;
     155      645414 :   P = Q_remove_denom(liftpol_shallow(P), &den);
     156      645414 :   if (typ(P) == t_POL) { GEN Pp = ZX_to_Flx(P, p); t = Flx_eval(Pp, r, p); }
     157      624127 :   else t = umodiu(P, p);
     158      645414 :   if (den) t = Fl_div(t, umodiu(den, p), p);
     159      645414 :   return t;
     160             : }
     161             : static GEN
     162       14917 : QabC_to_Flc(GEN C, ulong r, ulong p)
     163             : {
     164       14917 :   long i, l = lg(C);
     165       14917 :   GEN A = cgetg(l, t_VECSMALL);
     166      620207 :   for (i = 1; i < l; i++) uel(A,i) = Qab_to_Fl(gel(C,i), r, p);
     167       14917 :   return A;
     168             : }
     169             : static GEN
     170         385 : QabM_to_Flm(GEN M, ulong r, ulong p)
     171             : {
     172             :   long i, l;
     173         385 :   GEN A = cgetg_copy(M, &l);
     174       15302 :   for (i = 1; i < l; i++)
     175       14917 :     gel(A, i) = QabC_to_Flc(gel(M, i), r, p);
     176         385 :   return A;
     177             : }
     178             : /* A a t_POL */
     179             : static GEN
     180         553 : QabX_to_Flx(GEN A, ulong r, ulong p)
     181             : {
     182         553 :   long i, l = lg(A);
     183         553 :   GEN a = cgetg(l, t_VECSMALL);
     184         553 :   a[1] = ((ulong)A[1])&VARNBITS;
     185       40677 :   for (i = 2; i < l; i++) uel(a,i) = Qab_to_Fl(gel(A,i), r, p);
     186         553 :   return Flx_renormalize(a, l);
     187             : }
     188             : 
     189             : /* FIXME: remove */
     190             : static GEN
     191         966 : ZabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *den, int ratlift)
     192             : {
     193         966 :   GEN v = ZabM_indexrank(M, P, n);
     194         966 :   if (pv) *pv = v;
     195         966 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
     196         966 :   return ratlift? ZabM_inv_ratlift(M, P, n, den): ZabM_inv(M, P, n, den);
     197             : }
     198             : 
     199             : /* M matrix with coeff in Q(\chi)), where Q(\chi) = Q(X)/(P) for
     200             :  * P = cyclotomic Phi_n. Assume M rational if n <= 2 */
     201             : static GEN
     202        1701 : QabM_ker(GEN M, GEN P, long n)
     203             : {
     204        1701 :   if (n <= 2) return QM_ker(M);
     205         742 :   return ZabM_ker(Q_primpart(liftpol_shallow(M)), P, n);
     206             : }
     207             : /* pseudo-inverse of M. FIXME: should replace QabM_pseudoinv */
     208             : static GEN
     209        1225 : QabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     210             : {
     211             :   GEN cM, Mi;
     212        1225 :   if (n <= 2)
     213             :   {
     214         938 :     M = Q_primitive_part(M, &cM);
     215         938 :     Mi = ZM_pseudoinv(M, pv, pden); /* M^(-1) = Mi / (cM * den) */
     216             :   }
     217             :   else
     218             :   {
     219         287 :     M = Q_primitive_part(liftpol_shallow(M), &cM);
     220         287 :     Mi = ZabM_pseudoinv(M, P, n, pv, pden);
     221             :   }
     222        1225 :   *pden = mul_content(*pden, cM);
     223        1225 :   return Mi;
     224             : }
     225             : /* FIXME: delete */
     226             : static GEN
     227         994 : QabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     228             : {
     229         994 :   GEN Mi = QabM_pseudoinv_i(M, P, n, pv, pden);
     230         994 :   return P? gmodulo(Mi, P): Mi;
     231             : }
     232             : 
     233             : static GEN
     234        9765 : QabM_indexrank(GEN M, GEN P, long n)
     235             : {
     236             :   GEN z;
     237        9765 :   if (n <= 2)
     238             :   {
     239        8666 :     M = vec_Q_primpart(M);
     240        8666 :     z = ZM_indexrank(M); /* M^(-1) = Mi / (cM * den) */
     241             :   }
     242             :   else
     243             :   {
     244        1099 :     M = vec_Q_primpart(liftpol_shallow(M));
     245        1099 :     z = ZabM_indexrank(M, P, n);
     246             :   }
     247        9765 :   return z;
     248             : }
     249             : 
     250             : /*********************************************************************/
     251             : /*                    Simple arithmetic functions                    */
     252             : /*********************************************************************/
     253             : /* TODO: most of these should be exported and used in ifactor1.c */
     254             : /* phi(n) */
     255             : static ulong
     256      105399 : myeulerphiu(ulong n)
     257             : {
     258             :   pari_sp av;
     259      105399 :   if (n == 1) return 1;
     260       87129 :   av = avma; return gc_ulong(av, eulerphiu_fact(myfactoru(n)));
     261             : }
     262             : static long
     263       65562 : mymoebiusu(ulong n)
     264             : {
     265             :   pari_sp av;
     266       65562 :   if (n == 1) return 1;
     267       54068 :   av = avma; return gc_long(av, moebiusu_fact(myfactoru(n)));
     268             : }
     269             : 
     270             : static long
     271        2842 : mynumdivu(long N)
     272             : {
     273             :   pari_sp av;
     274        2842 :   if (N == 1) return 1;
     275        2737 :   av = avma; return gc_long(av, numdivu_fact(myfactoru(N)));
     276             : }
     277             : 
     278             : /* N\prod_{p|N} (1+1/p) */
     279             : static long
     280      337162 : mypsiu(ulong N)
     281             : {
     282      337162 :   pari_sp av = avma;
     283      337162 :   GEN P = gel(myfactoru(N), 1);
     284      337162 :   long j, l = lg(P), res = N;
     285      716198 :   for (j = 1; j < l; j++) res += res/P[j];
     286      337162 :   return gc_long(av,res);
     287             : }
     288             : /* write n = mf^2. Return m, set f. */
     289             : static ulong
     290         210 : mycore(ulong n, long *pf)
     291             : {
     292         210 :   pari_sp av = avma;
     293         210 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     294         210 :   long i, l = lg(P), m = 1, f = 1;
     295         850 :   for (i = 1; i < l; i++)
     296             :   {
     297         640 :     long j, p = P[i], e = E[i];
     298         640 :     if (e & 1) m *= p;
     299        1190 :     for (j = 2; j <= e; j+=2) f *= p;
     300             :   }
     301         210 :   *pf = f; return gc_long(av,m);
     302             : }
     303             : 
     304             : /* fa = factorization of -D > 0, return -D0 > 0 (where D0 is fundamental) */
     305             : static long
     306     8253259 : corediscs_fact(GEN fa)
     307             : {
     308     8253259 :   GEN P = gel(fa,1), E = gel(fa,2);
     309     8253259 :   long i, l = lg(P), m = 1;
     310    27307672 :   for (i = 1; i < l; i++)
     311             :   {
     312    19054413 :     long p = P[i], e = E[i];
     313    19054413 :     if (e & 1) m *= p;
     314             :   }
     315     8253259 :   if ((m&3L) != 3) m <<= 2;
     316     8253259 :   return m;
     317             : }
     318             : static long
     319        6573 : mubeta(long n)
     320             : {
     321        6573 :   pari_sp av = avma;
     322        6573 :   GEN E = gel(myfactoru(n), 2);
     323        6573 :   long i, s = 1, l = lg(E);
     324       13636 :   for (i = 1; i < l; i++)
     325             :   {
     326        7063 :     long e = E[i];
     327        7063 :     if (e >= 3) return gc_long(av,0);
     328        7063 :     if (e == 1) s *= -2;
     329             :   }
     330        6573 :   return gc_long(av,s);
     331             : }
     332             : 
     333             : /* n = n1*n2, n1 = ppo(n, m); return mubeta(n1)*moebiusu(n2).
     334             :  * N.B. If n from newt_params we, in fact, never return 0 */
     335             : static long
     336     5794061 : mubeta2(long n, long m)
     337             : {
     338     5794061 :   pari_sp av = avma;
     339     5794061 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     340     5794061 :   long i, s = 1, l = lg(P);
     341    11559926 :   for (i = 1; i < l; i++)
     342             :   {
     343     5765865 :     long p = P[i], e = E[i];
     344     5765865 :     if (m % p)
     345             :     { /* p^e in n1 */
     346     4736851 :       if (e >= 3) return gc_long(av,0);
     347     4736851 :       if (e == 1) s *= -2;
     348             :     }
     349             :     else
     350             :     { /* in n2 */
     351     1029014 :       if (e >= 2) return gc_long(av,0);
     352     1029014 :       s = -s;
     353             :     }
     354             :   }
     355     5794061 :   return gc_long(av,s);
     356             : }
     357             : 
     358             : /* write N = prod p^{ep} and n = df^2, d squarefree.
     359             :  * set g  = ppo(gcd(sqfpart(N), f), FC)
     360             :  *     N2 = prod p^if(e==1 || p|n, ep-1, ep-2) */
     361             : static void
     362     1428294 : newt_params(long N, long n, long FC, long *pg, long *pN2)
     363             : {
     364     1428294 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     365     1428294 :   long i, g = 1, N2 = 1, l = lg(P);
     366     3716811 :   for (i = 1; i < l; i++)
     367             :   {
     368     2288517 :     long p = P[i], e = E[i];
     369     2288517 :     if (e == 1)
     370     1962499 :     { if (FC % p && n % (p*p) == 0) g *= p; }
     371             :     else
     372      326018 :       N2 *= upowuu(p,(n % p)? e-2: e-1);
     373             :   }
     374     1428294 :   *pg = g; *pN2 = N2;
     375     1428294 : }
     376             : /* simplified version of newt_params for n = 1 (newdim) */
     377             : static void
     378       38332 : newd_params(long N, long *pN2)
     379             : {
     380       38332 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     381       38332 :   long i, N2 = 1, l = lg(P);
     382       96208 :   for (i = 1; i < l; i++)
     383             :   {
     384       57876 :     long p = P[i], e = E[i];
     385       57876 :     if (e > 2) N2 *= upowuu(p, e-2);
     386             :   }
     387       38332 :   *pN2 = N2;
     388       38332 : }
     389             : 
     390             : static long
     391          21 : newd_params2(long N)
     392             : {
     393          21 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     394          21 :   long i, N2 = 1, l = lg(P);
     395          56 :   for (i = 1; i < l; i++)
     396             :   {
     397          35 :     long p = P[i], e = E[i];
     398          35 :     if (e >= 2) N2 *= upowuu(p, e);
     399             :   }
     400          21 :   return N2;
     401             : }
     402             : 
     403             : /*******************************************************************/
     404             : /*   Relative trace between cyclotomic fields (TODO: export this)  */
     405             : /*******************************************************************/
     406             : /* g>=1; return g * prod_{p | g, (p,q) = 1} (1-1/p) */
     407             : static long
     408       36834 : phipart(long g, long q)
     409             : {
     410       36834 :   if (g > 1)
     411             :   {
     412       19642 :     GEN P = gel(myfactoru(g), 1);
     413       19642 :     long i, l = lg(P);
     414       40138 :     for (i = 1; i < l; i++) { long p = P[i]; if (q % p) g -= g / p; }
     415             :   }
     416       36834 :   return g;
     417             : }
     418             : /* Set s,v s.t. Trace(zeta_N^k) from Q(zeta_N) to Q(\zeta_N) = s * zeta_M^v
     419             :  * With k > 0, N = M*d and N, M != 2 mod 4 */
     420             : static long
     421       84609 : tracerelz(long *pv, long d, long M, long k)
     422             : {
     423             :   long s, g, q, muq;
     424       84609 :   if (d == 1) { *pv = k; return 1; }
     425       65471 :   *pv = 0; g = ugcd(k, d); q = d / g;
     426       65471 :   muq = mymoebiusu(q); if (!muq) return 0;
     427       47096 :   if (M != 1)
     428             :   {
     429       37758 :     long v = Fl_invsafe(q % M, M);
     430       37758 :     if (!v) return 0;
     431       27496 :     *pv = (v * (k/g)) % M;
     432             :   }
     433       36834 :   s = phipart(g, M*q); if (muq < 0) s = -s;
     434       36834 :   return s;
     435             : }
     436             : /* Pi = polcyclo(i), i = m or n. Let Ki = Q(zeta_i), initialize Tr_{Kn/Km} */
     437             : GEN
     438       33425 : Qab_trace_init(long n, long m, GEN Pn, GEN Pm)
     439             : {
     440             :   long a, i, j, N, M, vt, d, D;
     441             :   GEN T, G;
     442             : 
     443       33425 :   if (m == n || n <= 2) return mkvec(Pm);
     444       16485 :   vt = varn(Pn);
     445       16485 :   d = degpol(Pn);
     446             :   /* if (N != n) zeta_N = zeta_n^2 and zeta_n = - zeta_N^{(N+1)/2} */
     447       16485 :   N = ((n & 3) == 2)? n >> 1: n;
     448       16485 :   M = ((m & 3) == 2)? m >> 1: m; /* M | N | n */
     449       16485 :   a = N / M;
     450       16485 :   T = const_vec(d, NULL);
     451       16485 :   D = d / degpol(Pm); /* relative degree */
     452       16485 :   if (D == 1) G = NULL;
     453             :   else
     454             :   { /* zeta_M = zeta_n^A; s_j(zeta_M) = zeta_M <=> j = 1 (mod J) */
     455       15211 :     long lG, A = (N == n)? a: (a << 1), J = n / ugcd(n, A);
     456       15211 :     G = coprimes_zv(n);
     457      149912 :     for (j = lG = 1; j < n; j += J)
     458      134701 :       if (G[j]) G[lG++] = j;
     459       15211 :     setlg(G, lG); /* Gal(Q(zeta_n) / Q(zeta_m)) */
     460             :   }
     461       16485 :   T = const_vec(d, NULL);
     462       16485 :   gel(T,1) = utoipos(D); /* Tr 1 */
     463      139902 :   for (i = 1; i < d; i++)
     464             :   { /* if n = 2N, zeta_n^i = (-1)^i zeta_N^k */
     465             :     long s, v, k;
     466             :     GEN t;
     467             : 
     468      123417 :     if (gel(T, i+1)) continue;
     469       84609 :     k = (N == n)? i: ((odd(i)? i + N: i) >> 1);
     470       84609 :     if ((s = tracerelz(&v, a, M, k)))
     471             :     {
     472       55972 :       if (m != M) v *= 2;/* Tr = s * zeta_m^v */
     473       55972 :       if (n != N && odd(i)) s = -s;
     474       55972 :       t = Qab_Czeta(v, m, stoi(s), vt);
     475             :     }
     476             :     else
     477       28637 :       t = gen_0;
     478             :     /* t = Tr_{Kn/Km} zeta_n^i; fill using Galois action */
     479       84609 :     if (!G)
     480       19138 :       gel(T, i + 1) = t;
     481             :     else
     482      370349 :       for (j = 1; j <= D; j++)
     483             :       {
     484      304878 :         long z = Fl_mul(i,G[j], n);
     485      304878 :         if (z < d) gel(T, z + 1) = t;
     486             :       }
     487             :   }
     488       16485 :   return mkvec3(Pm, Pn, T);
     489             : }
     490             : /* x a t_POL modulo Phi_n */
     491             : static GEN
     492       59143 : tracerel_i(GEN T, GEN x)
     493             : {
     494       59143 :   long k, l = lg(x);
     495             :   GEN S;
     496       59143 :   if (l == 2) return gen_0;
     497       59143 :   S = gmul(gel(T,1), gel(x,2));
     498      249963 :   for (k = 3; k < l; k++) S = gadd(S, gmul(gel(T,k-1), gel(x,k)));
     499       59143 :   return S;
     500             : }
     501             : static GEN
     502      126406 : tracerel(GEN a, GEN v, GEN z)
     503             : {
     504      126406 :   a = liftpol_shallow(a);
     505      126406 :   a = simplify_shallow(z? gmul(z,a): a);
     506      126406 :   if (typ(a) == t_POL)
     507             :   {
     508       59143 :     GEN T = gel(v,3);
     509       59143 :     long degrel = itou(gel(T,1));
     510       59143 :     a = tracerel_i(T, RgX_rem(a, gel(v,2)));
     511       59143 :     if (degrel != 1) a = gdivgs(a, degrel);
     512       59143 :     if (typ(a) == t_POL) a = RgX_rem(a, gel(v,1));
     513             :   }
     514      126406 :   return a;
     515             : }
     516             : static GEN
     517        5348 : tracerel_z(GEN v, long t)
     518             : {
     519        5348 :   GEN Pn = gel(v,2);
     520        5348 :   return t? pol_xn(t, varn(Pn)): NULL;
     521             : }
     522             : /* v = Qab_trace_init(n,m); x is a t_VEC of polmodulo Phi_n; Kn = Q(zeta_n)
     523             :  * [Kn:Km]^(-1) Tr_{Kn/Km} (zeta_n^t * x); 0 <= t < [Kn:Km] */
     524             : GEN
     525           0 : Qab_tracerel(GEN v, long t, GEN a)
     526             : {
     527           0 :   if (lg(v) != 4) return a; /* => t = 0 */
     528           0 :   return tracerel(a, v, tracerel_z(v, t));
     529             : }
     530             : GEN
     531       12348 : QabV_tracerel(GEN v, long t, GEN x)
     532             : {
     533             :   GEN z;
     534       12348 :   if (lg(v) != 4) return x; /* => t = 0 */
     535        5348 :   z = tracerel_z(v, t);
     536      131754 :   pari_APPLY_same(tracerel(gel(x,i), v, z));
     537             : }
     538             : GEN
     539         133 : QabM_tracerel(GEN v, long t, GEN x)
     540             : {
     541         133 :   if (lg(v) != 4) return x;
     542         105 :   pari_APPLY_same(QabV_tracerel(v, t, gel(x,i)));
     543             : }
     544             : 
     545             : /* C*zeta_o^k mod X^o - 1 */
     546             : static GEN
     547     1020215 : Qab_Czeta(long k, long o, GEN C, long vt)
     548             : {
     549     1020215 :   if (!k) return C;
     550      557291 :   if (!odd(o))
     551             :   { /* optimization: reduce max degree by a factor 2 for free */
     552      501382 :     o >>= 1;
     553      501382 :     if (k >= o) { k -= o; C = gneg(C); if (!k) return C; }
     554             :   }
     555      369985 :   return monomial(C, k, vt);
     556             : }
     557             : /* zeta_o^k */
     558             : static GEN
     559      148393 : Qab_zeta(long k, long o, long vt) { return Qab_Czeta(k, o, gen_1, vt); }
     560             : 
     561             : /*              Operations on Dirichlet characters                       */
     562             : 
     563             : /* A Dirichlet character can be given in GP in different formats, but in this
     564             :  * package, it will be a vector CHI=[G,chi,ord], where G is the (Z/MZ)^* to
     565             :  * which the character belongs, chi is the character in Conrey format, ord is
     566             :  * the order */
     567             : 
     568             : static GEN
     569     1663830 : gmfcharorder(GEN CHI) { return gel(CHI, 3); }
     570             : long
     571     1628396 : mfcharorder(GEN CHI) { return itou(gmfcharorder(CHI)); }
     572             : static long
     573        9982 : mfcharistrivial(GEN CHI) { return !CHI || mfcharorder(CHI) == 1; }
     574             : static GEN
     575      872543 : gmfcharmodulus(GEN CHI) { return gmael3(CHI, 1, 1, 1); }
     576             : long
     577      872543 : mfcharmodulus(GEN CHI) { return itou(gmfcharmodulus(CHI)); }
     578             : GEN
     579      322812 : mfcharpol(GEN CHI) { return gel(CHI,4); }
     580             : 
     581             : /* vz[i+1] = image of (zeta_o)^i in Fp */
     582             : static ulong
     583      219611 : Qab_Czeta_Fl(long k, GEN vz, ulong C, ulong p)
     584             : {
     585             :   long o;
     586      219611 :   if (!k) return C;
     587      148484 :   o = lg(vz)-2;
     588      148484 :   if ((k << 1) == o) return Fl_neg(C,p);
     589      123123 :   return Fl_mul(C, vz[k+1], p);
     590             : }
     591             : 
     592             : static long
     593      885430 : znchareval_i(GEN CHI, long n, GEN ord)
     594      885430 : { return itos(znchareval(gel(CHI,1), gel(CHI,2), stoi(n), ord)); }
     595             : 
     596             : /* n coprime with the modulus of CHI */
     597             : static GEN
     598       12488 : mfchareval(GEN CHI, long n)
     599             : {
     600       12488 :   GEN Pn, C, go = gmfcharorder(CHI);
     601       12488 :   long k, o = go[2];
     602       12488 :   if (o == 1) return gen_1;
     603        5999 :   k = znchareval_i(CHI, n, go);
     604        5999 :   Pn = mfcharpol(CHI);
     605        5999 :   C = Qab_zeta(k, o, varn(Pn));
     606        5999 :   if (typ(C) != t_POL) return C;
     607        4872 :   return gmodulo(C, Pn);
     608             : }
     609             : /* d a multiple of ord(CHI); n coprime with char modulus;
     610             :  * return x s.t. CHI(n) = \zeta_d^x] */
     611             : static long
     612     1511874 : mfcharevalord(GEN CHI, long n, long d)
     613             : {
     614     1511874 :   if (mfcharorder(CHI) == 1) return 0;
     615      875735 :   return znchareval_i(CHI, n, utoi(d));
     616             : }
     617             : 
     618             : /* G a znstar, L a Conrey log: return a 'mfchar' */
     619             : static GEN
     620      371021 : mfcharGL(GEN G, GEN L)
     621             : {
     622      371021 :   GEN o = zncharorder(G,L);
     623      371021 :   long ord = itou(o), vt = fetch_user_var("t");
     624      371021 :   return mkvec4(G, L, o, polcyclo(ord,vt));
     625             : }
     626             : static GEN
     627        5033 : mfchartrivial()
     628        5033 : { return mfcharGL(znstar0(gen_1,1), cgetg(1,t_COL)); }
     629             : /* convert a generic character into an 'mfchar' */
     630             : static GEN
     631        3934 : get_mfchar(GEN CHI)
     632             : {
     633             :   GEN G, L;
     634        3934 :   if (typ(CHI) != t_VEC) CHI = znchar(CHI);
     635             :   else
     636             :   {
     637         889 :     long l = lg(CHI);
     638         889 :     if ((l != 3 && l != 5) || !checkznstar_i(gel(CHI,1)))
     639           7 :       pari_err_TYPE("checkNF [chi]", CHI);
     640         882 :     if (l == 5) return CHI;
     641             :   }
     642        3864 :   G = gel(CHI,1);
     643        3864 :   L = gel(CHI,2); if (typ(L) != t_COL) L = znconreylog(G,L);
     644        3864 :   return mfcharGL(G, L);
     645             : }
     646             : 
     647             : /* parse [N], [N,k], [N,k,CHI]. If 'joker' is set, allow wildcard for CHI */
     648             : static GEN
     649        9072 : checkCHI(GEN NK, long N, int joker)
     650             : {
     651             :   GEN CHI;
     652        9072 :   if (lg(NK) == 3)
     653         623 :     CHI = mfchartrivial();
     654             :   else
     655             :   {
     656             :     long i, l;
     657        8449 :     CHI = gel(NK,3); l = lg(CHI);
     658        8449 :     if (isintzero(CHI) && joker)
     659        4095 :       CHI = NULL; /* all character orbits */
     660        4354 :     else if (isintm1(CHI) && joker > 1)
     661        2373 :       CHI = gen_m1; /* sum over all character orbits */
     662        2114 :     else if ((typ(CHI) == t_VEC &&
     663         217 :              (l == 1 || l != 3 || !checkznstar_i(gel(CHI,1)))) && joker)
     664             :     {
     665         133 :       CHI = shallowtrans(CHI); /* list of characters */
     666         952 :       for (i = 1; i < l; i++) gel(CHI,i) = get_mfchar(gel(CHI,i));
     667             :     }
     668             :     else
     669             :     {
     670        1848 :       CHI = get_mfchar(CHI); /* single char */
     671        1848 :       if (N % mfcharmodulus(CHI)) pari_err_TYPE("checkNF [chi]", NK);
     672             :     }
     673             :   }
     674        9058 :   return CHI;
     675             : }
     676             : /* support half-integral weight */
     677             : static void
     678        9079 : checkNK2(GEN NK, long *N, long *nk, long *dk, GEN *CHI, int joker)
     679             : {
     680        9079 :   long l = lg(NK);
     681             :   GEN T;
     682        9079 :   if (typ(NK) != t_VEC || l < 3 || l > 4) pari_err_TYPE("checkNK", NK);
     683        9079 :   T = gel(NK,1); if (typ(T) != t_INT) pari_err_TYPE("checkNF [N]", NK);
     684        9079 :   *N = itos(T); if (*N <= 0) pari_err_TYPE("checkNF [N <= 0]", NK);
     685        9079 :   T = gel(NK,2);
     686        9079 :   switch(typ(T))
     687             :   {
     688        5705 :     case t_INT:  *nk = itos(T); *dk = 1; break;
     689        3367 :     case t_FRAC:
     690        3367 :       *nk = itos(gel(T,1));
     691        3367 :       *dk = itou(gel(T,2)); if (*dk == 2) break;
     692           7 :     default: pari_err_TYPE("checkNF [k]", NK);
     693             :   }
     694        9072 :   *CHI = checkCHI(NK, *N, joker);
     695        9058 : }
     696             : /* don't support half-integral weight */
     697             : static void
     698         126 : checkNK(GEN NK, long *N, long *k, GEN *CHI, int joker)
     699             : {
     700             :   long d;
     701         126 :   checkNK2(NK, N, k, &d, CHI, joker);
     702         126 :   if (d != 1) pari_err_TYPE("checkNF [k]", NK);
     703         126 : }
     704             : 
     705             : static GEN
     706        4851 : mfchargalois(long N, int odd, GEN flagorder)
     707             : {
     708        4851 :   GEN G = znstar0(utoi(N), 1), L = chargalois(G, flagorder);
     709        4851 :   long l = lg(L), i, j;
     710      112735 :   for (i = j = 1; i < l; i++)
     711             :   {
     712      107884 :     GEN chi = znconreyfromchar(G, gel(L,i));
     713      107884 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
     714             :   }
     715        4851 :   setlg(L, j); return L;
     716             : }
     717             : /* possible characters for non-trivial S_1(N, chi) */
     718             : static GEN
     719        1708 : mfwt1chars(long N, GEN vCHI)
     720             : {
     721        1708 :   if (vCHI) return vCHI; /*do not filter, user knows best*/
     722             :   /* Tate's theorem */
     723        1638 :   return mfchargalois(N, 1, uisprime(N)? mkvecsmall2(2,4): NULL);
     724             : }
     725             : static GEN
     726        3255 : mfchars(long N, long k, long dk, GEN vCHI)
     727        3255 : { return vCHI? vCHI: mfchargalois(N, (dk == 2)? 0: (k & 1), NULL); }
     728             : 
     729             : /* wrappers from mfchar to znchar */
     730             : static long
     731       66815 : mfcharparity(GEN CHI)
     732             : {
     733       66815 :   if (!CHI) return 1;
     734       66815 :   return zncharisodd(gel(CHI,1), gel(CHI,2)) ? -1 : 1;
     735             : }
     736             : /* if CHI is primitive, return CHI itself, not a copy */
     737             : static GEN
     738       71099 : mfchartoprimitive(GEN CHI, long *pF)
     739             : {
     740             :   pari_sp av;
     741             :   GEN chi, F;
     742       71099 :   if (!CHI) { if (pF) *pF = 1; return mfchartrivial(); }
     743       71099 :   av = avma; F = znconreyconductor(gel(CHI,1), gel(CHI,2), &chi);
     744       71099 :   if (typ(F) == t_INT) set_avma(av);
     745             :   else
     746             :   {
     747        7462 :     CHI = leafcopy(CHI);
     748        7462 :     gel(CHI,1) = znstar0(F, 1);
     749        7462 :     gel(CHI,2) = chi;
     750             :   }
     751       71099 :   if (pF) *pF = mfcharmodulus(CHI);
     752       71099 :   return CHI;
     753             : }
     754             : static long
     755      394317 : mfcharconductor(GEN CHI)
     756             : {
     757      394317 :   pari_sp av = avma;
     758      394317 :   GEN res = znconreyconductor(gel(CHI,1), gel(CHI,2), NULL);
     759      394317 :   if (typ(res) == t_VEC) res = gel(res, 1);
     760      394317 :   return gc_long(av, itos(res));
     761             : }
     762             : 
     763             : /*                      Operations on mf closures                    */
     764             : static GEN
     765       54271 : tagparams(long t, GEN NK) { return mkvec2(mkvecsmall(t), NK); }
     766             : static GEN
     767        1078 : lfuntag(long t, GEN x) { return mkvec2(mkvecsmall(t), x); }
     768             : static GEN
     769          56 : tag0(long t, GEN NK) { retmkvec(tagparams(t,NK)); }
     770             : static GEN
     771        9730 : tag(long t, GEN NK, GEN x) { retmkvec2(tagparams(t,NK), x); }
     772             : static GEN
     773       30632 : tag2(long t, GEN NK, GEN x, GEN y) { retmkvec3(tagparams(t,NK), x,y); }
     774             : static GEN
     775       13741 : tag3(long t, GEN NK, GEN x,GEN y,GEN z) { retmkvec4(tagparams(t,NK), x,y,z); }
     776             : static GEN
     777           0 : tag4(long t, GEN NK, GEN x,GEN y,GEN z,GEN a)
     778           0 : { retmkvec5(tagparams(t,NK), x,y,z,a); }
     779             : /* is F a "modular form" ? */
     780             : int
     781       16625 : checkmf_i(GEN F)
     782       16625 : { return typ(F) == t_VEC
     783       15995 :     && lg(F) > 1 && typ(gel(F,1)) == t_VEC
     784       11781 :     && lg(gel(F,1)) == 3
     785       11620 :     && typ(gmael(F,1,1)) == t_VECSMALL
     786       32620 :     && typ(gmael(F,1,2)) == t_VEC; }
     787      182161 : long mf_get_type(GEN F) { return gmael(F,1,1)[1]; }
     788      144697 : GEN mf_get_gN(GEN F) { return gmael3(F,1,2,1); }
     789      110222 : GEN mf_get_gk(GEN F) { return gmael3(F,1,2,2); }
     790             : /* k - 1/2, assume k in 1/2 + Z */
     791         413 : long mf_get_r(GEN F) { return itou(gel(mf_get_gk(F),1)) >> 1; }
     792       93149 : long mf_get_N(GEN F) { return itou(mf_get_gN(F)); }
     793       59234 : long mf_get_k(GEN F)
     794             : {
     795       59234 :   GEN gk = mf_get_gk(F);
     796       59234 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
     797       59234 :   return itou(gk);
     798             : }
     799       41804 : GEN mf_get_CHI(GEN F) { return gmael3(F,1,2,3); }
     800       21007 : GEN mf_get_field(GEN F) { return gmael3(F,1,2,4); }
     801       16660 : GEN mf_get_NK(GEN F) { return gmael(F,1,2); }
     802             : static void
     803         504 : mf_setfield(GEN f, GEN P)
     804             : {
     805         504 :   gel(f,1) = leafcopy(gel(f,1));
     806         504 :   gmael(f,1,2) = leafcopy(gmael(f,1,2));
     807         504 :   gmael3(f,1,2,4) = P;
     808         504 : }
     809             : 
     810             : /* UTILITY FUNCTIONS */
     811             : GEN
     812        5831 : mftocol(GEN F, long lim, long d)
     813        5831 : { GEN c = mfcoefs_i(F, lim, d); settyp(c,t_COL); return c; }
     814             : GEN
     815        1428 : mfvectomat(GEN vF, long lim, long d)
     816             : {
     817        1428 :   long j, l = lg(vF);
     818        1428 :   GEN M = cgetg(l, t_MAT);
     819        7049 :   for (j = 1; j < l; j++) gel(M,j) = mftocol(gel(vF,j), lim, d);
     820        1428 :   return M;
     821             : }
     822             : 
     823             : static GEN
     824        4186 : RgV_to_ser_full(GEN x) { return RgV_to_ser(x, 0, lg(x)+1); }
     825             : /* TODO: delete */
     826             : static GEN
     827         560 : mfcoefsser(GEN F, long n) { return RgV_to_ser_full(mfcoefs_i(F,n,1)); }
     828             : static GEN
     829         833 : sertovecslice(GEN S, long n)
     830             : {
     831         833 :   GEN v = gtovec0(S, -(lg(S) - 2 + valp(S)));
     832         833 :   long l = lg(v), n2 = n + 2;
     833         833 :   if (l < n2) pari_err_BUG("sertovecslice [n too large]");
     834         833 :   return (l == n2)? v: vecslice(v, 1, n2-1);
     835             : }
     836             : 
     837             : /* a, b two RgV of the same length, multiply as truncated power series */
     838             : static GEN
     839        3318 : RgV_mul_RgXn(GEN a, GEN b)
     840             : {
     841        3318 :   long n = lg(a)-1;
     842             :   GEN c;
     843        3318 :   a = RgV_to_RgX(a,0);
     844        3318 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, b, n);
     845        3318 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     846             : }
     847             : /* divide as truncated power series */
     848             : static GEN
     849         357 : RgV_div_RgXn(GEN a, GEN b)
     850             : {
     851         357 :   long n = lg(a)-1;
     852             :   GEN c;
     853         357 :   a = RgV_to_RgX(a,0);
     854         357 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, RgXn_inv(b,n), n);
     855         357 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     856             : }
     857             : /* a^b */
     858             : static GEN
     859          84 : RgV_pows_RgXn(GEN a, long b)
     860             : {
     861          84 :   long n = lg(a)-1;
     862             :   GEN c;
     863          84 :   a = RgV_to_RgX(a,0);
     864          84 :   if (b < 0) { a = RgXn_inv(a, n); b = -b; }
     865          84 :   c = RgXn_powu_i(a,b,n);
     866          84 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     867             : }
     868             : 
     869             : /* assume lg(V) >= n*d + 2 */
     870             : static GEN
     871        6720 : c_deflate(long n, long d, GEN v)
     872             : {
     873        6720 :   long i, id, l = n+2;
     874             :   GEN w;
     875        6720 :   if (d == 1) return lg(v) == l ? v: vecslice(v, 1, l-1);
     876         364 :   w = cgetg(l, typ(v));
     877       10283 :   for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
     878         364 :   return w;
     879             : }
     880             : 
     881             : static void
     882          14 : err_cyclo(void)
     883          14 : { pari_err_IMPL("changing cyclotomic fields in mf"); }
     884             : /* Q(zeta_a) = Q(zeta_b) ? */
     885             : static int
     886         567 : same_cyc(long a, long b)
     887         567 : { return (a == b) || (odd(a) && b == (a<<1)) || (odd(b) && a == (b<<1)); }
     888             : /* need to combine elements in Q(CHI1) and Q(CHI2) with result in Q(CHI),
     889             :  * CHI = CHI1 * CHI2 or CHI / CHI2 times some character of order 2 */
     890             : static GEN
     891        2492 : chicompat(GEN CHI, GEN CHI1, GEN CHI2)
     892             : {
     893        2492 :   long o1 = mfcharorder(CHI1);
     894        2492 :   long o2 = mfcharorder(CHI2), O, o;
     895             :   GEN T1, T2, P, Po;
     896        2492 :   if (o1 <= 2 && o2 <= 2) return NULL;
     897         574 :   o = mfcharorder(CHI);
     898         574 :   Po = mfcharpol(CHI);
     899         574 :   P = mfcharpol(CHI1);
     900         574 :   if (o1 == o2)
     901             :   {
     902          21 :     if (o1 == o) return NULL;
     903          14 :     if (!same_cyc(o1,o)) err_cyclo();
     904           0 :     return mkvec4(P, gen_1,gen_1, Qab_trace_init(o1, o, P, Po));
     905             :   }
     906         553 :   O = ulcm(o1, o2);
     907         553 :   if (!same_cyc(O,o)) err_cyclo();
     908         553 :   if (O != o1) P = (O == o2)? mfcharpol(CHI2): polcyclo(O, varn(P));
     909         553 :   T1 = o1 <= 2? gen_1: utoipos(O / o1);
     910         553 :   T2 = o2 <= 2? gen_1: utoipos(O / o2);
     911         553 :   return mkvec4(P, T1, T2, O == o? gen_1: Qab_trace_init(O, o, P, Po));
     912             : }
     913             : /* *F a vector of cyclotomic numbers */
     914             : static void
     915           7 : compatlift(GEN *F, long o, GEN P)
     916             : {
     917             :   long i, l;
     918           7 :   GEN f = *F, g = cgetg_copy(f,&l);
     919          56 :   for (i = 1; i < l; i++)
     920             :   {
     921          49 :     GEN fi = lift_shallow(gel(f,i));
     922          49 :     gel(g,i) = gmodulo(typ(fi)==t_POL? RgX_inflate(fi,o): fi, P);
     923             :   }
     924           7 :   *F = g;
     925           7 : }
     926             : static void
     927         651 : chicompatlift(GEN T, GEN *F, GEN *G)
     928             : {
     929         651 :   long o1 = itou(gel(T,2)), o2 = itou(gel(T,3));
     930         651 :   GEN P = gel(T,1);
     931         651 :   if (o1 != 1) compatlift(F, o1, P);
     932         651 :   if (o2 != 1 && G) compatlift(G, o2, P);
     933         651 : }
     934             : static GEN
     935         651 : chicompatfix(GEN T, GEN F)
     936             : {
     937         651 :   GEN V = gel(T,4);
     938         651 :   if (typ(V) == t_VEC) F = gmodulo(QabV_tracerel(V, 0, F), gel(V,1));
     939         651 :   return F;
     940             : }
     941             : 
     942             : static GEN
     943         637 : c_mul(long n, long d, GEN S)
     944             : {
     945         637 :   pari_sp av = avma;
     946         637 :   long nd = n*d;
     947         637 :   GEN F = gel(S,2), G = gel(S,3);
     948         637 :   F = mfcoefs_i(F, nd, 1);
     949         637 :   G = mfcoefs_i(G, nd, 1);
     950         637 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F,&G);
     951         637 :   F = c_deflate(n, d, RgV_mul_RgXn(F,G));
     952         637 :   if (lg(S) == 5) F = chicompatfix(gel(S,4), F);
     953         637 :   return gerepilecopy(av, F);
     954             : }
     955             : static GEN
     956          84 : c_pow(long n, long d, GEN S)
     957             : {
     958          84 :   pari_sp av = avma;
     959          84 :   long nd = n*d;
     960          84 :   GEN F = gel(S,2), a = gel(S,3), f = mfcoefs_i(F,nd,1);
     961          84 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F, NULL);
     962          84 :   f = RgV_pows_RgXn(f, itos(a));
     963          84 :   f = c_deflate(n, d, f);
     964          84 :   if (lg(S) == 5) f = chicompatfix(gel(S,4), f);
     965          84 :   return gerepilecopy(av, f);
     966             : }
     967             : 
     968             : /* F * Theta */
     969             : static GEN
     970         406 : mfmultheta(GEN F)
     971             : {
     972         406 :   if (typ(mf_get_gk(F)) == t_FRAC && mf_get_type(F) == t_MF_DIV)
     973             :   {
     974         112 :     GEN T = gel(F,3); /* hopefully mfTheta() */
     975         112 :     if (mf_get_type(T) == t_MF_THETA && mf_get_N(T) == 4) return gel(F,2);
     976             :   }
     977         294 :   return mfmul(F, mfTheta(NULL));
     978             : }
     979             : 
     980             : static GEN
     981          42 : c_bracket(long n, long d, GEN S)
     982             : {
     983          42 :   pari_sp av = avma;
     984          42 :   long i, nd = n*d;
     985          42 :   GEN F = gel(S,2), G = gel(S,3), tF, tG, C, mpow, res, gk, gl;
     986          42 :   GEN VF = mfcoefs_i(F, nd, 1);
     987          42 :   GEN VG = mfcoefs_i(G, nd, 1);
     988          42 :   ulong j, m = itou(gel(S,4));
     989             : 
     990          42 :   if (!n)
     991             :   {
     992          14 :     if (m > 0) { set_avma(av); return mkvec(gen_0); }
     993           7 :     return gerepilecopy(av, mkvec(gmul(gel(VF, 1), gel(VG, 1))));
     994             :   }
     995          28 :   tF = cgetg(nd+2, t_VEC);
     996          28 :   tG = cgetg(nd+2, t_VEC);
     997          28 :   res = NULL; gk = mf_get_gk(F); gl = mf_get_gk(G);
     998             :   /* pow[i,j+1] = i^j */
     999          28 :   if (lg(S) == 6) chicompatlift(gel(S,5),&VF,&VG);
    1000          28 :   mpow = cgetg(m+2, t_MAT);
    1001          28 :   gel(mpow,1) = const_col(nd, gen_1);
    1002          56 :   for (j = 1; j <= m; j++)
    1003             :   {
    1004          28 :     GEN c = cgetg(nd+1, t_COL);
    1005          28 :     gel(mpow,j+1) = c;
    1006         245 :     for (i = 1; i <= nd; i++) gel(c,i) = muliu(gcoeff(mpow,i,j), i);
    1007             :   }
    1008          28 :   C = binomial(gaddgs(gk, m-1), m);
    1009          28 :   if (odd(m)) C = gneg(C);
    1010          84 :   for (j = 0; j <= m; j++)
    1011             :   { /* C = (-1)^(m-j) binom(m+l-1, j) binom(m+k-1,m-j) */
    1012             :     GEN c;
    1013          56 :     gel(tF,1) = j == 0? gel(VF,1): gen_0;
    1014          56 :     gel(tG,1) = j == m? gel(VG,1): gen_0;
    1015          56 :     gel(tF,2) = gel(VF,2); /* assume nd >= 1 */
    1016          56 :     gel(tG,2) = gel(VG,2);
    1017         518 :     for (i = 2; i <= nd; i++)
    1018             :     {
    1019         462 :       gel(tF, i+1) = gmul(gcoeff(mpow,i,j+1),   gel(VF, i+1));
    1020         462 :       gel(tG, i+1) = gmul(gcoeff(mpow,i,m-j+1), gel(VG, i+1));
    1021             :     }
    1022          56 :     c = gmul(C, c_deflate(n, d, RgV_mul_RgXn(tF, tG)));
    1023          56 :     res = res? gadd(res, c): c;
    1024          56 :     if (j < m)
    1025          56 :       C = gdiv(gmul(C, gmulsg(m-j, gaddgs(gl,m-j-1))),
    1026          28 :                gmulsg(-(j+1), gaddgs(gk,j)));
    1027             :   }
    1028          28 :   if (lg(S) == 6) res = chicompatfix(gel(S,5), res);
    1029          28 :   return gerepileupto(av, res);
    1030             : }
    1031             : /* linear combination \sum L[j] vecF[j] */
    1032             : static GEN
    1033        2856 : c_linear(long n, long d, GEN F, GEN L, GEN dL)
    1034             : {
    1035        2856 :   pari_sp av = avma;
    1036        2856 :   long j, l = lg(L);
    1037        2856 :   GEN S = NULL;
    1038       10143 :   for (j = 1; j < l; j++)
    1039             :   {
    1040        7287 :     GEN c = gel(L,j);
    1041        7287 :     if (gequal0(c)) continue;
    1042        6650 :     c = gmul(c, mfcoefs_i(gel(F,j), n, d));
    1043        6650 :     S = S? gadd(S,c): c;
    1044             :   }
    1045        2856 :   if (!S) return zerovec(n+1);
    1046        2856 :   if (!is_pm1(dL)) S = gdiv(S, dL);
    1047        2856 :   return gerepileupto(av, S);
    1048             : }
    1049             : 
    1050             : /* B_d(T_j Trace^new) as t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)) or
    1051             :  * t_MF_HECKE(t_MF_NEWTRACE)
    1052             :  * or t_MF_NEWTRACE in level N. Set d and j, return t_MF_NEWTRACE component*/
    1053             : static GEN
    1054       69181 : bhn_parse(GEN f, long *d, long *j)
    1055             : {
    1056       69181 :   long t = mf_get_type(f);
    1057       69181 :   *d = *j = 1;
    1058       69181 :   if (t == t_MF_BD) { *d = itos(gel(f,3)); f = gel(f,2); t = mf_get_type(f); }
    1059       69181 :   if (t == t_MF_HECKE) { *j = gel(f,2)[1]; f = gel(f,3); }
    1060       69181 :   return f;
    1061             : }
    1062             : /* f as above, return the t_MF_NEWTRACE component */
    1063             : static GEN
    1064       22022 : bhn_newtrace(GEN f)
    1065             : {
    1066       22022 :   long t = mf_get_type(f);
    1067       22022 :   if (t == t_MF_BD) { f = gel(f,2); t = mf_get_type(f); }
    1068       22022 :   if (t == t_MF_HECKE) f = gel(f,3);
    1069       22022 :   return f;
    1070             : }
    1071             : static int
    1072        3367 : ok_bhn_linear(GEN vf)
    1073             : {
    1074        3367 :   long i, N0 = 0, l = lg(vf);
    1075             :   GEN CHI, gk;
    1076        3367 :   if (l == 1) return 1;
    1077        3367 :   gk = mf_get_gk(gel(vf,1));
    1078        3367 :   CHI = mf_get_CHI(gel(vf,1));
    1079       17311 :   for (i = 1; i < l; i++)
    1080             :   {
    1081       15967 :     GEN f = bhn_newtrace(gel(vf,i));
    1082       15967 :     long N = mf_get_N(f);
    1083       15967 :     if (mf_get_type(f) != t_MF_NEWTRACE) return 0;
    1084       13944 :     if (N < N0) return 0; /* largest level must come last */
    1085       13944 :     N0 = N;
    1086       13944 :     if (!gequal(gk,mf_get_gk(f))) return 0; /* same k */
    1087       13944 :     if (!gequal(gel(mf_get_CHI(f),2), gel(CHI,2))) return 0; /* same CHI */
    1088             :   }
    1089        1344 :   return 1;
    1090             : }
    1091             : 
    1092             : /* vF not empty, same hypotheses as bhnmat_extend */
    1093             : static GEN
    1094        6139 : bhnmat_extend_nocache(GEN M, long N, long n, long d, GEN vF)
    1095             : {
    1096             :   cachenew_t cache;
    1097        6139 :   long l = lg(vF);
    1098             :   GEN f;
    1099        6139 :   if (l == 1) return M? M: cgetg(1, t_MAT);
    1100        6055 :   f = bhn_newtrace(gel(vF,1)); /* N.B. mf_get_N(f) divides N */
    1101        6055 :   init_cachenew(&cache, n*d, N, f);
    1102        6055 :   M = bhnmat_extend(M, n, d, vF, &cache);
    1103        6055 :   dbg_cachenew(&cache); return M;
    1104             : }
    1105             : /* c_linear of "bhn" mf closures, same hypotheses as bhnmat_extend */
    1106             : static GEN
    1107        1652 : c_linear_bhn(long n, long d, GEN F)
    1108             : {
    1109             :   pari_sp av;
    1110        1652 :   GEN M, v, vF = gel(F,2), L = gel(F,3), dL = gel(F,4);
    1111        1652 :   if (lg(L) == 1) return zerovec(n+1);
    1112        1652 :   av = avma;
    1113        1652 :   M = bhnmat_extend_nocache(NULL, mf_get_N(F), n, d, vF);
    1114        1652 :   v = RgM_RgC_mul(M,L); settyp(v, t_VEC);
    1115        1652 :   if (!is_pm1(dL)) v = gdiv(v, dL);
    1116        1652 :   return gerepileupto(av, v);
    1117             : }
    1118             : 
    1119             : /* c in K, K := Q[X]/(T) vz = vector of consecutive powers of root z of T
    1120             :  * attached to an embedding s: K -> C. Return s(c) in C */
    1121             : static GEN
    1122       75565 : Rg_embed1(GEN c, GEN vz)
    1123             : {
    1124       75565 :   long t = typ(c);
    1125       75565 :   if (t == t_POLMOD) { c = gel(c,2); t = typ(c); }
    1126       75565 :   if (t == t_POL) c = RgX_RgV_eval(c, vz);
    1127       75565 :   return c;
    1128             : }
    1129             : /* return s(P) in C[X] */
    1130             : static GEN
    1131         882 : RgX_embed1(GEN P, GEN vz)
    1132             : {
    1133             :   long i, l;
    1134         882 :   GEN Q = cgetg_copy(P, &l);
    1135         882 :   Q[1] = P[1];
    1136        2317 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1137         882 :   return normalizepol_lg(Q,l); /* normally a no-op */
    1138             : }
    1139             : /* return s(P) in C^n */
    1140             : static GEN
    1141         728 : vecembed1(GEN P, GEN vz)
    1142             : {
    1143             :   long i, l;
    1144         728 :   GEN Q = cgetg_copy(P, &l);
    1145       30758 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1146         728 :   return Q;
    1147             : }
    1148             : /* P in L = K[X]/(U), K = Q[t]/T; s an embedding of K -> C attached
    1149             :  * to a root of T, extended to an embedding of L -> C attached to a root
    1150             :  * of s(U); vT powers of the root of T, vU powers of the root of s(U).
    1151             :  * Return s(P) in C^n */
    1152             : static GEN
    1153       13314 : Rg_embed2(GEN P, long vt, GEN vT, GEN vU)
    1154             : {
    1155             :   long i, l;
    1156             :   GEN Q;
    1157       13314 :   P = liftpol_shallow(P);
    1158       13314 :   if (typ(P) != t_POL) return P;
    1159       13300 :   if (varn(P) == vt) return Rg_embed1(P, vT);
    1160             :   /* varn(P) == vx */
    1161       13293 :   Q = cgetg_copy(P, &l); Q[1] = P[1];
    1162       39669 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vT);
    1163       13293 :   return Rg_embed1(Q, vU);
    1164             : }
    1165             : static GEN
    1166          42 : vecembed2(GEN P, long vt, GEN vT, GEN vU)
    1167             : {
    1168             :   long i, l;
    1169          42 :   GEN Q = cgetg_copy(P, &l);
    1170        1050 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1171          42 :   return Q;
    1172             : }
    1173             : static GEN
    1174         532 : RgX_embed2(GEN P, long vt, GEN vT, GEN vU)
    1175             : {
    1176             :   long i, l;
    1177         532 :   GEN Q = cgetg_copy(P, &l);
    1178        3724 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1179         532 :   Q[1] = P[1]; return normalizepol_lg(Q,l);
    1180             : }
    1181             : /* embed polynomial f in variable vx [ may be a scalar ], E from getembed */
    1182             : static GEN
    1183        1596 : RgX_embed(GEN f, long vx, GEN E)
    1184             : {
    1185             :   GEN vT;
    1186        1596 :   if (typ(f) != t_POL || varn(f) != vx) return mfembed(E, f);
    1187        1575 :   if (lg(E) == 1) return f;
    1188        1379 :   vT = gel(E,2);
    1189        1379 :   if (lg(E) == 3)
    1190         847 :     f = RgX_embed1(f, vT);
    1191             :   else
    1192         532 :     f = RgX_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1193        1379 :   return f;
    1194             : }
    1195             : /* embed vector, E from getembed */
    1196             : GEN
    1197        1617 : mfvecembed(GEN E, GEN v)
    1198             : {
    1199             :   GEN vT;
    1200        1617 :   if (lg(E) == 1) return v;
    1201         770 :   vT = gel(E,2);
    1202         770 :   if (lg(E) == 3)
    1203         728 :     v = vecembed1(v, vT);
    1204             :   else
    1205          42 :     v = vecembed2(v, varn(gel(E,1)), vT, gel(E,3));
    1206         770 :   return v;
    1207             : }
    1208             : GEN
    1209          63 : mfmatembed(GEN E, GEN f)
    1210             : {
    1211             :   long i, l;
    1212             :   GEN g;
    1213          63 :   if (lg(E) == 1) return f;
    1214          42 :   g = cgetg_copy(f, &l);
    1215         168 :   for (i = 1; i < l; i++) gel(g,i) = mfvecembed(E, gel(f,i));
    1216          42 :   return g;
    1217             : }
    1218             : /* embed vector of polynomials in var vx */
    1219             : static GEN
    1220          98 : RgXV_embed(GEN f, long vx, GEN E)
    1221             : {
    1222             :   long i, l;
    1223             :   GEN v;
    1224          98 :   if (lg(E) == 1) return f;
    1225          70 :   v = cgetg_copy(f, &l);
    1226        1358 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(gel(f,i), vx, E);
    1227          70 :   return v;
    1228             : }
    1229             : 
    1230             : /* embed scalar */
    1231             : GEN
    1232       96580 : mfembed(GEN E, GEN f)
    1233             : {
    1234             :   GEN vT;
    1235       96580 :   if (lg(E) == 1) return f;
    1236       13538 :   vT = gel(E,2);
    1237       13538 :   if (lg(E) == 3)
    1238        4424 :     f = Rg_embed1(f, vT);
    1239             :   else
    1240        9114 :     f = Rg_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1241       13538 :   return f;
    1242             : }
    1243             : /* vector of the sigma(f), sigma in vE */
    1244             : static GEN
    1245         294 : RgX_embedall(GEN f, long vx, GEN vE)
    1246             : {
    1247         294 :   long i, l = lg(vE);
    1248         294 :   GEN v = cgetg(l, t_VEC);
    1249         602 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(f, vx, gel(vE,i));
    1250         294 :   return l == 2? gel(v,1): v;
    1251             : }
    1252             : /* matrix whose colums are the sigma(v), sigma in vE */
    1253             : static GEN
    1254         336 : RgC_embedall(GEN v, GEN vE)
    1255             : {
    1256         336 :   long j, l = lg(vE);
    1257         336 :   GEN M = cgetg(l, t_MAT);
    1258         819 :   for (j = 1; j < l; j++) gel(M,j) = mfvecembed(gel(vE,j), v);
    1259         336 :   return M;
    1260             : }
    1261             : /* vector of the sigma(v), sigma in vE */
    1262             : static GEN
    1263        4907 : Rg_embedall_i(GEN v, GEN vE)
    1264             : {
    1265        4907 :   long j, l = lg(vE);
    1266        4907 :   GEN M = cgetg(l, t_VEC);
    1267       14735 :   for (j = 1; j < l; j++) gel(M,j) = mfembed(gel(vE,j), v);
    1268        4907 :   return M;
    1269             : }
    1270             : /* vector of the sigma(v), sigma in vE; if #vE == 1, return v */
    1271             : static GEN
    1272       90980 : Rg_embedall(GEN v, GEN vE)
    1273       90980 : { return (lg(vE) == 2)? mfembed(gel(vE,1), v): Rg_embedall_i(v, vE); }
    1274             : 
    1275             : static GEN
    1276         833 : c_div_i(long n, GEN S)
    1277             : {
    1278         833 :   GEN F = gel(S,2), G = gel(S,3);
    1279             :   GEN a0, a0i, H;
    1280         833 :   F = mfcoefs_i(F, n, 1);
    1281         833 :   G = mfcoefs_i(G, n, 1);
    1282         833 :   if (lg(S) == 5) chicompatlift(gel(S,4),&F,&G);
    1283         833 :   F = RgV_to_ser_full(F);
    1284         833 :   G = RgV_to_ser_full(G);
    1285         833 :   a0 = polcoef_i(G, 0, -1); /* != 0 */
    1286         833 :   if (gequal1(a0)) a0 = a0i = NULL;
    1287             :   else
    1288             :   {
    1289         602 :     a0i = ginv(a0);
    1290         602 :     G = gmul(ser_unscale(G,a0), a0i);
    1291         602 :     F = gmul(ser_unscale(F,a0), a0i);
    1292             :   }
    1293         833 :   H = gdiv(F, G);
    1294         833 :   if (a0) H = ser_unscale(H,a0i);
    1295         833 :   H = sertovecslice(H, n);
    1296         833 :   if (lg(S) == 5) H = chicompatfix(gel(S,4), H);
    1297         833 :   return H;
    1298             : }
    1299             : static GEN
    1300         833 : c_div(long n, long d, GEN S)
    1301             : {
    1302         833 :   pari_sp av = avma;
    1303         833 :   GEN D = (d==1)? c_div_i(n, S): c_deflate(n, d, c_div_i(n*d, S));
    1304         833 :   return gerepilecopy(av, D);
    1305             : }
    1306             : 
    1307             : static GEN
    1308          35 : c_shift(long n, long d, GEN F, GEN gsh)
    1309             : {
    1310          35 :   pari_sp av = avma;
    1311             :   GEN vF;
    1312          35 :   long sh = itos(gsh), n1 = n*d + sh;
    1313          35 :   if (n1 < 0) return zerovec(n+1);
    1314          35 :   vF = mfcoefs_i(F, n1, 1);
    1315          35 :   if (sh < 0) vF = shallowconcat(zerovec(-sh), vF);
    1316          35 :   else vF = vecslice(vF, sh+1, n1+1);
    1317          35 :   return gerepilecopy(av, c_deflate(n, d, vF));
    1318             : }
    1319             : 
    1320             : static GEN
    1321         147 : c_deriv(long n, long d, GEN F, GEN gm)
    1322             : {
    1323         147 :   pari_sp av = avma;
    1324         147 :   GEN V = mfcoefs_i(F, n, d), res;
    1325         147 :   long i, m = itos(gm);
    1326         147 :   if (!m) return V;
    1327         147 :   res = cgetg(n+2, t_VEC); gel(res,1) = gen_0;
    1328         147 :   if (m < 0)
    1329          49 :   { for (i=1; i <= n; i++) gel(res, i+1) = gdiv(gel(V, i+1), powuu(i,-m)); }
    1330             :   else
    1331        1953 :   { for (i=1; i <= n; i++) gel(res, i+1) = gmul(gel(V,i+1), powuu(i,m)); }
    1332         147 :   return gerepileupto(av, res);
    1333             : }
    1334             : 
    1335             : static GEN
    1336          14 : c_derivE2(long n, long d, GEN F, GEN gm)
    1337             : {
    1338          14 :   pari_sp av = avma;
    1339             :   GEN VF, VE, res, tmp, gk;
    1340          14 :   long i, m = itos(gm), nd;
    1341          14 :   if (m == 0) return mfcoefs_i(F, n, d);
    1342          14 :   nd = n*d;
    1343          14 :   VF = mfcoefs_i(F, nd, 1); VE = mfcoefs_i(mfEk(2), nd, 1);
    1344          14 :   gk = mf_get_gk(F);
    1345          14 :   if (m == 1)
    1346             :   {
    1347           7 :     res = cgetg(n+2, t_VEC);
    1348          56 :     for (i = 0; i <= n; i++) gel(res, i+1) = gmulsg(i, gel(VF, i*d+1));
    1349           7 :     tmp = c_deflate(n, d, RgV_mul_RgXn(VF, VE));
    1350           7 :     return gerepileupto(av, gsub(res, gmul(gdivgs(gk, 12), tmp)));
    1351             :   }
    1352             :   else
    1353             :   {
    1354             :     long j;
    1355          35 :     for (j = 1; j <= m; j++)
    1356             :     {
    1357          28 :       tmp = RgV_mul_RgXn(VF, VE);
    1358         140 :       for (i = 0; i <= nd; i++) gel(VF, i+1) = gmulsg(i, gel(VF, i+1));
    1359          28 :       VF = gsub(VF, gmul(gdivgs(gaddgs(gk, 2*(j-1)), 12), tmp));
    1360             :     }
    1361           7 :     return gerepilecopy(av, c_deflate(n, d, VF));
    1362             :   }
    1363             : }
    1364             : 
    1365             : /* Twist by the character (D/.) */
    1366             : static GEN
    1367           7 : c_twist(long n, long d, GEN F, GEN D)
    1368             : {
    1369           7 :   pari_sp av = avma;
    1370           7 :   GEN V = mfcoefs_i(F, n, d), res = cgetg(n+2, t_VEC);
    1371             :   long i;
    1372         119 :   for (i = 0; i <= n; i++)
    1373         112 :     gel(res, i + 1) = gmulsg(krois(D, i), gel(V, i+1));
    1374           7 :   return gerepileupto(av, res);
    1375             : }
    1376             : 
    1377             : /* form F given by closure, compute T(n)(F) as closure */
    1378             : static GEN
    1379         994 : c_hecke(long m, long l, GEN DATA, GEN F)
    1380             : {
    1381         994 :   pari_sp av = avma;
    1382         994 :   return gerepilecopy(av, hecke_i(m, l, NULL, F, DATA));
    1383             : }
    1384             : static GEN
    1385         140 : c_const(long n, long d, GEN C)
    1386             : {
    1387         140 :   GEN V = zerovec(n+1);
    1388         140 :   long i, j, l = lg(C);
    1389         140 :   if (l > d*n+2) l = d*n+2;
    1390         189 :   for (i = j = 1; i < l; i+=d, j++) gel(V, j) = gcopy(gel(C,i));
    1391         140 :   return V;
    1392             : }
    1393             : 
    1394             : /* m > 0 */
    1395             : static GEN
    1396         462 : eta3_ZXn(long m)
    1397             : {
    1398         462 :   long l = m+2, n, k;
    1399         462 :   GEN P = cgetg(l,t_POL);
    1400         462 :   P[1] = evalsigne(1)|evalvarn(0);
    1401        6125 :   for (n = 2; n < l; n++) gel(P,n) = gen_0;
    1402         462 :   for (n = k = 0;; n++)
    1403             :   {
    1404        2534 :     if (k + n >= m) { setlg(P, k+3); return P; }
    1405        2072 :     k += n;
    1406             :     /* now k = n(n+1) / 2 */
    1407        2072 :     gel(P, k+2) = odd(n)? utoineg(2*n+1): utoipos(2*n+1);
    1408             :   }
    1409             : }
    1410             : 
    1411             : static GEN
    1412         469 : c_delta(long n, long d)
    1413             : {
    1414         469 :   pari_sp ltop = avma;
    1415         469 :   long N = n*d;
    1416             :   GEN e;
    1417         469 :   if (!N) return mkvec(gen_0);
    1418         462 :   e = eta3_ZXn(N);
    1419         462 :   e = ZXn_sqr(e,N);
    1420         462 :   e = ZXn_sqr(e,N);
    1421         462 :   e = ZXn_sqr(e,N); /* eta(x)^24 */
    1422         462 :   settyp(e, t_VEC);
    1423         462 :   gel(e,1) = gen_0; /* Delta(x) = x*eta(x)^24 as a t_VEC */
    1424         462 :   return gerepilecopy(ltop, c_deflate(n, d, e));
    1425             : }
    1426             : 
    1427             : /* return s(d) such that s|f <=> d | f^2 */
    1428             : static long
    1429          49 : mysqrtu(ulong d)
    1430             : {
    1431          49 :   GEN fa = myfactoru(d), P = gel(fa,1), E = gel(fa,2);
    1432          49 :   long l = lg(P), i, s = 1;
    1433         126 :   for (i = 1; i < l; i++) s *= upowuu(P[i], (E[i]+1)>>1);
    1434          49 :   return s;
    1435             : }
    1436             : static GEN
    1437        1785 : c_theta(long n, long d, GEN psi)
    1438             : {
    1439        1785 :   long lim = usqrt(n*d), F = mfcharmodulus(psi), par = mfcharparity(psi);
    1440        1785 :   long f, d2 = d == 1? 1: mysqrtu(d);
    1441        1785 :   GEN V = zerovec(n + 1);
    1442        7966 :   for (f = d2; f <= lim; f += d2)
    1443        6181 :     if (ugcd(F, f) == 1)
    1444             :     {
    1445        6174 :       pari_sp av = avma;
    1446        6174 :       GEN c = mfchareval(psi, f);
    1447        6174 :       gel(V, f*f/d + 1) = gerepileupto(av, par < 0 ? gmulgs(c,2*f) : gmul2n(c,1));
    1448             :     }
    1449        1785 :   if (F == 1) gel(V, 1) = gen_1;
    1450        1785 :   return V; /* no gerepile needed */
    1451             : }
    1452             : 
    1453             : static GEN
    1454         189 : c_etaquo(long n, long d, GEN eta, GEN gs)
    1455             : {
    1456         189 :   pari_sp av = avma;
    1457         189 :   long s = itos(gs), nd = n*d, nds = nd - s + 1;
    1458             :   GEN c;
    1459         189 :   if (nds <= 0) return zerovec(n+1);
    1460         168 :   c = RgX_to_RgC(eta_product_ZXn(eta, nds), nds); settyp(c, t_VEC);
    1461         168 :   if (s > 0) c = shallowconcat(zerovec(s), c);
    1462         168 :   return gerepilecopy(av, c_deflate(n, d, c));
    1463             : }
    1464             : 
    1465             : static GEN
    1466          77 : c_ell(long n, long d, GEN E)
    1467             : {
    1468          77 :   pari_sp av = avma;
    1469             :   GEN v;
    1470          77 :   if (d == 1) return concat(gen_0, anell(E, n));
    1471           7 :   v = shallowconcat(gen_0, anell(E, n*d));
    1472           7 :   return gerepilecopy(av, c_deflate(n, d, v));
    1473             : }
    1474             : 
    1475             : static GEN
    1476          21 : c_cusptrace(long n, long d, GEN F)
    1477             : {
    1478          21 :   pari_sp av = avma;
    1479          21 :   GEN D = gel(F,2), res = cgetg(n+2, t_VEC);
    1480          21 :   long i, N = mf_get_N(F), k = mf_get_k(F);
    1481          21 :   gel(res, 1) = gen_0;
    1482         140 :   for (i = 1; i <= n; i++)
    1483         119 :     gel(res, i+1) = mfcusptrace_i(N, k, i*d, mydivisorsu(i*d), D);
    1484          21 :   return gerepilecopy(av, res);
    1485             : }
    1486             : 
    1487             : static GEN
    1488        1540 : c_newtrace(long n, long d, GEN F)
    1489             : {
    1490        1540 :   pari_sp av = avma;
    1491             :   cachenew_t cache;
    1492        1540 :   long N = mf_get_N(F);
    1493             :   GEN v;
    1494        1540 :   init_cachenew(&cache, n*d, N, F);
    1495        1540 :   v = colnewtrace(0, n, d, N, mf_get_k(F), &cache);
    1496        1540 :   settyp(v, t_VEC); return gerepilecopy(av, v);
    1497             : }
    1498             : 
    1499             : static GEN
    1500        5670 : c_Bd(long n, long d, GEN F, GEN A)
    1501             : {
    1502        5670 :   pari_sp av = avma;
    1503        5670 :   long a = itou(A), ad = ugcd(a,d), aad = a/ad, i, j;
    1504        5670 :   GEN w, v = mfcoefs_i(F, n/aad, d/ad);
    1505        5670 :   if (a == 1) return v;
    1506        5670 :   n++; w = zerovec(n);
    1507      115437 :   for (i = j = 1; j <= n; i++, j += aad) gel(w,j) = gcopy(gel(v,i));
    1508        5670 :   return gerepileupto(av, w);
    1509             : }
    1510             : 
    1511             : static GEN
    1512        3997 : c_dihedral(long n, long d, GEN bnr, GEN w, GEN k0j)
    1513             : {
    1514        3997 :   pari_sp av = avma;
    1515        3997 :   GEN V = dihan(bnr, w, k0j, n*d);
    1516        3997 :   GEN Tinit = gel(w,3), Pm = gel(Tinit,1);
    1517        3997 :   GEN A = c_deflate(n, d, V);
    1518        3997 :   if (degpol(Pm) == 1 || RgV_is_ZV(A)) return gerepilecopy(av, A);
    1519         735 :   return gerepileupto(av, gmodulo(A, Pm));
    1520             : }
    1521             : 
    1522             : static GEN
    1523         315 : c_mfEH(long n, long d, GEN F)
    1524             : {
    1525         315 :   pari_sp av = avma;
    1526             :   GEN v, M, A;
    1527         315 :   long i, r = mf_get_r(F);
    1528         315 :   if (n == 1)
    1529          14 :     return gerepilecopy(av, mkvec2(mfEHcoef(r,0),mfEHcoef(r,d)));
    1530             :   /* speedup mfcoef */
    1531         301 :   if (r == 1)
    1532             :   {
    1533          70 :     v = cgetg(n+2, t_VEC);
    1534          70 :     gel(v,1) = sstoQ(-1,12);
    1535       83258 :     for (i = 1; i <= n; i++)
    1536             :     {
    1537       83188 :       long id = i*d, a = id & 3;
    1538       83188 :       gel(v,i+1) = (a==1 || a==2)? gen_0: sstoQ(hclassno6u(id), 6);
    1539             :     }
    1540          70 :     return v; /* no gerepile needed */
    1541             :   }
    1542         231 :   M = mfEHmat(n*d+1,r);
    1543         231 :   if (d > 1)
    1544             :   {
    1545          35 :     long l = lg(M);
    1546         119 :     for (i = 1; i < l; i++) gel(M,i) = c_deflate(n, d, gel(M,i));
    1547             :   }
    1548         231 :   A = gel(F,2); /* [num(B), den(B)] */
    1549         231 :   v = RgC_Rg_div(RgM_RgC_mul(M, gel(A,1)), gel(A,2));
    1550         231 :   settyp(v,t_VEC); return gerepileupto(av, v);
    1551             : }
    1552             : 
    1553             : static GEN
    1554        8995 : c_mfeisen(long n, long d, GEN F)
    1555             : {
    1556        8995 :   pari_sp av = avma;
    1557        8995 :   GEN v, vchi, E0, P, T, CHI, gk = mf_get_gk(F);
    1558             :   long i, k;
    1559        8995 :   if (typ(gk) != t_INT) return c_mfEH(n, d, F);
    1560        8680 :   k = itou(gk);
    1561        8680 :   vchi = gel(F,2);
    1562        8680 :   E0 = gel(vchi,1);
    1563        8680 :   T = gel(vchi,2);
    1564        8680 :   P = gel(T,1);
    1565        8680 :   CHI = gel(vchi,3);
    1566        8680 :   v = cgetg(n+2, t_VEC);
    1567        8680 :   gel(v, 1) = gcopy(E0); /* E(0) */
    1568        8680 :   if (lg(vchi) == 5)
    1569             :   { /* E_k(chi1,chi2) */
    1570        6678 :     GEN CHI2 = gel(vchi,4), F3 = gel(F,3);
    1571        6678 :     long ord = F3[1], j = F3[2];
    1572      188923 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi2(k, CHI, CHI2, i*d, ord);
    1573        6678 :     v = QabV_tracerel(T, j, v);
    1574             :   }
    1575             :   else
    1576             :   { /* E_k(chi) */
    1577       25998 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi(k, CHI, i*d);
    1578             :   }
    1579        8680 :   if (degpol(P) != 1 && !RgV_is_QV(v)) return gerepileupto(av, gmodulo(v, P));
    1580        7007 :   return gerepilecopy(av, v);
    1581             : }
    1582             : 
    1583             : /* L(chi_D, 1-k) */
    1584             : static GEN
    1585          28 : lfunquadneg_naive(long D, long k)
    1586             : {
    1587          28 :   GEN B, dS, S = gen_0;
    1588          28 :   long r, N = labs(D);
    1589             :   pari_sp av;
    1590          28 :   if (k == 1 && N == 1) return gneg(ghalf);
    1591             :   /* B = N^k * denom(B) * B(x/N) */
    1592          28 :   B = ZX_rescale(Q_remove_denom(bernpol(k, 0), &dS), utoi(N));
    1593          28 :   dS = mul_denom(dS, stoi(-N*k));
    1594          28 :   av = avma;
    1595        7175 :   for (r = 0; r < N; r++)
    1596             :   {
    1597        7147 :     long c = kross(D, r);
    1598        7147 :     if (c)
    1599             :     {
    1600        5152 :       GEN tmp = poleval(B, utoi(r));
    1601        5152 :       S = c > 0 ? addii(S, tmp) : subii(S, tmp);
    1602        5152 :       S = gerepileuptoint(av, S);
    1603             :     }
    1604             :   }
    1605          28 :   return gdiv(S, dS);
    1606             : }
    1607             : 
    1608             : /* Returns vector of coeffs from F[0], F[d], ..., F[d*n] */
    1609             : static GEN
    1610       30758 : mfcoefs_i(GEN F, long n, long d)
    1611             : {
    1612       30758 :   if (n < 0) return gen_0;
    1613       30758 :   switch(mf_get_type(F))
    1614             :   {
    1615         140 :     case t_MF_CONST: return c_const(n, d, gel(F,2));
    1616        8995 :     case t_MF_EISEN: return c_mfeisen(n, d, F);
    1617         812 :     case t_MF_Ek: return c_Ek(n, d, F);
    1618         469 :     case t_MF_DELTA: return c_delta(n, d);
    1619        1547 :     case t_MF_THETA: return c_theta(n, d, gel(F,2));
    1620         189 :     case t_MF_ETAQUO: return c_etaquo(n, d, gel(F,2), gel(F,3));
    1621          77 :     case t_MF_ELL: return c_ell(n, d, gel(F,2));
    1622         637 :     case t_MF_MUL: return c_mul(n, d, F);
    1623          84 :     case t_MF_POW: return c_pow(n, d, F);
    1624          42 :     case t_MF_BRACKET: return c_bracket(n, d, F);
    1625        2856 :     case t_MF_LINEAR: return c_linear(n, d, gel(F,2), gel(F,3), gel(F,4));
    1626        1652 :     case t_MF_LINEAR_BHN: return c_linear_bhn(n, d, F);
    1627         833 :     case t_MF_DIV: return c_div(n, d, F);
    1628          35 :     case t_MF_SHIFT: return c_shift(n, d, gel(F,2), gel(F,3));
    1629         147 :     case t_MF_DERIV: return c_deriv(n, d, gel(F,2), gel(F,3));
    1630          14 :     case t_MF_DERIVE2: return c_derivE2(n, d, gel(F,2), gel(F,3));
    1631           7 :     case t_MF_TWIST: return c_twist(n, d, gel(F,2), gel(F,3));
    1632         994 :     case t_MF_HECKE: return c_hecke(n, d, gel(F,2), gel(F,3));
    1633        5670 :     case t_MF_BD: return c_Bd(n, d, gel(F,2), gel(F,3));
    1634          21 :     case t_MF_TRACE: return c_cusptrace(n, d, F);
    1635        1540 :     case t_MF_NEWTRACE: return c_newtrace(n, d, F);
    1636        3997 :     case t_MF_DIHEDRAL: return c_dihedral(n, d, gel(F,2), gel(F,3), gel(F,4));
    1637             :     default: pari_err_TYPE("mfcoefs",F); return NULL;/*LCOV_EXCL_LINE*/
    1638             :   }
    1639             : }
    1640             : 
    1641             : static GEN
    1642         308 : matdeflate(long n, long d, GEN M)
    1643             : {
    1644             :   long i, l;
    1645             :   GEN A;
    1646             :   /*  if (d == 1) return M; */
    1647         308 :   A = cgetg_copy(M,&l);
    1648        1204 :   for (i = 1; i < l; i++) gel(A,i) = c_deflate(n,d,gel(M,i));
    1649         308 :   return A;
    1650             : }
    1651             : static int
    1652        5607 : space_is_cusp(long space) { return space != mf_FULL && space != mf_EISEN; }
    1653             : /* safe with flraw mf */
    1654             : static GEN
    1655        2254 : mfcoefs_mf(GEN mf, long n, long d)
    1656             : {
    1657        2254 :   GEN MS, ME, E = MF_get_E(mf), S = MF_get_S(mf), M = MF_get_M(mf);
    1658        2254 :   long lE = lg(E), lS = lg(S), l = lE+lS-1;
    1659             : 
    1660        2254 :   if (l == 1) return cgetg(1, t_MAT);
    1661        2142 :   if (typ(M) == t_MAT && lg(M) != 1 && (n+1)*d < nbrows(M))
    1662          21 :     return matdeflate(n, d, M); /*cached; lg = 1 is possible from mfinit */
    1663        2121 :   ME = (lE == 1)? cgetg(1, t_MAT): mfvectomat(E, n, d);
    1664        2121 :   if (lS == 1)
    1665         399 :     MS = cgetg(1, t_MAT);
    1666        1722 :   else if (mf_get_type(gel(S,1)) == t_MF_DIV) /*k 1/2-integer or k=1 (exotic)*/
    1667         287 :     MS = matdeflate(n,d, mflineardivtomat(MF_get_N(mf), S, n*d));
    1668        1435 :   else if (MF_get_k(mf) == 1) /* k = 1 (dihedral) */
    1669             :   {
    1670         140 :     GEN M = mfvectomat(gmael(S,1,2), n, d);
    1671             :     long i;
    1672         140 :     MS = cgetg(lS, t_MAT);
    1673         448 :     for (i = 1; i < lS; i++)
    1674             :     {
    1675         308 :       GEN f = gel(S,i), dc = gel(f,4), c = RgM_RgC_mul(M, gel(f,3));
    1676         308 :       if (!equali1(dc)) c = RgC_Rg_div(c,dc);
    1677         308 :       gel(MS,i) = c;
    1678             :     }
    1679             :   }
    1680             :   else /* k >= 2 integer */
    1681        1295 :     MS = bhnmat_extend_nocache(NULL, MF_get_N(mf), n, d, S);
    1682        2121 :   return shallowconcat(ME,MS);
    1683             : }
    1684             : GEN
    1685        3731 : mfcoefs(GEN F, long n, long d)
    1686             : {
    1687        3731 :   if (!checkmf_i(F))
    1688             :   {
    1689          42 :     pari_sp av = avma;
    1690          42 :     GEN mf = checkMF_i(F); if (!mf) pari_err_TYPE("mfcoefs", F);
    1691          42 :     return gerepilecopy(av, mfcoefs_mf(mf,n,d));
    1692             :   }
    1693        3689 :   if (d <= 0) pari_err_DOMAIN("mfcoefs", "d", "<=", gen_0, stoi(d));
    1694        3689 :   if (n < 0) return cgetg(1, t_VEC);
    1695        3689 :   return mfcoefs_i(F, n, d);
    1696             : }
    1697             : 
    1698             : /* assume k >= 0 */
    1699             : static GEN
    1700         287 : mfak_i(GEN F, long k)
    1701             : {
    1702         287 :   if (!k) return gel(mfcoefs_i(F,0,1), 1);
    1703         154 :   return gel(mfcoefs_i(F,1,k), 2);
    1704             : }
    1705             : GEN
    1706         140 : mfcoef(GEN F, long n)
    1707             : {
    1708         140 :   pari_sp av = avma;
    1709         140 :   if (!checkmf_i(F)) pari_err_TYPE("mfcoef",F);
    1710         140 :   return n < 0? gen_0: gerepilecopy(av, mfak_i(F, n));
    1711             : }
    1712             : 
    1713             : static GEN
    1714         112 : paramconst() { return tagparams(t_MF_CONST, mkNK(1,0,mfchartrivial())); }
    1715             : static GEN
    1716          70 : mftrivial(void) { retmkvec2(paramconst(), cgetg(1,t_VEC)); }
    1717             : static GEN
    1718          42 : mf1(void) { retmkvec2(paramconst(), mkvec(gen_1)); }
    1719             : 
    1720             : /* induce mfchar CHI to G */
    1721             : static GEN
    1722      307237 : induce(GEN G, GEN CHI)
    1723             : {
    1724             :   GEN o, chi;
    1725      307237 :   if (typ(CHI) == t_INT) /* Kronecker */
    1726             :   {
    1727      300664 :     chi = znchar_quad(G, CHI);
    1728      300664 :     o = ZV_equal0(chi)? gen_1: gen_2;
    1729      300664 :     CHI = mkvec4(G,chi,o,cgetg(1,t_VEC));
    1730             :   }
    1731             :   else
    1732             :   {
    1733        6573 :     if (mfcharmodulus(CHI) == itos(znstar_get_N(G))) return CHI;
    1734        5999 :     CHI = leafcopy(CHI);
    1735        5999 :     chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    1736        5999 :     gel(CHI,1) = G;
    1737        5999 :     gel(CHI,2) = chi;
    1738             :   }
    1739      306663 :   return CHI;
    1740             : }
    1741             : /* induce mfchar CHI to znstar(G) */
    1742             : static GEN
    1743       42245 : induceN(long N, GEN CHI)
    1744             : {
    1745       42245 :   if (mfcharmodulus(CHI) != N) CHI = induce(znstar0(utoipos(N),1), CHI);
    1746       42245 :   return CHI;
    1747             : }
    1748             : /* *pCHI1 and *pCHI2 are mfchar, induce to common modulus */
    1749             : static void
    1750        6048 : char2(GEN *pCHI1, GEN *pCHI2)
    1751             : {
    1752        6048 :   GEN CHI1 = *pCHI1, G1 = gel(CHI1,1), N1 = znstar_get_N(G1);
    1753        6048 :   GEN CHI2 = *pCHI2, G2 = gel(CHI2,1), N2 = znstar_get_N(G2);
    1754        6048 :   if (!equalii(N1,N2))
    1755             :   {
    1756        4543 :     GEN G, d = gcdii(N1,N2);
    1757        4543 :     if      (equalii(N2,d)) *pCHI2 = induce(G1, CHI2);
    1758        1386 :     else if (equalii(N1,d)) *pCHI1 = induce(G2, CHI1);
    1759             :     else
    1760             :     {
    1761         154 :       if (!equali1(d)) N2 = diviiexact(N2,d);
    1762         154 :       G = znstar0(mulii(N1,N2), 1);
    1763         154 :       *pCHI1 = induce(G, CHI1);
    1764         154 :       *pCHI2 = induce(G, CHI2);
    1765             :     }
    1766             :   }
    1767        6048 : }
    1768             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1769             : static GEN
    1770      301693 : mfcharmul_i(GEN CHI1, GEN CHI2)
    1771             : {
    1772      301693 :   GEN G = gel(CHI1,1), chi3 = zncharmul(G, gel(CHI1,2), gel(CHI2,2));
    1773      301693 :   return mfcharGL(G, chi3);
    1774             : }
    1775             : /* mfchar or charinit; outputs a mfchar */
    1776             : static GEN
    1777        1036 : mfcharmul(GEN CHI1, GEN CHI2)
    1778             : {
    1779        1036 :   char2(&CHI1, &CHI2); return mfcharmul_i(CHI1,CHI2);
    1780             : }
    1781             : /* mfchar or charinit; outputs a mfchar */
    1782             : static GEN
    1783         126 : mfcharpow(GEN CHI, GEN n)
    1784             : {
    1785             :   GEN G, chi;
    1786         126 :   G = gel(CHI,1); chi = zncharpow(G, gel(CHI,2), n);
    1787         126 :   return mfchartoprimitive(mfcharGL(G, chi), NULL);
    1788             : }
    1789             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1790             : static GEN
    1791        5012 : mfchardiv_i(GEN CHI1, GEN CHI2)
    1792             : {
    1793        5012 :   GEN G = gel(CHI1,1), chi3 = znchardiv(G, gel(CHI1,2), gel(CHI2,2));
    1794        5012 :   return mfcharGL(G, chi3);
    1795             : }
    1796             : /* mfchar or charinit; outputs a mfchar */
    1797             : static GEN
    1798        5012 : mfchardiv(GEN CHI1, GEN CHI2)
    1799             : {
    1800        5012 :   char2(&CHI1, &CHI2); return mfchardiv_i(CHI1,CHI2);
    1801             : }
    1802             : static GEN
    1803          49 : mfcharconj(GEN CHI)
    1804             : {
    1805          49 :   CHI = leafcopy(CHI);
    1806          49 :   gel(CHI,2) = zncharconj(gel(CHI,1), gel(CHI,2));
    1807          49 :   return CHI;
    1808             : }
    1809             : 
    1810             : /* CHI mfchar, assume 4 | N. Multiply CHI by \chi_{-4} */
    1811             : static GEN
    1812         882 : mfchilift(GEN CHI, long N)
    1813             : {
    1814         882 :   CHI = induceN(N, CHI);
    1815         882 :   return mfcharmul_i(CHI, induce(gel(CHI,1), stoi(-4)));
    1816             : }
    1817             : /* CHI defined mod N, N4 = N/4;
    1818             :  * if CHI is defined mod N4 return CHI;
    1819             :  * else if CHI' = CHI*(-4,.) is defined mod N4, return CHI' (primitive)
    1820             :  * else error */
    1821             : static GEN
    1822          35 : mfcharchiliftprim(GEN CHI, long N4)
    1823             : {
    1824          35 :   long FC = mfcharconductor(CHI);
    1825             :   GEN CHIP;
    1826          35 :   if (N4 % FC == 0) return CHI;
    1827          14 :   CHIP = mfchartoprimitive(mfchilift(CHI, N4 << 2), &FC);
    1828          14 :   if (N4 % FC) pari_err_TYPE("mfkohnenbasis [incorrect CHI]", CHI);
    1829          14 :   return CHIP;
    1830             : }
    1831             : /* ensure CHI(-1) = (-1)^k [k integer] or 1 [half-integer], by multiplying
    1832             :  * by (-4/.) if needed */
    1833             : static GEN
    1834        2576 : mfchiadjust(GEN CHI, GEN gk, long N)
    1835             : {
    1836        2576 :   long par = mfcharparity(CHI);
    1837        2576 :   if (typ(gk) == t_INT &&  mpodd(gk)) par = -par;
    1838        2576 :   return par == 1 ? CHI : mfchilift(CHI, N);
    1839             : }
    1840             : 
    1841             : static GEN
    1842        3654 : mfsamefield(GEN T, GEN P, GEN Q)
    1843             : {
    1844        3654 :   if (degpol(P) == 1) return Q;
    1845         602 :   if (degpol(Q) == 1) return P;
    1846         511 :   if (!gequal(P,Q)) pari_err_TYPE("mfsamefield [different fields]",mkvec2(P,Q));
    1847         504 :   if (T) err_cyclo();
    1848         504 :   return P;
    1849             : }
    1850             : 
    1851             : GEN
    1852         455 : mfmul(GEN f, GEN g)
    1853             : {
    1854         455 :   pari_sp av = avma;
    1855             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    1856         455 :   if (!checkmf_i(f)) pari_err_TYPE("mfmul",f);
    1857         455 :   if (!checkmf_i(g)) pari_err_TYPE("mfmul",g);
    1858         455 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1859         455 :   K = gadd(mf_get_gk(f), mf_get_gk(g));
    1860         455 :   CHIf = mf_get_CHI(f);
    1861         455 :   CHIg = mf_get_CHI(g);
    1862         455 :   CHI = mfchiadjust(mfcharmul(CHIf,CHIg), K, itos(N));
    1863         455 :   T = chicompat(CHI, CHIf, CHIg);
    1864         455 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    1865         448 :   return gerepilecopy(av, T? tag3(t_MF_MUL,NK,f,g,T): tag2(t_MF_MUL,NK,f,g));
    1866             : }
    1867             : GEN
    1868          63 : mfpow(GEN f, long n)
    1869             : {
    1870          63 :   pari_sp av = avma;
    1871             :   GEN T, KK, NK, gn, CHI, CHIf;
    1872          63 :   if (!checkmf_i(f)) pari_err_TYPE("mfpow",f);
    1873          63 :   if (!n) return mf1();
    1874          63 :   if (n == 1) return gcopy(f);
    1875          63 :   KK = gmulsg(n,mf_get_gk(f));
    1876          63 :   gn = stoi(n);
    1877          63 :   CHIf = mf_get_CHI(f);
    1878          63 :   CHI = mfchiadjust(mfcharpow(CHIf,gn), KK, mf_get_N(f));
    1879          63 :   T = chicompat(CHI, CHIf, CHIf);
    1880          56 :   NK = mkgNK(mf_get_gN(f), KK, CHI, mf_get_field(f));
    1881          56 :   return gerepilecopy(av, T? tag3(t_MF_POW,NK,f,gn,T): tag2(t_MF_POW,NK,f,gn));
    1882             : }
    1883             : GEN
    1884          28 : mfbracket(GEN f, GEN g, long m)
    1885             : {
    1886          28 :   pari_sp av = avma;
    1887             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    1888          28 :   if (!checkmf_i(f)) pari_err_TYPE("mfbracket",f);
    1889          28 :   if (!checkmf_i(g)) pari_err_TYPE("mfbracket",g);
    1890          28 :   if (m < 0) pari_err_TYPE("mfbracket [m<0]",stoi(m));
    1891          28 :   K = gaddgs(gadd(mf_get_gk(f), mf_get_gk(g)), 2*m);
    1892          28 :   if (gsigne(K) < 0) pari_err_IMPL("mfbracket for this form");
    1893          28 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1894          28 :   CHIf = mf_get_CHI(f);
    1895          28 :   CHIg = mf_get_CHI(g);
    1896          28 :   CHI = mfcharmul(CHIf, CHIg);
    1897          28 :   CHI = mfchiadjust(CHI, K, itou(N));
    1898          28 :   T = chicompat(CHI, CHIf, CHIg);
    1899          28 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    1900          56 :   return gerepilecopy(av, T? tag4(t_MF_BRACKET, NK, f, g, utoi(m), T)
    1901          28 :                            : tag3(t_MF_BRACKET, NK, f, g, utoi(m)));
    1902             : }
    1903             : 
    1904             : /* remove 0 entries in L */
    1905             : static int
    1906        1253 : mflinear_strip(GEN *pF, GEN *pL)
    1907             : {
    1908        1253 :   pari_sp av = avma;
    1909        1253 :   GEN F = *pF, L = *pL;
    1910        1253 :   long i, j, l = lg(L);
    1911        1253 :   GEN F2 = cgetg(l, t_VEC), L2 = cgetg(l, t_VEC);
    1912        7679 :   for (i = j = 1; i < l; i++)
    1913             :   {
    1914        6426 :     if (gequal0(gel(L,i))) continue;
    1915        3626 :     gel(F2,j) = gel(F,i);
    1916        3626 :     gel(L2,j) = gel(L,i); j++;
    1917             :   }
    1918        1253 :   if (j == l) set_avma(av);
    1919             :   else
    1920             :   {
    1921         371 :     setlg(F2,j); *pF = F2;
    1922         371 :     setlg(L2,j); *pL = L2;
    1923             :   }
    1924        1253 :   return (j > 1);
    1925             : }
    1926             : static GEN
    1927        5544 : taglinear_i(long t, GEN NK, GEN F, GEN L)
    1928             : {
    1929             :   GEN dL;
    1930        5544 :   L = Q_remove_denom(L, &dL); if (!dL) dL = gen_1;
    1931        5544 :   return tag3(t, NK, F, L, dL);
    1932             : }
    1933             : static GEN
    1934        2268 : taglinear(GEN NK, GEN F, GEN L)
    1935             : {
    1936        2268 :   long t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1937        2268 :    return taglinear_i(t, NK, F, L);
    1938             : }
    1939             : /* assume F has parameters NK = [N,K,CHI] */
    1940             : static GEN
    1941         294 : mflinear_i(GEN NK, GEN F, GEN L)
    1942             : {
    1943         294 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1944         294 :   return taglinear(NK, F,L);
    1945             : }
    1946             : static GEN
    1947         511 : mflinear_bhn(GEN mf, GEN L)
    1948             : {
    1949             :   long i, l;
    1950         511 :   GEN P, NK, F = MF_get_S(mf);
    1951         511 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1952         504 :   l = lg(L); P = pol_x(1);
    1953        2653 :   for (i = 1; i < l; i++)
    1954             :   {
    1955        2149 :     GEN c = gel(L,i);
    1956        2149 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1)
    1957         518 :       P = mfsamefield(NULL, P, gel(c,1));
    1958             :   }
    1959         504 :   NK = mkgNK(MF_get_gN(mf), MF_get_gk(mf), MF_get_CHI(mf), P);
    1960         504 :   return taglinear_i(t_MF_LINEAR_BHN,  NK, F,L);
    1961             : }
    1962             : 
    1963             : /* F vector of forms with same weight and character but varying level, return
    1964             :  * global [N,k,chi,P] */
    1965             : static GEN
    1966        2821 : vecmfNK(GEN F)
    1967             : {
    1968        2821 :   long i, l = lg(F);
    1969             :   GEN N, f;
    1970        2821 :   if (l == 1) return mkNK(1, 0, mfchartrivial());
    1971        2821 :   f = gel(F,1); N = mf_get_gN(f);
    1972       34489 :   for (i = 2; i < l; i++) N = lcmii(N, mf_get_gN(gel(F,i)));
    1973        2821 :   return mkgNK(N, mf_get_gk(f), mf_get_CHI(f), mf_get_field(f));
    1974             : }
    1975             : /* do not use mflinear: mflineardivtomat rely on F being constant across the
    1976             :  * basis where mflinear strips the ones matched by 0 coeffs. Assume k and CHI
    1977             :  * constant, N is allowed to vary. */
    1978             : static GEN
    1979        1099 : vecmflinear(GEN F, GEN C)
    1980             : {
    1981        1099 :   long i, t, l = lg(C);
    1982        1099 :   GEN NK, v = cgetg(l, t_VEC);
    1983        1099 :   if (l == 1) return v;
    1984        1099 :   t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1985        1099 :   NK = vecmfNK(F);
    1986        3871 :   for (i = 1; i < l; i++) gel(v,i) = taglinear_i(t, NK, F, gel(C,i));
    1987        1099 :   return v;
    1988             : }
    1989             : /* vecmflinear(F,C), then divide everything by E, which has valuation 0 */
    1990             : static GEN
    1991         371 : vecmflineardiv0(GEN F, GEN C, GEN E)
    1992             : {
    1993         371 :   GEN v = vecmflinear(F, C);
    1994         371 :   long i, l = lg(v);
    1995         371 :   if (l == 1) return v;
    1996         371 :   gel(v,1) = mfdiv_val(gel(v,1), E, 0);
    1997        1456 :   for (i = 2; i < l; i++)
    1998             :   { /* v[i] /= E */
    1999        1085 :     GEN f = shallowcopy(gel(v,1));
    2000        1085 :     gel(f,2) = gel(v,i);
    2001        1085 :     gel(v,i) = f;
    2002             :   }
    2003         371 :   return v;
    2004             : }
    2005             : 
    2006             : /* Non empty linear combination of linear combinations of same
    2007             :  * F_j=\sum_i \mu_{i,j}G_i so R = \sum_i (\sum_j(\la_j\mu_{i,j})) G_i */
    2008             : static GEN
    2009        1722 : mflinear_linear(GEN F, GEN L, int strip)
    2010             : {
    2011        1722 :   long l = lg(F), j;
    2012        1722 :   GEN vF, M = cgetg(l, t_MAT);
    2013        1722 :   L = shallowcopy(L);
    2014       16842 :   for (j = 1; j < l; j++)
    2015             :   {
    2016       15120 :     GEN f = gel(F,j), c = gel(f,3), d = gel(f,4);
    2017       15120 :     if (typ(c) == t_VEC) c = shallowtrans(c);
    2018       15120 :     if (!isint1(d)) gel(L,j) = gdiv(gel(L,j),d);
    2019       15120 :     gel(M,j) = c;
    2020             :   }
    2021        1722 :   vF = gmael(F,1,2); L = RgM_RgC_mul(M,L);
    2022        1722 :   if (strip && !mflinear_strip(&vF,&L)) return mftrivial();
    2023        1722 :   return taglinear(vecmfNK(vF), vF, L);
    2024             : }
    2025             : /* F non-empty vector of forms of the form mfdiv(mflinear(B,v), E) where E
    2026             :  * does not vanish at oo, or mflinear(B,v). Apply mflinear(F, L) */
    2027             : static GEN
    2028        1722 : mflineardiv_linear(GEN F, GEN L, int strip)
    2029             : {
    2030        1722 :   long l = lg(F), j;
    2031             :   GEN v, E, f;
    2032        1722 :   if (lg(L) != l) pari_err_DIM("mflineardiv_linear");
    2033        1722 :   f = gel(F,1); /* l > 1 */
    2034        1722 :   if (mf_get_type(f) != t_MF_DIV) return mflinear_linear(F,L,strip);
    2035        1547 :   E = gel(f,3);
    2036        1547 :   v = cgetg(l, t_VEC);
    2037       16303 :   for (j = 1; j < l; j++) { GEN f = gel(F,j); gel(v,j) = gel(f,2); }
    2038        1547 :   return mfdiv_val(mflinear_linear(v,L,strip), E, 0);
    2039             : }
    2040             : static GEN
    2041         434 : vecmflineardiv_linear(GEN F, GEN M)
    2042             : {
    2043         434 :   long i, l = lg(M);
    2044         434 :   GEN v = cgetg(l, t_VEC);
    2045        1792 :   for (i = 1; i < l; i++) gel(v,i) = mflineardiv_linear(F, gel(M,i), 0);
    2046         434 :   return v;
    2047             : }
    2048             : 
    2049             : static GEN
    2050         623 : tobasis(GEN mf, GEN F, GEN L)
    2051             : {
    2052         623 :   if (checkmf_i(L) && mf) return mftobasis(mf, L, 0);
    2053         616 :   if (typ(F) != t_VEC) pari_err_TYPE("mflinear",F);
    2054         616 :   if (!is_vec_t(typ(L))) pari_err_TYPE("mflinear",L);
    2055         616 :   if (lg(L) != lg(F)) pari_err_DIM("mflinear");
    2056         616 :   return L;
    2057             : }
    2058             : GEN
    2059         665 : mflinear(GEN F, GEN L)
    2060             : {
    2061         665 :   pari_sp av = avma;
    2062         665 :   GEN G, NK, P, mf = checkMF_i(F), N = NULL, K = NULL, CHI = NULL;
    2063             :   long i, l;
    2064         665 :   if (mf)
    2065             :   {
    2066         525 :     GEN gk = MF_get_gk(mf);
    2067         525 :     F = MF_get_basis(F);
    2068         525 :     if (typ(gk) != t_INT)
    2069          42 :       return gerepilecopy(av, mflineardiv_linear(F, L, 1));
    2070         483 :     if (itou(gk) > 1 && space_is_cusp(MF_get_space(mf)))
    2071             :     {
    2072         266 :       L = tobasis(mf, F, L);
    2073         266 :       return gerepilecopy(av, mflinear_bhn(mf, L));
    2074             :     }
    2075             :   }
    2076         357 :   L = tobasis(mf, F, L);
    2077         357 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    2078             : 
    2079         350 :   l = lg(F);
    2080         350 :   if (l == 2 && gequal1(gel(L,1))) return gerepilecopy(av, gel(F,1));
    2081         266 :   P = pol_x(1);
    2082         854 :   for (i = 1; i < l; i++)
    2083             :   {
    2084         595 :     GEN f = gel(F,i), c = gel(L,i), Ni, Ki;
    2085         595 :     if (!checkmf_i(f)) pari_err_TYPE("mflinear", f);
    2086         595 :     Ni = mf_get_gN(f); N = N? lcmii(N, Ni): Ni;
    2087         595 :     Ki = mf_get_gk(f);
    2088         595 :     if (!K) K = Ki;
    2089         329 :     else if (!gequal(K, Ki))
    2090           7 :       pari_err_TYPE("mflinear [different weights]", mkvec2(K,Ki));
    2091         588 :     P = mfsamefield(NULL, P, mf_get_field(f));
    2092         588 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1)
    2093         126 :       P = mfsamefield(NULL, P, gel(c,1));
    2094             :   }
    2095         259 :   G = znstar0(N,1);
    2096         833 :   for (i = 1; i < l; i++)
    2097             :   {
    2098         581 :     GEN CHI2 = mf_get_CHI(gel(F,i));
    2099         581 :     CHI2 = induce(G, CHI2);
    2100         581 :     if (!CHI) CHI = CHI2;
    2101         322 :     else if (!gequal(CHI, CHI2))
    2102           7 :       pari_err_TYPE("mflinear [different characters]", mkvec2(CHI,CHI2));
    2103             :   }
    2104         252 :   NK = mkgNK(N, K, CHI, P);
    2105         252 :   return gerepilecopy(av, taglinear(NK,F,L));
    2106             : }
    2107             : 
    2108             : GEN
    2109          42 : mfshift(GEN F, long sh)
    2110             : {
    2111          42 :   pari_sp av = avma;
    2112          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfshift",F);
    2113          42 :   return gerepilecopy(av, tag2(t_MF_SHIFT, mf_get_NK(F), F, stoi(sh)));
    2114             : }
    2115             : static long
    2116          49 : mfval(GEN F)
    2117             : {
    2118          49 :   pari_sp av = avma;
    2119          49 :   long i = 0, n, sb;
    2120             :   GEN gk, gN;
    2121          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfval", F);
    2122          49 :   gN = mf_get_gN(F);
    2123          49 :   gk = mf_get_gk(F);
    2124          49 :   sb = mfsturmNgk(itou(gN), gk);
    2125          70 :   for (n = 1; n <= sb;)
    2126             :   {
    2127             :     GEN v;
    2128          63 :     if (n > 0.5*sb) n = sb+1;
    2129          63 :     v = mfcoefs_i(F, n, 1);
    2130         119 :     for (; i <= n; i++)
    2131          98 :       if (!gequal0(gel(v, i+1))) return gc_long(av,i);
    2132          21 :     n <<= 1;
    2133             :   }
    2134           7 :   return gc_long(av,-1);
    2135             : }
    2136             : 
    2137             : GEN
    2138        1946 : mfdiv_val(GEN f, GEN g, long vg)
    2139             : {
    2140             :   GEN T, N, K, NK, CHI, CHIf, CHIg;
    2141        1946 :   if (vg) { f = mfshift(f,vg); g = mfshift(g,vg); }
    2142        1946 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    2143        1946 :   K = gsub(mf_get_gk(f), mf_get_gk(g));
    2144        1946 :   CHIf = mf_get_CHI(f);
    2145        1946 :   CHIg = mf_get_CHI(g);
    2146        1946 :   CHI = mfchiadjust(mfchardiv(CHIf, CHIg), K, itos(N));
    2147        1946 :   T = chicompat(CHI, CHIf, CHIg);
    2148        1939 :   NK = mkgNK(N, K, CHI, mfsamefield(T, mf_get_field(f), mf_get_field(g)));
    2149        1939 :   return T? tag3(t_MF_DIV, NK, f, g, T): tag2(t_MF_DIV, NK, f, g);
    2150             : }
    2151             : GEN
    2152          49 : mfdiv(GEN F, GEN G)
    2153             : {
    2154          49 :   pari_sp av = avma;
    2155          49 :   long v = mfval(G);
    2156          49 :   if (!checkmf_i(F)) pari_err_TYPE("mfdiv", F);
    2157          42 :   if (v < 0 || (v && !gequal0(mfcoefs(F, v-1, 1))))
    2158          14 :     pari_err_DOMAIN("mfdiv", "ord(G)", ">", strtoGENstr("ord(F)"),
    2159             :                     mkvec2(F, G));
    2160          28 :   return gerepilecopy(av, mfdiv_val(F, G, v));
    2161             : }
    2162             : GEN
    2163         154 : mfderiv(GEN F, long m)
    2164             : {
    2165         154 :   pari_sp av = avma;
    2166             :   GEN NK, gk;
    2167         154 :   if (!checkmf_i(F)) pari_err_TYPE("mfderiv",F);
    2168         154 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2169         154 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2170         154 :   return gerepilecopy(av, tag2(t_MF_DERIV, NK, F, stoi(m)));
    2171             : }
    2172             : GEN
    2173          21 : mfderivE2(GEN F, long m)
    2174             : {
    2175          21 :   pari_sp av = avma;
    2176             :   GEN NK, gk;
    2177          21 :   if (!checkmf_i(F)) pari_err_TYPE("mfderivE2",F);
    2178          21 :   if (m < 0) pari_err_DOMAIN("mfderivE2","m","<",gen_0,stoi(m));
    2179          21 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2180          21 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2181          21 :   return gerepilecopy(av, tag2(t_MF_DERIVE2, NK, F, stoi(m)));
    2182             : }
    2183             : 
    2184             : GEN
    2185          14 : mftwist(GEN F, GEN D)
    2186             : {
    2187          14 :   pari_sp av = avma;
    2188             :   GEN NK, CHI, NT, Da;
    2189             :   long q;
    2190          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftwist", F);
    2191          14 :   if (typ(D) != t_INT) pari_err_TYPE("mftwist", D);
    2192          14 :   Da = mpabs_shallow(D);
    2193          14 :   CHI = mf_get_CHI(F); q = mfcharconductor(CHI);
    2194          14 :   NT = glcm(glcm(mf_get_gN(F), mulsi(q, Da)), sqri(Da));
    2195          14 :   NK = mkgNK(NT, mf_get_gk(F), CHI, mf_get_field(F));
    2196          14 :   return gerepilecopy(av, tag2(t_MF_TWIST, NK, F, D));
    2197             : }
    2198             : 
    2199             : /***************************************************************/
    2200             : /*                 Generic cache handling                      */
    2201             : /***************************************************************/
    2202             : enum { cache_FACT, cache_DIV, cache_H, cache_D, cache_DIH };
    2203             : typedef struct {
    2204             :   const char *name;
    2205             :   GEN cache;
    2206             :   ulong minself, maxself;
    2207             :   void (*init)(long);
    2208             :   ulong miss, maxmiss;
    2209             :   long compressed;
    2210             : } cache;
    2211             : 
    2212             : static void constfact(long lim);
    2213             : static void constdiv(long lim);
    2214             : static void consttabh(long lim);
    2215             : static void consttabdihedral(long lim);
    2216             : static void constcoredisc(long lim);
    2217             : static THREAD cache caches[] = {
    2218             : { "Factors",  NULL,  50000,    50000, &constfact, 0, 0, 0 },
    2219             : { "Divisors", NULL,  50000,    50000, &constdiv, 0, 0, 0 },
    2220             : { "H",        NULL, 100000, 10000000, &consttabh, 0, 0, 1 },
    2221             : { "CorediscF",NULL, 100000, 10000000, &constcoredisc, 0, 0, 0 },
    2222             : { "Dihedral", NULL,   1000,     3000, &consttabdihedral, 0, 0, 0 },
    2223             : };
    2224             : 
    2225             : static void
    2226         399 : cache_reset(long id) { caches[id].miss = caches[id].maxmiss = 0; }
    2227             : static void
    2228        8400 : cache_delete(long id) { guncloneNULL(caches[id].cache); }
    2229             : static void
    2230         413 : cache_set(long id, GEN S)
    2231             : {
    2232         413 :   GEN old = caches[id].cache;
    2233         413 :   caches[id].cache = gclone(S);
    2234         413 :   guncloneNULL(old);
    2235         413 : }
    2236             : 
    2237             : /* handle a cache miss: store stats, possibly reset table; return value
    2238             :  * if (now) cached; return NULL on failure. HACK: some caches contain an
    2239             :  * ulong where the 0 value is impossible, and return it (typecast to GEN) */
    2240             : static GEN
    2241   221502864 : cache_get(long id, ulong D)
    2242             : {
    2243   221502864 :   cache *S = &caches[id];
    2244   221502864 :   const ulong d = S->compressed? D>>1: D;
    2245             :   ulong max, l;
    2246             : 
    2247   221502864 :   if (!S->cache)
    2248             :   {
    2249         266 :     max = maxuu(minuu(D, S->maxself), S->minself);
    2250         266 :     S->init(max);
    2251         266 :     l = lg(S->cache);
    2252             :   }
    2253             :   else
    2254             :   {
    2255   221502598 :     l = lg(S->cache);
    2256   221502598 :     if (l <= d)
    2257             :     {
    2258         994 :       if (D > S->maxmiss) S->maxmiss = D;
    2259         994 :       if (DEBUGLEVEL >= 3)
    2260           0 :         err_printf("miss in cache %s: %lu, max = %lu\n",
    2261             :                    S->name, D, S->maxmiss);
    2262         994 :       if (S->miss++ >= 5 && D < S->maxself)
    2263             :       {
    2264          84 :         max = minuu(S->maxself, (long)(S->maxmiss * 1.2));
    2265          84 :         if (max <= S->maxself)
    2266             :         {
    2267          84 :           if (DEBUGLEVEL >= 3)
    2268           0 :             err_printf("resetting cache %s to %lu\n", S->name, max);
    2269          84 :           S->init(max); l = lg(S->cache);
    2270             :         }
    2271             :       }
    2272             :     }
    2273             :   }
    2274   221502864 :   return (l <= d)? NULL: gel(S->cache, d);
    2275             : }
    2276             : static GEN
    2277          70 : cache_report(long id)
    2278             : {
    2279          70 :   cache *S = &caches[id];
    2280          70 :   GEN v = zerocol(5);
    2281          70 :   gel(v,1) = strtoGENstr(S->name);
    2282          70 :   if (S->cache)
    2283             :   {
    2284          35 :     gel(v,2) = utoi(lg(S->cache)-1);
    2285          35 :     gel(v,3) = utoi(S->miss);
    2286          35 :     gel(v,4) = utoi(S->maxmiss);
    2287          35 :     gel(v,5) = utoi(gsizebyte(S->cache));
    2288             :   }
    2289          70 :   return v;
    2290             : }
    2291             : GEN
    2292          14 : getcache(void)
    2293             : {
    2294          14 :   pari_sp av = avma;
    2295          14 :   GEN M = cgetg(6, t_MAT);
    2296          14 :   gel(M,1) = cache_report(cache_FACT);
    2297          14 :   gel(M,2) = cache_report(cache_DIV);
    2298          14 :   gel(M,3) = cache_report(cache_H);
    2299          14 :   gel(M,4) = cache_report(cache_D);
    2300          14 :   gel(M,5) = cache_report(cache_DIH);
    2301          14 :   return gerepilecopy(av, shallowtrans(M));
    2302             : }
    2303             : 
    2304             : void
    2305        1680 : pari_close_mf(void)
    2306             : {
    2307        1680 :   cache_delete(cache_FACT);
    2308        1680 :   cache_delete(cache_DIV);
    2309        1680 :   cache_delete(cache_H);
    2310        1680 :   cache_delete(cache_D);
    2311        1680 :   cache_delete(cache_DIH);
    2312        1680 : }
    2313             : 
    2314             : /*************************************************************************/
    2315             : /* a odd, update local cache (recycle memory) */
    2316             : static GEN
    2317        2032 : update_factor_cache(long a, long lim, long *pb)
    2318             : {
    2319        2032 :   const long step = 16000; /* even; don't increase this: RAM cache thrashing */
    2320        2032 :   if (a + 2*step > lim)
    2321         224 :     *pb = lim; /* fuse last 2 chunks */
    2322             :   else
    2323        1808 :     *pb = a + step;
    2324        2032 :   return vecfactoroddu_i(a, *pb);
    2325             : }
    2326             : /* assume lim < MAX_LONG/8 */
    2327             : static void
    2328          77 : constcoredisc(long lim)
    2329             : {
    2330          77 :   pari_sp av2, av = avma;
    2331          77 :   GEN D = caches[cache_D].cache, CACHE = NULL;
    2332          77 :   long cachea, cacheb, N, LIM = !D ? 4 : lg(D)-1;
    2333          77 :   if (lim <= 0) lim = 5;
    2334          77 :   if (lim <= LIM) return;
    2335          77 :   cache_reset(cache_D);
    2336          77 :   D = zero_zv(lim);
    2337          77 :   av2 = avma;
    2338          77 :   cachea = cacheb = 0;
    2339     8253336 :   for (N = 1; N <= lim; N+=2)
    2340             :   { /* N odd */
    2341             :     long i, d, d2;
    2342             :     GEN F;
    2343     8253259 :     if (N > cacheb)
    2344             :     {
    2345        1008 :       set_avma(av2); cachea = N;
    2346        1008 :       CACHE = update_factor_cache(N, lim, &cacheb);
    2347             :     }
    2348     8253259 :     F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2349     8253259 :     D[N] = d = corediscs_fact(F); /* = 3 mod 4 or 4 mod 16 */
    2350     8253259 :     d2 = odd(d)? d<<3: d<<1;
    2351     8253259 :     for (i = 1;;)
    2352             :     {
    2353    11004329 :       if ((N << i) > lim) break;
    2354     5502160 :       D[N<<i] = d2; i++;
    2355     5502160 :       if ((N << i) > lim) break;
    2356     2751070 :       D[N<<i] = d; i++;
    2357             :     }
    2358             :   }
    2359          77 :   cache_set(cache_D, D);
    2360          77 :   set_avma(av);
    2361             : }
    2362             : 
    2363             : static void
    2364         112 : constfact(long lim)
    2365             : {
    2366             :   pari_sp av;
    2367         112 :   GEN VFACT = caches[cache_FACT].cache;
    2368         112 :   long LIM = VFACT? lg(VFACT)-1: 4;
    2369         112 :   if (lim <= 0) lim = 5;
    2370         112 :   if (lim <= LIM) return;
    2371          91 :   cache_reset(cache_FACT); av = avma;
    2372          91 :   cache_set(cache_FACT, vecfactoru_i(1,lim)); set_avma(av);
    2373             : }
    2374             : static void
    2375          84 : constdiv(long lim)
    2376             : {
    2377             :   pari_sp av;
    2378          84 :   GEN VFACT, VDIV = caches[cache_DIV].cache;
    2379          84 :   long N, LIM = VDIV? lg(VDIV)-1: 4;
    2380          84 :   if (lim <= 0) lim = 5;
    2381          84 :   if (lim <= LIM) return;
    2382          84 :   constfact(lim);
    2383          84 :   VFACT = caches[cache_FACT].cache;
    2384          84 :   cache_reset(cache_DIV); av = avma;
    2385          84 :   VDIV  = cgetg(lim+1, t_VEC);
    2386     4200084 :   for (N = 1; N <= lim; N++) gel(VDIV,N) = divisorsu_fact(gel(VFACT,N));
    2387          84 :   cache_set(cache_DIV, VDIV); set_avma(av);
    2388             : }
    2389             : 
    2390             : /* n > 1, D = divisors(n); sets L = 2*lambda(n), S = sigma(n) */
    2391             : static void
    2392    10567586 : lamsig(GEN D, long *pL, long *pS)
    2393             : {
    2394    10567586 :   pari_sp av = avma;
    2395    10567586 :   long i, l = lg(D), L = 1, S = D[l-1]+1;
    2396    37807024 :   for (i = 2; i < l; i++) /* skip d = 1 */
    2397             :   {
    2398    37807024 :     long d = D[i], nd = D[l-i]; /* nd = n/d */
    2399    37807024 :     if (d < nd) { L += d; S += d + nd; }
    2400             :     else
    2401             :     {
    2402    10567586 :       L <<= 1; if (d == nd) { L += d; S += d; }
    2403    10567586 :       break;
    2404             :     }
    2405             :   }
    2406    10567586 :   set_avma(av); *pL = L; *pS = S;
    2407    10567586 : }
    2408             : /* table of 6 * Hurwitz class numbers D <= lim */
    2409             : static void
    2410         147 : consttabh(long lim)
    2411             : {
    2412         147 :   pari_sp av = avma, av2;
    2413         147 :   GEN VHDH0, VDIV, CACHE = NULL;
    2414         147 :   GEN VHDH = caches[cache_H].cache;
    2415         147 :   long r, N, cachea, cacheb, lim0 = VHDH? lg(VHDH)-1: 2, LIM = lim0 << 1;
    2416             : 
    2417         147 :   if (lim <= 0) lim = 5;
    2418         147 :   if (lim <= LIM) return;
    2419         147 :   cache_reset(cache_H);
    2420         147 :   r = lim&3L; if (r) lim += 4-r;
    2421         147 :   cache_get(cache_DIV, lim);
    2422         147 :   VDIV = caches[cache_DIV].cache;
    2423         147 :   VHDH0 = cgetg(lim/2 + 1, t_VECSMALL);
    2424         147 :   VHDH0[1] = 2;
    2425         147 :   VHDH0[2] = 3;
    2426     6122627 :   for (N = 3; N <= lim0; N++) VHDH0[N] = VHDH[N];
    2427         147 :   av2 = avma;
    2428         147 :   cachea = cacheb = 0;
    2429     5283940 :   for (N = LIM + 3; N <= lim; N += 4)
    2430             :   {
    2431     5283793 :     long s = 0, limt = usqrt(N>>2), flsq = 0, ind, t, L, S;
    2432             :     GEN DN, DN2;
    2433     5283793 :     if (N + 2 >= lg(VDIV))
    2434             :     { /* use local cache */
    2435             :       GEN F;
    2436     4233961 :       if (N + 2 > cacheb)
    2437             :       {
    2438        1024 :         set_avma(av2); cachea = N;
    2439        1024 :         CACHE = update_factor_cache(N, lim+2, &cacheb);
    2440             :       }
    2441     4233961 :       F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2442     4233961 :       DN = divisorsu_fact(F);
    2443     4233961 :       F = gel(CACHE, ((N-cachea)>>1)+2); /* factoru(N+2) */
    2444     4233961 :       DN2 = divisorsu_fact(F);
    2445             :     }
    2446             :     else
    2447             :     { /* use global cache */
    2448     1049832 :       DN = gel(VDIV,N);
    2449     1049832 :       DN2 = gel(VDIV,N+2);
    2450             :     }
    2451     5283793 :     ind = N >> 1;
    2452  1115411521 :     for (t = 1; t <= limt; t++)
    2453             :     {
    2454  1110127728 :       ind -= (t<<2)-2; /* N/2 - 2t^2 */
    2455  1110127728 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2456             :     }
    2457     5283793 :     lamsig(DN, &L,&S);
    2458     5283793 :     VHDH0[N >> 1] = 2*S - 3*L - 2*s + flsq;
    2459     5283793 :     s = 0; flsq = 0; limt = (usqrt(N+2) - 1) >> 1;
    2460     5283793 :     ind = (N+1) >> 1;
    2461  1112795533 :     for (t = 1; t <= limt; t++)
    2462             :     {
    2463  1107511740 :       ind -= t<<2; /* (N+1)/2 - 2t(t+1) */
    2464  1107511740 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2465             :     }
    2466     5283793 :     lamsig(DN2, &L,&S);
    2467     5283793 :     VHDH0[(N+1) >> 1] = S - 3*(L >> 1) - s - flsq;
    2468             :   }
    2469         147 :   cache_set(cache_H, VHDH0); set_avma(av);
    2470             : }
    2471             : 
    2472             : /*************************************************************************/
    2473             : /* Core functions using factorizations, divisors of class numbers caches */
    2474             : /* TODO: myfactoru and factorization cache should be exported */
    2475             : static GEN
    2476    21289583 : myfactoru(long N)
    2477             : {
    2478    21289583 :   GEN z = cache_get(cache_FACT, N);
    2479    21289583 :   return z? gcopy(z): factoru(N);
    2480             : }
    2481             : static GEN
    2482    46625292 : mydivisorsu(long N)
    2483             : {
    2484    46625292 :   GEN z = cache_get(cache_DIV, N);
    2485    46625292 :   return z? leafcopy(z): divisorsu(N);
    2486             : }
    2487             : /* write -n = Df^2, D < 0 fundamental discriminant. Return D, set f. */
    2488             : static long
    2489    80347687 : mycoredisc2neg(ulong n, long *pf)
    2490             : {
    2491    80347687 :   ulong m, D = (ulong)cache_get(cache_D, n);
    2492    80347687 :   if (D) { *pf = usqrt(n/D); return -(long)D; }
    2493         196 :   m = mycore(n, pf);
    2494         196 :   if ((m&3) != 3) { m <<= 2; *pf >>= 1; }
    2495         196 :   return (long)-m;
    2496             : }
    2497             : /* write n = Df^2, D > 0 fundamental discriminant. Return D, set f. */
    2498             : static long
    2499          14 : mycoredisc2pos(ulong n, long *pf)
    2500             : {
    2501          14 :   ulong m = mycore(n, pf);
    2502          14 :   if ((m&3) != 1) { m <<= 2; *pf >>= 1; }
    2503          14 :   return (long)m;
    2504             : }
    2505             : 
    2506             : /* 1+p+...+p^e, e >= 1 */
    2507             : static ulong
    2508          49 : usumpow(ulong p, long e)
    2509             : {
    2510          49 :   ulong q = 1+p;
    2511             :   long i;
    2512          98 :   for (i = 1; i < e; i++) q = p*q + 1;
    2513          49 :   return q;
    2514             : }
    2515             : /* Hurwitz(D0 F^2)/ Hurwitz(D0)
    2516             :  * = \sum_{f|F}  f \prod_{p|f} (1-kro(D0/p)/p)
    2517             :  * = \prod_{p^e || F} (1 + (p^e-1) / (p-1) * (p-kro(D0/p))) */
    2518             : static long
    2519         294 : get_sh(long F, long D0)
    2520             : {
    2521         294 :   GEN fa = myfactoru(F), P = gel(fa,1), E = gel(fa,2);
    2522         294 :   long i, l = lg(P), t = 1;
    2523         794 :   for (i = 1; i < l; i++)
    2524             :   {
    2525         500 :     long p = P[i], e = E[i], s = kross(D0,p);
    2526         500 :     if (e == 1) { t *= 1 + p - s; continue; }
    2527         153 :     if (s == 1) { t *= upowuu(p,e); continue; }
    2528          49 :     t *= 1 + usumpow(p,e-1)*(p-s);
    2529             :   }
    2530         294 :   return t;
    2531             : }
    2532             : /* d > 0, d = 0,3 (mod 4). Return 6*hclassno(d); -d must be fundamental
    2533             :  * Faster than quadclassunit up to 5*10^5 or so */
    2534             : static ulong
    2535          42 : hclassno6u_count(ulong d)
    2536             : {
    2537          42 :   ulong a, b, b2, h = 0;
    2538          42 :   int f = 0;
    2539             : 
    2540          42 :   if (d > 500000)
    2541           7 :     return 6 * itou(gel(quadclassunit0(utoineg(d), 0, NULL, 0), 1));
    2542             : 
    2543             :   /* this part would work with -d non fundamental */
    2544          35 :   b = d&1; b2 = (1+d)>>2;
    2545          35 :   if (!b)
    2546             :   {
    2547           0 :     for (a=1; a*a<b2; a++)
    2548           0 :       if (b2%a == 0) h++;
    2549           0 :     f = (a*a==b2); b=2; b2=(4+d)>>2;
    2550             :   }
    2551        7133 :   while (b2*3 < d)
    2552             :   {
    2553        7098 :     if (b2%b == 0) h++;
    2554     1188551 :     for (a=b+1; a*a < b2; a++)
    2555     1181453 :       if (b2%a == 0) h += 2;
    2556        7098 :     if (a*a == b2) h++;
    2557        7098 :     b += 2; b2 = (b*b+d)>>2;
    2558             :   }
    2559          35 :   if (b2*3 == d) return 6*h+2;
    2560          35 :   if (f) return 6*h+3;
    2561          35 :   return 6*h;
    2562             : }
    2563             : /* D > 0; 6 * hclassno(D), using D = D0*F^2 */
    2564             : static long
    2565         336 : hclassno6u_2(ulong D, long D0, long F)
    2566             : {
    2567             :   long h;
    2568         336 :   if (F == 1) h = hclassno6u_count(D);
    2569             :   else
    2570             :   { /* second chance */
    2571         294 :     h = (ulong)cache_get(cache_H, -D0);
    2572         294 :     if (!h) h = hclassno6u_count(-D0);
    2573         294 :     h *= get_sh(F,D0);
    2574             :   }
    2575         336 :   return h;
    2576             : }
    2577             : /* D > 0; 6 * hclassno(D) (6*Hurwitz). Beware, cached value for D (=0,3 mod 4)
    2578             :  * is stored at D>>1 */
    2579             : ulong
    2580      156249 : hclassno6u(ulong D)
    2581             : {
    2582      156249 :   ulong z = (ulong)cache_get(cache_H, D);
    2583             :   long D0, F;
    2584      156249 :   if (z) return z;
    2585         336 :   D0 = mycoredisc2neg(D, &F);
    2586         336 :   return hclassno6u_2(D,D0,F);
    2587             : }
    2588             : /* same, where the decomposition D = D0*F^2 is already known */
    2589             : static ulong
    2590    66552479 : hclassno6u_i(ulong D, long D0, long F)
    2591             : {
    2592    66552479 :   ulong z = (ulong)cache_get(cache_H, D);
    2593    66552479 :   if (z) return z;
    2594           0 :   return hclassno6u_2(D,D0,F);
    2595             : }
    2596             : 
    2597             : #if 0
    2598             : /* D > 0, return h(-D) [ordinary class number].
    2599             :  * Assume consttabh(D or more) was previously called */
    2600             : static long
    2601             : hfromH(long D)
    2602             : {
    2603             :   pari_sp ltop = avma;
    2604             :   GEN m, d, fa = myfactoru(D), P = gel(fa,1), E = gel(fa,2);
    2605             :   GEN VH = caches[cache_H].cache;
    2606             :   long i, nd, S, l = lg(P);
    2607             : 
    2608             :   /* n = d[i] loops through squarefree divisors of f, where f^2 = largest square
    2609             :    * divisor of N = |D|; m[i] = moebius(n) */
    2610             :   nd = 1 << (l-1);
    2611             :   d = cgetg(nd+1, t_VECSMALL);
    2612             :   m = cgetg(nd+1, t_VECSMALL);
    2613             :   d[1] = 1; S = VH[D >> 1]; /* 6 hclassno(-D) */
    2614             :   m[1] = 1; nd = 1;
    2615             :   i = 1;
    2616             :   if (P[1] == 2 && E[1] <= 3) /* need D/n^2 to be a discriminant */
    2617             :   { if (odd(E[1]) || (E[1] == 2 && (D & 15) == 4)) i = 2; }
    2618             :   for (; i<l; i++)
    2619             :   {
    2620             :     long j, p = P[i];
    2621             :     if (E[i] == 1) continue;
    2622             :     for (j=1; j<=nd; j++)
    2623             :     {
    2624             :       long n, s, hn;
    2625             :       d[nd+j] = n = d[j] * p;
    2626             :       m[nd+j] = s = - m[j]; /* moebius(n) */
    2627             :       hn = VH[(D/(n*n)) >> 1]; /* 6 hclassno(-D/n^2) */
    2628             :       if (s > 0) S += hn; else S -= hn;
    2629             :     }
    2630             :     nd <<= 1;
    2631             :   }
    2632             :   return gc_long(ltop, S/6);
    2633             : }
    2634             : #endif
    2635             : /* D < -4 fundamental, h(D), ordinary class number */
    2636             : static long
    2637     6507354 : myh(long D)
    2638             : {
    2639     6507354 :   ulong z = (ulong)cache_get(cache_H, -D);
    2640     6507354 :   if (z) return z/6; /* should be hfromH(-D) if D non-fundamental */
    2641           0 :   return itou(quadclassno(stoi(D)));
    2642             : }
    2643             : 
    2644             : /*************************************************************************/
    2645             : /*                          TRACE FORMULAS                               */
    2646             : /* CHIP primitive, initialize for t_POLMOD output */
    2647             : static GEN
    2648       29932 : mfcharinit(GEN CHIP)
    2649             : {
    2650       29932 :   long n, o, l, vt, N = mfcharmodulus(CHIP);
    2651             :   GEN c, v, V, G, Pn;
    2652       29932 :   if (N == 1) return mkvec2(mkvec(gen_1), pol_x(0));
    2653        5348 :   G = gel(CHIP,1);
    2654        5348 :   v = ncharvecexpo(G, znconrey_normalized(G, gel(CHIP,2)));
    2655        5348 :   l = lg(v); V = cgetg(l, t_VEC);
    2656        5348 :   o = mfcharorder(CHIP);
    2657        5348 :   Pn = mfcharpol(CHIP); vt = varn(Pn);
    2658        5348 :   if (o <= 2)
    2659             :   {
    2660       58135 :     for (n = 1; n < l; n++)
    2661             :     {
    2662       53739 :       if (v[n] < 0) c = gen_0; else c = v[n]? gen_m1: gen_1;
    2663       53739 :       gel(V,n) = c;
    2664             :     }
    2665             :   }
    2666             :   else
    2667             :   {
    2668       16835 :     for (n = 1; n < l; n++)
    2669             :     {
    2670       15883 :       if (v[n] < 0) c = gen_0;
    2671             :       else
    2672             :       {
    2673        8890 :         c = Qab_zeta(v[n], o, vt);
    2674        8890 :         if (typ(c) == t_POL && lg(c) >= lg(Pn)) c = RgX_rem(c, Pn);
    2675             :       }
    2676       15883 :       gel(V,n) = c;
    2677             :     }
    2678             :   }
    2679        5348 :   return mkvec2(V, Pn);
    2680             : }
    2681             : static GEN
    2682      399553 : vchip_lift(GEN VCHI, long x, GEN C)
    2683             : {
    2684      399553 :   GEN V = gel(VCHI,1);
    2685      399553 :   long F = lg(V)-1;
    2686      399553 :   if (F == 1) return C;
    2687       18368 :   x %= F;
    2688       18368 :   if (!x) return C;
    2689       18368 :   if (x <= 0) x += F;
    2690       18368 :   return gmul(C, gel(V, x));
    2691             : }
    2692             : static long
    2693   124662909 : vchip_FC(GEN VCHI) { return lg(gel(VCHI,1))-1; }
    2694             : static GEN
    2695     4591272 : vchip_mod(GEN VCHI, GEN S)
    2696     4591272 : { return (typ(S) == t_POL)? RgX_rem(S, gel(VCHI,2)): S; }
    2697             : static GEN
    2698     1466969 : vchip_polmod(GEN VCHI, GEN S)
    2699     1466969 : { return (typ(S) == t_POL)? mkpolmod(S, gel(VCHI,2)): S; }
    2700             : 
    2701             : /* ceil(m/d) */
    2702             : static long
    2703      137144 : ceildiv(long m, long d)
    2704             : {
    2705             :   long q;
    2706      137144 :   if (!m) return 0;
    2707       40544 :   q = m/d; return m%d? q+1: q;
    2708             : }
    2709             : 
    2710             : /* contribution of scalar matrices in dimension formula */
    2711             : static GEN
    2712      296478 : A1(long N, long k)
    2713      296478 : { return sstoQ(mypsiu(N)*(k-1), 12); }
    2714             : static long
    2715        7665 : ceilA1(long N, long k)
    2716        7665 : { return ceildiv(mypsiu(N) * (k-1), 12); }
    2717             : 
    2718             : /* sturm bound, slightly larger than dimension */
    2719             : long
    2720       28609 : mfsturmNk(long N, long k) { return (mypsiu(N) * k) / 12; }
    2721             : long
    2722        2422 : mfsturmNgk(long N, GEN k)
    2723             : {
    2724        2422 :   long n,d; Qtoss(k,&n,&d);
    2725        2422 :   return 1 + (mypsiu(N)*n)/(d == 1? 12: 24);
    2726             : }
    2727             : static long
    2728          42 : mfsturmmf(GEN F) { return mfsturmNgk(mf_get_N(F), mf_get_gk(F)); }
    2729             : 
    2730             : /* List of all solutions of x^2 + x + 1 = 0 modulo N, x modulo N */
    2731             : static GEN
    2732         539 : sqrtm3modN(long N)
    2733             : {
    2734             :   pari_sp av;
    2735             :   GEN fa, P, E, B, mB, A, Q, T, R, v, gen_m3;
    2736         539 :   long l, i, n, ct, fl3 = 0, Ninit;
    2737         539 :   if (!odd(N) || (N%9) == 0) return cgetg(1,t_VECSMALL);
    2738         511 :   Ninit = N;
    2739         511 :   if ((N%3) == 0) { N /= 3; fl3 = 1; }
    2740         511 :   fa = myfactoru(N); P = gel(fa, 1); E = gel(fa, 2);
    2741         511 :   l = lg(P);
    2742         707 :   for (i = 1; i < l; i++)
    2743         518 :     if ((P[i]%3) == 2) return cgetg(1,t_VECSMALL);
    2744         189 :   A = cgetg(l, t_VECSMALL);
    2745         189 :   B = cgetg(l, t_VECSMALL);
    2746         189 :   mB= cgetg(l, t_VECSMALL);
    2747         189 :   Q = cgetg(l, t_VECSMALL); gen_m3 = utoineg(3);
    2748         385 :   for (i = 1; i < l; i++)
    2749             :   {
    2750         196 :     long p = P[i], e = E[i];
    2751         196 :     Q[i] = upowuu(p,e);
    2752         196 :     B[i] = itou( Zp_sqrt(gen_m3, utoipos(p), e) );
    2753         196 :     mB[i]= Q[i] - B[i];
    2754             :   }
    2755         189 :   ct = 1 << (l-1);
    2756         189 :   T = ZV_producttree(Q);
    2757         189 :   R = ZV_chinesetree(Q,T);
    2758         189 :   v = cgetg(ct+1, t_VECSMALL);
    2759         189 :   av = avma;
    2760         581 :   for (n = 1; n <= ct; n++)
    2761             :   {
    2762         392 :     long m = n-1, r;
    2763         812 :     for (i = 1; i < l; i++)
    2764             :     {
    2765         420 :       A[i] = (m&1L)? mB[i]: B[i];
    2766         420 :       m >>= 1;
    2767             :     }
    2768         392 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2769         462 :     if (fl3) while (r%3) r += N;
    2770         392 :     set_avma(av); v[n] = odd(r) ? (r-1) >> 1 : (r+Ninit-1) >> 1;
    2771             :   }
    2772         189 :   return v;
    2773             : }
    2774             : 
    2775             : /* number of elliptic points of order 3 in X0(N) */
    2776             : static long
    2777        9681 : nu3(long N)
    2778             : {
    2779             :   long i, l;
    2780             :   GEN P;
    2781        9681 :   if (!odd(N) || (N%9) == 0) return 0;
    2782        8617 :   if ((N%3) == 0) N /= 3;
    2783        8617 :   P = gel(myfactoru(N), 1); l = lg(P);
    2784       12649 :   for (i = 1; i < l; i++) if ((P[i]%3) == 2) return 0;
    2785        3864 :   return 1L<<(l-1);
    2786             : }
    2787             : /* number of elliptic points of order 2 in X0(N) */
    2788             : static long
    2789       16772 : nu2(long N)
    2790             : {
    2791             :   long i, l;
    2792             :   GEN P;
    2793       16772 :   if ((N&3L) == 0) return 0;
    2794       16772 :   if (!odd(N)) N >>= 1;
    2795       16772 :   P = gel(myfactoru(N), 1); l = lg(P);
    2796       21035 :   for (i = 1; i < l; i++) if ((P[i]&3L) == 3) return 0;
    2797        3822 :   return 1L<<(l-1);
    2798             : }
    2799             : 
    2800             : /* contribution of elliptic matrices of order 3 in dimension formula
    2801             :  * Only depends on CHIP the primitive char attached to CHI */
    2802             : static GEN
    2803       41930 : A21(long N, long k, GEN CHI)
    2804             : {
    2805             :   GEN res, G, chi, o;
    2806             :   long a21, i, limx, S;
    2807       41930 :   if ((N&1L) == 0) return gen_0;
    2808       20160 :   a21 = k%3 - 1;
    2809       20160 :   if (!a21) return gen_0;
    2810       19516 :   if (N <= 3) return sstoQ(a21, 3);
    2811       10220 :   if (!CHI) return sstoQ(nu3(N) * a21, 3);
    2812         539 :   res = sqrtm3modN(N); limx = (N - 1) >> 1;
    2813         539 :   G = gel(CHI,1); chi = gel(CHI,2);
    2814         539 :   o = gmfcharorder(CHI);
    2815         931 :   for (S = 0, i = 1; i < lg(res); i++)
    2816             :   { /* (x,N) = 1; S += chi(x) + chi(x^2) */
    2817         392 :     long x = res[i];
    2818         392 :     if (x <= limx)
    2819             :     { /* CHI(x)=e(c/o), 3rd-root of 1 */
    2820         196 :       GEN c = znchareval(G, chi, utoi(x), o);
    2821         196 :       if (!signe(c)) S += 2; else S--;
    2822             :     }
    2823             :   }
    2824         539 :   return sstoQ(a21 * S, 3);
    2825             : }
    2826             : 
    2827             : /* List of all square roots of -1 modulo N */
    2828             : static GEN
    2829         595 : sqrtm1modN(long N)
    2830             : {
    2831             :   pari_sp av;
    2832             :   GEN fa, P, E, B, mB, A, Q, T, R, v;
    2833         595 :   long l, i, n, ct, fleven = 0;
    2834         595 :   if ((N&3L) == 0) return cgetg(1,t_VECSMALL);
    2835         595 :   if ((N&1L) == 0) { N >>= 1; fleven = 1; }
    2836         595 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    2837         595 :   l = lg(P);
    2838         945 :   for (i = 1; i < l; i++)
    2839         665 :     if ((P[i]&3L) == 3) return cgetg(1,t_VECSMALL);
    2840         280 :   A = cgetg(l, t_VECSMALL);
    2841         280 :   B = cgetg(l, t_VECSMALL);
    2842         280 :   mB= cgetg(l, t_VECSMALL);
    2843         280 :   Q = cgetg(l, t_VECSMALL);
    2844         574 :   for (i = 1; i < l; i++)
    2845             :   {
    2846         294 :     long p = P[i], e = E[i];
    2847         294 :     Q[i] = upowuu(p,e);
    2848         294 :     B[i] = itou( Zp_sqrt(gen_m1, utoipos(p), e) );
    2849         294 :     mB[i]= Q[i] - B[i];
    2850             :   }
    2851         280 :   ct = 1 << (l-1);
    2852         280 :   T = ZV_producttree(Q);
    2853         280 :   R = ZV_chinesetree(Q,T);
    2854         280 :   v = cgetg(ct+1, t_VECSMALL);
    2855         280 :   av = avma;
    2856         868 :   for (n = 1; n <= ct; n++)
    2857             :   {
    2858         588 :     long m = n-1, r;
    2859        1232 :     for (i = 1; i < l; i++)
    2860             :     {
    2861         644 :       A[i] = (m&1L)? mB[i]: B[i];
    2862         644 :       m >>= 1;
    2863             :     }
    2864         588 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2865         588 :     if (fleven && !odd(r)) r += N;
    2866         588 :     set_avma(av); v[n] = r;
    2867             :   }
    2868         280 :   return v;
    2869             : }
    2870             : 
    2871             : /* contribution of elliptic matrices of order 4 in dimension formula.
    2872             :  * Only depends on CHIP the primitive char attached to CHI */
    2873             : static GEN
    2874       41930 : A22(long N, long k, GEN CHI)
    2875             : {
    2876             :   GEN G, chi, o, res;
    2877             :   long S, a22, i, limx, o2;
    2878       41930 :   if ((N&3L) == 0) return gen_0;
    2879       28875 :   a22 = (k & 3L) - 1; /* (k % 4) - 1 */
    2880       28875 :   if (!a22) return gen_0;
    2881       28875 :   if (N <= 2) return sstoQ(a22, 4);
    2882       17577 :   if (!CHI) return sstoQ(nu2(N)*a22, 4);
    2883         805 :   if (mfcharparity(CHI) == -1) return gen_0;
    2884         595 :   res = sqrtm1modN(N); limx = (N - 1) >> 1;
    2885         595 :   G = gel(CHI,1); chi = gel(CHI,2);
    2886         595 :   o = gmfcharorder(CHI);
    2887         595 :   o2 = itou(o)>>1;
    2888        1183 :   for (S = 0, i = 1; i < lg(res); i++)
    2889             :   { /* (x,N) = 1, S += real(chi(x)) */
    2890         588 :     long x = res[i];
    2891         588 :     if (x <= limx)
    2892             :     { /* CHI(x)=e(c/o), 4th-root of 1 */
    2893         294 :       long c = itou( znchareval(G, chi, utoi(x), o) );
    2894         294 :       if (!c) S++; else if (c == o2) S--;
    2895             :     }
    2896             :   }
    2897         595 :   return sstoQ(a22 * S, 2);
    2898             : }
    2899             : 
    2900             : /* sumdiv(N,d,eulerphi(gcd(d,N/d))) */
    2901             : static long
    2902       37289 : nuinf(long N)
    2903             : {
    2904       37289 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    2905       37289 :   long i, t = 1, l = lg(P);
    2906       79289 :   for (i=1; i<l; i++)
    2907             :   {
    2908       42000 :     long p = P[i], e = E[i];
    2909       42000 :     if (odd(e))
    2910       33635 :       t *= upowuu(p,e>>1) << 1;
    2911             :     else
    2912        8365 :       t *= upowuu(p,(e>>1)-1) * (p+1);
    2913             :   }
    2914       37289 :   return t;
    2915             : }
    2916             : 
    2917             : /* contribution of hyperbolic matrices in dimension formula */
    2918             : static GEN
    2919       42378 : A3(long N, long FC)
    2920             : {
    2921             :   long i, S, NF, l;
    2922             :   GEN D;
    2923       42378 :   if (FC == 1) return sstoQ(nuinf(N),2);
    2924        5089 :   D = mydivisorsu(N); l = lg(D);
    2925        5089 :   S = 0; NF = N/FC;
    2926       40544 :   for (i = 1; i < l; i++)
    2927             :   {
    2928       35455 :     long g = ugcd(D[i], D[l-i]);
    2929       35455 :     if (NF%g == 0) S += myeulerphiu(g);
    2930             :   }
    2931        5089 :   return sstoQ(S, 2);
    2932             : }
    2933             : 
    2934             : /* special contribution in weight 2 in dimension formula */
    2935             : static long
    2936       41538 : A4(long k, long FC)
    2937       41538 : { return (k==2 && FC==1)? 1: 0; }
    2938             : /* gcd(x,N) */
    2939             : static long
    2940   143803807 : myugcd(GEN GCD, ulong x)
    2941             : {
    2942   143803807 :   ulong N = lg(GCD)-1;
    2943   143803807 :   if (x >= N) x %= N;
    2944   143803807 :   return GCD[x+1];
    2945             : }
    2946             : /* 1_{gcd(x,N) = 1} * chi(x), return NULL if 0 */
    2947             : static GEN
    2948   188268374 : mychicgcd(GEN GCD, GEN VCHI, long x)
    2949             : {
    2950   188268374 :   long N = lg(GCD)-1;
    2951   188268374 :   if (N == 1) return gen_1;
    2952   152743801 :   x = umodsu(x, N);
    2953   152743801 :   if (GCD[x+1] != 1) return NULL;
    2954   119280609 :   x %= vchip_FC(VCHI); if (!x) return gen_1;
    2955     4468065 :   return gel(gel(VCHI,1), x);
    2956             : }
    2957             : 
    2958             : /* contribution of scalar matrices to trace formula */
    2959             : static GEN
    2960     4530001 : TA1(long N, long k, GEN VCHI, GEN GCD, long n)
    2961             : {
    2962             :   GEN S;
    2963             :   ulong m;
    2964     4530001 :   if (!uissquareall(n, &m)) return gen_0;
    2965      323974 :   if (m == 1) return A1(N,k); /* common */
    2966      288330 :   S = mychicgcd(GCD, VCHI, m);
    2967      288330 :   return S? gmul(gmul(powuu(m, k-2), A1(N,k)), S): gen_0;
    2968             : }
    2969             : 
    2970             : /* All square roots modulo 4N, x modulo 2N, precomputed to accelerate TA2 */
    2971             : static GEN
    2972      116298 : mksqr(long N)
    2973             : {
    2974      116298 :   pari_sp av = avma;
    2975      116298 :   long x, N2 = N << 1, N4 = N << 2;
    2976      116298 :   GEN v = const_vec(N2, cgetg(1, t_VECSMALL));
    2977      116298 :   gel(v, N2) = mkvecsmall(0); /* x = 0 */
    2978     2967825 :   for (x = 1; x <= N; x++)
    2979             :   {
    2980     2851527 :     long r = (((x*x - 1)%N4) >> 1) + 1;
    2981     2851527 :     gel(v,r) = vecsmall_append(gel(v,r), x);
    2982             :   }
    2983      116298 :   return gerepilecopy(av, v);
    2984             : }
    2985             : 
    2986             : static GEN
    2987      116298 : mkgcd(long N)
    2988             : {
    2989             :   GEN GCD, d;
    2990             :   long i, N2;
    2991      116298 :   if (N == 1) return mkvecsmall(N);
    2992       95963 :   GCD = cgetg(N + 1, t_VECSMALL);
    2993       95963 :   d = GCD+1; /* GCD[i+1] = d[i] = gcd(i,N) = gcd(N-i,N), i = 0..N-1 */
    2994       95963 :   d[0] = N; d[1] = d[N-1] = 1; N2 = N>>1;
    2995     1396892 :   for (i = 2; i <= N2; i++) d[i] = d[N-i] = ugcd(N, i);
    2996       95963 :   return GCD;
    2997             : }
    2998             : 
    2999             : /* Table of \sum_{x^2-tx+n=0 mod Ng}chi(x) for all g dividing gcd(N,F),
    3000             :  * F^2 largest such that (t^2-4n)/F^2=0 or 1 mod 4; t >= 0 */
    3001             : static GEN
    3002    10796667 : mutglistall(long t, long N, long NF, GEN VCHI, long n, GEN MUP, GEN li, GEN GCD)
    3003             : {
    3004    10796667 :   long i, lx = lg(li);
    3005    10796667 :   GEN DNF = mydivisorsu(NF), v = zerovec(NF);
    3006    10796667 :   long j, g, lDNF = lg(DNF);
    3007    28641109 :   for (i = 1; i < lx; i++)
    3008             :   {
    3009    17844442 :     long x = (li[i] + t) >> 1, y, lD;
    3010    17844442 :     GEN D, c = mychicgcd(GCD, VCHI, x);
    3011    17844442 :     if (li[i] && li[i] != N)
    3012             :     {
    3013    11312252 :       GEN c2 = mychicgcd(GCD, VCHI, t - x);
    3014    11312252 :       if (c2) c = c? gadd(c, c2): c2;
    3015             :     }
    3016    17844442 :     if (!c) continue;
    3017    12705301 :     y = (x*(x - t) + n) / N; /* exact division */
    3018    12705301 :     D = mydivisorsu(ugcd(labs(y), NF)); lD = lg(D);
    3019    35056847 :     for (j=1; j < lD; j++) { g = D[j]; gel(v,g) = gadd(gel(v,g), c); }
    3020             :   }
    3021             :   /* j = 1 corresponds to g = 1, and MUP[1] = 1 */
    3022    25683511 :   for (j=2; j < lDNF; j++) { g = DNF[j]; gel(v,g) = gmulsg(MUP[g], gel(v,g)); }
    3023    10796667 :   return v;
    3024             : }
    3025             : 
    3026             : /* special case (N,F) = 1: easier */
    3027             : static GEN
    3028    69550670 : mutg1(long t, long N, GEN VCHI, GEN li, GEN GCD)
    3029             : { /* (N,F) = 1 */
    3030    69550670 :   GEN S = NULL;
    3031    69550670 :   long i, lx = lg(li);
    3032   144439204 :   for (i = 1; i < lx; i++)
    3033             :   {
    3034    74888534 :     long x = (li[i] + t) >> 1;
    3035    74888534 :     GEN c = mychicgcd(GCD, VCHI, x);
    3036    74888534 :     if (c) S = S? gadd(S, c): c;
    3037    74888534 :     if (li[i] && li[i] != N)
    3038             :     {
    3039    38252627 :       c = mychicgcd(GCD, VCHI, t - x);
    3040    38252627 :       if (c) S = S? gadd(S, c): c;
    3041             :     }
    3042    74888534 :     if (S && !signe(S)) S = NULL; /* strive hard to add gen_0 */
    3043             :   }
    3044    69550670 :   return S; /* single value */
    3045             : }
    3046             : 
    3047             : /* Gegenbauer pol; n > 2, P = \sum_{0<=j<=n/2} (-1)^j (n-j)!/j!(n-2*j)! X^j */
    3048             : static GEN
    3049      350203 : mfrhopol(long n)
    3050             : {
    3051             : #ifdef LONG_IS_64BIT
    3052      300174 :   const long M = 2642249;
    3053             : #else
    3054       50029 :   const long M = 1629;
    3055             : #endif
    3056      350203 :   long j, d = n >> 1; /* >= 1 */
    3057      350203 :   GEN P = cgetg(d + 3, t_POL);
    3058             : 
    3059      350203 :   if (n > M) pari_err_IMPL("mfrhopol for large weight"); /* avoid overflow */
    3060      350203 :   P[1] = evalvarn(0)|evalsigne(1);
    3061      350203 :   gel(P,2) = gen_1;
    3062      350203 :   gel(P,3) = utoineg(n-1); /* j = 1 */
    3063      350203 :   if (d > 1) gel(P,4) = utoipos(((n-3)*(n-2)) >> 1); /* j = 2 */
    3064      350203 :   if (d > 2) gel(P,5) = utoineg(((n-5)*(n-4)*(n-3)) / 6); /* j = 3 */
    3065     1288707 :   for (j = 4; j <= d; j++)
    3066      938504 :     gel(P,j+2) = divis(mulis(gel(P,j+1), (n-2*j+1)*(n-2*j+2)), (n-j+1)*(-j));
    3067      350203 :   return P;
    3068             : }
    3069             : 
    3070             : /* polrecip(Q)(t2), assume Q(0) = 1 */
    3071             : static GEN
    3072     2984520 : ZXrecip_u_eval(GEN Q, ulong t2)
    3073             : {
    3074     2984520 :   GEN T = addiu(gel(Q,3), t2);
    3075     2984520 :   long l = lg(Q), j;
    3076    32182038 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mului(t2, T));
    3077     2984520 :   return T;
    3078             : }
    3079             : /* return sh * sqrt(n)^nu * G_nu(t/(2*sqrt(n))) for t != 0
    3080             :  * else (sh/2) * sqrt(n)^nu * G_nu(0) [ implies nu is even ]
    3081             :  * G_nu(z) = \sum_{0<=j<=nu/2} (-1)^j (nu-j)!/j!(nu-2*j)! * (2z)^(nu-2*j)) */
    3082             : static GEN
    3083    73769087 : mfrhopowsimp(GEN Q, GEN sh, long nu, long t, long t2, long n)
    3084             : {
    3085             :   GEN T;
    3086    73769087 :   switch (nu)
    3087             :   {
    3088    68229371 :     case 0: return t? sh: gmul2n(sh,-1);
    3089     1125075 :     case 1: return gmulsg(t, sh);
    3090     1391999 :     case 2: return t? gmulsg(t2 - n, sh): gmul(gmul2n(stoi(-n), -1), sh);
    3091         427 :     case 3: return gmul(mulss(t, t2 - 2*n), sh);
    3092     3022215 :     default:
    3093     3022215 :       if (!t) return gmul(gmul2n(gel(Q, lg(Q) - 1), -1), sh);
    3094     2984520 :       T = ZXrecip_u_eval(Q, t2); if (odd(nu)) T = mulsi(t, T);
    3095     2984520 :       return gmul(T, sh);
    3096             :   }
    3097             : }
    3098             : 
    3099             : /* contribution of elliptic matrices to trace formula */
    3100             : static GEN
    3101     4530001 : TA2(long N, long k, GEN VCHI, long n, GEN SQRTS, GEN MUP, GEN GCD)
    3102             : {
    3103     4530001 :   const long n4 = n << 2, N4 = N << 2, nu = k - 2;
    3104     4530001 :   const long st = (!odd(N) && odd(n)) ? 2 : 1;
    3105             :   long limt, t;
    3106             :   GEN S, Q;
    3107             : 
    3108     4530001 :   limt = usqrt(n4);
    3109     4530001 :   if (limt*limt == n4) limt--;
    3110     4530001 :   Q = nu > 3 ? ZX_z_unscale(mfrhopol(nu), n) : NULL;
    3111     4530001 :   S = gen_0;
    3112   139003242 :   for (t = odd(k)? st: 0; t <= limt; t += st) /* t^2 < 4n */
    3113             :   {
    3114   134473241 :     pari_sp av = avma;
    3115   134473241 :     long t2 = t*t, D = n4 - t2, F, D0, NF;
    3116             :     GEN sh, li;
    3117             : 
    3118   134473241 :     li = gel(SQRTS, (umodsu(-D - 1, N4) >> 1) + 1);
    3119   141051491 :     if (lg(li) == 1) continue;
    3120    80347337 :     D0 = mycoredisc2neg(D, &F);
    3121    80347337 :     NF = myugcd(GCD, F);
    3122    80347337 :     if (NF == 1)
    3123             :     { /* (N,F) = 1 => single value in mutglistall */
    3124    69550670 :       GEN mut = mutg1(t, N, VCHI, li, GCD);
    3125    69550670 :       if (!mut) { set_avma(av); continue; }
    3126    66552479 :       sh = gmul(sstoQ(hclassno6u_i(D,D0,F),6), mut);
    3127             :     }
    3128             :     else
    3129             :     {
    3130    10796667 :       GEN v = mutglistall(t, N, NF, VCHI, n, MUP, li, GCD);
    3131    10796667 :       GEN DF = mydivisorsu(F);
    3132    10796667 :       long i, lDF = lg(DF);
    3133    10796667 :       sh = gen_0;
    3134    41886432 :       for (i = 1; i < lDF; i++)
    3135             :       {
    3136    31089765 :         long Ff, f = DF[i], g = myugcd(GCD, f);
    3137    31089765 :         GEN mut = gel(v, g);
    3138    31089765 :         if (gequal0(mut)) continue;
    3139    17881304 :         Ff = DF[lDF-i]; /* F/f */
    3140    17881304 :         if (Ff == 1) sh = gadd(sh, mut);
    3141             :         else
    3142             :         {
    3143    12710789 :           GEN P = gel(myfactoru(Ff), 1);
    3144    12710789 :           long j, lP = lg(P);
    3145    27194881 :           for (j = 1; j < lP; j++) { long p = P[j]; Ff -= kross(D0, p)*Ff/p; }
    3146    12710789 :           sh = gadd(sh, gmulsg(Ff, mut));
    3147             :         }
    3148             :       }
    3149    10796667 :       if (gequal0(sh)) { set_avma(av); continue; }
    3150     7216608 :       if (D0 == -3) sh = gdivgs(sh, 3);
    3151     6848688 :       else if (D0 == -4) sh = gdivgs(sh, 2);
    3152     6507354 :       else sh = gmulgs(sh, myh(D0));
    3153             :     }
    3154    73769087 :     S = gerepileupto(av, gadd(S, mfrhopowsimp(Q,sh,nu,t,t2,n)));
    3155             :   }
    3156     4530001 :   return S;
    3157             : }
    3158             : 
    3159             : /* compute global auxiliary data for TA3 */
    3160             : static GEN
    3161      116298 : mkbez(long N, long FC)
    3162             : {
    3163      116298 :   long ct, i, NF = N/FC;
    3164      116298 :   GEN w, D = mydivisorsu(N);
    3165      116298 :   long l = lg(D);
    3166             : 
    3167      116298 :   w = cgetg(l, t_VEC);
    3168      338541 :   for (i = ct = 1; i < l; i++)
    3169             :   {
    3170      318206 :     long u, v, h, c = D[i], Nc = D[l-i];
    3171      318206 :     if (c > Nc) break;
    3172      222243 :     h = cbezout(c, Nc, &u, &v);
    3173      222243 :     if (h == 1) /* shortcut */
    3174      160272 :       gel(w, ct++) = mkvecsmall4(1,u*c,1,i);
    3175       61971 :     else if (!(NF%h))
    3176       52213 :       gel(w, ct++) = mkvecsmall4(h,u*(c/h),myeulerphiu(h),i);
    3177             :   }
    3178      116298 :   setlg(w,ct); stackdummy((pari_sp)(w+ct),(pari_sp)(w+l));
    3179      116298 :   return w;
    3180             : }
    3181             : 
    3182             : /* contribution of hyperbolic matrices to trace formula, d * nd = n,
    3183             :  * DN = divisorsu(N) */
    3184             : static GEN
    3185    18914371 : auxsum(GEN VCHI, GEN GCD, long d, long nd, GEN DN, GEN BEZ)
    3186             : {
    3187    18914371 :   GEN S = gen_0;
    3188    18914371 :   long ct, g = nd - d, lDN = lg(DN), lBEZ = lg(BEZ);
    3189    49650398 :   for (ct = 1; ct < lBEZ; ct++)
    3190             :   {
    3191    30736027 :     GEN y, B = gel(BEZ, ct);
    3192    30736027 :     long ic, c, Nc, uch, h = B[1];
    3193    30736027 :     if (g%h) continue;
    3194    30069774 :     uch = B[2];
    3195    30069774 :     ic  = B[4];
    3196    30069774 :     c = DN[ic];
    3197    30069774 :     Nc= DN[lDN - ic]; /* Nc = N/c */
    3198    30069774 :     if (ugcd(Nc, nd) == 1)
    3199    24209598 :       y = mychicgcd(GCD, VCHI, d + uch*g); /* 0 if (c,d) > 1 */
    3200             :     else
    3201     5860176 :       y = NULL;
    3202    30069774 :     if (c != Nc && ugcd(Nc, d) == 1)
    3203             :     {
    3204    21472591 :       GEN y2 = mychicgcd(GCD, VCHI, nd - uch*g); /* 0 if (c,nd) > 1 */
    3205    21472591 :       if (y2) y = y? gadd(y, y2): y2;
    3206             :     }
    3207    30069774 :     if (y) S = gadd(S, gmulsg(B[3], y));
    3208             :   }
    3209    18914371 :   return S;
    3210             : }
    3211             : 
    3212             : static GEN
    3213     4530001 : TA3(long N, long k, GEN VCHI, GEN GCD, GEN Dn, GEN BEZ)
    3214             : {
    3215     4530001 :   GEN S = gen_0, DN = mydivisorsu(N);
    3216     4530001 :   long i, l = lg(Dn);
    3217    23444372 :   for (i = 1; i < l; i++)
    3218             :   {
    3219    23408728 :     long d = Dn[i], nd = Dn[l-i]; /* = n/d */
    3220             :     GEN t, u;
    3221    23408728 :     if (d > nd) break;
    3222    18914371 :     t = auxsum(VCHI, GCD, d, nd, DN, BEZ);
    3223    18914371 :     if (isintzero(t)) continue;
    3224    17809519 :     u = powuu(d,k-1); if (d == nd) u = gmul2n(u,-1);
    3225    17809519 :     S = gadd(S, gmul(u,t));
    3226             :   }
    3227     4530001 :   return S;
    3228             : }
    3229             : 
    3230             : /* special contribution in weight 2 in trace formula */
    3231             : static long
    3232     4530001 : TA4(long k, GEN VCHIP, GEN Dn, GEN GCD)
    3233             : {
    3234             :   long i, l, S;
    3235     4530001 :   if (k != 2 || vchip_FC(VCHIP) != 1) return 0;
    3236     3862187 :   l = lg(Dn); S = 0;
    3237    36228892 :   for (i = 1; i < l; i++)
    3238             :   {
    3239    32366705 :     long d = Dn[i]; /* gcd(N,n/d) == 1? */
    3240    32366705 :     if (myugcd(GCD, Dn[l-i]) == 1) S += d;
    3241             :   }
    3242     3862187 :   return S;
    3243             : }
    3244             : 
    3245             : /* precomputation of products occurring im mutg, again to accelerate TA2 */
    3246             : static GEN
    3247      116298 : mkmup(long N)
    3248             : {
    3249      116298 :   GEN fa = myfactoru(N), P = gel(fa,1), D = divisorsu_fact(fa);
    3250      116298 :   long i, lP = lg(P), lD = lg(D);
    3251      116298 :   GEN MUP = zero_zv(N);
    3252      116298 :   MUP[1] = 1;
    3253      410620 :   for (i = 2; i < lD; i++)
    3254             :   {
    3255      294322 :     long j, g = D[i], Ng = D[lD-i]; /*  N/g */
    3256      809319 :     for (j = 1; j < lP; j++) { long p = P[j]; if (Ng%p) g += g/p; }
    3257      294322 :     MUP[D[i]] = g;
    3258             :   }
    3259      116298 :   return MUP;
    3260             : }
    3261             : 
    3262             : /* quadratic non-residues mod p; p odd prime, p^2 fits in a long */
    3263             : static GEN
    3264        2653 : non_residues(long p)
    3265             : {
    3266        2653 :   long i, j, p2 = p >> 1;
    3267        2653 :   GEN v = cgetg(p2+1, t_VECSMALL), w = const_vecsmall(p-1, 1);
    3268        4410 :   for (i = 2; i <= p2; i++) w[(i*i) % p] = 0; /* no need to check 1 */
    3269        8820 :   for (i = 2, j = 1; i < p; i++) if (w[i]) v[j++] = i;
    3270        2653 :   return v;
    3271             : }
    3272             : 
    3273             : /* CHIP primitive. Return t_VECSMALL v of length q such that
    3274             :  * Tr^new_{N,CHIP}(n) = 0 whenever v[(n%q) + 1] is non-zero */
    3275             : static GEN
    3276       30030 : mfnewzerodata(long N, GEN CHIP)
    3277             : {
    3278       30030 :   GEN V, M, L, faN = myfactoru(N), PN = gel(faN,1), EN = gel(faN,2);
    3279       30030 :   GEN G = gel(CHIP,1), chi = gel(CHIP,2);
    3280       30030 :   GEN fa = znstar_get_faN(G), P = ZV_to_zv(gel(fa,1)), E = gel(fa,2);
    3281       30030 :   long i, mod, j = 1, l = lg(PN);
    3282             : 
    3283       30030 :   M = cgetg(l, t_VECSMALL); M[1] = 0;
    3284       30030 :   V = cgetg(l, t_VEC);
    3285             :   /* Tr^new(n) = 0 if (n mod M[i]) in V[i]  */
    3286       30030 :   if ((N & 3) == 0)
    3287             :   {
    3288       12054 :     long e = EN[1];
    3289       12054 :     long c = (lg(P) > 1 && P[1] == 2)? E[1]: 0; /* c = v_2(FC) */
    3290             :     /* e >= 2 */
    3291       12054 :     if (c == e-1) return NULL; /* Tr^new = 0 */
    3292       11949 :     if (c == e)
    3293             :     {
    3294        3584 :       if (e == 2)
    3295             :       { /* sc: -4 */
    3296        1764 :         gel(V,1) = mkvecsmall(3);
    3297        1764 :         M[1] = 4;
    3298             :       }
    3299        1820 :       else if (e == 3)
    3300             :       { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3301        1820 :         long t = signe(gel(chi,1))? 7: 3;
    3302        1820 :         gel(V,1) = mkvecsmall2(5, t);
    3303        1820 :         M[1] = 8;
    3304             :       }
    3305             :     }
    3306        8365 :     else if (e == 5 && c == 3)
    3307         154 :     { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3308         154 :       long t = signe(gel(chi,1))? 7: 3;
    3309         154 :       gel(V,1) = mkvecsmalln(6, 2L,4L,5L,6L,8L,t);
    3310         154 :       M[1] = 8;
    3311             :     }
    3312        8211 :     else if ((e == 4 && c == 2) || (e == 5 && c <= 2) || (e == 6 && c <= 2)
    3313        6678 :          || (e >= 7 && c == e - 3))
    3314             :     { /* sc: 4 */
    3315        1533 :       gel(V,1) = mkvecsmall3(0,2,3);
    3316        1533 :       M[1] = 4;
    3317             :     }
    3318        6678 :     else if ((e <= 4 && c == 0) || (e >= 5 && c == e - 2))
    3319             :     { /* sc: 2 */
    3320        6412 :       gel(V,1) = mkvecsmall(0);
    3321        6412 :       M[1] = 2;
    3322             :     }
    3323         266 :     else if ((e == 6 && c == 3) || (e >= 7 && c <= e - 4))
    3324             :     { /* sc: -2 */
    3325         266 :       gel(V,1) = mkvecsmalln(7, 0L,2L,3L,4L,5L,6L,7L);
    3326         266 :       M[1] = 8;
    3327             :     }
    3328             :   }
    3329       29925 :   j = M[1]? 2: 1;
    3330       64169 :   for (i = odd(N)? 1: 2; i < l; i++) /* skip p=2, done above */
    3331             :   {
    3332       34244 :     long p = PN[i], e = EN[i];
    3333       34244 :     long z = zv_search(P, p), c = z? E[z]: 0; /* c = v_p(FC) */
    3334       34244 :     if ((e <= 2 && c == 1 && itos(gel(chi,z)) == (p>>1)) /* ord(CHI_p)=2 */
    3335       32053 :         || (e >= 3 && c <= e - 2))
    3336        2653 :     { /* sc: -p */
    3337        2653 :       GEN v = non_residues(p);
    3338        2653 :       if (e != 1) v = vecsmall_prepend(v, 0);
    3339        2653 :       gel(V,j) = v;
    3340        2653 :       M[j] = p; j++;
    3341             :     }
    3342       31591 :     else if (e >= 2 && c < e)
    3343             :     { /* sc: p */
    3344        2142 :       gel(V,j) = mkvecsmall(0);
    3345        2142 :       M[j] = p; j++;
    3346             :     }
    3347             :   }
    3348       29925 :   if (j == 1) return cgetg(1, t_VECSMALL);
    3349       13986 :   setlg(V,j); setlg(M,j); mod = zv_prod(M);
    3350       13986 :   L = zero_zv(mod);
    3351       30730 :   for (i = 1; i < j; i++)
    3352             :   {
    3353       16744 :     GEN v = gel(V,i);
    3354       16744 :     long s, m = M[i], lv = lg(v);
    3355       43610 :     for (s = 1; s < lv; s++)
    3356             :     {
    3357       26866 :       long a = v[s] + 1;
    3358       53452 :       do { L[a] = 1; a += m; } while (a <= mod);
    3359             :     }
    3360             :   }
    3361       13986 :   return L;
    3362             : }
    3363             : /* v=mfnewzerodata(N,CHI); returns TRUE if newtrace(n) must be zero,
    3364             :  * (but newtrace(n) may still be zero if we return FALSE) */
    3365             : static long
    3366     1921031 : mfnewchkzero(GEN v, long n) { long q = lg(v)-1; return q && v[(n%q) + 1]; }
    3367             : 
    3368             : /* if (!VCHIP): from mftraceform_cusp;
    3369             :  * else from initnewtrace and CHI is known to be primitive */
    3370             : static GEN
    3371      116298 : inittrace(long N, GEN CHI, GEN VCHIP)
    3372             : {
    3373             :   long FC;
    3374      116298 :   if (VCHIP)
    3375      116291 :     FC = mfcharmodulus(CHI);
    3376             :   else
    3377           7 :     VCHIP = mfcharinit(mfchartoprimitive(CHI, &FC));
    3378      116298 :   return mkvecn(5, mksqr(N), mkmup(N), mkgcd(N), VCHIP, mkbez(N, FC));
    3379             : }
    3380             : 
    3381             : /* p > 2 prime; return a sorted t_VECSMALL of primes s.t Tr^new(p) = 0 for all
    3382             :  * weights > 2 */
    3383             : static GEN
    3384       29925 : inittrconj(long N, long FC)
    3385             : {
    3386             :   GEN fa, P, E, v;
    3387             :   long i, k, l;
    3388             : 
    3389       29925 :   if (FC != 1) return cgetg(1,t_VECSMALL);
    3390             : 
    3391       24577 :   fa = myfactoru(N >> vals(N));
    3392       24577 :   P = gel(fa,1); l = lg(P);
    3393       24577 :   E = gel(fa,2);
    3394       24577 :   v = cgetg(l, t_VECSMALL);
    3395       53879 :   for (i = k = 1; i < l; i++)
    3396             :   {
    3397       29302 :     long j, p = P[i]; /* > 2 */
    3398       71064 :     for (j = 1; j < l; j++)
    3399       41762 :       if (j != i && E[j] == 1 && kross(-p, P[j]) == 1) v[k++] = p;
    3400             :   }
    3401       24577 :   setlg(v,k); return v;
    3402             : }
    3403             : 
    3404             : /* assume CHIP primitive, f(CHIP) | N; NZ = mfnewzerodata(N,CHIP) */
    3405             : static GEN
    3406       29925 : initnewtrace_i(long N, GEN CHIP, GEN NZ)
    3407             : {
    3408       29925 :   GEN T = const_vec(N, cgetg(1,t_VEC)), D, VCHIP;
    3409       29925 :   long FC = mfcharmodulus(CHIP), N1, N2, i, l;
    3410             : 
    3411       29925 :   if (!NZ) NZ = mkvecsmall(1); /*Tr^new = 0; initialize data nevertheless*/
    3412       29925 :   VCHIP = mfcharinit(CHIP);
    3413       29925 :   N1 = N/FC; newd_params(N1, &N2);
    3414       29925 :   D = mydivisorsu(N1/N2); l = lg(D);
    3415       29925 :   N2 *= FC;
    3416      146216 :   for (i = 1; i < l; i++)
    3417             :   {
    3418      116291 :     long M = D[i]*N2;
    3419      116291 :     gel(T,M) = inittrace(M, CHIP, VCHIP);
    3420             :   }
    3421       29925 :   gel(T,N) = shallowconcat(gel(T,N), mkvec2(NZ, inittrconj(N,FC)));
    3422       29925 :   return T;
    3423             : }
    3424             : /* don't initialize if Tr^new = 0, return NULL */
    3425             : static GEN
    3426       30030 : initnewtrace(long N, GEN CHI)
    3427             : {
    3428       30030 :   GEN CHIP = mfchartoprimitive(CHI, NULL), NZ = mfnewzerodata(N,CHIP);
    3429       30030 :   return NZ? initnewtrace_i(N, CHIP, NZ): NULL;
    3430             : }
    3431             : 
    3432             : /* (-1)^k */
    3433             : static long
    3434        7448 : m1pk(long k) { return odd(k)? -1 : 1; }
    3435             : static long
    3436        7119 : badchar(long N, long k, GEN CHI)
    3437        7119 : { return mfcharparity(CHI) != m1pk(k) || (CHI && N % mfcharconductor(CHI)); }
    3438             : 
    3439             : /* dimension of space of cusp forms S_k(\G_0(N),CHI)
    3440             :  * Only depends on CHIP the primitive char attached to CHI */
    3441             : long
    3442       41545 : mfcuspdim(long N, long k, GEN CHI)
    3443             : {
    3444       41545 :   pari_sp av = avma;
    3445             :   long FC;
    3446             :   GEN s;
    3447       41545 :   if (k <= 0) return 0;
    3448       41545 :   if (k == 1) return mfwt1cuspdim(N, CHI);
    3449       41356 :   FC = CHI? mfcharconductor(CHI): 1;
    3450       41356 :   if (FC == 1) CHI = NULL;
    3451       41356 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3452       41356 :   s = gadd(s, gsubsg(A4(k, FC), A3(N, FC)));
    3453       41356 :   return gc_long(av, itos(s));
    3454             : }
    3455             : 
    3456             : /* dimension of whole space M_k(\G_0(N),CHI)
    3457             :  * Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3458             : long
    3459         791 : mffulldim(long N, long k, GEN CHI)
    3460             : {
    3461         791 :   pari_sp av = avma;
    3462         791 :   long FC = CHI? mfcharconductor(CHI): 1;
    3463             :   GEN s;
    3464         791 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3465         791 :   if (k == 1) return gc_long(av, itos(A3(N, FC)) + mfwt1cuspdim(N, CHI));
    3466         574 :   if (FC == 1) CHI = NULL;
    3467         574 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3468         574 :   s = gadd(s, A3(N, FC));
    3469         574 :   return gc_long(av, itos(s));
    3470             : }
    3471             : 
    3472             : /* Dimension of the space of Eisenstein series */
    3473             : long
    3474         231 : mfeisensteindim(long N, long k, GEN CHI)
    3475             : {
    3476         231 :   pari_sp av = avma;
    3477         231 :   long s, FC = CHI? mfcharconductor(CHI): 1;
    3478         231 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3479         231 :   s = itos(gmul2n(A3(N, FC), 1));
    3480         231 :   if (k > 1) s -= A4(k, FC); else s >>= 1;
    3481         231 :   return gc_long(av,s);
    3482             : }
    3483             : 
    3484             : enum { _SQRTS = 1, _MUP, _GCD, _VCHIP, _BEZ, _NEWLZ, _TRCONJ };
    3485             : /* Trace of T(n) on space of cuspforms; only depends on CHIP the primitive char
    3486             :  * attached to CHI */
    3487             : static GEN
    3488     4530001 : mfcusptrace_i(long N, long k, long n, GEN Dn, GEN S)
    3489             : {
    3490     4530001 :   pari_sp av = avma;
    3491             :   GEN a, b, VCHIP, GCD;
    3492             :   long t;
    3493     4530001 :   if (!n) return gen_0;
    3494     4530001 :   VCHIP = gel(S,_VCHIP);
    3495     4530001 :   GCD = gel(S,_GCD);
    3496     4530001 :   t = TA4(k, VCHIP, Dn, GCD);
    3497     4530001 :   a = TA1(N, k, VCHIP, GCD, n); if (t) a = gaddgs(a,t);
    3498     4530001 :   b = TA2(N, k, VCHIP, n, gel(S,_SQRTS), gel(S,_MUP), GCD);
    3499     4530001 :   b = gadd(b, TA3(N, k, VCHIP, GCD, Dn, gel(S,_BEZ)));
    3500     4530001 :   b = gsub(a,b);
    3501     4530001 :   if (typ(b) != t_POL) return gerepileupto(av, b);
    3502       38675 :   return gerepilecopy(av, vchip_polmod(VCHIP, b));
    3503             : }
    3504             : 
    3505             : static GEN
    3506     5794061 : mfcusptracecache(long N, long k, long n, GEN Dn, GEN S, cachenew_t *cache)
    3507             : {
    3508     5794061 :   GEN C = NULL, T = gel(cache->vfull,N);
    3509     5794061 :   long lcache = lg(T);
    3510     5794061 :   if (n < lcache) C = gel(T, n);
    3511     5794061 :   if (C) cache->cuspHIT++; else C = mfcusptrace_i(N, k, n, Dn, S);
    3512     5794061 :   cache->cuspTOTAL++;
    3513     5794061 :   if (n < lcache) gel(T,n) = C;
    3514     5794061 :   return C;
    3515             : }
    3516             : 
    3517             : /* return the divisors of n, known to be among the elements of D */
    3518             : static GEN
    3519      339255 : div_restrict(GEN D, ulong n)
    3520             : {
    3521             :   long i, j, l;
    3522      339255 :   GEN v, VDIV = caches[cache_DIV].cache;
    3523      339255 :   if (lg(VDIV) > n) return gel(VDIV,n);
    3524           0 :   l = lg(D);
    3525           0 :   v = cgetg(l, t_VECSMALL);
    3526           0 :   for (i = j = 1; i < l; i++)
    3527             :   {
    3528           0 :     ulong d = D[i];
    3529           0 :     if (n % d == 0) v[j++] = d;
    3530             :   }
    3531           0 :   setlg(v,j); return v;
    3532             : }
    3533             : 
    3534             : /* for some prime divisors of N, Tr^new(p) = 0 */
    3535             : static int
    3536      193207 : trconj(GEN T, long N, long n)
    3537      193207 : { return (lg(T) > 1 && N % n == 0 && zv_search(T, n)); }
    3538             : 
    3539             : /* n > 0; trace formula on new space */
    3540             : static GEN
    3541     1921031 : mfnewtrace_i(long N, long k, long n, cachenew_t *cache)
    3542             : {
    3543     1921031 :   GEN VCHIP, s, Dn, DN1, SN, S = cache->DATA;
    3544             :   long FC, N1, N2, N1N2, g, i, j, lDN1;
    3545             : 
    3546     1921031 :   if (!S) return gen_0;
    3547     1921031 :   SN = gel(S,N);
    3548     1921031 :   if (mfnewchkzero(gel(SN,_NEWLZ), n)) return gen_0;
    3549     1428322 :   if (k > 2 && trconj(gel(SN,_TRCONJ), N, n)) return gen_0;
    3550     1428294 :   VCHIP = gel(SN, _VCHIP); FC = vchip_FC(VCHIP);
    3551     1428294 :   N1 = N/FC; newt_params(N1, n, FC, &g, &N2);
    3552     1428294 :   N1N2 = N1/N2;
    3553     1428294 :   DN1 = mydivisorsu(N1N2); lDN1 = lg(DN1);
    3554     1428294 :   N2 *= FC;
    3555     1428294 :   Dn = mydivisorsu(n); /* this one is probably out of cache */
    3556     1428294 :   s = gmulsg(mubeta2(N1N2,n), mfcusptracecache(N2, k, n, Dn, gel(S,N2), cache));
    3557     5454806 :   for (i = 2; i < lDN1; i++)
    3558             :   { /* skip M1 = 1, done above */
    3559     4026512 :     long M1 = DN1[i], N1M1 = DN1[lDN1-i];
    3560     4026512 :     GEN Dg = mydivisorsu(ugcd(M1, g));
    3561     4026512 :     M1 *= N2;
    3562     4026512 :     s = gadd(s, gmulsg(mubeta2(N1M1,n),
    3563     4026512 :                        mfcusptracecache(M1, k, n, Dn, gel(S,M1), cache)));
    3564     4365767 :     for (j = 2; j < lg(Dg); j++) /* skip d = 1, done above */
    3565             :     {
    3566      339255 :       long d = Dg[j], ndd = n/(d*d), M = M1/d;
    3567      339255 :       GEN z = mulsi(mubeta2(N1M1,ndd), powuu(d,k-1)), C = vchip_lift(VCHIP,d,z);
    3568      339255 :       GEN Dndd = div_restrict(Dn, ndd);
    3569      339255 :       s = gadd(s, gmul(C, mfcusptracecache(M, k, ndd, Dndd, gel(S,M), cache)));
    3570             :     }
    3571     4026512 :     s = vchip_mod(VCHIP, s);
    3572             :   }
    3573     1428294 :   return vchip_polmod(VCHIP, s);
    3574             : }
    3575             : 
    3576             : /* mfcuspdim(N,k,CHI) - mfnewdim(N,k,CHI); CHIP primitive (for efficiency) */
    3577             : static long
    3578        7938 : mfolddim_i(long N, long k, GEN CHIP)
    3579             : {
    3580        7938 :   long S, i, l, FC = mfcharmodulus(CHIP), N1 = N/FC, N2;
    3581             :   GEN D;
    3582        7938 :   newd_params(N1, &N2); /* will ensure mubeta != 0 */
    3583        7938 :   D = mydivisorsu(N1/N2); l = lg(D);
    3584        7938 :   N2 *= FC; S = 0;
    3585       31024 :   for (i = 2; i < l; i++)
    3586             :   {
    3587       23086 :     long M = D[l-i]*N2, d = mfcuspdim(M, k, CHIP);
    3588       23086 :     if (d) S -= mubeta(D[i]) * d;
    3589             :   }
    3590        7938 :   return S;
    3591             : }
    3592             : long
    3593         399 : mfolddim(long N, long k, GEN CHI)
    3594             : {
    3595         399 :   pari_sp av = avma;
    3596         399 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3597         399 :   return gc_long(av, mfolddim_i(N, k, CHIP));
    3598             : }
    3599             : /* Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3600             : long
    3601       15232 : mfnewdim(long N, long k, GEN CHI)
    3602             : {
    3603             :   pari_sp av;
    3604             :   long S;
    3605       15232 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3606       15232 :   S = mfcuspdim(N, k, CHIP); if (!S) return 0;
    3607        7525 :   av = avma; return gc_long(av, S - mfolddim_i(N, k, CHIP));
    3608             : }
    3609             : 
    3610             : /* trace form, given as closure */
    3611             : static GEN
    3612         924 : mftraceform_new(long N, long k, GEN CHI)
    3613             : {
    3614             :   GEN T;
    3615         924 :   if (k == 1) return initwt1newtrace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3616         903 :   T = initnewtrace(N,CHI); if (!T) return mftrivial();
    3617         903 :   return tag(t_MF_NEWTRACE, mkNK(N,k,CHI), T);
    3618             : }
    3619             : static GEN
    3620          14 : mftraceform_cusp(long N, long k, GEN CHI)
    3621             : {
    3622          14 :   if (k == 1) return initwt1trace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3623           7 :   return tag(t_MF_TRACE, mkNK(N,k,CHI), inittrace(N,CHI,NULL));
    3624             : }
    3625             : static GEN
    3626          91 : mftraceform_i(GEN NK, long space)
    3627             : {
    3628             :   GEN CHI;
    3629             :   long N, k;
    3630          91 :   checkNK(NK, &N, &k, &CHI, 0);
    3631          91 :   if (!mfdim_Nkchi(N, k, CHI, space)) return mftrivial();
    3632          70 :   switch(space)
    3633             :   {
    3634          49 :     case mf_NEW: return mftraceform_new(N, k, CHI);
    3635          14 :     case mf_CUSP:return mftraceform_cusp(N, k, CHI);
    3636             :   }
    3637           7 :   pari_err_DOMAIN("mftraceform", "space", "=", utoi(space), NK);
    3638             :   return NULL;/*LCOV_EXCL_LINE*/
    3639             : }
    3640             : GEN
    3641          91 : mftraceform(GEN NK, long space)
    3642          91 : { pari_sp av = avma; return gerepilecopy(av, mftraceform_i(NK,space)); }
    3643             : 
    3644             : static GEN
    3645       15456 : hecke_data(long N, long n)
    3646       15456 : { return mkvecsmall3(n, u_ppo(n, N), N); }
    3647             : /* 1/2-integral weight */
    3648             : static GEN
    3649          84 : heckef2_data(long N, long n)
    3650             : {
    3651             :   ulong f, fN, fN2;
    3652          84 :   if (!uissquareall(n, &f)) return NULL;
    3653          77 :   fN = u_ppo(f, N); fN2 = fN*fN;
    3654          77 :   return mkvec2(myfactoru(fN), mkvecsmall4(n, N, fN2, n/fN2));
    3655             : }
    3656             : /* N = mf_get_N(F) or a multiple */
    3657             : static GEN
    3658       22372 : mfhecke_i(long n, long N, GEN F)
    3659             : {
    3660       22372 :   if (n == 1) return F;
    3661       15246 :   return tag2(t_MF_HECKE, mf_get_NK(F), hecke_data(N,n), F);
    3662             : }
    3663             : 
    3664             : GEN
    3665         105 : mfhecke(GEN mf, GEN F, long n)
    3666             : {
    3667         105 :   pari_sp av = avma;
    3668             :   GEN NK, CHI, gk, DATA;
    3669             :   long N, nk, dk;
    3670         105 :   mf = checkMF(mf);
    3671         105 :   if (!checkmf_i(F)) pari_err_TYPE("mfhecke",F);
    3672         105 :   if (n <= 0) pari_err_TYPE("mfhecke [n <= 0]", stoi(n));
    3673         105 :   if (n == 1) return gcopy(F);
    3674         105 :   gk = mf_get_gk(F);
    3675         105 :   Qtoss(gk,&nk,&dk);
    3676         105 :   CHI = mf_get_CHI(F);
    3677         105 :   N = MF_get_N(mf);
    3678         105 :   if (dk == 2)
    3679             :   {
    3680          77 :     DATA = heckef2_data(N,n);
    3681          77 :     if (!DATA) return mftrivial();
    3682             :   }
    3683             :   else
    3684          28 :     DATA = hecke_data(N,n);
    3685          98 :   NK = mkgNK(lcmii(stoi(N), mf_get_gN(F)), gk, CHI, mf_get_field(F));
    3686          98 :   return gerepilecopy(av, tag2(t_MF_HECKE, NK, DATA, F));
    3687             : }
    3688             : 
    3689             : /* form F given by closure, compute B(d)(F) as closure (q -> q^d) */
    3690             : static GEN
    3691       30135 : mfbd_i(GEN F, long d)
    3692             : {
    3693             :   GEN D, NK, gk, CHI;
    3694       30135 :   if (d == 1) return F;
    3695       11214 :   if (d <= 0) pari_err_TYPE("mfbd [d <= 0]", stoi(d));
    3696       11214 :   if (mf_get_type(F) != t_MF_BD) D = utoi(d);
    3697           7 :   else { D = mului(d, gel(F,3)); F = gel(F,2); }
    3698       11214 :   gk = mf_get_gk(F); CHI = mf_get_CHI(F);
    3699       11214 :   if (typ(gk) != t_INT) CHI = mfcharmul(CHI, get_mfchar(utoi(d << 2)));
    3700       11214 :   NK = mkgNK(muliu(mf_get_gN(F), d), gk, CHI, mf_get_field(F));
    3701       11214 :   return tag2(t_MF_BD, NK, F, D);
    3702             : }
    3703             : GEN
    3704          42 : mfbd(GEN F, long d)
    3705             : {
    3706          42 :   pari_sp av = avma;
    3707          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfbd",F);
    3708          42 :   return gerepilecopy(av, mfbd_i(F, d));
    3709             : }
    3710             : 
    3711             : /* A[i+1] = a(t*i^2) */
    3712             : static GEN
    3713          98 : RgV_shimura(GEN A, long n, long t, long N, long r, GEN CHI)
    3714             : {
    3715          98 :   GEN R, a0, Pn = mfcharpol(CHI);
    3716          98 :   long m, st, ord = mfcharorder(CHI), vt = varn(Pn), Nt = t == 1? N: ulcm(N,t);
    3717             : 
    3718          98 :   R = cgetg(n + 2, t_VEC);
    3719          98 :   st = odd(r)? -t: t;
    3720          98 :   a0 = gel(A, 1);
    3721          98 :   if (!gequal0(a0))
    3722             :   {
    3723           7 :     long o = mfcharorder(CHI);
    3724           7 :     if (st != 1 && odd(o)) o <<= 1;
    3725           7 :     a0 = gmul(a0, charLFwtk(Nt, r, CHI, o, st));
    3726             :   }
    3727          98 :   gel(R, 1) = a0;
    3728         630 :   for (m = 1; m <= n; m++)
    3729             :   {
    3730         532 :     GEN Dm = mydivisorsu(u_ppo(m, Nt)), S = gel(A, m*m + 1);
    3731         532 :     long i, l = lg(Dm);
    3732         805 :     for (i = 2; i < l; i++)
    3733             :     { /* (e,Nt) = 1; skip i = 1: e = 1, done above */
    3734         273 :       long e = Dm[i], me = m / e, a = mfcharevalord(CHI, e, ord);
    3735         273 :       GEN c, C = powuu(e, r - 1);
    3736         273 :       if (kross(st, e) == -1) C = negi(C);
    3737         273 :       c = Qab_Czeta(a, ord, C, vt);
    3738         273 :       S = gadd(S, gmul(c, gel(A, me*me + 1)));
    3739             :     }
    3740         532 :     gel(R, m+1) = S;
    3741             :   }
    3742          98 :   return degpol(Pn) > 1? gmodulo(R, Pn): R;
    3743             : }
    3744             : 
    3745             : static long
    3746          28 : mfisinkohnen(GEN mf, GEN F)
    3747             : {
    3748          28 :   GEN v, gk = MF_get_gk(mf), CHI = MF_get_CHI(mf);
    3749          28 :   long i, eps, N4 = MF_get_N(mf) >> 2, sb = mfsturmNgk(N4 << 4, gk) + 1;
    3750          28 :   eps = N4 % mfcharconductor(CHI)? -1 : 1;
    3751          28 :   if (odd(MF_get_r(mf))) eps = -eps;
    3752          28 :   v = mfcoefs(F, sb, 1);
    3753         686 :   for (i = 2;     i <= sb; i+=4) if (!gequal0(gel(v,i+1))) return 0;
    3754         245 :   for (i = 2+eps; i <= sb; i+=4) if (!gequal0(gel(v,i+1))) return 0;
    3755          14 :   return 1;
    3756             : }
    3757             : 
    3758             : static long
    3759          42 : mfshimura_space_cusp(GEN mf)
    3760             : {
    3761             :   long N4;
    3762          42 :   if (MF_get_r(mf) == 1 && (N4 = MF_get_N(mf) >> 2) >= 4)
    3763             :   {
    3764          21 :     GEN E = gel(myfactoru(N4), 2);
    3765          21 :     long ma = vecsmall_max(E);
    3766          21 :     if (ma > 2 || (ma == 2 && !mfcharistrivial(MF_get_CHI(mf)))) return 0;
    3767             :   }
    3768          28 :   return 1;
    3769             : }
    3770             : 
    3771             : /* D is either a discriminant (not necessarily fundamental) with
    3772             :    sign(D)=(-1)^{k-1/2}*eps, or a positive squarefree integer t, which is then
    3773             :    transformed into a fundamental discriminant of the correct sign. */
    3774             : GEN
    3775          42 : mfshimura(GEN mf, GEN F, long t)
    3776             : {
    3777          42 :   pari_sp av = avma;
    3778             :   GEN G, res, mf2, CHI;
    3779          42 :   long sb, M, r, N, space = mf_FULL;
    3780             : 
    3781          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfshimura",F);
    3782          42 :   mf = checkMF(mf);
    3783          42 :   r = MF_get_r(mf);
    3784          42 :   if (r <= 0) pari_err_DOMAIN("mfshimura", "weight", "<=", ghalf, mf_get_gk(F));
    3785          42 :   if (t <= 0 || !uissquarefree(t)) pari_err_TYPE("mfshimura [t]", stoi(t));
    3786          35 :   N = MF_get_N(mf); M = N >> 1;
    3787          35 :   if (mfiscuspidal(mf,F))
    3788             :   {
    3789          28 :     if (mfshimura_space_cusp(mf)) space = mf_CUSP;
    3790          28 :     if (mfisinkohnen(mf,F)) M = N >> 2;
    3791             :   }
    3792          35 :   CHI = MF_get_CHI(mf);
    3793          35 :   mf2 = mfinit_Nkchi(M, r << 1, mfcharpow(CHI, gen_2), space, 0);
    3794          35 :   sb = mfsturm(mf2);
    3795          35 :   G = RgV_shimura(mfcoefs_i(F, sb*sb, t), sb, t, N, r, CHI);
    3796          35 :   res = mftobasis_i(mf2, G);
    3797             :   /* not mflinear(mf2,): we want lowest possible level */
    3798          35 :   G = mflinear(MF_get_basis(mf2), res);
    3799          35 :   return gerepilecopy(av, mkvec3(mf2, G, res));
    3800             : }
    3801             : 
    3802             : /* W ZabM (ZM if n = 1), a t_INT or NULL, b t_INT, ZXQ mod P or NULL.
    3803             :  * Write a/b = A/d with d t_INT and A Zab return [W,d,A,P] */
    3804             : static GEN
    3805        7168 : mkMinv(GEN W, GEN a, GEN b, GEN P)
    3806             : {
    3807        7168 :   GEN A = (b && typ(b) == t_POL)? Q_remove_denom(QXQ_inv(b,P), &b): NULL;
    3808        7168 :   if (a && b)
    3809             :   {
    3810        1148 :     a = Qdivii(a,b);
    3811        1148 :     if (typ(a) == t_INT) b = gen_1; else { b = gel(a,2); a = gel(a,1); }
    3812        1148 :     if (is_pm1(a)) a = NULL;
    3813             :   }
    3814        7168 :   if (a) A = A? ZX_Z_mul(A,a): a; else if (!A) A = gen_1;
    3815        7168 :   if (!b) b = gen_1;
    3816        7168 :   if (!P) P = gen_0;
    3817        7168 :   return mkvec4(W,b,A,P);
    3818             : }
    3819             : /* M square invertible QabM, return [M',d], M*M' = d*Id */
    3820             : static GEN
    3821         532 : QabM_Minv(GEN M, GEN P, long n)
    3822             : {
    3823             :   GEN dW, W, dM;
    3824         532 :   M = Q_remove_denom(M, &dM);
    3825         532 :   W = P? ZabM_inv(liftpol_shallow(M), P, n, &dW): ZM_inv(M, &dW);
    3826         532 :   return mkMinv(W, dM, dW, P);
    3827             : }
    3828             : /* Simplified form of mfclean, after a QabM_indexrank: M a ZabM with full
    3829             :  * column rank and z = indexrank(M) is known */
    3830             : static GEN
    3831         826 : mfclean2(GEN M, GEN z, GEN P, long n)
    3832             : {
    3833         826 :   GEN d, Minv, y = gel(z,1), W = rowpermute(M, y);
    3834         826 :   W = P? ZabM_inv(liftpol_shallow(W), P, n, &d): ZM_inv(W, &d);
    3835         826 :   M = rowslice(M, 1, y[lg(y)-1]);
    3836         826 :   Minv = mkMinv(W, NULL, d, P);
    3837         826 :   return mkvec3(y, Minv, M);
    3838             : }
    3839             : /* M QabM, lg(M)>1 and [y,z] its rank profile. Let Minv be the inverse of the
    3840             :  * invertible square matrix in mkMinv format. Return [y,Minv, M[..y[#y],]]
    3841             :  * P cyclotomic polynomial of order n > 2 or NULL */
    3842             : static GEN
    3843        4599 : mfclean(GEN M, GEN P, long n, int ratlift)
    3844             : {
    3845        4599 :   GEN W, v, y, z, d, Minv, dM, MdM = Q_remove_denom(M, &dM);
    3846        4599 :   if (n <= 2)
    3847        3633 :     W = ZM_pseudoinv(MdM, &v, &d);
    3848             :   else
    3849         966 :     W = ZabM_pseudoinv_i(liftpol_shallow(MdM), P, n, &v, &d, ratlift);
    3850        4599 :   y = gel(v,1);
    3851        4599 :   z = gel(v,2);
    3852        4599 :   if (lg(z) != lg(MdM)) M = vecpermute(M,z);
    3853        4599 :   M = rowslice(M, 1, y[lg(y)-1]);
    3854        4599 :   Minv = mkMinv(W, dM, d, P);
    3855        4599 :   return mkvec3(y, Minv, M);
    3856             : }
    3857             : /* call mfclean using only CHI */
    3858             : static GEN
    3859        3731 : mfcleanCHI(GEN M, GEN CHI, int ratlift)
    3860             : {
    3861        3731 :   long n = mfcharorder(CHI);
    3862        3731 :   GEN P = (n <= 2)? NULL: mfcharpol(CHI);
    3863        3731 :   return mfclean(M, P, n, ratlift);
    3864             : }
    3865             : 
    3866             : /* DATA component of a t_MF_NEWTRACE. Was it stripped to save memory ? */
    3867             : static int
    3868       30919 : newtrace_stripped(GEN DATA)
    3869       30919 : { return DATA && (lg(DATA) == 5 && typ(gel(DATA,3)) == t_INT); }
    3870             : /* f a t_MF_NEWTRACE */
    3871             : static GEN
    3872       30919 : newtrace_DATA(long N, GEN f)
    3873             : {
    3874       30919 :   GEN DATA = gel(f,2);
    3875       30919 :   return newtrace_stripped(DATA)? initnewtrace(N, DATA): DATA;
    3876             : }
    3877             : /* reset cachenew for new level incorporating new DATA, tf a t_MF_NEWTRACE
    3878             :  * (+ possibly initialize 'full' for new allowed levels) */
    3879             : static void
    3880       30919 : reset_cachenew(cachenew_t *cache, long N, GEN tf)
    3881             : {
    3882             :   long i, n, l;
    3883       30919 :   GEN v, DATA = newtrace_DATA(N,tf);
    3884       30919 :   cache->DATA = DATA;
    3885       30919 :   if (!DATA) return;
    3886       30814 :   n = cache->n;
    3887       30814 :   v = cache->vfull; l = N+1; /* = lg(DATA) */
    3888     1844234 :   for (i = 1; i < l; i++)
    3889     1813420 :     if (typ(gel(v,i)) == t_INT && lg(gel(DATA,i)) != 1)
    3890       47782 :       gel(v,i) = const_vec(n, NULL);
    3891       30814 :   cache->VCHIP = gel(gel(DATA,N),_VCHIP);
    3892             : }
    3893             : /* initialize a cache of newtrace / cusptrace up to index n and level | N;
    3894             :  * DATA may be NULL (<=> Tr^new = 0). tf a t_MF_NEWTRACE */
    3895             : static void
    3896       11907 : init_cachenew(cachenew_t *cache, long n, long N, GEN tf)
    3897             : {
    3898       11907 :   long i, l = N+1; /* = lg(tf.DATA) when DATA != NULL */
    3899             :   GEN v;
    3900       11907 :   cache->n = n;
    3901       11907 :   cache->vnew = v = cgetg(l, t_VEC);
    3902      793499 :   for (i = 1; i < l; i++) gel(v,i) = (N % i)? gen_0: const_vec(n, NULL);
    3903       11907 :   cache->newHIT = cache->newTOTAL = cache->cuspHIT = cache->cuspTOTAL = 0;
    3904       11907 :   cache->vfull = v = zerovec(N);
    3905       11907 :   reset_cachenew(cache, N, tf);
    3906       11907 : }
    3907             : static void
    3908       16240 : dbg_cachenew(cachenew_t *C)
    3909             : {
    3910       16240 :   if (DEBUGLEVEL >= 2 && C)
    3911           0 :     err_printf("newtrace cache hits: new = %ld/%ld, cusp = %ld/%ld\n",
    3912             :                     C->newHIT, C->newTOTAL, C->cuspHIT, C->cuspTOTAL);
    3913       16240 : }
    3914             : 
    3915             : /* newtrace_{N,k}(d*i), i = n0, ..., n */
    3916             : static GEN
    3917      137221 : colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *cache)
    3918             : {
    3919      137221 :   GEN v = cgetg(n-n0+2, t_COL);
    3920             :   long i;
    3921     3311553 :   for (i = n0; i <= n; i++) gel(v, i-n0+1) = mfnewtracecache(N, k, i*d, cache);
    3922      137221 :   return v;
    3923             : }
    3924             : /* T_n(l*m0, l*(m0+1), ..., l*m) F, F = t_MF_NEWTRACE [N,k],DATA, cache
    3925             :  * contains DATA != NULL as well as cached values of F */
    3926             : static GEN
    3927       75383 : heckenewtrace(long m0, long m, long l, long N, long NBIG, long k, long n, cachenew_t *cache)
    3928             : {
    3929       75383 :   long lD, a, k1, nl = n*l;
    3930       75383 :   GEN D, V, v = colnewtrace(m0, m, nl, N, k, cache); /* d=1 */
    3931             :   GEN VCHIP;
    3932       75383 :   if (n == 1) return v;
    3933       49434 :   VCHIP = cache->VCHIP;
    3934       49434 :   D = mydivisorsu(u_ppo(n, NBIG)); lD = lg(D);
    3935       49434 :   k1 = k - 1;
    3936      109732 :   for (a = 2; a < lD; a++)
    3937             :   { /* d > 1, (d,NBIG) = 1 */
    3938       60298 :     long i, j, d = D[a], c = ugcd(l, d), dl = d/c, m0d = ceildiv(m0, dl);
    3939       60298 :     GEN C = vchip_lift(VCHIP, d, powuu(d, k1));
    3940             :     /* m0=0: i = 1 => skip F(0) = 0 */
    3941       60298 :     if (!m0) { i = 1; j = dl; } else { i = 0; j = m0d*dl; }
    3942       60298 :     V = colnewtrace(m0d, m/dl, nl/(d*c), N, k, cache);
    3943             :     /* C = chi(d) d^(k-1) */
    3944      625058 :     for (; j <= m; i++, j += dl)
    3945      564760 :       gel(v,j-m0+1) = gadd(gel(v,j-m0+1), vchip_mod(VCHIP, gmul(C,gel(V,i+1))));
    3946             :   }
    3947       49434 :   return v;
    3948             : }
    3949             : 
    3950             : /* Given v = an[i], return an[d*i] */
    3951             : static GEN
    3952         658 : anextract(GEN v, long n, long d)
    3953             : {
    3954         658 :   GEN w = cgetg(n+2, t_VEC);
    3955             :   long i;
    3956        2744 :   for (i = 0; i <= n; i++) gel(w, i+1) = gel(v, i*d+1);
    3957         658 :   return w;
    3958             : }
    3959             : /* T_n(F)(0, l, ..., l*m) */
    3960             : static GEN
    3961        1414 : hecke_i(long m, long l, GEN V, GEN F, GEN DATA)
    3962             : {
    3963             :   long k, n, nNBIG, NBIG, lD, M, a, t, nl;
    3964             :   GEN D, v, CHI;
    3965        1414 :   if (typ(DATA) == t_VEC)
    3966             :   { /* 1/2-integral k */
    3967          98 :     if (!V) { GEN S = gel(DATA,2); V = mfcoefs_i(F, m*l*S[3], S[4]); }
    3968          98 :     return RgV_heckef2(m, l, V, F, DATA);
    3969             :   }
    3970        1316 :   k = mf_get_k(F);
    3971        1316 :   n = DATA[1]; nl = n*l;
    3972        1316 :   nNBIG = DATA[2];
    3973        1316 :   NBIG = DATA[3];
    3974        1316 :   if (nNBIG == 1) return V? V: mfcoefs_i(F,m,nl);
    3975         651 :   if (!V && mf_get_type(F) == t_MF_NEWTRACE)
    3976             :   { /* inline F to allow cache, T_n at level NBIG acting on Tr^new(N,k,CHI) */
    3977             :     cachenew_t cache;
    3978         322 :     long N = mf_get_N(F);
    3979         322 :     init_cachenew(&cache, m*nl, N, F);
    3980         322 :     v = heckenewtrace(0, m, l, N, NBIG, k, n, &cache);
    3981         322 :     dbg_cachenew(&cache);
    3982         322 :     settyp(v, t_VEC); return v;
    3983             :   }
    3984         329 :   CHI = mf_get_CHI(F);
    3985         329 :   D = mydivisorsu(nNBIG); lD = lg(D);
    3986         329 :   M = m + 1;
    3987         329 :   t = nNBIG * ugcd(nNBIG, l);
    3988         329 :   if (!V) V = mfcoefs_i(F, m * t, nl / t); /* usually nl = t */
    3989         329 :   v = anextract(V, m, t); /* mfcoefs(F, m, nl); d = 1 */
    3990         658 :   for (a = 2; a < lD; a++)
    3991             :   { /* d > 1, (d, NBIG) = 1 */
    3992         329 :     long d = D[a], c = ugcd(l, d), dl = d/c, i, idl;
    3993         329 :     GEN C = gmul(mfchareval(CHI, d), powuu(d, k-1));
    3994         329 :     GEN w = anextract(V, m/dl, t/(d*c)); /* mfcoefs(F, m/dl, nl/(d*c)) */
    3995        1008 :     for (i = idl = 1; idl <= M; i++, idl += dl)
    3996         679 :       gel(v,idl) = gadd(gel(v,idl), gmul(C, gel(w,i)));
    3997             :   }
    3998         329 :   return v;
    3999             : }
    4000             : 
    4001             : static GEN
    4002       11725 : mkmf(GEN x1, GEN x2, GEN x3, GEN x4, GEN x5)
    4003             : {
    4004       11725 :   GEN MF = obj_init(5, MF_SPLITN);
    4005       11725 :   gel(MF,1) = x1;
    4006       11725 :   gel(MF,2) = x2;
    4007       11725 :   gel(MF,3) = x3;
    4008       11725 :   gel(MF,4) = x4;
    4009       11725 :   gel(MF,5) = x5; return MF;
    4010             : }
    4011             : 
    4012             : /* return an integer b such that p | b => T_p^k Tr^new = 0, for all k > 0 */
    4013             : static long
    4014        7273 : get_badj(long N, long FC)
    4015             : {
    4016        7273 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    4017        7273 :   long i, b = 1, l = lg(P);
    4018       19397 :   for (i = 1; i < l; i++)
    4019       12124 :     if (E[i] > 1 && u_lval(FC, P[i]) < E[i]) b *= P[i];
    4020        7273 :   return b;
    4021             : }
    4022             : /* in place, assume perm strictly increasing */
    4023             : static void
    4024        1148 : vecpermute_inplace(GEN v, GEN perm)
    4025             : {
    4026        1148 :   long i, l = lg(perm);
    4027        7770 :   for (i = 1; i < l; i++) gel(v,i) = gel(v,perm[i]);
    4028        1148 : }
    4029             : 
    4030             : /* Find basis of newspace using closures; assume k >= 2 and !badchar.
    4031             :  * Return NULL if space is empty, else
    4032             :  * [mf1, list of closures T(j)traceform, list of corresponding j, matrix] */
    4033             : static GEN
    4034       14994 : mfnewinit(long N, long k, GEN CHI, cachenew_t *cache, long init)
    4035             : {
    4036             :   GEN S, vj, M, CHIP, mf1, listj, P, tf;
    4037             :   long j, ct, ctlj, dim, jin, SB, sb, two, ord, FC, badj;
    4038             : 
    4039       14994 :   dim = mfnewdim(N, k, CHI);
    4040       14994 :   if (!dim && !init) return NULL;
    4041        7273 :   sb = mfsturmNk(N, k);
    4042        7273 :   CHIP = mfchartoprimitive(CHI, &FC);
    4043             :   /* remove newtrace data from S to save space in output: negligible slowdown */
    4044        7273 :   tf = tag(t_MF_NEWTRACE, mkNK(N,k,CHIP), CHIP);
    4045        7273 :   badj = get_badj(N, FC);
    4046             :   /* try sbsmall first: Sturm bound not sharp for new space */
    4047        7273 :   SB = ceilA1(N, k);
    4048        7273 :   listj = cgetg(2*sb + 3, t_VECSMALL);
    4049      332983 :   for (j = ctlj = 1; ctlj < 2*sb + 3; j++)
    4050      325710 :     if (ugcd(j, badj) == 1) listj[ctlj++] = j;
    4051        7273 :   if (init)
    4052             :   {
    4053        3990 :     init_cachenew(cache, (SB+1)*listj[dim+1], N, tf);
    4054        3990 :     if (init == -1 || !dim) return NULL; /* old space or dim = 0 */
    4055             :   }
    4056             :   else
    4057        3283 :     reset_cachenew(cache, N, tf);
    4058             :   /* cache.DATA is not NULL */
    4059        6818 :   ord = mfcharorder(CHIP);
    4060        6818 :   P = ord <= 2? NULL: mfcharpol(CHIP);
    4061        6818 :   vj = cgetg(dim+1, t_VECSMALL);
    4062        6818 :   M = cgetg(dim+1, t_MAT);
    4063        6825 :   for (two = 1, ct = 0, jin = 1; two <= 2; two++)
    4064             :   {
    4065        6825 :     long a, jlim = jin + sb;
    4066       19642 :     for (a = jin; a <= jlim; a++)
    4067             :     {
    4068             :       GEN z, vecz;
    4069       19635 :       ct++; vj[ct] = listj[a];
    4070       19635 :       gel(M, ct) = heckenewtrace(0, SB, 1, N, N, k, vj[ct], cache);
    4071       19635 :       if (ct < dim) continue;
    4072             : 
    4073        7392 :       z = QabM_indexrank(M, P, ord);
    4074        7392 :       vecz = gel(z, 2); ct = lg(vecz) - 1;
    4075        7392 :       if (ct == dim) { M = mkvec3(z, gen_0, M); break; } /*maximal rank, done*/
    4076         574 :       vecpermute_inplace(M, vecz);
    4077         574 :       vecpermute_inplace(vj, vecz);
    4078             :     }
    4079        6825 :     if (a <= jlim) break;
    4080             :     /* sbsmall was not sufficient, use Sturm bound: must extend M */
    4081          70 :     for (j = 1; j <= ct; j++)
    4082             :     {
    4083          63 :       GEN t = heckenewtrace(SB + 1, sb, 1, N, N, k, vj[j], cache);
    4084          63 :       gel(M,j) = shallowconcat(gel(M, j), t);
    4085             :     }
    4086           7 :     jin = jlim + 1; SB = sb;
    4087             :   }
    4088        6818 :   S = cgetg(dim + 1, t_VEC);
    4089       25865 :   for (j = 1; j <= dim; j++) gel(S, j) = mfhecke_i(vj[j], N, tf);
    4090        6818 :   dbg_cachenew(cache);
    4091        6818 :   mf1 = mkvec4(utoipos(N), utoipos(k), CHI, utoi(mf_NEW));
    4092        6818 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    4093             : }
    4094             : /* k > 1 integral, mf space is mf_CUSP or mf_FULL */
    4095             : static GEN
    4096          42 : mfinittonew(GEN mf)
    4097             : {
    4098          42 :   GEN CHI = MF_get_CHI(mf), S = MF_get_S(mf), vMjd = MFcusp_get_vMjd(mf);
    4099          42 :   GEN M = MF_get_M(mf), vj, mf1;
    4100          42 :   long i, j, l, l0 = lg(S), N0 = MF_get_N(mf);
    4101         203 :   for (i = l0-1; i > 0; i--)
    4102             :   {
    4103         189 :     long N = gel(vMjd,i)[1];
    4104         189 :     if (N != N0) break;
    4105             :   }
    4106          42 :   if (i == l0-1) return NULL;
    4107          35 :   S = vecslice(S, i+1, l0-1); /* forms of conductor N0 */
    4108          35 :   l = lg(S); vj = cgetg(l, t_VECSMALL);
    4109         196 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd,j+i)[2];
    4110          35 :   M = vecslice(M, lg(M)-lg(S)+1, lg(M)-1); /* their coefficients */
    4111          35 :   M = mfcleanCHI(M, CHI, 0);
    4112          35 :   mf1 = mkvec4(utoipos(N0), MF_get_gk(mf), CHI, utoi(mf_NEW));
    4113          35 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    4114             : }
    4115             : 
    4116             : /* Bd(f)[m0..m], v = f[ceil(m0/d)..floor(m/d)], m0d = ceil(m0/d) */
    4117             : static GEN
    4118       69181 : RgC_Bd_expand(long m0, long m, GEN v, long d, long m0d)
    4119             : {
    4120             :   long i, j;
    4121             :   GEN w;
    4122       69181 :   if (d == 1) return v;
    4123       20160 :   w = zerocol(m-m0+1);
    4124       20160 :   if (!m0) { i = 1; j = d; } else { i = 0; j = m0d*d; }
    4125      411103 :   for (; j <= m; i++, j += d) gel(w,j-m0+1) = gel(v,i+1);
    4126       20160 :   return w;
    4127             : }
    4128             : /* S a non-empty vector of t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)); M the matrix
    4129             :  * of their coefficients r*0, r*1, ..., r*m0 (~ mfvectomat) or NULL (empty),
    4130             :  * extend it to coeffs up to m > m0. The forms B_d(T_j(tf_N))in S should be
    4131             :  * sorted by level N, then j, then increasing d. No reordering here. */
    4132             : static GEN
    4133        8218 : bhnmat_extend(GEN M, long m, long r, GEN S, cachenew_t *cache)
    4134             : {
    4135        8218 :   long i, mr, m0, m0r, Nold = 0, jold = 0, l = lg(S);
    4136        8218 :   GEN MAT = cgetg(l, t_MAT), v = NULL;
    4137        8218 :   if (M) { m0 = nbrows(M); m0r = m0 * r; } else m0 = m0r = 0;
    4138        8218 :   mr = m*r;
    4139       77399 :   for (i = 1; i < l; i++)
    4140             :   {
    4141             :     long d, j, md, N;
    4142       69181 :     GEN c, f = bhn_parse(gel(S,i), &d,&j); /* t_MF_NEWTRACE */
    4143       69181 :     N = mf_get_N(f);
    4144       69181 :     md = ceildiv(m0r,d);
    4145       69181 :     if (N != Nold) { reset_cachenew(cache, N, f); Nold = N; jold = 0; }
    4146       69181 :     if (!cache->DATA) { gel(MAT,i) = zerocol(m+1); continue; }
    4147       69181 :     if (j != jold || md)
    4148       55363 :     { v = heckenewtrace(md, mr/d, 1, N, N, mf_get_k(f), j,cache); jold=j; }
    4149       69181 :     c = RgC_Bd_expand(m0r, mr, v, d, md);
    4150       69181 :     if (r > 1) c = c_deflate(m-m0, r, c);
    4151       69181 :     if (M) c = shallowconcat(gel(M,i), c);
    4152       69181 :     gel(MAT,i) = c;
    4153             :   }
    4154        8218 :   return MAT;
    4155             : }
    4156             : 
    4157             : static GEN
    4158        3045 : mfinitcusp(long N, long k, GEN CHI, cachenew_t *cache, long space)
    4159             : {
    4160             :   long L, l, lDN1, FC, N1, d1, i, init;
    4161        3045 :   GEN vS, vMjd, DN1, vmf, CHIP = mfchartoprimitive(CHI, &FC);
    4162             : 
    4163        3045 :   d1 = (space == mf_OLD)? mfolddim_i(N, k, CHIP): mfcuspdim(N, k, CHIP);
    4164        3045 :   if (!d1) return NULL;
    4165        2793 :   N1 = N/FC; DN1 = mydivisorsu(N1); lDN1 = lg(DN1);
    4166        2793 :   init = (space == mf_OLD)? -1: 1;
    4167        2793 :   vmf = cgetg(lDN1, t_VEC);
    4168       16590 :   for (i = lDN1 - 1, l = 1; i; i--)
    4169             :   { /* by decreasing level to allow cache */
    4170       13797 :     GEN mf = mfnewinit(FC*DN1[i], k, CHIP, cache, init);
    4171       13797 :     if (mf) gel(vmf, l++) = mf;
    4172       13797 :     init = 0;
    4173             :   }
    4174        2793 :   setlg(vmf,l); vmf = vecreverse(vmf); /* reorder by increasing level */
    4175             : 
    4176        2793 :   L = mfsturmNk(N, k)+1;
    4177        2793 :   vS = vectrunc_init(L);
    4178        2793 :   vMjd = vectrunc_init(L);
    4179        8785 :   for (i = 1; i < l; i++)
    4180             :   {
    4181        5992 :     GEN DNM, mf = gel(vmf,i), S = MF_get_S(mf), vj = MFnew_get_vj(mf);
    4182        5992 :     long a, lDNM, lS = lg(S), M = MF_get_N(mf);
    4183        5992 :     DNM = mydivisorsu(N / M); lDNM = lg(DNM);
    4184       23002 :     for (a = 1; a < lS; a++)
    4185             :     {
    4186       17010 :       GEN tf = gel(S,a);
    4187       17010 :       long b, j = vj[a];
    4188       42189 :       for (b = 1; b < lDNM; b++)
    4189             :       {
    4190       25179 :         long d = DNM[b];
    4191       25179 :         vectrunc_append(vS, mfbd_i(tf, d));
    4192       25179 :         vectrunc_append(vMjd, mkvecsmall3(M, j, d));
    4193             :       }
    4194             :     }
    4195             :   }
    4196        2793 :   return mkmf(NULL, cgetg(1, t_VEC), vS, vMjd, NULL);
    4197             : }
    4198             : 
    4199             : long
    4200        3535 : mfsturm_mf(GEN mf)
    4201             : {
    4202        3535 :   GEN Mindex = MF_get_Mindex(mf);
    4203        3535 :   long n = lg(Mindex)-1;
    4204        3535 :   return n? Mindex[n]-1: 0;
    4205             : }
    4206             : 
    4207             : long
    4208         588 : mfsturm(GEN T)
    4209             : {
    4210             :   long N, nk, dk;
    4211         588 :   GEN CHI, mf = checkMF_i(T);
    4212         588 :   if (mf) return mfsturm_mf(mf);
    4213           7 :   checkNK2(T, &N, &nk, &dk, &CHI, 0);
    4214           7 :   return dk == 1 ? mfsturmNk(N, nk) : mfsturmNk(N, (nk + 1) >> 1);
    4215             : }
    4216             : long
    4217           7 : mfisequal(GEN F, GEN G, long lim)
    4218             : {
    4219           7 :   pari_sp av = avma;
    4220             :   long b;
    4221           7 :   if (!checkmf_i(F)) pari_err_TYPE("mfisequal",F);
    4222           7 :   if (!checkmf_i(G)) pari_err_TYPE("mfisequal",G);
    4223           7 :   b = lim? lim: maxss(mfsturmmf(F), mfsturmmf(G));
    4224           7 :   return gc_long(av, gequal(mfcoefs_i(F, b, 1), mfcoefs_i(G, b, 1)));
    4225             : }
    4226             : 
    4227             : GEN
    4228          35 : mffields(GEN mf)
    4229             : {
    4230          35 :   if (checkmf_i(mf)) return gcopy(mf_get_field(mf));
    4231          35 :   mf = checkMF(mf); return gcopy(MF_get_fields(mf));
    4232             : }
    4233             : 
    4234             : GEN
    4235         322 : mfeigenbasis(GEN mf)
    4236             : {
    4237         322 :   pari_sp ltop = avma;
    4238             :   GEN F, S, v, vP;
    4239             :   long i, l, k, dS;
    4240             : 
    4241         322 :   mf = checkMF(mf);
    4242         322 :   k = MF_get_k(mf);
    4243         322 :   S = MF_get_S(mf); dS = lg(S)-1;
    4244         322 :   if (!dS) return cgetg(1, t_VEC);
    4245         315 :   F = MF_get_newforms(mf);
    4246         315 :   vP = MF_get_fields(mf);
    4247         315 :   if (k == 1)
    4248             :   {
    4249         196 :     if (MF_get_space(mf) == mf_FULL)
    4250             :     {
    4251          14 :       long dE = lg(MF_get_E(mf)) - 1;
    4252          14 :       if (dE) F = rowslice(F, dE+1, dE+dS);
    4253             :     }
    4254         196 :     v = vecmflineardiv_linear(S, F);
    4255         196 :     l = lg(v);
    4256             :   }
    4257             :   else
    4258             :   {
    4259         119 :     GEN (*L)(GEN, GEN) = (MF_get_space(mf) == mf_FULL)? mflinear: mflinear_bhn;
    4260         119 :     l = lg(F); v = cgetg(l, t_VEC);
    4261         413 :     for (i = 1; i < l; i++) gel(v,i) = L(mf, gel(F,i));
    4262             :   }
    4263         819 :   for (i = 1; i < l; i++) mf_setfield(gel(v,i), gel(vP,i));
    4264         315 :   return gerepilecopy(ltop, v);
    4265             : }
    4266             : 
    4267             : /* Minv = [M, d, A], v a t_COL; A a Zab, d a t_INT; return (A/d) * M*v */
    4268             : static GEN
    4269        5887 : Minv_RgC_mul(GEN Minv, GEN v)
    4270             : {
    4271        5887 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4272        5887 :   v = RgM_RgC_mul(M, v);
    4273        5887 :   if (!equali1(A))
    4274             :   {
    4275        1568 :     if (typ(A) == t_POL && degpol(A) > 0) A = mkpolmod(A, gel(Minv,4));
    4276        1568 :     v = RgC_Rg_mul(v, A);
    4277             :   }
    4278        5887 :   if (!equali1(d)) v = RgC_Rg_div(v, d);
    4279        5887 :   return v;
    4280             : }
    4281             : static GEN
    4282        1099 : Minv_RgM_mul(GEN Minv, GEN B)
    4283             : {
    4284        1099 :   long j, l = lg(B);
    4285        1099 :   GEN M = cgetg(l, t_MAT);
    4286        4627 :   for (j = 1; j < l; j++) gel(M,j) = Minv_RgC_mul(Minv, gel(B,j));
    4287        1099 :   return M;
    4288             : }
    4289             : /* B * Minv; allow B = NULL for Id */
    4290             : static GEN
    4291        2212 : RgM_Minv_mul(GEN B, GEN Minv)
    4292             : {
    4293        2212 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4294        2212 :   if (B) M = RgM_mul(B, M);
    4295        2212 :   if (!equali1(A))
    4296             :   {
    4297         882 :     if (typ(A) == t_POL) A = mkpolmod(A, gel(Minv,4));
    4298         882 :     M = RgM_Rg_mul(M, A);
    4299             :   }
    4300        2212 :   if (!equali1(d)) M = RgM_Rg_div(M,d);
    4301        2212 :   return M;
    4302             : }
    4303             : 
    4304             : /* perm vector of strictly increasing indices, v a vector or arbitrary length;
    4305             :  * the last r entries of perm fall beyond v.
    4306             :  * Return v o perm[1..(-r)], discarding the last r entries of v */
    4307             : static GEN
    4308        1127 : vecpermute_partial(GEN v, GEN perm, long *r)
    4309             : {
    4310        1127 :   long i, n = lg(v)-1, l = lg(perm);
    4311             :   GEN w;
    4312        1127 :   if (perm[l-1] <= n) { *r = 0; return vecpermute(v,perm); }
    4313          63 :   for (i = 1; i < l; i++)
    4314          63 :     if (perm[i] > n) break;
    4315          21 :   *r = l - i; l = i;
    4316          21 :   w = cgetg(l, typ(v));
    4317          63 :   for (i = 1; i < l; i++) gel(w,i) = gel(v,perm[i]);
    4318          21 :   return w;
    4319             : }
    4320             : 
    4321             : /* given form F, find coeffs of F on mfbasis(mf). If power series, not
    4322             :  * guaranteed correct if precision less than Sturm bound */
    4323             : static GEN
    4324        1183 : mftobasis_i(GEN mf, GEN F)
    4325             : {
    4326             :   GEN v, Mindex, Minv;
    4327        1183 :   if (!MF_get_dim(mf)) return cgetg(1, t_COL);
    4328        1183 :   Mindex = MF_get_Mindex(mf);
    4329        1183 :   Minv = MF_get_Minv(mf);
    4330        1183 :   if (checkmf_i(F))
    4331             :   {
    4332         252 :     long n = Mindex[lg(Mindex)-1];
    4333         252 :     v = vecpermute(mfcoefs_i(F, n, 1), Mindex);
    4334         252 :     return Minv_RgC_mul(Minv, v);
    4335             :   }
    4336             :   else
    4337             :   {
    4338         931 :     GEN A = gel(Minv,1), d = gel(Minv,2);
    4339             :     long r;
    4340         931 :     v = F;
    4341         931 :     switch(typ(F))
    4342             :     {
    4343           0 :       case t_SER: v = sertocol(v);
    4344         931 :       case t_VEC: case t_COL: break;
    4345           0 :       default: pari_err_TYPE("mftobasis", F);
    4346             :     }
    4347         931 :     if (lg(v) == 1) pari_err_TYPE("mftobasis",v);
    4348         931 :     v = vecpermute_partial(v, Mindex, &r);
    4349         931 :     if (!r) return Minv_RgC_mul(Minv, v); /* single solution */
    4350             :     /* affine space of dimension r */
    4351          21 :     v = RgM_RgC_mul(vecslice(A, 1, lg(v)-1), v);
    4352          21 :     if (!equali1(d)) v = RgC_Rg_div(v,d);
    4353          21 :     return mkvec2(v, vecslice(A, lg(A)-r, lg(A)-1));
    4354             :   }
    4355             : }
    4356             : 
    4357             : static GEN
    4358         546 : const_mat(long n, GEN x)
    4359             : {
    4360         546 :   long j, l = n+1;
    4361         546 :   GEN A = cgetg(l,t_MAT);
    4362        3990 :   for (j = 1; j < l; j++) gel(A,j) = const_col(n, x);
    4363         546 :   return A;
    4364             : }
    4365             : 
    4366             : /* L is the mftobasis of a form on CUSP space. We allow mf_FULL or mf_CUSP */
    4367             : static GEN
    4368         273 : mftonew_i(GEN mf, GEN L, long *plevel)
    4369             : {
    4370             :   GEN S, listMjd, CHI, res, Aclos, Acoef, D, perm;
    4371         273 :   long N1, LC, lD, i, l, t, level, N = MF_get_N(mf);
    4372             : 
    4373         273 :   if (MF_get_k(mf) == 1) pari_err_IMPL("mftonew in weight 1");
    4374         273 :   listMjd = MFcusp_get_vMjd(mf);
    4375         273 :   CHI = MF_get_CHI(mf); LC = mfcharconductor(CHI);
    4376         273 :   S = MF_get_S(mf);
    4377             : 
    4378         273 :   N1 = N/LC;
    4379         273 :   D = mydivisorsu(N1); lD = lg(D);
    4380         273 :   perm = cgetg(N1+1, t_VECSMALL);
    4381        1995 :   for (i = 1; i < lD; i++) perm[D[i]] = i;
    4382         273 :   Aclos = const_mat(lD-1, cgetg(1,t_VEC));
    4383         273 :   Acoef = const_mat(lD-1, cgetg(1,t_VEC));
    4384         273 :   l = lg(listMjd);
    4385        2863 :   for (i = 1; i < l; i++)
    4386             :   {
    4387             :     long M, d;
    4388             :     GEN v;
    4389        2590 :     if (gequal0(gel(L,i))) continue;
    4390         266 :     v = gel(listMjd, i);
    4391         266 :     M = perm[ v[1]/LC ];
    4392         266 :     d = perm[ v[3] ];
    4393         266 :     gcoeff(Aclos,M,d) = vec_append(gcoeff(Aclos,M,d), gel(S,i));
    4394         266 :     gcoeff(Acoef,M,d) = shallowconcat(gcoeff(Acoef,M,d), gel(L,i));
    4395             :   }
    4396         273 :   res = cgetg(l, t_VEC); level = 1;
    4397        1995 :   for (i = t = 1; i < lD; i++)
    4398             :   {
    4399        1722 :     long j, M = D[i]*LC;
    4400        1722 :     GEN gM = utoipos(M);
    4401       15120 :     for (j = 1; j < lD; j++)
    4402             :     {
    4403       13398 :       GEN f = gcoeff(Aclos,i,j), C, NK;
    4404             :       long d;
    4405       13398 :       if (lg(f) == 1) continue;
    4406         238 :       NK = mf_get_NK(gel(f,1));
    4407         238 :       d = D[j];
    4408         238 :       C = gcoeff(Acoef,i,j);
    4409         238 :       level = ulcm(level, M*d);
    4410         238 :       gel(res,t++) = mkvec3(gM, utoipos(d), mflinear_i(NK,f,C));
    4411             :     }
    4412             :   }
    4413         273 :   if (plevel) *plevel = level;
    4414         273 :   setlg(res, t); return res;
    4415             : }
    4416             : GEN
    4417          35 : mftonew(GEN mf, GEN F)
    4418             : {
    4419          35 :   pari_sp av = avma;
    4420             :   GEN ES;
    4421             :   long s;
    4422          35 :   mf = checkMF(mf);
    4423          35 :   s = MF_get_space(mf);
    4424          35 :   if (s != mf_FULL && s != mf_CUSP)
    4425           7 :     pari_err_TYPE("mftonew [not a full or cuspidal space]", mf);
    4426          28 :   ES = mftobasisES(mf,F);
    4427          21 :   if (!gequal0(gel(ES,1)))
    4428           0 :     pari_err_TYPE("mftonew [not a cuspidal form]", F);
    4429          21 :   F = gel(ES,2);
    4430          21 :   return gerepilecopy(av, mftonew_i(mf,F, NULL));
    4431             : }
    4432             : 
    4433             : static GEN mfeisenstein_i(long k, GEN CHI1, GEN CHI2);
    4434             : 
    4435             : /* mfinit(F * Theta) */
    4436             : static GEN
    4437          84 : mf2init(GEN mf)
    4438             : {
    4439          84 :   GEN CHI = MF_get_CHI(mf), gk = gadd(MF_get_gk(mf), ghalf);
    4440          84 :   long N = MF_get_N(mf);
    4441          84 :   return mfinit_Nkchi(N, itou(gk), mfchiadjust(CHI, gk, N), mf_FULL, 0);
    4442             : }
    4443             : 
    4444             : static long
    4445         518 : mfvec_first_cusp(GEN v)
    4446             : {
    4447         518 :   long i, l = lg(v);
    4448        1330 :   for (i = 1; i < l; i++)
    4449             :   {
    4450        1246 :     GEN F = gel(v,i);
    4451        1246 :     long t = mf_get_type(F);
    4452        1246 :     if (t == t_MF_BD) { F = gel(F,2); t = mf_get_type(F); }
    4453        1246 :     if (t == t_MF_HECKE) { F = gel(F,3); t = mf_get_type(F); }
    4454        1246 :     if (t == t_MF_NEWTRACE) break;
    4455             :   }
    4456         518 :   return i;
    4457             : }
    4458             : /* vF a vector of mf F of type DIV(LINEAR(BAS,L), f) in (lcm) level N,
    4459             :  * F[2]=LINEAR(BAS,L), F[2][2]=BAS=fixed basis (Eisenstein or bhn type),
    4460             :  * F[2][3]=L, F[3]=f; mfvectomat(vF, n) */
    4461             : static GEN
    4462         525 : mflineardivtomat(long N, GEN vF, long n)
    4463             : {
    4464         525 :   GEN F, M, f, fc, ME, dB, B, a0, V = NULL;
    4465         525 :   long lM, lF = lg(vF), j;
    4466             : 
    4467         525 :   if (lF == 1) return cgetg(1,t_MAT);
    4468         518 :   F = gel(vF,1);
    4469         518 :   if (lg(F) == 5)
    4470             :   { /* chicompat */
    4471         238 :     V = gmael(F,4,4);
    4472         238 :     if (typ(V) == t_INT) V = NULL;
    4473             :   }
    4474         518 :   M = gmael(F,2,2); /* BAS */
    4475         518 :   lM = lg(M);
    4476         518 :   j = mfvec_first_cusp(M);
    4477         518 :   if (j == 1) ME = NULL;
    4478             :   else
    4479             :   { /* BAS starts by Eisenstein */
    4480         133 :     ME = mfvectomat(vecslice(M,1,j-1), n, 1);
    4481         133 :     M = vecslice(M, j,lM-1);
    4482             :   }
    4483         518 :   M = bhnmat_extend_nocache(NULL, N, n, 1, M);
    4484         518 :   if (ME) M = shallowconcat(ME,M);
    4485             :   /* M = mfcoefs of BAS */
    4486         518 :   B = cgetg(lF, t_MAT);
    4487         518 :   dB= cgetg(lF, t_VEC);
    4488        2478 :   for (j = 1; j < lF; j++)
    4489             :   {
    4490        1960 :     GEN g = gel(vF, j); /* t_MF_DIV */
    4491        1960 :     gel(B,j) = RgM_RgC_mul(M, gmael(g,2,3));
    4492        1960 :     gel(dB,j)= gmael(g,2,4);
    4493             :   }
    4494         518 :   f = mfcoefsser(gel(F,3),n);
    4495         518 :   a0 = polcoef_i(f, 0, -1);
    4496         518 :   if (gequal0(a0) || gequal1(a0))
    4497         294 :     a0 = NULL;
    4498             :   else
    4499         224 :     f = gdiv(ser_unscale(f, a0), a0);
    4500         518 :   fc = ginv(f);
    4501        2478 :   for (j = 1; j < lF; j++)
    4502             :   {
    4503        1960 :     pari_sp av = avma;
    4504        1960 :     GEN LISer = RgV_to_ser_full(gel(B,j)), f;
    4505        1960 :     if (a0) LISer = gdiv(ser_unscale(LISer, a0), a0);
    4506        1960 :     f = gmul(LISer, fc);
    4507        1960 :     if (a0) f = ser_unscale(f, ginv(a0));
    4508        1960 :     f = sertocol(f); setlg(f, n+2);
    4509        1960 :     if (!gequal1(gel(dB,j))) f = RgC_Rg_div(f, gel(dB,j));
    4510        1960 :     gel(B,j) = gerepileupto(av,f);
    4511             :   }
    4512         518 :   if (V) B = gmodulo(QabM_tracerel(V, 0, B), gel(V,1));
    4513         518 :   return B;
    4514             : }
    4515             : 
    4516             : static GEN
    4517         189 : mfheckemat_mfcoefs(GEN mf, GEN B, GEN DATA)
    4518             : {
    4519         189 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4520         189 :   long j, l = lg(B), sb = mfsturm_mf(mf);
    4521         189 :   GEN b = MF_get_basis(mf), Q = cgetg(l, t_VEC);
    4522         609 :   for (j = 1; j < l; j++)
    4523             :   {
    4524         420 :     GEN v = hecke_i(sb, 1, gel(B,j), gel(b,j), DATA); /* Tn b[j] */
    4525         420 :     settyp(v,t_COL); gel(Q,j) = vecpermute(v, Mindex);
    4526             :   }
    4527         189 :   return Minv_RgM_mul(Minv,Q);
    4528             : }
    4529             : /* T_p^2, p prime, 1/2-integral weight; B = mfcoefs(mf,sb*p^2,1) or (mf,sb,p^2)
    4530             :  * if p|N */
    4531             : static GEN
    4532           7 : mfheckemat_mfcoefs_p2(GEN mf, long p, GEN B)
    4533             : {
    4534           7 :   pari_sp av = avma;
    4535           7 :   GEN DATA = heckef2_data(MF_get_N(mf), p*p);
    4536           7 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, DATA));
    4537             : }
    4538             : /* convert Mindex from row-index to mfcoef indexation: a(n) is stored in
    4539             :  * mfcoefs()[n+1], so subtract 1 from all indices */
    4540             : static GEN
    4541          49 : Mindex_as_coef(GEN mf)
    4542             : {
    4543          49 :   GEN v, Mindex = MF_get_Mindex(mf);
    4544          49 :   long i, l = lg(Mindex);
    4545          49 :   v = cgetg(l, t_VECSMALL);
    4546         210 :   for (i = 1; i < l; i++) v[i] = Mindex[i]-1;
    4547          49 :   return v;
    4548             : }
    4549             : /* T_p, p prime; B = mfcoefs(mf,sb*p,1) or (mf,sb,p) if p|N; integral weight */
    4550             : static GEN
    4551          35 : mfheckemat_mfcoefs_p(GEN mf, long p, GEN B)
    4552             : {
    4553          35 :   pari_sp av = avma;
    4554          35 :   GEN vm, Q, C, Minv = MF_get_Minv(mf);
    4555          35 :   long lm, k, i, j, l = lg(B), N = MF_get_N(mf);
    4556             : 
    4557          35 :   if (N % p == 0) return Minv_RgM_mul(Minv, rowpermute(B, MF_get_Mindex(mf)));
    4558          21 :   k = MF_get_k(mf);
    4559          21 :   C = gmul(mfchareval(MF_get_CHI(mf), p), powuu(p, k-1));
    4560          21 :   vm = Mindex_as_coef(mf); lm = lg(vm);
    4561          21 :   Q = cgetg(l, t_MAT);
    4562         147 :   for (j = 1; j < l; j++) gel(Q,j) = cgetg(lm, t_COL);
    4563         147 :   for (i = 1; i < lm; i++)
    4564             :   {
    4565         126 :     long m = vm[i], mp = m*p;
    4566         126 :     GEN Cm = (m % p) == 0? C : NULL;
    4567        1260 :     for (j = 1; j < l; j++)
    4568             :     {
    4569        1134 :       GEN S = gel(B,j), s = gel(S, mp + 1);
    4570        1134 :       if (Cm) s = gadd(s, gmul(C, gel(S, m/p + 1)));
    4571        1134 :       gcoeff(Q, i, j) = s;
    4572             :     }
    4573             :   }
    4574          21 :   return gerepileupto(av, Minv_RgM_mul(Minv,Q));
    4575             : }
    4576             : /* Matrix of T(p), p prime, dim(mf) > 0 and integral weight */
    4577             : static GEN
    4578         182 : mfheckemat_p(GEN mf, long p)
    4579             : {
    4580         182 :   pari_sp av = avma;
    4581         182 :   long N = MF_get_N(mf), sb = mfsturm_mf(mf);
    4582         182 :   GEN B = (N % p)? mfcoefs_mf(mf, sb * p, 1): mfcoefs_mf(mf, sb, p);
    4583         182 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, hecke_data(N,p)));
    4584             : }
    4585             : 
    4586             : /* mf_NEW != (0), weight > 1, p prime. Use
    4587             :  * T(p) T(j) = T(j*p) + p^{k-1} \chi(p) 1_{p | j, p \nmid N} T(j/p) */
    4588             : static GEN
    4589         875 : mfnewmathecke_p(GEN mf, long p)
    4590             : {
    4591         875 :   pari_sp av = avma;
    4592         875 :   GEN tf, vj = MFnew_get_vj(mf), CHI = MF_get_CHI(mf);
    4593         875 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4594         875 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4595         875 :   long i, j, lvj = lg(vj), lim = vj[lvj-1] * p;
    4596         875 :   GEN M, perm, V, need = zero_zv(lim);
    4597         875 :   GEN C = (N % p)? gmul(mfchareval(CHI,p), powuu(p,k-1)): NULL;
    4598         875 :   tf = mftraceform_new(N, k, CHI);
    4599        3759 :   for (i = 1; i < lvj; i++)
    4600             :   {
    4601        2884 :     j = vj[i]; need[j*p] = 1;
    4602        2884 :     if (N % p && j % p == 0) need[j/p] = 1;
    4603             :   }
    4604         875 :   perm = zero_zv(lim);
    4605         875 :   V = cgetg(lim+1, t_VEC);
    4606       12047 :   for (i = j = 1; i <= lim; i++)
    4607       11172 :     if (need[i]) { gel(V,j) = mfhecke_i(i, N, tf); perm[i] = j; j++; }
    4608         875 :   setlg(V, j);
    4609         875 :   V = bhnmat_extend_nocache(NULL, N, mfsturm_mf(mf), 1, V);
    4610         875 :   V = rowpermute(V, Mindex); /* V[perm[i]] = coeffs(T_i newtrace) */
    4611         875 :   M = cgetg(lvj, t_MAT);
    4612        3759 :   for (i = 1; i < lvj; i++)
    4613             :   {
    4614             :     GEN t;
    4615        2884 :     j = vj[i]; t = gel(V, perm[j*p]);
    4616        2884 :     if (C && j % p == 0) t = RgC_add(t, RgC_Rg_mul(gel(V, perm[j/p]),C));
    4617        2884 :     gel(M,i) = t;
    4618             :   }
    4619         875 :   return gerepileupto(av, Minv_RgM_mul(Minv, M));
    4620             : }
    4621             : 
    4622             : GEN
    4623          77 : mfheckemat(GEN mf, GEN vn)
    4624             : {
    4625          77 :   pari_sp av = avma;
    4626          77 :   long lv, lvP, i, N, dim, nk, dk, p, sb, flint = (typ(vn)==t_INT);
    4627             :   GEN CHI, res, vT, FA, B, vP;
    4628             : 
    4629          77 :   mf = checkMF(mf);
    4630          77 :   if (typ(vn) != t_VECSMALL) vn = gtovecsmall(vn);
    4631          77 :   N = MF_get_N(mf); CHI = MF_get_CHI(mf); Qtoss(MF_get_gk(mf), &nk, &dk);
    4632          77 :   dim = MF_get_dim(mf);
    4633          77 :   lv = lg(vn);
    4634          77 :   res = cgetg(lv, t_VEC);
    4635          77 :   FA = cgetg(lv, t_VEC);
    4636          77 :   vP = cgetg(lv, t_VEC);
    4637          77 :   vT = const_vec(vecsmall_max(vn), NULL);
    4638         182 :   for (i = 1; i < lv; i++)
    4639             :   {
    4640         105 :     ulong n = (ulong)labs(vn[i]);
    4641             :     GEN fa;
    4642         105 :     if (!n) pari_err_TYPE("mfheckemat", vn);
    4643         105 :     if (dk == 1 || uissquareall(n, &n)) fa = myfactoru(n);
    4644           0 :     else { n = 0; fa = myfactoru(1); } /* dummy: T_{vn[i]} = 0 */
    4645         105 :     vn[i] = n;
    4646         105 :     gel(FA,i) = fa;
    4647         105 :     gel(vP,i) = gel(fa,1);
    4648             :   }
    4649          77 :   vP = shallowconcat1(vP); vecsmall_sort(vP);
    4650          77 :   vP = vecsmall_uniq_sorted(vP); /* all primes occurring in vn */
    4651          77 :   lvP = lg(vP); if (lvP == 1) goto END;
    4652          56 :   p = vP[lvP-1];
    4653          56 :   sb = mfsturm_mf(mf);
    4654          56 :   if (dk == 1 && nk != 1 && MF_get_space(mf) == mf_NEW)
    4655          21 :     B = NULL; /* special purpose mfnewmathecke_p is faster */
    4656          35 :   else if (lvP == 2 && N % p == 0)
    4657          21 :     B = mfcoefs_mf(mf, sb, dk==2? p*p: p); /* single prime | N, can optimize */
    4658             :   else
    4659          14 :     B = mfcoefs_mf(mf, sb * (dk==2? p*p: p), 1); /* general initialization */
    4660         126 :   for (i = 1; i < lvP; i++)
    4661             :   {
    4662          70 :     long j, l, q, e = 1;
    4663             :     GEN C, Tp, u1, u0;
    4664          70 :     p = vP[i];
    4665         189 :     for (j = 1; j < lv; j++) e = maxss(e, z_lval(vn[j], p));
    4666          70 :     if (!B)
    4667          28 :       Tp = mfnewmathecke_p(mf, p);
    4668          42 :     else if (dk == 2)
    4669           7 :       Tp = mfheckemat_mfcoefs_p2(mf,p, (lvP==2||N%p)? B: matdeflate(sb,p*p,B));
    4670             :     else
    4671          35 :       Tp = mfheckemat_mfcoefs_p(mf, p, (lvP==2||N%p)? B: matdeflate(sb,p,B));
    4672          70 :     gel(vT, p) = Tp;
    4673          70 :     if (e == 1) continue;
    4674          14 :     u0 = gen_1;
    4675          14 :     if (dk == 2)
    4676             :     {
    4677           0 :       C = N % p? gmul(mfchareval(CHI,p*p), powuu(p, nk-2)): NULL;
    4678           0 :       if (e == 2) u0 = sstoQ(p+1,p); /* special case T_{p^4} */
    4679             :     }
    4680             :     else
    4681          14 :       C = N % p? gmul(mfchareval(CHI,p),   powuu(p, nk-1)): NULL;
    4682          28 :     for (u1=Tp, q=p, l=2; l <= e; l++)
    4683             :     { /* u0 = T_{p^{l-2}}, u1 = T_{p^{l-1}} for l > 2 */
    4684          14 :       GEN v = gmul(Tp, u1);
    4685          14 :       if (C) v = gsub(v, gmul(C, u0));
    4686             :       /* q = p^l, vT[q] = T_q for k integer else T_{q^2} */
    4687          14 :       q *= p; u0 = u1; gel(vT, q) = u1 = v;
    4688             :     }
    4689             :   }
    4690          56 : END:
    4691             :   /* vT[p^e] = T_{p^e} for all p^e occurring below */
    4692         182 :   for (i = 1; i < lv; i++)
    4693             :   {
    4694         105 :     long n = vn[i], j, lP;
    4695             :     GEN fa, P, E, M;
    4696         105 :     if (n == 0) { gel(res,i) = zeromat(dim,dim); continue; }
    4697         105 :     if (n == 1) { gel(res,i) = matid(dim); continue; }
    4698          77 :     fa = gel(FA,i);
    4699          77 :     P = gel(fa,1); lP = lg(P);
    4700          77 :     E = gel(fa,2); M = gel(vT, upowuu(P[1], E[1]));
    4701          84 :     for (j = 2; j < lP; j++) M = RgM_mul(M, gel(vT, upowuu(P[j], E[j])));
    4702          77 :     gel(res,i) = M;
    4703             :   }
    4704          77 :   if (flint) res = gel(res,1);
    4705          77 :   return gerepilecopy(av, res);
    4706             : }
    4707             : 
    4708             : /* f = \sum_i v[i] T_listj[i] (Trace Form) attached to v; replace by f/a_1(f) */
    4709             : static GEN
    4710        1323 : mf_normalize(GEN mf, GEN v)
    4711             : {
    4712        1323 :   GEN c, dc = NULL, M = MF_get_M(mf), Mindex = MF_get_Mindex(mf);
    4713        1323 :   v = Q_primpart(v);
    4714        1323 :   c = RgMrow_RgC_mul(M, v, 2); /* a_1(f) */
    4715        1323 :   if (gequal1(c)) return v;
    4716         791 :   if (typ(c) == t_POL) c = gmodulo(c, mfcharpol(MF_get_CHI(mf)));
    4717         791 :   if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1 && degpol(gel(c,1)) >= 40
    4718           7 :                          && Mindex[1] == 2
    4719           7 :                          && mfcharorder(MF_get_CHI(mf)) <= 2)
    4720           7 :   { /* normalize using expansion at infinity (small coefficients) */
    4721           7 :     GEN w, P = gel(c,1), a1 = gel(c,2);
    4722           7 :     long i, l = lg(Mindex);
    4723           7 :     w = cgetg(l, t_COL);
    4724           7 :     gel(w,1) = gen_1;
    4725         280 :     for (i = 2; i < l; i++)
    4726             :     {
    4727         273 :       c = liftpol_shallow(RgMrow_RgC_mul(M, v, Mindex[i]));
    4728         273 :       gel(w,i) = QXQ_div(c, a1, P);
    4729             :     }
    4730             :     /* w = expansion at oo of normalized form */
    4731           7 :     v = Minv_RgC_mul(MF_get_Minv(mf), Q_remove_denom(w, &dc));
    4732           7 :     v = gmodulo(v, P); /* back to mfbasis coefficients */
    4733             :   }
    4734             :   else
    4735             :   {
    4736         784 :     c = ginv(c);
    4737         784 :     if (typ(c) == t_POLMOD) c = Q_remove_denom(c, &dc);
    4738         784 :     v = RgC_Rg_mul(v, c);
    4739             :   }
    4740         791 :   if (dc) v = RgC_Rg_div(v, dc);
    4741         791 :   return v;
    4742             : }
    4743             : static void
    4744         343 : pol_red(GEN NF, GEN *pP, GEN *pa, long flag)
    4745             : {
    4746         343 :   GEN dP, a, P = *pP;
    4747         343 :   long d = degpol(P);
    4748             : 
    4749         343 :   *pa = a = pol_x(varn(P));
    4750         343 :   if (d > 30) return;
    4751             : 
    4752         336 :   dP = RgX_disc(P);
    4753         336 :   if (typ(dP) != t_INT)
    4754          84 :   { dP = gnorm(dP); if (typ(dP) != t_INT) pari_err_BUG("mfnewsplit"); }
    4755         336 :   if (d == 2 || expi(dP) < 62)
    4756             :   {
    4757         308 :     if (expi(dP) < 31)
    4758         308 :       P = NF? rnfpolredabs(NF, P,flag): polredabs0(P,flag);
    4759             :     else
    4760           0 :       P = NF? rnfpolredbest(NF,P,flag): polredbest(P,flag);
    4761         308 :     if (flag)
    4762             :     {
    4763         280 :       a = gel(P,2); if (typ(a) == t_POLMOD) a = gel(a,2);
    4764         280 :       P = gel(P,1);
    4765             :     }
    4766             :   }
    4767         336 :   *pP = P;
    4768         336 :   *pa = a;
    4769             : }
    4770             : 
    4771             : /* Diagonalize and normalize. See mfsplit for meaning of flag. */
    4772             : static GEN
    4773         966 : mfspclean(GEN mf, GEN mf0, GEN NF, long ord, GEN simplesp, long flag)
    4774             : {
    4775         966 :   const long vz = 1;
    4776         966 :   long i, l = lg(simplesp), dim = MF_get_dim(mf);
    4777         966 :   GEN res = cgetg(l, t_MAT), pols = cgetg(l, t_VEC);
    4778         966 :   GEN zeros = (mf == mf0)? NULL: zerocol(dim - MF_get_dim(mf0));
    4779        2317 :   for (i = 1; i < l; i++)
    4780             :   {
    4781        1351 :     GEN ATP = gel(simplesp, i), A = gel(ATP,1), P = gel(ATP,3);
    4782        1351 :     long d = degpol(P);
    4783        1351 :     GEN a, v = (flag && d > flag)? NULL: gel(A,1);
    4784        1351 :     if (d == 1) P = pol_x(vz);
    4785             :     else
    4786             :     {
    4787         343 :       pol_red(NF, &P, &a, !!v);
    4788         343 :       if (v)
    4789             :       { /* Mod(a,P) root of charpoly(T), K*gpowers(a) = eigenvector of T */
    4790         315 :         GEN K, den, M = cgetg(d+1, t_MAT), T = gel(ATP,2);
    4791             :         long j;
    4792         315 :         T = shallowtrans(T);
    4793         315 :         gel(M,1) = vec_ei(d,1); /* basis of cyclic vectors */
    4794        1113 :         for (j = 2; j <= d; j++) gel(M,j) = RgM_RgC_mul(T, gel(M,j-1));
    4795         315 :         M = Q_primpart(M);
    4796         112 :         K = NF? ZabM_inv(liftpol_shallow(M), nf_get_pol(NF), ord, &den)
    4797         315 :               : ZM_inv(M,&den);
    4798         315 :         K = shallowtrans(K);
    4799         315 :         v = gequalX(a)? pol_x_powers(d, vz): RgXQ_powers(a, d-1, P);
    4800         315 :         v = gmodulo(RgM_RgC_mul(A, RgM_RgC_mul(K,v)), P);
    4801             :       }
    4802             :     }
    4803        1351 :     if (v)
    4804             :     {
    4805        1323 :       v = mf_normalize(mf0, v); if (zeros) v = shallowconcat(zeros,v);
    4806        1323 :       gel(res,i) = v; if (flag) setlg(res,i+1);
    4807             :     }
    4808             :     else
    4809          28 :       gel(res,i) = zerocol(dim);
    4810        1351 :     gel(pols,i) = P;
    4811             :   }
    4812         966 :   return mkvec2(res, pols);
    4813             : }
    4814             : 
    4815             : /* return v = v_{X-r}(P), and set Z = P / (X-r)^v */
    4816             : static long
    4817          70 : RgX_valrem_root(GEN P, GEN r, GEN *Z)
    4818             : {
    4819             :   long v;
    4820         140 :   for (v = 0; degpol(P); v++)
    4821             :   {
    4822         140 :     GEN t, Q = RgX_div_by_X_x(P, r, &t);
    4823         140 :     if (!gequal0(t)) break;
    4824          70 :     P = Q;
    4825             :   }
    4826          70 :   *Z = P; return v;
    4827             : }
    4828             : static GEN
    4829        1071 : mynffactor(GEN NF, GEN P, long dimlim)
    4830             : {
    4831             :   long i, l, v;
    4832             :   GEN R, E;
    4833        1071 :   if (dimlim != 1)
    4834             :   {
    4835         511 :     R = NF? nffactor(NF, P): QX_factor(P);
    4836         511 :     if (!dimlim) return R;
    4837          21 :     E = gel(R,2);
    4838          21 :     R = gel(R,1); l = lg(R);
    4839          98 :     for (i = 1; i < l; i++)
    4840          91 :       if (degpol(gel(R,i)) > dimlim) break;
    4841          21 :     if (i == 1) return NULL;
    4842          21 :     setlg(E,i);
    4843          21 :     setlg(R,i); return mkmat2(R, E);
    4844             :   }
    4845             :   /* dimlim = 1 */
    4846         560 :   R = nfroots(NF, P); l = lg(R);
    4847         560 :   if (l == 1) return NULL;
    4848         497 :   v = varn(P);
    4849         497 :   settyp(R, t_COL);
    4850         497 :   if (degpol(P) == l-1)
    4851         441 :     E = const_col(l-1, gen_1);
    4852             :   else
    4853             :   {
    4854          56 :     E = cgetg(l, t_COL);
    4855         126 :     for (i = 1; i < l; i++) gel(E,i) = utoi(RgX_valrem_root(P, gel(R,i), &P));
    4856             :   }
    4857         497 :   R = deg1_from_roots(R, v);
    4858         497 :   return mkmat2(R, E);
    4859             : }
    4860             : 
    4861             : /* Let K be a number field attached to NF (Q if NF = NULL). A K-vector
    4862             :  * space of dimension d > 0 is given by a t_MAT A (n x d, full column rank)
    4863             :  * giving a K-basis, X a section (d x n: left pseudo-inverse of A). Return a
    4864             :  * pair (T, fa), where T is an element of the Hecke algebra (a sum of Tp taken
    4865             :  * from vector vTp) acting on A (a d x d t_MAT) and fa is the factorization of
    4866             :  * its characteristic polynomial, limited to factors of degree <= dimlim if
    4867             :  * dimlim != 0 (return NULL if there are no factors of degree <= dimlim) */
    4868             : static GEN
    4869        1064 : findbestsplit(GEN NF, GEN vTp, GEN A, GEN X, long dimlim, long vz)
    4870             : {
    4871        1064 :   GEN T = NULL, Tkeep = NULL, fakeep = NULL;
    4872        1064 :   long lmax = 0, i, lT = lg(vTp);
    4873        1148 :   for (i = 1; i < lT; i++)
    4874             :   {
    4875        1148 :     GEN D, P, E, fa, TpA = gel(vTp,i);
    4876             :     long l;
    4877        2072 :     if (typ(TpA) == t_INT) break;
    4878        1071 :     if (lg(TpA) > lg(A)) TpA = RgM_mul(X, RgM_mul(TpA, A)); /* Tp | A */
    4879        1071 :     T = T ? RgM_add(T, TpA) : TpA;
    4880        1071 :     if (!NF) { P = QM_charpoly_ZX(T); setvarn(P, vz); }
    4881             :     else
    4882             :     {
    4883         203 :       P = charpoly(Q_remove_denom(T, &D), vz);
    4884         203 :       if (D) P = gdiv(RgX_unscale(P, D), powiu(D, degpol(P)));
    4885             :     }
    4886        1071 :     fa = mynffactor(NF, P, dimlim);
    4887        1071 :     if (!fa) return NULL;
    4888        1008 :     E = gel(fa, 2);
    4889             :     /* characteristic polynomial is separable ? */
    4890        1008 :     if (isint1(vecmax(E))) { Tkeep = T; fakeep = fa; break; }
    4891          84 :     l = lg(E);
    4892             :     /* characteristic polynomial has more factors than before ? */
    4893          84 :     if (l > lmax) { lmax = l; Tkeep = T; fakeep = fa; }
    4894             :   }
    4895        1001 :   return mkvec2(Tkeep, fakeep);
    4896             : }
    4897             : 
    4898             : static GEN
    4899         161 : nfcontent(GEN nf, GEN v)
    4900             : {
    4901         161 :   long i, l = lg(v);
    4902         161 :   GEN c = gel(v,1);
    4903         777 :   for (i = 2; i < l; i++) c = idealadd(nf, c, gel(v,i));
    4904         161 :   if (typ(c) == t_MAT && gequal1(gcoeff(c,1,1))) c = gen_1;
    4905         161 :   return c;
    4906             : }
    4907             : static GEN
    4908         252 : nf_primpart(GEN nf, GEN B)
    4909             : {
    4910         252 :   switch(typ(B))
    4911             :   {
    4912         161 :     case t_COL:
    4913             :     {
    4914         161 :       GEN A = matalgtobasis(nf, B), c = nfcontent(nf, A);
    4915         161 :       if (typ(c) == t_INT) return B;
    4916          21 :       c = idealred_elt(nf,c);
    4917          21 :       A = Q_primpart( nfC_nf_mul(nf, A, Q_primpart(nfinv(nf,c))) );
    4918          21 :       A = liftpol_shallow( matbasistoalg(nf, A) );
    4919          21 :       if (gexpo(A) > gexpo(B)) A = B;
    4920          21 :       return A;
    4921             :     }
    4922          91 :     case t_MAT:
    4923             :     {
    4924             :       long i, l;
    4925          91 :       GEN A = cgetg_copy(B, &l);
    4926         252 :       for (i = 1; i < l; i++) gel(A,i) = nf_primpart(nf, gel(B,i));
    4927          91 :       return A;
    4928             :     }
    4929           0 :     default:
    4930           0 :       pari_err_TYPE("nf_primpart", B);
    4931             :       return NULL; /*LCOV_EXCL_LINE*/
    4932             :   }
    4933             : }
    4934             : 
    4935             : /* rotate entries of v to accomodate new entry 'x' (push out oldest entry) */
    4936             : static void
    4937        1029 : vecpush(GEN v, GEN x)
    4938             : {
    4939             :   long i;
    4940        5145 :   for (i = lg(v)-1; i > 1; i--) gel(v,i) = gel(v,i-1);
    4941        1029 :   gel(v,1) = x;
    4942        1029 : }
    4943             : 
    4944             : /* sort t_VEC of vector spaces by increasing dimension */
    4945             : static GEN
    4946         966 : sort_by_dim(GEN v)
    4947             : {
    4948         966 :   long i, l = lg(v);
    4949         966 :   GEN D = cgetg(l, t_VECSMALL);
    4950        2317 :   for (i = 1; i < l; i++) D[i] = lg(gmael(v,i,2));
    4951         966 :   return vecpermute(v , vecsmall_indexsort(D));
    4952             : }
    4953             : static GEN
    4954         966 : split_starting_space(GEN mf)
    4955             : {
    4956         966 :   long d = MF_get_dim(mf), d2;
    4957         966 :   GEN id = matid(d);
    4958         966 :   switch(MF_get_space(mf))
    4959             :   {
    4960         959 :     case mf_NEW:
    4961         959 :     case mf_CUSP: return mkvec2(id, id);
    4962             :   }
    4963           7 :   d2 = lg(MF_get_S(mf))-1;
    4964           7 :   return mkvec2(vecslice(id, d-d2+1,d),
    4965             :                 shallowconcat(zeromat(d2,d-d2),matid(d2)));
    4966             : }
    4967             : /* If dimlim > 0, keep only the dimension <= dimlim eigenspaces.
    4968             :  * See mfsplit for the meaning of flag. */
    4969             : static GEN
    4970        1365 : split_ii(GEN mf, long dimlim, long flag, long *pnewd)
    4971             : {
    4972             :   forprime_t iter;
    4973        1365 :   GEN CHI = MF_get_CHI(mf), empty = cgetg(1, t_VEC), mf0 = mf;
    4974             :   GEN NF, POLCYC, todosp, Tpbigvec, simplesp;
    4975        1365 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4976        1365 :   long ord, FC, NEWT, dimsimple = 0, newd = -1;
    4977        1365 :   const long NBH = 5, vz = 1;
    4978             :   ulong p;
    4979             : 
    4980        1365 :   switch(MF_get_space(mf))
    4981             :   {
    4982        1162 :     case mf_NEW: break;
    4983         196 :     case mf_CUSP:
    4984             :     case mf_FULL:
    4985         196 :       if (k > 1) { mf0 = mfinittonew(mf); break; }
    4986         175 :       newd = lg(MF_get_S(mf))-1 - mfolddim(N, k, CHI);
    4987         175 :       break;
    4988           7 :     default: pari_err_TYPE("mfsplit [space does not contain newspace]", mf);
    4989             :       return NULL; /*LCOV_EXCL_LINE*/
    4990             :   }
    4991        1358 :   if (newd < 0) newd = mf0? MF_get_dim(mf0): 0;
    4992        1358 :   *pnewd = newd;
    4993        1358 :   if (!newd) return mkvec2(cgetg(1, t_MAT), empty);
    4994             : 
    4995         966 :   NEWT = (k > 1 && MF_get_space(mf0) == mf_NEW);
    4996         966 :   todosp = mkvec( split_starting_space(mf0) );
    4997         966 :   simplesp = empty;
    4998         966 :   FC = mfcharconductor(CHI);
    4999         966 :   ord = mfcharorder(CHI);
    5000         966 :   if (ord <= 2) NF = POLCYC = NULL;
    5001             :   else
    5002             :   {
    5003         161 :     POLCYC = mfcharpol(CHI);
    5004         161 :     NF = nfinit(POLCYC,DEFAULTPREC);
    5005             :   }
    5006         966 :   Tpbigvec = zerovec(NBH);
    5007         966 :   u_forprime_init(&iter, 2, ULONG_MAX);
    5008        1288 :   while (dimsimple < newd && (p = u_forprime_next(&iter)))
    5009             :   {
    5010             :     GEN nextsp;
    5011             :     long ind;
    5012        1274 :     if (N % (p*p) == 0 && N/p % FC == 0) continue; /* T_p = 0 in this case */
    5013        1029 :     vecpush(Tpbigvec, NEWT? mfnewmathecke_p(mf0,p): mfheckemat_p(mf0,p));
    5014        1029 :     nextsp = empty;
    5015        2093 :     for (ind = 1; ind < lg(todosp); ind++)
    5016             :     {
    5017        1064 :       GEN tmp = gel(todosp, ind), fa, P, E, D, Tp, DTp;
    5018        1064 :       GEN A = gel(tmp, 1);
    5019        1064 :       GEN X = gel(tmp, 2);
    5020             :       long lP, i;
    5021        1064 :       tmp = findbestsplit(NF, Tpbigvec, A, X, dimlim, vz);
    5022        1722 :       if (!tmp) continue; /* nothing there */
    5023        1001 :       Tp = gel(tmp, 1);
    5024        1001 :       fa = gel(tmp, 2);
    5025        1001 :       P = gel(fa, 1);
    5026        1001 :       E = gel(fa, 2); lP = lg(P);
    5027             :       /* lP > 1 */
    5028        1001 :       if (DEBUGLEVEL) err_printf("Exponents = %Ps\n", E);
    5029        1001 :       if (lP == 2)
    5030             :       {
    5031         707 :         GEN P1 = gel(P,1);
    5032         707 :         long e1 = itos(gel(E,1)), d1 = degpol(P1);
    5033         707 :         if (e1 * d1 == lg(Tp)-1)
    5034             :         {
    5035         658 :           if (e1 > 1) nextsp = vec_append(nextsp, mkvec2(A,X));
    5036             :           else
    5037             :           { /* simple module */
    5038         644 :             simplesp = vec_append(simplesp, mkvec3(A,Tp,P1));
    5039         644 :             dimsimple += d1;
    5040             :           }
    5041         658 :           continue;
    5042             :         }
    5043             :       }
    5044             :       /* Found splitting */
    5045         343 :       DTp = Q_remove_denom(Tp, &D);
    5046        1148 :       for (i = 1; i < lP; i++)
    5047             :       {
    5048         805 :         GEN Ai, Xi, dXi, AAi, v, y, Pi = gel(P,i);
    5049         805 :         Ai = RgX_RgM_eval(D? RgX_rescale(Pi,D): Pi, DTp);
    5050         805 :         Ai = QabM_ker(Ai, POLCYC, ord);
    5051         805 :         if (NF) Ai = nf_primpart(NF, Ai);
    5052             : 
    5053         805 :         AAi = RgM_mul(A, Ai);
    5054             :         /* gives section, works on nonsquare matrices */
    5055         805 :         Xi = QabM_pseudoinv(Ai, POLCYC, ord, &v, &dXi);
    5056         805 :         Xi = RgM_Rg_div(Xi, dXi);
    5057         805 :         y = gel(v,1);
    5058         805 :         if (isint1(gel(E,i)))
    5059             :         {
    5060         707 :           GEN Tpi = RgM_mul(Xi, RgM_mul(rowpermute(Tp,y), Ai));
    5061         707 :           simplesp = vec_append(simplesp, mkvec3(AAi, Tpi, Pi));
    5062         707 :           dimsimple += degpol(Pi);
    5063             :         }
    5064             :         else
    5065             :         {
    5066          98 :           Xi = RgM_mul(Xi, rowpermute(X,y));
    5067          98 :           nextsp = vec_append(nextsp, mkvec2(AAi, Xi));
    5068             :         }
    5069             :       }
    5070             :     }
    5071        1029 :     todosp = nextsp; if (lg(todosp) == 1) break;
    5072             :   }
    5073         966 :   if (DEBUGLEVEL) err_printf("end split, need to clean\n");
    5074         966 :   return mfspclean(mf, mf0, NF, ord, sort_by_dim(simplesp), flag);
    5075             : }
    5076             : static GEN
    5077          28 : dim_filter(GEN v, long dim)
    5078             : {
    5079          28 :   GEN P = gel(v,2);
    5080          28 :   long j, l = lg(P);
    5081         140 :   for (j = 1; j < l; j++)
    5082         126 :     if (degpol(gel(P,j)) > dim)
    5083             :     {
    5084          14 :       v = mkvec2(vecslice(gel(v,1),1,j-1), vecslice(P,1,j-1));
    5085          14 :       break;
    5086             :     }
    5087          28 :   return v;
    5088             : }
    5089             : static long
    5090         203 : dim_sum(GEN v)
    5091             : {
    5092         203 :   GEN P = gel(v,2);
    5093         203 :   long j, l = lg(P), d = 0;
    5094         490 :   for (j = 1; j < l; j++) d += degpol(gel(P,j));
    5095         203 :   return d;
    5096             : }
    5097             : static GEN
    5098        1295 : split_i(GEN mf, long dimlim, long flag)
    5099        1295 : { long junk; return split_ii(mf, dimlim, flag, &junk); }
    5100             : /* mf is either already split or output by mfinit. Splitting is done only for
    5101             :  * newspace except in weight 1. If flag = 0 (default) split completely.
    5102             :  * If flag = d > 0, only give the Galois polynomials in degree > d
    5103             :  * Flag is ignored if dimlim = 1. */
    5104             : GEN
    5105          98 : mfsplit(GEN mf0, long dimlim, long flag)
    5106             : {
    5107          98 :   pari_sp av = avma;
    5108          98 :   GEN v, mf = checkMF_i(mf0);
    5109          98 :   if (!mf) pari_err_TYPE("mfsplit", mf0);
    5110          98 :   if ((v = obj_check(mf, MF_SPLIT)))
    5111          28 :   { if (dimlim) v = dim_filter(v, dimlim); }
    5112          70 :   else if (dimlim && (v = obj_check(mf, MF_SPLITN)))
    5113          21 :   { v = (itos(gel(v,1)) >= dimlim)? dim_filter(gel(v,2), dimlim): NULL; }
    5114          98 :   if (!v)
    5115             :   {
    5116             :     long newd;
    5117          70 :     v = split_ii(mf, dimlim, flag, &newd);
    5118          70 :     if (lg(v) == 1) obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5119          70 :     else if (!flag)
    5120             :     {
    5121          49 :       if (dim_sum(v) == newd) obj_insert(mf, MF_SPLIT,v);
    5122          21 :       else obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5123             :     }
    5124             :   }
    5125          98 :   return gerepilecopy(av, v);
    5126             : }
    5127             : static GEN
    5128         385 : split(GEN mf) { return split_i(mf,0,0); }
    5129             : GEN
    5130         735 : MF_get_newforms(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),1); }
    5131             : GEN
    5132         560 : MF_get_fields(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),2); }
    5133             : 
    5134             : /*************************************************************************/
    5135             : /*                     Modular forms of Weight 1                         */
    5136             : /*************************************************************************/
    5137             : /* S_1(G_0(N)), small N. Return 1 if definitely empty; return 0 if maybe
    5138             :  * non-empty  */
    5139             : static int
    5140       16142 : wt1empty(long N)
    5141             : {
    5142       16142 :   if (N <= 100) switch (N)
    5143             :   { /* non-empty [32/100] */
    5144        5446 :     case 23: case 31: case 39: case 44: case 46:
    5145             :     case 47: case 52: case 55: case 56: case 57:
    5146             :     case 59: case 62: case 63: case 68: case 69:
    5147             :     case 71: case 72: case 76: case 77: case 78:
    5148             :     case 79: case 80: case 83: case 84: case 87:
    5149             :     case 88: case 92: case 93: case 94: case 95:
    5150        5446 :     case 99: case 100: return 0;
    5151        3542 :     default: return 1;
    5152             :   }
    5153        7154 :   if (N <= 600) switch(N)
    5154             :   { /* empty [111/500] */
    5155         336 :     case 101: case 102: case 105: case 106: case 109:
    5156             :     case 113: case 121: case 122: case 123: case 125:
    5157             :     case 130: case 134: case 137: case 146: case 149:
    5158             :     case 150: case 153: case 157: case 162: case 163:
    5159             :     case 169: case 170: case 173: case 178: case 181:
    5160             :     case 182: case 185: case 187: case 193: case 194:
    5161             :     case 197: case 202: case 205: case 210: case 218:
    5162             :     case 221: case 226: case 233: case 241: case 242:
    5163             :     case 245: case 246: case 250: case 257: case 265:
    5164             :     case 267: case 269: case 274: case 277: case 281:
    5165             :     case 289: case 293: case 298: case 305: case 306:
    5166             :     case 313: case 314: case 317: case 326: case 337:
    5167             :     case 338: case 346: case 349: case 353: case 361:
    5168             :     case 362: case 365: case 369: case 370: case 373:
    5169             :     case 374: case 377: case 386: case 389: case 394:
    5170             :     case 397: case 401: case 409: case 410: case 421:
    5171             :     case 425: case 427: case 433: case 442: case 449:
    5172             :     case 457: case 461: case 466: case 481: case 482:
    5173             :     case 485: case 490: case 493: case 509: case 514:
    5174             :     case 521: case 530: case 533: case 534: case 538:
    5175             :     case 541: case 545: case 554: case 557: case 562:
    5176             :     case 565: case 569: case 577: case 578: case 586:
    5177         336 :     case 593: return 1;
    5178        6804 :     default: return 0;
    5179             :   }
    5180          14 :   return 0;
    5181             : }
    5182             : 
    5183             : static GEN
    5184          28 : initwt1trace(GEN mf)
    5185             : {
    5186          28 :   GEN S = MF_get_S(mf), v, H;
    5187             :   long l, i;
    5188          28 :   if (lg(S) == 1) return mftrivial();
    5189          28 :   H = mfheckemat(mf, Mindex_as_coef(mf));
    5190          28 :   l = lg(H); v = cgetg(l, t_VEC);
    5191          63 :   for (i = 1; i < l; i++) gel(v,i) = gtrace(gel(H,i));
    5192          28 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5193          28 :   return mflineardiv_linear(S, v, 1);
    5194             : }
    5195             : static GEN
    5196          21 : initwt1newtrace(GEN mf)
    5197             : {
    5198          21 :   GEN v, D, S, Mindex, CHI = MF_get_CHI(mf);
    5199          21 :   long FC, lD, i, sb, N1, N2, lM, N = MF_get_N(mf);
    5200          21 :   CHI = mfchartoprimitive(CHI, &FC);
    5201          21 :   if (N % FC || mfcharparity(CHI) == 1) return mftrivial();
    5202          21 :   D = mydivisorsu(N/FC); lD = lg(D);
    5203          21 :   S = MF_get_S(mf);
    5204          21 :   if (lg(S) == 1) return mftrivial();
    5205          21 :   N2 = newd_params2(N);
    5206          21 :   N1 = N / N2;
    5207          21 :   Mindex = MF_get_Mindex(mf);
    5208          21 :   lM = lg(Mindex);
    5209          21 :   sb = Mindex[lM-1];
    5210          21 :   v = zerovec(sb+1);
    5211          42 :   for (i = 1; i < lD; i++)
    5212             :   {
    5213          21 :     long M = FC*D[i], j;
    5214          21 :     GEN tf = initwt1trace(M == N? mf: mfinit_Nkchi(M, 1, CHI, mf_CUSP, 0));
    5215             :     GEN listd, w;
    5216          21 :     if (mf_get_type(tf) == t_MF_CONST) continue;
    5217          21 :     w = mfcoefs_i(tf, sb, 1);
    5218          21 :     if (M == N) { v = gadd(v, w); continue; }
    5219           0 :     listd = mydivisorsu(u_ppo(ugcd(N/M, N1), FC));
    5220           0 :     for (j = 1; j < lg(listd); j++)
    5221             :     {
    5222           0 :       long d = listd[j], d2 = d*d; /* coprime to FC */
    5223           0 :       GEN dk = mfchareval(CHI, d);
    5224           0 :       long NMd = N/(M*d), m;
    5225           0 :       for (m = 1; m <= sb/d2; m++)
    5226             :       {
    5227           0 :         long be = mubeta2(NMd, m);
    5228           0 :         if (be)
    5229             :         {
    5230           0 :           GEN c = gmul(dk, gmulsg(be, gel(w, m+1)));
    5231           0 :           long n = m*d2;
    5232           0 :           gel(v, n+1) = gadd(gel(v, n+1), c);
    5233             :         }
    5234             :       }
    5235             :     }
    5236             :   }
    5237          21 :   if (gequal0(gel(v,2))) return mftrivial();
    5238          21 :   v = vecpermute(v,Mindex);
    5239          21 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5240          21 :   return mflineardiv_linear(S, v, 1);
    5241             : }
    5242             : 
    5243             : /* Matrix of T(p), p \nmid N */
    5244             : static GEN
    5245         196 : Tpmat(long p, long lim, GEN CHI)
    5246             : {
    5247         196 :   GEN M = zeromatcopy(lim, p*lim), chip = mfchareval(CHI, p); /* != 0 */
    5248             :   long i, j, pi, pj;
    5249         196 :   gcoeff(M, 1, 1) = gaddsg(1, chip);
    5250        4585 :   for (i = 1, pi = p; i < lim; i++,  pi += p) gcoeff(M, i+1, pi+1) = gen_1;
    5251        1869 :   for (j = 1, pj = p; pj < lim; j++, pj += p) gcoeff(M, pj+1, j+1) = chip;
    5252         196 :   return M;
    5253             : }
    5254             : 
    5255             : /* assume !wt1empty(N), in particular N>25 */
    5256             : /* Returns [[lim,p], mf (weight 2), p*lim x dim matrix] */
    5257             : static GEN
    5258        1799 : mfwt1_pre(long N)
    5259             : {
    5260        1799 :   GEN M, mf = mfinit_Nkchi(N, 2, mfchartrivial(), mf_CUSP, 0);
    5261             :   /*not empty for N>25*/
    5262             :   long p, lim;
    5263        1799 :   if (uisprime(N))
    5264             :   {
    5265         392 :     p = 2; /*N>25 is not 2 */
    5266         392 :     lim = ceilA1(N, 3);
    5267             :   }
    5268             :   else
    5269             :   {
    5270             :     forprime_t S;
    5271        1407 :     u_forprime_init(&S, 2, N);
    5272        2527 :     while ((p = u_forprime_next(&S)))
    5273        2527 :       if (N % p) break;
    5274        1407 :     lim = mfsturm_mf(mf) + 1;
    5275             :   }
    5276             :   /* p = smalllest prime not dividing N */
    5277        1799 :   M = bhnmat_extend_nocache(MF_get_M(mf), N, p*lim-1, 1, MF_get_S(mf));
    5278        1799 :   return mkvec3(mkvecsmall2(lim, p), mf, M);
    5279             : }
    5280             : 
    5281             : /* lg(A) > 1, E a t_POL */
    5282             : static GEN
    5283        1176 : mfmatsermul(GEN A, GEN E)
    5284             : {
    5285        1176 :   long j, l = lg(A), r = nbrows(A);
    5286        1176 :   GEN M = cgetg(l, t_MAT);
    5287        1176 :   E = RgXn_red_shallow(E, r+1);
    5288       14182 :   for (j = 1; j < l; j++)
    5289             :   {
    5290       13006 :     GEN c = RgV_to_RgX(gel(A,j), 0);
    5291       13006 :     gel(M, j) = RgX_to_RgC(RgXn_mul(c, E, r+1), r);
    5292             :   }
    5293        1176 :   return M;
    5294             : }
    5295             : /* lg(Ap) > 1, Ep an Flxn */
    5296             : static GEN
    5297         728 : mfmatsermul_Fl(GEN Ap, GEN Ep, ulong p)
    5298             : {
    5299         728 :   long j, l = lg(Ap), r = nbrows(Ap);
    5300         728 :   GEN M = cgetg(l, t_MAT);
    5301        9590 :   for (j = 1; j < l; j++)
    5302             :   {
    5303        8862 :     GEN c = Flv_to_Flx(gel(Ap,j), 0);
    5304        8862 :     gel(M,j) = Flx_to_Flv(Flxn_mul(c, Ep, r+1, p), r);
    5305             :   }
    5306         728 :   return M;
    5307             : }
    5308             : 
    5309             : /* CHI mod F | N, return mfchar of modulus N.
    5310             :  * FIXME: wasteful, G should be precomputed  */
    5311             : static GEN
    5312       12264 : mfcharinduce(GEN CHI, long N)
    5313             : {
    5314             :   GEN G, chi;
    5315       12264 :   if (mfcharmodulus(CHI) == N) return CHI;
    5316        1141 :   G = znstar0(utoipos(N), 1);
    5317        1141 :   chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    5318        1141 :   CHI = leafcopy(CHI);
    5319        1141 :   gel(CHI,1) = G;
    5320        1141 :   gel(CHI,2) = chi; return CHI;
    5321             : }
    5322             : 
    5323             : static GEN
    5324        3983 : gmfcharno(GEN CHI)
    5325             : {
    5326        3983 :   GEN G = gel(CHI,1), chi = gel(CHI,2);
    5327        3983 :   return mkintmod(znconreyexp(G, chi), znstar_get_N(G));
    5328             : }
    5329             : static long
    5330       12838 : mfcharno(GEN CHI)
    5331             : {
    5332       12838 :   GEN n = znconreyexp(gel(CHI,1), gel(CHI,2));
    5333       12838 :   return itou(n);
    5334             : }
    5335             : 
    5336             : /* return k such that minimal mfcharacter in Galois orbit of CHI is CHI^k */
    5337             : static long
    5338       11354 : mfconreyminimize(GEN CHI)
    5339             : {
    5340       11354 :   GEN G = gel(CHI,1), cyc, chi;
    5341       11354 :   cyc = ZV_to_zv(znstar_get_cyc(G));
    5342       11354 :   chi = ZV_to_zv(znconreychar(G, gel(CHI,2)));
    5343       11354 :   return zv_cyc_minimize(cyc, chi, coprimes_zv(mfcharorder(CHI)));
    5344             : }
    5345             : 
    5346             : /* find scalar c such that first non-0 entry of c*v is 1; return c*v
    5347             :  * (set c = NULL for 1) */
    5348             : static GEN
    5349        1701 : RgV_normalize(GEN v, GEN *pc)
    5350             : {
    5351        1701 :   long i, l = lg(v);
    5352        1701 :   *pc = NULL;
    5353        3948 :   for (i = 1; i < l; i++)
    5354             :   {
    5355        3948 :     GEN c = gel(v,i);
    5356        3948 :     if (!gequal0(c))
    5357             :     {
    5358        1701 :       if (gequal1(c)) { *pc = gen_1; return v; }
    5359         595 :       *pc = ginv(c); return RgV_Rg_mul(v, *pc);
    5360             :     }
    5361             :   }
    5362           0 :   return v;
    5363             : }
    5364             : static GEN
    5365        2289 : mftreatdihedral(GEN DIH, GEN POLCYC, long ordchi, long biglim, GEN *pS)
    5366             : {
    5367             :   GEN M, Minv, C;
    5368             :   long l, i;
    5369        2289 :   l = lg(DIH); if (l == 1) return NULL;
    5370        2289 :   if (!pS) return DIH;
    5371         728 :   C = cgetg(l, t_VEC);
    5372         728 :   M = cgetg(l, t_MAT);
    5373        2044 :   for (i = 1; i < l; i++)
    5374             :   {
    5375        1316 :     GEN c, v = mfcoefs_i(gel(DIH,i), biglim, 1);
    5376        1316 :     gel(M,i) = RgV_normalize(v, &c);
    5377        1316 :     gel(C,i) = Rg_col_ei(c, l-1, i);
    5378             :   }
    5379         728 :   Minv = gel(mfclean(M,POLCYC,ordchi,0),2);
    5380         728 :   M = RgM_Minv_mul(M, Minv);
    5381         728 :   C = RgM_Minv_mul(C, Minv);
    5382         728 :   *pS = vecmflinear(DIH, C);
    5383         728 :   return M;
    5384             : }
    5385             : 
    5386             : static GEN
    5387         189 : mfstabiter(GEN *pVC, GEN M, GEN A2, GEN E1inv, long lim, GEN P, long ordchi)
    5388             : {
    5389             :   GEN A, VC, con;
    5390         189 :   E1inv = primitive_part(E1inv, &con);
    5391         189 :   VC = con? ginv(con): gen_1;
    5392         189 :   A = mfmatsermul(A2, E1inv);
    5393             :   for(;;)
    5394         105 :   {
    5395         294 :     GEN R = shallowconcat(RgM_mul(M,A), rowslice(A,1,lim));
    5396         294 :     GEN B = QabM_ker(R, P, ordchi);
    5397         294 :     long lA = lg(A), lB = lg(B);
    5398         294 :     if (lB == 1) return NULL;
    5399         294 :     if (lB == lA) { *pVC = gmul(*pVC, VC); return A; }
    5400         105 :     B = rowslice(B, 1, lA-1);
    5401         105 :     if (ordchi > 2) B = gmodulo(B, P);
    5402         105 :     A = Q_primitive_part(RgM_mul(A,B), &con);
    5403         105 :     VC = gmul(VC,B); /* first VC is a scalar, then a RgM */
    5404         105 :     if (con) VC = RgM_Rg_div(VC, con);
    5405             :   }
    5406             : }
    5407             : static long
    5408         189 : mfstabitermodp(GEN Mp, GEN Ap, long p, long lim)
    5409             : {
    5410         189 :   GEN VC = NULL;
    5411             :   while (1)
    5412          21 :   {
    5413         210 :     GEN Rp = shallowconcat(Flm_mul(Mp,Ap,p), rowslice(Ap,1,lim));
    5414         210 :     GEN Bp = Flm_ker(Rp, p);
    5415         210 :     long lA = lg(Ap), lB = lg(Bp);
    5416         210 :     if (lB == 1) return 0;
    5417         210 :     if (lB == lA) return lA-1;
    5418          21 :     Bp = rowslice(Bp, 1, lA-1);
    5419          21 :     Ap = Flm_mul(Ap, Bp, p);
    5420          21 :     VC = VC? Flm_mul(VC, Bp, p): Bp;
    5421             :   }
    5422             : }
    5423             : 
    5424             : static GEN
    5425         350 : mfintereis(GEN *pVC, GEN A, GEN M2, GEN y, GEN den, GEN E2, GEN P, long ordchi)
    5426             : {
    5427         350 :   GEN z, M1 = mfmatsermul(A,E2), M1den = isint1(den)? M1: RgM_Rg_mul(M1,den);
    5428         350 :   M2 = RgM_mul(M2, rowpermute(M1, y));
    5429         350 :   z = QabM_ker(RgM_sub(M2,M1den), P, ordchi);
    5430         350 :   if (lg(z) == 1) return NULL;
    5431         350 :   if (ordchi > 2) z = gmodulo(z, P);
    5432         350 :   *pVC = typ(*pVC) == t_INT? z: RgM_mul(*pVC, z);
    5433         350 :   return RgM_mul(A,z);
    5434             : }
    5435             : static GEN
    5436         357 : mfintereismodp(GEN *pVC, GEN A, GEN M2, GEN E2, long dih, ulong p)
    5437             : {
    5438         357 :   GEN M1 = mfmatsermul_Fl(A, E2, p), z;
    5439         357 :   long j, lx = lg(A);
    5440         357 :   z = Flm_ker(shallowconcat(M1, M2), p);
    5441         357 :   j = lg(z) - 1; if (j == dih) return NULL;
    5442        1533 :   for (; j; j--) setlg(z[j], lx);
    5443         350 :   *pVC = *pVC? Flm_mul(*pVC, z, p): z;
    5444         350 :   return Flm_mul(A,z,p);
    5445             : }
    5446             : 
    5447             : static GEN
    5448         196 : mfcharinv_i(GEN CHI)
    5449             : {
    5450         196 :   GEN G = gel(CHI,1);
    5451         196 :   CHI = leafcopy(CHI); gel(CHI,2) =  zncharconj(G, gel(CHI,2)); return CHI;
    5452             : }
    5453             : 
    5454             : /* upper bound dim S_1(Gamma_0(N),chi) performing the linear algebra mod p */
    5455             : static long
    5456         196 : mfwt1dimmodp(GEN A, GEN ES, GEN M, long ordchi, long dih, long lim)
    5457             : {
    5458             :   GEN Ap, ES1p, VC;
    5459         196 :   ulong p, r = QabM_init(ordchi, &p);
    5460             : 
    5461         196 :   Ap = QabM_to_Flm(A, r, p);
    5462         196 :   VC = NULL;
    5463         196 :   ES1p = QabX_to_Flx(gel(ES,1), r, p);
    5464         196 :   if (lg(ES) >= 3)
    5465             :   {
    5466         182 :     GEN M2 = mfmatsermul_Fl(Ap, ES1p, p);
    5467         182 :     pari_sp av = avma;
    5468             :     long i;
    5469         532 :     for (i = 2; i < lg(ES); i++)
    5470             :     {
    5471         357 :       GEN ESip = QabX_to_Flx(gel(ES,i), r, p);
    5472         357 :       Ap = mfintereismodp(&VC, Ap, M2, ESip, dih, p);
    5473         357 :       if (!Ap) return dih;
    5474         350 :       gerepileall(av, 2, &Ap,&VC);
    5475             :     }
    5476             :   }
    5477             :   /* intersection of Eisenstein series quotients non empty: use Schaeffer */
    5478         189 :   Ap = mfmatsermul_Fl(Ap, Flxn_inv(ES1p,nbrows(Ap),p), p);
    5479         189 :   return mfstabitermodp(QabM_to_Flm(M,r,p), Ap, p, lim);
    5480             : }
    5481             : 
    5482             : /* Compute the full S_1(\G_0(N),\chi). If pS is NULL, only the dimension
    5483             :  * dim, in the form of a vector having dim components. Otherwise output
    5484             :  * a basis: ptvf contains a pointer to the vector of forms, and the
    5485             :  * program returns the corresponding matrix of Fourier expansions.
    5486             :  * ptdimdih gives the dimension of the subspace generated by dihedral forms;
    5487             :  * TMP is from mfwt1_pre or NULL. */
    5488             : static GEN
    5489       10815 : mfwt1basis(long N, GEN CHI, GEN TMP, GEN *pS, long *ptdimdih)
    5490             : {
    5491             :   GEN ES, E, mf, A, M, Tp, den, VC, C, POLCYC, ES1, ES1INV, DIH, a0, a0i;
    5492             :   long plim, lim, biglim, i, p, dA, dimp, ordchi, dih;
    5493             : 
    5494       10815 :   if (ptdimdih) *ptdimdih = 0;
    5495       10815 :   if (pS) *pS = NULL;
    5496       10815 :   if (wt1empty(N) || mfcharparity(CHI) != -1) return NULL;
    5497       10528 :   ordchi = mfcharorder(CHI);
    5498       10528 :   if (uisprime(N) && ordchi > 4) return NULL;
    5499       10500 :   if (!pS)
    5500             :   {
    5501        7042 :     dih = mfdihedralcuspdim(N, CHI);
    5502        7042 :     DIH = zerovec(dih);
    5503             :   }
    5504             :   else
    5505             :   {
    5506        3458 :     DIH = mfdihedralcusp(N, CHI);
    5507        3458 :     dih = lg(DIH) - 1;
    5508             :   }
    5509       10500 :   POLCYC = (ordchi <= 2)? NULL: mfcharpol(CHI);
    5510       10500 :   if (ptdimdih) *ptdimdih = dih;
    5511       10500 :   biglim = mfsturmNk(N, 2);
    5512       10500 :   if (N <= 600) switch(N)
    5513             :   {
    5514             :     long m;
    5515          14 :     case 219: case 273: case 283: case 331: case 333: case 344: case 416:
    5516             :     case 438: case 468: case 491: case 504: case 546: case 553: case 563:
    5517             :     case 566: case 581: case 592:
    5518          14 :       break; /* one chi with both exotic and dihedral forms */
    5519        9436 :     default: /* only dihedral forms */
    5520        9436 :       if (!dih) return NULL;
    5521             :       /* fall through */
    5522             :     case 124: case 133: case 148: case 171: case 201: case 209: case 224:
    5523             :     case 229: case 248: case 261: case 266: case 288: case 296: case 301:
    5524             :     case 309: case 325: case 342: case 371: case 372: case 380: case 399:
    5525             :     case 402: case 403: case 404: case 408: case 418: case 432: case 444:
    5526             :     case 448: case 451: case 453: case 458: case 496: case 497: case 513:
    5527             :     case 522: case 527: case 532: case 576: case 579:
    5528             :       /* no chi with both exotic and dihedral; one chi with exotic forms */
    5529        3192 :       if (dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5530         910 :       m = mfcharno(mfcharinduce(CHI,N));
    5531         910 :       if (N == 124 && (m != 67 && m != 87)) return NULL;
    5532         784 :       if (N == 133 && (m != 83 && m !=125)) return NULL;
    5533         490 :       if (N == 148 && (m !=105 && m !=117)) return NULL;
    5534         364 :       if (N == 171 && (m != 94 && m !=151)) return NULL;
    5535         364 :       if (N == 201 && (m != 29 && m !=104)) return NULL;
    5536         364 :       if (N == 209 && (m != 87 && m !=197)) return NULL;
    5537         364 :       if (N == 224 && (m != 95 && m !=191)) return NULL;
    5538         364 :       if (N == 229 && (m !=107 && m !=122)) return NULL;
    5539         364 :       if (N == 248 && (m != 87 && m !=191)) return NULL;
    5540         273 :       if (N == 261 && (m != 46 && m !=244)) return NULL;
    5541         273 :       if (N == 266 && (m != 83 && m !=125)) return NULL;
    5542         273 :       if (N == 288 && (m != 31 && m !=223)) return NULL;
    5543         273 :       if (N == 296 && (m !=105 && m !=265)) return NULL;
    5544             :   }
    5545         196 :   if (!TMP) TMP = mfwt1_pre(N);
    5546         196 :   lim = gel(TMP,1)[1]; p = gel(TMP,1)[2]; plim = p*lim;
    5547         196 :   mf  = gel(TMP,2);
    5548         196 :   A   = gel(TMP,3); /* p*lim x dim matrix */
    5549         196 :   E = mfeisensteinbasis(N, 1, mfcharinv_i(CHI));
    5550         196 :   ES = RgM_to_RgXV(mfvectomat(E, plim+1, 1), 0);
    5551         196 :   Tp = Tpmat(p, lim, CHI);
    5552         196 :   dimp = mfwt1dimmodp(A, ES, Tp, ordchi, dih, lim);
    5553         196 :   if (!dimp) return NULL;
    5554         196 :   if (dimp == dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5555         189 :   VC = gen_1; ES1 = gel(ES,1); /* does not vanish at oo */
    5556         189 :   if (lg(ES) > 2)
    5557             :   {
    5558             :     pari_sp btop;
    5559         175 :     GEN Ar = rowslice(A, 1, (3*lim)/2 + 1), M2 = mfmatsermul(Ar, ES1);
    5560             :     GEN v, y, M2M2I, M2I;
    5561         175 :     M2I = QabM_pseudoinv(M2, POLCYC, ordchi, &v, &den);
    5562         175 :     M2M2I = RgM_mul(M2,M2I);
    5563         175 :     y = gel(v,1); btop = avma;
    5564         525 :     for (i = 2; i < lg(ES); i++)
    5565             :     {
    5566         350 :       Ar = mfintereis(&VC, Ar, M2M2I, y, den, gel(ES,i), POLCYC,ordchi);
    5567         350 :       if (!Ar) return NULL;
    5568         350 :       if (gc_needed(btop, 1))
    5569             :       {
    5570           0 :         if (DEBUGMEM > 1) pari_warn(warnmem,"mfwt1basis i = %ld", i);
    5571           0 :         gerepileall(btop, 2, &Ar, &VC);
    5572             :       }
    5573             :     }
    5574         175 :     A = RgM_mul(A, vecslice(VC,1, lg(Ar)-1));
    5575             :   }
    5576         189 :   a0 = gel(ES1,2); /* non-zero */
    5577         189 :   if (gequal1(a0)) a0 = a0i = NULL;
    5578             :   else
    5579             :   {
    5580         189 :     a0i = ginv(a0);
    5581         189 :     ES1 = RgX_Rg_mul(RgX_unscale(ES1,a0), a0i);
    5582             :   }
    5583         189 :   ES1INV = RgXn_inv(ES1, plim-1);
    5584         189 :   if (a0) ES1INV = RgX_Rg_mul(RgX_unscale(ES1INV, a0i), a0i);
    5585         189 :   A = mfstabiter(&VC, Tp, A, ES1INV, lim, POLCYC, ordchi);
    5586         189 :   if (!A) return NULL;
    5587         189 :   dA = lg(A);
    5588         189 :   C = cgetg(dA, t_VEC);
    5589         189 :   M = cgetg(dA, t_MAT);
    5590         574 :   for (i = 1; i < dA; i++)
    5591             :   {
    5592         385 :     GEN c, v = gel(A,i);
    5593         385 :     gel(M,i) = RgV_normalize(v, &c);
    5594         385 :     gel(C,i) = RgC_Rg_mul(gel(VC,i), c);
    5595             :   }
    5596         189 :   if (pS)
    5597             :   {
    5598         140 :     GEN Minv = gel(mfclean(M, POLCYC, ordchi, 0), 2);
    5599         140 :     M = RgM_Minv_mul(M, Minv);
    5600         140 :     C = RgM_Minv_mul(C, Minv);
    5601         140 :     *pS = vecmflineardiv0(MF_get_S(mf), C, gel(E,1));
    5602             :   }
    5603         189 :   return M;
    5604             : }
    5605             : 
    5606             : static void
    5607         322 : MF_set_space(GEN mf, long x) { gmael(mf,1,4) = utoi(x); }
    5608             : static GEN
    5609         168 : mfwt1_cusptonew(GEN mf)
    5610             : {
    5611         168 :   const long vy = 1;
    5612         168 :   GEN vP, F, S, Snew, vF, v = split(mf);
    5613             :   long i, lP, dSnew, ct;
    5614             : 
    5615         168 :   F = gel(v,1);
    5616         168 :   vP= gel(v,2); lP = lg(vP);
    5617         168 :   if (lP == 1) { obj_insert(mf, MF_SPLIT, v); return NULL; }
    5618         154 :   MF_set_space(mf, mf_NEW);
    5619         154 :   S = MF_get_S(mf);
    5620         154 :   dSnew = dim_sum(v);
    5621         154 :   Snew = cgetg(dSnew + 1, t_VEC); ct = 0;
    5622         154 :   vF = cgetg(lP, t_MAT);
    5623         329 :   for (i = 1; i < lP; i++)
    5624             :   {
    5625         175 :     GEN V, P = gel(vP,i), f = liftpol_shallow(gel(F,i));
    5626         175 :     long j, d = degpol(P);
    5627         175 :     gel(vF,i) = V = zerocol(dSnew);
    5628         175 :     if (d == 1)
    5629             :     {
    5630          84 :       gel(Snew, ct+1) = mflineardiv_linear(S, f, 0);
    5631          84 :       gel(V, ct+1) = gen_1;
    5632             :     }
    5633             :     else
    5634             :     {
    5635          91 :       f = RgXV_to_RgM(f,d);
    5636         280 :       for (j = 1; j <= d; j++)
    5637             :       {
    5638         189 :         gel(Snew, ct+j) = mflineardiv_linear(S, row(f,j), 0);
    5639         189 :         gel(V, ct+j) = mkpolmod(pol_xn(j-1,vy), P);
    5640             :       }
    5641             :     }
    5642         175 :     ct += d;
    5643             :   }
    5644         154 :   obj_insert(mf, MF_SPLIT, mkvec2(vF, vP));
    5645         154 :   gel(mf,3) = Snew; return mf;
    5646             : }
    5647             : static GEN
    5648        3570 : mfwt1init(long N, GEN CHI, GEN TMP, long space, long flraw)
    5649             : {
    5650        3570 :   GEN mf, mf1, S, M = mfwt1basis(N, CHI, TMP, &S, NULL);
    5651        3570 :   if (!M) return NULL;
    5652         868 :   mf1 = mkvec4(stoi(N), gen_1, CHI, utoi(mf_CUSP));
    5653         868 :   mf = mkmf(mf1, cgetg(1,t_VEC), S, gen_0, NULL);
    5654         868 :   if (space == mf_NEW)
    5655             :   {
    5656         168 :     gel(mf,5) = mfcleanCHI(M,CHI, 0);
    5657         168 :     mf = mfwt1_cusptonew(mf); if (!mf) return NULL;
    5658         154 :     if (!flraw) M = mfcoefs_mf(mf, mfsturmNk(N,1)+1, 1);
    5659             :   }
    5660         854 :   gel(mf,5) = flraw? zerovec(3): mfcleanCHI(M, CHI, 0);
    5661         854 :   return mf;
    5662             : }
    5663             : 
    5664             : static GEN
    5665         973 : mfEMPTY(GEN mf1)
    5666             : {
    5667         973 :   GEN Minv = mkMinv(cgetg(1,t_MAT), NULL,NULL,NULL);
    5668         973 :   GEN M = mkvec3(cgetg(1,t_VECSMALL), Minv, cgetg(1,t_MAT));
    5669         973 :   return mkmf(mf1, cgetg(1,t_VEC), cgetg(1,t_VEC), cgetg(1,t_VEC), M);
    5670             : }
    5671             : static GEN
    5672         616 : mfEMPTYall(long N, GEN gk, GEN vCHI, long space)
    5673             : {
    5674             :   long i, l;
    5675             :   GEN v, gN, gs;
    5676         616 :   if (!vCHI) return cgetg(1, t_VEC);
    5677          14 :   gN = utoipos(N); gs = utoi(space);
    5678          14 :   l = lg(vCHI); v = cgetg(l, t_VEC);
    5679          42 :   for (i = 1; i < l; i++) gel(v,i) = mfEMPTY(mkvec4(gN,gk,gel(vCHI,i),gs));
    5680          14 :   return v;
    5681             : }
    5682             : 
    5683             : static GEN
    5684        3983 : fmt_dim(GEN CHI, long d, long dih)
    5685        3983 : { return mkvec4(gmfcharorder(CHI), gmfcharno(CHI), utoi(d), stoi(dih)); }
    5686             : /* merge two vector of fmt_dim's for the same vector of characters. If CHI
    5687             :  * is not NULL, remove dim-0 spaces and add character from CHI */
    5688             : static GEN
    5689           7 : merge_dims(GEN V, GEN W, GEN CHI)
    5690             : {
    5691           7 :   long i, j, id, l = lg(V);
    5692           7 :   GEN A = cgetg(l, t_VEC);
    5693           7 :   if (l == 1) return A;
    5694           7 :   id = CHI? 1: 3;
    5695          21 :   for (i = j = 1; i < l; i++)
    5696             :   {
    5697          14 :     GEN v = gel(V,i), w = gel(W,i);
    5698          14 :     long dv = itou(gel(v,id)), dvh = itou(gel(v,id+1)), d;
    5699          14 :     long dw = itou(gel(w,id)), dwh = itou(gel(w,id+1));
    5700          14 :     d = dv + dw;
    5701          14 :     if (d || CHI)
    5702          14 :       gel(A,j++) = CHI? fmt_dim(gel(CHI,i),d, dvh+dwh)
    5703          14 :                       : mkvec2s(d,dvh+dwh);
    5704             :   }
    5705           7 :   setlg(A, j); return A;
    5706             : }
    5707             : static GEN
    5708        3010 : mfdim0all(GEN w)
    5709             : {
    5710        3038 :   if (w) retconst_vec(lg(w)-1, zerovec(2));
    5711        3003 :   return cgetg(1,t_VEC);
    5712             : }
    5713             : static long
    5714        7245 : mfwt1cuspdim_i(long N, GEN CHI, GEN TMP, long *dih)
    5715             : {
    5716        7245 :   pari_sp av = avma;
    5717        7245 :   GEN b = mfwt1basis(N, CHI, TMP, NULL, dih);
    5718        7245 :   return gc_long(av, b? lg(b)-1: 0);
    5719             : }
    5720             : static long
    5721         406 : mfwt1cuspdim(long N, GEN CHI) { return mfwt1cuspdim_i(N, CHI, NULL, NULL); }
    5722             : static GEN
    5723        4144 : mfwt1cuspdimall(long N, GEN vCHI)
    5724             : {
    5725             :   GEN z, TMP, w;
    5726             :   long i, j, l;
    5727        4144 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5728        1141 :   w = mfwt1chars(N,vCHI);
    5729        1141 :   l = lg(w); if (l == 1) return cgetg(1,t_VEC);
    5730        1141 :   z = cgetg(l, t_VEC);
    5731        1141 :   TMP = mfwt1_pre(N);
    5732        7861 :   for (i = j = 1; i < l; i++)
    5733             :   {
    5734        6720 :     GEN CHI = gel(w,i);
    5735        6720 :     long dih, d = mfwt1cuspdim_i(N, CHI, TMP, &dih);
    5736        6720 :     if (vCHI)
    5737          42 :       gel(z,j++) = mkvec2s(d, dih);
    5738        6678 :     else if (d)
    5739        1428 :       gel(z,j++) = fmt_dim(CHI, d, dih);
    5740             :   }
    5741        1141 :   setlg(z,j); return z;
    5742             : }
    5743             : 
    5744             : /* dimension of S_1(Gamma_1(N)) */
    5745             : static long
    5746        4123 : mfwt1cuspdimsum(long N)
    5747             : {
    5748        4123 :   pari_sp av = avma;
    5749        4123 :   GEN v = mfwt1cuspdimall(N, NULL);
    5750        4123 :   long i, ct = 0, l = lg(v);
    5751        5544 :   for (i = 1; i < l; i++)
    5752             :   {
    5753        1421 :     GEN w = gel(v,i); /* [ord(CHI),*,dim,*] */
    5754        1421 :     ct += itou(gel(w,3))*myeulerphiu(itou(gel(w,1)));
    5755             :   }
    5756        4123 :   return gc_long(av,ct);
    5757             : }
    5758             : 
    5759             : static GEN
    5760          56 : mfwt1newdimall(long N, GEN vCHI)
    5761             : {
    5762             :   GEN z, w, vTMP, fa, P, E;
    5763             :   long i, c, l, lw, P1;
    5764          56 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5765          56 :   w = mfwt1chars(N,vCHI);
    5766          56 :   lw = lg(w); if (lw == 1) return cgetg(1,t_VEC);
    5767          56 :   vTMP = const_vec(N, NULL);
    5768          56 :   gel(vTMP,N) = mfwt1_pre(N);
    5769             :   /* if p || N and p \nmid F(CHI), S_1^new(G0(N),chi) = 0 */
    5770          56 :   fa = znstar_get_faN(gmael(w,1,1));
    5771          56 :   P = gel(fa,1); l = lg(P);
    5772          56 :   E = gel(fa,2);
    5773         154 :   for (i = P1 = 1; i < l; i++)
    5774          98 :     if (E[i] == 1) P1 *= itou(gel(P,i));
    5775             :   /* P1 = \prod_{v_p(N) = 1} p */
    5776          56 :   z = cgetg(lw, t_VEC);
    5777         182 :   for (i = c = 1; i < lw; i++)
    5778             :   {
    5779             :     long S, j, l, F, dihnew;
    5780         126 :     GEN D, CHI = gel(w,i), CHIP = mfchartoprimitive(CHI,&F);
    5781             : 
    5782         126 :     S = F % P1? 0: mfwt1cuspdim_i(N, CHI, gel(vTMP,N), &dihnew);
    5783         126 :     if (!S)
    5784             :     {
    5785          56 :       if (vCHI) gel(z, c++) = zerovec(2);
    5786          56 :       continue;
    5787             :     }
    5788          70 :     D = mydivisorsu(N/F); l = lg(D);
    5789          77 :     for (j = l-2; j > 0; j--) /* skip last M = N */
    5790             :     {
    5791           7 :       long M = D[j]*F, m, s, dih;
    5792           7 :       GEN TMP = gel(vTMP,M);
    5793           7 :       if (wt1empty(M) || !(m = mubeta(D[l-j]))) continue; /*m = mubeta(N/M)*/
    5794           7 :       if (!TMP) gel(vTMP,M) = TMP = mfwt1_pre(M);
    5795           7 :       s = mfwt1cuspdim_i(M, CHIP, TMP, &dih);
    5796           7 :       if (s) { S += m * s; dihnew += m * dih; }
    5797             :     }
    5798          70 :     if (vCHI)
    5799          63 :       gel(z,c++) = mkvec2s(S, dihnew);
    5800           7 :     else if (S)
    5801           7 :       gel(z, c++) = fmt_dim(CHI, S, dihnew);
    5802             :   }
    5803          56 :   setlg(z,c); return z;
    5804             : }
    5805             : 
    5806             : static GEN
    5807          28 : mfwt1olddimall(long N, GEN vCHI)
    5808             : {
    5809             :   long i, j, l;
    5810             :   GEN z, w;
    5811          28 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5812          28 :   w = mfwt1chars(N,vCHI);
    5813          28 :   l = lg(w); z = cgetg(l, t_VEC);
    5814          84 :   for (i = j = 1; i < l; i++)
    5815             :   {
    5816          56 :     GEN CHI = gel(w,i);
    5817          56 :     long d = mfolddim(N, 1, CHI);
    5818          56 :     if (vCHI)
    5819          28 :       gel(z,j++) = mkvec2s(d,d?-1:0);
    5820          28 :     else if (d)
    5821           7 :       gel(z, j++) = fmt_dim(CHI, d, -1);
    5822             :   }
    5823          28 :   setlg(z,j); return z;
    5824             : }
    5825             : 
    5826             : static long
    5827         469 : mfwt1olddimsum(long N)
    5828             : {
    5829             :   GEN D;
    5830         469 :   long N2, i, l, S = 0;
    5831         469 :   newd_params(N, &N2); /* will ensure mubeta != 0 */
    5832         469 :   D = mydivisorsu(N/N2); l = lg(D);
    5833        2485 :   for (i = 2; i < l; i++)
    5834             :   {
    5835        2016 :     long M = D[l-i]*N2, d = mfwt1cuspdimsum(M);
    5836        2016 :     if (d) S -= mubeta(D[i]) * d;
    5837             :   }
    5838         469 :   return S;
    5839             : }
    5840             : static long
    5841        1050 : mfwt1newdimsum(long N)
    5842             : {
    5843        1050 :   long S = mfwt1cuspdimsum(N);
    5844        1050 :   return S? S - mfwt1olddimsum(N): 0;
    5845             : }
    5846             : 
    5847             : /* return the automorphism of a degree-2 nf */
    5848             : static GEN
    5849        5712 : nf2_get_conj(GEN nf)
    5850             : {
    5851        5712 :   GEN pol = nf_get_pol(nf);
    5852        5712 :   return deg1pol_shallow(gen_m1, negi(gel(pol,3)), varn(pol));
    5853             : }
    5854             : 
    5855             : static long
    5856         210 : mfisdihedral(GEN vF, GEN DIH)
    5857             : {
    5858         210 :   GEN vG = gel(DIH,1), M = gel(DIH,2), v, G, bnr, w, gen, D, f, nf, con;
    5859             :   GEN f0, f0b, xin;
    5860             :   long i, l, e, j, L, n;
    5861         210 :   if (lg(M) == 1) return 0;
    5862          28 :   v = RgM_RgC_invimage(M, vF);
    5863          28 :   if (!v) return 0;
    5864          28 :   l = lg(v);
    5865          28 :   for (i = 1; i < l; i++)
    5866          28 :     if (!gequal0(gel(v,i))) break;
    5867          28 :   if (i == l) return 0;
    5868          28 :   G = gel(vG,i);
    5869          28 :   bnr = gel(G,2); D = cyc_get_expo(bnr_get_cyc(bnr));
    5870          28 :   w = gel(G,3);
    5871          28 :   f = bnr_get_mod(bnr);
    5872          28 :   nf = bnr_get_nf(bnr);
    5873          28 :   con = nf2_get_conj(nf);
    5874          28 :   f0 = gel(f,1); f0b = galoisapply(nf, con, f0);
    5875          28 :   xin = zv_to_ZV(gel(w,2)); /* xi(bnr.gen[i]) = e(xin[i] / D) */
    5876          28 :   if (!gequal(f0,f0b))
    5877             :   { /* finite part of conductor not ambiguous */
    5878          14 :     GEN a = idealmul(nf, f0, idealdivexact(nf, f0b, idealadd(nf, f0, f0b)));
    5879          14 :     GEN bnr0 = bnr, S;
    5880          14 :     bnr = Buchray(bnr_get_bnf(bnr), mkvec2(a, gel(f,2)), nf_INIT | nf_GEN);
    5881          14 :     S = bnrsurjection(bnr, bnr0);
    5882          14 :     xin = RgV_RgM_mul(xin, gel(S,1));
    5883             :     /* still xi(gen[i]) = e(xin[i] / D), for the new generators */
    5884             :   }
    5885          28 :   gen = bnr_get_gen(bnr); L = lg(gen);
    5886          42 :   for (j = 1, e = itou(D); j < L; j++)
    5887             :   {
    5888          35 :     GEN Ng = idealnorm(nf, gel(gen,j));
    5889          35 :     GEN a = shifti(gel(xin,j), 1); /* xi(g_j^2) = e(a/D) */
    5890          35 :     GEN b = FpV_dotproduct(xin, isprincipalray(bnr,Ng), D);
    5891          35 :     GEN m = Fp_sub(a, b, D); /* xi(g_j/\bar{g_j}) = e(m/D) */
    5892          35 :     e = ugcd(e, itou(m)); if (e == 1) break;
    5893             :   }
    5894          28 :   n = itou(D) / e;
    5895          28 :   return n == 1? 4: 2*n;
    5896             : }
    5897             : 
    5898             : static ulong
    5899         119 : myradicalu(ulong n) { return zv_prod(gel(myfactoru(n),1)); }
    5900             : 
    5901             : /* list of fundamental discriminants unramified outside N, with sign s
    5902             :  * [s = 0 => no sign condition] */
    5903             : static GEN
    5904         119 : mfunram(long N, long s)
    5905             : {
    5906         119 :   long cN = myradicalu(N >> vals(N)), p = 1, m = 1, l, c, i;
    5907         119 :   GEN D = mydivisorsu(cN), res;
    5908         119 :   l = lg(D);
    5909         119 :   if (s == 1) m = 0; else if (s == -1) p = 0;
    5910         119 :   res = cgetg(6*l - 5, t_VECSMALL);
    5911         119 :   c = 1;
    5912         119 :   if (!odd(N))
    5913             :   { /* d = 1 */
    5914          56 :     if (p) res[c++] = 8;
    5915          56 :     if (m) { res[c++] =-8; res[c++] =-4; }
    5916             :   }
    5917         364 :   for (i = 2; i < l; i++)
    5918             :   { /* skip d = 1, done above */
    5919         245 :     long d = D[i], d4 = d & 3L; /* d odd, squarefree, d4 = 1 or 3 */
    5920         245 :     if (d4 == 1) { if (p) res[c++] = d; }
    5921         182 :     else         { if (m) res[c++] =-d; }
    5922         245 :     if (!odd(N))
    5923             :     {
    5924          56 :       if (p) { res[c++] = 8*d; if (d4 == 3) res[c++] = 4*d; }
    5925          56 :       if (m) { res[c++] =-8*d; if (d4 == 1) res[c++] =-4*d; }
    5926             :     }
    5927             :   }
    5928         119 :   setlg(res, c); return res;
    5929             : }
    5930             : 
    5931             : /* Return 1 if F is definitely not S4 type; return 0 on failure. */
    5932             : static long
    5933         105 : mfisnotS4(long N, GEN w)
    5934             : {
    5935         105 :   GEN D = mfunram(N, 0);
    5936         105 :   long i, lD = lg(D), lw = lg(w);
    5937         616 :   for (i = 1; i < lD; i++)
    5938             :   {
    5939         511 :     long p, d = D[i], ok = 0;
    5940        1442 :     for (p = 2; p < lw; p++)
    5941        1442 :       if (w[p] && kross(d,p) == -1) { ok = 1; break; }
    5942         511 :     if (!ok) return 0;
    5943             :   }
    5944         105 :   return 1;
    5945             : }
    5946             : 
    5947             : /* Return 1 if Q(sqrt(5)) \not\subset Q(F), i.e. F is definitely not A5 type;
    5948             :  * return 0 on failure. */
    5949             : static long
    5950         105 : mfisnotA5(GEN F)
    5951             : {
    5952         105 :   GEN CHI = mf_get_CHI(F), P = mfcharpol(CHI), T, Q;
    5953             : 
    5954         105 :   if (mfcharorder(CHI) % 5 == 0) return 0;
    5955         105 :   T = mf_get_field(F); if (degpol(T) == 1) return 1;
    5956         105 :   if (degpol(P) > 1) T = rnfequation(P,T);
    5957         105 :   Q = gsubgs(pol_xn(2,varn(T)), 5);
    5958         105 :   return (typ(nfisincl(Q, T)) == t_INT);
    5959             : }
    5960             : 
    5961             : /* v[p+1]^2 / chi(p) - 2 = z + 1/z with z primitive root of unity of order n,
    5962             :  * return n */
    5963             : static long
    5964        6741 : mffindrootof1(GEN v, long p, GEN CHI)
    5965             : {
    5966        6741 :   GEN ap = gel(v,p+1), u0, u1, u1k, u2;
    5967        6741 :   long c = 1;
    5968        6741 :   if (gequal0(ap)) return 2;
    5969        5033 :   u0 = gen_2; u1k = u1 = gsubgs(gdiv(gsqr(ap), mfchareval(CHI, p)), 2);
    5970       14812 :   while (!gequalsg(2, liftpol_shallow(u1))) /* u1 = z^c + z^-c */
    5971             :   {
    5972        9779 :     u2 = gsub(gmul(u1k, u1), u0);
    5973        9779 :     u0 = u1; u1 = u2; c++;
    5974             :   }
    5975        5033 :   return c;
    5976             : }
    5977             : 
    5978             : /* we known that F is not dihedral */
    5979             : static long
    5980         182 : mfgaloistype_i(long N, GEN CHI, GEN F, GEN v)
    5981             : {
    5982             :   forprime_t iter;
    5983         182 :   long lim = lg(v)-2;
    5984         182 :   GEN w = zero_zv(lim);
    5985             :   pari_sp av;
    5986             :   ulong p;
    5987         182 :   u_forprime_init(&iter, 2, lim);
    5988         182 :   av = avma;
    5989        5292 :   while((p = u_forprime_next(&iter))) if (N%p) switch(mffindrootof1(v, p, CHI))
    5990             :   {
    5991        1400 :     case 1: case 2: continue;
    5992        3451 :     case 3: w[p] = 1; break;
    5993          70 :     case 4: return -24; /* S4 */
    5994           0 :     case 5: return -60; /* A5 */
    5995           7 :     default: pari_err_DOMAIN("mfgaloistype", "form", "not a",
    5996             :                              strtoGENstr("cuspidal eigenform"), F);
    5997           0 :     set_avma(av);
    5998             :   }
    5999         364 :   if (mfisnotS4(N,w) && mfisnotA5(F)) return -12; /* A4 */
    6000           0 :   return 0; /* FAILURE */
    6001             : }
    6002             : 
    6003             : static GEN
    6004         210 : mfgaloistype0(long N, GEN CHI, GEN F, GEN DIH, long lim)
    6005             : {
    6006         210 :   pari_sp av = avma;
    6007         210 :   GEN vF = mftocol(F, lim, 1);
    6008         210 :   long t = mfisdihedral(vF, DIH), bound;
    6009         210 :   if (t) { set_avma(av); return stoi(t); }
    6010         182 :   bound = maxss(200, 5*expu(N)*expu(N));
    6011             :   for(;;)
    6012             :   {
    6013         182 :     t = mfgaloistype_i(N, CHI, F, vF);
    6014         175 :     set_avma(av); if (t) return stoi(t);
    6015           0 :     if (lim > bound) return gen_0;
    6016           0 :     lim += lim >> 1;
    6017           0 :     vF = mfcoefs_i(F,lim,1);
    6018             :   }
    6019             : }
    6020             : 
    6021             : /* If f is NULL, give all the galoistypes, otherwise just for f */
    6022             : /* May return 0 as a type if failed to determine; in this case the type is
    6023             :  * either -12 or -60, most likely -12. FIXME using the Galois representation. */
    6024             : GEN
    6025         217 : mfgaloistype(GEN NK, GEN f)
    6026             : {
    6027         217 :   pari_sp av = avma;
    6028         217 :   GEN CHI, T, F, DIH, mf = checkMF_i(NK);
    6029             :   long N, k, lL, i, lim, SB;
    6030             : 
    6031         217 :   if (f && !checkmf_i(f)) pari_err_TYPE("mfgaloistype", f);
    6032         210 :   if (mf)
    6033             :   {
    6034         175 :     N = MF_get_N(mf);
    6035         175 :     k = MF_get_k(mf);
    6036         175 :     CHI = MF_get_CHI(mf);
    6037             :   }
    6038             :   else
    6039             :   {
    6040          35 :     checkNK(NK, &N, &k, &CHI, 0);
    6041          35 :     mf = f? NULL: mfinit_i(NK, mf_NEW);
    6042             :   }
    6043         210 :   if (k != 1) pari_err_DOMAIN("mfgaloistype", "k", "!=", gen_1, stoi(k));
    6044         210 :   SB = mf? mfsturm_mf(mf): mfsturmNk(N,1);
    6045         210 :   DIH = mfdihedralnew(N,CHI);
    6046         210 :   lim = lg(DIH) == 1? 200: SB;
    6047         210 :   DIH = mkvec2(DIH, mfvectomat(DIH,SB,1));
    6048         210 :   if (f) return gerepileuptoint(av, mfgaloistype0(N,CHI, f, DIH, lim));
    6049         112 :   F = mfeigenbasis(mf); lL = lg(F);
    6050         112 :   T = cgetg(lL, t_VEC);
    6051         224 :   for (i=1; i < lL; i++) gel(T,i) = mfgaloistype0(N, CHI, gel(F,i), DIH, lim);
    6052         112 :   return gerepileupto(av, T);
    6053             : }
    6054             : 
    6055             : /******************************************************************/
    6056             : /*                   Find all dihedral forms.                     */
    6057             : /******************************************************************/
    6058             : /* lim >= 2 */
    6059             : static void
    6060          14 : consttabdihedral(long lim)
    6061          14 : { cache_set(cache_DIH, mfdihedralall(mkvecsmall2(1,lim))); }
    6062             : 
    6063             : /* a ideal coprime to bnr modulus */
    6064             : static long
    6065       76356 : mfdiheval(GEN bnr, GEN w, GEN a)
    6066             : {
    6067       76356 :   GEN L, cycn = gel(w,1), chin = gel(w,2);
    6068       76356 :   long ordmax = cycn[1];
    6069       76356 :   L = ZV_to_Flv(isprincipalray(bnr,a), ordmax);
    6070       76356 :   return Flv_dotproduct(chin, L, ordmax);
    6071             : }
    6072             : 
    6073             : /* A(x^k) mod T */
    6074             : static GEN
    6075       32221 : Galois(GEN A, long k, GEN T)
    6076             : {
    6077       32221 :   if (typ(A) != t_POL) return A;
    6078       12061 :   return gmod(RgX_inflate(A, k), T);
    6079             : }
    6080             : static GEN
    6081         791 : vecGalois(GEN v, long k, GEN T)
    6082             : {
    6083             :   long i, l;
    6084         791 :   GEN w = cgetg_copy(v,&l);
    6085       33012 :   for (i = 1; i < l; i++) gel(w,i) = Galois(gel(v,i), k, T);
    6086         791 :   return w;
    6087             : }
    6088             : 
    6089             : static GEN
    6090      145733 : fix_pol(GEN S, GEN Pn, int *trace)
    6091             : {
    6092      145733 :   if (typ(S) != t_POL) return S;
    6093      102116 :   S = RgX_rem(S, Pn);
    6094      102116 :   if (typ(S) == t_POL)
    6095             :   {
    6096      102116 :     switch(lg(S))
    6097             :     {
    6098       34797 :       case 2: return gen_0;
    6099       16247 :       case 3: return gel(S,2);
    6100             :     }
    6101       51072 :     *trace = 1;
    6102             :   }
    6103       51072 :   return S;
    6104             : }
    6105             : 
    6106             : static GEN
    6107       10913 : dihan(GEN bnr, GEN w, GEN k0j, ulong lim)
    6108             : {
    6109       10913 :   GEN nf = bnr_get_nf(bnr), f = bid_get_ideal(bnr_get_bid(bnr));
    6110       10913 :   GEN v = zerovec(lim+1), cycn = gel(w,1), Tinit = gel(w,3);
    6111       10913 :   GEN Pn = gel(Tinit,lg(Tinit)==4? 2: 1);
    6112       10913 :   long j, ordmax = cycn[1], k0 = k0j[1], jdeg = k0j[2];
    6113       10913 :   long D = itos(nf_get_disc(nf)), vt = varn(Pn);
    6114       10913 :   int trace = 0;
    6115             :   ulong p, n;
    6116             :   forprime_t T;
    6117             : 
    6118       10913 :   if (!lim) return v;
    6119       10703 :   gel(v,2) = gen_1;
    6120       10703 :   u_forprime_init(&T, 2, lim);
    6121             :   /* fill in prime powers first */
    6122       80185 :   while ((p = u_forprime_next(&T)))
    6123             :   {
    6124             :     GEN vP, vchiP, S;
    6125             :     long k, lP;
    6126             :     ulong q, qk;
    6127       69482 :     if (kross(D,p) >= 0) q = p;
    6128       28196 :     else if (!(q = umuluu_le(p,p,lim))) continue;
    6129             :     /* q = Norm P */
    6130       46767 :     vP = idealprimedec(nf, utoipos(p));
    6131       46767 :     lP = lg(vP);
    6132       46767 :     vchiP = cgetg(lP, t_VECSMALL);
    6133      126994 :     for (j = k = 1; j < lP; j++)
    6134             :     {
    6135       80227 :       GEN P = gel(vP,j);
    6136       80227 :       if (!idealval(nf, f, P)) vchiP[k++] = mfdiheval(bnr,w,P);
    6137             :     }
    6138       46767 :     if (k == 1) continue;
    6139       45080 :     setlg(vchiP, k); lP = k;
    6140       45080 :     if (lP == 2)
    6141             :     { /* one prime above p not dividing f */
    6142       13804 :       long s, s0 = vchiP[1];
    6143       24157 :       for (qk=q, s = s0;; s = Fl_add(s,s0,ordmax))
    6144             :       {
    6145       24157 :         S = Qab_zeta(s, ordmax, vt);
    6146       24157 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6147       24157 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6148             :       }
    6149             :     }
    6150             :     else /* two primes above p not dividing f */
    6151             :     {
    6152       31276 :       long s, s0 = vchiP[1], s1 = vchiP[2];
    6153       31276 :       for (qk=q, k = 1;; k++)
    6154       13426 :       { /* sum over a,b s.t. Norm( P1^a P2^b ) = q^k, i.e. a+b = k */
    6155             :         long a;
    6156       44702 :         GEN S = gen_0;
    6157      154049 :         for (a = 0; a <= k; a++)
    6158             :         {
    6159      109347 :           s = Fl_add(Fl_mul(a, s0, ordmax), Fl_mul(k-a, s1, ordmax), ordmax);
    6160      109347 :           S = gadd(S, Qab_zeta(s, ordmax, vt));
    6161             :         }
    6162       44702 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6163       44702 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6164             :       }
    6165             :     }
    6166             :   }
    6167             :   /* complete with non-prime powers */
    6168      190232 :   for (n = 2; n <= lim; n++)
    6169             :   {
    6170      179529 :     GEN S, fa = myfactoru(n), P = gel(fa, 1), E = gel(fa, 2);
    6171             :     long q;
    6172      179529 :     if (lg(P) == 2) continue;
    6173             :     /* not a prime power */
    6174       76874 :     q = upowuu(P[1],E[1]);
    6175       76874 :     S = gmul(gel(v, q + 1), gel(v, n/q + 1));
    6176       76874 :     gel(v, n+1) = fix_pol(S, Pn, &trace);
    6177             :   }
    6178       10703 :   if (trace)
    6179             :   {
    6180        5558 :     v = QabV_tracerel(Tinit, jdeg, v);
    6181             :     /* Apply Galois Mod(k0, ordw) */
    6182        5558 :     if (k0 > 1) { GEN Pm = gel(Tinit,1); v = vecGalois(v, k0, Pm); }
    6183             :   }
    6184       10703 :   return v;
    6185             : }
    6186             : 
    6187             : /* as cyc_normalize for t_VECSMALL cyc */
    6188             : static GEN
    6189       26782 : cyc_normalize_zv(GEN cyc)
    6190             : {
    6191       26782 :   long i, o = cyc[1], l = lg(cyc); /* > 1 */
    6192       26782 :   GEN D = cgetg(l, t_VECSMALL);
    6193       31136 :   D[1] = o; for (i = 2; i < l; i++) D[i] = o / cyc[i];
    6194       26782 :   return D;
    6195             : }
    6196             : /* as char_normalize for t_VECSMALLs */
    6197             : static GEN
    6198      117950 : char_normalize_zv(GEN chi, GEN ncyc)
    6199             : {
    6200      117950 :   long i, l = lg(chi);
    6201      117950 :   GEN c = cgetg(l, t_VECSMALL);
    6202      117950 :   if (l > 1) {
    6203      117950 :     c[1] = chi[1];
    6204      159376 :     for (i = 2; i < l; i++) c[i] = chi[i] * ncyc[i];
    6205             :   }
    6206      117950 :   return c;
    6207             : }
    6208             : 
    6209             : static GEN
    6210        8946 : dihan_bnf(long D)
    6211             : {
    6212        8946 :   GEN c = getrand(), bnf;
    6213        8946 :   setrand(gen_1);
    6214        8946 :   bnf = Buchall(quadpoly(stoi(D)), 0, LOWDEFAULTPREC);
    6215        8946 :   setrand(c);
    6216        8946 :   return bnf;
    6217             : }
    6218             : static GEN
    6219       37233 : dihan_bnr(GEN bnf, GEN A)
    6220             : {
    6221       37233 :   GEN c = getrand(), bnr;
    6222       37233 :   setrand(gen_1);
    6223       37233 :   bnr = Buchray(bnf, A, nf_INIT|nf_GEN);
    6224       37233 :   setrand(c);
    6225       37233 :   return bnr;
    6226             : }
    6227             : /* Hecke xi * (D/.) = Dirichlet chi, return v in Q^r st chi(g_i) = e(v[i]).
    6228             :  * cycn = cyc_normalize_zv(bnr.cyc), chin = char_normalize_zv(chi,cyc) */
    6229             : static GEN
    6230       34412 : bnrchartwist2conrey(GEN chin, GEN cycn, GEN bnrconreyN, GEN kroconreyN)
    6231             : {
    6232       34412 :   long l = lg(bnrconreyN), c1 = cycn[1], i;
    6233       34412 :   GEN v = cgetg(l, t_COL);
    6234      125132 :   for (i = 1; i < l; i++)
    6235             :   {
    6236       90720 :     GEN d = sstoQ(zv_dotproduct(chin, gel(bnrconreyN,i)), c1);
    6237       90720 :     if (kroconreyN[i] < 0) d = gadd(d, ghalf);
    6238       90720 :     gel(v,i) = d;
    6239             :   }
    6240       34412 :   return v;
    6241             : }
    6242             : 
    6243             : /* chi(g_i) = e(v[i]) denormalize wrt Conrey generators orders */
    6244             : static GEN
    6245       34412 : conreydenormalize(GEN znN, GEN v)
    6246             : {
    6247       34412 :   GEN gcyc = znstar_get_conreycyc(znN), w;
    6248       34412 :   long l = lg(v), i;
    6249       34412 :   w = cgetg(l, t_COL);
    6250      125132 :   for (i = 1; i < l; i++)
    6251       90720 :     gel(w,i) = modii(gmul(gel(v,i), gel(gcyc,i)), gel(gcyc,i));
    6252       34412 :   return w;
    6253             : }
    6254             : 
    6255             : static long
    6256       83538 : Miyake(GEN vchi, GEN gb, GEN cycn)
    6257             : {
    6258       83538 :   long i, e = cycn[1], lb = lg(gb);
    6259       83538 :   GEN v = char_normalize_zv(vchi, cycn);
    6260      124264 :   for (i = 1; i < lb; i++)
    6261       99666 :     if ((zv_dotproduct(v, gel(gb,i)) -  v[i]) % e) return 1;
    6262       24598 :   return 0;
    6263             : }
    6264             : 
    6265             : /* list of Hecke characters not induced by a Dirichlet character up to Galois
    6266             :  * conjugation, whose conductor is bnr.cond; cycn = cyc_normalize(bnr.cyc)*/
    6267             : static GEN
    6268       26782 : mklvchi(GEN bnr, GEN cycn, GEN gb)
    6269             : {
    6270       26782 :   GEN cyc = bnr_get_cyc(bnr), cycsmall = ZV_to_zv(cyc);
    6271       26782 :   GEN vchi = cyc2elts(cycsmall);
    6272       26782 :   long ordmax = cycsmall[1], c, i, l;
    6273       26782 :   l = lg(vchi);
    6274      303450 :   for (i = c = 1; i < l; i++)
    6275             :   {
    6276      276668 :     GEN chi = gel(vchi,i);
    6277      276668 :     if (!gb || Miyake(chi, gb, cycn)) gel(vchi, c++) = Flv_to_ZV(chi);
    6278             :   }
    6279       26782 :   setlg(vchi, c); l = c;
    6280      278852 :   for (i = 1; i < l; i++)
    6281             :   {
    6282      252070 :     GEN chi = gel(vchi,i);
    6283             :     long n;
    6284      252070 :     if (!chi) continue;
    6285     1054578 :     for (n = 2; n < ordmax; n++)
    6286      965496 :       if (ugcd(n, ordmax) == 1)
    6287             :       {
    6288      397194 :         GEN tmp = vecmodii(gmulsg(n, chi), cyc);
    6289             :         long j;
    6290     7616616 :         for (j = i+1; j < l; j++)
    6291     7219422 :           if (gel(vchi,j) && gequal(gel(vchi,j), tmp)) gel(vchi,j) = NULL;
    6292             :       }
    6293             :   }
    6294      278852 :   for (i = c = 1; i < l; i++)
    6295             :   {
    6296      252070 :     GEN chi = gel(vchi,i);
    6297      252070 :     if (chi && bnrisconductor(bnr, chi)) gel(vchi, c++) = chi;
    6298             :   }
    6299       26782 :   setlg(vchi, c); return vchi;
    6300             : }
    6301             : 
    6302             : static GEN
    6303        7784 : get_gb(GEN bnr, GEN con)
    6304             : {
    6305        7784 :   GEN gb, g = bnr_get_gen(bnr), nf = bnr_get_nf(bnr);
    6306        7784 :   long i, l = lg(g);
    6307        7784 :   gb = cgetg(l, t_VEC);
    6308       18270 :   for (i = 1; i < l; i++)
    6309       10486 :     gel(gb,i) = ZV_to_zv(isprincipalray(bnr, galoisapply(nf, con, gel(g,i))));
    6310        7784 :   return gb;
    6311             : }
    6312             : static GEN
    6313       15834 : get_bnrconreyN(GEN bnr, GEN znN)
    6314             : {
    6315       15834 :   GEN z, g = znstar_get_conreygen(znN);
    6316       15834 :   long i, l = lg(g);
    6317       15834 :   z = cgetg(l, t_VEC);
    6318       57050 :   for (i = 1; i < l; i++) gel(z,i) = ZV_to_zv(isprincipalray(bnr,gel(g,i)));
    6319       15834 :   return z;
    6320             : }
    6321             : /* con = NULL if D > 0 or if D < 0 and id != idcon. */
    6322             : static GEN
    6323       33670 : mfdihedralcommon(GEN bnf, GEN id, GEN znN, GEN kroconreyN, long N, long D, GEN con)
    6324             : {
    6325       33670 :   GEN bnr = dihan_bnr(bnf, id), cyc = ZV_to_zv( bnr_get_cyc(bnr) );
    6326             :   GEN bnrconreyN, cycn, cycN, Lvchi, res, P, vT;
    6327             :   long j, ordmax, l, lc, deghecke, degrel, vt;
    6328             : 
    6329       33670 :   lc = lg(cyc); if (lc == 1) return NULL;
    6330       26782 :   cycn = cyc_normalize_zv(cyc);
    6331       26782 :   Lvchi = mklvchi(bnr, cycn, con? get_gb(bnr, con): NULL);
    6332       26782 :   l = lg(Lvchi);
    6333       26782 :   if (l == 1) return NULL;
    6334             : 
    6335       15834 :   bnrconreyN = get_bnrconreyN(bnr, znN);
    6336       15834 :   cycN = ZV_to_zv(znstar_get_cyc(znN));
    6337       15834 :   ordmax = cyc[1];
    6338       15834 :   vT = const_vec(odd(ordmax)? ordmax << 1: ordmax, NULL);
    6339       15834 :   vt = fetch_user_var("t");
    6340       15834 :   P = polcyclo(ordmax, vt);
    6341       15834 :   gel(vT,ordmax) = Qab_trace_init(ordmax, ordmax, P, P);
    6342       15834 :   deghecke = myeulerphiu(ordmax);
    6343       15834 :   res = cgetg(l, t_VEC);
    6344       50246 :   for (j = 1; j < l; j++)
    6345             :   {
    6346       34412 :     GEN T, v, vchi = ZV_to_zv(gel(Lvchi,j));
    6347       34412 :     GEN chi, chin = char_normalize_zv(vchi, cycn);
    6348             :     long o, vnum, k0;
    6349       34412 :     v = bnrchartwist2conrey(chin, cycn, bnrconreyN, kroconreyN);
    6350       34412 :     o = itou(Q_denom(v));
    6351       34412 :     T = gel(vT, o);
    6352       34412 :     if (!T) gel(vT,o) = T = Qab_trace_init(ordmax, o, P, polcyclo(o,vt));
    6353       34412 :     chi = conreydenormalize(znN, v);
    6354       34412 :     vnum = itou(znconreyexp(znN, chi));
    6355       34412 :     chi = ZV_to_zv(znconreychar(znN,chi));
    6356       34412 :     degrel = deghecke / degpol(gel(T,1));
    6357       34412 :     k0 = zv_cyc_minimize(cycN, chi, coprimes_zv(o));
    6358       34412 :     vnum = Fl_powu(vnum, k0, N);
    6359             :     /* encodes degrel forms: jdeg = 0..degrel-1 */
    6360       34412 :     gel(res,j) = mkvec3(mkvecsmalln(5, N, k0, vnum, D, degrel),
    6361             :                         id, mkvec3(cycn,chin,T));
    6362             :   }
    6363       15834 :   return res;
    6364             : }
    6365             : 
    6366             : static long
    6367       49322 : not_cond(long D, long n)
    6368             : {
    6369       49322 :   if (D > 0) return n == 4 && (D&7L) != 1;
    6370       30086 :   return n == 2 || n == 3 || (n == 4 && (D&7L)==1);
    6371             : }
    6372             : /* Append to v all dihedral weight 1 forms coming from D, if fundamental.
    6373             :  * level in [l1, l2] */
    6374             : static void
    6375       18578 : append_dihedral(GEN v, long D, long l1, long l2)
    6376             : {
    6377       18578 :   long Da = labs(D), no, i, numi, ct, min, max;
    6378             :   GEN bnf, con, LI, resall, arch1, arch2;
    6379             :   pari_sp av;
    6380             : 
    6381             :   /* min <= Nf <= max */
    6382       18578 :   max = l2 / Da;
    6383       18578 :   if (l1 == l2)
    6384             :   { /* assume Da | l2 */
    6385           0 :     min = max;
    6386           0 :     if (D > 0 && min < 3) return;
    6387             :   }
    6388             :   else /* assume l1 < l2 */
    6389       18578 :     min = (l1 + Da-1)/Da;
    6390       18578 :   if (!sisfundamental(D)) return;
    6391             : 
    6392        5684 :   av = avma;
    6393        5684 :   bnf = dihan_bnf(D);
    6394        5684 :   con = nf2_get_conj(bnf_get_nf(bnf));
    6395        5684 :   LI = ideallist(bnf, max);
    6396       55006 :   numi = 0; for (i = min; i <= max; i++) numi += lg(gel(LI, i)) - 1;
    6397        5684 :   if (D > 0)
    6398             :   {
    6399        1414 :     numi <<= 1;
    6400        1414 :     arch1 = mkvec2(gen_1,gen_0);
    6401        1414 :     arch2 = mkvec2(gen_0,gen_1);
    6402             :   }
    6403             :   else
    6404        4270 :     arch1 = arch2 = NULL;
    6405        5684 :   resall = cgetg(numi+1, t_VEC); ct = 1;
    6406       55006 :   for (no = min; no <= max; no++) if (!not_cond(D, no))
    6407             :   {
    6408       44604 :     long N = Da*no, lgc, lglis;
    6409       44604 :     GEN LIs = gel(LI, no), znN = znstar0(utoipos(N), 1), conreyN, kroconreyN;
    6410             : 
    6411       44604 :     conreyN = znstar_get_conreygen(znN); lgc = lg(conreyN);
    6412       44604 :     kroconreyN = cgetg(lgc, t_VECSMALL);
    6413      165928 :     for (i = 1; i < lgc; i++) kroconreyN[i] = krosi(D, gel(conreyN, i));
    6414       44604 :     lglis = lg(LIs);
    6415       87752 :     for (i = 1; i < lglis; i++)
    6416             :     {
    6417       43148 :       GEN id = gel(LIs, i), idcon, z;
    6418             :       long j;
    6419       43148 :       if (typ(id) == t_INT) continue;
    6420       28154 :       idcon = galoisapply(bnf, con, id);
    6421       51380 :       for (j = i; j < lglis; j++)
    6422       51380 :         if (gequal(idcon, gel(LIs, j))) { gel(LIs, j) = gen_0; break; }
    6423       28154 :       if (D < 0)
    6424             :       {
    6425       17458 :         GEN conk = i == j ? con : NULL;
    6426       17458 :         z = mfdihedralcommon(bnf, id, znN, kroconreyN, N, D, conk);
    6427       17458 :         if (z) gel(resall, ct++) = z;
    6428             :       }
    6429             :       else
    6430             :       {
    6431             :         GEN ide;
    6432       10696 :         ide = mkvec2(id, arch1);
    6433       10696 :         z = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, NULL);
    6434       10696 :         if (z) gel(resall, ct++) = z;
    6435       10696 :         if (gequal(idcon,id)) continue;
    6436        5516 :         ide = mkvec2(id, arch2);
    6437        5516 :         z = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, NULL);
    6438        5516 :         if (z) gel(resall, ct++) = z;
    6439             :       }
    6440             :     }
    6441             :   }
    6442        5684 :   if (ct == 1) set_avma(av);
    6443             :   else
    6444             :   {
    6445        4788 :     setlg(resall, ct);
    6446        4788 :     vectrunc_append(v, gerepilecopy(av, shallowconcat1(resall)));
    6447             :   }
    6448             : }
    6449             : 
    6450             : static long
    6451       42042 : di_N(GEN a) { return gel(a,1)[1]; }
    6452             : /* All primitive dihedral wt1 forms: LIM a t_VECSMALL with a single component
    6453             :  * (only level LIM) or 2 components [m,M], m < M (between m and M) */
    6454             : static GEN
    6455          14 : mfdihedralall(GEN LIM)
    6456             : {
    6457             :   GEN res, z;
    6458             :   long limD, ct, i, l1, l2;
    6459             : 
    6460          14 :   if (lg(LIM) == 2) l1 = l2 = LIM[1]; else { l1 = LIM[1]; l2 = LIM[2]; }
    6461          14 :   limD = l2;
    6462          14 :   res = vectrunc_init(2*limD);
    6463          14 :   if (l1 == l2)
    6464             :   {
    6465           0 :     GEN D = mydivisorsu(l1);
    6466           0 :     long l = lg(D), j;
    6467           0 :     for (j = 2; j < l; j++)
    6468             :     { /* skip d = 1 */
    6469           0 :       long d = D[j];
    6470           0 :       if (d == 2) continue;
    6471           0 :       append_dihedral(res, -d, l1,l2);
    6472           0 :       if (d >= 5 && D[l-j] >= 3) append_dihedral(res, d, l1,l2); /* Nf >= 3 */
    6473             :     }
    6474             :   }
    6475             :   else
    6476             :   {
    6477             :     long D;
    6478       13986 :     for (D = -3; D >= -limD; D--) append_dihedral(res, D, l1,l2);
    6479          14 :     limD /= 3; /* Nf >= 3 (GTM 193, prop 3.3.18) */
    6480        4620 :     for (D = 5; D <= limD;   D++) append_dihedral(res, D, l1,l2);
    6481             :   }
    6482          14 :   ct = lg(res);
    6483          14 :   if (ct > 1) res = shallowconcat1(res);
    6484          14 :   if (l1 == l2) return res; /* single level */
    6485          14 :   if (ct > 1)
    6486             :   { /* sort wrt N */
    6487          14 :     res = vecpermute(res, indexvecsort(res, mkvecsmall(1)));
    6488          14 :     ct = lg(res);
    6489             :   }
    6490          14 :   z = const_vec(l2-l1+1, cgetg(1,t_VEC));
    6491        7658 :   for (i = 1; i < ct;)
    6492             :   { /* regroup result sharing the same N */
    6493        7644 :     long n = di_N(gel(res,i)), j = i+1, k;
    6494             :     GEN v;
    6495       34412 :     while (j < ct && di_N(gel(res,j)) == n) j++;
    6496        7644 :     n -= l1-1;
    6497        7644 :     gel(z, n) = v = cgetg(j-i+1, t_VEC);
    6498       42056 :     for (k = 1; i < j; k++,i++) gel(v,k) = gel(res,i);
    6499             :   }
    6500          14 :   return z;
    6501             : }
    6502             : 
    6503             : /* return [vF, index], where vecpermute(vF,index) generates dihedral forms
    6504             :  * for character CHI */
    6505             : static GEN
    6506       23779 : mfdihedralnew_i(long N, GEN CHI)
    6507             : {
    6508             :   GEN bnf, Tinit, Pm, vf, M, V, NK, SP;
    6509             :   long Dold, d, ordw, i, SB, c, l, k0, k1, chino, chinoorig, lv;
    6510             : 
    6511       23779 :   SP = cache_get(cache_DIH, N);
    6512       23779 :   if (!SP) SP = mfdihedralall(mkvecsmall(N));
    6513       23779 :   lv = lg(SP); if (lv == 1) return NULL;
    6514       11354 :   CHI = mfcharinduce(CHI,N);
    6515       11354 :   ordw = mfcharorder(CHI);
    6516       11354 :   chinoorig = mfcharno(CHI);
    6517       11354 :   k0 = mfconreyminimize(CHI);
    6518       11354 :   chino = Fl_powu(chinoorig, k0, N);
    6519       11354 :   k1 = Fl_inv(k0 % ordw, ordw);
    6520       11354 :   V = cgetg(lv, t_VEC);
    6521       11354 :   d = 0;
    6522       35245 :   for (i = l = 1; i < lv; i++)
    6523             :   {
    6524       23891 :     GEN sp = gel(SP,i), T = gel(sp,1);
    6525       23891 :     if (T[3] != chino) continue;
    6526        3563 :     d += T[5];
    6527        3563 :     if (k1 != 1)
    6528             :     {
    6529          77 :       GEN t = leafcopy(T);
    6530          77 :       t[3] = chinoorig;
    6531          77 :       t[2] = (t[2]*k1) % ordw;
    6532          77 :       sp = mkvec4(t, gel(sp,2), gel(sp,3), gel(sp,4));
    6533             :     }
    6534        3563 :     gel(V, l++) = sp;
    6535             :   }
    6536       11354 :   setlg(V, l); /* dihedral forms of level N and character CHI */
    6537       11354 :   if (l == 1) return NULL;
    6538             : 
    6539        2338 :   SB = myeulerphiu(ordw) * mfsturmNk(N,1) + 1;
    6540        2338 :   M = cgetg(d+1, t_MAT);
    6541        2338 :   vf = cgetg(d+1, t_VEC);
    6542        2338 :   NK = mkNK(N, 1, CHI);
    6543        2338 :   bnf = NULL; Dold = 0;
    6544        5901 :   for (i = c = 1; i < l; i++)
    6545             :   { /* T = [N, k0, conreyno, D, degrel] */
    6546        3563 :     GEN bnr, Vi = gel(V,i), T = gel(Vi,1), id = gel(Vi,2), w = gel(Vi,3);
    6547        3563 :     long jdeg, k0i = T[2], D = T[4], degrel = T[5];
    6548             : 
    6549        3563 :     if (D != Dold) { Dold = D; bnf = dihan_bnf(D); }
    6550        3563 :     bnr = dihan_bnr(bnf, id);
    6551       10479 :     for (jdeg = 0; jdeg < degrel; jdeg++,c++)
    6552             :     {
    6553        6916 :       GEN k0j = mkvecsmall2(k0i, jdeg), an = dihan(bnr, w, k0j, SB);
    6554        6916 :       settyp(an, t_COL); gel(M,c) = Q_primpart(an);
    6555        6916 :       gel(vf,c) = tag3(t_MF_DIHEDRAL, NK, bnr, w, k0j);
    6556             :     }
    6557             :   }
    6558        2338 :   Tinit = gmael3(V,1,3,3); Pm = gel(Tinit,1);
    6559        2338 :   V = QabM_indexrank(M, degpol(Pm)==1? NULL: Pm, ordw);
    6560        2338 :   return mkvec2(vf,gel(V,2));
    6561             : }
    6562             : static long
    6563       15820 : mfdihedralnewdim(long N, GEN CHI)
    6564             : {
    6565       15820 :   pari_sp av = avma;
    6566       15820 :   GEN S = mfdihedralnew_i(N, CHI);
    6567       15820 :   return gc_long(av, S? lg(gel(S,2))-1: 0);
    6568             : }
    6569             : static GEN
    6570        7959 : mfdihedralnew(long N, GEN CHI)
    6571             : {
    6572        7959 :   pari_sp av = avma;
    6573        7959 :   GEN S = mfdihedralnew_i(N, CHI);
    6574        7959 :   if (!S) { set_avma(av); return cgetg(1, t_VEC); }
    6575         777 :   return vecpermute(gel(S,1), gel(S,2));
    6576             : }
    6577             : 
    6578             : static long
    6579        7042 : mfdihedralcuspdim(long N, GEN CHI)
    6580             : {
    6581        7042 :   pari_sp av = avma;
    6582             :   GEN D, CHIP;
    6583             :   long F, i, lD, dim;
    6584             : 
    6585        7042 :   CHIP = mfchartoprimitive(CHI, &F);
    6586        7042 :   D = mydivisorsu(N/F); lD = lg(D);
    6587        7042 :   dim = mfdihedralnewdim(N, CHI); /* d = 1 */
    6588       15820 :   for (i = 2; i < lD; i++)
    6589             :   {
    6590        8778 :     long d = D[i], a = mfdihedralnewdim(N / d, CHIP);
    6591        8778 :     if (a) dim += a * mynumdivu(d);
    6592             :   }
    6593        7042 :   return gc_long(av,dim);
    6594             : }
    6595             : 
    6596             : static GEN
    6597        5901 : mfbdall(GEN E, long N)
    6598             : {
    6599        5901 :   GEN v, D = mydivisorsu(N);
    6600        5901 :   long i, j, nD = lg(D) - 1, nE = lg(E) - 1;
    6601        5901 :   v = cgetg(nD*nE + 1, t_VEC);
    6602        7812 :   for (j = 1; j <= nE; j++)
    6603             :   {
    6604        1911 :     GEN Ej = gel(E, j);
    6605        6125 :     for (i = 0; i < nD; i++) gel(v, i*nE + j) = mfbd_i(Ej, D[i+1]);
    6606             :   }
    6607        5901 :   return v;
    6608             : }
    6609             : static GEN
    6610        3458 : mfdihedralcusp(long N, GEN CHI)
    6611             : {
    6612        3458 :   pari_sp av = avma;
    6613             :   GEN D, CHIP, z;
    6614             :   long F, i, lD;
    6615             : 
    6616        3458 :   CHIP = mfchartoprimitive(CHI, &F);
    6617        3458 :   D = mydivisorsu(N/F); lD = lg(D);
    6618        3458 :   z = cgetg(lD, t_VEC);
    6619        3458 :   gel(z,1) = mfdihedralnew(N, CHI);
    6620        7749 :   for (i = 2; i < lD; i++) /* skip 1 */
    6621             :   {
    6622        4291 :     GEN LF = mfdihedralnew(N / D[i], CHIP);
    6623        4291 :     gel(z,i) = mfbdall(LF, D[i]);
    6624             :   }
    6625        3458 :   return gerepilecopy(av, shallowconcat1(z));
    6626             : }
    6627             : 
    6628             : /* used to decide between ratlift and comatrix for ZM_inv; ratlift is better
    6629             :  * when N has many divisors */
    6630             : static int
    6631        2450 : abundant(ulong N) { return mynumdivu(N) >= 8; }
    6632             : 
    6633             : /* CHI an mfchar */
    6634             : static int
    6635         294 : cmp_ord(void *E, GEN a, GEN b)
    6636             : {
    6637         294 :   GEN chia = MF_get_CHI(a), chib = MF_get_CHI(b);
    6638         294 :   (void)E; return cmpii(gmfcharorder(chia), gmfcharorder(chib));
    6639             : }
    6640             : /* mfinit structure.
    6641             : -- mf[1] contains [N,k,CHI,space],
    6642             : -- mf[2] contains vector of closures of Eisenstein series, empty if not
    6643             :    full space.
    6644             : -- mf[3] contains vector of closures, so #mf[3] = dimension of cusp/new space.
    6645             : -- mf[4] contains the corresponding indices: either j for T(j)tf if newspace,
    6646             :    or [M,j,d] for B(d)T(j)tf_M if cuspspace or oldspace.
    6647             : -- mf[5] contains the matrix M of first coefficients of basis, never cleaned.
    6648             :  * NK is either [N,k] or [N,k,CHI].
    6649             :  * mfinit does not do the splitting, only the basis generation. */
    6650             : 
    6651             : /* Set flraw to 1 if do not need mf[5]: no mftobasis etc..., only the
    6652             :    expansions of the basis elements are needed. */
    6653             : 
    6654             : static GEN
    6655        4795 : mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw)
    6656             : {
    6657        4795 :   GEN M = NULL, mf = NULL, mf1 = mkvec4(utoi(N), stoi(k), CHI, utoi(space));
    6658        4795 :   long sb = mfsturmNk(N, k);
    6659             :   cachenew_t cache;
    6660        4795 :   if (k < 0 || badchar(N, k, CHI)) return mfEMPTY(mf1);
    6661        4760 :   if (k == 0) /*nothing*/;
    6662        4718 :   else if (k == 1)
    6663             :   {
    6664         364 :     switch (space)
    6665             :     {
    6666         336 :       case mf_NEW:
    6667             :       case mf_FULL:
    6668         336 :       case mf_CUSP: mf = mfwt1init(N, CHI, NULL, space, flraw); break;
    6669          14 :       case mf_EISEN:break;
    6670           7 :       case mf_OLD: pari_err_IMPL("mfinit in weight 1 for old space");
    6671           7 :       default: pari_err_FLAG("mfinit");
    6672             :     }
    6673             :   }
    6674             :   else /* k >= 2 */
    6675             :   {
    6676        4354 :     long ord = mfcharorder(CHI);
    6677        4354 :     GEN z = NULL, P = (ord <= 2)? NULL: mfcharpol(CHI);
    6678        4354 :     switch(space)
    6679             :     {
    6680         105 :       case mf_EISEN:
    6681         105 :         break;
    6682        1197 :       case mf_NEW:
    6683        1197 :         mf = mfnewinit(N, k, CHI, &cache, 1);
    6684        1197 :         if (mf && !flraw) { M = MF_get_M(mf); z = MF_get_Mindex(mf); }
    6685        1197 :         break;
    6686        3045 :       case mf_OLD:
    6687             :       case mf_CUSP:
    6688             :       case mf_FULL:
    6689        3045 :         mf = mfinitcusp(N, k, CHI, &cache, space);
    6690        3045 :         if (mf && !flraw)
    6691             :         {
    6692        2163 :           GEN S = MF_get_S(mf);
    6693        2163 :           M = bhnmat_extend(M, sb+1, 1, S, &cache);
    6694        2163 :           if (space != mf_FULL) gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6695             :         }
    6696        3045 :         dbg_cachenew(&cache);
    6697        3045 :         break;
    6698           7 :       default: pari_err_FLAG("mfinit");
    6699             :     }
    6700        4347 :     if (z) gel(mf,5) = mfclean2(M, z, P, ord);
    6701             :   }
    6702        4739 :   if (!mf) mf = mfEMPTY(mf1);
    6703             :   else
    6704             :   {
    6705        3829 :     gel(mf,1) = mf1;
    6706        3829 :     if (flraw) gel(mf,5) = zerovec(3);
    6707             :   }
    6708        4739 :   if (!space_is_cusp(space))
    6709             :   {
    6710         742 :     GEN E = mfeisensteinbasis(N, k, CHI);
    6711         742 :     gel(mf,2) = E;
    6712         742 :     if (!flraw)
    6713             :     {
    6714         476 :       if (M)
    6715         189 :         M = shallowconcat(mfvectomat(E, sb+1, 1), M);
    6716             :       else
    6717         287 :         M = mfcoefs_mf(mf, sb+1, 1);
    6718         476 :       gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6719             :     }
    6720             :   }
    6721        4739 :   return mf;
    6722             : }
    6723             : 
    6724             : /* mfinit for k = nk/dk */
    6725             : static GEN
    6726        2590 : mfinit_Nndkchi(long N, long nk, long dk, GEN CHI, long space, long flraw)
    6727         238 : { return (dk == 2)? mf2init_Nkchi(N, nk >> 1, CHI, space, flraw)
    6728        2828 :                   : mfinit_Nkchi(N, nk, CHI, space, flraw); }
    6729             : static GEN
    6730        3248 : mfinit_i(GEN NK, long space)
    6731             : {
    6732             :   GEN CHI, mf;
    6733             :   long N, k, dk, joker;
    6734        3248 :   if (checkmf_i(NK))
    6735             :   {
    6736         140 :     N = mf_get_N(NK);
    6737         140 :     Qtoss(mf_get_gk(NK), &k, &dk);
    6738         140 :     CHI = mf_get_CHI(NK);
    6739             :   }
    6740        3108 :   else if ((mf = checkMF_i(NK)))
    6741             :   {
    6742          21 :     long s = MF_get_space(mf);
    6743          21 :     if (s == space) return mf;
    6744          21 :     Qtoss(MF_get_gk(mf), &k, &dk);
    6745          21 :     if (dk == 1 && k > 1 && space == mf_NEW && (s == mf_CUSP || s == mf_FULL))
    6746          21 :       return mfinittonew(mf);
    6747           0 :     N = MF_get_N(mf);
    6748           0 :     CHI = MF_get_CHI(mf);
    6749             :   }
    6750             :   else
    6751        3087 :     checkNK2(NK, &N, &k, &dk, &CHI, 1);
    6752        3206 :   joker = !CHI || typ(CHI) == t_COL;
    6753        3206 :   if (joker)
    6754             :   {
    6755        1141 :     GEN mf, vCHI = CHI;
    6756             :     long i, j, l;
    6757        1141 :     if (CHI && lg(CHI) == 1) return cgetg(1,t_VEC);
    6758        1134 :     if (k < 0) return mfEMPTYall(N, sstoQ(k,dk), CHI, space);
    6759        1120 :     if (k == 1 && dk == 1 && space != mf_EISEN)
    6760         483 :     {
    6761             :       GEN TMP, gN, gs;
    6762        1085 :       if (space != mf_CUSP && space != mf_NEW)
    6763           0 :         pari_err_IMPL("mfinit([N,1,wildcard], space != cusp or new space)");
    6764        1085 :       if (wt1empty(N)) return mfEMPTYall(N, gen_1, CHI, space);
    6765         483 :       vCHI = mfwt1chars(N,vCHI);
    6766         483 :       l = lg(vCHI); mf = cgetg(l, t_VEC); if (l == 1) return mf;
    6767         483 :       TMP = mfwt1_pre(N); gN = utoipos(N); gs = utoi(space);
    6768        3717 :       for (i = j = 1; i < l; i++)
    6769             :       {
    6770        3234 :         pari_sp av = avma;
    6771        3234 :         GEN c = gel(vCHI,i), z = mfwt1init(N, c, TMP, space, 0);
    6772        3234 :         if (!z) {
    6773        2590 :           set_avma(av);
    6774        2590 :           if (CHI) z = mfEMPTY(mkvec4(gN,gen_1,c,gs));
    6775             :         }
    6776        3234 :         if (z) gel(mf, j++) = z;
    6777             :       }
    6778             :     }
    6779             :     else
    6780             :     {
    6781          35 :       vCHI = mfchars(N,k,dk,vCHI);
    6782          35 :       l = lg(vCHI); mf = cgetg(l, t_VEC);
    6783         119 :       for (i = j = 1; i < l; i++)
    6784             :       {
    6785          84 :         pari_sp av = avma;
    6786          84 :         GEN v = mfinit_Nndkchi(N, k, dk, gel(vCHI,i), space, 0);
    6787          84 :         if (MF_get_dim(v) || CHI) gel(mf, j++) = v; else set_avma(av);
    6788             :       }
    6789             :     }
    6790         518 :     setlg(mf,j);
    6791         518 :     if (!CHI) gen_sort_inplace(mf, NULL, &cmp_ord, NULL);
    6792         518 :     return mf;
    6793             :   }
    6794        2065 :   return mfinit_Nndkchi(N, k, dk, CHI, space, 0);
    6795             : }
    6796             : GEN
    6797        2289 : mfinit(GEN NK, long space)
    6798             : {
    6799        2289 :   pari_sp av = avma;
    6800        2289 :   return gerepilecopy(av, mfinit_i(NK, space));
    6801             : }
    6802             : 
    6803             : /* UTILITY FUNCTIONS */
    6804             : static void
    6805         364 : cusp_canon(GEN cusp, long N, long *pA, long *pC)
    6806             : {
    6807         364 :   pari_sp av = avma;
    6808             :   long A, C, tc, cg;
    6809         364 :   if (N <= 0) pari_err_DOMAIN("mfcuspwidth","N","<=",gen_0,stoi(N));
    6810         357 :   if (!cusp || (tc = typ(cusp)) == t_INFINITY) { *pA = 1; *pC = N; return; }
    6811         350 :   if (tc != t_INT && tc != t_FRAC) pari_err_TYPE("checkcusp", cusp);
    6812         350 :   Qtoss(cusp, &A,&C);
    6813         350 :   if (N % C)
    6814             :   {
    6815             :     ulong uC;
    6816          14 :     long u = Fl_invgen((C-1)%N + 1, N, &uC);
    6817          14 :     A = Fl_mul(A, u, N);
    6818          14 :     C = (long)uC;
    6819             :   }
    6820         350 :   cg = ugcd(C, N/C);
    6821         420 :   while (ugcd(A, N) > 1) A += cg;
    6822         350 :   *pA = A % N; *pC = C; set_avma(av);
    6823             : }
    6824             : static long
    6825         903 : mfcuspcanon_width(long N, long C)
    6826         903 : { return (!C || C == N)? 1 : N / ugcd(N, Fl_sqr(umodsu(C,N),N)); }
    6827             : /* v = [a,c] a ZC, width of cusp (a:c) */
    6828             : static long
    6829        8750 : mfZC_width(long N, GEN v)
    6830             : {
    6831        8750 :   ulong C = umodiu(gel(v,2), N);
    6832        8750 :   return (C == 0)? 1: N / ugcd(N, Fl_sqr(C,N));
    6833             : }
    6834             : long
    6835         161 : mfcuspwidth(GEN gN, GEN cusp)
    6836             : {
    6837         161 :   long N = 0, A, C;
    6838             :   GEN mf;
    6839         161 :   if (typ(gN) == t_INT) N = itos(gN);
    6840          42 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    6841           0 :   else pari_err_TYPE("mfcuspwidth", gN);
    6842         161 :   cusp_canon(cusp, N, &A, &C);
    6843         154 :   return mfcuspcanon_width(N, C);
    6844             : }
    6845             : 
    6846             : /* Q a t_INT */
    6847             : static GEN
    6848          14 : findq(GEN al, GEN Q)
    6849             : {
    6850             :   long n;
    6851          14 :   if (typ(al) == t_FRAC && cmpii(gel(al,2), Q) <= 0)
    6852           0 :     return mkvec(mkvec2(gel(al,1), gel(al,2)));
    6853          14 :   n = 1 + (long)ceil(2.0781*gtodouble(glog(Q, LOWDEFAULTPREC)));
    6854          14 :   return contfracpnqn(gboundcf(al,n), n);
    6855             : }
    6856             : static GEN
    6857          91 : findqga(long N, GEN z)
    6858             : {
    6859          91 :   GEN Q, LDC, CK = NULL, DK = NULL, ma, x, y = imag_i(z);
    6860             :   long j, l;
    6861          91 :   if (gcmpgs(gmulsg(2*N, y), 1) >= 0) return NULL;
    6862          14 :   x = real_i(z);
    6863          14 :   Q = ground(ginv(gsqrt(gmulsg(N, y), LOWDEFAULTPREC)));
    6864          14 :   LDC = findq(gmulsg(-N,x), Q);
    6865          14 :   ma = gen_1; l = lg(LDC);
    6866          35 :   for (j = 1; j < l; j++)
    6867             :   {
    6868          21 :     GEN D, DC = gel(LDC,j), C1 = gel(DC,2);
    6869          21 :     if (cmpii(C1,Q) > 0) break;
    6870          21 :     D = gel(DC,1);
    6871          21 :     if (ugcdiu(D,N) == 1)
    6872             :     {
    6873           7 :       GEN C = mului(N, C1), den;
    6874           7 :       den = gadd(gsqr(gmul(C,y)), gsqr(gadd(D, gmul(C,x))));
    6875           7 :       if (gcmp(den, ma) < 0) { ma = den; CK = C; DK = D; }
    6876             :     }
    6877             :   }
    6878          14 :   return DK? mkvec2(CK, DK): NULL;
    6879             : }
    6880             : 
    6881             : static long
    6882         154 : valNC2(GEN P, GEN E, long e)
    6883             : {
    6884         154 :   long i, d = 1, l = lg(P);
    6885         476 :   for (i = 1; i < l; i++)
    6886             :   {
    6887         322 :     long v = u_lval(e, P[i]) << 1;
    6888         322 :     if (v == E[i] + 1) v--;
    6889         322 :     d *= upowuu(P[i], v);
    6890             :   }
    6891         154 :   return d;
    6892             : }
    6893             : 
    6894             : static GEN
    6895          42 : findqganew(long N, GEN z)
    6896             : {
    6897          42 :   GEN MI, DI, x = real_i(z), y = imag_i(z), Ck = gen_0, Dk = gen_1, fa, P, E;
    6898             :   long i;
    6899          42 :   MI = ginv(utoi(N));
    6900          42 :   DI = mydivisorsu(mysqrtu(N));
    6901          42 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    6902         196 :   for (i = 1; i < lg(DI); i++)
    6903             :   {
    6904         154 :     long e = DI[i], g;
    6905             :     GEN U, C, D, m;
    6906         154 :     (void)cxredsl2(gmulsg(e, z), &U);
    6907         154 :     C = gcoeff(U,2,1); if (!signe(C)) continue;
    6908         154 :     D = gcoeff(U,2,2);
    6909         154 :     g = ugcdiu(D,e);
    6910         154 :     if (g > 1) { C = muliu(C,e/g); D = diviuexact(D,g); } else C = muliu(C,e);
    6911         154 :     m = gadd(gsqr(gadd(gmul(C, x), D)), gsqr(gmul(C, y)));
    6912         154 :     m = gdivgs(m, valNC2(P, E, e));
    6913         154 :     if (gcmp(m, MI) < 0) { MI = m; Ck = C; Dk = D; }
    6914             :   }
    6915          42 :   return signe(Ck)? mkvec2(Ck, Dk): NULL;
    6916             : }
    6917             : 
    6918             : /* Return z' and U = [a,b;c,d] \in SL_2(Z), z' = U*z,
    6919             :  * Im(z')/width(U.oo) > sqrt(3)/(2N). Set *pczd = c*z+d */
    6920             : static GEN
    6921         168 : cxredga0N(long N, GEN z, GEN *pU, GEN *pczd, long flag)
    6922             : {
    6923         168 :   GEN v = NULL, A, B, C, D;
    6924             :   long e;
    6925         168 :   if (N == 1) return cxredsl2_i(z, pU, pczd);
    6926         133 :   e = gexpo(gel(z,2));
    6927         133 :   if (e < 0) z = gprec_wensure(z, precision(z) + nbits2extraprec(-e));
    6928         133 :   v = flag? findqganew(N,z): findqga(N,z);
    6929         133 :   if (!v) { *pU = matid(2); *pczd = gen_1; return z; }
    6930          49 :   C = gel(v,1);
    6931          49 :   D = gel(v,2);
    6932          49 :   if (!is_pm1(bezout(C,D, &B,&A))) pari_err_BUG("cxredga0N [gcd > 1]");
    6933          49 :   B = negi(B);
    6934          49 :   *pU = mkmat2(mkcol2(A,C), mkcol2(B,D));
    6935          49 :   *pczd = gadd(gmul(C,z), D);
    6936          49 :   return gdiv(gadd(gmul(A,z), B), *pczd);
    6937             : }
    6938             : 
    6939             : static GEN
    6940         154 : lfunthetaall(GEN b, GEN vL, GEN t, long bitprec)
    6941             : {
    6942         154 :   long i, l = lg(vL);
    6943         154 :   GEN v = cgetg(l, t_VEC);
    6944         336 :   for (i = 1; i < l; i++)
    6945             :   {
    6946         182 :     GEN T, L = gel(vL,i), a0 = gel(L,1), ldata = gel(L,2);
    6947         182 :     GEN van = gel(ldata_get_an(ldata),2);
    6948         182 :     if (lg(van) == 1)
    6949             :     {
    6950           0 :       T = gmul(b, a0);
    6951           0 :       if (isexactzero(T)) { GEN z = real_0_bit(-bitprec); T = mkcomplex(z,z); }
    6952             :     }
    6953             :     else
    6954             :     {
    6955         182 :       T = gmul2n(lfuntheta(ldata, t, 0, bitprec), -1);
    6956         182 :       T = gmul(b, gadd(a0, T));
    6957             :     }
    6958         182 :     gel(v,i) = T;
    6959             :   }
    6960         154 :   return l == 2? gel(v,1): v;
    6961             : }
    6962             : 
    6963             : /* P in ZX, irreducible */
    6964             : static GEN
    6965         175 : ZX_roots(GEN P, long prec)
    6966             : {
    6967         175 :   long d = degpol(P);
    6968         175 :   if (d == 1) return mkvec(gen_0);
    6969         175 :   if (d == 2 && isint1(gel(P,2)) && isintzero(gel(P,3)) && isint1(gel(P,4)))
    6970           7 :     return mkvec2(powIs(3), gen_I()); /* order as polroots */
    6971         280 :   return (ZX_sturm_irred(P) == d)? ZX_realroots_irred(P, prec)
    6972         280 :                                  : QX_complex_roots(P, prec);
    6973             : }
    6974             : /* initializations for RgX_RgV_eval / RgC_embed */
    6975             : static GEN
    6976         210 : rootspowers(GEN v)
    6977             : {
    6978         210 :   long i, l = lg(v);
    6979         210 :   GEN w = cgetg(l, t_VEC);
    6980         826 :   for (i = 1; i < l; i++) gel(w,i) = gpowers(gel(v,i), l-2);
    6981         210 :   return w;
    6982             : }
    6983             : /* mf embeddings attached to Q(chi)/(T), chi attached to cyclotomic P */
    6984             : static GEN
    6985         875 : getembed(GEN P, GEN T, GEN zcyclo, long prec)
    6986             : {
    6987             :   long i, l;
    6988             :   GEN v;
    6989         875 :   if (degpol(P) == 1) P = NULL; /* mfcharpol for quadratic char */
    6990         875 :   if (degpol(T) == 1) T = NULL; /* dim 1 orbit */
    6991         875 :   if (T && P)
    6992          35 :   { /* K(y) / (T(y)), K = Q(t)/(P) cyclotomic */
    6993          35 :     GEN vr = RgX_is_ZX(T)? ZX_roots(T,prec): roots(RgX_embed1(T,zcyclo), prec);
    6994          35 :     v = rootspowers(vr); l = lg(v);
    6995         105 :     for (i = 1; i < l; i++) gel(v,i) = mkcol3(P,zcyclo,gel(v,i));
    6996             :   }
    6997         840 :   else if (T)
    6998             :   { /* Q(y) / (T(y)), T non-cyclotomic */
    6999         175 :     GEN vr = ZX_roots(T, prec);
    7000         175 :     v = rootspowers(vr); l = lg(v);
    7001         721 :     for (i = 1; i < l; i++) gel(v,i) = mkcol2(T, gel(v,i));
    7002             :   }
    7003             :   else /* cyclotomic or rational */
    7004         665 :     v = mkvec(P? mkvec2(P, zcyclo): cgetg(1,t_VEC));
    7005         875 :   return v;
    7006             : }
    7007             : static GEN
    7008         728 : grootsof1_CHI(GEN CHI, long prec)
    7009         728 : { return grootsof1(mfcharorder(CHI), prec); }
    7010             : /* return the [Q(F):Q(chi)] embeddings of F */
    7011             : static GEN
    7012         574 : mfgetembed(GEN F, long prec)
    7013             : {
    7014         574 :   GEN T = mf_get_field(F), CHI = mf_get_CHI(F), P = mfcharpol(CHI);
    7015         574 :   return getembed(P, T, grootsof1_CHI(CHI, prec), prec);
    7016             : }
    7017             : static GEN
    7018           7 : mfchiembed(GEN mf, long prec)
    7019             : {
    7020           7 :   GEN CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    7021           7 :   return getembed(P, pol_x(0), grootsof1_CHI(CHI, prec), prec);
    7022             : }
    7023             : /* mfgetembed for the successive eigenforms in MF_get_newforms */
    7024             : static GEN
    7025         147 : mfeigenembed(GEN mf, long prec)
    7026             : {
    7027         147 :   GEN vP = MF_get_fields(mf), vF = MF_get_newforms(mf);
    7028         147 :   GEN zcyclo, vE, CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    7029         147 :   long i, l = lg(vP);
    7030         147 :   vF = Q_remove_denom(liftpol_shallow(vF), NULL);
    7031         147 :   prec += nbits2extraprec(gexpo(vF));
    7032         147 :   zcyclo = grootsof1_CHI(CHI, prec);
    7033         147 :   vE = cgetg(l, t_VEC);
    7034         441 :   for (i = 1; i < l; i++) gel(vE,i) = getembed(P, gel(vP,i), zcyclo, prec);
    7035         147 :   return vE;
    7036             : }
    7037             : 
    7038             : static int
    7039          28 : checkPv(GEN P, GEN v)
    7040          28 : { return typ(P) == t_POL && is_vec_t(typ(v)) && lg(v)-1 >= degpol(P); }
    7041             : static int
    7042          28 : checkemb_i(GEN E)
    7043             : {
    7044          28 :   long t = typ(E), l = lg(E);
    7045          28 :   if (t == t_VEC) return l == 1 || (l == 3 && checkPv(gel(E,1), gel(E,2)));
    7046          21 :   if (t != t_COL) return 0;
    7047          21 :   if (l == 3) return checkPv(gel(E,1), gel(E,2));
    7048          21 :   return l == 4 && is_vec_t(typ(gel(E,2))) && checkPv(gel(E,1), gel(E,3));
    7049             : }
    7050             : static GEN
    7051          28 : anyembed(GEN v, GEN E)
    7052             : {
    7053          28 :   switch(typ(v))
    7054             :   {
    7055          21 :     case t_VEC: case t_COL: return mfvecembed(E, v);
    7056           7 :     case t_MAT: return mfmatembed(E, v);
    7057             :   }
    7058           0 :   return mfembed(E, v);
    7059             : }
    7060             : GEN
    7061          49 : mfembed0(GEN E, GEN v, long prec)
    7062             : {
    7063          49 :   pari_sp av = avma;
    7064          49 :   GEN mf, vE = NULL;
    7065          49 :   if (checkmf_i(E)) vE = mfgetembed(E, prec);
    7066          35 :   else if ((mf = checkMF_i(E))) vE = mfchiembed(mf, prec);
    7067          49 :   if (vE)
    7068             :   {
    7069          21 :     long i, l = lg(vE);
    7070             :     GEN w;
    7071          21 :     if (!v) return gerepilecopy(av, l == 2? gel(vE,1): vE);
    7072           0 :     w = cgetg(l, t_VEC);
    7073           0 :     for (i = 1; i < l; i++) gel(w,i) = anyembed(v, gel(vE,i));
    7074           0 :     return gerepilecopy(av, l == 2? gel(w,1): w);
    7075             :   }
    7076          28 :   if (!checkemb_i(E) || !v) pari_err_TYPE("mfembed", E);
    7077          28 :   return gerepilecopy(av, anyembed(v,E));
    7078             : }
    7079             : 
    7080             : /* dummy lfun create for theta evaluation */
    7081             : static GEN
    7082         882 : mfthetaancreate(GEN van, GEN N, GEN k)
    7083             : {
    7084         882 :   GEN L = zerovec(6);
    7085         882 :   gel(L,1) = lfuntag(t_LFUN_GENERIC, van);
    7086         882 :   gel(L,3) = mkvec2(gen_0, gen_1);
    7087         882 :   gel(L,4) = k;
    7088         882 :   gel(L,5) = N; return L;
    7089             : }
    7090             : /* destroy van and prepare to evaluate theta(sigma(van)), for all sigma in
    7091             :  * embeddings vector vE */
    7092             : static GEN
    7093         322 : van_embedall(GEN van, GEN vE, GEN gN, GEN gk)
    7094             : {
    7095         322 :   GEN a0 = gel(van,1), vL;
    7096         322 :   long i, lE = lg(vE), l = lg(van);
    7097         322 :   van++; van[0] = evaltyp(t_VEC) | evallg(l-1); /* remove a0 */
    7098         322 :   vL = cgetg(lE, t_VEC);
    7099         847 :   for (i = 1; i < lE; i++)
    7100             :   {
    7101         525 :     GEN E = gel(vE,i), v = mfvecembed(E, van);
    7102         525 :     gel(vL,i) = mkvec2(mfembed(E,a0), mfthetaancreate(v, gN, gk));
    7103             :   }
    7104         322 :   return vL;
    7105             : }
    7106             : 
    7107             : static int
    7108        1022 : cusp_AC(GEN cusp, long *A, long *C)
    7109             : {
    7110        1022 :   switch(typ(cusp))
    7111             :   {
    7112         105 :     case t_INFINITY: *A = 1; *C = 0; break;
    7113         273 :     case t_INT:  *A = itos(cusp); *C = 1; break;
    7114         427 :     case t_FRAC: *A = itos(gel(cusp, 1)); *C = itos(gel(cusp, 2)); break;
    7115         217 :     case t_REAL: case t_COMPLEX:
    7116         217 :       *A = 0; *C = 0;
    7117         217 :       if (gsigne(imag_i(cusp)) <= 0)
    7118           7 :         pari_err_DOMAIN("mfeval","imag(tau)","<=",gen_0,cusp);
    7119         210 :       return 0;
    7120           0 :     default: pari_err_TYPE("cusp_AC", cusp);
    7121             :   }
    7122         805 :   return 1;
    7123             : }
    7124             : static GEN
    7125         518 : cusp2mat(long A, long C)
    7126             : { long B, D;
    7127         518 :   cbezout(A, C, &D, &B);
    7128         518 :   return mkmat22s(A, -B, C, D);
    7129             : }
    7130             : static GEN
    7131          21 : mkS(void) { return mkmat22s(0,-1,1,0); }
    7132             : 
    7133             : /* if t is a cusp, return F(t), else NULL */
    7134             : static GEN
    7135         350 : evalcusp(GEN mf, GEN F, GEN t, long prec)
    7136             : {
    7137             :   long A, C;
    7138             :   GEN R;
    7139         350 :   if (!cusp_AC(t, &A,&C)) return NULL;
    7140         189 :   if (C % mf_get_N(F) == 0) return gel(mfcoefs_i(F, 0, 1), 1);
    7141         175 :   R = mfgaexpansion(mf, F, cusp2mat(A,C), 0, prec);
    7142         175 :   return gequal0(gel(R,1))? gmael(R,3,1): gen_0;
    7143             : }
    7144             : /* Evaluate an mf closure numerically, i.e., in the usual sense, either for a
    7145             :  * single tau or a vector of tau; for each, return a vector of results
    7146             :  * corresponding to all complex embeddings of F. If flag is non-zero, allow
    7147             :  * replacing F by F | gamma to increase imag(gamma^(-1).tau) [ expensive if
    7148             :  * MF_EISENSPACE not present ] */
    7149             : static GEN
    7150         161 : mfeval_i(GEN mf, GEN F, GEN vtau, long flag, long bitprec)
    7151             : {
    7152             :   GEN L0, vL, vb, sqN, vczd, vTAU, vs, van, vE;
    7153         161 :   long N = MF_get_N(mf), N0, ta, lv, i, prec = nbits2prec(bitprec);
    7154         161 :   GEN gN = utoipos(N), gk = mf_get_gk(F), gk1 = gsubgs(gk,1), vgk;
    7155         161 :   long flscal = 0;
    7156             : 
    7157             :   /* gen_0 is ignored, second component assumes Ramanujan-Petersson in
    7158             :    * 1/2-integer weight */
    7159         161 :   vgk = mkvec2(gen_0, mfiscuspidal(mf,F)? gmul2n(gk1,-1): gk1);
    7160         161 :   ta = typ(vtau);
    7161         161 :   if (!is_vec_t(ta)) { flscal = 1; vtau = mkvec(vtau); ta = t_VEC; }
    7162         161 :   lv = lg(vtau);
    7163         161 :   sqN = sqrtr_abs(utor(N, prec));
    7164         161 :   vs = const_vec(lv-1, NULL);
    7165         161 :   vb = const_vec(lv-1, NULL);
    7166         161 :   vL = cgetg(lv, t_VEC);
    7167         161 :   vTAU = cgetg(lv, t_VEC);
    7168         161 :   vczd = cgetg(lv, t_VEC);
    7169         161 :   L0 = mfthetaancreate(NULL, gN, vgk); /* only for thetacost */
    7170         161 :   vE = mfgetembed(F, prec);
    7171         161 :   N0 = 0;
    7172         343 :   for (i = 1; i < lv; i++)
    7173             :   {
    7174         189 :     GEN z = gel(vtau,i), tau, U;
    7175             :     long w, n;
    7176             : 
    7177         189 :     gel(vs,i) = evalcusp(mf, F, z, prec);
    7178         182 :     if (gel(vs,i)) continue;
    7179         154 :     tau = cxredga0N(N, z, &U, &gel(vczd,i), flag);
    7180         154 :     if (!flag) w = 0; else { w = mfZC_width(N, gel(U,1)); tau = gdivgs(tau,w); }
    7181         154 :     gel(vTAU,i) = mulcxmI(gmul(tau, sqN));
    7182         154 :     n = lfunthetacost(L0, real_i(gel(vTAU,i)), 0, bitprec);
    7183         154 :     if (N0 < n) N0 = n;
    7184         154 :     if (flag)
    7185             :     {
    7186          42 :       GEN A, al, v = mfslashexpansion(mf, F, ZM_inv(U,NULL), n, 0, &A, prec);
    7187          42 :       gel(vL,i) = van_embedall(v, vE, gN, vgk);
    7188          42 :       al = gel(A,1);
    7189          42 :       if (!gequal0(al))
    7190           7 :         gel(vb,i) = gexp(gmul(gmul(gmulsg(w,al),PiI2(prec)), tau), prec);
    7191             :     }
    7192             :   }
    7193         154 :   if (!flag)
    7194             :   {
    7195         112 :     van = mfcoefs_i(F, N0, 1);
    7196         112 :     vL = const_vec(lv-1, van_embedall(van, vE, gN, vgk));
    7197             :   }
    7198         336 :   for (i = 1; i < lv; i++)
    7199             :   {
    7200             :     GEN T;
    7201         182 :     if (gel(vs,i)) continue;
    7202         154 :     T = gpow(gel(vczd,i), gneg(gk), prec);
    7203         154 :     if (flag && gel(vb,i)) T = gmul(T, gel(vb,i));
    7204         154 :     gel(vs,i) = lfunthetaall(T, gel(vL,i), gel(vTAU,i), bitprec);
    7205             :   }
    7206         154 :   return flscal? gel(vs,1): vs;
    7207             : }
    7208             : 
    7209             : static long
    7210        1134 : mfistrivial(GEN F)
    7211             : {
    7212        1134 :   switch(mf_get_type(F))
    7213             :   {
    7214           7 :     case t_MF_CONST: return lg(gel(F,2)) == 1;
    7215         259 :     case t_MF_LINEAR: case t_MF_LINEAR_BHN: return gequal0(gel(F,3));
    7216         868 :     default: return 0;
    7217             :   }
    7218             : }
    7219             : 
    7220             : static long
    7221         952 : mf_same_k(GEN mf, GEN f) { return gequal(MF_get_gk(mf), mf_get_gk(f)); }
    7222             : static long
    7223         910 : mf_same_CHI(GEN mf, GEN f)
    7224             : {
    7225         910 :   GEN F1, F2, chi1, chi2, CHI1 = MF_get_CHI(mf), CHI2 = mf_get_CHI(f);
    7226             :   /* are the primitive chars attached to CHI1 and CHI2 equal ? */
    7227         910 :   F1 = znconreyconductor(gel(CHI1,1), gel(CHI1,2), &chi1);
    7228         910 :   if (typ(F1) == t_VEC) F1 = gel(F1,1);
    7229         910 :   F2 = znconreyconductor(gel(CHI2,1), gel(CHI2,2), &chi2);
    7230         910 :   if (typ(F2) == t_VEC) F2 = gel(F2,1);
    7231         910 :   return equalii(F1,F2) && ZV_equal(chi1,chi2);
    7232             : }
    7233             : /* check k and CHI rigorously, but not coefficients nor N */
    7234             : static long
    7235         231 : mfisinspace_i(GEN mf, GEN F)
    7236             : {
    7237         231 :   return mfistrivial(F) || (mf_same_k(mf,F) && mf_same_CHI(mf,F));
    7238             : }
    7239             : static void
    7240           7 : err_space(GEN F)
    7241           7 : { pari_err_DOMAIN("mftobasis", "form", "does not belong to",
    7242           0 :                   strtoGENstr("space"), F); }
    7243             : 
    7244             : static long
    7245         147 : mfcheapeisen(GEN mf)
    7246             : {
    7247         147 :   long k, L, N = MF_get_N(mf);
    7248             :   GEN P;
    7249         147 :   if (N <= 70) return 1;
    7250          84 :   k = itos(gceil(MF_get_gk(mf)));
    7251          84 :   if (odd(k)) k--;
    7252          84 :   switch (k)
    7253             :   {
    7254           0 :     case 2:  L = 190; break;
    7255          14 :     case 4:  L = 162; break;
    7256          70 :     case 6:
    7257          70 :     case 8:  L = 88; break;
    7258           0 :     case 10: L = 78; break;
    7259           0 :     default: L = 66; break;
    7260             :   }
    7261          84 :   P = gel(myfactoru(N), 1);
    7262          84 :   return P[lg(P)-1] <= L;
    7263             : }
    7264             : 
    7265             : static GEN
    7266         182 : myimag_i(GEN tau)
    7267             : {
    7268         182 :   long tc = typ(tau);
    7269         182 :   if (tc == t_INFINITY || tc == t_INT || tc == t_FRAC)
    7270          28 :     return gen_1;
    7271         154 :   if (tc == t_VEC)
    7272             :   {
    7273             :     long ltau, i;
    7274           7 :     GEN z = cgetg_copy(tau, &ltau);
    7275          42 :     for (i=1; i<ltau; i++) gel(z,i) = myimag_i(gel(tau,i));
    7276           7 :     return z;
    7277             :   }
    7278         147 :   return imag_i(tau);
    7279             : }
    7280             : 
    7281             : static GEN
    7282         147 : mintau(GEN vtau)
    7283             : {
    7284         147 :   if (!is_vec_t(typ(vtau))) return myimag_i(vtau);
    7285           7 :   return (lg(vtau) == 1)? gen_1: vecmin(myimag_i(vtau));
    7286             : }
    7287             : 
    7288             : /* initialization for mfgaexpansion: what does not depend on cusp */
    7289             : static GEN
    7290         945 : mf_eisendec(GEN mf, GEN F, long prec)
    7291             : {
    7292         945 :   GEN B = liftpol_shallow(mfeisensteindec(mf, F)), v = variables_vecsmall(B);
    7293         945 :   GEN Mvecj = obj_check(mf, MF_EISENSPACE);
    7294         945 :   long l = lg(v), i, ord;
    7295         945 :   if (lg(Mvecj) < 5) Mvecj = gel(Mvecj,1);
    7296         945 :   ord = itou(gel(Mvecj,4));
    7297        1001 :   for (i = 1; i < l; i++)
    7298         714 :     if (v[i] != 1)
    7299             :     {
    7300             :       GEN d;
    7301             :       long e;
    7302         658 :       B = Q_remove_denom(B, &d);
    7303         658 :       e = gexpo(B);
    7304         658 :       if (e > 0) prec += nbits2prec(e);
    7305         658 :       B = gsubst(B, v[i], rootsof1u_cx(ord, prec));
    7306         658 :       if (d) B = gdiv(B, d);
    7307         658 :       break;
    7308             :     }
    7309         945 :   return B;
    7310             : }
    7311             : 
    7312             : GEN
    7313         161 : mfeval(GEN mf0, GEN F, GEN vtau, long bitprec)
    7314             : {
    7315         161 :   pari_sp av = avma;
    7316         161 :   long flnew = 1;
    7317         161 :   GEN mf = checkMF_i(mf0);
    7318         161 :   if (!mf) pari_err_TYPE("mfeval", mf0);
    7319         161 :   if (!checkmf_i(F)) pari_err_TYPE("mfeval", F);
    7320         161 :   if (!mfisinspace_i(mf, F)) err_space(F);
    7321         161 :   if (!obj_check(mf, MF_EISENSPACE)) flnew = mfcheapeisen(mf);
    7322         161 :   if (flnew && gcmpgs(gmulsg(2*MF_get_N(mf), mintau(vtau)), 1) >= 0) flnew = 0;
    7323         161 :   return gerepilecopy(av, mfeval_i(mf, F, vtau, flnew, bitprec));
    7324             : }
    7325             : 
    7326             : static long
    7327         189 : val(GEN v, long bit)
    7328             : {
    7329         189 :   long c, l = lg(v);
    7330         392 :   for (c = 1; c < l; c++)
    7331         378 :     if (gexpo(gel(v,c)) > -bit) return c-1;
    7332          14 :   return -1;
    7333             : }
    7334             : GEN
    7335         203 : mfcuspval(GEN mf, GEN F, GEN cusp, long bitprec)
    7336             : {
    7337         203 :   pari_sp av = avma;
    7338         203 :   long lvE, w, N, sb, n, A, C, prec = nbits2prec(bitprec);
    7339             :   GEN ga, gk, vE;
    7340         203 :   mf = checkMF(mf);
    7341         203 :   if (!checkmf_i(F)) pari_err_TYPE("mfcuspval",F);
    7342         203 :   N = MF_get_N(mf);
    7343         203 :   cusp_canon(cusp, N, &A, &C);
    7344         203 :   gk = mf_get_gk(F);
    7345         203 :   if (typ(gk) != t_INT)
    7346             :   {
    7347          42 :     GEN FT = mfmultheta(F), mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7348          42 :     GEN r = mfcuspval(mf2, FT, cusp, bitprec);
    7349          42 :     if ((C & 3L) == 2)
    7350             :     {
    7351          14 :       GEN z = sstoQ(1,4);
    7352          14 :       r = gsub(r, typ(r) == t_VEC? const_vec(lg(r)-1, z): z);
    7353             :     }
    7354          42 :     return gerepileupto(av, r);
    7355             :   }
    7356         161 :   vE = mfgetembed(F, prec);
    7357         161 :   lvE = lg(vE);
    7358         161 :   w = mfcuspcanon_width(N, C);
    7359         161 :   sb = w * mfsturmNk(N, itos(gk));
    7360         161 :   ga = cusp2mat(A,C);
    7361         168 :   for (n = 8;; n = minss(sb, n << 1))
    7362           7 :   {
    7363         168 :     GEN R = mfgaexpansion(mf, F, ga, n, prec), res = liftpol_shallow(gel(R,3));
    7364         168 :     GEN v = cgetg(lvE-1, t_VECSMALL);
    7365         168 :     long j, ok = 1;
    7366         168 :     res = RgC_embedall(res, vE);
    7367         357 :     for (j = 1; j < lvE; j++)
    7368             :     {
    7369         189 :       v[j] = val(gel(res,j), bitprec/2);
    7370         189 :       if (v[j] < 0) ok = 0;
    7371             :     }
    7372         168 :     if (ok)
    7373             :     {
    7374         154 :       res = cgetg(lvE, t_VEC);
    7375         329 :       for (j = 1; j < lvE; j++) gel(res,j) = gadd(gel(R,1), sstoQ(v[j], w));
    7376         154 :       return gerepilecopy(av, lvE==2? gel(res,1): res);
    7377             :     }
    7378          14 :     if (n == sb) return lvE==2? mkoo(): const_vec(lvE-1, mkoo()); /* 0 */
    7379             :   }
    7380             : }
    7381             : 
    7382             : long
    7383         217 : mfiscuspidal(GEN mf, GEN F)
    7384             : {
    7385         217 :   pari_sp av = avma;
    7386             :   GEN mf2;
    7387         217 :   if (space_is_cusp(MF_get_space(mf))) return 1;
    7388          91 :   if (typ(mf_get_gk(F)) == t_INT)
    7389             :   {
    7390          56 :     GEN v = mftobasis(mf,F,0), vE = vecslice(v, 1, lg(MF_get_E(mf))-1);
    7391          56 :     return gc_long(av, gequal0(vE));
    7392             :   }
    7393          35 :   if (!gequal0(mfak_i(F, 0))) return 0;
    7394          21 :   mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7395          21 :   return mfiscuspidal(mf2, mfmultheta(F));
    7396             : }
    7397             : 
    7398             : /* F = vector of newforms in mftobasis format */
    7399             : static GEN
    7400          91 : mffrickeeigen_i(GEN mf, GEN F, GEN vE, long prec)
    7401             : {
    7402          91 :   GEN M, Z, L0, gN = MF_get_gN(mf), gk = MF_get_gk(mf);
    7403          91 :   long N0, i, lM, bit = prec2nbits(prec), k = itou(gk);
    7404          91 :   long LIM = 5; /* Sturm bound is enough */
    7405             : 
    7406          91 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7407          91 : START:
    7408          91 :   N0 = lfunthetacost(L0, gen_1, LIM, bit);
    7409          91 :   M = mfcoefs_mf(mf, N0, 1);
    7410          91 :   lM = lg(F);
    7411          91 :   Z = cgetg(lM, t_VEC);
    7412         259 :   for (i = 1; i < lM; i++)
    7413             :   { /* expansion of D * F[i] */
    7414         168 :     GEN D, z, van = RgM_RgC_mul(M, Q_remove_denom(gel(F,i), &D));
    7415         168 :     GEN L = van_embedall(van, gel(vE,i), gN, gk);
    7416         168 :     long l = lg(L), j, bit_add = D? expi(D): 0;
    7417         168 :     gel(Z,i) = z = cgetg(l, t_VEC);
    7418         511 :     for (j = 1; j < l; j++)
    7419             :     {
    7420             :       GEN v, C, C0;
    7421             :       long m, e;
    7422         476 :       for (m = 0; m <= LIM; m++)
    7423             :       {
    7424         476 :         v = lfuntheta(gmael(L,j,2), gen_1, m, bit);
    7425         476 :         if (gexpo(v) > bit_add - bit/2) break;
    7426             :       }
    7427         343 :       if (m > LIM) { LIM <<= 1; goto START; }
    7428         343 :       C = mulcxpowIs(gdiv(v,conj_i(v)), 2*m - k);
    7429         343 :       C0 = grndtoi(C, &e); if (e < 5-bit_accuracy(precision(C))) C = C0;
    7430         343 :       gel(z,j) = C;
    7431             :     }
    7432             :   }
    7433          91 :   return Z;
    7434             : }
    7435             : static GEN
    7436          70 : mffrickeeigen(GEN mf, GEN vE, long prec)
    7437             : {
    7438          70 :   GEN D = obj_check(mf, MF_FRICKE);
    7439          70 :   if (D) { long p = gprecision(D); if (!p || p >= prec) return D; }
    7440          63 :   D = mffrickeeigen_i(mf, MF_get_newforms(mf), vE, prec);
    7441          63 :   return obj_insert(mf, MF_FRICKE, D);
    7442             : }
    7443             : 
    7444             : /* integral weight, new space for primitive quadratic character CHIP;
    7445             :  * MF = vector of embedded eigenforms coefs on mfbasis, by orbit.
    7446             :  * Assume N > Q > 1 and (Q,f(CHIP)) = 1 */
    7447             : static GEN
    7448          56 : mfatkineigenquad(GEN mf, GEN CHIP, long Q, GEN MF, long bitprec)
    7449             : {
    7450             :   GEN L0, la2, S, F, vP, tau, wtau, Z, va, vb, den, coe, sqrtQ, sqrtN;
    7451          56 :   GEN M, gN, gk = MF_get_gk(mf);
    7452          56 :   long N0, x, yq, i, j, lF, dim, muQ, prec = nbits2prec(bitprec);
    7453          56 :   long N = MF_get_N(mf), k = itos(gk), NQ = N / Q;
    7454             : 
    7455             :   /* Q coprime to FC */
    7456          56 :   F = MF_get_newforms(mf);
    7457          56 :   vP = MF_get_fields(mf);
    7458          56 :   lF = lg(F);
    7459          56 :   Z = cgetg(lF, t_VEC);
    7460          56 :   S = MF_get_S(mf); dim = lg(S) - 1;
    7461          56 :   muQ = mymoebiusu(Q);
    7462          56 :   if (muQ)
    7463             :   {
    7464          42 :     GEN SQ = cgetg(dim+1,t_VEC), Qk = gpow(stoi(Q), sstoQ(k-2, 2), prec);
    7465          42 :     long i, bit2 = bitprec >> 1;
    7466         154 :     for (j = 1; j <= dim; j++) gel(SQ,j) = mfak_i(gel(S,j), Q);
    7467          84 :     for (i = 1; i < lF; i++)
    7468             :     {
    7469          42 :       GEN S = RgV_dotproduct(gel(F,i), SQ), T = gel(vP,i);
    7470             :       long e;
    7471          42 :       if (degpol(T) > 1 && typ(S) != t_POLMOD) S = gmodulo(S, T);
    7472          42 :       S = grndtoi(gdiv(conjvec(S, prec), Qk), &e);
    7473          42 :       if (e > -bit2) pari_err_PREC("mfatkineigenquad");
    7474          42 :       if (muQ == -1) S = gneg(S);
    7475          42 :       gel(Z,i) = S;
    7476             :     }
    7477          42 :     return Z;
    7478             :   }
    7479          14 :   la2 = mfchareval(CHIP, Q); /* 1 or -1 */
    7480          14 :   (void)cbezout(Q, NQ, &x, &yq);
    7481          14 :   sqrtQ = sqrtr_abs(utor(Q,prec));
    7482          14 :   tau = mkcomplex(gadd(sstoQ(-1, NQ), ginv(utoi(1000))),
    7483             :                   divru(sqrtQ, N));
    7484          14 :   den = gaddgs(gmulsg(NQ, tau), 1);
    7485          14 :   wtau = gdiv(gsub(gmulsg(x, tau), sstoQ(yq, Q)), den);
    7486          14 :   coe = gpowgs(gmul(sqrtQ, den), k);
    7487             : 
    7488          14 :   sqrtN = sqrtr_abs(utor(N,prec));
    7489          14 :   tau  = mulcxmI(gmul(tau,  sqrtN));
    7490          14 :   wtau = mulcxmI(gmul(wtau, sqrtN));
    7491          14 :   gN = utoipos(N);
    7492          14 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7493          14 :   N0 = maxss(lfunthetacost(L0,real_i(tau), 0,bitprec),
    7494             :              lfunthetacost(L0,real_i(wtau),0,bitprec));
    7495          14 :   M = mfcoefs_mf(mf, N0, 1);
    7496          14 :   va = cgetg(dim+1, t_VEC);
    7497          14 :   vb = cgetg(dim+1, t_VEC);
    7498         105 :   for (j = 1; j <= dim; j++)
    7499             :   {
    7500          91 :     GEN L, v = vecslice(gel(M,j), 2, N0+1); /* remove a0 */
    7501          91 :     settyp(v, t_VEC); L = mfthetaancreate(v, gN, gk);
    7502          91 :     gel(va,j) = lfuntheta(L, tau,0,bitprec);
    7503          91 :     gel(vb,j) = lfuntheta(L,wtau,0,bitprec);
    7504             :   }
    7505          84 :   for (i = 1; i < lF; i++)
    7506             :   {
    7507          70 :     GEN z, FE = gel(MF,i);
    7508          70 :     long l = lg(FE);
    7509          70 :     z = cgetg(l, t_VEC);
    7510          70 :     for (j = 1; j < l; j++)
    7511             :     {
    7512          70 :       GEN f = gel(FE,j), a = RgV_dotproduct(va,f), b = RgV_dotproduct(vb,f);
    7513          70 :       GEN la = ground( gdiv(b, gmul(a,coe)) );
    7514          70 :       if (!gequal(gsqr(la), la2)) pari_err_PREC("mfatkineigenquad");
    7515          70 :       if (typ(la) == t_INT)
    7516             :       {
    7517          70 :         if (j != 1) pari_err_BUG("mfatkineigenquad");
    7518          70 :         z = const_vec(l-1, la); break;
    7519             :       }
    7520           0 :       gel(z,j) = la;
    7521             :     }
    7522          70 :     gel(Z,i) = z;
    7523             :   }
    7524          14 :   return Z;
    7525             : }
    7526             : 
    7527             : static GEN
    7528          84 : myusqrt(ulong a, long prec)
    7529             : {
    7530          84 :   if (a == 1UL) return gen_1;
    7531          70 :   if (uissquareall(a, &a)) return utoipos(a);
    7532          49 :   return sqrtr_abs(utor(a, prec));
    7533             : }
    7534             : /* Assume mf is a non-trivial new space, rational primitive character CHIP
    7535             :  * and (Q,FC) = 1 */
    7536             : static GEN
    7537          98 : mfatkinmatnewquad(GEN mf, GEN CHIP, long Q, long flag, long PREC)
    7538             : {
    7539          98 :   GEN cM, M, D, MF, den, vE, F = MF_get_newforms(mf);
    7540          98 :   long i, c, e, prec, bitprec, lF = lg(F), N = MF_get_N(mf), k = MF_get_k(mf);
    7541             : 
    7542          98 :   if (Q == 1) return mkvec4(gen_0, matid(MF_get_dim(mf)), gen_1, mf);
    7543          98 :   den = gel(MF_get_Minv(mf), 2);
    7544          98 :   bitprec = expi(den) + 64;
    7545          98 :   if (!flag) bitprec = maxss(bitprec, prec2nbits(PREC));
    7546             : 
    7547          28 : START:
    7548          98 :   prec = nbits2prec(bitprec);
    7549          98 :   vE = mfeigenembed(mf, prec);
    7550          98 :   M = cgetg(lF, t_VEC);
    7551         266 :   for (i = 1; i < lF; i++) gel(M,i) = RgC_embedall(gel(F,i), gel(vE,i));
    7552          98 :   if (Q != N)
    7553             :   {
    7554          56 :     D = mfatkineigenquad(mf, CHIP, Q, M, bitprec);
    7555          56 :     c = odd(k)? Q: 1;
    7556             :   }
    7557             :   else
    7558             :   {
    7559          42 :     D = mffrickeeigen(mf, vE, DEFAULTPREC);
    7560          42 :     c = mfcharmodulus(CHIP); if (odd(k)) c = -Q/c;
    7561             :   }
    7562          98 :   D = shallowconcat1(D);
    7563          98 :   if (vec_isconst(D)) { MF = diagonal_shallow(D); flag = 0; }
    7564             :   else
    7565             :   {
    7566          63 :     M = shallowconcat1(M);
    7567          63 :     MF = RgM_mul(matmuldiagonal(M,D), ginv(M));
    7568             :   }
    7569          98 :   if (!flag) return mkvec4(gen_0, MF, gen_1, mf);
    7570             : 
    7571          21 :   if (c > 0)
    7572          21 :     cM = myusqrt(c, PREC);
    7573             :   else
    7574             :   {
    7575           0 :     MF = imag_i(MF); c = -c;
    7576           0 :     cM = mkcomplex(gen_0, myusqrt(c,PREC));
    7577             :   }
    7578          21 :   if (c != 1) MF = RgM_Rg_mul(MF, myusqrt(c,prec));
    7579          21 :   MF = grndtoi(RgM_Rg_mul(MF,den), &e);
    7580          21 :   if (e > -32) { bitprec <<= 1; goto START; }
    7581          21 :   MF = RgM_Rg_div(MF, den);
    7582          21 :   if (is_rational_t(typ(cM)) && !isint1(cM))
    7583           0 :   { MF = RgM_Rg_div(MF, cM); cM = gen_1; }
    7584          21 :   return mkvec4(gen_0, MF, cM, mf);
    7585             : }
    7586             : 
    7587             : /* let CHI mod N, Q || N, return \bar{CHI_Q} * CHI_{N/Q} */
    7588             : static GEN
    7589          91 : mfcharAL(GEN CHI, long Q)
    7590             : {
    7591          91 :   GEN G = gel(CHI,1), c = gel(CHI,2), cycc, d, P, E, F;
    7592          91 :   long l = lg(c), N = mfcharmodulus(CHI), i;
    7593          91 :   if (N == Q) return mfcharconj(CHI);
    7594          42 :   if (N == 1) return CHI;
    7595          42 :   CHI = leafcopy(CHI);
    7596          42 :   gel(CHI,2) = d = leafcopy(c);
    7597          42 :   F = znstar_get_faN(G);
    7598          42 :   P = gel(F,1);
    7599          42 :   E = gel(F,2);
    7600          42 :   cycc = znstar_get_conreycyc(G);
    7601          42 :   if (!odd(Q) && equaliu(gel(P,1), 2) && E[1] >= 3)
    7602          14 :     gel(d,2) = Fp_neg(gel(d,2), gel(cycc,2));
    7603          56 :   else for (i = 1; i < l; i++)
    7604          28 :     if (!umodui(Q, gel(P,i))) gel(d,i) = Fp_neg(gel(d,i), gel(cycc,i));
    7605          42 :   return CHI;
    7606             : }
    7607             : static long
    7608         210 : atkin_get_NQ(long N, long Q, const char *f)
    7609             : {
    7610         210 :   long NQ = N / Q;
    7611         210 :   if (N % Q) pari_err_DOMAIN(f,"N % Q","!=",gen_0,utoi(Q));
    7612         210 :   if (ugcd(NQ, Q) > 1) pari_err_DOMAIN(f,"gcd(Q,N/Q)","!=",gen_1,utoi(Q));
    7613         210 :   return NQ;
    7614             : }
    7615             : 
    7616             : /* transform mf to new_NEW if possible */
    7617             : static GEN
    7618        1267 : MF_set_new(GEN mf)
    7619             : {
    7620        1267 :   GEN vMjd, vj, gk = MF_get_gk(mf);
    7621             :   long l, j;
    7622        1267 :   if (MF_get_space(mf) != mf_CUSP
    7623        1267 :       || typ(gk) != t_INT || itou(gk) == 1) return mf;
    7624         175 :   vMjd = MFcusp_get_vMjd(mf); l = lg(vMjd);
    7625         175 :   if (l > 1 && gel(vMjd,1)[1] != MF_get_N(mf)) return mf; /* oldspace != 0 */
    7626         168 :   mf = shallowcopy(mf);
    7627         168 :   gel(mf,1) = shallowcopy(gel(mf,1));
    7628         168 :   MF_set_space(mf, mf_NEW);
    7629         168 :   vj = cgetg(l, t_VECSMALL);
    7630         917 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd, j)[2];
    7631         168 :   gel(mf,4) = vj; return mf;
    7632             : }
    7633             : 
    7634             : /* if flag = 1, rationalize, else don't */
    7635             : static GEN
    7636         189 : mfatkininit_i(GEN mf, long Q, long flag, long prec)
    7637             : {
    7638             :   GEN M, B, C, CHI, CHIAL, G, chi, P, z, g, mfB, s, Mindex, Minv;
    7639         189 :   long j, l, lim, ord, FC, NQ, cQ, nk, dk, N = MF_get_N(mf);
    7640             : 
    7641         189 :   B = MF_get_basis(mf); l = lg(B);
    7642         189 :   M = cgetg(l, t_MAT); if (l == 1) return mkvec4(gen_0,M,gen_1,mf);
    7643         189 :   Qtoss(MF_get_gk(mf), &nk,&dk);
    7644         189 :   Q = labs(Q);
    7645         189 :   NQ = atkin_get_NQ(N, Q, "mfatkininit");
    7646         189 :   CHI = MF_get_CHI(mf);
    7647         189 :   CHI = mfchartoprimitive(CHI, &FC);
    7648         189 :   ord = mfcharorder(CHI);
    7649         189 :   mf = MF_set_new(mf);
    7650         189 :   if (MF_get_space(mf) == mf_NEW && ord <= 2 && NQ % FC == 0 && dk == 1)
    7651          98 :     return mfatkinmatnewquad(mf, CHI, Q, flag, prec);
    7652             :   /* now flag != 0 */
    7653          91 :   G   = gel(CHI,1);
    7654          91 :   chi = gel(CHI,2);
    7655          91 :   if (Q == N) { g = mkmat22s(0, -1, N, 0); cQ = NQ; } /* Fricke */
    7656             :   else
    7657             :   {
    7658          28 :     GEN F, gQP = utoi(ugcd(Q, FC));
    7659             :     long t, v;
    7660          28 :     chi = znchardecompose(G, chi, gQP);
    7661          28 :     F = znconreyconductor(G, chi, &chi);
    7662          28 :     G = znstar0(F,1);
    7663          28 :     (void)cbezout(Q, NQ, &t, &v);
    7664          28 :     g = mkmat22s(Q*t, 1, -N*v, Q);
    7665          28 :     cQ = -NQ*v;
    7666             :   }
    7667          91 :   C = s = gen_1;
    7668             :   /* N.B. G,chi are G_Q,chi_Q [primitive] at this point */
    7669          91 :   if (lg(chi) != 1) C = ginv( znchargauss(G, chi, gen_1, prec2nbits(prec)) );
    7670          91 :   if (dk == 1)
    7671          84 :   { if (odd(nk)) s = myusqrt(Q,prec); }
    7672             :   else
    7673             :   {
    7674           7 :     long r = nk >> 1; /* k-1/2 */
    7675           7 :     s = gpow(utoipos(Q), mkfracss(odd(r)? 1: 3, 4), prec);
    7676           7 :     if (odd(cQ))
    7677             :     {
    7678           7 :       long t = r + ((cQ-1) >> 1);
    7679           7 :       s = mkcomplex(s, odd(t)? gneg(s): s);
    7680             :     }
    7681             :   }
    7682          91 :   if (!isint1(s)) C = gmul(C, s);
    7683          91 :   CHIAL = mfcharAL(CHI, Q);
    7684          91 :   if (dk == 2)
    7685           7 :     CHIAL = mfcharmul(CHIAL, induce(gel(CHIAL,1), utoipos(odd(Q) ? Q<<2 : Q)));
    7686          91 :   CHIAL = mfchartoprimitive(CHIAL,NULL);
    7687          91 :   mfB = gequal(CHIAL,CHI)? mf: mfinit_Nndkchi(N,nk,dk,CHIAL,MF_get_space(mf),0);
    7688          91 :   Mindex = MF_get_Mindex(mfB);
    7689          91 :   Minv = MF_get_Minv(mfB);
    7690          91 :   P = z = NULL;
    7691          91 :   if (ord > 2) { P = mfcharpol(CHI); z = rootsof1u_cx(ord, prec); }
    7692          91 :   lim = maxss(mfsturm(mfB), mfsturm(mf)) + 1;
    7693         287 :   for (j = 1; j < l; j++)
    7694             :   {
    7695         196 :     GEN v = mfslashexpansion(mf, gel(B,j), g, lim, 0, NULL, prec+EXTRAPREC64);
    7696             :     long junk;
    7697         196 :     if (!isint1(C)) v = RgV_Rg_mul(v, C);
    7698         196 :     v = bestapprnf(v, P, z, prec);
    7699         196 :     v = vecpermute_partial(v, Mindex, &junk);
    7700         196 :     v = Minv_RgC_mul(Minv, v); /* cf mftobasis_i */
    7701         196 :     gel(M, j) = v;
    7702             :   }
    7703          91 :   if (is_rational_t(typ(C)) && !gequal1(C)) { M = gdiv(M, C); C = gen_1; }
    7704          91 :   if (mfB == mf) mfB = gen_0;
    7705          91 :   return mkvec4(mfB, M, C, mf);
    7706             : }
    7707             : GEN
    7708          77 : mfatkininit(GEN mf, long Q, long prec)
    7709             : {
    7710          77 :   pari_sp av = avma;
    7711          77 :   mf = checkMF(mf); return gerepilecopy(av, mfatkininit_i(mf, Q, 1, prec));
    7712             : }
    7713             : static void
    7714          56 : checkmfa(GEN z)
    7715             : {
    7716          56 :   if (typ(z) != t_VEC || lg(z) != 5 || typ(gel(z,2)) != t_MAT
    7717          56 :       || !checkMF_i(gel(z,4))
    7718          56 :       || (!isintzero(gel(z,1)) && !checkMF_i(gel(z,1))))
    7719           0 :     pari_err_TYPE("mfatkin [please apply mfatkininit()]",z);
    7720          56 : }
    7721             : 
    7722             : /* Apply atkin Q to closure F */
    7723             : GEN
    7724          56 : mfatkin(GEN mfa, GEN F)
    7725             : {
    7726          56 :   pari_sp av = avma;
    7727             :   GEN z, mfB, MQ, mf;
    7728          56 :   checkmfa(mfa);
    7729          56 :   mfB= gel(mfa,1);
    7730          56 :   MQ = gel(mfa,2);
    7731          56 :   mf = gel(mfa,4);
    7732          56 :   if (typ(mfB) == t_INT) mfB = mf;
    7733          56 :   z = RgM_RgC_mul(MQ, mftobasis_i(mf,F));
    7734          56 :   return gerepileupto(av, mflinear(mfB, z));
    7735             : }
    7736             : 
    7737             : GEN
    7738          49 : mfatkineigenvalues(GEN mf, long Q, long prec)
    7739             : {
    7740          49 :   pari_sp av = avma;
    7741             :   GEN vF, L, CHI, M, mfatk, C, MQ, vE, mfB;
    7742             :   long N, NQ, l, i;
    7743             : 
    7744          49 :   mf = checkMF(mf); N = MF_get_N(mf);
    7745          49 :   vF = MF_get_newforms(mf); l = lg(vF);
    7746             :   /* N.B. k is integral */
    7747          49 :   if (l == 1) { set_avma(av); return cgetg(1, t_VEC); }
    7748          49 :   L = cgetg(l, t_VEC);
    7749          49 :   if (Q == 1)
    7750             :   {
    7751           7 :     GEN vP = MF_get_fields(mf);
    7752          21 :     for (i = 1; i < l; i++) gel(L,i) = const_vec(degpol(gel(vP,i)), gen_1);
    7753           7 :     return L;
    7754             :   }
    7755          42 :   vE = mfeigenembed(mf,prec);
    7756          42 :   if (Q == N) return gerepileupto(av, mffrickeeigen(mf, vE, prec));
    7757          21 :   Q = labs(Q);
    7758          21 :   NQ = atkin_get_NQ(N, Q, "mfatkineigenvalues"); /* != 1 */
    7759          21 :   mfatk = mfatkininit(mf, Q, prec);
    7760          21 :   mfB= gel(mfatk,1); if (typ(mfB) != t_VEC) mfB = mf;
    7761          21 :   MQ = gel(mfatk,2);
    7762          21 :   C  = gel(mfatk,3);
    7763          21 :   M = row(mfcoefs_mf(mfB,1,1), 2); /* vec of a_1(b_i) for mfbasis functions */
    7764          56 :   for (i = 1; i < l; i++)
    7765             :   {
    7766          35 :     GEN c = RgV_dotproduct(RgM_RgC_mul(MQ,gel(vF,i)), M); /* C * eigen_i */
    7767          35 :     gel(L,i) = Rg_embedall_i(c, gel(vE,i));
    7768             :   }
    7769          21 :   if (!gequal1(C)) L = gdiv(L, C);
    7770          21 :   CHI = MF_get_CHI(mf);
    7771          21 :   if (mfcharorder(CHI) <= 2 && NQ % mfcharconductor(CHI) == 0) L = ground(L);
    7772          21 :   return gerepilecopy(av, L);
    7773             : }
    7774             : 
    7775             : /* expand B_d V, keeping same length */
    7776             : static GEN
    7777        5852 : bdexpand(GEN V, long d)
    7778             : {
    7779             :   GEN W;
    7780             :   long N, n;
    7781        5852 :   if (d == 1) return V;
    7782        2121 :   N = lg(V)-1; W = zerovec(N);
    7783       42749 :   for (n = 0; n <= (N-1)/d; n++) gel(W, n*d+1) = gel(V, n+1);
    7784        2121 :   return W;
    7785             : }
    7786             : /* expand B_d V, increasing length up to lim */
    7787             : static GEN
    7788         287 : bdexpandall(GEN V, long d, long lim)
    7789             : {
    7790             :   GEN W;
    7791             :   long N, n;
    7792         287 :   if (d == 1) return V;
    7793          35 :   N = lg(V)-1; W = zerovec(lim);
    7794         259 :   for (n = 0; n <= N-1 && n*d <= lim; n++) gel(W, n*d+1) = gel(V, n+1);
    7795          35 :   return W;
    7796             : }
    7797             : 
    7798             : static void
    7799        8813 : parse_vecj(GEN T, GEN *E1, GEN *E2)
    7800             : {
    7801        8813 :   if (lg(T)==3) { *E1 = gel(T,1); *E2 = gel(T,2); }
    7802        4732 :   else { *E1 = T; *E2 = NULL; }
    7803        8813 : }
    7804             : 
    7805             : /* g in M_2(Z) ? */
    7806             : static int
    7807        2744 : check_M2Z(GEN g)
    7808        2744 : {  return typ(g) == t_MAT && lg(g) == 3 && lgcols(g) == 3 && RgM_is_ZM(g); }
    7809             : /* g in SL_2(Z) ? */
    7810             : static int
    7811        1673 : check_SL2Z(GEN g) { return check_M2Z(g) && equali1(ZM_det(g)); }
    7812             : 
    7813             : static GEN
    7814        9023 : mfcharcxeval(GEN CHI, long n, long prec)
    7815             : {
    7816        9023 :   ulong ord, N = mfcharmodulus(CHI);
    7817             :   GEN ordg;
    7818        9023 :   if (N == 1) return gen_1;
    7819        3696 :   if (ugcd(N, labs(n)) > 1) return gen_0;
    7820        3696 :   ordg = gmfcharorder(CHI);
    7821        3696 :   ord = itou(ordg);
    7822        3696 :   return rootsof1q_cx(znchareval_i(CHI,n,ordg), ord, prec);
    7823             : }
    7824             : 
    7825             : static GEN
    7826        4837 : RgV_shift(GEN V, GEN gn)
    7827             : {
    7828             :   long i, n, l;
    7829             :   GEN W;
    7830        4837 :   if (typ(gn) != t_INT) pari_err_BUG("RgV_shift [n not integral]");
    7831        4837 :   n = itos(gn);
    7832        4837 :   if (n < 0) pari_err_BUG("RgV_shift [n negative]");
    7833        4837 :   if (!n) return V;
    7834         112 :   W = cgetg_copy(V, &l); if (n > l-1) n = l-1;
    7835         308 :   for (i=1; i <= n; i++) gel(W,i) = gen_0;
    7836        4900 :   for (    ; i < l; i++) gel(W,i) = gel(V, i-n);
    7837         112 :   return W;
    7838             : }
    7839             : static GEN
    7840        7483 : hash_eisengacx(hashtable *H, void *E, long w, GEN ga, long n, long prec)
    7841             : {
    7842        7483 :   ulong h = H->hash(E);
    7843        7483 :   hashentry *e = hash_search2(H, E, h);
    7844             :   GEN v;
    7845        7483 :   if (e) v = (GEN)e->val;
    7846             :   else
    7847             :   {
    7848        5012 :     v = mfeisensteingacx((GEN)E, w, ga, n, prec);
    7849        5012 :     hash_insert2(H, E, (void*)v, h);
    7850             :   }
    7851        7483 :   return v;
    7852             : }
    7853             : static GEN
    7854        4837 : vecj_expand(GEN B, hashtable *H, long w, GEN ga, long n, long prec)
    7855             : {
    7856             :   GEN E1, E2, v;
    7857        4837 :   parse_vecj(B, &E1, &E2);
    7858        4837 :   v = hash_eisengacx(H, (void*)E1, w, ga, n, prec);
    7859        4837 :   if (E2)
    7860             :   {
    7861        2590 :     GEN u = hash_eisengacx(H, (void*)E2, w, ga, n, prec);
    7862        2590 :     GEN a = gadd(gel(v,1), gel(u,1));
    7863        2590 :     GEN b = RgV_mul_RgXn(gel(v,2), gel(u,2));
    7864        2590 :     v = mkvec2(a,b);
    7865             :   }
    7866        4837 :   return v;
    7867             : }
    7868             : static GEN
    7869        1008 : shift_M(GEN M, GEN Valpha, long w)
    7870             : {
    7871        1008 :   long i, l = lg(Valpha);
    7872        1008 :   GEN almin = vecmin(Valpha);
    7873        5845 :   for (i = 1; i < l; i++)
    7874             :   {
    7875        4837 :     GEN alpha = gel(Valpha, i), gsh = gmulsg(w, gsub(alpha,almin));
    7876        4837 :     gel(M,i) = RgV_shift(gel(M,i), gsh);
    7877             :   }
    7878        1008 :   return almin;
    7879             : }
    7880             : static GEN mfeisensteinspaceinit(GEN NK);
    7881             : #if 0
    7882             : /* ga in M_2^+(Z)), n >= 0 */
    7883             : static GEN
    7884             : mfgaexpansion_init(GEN mf, GEN ga, long n, long prec)
    7885             : {
    7886             :   GEN M, Mvecj, vecj, almin, Valpha;
    7887             :   long i, w, l, N = MF_get_N(mf), c = itos(gcoeff(ga,2,1));
    7888             :   hashtable *H;
    7889             : 
    7890             :   if (c % N == 0)
    7891             :   { /* ga in G_0(N), trivial case; w = 1 */
    7892             :     GEN chid = mfcharcxeval(MF_get_CHI(mf), itos(gcoeff(ga,2,2)), prec);
    7893             :     return mkvec2(chid, utoi(n));
    7894             :   }
    7895             : 
    7896             :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7897             :   if (lg(Mvecj) < 5) pari_err_IMPL("mfgaexpansion_init in this case");
    7898             :   w = mfcuspcanon_width(N, c);
    7899             :   vecj = gel(Mvecj, 3);
    7900             :   l = lg(vecj);
    7901             :   M = cgetg(l, t_VEC);
    7902             :   Valpha = cgetg(l, t_VEC);
    7903             :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7904             :                      (int(*)(void*,void*))&gidentical, 1);
    7905             :   for (i = 1; i < l; i++)
    7906             :   {
    7907             :     GEN v = vecj_expand(gel(vecj,i), H, w, ga, n, prec);
    7908             :     gel(Valpha,i) = gel(v,1);
    7909             :     gel(M,i) = gel(v,2);
    7910             :   }
    7911             :   almin = shift_M(M, Valpha, w);
    7912             :   return mkvec3(almin, utoi(w), M);
    7913             : }
    7914             : /* half-integer weight not supported; vF = [F,eisendec(F)].
    7915             :  * Minit = mfgaexpansion_init(mf, ga, n, prec) */
    7916             : static GEN
    7917             : mfgaexpansion_with_init(GEN Minit, GEN vF)
    7918             : {
    7919             :   GEN v;
    7920             :   if (lg(Minit) == 3)
    7921             :   { /* ga in G_0(N) */
    7922             :     GEN chid = gel(Minit,1), gn = gel(Minit,2);
    7923             :     v = mfcoefs_i(gel(vF,1), itou(gn), 1);
    7924             :     v = mkvec3(gen_0, gen_1, RgV_Rg_mul(v,chid));
    7925             :   }
    7926             :   else
    7927             :   {
    7928             :     GEN V = RgM_RgC_mul(gel(Minit,3), gel(vF,2));
    7929             :     v = mkvec3(gel(Minit,1), gel(Minit,2), V);
    7930             :   }
    7931             :   return v;
    7932             : }
    7933             : #endif
    7934             : 
    7935             : /* B = mfeisensteindec(F) already embedded, ga in M_2^+(Z)), n >= 0 */
    7936             : static GEN
    7937        1008 : mfgaexpansion_i(GEN mf, GEN B0, GEN ga, long n, long prec)
    7938             : {
    7939        1008 :   GEN M, Mvecj, vecj, almin, Valpha, B, E = NULL;
    7940        1008 :   long i, j, w, nw, l, N = MF_get_N(mf), bit = prec2nbits(prec) / 2;
    7941             :   hashtable *H;
    7942             : 
    7943        1008 :   Mvecj = obj_check(mf, MF_EISENSPACE);
    7944        1008 :   if (lg(Mvecj) < 5) { E = gel(Mvecj, 2); Mvecj = gel(Mvecj, 1); }
    7945        1008 :   vecj = gel(Mvecj, 3);
    7946        1008 :   l = lg(vecj);
    7947        1008 :   B = cgetg(l, t_COL);
    7948        1008 :   M = cgetg(l, t_VEC);
    7949        1008 :   Valpha = cgetg(l, t_VEC);
    7950        1008 :   w = mfZC_width(N, gel(ga,1));
    7951        1008 :   nw = E ? n + w : n;
    7952        1008 :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7953             :                      (int(*)(void*,void*))&gidentical, 1);
    7954        8743 :   for (i = j = 1; i < l; i++)
    7955             :   {
    7956             :     GEN v;
    7957        7735 :     if (gequal0(gel(B0,i))) continue;
    7958        4837 :     v = vecj_expand(gel(vecj,i), H, w, ga, nw, prec);
    7959        4837 :     gel(B,j) = gel(B0,i);
    7960        4837 :     gel(Valpha,j) = gel(v,1);
    7961        4837 :     gel(M,j) = gel(v,2); j++;
    7962             :   }
    7963        1008 :   setlg(Valpha, j);
    7964        1008 :   setlg(B, j);
    7965        1008 :   setlg(M, j); l = j;
    7966        1008 :   if (l == 1) return mkvec3(gen_0, utoi(w), zerovec(n+1));
    7967        1008 :   almin = shift_M(M, Valpha, w);
    7968        1008 :   B = RgM_RgC_mul(M, B); l = lg(B);
    7969      147602 :   for (i = 1; i < l; i++)
    7970      146594 :     if (gexpo(gel(B,i)) < -bit) gel(B,i) = gen_0;
    7971        1008 :   settyp(B, t_VEC);
    7972        1008 :   if (E)
    7973             :   {
    7974             :     GEN v, e;
    7975          56 :     long ell = 0, vB, ve;
    7976         126 :     for (i = 1; i < l; i++)
    7977         126 :       if (!gequal0(gel(B,i))) break;
    7978          56 :     vB = i-1;
    7979          56 :     v = hash_eisengacx(H, (void*)E, w, ga, n + vB, prec);
    7980          56 :     e = gel(v,2); l = lg(e);
    7981          56 :     for (i = 1; i < l; i++)
    7982          56 :       if (!gequal0(gel(e,i))) break;
    7983          56 :     ve = i-1;
    7984          56 :     almin = gsub(almin, gel(v,1));
    7985          56 :     if (gsigne(almin) < 0)
    7986             :     {
    7987           0 :       GEN gell = gceil(gmulsg(-w, almin));
    7988           0 :       ell = itos(gell);
    7989           0 :       almin = gadd(almin, gdivgs(gell, w));
    7990           0 :       if (nw < ell) pari_err_IMPL("alpha < 0 in mfgaexpansion");
    7991             :     }
    7992          56 :     if (ve) { ell += ve; e = vecslice(e, ve+1, l-1); }
    7993          56 :     B = vecslice(B, ell + 1, minss(n + ell + 1, lg(B)-1));
    7994          56 :     B = RgV_div_RgXn(B, e);
    7995             :   }
    7996        1008 :   return mkvec3(almin, utoi(w), B);
    7997             : }
    7998             : 
    7999             : /* Theta multiplier: assume 4 | C, (C,D)=1 */
    8000             : static GEN
    8001         301 : mfthetamultiplier(GEN C, GEN D)
    8002             : {
    8003         301 :   long s = kronecker(C, D);
    8004         301 :   if (Mod4(D) == 1) return s > 0 ? gen_1: gen_m1;
    8005          84 :   return s > 0? powIs(3): gen_I();
    8006             : }
    8007             : /* theta | [*,*;C,D] defined over Q(i) [else over Q] */
    8008             : static int
    8009          56 : mfthetaI(long C, long D) { return odd(C) || (D & 3) == 3; }
    8010             : /* (theta | M) [0..n], assume (C,D) = 1 */
    8011             : static GEN
    8012         301 : mfthetaexpansion(GEN M, long n)
    8013             : {
    8014         301 :   GEN w, s, al, sla, E, V = zerovec(n+1), C = gcoeff(M,2,1), D = gcoeff(M,2,2);
    8015         301 :   long lim, la, f, C4 = Mod4(C);
    8016         301 :   switch (C4)
    8017             :   {
    8018          70 :     case 0: al = gen_0; w = gen_1;
    8019          70 :       s = mfthetamultiplier(C,D);
    8020          70 :       lim = usqrt(n); gel(V, 1) = s;
    8021          70 :       s = gmul2n(s, 1);
    8022         756 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = s;
    8023          70 :       break;
    8024         105 :     case 2: al = sstoQ(1,4); w = gen_1;
    8025         105 :       E = subii(C, shifti(D,1)); /* (E, D) = 1 */
    8026         105 :       s = gmul2n(mfthetamultiplier(E, D), 1);
    8027         105 :       if ((!signe(E) && equalim1(D)) || (signe(E) > 0 && signe(C) < 0))
    8028          14 :         s = gneg(s);
    8029         105 :       lim = (usqrt(n << 2) - 1) >> 1;
    8030         966 :       for (f = 0; f <= lim; f++) gel(V, f*(f+1) + 1) = s;
    8031         105 :       break;
    8032         126 :     default: al = gen_0; w = utoipos(4);
    8033         126 :       la = (-Mod4(D)*C4) & 3L;
    8034         126 :       E = negi(addii(D, mului(la, C)));
    8035         126 :       s = mfthetamultiplier(E, C); /* (E,C) = 1 */
    8036         126 :       if (signe(C) < 0 && signe(E) >= 0) s = gneg(s);
    8037         126 :       s = gsub(s, mulcxI(s));
    8038         126 :       sla = gmul(s, powIs(-la));
    8039         126 :       lim = usqrt(n); gel(V, 1) = gmul2n(s, -1);
    8040        1624 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = odd(f) ? sla : s;
    8041         126 :       break;
    8042             :   }
    8043         301 :   return mkvec3(al, w, V);
    8044             : }
    8045             : 
    8046             : /* F 1/2 integral weight */
    8047             : static GEN
    8048         301 : mf2gaexpansion(GEN mf2, GEN F, GEN ga, long n, long prec)
    8049             : {
    8050         301 :   GEN FT = mfmultheta(F), mf = obj_checkbuild(mf2, MF_MF2INIT, &mf2init);
    8051         301 :   GEN res, V1, Tres, V2, al, V, gsh, C = gcoeff(ga,2,1);
    8052         301 :   long w2, N = MF_get_N(mf), w = mfcuspcanon_width(N, umodiu(C,N));
    8053         301 :   long ext = (Mod4(C) != 2)? 0: (w+3) >> 2;
    8054         301 :   long prec2 = prec + nbits2extraprec((long)M_PI/(2*M_LN2)*sqrt(n + ext));
    8055         301 :   res = mfgaexpansion(mf, FT, ga, n + ext, prec2);
    8056         301 :   Tres = mfthetaexpansion(ga, n + ext);
    8057         301 :   V1 = gel(res,3);
    8058         301 :   V2 = gel(Tres,3);
    8059         301 :   al = gsub(gel(res,1), gel(Tres,1));
    8060         301 :   w2 = itos(gel(Tres,2));
    8061         301 :   if (w != itos(gel(res,2)) || w % w2)
    8062           0 :     pari_err_BUG("mf2gaexpansion [incorrect w2 or w]");
    8063         301 :   if (w2 != w) V2 = bdexpand(V2, w/w2);
    8064         301 :   V = RgV_div_RgXn(V1, V2);
    8065         301 :   gsh = gfloor(gmulsg(w, al));
    8066         301 :   if (!gequal0(gsh))
    8067             :   {
    8068          35 :     al = gsub(al, gdivgs(gsh, w));
    8069          35 :     if (gsigne(gsh) > 0)
    8070             :     {
    8071           0 :       V = RgV_shift(V, gsh);
    8072           0 :       V = vecslice(V, 1, n + 1);
    8073             :     }
    8074             :     else
    8075             :     {
    8076          35 :       long sh = -itos(gsh), i;
    8077          35 :       if (sh > ext) pari_err_BUG("mf2gaexpansion [incorrect sh]");
    8078         154 :       for (i = 1; i <= sh; i++)
    8079         119 :         if (!gequal0(gel(V,i))) pari_err_BUG("mf2gaexpansion [sh too large]");
    8080          35 :       V = vecslice(V, sh+1, n + sh+1);
    8081             :     }
    8082             :   }
    8083         301 :   obj_free(mf); return mkvec3(al, stoi(w), gprec_wtrunc(V, prec));
    8084             : }
    8085             : 
    8086             : static GEN
    8087          70 : mfgaexpansionatkin(GEN mf, GEN F, GEN C, GEN D, long Q, long n, long prec)
    8088             : {
    8089          70 :   GEN mfa = mfatkininit_i(mf, Q, 0, prec), MQ = gel(mfa,2);
    8090          70 :   long i, FC, k = MF_get_k(mf);
    8091          70 :   GEN x, v, V, z, s, CHI = mfchartoprimitive(MF_get_CHI(mf), &FC);
    8092             : 
    8093             :   /* V = mfcoefs(F | w_Q, n), can't use mfatkin because MQ non-rational */
    8094          70 :   V = RgM_RgC_mul(mfcoefs_mf(mf,n,1), RgM_RgC_mul(MQ, mftobasis_i(mf,F)));
    8095          70 :   (void)bezout(utoipos(Q), C, &x, &v);
    8096          70 :   s = mfchareval(CHI, (umodiu(x, FC) * umodiu(D, FC)) % FC);
    8097          70 :   s = gdiv(s, gpow(utoipos(Q), sstoQ(k,2), prec));
    8098          70 :   V = RgV_Rg_mul(V, s);
    8099          70 :   z = rootsof1powinit(umodiu(D,Q)*umodiu(v,Q) % Q, Q, prec);
    8100        8253 :   for (i = 1; i <= n+1; i++) gel(V,i) = gmul(gel(V,i), rootsof1pow(z, i-1));
    8101          70 :   return mkvec3(gen_0, utoipos(Q), V);
    8102             : }
    8103             : 
    8104             : static long
    8105          70 : inveis_extraprec(long N, GEN ga, GEN Mvecj, long n)
    8106             : {
    8107          70 :   long e, w = mfZC_width(N, gel(ga,1));
    8108          70 :   GEN f, E = gel(Mvecj,2), v = mfeisensteingacx(E, w, ga, n, DEFAULTPREC);
    8109          70 :   v = gel(v,2);
    8110          70 :   f = RgV_to_RgX(v,0); n -= RgX_valrem(f, &f);
    8111          70 :   e = gexpo(RgXn_inv(f, n+1));
    8112          70 :   return (e > 0)? nbits2extraprec(e): 0;
    8113             : }
    8114             : /* allow F of the form [F, mf_eisendec(F)]~ */
    8115             : static GEN
    8116        1666 : mfgaexpansion(GEN mf, GEN F, GEN ga, long n, long prec)
    8117             : {
    8118        1666 :   GEN v, EF = NULL, res, Mvecj, c, d;
    8119             :   long precnew, N;
    8120             : 
    8121        1666 :   if (n < 0) pari_err_DOMAIN("mfgaexpansion", "n", "<", gen_0, stoi(n));
    8122        1666 :   if (typ(F) == t_COL && lg(F) == 3) { EF = gel(F,2); F = gel(F,1); }
    8123        1666 :   if (!checkmf_i(F)) pari_err_TYPE("mfgaexpansion", F);
    8124        1666 :   if (!check_SL2Z(ga)) pari_err_TYPE("mfgaexpansion",ga);
    8125        1666 :   if (typ(mf_get_gk(F)) != t_INT) return mf2gaexpansion(mf, F, ga, n, prec);
    8126        1365 :   c = gcoeff(ga,2,1);
    8127        1365 :   d = gcoeff(ga,2,2);
    8128        1365 :   N = MF_get_N(mf);
    8129        1365 :   if (!umodiu(c, mf_get_N(F)))
    8130             :   { /* trivial case: ga in Gamma_0(N) */
    8131         287 :     long w = mfcuspcanon_width(N, umodiu(c,N));
    8132         287 :     GEN CHI = mf_get_CHI(F);
    8133         287 :     GEN chid = mfcharcxeval(CHI, umodiu(d,mfcharmodulus(CHI)), prec);
    8134         287 :     v = mfcoefs_i(F, n/w, 1); if (!isint1(chid)) v = RgV_Rg_mul(v,chid);
    8135         287 :     return mkvec3(gen_0, stoi(w), bdexpandall(v,w,n+1));
    8136             :   }
    8137        1078 :   mf = MF_set_new(mf);
    8138        1078 :   if (MF_get_space(mf) == mf_NEW)
    8139             :   {
    8140         441 :     long cN = umodiu(c,N), g = ugcd(cN,N), Q = N/g;
    8141         441 :     GEN CHI = MF_get_CHI(mf);
    8142         441 :     if (ugcd(cN, Q)==1 && mfcharorder(CHI) <= 2
    8143         217 :                        && g % mfcharconductor(CHI) == 0
    8144         112 :                        && degpol(mf_get_field(F)) == 1)
    8145          70 :       return mfgaexpansionatkin(mf, F, c, d, Q, n, prec);
    8146             :   }
    8147        1008 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    8148        1008 :   precnew = prec;
    8149        1008 :   if (lg(Mvecj) < 5) precnew += inveis_extraprec(N, ga, Mvecj, n);
    8150        1008 :   if (!EF) EF = mf_eisendec(mf, F, precnew);
    8151        1008 :   res = mfgaexpansion_i(mf, EF, ga, n, precnew);
    8152        1008 :   return precnew == prec ? res : gprec_wtrunc(res, prec);
    8153             : }
    8154             : 
    8155             : /* parity = -1 or +1 */
    8156             : static GEN
    8157         217 : findd(long N, long parity)
    8158             : {
    8159         217 :   GEN L, D = mydivisorsu(N);
    8160         217 :   long i, j, l = lg(D);
    8161         217 :   L = cgetg(l, t_VEC);
    8162        1218 :   for (i = j = 1; i < l; i++)
    8163             :   {
    8164        1001 :     long d = D[i];
    8165        1001 :     if (parity == -1) d = -d;
    8166        1001 :     if (sisfundamental(d)) gel(L,j++) = stoi(d);
    8167             :   }
    8168         217 :   setlg(L,j); return L;
    8169             : }
    8170             : /* does ND contain a divisor of N ? */
    8171             : static int
    8172         413 : seenD(long N, GEN ND)
    8173             : {
    8174         413 :   long j, l = lg(ND);
    8175         427 :   for (j = 1; j < l; j++)
    8176          14 :     if (N % ND[j] == 0) return 1;
    8177         413 :   return 0;
    8178             : }
    8179             : static GEN
    8180          42 : search_levels(GEN vN, const char *f)
    8181             : {
    8182          42 :   switch(typ(vN))
    8183             :   {
    8184           7 :     case t_INT: vN = mkvecsmall(itos(vN)); break;
    8185          35 :     case t_VEC: case t_COL: vN = ZV_to_zv(vN); break;
    8186           0 :     case t_VECSMALL: vN = leafcopy(vN); break;
    8187           0 :     default: pari_err_TYPE(f, vN);
    8188             :   }
    8189          42 :   vecsmall_sort(vN); return vN;
    8190             : }
    8191             : GEN
    8192          14 : mfsearch(GEN NK, GEN V, long space)
    8193             : {
    8194          14 :   pari_sp av = avma;
    8195             :   GEN F, gk, NbyD, vN;
    8196             :   long n, nk, dk, parity, nV, i, lvN;
    8197             : 
    8198          14 :   if (typ(NK) != t_VEC || lg(NK) != 3) pari_err_TYPE("mfsearch", NK);
    8199          14 :   gk = gel(NK,2);
    8200          14 :   if (typ(gmul2n(gk, 1)) != t_INT) pari_err_TYPE("mfsearch [k]", gk);
    8201          14 :   switch(typ(V))
    8202             :   {
    8203          14 :     case t_VEC: V = shallowtrans(V);
    8204          14 :     case t_COL: break;
    8205           0 :     default: pari_err_TYPE("mfsearch [V]", V);
    8206             :   }
    8207          14 :   vN = search_levels(gel(NK,1), "mfsearch [N]");
    8208          14 :   lvN = lg(vN);
    8209             : 
    8210          14 :   Qtoss(gk, &nk,&dk);
    8211          14 :   parity = (dk == 1 && odd(nk)) ? -1 : 1;
    8212          14 :   nV = lg(V)-2;
    8213          14 :   F = cgetg(1, t_VEC);
    8214          14 :   NbyD = const_vec(vN[lvN-1], cgetg(1,t_VECSMALL));
    8215         231 :   for (n = 1; n < lvN; n++)
    8216             :   {
    8217         217 :     long N = vN[n];
    8218             :     GEN L;
    8219         217 :     if (N <= 0 || (dk == 2 && (N & 3))) continue;
    8220         217 :     L = findd(N, parity);
    8221         630 :     for (i = 1; i < lg(L); i++)
    8222             :     {
    8223         413 :       GEN mf, M, CO, gD = gel(L,i);
    8224         413 :       GEN *ND = (GEN*)NbyD + itou(gD); /* points to NbyD[|D|] */
    8225             : 
    8226         413 :       if (seenD(N, *ND)) continue;
    8227         413 :       mf = mfinit_Nndkchi(N, nk, dk, get_mfchar(gD), space, 1);
    8228         413 :       M = mfcoefs_mf(mf, nV, 1);
    8229         413 :       CO = inverseimage(M, V); if (lg(CO) == 1) continue;
    8230             : 
    8231          42 :       F = vec_append(F, mflinear(mf,CO));
    8232          42 :       *ND = vecsmall_append(*ND, N); /* add to NbyD[|D|] */
    8233             :     }
    8234             :   }
    8235          14 :   return gerepilecopy(av, F);
    8236             : }
    8237             : 
    8238             : static GEN
    8239         882 : search_from_split(GEN mf, GEN vap, GEN vlp)
    8240             : {
    8241         882 :   pari_sp av = avma;
    8242         882 :   long lvlp = lg(vlp), j, jv, l1;
    8243         882 :   GEN v, NK, S1, S, M = NULL;
    8244             : 
    8245         882 :   S1 = gel(split_i(mf, 1, 0), 1); /* rational newforms */
    8246         882 :   l1 = lg(S1);
    8247         882 :   if (l1 == 1) return gc_NULL(av);
    8248         448 :   v = cgetg(l1, t_VEC);
    8249         448 :   S = MF_get_S(mf);
    8250         448 :   NK = mf_get_NK(gel(S,1));
    8251         448 :   if (lvlp > 1) M = rowpermute(mfcoefs_mf(mf, vlp[lvlp-1], 1), vlp);
    8252         966 :   for (j = jv = 1; j < l1; j++)
    8253             :   {
    8254         518 :     GEN vF = gel(S1,j);
    8255             :     long t;
    8256         651 :     for (t = lvlp-1; t > 0; t--)
    8257             :     { /* lhs = vlp[j]-th coefficient of eigenform */
    8258         595 :       GEN rhs = gel(vap,t), lhs = RgMrow_RgC_mul(M, vF, t);
    8259         595 :       if (!gequal(lhs, rhs)) break;
    8260             :     }
    8261         518 :     if (!t) gel(v,jv++) = mflinear_i(NK,S,vF);
    8262             :   }
    8263         448 :   if (jv == 1) return gc_NULL(av);
    8264          56 :   setlg(v,jv); return v;
    8265             : }
    8266             : GEN
    8267          28 : mfeigensearch(GEN NK, GEN AP)
    8268             : {
    8269          28 :   pari_sp av = avma;
    8270          28 :   GEN k, vN, vap, vlp, vres = cgetg(1, t_VEC), D;
    8271             :   long n, lvN, i, l, even;
    8272             : 
    8273          28 :   if (!AP) l = 1;
    8274             :   else
    8275             :   {
    8276          28 :     l = lg(AP);
    8277          28 :     if (typ(AP) != t_VEC) pari_err_TYPE("mfeigensearch",AP);
    8278             :   }
    8279          28 :   vap = cgetg(l, t_VEC);
    8280          28 :   vlp = cgetg(l, t_VECSMALL);
    8281          28 :   if (l > 1)
    8282             :   {
    8283          28 :     GEN perm = indexvecsort(AP, mkvecsmall(1));
    8284          77 :     for (i = 1; i < l; i++)
    8285             :     {
    8286          49 :       GEN v = gel(AP,perm[i]), gp, ap;
    8287          49 :       if (typ(v) != t_VEC || lg(v) != 3) pari_err_TYPE("mfeigensearch", AP);
    8288          49 :       gp = gel(v,1);
    8289          49 :       ap = gel(v,2);
    8290          49 :       if (typ(gp) != t_INT || (typ(ap) != t_INT && typ(ap) != t_INTMOD))
    8291           0 :         pari_err_TYPE("mfeigensearch", AP);
    8292          49 :       gel(vap,i) = ap;
    8293          49 :       vlp[i] = itos(gp)+1; if (vlp[i] < 0) pari_err_TYPE("mfeigensearch", AP);
    8294             :     }
    8295             :   }
    8296          28 :   l = lg(NK);
    8297          28 :   if (typ(NK) != t_VEC || l != 3) pari_err_TYPE("mfeigensearch",NK);
    8298          28 :   k = gel(NK,2);
    8299          28 :   vN = search_levels(gel(NK,1), "mfeigensearch [N]");
    8300          28 :   lvN = lg(vN);
    8301          28 :   vecsmall_sort(vlp);
    8302          28 :   even = !mpodd(k);
    8303         966 :   for (n = 1; n < lvN; n++)
    8304             :   {
    8305         938 :     pari_sp av2 = avma;
    8306             :     GEN mf, L;
    8307         938 :     long N = vN[n];
    8308         938 :     if (even) D = gen_1;
    8309             :     else
    8310             :     {
    8311         112 :       long r = (N&3L);
    8312         112 :       if (r == 1 || r == 2) continue;
    8313          56 :       D = stoi( corediscs(-N, NULL) ); /* < 0 */
    8314             :     }
    8315         882 :     mf = mfinit_i(mkvec3(utoipos(N), k, D), mf_NEW);
    8316         882 :     L = search_from_split(mf, vap, vlp);
    8317         882 :     if (L) vres = shallowconcat(vres, L); else set_avma(av2);
    8318             :   }
    8319          28 :   return gerepilecopy(av, vres);
    8320             : }
    8321             : 
    8322             : /* tf_{N,k}(n) */
    8323             : static GEN
    8324     3174332 : mfnewtracecache(long N, long k, long n, cachenew_t *cache)
    8325             : {
    8326     3174332 :   GEN C = NULL, S;
    8327             :   long lcache;
    8328     3174332 :   if (!n) return gen_0;
    8329     3070053 :   S = gel(cache->vnew,N);
    8330     3070053 :   lcache = lg(S);
    8331     3070053 :   if (n < lcache) C = gel(S, n);
    8332     3070053 :   if (C) cache->newHIT++;
    8333     1921031 :   else C = mfnewtrace_i(N,k,n,cache);
    8334     3070053 :   cache->newTOTAL++;
    8335     3070053 :   if (n < lcache) gel(S,n) = C;
    8336     3070053 :   return C;
    8337             : }
    8338             : 
    8339             : static long
    8340        1386 : mfdim_Nkchi(long N, long k, GEN CHI, long space)
    8341             : {
    8342        1386 :   if (k < 0 || badchar(N,k,CHI)) return 0;
    8343        1085 :   if (k == 0)
    8344          35 :     return mfcharistrivial(CHI) && !space_is_cusp(space)? 1: 0;
    8345        1050 :   switch(space)
    8346             :   {
    8347         238 :     case mf_NEW: return mfnewdim(N,k,CHI);
    8348         196 :     case mf_CUSP:return mfcuspdim(N,k,CHI);
    8349         168 :     case mf_OLD: return mfolddim(N,k,CHI);
    8350         217 :     case mf_FULL:return mffulldim(N,k,CHI);
    8351         231 :     case mf_EISEN: return mfeisensteindim(N,k,CHI);
    8352           0 :     default: pari_err_FLAG("mfdim");
    8353             :   }
    8354             :   return 0;/*LCOV_EXCL_LINE*/
    8355             : }
    8356             : static long
    8357        2114 : mfwt1dimsum(long N, long space)
    8358             : {
    8359        2114 :   switch(space)
    8360             :   {
    8361        1050 :     case mf_NEW:  return mfwt1newdimsum(N);
    8362        1057 :     case mf_CUSP: return mfwt1cuspdimsum(N);
    8363           7 :     case mf_OLD:  return mfwt1olddimsum(N);
    8364             :   }
    8365           0 :   pari_err_FLAG("mfdim");
    8366             :   return 0; /*LCOV_EXCL_LINE*/
    8367             : }
    8368             : /* mfdim for k = nk/dk */
    8369             : static long
    8370       44744 : mfdim_Nndkchi(long N, long nk, long dk, GEN CHI, long space)
    8371       43463 : { return (dk == 2)? mf2dim_Nkchi(N, nk >> 1, CHI, space)
    8372       88186 :                   : mfdim_Nkchi(N, nk, CHI, space); }
    8373             : /* FIXME: use direct dim Gamma1(N) formula, don't compute individual spaces */
    8374             : static long
    8375         252 : mfwtkdimsum(long N, long k, long dk, long space)
    8376             : {
    8377         252 :   GEN w = mfchars(N, k, dk, NULL);
    8378         252 :   long i, j, D = 0, l = lg(w);
    8379        1239 :   for (i = j = 1; i < l; i++)
    8380             :   {
    8381         987 :     GEN CHI = gel(w,i);
    8382         987 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8383         987 :     if (d) D += d * myeulerphiu(mfcharorder(CHI));
    8384             :   }
    8385         252 :   return D;
    8386             : }
    8387             : static GEN
    8388         105 : mfwt1dims(long N, GEN vCHI, long space)
    8389             : {
    8390         105 :   GEN D = NULL;
    8391         105 :   switch(space)
    8392             :   {
    8393          56 :     case mf_NEW: D = mfwt1newdimall(N, vCHI); break;
    8394          21 :     case mf_CUSP:D = mfwt1cuspdimall(N, vCHI); break;
    8395          28 :     case mf_OLD: D = mfwt1olddimall(N, vCHI); break;
    8396           0 :     default: pari_err_FLAG("mfdim");
    8397             :   }
    8398         105 :   return D;
    8399             : }
    8400             : static GEN
    8401        2961 : mfwtkdims(long N, long k, long dk, GEN vCHI, long space)
    8402             : {
    8403        2961 :   GEN D, w = mfchars(N, k, dk, vCHI);
    8404        2961 :   long i, j, l = lg(w);
    8405        2961 :   D = cgetg(l, t_VEC);
    8406       46592 :   for (i = j = 1; i < l; i++)
    8407             :   {
    8408       43631 :     GEN CHI = gel(w,i);
    8409       43631 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8410       43631 :     if (vCHI)
    8411         574 :       gel(D, j++) = mkvec2s(d, 0);
    8412       43057 :     else if (d)
    8413        2520 :       gel(D, j++) = fmt_dim(CHI, d, 0);
    8414             :   }
    8415        2961 :   setlg(D,j); return D;
    8416             : }
    8417             : GEN
    8418        5719 : mfdim(GEN NK, long space)
    8419             : {
    8420        5719 :   pari_sp av = avma;
    8421             :   long N, k, dk, joker;
    8422             :   GEN CHI, mf;
    8423        5719 :   if ((mf = checkMF_i(NK))) return utoi(MF_get_dim(mf));
    8424        5586 :   checkNK2(NK, &N, &k, &dk, &CHI, 2);
    8425        5586 :   if (!CHI) joker = 1;
    8426             :   else
    8427        2611 :     switch(typ(CHI))
    8428             :     {
    8429        2373 :       case t_INT: joker = 2; break;
    8430         112 :       case t_COL: joker = 3; break;
    8431         126 :       default: joker = 0; break;
    8432             :     }
    8433        5586 :   if (joker)
    8434             :   {
    8435             :     long d;
    8436             :     GEN D;
    8437        5460 :     if (k < 0) switch(joker)
    8438             :     {
    8439           0 :       case 1: return cgetg(1,t_VEC);
    8440           7 :       case 2: return gen_0;
    8441           0 :       case 3: return mfdim0all(CHI);
    8442             :     }
    8443        5453 :     if (k == 0)
    8444             :     {
    8445          28 :       if (space_is_cusp(space)) switch(joker)
    8446             :       {
    8447           7 :         case 1: return cgetg(1,t_VEC);
    8448           0 :         case 2: return gen_0;
    8449           7 :         case 3: return mfdim0all(CHI);
    8450             :       }
    8451          14 :       switch(joker)
    8452             :       {
    8453             :         long i, l;
    8454           7 :         case 1: retmkvec(fmt_dim(mfchartrivial(),0,0));
    8455           0 :         case 2: return gen_1;
    8456           7 :         case 3: l = lg(CHI); D = cgetg(l,t_VEC);
    8457          35 :                 for (i = 1; i < l; i++)
    8458             :                 {
    8459          28 :                   long t = mfcharistrivial(gel(CHI,i));
    8460          28 :                   gel(D,i) = mkvec2(t? gen_1: gen_0, gen_0);
    8461             :                 }
    8462           7 :                 return D;
    8463             :       }
    8464             :     }
    8465        5425 :     if (dk == 1 && k == 1 && space != mf_EISEN)
    8466         105 :     {
    8467        2219 :       long fix = 0, space0 = space;
    8468        2219 :       if (space == mf_FULL) space = mf_CUSP; /* remove Eisenstein part */
    8469        2219 :       if (joker == 2)
    8470             :       {
    8471        2114 :         d = mfwt1dimsum(N, space);
    8472        2114 :         if (space0 == mf_FULL) d += mfwtkdimsum(N,k,dk,mf_EISEN);/*add it back*/
    8473        2114 :         set_avma(av); return utoi(d);
    8474             :       }
    8475             :       /* must initialize explicitly: trivial spaces for E_k/S_k differ */
    8476         105 :       if (space0 == mf_FULL)
    8477             :       {
    8478           7 :         if (!CHI) fix = 1; /* must remove 0 spaces */
    8479           7 :         CHI = mfchars(N, k, dk, CHI);
    8480             :       }
    8481         105 :       D = mfwt1dims(N, CHI, space);
    8482         105 :       if (space0 == mf_FULL)
    8483             :       {
    8484           7 :         GEN D2 = mfwtkdims(N, k, dk, CHI, mf_EISEN);
    8485           7 :         D = merge_dims(D, D2, fix? CHI: NULL);
    8486             :       }
    8487             :     }
    8488             :     else
    8489             :     {
    8490        3206 :       if (joker==2) { d = mfwtkdimsum(N,k,dk,space); set_avma(av); return utoi(d); }
    8491        2954 :       D = mfwtkdims(N, k, dk, CHI, space);
    8492             :     }
    8493        3059 :     if (!CHI) return gerepileupto(av, vecsort(D, mkvecsmall(1)));
    8494         105 :     return gerepilecopy(av, D);
    8495             :   }
    8496         126 :   return utoi( mfdim_Nndkchi(N, k, dk, CHI, space) );
    8497             : }
    8498             : 
    8499             : GEN
    8500         315 : mfbasis(GEN NK, long space)
    8501             : {
    8502         315 :   pari_sp av = avma;
    8503             :   long N, k, dk;
    8504             :   GEN mf, CHI;
    8505         315 :   if ((mf = checkMF_i(NK))) return concat(gel(mf,2), gel(mf,3));
    8506           7 :   checkNK2(NK, &N, &k, &dk, &CHI, 0);
    8507           7 :   if (dk == 2) return gerepilecopy(av, mf2basis(N, k>>1, CHI, NULL, space));
    8508           7 :   mf = mfinit_Nkchi(N, k, CHI, space, 1);
    8509           7 :   return gerepilecopy(av, MF_get_basis(mf));
    8510             : }
    8511             : 
    8512             : static GEN
    8513          49 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
    8514          49 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
    8515             : /* r / x + O(1) */
    8516             : static GEN
    8517          49 : simple_pole(GEN r)
    8518             : {
    8519          49 :   GEN S = deg1ser_shallow(gen_0, r, 0, 1);
    8520          49 :   setvalp(S, -1); return S;
    8521             : }
    8522             : 
    8523             : /* F form, E embedding; mfa = mfatkininit or root number (eigenform case) */
    8524             : static GEN
    8525         154 : mflfuncreate(GEN mfa, GEN F, GEN E, GEN N, GEN gk)
    8526             : {
    8527         154 :   GEN LF = cgetg(8,t_VEC), polar = cgetg(1,t_COL), eps;
    8528         154 :   long k = itou(gk);
    8529         154 :   gel(LF,1) = lfuntag(t_LFUN_MFCLOS, mkvec3(F,E,gen_1));
    8530         154 :   if (typ(mfa) != t_VEC)
    8531          98 :     eps = mfa; /* cuspidal eigenform: root number; no poles */
    8532             :   else
    8533             :   { /* mfatkininit */
    8534          56 :     GEN a0, b0, vF, vG, G = NULL;
    8535          56 :     GEN M = gel(mfa,2), C = gel(mfa,3), mf = gel(mfa,4);
    8536          56 :     M = gdiv(mfmatembed(E, M), C);
    8537          56 :     vF = mfvecembed(E, mftobasis_i(mf, F));
    8538          56 :     vG = RgM_RgC_mul(M, vF);
    8539          56 :     if (gequal(vF,vG)) eps = gen_1;
    8540          42 :     else if (gequal(vF,gneg(vG))) eps = gen_m1;
    8541             :     else
    8542             :     { /* not self-dual */
    8543          42 :       eps = NULL;
    8544          42 :       G = mfatkin(mfa, F);
    8545          42 :       gel(LF,2) = lfuntag(t_LFUN_MFCLOS, mkvec3(G,E,ginv(C)));
    8546          42 :       gel(LF,6) = powIs(k);
    8547             :     }
    8548             :     /* polar part */
    8549          56 :     a0 = mfembed(E, mfcoef(F,0));
    8550          56 :     b0 = eps? gmul(eps,a0): gdiv(mfembed(E, mfcoef(G,0)), C);
    8551          56 :     if (!gequal0(b0))
    8552             :     {
    8553          28 :       b0 = mulcxpowIs(gmul2n(b0,1), k);
    8554          28 :       polar = vec_append(polar, mkvec2(gk, simple_pole(b0)));
    8555             :     }
    8556          56 :     if (!gequal0(a0))
    8557             :     {
    8558          21 :       a0 = gneg(gmul2n(a0,1));
    8559          21 :       polar = vec_append(polar, mkvec2(gen_0, simple_pole(a0)));
    8560             :     }
    8561             :   }
    8562         154 :   if (eps) /* self-dual */
    8563             :   {
    8564         112 :     gel(LF,2) = mfcharorder(mf_get_CHI(F)) <= 2? gen_0: gen_1;
    8565         112 :     gel(LF,6) = mulcxpowIs(eps,k);
    8566             :   }
    8567         154 :   gel(LF,3) = mkvec2(gen_0, gen_1);
    8568         154 :   gel(LF,4) = gk;
    8569         154 :   gel(LF,5) = N;
    8570         154 :   if (lg(polar) == 1) setlg(LF,7); else gel(LF,7) = polar;
    8571         154 :   return LF;
    8572             : }
    8573             : static GEN
    8574         126 : mflfuncreateall(long sd, GEN mfa, GEN F, GEN vE, GEN gN, GEN gk)
    8575             : {
    8576         126 :   long i, l = lg(vE);
    8577         126 :   GEN L = cgetg(l, t_VEC);
    8578         280 :   for (i = 1; i < l; i++)
    8579         154 :     gel(L,i) = mflfuncreate(sd? gel(mfa,i): mfa, F, gel(vE,i), gN, gk);
    8580         126 :   return L;
    8581             : }
    8582             : GEN
    8583          77 : lfunmf(GEN mf, GEN F, long bitprec)
    8584             : {
    8585          77 :   pari_sp av = avma;
    8586          77 :   long i, l, prec = nbits2prec(bitprec);
    8587             :   GEN L, gk, gN;
    8588          77 :   mf = checkMF(mf);
    8589          77 :   gk = MF_get_gk(mf);
    8590          77 :   gN = MF_get_gN(mf);
    8591          77 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
    8592          77 :   if (F)
    8593             :   {
    8594             :     GEN v;
    8595          70 :     long s = MF_get_space(mf);
    8596          70 :     if (!checkmf_i(F)) pari_err_TYPE("lfunmf", F);
    8597          70 :     if (!mfisinspace_i(mf, F)) err_space(F);
    8598          70 :     L = NULL;
    8599          70 :     if ((s == mf_NEW || s == mf_CUSP || s == mf_FULL)
    8600          56 :         && gequal(mfcoefs_i(F,1,1), mkvec2(gen_0,gen_1)))
    8601             :     { /* check if eigenform */
    8602          35 :       GEN vP, vF, b = mftobasis_i(mf, F);
    8603          35 :       long lF, d = degpol(mf_get_field(F));
    8604          35 :       v = mfsplit(mf, d, 0);
    8605          35 :       vF = gel(v,1);
    8606          35 :       vP = gel(v,2); lF = lg(vF);
    8607          35 :       for (i = 1; i < lF; i++)
    8608          28 :         if (degpol(gel(vP,i)) == d && gequal(gel(vF,i), b))
    8609             :         {
    8610          28 :           GEN vE = mfgetembed(F, prec);
    8611          28 :           GEN Z = mffrickeeigen_i(mf, mkvec(b), mkvec(vE), prec);
    8612          28 :           L = mflfuncreateall(1, gel(Z,1), F, vE, gN, gk);
    8613          28 :           break;
    8614             :         }
    8615             :     }
    8616          70 :     if (!L)
    8617             :     { /* not an eigenform: costly general case */
    8618          42 :       GEN mfa = mfatkininit_i(mf, itou(gN), 1, prec);
    8619          42 :       L = mflfuncreateall(0,mfa, F, mfgetembed(F,prec), gN, gk);
    8620             :     }
    8621          70 :     if (lg(L) == 2) L = gel(L,1);
    8622             :   }
    8623             :   else
    8624             :   {
    8625           7 :     GEN M = mfeigenbasis(mf), vE = mfeigenembed(mf, prec);
    8626           7 :     GEN v = mffrickeeigen(mf, vE, prec);
    8627           7 :     l = lg(vE); L = cgetg(l, t_VEC);
    8628          63 :     for (i = 1; i < l; i++)
    8629          56 :       gel(L,i) = mflfuncreateall(1,gel(v,i), gel(M,i), gel(vE,i), gN, gk);
    8630             :   }
    8631          77 :   return gerepilecopy(av, L);
    8632             : }
    8633             : 
    8634             : GEN
    8635          28 : mffromell(GEN E)
    8636             : {
    8637          28 :   pari_sp av = avma;
    8638             :   GEN mf, F, z, v, S;
    8639             :   long N, i, l;
    8640             : 
    8641          28 :   checkell(E);
    8642          28 :   if (ell_get_type(E) != t_ELL_Q) pari_err_TYPE("mfffromell [E not over Q]", E);
    8643          28 :   N = itos(ellQ_get_N(E));
    8644          28 :   mf = mfinit_i(mkvec2(utoi(N), gen_2), mf_NEW);
    8645          28 :   v = split_i(mf, 1, 0);
    8646          28 :   S = gel(v,1); l = lg(S); /* rational newforms */
    8647          28 :   F = tag(t_MF_ELL, mkNK(N,2,mfchartrivial()), E);
    8648          28 :   z = mftobasis_i(mf, F);
    8649          28 :   for(i = 1; i < l; i++)
    8650          28 :     if (gequal(z, gel(S,i))) break;
    8651          28 :   if (i == l) pari_err_BUG("mffromell [E is not modular]");
    8652          28 :   return gerepilecopy(av, mkvec3(mf, F, z));
    8653             : }
    8654             : 
    8655             : /* returns -1 if not, degree otherwise */
    8656             : long
    8657          98 : polishomogeneous(GEN P)
    8658             : {
    8659             :   long i, D, l;
    8660          98 :   if (typ(P) != t_POL) return 0;
    8661          49 :   D = -1; l = lg(P);
    8662         231 :   for (i = 2; i < l; i++)
    8663             :   {
    8664         182 :     GEN c = gel(P,i);
    8665             :     long d;
    8666         182 :     if (gequal0(c)) continue;
    8667          84 :     d = polishomogeneous(c);
    8668          84 :     if (d < 0) return -1;
    8669          84 :     if (D < 0) D = d + i-2; else if (D != d + i-2) return -1;
    8670             :   }
    8671          49 :   return D;
    8672             : }
    8673             : 
    8674             : /* P a t_POL, 1 if spherical, 0 otherwise */
    8675             : static int
    8676          14 : RgX_isspherical(GEN Qi, GEN P)
    8677             : {
    8678          14 :   pari_sp av = avma;
    8679             :   GEN va, S;
    8680             :   long lva, i, j;
    8681          14 :   if (degpol(P) <= 1) return 1;
    8682          14 :   va = variables_vecsmall(P); lva = lg(va);
    8683          14 :   if (lva > lg(Qi)) pari_err(e_MISC, "too many variables in mffromqf");
    8684          14 :   S = gen_0;
    8685          42 :   for (j = 1; j < lva; j++)
    8686             :   {
    8687          28 :     GEN col = gel(Qi, j), Pj = deriv(P, va[j]);
    8688          70 :     for (i = 1; i <= j; i++)
    8689             :     {
    8690          42 :       GEN coe = gel(col, i);
    8691          42 :       if (i != j) coe = gmul2n(coe, 1);
    8692          42 :       if (!gequal0(coe)) S = gadd(S, gmul(coe, deriv(Pj, va[i])));
    8693             :     }
    8694             :   }
    8695          14 :   return gc_bool(av, gequal0(S));
    8696             : }
    8697             : 
    8698             : static GEN
    8699          49 : c_QFsimple_i(long n, GEN Q, GEN P)
    8700             : {
    8701          49 :   GEN V, v = qfrep0(Q, utoi(n), 1);
    8702          49 :   long i, l = lg(v);
    8703          49 :   V = cgetg(l+1, t_VEC);
    8704          91 :   if (!P || equali1(P))
    8705             :   {
    8706          42 :     gel(V,1) = gen_1;
    8707         420 :     for (i = 2; i <= l; i++) gel(V,i) = utoi(v[i-1] << 1);
    8708             :   }
    8709             :   else
    8710             :   {
    8711           7 :     gel(V,1) = gcopy(P);
    8712           7 :     for (i = 2; i <= l; i++) gel(V,i) = gmulgs(P, v[i-1] << 1);
    8713             :   }
    8714          49 :   return V;
    8715             : }
    8716             : 
    8717             : /* v a t_VECSMALL of variable numbers, lg(r) >= lg(v), r is a vector of
    8718             :  * scalars [not involving any variable in v] */
    8719             : static GEN
    8720          14 : gsubstvec_i(GEN e, GEN v, GEN r)
    8721             : {
    8722          14 :   long i, l = lg(v);
    8723          42 :   for(i = 1; i < l; i++) e = gsubst(e, v[i], gel(r,i));
    8724          14 :   return e;
    8725             : }
    8726             : static GEN
    8727          56 : c_QF_i(long n, GEN Q, GEN P)
    8728             : {
    8729          56 :   pari_sp av = avma;
    8730             :   GEN V, v, va;
    8731             :   long i, l;
    8732          56 :   if (!P || typ(P) != t_POL) return gerepileupto(av, c_QFsimple_i(n, Q, P));
    8733           7 :   v = gel(minim(Q, utoi(2*n), NULL), 3);
    8734           7 :   va = variables_vecsmall(P);
    8735           7 :   V = zerovec(n + 1); l = lg(v);
    8736          21 :   for (i = 1; i < l; i++)
    8737             :   {
    8738          14 :     pari_sp av = avma;
    8739          14 :     GEN X = gel(v,i);
    8740          14 :     long c = (itos(qfeval(Q, X)) >> 1) + 1;
    8741          14 :     gel(V, c) = gerepileupto(av, gadd(gel(V, c), gsubstvec_i(P, va, X)));
    8742             :   }
    8743           7 :   return gmul2n(V, 1);
    8744             : }
    8745             : 
    8746             : GEN
    8747          63 : mffromqf(GEN Q, GEN P)
    8748             : {
    8749          63 :   pari_sp av = avma;
    8750             :   GEN G, Qi, F, D, N, mf, v, gk, chi;
    8751             :   long m, d, space;
    8752          63 :   if (typ(Q) != t_MAT) pari_err_TYPE("mffromqf", Q);
    8753          63 :   if (!RgM_is_ZM(Q) || !qfiseven(Q))
    8754           0 :     pari_err_TYPE("mffromqf [not integral or even]", Q);
    8755          63 :   m = lg(Q)-1;
    8756          63 :   Qi = ZM_inv(Q, &N);
    8757          63 :   if (!qfiseven(Qi)) N = shifti(N, 1);
    8758          63 :   d = 0;
    8759          63 :   if (!P || gequal1(P)) P = NULL;
    8760             :   else
    8761             :   {
    8762          21 :     P = simplify_shallow(P);
    8763          21 :     if (typ(P) == t_POL)
    8764             :     {
    8765          14 :       d = polishomogeneous(P);
    8766          14 :       if (d < 0) pari_err_TYPE("mffromqf [not homogeneous t_POL]", P);
    8767          14 :       if (!RgX_isspherical(Qi, P))
    8768           0 :         pari_err_TYPE("mffromqf [not a spherical t_POL]", P);
    8769             :     }
    8770             :   }
    8771          63 :   gk = sstoQ(m + 2*d, 2);
    8772          63 :   D = ZM_det(Q);
    8773          63 :   if (!odd(m)) { if ((m & 3) == 2) D = negi(D); } else D = shifti(D, 1);
    8774          63 :   space = d > 0 ? mf_CUSP : mf_FULL;
    8775          63 :   G = znstar0(N,1);
    8776          63 :   chi = mkvec2(G, znchar_quad(G,D));
    8777          63 :   mf = mfinit(mkvec3(N, gk, chi), space);
    8778          63 :   if (odd(d))
    8779             :   {
    8780           7 :     F = mftrivial();
    8781           7 :     v = zerocol(MF_get_dim(mf));
    8782             :   }
    8783             :   else
    8784             :   {
    8785          56 :     F = c_QF_i(mfsturm(mf), Q, P);
    8786          56 :     v = mftobasis_i(mf, F);
    8787          56 :     F = mflinear(mf, v);
    8788             :   }
    8789          63 :   return gerepilecopy(av, mkvec3(mf, F, v));
    8790             : }
    8791             : 
    8792             : /***********************************************************************/
    8793             : /*                          Eisenstein Series                          */
    8794             : /***********************************************************************/
    8795             : /* \sigma_{k-1}(\chi,n) */
    8796             : static GEN
    8797       23996 : sigchi(long k, GEN CHI, long n)
    8798             : {
    8799       23996 :   pari_sp av = avma;
    8800       23996 :   GEN S = gen_1, D = mydivisorsu(u_ppo(n,mfcharmodulus(CHI)));
    8801       23996 :   long i, l = lg(D), ord = mfcharorder(CHI), vt = varn(mfcharpol(CHI));
    8802       83279 :   for (i = 2; i < l; i++) /* skip D[1] = 1 */
    8803             :   {
    8804       59283 :     long d = D[i], a = mfcharevalord(CHI, d, ord);
    8805       59283 :     S = gadd(S, Qab_Czeta(a, ord, powuu(d, k-1), vt));
    8806             :   }
    8807       23996 :   return gerepileupto(av,S);
    8808             : }
    8809             : 
    8810             : /* write n = n0*n1*n2, (n0,N1*N2) = 1, n1 | N1^oo, n2 | N2^oo;
    8811             :  * return NULL if (n,N1,N2) > 1, else return factoru(n0) */
    8812             : static GEN
    8813      329427 : sigchi2_dec(long n, long N1, long N2, long *pn1, long *pn2)
    8814             : {
    8815      329427 :   GEN P0, E0, P, E, fa = myfactoru(n);
    8816             :   long i, j, l;
    8817      329427 :   *pn1 = 1;
    8818      329427 :   *pn2 = 1;
    8819      329427 :   if (N1 == 1 && N2 == 1) return fa;
    8820      315938 :   P = gel(fa,1); l = lg(P);
    8821      315938 :   E = gel(fa,2);
    8822      315938 :   P0 = cgetg(l, t_VECSMALL);
    8823      315938 :   E0 = cgetg(l, t_VECSMALL);
    8824      743008 :   for (i = j = 1; i < l; i++)
    8825             :   {
    8826      449792 :     long p = P[i], e = E[i];
    8827      449792 :     if (N1 % p == 0)
    8828             :     {
    8829       56224 :       if (N2 % p == 0) return NULL;
    8830       33502 :       *pn1 *= upowuu(p,e);
    8831             :     }
    8832      393568 :     else if (N2 % p == 0)
    8833       87724 :       *pn2 *= upowuu(p,e);
    8834      305844 :     else { P0[j] = p; E0[j] = e; j++; }
    8835             :   }
    8836      293216 :   setlg(P0, j);
    8837      293216 :   setlg(E0, j); return mkvec2(P0,E0);
    8838             : }
    8839             : 
    8840             : /* sigma_{k-1}(\chi_1,\chi_2,n), ord multiple of lcm(ord(CHI1),ord(CHI2)) */
    8841             : static GEN
    8842      274939 : sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord)
    8843             : {
    8844      274939 :   pari_sp av = avma;
    8845             :   GEN S, D;
    8846      274939 :   long i, l, n1, n2, vt, N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    8847      274939 :   D = sigchi2_dec(n, N1, N2, &n1, &n2); if (!D) return gc_const(av, gen_0);
    8848      256886 :   D = divisorsu_fact(D); l = lg(D);
    8849      256886 :   vt = varn(mfcharpol(CHI1));
    8850      952910 :   for (i = 1, S = gen_0; i < l; i++)
    8851             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8852      696024 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1; (n/d,N2) = 1 */
    8853      696024 :     a = mfcharevalord(CHI1, d, ord) + mfcharevalord(CHI2, nd, ord);
    8854      696024 :     if (a >= ord) a -= ord;
    8855      696024 :     S = gadd(S, Qab_Czeta(a, ord, powuu(d, k-1), vt));
    8856             :   }
    8857      256886 :   return gerepileupto(av, S);
    8858             : }
    8859             : 
    8860             : /**************************************************************************/
    8861             : /**           Dirichlet characters with precomputed values               **/
    8862             : /**************************************************************************/
    8863             : /* CHI mfchar */
    8864             : static GEN
    8865       13230 : mfcharcxinit(GEN CHI, long prec)
    8866             : {
    8867       13230 :   GEN G = gel(CHI,1), chi = gel(CHI,2), z, V;
    8868       13230 :   GEN v = ncharvecexpo(G, znconrey_normalized(G,chi));
    8869       13230 :   long n, l = lg(v), o = mfcharorder(CHI);
    8870       13230 :   V = cgetg(l, t_VEC);
    8871       13230 :   z = grootsof1(o, prec); /* Mod(t, Phi_o(t)) -> e(1/o) */
    8872      120085 :   for (n = 1; n < l; n++) gel(V,n) = v[n] < 0? gen_0: gel(z, v[n]+1);
    8873       13230 :   return mkvecn(6, G, chi, gmfcharorder(CHI), v, V, mfcharpol(CHI));
    8874             : }
    8875             : /* v a "CHIvec" */
    8876             : static long
    8877    23965403 : CHIvec_N(GEN v) { return itou(znstar_get_N(gel(v,1))); }
    8878             : static GEN
    8879       14406 : CHIvec_CHI(GEN v)
    8880       14406 : { return mkvec4(gel(v,1), gel(v,2), gel(v,3), gel(v,6)); }
    8881             : /* character order */
    8882             : static long
    8883       38486 : CHIvec_ord(GEN v) { return itou(gel(v,3)); }
    8884             : /* character exponents, i.e. t such that chi(n) = e(t) */
    8885             : static GEN
    8886      416829 : CHIvec_expo(GEN v) { return gel(v,4); }
    8887             : /* character values chi(n) */
    8888             : static GEN
    8889    23334311 : CHIvec_val(GEN v) { return gel(v,5); }
    8890             : /* CHI(n) */
    8891             : static GEN
    8892    23324910 : mychareval(GEN v, long n)
    8893             : {
    8894    23324910 :   long N = CHIvec_N(v), ind = n%N;
    8895    23324910 :   if (ind <= 0) ind += N;
    8896    23324910 :   return gel(CHIvec_val(v), ind);
    8897             : }
    8898             : /* return c such that CHI(n) = e(c / ordz) or -1 if (n,N) > 1 */
    8899             : static long
    8900      416829 : mycharexpo(GEN v, long n)
    8901             : {
    8902      416829 :   long N = CHIvec_N(v), ind = n%N;
    8903      416829 :   if (ind <= 0) ind += N;
    8904      416829 :   return CHIvec_expo(v)[ind];
    8905             : }
    8906             : /* faster than mfcharparity */
    8907             : static long
    8908       53823 : CHIvec_parity(GEN v) { return mycharexpo(v,-1) ? -1: 1; }
    8909             : /**************************************************************************/
    8910             : 
    8911             : static ulong
    8912       54488 : sigchi2_Fl(long k, GEN CHI1vec, GEN CHI2vec, long n, GEN vz, ulong p)
    8913             : {
    8914       54488 :   pari_sp av = avma;
    8915       54488 :   long ordz = lg(vz)-2, i, l, n1, n2;
    8916       54488 :   ulong S = 0;
    8917       54488 :   GEN D = sigchi2_dec(n, CHIvec_N(CHI1vec), CHIvec_N(CHI2vec), &n1, &n2);
    8918       54488 :   if (!D) return gc_ulong(av,S);
    8919       49819 :   D = divisorsu_fact(D);
    8920       49819 :   l = lg(D);
    8921      169470 :   for (i = 1; i < l; i++)
    8922             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8923      119651 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1, (n/d,N2)=1 */
    8924      119651 :     a = mycharexpo(CHI2vec, nd) + mycharexpo(CHI1vec, d);
    8925      119651 :     if (a >= ordz) a -= ordz;
    8926      119651 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, Fl_powu(d,k-1,p), p), p);
    8927             :   }
    8928       49819 :   return gc_ulong(av,S);
    8929             : }
    8930             : 
    8931             : /**********************************************************************/
    8932             : /* Fourier expansions of Eisenstein series                            */
    8933             : /**********************************************************************/
    8934             : /* L(CHI_t,0) / 2, CHI_t(n) = CHI(n)(t/n) as a character modulo N*t,
    8935             :  * order(CHI) | ord != 0 */
    8936             : static GEN
    8937        2163 : charLFwt1(long N, GEN CHI, long ord, long t)
    8938             : {
    8939             :   GEN S;
    8940             :   long r, vt;
    8941             : 
    8942        2163 :   if (N == 1 && t == 1) return mkfrac(gen_m1,stoi(4));
    8943        2163 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8944       64393 :   for (r = 1; r < N; r++)
    8945             :   { /* S += r*chi(r) */
    8946             :     long a;
    8947       62230 :     if (ugcd(N,r) != 1) continue;
    8948       50190 :     a = mfcharevalord(CHI,r,ord);
    8949       50190 :     if (t != 1 && kross(t, r) < 0) r = -r;
    8950       50190 :     S = gadd(S, Qab_Czeta(a, ord, stoi(r), vt));
    8951             :   }
    8952        2163 :   return gdivgs(S, -2*N);
    8953             : }
    8954             : /* L(CHI,0) / 2, mod p */
    8955             : static ulong
    8956        1960 : charLFwt1_Fl(GEN CHIvec, GEN vz, ulong p)
    8957             : {
    8958        1960 :   long r, m = CHIvec_N(CHIvec);
    8959             :   ulong S;
    8960        1960 :   if (m == 1) return Rg_to_Fl(mkfrac(gen_m1,stoi(4)), p);
    8961        1960 :   S = 0;
    8962       95746 :   for (r = 1; r < m; r++)
    8963             :   { /* S += r*chi(r) */
    8964       93786 :     long a = mycharexpo(CHIvec,r);
    8965       93786 :     if (a < 0) continue;
    8966       91490 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, r, p), p);
    8967             :   }
    8968        1960 :   return Fl_div(Fl_neg(S,p), 2*m, p);
    8969             : }
    8970             : /* L(CHI_t,1-k) / 2, CHI_t(n) = CHI(n) * (t/n), order(CHI) | ord != 0;
    8971             :  * assume conductor of CHI_t divides N */
    8972             : static GEN
    8973        3682 : charLFwtk(long N, long k, GEN CHI, long ord, long t)
    8974             : {
    8975             :   GEN S, P, dS;
    8976             :   long r, vt;
    8977             : 
    8978        3682 :   if (k == 1) return charLFwt1(N, CHI, ord, t);
    8979        1519 :   if (N == 1 && t == 1) return gdivgs(bernfrac(k),-2*k);
    8980         917 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8981         917 :   P = ZX_rescale(Q_remove_denom(bernpol(k,0), &dS), utoi(N));
    8982         917 :   dS = mul_denom(dS, stoi(-2*N*k));
    8983       13342 :   for (r = 1; r < N; r++)
    8984             :   { /* S += P(r)*chi(r) */
    8985             :     long a;
    8986             :     GEN C;
    8987       12425 :     if (ugcd(r,N) != 1) continue;
    8988       10080 :     a = mfcharevalord(CHI,r,ord);
    8989       10080 :     C = poleval(P, utoi(r));
    8990       10080 :     if (t != 1 && kross(t, r) < 0) C = gneg(C);
    8991       10080 :     S = gadd(S, Qab_Czeta(a, ord, C, vt));
    8992             :   }
    8993         917 :   return gdiv(S, dS);
    8994             : }
    8995             : /* L(CHI,1-k) / 2, mod p */
    8996             : static ulong
    8997        2723 : charLFwtk_Fl(long k, GEN CHIvec, GEN vz, ulong p)
    8998             : {
    8999             :   GEN P;
    9000             :   long r, m;
    9001             :   ulong S;
    9002        2723 :   if (k == 1) return charLFwt1_Fl(CHIvec, vz, p);
    9003         763 :   m = CHIvec_N(CHIvec);
    9004         763 :   if (m == 1) return Rg_to_Fl(gdivgs(bernfrac(k),-2*k), p);
    9005         448 :   S = 0;
    9006         448 :   P = RgX_to_Flx(RgX_rescale(bernpol(k,0), utoi(m)), p);
    9007       10038 :   for (r = 1; r < m; r++)
    9008             :   { /* S += P(r)*chi(r) */
    9009        9590 :     long a = mycharexpo(CHIvec,r);
    9010        9590 :     if (a < 0) continue;
    9011        8470 :     S = Fl_add(S, Qab_Czeta_Fl(a, vz, Flx_eval(P,r,p), p), p);
    9012             :   }
    9013         448 :   return Fl_div(Fl_neg(S,p), 2*k*m, p);
    9014             : }
    9015             : 
    9016             : static GEN
    9017        6622 : mfeisenstein2_0(long k, GEN CHI1, GEN CHI2, long ord)
    9018             : {
    9019        6622 :   if (k == 1 && mfcharistrivial(CHI1))
    9020        2163 :     return charLFwtk(mfcharmodulus(CHI2), 1, CHI2, ord, 1);
    9021        4459 :   else if (mfcharistrivial(CHI2))
    9022        1330 :     return charLFwtk(mfcharmodulus(CHI1), k, CHI1, ord, 1);
    9023        3129 :   else return gen_0;
    9024             : }
    9025             : static ulong
    9026        4137 : mfeisenstein2_0_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p)
    9027             : {
    9028        4137 :   if (k == 1 && CHIvec_ord(CHI1vec) == 1)
    9029        1960 :     return charLFwtk_Fl(k, CHI2vec, vz, p);
    9030        2177 :   else if (CHIvec_ord(CHI2vec) == 1)
    9031         763 :     return charLFwtk_Fl(k, CHI1vec, vz, p);
    9032        1414 :   else return 0;
    9033             : }
    9034             : static GEN
    9035         126 : NK_eisen2(long k, GEN CHI1, GEN CHI2, long ord)
    9036             : {
    9037         126 :   long o, N = mfcharmodulus(CHI1)*mfcharmodulus(CHI2);
    9038         126 :   GEN CHI = mfcharmul(CHI1, CHI2);
    9039         126 :   o = mfcharorder(CHI);
    9040         126 :   if ((ord & 3) == 2) ord >>= 1;
    9041         126 :   if ((o & 3) == 2) o >>= 1;
    9042         126 :   if (ord != o) pari_err_IMPL("mfeisenstein for these characters");
    9043         119 :   return mkNK(N, k, CHI);
    9044             : }
    9045             : static GEN
    9046         343 : mfeisenstein_i(long k, GEN CHI1, GEN CHI2)
    9047             : {
    9048         343 :   long s = 1, ord, vt;
    9049             :   GEN E0, NK, vchi, T;
    9050         343 :   if (CHI2) { CHI2 = get_mfchar(CHI2); if (mfcharparity(CHI2) < 0) s = -s; }
    9051         343 :   if (CHI1) { CHI1 = get_mfchar(CHI1); if (mfcharparity(CHI1) < 0) s = -s; }
    9052         329 :   if (s != m1pk(k)) return mftrivial();
    9053         308 :   if (!CHI1) CHI1 = mfchartrivial();
    9054         308 :   if (!CHI2)
    9055             :   { /* E_k(chi1) */
    9056         182 :     vt = varn(mfcharpol(CHI1));
    9057         182 :     ord = mfcharorder(CHI1);
    9058         182 :     NK = mkNK(mfcharmodulus(CHI1), k, CHI1);
    9059         182 :     E0 = charLFwtk(mfcharmodulus(CHI1), k, CHI1, ord, 1);
    9060         182 :     vchi = mkvec3(E0, mkvec(mfcharpol(CHI1)), CHI1);
    9061         182 :     return tag(t_MF_EISEN, NK, vchi);
    9062             :   }
    9063             :   /* E_k(chi1,chi2) */
    9064         126 :   vt = varn(mfcharpol(CHI1));
    9065         126 :   ord = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    9066         126 :   NK = NK_eisen2(k, CHI1, CHI2, ord);
    9067         119 :   E0 = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9068         119 :   T = mkvec(polcyclo(ord, vt));
    9069         119 :   vchi = mkvec4(E0, T, CHI1, CHI2);
    9070         119 :   return tag2(t_MF_EISEN, NK, vchi, mkvecsmall2(ord,0));
    9071             : }
    9072             : GEN
    9073         343 : mfeisenstein(long k, GEN CHI1, GEN CHI2)
    9074             : {
    9075         343 :   pari_sp av = avma;
    9076         343 :   if (k < 1) pari_err_DOMAIN("mfeisenstein", "k", "<", gen_1, stoi(k));
    9077         343 :   return gerepilecopy(av, mfeisenstein_i(k, CHI1, CHI2));
    9078             : }
    9079             : 
    9080             : static GEN
    9081        1610 : mfeisenstein2all(long N0, GEN NK, long k, GEN CHI1, GEN CHI2, GEN T, long o)
    9082             : {
    9083        1610 :   GEN E, E0 = mfeisenstein2_0(k, CHI1,CHI2, o), vchi = mkvec4(E0, T, CHI1,CHI2);
    9084        1610 :   long j, d = (lg(T)==4)? itou(gmael(T,3,1)): 1;
    9085        1610 :   E = cgetg(d+1, t_VEC);
    9086        3269 :   for (j=1; j<=d; j++) gel(E,j) = tag2(t_MF_EISEN, NK,vchi,mkvecsmall2(o,j-1));
    9087        1610 :   return mfbdall(E, N0 / mf_get_N(gel(E,1)));
    9088             : }
    9089             : 
    9090             : /* list of characters on G = (Z/NZ)^*, v[i] = NULL if (i,N) > 1, else
    9091             :  * the conductor of Conrey label i, [conductor, primitive char].
    9092             :  * Trivial chi (label 1) comes first */
    9093             : static GEN
    9094         707 : zncharsG(GEN G)
    9095             : {
    9096         707 :   long i, l, N = itou(znstar_get_N(G));
    9097             :   GEN vCHI, V;
    9098         707 :   if (N == 1) return mkvec2(gen_1,cgetg(1,t_COL));
    9099         707 :   vCHI = const_vec(N,NULL);
    9100         707 :   V = cyc2elts(znstar_get_conreycyc(G));
    9101         707 :   l = lg(V);
    9102       23667 :   for (i = 1; i < l; i++)
    9103             :   {
    9104       22960 :     GEN chi0, chi = zc_to_ZC(gel(V,i)), n, F;
    9105       22960 :     F = znconreyconductor(G, chi, &chi0);
    9106       22960 :     if (typ(F) != t_INT) F = gel(F,1);
    9107       22960 :     n = znconreyexp(G, chi);
    9108       22960 :     gel(vCHI, itos(n)) = mkvec2(chi0, F);
    9109             :   }
    9110         707 :   return vCHI;
    9111             : }
    9112             : 
    9113             : /* CHI primitive, f(CHI) | N. Return pairs (CHI1,CHI2) both primitive
    9114             :  * such that f(CHI1)*f(CHI2) | N and CHI1 * CHI2 = CHI;
    9115             :  * if k = 1, CHI1 is even; if k = 2, omit (1,1) if CHI = 1 */
    9116             : static GEN
    9117         910 : mfeisensteinbasis_i(long N0, long k, GEN CHI)
    9118             : {
    9119         910 :   GEN G = gel(CHI,1), chi = gel(CHI,2), vT = const_vec(myeulerphiu(N0), NULL);
    9120         910 :   GEN CHI0, GN, chiN, Lchi, LG, V, RES, NK, T, C = mfcharpol(CHI);
    9121         910 :   long i, j, l, n, n1, N, ord = mfcharorder(CHI);
    9122         910 :   long F = mfcharmodulus(CHI), vt = varn(mfcharpol(CHI));
    9123             : 
    9124         910 :   CHI0 = (F == 1)? CHI: mfchartrivial();
    9125         910 :   j = 1; RES = cgetg(N0+1, t_VEC);
    9126         910 :   T = gel(vT,ord) = Qab_trace_init(ord, ord, C, C);
    9127         910 :   if (F != 1 || k != 2)
    9128             :   { /* N1 = 1 */
    9129         770 :     NK = mkNK(F, k, CHI);
    9130         770 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI0, CHI, T, ord);
    9131         770 :     if (F != 1 && k != 1)
    9132         266 :       gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI, CHI0, T, ord);
    9133             :   }
    9134         910 :   if (N0 == 1) { setlg(RES,j); return RES; }
    9135         840 :   GN = G; chiN = chi;
    9136         840 :   if (F == N0) N = N0;
    9137             :   else
    9138             :   {
    9139         504 :     GEN faN = myfactoru(N0), P = gel(faN,1), E = gel(faN,2);
    9140         504 :     long lP = lg(P);
    9141        1316 :     for (i = N = 1; i < lP; i++)
    9142             :     {
    9143         812 :       long p = P[i];
    9144         812 :       N *= upowuu(p, maxuu(E[i]/2, z_lval(F,p)));
    9145             :     }
    9146         504 :     if ((N & 3) == 2) N >>= 1;
    9147         504 :     if (N == 1) { setlg(RES,j); return RES; }
    9148         371 :     if (F != N)
    9149             :     {
    9150         105 :       GN = znstar0(utoipos(N),1);
    9151         105 :       chiN = zncharinduce(G, chi, GN);
    9152             :     }
    9153             :   }
    9154         707 :   LG = const_vec(N, NULL); /* LG[d] = znstar(d,1) or NULL */
    9155         707 :   gel(LG,1) = gel(CHI0,1);
    9156         707 :   gel(LG,F) = G;
    9157         707 :   gel(LG,N) = GN;
    9158         707 :   Lchi = coprimes_zv(N);
    9159         707 :   n = itou(znconreyexp(GN,chiN));
    9160         707 :   V = zncharsG(GN); l = lg(V);
    9161       31241 :   for (n1 = 2; n1 < l; n1++) /* skip 1 (trivial char) */
    9162             :   {
    9163       30534 :     GEN v = gel(V,n1), w, chi1, chi2, G1, G2, CHI1, CHI2;
    9164             :     long N12, N1, N2, no, o12, t, m;
    9165       30534 :     if (!Lchi[n1] || n1 == n) continue; /* skip trivial chi2 */
    9166       21581 :     chi1 = gel(v,1); N1 = itou(gel(v,2)); /* conductor of chi1 */
    9167       21581 :     w = gel(V, Fl_div(n,n1,N));
    9168       21581 :     chi2 = gel(w,1); N2 = itou(gel(w,2)); /* conductor of chi2 */
    9169       21581 :     N12 = N1 * N2;
    9170       21581 :     if (N0 % N12) continue;
    9171             : 
    9172         763 :     G1 = gel(LG,N1); if (!G1) gel(LG,N1) = G1 = znstar0(utoipos(N1), 1);
    9173         763 :     if (k == 1 && zncharisodd(G1,chi1)) continue;
    9174         574 :     G2 = gel(LG,N2); if (!G2) gel(LG,N2) = G2 = znstar0(utoipos(N2), 1);
    9175         574 :     CHI1 = mfcharGL(G1, chi1);
    9176         574 :     CHI2 = mfcharGL(G2, chi2);
    9177         574 :     o12 = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    9178             :     /* remove Galois orbit: same trace */
    9179         574 :     no = Fl_powu(n1, ord, N);
    9180         875 :     for (t = 1+ord, m = n1; t <= o12; t += ord)
    9181             :     { /* m <-> CHI1^t, if t in Gal(Q(chi1,chi2)/Q), omit (CHI1^t,CHI2^t) */
    9182         301 :       m = Fl_mul(m, no, N); if (!m) break;
    9183         301 :       if (ugcd(t, o12) == 1) Lchi[m] = 0;
    9184             :     }
    9185         574 :     T = gel(vT,o12);
    9186         574 :     if (!T) T = gel(vT,o12) = Qab_trace_init(o12, ord, polcyclo(o12,vt), C);
    9187         574 :     NK = mkNK(N12, k, CHI);
    9188         574 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI1, CHI2, T, o12);
    9189             :   }
    9190         707 :   setlg(RES,j); return RES;
    9191             : }
    9192             : 
    9193             : static GEN
    9194         700 : mfbd_E2(GEN E2, long d, GEN CHI)
    9195             : {
    9196         700 :   GEN E2d = mfbd_i(E2, d);
    9197         700 :   GEN F = mkvec2(E2, E2d), L = mkvec2(gen_1, utoineg(d));
    9198             :   /* cannot use mflinear_i: E2 and E2d do not have the same level */
    9199         700 :   return tag3(t_MF_LINEAR, mkNK(d,2,CHI), F, L, gen_1);
    9200             : }
    9201             : /* C-basis of E_k(Gamma_0(N),chi). If k = 1, the first basis element must not
    9202             :  * vanish at oo [used in mfwt1basis]. Here E_1(CHI), whose q^0 coefficient
    9203             :  * does not vanish (since L(CHI,0) does not) *if* CHI is not trivial; which
    9204             :  * must be the case in weight 1.
    9205             :  *
    9206             :  * (k>=3): In weight k >= 3, basis is B(d) E(CHI1,(CHI/CHI1)_prim), where
    9207             :  * CHI1 is primitive modulo N1, and if N2 is the conductor of CHI/CHI1
    9208             :  * then d*N1*N2 | N.
    9209             :  * (k=2): In weight k=2, same if CHI is nontrivial. If CHI is trivial, must
    9210             :  * not take CHI1 trivial, and must add E_2(tau)-dE_2(d tau)), where
    9211             :  * d|N, d > 1.
    9212             :  * (k=1): In weight k=1, same as k >= 3 except that we restrict to CHI1 even */
    9213             : static GEN
    9214         938 : mfeisensteinbasis(long N, long k, GEN CHI)
    9215             : {
    9216             :   long i, F;
    9217             :   GEN L;
    9218         938 :   if (badchar(N, k, CHI)) return cgetg(1, t_VEC);
    9219         938 :   if (k == 0) return mfcharistrivial(CHI)? mkvec(mf1()): cgetg(1, t_VEC);
    9220         910 :   CHI = mfchartoprimitive(CHI, &F);
    9221         910 :   L = mfeisensteinbasis_i(N, k, CHI);
    9222         910 :   if (F == 1 && k == 2)
    9223             :   {
    9224         140 :     GEN v, E2 = mfeisenstein(2, NULL, NULL), D = mydivisorsu(N);
    9225         140 :     long nD = lg(D)-1;
    9226         140 :     v = cgetg(nD, t_VEC); L = vec_append(L,v);
    9227         833 :     for (i = 1; i < nD; i++) gel(v,i) = mfbd_E2(E2, D[i+1], CHI);
    9228             :   }
    9229         910 :   return lg(L) == 1? L: shallowconcat1(L);
    9230             : }
    9231             : 
    9232             : static GEN
    9233          77 : not_in_space(GEN F, long flag)
    9234             : {
    9235          77 :   if (!flag) err_space(F);
    9236          70 :   return cgetg(1, t_COL);
    9237             : }
    9238             : /* when flag set, no error */
    9239             : GEN
    9240         819 : mftobasis(GEN mf, GEN F, long flag)
    9241             : {
    9242         819 :   pari_sp av2, av = avma;
    9243             :   GEN G, v, y, gk;
    9244         819 :   long N, B, ismf = checkmf_i(F);
    9245             : 
    9246         819 :   mf = checkMF(mf);
    9247         819 :   if (ismf)
    9248             :   {
    9249         728 :     if (mfistrivial(F)) return zerocol(MF_get_dim(mf));
    9250         721 :     if (!mf_same_k(mf, F) || !mf_same_CHI(mf, F)) return not_in_space(F, flag);
    9251             :   }
    9252         770 :   N = MF_get_N(mf);
    9253         770 :   gk = MF_get_gk(mf);
    9254         770 :   if (ismf)
    9255             :   {
    9256         679 :     long NF = mf_get_N(F);
    9257         679 :     B = maxuu(mfsturmNgk(NF,gk), mfsturmNgk(N,gk)) + 1;
    9258         679 :     v = mfcoefs_i(F,B,1);
    9259             :   }
    9260             :   else
    9261             :   {
    9262          91 :     B = mfsturmNgk(N, gk) + 1;
    9263          91 :     switch(typ(F))
    9264             :     { /* F(0),...,F(lg(v)-2) */
    9265          63 :       case t_SER: v = sertocol(F); settyp(v,t_VEC); break;
    9266          14 :       case t_VEC: v = F; break;
    9267           7 :       case t_COL: v = shallowtrans(F); break;
    9268           7 :       default: pari_err_TYPE("mftobasis",F);
    9269             :                v = NULL;/*LCOV_EXCL_LINE*/
    9270             :     }
    9271          84 :     if (flag) B = minss(B, lg(v)-2);
    9272             :   }
    9273         763 :   y = mftobasis_i(mf, v);
    9274         763 :   if (typ(y) == t_VEC)
    9275             :   {
    9276          21 :     if (flag) return gerepilecopy(av, y);
    9277           0 :     pari_err(e_MISC, "not enough coefficients in mftobasis");
    9278             :   }
    9279         742 :   av2 = avma;
    9280         742 :   if (MF_get_space(mf) == mf_FULL || mfsturm(mf)+1 == B) return y;
    9281         287 :   G = mflinear(mf, y);
    9282         287 :   if (!gequal(v, mfcoefs_i(G, lg(v)-2,1))) y = NULL;
    9283         287 :   if (!y) { set_avma(av); return not_in_space(F, flag); }
    9284         252 :   set_avma(av2); return gerepileupto(av, y);
    9285             : }
    9286             : 
    9287             : /* assume N > 0; first cusp is always 0 */
    9288             : static GEN
    9289          49 : mfcusps_i(long N)
    9290             : {
    9291             :   long i, c, l;
    9292             :   GEN D, v;
    9293             : 
    9294          49 :   if (N == 1) return mkvec(gen_0);
    9295          49 :   D = mydivisorsu(N); l = lg(D); /* left on stack */
    9296          49 :   c = mfnumcuspsu_fact(myfactoru(N));
    9297          49 :   v = cgetg(c + 1, t_VEC);
    9298         350 :   for (i = c = 1; i < l; i++)
    9299             :   {
    9300         301 :     long C = D[i], NC = D[l-i], lima = ugcd(C, NC), A0, A;
    9301         889 :     for (A0 = 0; A0 < lima; A0++)
    9302         588 :       if (ugcd(A0, lima) == 1)
    9303             :       {
    9304         539 :         A = A0; while (ugcd(A,C) > 1) A += lima;
    9305         392 :         gel(v, c++) = sstoQ(A, C);
    9306             :       }
    9307             :   }
    9308          49 :   return v;
    9309             : }
    9310             : /* List of cusps of Gamma_0(N) */
    9311             : GEN
    9312          28 : mfcusps(GEN gN)
    9313             : {
    9314             :   long N;
    9315             :   GEN mf;
    9316          28 :   if (typ(gN) == t_INT) N = itos(gN);
    9317          14 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    9318           0 :   else { pari_err_TYPE("mfcusps", gN); N = 0; }
    9319          28 :   if (N <= 0) pari_err_DOMAIN("mfcusps", "N", "<=", gen_0, stoi(N));
    9320          28 :   return mfcusps_i(N);
    9321             : }
    9322             : 
    9323             : long
    9324         315 : mfcuspisregular(GEN NK, GEN cusp)
    9325             : {
    9326             :   long v, N, dk, nk, t, o;
    9327             :   GEN mf, CHI, go, A, C, g, c, d;
    9328         315 :   if ((mf = checkMF_i(NK)))
    9329             :   {
    9330          49 :     GEN gk = MF_get_gk(mf);
    9331          49 :     N = MF_get_N(mf);
    9332          49 :     CHI = MF_get_CHI(mf);
    9333          49 :     Qtoss(gk, &nk, &dk);
    9334             :   }
    9335             :   else
    9336         266 :     checkNK2(NK, &N, &nk, &dk, &CHI, 0);
    9337         315 :   if (typ(cusp) == t_INFINITY) return 1;
    9338         315 :   if (typ(cusp) == t_FRAC) { A = gel(cusp,1); C = gel(cusp,2); }
    9339          28 :   else { A = cusp; C = gen_1; }
    9340         315 :   g = diviuexact(mului(N,C), ugcd(N, Fl_sqr(umodiu(C,N), N)));
    9341         315 :   c = mulii(negi(C),g);
    9342         315 :   d = addiu(mulii(A,g), 1);
    9343         315 :   if (!CHI) return 1;
    9344         315 :   go = gmfcharorder(CHI);
    9345         315 :   v = vali(go); if (v < 2) go = shifti(go, 2-v);
    9346         315 :   t = itou( znchareval(gel(CHI,1), gel(CHI,2), d, go) );
    9347         315 :   if (dk == 1) return t == 0;
    9348         154 :   o = itou(go);
    9349         154 :   if (kronecker(c,d) < 0) t = Fl_add(t, o/2, o);
    9350         154 :   if (Mod4(d) == 1) return t == 0;
    9351          14 :   t = Fl_sub(t, Fl_mul(o/4, nk, o), o);
    9352          14 :   return t == 0;
    9353             : }
    9354             : 
    9355             : /* Some useful closures */
    9356             : 
    9357             : /* sum_{d|n} d^k */
    9358             : static GEN
    9359       37660 : mysumdivku(ulong n, ulong k)
    9360             : {
    9361       37660 :   GEN fa = myfactoru(n);
    9362       37660 :   return k == 1? usumdiv_fact(fa): usumdivk_fact(fa,k);
    9363             : }
    9364             : static GEN
    9365         812 : c_Ek(long n, long d, GEN F)
    9366             : {
    9367         812 :   GEN E = cgetg(n + 2, t_VEC), C = gel(F,2);
    9368         812 :   long i, k = mf_get_k(F);
    9369         812 :   gel (E, 1) = gen_1;
    9370       25270 :   for (i = 1; i <= n; i++)
    9371             :   {
    9372       24458 :     pari_sp av = avma;
    9373       24458 :     gel(E, i+1) = gerepileupto(av, gmul(C, mysumdivku(i*d, k-1)));
    9374             :   }
    9375         812 :   return E;
    9376             : }
    9377             : 
    9378             : GEN
    9379         364 : mfEk(long k)
    9380             : {
    9381         364 :   pari_sp av = avma;
    9382             :   GEN E0, NK;
    9383         364 :   if (k < 0 || odd(k)) pari_err_TYPE("mfEk [incorrect k]", stoi(k));
    9384         364 :   if (!k) return mf1();
    9385         357 :   E0 = gdivsg(-2*k, bernfrac(k));
    9386         357 :   NK = mkNK(1,k,mfchartrivial());
    9387         357 :   return gerepilecopy(av, tag(t_MF_Ek, NK, E0));
    9388             : }
    9389             : 
    9390             : GEN
    9391          56 : mfDelta(void)
    9392             : {
    9393          56 :   pari_sp av = avma;
    9394          56 :   return gerepilecopy(av, tag0(t_MF_DELTA, mkNK(1,12,mfchartrivial())));
    9395             : }
    9396             : 
    9397             : GEN
    9398         742 : mfTheta(GEN psi)
    9399             : {
    9400         742 :   pari_sp av = avma;
    9401             :   GEN N, gk, psi2;
    9402             :   long par;
    9403         742 :   if (!psi) { psi = mfchartrivial(); N = utoipos(4); par = 1; }
    9404             :   else
    9405             :   {
    9406             :     long FC;
    9407          21 :     psi = get_mfchar(psi);
    9408          21 :     FC = mfcharconductor(psi);
    9409          21 :     if (mfcharmodulus(psi) != FC)
    9410           0 :       pari_err_TYPE("mfTheta [nonprimitive character]", psi);
    9411          21 :     par = mfcharparity(psi);
    9412          21 :     N = shifti(sqru(FC),2);
    9413             :   }
    9414         742 :   if (par > 0) { gk = ghalf; psi2 = psi; }
    9415           7 :   else { gk = gsubsg(2, ghalf); psi2 = mfcharmul(psi, get_mfchar(stoi(-4))); }
    9416         742 :   return gerepilecopy(av, tag(t_MF_THETA, mkgNK(N, gk, psi2, pol_x(1)), psi));
    9417             : }
    9418             : 
    9419             : /* Output 0 if not desired eta product: if flag=0 (default) require
    9420             :  * holomorphic at cusps. If flag set, accept meromorphic, but sill in some
    9421             :  * modular function space */
    9422             : GEN
    9423         196 : mffrometaquo(GEN eta, long flag)
    9424             : {
    9425         196 :   pari_sp av = avma;
    9426             :   GEN NK, N, k, BR, P;
    9427         196 :   long v, cusp = 0;
    9428         196 :   if (!etaquotype(&eta, &N,&k,&P, &v, NULL, flag? NULL: &cusp) || cusp < 0)
    9429          14 :     return gc_const(av, gen_0);
    9430         182 :   if (lg(gel(eta,1)) == 1) { set_avma(av); return mf1(); }
    9431         175 :   BR = mkvec2(ZV_to_zv(gel(eta,1)), ZV_to_zv(gel(eta,2)));
    9432         175 :   if (v < 0) v = 0;
    9433         175 :   NK = mkgNK(N, k, get_mfchar(P), pol_x(1));
    9434         175 :   return gerepilecopy(av, tag2(t_MF_ETAQUO, NK, BR, utoi(v)));
    9435             : }
    9436             : 
    9437             : /* Q^(-r) */
    9438             : static GEN
    9439         361 : RgXn_negpow(GEN Q, long r, long L)
    9440             : {
    9441         361 :   if (r < 0) r = -r; else Q = RgXn_inv_i(Q, L);
    9442         361 :   if (r != 1) Q = RgXn_powu_i(Q, r, L);
    9443         361 :   return Q;
    9444             : }
    9445             : /* flag same as in mffrometaquo: if set, accept meromorphic. */
    9446             : static GEN
    9447          42 : mfisetaquo_i(GEN F, long flag)
    9448             : {
    9449             :   GEN gk, P, E, M, S, G, CHI, v, w;
    9450             :   long b, l, L, N, vS, m, j;
    9451          42 :   const long bextra = 10;
    9452             : 
    9453          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfisetaquo",F);
    9454          42 :   CHI = mf_get_CHI(F); if (mfcharorder(CHI) > 2) return NULL;
    9455          42 :   N = mf_get_N(F);
    9456          42 :   gk = mf_get_gk(F);
    9457          42 :   b = mfsturmNgk(N, gk);
    9458          42 :   L = maxss(N, b) + bextra;
    9459          42 :   S = mfcoefs_i(F, L, 1);
    9460          42 :   if (!RgV_is_ZV(S)) return NULL;
    9461         203 :   for (vS = 1; vS <= L+1; vS++)
    9462         203 :     if (signe(gel(S,vS))) break;
    9463          42 :   vS--; if (vS) { S = vecslice(S, vS+1, L+1); L -= vS; }
    9464          42 :   S = RgV_to_RgX(S, 0); l = lg(S)-2;
    9465          42 :   P = cgetg(l, t_COL);
    9466          42 :   E = cgetg(l, t_COL); w = v = gen_0; /* w = weight, v = valuation */
    9467         550 :   for (m = j = 1; m+2 < lg(S); m++)
    9468             :   {
    9469         515 :     GEN c = gel(S,m+2);
    9470             :     long r;
    9471         515 :     if (is_bigint(c)) return NULL;
    9472         508 :     r = -itos(c);
    9473         508 :     if (r)
    9474             :     {
    9475         361 :       S = ZXn_mul(S, RgXn_negpow(eta_ZXn(m, L), r, L), L);
    9476         361 :       gel(P,j) = utoipos(m);
    9477         361 :       gel(E,j) = stoi(r);
    9478         361 :       v = addmuliu(v, gel(E,j), m);
    9479         361 :       w = addis(w, r);
    9480         361 :       j++;
    9481             :     }
    9482             :   }
    9483          35 :   if (!equalii(w, gmul2n(gk, 1)) || (!flag && !equalii(v, muluu(24,vS))))
    9484           7 :     return NULL;
    9485          28 :   setlg(P, j);
    9486          28 :   setlg(E, j); M = mkmat2(P, E); G = mffrometaquo(M, flag);
    9487          28 :   return (typ(G) != t_INT
    9488          28 :           && (mfsturmmf(G) <= b + bextra || mfisequal(F, G, b)))? M: NULL;
    9489             : }
    9490             : GEN
    9491          42 : mfisetaquo(GEN F, long flag)
    9492             : {
    9493          42 :   pari_sp av = avma;
    9494          42 :   GEN M = mfisetaquo_i(F, flag);
    9495          42 :   return M? gerepilecopy(av, M): gc_const(av, gen_0);
    9496             : }
    9497             : 
    9498             : #if 0
    9499             : /* number of primitive characters modulo N */
    9500             : static ulong
    9501             : numprimchars(ulong N)
    9502             : {
    9503             :   GEN fa, P, E;
    9504             :   long i, l;
    9505             :   ulong n;
    9506             :   if ((N & 3) == 2) return 0;
    9507             :   fa = myfactoru(N);
    9508             :   P = gel(fa,1); l = lg(P);
    9509             :   E = gel(fa,2);
    9510             :   for (i = n = 1; i < l; i++)
    9511             :   {
    9512             :     ulong p = P[i], e = E[i];
    9513             :     if (e == 2) n *= p-2; else n *= (p-1)*(p-1)*upowuu(p,e-2);
    9514             :   }
    9515             :   return n;
    9516             : }
    9517             : #endif
    9518             : 
    9519             : /* Space generated by products of two Eisenstein series */
    9520             : 
    9521             : static int
    9522       73122 : cmp_small_priority(void *E, GEN a, GEN b)
    9523             : {
    9524       73122 :   GEN prio = (GEN)E;
    9525       73122 :   return cmpss(prio[(long)a], prio[(long)b]);
    9526             : }
    9527             : static long
    9528        1148 : znstar_get_expo(GEN G) { return itou(cyc_get_expo(znstar_get_cyc(G))); }
    9529             : 
    9530             : /* Return [vchi, bymod, vG]:
    9531             :  * vG[f] = znstar(f,1) for f a conductor of (at least) a char mod N; else NULL
    9532             :  * bymod[f] = vecsmall of conrey indexes of chars modulo f | N; else NULL
    9533             :  * vchi[n] = a list of CHIvec [G0,chi0,o,ncharvecexpo(G0,nchi0),...]:
    9534             :  *   chi0 = primitive char attached to Conrey Mod(n,N)
    9535             :  * (resp. NULL if (n,N) > 1) */
    9536             : static GEN
    9537         574 : charsmodN(long N)
    9538             : {
    9539         574 :   GEN D, G, prio, phio, dummy = cgetg(1,t_VEC);
    9540         574 :   GEN vP, vG = const_vec(N,NULL), vCHI  = const_vec(N,NULL);
    9541         574 :   GEN bymod = const_vec(N,NULL);
    9542         574 :   long pn, i, l, vt = fetch_user_var("t");
    9543         574 :   D = mydivisorsu(N); l = lg(D);
    9544        3598 :   for (i = 1; i < l; i++)
    9545        3024 :     gel(bymod, D[i]) = vecsmalltrunc_init(myeulerphiu(D[i])+1);
    9546         574 :   gel(vG,N) = G = znstar0(utoipos(N),1);
    9547         574 :   pn = znstar_get_expo(G);  /* exponent(Z/NZ)^* */
    9548         574 :   vP = const_vec(pn,NULL);
    9549       26152 :   for (i = 1; i <= N; i++)
    9550             :   {
    9551             :     GEN P, gF, G0, chi0, nchi0, chi, v, go;
    9552             :     long j, F, o;
    9553       25578 :     if (ugcd(i,N) != 1) continue;
    9554       13601 :     chi = znconreylog(G, utoipos(i));
    9555       13601 :     gF = znconreyconductor(G, chi, &chi0);
    9556       13601 :     F = (typ(gF) == t_INT)? itou(gF): itou(gel(gF,1));
    9557       13601 :     G0 = gel(vG, F); if (!G0) G0 = gel(vG,F) = znstar0(gF, 1);
    9558       13601 :     nchi0 = znconreylog_normalize(G0,chi0);
    9559       13601 :     go = gel(nchi0,1); o = itou(go); /* order(chi0) */
    9560       13601 :     v = ncharvecexpo(G0, nchi0);
    9561       13601 :     if (!equaliu(go, pn)) v = zv_z_mul(v, pn / o);
    9562       13601 :     P = gel(vP, o); if (!P) P = gel(vP,o) = polcyclo(o,vt);
    9563             :     /* mfcharcxinit with dummy complex powers */
    9564       13601 :     gel(vCHI,i) = mkvecn(6, G0, chi0, go, v, dummy, P);
    9565       13601 :     D = mydivisorsu(N / F); l = lg(D);
    9566       39179 :     for (j = 1; j < l; j++) vecsmalltrunc_append(gel(bymod, F*D[j]), i);
    9567             :   }
    9568         574 :   phio = zero_zv(pn); l = lg(vCHI); prio = cgetg(l, t_VEC);
    9569       26152 :   for (i = 1; i < l; i++)
    9570             :   {
    9571       25578 :     GEN CHI = gel(vCHI,i);
    9572             :     long o;
    9573       25578 :     if (!CHI) continue;
    9574       13601 :     o = CHIvec_ord(CHI);
    9575       13601 :     if (!phio[o]) phio[o] = myeulerphiu(o);
    9576       13601 :     prio[i] = phio[o];
    9577             :   }
    9578         574 :   l = lg(bymod);
    9579             :   /* sort characters by increasing value of phi(order) */
    9580       26152 :   for (i = 1; i < l; i++)
    9581             :   {
    9582       25578 :     GEN z = gel(bymod,i);
    9583       25578 :     if (z) gen_sort_inplace(z, (void*)prio, &cmp_small_priority, NULL);
    9584             :   }
    9585         574 :   return mkvec3(vCHI, bymod, vG);
    9586             : }
    9587             : 
    9588             : static GEN
    9589        4893 : mfeisenstein2pure(long k, GEN CHI1, GEN CHI2, long ord, GEN P, long lim)
    9590             : {
    9591        4893 :   GEN c, V = cgetg(lim+2, t_COL);
    9592             :   long n;
    9593        4893 :   c = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9594        4893 :   if (P) c = grem(c, P);
    9595        4893 :   gel(V,1) = c;
    9596       97587 :   for (n=1; n <= lim; n++)
    9597             :   {
    9598       92694 :     c = sigchi2(k, CHI1, CHI2, n, ord);
    9599       92694 :     if (P) c = grem(c, P);
    9600       92694 :     gel(V,n+1) = c;
    9601             :   }
    9602        4893 :   return V;
    9603             : }
    9604             : static GEN
    9605        4137 : mfeisenstein2pure_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p, long lim)
    9606             : {
    9607        4137 :   GEN V = cgetg(lim+2, t_VECSMALL);
    9608             :   long n;
    9609        4137 :   V[1] = mfeisenstein2_0_Fl(k, CHI1vec, CHI2vec, vz, p);
    9610       58625 :   for (n=1; n <= lim; n++) V[n+1] = sigchi2_Fl(k, CHI1vec, CHI2vec, n, vz, p);
    9611        4137 :   return V;
    9612             : }
    9613             : 
    9614             : static GEN
    9615         224 : getcolswt2(GEN M, GEN D, ulong p)
    9616             : {
    9617         224 :   GEN R, v = gel(M,1);
    9618         224 :   long i, l = lg(M) - 1;
    9619         224 :   R = cgetg(l, t_MAT); /* skip D[1] = 1 */
    9620         896 :   for (i = 1; i < l; i++)
    9621             :   {
    9622         672 :     GEN w = Flv_Fl_mul(gel(M,i+1), D[i+1], p);
    9623         672 :     gel(R,i) = Flv_sub(v, w, p);
    9624             :   }
    9625         224 :   return R;
    9626             : }
    9627             : static GEN
    9628        5124 : expandbd(GEN V, long d)
    9629             : {
    9630             :   long L, n, nd;
    9631             :   GEN W;
    9632        5124 :   if (d == 1) return V;
    9633        1988 :   L = lg(V)-1; W = zerocol(L); /* nd = n/d */
    9634       17500 :   for (n = nd = 0; n < L; n += d, nd++) gel(W, n+1) = gel(V, nd+1);
    9635        1988 :   return W;
    9636             : }
    9637             : static GEN
    9638        6587 : expandbd_Fl(GEN V, long d)
    9639             : {
    9640             :   long L, n, nd;
    9641             :   GEN W;
    9642        6587 :   if (d == 1) return V;
    9643        2450 :   L = lg(V)-1; W = zero_Flv(L); /* nd = n/d */
    9644       15729 :   for (n = nd = 0; n < L; n += d, nd++) W[n+1] = V[nd+1];
    9645        2450 :   return W;
    9646             : }
    9647             : static void
    9648        4137 : getcols_i(GEN *pM, GEN *pvj, GEN gk, GEN CHI1vec, GEN CHI2vec, long NN1, GEN vz,
    9649             :           ulong p, long lim)
    9650             : {
    9651        4137 :   GEN CHI1 = CHIvec_CHI(CHI1vec), CHI2 = CHIvec_CHI(CHI2vec);
    9652        4137 :   long N2 = CHIvec_N(CHI2vec);
    9653        4137 :   GEN vj, M, D = mydivisorsu(NN1/N2);
    9654        4137 :   long i, l = lg(D), k = gk[2];
    9655        4137 :   GEN V = mfeisenstein2pure_Fl(k, CHI1vec, CHI2vec, vz, p, lim);
    9656        4137 :   M = cgetg(l, t_MAT);
    9657       10724 :   for (i = 1; i < l; i++) gel(M,i) = expandbd_Fl(V, D[i]);
    9658        4137 :   if (k == 2 && N2 == 1 && CHIvec_N(CHI1vec) == 1)
    9659             :   {
    9660         224 :     M = getcolswt2(M, D, p); l--;
    9661         224 :     D = vecslice(D, 2, l);
    9662             :   }
    9663        4137 :   *pM = M;
    9664        4137 :   *pvj = vj = cgetg(l, t_VEC);
    9665       10500 :   for (i = 1; i < l; i++) gel(vj,i) = mkvec4(gk, CHI1, CHI2, utoipos(D[i]));
    9666        4137 : }
    9667             : 
    9668             : /* find all CHI1, CHI2 mod N such that CHI1*CHI2 = CHI, f(CHI1)*f(CHI2) | N.
    9669             :  * set M = mfcoefs(B_e E(CHI1,CHI2), lim), vj = [e,i1,i2] */
    9670             : static void
    9671        1687 : getcols(GEN *pM, GEN *pv, long k, long nCHI, GEN allN, GEN vz, ulong p,
    9672             :         long lim)
    9673             : {
    9674        1687 :   GEN vCHI = gel(allN,1), gk = utoi(k);
    9675        1687 :   GEN M = cgetg(1,t_MAT), v = cgetg(1,t_VEC);
    9676        1687 :   long i1, N = lg(vCHI)-1;
    9677       84938 :   for (i1 = 1; i1 <= N; i1++)
    9678             :   {
    9679       83251 :     GEN CHI1vec = gel(vCHI, i1), CHI2vec, M1, v1;
    9680             :     long NN1, i2;
    9681      145859 :     if (!CHI1vec) continue;
    9682       65632 :     if (k == 1 && CHIvec_parity(CHI1vec) == -1) continue;
    9683       40936 :     NN1 = N/CHIvec_N(CHI1vec); /* N/f(chi1) */;
    9684       40936 :     i2 = Fl_div(nCHI,i1, N);
    9685       40936 :     if (!i2) i2 = 1;
    9686       40936 :     CHI2vec = gel(vCHI,i2);
    9687       40936 :     if (NN1 % CHIvec_N(CHI2vec)) continue; /* f(chi1)f(chi2) | N ? */
    9688        3024 :     getcols_i(&M1, &v1, gk, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9689        3024 :     M = shallowconcat(M, M1);
    9690        3024 :     v = shallowconcat(v, v1);
    9691             :   }
    9692        1687 :   *pM = M;
    9693        1687 :   *pv = v;
    9694        1687 : }
    9695             : 
    9696             : static void
    9697        1120 : update_Mj(GEN *M, GEN *vecj, GEN *pz, ulong p)
    9698             : {
    9699             :   GEN perm;
    9700        1120 :   *pz = Flm_indexrank(*M, p); perm = gel(*pz,2);
    9701        1120 :   *M = vecpermute(*M, perm);
    9702        1120 :   *vecj = vecpermute(*vecj, perm);
    9703        1120 : }
    9704             : static int
    9705         357 : getcolsgen(long dim, GEN *pM, GEN *pvj, GEN *pz, long k, long ell, long nCHI,
    9706             :            GEN allN, GEN vz, ulong p, long lim)
    9707             : {
    9708         357 :   GEN vCHI = gel(allN,1), bymod = gel(allN,2), gell = utoi(ell);
    9709         357 :   long i1, N = lg(vCHI)-1;
    9710         357 :   long L = lim+1;
    9711         357 :   if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9712         357 :   if (lg(*pvj)-1 == dim) return 1;
    9713        1526 :   for (i1 = 1; i1 <= N; i1++)
    9714             :   {
    9715        1505 :     GEN CHI1vec = gel(vCHI, i1), T;
    9716             :     long par1, j, l, N1, NN1;
    9717             : 
    9718        1505 :     if (!CHI1vec) continue;
    9719        1484 :     par1 = CHIvec_parity(CHI1vec);
    9720        1484 :     if (ell == 1 && par1 == -1) continue;
    9721         903 :     if (odd(ell)) par1 = -par1;
    9722         903 :     N1 = CHIvec_N(CHI1vec);
    9723         903 :     NN1 = N/N1;
    9724         903 :     T = gel(bymod, NN1); l = lg(T);
    9725        3528 :     for (j = 1; j < l; j++)
    9726             :     {
    9727        2947 :       long i2 = T[j], l1, l2, j1, s, nC;
    9728        2947 :       GEN M, M1, M2, vj, vj1, vj2, CHI2vec = gel(vCHI, i2);
    9729        2947 :       if (CHIvec_parity(CHI2vec) != par1) continue;
    9730        1113 :       nC = Fl_div(nCHI, Fl_mul(i1,i2,N), N);
    9731        1113 :       getcols(&M2, &vj2, k-ell, nC, allN, vz, p, lim);
    9732        1113 :       l2 = lg(M2); if (l2 == 1) continue;
    9733        1113 :       getcols_i(&M1, &vj1, gell, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9734        1113 :       l1 = lg(M1);
    9735        1113 :       M1 = Flm_to_FlxV(M1, 0);
    9736        1113 :       M2 = Flm_to_FlxV(M2, 0);
    9737        1113 :       M  = cgetg((l1-1)*(l2-1) + 1, t_MAT);
    9738        1113 :       vj = cgetg((l1-1)*(l2-1) + 1, t_VEC);
    9739        2737 :       for (j1 = s = 1; j1 < l1; j1++)
    9740             :       {
    9741        1624 :         GEN E = gel(M1,j1), v = gel(vj1,j1);
    9742             :         long j2;
    9743        6811 :         for (j2 = 1; j2 < l2; j2++, s++)
    9744             :         {
    9745        5187 :           GEN c = Flx_to_Flv(Flxn_mul(E, gel(M2,j2), L, p), L);
    9746        5187 :           gel(M,s) = c;
    9747        5187 :           gel(vj,s) = mkvec2(v, gel(vj2,j2));
    9748             :         }
    9749             :       }
    9750        1113 :       *pM = shallowconcat(*pM, M);
    9751        1113 :       *pvj = shallowconcat(*pvj, vj);
    9752        1113 :       if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9753        1113 :       if (lg(*pvj)-1 == dim) return 1;
    9754             :     }
    9755             :   }
    9756          21 :   if (ell == 1)
    9757             :   {
    9758          21 :     update_Mj(pM, pvj, pz, p);
    9759          21 :     return (lg(*pvj)-1 == dim);
    9760             :   }
    9761           0 :   return 0;
    9762             : }
    9763             : 
    9764             : static GEN
    9765        1456 : mkF2bd(long d, long lim)
    9766             : {
    9767        1456 :   GEN V = zerovec(lim + 1);
    9768             :   long n;
    9769        1456 :   gel(V, 1) = ginv(stoi(-24));
    9770       14623 :   for (n = 1; n <= lim/d; n++) gel(V, n*d + 1) = mysumdivku(n, 1);
    9771        1456 :   return V;
    9772             : }
    9773             : 
    9774             : static GEN
    9775        5467 : mkeisen(GEN E, long ord, GEN P, long lim)
    9776             : {
    9777        5467 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9778        5467 :   GEN CHI1 = gel(E,2), CHI2 = gel(E,3);
    9779        5467 :   if (k == 2 && mfcharistrivial(CHI1) && mfcharistrivial(CHI2))
    9780         574 :     return gsub(mkF2bd(1,lim), gmulgs(mkF2bd(e,lim), e));
    9781             :   else
    9782             :   {
    9783        4893 :     GEN V = mfeisenstein2pure(k, CHI1, CHI2, ord, P, lim);
    9784        4893 :     return expandbd(V, e);
    9785             :   }
    9786             : }
    9787             : static GEN
    9788         532 : mkM(GEN vj, long pn, GEN P, long lim)
    9789             : {
    9790         532 :   long j, l = lg(vj), L = lim+1;
    9791         532 :   GEN M = cgetg(l, t_MAT);
    9792        4508 :   for (j = 1; j < l; j++)
    9793             :   {
    9794             :     GEN E1, E2;
    9795        3976 :     parse_vecj(gel(vj,j), &E1,&E2);
    9796        3976 :     E1 = RgV_to_RgX(mkeisen(E1, pn, P, lim), 0);
    9797        3976 :     if (E2)
    9798             :     {
    9799        1491 :       E2 = RgV_to_RgX(mkeisen(E2, pn, P, lim), 0);
    9800        1491 :       E1 = RgXn_mul(E1, E2, L);
    9801             :     }
    9802        3976 :     E1 = RgX_to_RgC(E1, L);
    9803        3976 :     if (P && E2) E1 = RgXQV_red(E1, P);
    9804        3976 :     gel(M,j) = E1;
    9805             :   }
    9806         532 :   return M;
    9807             : }
    9808             : 
    9809             : /* assume N > 2 */
    9810             : static GEN
    9811          35 : mffindeisen1(long N)
    9812             : {
    9813          35 :   GEN G = znstar0(utoipos(N), 1), L = chargalois(G, NULL), chi0 = NULL;
    9814          35 :   long j, m = N, l = lg(L);
    9815         259 :   for (j = 1; j < l; j++)
    9816             :   {
    9817         245 :     GEN chi = gel(L,j);
    9818         245 :     long r = myeulerphiu(itou(zncharorder(G,chi)));
    9819         245 :     if (r >= m) continue;
    9820         182 :     chi = znconreyfromchar(G, chi);
    9821         182 :     if (zncharisodd(G,chi)) { m = r; chi0 = chi; if (r == 1) break; }
    9822             :   }
    9823          35 :   if (!chi0) pari_err_BUG("mffindeisen1 [no Eisenstein series found]");
    9824          35 :   chi0 = znchartoprimitive(G,chi0);
    9825          35 :   return mfcharGL(gel(chi0,1), gel(chi0,2));
    9826             : }
    9827             : 
    9828             : static GEN
    9829         574 : mfeisensteinspaceinit_i(long N, long k, GEN CHI)
    9830             : {
    9831         574 :   GEN M, Minv, vj, vG, GN, allN, P, vz, z = NULL;
    9832         574 :   long nCHI, lim, ell, ord, dim = mffulldim(N, k, CHI);
    9833             :   ulong r, p;
    9834             : 
    9835         574 :   if (!dim) retmkvec3(cgetg(1,t_VECSMALL),
    9836             :                       mkvec2(cgetg(1,t_MAT),gen_1),cgetg(1,t_VEC));
    9837         574 :   lim = mfsturmNk(N, k) + 1;
    9838         574 :   allN = charsmodN(N);
    9839         574 :   vG = gel(allN,3);
    9840         574 :   GN = gel(vG,N);
    9841         574 :   ord = znstar_get_expo(GN);
    9842         574 :   P = ord <= 2? NULL: polcyclo(ord, varn(mfcharpol(CHI)));
    9843         574 :   CHI = induce(GN, CHI); /* lift CHI mod N before mfcharno*/
    9844         574 :   nCHI = mfcharno(CHI);
    9845         574 :   r = QabM_init(ord, &p);
    9846         574 :   vz = Fl_powers(r, ord, p);
    9847         574 :   getcols(&M, &vj, k, nCHI, allN, vz, p, lim);
    9848         595 :   for (ell = k>>1; ell >= 1; ell--)
    9849         357 :     if (getcolsgen(dim, &M, &vj, &z, k, ell, nCHI, allN, vz, p, lim)) break;
    9850         574 :   if (!z) update_Mj(&M, &vj, &z, p);
    9851         574 :   if (lg(vj) - 1 < dim) return NULL;
    9852         532 :   M = mkM(vj, ord, P, lim);
    9853         532 :   Minv = QabM_Minv(rowpermute(M, gel(z,1)), P, ord);
    9854         532 :   return mkvec4(gel(z,1), Minv, vj, utoi(ord));
    9855             : }
    9856             : /* true mf */
    9857             : static GEN
    9858         532 : mfeisensteinspaceinit(GEN mf)
    9859             : {
    9860         532 :   pari_sp av = avma;
    9861         532 :   GEN z, CHI = MF_get_CHI(mf);
    9862         532 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    9863         532 :   if (!CHI) CHI = mfchartrivial();
    9864         532 :   z = mfeisensteinspaceinit_i(N, k, CHI);
    9865         532 :   if (!z)
    9866             :   {
    9867          35 :     GEN E, CHIN = mffindeisen1(N), CHI0 = mfchartrivial();
    9868          35 :     z = mfeisensteinspaceinit_i(N, k+1, mfcharmul(CHI, CHIN));
    9869          35 :     if (z) E = mkvec4(gen_1, CHI0, CHIN, gen_1);
    9870             :     else
    9871             :     {
    9872           7 :       z = mfeisensteinspaceinit_i(N, k+2, CHI);
    9873           7 :       E = mkvec4(gen_2, CHI0, CHI0, utoipos(N));
    9874             :     }
    9875          35 :     z = mkvec2(z, E);
    9876             :   }
    9877         532 :   return gerepilecopy(av, z);
    9878             : }
    9879             : 
    9880             : /* decomposition of modular form on eisenspace */
    9881             : static GEN
    9882         945 : mfeisensteindec(GEN mf, GEN F)
    9883             : {
    9884         945 :   pari_sp av = avma;
    9885             :   GEN M, Mindex, Mvecj, V, B, CHI;
    9886             :   long o, ord;
    9887             : 
    9888         945 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    9889         945 :   if (lg(Mvecj) < 5)
    9890             :   {
    9891          56 :     GEN E, e = gel(Mvecj,2), gkE = gel(e,1);
    9892          56 :     long dE = itou(gel(e,4));
    9893          56 :     Mvecj = gel(Mvecj,1);
    9894          56 :     E = mfeisenstein(itou(gkE), NULL, gel(e,3));
    9895          56 :     if (dE != 1) E = mfbd_E2(E, dE, gel(e,2)); /* here k = 2 */
    9896          56 :     F = mfmul(F, E);
    9897             :   }
    9898         945 :   M = gel(Mvecj, 2);
    9899         945 :   if (lg(M) == 1) return cgetg(1, t_VEC);
    9900         945 :   Mindex = gel(Mvecj, 1);
    9901         945 :   ord = itou(gel(Mvecj,4));
    9902         945 :   V = mfcoefs(F, Mindex[lg(Mindex)-1]-1, 1); settyp(V, t_COL);
    9903         945 :   CHI = mf_get_CHI(F);
    9904         945 :   o = mfcharorder(CHI);
    9905         945 :   if (o > 2 && o != ord)
    9906             :   { /* convert Mod(.,polcyclo(o)) to Mod(., polcyclo(N)) for o | N,
    9907             :      * o and N both != 2 (mod 4) */
    9908          84 :     GEN z, P = gel(M,4); /* polcyclo(ord) */
    9909          84 :     long vt = varn(P);
    9910          84 :     z = gmodulo(pol_xn(ord/o, vt), P);
    9911          84 :     if (ord % o) pari_err_TYPE("mfeisensteindec", V);
    9912          84 :     V = gsubst(liftpol_shallow(V), vt, z);
    9913             :   }
    9914         945 :   B = Minv_RgC_mul(M, vecpermute(V, Mindex));
    9915         945 :   return gerepileupto(av, B);
    9916             : }
    9917             : 
    9918             : /*********************************************************************/
    9919             : /*                        END EISENSPACE                             */
    9920             : /*********************************************************************/
    9921             : 
    9922             : static GEN
    9923          70 : sertocol2(GEN S, long l)
    9924             : {
    9925          70 :   GEN C = cgetg(l + 2, t_COL);
    9926             :   long i;
    9927         420 :   for (i = 0; i <= l; i++) gel(C, i+1) = polcoef_i(S, i, -1);
    9928          70 :   return C;
    9929             : }
    9930             : 
    9931             : /* Compute polynomial P0 such that F=E4^(k/4)P0(E6/E4^(3/2)). */
    9932             : static GEN
    9933          14 : mfcanfindp0(GEN F, long k)
    9934             : {
    9935          14 :   pari_sp ltop = avma;
    9936             :   GEN E4, E6, V, V1, Q, W, res, M, B;
    9937             :   long l, j;
    9938          14 :   l = k/6 + 2;
    9939          14 :   V = mfcoefsser(F,l);
    9940          14 :   E4 = mfcoefsser(mfEk(4),l);
    9941          14 :   E6 = mfcoefsser(mfEk(6),l);
    9942          14 :   V1 = gdiv(V, gpow(E4, sstoQ(k,4), 0));
    9943          14 :   Q = gdiv(E6, gpow(E4, sstoQ(3,2), 0));
    9944          14 :   W = gpowers(Q, l - 1);
    9945          14 :   M = cgetg(l + 1, t_MAT);
    9946          70 :   for (j = 1; j <= l; j++) gel(M,j) = sertocol2(gel(W,j), l);
    9947          14 :   B = sertocol2(V1, l);
    9948          14 :   res = inverseimage(M, B);
    9949          14 :   if (lg(res) == 1) err_space(F);
    9950          14 :   return gerepilecopy(ltop, gtopolyrev(res, 0));
    9951             : }
    9952             : 
    9953             : /* Compute the first n+1 Taylor coeffs at tau=I of a modular form
    9954             :  * on SL_2(Z). */
    9955             : GEN
    9956          14 : mftaylor(GEN F, long n, long flreal, long prec)
    9957             : {
    9958          14 :   pari_sp ltop = avma;
    9959          14 :   GEN P0, Pm1 = gen_0, v;
    9960          14 :   GEN X2 = mkpoln(3, ghalf,gen_0,gneg(ghalf)); /* (x^2-1) / 2 */
    9961             :   long k, m;
    9962          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftaylor",F);
    9963          14 :   k = mf_get_k(F);
    9964          14 :   if (mf_get_N(F) != 1 || k < 0) pari_err_IMPL("mftaylor for this form");
    9965          14 :   P0 = mfcanfindp0(F, k);
    9966          14 :   v = cgetg(n+2, t_VEC); gel(v, 1) = RgX_coeff(P0,0);
    9967         154 :   for (m = 0; m < n; m++)
    9968             :   {
    9969         140 :     GEN P1 = gdivgs(gmulsg(-(k + 2*m), RgX_shift(P0,1)), 12);
    9970         140 :     P1 = gadd(P1, gmul(X2, RgX_deriv(P0)));
    9971         140 :     if (m) P1 = gsub(P1, gdivgs(gmulsg(m*(m+k-1), Pm1), 144));
    9972         140 :     Pm1 = P0; P0 = P1;
    9973         140 :     gel(v, m+2) = RgX_coeff(P0, 0);
    9974             :   }
    9975          14 :   if (flreal)
    9976             :   {
    9977           7 :     GEN pi2 = Pi2n(1, prec), pim4 = gmulsg(-2, pi2), VPC;
    9978           7 :     GEN C = gmulsg(3, gdiv(gpowgs(ggamma(ginv(utoi(4)), prec), 8), gpowgs(pi2, 6)));
    9979             :     /* E_4(i): */
    9980           7 :     GEN facn = gen_1;
    9981           7 :     VPC = gpowers(gmul(pim4, gsqrt(C, prec)), n);
    9982           7 :     C = gpow(C, sstoQ(k,4), prec);
    9983          84 :     for (m = 0; m <= n; m++)
    9984             :     {
    9985          77 :       gel(v, m+1) = gdiv(gmul(C, gmul(gel(v, m+1), gel(VPC, m+1))), facn);
    9986          77 :       facn = gmulgs(facn, m+1);
    9987             :     }
    9988             :   }
    9989          14 :   return gerepilecopy(ltop, v);
    9990             : }
    9991             : 
    9992             : #if 0
    9993             : /* To be used in mfeigensearch() */
    9994             : GEN
    9995             : mfreadratfile()
    9996             : {
    9997             :   GEN eqn;
    9998             :   pariFILE *F = pari_fopengz("rateigen300.gp");
    9999             :   eqn = gp_readvec_stream(F->file);
   10000             :   pari_fclose(F);
   10001             :   return eqn;
   10002             : }
   10003             : #endif
   10004             :  /*****************************************************************/
   10005             : /*           EISENSTEIN CUSPS: COMPLEX DIRECTLY: one F_k         */
   10006             : /*****************************************************************/
   10007             : 
   10008             : /* CHIvec = charinit(CHI); data = [N1g/g1,N2g/g2,g1/g,g2/g,C/g1,C/g2,
   10009             :  * (N1g/g1)^{-1},(N2g/g2)^{-1}] */
   10010             : 
   10011             : /* nm = n/m;
   10012             :  * z1 = powers of \z_{C/g}^{(Ae/g)^{-1}},
   10013             :  * z2 = powers of \z_N^{A^{-1}(g1g2/C)}]
   10014             :  * N.B. : we compute value and conjugate at the end, so it is (Ae/g)^{-1}
   10015             :  * and not -(Ae/g)^{-1} */
   10016             : static GEN
   10017     7643496 : eiscnm(long nm, long m, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1)
   10018             : {
   10019     7643496 :   long Cg1 = data[5], s10 = (nm*data[7]) % Cg1, r10 = (nm - data[1]*s10) / Cg1;
   10020     7643496 :   long Cg2 = data[6], s20 = (m *data[8]) % Cg2, r20 = (m  - data[2]*s20) / Cg2;
   10021             :   long j1, r1, s1;
   10022     7643496 :   GEN T = gen_0;
   10023    18404232 :   for (j1 = 0, r1 = r10, s1 = s10; j1 < data[3]; j1++, r1 -= data[1], s1 += Cg1)
   10024             :   {
   10025    10760736 :     GEN c1 = mychareval(CHI1vec, r1);
   10026    10760736 :     if (!gequal0(c1))
   10027             :     {
   10028             :       long j2, r2, s2;
   10029     7907396 :       GEN S = gen_0;
   10030    20468378 :       for (j2 = 0, r2 = r20, s2 = s20; j2 < data[4]; j2++, r2 -= data[2], s2 += Cg2)
   10031             :       {
   10032    12560982 :         GEN c2 = mychareval(CHI2vec, r2);
   10033    12560982 :         if (!gequal0(c2)) S = gadd(S, gmul(c2, rootsof1pow(z1, s1*s2)));
   10034             :       }
   10035     7907396 :       T = gadd(T, gmul(c1, S));
   10036             :     }
   10037             :   }
   10038     7643496 :   return conj_i(T);
   10039             : }
   10040             : 
   10041             : static GEN
   10042      593467 : fg1g2n(long n, long k, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1, GEN z2)
   10043             : {
   10044      593467 :   pari_sp av = avma;
   10045      593467 :   GEN S = gen_0, D = mydivisorsu(n);
   10046      593467 :   long i, l = lg(D);
   10047     4415215 :   for (i = 1; i < l; i++)
   10048             :   {
   10049     3821748 :     long m = D[i], nm = D[l-i]; /* n/m */
   10050     3821748 :     GEN u = eiscnm( nm,  m, CHI1vec, CHI2vec, data, z1);
   10051     3821748 :     GEN v = eiscnm(-nm, -m, CHI1vec, CHI2vec, data, z1);
   10052     3821748 :     GEN w = odd(k) ? gsub(u, v) : gadd(u, v);
   10053     3821748 :     S = gadd(S, gmul(powuu(m, k-1), w));
   10054             :   }
   10055      593467 :   return gerepileupto(av, gmul(S, rootsof1pow(z2, n)));
   10056             : }
   10057             : 
   10058             : static GEN
   10059       13230 : gausssumcx(GEN CHIvec, long prec)
   10060             : {
   10061             :   GEN z, S, V;
   10062       13230 :   long m, N = CHIvec_N(CHIvec);
   10063       13230 :   if (N == 1) return gen_1;
   10064        7210 :   V = CHIvec_val(CHIvec);
   10065        7210 :   z = rootsof1u_cx(N, prec);
   10066        7210 :   S = gmul(z, gel(V, N));
   10067      100835 :   for (m = N-1; m >= 1; m--) S = gmul(z, gadd(gel(V, m), S));
   10068        7210 :   return S;
   10069             : }
   10070             : 
   10071             : /* Computation of Q_k(\z_N^s) as a polynomial in \z_N^s. FIXME: explicit
   10072             :  * formula ? */
   10073             : static GEN
   10074        2191 : mfqk(long k, long N)
   10075             : {
   10076             :   GEN X, P, ZI, Q, Xm1, invden;
   10077             :   long i;
   10078        2191 :   ZI = gdivgs(RgX_shift_shallow(RgV_to_RgX(identity_ZV(N-1), 0), 1), N);
   10079        2191 :   if (k == 1) return ZI;
   10080        1113 :   P = gsubgs(pol_xn(N,0), 1);
   10081        1113 :   invden = RgXQ_powu(ZI, k, P);
   10082        1113 :   X = pol_x(0); Q = gneg(X); Xm1 = gsubgs(X, 1);
   10083        2765 :   for (i = 2; i < k; i++)
   10084        1652 :     Q = RgX_shift_shallow(ZX_add(gmul(Xm1, ZX_deriv(Q)), gmulsg(-i, Q)), 1);
   10085        1113 :   return RgXQ_mul(Q, invden, P);
   10086             : }
   10087             : 
   10088             : /* CHI mfchar; M is a multiple of the conductor of CHI, but is NOT
   10089             :  * necessarily its modulus */
   10090             : static GEN
   10091        3066 : mfskcx(long k, GEN CHI, long M, long prec)
   10092             : {
   10093             :   GEN S, CHIvec, P;
   10094             :   long F, m, i, l;
   10095        3066 :   CHI = mfchartoprimitive(CHI, &F);
   10096        3066 :   CHIvec = mfcharcxinit(CHI, prec);
   10097        3066 :   if (F == 1) S = gdivgs(bernfrac(k), k);
   10098             :   else
   10099             :   {
   10100        2191 :     GEN Q = mfqk(k, F), V = CHIvec_val(CHIvec);
   10101        2191 :     S = gmul(gel(V, F), RgX_coeff(Q, 0));
   10102       38682 :     for (m = 1; m < F; m++) S = gadd(S, gmul(gel(V, m), RgX_coeff(Q, m)));
   10103        2191 :     S = conj_i(S);
   10104             :   }
   10105             :   /* prime divisors of M not dividing f(chi) */
   10106        3066 :   P = gel(myfactoru(u_ppo(M/F,F)), 1); l = lg(P);
   10107        3192 :   for (i = 1; i < l; i++)
   10108             :   {
   10109         126 :     long p = P[i];
   10110         126 :     S = gmul(S, gsubsg(1, gdiv(mychareval(CHIvec, p), powuu(p, k))));
   10111             :   }
   10112        3066 :   return gmul(gmul(gausssumcx(CHIvec, prec), S), powuu(M/F, k));
   10113             : }
   10114             : 
   10115             : static GEN
   10116        5684 : f00_i(long k, GEN CHI1vec, GEN CHI2vec, GEN G2, GEN S, long prec)
   10117             : {
   10118             :   GEN c, a;
   10119        5684 :   long N1 = CHIvec_N(CHI1vec), N2 = CHIvec_N(CHI2vec);
   10120        5684 :   if (S[2] != N1) return gen_0;
   10121        3066 :   c = mychareval(CHI1vec, S[3]);
   10122        3066 :   if (isintzero(c)) return gen_0;
   10123        3066 :   a = mfskcx(k, mfchardiv(CHIvec_CHI(CHI2vec), CHIvec_CHI(CHI1vec)), N1*N2, prec);
   10124        3066 :   a = gmul(a, conj_i(gmul(c,G2)));
   10125        3066 :   return gdiv(a, mulsi(-N2, powuu(S[1], k-1)));
   10126             : }
   10127             : 
   10128             : static GEN
   10129        4487 : f00(long k, GEN CHI1vec,GEN CHI2vec, GEN G1,GEN G2, GEN data, long prec)
   10130             : {
   10131             :   GEN T1, T2;
   10132        4487 :   T2 = f00_i(k, CHI1vec, CHI2vec, G2, data, prec);
   10133        4487 :   if (k > 1) return T2;
   10134        1197 :   T1 = f00_i(k, CHI2vec, CHI1vec, G1, data, prec);
   10135        1197 :   return gadd(T1, T2);
   10136             : }
   10137             : 
   10138             : /* ga in SL_2(Z), find beta [a,b;c,d] in Gamma_0(N) and mu in Z such that
   10139             :  * beta * ga * T^u = [A',B';C',D'] with C' | N and N | B', C' > 0 */
   10140             : static void
   10141        5082 : mfgatogap(GEN ga, long N, long *pA, long *pC, long *pD, long *pd, long *pmu)
   10142             : {
   10143        5082 :   GEN A = gcoeff(ga,1,1), B = gcoeff(ga,1,2);
   10144        5082 :   GEN C = gcoeff(ga,2,1), D = gcoeff(ga,2,2), a, b, c, d;
   10145             :   long t, Ap, Cp, B1, D1, mu;
   10146        5082 :   Cp = itou(bezout(muliu(A,N), C, &c, &d)); /* divides N */
   10147        5082 :   t = 0;
   10148        5082 :   if (Cp > 1)
   10149             :   { /* (d, N/Cp) = 1, find t such that (d - t*(A*N/Cp), N) = 1 */
   10150        2408 :     long dN = umodiu(d,Cp), Q = (N/Cp * umodiu(A,Cp)) % Cp;
   10151        2779 :     while (ugcd(dN, Cp) > 1) { t++; dN = Fl_sub(dN, Q, Cp); }
   10152             :   }
   10153        5082 :   if (t)
   10154             :   {
   10155         371 :     c = addii(c, mului(t, diviuexact(C,Cp)));
   10156         371 :     d = subii(d, mului(t, muliu(A, N/Cp))); /* (d,N) = 1 */
   10157             :   }
   10158        5082 :   D1 = umodiu(mulii(d,D), N);
   10159        5082 :   (void)bezout(d, mulis(c,-N), &a, &b); /* = 1 */
   10160        5082 :   t = 0; Ap = umodiu(addii(mulii(a,A), mulii(b,C)), N); /* (Ap,Cp) = 1 */
   10161        6937 :   while (ugcd(Ap, N) > 1) { t++; Ap = Fl_add(Ap, Cp, N); }
   10162        5082 :   B1 = umodiu(a,N)*umodiu(B,N) + umodiu(b,N)*umodiu(D,N) + t*D1;
   10163        5082 :   B1 %= N;
   10164        5082 :   *pmu = mu = Fl_neg(Fl_div(B1, Ap, N), N);
   10165             :   /* A', D' and d only needed modulo N */
   10166        5082 :   *pd = umodiu(d, N);
   10167        5082 :   *pA = Ap;
   10168        5082 :   *pC = Cp; *pD = (D1 + Cp*mu) % N;
   10169        5082 : }
   10170             : 
   10171             : #if 0
   10172             : /* CHI is a mfchar, return alpha(CHI) */
   10173             : static long
   10174             : mfalchi(GEN CHI, long AN, long cg)
   10175             : {
   10176             :   GEN G = gel(CHI,1), chi = gel(CHI,2), go = gmfcharorder(CHI);
   10177             :   long o = itou(go), a = itos( znchareval(G, chi, stoi(1 + AN/cg), go) );
   10178             :   if (a < 0 || (cg * a) % o) pari_err_BUG("mfalchi");
   10179             :   return (cg * a) / o;
   10180             : }
   10181             : #endif
   10182             : /* return A such that CHI1(t) * CHI2(t) = e(A) or NULL if (t,N1*N2) > 1 */
   10183             : static GEN
   10184       10164 : mfcharmuleval(GEN CHI1vec, GEN CHI2vec, long t)
   10185             : {
   10186       10164 :   long a1 = mycharexpo(CHI1vec, t), o1 = CHIvec_ord(CHI1vec);
   10187       10164 :   long a2 = mycharexpo(CHI2vec, t), o2 = CHIvec_ord(CHI2vec);;
   10188       10164 :   if (a1 < 0 || a2 < 0) return NULL;
   10189       10164 :   return sstoQ(a1*o2 + a2*o1, o1*o2);
   10190             : }
   10191             : static GEN
   10192        5082 : mfcharmulcxeval(GEN CHI1vec, GEN CHI2vec, long t, long prec)
   10193             : {
   10194        5082 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, t);
   10195             :   long n, d;
   10196        5082 :   if (!A) return gen_0;
   10197        5082 :   Qtoss(A, &n,&d); return rootsof1q_cx(n, d, prec);
   10198             : }
   10199             : /* alpha(CHI1 * CHI2) */
   10200             : static long
   10201        5082 : mfalchi2(GEN CHI1vec, GEN CHI2vec, long AN, long cg)
   10202             : {
   10203        5082 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, 1 + AN/cg);
   10204             :   long a;
   10205        5082 :   if (!A) pari_err_BUG("mfalchi2");
   10206        5082 :   A = gmulsg(cg, A);
   10207        5082 :   if (typ(A) != t_INT) pari_err_BUG("mfalchi2");
   10208        5082 :   a = itos(A) % cg; if (a < 0) a += cg;
   10209        5082 :   return a;
   10210             : }
   10211             : 
   10212             : /* return g = (a,b), set u >= 0 s.t. g = a * u (mod b) */
   10213             : static long
   10214       20328 : mybezout(long a, long b, long *pu)
   10215             : {
   10216       20328 :   long junk, g = cbezout(a, b, pu, &junk);
   10217       20328 :   if (*pu < 0) *pu += b/g;
   10218       20328 :   return g;
   10219             : }
   10220             : 
   10221             : /* E = [k, CHI1,CHI2, e], CHI1 and CHI2 primitive mfchars such that,
   10222             :  * CHI1(-1)*CHI2(-1) = (-1)^k; expansion of (B_e (E_k(CHI1,CHI2))) | ga.
   10223             :  * w is the width for the space of the calling function. */
   10224             : static GEN
   10225        5082 : mfeisensteingacx(GEN E, long w, GEN ga, long lim, long prec)
   10226             : {
   10227        5082 :   GEN CHI1vec, CHI2vec, CHI1 = gel(E,2), CHI2 = gel(E,3), v, S, ALPHA;
   10228             :   GEN G1, G2, z1, z2, data;
   10229        5082 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
   10230        5082 :   long N1 = mfcharmodulus(CHI1);
   10231        5082 :   long N2 = mfcharmodulus(CHI2), N = e * N1 * N2;
   10232             :   long NsurC, cg, wN, A, C, Ai, d, mu, alchi, na, da;
   10233             :   long eg, g, gH, U, u0, u1, u2, Aig, H, m, n, t, Cg, NC1, NC2;
   10234             : 
   10235        5082 :   mfgatogap(ga, N, &A, &C, &Ai, &d, &mu);
   10236        5082 :   CHI1vec = mfcharcxinit(CHI1, prec);
   10237        5082 :   CHI2vec = mfcharcxinit(CHI2, prec);
   10238        5082 :   NsurC = N/C; cg  = ugcd(C, NsurC); wN = NsurC / cg;
   10239        5082 :   if (w%wN) pari_err_BUG("mfeisensteingacx [wN does not divide w]");
   10240        5082 :   alchi = mfalchi2(CHI1vec, CHI2vec, A*N, cg);
   10241        5082 :   ALPHA = sstoQ(alchi, NsurC);
   10242             : 
   10243        5082 :   g = mybezout(A*e, C, &u0); Cg = C/g; eg = e/g;
   10244        5082 :   NC1 = mybezout(N1, Cg, &u1);
   10245        5082 :   NC2 = mybezout(N2, Cg, &u2);
   10246        5082 :   H = (NC1*NC2*g)/Cg;
   10247        5082 :   Aig = (Ai*H)%N; if (Aig < 0) Aig += N;
   10248        5082 :   z1 = rootsof1powinit(u0, Cg, prec);
   10249        5082 :   z2 = rootsof1powinit(Aig, N, prec);
   10250        5082 :   data = mkvecsmalln(8, N1/NC1, N2/NC2, NC1, NC2, Cg/NC1, Cg/NC2, u1, u2);
   10251        5082 :   v = zerovec(lim + 1);
   10252             :   /* need n*H = alchi (mod cg) */
   10253        5082 :   gH = mybezout(H, cg, &U);
   10254        5082 :   if (gH > 1)
   10255             :   {
   10256         399 :     if (alchi % gH) return mkvec2(gen_0, v);
   10257         399 :     alchi /= gH; cg /= gH; H /= gH;
   10258             :   }
   10259        5082 :   G1 = gausssumcx(CHI1vec, prec);
   10260        5082 :   G2 = gausssumcx(CHI2vec, prec);
   10261        5082 :   if (!alchi)
   10262        4487 :     gel(v,1) = f00(k, CHI1vec,CHI2vec,G1,G2, mkvecsmall3(NC2,Cg,A*eg), prec);
   10263        5082 :   n = Fl_mul(alchi,U,cg); if (!n) n = cg;
   10264        5082 :   m = (n*H - alchi) / cg; /* positive, exact division */
   10265      598549 :   for (; m <= lim; n+=cg, m+=H)
   10266      593467 :     gel(v, m+1) = fg1g2n(n, k, CHI1vec, CHI2vec, data, z1,z2);
   10267        5082 :   t = (2*e)/g; if (odd(k)) t = -t;
   10268        5082 :   v = gdiv(v, gmul(conj_i(gmul(G1,G2)), mulsi(t, powuu(eg*N2/NC2, k-1))));
   10269        5082 :   if (k == 2 && N1 == 1 && N2 == 1) v = gsub(mkF2bd(wN,lim), gmulsg(e,v));
   10270             : 
   10271        5082 :   Qtoss(ALPHA, &na,&da);
   10272        5082 :   S = conj_i( mfcharmulcxeval(CHI1vec,CHI2vec,d,prec) ); /* CHI(1/d) */
   10273        5082 :   if (wN > 1)
   10274             :   {
   10275        3927 :     GEN z = rootsof1powinit(-mu, wN, prec);
   10276        3927 :     long i, l = lg(v);
   10277      571123 :     for (i = 1; i < l; i++) gel(v,i) = gmul(gel(v,i), rootsof1pow(z,i-1));
   10278             :   }
   10279        5082 :   v = RgV_Rg_mul(v, gmul(S, rootsof1q_cx(-mu*na, da, prec)));
   10280        5082 :   return mkvec2(ALPHA, bdexpand(v, w/wN));
   10281             : }
   10282             : 
   10283             : /*****************************************************************/
   10284             : /*                       END EISENSTEIN CUSPS                    */
   10285             : /*****************************************************************/
   10286             : 
   10287             : static GEN
   10288        1596 : mfchisimpl(GEN CHI)
   10289             : {
   10290             :   GEN G, chi;
   10291        1596 :   if (typ(CHI) == t_INT) return CHI;
   10292        1596 :   G = gel(CHI, 1); chi = gel(CHI, 2);
   10293        1596 :   switch(mfcharorder(CHI))
   10294             :   {
   10295        1148 :     case 1: chi = gen_1; break;
   10296         427 :     case 2: chi = znchartokronecker(G,chi,1); break;
   10297          21 :     default:chi = mkintmod(znconreyexp(G,chi), znstar_get_N(G)); break;
   10298             :   }
   10299        1596 :   return chi;
   10300             : }
   10301             : 
   10302             : GEN
   10303         700 : mfparams(GEN F)
   10304             : {
   10305         700 :   pari_sp av = avma;
   10306             :   GEN z, mf, CHI;
   10307         700 :   if ((mf = checkMF_i(F)))
   10308             :   {
   10309          14 :     long N = MF_get_N(mf);
   10310          14 :     GEN gk = MF_get_gk(mf);
   10311          14 :     CHI = MF_get_CHI(mf);
   10312          14 :     z = mkvec5(utoi(N), gk, CHI, utoi(MF_get_space(mf)), mfcharpol(CHI));
   10313             :   }
   10314             :   else
   10315             :   {
   10316         686 :     if (!checkmf_i(F)) pari_err_TYPE("mfparams", F);
   10317         686 :     z = vec_append(mf_get_NK(F), mfcharpol(mf_get_CHI(F)));
   10318             :   }
   10319         700 :   gel(z,3) = mfchisimpl(gel(z,3));
   10320         700 :   return gerepilecopy(av, z);
   10321             : }
   10322             : 
   10323             : GEN
   10324          14 : mfisCM(GEN F)
   10325             : {
   10326          14 :   pari_sp av = avma;
   10327             :   forprime_t S;
   10328             :   GEN D, v;
   10329             :   long N, k, lD, sb, p, i;
   10330          14 :   if (!checkmf_i(F)) pari_err_TYPE("mfisCM", F);
   10331          14 :   N = mf_get_N(F);
   10332          14 :   k = mf_get_k(F); if (N < 0 || k < 0) pari_err_IMPL("mfisCM for this F");
   10333          14 :   D = mfunram(N, -1);
   10334          14 :   lD = lg(D);
   10335          14 :   sb = maxss(mfsturmNk(N, k), 4*N);
   10336          14 :   v = mfcoefs_i(F, sb, 1);
   10337          14 :   u_forprime_init(&S, 2, sb);
   10338         504 :   while ((p = u_forprime_next(&S)))
   10339             :   {
   10340         490 :     GEN ap = gel(v, p+1);
   10341         490 :     if (!gequal0(ap))
   10342         406 :       for (i = 1; i < lD; i++)
   10343         245 :         if (kross(D[i], p) == -1) { D = vecsplice(D, i); lD--; }
   10344             :   }
   10345          14 :   if (lD == 1) return gc_const(av, gen_0);
   10346          14 :   if (lD == 2) { set_avma(av); return stoi(D[1]); }
   10347           7 :   if (k > 1) pari_err_BUG("mfisCM");
   10348           7 :   return gerepileupto(av, zv_to_ZV(D));
   10349             : }
   10350             : 
   10351             : static long
   10352         287 : mfspace_i(GEN mf, GEN F)
   10353             : {
   10354             :   GEN v, vF, gk;
   10355             :   long n, nE, i, l, s, N;
   10356             : 
   10357         287 :   mf = checkMF(mf); s = MF_get_space(mf);
   10358         287 :   if (!F) return s;
   10359         287 :   if (!checkmf_i(F)) pari_err_TYPE("mfspace",F);
   10360         287 :   v = mftobasis(mf, F, 1);
   10361         287 :   n = lg(v)-1; if (!n) return -1;
   10362         224 :   nE = lg(MF_get_E(mf))-1;
   10363         224 :   switch(s)
   10364             :   {
   10365          56 :     case mf_NEW: case mf_OLD: case mf_EISEN: return s;
   10366         140 :     case mf_FULL:
   10367         140 :       if (mf_get_type(F) == t_MF_THETA) return mf_EISEN;
   10368         133 :       if (!gequal0(vecslice(v,1,nE)))
   10369          63 :         return gequal0(vecslice(v,nE+1,n))? mf_EISEN: mf_FULL;
   10370             :   }
   10371             :   /* mf is mf_CUSP or mf_FULL, F a cusp form */
   10372          98 :   gk = mf_get_gk(F);
   10373          98 :   if (typ(gk) == t_FRAC || equali1(gk)) return mf_CUSP;
   10374          84 :   vF = mftonew_i(mf, vecslice(v, nE+1, n), &N);
   10375          84 :   if (N != MF_get_N(mf)) return mf_OLD;
   10376          56 :   l = lg(vF);
   10377          91 :   for (i = 1; i < l; i++)
   10378          56 :     if (itos(gmael(vF,i,1)) != N) return mf_CUSP;
   10379          35 :   return mf_NEW;
   10380             : }
   10381             : long
   10382         287 : mfspace(GEN mf, GEN F)
   10383         287 : { pari_sp av = avma; return gc_long(av, mfspace_i(mf,F)); }
   10384             : static GEN
   10385          21 : lfunfindchi(GEN ldata, GEN van, long prec)
   10386             : {
   10387          21 :   GEN gN = ldata_get_conductor(ldata), gk = ldata_get_k(ldata);
   10388          21 :   GEN G = znstar0(gN,1), cyc = znstar_get_conreycyc(G), L, go, vz;
   10389          21 :   long N = itou(gN), odd = typ(gk) == t_INT && mpodd(gk);
   10390          21 :   long i, j, o, l, B0 = 2, B = lg(van)-1, bit = 10 - prec2nbits(prec);
   10391             : 
   10392             :   /* if van is integral, chi must be trivial */
   10393          21 :   if (typ(van) == t_VECSMALL) return mfcharGL(G, zerocol(lg(cyc)-1));
   10394          14 :   L = cyc2elts(cyc); l = lg(L);
   10395          42 :   for (i = j = 1; i < l; i++)
   10396             :   {
   10397          28 :     GEN chi = zc_to_ZC(gel(L,i));
   10398          28 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
   10399             :   }
   10400          14 :   setlg(L,j); l = j;
   10401          14 :   if (l <= 2) return gel(L,1);
   10402           0 :   o = znstar_get_expo(G); go = utoi(o);
   10403           0 :   vz = grootsof1(o, prec);
   10404             :   for (;;)
   10405           0 :   {
   10406             :     long n;
   10407           0 :     for (n = B0; n <= B; n++)
   10408             :     {
   10409             :       GEN an, r;
   10410             :       long j;
   10411           0 :       if (ugcd(n, N) != 1) continue;
   10412           0 :       an = gel(van,n); if (gexpo(an) < bit) continue;
   10413           0 :       r = gdiv(an, conj_i(an));
   10414           0 :       for (i = 1; i < l; i++)
   10415             :       {
   10416           0 :         GEN CHI = gel(L,i);
   10417           0 :         if (gexpo(gsub(r, gel(vz, znchareval_i(CHI,n,go)+1))) > bit)
   10418           0 :           gel(L,i) = NULL;
   10419             :       }
   10420           0 :       for (i = j = 1; i < l; i++)
   10421           0 :         if (gel(L,i)) gel(L,j++) = gel(L,i);
   10422           0 :       l = j; setlg(L,l);
   10423           0 :       if (l == 2) return gel(L,1);
   10424             :     }
   10425           0 :     B0 = B+1; B <<= 1;
   10426           0 :     van = ldata_vecan(ldata_get_an(ldata), B, prec);
   10427             :   }
   10428             : }
   10429             : 
   10430             : GEN
   10431          21 : mffromlfun(GEN L, long prec)
   10432             : {
   10433          21 :   pari_sp av = avma;
   10434          21 :   GEN ldata = lfunmisc_to_ldata_shallow(L), Vga = ldata_get_gammavec(ldata);
   10435          21 :   GEN van, a0, CHI, NK, gk = ldata_get_k(ldata);
   10436             :   long N, space;
   10437          21 :   if (!gequal(Vga, mkvec2(gen_0, gen_1))) pari_err_TYPE("mffromlfun", L);
   10438          21 :   N = itou(ldata_get_conductor(ldata));
   10439          21 :   van = ldata_vecan(ldata_get_an(ldata), mfsturmNgk(N,gk) + 2, prec);
   10440          21 :   CHI = lfunfindchi(ldata, van, prec);
   10441          21 :   if (typ(van) != t_VEC) van = vecsmall_to_vec_inplace(van);
   10442          21 :   space = (lg(ldata) == 7)? mf_CUSP: mf_FULL;
   10443          21 :   a0 = (space == mf_CUSP)? gen_0: gneg(lfun(L, gen_0, prec2nbits(prec)));
   10444          21 :   NK = mkvec3(utoi(N), gk, mfchisimpl(CHI));
   10445          21 :   return gerepilecopy(av, mkvec3(NK, utoi(space), shallowconcat(a0, van)));
   10446             : }
   10447             : /*******************************************************************/
   10448             : /*                                                                 */
   10449             : /*                       HALF-INTEGRAL WEIGHT                      */
   10450             : /*                                                                 */
   10451             : /*******************************************************************/
   10452             : /* We use the prefix mf2; k represents the weight -1/2, so e.g.
   10453             :    k = 2 is weight 5/2. N is the level, so 4\mid N, and CHI is the
   10454             :    character, always even. */
   10455             : 
   10456             : static long
   10457        3360 : lamCO(long r, long s, long p)
   10458             : {
   10459        3360 :   if ((s << 1) <= r)
   10460             :   {
   10461        1232 :     long rp = r >> 1;
   10462        1232 :     if (odd(r)) return upowuu(p, rp) << 1;
   10463         336 :     else return (p + 1)*upowuu(p, rp - 1);
   10464             :   }
   10465        2128 :   else return upowuu(p, r - s) << 1;
   10466             : }
   10467             : 
   10468             : static int
   10469        1568 : condC(GEN faN, GEN valF)
   10470             : {
   10471        1568 :   GEN P = gel(faN, 1), E = gel(faN, 2);
   10472        1568 :   long l = lg(P), i;
   10473        3696 :   for (i = 1; i < l; i++)
   10474        3024 :     if ((P[i] & 3L) == 3)
   10475             :     {
   10476        1120 :       long r = E[i];
   10477        1120 :       if (odd(r) || r < (valF[i] << 1)) return 1;
   10478             :     }
   10479         672 :   return 0;
   10480             : }
   10481             : 
   10482             : /* returns 2*zetaCO; weight is k + 1/2 */
   10483             : static long
   10484        3696 : zeta2CO(GEN faN, GEN valF, long r2, long s2, long k)
   10485             : {
   10486        3696 :   if (r2 >= 4) return lamCO(r2, s2, 2) << 1;
   10487        2912 :   if (r2 == 3) return 6;
   10488        1568 :   if (condC(faN, valF)) return 4;
   10489         672 :   if (odd(k)) return s2 ? 3 : 5; else return s2 ? 5: 3;
   10490             : }
   10491             : 
   10492             : /* returns 4 times last term in formula */
   10493             : static long
   10494        3696 : dim22(long N, long F, long k)
   10495             : {
   10496        3696 :   pari_sp av = avma;
   10497        3696 :   GEN vF, faN = myfactoru(N), P = gel(faN, 1), E = gel(faN, 2);
   10498        3696 :   long i, D, l = lg(P);
   10499        3696 :   vF = cgetg(l, t_VECSMALL);
   10500        9968 :   for (i = 1; i < l; i++) vF[i] = u_lval(F, P[i]);
   10501        3696 :   D = zeta2CO(faN, vF, E[1], vF[1], k);
   10502        6272 :   for (i = 2; i < l; i++) D *= lamCO(E[i], vF[i], P[i]);
   10503        3696 :   return gc_long(av,D);
   10504             : }
   10505             : 
   10506             : /* PSI not necessarily primitive, of conductor F */
   10507             : static int
   10508       13846 : charistotallyeven(GEN PSI, long F)
   10509             : {
   10510       13846 :   pari_sp av = avma;
   10511       13846 :   GEN P = gel(myfactoru(F), 1);
   10512       13846 :   GEN G = gel(PSI,1), psi = gel(PSI,2);
   10513             :   long i;
   10514       14350 :   for (i = 1; i < lg(P); i++)
   10515             :   {
   10516         532 :     GEN psip = znchardecompose(G, psi, utoipos(P[i]));
   10517         532 :     if (zncharisodd(G, psip)) return gc_bool(av,0);
   10518             :   }
   10519       13818 :   return gc_bool(av,1);
   10520             : }
   10521             : 
   10522             : static GEN
   10523      299775 : get_PSI(GEN CHI, long t)
   10524             : {
   10525      299775 :   long r = t & 3L, t2 = (r == 2 || r == 3) ? t << 2 : t;
   10526      299775 :   return mfcharmul_i(CHI, induce(gel(CHI,1), utoipos(t2)));
   10527             : }
   10528             : /* space = mf_CUSP, mf_EISEN or mf_FULL, weight k + 1/2 */
   10529             : static long
   10530       41363 : mf2dimwt12(long N, GEN CHI, long space)
   10531             : {
   10532       41363 :   pari_sp av = avma;
   10533       41363 :   GEN D = mydivisorsu(N >> 2);
   10534       41363 :   long i, l = lg(D), dim3 = 0, dim4 = 0;
   10535             : 
   10536       41363 :   CHI = induceN(N, CHI);
   10537      341138 :   for (i = 1; i < l; i++)
   10538             :   {
   10539      299775 :     long rp, t = D[i], Mt = D[l-i];
   10540      299775 :     GEN PSI = get_PSI(CHI,t);
   10541      299775 :     rp = mfcharconductor(PSI);
   10542      299775 :     if (Mt % (rp*rp) == 0) { dim4++; if (charistotallyeven(PSI,rp)) dim3++; }
   10543             :   }
   10544       41363 :   set_avma(av);
   10545       41363 :   switch (space)
   10546             :   {
   10547       40439 :     case mf_CUSP: return dim4 - dim3;
   10548         462 :     case mf_EISEN:return dim3;
   10549         462 :     case mf_FULL: return dim4;
   10550             :   }
   10551             :   return 0; /*LCOV_EXCL_LINE*/
   10552             : }
   10553             : 
   10554             : static long
   10555         693 : mf2dimwt32(long N, GEN CHI, long F, long space)
   10556             : {
   10557             :   long D;
   10558         693 :   switch(space)
   10559             :   {
   10560         231 :     case mf_CUSP: D = mypsiu(N) - 6*dim22(N, F, 1);
   10561         231 :       if (D%24) pari_err_BUG("mfdim");
   10562         231 :       return D/24 + mf2dimwt12(N, CHI, 4);
   10563         231 :     case mf_FULL: D = mypsiu(N) + 6*dim22(N, F, 0);
   10564         231 :       if (D%24) pari_err_BUG("mfdim");
   10565         231 :       return D/24 + mf2dimwt12(N, CHI, 1);
   10566         231 :     case mf_EISEN: D = dim22(N, F, 0) + dim22(N, F, 1);
   10567         231 :       if (D & 3L) pari_err_BUG("mfdim");
   10568         231 :       return (D >> 2) - mf2dimwt12(N, CHI, 3);
   10569             :   }
   10570             :   return 0; /*LCOV_EXCL_LINE*/
   10571             : }
   10572             : 
   10573             : /* F = conductor(CHI), weight k = r+1/2 */
   10574             : static long
   10575       43701 : checkmf2(long N, long r, GEN CHI, long F, long space)
   10576             : {
   10577       43701 :   switch(space)
   10578             :   {
   10579       43680 :     case mf_FULL: case mf_CUSP: case mf_EISEN: break;
   10580          14 :     case mf_NEW: case mf_OLD:
   10581          14 :       pari_err_TYPE("half-integral weight [new/old spaces]", utoi(space));
   10582           7 :     default:
   10583           7 :       pari_err_TYPE("half-integral weight [incorrect space]",utoi(space));
   10584             :   }
   10585       43680 :   if (N & 3L)
   10586           0 :     pari_err_DOMAIN("half-integral weight", "N % 4", "!="