Code coverage tests

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

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

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

LCOV - code coverage report
Current view: top level - basemath - mftrace.c (source / functions) Hit Total Coverage
Test: PARI/GP v2.12.0 lcov report (development 23011-59c7027a2) Lines: 7105 7271 97.7 %
Date: 2018-09-22 05:37:52 Functions: 734 736 99.7 %
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, 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 mygmodulo_lift(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 k, GEN CHI, long ord);
      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       27426 : mkgNK(GEN N, GEN k, GEN CHI, GEN P) { return mkvec4(N, k, CHI, P); }
      77             : static GEN
      78       12278 : mkNK(long N, long k, GEN CHI) { return mkgNK(stoi(N), stoi(k), CHI, pol_x(1)); }
      79             : GEN
      80        6615 : MF_get_CHI(GEN mf) { return gmael(mf,1,3); }
      81             : GEN
      82       16555 : MF_get_gN(GEN mf) { return gmael(mf,1,1); }
      83             : long
      84       15862 : MF_get_N(GEN mf) { return itou(MF_get_gN(mf)); }
      85             : GEN
      86       11347 : MF_get_gk(GEN mf) { return gmael(mf,1,2); }
      87             : long
      88        5572 : MF_get_k(GEN mf)
      89             : {
      90        5572 :   GEN gk = MF_get_gk(mf);
      91        5572 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
      92        5572 :   return itou(gk);
      93             : }
      94             : long
      95         147 : MF_get_r(GEN mf)
      96             : {
      97         147 :   GEN gk = MF_get_gk(mf);
      98         147 :   if (typ(gk) == t_INT) pari_err_IMPL("integral weight");
      99         147 :   return itou(gel(gk, 1)) >> 1;
     100             : }
     101             : long
     102       11543 : MF_get_space(GEN mf) { return itos(gmael(mf,1,4)); }
     103             : GEN
     104        3465 : MF_get_E(GEN mf) { return gel(mf,2); }
     105             : GEN
     106       17318 : MF_get_S(GEN mf) { return gel(mf,3); }
     107             : GEN
     108        1099 : MF_get_basis(GEN mf) { return shallowconcat(gel(mf,2), gel(mf,3)); }
     109             : long
     110        4291 : MF_get_dim(GEN mf)
     111             : {
     112        4291 :   switch(MF_get_space(mf))
     113             :   {
     114             :     case mf_FULL:
     115         560 :       return lg(MF_get_S(mf)) - 1 + lg(MF_get_E(mf))-1;
     116             :     case mf_EISEN:
     117         140 :       return lg(MF_get_E(mf))-1;
     118             :     default: /* mf_NEW, mf_CUSP, mf_OLD */
     119        3591 :       return lg(MF_get_S(mf)) - 1;
     120             :   }
     121             : }
     122             : GEN
     123        6461 : MFnew_get_vj(GEN mf) { return gel(mf,4); }
     124             : GEN
     125         476 : MFcusp_get_vMjd(GEN mf) { return gel(mf,4); }
     126             : GEN
     127        5747 : MF_get_M(GEN mf) { return gmael(mf,5,3); }
     128             : GEN
     129        2261 : MF_get_Minv(GEN mf) { return gmael(mf,5,2); }
     130             : GEN
     131        7133 : MF_get_Mindex(GEN mf) { return gmael(mf,5,1); }
     132             : 
     133             : /* ordinary gtocol forgets about initial 0s */
     134             : GEN
     135        3136 : 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         588 : QabM_init(long n, ulong *p)
     142             : {
     143         588 :   ulong pinit = 1000000007;
     144             :   forprime_t T;
     145         588 :   if (n <= 1) { *p = pinit; return 0; }
     146         434 :   u_forprime_arith_init(&T, pinit, ULONG_MAX, 1, n);
     147         434 :   *p = u_forprime_next(&T);
     148         434 :   return Flx_oneroot(ZX_to_Flx(polcyclo(n, 0), *p), *p);
     149             : }
     150             : static ulong
     151      480361 : Qab_to_Fl(GEN P, ulong r, ulong p)
     152             : {
     153             :   ulong t;
     154             :   GEN den;
     155      480361 :   P = Q_remove_denom(liftpol_shallow(P), &den);
     156      480361 :   if (typ(P) == t_POL) { GEN Pp = ZX_to_Flx(P, p); t = Flx_eval(Pp, r, p); }
     157      465843 :   else t = umodiu(P, p);
     158      480361 :   if (den) t = Fl_div(t, umodiu(den, p), p);
     159      480361 :   return t;
     160             : }
     161             : static GEN
     162        9807 : QabC_to_Flc(GEN C, ulong r, ulong p)
     163             : {
     164        9807 :   long i, l = lg(C);
     165        9807 :   GEN A = cgetg(l, t_VECSMALL);
     166        9807 :   for (i = 1; i < l; i++) uel(A,i) = Qab_to_Fl(gel(C,i), r, p);
     167        9807 :   return A;
     168             : }
     169             : static GEN
     170         231 : QabM_to_Flm(GEN M, ulong r, ulong p)
     171             : {
     172             :   long i, l;
     173         231 :   GEN A = cgetg_copy(M, &l);
     174       10038 :   for (i = 1; i < l; i++)
     175        9807 :     gel(A, i) = QabC_to_Flc(gel(M, i), r, p);
     176         231 :   return A;
     177             : }
     178             : /* A a t_POL */
     179             : static GEN
     180         336 : QabX_to_Flx(GEN A, ulong r, ulong p)
     181             : {
     182         336 :   long i, l = lg(A);
     183         336 :   GEN a = cgetg(l, t_VECSMALL);
     184         336 :   a[1] = ((ulong)A[1])&VARNBITS;
     185         336 :   for (i = 2; i < l; i++) uel(a,i) = Qab_to_Fl(gel(A,i), r, p);
     186         336 :   return Flx_renormalize(a, l);
     187             : }
     188             : 
     189             : /* FIXME: remove */
     190             : static GEN
     191         721 : ZabM_pseudoinv_i(GEN M, GEN P, long n, GEN *pv, GEN *den, int ratlift)
     192             : {
     193         721 :   GEN v = ZabM_indexrank(M, P, n);
     194         721 :   if (pv) *pv = v;
     195         721 :   M = shallowmatextract(M,gel(v,1),gel(v,2));
     196         721 :   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        1337 : QabM_ker(GEN M, GEN P, long n)
     203             : {
     204             :   GEN B;
     205        1337 :   if (n <= 2)
     206         882 :     B = ZM_ker(Q_primpart(M));
     207             :   else
     208         455 :     B = ZabM_ker(Q_primpart(liftpol_shallow(M)), P, n);
     209        1337 :   return B;
     210             : }
     211             : /* pseudo-inverse of M */
     212             : static GEN
     213        1078 : QabM_pseudoinv(GEN M, GEN P, long n, GEN *pv, GEN *pden)
     214             : {
     215             :   GEN cM, Mi;
     216        1078 :   if (n <= 2)
     217             :   {
     218         875 :     M = Q_primitive_part(M, &cM);
     219         875 :     Mi = ZM_pseudoinv(M, pv, pden); /* M^(-1) = Mi / (cM * den) */
     220             :   }
     221             :   else
     222             :   {
     223         203 :     M = Q_primitive_part(liftpol_shallow(M), &cM);
     224         203 :     Mi = ZabM_pseudoinv(M, P, n, pv, pden);
     225         203 :     Mi = gmodulo(Mi, P);
     226             :   }
     227        1078 :   *pden = mul_content(*pden, cM);
     228        1078 :   return Mi;
     229             : }
     230             : 
     231             : static GEN
     232        9338 : QabM_indexrank(GEN M, GEN P, long n)
     233             : {
     234             :   GEN z;
     235        9338 :   if (n <= 2)
     236             :   {
     237        8232 :     M = vec_Q_primpart(M);
     238        8232 :     z = ZM_indexrank(M); /* M^(-1) = Mi / (cM * den) */
     239             :   }
     240             :   else
     241             :   {
     242        1106 :     M = vec_Q_primpart(liftpol_shallow(M));
     243        1106 :     z = ZabM_indexrank(M, P, n);
     244             :   }
     245        9338 :   return z;
     246             : }
     247             : 
     248             : /*********************************************************************/
     249             : /*                    Simple arithmetic functions                    */
     250             : /*********************************************************************/
     251             : /* TODO: most of these should be exported and used in ifactor1.c */
     252             : /* phi(n) */
     253             : static ulong
     254      106106 : myeulerphiu(ulong n)
     255             : {
     256             :   pari_sp av;
     257      106106 :   if (n == 1) return 1;
     258       90874 :   av = avma; return gc_ulong(av, eulerphiu_fact(myfactoru(n)));
     259             : }
     260             : static long
     261       77476 : mymoebiusu(ulong n)
     262             : {
     263             :   pari_sp av;
     264       77476 :   if (n == 1) return 1;
     265       70182 :   av = avma; return gc_long(av, moebiusu_fact(myfactoru(n)));
     266             : }
     267             : 
     268             : static long
     269        2709 : mynumdivu(long N)
     270             : {
     271             :   pari_sp av;
     272        2709 :   if (N == 1) return 1;
     273        2604 :   av = avma; return gc_long(av, numdivu_fact(myfactoru(N)));
     274             : }
     275             : 
     276             : /* N\prod_{p|N} (1+1/p) */
     277             : static long
     278      266987 : mypsiu(ulong N)
     279             : {
     280      266987 :   pari_sp av = avma;
     281      266987 :   GEN P = gel(myfactoru(N), 1);
     282      266987 :   long j, l = lg(P), res = N;
     283      266987 :   for (j = 1; j < l; j++) res += res/P[j];
     284      266987 :   return gc_long(av,res);
     285             : }
     286             : /* write n = mf^2. Return m, set f. */
     287             : static ulong
     288         210 : mycore(ulong n, long *pf)
     289             : {
     290         210 :   pari_sp av = avma;
     291         210 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     292         210 :   long i, l = lg(P), m = 1, f = 1;
     293         850 :   for (i = 1; i < l; i++)
     294             :   {
     295         640 :     long j, p = P[i], e = E[i];
     296         640 :     if (e & 1) m *= p;
     297         640 :     for (j = 2; j <= e; j+=2) f *= p;
     298             :   }
     299         210 :   *pf = f; return gc_long(av,m);
     300             : }
     301             : 
     302             : /* fa = factorization of -D > 0, return -D0 > 0 (where D0 is fundamental) */
     303             : static long
     304     7553259 : corediscs_fact(GEN fa)
     305             : {
     306     7553259 :   GEN P = gel(fa,1), E = gel(fa,2);
     307     7553259 :   long i, l = lg(P), m = 1;
     308    25047428 :   for (i = 1; i < l; i++)
     309             :   {
     310    17494169 :     long p = P[i], e = E[i];
     311    17494169 :     if (e & 1) m *= p;
     312             :   }
     313     7553259 :   if ((m&3L) != 3) m <<= 2;
     314     7553259 :   return m;
     315             : }
     316             : static long
     317        6118 : mubeta(long n)
     318             : {
     319        6118 :   pari_sp av = avma;
     320        6118 :   GEN E = gel(myfactoru(n), 2);
     321        6118 :   long i, s = 1, l = lg(E);
     322       12698 :   for (i = 1; i < l; i++)
     323             :   {
     324        6580 :     long e = E[i];
     325        6580 :     if (e >= 3) return gc_long(av,0);
     326        6580 :     if (e == 1) s *= -2;
     327             :   }
     328        6118 :   return gc_long(av,s);
     329             : }
     330             : 
     331             : /* n = n1*n2, n1 = ppo(n, m); return mubeta(n1)*moebiusu(n2).
     332             :  * N.B. If n from newt_params we, in fact, never return 0 */
     333             : static long
     334     4136930 : mubeta2(long n, long m)
     335             : {
     336     4136930 :   pari_sp av = avma;
     337     4136930 :   GEN fa = myfactoru(n), P = gel(fa,1), E = gel(fa,2);
     338     4136930 :   long i, s = 1, l = lg(P);
     339     8419236 :   for (i = 1; i < l; i++)
     340             :   {
     341     4282306 :     long p = P[i], e = E[i];
     342     4282306 :     if (m % p)
     343             :     { /* p^e in n1 */
     344     3409707 :       if (e >= 3) return gc_long(av,0);
     345     3409707 :       if (e == 1) s *= -2;
     346             :     }
     347             :     else
     348             :     { /* in n2 */
     349      872599 :       if (e >= 2) return gc_long(av,0);
     350      872599 :       s = -s;
     351             :     }
     352             :   }
     353     4136930 :   return gc_long(av,s);
     354             : }
     355             : 
     356             : /* write N = prod p^{ep} and n = df^2, d squarefree.
     357             :  * set g  = ppo(gcd(sqfpart(N), f), FC)
     358             :  *     N2 = prod p^if(e==1 || p|n, ep-1, ep-2) */
     359             : static void
     360      950887 : newt_params(long N, long n, long FC, long *pg, long *pN2)
     361             : {
     362      950887 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     363      950887 :   long i, g = 1, N2 = 1, l = lg(P);
     364     2531389 :   for (i = 1; i < l; i++)
     365             :   {
     366     1580502 :     long p = P[i], e = E[i];
     367     1580502 :     if (e == 1)
     368     1339639 :     { if (FC % p && n % (p*p) == 0) g *= p; }
     369             :     else
     370      240863 :       N2 *= upowuu(p,(n % p)? e-2: e-1);
     371             :   }
     372      950887 :   *pg = g; *pN2 = N2;
     373      950887 : }
     374             : /* simplified version of newt_params for n = 1 (newdim) */
     375             : static void
     376       32767 : newd_params(long N, long *pN2)
     377             : {
     378       32767 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     379       32767 :   long i, N2 = 1, l = lg(P);
     380       83125 :   for (i = 1; i < l; i++)
     381             :   {
     382       50358 :     long p = P[i], e = E[i];
     383       50358 :     if (e > 2) N2 *= upowuu(p, e-2);
     384             :   }
     385       32767 :   *pN2 = N2;
     386       32767 : }
     387             : 
     388             : static long
     389          21 : newd_params2(long N)
     390             : {
     391          21 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
     392          21 :   long i, N2 = 1, l = lg(P);
     393          56 :   for (i = 1; i < l; i++)
     394             :   {
     395          35 :     long p = P[i], e = E[i];
     396          35 :     if (e >= 2) N2 *= upowuu(p, e);
     397             :   }
     398          21 :   return N2;
     399             : }
     400             : 
     401             : /*******************************************************************/
     402             : /*   Relative trace between cyclotomic fields (TODO: export this)  */
     403             : /*******************************************************************/
     404             : /* g>=1; return g * prod_{p | g, (p,q) = 1} (1-1/p) */
     405             : static long
     406       42651 : phipart(long g, long q)
     407             : {
     408       42651 :   if (g > 1)
     409             :   {
     410       16625 :     GEN P = gel(myfactoru(g), 1);
     411       16625 :     long i, l = lg(P);
     412       16625 :     for (i = 1; i < l; i++) { long p = P[i]; if (q % p) g -= g / p; }
     413             :   }
     414       42651 :   return g;
     415             : }
     416             : /* Trace(zeta_n^k) from Q(\zeta_n) to Q(\zeta_m) with n = m*d; k > 0 */
     417             : static GEN
     418       77385 : tracerelz(long d, long m, long k, long vt)
     419             : {
     420       77385 :   long s, v, g = ugcd(k, d), q = d / g, muq = mymoebiusu(q);
     421       77385 :   if (!muq) return gen_0;
     422       50351 :   if (m == 1)
     423             :   {
     424       17843 :     s = phipart(g, q); if (muq < 0) s = -s;
     425       17843 :     return stoi(s);
     426             :   }
     427       32508 :   if (ugcd(q, m) > 1) return gen_0;
     428       24808 :   s = phipart(g, m*q); if (muq < 0) s = -s;
     429       24808 :   v = Fl_inv(q % m, m);
     430       24808 :   v = (v*(k/g)) % m;
     431       24808 :   return mygmodulo_lift(v, m, stoi(s), vt);
     432             : }
     433             : /* m | n, both not 2 mod 4. Pn = polcyclo(n) */
     434             : GEN
     435       17983 : Qab_trace_init(GEN Pn, long n, long m)
     436             : {
     437             :   GEN T, Pm;
     438             :   long a, i, d, vt;
     439       17983 :   if (m == n) return mkvec(Pn);
     440       12593 :   d = degpol(Pn);
     441       12593 :   vt = varn(Pn);
     442       12593 :   Pm = polcyclo(m, vt);
     443       12593 :   T = cgetg(d+1, t_VEC);
     444       12593 :   gel(T,1) = utoipos(d / degpol(Pm)); /* Tr 1 */
     445       12593 :   a = n / m;
     446       12593 :   for (i = 1; i < d; i++) gel(T,i+1) = tracerelz(a, m, i, vt);
     447       12593 :   return mkvec3(Pm, Pn, T);
     448             : }
     449             : /* x a t_POL modulo Phi_n; n, m not 2 mod 4, degrel != 1*/
     450             : static GEN
     451       48286 : tracerel_i(GEN T, GEN x)
     452             : {
     453       48286 :   long k, l = lg(x);
     454       48286 :   GEN S = gen_0;
     455       48286 :   for (k = 2; k < l; k++) S = gadd(S, gmul(gel(T,k-1), gel(x,k)));
     456       48286 :   return S;
     457             : }
     458             : /* v = Qab_trace_init(n,m); x is a t_VEC of polmodulo Phi_n
     459             :  * Tr_{Q(zeta_n)/Q(zeta_m)} (zeta_n^t * x) */
     460             : GEN
     461        4081 : QabV_tracerel(GEN v, long t, GEN x)
     462             : {
     463             :   long l, j, degrel;
     464             :   GEN y, z, Pm, Pn, T;
     465        4081 :   if (lg(v) != 4) return x;
     466        4081 :   y = cgetg_copy(x, &l);
     467        4081 :   Pm = gel(v,1);
     468        4081 :   Pn = gel(v,2);
     469        4081 :   T  = gel(v,3);
     470        4081 :   degrel = degpol(Pn) / degpol(Pm);
     471        4081 :   z = RgX_rem(pol_xn(t, varn(Pn)), Pn);
     472      100520 :   for (j = 1; j < l; j++)
     473             :   {
     474       96439 :     GEN a = liftpol_shallow(gel(x,j));
     475       96439 :     a = simplify_shallow( gmul(a, z) );
     476       96439 :     if (typ(a) == t_POL)
     477             :     {
     478       48286 :       a = gdivgs(tracerel_i(T, RgX_rem(a, Pn)), degrel);
     479       48286 :       if (typ(a) == t_POL) a = RgX_rem(a, Pm);
     480             :     }
     481       96439 :     gel(y,j) = a;
     482             :   }
     483        4081 :   return y;
     484             : }
     485             : 
     486             : /*              Operations on Dirichlet characters                       */
     487             : 
     488             : /* A Dirichlet character can be given in GP in different formats, but in this
     489             :  * package, it will be a vector CHI=[G,chi,ord], where G is the (Z/MZ)^* to
     490             :  * which the character belongs, chi is the character in Conrey format, ord is
     491             :  * the order */
     492             : 
     493             : static GEN
     494      913752 : gmfcharorder(GEN CHI) { return gel(CHI, 3); }
     495             : long
     496      883365 : mfcharorder(GEN CHI) { return itou(gmfcharorder(CHI)); }
     497             : static long
     498        8274 : mfcharistrivial(GEN CHI) { return !CHI || mfcharorder(CHI) == 1; }
     499             : static GEN
     500      592382 : gmfcharmodulus(GEN CHI) { return gmael3(CHI, 1, 1, 1); }
     501             : long
     502      592382 : mfcharmodulus(GEN CHI) { return itou(gmfcharmodulus(CHI)); }
     503             : GEN
     504      183554 : mfcharpol(GEN CHI) { return gel(CHI,4); }
     505             : static long
     506      448371 : ord_canon(long ord)
     507             : {
     508      448371 :   if ((ord & 3L) == 2) ord >>= 1;
     509      448371 :   return ord;
     510             : }
     511             : static long
     512       27643 : mfcharorder_canon(GEN CHI) { return ord_canon(mfcharorder(CHI)); }
     513             : 
     514             : /* t^k mod polcyclo(ord), ord = order(CHI) > 1 */
     515             : static GEN
     516        1113 : mygmodulo(GEN CHI, long k)
     517             : {
     518             :   GEN C, Pn;
     519             :   long ord;
     520        1113 :   if (!k) return gen_1;
     521         770 :   ord = mfcharorder(CHI);
     522         770 :   if ((k << 1) == ord) return gen_m1;
     523         434 :   Pn = mfcharpol(CHI);
     524         434 :   if ((ord&3L) != 2)
     525          84 :     C = gen_1;
     526             :   else
     527             :   {
     528         350 :     ord >>= 1;
     529         350 :     if (odd(k)) { C = gen_m1; k += ord; } else C = gen_1;
     530         350 :     k >>= 1;
     531             :   }
     532         434 :   return gmodulo(monomial(C, k, varn(Pn)), Pn);
     533             : }
     534             : /* C*zeta_ord^k */
     535             : static GEN
     536      613529 : mygmodulo_lift(long k, long ord, GEN C, long vt)
     537             : {
     538      613529 :   if (!k) return C;
     539      324940 :   if ((k << 1) == ord) return gneg(C);
     540      219751 :   if ((ord&3L) == 2)
     541             :   {
     542       87185 :     if (odd(k)) { C = gneg(C); k += ord >> 1; }
     543       87185 :     k >>= 1;
     544             :   }
     545      219751 :   return monomial(C, k, vt);
     546             : }
     547             : /* vz[i+1] = image of (zeta_ord)^i in Fp */
     548             : static ulong
     549      171437 : mygmodulo_Fl(long k, GEN vz, ulong C, ulong p)
     550             : {
     551             :   long ord;
     552      171437 :   if (!k) return C;
     553      111069 :   ord = lg(vz)-2;
     554      111069 :   if ((k << 1) == ord) return Fl_neg(C,p);
     555       89446 :   if ((ord&3L) == 2)
     556             :   {
     557       70140 :     if (odd(k)) { C = Fl_neg(C,p); k += ord >> 1; }
     558       70140 :     k >>= 1;
     559             :   }
     560       89446 :   return Fl_mul(C, vz[k+1], p);
     561             : }
     562             : 
     563             : static long
     564      450471 : znchareval_i(GEN CHI, long n, GEN ord)
     565      450471 : { return itos(znchareval(gel(CHI,1), gel(CHI,2), stoi(n), ord)); }
     566             : 
     567             : /* G a znstar, L a Conrey log: return a 'mfchar' */
     568             : static GEN
     569      368900 : mfcharGL(GEN G, GEN L)
     570             : {
     571      368900 :   GEN o = zncharorder(G,L);
     572      368900 :   long ord = ord_canon(itou(o)), vt = fetch_user_var("t");
     573      368900 :   return mkvec4(G, L, o, polcyclo(ord,vt));
     574             : }
     575             : static GEN
     576        3927 : mfchartrivial()
     577        3927 : { return mfcharGL(znstar0(gen_1,1), cgetg(1,t_COL)); }
     578             : /* convert a generic character into an 'mfchar' */
     579             : static GEN
     580        3850 : get_mfchar(GEN CHI)
     581             : {
     582             :   GEN G, L;
     583        3850 :   if (typ(CHI) != t_VEC) CHI = znchar(CHI);
     584             :   else
     585             :   {
     586         833 :     long l = lg(CHI);
     587         833 :     if ((l != 3 && l != 5) || !checkznstar_i(gel(CHI,1)))
     588           7 :       pari_err_TYPE("checkNF [chi]", CHI);
     589         826 :     if (l == 5) return CHI;
     590             :   }
     591        3815 :   G = gel(CHI,1);
     592        3815 :   L = gel(CHI,2); if (typ(L) != t_COL) L = znconreylog(G,L);
     593        3815 :   return mfcharGL(G, L);
     594             : }
     595             : 
     596             : /* parse [N], [N,k], [N,k,CHI]. If 'joker' is set, allow wildcard for CHI */
     597             : static GEN
     598        8932 : checkCHI(GEN NK, long N, int joker)
     599             : {
     600             :   GEN CHI;
     601        8932 :   if (lg(NK) == 3)
     602         602 :     CHI = mfchartrivial();
     603             :   else
     604             :   {
     605             :     long i, l;
     606        8330 :     CHI = gel(NK,3); l = lg(CHI);
     607        8330 :     if (isintzero(CHI) && joker)
     608        4095 :       CHI = NULL; /* all character orbits */
     609        4235 :     else if (isintm1(CHI) && joker > 1)
     610        2373 :       CHI = gen_m1; /* sum over all character orbits */
     611        1995 :     else if ((typ(CHI) == t_VEC &&
     612         189 :              (l == 1 || l != 3 || !checkznstar_i(gel(CHI,1)))) && joker)
     613             :     {
     614         133 :       CHI = shallowtrans(CHI); /* list of characters */
     615         133 :       for (i = 1; i < l; i++) gel(CHI,i) = get_mfchar(gel(CHI,i));
     616             :     }
     617             :     else
     618             :     {
     619        1729 :       CHI = get_mfchar(CHI); /* single char */
     620        1729 :       if (N % mfcharmodulus(CHI)) pari_err_TYPE("checkNF [chi]", NK);
     621             :     }
     622             :   }
     623        8918 :   return CHI;
     624             : }
     625             : /* support half-integral weight */
     626             : static void
     627        8939 : checkNK2(GEN NK, long *N, long *nk, long *dk, GEN *CHI, int joker)
     628             : {
     629        8939 :   long l = lg(NK);
     630             :   GEN T;
     631        8939 :   if (typ(NK) != t_VEC || l < 3 || l > 4) pari_err_TYPE("checkNK", NK);
     632        8939 :   T = gel(NK,1); if (typ(T) != t_INT) pari_err_TYPE("checkNF [N]", NK);
     633        8939 :   *N = itos(T); if (*N <= 0) pari_err_TYPE("checkNF [N <= 0]", NK);
     634        8939 :   T = gel(NK,2);
     635        8939 :   switch(typ(T))
     636             :   {
     637        5579 :     case t_INT:  *nk = itos(T); *dk = 1; break;
     638             :     case t_FRAC:
     639        3353 :       *nk = itos(gel(T,1));
     640        3353 :       *dk = itou(gel(T,2)); if (*dk == 2) break;
     641           7 :     default: pari_err_TYPE("checkNF [k]", NK);
     642             :   }
     643        8932 :   *CHI = checkCHI(NK, *N, joker);
     644        8918 : }
     645             : /* don't support half-integral weight */
     646             : static void
     647         126 : checkNK(GEN NK, long *N, long *k, GEN *CHI, int joker)
     648             : {
     649             :   long d;
     650         126 :   checkNK2(NK, N, k, &d, CHI, joker);
     651         126 :   if (d != 1) pari_err_TYPE("checkNF [k]", NK);
     652         126 : }
     653             : 
     654             : static GEN
     655        4851 : mfchargalois(long N, int odd, GEN flagorder)
     656             : {
     657        4851 :   GEN G = znstar0(utoi(N), 1), L = chargalois(G, flagorder);
     658        4851 :   long l = lg(L), i, j;
     659      112735 :   for (i = j = 1; i < l; i++)
     660             :   {
     661      107884 :     GEN chi = znconreyfromchar(G, gel(L,i));
     662      107884 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
     663             :   }
     664        4851 :   setlg(L, j); return L;
     665             : }
     666             : /* possible characters for non-trivial S_1(N, chi) */
     667             : static GEN
     668        1708 : mfwt1chars(long N, GEN vCHI)
     669             : {
     670        1708 :   if (vCHI) return vCHI; /*do not filter, user knows best*/
     671             :   /* Tate's theorem */
     672        1638 :   return mfchargalois(N, 1, uisprime(N)? mkvecsmall2(2,4): NULL);
     673             : }
     674             : static GEN
     675        3255 : mfchars(long N, long k, long dk, GEN vCHI)
     676        3255 : { return vCHI? vCHI: mfchargalois(N, (dk == 2)? 0: (k & 1), NULL); }
     677             : 
     678             : /* wrappers from mfchar to znchar */
     679             : static long
     680       65366 : mfcharparity(GEN CHI)
     681             : {
     682       65366 :   if (!CHI) return 1;
     683       65366 :   return zncharisodd(gel(CHI,1), gel(CHI,2)) ? -1 : 1;
     684             : }
     685             : /* if CHI is primitive, return CHI itself, not a copy */
     686             : static GEN
     687       63504 : mfchartoprimitive(GEN CHI, long *pF)
     688             : {
     689             :   pari_sp av;
     690             :   GEN chi, F;
     691       63504 :   if (!CHI) { if (pF) *pF = 1; return mfchartrivial(); }
     692       63504 :   av = avma; F = znconreyconductor(gel(CHI,1), gel(CHI,2), &chi);
     693       63504 :   if (typ(F) == t_INT) set_avma(av);
     694             :   else
     695             :   {
     696        7273 :     CHI = leafcopy(CHI);
     697        7273 :     gel(CHI,1) = znstar0(F, 1);
     698        7273 :     gel(CHI,2) = chi;
     699             :   }
     700       63504 :   if (pF) *pF = mfcharmodulus(CHI);
     701       63504 :   return CHI;
     702             : }
     703             : static long
     704      391314 : mfcharconductor(GEN CHI)
     705             : {
     706      391314 :   pari_sp av = avma;
     707      391314 :   GEN res = znconreyconductor(gel(CHI,1), gel(CHI,2), NULL);
     708      391314 :   if (typ(res) == t_VEC) res = gel(res, 1);
     709      391314 :   return gc_long(av, itos(res));
     710             : }
     711             : 
     712             : /* n coprime with the modulus of CHI */
     713             : static GEN
     714        5537 : mfchareval_i(GEN CHI, long n)
     715             : {
     716        5537 :   GEN ord = gmfcharorder(CHI);
     717        5537 :   if (equali1(ord)) return gen_1;
     718        1113 :   return mygmodulo(CHI, znchareval_i(CHI, n, ord));
     719             : }
     720             : /* d a multiple of ord(CHI); n coprime with char modulus;
     721             :  * return x s.t. CHI(n) = \zeta_d^x] */
     722             : static long
     723      781998 : mfcharevalord(GEN CHI, long n, long d)
     724             : {
     725      781998 :   if (mfcharorder(CHI) == 1) return 0;
     726      441840 :   return znchareval_i(CHI, n, utoi(d));
     727             : }
     728             : 
     729             : /*                      Operations on mf closures                    */
     730             : static GEN
     731       47971 : tagparams(long t, GEN NK) { return mkvec2(mkvecsmall(t), NK); }
     732             : static GEN
     733         952 : lfuntag(long t, GEN x) { return mkvec2(mkvecsmall(t), x); }
     734             : static GEN
     735          49 : tag0(long t, GEN NK) { retmkvec(tagparams(t,NK)); }
     736             : static GEN
     737        8638 : tag(long t, GEN NK, GEN x) { retmkvec2(tagparams(t,NK), x); }
     738             : static GEN
     739       27559 : tag2(long t, GEN NK, GEN x, GEN y) { retmkvec3(tagparams(t,NK), x,y); }
     740             : static GEN
     741       11613 : tag3(long t, GEN NK, GEN x,GEN y,GEN z) { retmkvec4(tagparams(t,NK), x,y,z); }
     742             : /* is F a "modular form" ? */
     743             : int
     744       14630 : checkmf_i(GEN F)
     745       14630 : { return typ(F) == t_VEC
     746       14140 :     && lg(F) > 1 && typ(gel(F,1)) == t_VEC
     747       10108 :     && lg(gel(F,1)) == 3
     748        9947 :     && typ(gmael(F,1,1)) == t_VECSMALL
     749       24577 :     && typ(gmael(F,1,2)) == t_VEC; }
     750      128905 : long mf_get_type(GEN F) { return gmael(F,1,1)[1]; }
     751       94612 : GEN mf_get_gN(GEN F) { return gmael3(F,1,2,1); }
     752       81991 : GEN mf_get_gk(GEN F) { return gmael3(F,1,2,2); }
     753             : /* k - 1/2, assume k in 1/2 + Z */
     754         266 : long mf_get_r(GEN F) { return itou(gel(mf_get_gk(F),1)) >> 1; }
     755       66948 : long mf_get_N(GEN F) { return itou(mf_get_gN(F)); }
     756       46109 : long mf_get_k(GEN F)
     757             : {
     758       46109 :   GEN gk = mf_get_gk(F);
     759       46109 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
     760       46109 :   return itou(gk);
     761             : }
     762       29274 : GEN mf_get_CHI(GEN F) { return gmael3(F,1,2,3); }
     763       17346 : GEN mf_get_field(GEN F) { return gmael3(F,1,2,4); }
     764       15617 : GEN mf_get_NK(GEN F) { return gmael(F,1,2); }
     765             : static void
     766         308 : mf_setfield(GEN f, GEN P)
     767             : {
     768         308 :   gel(f,1) = leafcopy(gel(f,1));
     769         308 :   gmael(f,1,2) = leafcopy(gmael(f,1,2));
     770         308 :   gmael3(f,1,2,4) = P;
     771         308 : }
     772             : 
     773             : /* UTILITY FUNCTIONS */
     774             : GEN
     775        3969 : mftocol(GEN F, long lim, long d)
     776        3969 : { GEN c = mfcoefs_i(F, lim, d); settyp(c,t_COL); return c; }
     777             : GEN
     778        1064 : mfvectomat(GEN vF, long lim, long d)
     779             : {
     780        1064 :   long j, l = lg(vF);
     781        1064 :   GEN M = cgetg(l, t_MAT);
     782        1064 :   for (j = 1; j < l; j++) gel(M,j) = mftocol(gel(vF,j), lim, d);
     783        1064 :   return M;
     784             : }
     785             : 
     786             : static GEN
     787        3892 : RgV_to_ser_full(GEN x) { return RgV_to_ser(x, 0, lg(x)+1); }
     788             : /* TODO: delete */
     789             : static GEN
     790         819 : mfcoefsser(GEN F, long n) { return RgV_to_ser_full(mfcoefs_i(F,n,1)); }
     791             : static GEN
     792         224 : sertovecslice(GEN S, long n)
     793             : {
     794         224 :   GEN v = gtovec0(S, -(lg(S) - 2 + valp(S)));
     795         224 :   long l = lg(v), n2 = n + 2;
     796         224 :   if (l < n2) pari_err_BUG("sertovecslice [n too large]");
     797         224 :   return (l == n2)? v: vecslice(v, 1, n2-1);
     798             : }
     799             : 
     800             : /* a, b two RgV of the same length, multiply as truncated power series */
     801             : static GEN
     802        2954 : RgV_mul_RgXn(GEN a, GEN b)
     803             : {
     804        2954 :   long n = lg(a)-1;
     805             :   GEN c;
     806        2954 :   a = RgV_to_RgX(a,0);
     807        2954 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, b, n);
     808        2954 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     809             : }
     810             : /* divide as truncated power series */
     811             : static GEN
     812         259 : RgV_div_RgXn(GEN a, GEN b)
     813             : {
     814         259 :   long n = lg(a)-1;
     815             :   GEN c;
     816         259 :   a = RgV_to_RgX(a,0);
     817         259 :   b = RgV_to_RgX(b,0); c = RgXn_mul(a, RgXn_inv(b,n), n);
     818         259 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     819             : }
     820             : /* a^b */
     821             : static GEN
     822          77 : RgV_pows_RgXn(GEN a, long b)
     823             : {
     824          77 :   long n = lg(a)-1;
     825             :   GEN c;
     826          77 :   a = RgV_to_RgX(a,0);
     827          77 :   if (b < 0) { a = RgXn_inv(a, n); b = -b; }
     828          77 :   c = RgXn_powu_i(a,b,n);
     829          77 :   c = RgX_to_RgC(c,n); settyp(c,t_VEC); return c;
     830             : }
     831             : 
     832             : /* assume lg(V) >= n*d + 2 */
     833             : static GEN
     834        5775 : c_deflate(long n, long d, GEN v)
     835             : {
     836        5775 :   long i, id, l = n+2;
     837             :   GEN w;
     838        5775 :   if (d == 1) return lg(v) == l ? v: vecslice(v, 1, l-1);
     839         294 :   w = cgetg(l, typ(v));
     840         294 :   for (i = id = 1; i < l; i++, id += d) gel(w, i) = gel(v, id);
     841         294 :   return w;
     842             : }
     843             : static GEN
     844         518 : c_mul(long n, long d, GEN F, GEN G)
     845             : {
     846         518 :   pari_sp av = avma;
     847         518 :   long nd = n*d;
     848         518 :   GEN VF = mfcoefs_i(F, nd, 1);
     849         518 :   GEN VG = mfcoefs_i(G, nd, 1);
     850         518 :   return gerepilecopy(av, c_deflate(n, d, RgV_mul_RgXn(VF,VG)));
     851             : }
     852             : static GEN
     853          77 : c_pow(long n, long d, GEN F, GEN a)
     854             : {
     855          77 :   pari_sp av = avma;
     856          77 :   long nd = n*d;
     857          77 :   GEN f = RgV_pows_RgXn(mfcoefs_i(F,nd,1), itos(a));
     858          77 :   return gerepilecopy(av, c_deflate(n, d, f));
     859             : }
     860             : 
     861             : /* F * Theta */
     862             : static GEN
     863         336 : mfmultheta(GEN F)
     864             : {
     865         336 :   if (typ(mf_get_gk(F)) == t_FRAC && mf_get_type(F) == t_MF_DIV)
     866             :   {
     867         112 :     GEN T = gel(F,3); /* hopefully mfTheta() */
     868         112 :     if (mf_get_type(T) == t_MF_THETA && mf_get_N(T) == 4) return gel(F,2);
     869             :   }
     870         224 :   return mfmul(F, mfTheta(NULL));
     871             : }
     872             : 
     873             : static GEN
     874          21 : c_bracket(long n, long d, GEN F, GEN G, GEN gm)
     875             : {
     876          21 :   pari_sp av = avma;
     877          21 :   long i, nd = n*d;
     878          21 :   GEN VF = mfcoefs_i(F, nd, 1), tF = cgetg(nd+2, t_VEC);
     879          21 :   GEN VG = mfcoefs_i(G, nd, 1), tG = cgetg(nd+2, t_VEC);
     880          21 :   GEN C, mpow, res = NULL, gk = mf_get_gk(F), gl = mf_get_gk(G);
     881          21 :   ulong j, m = itou(gm);
     882             :   /* pow[i,j+1] = i^j */
     883          21 :   mpow = cgetg(m+2, t_MAT);
     884          21 :   gel(mpow,1) = const_col(nd, gen_1);
     885          49 :   for (j = 1; j <= m; j++)
     886             :   {
     887          28 :     GEN c = cgetg(nd+1, t_COL);
     888          28 :     gel(mpow,j+1) = c;
     889          28 :     for (i = 1; i <= nd; i++) gel(c,i) = muliu(gcoeff(mpow,i,j), i);
     890             :   }
     891          21 :   C = binomial(gaddgs(gk, m-1), m);
     892          21 :   if (odd(m)) C = gneg(C);
     893          70 :   for (j = 0; j <= m; j++)
     894             :   { /* C = (-1)^(m-j) binom(m+l-1, j) binom(m+k-1,m-j) */
     895             :     GEN c;
     896          49 :     gel(tF,1) = j == 0? gel(VF,1): gen_0;
     897          49 :     gel(tG,1) = j == m? gel(VG,1): gen_0;
     898          49 :     gel(tF,2) = gel(VF,2);
     899          49 :     gel(tG,2) = gel(VG,2);
     900         413 :     for (i = 2; i <= nd; i++)
     901             :     {
     902         364 :       gel(tF, i+1) = gmul(gcoeff(mpow,i,j+1),   gel(VF, i+1));
     903         364 :       gel(tG, i+1) = gmul(gcoeff(mpow,i,m-j+1), gel(VG, i+1));
     904             :     }
     905          49 :     c = gmul(C, c_deflate(n, d, RgV_mul_RgXn(tF, tG)));
     906          49 :     res = res? gadd(res, c): c;
     907          49 :     if (j < m)
     908          56 :       C = gdiv(gmul(C, gmulsg(m-j, gaddgs(gl,m-j-1))),
     909          28 :                gmulsg(-(j+1), gaddgs(gk,j)));
     910             :   }
     911          21 :   return gerepileupto(av, res);
     912             : }
     913             : /* linear combination \sum L[j] vecF[j] */
     914             : static GEN
     915        2387 : c_linear(long n, long d, GEN F, GEN L, GEN dL)
     916             : {
     917        2387 :   pari_sp av = avma;
     918        2387 :   long j, l = lg(L);
     919        2387 :   GEN S = NULL;
     920        7546 :   for (j = 1; j < l; j++)
     921             :   {
     922        5159 :     GEN c = gel(L,j);
     923        5159 :     if (gequal0(c)) continue;
     924        4578 :     c = gmul(c, mfcoefs_i(gel(F,j), n, d));
     925        4578 :     S = S? gadd(S,c): c;
     926             :   }
     927        2387 :   if (!S) return zerovec(n+1);
     928        2387 :   if (!is_pm1(dL)) S = gdiv(S, dL);
     929        2387 :   return gerepileupto(av, S);
     930             : }
     931             : 
     932             : /* B_d(T_j Trace^new) as t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)) or
     933             :  * t_MF_HECKE(t_MF_NEWTRACE)
     934             :  * or t_MF_NEWTRACE in level N. Set d and j, return t_MF_NEWTRACE component*/
     935             : static GEN
     936       52416 : bhn_parse(GEN f, long *d, long *j)
     937             : {
     938       52416 :   long t = mf_get_type(f);
     939       52416 :   *d = *j = 1;
     940       52416 :   if (t == t_MF_BD) { *d = itos(gel(f,3)); f = gel(f,2); t = mf_get_type(f); }
     941       52416 :   if (t == t_MF_HECKE) { *j = gel(f,2)[1]; f = gel(f,3); }
     942       52416 :   return f;
     943             : }
     944             : /* f as above, return the t_MF_NEWTRACE component */
     945             : static GEN
     946       13629 : bhn_newtrace(GEN f)
     947             : {
     948       13629 :   long t = mf_get_type(f);
     949       13629 :   if (t == t_MF_BD) { f = gel(f,2); t = mf_get_type(f); }
     950       13629 :   if (t == t_MF_HECKE) f = gel(f,3);
     951       13629 :   return f;
     952             : }
     953             : static int
     954        2436 : ok_bhn_linear(GEN vf)
     955             : {
     956        2436 :   long i, N0 = 0, l = lg(vf);
     957             :   GEN CHI, gk;
     958        2436 :   if (l == 1) return 1;
     959        2436 :   gk = mf_get_gk(gel(vf,1));
     960        2436 :   CHI = mf_get_CHI(gel(vf,1));
     961        9534 :   for (i = 1; i < l; i++)
     962             :   {
     963        8568 :     GEN f = bhn_newtrace(gel(vf,i));
     964        8568 :     long N = mf_get_N(f);
     965        8568 :     if (mf_get_type(f) != t_MF_NEWTRACE) return 0;
     966        7098 :     if (N < N0) return 0; /* largest level must come last */
     967        7098 :     N0 = N;
     968        7098 :     if (!gequal(gk,mf_get_gk(f))) return 0; /* same k */
     969        7098 :     if (!gequal(gel(mf_get_CHI(f),2), gel(CHI,2))) return 0; /* same CHI */
     970             :   }
     971         966 :   return 1;
     972             : }
     973             : 
     974             : /* vF not empty, same hypotheses as bhnmat_extend */
     975             : static GEN
     976        5138 : bhnmat_extend_nocache(GEN M, long N, long n, long d, GEN vF)
     977             : {
     978             :   cachenew_t cache;
     979        5138 :   long l = lg(vF);
     980             :   GEN f;
     981        5138 :   if (l == 1) return M? M: cgetg(1, t_MAT);
     982        5061 :   f = bhn_newtrace(gel(vF,1)); /* N.B. mf_get_N(f) divides N */
     983        5061 :   init_cachenew(&cache, n*d, N, f);
     984        5061 :   M = bhnmat_extend(M, n, d, vF, &cache);
     985        5061 :   dbg_cachenew(&cache); return M;
     986             : }
     987             : /* c_linear of "bhn" mf closures, same hypotheses as bhnmat_extend */
     988             : static GEN
     989        1001 : c_linear_bhn(long n, long d, GEN F)
     990             : {
     991             :   pari_sp av;
     992        1001 :   GEN M, v, vF = gel(F,2), L = gel(F,3), dL = gel(F,4);
     993        1001 :   if (lg(L) == 1) return zerovec(n+1);
     994        1001 :   av = avma;
     995        1001 :   M = bhnmat_extend_nocache(NULL, mf_get_N(F), n, d, vF);
     996        1001 :   v = RgM_RgC_mul(M,L); settyp(v, t_VEC);
     997        1001 :   if (!is_pm1(dL)) v = gdiv(v, dL);
     998        1001 :   return gerepileupto(av, v);
     999             : }
    1000             : 
    1001             : /* c in K, K := Q[X]/(T) vz = vector of consecutive powers of root z of T
    1002             :  * attached to an embedding s: K -> C. Return s(c) in C */
    1003             : static GEN
    1004       74109 : Rg_embed1(GEN c, GEN vz)
    1005             : {
    1006       74109 :   long t = typ(c);
    1007       74109 :   if (t == t_POLMOD) { c = gel(c,2); t = typ(c); }
    1008       74109 :   if (t == t_POL) c = RgX_RgV_eval(c, vz);
    1009       74109 :   return c;
    1010             : }
    1011             : /* return s(P) in C[X] */
    1012             : static GEN
    1013         861 : RgX_embed1(GEN P, GEN vz)
    1014             : {
    1015             :   long i, l;
    1016         861 :   GEN Q = cgetg_copy(P, &l);
    1017         861 :   Q[1] = P[1];
    1018         861 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1019         861 :   return normalizepol_lg(Q,l); /* normally a no-op */
    1020             : }
    1021             : /* return s(P) in C^n */
    1022             : static GEN
    1023         567 : vecembed1(GEN P, GEN vz)
    1024             : {
    1025             :   long i, l;
    1026         567 :   GEN Q = cgetg_copy(P, &l);
    1027         567 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vz);
    1028         567 :   return Q;
    1029             : }
    1030             : /* P in L = K[X]/(U), K = Q[t]/T; s an embedding of K -> C attached
    1031             :  * to a root of T, extended to an embedding of L -> C attached to a root
    1032             :  * of s(U); vT powers of the root of T, vU powers of the root of s(U).
    1033             :  * Return s(P) in C^n */
    1034             : static GEN
    1035       13314 : Rg_embed2(GEN P, long vt, GEN vT, GEN vU)
    1036             : {
    1037             :   long i, l;
    1038             :   GEN Q;
    1039       13314 :   P = liftpol_shallow(P);
    1040       13314 :   if (typ(P) != t_POL) return P;
    1041       13300 :   if (varn(P) == vt) return Rg_embed1(P, vT);
    1042             :   /* varn(P) == vx */
    1043       13293 :   Q = cgetg_copy(P, &l); Q[1] = P[1];
    1044       13293 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed1(gel(P,i), vT);
    1045       13293 :   return Rg_embed1(Q, vU);
    1046             : }
    1047             : static GEN
    1048          42 : vecembed2(GEN P, long vt, GEN vT, GEN vU)
    1049             : {
    1050             :   long i, l;
    1051          42 :   GEN Q = cgetg_copy(P, &l);
    1052          42 :   for (i = 1; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1053          42 :   return Q;
    1054             : }
    1055             : static GEN
    1056         532 : RgX_embed2(GEN P, long vt, GEN vT, GEN vU)
    1057             : {
    1058             :   long i, l;
    1059         532 :   GEN Q = cgetg_copy(P, &l);
    1060         532 :   for (i = 2; i < l; i++) gel(Q,i) = Rg_embed2(gel(P,i), vt, vT, vU);
    1061         532 :   Q[1] = P[1]; return normalizepol_lg(Q,l);
    1062             : }
    1063             : /* embed polynomial f in variable vx [ may be a scalar ], E from getembed */
    1064             : static GEN
    1065        1575 : RgX_embed(GEN f, long vx, GEN E)
    1066             : {
    1067             :   GEN vT;
    1068        1575 :   if (typ(f) != t_POL || varn(f) != vx) return mfembed(E, f);
    1069        1554 :   if (lg(E) == 1) return f;
    1070        1358 :   vT = gel(E,2);
    1071        1358 :   if (lg(E) == 3)
    1072         826 :     f = RgX_embed1(f, vT);
    1073             :   else
    1074         532 :     f = RgX_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1075        1358 :   return f;
    1076             : }
    1077             : /* embed vector, E from getembed */
    1078             : GEN
    1079        1407 : mfvecembed(GEN E, GEN v)
    1080             : {
    1081             :   GEN vT;
    1082        1407 :   if (lg(E) == 1) return v;
    1083         609 :   vT = gel(E,2);
    1084         609 :   if (lg(E) == 3)
    1085         567 :     v = vecembed1(v, vT);
    1086             :   else
    1087          42 :     v = vecembed2(v, varn(gel(E,1)), vT, gel(E,3));
    1088         609 :   return v;
    1089             : }
    1090             : GEN
    1091           7 : mfmatembed(GEN E, GEN f)
    1092             : {
    1093             :   long i, l;
    1094             :   GEN g;
    1095           7 :   if (lg(E) == 1) return f;
    1096           7 :   g = cgetg_copy(f, &l);
    1097           7 :   for (i = 1; i < l; i++) gel(g,i) = mfvecembed(E, gel(f,i));
    1098           7 :   return g;
    1099             : }
    1100             : /* embed vector of polynomials in var vx */
    1101             : static GEN
    1102          98 : RgXV_embed(GEN f, long vx, GEN E)
    1103             : {
    1104             :   long i, l;
    1105             :   GEN v;
    1106          98 :   if (lg(E) == 1) return f;
    1107          70 :   v = cgetg_copy(f, &l);
    1108          70 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(gel(f,i), vx, E);
    1109          70 :   return v;
    1110             : }
    1111             : 
    1112             : /* embed scalar */
    1113             : GEN
    1114       95927 : mfembed(GEN E, GEN f)
    1115             : {
    1116             :   GEN vT;
    1117       95927 :   if (lg(E) == 1) return f;
    1118       13468 :   vT = gel(E,2);
    1119       13468 :   if (lg(E) == 3)
    1120        4354 :     f = Rg_embed1(f, vT);
    1121             :   else
    1122        9114 :     f = Rg_embed2(f, varn(gel(E,1)), vT, gel(E,3));
    1123       13468 :   return f;
    1124             : }
    1125             : /* vector of the sigma(f), sigma in vE */
    1126             : static GEN
    1127         273 : RgX_embedall(GEN f, long vx, GEN vE)
    1128             : {
    1129         273 :   long i, l = lg(vE);
    1130         273 :   GEN v = cgetg(l, t_VEC);
    1131         273 :   for (i = 1; i < l; i++) gel(v,i) = RgX_embed(f, vx, gel(vE,i));
    1132         273 :   return l == 2? gel(v,1): v;
    1133             : }
    1134             : /* matrix whose colums are the sigma(v), sigma in vE */
    1135             : static GEN
    1136         329 : RgC_embedall(GEN v, GEN vE)
    1137             : {
    1138         329 :   long j, l = lg(vE);
    1139         329 :   GEN M = cgetg(l, t_MAT);
    1140         329 :   for (j = 1; j < l; j++) gel(M,j) = mfvecembed(gel(vE,j), v);
    1141         329 :   return M;
    1142             : }
    1143             : /* vector of the sigma(v), sigma in vE */
    1144             : static GEN
    1145        4907 : Rg_embedall_i(GEN v, GEN vE)
    1146             : {
    1147        4907 :   long j, l = lg(vE);
    1148        4907 :   GEN M = cgetg(l, t_VEC);
    1149        4907 :   for (j = 1; j < l; j++) gel(M,j) = mfembed(gel(vE,j), v);
    1150        4907 :   return M;
    1151             : }
    1152             : /* vector of the sigma(v), sigma in vE; if #vE == 1, return v */
    1153             : static GEN
    1154       90446 : Rg_embedall(GEN v, GEN vE)
    1155       90446 : { return (lg(vE) == 2)? mfembed(gel(vE,1), v): Rg_embedall_i(v, vE); }
    1156             : 
    1157             : static GEN
    1158         224 : c_div_i(long n, GEN F, GEN G)
    1159             : {
    1160             :   GEN VF, VG, a0, a0i, H;
    1161         224 :   VF = mfcoefsser(F, n); VG = mfcoefsser(G, n);
    1162         224 :   a0 = polcoef_i(VG, 0, -1);
    1163         224 :   if (gequal0(a0) || gequal1(a0)) a0 = a0i = NULL;
    1164             :   else
    1165             :   {
    1166          49 :     a0i = ginv(a0);
    1167          49 :     VG = gmul(ser_unscale(VG,a0), a0i);
    1168          49 :     VF = gmul(ser_unscale(VF,a0), a0i);
    1169             :   }
    1170         224 :   H = gdiv(VF, VG);
    1171         224 :   if (a0) H = ser_unscale(H,a0i);
    1172         224 :   return sertovecslice(H, n);
    1173             : }
    1174             : static GEN
    1175         224 : c_div(long n, long d, GEN F, GEN G)
    1176             : {
    1177         224 :   pari_sp av = avma;
    1178         224 :   GEN D = (d==1)? c_div_i(n, F,G): c_deflate(n, d, c_div_i(n*d, F,G));
    1179         224 :   return gerepilecopy(av, D);
    1180             : }
    1181             : 
    1182             : static GEN
    1183          35 : c_shift(long n, long d, GEN F, GEN gsh)
    1184             : {
    1185          35 :   pari_sp av = avma;
    1186             :   GEN vF;
    1187          35 :   long sh = itos(gsh), n1 = n*d + sh;
    1188          35 :   if (n1 < 0) return zerovec(n+1);
    1189          35 :   vF = mfcoefs_i(F, n1, 1);
    1190          35 :   if (sh < 0) vF = shallowconcat(zerovec(-sh), vF);
    1191          35 :   else vF = vecslice(vF, sh+1, n1+1);
    1192          35 :   return gerepilecopy(av, c_deflate(n, d, vF));
    1193             : }
    1194             : 
    1195             : static GEN
    1196          21 : c_deriv(long n, long d, GEN F, GEN gm)
    1197             : {
    1198          21 :   pari_sp av = avma;
    1199          21 :   GEN V = mfcoefs_i(F, n, d), res;
    1200          21 :   long i, m = itos(gm);
    1201          21 :   if (!m) return V;
    1202          21 :   res = cgetg(n+2, t_VEC); gel(res,1) = gen_0;
    1203          21 :   if (m < 0)
    1204           7 :   { for (i=1; i <= n; i++) gel(res, i+1) = gdiv(gel(V, i+1), powuu(i,-m)); }
    1205             :   else
    1206          14 :   { for (i=1; i <= n; i++) gel(res, i+1) = gmul(gel(V,i+1), powuu(i,m)); }
    1207          21 :   return gerepileupto(av, res);
    1208             : }
    1209             : 
    1210             : static GEN
    1211          14 : c_derivE2(long n, long d, GEN F, GEN gm)
    1212             : {
    1213          14 :   pari_sp av = avma;
    1214             :   GEN VF, VE, res, tmp, gk;
    1215          14 :   long i, m = itos(gm), nd;
    1216          14 :   if (m == 0) return mfcoefs_i(F, n, d);
    1217          14 :   nd = n*d;
    1218          14 :   VF = mfcoefs_i(F, nd, 1); VE = mfcoefs_i(mfEk(2), nd, 1);
    1219          14 :   gk = mf_get_gk(F);
    1220          14 :   if (m == 1)
    1221             :   {
    1222           7 :     res = cgetg(n+2, t_VEC);
    1223           7 :     for (i = 0; i <= n; i++) gel(res, i+1) = gmulsg(i, gel(VF, i*d+1));
    1224           7 :     tmp = c_deflate(n, d, RgV_mul_RgXn(VF, VE));
    1225           7 :     return gerepileupto(av, gsub(res, gmul(gdivgs(gk, 12), tmp)));
    1226             :   }
    1227             :   else
    1228             :   {
    1229             :     long j;
    1230          35 :     for (j = 1; j <= m; j++)
    1231             :     {
    1232          28 :       tmp = RgV_mul_RgXn(VF, VE);
    1233          28 :       for (i = 0; i <= nd; i++) gel(VF, i+1) = gmulsg(i, gel(VF, i+1));
    1234          28 :       VF = gsub(VF, gmul(gdivgs(gaddgs(gk, 2*(j-1)), 12), tmp));
    1235             :     }
    1236           7 :     return gerepilecopy(av, c_deflate(n, d, VF));
    1237             :   }
    1238             : }
    1239             : 
    1240             : /* Twist by the character (D/.) */
    1241             : static GEN
    1242           7 : c_twist(long n, long d, GEN F, GEN D)
    1243             : {
    1244           7 :   pari_sp av = avma;
    1245           7 :   GEN V = mfcoefs_i(F, n, d), res = cgetg(n+2, t_VEC);
    1246             :   long i;
    1247         119 :   for (i = 0; i <= n; i++)
    1248         112 :     gel(res, i + 1) = gmulsg(krois(D, i), gel(V, i+1));
    1249           7 :   return gerepileupto(av, res);
    1250             : }
    1251             : 
    1252             : /* form F given by closure, compute T(n)(F) as closure */
    1253             : static GEN
    1254         434 : c_hecke(long m, long l, GEN DATA, GEN F)
    1255             : {
    1256         434 :   pari_sp av = avma;
    1257         434 :   return gerepilecopy(av, hecke_i(m, l, NULL, F, DATA));
    1258             : }
    1259             : static GEN
    1260         140 : c_const(long n, long d, GEN C)
    1261             : {
    1262         140 :   GEN V = zerovec(n+1);
    1263         140 :   long i, j, l = lg(C);
    1264         140 :   if (l > d*n+2) l = d*n+2;
    1265         140 :   for (i = j = 1; i < l; i+=d, j++) gel(V, j) = gcopy(gel(C,i));
    1266         140 :   return V;
    1267             : }
    1268             : 
    1269             : /* m > 0 */
    1270             : static GEN
    1271         455 : eta3_ZXn(long m)
    1272             : {
    1273         455 :   long l = m+2, n, k;
    1274         455 :   GEN P = cgetg(l,t_POL);
    1275         455 :   P[1] = evalsigne(1)|evalvarn(0);
    1276         455 :   for (n = 2; n < l; n++) gel(P,n) = gen_0;
    1277        2471 :   for (n = k = 0;; n++)
    1278             :   {
    1279        4487 :     if (k + n >= m) { setlg(P, k+3); return P; }
    1280        2016 :     k += n;
    1281             :     /* now k = n(n+1) / 2 */
    1282        2016 :     gel(P, k+2) = odd(n)? utoineg(2*n+1): utoipos(2*n+1);
    1283             :   }
    1284             : }
    1285             : 
    1286             : static GEN
    1287         462 : c_delta(long n, long d)
    1288             : {
    1289         462 :   pari_sp ltop = avma;
    1290         462 :   long N = n*d;
    1291             :   GEN e;
    1292         462 :   if (!N) return mkvec(gen_0);
    1293         455 :   e = eta3_ZXn(N);
    1294         455 :   e = ZXn_sqr(e,N);
    1295         455 :   e = ZXn_sqr(e,N);
    1296         455 :   e = ZXn_sqr(e,N); /* eta(x)^24 */
    1297         455 :   settyp(e, t_VEC);
    1298         455 :   gel(e,1) = gen_0; /* Delta(x) = x*eta(x)^24 as a t_VEC */
    1299         455 :   return gerepilecopy(ltop, c_deflate(n, d, e));
    1300             : }
    1301             : 
    1302             : /* return s(d) such that s|f <=> d | f^2 */
    1303             : static long
    1304          35 : mysqrtu(ulong d)
    1305             : {
    1306          35 :   GEN fa = myfactoru(d), P = gel(fa,1), E = gel(fa,2);
    1307          35 :   long l = lg(P), i, s = 1;
    1308          35 :   for (i = 1; i < l; i++) s *= upowuu(P[i], (E[i]+1)>>1);
    1309          35 :   return s;
    1310             : }
    1311             : static GEN
    1312        1428 : c_theta(long n, long d, GEN psi)
    1313             : {
    1314        1428 :   long lim = usqrt(n*d), F = mfcharmodulus(psi), par = mfcharparity(psi);
    1315        1428 :   long f, d2 = d == 1? 1: mysqrtu(d);
    1316        1428 :   GEN V = zerovec(n + 1);
    1317        5558 :   for (f = d2; f <= lim; f += d2)
    1318        4130 :     if (ugcd(F, f) == 1)
    1319             :     {
    1320        4123 :       pari_sp av = avma;
    1321        4123 :       GEN c = mfchareval_i(psi, f);
    1322        4123 :       gel(V, f*f/d + 1) = gerepileupto(av, par < 0 ? gmulgs(c,2*f) : gmul2n(c,1));
    1323             :     }
    1324        1428 :   if (F == 1) gel(V, 1) = gen_1;
    1325        1428 :   return V; /* no gerepile needed */
    1326             : }
    1327             : 
    1328             : static GEN
    1329         133 : c_etaquo(long n, long d, GEN eta, GEN gs)
    1330             : {
    1331         133 :   pari_sp av = avma;
    1332         133 :   long s = itos(gs), nd = n*d, nds = nd - s + 1;
    1333             :   GEN c;
    1334         133 :   if (nds <= 0) return zerovec(n+1);
    1335         112 :   c = RgX_to_RgC(eta_product_ZXn(eta, nds), nds); settyp(c, t_VEC);
    1336         112 :   if (s > 0) c = shallowconcat(zerovec(s), c);
    1337         112 :   return gerepilecopy(av, c_deflate(n, d, c));
    1338             : }
    1339             : 
    1340             : static GEN
    1341          63 : c_ell(long n, long d, GEN E)
    1342             : {
    1343          63 :   pari_sp av = avma;
    1344             :   GEN v;
    1345          63 :   if (d == 1) return concat(gen_0, anell(E, n));
    1346           7 :   v = shallowconcat(gen_0, anell(E, n*d));
    1347           7 :   return gerepilecopy(av, c_deflate(n, d, v));
    1348             : }
    1349             : 
    1350             : static GEN
    1351          21 : c_cusptrace(long n, long d, GEN F)
    1352             : {
    1353          21 :   pari_sp av = avma;
    1354          21 :   GEN D = gel(F,2), res = cgetg(n+2, t_VEC);
    1355          21 :   long i, N = mf_get_N(F), k = mf_get_k(F);
    1356          21 :   gel(res, 1) = gen_0;
    1357         140 :   for (i = 1; i <= n; i++)
    1358         119 :     gel(res, i+1) = mfcusptrace_i(N, k, i*d, mydivisorsu(i*d), D);
    1359          21 :   return gerepilecopy(av, res);
    1360             : }
    1361             : 
    1362             : static GEN
    1363         749 : c_newtrace(long n, long d, GEN F)
    1364             : {
    1365         749 :   pari_sp av = avma;
    1366             :   cachenew_t cache;
    1367         749 :   long N = mf_get_N(F);
    1368             :   GEN v;
    1369         749 :   init_cachenew(&cache, n*d, N, F);
    1370         749 :   v = colnewtrace(0, n, d, N, mf_get_k(F), &cache);
    1371         749 :   settyp(v, t_VEC); return gerepilecopy(av, v);
    1372             : }
    1373             : 
    1374             : static GEN
    1375        3780 : c_Bd(long n, long d, GEN F, GEN A)
    1376             : {
    1377        3780 :   pari_sp av = avma;
    1378        3780 :   long a = itou(A), ad = ugcd(a,d), aad = a/ad, i, j;
    1379        3780 :   GEN w, v = mfcoefs_i(F, n/aad, d/ad);
    1380        3780 :   if (a == 1) return v;
    1381        3780 :   n++; w = zerovec(n);
    1382        3780 :   for (i = j = 1; j <= n; i++, j += aad) gel(w,j) = gcopy(gel(v,i));
    1383        3780 :   return gerepileupto(av, w);
    1384             : }
    1385             : 
    1386             : static GEN
    1387        3591 : c_dihedral(long n, long d, GEN bnr, GEN w, GEN k0j)
    1388             : {
    1389        3591 :   pari_sp av = avma;
    1390        3591 :   GEN V = dihan(bnr, w, k0j, n*d);
    1391        3591 :   GEN Tinit = gel(w,3), Pm = gel(Tinit,1);
    1392        3591 :   GEN A = c_deflate(n, d, V);
    1393        3591 :   if (degpol(Pm) == 1 || RgV_is_ZV(A)) return gerepilecopy(av, A);
    1394         763 :   return gerepileupto(av, gmodulo(A, Pm));
    1395             : }
    1396             : 
    1397             : static GEN
    1398         140 : c_mfEH(long n, long d, GEN F)
    1399             : {
    1400         140 :   pari_sp av = avma;
    1401             :   GEN v, M, A;
    1402         140 :   long i, r = mf_get_r(F);
    1403         140 :   if (n == 1)
    1404          14 :     return gerepilecopy(av, mkvec2(mfEHcoef(r,0),mfEHcoef(r,d)));
    1405             :   /* speedup mfcoef */
    1406         126 :   if (r == 1)
    1407             :   {
    1408          70 :     v = cgetg(n+2, t_VEC);
    1409          70 :     gel(v,1) = sstoQ(-1,12);
    1410       83258 :     for (i = 1; i <= n; i++)
    1411             :     {
    1412       83188 :       long id = i*d, a = id & 3;
    1413       83188 :       gel(v,i+1) = (a==1 || a==2)? gen_0: sstoQ(hclassno6u(id), 6);
    1414             :     }
    1415          70 :     return v; /* no gerepile needed */
    1416             :   }
    1417          56 :   M = mfEHmat(n*d+1,r);
    1418          56 :   if (d > 1)
    1419             :   {
    1420           7 :     long l = lg(M);
    1421           7 :     for (i = 1; i < l; i++) gel(M,i) = c_deflate(n, d, gel(M,i));
    1422             :   }
    1423          56 :   A = gel(F,2); /* [num(B), den(B)] */
    1424          56 :   v = RgC_Rg_div(RgM_RgC_mul(M, gel(A,1)), gel(A,2));
    1425          56 :   settyp(v,t_VEC); return gerepileupto(av, v);
    1426             : }
    1427             : 
    1428             : static GEN
    1429        5733 : c_mfeisen(long n, long d, GEN F)
    1430             : {
    1431        5733 :   pari_sp av = avma;
    1432        5733 :   GEN v, vchi, E0, P, T, CHI, gk = mf_get_gk(F);
    1433             :   long i, k;
    1434        5733 :   if (typ(gk) != t_INT) return c_mfEH(n, d, F);
    1435        5593 :   k = itou(gk);
    1436        5593 :   vchi = gel(F,2);
    1437        5593 :   E0 = gel(vchi,1);
    1438        5593 :   T = gel(vchi,2);
    1439        5593 :   P = gel(T,1);
    1440        5593 :   CHI = gel(vchi,3);
    1441        5593 :   v = cgetg(n+2, t_VEC);
    1442        5593 :   gel(v, 1) = gcopy(E0); /* E(0) */
    1443        5593 :   if (lg(vchi) == 5)
    1444             :   { /* E_k(chi1,chi2) */
    1445        3822 :     GEN CHI2 = gel(vchi,4), F3 = gel(F,3);
    1446        3822 :     long ord = F3[1], j = F3[2];
    1447        3822 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi2(k, CHI, CHI2, i*d, ord);
    1448        3822 :     if (lg(T) == 4) v = QabV_tracerel(T, j, v);
    1449             :   }
    1450             :   else
    1451             :   { /* E_k(chi) */
    1452        1771 :     for (i = 1; i <= n; i++) gel(v, i+1) = sigchi(k, CHI, i*d);
    1453             :   }
    1454        5593 :   if (degpol(P) != 1 && !RgV_is_QV(v)) return gerepileupto(av, gmodulo(v, P));
    1455        4942 :   return gerepilecopy(av, v);
    1456             : }
    1457             : 
    1458             : /* L(chi_D, 1-k) */
    1459             : static GEN
    1460          28 : lfunquadneg(long D, long k)
    1461             : {
    1462          28 :   GEN B, dS, S = gen_0;
    1463          28 :   long r, N = labs(D);
    1464             :   pari_sp av;
    1465          28 :   if (k == 1 && N == 1) return gneg(ghalf);
    1466             :   /* B = N^k * denom(B) * B(x/N) */
    1467          28 :   B = ZX_rescale(Q_remove_denom(bernpol(k, 0), &dS), utoi(N));
    1468          28 :   dS = mul_denom(dS, stoi(-N*k));
    1469          28 :   av = avma;
    1470        7175 :   for (r = 0; r < N; r++)
    1471             :   {
    1472        7147 :     long c = kross(D, r);
    1473        7147 :     if (c)
    1474             :     {
    1475        5152 :       GEN tmp = poleval(B, utoi(r));
    1476        5152 :       S = c > 0 ? addii(S, tmp) : subii(S, tmp);
    1477        5152 :       S = gerepileuptoint(av, S);
    1478             :     }
    1479             :   }
    1480          28 :   return gdiv(S, dS);
    1481             : }
    1482             : 
    1483             : /* Returns vector of coeffs from F[0], F[d], ..., F[d*n] */
    1484             : static GEN
    1485       21434 : mfcoefs_i(GEN F, long n, long d)
    1486             : {
    1487       21434 :   if (n < 0) return gen_0;
    1488       21434 :   switch(mf_get_type(F))
    1489             :   {
    1490         140 :     case t_MF_CONST: return c_const(n, d, gel(F,2));
    1491        5733 :     case t_MF_EISEN: return c_mfeisen(n, d, F);
    1492         658 :     case t_MF_Ek: return c_Ek(n, d, F);
    1493         462 :     case t_MF_DELTA: return c_delta(n, d);
    1494        1365 :     case t_MF_THETA: return c_theta(n, d, gel(F,2));
    1495         133 :     case t_MF_ETAQUO: return c_etaquo(n, d, gel(F,2), gel(F,3));
    1496          63 :     case t_MF_ELL: return c_ell(n, d, gel(F,2));
    1497         518 :     case t_MF_MUL: return c_mul(n, d, gel(F,2), gel(F,3));
    1498          77 :     case t_MF_POW: return c_pow(n, d, gel(F,2), gel(F,3));
    1499          21 :     case t_MF_BRACKET: return c_bracket(n, d, gel(F,2), gel(F,3), gel(F,4));
    1500        2387 :     case t_MF_LINEAR: return c_linear(n, d, gel(F,2), gel(F,3), gel(F,4));
    1501        1001 :     case t_MF_LINEAR_BHN: return c_linear_bhn(n, d, F);
    1502         224 :     case t_MF_DIV: return c_div(n, d, gel(F,2), gel(F,3));
    1503          35 :     case t_MF_SHIFT: return c_shift(n, d, gel(F,2), gel(F,3));
    1504          21 :     case t_MF_DERIV: return c_deriv(n, d, gel(F,2), gel(F,3));
    1505          14 :     case t_MF_DERIVE2: return c_derivE2(n, d, gel(F,2), gel(F,3));
    1506           7 :     case t_MF_TWIST: return c_twist(n, d, gel(F,2), gel(F,3));
    1507         434 :     case t_MF_HECKE: return c_hecke(n, d, gel(F,2), gel(F,3));
    1508        3780 :     case t_MF_BD: return c_Bd(n, d, gel(F,2), gel(F,3));
    1509          21 :     case t_MF_TRACE: return c_cusptrace(n, d, F);
    1510         749 :     case t_MF_NEWTRACE: return c_newtrace(n, d, F);
    1511        3591 :     case t_MF_DIHEDRAL: return c_dihedral(n, d, gel(F,2), gel(F,3), gel(F,4));
    1512             :     default: pari_err_TYPE("mfcoefs",F); return NULL;/*LCOV_EXCL_LINE*/
    1513             :   }
    1514             : }
    1515             : 
    1516             : static GEN
    1517         147 : matdeflate(long n, long d, GEN M)
    1518             : {
    1519             :   long i, l;
    1520             :   GEN A;
    1521             :   /*  if (d == 1) return M; */
    1522         147 :   A = cgetg_copy(M,&l);
    1523         147 :   for (i = 1; i < l; i++) gel(A,i) = c_deflate(n,d,gel(M,i));
    1524         147 :   return A;
    1525             : }
    1526             : static int
    1527        5201 : space_is_cusp(long space) { return space != mf_FULL && space != mf_EISEN; }
    1528             : /* safe with flraw mf */
    1529             : static GEN
    1530        1995 : mfcoefs_mf(GEN mf, long n, long d)
    1531             : {
    1532        1995 :   GEN MS, ME, E = MF_get_E(mf), S = MF_get_S(mf), M = MF_get_M(mf);
    1533        1995 :   long lE = lg(E), lS = lg(S), l = lE+lS-1;
    1534             : 
    1535        1995 :   if (l == 1) return cgetg(1, t_MAT);
    1536        1883 :   if (typ(M) == t_MAT && lg(M) != 1 && (n+1)*d < nbrows(M))
    1537          21 :     return matdeflate(n, d, M); /*cached; lg = 1 is possible from mfinit */
    1538        1862 :   ME = (lE == 1)? cgetg(1, t_MAT): mfvectomat(E, n, d);
    1539        1862 :   if (lS == 1)
    1540         357 :     MS = cgetg(1, t_MAT);
    1541        1505 :   else if (mf_get_type(gel(S,1)) == t_MF_DIV) /*k 1/2-integer or k=1 (exotic)*/
    1542         126 :     MS = matdeflate(n,d, mflineardivtomat(MF_get_N(mf), S, n*d));
    1543        1379 :   else if (MF_get_k(mf) == 1) /* k = 1 (dihedral) */
    1544             :   {
    1545         140 :     GEN M = mfvectomat(gmael(S,1,2), n, d);
    1546             :     long i;
    1547         140 :     MS = cgetg(lS, t_MAT);
    1548         448 :     for (i = 1; i < lS; i++)
    1549             :     {
    1550         308 :       GEN f = gel(S,i), dc = gel(f,4), c = RgM_RgC_mul(M, gel(f,3));
    1551         308 :       if (!equali1(dc)) c = RgC_Rg_div(c,dc);
    1552         308 :       gel(MS,i) = c;
    1553             :     }
    1554             :   }
    1555             :   else /* k >= 2 integer */
    1556        1239 :     MS = bhnmat_extend_nocache(NULL, MF_get_N(mf), n, d, S);
    1557        1862 :   return shallowconcat(ME,MS);
    1558             : }
    1559             : GEN
    1560        3255 : mfcoefs(GEN F, long n, long d)
    1561             : {
    1562        3255 :   if (!checkmf_i(F))
    1563             :   {
    1564          42 :     pari_sp av = avma;
    1565          42 :     GEN mf = checkMF_i(F); if (!mf) pari_err_TYPE("mfcoefs", F);
    1566          42 :     return gerepilecopy(av, mfcoefs_mf(mf,n,d));
    1567             :   }
    1568        3213 :   if (d <= 0) pari_err_DOMAIN("mfcoefs", "d", "<=", gen_0, stoi(d));
    1569        3213 :   if (n < 0) return cgetg(1, t_VEC);
    1570        3213 :   return mfcoefs_i(F, n, d);
    1571             : }
    1572             : 
    1573             : /* assume k >= 0 */
    1574             : static GEN
    1575         210 : mfak_i(GEN F, long k)
    1576             : {
    1577         210 :   if (!k) return gel(mfcoefs_i(F,0,1), 1);
    1578         154 :   return gel(mfcoefs_i(F,1,k), 2);
    1579             : }
    1580             : GEN
    1581          70 : mfcoef(GEN F, long n)
    1582             : {
    1583          70 :   pari_sp av = avma;
    1584          70 :   if (!checkmf_i(F)) pari_err_TYPE("mfcoef",F);
    1585          70 :   return n < 0? gen_0: gerepilecopy(av, mfak_i(F, n));
    1586             : }
    1587             : 
    1588             : static GEN
    1589         112 : paramconst() { return tagparams(t_MF_CONST, mkNK(1,0,mfchartrivial())); }
    1590             : static GEN
    1591          70 : mftrivial(void) { retmkvec2(paramconst(), cgetg(1,t_VEC)); }
    1592             : static GEN
    1593          42 : mf1(void) { retmkvec2(paramconst(), mkvec(gen_1)); }
    1594             : 
    1595             : /* induce mfchar CHI to G */
    1596             : static GEN
    1597      306621 : induce(GEN G, GEN CHI)
    1598             : {
    1599             :   GEN o, chi;
    1600      306621 :   if (typ(CHI) == t_INT) /* Kronecker */
    1601             :   {
    1602      300860 :     chi = znchar_quad(G, CHI);
    1603      300860 :     o = ZV_equal0(chi)? gen_1: gen_2;
    1604      300860 :     CHI = mkvec4(G,chi,o,cgetg(1,t_VEC));
    1605             :   }
    1606             :   else
    1607             :   {
    1608        5761 :     if (mfcharmodulus(CHI) == itos(znstar_get_N(G))) return CHI;
    1609        5362 :     CHI = leafcopy(CHI);
    1610        5362 :     chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    1611        5362 :     gel(CHI,1) = G;
    1612        5362 :     gel(CHI,2) = chi;
    1613             :   }
    1614      306222 :   return CHI;
    1615             : }
    1616             : /* induce mfchar CHI to znstar(G) */
    1617             : static GEN
    1618       42441 : induceN(long N, GEN CHI)
    1619             : {
    1620       42441 :   if (mfcharmodulus(CHI) != N) CHI = induce(znstar0(utoipos(N),1), CHI);
    1621       42441 :   return CHI;
    1622             : }
    1623             : /* *pCHI1 and *pCHI2 are mfchar, induce to common modulus */
    1624             : static void
    1625        5257 : char2(GEN *pCHI1, GEN *pCHI2)
    1626             : {
    1627        5257 :   GEN CHI1 = *pCHI1, G1 = gel(CHI1,1), N1 = znstar_get_N(G1);
    1628        5257 :   GEN CHI2 = *pCHI2, G2 = gel(CHI2,1), N2 = znstar_get_N(G2);
    1629        5257 :   if (!equalii(N1,N2))
    1630             :   {
    1631        3850 :     GEN G, d = gcdii(N1,N2);
    1632        3850 :     if      (equalii(N2,d)) *pCHI2 = induce(G1, CHI2);
    1633        1267 :     else if (equalii(N1,d)) *pCHI1 = induce(G2, CHI1);
    1634             :     else
    1635             :     {
    1636         154 :       if (!equali1(d)) N2 = diviiexact(N2,d);
    1637         154 :       G = znstar0(mulii(N1,N2), 1);
    1638         154 :       *pCHI1 = induce(G, CHI1);
    1639         154 :       *pCHI2 = induce(G, CHI2);
    1640             :     }
    1641             :   }
    1642        5257 : }
    1643             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1644             : static GEN
    1645      301875 : mfcharmul_i(GEN CHI1, GEN CHI2)
    1646             : {
    1647      301875 :   GEN G = gel(CHI1,1), chi3 = zncharmul(G, gel(CHI1,2), gel(CHI2,2));
    1648      301875 :   return mfcharGL(G, chi3);
    1649             : }
    1650             : /* mfchar or charinit; outputs a mfchar */
    1651             : static GEN
    1652        1022 : mfcharmul(GEN CHI1, GEN CHI2)
    1653             : {
    1654        1022 :   char2(&CHI1, &CHI2); return mfcharmul_i(CHI1,CHI2);
    1655             : }
    1656             : /* mfchar or charinit; outputs a mfchar */
    1657             : static GEN
    1658         105 : mfcharpow(GEN CHI, GEN n)
    1659             : {
    1660             :   GEN G, chi;
    1661         105 :   G = gel(CHI,1); chi = zncharpow(G, gel(CHI,2), n);
    1662         105 :   return mfcharGL(G, chi);
    1663             : }
    1664             : /* mfchar or charinit wrt same modulus; outputs a mfchar */
    1665             : static GEN
    1666        4235 : mfchardiv_i(GEN CHI1, GEN CHI2)
    1667             : {
    1668        4235 :   GEN G = gel(CHI1,1), chi3 = znchardiv(G, gel(CHI1,2), gel(CHI2,2));
    1669        4235 :   return mfcharGL(G, chi3);
    1670             : }
    1671             : /* mfchar or charinit; outputs a mfchar */
    1672             : static GEN
    1673        4235 : mfchardiv(GEN CHI1, GEN CHI2)
    1674             : {
    1675        4235 :   char2(&CHI1, &CHI2); return mfchardiv_i(CHI1,CHI2);
    1676             : }
    1677             : static GEN
    1678          28 : mfcharconj(GEN CHI)
    1679             : {
    1680          28 :   CHI = leafcopy(CHI);
    1681          28 :   gel(CHI,2) = zncharconj(gel(CHI,1), gel(CHI,2));
    1682          28 :   return CHI;
    1683             : }
    1684             : 
    1685             : /* CHI mfchar, assume 4 | N. Multiply CHI by \chi_{-4} */
    1686             : static GEN
    1687        1071 : mfchilift(GEN CHI, long N)
    1688             : {
    1689        1071 :   CHI = induceN(N, CHI);
    1690        1071 :   return mfcharmul_i(CHI, induce(gel(CHI,1), stoi(-4)));
    1691             : }
    1692             : /* CHI defined mod N, N4 = N/4;
    1693             :  * if CHI is defined mod N4 return CHI;
    1694             :  * else if CHI' = CHI*(-4,.) is defined mod N4, return CHI' (primitive)
    1695             :  * else return NULL */
    1696             : static GEN
    1697          70 : mfcharchiliftprim(GEN CHI, long N4)
    1698             : {
    1699          70 :   long FC = mfcharconductor(CHI);
    1700          70 :   if (N4 % FC == 0) return CHI;
    1701          14 :   CHI = mfchilift(CHI, N4 << 2);
    1702          14 :   CHI = mfchartoprimitive(CHI, &FC);
    1703          14 :   return (N4 % FC == 0)? CHI: NULL;
    1704             : }
    1705             : static GEN
    1706        2198 : mfchiadjust(GEN CHI, GEN gk, long N)
    1707             : {
    1708        2198 :   long par = mfcharparity(CHI);
    1709        2198 :   if (typ(gk) == t_INT &&  mpodd(gk)) par = -par;
    1710        2198 :   return par == 1 ? CHI : mfchilift(CHI, N);
    1711             : }
    1712             : 
    1713             : static GEN
    1714        3003 : mfsamefield(GEN P, GEN Q)
    1715             : {
    1716        3003 :   if (degpol(P) == 1) return Q;
    1717         455 :   if (degpol(Q) == 1) return P;
    1718         427 :   if (!gequal(P,Q)) pari_err_TYPE("mfsamefield [different fields]",mkvec2(P,Q));
    1719         420 :   return P;
    1720             : }
    1721             : 
    1722             : GEN
    1723         336 : mfmul(GEN f, GEN g)
    1724             : {
    1725         336 :   pari_sp av = avma;
    1726             :   GEN N, K, NK, CHI;
    1727         336 :   if (!checkmf_i(f)) pari_err_TYPE("mfmul",f);
    1728         336 :   if (!checkmf_i(g)) pari_err_TYPE("mfmul",g);
    1729         336 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1730         336 :   K = gadd(mf_get_gk(f), mf_get_gk(g));
    1731         336 :   CHI = mfcharmul(mf_get_CHI(f), mf_get_CHI(g));
    1732         336 :   CHI = mfchiadjust(CHI, K, itos(N));
    1733         336 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    1734         329 :   return gerepilecopy(av, tag2(t_MF_MUL, NK, f, g));
    1735             : }
    1736             : GEN
    1737          49 : mfpow(GEN f, long n)
    1738             : {
    1739          49 :   pari_sp av = avma;
    1740             :   GEN KK, NK, gn, CHI;
    1741          49 :   if (!checkmf_i(f)) pari_err_TYPE("mfpow",f);
    1742          49 :   if (!n) return mf1();
    1743          49 :   if (n == 1) return gcopy(f);
    1744          49 :   KK = gmulsg(n,mf_get_gk(f));
    1745          49 :   gn = stoi(n);
    1746          49 :   CHI = mfcharpow(mf_get_CHI(f), gn);
    1747          49 :   CHI = mfchiadjust(CHI, KK, mf_get_N(f));
    1748          49 :   NK = mkgNK(mf_get_gN(f), KK, CHI, mf_get_field(f));
    1749          49 :   return gerepilecopy(av, tag2(t_MF_POW, NK, f, gn));
    1750             : }
    1751             : GEN
    1752          21 : mfbracket(GEN f, GEN g, long m)
    1753             : {
    1754          21 :   pari_sp av = avma;
    1755             :   GEN N, K, NK, CHI;
    1756          21 :   if (!checkmf_i(f)) pari_err_TYPE("mfbracket",f);
    1757          21 :   if (!checkmf_i(g)) pari_err_TYPE("mfbracket",g);
    1758          21 :   if (m < 0) pari_err_TYPE("mfbracket [m<0]",stoi(m));
    1759          21 :   K = gaddgs(gadd(mf_get_gk(f), mf_get_gk(g)), 2*m);
    1760          21 :   if (gsigne(K) < 0) pari_err_IMPL("mfbracket for this form");
    1761          21 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1762          21 :   CHI = mfcharmul(mf_get_CHI(f), mf_get_CHI(g));
    1763          21 :   CHI = mfchiadjust(CHI, K, itou(N));
    1764          21 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    1765          21 :   return gerepilecopy(av, tag3(t_MF_BRACKET, NK, f, g, utoi(m)));
    1766             : }
    1767             : 
    1768             : /* remove 0 entries in L */
    1769             : static int
    1770        1078 : mflinear_strip(GEN *pF, GEN *pL)
    1771             : {
    1772        1078 :   pari_sp av = avma;
    1773        1078 :   GEN F = *pF, L = *pL;
    1774        1078 :   long i, j, l = lg(L);
    1775        1078 :   GEN F2 = cgetg(l, t_VEC), L2 = cgetg(l, t_VEC);
    1776        6545 :   for (i = j = 1; i < l; i++)
    1777             :   {
    1778        5467 :     if (gequal0(gel(L,i))) continue;
    1779        3031 :     gel(F2,j) = gel(F,i);
    1780        3031 :     gel(L2,j) = gel(L,i); j++;
    1781             :   }
    1782        1078 :   if (j == l) set_avma(av);
    1783             :   else
    1784             :   {
    1785         280 :     setlg(F2,j); *pF = F2;
    1786         280 :     setlg(L2,j); *pL = L2;
    1787             :   }
    1788        1078 :   return (j > 1);
    1789             : }
    1790             : static GEN
    1791        4102 : taglinear_i(long t, GEN NK, GEN F, GEN L)
    1792             : {
    1793             :   GEN dL;
    1794        4102 :   L = Q_remove_denom(L, &dL); if (!dL) dL = gen_1;
    1795        4102 :   return tag3(t, NK, F, L, dL);
    1796             : }
    1797             : static GEN
    1798        1442 : taglinear(GEN NK, GEN F, GEN L)
    1799             : {
    1800        1442 :   long t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1801        1442 :    return taglinear_i(t, NK, F, L);
    1802             : }
    1803             : /* assume F has parameters NK = [N,K,CHI] */
    1804             : static GEN
    1805         301 : mflinear_i(GEN NK, GEN F, GEN L)
    1806             : {
    1807         301 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1808         301 :   return taglinear(NK, F,L);
    1809             : }
    1810             : static GEN
    1811         462 : mflinear_bhn(GEN mf, GEN L)
    1812             : {
    1813             :   long i, l;
    1814         462 :   GEN P, NK, F = MF_get_S(mf);
    1815         462 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1816         455 :   l = lg(L); P = pol_x(1);
    1817        2520 :   for (i = 1; i < l; i++)
    1818             :   {
    1819        2065 :     GEN c = gel(L,i);
    1820        2065 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1) P = mfsamefield(P,gel(c,1));
    1821             :   }
    1822         455 :   NK = mkgNK(MF_get_gN(mf), MF_get_gk(mf), MF_get_CHI(mf), P);
    1823         455 :   return taglinear_i(t_MF_LINEAR_BHN,  NK, F,L);
    1824             : }
    1825             : 
    1826             : /* F vector of forms with same weight and character but varying level, return
    1827             :  * global [N,k,chi,P] */
    1828             : static GEN
    1829        1974 : vecmfNK(GEN F)
    1830             : {
    1831        1974 :   long i, l = lg(F);
    1832             :   GEN N, f;
    1833        1974 :   if (l == 1) return mkNK(1, 0, mfchartrivial());
    1834        1974 :   f = gel(F,1); N = mf_get_gN(f);
    1835        1974 :   for (i = 2; i < l; i++) N = lcmii(N, mf_get_gN(gel(F,i)));
    1836        1974 :   return mkgNK(N, mf_get_gk(f), mf_get_CHI(f), mf_get_field(f));
    1837             : }
    1838             : /* do not use mflinear: mflineardivtomat rely on F being constant across the
    1839             :  * basis where mflinear strips the ones matched by 0 coeffs. Assume k and CHI
    1840             :  * constant, N is allowed to vary. */
    1841             : static GEN
    1842         994 : vecmflinear(GEN F, GEN C)
    1843             : {
    1844         994 :   long i, t, l = lg(C);
    1845         994 :   GEN NK, v = cgetg(l, t_VEC);
    1846         994 :   if (l == 1) return v;
    1847         994 :   t = ok_bhn_linear(F)? t_MF_LINEAR_BHN: t_MF_LINEAR;
    1848         994 :   NK = vecmfNK(F);
    1849         994 :   for (i = 1; i < l; i++) gel(v,i) = taglinear_i(t, NK, F, gel(C,i));
    1850         994 :   return v;
    1851             : }
    1852             : /* vecmflinear(F,C), then divide everything by E, which has valuation 0 */
    1853             : static GEN
    1854         266 : vecmflineardiv0(GEN F, GEN C, GEN E)
    1855             : {
    1856         266 :   GEN v = vecmflinear(F, C);
    1857         266 :   long i, l = lg(v);
    1858         266 :   for (i = 1; i < l; i++) gel(v,i) = mfdiv_val(gel(v,i), E, 0);
    1859         266 :   return v;
    1860             : }
    1861             : 
    1862             : /* Non empty linear combination of linear combinations of same
    1863             :  * F_j=\sum_i \mu_{i,j}G_i so R = \sum_i (\sum_j(\la_j\mu_{i,j})) G_i */
    1864             : static GEN
    1865         980 : mflinear_linear(GEN F, GEN L, int strip)
    1866             : {
    1867         980 :   long l = lg(F), j;
    1868         980 :   GEN vF, M = cgetg(l, t_MAT);
    1869        6839 :   for (j = 1; j < l; j++)
    1870             :   {
    1871        5859 :     GEN f = gel(F,j), c = gel(f,3), d = gel(f,4);
    1872        5859 :     if (typ(c) == t_VEC) c = shallowtrans(c);
    1873        5859 :     if (!isint1(d)) c = RgC_Rg_div(c, d);
    1874        5859 :     gel(M,j) = c;
    1875             :   }
    1876         980 :   vF = gmael(F,1,2);
    1877         980 :   L = RgM_RgC_mul(M,L);
    1878         980 :   if (strip && !mflinear_strip(&vF,&L)) return mftrivial();
    1879         980 :   return taglinear(vecmfNK(vF), vF, L);
    1880             : }
    1881             : /* F non-empty vector of forms of the form mfdiv(mflinear(B,v), E) where E
    1882             :  * does not vanish at oo, or mflinear(B,v). Apply mflinear(F, L) */
    1883             : static GEN
    1884         980 : mflineardiv_linear(GEN F, GEN L, int strip)
    1885             : {
    1886         980 :   long l = lg(F), j;
    1887             :   GEN v, E, f;
    1888         980 :   if (lg(L) != l) pari_err_DIM("mflineardiv_linear");
    1889         980 :   f = gel(F,1); /* l > 1 */
    1890         980 :   if (mf_get_type(f) != t_MF_DIV) return mflinear_linear(F,L,strip);
    1891         812 :   E = gel(f,3);
    1892         812 :   v = cgetg(l, t_VEC);
    1893         812 :   for (j = 1; j < l; j++) { GEN f = gel(F,j); gel(v,j) = gel(f,2); }
    1894         812 :   return mfdiv_val(mflinear_linear(v,L,strip), E, 0);
    1895             : }
    1896             : static GEN
    1897         259 : vecmflineardiv_linear(GEN F, GEN M)
    1898             : {
    1899         259 :   long i, l = lg(M);
    1900         259 :   GEN v = cgetg(l, t_VEC);
    1901         259 :   for (i = 1; i < l; i++) gel(v,i) = mflineardiv_linear(F, gel(M,i), 0);
    1902         259 :   return v;
    1903             : }
    1904             : 
    1905             : static GEN
    1906         476 : tobasis(GEN mf, GEN F, GEN L)
    1907             : {
    1908         476 :   if (checkmf_i(L) && mf) return mftobasis(mf, L, 0);
    1909         469 :   if (typ(F) != t_VEC) pari_err_TYPE("mflinear",F);
    1910         469 :   if (!is_vec_t(typ(L))) pari_err_TYPE("mflinear",L);
    1911         469 :   if (lg(L) != lg(F)) pari_err_DIM("mflinear");
    1912         469 :   return L;
    1913             : }
    1914             : GEN
    1915         504 : mflinear(GEN F, GEN L)
    1916             : {
    1917         504 :   pari_sp av = avma;
    1918         504 :   GEN G, NK, P, mf = checkMF_i(F), N = NULL, K = NULL, CHI = NULL;
    1919             :   long i, l;
    1920         504 :   if (mf)
    1921             :   {
    1922         378 :     GEN gk = MF_get_gk(mf);
    1923         378 :     F = MF_get_basis(F);
    1924         378 :     if (typ(gk) != t_INT)
    1925          28 :       return gerepilecopy(av, mflineardiv_linear(F, L, 1));
    1926         350 :     if (itou(gk) > 1 && space_is_cusp(MF_get_space(mf)))
    1927             :     {
    1928         238 :       L = tobasis(mf, F, L);
    1929         238 :       return gerepilecopy(av, mflinear_bhn(mf, L));
    1930             :     }
    1931             :   }
    1932         238 :   L = tobasis(mf, F, L);
    1933         238 :   if (!mflinear_strip(&F,&L)) return mftrivial();
    1934             : 
    1935         231 :   l = lg(F);
    1936         231 :   if (l == 2 && gequal1(gel(L,1))) return gerepilecopy(av, gel(F,1));
    1937         175 :   P = pol_x(1);
    1938         581 :   for (i = 1; i < l; i++)
    1939             :   {
    1940         413 :     GEN f = gel(F,i), c = gel(L,i), Ni, Ki;
    1941         413 :     if (!checkmf_i(f)) pari_err_TYPE("mflinear", f);
    1942         413 :     Ni = mf_get_gN(f); N = N? lcmii(N, Ni): Ni;
    1943         413 :     Ki = mf_get_gk(f);
    1944         413 :     if (!K) K = Ki;
    1945         238 :     else if (!gequal(K, Ki))
    1946           7 :       pari_err_TYPE("mflinear [different weights]", mkvec2(K,Ki));
    1947         406 :     P = mfsamefield(P, mf_get_field(f));
    1948         406 :     if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1) P = mfsamefield(P, gel(c,1));
    1949             :   }
    1950         168 :   G = znstar0(N,1);
    1951         560 :   for (i = 1; i < l; i++)
    1952             :   {
    1953         399 :     GEN CHI2 = mf_get_CHI(gel(F,i));
    1954         399 :     CHI2 = induce(G, CHI2);
    1955         399 :     if (!CHI) CHI = CHI2;
    1956         231 :     else if (!gequal(CHI, CHI2))
    1957           7 :       pari_err_TYPE("mflinear [different characters]", mkvec2(CHI,CHI2));
    1958             :   }
    1959         161 :   NK = mkgNK(N, K, CHI, P);
    1960         161 :   return gerepilecopy(av, taglinear(NK,F,L));
    1961             : }
    1962             : 
    1963             : GEN
    1964          42 : mfshift(GEN F, long sh)
    1965             : {
    1966          42 :   pari_sp av = avma;
    1967          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfshift",F);
    1968          42 :   return gerepilecopy(av, tag2(t_MF_SHIFT, mf_get_NK(F), F, stoi(sh)));
    1969             : }
    1970             : static long
    1971          42 : mfval(GEN F)
    1972             : {
    1973          42 :   pari_sp av = avma;
    1974          42 :   long i = 0, n, sb;
    1975             :   GEN gk, gN;
    1976          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfval", F);
    1977          42 :   gN = mf_get_gN(F);
    1978          42 :   gk = mf_get_gk(F);
    1979          42 :   sb = mfsturmNgk(itou(gN), gk);
    1980         105 :   for (n = 1; n <= sb;)
    1981             :   {
    1982             :     GEN v;
    1983          56 :     if (n > 0.5*sb) n = sb+1;
    1984          56 :     v = mfcoefs_i(F, n, 1);
    1985         112 :     for (; i <= n; i++)
    1986          91 :       if (!gequal0(gel(v, i+1))) return gc_long(av,i);
    1987          21 :     n <<= 1;
    1988             :   }
    1989           7 :   return gc_long(av,-1);
    1990             : }
    1991             : 
    1992             : GEN
    1993        1722 : mfdiv_val(GEN f, GEN g, long vg)
    1994             : {
    1995             :   GEN N, K, NK, CHI;
    1996        1722 :   if (vg) { f = mfshift(f,vg); g = mfshift(g,vg); }
    1997        1722 :   N = lcmii(mf_get_gN(f), mf_get_gN(g));
    1998        1722 :   K = gsub(mf_get_gk(f), mf_get_gk(g));
    1999        1722 :   CHI = mfchardiv(mf_get_CHI(f), mf_get_CHI(g));
    2000        1722 :   CHI = mfchiadjust(CHI, K, itos(N));
    2001        1722 :   NK = mkgNK(N, K, CHI, mfsamefield(mf_get_field(f), mf_get_field(g)));
    2002        1722 :   return tag2(t_MF_DIV, NK, f, g);
    2003             : }
    2004             : GEN
    2005          42 : mfdiv(GEN F, GEN G)
    2006             : {
    2007          42 :   pari_sp av = avma;
    2008          42 :   long v = mfval(G);
    2009          42 :   if (!checkmf_i(F)) pari_err_TYPE("mfdiv", F);
    2010          35 :   if (v < 0 || (v && !gequal0(mfcoefs(F, v-1, 1))))
    2011          14 :     pari_err_DOMAIN("mfdiv", "ord(G)", ">", strtoGENstr("ord(F)"),
    2012             :                     mkvec2(F, G));
    2013          21 :   return gerepilecopy(av, mfdiv_val(F, G, v));
    2014             : }
    2015             : GEN
    2016          28 : mfderiv(GEN F, long m)
    2017             : {
    2018          28 :   pari_sp av = avma;
    2019             :   GEN NK, gk;
    2020          28 :   if (!checkmf_i(F)) pari_err_TYPE("mfderiv",F);
    2021          28 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2022          28 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2023          28 :   return gerepilecopy(av, tag2(t_MF_DERIV, NK, F, stoi(m)));
    2024             : }
    2025             : GEN
    2026          21 : mfderivE2(GEN F, long m)
    2027             : {
    2028          21 :   pari_sp av = avma;
    2029             :   GEN NK, gk;
    2030          21 :   if (!checkmf_i(F)) pari_err_TYPE("mfderivE2",F);
    2031          21 :   if (m < 0) pari_err_DOMAIN("mfderivE2","m","<",gen_0,stoi(m));
    2032          21 :   gk = gaddgs(mf_get_gk(F), 2*m);
    2033          21 :   NK = mkgNK(mf_get_gN(F), gk, mf_get_CHI(F), mf_get_field(F));
    2034          21 :   return gerepilecopy(av, tag2(t_MF_DERIVE2, NK, F, stoi(m)));
    2035             : }
    2036             : 
    2037             : GEN
    2038          14 : mftwist(GEN F, GEN D)
    2039             : {
    2040          14 :   pari_sp av = avma;
    2041             :   GEN NK, CHI, NT, Da;
    2042             :   long q;
    2043          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftwist", F);
    2044          14 :   if (typ(D) != t_INT) pari_err_TYPE("mftwist", D);
    2045          14 :   Da = mpabs_shallow(D);
    2046          14 :   CHI = mf_get_CHI(F); q = mfcharconductor(CHI);
    2047          14 :   NT = glcm(glcm(mf_get_gN(F), mulsi(q, Da)), sqri(Da));
    2048          14 :   NK = mkgNK(NT, mf_get_gk(F), CHI, mf_get_field(F));
    2049          14 :   return gerepilecopy(av, tag2(t_MF_TWIST, NK, F, D));
    2050             : }
    2051             : 
    2052             : /***************************************************************/
    2053             : /*                 Generic cache handling                      */
    2054             : /***************************************************************/
    2055             : enum { cache_FACT, cache_DIV, cache_H, cache_D, cache_DIH };
    2056             : typedef struct {
    2057             :   const char *name;
    2058             :   GEN cache;
    2059             :   ulong minself;
    2060             :   ulong maxself;
    2061             :   void (*init)(long);
    2062             :   ulong miss;
    2063             :   ulong maxmiss;
    2064             : } cache;
    2065             : 
    2066             : static void constfact(long lim);
    2067             : static void constdiv(long lim);
    2068             : static void consttabh(long lim);
    2069             : static void consttabdihedral(long lim);
    2070             : static void constcoredisc(long lim);
    2071             : static THREAD cache caches[] = {
    2072             : { "Factors",  NULL,  50000,    50000, &constfact, 0, 0 },
    2073             : { "Divisors", NULL,  50000,    50000, &constdiv, 0, 0 },
    2074             : { "H",        NULL, 100000, 10000000, &consttabh, 0, 0 },
    2075             : { "CorediscF",NULL, 100000, 10000000, &constcoredisc, 0, 0 },
    2076             : { "Dihedral", NULL,   1000,     3000, &consttabdihedral, 0, 0 },
    2077             : };
    2078             : 
    2079             : static void
    2080         315 : cache_reset(long id) { caches[id].miss = caches[id].maxmiss = 0; }
    2081             : static void
    2082        6168 : cache_delete(long id) { if (caches[id].cache) gunclone(caches[id].cache); }
    2083             : static void
    2084         322 : cache_set(long id, GEN S)
    2085             : {
    2086         322 :   GEN old = caches[id].cache;
    2087         322 :   caches[id].cache = gclone(S);
    2088         322 :   if (old) gunclone(old);
    2089         322 : }
    2090             : 
    2091             : /* handle a cache miss: store stats, possibly reset table; return value
    2092             :  * if (now) cached; return NULL on failure. HACK: some caches contain an
    2093             :  * ulong where the 0 value is impossible, and return it (typecase to GEN) */
    2094             : static GEN
    2095   143128743 : cache_get(long id, ulong D)
    2096             : {
    2097   143128743 :   cache *S = &caches[id];
    2098             :   /* cache_H is compressed: D=0,1 mod 4 */
    2099   143128743 :   const ulong d = (id == cache_H)? D>>1: D;
    2100             :   ulong max, l;
    2101             : 
    2102   143128743 :   if (!S->cache)
    2103             :   {
    2104         182 :     max = maxuu(minuu(D, S->maxself), S->minself);
    2105         182 :     S->init(max);
    2106         182 :     l = lg(S->cache);
    2107             :   }
    2108             :   else
    2109             :   {
    2110   143128561 :     l = lg(S->cache);
    2111   143128561 :     if (l <= d)
    2112             :     {
    2113         987 :       if (D > S->maxmiss) S->maxmiss = D;
    2114         987 :       if (DEBUGLEVEL >= 3)
    2115           0 :         err_printf("miss in cache %s: %lu, max = %lu\n",
    2116             :                    S->name, D, S->maxmiss);
    2117         987 :       if (S->miss++ >= 5 && D < S->maxself)
    2118             :       {
    2119          84 :         max = minuu(S->maxself, (long)(S->maxmiss * 1.2));
    2120          84 :         if (max <= S->maxself)
    2121             :         {
    2122          84 :           if (DEBUGLEVEL >= 3)
    2123           0 :             err_printf("resetting cache %s to %lu\n", S->name, max);
    2124          84 :           S->init(max); l = lg(S->cache);
    2125             :         }
    2126             :       }
    2127             :     }
    2128             :   }
    2129   143128743 :   return (l <= d)? NULL: gel(S->cache, d);
    2130             : }
    2131             : static GEN
    2132          70 : cache_report(long id)
    2133             : {
    2134          70 :   cache *S = &caches[id];
    2135          70 :   GEN v = zerocol(5);
    2136          70 :   gel(v,1) = strtoGENstr(S->name);
    2137          70 :   if (S->cache)
    2138             :   {
    2139          35 :     gel(v,2) = utoi(lg(S->cache)-1);
    2140          35 :     gel(v,3) = utoi(S->miss);
    2141          35 :     gel(v,4) = utoi(S->maxmiss);
    2142          35 :     gel(v,5) = utoi(gsizebyte(S->cache));
    2143             :   }
    2144          70 :   return v;
    2145             : }
    2146             : GEN
    2147          14 : getcache(void)
    2148             : {
    2149          14 :   pari_sp av = avma;
    2150          14 :   GEN M = cgetg(6, t_MAT);
    2151          14 :   gel(M,1) = cache_report(cache_FACT);
    2152          14 :   gel(M,2) = cache_report(cache_DIV);
    2153          14 :   gel(M,3) = cache_report(cache_H);
    2154          14 :   gel(M,4) = cache_report(cache_D);
    2155          14 :   gel(M,5) = cache_report(cache_DIH);
    2156          14 :   return gerepilecopy(av, shallowtrans(M));
    2157             : }
    2158             : 
    2159             : void
    2160        1542 : pari_close_mf(void)
    2161             : {
    2162        1542 :   cache_delete(cache_DIH);
    2163        1542 :   cache_delete(cache_DIV);
    2164        1542 :   cache_delete(cache_FACT);
    2165        1542 :   cache_delete(cache_H);
    2166        1542 : }
    2167             : 
    2168             : /*************************************************************************/
    2169             : /* a odd, update local cache (recycle memory) */
    2170             : static GEN
    2171        1885 : update_factor_cache(long a, long lim, long *pb)
    2172             : {
    2173        1885 :   const long step = 16000; /* even; don't increase this: RAM cache thrashing */
    2174        1885 :   if (a + 2*step > lim)
    2175         189 :     *pb = lim; /* fuse last 2 chunks */
    2176             :   else
    2177        1696 :     *pb = a + step;
    2178        1885 :   return vecfactoroddu_i(a, *pb);
    2179             : }
    2180             : /* assume lim < MAX_LONG/8 */
    2181             : static void
    2182          63 : constcoredisc(long lim)
    2183             : {
    2184          63 :   pari_sp av2, av = avma;
    2185          63 :   GEN D = caches[cache_D].cache, CACHE = NULL;
    2186          63 :   long cachea, cacheb, N, LIM = !D ? 4 : lg(D)-1;
    2187          63 :   if (lim <= 0) lim = 5;
    2188          63 :   if (lim <= LIM) return;
    2189          63 :   cache_reset(cache_D);
    2190          63 :   D = zero_zv(lim);
    2191          63 :   av2 = avma;
    2192          63 :   cachea = cacheb = 0;
    2193     7553322 :   for (N = 1; N <= lim; N+=2)
    2194             :   { /* N odd */
    2195             :     long i, d, d2;
    2196             :     GEN F;
    2197     7553259 :     if (N > cacheb)
    2198             :     {
    2199         924 :       set_avma(av2); cachea = N;
    2200         924 :       CACHE = update_factor_cache(N, lim, &cacheb);
    2201             :     }
    2202     7553259 :     F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2203     7553259 :     D[N] = d = corediscs_fact(F); /* = 3 mod 4 or 4 mod 16 */
    2204     7553259 :     d2 = odd(d)? d<<3: d<<1;
    2205     7553259 :     for (i = 1;;)
    2206             :     {
    2207    12588751 :       if ((N << i) > lim) break;
    2208     5035484 :       D[N<<i] = d2; i++;
    2209     5035484 :       if ((N << i) > lim) break;
    2210     2517746 :       D[N<<i] = d; i++;
    2211             :     }
    2212             :   }
    2213          63 :   cache_set(cache_D, D);
    2214          63 :   set_avma(av);
    2215             : }
    2216             : 
    2217             : static void
    2218          70 : constfact(long lim)
    2219             : {
    2220             :   pari_sp av;
    2221          70 :   GEN VFACT = caches[cache_FACT].cache;
    2222          70 :   long LIM = VFACT? lg(VFACT)-1: 4;
    2223          70 :   if (lim <= 0) lim = 5;
    2224          70 :   if (lim <= LIM) return;
    2225          63 :   cache_reset(cache_FACT); av = avma;
    2226          63 :   cache_set(cache_FACT, vecfactoru_i(1,lim)); set_avma(av);
    2227             : }
    2228             : static void
    2229          63 : constdiv(long lim)
    2230             : {
    2231             :   pari_sp av;
    2232          63 :   GEN VFACT, VDIV = caches[cache_DIV].cache;
    2233          63 :   long N, LIM = VDIV? lg(VDIV)-1: 4;
    2234          63 :   if (lim <= 0) lim = 5;
    2235          63 :   if (lim <= LIM) return;
    2236          63 :   constfact(lim);
    2237          63 :   VFACT = caches[cache_FACT].cache;
    2238          63 :   cache_reset(cache_DIV); av = avma;
    2239          63 :   VDIV  = cgetg(lim+1, t_VEC);
    2240          63 :   for (N = 1; N <= lim; N++) gel(VDIV,N) = divisorsu_fact(gel(VFACT,N));
    2241          63 :   cache_set(cache_DIV, VDIV); set_avma(av);
    2242             : }
    2243             : 
    2244             : /* n > 1, D = divisors(n); sets L = 2*lambda(n), S = sigma(n) */
    2245             : static void
    2246     9517628 : lamsig(GEN D, long *pL, long *pS)
    2247             : {
    2248     9517628 :   pari_sp av = avma;
    2249     9517628 :   long i, l = lg(D), L = 1, S = D[l-1]+1;
    2250    34382155 :   for (i = 2; i < l; i++) /* skip d = 1 */
    2251             :   {
    2252    34382155 :     long d = D[i], nd = D[l-i]; /* nd = n/d */
    2253    34382155 :     if (d < nd) { L += d; S += d + nd; }
    2254             :     else
    2255             :     {
    2256     9517628 :       L <<= 1; if (d == nd) { L += d; S += d; }
    2257     9517628 :       break;
    2258             :     }
    2259             :   }
    2260     9517628 :   set_avma(av); *pL = L; *pS = S;
    2261     9517628 : }
    2262             : /* table of 6 * Hurwitz class numbers D <= lim */
    2263             : static void
    2264         126 : consttabh(long lim)
    2265             : {
    2266         126 :   pari_sp av = avma, av2;
    2267         126 :   GEN VHDH0, VDIV, CACHE = NULL;
    2268         126 :   GEN VHDH = caches[cache_H].cache;
    2269         126 :   long r, N, cachea, cacheb, lim0 = VHDH? lg(VHDH)-1: 2, LIM = lim0 << 1;
    2270             : 
    2271         126 :   if (lim <= 0) lim = 5;
    2272         126 :   if (lim <= LIM) return;
    2273         126 :   cache_reset(cache_H);
    2274         126 :   r = lim&3L; if (r) lim += 4-r;
    2275         126 :   cache_get(cache_DIV, lim);
    2276         126 :   VDIV = caches[cache_DIV].cache;
    2277         126 :   VHDH0 = cgetg(lim/2 + 1, t_VECSMALL);
    2278         126 :   VHDH0[1] = 2;
    2279         126 :   VHDH0[2] = 3;
    2280         126 :   for (N = 3; N <= lim0; N++) VHDH0[N] = VHDH[N];
    2281         126 :   av2 = avma;
    2282         126 :   cachea = cacheb = 0;
    2283     4758940 :   for (N = LIM + 3; N <= lim; N += 4)
    2284             :   {
    2285     4758814 :     long s = 0, limt = usqrt(N>>2), flsq = 0, ind, t, L, S;
    2286             :     GEN DN, DN2;
    2287     4758814 :     if (N + 2 >= lg(VDIV))
    2288             :     { /* use local cache */
    2289             :       GEN F;
    2290     3971440 :       if (N + 2 > cacheb)
    2291             :       {
    2292         961 :         set_avma(av2); cachea = N;
    2293         961 :         CACHE = update_factor_cache(N, lim+2, &cacheb);
    2294             :       }
    2295     3971440 :       F = gel(CACHE, ((N-cachea)>>1)+1); /* factoru(N) */
    2296     3971440 :       DN = divisorsu_fact(F);
    2297     3971440 :       F = gel(CACHE, ((N-cachea)>>1)+2); /* factoru(N+2) */
    2298     3971440 :       DN2 = divisorsu_fact(F);
    2299             :     }
    2300             :     else
    2301             :     { /* use global cache */
    2302      787374 :       DN = gel(VDIV,N);
    2303      787374 :       DN2 = gel(VDIV,N+2);
    2304             :     }
    2305     4758814 :     ind = N >> 1;
    2306  1059809401 :     for (t = 1; t <= limt; t++)
    2307             :     {
    2308  1055050587 :       ind -= (t<<2)-2; /* N/2 - 2t^2 */
    2309  1055050587 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2310             :     }
    2311     4758814 :     lamsig(DN, &L,&S);
    2312     4758814 :     VHDH0[N >> 1] = 2*S - 3*L - 2*s + flsq;
    2313     4758814 :     s = 0; flsq = 0; limt = (usqrt(N+2) - 1) >> 1;
    2314     4758814 :     ind = (N+1) >> 1;
    2315  1057451335 :     for (t = 1; t <= limt; t++)
    2316             :     {
    2317  1052692521 :       ind -= t<<2; /* (N+1)/2 - 2t(t+1) */
    2318  1052692521 :       if (ind) s += VHDH0[ind]; else flsq = 1;
    2319             :     }
    2320     4758814 :     lamsig(DN2, &L,&S);
    2321     4758814 :     VHDH0[(N+1) >> 1] = S - 3*(L >> 1) - s - flsq;
    2322             :   }
    2323         126 :   cache_set(cache_H, VHDH0); set_avma(av);
    2324             : }
    2325             : 
    2326             : /*************************************************************************/
    2327             : /* Core functions using factorizations, divisors of class numbers caches */
    2328             : /* TODO: myfactoru and factorization cache should be exported */
    2329             : static GEN
    2330    15424199 : myfactoru(long N)
    2331             : {
    2332    15424199 :   GEN z = cache_get(cache_FACT, N);
    2333    15424199 :   return z? gcopy(z): factoru(N);
    2334             : }
    2335             : static GEN
    2336    37832067 : mydivisorsu(long N)
    2337             : {
    2338    37832067 :   GEN z = cache_get(cache_DIV, N);
    2339    37832067 :   return z? leafcopy(z): divisorsu(N);
    2340             : }
    2341             : /* write -n = Df^2, D < 0 fundamental discriminant. Return D, set f. */
    2342             : static long
    2343    48318641 : mycoredisc2neg(ulong n, long *pf)
    2344             : {
    2345    48318641 :   ulong m, D = (ulong)cache_get(cache_D, n);
    2346    48318641 :   if (D) { *pf = usqrt(n/D); return -(long)D; }
    2347         196 :   m = mycore(n, pf);
    2348         196 :   if ((m&3) != 3) { m <<= 2; *pf >>= 1; }
    2349         196 :   return (long)-m;
    2350             : }
    2351             : /* write n = Df^2, D > 0 fundamental discriminant. Return D, set f. */
    2352             : static long
    2353          14 : mycoredisc2pos(ulong n, long *pf)
    2354             : {
    2355          14 :   ulong m = mycore(n, pf);
    2356          14 :   if ((m&3) != 1) { m <<= 2; *pf >>= 1; }
    2357          14 :   return (long)m;
    2358             : }
    2359             : 
    2360             : /* 1+p+...+p^e, e >= 1 */
    2361             : static ulong
    2362          49 : usumpow(ulong p, long e)
    2363             : {
    2364          49 :   ulong q = 1+p;
    2365             :   long i;
    2366          49 :   for (i = 1; i < e; i++) q = p*q + 1;
    2367          49 :   return q;
    2368             : }
    2369             : /* Hurwitz(D0 F^2)/ Hurwitz(D0)
    2370             :  * = \sum_{f|F}  f \prod_{p|f} (1-kro(D0/p)/p)
    2371             :  * = \prod_{p^e || F} (1 + (p^e-1) / (p-1) * (p-kro(D0/p))) */
    2372             : static long
    2373         294 : get_sh(long F, long D0)
    2374             : {
    2375         294 :   GEN fa = myfactoru(F), P = gel(fa,1), E = gel(fa,2);
    2376         294 :   long i, l = lg(P), t = 1;
    2377         794 :   for (i = 1; i < l; i++)
    2378             :   {
    2379         500 :     long p = P[i], e = E[i], s = kross(D0,p);
    2380         500 :     if (e == 1) { t *= 1 + p - s; continue; }
    2381         153 :     if (s == 1) { t *= upowuu(p,e); continue; }
    2382          49 :     t *= 1 + usumpow(p,e-1)*(p-s);
    2383             :   }
    2384         294 :   return t;
    2385             : }
    2386             : /* d > 0, d = 0,3 (mod 4). Return 6*hclassno(d); -d must be fundamental
    2387             :  * Faster than quadclassunit up to 5*10^5 or so */
    2388             : static ulong
    2389          42 : hclassno6u_count(ulong d)
    2390             : {
    2391          42 :   ulong a, b, b2, h = 0;
    2392          42 :   int f = 0;
    2393             : 
    2394          42 :   if (d > 500000)
    2395           7 :     return 6 * itou(gel(quadclassunit0(utoineg(d), 0, NULL, 0), 1));
    2396             : 
    2397             :   /* this part would work with -d non fundamental */
    2398          35 :   b = d&1; b2 = (1+d)>>2;
    2399          35 :   if (!b)
    2400             :   {
    2401           0 :     for (a=1; a*a<b2; a++)
    2402           0 :       if (b2%a == 0) h++;
    2403           0 :     f = (a*a==b2); b=2; b2=(4+d)>>2;
    2404             :   }
    2405        7168 :   while (b2*3 < d)
    2406             :   {
    2407        7098 :     if (b2%b == 0) h++;
    2408     1188551 :     for (a=b+1; a*a < b2; a++)
    2409     1181453 :       if (b2%a == 0) h += 2;
    2410        7098 :     if (a*a == b2) h++;
    2411        7098 :     b += 2; b2 = (b*b+d)>>2;
    2412             :   }
    2413          35 :   if (b2*3 == d) return 6*h+2;
    2414          35 :   if (f) return 6*h+3;
    2415          35 :   return 6*h;
    2416             : }
    2417             : /* D > 0; 6 * hclassno(D), using D = D0*F^2 */
    2418             : static long
    2419         336 : hclassno6u_2(ulong D, long D0, long F)
    2420             : {
    2421             :   long h;
    2422         336 :   if (F == 1) h = hclassno6u_count(D);
    2423             :   else
    2424             :   { /* second chance */
    2425         294 :     h = (ulong)cache_get(cache_H, -D0);
    2426         294 :     if (!h) h = hclassno6u_count(-D0);
    2427         294 :     h *= get_sh(F,D0);
    2428             :   }
    2429         336 :   return h;
    2430             : }
    2431             : /* D > 0; 6 * hclassno(D) (6*Hurwitz). Beware, cached value for D (=0,3 mod 4)
    2432             :  * is stored at D>>1 */
    2433             : ulong
    2434      155745 : hclassno6u(ulong D)
    2435             : {
    2436      155745 :   ulong z = (ulong)cache_get(cache_H, D);
    2437             :   long D0, F;
    2438      155745 :   if (z) return z;
    2439         336 :   D0 = mycoredisc2neg(D, &F);
    2440         336 :   return hclassno6u_2(D,D0,F);
    2441             : }
    2442             : /* same, where the decomposition D = D0*F^2 is already known */
    2443             : static ulong
    2444    36572746 : hclassno6u_i(ulong D, long D0, long F)
    2445             : {
    2446    36572746 :   ulong z = (ulong)cache_get(cache_H, D);
    2447    36572746 :   if (z) return z;
    2448           0 :   return hclassno6u_2(D,D0,F);
    2449             : }
    2450             : 
    2451             : #if 0
    2452             : /* D > 0, return h(-D) [ordinary class number].
    2453             :  * Assume consttabh(D or more) was previously called */
    2454             : static long
    2455             : hfromH(long D)
    2456             : {
    2457             :   pari_sp ltop = avma;
    2458             :   GEN m, d, fa = myfactoru(D), P = gel(fa,1), E = gel(fa,2);
    2459             :   GEN VH = caches[cache_H].cache;
    2460             :   long i, nd, S, l = lg(P);
    2461             : 
    2462             :   /* n = d[i] loops through squarefree divisors of f, where f^2 = largest square
    2463             :    * divisor of N = |D|; m[i] = moebius(n) */
    2464             :   nd = 1 << (l-1);
    2465             :   d = cgetg(nd+1, t_VECSMALL);
    2466             :   m = cgetg(nd+1, t_VECSMALL);
    2467             :   d[1] = 1; S = VH[D >> 1]; /* 6 hclassno(-D) */
    2468             :   m[1] = 1; nd = 1;
    2469             :   i = 1;
    2470             :   if (P[1] == 2 && E[1] <= 3) /* need D/n^2 to be a discriminant */
    2471             :   { if (odd(E[1]) || (E[1] == 2 && (D & 15) == 4)) i = 2; }
    2472             :   for (; i<l; i++)
    2473             :   {
    2474             :     long j, p = P[i];
    2475             :     if (E[i] == 1) continue;
    2476             :     for (j=1; j<=nd; j++)
    2477             :     {
    2478             :       long n, s, hn;
    2479             :       d[nd+j] = n = d[j] * p;
    2480             :       m[nd+j] = s = - m[j]; /* moebius(n) */
    2481             :       hn = VH[(D/(n*n)) >> 1]; /* 6 hclassno(-D/n^2) */
    2482             :       if (s > 0) S += hn; else S -= hn;
    2483             :     }
    2484             :     nd <<= 1;
    2485             :   }
    2486             :   return gc_long(ltop, S/6);
    2487             : }
    2488             : #endif
    2489             : /* D < -4 fundamental, h(D), ordinary class number */
    2490             : static long
    2491     4801496 : myh(long D)
    2492             : {
    2493     4801496 :   ulong z = (ulong)cache_get(cache_H, -D);
    2494     4801496 :   if (z) return z/6; /* should be hfromH(-D) if D non-fundamental */
    2495           0 :   return itou(quadclassno(stoi(D)));
    2496             : }
    2497             : 
    2498             : /*************************************************************************/
    2499             : /*                          TRACE FORMULAS                               */
    2500             : /* CHIP primitive, initialize for t_POLMOD output */
    2501             : static GEN
    2502       24927 : mfcharinit(GEN CHIP)
    2503             : {
    2504       24927 :   long n, o, l, vt, N = mfcharmodulus(CHIP);
    2505             :   GEN c, v, V, G, Pn;
    2506       24927 :   if (N == 1) return mkvec2(mkvec(gen_1), pol_x(0));
    2507        3997 :   G = gel(CHIP,1);
    2508        3997 :   v = ncharvecexpo(G, znconrey_normalized(G, gel(CHIP,2)));
    2509        3997 :   l = lg(v); V = cgetg(l, t_VEC);
    2510        3997 :   o = mfcharorder(CHIP);
    2511        3997 :   Pn = mfcharpol(CHIP); vt = varn(Pn);
    2512        3997 :   if (o <= 2)
    2513             :   {
    2514       29176 :     for (n = 1; n < l; n++)
    2515             :     {
    2516       26103 :       if (v[n] < 0) c = gen_0; else c = v[n]? gen_m1: gen_1;
    2517       26103 :       gel(V,n) = c;
    2518             :     }
    2519             :   }
    2520             :   else
    2521             :   {
    2522       16639 :     for (n = 1; n < l; n++)
    2523             :     {
    2524       15715 :       if (v[n] < 0) c = gen_0;
    2525             :       else
    2526             :       {
    2527        8750 :         c = mygmodulo_lift(v[n], o, gen_1, vt);
    2528        8750 :         if (typ(c) == t_POL && lg(c) >= lg(Pn)) c = RgX_rem(c, Pn);
    2529             :       }
    2530       15715 :       gel(V,n) = c;
    2531             :     }
    2532             :   }
    2533        3997 :   return mkvec2(V, Pn);
    2534             : }
    2535             : static GEN
    2536      334768 : vchip_lift(GEN VCHI, long x, GEN C)
    2537             : {
    2538      334768 :   GEN V = gel(VCHI,1);
    2539      334768 :   long F = lg(V)-1;
    2540      334768 :   if (F == 1) return C;
    2541       26068 :   x %= F;
    2542       26068 :   if (!x) return C;
    2543       26068 :   if (x <= 0) x += F;
    2544       26068 :   return gmul(C, gel(V, x));
    2545             : }
    2546             : static long
    2547    79536779 : vchip_FC(GEN VCHI) { return lg(gel(VCHI,1))-1; }
    2548             : static GEN
    2549     3258297 : vchip_mod(GEN VCHI, GEN S)
    2550     3258297 : { return (typ(S) == t_POL)? RgX_rem(S, gel(VCHI,2)): S; }
    2551             : static GEN
    2552      983703 : vchip_polmod(GEN VCHI, GEN S)
    2553      983703 : { return (typ(S) == t_POL)? mkpolmod(S, gel(VCHI,2)): S; }
    2554             : 
    2555             : /* ceil(m/d) */
    2556             : static long
    2557      106169 : ceildiv(long m, long d)
    2558             : {
    2559             :   long q;
    2560      106169 :   if (!m) return 0;
    2561       37352 :   q = m/d; return m%d? q+1: q;
    2562             : }
    2563             : 
    2564             : /* contribution of scalar matrices in dimension formula */
    2565             : static GEN
    2566      228347 : A1(long N, long k)
    2567      228347 : { return sstoQ(mypsiu(N)*(k-1), 12); }
    2568             : static long
    2569        7231 : ceilA1(long N, long k)
    2570        7231 : { return ceildiv(mypsiu(N) * (k-1), 12); }
    2571             : 
    2572             : /* sturm bound, slightly larger than dimension */
    2573             : long
    2574       28917 : mfsturmNk(long N, long k) { return 1 + (mypsiu(N)*k)/12; }
    2575             : long
    2576        1925 : mfsturmNgk(long N, GEN k)
    2577             : {
    2578        1925 :   long n,d; Qtoss(k,&n,&d);
    2579        1925 :   return (d == 1)? mfsturmNk(N,n): 1 + (mypsiu(N)*n)/24;
    2580             : }
    2581             : 
    2582             : /* List of all solutions of x^2 + x + 1 = 0 modulo N, x modulo N */
    2583             : static GEN
    2584         504 : sqrtm3modN(long N)
    2585             : {
    2586             :   pari_sp av;
    2587             :   GEN fa, P, E, B, mB, A, Q, T, R, v, gen_m3;
    2588         504 :   long l, i, n, ct, fl3 = 0, Ninit;
    2589         504 :   if (!odd(N) || (N%9) == 0) return cgetg(1,t_VECSMALL);
    2590         476 :   Ninit = N;
    2591         476 :   if ((N%3) == 0) { N /= 3; fl3 = 1; }
    2592         476 :   fa = myfactoru(N); P = gel(fa, 1); E = gel(fa, 2);
    2593         476 :   l = lg(P);
    2594         665 :   for (i = 1; i < l; i++)
    2595         483 :     if ((P[i]%3) == 2) return cgetg(1,t_VECSMALL);
    2596         182 :   A = cgetg(l, t_VECSMALL);
    2597         182 :   B = cgetg(l, t_VECSMALL);
    2598         182 :   mB= cgetg(l, t_VECSMALL);
    2599         182 :   Q = cgetg(l, t_VECSMALL); gen_m3 = utoineg(3);
    2600         371 :   for (i = 1; i < l; i++)
    2601             :   {
    2602         189 :     long p = P[i], e = E[i];
    2603         189 :     Q[i] = upowuu(p,e);
    2604         189 :     B[i] = itou( Zp_sqrt(gen_m3, utoipos(p), e) );
    2605         189 :     mB[i]= Q[i] - B[i];
    2606             :   }
    2607         182 :   ct = 1 << (l-1);
    2608         182 :   T = ZV_producttree(Q);
    2609         182 :   R = ZV_chinesetree(Q,T);
    2610         182 :   v = cgetg(ct+1, t_VECSMALL);
    2611         182 :   av = avma;
    2612         560 :   for (n = 1; n <= ct; n++)
    2613             :   {
    2614         378 :     long m = n-1, r;
    2615         784 :     for (i = 1; i < l; i++)
    2616             :     {
    2617         406 :       A[i] = (m&1L)? mB[i]: B[i];
    2618         406 :       m >>= 1;
    2619             :     }
    2620         378 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2621         378 :     if (fl3) while (r%3) r += N;
    2622         378 :     set_avma(av); v[n] = odd(r) ? (r-1) >> 1 : (r+Ninit-1) >> 1;
    2623             :   }
    2624         182 :   return v;
    2625             : }
    2626             : 
    2627             : /* number of elliptic points of order 3 in X0(N) */
    2628             : static long
    2629        9240 : nu3(long N)
    2630             : {
    2631             :   long i, l;
    2632             :   GEN P;
    2633        9240 :   if (!odd(N) || (N%9) == 0) return 0;
    2634        8246 :   if ((N%3) == 0) N /= 3;
    2635        8246 :   P = gel(myfactoru(N), 1); l = lg(P);
    2636        8246 :   for (i = 1; i < l; i++) if ((P[i]%3) == 2) return 0;
    2637        3619 :   return 1L<<(l-1);
    2638             : }
    2639             : /* number of elliptic points of order 2 in X0(N) */
    2640             : static long
    2641       16065 : nu2(long N)
    2642             : {
    2643             :   long i, l;
    2644             :   GEN P;
    2645       16065 :   if ((N&3L) == 0) return 0;
    2646       16065 :   if (!odd(N)) N >>= 1;
    2647       16065 :   P = gel(myfactoru(N), 1); l = lg(P);
    2648       16065 :   for (i = 1; i < l; i++) if ((P[i]&3L) == 3) return 0;
    2649        3661 :   return 1L<<(l-1);
    2650             : }
    2651             : 
    2652             : /* contribution of elliptic matrices of order 3 in dimension formula
    2653             :  * Only depends on CHIP the primitive char attached to CHI */
    2654             : static GEN
    2655       39613 : A21(long N, long k, GEN CHI)
    2656             : {
    2657             :   GEN res, G, chi, o;
    2658             :   long a21, i, limx, S;
    2659       39613 :   if ((N&1L) == 0) return gen_0;
    2660       19264 :   a21 = k%3 - 1;
    2661       19264 :   if (!a21) return gen_0;
    2662       18620 :   if (N <= 3) return sstoQ(a21, 3);
    2663        9744 :   if (!CHI) return sstoQ(nu3(N) * a21, 3);
    2664         504 :   res = sqrtm3modN(N); limx = (N - 1) >> 1;
    2665         504 :   G = gel(CHI,1); chi = gel(CHI,2);
    2666         504 :   o = gmfcharorder(CHI);
    2667         882 :   for (S = 0, i = 1; i < lg(res); i++)
    2668             :   { /* (x,N) = 1; S += chi(x) + chi(x^2) */
    2669         378 :     long x = res[i];
    2670         378 :     if (x <= limx)
    2671             :     { /* CHI(x)=e(c/o), 3rd-root of 1 */
    2672         189 :       GEN c = znchareval(G, chi, utoi(x), o);
    2673         189 :       if (!signe(c)) S += 2; else S--;
    2674             :     }
    2675             :   }
    2676         504 :   return sstoQ(a21 * S, 3);
    2677             : }
    2678             : 
    2679             : /* List of all square roots of -1 modulo N */
    2680             : static GEN
    2681         567 : sqrtm1modN(long N)
    2682             : {
    2683             :   pari_sp av;
    2684             :   GEN fa, P, E, B, mB, A, Q, T, R, v;
    2685         567 :   long l, i, n, ct, fleven = 0;
    2686         567 :   if ((N&3L) == 0) return cgetg(1,t_VECSMALL);
    2687         567 :   if ((N&1L) == 0) { N >>= 1; fleven = 1; }
    2688         567 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    2689         567 :   l = lg(P);
    2690         917 :   for (i = 1; i < l; i++)
    2691         637 :     if ((P[i]&3L) == 3) return cgetg(1,t_VECSMALL);
    2692         280 :   A = cgetg(l, t_VECSMALL);
    2693         280 :   B = cgetg(l, t_VECSMALL);
    2694         280 :   mB= cgetg(l, t_VECSMALL);
    2695         280 :   Q = cgetg(l, t_VECSMALL);
    2696         574 :   for (i = 1; i < l; i++)
    2697             :   {
    2698         294 :     long p = P[i], e = E[i];
    2699         294 :     Q[i] = upowuu(p,e);
    2700         294 :     B[i] = itou( Zp_sqrt(gen_m1, utoipos(p), e) );
    2701         294 :     mB[i]= Q[i] - B[i];
    2702             :   }
    2703         280 :   ct = 1 << (l-1);
    2704         280 :   T = ZV_producttree(Q);
    2705         280 :   R = ZV_chinesetree(Q,T);
    2706         280 :   v = cgetg(ct+1, t_VECSMALL);
    2707         280 :   av = avma;
    2708         868 :   for (n = 1; n <= ct; n++)
    2709             :   {
    2710         588 :     long m = n-1, r;
    2711        1232 :     for (i = 1; i < l; i++)
    2712             :     {
    2713         644 :       A[i] = (m&1L)? mB[i]: B[i];
    2714         644 :       m >>= 1;
    2715             :     }
    2716         588 :     r = itou( ZV_chinese_tree(A, Q, T, R) );
    2717         588 :     if (fleven && !odd(r)) r += N;
    2718         588 :     set_avma(av); v[n] = r;
    2719             :   }
    2720         280 :   return v;
    2721             : }
    2722             : 
    2723             : /* contribution of elliptic matrices of order 4 in dimension formula.
    2724             :  * Only depends on CHIP the primitive char attached to CHI */
    2725             : static GEN
    2726       39613 : A22(long N, long k, GEN CHI)
    2727             : {
    2728             :   GEN G, chi, o, res;
    2729             :   long S, a22, i, limx, o2;
    2730       39613 :   if ((N&3L) == 0) return gen_0;
    2731       27650 :   a22 = (k & 3L) - 1; /* (k % 4) - 1 */
    2732       27650 :   if (!a22) return gen_0;
    2733       27650 :   if (N <= 2) return sstoQ(a22, 4);
    2734       16835 :   if (!CHI) return sstoQ(nu2(N)*a22, 4);
    2735         770 :   if (mfcharparity(CHI) == -1) return gen_0;
    2736         567 :   res = sqrtm1modN(N); limx = (N - 1) >> 1;
    2737         567 :   G = gel(CHI,1); chi = gel(CHI,2);
    2738         567 :   o = gmfcharorder(CHI);
    2739         567 :   o2 = itou(o)>>1;
    2740        1155 :   for (S = 0, i = 1; i < lg(res); i++)
    2741             :   { /* (x,N) = 1, S += real(chi(x)) */
    2742         588 :     long x = res[i];
    2743         588 :     if (x <= limx)
    2744             :     { /* CHI(x)=e(c/o), 4th-root of 1 */
    2745         294 :       long c = itou( znchareval(G, chi, utoi(x), o) );
    2746         294 :       if (!c) S++; else if (c == o2) S--;
    2747             :     }
    2748             :   }
    2749         567 :   return sstoQ(a22 * S, 2);
    2750             : }
    2751             : 
    2752             : /* sumdiv(N,d,eulerphi(gcd(d,N/d))) */
    2753             : static long
    2754       35672 : nuinf(long N)
    2755             : {
    2756       35672 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    2757       35672 :   long i, t = 1, l = lg(P);
    2758       75873 :   for (i=1; i<l; i++)
    2759             :   {
    2760       40201 :     long p = P[i], e = E[i];
    2761       40201 :     if (odd(e))
    2762       32249 :       t *= upowuu(p,e>>1) << 1;
    2763             :     else
    2764        7952 :       t *= upowuu(p,(e>>1)-1) * (p+1);
    2765             :   }
    2766       35672 :   return t;
    2767             : }
    2768             : 
    2769             : /* contribution of hyperbolic matrices in dimension formula */
    2770             : static GEN
    2771       40012 : A3(long N, long FC)
    2772             : {
    2773             :   long i, S, NF, l;
    2774             :   GEN D;
    2775       40012 :   if (FC == 1) return sstoQ(nuinf(N),2);
    2776        4340 :   D = mydivisorsu(N); l = lg(D);
    2777        4340 :   S = 0; NF = N/FC;
    2778       33173 :   for (i = 1; i < l; i++)
    2779             :   {
    2780       28833 :     long g = ugcd(D[i], D[l-i]);
    2781       28833 :     if (NF%g == 0) S += myeulerphiu(g);
    2782             :   }
    2783        4340 :   return sstoQ(S, 2);
    2784             : }
    2785             : 
    2786             : /* special contribution in weight 2 in dimension formula */
    2787             : static long
    2788       39277 : A4(long k, long FC)
    2789       39277 : { return (k==2 && FC==1)? 1: 0; }
    2790             : /* gcd(x,N) */
    2791             : static long
    2792    94470355 : myugcd(GEN GCD, ulong x)
    2793             : {
    2794    94470355 :   ulong N = lg(GCD)-1;
    2795    94470355 :   if (x >= N) x %= N;
    2796    94470355 :   return GCD[x+1];
    2797             : }
    2798             : /* 1_{gcd(x,N) = 1} * chi(x), return NULL if 0 */
    2799             : static GEN
    2800   127774045 : mychicgcd(GEN GCD, GEN VCHI, long x)
    2801             : {
    2802   127774045 :   long N = lg(GCD)-1;
    2803   127774045 :   if (N == 1) return gen_1;
    2804   110229749 :   x = smodss(x, N);
    2805   110229749 :   if (GCD[x+1] != 1) return NULL;
    2806    76139798 :   x %= vchip_FC(VCHI); if (!x) return gen_1;
    2807     6634362 :   return gel(gel(VCHI,1), x);
    2808             : }
    2809             : 
    2810             : /* contribution of scalar matrices to trace formula */
    2811             : static GEN
    2812     3103289 : TA1(long N, long k, GEN VCHI, GEN GCD, long n)
    2813             : {
    2814             :   GEN S;
    2815             :   ulong m;
    2816     3103289 :   if (!uissquareall(n, &m)) return gen_0;
    2817      244965 :   if (m == 1) return A1(N,k); /* common */
    2818      215033 :   S = mychicgcd(GCD, VCHI, m);
    2819      215033 :   return S? gmul(gmul(powuu(m, k-2), A1(N,k)), S): gen_0;
    2820             : }
    2821             : 
    2822             : /* All square roots modulo 4N, x modulo 2N, precomputed to accelerate TA2 */
    2823             : static GEN
    2824       98847 : mksqr(long N)
    2825             : {
    2826       98847 :   pari_sp av = avma;
    2827       98847 :   long x, N2 = N << 1, N4 = N << 2;
    2828       98847 :   GEN v = const_vec(N2, cgetg(1, t_VECSMALL));
    2829       98847 :   gel(v, N2) = mkvecsmall(0); /* x = 0 */
    2830     2272872 :   for (x = 1; x <= N; x++)
    2831             :   {
    2832     2174025 :     long r = (((x*x - 1)%N4) >> 1) + 1;
    2833     2174025 :     gel(v,r) = vecsmall_append(gel(v,r), x);
    2834             :   }
    2835       98847 :   return gerepilecopy(av, v);
    2836             : }
    2837             : 
    2838             : static GEN
    2839       98847 : mkgcd(long N)
    2840             : {
    2841             :   GEN GCD, d;
    2842             :   long i, N2;
    2843       98847 :   if (N == 1) return mkvecsmall(N);
    2844       81760 :   GCD = cgetg(N + 1, t_VECSMALL);
    2845       81760 :   d = GCD+1; /* GCD[i+1] = d[i] = gcd(i,N) = gcd(N-i,N), i = 0..N-1 */
    2846       81760 :   d[0] = N; d[1] = d[N-1] = 1; N2 = N>>1;
    2847       81760 :   for (i = 2; i <= N2; i++) d[i] = d[N-i] = ugcd(N, i);
    2848       81760 :   return GCD;
    2849             : }
    2850             : 
    2851             : /* Table of \sum_{x^2-tx+n=0 mod Ng}chi(x) for all g dividing gcd(N,F),
    2852             :  * F^2 largest such that (t^2-4n)/F^2=0 or 1 mod 4; t >= 0 */
    2853             : static GEN
    2854     9676415 : mutglistall(long t, long N, long NF, GEN VCHI, long n, GEN MUP, GEN li, GEN GCD)
    2855             : {
    2856     9676415 :   long i, lx = lg(li);
    2857     9676415 :   GEN DNF = mydivisorsu(NF), v = zerovec(NF);
    2858     9676415 :   long j, g, lDNF = lg(DNF);
    2859    26514558 :   for (i = 1; i < lx; i++)
    2860             :   {
    2861    16838143 :     long x = (li[i] + t) >> 1, y, lD;
    2862    16838143 :     GEN D, c = mychicgcd(GCD, VCHI, x);
    2863    16838143 :     if (li[i] && li[i] != N)
    2864             :     {
    2865    11106200 :       GEN c2 = mychicgcd(GCD, VCHI, t - x);
    2866    11106200 :       if (c2) c = c? gadd(c, c2): c2;
    2867             :     }
    2868    16838143 :     if (!c) continue;
    2869     9834531 :     y = (x*(x - t) + n) / N; /* exact division */
    2870     9834531 :     D = mydivisorsu(ugcd(labs(y), NF)); lD = lg(D);
    2871     9834531 :     for (j=1; j < lD; j++) { g = D[j]; gel(v,g) = gadd(gel(v,g), c); }
    2872             :   }
    2873             :   /* j = 1 corresponds to g = 1, and MUP[1] = 1 */
    2874     9676415 :   for (j=2; j < lDNF; j++) { g = DNF[j]; gel(v,g) = gmulsg(MUP[g], gel(v,g)); }
    2875     9676415 :   return v;
    2876             : }
    2877             : 
    2878             : /* special case (N,F) = 1: easier */
    2879             : static GEN
    2880    38641876 : mutg1(long t, long N, GEN VCHI, GEN li, GEN GCD)
    2881             : { /* (N,F) = 1 */
    2882    38641876 :   GEN S = NULL;
    2883    38641876 :   long i, lx = lg(li);
    2884    81959969 :   for (i = 1; i < lx; i++)
    2885             :   {
    2886    43318093 :     long x = (li[i] + t) >> 1;
    2887    43318093 :     GEN c = mychicgcd(GCD, VCHI, x);
    2888    43318093 :     if (c) S = S? gadd(S, c): c;
    2889    43318093 :     if (li[i] && li[i] != N)
    2890             :     {
    2891    25072719 :       c = mychicgcd(GCD, VCHI, t - x);
    2892    25072719 :       if (c) S = S? gadd(S, c): c;
    2893             :     }
    2894    43318093 :     if (S && !signe(S)) S = NULL; /* strive hard to add gen_0 */
    2895             :   }
    2896    38641876 :   return S; /* single value */
    2897             : }
    2898             : 
    2899             : /* Gegenbauer pol; n > 2, P = \sum_{0<=j<=n/2} (-1)^j (n-j)!/j!(n-2*j)! X^j */
    2900             : static GEN
    2901      340921 : mfrhopol(long n)
    2902             : {
    2903             : #ifdef LONG_IS_64BIT
    2904      292218 :   const long M = 2642249;
    2905             : #else
    2906       48703 :   const long M = 1629;
    2907             : #endif
    2908      340921 :   long j, d = n >> 1; /* >= 1 */
    2909      340921 :   GEN P = cgetg(d + 3, t_POL);
    2910             : 
    2911      340921 :   if (n > M) pari_err_IMPL("mfrhopol for large weight"); /* avoid overflow */
    2912      340921 :   P[1] = evalvarn(0)|evalsigne(1);
    2913      340921 :   gel(P,2) = gen_1;
    2914      340921 :   gel(P,3) = utoineg(n-1); /* j = 1 */
    2915      340921 :   if (d > 1) gel(P,4) = utoipos(((n-3)*(n-2)) >> 1); /* j = 2 */
    2916      340921 :   if (d > 2) gel(P,5) = utoineg(((n-5)*(n-4)*(n-3)) / 6); /* j = 3 */
    2917     1279754 :   for (j = 4; j <= d; j++)
    2918      938833 :     gel(P,j+2) = divis(mulis(gel(P,j+1), (n-2*j+1)*(n-2*j+2)), (n-j+1)*(-j));
    2919      340921 :   return P;
    2920             : }
    2921             : 
    2922             : /* polrecip(Q)(t2), assume Q(0) = 1 */
    2923             : static GEN
    2924     2883916 : ZXrecip_u_eval(GEN Q, ulong t2)
    2925             : {
    2926     2883916 :   GEN T = addiu(gel(Q,3), t2);
    2927     2883916 :   long l = lg(Q), j;
    2928     2883916 :   for (j = 4; j < l; j++) T = addii(gel(Q,j), mului(t2, T));
    2929     2883916 :   return T;
    2930             : }
    2931             : /* return sh * sqrt(n)^nu * G_nu(t/(2*sqrt(n))) for t != 0
    2932             :  * else (sh/2) * sqrt(n)^nu * G_nu(0) [ implies nu is even ]
    2933             :  * G_nu(z) = \sum_{0<=j<=nu/2} (-1)^j (nu-j)!/j!(nu-2*j)! * (2z)^(nu-2*j)) */
    2934             : static GEN
    2935    41892767 : mfrhopowsimp(GEN Q, GEN sh, long nu, long t, long t2, long n)
    2936             : {
    2937             :   GEN T;
    2938    41892767 :   switch (nu)
    2939             :   {
    2940    34114150 :     case 0: return t? sh: gmul2n(sh,-1);
    2941     3462326 :     case 1: return gmulsg(t, sh);
    2942     1394197 :     case 2: return t? gmulsg(t2 - n, sh): gmul(gmul2n(stoi(-n), -1), sh);
    2943         469 :     case 3: return gmul(mulss(t, t2 - 2*n), sh);
    2944             :     default:
    2945     2921625 :       if (!t) return gmul(gmul2n(gel(Q, lg(Q) - 1), -1), sh);
    2946     2883916 :       T = ZXrecip_u_eval(Q, t2); if (odd(nu)) T = mulsi(t, T);
    2947     2883916 :       return gmul(T, sh);
    2948             :   }
    2949             : }
    2950             : 
    2951             : /* contribution of elliptic matrices to trace formula */
    2952             : static GEN
    2953     3103289 : TA2(long N, long k, GEN VCHI, long n, GEN SQRTS, GEN MUP, GEN GCD)
    2954             : {
    2955     3103289 :   const long n4 = n << 2, N4 = N << 2, nu = k - 2;
    2956     3103289 :   const long st = (!odd(N) && odd(n)) ? 2 : 1;
    2957             :   long limt, t;
    2958             :   GEN S, Q;
    2959             : 
    2960     3103289 :   limt = usqrt(n4);
    2961     3103289 :   if (limt*limt == n4) limt--;
    2962     3103289 :   Q = nu > 3 ? ZX_z_unscale(mfrhopol(nu), n) : NULL;
    2963     3103289 :   S = gen_0;
    2964    86048452 :   for (t = odd(k)? st: 0; t <= limt; t += st) /* t^2 < 4n */
    2965             :   {
    2966    82945163 :     pari_sp av = avma;
    2967    82945163 :     long t2 = t*t, D = n4 - t2, F, D0, NF;
    2968             :     GEN sh, li;
    2969             : 
    2970    82945163 :     li = gel(SQRTS, (smodss(-D - 1, N4) >> 1) + 1);
    2971   123997559 :     if (lg(li) == 1) continue;
    2972    48318291 :     D0 = mycoredisc2neg(D, &F);
    2973    48318291 :     NF = myugcd(GCD, F);
    2974    48318291 :     if (NF == 1)
    2975             :     { /* (N,F) = 1 => single value in mutglistall */
    2976    38641876 :       GEN mut = mutg1(t, N, VCHI, li, GCD);
    2977    38641876 :       if (!mut) { set_avma(av); continue; }
    2978    36572746 :       sh = gmul(sstoQ(hclassno6u_i(D,D0,F),6), mut);
    2979             :     }
    2980             :     else
    2981             :     {
    2982     9676415 :       GEN v = mutglistall(t, N, NF, VCHI, n, MUP, li, GCD);
    2983     9676415 :       GEN DF = mydivisorsu(F);
    2984     9676415 :       long i, lDF = lg(DF);
    2985     9676415 :       sh = gen_0;
    2986    37413313 :       for (i = 1; i < lDF; i++)
    2987             :       {
    2988    27736898 :         long Ff, f = DF[i], g = myugcd(GCD, f);
    2989    27736898 :         GEN mut = gel(v, g);
    2990    27736898 :         if (gequal0(mut)) continue;
    2991    12847583 :         Ff = DF[lDF-i]; /* F/f */
    2992    12847583 :         if (Ff == 1) sh = gadd(sh, mut);
    2993             :         else
    2994             :         {
    2995     9211083 :           GEN P = gel(myfactoru(Ff), 1);
    2996     9211083 :           long j, lP = lg(P);
    2997     9211083 :           for (j = 1; j < lP; j++) { long p = P[j]; Ff -= kross(D0, p)*Ff/p; }
    2998     9211083 :           sh = gadd(sh, gmulsg(Ff, mut));
    2999             :         }
    3000             :       }
    3001     9676415 :       if (gequal0(sh)) { set_avma(av); continue; }
    3002     5320021 :       if (D0 == -3) sh = gdivgs(sh, 3);
    3003     5060916 :       else if (D0 == -4) sh = gdivgs(sh, 2);
    3004     4801496 :       else sh = gmulgs(sh, myh(D0));
    3005             :     }
    3006    41892767 :     S = gerepileupto(av, gadd(S, mfrhopowsimp(Q,sh,nu,t,t2,n)));
    3007             :   }
    3008     3103289 :   return S;
    3009             : }
    3010             : 
    3011             : /* compute global auxiliary data for TA3 */
    3012             : static GEN
    3013       98847 : mkbez(long N, long FC)
    3014             : {
    3015       98847 :   long ct, i, NF = N/FC;
    3016       98847 :   GEN w, D = mydivisorsu(N);
    3017       98847 :   long l = lg(D);
    3018             : 
    3019       98847 :   w = cgetg(l, t_VEC);
    3020      285257 :   for (i = ct = 1; i < l; i++)
    3021             :   {
    3022      268170 :     long u, v, h, c = D[i], Nc = D[l-i];
    3023      268170 :     if (c > Nc) break;
    3024      186410 :     h = cbezout(c, Nc, &u, &v);
    3025      186410 :     if (h == 1) /* shortcut */
    3026      136339 :       gel(w, ct++) = mkvecsmall4(1,u*c,1,i);
    3027       50071 :     else if (!(NF%h))
    3028       44415 :       gel(w, ct++) = mkvecsmall4(h,u*(c/h),myeulerphiu(h),i);
    3029             :   }
    3030       98847 :   setlg(w,ct); stackdummy((pari_sp)(w+ct),(pari_sp)(w+l));
    3031       98847 :   return w;
    3032             : }
    3033             : 
    3034             : /* contribution of hyperbolic matrices to trace formula, d * nd = n,
    3035             :  * DN = divisorsu(N) */
    3036             : static GEN
    3037    12256286 : auxsum(GEN VCHI, GEN GCD, long d, long nd, GEN DN, GEN BEZ)
    3038             : {
    3039    12256286 :   GEN S = gen_0;
    3040    12256286 :   long ct, g = nd - d, lDN = lg(DN), lBEZ = lg(BEZ);
    3041    34348552 :   for (ct = 1; ct < lBEZ; ct++)
    3042             :   {
    3043    22092266 :     GEN y, B = gel(BEZ, ct);
    3044    22092266 :     long ic, c, Nc, uch, h = B[1];
    3045    22092266 :     if (g%h) continue;
    3046    21623980 :     uch = B[2];
    3047    21623980 :     ic  = B[4];
    3048    21623980 :     c = DN[ic];
    3049    21623980 :     Nc= DN[lDN - ic]; /* Nc = N/c */
    3050    21623980 :     if (ugcd(Nc, nd) == 1)
    3051    16051868 :       y = mychicgcd(GCD, VCHI, d + uch*g); /* 0 if (c,d) > 1 */
    3052             :     else
    3053     5572112 :       y = NULL;
    3054    21623980 :     if (c != Nc && ugcd(Nc, d) == 1)
    3055             :     {
    3056    15171989 :       GEN y2 = mychicgcd(GCD, VCHI, nd - uch*g); /* 0 if (c,nd) > 1 */
    3057    15171989 :       if (y2) y = y? gadd(y, y2): y2;
    3058             :     }
    3059    21623980 :     if (y) S = gadd(S, gmulsg(B[3], y));
    3060             :   }
    3061    12256286 :   return S;
    3062             : }
    3063             : 
    3064             : static GEN
    3065     3103289 : TA3(long N, long k, GEN VCHI, GEN GCD, GEN Dn, GEN BEZ)
    3066             : {
    3067     3103289 :   GEN S = gen_0, DN = mydivisorsu(N);
    3068     3103289 :   long i, l = lg(Dn);
    3069    15359575 :   for (i = 1; i < l; i++)
    3070             :   {
    3071    15329643 :     long d = Dn[i], nd = Dn[l-i]; /* = n/d */
    3072             :     GEN t, u;
    3073    15329643 :     if (d > nd) break;
    3074    12256286 :     t = auxsum(VCHI, GCD, d, nd, DN, BEZ);
    3075    12256286 :     if (isintzero(t)) continue;
    3076    11163355 :     u = powuu(d,k-1); if (d == nd) u = gmul2n(u,-1);
    3077    11163355 :     S = gadd(S, gmul(u,t));
    3078             :   }
    3079     3103289 :   return S;
    3080             : }
    3081             : 
    3082             : /* special contribution in weight 2 in trace formula */
    3083             : static long
    3084     3103289 : TA4(long k, GEN VCHIP, GEN Dn, GEN GCD)
    3085             : {
    3086             :   long i, l, S;
    3087     3103289 :   if (k != 2 || vchip_FC(VCHIP) != 1) return 0;
    3088     2427152 :   l = lg(Dn); S = 0;
    3089    20842318 :   for (i = 1; i < l; i++)
    3090             :   {
    3091    18415166 :     long d = Dn[i]; /* gcd(N,n/d) == 1? */
    3092    18415166 :     if (myugcd(GCD, Dn[l-i]) == 1) S += d;
    3093             :   }
    3094     2427152 :   return S;
    3095             : }
    3096             : 
    3097             : /* precomputation of products occurring im mutg, again to accelerate TA2 */
    3098             : static GEN
    3099       98847 : mkmup(long N)
    3100             : {
    3101       98847 :   GEN fa = myfactoru(N), P = gel(fa,1), D = divisorsu_fact(fa);
    3102       98847 :   long i, lP = lg(P), lD = lg(D);
    3103       98847 :   GEN MUP = zero_zv(N);
    3104       98847 :   MUP[1] = 1;
    3105      344022 :   for (i = 2; i < lD; i++)
    3106             :   {
    3107      245175 :     long j, g = D[i], Ng = D[lD-i]; /*  N/g */
    3108      245175 :     for (j = 1; j < lP; j++) { long p = P[j]; if (Ng%p) g += g/p; }
    3109      245175 :     MUP[D[i]] = g;
    3110             :   }
    3111       98847 :   return MUP;
    3112             : }
    3113             : 
    3114             : /* quadratic non-residues mod p; p odd prime, p^2 fits in a long */
    3115             : static GEN
    3116        1400 : non_residues(long p)
    3117             : {
    3118        1400 :   long i, j, p2 = p >> 1;
    3119        1400 :   GEN v = cgetg(p2+1, t_VECSMALL), w = const_vecsmall(p-1, 1);
    3120        1400 :   for (i = 2; i <= p2; i++) w[(i*i) % p] = 0; /* no need to check 1 */
    3121        1400 :   for (i = 2, j = 1; i < p; i++) if (w[i]) v[j++] = i;
    3122        1400 :   return v;
    3123             : }
    3124             : 
    3125             : /* CHIP primitive. Return t_VECSMALL v of length q such that
    3126             :  * Tr^new_{N,CHIP}(n) = 0 whenever v[(n%q) + 1] is non-zero */
    3127             : static GEN
    3128       24955 : mfnewzerodata(long N, GEN CHIP)
    3129             : {
    3130       24955 :   GEN V, M, L, faN = myfactoru(N), PN = gel(faN,1), EN = gel(faN,2);
    3131       24955 :   GEN G = gel(CHIP,1), chi = gel(CHIP,2);
    3132       24955 :   GEN fa = znstar_get_faN(G), P = ZV_to_zv(gel(fa,1)), E = gel(fa,2);
    3133       24955 :   long i, mod, j = 1, l = lg(PN);
    3134             : 
    3135       24955 :   M = cgetg(l, t_VECSMALL); M[1] = 0;
    3136       24955 :   V = cgetg(l, t_VEC);
    3137             :   /* Tr^new(n) = 0 if (n mod M[i]) in V[i]  */
    3138       24955 :   if ((N & 3) == 0)
    3139             :   {
    3140        9541 :     long e = EN[1];
    3141        9541 :     long c = (lg(P) > 1 && P[1] == 2)? E[1]: 0; /* c = v_2(FC) */
    3142             :     /* e >= 2 */
    3143        9541 :     if (c == e-1) return NULL; /* Tr^new = 0 */
    3144        9506 :     if (c == e)
    3145             :     {
    3146        2422 :       if (e == 2)
    3147             :       { /* sc: -4 */
    3148        1652 :         gel(V,1) = mkvecsmall(3);
    3149        1652 :         M[1] = 4;
    3150             :       }
    3151         770 :       else if (e == 3)
    3152             :       { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3153         770 :         long t = signe(gel(chi,1))? 7: 3;
    3154         770 :         gel(V,1) = mkvecsmall2(5, t);
    3155         770 :         M[1] = 8;
    3156             :       }
    3157             :     }
    3158        7084 :     else if (e == 5 && c == 3)
    3159         154 :     { /* sc: -8 (CHI_2(-1)=-1<=>chi[1]=1) and 8 (CHI_2(-1)=1 <=> chi[1]=0) */
    3160         154 :       long t = signe(gel(chi,1))? 7: 3;
    3161         154 :       gel(V,1) = mkvecsmalln(6, 2L,4L,5L,6L,8L,t);
    3162         154 :       M[1] = 8;
    3163             :     }
    3164        6930 :     else if ((e == 4 && c == 2) || (e == 5 && c <= 2) || (e == 6 && c <= 2)
    3165        5719 :          || (e >= 7 && c == e - 3))
    3166             :     { /* sc: 4 */
    3167        1211 :       gel(V,1) = mkvecsmall3(0,2,3);
    3168        1211 :       M[1] = 4;
    3169             :     }
    3170        5719 :     else if ((e <= 4 && c == 0) || (e >= 5 && c == e - 2))
    3171             :     { /* sc: 2 */
    3172        5453 :       gel(V,1) = mkvecsmall(0);
    3173        5453 :       M[1] = 2;
    3174             :     }
    3175         266 :     else if ((e == 6 && c == 3) || (e >= 7 && c <= e - 4))
    3176             :     { /* sc: -2 */
    3177         266 :       gel(V,1) = mkvecsmalln(7, 0L,2L,3L,4L,5L,6L,7L);
    3178         266 :       M[1] = 8;
    3179             :     }
    3180             :   }
    3181       24920 :   j = M[1]? 2: 1;
    3182       53508 :   for (i = odd(N)? 1: 2; i < l; i++) /* skip p=2, done above */
    3183             :   {
    3184       28588 :     long p = PN[i], e = EN[i];
    3185       28588 :     long z = zv_search(P, p), c = z? E[z]: 0; /* c = v_p(FC) */
    3186       28588 :     if ((e <= 2 && c == 1 && itos(gel(chi,z)) == (p>>1)) /* ord(CHI_p)=2 */
    3187       27650 :         || (e >= 3 && c <= e - 2))
    3188        1400 :     { /* sc: -p */
    3189        1400 :       GEN v = non_residues(p);
    3190        1400 :       if (e != 1) v = vecsmall_prepend(v, 0);
    3191        1400 :       gel(V,j) = v;
    3192        1400 :       M[j] = p; j++;
    3193             :     }
    3194       27188 :     else if (e >= 2 && c < e)
    3195             :     { /* sc: p */
    3196        1827 :       gel(V,j) = mkvecsmall(0);
    3197        1827 :       M[j] = p; j++;
    3198             :     }
    3199             :   }
    3200       24920 :   if (j == 1) return cgetg(1, t_VECSMALL);
    3201       11207 :   setlg(V,j); setlg(M,j); mod = zv_prod(M);
    3202       11207 :   L = zero_zv(mod);
    3203       23940 :   for (i = 1; i < j; i++)
    3204             :   {
    3205       12733 :     GEN v = gel(V,i);
    3206       12733 :     long s, m = M[i], lv = lg(v);
    3207       33152 :     for (s = 1; s < lv; s++)
    3208             :     {
    3209       20419 :       long a = v[s] + 1;
    3210       30205 :       do { L[a] = 1; a += m; } while (a <= mod);
    3211             :     }
    3212             :   }
    3213       11207 :   return L;
    3214             : }
    3215             : /* v=mfnewzerodata(N,CHI); returns TRUE if newtrace(n) must be zero,
    3216             :  * (but newtrace(n) may still be zero if we return FALSE) */
    3217             : static long
    3218     1296834 : mfnewchkzero(GEN v, long n) { long q = lg(v)-1; return q && v[(n%q) + 1]; }
    3219             : 
    3220             : /* if (!VCHIP): from mftraceform_cusp;
    3221             :  * else from initnewtrace and CHI is known to be primitive */
    3222             : static GEN
    3223       98847 : inittrace(long N, GEN CHI, GEN VCHIP)
    3224             : {
    3225             :   long FC;
    3226       98847 :   if (VCHIP)
    3227       98840 :     FC = mfcharmodulus(CHI);
    3228             :   else
    3229           7 :     VCHIP = mfcharinit(mfchartoprimitive(CHI, &FC));
    3230       98847 :   return mkvecn(5, mksqr(N), mkmup(N), mkgcd(N), VCHIP, mkbez(N, FC));
    3231             : }
    3232             : 
    3233             : /* p > 2 prime; return a sorted t_VECSMALL of primes s.t Tr^new(p) = 0 for all
    3234             :  * weights > 2 */
    3235             : static GEN
    3236       24920 : inittrconj(long N, long FC)
    3237             : {
    3238             :   GEN fa, P, E, v;
    3239             :   long i, k, l;
    3240             : 
    3241       24920 :   if (FC != 1) return cgetg(1,t_VECSMALL);
    3242             : 
    3243       20923 :   fa = myfactoru(N >> vals(N));
    3244       20923 :   P = gel(fa,1); l = lg(P);
    3245       20923 :   E = gel(fa,2);
    3246       20923 :   v = cgetg(l, t_VECSMALL);
    3247       45850 :   for (i = k = 1; i < l; i++)
    3248             :   {
    3249       24927 :     long j, p = P[i]; /* > 2 */
    3250       60606 :     for (j = 1; j < l; j++)
    3251       35679 :       if (j != i && E[j] == 1 && kross(-p, P[j]) == 1) v[k++] = p;
    3252             :   }
    3253       20923 :   setlg(v,k); return v;
    3254             : }
    3255             : 
    3256             : /* assume CHIP primitive, f(CHIP) | N; NZ = mfnewzerodata(N,CHIP) */
    3257             : static GEN
    3258       24920 : initnewtrace_i(long N, GEN CHIP, GEN NZ)
    3259             : {
    3260       24920 :   GEN T = const_vec(N, cgetg(1,t_VEC)), D, VCHIP;
    3261       24920 :   long FC = mfcharmodulus(CHIP), N1, N2, i, l;
    3262             : 
    3263       24920 :   if (!NZ) NZ = mkvecsmall(1); /*Tr^new = 0; initialize data nevertheless*/
    3264       24920 :   VCHIP = mfcharinit(CHIP);
    3265       24920 :   N1 = N/FC; newd_params(N1, &N2);
    3266       24920 :   D = mydivisorsu(N1/N2); l = lg(D);
    3267       24920 :   N2 *= FC;
    3268      123760 :   for (i = 1; i < l; i++)
    3269             :   {
    3270       98840 :     long M = D[i]*N2;
    3271       98840 :     gel(T,M) = inittrace(M, CHIP, VCHIP);
    3272             :   }
    3273       24920 :   gel(T,N) = shallowconcat(gel(T,N), mkvec2(NZ, inittrconj(N,FC)));
    3274       24920 :   return T;
    3275             : }
    3276             : /* don't initialize if Tr^new = 0, return NULL */
    3277             : static GEN
    3278       24955 : initnewtrace(long N, GEN CHI)
    3279             : {
    3280       24955 :   GEN CHIP = mfchartoprimitive(CHI, NULL), NZ = mfnewzerodata(N,CHIP);
    3281       24955 :   return NZ? initnewtrace_i(N, CHIP, NZ): NULL;
    3282             : }
    3283             : 
    3284             : /* (-1)^k */
    3285             : static long
    3286        6895 : m1pk(long k) { return odd(k)? -1 : 1; }
    3287             : static long
    3288        6650 : badchar(long N, long k, GEN CHI)
    3289        6650 : { return mfcharparity(CHI) != m1pk(k) || (CHI && N % mfcharconductor(CHI)); }
    3290             : 
    3291             : /* dimension of space of cusp forms S_k(\G_0(N),CHI)
    3292             :  * Only depends on CHIP the primitive char attached to CHI */
    3293             : long
    3294       39228 : mfcuspdim(long N, long k, GEN CHI)
    3295             : {
    3296       39228 :   pari_sp av = avma;
    3297             :   long FC;
    3298             :   GEN s;
    3299       39228 :   if (k <= 0) return 0;
    3300       39228 :   if (k == 1) return mfwt1cuspdim(N, CHI);
    3301       39095 :   FC = CHI? mfcharconductor(CHI): 1;
    3302       39095 :   if (FC == 1) CHI = NULL;
    3303       39095 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3304       39095 :   s = gadd(s, gsubsg(A4(k, FC), A3(N, FC)));
    3305       39095 :   return gc_long(av, itos(s));
    3306             : }
    3307             : 
    3308             : /* dimension of whole space M_k(\G_0(N),CHI)
    3309             :  * Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3310             : long
    3311         686 : mffulldim(long N, long k, GEN CHI)
    3312             : {
    3313         686 :   pari_sp av = avma;
    3314         686 :   long FC = CHI? mfcharconductor(CHI): 1;
    3315             :   GEN s;
    3316         686 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3317         686 :   if (k == 1) return gc_long(av, itos(A3(N, FC)) + mfwt1cuspdim(N, CHI));
    3318         518 :   if (FC == 1) CHI = NULL;
    3319         518 :   s = gsub(A1(N, k), gadd(A21(N, k, CHI), A22(N, k, CHI)));
    3320         518 :   s = gadd(s, A3(N, FC));
    3321         518 :   return gc_long(av, itos(s));
    3322             : }
    3323             : 
    3324             : /* Dimension of the space of Eisenstein series */
    3325             : long
    3326         231 : mfeisensteindim(long N, long k, GEN CHI)
    3327             : {
    3328         231 :   pari_sp av = avma;
    3329         231 :   long s, FC = CHI? mfcharconductor(CHI): 1;
    3330         231 :   if (k <= 0) return (k == 0 && FC == 1)? 1: 0;
    3331         231 :   s = itos(gmul2n(A3(N, FC), 1));
    3332         231 :   if (k > 1) s -= A4(k, FC); else s >>= 1;
    3333         231 :   return gc_long(av,s);
    3334             : }
    3335             : 
    3336             : enum { _SQRTS = 1, _MUP, _GCD, _VCHIP, _BEZ, _NEWLZ, _TRCONJ };
    3337             : /* Trace of T(n) on space of cuspforms; only depends on CHIP the primitive char
    3338             :  * attached to CHI */
    3339             : static GEN
    3340     3103289 : mfcusptrace_i(long N, long k, long n, GEN Dn, GEN S)
    3341             : {
    3342     3103289 :   pari_sp av = avma;
    3343             :   GEN a, b, VCHIP, GCD;
    3344             :   long t;
    3345     3103289 :   if (!n) return gen_0;
    3346     3103289 :   VCHIP = gel(S,_VCHIP);
    3347     3103289 :   GCD = gel(S,_GCD);
    3348     3103289 :   t = TA4(k, VCHIP, Dn, GCD);
    3349     3103289 :   a = TA1(N, k, VCHIP, GCD, n); if (t) a = gaddgs(a,t);
    3350     3103289 :   b = TA2(N, k, VCHIP, n, gel(S,_SQRTS), gel(S,_MUP), GCD);
    3351     3103289 :   b = gadd(b, TA3(N, k, VCHIP, GCD, Dn, gel(S,_BEZ)));
    3352     3103289 :   b = gsub(a,b);
    3353     3103289 :   if (typ(b) != t_POL) return gerepileupto(av, b);
    3354       32816 :   return gerepilecopy(av, vchip_polmod(VCHIP, b));
    3355             : }
    3356             : 
    3357             : static GEN
    3358     4136930 : mfcusptracecache(long N, long k, long n, GEN Dn, GEN S, cachenew_t *cache)
    3359             : {
    3360     4136930 :   GEN C = NULL, T = gel(cache->vfull,N);
    3361     4136930 :   long lcache = lg(T);
    3362     4136930 :   if (n < lcache) C = gel(T, n);
    3363     4136930 :   if (C) cache->cuspHIT++; else C = mfcusptrace_i(N, k, n, Dn, S);
    3364     4136930 :   cache->cuspTOTAL++;
    3365     4136930 :   if (n < lcache) gel(T,n) = C;
    3366     4136930 :   return C;
    3367             : }
    3368             : 
    3369             : /* return the divisors of n, known to be among the elements of D */
    3370             : static GEN
    3371      288246 : div_restrict(GEN D, ulong n)
    3372             : {
    3373             :   long i, j, l;
    3374      288246 :   GEN v, VDIV = caches[cache_DIV].cache;
    3375      288246 :   if (lg(VDIV) > n) return gel(VDIV,n);
    3376           0 :   l = lg(D);
    3377           0 :   v = cgetg(l, t_VECSMALL);
    3378           0 :   for (i = j = 1; i < l; i++)
    3379             :   {
    3380           0 :     ulong d = D[i];
    3381           0 :     if (n % d == 0) v[j++] = d;
    3382             :   }
    3383           0 :   setlg(v,j); return v;
    3384             : }
    3385             : 
    3386             : /* for some prime divisors of N, Tr^new(p) = 0 */
    3387             : static int
    3388      219317 : trconj(GEN T, long N, long n)
    3389      219317 : { return (lg(T) > 1 && N % n == 0 && zv_search(T, n)); }
    3390             : 
    3391             : /* n > 0; trace formula on new space */
    3392             : static GEN
    3393     1296834 : mfnewtrace_i(long N, long k, long n, cachenew_t *cache)
    3394             : {
    3395     1296834 :   GEN VCHIP, s, Dn, DN1, SN, S = cache->DATA;
    3396             :   long FC, N1, N2, N1N2, g, i, j, lDN1;
    3397             : 
    3398     1296834 :   if (!S) return gen_0;
    3399     1296834 :   SN = gel(S,N);
    3400     1296834 :   if (mfnewchkzero(gel(SN,_NEWLZ), n)) return gen_0;
    3401      950922 :   if (k > 2 && trconj(gel(SN,_TRCONJ), N, n)) return gen_0;
    3402      950887 :   VCHIP = gel(SN, _VCHIP); FC = vchip_FC(VCHIP);
    3403      950887 :   N1 = N/FC; newt_params(N1, n, FC, &g, &N2);
    3404      950887 :   N1N2 = N1/N2;
    3405      950887 :   DN1 = mydivisorsu(N1N2); lDN1 = lg(DN1);
    3406      950887 :   N2 *= FC;
    3407      950887 :   Dn = mydivisorsu(n); /* this one is probably out of cache */
    3408      950887 :   s = gmulsg(mubeta2(N1N2,n), mfcusptracecache(N2, k, n, Dn, gel(S,N2), cache));
    3409     3848684 :   for (i = 2; i < lDN1; i++)
    3410             :   { /* skip M1 = 1, done above */
    3411     2897797 :     long M1 = DN1[i], N1M1 = DN1[lDN1-i];
    3412     2897797 :     GEN Dg = mydivisorsu(ugcd(M1, g));
    3413     2897797 :     M1 *= N2;
    3414     2897797 :     s = gadd(s, gmulsg(mubeta2(N1M1,n),
    3415     2897797 :                        mfcusptracecache(M1, k, n, Dn, gel(S,M1), cache)));
    3416     3186043 :     for (j = 2; j < lg(Dg); j++) /* skip d = 1, done above */
    3417             :     {
    3418      288246 :       long d = Dg[j], ndd = n/(d*d), M = M1/d;
    3419      288246 :       GEN z = mulsi(mubeta2(N1M1,ndd), powuu(d,k-1)), C = vchip_lift(VCHIP,d,z);
    3420      288246 :       GEN Dndd = div_restrict(Dn, ndd);
    3421      288246 :       s = gadd(s, gmul(C, mfcusptracecache(M, k, ndd, Dndd, gel(S,M), cache)));
    3422             :     }
    3423     2897797 :     s = vchip_mod(VCHIP, s);
    3424             :   }
    3425      950887 :   return vchip_polmod(VCHIP, s);
    3426             : }
    3427             : 
    3428             : /* mfcuspdim(N,k,CHI) - mfnewdim(N,k,CHI); CHIP primitive (for efficiency) */
    3429             : static long
    3430        7378 : mfolddim_i(long N, long k, GEN CHIP)
    3431             : {
    3432        7378 :   long S, i, l, FC = mfcharmodulus(CHIP), N1 = N/FC, N2;
    3433             :   GEN D;
    3434        7378 :   newd_params(N1, &N2); /* will ensure mubeta != 0 */
    3435        7378 :   D = mydivisorsu(N1/N2); l = lg(D);
    3436        7378 :   N2 *= FC; S = 0;
    3437       29155 :   for (i = 2; i < l; i++)
    3438             :   {
    3439       21777 :     long M = D[l-i]*N2, d = mfcuspdim(M, k, CHIP);
    3440       21777 :     if (d) S -= mubeta(D[i]) * d;
    3441             :   }
    3442        7378 :   return S;
    3443             : }
    3444             : long
    3445         322 : mfolddim(long N, long k, GEN CHI)
    3446             : {
    3447         322 :   pari_sp av = avma;
    3448         322 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3449         322 :   return gc_long(av, mfolddim_i(N, k, CHIP));
    3450             : }
    3451             : /* Only depends on CHIP the primitive char attached to CHI; assumes !badchar */
    3452             : long
    3453       14385 : mfnewdim(long N, long k, GEN CHI)
    3454             : {
    3455             :   pari_sp av;
    3456             :   long S;
    3457       14385 :   GEN CHIP = mfchartoprimitive(CHI, NULL);
    3458       14385 :   S = mfcuspdim(N, k, CHIP); if (!S) return 0;
    3459        7042 :   av = avma; return gc_long(av, S - mfolddim_i(N, k, CHIP));
    3460             : }
    3461             : 
    3462             : /* trace form, given as closure */
    3463             : static GEN
    3464         896 : mftraceform_new(long N, long k, GEN CHI)
    3465             : {
    3466             :   GEN T;
    3467         896 :   if (k == 1) return initwt1newtrace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3468         875 :   T = initnewtrace(N,CHI); if (!T) return mftrivial();
    3469         875 :   return tag(t_MF_NEWTRACE, mkNK(N,k,CHI), T);
    3470             : }
    3471             : static GEN
    3472          14 : mftraceform_cusp(long N, long k, GEN CHI)
    3473             : {
    3474          14 :   if (k == 1) return initwt1trace(mfinit_Nkchi(N, 1, CHI, mf_CUSP, 0));
    3475           7 :   return tag(t_MF_TRACE, mkNK(N,k,CHI), inittrace(N,CHI,NULL));
    3476             : }
    3477             : static GEN
    3478          91 : mftraceform_i(GEN NK, long space)
    3479             : {
    3480             :   GEN CHI;
    3481             :   long N, k;
    3482          91 :   checkNK(NK, &N, &k, &CHI, 0);
    3483          91 :   if (!mfdim_Nkchi(N, k, CHI, space)) return mftrivial();
    3484          70 :   switch(space)
    3485             :   {
    3486          49 :     case mf_NEW: return mftraceform_new(N, k, CHI);
    3487          14 :     case mf_CUSP:return mftraceform_cusp(N, k, CHI);
    3488             :   }
    3489           7 :   pari_err_DOMAIN("mftraceform", "space", "=", utoi(space), NK);
    3490             :   return NULL;/*LCOV_EXCL_LINE*/
    3491             : }
    3492             : GEN
    3493          91 : mftraceform(GEN NK, long space)
    3494          91 : { pari_sp av = avma; return gerepilecopy(av, mftraceform_i(NK,space)); }
    3495             : 
    3496             : static GEN
    3497       14322 : hecke_data(long N, long n)
    3498       14322 : { return mkvecsmall3(n, u_ppo(n, N), N); }
    3499             : /* 1/2-integral weight */
    3500             : static GEN
    3501          84 : heckef2_data(long N, long n)
    3502             : {
    3503             :   ulong f, fN, fN2;
    3504          84 :   if (!uissquareall(n, &f)) return NULL;
    3505          77 :   fN = u_ppo(f, N); fN2 = fN*fN;
    3506          77 :   return mkvec2(myfactoru(fN), mkvecsmall4(n, N, fN2, n/fN2));
    3507             : }
    3508             : /* N = mf_get_N(F) or a multiple */
    3509             : static GEN
    3510       20902 : mfhecke_i(long n, long N, GEN F)
    3511             : {
    3512       20902 :   if (n == 1) return F;
    3513       14196 :   return tag2(t_MF_HECKE, mf_get_NK(F), hecke_data(N,n), F);
    3514             : }
    3515             : 
    3516             : GEN
    3517         105 : mfhecke(GEN mf, GEN F, long n)
    3518             : {
    3519         105 :   pari_sp av = avma;
    3520             :   GEN NK, CHI, gk, DATA;
    3521             :   long N, nk, dk;
    3522         105 :   mf = checkMF(mf);
    3523         105 :   if (!checkmf_i(F)) pari_err_TYPE("mfhecke",F);
    3524         105 :   if (n <= 0) pari_err_TYPE("mfhecke [n <= 0]", stoi(n));
    3525         105 :   if (n == 1) return gcopy(F);
    3526         105 :   gk = mf_get_gk(F);
    3527         105 :   Qtoss(gk,&nk,&dk);
    3528         105 :   CHI = mf_get_CHI(F);
    3529         105 :   N = MF_get_N(mf);
    3530         105 :   if (dk == 2)
    3531             :   {
    3532          77 :     DATA = heckef2_data(N,n);
    3533          77 :     if (!DATA) return mftrivial();
    3534             :   }
    3535             :   else
    3536          28 :     DATA = hecke_data(N,n);
    3537          98 :   NK = mkgNK(lcmii(stoi(N), mf_get_gN(F)), gk, CHI, mf_get_field(F));
    3538          98 :   return gerepilecopy(av, tag2(t_MF_HECKE, NK, DATA, F));
    3539             : }
    3540             : 
    3541             : /* form F given by closure, compute B(d)(F) as closure (q -> q^d) */
    3542             : static GEN
    3543       26810 : mfbd_i(GEN F, long d)
    3544             : {
    3545             :   GEN D, NK, gk, CHI;
    3546       26810 :   if (d == 1) return F;
    3547        9590 :   if (d <= 0) pari_err_TYPE("mfbd [d <= 0]", stoi(d));
    3548        9590 :   if (mf_get_type(F) != t_MF_BD) D = utoi(d);
    3549           7 :   else { D = mului(d, gel(F,3)); F = gel(F,2); }
    3550        9590 :   gk = mf_get_gk(F); CHI = mf_get_CHI(F);
    3551        9590 :   if (typ(gk) != t_INT) CHI = mfcharmul(CHI, get_mfchar(utoi(d << 2)));
    3552        9590 :   NK = mkgNK(muliu(mf_get_gN(F), d), gk, CHI, mf_get_field(F));
    3553        9590 :   return tag2(t_MF_BD, NK, F, D);
    3554             : }
    3555             : GEN
    3556          35 : mfbd(GEN F, long d)
    3557             : {
    3558          35 :   pari_sp av = avma;
    3559          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfbd",F);
    3560          35 :   return gerepilecopy(av, mfbd_i(F, d));
    3561             : }
    3562             : 
    3563             : /* CHI is a character defined modulo N4 */
    3564             : static GEN
    3565          98 : RgV_shimura(GEN V, long n, long D, long N4, long r, GEN CHI)
    3566             : {
    3567          98 :   GEN R, a0, Pn = mfcharpol(CHI);
    3568          98 :   long m, Da, ND, ord = mfcharorder(CHI), vt = varn(Pn), d4 = D & 3L;
    3569             : 
    3570          98 :   if (d4 == 2 || d4 == 3) D *= 4;
    3571          98 :   Da = labs(D); ND = N4*Da;
    3572          98 :   R = cgetg(n + 2, t_VEC);
    3573          98 :   a0 = gel(V, 1);
    3574          98 :   if (!gequal0(a0))
    3575             :   {
    3576           7 :     long D4 = D << 2;
    3577           7 :     GEN CHID = induceN(ulcm(mfcharmodulus(CHI), labs(D4)), CHI);
    3578           7 :     CHID = mfcharmul_i(CHID, induce(gel(CHID,1), stoi(D4)));
    3579           7 :     a0 = gmul(a0, charLFwtk(r, CHID, mfcharorder(CHID)));
    3580             :   }
    3581          98 :   if (odd(ND) && !odd(mfcharmodulus(CHI))) ND <<= 1;
    3582          98 :   gel(R, 1) = a0;
    3583         567 :   for (m = 1; m <= n; m++)
    3584             :   {
    3585         469 :     GEN Dm = mydivisorsu(u_ppo(m, ND)), S = gel(V, m*m + 1);
    3586         469 :     long i, l = lg(Dm);
    3587         770 :     for (i = 2; i < l; i++)
    3588             :     { /* (e,ND) = 1; skip i = 1: e = 1, done above */
    3589         301 :       long e = Dm[i], me = m / e;
    3590         301 :       long a = mfcharevalord(CHI, e, ord);
    3591         301 :       GEN c, C = powuu(e, r - 1);
    3592         301 :       if (kross(D, e) == -1) C = negi(C);
    3593         301 :       c = mygmodulo_lift(a, ord, C, vt);
    3594         301 :       S = gadd(S, gmul(c, gel(V, me*me + 1)));
    3595             :     }
    3596         469 :     gel(R, m+1) = S;
    3597             :   }
    3598          98 :   return degpol(Pn) > 1? gmodulo(R, Pn): R;
    3599             : }
    3600             : static GEN
    3601          28 : c_shimura(long n, GEN F, long D, GEN CHI)
    3602             : {
    3603          28 :   GEN v = mfcoefs_i(F, n*n, labs(D));
    3604          28 :   return RgV_shimura(v, n, D, mf_get_N(F)>>2, mf_get_r(F), CHI);
    3605             : }
    3606             : 
    3607             : static long
    3608          14 : mfisinkohnen(GEN mf, GEN F)
    3609             : {
    3610          14 :   GEN v, gk = MF_get_gk(mf), CHI = MF_get_CHI(mf);
    3611          14 :   long i, sb, eps, N4 = MF_get_N(mf) >> 2, r = MF_get_r(mf);
    3612          14 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
    3613          14 :   eps = N4 % mfcharconductor(CHI)? -1 : 1;
    3614          14 :   if (odd(r)) eps = -eps;
    3615          14 :   v = mfcoefs(F, sb, 1);
    3616         896 :   for (i = 0; i <= sb; i++)
    3617             :   {
    3618         882 :     long j = i & 3L;
    3619         882 :     if ((j == 2 || j == 2 + eps) && !gequal0(gel(v,i+1))) return 0;
    3620             :   }
    3621          14 :   return 1;
    3622             : }
    3623             : 
    3624             : static long
    3625          35 : mfshimura_space_cusp(GEN mf)
    3626             : {
    3627          35 :   long fl = 1, r = MF_get_r(mf), M = MF_get_N(mf) >> 2;
    3628          35 :   if (r == 1 && M >= 4)
    3629             :   {
    3630          14 :     GEN E = gel(myfactoru(M), 2);
    3631          14 :     long ma = vecsmall_max(E);
    3632          14 :     if (ma > 2 || (ma == 2 && !mfcharistrivial(MF_get_CHI(mf)))) fl = 0;
    3633             :   }
    3634          35 :   return fl;
    3635             : }
    3636             : 
    3637             : /* D is either a discriminant (not necessarily fundamental) with
    3638             :    sign(D)=(-1)^{k-1/2}*eps, or a positive squarefree integer t, which is then
    3639             :    transformed into a fundamental discriminant of the correct sign. */
    3640             : GEN
    3641          35 : mfshimura(GEN mf, GEN F, long D)
    3642             : {
    3643          35 :   pari_sp av = avma;
    3644             :   GEN gk, G, res, mf2, CHI, CHIP;
    3645          35 :   long M, r, space, cusp, N4, flagdisc = 0;
    3646          35 :   if (!checkmf_i(F)) pari_err_TYPE("mfshimura",F);
    3647          35 :   gk = mf_get_gk(F);
    3648          35 :   if (typ(gk) != t_FRAC) pari_err_TYPE("mfshimura [integral weight]", F);
    3649          35 :   r = MF_get_r(mf);
    3650          35 :   if (r <= 0) pari_err_DOMAIN("mfshimura", "weight", "<=", ghalf, gk);
    3651          35 :   N4 = MF_get_N(mf) >> 2; CHI = MF_get_CHI(mf);
    3652          35 :   CHIP = mfcharchiliftprim(CHI, N4);
    3653          35 :   if (!CHIP) CHIP = CHI;
    3654             :   else
    3655             :   {
    3656          35 :     long epsD = CHI == CHIP? D: -D, rd = D & 3L;
    3657          35 :     if (odd(r)) epsD = -epsD;
    3658          35 :     if (epsD > 0 && (rd == 0 || rd == 1)) flagdisc = 1;
    3659             :     else
    3660             :     {
    3661          14 :       if (D < 0 || !uissquarefree(D))
    3662           7 :         pari_err_TYPE("shimura [incorrect D]", stoi(D));
    3663           7 :       D = epsD;
    3664             :     }
    3665             :   }
    3666          28 :   M = N4;
    3667          28 :   cusp = mfiscuspidal(mf,F);
    3668          28 :   space = cusp && mfshimura_space_cusp(mf)? mf_CUSP : mf_FULL;
    3669          28 :   if (!cusp || !flagdisc || !mfisinkohnen(mf,F)) M <<= 1;
    3670          28 :   mf2 = mfinit_Nkchi(M, r << 1, mfcharpow(CHI, gen_2), space, 0);
    3671          28 :   G = c_shimura(mfsturm(mf2), F, D, CHIP);
    3672          28 :   res = mftobasis_i(mf2, G);
    3673             :   /* not mflinear(mf2,): we want lowest possible level */
    3674          28 :   G = mflinear(MF_get_basis(mf2), res);
    3675          28 :   return gerepilecopy(av, mkvec3(mf2, G, res));
    3676             : }
    3677             : 
    3678             : /* W ZabM (ZM if n = 1), a t_INT or NULL, b t_INT, ZXQ mod P or NULL.
    3679             :  * Write a/b = A/d with d t_INT and A Zab return [W,d,A,P] */
    3680             : static GEN
    3681        6587 : mkMinv(GEN W, GEN a, GEN b, GEN P)
    3682             : {
    3683        6587 :   GEN A = (b && typ(b) == t_POL)? Q_remove_denom(QXQ_inv(b,P), &b): NULL;
    3684        6587 :   if (a && b)
    3685             :   {
    3686         938 :     a = Qdivii(a,b);
    3687         938 :     if (typ(a) == t_INT) b = gen_1; else { b = gel(a,2); a = gel(a,1); }
    3688         938 :     if (is_pm1(a)) a = NULL;
    3689             :   }
    3690        6587 :   if (a) A = A? ZX_Z_mul(A,a): a; else if (!A) A = gen_1;
    3691        6587 :   if (!b) b = gen_1;
    3692        6587 :   if (!P) P = gen_0;
    3693        6587 :   return mkvec4(W,b,A,P);
    3694             : }
    3695             : /* M square invertible QabM, return [M',d], M*M' = d*Id */
    3696             : static GEN
    3697         441 : QabM_Minv(GEN M, GEN P, long n)
    3698             : {
    3699             :   GEN dW, W, dM;
    3700         441 :   M = Q_remove_denom(M, &dM);
    3701         441 :   W = P? ZabM_inv(liftpol_shallow(M), P, n, &dW): ZM_inv(M, &dW);
    3702         441 :   return mkMinv(W, dM, dW, P);
    3703             : }
    3704             : /* Simplified form of mfclean, after a QabM_indexrank: M a ZabM with full
    3705             :  * column rank and z = indexrank(M) is known */
    3706             : static GEN
    3707         805 : mfclean2(GEN M, GEN z, GEN P, long n)
    3708             : {
    3709         805 :   GEN d, Minv, y = gel(z,1), W = rowpermute(M, y);
    3710         805 :   W = P? ZabM_inv(liftpol_shallow(W), P, n, &d): ZM_inv(W, &d);
    3711         805 :   M = rowslice(M, 1, y[lg(y)-1]);
    3712         805 :   Minv = mkMinv(W, NULL, d, P);
    3713         805 :   return mkvec3(y, Minv, M);
    3714             : }
    3715             : /* M QabM, lg(M)>1 and [y,z] its rank profile. Let Minv be the inverse of the
    3716             :  * invertible square matrix in mkMinv format. Return [y,Minv, M[..y[#y],]]
    3717             :  * P cyclotomic polynomial of order n != 2 mod 4 or NULL */
    3718             : static GEN
    3719        4200 : mfclean(GEN M, GEN P, long n, int ratlift)
    3720             : {
    3721        4200 :   GEN W, v, y, z, d, Minv, dM, MdM = Q_remove_denom(M, &dM);
    3722        4200 :   if (n == 1)
    3723        3479 :     W = ZM_pseudoinv(MdM, &v, &d);
    3724             :   else
    3725         721 :     W = ZabM_pseudoinv_i(liftpol_shallow(MdM), P, n, &v, &d, ratlift);
    3726        4200 :   y = gel(v,1);
    3727        4200 :   z = gel(v,2);
    3728        4200 :   if (lg(z) != lg(MdM)) M = vecpermute(M,z);
    3729        4200 :   M = rowslice(M, 1, y[lg(y)-1]);
    3730        4200 :   Minv = mkMinv(W, dM, d, P);
    3731        4200 :   return mkvec3(y, Minv, M);
    3732             : }
    3733             : /* call mfclean using only CHI */
    3734             : static GEN
    3735        3409 : mfcleanCHI(GEN M, GEN CHI, int ratlift)
    3736             : {
    3737        3409 :   long n = mfcharorder_canon(CHI);
    3738        3409 :   GEN P = (n == 1)? NULL: mfcharpol(CHI);
    3739        3409 :   return mfclean(M, P, n, ratlift);
    3740             : }
    3741             : 
    3742             : /* DATA component of a t_MF_NEWTRACE. Was it stripped to save memory ? */
    3743             : static int
    3744       25816 : newtrace_stripped(GEN DATA)
    3745       25816 : { return DATA && (lg(DATA) == 5 && typ(gel(DATA,3)) == t_INT); }
    3746             : /* f a t_MF_NEWTRACE */
    3747             : static GEN
    3748       25816 : newtrace_DATA(long N, GEN f)
    3749             : {
    3750       25816 :   GEN DATA = gel(f,2);
    3751       25816 :   return newtrace_stripped(DATA)? initnewtrace(N, DATA): DATA;
    3752             : }
    3753             : /* reset cachenew for new level incorporating new DATA, tf a t_MF_NEWTRACE
    3754             :  * (+ possibly initialize 'full' for new allowed levels) */
    3755             : static void
    3756       25816 : reset_cachenew(cachenew_t *cache, long N, GEN tf)
    3757             : {
    3758             :   long i, n, l;
    3759       25816 :   GEN v, DATA = newtrace_DATA(N,tf);
    3760       25816 :   cache->DATA = DATA;
    3761       25816 :   if (!DATA) return;
    3762       25781 :   n = cache->n;
    3763       25781 :   v = cache->vfull; l = N+1; /* = lg(DATA) */
    3764     1387295 :   for (i = 1; i < l; i++)
    3765     1361514 :     if (typ(gel(v,i)) == t_INT && lg(gel(DATA,i)) != 1)
    3766       40719 :       gel(v,i) = const_vec(n, NULL);
    3767       25781 :   cache->VCHIP = gel(gel(DATA,N),_VCHIP);
    3768             : }
    3769             : /* initialize a cache of newtrace / cusptrace up to index n and level | N;
    3770             :  * DATA may be NULL (<=> Tr^new = 0). tf a t_MF_NEWTRACE */
    3771             : static void
    3772        9842 : init_cachenew(cachenew_t *cache, long n, long N, GEN tf)
    3773             : {
    3774        9842 :   long i, l = N+1; /* = lg(tf.DATA) when DATA != NULL */
    3775             :   GEN v;
    3776        9842 :   cache->n = n;
    3777        9842 :   cache->vnew = v = cgetg(l, t_VEC);
    3778        9842 :   for (i = 1; i < l; i++) gel(v,i) = (N % i)? gen_0: const_vec(n, NULL);
    3779        9842 :   cache->newHIT = cache->newTOTAL = cache->cuspHIT = cache->cuspTOTAL = 0;
    3780        9842 :   cache->vfull = v = zerovec(N);
    3781        9842 :   reset_cachenew(cache, N, tf);
    3782        9842 : }
    3783             : static void
    3784       14574 : dbg_cachenew(cachenew_t *C)
    3785             : {
    3786       14574 :   if (DEBUGLEVEL >= 2 && C)
    3787           0 :     err_printf("newtrace cache hits: new = %ld/%ld, cusp = %ld/%ld\n",
    3788             :                     C->newHIT, C->newTOTAL, C->cuspHIT, C->cuspTOTAL);
    3789       14574 : }
    3790             : 
    3791             : /* newtrace_{N,k}(d*i), i = n0, ..., n */
    3792             : static GEN
    3793      109690 : colnewtrace(long n0, long n, long d, long N, long k, cachenew_t *cache)
    3794             : {
    3795      109690 :   GEN v = cgetg(n-n0+2, t_COL);
    3796             :   long i;
    3797      109690 :   for (i = n0; i <= n; i++) gel(v, i-n0+1) = mfnewtracecache(N, k, i*d, cache);
    3798      109690 :   return v;
    3799             : }
    3800             : /* T_n(l*m0, l*(m0+1), ..., l*m) F, F = t_MF_NEWTRACE [N,k],DATA, cache
    3801             :  * contains DATA != NULL as well as cached values of F */
    3802             : static GEN
    3803       62419 : heckenewtrace(long m0, long m, long l, long N, long NBIG, long k, long n, cachenew_t *cache)
    3804             : {
    3805       62419 :   long lD, a, k1, nl = n*l;
    3806       62419 :   GEN D, V, v = colnewtrace(m0, m, nl, N, k, cache); /* d=1 */
    3807             :   GEN VCHIP;
    3808       62419 :   if (n == 1) return v;
    3809       39851 :   VCHIP = cache->VCHIP;
    3810       39851 :   D = mydivisorsu(u_ppo(n, NBIG)); lD = lg(D);
    3811       39851 :   k1 = k - 1;
    3812       86373 :   for (a = 2; a < lD; a++)
    3813             :   { /* d > 1, (d,NBIG) = 1 */
    3814       46522 :     long i, j, d = D[a], c = ugcd(l, d), dl = d/c, m0d = ceildiv(m0, dl);
    3815       46522 :     GEN C = vchip_lift(VCHIP, d, powuu(d, k1));
    3816             :     /* m0=0: i = 1 => skip F(0) = 0 */
    3817       46522 :     if (!m0) { i = 1; j = dl; } else { i = 0; j = m0d*dl; }
    3818       46522 :     V = colnewtrace(m0d, m/dl, nl/(d*c), N, k, cache);
    3819             :     /* C = chi(d) d^(k-1) */
    3820      407022 :     for (; j <= m; i++, j += dl)
    3821      360500 :       gel(v,j-m0+1) = gadd(gel(v,j-m0+1), vchip_mod(VCHIP, gmul(C,gel(V,i+1))));
    3822             :   }
    3823       39851 :   return v;
    3824             : }
    3825             : 
    3826             : /* Given v = an[i], return an[d*i] */
    3827             : static GEN
    3828         406 : anextract(GEN v, long n, long d)
    3829             : {
    3830         406 :   GEN w = cgetg(n+2, t_VEC);
    3831             :   long i;
    3832         406 :   for (i = 0; i <= n; i++) gel(w, i+1) = gel(v, i*d+1);
    3833         406 :   return w;
    3834             : }
    3835             : /* T_n(F)(0, l, ..., l*m) */
    3836             : static GEN
    3837         700 : hecke_i(long m, long l, GEN V, GEN F, GEN DATA)
    3838             : {
    3839             :   long k, n, nNBIG, NBIG, lD, M, a, t, nl;
    3840             :   GEN D, v, CHI;
    3841         700 :   if (typ(DATA) == t_VEC)
    3842             :   { /* 1/2-integral k */
    3843          98 :     if (!V) { GEN S = gel(DATA,2); V = mfcoefs_i(F, m*l*S[3], S[4]); }
    3844          98 :     return RgV_heckef2(m, l, V, F, DATA);
    3845             :   }
    3846         602 :   k = mf_get_k(F);
    3847         602 :   n = DATA[1]; nl = n*l;
    3848         602 :   nNBIG = DATA[2];
    3849         602 :   NBIG = DATA[3];
    3850         602 :   if (nNBIG == 1) return V? V: mfcoefs_i(F,m,nl);
    3851         413 :   if (!V && mf_get_type(F) == t_MF_NEWTRACE)
    3852             :   { /* inline F to allow cache, T_n at level NBIG acting on Tr^new(N,k,CHI) */
    3853             :     cachenew_t cache;
    3854         210 :     long N = mf_get_N(F);
    3855         210 :     init_cachenew(&cache, m*nl, N, F);
    3856         210 :     v = heckenewtrace(0, m, l, N, NBIG, k, n, &cache);
    3857         210 :     dbg_cachenew(&cache);
    3858         210 :     settyp(v, t_VEC); return v;
    3859             :   }
    3860         203 :   CHI = mf_get_CHI(F);
    3861         203 :   D = mydivisorsu(nNBIG); lD = lg(D);
    3862         203 :   M = m + 1;
    3863         203 :   t = nNBIG * ugcd(nNBIG, l);
    3864         203 :   if (!V) V = mfcoefs_i(F, m * t, nl / t); /* usually nl = t */
    3865         203 :   v = anextract(V, m, t); /* mfcoefs(F, m, nl); d = 1 */
    3866         406 :   for (a = 2; a < lD; a++)
    3867             :   { /* d > 1, (d, NBIG) = 1 */
    3868         203 :     long d = D[a], c = ugcd(l, d), dl = d/c, i, idl;
    3869         203 :     GEN C = gmul(mfchareval_i(CHI, d), powuu(d, k-1));
    3870         203 :     GEN w = anextract(V, m/dl, t/(d*c)); /* mfcoefs(F, m/dl, nl/(d*c)) */
    3871         644 :     for (i = idl = 1; idl <= M; i++, idl += dl)
    3872         441 :       gel(v,idl) = gadd(gel(v,idl), gmul(C, gel(w,i)));
    3873             :   }
    3874         203 :   return v;
    3875             : }
    3876             : 
    3877             : static GEN
    3878       11025 : mkmf(GEN x1, GEN x2, GEN x3, GEN x4, GEN x5)
    3879             : {
    3880       11025 :   GEN MF = obj_init(5, MF_SPLITN);
    3881       11025 :   gel(MF,1) = x1;
    3882       11025 :   gel(MF,2) = x2;
    3883       11025 :   gel(MF,3) = x3;
    3884       11025 :   gel(MF,4) = x4;
    3885       11025 :   gel(MF,5) = x5; return MF;
    3886             : }
    3887             : 
    3888             : /* return an integer b such that p | b => T_p^k Tr^new = 0, for all k > 0 */
    3889             : static long
    3890        6846 : get_badj(long N, long FC)
    3891             : {
    3892        6846 :   GEN fa = myfactoru(N), P = gel(fa,1), E = gel(fa,2);
    3893        6846 :   long i, b = 1, l = lg(P);
    3894       18221 :   for (i = 1; i < l; i++)
    3895       11375 :     if (E[i] > 1 && u_lval(FC, P[i]) < E[i]) b *= P[i];
    3896        6846 :   return b;
    3897             : }
    3898             : /* in place, assume perm strictly increasing */
    3899             : static void
    3900        1106 : vecpermute_inplace(GEN v, GEN perm)
    3901             : {
    3902        1106 :   long i, l = lg(perm);
    3903        1106 :   for (i = 1; i < l; i++) gel(v,i) = gel(v,perm[i]);
    3904        1106 : }
    3905             : 
    3906             : /* Find basis of newspace using closures; assume k >= 2 and !badchar.
    3907             :  * Return NULL if space is empty, else
    3908             :  * [mf1, list of closures T(j)traceform, list of corresponding j, matrix] */
    3909             : static GEN
    3910       14147 : mfnewinit(long N, long k, GEN CHI, cachenew_t *cache, long init)
    3911             : {
    3912             :   GEN S, vj, M, CHIP, mf1, listj, P, tf;
    3913             :   long j, ct, ctlj, dim, jin, SB, sb, two, ord, FC, badj;
    3914             : 
    3915       14147 :   dim = mfnewdim(N, k, CHI);
    3916       14147 :   if (!dim && !init) return NULL;
    3917        6846 :   sb = mfsturmNk(N, k);
    3918        6846 :   CHIP = mfchartoprimitive(CHI, &FC);
    3919             :   /* remove newtrace data from S to save space in output: negligible slowdown */
    3920        6846 :   tf = tag(t_MF_NEWTRACE, mkNK(N,k,CHIP), CHIP);
    3921        6846 :   badj = get_badj(N, FC);
    3922             :   /* try sbsmall first: Sturm bound not sharp for new space */
    3923        6846 :   SB = ceilA1(N, k);
    3924        6846 :   listj = cgetg(2*sb + 3, t_VECSMALL);
    3925      315161 :   for (j = ctlj = 1; ctlj < 2*sb + 3; j++)
    3926      308315 :     if (ugcd(j, badj) == 1) listj[ctlj++] = j;
    3927        6846 :   if (init)
    3928             :   {
    3929        3822 :     init_cachenew(cache, (SB+1)*listj[dim+1], N, tf);
    3930        3822 :     if (init == -1 || !dim) return NULL; /* old space or dim = 0 */
    3931             :   }
    3932             :   else
    3933        3024 :     reset_cachenew(cache, N, tf);
    3934             :   /* cache.DATA is not NULL */
    3935        6419 :   ord = mfcharorder_canon(CHIP);
    3936        6419 :   P = ord == 1? NULL: mfcharpol(CHIP);
    3937        6419 :   vj = cgetg(dim+1, t_VECSMALL);
    3938        6419 :   M = cgetg(dim+1, t_MAT);
    3939        6426 :   for (two = 1, ct = 0, jin = 1; two <= 2; two++)
    3940             :   {
    3941        6426 :     long a, jlim = jin + sb;
    3942       18256 :     for (a = jin; a <= jlim; a++)
    3943             :     {
    3944             :       GEN z, vecz;
    3945       18249 :       ct++; vj[ct] = listj[a];
    3946       18249 :       gel(M, ct) = heckenewtrace(0, SB, 1, N, N, k, vj[ct], cache);
    3947       18249 :       if (ct < dim) continue;
    3948             : 
    3949        6972 :       z = QabM_indexrank(M, P, ord);
    3950        6972 :       vecz = gel(z, 2); ct = lg(vecz) - 1;
    3951        6972 :       if (ct == dim) { M = mkvec3(z, gen_0, M); break; } /*maximal rank, done*/
    3952         553 :       vecpermute_inplace(M, vecz);
    3953         553 :       vecpermute_inplace(vj, vecz);
    3954             :     }
    3955        6426 :     if (a <= jlim) break;
    3956             :     /* sbsmall was not sufficient, use Sturm bound: must extend M */
    3957          70 :     for (j = 1; j <= ct; j++)
    3958             :     {
    3959          63 :       GEN t = heckenewtrace(SB + 1, sb, 1, N, N, k, vj[j], cache);
    3960          63 :       gel(M,j) = shallowconcat(gel(M, j), t);
    3961             :     }
    3962           7 :     jin = jlim + 1; SB = sb;
    3963             :   }
    3964        6419 :   S = cgetg(dim + 1, t_VEC);
    3965        6419 :   for (j = 1; j <= dim; j++) gel(S, j) = mfhecke_i(vj[j], N, tf);
    3966        6419 :   dbg_cachenew(cache);
    3967        6419 :   mf1 = mkvec4(utoipos(N), utoipos(k), CHI, utoi(mf_NEW));
    3968        6419 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    3969             : }
    3970             : /* k > 1 integral, mf space is mf_CUSP or mf_FULL */
    3971             : static GEN
    3972          28 : mfinittonew(GEN mf)
    3973             : {
    3974          28 :   GEN CHI = MF_get_CHI(mf), S = MF_get_S(mf), vMjd = MFcusp_get_vMjd(mf);
    3975          28 :   GEN M = MF_get_M(mf), vj, mf1;
    3976          28 :   long i, j, l, l0 = lg(S), N0 = MF_get_N(mf);
    3977         161 :   for (i = l0-1; i > 0; i--)
    3978             :   {
    3979         161 :     long N = gel(vMjd,i)[1];
    3980         161 :     if (N != N0) break;
    3981             :   }
    3982          28 :   if (i == l0-1) return NULL;
    3983          28 :   S = vecslice(S, i+1, l0-1); /* forms of conductor N0 */
    3984          28 :   l = lg(S); vj = cgetg(l, t_VECSMALL);
    3985          28 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd,j+i)[2];
    3986          28 :   M = vecslice(M, lg(M)-lg(S)+1, lg(M)-1); /* their coefficients */
    3987          28 :   M = mfcleanCHI(M, CHI, 0);
    3988          28 :   mf1 = mkvec4(utoipos(N0), MF_get_gk(mf), CHI, utoi(mf_NEW));
    3989          28 :   return mkmf(mf1, cgetg(1,t_VEC), S, vj, M);
    3990             : }
    3991             : 
    3992             : /* Bd(f)[m0..m], v = f[ceil(m0/d)..floor(m/d)], m0d = ceil(m0/d) */
    3993             : static GEN
    3994       52416 : RgC_Bd_expand(long m0, long m, GEN v, long d, long m0d)
    3995             : {
    3996             :   long i, j;
    3997             :   GEN w;
    3998       52416 :   if (d == 1) return v;
    3999       14413 :   w = zerocol(m-m0+1);
    4000       14413 :   if (!m0) { i = 1; j = d; } else { i = 0; j = m0d*d; }
    4001       14413 :   for (; j <= m; i++, j += d) gel(w,j-m0+1) = gel(v,i+1);
    4002       14413 :   return w;
    4003             : }
    4004             : /* S a non-empty vector of t_MF_BD(t_MF_HECKE(t_MF_NEWTRACE)); M the matrix
    4005             :  * of their coefficients r*0, r*1, ..., r*m0 (~ mfvectomat) or NULL (empty),
    4006             :  * extend it to coeffs up to m > m0. The forms B_d(T_j(tf_N))in S should be
    4007             :  * sorted by level N, then j, then increasing d. No reordering here. */
    4008             : static GEN
    4009        7119 : bhnmat_extend(GEN M, long m, long r, GEN S, cachenew_t *cache)
    4010             : {
    4011        7119 :   long i, mr, m0, m0r, Nold = 0, jold = 0, l = lg(S);
    4012        7119 :   GEN MAT = cgetg(l, t_MAT), v = NULL;
    4013        7119 :   if (M) { m0 = nbrows(M); m0r = m0 * r; } else m0 = m0r = 0;
    4014        7119 :   mr = m*r;
    4015       59535 :   for (i = 1; i < l; i++)
    4016             :   {
    4017             :     long d, j, md, N;
    4018       52416 :     GEN c, f = bhn_parse(gel(S,i), &d,&j); /* t_MF_NEWTRACE */
    4019       52416 :     N = mf_get_N(f);
    4020       52416 :     md = ceildiv(m0r,d);
    4021       52416 :     if (N != Nold) { reset_cachenew(cache, N, f); Nold = N; jold = 0; }
    4022       52416 :     if (!cache->DATA) { gel(MAT,i) = zerocol(m+1); continue; }
    4023       52416 :     if (j != jold || md)
    4024       43897 :     { v = heckenewtrace(md, mr/d, 1, N, N, mf_get_k(f), j,cache); jold=j; }
    4025       52416 :     c = RgC_Bd_expand(m0r, mr, v, d, md);
    4026       52416 :     if (r > 1) c = c_deflate(m-m0, r, c);
    4027       52416 :     if (M) c = shallowconcat(gel(M,i), c);
    4028       52416 :     gel(MAT,i) = c;
    4029             :   }
    4030        7119 :   return MAT;
    4031             : }
    4032             : 
    4033             : static GEN
    4034        2884 : mfinitcusp(long N, long k, GEN CHI, cachenew_t *cache, long space)
    4035             : {
    4036             :   long L, l, lDN1, FC, N1, d1, i, init;
    4037        2884 :   GEN vS, vMjd, DN1, vmf, CHIP = mfchartoprimitive(CHI, &FC);
    4038             : 
    4039        2884 :   d1 = (space == mf_OLD)? mfolddim_i(N, k, CHIP): mfcuspdim(N, k, CHIP);
    4040        2884 :   if (!d1) return NULL;
    4041        2646 :   N1 = N/FC; DN1 = mydivisorsu(N1); lDN1 = lg(DN1);
    4042        2646 :   init = (space == mf_OLD)? -1: 1;
    4043        2646 :   vmf = cgetg(lDN1, t_VEC);
    4044       15617 :   for (i = lDN1 - 1, l = 1; i; i--)
    4045             :   { /* by decreasing level to allow cache */
    4046       12971 :     GEN mf = mfnewinit(FC*DN1[i], k, CHIP, cache, init);
    4047       12971 :     if (mf) gel(vmf, l++) = mf;
    4048       12971 :     init = 0;
    4049             :   }
    4050        2646 :   setlg(vmf,l); vmf = vecreverse(vmf); /* reorder by increasing level */
    4051             : 
    4052        2646 :   L = mfsturmNk(N, k)+1;
    4053        2646 :   vS = vectrunc_init(L);
    4054        2646 :   vMjd = vectrunc_init(L);
    4055        8260 :   for (i = 1; i < l; i++)
    4056             :   {
    4057        5614 :     GEN DNM, mf = gel(vmf,i), S = MF_get_S(mf), vj = MFnew_get_vj(mf);
    4058        5614 :     long a, lDNM, lS = lg(S), M = MF_get_N(mf);
    4059        5614 :     DNM = mydivisorsu(N / M); lDNM = lg(DNM);
    4060       21315 :     for (a = 1; a < lS; a++)
    4061             :     {
    4062       15701 :       GEN tf = gel(S,a);
    4063       15701 :       long b, j = vj[a];
    4064       38696 :       for (b = 1; b < lDNM; b++)
    4065             :       {
    4066       22995 :         long d = DNM[b];
    4067       22995 :         vectrunc_append(vS, mfbd_i(tf, d));
    4068       22995 :         vectrunc_append(vMjd, mkvecsmall3(M, j, d));
    4069             :       }
    4070             :     }
    4071             :   }
    4072        2646 :   return mkmf(NULL, cgetg(1, t_VEC), vS, vMjd, NULL);
    4073             : }
    4074             : 
    4075             : long
    4076        2975 : mfsturm_mf(GEN mf)
    4077             : {
    4078        2975 :   GEN Mindex = MF_get_Mindex(mf);
    4079        2975 :   long n = lg(Mindex)-1;
    4080        2975 :   return n? Mindex[n]: 0;
    4081             : }
    4082             : 
    4083             : long
    4084         532 : mfsturm(GEN T)
    4085             : {
    4086             :   long N, nk, dk;
    4087         532 :   GEN CHI, mf = checkMF_i(T);
    4088         532 :   if (mf) return mfsturm_mf(mf);
    4089           7 :   checkNK2(T, &N, &nk, &dk, &CHI, 0);
    4090           7 :   return dk == 1 ? mfsturmNk(N, nk) : mfsturmNk(N, (nk + 1) >> 1);
    4091             : }
    4092             : 
    4093             : long
    4094           7 : mfisequal(GEN F, GEN G, long lim)
    4095             : {
    4096           7 :   pari_sp av = avma;
    4097             :   long sb;
    4098           7 :   if (!checkmf_i(F)) pari_err_TYPE("mfisequal",F);
    4099           7 :   if (!checkmf_i(G)) pari_err_TYPE("mfisequal",G);
    4100           7 :   if (lim) sb = lim;
    4101             :   else
    4102             :   {
    4103             :     GEN gN, gk;
    4104           7 :     gN = mf_get_gN(F); gk = mf_get_gk(F);
    4105           7 :     sb = mfsturmNgk(itou(gN), gk);
    4106           7 :     gN = mf_get_gN(G); gk = mf_get_gk(G);
    4107           7 :     sb = maxss(sb, mfsturmNgk(itou(gN), gk));
    4108             :   }
    4109           7 :   return gc_long(av, gequal(mfcoefs_i(F, sb+1, 1), mfcoefs_i(G, sb+1, 1)));
    4110             : }
    4111             : 
    4112             : GEN
    4113          35 : mffields(GEN mf)
    4114             : {
    4115          35 :   if (checkmf_i(mf)) return gcopy(mf_get_field(mf));
    4116          35 :   mf = checkMF(mf); return gcopy(MF_get_fields(mf));
    4117             : }
    4118             : 
    4119             : GEN
    4120         154 : mfeigenbasis(GEN mf)
    4121             : {
    4122         154 :   pari_sp ltop = avma;
    4123             :   GEN F, S, v, vP;
    4124             :   long i, l, k, dS;
    4125             : 
    4126         154 :   mf = checkMF(mf);
    4127         154 :   k = MF_get_k(mf);
    4128         154 :   S = MF_get_S(mf); dS = lg(S)-1;
    4129         154 :   if (!dS) return cgetg(1, t_VEC);
    4130         147 :   F = MF_get_newforms(mf);
    4131         147 :   vP = MF_get_fields(mf);
    4132         147 :   if (k == 1)
    4133             :   {
    4134          49 :     if (MF_get_space(mf) == mf_FULL)
    4135             :     {
    4136           7 :       long dE = lg(MF_get_E(mf)) - 1;
    4137           7 :       if (dE) F = rowslice(F, dE+1, dE+dS);
    4138             :     }
    4139          49 :     v = vecmflineardiv_linear(S, F);
    4140          49 :     l = lg(v);
    4141             :   }
    4142             :   else
    4143             :   {
    4144          98 :     GEN (*L)(GEN, GEN) = (MF_get_space(mf) == mf_FULL)? mflinear: mflinear_bhn;
    4145          98 :     l = lg(F); v = cgetg(l, t_VEC);
    4146          98 :     for (i = 1; i < l; i++) gel(v,i) = L(mf, gel(F,i));
    4147             :   }
    4148         147 :   for (i = 1; i < l; i++) mf_setfield(gel(v,i), gel(vP,i));
    4149         147 :   return gerepilecopy(ltop, v);
    4150             : }
    4151             : 
    4152             : /* Minv = [M, d, A], v a t_COL; A a Zab, d a t_INT; return (A/d) * M*v */
    4153             : static GEN
    4154        5348 : Minv_RgC_mul(GEN Minv, GEN v)
    4155             : {
    4156        5348 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4157        5348 :   v = RgM_RgC_mul(M, v);
    4158        5348 :   if (!equali1(A))
    4159             :   {
    4160        1225 :     if (typ(A) == t_POL && degpol(A) > 0) A = mkpolmod(A, gel(Minv,4));
    4161        1225 :     v = RgC_Rg_mul(v, A);
    4162             :   }
    4163        5348 :   if (!equali1(d)) v = RgC_Rg_div(v, d);
    4164        5348 :   return v;
    4165             : }
    4166             : static GEN
    4167         987 : Minv_RgM_mul(GEN Minv, GEN B)
    4168             : {
    4169         987 :   long j, l = lg(B);
    4170         987 :   GEN M = cgetg(l, t_MAT);
    4171         987 :   for (j = 1; j < l; j++) gel(M,j) = Minv_RgC_mul(Minv, gel(B,j));
    4172         987 :   return M;
    4173             : }
    4174             : /* B * Minv; allow B = NULL for Id */
    4175             : static GEN
    4176        2002 : RgM_Minv_mul(GEN B, GEN Minv)
    4177             : {
    4178        2002 :   GEN M = gel(Minv,1), d = gel(Minv,2), A = gel(Minv,3);
    4179        2002 :   if (B) M = RgM_mul(B, M);
    4180        2002 :   if (!equali1(A))
    4181             :   {
    4182         714 :     if (typ(A) == t_POL) A = mkpolmod(A, gel(Minv,4));
    4183         714 :     M = RgM_Rg_mul(M, A);
    4184             :   }
    4185        2002 :   if (!equali1(d)) M = RgM_Rg_div(M,d);
    4186        2002 :   return M;
    4187             : }
    4188             : 
    4189             : /* perm vector of strictly increasing indices, v a vector or arbitrary length;
    4190             :  * the last r entries of perm fall beyond v.
    4191             :  * Return v o perm[1..(-r)], discarding the last r entries of v */
    4192             : static GEN
    4193        1043 : vecpermute_partial(GEN v, GEN perm, long *r)
    4194             : {
    4195        1043 :   long i, n = lg(v)-1, l = lg(perm);
    4196             :   GEN w;
    4197        1043 :   if (perm[l-1] <= n) { *r = 0; return vecpermute(v,perm); }
    4198          63 :   for (i = 1; i < l; i++)
    4199          63 :     if (perm[i] > n) break;
    4200          21 :   *r = l - i; l = i;
    4201          21 :   w = cgetg(l, typ(v));
    4202          21 :   for (i = 1; i < l; i++) gel(w,i) = gel(v,perm[i]);
    4203          21 :   return w;
    4204             : }
    4205             : 
    4206             : /* given form F, find coeffs of F on mfbasis(mf). If power series, not
    4207             :  * guaranteed correct if precision less than Sturm bound */
    4208             : static GEN
    4209        1050 : mftobasis_i(GEN mf, GEN F)
    4210             : {
    4211             :   GEN v, Mindex, Minv;
    4212        1050 :   if (!MF_get_dim(mf)) return cgetg(1, t_COL);
    4213        1050 :   Mindex = MF_get_Mindex(mf);
    4214        1050 :   Minv = MF_get_Minv(mf);
    4215        1050 :   if (checkmf_i(F))
    4216             :   {
    4217         154 :     long n = Mindex[lg(Mindex)-1];
    4218         154 :     v = vecpermute(mfcoefs_i(F, n, 1), Mindex);
    4219         154 :     return Minv_RgC_mul(Minv, v);
    4220             :   }
    4221             :   else
    4222             :   {
    4223         896 :     GEN A = gel(Minv,1), d = gel(Minv,2);
    4224             :     long r;
    4225         896 :     v = F;
    4226         896 :     switch(typ(F))
    4227             :     {
    4228           0 :       case t_SER: v = sertocol(v);
    4229         896 :       case t_VEC: case t_COL: break;
    4230           0 :       default: pari_err_TYPE("mftobasis", F);
    4231             :     }
    4232         896 :     if (lg(v) == 1) pari_err_TYPE("mftobasis",v);
    4233         896 :     v = vecpermute_partial(v, Mindex, &r);
    4234         896 :     if (!r) return Minv_RgC_mul(Minv, v); /* single solution */
    4235             :     /* affine space of dimension r */
    4236          21 :     v = RgM_RgC_mul(vecslice(A, 1, lg(v)-1), v);
    4237          21 :     if (!equali1(d)) v = RgC_Rg_div(v,d);
    4238          21 :     return mkvec2(v, vecslice(A, lg(A)-r, lg(A)-1));
    4239             :   }
    4240             : }
    4241             : 
    4242             : static GEN
    4243         560 : const_mat(long n, GEN x)
    4244             : {
    4245         560 :   long j, l = n+1;
    4246         560 :   GEN A = cgetg(l,t_MAT);
    4247         560 :   for (j = 1; j < l; j++) gel(A,j) = const_col(n, x);
    4248         560 :   return A;
    4249             : }
    4250             : 
    4251             : /* L is the mftobasis of a form on CUSP space. We allow mf_FULL or mf_CUSP */
    4252             : static GEN
    4253         280 : mftonew_i(GEN mf, GEN L, long *plevel)
    4254             : {
    4255             :   GEN S, listMjd, CHI, res, Aclos, Acoef, D, perm;
    4256         280 :   long N1, LC, lD, i, l, t, level, N = MF_get_N(mf);
    4257             : 
    4258         280 :   if (MF_get_k(mf) == 1) pari_err_IMPL("mftonew in weight 1");
    4259         280 :   listMjd = MFcusp_get_vMjd(mf);
    4260         280 :   CHI = MF_get_CHI(mf); LC = mfcharconductor(CHI);
    4261         280 :   S = MF_get_S(mf);
    4262             : 
    4263         280 :   N1 = N/LC;
    4264         280 :   D = mydivisorsu(N1); lD = lg(D);
    4265         280 :   perm = cgetg(N1+1, t_VECSMALL);
    4266         280 :   for (i = 1; i < lD; i++) perm[D[i]] = i;
    4267         280 :   Aclos = const_mat(lD-1, cgetg(1,t_VEC));
    4268         280 :   Acoef = const_mat(lD-1, cgetg(1,t_VEC));
    4269         280 :   l = lg(listMjd);
    4270        2877 :   for (i = 1; i < l; i++)
    4271             :   {
    4272             :     long M, d;
    4273             :     GEN v;
    4274        2597 :     if (gequal0(gel(L,i))) continue;
    4275         273 :     v = gel(listMjd, i);
    4276         273 :     M = perm[ v[1]/LC ];
    4277         273 :     d = perm[ v[3] ];
    4278         273 :     gcoeff(Aclos,M,d) = vec_append(gcoeff(Aclos,M,d), gel(S,i));
    4279         273 :     gcoeff(Acoef,M,d) = shallowconcat(gcoeff(Acoef,M,d), gel(L,i));
    4280             :   }
    4281         280 :   res = cgetg(l, t_VEC); level = 1;
    4282        2009 :   for (i = t = 1; i < lD; i++)
    4283             :   {
    4284        1729 :     long j, M = D[i]*LC;
    4285        1729 :     GEN gM = utoipos(M);
    4286       15134 :     for (j = 1; j < lD; j++)
    4287             :     {
    4288       13405 :       GEN f = gcoeff(Aclos,i,j), C, NK;
    4289             :       long d;
    4290       13405 :       if (lg(f) == 1) continue;
    4291         245 :       NK = mf_get_NK(gel(f,1));
    4292         245 :       d = D[j];
    4293         245 :       C = gcoeff(Acoef,i,j);
    4294         245 :       level = ulcm(level, M*d);
    4295         245 :       gel(res,t++) = mkvec3(gM, utoipos(d), mflinear_i(NK,f,C));
    4296             :     }
    4297             :   }
    4298         280 :   if (plevel) *plevel = level;
    4299         280 :   setlg(res, t); return res;
    4300             : }
    4301             : GEN
    4302          35 : mftonew(GEN mf, GEN F)
    4303             : {
    4304          35 :   pari_sp av = avma;
    4305             :   GEN ES;
    4306             :   long s;
    4307          35 :   mf = checkMF(mf);
    4308          35 :   s = MF_get_space(mf);
    4309          35 :   if (s != mf_FULL && s != mf_CUSP)
    4310           7 :     pari_err_TYPE("mftonew [not a full or cuspidal space]", mf);
    4311          28 :   ES = mftobasisES(mf,F);
    4312          21 :   if (!gequal0(gel(ES,1)))
    4313           0 :     pari_err_TYPE("mftonew [not a cuspidal form]", F);
    4314          21 :   F = gel(ES,2);
    4315          21 :   return gerepilecopy(av, mftonew_i(mf,F, NULL));
    4316             : }
    4317             : 
    4318             : static GEN mfeisenstein_i(long k, GEN CHI1, GEN CHI2);
    4319             : 
    4320             : /* mfinit(F * Theta) */
    4321             : static GEN
    4322          70 : mf2init(GEN mf)
    4323             : {
    4324          70 :   GEN CHI = MF_get_CHI(mf), gk = gadd(MF_get_gk(mf), ghalf);
    4325          70 :   long N = MF_get_N(mf);
    4326          70 :   return mfinit_Nkchi(N, itou(gk), mfchiadjust(CHI, gk, N), mf_FULL, 0);
    4327             : }
    4328             : 
    4329             : static long
    4330         329 : mfvec_first_cusp(GEN v)
    4331             : {
    4332         329 :   long i, l = lg(v);
    4333         798 :   for (i = 1; i < l; i++)
    4334             :   {
    4335         721 :     GEN F = gel(v,i);
    4336         721 :     long t = mf_get_type(F);
    4337         721 :     if (t == t_MF_BD) { F = gel(F,2); t = mf_get_type(F); }
    4338         721 :     if (t == t_MF_HECKE) { F = gel(F,3); t = mf_get_type(F); }
    4339         721 :     if (t == t_MF_NEWTRACE) break;
    4340             :   }
    4341         329 :   return i;
    4342             : }
    4343             : /* vF a vector of mf F of type DIV(LINEAR(BAS,L), f) in (lcm) level N,
    4344             :  * F[2]=LINEAR(BAS,L), F[2][2]=BAS=fixed basis (Eisentstein or bhn type),
    4345             :  * F[2][3]=L, F[3]=f; mfvectomat(vF, n) */
    4346             : static GEN
    4347         336 : mflineardivtomat(long N, GEN vF, long n)
    4348             : {
    4349             :   GEN F, M, f, fc, V, ME, B, a0;
    4350         336 :   long lM, lF = lg(vF), i, j;
    4351             : 
    4352         336 :   if (lF == 1) return cgetg(1,t_MAT);
    4353         329 :   F = gel(vF,1);
    4354         329 :   M = gmael(F,2,2); /* BAS */
    4355         329 :   lM = lg(M);
    4356         329 :   i = mfvec_first_cusp(M);
    4357         329 :   if (i == 1) ME = NULL;
    4358             :   else
    4359             :   { /* BAS starts by Eisenstein */
    4360         105 :     ME = mfvectomat(vecslice(M,1,i-1), n, 1);
    4361         105 :     M = vecslice(M, i,lM-1);
    4362             :   }
    4363         329 :   M = bhnmat_extend_nocache(NULL, N, n, 1, M);
    4364         329 :   if (ME) M = shallowconcat(ME,M);
    4365             :   /* M = mfcoefs of BAS */
    4366         329 :   f = mfcoefsser(gel(F,3),n);
    4367         329 :   a0 = polcoef_i(f, 0, -1);
    4368         329 :   if (gequal0(a0) || gequal1(a0))
    4369         266 :     a0 = NULL;
    4370             :   else
    4371          63 :     f = gdiv(ser_unscale(f, a0), a0);
    4372         329 :   fc = ginv(f);
    4373         329 :   V = cgetg(lM, t_VEC);
    4374        3402 :   for (i = 1; i < lM; i++)
    4375             :   {
    4376        3073 :     pari_sp av = avma;
    4377        3073 :     GEN LISer = RgV_to_ser_full(gel(M,i)), f;
    4378        3073 :     if (a0) LISer = gdiv(ser_unscale(LISer, a0), a0);
    4379        3073 :     f = gmul(LISer, fc);
    4380        3073 :     if (a0) f = ser_unscale(f, ginv(a0));
    4381        3073 :     f = sertocol(f); setlg(f, n+2);
    4382        3073 :     gel(V,i) = gerepileupto(av,f);
    4383             :   }
    4384         329 :   B = cgetg(lF, t_MAT);
    4385        1568 :   for (j = 1; j < lF; j++)
    4386             :   {
    4387        1239 :     pari_sp av = avma;
    4388        1239 :     GEN S = gen_0, coe;
    4389        1239 :     F = gel(vF, j); /* t_MF_DIV */
    4390        1239 :     coe = gdiv(gmael(F,2,3), gmael(F,2,4));
    4391       17108 :     for (i = 1; i < lM; i++)
    4392             :     {
    4393       15869 :       GEN co = gel(coe, i);
    4394       15869 :       if (!gequal0(co)) S = gadd(S, gmul(co, gel(V, i)));
    4395             :     }
    4396        1239 :     gel(B,j) = gerepileupto(av, S);
    4397             :   }
    4398         329 :   return B;
    4399             : }
    4400             : 
    4401             : static GEN
    4402         105 : mfheckemat_mfcoefs(GEN mf, GEN B, GEN DATA)
    4403             : {
    4404         105 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4405         105 :   long j, l = lg(B), sb = mfsturm_mf(mf)-1;
    4406         105 :   GEN b = MF_get_basis(mf), Q = cgetg(l, t_VEC);
    4407         371 :   for (j = 1; j < l; j++)
    4408             :   {
    4409         266 :     GEN v = hecke_i(sb, 1, gel(B,j), gel(b,j), DATA); /* Tn b[j] */
    4410         266 :     settyp(v,t_COL); gel(Q,j) = vecpermute(v, Mindex);
    4411             :   }
    4412         105 :   return Minv_RgM_mul(Minv,Q);
    4413             : }
    4414             : /* T_p^2, p prime, 1/2-integral weight; B = mfcoefs(mf,sb*p^2,1) or (mf,sb,p^2)
    4415             :  * if p|N */
    4416             : static GEN
    4417           7 : mfheckemat_mfcoefs_p2(GEN mf, long p, GEN B)
    4418             : {
    4419           7 :   pari_sp av = avma;
    4420           7 :   GEN DATA = heckef2_data(MF_get_N(mf), p*p);
    4421           7 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, DATA));
    4422             : }
    4423             : /* convert Mindex from row-index to mfcoef indexation: a(n) is stored in
    4424             :  * mfcoefs()[n+1], so subtract 1 from all indices */
    4425             : static GEN
    4426          49 : Mindex_as_coef(GEN mf)
    4427             : {
    4428          49 :   GEN v, Mindex = MF_get_Mindex(mf);
    4429          49 :   long i, l = lg(Mindex);
    4430          49 :   v = cgetg(l, t_VECSMALL);
    4431          49 :   for (i = 1; i < l; i++) v[i] = Mindex[i]-1;
    4432          49 :   return v;
    4433             : }
    4434             : /* T_p, p prime; B = mfcoefs(mf,sb*p,1) or (mf,sb,p) if p|N; integral weight */
    4435             : static GEN
    4436          35 : mfheckemat_mfcoefs_p(GEN mf, long p, GEN B)
    4437             : {
    4438          35 :   pari_sp av = avma;
    4439          35 :   GEN vm, Q, C, Minv = MF_get_Minv(mf);
    4440          35 :   long lm, k, i, j, l = lg(B), N = MF_get_N(mf);
    4441             : 
    4442          35 :   if (N % p == 0) return Minv_RgM_mul(Minv, rowpermute(B, MF_get_Mindex(mf)));
    4443          21 :   k = MF_get_k(mf);
    4444          21 :   C = gmul(mfchareval_i(MF_get_CHI(mf), p), powuu(p, k-1));
    4445          21 :   vm = Mindex_as_coef(mf); lm = lg(vm);
    4446          21 :   Q = cgetg(l, t_MAT);
    4447          21 :   for (j = 1; j < l; j++) gel(Q,j) = cgetg(lm, t_COL);
    4448         147 :   for (i = 1; i < lm; i++)
    4449             :   {
    4450         126 :     long m = vm[i], mp = m*p;
    4451         126 :     GEN Cm = (m % p) == 0? C : NULL;
    4452        1260 :     for (j = 1; j < l; j++)
    4453             :     {
    4454        1134 :       GEN S = gel(B,j), s = gel(S, mp + 1);
    4455        1134 :       if (Cm) s = gadd(s, gmul(C, gel(S, m/p + 1)));
    4456        1134 :       gcoeff(Q, i, j) = s;
    4457             :     }
    4458             :   }
    4459          21 :   return gerepileupto(av, Minv_RgM_mul(Minv,Q));
    4460             : }
    4461             : /* Matrix of T(p), p prime, dim(mf) > 0 and integral weight */
    4462             : static GEN
    4463          98 : mfheckemat_p(GEN mf, long p)
    4464             : {
    4465          98 :   pari_sp av = avma;
    4466          98 :   long N = MF_get_N(mf), sb = mfsturm_mf(mf)-1;
    4467          98 :   GEN B = (N % p)? mfcoefs_mf(mf, sb * p, 1): mfcoefs_mf(mf, sb, p);
    4468          98 :   return gerepileupto(av, mfheckemat_mfcoefs(mf, B, hecke_data(N,p)));
    4469             : }
    4470             : 
    4471             : /* mf_NEW != (0), weight > 1, p prime. Use
    4472             :  * T(p) T(j) = T(j*p) + p^{k-1} \chi(p) 1_{p | j, p \nmid N} T(j/p) */
    4473             : static GEN
    4474         847 : mfnewmathecke_p(GEN mf, long p)
    4475             : {
    4476         847 :   pari_sp av = avma;
    4477         847 :   GEN tf, vj = MFnew_get_vj(mf), CHI = MF_get_CHI(mf);
    4478         847 :   GEN Mindex = MF_get_Mindex(mf), Minv = MF_get_Minv(mf);
    4479         847 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4480         847 :   long i, j, lvj = lg(vj), lim = vj[lvj-1] * p;
    4481         847 :   GEN M, perm, V, need = zero_zv(lim);
    4482         847 :   GEN C = (N % p)? gmul(mfchareval_i(CHI,p), powuu(p,k-1)): NULL;
    4483         847 :   tf = mftraceform_new(N, k, CHI);
    4484        3647 :   for (i = 1; i < lvj; i++)
    4485             :   {
    4486        2800 :     j = vj[i]; need[j*p] = 1;
    4487        2800 :     if (N % p && j % p == 0) need[j/p] = 1;
    4488             :   }
    4489         847 :   perm = zero_zv(lim);
    4490         847 :   V = cgetg(lim+1, t_VEC);
    4491       11851 :   for (i = j = 1; i <= lim; i++)
    4492       11004 :     if (need[i]) { gel(V,j) = mfhecke_i(i, N, tf); perm[i] = j; j++; }
    4493         847 :   setlg(V, j);
    4494         847 :   V = bhnmat_extend_nocache(NULL, N, mfsturm_mf(mf)-1, 1, V);
    4495         847 :   V = rowpermute(V, Mindex); /* V[perm[i]] = coeffs(T_i newtrace) */
    4496         847 :   M = cgetg(lvj, t_MAT);
    4497        3647 :   for (i = 1; i < lvj; i++)
    4498             :   {
    4499             :     GEN t;
    4500        2800 :     j = vj[i]; t = gel(V, perm[j*p]);
    4501        2800 :     if (C && j % p == 0) t = RgC_add(t, RgC_Rg_mul(gel(V, perm[j/p]),C));
    4502        2800 :     gel(M,i) = t;
    4503             :   }
    4504         847 :   return gerepileupto(av, Minv_RgM_mul(Minv, M));
    4505             : }
    4506             : 
    4507             : GEN
    4508          77 : mfheckemat(GEN mf, GEN vn)
    4509             : {
    4510          77 :   pari_sp av = avma;
    4511          77 :   long lv, lvP, i, N, dim, nk, dk, p, sb, flint = (typ(vn)==t_INT);
    4512             :   GEN CHI, res, vT, FA, B, vP;
    4513             : 
    4514          77 :   mf = checkMF(mf);
    4515          77 :   if (typ(vn) != t_VECSMALL) vn = gtovecsmall(vn);
    4516          77 :   N = MF_get_N(mf); CHI = MF_get_CHI(mf); Qtoss(MF_get_gk(mf), &nk, &dk);
    4517          77 :   dim = MF_get_dim(mf);
    4518          77 :   lv = lg(vn);
    4519          77 :   res = cgetg(lv, t_VEC);
    4520          77 :   FA = cgetg(lv, t_VEC);
    4521          77 :   vP = cgetg(lv, t_VEC);
    4522          77 :   vT = const_vec(vecsmall_max(vn), NULL);
    4523         182 :   for (i = 1; i < lv; i++)
    4524             :   {
    4525         105 :     ulong n = (ulong)labs(vn[i]);
    4526             :     GEN fa;
    4527         105 :     if (!n) pari_err_TYPE("mfheckemat", vn);
    4528         105 :     if (dk == 1 || uissquareall(n, &n)) fa = myfactoru(n);
    4529           0 :     else { n = 0; fa = myfactoru(1); } /* dummy: T_{vn[i]} = 0 */
    4530         105 :     vn[i] = n;
    4531         105 :     gel(FA,i) = fa;
    4532         105 :     gel(vP,i) = gel(fa,1);
    4533             :   }
    4534          77 :   vP = shallowconcat1(vP); vecsmall_sort(vP);
    4535          77 :   vP = vecsmall_uniq_sorted(vP); /* all primes occurring in vn */
    4536          77 :   lvP = lg(vP); if (lvP == 1) goto END;
    4537          56 :   p = vP[lvP-1];
    4538          56 :   sb = mfsturm_mf(mf)-1;
    4539          56 :   if (dk == 1 && nk != 1 && MF_get_space(mf) == mf_NEW)
    4540          21 :     B = NULL; /* special purpose mfnewmathecke_p is faster */
    4541          35 :   else if (lvP == 2 && N % p == 0)
    4542          21 :     B = mfcoefs_mf(mf, sb, dk==2? p*p: p); /* single prime | N, can optimize */
    4543             :   else
    4544          14 :     B = mfcoefs_mf(mf, sb * (dk==2? p*p: p), 1); /* general initialization */
    4545         126 :   for (i = 1; i < lvP; i++)
    4546             :   {
    4547          70 :     long j, l, q, e = 1;
    4548             :     GEN C, Tp, u1, u0;
    4549          70 :     p = vP[i];
    4550          70 :     for (j = 1; j < lv; j++) e = maxss(e, z_lval(vn[j], p));
    4551          70 :     if (!B)
    4552          28 :       Tp = mfnewmathecke_p(mf, p);
    4553          42 :     else if (dk == 2)
    4554           7 :       Tp = mfheckemat_mfcoefs_p2(mf,p, (lvP==2||N%p)? B: matdeflate(sb,p*p,B));
    4555             :     else
    4556          35 :       Tp = mfheckemat_mfcoefs_p(mf, p, (lvP==2||N%p)? B: matdeflate(sb,p,B));
    4557          70 :     gel(vT, p) = Tp;
    4558          70 :     if (e == 1) continue;
    4559          14 :     u0 = gen_1;
    4560          14 :     if (dk == 2)
    4561             :     {
    4562           0 :       C = N % p? gmul(mfchareval_i(CHI,p*p), powuu(p, nk-2)): NULL;
    4563           0 :       if (e == 2) u0 = sstoQ(p+1,p); /* special case T_{p^4} */
    4564             :     }
    4565             :     else
    4566          14 :       C = N % p? gmul(mfchareval_i(CHI,p),   powuu(p, nk-1)): NULL;
    4567          28 :     for (u1=Tp, q=p, l=2; l <= e; l++)
    4568             :     { /* u0 = T_{p^{l-2}}, u1 = T_{p^{l-1}} for l > 2 */
    4569          14 :       GEN v = gmul(Tp, u1);
    4570          14 :       if (C) v = gsub(v, gmul(C, u0));
    4571             :       /* q = p^l, vT[q] = T_q for k integer else T_{q^2} */
    4572          14 :       q *= p; u0 = u1; gel(vT, q) = u1 = v;
    4573             :     }
    4574             :   }
    4575             : END:
    4576             :   /* vT[p^e] = T_{p^e} for all p^e occurring below */
    4577         182 :   for (i = 1; i < lv; i++)
    4578             :   {
    4579         105 :     long n = vn[i], j, lP;
    4580             :     GEN fa, P, E, M;
    4581         105 :     if (n == 0) { gel(res,i) = zeromat(dim,dim); continue; }
    4582         105 :     if (n == 1) { gel(res,i) = matid(dim); continue; }
    4583          77 :     fa = gel(FA,i);
    4584          77 :     P = gel(fa,1); lP = lg(P);
    4585          77 :     E = gel(fa,2); M = gel(vT, upowuu(P[1], E[1]));
    4586          77 :     for (j = 2; j < lP; j++) M = RgM_mul(M, gel(vT, upowuu(P[j], E[j])));
    4587          77 :     gel(res,i) = M;
    4588             :   }
    4589          77 :   if (flint) res = gel(res,1);
    4590          77 :   return gerepilecopy(av, res);
    4591             : }
    4592             : 
    4593             : 
    4594             : /* f = \sum_i v[i] T_listj[i] (Trace Form) attached to v; replace by f/a_1(f) */
    4595             : static GEN
    4596        1197 : mf_normalize(GEN mf, GEN v)
    4597             : {
    4598        1197 :   GEN c, dc = NULL, M = MF_get_M(mf), Mindex = MF_get_Mindex(mf);
    4599        1197 :   v = Q_primpart(v);
    4600        1197 :   c = RgMrow_RgC_mul(M, v, 2); /* a_1(f) */
    4601        1197 :   if (gequal1(c)) return v;
    4602         742 :   if (typ(c) == t_POL) c = gmodulo(c, mfcharpol(MF_get_CHI(mf)));
    4603         742 :   if (typ(c) == t_POLMOD && varn(gel(c,1)) == 1 && degpol(gel(c,1)) >= 40
    4604           7 :                          && Mindex[1] == 2
    4605           7 :                          && mfcharorder(MF_get_CHI(mf)) <= 2)
    4606           7 :   { /* normalize using expansion at infinity (small coefficients) */
    4607           7 :     GEN w, P = gel(c,1), a1 = gel(c,2);
    4608           7 :     long i, l = lg(Mindex);
    4609           7 :     w = cgetg(l, t_COL);
    4610           7 :     gel(w,1) = gen_1;
    4611         280 :     for (i = 2; i < l; i++)
    4612             :     {
    4613         273 :       c = liftpol_shallow(RgMrow_RgC_mul(M, v, Mindex[i]));
    4614         273 :       gel(w,i) = QXQ_div_ratlift(c, a1, P);
    4615             :     }
    4616             :     /* w = expansion at oo of normalized form */
    4617           7 :     v = Minv_RgC_mul(MF_get_Minv(mf), Q_remove_denom(w, &dc));
    4618           7 :     v = gmodulo(v, P); /* back to mfbasis coefficients */
    4619             :   }
    4620             :   else
    4621             :   {
    4622         735 :     c = ginv(c);
    4623         735 :     if (typ(c) == t_POLMOD) c = Q_remove_denom(c, &dc);
    4624         735 :     v = RgC_Rg_mul(v, c);
    4625             :   }
    4626         742 :   if (dc) v = RgC_Rg_div(v, dc);
    4627         742 :   return v;
    4628             : }
    4629             : static void
    4630         266 : pol_red(GEN NF, GEN *pP, GEN *pa, long flag)
    4631             : {
    4632         266 :   GEN dP, a, P = *pP;
    4633         266 :   long d = degpol(P);
    4634             : 
    4635         266 :   *pa = a = pol_x(varn(P));
    4636         266 :   if (d > 30) return;
    4637             : 
    4638         259 :   dP = RgX_disc(P);
    4639         259 :   if (typ(dP) != t_INT)
    4640          35 :   { dP = gnorm(dP); if (typ(dP) != t_INT) pari_err_BUG("mfnewsplit"); }
    4641         259 :   if (d == 2 || expi(dP) < 62)
    4642             :   {
    4643         231 :     if (expi(dP) < 31)
    4644         231 :       P = NF? rnfpolredabs(NF, P,flag): polredabs0(P,flag);
    4645             :     else
    4646           0 :       P = NF? rnfpolredbest(NF,P,flag): polredbest(P,flag);
    4647         231 :     if (flag)
    4648             :     {
    4649         203 :       a = gel(P,2); if (typ(a) == t_POLMOD) a = gel(a,2);
    4650         203 :       P = gel(P,1);
    4651             :     }
    4652             :   }
    4653         259 :   *pP = P;
    4654         259 :   *pa = a;
    4655             : }
    4656             : 
    4657             : /* Diagonalize and normalize. See mfsplit for meaning of flag. */
    4658             : static GEN
    4659         861 : mfspclean(GEN mf, GEN mf0, GEN NF, long ord, GEN simplesp, long flag)
    4660             : {
    4661         861 :   const long vz = 1;
    4662         861 :   long i, l = lg(simplesp), dim = MF_get_dim(mf);
    4663         861 :   GEN res = cgetg(l, t_MAT), pols = cgetg(l, t_VEC);
    4664         861 :   GEN zeros = (mf == mf0)? NULL: zerocol(dim - MF_get_dim(mf0));
    4665        2086 :   for (i = 1; i < l; i++)
    4666             :   {
    4667        1225 :     GEN ATP = gel(simplesp, i), A = gel(ATP,1), P = gel(ATP,3);
    4668        1225 :     long d = degpol(P);
    4669        1225 :     GEN a, v = (flag && d > flag)? NULL: gel(A,1);
    4670        1225 :     if (d == 1) P = pol_x(vz);
    4671             :     else
    4672             :     {
    4673         266 :       pol_red(NF, &P, &a, !!v);
    4674         266 :       if (v)
    4675             :       { /* Mod(a,P) root of charpoly(T), K*gpowers(a) = eigenvector of T */
    4676         238 :         GEN K, den, M = cgetg(d+1, t_MAT), T = gel(ATP,2);
    4677             :         long j;
    4678         238 :         T = shallowtrans(T);
    4679         238 :         gel(M,1) = vec_ei(d,1); /* basis of cyclic vectors */
    4680         238 :         for (j = 2; j <= d; j++) gel(M,j) = RgM_RgC_mul(T, gel(M,j-1));
    4681         238 :         M = Q_primpart(M);
    4682         287 :         K = NF? ZabM_inv(liftpol_shallow(M), nf_get_pol(NF), ord, &den)
    4683         287 :               : ZM_inv(M,&den);
    4684         238 :         K = shallowtrans(K);
    4685         238 :         v = gequalX(a)? pol_x_powers(d, vz): RgXQ_powers(a, d-1, P);
    4686         238 :         v = gmodulo(RgM_RgC_mul(A, RgM_RgC_mul(K,v)), P);
    4687             :       }
    4688             :     }
    4689        1225 :     if (v)
    4690             :     {
    4691        1197 :       v = mf_normalize(mf0, v); if (zeros) v = shallowconcat(zeros,v);
    4692        1197 :       gel(res,i) = v; if (flag) setlg(res,i+1);
    4693             :     }
    4694             :     else
    4695          28 :       gel(res,i) = zerocol(dim);
    4696        1225 :     gel(pols,i) = P;
    4697             :   }
    4698         861 :   return mkvec2(res, pols);
    4699             : }
    4700             : 
    4701             : /* return v = v_{X-r}(P), and set Z = P / (X-r)^v */
    4702             : static long
    4703          63 : RgX_valrem_root(GEN P, GEN r, GEN *Z)
    4704             : {
    4705             :   long v;
    4706         126 :   for (v = 0; degpol(P); v++)
    4707             :   {
    4708         126 :     GEN t, Q = RgX_div_by_X_x(P, r, &t);
    4709         126 :     if (!gequal0(t)) break;
    4710          63 :     P = Q;
    4711             :   }
    4712          63 :   *Z = P; return v;
    4713             : }
    4714             : static GEN
    4715         959 : mynffactor(GEN NF, GEN P, long dimlim)
    4716             : {
    4717             :   long i, l, v;
    4718             :   GEN R, E;
    4719         959 :   if (dimlim != 1)
    4720             :   {
    4721         406 :     R = NF? nffactor(NF, P): QX_factor(P);
    4722         406 :     if (!dimlim) return R;
    4723          21 :     E = gel(R,2);
    4724          21 :     R = gel(R,1); l = lg(R);
    4725          98 :     for (i = 1; i < l; i++)
    4726          91 :       if (degpol(gel(R,i)) > dimlim) break;
    4727          21 :     if (i == 1) return NULL;
    4728          21 :     setlg(E,i);
    4729          21 :     setlg(R,i); return mkmat2(R, E);
    4730             :   }
    4731             :   /* dimlim = 1 */
    4732         553 :   R = nfroots(NF, P); l = lg(R);
    4733         553 :   if (l == 1) return NULL;
    4734         490 :   v = varn(P);
    4735         490 :   settyp(R, t_COL);
    4736         490 :   if (degpol(P) == l-1)
    4737         441 :     E = const_col(l-1, gen_1);
    4738             :   else
    4739             :   {
    4740          49 :     E = cgetg(l, t_COL);
    4741          49 :     for (i = 1; i < l; i++) gel(E,i) = utoi(RgX_valrem_root(P, gel(R,i), &P));
    4742             :   }
    4743         490 :   R = deg1_from_roots(R, v);
    4744         490 :   return mkmat2(R, E);
    4745             : }
    4746             : 
    4747             : /* Let K be a number field attached to NF (Q if NF = NULL). A K-vector
    4748             :  * space of dimension d > 0 is given by a t_MAT A (n x d, full column rank)
    4749             :  * giving a K-basis, X a section (d x n: left pseudo-inverse of A). Return a
    4750             :  * pair (T, fa), where T is an element of the Hecke algebra (a sum of Tp taken
    4751             :  * from vector vTp) acting on A (a d x d t_MAT) and fa is the factorization of
    4752             :  * its characteristic polynomial, limited to factors of degree <= dimlim if
    4753             :  * dimlim != 0 (return NULL if there are no factors of degree <= dimlim) */
    4754             : static GEN
    4755         952 : findbestsplit(GEN NF, GEN vTp, GEN A, GEN X, long dimlim, long vz)
    4756             : {
    4757         952 :   GEN T = NULL, Tkeep = NULL, fakeep = NULL;
    4758         952 :   long lmax = 0, i, lT = lg(vTp);
    4759        2058 :   for (i = 1; i < lT; i++)
    4760             :   {
    4761        1029 :     GEN D, P, E, fa, TpA = gel(vTp,i);
    4762             :     long l;
    4763        1918 :     if (typ(TpA) == t_INT) break;
    4764         959 :     if (lg(TpA) > lg(A)) TpA = RgM_mul(X, RgM_mul(TpA, A)); /* Tp | A */
    4765         959 :     T = T ? RgM_add(T, TpA) : TpA;
    4766         959 :     if (!NF) { P = QM_charpoly_ZX(T); setvarn(P, vz); }
    4767             :     else
    4768             :     {
    4769         112 :       P = charpoly(Q_remove_denom(T, &D), vz);
    4770         112 :       if (D) P = gdiv(RgX_unscale(P, D), powiu(D, degpol(P)));
    4771             :     }
    4772         959 :     fa = mynffactor(NF, P, dimlim);
    4773         959 :     if (!fa) return NULL;
    4774         896 :     E = gel(fa, 2);
    4775             :     /* characteristic polynomial is separable ? */
    4776         896 :     if (isint1(vecmax(E))) { Tkeep = T; fakeep = fa; break; }
    4777          77 :     l = lg(E);
    4778             :     /* characteristic polynomial has more factors than before ? */
    4779          77 :     if (l > lmax) { lmax = l; Tkeep = T; fakeep = fa; }
    4780             :   }
    4781         889 :   return mkvec2(Tkeep, fakeep);
    4782             : }
    4783             : 
    4784             : static GEN
    4785         147 : nfcontent(GEN nf, GEN v)
    4786             : {
    4787         147 :   long i, l = lg(v);
    4788         147 :   GEN c = gel(v,1);
    4789         147 :   for (i = 2; i < l; i++) c = idealadd(nf, c, gel(v,i));
    4790         147 :   if (typ(c) == t_MAT && gequal1(gcoeff(c,1,1))) c = gen_1;
    4791         147 :   return c;
    4792             : }
    4793             : static GEN
    4794         224 : nf_primpart(GEN nf, GEN B)
    4795             : {
    4796         224 :   switch(typ(B))
    4797             :   {
    4798             :     case t_COL:
    4799             :     {
    4800         147 :       GEN A = matalgtobasis(nf, B), c = nfcontent(nf, A);
    4801         147 :       if (typ(c) == t_INT) return B;
    4802          14 :       c = idealred_elt(nf,c);
    4803          14 :       A = Q_primpart( nfC_nf_mul(nf, A, Q_primpart(nfinv(nf,c))) );
    4804          14 :       A = liftpol_shallow( matbasistoalg(nf, A) );
    4805          14 :       if (gexpo(A) > gexpo(B)) A = B;
    4806          14 :       return A;
    4807             :     }
    4808             :     case t_MAT:
    4809             :     {
    4810             :       long i, l;
    4811          77 :       GEN A = cgetg_copy(B, &l);
    4812          77 :       for (i = 1; i < l; i++) gel(A,i) = nf_primpart(nf, gel(B,i));
    4813          77 :       return A;
    4814             :     }
    4815             :     default:
    4816           0 :       pari_err_TYPE("nf_primpart", B);
    4817             :       return NULL; /*LCOV_EXCL_LINE*/
    4818             :   }
    4819             : }
    4820             : 
    4821             : /* rotate entries of v to accomodate new entry 'x' (push out oldest entry) */
    4822             : static void
    4823         917 : vecpush(GEN v, GEN x)
    4824             : {
    4825             :   long i;
    4826         917 :   for (i = lg(v)-1; i > 1; i--) gel(v,i) = gel(v,i-1);
    4827         917 :   gel(v,1) = x;
    4828         917 : }
    4829             : 
    4830             : /* sort t_VEC of vector spaces by increasing dimension */
    4831             : static GEN
    4832         861 : sort_by_dim(GEN v)
    4833             : {
    4834         861 :   long i, l = lg(v);
    4835         861 :   GEN D = cgetg(l, t_VECSMALL);
    4836         861 :   for (i = 1; i < l; i++) D[i] = lg(gmael(v,i,2));
    4837         861 :   return vecpermute(v , vecsmall_indexsort(D));
    4838             : }
    4839             : static GEN
    4840         861 : split_starting_space(GEN mf)
    4841             : {
    4842         861 :   long d = MF_get_dim(mf), d2;
    4843         861 :   GEN id = matid(d);
    4844         861 :   switch(MF_get_space(mf))
    4845             :   {
    4846             :     case mf_NEW:
    4847         854 :     case mf_CUSP: return mkvec2(id, id);
    4848             :   }
    4849           7 :   d2 = lg(MF_get_S(mf))-1;
    4850           7 :   return mkvec2(vecslice(id, d-d2+1,d),
    4851             :                 shallowconcat(zeromat(d2,d-d2),matid(d2)));
    4852             : }
    4853             : /* If dimlim > 0, keep only the dimension <= dimlim eigenspaces.
    4854             :  * See mfsplit for the meaning of flag. */
    4855             : static GEN
    4856        1253 : split_ii(GEN mf, long dimlim, long flag, long *pnewd)
    4857             : {
    4858             :   forprime_t iter;
    4859        1253 :   GEN CHI = MF_get_CHI(mf), empty = cgetg(1, t_VEC), mf0 = mf;
    4860             :   GEN NF, POLCYC, todosp, Tpbigvec, simplesp;
    4861        1253 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    4862        1253 :   long ord, FC, NEWT, dimsimple = 0, newd = -1;
    4863        1253 :   const long NBH = 5, vz = 1;
    4864             :   ulong p;
    4865             : 
    4866        1253 :   switch(MF_get_space(mf))
    4867             :   {
    4868        1141 :     case mf_NEW: break;
    4869             :     case mf_CUSP:
    4870             :     case mf_FULL:
    4871         105 :       if (k > 1) { mf0 = mfinittonew(mf); break; }
    4872          98 :       newd = lg(MF_get_S(mf))-1 - mfolddim(N, k, CHI);
    4873          98 :       break;
    4874           7 :     default: pari_err_TYPE("mfsplit [space does not contain newspace]", mf);
    4875             :       return NULL; /*LCOV_EXCL_LINE*/
    4876             :   }
    4877        1246 :   if (newd < 0) newd = mf0? MF_get_dim(mf0): 0;
    4878        1246 :   *pnewd = newd;
    4879        1246 :   if (!newd) return mkvec2(cgetg(1, t_MAT), empty);
    4880             : 
    4881         861 :   NEWT = (k > 1 && MF_get_space(mf0) == mf_NEW);
    4882         861 :   todosp = mkvec( split_starting_space(mf0) );
    4883         861 :   simplesp = empty;
    4884         861 :   FC = mfcharconductor(CHI);
    4885         861 :   ord = mfcharorder_canon(CHI);
    4886         861 :   if (ord == 1) NF = POLCYC = NULL;
    4887             :   else
    4888             :   {
    4889          77 :     POLCYC = mfcharpol(CHI);
    4890          77 :     NF = nfinit(POLCYC,DEFAULTPREC);
    4891             :   }
    4892         861 :   Tpbigvec = zerovec(NBH);
    4893         861 :   u_forprime_init(&iter, 2, ULONG_MAX);
    4894         861 :   while (dimsimple < newd && (p = u_forprime_next(&iter)))
    4895             :   {
    4896             :     GEN nextsp;
    4897             :     long ind;
    4898        1141 :     if (N % (p*p) == 0 && N/p % FC == 0) continue; /* T_p = 0 in this case */
    4899         917 :     vecpush(Tpbigvec, NEWT? mfnewmathecke_p(mf0,p): mfheckemat_p(mf0,p));
    4900         917 :     nextsp = empty;
    4901        1869 :     for (ind = 1; ind < lg(todosp); ind++)
    4902             :     {
    4903         952 :       GEN tmp = gel(todosp, ind), fa, P, E, D, Tp, DTp;
    4904         952 :       GEN A = gel(tmp, 1);
    4905         952 :       GEN X = gel(tmp, 2);
    4906             :       long lP, i;
    4907         952 :       tmp = findbestsplit(NF, Tpbigvec, A, X, dimlim, vz);
    4908        1589 :       if (!tmp) continue; /* nothing there */
    4909         889 :       Tp = gel(tmp, 1);
    4910         889 :       fa = gel(tmp, 2);
    4911         889 :       P = gel(fa, 1);
    4912         889 :       E = gel(fa, 2); lP = lg(P);
    4913             :       /* lP > 1 */
    4914         889 :       if (DEBUGLEVEL) err_printf("Exponents = %Ps\n", E);
    4915         889 :       if (lP == 2)
    4916             :       {
    4917         616 :         GEN P1 = gel(P,1);
    4918         616 :         long e1 = itos(gel(E,1)), d1 = degpol(P1);
    4919         616 :         if (e1 * d1 == lg(Tp)-1)
    4920             :         {
    4921         574 :           if (e1 > 1) nextsp = vec_append(nextsp, mkvec2(A,X));
    4922             :           else
    4923             :           { /* simple module */
    4924         567 :             simplesp = vec_append(simplesp, mkvec3(A,Tp,P1));
    4925         567 :             dimsimple += d1;
    4926             :           }
    4927         574 :           continue;
    4928             :         }
    4929             :       }
    4930             :       /* Found splitting */
    4931         315 :       DTp = Q_remove_denom(Tp, &D);
    4932        1071 :       for (i = 1; i < lP; i++)
    4933             :       {
    4934         756 :         GEN Ai, Xi, dXi, AAi, v, y, Pi = gel(P,i);
    4935         756 :         Ai = RgX_RgM_eval(D? RgX_rescale(Pi,D): Pi, DTp);
    4936         756 :         Ai = QabM_ker(Ai, POLCYC, ord);
    4937         756 :         if (NF) Ai = nf_primpart(NF, Ai);
    4938             : 
    4939         756 :         AAi = RgM_mul(A, Ai);
    4940             :         /* gives section, works on nonsquare matrices */
    4941         756 :         Xi = QabM_pseudoinv(Ai, POLCYC, ord, &v, &dXi);
    4942         756 :         Xi = RgM_Rg_div(Xi, dXi);
    4943         756 :         y = gel(v,1);
    4944         756 :         if (isint1(gel(E,i)))
    4945             :         {
    4946         658 :           GEN Tpi = RgM_mul(Xi, RgM_mul(rowpermute(Tp,y), Ai));
    4947         658 :           simplesp = vec_append(simplesp, mkvec3(AAi, Tpi, Pi));
    4948         658 :           dimsimple += degpol(Pi);
    4949             :         }
    4950             :         else
    4951             :         {
    4952          98 :           Xi = RgM_mul(Xi, rowpermute(X,y));
    4953          98 :           nextsp = vec_append(nextsp, mkvec2(AAi, Xi));
    4954             :         }
    4955             :       }
    4956             :     }
    4957         917 :     todosp = nextsp; if (lg(todosp) == 1) break;
    4958             :   }
    4959         861 :   if (DEBUGLEVEL) err_printf("end split, need to clean\n");
    4960         861 :   return mfspclean(mf, mf0, NF, ord, sort_by_dim(simplesp), flag);
    4961             : }
    4962             : static GEN
    4963          14 : dim_filter(GEN v, long dim)
    4964             : {
    4965          14 :   GEN P = gel(v,2);
    4966          14 :   long j, l = lg(P);
    4967         112 :   for (j = 1; j < l; j++)
    4968         105 :     if (degpol(gel(P,j)) > dim)
    4969             :     {
    4970           7 :       v = mkvec2(vecslice(gel(v,1),1,j-1), vecslice(P,1,j-1));
    4971           7 :       break;
    4972             :     }
    4973          14 :   return v;
    4974             : }
    4975             : static long
    4976         119 : dim_sum(GEN v)
    4977             : {
    4978         119 :   GEN P = gel(v,2);
    4979         119 :   long j, l = lg(P), d = 0;
    4980         119 :   for (j = 1; j < l; j++) d += degpol(gel(P,j));
    4981         119 :   return d;
    4982             : }
    4983             : static GEN
    4984        1190 : split_i(GEN mf, long dimlim, long flag)
    4985        1190 : { long junk; return split_ii(mf, dimlim, flag, &junk); }
    4986             : /* mf is either already split or output by mfinit. Splitting is done only for
    4987             :  * newspace except in weight 1. If flag = 0 (default) split completely.
    4988             :  * If flag = d > 0, only give the Galois polynomials in degree > d
    4989             :  * Flag is ignored if dimlim = 1. */
    4990             : GEN
    4991          77 : mfsplit(GEN mf0, long dimlim, long flag)
    4992             : {
    4993          77 :   pari_sp av = avma;
    4994          77 :   GEN v, mf = checkMF_i(mf0);
    4995          77 :   if (!mf) pari_err_TYPE("mfsplit", mf0);
    4996          77 :   if ((v = obj_check(mf, MF_SPLIT)))
    4997          14 :   { if (dimlim) v = dim_filter(v, dimlim); }
    4998          63 :   else if (dimlim && (v = obj_check(mf, MF_SPLITN)))
    4999          21 :   { v = (itos(gel(v,1)) >= dimlim)? dim_filter(gel(v,2), dimlim): NULL; }
    5000          77 :   if (!v)
    5001             :   {
    5002             :     long newd;
    5003          63 :     v = split_ii(mf, dimlim, flag, &newd);
    5004          63 :     if (lg(v) == 1) obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5005          63 :     else if (!flag)
    5006             :     {
    5007          42 :       if (dim_sum(v) == newd) obj_insert(mf, MF_SPLIT,v);
    5008          21 :       else obj_insert(mf, MF_SPLITN, mkvec2(utoi(dimlim), v));
    5009             :     }
    5010             :   }
    5011          77 :   return gerepilecopy(av, v);
    5012             : }
    5013             : static GEN
    5014         287 : split(GEN mf) { return split_i(mf,0,0); }
    5015             : GEN
    5016         567 : MF_get_newforms(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),1); }
    5017             : GEN
    5018         392 : MF_get_fields(GEN mf) { return gel(obj_checkbuild(mf,MF_SPLIT,&split),2); }
    5019             : 
    5020             : /*************************************************************************/
    5021             : /*                     Modular forms of Weight 1                         */
    5022             : /*************************************************************************/
    5023             : /* S_1(G_0(N)), small N. Return 1 if definitely empty; return 0 if maybe
    5024             :  * non-empty  */
    5025             : static int
    5026       15932 : wt1empty(long N)
    5027             : {
    5028       15932 :   if (N <= 100) switch (N)
    5029             :   { /* non-empty [32/100] */
    5030             :     case 23: case 31: case 39: case 44: case 46:
    5031             :     case 47: case 52: case 55: case 56: case 57:
    5032             :     case 59: case 62: case 63: case 68: case 69:
    5033             :     case 71: case 72: case 76: case 77: case 78:
    5034             :     case 79: case 80: case 83: case 84: case 87:
    5035             :     case 88: case 92: case 93: case 94: case 95:
    5036        5425 :     case 99: case 100: return 0;
    5037        3437 :     default: return 1;
    5038             :   }
    5039        7070 :   if (N <= 600) switch(N)
    5040             :   { /* empty [111/500] */
    5041             :     case 101: case 102: case 105: case 106: case 109:
    5042             :     case 113: case 121: case 122: case 123: case 125:
    5043             :     case 130: case 134: case 137: case 146: case 149:
    5044             :     case 150: case 153: case 157: case 162: case 163:
    5045             :     case 169: case 170: case 173: case 178: case 181:
    5046             :     case 182: case 185: case 187: case 193: case 194:
    5047             :     case 197: case 202: case 205: case 210: case 218:
    5048             :     case 221: case 226: case 233: case 241: case 242:
    5049             :     case 245: case 246: case 250: case 257: case 265:
    5050             :     case 267: case 269: case 274: case 277: case 281:
    5051             :     case 289: case 293: case 298: case 305: case 306:
    5052             :     case 313: case 314: case 317: case 326: case 337:
    5053             :     case 338: case 346: case 349: case 353: case 361:
    5054             :     case 362: case 365: case 369: case 370: case 373:
    5055             :     case 374: case 377: case 386: case 389: case 394:
    5056             :     case 397: case 401: case 409: case 410: case 421:
    5057             :     case 425: case 427: case 433: case 442: case 449:
    5058             :     case 457: case 461: case 466: case 481: case 482:
    5059             :     case 485: case 490: case 493: case 509: case 514:
    5060             :     case 521: case 530: case 533: case 534: case 538:
    5061             :     case 541: case 545: case 554: case 557: case 562:
    5062             :     case 565: case 569: case 577: case 578: case 586:
    5063         336 :     case 593: return 1;
    5064        6720 :     default: return 0;
    5065             :   }
    5066          14 :   return 0;
    5067             : }
    5068             : 
    5069             : static GEN
    5070          28 : initwt1trace(GEN mf)
    5071             : {
    5072          28 :   GEN S = MF_get_S(mf), v, H;
    5073             :   long l, i;
    5074          28 :   if (lg(S) == 1) return mftrivial();
    5075          28 :   H = mfheckemat(mf, Mindex_as_coef(mf));
    5076          28 :   l = lg(H); v = cgetg(l, t_VEC);
    5077          28 :   for (i = 1; i < l; i++) gel(v,i) = gtrace(gel(H,i));
    5078          28 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5079          28 :   return mflineardiv_linear(S, v, 1);
    5080             : }
    5081             : static GEN
    5082          21 : initwt1newtrace(GEN mf)
    5083             : {
    5084          21 :   GEN v, D, S, Mindex, CHI = MF_get_CHI(mf);
    5085          21 :   long FC, lD, i, sb, N1, N2, lM, N = MF_get_N(mf);
    5086          21 :   CHI = mfchartoprimitive(CHI, &FC);
    5087          21 :   if (N % FC || mfcharparity(CHI) == 1) return mftrivial();
    5088          21 :   D = mydivisorsu(N/FC); lD = lg(D);
    5089          21 :   S = MF_get_S(mf);
    5090          21 :   if (lg(S) == 1) return mftrivial();
    5091          21 :   N2 = newd_params2(N);
    5092          21 :   N1 = N / N2;
    5093          21 :   Mindex = MF_get_Mindex(mf);
    5094          21 :   lM = lg(Mindex);
    5095          21 :   sb = Mindex[lM-1];
    5096          21 :   v = zerovec(sb+1);
    5097          42 :   for (i = 1; i < lD; i++)
    5098             :   {
    5099          21 :     long M = FC*D[i], j;
    5100          21 :     GEN tf = initwt1trace(M == N? mf: mfinit_Nkchi(M, 1, CHI, mf_CUSP, 0));
    5101             :     GEN listd, w;
    5102          21 :     if (mf_get_type(tf) == t_MF_CONST) continue;
    5103          21 :     w = mfcoefs_i(tf, sb, 1);
    5104          21 :     if (M == N) { v = gadd(v, w); continue; }
    5105           0 :     listd = mydivisorsu(u_ppo(ugcd(N/M, N1), FC));
    5106           0 :     for (j = 1; j < lg(listd); j++)
    5107             :     {
    5108           0 :       long d = listd[j], d2 = d*d; /* coprime to FC */
    5109           0 :       GEN dk = mfchareval_i(CHI, d);
    5110           0 :       long NMd = N/(M*d), m;
    5111           0 :       for (m = 1; m <= sb/d2; m++)
    5112             :       {
    5113           0 :         long be = mubeta2(NMd, m);
    5114           0 :         if (be)
    5115             :         {
    5116           0 :           GEN c = gmul(dk, gmulsg(be, gel(w, m+1)));
    5117           0 :           long n = m*d2;
    5118           0 :           gel(v, n+1) = gadd(gel(v, n+1), c);
    5119             :         }
    5120             :       }
    5121             :     }
    5122             :   }
    5123          21 :   if (gequal0(gel(v,2))) return mftrivial();
    5124          21 :   v = vecpermute(v,Mindex);
    5125          21 :   v = Minv_RgC_mul(MF_get_Minv(mf), v);
    5126          21 :   return mflineardiv_linear(S, v, 1);
    5127             : }
    5128             : 
    5129             : /* Matrix of T(p), p \nmid N */
    5130             : static GEN
    5131         119 : Tpmat(long p, long lim, GEN CHI)
    5132             : {
    5133         119 :   GEN M = zeromatcopy(lim, p*lim), chip = mfchareval_i(CHI, p); /* != 0 */
    5134             :   long i, j, pi, pj;
    5135         119 :   gcoeff(M, 1, 1) = gaddsg(1, chip);
    5136         119 :   for (i = 1, pi = p; i < lim; i++,  pi += p) gcoeff(M, i+1, pi+1) = gen_1;
    5137         119 :   for (j = 1, pj = p; pj < lim; j++, pj += p) gcoeff(M, pj+1, j+1) = chip;
    5138         119 :   return M;
    5139             : }
    5140             : 
    5141             : /* assume !wt1empty(N), in particular N>25 */
    5142             : /* Returns [[lim,p], mf (weight 2), p*lim x dim matrix] */
    5143             : static GEN
    5144        1722 : mfwt1_pre(long N)
    5145             : {
    5146        1722 :   GEN M, mf = mfinit_Nkchi(N, 2, mfchartrivial(), mf_CUSP, 0);
    5147             :   /*not empty for N>25*/
    5148             :   long p, lim;
    5149        1722 :   if (uisprime(N))
    5150             :   {
    5151         385 :     p = 2; /*N>25 is not 2 */
    5152         385 :     lim = ceilA1(N, 3);
    5153             :   }
    5154             :   else
    5155             :   {
    5156             :     forprime_t S;
    5157        1337 :     u_forprime_init(&S, 2, N);
    5158        1337 :     while ((p = u_forprime_next(&S)))
    5159        2422 :       if (N % p) break;
    5160        1337 :     lim = mfsturm_mf(mf) + 1;
    5161             :   }
    5162             :   /* p = smalllest prime not dividing N */
    5163        1722 :   M = bhnmat_extend_nocache(MF_get_M(mf), N, p*lim-1, 1, MF_get_S(mf));
    5164        1722 :   return mkvec3(mkvecsmall2(lim, p), mf, M);
    5165             : }
    5166             : 
    5167             : /* lg(A) > 1, E a t_POL */
    5168             : static GEN
    5169         833 : mfmatsermul(GEN A, GEN E)
    5170             : {
    5171         833 :   long j, l = lg(A), r = nbrows(A);
    5172         833 :   GEN M = cgetg(l, t_MAT);
    5173         833 :   E = RgXn_red_shallow(E, r+1);
    5174        8925 :   for (j = 1; j < l; j++)
    5175             :   {
    5176        8092 :     GEN c = RgV_to_RgX(gel(A,j), 0);
    5177        8092 :     gel(M, j) = RgX_to_RgC(RgXn_mul(c, E, r+1), r);
    5178             :   }
    5179         833 :   return M;
    5180             : }
    5181             : /* lg(Ap) > 1, Ep an Flxn */
    5182             : static GEN
    5183         441 : mfmatsermul_Fl(GEN Ap, GEN Ep, ulong p)
    5184             : {
    5185         441 :   long j, l = lg(Ap), r = nbrows(Ap);
    5186         441 :   GEN M = cgetg(l, t_MAT);
    5187        6258 :   for (j = 1; j < l; j++)
    5188             :   {
    5189        5817 :     GEN c = Flv_to_Flx(gel(Ap,j), 0);
    5190        5817 :     gel(M,j) = Flx_to_Flv(Flxn_mul(c, Ep, r+1, p), r);
    5191             :   }
    5192         441 :   return M;
    5193             : }
    5194             : 
    5195             : /* CHI mod F | N, return mfchar of modulus N.
    5196             :  * FIXME: wasteful, G should be precomputed  */
    5197             : static GEN
    5198       16212 : mfcharinduce(GEN CHI, long N)
    5199             : {
    5200             :   GEN G, chi;
    5201       16212 :   if (mfcharmodulus(CHI) == N) return CHI;
    5202        2877 :   G = znstar0(utoipos(N), 1);
    5203        2877 :   chi = zncharinduce(gel(CHI,1), gel(CHI,2), G);
    5204        2877 :   CHI = leafcopy(CHI);
    5205        2877 :   gel(CHI,1) = G;
    5206        2877 :   gel(CHI,2) = chi; return CHI;
    5207             : }
    5208             : 
    5209             : static GEN
    5210        3983 : gmfcharno(GEN CHI)
    5211             : {
    5212        3983 :   GEN G = gel(CHI,1), chi = gel(CHI,2);
    5213        3983 :   return mkintmod(znconreyexp(G, chi), znstar_get_N(G));
    5214             : }
    5215             : static long
    5216       12453 : mfcharno(GEN CHI)
    5217             : {
    5218       12453 :   GEN n = znconreyexp(gel(CHI,1), gel(CHI,2));
    5219       12453 :   return itou(n);
    5220             : }
    5221             : 
    5222             : /* return k such that minimal mfcharacter in Galois orbit of CHI is CHI^k */
    5223             : static long
    5224       11151 : mfconreyminimize(GEN CHI)
    5225             : {
    5226       11151 :   GEN G = gel(CHI,1), cyc, chi;
    5227       11151 :   cyc = ZV_to_zv(znstar_get_cyc(G));
    5228       11151 :   chi = ZV_to_zv(znconreychar(G, gel(CHI,2)));
    5229       11151 :   return zv_cyc_minimize(cyc, chi, coprimes_zv(mfcharorder(CHI)));
    5230             : }
    5231             : 
    5232             : /* find scalar c such that first non-0 entry of c*v is 1; return c*v
    5233             :  * (set c = NULL for 1) */
    5234             : static GEN
    5235        1561 : RgV_normalize(GEN v, GEN *pc)
    5236             : {
    5237        1561 :   long i, l = lg(v);
    5238        1561 :   *pc = NULL;
    5239        3668 :   for (i = 1; i < l; i++)
    5240             :   {
    5241        3668 :     GEN c = gel(v,i);
    5242        3668 :     if (!gequal0(c))
    5243             :     {
    5244        1561 :       if (gequal1(c)) { *pc = gen_1; return v; }
    5245         455 :       *pc = ginv(c); return RgV_Rg_mul(v, *pc);
    5246             :     }
    5247             :   }
    5248           0 :   return v;
    5249             : }
    5250             : /* ordchi != 2 mod 4 */
    5251             : static GEN
    5252        2282 : mftreatdihedral(GEN DIH, GEN POLCYC, long ordchi, long biglim, GEN *pS)
    5253             : {
    5254             :   GEN M, Minv, C;
    5255             :   long l, i;
    5256        2282 :   l = lg(DIH); if (l == 1) return NULL;
    5257        2282 :   if (!pS) return DIH;
    5258         728 :   C = cgetg(l, t_VEC);
    5259         728 :   M = cgetg(l, t_MAT);
    5260        2044 :   for (i = 1; i < l; i++)
    5261             :   {
    5262        1316 :     GEN c, v = mfcoefs_i(gel(DIH,i), biglim, 1);
    5263        1316 :     gel(M,i) = RgV_normalize(v, &c);
    5264        1316 :     gel(C,i) = Rg_col_ei(c, l-1, i);
    5265             :   }
    5266         728 :   Minv = gel(mfclean(M,POLCYC,ordchi,0),2);
    5267         728 :   M = RgM_Minv_mul(M, Minv);
    5268         728 :   C = RgM_Minv_mul(C, Minv);
    5269         728 :   *pS = vecmflinear(DIH, C);
    5270         728 :   return M;
    5271             : }
    5272             : 
    5273             : static GEN
    5274         112 : mfstabiter(GEN M, GEN A2, GEN E1inv, long lim, GEN P, long ordchi)
    5275             : {
    5276             :   GEN A, VC, con;
    5277         112 :   E1inv = primitive_part(E1inv, &con);
    5278         112 :   VC = con? ginv(con): gen_1;
    5279         112 :   A = mfmatsermul(A2, E1inv);
    5280             :   while(1)
    5281          56 :   {
    5282         168 :     GEN R = shallowconcat(RgM_mul(M,A), rowslice(A,1,lim));
    5283         168 :     GEN B = QabM_ker(R, P, ordchi);
    5284         168 :     long lA = lg(A), lB = lg(B);
    5285         168 :     if (lB == 1) return NULL;
    5286         168 :     if (lB == lA) return mkvec2(A, VC);
    5287          56 :     B = rowslice(B, 1, lA-1);
    5288          56 :     if (ordchi != 1) B = gmodulo(B, P);
    5289          56 :     A = Q_primitive_part(RgM_mul(A,B), &con);
    5290          56 :     VC = gmul(VC,B); /* first VC is a scalar, then a RgM */
    5291          56 :     if (con) VC = RgM_Rg_div(VC, con);
    5292             :   }
    5293             : }
    5294             : static long
    5295         112 : mfstabitermodp(GEN Mp, GEN Ap, long p, long lim)
    5296             : {
    5297         112 :   GEN VC = NULL;
    5298             :   while (1)
    5299           7 :   {
    5300         119 :     GEN Rp = shallowconcat(Flm_mul(Mp,Ap,p), rowslice(Ap,1,lim));
    5301         119 :     GEN Bp = Flm_ker(Rp, p);
    5302         119 :     long lA = lg(Ap), lB = lg(Bp);
    5303         119 :     if (lB == 1) return 0;
    5304         119 :     if (lB == lA) return lA-1;
    5305           7 :     Bp = rowslice(Bp, 1, lA-1);
    5306           7 :     Ap = Flm_mul(Ap, Bp, p);
    5307           7 :     VC = VC? Flm_mul(VC, Bp, p): Bp;
    5308             :   }
    5309             : }
    5310             : 
    5311             : static GEN
    5312         210 : mfintereis(GEN A, GEN M2, GEN y, GEN den, GEN E2, GEN P, long ordchi)
    5313             : {
    5314         210 :   GEN z, M1 = mfmatsermul(A,E2), M1den = is_pm1(den)? M1: RgM_Rg_mul(M1,den);
    5315         210 :   M2 = RgM_mul(M2, rowpermute(M1, y));
    5316         210 :   z = QabM_ker(RgM_sub(M2,M1den), P, ordchi);
    5317         210 :   if (ordchi != 1) z = gmodulo(z, P);
    5318         210 :   return mkvec2(RgM_mul(A,z), z);
    5319             : }
    5320             : static GEN
    5321         217 : mfintereismodp(GEN A, GEN M2, GEN E2, ulong p)
    5322             : {
    5323         217 :   GEN M1 = mfmatsermul_Fl(A, E2, p), z;
    5324         217 :   long j, lx = lg(A);
    5325         217 :   z = Flm_ker(shallowconcat(M1, M2), p);
    5326         217 :   for (j = lg(z) - 1; j; j--) setlg(z[j], lx);
    5327         217 :   return mkvec2(Flm_mul(A,z,p), z);
    5328             : }
    5329             : 
    5330             : static GEN
    5331         119 : mfcharinv_i(GEN CHI)
    5332             : {
    5333         119 :   GEN G = gel(CHI,1);
    5334         119 :   CHI = leafcopy(CHI); gel(CHI,2) =  zncharconj(G, gel(CHI,2)); return CHI;
    5335             : }
    5336             : 
    5337             : /* upper bound dim S_1(Gamma_0(N),chi) performing the linear algebra mod p */
    5338             : static long
    5339         119 : mfwt1dimmodp(GEN A, GEN ES, GEN M, long ordchi, long dih, long lim)
    5340             : {
    5341             :   GEN Ap, ApF, ES1p, VC;
    5342         119 :   ulong p, r = QabM_init(ordchi, &p);
    5343             : 
    5344         119 :   ApF = Ap = QabM_to_Flm(A, r, p);
    5345         119 :   VC = NULL;
    5346         119 :   ES1p = QabX_to_Flx(gel(ES,1), r, p);
    5347         119 :   if (lg(ES) >= 3)
    5348             :   {
    5349         112 :     GEN M2 = mfmatsermul_Fl(ApF, ES1p, p);
    5350         112 :     pari_sp av = avma;
    5351             :     long i;
    5352         322 :     for (i = 2; i < lg(ES); i++)
    5353             :     {
    5354         217 :       GEN ESip = QabX_to_Flx(gel(ES,i), r, p);
    5355         217 :       GEN C, ApC = mfintereismodp(Ap, M2, ESip, p);
    5356         217 :       Ap = gel(ApC,1);
    5357         217 :       if (lg(Ap)-1 == dih) return dih;
    5358         210 :       C = gel(ApC,2); VC = VC? Flm_mul(VC, C, p): C;
    5359         210 :       gerepileall(av, 2, &Ap,&VC);
    5360             :     }
    5361             :   }
    5362             :   /* intersection of Eisenstein series quotients non empty: use Schaeffer */
    5363         112 :   Ap = mfmatsermul_Fl(Ap, Flxn_inv(ES1p,nbrows(Ap),p), p);
    5364         112 :   return mfstabitermodp(QabM_to_Flm(M,r,p), Ap, p, lim);
    5365             : }
    5366             : 
    5367             : /* Compute the full S_1(\G_0(N),\chi). If pS is NULL, only the dimension
    5368             :  * dim, in the form of a vector having dim components. Otherwise output
    5369             :  * a basis: ptvf contains a pointer to the vector of forms, and the
    5370             :  * program returns the corresponding matrix of Fourier expansions.
    5371             :  * ptdimdih gives the dimension of the subspace generated by dihedral forms;
    5372             :  * TMP is from mfwt1_pre or NULL. */
    5373             : static GEN
    5374       10605 : mfwt1basis(long N, GEN CHI, GEN TMP, GEN *pS, long *ptdimdih)
    5375             : {
    5376             :   GEN ES, mf, A, M, Tp, tmp1, tmp2, den;
    5377             :   GEN S, ESA, VC, C, POLCYC, ES1, ES1INV, DIH, a0, a0i;
    5378             :   long plim, lim, biglim, i, p, dA, dimp, ordchi, dih;
    5379             : 
    5380       10605 :   if (ptdimdih) *ptdimdih = 0;
    5381       10605 :   if (pS) *pS = NULL;
    5382       10605 :   if (wt1empty(N) || mfcharparity(CHI) != -1) return NULL;
    5383       10423 :   ordchi = mfcharorder_canon(CHI);
    5384       10423 :   if (uisprime(N) && ordchi > 4) return NULL;
    5385       10395 :   if (!pS)
    5386             :   {
    5387        7014 :     dih = mfdihedralcuspdim(N, CHI);
    5388        7014 :     DIH = zerovec(dih);
    5389             :   }
    5390             :   else
    5391             :   {
    5392        3381 :     DIH = mfdihedralcusp(N, CHI);
    5393        3381 :     dih = lg(DIH) - 1;
    5394             :   }
    5395       10395 :   POLCYC = (ordchi == 1)? NULL: mfcharpol(CHI);
    5396       10395 :   if (ptdimdih) *ptdimdih = dih;
    5397       10395 :   biglim = mfsturmNk(N, 2);
    5398       10395 :   if (N <= 600) switch(N)
    5399             :   {
    5400             :     long m;
    5401             :     case 219: case 273: case 283: case 331: case 333: case 344: case 416:
    5402             :     case 438: case 468: case 491: case 504: case 546: case 553: case 563:
    5403             :     case 566: case 581: case 592:
    5404          14 :       break; /* one chi with both exotic and dihedral forms */
    5405             :     default: /* only dihedral forms */
    5406        9408 :       if (!dih) return NULL;
    5407             :       /* fall through */
    5408             :     case 124: case 133: case 148: case 171: case 201: case 209: case 224:
    5409             :     case 229: case 248: case 261: case 266: case 288: case 296: case 301:
    5410             :     case 309: case 325: case 342: case 371: case 372: case 380: case 399:
    5411             :     case 402: case 403: case 404: case 408: case 418: case 432: case 444:
    5412             :     case 448: case 451: case 453: case 458: case 496: case 497: case 513:
    5413             :     case 522: case 527: case 532: case 576: case 579:
    5414             :       /* no chi with both exotic and dihedral; one chi with exotic forms */
    5415        3108 :       if (dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5416         833 :       CHI = mfcharinduce(CHI,N);
    5417         833 :       m = mfcharno(CHI);
    5418         833 :       if (N == 124 && (m != 67 && m != 87)) return NULL;
    5419         707 :       if (N == 133 && (m != 83 && m !=125)) return NULL;
    5420         413 :       if (N == 148 && (m !=105 && m !=117)) return NULL;
    5421         287 :       if (N == 171 && (m != 94 && m !=151)) return NULL;
    5422         287 :       if (N == 201 && (m != 29 && m !=104)) return NULL;
    5423         287 :       if (N == 209 && (m != 87 && m !=197)) return NULL;
    5424         287 :       if (N == 224 && (m != 95 && m !=191)) return NULL;
    5425         287 :       if (N == 229 && (m !=107 && m !=122)) return NULL;
    5426         287 :       if (N == 248 && (m != 87 && m !=191)) return NULL;
    5427         196 :       if (N == 261 && (m != 46 && m !=244)) return NULL;
    5428         196 :       if (N == 266 && (m != 83 && m !=125)) return NULL;
    5429         196 :       if (N == 288 && (m != 31 && m !=223)) return NULL;
    5430         196 :       if (N == 296 && (m !=105 && m !=265)) return NULL;
    5431             :   }
    5432         119 :   if (!TMP) TMP = mfwt1_pre(N);
    5433         119 :   tmp1= gel(TMP,1); lim = tmp1[1]; p = tmp1[2]; plim = p*lim;
    5434         119 :   mf  = gel(TMP,2);
    5435         119 :   A   = gel(TMP,3); /* p*lim x dim matrix */
    5436         119 :   S = MF_get_S(mf);
    5437         119 :   ESA = mfeisensteinbasis(N, 1, mfcharinv_i(CHI));
    5438         119 :   ES = RgM_to_RgXV(mfvectomat(ESA, plim+1, 1), 0);
    5439         119 :   ES1 = gel(ES,1); /* does not vanish at oo */
    5440         119 :   Tp = Tpmat(p, lim, CHI);
    5441         119 :   dimp = mfwt1dimmodp(A, ES, Tp, ordchi, dih, lim);
    5442         119 :   if (!dimp) return NULL;
    5443         119 :   if (dimp == dih) return mftreatdihedral(DIH, POLCYC, ordchi, biglim, pS);
    5444         112 :   VC = gen_1;
    5445         112 :   if (lg(ES) >= 3)
    5446             :   {
    5447             :     pari_sp btop;
    5448         105 :     long lim2 = (3*lim)/2 + 1;
    5449         105 :     GEN Ash = rowslice(A, 1, lim2), M2 = mfmatsermul(Ash, ES1);
    5450             :     GEN v, y, M2M2I, M2I;
    5451         105 :     M2I = QabM_pseudoinv(M2, POLCYC, ordchi, &v, &den);
    5452         105 :     y = gel(v,1);
    5453         105 :     M2M2I = RgM_mul(M2,M2I);
    5454         105 :     btop = avma;
    5455         315 :     for (i = 2; i < lg(ES); i++)
    5456             :     {
    5457         210 :       GEN APC = mfintereis(Ash, M2M2I, y, den, gel(ES,i), POLCYC,ordchi);
    5458         210 :       Ash = gel(APC,1); if (lg(Ash) == 1) return NULL;
    5459         210 :       VC = gmul(VC, gel(APC,2));
    5460         210 :       if (gc_needed(btop, 1))
    5461             :       {
    5462           6 :         if (DEBUGMEM > 1) pari_warn(warnmem,"mfwt1basis i = %ld", i);
    5463           6 :         gerepileall(btop, 2, &Ash, &VC);
    5464             :       }
    5465             :     }
    5466         105 :     A = RgM_mul(A, vecslice(VC,1, lg(Ash)-1));
    5467             :   }
    5468         112 :   a0 = gel(ES1,2); /* non-zero */
    5469         112 :   if (gequal1(a0)) a0 = a0i = NULL;
    5470             :   else
    5471             :   {
    5472         112 :     a0i = ginv(a0);
    5473         112 :     ES1 = RgX_Rg_mul(RgX_unscale(ES1,a0), a0i);
    5474             :   }
    5475         112 :   ES1INV = RgXn_inv(ES1, plim-1);
    5476         112 :   if (a0) ES1INV = RgX_Rg_mul(RgX_unscale(ES1INV, a0i), a0i);
    5477         112 :   tmp2 = mfstabiter(Tp, A, ES1INV, lim, POLCYC, ordchi);
    5478         112 :   if (!tmp2) return NULL;
    5479         112 :   A = gel(tmp2,1); dA = lg(A);
    5480         112 :   VC = gmul(VC, gel(tmp2,2));
    5481         112 :   C = cgetg(dA, t_VEC);
    5482         112 :   M = cgetg(dA, t_MAT);
    5483         357 :   for (i = 1; i < dA; i++)
    5484             :   {
    5485         245 :     GEN c, v = gel(A,i);
    5486         245 :     gel(M,i) = RgV_normalize(v, &c);
    5487         245 :     gel(C,i) = RgC_Rg_mul(gel(VC,i), c);
    5488             :   }
    5489         112 :   if (pS)
    5490             :   {
    5491          63 :     GEN Minv = gel(mfclean(M, POLCYC, ordchi, 0), 2);
    5492          63 :     M = RgM_Minv_mul(M, Minv);
    5493          63 :     C = RgM_Minv_mul(C, Minv);
    5494          63 :     *pS = vecmflineardiv0(S, C, gel(ESA,1));
    5495             :   }
    5496         112 :   return M;
    5497             : }
    5498             : 
    5499             : static void
    5500         245 : MF_set_space(GEN mf, long x) { gmael(mf,1,4) = utoi(x); }
    5501             : static GEN
    5502          91 : mfwt1_cusptonew(GEN mf)
    5503             : {
    5504          91 :   const long vy = 1;
    5505          91 :   GEN vP, F, S, Snew, vF, v = split(mf);
    5506             :   long i, lP, dSnew, ct;
    5507             : 
    5508          91 :   F = gel(v,1);
    5509          91 :   vP= gel(v,2); lP = lg(vP);
    5510          91 :   if (lP == 1) { obj_insert(mf, MF_SPLIT, v); return NULL; }
    5511          77 :   MF_set_space(mf, mf_NEW);
    5512          77 :   S = MF_get_S(mf);
    5513          77 :   dSnew = dim_sum(v);
    5514          77 :   Snew = cgetg(dSnew + 1, t_VEC); ct = 0;
    5515          77 :   vF = cgetg(lP, t_MAT);
    5516         168 :   for (i = 1; i < lP; i++)
    5517             :   {
    5518          91 :     GEN V, P = gel(vP,i), f = liftpol_shallow(gel(F,i));
    5519          91 :     long j, d = degpol(P);
    5520          91 :     gel(vF,i) = V = zerocol(dSnew);
    5521          91 :     if (d == 1)
    5522             :     {
    5523          56 :       gel(Snew, ct+1) = mflineardiv_linear(S, f, 0);
    5524          56 :       gel(V, ct+1) = gen_1;
    5525             :     }
    5526             :     else
    5527             :     {
    5528          35 :       f = RgXV_to_RgM(f,d);
    5529         112 :       for (j = 1; j <= d; j++)
    5530             :       {
    5531          77 :         gel(Snew, ct+j) = mflineardiv_linear(S, row(f,j), 0);
    5532          77 :         gel(V, ct+j) = mkpolmod(pol_xn(j-1,vy), P);
    5533             :       }
    5534             :     }
    5535          91 :     ct += d;
    5536             :   }
    5537          77 :   obj_insert(mf, MF_SPLIT, mkvec2(vF, vP));
    5538          77 :   gel(mf,3) = Snew; return mf;
    5539             : }
    5540             : static GEN
    5541        3465 : mfwt1init(long N, GEN CHI, GEN TMP, long space, long flraw)
    5542             : {
    5543        3465 :   GEN mf, mf1, S, M = mfwt1basis(N, CHI, TMP, &S, NULL);
    5544        3465 :   if (!M) return NULL;
    5545         791 :   mf1 = mkvec4(stoi(N), gen_1, CHI, utoi(mf_CUSP));
    5546         791 :   mf = mkmf(mf1, cgetg(1,t_VEC), S, gen_0, NULL);
    5547         791 :   if (space == mf_NEW)
    5548             :   {
    5549          91 :     gel(mf,5) = mfcleanCHI(M,CHI, 0);
    5550          91 :     mf = mfwt1_cusptonew(mf); if (!mf) return NULL;
    5551          77 :     if (!flraw) M = mfcoefs_mf(mf, mfsturmNk(N,1)+1, 1);
    5552             :   }
    5553         777 :   gel(mf,5) = flraw? zerovec(3): mfcleanCHI(M, CHI, 0);
    5554         777 :   return mf;
    5555             : }
    5556             : 
    5557             : static GEN
    5558         931 : mfEMPTY(GEN mf1)
    5559             : {
    5560         931 :   GEN Minv = mkMinv(cgetg(1,t_MAT), NULL,NULL,NULL);
    5561         931 :   GEN M = mkvec3(cgetg(1,t_VECSMALL), Minv, cgetg(1,t_MAT));
    5562         931 :   return mkmf(mf1, cgetg(1,t_VEC), cgetg(1,t_VEC), cgetg(1,t_VEC), M);
    5563             : }
    5564             : static GEN
    5565         616 : mfEMPTYall(long N, GEN gk, GEN vCHI, long space)
    5566             : {
    5567             :   long i, l;
    5568             :   GEN v, gN, gs;
    5569         616 :   if (!vCHI) return cgetg(1, t_VEC);
    5570          14 :   gN = utoipos(N); gs = utoi(space);
    5571          14 :   l = lg(vCHI); v = cgetg(l, t_VEC);
    5572          14 :   for (i = 1; i < l; i++) gel(v,i) = mfEMPTY(mkvec4(gN,gk,gel(vCHI,i),gs));
    5573          14 :   return v;
    5574             : }
    5575             : 
    5576             : static GEN
    5577        3983 : fmt_dim(GEN CHI, long d, long dih)
    5578        3983 : { return mkvec4(gmfcharorder(CHI), gmfcharno(CHI), utoi(d), stoi(dih)); }
    5579             : /* merge two vector of fmt_dim's for the same vector of characters. If CHI
    5580             :  * is not NULL, remove dim-0 spaces and add character from CHI */
    5581             : static GEN
    5582           7 : merge_dims(GEN V, GEN W, GEN CHI)
    5583             : {
    5584           7 :   long i, j, id, l = lg(V);
    5585           7 :   GEN A = cgetg(l, t_VEC);
    5586           7 :   if (l == 1) return A;
    5587           7 :   id = CHI? 1: 3;
    5588          21 :   for (i = j = 1; i < l; i++)
    5589             :   {
    5590          14 :     GEN v = gel(V,i), w = gel(W,i);
    5591          14 :     long dv = itou(gel(v,id)), dvh = itou(gel(v,id+1)), d;
    5592          14 :     long dw = itou(gel(w,id)), dwh = itou(gel(w,id+1));
    5593          14 :     d = dv + dw;
    5594          14 :     if (d || CHI)
    5595          42 :       gel(A,j++) = CHI? fmt_dim(gel(CHI,i),d, dvh+dwh)
    5596          28 :                       : mkvec2s(d,dvh+dwh);
    5597             :   }
    5598           7 :   setlg(A, j); return A;
    5599             : }
    5600             : static GEN
    5601        3010 : mfdim0all(GEN w)
    5602             : {
    5603        3010 :   if (w) retconst_vec(lg(w)-1, zerovec(2));
    5604        3003 :   return cgetg(1,t_VEC);
    5605             : }
    5606             : static long
    5607        7140 : mfwt1cuspdim_i(long N, GEN CHI, GEN TMP, long *dih)
    5608             : {
    5609        7140 :   pari_sp av = avma;
    5610        7140 :   GEN b = mfwt1basis(N, CHI, TMP, NULL, dih);
    5611        7140 :   return gc_long(av, b? lg(b)-1: 0);
    5612             : }
    5613             : static long
    5614         301 : mfwt1cuspdim(long N, GEN CHI) { return mfwt1cuspdim_i(N, CHI, NULL, NULL); }
    5615             : static GEN
    5616        4144 : mfwt1cuspdimall(long N, GEN vCHI)
    5617             : {
    5618             :   GEN z, TMP, w;
    5619             :   long i, j, l;
    5620        4144 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5621        1141 :   w = mfwt1chars(N,vCHI);
    5622        1141 :   l = lg(w); if (l == 1) return cgetg(1,t_VEC);
    5623        1141 :   z = cgetg(l, t_VEC);
    5624        1141 :   TMP = mfwt1_pre(N);
    5625        7861 :   for (i = j = 1; i < l; i++)
    5626             :   {
    5627        6720 :     GEN CHI = gel(w,i);
    5628        6720 :     long dih, d = mfwt1cuspdim_i(N, CHI, TMP, &dih);
    5629        6720 :     if (vCHI)
    5630          42 :       gel(z,j++) = mkvec2s(d, dih);
    5631        6678 :     else if (d)
    5632        1428 :       gel(z,j++) = fmt_dim(CHI, d, dih);
    5633             :   }
    5634        1141 :   setlg(z,j); return z;
    5635             : }
    5636             : 
    5637             : /* dimension of S_1(Gamma_1(N)) */
    5638             : static long
    5639        4123 : mfwt1cuspdimsum(long N)
    5640             : {
    5641        4123 :   pari_sp av = avma;
    5642        4123 :   GEN v = mfwt1cuspdimall(N, NULL);
    5643        4123 :   long i, ct = 0, l = lg(v);
    5644        5544 :   for (i = 1; i < l; i++)
    5645             :   {
    5646        1421 :     GEN w = gel(v,i); /* [ord(CHI),*,dim,*] */
    5647        1421 :     ct += itou(gel(w,3))*myeulerphiu(itou(gel(w,1)));
    5648             :   }
    5649        4123 :   return gc_long(av,ct);
    5650             : }
    5651             : 
    5652             : static GEN
    5653          56 : mfwt1newdimall(long N, GEN vCHI)
    5654             : {
    5655             :   GEN z, w, vTMP, fa, P, E;
    5656             :   long i, c, l, lw, P1;
    5657          56 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5658          56 :   w = mfwt1chars(N,vCHI);
    5659          56 :   lw = lg(w); if (lw == 1) return cgetg(1,t_VEC);
    5660          56 :   vTMP = const_vec(N, NULL);
    5661          56 :   gel(vTMP,N) = mfwt1_pre(N);
    5662             :   /* if p || N and p \nmid F(CHI), S_1^new(G0(N),chi) = 0 */
    5663          56 :   fa = znstar_get_faN(gmael(w,1,1));
    5664          56 :   P = gel(fa,1); l = lg(P);
    5665          56 :   E = gel(fa,2);
    5666         154 :   for (i = P1 = 1; i < l; i++)
    5667          98 :     if (E[i] == 1) P1 *= itou(gel(P,i));
    5668             :   /* P1 = \prod_{v_p(N) = 1} p */
    5669          56 :   z = cgetg(lw, t_VEC);
    5670         182 :   for (i = c = 1; i < lw; i++)
    5671             :   {
    5672             :     long S, j, l, F, dihnew;
    5673         126 :     GEN D, CHI = gel(w,i), CHIP = mfchartoprimitive(CHI,&F);
    5674             : 
    5675         126 :     S = F % P1? 0: mfwt1cuspdim_i(N, CHI, gel(vTMP,N), &dihnew);
    5676         126 :     if (!S)
    5677             :     {
    5678          56 :       if (vCHI) gel(z, c++) = zerovec(2);
    5679          56 :       continue;
    5680             :     }
    5681          70 :     D = mydivisorsu(N/F); l = lg(D);
    5682          77 :     for (j = l-2; j > 0; j--) /* skip last M = N */
    5683             :     {
    5684           7 :       long M = D[j]*F, m, s, dih;
    5685           7 :       GEN TMP = gel(vTMP,M);
    5686           7 :       if (wt1empty(M) || !(m = mubeta(D[l-j]))) continue; /*m = mubeta(N/M)*/
    5687           7 :       if (!TMP) gel(vTMP,M) = TMP = mfwt1_pre(M);
    5688           7 :       s = mfwt1cuspdim_i(M, CHIP, TMP, &dih);
    5689           7 :       if (s) { S += m * s; dihnew += m * dih; }
    5690             :     }
    5691          70 :     if (vCHI)
    5692          63 :       gel(z,c++) = mkvec2s(S, dihnew);
    5693           7 :     else if (S)
    5694           7 :       gel(z, c++) = fmt_dim(CHI, S, dihnew);
    5695             :   }
    5696          56 :   setlg(z,c); return z;
    5697             : }
    5698             : 
    5699             : static GEN
    5700          28 : mfwt1olddimall(long N, GEN vCHI)
    5701             : {
    5702             :   long i, j, l;
    5703             :   GEN z, w;
    5704          28 :   if (wt1empty(N)) return mfdim0all(vCHI);
    5705          28 :   w = mfwt1chars(N,vCHI);
    5706          28 :   l = lg(w); z = cgetg(l, t_VEC);
    5707          84 :   for (i = j = 1; i < l; i++)
    5708             :   {
    5709          56 :     GEN CHI = gel(w,i);
    5710          56 :     long d = mfolddim(N, 1, CHI);
    5711          56 :     if (vCHI)
    5712          28 :       gel(z,j++) = mkvec2s(d,d?-1:0);
    5713          28 :     else if (d)
    5714           7 :       gel(z, j++) = fmt_dim(CHI, d, -1);
    5715             :   }
    5716          28 :   setlg(z,j); return z;
    5717             : }
    5718             : 
    5719             : static long
    5720         469 : mfwt1olddimsum(long N)
    5721             : {
    5722             :   GEN D;
    5723         469 :   long N2, i, l, S = 0;
    5724         469 :   newd_params(N, &N2); /* will ensure mubeta != 0 */
    5725         469 :   D = mydivisorsu(N/N2); l = lg(D);
    5726        2485 :   for (i = 2; i < l; i++)
    5727             :   {
    5728        2016 :     long M = D[l-i]*N2, d = mfwt1cuspdimsum(M);
    5729        2016 :     if (d) S -= mubeta(D[i]) * d;
    5730             :   }
    5731         469 :   return S;
    5732             : }
    5733             : static long
    5734        1050 : mfwt1newdimsum(long N)
    5735             : {
    5736        1050 :   long S = mfwt1cuspdimsum(N);
    5737        1050 :   return S? S - mfwt1olddimsum(N): 0;
    5738             : }
    5739             : 
    5740             : /* Guess Galois type of wt1 eigenforms. */
    5741             : /* NK can be mf or [N,1,CHI] */
    5742             : static long
    5743          49 : mfisdihedral(GEN F, GEN DIH)
    5744             : {
    5745          49 :   GEN vG = gel(DIH,1), M = gel(DIH,2), v;
    5746             :   long i, l;
    5747          49 :   if (lg(M) == 1) return 0;
    5748          28 :   v = RgM_RgC_invimage(M, mftocol(F, nbrows(M)-1, 1));
    5749          28 :   if (!v) return 0;
    5750          28 :   l = lg(v);
    5751          28 :   for (i = 1; i < l; i++)
    5752          28 :     if (!gequal0(gel(v,i)))
    5753             :     {
    5754          28 :       GEN G = gel(vG,i), bnr = gel(G,2), w = gel(G,3);
    5755          28 :       GEN gen, cyc = bnr_get_cyc(bnr), D = gel(cyc,1);
    5756          28 :       GEN f = bnr_get_mod(bnr), nf = bnr_get_nf(bnr);
    5757          28 :       GEN con = gel(galoisconj(nf,gen_1), 2);
    5758          28 :       GEN f0 = gel(f,1), f0b = galoisapply(nf, con, f0);
    5759          28 :       GEN xin = zv_to_ZV(gel(w,2)); /* xi(bnr.gen[i]) = e(xin[i] / D) */
    5760             :       long e, j, L, n;
    5761          28 :       if (!gequal(f0,f0b))
    5762             :       { /* finite part of conductor not ambiguous */
    5763          14 :         GEN a = idealmul(nf, f0, idealdivexact(nf, f0b, idealadd(nf, f0, f0b)));
    5764          14 :         GEN bnr0 = bnr;
    5765          14 :         bnr = bnrinit0(bnr_get_bnf(bnr), mkvec2(a, gel(f,2)), 1);
    5766          14 :         xin = RgV_RgM_mul(xin, bnrsurjection(bnr, bnr0));
    5767             :         /* still xi(gen[i]) = e(xin[i] / D), for the new generators */
    5768             :       }
    5769          28 :       gen = bnr_get_gen(bnr); L = lg(gen);
    5770          42 :       for (j = 1, e = itou(D); j < L; j++)
    5771             :       {
    5772          35 :         GEN Ng = idealnorm(nf, gel(gen,j));
    5773          35 :         GEN a = shifti(gel(xin,j), 1); /* xi(g_j^2) = e(a/D) */
    5774          35 :         GEN b = FpV_dotproduct(xin, isprincipalray(bnr,Ng), D);
    5775          35 :         GEN m = Fp_sub(a, b, D); /* xi(g_j/\bar{g_j}) = e(m/D) */
    5776          35 :         e = ugcd(e, itou(m)); if (e == 1) break;
    5777             :       }
    5778          28 :       n = itou(D) / e;
    5779          28 :       return n == 1? 4: 2*n;
    5780             :     }
    5781           0 :   return 0;
    5782             : }
    5783             : 
    5784             : static ulong
    5785          21 : radical_u(ulong n)
    5786          21 : { return zv_prod(gel(myfactoru(n),1)); }
    5787             : 
    5788             : /* list of fundamental discriminants unramified outside N, with sign s
    5789             :  * [s = 0 => no sign condition] */
    5790             : static GEN
    5791          21 : mfunram(long N, long s)
    5792             : {
    5793          21 :   long cN = radical_u(N >> vals(N)), p = 1, m = 1, l, c, i;
    5794          21 :   GEN D = mydivisorsu(cN), res;
    5795          21 :   l = lg(D);
    5796          21 :   if (s == 1) m = 0; else if (s == -1) p = 0;
    5797          21 :   res = cgetg(6*l - 5, t_VECSMALL);
    5798          21 :   c = 1;
    5799          21 :   if (!odd(N))
    5800             :   { /* d = 1 */
    5801          14 :     if (p) res[c++] = 8;
    5802          14 :     if (m) { res[c++] =-8; res[c++] =-4; }
    5803             :   }
    5804          56 :   for (i = 2; i < l; i++)
    5805             :   { /* skip d = 1, done above */
    5806          35 :     long d = D[i], d4 = d & 3L; /* d odd, squarefree, d4 = 1 or 3 */
    5807          35 :     if (d4 == 1) { if (p) res[c++] = d; }
    5808          28 :     else         { if (m) res[c++] =-d; }
    5809          35 :     if (!odd(N))
    5810             :     {
    5811          14 :       if (p) { res[c++] = 8*d; if (d4 == 3) res[c++] = 4*d; }
    5812          14 :       if (m) { res[c++] =-8*d; if (d4 == 1) res[c++] =-4*d; }
    5813             :     }
    5814             :   }
    5815          21 :   setlg(res, c); return res;
    5816             : }
    5817             : 
    5818             : /* Return 1 if F is definitely not S4 type; return 0 on failure. */
    5819             : static long
    5820           7 : mfisnotS4(long N, GEN w)
    5821             : {
    5822           7 :   GEN D = mfunram(N, 0);
    5823           7 :   long i, lD = lg(D), lw = lg(w);
    5824          56 :   for (i = 1; i < lD; i++)
    5825             :   {
    5826          49 :     long p, d = D[i], ok = 0;
    5827         154 :     for (p = 2; p < lw; p++)
    5828         154 :       if (w[p] && kross(d,p) == -1) { ok = 1; break; }
    5829          49 :     if (!ok) return 0;
    5830             :   }
    5831           7 :   return 1;
    5832             : }
    5833             : 
    5834             : /* Return 1 if Q(sqrt(5)) \not\subset Q(F), i.e. F is definitely not A5 type;
    5835             :  * return 0 on failure. */
    5836             : static long
    5837           7 : mfisnotA5(GEN F)
    5838             : {
    5839           7 :   GEN CHI = mf_get_CHI(F), P = mfcharpol(CHI), T, Q;
    5840             : 
    5841           7 :   if (mfcharorder(CHI) % 5 == 0) return 0;
    5842           7 :   T = mf_get_field(F); if (degpol(T) == 1) return 1;
    5843           7 :   if (degpol(P) > 1) T = rnfequation(P,T);
    5844           7 :   Q = gsubgs(pol_xn(2,varn(T)), 5);
    5845           7 :   return (typ(nfisincl(Q, T)) == t_INT);
    5846             : }
    5847             : 
    5848             : /* Given x = z + 1/z with z prim. root of unity of order n, find n */
    5849             : static long
    5850         357 : mffindrootof1(GEN u1)
    5851             : {
    5852         357 :   pari_sp av = avma;
    5853         357 :   GEN u0 = gen_2, u1k = u1, u2;
    5854         357 :   long c = 1;
    5855        1379 :   while (!gequalsg(2, liftpol_shallow(u1))) /* u1 = z^c + z^-c */
    5856             :   {
    5857         665 :     u2 = gsub(gmul(u1k, u1), u0);
    5858         665 :     u0 = u1; u1 = u2; c++;
    5859             :   }
    5860         357 :   return gc_long(av,c);
    5861             : }
    5862             : 
    5863             : /* we known that F is not dihedral */
    5864             : static long
    5865          21 : mfgaloistype_i(long N, GEN CHI, GEN F, long lim)
    5866             : {
    5867             :   forprime_t iter;
    5868          21 :   GEN v = mfcoefs_i(F,lim,1), w = zero_zv(lim);
    5869             :   ulong p;
    5870          21 :   u_forprime_init(&iter, 2, lim);
    5871         406 :   while((p = u_forprime_next(&iter)))
    5872             :   {
    5873             :     GEN u;
    5874             :     long n;
    5875         378 :     if (!(N%p)) continue;
    5876         357 :     u = gdiv(gsqr(gel(v, p+1)), mfchareval_i(CHI, p));
    5877         357 :     n = mffindrootof1(gsubgs(u,2));
    5878         357 :     if (n == 3) w[p] = 1;
    5879         357 :     if (n == 4) return -24; /* S4 */
    5880         350 :     if (n == 5) return -60; /* A5 */
    5881         350 :     if (n > 5) pari_err_DOMAIN("mfgaloistype", "form", "not a",
    5882             :                                strtoGENstr("cuspidal eigenform"), F);
    5883             :   }
    5884           7 :   if (mfisnotS4(N,w) && mfisnotA5(F)) return -12; /* A4 */
    5885           0 :   return 0; /* FAILURE */
    5886             : }
    5887             : 
    5888             : static GEN
    5889          49 : mfgaloistype0(long N, GEN CHI, GEN F, GEN DIH, long lim)
    5890             : {
    5891          49 :   pari_sp av = avma;
    5892          49 :   long t = mfisdihedral(F, DIH);
    5893          49 :   set_avma(av);
    5894          49 :   if (t) return stoi(t);
    5895             :   for(;;)
    5896             :   {
    5897          21 :     t = mfgaloistype_i(N, CHI, F, lim);
    5898          14 :     set_avma(av); if (t) return stoi(t);
    5899           0 :     lim += lim >> 1;
    5900             :   }
    5901             : }
    5902             : 
    5903             : /* If f is NULL, give all the galoistypes, otherwise just for f */
    5904             : GEN
    5905          63 : mfgaloistype(GEN NK, GEN f)
    5906             : {
    5907          63 :   pari_sp av = avma;
    5908          63 :   GEN CHI, T, F, DIH, mf = checkMF_i(NK);
    5909             :   long N, k, lL, i, lim, SB;
    5910             : 
    5911          63 :   if (f && !checkmf_i(f)) pari_err_TYPE("mfgaloistype", f);
    5912          56 :   if (mf)
    5913             :   {
    5914          21 :     N = MF_get_N(mf);
    5915          21 :     k = MF_get_k(mf);
    5916          21 :     CHI = MF_get_CHI(mf);
    5917             :   }
    5918             :   else
    5919             :   {
    5920          35 :     checkNK(NK, &N, &k, &CHI, 0);
    5921          35 :     mf = f? NULL: mfinit_i(NK, mf_NEW);
    5922             :   }
    5923          56 :   if (k != 1) pari_err_DOMAIN("mfgaloistype", "k", "!=", gen_1, stoi(k));
    5924          56 :   SB = mfsturmNk(N,1) + 1;
    5925          56 :   lim = maxss(200, 3*SB);
    5926          56 :   DIH = mfdihedralnew(N,CHI);
    5927          56 :   DIH = mkvec2(DIH, mfvectomat(DIH,SB,1));
    5928          56 :   if (f) return gerepileuptoint(av, mfgaloistype0(N,CHI, f, DIH, lim));
    5929          49 :   F = mfeigenbasis(mf); lL = lg(F);
    5930          49 :   T = cgetg(lL, t_VEC);
    5931          49 :   for (i=1; i < lL; i++) gel(T,i) = mfgaloistype0(N,CHI, gel(F,i), DIH, lim);
    5932          49 :   return gerepileupto(av, T);
    5933             : }
    5934             : 
    5935             : /******************************************************************/
    5936             : /*                   Find all dihedral forms.                     */
    5937             : /******************************************************************/
    5938             : /* lim >= 2 */
    5939             : static void
    5940           7 : consttabdihedral(long lim)
    5941           7 : { cache_set(cache_DIH, mfdihedralall(mkvecsmall2(1,lim))); }
    5942             : 
    5943             : /* a ideal coprime to bnr modulus */
    5944             : static long
    5945       77385 : mfdiheval(GEN bnr, GEN w, GEN a)
    5946             : {
    5947       77385 :   GEN L, cycn = gel(w,1), chin = gel(w,2);
    5948       77385 :   long ordmax = cycn[1];
    5949       77385 :   L = ZV_to_Flv(isprincipalray(bnr,a), ordmax);
    5950       77385 :   return Flv_dotproduct(chin, L, ordmax);
    5951             : }
    5952             : 
    5953             : /* A(x^k) mod T */
    5954             : static GEN
    5955       25711 : Galois(GEN A, long k, GEN T)
    5956             : {
    5957       25711 :   if (typ(A) != t_POL) return A;
    5958        9730 :   return gmod(RgX_inflate(A, k), T);
    5959             : }
    5960             : static GEN
    5961         609 : vecGalois(GEN v, long k, GEN T)
    5962             : {
    5963             :   long i, l;
    5964         609 :   GEN w = cgetg_copy(v,&l);
    5965         609 :   for (i = 1; i < l; i++) gel(w,i) = Galois(gel(v,i), k, T);
    5966         609 :   return w;
    5967             : }
    5968             : 
    5969             : static GEN
    5970      154392 : fix_pol(GEN S, GEN Pn, int *trace)
    5971             : {
    5972      154392 :   if (typ(S) != t_POL) return S;
    5973      107884 :   S = RgX_rem(S, Pn);
    5974      107884 :   if (typ(S) == t_POL)
    5975             :   {
    5976      107884 :     switch(lg(S))
    5977             :     {
    5978       37919 :       case 2: return gen_0;
    5979       17094 :       case 3: return gel(S,2);
    5980             :     }
    5981       52871 :     *trace = 1;
    5982             :   }
    5983       52871 :   return S;
    5984             : }
    5985             : 
    5986             : static GEN
    5987       10465 : dihan(GEN bnr, GEN w, GEN k0j, ulong lim)
    5988             : {
    5989       10465 :   GEN nf = bnr_get_nf(bnr), f = bid_get_ideal(bnr_get_bid(bnr));
    5990       10465 :   GEN v = zerovec(lim+1), cycn = gel(w,1), Tinit = gel(w,3);
    5991       10465 :   GEN Pn = gel(Tinit,lg(Tinit)==4? 2: 1);
    5992       10465 :   long j, ordmax = cycn[1], k0 = k0j[1], jdeg = k0j[2];
    5993       10465 :   long D = itos(nf_get_disc(nf)), vt = varn(Pn);
    5994       10465 :   int trace = 0;
    5995             :   ulong p, n;
    5996             :   forprime_t T;
    5997             : 
    5998       10465 :   if (!lim) return v;
    5999       10465 :   gel(v,2) = gen_1;
    6000       10465 :   u_forprime_init(&T, 2, lim);
    6001             :   /* fill in prime powers first */
    6002       10465 :   while ((p = u_forprime_next(&T)))
    6003             :   {
    6004             :     GEN vP, vchiP, S;
    6005             :     long k, lP;
    6006             :     ulong q, qk;
    6007       70980 :     if (kross(D,p) >= 0) q = p;
    6008       29232 :     else if (!(q = umuluu_le(p,p,lim))) continue;
    6009             :     /* q = Norm P */
    6010       47390 :     vP = idealprimedec(nf, utoipos(p));
    6011       47390 :     lP = lg(vP);
    6012       47390 :     vchiP = cgetg(lP, t_VECSMALL);
    6013      128674 :     for (j = k = 1; j < lP; j++)
    6014             :     {
    6015       81284 :       GEN P = gel(vP,j);
    6016       81284 :       if (!idealval(nf, f, P)) vchiP[k++] = mfdiheval(bnr,w,P);
    6017             :     }
    6018       47390 :     if (k == 1) continue;
    6019       45689 :     setlg(vchiP, k); lP = k;
    6020       45689 :     if (lP == 2)
    6021             :     { /* one prime above p not dividing f */
    6022       13993 :       long s, s0 = vchiP[1];
    6023       24738 :       for (qk=q, s = s0;; s = Fl_add(s,s0,ordmax))
    6024             :       {
    6025       35483 :         S = mygmodulo_lift(s, ordmax, gen_1, vt);
    6026       24738 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6027       24738 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6028             :       }
    6029             :     }
    6030             :     else /* two primes above p not dividing f */
    6031             :     {
    6032       31696 :       long s, s0 = vchiP[1], s1 = vchiP[2];
    6033       46928 :       for (qk=q, k = 1;; k++)
    6034       15232 :       { /* sum over a,b s.t. Norm( P1^a P2^b ) = q^k, i.e. a+b = k */
    6035             :         long a;
    6036       46928 :         GEN S = gen_0;
    6037      163723 :         for (a = 0; a <= k; a++)
    6038             :         {
    6039      116795 :           s = Fl_add(Fl_mul(a, s0, ordmax), Fl_mul(k-a, s1, ordmax), ordmax);
    6040      116795 :           S = gadd(S, mygmodulo_lift(s, ordmax, gen_1, vt));
    6041             :         }
    6042       46928 :         gel(v, qk+1) = fix_pol(S, Pn, &trace);
    6043       46928 :         if (!(qk = umuluu_le(qk,q,lim))) break;
    6044             :       }
    6045             :     }
    6046             :   }
    6047             :   /* complete with non-prime powers */
    6048      200116 :   for (n = 2; n <= lim; n++)
    6049             :   {
    6050      189651 :     GEN S, fa = myfactoru(n), P = gel(fa, 1), E = gel(fa, 2);
    6051             :     long q;
    6052      189651 :     if (lg(P) == 2) continue;
    6053             :     /* not a prime power */
    6054       82726 :     q = upowuu(P[1],E[1]);
    6055       82726 :     S = gmul(gel(v, q + 1), gel(v, n/q + 1));
    6056       82726 :     gel(v, n+1) = fix_pol(S, Pn, &trace);
    6057             :   }
    6058       10465 :   if (trace)
    6059             :   {
    6060        5369 :     if (lg(Tinit) == 4) v = QabV_tracerel(Tinit, jdeg, v);
    6061             :     /* Apply Galois Mod(k0, ordw) */
    6062        5369 :     if (k0 > 1) { GEN Pm = gel(Tinit,1); v = vecGalois(v, k0, Pm); }
    6063             :   }
    6064       10465 :   return v;
    6065             : }
    6066             : 
    6067             : /* as cyc_normalize for t_VECSMALL cyc */
    6068             : static GEN
    6069       13391 : cyc_normalize_zv(GEN cyc)
    6070             : {
    6071       13391 :   long i, o = cyc[1], l = lg(cyc); /* > 1 */
    6072       13391 :   GEN D = cgetg(l, t_VECSMALL);
    6073       13391 :   D[1] = o; for (i = 2; i < l; i++) D[i] = o / cyc[i];
    6074       13391 :   return D;
    6075             : }
    6076             : /* as char_normalize for t_VECSMALLs */
    6077             : static GEN
    6078       58975 : char_normalize_zv(GEN chi, GEN ncyc)
    6079             : {
    6080       58975 :   long i, l = lg(chi);
    6081       58975 :   GEN c = cgetg(l, t_VECSMALL);
    6082       58975 :   if (l > 1) {
    6083       58975 :     c[1] = chi[1];
    6084       58975 :     for (i = 2; i < l; i++) c[i] = chi[i] * ncyc[i];
    6085             :   }
    6086       58975 :   return c;
    6087             : }
    6088             : 
    6089             : static GEN
    6090        6097 : dihan_bnf(long D)
    6091        6097 : { setrand(gen_1); return Buchall(quadpoly(stoi(D)), 0, LOWDEFAULTPREC); }
    6092             : static GEN
    6093       20391 : dihan_bnr(GEN bnf, GEN A)
    6094       20391 : { setrand(gen_1); return bnrinit0(bnf, A, 1); }
    6095             : 
    6096             : /* Hecke xi * (D/.) = Dirichlet chi, return v in Q^r st chi(g_i) = e(v[i]).
    6097             :  * cycn = cyc_normalize_zv(bnr.cyc), chin = char_normalize_zv(chi,cyc) */
    6098             : static GEN
    6099       17206 : bnrchartwist2conrey(GEN chin, GEN cycn, GEN bnrconreyN, GEN kroconreyN)
    6100             : {
    6101       17206 :   long l = lg(bnrconreyN), c1 = cycn[1], i;
    6102       17206 :   GEN v = cgetg(l, t_COL);
    6103       62566 :   for (i = 1; i < l; i++)
    6104             :   {
    6105       45360 :     GEN d = sstoQ(zv_dotproduct(chin, gel(bnrconreyN,i)), c1);
    6106       45360 :     if (kroconreyN[i] < 0) d = gadd(d, ghalf);
    6107       45360 :     gel(v,i) = d;
    6108             :   }
    6109       17206 :   return v;
    6110             : }
    6111             : 
    6112             : /* chi(g_i) = e(v[i]) denormalize wrt Conrey generators orders */
    6113             : static GEN
    6114       17206 : conreydenormalize(GEN znN, GEN v)
    6115             : {
    6116       17206 :   GEN gcyc = znstar_get_conreycyc(znN), w;
    6117       17206 :   long l = lg(v), i;
    6118       17206 :   w = cgetg(l, t_COL);
    6119       62566 :   for (i = 1; i < l; i++)
    6120       45360 :     gel(w,i) = modii(gmul(gel(v,i), gel(gcyc,i)), gel(gcyc,i));
    6121       17206 :   return w;
    6122             : }
    6123             : 
    6124             : static long
    6125       41769 : Miyake(GEN vchi, GEN gb, GEN cycn)
    6126             : {
    6127       41769 :   long i, e = cycn[1], lb = lg(gb);
    6128       41769 :   GEN v = char_normalize_zv(vchi, cycn);
    6129       62132 :   for (i = 1; i < lb; i++)
    6130       49833 :     if ((zv_dotproduct(v, gel(gb,i)) -  v[i]) % e) return 1;
    6131       12299 :   return 0;
    6132             : }
    6133             : 
    6134             : /* list of Hecke characters not induced by a Dirichlet character up to Galois
    6135             :  * conjugation, whose conductor is bnr.cond; cycn = cyc_normalize(bnr.cyc)*/
    6136             : static GEN
    6137       13391 : mklvchi(GEN bnr, GEN con, GEN cycn)
    6138             : {
    6139       13391 :   GEN gb = NULL, cyc = bnr_get_cyc(bnr), cycsmall = ZV_to_zv(cyc);
    6140       13391 :   GEN vchi = cyc2elts(cycsmall);
    6141       13391 :   long ordmax = cycsmall[1], c, i, l;
    6142       13391 :   if (con)
    6143             :   {
    6144        3892 :     GEN g = bnr_get_gen(bnr), nf = bnr_get_nf(bnr);
    6145        3892 :     long lg = lg(g);
    6146        3892 :     gb = cgetg(lg, t_VEC);
    6147        9135 :     for (i = 1; i < lg; i++)
    6148        5243 :       gel(gb,i) = ZV_to_zv(isprincipalray(bnr, galoisapply(nf, con, gel(g,i))));
    6149             :   }
    6150       13391 :   l = lg(vchi);
    6151      151725 :   for (i = c = 1; i < l; i++)
    6152             :   {
    6153      138334 :     GEN chi = gel(vchi,i);
    6154      138334 :     if (!con || Miyake(chi, gb, cycn)) gel(vchi, c++) = Flv_to_ZV(chi);
    6155             :   }
    6156       13391 :   setlg(vchi, c); l = c;
    6157      139426 :   for (i = 1; i < l; i++)
    6158             :   {
    6159      126035 :     GEN chi = gel(vchi,i);
    6160             :     long n;
    6161      126035 :     if (!chi) continue;
    6162      527289 :     for (n = 2; n < ordmax; n++)
    6163      482748 :       if (ugcd(n, ordmax) == 1)
    6164             :       {
    6165      198597 :         GEN tmp = vecmodii(gmulsg(n, chi), cyc);
    6166             :         long j;
    6167     3809050 :         for (j = i+1; j < l; j++)
    6168     3610453 :           if (gel(vchi,j) && gequal(gel(vchi,j), tmp)) gel(vchi,j) = NULL;
    6169             :       }
    6170             :   }
    6171      139426 :   for (i = c = 1; i < l; i++)
    6172             :   {
    6173      126035 :     GEN chi = gel(vchi,i);
    6174      126035 :     if (chi && bnrisconductor(bnr, chi)) gel(vchi, c++) = chi;
    6175             :   }
    6176       13391 :   setlg(vchi, c); return vchi;
    6177             : }
    6178             : 
    6179             : /* con = NULL if D > 0 or if D < 0 and id != idcon. */
    6180             : static GEN
    6181       16835 : mfdihedralcommon(GEN bnf, GEN id, GEN znN, GEN kroconreyN, long N, long D, GEN con)
    6182             : {
    6183             :   GEN bnr, bnrconreyN, cyc, cycn, cycN, Lvchi, res, g, P;
    6184             :   long i, j, ordmax, l, lc, deghecke, degrel;
    6185             : 
    6186       16835 :   bnr = dihan_bnr(bnf, id);
    6187       16835 :   cyc = ZV_to_zv( bnr_get_cyc(bnr) );
    6188       16835 :   lc = lg(cyc); if (lc == 1) return NULL;
    6189             : 
    6190       13391 :   g = znstar_get_conreygen(znN); l = lg(g);
    6191       13391 :   bnrconreyN = cgetg(l, t_VEC);
    6192       50288 :   for (i = 1; i < l; i++)
    6193       36897 :     gel(bnrconreyN,i) = ZV_to_zv(isprincipalray(bnr,gel(g,i)));
    6194             : 
    6195       13391 :   cycn = cyc_normalize_zv(cyc);
    6196       13391 :   cycN = ZV_to_zv(znstar_get_cyc(znN));
    6197       13391 :   ordmax = cyc[1];
    6198       13391 :   P = polcyclo(ord_canon(ordmax), fetch_user_var("t"));
    6199       13391 :   deghecke = myeulerphiu(ordmax);
    6200       13391 :   Lvchi = mklvchi(bnr, con, cycn); l = lg(Lvchi);
    6201       13391 :   if (l == 1) return NULL;
    6202        7917 :   res = cgetg(l, t_VEC);
    6203       25123 :   for (j = 1; j < l; j++)
    6204             :   {
    6205       17206 :     GEN T, Tinit, v, vchi = ZV_to_zv(gel(Lvchi,j));
    6206       17206 :     GEN chi, chin = char_normalize_zv(vchi, cycn);
    6207             :     long ordw, vnum, k0;
    6208       17206 :     v = bnrchartwist2conrey(chin, cycn, bnrconreyN, kroconreyN);
    6209       17206 :     ordw = itou(Q_denom(v));
    6210       17206 :     Tinit = Qab_trace_init(P, ord_canon(ordmax), ord_canon(ordw));
    6211       17206 :     chi = conreydenormalize(znN, v);
    6212       17206 :     vnum = itou(znconreyexp(znN, chi));
    6213       17206 :     chi = ZV_to_zv(znconreychar(znN,chi));
    6214       17206 :     degrel = deghecke / myeulerphiu(ordw);
    6215       17206 :     k0 = zv_cyc_minimize(cycN, chi, coprimes_zv(ordw));
    6216       17206 :     vnum = Fl_powu(vnum, k0, N);
    6217             :     /* encodes degrel forms: jdeg = 0..degrel-1 */
    6218       17206 :     T = mkvecsmalln(6, N, k0, vnum, D, ordmax, degrel);
    6219       17206 :     gel(res,j) = mkvec3(T, id, mkvec3(cycn,chin,Tinit));
    6220             :   }
    6221        7917 :   return res;
    6222             : }
    6223             : 
    6224             : /* Append to v all dihedral weight 1 forms coming from D, if fundamental. */
    6225             : /* B a t_VECSMALL: if #B=1, only that level; if B=[Bmin,Bmax], Bmin <= Bmax:
    6226             :  * between those levels. */
    6227             : static void
    6228        9289 : append_dihedral(GEN v, long D, GEN B)
    6229             : {
    6230        9289 :   long Da = labs(D), no, N, i, numi, ct, min, max;
    6231             :   GEN bnf, con, LI, resall, varch;
    6232             :   pari_sp av;
    6233             : 
    6234        9289 :   if (lg(B) == 2)
    6235             :   {
    6236           0 :     long b = B[1], m = D > 0? 3: 1;
    6237           0 :     min = b / Da;
    6238           0 :     if (b % Da || min < m) return;
    6239           0 :     max = min;
    6240             :   }
    6241             :   else
    6242             :   { /* assume B[1] < B[2] */
    6243        9289 :     min = (B[1] + Da-1)/Da;
    6244        9289 :     max = B[2]/Da;
    6245             :   }
    6246        9289 :   if (!sisfundamental(D)) return;
    6247             : 
    6248        2842 :   av = avma;
    6249        2842 :   bnf = dihan_bnf(D);
    6250        2842 :   con = gel(galoisconj(bnf,gen_1), 2);
    6251        2842 :   LI = ideallist(bnf, max);
    6252        2842 :   numi = 0; for (i = min; i <= max; i++) numi += lg(gel(LI, i)) - 1;
    6253        2842 :   if (D > 0)
    6254             :   {
    6255         707 :     numi <<= 1;
    6256         707 :     varch = mkvec2(mkvec2(gen_1,gen_0), mkvec2(gen_0,gen_1));
    6257             :   }
    6258             :   else
    6259        2135 :     varch = NULL;
    6260        2842 :   resall = cgetg(numi+1, t_VEC); ct = 1;
    6261       27503 :   for (no = min; no <= max; no++)
    6262             :   {
    6263             :     GEN LIs, znN, conreyN, kroconreyN;
    6264             :     long flcond, lgc, lglis;
    6265       24661 :     if (D < 0)
    6266       15043 :       flcond = (no == 2 || no == 3 || (no == 4 && (D&7L)==1));
    6267             :     else
    6268        9618 :       flcond = (no == 4 && (D&7L) != 1);
    6269       24661 :     if (flcond) continue;
    6270       22302 :     LIs = gel(LI, no);
    6271       22302 :     N = Da*no;
    6272       22302 :     znN = znstar0(utoi(N), 1);
    6273       22302 :     conreyN = znstar_get_conreygen(znN); lgc = lg(conreyN);
    6274       22302 :     kroconreyN = cgetg(lgc, t_VECSMALL);
    6275       22302 :     for (i = 1; i < lgc; i++) kroconreyN[i] = krosi(D, gel(conreyN, i));
    6276       22302 :     lglis = lg(LIs);
    6277       43876 :     for (i = 1; i < lglis; i++)
    6278             :     {
    6279       21574 :       GEN id = gel(LIs, i), idcon, conk;
    6280             :       long j, inf, maxinf;
    6281       21574 :       if (typ(id) == t_INT) continue;
    6282       14077 :       idcon = galoisapply(bnf, con, id);
    6283       14077 :       conk = (D < 0 && gequal(idcon, id)) ? con : NULL;
    6284       42294 :       for (j = i; j < lglis; j++)
    6285       28217 :         if (gequal(idcon, gel(LIs, j))) gel(LIs, j) = gen_0;
    6286       14077 :       maxinf = (D < 0 || gequal(idcon,id))? 1: 2;
    6287       30912 :       for (inf = 1; inf <= maxinf; inf++)
    6288             :       {
    6289       16835 :         GEN ide = (D > 0)? mkvec2(id, gel(varch,inf)): id;
    6290       16835 :         GEN res = mfdihedralcommon(bnf, ide, znN, kroconreyN, N, D, conk);
    6291       16835 :         if (res) gel(resall, ct++) = res;
    6292             :       }
    6293             :     }
    6294             :   }
    6295        2842 :   if (ct == 1) set_avma(av);
    6296             :   else
    6297             :   {
    6298        2394 :     setlg(resall, ct);
    6299        2394 :     vectrunc_append(v, gerepilecopy(av, shallowconcat1(resall)));
    6300             :   }
    6301             : }
    6302             : 
    6303             : static long
    6304       21021 : di_N(GEN a) { return gel(a,1)[1]; }
    6305             : /* All primitive dihedral wt1 forms: LIM a t_VECSMALL with a single component
    6306             :  * (only level LIM) or 2 components [m,M], m < M (between m and M) */
    6307             : static GEN
    6308           7 : mfdihedralall(GEN LIM)
    6309             : {
    6310             :   GEN res, z;
    6311             :   long limD, ct, i, l1, l2;
    6312             : 
    6313           7 :   if (lg(LIM) == 2) l1 = l2 = LIM[1]; else { l1 = LIM[1]; l2 = LIM[2]; }
    6314           7 :   limD = l2;
    6315           7 :   res = vectrunc_init(2*limD);
    6316           7 :   if (l1 == l2)
    6317             :   {
    6318           0 :     GEN D = mydivisorsu(l1);
    6319           0 :     long l = lg(D), j;
    6320           0 :     for (j = 2; j < l; j++)
    6321             :     {
    6322           0 :       long d = D[j];
    6323           0 :       append_dihedral(res, -d, LIM);
    6324           0 :       if (d >= 5 && D[l-j] >= 3) append_dihedral(res, d, LIM);
    6325             :     }
    6326             :   }
    6327             :   else
    6328             :   {
    6329             :     long D;
    6330           7 :     for (D = -3; D >= -limD; D--) append_dihedral(res, D, LIM);
    6331           7 :     limD /= 3;
    6332           7 :     for (D = 5; D <= limD;   D++) append_dihedral(res, D, LIM);
    6333             :   }
    6334           7 :   if (l1 == l2) return gel(res,1); /* single level */
    6335           7 :   ct = lg(res);
    6336           7 :   if (ct > 1)
    6337             :   { /* concat and sort wrt N */
    6338           7 :     res = shallowconcat1(res);
    6339           7 :     res = vecpermute(res, indexvecsort(res, mkvecsmall(1)));
    6340           7 :     ct = lg(res);
    6341             :   }
    6342           7 :   z = const_vec(l2-l1+1, cgetg(1,t_VEC));
    6343        3836 :   for (i = 1; i < ct;)
    6344             :   { /* regroup result sharing the same N */
    6345        3822 :     long n = di_N(gel(res,i)), j = i+1, k;
    6346             :     GEN v;
    6347        3822 :     while (j < ct && di_N(gel(res,j)) == n) j++;
    6348        3822 :     n -= l1-1;
    6349        3822 :     gel(z, n) = v = cgetg(j-i+1, t_VEC);
    6350        3822 :     for (k = 1; i < j; k++,i++) gel(v,k) = gel(res,i);
    6351             :   }
    6352           7 :   return z;
    6353             : }
    6354             : 
    6355             : /* return [vF, index], where vecpermute(vF,index) generates dihedral forms
    6356             :  * for character CHI */
    6357             : static GEN
    6358       23429 : mfdihedralnew_i(long N, GEN CHI)
    6359             : {
    6360             :   GEN bnf, Tinit, Pm, vf, M, V, NK, SP;
    6361             :   long Dold, d, ordw, i, SB, c, l, k0, k1, chino, chinoorig, lv;
    6362             : 
    6363       23429 :   SP = cache_get(cache_DIH, N);
    6364       23429 :   if (!SP) SP = mfdihedralall(mkvecsmall(N));
    6365       23429 :   lv = lg(SP); if (lv == 1) return NULL;
    6366       11151 :   CHI = mfcharinduce(CHI,N);
    6367       11151 :   ordw = mfcharorder(CHI);
    6368       11151 :   chinoorig = mfcharno(CHI);
    6369       11151 :   k0 = mfconreyminimize(CHI);
    6370       11151 :   chino = Fl_powu(chinoorig, k0, N);
    6371       11151 :   k1 = Fl_inv(k0 % ordw, ordw);
    6372       11151 :   V = cgetg(lv, t_VEC);
    6373       11151 :   d = 0;
    6374       34615 :   for (i = l = 1; i < lv; i++)
    6375             :   {
    6376       23464 :     GEN sp = gel(SP,i), T = gel(sp,1);
    6377       23464 :     if (T[3] != chino) continue;
    6378        3556 :     d += T[6];
    6379        3556 :     if (k1 != 1)
    6380             :     {
    6381          77 :       GEN t = leafcopy(T);
    6382          77 :       t[3] = chinoorig;
    6383          77 :       t[2] = (t[2]*k1)%ordw;
    6384          77 :       sp = mkvec4(t, gel(sp,2), gel(sp,3), gel(sp,4));
    6385             :     }
    6386        3556 :     gel(V, l++) = sp;
    6387             :   }
    6388       11151 :   setlg(V, l); /* dihedral forms of level N and character CHI */
    6389       11151 :   if (l == 1) return NULL;
    6390             : 
    6391        2331 :   SB = myeulerphiu(ordw) * mfsturmNk(N,1) + 1;
    6392        2331 :   M = cgetg(d+1, t_MAT);
    6393        2331 :   vf = cgetg(d+1, t_VEC);
    6394        2331 :   NK = mkNK(N, 1, CHI);
    6395        2331 :   bnf = NULL; Dold = 0;
    6396        5887 :   for (i = c = 1; i < l; i++)
    6397             :   { /* T = [N, k0, conreyno, D, ordmax, degrel] */
    6398        3556 :     GEN bnr, Vi = gel(V,i), T = gel(Vi,1), id = gel(Vi,2), w = gel(Vi,3);
    6399        3556 :     long jdeg, k0i = T[2], D = T[4], degrel = T[6];
    6400             : 
    6401        3556 :     if (D != Dold) { Dold = D; bnf = dihan_bnf(D); }
    6402        3556 :     bnr = dihan_bnr(bnf, id);
    6403       10430 :     for (jdeg = 0; jdeg < degrel; jdeg++,c++)
    6404             :     {
    6405        6874 :       GEN k0j = mkvecsmall2(k0i, jdeg), an = dihan(bnr, w, k0j, SB);
    6406        6874 :       settyp(an, t_COL); gel(M,c) = Q_primpart(an);
    6407        6874 :       gel(vf,c) = tag3(t_MF_DIHEDRAL, NK, bnr, w, k0j);
    6408             :     }
    6409             :   }
    6410        2331 :   Tinit = gmael3(V,1,3,3); Pm = gel(Tinit,1);
    6411        2331 :   V = QabM_indexrank(M, degpol(Pm)==1? NULL: Pm, ord_canon(ordw));
    6412        2331 :   return mkvec2(vf,gel(V,2));
    6413             : }
    6414             : static long
    6415       15764 : mfdihedralnewdim(long N, GEN CHI)
    6416             : {
    6417       15764 :   pari_sp av = avma;
    6418       15764 :   GEN S = mfdihedralnew_i(N, CHI);
    6419       15764 :   return gc_long(av, S? lg(gel(S,2))-1: 0);
    6420             : }
    6421             : static GEN
    6422        7665 : mfdihedralnew(long N, GEN CHI)
    6423             : {
    6424        7665 :   pari_sp av = avma;
    6425        7665 :   GEN S = mfdihedralnew_i(N, CHI);
    6426        7665 :   if (!S) { set_avma(av); return cgetg(1, t_VEC); }
    6427         777 :   return vecpermute(gel(S,1), gel(S,2));
    6428             : }
    6429             : 
    6430             : static long
    6431        7014 : mfdihedralcuspdim(long N, GEN CHI)
    6432             : {
    6433        7014 :   pari_sp av = avma;
    6434             :   GEN D, CHIP;
    6435             :   long F, i, lD, dim;
    6436             : 
    6437        7014 :   CHIP = mfchartoprimitive(CHI, &F);
    6438        7014 :   D = mydivisorsu(N/F); lD = lg(D);
    6439        7014 :   dim = mfdihedralnewdim(N, CHI); /* d = 1 */
    6440       15764 :   for (i = 2; i < lD; i++)
    6441             :   {
    6442        8750 :     long d = D[i], M = N/d, a = mfdihedralnewdim(M, CHIP);
    6443        8750 :     if (a) dim += a * mynumdivu(d);
    6444             :   }
    6445        7014 :   return gc_long(av,dim);
    6446             : }
    6447             : 
    6448             : static GEN
    6449        5446 : mfbdall(GEN E, long N)
    6450             : {
    6451        5446 :   GEN v, D = mydivisorsu(N);
    6452        5446 :   long i, j, nD = lg(D) - 1, nE = lg(E) - 1;
    6453        5446 :   v = cgetg(nD*nE + 1, t_VEC);
    6454        6965 :   for (j = 1; j <= nE; j++)
    6455             :   {
    6456        1519 :     GEN Ej = gel(E, j);
    6457        1519 :     for (i = 0; i < nD; i++) gel(v, i*nE + j) = mfbd_i(Ej, D[i+1]);
    6458             :   }
    6459        5446 :   return v;
    6460             : }
    6461             : static GEN
    6462        3381 : mfdihedralcusp(long N, GEN CHI)
    6463             : {
    6464        3381 :   pari_sp av = avma;
    6465             :   GEN D, CHIP, z;
    6466             :   long F, i, lD;
    6467             : 
    6468        3381 :   CHIP = mfchartoprimitive(CHI, &F);
    6469        3381 :   D = mydivisorsu(N/F); lD = lg(D);
    6470        3381 :   z = cgetg(lD, t_VEC);
    6471        3381 :   gel(z,1) = mfdihedralnew(N, CHI);
    6472        7609 :   for (i = 2; i < lD; i++) /* skip 1 */
    6473             :   {
    6474        4228 :     long d = D[i], M = N / d;
    6475        4228 :     GEN LF = mfdihedralnew(M, mfcharinduce(CHIP, M));
    6476        4228 :     gel(z,i) = mfbdall(LF, d);
    6477             :   }
    6478        3381 :   return gerepilecopy(av, shallowconcat1(z));
    6479             : }
    6480             : 
    6481             : /* used to decide between ratlift and comatrix for ZM_inv; ratlift is better
    6482             :  * when N has many divisors */
    6483             : static int
    6484        2317 : abundant(ulong N) { return mynumdivu(N) >= 8; }
    6485             : 
    6486             : /* CHI an mfchar */
    6487             : static int
    6488         294 : cmp_ord(void *E, GEN a, GEN b)
    6489             : {
    6490         294 :   GEN chia = MF_get_CHI(a), chib = MF_get_CHI(b);
    6491         294 :   (void)E; return cmpii(gmfcharorder(chia), gmfcharorder(chib));
    6492             : }
    6493             : /* mfinit structure.
    6494             : -- mf[1] contains [N,k,CHI,space],
    6495             : -- mf[2] contains vector of closures of Eisenstein series, empty if not
    6496             :    full space.
    6497             : -- mf[3] contains vector of closures, so #mf[3] = dimension of cusp/new space.
    6498             : -- mf[4] contains the corresponding indices: either j for T(j)tf if newspace,
    6499             :    or [M,j,d] for B(d)T(j)tf_M if cuspspace or oldspace.
    6500             : -- mf[5] contains the matrix M of first coefficients of basis, never cleaned.
    6501             :  * NK is either [N,k] or [N,k,CHI].
    6502             :  * mfinit does not do the splitting, only the basis generation. */
    6503             : 
    6504             : /* Set flraw to 1 if do not need mf[5]: no mftobasis etc..., only the
    6505             :    expansions of the basis elements are needed. */
    6506             : 
    6507             : static GEN
    6508        4508 : mfinit_Nkchi(long N, long k, GEN CHI, long space, long flraw)
    6509             : {
    6510        4508 :   GEN M = NULL, mf = NULL, mf1 = mkvec4(utoi(N), stoi(k), CHI, utoi(space));
    6511        4508 :   long sb = mfsturmNk(N, k);
    6512             :   cachenew_t cache;
    6513        4508 :   if (k < 0 || badchar(N, k, CHI)) return mfEMPTY(mf1);
    6514        4473 :   if (k == 0) /*nothing*/;
    6515        4431 :   else if (k == 1)
    6516             :   {
    6517         259 :     switch (space)
    6518             :     {
    6519             :       case mf_NEW:
    6520             :       case mf_FULL:
    6521         231 :       case mf_CUSP: mf = mfwt1init(N, CHI, NULL, space, flraw); break;
    6522          14 :       case mf_EISEN:break;
    6523           7 :       case mf_OLD: pari_err_IMPL("mfinit in weight 1 for old space");
    6524           7 :       default: pari_err_FLAG("mfinit");
    6525             :     }
    6526             :   }
    6527             :   else /* k >= 2 */
    6528             :   {
    6529        4172 :     long ord = mfcharorder_canon(CHI);
    6530        4172 :     GEN z = NULL, P = (ord == 1)? NULL: mfcharpol(CHI);
    6531        4172 :     switch(space)
    6532             :     {
    6533             :       case mf_EISEN:
    6534         105 :         break;
    6535             :       case mf_NEW:
    6536        1176 :         mf = mfnewinit(N, k, CHI, &cache, 1);
    6537        1176 :         if (mf && !flraw) { M = MF_get_M(mf); z = MF_get_Mindex(mf); }
    6538        1176 :         break;
    6539             :       case mf_OLD:
    6540             :       case mf_CUSP:
    6541             :       case mf_FULL:
    6542        2884 :         mf = mfinitcusp(N, k, CHI, &cache, space);
    6543        2884 :         if (mf && !flraw)
    6544             :         {
    6545        2058 :           GEN S = MF_get_S(mf);
    6546        2058 :           M = bhnmat_extend(M, sb+1, 1, S, &cache);
    6547        2058 :           if (space != mf_FULL) gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6548             :         }
    6549        2884 :         dbg_cachenew(&cache);
    6550        2884 :         break;
    6551           7 :       default: pari_err_FLAG("mfinit");
    6552             :     }
    6553        4165 :     if (z) gel(mf,5) = mfclean2(M, z, P, ord);
    6554             :   }
    6555        4452 :   if (!mf) mf = mfEMPTY(mf1);
    6556             :   else
    6557             :   {
    6558        3584 :     gel(mf,1) = mf1;
    6559        3584 :     if (flraw) gel(mf,5) = zerovec(3);
    6560             :   }
    6561        4452 :   if (!space_is_cusp(space))
    6562             :   {
    6563         637 :     GEN E = mfeisensteinbasis(N, k, CHI);
    6564         637 :     gel(mf,2) = E;
    6565         637 :     if (!flraw)
    6566             :     {
    6567         427 :       if (M)
    6568         168 :         M = shallowconcat(mfvectomat(E, sb+1, 1), M);
    6569             :       else
    6570         259 :         M = mfcoefs_mf(mf, sb+1, 1);
    6571         427 :       gel(mf,5) = mfcleanCHI(M, CHI, abundant(N));
    6572             :     }
    6573             :   }
    6574        4452 :   return mf;
    6575             : }
    6576             : 
    6577             : /* mfinit for k = nk/dk */
    6578             : static GEN
    6579        2429 : mfinit_Nndkchi(long N, long nk, long dk, GEN CHI, long space, long flraw)
    6580         210 : { return (dk == 2)? mf2init_Nkchi(N, nk >> 1, CHI, space, flraw)
    6581        2639 :                   : mfinit_Nkchi(N, nk, CHI, space, flraw); }
    6582             : static GEN
    6583        3094 : mfinit_i(GEN NK, long space)
    6584             : {
    6585             :   GEN CHI, mf;
    6586             :   long N, k, dk, joker;
    6587        3094 :   if (checkmf_i(NK))
    6588             :   {
    6589         126 :     N = mf_get_N(NK);
    6590         126 :     Qtoss(mf_get_gk(NK), &k, &dk);
    6591         126 :     CHI = mf_get_CHI(NK);
    6592             :   }
    6593        2968 :   else if ((mf = checkMF_i(NK)))
    6594             :   {
    6595          21 :     long s = MF_get_space(mf);
    6596          21 :     if (s == space) return mf;
    6597          21 :     Qtoss(MF_get_gk(mf), &k, &dk);
    6598          21 :     if (dk == 1 && k > 1 && space == mf_NEW && (s == mf_CUSP || s == mf_FULL))
    6599          21 :       return mfinittonew(mf);
    6600           0 :     N = MF_get_N(mf);
    6601           0 :     CHI = MF_get_CHI(mf);
    6602             :   }
    6603             :   else
    6604        2947 :     checkNK2(NK, &N, &k, &dk, &CHI, 1);
    6605        3052 :   joker = !CHI || typ(CHI) == t_COL;
    6606        3052 :   if (joker)
    6607             :   {
    6608        1141 :     GEN mf, vCHI = CHI;
    6609             :     long i, j, l;
    6610        1141 :     if (CHI && lg(CHI) == 1) return cgetg(1,t_VEC);
    6611        1134 :     if (k < 0) return mfEMPTYall(N, sstoQ(k,dk), CHI, space);
    6612        1120 :     if (k == 1 && dk == 1 && space != mf_EISEN)
    6613         483 :     {
    6614             :       GEN TMP, gN, gs;
    6615        1085 :       if (space != mf_CUSP && space != mf_NEW)
    6616           0 :         pari_err_IMPL("mfinit([N,1,wildcard], space != cusp or new space)");
    6617        1085 :       if (wt1empty(N)) return mfEMPTYall(N, gen_1, CHI, space);
    6618         483 :       vCHI = mfwt1chars(N,vCHI);
    6619         483 :       l = lg(vCHI); mf = cgetg(l, t_VEC); if (l == 1) return mf;
    6620         483 :       TMP = mfwt1_pre(N); gN = utoipos(N); gs = utoi(space);
    6621        3717 :       for (i = j = 1; i < l; i++)
    6622             :       {
    6623        3234 :         GEN c = gel(vCHI,i), z = mfwt1init(N, c, TMP, space, 0);
    6624        3234 :         if (CHI && !z) z = mfEMPTY(mkvec4(gN,gen_1,c,gs));
    6625        3234 :         if (z) gel(mf, j++) = z;
    6626             :       }
    6627             :     }
    6628             :     else
    6629             :     {
    6630          35 :       vCHI = mfchars(N,k,dk,vCHI);
    6631          35 :       l = lg(vCHI); mf = cgetg(l, t_VEC);
    6632         119 :       for (i = j = 1; i < l; i++)
    6633             :       {
    6634          84 :         GEN v = mfinit_Nndkchi(N, k, dk, gel(vCHI,i), space, 0);
    6635          84 :         if (MF_get_dim(v) || CHI) gel(mf, j++) = v;
    6636             :       }
    6637             :     }
    6638         518 :     setlg(mf,j);
    6639         518 :     if (!CHI) gen_sort_inplace(mf, NULL, &cmp_ord, NULL);
    6640         518 :     return mf;
    6641             :   }
    6642        1911 :   return mfinit_Nndkchi(N, k, dk, CHI, space, 0);
    6643             : }
    6644             : GEN
    6645        2142 : mfinit(GEN NK, long space)
    6646             : {
    6647        2142 :   pari_sp av = avma;
    6648        2142 :   return gerepilecopy(av, mfinit_i(NK, space));
    6649             : }
    6650             : 
    6651             : /* UTILITY FUNCTIONS */
    6652             : static void
    6653         357 : cusp_canon(GEN cusp, long N, long *pA, long *pC)
    6654             : {
    6655         357 :   pari_sp av = avma;
    6656             :   long A, C, tc, cg;
    6657         357 :   if (N <= 0) pari_err_DOMAIN("mfcuspwidth","N","<=",gen_0,stoi(N));
    6658         350 :   if (!cusp || (tc = typ(cusp)) == t_INFINITY) { *pA = 1; *pC = N; return; }
    6659         343 :   if (tc != t_INT && tc != t_FRAC) pari_err_TYPE("checkcusp", cusp);
    6660         343 :   Qtoss(cusp, &A,&C);
    6661         343 :   if (N % C)
    6662             :   {
    6663             :     ulong uC;
    6664          14 :     long u = Fl_invgen((C-1)%N + 1, N, &uC);
    6665          14 :     A = Fl_mul(A, u, N);
    6666          14 :     C = (long)uC;
    6667             :   }
    6668         343 :   cg = ugcd(C, N/C);
    6669         343 :   while (ugcd(A, N) > 1) A += cg;
    6670         343 :   *pA = A % N; *pC = C; set_avma(av);
    6671             : }
    6672             : static long
    6673         805 : mfcuspcanon_width(long N, long C)
    6674         805 : { return (!C || C == N)? 1 : N / ugcd(N, Fl_sqr(umodsu(C,N),N)); }
    6675             : /* v = [a,c] a ZC, width of cusp (a:c) */
    6676             : static long
    6677        7399 : mfZC_width(long N, GEN v)
    6678             : {
    6679        7399 :   ulong C = umodiu(gel(v,2), N);
    6680        7399 :   return (C == 0)? 1: N / ugcd(N, Fl_sqr(C,N));
    6681             : }
    6682             : long
    6683         161 : mfcuspwidth(GEN gN, GEN cusp)
    6684             : {
    6685         161 :   long N = 0, A, C;
    6686             :   GEN mf;
    6687         161 :   if (typ(gN) == t_INT) N = itos(gN);
    6688          42 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    6689           0 :   else pari_err_TYPE("mfcuspwidth", gN);
    6690         161 :   cusp_canon(cusp, N, &A, &C);
    6691         154 :   return mfcuspcanon_width(N, C);
    6692             : }
    6693             : 
    6694             : /* Q a t_INT */
    6695             : static GEN
    6696          14 : findq(GEN al, GEN Q)
    6697             : {
    6698             :   long n;
    6699          14 :   if (typ(al) == t_FRAC && cmpii(gel(al,2), Q) <= 0)
    6700           0 :     return mkvec(mkvec2(gel(al,1), gel(al,2)));
    6701          14 :   n = 1 + (long)ceil(2.0781*gtodouble(glog(Q, LOWDEFAULTPREC)));
    6702          14 :   return contfracpnqn(gboundcf(al,n), n);
    6703             : }
    6704             : static GEN
    6705          91 : findqga(long N, GEN z)
    6706             : {
    6707          91 :   GEN Q, LDC, CK = NULL, DK = NULL, ma, x, y = imag_i(z);
    6708             :   long j, l;
    6709          91 :   if (gcmpgs(gmulsg(2*N, y), 1) >= 0) return NULL;
    6710          14 :   x = real_i(z);
    6711          14 :   Q = ground(ginv(gsqrt(gmulsg(N, y), LOWDEFAULTPREC)));
    6712          14 :   LDC = findq(gmulsg(-N,x), Q);
    6713          14 :   ma = gen_1; l = lg(LDC);
    6714          35 :   for (j = 1; j < l; j++)
    6715             :   {
    6716          21 :     GEN D, DC = gel(LDC,j), C1 = gel(DC,2);
    6717          21 :     if (cmpii(C1,Q) > 0) break;
    6718          21 :     D = gel(DC,1);
    6719          21 :     if (ugcdiu(D,N) == 1)
    6720             :     {
    6721           7 :       GEN C = mului(N, C1), den;
    6722           7 :       den = gadd(gsqr(gmul(C,y)), gsqr(gadd(D, gmul(C,x))));
    6723           7 :       if (gcmp(den, ma) < 0) { ma = den; CK = C; DK = D; }
    6724             :     }
    6725             :   }
    6726          14 :   return DK? mkvec2(CK, DK): NULL;
    6727             : }
    6728             : 
    6729             : static long
    6730          70 : valNC2(GEN P, GEN E, long e)
    6731             : {
    6732          70 :   long i, d = 1, l = lg(P);
    6733         168 :   for (i = 1; i < l; i++)
    6734             :   {
    6735          98 :     long v = u_lval(e, P[i]) << 1;
    6736          98 :     if (v == E[i] + 1) v--;
    6737          98 :     d *= upowuu(P[i], v);
    6738             :   }
    6739          70 :   return d;
    6740             : }
    6741             : 
    6742             : static GEN
    6743          28 : findqganew(long N, GEN z)
    6744             : {
    6745          28 :   GEN MI, DI, x = real_i(z), y = imag_i(z), Ck = gen_0, Dk = gen_1, fa, P, E;
    6746             :   long i;
    6747          28 :   MI = ginv(utoi(N));
    6748          28 :   DI = mydivisorsu(mysqrtu(N));
    6749          28 :   fa = myfactoru(N); P = gel(fa,1); E = gel(fa,2);
    6750          98 :   for (i = 1; i < lg(DI); i++)
    6751             :   {
    6752          70 :     long e = DI[i], g;
    6753             :     GEN U, C, D, m;
    6754          70 :     (void)cxredsl2(gmulsg(e, z), &U);
    6755          70 :     C = gcoeff(U,2,1); if (!signe(C)) continue;
    6756          70 :     D = gcoeff(U,2,2);
    6757          70 :     g = ugcdiu(D,e);
    6758          70 :     if (g > 1) { C = muliu(C,e/g); D = diviuexact(D,g); } else C = muliu(C,e);
    6759          70 :     m = gadd(gsqr(gadd(gmul(C, x), D)), gsqr(gmul(C, y)));
    6760          70 :     m = gdivgs(m, valNC2(P, E, e));
    6761          70 :     if (gcmp(m, MI) < 0) { MI = m; Ck = C; Dk = D; }
    6762             :   }
    6763          28 :   return signe(Ck)? mkvec2(Ck, Dk): NULL;
    6764             : }
    6765             : 
    6766             : /* Return z' and U = [a,b;c,d] \in SL_2(Z), z' = U*z,
    6767             :  * Im(z')/width(U.oo) > sqrt(3)/(2N). Set *pczd = c*z+d */
    6768             : static GEN
    6769         154 : cxredga0N(long N, GEN z, GEN *pU, GEN *pczd, long flag)
    6770             : {
    6771         154 :   GEN v = NULL, A, B, C, D;
    6772             :   long e;
    6773         154 :   if (N == 1) return cxredsl2_i(z, pU, pczd);
    6774         119 :   e = gexpo(gel(z,2));
    6775         119 :   if (e < 0) z = gprec_wensure(z, precision(z) + nbits2extraprec(-e));
    6776         119 :   v = flag? findqganew(N,z): findqga(N,z);
    6777         119 :   if (!v) { *pU = matid(2); *pczd = gen_1; return z; }
    6778          35 :   C = gel(v,1);
    6779          35 :   D = gel(v,2);
    6780          35 :   if (!is_pm1(bezout(C,D, &B,&A))) pari_err_BUG("cxredga0N [gcd > 1]");
    6781          35 :   B = negi(B);
    6782          35 :   *pU = mkmat2(mkcol2(A,C), mkcol2(B,D));
    6783          35 :   *pczd = gadd(gmul(C,z), D);
    6784          35 :   return gdiv(gadd(gmul(A,z), B), *pczd);
    6785             : }
    6786             : 
    6787             : static GEN
    6788         147 : lfunthetaall(GEN b, GEN vL, GEN t, long bitprec)
    6789             : {
    6790         147 :   long i, l = lg(vL);
    6791         147 :   GEN v = cgetg(l, t_VEC);
    6792         322 :   for (i = 1; i < l; i++)
    6793             :   {
    6794         175 :     GEN T, L = gel(vL,i), a0 = gel(L,1), ldata = gel(L,2);
    6795         175 :     GEN van = gel(ldata_get_an(ldata),2);
    6796         175 :     if (lg(van) == 1)
    6797             :     {
    6798           0 :       T = gmul(b, a0);
    6799           0 :       if (isexactzero(T)) { GEN z = real_0_bit(-bitprec); T = mkcomplex(z,z); }
    6800             :     }
    6801             :     else
    6802             :     {
    6803         175 :       T = gmul2n(lfuntheta(ldata, t, 0, bitprec), -1);
    6804         175 :       T = gmul(b, gadd(a0, T));
    6805             :     }
    6806         175 :     gel(v,i) = T;
    6807             :   }
    6808         147 :   return l == 2? gel(v,1): v;
    6809             : }
    6810             : 
    6811             : /* P in ZX */
    6812             : static GEN
    6813         168 : ZX_roots(GEN P, long prec)
    6814             : {
    6815         168 :   long d = degpol(P);
    6816         168 :   if (d == 1) return mkvec(gen_0);
    6817         168 :   if (d == 2 && isint1(gel(P,2)) && isintzero(gel(P,3)) && isint1(gel(P,4)))
    6818           7 :     return mkvec2(powIs(3), gen_I()); /* order as polroots */
    6819         161 :   return (ZX_sturm(P) == d)? realroots(P,NULL,prec): QX_complex_roots(P,prec);
    6820             : }
    6821             : /* initializations for RgX_RgV_eval / RgC_embed */
    6822             : static GEN
    6823         203 : rootspowers(GEN v)
    6824             : {
    6825         203 :   long i, l = lg(v);
    6826         203 :   GEN w = cgetg(l, t_VEC);
    6827         203 :   for (i = 1; i < l; i++) gel(w,i) = gpowers(gel(v,i), l-2);
    6828         203 :   return w;
    6829             : }
    6830             : /* mf embeddings attached to Q(chi)/(T), chi attached to cyclotomic P */
    6831             : static GEN
    6832         819 : getembed(GEN P, GEN T, GEN zcyclo, long prec)
    6833             : {
    6834             :   long i, l;
    6835             :   GEN v;
    6836         819 :   if (degpol(P) == 1) P = NULL; /* mfcharpol for quadratic char */
    6837         819 :   if (degpol(T) == 1) T = NULL; /* dim 1 orbit */
    6838         819 :   if (T && P)
    6839          35 :   { /* K(y) / (T(y)), K = Q(t)/(P) cyclotomic */
    6840          35 :     GEN vr = RgX_is_ZX(T)? ZX_roots(T,prec): roots(RgX_embed1(T,zcyclo), prec);
    6841          35 :     v = rootspowers(vr); l = lg(v);
    6842          35 :     for (i = 1; i < l; i++) gel(v,i) = mkcol3(P,zcyclo,gel(v,i));
    6843             :   }
    6844         784 :   else if (T)
    6845             :   { /* Q(y) / (T(y)), T non-cyclotomic */
    6846         168 :     GEN vr = ZX_roots(T, prec);
    6847         168 :     v = rootspowers(vr); l = lg(v);
    6848         168 :     for (i = 1; i < l; i++) gel(v,i) = mkcol2(T, gel(v,i));
    6849             :   }
    6850             :   else /* cyclotomic or rational */
    6851         616 :     v = mkvec(P? mkvec2(P, zcyclo): cgetg(1,t_VEC));
    6852         819 :   return v;
    6853             : }
    6854             : static GEN
    6855         672 : grootsof1_CHI(GEN CHI, long prec)
    6856         672 : { return grootsof1(mfcharorder_canon(CHI), prec); }
    6857             : /* return the [Q(F):Q(chi)] embeddings of F */
    6858             : static GEN
    6859         518 : mfgetembed(GEN F, long prec)
    6860             : {
    6861         518 :   GEN T = mf_get_field(F), CHI = mf_get_CHI(F), P = mfcharpol(CHI);
    6862         518 :   return getembed(P, T, grootsof1_CHI(CHI, prec), prec);
    6863             : }
    6864             : static GEN
    6865           7 : mfchiembed(GEN mf, long prec)
    6866             : {
    6867           7 :   GEN CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    6868           7 :   return getembed(P, pol_x(0), grootsof1_CHI(CHI, prec), prec);
    6869             : }
    6870             : /* mfgetembed for the successive eigenforms in MF_get_newforms */
    6871             : static GEN
    6872         147 : mfeigenembed(GEN mf, long prec)
    6873             : {
    6874         147 :   GEN vP = MF_get_fields(mf), vF = MF_get_newforms(mf);
    6875         147 :   GEN zcyclo, vE, CHI = MF_get_CHI(mf), P = mfcharpol(CHI);
    6876         147 :   long i, l = lg(vP);
    6877         147 :   vF = Q_remove_denom(liftpol_shallow(vF), NULL);
    6878         147 :   prec += nbits2extraprec(gexpo(vF));
    6879         147 :   zcyclo = grootsof1_CHI(CHI, prec);
    6880         147 :   vE = cgetg(l, t_VEC);
    6881         147 :   for (i = 1; i < l; i++) gel(vE,i) = getembed(P, gel(vP,i), zcyclo, prec);
    6882         147 :   return vE;
    6883             : }
    6884             : 
    6885             : static int
    6886          28 : checkPv(GEN P, GEN v)
    6887          28 : { return typ(P) == t_POL && typ(v) == t_VEC && lg(v)-1 >= degpol(P); }
    6888             : static int
    6889          28 : checkemb_i(GEN E)
    6890             : {
    6891          28 :   long t = typ(E), l = lg(E);
    6892          28 :   if (t == t_VEC) return l == 1 || (l == 3 && checkPv(gel(E,1), gel(E,2)));
    6893          21 :   if (t != t_COL) return 0;
    6894          21 :   if (l == 3) return checkPv(gel(E,1), gel(E,2));
    6895          21 :   return l == 4 && typ(gel(E,2)) == t_VEC && checkPv(gel(E,1), gel(E,3));
    6896             : }
    6897             : static GEN
    6898          28 : anyembed(GEN v, GEN E)
    6899             : {
    6900          28 :   switch(typ(v))
    6901             :   {
    6902          21 :     case t_VEC: case t_COL: return mfvecembed(E, v);
    6903           7 :     case t_MAT: return mfmatembed(E, v);
    6904             :   }
    6905           0 :   return mfembed(E, v);
    6906             : }
    6907             : GEN
    6908          49 : mfembed0(GEN E, GEN v, long prec)
    6909             : {
    6910          49 :   pari_sp av = avma;
    6911          49 :   GEN mf, vE = NULL;
    6912          49 :   if (checkmf_i(E)) vE = mfgetembed(E, prec);
    6913          35 :   else if ((mf = checkMF_i(E))) vE = mfchiembed(mf, prec);
    6914          49 :   if (vE)
    6915             :   {
    6916          21 :     long i, l = lg(vE);
    6917             :     GEN w;
    6918          21 :     if (!v) return gerepilecopy(av, l == 2? gel(vE,1): vE);
    6919           0 :     w = cgetg(l, t_VEC);
    6920           0 :     for (i = 1; i < l; i++) gel(w,i) = anyembed(v, gel(vE,i));
    6921           0 :     return gerepilecopy(av, l == 2? gel(w,1): w);
    6922             :   }
    6923          28 :   if (!checkemb_i(E) || !v) pari_err_TYPE("mfembed", E);
    6924          28 :   return gerepilecopy(av, anyembed(v,E));
    6925             : }
    6926             : 
    6927             : /* dummy lfun create for theta evaluation */
    6928             : static GEN
    6929         840 : mfthetaancreate(GEN van, GEN N, GEN k)
    6930             : {
    6931         840 :   GEN L = zerovec(6);
    6932         840 :   gel(L,1) = lfuntag(t_LFUN_GENERIC, van);
    6933         840 :   gel(L,3) = mkvec2(gen_0, gen_1);
    6934         840 :   gel(L,4) = k;
    6935         840 :   gel(L,5) = N; return L;
    6936             : }
    6937             : /* destroy van and prepare to evaluate theta(sigma(van)), for all sigma in
    6938             :  * embeddings vector vE */
    6939             : static GEN
    6940         301 : van_embedall(GEN van, GEN vE, GEN gN, GEN gk)
    6941             : {
    6942         301 :   GEN a0 = gel(van,1), vL;
    6943         301 :   long i, lE = lg(vE), l = lg(van);
    6944         301 :   van++; van[0] = evaltyp(t_VEC) | evallg(l-1); /* remove a0 */
    6945         301 :   vL = cgetg(lE, t_VEC);
    6946         805 :   for (i = 1; i < lE; i++)
    6947             :   {
    6948         504 :     GEN E = gel(vE,i), v = mfvecembed(E, van);
    6949         504 :     gel(vL,i) = mkvec2(mfembed(E,a0), mfthetaancreate(v, gN, gk));
    6950             :   }
    6951         301 :   return vL;
    6952             : }
    6953             : 
    6954             : static int
    6955         973 : cusp_AC(GEN cusp, long *A, long *C)
    6956             : {
    6957         973 :   switch(typ(cusp))
    6958             :   {
    6959          84 :     case t_INFINITY: *A = 1; *C = 0; break;
    6960         273 :     case t_INT:  *A = itos(cusp); *C = 1; break;
    6961         420 :     case t_FRAC: *A = itos(gel(cusp, 1)); *C = itos(gel(cusp, 2)); break;
    6962             :     case t_REAL: case t_COMPLEX:
    6963         196 :       *A = 0; *C = 0;
    6964         196 :       if (gsigne(imag_i(cusp)) <= 0)
    6965           7 :         pari_err_DOMAIN("mfeval","imag(tau)","<=",gen_0,cusp);
    6966         189 :       return 0;
    6967           0 :     default: pari_err_TYPE("cusp_AC", cusp);
    6968             :   }
    6969         777 :   return 1;
    6970             : }
    6971             : static GEN
    6972         511 : cusp2mat(long A, long C)
    6973             : { long B, D;
    6974         511 :   cbezout(A, C, &D, &B);
    6975         511 :   return mkmat22s(A, -B, C, D);
    6976             : }
    6977             : static GEN
    6978           7 : mkS(void) { return mkmat22s(0,-1,1,0); }
    6979             : 
    6980             : /* if t is a cusp, return F(t), else NULL */
    6981             : static GEN
    6982         343 : evalcusp(GEN mf, GEN F, GEN t, long prec)
    6983             : {
    6984             :   long A, C;
    6985             :   GEN R;
    6986         343 :   if (!cusp_AC(t, &A,&C)) return NULL;
    6987         189 :   if (C % mf_get_N(F) == 0) return gel(mfcoefs_i(F, 0, 1), 1);
    6988         175 :   R = mfgaexpansion(mf, F, cusp2mat(A,C), 0, prec);
    6989         175 :   return gequal0(gel(R,1))? gmael(R,3,1): gen_0;
    6990             : }
    6991             : /* Evaluate an mf closure numerically, i.e., in the usual sense, either for a
    6992             :  * single tau or a vector of tau; for each, return a vector of results
    6993             :  * corresponding to all complex embeddings of F. If flag is non-zero, allow
    6994             :  * replacing F by F | gamma to increase imag(gamma^(-1).tau) [ expensive if
    6995             :  * MF_EISENSPACE not present ] */
    6996             : static GEN
    6997         154 : mfeval_i(GEN mf, GEN F, GEN vtau, long flag, long bitprec)
    6998             : {
    6999             :   GEN L0, vL, vb, sqN, vczd, vTAU, vs, van, vE;
    7000         154 :   long N = mf_get_N(F), N0, ta, lv, i, prec = nbits2prec(bitprec);
    7001         154 :   GEN gN = utoipos(N), gk = mf_get_gk(F), gk1 = gsubgs(gk,1), vgk;
    7002         154 :   long flscal = 0;
    7003             : 
    7004             :   /* gen_0 is ignored, second component assumes Ramanujan-Petersson in
    7005             :    * 1/2-integer weight */
    7006         154 :   vgk = mkvec2(gen_0, mfiscuspidal(mf,F)? gmul2n(gk1,-1): gk1);
    7007         154 :   ta = typ(vtau);
    7008         154 :   if (!is_vec_t(ta)) { flscal = 1; vtau = mkvec(vtau); ta = t_VEC; }
    7009         154 :   lv = lg(vtau);
    7010         154 :   sqN = sqrtr_abs(utor(N, prec));
    7011         154 :   vs = const_vec(lv-1, NULL);
    7012         154 :   vb = const_vec(lv-1, NULL);
    7013         154 :   vL = cgetg(lv, t_VEC);
    7014         154 :   vTAU = cgetg(lv, t_VEC);
    7015         154 :   vczd = cgetg(lv, t_VEC);
    7016         154 :   L0 = mfthetaancreate(NULL, gN, vgk); /* only for thetacost */
    7017         154 :   vE = mfgetembed(F, prec);
    7018         154 :   N0 = 0;
    7019         329 :   for (i = 1; i < lv; i++)
    7020             :   {
    7021         182 :     GEN z = gel(vtau,i), tau, U;
    7022             :     long w, n;
    7023             : 
    7024         182 :     gel(vs,i) = evalcusp(mf, F, z, prec);
    7025         175 :     if (gel(vs,i)) continue;
    7026         147 :     tau = cxredga0N(N, z, &U, &gel(vczd,i), flag);
    7027         147 :     if (!flag) w = 0; else { w = mfZC_width(N, gel(U,1)); tau = gdivgs(tau,w); }
    7028         147 :     gel(vTAU,i) = mulcxmI(gmul(tau, sqN));
    7029         147 :     n = lfunthetacost(L0, real_i(gel(vTAU,i)), 0, bitprec);
    7030         147 :     if (N0 < n) N0 = n;
    7031         147 :     if (flag)
    7032             :     {
    7033          35 :       GEN A, al, v = mfslashexpansion(mf, F, ZM_inv(U,NULL), N0, 0, &A, prec);
    7034          35 :       gel(vL,i) = van_embedall(v, vE, gN, vgk);
    7035          35 :       al = gel(A,1);
    7036          35 :       if (!gequal0(al))
    7037           7 :         gel(vb,i) = gexp(gmul(gmul(gmulsg(w,al),PiI2(prec)), tau), prec);
    7038             :     }
    7039             :   }
    7040         147 :   if (!flag)
    7041             :   {
    7042         112 :     van = mfcoefs_i(F, N0, 1);
    7043         112 :     vL = const_vec(lv-1, van_embedall(van, vE, gN, vgk));
    7044             :   }
    7045         322 :   for (i = 1; i < lv; i++)
    7046             :   {
    7047             :     GEN T;
    7048         175 :     if (gel(vs,i)) continue;
    7049         147 :     T = gpow(gel(vczd,i), gneg(gk), prec);
    7050         147 :     if (flag && gel(vb,i)) T = gmul(T, gel(vb,i));
    7051         147 :     gel(vs,i) = lfunthetaall(T, gel(vL,i), gel(vTAU,i), bitprec);
    7052             :   }
    7053         147 :   return flscal? gel(vs,1): vs;
    7054             : }
    7055             : 
    7056             : static long
    7057        1078 : mfistrivial(GEN F)
    7058             : {
    7059        1078 :   switch(mf_get_type(F))
    7060             :   {
    7061           7 :     case t_MF_CONST: return lg(gel(F,2)) == 1;
    7062         224 :     case t_MF_LINEAR: case t_MF_LINEAR_BHN: return gequal0(gel(F,3));
    7063         847 :     default: return 0;
    7064             :   }
    7065             : }
    7066             : 
    7067             : static long
    7068         896 : mf_same_k(GEN mf, GEN f) { return gequal(MF_get_gk(mf), mf_get_gk(f)); }
    7069             : static long
    7070         854 : mf_same_CHI(GEN mf, GEN f)
    7071             : {
    7072         854 :   GEN F1, F2, chi1, chi2, CHI1 = MF_get_CHI(mf), CHI2 = mf_get_CHI(f);
    7073             :   /* are the primitive chars attached to CHI1 and CHI2 equal ? */
    7074         854 :   F1 = znconreyconductor(gel(CHI1,1), gel(CHI1,2), &chi1);
    7075         854 :   if (typ(F1) == t_VEC) F1 = gel(F1,1);
    7076         854 :   F2 = znconreyconductor(gel(CHI2,1), gel(CHI2,2), &chi2);
    7077         854 :   if (typ(F2) == t_VEC) F2 = gel(F2,1);
    7078         854 :   return equalii(F1,F2) && ZV_equal(chi1,chi2);
    7079             : }
    7080             : /* check k and CHI rigorously, but not coefficients nor N */
    7081             : static long
    7082         189 : mfisinspace_i(GEN mf, GEN F)
    7083             : {
    7084         189 :   return mfistrivial(F) || (mf_same_k(mf,F) && mf_same_CHI(mf,F));
    7085             : }
    7086             : static void
    7087           7 : err_space(GEN F)
    7088           7 : { pari_err_DOMAIN("mftobasis", "form", "does not belong to",
    7089           0 :                   strtoGENstr("space"), F); }
    7090             : 
    7091             : static long
    7092         140 : mfcheapeisen(GEN mf)
    7093             : {
    7094         140 :   long k, L, N = MF_get_N(mf);
    7095             :   GEN P;
    7096         140 :   if (N <= 70) return 1;
    7097          84 :   k = itos(gceil(MF_get_gk(mf)));
    7098          84 :   if (odd(k)) k--;
    7099          84 :   switch (k)
    7100             :   {
    7101           0 :     case 2:  L = 190; break;
    7102          14 :     case 4:  L = 162; break;
    7103             :     case 6:
    7104          70 :     case 8:  L = 88; break;
    7105           0 :     case 10: L = 78; break;
    7106           0 :     default: L = 66; break;
    7107             :   }
    7108          84 :   P = gel(myfactoru(N), 1);
    7109          84 :   return P[lg(P)-1] <= L;
    7110             : }
    7111             : 
    7112             : static GEN
    7113         175 : myimag_i(GEN tau)
    7114             : {
    7115         175 :   long tc = typ(tau);
    7116         175 :   if (tc == t_INFINITY || tc == t_INT || tc == t_FRAC)
    7117          28 :     return gen_1;
    7118         147 :   if (tc == t_VEC)
    7119             :   {
    7120             :     long ltau, i;
    7121           7 :     GEN z = cgetg_copy(tau, &ltau);
    7122           7 :     for (i=1; i<ltau; i++) gel(z,i) = myimag_i(gel(tau,i));
    7123           7 :     return z;
    7124             :   }
    7125         140 :   return imag_i(tau);
    7126             : }
    7127             : 
    7128             : static GEN
    7129         140 : mintau(GEN vtau)
    7130             : {
    7131         140 :   if (!is_vec_t(typ(vtau))) return myimag_i(vtau);
    7132           7 :   return (lg(vtau) == 1)? gen_1: vecmin(myimag_i(vtau));
    7133             : }
    7134             : 
    7135             : /* initialization for mfgaexpansion: what does not depend on cusp */
    7136             : static GEN
    7137         826 : mf_eisendec(GEN mf, GEN F, long prec)
    7138             : {
    7139         826 :   GEN B = liftpol_shallow(mfeisensteindec(mf, F)), v = variables_vecsmall(B);
    7140         826 :   GEN Mvecj = obj_check(mf, MF_EISENSPACE);
    7141         826 :   long l = lg(v), i, ord;
    7142         826 :   if (lg(Mvecj) < 5) Mvecj = gel(Mvecj,1);
    7143         826 :   ord = itou(gel(Mvecj,4));
    7144         882 :   for (i = 1; i < l; i++)
    7145         637 :     if (v[i] != 1) { B = gsubst(B, v[i], rootsof1u_cx(ord, prec)); break; }
    7146         826 :   return B;
    7147             : }
    7148             : 
    7149             : GEN
    7150         154 : mfeval(GEN mf0, GEN F, GEN vtau, long bitprec)
    7151             : {
    7152         154 :   pari_sp av = avma;
    7153         154 :   long flnew = 1;
    7154         154 :   GEN mf = checkMF_i(mf0);
    7155         154 :   if (!mf) pari_err_TYPE("mfeval", mf0);
    7156         154 :   if (!checkmf_i(F)) pari_err_TYPE("mfeval", F);
    7157         154 :   if (!mfisinspace_i(mf, F)) err_space(F);
    7158         154 :   if (!obj_check(mf, MF_EISENSPACE)) flnew = mfcheapeisen(mf);
    7159         154 :   if (flnew && gcmpgs(gmulsg(2*MF_get_N(mf), mintau(vtau)), 1) >= 0) flnew = 0;
    7160         154 :   return gerepilecopy(av, mfeval_i(mf, F, vtau, flnew, bitprec));
    7161             : }
    7162             : 
    7163             : static long
    7164         182 : val(GEN v, long bit)
    7165             : {
    7166         182 :   long c, l = lg(v);
    7167         399 :   for (c = 1; c < l; c++)
    7168         385 :     if (gexpo(gel(v,c)) > -bit) return c-1;
    7169          14 :   return -1;
    7170             : }
    7171             : GEN
    7172         196 : mfcuspval(GEN mf, GEN F, GEN cusp, long bitprec)
    7173             : {
    7174         196 :   pari_sp av = avma;
    7175         196 :   long lvE, w, N, sb, n, A, C, prec = nbits2prec(bitprec);
    7176             :   GEN ga, gk, vE;
    7177         196 :   mf = checkMF(mf);
    7178         196 :   if (!checkmf_i(F)) pari_err_TYPE("mfcuspval",F);
    7179         196 :   N = MF_get_N(mf);
    7180         196 :   cusp_canon(cusp, N, &A, &C);
    7181         196 :   gk = mf_get_gk(F);
    7182         196 :   if (typ(gk) != t_INT)
    7183             :   {
    7184          42 :     GEN FT = mfmultheta(F), mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7185          42 :     GEN r = mfcuspval(mf2, FT, cusp, bitprec);
    7186          42 :     if ((C & 3L) == 2)
    7187             :     {
    7188          14 :       GEN z = sstoQ(1,4);
    7189          14 :       r = gsub(r, typ(r) == t_VEC? const_vec(lg(r)-1, z): z);
    7190             :     }
    7191          42 :     return gerepileupto(av, r);
    7192             :   }
    7193         154 :   vE = mfgetembed(F, prec);
    7194         154 :   lvE = lg(vE);
    7195         154 :   w = mfcuspcanon_width(N, C);
    7196         154 :   sb = w * mfsturmNk(N, itos(gk));
    7197         154 :   ga = cusp2mat(A,C);
    7198         161 :   for (n = 8;; n = minss(sb, n << 1))
    7199           7 :   {
    7200         161 :     GEN R = mfgaexpansion(mf, F, ga, n, prec), res = liftpol_shallow(gel(R,3));
    7201         161 :     GEN v = cgetg(lvE-1, t_VECSMALL);
    7202         161 :     long j, ok = 1;
    7203         161 :     res = RgC_embedall(res, vE);
    7204         343 :     for (j = 1; j < lvE; j++)
    7205             :     {
    7206         182 :       v[j] = val(gel(res,j), bitprec/2);
    7207         182 :       if (v[j] < 0) ok = 0;
    7208             :     }
    7209         161 :     if (ok)
    7210             :     {
    7211         147 :       res = cgetg(lvE, t_VEC);
    7212         147 :       for (j = 1; j < lvE; j++) gel(res,j) = gadd(gel(R,1), sstoQ(v[j], w));
    7213         147 :       return gerepilecopy(av, lvE==2? gel(res,1): res);
    7214             :     }
    7215          14 :     if (n == sb) return lvE==2? mkoo(): const_vec(lvE-1, mkoo()); /* 0 */
    7216             :   }
    7217             : }
    7218             : 
    7219             : long
    7220         196 : mfiscuspidal(GEN mf, GEN F)
    7221             : {
    7222         196 :   pari_sp av = avma;
    7223             :   GEN mf2;
    7224         196 :   if (space_is_cusp(MF_get_space(mf))) return 1;
    7225          77 :   if (typ(mf_get_gk(F)) == t_INT)
    7226             :   {
    7227          49 :     GEN v = mftobasis(mf,F,0), vE = vecslice(v, 1, lg(MF_get_E(mf))-1);
    7228          49 :     return gc_long(av, gequal0(vE));
    7229             :   }
    7230          28 :   if (!gequal0(mfak_i(F, 0))) return 0;
    7231          14 :   mf2 = obj_checkbuild(mf, MF_MF2INIT, &mf2init);
    7232          14 :   return mfiscuspidal(mf2, mfmultheta(F));
    7233             : }
    7234             : 
    7235             : /* F = vector of newforms in mftobasis format */
    7236             : static GEN
    7237          77 : mffrickeeigen_i(GEN mf, GEN F, GEN vE, long prec)
    7238             : {
    7239          77 :   GEN M, Z, L0, gN = MF_get_gN(mf), gk = MF_get_gk(mf);
    7240          77 :   long N0, i, lM, bit = prec2nbits(prec), k = itou(gk);
    7241          77 :   long LIM = 5; /* Sturm bound is enough */
    7242             : 
    7243          77 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7244             : START:
    7245          77 :   N0 = lfunthetacost(L0, gen_1, LIM, bit);
    7246          77 :   M = mfcoefs_mf(mf, N0, 1);
    7247          77 :   lM = lg(F);
    7248          77 :   Z = cgetg(lM, t_VEC);
    7249         231 :   for (i = 1; i < lM; i++)
    7250             :   { /* expansion of D * F[i] */
    7251         154 :     GEN D, z, van = RgM_RgC_mul(M, Q_remove_denom(gel(F,i), &D));
    7252         154 :     GEN L = van_embedall(van, gel(vE,i), gN, gk);
    7253         154 :     long l = lg(L), j, bit_add = D? expi(D): 0;
    7254         154 :     gel(Z,i) = z = cgetg(l, t_VEC);
    7255         483 :     for (j = 1; j < l; j++)
    7256             :     {
    7257             :       GEN v, C, C0;
    7258             :       long m, e;
    7259         462 :       for (m = 0; m <= LIM; m++)
    7260             :       {
    7261         462 :         v = lfuntheta(gmael(L,j,2), gen_1, m, bit);
    7262         462 :         if (gexpo(v) > bit_add - bit/2) break;
    7263             :       }
    7264         329 :       if (m > LIM) { LIM <<= 1; goto START; }
    7265         329 :       C = mulcxpowIs(gdiv(v,conj_i(v)), 2*m - k);
    7266         329 :       C0 = grndtoi(C, &e); if (e < 5-bit_accuracy(precision(C))) C = C0;
    7267         329 :       gel(z,j) = C;
    7268             :     }
    7269             :   }
    7270          77 :   return Z;
    7271             : }
    7272             : static GEN
    7273          70 : mffrickeeigen(GEN mf, GEN vE, long prec)
    7274             : {
    7275          70 :   GEN D = obj_check(mf, MF_FRICKE);
    7276          70 :   if (D) { long p = gprecision(D); if (!p || p >= prec) return D; }
    7277          63 :   D = mffrickeeigen_i(mf, MF_get_newforms(mf), vE, prec);
    7278          63 :   return obj_insert(mf, MF_FRICKE, D);
    7279             : }
    7280             : 
    7281             : /* integral weight, new space for primitive quadratic character CHIP;
    7282             :  * MF = vector of embedded eigenforms coefs on mfbasis, by orbit.
    7283             :  * Assume N > Q > 1 and (Q,f(CHIP)) = 1 */
    7284             : static GEN
    7285          56 : mfatkineigenquad(GEN mf, GEN CHIP, long Q, GEN MF, long bitprec)
    7286             : {
    7287             :   GEN L0, la2, S, F, vP, tau, wtau, Z, va, vb, den, coe, sqrtQ, sqrtN;
    7288          56 :   GEN M, gN, gk = MF_get_gk(mf);
    7289          56 :   long N0, x, yq, i, j, lF, dim, muQ, prec = nbits2prec(bitprec);
    7290          56 :   long N = MF_get_N(mf), k = itos(gk), NQ = N / Q;
    7291             : 
    7292             :   /* Q coprime to FC */
    7293          56 :   F = MF_get_newforms(mf);
    7294          56 :   vP = MF_get_fields(mf);
    7295          56 :   lF = lg(F);
    7296          56 :   Z = cgetg(lF, t_VEC);
    7297          56 :   S = MF_get_S(mf); dim = lg(S) - 1;
    7298          56 :   muQ = mymoebiusu(Q);
    7299          56 :   if (muQ)
    7300             :   {
    7301          42 :     GEN SQ = cgetg(dim+1,t_VEC), Qk = gpow(stoi(Q), sstoQ(k-2, 2), prec);
    7302          42 :     long i, bit2 = bitprec >> 1;
    7303          42 :     for (j = 1; j <= dim; j++) gel(SQ,j) = mfak_i(gel(S,j), Q);
    7304          84 :     for (i = 1; i < lF; i++)
    7305             :     {
    7306          42 :       GEN S = RgV_dotproduct(gel(F,i), SQ), T = gel(vP,i);
    7307             :       long e;
    7308          42 :       if (degpol(T) > 1 && typ(S) != t_POLMOD) S = gmodulo(S, T);
    7309          42 :       S = grndtoi(gdiv(conjvec(S, prec), Qk), &e);
    7310          42 :       if (e > -bit2) pari_err_PREC("mfatkineigenquad");
    7311          42 :       if (muQ == -1) S = gneg(S);
    7312          42 :       gel(Z,i) = S;
    7313             :     }
    7314          42 :     return Z;
    7315             :   }
    7316          14 :   la2 = mfchareval_i(CHIP, Q); /* 1 or -1 */
    7317          14 :   (void)cbezout(Q, NQ, &x, &yq);
    7318          14 :   sqrtQ = sqrtr_abs(utor(Q,prec));
    7319          14 :   tau = mkcomplex(gadd(sstoQ(-1, NQ), ginv(utoi(1000))),
    7320             :                   divru(sqrtQ, N));
    7321          14 :   den = gaddgs(gmulsg(NQ, tau), 1);
    7322          14 :   wtau = gdiv(gsub(gmulsg(x, tau), sstoQ(yq, Q)), den);
    7323          14 :   coe = gpowgs(gmul(sqrtQ, den), k);
    7324             : 
    7325          14 :   sqrtN = sqrtr_abs(utor(N,prec));
    7326          14 :   tau  = mulcxmI(gmul(tau,  sqrtN));
    7327          14 :   wtau = mulcxmI(gmul(wtau, sqrtN));
    7328          14 :   gN = utoipos(N);
    7329          14 :   L0 = mfthetaancreate(NULL, gN, gk); /* only for thetacost */
    7330          14 :   N0 = maxss(lfunthetacost(L0,real_i(tau), 0,bitprec),
    7331             :              lfunthetacost(L0,real_i(wtau),0,bitprec));
    7332          14 :   M = mfcoefs_mf(mf, N0, 1);
    7333          14 :   va = cgetg(dim+1, t_VEC);
    7334          14 :   vb = cgetg(dim+1, t_VEC);
    7335         105 :   for (j = 1; j <= dim; j++)
    7336             :   {
    7337          91 :     GEN L, v = vecslice(gel(M,j), 2, N0+1); /* remove a0 */
    7338          91 :     settyp(v, t_VEC); L = mfthetaancreate(v, gN, gk);
    7339          91 :     gel(va,j) = lfuntheta(L, tau,0,bitprec);
    7340          91 :     gel(vb,j) = lfuntheta(L,wtau,0,bitprec);
    7341             :   }
    7342          84 :   for (i = 1; i < lF; i++)
    7343             :   {
    7344          70 :     GEN z, FE = gel(MF,i);
    7345          70 :     long l = lg(FE);
    7346          70 :     z = cgetg(l, t_VEC);
    7347          70 :     for (j = 1; j < l; j++)
    7348             :     {
    7349          70 :       GEN f = gel(FE,j), a = RgV_dotproduct(va,f), b = RgV_dotproduct(vb,f);
    7350          70 :       GEN la = ground( gdiv(b, gmul(a,coe)) );
    7351          70 :       if (!gequal(gsqr(la), la2)) pari_err_PREC("mfatkineigenquad");
    7352          70 :       if (typ(la) == t_INT)
    7353             :       {
    7354          70 :         if (j != 1) pari_err_BUG("mfatkineigenquad");
    7355          70 :         z = const_vec(l-1, la); break;
    7356             :       }
    7357           0 :       gel(z,j) = la;
    7358             :     }
    7359          70 :     gel(Z,i) = z;
    7360             :   }
    7361          14 :   return Z;
    7362             : }
    7363             : 
    7364             : static GEN
    7365          70 : myusqrt(ulong a, long prec)
    7366             : {
    7367          70 :   if (a == 1UL) return gen_1;
    7368          56 :   if (uissquareall(a, &a)) return utoipos(a);
    7369          42 :   return sqrtr_abs(utor(a, prec));
    7370             : }
    7371             : /* Assume mf is a non-trivial new space, rational primitive character CHIP
    7372             :  * and (Q,FC) = 1 */
    7373             : static GEN
    7374          98 : mfatkinmatnewquad(GEN mf, GEN CHIP, long Q, long flag, long PREC)
    7375             : {
    7376          98 :   GEN cM, M, D, MF, den, vE, F = MF_get_newforms(mf);
    7377          98 :   long i, c, e, prec, bitprec, lF = lg(F), N = MF_get_N(mf), k = MF_get_k(mf);
    7378             : 
    7379          98 :   if (Q == 1) return mkvec4(gen_0, matid(MF_get_dim(mf)), gen_1, mf);
    7380          98 :   den = gel(MF_get_Minv(mf), 2);
    7381          98 :   bitprec = expi(den) + 64;
    7382          98 :   if (!flag) bitprec = maxss(bitprec, prec2nbits(PREC));
    7383             : 
    7384             : START:
    7385          98 :   prec = nbits2prec(bitprec);
    7386          98 :   vE = mfeigenembed(mf, prec);
    7387          98 :   M = cgetg(lF, t_VEC);
    7388          98 :   for (i = 1; i < lF; i++) gel(M,i) = RgC_embedall(gel(F,i), gel(vE,i));
    7389          98 :   if (Q != N)
    7390             :   {
    7391          56 :     D = mfatkineigenquad(mf, CHIP, Q, M, bitprec);
    7392          56 :     c = odd(k)? Q: 1;
    7393             :   }
    7394             :   else
    7395             :   {
    7396          42 :     D = mffrickeeigen(mf, vE, DEFAULTPREC);
    7397          42 :     c = mfcharmodulus(CHIP); if (odd(k)) c = -Q/c;
    7398             :   }
    7399          98 :   D = shallowconcat1(D);
    7400          98 :   if (vec_isconst(D)) { MF = diagonal_shallow(D); flag = 0; }
    7401             :   else
    7402             :   {
    7403          63 :     M = shallowconcat1(M);
    7404          63 :     MF = RgM_mul(matmuldiagonal(M,D), ginv(M));
    7405             :   }
    7406          98 :   if (!flag) return mkvec4(gen_0, MF, gen_1, mf);
    7407             : 
    7408          21 :   if (c > 0)
    7409          21 :     cM = myusqrt(c, PREC);
    7410             :   else
    7411             :   {
    7412           0 :     MF = imag_i(MF); c = -c;
    7413           0 :     cM = mkcomplex(gen_0, myusqrt(c,PREC));
    7414             :   }
    7415          21 :   if (c != 1) MF = RgM_Rg_mul(MF, myusqrt(c,prec));
    7416          21 :   MF = grndtoi(RgM_Rg_mul(MF,den), &e);
    7417          21 :   if (e > -32) { bitprec <<= 1; goto START; }
    7418          21 :   MF = RgM_Rg_div(MF, den);
    7419          21 :   if (is_rational_t(typ(cM)) && !isint1(cM))
    7420           0 :   { MF = RgM_Rg_div(MF, cM); cM = gen_1; }
    7421          21 :   return mkvec4(gen_0, MF, cM, mf);
    7422             : }
    7423             : 
    7424             : /* let CHI mod N, Q || N, return \bar{CHI_Q} * CHI_{N/Q} */
    7425             : static GEN
    7426          70 : mfcharAL(GEN CHI, long Q)
    7427             : {
    7428          70 :   GEN G = gel(CHI,1), c = gel(CHI,2), cycc, d, P, E, F;
    7429          70 :   long l = lg(c), N = mfcharmodulus(CHI), i;
    7430          70 :   if (N == Q) return mfcharconj(CHI);
    7431          42 :   if (N == 1) return CHI;
    7432          42 :   CHI = leafcopy(CHI);
    7433          42 :   gel(CHI,2) = d = leafcopy(c);
    7434          42 :   F = znstar_get_faN(G);
    7435          42 :   P = gel(F,1);
    7436          42 :   E = gel(F,2);
    7437          42 :   cycc = znstar_get_conreycyc(G);
    7438          42 :   if (!odd(Q) && equaliu(gel(P,1), 2) && E[1] >= 3)
    7439          14 :     gel(d,2) = Fp_neg(gel(d,2), gel(cycc,2));
    7440          56 :   else for (i = 1; i < l; i++)
    7441          28 :     if (!umodui(Q, gel(P,i))) gel(d,i) = Fp_neg(gel(d,i), gel(cycc,i));
    7442          42 :   return CHI;
    7443             : }
    7444             : static long
    7445         189 : atkin_get_NQ(long N, long Q, const char *f)
    7446             : {
    7447         189 :   long NQ = N / Q;
    7448         189 :   if (N % Q) pari_err_DOMAIN(f,"N % Q","!=",gen_0,utoi(Q));
    7449         189 :   if (ugcd(NQ, Q) > 1) pari_err_DOMAIN(f,"gcd(Q,N/Q)","!=",gen_1,utoi(Q));
    7450         189 :   return NQ;
    7451             : }
    7452             : 
    7453             : /* transform mf to new_NEW if possible */
    7454             : static GEN
    7455        1127 : MF_set_new(GEN mf)
    7456             : {
    7457        1127 :   GEN vMjd, vj, gk = MF_get_gk(mf);
    7458             :   long l, j;
    7459        1127 :   if (MF_get_space(mf) != mf_CUSP
    7460         182 :       || typ(gk) != t_INT || itou(gk) == 1) return mf;
    7461         168 :   vMjd = MFcusp_get_vMjd(mf); l = lg(vMjd);
    7462         168 :   if (l > 1 && gel(vMjd,1)[1] != MF_get_N(mf)) return mf; /* oldspace != 0 */
    7463         168 :   mf = shallowcopy(mf);
    7464         168 :   gel(mf,1) = shallowcopy(gel(mf,1));
    7465         168 :   MF_set_space(mf, mf_NEW);
    7466         168 :   vj = cgetg(l, t_VECSMALL);
    7467         168 :   for (j = 1; j < l; j++) vj[j] = gel(vMjd, j)[2];
    7468         168 :   gel(mf,4) = vj; return mf;
    7469             : }
    7470             : 
    7471             : /* if flag = 1, rationalize, else don't */
    7472             : static GEN
    7473         168 : mfatkininit_i(GEN mf, long Q, long flag, long prec)
    7474             : {
    7475             :   GEN M, B, C, CHI, CHIAL, G, chi, P, z, g, mfB, s, Mindex, Minv;
    7476         168 :   long j, l, lim, ord, FC, NQ, cQ, nk, dk, N = MF_get_N(mf);
    7477             : 
    7478         168 :   B = MF_get_basis(mf); l = lg(B);
    7479         168 :   M = cgetg(l, t_MAT); if (l == 1) return mkvec4(gen_0,M,gen_1,mf);
    7480         168 :   Qtoss(MF_get_gk(mf), &nk,&dk);
    7481         168 :   Q = labs(Q);
    7482         168 :   NQ = atkin_get_NQ(N, Q, "mfatkininit");
    7483         168 :   CHI = MF_get_CHI(mf);
    7484         168 :   CHI = mfchartoprimitive(CHI, &FC);
    7485         168 :   ord = mfcharorder_canon(CHI);
    7486         168 :   mf = MF_set_new(mf);
    7487         168 :   if (MF_get_space(mf) == mf_NEW && ord == 1 && NQ % FC == 0 && dk == 1)
    7488          98 :     return mfatkinmatnewquad(mf, CHI, Q, flag, prec);
    7489             :   /* now flag != 0 */
    7490          70 :   G   = gel(CHI,1);
    7491          70 :   chi = gel(CHI,2);
    7492          70 :   if (Q == N) { g = mkmat22s(0, -1, N, 0); cQ = NQ; } /* Fricke */
    7493             :   else
    7494             :   {
    7495          28 :     GEN F, gQP = utoi(ugcd(Q, FC));
    7496             :     long t, v;
    7497          28 :     chi = znchardecompose(G, chi, gQP);
    7498          28 :     F = znconreyconductor(G, chi, &chi);
    7499          28 :     G = znstar0(F,1);
    7500          28 :     (void)cbezout(Q, NQ, &t, &v);
    7501          28 :     g = mkmat22s(Q*t, 1, -N*v, Q);
    7502          28 :     cQ = -NQ*v;
    7503             :   }
    7504          70 :   C = s = gen_1;
    7505             :   /* N.B. G,chi are G_Q,chi_Q [primitive] at this point */
    7506          70 :   if (lg(chi) != 1) C = ginv( znchargauss(G, chi, gen_1, prec2nbits(prec)) );
    7507          70 :   if (dk == 1)
    7508          63 :   { if (odd(nk)) s = myusqrt(Q,prec); }
    7509             :   else
    7510             :   {
    7511           7 :     long r = nk >> 1; /* k-1/2 */
    7512           7 :     s = gpow(utoipos(Q), mkfracss(odd(r)? 1: 3, 4), prec);
    7513           7 :     if (odd(cQ))
    7514             :     {
    7515           7 :       long t = r + ((cQ-1) >> 1);
    7516           7 :       s = mkcomplex(s, odd(t)? gneg(s): s);
    7517             :     }
    7518             :   }
    7519          70 :   if (!isint1(s)) C = gmul(C, s);
    7520          70 :   CHIAL = mfcharAL(CHI, Q);
    7521          70 :   if (dk == 2)
    7522           7 :     CHIAL = mfcharmul(CHIAL, induce(gel(CHIAL,1), utoipos(odd(Q) ? Q<<2 : Q)));
    7523          70 :   CHIAL = mfchartoprimitive(CHIAL,NULL);
    7524          70 :   mfB = gequal(CHIAL,CHI)? mf: mfinit_Nndkchi(N,nk,dk,CHIAL,MF_get_space(mf),0);
    7525          70 :   Mindex = MF_get_Mindex(mfB);
    7526          70 :   Minv = MF_get_Minv(mfB);
    7527          70 :   P = z = NULL;
    7528          70 :   if (ord != 1) { P = mfcharpol(CHI); z = rootsof1u_cx(ord, prec); }
    7529          70 :   lim = maxss(mfsturm(mfB), mfsturm(mf)) + 1;
    7530         217 :   for (j = 1; j < l; j++)
    7531             :   {
    7532         147 :     GEN v = mfslashexpansion(mf, gel(B,j), g, lim, 0, NULL, prec+1);
    7533             :     long junk;
    7534         147 :     if (!isint1(C)) v = RgV_Rg_mul(v, C);
    7535         147 :     v = bestapprnf(v, P, z, prec);
    7536         147 :     v = vecpermute_partial(v, Mindex, &junk);
    7537         147 :     v = Minv_RgC_mul(Minv, v); /* cf mftobasis_i */
    7538         147 :     gel(M, j) = v;
    7539             :   }
    7540          70 :   if (is_rational_t(typ(C)) && !gequal1(C)) { M = gdiv(M, C); C = gen_1; }
    7541          70 :   if (mfB == mf) mfB = gen_0;
    7542          70 :   return mkvec4(mfB, M, C, mf);
    7543             : }
    7544             : GEN
    7545          77 : mfatkininit(GEN mf, long Q, long prec)
    7546             : {
    7547          77 :   pari_sp av = avma;
    7548          77 :   mf = checkMF(mf); return gerepilecopy(av, mfatkininit_i(mf, Q, 1, prec));
    7549             : }
    7550             : static void
    7551          21 : checkmfa(GEN z)
    7552             : {
    7553          21 :   if (typ(z) != t_VEC || lg(z) != 5 || typ(gel(z,2)) != t_MAT
    7554          21 :       || !checkMF_i(gel(z,4))
    7555          21 :       || (!isintzero(gel(z,1)) && !checkMF_i(gel(z,1))))
    7556           0 :     pari_err_TYPE("mfatkin [please apply mfatkininit()]",z);
    7557          21 : }
    7558             : 
    7559             : /* Apply atkin Q to closure F */
    7560             : GEN
    7561          21 : mfatkin(GEN mfa, GEN F)
    7562             : {
    7563          21 :   pari_sp av = avma;
    7564             :   GEN z, mfB, MQ, mf;
    7565          21 :   checkmfa(mfa);
    7566          21 :   mfB= gel(mfa,1);
    7567          21 :   MQ = gel(mfa,2);
    7568          21 :   mf = gel(mfa,4);
    7569          21 :   if (typ(mfB) == t_INT) mfB = mf;
    7570          21 :   z = RgM_RgC_mul(MQ, mftobasis_i(mf,F));
    7571          21 :   return gerepileupto(av, mflinear(mfB, z));
    7572             : }
    7573             : 
    7574             : GEN
    7575          49 : mfatkineigenvalues(GEN mf, long Q, long prec)
    7576             : {
    7577          49 :   pari_sp av = avma;
    7578             :   GEN vF, L, CHI, M, mfatk, C, MQ, vE, mfB;
    7579             :   long N, NQ, l, i;
    7580             : 
    7581          49 :   mf = checkMF(mf); N = MF_get_N(mf);
    7582          49 :   vF = MF_get_newforms(mf); l = lg(vF);
    7583             :   /* N.B. k is integral */
    7584          49 :   if (l == 1) { set_avma(av); return cgetg(1, t_VEC); }
    7585          49 :   L = cgetg(l, t_VEC);
    7586          49 :   if (Q == 1)
    7587             :   {
    7588           7 :     GEN vP = MF_get_fields(mf);
    7589           7 :     for (i = 1; i < l; i++) gel(L,i) = const_vec(degpol(gel(vP,i)), gen_1);
    7590           7 :     return L;
    7591             :   }
    7592          42 :   vE = mfeigenembed(mf,prec);
    7593          42 :   if (Q == N) return gerepileupto(av, mffrickeeigen(mf, vE, prec));
    7594          21 :   Q = labs(Q);
    7595          21 :   NQ = atkin_get_NQ(N, Q, "mfatkineigenvalues"); /* != 1 */
    7596          21 :   mfatk = mfatkininit(mf, Q, prec);
    7597          21 :   mfB= gel(mfatk,1); if (typ(mfB) != t_VEC) mfB = mf;
    7598          21 :   MQ = gel(mfatk,2);
    7599          21 :   C  = gel(mfatk,3);
    7600          21 :   M = row(mfcoefs_mf(mfB,1,1), 2); /* vec of a_1(b_i) for mfbasis functions */
    7601          56 :   for (i = 1; i < l; i++)
    7602             :   {
    7603          35 :     GEN c = RgV_dotproduct(RgM_RgC_mul(MQ,gel(vF,i)), M); /* C * eigen_i */
    7604          35 :     gel(L,i) = Rg_embedall_i(c, gel(vE,i));
    7605             :   }
    7606          21 :   if (!gequal1(C)) L = gdiv(L, C);
    7607          21 :   CHI = MF_get_CHI(mf);
    7608          21 :   if (mfcharorder(CHI) <= 2 && NQ % mfcharconductor(CHI) == 0) L = ground(L);
    7609          21 :   return gerepilecopy(av, L);
    7610             : }
    7611             : 
    7612             : /* expand B_d V, keeping same length */
    7613             : static GEN
    7614        4788 : bdexpand(GEN V, long d)
    7615             : {
    7616             :   GEN W;
    7617             :   long N, n;
    7618        4788 :   if (d == 1) return V;
    7619        1624 :   N = lg(V)-1; W = zerovec(N);
    7620        1624 :   for (n = 0; n <= (N-1)/d; n++) gel(W, n*d+1) = gel(V, n+1);
    7621        1624 :   return W;
    7622             : }
    7623             : /* expand B_d V, increasing length up to lim */
    7624             : static GEN
    7625         259 : bdexpandall(GEN V, long d, long lim)
    7626             : {
    7627             :   GEN W;
    7628             :   long N, n;
    7629         259 :   if (d == 1) return V;
    7630          35 :   N = lg(V)-1; W = zerovec(lim);
    7631          35 :   for (n = 0; n <= N-1 && n*d <= lim; n++) gel(W, n*d+1) = gel(V, n+1);
    7632          35 :   return W;
    7633             : }
    7634             : 
    7635             : static void
    7636        7798 : parse_vecj(GEN T, GEN *E1, GEN *E2)
    7637             : {
    7638        7798 :   if (lg(T)==3) { *E1 = gel(T,1); *E2 = gel(T,2); }
    7639        4165 :   else { *E1 = T; *E2 = NULL; }
    7640        7798 : }
    7641             : 
    7642             : /* g in M_2(Z) ? */
    7643             : static int
    7644        2380 : check_M2Z(GEN g)
    7645        2380 : {  return typ(g) == t_MAT && lg(g) == 3 && lgcols(g) == 3 && RgM_is_ZM(g); }
    7646             : /* g in SL_2(Z) ? */
    7647             : static int
    7648        1463 : check_SL2Z(GEN g) { return check_M2Z(g) && equali1(ZM_det(g)); }
    7649             : 
    7650             : static GEN
    7651        7518 : mfcharcxeval(GEN CHI, long n, long prec)
    7652             : {
    7653             :   GEN ordg;
    7654             :   ulong ord;
    7655        7518 :   if (ugcd(mfcharmodulus(CHI), labs(n)) > 1) return gen_0;
    7656        7518 :   ordg = gmfcharorder(CHI);
    7657        7518 :   ord = itou(ordg);
    7658        7518 :   return rootsof1q_cx(znchareval_i(CHI,n,ordg), ord, prec);
    7659             : }
    7660             : 
    7661             : static GEN
    7662        4403 : RgV_shift(GEN V, GEN gn)
    7663             : {
    7664             :   long i, n, l;
    7665             :   GEN W;
    7666        4403 :   if (typ(gn) != t_INT) pari_err_BUG("RgV_shift [n not integral]");
    7667        4403 :   n = itos(gn);
    7668        4403 :   if (n < 0) pari_err_BUG("RgV_shift [n negative]");
    7669        4403 :   if (!n) return V;
    7670          98 :   W = cgetg_copy(V, &l); if (n > l-1) n = l-1;
    7671          98 :   for (i=1; i <= n; i++) gel(W,i) = gen_0;
    7672          98 :   for (    ; i < l; i++) gel(W,i) = gel(V, i-n);
    7673          98 :   return W;
    7674             : }
    7675             : static GEN
    7676        6776 : hash_eisengacx(hashtable *H, void *E, long w, GEN ga, long n, long prec)
    7677             : {
    7678        6776 :   ulong h = H->hash(E);
    7679        6776 :   hashentry *e = hash_search2(H, E, h);
    7680             :   GEN v;
    7681        6776 :   if (e) v = (GEN)e->val;
    7682             :   else
    7683             :   {
    7684        4410 :     v = mfeisensteingacx((GEN)E, w, ga, n, prec);
    7685        4410 :     hash_insert2(H, E, (void*)v, h);
    7686             :   }
    7687        6776 :   return v;
    7688             : }
    7689             : static GEN
    7690        4403 : vecj_expand(GEN B, hashtable *H, long w, GEN ga, long n, long prec)
    7691             : {
    7692             :   GEN E1, E2, v;
    7693        4403 :   parse_vecj(B, &E1, &E2);
    7694        4403 :   v = hash_eisengacx(H, (void*)E1, w, ga, n, prec);
    7695        4403 :   if (E2)
    7696             :   {
    7697        2352 :     GEN u = hash_eisengacx(H, (void*)E2, w, ga, n, prec);
    7698        2352 :     GEN a = gadd(gel(v,1), gel(u,1));
    7699        2352 :     GEN b = RgV_mul_RgXn(gel(v,2), gel(u,2));
    7700        2352 :     v = mkvec2(a,b);
    7701             :   }
    7702        4403 :   return v;
    7703             : }
    7704             : static GEN
    7705         889 : shift_M(GEN M, GEN Valpha, long w)
    7706             : {
    7707         889 :   long i, l = lg(Valpha);
    7708         889 :   GEN almin = vecmin(Valpha);
    7709        5292 :   for (i = 1; i < l; i++)
    7710             :   {
    7711        4403 :     GEN alpha = gel(Valpha, i), gsh = gmulsg(w, gsub(alpha,almin));
    7712        4403 :     gel(M,i) = RgV_shift(gel(M,i), gsh);
    7713             :   }
    7714         889 :   return almin;
    7715             : }
    7716             : static GEN mfeisensteinspaceinit(GEN NK);
    7717             : #if 0
    7718             : /* ga in M_2^+(Z)), n >= 0 */
    7719             : static GEN
    7720             : mfgaexpansion_init(GEN mf, GEN ga, long n, long prec)
    7721             : {
    7722             :   GEN M, Mvecj, vecj, almin, Valpha;
    7723             :   long i, w, l, N = MF_get_N(mf), c = itos(gcoeff(ga,2,1));
    7724             :   hashtable *H;
    7725             : 
    7726             :   if (c % N == 0)
    7727             :   { /* ga in G_0(N), trivial case; w = 1 */
    7728             :     GEN chid = mfcharcxeval(MF_get_CHI(mf), itos(gcoeff(ga,2,2)), prec);
    7729             :     return mkvec2(chid, utoi(n));
    7730             :   }
    7731             : 
    7732             :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7733             :   if (lg(Mvecj) < 5) pari_err_IMPL("mfgaexpansion_init in this case");
    7734             :   w = mfcuspcanon_width(N, c);
    7735             :   vecj = gel(Mvecj, 3);
    7736             :   l = lg(vecj);
    7737             :   M = cgetg(l, t_VEC);
    7738             :   Valpha = cgetg(l, t_VEC);
    7739             :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7740             :                      (int(*)(void*,void*))&gidentical, 1);
    7741             :   for (i = 1; i < l; i++)
    7742             :   {
    7743             :     GEN v = vecj_expand(gel(vecj,i), H, w, ga, n, prec);
    7744             :     gel(Valpha,i) = gel(v,1);
    7745             :     gel(M,i) = gel(v,2);
    7746             :   }
    7747             :   almin = shift_M(M, Valpha, w);
    7748             :   return mkvec3(almin, utoi(w), M);
    7749             : }
    7750             : /* half-integer weight not supported; vF = [F,eisendec(F)].
    7751             :  * Minit = mfgaexpansion_init(mf, ga, n, prec) */
    7752             : static GEN
    7753             : mfgaexpansion_with_init(GEN Minit, GEN vF)
    7754             : {
    7755             :   GEN v;
    7756             :   if (lg(Minit) == 3)
    7757             :   { /* ga in G_0(N) */
    7758             :     GEN chid = gel(Minit,1), gn = gel(Minit,2);
    7759             :     v = mfcoefs_i(gel(vF,1), itou(gn), 1);
    7760             :     v = mkvec3(gen_0, gen_1, RgV_Rg_mul(v,chid));
    7761             :   }
    7762             :   else
    7763             :   {
    7764             :     GEN V = RgM_RgC_mul(gel(Minit,3), gel(vF,2));
    7765             :     v = mkvec3(gel(Minit,1), gel(Minit,2), V);
    7766             :   }
    7767             :   return v;
    7768             : }
    7769             : #endif
    7770             : 
    7771             : /* B = mfeisensteindec(F) already embedded, ga in M_2^+(Z)), n >= 0 */
    7772             : static GEN
    7773         889 : mfgaexpansion_i(GEN mf, GEN B0, GEN ga, long n, long prec)
    7774             : {
    7775         889 :   GEN M, Mvecj, vecj, almin, Valpha, B, E = NULL;
    7776         889 :   long i, j, w, nw, l, N = MF_get_N(mf), bit = prec2nbits(prec) / 2;
    7777             :   hashtable *H;
    7778             : 
    7779         889 :   Mvecj = obj_check(mf, MF_EISENSPACE);
    7780         889 :   if (lg(Mvecj) < 5) { E = gel(Mvecj, 2); Mvecj = gel(Mvecj, 1); }
    7781         889 :   vecj = gel(Mvecj, 3);
    7782         889 :   l = lg(vecj);
    7783         889 :   B = cgetg(l, t_COL);
    7784         889 :   M = cgetg(l, t_VEC);
    7785         889 :   Valpha = cgetg(l, t_VEC);
    7786         889 :   w = mfZC_width(N, gel(ga,1));
    7787         889 :   nw = E ? n + w : n;
    7788         889 :   H = hash_create(l, (ulong(*)(void*))&hash_GEN,
    7789             :                      (int(*)(void*,void*))&gidentical, 1);
    7790        7882 :   for (i = j = 1; i < l; i++)
    7791             :   {
    7792             :     GEN v;
    7793        6993 :     if (gequal0(gel(B0,i))) continue;
    7794        4403 :     v = vecj_expand(gel(vecj,i), H, w, ga, nw, prec);
    7795        4403 :     gel(B,j) = gel(B0,i);
    7796        4403 :     gel(Valpha,j) = gel(v,1);
    7797        4403 :     gel(M,j) = gel(v,2); j++;
    7798             :   }
    7799         889 :   setlg(Valpha, j);
    7800         889 :   setlg(B, j);
    7801         889 :   setlg(M, j); l = j;
    7802         889 :   if (l == 1) return mkvec3(gen_0, utoi(w), zerovec(n+1));
    7803         889 :   almin = shift_M(M, Valpha, w);
    7804         889 :   B = RgM_RgC_mul(M, B); l = lg(B);
    7805      138110 :   for (i = 1; i < l; i++)
    7806      137221 :     if (gexpo(gel(B,i)) < -bit) gel(B,i) = gen_0;
    7807         889 :   settyp(B, t_VEC);
    7808         889 :   if (E)
    7809             :   {
    7810          21 :     GEN v = hash_eisengacx(H, (void*)E, w, ga, n, prec);
    7811          21 :     long ell = 0;
    7812          21 :     almin = gsub(almin, gel(v,1));
    7813          21 :     if (gsigne(almin) < 0)
    7814             :     {
    7815           0 :       GEN gell = gceil(gmulsg(-w, almin));
    7816           0 :       ell = itos(gell);
    7817           0 :       almin = gadd(almin, gdivgs(gell, w));
    7818           0 :       if (nw < ell) pari_err_IMPL("alpha < 0 in mfgaexpansion");
    7819             :     }
    7820          21 :     B = vecslice(B, ell + 1, n + ell + 1);
    7821          21 :     B = RgV_div_RgXn(B, gel(v,2));
    7822             :   }
    7823         889 :   return mkvec3(almin, utoi(w), B);
    7824             : }
    7825             : 
    7826             : /* Theta multiplier: assume 4 | C, (C,D)=1 */
    7827             : static GEN
    7828         238 : mfthetamultiplier(long C, long D)
    7829             : {
    7830         238 :   long s = kross(C, D);
    7831         238 :   if ((D&3L) == 1) return stoi(s);
    7832          49 :   return s > 0 ? powIs(3) : gen_I();
    7833             : }
    7834             : static GEN
    7835         238 : mfthetaexpansion(GEN M, long n)
    7836             : {
    7837         238 :   GEN s, al, sla, V = zerovec(n + 1);
    7838         238 :   long w, lim, la, f, C = itos(gcoeff(M, 2, 1)), D = itos(gcoeff(M, 2, 2));
    7839         238 :   switch (C & 3L)
    7840             :   {
    7841          56 :     case 0: al = gen_0; w = 1;
    7842          56 :       s = mfthetamultiplier(C,D);
    7843          56 :       lim = usqrt(n); gel(V, 1) = s;
    7844          56 :       s = gmul2n(s, 1);
    7845          56 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = s;
    7846          56 :       break;
    7847          84 :     case 2: al = sstoQ(1,4); w = 1;
    7848          84 :       s = gmul2n(mfthetamultiplier(C - 2*D, D), 1);
    7849          84 :       lim = (usqrt(n << 2) - 1) >> 1;
    7850          84 :       for (f = 0; f <= lim; f++) gel(V, f*(f+1) + 1) = s;
    7851          84 :       break;
    7852          98 :     default: al = gen_0; w = 4; la = (-D*C) & 3L;
    7853          98 :       s = mfthetamultiplier(-(D + la*C), C);
    7854          98 :       s = gsub(s, mulcxI(s));
    7855          98 :       sla = gmul(s, powIs(-la));
    7856          98 :       lim = usqrt(n); gel(V, 1) = gmul2n(s, -1);
    7857          98 :       for (f = 1; f <= lim; f++) gel(V, f*f + 1) = odd(f) ? sla : s;
    7858          98 :       break;
    7859             :   }
    7860         238 :   return mkvec3(al, stoi(w), V);
    7861             : }
    7862             : 
    7863             : /* F 1/2 integral weight */
    7864             : static GEN
    7865         238 : mf2gaexpansion(GEN mf2, GEN F, GEN ga, long n, long prec)
    7866             : {
    7867         238 :   GEN FT = mfmultheta(F), mf = obj_checkbuild(mf2, MF_MF2INIT, &mf2init);
    7868             :   GEN res, V1, Tres, V2, al, V, gsh;
    7869         238 :   long w2, C = itos(gcoeff(ga,2,1)), w = mfcuspcanon_width(MF_get_N(mf), C);
    7870         238 :   long ext = ((C & 3L) != 2)? 0: (w+3) >> 2;
    7871         238 :   long prec2 = prec + nbits2extraprec((long)M_PI/(2*M_LN2)*sqrt(n + ext));
    7872         238 :   res = mfgaexpansion(mf, FT, ga, n + ext, prec2);
    7873         238 :   Tres = mfthetaexpansion(ga, n + ext);
    7874         238 :   V1 = gel(res,3);
    7875         238 :   V2 = gel(Tres,3);
    7876         238 :   al = gsub(gel(res,1), gel(Tres,1));
    7877         238 :   w2 = itos(gel(Tres,2));
    7878         238 :   if (w != itos(gel(res,2)) || w % w2)
    7879           0 :     pari_err_BUG("mf2gaexpansion [incorrect w2 or w]");
    7880         238 :   if (w2 != w) V2 = bdexpand(V2, w/w2);
    7881         238 :   V = RgV_div_RgXn(V1, V2);
    7882         238 :   gsh = gfloor(gmulsg(w, al));
    7883         238 :   if (!gequal0(gsh))
    7884             :   {
    7885          28 :     al = gsub(al, gdivgs(gsh, w));
    7886          28 :     if (gsigne(gsh) > 0)
    7887             :     {
    7888           0 :       V = RgV_shift(V, gsh);
    7889           0 :       V = vecslice(V, 1, n + 1);
    7890             :     }
    7891             :     else
    7892             :     {
    7893          28 :       long sh = -itos(gsh), i;
    7894          28 :       if (sh > ext) pari_err_BUG("mf2gaexpansion [incorrect sh]");
    7895         119 :       for (i = 1; i <= sh; i++)
    7896          91 :         if (!gequal0(gel(V,i))) pari_err_BUG("mf2gaexpansion [sh too large]");
    7897          28 :       V = vecslice(V, sh+1, n + sh+1);
    7898             :     }
    7899             :   }
    7900         238 :   obj_free(mf); return mkvec3(al, stoi(w), gprec_wtrunc(V, prec));
    7901             : }
    7902             : 
    7903             : static GEN
    7904          70 : mfgaexpansionatkin(GEN mf, GEN F, GEN C, GEN D, long Q, long n, long prec)
    7905             : {
    7906          70 :   GEN mfa = mfatkininit_i(mf, Q, 0, prec), MQ = gel(mfa,2);
    7907          70 :   long i, FC, k = MF_get_k(mf);
    7908          70 :   GEN x, v, V, z, s, CHI = mfchartoprimitive(MF_get_CHI(mf), &FC);
    7909             : 
    7910             :   /* V = mfcoefs(F | w_Q, n), can't use mfatkin because MQ non-rational */
    7911          70 :   V = RgM_RgC_mul(mfcoefs_mf(mf,n,1), RgM_RgC_mul(MQ, mftobasis_i(mf,F)));
    7912          70 :   (void)bezout(utoipos(Q), C, &x, &v);
    7913          70 :   s = mfchareval_i(CHI, (umodiu(x, FC) * umodiu(D, FC)) % FC);
    7914          70 :   s = gdiv(s, gpow(utoipos(Q), sstoQ(k,2), prec));
    7915          70 :   V = RgV_Rg_mul(V, s);
    7916          70 :   z = rootsof1powinit(umodiu(D,Q)*umodiu(v,Q) % Q, Q, prec);
    7917          70 :   for (i = 1; i <= n+1; i++) gel(V,i) = gmul(gel(V,i), rootsof1pow(z, i-1));
    7918          70 :   return mkvec3(gen_0, utoipos(Q), V);
    7919             : }
    7920             : 
    7921             : /* allow F of the form [F, mf_eisendec(F)]~ */
    7922             : static GEN
    7923        1456 : mfgaexpansion(GEN mf, GEN F, GEN ga, long n, long prec)
    7924             : {
    7925        1456 :   GEN v, EF = NULL, res, Mvecj, c, d;
    7926             :   long precnew, N;
    7927             : 
    7928        1456 :   if (n < 0) pari_err_DOMAIN("mfgaexpansion", "n", "<", gen_0, stoi(n));
    7929        1456 :   if (typ(F) == t_COL && lg(F) == 3) { EF = gel(F,2); F = gel(F,1); }
    7930        1456 :   if (!checkmf_i(F)) pari_err_TYPE("mfgaexpansion", F);
    7931        1456 :   if (!check_SL2Z(ga)) pari_err_TYPE("mfgaexpansion",ga);
    7932        1456 :   if (typ(mf_get_gk(F)) != t_INT) return mf2gaexpansion(mf, F, ga, n, prec);
    7933        1218 :   c = gcoeff(ga,2,1);
    7934        1218 :   d = gcoeff(ga,2,2);
    7935        1218 :   N = MF_get_N(mf);
    7936        1218 :   if (!umodiu(c, mf_get_N(F)))
    7937             :   { /* trivial case: ga in Gamma_0(N) */
    7938         259 :     long w = mfcuspcanon_width(N, umodiu(c,N));
    7939         259 :     GEN CHI = mf_get_CHI(F);
    7940         259 :     GEN chid = mfcharcxeval(CHI, umodiu(d,mfcharmodulus(CHI)), prec);
    7941         259 :     v = mfcoefs_i(F, n/w, 1); if (!isint1(chid)) v = RgV_Rg_mul(v,chid);
    7942         259 :     return mkvec3(gen_0, stoi(w), bdexpandall(v,w,n+1));
    7943             :   }
    7944         959 :   mf = MF_set_new(mf);
    7945         959 :   if (MF_get_space(mf) == mf_NEW)
    7946             :   {
    7947         441 :     long cN = umodiu(c,N), g = ugcd(cN,N), Q = N/g;
    7948         441 :     GEN CHI = MF_get_CHI(mf);
    7949         441 :     if (ugcd(cN, Q)==1 && mfcharorder(CHI) <= 2
    7950         217 :                        && g % mfcharconductor(CHI) == 0
    7951         112 :                        && degpol(mf_get_field(F)) == 1)
    7952          70 :       return mfgaexpansionatkin(mf, F, c, d, Q, n, prec);
    7953             :   }
    7954         889 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    7955         889 :   precnew = prec;
    7956         889 :   if (lg(Mvecj) < 5)
    7957             :   {
    7958          21 :     long e, w = mfZC_width(N, gel(ga,1));
    7959          21 :     GEN v, E = gel(Mvecj,2);
    7960          21 :     v = mfeisensteingacx(E, w, ga, n, LOWDEFAULTPREC);
    7961          21 :     v = gel(v,2);
    7962          21 :     e = gexpo(RgXn_inv(RgV_to_RgX(v,0), n+1));
    7963          21 :     if (e > 0) precnew += nbits2extraprec(e);
    7964             :   }
    7965         889 :   if (!EF) EF = mf_eisendec(mf, F, precnew);
    7966         889 :   res = mfgaexpansion_i(mf, EF, ga, n, precnew);
    7967         889 :   return precnew == prec ? res : gprec_wtrunc(res, prec);
    7968             : }
    7969             : 
    7970             : /* parity = -1 or +1 */
    7971             : static GEN
    7972         217 : findd(long N, long parity)
    7973             : {
    7974         217 :   GEN L, D = mydivisorsu(N);
    7975         217 :   long i, j, l = lg(D);
    7976         217 :   L = cgetg(l, t_VEC);
    7977        1218 :   for (i = j = 1; i < l; i++)
    7978             :   {
    7979        1001 :     long d = D[i];
    7980        1001 :     if (parity == -1) d = -d;
    7981        1001 :     if (sisfundamental(d)) gel(L,j++) = stoi(d);
    7982             :   }
    7983         217 :   setlg(L,j); return L;
    7984             : }
    7985             : /* does ND contain a divisor of N ? */
    7986             : static int
    7987         413 : seenD(long N, GEN ND)
    7988             : {
    7989         413 :   long j, l = lg(ND);
    7990         427 :   for (j = 1; j < l; j++)
    7991          14 :     if (N % ND[j] == 0) return 1;
    7992         413 :   return 0;
    7993             : }
    7994             : static GEN
    7995          42 : search_levels(GEN vN, const char *f)
    7996             : {
    7997          42 :   switch(typ(vN))
    7998             :   {
    7999           7 :     case t_INT: vN = mkvecsmall(itos(vN)); break;
    8000          35 :     case t_VEC: case t_COL: vN = ZV_to_zv(vN); break;
    8001           0 :     case t_VECSMALL: vN = leafcopy(vN); break;
    8002           0 :     default: pari_err_TYPE(f, vN);
    8003             :   }
    8004          42 :   vecsmall_sort(vN); return vN;
    8005             : }
    8006             : GEN
    8007          14 : mfsearch(GEN NK, GEN V, long space)
    8008             : {
    8009          14 :   pari_sp av = avma;
    8010             :   GEN F, gk, NbyD, vN;
    8011             :   long n, nk, dk, parity, nV, i, lvN;
    8012             : 
    8013          14 :   if (typ(NK) != t_VEC || lg(NK) != 3) pari_err_TYPE("mfsearch", NK);
    8014          14 :   gk = gel(NK,2);
    8015          14 :   if (typ(gmul2n(gk, 1)) != t_INT) pari_err_TYPE("mfsearch [k]", gk);
    8016          14 :   switch(typ(V))
    8017             :   {
    8018          14 :     case t_VEC: V = shallowtrans(V);
    8019          14 :     case t_COL: break;
    8020           0 :     default: pari_err_TYPE("mfsearch [V]", V);
    8021             :   }
    8022          14 :   vN = search_levels(gel(NK,1), "mfsearch [N]");
    8023          14 :   lvN = lg(vN);
    8024             : 
    8025          14 :   Qtoss(gk, &nk,&dk);
    8026          14 :   parity = (dk == 1 && odd(nk)) ? -1 : 1;
    8027          14 :   nV = lg(V)-2;
    8028          14 :   F = cgetg(1, t_VEC);
    8029          14 :   NbyD = const_vec(vN[lvN-1], cgetg(1,t_VECSMALL));
    8030         231 :   for (n = 1; n < lvN; n++)
    8031             :   {
    8032         217 :     long N = vN[n];
    8033             :     GEN L;
    8034         217 :     if (N <= 0 || (dk == 2 && (N & 3))) continue;
    8035         217 :     L = findd(N, parity);
    8036         630 :     for (i = 1; i < lg(L); i++)
    8037             :     {
    8038         413 :       GEN mf, M, CO, gD = gel(L,i);
    8039         413 :       GEN *ND = (GEN*)NbyD + itou(gD); /* points to NbyD[|D|] */
    8040             : 
    8041         413 :       if (seenD(N, *ND)) continue;
    8042         413 :       mf = mfinit_Nndkchi(N, nk, dk, get_mfchar(gD), space, 1);
    8043         413 :       M = mfcoefs_mf(mf, nV, 1);
    8044         413 :       CO = inverseimage(M, V); if (lg(CO) == 1) continue;
    8045             : 
    8046          42 :       F = vec_append(F, mflinear(mf,CO));
    8047          42 :       *ND = vecsmall_append(*ND, N); /* add to NbyD[|D|] */
    8048             :     }
    8049             :   }
    8050          14 :   return gerepilecopy(av, F);
    8051             : }
    8052             : 
    8053             : static GEN
    8054         882 : search_from_split(GEN mf, GEN vap, GEN vlp)
    8055             : {
    8056         882 :   pari_sp av = avma;
    8057         882 :   long lvlp = lg(vlp), j, jv, l1;
    8058         882 :   GEN v, NK, S1, S, M = NULL;
    8059             : 
    8060         882 :   S1 = gel(split_i(mf, 1, 0), 1); /* rational newforms */
    8061         882 :   l1 = lg(S1);
    8062         882 :   if (l1 == 1) return gc_NULL(av);
    8063         448 :   v = cgetg(l1, t_VEC);
    8064         448 :   S = MF_get_S(mf);
    8065         448 :   NK = mf_get_NK(gel(S,1));
    8066         448 :   if (lvlp > 1) M = rowpermute(mfcoefs_mf(mf, vlp[lvlp-1], 1), vlp);
    8067         966 :   for (j = jv = 1; j < l1; j++)
    8068             :   {
    8069         518 :     GEN vF = gel(S1,j);
    8070             :     long t;
    8071         651 :     for (t = lvlp-1; t > 0; t--)
    8072             :     { /* lhs = vlp[j]-th coefficient of eigenform */
    8073         595 :       GEN rhs = gel(vap,t), lhs = RgMrow_RgC_mul(M, vF, t);
    8074         595 :       if (!gequal(lhs, rhs)) break;
    8075             :     }
    8076         518 :     if (!t) gel(v,jv++) = mflinear_i(NK,S,vF);
    8077             :   }
    8078         448 :   if (jv == 1) return gc_NULL(av);
    8079          56 :   setlg(v,jv); return v;
    8080             : }
    8081             : GEN
    8082          28 : mfeigensearch(GEN NK, GEN AP)
    8083             : {
    8084          28 :   pari_sp av = avma;
    8085          28 :   GEN k, vN, vap, vlp, vres = cgetg(1, t_VEC), D;
    8086             :   long n, lvN, i, l, even;
    8087             : 
    8088          28 :   if (!AP) l = 1;
    8089             :   else
    8090             :   {
    8091          28 :     l = lg(AP);
    8092          28 :     if (typ(AP) != t_VEC) pari_err_TYPE("mfeigensearch",AP);
    8093             :   }
    8094          28 :   vap = cgetg(l, t_VEC);
    8095          28 :   vlp = cgetg(l, t_VECSMALL);
    8096          28 :   if (l > 1)
    8097             :   {
    8098          28 :     GEN perm = indexvecsort(AP, mkvecsmall(1));
    8099          77 :     for (i = 1; i < l; i++)
    8100             :     {
    8101          49 :       GEN v = gel(AP,perm[i]), gp, ap;
    8102          49 :       if (typ(v) != t_VEC || lg(v) != 3) pari_err_TYPE("mfeigensearch", AP);
    8103          49 :       gp = gel(v,1);
    8104          49 :       ap = gel(v,2);
    8105          49 :       if (typ(gp) != t_INT || (typ(ap) != t_INT && typ(ap) != t_INTMOD))
    8106           0 :         pari_err_TYPE("mfeigensearch", AP);
    8107          49 :       gel(vap,i) = ap;
    8108          49 :       vlp[i] = itos(gp)+1; if (vlp[i] < 0) pari_err_TYPE("mfeigensearch", AP);
    8109             :     }
    8110             :   }
    8111          28 :   l = lg(NK);
    8112          28 :   if (typ(NK) != t_VEC || l != 3) pari_err_TYPE("mfeigensearch",NK);
    8113          28 :   k = gel(NK,2);
    8114          28 :   vN = search_levels(gel(NK,1), "mfeigensearch [N]");
    8115          28 :   lvN = lg(vN);
    8116          28 :   vecsmall_sort(vlp);
    8117          28 :   even = !mpodd(k);
    8118         966 :   for (n = 1; n < lvN; n++)
    8119             :   {
    8120         938 :     pari_sp av2 = avma;
    8121             :     GEN mf, L;
    8122         938 :     long N = vN[n];
    8123         938 :     if (even) D = gen_1;
    8124             :     else
    8125             :     {
    8126         112 :       long r = (N&3L);
    8127         112 :       if (r == 1 || r == 2) continue;
    8128          56 :       D = stoi( corediscs(-N, NULL) ); /* < 0 */
    8129             :     }
    8130         882 :     mf = mfinit_i(mkvec3(utoipos(N), k, D), mf_NEW);
    8131         882 :     L = search_from_split(mf, vap, vlp);
    8132         882 :     if (L) vres = shallowconcat(vres, L); else set_avma(av2);
    8133             :   }
    8134          28 :   return gerepilecopy(av, vres);
    8135             : }
    8136             : 
    8137             : /* tf_{N,k}(n) */
    8138             : static GEN
    8139     2234225 : mfnewtracecache(long N, long k, long n, cachenew_t *cache)
    8140             : {
    8141     2234225 :   GEN C = NULL, S;
    8142             :   long lcache;
    8143     2234225 :   if (!n) return gen_0;
    8144     2154719 :   S = gel(cache->vnew,N);
    8145     2154719 :   lcache = lg(S);
    8146     2154719 :   if (n < lcache) C = gel(S, n);
    8147     2154719 :   if (C) cache->newHIT++;
    8148     1296834 :   else C = mfnewtrace_i(N,k,n,cache);
    8149     2154719 :   cache->newTOTAL++;
    8150     2154719 :   if (n < lcache) gel(S,n) = C;
    8151     2154719 :   return C;
    8152             : }
    8153             : 
    8154             : static long
    8155        1386 : mfdim_Nkchi(long N, long k, GEN CHI, long space)
    8156             : {
    8157        1386 :   if (k < 0 || badchar(N,k,CHI)) return 0;
    8158        1085 :   if (k == 0)
    8159          35 :     return mfcharistrivial(CHI) && !space_is_cusp(space)? 1: 0;
    8160        1050 :   switch(space)
    8161             :   {
    8162         238 :     case mf_NEW: return mfnewdim(N,k,CHI);
    8163         196 :     case mf_CUSP:return mfcuspdim(N,k,CHI);
    8164         168 :     case mf_OLD: return mfolddim(N,k,CHI);
    8165         217 :     case mf_FULL:return mffulldim(N,k,CHI);
    8166         231 :     case mf_EISEN: return mfeisensteindim(N,k,CHI);
    8167           0 :     default: pari_err_FLAG("mfdim");
    8168             :   }
    8169             :   return 0;/*LCOV_EXCL_LINE*/
    8170             : }
    8171             : static long
    8172        2114 : mfwt1dimsum(long N, long space)
    8173             : {
    8174        2114 :   switch(space)
    8175             :   {
    8176        1050 :     case mf_NEW:  return mfwt1newdimsum(N);
    8177        1057 :     case mf_CUSP: return mfwt1cuspdimsum(N);
    8178           7 :     case mf_OLD:  return mfwt1olddimsum(N);
    8179             :   }
    8180           0 :   pari_err_FLAG("mfdim");
    8181             :   return 0; /*LCOV_EXCL_LINE*/
    8182             : }
    8183             : /* mfdim for k = nk/dk */
    8184             : static long
    8185       44744 : mfdim_Nndkchi(long N, long nk, long dk, GEN CHI, long space)
    8186       43463 : { return (dk == 2)? mf2dim_Nkchi(N, nk >> 1, CHI, space)
    8187       88207 :                   : mfdim_Nkchi(N, nk, CHI, space); }
    8188             : /* FIXME: use direct dim Gamma1(N) formula, don't compute individual spaces */
    8189             : static long
    8190         252 : mfwtkdimsum(long N, long k, long dk, long space)
    8191             : {
    8192         252 :   GEN w = mfchars(N, k, dk, NULL);
    8193         252 :   long i, j, D = 0, l = lg(w);
    8194        1239 :   for (i = j = 1; i < l; i++)
    8195             :   {
    8196         987 :     GEN CHI = gel(w,i);
    8197         987 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8198         987 :     if (d) D += d * myeulerphiu(mfcharorder(CHI));
    8199             :   }
    8200         252 :   return D;
    8201             : }
    8202             : static GEN
    8203         105 : mfwt1dims(long N, GEN vCHI, long space)
    8204             : {
    8205         105 :   GEN D = NULL;
    8206         105 :   switch(space)
    8207             :   {
    8208          56 :     case mf_NEW: D = mfwt1newdimall(N, vCHI); break;
    8209          21 :     case mf_CUSP:D = mfwt1cuspdimall(N, vCHI); break;
    8210          28 :     case mf_OLD: D = mfwt1olddimall(N, vCHI); break;
    8211           0 :     default: pari_err_FLAG("mfdim");
    8212             :   }
    8213         105 :   return D;
    8214             : }
    8215             : static GEN
    8216        2961 : mfwtkdims(long N, long k, long dk, GEN vCHI, long space)
    8217             : {
    8218        2961 :   GEN D, w = mfchars(N, k, dk, vCHI);
    8219        2961 :   long i, j, l = lg(w);
    8220        2961 :   D = cgetg(l, t_VEC);
    8221       46592 :   for (i = j = 1; i < l; i++)
    8222             :   {
    8223       43631 :     GEN CHI = gel(w,i);
    8224       43631 :     long d = mfdim_Nndkchi(N,k,dk,CHI,space);
    8225       43631 :     if (vCHI)
    8226         574 :       gel(D, j++) = mkvec2s(d, 0);
    8227       43057 :     else if (d)
    8228        2520 :       gel(D, j++) = fmt_dim(CHI, d, 0);
    8229             :   }
    8230        2961 :   setlg(D,j); return D;
    8231             : }
    8232             : GEN
    8233        5719 : mfdim(GEN NK, long space)
    8234             : {
    8235        5719 :   pari_sp av = avma;
    8236             :   long N, k, dk, joker;
    8237             :   GEN CHI, mf;
    8238        5719 :   if ((mf = checkMF_i(NK))) return utoi(MF_get_dim(mf));
    8239        5586 :   checkNK2(NK, &N, &k, &dk, &CHI, 2);
    8240        5586 :   if (!CHI) joker = 1;
    8241             :   else
    8242        2611 :     switch(typ(CHI))
    8243             :     {
    8244        2373 :       case t_INT: joker = 2; break;
    8245         112 :       case t_COL: joker = 3; break;
    8246         126 :       default: joker = 0; break;
    8247             :     }
    8248        5586 :   if (joker)
    8249             :   {
    8250             :     long d;
    8251             :     GEN D;
    8252        5460 :     if (k < 0) switch(joker)
    8253             :     {
    8254           0 :       case 1: return cgetg(1,t_VEC);
    8255           7 :       case 2: return gen_0;
    8256           0 :       case 3: return mfdim0all(CHI);
    8257             :     }
    8258        5453 :     if (k == 0)
    8259             :     {
    8260          28 :       if (space_is_cusp(space)) switch(joker)
    8261             :       {
    8262           7 :         case 1: return cgetg(1,t_VEC);
    8263           0 :         case 2: return gen_0;
    8264           7 :         case 3: return mfdim0all(CHI);
    8265             :       }
    8266          14 :       switch(joker)
    8267             :       {
    8268             :         long i, l;
    8269           7 :         case 1: retmkvec(fmt_dim(mfchartrivial(),0,0));
    8270           0 :         case 2: return gen_1;
    8271           7 :         case 3: l = lg(CHI); D = cgetg(l,t_VEC);
    8272          35 :                 for (i = 1; i < l; i++)
    8273             :                 {
    8274          28 :                   long t = mfcharistrivial(gel(CHI,i));
    8275          28 :                   gel(D,i) = mkvec2(t? gen_1: gen_0, gen_0);
    8276             :                 }
    8277           7 :                 return D;
    8278             :       }
    8279             :     }
    8280        5425 :     if (dk == 1 && k == 1 && space != mf_EISEN)
    8281         105 :     {
    8282        2219 :       long fix = 0, space0 = space;
    8283        2219 :       if (space == mf_FULL) space = mf_CUSP; /* remove Eisenstein part */
    8284        2219 :       if (joker == 2)
    8285             :       {
    8286        2114 :         d = mfwt1dimsum(N, space);
    8287        2114 :         if (space0 == mf_FULL) d += mfwtkdimsum(N,k,dk,mf_EISEN);/*add it back*/
    8288        2114 :         set_avma(av); return utoi(d);
    8289             :       }
    8290             :       /* must initialize explicitly: trivial spaces for E_k/S_k differ */
    8291         105 :       if (space0 == mf_FULL)
    8292             :       {
    8293           7 :         if (!CHI) fix = 1; /* must remove 0 spaces */
    8294           7 :         CHI = mfchars(N, k, dk, CHI);
    8295             :       }
    8296         105 :       D = mfwt1dims(N, CHI, space);
    8297         105 :       if (space0 == mf_FULL)
    8298             :       {
    8299           7 :         GEN D2 = mfwtkdims(N, k, dk, CHI, mf_EISEN);
    8300           7 :         D = merge_dims(D, D2, fix? CHI: NULL);
    8301             :       }
    8302             :     }
    8303             :     else
    8304             :     {
    8305        3206 :       if (joker==2) { d = mfwtkdimsum(N,k,dk,space); set_avma(av); return utoi(d); }
    8306        2954 :       D = mfwtkdims(N, k, dk, CHI, space);
    8307             :     }
    8308        3059 :     if (!CHI) return gerepileupto(av, vecsort(D, mkvecsmall(1)));
    8309         105 :     return gerepilecopy(av, D);
    8310             :   }
    8311         126 :   return utoi( mfdim_Nndkchi(N, k, dk, CHI, space) );
    8312             : }
    8313             : 
    8314             : GEN
    8315         308 : mfbasis(GEN NK, long space)
    8316             : {
    8317         308 :   pari_sp av = avma;
    8318             :   long N, k, dk;
    8319             :   GEN mf, CHI;
    8320         308 :   if ((mf = checkMF_i(NK))) return concat(gel(mf,2), gel(mf,3));
    8321           7 :   checkNK2(NK, &N, &k, &dk, &CHI, 0);
    8322           7 :   if (dk == 2) return gerepilecopy(av, mf2basis(N, k>>1, CHI, space));
    8323           7 :   mf = mfinit_Nkchi(N, k, CHI, space, 1);
    8324           7 :   return gerepilecopy(av, MF_get_basis(mf));
    8325             : }
    8326             : 
    8327             : static GEN
    8328          28 : deg1ser_shallow(GEN a1, GEN a0, long v, long e)
    8329          28 : { return RgX_to_ser(deg1pol_shallow(a1, a0, v), e+2); }
    8330             : /* r / x + O(1) */
    8331             : static GEN
    8332          28 : simple_pole(GEN r)
    8333             : {
    8334          28 :   GEN S = deg1ser_shallow(gen_0, r, 0, 1);
    8335          28 :   setvalp(S, -1); return S;
    8336             : }
    8337             : 
    8338             : /* F form, E embedding; mfa = mfatkininit or root number (eigenform case) */
    8339             : static GEN
    8340         105 : mflfuncreate(GEN mfa, GEN F, GEN E, GEN N, GEN gk)
    8341             : {
    8342         105 :   GEN LF = cgetg(8,t_VEC), polar = cgetg(1,t_COL), eps;
    8343         105 :   long k = itou(gk);
    8344         105 :   gel(LF,1) = lfuntag(t_LFUN_MFCLOS, mkvec3(F,E,gen_1));
    8345         105 :   if (typ(mfa) != t_VEC)
    8346          84 :     eps = mfa; /* cuspidal eigenform: root number; no poles */
    8347             :   else
    8348             :   { /* mfatkininit */
    8349          21 :     GEN a0, b0, vF, vG, G = NULL, M = gdiv(gel(mfa,2), gel(mfa,3)), mf = gel(mfa,4);
    8350          21 :     vF = mftobasis_i(mf, F);
    8351          21 :     vG = RgM_RgC_mul(M, vF);
    8352          21 :     if (gequal(vF,vG)) eps = gen_1;
    8353           7 :     else if (gequal(vF,gneg(vG))) eps = gen_m1;
    8354             :     else
    8355             :     { /* not self-dual */
    8356           7 :       eps = NULL;
    8357           7 :       G = mfatkin(mfa, F);
    8358           7 :       gel(LF,2) = lfuntag(t_LFUN_MFCLOS, mkvec3(G,E,ginv(gel(mfa,3))));
    8359           7 :       gel(LF,6) = powIs(k);
    8360             :     }
    8361             :     /* polar part */
    8362          21 :     a0 = mfcoef(F,0);
    8363          21 :     b0 = eps? gmul(eps,a0): mfcoef(G,0);
    8364          21 :     if (!gequal0(b0))
    8365             :     {
    8366          14 :       b0 = mulcxpowIs(gmul2n(b0,1), k);
    8367          14 :       polar = vec_append(polar, mkvec2(gk, simple_pole(b0)));
    8368             :     }
    8369          21 :     if (!gequal0(a0))
    8370             :     {
    8371          14 :       a0 = gneg(gmul2n(a0,1));
    8372          14 :       polar = vec_append(polar, mkvec2(gen_0, simple_pole(a0)));
    8373             :     }
    8374             :   }
    8375         105 :   if (eps) /* self-dual */
    8376             :   {
    8377          98 :     gel(LF,2) = mfcharorder(mf_get_CHI(F)) <= 2? gen_0: gen_1;
    8378          98 :     gel(LF,6) = mulcxpowIs(eps,k);
    8379             :   }
    8380         105 :   gel(LF,3) = mkvec2(gen_0, gen_1);
    8381         105 :   gel(LF,4) = gk;
    8382         105 :   gel(LF,5) = N;
    8383         105 :   if (lg(polar) == 1) setlg(LF,7); else gel(LF,7) = polar;
    8384         105 :   return LF;
    8385             : }
    8386             : static GEN
    8387          91 : mflfuncreateall(long sd, GEN mfa, GEN F, GEN vE, GEN gN, GEN gk)
    8388             : {
    8389          91 :   long i, l = lg(vE);
    8390          91 :   GEN L = cgetg(l, t_VEC);
    8391         196 :   for (i = 1; i < l; i++)
    8392         105 :     gel(L,i) = mflfuncreate(sd? gel(mfa,i): mfa, F, gel(vE,i), gN, gk);
    8393          91 :   return L;
    8394             : }
    8395             : GEN
    8396          42 : lfunmf(GEN mf, GEN F, long bitprec)
    8397             : {
    8398          42 :   pari_sp av = avma;
    8399          42 :   long i, l, prec = nbits2prec(bitprec);
    8400             :   GEN L, gk, gN;
    8401          42 :   mf = checkMF(mf);
    8402          42 :   gk = MF_get_gk(mf);
    8403          42 :   gN = MF_get_gN(mf);
    8404          42 :   if (typ(gk)!=t_INT) pari_err_IMPL("half-integral weight");
    8405          42 :   if (F)
    8406             :   {
    8407             :     GEN v;
    8408          35 :     long s = MF_get_space(mf);
    8409          35 :     if (!checkmf_i(F)) pari_err_TYPE("lfunmf", F);
    8410          35 :     if (!mfisinspace_i(mf, F)) err_space(F);
    8411          35 :     L = NULL;
    8412          35 :     if ((s == mf_NEW || s == mf_CUSP || s == mf_FULL)
    8413          21 :         && gequal(mfcoefs_i(F,1,1), mkvec2(gen_0,gen_1)))
    8414             :     { /* check if eigenform */
    8415          14 :       GEN vP, vF, b = mftobasis_i(mf, F);
    8416          14 :       long lF, d = degpol(mf_get_field(F));
    8417          14 :       v = mfsplit(mf, d, 0);
    8418          14 :       vF = gel(v,1);
    8419          14 :       vP = gel(v,2); lF = lg(vF);
    8420          14 :       for (i = 1; i < lF; i++)
    8421          14 :         if (degpol(gel(vP,i)) == d && gequal(gel(vF,i), b))
    8422             :         {
    8423          14 :           GEN vE = mfgetembed(F, prec);
    8424          14 :           GEN Z = mffrickeeigen_i(mf, mkvec(b), mkvec(vE), prec);
    8425          14 :           L = mflfuncreateall(1, gel(Z,1), F, vE, gN, gk);
    8426          14 :           break;
    8427             :         }
    8428             :     }
    8429          35 :     if (!L)
    8430             :     { /* not an eigenform: costly general case */
    8431          21 :       GEN mfa = mfatkininit_i(mf, itou(gN), 1, prec);
    8432          21 :       L = mflfuncreateall(0,mfa, F, mfgetembed(F,prec), gN, gk);
    8433             :     }
    8434          35 :     if (lg(L) == 2) L = gel(L,1);
    8435             :   }
    8436             :   else
    8437             :   {
    8438           7 :     GEN M = mfeigenbasis(mf), vE = mfeigenembed(mf, prec);
    8439           7 :     GEN v = mffrickeeigen(mf, vE, prec);
    8440           7 :     l = lg(vE); L = cgetg(l, t_VEC);
    8441          63 :     for (i = 1; i < l; i++)
    8442          56 :       gel(L,i) = mflfuncreateall(1,gel(v,i), gel(M,i), gel(vE,i), gN, gk);
    8443             :   }
    8444          42 :   return gerepilecopy(av, L);
    8445             : }
    8446             : 
    8447             : GEN
    8448          21 : mffromell(GEN E)
    8449             : {
    8450          21 :   pari_sp av = avma;
    8451             :   GEN mf, F, z, v, S;
    8452             :   long N, i, l;
    8453             : 
    8454          21 :   checkell(E);
    8455          21 :   if (ell_get_type(E) != t_ELL_Q) pari_err_TYPE("mfffromell [E not over Q]", E);
    8456          21 :   N = itos(ellQ_get_N(E));
    8457          21 :   mf = mfinit_i(mkvec2(utoi(N), gen_2), mf_NEW);
    8458          21 :   v = split_i(mf, 1, 0);
    8459          21 :   S = gel(v,1); l = lg(S); /* rational newforms */
    8460          21 :   F = tag(t_MF_ELL, mkNK(N,2,mfchartrivial()), E);
    8461          21 :   z = mftobasis_i(mf, F);
    8462          21 :   for(i = 1; i < l; i++)
    8463          21 :     if (gequal(z, gel(S,i))) break;
    8464          21 :   if (i == l) pari_err_BUG("mffromell [E is not modular]");
    8465          21 :   return gerepilecopy(av, mkvec3(mf, F, z));
    8466             : }
    8467             : 
    8468             : /* returns -1 if not, degree otherwise */
    8469             : long
    8470          98 : polishomogeneous(GEN P)
    8471             : {
    8472             :   long i, D, l;
    8473          98 :   if (typ(P) != t_POL) return 0;
    8474          49 :   D = -1; l = lg(P);
    8475         231 :   for (i = 2; i < l; i++)
    8476             :   {
    8477         182 :     GEN c = gel(P,i);
    8478             :     long d;
    8479         182 :     if (gequal0(c)) continue;
    8480          84 :     d = polishomogeneous(c);
    8481          84 :     if (d < 0) return -1;
    8482          84 :     if (D < 0) D = d + i-2; else if (D != d + i-2) return -1;
    8483             :   }
    8484          49 :   return D;
    8485             : }
    8486             : 
    8487             : /* P a t_POL, 1 if spherical, 0 otherwise */
    8488             : static int
    8489          14 : RgX_isspherical(GEN Qi, GEN P)
    8490             : {
    8491          14 :   pari_sp av = avma;
    8492             :   GEN va, S;
    8493             :   long lva, i, j;
    8494          14 :   if (degpol(P) <= 1) return 1;
    8495          14 :   va = variables_vecsmall(P); lva = lg(va);
    8496          14 :   if (lva > lg(Qi)) pari_err(e_MISC, "too many variables in mffromqf");
    8497          14 :   S = gen_0;
    8498          42 :   for (j = 1; j < lva; j++)
    8499             :   {
    8500          28 :     GEN col = gel(Qi, j), Pj = deriv(P, va[j]);
    8501          70 :     for (i = 1; i <= j; i++)
    8502             :     {
    8503          42 :       GEN coe = gel(col, i);
    8504          42 :       if (i != j) coe = gmul2n(coe, 1);
    8505          42 :       if (!gequal0(coe)) S = gadd(S, gmul(coe, deriv(Pj, va[i])));
    8506             :     }
    8507             :   }
    8508          14 :   return gc_bool(av, gequal0(S));
    8509             : }
    8510             : 
    8511             : static GEN
    8512          28 : c_QFsimple_i(long n, GEN Q, GEN P)
    8513             : {
    8514          28 :   pari_sp av = avma;
    8515          28 :   GEN V, v = qfrep0(Q, utoi(n), 1);
    8516          28 :   long i, l = lg(v);
    8517          28 :   V = cgetg(l+1, t_VEC);
    8518          49 :   if (!P || equali1(P))
    8519             :   {
    8520          21 :     gel(V,1) = gen_1;
    8521          21 :     for (i = 2; i <= l; i++) gel(V,i) = utoi(v[i-1] << 1);
    8522             :   }
    8523             :   else
    8524             :   {
    8525           7 :     gel(V,1) = gcopy(P);
    8526           7 :     for (i = 2; i <= l; i++) gel(V,i) = gmulgs(P, v[i-1] << 1);
    8527             :   }
    8528          28 :   return gerepileupto(av, V);
    8529             : }
    8530             : static GEN
    8531          35 : c_QF_i(long n, GEN Q, GEN P)
    8532             : {
    8533          35 :   pari_sp av = avma;
    8534             :   GEN V, v, va;
    8535             :   long i, lva, lq, l;
    8536          35 :   if (!P || typ(P) != t_POL) return c_QFsimple_i(n, Q, P);
    8537           7 :   v = gel(minim(Q, utoi(2*n), NULL), 3);
    8538           7 :   va = variables_vec(P); lq = lg(Q) - 1; lva = lg(va) - 1;
    8539           7 :   V = zerovec(n + 1); l = lg(v);
    8540          35 :   for (i = 1; i < l; i++)
    8541             :   {
    8542          28 :     GEN X = gel(v,i);
    8543          28 :     long ind = (itos(qfeval0(Q, X, NULL)) >> 1) + 1;
    8544          28 :     if (lq > lva) X = vecslice(X, 1, lva);
    8545          28 :     gel(V, ind) = gadd(gel(V, ind), gsubstvec(P, va, X));
    8546             :   }
    8547           7 :   return gerepilecopy(av, gmul2n(V, 1));
    8548             : }
    8549             : 
    8550             : GEN
    8551          42 : mffromqf(GEN Q, GEN P)
    8552             : {
    8553          42 :   pari_sp av = avma;
    8554             :   GEN G, Qi, F, D, N, mf, v, gk, gwt, chi;
    8555             :   long m, d, space;
    8556          42 :   if (typ(Q) != t_MAT) pari_err_TYPE("mffromqf", Q);
    8557          42 :   if (!RgM_is_ZM(Q) || !qf_iseven(Q))
    8558           0 :     pari_err_TYPE("mffromqf [not integral or even]", Q);
    8559          42 :   m = lg(Q)-1;
    8560          42 :   gk = sstoQ(m, 2);
    8561          42 :   Qi = ZM_inv(Q, &N);
    8562          42 :   if (!qf_iseven(Qi)) N = shifti(N, 1);
    8563          42 :   d = 0;
    8564          42 :   if (!P || gequal1(P)) P = NULL;
    8565             :   else
    8566             :   {
    8567          21 :     P = simplify_shallow(P);
    8568          21 :     if (typ(P) == t_POL)
    8569             :     {
    8570          14 :       d = polishomogeneous(P);
    8571          14 :       if (d < 0) pari_err_TYPE("mffromqf [not homogeneous t_POL]", P);
    8572          14 :       if (!RgX_isspherical(Qi, P))
    8573           0 :         pari_err_TYPE("mffromqf [not a spherical t_POL]", P);
    8574             :     }
    8575             :   }
    8576          42 :   D = ZM_det(Q);
    8577          42 :   if (typ(gk) == t_INT) { if (mpodd(gk)) D = negi(D); } else D = shifti(D, 1);
    8578          42 :   space = d > 0 ? mf_CUSP : mf_FULL;
    8579          42 :   G = znstar0(N,1);
    8580          42 :   chi = mkvec2(G, znchar_quad(G,D));
    8581          42 :   gwt = gaddgs(gk, d);
    8582          42 :   mf = mfinit(mkvec3(N, gwt, chi), space);
    8583          42 :   if (odd(d))
    8584             :   {
    8585           7 :     F = mftrivial();
    8586           7 :     v = zerocol(MF_get_dim(mf));
    8587             :   }
    8588             :   else
    8589             :   {
    8590          35 :     F = c_QF_i(mfsturm(mf), Q, P);
    8591          35 :     v = mftobasis_i(mf, F);
    8592          35 :     F = mflinear(mf, v);
    8593             :   }
    8594          42 :   return gerepilecopy(av, mkvec3(mf, F, v));
    8595             : }
    8596             : 
    8597             : /***********************************************************************/
    8598             : /*                          Eisenstein Series                          */
    8599             : /***********************************************************************/
    8600             : /* \sigma_{k-1}(\chi,n) */
    8601             : static GEN
    8602       22001 : sigchi(long k, GEN CHI, long n)
    8603             : {
    8604       22001 :   pari_sp av = avma;
    8605       22001 :   GEN S = gen_1, D = mydivisorsu(u_ppo(n,mfcharmodulus(CHI)));
    8606       22001 :   long i, l = lg(D), ord = mfcharorder(CHI), vt = varn(mfcharpol(CHI));
    8607       77574 :   for (i = 2; i < l; i++) /* skip D[1] = 1 */
    8608             :   {
    8609       55573 :     long d = D[i], a = mfcharevalord(CHI, d, ord);
    8610       55573 :     S = gadd(S, mygmodulo_lift(a, ord, powuu(d, k-1), vt));
    8611             :   }
    8612       22001 :   return gerepileupto(av,S);
    8613             : }
    8614             : 
    8615             : /* write n = n0*n1*n2, (n0,N1*N2) = 1, n1 | N1^oo, n2 | N2^oo;
    8616             :  * return NULL if (n,N1,N2) > 1, else return factoru(n0) */
    8617             : static GEN
    8618      198989 : sigchi2_dec(long n, long N1, long N2, long *pn1, long *pn2)
    8619             : {
    8620      198989 :   GEN P0, E0, P, E, fa = myfactoru(n);
    8621             :   long i, j, l;
    8622      198989 :   *pn1 = 1;
    8623      198989 :   *pn2 = 1;
    8624      198989 :   if (N1 == 1 && N2 == 1) return fa;
    8625      185493 :   P = gel(fa,1); l = lg(P);
    8626      185493 :   E = gel(fa,2);
    8627      185493 :   P0 = cgetg(l, t_VECSMALL);
    8628      185493 :   E0 = cgetg(l, t_VECSMALL);
    8629      402626 :   for (i = j = 1; i < l; i++)
    8630             :   {
    8631      239078 :     long p = P[i], e = E[i];
    8632      239078 :     if (N1 % p == 0)
    8633             :     {
    8634       37205 :       if (N2 % p == 0) return NULL;
    8635       15260 :       *pn1 *= upowuu(p,e);
    8636             :     }
    8637      201873 :     else if (N2 % p == 0)
    8638       39872 :       *pn2 *= upowuu(p,e);
    8639      162001 :     else { P0[j] = p; E0[j] = e; j++; }
    8640             :   }
    8641      163548 :   setlg(P0, j);
    8642      163548 :   setlg(E0, j); return mkvec2(P0,E0);
    8643             : }
    8644             : 
    8645             : /* sigma_{k-1}(\chi_1,\chi_2,n), ord multiple of lcm(ord(CHI1),ord(CHI2)) */
    8646             : static GEN
    8647      151032 : sigchi2(long k, GEN CHI1, GEN CHI2, long n, long ord)
    8648             : {
    8649      151032 :   pari_sp av = avma;
    8650      151032 :   GEN S = gen_0, D;
    8651      151032 :   long i, l, n1, n2, vt, N1 = mfcharmodulus(CHI1), N2 = mfcharmodulus(CHI2);
    8652      151032 :   D = sigchi2_dec(n, N1, N2, &n1, &n2); if (!D) { set_avma(av); return S; }
    8653      133455 :   D = divisorsu_fact(D); l = lg(D);
    8654      133455 :   vt = varn(mfcharpol(CHI1));
    8655      477015 :   for (i = 1; i < l; i++)
    8656             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8657      343560 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1; (n/d,N2) = 1 */
    8658      343560 :     a = mfcharevalord(CHI1, d, ord) + mfcharevalord(CHI2, nd, ord);
    8659      343560 :     if (a >= ord) a -= ord;
    8660      343560 :     S = gadd(S, mygmodulo_lift(a, ord, powuu(d, k-1), vt));
    8661             :   }
    8662      133455 :   return gerepileupto(av, S);
    8663             : }
    8664             : 
    8665             : /**************************************************************************/
    8666             : /**           Dirichlet characters with precomputed values               **/
    8667             : /**************************************************************************/
    8668             : /* CHI mfchar */
    8669             : static GEN
    8670       11375 : mfcharcxinit(GEN CHI, long prec)
    8671             : {
    8672       11375 :   GEN G = gel(CHI,1), chi = gel(CHI,2), z, V;
    8673       11375 :   GEN v = ncharvecexpo(G, znconrey_normalized(G,chi));
    8674       11375 :   long n, l = lg(v), o = mfcharorder(CHI);
    8675       11375 :   V = cgetg(l, t_VEC);
    8676       11375 :   z = grootsof1(o, prec); /* Mod(t, Phi_o(t)) -> e(1/o) */
    8677       11375 :   for (n = 1; n < l; n++) gel(V,n) = v[n] < 0? gen_0: gel(z, v[n]+1);
    8678       11375 :   return mkvecn(6, G, chi, gmfcharorder(CHI), v, V, mfcharpol(CHI));
    8679             : }
    8680             : /* v a "CHIvec" */
    8681             : static long
    8682    20373346 : CHIvec_N(GEN v) { return itou(znstar_get_N(gel(v,1))); }
    8683             : static GEN
    8684       11606 : CHIvec_CHI(GEN v)
    8685       11606 : { return mkvec4(gel(v,1), gel(v,2), gel(v,3), gel(v,6)); }
    8686             : /* character order */
    8687             : static long
    8688       32487 : CHIvec_ord(GEN v) { return itou(gel(v,3)); }
    8689             : /* character exponents, i.e. t such that chi(n) = e(t) */
    8690             : static GEN
    8691      333494 : CHIvec_expo(GEN v) { return gel(v,4); }
    8692             : /* character values chi(n) */
    8693             : static GEN
    8694    19870921 : CHIvec_val(GEN v) { return gel(v,5); }
    8695             : /* CHI(n) */
    8696             : static GEN
    8697    19857817 : mychareval(GEN v, long n)
    8698             : {
    8699    19857817 :   long N = CHIvec_N(v), ind = n%N;
    8700    19857817 :   if (ind <= 0) ind += N;
    8701    19857817 :   return gel(CHIvec_val(v), ind);
    8702             : }
    8703             : /* return c such that CHI(n) = e(c / ordz) or -1 if (n,N) > 1 */
    8704             : static long
    8705      333494 : mycharexpo(GEN v, long n)
    8706             : {
    8707      333494 :   long N = CHIvec_N(v), ind = n%N;
    8708      333494 :   if (ind <= 0) ind += N;
    8709      333494 :   return CHIvec_expo(v)[ind];
    8710             : }
    8711             : /* faster than mfcharparity */
    8712             : static long
    8713       37338 : CHIvec_parity(GEN v) { return mycharexpo(v,-1) ? -1: 1; }
    8714             : /**************************************************************************/
    8715             : 
    8716             : static ulong
    8717       47957 : sigchi2_Fl(long k, GEN CHI1vec, GEN CHI2vec, long n, GEN vz, ulong p)
    8718             : {
    8719       47957 :   pari_sp av = avma;
    8720       47957 :   long ordz = lg(vz)-2, i, l, n1, n2;
    8721       47957 :   ulong S = 0;
    8722       47957 :   GEN D = sigchi2_dec(n, CHIvec_N(CHI1vec), CHIvec_N(CHI2vec), &n1, &n2);
    8723       47957 :   if (!D) return gc_ulong(av,S);
    8724       43589 :   D = divisorsu_fact(D);
    8725       43589 :   l = lg(D);
    8726      147294 :   for (i = 1; i < l; i++)
    8727             :   { /* S += d^(k-1)*chi1(d)*chi2(n/d) */
    8728      103705 :     long a, d = n2*D[i], nd = n1*D[l-i]; /* (d,N1)=1, (n/d,N2)=1 */
    8729      103705 :     a = mycharexpo(CHI2vec, nd) + mycharexpo(CHI1vec, d);
    8730      103705 :     if (a >= ordz) a -= ordz;
    8731      103705 :     S = Fl_add(S, mygmodulo_Fl(a, vz, Fl_powu(d,k-1,p), p), p);
    8732             :   }
    8733       43589 :   return gc_ulong(av,S);
    8734             : }
    8735             : 
    8736             : /**********************************************************************/
    8737             : /* Fourier expansions of Eisenstein series                            */
    8738             : /**********************************************************************/
    8739             : /* L(CHI,0) / 2, order(CHI) | ord != 0 */
    8740             : static GEN
    8741        1624 : charLFwt1(GEN CHI, long ord)
    8742             : {
    8743             :   GEN S;
    8744        1624 :   long r, vt, m = mfcharmodulus(CHI);
    8745             : 
    8746        1624 :   if (m == 1) return mkfrac(gen_m1,stoi(4));
    8747        1624 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8748       42469 :   for (r = 1; r < m; r++)
    8749             :   { /* S += r*chi(r) */
    8750             :     long a;
    8751       40845 :     if (ugcd(m,r) != 1) continue;
    8752       31402 :     a = mfcharevalord(CHI,r,ord);
    8753       31402 :     S = gadd(S, mygmodulo_lift(a, ord, utoi(r), vt));
    8754             :   }
    8755        1624 :   return gdivgs(S, -2*m);
    8756             : }
    8757             : /* L(CHI,0) / 2, mod p */
    8758             : static ulong
    8759        1323 : charLFwt1_Fl(GEN CHIvec, GEN vz, ulong p)
    8760             : {
    8761        1323 :   long r, m = CHIvec_N(CHIvec);
    8762             :   ulong S;
    8763        1323 :   if (m == 1) return Rg_to_Fl(mkfrac(gen_m1,stoi(4)), p);
    8764        1323 :   S = 0;
    8765       64659 :   for (r = 1; r < m; r++)
    8766             :   { /* S += r*chi(r) */
    8767       63336 :     long a = mycharexpo(CHIvec,r);
    8768       63336 :     if (a < 0) continue;
    8769       61166 :     S = Fl_add(S, mygmodulo_Fl(a, vz, r, p), p);
    8770             :   }
    8771        1323 :   return Fl_div(Fl_neg(S,p), 2*m, p);
    8772             : }
    8773             : /* L(CHI,1-k) / 2, order(CHI) | ord != 0 */
    8774             : static GEN
    8775        1386 : charLFwtk(long k, GEN CHI, long ord)
    8776             : {
    8777             :   GEN S, P, dS;
    8778             :   long r, m, vt;
    8779             : 
    8780        1386 :   if (k == 1) return charLFwt1(CHI, ord);
    8781        1386 :   m = mfcharmodulus(CHI);
    8782        1386 :   if (m == 1) return gdivgs(bernfrac(k),-2*k);
    8783         791 :   S = gen_0; vt = varn(mfcharpol(CHI));
    8784         791 :   P = ZX_rescale(Q_remove_denom(bernpol(k,0), &dS), utoi(m));
    8785         791 :   dS = mul_denom(dS, stoi(-2*m*k));
    8786       10171 :   for (r = 1; r < m; r++)
    8787             :   { /* S += P(r)*chi(r) */
    8788             :     long a;
    8789        9380 :     if (ugcd(r,m) != 1) continue;
    8790        7602 :     a = mfcharevalord(CHI,r,ord);
    8791        7602 :     S = gadd(S, mygmodulo_lift(a, ord, poleval(P, utoi(r)), vt));
    8792             :   }
    8793         791 :   return gdiv(S, dS);
    8794             : }
    8795             : /* L(CHI,1-k) / 2, mod p */
    8796             : static ulong
    8797        1988 : charLFwtk_Fl(long k, GEN CHIvec, GEN vz, ulong p)
    8798             : {
    8799             :   GEN P;
    8800             :   long r, m;
    8801             :   ulong S;
    8802        1988 :   if (k == 1) return charLFwt1_Fl(CHIvec, vz, p);
    8803         665 :   m = CHIvec_N(CHIvec);
    8804         665 :   if (m == 1) return Rg_to_Fl(gdivgs(bernfrac(k),-2*k), p);
    8805         399 :   S = 0;
    8806         399 :   P = RgX_to_Flx(RgX_rescale(bernpol(k,0), utoi(m)), p);
    8807        8085 :   for (r = 1; r < m; r++)
    8808             :   { /* S += P(r)*chi(r) */
    8809        7686 :     long a = mycharexpo(CHIvec,r);
    8810        7686 :     if (a < 0) continue;
    8811        6566 :     S = Fl_add(S, mygmodulo_Fl(a, vz, Flx_eval(P,r,p), p), p);
    8812             :   }
    8813         399 :   return Fl_div(Fl_neg(S,p), 2*k*m, p);
    8814             : }
    8815             : 
    8816             : static GEN
    8817        5621 : mfeisenstein2_0(long k, GEN CHI1, GEN CHI2, long ord)
    8818             : {
    8819        5621 :   if (k == 1 && mfcharistrivial(CHI1))
    8820        1624 :     return charLFwt1(CHI2, ord);
    8821        3997 :   else if (mfcharistrivial(CHI2))
    8822        1239 :     return charLFwtk(k, CHI1, ord);
    8823        2758 :   else return gen_0;
    8824             : }
    8825             : static ulong
    8826        3290 : mfeisenstein2_0_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p)
    8827             : {
    8828        3290 :   if (k == 1 && CHIvec_ord(CHI1vec) == 1)
    8829        1323 :     return charLFwtk_Fl(k, CHI2vec, vz, p);
    8830        1967 :   else if (CHIvec_ord(CHI2vec) == 1)
    8831         665 :     return charLFwtk_Fl(k, CHI1vec, vz, p);
    8832        1302 :   else return 0;
    8833             : }
    8834             : static GEN
    8835          84 : NK_eisen2(long k, GEN CHI1, GEN CHI2)
    8836             : {
    8837          84 :   long N = mfcharmodulus(CHI1)*mfcharmodulus(CHI2);
    8838          84 :   return mkNK(N, k, mfcharmul(CHI1,CHI2));
    8839             : }
    8840             : static GEN
    8841         259 : mfeisenstein_i(long k, GEN CHI1, GEN CHI2)
    8842             : {
    8843         259 :   long s = 1, ord, vt;
    8844             :   GEN E0, NK, vchi, T;
    8845         259 :   if (CHI2) { CHI2 = get_mfchar(CHI2); if (mfcharparity(CHI2) < 0) s = -s; }
    8846         259 :   if (CHI1) { CHI1 = get_mfchar(CHI1); if (mfcharparity(CHI1) < 0) s = -s; }
    8847         245 :   if (s != m1pk(k)) return mftrivial();
    8848         224 :   if (!CHI1) CHI1 = mfchartrivial();
    8849         224 :   if (!CHI2)
    8850             :   { /* E_k(chi1) */
    8851         140 :     vt = varn(mfcharpol(CHI1));
    8852         140 :     ord = mfcharorder(CHI1);
    8853         140 :     NK = mkNK(mfcharmodulus(CHI1), k, CHI1);
    8854         140 :     E0 = charLFwtk(k, CHI1, ord);
    8855         140 :     vchi = mkvec3(E0, mkvec(mfcharpol(CHI1)), CHI1);
    8856         140 :     return tag(t_MF_EISEN, NK, vchi);
    8857             :   }
    8858             :   /* E_k(chi1,chi2) */
    8859          84 :   vt = varn(mfcharpol(CHI1));
    8860          84 :   NK = NK_eisen2(k, CHI1, CHI2);
    8861          84 :   ord = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    8862          84 :   E0 = mfeisenstein2_0(k, CHI1, CHI2, ord);
    8863          84 :   T = mkvec(polcyclo(ord_canon(ord), vt));
    8864          84 :   vchi = mkvec4(E0, T, CHI1, CHI2);
    8865          84 :   return tag2(t_MF_EISEN, NK, vchi, mkvecsmall2(ord,0));
    8866             : }
    8867             : GEN
    8868         259 : mfeisenstein(long k, GEN CHI1, GEN CHI2)
    8869             : {
    8870         259 :   pari_sp av = avma;
    8871         259 :   if (k < 1) pari_err_DOMAIN("mfeisenstein", "k", "<", gen_1, stoi(k));
    8872         259 :   return gerepilecopy(av, mfeisenstein_i(k, CHI1, CHI2));
    8873             : }
    8874             : 
    8875             : static GEN
    8876        1218 : mfeisenstein2all(long N0, GEN NK, long k, GEN CHI1, GEN CHI2, GEN T, long o)
    8877             : {
    8878        1218 :   GEN E, E0 = mfeisenstein2_0(k, CHI1,CHI2, o), vchi = mkvec4(E0, T, CHI1,CHI2);
    8879        1218 :   long j, d = (lg(T)==4)? itou(gmael(T,3,1)): 1;
    8880        1218 :   E = cgetg(d+1, t_VEC);
    8881        1218 :   for (j=1; j<=d; j++) gel(E,j) = tag2(t_MF_EISEN, NK,vchi,mkvecsmall2(o,j-1));
    8882        1218 :   return mfbdall(E, N0 / mf_get_N(gel(E,1)));
    8883             : }
    8884             : 
    8885             : static GEN
    8886         532 : zncharsG(GEN G)
    8887             : {
    8888         532 :   long i, l, N = itou(znstar_get_N(G));
    8889             :   GEN vCHI, V;
    8890         532 :   if (N == 1) return mkvec2(gen_1,cgetg(1,t_COL));
    8891         532 :   vCHI = const_vec(N,NULL);
    8892         532 :   V = cyc2elts(znstar_get_conreycyc(G));
    8893         532 :   l = lg(V);
    8894       15848 :   for (i = 1; i < l; i++)
    8895             :   {
    8896       15316 :     GEN chi0, chi = zc_to_ZC(gel(V,i)), n, F;
    8897       15316 :     F = znconreyconductor(G, chi, &chi0);
    8898       15316 :     if (typ(F) != t_INT) F = gel(F,1);
    8899       15316 :     n = znconreyexp(G, chi);
    8900       15316 :     gel(vCHI, itos(n)) = mkvec2(F, chi0);
    8901             :   }
    8902         532 :   return vCHI;
    8903             : }
    8904             : 
    8905             : /* CHI primitive, f(CHI) | N. Return pairs (CHI1,CHI2) both primitive
    8906             :  * such that f(CHI1)*f(CHI2) | N and CHI1 * CHI2 = CHI;
    8907             :  * if k = 1, CHI1 is even; if k = 2, omit (1,1) if CHI = 1 */
    8908             : static GEN
    8909         728 : mfeisensteinbasis_i(long N0, long k, GEN CHI)
    8910             : {
    8911         728 :   GEN G = gel(CHI,1), chi = gel(CHI,2), vT = const_vec(myeulerphiu(N0), NULL);
    8912             :   GEN CHI0, GN, chiN, Lchi, LG, V, RES, NK, T;
    8913         728 :   long i, j, l, n, n1, N, ord = mfcharorder(CHI), OC = ord_canon(ord);
    8914         728 :   long F = mfcharmodulus(CHI), vt = varn(mfcharpol(CHI));
    8915             : 
    8916         728 :   CHI0 = (F == 1)? CHI: mfchartrivial();
    8917         728 :   j = 1; RES = cgetg(N0+1, t_VEC);
    8918         728 :   T = gel(vT,OC) = Qab_trace_init(polcyclo(OC,vt), OC, OC);
    8919         728 :   if (F != 1 || k != 2)
    8920             :   { /* N1 = 1 */
    8921         602 :     NK = mkNK(F, k, CHI);
    8922         602 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI0, CHI, T, ord);
    8923         602 :     if (F != 1 && k != 1)
    8924         203 :       gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI, CHI0, T, ord);
    8925             :   }
    8926         728 :   if (N0 == 1) { setlg(RES,j); return RES; }
    8927         658 :   GN = G; chiN = chi;
    8928         658 :   if (F == N0) N = N0;
    8929             :   else
    8930             :   {
    8931         413 :     GEN faN = myfactoru(N0), P = gel(faN,1), E = gel(faN,2);
    8932         413 :     long lP = lg(P);
    8933        1050 :     for (i = N = 1; i < lP; i++)
    8934             :     {
    8935         637 :       long p = P[i];
    8936         637 :       N *= upowuu(p, maxuu(E[i]/2, z_lval(F,p)));
    8937             :     }
    8938         413 :     if ((N & 3) == 2) N >>= 1;
    8939         413 :     if (N == 1) { setlg(RES,j); return RES; }
    8940         287 :     if (F != N)
    8941             :     {
    8942          91 :       GN = znstar0(utoipos(N),1);
    8943          91 :       chiN = zncharinduce(G, chi, GN);
    8944             :     }
    8945             :   }
    8946         532 :   LG = const_vec(N, NULL); /* LG[d] = znstar(d,1) or NULL */
    8947         532 :   gel(LG,1) = gel(CHI0,1);
    8948         532 :   gel(LG,F) = G;
    8949         532 :   gel(LG,N) = GN;
    8950         532 :   Lchi = coprimes_zv(N);
    8951         532 :   n = itou(znconreyexp(GN,chiN));
    8952         532 :   V = zncharsG(GN); l = lg(V);
    8953       20342 :   for (n1 = 2; n1 < l; n1++) /* skip 1 (trivial char) */
    8954             :   {
    8955       19810 :     GEN v = gel(V,n1), w, chi1, chi2, G1, G2, CHI1, CHI2;
    8956             :     long N12, N1, N2, no, oc, o12, t, m;
    8957       19810 :     if (!Lchi[n1]) continue;
    8958       14735 :     chi1 = gel(v,2); N1 = itou(gel(v,1)); /* conductor of chi1 */
    8959       14735 :     w = gel(V, Fl_div(n,n1,N));
    8960       14735 :     chi2 = gel(w,2); N2 = itou(gel(w,1)); /* conductor of chi2 */
    8961       14735 :     N12 = N1 * N2;
    8962       14735 :     if (N2 == 1 || N0 % N12) continue;
    8963             : 
    8964         546 :     G1 = gel(LG,N1); if (!G1) gel(LG,N1) = G1 = znstar0(utoipos(N1), 1);
    8965         546 :     if (k == 1 && zncharisodd(G1,chi1)) continue;
    8966         413 :     G2 = gel(LG,N2); if (!G2) gel(LG,N2) = G2 = znstar0(utoipos(N2), 1);
    8967         413 :     CHI1 = mfcharGL(G1, chi1);
    8968         413 :     CHI2 = mfcharGL(G2, chi2);
    8969         413 :     o12 = ulcm(mfcharorder(CHI1), mfcharorder(CHI2));
    8970             :     /* remove Galois orbit: same trace */
    8971         413 :     no = Fl_powu(n1, ord, N);
    8972         707 :     for (t = 1+ord, m = n1; t <= o12; t += ord)
    8973             :     { /* m <-> CHI1^t, if t in Gal(Q(chi1,chi2)/Q), omit (CHI1^t,CHI2^t) */
    8974         294 :       m = Fl_mul(m, no, N); if (!m) break;
    8975         294 :       if (ugcd(t, o12) == 1) Lchi[m] = 0;
    8976             :     }
    8977         413 :     oc = ord_canon(o12); T = gel(vT,oc);
    8978         413 :     if (!T) T = gel(vT,oc) = Qab_trace_init(polcyclo(oc,vt), oc, OC);
    8979         413 :     NK = mkNK(N12, k, CHI);
    8980         413 :     gel(RES, j++) = mfeisenstein2all(N0, NK, k, CHI1, CHI2, T, o12);
    8981             :   }
    8982         532 :   setlg(RES,j); return RES;
    8983             : }
    8984             : 
    8985             : static GEN
    8986         616 : mfbd_E2(GEN E2, long d, GEN CHI)
    8987             : {
    8988         616 :   GEN E2d = mfbd_i(E2, d);
    8989         616 :   GEN F = mkvec2(E2, E2d), L = mkvec2(gen_1, utoineg(d));
    8990             :   /* cannot use mflinear_i: E2 and E2d do not have the same level */
    8991         616 :   return tag3(t_MF_LINEAR, mkNK(d,2,CHI), F, L, gen_1);
    8992             : }
    8993             : /* C-basis of E_k(Gamma_0(N),chi). If k = 1, the first basis element must not
    8994             :  * vanish at oo [used in mfwt1basis]. Here E_1(CHI), whose q^0 coefficient
    8995             :  * does not vanish (since L(CHI,0) does not) *if* CHI is not trivial; which
    8996             :  * must be the case in weight 1.
    8997             :  *
    8998             :  * (k>=3): In weight k >= 3, basis is B(d) E(CHI1,(CHI/CHI1)_prim), where
    8999             :  * CHI1 is primitive modulo N1, and if N2 is the conductor of CHI/CHI1
    9000             :  * then d*N1*N2 | N.
    9001             :  * (k=2): In weight k=2, same if CHI is nontrivial. If CHI is trivial, must
    9002             :  * not take CHI1 trivial, and must add E_2(tau)-dE_2(d tau)), where
    9003             :  * d|N, d > 1.
    9004             :  * (k=1): In weight k=1, same as k >= 3 except that we restrict to CHI1 even */
    9005             : static GEN
    9006         756 : mfeisensteinbasis(long N, long k, GEN CHI)
    9007             : {
    9008             :   long i, F;
    9009             :   GEN L;
    9010         756 :   if (badchar(N, k, CHI)) return cgetg(1, t_VEC);
    9011         756 :   if (k == 0) return mfcharistrivial(CHI)? mkvec(mf1()): cgetg(1, t_VEC);
    9012         728 :   CHI = mfchartoprimitive(CHI, &F);
    9013         728 :   L = mfeisensteinbasis_i(N, k, CHI);
    9014         728 :   if (F == 1 && k == 2)
    9015             :   {
    9016         126 :     GEN v, E2 = mfeisenstein(2, NULL, NULL), D = mydivisorsu(N);
    9017         126 :     long nD = lg(D)-1;
    9018         126 :     v = cgetg(nD, t_VEC); L = vec_append(L,v);
    9019         126 :     for (i = 1; i < nD; i++) gel(v,i) = mfbd_E2(E2, D[i+1], CHI);
    9020             :   }
    9021         728 :   return lg(L) == 1? L: shallowconcat1(L);
    9022             : }
    9023             : 
    9024             : static GEN
    9025          70 : not_in_space(GEN F, long flag)
    9026             : {
    9027          70 :   if (!flag) err_space(F);
    9028          63 :   return cgetg(1, t_COL);
    9029             : }
    9030             : /* when flag set, no error */
    9031             : GEN
    9032         805 : mftobasis(GEN mf, GEN F, long flag)
    9033             : {
    9034         805 :   pari_sp av2, av = avma;
    9035             :   GEN G, v, y, gk;
    9036         805 :   long N, B, ismf = checkmf_i(F);
    9037             : 
    9038         805 :   mf = checkMF(mf);
    9039         805 :   if (ismf)
    9040             :   {
    9041         714 :     if (mfistrivial(F)) return zerocol(MF_get_dim(mf));
    9042         707 :     if (!mf_same_k(mf, F) || !mf_same_CHI(mf, F)) return not_in_space(F, flag);
    9043             :   }
    9044         756 :   N = MF_get_N(mf);
    9045         756 :   gk = MF_get_gk(mf);
    9046         756 :   if (ismf)
    9047             :   {
    9048         665 :     long NF = mf_get_N(F);
    9049         665 :     B = maxuu(mfsturmNgk(NF,gk), mfsturmNgk(N,gk)) + 1;
    9050         665 :     v = mfcoefs_i(F,B,1);
    9051             :   }
    9052             :   else
    9053             :   {
    9054          91 :     B = mfsturmNgk(N, gk) + 1;
    9055          91 :     switch(typ(F))
    9056             :     { /* F(0),...,F(lg(v)-2) */
    9057          63 :       case t_SER: v = sertocol(F); settyp(v,t_VEC); break;
    9058          14 :       case t_VEC: v = F; break;
    9059           7 :       case t_COL: v = shallowtrans(F); break;
    9060           7 :       default: pari_err_TYPE("mftobasis",F);
    9061             :                v = NULL;/*LCOV_EXCL_LINE*/
    9062             :     }
    9063          84 :     if (flag) B = minss(B, lg(v)-2);
    9064             :   }
    9065         749 :   y = mftobasis_i(mf, v);
    9066         749 :   if (typ(y) == t_VEC)
    9067             :   {
    9068          21 :     if (flag) return gerepilecopy(av, y);
    9069           0 :     pari_err(e_MISC, "not enough coefficients in mftobasis");
    9070             :   }
    9071         728 :   av2 = avma;
    9072         728 :   if (MF_get_space(mf) == mf_FULL || mfsturm(mf)+1 == B) return y;
    9073         210 :   G = mflinear(mf, y);
    9074         210 :   if (!gequal(v, mfcoefs_i(G, lg(v)-2,1))) y = NULL;
    9075         210 :   if (!y) { set_avma(av); return not_in_space(F, flag); }
    9076         182 :   set_avma(av2); return gerepileupto(av, y);
    9077             : }
    9078             : 
    9079             : /* assume N > 0; first cusp is always 0 */
    9080             : static GEN
    9081          49 : mfcusps_i(long N)
    9082             : {
    9083             :   long i, c, l;
    9084             :   GEN D, v;
    9085             : 
    9086          49 :   if (N == 1) return mkvec(gen_0);
    9087          49 :   D = mydivisorsu(N); l = lg(D); /* left on stack */
    9088          49 :   c = mfnumcuspsu_fact(myfactoru(N));
    9089          49 :   v = cgetg(c + 1, t_VEC);
    9090         350 :   for (i = c = 1; i < l; i++)
    9091             :   {
    9092         301 :     long C = D[i], NC = D[l-i], lima = ugcd(C, NC), A0, A;
    9093         889 :     for (A0 = 0; A0 < lima; A0++)
    9094         588 :       if (ugcd(A0, lima) == 1)
    9095             :       {
    9096         392 :         A = A0; while (ugcd(A,C) > 1) A += lima;
    9097         392 :         gel(v, c++) = sstoQ(A, C);
    9098             :       }
    9099             :   }
    9100          49 :   return v;
    9101             : }
    9102             : /* List of cusps of Gamma_0(N) */
    9103             : GEN
    9104          28 : mfcusps(GEN gN)
    9105             : {
    9106             :   long N;
    9107             :   GEN mf;
    9108          28 :   if (typ(gN) == t_INT) N = itos(gN);
    9109          14 :   else if ((mf = checkMF_i(gN))) N = MF_get_N(mf);
    9110           0 :   else { pari_err_TYPE("mfcusps", gN); N = 0; }
    9111          28 :   if (N <= 0) pari_err_DOMAIN("mfcusps", "N", "<=", gen_0, stoi(N));
    9112          28 :   return mfcusps_i(N);
    9113             : }
    9114             : 
    9115             : long
    9116         315 : mfcuspisregular(GEN NK, GEN cusp)
    9117             : {
    9118             :   long v, N, dk, nk, t, o;
    9119             :   GEN mf, CHI, go, A, C, g, c, d;
    9120         315 :   if ((mf = checkMF_i(NK)))
    9121             :   {
    9122          49 :     GEN gk = MF_get_gk(mf);
    9123          49 :     N = MF_get_N(mf);
    9124          49 :     CHI = MF_get_CHI(mf);
    9125          49 :     Qtoss(gk, &nk, &dk);
    9126             :   }
    9127             :   else
    9128         266 :     checkNK2(NK, &N, &nk, &dk, &CHI, 0);
    9129         315 :   if (typ(cusp) == t_INFINITY) return 1;
    9130         315 :   if (typ(cusp) == t_FRAC) { A = gel(cusp,1); C = gel(cusp,2); }
    9131          28 :   else { A = cusp; C = gen_1; }
    9132         315 :   g = diviuexact(mului(N,C), ugcd(N, Fl_sqr(umodiu(C,N), N)));
    9133         315 :   c = mulii(negi(C),g);
    9134         315 :   d = addiu(mulii(A,g), 1);
    9135         315 :   if (!CHI) return 1;
    9136         315 :   go = gmfcharorder(CHI);
    9137         315 :   v = vali(go); if (v < 2) go = shifti(go, 2-v);
    9138         315 :   t = itou( znchareval(gel(CHI,1), gel(CHI,2), d, go) );
    9139         315 :   if (dk == 1) return t == 0;
    9140         154 :   o = itou(go);
    9141         154 :   if (kronecker(c,d) < 0) t = Fl_add(t, o/2, o);
    9142         154 :   if (Mod4(d) == 1) return t == 0;
    9143          14 :   t = Fl_sub(t, Fl_mul(o/4, nk, o), o);
    9144          14 :   return t == 0;
    9145             : }
    9146             : 
    9147             : /* Some useful closures */
    9148             : 
    9149             : /* sum_{d|n} d^k */
    9150             : static GEN
    9151       16464 : mysumdivku(ulong n, ulong k)
    9152             : {
    9153       16464 :   GEN fa = myfactoru(n);
    9154       16464 :   return k == 1? usumdiv_fact(fa): usumdivk_fact(fa,k);
    9155             : }
    9156             : static GEN
    9157         658 : c_Ek(long n, long d, GEN F)
    9158             : {
    9159         658 :   GEN E = cgetg(n + 2, t_VEC), C = gel(F,2);
    9160         658 :   long i, k = mf_get_k(F);
    9161         658 :   gel (E, 1) = gen_1;
    9162        8260 :   for (i = 1; i <= n; i++)
    9163             :   {
    9164        7602 :     pari_sp av = avma;
    9165        7602 :     gel(E, i+1) = gerepileupto(av, gmul(C, mysumdivku(i*d, k-1)));
    9166             :   }
    9167         658 :   return E;
    9168             : }
    9169             : 
    9170             : GEN
    9171         189 : mfEk(long k)
    9172             : {
    9173         189 :   pari_sp av = avma;
    9174             :   GEN E0, NK;
    9175         189 :   if (k < 0 || odd(k)) pari_err_TYPE("mfEk [incorrect k]", stoi(k));
    9176         189 :   if (!k) return mf1();
    9177         182 :   E0 = gdivsg(-2*k, bernfrac(k));
    9178         182 :   NK = mkNK(1,k,mfchartrivial());
    9179         182 :   return gerepilecopy(av, tag(t_MF_Ek, NK, E0));
    9180             : }
    9181             : 
    9182             : GEN
    9183          49 : mfDelta(void)
    9184             : {
    9185          49 :   pari_sp av = avma;
    9186          49 :   return gerepilecopy(av, tag0(t_MF_DELTA, mkNK(1,12,mfchartrivial())));
    9187             : }
    9188             : 
    9189             : GEN
    9190         504 : mfTheta(GEN psi)
    9191             : {
    9192         504 :   pari_sp av = avma;
    9193             :   GEN N, gk, psi2;
    9194             :   long par;
    9195         504 :   if (!psi) { psi = mfchartrivial(); N = utoipos(4); par = 1; }
    9196             :   else
    9197             :   {
    9198             :     long FC;
    9199          21 :     psi = get_mfchar(psi);
    9200          21 :     FC = mfcharconductor(psi);
    9201          21 :     if (mfcharmodulus(psi) != FC)
    9202           0 :       pari_err_TYPE("mfTheta [nonprimitive character]", psi);
    9203          21 :     par = mfcharparity(psi);
    9204          21 :     N = shifti(sqru(FC),2);
    9205             :   }
    9206         504 :   if (par > 0) { gk = ghalf; psi2 = psi; }
    9207           7 :   else { gk = gsubsg(2, ghalf); psi2 = mfcharmul(psi, get_mfchar(stoi(-4))); }
    9208         504 :   return gerepilecopy(av, tag(t_MF_THETA, mkgNK(N, gk, psi2, pol_x(1)), psi));
    9209             : }
    9210             : 
    9211             : /* Output 0 if not desired eta product: if flag=0 (default) require
    9212             :  * holomorphic at cusps. If flag set, accept meromorphic, but sill in some
    9213             :  * modular function space */
    9214             : GEN
    9215         140 : mffrometaquo(GEN eta, long flag)
    9216             : {
    9217         140 :   pari_sp av = avma;
    9218             :   GEN NK, N, k, BR, P;
    9219         140 :   long v, cusp = 0;
    9220         140 :   if (!etaquotype(&eta, &N,&k,&P, &v, NULL, flag? NULL: &cusp) || cusp < 0)
    9221             :   {
    9222          14 :     set_avma(av); return gen_0;
    9223             :   }
    9224         126 :   if (lg(gel(eta,1)) == 1) { set_avma(av); return mf1(); }
    9225         119 :   BR = mkvec2(ZV_to_zv(gel(eta,1)), ZV_to_zv(gel(eta,2)));
    9226         119 :   if (v < 0) v = 0;
    9227         119 :   NK = mkgNK(N, k, get_mfchar(P), pol_x(1));
    9228         119 :   return gerepilecopy(av, tag2(t_MF_ETAQUO, NK, BR, utoi(v)));
    9229             : }
    9230             : 
    9231             : #if 0
    9232             : /* number of primitive characters modulo N */
    9233             : static ulong
    9234             : numprimchars(ulong N)
    9235             : {
    9236             :   GEN fa, P, E;
    9237             :   long i, l;
    9238             :   ulong n;
    9239             :   if ((N & 3) == 2) return 0;
    9240             :   fa = myfactoru(N);
    9241             :   P = gel(fa,1); l = lg(P);
    9242             :   E = gel(fa,2);
    9243             :   for (i = n = 1; i < l; i++)
    9244             :   {
    9245             :     ulong p = P[i], e = E[i];
    9246             :     if (e == 2) n *= p-2; else n *= (p-1)*(p-1)*upowuu(p,e-2);
    9247             :   }
    9248             :   return n;
    9249             : }
    9250             : #endif
    9251             : 
    9252             : /* Space generated by products of two Eisenstein series */
    9253             : 
    9254             : INLINE int
    9255      112273 : cmp_small(long a, long b) { return a>b? 1: (a<b? -1: 0); }
    9256             : static int
    9257       62657 : cmp_small_priority(void *E, GEN a, GEN b)
    9258             : {
    9259       62657 :   GEN prio = (GEN)E;
    9260       62657 :   return cmp_small(prio[(long)a], prio[(long)b]);
    9261             : }
    9262             : static long
    9263         938 : znstar_get_expo(GEN G)
    9264             : {
    9265         938 :   GEN cyc = znstar_get_cyc(G);
    9266         938 :   return (lg(cyc) == 1)? 1: itou(gel(cyc,1));
    9267             : }
    9268             : 
    9269             : /* Return [vchi, bymod, vG]:
    9270             :  * vG[f] = znstar(f,1) for f a conductor of (at least) a char mod N; else NULL
    9271             :  * bymod[f] = vecsmall of conrey indexes of chars modulo f | N; else NULL
    9272             :  * vchi[n] = a list of CHIvec [G0,chi0,o,ncharvecexpo(G0,nchi0),...]:
    9273             :  *   chi0 = primitive char attached to Conrey Mod(n,N)
    9274             :  * (resp. NULL if (n,N) > 1) */
    9275             : static GEN
    9276         469 : charsmodN(long N)
    9277             : {
    9278         469 :   GEN D, G, prio, phio, dummy = cgetg(1,t_VEC);
    9279         469 :   GEN vP, vG = const_vec(N,NULL), vCHI  = const_vec(N,NULL);
    9280         469 :   GEN bymod = const_vec(N,NULL);
    9281         469 :   long pn, i, l, vt = fetch_user_var("t");
    9282         469 :   D = mydivisorsu(N); l = lg(D);
    9283        3059 :   for (i = 1; i < l; i++)
    9284        2590 :     gel(bymod, D[i]) = vecsmalltrunc_init(myeulerphiu(D[i])+1);
    9285         469 :   gel(vG,N) = G = znstar0(utoipos(N),1);
    9286         469 :   pn = znstar_get_expo(G);  /* exponent(Z/NZ)^* */
    9287         469 :   vP = const_vec(pn,NULL);
    9288       22456 :   for (i = 1; i <= N; i++)
    9289             :   {
    9290             :     GEN P, gF, G0, chi0, nchi0, chi, v, go;
    9291             :     long j, F, o;
    9292       21987 :     if (ugcd(i,N) != 1) continue;
    9293       11067 :     chi = znconreylog(G, utoipos(i));
    9294       11067 :     gF = znconreyconductor(G, chi, &chi0);
    9295       11067 :     F = (typ(gF) == t_INT)? itou(gF): itou(gel(gF,1));
    9296       11067 :     G0 = gel(vG, F); if (!G0) G0 = gel(vG,F) = znstar0(gF, 1);
    9297       11067 :     nchi0 = znconreylog_normalize(G0,chi0);
    9298       11067 :     go = gel(nchi0,1); o = itou(go); /* order(chi0) */
    9299       11067 :     v = ncharvecexpo(G0, nchi0);
    9300       11067 :     if (!equaliu(go, pn)) v = zv_z_mul(v, pn / o);
    9301       11067 :     P = gel(vP, o); if (!P) P = gel(vP,o) = polcyclo(o,vt);
    9302             :     /* mfcharcxinit with dummy complex powers */
    9303       11067 :     gel(vCHI,i) = mkvecn(6, G0, chi0, go, v, dummy, P);
    9304       11067 :     D = mydivisorsu(N / F); l = lg(D);
    9305       11067 :     for (j = 1; j < l; j++) vecsmalltrunc_append(gel(bymod, F*D[j]), i);
    9306             :   }
    9307         469 :   phio = zero_zv(pn); l = lg(vCHI); prio = cgetg(l, t_VEC);
    9308       22456 :   for (i = 1; i < l; i++)
    9309             :   {
    9310       21987 :     GEN CHI = gel(vCHI,i);
    9311             :     long o;
    9312       21987 :     if (!CHI) continue;
    9313       11067 :     o = CHIvec_ord(CHI);
    9314       11067 :     if (!phio[o]) phio[o] = myeulerphiu(o);
    9315       11067 :     prio[i] = phio[o];
    9316             :   }
    9317         469 :   l = lg(bymod);
    9318             :   /* sort characters by increasing value of phi(order) */
    9319       22456 :   for (i = 1; i < l; i++)
    9320             :   {
    9321       21987 :     GEN z = gel(bymod,i);
    9322       21987 :     if (z) gen_sort_inplace(z, (void*)prio, &cmp_small_priority, NULL);
    9323             :   }
    9324         469 :   return mkvec3(vCHI, bymod, vG);
    9325             : }
    9326             : 
    9327             : static GEN
    9328        4319 : mfeisenstein2pure(long k, GEN CHI1, GEN CHI2, long ord, GEN P, long lim)
    9329             : {
    9330        4319 :   GEN c, V = cgetg(lim+2, t_COL);
    9331             :   long n;
    9332        4319 :   c = mfeisenstein2_0(k, CHI1, CHI2, ord);
    9333        4319 :   if (P) c = grem(c, P);
    9334        4319 :   gel(V,1) = c;
    9335       92512 :   for (n=1; n <= lim; n++)
    9336             :   {
    9337       88193 :     c = sigchi2(k, CHI1, CHI2, n, ord);
    9338       88193 :     if (P) c = grem(c, P);
    9339       88193 :     gel(V,n+1) = c;
    9340             :   }
    9341        4319 :   return V;
    9342             : }
    9343             : static GEN
    9344        3290 : mfeisenstein2pure_Fl(long k, GEN CHI1vec, GEN CHI2vec, GEN vz, ulong p, long lim)
    9345             : {
    9346        3290 :   GEN V = cgetg(lim+2, t_VECSMALL);
    9347             :   long n;
    9348        3290 :   V[1] = mfeisenstein2_0_Fl(k, CHI1vec, CHI2vec, vz, p);
    9349        3290 :   for (n=1; n <= lim; n++) V[n+1] = sigchi2_Fl(k, CHI1vec, CHI2vec, n, vz, p);
    9350        3290 :   return V;
    9351             : }
    9352             : 
    9353             : static GEN
    9354         175 : getcolswt2(GEN M, GEN D, ulong p)
    9355             : {
    9356         175 :   GEN R, v = gel(M,1);
    9357         175 :   long i, l = lg(M) - 1;
    9358         175 :   R = cgetg(l, t_MAT); /* skip D[1] = 1 */
    9359         616 :   for (i = 1; i < l; i++)
    9360             :   {
    9361         441 :     GEN w = Flv_Fl_mul(gel(M,i+1), D[i+1], p);
    9362         441 :     gel(R,i) = Flv_sub(v, w, p);
    9363             :   }
    9364         175 :   return R;
    9365             : }
    9366             : static GEN
    9367        4319 : expandbd(GEN V, long d)
    9368             : {
    9369             :   long L, n, nd;
    9370             :   GEN W;
    9371        4319 :   if (d == 1) return V;
    9372        1575 :   L = lg(V)-1; W = zerocol(L); /* nd = n/d */
    9373        1575 :   for (n = nd = 0; n < L; n += d, nd++) gel(W, n+1) = gel(V, nd+1);
    9374        1575 :   return W;
    9375             : }
    9376             : static GEN
    9377        5222 : expandbd_Fl(GEN V, long d)
    9378             : {
    9379             :   long L, n, nd;
    9380             :   GEN W;
    9381        5222 :   if (d == 1) return V;
    9382        1932 :   L = lg(V)-1; W = zero_Flv(L); /* nd = n/d */
    9383        1932 :   for (n = nd = 0; n < L; n += d, nd++) W[n+1] = V[nd+1];
    9384        1932 :   return W;
    9385             : }
    9386             : static void
    9387        3290 : getcols_i(GEN *pM, GEN *pvj, GEN gk, GEN CHI1vec, GEN CHI2vec, long NN1, GEN vz,
    9388             :           ulong p, long lim)
    9389             : {
    9390        3290 :   GEN CHI1 = CHIvec_CHI(CHI1vec), CHI2 = CHIvec_CHI(CHI2vec);
    9391        3290 :   long N2 = CHIvec_N(CHI2vec);
    9392        3290 :   GEN vj, M, D = mydivisorsu(NN1/N2);
    9393        3290 :   long i, l = lg(D), k = gk[2];
    9394        3290 :   GEN V = mfeisenstein2pure_Fl(k, CHI1vec, CHI2vec, vz, p, lim);
    9395        3290 :   M = cgetg(l, t_MAT);
    9396        3290 :   for (i = 1; i < l; i++) gel(M,i) = expandbd_Fl(V, D[i]);
    9397        3290 :   if (k == 2 && N2 == 1 && CHIvec_N(CHI1vec) == 1)
    9398             :   {
    9399         175 :     M = getcolswt2(M, D, p); l--;
    9400         175 :     D = vecslice(D, 2, l);
    9401             :   }
    9402        3290 :   *pM = M;
    9403        3290 :   *pvj = vj = cgetg(l, t_VEC);
    9404        3290 :   for (i = 1; i < l; i++) gel(vj,i) = mkvec4(gk, CHI1, CHI2, utoipos(D[i]));
    9405        3290 : }
    9406             : 
    9407             : /* find all CHI1, CHI2 mod N such that CHI1*CHI2 = CHI, f(CHI1)*f(CHI2) | N.
    9408             :  * set M = mfcoefs(B_e E(CHI1,CHI2), lim), vj = [e,i1,i2] */
    9409             : static void
    9410        1267 : getcols(GEN *pM, GEN *pv, long k, long nCHI, GEN allN, GEN vz, ulong p,
    9411             :         long lim)
    9412             : {
    9413        1267 :   GEN vCHI = gel(allN,1), gk = utoi(k);
    9414        1267 :   GEN M = cgetg(1,t_MAT), v = cgetg(1,t_VEC);
    9415        1267 :   long i1, N = lg(vCHI)-1;
    9416       63322 :   for (i1 = 1; i1 <= N; i1++)
    9417             :   {
    9418       62055 :     GEN CHI1vec = gel(vCHI, i1), CHI2vec, M1, v1;
    9419             :     long NN1, i2;
    9420      121618 :     if (!CHI1vec) continue;
    9421       46718 :     if (k == 1 && CHIvec_parity(CHI1vec) == -1) continue;
    9422       29582 :     NN1 = N/CHIvec_N(CHI1vec); /* N/f(chi1) */;
    9423       29582 :     i2 = Fl_div(nCHI,i1, N);
    9424       29582 :     if (!i2) i2 = 1;
    9425       29582 :     CHI2vec = gel(vCHI,i2);
    9426       29582 :     if (NN1 % CHIvec_N(CHI2vec)) continue; /* f(chi1)f(chi2) | N ? */
    9427        2492 :     getcols_i(&M1, &v1, gk, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9428        2492 :     M = shallowconcat(M, M1);
    9429        2492 :     v = shallowconcat(v, v1);
    9430             :   }
    9431        1267 :   *pM = M;
    9432        1267 :   *pv = v;
    9433        1267 : }
    9434             : 
    9435             : static void
    9436         833 : update_Mj(GEN *M, GEN *vecj, GEN *pz, ulong p)
    9437             : {
    9438             :   GEN perm;
    9439         833 :   *pz = Flm_indexrank(*M, p); perm = gel(*pz,2);
    9440         833 :   *M = vecpermute(*M, perm);
    9441         833 :   *vecj = vecpermute(*vecj, perm);
    9442         833 : }
    9443             : static int
    9444         301 : getcolsgen(long dim, GEN *pM, GEN *pvj, GEN *pz, long k, long ell, long nCHI,
    9445             :            GEN allN, GEN vz, ulong p, long lim)
    9446             : {
    9447         301 :   GEN vCHI = gel(allN,1), bymod = gel(allN,2), gell = utoi(ell);
    9448         301 :   long i1, N = lg(vCHI)-1;
    9449         301 :   long L = lim+1;
    9450         301 :   if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9451         301 :   if (lg(*pvj)-1 == dim) return 1;
    9452        1099 :   for (i1 = 1; i1 <= N; i1++)
    9453             :   {
    9454        1085 :     GEN CHI1vec = gel(vCHI, i1), T;
    9455             :     long par1, j, l, N1, NN1;
    9456             : 
    9457        1085 :     if (!CHI1vec) continue;
    9458        1071 :     par1 = CHIvec_parity(CHI1vec);
    9459        1071 :     if (ell == 1 && par1 == -1) continue;
    9460         672 :     if (odd(ell)) par1 = -par1;
    9461         672 :     N1 = CHIvec_N(CHI1vec);
    9462         672 :     NN1 = N/N1;
    9463         672 :     T = gel(bymod, NN1); l = lg(T);
    9464        2394 :     for (j = 1; j < l; j++)
    9465             :     {
    9466        1995 :       long i2 = T[j], l1, l2, j1, s, nC;
    9467        1995 :       GEN M, M1, M2, vj, vj1, vj2, CHI2vec = gel(vCHI, i2);
    9468        3192 :       if (CHIvec_parity(CHI2vec) != par1) continue;
    9469         798 :       nC = Fl_div(nCHI, Fl_mul(i1,i2,N), N);
    9470         798 :       getcols(&M2, &vj2, k-ell, nC, allN, vz, p, lim);
    9471         798 :       l2 = lg(M2); if (l2 == 1) continue;
    9472         798 :       getcols_i(&M1, &vj1, gell, CHI1vec, CHI2vec, NN1, vz, p, lim);
    9473         798 :       l1 = lg(M1);
    9474         798 :       M1 = Flm_to_FlxV(M1, 0);
    9475         798 :       M2 = Flm_to_FlxV(M2, 0);
    9476         798 :       M  = cgetg((l1-1)*(l2-1) + 1, t_MAT);
    9477         798 :       vj = cgetg((l1-1)*(l2-1) + 1, t_VEC);
    9478        1995 :       for (j1 = s = 1; j1 < l1; j1++)
    9479             :       {
    9480        1197 :         GEN E = gel(M1,j1), v = gel(vj1,j1);
    9481             :         long j2;
    9482        5166 :         for (j2 = 1; j2 < l2; j2++, s++)
    9483             :         {
    9484        3969 :           GEN c = Flx_to_Flv(Flxn_mul(E, gel(M2,j2), L, p), L);
    9485        3969 :           gel(M,s) = c;
    9486        3969 :           gel(vj,s) = mkvec2(v, gel(vj2,j2));
    9487             :         }
    9488             :       }
    9489         798 :       *pM = shallowconcat(*pM, M);
    9490         798 :       *pvj = shallowconcat(*pvj, vj);
    9491         798 :       if (lg(*pvj)-1 >= dim) update_Mj(pM, pvj, pz, p);
    9492         798 :       if (lg(*pvj)-1 == dim) return 1;
    9493             :     }
    9494             :   }
    9495          14 :   if (ell == 1)
    9496             :   {
    9497          14 :     update_Mj(pM, pvj, pz, p);
    9498          14 :     return (lg(*pvj)-1 == dim);
    9499             :   }
    9500           0 :   return 0;
    9501             : }
    9502             : 
    9503             : static GEN
    9504         931 : mkF2bd(long d, long lim)
    9505             : {
    9506         931 :   GEN V = zerovec(lim + 1);
    9507             :   long n;
    9508         931 :   gel(V, 1) = ginv(stoi(-24));
    9509         931 :   for (n = 1; n <= lim/d; n++) gel(V, n*d + 1) = mysumdivku(n, 1);
    9510         931 :   return V;
    9511             : }
    9512             : 
    9513             : static GEN
    9514        4676 : mkeisen(GEN E, long ord, GEN P, long lim)
    9515             : {
    9516        4676 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9517        4676 :   GEN CHI1 = gel(E,2), CHI2 = gel(E,3);
    9518        4676 :   if (k == 2 && mfcharistrivial(CHI1) && mfcharistrivial(CHI2))
    9519         357 :     return gsub(mkF2bd(1,lim), gmulgs(mkF2bd(e,lim), e));
    9520             :   else
    9521             :   {
    9522        4319 :     GEN V = mfeisenstein2pure(k, CHI1, CHI2, ord, P, lim);
    9523        4319 :     return expandbd(V, e);
    9524             :   }
    9525             : }
    9526             : static GEN
    9527         441 : mkM(GEN vj, long pn, GEN P, long lim)
    9528             : {
    9529         441 :   long j, l = lg(vj), L = lim+1;
    9530         441 :   GEN M = cgetg(l, t_MAT);
    9531        3836 :   for (j = 1; j < l; j++)
    9532             :   {
    9533             :     GEN E1, E2;
    9534        3395 :     parse_vecj(gel(vj,j), &E1,&E2);
    9535        3395 :     E1 = RgV_to_RgX(mkeisen(E1, pn, P, lim), 0);
    9536        3395 :     if (E2)
    9537             :     {
    9538        1281 :       E2 = RgV_to_RgX(mkeisen(E2, pn, P, lim), 0);
    9539        1281 :       E1 = RgXn_mul(E1, E2, L);
    9540             :     }
    9541        3395 :     E1 = RgX_to_RgC(E1, L);
    9542        3395 :     if (P && E2) E1 = RgXQV_red(E1, P);
    9543        3395 :     gel(M,j) = E1;
    9544             :   }
    9545         441 :   return M;
    9546             : }
    9547             : 
    9548             : /* assume N > 2 */
    9549             : static GEN
    9550          21 : mffindeisen1(long N)
    9551             : {
    9552          21 :   GEN G = znstar0(utoipos(N), 1), L = chargalois(G, NULL), chi0 = NULL;
    9553          21 :   long j, m = N, l = lg(L);
    9554         154 :   for (j = 1; j < l; j++)
    9555             :   {
    9556         147 :     GEN chi = gel(L,j);
    9557         147 :     long r = myeulerphiu(itou(zncharorder(G,chi)));
    9558         147 :     if (r >= m) continue;
    9559         105 :     chi = znconreyfromchar(G, chi);
    9560         105 :     if (zncharisodd(G,chi)) { m = r; chi0 = chi; if (r == 1) break; }
    9561             :   }
    9562          21 :   if (!chi0) pari_err_BUG("mffindeisen1 [no Eisenstein series found]");
    9563          21 :   chi0 = znchartoprimitive(G,chi0);
    9564          21 :   return mfcharGL(gel(chi0,1), gel(chi0,2));
    9565             : }
    9566             : 
    9567             : static GEN
    9568         469 : mfeisensteinspaceinit_i(long N, long k, GEN CHI)
    9569             : {
    9570         469 :   GEN M, Minv, vj, vG, GN, allN, P, vz, z = NULL;
    9571         469 :   long nCHI, lim, ell, ord, pn, dim = mffulldim(N, k, CHI);
    9572             :   ulong r, p;
    9573             : 
    9574         469 :   if (!dim) retmkvec3(cgetg(1,t_VECSMALL),
    9575             :                       mkvec2(cgetg(1,t_MAT),gen_1),cgetg(1,t_VEC));
    9576         469 :   lim = mfsturmNk(N, k) + 1;
    9577         469 :   allN = charsmodN(N);
    9578         469 :   vG = gel(allN,3);
    9579         469 :   GN = gel(vG,N);
    9580         469 :   pn = znstar_get_expo(GN);
    9581         469 :   ord = ord_canon(pn);
    9582         469 :   P = ord == 1? NULL: polcyclo(ord, varn(mfcharpol(CHI)));
    9583         469 :   CHI = induce(GN, CHI); /* lift CHI mod N before mfcharno*/
    9584         469 :   nCHI = mfcharno(CHI);
    9585         469 :   r = QabM_init(ord, &p);
    9586         469 :   vz = Fl_powers(r, pn, p);
    9587         469 :   getcols(&M, &vj, k, nCHI, allN, vz, p, lim);
    9588         483 :   for (ell = k>>1; ell >= 1; ell--)
    9589         301 :     if (getcolsgen(dim, &M, &vj, &z, k, ell, nCHI, allN, vz, p, lim)) break;
    9590         469 :   if (!z) update_Mj(&M, &vj, &z, p);
    9591         469 :   if (lg(vj) - 1 < dim) return NULL;
    9592         441 :   M = mkM(vj, pn, P, lim);
    9593         441 :   Minv = QabM_Minv(rowpermute(M, gel(z,1)), P, ord);
    9594         441 :   return mkvec4(gel(z,1), Minv, vj, utoi(ord));
    9595             : }
    9596             : /* true mf */
    9597             : static GEN
    9598         441 : mfeisensteinspaceinit(GEN mf)
    9599             : {
    9600         441 :   pari_sp av = avma;
    9601         441 :   GEN z, CHI = MF_get_CHI(mf);
    9602         441 :   long N = MF_get_N(mf), k = MF_get_k(mf);
    9603         441 :   if (!CHI) CHI = mfchartrivial();
    9604         441 :   z = mfeisensteinspaceinit_i(N, k, CHI);
    9605         441 :   if (!z)
    9606             :   {
    9607          21 :     GEN E, CHIN = mffindeisen1(N), CHI0 = mfchartrivial();
    9608          21 :     z = mfeisensteinspaceinit_i(N, k+1, mfcharmul(CHI, CHIN));
    9609          21 :     if (z) E = mkvec4(gen_1, CHI0, CHIN, gen_1);
    9610             :     else
    9611             :     {
    9612           7 :       z = mfeisensteinspaceinit_i(N, k+2, CHI);
    9613           7 :       E = mkvec4(gen_2, CHI0, CHI0, utoipos(N));
    9614             :     }
    9615          21 :     z = mkvec2(z, E);
    9616             :   }
    9617         441 :   return gerepilecopy(av, z);
    9618             : }
    9619             : 
    9620             : /* decomposition of modular form on eisenspace */
    9621             : static GEN
    9622         826 : mfeisensteindec(GEN mf, GEN F)
    9623             : {
    9624         826 :   pari_sp av = avma;
    9625             :   GEN M, Mindex, Mvecj, V, B, CHI;
    9626             :   long o, ord;
    9627             : 
    9628         826 :   Mvecj = obj_checkbuild(mf, MF_EISENSPACE, &mfeisensteinspaceinit);
    9629         826 :   if (lg(Mvecj) < 5)
    9630             :   {
    9631          21 :     GEN E, e = gel(Mvecj,2), gkE = gel(e,1);
    9632          21 :     long dE = itou(gel(e,4));
    9633          21 :     Mvecj = gel(Mvecj,1);
    9634          21 :     E = mfeisenstein(itou(gkE), NULL, gel(e,3));
    9635          21 :     if (dE != 1) E = mfbd_E2(E, dE, gel(e,2)); /* here k = 2 */
    9636          21 :     F = mfmul(F, E);
    9637             :   }
    9638         826 :   M = gel(Mvecj, 2);
    9639         826 :   if (lg(M) == 1) return cgetg(1, t_VEC);
    9640         826 :   Mindex = gel(Mvecj, 1);
    9641         826 :   ord = itou(gel(Mvecj,4));
    9642         826 :   V = mfcoefs(F, Mindex[lg(Mindex)-1]-1, 1); settyp(V, t_COL);
    9643         826 :   CHI = mf_get_CHI(F);
    9644         826 :   o = mfcharorder_canon(CHI);
    9645         826 :   if (o > 1 && o != ord)
    9646             :   { /* convert Mod(.,polcyclo(o)) to Mod(., polcyclo(N)) for o | N,
    9647             :      * o and N both != 2 (mod 4) */
    9648          49 :     GEN z, P = gel(M,4); /* polcyclo(ord) */
    9649          49 :     long vt = varn(P);
    9650          49 :     z = gmodulo(pol_xn(ord/o, vt), P);
    9651          49 :     if (ord % o) pari_err_TYPE("mfeisensteindec", V);
    9652          49 :     V = gsubst(liftpol_shallow(V), vt, z);
    9653             :   }
    9654         826 :   B = Minv_RgC_mul(M, vecpermute(V, Mindex));
    9655         826 :   return gerepileupto(av, B);
    9656             : }
    9657             : 
    9658             : /*********************************************************************/
    9659             : /*                        END EISENSPACE                             */
    9660             : /*********************************************************************/
    9661             : 
    9662             : static GEN
    9663          70 : sertocol2(GEN S, long l)
    9664             : {
    9665          70 :   GEN C = cgetg(l + 2, t_COL);
    9666             :   long i;
    9667          70 :   for (i = 0; i <= l; i++) gel(C, i+1) = polcoef_i(S, i, -1);
    9668          70 :   return C;
    9669             : }
    9670             : 
    9671             : /* Compute polynomial P0 such that F=E4^(k/4)P0(E6/E4^(3/2)). */
    9672             : static GEN
    9673          14 : mfcanfindp0(GEN F, long k)
    9674             : {
    9675          14 :   pari_sp ltop = avma;
    9676             :   GEN E4, E6, V, V1, Q, W, res, M, B;
    9677             :   long l, j;
    9678          14 :   l = k/6 + 2;
    9679          14 :   V = mfcoefsser(F,l);
    9680          14 :   E4 = mfcoefsser(mfEk(4),l);
    9681          14 :   E6 = mfcoefsser(mfEk(6),l);
    9682          14 :   V1 = gdiv(V, gpow(E4, sstoQ(k,4), 0));
    9683          14 :   Q = gdiv(E6, gpow(E4, sstoQ(3,2), 0));
    9684          14 :   W = gpowers(Q, l - 1);
    9685          14 :   M = cgetg(l + 1, t_MAT);
    9686          14 :   for (j = 1; j <= l; j++) gel(M,j) = sertocol2(gel(W,j), l);
    9687          14 :   B = sertocol2(V1, l);
    9688          14 :   res = inverseimage(M, B);
    9689          14 :   if (lg(res) == 1) err_space(F);
    9690          14 :   return gerepilecopy(ltop, gtopolyrev(res, 0));
    9691             : }
    9692             : 
    9693             : /* Compute the first n+1 Taylor coeffs at tau=I of a modular form
    9694             :  * on SL_2(Z). */
    9695             : GEN
    9696          14 : mftaylor(GEN F, long n, long flreal, long prec)
    9697             : {
    9698          14 :   pari_sp ltop = avma;
    9699          14 :   GEN P0, Pm1 = gen_0, v;
    9700          14 :   GEN X2 = mkpoln(3, ghalf,gen_0,gneg(ghalf)); /* (x^2-1) / 2 */
    9701             :   long k, m;
    9702          14 :   if (!checkmf_i(F)) pari_err_TYPE("mftaylor",F);
    9703          14 :   k = mf_get_k(F);
    9704          14 :   if (mf_get_N(F) != 1 || k < 0) pari_err_IMPL("mftaylor for this form");
    9705          14 :   P0 = mfcanfindp0(F, k);
    9706          14 :   v = cgetg(n+2, t_VEC); gel(v, 1) = RgX_coeff(P0,0);
    9707         154 :   for (m = 0; m < n; m++)
    9708             :   {
    9709         140 :     GEN P1 = gdivgs(gmulsg(-(k + 2*m), RgX_shift(P0,1)), 12);
    9710         140 :     P1 = gadd(P1, gmul(X2, RgX_deriv(P0)));
    9711         140 :     if (m) P1 = gsub(P1, gdivgs(gmulsg(m*(m+k-1), Pm1), 144));
    9712         140 :     Pm1 = P0; P0 = P1;
    9713         140 :     gel(v, m+2) = RgX_coeff(P0, 0);
    9714             :   }
    9715          14 :   if (flreal)
    9716             :   {
    9717           7 :     GEN pi2 = Pi2n(1, prec), pim4 = gmulsg(-2, pi2), VPC;
    9718           7 :     GEN C = gmulsg(3, gdiv(gpowgs(ggamma(ginv(utoi(4)), prec), 8), gpowgs(pi2, 6)));
    9719             :     /* E_4(i): */
    9720           7 :     GEN facn = gen_1;
    9721           7 :     VPC = gpowers(gmul(pim4, gsqrt(C, prec)), n);
    9722           7 :     C = gpow(C, sstoQ(k,4), prec);
    9723          84 :     for (m = 0; m <= n; m++)
    9724             :     {
    9725          77 :       gel(v, m+1) = gdiv(gmul(C, gmul(gel(v, m+1), gel(VPC, m+1))), facn);
    9726          77 :       facn = gmulgs(facn, m+1);
    9727             :     }
    9728             :   }
    9729          14 :   return gerepilecopy(ltop, v);
    9730             : }
    9731             : 
    9732             : #if 0
    9733             : /* To be used in mfeigensearch() */
    9734             : GEN
    9735             : mfreadratfile()
    9736             : {
    9737             :   GEN eqn;
    9738             :   pariFILE *F = pari_fopengz("rateigen300.gp");
    9739             :   eqn = gp_readvec_stream(F->file);
    9740             :   pari_fclose(F);
    9741             :   return eqn;
    9742             : }
    9743             : #endif
    9744             :  /*****************************************************************/
    9745             : /*           EISENSTEIN CUSPS: COMPLEX DIRECTLY: one F_k         */
    9746             : /*****************************************************************/
    9747             : 
    9748             : /* CHIvec = charinit(CHI); data = [N1g/g1,N2g/g2,g1/g,g2/g,C/g1,C/g2,
    9749             :  * (N1g/g1)^{-1},(N2g/g2)^{-1}] */
    9750             : 
    9751             : /* nm = n/m;
    9752             :  * z1 = powers of \z_{C/g}^{(Ae/g)^{-1}},
    9753             :  * z2 = powers of \z_N^{A^{-1}(g1g2/C)}]
    9754             :  * N.B. : we compute value and conjugate at the end, so it is (Ae/g)^{-1}
    9755             :  * and not -(Ae/g)^{-1} */
    9756             : static GEN
    9757     5903170 : eiscnm(long nm, long m, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1)
    9758             : {
    9759     5903170 :   long Cg1 = data[5], s10 = (nm*data[7]) % Cg1, r10 = (nm - data[1]*s10) / Cg1;
    9760     5903170 :   long Cg2 = data[6], s20 = (m *data[8]) % Cg2, r20 = (m  - data[2]*s20) / Cg2;
    9761             :   long j1, r1, s1;
    9762     5903170 :   GEN T = gen_0;
    9763    14927038 :   for (j1 = 0, r1 = r10, s1 = s10; j1 < data[3]; j1++, r1 -= data[1], s1 += Cg1)
    9764             :   {
    9765     9023868 :     GEN c1 = mychareval(CHI1vec, r1);
    9766     9023868 :     if (!gequal0(c1))
    9767             :     {
    9768             :       long j2, r2, s2;
    9769     6173426 :       GEN S = gen_0;
    9770    17004736 :       for (j2 = 0, r2 = r20, s2 = s20; j2 < data[4]; j2++, r2 -= data[2], s2 += Cg2)
    9771             :       {
    9772    10831310 :         GEN c2 = mychareval(CHI2vec, r2);
    9773    10831310 :         if (!gequal0(c2)) S = gadd(S, gmul(c2, rootsof1pow(z1, s1*s2)));
    9774             :       }
    9775     6173426 :       T = gadd(T, gmul(c1, S));
    9776             :     }
    9777             :   }
    9778     5903170 :   return conj_i(T);
    9779             : }
    9780             : 
    9781             : static GEN
    9782      461986 : fg1g2n(long n, long k, GEN CHI1vec, GEN CHI2vec, GEN data, GEN z1, GEN z2)
    9783             : {
    9784      461986 :   pari_sp av = avma;
    9785      461986 :   GEN S = gen_0, D = mydivisorsu(n);
    9786      461986 :   long i, l = lg(D);
    9787     3413571 :   for (i = 1; i < l; i++)
    9788             :   {
    9789     2951585 :     long m = D[i], nm = D[l-i]; /* n/m */
    9790     2951585 :     GEN u = eiscnm( nm,  m, CHI1vec, CHI2vec, data, z1);
    9791     2951585 :     GEN v = eiscnm(-nm, -m, CHI1vec, CHI2vec, data, z1);
    9792     2951585 :     GEN w = odd(k) ? gsub(u, v) : gadd(u, v);
    9793     2951585 :     S = gadd(S, gmul(powuu(m, k-1), w));
    9794             :   }
    9795      461986 :   return gerepileupto(av, gmul(S, rootsof1pow(z2, n)));
    9796             : }
    9797             : 
    9798             : static GEN
    9799       11375 : gausssumcx(GEN CHIvec, long prec)
    9800             : {
    9801       11375 :   GEN z, S, V = CHIvec_val(CHIvec);
    9802       11375 :   long m, N = CHIvec_N(CHIvec);
    9803       11375 :   z = rootsof1u_cx(N, prec);
    9804       11375 :   S = gmul(z, gel(V, N));
    9805       11375 :   for (m = N-1; m >= 1; m--) S = gmul(z, gadd(gel(V, m), S));
    9806       11375 :   return S;
    9807             : }
    9808             : 
    9809             : /* Computation of Q_k(\z_N^s) as a polynomial in \z_N^s. FIXME: explicit
    9810             :  * formula ? */
    9811             : static GEN
    9812        1729 : mfqk(long k, long N)
    9813             : {
    9814        1729 :   GEN X = pol_x(0), P = gsubgs(gpowgs(X,N), 1), ZI, Q, Xm1, invden;
    9815             :   long i;
    9816        1729 :   ZI = cgetg(N, t_VEC);
    9817        1729 :   for (i = 1; i < N; i++) gel(ZI, i) = utoi(i);
    9818        1729 :   ZI = gdivgs(gmul(X, gtopolyrev(ZI, 0)), N);
    9819        1729 :   if (k == 1) return ZI;
    9820        1071 :   invden = RgXQ_powu(ZI, k, P);
    9821        1071 :   Q = gneg(X); Xm1 = gsubgs(X, 1);
    9822        2716 :   for (i = 2; i < k; i++)
    9823        1645 :     Q = gmul(X, ZX_add(gmul(Xm1, ZX_deriv(Q)), gmulsg(-i, Q)));
    9824        1071 :   return RgXQ_mul(Q, invden, P);
    9825             : }
    9826             : /* CHI mfchar */
    9827             : /* Warning: M is a multiple of the conductor of CHI, but is NOT
    9828             :    necessarily its modulus */
    9829             : 
    9830             : static GEN
    9831        2513 : mfskcx(long k, GEN CHI, long M, long prec)
    9832             : {
    9833             :   GEN S, CHIvec, P;
    9834             :   long F, m, i, l;
    9835        2513 :   CHI = mfchartoprimitive(CHI, &F);
    9836        2513 :   CHIvec = mfcharcxinit(CHI, prec);
    9837        2513 :   if (F == 1) S = gdivgs(bernfrac(k), k);
    9838             :   else
    9839             :   {
    9840        1729 :     GEN Q = mfqk(k, F), V = CHIvec_val(CHIvec);
    9841        1729 :     S = gmul(gel(V, F), RgX_coeff(Q, 0));
    9842        1729 :     for (m = 1; m < F; m++) S = gadd(S, gmul(gel(V, m), RgX_coeff(Q, m)));
    9843        1729 :     S = conj_i(S);
    9844             :   }
    9845             :   /* prime divisors of M not dividing f(chi) */
    9846        2513 :   P = gel(myfactoru(u_ppo(M/F,F)), 1); l = lg(P);
    9847        2639 :   for (i = 1; i < l; i++)
    9848             :   {
    9849         126 :     long p = P[i];
    9850         126 :     S = gmul(S, gsubsg(1, gdiv(mychareval(CHIvec, p), powuu(p, k))));
    9851             :   }
    9852        2513 :   return gmul(gmul(gausssumcx(CHIvec, prec), S), powuu(M/F, k));
    9853             : }
    9854             : 
    9855             : static GEN
    9856        4634 : f00_i(long k, GEN CHI1vec, GEN CHI2vec, GEN G2, GEN S, long prec)
    9857             : {
    9858             :   GEN c, a;
    9859        4634 :   long N1 = CHIvec_N(CHI1vec), N2 = CHIvec_N(CHI2vec);
    9860        4634 :   if (S[2] != N1) return gen_0;
    9861        2513 :   c = mychareval(CHI1vec, S[3]);
    9862        2513 :   if (isintzero(c)) return gen_0;
    9863        2513 :   a = mfskcx(k, mfchardiv(CHIvec_CHI(CHI2vec), CHIvec_CHI(CHI1vec)), N1*N2, prec);
    9864        2513 :   a = gmul(a, conj_i(gmul(c,G2)));
    9865        2513 :   return gdiv(a, mulsi(-N2, powuu(S[1], k-1)));
    9866             : }
    9867             : 
    9868             : static GEN
    9869        3857 : f00(long k, GEN CHI1vec,GEN CHI2vec, GEN G1,GEN G2, GEN data, long prec)
    9870             : {
    9871             :   GEN T1, T2;
    9872        3857 :   T2 = f00_i(k, CHI1vec, CHI2vec, G2, data, prec);
    9873        3857 :   if (k > 1) return T2;
    9874         777 :   T1 = f00_i(k, CHI2vec, CHI1vec, G1, data, prec);
    9875         777 :   return gadd(T1, T2);
    9876             : }
    9877             : 
    9878             : /* ga in SL_2(Z), find beta [a,b;c,d] in Gamma_0(N) and mu in Z such that
    9879             :  * beta * ga * T^u = [A',B';C',D'] with C' | N and N | B', C' > 0 */
    9880             : static void
    9881        4431 : mfgatogap(GEN ga, long N, long *pA, long *pC, long *pD, long *pd, long *pmu)
    9882             : {
    9883        4431 :   GEN A = gcoeff(ga,1,1), B = gcoeff(ga,1,2);
    9884        4431 :   GEN C = gcoeff(ga,2,1), D = gcoeff(ga,2,2), a, b, c, d;
    9885             :   long t, Ap, Cp, B1, D1, mu;
    9886        4431 :   Cp = itou(bezout(muliu(A,N), C, &c, &d)); /* divides N */
    9887        4431 :   t = 0;
    9888        4431 :   if (Cp > 1)
    9889             :   { /* (d, N/Cp) = 1, find t such that (d - t*(A*N/Cp), N) = 1 */
    9890        2345 :     long dN = umodiu(d,Cp), Q = (N/Cp * umodiu(A,Cp)) % Cp;
    9891        2345 :     while (ugcd(dN, Cp) > 1) { t++; dN = Fl_sub(dN, Q, Cp); }
    9892             :   }
    9893        4431 :   if (t)
    9894             :   {
    9895         371 :     c = addii(c, mului(t, diviuexact(C,Cp)));
    9896         371 :     d = subii(d, mului(t, muliu(A, N/Cp))); /* (d,N) = 1 */
    9897             :   }
    9898        4431 :   D1 = umodiu(mulii(d,D), N);
    9899        4431 :   (void)bezout(d, mulis(c,-N), &a, &b); /* = 1 */
    9900        4431 :   t = 0; Ap = umodiu(addii(mulii(a,A), mulii(b,C)), N); /* (Ap,Cp) = 1 */
    9901        4431 :   while (ugcd(Ap, N) > 1) { t++; Ap = Fl_add(Ap, Cp, N); }
    9902        4431 :   B1 = umodiu(a,N)*umodiu(B,N) + umodiu(b,N)*umodiu(D,N) + t*D1;
    9903        4431 :   B1 %= N;
    9904        4431 :   *pmu = mu = Fl_neg(Fl_div(B1, Ap, N), N);
    9905             :   /* A', D' and d only needed modulo N */
    9906        4431 :   *pd = umodiu(d, N);
    9907        4431 :   *pA = Ap;
    9908        4431 :   *pC = Cp; *pD = (D1 + Cp*mu) % N;
    9909        4431 : }
    9910             : 
    9911             : #if 0
    9912             : /* CHI is a mfchar, return alpha(CHI) */
    9913             : static long
    9914             : mfalchi(GEN CHI, long AN, long cg)
    9915             : {
    9916             :   GEN G = gel(CHI,1), chi = gel(CHI,2), go = gmfcharorder(CHI);
    9917             :   long o = itou(go), a = itos( znchareval(G, chi, stoi(1 + AN/cg), go) );
    9918             :   if (a < 0 || (cg * a) % o) pari_err_BUG("mfalchi");
    9919             :   return (cg * a) / o;
    9920             : }
    9921             : #endif
    9922             : /* return A such that CHI1(t) * CHI2(t) = e(A) or NULL if (t,N1*N2) > 1 */
    9923             : static GEN
    9924        8862 : mfcharmuleval(GEN CHI1vec, GEN CHI2vec, long t)
    9925             : {
    9926        8862 :   long a1 = mycharexpo(CHI1vec, t), o1 = CHIvec_ord(CHI1vec);
    9927        8862 :   long a2 = mycharexpo(CHI2vec, t), o2 = CHIvec_ord(CHI2vec);;
    9928        8862 :   if (a1 < 0 || a2 < 0) return NULL;
    9929        8862 :   return sstoQ(a1*o2 + a2*o1, o1*o2);
    9930             : }
    9931             : static GEN
    9932        4431 : mfcharmulcxeval(GEN CHI1vec, GEN CHI2vec, long t, long prec)
    9933             : {
    9934        4431 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, t);
    9935             :   long n, d;
    9936        4431 :   if (!A) return gen_0;
    9937        4431 :   Qtoss(A, &n,&d); return rootsof1q_cx(n, d, prec);
    9938             : }
    9939             : /* alpha(CHI1 * CHI2) */
    9940             : static long
    9941        4431 : mfalchi2(GEN CHI1vec, GEN CHI2vec, long AN, long cg)
    9942             : {
    9943        4431 :   GEN A = mfcharmuleval(CHI1vec, CHI2vec, 1 + AN/cg);
    9944             :   long a;
    9945        4431 :   if (!A) pari_err_BUG("mfalchi2");
    9946        4431 :   A = gmulsg(cg, A);
    9947        4431 :   if (typ(A) != t_INT) pari_err_BUG("mfalchi2");
    9948        4431 :   a = itos(A) % cg; if (a < 0) a += cg;
    9949        4431 :   return a;
    9950             : }
    9951             : 
    9952             : /* return g = (a,b), set u >= 0 s.t. g = a * u (mod b) */
    9953             : static long
    9954       17724 : mybezout(long a, long b, long *pu)
    9955             : {
    9956       17724 :   long junk, g = cbezout(a, b, pu, &junk);
    9957       17724 :   if (*pu < 0) *pu += b/g;
    9958       17724 :   return g;
    9959             : }
    9960             : 
    9961             : /* E = [k, CHI1,CHI2, e], CHI1 and CHI2 primitive mfchars such that,
    9962             :  * CHI1(-1)*CHI2(-1) = (-1)^k; expansion of (B_e (E_k(CHI1,CHI2))) | ga.
    9963             :  * w is the width for the space of the calling function. */
    9964             : static GEN
    9965        4431 : mfeisensteingacx(GEN E, long w, GEN ga, long lim, long prec)
    9966             : {
    9967        4431 :   GEN CHI1vec, CHI2vec, CHI1 = gel(E,2), CHI2 = gel(E,3), v, S, ALPHA;
    9968             :   GEN G1, G2, z1, z2, data;
    9969        4431 :   long k = itou(gel(E,1)), e = itou(gel(E,4));
    9970        4431 :   long N1 = mfcharmodulus(CHI1);
    9971        4431 :   long N2 = mfcharmodulus(CHI2), N = e * N1 * N2;
    9972             :   long NsurC, cg, wN, A, C, Ai, d, mu, alchi, na, da;
    9973             :   long eg, g, gH, U, u0, u1, u2, Aig, H, m, n, t, Cg, NC1, NC2;
    9974             : 
    9975        4431 :   mfgatogap(ga, N, &A, &C, &Ai, &d, &mu);
    9976        4431 :   CHI1vec = mfcharcxinit(CHI1, prec);
    9977        4431 :   CHI2vec = mfcharcxinit(CHI2, prec);
    9978        4431 :   NsurC = N/C; cg  = ugcd(C, NsurC); wN = NsurC / cg;
    9979        4431 :   if (w%wN) pari_err_BUG("mfeisensteingacx [wN does not divide w]");
    9980        4431 :   alchi = mfalchi2(CHI1vec, CHI2vec, A*N, cg);
    9981        4431 :   ALPHA = sstoQ(alchi, NsurC);
    9982             : 
    9983        4431 :   g = mybezout(A*e, C, &u0); Cg = C/g; eg = e/g;
    9984        4431 :   NC1 = mybezout(N1, Cg, &u1);
    9985        4431 :   NC2 = mybezout(N2, Cg, &u2);
    9986        4431 :   H = (NC1*NC2*g)/Cg;
    9987        4431 :   Aig = (Ai*H)%N; if (Aig < 0) Aig += N;
    9988        4431 :   z1 = rootsof1powinit(u0, Cg, prec);
    9989        4431 :   z2 = rootsof1powinit(Aig, N, prec);
    9990        4431 :   data = mkvecsmalln(8, N1/NC1, N2/NC2, NC1, NC2, Cg/NC1, Cg/NC2, u1, u2);
    9991        4431 :   v = zerovec(lim + 1);
    9992             :   /* need n*H = alchi (mod cg) */
    9993        4431 :   gH = mybezout(H, cg, &U);
    9994        4431 :   if (gH > 1)
    9995             :   {
    9996         357 :     if (alchi % gH) return mkvec2(gen_0, v);
    9997         357 :     alchi /= gH; cg /= gH; H /= gH;
    9998             :   }
    9999        4431 :   G1 = gausssumcx(CHI1vec, prec);
   10000        4431 :   G2 = gausssumcx(CHI2vec, prec);
   10001        4431 :   if (!alchi)
   10002        3857 :     gel(v,1) = f00(k, CHI1vec,CHI2vec,G1,G2, mkvecsmall3(NC2,Cg,A*eg), prec);
   10003        4431 :   n = Fl_mul(alchi,U,cg); if (!n) n = cg;
   10004        4431 :   m = (n*H - alchi) / cg; /* positive, exact division */
   10005      466417 :   for (; m <= lim; n+=cg, m+=H)
   10006      461986 :     gel(v, m+1) = fg1g2n(n, k, CHI1vec, CHI2vec, data, z1,z2);
   10007        4431 :   t = (2*e)/g; if (odd(k)) t = -t;
   10008        4431 :   v = gdiv(v, gmul(conj_i(gmul(G1,G2)), mulsi(t, powuu(eg*N2/NC2, k-1))));
   10009        4431 :   if (k == 2 && N1 == 1 && N2 == 1) v = gsub(mkF2bd(wN,lim), gmulsg(e,v));
   10010             : 
   10011        4431 :   Qtoss(ALPHA, &na,&da);
   10012        4431 :   S = conj_i( mfcharmulcxeval(CHI1vec,CHI2vec,d,prec) ); /* CHI(1/d) */
   10013        4431 :   if (wN > 1)
   10014             :   {
   10015        3290 :     GEN z = rootsof1powinit(-mu, wN, prec);
   10016        3290 :     long i, l = lg(v);
   10017        3290 :     for (i = 1; i < l; i++) gel(v,i) = gmul(gel(v,i), rootsof1pow(z,i-1));
   10018             :   }
   10019        4431 :   v = RgV_Rg_mul(v, gmul(S, rootsof1q_cx(-mu*na, da, prec)));
   10020        4431 :   return mkvec2(ALPHA, bdexpand(v, w/wN));
   10021             : }
   10022             : 
   10023             : /*****************************************************************/
   10024             : /*                       END EISENSTEIN CUSPS                    */
   10025             : /*****************************************************************/
   10026             : 
   10027             : static GEN
   10028        1582 : mfchisimpl(GEN CHI)
   10029             : {
   10030             :   GEN G, chi;
   10031        1582 :   if (typ(CHI) == t_INT) return CHI;
   10032        1582 :   G = gel(CHI, 1); chi = gel(CHI, 2);
   10033        1582 :   switch(mfcharorder(CHI))
   10034             :   {
   10035        1134 :     case 1: chi = gen_1; break;
   10036         427 :     case 2: chi = znchartokronecker(G,chi,1); break;
   10037          21 :     default:chi = mkintmod(znconreyexp(G,chi), znstar_get_N(G)); break;
   10038             :   }
   10039        1582 :   return chi;
   10040             : }
   10041             : 
   10042             : GEN
   10043         700 : mfparams(GEN F)
   10044             : {
   10045         700 :   pari_sp av = avma;
   10046             :   GEN z, mf;
   10047         700 :   if ((mf = checkMF_i(F)))
   10048             :   {
   10049          14 :     long N = MF_get_N(mf);
   10050          14 :     GEN gk = MF_get_gk(mf);
   10051          14 :     z = mkvec4(utoi(N), gk, MF_get_CHI(mf), utoi(MF_get_space(mf)));
   10052             :   }
   10053             :   else
   10054             :   {
   10055         686 :     if (!checkmf_i(F)) pari_err_TYPE("mfparams", F);
   10056         686 :     z = shallowcopy( mf_get_NK(F) );
   10057             :   }
   10058         700 :   gel(z,3) = mfchisimpl(gel(z,3));
   10059         700 :   return gerepilecopy(av, z);
   10060             : }
   10061             : 
   10062             : GEN
   10063          14 : mfisCM(GEN F)
   10064             : {
   10065          14 :   pari_sp av = avma;
   10066             :   forprime_t S;
   10067             :   GEN D, v;
   10068             :   long N, k, lD, sb, p, i;
   10069          14 :   if (!checkmf_i(F)) pari_err_TYPE("mfisCM", F);
   10070          14 :   N = mf_get_N(F);
   10071          14 :   k = mf_get_k(F); if (N < 0 || k < 0) pari_err_IMPL("mfisCM for this F");
   10072          14 :   D = mfunram(N, -1);
   10073          14 :   lD = lg(D);
   10074          14 :   sb = maxss(mfsturmNk(N, k), 4*N);
   10075          14 :   v = mfcoefs_i(F, sb, 1);
   10076          14 :   u_forprime_init(&S, 2, sb);
   10077         518 :   while ((p = u_forprime_next(&S)))
   10078             :   {
   10079         490 :     GEN ap = gel(v, p+1);
   10080         490 :     if (!gequal0(ap))
   10081         406 :       for (i = 1; i < lD; i++)
   10082         245 :         if (kross(D[i], p) == -1) { D = vecsplice(D, i); lD--; }
   10083             :   }
   10084          14 :   if (lD == 1) { set_avma(av); return gen_0; }
   10085          14 :   if (lD == 2) { set_avma(av); return stoi(D[1]); }
   10086           7 :   if (k > 1) pari_err_BUG("mfisCM");
   10087           7 :   return gerepileupto(av, zv_to_ZV(D));
   10088             : }
   10089             : 
   10090             : static long
   10091         287 : mfspace_i(GEN mf, GEN F)
   10092             : {
   10093             :   GEN v, vF, gk;
   10094             :   long n, nE, i, l, s, N;
   10095             : 
   10096         287 :   mf = checkMF(mf); s = MF_get_space(mf);
   10097         287 :   if (!F) return s;
   10098         287 :   if (!checkmf_i(F)) pari_err_TYPE("mfspace",F);
   10099         287 :   v = mftobasis(mf, F, 1);
   10100         287 :   n = lg(v)-1; if (!n) return -1;
   10101         231 :   nE = lg(MF_get_E(mf))-1;
   10102         231 :   switch(s)
   10103             :   {
   10104          56 :     case mf_NEW: case mf_OLD: case mf_EISEN: return s;
   10105             :     case mf_FULL:
   10106         140 :       if (mf_get_type(F) == t_MF_THETA) return mf_EISEN;
   10107         133 :       if (!gequal0(vecslice(v,1,nE)))
   10108          63 :         return gequal0(vecslice(v,nE+1,n))? mf_EISEN: mf_FULL;
   10109             :   }
   10110             :   /* mf is mf_CUSP or mf_FULL, F a cusp form */
   10111         105 :   gk = mf_get_gk(F);
   10112         105 :   if (typ(gk) == t_FRAC || equali1(gk)) return mf_CUSP;
   10113          91 :   vF = mftonew_i(mf, vecslice(v, nE+1, n), &N);
   10114          91 :   if (N != MF_get_N(mf)) return mf_OLD;
   10115          63 :   l = lg(vF);
   10116         105 :   for (i = 1; i < l; i++)
   10117          63 :     if (itos(gmael(vF,i,1)) != N) return mf_CUSP;
   10118          42 :   return mf_NEW;
   10119             : }
   10120             : long
   10121         287 : mfspace(GEN mf, GEN F)
   10122         287 : { pari_sp av = avma; return gc_long(av, mfspace_i(mf,F)); }
   10123             : static GEN
   10124           7 : lfunfindchi(GEN ldata, GEN van, long prec)
   10125             : {
   10126           7 :   GEN gN = ldata_get_conductor(ldata), G = znstar0(gN,1), L, go, vz;
   10127           7 :   long k = ldata_get_k(ldata), N = itou(gN), bit = 10 - prec2nbits(prec);
   10128           7 :   long i, j, o, l, odd = k & 1, B0 = 2, B = lg(van)-1;
   10129             : 
   10130           7 :   van = shallowcopy(van);
   10131           7 :   L = cyc2elts(znstar_get_conreycyc(G));
   10132           7 :   l = lg(L);
   10133          21 :   for (i = j = 1; i < l; i++)
   10134             :   {
   10135          14 :     GEN chi = zc_to_ZC(gel(L,i));
   10136          14 :     if (zncharisodd(G,chi) == odd) gel(L,j++) = mfcharGL(G,chi);
   10137             :   }
   10138           7 :   setlg(L,j); l = j;
   10139           7 :   if (l <= 2) return gel(L,1);
   10140           0 :   o = znstar_get_expo(G); go = utoi(o);
   10141           0 :   vz = grootsof1(o, prec);
   10142             :   for (;;)
   10143           0 :   {
   10144             :     long n;
   10145           0 :     for (n = B0; n <= B; n++)
   10146             :     {
   10147           0 :       GEN an = gel(van,n), r;
   10148             :       long j;
   10149           0 :       if (ugcd(n, N) != 1 || gexpo(an) < bit) continue;
   10150           0 :       r = gdiv(an, conj_i(an));
   10151           0 :       for (i = 1; i < l; i++)
   10152             :       {
   10153           0 :         GEN CHI = gel(L,i);
   10154           0 :         if (gexpo(gsub(r, gel(vz, znchareval_i(CHI,n,go)+1))) > bit)
   10155           0 :           gel(L,i) = NULL;
   10156             :       }
   10157           0 :       for (i = j = 1; i < l; i++)
   10158           0 :         if (gel(L,i)) gel(L,j++) = gel(L,i);
   10159           0 :       l = j; setlg(L,l);
   10160           0 :       if (l == 2) return gel(L,1);
   10161             :     }
   10162           0 :     B0 = B+1; B <<= 1;
   10163           0 :     van = ldata_vecan(ldata_get_an(ldata), B, prec);
   10164             :   }
   10165             : }
   10166             : 
   10167             : GEN
   10168           7 : mffromlfun(GEN L, long prec)
   10169             : {
   10170           7 :   pari_sp av = avma;
   10171           7 :   GEN ldata = lfunmisc_to_ldata_shallow(L), Vga = ldata_get_gammavec(ldata);
   10172             :   GEN van, a0, CHI, NK;
   10173             :   long k, N, space;
   10174           7 :   if (!gequal(Vga, mkvec2(gen_0, gen_1))) pari_err_TYPE("mffromlfun", L);
   10175           7 :   k = ldata_get_k(ldata);
   10176           7 :   N = itou(ldata_get_conductor(ldata));
   10177           7 :   van = ldata_vecan(ldata_get_an(ldata), mfsturmNk(N,k) + 2, prec);
   10178           7 :   CHI = lfunfindchi(ldata, van, prec);
   10179           7 :   space = (lg(ldata) == 7)? mf_CUSP: mf_FULL;
   10180           7 :   a0 = (space == mf_CUSP)? gen_0: gneg(lfun(L, gen_0, prec2nbits(prec)));
   10181           7 :   NK = mkvec3(utoi(N), utoi(k), mfchisimpl(CHI));
   10182           7 :   return gerepilecopy(av, mkvec3(NK, utoi(space), shallowconcat(a0, van)));
   10183             : }
   10184             : /*******************************************************************/
   10185             : /*                                                                 */
   10186             : /*                       HALF-INTEGRAL WEIGHT                      */
   10187             : /*                                                                 */
   10188             : /*******************************************************************/
   10189             : /* We use the prefix mf2; k represents the weight -1/2, so e.g.
   10190             :    k = 2 is weight 5/2. N is the level, so 4\mid N, and CHI is the
   10191             :    character, always even. */
   10192             : 
   10193             : static long
   10194        3360 : lamCO(long r, long s, long p)
   10195             : {
   10196        3360 :   if ((s << 1) <= r)
   10197             :   {
   10198        1232 :     long rp = r >> 1;
   10199        1232 :     if (odd(r)) return upowuu(p, rp) << 1;
   10200         336 :     else return (p + 1)*upowuu(p, rp - 1);
   10201             :   }
   10202        2128 :   else return upowuu(p, r - s) << 1;
   10203             : }
   10204             : 
   10205             : static int
   10206        1568 : condC(GEN faN, GEN valF)
   10207             : {
   10208        1568 :   GEN P = gel(faN, 1), E = gel(faN, 2);
   10209        1568 :   long l = lg(P), i;
   10210        3696 :   for (i = 1; i < l; i++)
   10211        3024 :     if ((P[i] & 3L) == 3)
   10212             :     {
   10213        1120 :       long r = E[i];
   10214        1120 :       if (odd(r) || r < (valF[i] << 1)) return 1;
   10215             :     }
   10216         672 :   return 0;
   10217             : }
   10218             : 
   10219             : /* returns 2*zetaCO; weight is k + 1/2 */
   10220             : static long
   10221        3696 : zeta2CO(GEN faN, GEN valF, long r2, long s2, long k)
   10222             : {
   10223        3696 :   if (r2 >= 4) return lamCO(r2, s2, 2) << 1;
   10224        2912 :   if (r2 == 3) return 6;
   10225        1568 :   if (condC(faN, valF)) return 4;
   10226         672 :   if (odd(k)) return s2 ? 3 : 5; else return s2 ? 5: 3;
   10227             : }
   10228             : 
   10229             : /* returns 4 times last term in formula */
   10230             : static long
   10231        3696 : dim22(long N, long F, long k)
   10232             : {
   10233        3696 :   pari_sp av = avma;
   10234        3696 :   GEN vF, faN = myfactoru(N), P = gel(faN, 1), E = gel(faN, 2);
   10235        3696 :   long i, D, l = lg(P);
   10236        3696 :   vF = cgetg(l, t_VECSMALL);
   10237        3696 :   for (i = 1; i < l; i++) vF[i] = u_lval(F, P[i]);
   10238        3696 :   D = zeta2CO(faN, vF, E[1], vF[1], k);
   10239        3696 :   for (i = 2; i < l; i++) D *= lamCO(E[i], vF[i], P[i]);
   10240        3696 :   return gc_long(av,D);
   10241             : }
   10242             : 
   10243             : /* PSI not necessarily primitive, of conductor F */
   10244             : static int
   10245       13846 : charistotallyeven(GEN PSI, long F)
   10246             : {
   10247       13846 :   pari_sp av = avma;
   10248       13846 :   GEN P = gel(myfactoru(F), 1);
   10249       13846 :   GEN G = gel(PSI,1), psi = gel(PSI,2);
   10250             :   long i;
   10251       14350 :   for (i = 1; i < lg(P); i++)
   10252             :   {
   10253         532 :     GEN psip = znchardecompose(G, psi, utoipos(P[i]));
   10254         532 :     if (zncharisodd(G, psip)) return gc_bool(av,0);
   10255             :   }
   10256       13818 :   return gc_bool(av,1);
   10257             : }
   10258             : 
   10259             : static GEN
   10260      299775 : get_PSI(GEN CHI, long t)
   10261             : {
   10262      299775 :   long r = t & 3L, t2 = (r == 2 || r == 3) ? t << 2 : t;
   10263      299775 :   return mfcharmul_i(CHI, induce(gel(CHI,1), utoipos(t2)));
   10264             : }
   10265             : /* space = mf_CUSP, mf_EISEN or mf_FULL, weight k + 1/2 */
   10266             : static long
   10267       41363 : mf2dimwt12(long N, GEN CHI, long space)
   10268             : {
   10269       41363 :   pari_sp av = avma;
   10270       41363 :   GEN D = mydivisorsu(N >> 2);
   10271       41363 :   long i, l = lg(D), dim3 = 0, dim4 = 0;
   10272             : 
   10273       41363 :   CHI = induceN(N, CHI);
   10274      341138 :   for (i = 1; i < l; i++)
   10275             :   {
   10276      299775 :     long rp, t = D[i], Mt = D[l-i];
   10277      299775 :     GEN PSI = get_PSI(CHI,t);
   10278      299775 :     rp = mfcharconductor(PSI);
   10279      299775 :     if (Mt % (rp*rp) == 0) { dim4++; if (charistotallyeven(PSI,rp)) dim3++; }
   10280             :   }
   10281       41363 :   set_avma(av);
   10282       41363 :   switch (space)
   10283             :   {
   10284       40439 :     case mf_CUSP: return dim4 - dim3;
   10285         462 :     case mf_EISEN:return dim3;
   10286         462 :     case mf_FULL: return dim4;
   10287             :   }
   10288             :   return 0; /*LCOV_EXCL_LINE*/
   10289             : }
   10290             : 
   10291             : static long
   10292         693 : mf2dimwt32(long N, GEN CHI, long F, long space)
   10293             : {
   10294             :   long D;
   10295         693 :   switch(space)
   10296             :   {
   10297         231 :     case mf_CUSP: D = mypsiu(N) - 6*dim22(N, F, 1);
   10298         231 :       if (D%24) pari_err_BUG("mfdim");
   10299         231 :       return D/24 + mf2dimwt12(N, CHI, 4);
   10300         231 :     case mf_FULL: D = mypsiu(N) + 6*dim22(N, F, 0);
   10301         231 :       if (D%24) pari_err_BUG("mfdim");
   10302         231 :       return D/24 + mf2dimwt12(N, CHI, 1);
   10303         231 :     case mf_EISEN: D = dim22(N, F, 0) + dim22(N, F, 1);
   10304         231 :       if (D & 3L) pari_err_BUG("mfdim");
   10305         231 :       return (D >> 2) - mf2dimwt12(N, CHI, 3);
   10306             :   }
   10307             :   return 0; /*LCOV_EXCL_LINE*/
   10308             : }
   10309             : 
   10310             : /* F = conductor(CHI), weight k = r+1/2 */
   10311             : static long
   10312       43673 : checkmf2(long N, long r, GEN CHI, long F, long space)
   10313             : {
   10314       43673 :   switch(space)
   10315             :   {
   10316       43652 :     case mf_FULL: case mf_CUSP: case mf_EISEN: break;
   10317             :     case mf_NEW: case mf_OLD:
   10318          14 :       pari_err_TYPE("half-integral weight [new/old spaces]", utoi(space));
   10319             :     default:
   10320           7 :       pari_err_TYPE("half-integral weight [incorrect space]",utoi(space));
   10321             :   }
   10322       43652 :   if (N & 3L)
   10323           0 :     pari_err_DOMAIN("half-integral weight", "N % 4", "!=", gen_0, stoi(N));
   10324       43652 :   return r >= 0 && mfcharparity(CHI) == 1 && N % F == 0;
   10325             : }
   10326             : 
   10327             : /* weight k = r + 1/2 */
   10328             : static long
   10329       43463 : mf2dim_Nkchi(long N, long r, GEN CHI, ulong space)
   10330             : {
   10331       43463 :   long D, D2, F = mfcharconductor(CHI);
   10332       43463 :   if (!checkmf2(N, r, CHI, F, space)) return 0;
   10333       43442 :   if (r == 0) return mf2dimwt12(N, CHI, space);
   10334        2772 :   if (r == 1) return mf2dimwt32(N, CHI, F, space);
   10335        2079 :   if (space == mf_EISEN)
   10336             :   {
   10337         693 :     D = dim22(N, F, r) + dim22(N, F, 1-r);
   10338         693 :     if (D & 3L) pari_err_BUG("mfdim");
   10339         693 :     return D >> 2;
   10340             :   }
   10341        1386 :   D2 = space == mf_FULL? dim22(N, F, 1-r): -dim22(N, F, r);
   10342        1386 :   D = (2*r-1)*mypsiu(N) + 6*D2;
   10343        1386 :   if (D%24) pari_err_BUG("mfdim");
   10344        1386 :   return D/24;
   10345             : }
   10346             : 
   10347             : /* weight k=r+1/2 */
   10348             : static GEN
   10349         210 : mf2init_Nkchi(long N, long r, GEN CHI, long space, long flraw)
   10350             : {
   10351         210 :   GEN Minv, Minvmat, B, M, gk = gaddsg(r,ghalf);
   10352         210 :   GEN mf1 = mkvec4(utoi(N),gk,CHI,utoi(space));
   10353             :   long L;
   10354         210 :   if (!checkmf2(N, r, CHI, mfcharconductor(CHI), space)) return mfEMPTY(mf1);
   10355         210 :   if (space==mf_EISEN) pari_err_IMPL("half-integral weight Eisenstein space");
   10356         210 :   L = mfsturmNgk(N, gk) + 1;
   10357         210 :   B = mf2basis(N, r, CHI, space);
   10358         210 :   M = mflineardivtomat(N,B,L);
   10359         210 :   if (flraw) M = mkvec3(gen_0,gen_0,M);
   10360             :   else
   10361             :   {
   10362         210 :     M = mfcleanCHI(M, CHI, 0);
   10363         210 :     Minv = gel(M,2);
   10364         210 :     Minvmat = RgM_Minv_mul(NULL, Minv);
   10365         210 :     B = vecmflineardiv_linear(B, Minvmat);
   10366         210 :     gel(M,3) = RgM_Minv_mul(gel(M,3), Minv);
   10367         210 :     gel(M,2) = mkMinv(matid(lg(B)-1), NULL,NULL,NULL);
   10368             :   }
   10369         210 :   return mkmf(mf1, cgetg(1,t_VEC), B, gen_0, M);
   10370             : }
   10371             : 
   10372             : /**************************************************************************/
   10373             : /*                          Kohnen + space                                */
   10374             : /**************************************************************************/
   10375             : 
   10376             : static GEN
   10377          21 : mfkohnenbasis_i(GEN mf, GEN CHIP, long eps, long sb)
   10378             : {
   10379          21 :   GEN M = shallowtrans(mfcoefs_mf(mf, sb, 1)), ME;
   10380             :   long c, i, n;
   10381          21 :   ME = cgetg(sb + 2, t_MAT);
   10382         784 :   for (i = 0, c = 1; i <= sb; i++)
   10383             :   {
   10384         763 :     long j = i & 3L;
   10385         763 :     if (j == 2 || j == 2 + eps) gel(ME, c++) = gel(M, i+1);
   10386             :   }
   10387          21 :   setlg(ME, c); ME = shallowtrans(Q_primpart(ME));
   10388          21 :   n = mfcharorder_canon(CHIP);
   10389          21 :   return n == 1? ZM_ker(ME): ZabM_ker(liftpol_shallow(ME), mfcharpol(CHIP), n);
   10390             : }
   10391             : GEN
   10392          21 : mfkohnenbasis(GEN mf)
   10393             : {
   10394          21 :   pari_sp av = avma;
   10395             :   GEN gk, CHI, CHIP, K;
   10396             :   long N4, r, eps, sb;
   10397          21 :   mf = checkMF(mf);
   10398          21 :   if (MF_get_space(mf) != mf_CUSP)
   10399           0 :     pari_err_TYPE("mfkohnenbasis [not a cuspidal space", mf);
   10400          21 :   if (!MF_get_dim(mf)) return cgetg(1, t_MAT);
   10401          21 :   N4 = MF_get_N(mf) >> 2; gk = MF_get_gk(mf); CHI = MF_get_CHI(mf);
   10402          21 :   if (typ(gk) == t_INT) pari_err_TYPE("mfkohnenbasis", gk);
   10403          21 :   r = MF_get_r(mf);
   10404          21 :   CHIP = mfcharchiliftprim(CHI, N4);
   10405          21 :   eps = CHIP==CHI? 1: -1;
   10406          21 :   if (!CHIP) pari_err_TYPE("mfkohnenbasis [incorrect CHI]", CHI);
   10407          21 :   if (odd(r)) eps = -eps;
   10408          21 :   if (uissquarefree(N4))
   10409             :   {
   10410          14 :     long d = mfdim_Nkchi(N4, 2*r, mfcharpow(CHI, gen_2), mf_CUSP);
   10411          14 :     sb = mfsturmNgk(N4 << 2, gk) + 1;
   10412          14 :     K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10413          14 :     if (lg(K) - 1 == d) return gerepilecopy(av, K);
   10414             :   }
   10415           7 :   sb = mfsturmNgk(N4 << 4, gk) + 1;
   10416           7 :   K = mfkohnenbasis_i(mf, CHIP, eps, sb);
   10417           7 :   return gerepilecopy(av, K);
   10418             : }
   10419             : 
   10420             : /* return [mf3, bijection, mfkohnenbasis, codeshi] */
   10421             : static GEN
   10422          14 : mfkohnenbijection_i(GEN mf)
   10423             : {
   10424          14 :   GEN vB, mf3, K, SHI, P, CHI = MF_get_CHI(mf);
   10425          14 :   long n, lK, i, dim, m, lw, sb3, N4 = MF_get_N(mf)>>2, r = MF_get_r(mf);
   10426          14 :   long Dp[] = {1, 5, 8, 12, 13, 17, 21, 24};
   10427          14 :   long Dm[] = {-3, -4, -7, -8, -11, -15, -19, -20}, *D = odd(r)? Dm: Dp;
   10428          14 :   const long nbD = 8, MAXm = 6560; /* #D, 3^#D - 1 */
   10429             : 
   10430          14 :   K = mfkohnenbasis(mf); lK = lg(K);
   10431          14 :   mf3 = mfinit_Nkchi(N4, r<<1, mfcharpow(CHI,gen_2), mf_CUSP, 0);
   10432          14 :   if (MF_get_dim(mf3) != lK - 1)
   10433           0 :     pari_err_BUG("mfkohnenbijection [different dimensions]");
   10434          14 :   if (lK == 1) return mkvec4(mf3, cgetg(1, t_MAT), K, cgetg(1, t_VEC));
   10435          14 :   CHI = mfcharchiliftprim(CHI, N4);
   10436          14 :   if (!CHI) pari_err_TYPE("mfkohnenbijection [incorrect CHI]", CHI);
   10437          14 :   n = mfcharorder_canon(CHI);
   10438          14 :   P = n==1? NULL: mfcharpol(CHI);
   10439          14 :   SHI = cgetg(nbD+1, t_VEC);
   10440          14 :   sb3 = mfsturm(mf3);
   10441          14 :   vB = RgM_mul(mfcoefs_mf(mf, labs(D[nbD-1])*sb3*sb3, 1), K);
   10442          14 :   dim = MF_get_dim(mf3);
   10443          35 :   for (m = 1, lw = 0; m <= MAXm; m += (m%3)? 2: 1)
   10444             :   {
   10445             :     pari_sp av;
   10446          35 :     ulong m1, y, v = u_lvalrem(m, 3, &y);
   10447             :     GEN z, M;
   10448             :     long j;
   10449          35 :     if (y == 1)
   10450             :     {
   10451          28 :       long d = D[v];
   10452          28 :       GEN a = cgetg(lK, t_MAT);
   10453          98 :       for (i = 1; i < lK; i++)
   10454             :       {
   10455          70 :         pari_sp av2 = avma;
   10456          70 :         GEN f = c_deflate(sb3*sb3, labs(d), gel(vB,i));
   10457          70 :         f = mftobasis_i(mf3, RgV_shimura(f, sb3, d, N4, r, CHI));
   10458          70 :         gel(a,i) = gerepileupto(av2, f);
   10459             :       }
   10460          28 :       lw++; gel(SHI,v+1) = a;
   10461             :     }
   10462          35 :     av = avma; M = NULL;
   10463          91 :     for (j = 1, m1 = m; j <= lw; j++, m1/=3)
   10464             :     {
   10465          56 :       long s = m1%3;
   10466          56 :       if (s)
   10467             :       {
   10468          42 :         GEN t = gel(SHI,j);
   10469          42 :         if (M) M = (s == 2)? RgM_sub(M, t): RgM_add(M, t);
   10470          35 :         else   M = (s == 2)? RgM_neg(t): t;
   10471             :       }
   10472             :     }
   10473          35 :     z = QabM_indexrank(M,P,n);
   10474          35 :     if (lg(gel(z,2)) > dim)
   10475             :     {
   10476          14 :       GEN d = ZV_to_zv( digits(utoipos(m), utoipos(3)) );
   10477          14 :       GEN mres, dMi, Mi = QabM_pseudoinv(M,P,n, NULL,&dMi);
   10478          14 :       long ld = lg(d), c = 1;
   10479          14 :       if (DEBUGLEVEL>1)
   10480           0 :         err_printf("mfkohnenbijection: used %ld discriminants\n",lw);
   10481          14 :       mres = cgetg(ld, t_VEC);
   10482          42 :       for (j = ld-1; j >= 1; j--)
   10483          28 :         if (d[j]) gel(mres,c++) = mkvec2s(D[ld-j-1], d[j]);
   10484          14 :       setlg(mres,c); return mkvec4(mf3, RgM_Rg_div(Mi,dMi), K, mres);
   10485             :     }
   10486          21 :     set_avma(av);
   10487             :   }
   10488           0 :   pari_err_BUG("mfkohnenbijection");
   10489             :   return NULL; /*LCOV_EXCL_LINE*/
   10490             : }
   10491             : GEN
   10492          14 : mfkohnenbijection(GEN mf)
   10493             : {
   10494          14 :   pari_sp av = avma;
   10495             :   long N;
   10496          14 :   mf = checkMF(mf); N = MF_get_N(mf);
   10497          14 :   if (!uissquarefree(N >> 2))
   10498           0 :     pari_err_TYPE("mfkohnenbijection [N/4 not squarefree]", utoi(N));
   10499          14 :   if (MF_get_space(mf) != mf_CUSP || MF_get_r(mf) == 0 || !mfshimura_space_cusp(mf))
   10500           0 :     pari_err_TYPE("mfkohnenbijection [incorrect mf for Kohnen]", mf);
   10501          14 :   return gerepilecopy(av, mfkohnenbijection_i(mf));
   10502             : }
   10503             : 
   10504             : static int
   10505           7 : checkbij_i(GEN b)
   10506             : {
   10507          14 :   return typ(b) == t_VEC && lg(b) == 5 && checkMF_i(gel(b,1))
   10508           7 :          && typ(gel(b,2)) == t_MAT
   10509           7 :          && typ(gel(b,3)) == t_MAT
   10510          14 :          && typ(gel(b,4)) == t_VEC;
   10511             : }
   10512             : 
   10513             : /* bij is the output of mfkohnenbijection */
   10514             : GEN
   10515           7 : mfkohneneigenbasis(GEN mf, GEN bij)
   10516             : {
   10517           7 :   pari_sp av = avma;
   10518             :   GEN mf3, mf30, B, KM, M, k;
   10519             :   long r, i, l, N4;
   10520           7 :   mf = checkMF(mf);
   10521           7 :   if (!checkbij_i(bij))
   10522           0 :     pari_err_TYPE("mfkohneneigenbasis [bijection]", bij);
   10523           7 :   if (MF_get_space(mf) != mf_CUSP)
   10524           0 :     pari_err_TYPE("mfkohneneigenbasis [not a cuspidal space]", mf);
   10525           7 :   if (!MF_get_dim(mf))
   10526           0 :     retmkvec3(cgetg(1, t_VEC), cgetg(1, t_VEC), cgetg(1, t_VEC));
   10527           7 :   N4 = MF_get_N(mf) >> 2; k = MF_get_gk(mf);
   10528           7 :   if (typ(k) == t_INT) pari_err_TYPE("mfkohneneigenbasis", k);
   10529           7 :   if (!uissquarefree(N4))
   10530           0 :     pari_err_TYPE("mfkohneneigenbasis [N not squarefree]", utoipos(N4));
   10531           7 :   r = MF_get_r(mf);
   10532           7 :   KM = RgM_mul(gel(bij,3), gel(bij,2));
   10533           7 :   mf3 = gel(bij,1);
   10534           7 :   mf30 = mfinit_Nkchi(N4, 2*r, MF_get_CHI(mf3), mf_NEW, 0);
   10535           7 :   B = mfcoefs_mf(mf30, mfsturm_mf(mf3), 1); l = lg(B);
   10536           7 :   M = cgetg(l, t_MAT);
   10537           7 :   for (i=1; i<l; i++) gel(M,i) = RgM_RgC_mul(KM, mftobasis_i(mf3, gel(B,i)));
   10538           7 :   return gerepilecopy(av, mkvec3(mf30, M, RgM_mul(M, MF_get_newforms(mf30))));
   10539             : }
   10540             : /*************************** End Kohnen ************************************/
   10541             : /***************************************************************************/
   10542             : 
   10543             : static GEN desc(GEN F);
   10544             : static GEN
   10545         504 : desc_mfeisen(GEN F)
   10546             : {
   10547         504 :   GEN R, gk = mf_get_gk(F);
   10548         504 :   if (typ(gk) == t_FRAC)
   10549           7 :     R = gsprintf("H_{%Ps}", gk);
   10550             :   else
   10551             :   {
   10552         497 :     GEN vchi = gel(F, 2), CHI = mfchisimpl(gel(vchi, 3));
   10553         497 :     long k = itou(gk);
   10554         497 :     if (lg(vchi) < 5) R = gsprintf("F_%ld(%Ps)", k, CHI);
   10555             :     else
   10556             :     {
   10557         294 :       GEN CHI2 = mfchisimpl(gel(vchi, 4));
   10558         294 :       R = gsprintf("F_%ld(%Ps, %Ps)", k, CHI, CHI2);
   10559             :     }
   10560             :   }
   10561         504 :   return R;
   10562             : }
   10563             : static GEN
   10564          35 : desc_hecke(GEN F)
   10565             : {
   10566             :   long n, N;
   10567          35 :   GEN D = gel(F,2);
   10568          35 :   if (typ(D) == t_VECSMALL) { N = D[3]; n = D[1]; }
   10569          14 :   else { GEN nN = gel(D,2); n = nN[1]; N = nN[2]; } /* half integer */
   10570          35 :   return gsprintf("T_%ld(%ld)(%Ps)", N, n, desc(gel(F,3)));
   10571             : }
   10572             : static GEN
   10573          98 : desc_linear(GEN FLD, GEN dL)
   10574             : {
   10575          98 :   GEN F = gel(FLD,2), L = gel(FLD,3), R = strtoGENstr("LIN([");
   10576          98 :   long n = lg(F) - 1, i;
   10577         168 :   for (i = 1; i <= n; i++)
   10578             :   {
   10579         168 :     R = shallowconcat(R, desc(gel(F,i))); if (i == n) break;
   10580          70 :     R = shallowconcat(R, strtoGENstr(", "));
   10581             :   }
   10582          98 :   return shallowconcat(R, gsprintf("], %Ps)", gdiv(L, dL)));
   10583             : }
   10584             : static GEN
   10585          21 : desc_dihedral(GEN F)
   10586             : {
   10587          21 :   GEN bnr = gel(F,2), D = nf_get_disc(bnr_get_nf(bnr)), f = bnr_get_mod(bnr);
   10588          21 :   GEN cyc = bnr_get_cyc(bnr);
   10589          21 :   GEN w = gel(F,3), chin = zv_to_ZV(gel(w,2)), o = utoi(gel(w,1)[1]);
   10590          21 :   GEN chi = char_denormalize(cyc, o, chin);
   10591          21 :   if (lg(gel(f,2)) == 1) f = gel(f,1);
   10592          21 :   return gsprintf("DIH(%Ps, %Ps, %Ps, %Ps)",D,f,cyc,chi);
   10593             : }
   10594             : 
   10595             : static vo